SDL_gpu_vulkan.c 453 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2025 Sam Lantinga <slouken@libsdl.org>
  4. This software is provided 'as-is', without any express or implied
  5. warranty. In no event will the authors be held liable for any damages
  6. arising from the use of this software.
  7. Permission is granted to anyone to use this software for any purpose,
  8. including commercial applications, and to alter it and redistribute it
  9. freely, subject to the following restrictions:
  10. 1. The origin of this software must not be misrepresented; you must not
  11. claim that you wrote the original software. If you use this software
  12. in a product, an acknowledgment in the product documentation would be
  13. appreciated but is not required.
  14. 2. Altered source versions must be plainly marked as such, and must not be
  15. misrepresented as being the original software.
  16. 3. This notice may not be removed or altered from any source distribution.
  17. */
  18. #include "SDL_internal.h"
  19. #ifdef SDL_GPU_VULKAN
  20. // Needed for VK_KHR_portability_subset
  21. #define VK_ENABLE_BETA_EXTENSIONS
  22. #define VK_NO_PROTOTYPES
  23. #include "../../video/khronos/vulkan/vulkan.h"
  24. #include <SDL3/SDL_vulkan.h>
  25. #include "../SDL_sysgpu.h"
  26. // Global Vulkan Loader Entry Points
  27. static PFN_vkGetInstanceProcAddr vkGetInstanceProcAddr = NULL;
  28. #define VULKAN_GLOBAL_FUNCTION(name) \
  29. static PFN_##name name = NULL;
  30. #include "SDL_gpu_vulkan_vkfuncs.h"
  31. typedef struct VulkanExtensions
  32. {
  33. // These extensions are required!
  34. // Globally supported
  35. Uint8 KHR_swapchain;
  36. // Core since 1.1, needed for negative VkViewport::height
  37. Uint8 KHR_maintenance1;
  38. // These extensions are optional!
  39. // Core since 1.2, but requires annoying paperwork to implement
  40. Uint8 KHR_driver_properties;
  41. // Only required for special implementations (i.e. MoltenVK)
  42. Uint8 KHR_portability_subset;
  43. // Only required to detect devices using Dozen D3D12 driver
  44. Uint8 MSFT_layered_driver;
  45. // Only required for decoding HDR ASTC textures
  46. Uint8 EXT_texture_compression_astc_hdr;
  47. } VulkanExtensions;
  48. // Defines
  49. #define SMALL_ALLOCATION_THRESHOLD 2097152 // 2 MiB
  50. #define SMALL_ALLOCATION_SIZE 16777216 // 16 MiB
  51. #define LARGE_ALLOCATION_INCREMENT 67108864 // 64 MiB
  52. #define MAX_UBO_SECTION_SIZE 4096 // 4 KiB
  53. #define DESCRIPTOR_POOL_SIZE 128
  54. #define WINDOW_PROPERTY_DATA "SDL_GPUVulkanWindowPropertyData"
  55. #define IDENTITY_SWIZZLE \
  56. { \
  57. VK_COMPONENT_SWIZZLE_IDENTITY, \
  58. VK_COMPONENT_SWIZZLE_IDENTITY, \
  59. VK_COMPONENT_SWIZZLE_IDENTITY, \
  60. VK_COMPONENT_SWIZZLE_IDENTITY \
  61. }
  62. // Conversions
  63. static VkPresentModeKHR SDLToVK_PresentMode[] = {
  64. VK_PRESENT_MODE_FIFO_KHR,
  65. VK_PRESENT_MODE_IMMEDIATE_KHR,
  66. VK_PRESENT_MODE_MAILBOX_KHR
  67. };
  68. static VkFormat SDLToVK_TextureFormat[] = {
  69. VK_FORMAT_UNDEFINED, // INVALID
  70. VK_FORMAT_R8_UNORM, // A8_UNORM
  71. VK_FORMAT_R8_UNORM, // R8_UNORM
  72. VK_FORMAT_R8G8_UNORM, // R8G8_UNORM
  73. VK_FORMAT_R8G8B8A8_UNORM, // R8G8B8A8_UNORM
  74. VK_FORMAT_R16_UNORM, // R16_UNORM
  75. VK_FORMAT_R16G16_UNORM, // R16G16_UNORM
  76. VK_FORMAT_R16G16B16A16_UNORM, // R16G16B16A16_UNORM
  77. VK_FORMAT_A2B10G10R10_UNORM_PACK32, // R10G10B10A2_UNORM
  78. VK_FORMAT_R5G6B5_UNORM_PACK16, // B5G6R5_UNORM
  79. VK_FORMAT_A1R5G5B5_UNORM_PACK16, // B5G5R5A1_UNORM
  80. VK_FORMAT_B4G4R4A4_UNORM_PACK16, // B4G4R4A4_UNORM
  81. VK_FORMAT_B8G8R8A8_UNORM, // B8G8R8A8_UNORM
  82. VK_FORMAT_BC1_RGBA_UNORM_BLOCK, // BC1_UNORM
  83. VK_FORMAT_BC2_UNORM_BLOCK, // BC2_UNORM
  84. VK_FORMAT_BC3_UNORM_BLOCK, // BC3_UNORM
  85. VK_FORMAT_BC4_UNORM_BLOCK, // BC4_UNORM
  86. VK_FORMAT_BC5_UNORM_BLOCK, // BC5_UNORM
  87. VK_FORMAT_BC7_UNORM_BLOCK, // BC7_UNORM
  88. VK_FORMAT_BC6H_SFLOAT_BLOCK, // BC6H_FLOAT
  89. VK_FORMAT_BC6H_UFLOAT_BLOCK, // BC6H_UFLOAT
  90. VK_FORMAT_R8_SNORM, // R8_SNORM
  91. VK_FORMAT_R8G8_SNORM, // R8G8_SNORM
  92. VK_FORMAT_R8G8B8A8_SNORM, // R8G8B8A8_SNORM
  93. VK_FORMAT_R16_SNORM, // R16_SNORM
  94. VK_FORMAT_R16G16_SNORM, // R16G16_SNORM
  95. VK_FORMAT_R16G16B16A16_SNORM, // R16G16B16A16_SNORM
  96. VK_FORMAT_R16_SFLOAT, // R16_FLOAT
  97. VK_FORMAT_R16G16_SFLOAT, // R16G16_FLOAT
  98. VK_FORMAT_R16G16B16A16_SFLOAT, // R16G16B16A16_FLOAT
  99. VK_FORMAT_R32_SFLOAT, // R32_FLOAT
  100. VK_FORMAT_R32G32_SFLOAT, // R32G32_FLOAT
  101. VK_FORMAT_R32G32B32A32_SFLOAT, // R32G32B32A32_FLOAT
  102. VK_FORMAT_B10G11R11_UFLOAT_PACK32, // R11G11B10_UFLOAT
  103. VK_FORMAT_R8_UINT, // R8_UINT
  104. VK_FORMAT_R8G8_UINT, // R8G8_UINT
  105. VK_FORMAT_R8G8B8A8_UINT, // R8G8B8A8_UINT
  106. VK_FORMAT_R16_UINT, // R16_UINT
  107. VK_FORMAT_R16G16_UINT, // R16G16_UINT
  108. VK_FORMAT_R16G16B16A16_UINT, // R16G16B16A16_UINT
  109. VK_FORMAT_R32_UINT, // R32_UINT
  110. VK_FORMAT_R32G32_UINT, // R32G32_UINT
  111. VK_FORMAT_R32G32B32A32_UINT, // R32G32B32A32_UINT
  112. VK_FORMAT_R8_SINT, // R8_INT
  113. VK_FORMAT_R8G8_SINT, // R8G8_INT
  114. VK_FORMAT_R8G8B8A8_SINT, // R8G8B8A8_INT
  115. VK_FORMAT_R16_SINT, // R16_INT
  116. VK_FORMAT_R16G16_SINT, // R16G16_INT
  117. VK_FORMAT_R16G16B16A16_SINT, // R16G16B16A16_INT
  118. VK_FORMAT_R32_SINT, // R32_INT
  119. VK_FORMAT_R32G32_SINT, // R32G32_INT
  120. VK_FORMAT_R32G32B32A32_SINT, // R32G32B32A32_INT
  121. VK_FORMAT_R8G8B8A8_SRGB, // R8G8B8A8_UNORM_SRGB
  122. VK_FORMAT_B8G8R8A8_SRGB, // B8G8R8A8_UNORM_SRGB
  123. VK_FORMAT_BC1_RGBA_SRGB_BLOCK, // BC1_UNORM_SRGB
  124. VK_FORMAT_BC2_SRGB_BLOCK, // BC3_UNORM_SRGB
  125. VK_FORMAT_BC3_SRGB_BLOCK, // BC3_UNORM_SRGB
  126. VK_FORMAT_BC7_SRGB_BLOCK, // BC7_UNORM_SRGB
  127. VK_FORMAT_D16_UNORM, // D16_UNORM
  128. VK_FORMAT_X8_D24_UNORM_PACK32, // D24_UNORM
  129. VK_FORMAT_D32_SFLOAT, // D32_FLOAT
  130. VK_FORMAT_D24_UNORM_S8_UINT, // D24_UNORM_S8_UINT
  131. VK_FORMAT_D32_SFLOAT_S8_UINT, // D32_FLOAT_S8_UINT
  132. VK_FORMAT_ASTC_4x4_UNORM_BLOCK, // ASTC_4x4_UNORM
  133. VK_FORMAT_ASTC_5x4_UNORM_BLOCK, // ASTC_5x4_UNORM
  134. VK_FORMAT_ASTC_5x5_UNORM_BLOCK, // ASTC_5x5_UNORM
  135. VK_FORMAT_ASTC_6x5_UNORM_BLOCK, // ASTC_6x5_UNORM
  136. VK_FORMAT_ASTC_6x6_UNORM_BLOCK, // ASTC_6x6_UNORM
  137. VK_FORMAT_ASTC_8x5_UNORM_BLOCK, // ASTC_8x5_UNORM
  138. VK_FORMAT_ASTC_8x6_UNORM_BLOCK, // ASTC_8x6_UNORM
  139. VK_FORMAT_ASTC_8x8_UNORM_BLOCK, // ASTC_8x8_UNORM
  140. VK_FORMAT_ASTC_10x5_UNORM_BLOCK, // ASTC_10x5_UNORM
  141. VK_FORMAT_ASTC_10x6_UNORM_BLOCK, // ASTC_10x6_UNORM
  142. VK_FORMAT_ASTC_10x8_UNORM_BLOCK, // ASTC_10x8_UNORM
  143. VK_FORMAT_ASTC_10x10_UNORM_BLOCK, // ASTC_10x10_UNORM
  144. VK_FORMAT_ASTC_12x10_UNORM_BLOCK, // ASTC_12x10_UNORM
  145. VK_FORMAT_ASTC_12x12_UNORM_BLOCK, // ASTC_12x12_UNORM
  146. VK_FORMAT_ASTC_4x4_SRGB_BLOCK, // ASTC_4x4_UNORM_SRGB
  147. VK_FORMAT_ASTC_5x4_SRGB_BLOCK, // ASTC_5x4_UNORM_SRGB
  148. VK_FORMAT_ASTC_5x5_SRGB_BLOCK, // ASTC_5x5_UNORM_SRGB
  149. VK_FORMAT_ASTC_6x5_SRGB_BLOCK, // ASTC_6x5_UNORM_SRGB
  150. VK_FORMAT_ASTC_6x6_SRGB_BLOCK, // ASTC_6x6_UNORM_SRGB
  151. VK_FORMAT_ASTC_8x5_SRGB_BLOCK, // ASTC_8x5_UNORM_SRGB
  152. VK_FORMAT_ASTC_8x6_SRGB_BLOCK, // ASTC_8x6_UNORM_SRGB
  153. VK_FORMAT_ASTC_8x8_SRGB_BLOCK, // ASTC_8x8_UNORM_SRGB
  154. VK_FORMAT_ASTC_10x5_SRGB_BLOCK, // ASTC_10x5_UNORM_SRGB
  155. VK_FORMAT_ASTC_10x6_SRGB_BLOCK, // ASTC_10x6_UNORM_SRGB
  156. VK_FORMAT_ASTC_10x8_SRGB_BLOCK, // ASTC_10x8_UNORM_SRGB
  157. VK_FORMAT_ASTC_10x10_SRGB_BLOCK, // ASTC_10x10_UNORM_SRGB
  158. VK_FORMAT_ASTC_12x10_SRGB_BLOCK, // ASTC_12x10_UNORM_SRGB
  159. VK_FORMAT_ASTC_12x12_SRGB_BLOCK, // ASTC_12x12_UNORM_SRGB
  160. VK_FORMAT_ASTC_4x4_SFLOAT_BLOCK_EXT, // ASTC_4x4_FLOAT
  161. VK_FORMAT_ASTC_5x4_SFLOAT_BLOCK_EXT, // ASTC_5x4_FLOAT
  162. VK_FORMAT_ASTC_5x5_SFLOAT_BLOCK_EXT, // ASTC_5x5_FLOAT
  163. VK_FORMAT_ASTC_6x5_SFLOAT_BLOCK_EXT, // ASTC_6x5_FLOAT
  164. VK_FORMAT_ASTC_6x6_SFLOAT_BLOCK_EXT, // ASTC_6x6_FLOAT
  165. VK_FORMAT_ASTC_8x5_SFLOAT_BLOCK_EXT, // ASTC_8x5_FLOAT
  166. VK_FORMAT_ASTC_8x6_SFLOAT_BLOCK_EXT, // ASTC_8x6_FLOAT
  167. VK_FORMAT_ASTC_8x8_SFLOAT_BLOCK_EXT, // ASTC_8x8_FLOAT
  168. VK_FORMAT_ASTC_10x5_SFLOAT_BLOCK_EXT, // ASTC_10x5_FLOAT
  169. VK_FORMAT_ASTC_10x6_SFLOAT_BLOCK_EXT, // ASTC_10x6_FLOAT
  170. VK_FORMAT_ASTC_10x8_SFLOAT_BLOCK_EXT, // ASTC_10x8_FLOAT
  171. VK_FORMAT_ASTC_10x10_SFLOAT_BLOCK_EXT, // ASTC_10x10_FLOAT
  172. VK_FORMAT_ASTC_12x10_SFLOAT_BLOCK_EXT, // ASTC_12x10_FLOAT
  173. VK_FORMAT_ASTC_12x12_SFLOAT_BLOCK // ASTC_12x12_FLOAT
  174. };
  175. SDL_COMPILE_TIME_ASSERT(SDLToVK_TextureFormat, SDL_arraysize(SDLToVK_TextureFormat) == SDL_GPU_TEXTUREFORMAT_MAX_ENUM_VALUE);
  176. static VkComponentMapping SwizzleForSDLFormat(SDL_GPUTextureFormat format)
  177. {
  178. if (format == SDL_GPU_TEXTUREFORMAT_A8_UNORM) {
  179. // TODO: use VK_FORMAT_A8_UNORM_KHR from VK_KHR_maintenance5 when available
  180. return (VkComponentMapping){
  181. VK_COMPONENT_SWIZZLE_ZERO,
  182. VK_COMPONENT_SWIZZLE_ZERO,
  183. VK_COMPONENT_SWIZZLE_ZERO,
  184. VK_COMPONENT_SWIZZLE_R,
  185. };
  186. }
  187. if (format == SDL_GPU_TEXTUREFORMAT_B4G4R4A4_UNORM) {
  188. // ARGB -> BGRA
  189. // TODO: use VK_FORMAT_A4R4G4B4_UNORM_PACK16_EXT from VK_EXT_4444_formats when available
  190. return (VkComponentMapping){
  191. VK_COMPONENT_SWIZZLE_G,
  192. VK_COMPONENT_SWIZZLE_R,
  193. VK_COMPONENT_SWIZZLE_A,
  194. VK_COMPONENT_SWIZZLE_B,
  195. };
  196. }
  197. return (VkComponentMapping)IDENTITY_SWIZZLE;
  198. }
  199. static VkFormat SwapchainCompositionToFormat[] = {
  200. VK_FORMAT_B8G8R8A8_UNORM, // SDR
  201. VK_FORMAT_B8G8R8A8_SRGB, // SDR_LINEAR
  202. VK_FORMAT_R16G16B16A16_SFLOAT, // HDR_EXTENDED_LINEAR
  203. VK_FORMAT_A2B10G10R10_UNORM_PACK32 // HDR10_ST2084
  204. };
  205. static VkFormat SwapchainCompositionToFallbackFormat[] = {
  206. VK_FORMAT_R8G8B8A8_UNORM, // SDR
  207. VK_FORMAT_R8G8B8A8_SRGB, // SDR_LINEAR
  208. VK_FORMAT_UNDEFINED, // HDR_EXTENDED_LINEAR (no fallback)
  209. VK_FORMAT_UNDEFINED // HDR10_ST2084 (no fallback)
  210. };
  211. static SDL_GPUTextureFormat SwapchainCompositionToSDLFormat(
  212. SDL_GPUSwapchainComposition composition,
  213. bool usingFallback)
  214. {
  215. switch (composition) {
  216. case SDL_GPU_SWAPCHAINCOMPOSITION_SDR:
  217. return usingFallback ? SDL_GPU_TEXTUREFORMAT_R8G8B8A8_UNORM : SDL_GPU_TEXTUREFORMAT_B8G8R8A8_UNORM;
  218. case SDL_GPU_SWAPCHAINCOMPOSITION_SDR_LINEAR:
  219. return usingFallback ? SDL_GPU_TEXTUREFORMAT_R8G8B8A8_UNORM_SRGB : SDL_GPU_TEXTUREFORMAT_B8G8R8A8_UNORM_SRGB;
  220. case SDL_GPU_SWAPCHAINCOMPOSITION_HDR_EXTENDED_LINEAR:
  221. return SDL_GPU_TEXTUREFORMAT_R16G16B16A16_FLOAT;
  222. case SDL_GPU_SWAPCHAINCOMPOSITION_HDR10_ST2084:
  223. return SDL_GPU_TEXTUREFORMAT_R10G10B10A2_UNORM;
  224. default:
  225. return SDL_GPU_TEXTUREFORMAT_INVALID;
  226. }
  227. }
  228. static VkColorSpaceKHR SwapchainCompositionToColorSpace[] = {
  229. VK_COLOR_SPACE_SRGB_NONLINEAR_KHR, // SDR
  230. VK_COLOR_SPACE_SRGB_NONLINEAR_KHR, // SDR_LINEAR
  231. VK_COLOR_SPACE_EXTENDED_SRGB_LINEAR_EXT, // HDR_EXTENDED_LINEAR
  232. VK_COLOR_SPACE_HDR10_ST2084_EXT // HDR10_ST2084
  233. };
  234. static VkComponentMapping SwapchainCompositionSwizzle[] = {
  235. IDENTITY_SWIZZLE, // SDR
  236. IDENTITY_SWIZZLE, // SDR_LINEAR
  237. IDENTITY_SWIZZLE, // HDR_EXTENDED_LINEAR
  238. {
  239. // HDR10_ST2084
  240. VK_COMPONENT_SWIZZLE_R,
  241. VK_COMPONENT_SWIZZLE_G,
  242. VK_COMPONENT_SWIZZLE_B,
  243. VK_COMPONENT_SWIZZLE_A,
  244. }
  245. };
  246. static VkFormat SDLToVK_VertexFormat[] = {
  247. VK_FORMAT_UNDEFINED, // INVALID
  248. VK_FORMAT_R32_SINT, // INT
  249. VK_FORMAT_R32G32_SINT, // INT2
  250. VK_FORMAT_R32G32B32_SINT, // INT3
  251. VK_FORMAT_R32G32B32A32_SINT, // INT4
  252. VK_FORMAT_R32_UINT, // UINT
  253. VK_FORMAT_R32G32_UINT, // UINT2
  254. VK_FORMAT_R32G32B32_UINT, // UINT3
  255. VK_FORMAT_R32G32B32A32_UINT, // UINT4
  256. VK_FORMAT_R32_SFLOAT, // FLOAT
  257. VK_FORMAT_R32G32_SFLOAT, // FLOAT2
  258. VK_FORMAT_R32G32B32_SFLOAT, // FLOAT3
  259. VK_FORMAT_R32G32B32A32_SFLOAT, // FLOAT4
  260. VK_FORMAT_R8G8_SINT, // BYTE2
  261. VK_FORMAT_R8G8B8A8_SINT, // BYTE4
  262. VK_FORMAT_R8G8_UINT, // UBYTE2
  263. VK_FORMAT_R8G8B8A8_UINT, // UBYTE4
  264. VK_FORMAT_R8G8_SNORM, // BYTE2_NORM
  265. VK_FORMAT_R8G8B8A8_SNORM, // BYTE4_NORM
  266. VK_FORMAT_R8G8_UNORM, // UBYTE2_NORM
  267. VK_FORMAT_R8G8B8A8_UNORM, // UBYTE4_NORM
  268. VK_FORMAT_R16G16_SINT, // SHORT2
  269. VK_FORMAT_R16G16B16A16_SINT, // SHORT4
  270. VK_FORMAT_R16G16_UINT, // USHORT2
  271. VK_FORMAT_R16G16B16A16_UINT, // USHORT4
  272. VK_FORMAT_R16G16_SNORM, // SHORT2_NORM
  273. VK_FORMAT_R16G16B16A16_SNORM, // SHORT4_NORM
  274. VK_FORMAT_R16G16_UNORM, // USHORT2_NORM
  275. VK_FORMAT_R16G16B16A16_UNORM, // USHORT4_NORM
  276. VK_FORMAT_R16G16_SFLOAT, // HALF2
  277. VK_FORMAT_R16G16B16A16_SFLOAT // HALF4
  278. };
  279. SDL_COMPILE_TIME_ASSERT(SDLToVK_VertexFormat, SDL_arraysize(SDLToVK_VertexFormat) == SDL_GPU_VERTEXELEMENTFORMAT_MAX_ENUM_VALUE);
  280. static VkIndexType SDLToVK_IndexType[] = {
  281. VK_INDEX_TYPE_UINT16,
  282. VK_INDEX_TYPE_UINT32
  283. };
  284. static VkPrimitiveTopology SDLToVK_PrimitiveType[] = {
  285. VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST,
  286. VK_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP,
  287. VK_PRIMITIVE_TOPOLOGY_LINE_LIST,
  288. VK_PRIMITIVE_TOPOLOGY_LINE_STRIP,
  289. VK_PRIMITIVE_TOPOLOGY_POINT_LIST
  290. };
  291. static VkCullModeFlags SDLToVK_CullMode[] = {
  292. VK_CULL_MODE_NONE,
  293. VK_CULL_MODE_FRONT_BIT,
  294. VK_CULL_MODE_BACK_BIT,
  295. VK_CULL_MODE_FRONT_AND_BACK
  296. };
  297. static VkFrontFace SDLToVK_FrontFace[] = {
  298. VK_FRONT_FACE_COUNTER_CLOCKWISE,
  299. VK_FRONT_FACE_CLOCKWISE
  300. };
  301. static VkBlendFactor SDLToVK_BlendFactor[] = {
  302. VK_BLEND_FACTOR_ZERO, // INVALID
  303. VK_BLEND_FACTOR_ZERO,
  304. VK_BLEND_FACTOR_ONE,
  305. VK_BLEND_FACTOR_SRC_COLOR,
  306. VK_BLEND_FACTOR_ONE_MINUS_SRC_COLOR,
  307. VK_BLEND_FACTOR_DST_COLOR,
  308. VK_BLEND_FACTOR_ONE_MINUS_DST_COLOR,
  309. VK_BLEND_FACTOR_SRC_ALPHA,
  310. VK_BLEND_FACTOR_ONE_MINUS_SRC_ALPHA,
  311. VK_BLEND_FACTOR_DST_ALPHA,
  312. VK_BLEND_FACTOR_ONE_MINUS_DST_ALPHA,
  313. VK_BLEND_FACTOR_CONSTANT_COLOR,
  314. VK_BLEND_FACTOR_ONE_MINUS_CONSTANT_COLOR,
  315. VK_BLEND_FACTOR_SRC_ALPHA_SATURATE
  316. };
  317. SDL_COMPILE_TIME_ASSERT(SDLToVK_BlendFactor, SDL_arraysize(SDLToVK_BlendFactor) == SDL_GPU_BLENDFACTOR_MAX_ENUM_VALUE);
  318. static VkBlendOp SDLToVK_BlendOp[] = {
  319. VK_BLEND_OP_ADD, // INVALID
  320. VK_BLEND_OP_ADD,
  321. VK_BLEND_OP_SUBTRACT,
  322. VK_BLEND_OP_REVERSE_SUBTRACT,
  323. VK_BLEND_OP_MIN,
  324. VK_BLEND_OP_MAX
  325. };
  326. SDL_COMPILE_TIME_ASSERT(SDLToVK_BlendOp, SDL_arraysize(SDLToVK_BlendOp) == SDL_GPU_BLENDOP_MAX_ENUM_VALUE);
  327. static VkCompareOp SDLToVK_CompareOp[] = {
  328. VK_COMPARE_OP_NEVER, // INVALID
  329. VK_COMPARE_OP_NEVER,
  330. VK_COMPARE_OP_LESS,
  331. VK_COMPARE_OP_EQUAL,
  332. VK_COMPARE_OP_LESS_OR_EQUAL,
  333. VK_COMPARE_OP_GREATER,
  334. VK_COMPARE_OP_NOT_EQUAL,
  335. VK_COMPARE_OP_GREATER_OR_EQUAL,
  336. VK_COMPARE_OP_ALWAYS
  337. };
  338. SDL_COMPILE_TIME_ASSERT(SDLToVK_CompareOp, SDL_arraysize(SDLToVK_CompareOp) == SDL_GPU_COMPAREOP_MAX_ENUM_VALUE);
  339. static VkStencilOp SDLToVK_StencilOp[] = {
  340. VK_STENCIL_OP_KEEP, // INVALID
  341. VK_STENCIL_OP_KEEP,
  342. VK_STENCIL_OP_ZERO,
  343. VK_STENCIL_OP_REPLACE,
  344. VK_STENCIL_OP_INCREMENT_AND_CLAMP,
  345. VK_STENCIL_OP_DECREMENT_AND_CLAMP,
  346. VK_STENCIL_OP_INVERT,
  347. VK_STENCIL_OP_INCREMENT_AND_WRAP,
  348. VK_STENCIL_OP_DECREMENT_AND_WRAP
  349. };
  350. SDL_COMPILE_TIME_ASSERT(SDLToVK_StencilOp, SDL_arraysize(SDLToVK_StencilOp) == SDL_GPU_STENCILOP_MAX_ENUM_VALUE);
  351. static VkAttachmentLoadOp SDLToVK_LoadOp[] = {
  352. VK_ATTACHMENT_LOAD_OP_LOAD,
  353. VK_ATTACHMENT_LOAD_OP_CLEAR,
  354. VK_ATTACHMENT_LOAD_OP_DONT_CARE
  355. };
  356. static VkAttachmentStoreOp SDLToVK_StoreOp[] = {
  357. VK_ATTACHMENT_STORE_OP_STORE,
  358. VK_ATTACHMENT_STORE_OP_DONT_CARE,
  359. VK_ATTACHMENT_STORE_OP_DONT_CARE,
  360. VK_ATTACHMENT_STORE_OP_STORE
  361. };
  362. static VkSampleCountFlagBits SDLToVK_SampleCount[] = {
  363. VK_SAMPLE_COUNT_1_BIT,
  364. VK_SAMPLE_COUNT_2_BIT,
  365. VK_SAMPLE_COUNT_4_BIT,
  366. VK_SAMPLE_COUNT_8_BIT
  367. };
  368. static VkVertexInputRate SDLToVK_VertexInputRate[] = {
  369. VK_VERTEX_INPUT_RATE_VERTEX,
  370. VK_VERTEX_INPUT_RATE_INSTANCE
  371. };
  372. static VkFilter SDLToVK_Filter[] = {
  373. VK_FILTER_NEAREST,
  374. VK_FILTER_LINEAR
  375. };
  376. static VkSamplerMipmapMode SDLToVK_SamplerMipmapMode[] = {
  377. VK_SAMPLER_MIPMAP_MODE_NEAREST,
  378. VK_SAMPLER_MIPMAP_MODE_LINEAR
  379. };
  380. static VkSamplerAddressMode SDLToVK_SamplerAddressMode[] = {
  381. VK_SAMPLER_ADDRESS_MODE_REPEAT,
  382. VK_SAMPLER_ADDRESS_MODE_MIRRORED_REPEAT,
  383. VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE
  384. };
  385. // Structures
  386. typedef struct VulkanMemoryAllocation VulkanMemoryAllocation;
  387. typedef struct VulkanBuffer VulkanBuffer;
  388. typedef struct VulkanBufferContainer VulkanBufferContainer;
  389. typedef struct VulkanUniformBuffer VulkanUniformBuffer;
  390. typedef struct VulkanTexture VulkanTexture;
  391. typedef struct VulkanTextureContainer VulkanTextureContainer;
  392. typedef struct VulkanFenceHandle
  393. {
  394. VkFence fence;
  395. SDL_AtomicInt referenceCount;
  396. } VulkanFenceHandle;
  397. // Memory Allocation
  398. typedef struct VulkanMemoryFreeRegion
  399. {
  400. VulkanMemoryAllocation *allocation;
  401. VkDeviceSize offset;
  402. VkDeviceSize size;
  403. Uint32 allocationIndex;
  404. Uint32 sortedIndex;
  405. } VulkanMemoryFreeRegion;
  406. typedef struct VulkanMemoryUsedRegion
  407. {
  408. VulkanMemoryAllocation *allocation;
  409. VkDeviceSize offset;
  410. VkDeviceSize size;
  411. VkDeviceSize resourceOffset; // differs from offset based on alignment
  412. VkDeviceSize resourceSize; // differs from size based on alignment
  413. VkDeviceSize alignment;
  414. Uint8 isBuffer;
  415. union
  416. {
  417. VulkanBuffer *vulkanBuffer;
  418. VulkanTexture *vulkanTexture;
  419. };
  420. } VulkanMemoryUsedRegion;
  421. typedef struct VulkanMemorySubAllocator
  422. {
  423. Uint32 memoryTypeIndex;
  424. VulkanMemoryAllocation **allocations;
  425. Uint32 allocationCount;
  426. VulkanMemoryFreeRegion **sortedFreeRegions;
  427. Uint32 sortedFreeRegionCount;
  428. Uint32 sortedFreeRegionCapacity;
  429. } VulkanMemorySubAllocator;
  430. struct VulkanMemoryAllocation
  431. {
  432. VulkanMemorySubAllocator *allocator;
  433. VkDeviceMemory memory;
  434. VkDeviceSize size;
  435. VulkanMemoryUsedRegion **usedRegions;
  436. Uint32 usedRegionCount;
  437. Uint32 usedRegionCapacity;
  438. VulkanMemoryFreeRegion **freeRegions;
  439. Uint32 freeRegionCount;
  440. Uint32 freeRegionCapacity;
  441. Uint8 availableForAllocation;
  442. VkDeviceSize freeSpace;
  443. VkDeviceSize usedSpace;
  444. Uint8 *mapPointer;
  445. SDL_Mutex *memoryLock;
  446. };
  447. typedef struct VulkanMemoryAllocator
  448. {
  449. VulkanMemorySubAllocator subAllocators[VK_MAX_MEMORY_TYPES];
  450. } VulkanMemoryAllocator;
  451. // Memory structures
  452. typedef enum VulkanBufferType
  453. {
  454. VULKAN_BUFFER_TYPE_GPU,
  455. VULKAN_BUFFER_TYPE_UNIFORM,
  456. VULKAN_BUFFER_TYPE_TRANSFER
  457. } VulkanBufferType;
  458. struct VulkanBuffer
  459. {
  460. VulkanBufferContainer *container;
  461. Uint32 containerIndex;
  462. VkBuffer buffer;
  463. VulkanMemoryUsedRegion *usedRegion;
  464. // Needed for uniforms and defrag
  465. VulkanBufferType type;
  466. SDL_GPUBufferUsageFlags usage;
  467. VkDeviceSize size;
  468. SDL_AtomicInt referenceCount;
  469. bool transitioned;
  470. bool markedForDestroy; // so that defrag doesn't double-free
  471. VulkanUniformBuffer *uniformBufferForDefrag;
  472. };
  473. struct VulkanBufferContainer
  474. {
  475. VulkanBuffer *activeBuffer;
  476. VulkanBuffer **buffers;
  477. Uint32 bufferCapacity;
  478. Uint32 bufferCount;
  479. bool dedicated;
  480. char *debugName;
  481. };
  482. // Renderer Structure
  483. typedef struct QueueFamilyIndices
  484. {
  485. Uint32 graphicsFamily;
  486. Uint32 presentFamily;
  487. Uint32 computeFamily;
  488. Uint32 transferFamily;
  489. } QueueFamilyIndices;
  490. typedef struct VulkanSampler
  491. {
  492. VkSampler sampler;
  493. SDL_AtomicInt referenceCount;
  494. } VulkanSampler;
  495. typedef struct VulkanShader
  496. {
  497. VkShaderModule shaderModule;
  498. char *entrypointName;
  499. SDL_GPUShaderStage stage;
  500. Uint32 numSamplers;
  501. Uint32 numStorageTextures;
  502. Uint32 numStorageBuffers;
  503. Uint32 numUniformBuffers;
  504. SDL_AtomicInt referenceCount;
  505. } VulkanShader;
  506. /* Textures are made up of individual subresources.
  507. * This helps us barrier the resource efficiently.
  508. */
  509. typedef struct VulkanTextureSubresource
  510. {
  511. VulkanTexture *parent;
  512. Uint32 layer;
  513. Uint32 level;
  514. VkImageView *renderTargetViews; // One render target view per depth slice
  515. VkImageView computeWriteView;
  516. VkImageView depthStencilView;
  517. } VulkanTextureSubresource;
  518. struct VulkanTexture
  519. {
  520. VulkanTextureContainer *container;
  521. Uint32 containerIndex;
  522. VulkanMemoryUsedRegion *usedRegion;
  523. VkImage image;
  524. VkImageView fullView; // used for samplers and storage reads
  525. VkComponentMapping swizzle;
  526. VkImageAspectFlags aspectFlags;
  527. Uint32 depth; // used for cleanup only
  528. // FIXME: It'd be nice if we didn't have to have this on the texture...
  529. SDL_GPUTextureUsageFlags usage; // used for defrag transitions only.
  530. Uint32 subresourceCount;
  531. VulkanTextureSubresource *subresources;
  532. bool markedForDestroy; // so that defrag doesn't double-free
  533. SDL_AtomicInt referenceCount;
  534. };
  535. struct VulkanTextureContainer
  536. {
  537. TextureCommonHeader header;
  538. VulkanTexture *activeTexture;
  539. Uint32 textureCapacity;
  540. Uint32 textureCount;
  541. VulkanTexture **textures;
  542. char *debugName;
  543. bool canBeCycled;
  544. };
  545. typedef enum VulkanBufferUsageMode
  546. {
  547. VULKAN_BUFFER_USAGE_MODE_COPY_SOURCE,
  548. VULKAN_BUFFER_USAGE_MODE_COPY_DESTINATION,
  549. VULKAN_BUFFER_USAGE_MODE_VERTEX_READ,
  550. VULKAN_BUFFER_USAGE_MODE_INDEX_READ,
  551. VULKAN_BUFFER_USAGE_MODE_INDIRECT,
  552. VULKAN_BUFFER_USAGE_MODE_GRAPHICS_STORAGE_READ,
  553. VULKAN_BUFFER_USAGE_MODE_COMPUTE_STORAGE_READ,
  554. VULKAN_BUFFER_USAGE_MODE_COMPUTE_STORAGE_READ_WRITE,
  555. } VulkanBufferUsageMode;
  556. typedef enum VulkanTextureUsageMode
  557. {
  558. VULKAN_TEXTURE_USAGE_MODE_UNINITIALIZED,
  559. VULKAN_TEXTURE_USAGE_MODE_COPY_SOURCE,
  560. VULKAN_TEXTURE_USAGE_MODE_COPY_DESTINATION,
  561. VULKAN_TEXTURE_USAGE_MODE_SAMPLER,
  562. VULKAN_TEXTURE_USAGE_MODE_GRAPHICS_STORAGE_READ,
  563. VULKAN_TEXTURE_USAGE_MODE_COMPUTE_STORAGE_READ,
  564. VULKAN_TEXTURE_USAGE_MODE_COMPUTE_STORAGE_READ_WRITE,
  565. VULKAN_TEXTURE_USAGE_MODE_COLOR_ATTACHMENT,
  566. VULKAN_TEXTURE_USAGE_MODE_DEPTH_STENCIL_ATTACHMENT,
  567. VULKAN_TEXTURE_USAGE_MODE_PRESENT
  568. } VulkanTextureUsageMode;
  569. typedef enum VulkanUniformBufferStage
  570. {
  571. VULKAN_UNIFORM_BUFFER_STAGE_VERTEX,
  572. VULKAN_UNIFORM_BUFFER_STAGE_FRAGMENT,
  573. VULKAN_UNIFORM_BUFFER_STAGE_COMPUTE
  574. } VulkanUniformBufferStage;
  575. typedef struct VulkanFramebuffer
  576. {
  577. VkFramebuffer framebuffer;
  578. SDL_AtomicInt referenceCount;
  579. } VulkanFramebuffer;
  580. typedef struct WindowData
  581. {
  582. SDL_Window *window;
  583. SDL_GPUSwapchainComposition swapchainComposition;
  584. SDL_GPUPresentMode presentMode;
  585. bool needsSwapchainRecreate;
  586. Uint32 swapchainCreateWidth;
  587. Uint32 swapchainCreateHeight;
  588. // Window surface
  589. VkSurfaceKHR surface;
  590. // Swapchain for window surface
  591. VkSwapchainKHR swapchain;
  592. VkFormat format;
  593. VkColorSpaceKHR colorSpace;
  594. VkComponentMapping swapchainSwizzle;
  595. bool usingFallbackFormat;
  596. // Swapchain images
  597. VulkanTextureContainer *textureContainers; // use containers so that swapchain textures can use the same API as other textures
  598. Uint32 imageCount;
  599. Uint32 width;
  600. Uint32 height;
  601. // Synchronization primitives
  602. VkSemaphore imageAvailableSemaphore[MAX_FRAMES_IN_FLIGHT];
  603. VkSemaphore *renderFinishedSemaphore;
  604. SDL_GPUFence *inFlightFences[MAX_FRAMES_IN_FLIGHT];
  605. Uint32 frameCounter;
  606. } WindowData;
  607. typedef struct SwapchainSupportDetails
  608. {
  609. VkSurfaceCapabilitiesKHR capabilities;
  610. VkSurfaceFormatKHR *formats;
  611. Uint32 formatsLength;
  612. VkPresentModeKHR *presentModes;
  613. Uint32 presentModesLength;
  614. } SwapchainSupportDetails;
  615. typedef struct VulkanPresentData
  616. {
  617. WindowData *windowData;
  618. Uint32 swapchainImageIndex;
  619. } VulkanPresentData;
  620. struct VulkanUniformBuffer
  621. {
  622. VulkanBuffer *buffer;
  623. Uint32 drawOffset;
  624. Uint32 writeOffset;
  625. };
  626. typedef struct VulkanDescriptorInfo
  627. {
  628. VkDescriptorType descriptorType;
  629. VkShaderStageFlagBits stageFlag;
  630. } VulkanDescriptorInfo;
  631. typedef struct DescriptorSetPool
  632. {
  633. // It's a pool... of pools!!!
  634. Uint32 poolCount;
  635. VkDescriptorPool *descriptorPools;
  636. // We'll just manage the descriptor sets ourselves instead of freeing the sets
  637. VkDescriptorSet *descriptorSets;
  638. Uint32 descriptorSetCount;
  639. Uint32 descriptorSetIndex;
  640. } DescriptorSetPool;
  641. // A command buffer acquires a cache at command buffer acquisition time
  642. typedef struct DescriptorSetCache
  643. {
  644. // Pools are indexed by DescriptorSetLayoutID which increases monotonically
  645. // There's only a certain number of maximum layouts possible since we de-duplicate them.
  646. DescriptorSetPool *pools;
  647. Uint32 poolCount;
  648. } DescriptorSetCache;
  649. typedef struct DescriptorSetLayoutHashTableKey
  650. {
  651. VkShaderStageFlagBits shaderStage;
  652. // Category 1: read resources
  653. Uint32 samplerCount;
  654. Uint32 storageBufferCount;
  655. Uint32 storageTextureCount;
  656. // Category 2: write resources
  657. Uint32 writeStorageBufferCount;
  658. Uint32 writeStorageTextureCount;
  659. // Category 3: uniform buffers
  660. Uint32 uniformBufferCount;
  661. } DescriptorSetLayoutHashTableKey;
  662. typedef uint32_t DescriptorSetLayoutID;
  663. typedef struct DescriptorSetLayout
  664. {
  665. DescriptorSetLayoutID ID;
  666. VkDescriptorSetLayout descriptorSetLayout;
  667. // Category 1: read resources
  668. Uint32 samplerCount;
  669. Uint32 storageBufferCount;
  670. Uint32 storageTextureCount;
  671. // Category 2: write resources
  672. Uint32 writeStorageBufferCount;
  673. Uint32 writeStorageTextureCount;
  674. // Category 3: uniform buffers
  675. Uint32 uniformBufferCount;
  676. } DescriptorSetLayout;
  677. typedef struct GraphicsPipelineResourceLayoutHashTableKey
  678. {
  679. Uint32 vertexSamplerCount;
  680. Uint32 vertexStorageTextureCount;
  681. Uint32 vertexStorageBufferCount;
  682. Uint32 vertexUniformBufferCount;
  683. Uint32 fragmentSamplerCount;
  684. Uint32 fragmentStorageTextureCount;
  685. Uint32 fragmentStorageBufferCount;
  686. Uint32 fragmentUniformBufferCount;
  687. } GraphicsPipelineResourceLayoutHashTableKey;
  688. typedef struct VulkanGraphicsPipelineResourceLayout
  689. {
  690. VkPipelineLayout pipelineLayout;
  691. /*
  692. * Descriptor set layout is as follows:
  693. * 0: vertex resources
  694. * 1: vertex uniform buffers
  695. * 2: fragment resources
  696. * 3: fragment uniform buffers
  697. */
  698. DescriptorSetLayout *descriptorSetLayouts[4];
  699. Uint32 vertexSamplerCount;
  700. Uint32 vertexStorageTextureCount;
  701. Uint32 vertexStorageBufferCount;
  702. Uint32 vertexUniformBufferCount;
  703. Uint32 fragmentSamplerCount;
  704. Uint32 fragmentStorageTextureCount;
  705. Uint32 fragmentStorageBufferCount;
  706. Uint32 fragmentUniformBufferCount;
  707. } VulkanGraphicsPipelineResourceLayout;
  708. typedef struct VulkanGraphicsPipeline
  709. {
  710. GraphicsPipelineCommonHeader header;
  711. VkPipeline pipeline;
  712. SDL_GPUPrimitiveType primitiveType;
  713. VulkanGraphicsPipelineResourceLayout *resourceLayout;
  714. VulkanShader *vertexShader;
  715. VulkanShader *fragmentShader;
  716. SDL_AtomicInt referenceCount;
  717. } VulkanGraphicsPipeline;
  718. typedef struct ComputePipelineResourceLayoutHashTableKey
  719. {
  720. Uint32 samplerCount;
  721. Uint32 readonlyStorageTextureCount;
  722. Uint32 readonlyStorageBufferCount;
  723. Uint32 readWriteStorageTextureCount;
  724. Uint32 readWriteStorageBufferCount;
  725. Uint32 uniformBufferCount;
  726. } ComputePipelineResourceLayoutHashTableKey;
  727. typedef struct VulkanComputePipelineResourceLayout
  728. {
  729. VkPipelineLayout pipelineLayout;
  730. /*
  731. * Descriptor set layout is as follows:
  732. * 0: samplers, then read-only textures, then read-only buffers
  733. * 1: write-only textures, then write-only buffers
  734. * 2: uniform buffers
  735. */
  736. DescriptorSetLayout *descriptorSetLayouts[3];
  737. Uint32 numSamplers;
  738. Uint32 numReadonlyStorageTextures;
  739. Uint32 numReadonlyStorageBuffers;
  740. Uint32 numReadWriteStorageTextures;
  741. Uint32 numReadWriteStorageBuffers;
  742. Uint32 numUniformBuffers;
  743. } VulkanComputePipelineResourceLayout;
  744. typedef struct VulkanComputePipeline
  745. {
  746. ComputePipelineCommonHeader header;
  747. VkShaderModule shaderModule;
  748. VkPipeline pipeline;
  749. VulkanComputePipelineResourceLayout *resourceLayout;
  750. SDL_AtomicInt referenceCount;
  751. } VulkanComputePipeline;
  752. typedef struct RenderPassColorTargetDescription
  753. {
  754. VkFormat format;
  755. SDL_GPULoadOp loadOp;
  756. SDL_GPUStoreOp storeOp;
  757. } RenderPassColorTargetDescription;
  758. typedef struct RenderPassDepthStencilTargetDescription
  759. {
  760. VkFormat format;
  761. SDL_GPULoadOp loadOp;
  762. SDL_GPUStoreOp storeOp;
  763. SDL_GPULoadOp stencilLoadOp;
  764. SDL_GPUStoreOp stencilStoreOp;
  765. } RenderPassDepthStencilTargetDescription;
  766. typedef struct CommandPoolHashTableKey
  767. {
  768. SDL_ThreadID threadID;
  769. } CommandPoolHashTableKey;
  770. typedef struct RenderPassHashTableKey
  771. {
  772. RenderPassColorTargetDescription colorTargetDescriptions[MAX_COLOR_TARGET_BINDINGS];
  773. Uint32 numColorTargets;
  774. VkFormat resolveTargetFormats[MAX_COLOR_TARGET_BINDINGS];
  775. Uint32 numResolveTargets;
  776. RenderPassDepthStencilTargetDescription depthStencilTargetDescription;
  777. VkSampleCountFlagBits sampleCount;
  778. } RenderPassHashTableKey;
  779. typedef struct VulkanRenderPassHashTableValue
  780. {
  781. VkRenderPass handle;
  782. } VulkanRenderPassHashTableValue;
  783. typedef struct FramebufferHashTableKey
  784. {
  785. VkImageView colorAttachmentViews[MAX_COLOR_TARGET_BINDINGS];
  786. Uint32 numColorTargets;
  787. VkImageView resolveAttachmentViews[MAX_COLOR_TARGET_BINDINGS];
  788. Uint32 numResolveAttachments;
  789. VkImageView depthStencilAttachmentView;
  790. Uint32 width;
  791. Uint32 height;
  792. } FramebufferHashTableKey;
  793. // Command structures
  794. typedef struct VulkanFencePool
  795. {
  796. SDL_Mutex *lock;
  797. VulkanFenceHandle **availableFences;
  798. Uint32 availableFenceCount;
  799. Uint32 availableFenceCapacity;
  800. } VulkanFencePool;
  801. typedef struct VulkanCommandPool VulkanCommandPool;
  802. typedef struct VulkanRenderer VulkanRenderer;
  803. typedef struct VulkanCommandBuffer
  804. {
  805. CommandBufferCommonHeader common;
  806. VulkanRenderer *renderer;
  807. VkCommandBuffer commandBuffer;
  808. VulkanCommandPool *commandPool;
  809. VulkanPresentData *presentDatas;
  810. Uint32 presentDataCount;
  811. Uint32 presentDataCapacity;
  812. VkSemaphore *waitSemaphores;
  813. Uint32 waitSemaphoreCount;
  814. Uint32 waitSemaphoreCapacity;
  815. VkSemaphore *signalSemaphores;
  816. Uint32 signalSemaphoreCount;
  817. Uint32 signalSemaphoreCapacity;
  818. VulkanComputePipeline *currentComputePipeline;
  819. VulkanGraphicsPipeline *currentGraphicsPipeline;
  820. // Keep track of resources transitioned away from their default state to barrier them on pass end
  821. VulkanTextureSubresource *colorAttachmentSubresources[MAX_COLOR_TARGET_BINDINGS];
  822. Uint32 colorAttachmentSubresourceCount;
  823. VulkanTextureSubresource *resolveAttachmentSubresources[MAX_COLOR_TARGET_BINDINGS];
  824. Uint32 resolveAttachmentSubresourceCount;
  825. VulkanTextureSubresource *depthStencilAttachmentSubresource; // may be NULL
  826. // Dynamic state
  827. VkViewport currentViewport;
  828. VkRect2D currentScissor;
  829. float blendConstants[4];
  830. Uint8 stencilRef;
  831. // Resource bind state
  832. DescriptorSetCache *descriptorSetCache; // acquired when command buffer is acquired
  833. bool needNewVertexResourceDescriptorSet;
  834. bool needNewVertexUniformDescriptorSet;
  835. bool needNewVertexUniformOffsets;
  836. bool needNewFragmentResourceDescriptorSet;
  837. bool needNewFragmentUniformDescriptorSet;
  838. bool needNewFragmentUniformOffsets;
  839. bool needNewComputeReadOnlyDescriptorSet;
  840. bool needNewComputeReadWriteDescriptorSet;
  841. bool needNewComputeUniformDescriptorSet;
  842. bool needNewComputeUniformOffsets;
  843. VkDescriptorSet vertexResourceDescriptorSet;
  844. VkDescriptorSet vertexUniformDescriptorSet;
  845. VkDescriptorSet fragmentResourceDescriptorSet;
  846. VkDescriptorSet fragmentUniformDescriptorSet;
  847. VkDescriptorSet computeReadOnlyDescriptorSet;
  848. VkDescriptorSet computeReadWriteDescriptorSet;
  849. VkDescriptorSet computeUniformDescriptorSet;
  850. VkBuffer vertexBuffers[MAX_VERTEX_BUFFERS];
  851. VkDeviceSize vertexBufferOffsets[MAX_VERTEX_BUFFERS];
  852. Uint32 vertexBufferCount;
  853. bool needVertexBufferBind;
  854. VkImageView vertexSamplerTextureViewBindings[MAX_TEXTURE_SAMPLERS_PER_STAGE];
  855. VkSampler vertexSamplerBindings[MAX_TEXTURE_SAMPLERS_PER_STAGE];
  856. VkImageView vertexStorageTextureViewBindings[MAX_STORAGE_TEXTURES_PER_STAGE];
  857. VkBuffer vertexStorageBufferBindings[MAX_STORAGE_BUFFERS_PER_STAGE];
  858. VkImageView fragmentSamplerTextureViewBindings[MAX_TEXTURE_SAMPLERS_PER_STAGE];
  859. VkSampler fragmentSamplerBindings[MAX_TEXTURE_SAMPLERS_PER_STAGE];
  860. VkImageView fragmentStorageTextureViewBindings[MAX_STORAGE_TEXTURES_PER_STAGE];
  861. VkBuffer fragmentStorageBufferBindings[MAX_STORAGE_BUFFERS_PER_STAGE];
  862. VkImageView computeSamplerTextureViewBindings[MAX_TEXTURE_SAMPLERS_PER_STAGE];
  863. VkSampler computeSamplerBindings[MAX_TEXTURE_SAMPLERS_PER_STAGE];
  864. VkImageView readOnlyComputeStorageTextureViewBindings[MAX_STORAGE_TEXTURES_PER_STAGE];
  865. VkBuffer readOnlyComputeStorageBufferBindings[MAX_STORAGE_BUFFERS_PER_STAGE];
  866. // Track these separately because barriers can happen mid compute pass
  867. VulkanTexture *readOnlyComputeStorageTextures[MAX_STORAGE_TEXTURES_PER_STAGE];
  868. VulkanBuffer *readOnlyComputeStorageBuffers[MAX_STORAGE_BUFFERS_PER_STAGE];
  869. VkImageView readWriteComputeStorageTextureViewBindings[MAX_COMPUTE_WRITE_TEXTURES];
  870. VkBuffer readWriteComputeStorageBufferBindings[MAX_COMPUTE_WRITE_BUFFERS];
  871. // Track these separately because they are barriered when the compute pass begins
  872. VulkanTextureSubresource *readWriteComputeStorageTextureSubresources[MAX_COMPUTE_WRITE_TEXTURES];
  873. Uint32 readWriteComputeStorageTextureSubresourceCount;
  874. VulkanBuffer *readWriteComputeStorageBuffers[MAX_COMPUTE_WRITE_BUFFERS];
  875. // Uniform buffers
  876. VulkanUniformBuffer *vertexUniformBuffers[MAX_UNIFORM_BUFFERS_PER_STAGE];
  877. VulkanUniformBuffer *fragmentUniformBuffers[MAX_UNIFORM_BUFFERS_PER_STAGE];
  878. VulkanUniformBuffer *computeUniformBuffers[MAX_UNIFORM_BUFFERS_PER_STAGE];
  879. // Track used resources
  880. VulkanBuffer **usedBuffers;
  881. Sint32 usedBufferCount;
  882. Sint32 usedBufferCapacity;
  883. VulkanTexture **usedTextures;
  884. Sint32 usedTextureCount;
  885. Sint32 usedTextureCapacity;
  886. VulkanSampler **usedSamplers;
  887. Sint32 usedSamplerCount;
  888. Sint32 usedSamplerCapacity;
  889. VulkanGraphicsPipeline **usedGraphicsPipelines;
  890. Sint32 usedGraphicsPipelineCount;
  891. Sint32 usedGraphicsPipelineCapacity;
  892. VulkanComputePipeline **usedComputePipelines;
  893. Sint32 usedComputePipelineCount;
  894. Sint32 usedComputePipelineCapacity;
  895. VulkanFramebuffer **usedFramebuffers;
  896. Sint32 usedFramebufferCount;
  897. Sint32 usedFramebufferCapacity;
  898. VulkanUniformBuffer **usedUniformBuffers;
  899. Sint32 usedUniformBufferCount;
  900. Sint32 usedUniformBufferCapacity;
  901. VulkanFenceHandle *inFlightFence;
  902. bool autoReleaseFence;
  903. bool swapchainRequested;
  904. bool isDefrag; // Whether this CB was created for defragging
  905. } VulkanCommandBuffer;
  906. struct VulkanCommandPool
  907. {
  908. SDL_ThreadID threadID;
  909. VkCommandPool commandPool;
  910. VulkanCommandBuffer **inactiveCommandBuffers;
  911. Uint32 inactiveCommandBufferCapacity;
  912. Uint32 inactiveCommandBufferCount;
  913. };
  914. // Context
  915. struct VulkanRenderer
  916. {
  917. VkInstance instance;
  918. VkPhysicalDevice physicalDevice;
  919. VkPhysicalDeviceProperties2KHR physicalDeviceProperties;
  920. VkPhysicalDeviceDriverPropertiesKHR physicalDeviceDriverProperties;
  921. VkPhysicalDeviceFeatures desiredDeviceFeatures;
  922. VkDevice logicalDevice;
  923. Uint8 integratedMemoryNotification;
  924. Uint8 outOfDeviceLocalMemoryWarning;
  925. Uint8 outofBARMemoryWarning;
  926. Uint8 fillModeOnlyWarning;
  927. bool debugMode;
  928. bool preferLowPower;
  929. bool requireHardwareAcceleration;
  930. SDL_PropertiesID props;
  931. Uint32 allowedFramesInFlight;
  932. VulkanExtensions supports;
  933. bool supportsDebugUtils;
  934. bool supportsColorspace;
  935. bool supportsPhysicalDeviceProperties2;
  936. bool supportsFillModeNonSolid;
  937. bool supportsMultiDrawIndirect;
  938. VulkanMemoryAllocator *memoryAllocator;
  939. VkPhysicalDeviceMemoryProperties memoryProperties;
  940. bool checkEmptyAllocations;
  941. WindowData **claimedWindows;
  942. Uint32 claimedWindowCount;
  943. Uint32 claimedWindowCapacity;
  944. Uint32 queueFamilyIndex;
  945. VkQueue unifiedQueue;
  946. VulkanCommandBuffer **submittedCommandBuffers;
  947. Uint32 submittedCommandBufferCount;
  948. Uint32 submittedCommandBufferCapacity;
  949. VulkanFencePool fencePool;
  950. SDL_HashTable *commandPoolHashTable;
  951. SDL_HashTable *renderPassHashTable;
  952. SDL_HashTable *framebufferHashTable;
  953. SDL_HashTable *graphicsPipelineResourceLayoutHashTable;
  954. SDL_HashTable *computePipelineResourceLayoutHashTable;
  955. SDL_HashTable *descriptorSetLayoutHashTable;
  956. VulkanUniformBuffer **uniformBufferPool;
  957. Uint32 uniformBufferPoolCount;
  958. Uint32 uniformBufferPoolCapacity;
  959. DescriptorSetCache **descriptorSetCachePool;
  960. Uint32 descriptorSetCachePoolCount;
  961. Uint32 descriptorSetCachePoolCapacity;
  962. SDL_AtomicInt layoutResourceID;
  963. Uint32 minUBOAlignment;
  964. // Deferred resource destruction
  965. VulkanTexture **texturesToDestroy;
  966. Uint32 texturesToDestroyCount;
  967. Uint32 texturesToDestroyCapacity;
  968. VulkanBuffer **buffersToDestroy;
  969. Uint32 buffersToDestroyCount;
  970. Uint32 buffersToDestroyCapacity;
  971. VulkanSampler **samplersToDestroy;
  972. Uint32 samplersToDestroyCount;
  973. Uint32 samplersToDestroyCapacity;
  974. VulkanGraphicsPipeline **graphicsPipelinesToDestroy;
  975. Uint32 graphicsPipelinesToDestroyCount;
  976. Uint32 graphicsPipelinesToDestroyCapacity;
  977. VulkanComputePipeline **computePipelinesToDestroy;
  978. Uint32 computePipelinesToDestroyCount;
  979. Uint32 computePipelinesToDestroyCapacity;
  980. VulkanShader **shadersToDestroy;
  981. Uint32 shadersToDestroyCount;
  982. Uint32 shadersToDestroyCapacity;
  983. VulkanFramebuffer **framebuffersToDestroy;
  984. Uint32 framebuffersToDestroyCount;
  985. Uint32 framebuffersToDestroyCapacity;
  986. SDL_Mutex *allocatorLock;
  987. SDL_Mutex *disposeLock;
  988. SDL_Mutex *submitLock;
  989. SDL_Mutex *acquireCommandBufferLock;
  990. SDL_Mutex *acquireUniformBufferLock;
  991. SDL_Mutex *renderPassFetchLock;
  992. SDL_Mutex *framebufferFetchLock;
  993. SDL_Mutex *graphicsPipelineLayoutFetchLock;
  994. SDL_Mutex *computePipelineLayoutFetchLock;
  995. SDL_Mutex *descriptorSetLayoutFetchLock;
  996. SDL_Mutex *windowLock;
  997. Uint8 defragInProgress;
  998. VulkanMemoryAllocation **allocationsToDefrag;
  999. Uint32 allocationsToDefragCount;
  1000. Uint32 allocationsToDefragCapacity;
  1001. #define VULKAN_INSTANCE_FUNCTION(func) \
  1002. PFN_##func func;
  1003. #define VULKAN_DEVICE_FUNCTION(func) \
  1004. PFN_##func func;
  1005. #include "SDL_gpu_vulkan_vkfuncs.h"
  1006. };
  1007. // Forward declarations
  1008. static bool VULKAN_INTERNAL_DefragmentMemory(VulkanRenderer *renderer, VulkanCommandBuffer *commandBuffer);
  1009. static bool VULKAN_INTERNAL_BeginCommandBuffer(VulkanRenderer *renderer, VulkanCommandBuffer *commandBuffer);
  1010. static void VULKAN_ReleaseWindow(SDL_GPURenderer *driverData, SDL_Window *window);
  1011. static bool VULKAN_Wait(SDL_GPURenderer *driverData);
  1012. static bool VULKAN_WaitForFences(SDL_GPURenderer *driverData, bool waitAll, SDL_GPUFence *const *fences, Uint32 numFences);
  1013. static bool VULKAN_Submit(SDL_GPUCommandBuffer *commandBuffer);
  1014. static SDL_GPUCommandBuffer *VULKAN_AcquireCommandBuffer(SDL_GPURenderer *driverData);
  1015. // Error Handling
  1016. static inline const char *VkErrorMessages(VkResult code)
  1017. {
  1018. #define ERR_TO_STR(e) \
  1019. case e: \
  1020. return #e;
  1021. switch (code) {
  1022. ERR_TO_STR(VK_ERROR_OUT_OF_HOST_MEMORY)
  1023. ERR_TO_STR(VK_ERROR_OUT_OF_DEVICE_MEMORY)
  1024. ERR_TO_STR(VK_ERROR_FRAGMENTED_POOL)
  1025. ERR_TO_STR(VK_ERROR_OUT_OF_POOL_MEMORY)
  1026. ERR_TO_STR(VK_ERROR_INITIALIZATION_FAILED)
  1027. ERR_TO_STR(VK_ERROR_LAYER_NOT_PRESENT)
  1028. ERR_TO_STR(VK_ERROR_EXTENSION_NOT_PRESENT)
  1029. ERR_TO_STR(VK_ERROR_FEATURE_NOT_PRESENT)
  1030. ERR_TO_STR(VK_ERROR_TOO_MANY_OBJECTS)
  1031. ERR_TO_STR(VK_ERROR_DEVICE_LOST)
  1032. ERR_TO_STR(VK_ERROR_INCOMPATIBLE_DRIVER)
  1033. ERR_TO_STR(VK_ERROR_OUT_OF_DATE_KHR)
  1034. ERR_TO_STR(VK_ERROR_SURFACE_LOST_KHR)
  1035. ERR_TO_STR(VK_ERROR_FULL_SCREEN_EXCLUSIVE_MODE_LOST_EXT)
  1036. ERR_TO_STR(VK_SUBOPTIMAL_KHR)
  1037. ERR_TO_STR(VK_ERROR_NATIVE_WINDOW_IN_USE_KHR)
  1038. ERR_TO_STR(VK_ERROR_INVALID_SHADER_NV)
  1039. default:
  1040. return "Unhandled VkResult!";
  1041. }
  1042. #undef ERR_TO_STR
  1043. }
  1044. #define SET_ERROR(fmt, msg) \
  1045. do { \
  1046. if (renderer->debugMode) { \
  1047. SDL_LogError(SDL_LOG_CATEGORY_GPU, fmt, msg); \
  1048. } \
  1049. SDL_SetError((fmt), (msg)); \
  1050. } while (0)
  1051. #define SET_STRING_ERROR(msg) SET_ERROR("%s", msg)
  1052. #define SET_ERROR_AND_RETURN(fmt, msg, ret) \
  1053. do { \
  1054. SET_ERROR(fmt, msg); \
  1055. return ret; \
  1056. } while (0)
  1057. #define SET_STRING_ERROR_AND_RETURN(msg, ret) SET_ERROR_AND_RETURN("%s", msg, ret)
  1058. #define CHECK_VULKAN_ERROR_AND_RETURN(res, fn, ret) \
  1059. do { \
  1060. if ((res) != VK_SUCCESS) { \
  1061. if (renderer->debugMode) { \
  1062. SDL_LogError(SDL_LOG_CATEGORY_GPU, "%s %s", #fn, VkErrorMessages(res)); \
  1063. } \
  1064. SDL_SetError("%s %s", #fn, VkErrorMessages(res)); \
  1065. return (ret); \
  1066. } \
  1067. } while (0)
  1068. // Utility
  1069. static inline VkPolygonMode SDLToVK_PolygonMode(
  1070. VulkanRenderer *renderer,
  1071. SDL_GPUFillMode mode)
  1072. {
  1073. if (mode == SDL_GPU_FILLMODE_FILL) {
  1074. return VK_POLYGON_MODE_FILL; // always available!
  1075. }
  1076. if (renderer->supportsFillModeNonSolid && mode == SDL_GPU_FILLMODE_LINE) {
  1077. return VK_POLYGON_MODE_LINE;
  1078. }
  1079. if (!renderer->fillModeOnlyWarning) {
  1080. SDL_LogWarn(SDL_LOG_CATEGORY_GPU, "Unsupported fill mode requested, using FILL!");
  1081. renderer->fillModeOnlyWarning = 1;
  1082. }
  1083. return VK_POLYGON_MODE_FILL;
  1084. }
  1085. // Memory Management
  1086. // Vulkan: Memory Allocation
  1087. static inline VkDeviceSize VULKAN_INTERNAL_NextHighestAlignment(
  1088. VkDeviceSize n,
  1089. VkDeviceSize align)
  1090. {
  1091. return align * ((n + align - 1) / align);
  1092. }
  1093. static inline Uint32 VULKAN_INTERNAL_NextHighestAlignment32(
  1094. Uint32 n,
  1095. Uint32 align)
  1096. {
  1097. return align * ((n + align - 1) / align);
  1098. }
  1099. static void VULKAN_INTERNAL_MakeMemoryUnavailable(
  1100. VulkanMemoryAllocation *allocation)
  1101. {
  1102. Uint32 i, j;
  1103. VulkanMemoryFreeRegion *freeRegion;
  1104. allocation->availableForAllocation = 0;
  1105. for (i = 0; i < allocation->freeRegionCount; i += 1) {
  1106. freeRegion = allocation->freeRegions[i];
  1107. // close the gap in the sorted list
  1108. if (allocation->allocator->sortedFreeRegionCount > 1) {
  1109. for (j = freeRegion->sortedIndex; j < allocation->allocator->sortedFreeRegionCount - 1; j += 1) {
  1110. allocation->allocator->sortedFreeRegions[j] =
  1111. allocation->allocator->sortedFreeRegions[j + 1];
  1112. allocation->allocator->sortedFreeRegions[j]->sortedIndex = j;
  1113. }
  1114. }
  1115. allocation->allocator->sortedFreeRegionCount -= 1;
  1116. }
  1117. }
  1118. static void VULKAN_INTERNAL_MarkAllocationsForDefrag(
  1119. VulkanRenderer *renderer)
  1120. {
  1121. Uint32 memoryType, allocationIndex;
  1122. VulkanMemorySubAllocator *currentAllocator;
  1123. for (memoryType = 0; memoryType < VK_MAX_MEMORY_TYPES; memoryType += 1) {
  1124. currentAllocator = &renderer->memoryAllocator->subAllocators[memoryType];
  1125. for (allocationIndex = 0; allocationIndex < currentAllocator->allocationCount; allocationIndex += 1) {
  1126. if (currentAllocator->allocations[allocationIndex]->availableForAllocation == 1) {
  1127. if (currentAllocator->allocations[allocationIndex]->freeRegionCount > 1) {
  1128. EXPAND_ARRAY_IF_NEEDED(
  1129. renderer->allocationsToDefrag,
  1130. VulkanMemoryAllocation *,
  1131. renderer->allocationsToDefragCount + 1,
  1132. renderer->allocationsToDefragCapacity,
  1133. renderer->allocationsToDefragCapacity * 2);
  1134. renderer->allocationsToDefrag[renderer->allocationsToDefragCount] =
  1135. currentAllocator->allocations[allocationIndex];
  1136. renderer->allocationsToDefragCount += 1;
  1137. VULKAN_INTERNAL_MakeMemoryUnavailable(
  1138. currentAllocator->allocations[allocationIndex]);
  1139. }
  1140. }
  1141. }
  1142. }
  1143. }
  1144. static void VULKAN_INTERNAL_RemoveMemoryFreeRegion(
  1145. VulkanRenderer *renderer,
  1146. VulkanMemoryFreeRegion *freeRegion)
  1147. {
  1148. Uint32 i;
  1149. SDL_LockMutex(renderer->allocatorLock);
  1150. if (freeRegion->allocation->availableForAllocation) {
  1151. // close the gap in the sorted list
  1152. if (freeRegion->allocation->allocator->sortedFreeRegionCount > 1) {
  1153. for (i = freeRegion->sortedIndex; i < freeRegion->allocation->allocator->sortedFreeRegionCount - 1; i += 1) {
  1154. freeRegion->allocation->allocator->sortedFreeRegions[i] =
  1155. freeRegion->allocation->allocator->sortedFreeRegions[i + 1];
  1156. freeRegion->allocation->allocator->sortedFreeRegions[i]->sortedIndex = i;
  1157. }
  1158. }
  1159. freeRegion->allocation->allocator->sortedFreeRegionCount -= 1;
  1160. }
  1161. // close the gap in the buffer list
  1162. if (freeRegion->allocation->freeRegionCount > 1 && freeRegion->allocationIndex != freeRegion->allocation->freeRegionCount - 1) {
  1163. freeRegion->allocation->freeRegions[freeRegion->allocationIndex] =
  1164. freeRegion->allocation->freeRegions[freeRegion->allocation->freeRegionCount - 1];
  1165. freeRegion->allocation->freeRegions[freeRegion->allocationIndex]->allocationIndex =
  1166. freeRegion->allocationIndex;
  1167. }
  1168. freeRegion->allocation->freeRegionCount -= 1;
  1169. freeRegion->allocation->freeSpace -= freeRegion->size;
  1170. SDL_free(freeRegion);
  1171. SDL_UnlockMutex(renderer->allocatorLock);
  1172. }
  1173. static void VULKAN_INTERNAL_NewMemoryFreeRegion(
  1174. VulkanRenderer *renderer,
  1175. VulkanMemoryAllocation *allocation,
  1176. VkDeviceSize offset,
  1177. VkDeviceSize size)
  1178. {
  1179. VulkanMemoryFreeRegion *newFreeRegion;
  1180. VkDeviceSize newOffset, newSize;
  1181. Sint32 insertionIndex = 0;
  1182. SDL_LockMutex(renderer->allocatorLock);
  1183. // look for an adjacent region to merge
  1184. for (Sint32 i = allocation->freeRegionCount - 1; i >= 0; i -= 1) {
  1185. // check left side
  1186. if (allocation->freeRegions[i]->offset + allocation->freeRegions[i]->size == offset) {
  1187. newOffset = allocation->freeRegions[i]->offset;
  1188. newSize = allocation->freeRegions[i]->size + size;
  1189. VULKAN_INTERNAL_RemoveMemoryFreeRegion(renderer, allocation->freeRegions[i]);
  1190. VULKAN_INTERNAL_NewMemoryFreeRegion(renderer, allocation, newOffset, newSize);
  1191. SDL_UnlockMutex(renderer->allocatorLock);
  1192. return;
  1193. }
  1194. // check right side
  1195. if (allocation->freeRegions[i]->offset == offset + size) {
  1196. newOffset = offset;
  1197. newSize = allocation->freeRegions[i]->size + size;
  1198. VULKAN_INTERNAL_RemoveMemoryFreeRegion(renderer, allocation->freeRegions[i]);
  1199. VULKAN_INTERNAL_NewMemoryFreeRegion(renderer, allocation, newOffset, newSize);
  1200. SDL_UnlockMutex(renderer->allocatorLock);
  1201. return;
  1202. }
  1203. }
  1204. // region is not contiguous with another free region, make a new one
  1205. allocation->freeRegionCount += 1;
  1206. if (allocation->freeRegionCount > allocation->freeRegionCapacity) {
  1207. allocation->freeRegionCapacity *= 2;
  1208. allocation->freeRegions = SDL_realloc(
  1209. allocation->freeRegions,
  1210. sizeof(VulkanMemoryFreeRegion *) * allocation->freeRegionCapacity);
  1211. }
  1212. newFreeRegion = SDL_malloc(sizeof(VulkanMemoryFreeRegion));
  1213. newFreeRegion->offset = offset;
  1214. newFreeRegion->size = size;
  1215. newFreeRegion->allocation = allocation;
  1216. allocation->freeSpace += size;
  1217. allocation->freeRegions[allocation->freeRegionCount - 1] = newFreeRegion;
  1218. newFreeRegion->allocationIndex = allocation->freeRegionCount - 1;
  1219. if (allocation->availableForAllocation) {
  1220. for (Uint32 i = 0; i < allocation->allocator->sortedFreeRegionCount; i += 1) {
  1221. if (allocation->allocator->sortedFreeRegions[i]->size < size) {
  1222. // this is where the new region should go
  1223. break;
  1224. }
  1225. insertionIndex += 1;
  1226. }
  1227. if (allocation->allocator->sortedFreeRegionCount + 1 > allocation->allocator->sortedFreeRegionCapacity) {
  1228. allocation->allocator->sortedFreeRegionCapacity *= 2;
  1229. allocation->allocator->sortedFreeRegions = SDL_realloc(
  1230. allocation->allocator->sortedFreeRegions,
  1231. sizeof(VulkanMemoryFreeRegion *) * allocation->allocator->sortedFreeRegionCapacity);
  1232. }
  1233. // perform insertion sort
  1234. if (allocation->allocator->sortedFreeRegionCount > 0 && (Uint32)insertionIndex != allocation->allocator->sortedFreeRegionCount) {
  1235. for (Sint32 i = allocation->allocator->sortedFreeRegionCount; i > insertionIndex && i > 0; i -= 1) {
  1236. allocation->allocator->sortedFreeRegions[i] = allocation->allocator->sortedFreeRegions[i - 1];
  1237. allocation->allocator->sortedFreeRegions[i]->sortedIndex = i;
  1238. }
  1239. }
  1240. allocation->allocator->sortedFreeRegionCount += 1;
  1241. allocation->allocator->sortedFreeRegions[insertionIndex] = newFreeRegion;
  1242. newFreeRegion->sortedIndex = insertionIndex;
  1243. }
  1244. SDL_UnlockMutex(renderer->allocatorLock);
  1245. }
  1246. static VulkanMemoryUsedRegion *VULKAN_INTERNAL_NewMemoryUsedRegion(
  1247. VulkanRenderer *renderer,
  1248. VulkanMemoryAllocation *allocation,
  1249. VkDeviceSize offset,
  1250. VkDeviceSize size,
  1251. VkDeviceSize resourceOffset,
  1252. VkDeviceSize resourceSize,
  1253. VkDeviceSize alignment)
  1254. {
  1255. VulkanMemoryUsedRegion *memoryUsedRegion;
  1256. SDL_LockMutex(renderer->allocatorLock);
  1257. if (allocation->usedRegionCount == allocation->usedRegionCapacity) {
  1258. allocation->usedRegionCapacity *= 2;
  1259. allocation->usedRegions = SDL_realloc(
  1260. allocation->usedRegions,
  1261. allocation->usedRegionCapacity * sizeof(VulkanMemoryUsedRegion *));
  1262. }
  1263. memoryUsedRegion = SDL_malloc(sizeof(VulkanMemoryUsedRegion));
  1264. memoryUsedRegion->allocation = allocation;
  1265. memoryUsedRegion->offset = offset;
  1266. memoryUsedRegion->size = size;
  1267. memoryUsedRegion->resourceOffset = resourceOffset;
  1268. memoryUsedRegion->resourceSize = resourceSize;
  1269. memoryUsedRegion->alignment = alignment;
  1270. allocation->usedSpace += size;
  1271. allocation->usedRegions[allocation->usedRegionCount] = memoryUsedRegion;
  1272. allocation->usedRegionCount += 1;
  1273. SDL_UnlockMutex(renderer->allocatorLock);
  1274. return memoryUsedRegion;
  1275. }
  1276. static void VULKAN_INTERNAL_RemoveMemoryUsedRegion(
  1277. VulkanRenderer *renderer,
  1278. VulkanMemoryUsedRegion *usedRegion)
  1279. {
  1280. Uint32 i;
  1281. SDL_LockMutex(renderer->allocatorLock);
  1282. for (i = 0; i < usedRegion->allocation->usedRegionCount; i += 1) {
  1283. if (usedRegion->allocation->usedRegions[i] == usedRegion) {
  1284. // plug the hole
  1285. if (i != usedRegion->allocation->usedRegionCount - 1) {
  1286. usedRegion->allocation->usedRegions[i] = usedRegion->allocation->usedRegions[usedRegion->allocation->usedRegionCount - 1];
  1287. }
  1288. break;
  1289. }
  1290. }
  1291. usedRegion->allocation->usedSpace -= usedRegion->size;
  1292. usedRegion->allocation->usedRegionCount -= 1;
  1293. VULKAN_INTERNAL_NewMemoryFreeRegion(
  1294. renderer,
  1295. usedRegion->allocation,
  1296. usedRegion->offset,
  1297. usedRegion->size);
  1298. if (usedRegion->allocation->usedRegionCount == 0) {
  1299. renderer->checkEmptyAllocations = true;
  1300. }
  1301. SDL_free(usedRegion);
  1302. SDL_UnlockMutex(renderer->allocatorLock);
  1303. }
  1304. static bool VULKAN_INTERNAL_CheckMemoryTypeArrayUnique(
  1305. Uint32 memoryTypeIndex,
  1306. const Uint32 *memoryTypeIndexArray,
  1307. Uint32 count)
  1308. {
  1309. Uint32 i = 0;
  1310. for (i = 0; i < count; i += 1) {
  1311. if (memoryTypeIndexArray[i] == memoryTypeIndex) {
  1312. return false;
  1313. }
  1314. }
  1315. return true;
  1316. }
  1317. /* Returns an array of memory type indices in order of preference.
  1318. * Memory types are requested with the following three guidelines:
  1319. *
  1320. * Required: Absolutely necessary
  1321. * Preferred: Nice to have, but not necessary
  1322. * Tolerable: Can be allowed if there are no other options
  1323. *
  1324. * We return memory types in this order:
  1325. * 1. Required and preferred. This is the best category.
  1326. * 2. Required only.
  1327. * 3. Required, preferred, and tolerable.
  1328. * 4. Required and tolerable. This is the worst category.
  1329. */
  1330. static Uint32 *VULKAN_INTERNAL_FindBestMemoryTypes(
  1331. VulkanRenderer *renderer,
  1332. Uint32 typeFilter,
  1333. VkMemoryPropertyFlags requiredProperties,
  1334. VkMemoryPropertyFlags preferredProperties,
  1335. VkMemoryPropertyFlags tolerableProperties,
  1336. Uint32 *pCount)
  1337. {
  1338. Uint32 i;
  1339. Uint32 index = 0;
  1340. Uint32 *result = SDL_malloc(sizeof(Uint32) * renderer->memoryProperties.memoryTypeCount);
  1341. // required + preferred + !tolerable
  1342. for (i = 0; i < renderer->memoryProperties.memoryTypeCount; i += 1) {
  1343. if ((typeFilter & (1 << i)) &&
  1344. (renderer->memoryProperties.memoryTypes[i].propertyFlags & requiredProperties) == requiredProperties &&
  1345. (renderer->memoryProperties.memoryTypes[i].propertyFlags & preferredProperties) == preferredProperties &&
  1346. (renderer->memoryProperties.memoryTypes[i].propertyFlags & tolerableProperties) == 0) {
  1347. if (VULKAN_INTERNAL_CheckMemoryTypeArrayUnique(
  1348. i,
  1349. result,
  1350. index)) {
  1351. result[index] = i;
  1352. index += 1;
  1353. }
  1354. }
  1355. }
  1356. // required + !preferred + !tolerable
  1357. for (i = 0; i < renderer->memoryProperties.memoryTypeCount; i += 1) {
  1358. if ((typeFilter & (1 << i)) &&
  1359. (renderer->memoryProperties.memoryTypes[i].propertyFlags & requiredProperties) == requiredProperties &&
  1360. (renderer->memoryProperties.memoryTypes[i].propertyFlags & preferredProperties) == 0 &&
  1361. (renderer->memoryProperties.memoryTypes[i].propertyFlags & tolerableProperties) == 0) {
  1362. if (VULKAN_INTERNAL_CheckMemoryTypeArrayUnique(
  1363. i,
  1364. result,
  1365. index)) {
  1366. result[index] = i;
  1367. index += 1;
  1368. }
  1369. }
  1370. }
  1371. // required + preferred + tolerable
  1372. for (i = 0; i < renderer->memoryProperties.memoryTypeCount; i += 1) {
  1373. if ((typeFilter & (1 << i)) &&
  1374. (renderer->memoryProperties.memoryTypes[i].propertyFlags & requiredProperties) == requiredProperties &&
  1375. (renderer->memoryProperties.memoryTypes[i].propertyFlags & preferredProperties) == preferredProperties &&
  1376. (renderer->memoryProperties.memoryTypes[i].propertyFlags & tolerableProperties) == tolerableProperties) {
  1377. if (VULKAN_INTERNAL_CheckMemoryTypeArrayUnique(
  1378. i,
  1379. result,
  1380. index)) {
  1381. result[index] = i;
  1382. index += 1;
  1383. }
  1384. }
  1385. }
  1386. // required + !preferred + tolerable
  1387. for (i = 0; i < renderer->memoryProperties.memoryTypeCount; i += 1) {
  1388. if ((typeFilter & (1 << i)) &&
  1389. (renderer->memoryProperties.memoryTypes[i].propertyFlags & requiredProperties) == requiredProperties &&
  1390. (renderer->memoryProperties.memoryTypes[i].propertyFlags & preferredProperties) == 0 &&
  1391. (renderer->memoryProperties.memoryTypes[i].propertyFlags & tolerableProperties) == tolerableProperties) {
  1392. if (VULKAN_INTERNAL_CheckMemoryTypeArrayUnique(
  1393. i,
  1394. result,
  1395. index)) {
  1396. result[index] = i;
  1397. index += 1;
  1398. }
  1399. }
  1400. }
  1401. *pCount = index;
  1402. return result;
  1403. }
  1404. static Uint32 *VULKAN_INTERNAL_FindBestBufferMemoryTypes(
  1405. VulkanRenderer *renderer,
  1406. VkBuffer buffer,
  1407. VkMemoryPropertyFlags requiredMemoryProperties,
  1408. VkMemoryPropertyFlags preferredMemoryProperties,
  1409. VkMemoryPropertyFlags tolerableMemoryProperties,
  1410. VkMemoryRequirements *pMemoryRequirements,
  1411. Uint32 *pCount)
  1412. {
  1413. renderer->vkGetBufferMemoryRequirements(
  1414. renderer->logicalDevice,
  1415. buffer,
  1416. pMemoryRequirements);
  1417. return VULKAN_INTERNAL_FindBestMemoryTypes(
  1418. renderer,
  1419. pMemoryRequirements->memoryTypeBits,
  1420. requiredMemoryProperties,
  1421. preferredMemoryProperties,
  1422. tolerableMemoryProperties,
  1423. pCount);
  1424. }
  1425. static Uint32 *VULKAN_INTERNAL_FindBestImageMemoryTypes(
  1426. VulkanRenderer *renderer,
  1427. VkImage image,
  1428. VkMemoryPropertyFlags preferredMemoryPropertyFlags,
  1429. VkMemoryRequirements *pMemoryRequirements,
  1430. Uint32 *pCount)
  1431. {
  1432. renderer->vkGetImageMemoryRequirements(
  1433. renderer->logicalDevice,
  1434. image,
  1435. pMemoryRequirements);
  1436. return VULKAN_INTERNAL_FindBestMemoryTypes(
  1437. renderer,
  1438. pMemoryRequirements->memoryTypeBits,
  1439. 0,
  1440. preferredMemoryPropertyFlags,
  1441. 0,
  1442. pCount);
  1443. }
  1444. static void VULKAN_INTERNAL_DeallocateMemory(
  1445. VulkanRenderer *renderer,
  1446. VulkanMemorySubAllocator *allocator,
  1447. Uint32 allocationIndex)
  1448. {
  1449. Uint32 i;
  1450. VulkanMemoryAllocation *allocation = allocator->allocations[allocationIndex];
  1451. SDL_LockMutex(renderer->allocatorLock);
  1452. // If this allocation was marked for defrag, cancel that
  1453. for (i = 0; i < renderer->allocationsToDefragCount; i += 1) {
  1454. if (allocation == renderer->allocationsToDefrag[i]) {
  1455. renderer->allocationsToDefrag[i] = renderer->allocationsToDefrag[renderer->allocationsToDefragCount - 1];
  1456. renderer->allocationsToDefragCount -= 1;
  1457. break;
  1458. }
  1459. }
  1460. for (i = 0; i < allocation->freeRegionCount; i += 1) {
  1461. VULKAN_INTERNAL_RemoveMemoryFreeRegion(
  1462. renderer,
  1463. allocation->freeRegions[i]);
  1464. }
  1465. SDL_free(allocation->freeRegions);
  1466. /* no need to iterate used regions because deallocate
  1467. * only happens when there are 0 used regions
  1468. */
  1469. SDL_free(allocation->usedRegions);
  1470. renderer->vkFreeMemory(
  1471. renderer->logicalDevice,
  1472. allocation->memory,
  1473. NULL);
  1474. SDL_DestroyMutex(allocation->memoryLock);
  1475. SDL_free(allocation);
  1476. if (allocationIndex != allocator->allocationCount - 1) {
  1477. allocator->allocations[allocationIndex] = allocator->allocations[allocator->allocationCount - 1];
  1478. }
  1479. allocator->allocationCount -= 1;
  1480. SDL_UnlockMutex(renderer->allocatorLock);
  1481. }
  1482. static Uint8 VULKAN_INTERNAL_AllocateMemory(
  1483. VulkanRenderer *renderer,
  1484. Uint32 memoryTypeIndex,
  1485. VkDeviceSize allocationSize,
  1486. Uint8 isHostVisible,
  1487. VulkanMemoryAllocation **pMemoryAllocation)
  1488. {
  1489. VulkanMemoryAllocation *allocation;
  1490. VulkanMemorySubAllocator *allocator = &renderer->memoryAllocator->subAllocators[memoryTypeIndex];
  1491. VkMemoryAllocateInfo allocInfo;
  1492. VkResult result;
  1493. allocInfo.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
  1494. allocInfo.pNext = NULL;
  1495. allocInfo.memoryTypeIndex = memoryTypeIndex;
  1496. allocInfo.allocationSize = allocationSize;
  1497. allocation = SDL_malloc(sizeof(VulkanMemoryAllocation));
  1498. allocation->size = allocationSize;
  1499. allocation->freeSpace = 0; // added by FreeRegions
  1500. allocation->usedSpace = 0; // added by UsedRegions
  1501. allocation->memoryLock = SDL_CreateMutex();
  1502. allocator->allocationCount += 1;
  1503. allocator->allocations = SDL_realloc(
  1504. allocator->allocations,
  1505. sizeof(VulkanMemoryAllocation *) * allocator->allocationCount);
  1506. allocator->allocations[allocator->allocationCount - 1] = allocation;
  1507. allocInfo.pNext = NULL;
  1508. allocation->availableForAllocation = 1;
  1509. allocation->usedRegions = SDL_malloc(sizeof(VulkanMemoryUsedRegion *));
  1510. allocation->usedRegionCount = 0;
  1511. allocation->usedRegionCapacity = 1;
  1512. allocation->freeRegions = SDL_malloc(sizeof(VulkanMemoryFreeRegion *));
  1513. allocation->freeRegionCount = 0;
  1514. allocation->freeRegionCapacity = 1;
  1515. allocation->allocator = allocator;
  1516. result = renderer->vkAllocateMemory(
  1517. renderer->logicalDevice,
  1518. &allocInfo,
  1519. NULL,
  1520. &allocation->memory);
  1521. if (result != VK_SUCCESS) {
  1522. // Uh oh, we couldn't allocate, time to clean up
  1523. SDL_free(allocation->freeRegions);
  1524. allocator->allocationCount -= 1;
  1525. allocator->allocations = SDL_realloc(
  1526. allocator->allocations,
  1527. sizeof(VulkanMemoryAllocation *) * allocator->allocationCount);
  1528. SDL_free(allocation);
  1529. return 0;
  1530. }
  1531. // Persistent mapping for host-visible memory
  1532. if (isHostVisible) {
  1533. result = renderer->vkMapMemory(
  1534. renderer->logicalDevice,
  1535. allocation->memory,
  1536. 0,
  1537. VK_WHOLE_SIZE,
  1538. 0,
  1539. (void **)&allocation->mapPointer);
  1540. CHECK_VULKAN_ERROR_AND_RETURN(result, vkMapMemory, 0);
  1541. } else {
  1542. allocation->mapPointer = NULL;
  1543. }
  1544. VULKAN_INTERNAL_NewMemoryFreeRegion(
  1545. renderer,
  1546. allocation,
  1547. 0,
  1548. allocation->size);
  1549. *pMemoryAllocation = allocation;
  1550. return 1;
  1551. }
  1552. static Uint8 VULKAN_INTERNAL_BindBufferMemory(
  1553. VulkanRenderer *renderer,
  1554. VulkanMemoryUsedRegion *usedRegion,
  1555. VkDeviceSize alignedOffset,
  1556. VkBuffer buffer)
  1557. {
  1558. VkResult vulkanResult;
  1559. SDL_LockMutex(usedRegion->allocation->memoryLock);
  1560. vulkanResult = renderer->vkBindBufferMemory(
  1561. renderer->logicalDevice,
  1562. buffer,
  1563. usedRegion->allocation->memory,
  1564. alignedOffset);
  1565. SDL_UnlockMutex(usedRegion->allocation->memoryLock);
  1566. CHECK_VULKAN_ERROR_AND_RETURN(vulkanResult, vkBindBufferMemory, 0);
  1567. return 1;
  1568. }
  1569. static Uint8 VULKAN_INTERNAL_BindImageMemory(
  1570. VulkanRenderer *renderer,
  1571. VulkanMemoryUsedRegion *usedRegion,
  1572. VkDeviceSize alignedOffset,
  1573. VkImage image)
  1574. {
  1575. VkResult vulkanResult;
  1576. SDL_LockMutex(usedRegion->allocation->memoryLock);
  1577. vulkanResult = renderer->vkBindImageMemory(
  1578. renderer->logicalDevice,
  1579. image,
  1580. usedRegion->allocation->memory,
  1581. alignedOffset);
  1582. SDL_UnlockMutex(usedRegion->allocation->memoryLock);
  1583. CHECK_VULKAN_ERROR_AND_RETURN(vulkanResult, vkBindImageMemory, 0);
  1584. return 1;
  1585. }
  1586. static Uint8 VULKAN_INTERNAL_BindResourceMemory(
  1587. VulkanRenderer *renderer,
  1588. Uint32 memoryTypeIndex,
  1589. VkMemoryRequirements *memoryRequirements,
  1590. VkDeviceSize resourceSize, // may be different from requirements size!
  1591. bool dedicated, // the entire memory allocation should be used for this resource
  1592. VkBuffer buffer, // may be VK_NULL_HANDLE
  1593. VkImage image, // may be VK_NULL_HANDLE
  1594. VulkanMemoryUsedRegion **pMemoryUsedRegion)
  1595. {
  1596. VulkanMemoryAllocation *allocation;
  1597. VulkanMemorySubAllocator *allocator;
  1598. VulkanMemoryFreeRegion *region;
  1599. VulkanMemoryFreeRegion *selectedRegion;
  1600. VulkanMemoryUsedRegion *usedRegion;
  1601. VkDeviceSize requiredSize, allocationSize;
  1602. VkDeviceSize alignedOffset = 0;
  1603. VkDeviceSize newRegionSize, newRegionOffset;
  1604. Uint8 isHostVisible, smallAllocation, allocationResult;
  1605. Sint32 i;
  1606. isHostVisible =
  1607. (renderer->memoryProperties.memoryTypes[memoryTypeIndex].propertyFlags &
  1608. VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT) != 0;
  1609. allocator = &renderer->memoryAllocator->subAllocators[memoryTypeIndex];
  1610. requiredSize = memoryRequirements->size;
  1611. smallAllocation = requiredSize <= SMALL_ALLOCATION_THRESHOLD;
  1612. if ((buffer == VK_NULL_HANDLE && image == VK_NULL_HANDLE) ||
  1613. (buffer != VK_NULL_HANDLE && image != VK_NULL_HANDLE)) {
  1614. SDL_LogError(SDL_LOG_CATEGORY_GPU, "BindResourceMemory must be given either a VulkanBuffer or a VulkanTexture");
  1615. return 0;
  1616. }
  1617. SDL_LockMutex(renderer->allocatorLock);
  1618. selectedRegion = NULL;
  1619. if (dedicated) {
  1620. // Force an allocation
  1621. allocationSize = requiredSize;
  1622. } else {
  1623. // Search for a suitable existing free region
  1624. for (i = allocator->sortedFreeRegionCount - 1; i >= 0; i -= 1) {
  1625. region = allocator->sortedFreeRegions[i];
  1626. if (smallAllocation && region->allocation->size != SMALL_ALLOCATION_SIZE) {
  1627. // region is not in a small allocation
  1628. continue;
  1629. }
  1630. if (!smallAllocation && region->allocation->size == SMALL_ALLOCATION_SIZE) {
  1631. // allocation is not small and current region is in a small allocation
  1632. continue;
  1633. }
  1634. alignedOffset = VULKAN_INTERNAL_NextHighestAlignment(
  1635. region->offset,
  1636. memoryRequirements->alignment);
  1637. if (alignedOffset + requiredSize <= region->offset + region->size) {
  1638. selectedRegion = region;
  1639. break;
  1640. }
  1641. }
  1642. if (selectedRegion != NULL) {
  1643. region = selectedRegion;
  1644. allocation = region->allocation;
  1645. usedRegion = VULKAN_INTERNAL_NewMemoryUsedRegion(
  1646. renderer,
  1647. allocation,
  1648. region->offset,
  1649. requiredSize + (alignedOffset - region->offset),
  1650. alignedOffset,
  1651. resourceSize,
  1652. memoryRequirements->alignment);
  1653. usedRegion->isBuffer = buffer != VK_NULL_HANDLE;
  1654. newRegionSize = region->size - ((alignedOffset - region->offset) + requiredSize);
  1655. newRegionOffset = alignedOffset + requiredSize;
  1656. // remove and add modified region to re-sort
  1657. VULKAN_INTERNAL_RemoveMemoryFreeRegion(renderer, region);
  1658. // if size is 0, no need to re-insert
  1659. if (newRegionSize != 0) {
  1660. VULKAN_INTERNAL_NewMemoryFreeRegion(
  1661. renderer,
  1662. allocation,
  1663. newRegionOffset,
  1664. newRegionSize);
  1665. }
  1666. SDL_UnlockMutex(renderer->allocatorLock);
  1667. if (buffer != VK_NULL_HANDLE) {
  1668. if (!VULKAN_INTERNAL_BindBufferMemory(
  1669. renderer,
  1670. usedRegion,
  1671. alignedOffset,
  1672. buffer)) {
  1673. VULKAN_INTERNAL_RemoveMemoryUsedRegion(
  1674. renderer,
  1675. usedRegion);
  1676. return 0;
  1677. }
  1678. } else if (image != VK_NULL_HANDLE) {
  1679. if (!VULKAN_INTERNAL_BindImageMemory(
  1680. renderer,
  1681. usedRegion,
  1682. alignedOffset,
  1683. image)) {
  1684. VULKAN_INTERNAL_RemoveMemoryUsedRegion(
  1685. renderer,
  1686. usedRegion);
  1687. return 0;
  1688. }
  1689. }
  1690. *pMemoryUsedRegion = usedRegion;
  1691. return 1;
  1692. }
  1693. // No suitable free regions exist, allocate a new memory region
  1694. if (
  1695. renderer->allocationsToDefragCount == 0 &&
  1696. !renderer->defragInProgress) {
  1697. // Mark currently fragmented allocations for defrag
  1698. VULKAN_INTERNAL_MarkAllocationsForDefrag(renderer);
  1699. }
  1700. if (requiredSize > SMALL_ALLOCATION_THRESHOLD) {
  1701. // allocate a page of required size aligned to LARGE_ALLOCATION_INCREMENT increments
  1702. allocationSize =
  1703. VULKAN_INTERNAL_NextHighestAlignment(requiredSize, LARGE_ALLOCATION_INCREMENT);
  1704. } else {
  1705. allocationSize = SMALL_ALLOCATION_SIZE;
  1706. }
  1707. }
  1708. allocationResult = VULKAN_INTERNAL_AllocateMemory(
  1709. renderer,
  1710. memoryTypeIndex,
  1711. allocationSize,
  1712. isHostVisible,
  1713. &allocation);
  1714. // Uh oh, we're out of memory
  1715. if (allocationResult == 0) {
  1716. SDL_UnlockMutex(renderer->allocatorLock);
  1717. // Responsibility of the caller to handle being out of memory
  1718. return 2;
  1719. }
  1720. usedRegion = VULKAN_INTERNAL_NewMemoryUsedRegion(
  1721. renderer,
  1722. allocation,
  1723. 0,
  1724. requiredSize,
  1725. 0,
  1726. resourceSize,
  1727. memoryRequirements->alignment);
  1728. usedRegion->isBuffer = buffer != VK_NULL_HANDLE;
  1729. region = allocation->freeRegions[0];
  1730. newRegionOffset = region->offset + requiredSize;
  1731. newRegionSize = region->size - requiredSize;
  1732. VULKAN_INTERNAL_RemoveMemoryFreeRegion(renderer, region);
  1733. if (newRegionSize != 0) {
  1734. VULKAN_INTERNAL_NewMemoryFreeRegion(
  1735. renderer,
  1736. allocation,
  1737. newRegionOffset,
  1738. newRegionSize);
  1739. }
  1740. SDL_UnlockMutex(renderer->allocatorLock);
  1741. if (buffer != VK_NULL_HANDLE) {
  1742. if (!VULKAN_INTERNAL_BindBufferMemory(
  1743. renderer,
  1744. usedRegion,
  1745. 0,
  1746. buffer)) {
  1747. VULKAN_INTERNAL_RemoveMemoryUsedRegion(
  1748. renderer,
  1749. usedRegion);
  1750. return 0;
  1751. }
  1752. } else if (image != VK_NULL_HANDLE) {
  1753. if (!VULKAN_INTERNAL_BindImageMemory(
  1754. renderer,
  1755. usedRegion,
  1756. 0,
  1757. image)) {
  1758. VULKAN_INTERNAL_RemoveMemoryUsedRegion(
  1759. renderer,
  1760. usedRegion);
  1761. return 0;
  1762. }
  1763. }
  1764. *pMemoryUsedRegion = usedRegion;
  1765. return 1;
  1766. }
  1767. static Uint8 VULKAN_INTERNAL_BindMemoryForImage(
  1768. VulkanRenderer *renderer,
  1769. VkImage image,
  1770. VulkanMemoryUsedRegion **usedRegion)
  1771. {
  1772. Uint8 bindResult = 0;
  1773. Uint32 memoryTypeCount = 0;
  1774. Uint32 *memoryTypesToTry = NULL;
  1775. Uint32 selectedMemoryTypeIndex = 0;
  1776. Uint32 i;
  1777. VkMemoryPropertyFlags preferredMemoryPropertyFlags;
  1778. VkMemoryRequirements memoryRequirements;
  1779. /* Vulkan memory types have several memory properties.
  1780. *
  1781. * Unlike buffers, images are always optimally stored device-local,
  1782. * so that is the only property we prefer here.
  1783. *
  1784. * If memory is constrained, it is fine for the texture to not
  1785. * be device-local.
  1786. */
  1787. preferredMemoryPropertyFlags = VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT;
  1788. memoryTypesToTry = VULKAN_INTERNAL_FindBestImageMemoryTypes(
  1789. renderer,
  1790. image,
  1791. preferredMemoryPropertyFlags,
  1792. &memoryRequirements,
  1793. &memoryTypeCount);
  1794. for (i = 0; i < memoryTypeCount; i += 1) {
  1795. bindResult = VULKAN_INTERNAL_BindResourceMemory(
  1796. renderer,
  1797. memoryTypesToTry[i],
  1798. &memoryRequirements,
  1799. memoryRequirements.size,
  1800. false,
  1801. VK_NULL_HANDLE,
  1802. image,
  1803. usedRegion);
  1804. if (bindResult == 1) {
  1805. selectedMemoryTypeIndex = memoryTypesToTry[i];
  1806. break;
  1807. }
  1808. }
  1809. SDL_free(memoryTypesToTry);
  1810. // Check for warnings on success
  1811. if (bindResult == 1) {
  1812. if (!renderer->outOfDeviceLocalMemoryWarning) {
  1813. if ((renderer->memoryProperties.memoryTypes[selectedMemoryTypeIndex].propertyFlags & VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT) == 0) {
  1814. SDL_LogWarn(SDL_LOG_CATEGORY_GPU, "Out of device-local memory, allocating textures on host-local memory!");
  1815. renderer->outOfDeviceLocalMemoryWarning = 1;
  1816. }
  1817. }
  1818. }
  1819. return bindResult;
  1820. }
  1821. static Uint8 VULKAN_INTERNAL_BindMemoryForBuffer(
  1822. VulkanRenderer *renderer,
  1823. VkBuffer buffer,
  1824. VkDeviceSize size,
  1825. VulkanBufferType type,
  1826. bool dedicated,
  1827. VulkanMemoryUsedRegion **usedRegion)
  1828. {
  1829. Uint8 bindResult = 0;
  1830. Uint32 memoryTypeCount = 0;
  1831. Uint32 *memoryTypesToTry = NULL;
  1832. Uint32 selectedMemoryTypeIndex = 0;
  1833. Uint32 i;
  1834. VkMemoryPropertyFlags requiredMemoryPropertyFlags = 0;
  1835. VkMemoryPropertyFlags preferredMemoryPropertyFlags = 0;
  1836. VkMemoryPropertyFlags tolerableMemoryPropertyFlags = 0;
  1837. VkMemoryRequirements memoryRequirements;
  1838. /* Buffers need to be optimally bound to a memory type
  1839. * based on their use case and the architecture of the system.
  1840. *
  1841. * It is important to understand the distinction between device and host.
  1842. *
  1843. * On a traditional high-performance desktop computer,
  1844. * the "device" would be the GPU, and the "host" would be the CPU.
  1845. * Memory being copied between these two must cross the PCI bus.
  1846. * On these systems we have to be concerned about bandwidth limitations
  1847. * and causing memory stalls, so we have taken a great deal of care
  1848. * to structure this API to guide the client towards optimal usage.
  1849. *
  1850. * Other kinds of devices do not necessarily have this distinction.
  1851. * On an iPhone or Nintendo Switch, all memory is accessible both to the
  1852. * GPU and the CPU at all times. These kinds of systems are known as
  1853. * UMA, or Unified Memory Architecture. A desktop computer using the
  1854. * CPU's integrated graphics can also be thought of as UMA.
  1855. *
  1856. * Vulkan memory types have several memory properties.
  1857. * The relevant memory properties are as follows:
  1858. *
  1859. * DEVICE_LOCAL:
  1860. * This memory is on-device and most efficient for device access.
  1861. * On UMA systems all memory is device-local.
  1862. * If memory is not device-local, then it is host-local.
  1863. *
  1864. * HOST_VISIBLE:
  1865. * This memory can be mapped for host access, meaning we can obtain
  1866. * a pointer to directly access the memory.
  1867. *
  1868. * HOST_COHERENT:
  1869. * Host-coherent memory does not require cache management operations
  1870. * when mapped, so we always set this alongside HOST_VISIBLE
  1871. * to avoid extra record keeping.
  1872. *
  1873. * HOST_CACHED:
  1874. * Host-cached memory is faster to access than uncached memory
  1875. * but memory of this type might not always be available.
  1876. *
  1877. * GPU buffers, like vertex buffers, indirect buffers, etc
  1878. * are optimally stored in device-local memory.
  1879. * However, if device-local memory is low, these buffers
  1880. * can be accessed from host-local memory with a performance penalty.
  1881. *
  1882. * Uniform buffers must be host-visible and coherent because
  1883. * the client uses them to quickly push small amounts of data.
  1884. * We prefer uniform buffers to also be device-local because
  1885. * they are accessed by shaders, but the amount of memory
  1886. * that is both device-local and host-visible
  1887. * is often constrained, particularly on low-end devices.
  1888. *
  1889. * Transfer buffers must be host-visible and coherent because
  1890. * the client uses them to stage data to be transferred
  1891. * to device-local memory, or to read back data transferred
  1892. * from the device. We prefer the cache bit for performance
  1893. * but it isn't strictly necessary. We tolerate device-local
  1894. * memory in this situation because, as mentioned above,
  1895. * on certain devices all memory is device-local, and even
  1896. * though the transfer isn't strictly necessary it is still
  1897. * useful for correctly timelining data.
  1898. */
  1899. if (type == VULKAN_BUFFER_TYPE_GPU) {
  1900. preferredMemoryPropertyFlags |=
  1901. VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT;
  1902. } else if (type == VULKAN_BUFFER_TYPE_UNIFORM) {
  1903. requiredMemoryPropertyFlags |=
  1904. VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT |
  1905. VK_MEMORY_PROPERTY_HOST_COHERENT_BIT;
  1906. preferredMemoryPropertyFlags |=
  1907. VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT;
  1908. } else if (type == VULKAN_BUFFER_TYPE_TRANSFER) {
  1909. requiredMemoryPropertyFlags |=
  1910. VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT |
  1911. VK_MEMORY_PROPERTY_HOST_COHERENT_BIT;
  1912. preferredMemoryPropertyFlags |=
  1913. VK_MEMORY_PROPERTY_HOST_CACHED_BIT;
  1914. tolerableMemoryPropertyFlags |=
  1915. VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT;
  1916. } else {
  1917. SDL_LogError(SDL_LOG_CATEGORY_GPU, "Unrecognized buffer type!");
  1918. return 0;
  1919. }
  1920. memoryTypesToTry = VULKAN_INTERNAL_FindBestBufferMemoryTypes(
  1921. renderer,
  1922. buffer,
  1923. requiredMemoryPropertyFlags,
  1924. preferredMemoryPropertyFlags,
  1925. tolerableMemoryPropertyFlags,
  1926. &memoryRequirements,
  1927. &memoryTypeCount);
  1928. for (i = 0; i < memoryTypeCount; i += 1) {
  1929. bindResult = VULKAN_INTERNAL_BindResourceMemory(
  1930. renderer,
  1931. memoryTypesToTry[i],
  1932. &memoryRequirements,
  1933. size,
  1934. dedicated,
  1935. buffer,
  1936. VK_NULL_HANDLE,
  1937. usedRegion);
  1938. if (bindResult == 1) {
  1939. selectedMemoryTypeIndex = memoryTypesToTry[i];
  1940. break;
  1941. }
  1942. }
  1943. SDL_free(memoryTypesToTry);
  1944. // Check for warnings on success
  1945. if (bindResult == 1) {
  1946. if (type == VULKAN_BUFFER_TYPE_GPU) {
  1947. if (!renderer->outOfDeviceLocalMemoryWarning) {
  1948. if ((renderer->memoryProperties.memoryTypes[selectedMemoryTypeIndex].propertyFlags & VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT) == 0) {
  1949. SDL_LogWarn(SDL_LOG_CATEGORY_GPU, "Out of device-local memory, allocating buffers on host-local memory, expect degraded performance!");
  1950. renderer->outOfDeviceLocalMemoryWarning = 1;
  1951. }
  1952. }
  1953. } else if (type == VULKAN_BUFFER_TYPE_UNIFORM) {
  1954. if (!renderer->outofBARMemoryWarning) {
  1955. if ((renderer->memoryProperties.memoryTypes[selectedMemoryTypeIndex].propertyFlags & VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT) == 0) {
  1956. SDL_LogWarn(SDL_LOG_CATEGORY_GPU, "Out of BAR memory, allocating uniform buffers on host-local memory, expect degraded performance!");
  1957. renderer->outofBARMemoryWarning = 1;
  1958. }
  1959. }
  1960. } else if (type == VULKAN_BUFFER_TYPE_TRANSFER) {
  1961. if (!renderer->integratedMemoryNotification) {
  1962. if ((renderer->memoryProperties.memoryTypes[selectedMemoryTypeIndex].propertyFlags & VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT) == VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT) {
  1963. SDL_LogInfo(SDL_LOG_CATEGORY_GPU, "Integrated memory detected, allocating TransferBuffers on device-local memory!");
  1964. renderer->integratedMemoryNotification = 1;
  1965. }
  1966. }
  1967. }
  1968. }
  1969. return bindResult;
  1970. }
  1971. // Resource tracking
  1972. #define TRACK_RESOURCE(resource, type, array, count, capacity) \
  1973. for (Sint32 i = commandBuffer->count - 1; i >= 0; i -= 1) { \
  1974. if (commandBuffer->array[i] == resource) { \
  1975. return; \
  1976. } \
  1977. } \
  1978. \
  1979. if (commandBuffer->count == commandBuffer->capacity) { \
  1980. commandBuffer->capacity += 1; \
  1981. commandBuffer->array = SDL_realloc( \
  1982. commandBuffer->array, \
  1983. commandBuffer->capacity * sizeof(type)); \
  1984. } \
  1985. commandBuffer->array[commandBuffer->count] = resource; \
  1986. commandBuffer->count += 1; \
  1987. SDL_AtomicIncRef(&resource->referenceCount);
  1988. static void VULKAN_INTERNAL_TrackBuffer(
  1989. VulkanCommandBuffer *commandBuffer,
  1990. VulkanBuffer *buffer)
  1991. {
  1992. TRACK_RESOURCE(
  1993. buffer,
  1994. VulkanBuffer *,
  1995. usedBuffers,
  1996. usedBufferCount,
  1997. usedBufferCapacity)
  1998. }
  1999. static void VULKAN_INTERNAL_TrackTexture(
  2000. VulkanCommandBuffer *commandBuffer,
  2001. VulkanTexture *texture)
  2002. {
  2003. TRACK_RESOURCE(
  2004. texture,
  2005. VulkanTexture *,
  2006. usedTextures,
  2007. usedTextureCount,
  2008. usedTextureCapacity)
  2009. }
  2010. static void VULKAN_INTERNAL_TrackSampler(
  2011. VulkanCommandBuffer *commandBuffer,
  2012. VulkanSampler *sampler)
  2013. {
  2014. TRACK_RESOURCE(
  2015. sampler,
  2016. VulkanSampler *,
  2017. usedSamplers,
  2018. usedSamplerCount,
  2019. usedSamplerCapacity)
  2020. }
  2021. static void VULKAN_INTERNAL_TrackGraphicsPipeline(
  2022. VulkanCommandBuffer *commandBuffer,
  2023. VulkanGraphicsPipeline *graphicsPipeline)
  2024. {
  2025. TRACK_RESOURCE(
  2026. graphicsPipeline,
  2027. VulkanGraphicsPipeline *,
  2028. usedGraphicsPipelines,
  2029. usedGraphicsPipelineCount,
  2030. usedGraphicsPipelineCapacity)
  2031. }
  2032. static void VULKAN_INTERNAL_TrackComputePipeline(
  2033. VulkanCommandBuffer *commandBuffer,
  2034. VulkanComputePipeline *computePipeline)
  2035. {
  2036. TRACK_RESOURCE(
  2037. computePipeline,
  2038. VulkanComputePipeline *,
  2039. usedComputePipelines,
  2040. usedComputePipelineCount,
  2041. usedComputePipelineCapacity)
  2042. }
  2043. static void VULKAN_INTERNAL_TrackFramebuffer(
  2044. VulkanCommandBuffer *commandBuffer,
  2045. VulkanFramebuffer *framebuffer)
  2046. {
  2047. TRACK_RESOURCE(
  2048. framebuffer,
  2049. VulkanFramebuffer *,
  2050. usedFramebuffers,
  2051. usedFramebufferCount,
  2052. usedFramebufferCapacity);
  2053. }
  2054. static void VULKAN_INTERNAL_TrackUniformBuffer(
  2055. VulkanCommandBuffer *commandBuffer,
  2056. VulkanUniformBuffer *uniformBuffer)
  2057. {
  2058. for (Sint32 i = commandBuffer->usedUniformBufferCount - 1; i >= 0; i -= 1) {
  2059. if (commandBuffer->usedUniformBuffers[i] == uniformBuffer) {
  2060. return;
  2061. }
  2062. }
  2063. if (commandBuffer->usedUniformBufferCount == commandBuffer->usedUniformBufferCapacity) {
  2064. commandBuffer->usedUniformBufferCapacity += 1;
  2065. commandBuffer->usedUniformBuffers = SDL_realloc(
  2066. commandBuffer->usedUniformBuffers,
  2067. commandBuffer->usedUniformBufferCapacity * sizeof(VulkanUniformBuffer *));
  2068. }
  2069. commandBuffer->usedUniformBuffers[commandBuffer->usedUniformBufferCount] = uniformBuffer;
  2070. commandBuffer->usedUniformBufferCount += 1;
  2071. VULKAN_INTERNAL_TrackBuffer(
  2072. commandBuffer,
  2073. uniformBuffer->buffer);
  2074. }
  2075. #undef TRACK_RESOURCE
  2076. // Memory Barriers
  2077. /*
  2078. * In Vulkan, we must manually synchronize operations that write to resources on the GPU
  2079. * so that read-after-write, write-after-read, and write-after-write hazards do not occur.
  2080. * Additionally, textures are required to be in specific layouts for specific use cases.
  2081. * Both of these tasks are accomplished with vkCmdPipelineBarrier.
  2082. *
  2083. * To insert the correct barriers, we keep track of "usage modes" for buffers and textures.
  2084. * These indicate the current usage of that resource on the command buffer.
  2085. * The transition from one usage mode to another indicates how the barrier should be constructed.
  2086. *
  2087. * Pipeline barriers cannot be inserted during a render pass, but they can be inserted
  2088. * during a compute or copy pass.
  2089. *
  2090. * This means that the "default" usage mode of any given resource should be that it should be
  2091. * ready for a graphics-read operation, because we cannot barrier during a render pass.
  2092. * In the case where a resource is only used in compute, its default usage mode can be compute-read.
  2093. * This strategy allows us to avoid expensive record keeping of command buffer/resource usage mode pairs,
  2094. * and it fully covers synchronization between all combinations of stages.
  2095. *
  2096. * In Upload and Copy functions, we transition the resource immediately before and after the copy command.
  2097. *
  2098. * When binding a resource for compute, we transition when the Bind functions are called.
  2099. * If a bind slot containing a resource is overwritten, we transition the resource in that slot back to its default.
  2100. * When EndComputePass is called we transition all bound resources back to their default state.
  2101. *
  2102. * When binding a texture as a render pass attachment, we transition the resource on BeginRenderPass
  2103. * and transition it back to its default on EndRenderPass.
  2104. *
  2105. * This strategy imposes certain limitations on resource usage flags.
  2106. * For example, a texture cannot have both the SAMPLER and GRAPHICS_STORAGE usage flags,
  2107. * because then it is impossible for the backend to infer which default usage mode the texture should use.
  2108. *
  2109. * Sync hazards can be detected by setting VK_KHRONOS_VALIDATION_VALIDATE_SYNC=1 when using validation layers.
  2110. */
  2111. static void VULKAN_INTERNAL_BufferMemoryBarrier(
  2112. VulkanRenderer *renderer,
  2113. VulkanCommandBuffer *commandBuffer,
  2114. VulkanBufferUsageMode sourceUsageMode,
  2115. VulkanBufferUsageMode destinationUsageMode,
  2116. VulkanBuffer *buffer)
  2117. {
  2118. VkPipelineStageFlags srcStages = 0;
  2119. VkPipelineStageFlags dstStages = 0;
  2120. VkBufferMemoryBarrier memoryBarrier;
  2121. memoryBarrier.sType = VK_STRUCTURE_TYPE_BUFFER_MEMORY_BARRIER;
  2122. memoryBarrier.pNext = NULL;
  2123. memoryBarrier.srcAccessMask = 0;
  2124. memoryBarrier.dstAccessMask = 0;
  2125. memoryBarrier.srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
  2126. memoryBarrier.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
  2127. memoryBarrier.buffer = buffer->buffer;
  2128. memoryBarrier.offset = 0;
  2129. memoryBarrier.size = buffer->size;
  2130. if (sourceUsageMode == VULKAN_BUFFER_USAGE_MODE_COPY_SOURCE) {
  2131. srcStages = VK_PIPELINE_STAGE_TRANSFER_BIT;
  2132. memoryBarrier.srcAccessMask = VK_ACCESS_TRANSFER_READ_BIT;
  2133. } else if (sourceUsageMode == VULKAN_BUFFER_USAGE_MODE_COPY_DESTINATION) {
  2134. srcStages = VK_PIPELINE_STAGE_TRANSFER_BIT;
  2135. memoryBarrier.srcAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT;
  2136. } else if (sourceUsageMode == VULKAN_BUFFER_USAGE_MODE_VERTEX_READ) {
  2137. srcStages = VK_PIPELINE_STAGE_VERTEX_INPUT_BIT;
  2138. memoryBarrier.srcAccessMask = VK_ACCESS_VERTEX_ATTRIBUTE_READ_BIT;
  2139. } else if (sourceUsageMode == VULKAN_BUFFER_USAGE_MODE_INDEX_READ) {
  2140. srcStages = VK_PIPELINE_STAGE_VERTEX_INPUT_BIT;
  2141. memoryBarrier.srcAccessMask = VK_ACCESS_INDEX_READ_BIT;
  2142. } else if (sourceUsageMode == VULKAN_BUFFER_USAGE_MODE_INDIRECT) {
  2143. srcStages = VK_PIPELINE_STAGE_DRAW_INDIRECT_BIT;
  2144. memoryBarrier.srcAccessMask = VK_ACCESS_INDIRECT_COMMAND_READ_BIT;
  2145. } else if (sourceUsageMode == VULKAN_BUFFER_USAGE_MODE_GRAPHICS_STORAGE_READ) {
  2146. srcStages = VK_PIPELINE_STAGE_VERTEX_SHADER_BIT | VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT;
  2147. memoryBarrier.srcAccessMask = VK_ACCESS_SHADER_READ_BIT;
  2148. } else if (sourceUsageMode == VULKAN_BUFFER_USAGE_MODE_COMPUTE_STORAGE_READ) {
  2149. srcStages = VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT;
  2150. memoryBarrier.srcAccessMask = VK_ACCESS_SHADER_READ_BIT;
  2151. } else if (sourceUsageMode == VULKAN_BUFFER_USAGE_MODE_COMPUTE_STORAGE_READ_WRITE) {
  2152. srcStages = VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT;
  2153. memoryBarrier.srcAccessMask = VK_ACCESS_SHADER_READ_BIT | VK_ACCESS_SHADER_WRITE_BIT;
  2154. } else {
  2155. SDL_LogError(SDL_LOG_CATEGORY_GPU, "Unrecognized buffer source barrier type!");
  2156. return;
  2157. }
  2158. if (destinationUsageMode == VULKAN_BUFFER_USAGE_MODE_COPY_SOURCE) {
  2159. dstStages = VK_PIPELINE_STAGE_TRANSFER_BIT;
  2160. memoryBarrier.dstAccessMask = VK_ACCESS_TRANSFER_READ_BIT;
  2161. } else if (destinationUsageMode == VULKAN_BUFFER_USAGE_MODE_COPY_DESTINATION) {
  2162. dstStages = VK_PIPELINE_STAGE_TRANSFER_BIT;
  2163. memoryBarrier.dstAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT;
  2164. } else if (destinationUsageMode == VULKAN_BUFFER_USAGE_MODE_VERTEX_READ) {
  2165. dstStages = VK_PIPELINE_STAGE_VERTEX_INPUT_BIT;
  2166. memoryBarrier.dstAccessMask = VK_ACCESS_VERTEX_ATTRIBUTE_READ_BIT;
  2167. } else if (destinationUsageMode == VULKAN_BUFFER_USAGE_MODE_INDEX_READ) {
  2168. dstStages = VK_PIPELINE_STAGE_VERTEX_INPUT_BIT;
  2169. memoryBarrier.dstAccessMask = VK_ACCESS_INDEX_READ_BIT;
  2170. } else if (destinationUsageMode == VULKAN_BUFFER_USAGE_MODE_INDIRECT) {
  2171. dstStages = VK_PIPELINE_STAGE_DRAW_INDIRECT_BIT;
  2172. memoryBarrier.dstAccessMask = VK_ACCESS_INDIRECT_COMMAND_READ_BIT;
  2173. } else if (destinationUsageMode == VULKAN_BUFFER_USAGE_MODE_GRAPHICS_STORAGE_READ) {
  2174. dstStages = VK_PIPELINE_STAGE_VERTEX_SHADER_BIT | VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT;
  2175. memoryBarrier.dstAccessMask = VK_ACCESS_SHADER_READ_BIT;
  2176. } else if (destinationUsageMode == VULKAN_BUFFER_USAGE_MODE_COMPUTE_STORAGE_READ) {
  2177. dstStages = VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT;
  2178. memoryBarrier.dstAccessMask = VK_ACCESS_SHADER_READ_BIT;
  2179. } else if (destinationUsageMode == VULKAN_BUFFER_USAGE_MODE_COMPUTE_STORAGE_READ_WRITE) {
  2180. dstStages = VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT;
  2181. memoryBarrier.dstAccessMask = VK_ACCESS_SHADER_READ_BIT | VK_ACCESS_SHADER_WRITE_BIT;
  2182. } else {
  2183. SDL_LogError(SDL_LOG_CATEGORY_GPU, "Unrecognized buffer destination barrier type!");
  2184. return;
  2185. }
  2186. renderer->vkCmdPipelineBarrier(
  2187. commandBuffer->commandBuffer,
  2188. srcStages,
  2189. dstStages,
  2190. 0,
  2191. 0,
  2192. NULL,
  2193. 1,
  2194. &memoryBarrier,
  2195. 0,
  2196. NULL);
  2197. buffer->transitioned = true;
  2198. }
  2199. static void VULKAN_INTERNAL_TextureSubresourceMemoryBarrier(
  2200. VulkanRenderer *renderer,
  2201. VulkanCommandBuffer *commandBuffer,
  2202. VulkanTextureUsageMode sourceUsageMode,
  2203. VulkanTextureUsageMode destinationUsageMode,
  2204. VulkanTextureSubresource *textureSubresource)
  2205. {
  2206. VkPipelineStageFlags srcStages = 0;
  2207. VkPipelineStageFlags dstStages = 0;
  2208. VkImageMemoryBarrier memoryBarrier;
  2209. memoryBarrier.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER;
  2210. memoryBarrier.pNext = NULL;
  2211. memoryBarrier.srcAccessMask = 0;
  2212. memoryBarrier.dstAccessMask = 0;
  2213. memoryBarrier.oldLayout = VK_IMAGE_LAYOUT_UNDEFINED;
  2214. memoryBarrier.newLayout = VK_IMAGE_LAYOUT_UNDEFINED;
  2215. memoryBarrier.srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
  2216. memoryBarrier.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
  2217. memoryBarrier.image = textureSubresource->parent->image;
  2218. memoryBarrier.subresourceRange.aspectMask = textureSubresource->parent->aspectFlags;
  2219. memoryBarrier.subresourceRange.baseArrayLayer = textureSubresource->layer;
  2220. memoryBarrier.subresourceRange.layerCount = 1;
  2221. memoryBarrier.subresourceRange.baseMipLevel = textureSubresource->level;
  2222. memoryBarrier.subresourceRange.levelCount = 1;
  2223. if (sourceUsageMode == VULKAN_TEXTURE_USAGE_MODE_UNINITIALIZED) {
  2224. srcStages = VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT;
  2225. memoryBarrier.srcAccessMask = 0;
  2226. memoryBarrier.oldLayout = VK_IMAGE_LAYOUT_UNDEFINED;
  2227. } else if (sourceUsageMode == VULKAN_TEXTURE_USAGE_MODE_COPY_SOURCE) {
  2228. srcStages = VK_PIPELINE_STAGE_TRANSFER_BIT;
  2229. memoryBarrier.srcAccessMask = VK_ACCESS_TRANSFER_READ_BIT;
  2230. memoryBarrier.oldLayout = VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL;
  2231. } else if (sourceUsageMode == VULKAN_TEXTURE_USAGE_MODE_COPY_DESTINATION) {
  2232. srcStages = VK_PIPELINE_STAGE_TRANSFER_BIT;
  2233. memoryBarrier.srcAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT;
  2234. memoryBarrier.oldLayout = VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL;
  2235. } else if (sourceUsageMode == VULKAN_TEXTURE_USAGE_MODE_SAMPLER) {
  2236. srcStages = VK_PIPELINE_STAGE_VERTEX_SHADER_BIT | VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT;
  2237. memoryBarrier.srcAccessMask = VK_ACCESS_SHADER_READ_BIT;
  2238. memoryBarrier.oldLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
  2239. } else if (sourceUsageMode == VULKAN_TEXTURE_USAGE_MODE_GRAPHICS_STORAGE_READ) {
  2240. srcStages = VK_PIPELINE_STAGE_VERTEX_SHADER_BIT | VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT;
  2241. memoryBarrier.srcAccessMask = VK_ACCESS_SHADER_READ_BIT;
  2242. memoryBarrier.oldLayout = VK_IMAGE_LAYOUT_GENERAL;
  2243. } else if (sourceUsageMode == VULKAN_TEXTURE_USAGE_MODE_COMPUTE_STORAGE_READ) {
  2244. srcStages = VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT;
  2245. memoryBarrier.srcAccessMask = VK_ACCESS_SHADER_READ_BIT;
  2246. memoryBarrier.oldLayout = VK_IMAGE_LAYOUT_GENERAL;
  2247. } else if (sourceUsageMode == VULKAN_TEXTURE_USAGE_MODE_COMPUTE_STORAGE_READ_WRITE) {
  2248. srcStages = VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT;
  2249. memoryBarrier.srcAccessMask = VK_ACCESS_SHADER_READ_BIT | VK_ACCESS_SHADER_WRITE_BIT;
  2250. memoryBarrier.oldLayout = VK_IMAGE_LAYOUT_GENERAL;
  2251. } else if (sourceUsageMode == VULKAN_TEXTURE_USAGE_MODE_COLOR_ATTACHMENT) {
  2252. srcStages = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
  2253. memoryBarrier.srcAccessMask = VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT;
  2254. memoryBarrier.oldLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
  2255. } else if (sourceUsageMode == VULKAN_TEXTURE_USAGE_MODE_DEPTH_STENCIL_ATTACHMENT) {
  2256. srcStages = VK_PIPELINE_STAGE_EARLY_FRAGMENT_TESTS_BIT | VK_PIPELINE_STAGE_LATE_FRAGMENT_TESTS_BIT;
  2257. memoryBarrier.srcAccessMask = VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT;
  2258. memoryBarrier.oldLayout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL;
  2259. } else {
  2260. SDL_LogError(SDL_LOG_CATEGORY_GPU, "Unrecognized texture source barrier type!");
  2261. return;
  2262. }
  2263. if (destinationUsageMode == VULKAN_TEXTURE_USAGE_MODE_COPY_SOURCE) {
  2264. dstStages = VK_PIPELINE_STAGE_TRANSFER_BIT;
  2265. memoryBarrier.dstAccessMask = VK_ACCESS_TRANSFER_READ_BIT;
  2266. memoryBarrier.newLayout = VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL;
  2267. } else if (destinationUsageMode == VULKAN_TEXTURE_USAGE_MODE_COPY_DESTINATION) {
  2268. dstStages = VK_PIPELINE_STAGE_TRANSFER_BIT;
  2269. memoryBarrier.dstAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT;
  2270. memoryBarrier.newLayout = VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL;
  2271. } else if (destinationUsageMode == VULKAN_TEXTURE_USAGE_MODE_SAMPLER) {
  2272. dstStages = VK_PIPELINE_STAGE_VERTEX_SHADER_BIT | VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT;
  2273. memoryBarrier.dstAccessMask = VK_ACCESS_SHADER_READ_BIT;
  2274. memoryBarrier.newLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
  2275. } else if (destinationUsageMode == VULKAN_TEXTURE_USAGE_MODE_GRAPHICS_STORAGE_READ) {
  2276. dstStages = VK_PIPELINE_STAGE_VERTEX_SHADER_BIT | VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT;
  2277. memoryBarrier.dstAccessMask = VK_ACCESS_SHADER_READ_BIT;
  2278. memoryBarrier.newLayout = VK_IMAGE_LAYOUT_GENERAL;
  2279. } else if (destinationUsageMode == VULKAN_TEXTURE_USAGE_MODE_COMPUTE_STORAGE_READ) {
  2280. dstStages = VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT;
  2281. memoryBarrier.dstAccessMask = VK_ACCESS_SHADER_READ_BIT;
  2282. memoryBarrier.newLayout = VK_IMAGE_LAYOUT_GENERAL;
  2283. } else if (destinationUsageMode == VULKAN_TEXTURE_USAGE_MODE_COMPUTE_STORAGE_READ_WRITE) {
  2284. dstStages = VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT;
  2285. memoryBarrier.dstAccessMask = VK_ACCESS_SHADER_READ_BIT | VK_ACCESS_SHADER_WRITE_BIT;
  2286. memoryBarrier.newLayout = VK_IMAGE_LAYOUT_GENERAL;
  2287. } else if (destinationUsageMode == VULKAN_TEXTURE_USAGE_MODE_COLOR_ATTACHMENT) {
  2288. dstStages = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
  2289. memoryBarrier.dstAccessMask = VK_ACCESS_COLOR_ATTACHMENT_READ_BIT | VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT;
  2290. memoryBarrier.newLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
  2291. } else if (destinationUsageMode == VULKAN_TEXTURE_USAGE_MODE_DEPTH_STENCIL_ATTACHMENT) {
  2292. dstStages = VK_PIPELINE_STAGE_EARLY_FRAGMENT_TESTS_BIT | VK_PIPELINE_STAGE_LATE_FRAGMENT_TESTS_BIT;
  2293. memoryBarrier.dstAccessMask = VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_READ_BIT | VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT;
  2294. memoryBarrier.newLayout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL;
  2295. } else if (destinationUsageMode == VULKAN_TEXTURE_USAGE_MODE_PRESENT) {
  2296. dstStages = VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT;
  2297. memoryBarrier.dstAccessMask = 0;
  2298. memoryBarrier.newLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR;
  2299. } else {
  2300. SDL_LogError(SDL_LOG_CATEGORY_GPU, "Unrecognized texture destination barrier type!");
  2301. return;
  2302. }
  2303. renderer->vkCmdPipelineBarrier(
  2304. commandBuffer->commandBuffer,
  2305. srcStages,
  2306. dstStages,
  2307. 0,
  2308. 0,
  2309. NULL,
  2310. 0,
  2311. NULL,
  2312. 1,
  2313. &memoryBarrier);
  2314. }
  2315. static VulkanBufferUsageMode VULKAN_INTERNAL_DefaultBufferUsageMode(
  2316. VulkanBuffer *buffer)
  2317. {
  2318. // NOTE: order matters here!
  2319. if (buffer->usage & SDL_GPU_BUFFERUSAGE_VERTEX) {
  2320. return VULKAN_BUFFER_USAGE_MODE_VERTEX_READ;
  2321. } else if (buffer->usage & SDL_GPU_BUFFERUSAGE_INDEX) {
  2322. return VULKAN_BUFFER_USAGE_MODE_INDEX_READ;
  2323. } else if (buffer->usage & SDL_GPU_BUFFERUSAGE_INDIRECT) {
  2324. return VULKAN_BUFFER_USAGE_MODE_INDIRECT;
  2325. } else if (buffer->usage & SDL_GPU_BUFFERUSAGE_GRAPHICS_STORAGE_READ) {
  2326. return VULKAN_BUFFER_USAGE_MODE_GRAPHICS_STORAGE_READ;
  2327. } else if (buffer->usage & SDL_GPU_BUFFERUSAGE_COMPUTE_STORAGE_READ) {
  2328. return VULKAN_BUFFER_USAGE_MODE_COMPUTE_STORAGE_READ;
  2329. } else if (buffer->usage & SDL_GPU_BUFFERUSAGE_COMPUTE_STORAGE_WRITE) {
  2330. return VULKAN_BUFFER_USAGE_MODE_COMPUTE_STORAGE_READ_WRITE;
  2331. } else {
  2332. SDL_LogError(SDL_LOG_CATEGORY_GPU, "Buffer has no default usage mode!");
  2333. return VULKAN_BUFFER_USAGE_MODE_VERTEX_READ;
  2334. }
  2335. }
  2336. static VulkanTextureUsageMode VULKAN_INTERNAL_DefaultTextureUsageMode(
  2337. VulkanTexture *texture)
  2338. {
  2339. // NOTE: order matters here!
  2340. // NOTE: graphics storage bits and sampler bit are mutually exclusive!
  2341. if (texture->usage & SDL_GPU_TEXTUREUSAGE_SAMPLER) {
  2342. return VULKAN_TEXTURE_USAGE_MODE_SAMPLER;
  2343. } else if (texture->usage & SDL_GPU_TEXTUREUSAGE_GRAPHICS_STORAGE_READ) {
  2344. return VULKAN_TEXTURE_USAGE_MODE_GRAPHICS_STORAGE_READ;
  2345. } else if (texture->usage & SDL_GPU_TEXTUREUSAGE_COLOR_TARGET) {
  2346. return VULKAN_TEXTURE_USAGE_MODE_COLOR_ATTACHMENT;
  2347. } else if (texture->usage & SDL_GPU_TEXTUREUSAGE_DEPTH_STENCIL_TARGET) {
  2348. return VULKAN_TEXTURE_USAGE_MODE_DEPTH_STENCIL_ATTACHMENT;
  2349. } else if (texture->usage & SDL_GPU_TEXTUREUSAGE_COMPUTE_STORAGE_READ) {
  2350. return VULKAN_TEXTURE_USAGE_MODE_COMPUTE_STORAGE_READ;
  2351. } else if (texture->usage & SDL_GPU_TEXTUREUSAGE_COMPUTE_STORAGE_WRITE) {
  2352. return VULKAN_TEXTURE_USAGE_MODE_COMPUTE_STORAGE_READ_WRITE;
  2353. } else if (texture->usage & SDL_GPU_TEXTUREUSAGE_COMPUTE_STORAGE_SIMULTANEOUS_READ_WRITE) {
  2354. return VULKAN_TEXTURE_USAGE_MODE_COMPUTE_STORAGE_READ_WRITE;
  2355. } else {
  2356. SDL_LogError(SDL_LOG_CATEGORY_GPU, "Texture has no default usage mode!");
  2357. return VULKAN_TEXTURE_USAGE_MODE_SAMPLER;
  2358. }
  2359. }
  2360. static void VULKAN_INTERNAL_BufferTransitionFromDefaultUsage(
  2361. VulkanRenderer *renderer,
  2362. VulkanCommandBuffer *commandBuffer,
  2363. VulkanBufferUsageMode destinationUsageMode,
  2364. VulkanBuffer *buffer)
  2365. {
  2366. VULKAN_INTERNAL_BufferMemoryBarrier(
  2367. renderer,
  2368. commandBuffer,
  2369. VULKAN_INTERNAL_DefaultBufferUsageMode(buffer),
  2370. destinationUsageMode,
  2371. buffer);
  2372. }
  2373. static void VULKAN_INTERNAL_BufferTransitionToDefaultUsage(
  2374. VulkanRenderer *renderer,
  2375. VulkanCommandBuffer *commandBuffer,
  2376. VulkanBufferUsageMode sourceUsageMode,
  2377. VulkanBuffer *buffer)
  2378. {
  2379. VULKAN_INTERNAL_BufferMemoryBarrier(
  2380. renderer,
  2381. commandBuffer,
  2382. sourceUsageMode,
  2383. VULKAN_INTERNAL_DefaultBufferUsageMode(buffer),
  2384. buffer);
  2385. }
  2386. static void VULKAN_INTERNAL_TextureSubresourceTransitionFromDefaultUsage(
  2387. VulkanRenderer *renderer,
  2388. VulkanCommandBuffer *commandBuffer,
  2389. VulkanTextureUsageMode destinationUsageMode,
  2390. VulkanTextureSubresource *textureSubresource)
  2391. {
  2392. VULKAN_INTERNAL_TextureSubresourceMemoryBarrier(
  2393. renderer,
  2394. commandBuffer,
  2395. VULKAN_INTERNAL_DefaultTextureUsageMode(textureSubresource->parent),
  2396. destinationUsageMode,
  2397. textureSubresource);
  2398. }
  2399. static void VULKAN_INTERNAL_TextureTransitionFromDefaultUsage(
  2400. VulkanRenderer *renderer,
  2401. VulkanCommandBuffer *commandBuffer,
  2402. VulkanTextureUsageMode destinationUsageMode,
  2403. VulkanTexture *texture)
  2404. {
  2405. for (Uint32 i = 0; i < texture->subresourceCount; i += 1) {
  2406. VULKAN_INTERNAL_TextureSubresourceTransitionFromDefaultUsage(
  2407. renderer,
  2408. commandBuffer,
  2409. destinationUsageMode,
  2410. &texture->subresources[i]);
  2411. }
  2412. }
  2413. static void VULKAN_INTERNAL_TextureSubresourceTransitionToDefaultUsage(
  2414. VulkanRenderer *renderer,
  2415. VulkanCommandBuffer *commandBuffer,
  2416. VulkanTextureUsageMode sourceUsageMode,
  2417. VulkanTextureSubresource *textureSubresource)
  2418. {
  2419. VULKAN_INTERNAL_TextureSubresourceMemoryBarrier(
  2420. renderer,
  2421. commandBuffer,
  2422. sourceUsageMode,
  2423. VULKAN_INTERNAL_DefaultTextureUsageMode(textureSubresource->parent),
  2424. textureSubresource);
  2425. }
  2426. static void VULKAN_INTERNAL_TextureTransitionToDefaultUsage(
  2427. VulkanRenderer *renderer,
  2428. VulkanCommandBuffer *commandBuffer,
  2429. VulkanTextureUsageMode sourceUsageMode,
  2430. VulkanTexture *texture)
  2431. {
  2432. // FIXME: could optimize this barrier
  2433. for (Uint32 i = 0; i < texture->subresourceCount; i += 1) {
  2434. VULKAN_INTERNAL_TextureSubresourceTransitionToDefaultUsage(
  2435. renderer,
  2436. commandBuffer,
  2437. sourceUsageMode,
  2438. &texture->subresources[i]);
  2439. }
  2440. }
  2441. // Resource Disposal
  2442. static void VULKAN_INTERNAL_ReleaseFramebuffer(
  2443. VulkanRenderer *renderer,
  2444. VulkanFramebuffer *framebuffer)
  2445. {
  2446. SDL_LockMutex(renderer->disposeLock);
  2447. EXPAND_ARRAY_IF_NEEDED(
  2448. renderer->framebuffersToDestroy,
  2449. VulkanFramebuffer *,
  2450. renderer->framebuffersToDestroyCount + 1,
  2451. renderer->framebuffersToDestroyCapacity,
  2452. renderer->framebuffersToDestroyCapacity * 2);
  2453. renderer->framebuffersToDestroy[renderer->framebuffersToDestroyCount] = framebuffer;
  2454. renderer->framebuffersToDestroyCount += 1;
  2455. SDL_UnlockMutex(renderer->disposeLock);
  2456. }
  2457. static void VULKAN_INTERNAL_DestroyFramebuffer(
  2458. VulkanRenderer *renderer,
  2459. VulkanFramebuffer *framebuffer)
  2460. {
  2461. renderer->vkDestroyFramebuffer(
  2462. renderer->logicalDevice,
  2463. framebuffer->framebuffer,
  2464. NULL);
  2465. SDL_free(framebuffer);
  2466. }
  2467. typedef struct CheckOneFramebufferForRemovalData
  2468. {
  2469. Uint32 keysToRemoveCapacity;
  2470. Uint32 keysToRemoveCount;
  2471. FramebufferHashTableKey **keysToRemove;
  2472. VkImageView view;
  2473. } CheckOneFramebufferForRemovalData;
  2474. static bool SDLCALL CheckOneFramebufferForRemoval(void *userdata, const SDL_HashTable *table, const void *vkey, const void *vvalue)
  2475. {
  2476. CheckOneFramebufferForRemovalData *data = (CheckOneFramebufferForRemovalData *) userdata;
  2477. FramebufferHashTableKey *key = (FramebufferHashTableKey *) vkey;
  2478. VkImageView view = data->view;
  2479. bool remove = false;
  2480. for (Uint32 i = 0; i < key->numColorTargets; i += 1) {
  2481. if (key->colorAttachmentViews[i] == view) {
  2482. remove = true;
  2483. }
  2484. }
  2485. for (Uint32 i = 0; i < key->numResolveAttachments; i += 1) {
  2486. if (key->resolveAttachmentViews[i] == view) {
  2487. remove = true;
  2488. }
  2489. }
  2490. if (key->depthStencilAttachmentView == view) {
  2491. remove = true;
  2492. }
  2493. if (remove) {
  2494. if (data->keysToRemoveCount == data->keysToRemoveCapacity) {
  2495. data->keysToRemoveCapacity *= 2;
  2496. void *ptr = SDL_realloc(data->keysToRemove, data->keysToRemoveCapacity * sizeof(FramebufferHashTableKey *));
  2497. if (!ptr) {
  2498. return false; // ugh, stop iterating. We're in trouble.
  2499. }
  2500. data->keysToRemove = (FramebufferHashTableKey **) ptr;
  2501. }
  2502. data->keysToRemove[data->keysToRemoveCount] = key;
  2503. data->keysToRemoveCount++;
  2504. }
  2505. return true; // keep iterating.
  2506. }
  2507. static void VULKAN_INTERNAL_RemoveFramebuffersContainingView(
  2508. VulkanRenderer *renderer,
  2509. VkImageView view)
  2510. {
  2511. // Can't remove while iterating!
  2512. CheckOneFramebufferForRemovalData data = { 8, 0, NULL, view };
  2513. data.keysToRemove = (FramebufferHashTableKey **) SDL_malloc(data.keysToRemoveCapacity * sizeof(FramebufferHashTableKey *));
  2514. if (!data.keysToRemove) {
  2515. return; // uhoh.
  2516. }
  2517. SDL_LockMutex(renderer->framebufferFetchLock);
  2518. SDL_IterateHashTable(renderer->framebufferHashTable, CheckOneFramebufferForRemoval, &data);
  2519. for (Uint32 i = 0; i < data.keysToRemoveCount; i += 1) {
  2520. SDL_RemoveFromHashTable(renderer->framebufferHashTable, (void *)data.keysToRemove[i]);
  2521. }
  2522. SDL_UnlockMutex(renderer->framebufferFetchLock);
  2523. SDL_free(data.keysToRemove);
  2524. }
  2525. static void VULKAN_INTERNAL_DestroyTexture(
  2526. VulkanRenderer *renderer,
  2527. VulkanTexture *texture)
  2528. {
  2529. // Clean up subresources
  2530. for (Uint32 subresourceIndex = 0; subresourceIndex < texture->subresourceCount; subresourceIndex += 1) {
  2531. if (texture->subresources[subresourceIndex].renderTargetViews != NULL) {
  2532. for (Uint32 depthIndex = 0; depthIndex < texture->depth; depthIndex += 1) {
  2533. VULKAN_INTERNAL_RemoveFramebuffersContainingView(
  2534. renderer,
  2535. texture->subresources[subresourceIndex].renderTargetViews[depthIndex]);
  2536. }
  2537. for (Uint32 depthIndex = 0; depthIndex < texture->depth; depthIndex += 1) {
  2538. renderer->vkDestroyImageView(
  2539. renderer->logicalDevice,
  2540. texture->subresources[subresourceIndex].renderTargetViews[depthIndex],
  2541. NULL);
  2542. }
  2543. SDL_free(texture->subresources[subresourceIndex].renderTargetViews);
  2544. }
  2545. if (texture->subresources[subresourceIndex].computeWriteView != VK_NULL_HANDLE) {
  2546. renderer->vkDestroyImageView(
  2547. renderer->logicalDevice,
  2548. texture->subresources[subresourceIndex].computeWriteView,
  2549. NULL);
  2550. }
  2551. if (texture->subresources[subresourceIndex].depthStencilView != VK_NULL_HANDLE) {
  2552. VULKAN_INTERNAL_RemoveFramebuffersContainingView(
  2553. renderer,
  2554. texture->subresources[subresourceIndex].depthStencilView);
  2555. renderer->vkDestroyImageView(
  2556. renderer->logicalDevice,
  2557. texture->subresources[subresourceIndex].depthStencilView,
  2558. NULL);
  2559. }
  2560. }
  2561. SDL_free(texture->subresources);
  2562. if (texture->fullView) {
  2563. renderer->vkDestroyImageView(
  2564. renderer->logicalDevice,
  2565. texture->fullView,
  2566. NULL);
  2567. }
  2568. if (texture->image) {
  2569. renderer->vkDestroyImage(
  2570. renderer->logicalDevice,
  2571. texture->image,
  2572. NULL);
  2573. }
  2574. if (texture->usedRegion) {
  2575. VULKAN_INTERNAL_RemoveMemoryUsedRegion(
  2576. renderer,
  2577. texture->usedRegion);
  2578. }
  2579. SDL_free(texture);
  2580. }
  2581. static void VULKAN_INTERNAL_DestroyBuffer(
  2582. VulkanRenderer *renderer,
  2583. VulkanBuffer *buffer)
  2584. {
  2585. renderer->vkDestroyBuffer(
  2586. renderer->logicalDevice,
  2587. buffer->buffer,
  2588. NULL);
  2589. VULKAN_INTERNAL_RemoveMemoryUsedRegion(
  2590. renderer,
  2591. buffer->usedRegion);
  2592. SDL_free(buffer);
  2593. }
  2594. static void VULKAN_INTERNAL_DestroyCommandPool(
  2595. VulkanRenderer *renderer,
  2596. VulkanCommandPool *commandPool)
  2597. {
  2598. Uint32 i;
  2599. VulkanCommandBuffer *commandBuffer;
  2600. renderer->vkDestroyCommandPool(
  2601. renderer->logicalDevice,
  2602. commandPool->commandPool,
  2603. NULL);
  2604. for (i = 0; i < commandPool->inactiveCommandBufferCount; i += 1) {
  2605. commandBuffer = commandPool->inactiveCommandBuffers[i];
  2606. SDL_free(commandBuffer->presentDatas);
  2607. SDL_free(commandBuffer->waitSemaphores);
  2608. SDL_free(commandBuffer->signalSemaphores);
  2609. SDL_free(commandBuffer->usedBuffers);
  2610. SDL_free(commandBuffer->usedTextures);
  2611. SDL_free(commandBuffer->usedSamplers);
  2612. SDL_free(commandBuffer->usedGraphicsPipelines);
  2613. SDL_free(commandBuffer->usedComputePipelines);
  2614. SDL_free(commandBuffer->usedFramebuffers);
  2615. SDL_free(commandBuffer->usedUniformBuffers);
  2616. SDL_free(commandBuffer);
  2617. }
  2618. SDL_free(commandPool->inactiveCommandBuffers);
  2619. SDL_free(commandPool);
  2620. }
  2621. static void VULKAN_INTERNAL_DestroyDescriptorSetLayout(
  2622. VulkanRenderer *renderer,
  2623. DescriptorSetLayout *layout)
  2624. {
  2625. if (layout == NULL) {
  2626. return;
  2627. }
  2628. if (layout->descriptorSetLayout != VK_NULL_HANDLE) {
  2629. renderer->vkDestroyDescriptorSetLayout(
  2630. renderer->logicalDevice,
  2631. layout->descriptorSetLayout,
  2632. NULL);
  2633. }
  2634. SDL_free(layout);
  2635. }
  2636. static void VULKAN_INTERNAL_DestroyGraphicsPipeline(
  2637. VulkanRenderer *renderer,
  2638. VulkanGraphicsPipeline *graphicsPipeline)
  2639. {
  2640. renderer->vkDestroyPipeline(
  2641. renderer->logicalDevice,
  2642. graphicsPipeline->pipeline,
  2643. NULL);
  2644. (void)SDL_AtomicDecRef(&graphicsPipeline->vertexShader->referenceCount);
  2645. (void)SDL_AtomicDecRef(&graphicsPipeline->fragmentShader->referenceCount);
  2646. SDL_free(graphicsPipeline);
  2647. }
  2648. static void VULKAN_INTERNAL_DestroyComputePipeline(
  2649. VulkanRenderer *renderer,
  2650. VulkanComputePipeline *computePipeline)
  2651. {
  2652. if (computePipeline->pipeline != VK_NULL_HANDLE) {
  2653. renderer->vkDestroyPipeline(
  2654. renderer->logicalDevice,
  2655. computePipeline->pipeline,
  2656. NULL);
  2657. }
  2658. if (computePipeline->shaderModule != VK_NULL_HANDLE) {
  2659. renderer->vkDestroyShaderModule(
  2660. renderer->logicalDevice,
  2661. computePipeline->shaderModule,
  2662. NULL);
  2663. }
  2664. SDL_free(computePipeline);
  2665. }
  2666. static void VULKAN_INTERNAL_DestroyShader(
  2667. VulkanRenderer *renderer,
  2668. VulkanShader *vulkanShader)
  2669. {
  2670. renderer->vkDestroyShaderModule(
  2671. renderer->logicalDevice,
  2672. vulkanShader->shaderModule,
  2673. NULL);
  2674. SDL_free(vulkanShader->entrypointName);
  2675. SDL_free(vulkanShader);
  2676. }
  2677. static void VULKAN_INTERNAL_DestroySampler(
  2678. VulkanRenderer *renderer,
  2679. VulkanSampler *vulkanSampler)
  2680. {
  2681. renderer->vkDestroySampler(
  2682. renderer->logicalDevice,
  2683. vulkanSampler->sampler,
  2684. NULL);
  2685. SDL_free(vulkanSampler);
  2686. }
  2687. static void VULKAN_INTERNAL_DestroySwapchainImage(
  2688. VulkanRenderer *renderer,
  2689. WindowData *windowData)
  2690. {
  2691. Uint32 i;
  2692. if (windowData == NULL) {
  2693. return;
  2694. }
  2695. for (i = 0; i < windowData->imageCount; i += 1) {
  2696. VULKAN_INTERNAL_RemoveFramebuffersContainingView(
  2697. renderer,
  2698. windowData->textureContainers[i].activeTexture->subresources[0].renderTargetViews[0]);
  2699. renderer->vkDestroyImageView(
  2700. renderer->logicalDevice,
  2701. windowData->textureContainers[i].activeTexture->subresources[0].renderTargetViews[0],
  2702. NULL);
  2703. SDL_free(windowData->textureContainers[i].activeTexture->subresources[0].renderTargetViews);
  2704. SDL_free(windowData->textureContainers[i].activeTexture->subresources);
  2705. SDL_free(windowData->textureContainers[i].activeTexture);
  2706. }
  2707. SDL_free(windowData->textureContainers);
  2708. windowData->textureContainers = NULL;
  2709. for (i = 0; i < MAX_FRAMES_IN_FLIGHT; i += 1) {
  2710. if (windowData->imageAvailableSemaphore[i]) {
  2711. renderer->vkDestroySemaphore(
  2712. renderer->logicalDevice,
  2713. windowData->imageAvailableSemaphore[i],
  2714. NULL);
  2715. windowData->imageAvailableSemaphore[i] = VK_NULL_HANDLE;
  2716. }
  2717. }
  2718. for (i = 0; i < windowData->imageCount; i += 1) {
  2719. if (windowData->renderFinishedSemaphore[i]) {
  2720. renderer->vkDestroySemaphore(
  2721. renderer->logicalDevice,
  2722. windowData->renderFinishedSemaphore[i],
  2723. NULL);
  2724. windowData->renderFinishedSemaphore[i] = VK_NULL_HANDLE;
  2725. }
  2726. }
  2727. SDL_free(windowData->renderFinishedSemaphore);
  2728. windowData->renderFinishedSemaphore = NULL;
  2729. windowData->imageCount = 0;
  2730. }
  2731. static void VULKAN_INTERNAL_DestroySwapchain(
  2732. VulkanRenderer *renderer,
  2733. WindowData *windowData)
  2734. {
  2735. if (windowData == NULL) {
  2736. return;
  2737. }
  2738. VULKAN_INTERNAL_DestroySwapchainImage(renderer, windowData);
  2739. if (windowData->swapchain) {
  2740. renderer->vkDestroySwapchainKHR(
  2741. renderer->logicalDevice,
  2742. windowData->swapchain,
  2743. NULL);
  2744. windowData->swapchain = VK_NULL_HANDLE;
  2745. }
  2746. if (windowData->surface) {
  2747. renderer->vkDestroySurfaceKHR(
  2748. renderer->instance,
  2749. windowData->surface,
  2750. NULL);
  2751. windowData->surface = VK_NULL_HANDLE;
  2752. }
  2753. }
  2754. static void VULKAN_INTERNAL_DestroyGraphicsPipelineResourceLayout(
  2755. VulkanRenderer *renderer,
  2756. VulkanGraphicsPipelineResourceLayout *resourceLayout)
  2757. {
  2758. if (resourceLayout->pipelineLayout != VK_NULL_HANDLE) {
  2759. renderer->vkDestroyPipelineLayout(
  2760. renderer->logicalDevice,
  2761. resourceLayout->pipelineLayout,
  2762. NULL);
  2763. }
  2764. SDL_free(resourceLayout);
  2765. }
  2766. static void VULKAN_INTERNAL_DestroyComputePipelineResourceLayout(
  2767. VulkanRenderer *renderer,
  2768. VulkanComputePipelineResourceLayout *resourceLayout)
  2769. {
  2770. if (resourceLayout->pipelineLayout != VK_NULL_HANDLE) {
  2771. renderer->vkDestroyPipelineLayout(
  2772. renderer->logicalDevice,
  2773. resourceLayout->pipelineLayout,
  2774. NULL);
  2775. }
  2776. SDL_free(resourceLayout);
  2777. }
  2778. static void VULKAN_INTERNAL_DestroyDescriptorSetCache(
  2779. VulkanRenderer *renderer,
  2780. DescriptorSetCache *descriptorSetCache)
  2781. {
  2782. for (Uint32 i = 0; i < descriptorSetCache->poolCount; i += 1) {
  2783. for (Uint32 j = 0; j < descriptorSetCache->pools[i].poolCount; j += 1) {
  2784. renderer->vkDestroyDescriptorPool(
  2785. renderer->logicalDevice,
  2786. descriptorSetCache->pools[i].descriptorPools[j],
  2787. NULL);
  2788. }
  2789. SDL_free(descriptorSetCache->pools[i].descriptorSets);
  2790. SDL_free(descriptorSetCache->pools[i].descriptorPools);
  2791. }
  2792. SDL_free(descriptorSetCache->pools);
  2793. SDL_free(descriptorSetCache);
  2794. }
  2795. // Hashtable functions
  2796. static Uint32 SDLCALL VULKAN_INTERNAL_GraphicsPipelineResourceLayoutHashFunction(void *userdata, const void *key)
  2797. {
  2798. GraphicsPipelineResourceLayoutHashTableKey *hashTableKey = (GraphicsPipelineResourceLayoutHashTableKey *)key;
  2799. /* The algorithm for this hashing function
  2800. * is taken from Josh Bloch's "Effective Java".
  2801. * (https://stackoverflow.com/a/113600/12492383)
  2802. */
  2803. const Uint32 hashFactor = 31;
  2804. Uint32 result = 1;
  2805. result = result * hashFactor + hashTableKey->vertexSamplerCount;
  2806. result = result * hashFactor + hashTableKey->vertexStorageBufferCount;
  2807. result = result * hashFactor + hashTableKey->vertexStorageTextureCount;
  2808. result = result * hashFactor + hashTableKey->vertexUniformBufferCount;
  2809. result = result * hashFactor + hashTableKey->fragmentSamplerCount;
  2810. result = result * hashFactor + hashTableKey->fragmentStorageBufferCount;
  2811. result = result * hashFactor + hashTableKey->fragmentStorageTextureCount;
  2812. result = result * hashFactor + hashTableKey->fragmentUniformBufferCount;
  2813. return result;
  2814. }
  2815. static bool SDLCALL VULKAN_INTERNAL_GraphicsPipelineResourceLayoutHashKeyMatch(void *userdata, const void *aKey, const void *bKey)
  2816. {
  2817. return SDL_memcmp(aKey, bKey, sizeof(GraphicsPipelineResourceLayoutHashTableKey)) == 0;
  2818. }
  2819. static void SDLCALL VULKAN_INTERNAL_GraphicsPipelineResourceLayoutHashDestroy(void *userdata, const void *key, const void *value)
  2820. {
  2821. VulkanRenderer *renderer = (VulkanRenderer *)userdata;
  2822. VulkanGraphicsPipelineResourceLayout *resourceLayout = (VulkanGraphicsPipelineResourceLayout *)value;
  2823. VULKAN_INTERNAL_DestroyGraphicsPipelineResourceLayout(renderer, resourceLayout);
  2824. SDL_free((void *)key);
  2825. }
  2826. static Uint32 SDLCALL VULKAN_INTERNAL_ComputePipelineResourceLayoutHashFunction(void *userdata, const void *key)
  2827. {
  2828. ComputePipelineResourceLayoutHashTableKey *hashTableKey = (ComputePipelineResourceLayoutHashTableKey *)key;
  2829. /* The algorithm for this hashing function
  2830. * is taken from Josh Bloch's "Effective Java".
  2831. * (https://stackoverflow.com/a/113600/12492383)
  2832. */
  2833. const Uint32 hashFactor = 31;
  2834. Uint32 result = 1;
  2835. result = result * hashFactor + hashTableKey->samplerCount;
  2836. result = result * hashFactor + hashTableKey->readonlyStorageTextureCount;
  2837. result = result * hashFactor + hashTableKey->readonlyStorageBufferCount;
  2838. result = result * hashFactor + hashTableKey->readWriteStorageTextureCount;
  2839. result = result * hashFactor + hashTableKey->readWriteStorageBufferCount;
  2840. result = result * hashFactor + hashTableKey->uniformBufferCount;
  2841. return result;
  2842. }
  2843. static bool SDLCALL VULKAN_INTERNAL_ComputePipelineResourceLayoutHashKeyMatch(void *userdata, const void *aKey, const void *bKey)
  2844. {
  2845. return SDL_memcmp(aKey, bKey, sizeof(ComputePipelineResourceLayoutHashTableKey)) == 0;
  2846. }
  2847. static void SDLCALL VULKAN_INTERNAL_ComputePipelineResourceLayoutHashDestroy(void *userdata, const void *key, const void *value)
  2848. {
  2849. VulkanRenderer *renderer = (VulkanRenderer *)userdata;
  2850. VulkanComputePipelineResourceLayout *resourceLayout = (VulkanComputePipelineResourceLayout *)value;
  2851. VULKAN_INTERNAL_DestroyComputePipelineResourceLayout(renderer, resourceLayout);
  2852. SDL_free((void *)key);
  2853. }
  2854. static Uint32 SDLCALL VULKAN_INTERNAL_DescriptorSetLayoutHashFunction(void *userdata, const void *key)
  2855. {
  2856. DescriptorSetLayoutHashTableKey *hashTableKey = (DescriptorSetLayoutHashTableKey *)key;
  2857. /* The algorithm for this hashing function
  2858. * is taken from Josh Bloch's "Effective Java".
  2859. * (https://stackoverflow.com/a/113600/12492383)
  2860. */
  2861. const Uint32 hashFactor = 31;
  2862. Uint32 result = 1;
  2863. result = result * hashFactor + hashTableKey->shaderStage;
  2864. result = result * hashFactor + hashTableKey->samplerCount;
  2865. result = result * hashFactor + hashTableKey->storageTextureCount;
  2866. result = result * hashFactor + hashTableKey->storageBufferCount;
  2867. result = result * hashFactor + hashTableKey->writeStorageTextureCount;
  2868. result = result * hashFactor + hashTableKey->writeStorageBufferCount;
  2869. result = result * hashFactor + hashTableKey->uniformBufferCount;
  2870. return result;
  2871. }
  2872. static bool SDLCALL VULKAN_INTERNAL_DescriptorSetLayoutHashKeyMatch(void *userdata, const void *aKey, const void *bKey)
  2873. {
  2874. return SDL_memcmp(aKey, bKey, sizeof(DescriptorSetLayoutHashTableKey)) == 0;
  2875. }
  2876. static void SDLCALL VULKAN_INTERNAL_DescriptorSetLayoutHashDestroy(void *userdata, const void *key, const void *value)
  2877. {
  2878. VulkanRenderer *renderer = (VulkanRenderer *)userdata;
  2879. DescriptorSetLayout *layout = (DescriptorSetLayout *)value;
  2880. VULKAN_INTERNAL_DestroyDescriptorSetLayout(renderer, layout);
  2881. SDL_free((void *)key);
  2882. }
  2883. static Uint32 SDLCALL VULKAN_INTERNAL_CommandPoolHashFunction(void *userdata, const void *key)
  2884. {
  2885. return (Uint32)((CommandPoolHashTableKey *)key)->threadID;
  2886. }
  2887. static bool SDLCALL VULKAN_INTERNAL_CommandPoolHashKeyMatch(void *userdata, const void *aKey, const void *bKey)
  2888. {
  2889. CommandPoolHashTableKey *a = (CommandPoolHashTableKey *)aKey;
  2890. CommandPoolHashTableKey *b = (CommandPoolHashTableKey *)bKey;
  2891. return a->threadID == b->threadID;
  2892. }
  2893. static void SDLCALL VULKAN_INTERNAL_CommandPoolHashDestroy(void *userdata, const void *key, const void *value)
  2894. {
  2895. VulkanRenderer *renderer = (VulkanRenderer *)userdata;
  2896. VulkanCommandPool *pool = (VulkanCommandPool *)value;
  2897. VULKAN_INTERNAL_DestroyCommandPool(renderer, pool);
  2898. SDL_free((void *)key);
  2899. }
  2900. static Uint32 SDLCALL VULKAN_INTERNAL_RenderPassHashFunction(void *userdata, const void *key)
  2901. {
  2902. RenderPassHashTableKey *hashTableKey = (RenderPassHashTableKey *)key;
  2903. /* The algorithm for this hashing function
  2904. * is taken from Josh Bloch's "Effective Java".
  2905. * (https://stackoverflow.com/a/113600/12492383)
  2906. */
  2907. const Uint32 hashFactor = 31;
  2908. Uint32 result = 1;
  2909. for (Uint32 i = 0; i < hashTableKey->numColorTargets; i += 1) {
  2910. result = result * hashFactor + hashTableKey->colorTargetDescriptions[i].loadOp;
  2911. result = result * hashFactor + hashTableKey->colorTargetDescriptions[i].storeOp;
  2912. result = result * hashFactor + hashTableKey->colorTargetDescriptions[i].format;
  2913. }
  2914. for (Uint32 i = 0; i < hashTableKey->numResolveTargets; i += 1) {
  2915. result = result * hashFactor + hashTableKey->resolveTargetFormats[i];
  2916. }
  2917. result = result * hashFactor + hashTableKey->depthStencilTargetDescription.loadOp;
  2918. result = result * hashFactor + hashTableKey->depthStencilTargetDescription.storeOp;
  2919. result = result * hashFactor + hashTableKey->depthStencilTargetDescription.stencilLoadOp;
  2920. result = result * hashFactor + hashTableKey->depthStencilTargetDescription.stencilStoreOp;
  2921. result = result * hashFactor + hashTableKey->depthStencilTargetDescription.format;
  2922. result = result * hashFactor + hashTableKey->sampleCount;
  2923. return result;
  2924. }
  2925. static bool SDLCALL VULKAN_INTERNAL_RenderPassHashKeyMatch(void *userdata, const void *aKey, const void *bKey)
  2926. {
  2927. RenderPassHashTableKey *a = (RenderPassHashTableKey *)aKey;
  2928. RenderPassHashTableKey *b = (RenderPassHashTableKey *)bKey;
  2929. if (a->numColorTargets != b->numColorTargets) {
  2930. return 0;
  2931. }
  2932. if (a->numResolveTargets != b->numResolveTargets) {
  2933. return 0;
  2934. }
  2935. if (a->sampleCount != b->sampleCount) {
  2936. return 0;
  2937. }
  2938. for (Uint32 i = 0; i < a->numColorTargets; i += 1) {
  2939. if (a->colorTargetDescriptions[i].format != b->colorTargetDescriptions[i].format) {
  2940. return 0;
  2941. }
  2942. if (a->colorTargetDescriptions[i].loadOp != b->colorTargetDescriptions[i].loadOp) {
  2943. return 0;
  2944. }
  2945. if (a->colorTargetDescriptions[i].storeOp != b->colorTargetDescriptions[i].storeOp) {
  2946. return 0;
  2947. }
  2948. }
  2949. for (Uint32 i = 0; i < a->numResolveTargets; i += 1) {
  2950. if (a->resolveTargetFormats[i] != b->resolveTargetFormats[i]) {
  2951. return 0;
  2952. }
  2953. }
  2954. if (a->depthStencilTargetDescription.format != b->depthStencilTargetDescription.format) {
  2955. return 0;
  2956. }
  2957. if (a->depthStencilTargetDescription.loadOp != b->depthStencilTargetDescription.loadOp) {
  2958. return 0;
  2959. }
  2960. if (a->depthStencilTargetDescription.storeOp != b->depthStencilTargetDescription.storeOp) {
  2961. return 0;
  2962. }
  2963. if (a->depthStencilTargetDescription.stencilLoadOp != b->depthStencilTargetDescription.stencilLoadOp) {
  2964. return 0;
  2965. }
  2966. if (a->depthStencilTargetDescription.stencilStoreOp != b->depthStencilTargetDescription.stencilStoreOp) {
  2967. return 0;
  2968. }
  2969. return 1;
  2970. }
  2971. static void SDLCALL VULKAN_INTERNAL_RenderPassHashDestroy(void *userdata, const void *key, const void *value)
  2972. {
  2973. VulkanRenderer *renderer = (VulkanRenderer *)userdata;
  2974. VulkanRenderPassHashTableValue *renderPassWrapper = (VulkanRenderPassHashTableValue *)value;
  2975. renderer->vkDestroyRenderPass(
  2976. renderer->logicalDevice,
  2977. renderPassWrapper->handle,
  2978. NULL);
  2979. SDL_free(renderPassWrapper);
  2980. SDL_free((void *)key);
  2981. }
  2982. static Uint32 SDLCALL VULKAN_INTERNAL_FramebufferHashFunction(void *userdata, const void *key)
  2983. {
  2984. FramebufferHashTableKey *hashTableKey = (FramebufferHashTableKey *)key;
  2985. /* The algorithm for this hashing function
  2986. * is taken from Josh Bloch's "Effective Java".
  2987. * (https://stackoverflow.com/a/113600/12492383)
  2988. */
  2989. const Uint32 hashFactor = 31;
  2990. Uint32 result = 1;
  2991. for (Uint32 i = 0; i < hashTableKey->numColorTargets; i += 1) {
  2992. result = result * hashFactor + (Uint32)(uintptr_t)hashTableKey->colorAttachmentViews[i];
  2993. }
  2994. for (Uint32 i = 0; i < hashTableKey->numResolveAttachments; i += 1) {
  2995. result = result * hashFactor + (Uint32)(uintptr_t)hashTableKey->resolveAttachmentViews[i];
  2996. }
  2997. result = result * hashFactor + (Uint32)(uintptr_t)hashTableKey->depthStencilAttachmentView;
  2998. result = result * hashFactor + hashTableKey->width;
  2999. result = result * hashFactor + hashTableKey->height;
  3000. return result;
  3001. }
  3002. static bool SDLCALL VULKAN_INTERNAL_FramebufferHashKeyMatch(void *userdata, const void *aKey, const void *bKey)
  3003. {
  3004. FramebufferHashTableKey *a = (FramebufferHashTableKey *)aKey;
  3005. FramebufferHashTableKey *b = (FramebufferHashTableKey *)bKey;
  3006. if (a->numColorTargets != b->numColorTargets) {
  3007. return 0;
  3008. }
  3009. if (a->numResolveAttachments != b->numResolveAttachments) {
  3010. return 0;
  3011. }
  3012. for (Uint32 i = 0; i < a->numColorTargets; i += 1) {
  3013. if (a->colorAttachmentViews[i] != b->colorAttachmentViews[i]) {
  3014. return 0;
  3015. }
  3016. }
  3017. for (Uint32 i = 0; i < a->numResolveAttachments; i += 1) {
  3018. if (a->resolveAttachmentViews[i] != b->resolveAttachmentViews[i]) {
  3019. return 0;
  3020. }
  3021. }
  3022. if (a->depthStencilAttachmentView != b->depthStencilAttachmentView) {
  3023. return 0;
  3024. }
  3025. if (a->width != b->width) {
  3026. return 0;
  3027. }
  3028. if (a->height != b->height) {
  3029. return 0;
  3030. }
  3031. return 1;
  3032. }
  3033. static void SDLCALL VULKAN_INTERNAL_FramebufferHashDestroy(void *userdata, const void *key, const void *value)
  3034. {
  3035. VulkanRenderer *renderer = (VulkanRenderer *)userdata;
  3036. VulkanFramebuffer *framebuffer = (VulkanFramebuffer *)value;
  3037. VULKAN_INTERNAL_ReleaseFramebuffer(renderer, framebuffer);
  3038. SDL_free((void *)key);
  3039. }
  3040. // Descriptor pools
  3041. static bool VULKAN_INTERNAL_AllocateDescriptorSets(
  3042. VulkanRenderer *renderer,
  3043. VkDescriptorPool descriptorPool,
  3044. VkDescriptorSetLayout descriptorSetLayout,
  3045. Uint32 descriptorSetCount,
  3046. VkDescriptorSet *descriptorSetArray)
  3047. {
  3048. VkDescriptorSetAllocateInfo descriptorSetAllocateInfo;
  3049. VkDescriptorSetLayout *descriptorSetLayouts = SDL_stack_alloc(VkDescriptorSetLayout, descriptorSetCount);
  3050. VkResult vulkanResult;
  3051. Uint32 i;
  3052. for (i = 0; i < descriptorSetCount; i += 1) {
  3053. descriptorSetLayouts[i] = descriptorSetLayout;
  3054. }
  3055. descriptorSetAllocateInfo.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO;
  3056. descriptorSetAllocateInfo.pNext = NULL;
  3057. descriptorSetAllocateInfo.descriptorPool = descriptorPool;
  3058. descriptorSetAllocateInfo.descriptorSetCount = descriptorSetCount;
  3059. descriptorSetAllocateInfo.pSetLayouts = descriptorSetLayouts;
  3060. vulkanResult = renderer->vkAllocateDescriptorSets(
  3061. renderer->logicalDevice,
  3062. &descriptorSetAllocateInfo,
  3063. descriptorSetArray);
  3064. SDL_stack_free(descriptorSetLayouts);
  3065. CHECK_VULKAN_ERROR_AND_RETURN(vulkanResult, vkAllocateDescriptorSets, false);
  3066. return true;
  3067. }
  3068. static bool VULKAN_INTERNAL_AllocateDescriptorsFromPool(
  3069. VulkanRenderer *renderer,
  3070. DescriptorSetLayout *descriptorSetLayout,
  3071. DescriptorSetPool *descriptorSetPool)
  3072. {
  3073. VkDescriptorPoolSize descriptorPoolSizes[
  3074. MAX_TEXTURE_SAMPLERS_PER_STAGE +
  3075. MAX_STORAGE_TEXTURES_PER_STAGE +
  3076. MAX_STORAGE_BUFFERS_PER_STAGE +
  3077. MAX_COMPUTE_WRITE_TEXTURES +
  3078. MAX_COMPUTE_WRITE_BUFFERS +
  3079. MAX_UNIFORM_BUFFERS_PER_STAGE];
  3080. VkDescriptorPoolCreateInfo descriptorPoolInfo;
  3081. VkDescriptorPool pool;
  3082. VkResult vulkanResult;
  3083. // Category 1
  3084. for (Uint32 i = 0; i < descriptorSetLayout->samplerCount; i += 1) {
  3085. descriptorPoolSizes[i].type = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
  3086. descriptorPoolSizes[i].descriptorCount = DESCRIPTOR_POOL_SIZE;
  3087. }
  3088. for (Uint32 i = descriptorSetLayout->samplerCount; i < descriptorSetLayout->samplerCount + descriptorSetLayout->storageTextureCount; i += 1) {
  3089. descriptorPoolSizes[i].type = VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE; // Yes, we are declaring the storage image as a sampled image, because shaders are stupid.
  3090. descriptorPoolSizes[i].descriptorCount = DESCRIPTOR_POOL_SIZE;
  3091. }
  3092. for (Uint32 i = descriptorSetLayout->samplerCount + descriptorSetLayout->storageTextureCount; i < descriptorSetLayout->samplerCount + descriptorSetLayout->storageTextureCount + descriptorSetLayout->storageBufferCount; i += 1) {
  3093. descriptorPoolSizes[i].type = VK_DESCRIPTOR_TYPE_STORAGE_BUFFER;
  3094. descriptorPoolSizes[i].descriptorCount = DESCRIPTOR_POOL_SIZE;
  3095. }
  3096. // Category 2
  3097. for (Uint32 i = 0; i < descriptorSetLayout->writeStorageTextureCount; i += 1) {
  3098. descriptorPoolSizes[i].type = VK_DESCRIPTOR_TYPE_STORAGE_IMAGE;
  3099. descriptorPoolSizes[i].descriptorCount = DESCRIPTOR_POOL_SIZE;
  3100. }
  3101. for (Uint32 i = descriptorSetLayout->writeStorageTextureCount; i < descriptorSetLayout->writeStorageTextureCount + descriptorSetLayout->writeStorageBufferCount; i += 1) {
  3102. descriptorPoolSizes[i].type = VK_DESCRIPTOR_TYPE_STORAGE_BUFFER;
  3103. descriptorPoolSizes[i].descriptorCount = DESCRIPTOR_POOL_SIZE;
  3104. }
  3105. // Category 3
  3106. for (Uint32 i = 0; i < descriptorSetLayout->uniformBufferCount; i += 1) {
  3107. descriptorPoolSizes[i].type = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC;
  3108. descriptorPoolSizes[i].descriptorCount = DESCRIPTOR_POOL_SIZE;
  3109. }
  3110. descriptorPoolInfo.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO;
  3111. descriptorPoolInfo.pNext = NULL;
  3112. descriptorPoolInfo.flags = 0;
  3113. descriptorPoolInfo.maxSets = DESCRIPTOR_POOL_SIZE;
  3114. descriptorPoolInfo.poolSizeCount =
  3115. descriptorSetLayout->samplerCount +
  3116. descriptorSetLayout->storageTextureCount +
  3117. descriptorSetLayout->storageBufferCount +
  3118. descriptorSetLayout->writeStorageTextureCount +
  3119. descriptorSetLayout->writeStorageBufferCount +
  3120. descriptorSetLayout->uniformBufferCount;
  3121. descriptorPoolInfo.pPoolSizes = descriptorPoolSizes;
  3122. vulkanResult = renderer->vkCreateDescriptorPool(
  3123. renderer->logicalDevice,
  3124. &descriptorPoolInfo,
  3125. NULL,
  3126. &pool);
  3127. CHECK_VULKAN_ERROR_AND_RETURN(vulkanResult, vkCreateDescriptorPool, false);
  3128. descriptorSetPool->poolCount += 1;
  3129. descriptorSetPool->descriptorPools = SDL_realloc(
  3130. descriptorSetPool->descriptorPools,
  3131. sizeof(VkDescriptorPool) * descriptorSetPool->poolCount);
  3132. descriptorSetPool->descriptorPools[descriptorSetPool->poolCount - 1] = pool;
  3133. descriptorSetPool->descriptorSets = SDL_realloc(
  3134. descriptorSetPool->descriptorSets,
  3135. sizeof(VkDescriptorSet) * descriptorSetPool->poolCount * DESCRIPTOR_POOL_SIZE);
  3136. if (!VULKAN_INTERNAL_AllocateDescriptorSets(
  3137. renderer,
  3138. pool,
  3139. descriptorSetLayout->descriptorSetLayout,
  3140. DESCRIPTOR_POOL_SIZE,
  3141. &descriptorSetPool->descriptorSets[descriptorSetPool->descriptorSetCount])) {
  3142. return false;
  3143. }
  3144. descriptorSetPool->descriptorSetCount += DESCRIPTOR_POOL_SIZE;
  3145. return true;
  3146. }
  3147. // NOTE: these categories should be mutually exclusive
  3148. static DescriptorSetLayout *VULKAN_INTERNAL_FetchDescriptorSetLayout(
  3149. VulkanRenderer *renderer,
  3150. VkShaderStageFlagBits shaderStage,
  3151. // Category 1: read resources
  3152. Uint32 samplerCount,
  3153. Uint32 storageTextureCount,
  3154. Uint32 storageBufferCount,
  3155. // Category 2: write resources
  3156. Uint32 writeStorageTextureCount,
  3157. Uint32 writeStorageBufferCount,
  3158. // Category 3: uniform buffers
  3159. Uint32 uniformBufferCount)
  3160. {
  3161. DescriptorSetLayoutHashTableKey key;
  3162. SDL_zero(key);
  3163. DescriptorSetLayout *layout = NULL;
  3164. key.shaderStage = shaderStage;
  3165. key.samplerCount = samplerCount;
  3166. key.storageTextureCount = storageTextureCount;
  3167. key.storageBufferCount = storageBufferCount;
  3168. key.writeStorageTextureCount = writeStorageTextureCount;
  3169. key.writeStorageBufferCount = writeStorageBufferCount;
  3170. key.uniformBufferCount = uniformBufferCount;
  3171. SDL_LockMutex(renderer->descriptorSetLayoutFetchLock);
  3172. if (SDL_FindInHashTable(
  3173. renderer->descriptorSetLayoutHashTable,
  3174. (const void *)&key,
  3175. (const void **)&layout)) {
  3176. SDL_UnlockMutex(renderer->descriptorSetLayoutFetchLock);
  3177. return layout;
  3178. }
  3179. VkDescriptorSetLayout descriptorSetLayout;
  3180. VkDescriptorSetLayoutBinding descriptorSetLayoutBindings[
  3181. MAX_TEXTURE_SAMPLERS_PER_STAGE +
  3182. MAX_STORAGE_TEXTURES_PER_STAGE +
  3183. MAX_STORAGE_BUFFERS_PER_STAGE +
  3184. MAX_COMPUTE_WRITE_TEXTURES +
  3185. MAX_COMPUTE_WRITE_BUFFERS];
  3186. VkDescriptorSetLayoutCreateInfo descriptorSetLayoutCreateInfo;
  3187. descriptorSetLayoutCreateInfo.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO;
  3188. descriptorSetLayoutCreateInfo.pNext = NULL;
  3189. descriptorSetLayoutCreateInfo.flags = 0;
  3190. // Category 1
  3191. for (Uint32 i = 0; i < samplerCount; i += 1) {
  3192. descriptorSetLayoutBindings[i].binding = i;
  3193. descriptorSetLayoutBindings[i].descriptorCount = 1;
  3194. descriptorSetLayoutBindings[i].descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
  3195. descriptorSetLayoutBindings[i].stageFlags = shaderStage;
  3196. descriptorSetLayoutBindings[i].pImmutableSamplers = NULL;
  3197. }
  3198. for (Uint32 i = samplerCount; i < samplerCount + storageTextureCount; i += 1) {
  3199. descriptorSetLayoutBindings[i].binding = i;
  3200. descriptorSetLayoutBindings[i].descriptorCount = 1;
  3201. descriptorSetLayoutBindings[i].descriptorType = VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE; // Yes, we are declaring the storage image as a sampled image, because shaders are stupid.
  3202. descriptorSetLayoutBindings[i].stageFlags = shaderStage;
  3203. descriptorSetLayoutBindings[i].pImmutableSamplers = NULL;
  3204. }
  3205. for (Uint32 i = samplerCount + storageTextureCount; i < samplerCount + storageTextureCount + storageBufferCount; i += 1) {
  3206. descriptorSetLayoutBindings[i].binding = i;
  3207. descriptorSetLayoutBindings[i].descriptorCount = 1;
  3208. descriptorSetLayoutBindings[i].descriptorType = VK_DESCRIPTOR_TYPE_STORAGE_BUFFER;
  3209. descriptorSetLayoutBindings[i].stageFlags = shaderStage;
  3210. descriptorSetLayoutBindings[i].pImmutableSamplers = NULL;
  3211. }
  3212. // Category 2
  3213. for (Uint32 i = 0; i < writeStorageTextureCount; i += 1) {
  3214. descriptorSetLayoutBindings[i].binding = i;
  3215. descriptorSetLayoutBindings[i].descriptorCount = 1;
  3216. descriptorSetLayoutBindings[i].descriptorType = VK_DESCRIPTOR_TYPE_STORAGE_IMAGE;
  3217. descriptorSetLayoutBindings[i].stageFlags = shaderStage;
  3218. descriptorSetLayoutBindings[i].pImmutableSamplers = NULL;
  3219. }
  3220. for (Uint32 i = writeStorageTextureCount; i < writeStorageTextureCount + writeStorageBufferCount; i += 1) {
  3221. descriptorSetLayoutBindings[i].binding = i;
  3222. descriptorSetLayoutBindings[i].descriptorCount = 1;
  3223. descriptorSetLayoutBindings[i].descriptorType = VK_DESCRIPTOR_TYPE_STORAGE_BUFFER;
  3224. descriptorSetLayoutBindings[i].stageFlags = shaderStage;
  3225. descriptorSetLayoutBindings[i].pImmutableSamplers = NULL;
  3226. }
  3227. // Category 3
  3228. for (Uint32 i = 0; i < uniformBufferCount; i += 1) {
  3229. descriptorSetLayoutBindings[i].binding = i;
  3230. descriptorSetLayoutBindings[i].descriptorCount = 1;
  3231. descriptorSetLayoutBindings[i].descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC;
  3232. descriptorSetLayoutBindings[i].stageFlags = shaderStage;
  3233. descriptorSetLayoutBindings[i].pImmutableSamplers = NULL;
  3234. }
  3235. descriptorSetLayoutCreateInfo.pBindings = descriptorSetLayoutBindings;
  3236. descriptorSetLayoutCreateInfo.bindingCount =
  3237. samplerCount +
  3238. storageTextureCount +
  3239. storageBufferCount +
  3240. writeStorageTextureCount +
  3241. writeStorageBufferCount +
  3242. uniformBufferCount;
  3243. VkResult vulkanResult = renderer->vkCreateDescriptorSetLayout(
  3244. renderer->logicalDevice,
  3245. &descriptorSetLayoutCreateInfo,
  3246. NULL,
  3247. &descriptorSetLayout);
  3248. if (vulkanResult != VK_SUCCESS) {
  3249. SDL_UnlockMutex(renderer->descriptorSetLayoutFetchLock);
  3250. CHECK_VULKAN_ERROR_AND_RETURN(vulkanResult, vkCreateDescriptorSetLayout, NULL);
  3251. }
  3252. layout = SDL_malloc(sizeof(DescriptorSetLayout));
  3253. layout->descriptorSetLayout = descriptorSetLayout;
  3254. layout->samplerCount = samplerCount;
  3255. layout->storageBufferCount = storageBufferCount;
  3256. layout->storageTextureCount = storageTextureCount;
  3257. layout->writeStorageBufferCount = writeStorageBufferCount;
  3258. layout->writeStorageTextureCount = writeStorageTextureCount;
  3259. layout->uniformBufferCount = uniformBufferCount;
  3260. layout->ID = SDL_AtomicIncRef(&renderer->layoutResourceID);
  3261. DescriptorSetLayoutHashTableKey *allocedKey = SDL_malloc(sizeof(DescriptorSetLayoutHashTableKey));
  3262. SDL_memcpy(allocedKey, &key, sizeof(DescriptorSetLayoutHashTableKey));
  3263. SDL_InsertIntoHashTable(
  3264. renderer->descriptorSetLayoutHashTable,
  3265. (const void *)allocedKey,
  3266. (const void *)layout, true);
  3267. SDL_UnlockMutex(renderer->descriptorSetLayoutFetchLock);
  3268. return layout;
  3269. }
  3270. static VulkanGraphicsPipelineResourceLayout *VULKAN_INTERNAL_FetchGraphicsPipelineResourceLayout(
  3271. VulkanRenderer *renderer,
  3272. VulkanShader *vertexShader,
  3273. VulkanShader *fragmentShader)
  3274. {
  3275. GraphicsPipelineResourceLayoutHashTableKey key;
  3276. SDL_zero(key);
  3277. VulkanGraphicsPipelineResourceLayout *pipelineResourceLayout = NULL;
  3278. key.vertexSamplerCount = vertexShader->numSamplers;
  3279. key.vertexStorageTextureCount = vertexShader->numStorageTextures;
  3280. key.vertexStorageBufferCount = vertexShader->numStorageBuffers;
  3281. key.vertexUniformBufferCount = vertexShader->numUniformBuffers;
  3282. key.fragmentSamplerCount = fragmentShader->numSamplers;
  3283. key.fragmentStorageTextureCount = fragmentShader->numStorageTextures;
  3284. key.fragmentStorageBufferCount = fragmentShader->numStorageBuffers;
  3285. key.fragmentUniformBufferCount = fragmentShader->numUniformBuffers;
  3286. SDL_LockMutex(renderer->graphicsPipelineLayoutFetchLock);
  3287. if (SDL_FindInHashTable(
  3288. renderer->graphicsPipelineResourceLayoutHashTable,
  3289. (const void *)&key,
  3290. (const void **)&pipelineResourceLayout)) {
  3291. SDL_UnlockMutex(renderer->graphicsPipelineLayoutFetchLock);
  3292. return pipelineResourceLayout;
  3293. }
  3294. VkPipelineLayoutCreateInfo pipelineLayoutCreateInfo;
  3295. VkDescriptorSetLayout descriptorSetLayouts[4];
  3296. VkResult vulkanResult;
  3297. pipelineResourceLayout = SDL_calloc(1, sizeof(VulkanGraphicsPipelineResourceLayout));
  3298. pipelineResourceLayout->descriptorSetLayouts[0] = VULKAN_INTERNAL_FetchDescriptorSetLayout(
  3299. renderer,
  3300. VK_SHADER_STAGE_VERTEX_BIT,
  3301. vertexShader->numSamplers,
  3302. vertexShader->numStorageTextures,
  3303. vertexShader->numStorageBuffers,
  3304. 0,
  3305. 0,
  3306. 0);
  3307. pipelineResourceLayout->descriptorSetLayouts[1] = VULKAN_INTERNAL_FetchDescriptorSetLayout(
  3308. renderer,
  3309. VK_SHADER_STAGE_VERTEX_BIT,
  3310. 0,
  3311. 0,
  3312. 0,
  3313. 0,
  3314. 0,
  3315. vertexShader->numUniformBuffers);
  3316. pipelineResourceLayout->descriptorSetLayouts[2] = VULKAN_INTERNAL_FetchDescriptorSetLayout(
  3317. renderer,
  3318. VK_SHADER_STAGE_FRAGMENT_BIT,
  3319. fragmentShader->numSamplers,
  3320. fragmentShader->numStorageTextures,
  3321. fragmentShader->numStorageBuffers,
  3322. 0,
  3323. 0,
  3324. 0);
  3325. pipelineResourceLayout->descriptorSetLayouts[3] = VULKAN_INTERNAL_FetchDescriptorSetLayout(
  3326. renderer,
  3327. VK_SHADER_STAGE_FRAGMENT_BIT,
  3328. 0,
  3329. 0,
  3330. 0,
  3331. 0,
  3332. 0,
  3333. fragmentShader->numUniformBuffers);
  3334. descriptorSetLayouts[0] = pipelineResourceLayout->descriptorSetLayouts[0]->descriptorSetLayout;
  3335. descriptorSetLayouts[1] = pipelineResourceLayout->descriptorSetLayouts[1]->descriptorSetLayout;
  3336. descriptorSetLayouts[2] = pipelineResourceLayout->descriptorSetLayouts[2]->descriptorSetLayout;
  3337. descriptorSetLayouts[3] = pipelineResourceLayout->descriptorSetLayouts[3]->descriptorSetLayout;
  3338. pipelineResourceLayout->vertexSamplerCount = vertexShader->numSamplers;
  3339. pipelineResourceLayout->vertexStorageTextureCount = vertexShader->numStorageTextures;
  3340. pipelineResourceLayout->vertexStorageBufferCount = vertexShader->numStorageBuffers;
  3341. pipelineResourceLayout->vertexUniformBufferCount = vertexShader->numUniformBuffers;
  3342. pipelineResourceLayout->fragmentSamplerCount = fragmentShader->numSamplers;
  3343. pipelineResourceLayout->fragmentStorageTextureCount = fragmentShader->numStorageTextures;
  3344. pipelineResourceLayout->fragmentStorageBufferCount = fragmentShader->numStorageBuffers;
  3345. pipelineResourceLayout->fragmentUniformBufferCount = fragmentShader->numUniformBuffers;
  3346. // Create the pipeline layout
  3347. pipelineLayoutCreateInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO;
  3348. pipelineLayoutCreateInfo.pNext = NULL;
  3349. pipelineLayoutCreateInfo.flags = 0;
  3350. pipelineLayoutCreateInfo.setLayoutCount = 4;
  3351. pipelineLayoutCreateInfo.pSetLayouts = descriptorSetLayouts;
  3352. pipelineLayoutCreateInfo.pushConstantRangeCount = 0;
  3353. pipelineLayoutCreateInfo.pPushConstantRanges = NULL;
  3354. vulkanResult = renderer->vkCreatePipelineLayout(
  3355. renderer->logicalDevice,
  3356. &pipelineLayoutCreateInfo,
  3357. NULL,
  3358. &pipelineResourceLayout->pipelineLayout);
  3359. if (vulkanResult != VK_SUCCESS) {
  3360. VULKAN_INTERNAL_DestroyGraphicsPipelineResourceLayout(renderer, pipelineResourceLayout);
  3361. SDL_UnlockMutex(renderer->graphicsPipelineLayoutFetchLock);
  3362. CHECK_VULKAN_ERROR_AND_RETURN(vulkanResult, vkCreatePipelineLayout, NULL);
  3363. }
  3364. GraphicsPipelineResourceLayoutHashTableKey *allocedKey = SDL_malloc(sizeof(GraphicsPipelineResourceLayoutHashTableKey));
  3365. SDL_memcpy(allocedKey, &key, sizeof(GraphicsPipelineResourceLayoutHashTableKey));
  3366. SDL_InsertIntoHashTable(
  3367. renderer->graphicsPipelineResourceLayoutHashTable,
  3368. (const void *)allocedKey,
  3369. (const void *)pipelineResourceLayout, true);
  3370. SDL_UnlockMutex(renderer->graphicsPipelineLayoutFetchLock);
  3371. return pipelineResourceLayout;
  3372. }
  3373. static VulkanComputePipelineResourceLayout *VULKAN_INTERNAL_FetchComputePipelineResourceLayout(
  3374. VulkanRenderer *renderer,
  3375. const SDL_GPUComputePipelineCreateInfo *createinfo)
  3376. {
  3377. ComputePipelineResourceLayoutHashTableKey key;
  3378. SDL_zero(key);
  3379. VulkanComputePipelineResourceLayout *pipelineResourceLayout = NULL;
  3380. key.samplerCount = createinfo->num_samplers;
  3381. key.readonlyStorageTextureCount = createinfo->num_readonly_storage_textures;
  3382. key.readonlyStorageBufferCount = createinfo->num_readonly_storage_buffers;
  3383. key.readWriteStorageTextureCount = createinfo->num_readwrite_storage_textures;
  3384. key.readWriteStorageBufferCount = createinfo->num_readwrite_storage_buffers;
  3385. key.uniformBufferCount = createinfo->num_uniform_buffers;
  3386. SDL_LockMutex(renderer->computePipelineLayoutFetchLock);
  3387. if (SDL_FindInHashTable(
  3388. renderer->computePipelineResourceLayoutHashTable,
  3389. (const void *)&key,
  3390. (const void **)&pipelineResourceLayout)) {
  3391. SDL_UnlockMutex(renderer->computePipelineLayoutFetchLock);
  3392. return pipelineResourceLayout;
  3393. }
  3394. VkDescriptorSetLayout descriptorSetLayouts[3];
  3395. VkPipelineLayoutCreateInfo pipelineLayoutCreateInfo;
  3396. VkResult vulkanResult;
  3397. pipelineResourceLayout = SDL_calloc(1, sizeof(VulkanComputePipelineResourceLayout));
  3398. pipelineResourceLayout->descriptorSetLayouts[0] = VULKAN_INTERNAL_FetchDescriptorSetLayout(
  3399. renderer,
  3400. VK_SHADER_STAGE_COMPUTE_BIT,
  3401. createinfo->num_samplers,
  3402. createinfo->num_readonly_storage_textures,
  3403. createinfo->num_readonly_storage_buffers,
  3404. 0,
  3405. 0,
  3406. 0);
  3407. pipelineResourceLayout->descriptorSetLayouts[1] = VULKAN_INTERNAL_FetchDescriptorSetLayout(
  3408. renderer,
  3409. VK_SHADER_STAGE_COMPUTE_BIT,
  3410. 0,
  3411. 0,
  3412. 0,
  3413. createinfo->num_readwrite_storage_textures,
  3414. createinfo->num_readwrite_storage_buffers,
  3415. 0);
  3416. pipelineResourceLayout->descriptorSetLayouts[2] = VULKAN_INTERNAL_FetchDescriptorSetLayout(
  3417. renderer,
  3418. VK_SHADER_STAGE_COMPUTE_BIT,
  3419. 0,
  3420. 0,
  3421. 0,
  3422. 0,
  3423. 0,
  3424. createinfo->num_uniform_buffers);
  3425. descriptorSetLayouts[0] = pipelineResourceLayout->descriptorSetLayouts[0]->descriptorSetLayout;
  3426. descriptorSetLayouts[1] = pipelineResourceLayout->descriptorSetLayouts[1]->descriptorSetLayout;
  3427. descriptorSetLayouts[2] = pipelineResourceLayout->descriptorSetLayouts[2]->descriptorSetLayout;
  3428. pipelineResourceLayout->numSamplers = createinfo->num_samplers;
  3429. pipelineResourceLayout->numReadonlyStorageTextures = createinfo->num_readonly_storage_textures;
  3430. pipelineResourceLayout->numReadonlyStorageBuffers = createinfo->num_readonly_storage_buffers;
  3431. pipelineResourceLayout->numReadWriteStorageTextures = createinfo->num_readwrite_storage_textures;
  3432. pipelineResourceLayout->numReadWriteStorageBuffers = createinfo->num_readwrite_storage_buffers;
  3433. pipelineResourceLayout->numUniformBuffers = createinfo->num_uniform_buffers;
  3434. // Create the pipeline layout
  3435. pipelineLayoutCreateInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO;
  3436. pipelineLayoutCreateInfo.pNext = NULL;
  3437. pipelineLayoutCreateInfo.flags = 0;
  3438. pipelineLayoutCreateInfo.setLayoutCount = 3;
  3439. pipelineLayoutCreateInfo.pSetLayouts = descriptorSetLayouts;
  3440. pipelineLayoutCreateInfo.pushConstantRangeCount = 0;
  3441. pipelineLayoutCreateInfo.pPushConstantRanges = NULL;
  3442. vulkanResult = renderer->vkCreatePipelineLayout(
  3443. renderer->logicalDevice,
  3444. &pipelineLayoutCreateInfo,
  3445. NULL,
  3446. &pipelineResourceLayout->pipelineLayout);
  3447. if (vulkanResult != VK_SUCCESS) {
  3448. VULKAN_INTERNAL_DestroyComputePipelineResourceLayout(renderer, pipelineResourceLayout);
  3449. SDL_UnlockMutex(renderer->computePipelineLayoutFetchLock);
  3450. CHECK_VULKAN_ERROR_AND_RETURN(vulkanResult, vkCreatePipelineLayout, NULL);
  3451. }
  3452. ComputePipelineResourceLayoutHashTableKey *allocedKey = SDL_malloc(sizeof(ComputePipelineResourceLayoutHashTableKey));
  3453. SDL_memcpy(allocedKey, &key, sizeof(ComputePipelineResourceLayoutHashTableKey));
  3454. SDL_InsertIntoHashTable(
  3455. renderer->computePipelineResourceLayoutHashTable,
  3456. (const void *)allocedKey,
  3457. (const void *)pipelineResourceLayout, true);
  3458. SDL_UnlockMutex(renderer->computePipelineLayoutFetchLock);
  3459. return pipelineResourceLayout;
  3460. }
  3461. // Data Buffer
  3462. static VulkanBuffer *VULKAN_INTERNAL_CreateBuffer(
  3463. VulkanRenderer *renderer,
  3464. VkDeviceSize size,
  3465. SDL_GPUBufferUsageFlags usageFlags,
  3466. VulkanBufferType type,
  3467. bool dedicated,
  3468. const char *debugName)
  3469. {
  3470. VulkanBuffer *buffer;
  3471. VkResult vulkanResult;
  3472. VkBufferCreateInfo createinfo;
  3473. VkBufferUsageFlags vulkanUsageFlags = 0;
  3474. Uint8 bindResult;
  3475. if (usageFlags & SDL_GPU_BUFFERUSAGE_VERTEX) {
  3476. vulkanUsageFlags |= VK_BUFFER_USAGE_VERTEX_BUFFER_BIT;
  3477. }
  3478. if (usageFlags & SDL_GPU_BUFFERUSAGE_INDEX) {
  3479. vulkanUsageFlags |= VK_BUFFER_USAGE_INDEX_BUFFER_BIT;
  3480. }
  3481. if (usageFlags & (SDL_GPU_BUFFERUSAGE_GRAPHICS_STORAGE_READ |
  3482. SDL_GPU_BUFFERUSAGE_COMPUTE_STORAGE_READ |
  3483. SDL_GPU_BUFFERUSAGE_COMPUTE_STORAGE_WRITE)) {
  3484. vulkanUsageFlags |= VK_BUFFER_USAGE_STORAGE_BUFFER_BIT;
  3485. }
  3486. if (usageFlags & SDL_GPU_BUFFERUSAGE_INDIRECT) {
  3487. vulkanUsageFlags |= VK_BUFFER_USAGE_INDIRECT_BUFFER_BIT;
  3488. }
  3489. if (type == VULKAN_BUFFER_TYPE_UNIFORM) {
  3490. vulkanUsageFlags |= VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT;
  3491. } else {
  3492. // GPU buffers need transfer bits for defrag, transfer buffers need them for transfers
  3493. vulkanUsageFlags |= VK_BUFFER_USAGE_TRANSFER_SRC_BIT | VK_BUFFER_USAGE_TRANSFER_DST_BIT;
  3494. }
  3495. buffer = SDL_calloc(1, sizeof(VulkanBuffer));
  3496. buffer->size = size;
  3497. buffer->usage = usageFlags;
  3498. buffer->type = type;
  3499. buffer->markedForDestroy = false;
  3500. buffer->transitioned = false;
  3501. createinfo.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
  3502. createinfo.pNext = NULL;
  3503. createinfo.flags = 0;
  3504. createinfo.size = size;
  3505. createinfo.usage = vulkanUsageFlags;
  3506. createinfo.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
  3507. createinfo.queueFamilyIndexCount = 1;
  3508. createinfo.pQueueFamilyIndices = &renderer->queueFamilyIndex;
  3509. // Set transfer bits so we can defrag
  3510. createinfo.usage |= VK_BUFFER_USAGE_TRANSFER_SRC_BIT | VK_BUFFER_USAGE_TRANSFER_DST_BIT;
  3511. vulkanResult = renderer->vkCreateBuffer(
  3512. renderer->logicalDevice,
  3513. &createinfo,
  3514. NULL,
  3515. &buffer->buffer);
  3516. if (vulkanResult != VK_SUCCESS) {
  3517. SDL_free(buffer);
  3518. CHECK_VULKAN_ERROR_AND_RETURN(vulkanResult, vkCreateBuffer, NULL);
  3519. }
  3520. bindResult = VULKAN_INTERNAL_BindMemoryForBuffer(
  3521. renderer,
  3522. buffer->buffer,
  3523. buffer->size,
  3524. buffer->type,
  3525. dedicated,
  3526. &buffer->usedRegion);
  3527. if (bindResult != 1) {
  3528. renderer->vkDestroyBuffer(
  3529. renderer->logicalDevice,
  3530. buffer->buffer,
  3531. NULL);
  3532. SDL_free(buffer);
  3533. return NULL;
  3534. }
  3535. buffer->usedRegion->vulkanBuffer = buffer; // lol
  3536. SDL_SetAtomicInt(&buffer->referenceCount, 0);
  3537. if (renderer->debugMode && renderer->supportsDebugUtils && debugName != NULL) {
  3538. VkDebugUtilsObjectNameInfoEXT nameInfo;
  3539. nameInfo.sType = VK_STRUCTURE_TYPE_DEBUG_UTILS_OBJECT_NAME_INFO_EXT;
  3540. nameInfo.pNext = NULL;
  3541. nameInfo.pObjectName = debugName;
  3542. nameInfo.objectType = VK_OBJECT_TYPE_BUFFER;
  3543. nameInfo.objectHandle = (uint64_t)buffer->buffer;
  3544. renderer->vkSetDebugUtilsObjectNameEXT(
  3545. renderer->logicalDevice,
  3546. &nameInfo);
  3547. }
  3548. return buffer;
  3549. }
  3550. static VulkanBufferContainer *VULKAN_INTERNAL_CreateBufferContainer(
  3551. VulkanRenderer *renderer,
  3552. VkDeviceSize size,
  3553. SDL_GPUBufferUsageFlags usageFlags,
  3554. VulkanBufferType type,
  3555. bool dedicated,
  3556. const char *debugName)
  3557. {
  3558. VulkanBufferContainer *bufferContainer;
  3559. VulkanBuffer *buffer;
  3560. buffer = VULKAN_INTERNAL_CreateBuffer(
  3561. renderer,
  3562. size,
  3563. usageFlags,
  3564. type,
  3565. dedicated,
  3566. debugName);
  3567. if (buffer == NULL) {
  3568. return NULL;
  3569. }
  3570. bufferContainer = SDL_calloc(1, sizeof(VulkanBufferContainer));
  3571. bufferContainer->activeBuffer = buffer;
  3572. buffer->container = bufferContainer;
  3573. buffer->containerIndex = 0;
  3574. bufferContainer->bufferCapacity = 1;
  3575. bufferContainer->bufferCount = 1;
  3576. bufferContainer->buffers = SDL_calloc(bufferContainer->bufferCapacity, sizeof(VulkanBuffer *));
  3577. bufferContainer->buffers[0] = bufferContainer->activeBuffer;
  3578. bufferContainer->dedicated = dedicated;
  3579. bufferContainer->debugName = NULL;
  3580. if (debugName != NULL) {
  3581. bufferContainer->debugName = SDL_strdup(debugName);
  3582. }
  3583. return bufferContainer;
  3584. }
  3585. // Texture Subresource Utilities
  3586. static Uint32 VULKAN_INTERNAL_GetTextureSubresourceIndex(
  3587. Uint32 mipLevel,
  3588. Uint32 layer,
  3589. Uint32 numLevels)
  3590. {
  3591. return mipLevel + (layer * numLevels);
  3592. }
  3593. static VulkanTextureSubresource *VULKAN_INTERNAL_FetchTextureSubresource(
  3594. VulkanTextureContainer *textureContainer,
  3595. Uint32 layer,
  3596. Uint32 level)
  3597. {
  3598. Uint32 index = VULKAN_INTERNAL_GetTextureSubresourceIndex(
  3599. level,
  3600. layer,
  3601. textureContainer->header.info.num_levels);
  3602. return &textureContainer->activeTexture->subresources[index];
  3603. }
  3604. static bool VULKAN_INTERNAL_CreateRenderTargetView(
  3605. VulkanRenderer *renderer,
  3606. VulkanTexture *texture,
  3607. Uint32 layerOrDepth,
  3608. Uint32 level,
  3609. VkFormat format,
  3610. VkComponentMapping swizzle,
  3611. VkImageView *pView)
  3612. {
  3613. VkResult vulkanResult;
  3614. VkImageViewCreateInfo imageViewCreateInfo;
  3615. // create framebuffer compatible views for RenderTarget
  3616. imageViewCreateInfo.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO;
  3617. imageViewCreateInfo.pNext = NULL;
  3618. imageViewCreateInfo.flags = 0;
  3619. imageViewCreateInfo.image = texture->image;
  3620. imageViewCreateInfo.format = format;
  3621. imageViewCreateInfo.components = swizzle;
  3622. imageViewCreateInfo.subresourceRange.aspectMask = texture->aspectFlags;
  3623. imageViewCreateInfo.subresourceRange.baseMipLevel = level;
  3624. imageViewCreateInfo.subresourceRange.levelCount = 1;
  3625. imageViewCreateInfo.subresourceRange.baseArrayLayer = layerOrDepth;
  3626. imageViewCreateInfo.subresourceRange.layerCount = 1;
  3627. imageViewCreateInfo.viewType = VK_IMAGE_VIEW_TYPE_2D;
  3628. vulkanResult = renderer->vkCreateImageView(
  3629. renderer->logicalDevice,
  3630. &imageViewCreateInfo,
  3631. NULL,
  3632. pView);
  3633. if (vulkanResult != VK_SUCCESS) {
  3634. *pView = (VkImageView)VK_NULL_HANDLE;
  3635. CHECK_VULKAN_ERROR_AND_RETURN(vulkanResult, vkCreateImageView, false);
  3636. }
  3637. return true;
  3638. }
  3639. static bool VULKAN_INTERNAL_CreateSubresourceView(
  3640. VulkanRenderer *renderer,
  3641. const SDL_GPUTextureCreateInfo *createinfo,
  3642. VulkanTexture *texture,
  3643. Uint32 layer,
  3644. Uint32 level,
  3645. VkComponentMapping swizzle,
  3646. VkImageView *pView)
  3647. {
  3648. VkResult vulkanResult;
  3649. VkImageViewCreateInfo imageViewCreateInfo;
  3650. // create framebuffer compatible views for RenderTarget
  3651. imageViewCreateInfo.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO;
  3652. imageViewCreateInfo.pNext = NULL;
  3653. imageViewCreateInfo.flags = 0;
  3654. imageViewCreateInfo.image = texture->image;
  3655. imageViewCreateInfo.format = SDLToVK_TextureFormat[createinfo->format];
  3656. imageViewCreateInfo.components = swizzle;
  3657. imageViewCreateInfo.subresourceRange.aspectMask = texture->aspectFlags;
  3658. imageViewCreateInfo.subresourceRange.baseMipLevel = level;
  3659. imageViewCreateInfo.subresourceRange.levelCount = 1;
  3660. imageViewCreateInfo.subresourceRange.baseArrayLayer = layer;
  3661. imageViewCreateInfo.subresourceRange.layerCount = 1;
  3662. imageViewCreateInfo.viewType = (createinfo->type == SDL_GPU_TEXTURETYPE_3D) ? VK_IMAGE_VIEW_TYPE_3D : VK_IMAGE_VIEW_TYPE_2D;
  3663. vulkanResult = renderer->vkCreateImageView(
  3664. renderer->logicalDevice,
  3665. &imageViewCreateInfo,
  3666. NULL,
  3667. pView);
  3668. if (vulkanResult != VK_SUCCESS) {
  3669. *pView = (VkImageView)VK_NULL_HANDLE;
  3670. CHECK_VULKAN_ERROR_AND_RETURN(vulkanResult, vkCreateImageView, false);
  3671. }
  3672. return true;
  3673. }
  3674. // Swapchain
  3675. static bool VULKAN_INTERNAL_QuerySwapchainSupport(
  3676. VulkanRenderer *renderer,
  3677. VkPhysicalDevice physicalDevice,
  3678. VkSurfaceKHR surface,
  3679. SwapchainSupportDetails *outputDetails)
  3680. {
  3681. VkResult result;
  3682. VkBool32 supportsPresent;
  3683. renderer->vkGetPhysicalDeviceSurfaceSupportKHR(
  3684. physicalDevice,
  3685. renderer->queueFamilyIndex,
  3686. surface,
  3687. &supportsPresent);
  3688. // Initialize these in case anything fails
  3689. outputDetails->formatsLength = 0;
  3690. outputDetails->presentModesLength = 0;
  3691. if (!supportsPresent) {
  3692. SET_STRING_ERROR_AND_RETURN("This surface does not support presenting!", false);
  3693. }
  3694. // Run the device surface queries
  3695. result = renderer->vkGetPhysicalDeviceSurfaceCapabilitiesKHR(
  3696. physicalDevice,
  3697. surface,
  3698. &outputDetails->capabilities);
  3699. CHECK_VULKAN_ERROR_AND_RETURN(result, vkGetPhysicalDeviceSurfaceCapabilitiesKHR, false);
  3700. if (!(outputDetails->capabilities.supportedCompositeAlpha & VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR)) {
  3701. SDL_LogWarn(SDL_LOG_CATEGORY_GPU, "Opaque presentation unsupported! Expect weird transparency bugs!");
  3702. }
  3703. result = renderer->vkGetPhysicalDeviceSurfaceFormatsKHR(
  3704. physicalDevice,
  3705. surface,
  3706. &outputDetails->formatsLength,
  3707. NULL);
  3708. CHECK_VULKAN_ERROR_AND_RETURN(result, vkGetPhysicalDeviceSurfaceFormatsKHR, false);
  3709. result = renderer->vkGetPhysicalDeviceSurfacePresentModesKHR(
  3710. physicalDevice,
  3711. surface,
  3712. &outputDetails->presentModesLength,
  3713. NULL);
  3714. CHECK_VULKAN_ERROR_AND_RETURN(result, vkGetPhysicalDeviceSurfacePresentModesKHR, false);
  3715. // Generate the arrays, if applicable
  3716. outputDetails->formats = NULL;
  3717. if (outputDetails->formatsLength != 0) {
  3718. outputDetails->formats = (VkSurfaceFormatKHR *)SDL_malloc(
  3719. sizeof(VkSurfaceFormatKHR) * outputDetails->formatsLength);
  3720. if (!outputDetails->formats) { // OOM
  3721. return false;
  3722. }
  3723. result = renderer->vkGetPhysicalDeviceSurfaceFormatsKHR(
  3724. physicalDevice,
  3725. surface,
  3726. &outputDetails->formatsLength,
  3727. outputDetails->formats);
  3728. if (result != VK_SUCCESS) {
  3729. SDL_free(outputDetails->formats);
  3730. CHECK_VULKAN_ERROR_AND_RETURN(result, vkGetPhysicalDeviceSurfaceFormatsKHR, false);
  3731. }
  3732. }
  3733. outputDetails->presentModes = NULL;
  3734. if (outputDetails->presentModesLength != 0) {
  3735. outputDetails->presentModes = (VkPresentModeKHR *)SDL_malloc(
  3736. sizeof(VkPresentModeKHR) * outputDetails->presentModesLength);
  3737. if (!outputDetails->presentModes) { // OOM
  3738. SDL_free(outputDetails->formats);
  3739. return false;
  3740. }
  3741. result = renderer->vkGetPhysicalDeviceSurfacePresentModesKHR(
  3742. physicalDevice,
  3743. surface,
  3744. &outputDetails->presentModesLength,
  3745. outputDetails->presentModes);
  3746. if (result != VK_SUCCESS) {
  3747. SDL_free(outputDetails->formats);
  3748. SDL_free(outputDetails->presentModes);
  3749. CHECK_VULKAN_ERROR_AND_RETURN(result, vkGetPhysicalDeviceSurfacePresentModesKHR, false);
  3750. }
  3751. }
  3752. /* If we made it here, all the queries were successful. This does NOT
  3753. * necessarily mean there are any supported formats or present modes!
  3754. */
  3755. return true;
  3756. }
  3757. static bool VULKAN_INTERNAL_VerifySwapSurfaceFormat(
  3758. VkFormat desiredFormat,
  3759. VkColorSpaceKHR desiredColorSpace,
  3760. VkSurfaceFormatKHR *availableFormats,
  3761. Uint32 availableFormatsLength)
  3762. {
  3763. Uint32 i;
  3764. for (i = 0; i < availableFormatsLength; i += 1) {
  3765. if (availableFormats[i].format == desiredFormat &&
  3766. availableFormats[i].colorSpace == desiredColorSpace) {
  3767. return true;
  3768. }
  3769. }
  3770. return false;
  3771. }
  3772. static bool VULKAN_INTERNAL_VerifySwapPresentMode(
  3773. VkPresentModeKHR presentMode,
  3774. const VkPresentModeKHR *availablePresentModes,
  3775. Uint32 availablePresentModesLength)
  3776. {
  3777. Uint32 i;
  3778. for (i = 0; i < availablePresentModesLength; i += 1) {
  3779. if (availablePresentModes[i] == presentMode) {
  3780. return true;
  3781. }
  3782. }
  3783. return false;
  3784. }
  3785. /* It would be nice if VULKAN_INTERNAL_CreateSwapchain could return a bool.
  3786. * Unfortunately, some Win32 NVIDIA drivers are stupid
  3787. * and will return surface extents of (0, 0)
  3788. * in certain edge cases, and the swapchain extents are not allowed to be 0.
  3789. * In this case, the client probably still wants to claim the window
  3790. * or recreate the swapchain, so we should return 2 to indicate retry.
  3791. * -cosmonaut
  3792. */
  3793. #define VULKAN_INTERNAL_TRY_AGAIN 2
  3794. static Uint32 VULKAN_INTERNAL_CreateSwapchain(
  3795. VulkanRenderer *renderer,
  3796. WindowData *windowData)
  3797. {
  3798. VkResult vulkanResult;
  3799. VkSwapchainCreateInfoKHR swapchainCreateInfo;
  3800. VkImage *swapchainImages;
  3801. VkSemaphoreCreateInfo semaphoreCreateInfo;
  3802. SwapchainSupportDetails swapchainSupportDetails;
  3803. bool hasValidSwapchainComposition, hasValidPresentMode;
  3804. VkCompositeAlphaFlagsKHR compositeAlphaFlag = 0;
  3805. Uint32 i;
  3806. windowData->frameCounter = 0;
  3807. // We dont have to create surface again on recreate swapchain
  3808. if (windowData->surface == VK_NULL_HANDLE) {
  3809. SDL_VideoDevice *_this = SDL_GetVideoDevice();
  3810. SDL_assert(_this && _this->Vulkan_CreateSurface);
  3811. // Each swapchain must have its own surface.
  3812. if (!_this->Vulkan_CreateSurface(
  3813. _this,
  3814. windowData->window,
  3815. renderer->instance,
  3816. NULL, // FIXME: VAllocationCallbacks
  3817. &windowData->surface)) {
  3818. return false;
  3819. }
  3820. }
  3821. SDL_assert(windowData->surface);
  3822. if (!VULKAN_INTERNAL_QuerySwapchainSupport(
  3823. renderer,
  3824. renderer->physicalDevice,
  3825. windowData->surface,
  3826. &swapchainSupportDetails)) {
  3827. renderer->vkDestroySurfaceKHR(
  3828. renderer->instance,
  3829. windowData->surface,
  3830. NULL);
  3831. windowData->surface = VK_NULL_HANDLE;
  3832. if (swapchainSupportDetails.formatsLength > 0) {
  3833. SDL_free(swapchainSupportDetails.formats);
  3834. }
  3835. if (swapchainSupportDetails.presentModesLength > 0) {
  3836. SDL_free(swapchainSupportDetails.presentModes);
  3837. }
  3838. return false;
  3839. }
  3840. // Verify that we can use the requested composition and present mode
  3841. windowData->format = SwapchainCompositionToFormat[windowData->swapchainComposition];
  3842. windowData->colorSpace = SwapchainCompositionToColorSpace[windowData->swapchainComposition];
  3843. windowData->swapchainSwizzle = SwapchainCompositionSwizzle[windowData->swapchainComposition];
  3844. windowData->usingFallbackFormat = false;
  3845. hasValidSwapchainComposition = VULKAN_INTERNAL_VerifySwapSurfaceFormat(
  3846. windowData->format,
  3847. windowData->colorSpace,
  3848. swapchainSupportDetails.formats,
  3849. swapchainSupportDetails.formatsLength);
  3850. if (!hasValidSwapchainComposition) {
  3851. // Let's try again with the fallback format...
  3852. windowData->format = SwapchainCompositionToFallbackFormat[windowData->swapchainComposition];
  3853. windowData->usingFallbackFormat = true;
  3854. hasValidSwapchainComposition = VULKAN_INTERNAL_VerifySwapSurfaceFormat(
  3855. windowData->format,
  3856. windowData->colorSpace,
  3857. swapchainSupportDetails.formats,
  3858. swapchainSupportDetails.formatsLength);
  3859. }
  3860. hasValidPresentMode = VULKAN_INTERNAL_VerifySwapPresentMode(
  3861. SDLToVK_PresentMode[windowData->presentMode],
  3862. swapchainSupportDetails.presentModes,
  3863. swapchainSupportDetails.presentModesLength);
  3864. if (!hasValidSwapchainComposition || !hasValidPresentMode) {
  3865. renderer->vkDestroySurfaceKHR(
  3866. renderer->instance,
  3867. windowData->surface,
  3868. NULL);
  3869. windowData->surface = VK_NULL_HANDLE;
  3870. if (swapchainSupportDetails.formatsLength > 0) {
  3871. SDL_free(swapchainSupportDetails.formats);
  3872. }
  3873. if (swapchainSupportDetails.presentModesLength > 0) {
  3874. SDL_free(swapchainSupportDetails.presentModes);
  3875. }
  3876. if (!hasValidSwapchainComposition) {
  3877. SET_STRING_ERROR_AND_RETURN("Device does not support requested swapchain composition!", false);
  3878. }
  3879. if (!hasValidPresentMode) {
  3880. SET_STRING_ERROR_AND_RETURN("Device does not support requested present_mode!", false);
  3881. }
  3882. return false;
  3883. }
  3884. // NVIDIA + Win32 can return 0 extent when the window is minimized. Try again!
  3885. if (swapchainSupportDetails.capabilities.currentExtent.width == 0 ||
  3886. swapchainSupportDetails.capabilities.currentExtent.height == 0) {
  3887. renderer->vkDestroySurfaceKHR(
  3888. renderer->instance,
  3889. windowData->surface,
  3890. NULL);
  3891. windowData->surface = VK_NULL_HANDLE;
  3892. if (swapchainSupportDetails.formatsLength > 0) {
  3893. SDL_free(swapchainSupportDetails.formats);
  3894. }
  3895. if (swapchainSupportDetails.presentModesLength > 0) {
  3896. SDL_free(swapchainSupportDetails.presentModes);
  3897. }
  3898. return VULKAN_INTERNAL_TRY_AGAIN;
  3899. }
  3900. Uint32 requestedImageCount = renderer->allowedFramesInFlight;
  3901. #ifdef SDL_PLATFORM_APPLE
  3902. windowData->width = swapchainSupportDetails.capabilities.currentExtent.width;
  3903. windowData->height = swapchainSupportDetails.capabilities.currentExtent.height;
  3904. #else
  3905. windowData->width = SDL_clamp(
  3906. windowData->swapchainCreateWidth,
  3907. swapchainSupportDetails.capabilities.minImageExtent.width,
  3908. swapchainSupportDetails.capabilities.maxImageExtent.width);
  3909. windowData->height = SDL_clamp(windowData->swapchainCreateHeight,
  3910. swapchainSupportDetails.capabilities.minImageExtent.height,
  3911. swapchainSupportDetails.capabilities.maxImageExtent.height);
  3912. #endif
  3913. if (swapchainSupportDetails.capabilities.maxImageCount > 0 &&
  3914. requestedImageCount > swapchainSupportDetails.capabilities.maxImageCount) {
  3915. requestedImageCount = swapchainSupportDetails.capabilities.maxImageCount;
  3916. }
  3917. if (requestedImageCount < swapchainSupportDetails.capabilities.minImageCount) {
  3918. requestedImageCount = swapchainSupportDetails.capabilities.minImageCount;
  3919. }
  3920. if (windowData->presentMode == SDL_GPU_PRESENTMODE_MAILBOX) {
  3921. /* Required for proper triple-buffering.
  3922. * Note that this is below the above maxImageCount check!
  3923. * If the driver advertises MAILBOX but does not support 3 swap
  3924. * images, it's not real mailbox support, so let it fail hard.
  3925. * -flibit
  3926. */
  3927. requestedImageCount = SDL_max(requestedImageCount, 3);
  3928. }
  3929. // Default to opaque, if available, followed by inherit, and overwrite with a value that supports transparency, if necessary.
  3930. if (swapchainSupportDetails.capabilities.supportedCompositeAlpha & VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR) {
  3931. compositeAlphaFlag = VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR;
  3932. } else if (swapchainSupportDetails.capabilities.supportedCompositeAlpha & VK_COMPOSITE_ALPHA_INHERIT_BIT_KHR) {
  3933. compositeAlphaFlag = VK_COMPOSITE_ALPHA_INHERIT_BIT_KHR;
  3934. }
  3935. if ((windowData->window->flags & SDL_WINDOW_TRANSPARENT) || !compositeAlphaFlag) {
  3936. if (swapchainSupportDetails.capabilities.supportedCompositeAlpha & VK_COMPOSITE_ALPHA_PRE_MULTIPLIED_BIT_KHR) {
  3937. compositeAlphaFlag = VK_COMPOSITE_ALPHA_PRE_MULTIPLIED_BIT_KHR;
  3938. } else if (swapchainSupportDetails.capabilities.supportedCompositeAlpha & VK_COMPOSITE_ALPHA_POST_MULTIPLIED_BIT_KHR) {
  3939. compositeAlphaFlag = VK_COMPOSITE_ALPHA_POST_MULTIPLIED_BIT_KHR;
  3940. } else if (swapchainSupportDetails.capabilities.supportedCompositeAlpha & VK_COMPOSITE_ALPHA_INHERIT_BIT_KHR) {
  3941. compositeAlphaFlag = VK_COMPOSITE_ALPHA_INHERIT_BIT_KHR;
  3942. } else {
  3943. SDL_LogWarn(SDL_LOG_CATEGORY_GPU, "SDL_WINDOW_TRANSPARENT flag set, but no suitable swapchain composite alpha value supported!");
  3944. }
  3945. }
  3946. swapchainCreateInfo.sType = VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR;
  3947. swapchainCreateInfo.pNext = NULL;
  3948. swapchainCreateInfo.flags = 0;
  3949. swapchainCreateInfo.surface = windowData->surface;
  3950. swapchainCreateInfo.minImageCount = requestedImageCount;
  3951. swapchainCreateInfo.imageFormat = windowData->format;
  3952. swapchainCreateInfo.imageColorSpace = windowData->colorSpace;
  3953. swapchainCreateInfo.imageExtent.width = windowData->width;
  3954. swapchainCreateInfo.imageExtent.height = windowData->height;
  3955. swapchainCreateInfo.imageArrayLayers = 1;
  3956. swapchainCreateInfo.imageUsage =
  3957. VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT |
  3958. VK_IMAGE_USAGE_TRANSFER_DST_BIT;
  3959. swapchainCreateInfo.imageSharingMode = VK_SHARING_MODE_EXCLUSIVE;
  3960. swapchainCreateInfo.queueFamilyIndexCount = 0;
  3961. swapchainCreateInfo.pQueueFamilyIndices = NULL;
  3962. #ifdef SDL_PLATFORM_ANDROID
  3963. swapchainCreateInfo.preTransform = VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR;
  3964. #else
  3965. swapchainCreateInfo.preTransform = swapchainSupportDetails.capabilities.currentTransform;
  3966. #endif
  3967. swapchainCreateInfo.compositeAlpha = compositeAlphaFlag;
  3968. swapchainCreateInfo.presentMode = SDLToVK_PresentMode[windowData->presentMode];
  3969. swapchainCreateInfo.clipped = VK_TRUE;
  3970. swapchainCreateInfo.oldSwapchain = windowData->swapchain;
  3971. vulkanResult = renderer->vkCreateSwapchainKHR(
  3972. renderer->logicalDevice,
  3973. &swapchainCreateInfo,
  3974. NULL,
  3975. &windowData->swapchain);
  3976. if (swapchainCreateInfo.oldSwapchain != VK_NULL_HANDLE) {
  3977. renderer->vkDestroySwapchainKHR(renderer->logicalDevice, swapchainCreateInfo.oldSwapchain, NULL);
  3978. }
  3979. if (swapchainSupportDetails.formatsLength > 0) {
  3980. SDL_free(swapchainSupportDetails.formats);
  3981. }
  3982. if (swapchainSupportDetails.presentModesLength > 0) {
  3983. SDL_free(swapchainSupportDetails.presentModes);
  3984. }
  3985. if (vulkanResult != VK_SUCCESS) {
  3986. renderer->vkDestroySurfaceKHR(
  3987. renderer->instance,
  3988. windowData->surface,
  3989. NULL);
  3990. windowData->surface = VK_NULL_HANDLE;
  3991. windowData->swapchain = VK_NULL_HANDLE;
  3992. CHECK_VULKAN_ERROR_AND_RETURN(vulkanResult, vkCreateSwapchainKHR, false);
  3993. }
  3994. vulkanResult = renderer->vkGetSwapchainImagesKHR(
  3995. renderer->logicalDevice,
  3996. windowData->swapchain,
  3997. &windowData->imageCount,
  3998. NULL);
  3999. CHECK_VULKAN_ERROR_AND_RETURN(vulkanResult, vkGetSwapchainImagesKHR, false);
  4000. windowData->textureContainers = SDL_malloc(
  4001. sizeof(VulkanTextureContainer) * windowData->imageCount);
  4002. if (!windowData->textureContainers) { // OOM
  4003. renderer->vkDestroySurfaceKHR(
  4004. renderer->instance,
  4005. windowData->surface,
  4006. NULL);
  4007. renderer->vkDestroySwapchainKHR(
  4008. renderer->logicalDevice,
  4009. windowData->swapchain,
  4010. NULL);
  4011. windowData->surface = VK_NULL_HANDLE;
  4012. windowData->swapchain = VK_NULL_HANDLE;
  4013. return false;
  4014. }
  4015. swapchainImages = SDL_stack_alloc(VkImage, windowData->imageCount);
  4016. vulkanResult = renderer->vkGetSwapchainImagesKHR(
  4017. renderer->logicalDevice,
  4018. windowData->swapchain,
  4019. &windowData->imageCount,
  4020. swapchainImages);
  4021. CHECK_VULKAN_ERROR_AND_RETURN(vulkanResult, vkGetSwapchainImagesKHR, false);
  4022. for (i = 0; i < windowData->imageCount; i += 1) {
  4023. // Initialize dummy container
  4024. SDL_zero(windowData->textureContainers[i]);
  4025. windowData->textureContainers[i].canBeCycled = false;
  4026. windowData->textureContainers[i].header.info.width = windowData->width;
  4027. windowData->textureContainers[i].header.info.height = windowData->height;
  4028. windowData->textureContainers[i].header.info.layer_count_or_depth = 1;
  4029. windowData->textureContainers[i].header.info.format = SwapchainCompositionToSDLFormat(
  4030. windowData->swapchainComposition,
  4031. windowData->usingFallbackFormat);
  4032. windowData->textureContainers[i].header.info.type = SDL_GPU_TEXTURETYPE_2D;
  4033. windowData->textureContainers[i].header.info.num_levels = 1;
  4034. windowData->textureContainers[i].header.info.sample_count = SDL_GPU_SAMPLECOUNT_1;
  4035. windowData->textureContainers[i].header.info.usage = SDL_GPU_TEXTUREUSAGE_COLOR_TARGET;
  4036. windowData->textureContainers[i].activeTexture = SDL_malloc(sizeof(VulkanTexture));
  4037. windowData->textureContainers[i].activeTexture->image = swapchainImages[i];
  4038. // Swapchain memory is managed by the driver
  4039. windowData->textureContainers[i].activeTexture->usedRegion = NULL;
  4040. windowData->textureContainers[i].activeTexture->swizzle = windowData->swapchainSwizzle;
  4041. windowData->textureContainers[i].activeTexture->aspectFlags = VK_IMAGE_ASPECT_COLOR_BIT;
  4042. windowData->textureContainers[i].activeTexture->depth = 1;
  4043. windowData->textureContainers[i].activeTexture->usage = SDL_GPU_TEXTUREUSAGE_COLOR_TARGET;
  4044. windowData->textureContainers[i].activeTexture->container = &windowData->textureContainers[i];
  4045. SDL_SetAtomicInt(&windowData->textureContainers[i].activeTexture->referenceCount, 0);
  4046. // Create slice
  4047. windowData->textureContainers[i].activeTexture->subresourceCount = 1;
  4048. windowData->textureContainers[i].activeTexture->subresources = SDL_malloc(sizeof(VulkanTextureSubresource));
  4049. windowData->textureContainers[i].activeTexture->subresources[0].parent = windowData->textureContainers[i].activeTexture;
  4050. windowData->textureContainers[i].activeTexture->subresources[0].layer = 0;
  4051. windowData->textureContainers[i].activeTexture->subresources[0].level = 0;
  4052. windowData->textureContainers[i].activeTexture->subresources[0].renderTargetViews = SDL_malloc(sizeof(VkImageView));
  4053. if (!VULKAN_INTERNAL_CreateRenderTargetView(
  4054. renderer,
  4055. windowData->textureContainers[i].activeTexture,
  4056. 0,
  4057. 0,
  4058. windowData->format,
  4059. windowData->swapchainSwizzle,
  4060. &windowData->textureContainers[i].activeTexture->subresources[0].renderTargetViews[0])) {
  4061. renderer->vkDestroySurfaceKHR(
  4062. renderer->instance,
  4063. windowData->surface,
  4064. NULL);
  4065. renderer->vkDestroySwapchainKHR(
  4066. renderer->logicalDevice,
  4067. windowData->swapchain,
  4068. NULL);
  4069. windowData->surface = VK_NULL_HANDLE;
  4070. windowData->swapchain = VK_NULL_HANDLE;
  4071. return false;
  4072. }
  4073. }
  4074. SDL_stack_free(swapchainImages);
  4075. semaphoreCreateInfo.sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO;
  4076. semaphoreCreateInfo.pNext = NULL;
  4077. semaphoreCreateInfo.flags = 0;
  4078. for (i = 0; i < MAX_FRAMES_IN_FLIGHT; i += 1) {
  4079. vulkanResult = renderer->vkCreateSemaphore(
  4080. renderer->logicalDevice,
  4081. &semaphoreCreateInfo,
  4082. NULL,
  4083. &windowData->imageAvailableSemaphore[i]);
  4084. if (vulkanResult != VK_SUCCESS) {
  4085. renderer->vkDestroySurfaceKHR(
  4086. renderer->instance,
  4087. windowData->surface,
  4088. NULL);
  4089. renderer->vkDestroySwapchainKHR(
  4090. renderer->logicalDevice,
  4091. windowData->swapchain,
  4092. NULL);
  4093. windowData->surface = VK_NULL_HANDLE;
  4094. windowData->swapchain = VK_NULL_HANDLE;
  4095. CHECK_VULKAN_ERROR_AND_RETURN(vulkanResult, vkCreateSemaphore, false);
  4096. }
  4097. windowData->inFlightFences[i] = NULL;
  4098. }
  4099. windowData->renderFinishedSemaphore = SDL_malloc(
  4100. sizeof(VkSemaphore) * windowData->imageCount);
  4101. for (i = 0; i < windowData->imageCount; i += 1) {
  4102. vulkanResult = renderer->vkCreateSemaphore(
  4103. renderer->logicalDevice,
  4104. &semaphoreCreateInfo,
  4105. NULL,
  4106. &windowData->renderFinishedSemaphore[i]);
  4107. if (vulkanResult != VK_SUCCESS) {
  4108. renderer->vkDestroySurfaceKHR(
  4109. renderer->instance,
  4110. windowData->surface,
  4111. NULL);
  4112. renderer->vkDestroySwapchainKHR(
  4113. renderer->logicalDevice,
  4114. windowData->swapchain,
  4115. NULL);
  4116. windowData->surface = VK_NULL_HANDLE;
  4117. windowData->swapchain = VK_NULL_HANDLE;
  4118. CHECK_VULKAN_ERROR_AND_RETURN(vulkanResult, vkCreateSemaphore, false);
  4119. }
  4120. }
  4121. windowData->needsSwapchainRecreate = false;
  4122. return true;
  4123. }
  4124. // Command Buffers
  4125. static bool VULKAN_INTERNAL_BeginCommandBuffer(
  4126. VulkanRenderer *renderer,
  4127. VulkanCommandBuffer *commandBuffer)
  4128. {
  4129. VkCommandBufferBeginInfo beginInfo;
  4130. VkResult result;
  4131. beginInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO;
  4132. beginInfo.pNext = NULL;
  4133. beginInfo.flags = 0;
  4134. beginInfo.pInheritanceInfo = NULL;
  4135. beginInfo.flags = VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT;
  4136. result = renderer->vkBeginCommandBuffer(
  4137. commandBuffer->commandBuffer,
  4138. &beginInfo);
  4139. CHECK_VULKAN_ERROR_AND_RETURN(result, vkBeginCommandBuffer, false);
  4140. return true;
  4141. }
  4142. static bool VULKAN_INTERNAL_EndCommandBuffer(
  4143. VulkanRenderer *renderer,
  4144. VulkanCommandBuffer *commandBuffer)
  4145. {
  4146. VkResult result = renderer->vkEndCommandBuffer(
  4147. commandBuffer->commandBuffer);
  4148. CHECK_VULKAN_ERROR_AND_RETURN(result, vkEndCommandBuffer, false);
  4149. return true;
  4150. }
  4151. static void VULKAN_DestroyDevice(
  4152. SDL_GPUDevice *device)
  4153. {
  4154. VulkanRenderer *renderer = (VulkanRenderer *)device->driverData;
  4155. VulkanMemorySubAllocator *allocator;
  4156. VULKAN_Wait(device->driverData);
  4157. for (Sint32 i = renderer->claimedWindowCount - 1; i >= 0; i -= 1) {
  4158. VULKAN_ReleaseWindow(device->driverData, renderer->claimedWindows[i]->window);
  4159. }
  4160. SDL_free(renderer->claimedWindows);
  4161. VULKAN_Wait(device->driverData);
  4162. SDL_free(renderer->submittedCommandBuffers);
  4163. for (Uint32 i = 0; i < renderer->uniformBufferPoolCount; i += 1) {
  4164. VULKAN_INTERNAL_DestroyBuffer(
  4165. renderer,
  4166. renderer->uniformBufferPool[i]->buffer);
  4167. SDL_free(renderer->uniformBufferPool[i]);
  4168. }
  4169. SDL_free(renderer->uniformBufferPool);
  4170. for (Uint32 i = 0; i < renderer->descriptorSetCachePoolCount; i += 1) {
  4171. VULKAN_INTERNAL_DestroyDescriptorSetCache(
  4172. renderer,
  4173. renderer->descriptorSetCachePool[i]);
  4174. }
  4175. SDL_free(renderer->descriptorSetCachePool);
  4176. for (Uint32 i = 0; i < renderer->fencePool.availableFenceCount; i += 1) {
  4177. renderer->vkDestroyFence(
  4178. renderer->logicalDevice,
  4179. renderer->fencePool.availableFences[i]->fence,
  4180. NULL);
  4181. SDL_free(renderer->fencePool.availableFences[i]);
  4182. }
  4183. SDL_free(renderer->fencePool.availableFences);
  4184. SDL_DestroyMutex(renderer->fencePool.lock);
  4185. SDL_DestroyHashTable(renderer->commandPoolHashTable);
  4186. SDL_DestroyHashTable(renderer->renderPassHashTable);
  4187. SDL_DestroyHashTable(renderer->framebufferHashTable);
  4188. SDL_DestroyHashTable(renderer->graphicsPipelineResourceLayoutHashTable);
  4189. SDL_DestroyHashTable(renderer->computePipelineResourceLayoutHashTable);
  4190. SDL_DestroyHashTable(renderer->descriptorSetLayoutHashTable);
  4191. for (Uint32 i = 0; i < VK_MAX_MEMORY_TYPES; i += 1) {
  4192. allocator = &renderer->memoryAllocator->subAllocators[i];
  4193. for (Sint32 j = allocator->allocationCount - 1; j >= 0; j -= 1) {
  4194. for (Sint32 k = allocator->allocations[j]->usedRegionCount - 1; k >= 0; k -= 1) {
  4195. VULKAN_INTERNAL_RemoveMemoryUsedRegion(
  4196. renderer,
  4197. allocator->allocations[j]->usedRegions[k]);
  4198. }
  4199. VULKAN_INTERNAL_DeallocateMemory(
  4200. renderer,
  4201. allocator,
  4202. j);
  4203. }
  4204. SDL_free(renderer->memoryAllocator->subAllocators[i].allocations);
  4205. SDL_free(renderer->memoryAllocator->subAllocators[i].sortedFreeRegions);
  4206. }
  4207. SDL_free(renderer->memoryAllocator);
  4208. SDL_free(renderer->texturesToDestroy);
  4209. SDL_free(renderer->buffersToDestroy);
  4210. SDL_free(renderer->graphicsPipelinesToDestroy);
  4211. SDL_free(renderer->computePipelinesToDestroy);
  4212. SDL_free(renderer->shadersToDestroy);
  4213. SDL_free(renderer->samplersToDestroy);
  4214. SDL_free(renderer->framebuffersToDestroy);
  4215. SDL_free(renderer->allocationsToDefrag);
  4216. SDL_DestroyMutex(renderer->allocatorLock);
  4217. SDL_DestroyMutex(renderer->disposeLock);
  4218. SDL_DestroyMutex(renderer->submitLock);
  4219. SDL_DestroyMutex(renderer->acquireCommandBufferLock);
  4220. SDL_DestroyMutex(renderer->acquireUniformBufferLock);
  4221. SDL_DestroyMutex(renderer->renderPassFetchLock);
  4222. SDL_DestroyMutex(renderer->framebufferFetchLock);
  4223. SDL_DestroyMutex(renderer->graphicsPipelineLayoutFetchLock);
  4224. SDL_DestroyMutex(renderer->computePipelineLayoutFetchLock);
  4225. SDL_DestroyMutex(renderer->descriptorSetLayoutFetchLock);
  4226. SDL_DestroyMutex(renderer->windowLock);
  4227. renderer->vkDestroyDevice(renderer->logicalDevice, NULL);
  4228. renderer->vkDestroyInstance(renderer->instance, NULL);
  4229. SDL_DestroyProperties(renderer->props);
  4230. SDL_free(renderer);
  4231. SDL_free(device);
  4232. SDL_Vulkan_UnloadLibrary();
  4233. }
  4234. static SDL_PropertiesID VULKAN_GetDeviceProperties(
  4235. SDL_GPUDevice *device)
  4236. {
  4237. VulkanRenderer *renderer = (VulkanRenderer *)device->driverData;
  4238. return renderer->props;
  4239. }
  4240. static DescriptorSetCache *VULKAN_INTERNAL_AcquireDescriptorSetCache(
  4241. VulkanRenderer *renderer)
  4242. {
  4243. DescriptorSetCache *cache;
  4244. if (renderer->descriptorSetCachePoolCount == 0) {
  4245. cache = SDL_malloc(sizeof(DescriptorSetCache));
  4246. cache->poolCount = 0;
  4247. cache->pools = NULL;
  4248. } else {
  4249. cache = renderer->descriptorSetCachePool[renderer->descriptorSetCachePoolCount - 1];
  4250. renderer->descriptorSetCachePoolCount -= 1;
  4251. }
  4252. return cache;
  4253. }
  4254. static void VULKAN_INTERNAL_ReturnDescriptorSetCacheToPool(
  4255. VulkanRenderer *renderer,
  4256. DescriptorSetCache *descriptorSetCache)
  4257. {
  4258. EXPAND_ARRAY_IF_NEEDED(
  4259. renderer->descriptorSetCachePool,
  4260. DescriptorSetCache *,
  4261. renderer->descriptorSetCachePoolCount + 1,
  4262. renderer->descriptorSetCachePoolCapacity,
  4263. renderer->descriptorSetCachePoolCapacity * 2);
  4264. renderer->descriptorSetCachePool[renderer->descriptorSetCachePoolCount] = descriptorSetCache;
  4265. renderer->descriptorSetCachePoolCount += 1;
  4266. for (Uint32 i = 0; i < descriptorSetCache->poolCount; i += 1) {
  4267. descriptorSetCache->pools[i].descriptorSetIndex = 0;
  4268. }
  4269. }
  4270. static VkDescriptorSet VULKAN_INTERNAL_FetchDescriptorSet(
  4271. VulkanRenderer *renderer,
  4272. VulkanCommandBuffer *vulkanCommandBuffer,
  4273. DescriptorSetLayout *descriptorSetLayout)
  4274. {
  4275. // Grow the pool to meet the descriptor set layout ID
  4276. if (descriptorSetLayout->ID >= vulkanCommandBuffer->descriptorSetCache->poolCount) {
  4277. vulkanCommandBuffer->descriptorSetCache->pools = SDL_realloc(
  4278. vulkanCommandBuffer->descriptorSetCache->pools,
  4279. sizeof(DescriptorSetPool) * (descriptorSetLayout->ID + 1));
  4280. for (Uint32 i = vulkanCommandBuffer->descriptorSetCache->poolCount; i < descriptorSetLayout->ID + 1; i += 1) {
  4281. SDL_zero(vulkanCommandBuffer->descriptorSetCache->pools[i]);
  4282. }
  4283. vulkanCommandBuffer->descriptorSetCache->poolCount = descriptorSetLayout->ID + 1;
  4284. }
  4285. DescriptorSetPool *pool =
  4286. &vulkanCommandBuffer->descriptorSetCache->pools[descriptorSetLayout->ID];
  4287. if (pool->descriptorSetIndex == pool->descriptorSetCount) {
  4288. if (!VULKAN_INTERNAL_AllocateDescriptorsFromPool(
  4289. renderer,
  4290. descriptorSetLayout,
  4291. pool)) {
  4292. return VK_NULL_HANDLE;
  4293. }
  4294. }
  4295. VkDescriptorSet descriptorSet = pool->descriptorSets[pool->descriptorSetIndex];
  4296. pool->descriptorSetIndex += 1;
  4297. return descriptorSet;
  4298. }
  4299. static void VULKAN_INTERNAL_BindGraphicsDescriptorSets(
  4300. VulkanRenderer *renderer,
  4301. VulkanCommandBuffer *commandBuffer)
  4302. {
  4303. VulkanGraphicsPipelineResourceLayout *resourceLayout;
  4304. DescriptorSetLayout *descriptorSetLayout;
  4305. VkWriteDescriptorSet writeDescriptorSets[
  4306. (MAX_TEXTURE_SAMPLERS_PER_STAGE +
  4307. MAX_STORAGE_TEXTURES_PER_STAGE +
  4308. MAX_STORAGE_BUFFERS_PER_STAGE +
  4309. MAX_UNIFORM_BUFFERS_PER_STAGE) * 2];
  4310. VkDescriptorBufferInfo bufferInfos[MAX_STORAGE_BUFFERS_PER_STAGE * 2];
  4311. VkDescriptorImageInfo imageInfos[(MAX_TEXTURE_SAMPLERS_PER_STAGE + MAX_STORAGE_TEXTURES_PER_STAGE) * 2];
  4312. Uint32 dynamicOffsets[MAX_UNIFORM_BUFFERS_PER_STAGE * 2];
  4313. Uint32 writeCount = 0;
  4314. Uint32 bufferInfoCount = 0;
  4315. Uint32 imageInfoCount = 0;
  4316. Uint32 dynamicOffsetCount = 0;
  4317. if (
  4318. !commandBuffer->needVertexBufferBind &&
  4319. !commandBuffer->needNewVertexResourceDescriptorSet &&
  4320. !commandBuffer->needNewVertexUniformDescriptorSet &&
  4321. !commandBuffer->needNewVertexUniformOffsets &&
  4322. !commandBuffer->needNewFragmentResourceDescriptorSet &&
  4323. !commandBuffer->needNewFragmentUniformDescriptorSet &&
  4324. !commandBuffer->needNewFragmentUniformOffsets
  4325. ) {
  4326. return;
  4327. }
  4328. if (commandBuffer->needVertexBufferBind && commandBuffer->vertexBufferCount > 0) {
  4329. renderer->vkCmdBindVertexBuffers(
  4330. commandBuffer->commandBuffer,
  4331. 0,
  4332. commandBuffer->vertexBufferCount,
  4333. commandBuffer->vertexBuffers,
  4334. commandBuffer->vertexBufferOffsets);
  4335. commandBuffer->needVertexBufferBind = false;
  4336. }
  4337. resourceLayout = commandBuffer->currentGraphicsPipeline->resourceLayout;
  4338. if (commandBuffer->needNewVertexResourceDescriptorSet) {
  4339. descriptorSetLayout = resourceLayout->descriptorSetLayouts[0];
  4340. commandBuffer->vertexResourceDescriptorSet = VULKAN_INTERNAL_FetchDescriptorSet(
  4341. renderer,
  4342. commandBuffer,
  4343. descriptorSetLayout);
  4344. for (Uint32 i = 0; i < resourceLayout->vertexSamplerCount; i += 1) {
  4345. VkWriteDescriptorSet *currentWriteDescriptorSet = &writeDescriptorSets[writeCount];
  4346. currentWriteDescriptorSet->sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
  4347. currentWriteDescriptorSet->pNext = NULL;
  4348. currentWriteDescriptorSet->descriptorCount = 1;
  4349. currentWriteDescriptorSet->descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
  4350. currentWriteDescriptorSet->dstArrayElement = 0;
  4351. currentWriteDescriptorSet->dstBinding = i;
  4352. currentWriteDescriptorSet->dstSet = commandBuffer->vertexResourceDescriptorSet;
  4353. currentWriteDescriptorSet->pTexelBufferView = NULL;
  4354. currentWriteDescriptorSet->pBufferInfo = NULL;
  4355. imageInfos[imageInfoCount].sampler = commandBuffer->vertexSamplerBindings[i];
  4356. imageInfos[imageInfoCount].imageView = commandBuffer->vertexSamplerTextureViewBindings[i];
  4357. imageInfos[imageInfoCount].imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
  4358. currentWriteDescriptorSet->pImageInfo = &imageInfos[imageInfoCount];
  4359. writeCount += 1;
  4360. imageInfoCount += 1;
  4361. }
  4362. for (Uint32 i = 0; i < resourceLayout->vertexStorageTextureCount; i += 1) {
  4363. VkWriteDescriptorSet *currentWriteDescriptorSet = &writeDescriptorSets[writeCount];
  4364. currentWriteDescriptorSet->sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
  4365. currentWriteDescriptorSet->pNext = NULL;
  4366. currentWriteDescriptorSet->descriptorCount = 1;
  4367. currentWriteDescriptorSet->descriptorType = VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE; // Yes, we are declaring a storage image as a sampled image, because shaders are stupid.
  4368. currentWriteDescriptorSet->dstArrayElement = 0;
  4369. currentWriteDescriptorSet->dstBinding = resourceLayout->vertexSamplerCount + i;
  4370. currentWriteDescriptorSet->dstSet = commandBuffer->vertexResourceDescriptorSet;
  4371. currentWriteDescriptorSet->pTexelBufferView = NULL;
  4372. currentWriteDescriptorSet->pBufferInfo = NULL;
  4373. imageInfos[imageInfoCount].sampler = VK_NULL_HANDLE;
  4374. imageInfos[imageInfoCount].imageView = commandBuffer->vertexStorageTextureViewBindings[i];
  4375. imageInfos[imageInfoCount].imageLayout = VK_IMAGE_LAYOUT_GENERAL;
  4376. currentWriteDescriptorSet->pImageInfo = &imageInfos[imageInfoCount];
  4377. writeCount += 1;
  4378. imageInfoCount += 1;
  4379. }
  4380. for (Uint32 i = 0; i < resourceLayout->vertexStorageBufferCount; i += 1) {
  4381. VkWriteDescriptorSet *currentWriteDescriptorSet = &writeDescriptorSets[writeCount];
  4382. currentWriteDescriptorSet->sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
  4383. currentWriteDescriptorSet->pNext = NULL;
  4384. currentWriteDescriptorSet->descriptorCount = 1;
  4385. currentWriteDescriptorSet->descriptorType = VK_DESCRIPTOR_TYPE_STORAGE_BUFFER;
  4386. currentWriteDescriptorSet->dstArrayElement = 0;
  4387. currentWriteDescriptorSet->dstBinding = resourceLayout->vertexSamplerCount + resourceLayout->vertexStorageTextureCount + i;
  4388. currentWriteDescriptorSet->dstSet = commandBuffer->vertexResourceDescriptorSet;
  4389. currentWriteDescriptorSet->pTexelBufferView = NULL;
  4390. currentWriteDescriptorSet->pImageInfo = NULL;
  4391. bufferInfos[bufferInfoCount].buffer = commandBuffer->vertexStorageBufferBindings[i];
  4392. bufferInfos[bufferInfoCount].offset = 0;
  4393. bufferInfos[bufferInfoCount].range = VK_WHOLE_SIZE;
  4394. currentWriteDescriptorSet->pBufferInfo = &bufferInfos[bufferInfoCount];
  4395. writeCount += 1;
  4396. bufferInfoCount += 1;
  4397. }
  4398. commandBuffer->needNewVertexResourceDescriptorSet = false;
  4399. }
  4400. if (commandBuffer->needNewVertexUniformDescriptorSet) {
  4401. descriptorSetLayout = resourceLayout->descriptorSetLayouts[1];
  4402. commandBuffer->vertexUniformDescriptorSet = VULKAN_INTERNAL_FetchDescriptorSet(
  4403. renderer,
  4404. commandBuffer,
  4405. descriptorSetLayout);
  4406. for (Uint32 i = 0; i < resourceLayout->vertexUniformBufferCount; i += 1) {
  4407. VkWriteDescriptorSet *currentWriteDescriptorSet = &writeDescriptorSets[writeCount];
  4408. currentWriteDescriptorSet->sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
  4409. currentWriteDescriptorSet->pNext = NULL;
  4410. currentWriteDescriptorSet->descriptorCount = 1;
  4411. currentWriteDescriptorSet->descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC;
  4412. currentWriteDescriptorSet->dstArrayElement = 0;
  4413. currentWriteDescriptorSet->dstBinding = i;
  4414. currentWriteDescriptorSet->dstSet = commandBuffer->vertexUniformDescriptorSet;
  4415. currentWriteDescriptorSet->pTexelBufferView = NULL;
  4416. currentWriteDescriptorSet->pImageInfo = NULL;
  4417. bufferInfos[bufferInfoCount].buffer = commandBuffer->vertexUniformBuffers[i]->buffer->buffer;
  4418. bufferInfos[bufferInfoCount].offset = 0;
  4419. bufferInfos[bufferInfoCount].range = MAX_UBO_SECTION_SIZE;
  4420. currentWriteDescriptorSet->pBufferInfo = &bufferInfos[bufferInfoCount];
  4421. writeCount += 1;
  4422. bufferInfoCount += 1;
  4423. }
  4424. commandBuffer->needNewVertexUniformDescriptorSet = false;
  4425. }
  4426. for (Uint32 i = 0; i < resourceLayout->vertexUniformBufferCount; i += 1) {
  4427. dynamicOffsets[dynamicOffsetCount] = commandBuffer->vertexUniformBuffers[i]->drawOffset;
  4428. dynamicOffsetCount += 1;
  4429. }
  4430. if (commandBuffer->needNewFragmentResourceDescriptorSet) {
  4431. descriptorSetLayout = resourceLayout->descriptorSetLayouts[2];
  4432. commandBuffer->fragmentResourceDescriptorSet = VULKAN_INTERNAL_FetchDescriptorSet(
  4433. renderer,
  4434. commandBuffer,
  4435. descriptorSetLayout);
  4436. for (Uint32 i = 0; i < resourceLayout->fragmentSamplerCount; i += 1) {
  4437. VkWriteDescriptorSet *currentWriteDescriptorSet = &writeDescriptorSets[writeCount];
  4438. currentWriteDescriptorSet->sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
  4439. currentWriteDescriptorSet->pNext = NULL;
  4440. currentWriteDescriptorSet->descriptorCount = 1;
  4441. currentWriteDescriptorSet->descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
  4442. currentWriteDescriptorSet->dstArrayElement = 0;
  4443. currentWriteDescriptorSet->dstBinding = i;
  4444. currentWriteDescriptorSet->dstSet = commandBuffer->fragmentResourceDescriptorSet;
  4445. currentWriteDescriptorSet->pTexelBufferView = NULL;
  4446. currentWriteDescriptorSet->pBufferInfo = NULL;
  4447. imageInfos[imageInfoCount].sampler = commandBuffer->fragmentSamplerBindings[i];
  4448. imageInfos[imageInfoCount].imageView = commandBuffer->fragmentSamplerTextureViewBindings[i];
  4449. imageInfos[imageInfoCount].imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
  4450. currentWriteDescriptorSet->pImageInfo = &imageInfos[imageInfoCount];
  4451. writeCount += 1;
  4452. imageInfoCount += 1;
  4453. }
  4454. for (Uint32 i = 0; i < resourceLayout->fragmentStorageTextureCount; i += 1) {
  4455. VkWriteDescriptorSet *currentWriteDescriptorSet = &writeDescriptorSets[writeCount];
  4456. currentWriteDescriptorSet->sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
  4457. currentWriteDescriptorSet->pNext = NULL;
  4458. currentWriteDescriptorSet->descriptorCount = 1;
  4459. currentWriteDescriptorSet->descriptorType = VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE; // Yes, we are declaring a storage image as a sampled image, because shaders are stupid.
  4460. currentWriteDescriptorSet->dstArrayElement = 0;
  4461. currentWriteDescriptorSet->dstBinding = resourceLayout->fragmentSamplerCount + i;
  4462. currentWriteDescriptorSet->dstSet = commandBuffer->fragmentResourceDescriptorSet;
  4463. currentWriteDescriptorSet->pTexelBufferView = NULL;
  4464. currentWriteDescriptorSet->pBufferInfo = NULL;
  4465. imageInfos[imageInfoCount].sampler = VK_NULL_HANDLE;
  4466. imageInfos[imageInfoCount].imageView = commandBuffer->fragmentStorageTextureViewBindings[i];
  4467. imageInfos[imageInfoCount].imageLayout = VK_IMAGE_LAYOUT_GENERAL;
  4468. currentWriteDescriptorSet->pImageInfo = &imageInfos[imageInfoCount];
  4469. writeCount += 1;
  4470. imageInfoCount += 1;
  4471. }
  4472. for (Uint32 i = 0; i < resourceLayout->fragmentStorageBufferCount; i += 1) {
  4473. VkWriteDescriptorSet *currentWriteDescriptorSet = &writeDescriptorSets[writeCount];
  4474. currentWriteDescriptorSet->sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
  4475. currentWriteDescriptorSet->pNext = NULL;
  4476. currentWriteDescriptorSet->descriptorCount = 1;
  4477. currentWriteDescriptorSet->descriptorType = VK_DESCRIPTOR_TYPE_STORAGE_BUFFER;
  4478. currentWriteDescriptorSet->dstArrayElement = 0;
  4479. currentWriteDescriptorSet->dstBinding = resourceLayout->fragmentSamplerCount + resourceLayout->fragmentStorageTextureCount + i;
  4480. currentWriteDescriptorSet->dstSet = commandBuffer->fragmentResourceDescriptorSet;
  4481. currentWriteDescriptorSet->pTexelBufferView = NULL;
  4482. currentWriteDescriptorSet->pImageInfo = NULL;
  4483. bufferInfos[bufferInfoCount].buffer = commandBuffer->fragmentStorageBufferBindings[i];
  4484. bufferInfos[bufferInfoCount].offset = 0;
  4485. bufferInfos[bufferInfoCount].range = VK_WHOLE_SIZE;
  4486. currentWriteDescriptorSet->pBufferInfo = &bufferInfos[bufferInfoCount];
  4487. writeCount += 1;
  4488. bufferInfoCount += 1;
  4489. }
  4490. commandBuffer->needNewFragmentResourceDescriptorSet = false;
  4491. }
  4492. if (commandBuffer->needNewFragmentUniformDescriptorSet) {
  4493. descriptorSetLayout = resourceLayout->descriptorSetLayouts[3];
  4494. commandBuffer->fragmentUniformDescriptorSet = VULKAN_INTERNAL_FetchDescriptorSet(
  4495. renderer,
  4496. commandBuffer,
  4497. descriptorSetLayout);
  4498. for (Uint32 i = 0; i < resourceLayout->fragmentUniformBufferCount; i += 1) {
  4499. VkWriteDescriptorSet *currentWriteDescriptorSet = &writeDescriptorSets[writeCount];
  4500. currentWriteDescriptorSet->sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
  4501. currentWriteDescriptorSet->pNext = NULL;
  4502. currentWriteDescriptorSet->descriptorCount = 1;
  4503. currentWriteDescriptorSet->descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC;
  4504. currentWriteDescriptorSet->dstArrayElement = 0;
  4505. currentWriteDescriptorSet->dstBinding = i;
  4506. currentWriteDescriptorSet->dstSet = commandBuffer->fragmentUniformDescriptorSet;
  4507. currentWriteDescriptorSet->pTexelBufferView = NULL;
  4508. currentWriteDescriptorSet->pImageInfo = NULL;
  4509. bufferInfos[bufferInfoCount].buffer = commandBuffer->fragmentUniformBuffers[i]->buffer->buffer;
  4510. bufferInfos[bufferInfoCount].offset = 0;
  4511. bufferInfos[bufferInfoCount].range = MAX_UBO_SECTION_SIZE;
  4512. currentWriteDescriptorSet->pBufferInfo = &bufferInfos[bufferInfoCount];
  4513. writeCount += 1;
  4514. bufferInfoCount += 1;
  4515. }
  4516. commandBuffer->needNewFragmentUniformDescriptorSet = false;
  4517. }
  4518. for (Uint32 i = 0; i < resourceLayout->fragmentUniformBufferCount; i += 1) {
  4519. dynamicOffsets[dynamicOffsetCount] = commandBuffer->fragmentUniformBuffers[i]->drawOffset;
  4520. dynamicOffsetCount += 1;
  4521. }
  4522. renderer->vkUpdateDescriptorSets(
  4523. renderer->logicalDevice,
  4524. writeCount,
  4525. writeDescriptorSets,
  4526. 0,
  4527. NULL);
  4528. VkDescriptorSet sets[4];
  4529. sets[0] = commandBuffer->vertexResourceDescriptorSet;
  4530. sets[1] = commandBuffer->vertexUniformDescriptorSet;
  4531. sets[2] = commandBuffer->fragmentResourceDescriptorSet;
  4532. sets[3] = commandBuffer->fragmentUniformDescriptorSet;
  4533. renderer->vkCmdBindDescriptorSets(
  4534. commandBuffer->commandBuffer,
  4535. VK_PIPELINE_BIND_POINT_GRAPHICS,
  4536. resourceLayout->pipelineLayout,
  4537. 0,
  4538. 4,
  4539. sets,
  4540. dynamicOffsetCount,
  4541. dynamicOffsets);
  4542. commandBuffer->needNewVertexUniformOffsets = false;
  4543. commandBuffer->needNewFragmentUniformOffsets = false;
  4544. }
  4545. static void VULKAN_DrawIndexedPrimitives(
  4546. SDL_GPUCommandBuffer *commandBuffer,
  4547. Uint32 numIndices,
  4548. Uint32 numInstances,
  4549. Uint32 firstIndex,
  4550. Sint32 vertexOffset,
  4551. Uint32 firstInstance)
  4552. {
  4553. VulkanCommandBuffer *vulkanCommandBuffer = (VulkanCommandBuffer *)commandBuffer;
  4554. VulkanRenderer *renderer = vulkanCommandBuffer->renderer;
  4555. VULKAN_INTERNAL_BindGraphicsDescriptorSets(renderer, vulkanCommandBuffer);
  4556. renderer->vkCmdDrawIndexed(
  4557. vulkanCommandBuffer->commandBuffer,
  4558. numIndices,
  4559. numInstances,
  4560. firstIndex,
  4561. vertexOffset,
  4562. firstInstance);
  4563. }
  4564. static void VULKAN_DrawPrimitives(
  4565. SDL_GPUCommandBuffer *commandBuffer,
  4566. Uint32 numVertices,
  4567. Uint32 numInstances,
  4568. Uint32 firstVertex,
  4569. Uint32 firstInstance)
  4570. {
  4571. VulkanCommandBuffer *vulkanCommandBuffer = (VulkanCommandBuffer *)commandBuffer;
  4572. VulkanRenderer *renderer = vulkanCommandBuffer->renderer;
  4573. VULKAN_INTERNAL_BindGraphicsDescriptorSets(renderer, vulkanCommandBuffer);
  4574. renderer->vkCmdDraw(
  4575. vulkanCommandBuffer->commandBuffer,
  4576. numVertices,
  4577. numInstances,
  4578. firstVertex,
  4579. firstInstance);
  4580. }
  4581. static void VULKAN_DrawPrimitivesIndirect(
  4582. SDL_GPUCommandBuffer *commandBuffer,
  4583. SDL_GPUBuffer *buffer,
  4584. Uint32 offset,
  4585. Uint32 drawCount)
  4586. {
  4587. VulkanCommandBuffer *vulkanCommandBuffer = (VulkanCommandBuffer *)commandBuffer;
  4588. VulkanRenderer *renderer = vulkanCommandBuffer->renderer;
  4589. VulkanBuffer *vulkanBuffer = ((VulkanBufferContainer *)buffer)->activeBuffer;
  4590. Uint32 pitch = sizeof(SDL_GPUIndirectDrawCommand);
  4591. Uint32 i;
  4592. VULKAN_INTERNAL_BindGraphicsDescriptorSets(renderer, vulkanCommandBuffer);
  4593. if (renderer->supportsMultiDrawIndirect) {
  4594. // Real multi-draw!
  4595. renderer->vkCmdDrawIndirect(
  4596. vulkanCommandBuffer->commandBuffer,
  4597. vulkanBuffer->buffer,
  4598. offset,
  4599. drawCount,
  4600. pitch);
  4601. } else {
  4602. // Fake multi-draw...
  4603. for (i = 0; i < drawCount; i += 1) {
  4604. renderer->vkCmdDrawIndirect(
  4605. vulkanCommandBuffer->commandBuffer,
  4606. vulkanBuffer->buffer,
  4607. offset + (pitch * i),
  4608. 1,
  4609. pitch);
  4610. }
  4611. }
  4612. VULKAN_INTERNAL_TrackBuffer(vulkanCommandBuffer, vulkanBuffer);
  4613. }
  4614. static void VULKAN_DrawIndexedPrimitivesIndirect(
  4615. SDL_GPUCommandBuffer *commandBuffer,
  4616. SDL_GPUBuffer *buffer,
  4617. Uint32 offset,
  4618. Uint32 drawCount)
  4619. {
  4620. VulkanCommandBuffer *vulkanCommandBuffer = (VulkanCommandBuffer *)commandBuffer;
  4621. VulkanRenderer *renderer = vulkanCommandBuffer->renderer;
  4622. VulkanBuffer *vulkanBuffer = ((VulkanBufferContainer *)buffer)->activeBuffer;
  4623. Uint32 pitch = sizeof(SDL_GPUIndexedIndirectDrawCommand);
  4624. Uint32 i;
  4625. VULKAN_INTERNAL_BindGraphicsDescriptorSets(renderer, vulkanCommandBuffer);
  4626. if (renderer->supportsMultiDrawIndirect) {
  4627. // Real multi-draw!
  4628. renderer->vkCmdDrawIndexedIndirect(
  4629. vulkanCommandBuffer->commandBuffer,
  4630. vulkanBuffer->buffer,
  4631. offset,
  4632. drawCount,
  4633. pitch);
  4634. } else {
  4635. // Fake multi-draw...
  4636. for (i = 0; i < drawCount; i += 1) {
  4637. renderer->vkCmdDrawIndexedIndirect(
  4638. vulkanCommandBuffer->commandBuffer,
  4639. vulkanBuffer->buffer,
  4640. offset + (pitch * i),
  4641. 1,
  4642. pitch);
  4643. }
  4644. }
  4645. VULKAN_INTERNAL_TrackBuffer(vulkanCommandBuffer, vulkanBuffer);
  4646. }
  4647. // Debug Naming
  4648. static void VULKAN_INTERNAL_SetBufferName(
  4649. VulkanRenderer *renderer,
  4650. VulkanBuffer *buffer,
  4651. const char *text)
  4652. {
  4653. VkDebugUtilsObjectNameInfoEXT nameInfo;
  4654. if (renderer->debugMode && renderer->supportsDebugUtils) {
  4655. nameInfo.sType = VK_STRUCTURE_TYPE_DEBUG_UTILS_OBJECT_NAME_INFO_EXT;
  4656. nameInfo.pNext = NULL;
  4657. nameInfo.pObjectName = text;
  4658. nameInfo.objectType = VK_OBJECT_TYPE_BUFFER;
  4659. nameInfo.objectHandle = (uint64_t)buffer->buffer;
  4660. renderer->vkSetDebugUtilsObjectNameEXT(
  4661. renderer->logicalDevice,
  4662. &nameInfo);
  4663. }
  4664. }
  4665. static void VULKAN_SetBufferName(
  4666. SDL_GPURenderer *driverData,
  4667. SDL_GPUBuffer *buffer,
  4668. const char *text)
  4669. {
  4670. VulkanRenderer *renderer = (VulkanRenderer *)driverData;
  4671. VulkanBufferContainer *container = (VulkanBufferContainer *)buffer;
  4672. size_t textLength = SDL_strlen(text) + 1;
  4673. if (renderer->debugMode && renderer->supportsDebugUtils) {
  4674. container->debugName = SDL_realloc(
  4675. container->debugName,
  4676. textLength);
  4677. SDL_utf8strlcpy(
  4678. container->debugName,
  4679. text,
  4680. textLength);
  4681. for (Uint32 i = 0; i < container->bufferCount; i += 1) {
  4682. VULKAN_INTERNAL_SetBufferName(
  4683. renderer,
  4684. container->buffers[i],
  4685. text);
  4686. }
  4687. }
  4688. }
  4689. static void VULKAN_INTERNAL_SetTextureName(
  4690. VulkanRenderer *renderer,
  4691. VulkanTexture *texture,
  4692. const char *text)
  4693. {
  4694. VkDebugUtilsObjectNameInfoEXT nameInfo;
  4695. if (renderer->debugMode && renderer->supportsDebugUtils) {
  4696. nameInfo.sType = VK_STRUCTURE_TYPE_DEBUG_UTILS_OBJECT_NAME_INFO_EXT;
  4697. nameInfo.pNext = NULL;
  4698. nameInfo.pObjectName = text;
  4699. nameInfo.objectType = VK_OBJECT_TYPE_IMAGE;
  4700. nameInfo.objectHandle = (uint64_t)texture->image;
  4701. renderer->vkSetDebugUtilsObjectNameEXT(
  4702. renderer->logicalDevice,
  4703. &nameInfo);
  4704. }
  4705. }
  4706. static void VULKAN_SetTextureName(
  4707. SDL_GPURenderer *driverData,
  4708. SDL_GPUTexture *texture,
  4709. const char *text)
  4710. {
  4711. VulkanRenderer *renderer = (VulkanRenderer *)driverData;
  4712. VulkanTextureContainer *container = (VulkanTextureContainer *)texture;
  4713. size_t textLength = SDL_strlen(text) + 1;
  4714. if (renderer->debugMode && renderer->supportsDebugUtils) {
  4715. container->debugName = SDL_realloc(
  4716. container->debugName,
  4717. textLength);
  4718. SDL_utf8strlcpy(
  4719. container->debugName,
  4720. text,
  4721. textLength);
  4722. for (Uint32 i = 0; i < container->textureCount; i += 1) {
  4723. VULKAN_INTERNAL_SetTextureName(
  4724. renderer,
  4725. container->textures[i],
  4726. text);
  4727. }
  4728. }
  4729. }
  4730. static void VULKAN_InsertDebugLabel(
  4731. SDL_GPUCommandBuffer *commandBuffer,
  4732. const char *text)
  4733. {
  4734. VulkanCommandBuffer *vulkanCommandBuffer = (VulkanCommandBuffer *)commandBuffer;
  4735. VulkanRenderer *renderer = vulkanCommandBuffer->renderer;
  4736. VkDebugUtilsLabelEXT labelInfo;
  4737. if (renderer->supportsDebugUtils) {
  4738. SDL_zero(labelInfo);
  4739. labelInfo.sType = VK_STRUCTURE_TYPE_DEBUG_UTILS_LABEL_EXT;
  4740. labelInfo.pLabelName = text;
  4741. renderer->vkCmdInsertDebugUtilsLabelEXT(
  4742. vulkanCommandBuffer->commandBuffer,
  4743. &labelInfo);
  4744. }
  4745. }
  4746. static void VULKAN_PushDebugGroup(
  4747. SDL_GPUCommandBuffer *commandBuffer,
  4748. const char *name)
  4749. {
  4750. VulkanCommandBuffer *vulkanCommandBuffer = (VulkanCommandBuffer *)commandBuffer;
  4751. VulkanRenderer *renderer = vulkanCommandBuffer->renderer;
  4752. VkDebugUtilsLabelEXT labelInfo;
  4753. if (renderer->supportsDebugUtils) {
  4754. SDL_zero(labelInfo);
  4755. labelInfo.sType = VK_STRUCTURE_TYPE_DEBUG_UTILS_LABEL_EXT;
  4756. labelInfo.pLabelName = name;
  4757. renderer->vkCmdBeginDebugUtilsLabelEXT(
  4758. vulkanCommandBuffer->commandBuffer,
  4759. &labelInfo);
  4760. }
  4761. }
  4762. static void VULKAN_PopDebugGroup(
  4763. SDL_GPUCommandBuffer *commandBuffer)
  4764. {
  4765. VulkanCommandBuffer *vulkanCommandBuffer = (VulkanCommandBuffer *)commandBuffer;
  4766. VulkanRenderer *renderer = vulkanCommandBuffer->renderer;
  4767. if (renderer->supportsDebugUtils) {
  4768. renderer->vkCmdEndDebugUtilsLabelEXT(vulkanCommandBuffer->commandBuffer);
  4769. }
  4770. }
  4771. static VulkanTexture *VULKAN_INTERNAL_CreateTexture(
  4772. VulkanRenderer *renderer,
  4773. bool transitionToDefaultLayout,
  4774. const SDL_GPUTextureCreateInfo *createinfo)
  4775. {
  4776. VkResult vulkanResult;
  4777. VkImageCreateInfo imageCreateInfo;
  4778. VkImageCreateFlags imageCreateFlags = 0;
  4779. VkImageViewCreateInfo imageViewCreateInfo;
  4780. Uint8 bindResult;
  4781. VkImageUsageFlags vkUsageFlags = VK_IMAGE_USAGE_TRANSFER_SRC_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT;
  4782. Uint32 layerCount = (createinfo->type == SDL_GPU_TEXTURETYPE_3D) ? 1 : createinfo->layer_count_or_depth;
  4783. Uint32 depth = (createinfo->type == SDL_GPU_TEXTURETYPE_3D) ? createinfo->layer_count_or_depth : 1;
  4784. VulkanTexture *texture = SDL_calloc(1, sizeof(VulkanTexture));
  4785. texture->swizzle = SwizzleForSDLFormat(createinfo->format);
  4786. texture->depth = depth;
  4787. texture->usage = createinfo->usage;
  4788. SDL_SetAtomicInt(&texture->referenceCount, 0);
  4789. if (IsDepthFormat(createinfo->format)) {
  4790. texture->aspectFlags = VK_IMAGE_ASPECT_DEPTH_BIT;
  4791. if (IsStencilFormat(createinfo->format)) {
  4792. texture->aspectFlags |= VK_IMAGE_ASPECT_STENCIL_BIT;
  4793. }
  4794. } else {
  4795. texture->aspectFlags = VK_IMAGE_ASPECT_COLOR_BIT;
  4796. }
  4797. if (createinfo->type == SDL_GPU_TEXTURETYPE_CUBE || createinfo->type == SDL_GPU_TEXTURETYPE_CUBE_ARRAY) {
  4798. imageCreateFlags |= VK_IMAGE_CREATE_CUBE_COMPATIBLE_BIT;
  4799. } else if (createinfo->type == SDL_GPU_TEXTURETYPE_3D) {
  4800. imageCreateFlags |= VK_IMAGE_CREATE_2D_ARRAY_COMPATIBLE_BIT;
  4801. }
  4802. if (createinfo->usage & (SDL_GPU_TEXTUREUSAGE_SAMPLER |
  4803. SDL_GPU_TEXTUREUSAGE_GRAPHICS_STORAGE_READ |
  4804. SDL_GPU_TEXTUREUSAGE_COMPUTE_STORAGE_READ)) {
  4805. vkUsageFlags |= VK_IMAGE_USAGE_SAMPLED_BIT;
  4806. }
  4807. if (createinfo->usage & SDL_GPU_TEXTUREUSAGE_COLOR_TARGET) {
  4808. vkUsageFlags |= VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT;
  4809. }
  4810. if (createinfo->usage & SDL_GPU_TEXTUREUSAGE_DEPTH_STENCIL_TARGET) {
  4811. vkUsageFlags |= VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT;
  4812. }
  4813. if (createinfo->usage & (SDL_GPU_TEXTUREUSAGE_COMPUTE_STORAGE_WRITE |
  4814. SDL_GPU_TEXTUREUSAGE_COMPUTE_STORAGE_SIMULTANEOUS_READ_WRITE)) {
  4815. vkUsageFlags |= VK_IMAGE_USAGE_STORAGE_BIT;
  4816. }
  4817. imageCreateInfo.sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO;
  4818. imageCreateInfo.pNext = NULL;
  4819. imageCreateInfo.flags = imageCreateFlags;
  4820. imageCreateInfo.imageType = createinfo->type == SDL_GPU_TEXTURETYPE_3D ? VK_IMAGE_TYPE_3D : VK_IMAGE_TYPE_2D;
  4821. imageCreateInfo.format = SDLToVK_TextureFormat[createinfo->format];
  4822. imageCreateInfo.extent.width = createinfo->width;
  4823. imageCreateInfo.extent.height = createinfo->height;
  4824. imageCreateInfo.extent.depth = depth;
  4825. imageCreateInfo.mipLevels = createinfo->num_levels;
  4826. imageCreateInfo.arrayLayers = layerCount;
  4827. imageCreateInfo.samples = SDLToVK_SampleCount[createinfo->sample_count];
  4828. imageCreateInfo.tiling = VK_IMAGE_TILING_OPTIMAL;
  4829. imageCreateInfo.usage = vkUsageFlags;
  4830. imageCreateInfo.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
  4831. imageCreateInfo.queueFamilyIndexCount = 0;
  4832. imageCreateInfo.pQueueFamilyIndices = NULL;
  4833. imageCreateInfo.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED;
  4834. vulkanResult = renderer->vkCreateImage(
  4835. renderer->logicalDevice,
  4836. &imageCreateInfo,
  4837. NULL,
  4838. &texture->image);
  4839. if (vulkanResult != VK_SUCCESS) {
  4840. VULKAN_INTERNAL_DestroyTexture(renderer, texture);
  4841. CHECK_VULKAN_ERROR_AND_RETURN(vulkanResult, vkCreateImage, NULL);
  4842. }
  4843. bindResult = VULKAN_INTERNAL_BindMemoryForImage(
  4844. renderer,
  4845. texture->image,
  4846. &texture->usedRegion);
  4847. if (bindResult != 1) {
  4848. renderer->vkDestroyImage(
  4849. renderer->logicalDevice,
  4850. texture->image,
  4851. NULL);
  4852. VULKAN_INTERNAL_DestroyTexture(renderer, texture);
  4853. SET_STRING_ERROR_AND_RETURN("Unable to bind memory for texture!", NULL);
  4854. }
  4855. texture->usedRegion->vulkanTexture = texture; // lol
  4856. if (createinfo->usage & (SDL_GPU_TEXTUREUSAGE_SAMPLER | SDL_GPU_TEXTUREUSAGE_GRAPHICS_STORAGE_READ | SDL_GPU_TEXTUREUSAGE_COMPUTE_STORAGE_READ)) {
  4857. imageViewCreateInfo.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO;
  4858. imageViewCreateInfo.pNext = NULL;
  4859. imageViewCreateInfo.flags = 0;
  4860. imageViewCreateInfo.image = texture->image;
  4861. imageViewCreateInfo.format = SDLToVK_TextureFormat[createinfo->format];
  4862. imageViewCreateInfo.components = texture->swizzle;
  4863. imageViewCreateInfo.subresourceRange.aspectMask = texture->aspectFlags & ~VK_IMAGE_ASPECT_STENCIL_BIT; // Can't sample stencil values
  4864. imageViewCreateInfo.subresourceRange.baseMipLevel = 0;
  4865. imageViewCreateInfo.subresourceRange.levelCount = createinfo->num_levels;
  4866. imageViewCreateInfo.subresourceRange.baseArrayLayer = 0;
  4867. imageViewCreateInfo.subresourceRange.layerCount = layerCount;
  4868. if (createinfo->type == SDL_GPU_TEXTURETYPE_CUBE) {
  4869. imageViewCreateInfo.viewType = VK_IMAGE_VIEW_TYPE_CUBE;
  4870. } else if (createinfo->type == SDL_GPU_TEXTURETYPE_CUBE_ARRAY) {
  4871. imageViewCreateInfo.viewType = VK_IMAGE_VIEW_TYPE_CUBE_ARRAY;
  4872. } else if (createinfo->type == SDL_GPU_TEXTURETYPE_3D) {
  4873. imageViewCreateInfo.viewType = VK_IMAGE_VIEW_TYPE_3D;
  4874. } else if (createinfo->type == SDL_GPU_TEXTURETYPE_2D_ARRAY) {
  4875. imageViewCreateInfo.viewType = VK_IMAGE_VIEW_TYPE_2D_ARRAY;
  4876. } else {
  4877. imageViewCreateInfo.viewType = VK_IMAGE_VIEW_TYPE_2D;
  4878. }
  4879. vulkanResult = renderer->vkCreateImageView(
  4880. renderer->logicalDevice,
  4881. &imageViewCreateInfo,
  4882. NULL,
  4883. &texture->fullView);
  4884. if (vulkanResult != VK_SUCCESS) {
  4885. VULKAN_INTERNAL_DestroyTexture(renderer, texture);
  4886. CHECK_VULKAN_ERROR_AND_RETURN(vulkanResult, "vkCreateImageView", NULL);
  4887. }
  4888. }
  4889. // Define slices
  4890. texture->subresourceCount = layerCount * createinfo->num_levels;
  4891. texture->subresources = SDL_calloc(
  4892. texture->subresourceCount,
  4893. sizeof(VulkanTextureSubresource));
  4894. for (Uint32 i = 0; i < layerCount; i += 1) {
  4895. for (Uint32 j = 0; j < createinfo->num_levels; j += 1) {
  4896. Uint32 subresourceIndex = VULKAN_INTERNAL_GetTextureSubresourceIndex(
  4897. j,
  4898. i,
  4899. createinfo->num_levels);
  4900. if (createinfo->usage & SDL_GPU_TEXTUREUSAGE_COLOR_TARGET) {
  4901. texture->subresources[subresourceIndex].renderTargetViews = SDL_malloc(
  4902. depth * sizeof(VkImageView));
  4903. if (depth > 1) {
  4904. for (Uint32 k = 0; k < depth; k += 1) {
  4905. if (!VULKAN_INTERNAL_CreateRenderTargetView(
  4906. renderer,
  4907. texture,
  4908. k,
  4909. j,
  4910. SDLToVK_TextureFormat[createinfo->format],
  4911. texture->swizzle,
  4912. &texture->subresources[subresourceIndex].renderTargetViews[k])) {
  4913. VULKAN_INTERNAL_DestroyTexture(renderer, texture);
  4914. return NULL;
  4915. }
  4916. }
  4917. } else {
  4918. if (!VULKAN_INTERNAL_CreateRenderTargetView(
  4919. renderer,
  4920. texture,
  4921. i,
  4922. j,
  4923. SDLToVK_TextureFormat[createinfo->format],
  4924. texture->swizzle,
  4925. &texture->subresources[subresourceIndex].renderTargetViews[0])) {
  4926. VULKAN_INTERNAL_DestroyTexture(renderer, texture);
  4927. return NULL;
  4928. }
  4929. }
  4930. }
  4931. if ((createinfo->usage & SDL_GPU_TEXTUREUSAGE_COMPUTE_STORAGE_WRITE) || (createinfo->usage & SDL_GPU_TEXTUREUSAGE_COMPUTE_STORAGE_SIMULTANEOUS_READ_WRITE)) {
  4932. if (!VULKAN_INTERNAL_CreateSubresourceView(
  4933. renderer,
  4934. createinfo,
  4935. texture,
  4936. i,
  4937. j,
  4938. texture->swizzle,
  4939. &texture->subresources[subresourceIndex].computeWriteView)) {
  4940. VULKAN_INTERNAL_DestroyTexture(renderer, texture);
  4941. return NULL;
  4942. }
  4943. }
  4944. if (createinfo->usage & SDL_GPU_TEXTUREUSAGE_DEPTH_STENCIL_TARGET) {
  4945. if (!VULKAN_INTERNAL_CreateSubresourceView(
  4946. renderer,
  4947. createinfo,
  4948. texture,
  4949. i,
  4950. j,
  4951. texture->swizzle,
  4952. &texture->subresources[subresourceIndex].depthStencilView)) {
  4953. VULKAN_INTERNAL_DestroyTexture(renderer, texture);
  4954. return NULL;
  4955. }
  4956. }
  4957. texture->subresources[subresourceIndex].parent = texture;
  4958. texture->subresources[subresourceIndex].layer = i;
  4959. texture->subresources[subresourceIndex].level = j;
  4960. }
  4961. }
  4962. // Set debug name if applicable
  4963. if (renderer->debugMode && renderer->supportsDebugUtils && SDL_HasProperty(createinfo->props, SDL_PROP_GPU_TEXTURE_CREATE_NAME_STRING)) {
  4964. VkDebugUtilsObjectNameInfoEXT nameInfo;
  4965. nameInfo.sType = VK_STRUCTURE_TYPE_DEBUG_UTILS_OBJECT_NAME_INFO_EXT;
  4966. nameInfo.pNext = NULL;
  4967. nameInfo.pObjectName = SDL_GetStringProperty(createinfo->props, SDL_PROP_GPU_TEXTURE_CREATE_NAME_STRING, NULL);
  4968. nameInfo.objectType = VK_OBJECT_TYPE_IMAGE;
  4969. nameInfo.objectHandle = (uint64_t)texture->image;
  4970. renderer->vkSetDebugUtilsObjectNameEXT(
  4971. renderer->logicalDevice,
  4972. &nameInfo);
  4973. }
  4974. if (transitionToDefaultLayout) {
  4975. // Let's transition to the default barrier state, because for some reason Vulkan doesn't let us do that with initialLayout.
  4976. VulkanCommandBuffer *barrierCommandBuffer = (VulkanCommandBuffer *)VULKAN_AcquireCommandBuffer((SDL_GPURenderer *)renderer);
  4977. VULKAN_INTERNAL_TextureTransitionToDefaultUsage(
  4978. renderer,
  4979. barrierCommandBuffer,
  4980. VULKAN_TEXTURE_USAGE_MODE_UNINITIALIZED,
  4981. texture);
  4982. VULKAN_INTERNAL_TrackTexture(barrierCommandBuffer, texture);
  4983. if (!VULKAN_Submit((SDL_GPUCommandBuffer *)barrierCommandBuffer)) {
  4984. VULKAN_INTERNAL_DestroyTexture(renderer, texture);
  4985. return NULL;
  4986. }
  4987. }
  4988. return texture;
  4989. }
  4990. static void VULKAN_INTERNAL_CycleActiveBuffer(
  4991. VulkanRenderer *renderer,
  4992. VulkanBufferContainer *container)
  4993. {
  4994. VulkanBuffer *buffer;
  4995. // If a previously-cycled buffer is available, we can use that.
  4996. for (Uint32 i = 0; i < container->bufferCount; i += 1) {
  4997. buffer = container->buffers[i];
  4998. if (SDL_GetAtomicInt(&buffer->referenceCount) == 0) {
  4999. container->activeBuffer = buffer;
  5000. return;
  5001. }
  5002. }
  5003. // No buffer handle is available, create a new one.
  5004. buffer = VULKAN_INTERNAL_CreateBuffer(
  5005. renderer,
  5006. container->activeBuffer->size,
  5007. container->activeBuffer->usage,
  5008. container->activeBuffer->type,
  5009. container->dedicated,
  5010. container->debugName);
  5011. if (!buffer) {
  5012. return;
  5013. }
  5014. EXPAND_ARRAY_IF_NEEDED(
  5015. container->buffers,
  5016. VulkanBuffer *,
  5017. container->bufferCount + 1,
  5018. container->bufferCapacity,
  5019. container->bufferCapacity * 2);
  5020. container->buffers[container->bufferCount] = buffer;
  5021. buffer->container = container;
  5022. buffer->containerIndex = container->bufferCount;
  5023. container->bufferCount += 1;
  5024. container->activeBuffer = buffer;
  5025. }
  5026. static void VULKAN_INTERNAL_CycleActiveTexture(
  5027. VulkanRenderer *renderer,
  5028. VulkanCommandBuffer *commandBuffer,
  5029. VulkanTextureContainer *container)
  5030. {
  5031. VulkanTexture *texture;
  5032. // If a previously-cycled texture is available, we can use that.
  5033. for (Uint32 i = 0; i < container->textureCount; i += 1) {
  5034. texture = container->textures[i];
  5035. if (SDL_GetAtomicInt(&texture->referenceCount) == 0) {
  5036. container->activeTexture = texture;
  5037. return;
  5038. }
  5039. }
  5040. // No texture is available, generate a new one.
  5041. texture = VULKAN_INTERNAL_CreateTexture(
  5042. renderer,
  5043. false,
  5044. &container->header.info);
  5045. VULKAN_INTERNAL_TextureTransitionToDefaultUsage(
  5046. renderer,
  5047. commandBuffer,
  5048. VULKAN_TEXTURE_USAGE_MODE_UNINITIALIZED,
  5049. texture);
  5050. if (!texture) {
  5051. return;
  5052. }
  5053. EXPAND_ARRAY_IF_NEEDED(
  5054. container->textures,
  5055. VulkanTexture *,
  5056. container->textureCount + 1,
  5057. container->textureCapacity,
  5058. container->textureCapacity * 2);
  5059. container->textures[container->textureCount] = texture;
  5060. texture->container = container;
  5061. texture->containerIndex = container->textureCount;
  5062. container->textureCount += 1;
  5063. container->activeTexture = texture;
  5064. }
  5065. static VulkanBuffer *VULKAN_INTERNAL_PrepareBufferForWrite(
  5066. VulkanRenderer *renderer,
  5067. VulkanCommandBuffer *commandBuffer,
  5068. VulkanBufferContainer *bufferContainer,
  5069. bool cycle,
  5070. VulkanBufferUsageMode destinationUsageMode)
  5071. {
  5072. if (
  5073. cycle &&
  5074. SDL_GetAtomicInt(&bufferContainer->activeBuffer->referenceCount) > 0) {
  5075. VULKAN_INTERNAL_CycleActiveBuffer(
  5076. renderer,
  5077. bufferContainer);
  5078. }
  5079. VULKAN_INTERNAL_BufferTransitionFromDefaultUsage(
  5080. renderer,
  5081. commandBuffer,
  5082. destinationUsageMode,
  5083. bufferContainer->activeBuffer);
  5084. return bufferContainer->activeBuffer;
  5085. }
  5086. static VulkanTextureSubresource *VULKAN_INTERNAL_PrepareTextureSubresourceForWrite(
  5087. VulkanRenderer *renderer,
  5088. VulkanCommandBuffer *commandBuffer,
  5089. VulkanTextureContainer *textureContainer,
  5090. Uint32 layer,
  5091. Uint32 level,
  5092. bool cycle,
  5093. VulkanTextureUsageMode destinationUsageMode)
  5094. {
  5095. VulkanTextureSubresource *textureSubresource = VULKAN_INTERNAL_FetchTextureSubresource(
  5096. textureContainer,
  5097. layer,
  5098. level);
  5099. if (
  5100. cycle &&
  5101. textureContainer->canBeCycled &&
  5102. SDL_GetAtomicInt(&textureContainer->activeTexture->referenceCount) > 0) {
  5103. VULKAN_INTERNAL_CycleActiveTexture(
  5104. renderer,
  5105. commandBuffer,
  5106. textureContainer);
  5107. textureSubresource = VULKAN_INTERNAL_FetchTextureSubresource(
  5108. textureContainer,
  5109. layer,
  5110. level);
  5111. }
  5112. // always do barrier because of layout transitions
  5113. VULKAN_INTERNAL_TextureSubresourceTransitionFromDefaultUsage(
  5114. renderer,
  5115. commandBuffer,
  5116. destinationUsageMode,
  5117. textureSubresource);
  5118. return textureSubresource;
  5119. }
  5120. static VkRenderPass VULKAN_INTERNAL_CreateRenderPass(
  5121. VulkanRenderer *renderer,
  5122. const SDL_GPUColorTargetInfo *colorTargetInfos,
  5123. Uint32 numColorTargets,
  5124. const SDL_GPUDepthStencilTargetInfo *depthStencilTargetInfo)
  5125. {
  5126. VkResult vulkanResult;
  5127. VkAttachmentDescription attachmentDescriptions[2 * MAX_COLOR_TARGET_BINDINGS + 1 /* depth */];
  5128. VkAttachmentReference colorAttachmentReferences[MAX_COLOR_TARGET_BINDINGS];
  5129. VkAttachmentReference resolveReferences[MAX_COLOR_TARGET_BINDINGS];
  5130. VkAttachmentReference depthStencilAttachmentReference;
  5131. VkRenderPassCreateInfo renderPassCreateInfo;
  5132. VkSubpassDescription subpass;
  5133. VkRenderPass renderPass;
  5134. Uint32 i;
  5135. Uint32 attachmentDescriptionCount = 0;
  5136. Uint32 colorAttachmentReferenceCount = 0;
  5137. Uint32 resolveReferenceCount = 0;
  5138. for (i = 0; i < numColorTargets; i += 1) {
  5139. VulkanTextureContainer *container = (VulkanTextureContainer *)colorTargetInfos[i].texture;
  5140. attachmentDescriptions[attachmentDescriptionCount].flags = 0;
  5141. attachmentDescriptions[attachmentDescriptionCount].format = SDLToVK_TextureFormat[container->header.info.format];
  5142. attachmentDescriptions[attachmentDescriptionCount].samples = SDLToVK_SampleCount[container->header.info.sample_count];
  5143. attachmentDescriptions[attachmentDescriptionCount].loadOp = SDLToVK_LoadOp[colorTargetInfos[i].load_op];
  5144. attachmentDescriptions[attachmentDescriptionCount].storeOp = SDLToVK_StoreOp[colorTargetInfos[i].store_op];
  5145. attachmentDescriptions[attachmentDescriptionCount].stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE;
  5146. attachmentDescriptions[attachmentDescriptionCount].stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE;
  5147. attachmentDescriptions[attachmentDescriptionCount].initialLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
  5148. attachmentDescriptions[attachmentDescriptionCount].finalLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
  5149. colorAttachmentReferences[colorAttachmentReferenceCount].attachment = attachmentDescriptionCount;
  5150. colorAttachmentReferences[colorAttachmentReferenceCount].layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
  5151. attachmentDescriptionCount += 1;
  5152. if (colorTargetInfos[i].store_op == SDL_GPU_STOREOP_RESOLVE || colorTargetInfos[i].store_op == SDL_GPU_STOREOP_RESOLVE_AND_STORE) {
  5153. VulkanTextureContainer *resolveContainer = (VulkanTextureContainer *)colorTargetInfos[i].resolve_texture;
  5154. attachmentDescriptions[attachmentDescriptionCount].flags = 0;
  5155. attachmentDescriptions[attachmentDescriptionCount].format = SDLToVK_TextureFormat[resolveContainer->header.info.format];
  5156. attachmentDescriptions[attachmentDescriptionCount].samples = SDLToVK_SampleCount[resolveContainer->header.info.sample_count];
  5157. attachmentDescriptions[attachmentDescriptionCount].loadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE; // The texture will be overwritten anyway
  5158. attachmentDescriptions[attachmentDescriptionCount].storeOp = VK_ATTACHMENT_STORE_OP_STORE; // Always store the resolve texture
  5159. attachmentDescriptions[attachmentDescriptionCount].stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE;
  5160. attachmentDescriptions[attachmentDescriptionCount].stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE;
  5161. attachmentDescriptions[attachmentDescriptionCount].initialLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
  5162. attachmentDescriptions[attachmentDescriptionCount].finalLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
  5163. resolveReferences[colorAttachmentReferenceCount].attachment = attachmentDescriptionCount;
  5164. resolveReferences[colorAttachmentReferenceCount].layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
  5165. attachmentDescriptionCount += 1;
  5166. resolveReferenceCount += 1;
  5167. } else {
  5168. resolveReferences[colorAttachmentReferenceCount].attachment = VK_ATTACHMENT_UNUSED;
  5169. }
  5170. colorAttachmentReferenceCount += 1;
  5171. }
  5172. subpass.pipelineBindPoint = VK_PIPELINE_BIND_POINT_GRAPHICS;
  5173. subpass.flags = 0;
  5174. subpass.inputAttachmentCount = 0;
  5175. subpass.pInputAttachments = NULL;
  5176. subpass.colorAttachmentCount = numColorTargets;
  5177. subpass.pColorAttachments = colorAttachmentReferences;
  5178. subpass.preserveAttachmentCount = 0;
  5179. subpass.pPreserveAttachments = NULL;
  5180. if (depthStencilTargetInfo == NULL) {
  5181. subpass.pDepthStencilAttachment = NULL;
  5182. } else {
  5183. VulkanTextureContainer *container = (VulkanTextureContainer *)depthStencilTargetInfo->texture;
  5184. attachmentDescriptions[attachmentDescriptionCount].flags = 0;
  5185. attachmentDescriptions[attachmentDescriptionCount].format = SDLToVK_TextureFormat[container->header.info.format];
  5186. attachmentDescriptions[attachmentDescriptionCount].samples = SDLToVK_SampleCount[container->header.info.sample_count];
  5187. attachmentDescriptions[attachmentDescriptionCount].loadOp = SDLToVK_LoadOp[depthStencilTargetInfo->load_op];
  5188. attachmentDescriptions[attachmentDescriptionCount].storeOp = SDLToVK_StoreOp[depthStencilTargetInfo->store_op];
  5189. attachmentDescriptions[attachmentDescriptionCount].stencilLoadOp = SDLToVK_LoadOp[depthStencilTargetInfo->stencil_load_op];
  5190. attachmentDescriptions[attachmentDescriptionCount].stencilStoreOp = SDLToVK_StoreOp[depthStencilTargetInfo->stencil_store_op];
  5191. attachmentDescriptions[attachmentDescriptionCount].initialLayout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL;
  5192. attachmentDescriptions[attachmentDescriptionCount].finalLayout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL;
  5193. depthStencilAttachmentReference.attachment = attachmentDescriptionCount;
  5194. depthStencilAttachmentReference.layout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL;
  5195. subpass.pDepthStencilAttachment = &depthStencilAttachmentReference;
  5196. attachmentDescriptionCount += 1;
  5197. }
  5198. if (resolveReferenceCount > 0) {
  5199. subpass.pResolveAttachments = resolveReferences;
  5200. } else {
  5201. subpass.pResolveAttachments = NULL;
  5202. }
  5203. renderPassCreateInfo.sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO;
  5204. renderPassCreateInfo.pNext = NULL;
  5205. renderPassCreateInfo.flags = 0;
  5206. renderPassCreateInfo.pAttachments = attachmentDescriptions;
  5207. renderPassCreateInfo.attachmentCount = attachmentDescriptionCount;
  5208. renderPassCreateInfo.subpassCount = 1;
  5209. renderPassCreateInfo.pSubpasses = &subpass;
  5210. renderPassCreateInfo.dependencyCount = 0;
  5211. renderPassCreateInfo.pDependencies = NULL;
  5212. vulkanResult = renderer->vkCreateRenderPass(
  5213. renderer->logicalDevice,
  5214. &renderPassCreateInfo,
  5215. NULL,
  5216. &renderPass);
  5217. CHECK_VULKAN_ERROR_AND_RETURN(vulkanResult, vkCreateRenderPass, VK_NULL_HANDLE);
  5218. return renderPass;
  5219. }
  5220. static VkRenderPass VULKAN_INTERNAL_CreateTransientRenderPass(
  5221. VulkanRenderer *renderer,
  5222. SDL_GPUGraphicsPipelineTargetInfo targetInfo,
  5223. VkSampleCountFlagBits sampleCount)
  5224. {
  5225. VkAttachmentDescription attachmentDescriptions[MAX_COLOR_TARGET_BINDINGS + 1 /* depth */];
  5226. VkAttachmentReference colorAttachmentReferences[MAX_COLOR_TARGET_BINDINGS];
  5227. VkAttachmentReference depthStencilAttachmentReference;
  5228. SDL_GPUColorTargetDescription attachmentDescription;
  5229. VkSubpassDescription subpass;
  5230. VkRenderPassCreateInfo renderPassCreateInfo;
  5231. VkRenderPass renderPass;
  5232. VkResult result;
  5233. Uint32 attachmentDescriptionCount = 0;
  5234. Uint32 colorAttachmentReferenceCount = 0;
  5235. Uint32 i;
  5236. for (i = 0; i < targetInfo.num_color_targets; i += 1) {
  5237. attachmentDescription = targetInfo.color_target_descriptions[i];
  5238. attachmentDescriptions[attachmentDescriptionCount].flags = 0;
  5239. attachmentDescriptions[attachmentDescriptionCount].format = SDLToVK_TextureFormat[attachmentDescription.format];
  5240. attachmentDescriptions[attachmentDescriptionCount].samples = sampleCount;
  5241. attachmentDescriptions[attachmentDescriptionCount].loadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE;
  5242. attachmentDescriptions[attachmentDescriptionCount].storeOp = VK_ATTACHMENT_STORE_OP_DONT_CARE;
  5243. attachmentDescriptions[attachmentDescriptionCount].stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE;
  5244. attachmentDescriptions[attachmentDescriptionCount].stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE;
  5245. attachmentDescriptions[attachmentDescriptionCount].initialLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
  5246. attachmentDescriptions[attachmentDescriptionCount].finalLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
  5247. colorAttachmentReferences[colorAttachmentReferenceCount].attachment = attachmentDescriptionCount;
  5248. colorAttachmentReferences[colorAttachmentReferenceCount].layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
  5249. attachmentDescriptionCount += 1;
  5250. colorAttachmentReferenceCount += 1;
  5251. }
  5252. subpass.pipelineBindPoint = VK_PIPELINE_BIND_POINT_GRAPHICS;
  5253. subpass.flags = 0;
  5254. subpass.inputAttachmentCount = 0;
  5255. subpass.pInputAttachments = NULL;
  5256. subpass.colorAttachmentCount = targetInfo.num_color_targets;
  5257. subpass.pColorAttachments = colorAttachmentReferences;
  5258. subpass.preserveAttachmentCount = 0;
  5259. subpass.pPreserveAttachments = NULL;
  5260. if (targetInfo.has_depth_stencil_target) {
  5261. attachmentDescriptions[attachmentDescriptionCount].flags = 0;
  5262. attachmentDescriptions[attachmentDescriptionCount].format = SDLToVK_TextureFormat[targetInfo.depth_stencil_format];
  5263. attachmentDescriptions[attachmentDescriptionCount].samples = sampleCount;
  5264. attachmentDescriptions[attachmentDescriptionCount].loadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE;
  5265. attachmentDescriptions[attachmentDescriptionCount].storeOp = VK_ATTACHMENT_STORE_OP_DONT_CARE;
  5266. attachmentDescriptions[attachmentDescriptionCount].stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE;
  5267. attachmentDescriptions[attachmentDescriptionCount].stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE;
  5268. attachmentDescriptions[attachmentDescriptionCount].initialLayout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL;
  5269. attachmentDescriptions[attachmentDescriptionCount].finalLayout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL;
  5270. depthStencilAttachmentReference.attachment = attachmentDescriptionCount;
  5271. depthStencilAttachmentReference.layout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL;
  5272. subpass.pDepthStencilAttachment = &depthStencilAttachmentReference;
  5273. attachmentDescriptionCount += 1;
  5274. } else {
  5275. subpass.pDepthStencilAttachment = NULL;
  5276. }
  5277. // Resolve attachments aren't needed for transient passes
  5278. subpass.pResolveAttachments = NULL;
  5279. renderPassCreateInfo.sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO;
  5280. renderPassCreateInfo.pNext = NULL;
  5281. renderPassCreateInfo.flags = 0;
  5282. renderPassCreateInfo.pAttachments = attachmentDescriptions;
  5283. renderPassCreateInfo.attachmentCount = attachmentDescriptionCount;
  5284. renderPassCreateInfo.subpassCount = 1;
  5285. renderPassCreateInfo.pSubpasses = &subpass;
  5286. renderPassCreateInfo.dependencyCount = 0;
  5287. renderPassCreateInfo.pDependencies = NULL;
  5288. result = renderer->vkCreateRenderPass(
  5289. renderer->logicalDevice,
  5290. &renderPassCreateInfo,
  5291. NULL,
  5292. &renderPass);
  5293. CHECK_VULKAN_ERROR_AND_RETURN(result, vkCreateRenderPass, VK_NULL_HANDLE);
  5294. return renderPass;
  5295. }
  5296. static SDL_GPUGraphicsPipeline *VULKAN_CreateGraphicsPipeline(
  5297. SDL_GPURenderer *driverData,
  5298. const SDL_GPUGraphicsPipelineCreateInfo *createinfo)
  5299. {
  5300. VkResult vulkanResult;
  5301. Uint32 i;
  5302. VulkanGraphicsPipeline *graphicsPipeline = (VulkanGraphicsPipeline *)SDL_malloc(sizeof(VulkanGraphicsPipeline));
  5303. VkGraphicsPipelineCreateInfo vkPipelineCreateInfo;
  5304. VkPipelineShaderStageCreateInfo shaderStageCreateInfos[2];
  5305. VkPipelineVertexInputStateCreateInfo vertexInputStateCreateInfo;
  5306. VkVertexInputBindingDescription *vertexInputBindingDescriptions = SDL_stack_alloc(VkVertexInputBindingDescription, createinfo->vertex_input_state.num_vertex_buffers);
  5307. VkVertexInputAttributeDescription *vertexInputAttributeDescriptions = SDL_stack_alloc(VkVertexInputAttributeDescription, createinfo->vertex_input_state.num_vertex_attributes);
  5308. VkPipelineInputAssemblyStateCreateInfo inputAssemblyStateCreateInfo;
  5309. VkPipelineViewportStateCreateInfo viewportStateCreateInfo;
  5310. VkPipelineRasterizationStateCreateInfo rasterizationStateCreateInfo;
  5311. VkPipelineMultisampleStateCreateInfo multisampleStateCreateInfo;
  5312. VkPipelineDepthStencilStateCreateInfo depthStencilStateCreateInfo;
  5313. VkStencilOpState frontStencilState;
  5314. VkStencilOpState backStencilState;
  5315. VkPipelineColorBlendStateCreateInfo colorBlendStateCreateInfo;
  5316. VkPipelineColorBlendAttachmentState *colorBlendAttachmentStates = SDL_stack_alloc(
  5317. VkPipelineColorBlendAttachmentState,
  5318. createinfo->target_info.num_color_targets);
  5319. static const VkDynamicState dynamicStates[] = {
  5320. VK_DYNAMIC_STATE_VIEWPORT,
  5321. VK_DYNAMIC_STATE_SCISSOR,
  5322. VK_DYNAMIC_STATE_BLEND_CONSTANTS,
  5323. VK_DYNAMIC_STATE_STENCIL_REFERENCE
  5324. };
  5325. VkPipelineDynamicStateCreateInfo dynamicStateCreateInfo;
  5326. VulkanRenderer *renderer = (VulkanRenderer *)driverData;
  5327. // Create a "compatible" render pass
  5328. VkRenderPass transientRenderPass = VULKAN_INTERNAL_CreateTransientRenderPass(
  5329. renderer,
  5330. createinfo->target_info,
  5331. SDLToVK_SampleCount[createinfo->multisample_state.sample_count]);
  5332. // Dynamic state
  5333. dynamicStateCreateInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_DYNAMIC_STATE_CREATE_INFO;
  5334. dynamicStateCreateInfo.pNext = NULL;
  5335. dynamicStateCreateInfo.flags = 0;
  5336. dynamicStateCreateInfo.dynamicStateCount = SDL_arraysize(dynamicStates);
  5337. dynamicStateCreateInfo.pDynamicStates = dynamicStates;
  5338. // Shader stages
  5339. graphicsPipeline->vertexShader = (VulkanShader *)createinfo->vertex_shader;
  5340. SDL_AtomicIncRef(&graphicsPipeline->vertexShader->referenceCount);
  5341. shaderStageCreateInfos[0].sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
  5342. shaderStageCreateInfos[0].pNext = NULL;
  5343. shaderStageCreateInfos[0].flags = 0;
  5344. shaderStageCreateInfos[0].stage = VK_SHADER_STAGE_VERTEX_BIT;
  5345. shaderStageCreateInfos[0].module = graphicsPipeline->vertexShader->shaderModule;
  5346. shaderStageCreateInfos[0].pName = graphicsPipeline->vertexShader->entrypointName;
  5347. shaderStageCreateInfos[0].pSpecializationInfo = NULL;
  5348. graphicsPipeline->fragmentShader = (VulkanShader *)createinfo->fragment_shader;
  5349. SDL_AtomicIncRef(&graphicsPipeline->fragmentShader->referenceCount);
  5350. shaderStageCreateInfos[1].sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
  5351. shaderStageCreateInfos[1].pNext = NULL;
  5352. shaderStageCreateInfos[1].flags = 0;
  5353. shaderStageCreateInfos[1].stage = VK_SHADER_STAGE_FRAGMENT_BIT;
  5354. shaderStageCreateInfos[1].module = graphicsPipeline->fragmentShader->shaderModule;
  5355. shaderStageCreateInfos[1].pName = graphicsPipeline->fragmentShader->entrypointName;
  5356. shaderStageCreateInfos[1].pSpecializationInfo = NULL;
  5357. if (renderer->debugMode) {
  5358. if (graphicsPipeline->vertexShader->stage != SDL_GPU_SHADERSTAGE_VERTEX) {
  5359. SDL_assert_release(!"CreateGraphicsPipeline was passed a fragment shader for the vertex stage");
  5360. }
  5361. if (graphicsPipeline->fragmentShader->stage != SDL_GPU_SHADERSTAGE_FRAGMENT) {
  5362. SDL_assert_release(!"CreateGraphicsPipeline was passed a vertex shader for the fragment stage");
  5363. }
  5364. }
  5365. // Vertex input
  5366. for (i = 0; i < createinfo->vertex_input_state.num_vertex_buffers; i += 1) {
  5367. vertexInputBindingDescriptions[i].binding = createinfo->vertex_input_state.vertex_buffer_descriptions[i].slot;
  5368. vertexInputBindingDescriptions[i].inputRate = SDLToVK_VertexInputRate[createinfo->vertex_input_state.vertex_buffer_descriptions[i].input_rate];
  5369. vertexInputBindingDescriptions[i].stride = createinfo->vertex_input_state.vertex_buffer_descriptions[i].pitch;
  5370. }
  5371. for (i = 0; i < createinfo->vertex_input_state.num_vertex_attributes; i += 1) {
  5372. vertexInputAttributeDescriptions[i].binding = createinfo->vertex_input_state.vertex_attributes[i].buffer_slot;
  5373. vertexInputAttributeDescriptions[i].format = SDLToVK_VertexFormat[createinfo->vertex_input_state.vertex_attributes[i].format];
  5374. vertexInputAttributeDescriptions[i].location = createinfo->vertex_input_state.vertex_attributes[i].location;
  5375. vertexInputAttributeDescriptions[i].offset = createinfo->vertex_input_state.vertex_attributes[i].offset;
  5376. }
  5377. vertexInputStateCreateInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO;
  5378. vertexInputStateCreateInfo.pNext = NULL;
  5379. vertexInputStateCreateInfo.flags = 0;
  5380. vertexInputStateCreateInfo.vertexBindingDescriptionCount = createinfo->vertex_input_state.num_vertex_buffers;
  5381. vertexInputStateCreateInfo.pVertexBindingDescriptions = vertexInputBindingDescriptions;
  5382. vertexInputStateCreateInfo.vertexAttributeDescriptionCount = createinfo->vertex_input_state.num_vertex_attributes;
  5383. vertexInputStateCreateInfo.pVertexAttributeDescriptions = vertexInputAttributeDescriptions;
  5384. // Topology
  5385. inputAssemblyStateCreateInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO;
  5386. inputAssemblyStateCreateInfo.pNext = NULL;
  5387. inputAssemblyStateCreateInfo.flags = 0;
  5388. inputAssemblyStateCreateInfo.primitiveRestartEnable = VK_FALSE;
  5389. inputAssemblyStateCreateInfo.topology = SDLToVK_PrimitiveType[createinfo->primitive_type];
  5390. graphicsPipeline->primitiveType = createinfo->primitive_type;
  5391. // Viewport
  5392. // NOTE: viewport and scissor are dynamic, and must be set using the command buffer
  5393. viewportStateCreateInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO;
  5394. viewportStateCreateInfo.pNext = NULL;
  5395. viewportStateCreateInfo.flags = 0;
  5396. viewportStateCreateInfo.viewportCount = 1;
  5397. viewportStateCreateInfo.pViewports = NULL;
  5398. viewportStateCreateInfo.scissorCount = 1;
  5399. viewportStateCreateInfo.pScissors = NULL;
  5400. // Rasterization
  5401. rasterizationStateCreateInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO;
  5402. rasterizationStateCreateInfo.pNext = NULL;
  5403. rasterizationStateCreateInfo.flags = 0;
  5404. rasterizationStateCreateInfo.depthClampEnable = !createinfo->rasterizer_state.enable_depth_clip;
  5405. rasterizationStateCreateInfo.rasterizerDiscardEnable = VK_FALSE;
  5406. rasterizationStateCreateInfo.polygonMode = SDLToVK_PolygonMode(
  5407. renderer,
  5408. createinfo->rasterizer_state.fill_mode);
  5409. rasterizationStateCreateInfo.cullMode = SDLToVK_CullMode[createinfo->rasterizer_state.cull_mode];
  5410. rasterizationStateCreateInfo.frontFace = SDLToVK_FrontFace[createinfo->rasterizer_state.front_face];
  5411. rasterizationStateCreateInfo.depthBiasEnable =
  5412. createinfo->rasterizer_state.enable_depth_bias;
  5413. rasterizationStateCreateInfo.depthBiasConstantFactor =
  5414. createinfo->rasterizer_state.depth_bias_constant_factor;
  5415. rasterizationStateCreateInfo.depthBiasClamp =
  5416. createinfo->rasterizer_state.depth_bias_clamp;
  5417. rasterizationStateCreateInfo.depthBiasSlopeFactor =
  5418. createinfo->rasterizer_state.depth_bias_slope_factor;
  5419. rasterizationStateCreateInfo.lineWidth = 1.0f;
  5420. // Multisample
  5421. Uint32 sampleMask = 0xFFFFFFFF;
  5422. multisampleStateCreateInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO;
  5423. multisampleStateCreateInfo.pNext = NULL;
  5424. multisampleStateCreateInfo.flags = 0;
  5425. multisampleStateCreateInfo.rasterizationSamples = SDLToVK_SampleCount[createinfo->multisample_state.sample_count];
  5426. multisampleStateCreateInfo.sampleShadingEnable = VK_FALSE;
  5427. multisampleStateCreateInfo.minSampleShading = 1.0f;
  5428. multisampleStateCreateInfo.pSampleMask = &sampleMask;
  5429. multisampleStateCreateInfo.alphaToCoverageEnable = createinfo->multisample_state.enable_alpha_to_coverage;
  5430. multisampleStateCreateInfo.alphaToOneEnable = VK_FALSE;
  5431. // Depth Stencil State
  5432. frontStencilState.failOp = SDLToVK_StencilOp[createinfo->depth_stencil_state.front_stencil_state.fail_op];
  5433. frontStencilState.passOp = SDLToVK_StencilOp[createinfo->depth_stencil_state.front_stencil_state.pass_op];
  5434. frontStencilState.depthFailOp = SDLToVK_StencilOp[createinfo->depth_stencil_state.front_stencil_state.depth_fail_op];
  5435. frontStencilState.compareOp = SDLToVK_CompareOp[createinfo->depth_stencil_state.front_stencil_state.compare_op];
  5436. frontStencilState.compareMask =
  5437. createinfo->depth_stencil_state.compare_mask;
  5438. frontStencilState.writeMask =
  5439. createinfo->depth_stencil_state.write_mask;
  5440. frontStencilState.reference = 0;
  5441. backStencilState.failOp = SDLToVK_StencilOp[createinfo->depth_stencil_state.back_stencil_state.fail_op];
  5442. backStencilState.passOp = SDLToVK_StencilOp[createinfo->depth_stencil_state.back_stencil_state.pass_op];
  5443. backStencilState.depthFailOp = SDLToVK_StencilOp[createinfo->depth_stencil_state.back_stencil_state.depth_fail_op];
  5444. backStencilState.compareOp = SDLToVK_CompareOp[createinfo->depth_stencil_state.back_stencil_state.compare_op];
  5445. backStencilState.compareMask =
  5446. createinfo->depth_stencil_state.compare_mask;
  5447. backStencilState.writeMask =
  5448. createinfo->depth_stencil_state.write_mask;
  5449. backStencilState.reference = 0;
  5450. depthStencilStateCreateInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_DEPTH_STENCIL_STATE_CREATE_INFO;
  5451. depthStencilStateCreateInfo.pNext = NULL;
  5452. depthStencilStateCreateInfo.flags = 0;
  5453. depthStencilStateCreateInfo.depthTestEnable =
  5454. createinfo->depth_stencil_state.enable_depth_test;
  5455. depthStencilStateCreateInfo.depthWriteEnable =
  5456. createinfo->depth_stencil_state.enable_depth_write;
  5457. depthStencilStateCreateInfo.depthCompareOp = SDLToVK_CompareOp[createinfo->depth_stencil_state.compare_op];
  5458. depthStencilStateCreateInfo.depthBoundsTestEnable = VK_FALSE;
  5459. depthStencilStateCreateInfo.stencilTestEnable =
  5460. createinfo->depth_stencil_state.enable_stencil_test;
  5461. depthStencilStateCreateInfo.front = frontStencilState;
  5462. depthStencilStateCreateInfo.back = backStencilState;
  5463. depthStencilStateCreateInfo.minDepthBounds = 0; // unused
  5464. depthStencilStateCreateInfo.maxDepthBounds = 0; // unused
  5465. // Color Blend
  5466. for (i = 0; i < createinfo->target_info.num_color_targets; i += 1) {
  5467. SDL_GPUColorTargetBlendState blendState = createinfo->target_info.color_target_descriptions[i].blend_state;
  5468. SDL_GPUColorComponentFlags colorWriteMask = blendState.enable_color_write_mask ?
  5469. blendState.color_write_mask :
  5470. 0xF;
  5471. colorBlendAttachmentStates[i].blendEnable =
  5472. blendState.enable_blend;
  5473. colorBlendAttachmentStates[i].srcColorBlendFactor = SDLToVK_BlendFactor[blendState.src_color_blendfactor];
  5474. colorBlendAttachmentStates[i].dstColorBlendFactor = SDLToVK_BlendFactor[blendState.dst_color_blendfactor];
  5475. colorBlendAttachmentStates[i].colorBlendOp = SDLToVK_BlendOp[blendState.color_blend_op];
  5476. colorBlendAttachmentStates[i].srcAlphaBlendFactor = SDLToVK_BlendFactor[blendState.src_alpha_blendfactor];
  5477. colorBlendAttachmentStates[i].dstAlphaBlendFactor = SDLToVK_BlendFactor[blendState.dst_alpha_blendfactor];
  5478. colorBlendAttachmentStates[i].alphaBlendOp = SDLToVK_BlendOp[blendState.alpha_blend_op];
  5479. colorBlendAttachmentStates[i].colorWriteMask =
  5480. colorWriteMask;
  5481. }
  5482. colorBlendStateCreateInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO;
  5483. colorBlendStateCreateInfo.pNext = NULL;
  5484. colorBlendStateCreateInfo.flags = 0;
  5485. colorBlendStateCreateInfo.attachmentCount =
  5486. createinfo->target_info.num_color_targets;
  5487. colorBlendStateCreateInfo.pAttachments =
  5488. colorBlendAttachmentStates;
  5489. colorBlendStateCreateInfo.blendConstants[0] = 1.0f;
  5490. colorBlendStateCreateInfo.blendConstants[1] = 1.0f;
  5491. colorBlendStateCreateInfo.blendConstants[2] = 1.0f;
  5492. colorBlendStateCreateInfo.blendConstants[3] = 1.0f;
  5493. // We don't support LogicOp, so this is easy.
  5494. colorBlendStateCreateInfo.logicOpEnable = VK_FALSE;
  5495. colorBlendStateCreateInfo.logicOp = 0;
  5496. // Pipeline Layout
  5497. graphicsPipeline->resourceLayout =
  5498. VULKAN_INTERNAL_FetchGraphicsPipelineResourceLayout(
  5499. renderer,
  5500. graphicsPipeline->vertexShader,
  5501. graphicsPipeline->fragmentShader);
  5502. if (graphicsPipeline->resourceLayout == NULL) {
  5503. SDL_stack_free(vertexInputBindingDescriptions);
  5504. SDL_stack_free(vertexInputAttributeDescriptions);
  5505. SDL_stack_free(colorBlendAttachmentStates);
  5506. SDL_free(graphicsPipeline);
  5507. SET_STRING_ERROR_AND_RETURN("Failed to initialize pipeline resource layout!", NULL);
  5508. }
  5509. // Pipeline
  5510. vkPipelineCreateInfo.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
  5511. vkPipelineCreateInfo.pNext = NULL;
  5512. vkPipelineCreateInfo.flags = 0;
  5513. vkPipelineCreateInfo.stageCount = 2;
  5514. vkPipelineCreateInfo.pStages = shaderStageCreateInfos;
  5515. vkPipelineCreateInfo.pVertexInputState = &vertexInputStateCreateInfo;
  5516. vkPipelineCreateInfo.pInputAssemblyState = &inputAssemblyStateCreateInfo;
  5517. vkPipelineCreateInfo.pTessellationState = VK_NULL_HANDLE;
  5518. vkPipelineCreateInfo.pViewportState = &viewportStateCreateInfo;
  5519. vkPipelineCreateInfo.pRasterizationState = &rasterizationStateCreateInfo;
  5520. vkPipelineCreateInfo.pMultisampleState = &multisampleStateCreateInfo;
  5521. vkPipelineCreateInfo.pDepthStencilState = &depthStencilStateCreateInfo;
  5522. vkPipelineCreateInfo.pColorBlendState = &colorBlendStateCreateInfo;
  5523. vkPipelineCreateInfo.pDynamicState = &dynamicStateCreateInfo;
  5524. vkPipelineCreateInfo.layout = graphicsPipeline->resourceLayout->pipelineLayout;
  5525. vkPipelineCreateInfo.renderPass = transientRenderPass;
  5526. vkPipelineCreateInfo.subpass = 0;
  5527. vkPipelineCreateInfo.basePipelineHandle = VK_NULL_HANDLE;
  5528. vkPipelineCreateInfo.basePipelineIndex = 0;
  5529. // TODO: enable pipeline caching
  5530. vulkanResult = renderer->vkCreateGraphicsPipelines(
  5531. renderer->logicalDevice,
  5532. VK_NULL_HANDLE,
  5533. 1,
  5534. &vkPipelineCreateInfo,
  5535. NULL,
  5536. &graphicsPipeline->pipeline);
  5537. SDL_stack_free(vertexInputBindingDescriptions);
  5538. SDL_stack_free(vertexInputAttributeDescriptions);
  5539. SDL_stack_free(colorBlendAttachmentStates);
  5540. renderer->vkDestroyRenderPass(
  5541. renderer->logicalDevice,
  5542. transientRenderPass,
  5543. NULL);
  5544. if (vulkanResult != VK_SUCCESS) {
  5545. SDL_free(graphicsPipeline);
  5546. CHECK_VULKAN_ERROR_AND_RETURN(vulkanResult, vkCreateGraphicsPipelines, NULL);
  5547. }
  5548. SDL_SetAtomicInt(&graphicsPipeline->referenceCount, 0);
  5549. if (renderer->debugMode && renderer->supportsDebugUtils && SDL_HasProperty(createinfo->props, SDL_PROP_GPU_GRAPHICSPIPELINE_CREATE_NAME_STRING)) {
  5550. VkDebugUtilsObjectNameInfoEXT nameInfo;
  5551. nameInfo.sType = VK_STRUCTURE_TYPE_DEBUG_UTILS_OBJECT_NAME_INFO_EXT;
  5552. nameInfo.pNext = NULL;
  5553. nameInfo.pObjectName = SDL_GetStringProperty(createinfo->props, SDL_PROP_GPU_GRAPHICSPIPELINE_CREATE_NAME_STRING, NULL);
  5554. nameInfo.objectType = VK_OBJECT_TYPE_PIPELINE;
  5555. nameInfo.objectHandle = (uint64_t)graphicsPipeline->pipeline;
  5556. renderer->vkSetDebugUtilsObjectNameEXT(
  5557. renderer->logicalDevice,
  5558. &nameInfo);
  5559. }
  5560. // Put this data in the pipeline we can do validation in gpu.c
  5561. graphicsPipeline->header.num_vertex_samplers = graphicsPipeline->resourceLayout->vertexSamplerCount;
  5562. graphicsPipeline->header.num_vertex_storage_buffers = graphicsPipeline->resourceLayout->vertexStorageBufferCount;
  5563. graphicsPipeline->header.num_vertex_storage_textures = graphicsPipeline->resourceLayout->vertexStorageTextureCount;
  5564. graphicsPipeline->header.num_vertex_uniform_buffers = graphicsPipeline->resourceLayout->vertexUniformBufferCount;
  5565. graphicsPipeline->header.num_fragment_samplers = graphicsPipeline->resourceLayout->fragmentSamplerCount;
  5566. graphicsPipeline->header.num_fragment_storage_buffers = graphicsPipeline->resourceLayout->fragmentStorageBufferCount;
  5567. graphicsPipeline->header.num_fragment_storage_textures = graphicsPipeline->resourceLayout->fragmentStorageTextureCount;
  5568. graphicsPipeline->header.num_fragment_uniform_buffers = graphicsPipeline->resourceLayout->fragmentUniformBufferCount;
  5569. return (SDL_GPUGraphicsPipeline *)graphicsPipeline;
  5570. }
  5571. static bool VULKAN_INTERNAL_IsValidShaderBytecode(
  5572. const Uint8 *code,
  5573. size_t codeSize)
  5574. {
  5575. // SPIR-V bytecode has a 4 byte header containing 0x07230203. SPIR-V is
  5576. // defined as a stream of words and not a stream of bytes so both byte
  5577. // orders need to be considered.
  5578. //
  5579. // FIXME: It is uncertain if drivers are able to load both byte orders. If
  5580. // needed we may need to do an optional swizzle internally so apps can
  5581. // continue to treat shader code as an opaque blob.
  5582. if (codeSize < 4 || code == NULL) {
  5583. return false;
  5584. }
  5585. const Uint32 magic = 0x07230203;
  5586. const Uint32 magicInv = 0x03022307;
  5587. return SDL_memcmp(code, &magic, 4) == 0 || SDL_memcmp(code, &magicInv, 4) == 0;
  5588. }
  5589. static SDL_GPUComputePipeline *VULKAN_CreateComputePipeline(
  5590. SDL_GPURenderer *driverData,
  5591. const SDL_GPUComputePipelineCreateInfo *createinfo)
  5592. {
  5593. VkShaderModuleCreateInfo shaderModuleCreateInfo;
  5594. VkComputePipelineCreateInfo vkShaderCreateInfo;
  5595. VkPipelineShaderStageCreateInfo pipelineShaderStageCreateInfo;
  5596. VkResult vulkanResult;
  5597. VulkanRenderer *renderer = (VulkanRenderer *)driverData;
  5598. VulkanComputePipeline *vulkanComputePipeline;
  5599. if (createinfo->format != SDL_GPU_SHADERFORMAT_SPIRV) {
  5600. SET_STRING_ERROR_AND_RETURN("Incompatible shader format for Vulkan!", NULL);
  5601. }
  5602. if (!VULKAN_INTERNAL_IsValidShaderBytecode(createinfo->code, createinfo->code_size)) {
  5603. SET_STRING_ERROR_AND_RETURN("The provided shader code is not valid SPIR-V!", NULL);
  5604. }
  5605. vulkanComputePipeline = SDL_malloc(sizeof(VulkanComputePipeline));
  5606. shaderModuleCreateInfo.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO;
  5607. shaderModuleCreateInfo.pNext = NULL;
  5608. shaderModuleCreateInfo.flags = 0;
  5609. shaderModuleCreateInfo.codeSize = createinfo->code_size;
  5610. shaderModuleCreateInfo.pCode = (Uint32 *)createinfo->code;
  5611. vulkanResult = renderer->vkCreateShaderModule(
  5612. renderer->logicalDevice,
  5613. &shaderModuleCreateInfo,
  5614. NULL,
  5615. &vulkanComputePipeline->shaderModule);
  5616. if (vulkanResult != VK_SUCCESS) {
  5617. SDL_free(vulkanComputePipeline);
  5618. CHECK_VULKAN_ERROR_AND_RETURN(vulkanResult, vkCreateShaderModule, NULL);
  5619. }
  5620. pipelineShaderStageCreateInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
  5621. pipelineShaderStageCreateInfo.pNext = NULL;
  5622. pipelineShaderStageCreateInfo.flags = 0;
  5623. pipelineShaderStageCreateInfo.stage = VK_SHADER_STAGE_COMPUTE_BIT;
  5624. pipelineShaderStageCreateInfo.module = vulkanComputePipeline->shaderModule;
  5625. pipelineShaderStageCreateInfo.pName = createinfo->entrypoint;
  5626. pipelineShaderStageCreateInfo.pSpecializationInfo = NULL;
  5627. vulkanComputePipeline->resourceLayout = VULKAN_INTERNAL_FetchComputePipelineResourceLayout(
  5628. renderer,
  5629. createinfo);
  5630. if (vulkanComputePipeline->resourceLayout == NULL) {
  5631. renderer->vkDestroyShaderModule(
  5632. renderer->logicalDevice,
  5633. vulkanComputePipeline->shaderModule,
  5634. NULL);
  5635. SDL_free(vulkanComputePipeline);
  5636. return NULL;
  5637. }
  5638. vkShaderCreateInfo.sType = VK_STRUCTURE_TYPE_COMPUTE_PIPELINE_CREATE_INFO;
  5639. vkShaderCreateInfo.pNext = NULL;
  5640. vkShaderCreateInfo.flags = 0;
  5641. vkShaderCreateInfo.stage = pipelineShaderStageCreateInfo;
  5642. vkShaderCreateInfo.layout = vulkanComputePipeline->resourceLayout->pipelineLayout;
  5643. vkShaderCreateInfo.basePipelineHandle = (VkPipeline)VK_NULL_HANDLE;
  5644. vkShaderCreateInfo.basePipelineIndex = 0;
  5645. vulkanResult = renderer->vkCreateComputePipelines(
  5646. renderer->logicalDevice,
  5647. (VkPipelineCache)VK_NULL_HANDLE,
  5648. 1,
  5649. &vkShaderCreateInfo,
  5650. NULL,
  5651. &vulkanComputePipeline->pipeline);
  5652. if (vulkanResult != VK_SUCCESS) {
  5653. VULKAN_INTERNAL_DestroyComputePipeline(renderer, vulkanComputePipeline);
  5654. CHECK_VULKAN_ERROR_AND_RETURN(vulkanResult, vkCreateComputePipeline, NULL);
  5655. return NULL;
  5656. }
  5657. SDL_SetAtomicInt(&vulkanComputePipeline->referenceCount, 0);
  5658. if (renderer->debugMode && renderer->supportsDebugUtils && SDL_HasProperty(createinfo->props, SDL_PROP_GPU_COMPUTEPIPELINE_CREATE_NAME_STRING)) {
  5659. VkDebugUtilsObjectNameInfoEXT nameInfo;
  5660. nameInfo.sType = VK_STRUCTURE_TYPE_DEBUG_UTILS_OBJECT_NAME_INFO_EXT;
  5661. nameInfo.pNext = NULL;
  5662. nameInfo.pObjectName = SDL_GetStringProperty(createinfo->props, SDL_PROP_GPU_COMPUTEPIPELINE_CREATE_NAME_STRING, NULL);
  5663. nameInfo.objectType = VK_OBJECT_TYPE_PIPELINE;
  5664. nameInfo.objectHandle = (uint64_t)vulkanComputePipeline->pipeline;
  5665. renderer->vkSetDebugUtilsObjectNameEXT(
  5666. renderer->logicalDevice,
  5667. &nameInfo);
  5668. }
  5669. // Track these here for debug layer
  5670. vulkanComputePipeline->header.numSamplers = vulkanComputePipeline->resourceLayout->numSamplers;
  5671. vulkanComputePipeline->header.numReadonlyStorageTextures = vulkanComputePipeline->resourceLayout->numReadonlyStorageTextures;
  5672. vulkanComputePipeline->header.numReadonlyStorageBuffers = vulkanComputePipeline->resourceLayout->numReadonlyStorageBuffers;
  5673. vulkanComputePipeline->header.numReadWriteStorageTextures = vulkanComputePipeline->resourceLayout->numReadWriteStorageTextures;
  5674. vulkanComputePipeline->header.numReadWriteStorageBuffers = vulkanComputePipeline->resourceLayout->numReadWriteStorageBuffers;
  5675. vulkanComputePipeline->header.numUniformBuffers = vulkanComputePipeline->resourceLayout->numUniformBuffers;
  5676. return (SDL_GPUComputePipeline *)vulkanComputePipeline;
  5677. }
  5678. static SDL_GPUSampler *VULKAN_CreateSampler(
  5679. SDL_GPURenderer *driverData,
  5680. const SDL_GPUSamplerCreateInfo *createinfo)
  5681. {
  5682. VulkanRenderer *renderer = (VulkanRenderer *)driverData;
  5683. VulkanSampler *vulkanSampler = SDL_malloc(sizeof(VulkanSampler));
  5684. VkResult vulkanResult;
  5685. VkSamplerCreateInfo vkSamplerCreateInfo;
  5686. vkSamplerCreateInfo.sType = VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO;
  5687. vkSamplerCreateInfo.pNext = NULL;
  5688. vkSamplerCreateInfo.flags = 0;
  5689. vkSamplerCreateInfo.magFilter = SDLToVK_Filter[createinfo->mag_filter];
  5690. vkSamplerCreateInfo.minFilter = SDLToVK_Filter[createinfo->min_filter];
  5691. vkSamplerCreateInfo.mipmapMode = SDLToVK_SamplerMipmapMode[createinfo->mipmap_mode];
  5692. vkSamplerCreateInfo.addressModeU = SDLToVK_SamplerAddressMode[createinfo->address_mode_u];
  5693. vkSamplerCreateInfo.addressModeV = SDLToVK_SamplerAddressMode[createinfo->address_mode_v];
  5694. vkSamplerCreateInfo.addressModeW = SDLToVK_SamplerAddressMode[createinfo->address_mode_w];
  5695. vkSamplerCreateInfo.mipLodBias = createinfo->mip_lod_bias;
  5696. vkSamplerCreateInfo.anisotropyEnable = createinfo->enable_anisotropy;
  5697. vkSamplerCreateInfo.maxAnisotropy = createinfo->max_anisotropy;
  5698. vkSamplerCreateInfo.compareEnable = createinfo->enable_compare;
  5699. vkSamplerCreateInfo.compareOp = SDLToVK_CompareOp[createinfo->compare_op];
  5700. vkSamplerCreateInfo.minLod = createinfo->min_lod;
  5701. vkSamplerCreateInfo.maxLod = createinfo->max_lod;
  5702. vkSamplerCreateInfo.borderColor = VK_BORDER_COLOR_FLOAT_TRANSPARENT_BLACK; // arbitrary, unused
  5703. vkSamplerCreateInfo.unnormalizedCoordinates = VK_FALSE;
  5704. vulkanResult = renderer->vkCreateSampler(
  5705. renderer->logicalDevice,
  5706. &vkSamplerCreateInfo,
  5707. NULL,
  5708. &vulkanSampler->sampler);
  5709. if (vulkanResult != VK_SUCCESS) {
  5710. SDL_free(vulkanSampler);
  5711. CHECK_VULKAN_ERROR_AND_RETURN(vulkanResult, vkCreateSampler, NULL);
  5712. }
  5713. SDL_SetAtomicInt(&vulkanSampler->referenceCount, 0);
  5714. if (renderer->debugMode && renderer->supportsDebugUtils && SDL_HasProperty(createinfo->props, SDL_PROP_GPU_SAMPLER_CREATE_NAME_STRING)) {
  5715. VkDebugUtilsObjectNameInfoEXT nameInfo;
  5716. nameInfo.sType = VK_STRUCTURE_TYPE_DEBUG_UTILS_OBJECT_NAME_INFO_EXT;
  5717. nameInfo.pNext = NULL;
  5718. nameInfo.pObjectName = SDL_GetStringProperty(createinfo->props, SDL_PROP_GPU_SAMPLER_CREATE_NAME_STRING, NULL);
  5719. nameInfo.objectType = VK_OBJECT_TYPE_SAMPLER;
  5720. nameInfo.objectHandle = (uint64_t)vulkanSampler->sampler;
  5721. renderer->vkSetDebugUtilsObjectNameEXT(
  5722. renderer->logicalDevice,
  5723. &nameInfo);
  5724. }
  5725. return (SDL_GPUSampler *)vulkanSampler;
  5726. }
  5727. static SDL_GPUShader *VULKAN_CreateShader(
  5728. SDL_GPURenderer *driverData,
  5729. const SDL_GPUShaderCreateInfo *createinfo)
  5730. {
  5731. VulkanShader *vulkanShader;
  5732. VkResult vulkanResult;
  5733. VkShaderModuleCreateInfo vkShaderModuleCreateInfo;
  5734. VulkanRenderer *renderer = (VulkanRenderer *)driverData;
  5735. if (!VULKAN_INTERNAL_IsValidShaderBytecode(createinfo->code, createinfo->code_size)) {
  5736. SET_STRING_ERROR_AND_RETURN("The provided shader code is not valid SPIR-V!", NULL);
  5737. }
  5738. vulkanShader = SDL_malloc(sizeof(VulkanShader));
  5739. vkShaderModuleCreateInfo.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO;
  5740. vkShaderModuleCreateInfo.pNext = NULL;
  5741. vkShaderModuleCreateInfo.flags = 0;
  5742. vkShaderModuleCreateInfo.codeSize = createinfo->code_size;
  5743. vkShaderModuleCreateInfo.pCode = (Uint32 *)createinfo->code;
  5744. vulkanResult = renderer->vkCreateShaderModule(
  5745. renderer->logicalDevice,
  5746. &vkShaderModuleCreateInfo,
  5747. NULL,
  5748. &vulkanShader->shaderModule);
  5749. if (vulkanResult != VK_SUCCESS) {
  5750. SDL_free(vulkanShader);
  5751. CHECK_VULKAN_ERROR_AND_RETURN(vulkanResult, vkCreateShaderModule, NULL);
  5752. }
  5753. const char *entrypoint = createinfo->entrypoint;
  5754. if (!entrypoint) {
  5755. entrypoint = "main";
  5756. }
  5757. vulkanShader->entrypointName = SDL_strdup(entrypoint);
  5758. vulkanShader->stage = createinfo->stage;
  5759. vulkanShader->numSamplers = createinfo->num_samplers;
  5760. vulkanShader->numStorageTextures = createinfo->num_storage_textures;
  5761. vulkanShader->numStorageBuffers = createinfo->num_storage_buffers;
  5762. vulkanShader->numUniformBuffers = createinfo->num_uniform_buffers;
  5763. SDL_SetAtomicInt(&vulkanShader->referenceCount, 0);
  5764. if (renderer->debugMode && SDL_HasProperty(createinfo->props, SDL_PROP_GPU_SHADER_CREATE_NAME_STRING)) {
  5765. VkDebugUtilsObjectNameInfoEXT nameInfo;
  5766. nameInfo.sType = VK_STRUCTURE_TYPE_DEBUG_UTILS_OBJECT_NAME_INFO_EXT;
  5767. nameInfo.pNext = NULL;
  5768. nameInfo.pObjectName = SDL_GetStringProperty(createinfo->props, SDL_PROP_GPU_SHADER_CREATE_NAME_STRING, NULL);
  5769. nameInfo.objectType = VK_OBJECT_TYPE_SHADER_MODULE;
  5770. nameInfo.objectHandle = (uint64_t)vulkanShader->shaderModule;
  5771. renderer->vkSetDebugUtilsObjectNameEXT(
  5772. renderer->logicalDevice,
  5773. &nameInfo);
  5774. }
  5775. return (SDL_GPUShader *)vulkanShader;
  5776. }
  5777. static bool VULKAN_SupportsSampleCount(
  5778. SDL_GPURenderer *driverData,
  5779. SDL_GPUTextureFormat format,
  5780. SDL_GPUSampleCount sampleCount)
  5781. {
  5782. VulkanRenderer *renderer = (VulkanRenderer *)driverData;
  5783. VkSampleCountFlags bits = IsDepthFormat(format) ? renderer->physicalDeviceProperties.properties.limits.framebufferDepthSampleCounts : renderer->physicalDeviceProperties.properties.limits.framebufferColorSampleCounts;
  5784. VkSampleCountFlagBits vkSampleCount = SDLToVK_SampleCount[sampleCount];
  5785. return !!(bits & vkSampleCount);
  5786. }
  5787. static SDL_GPUTexture *VULKAN_CreateTexture(
  5788. SDL_GPURenderer *driverData,
  5789. const SDL_GPUTextureCreateInfo *createinfo)
  5790. {
  5791. VulkanRenderer *renderer = (VulkanRenderer *)driverData;
  5792. VulkanTexture *texture;
  5793. VulkanTextureContainer *container;
  5794. texture = VULKAN_INTERNAL_CreateTexture(
  5795. renderer,
  5796. true,
  5797. createinfo);
  5798. if (texture == NULL) {
  5799. return NULL;
  5800. }
  5801. container = SDL_malloc(sizeof(VulkanTextureContainer));
  5802. // Copy properties so we don't lose information when the client destroys them
  5803. container->header.info = *createinfo;
  5804. container->header.info.props = SDL_CreateProperties();
  5805. if (createinfo->props) {
  5806. SDL_CopyProperties(createinfo->props, container->header.info.props);
  5807. }
  5808. container->canBeCycled = true;
  5809. container->activeTexture = texture;
  5810. container->textureCapacity = 1;
  5811. container->textureCount = 1;
  5812. container->textures = SDL_malloc(
  5813. container->textureCapacity * sizeof(VulkanTexture *));
  5814. container->textures[0] = container->activeTexture;
  5815. container->debugName = NULL;
  5816. if (SDL_HasProperty(createinfo->props, SDL_PROP_GPU_TEXTURE_CREATE_NAME_STRING)) {
  5817. container->debugName = SDL_strdup(SDL_GetStringProperty(createinfo->props, SDL_PROP_GPU_TEXTURE_CREATE_NAME_STRING, NULL));
  5818. }
  5819. texture->container = container;
  5820. texture->containerIndex = 0;
  5821. return (SDL_GPUTexture *)container;
  5822. }
  5823. static SDL_GPUBuffer *VULKAN_CreateBuffer(
  5824. SDL_GPURenderer *driverData,
  5825. SDL_GPUBufferUsageFlags usageFlags,
  5826. Uint32 size,
  5827. const char *debugName)
  5828. {
  5829. return (SDL_GPUBuffer *)VULKAN_INTERNAL_CreateBufferContainer(
  5830. (VulkanRenderer *)driverData,
  5831. (VkDeviceSize)size,
  5832. usageFlags,
  5833. VULKAN_BUFFER_TYPE_GPU,
  5834. false,
  5835. debugName);
  5836. }
  5837. static VulkanUniformBuffer *VULKAN_INTERNAL_CreateUniformBuffer(
  5838. VulkanRenderer *renderer,
  5839. Uint32 size)
  5840. {
  5841. VulkanUniformBuffer *uniformBuffer = SDL_calloc(1, sizeof(VulkanUniformBuffer));
  5842. uniformBuffer->buffer = VULKAN_INTERNAL_CreateBuffer(
  5843. renderer,
  5844. (VkDeviceSize)size,
  5845. 0,
  5846. VULKAN_BUFFER_TYPE_UNIFORM,
  5847. false,
  5848. NULL);
  5849. uniformBuffer->drawOffset = 0;
  5850. uniformBuffer->writeOffset = 0;
  5851. uniformBuffer->buffer->uniformBufferForDefrag = uniformBuffer;
  5852. return uniformBuffer;
  5853. }
  5854. static SDL_GPUTransferBuffer *VULKAN_CreateTransferBuffer(
  5855. SDL_GPURenderer *driverData,
  5856. SDL_GPUTransferBufferUsage usage,
  5857. Uint32 size,
  5858. const char *debugName)
  5859. {
  5860. return (SDL_GPUTransferBuffer *)VULKAN_INTERNAL_CreateBufferContainer(
  5861. (VulkanRenderer *)driverData,
  5862. (VkDeviceSize)size,
  5863. 0,
  5864. VULKAN_BUFFER_TYPE_TRANSFER,
  5865. true, // Dedicated allocations preserve the data even if a defrag is triggered.
  5866. debugName);
  5867. }
  5868. static void VULKAN_INTERNAL_ReleaseTexture(
  5869. VulkanRenderer *renderer,
  5870. VulkanTexture *vulkanTexture)
  5871. {
  5872. if (vulkanTexture->markedForDestroy) {
  5873. return;
  5874. }
  5875. SDL_LockMutex(renderer->disposeLock);
  5876. EXPAND_ARRAY_IF_NEEDED(
  5877. renderer->texturesToDestroy,
  5878. VulkanTexture *,
  5879. renderer->texturesToDestroyCount + 1,
  5880. renderer->texturesToDestroyCapacity,
  5881. renderer->texturesToDestroyCapacity * 2);
  5882. renderer->texturesToDestroy[renderer->texturesToDestroyCount] = vulkanTexture;
  5883. renderer->texturesToDestroyCount += 1;
  5884. vulkanTexture->markedForDestroy = true;
  5885. SDL_UnlockMutex(renderer->disposeLock);
  5886. }
  5887. static void VULKAN_ReleaseTexture(
  5888. SDL_GPURenderer *driverData,
  5889. SDL_GPUTexture *texture)
  5890. {
  5891. VulkanRenderer *renderer = (VulkanRenderer *)driverData;
  5892. VulkanTextureContainer *vulkanTextureContainer = (VulkanTextureContainer *)texture;
  5893. Uint32 i;
  5894. SDL_LockMutex(renderer->disposeLock);
  5895. for (i = 0; i < vulkanTextureContainer->textureCount; i += 1) {
  5896. VULKAN_INTERNAL_ReleaseTexture(renderer, vulkanTextureContainer->textures[i]);
  5897. }
  5898. SDL_DestroyProperties(vulkanTextureContainer->header.info.props);
  5899. // Containers are just client handles, so we can destroy immediately
  5900. SDL_free(vulkanTextureContainer->debugName);
  5901. SDL_free(vulkanTextureContainer->textures);
  5902. SDL_free(vulkanTextureContainer);
  5903. SDL_UnlockMutex(renderer->disposeLock);
  5904. }
  5905. static void VULKAN_ReleaseSampler(
  5906. SDL_GPURenderer *driverData,
  5907. SDL_GPUSampler *sampler)
  5908. {
  5909. VulkanRenderer *renderer = (VulkanRenderer *)driverData;
  5910. VulkanSampler *vulkanSampler = (VulkanSampler *)sampler;
  5911. SDL_LockMutex(renderer->disposeLock);
  5912. EXPAND_ARRAY_IF_NEEDED(
  5913. renderer->samplersToDestroy,
  5914. VulkanSampler *,
  5915. renderer->samplersToDestroyCount + 1,
  5916. renderer->samplersToDestroyCapacity,
  5917. renderer->samplersToDestroyCapacity * 2);
  5918. renderer->samplersToDestroy[renderer->samplersToDestroyCount] = vulkanSampler;
  5919. renderer->samplersToDestroyCount += 1;
  5920. SDL_UnlockMutex(renderer->disposeLock);
  5921. }
  5922. static void VULKAN_INTERNAL_ReleaseBuffer(
  5923. VulkanRenderer *renderer,
  5924. VulkanBuffer *vulkanBuffer)
  5925. {
  5926. if (vulkanBuffer->markedForDestroy) {
  5927. return;
  5928. }
  5929. SDL_LockMutex(renderer->disposeLock);
  5930. EXPAND_ARRAY_IF_NEEDED(
  5931. renderer->buffersToDestroy,
  5932. VulkanBuffer *,
  5933. renderer->buffersToDestroyCount + 1,
  5934. renderer->buffersToDestroyCapacity,
  5935. renderer->buffersToDestroyCapacity * 2);
  5936. renderer->buffersToDestroy[renderer->buffersToDestroyCount] = vulkanBuffer;
  5937. renderer->buffersToDestroyCount += 1;
  5938. vulkanBuffer->markedForDestroy = true;
  5939. vulkanBuffer->container = NULL;
  5940. SDL_UnlockMutex(renderer->disposeLock);
  5941. }
  5942. static void VULKAN_INTERNAL_ReleaseBufferContainer(
  5943. VulkanRenderer *renderer,
  5944. VulkanBufferContainer *bufferContainer)
  5945. {
  5946. Uint32 i;
  5947. SDL_LockMutex(renderer->disposeLock);
  5948. for (i = 0; i < bufferContainer->bufferCount; i += 1) {
  5949. VULKAN_INTERNAL_ReleaseBuffer(renderer, bufferContainer->buffers[i]);
  5950. }
  5951. // Containers are just client handles, so we can free immediately
  5952. if (bufferContainer->debugName != NULL) {
  5953. SDL_free(bufferContainer->debugName);
  5954. bufferContainer->debugName = NULL;
  5955. }
  5956. SDL_free(bufferContainer->buffers);
  5957. SDL_free(bufferContainer);
  5958. SDL_UnlockMutex(renderer->disposeLock);
  5959. }
  5960. static void VULKAN_ReleaseBuffer(
  5961. SDL_GPURenderer *driverData,
  5962. SDL_GPUBuffer *buffer)
  5963. {
  5964. VulkanRenderer *renderer = (VulkanRenderer *)driverData;
  5965. VulkanBufferContainer *vulkanBufferContainer = (VulkanBufferContainer *)buffer;
  5966. VULKAN_INTERNAL_ReleaseBufferContainer(
  5967. renderer,
  5968. vulkanBufferContainer);
  5969. }
  5970. static void VULKAN_ReleaseTransferBuffer(
  5971. SDL_GPURenderer *driverData,
  5972. SDL_GPUTransferBuffer *transferBuffer)
  5973. {
  5974. VulkanRenderer *renderer = (VulkanRenderer *)driverData;
  5975. VulkanBufferContainer *transferBufferContainer = (VulkanBufferContainer *)transferBuffer;
  5976. VULKAN_INTERNAL_ReleaseBufferContainer(
  5977. renderer,
  5978. transferBufferContainer);
  5979. }
  5980. static void VULKAN_ReleaseShader(
  5981. SDL_GPURenderer *driverData,
  5982. SDL_GPUShader *shader)
  5983. {
  5984. VulkanRenderer *renderer = (VulkanRenderer *)driverData;
  5985. VulkanShader *vulkanShader = (VulkanShader *)shader;
  5986. SDL_LockMutex(renderer->disposeLock);
  5987. EXPAND_ARRAY_IF_NEEDED(
  5988. renderer->shadersToDestroy,
  5989. VulkanShader *,
  5990. renderer->shadersToDestroyCount + 1,
  5991. renderer->shadersToDestroyCapacity,
  5992. renderer->shadersToDestroyCapacity * 2);
  5993. renderer->shadersToDestroy[renderer->shadersToDestroyCount] = vulkanShader;
  5994. renderer->shadersToDestroyCount += 1;
  5995. SDL_UnlockMutex(renderer->disposeLock);
  5996. }
  5997. static void VULKAN_ReleaseComputePipeline(
  5998. SDL_GPURenderer *driverData,
  5999. SDL_GPUComputePipeline *computePipeline)
  6000. {
  6001. VulkanRenderer *renderer = (VulkanRenderer *)driverData;
  6002. VulkanComputePipeline *vulkanComputePipeline = (VulkanComputePipeline *)computePipeline;
  6003. SDL_LockMutex(renderer->disposeLock);
  6004. EXPAND_ARRAY_IF_NEEDED(
  6005. renderer->computePipelinesToDestroy,
  6006. VulkanComputePipeline *,
  6007. renderer->computePipelinesToDestroyCount + 1,
  6008. renderer->computePipelinesToDestroyCapacity,
  6009. renderer->computePipelinesToDestroyCapacity * 2);
  6010. renderer->computePipelinesToDestroy[renderer->computePipelinesToDestroyCount] = vulkanComputePipeline;
  6011. renderer->computePipelinesToDestroyCount += 1;
  6012. SDL_UnlockMutex(renderer->disposeLock);
  6013. }
  6014. static void VULKAN_ReleaseGraphicsPipeline(
  6015. SDL_GPURenderer *driverData,
  6016. SDL_GPUGraphicsPipeline *graphicsPipeline)
  6017. {
  6018. VulkanRenderer *renderer = (VulkanRenderer *)driverData;
  6019. VulkanGraphicsPipeline *vulkanGraphicsPipeline = (VulkanGraphicsPipeline *)graphicsPipeline;
  6020. SDL_LockMutex(renderer->disposeLock);
  6021. EXPAND_ARRAY_IF_NEEDED(
  6022. renderer->graphicsPipelinesToDestroy,
  6023. VulkanGraphicsPipeline *,
  6024. renderer->graphicsPipelinesToDestroyCount + 1,
  6025. renderer->graphicsPipelinesToDestroyCapacity,
  6026. renderer->graphicsPipelinesToDestroyCapacity * 2);
  6027. renderer->graphicsPipelinesToDestroy[renderer->graphicsPipelinesToDestroyCount] = vulkanGraphicsPipeline;
  6028. renderer->graphicsPipelinesToDestroyCount += 1;
  6029. SDL_UnlockMutex(renderer->disposeLock);
  6030. }
  6031. // Command Buffer render state
  6032. static VkRenderPass VULKAN_INTERNAL_FetchRenderPass(
  6033. VulkanRenderer *renderer,
  6034. const SDL_GPUColorTargetInfo *colorTargetInfos,
  6035. Uint32 numColorTargets,
  6036. const SDL_GPUDepthStencilTargetInfo *depthStencilTargetInfo)
  6037. {
  6038. VulkanRenderPassHashTableValue *renderPassWrapper = NULL;
  6039. VkRenderPass renderPassHandle;
  6040. RenderPassHashTableKey key;
  6041. Uint32 i;
  6042. SDL_zero(key);
  6043. for (i = 0; i < numColorTargets; i += 1) {
  6044. key.colorTargetDescriptions[i].format = SDLToVK_TextureFormat[((VulkanTextureContainer *)colorTargetInfos[i].texture)->header.info.format];
  6045. key.colorTargetDescriptions[i].loadOp = colorTargetInfos[i].load_op;
  6046. key.colorTargetDescriptions[i].storeOp = colorTargetInfos[i].store_op;
  6047. if (colorTargetInfos[i].resolve_texture != NULL) {
  6048. key.resolveTargetFormats[key.numResolveTargets] = SDLToVK_TextureFormat[((VulkanTextureContainer *)colorTargetInfos[i].resolve_texture)->header.info.format];
  6049. key.numResolveTargets += 1;
  6050. }
  6051. }
  6052. key.sampleCount = VK_SAMPLE_COUNT_1_BIT;
  6053. if (numColorTargets > 0) {
  6054. key.sampleCount = SDLToVK_SampleCount[((VulkanTextureContainer *)colorTargetInfos[0].texture)->header.info.sample_count];
  6055. }
  6056. key.numColorTargets = numColorTargets;
  6057. if (depthStencilTargetInfo == NULL) {
  6058. key.depthStencilTargetDescription.format = 0;
  6059. key.depthStencilTargetDescription.loadOp = SDL_GPU_LOADOP_DONT_CARE;
  6060. key.depthStencilTargetDescription.storeOp = SDL_GPU_STOREOP_DONT_CARE;
  6061. key.depthStencilTargetDescription.stencilLoadOp = SDL_GPU_LOADOP_DONT_CARE;
  6062. key.depthStencilTargetDescription.stencilStoreOp = SDL_GPU_STOREOP_DONT_CARE;
  6063. } else {
  6064. key.depthStencilTargetDescription.format = SDLToVK_TextureFormat[((VulkanTextureContainer *)depthStencilTargetInfo->texture)->header.info.format];
  6065. key.depthStencilTargetDescription.loadOp = depthStencilTargetInfo->load_op;
  6066. key.depthStencilTargetDescription.storeOp = depthStencilTargetInfo->store_op;
  6067. key.depthStencilTargetDescription.stencilLoadOp = depthStencilTargetInfo->stencil_load_op;
  6068. key.depthStencilTargetDescription.stencilStoreOp = depthStencilTargetInfo->stencil_store_op;
  6069. }
  6070. SDL_LockMutex(renderer->renderPassFetchLock);
  6071. bool result = SDL_FindInHashTable(
  6072. renderer->renderPassHashTable,
  6073. (const void *)&key,
  6074. (const void **)&renderPassWrapper);
  6075. if (result) {
  6076. SDL_UnlockMutex(renderer->renderPassFetchLock);
  6077. return renderPassWrapper->handle;
  6078. }
  6079. renderPassHandle = VULKAN_INTERNAL_CreateRenderPass(
  6080. renderer,
  6081. colorTargetInfos,
  6082. numColorTargets,
  6083. depthStencilTargetInfo);
  6084. if (renderPassHandle == VK_NULL_HANDLE) {
  6085. SDL_UnlockMutex(renderer->renderPassFetchLock);
  6086. return VK_NULL_HANDLE;
  6087. }
  6088. // Have to malloc the key to store it in the hashtable
  6089. RenderPassHashTableKey *allocedKey = SDL_malloc(sizeof(RenderPassHashTableKey));
  6090. SDL_memcpy(allocedKey, &key, sizeof(RenderPassHashTableKey));
  6091. renderPassWrapper = SDL_malloc(sizeof(VulkanRenderPassHashTableValue));
  6092. renderPassWrapper->handle = renderPassHandle;
  6093. SDL_InsertIntoHashTable(
  6094. renderer->renderPassHashTable,
  6095. (const void *)allocedKey,
  6096. (const void *)renderPassWrapper, true);
  6097. SDL_UnlockMutex(renderer->renderPassFetchLock);
  6098. return renderPassHandle;
  6099. }
  6100. static VulkanFramebuffer *VULKAN_INTERNAL_FetchFramebuffer(
  6101. VulkanRenderer *renderer,
  6102. VkRenderPass renderPass,
  6103. const SDL_GPUColorTargetInfo *colorTargetInfos,
  6104. Uint32 numColorTargets,
  6105. const SDL_GPUDepthStencilTargetInfo *depthStencilTargetInfo,
  6106. Uint32 width,
  6107. Uint32 height)
  6108. {
  6109. VulkanFramebuffer *vulkanFramebuffer = NULL;
  6110. VkFramebufferCreateInfo framebufferInfo;
  6111. VkResult result;
  6112. VkImageView imageViewAttachments[2 * MAX_COLOR_TARGET_BINDINGS + 1 /* depth */];
  6113. FramebufferHashTableKey key;
  6114. Uint32 attachmentCount = 0;
  6115. Uint32 i;
  6116. SDL_zero(imageViewAttachments);
  6117. SDL_zero(key);
  6118. key.numColorTargets = numColorTargets;
  6119. for (i = 0; i < numColorTargets; i += 1) {
  6120. VulkanTextureContainer *container = (VulkanTextureContainer *)colorTargetInfos[i].texture;
  6121. VulkanTextureSubresource *subresource = VULKAN_INTERNAL_FetchTextureSubresource(
  6122. container,
  6123. container->header.info.type == SDL_GPU_TEXTURETYPE_3D ? 0 : colorTargetInfos[i].layer_or_depth_plane,
  6124. colorTargetInfos[i].mip_level);
  6125. Uint32 rtvIndex =
  6126. container->header.info.type == SDL_GPU_TEXTURETYPE_3D ? colorTargetInfos[i].layer_or_depth_plane : 0;
  6127. key.colorAttachmentViews[i] = subresource->renderTargetViews[rtvIndex];
  6128. if (colorTargetInfos[i].resolve_texture != NULL) {
  6129. VulkanTextureContainer *resolveTextureContainer = (VulkanTextureContainer *)colorTargetInfos[i].resolve_texture;
  6130. VulkanTextureSubresource *resolveSubresource = VULKAN_INTERNAL_FetchTextureSubresource(
  6131. resolveTextureContainer,
  6132. colorTargetInfos[i].layer_or_depth_plane,
  6133. colorTargetInfos[i].mip_level);
  6134. key.resolveAttachmentViews[key.numResolveAttachments] = resolveSubresource->renderTargetViews[0];
  6135. key.numResolveAttachments += 1;
  6136. }
  6137. }
  6138. if (depthStencilTargetInfo == NULL) {
  6139. key.depthStencilAttachmentView = VK_NULL_HANDLE;
  6140. } else {
  6141. VulkanTextureSubresource *subresource = VULKAN_INTERNAL_FetchTextureSubresource(
  6142. (VulkanTextureContainer *)depthStencilTargetInfo->texture,
  6143. depthStencilTargetInfo->layer,
  6144. depthStencilTargetInfo->mip_level);
  6145. key.depthStencilAttachmentView = subresource->depthStencilView;
  6146. }
  6147. key.width = width;
  6148. key.height = height;
  6149. SDL_LockMutex(renderer->framebufferFetchLock);
  6150. bool findResult = SDL_FindInHashTable(
  6151. renderer->framebufferHashTable,
  6152. (const void *)&key,
  6153. (const void **)&vulkanFramebuffer);
  6154. if (findResult) {
  6155. SDL_UnlockMutex(renderer->framebufferFetchLock);
  6156. return vulkanFramebuffer;
  6157. }
  6158. vulkanFramebuffer = SDL_malloc(sizeof(VulkanFramebuffer));
  6159. SDL_SetAtomicInt(&vulkanFramebuffer->referenceCount, 0);
  6160. // Create a new framebuffer
  6161. for (i = 0; i < numColorTargets; i += 1) {
  6162. VulkanTextureContainer *container = (VulkanTextureContainer *)colorTargetInfos[i].texture;
  6163. VulkanTextureSubresource *subresource = VULKAN_INTERNAL_FetchTextureSubresource(
  6164. container,
  6165. container->header.info.type == SDL_GPU_TEXTURETYPE_3D ? 0 : colorTargetInfos[i].layer_or_depth_plane,
  6166. colorTargetInfos[i].mip_level);
  6167. Uint32 rtvIndex =
  6168. container->header.info.type == SDL_GPU_TEXTURETYPE_3D ? colorTargetInfos[i].layer_or_depth_plane : 0;
  6169. imageViewAttachments[attachmentCount] = subresource->renderTargetViews[rtvIndex];
  6170. attachmentCount += 1;
  6171. if (colorTargetInfos[i].store_op == SDL_GPU_STOREOP_RESOLVE || colorTargetInfos[i].store_op == SDL_GPU_STOREOP_RESOLVE_AND_STORE) {
  6172. VulkanTextureContainer *resolveContainer = (VulkanTextureContainer *)colorTargetInfos[i].resolve_texture;
  6173. VulkanTextureSubresource *resolveSubresource = VULKAN_INTERNAL_FetchTextureSubresource(
  6174. resolveContainer,
  6175. colorTargetInfos[i].resolve_layer,
  6176. colorTargetInfos[i].resolve_mip_level);
  6177. imageViewAttachments[attachmentCount] = resolveSubresource->renderTargetViews[0];
  6178. attachmentCount += 1;
  6179. }
  6180. }
  6181. if (depthStencilTargetInfo != NULL) {
  6182. VulkanTextureSubresource *subresource = VULKAN_INTERNAL_FetchTextureSubresource(
  6183. (VulkanTextureContainer *)depthStencilTargetInfo->texture,
  6184. depthStencilTargetInfo->layer,
  6185. depthStencilTargetInfo->mip_level);
  6186. imageViewAttachments[attachmentCount] = subresource->depthStencilView;
  6187. attachmentCount += 1;
  6188. }
  6189. framebufferInfo.sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO;
  6190. framebufferInfo.pNext = NULL;
  6191. framebufferInfo.flags = 0;
  6192. framebufferInfo.renderPass = renderPass;
  6193. framebufferInfo.attachmentCount = attachmentCount;
  6194. framebufferInfo.pAttachments = imageViewAttachments;
  6195. framebufferInfo.width = key.width;
  6196. framebufferInfo.height = key.height;
  6197. framebufferInfo.layers = 1;
  6198. result = renderer->vkCreateFramebuffer(
  6199. renderer->logicalDevice,
  6200. &framebufferInfo,
  6201. NULL,
  6202. &vulkanFramebuffer->framebuffer);
  6203. if (result == VK_SUCCESS) {
  6204. // Have to malloc the key to store it in the hashtable
  6205. FramebufferHashTableKey *allocedKey = SDL_malloc(sizeof(FramebufferHashTableKey));
  6206. SDL_memcpy(allocedKey, &key, sizeof(FramebufferHashTableKey));
  6207. SDL_InsertIntoHashTable(
  6208. renderer->framebufferHashTable,
  6209. (const void *)allocedKey,
  6210. (const void *)vulkanFramebuffer, true);
  6211. } else {
  6212. SDL_free(vulkanFramebuffer);
  6213. SDL_UnlockMutex(renderer->framebufferFetchLock);
  6214. CHECK_VULKAN_ERROR_AND_RETURN(result, vkCreateFramebuffer, NULL);
  6215. }
  6216. SDL_UnlockMutex(renderer->framebufferFetchLock);
  6217. return vulkanFramebuffer;
  6218. }
  6219. static void VULKAN_INTERNAL_SetCurrentViewport(
  6220. VulkanCommandBuffer *commandBuffer,
  6221. const SDL_GPUViewport *viewport)
  6222. {
  6223. VulkanCommandBuffer *vulkanCommandBuffer = commandBuffer;
  6224. VulkanRenderer *renderer = vulkanCommandBuffer->renderer;
  6225. vulkanCommandBuffer->currentViewport.x = viewport->x;
  6226. vulkanCommandBuffer->currentViewport.width = viewport->w;
  6227. vulkanCommandBuffer->currentViewport.minDepth = viewport->min_depth;
  6228. vulkanCommandBuffer->currentViewport.maxDepth = viewport->max_depth;
  6229. // Viewport flip for consistency with other backends
  6230. vulkanCommandBuffer->currentViewport.y = viewport->y + viewport->h;
  6231. vulkanCommandBuffer->currentViewport.height = -viewport->h;
  6232. renderer->vkCmdSetViewport(
  6233. vulkanCommandBuffer->commandBuffer,
  6234. 0,
  6235. 1,
  6236. &vulkanCommandBuffer->currentViewport);
  6237. }
  6238. static void VULKAN_SetViewport(
  6239. SDL_GPUCommandBuffer *commandBuffer,
  6240. const SDL_GPUViewport *viewport)
  6241. {
  6242. VulkanCommandBuffer *vulkanCommandBuffer = (VulkanCommandBuffer *)commandBuffer;
  6243. VULKAN_INTERNAL_SetCurrentViewport(
  6244. vulkanCommandBuffer,
  6245. viewport);
  6246. }
  6247. static void VULKAN_INTERNAL_SetCurrentScissor(
  6248. VulkanCommandBuffer *vulkanCommandBuffer,
  6249. const SDL_Rect *scissor)
  6250. {
  6251. VulkanRenderer *renderer = vulkanCommandBuffer->renderer;
  6252. vulkanCommandBuffer->currentScissor.offset.x = scissor->x;
  6253. vulkanCommandBuffer->currentScissor.offset.y = scissor->y;
  6254. vulkanCommandBuffer->currentScissor.extent.width = scissor->w;
  6255. vulkanCommandBuffer->currentScissor.extent.height = scissor->h;
  6256. renderer->vkCmdSetScissor(
  6257. vulkanCommandBuffer->commandBuffer,
  6258. 0,
  6259. 1,
  6260. &vulkanCommandBuffer->currentScissor);
  6261. }
  6262. static void VULKAN_SetScissor(
  6263. SDL_GPUCommandBuffer *commandBuffer,
  6264. const SDL_Rect *scissor)
  6265. {
  6266. VulkanCommandBuffer *vulkanCommandBuffer = (VulkanCommandBuffer *)commandBuffer;
  6267. VULKAN_INTERNAL_SetCurrentScissor(
  6268. vulkanCommandBuffer,
  6269. scissor);
  6270. }
  6271. static void VULKAN_INTERNAL_SetCurrentBlendConstants(
  6272. VulkanCommandBuffer *vulkanCommandBuffer,
  6273. SDL_FColor blendConstants)
  6274. {
  6275. VulkanRenderer *renderer = vulkanCommandBuffer->renderer;
  6276. vulkanCommandBuffer->blendConstants[0] = blendConstants.r;
  6277. vulkanCommandBuffer->blendConstants[1] = blendConstants.g;
  6278. vulkanCommandBuffer->blendConstants[2] = blendConstants.b;
  6279. vulkanCommandBuffer->blendConstants[3] = blendConstants.a;
  6280. renderer->vkCmdSetBlendConstants(
  6281. vulkanCommandBuffer->commandBuffer,
  6282. vulkanCommandBuffer->blendConstants);
  6283. }
  6284. static void VULKAN_SetBlendConstants(
  6285. SDL_GPUCommandBuffer *commandBuffer,
  6286. SDL_FColor blendConstants)
  6287. {
  6288. VulkanCommandBuffer *vulkanCommandBuffer = (VulkanCommandBuffer *)commandBuffer;
  6289. VULKAN_INTERNAL_SetCurrentBlendConstants(
  6290. vulkanCommandBuffer,
  6291. blendConstants);
  6292. }
  6293. static void VULKAN_INTERNAL_SetCurrentStencilReference(
  6294. VulkanCommandBuffer *vulkanCommandBuffer,
  6295. Uint8 reference)
  6296. {
  6297. VulkanRenderer *renderer = vulkanCommandBuffer->renderer;
  6298. vulkanCommandBuffer->stencilRef = reference;
  6299. renderer->vkCmdSetStencilReference(
  6300. vulkanCommandBuffer->commandBuffer,
  6301. VK_STENCIL_FACE_FRONT_AND_BACK,
  6302. vulkanCommandBuffer->stencilRef);
  6303. }
  6304. static void VULKAN_SetStencilReference(
  6305. SDL_GPUCommandBuffer *commandBuffer,
  6306. Uint8 reference)
  6307. {
  6308. VulkanCommandBuffer *vulkanCommandBuffer = (VulkanCommandBuffer *)commandBuffer;
  6309. VULKAN_INTERNAL_SetCurrentStencilReference(
  6310. vulkanCommandBuffer,
  6311. reference);
  6312. }
  6313. static void VULKAN_BindVertexSamplers(
  6314. SDL_GPUCommandBuffer *commandBuffer,
  6315. Uint32 firstSlot,
  6316. const SDL_GPUTextureSamplerBinding *textureSamplerBindings,
  6317. Uint32 numBindings)
  6318. {
  6319. VulkanCommandBuffer *vulkanCommandBuffer = (VulkanCommandBuffer *)commandBuffer;
  6320. for (Uint32 i = 0; i < numBindings; i += 1) {
  6321. VulkanTextureContainer *textureContainer = (VulkanTextureContainer *)textureSamplerBindings[i].texture;
  6322. VulkanSampler *sampler = (VulkanSampler *)textureSamplerBindings[i].sampler;
  6323. if (vulkanCommandBuffer->vertexSamplerBindings[firstSlot + i] != sampler->sampler) {
  6324. VULKAN_INTERNAL_TrackSampler(
  6325. vulkanCommandBuffer,
  6326. (VulkanSampler *)textureSamplerBindings[i].sampler);
  6327. vulkanCommandBuffer->vertexSamplerBindings[firstSlot + i] = sampler->sampler;
  6328. vulkanCommandBuffer->needNewVertexResourceDescriptorSet = true;
  6329. }
  6330. if (vulkanCommandBuffer->vertexSamplerTextureViewBindings[firstSlot + i] != textureContainer->activeTexture->fullView) {
  6331. VULKAN_INTERNAL_TrackTexture(
  6332. vulkanCommandBuffer,
  6333. textureContainer->activeTexture);
  6334. vulkanCommandBuffer->vertexSamplerTextureViewBindings[firstSlot + i] = textureContainer->activeTexture->fullView;
  6335. vulkanCommandBuffer->needNewVertexResourceDescriptorSet = true;
  6336. }
  6337. }
  6338. }
  6339. static void VULKAN_BindVertexStorageTextures(
  6340. SDL_GPUCommandBuffer *commandBuffer,
  6341. Uint32 firstSlot,
  6342. SDL_GPUTexture *const *storageTextures,
  6343. Uint32 numBindings)
  6344. {
  6345. VulkanCommandBuffer *vulkanCommandBuffer = (VulkanCommandBuffer *)commandBuffer;
  6346. for (Uint32 i = 0; i < numBindings; i += 1) {
  6347. VulkanTextureContainer *textureContainer = (VulkanTextureContainer *)storageTextures[i];
  6348. if (vulkanCommandBuffer->vertexStorageTextureViewBindings[firstSlot + i] != textureContainer->activeTexture->fullView) {
  6349. VULKAN_INTERNAL_TrackTexture(
  6350. vulkanCommandBuffer,
  6351. textureContainer->activeTexture);
  6352. vulkanCommandBuffer->vertexStorageTextureViewBindings[firstSlot + i] = textureContainer->activeTexture->fullView;
  6353. vulkanCommandBuffer->needNewVertexResourceDescriptorSet = true;
  6354. }
  6355. }
  6356. }
  6357. static void VULKAN_BindVertexStorageBuffers(
  6358. SDL_GPUCommandBuffer *commandBuffer,
  6359. Uint32 firstSlot,
  6360. SDL_GPUBuffer *const *storageBuffers,
  6361. Uint32 numBindings)
  6362. {
  6363. VulkanCommandBuffer *vulkanCommandBuffer = (VulkanCommandBuffer *)commandBuffer;
  6364. for (Uint32 i = 0; i < numBindings; i += 1) {
  6365. VulkanBufferContainer *bufferContainer = (VulkanBufferContainer *)storageBuffers[i];
  6366. if (vulkanCommandBuffer->vertexStorageBufferBindings[firstSlot + i] != bufferContainer->activeBuffer->buffer) {
  6367. VULKAN_INTERNAL_TrackBuffer(
  6368. vulkanCommandBuffer,
  6369. bufferContainer->activeBuffer);
  6370. vulkanCommandBuffer->vertexStorageBufferBindings[firstSlot + i] = bufferContainer->activeBuffer->buffer;
  6371. vulkanCommandBuffer->needNewVertexResourceDescriptorSet = true;
  6372. }
  6373. }
  6374. }
  6375. static void VULKAN_BindFragmentSamplers(
  6376. SDL_GPUCommandBuffer *commandBuffer,
  6377. Uint32 firstSlot,
  6378. const SDL_GPUTextureSamplerBinding *textureSamplerBindings,
  6379. Uint32 numBindings)
  6380. {
  6381. VulkanCommandBuffer *vulkanCommandBuffer = (VulkanCommandBuffer *)commandBuffer;
  6382. for (Uint32 i = 0; i < numBindings; i += 1) {
  6383. VulkanTextureContainer *textureContainer = (VulkanTextureContainer *)textureSamplerBindings[i].texture;
  6384. VulkanSampler *sampler = (VulkanSampler *)textureSamplerBindings[i].sampler;
  6385. if (vulkanCommandBuffer->fragmentSamplerBindings[firstSlot + i] != sampler->sampler) {
  6386. VULKAN_INTERNAL_TrackSampler(
  6387. vulkanCommandBuffer,
  6388. (VulkanSampler *)textureSamplerBindings[i].sampler);
  6389. vulkanCommandBuffer->fragmentSamplerBindings[firstSlot + i] = sampler->sampler;
  6390. vulkanCommandBuffer->needNewFragmentResourceDescriptorSet = true;
  6391. }
  6392. if (vulkanCommandBuffer->fragmentSamplerTextureViewBindings[firstSlot + i] != textureContainer->activeTexture->fullView) {
  6393. VULKAN_INTERNAL_TrackTexture(
  6394. vulkanCommandBuffer,
  6395. textureContainer->activeTexture);
  6396. vulkanCommandBuffer->fragmentSamplerTextureViewBindings[firstSlot + i] = textureContainer->activeTexture->fullView;
  6397. vulkanCommandBuffer->needNewFragmentResourceDescriptorSet = true;
  6398. }
  6399. }
  6400. }
  6401. static void VULKAN_BindFragmentStorageTextures(
  6402. SDL_GPUCommandBuffer *commandBuffer,
  6403. Uint32 firstSlot,
  6404. SDL_GPUTexture *const *storageTextures,
  6405. Uint32 numBindings)
  6406. {
  6407. VulkanCommandBuffer *vulkanCommandBuffer = (VulkanCommandBuffer *)commandBuffer;
  6408. for (Uint32 i = 0; i < numBindings; i += 1) {
  6409. VulkanTextureContainer *textureContainer = (VulkanTextureContainer *)storageTextures[i];
  6410. if (vulkanCommandBuffer->fragmentStorageTextureViewBindings[firstSlot + i] != textureContainer->activeTexture->fullView) {
  6411. VULKAN_INTERNAL_TrackTexture(
  6412. vulkanCommandBuffer,
  6413. textureContainer->activeTexture);
  6414. vulkanCommandBuffer->fragmentStorageTextureViewBindings[firstSlot + i] = textureContainer->activeTexture->fullView;
  6415. vulkanCommandBuffer->needNewFragmentResourceDescriptorSet = true;
  6416. }
  6417. }
  6418. }
  6419. static void VULKAN_BindFragmentStorageBuffers(
  6420. SDL_GPUCommandBuffer *commandBuffer,
  6421. Uint32 firstSlot,
  6422. SDL_GPUBuffer *const *storageBuffers,
  6423. Uint32 numBindings)
  6424. {
  6425. VulkanCommandBuffer *vulkanCommandBuffer = (VulkanCommandBuffer *)commandBuffer;
  6426. VulkanBufferContainer *bufferContainer;
  6427. Uint32 i;
  6428. for (i = 0; i < numBindings; i += 1) {
  6429. bufferContainer = (VulkanBufferContainer *)storageBuffers[i];
  6430. if (vulkanCommandBuffer->fragmentStorageBufferBindings[firstSlot + i] != bufferContainer->activeBuffer->buffer) {
  6431. VULKAN_INTERNAL_TrackBuffer(
  6432. vulkanCommandBuffer,
  6433. bufferContainer->activeBuffer);
  6434. vulkanCommandBuffer->fragmentStorageBufferBindings[firstSlot + i] = bufferContainer->activeBuffer->buffer;
  6435. vulkanCommandBuffer->needNewFragmentResourceDescriptorSet = true;
  6436. }
  6437. }
  6438. }
  6439. static VulkanUniformBuffer *VULKAN_INTERNAL_AcquireUniformBufferFromPool(
  6440. VulkanCommandBuffer *commandBuffer)
  6441. {
  6442. VulkanRenderer *renderer = commandBuffer->renderer;
  6443. VulkanUniformBuffer *uniformBuffer;
  6444. SDL_LockMutex(renderer->acquireUniformBufferLock);
  6445. if (renderer->uniformBufferPoolCount > 0) {
  6446. uniformBuffer = renderer->uniformBufferPool[renderer->uniformBufferPoolCount - 1];
  6447. renderer->uniformBufferPoolCount -= 1;
  6448. } else {
  6449. uniformBuffer = VULKAN_INTERNAL_CreateUniformBuffer(
  6450. renderer,
  6451. UNIFORM_BUFFER_SIZE);
  6452. }
  6453. SDL_UnlockMutex(renderer->acquireUniformBufferLock);
  6454. VULKAN_INTERNAL_TrackUniformBuffer(commandBuffer, uniformBuffer);
  6455. return uniformBuffer;
  6456. }
  6457. static void VULKAN_INTERNAL_ReturnUniformBufferToPool(
  6458. VulkanRenderer *renderer,
  6459. VulkanUniformBuffer *uniformBuffer)
  6460. {
  6461. if (renderer->uniformBufferPoolCount >= renderer->uniformBufferPoolCapacity) {
  6462. renderer->uniformBufferPoolCapacity *= 2;
  6463. renderer->uniformBufferPool = SDL_realloc(
  6464. renderer->uniformBufferPool,
  6465. renderer->uniformBufferPoolCapacity * sizeof(VulkanUniformBuffer *));
  6466. }
  6467. renderer->uniformBufferPool[renderer->uniformBufferPoolCount] = uniformBuffer;
  6468. renderer->uniformBufferPoolCount += 1;
  6469. uniformBuffer->writeOffset = 0;
  6470. uniformBuffer->drawOffset = 0;
  6471. }
  6472. static void VULKAN_INTERNAL_PushUniformData(
  6473. VulkanCommandBuffer *commandBuffer,
  6474. VulkanUniformBufferStage uniformBufferStage,
  6475. Uint32 slotIndex,
  6476. const void *data,
  6477. Uint32 length)
  6478. {
  6479. Uint32 blockSize =
  6480. VULKAN_INTERNAL_NextHighestAlignment32(
  6481. length,
  6482. commandBuffer->renderer->minUBOAlignment);
  6483. VulkanUniformBuffer *uniformBuffer;
  6484. if (uniformBufferStage == VULKAN_UNIFORM_BUFFER_STAGE_VERTEX) {
  6485. if (commandBuffer->vertexUniformBuffers[slotIndex] == NULL) {
  6486. commandBuffer->vertexUniformBuffers[slotIndex] = VULKAN_INTERNAL_AcquireUniformBufferFromPool(
  6487. commandBuffer);
  6488. }
  6489. uniformBuffer = commandBuffer->vertexUniformBuffers[slotIndex];
  6490. } else if (uniformBufferStage == VULKAN_UNIFORM_BUFFER_STAGE_FRAGMENT) {
  6491. if (commandBuffer->fragmentUniformBuffers[slotIndex] == NULL) {
  6492. commandBuffer->fragmentUniformBuffers[slotIndex] = VULKAN_INTERNAL_AcquireUniformBufferFromPool(
  6493. commandBuffer);
  6494. }
  6495. uniformBuffer = commandBuffer->fragmentUniformBuffers[slotIndex];
  6496. } else if (uniformBufferStage == VULKAN_UNIFORM_BUFFER_STAGE_COMPUTE) {
  6497. if (commandBuffer->computeUniformBuffers[slotIndex] == NULL) {
  6498. commandBuffer->computeUniformBuffers[slotIndex] = VULKAN_INTERNAL_AcquireUniformBufferFromPool(
  6499. commandBuffer);
  6500. }
  6501. uniformBuffer = commandBuffer->computeUniformBuffers[slotIndex];
  6502. } else {
  6503. SDL_LogError(SDL_LOG_CATEGORY_GPU, "Unrecognized shader stage!");
  6504. return;
  6505. }
  6506. // If there is no more room, acquire a new uniform buffer
  6507. if (uniformBuffer->writeOffset + blockSize + MAX_UBO_SECTION_SIZE >= uniformBuffer->buffer->size) {
  6508. uniformBuffer = VULKAN_INTERNAL_AcquireUniformBufferFromPool(commandBuffer);
  6509. uniformBuffer->drawOffset = 0;
  6510. uniformBuffer->writeOffset = 0;
  6511. if (uniformBufferStage == VULKAN_UNIFORM_BUFFER_STAGE_VERTEX) {
  6512. commandBuffer->vertexUniformBuffers[slotIndex] = uniformBuffer;
  6513. commandBuffer->needNewVertexUniformDescriptorSet = true;
  6514. } else if (uniformBufferStage == VULKAN_UNIFORM_BUFFER_STAGE_FRAGMENT) {
  6515. commandBuffer->fragmentUniformBuffers[slotIndex] = uniformBuffer;
  6516. commandBuffer->needNewFragmentUniformDescriptorSet = true;
  6517. } else if (uniformBufferStage == VULKAN_UNIFORM_BUFFER_STAGE_COMPUTE) {
  6518. commandBuffer->computeUniformBuffers[slotIndex] = uniformBuffer;
  6519. commandBuffer->needNewComputeUniformDescriptorSet = true;
  6520. } else {
  6521. SDL_LogError(SDL_LOG_CATEGORY_GPU, "Unrecognized shader stage!");
  6522. return;
  6523. }
  6524. }
  6525. uniformBuffer->drawOffset = uniformBuffer->writeOffset;
  6526. Uint8 *dst =
  6527. uniformBuffer->buffer->usedRegion->allocation->mapPointer +
  6528. uniformBuffer->buffer->usedRegion->resourceOffset +
  6529. uniformBuffer->writeOffset;
  6530. SDL_memcpy(
  6531. dst,
  6532. data,
  6533. length);
  6534. uniformBuffer->writeOffset += blockSize;
  6535. if (uniformBufferStage == VULKAN_UNIFORM_BUFFER_STAGE_VERTEX) {
  6536. commandBuffer->needNewVertexUniformOffsets = true;
  6537. } else if (uniformBufferStage == VULKAN_UNIFORM_BUFFER_STAGE_FRAGMENT) {
  6538. commandBuffer->needNewFragmentUniformOffsets = true;
  6539. } else if (uniformBufferStage == VULKAN_UNIFORM_BUFFER_STAGE_COMPUTE) {
  6540. commandBuffer->needNewComputeUniformOffsets = true;
  6541. } else {
  6542. SDL_LogError(SDL_LOG_CATEGORY_GPU, "Unrecognized shader stage!");
  6543. return;
  6544. }
  6545. }
  6546. static void VULKAN_BeginRenderPass(
  6547. SDL_GPUCommandBuffer *commandBuffer,
  6548. const SDL_GPUColorTargetInfo *colorTargetInfos,
  6549. Uint32 numColorTargets,
  6550. const SDL_GPUDepthStencilTargetInfo *depthStencilTargetInfo)
  6551. {
  6552. VulkanCommandBuffer *vulkanCommandBuffer = (VulkanCommandBuffer *)commandBuffer;
  6553. VulkanRenderer *renderer = vulkanCommandBuffer->renderer;
  6554. VkRenderPass renderPass;
  6555. VulkanFramebuffer *framebuffer;
  6556. Uint32 w, h;
  6557. VkClearValue *clearValues;
  6558. Uint32 clearCount = 0;
  6559. Uint32 totalColorAttachmentCount = 0;
  6560. Uint32 i;
  6561. SDL_GPUViewport defaultViewport;
  6562. SDL_Rect defaultScissor;
  6563. SDL_FColor defaultBlendConstants;
  6564. Uint32 framebufferWidth = SDL_MAX_UINT32;
  6565. Uint32 framebufferHeight = SDL_MAX_UINT32;
  6566. for (i = 0; i < numColorTargets; i += 1) {
  6567. VulkanTextureContainer *textureContainer = (VulkanTextureContainer *)colorTargetInfos[i].texture;
  6568. w = textureContainer->header.info.width >> colorTargetInfos[i].mip_level;
  6569. h = textureContainer->header.info.height >> colorTargetInfos[i].mip_level;
  6570. // The framebuffer cannot be larger than the smallest attachment.
  6571. if (w < framebufferWidth) {
  6572. framebufferWidth = w;
  6573. }
  6574. if (h < framebufferHeight) {
  6575. framebufferHeight = h;
  6576. }
  6577. }
  6578. if (depthStencilTargetInfo != NULL) {
  6579. VulkanTextureContainer *textureContainer = (VulkanTextureContainer *)depthStencilTargetInfo->texture;
  6580. w = textureContainer->header.info.width >> depthStencilTargetInfo->mip_level;
  6581. h = textureContainer->header.info.height >> depthStencilTargetInfo->mip_level;
  6582. // The framebuffer cannot be larger than the smallest attachment.
  6583. if (w < framebufferWidth) {
  6584. framebufferWidth = w;
  6585. }
  6586. if (h < framebufferHeight) {
  6587. framebufferHeight = h;
  6588. }
  6589. }
  6590. for (i = 0; i < numColorTargets; i += 1) {
  6591. VulkanTextureContainer *textureContainer = (VulkanTextureContainer *)colorTargetInfos[i].texture;
  6592. VulkanTextureSubresource *subresource = VULKAN_INTERNAL_PrepareTextureSubresourceForWrite(
  6593. renderer,
  6594. vulkanCommandBuffer,
  6595. textureContainer,
  6596. textureContainer->header.info.type == SDL_GPU_TEXTURETYPE_3D ? 0 : colorTargetInfos[i].layer_or_depth_plane,
  6597. colorTargetInfos[i].mip_level,
  6598. colorTargetInfos[i].cycle,
  6599. VULKAN_TEXTURE_USAGE_MODE_COLOR_ATTACHMENT);
  6600. vulkanCommandBuffer->colorAttachmentSubresources[vulkanCommandBuffer->colorAttachmentSubresourceCount] = subresource;
  6601. vulkanCommandBuffer->colorAttachmentSubresourceCount += 1;
  6602. VULKAN_INTERNAL_TrackTexture(vulkanCommandBuffer, subresource->parent);
  6603. totalColorAttachmentCount += 1;
  6604. clearCount += 1;
  6605. if (colorTargetInfos[i].store_op == SDL_GPU_STOREOP_RESOLVE || colorTargetInfos[i].store_op == SDL_GPU_STOREOP_RESOLVE_AND_STORE) {
  6606. VulkanTextureContainer *resolveContainer = (VulkanTextureContainer *)colorTargetInfos[i].resolve_texture;
  6607. VulkanTextureSubresource *resolveSubresource = VULKAN_INTERNAL_PrepareTextureSubresourceForWrite(
  6608. renderer,
  6609. vulkanCommandBuffer,
  6610. resolveContainer,
  6611. colorTargetInfos[i].resolve_layer,
  6612. colorTargetInfos[i].resolve_mip_level,
  6613. colorTargetInfos[i].cycle_resolve_texture,
  6614. VULKAN_TEXTURE_USAGE_MODE_COLOR_ATTACHMENT);
  6615. vulkanCommandBuffer->resolveAttachmentSubresources[vulkanCommandBuffer->resolveAttachmentSubresourceCount] = resolveSubresource;
  6616. vulkanCommandBuffer->resolveAttachmentSubresourceCount += 1;
  6617. VULKAN_INTERNAL_TrackTexture(vulkanCommandBuffer, resolveSubresource->parent);
  6618. totalColorAttachmentCount += 1;
  6619. clearCount += 1;
  6620. }
  6621. }
  6622. if (depthStencilTargetInfo != NULL) {
  6623. VulkanTextureContainer *textureContainer = (VulkanTextureContainer *)depthStencilTargetInfo->texture;
  6624. VulkanTextureSubresource *subresource = VULKAN_INTERNAL_PrepareTextureSubresourceForWrite(
  6625. renderer,
  6626. vulkanCommandBuffer,
  6627. textureContainer,
  6628. depthStencilTargetInfo->layer,
  6629. depthStencilTargetInfo->mip_level,
  6630. depthStencilTargetInfo->cycle,
  6631. VULKAN_TEXTURE_USAGE_MODE_DEPTH_STENCIL_ATTACHMENT);
  6632. vulkanCommandBuffer->depthStencilAttachmentSubresource = subresource;
  6633. VULKAN_INTERNAL_TrackTexture(vulkanCommandBuffer, subresource->parent);
  6634. clearCount += 1;
  6635. }
  6636. // Fetch required render objects
  6637. renderPass = VULKAN_INTERNAL_FetchRenderPass(
  6638. renderer,
  6639. colorTargetInfos,
  6640. numColorTargets,
  6641. depthStencilTargetInfo);
  6642. if (renderPass == VK_NULL_HANDLE) {
  6643. return;
  6644. }
  6645. framebuffer = VULKAN_INTERNAL_FetchFramebuffer(
  6646. renderer,
  6647. renderPass,
  6648. colorTargetInfos,
  6649. numColorTargets,
  6650. depthStencilTargetInfo,
  6651. framebufferWidth,
  6652. framebufferHeight);
  6653. if (framebuffer == NULL) {
  6654. return;
  6655. }
  6656. VULKAN_INTERNAL_TrackFramebuffer(vulkanCommandBuffer, framebuffer);
  6657. // Set clear values
  6658. clearValues = SDL_stack_alloc(VkClearValue, clearCount);
  6659. int clearIndex = 0;
  6660. for (i = 0; i < numColorTargets; i += 1) {
  6661. clearValues[clearIndex].color.float32[0] = colorTargetInfos[i].clear_color.r;
  6662. clearValues[clearIndex].color.float32[1] = colorTargetInfos[i].clear_color.g;
  6663. clearValues[clearIndex].color.float32[2] = colorTargetInfos[i].clear_color.b;
  6664. clearValues[clearIndex].color.float32[3] = colorTargetInfos[i].clear_color.a;
  6665. clearIndex += 1;
  6666. if (colorTargetInfos[i].store_op == SDL_GPU_STOREOP_RESOLVE || colorTargetInfos[i].store_op == SDL_GPU_STOREOP_RESOLVE_AND_STORE) {
  6667. // Skip over the resolve texture, we're not clearing it
  6668. clearIndex += 1;
  6669. }
  6670. }
  6671. if (depthStencilTargetInfo != NULL) {
  6672. clearValues[totalColorAttachmentCount].depthStencil.depth =
  6673. depthStencilTargetInfo->clear_depth;
  6674. clearValues[totalColorAttachmentCount].depthStencil.stencil =
  6675. depthStencilTargetInfo->clear_stencil;
  6676. }
  6677. VkRenderPassBeginInfo renderPassBeginInfo;
  6678. renderPassBeginInfo.sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO;
  6679. renderPassBeginInfo.pNext = NULL;
  6680. renderPassBeginInfo.renderPass = renderPass;
  6681. renderPassBeginInfo.framebuffer = framebuffer->framebuffer;
  6682. renderPassBeginInfo.pClearValues = clearValues;
  6683. renderPassBeginInfo.clearValueCount = clearCount;
  6684. renderPassBeginInfo.renderArea.extent.width = framebufferWidth;
  6685. renderPassBeginInfo.renderArea.extent.height = framebufferHeight;
  6686. renderPassBeginInfo.renderArea.offset.x = 0;
  6687. renderPassBeginInfo.renderArea.offset.y = 0;
  6688. renderer->vkCmdBeginRenderPass(
  6689. vulkanCommandBuffer->commandBuffer,
  6690. &renderPassBeginInfo,
  6691. VK_SUBPASS_CONTENTS_INLINE);
  6692. SDL_stack_free(clearValues);
  6693. // Set sensible default states
  6694. defaultViewport.x = 0;
  6695. defaultViewport.y = 0;
  6696. defaultViewport.w = (float)framebufferWidth;
  6697. defaultViewport.h = (float)framebufferHeight;
  6698. defaultViewport.min_depth = 0;
  6699. defaultViewport.max_depth = 1;
  6700. VULKAN_INTERNAL_SetCurrentViewport(
  6701. vulkanCommandBuffer,
  6702. &defaultViewport);
  6703. defaultScissor.x = 0;
  6704. defaultScissor.y = 0;
  6705. defaultScissor.w = (Sint32)framebufferWidth;
  6706. defaultScissor.h = (Sint32)framebufferHeight;
  6707. VULKAN_INTERNAL_SetCurrentScissor(
  6708. vulkanCommandBuffer,
  6709. &defaultScissor);
  6710. defaultBlendConstants.r = 1.0f;
  6711. defaultBlendConstants.g = 1.0f;
  6712. defaultBlendConstants.b = 1.0f;
  6713. defaultBlendConstants.a = 1.0f;
  6714. VULKAN_INTERNAL_SetCurrentBlendConstants(
  6715. vulkanCommandBuffer,
  6716. defaultBlendConstants);
  6717. VULKAN_INTERNAL_SetCurrentStencilReference(
  6718. vulkanCommandBuffer,
  6719. 0);
  6720. }
  6721. static void VULKAN_BindGraphicsPipeline(
  6722. SDL_GPUCommandBuffer *commandBuffer,
  6723. SDL_GPUGraphicsPipeline *graphicsPipeline)
  6724. {
  6725. VulkanCommandBuffer *vulkanCommandBuffer = (VulkanCommandBuffer *)commandBuffer;
  6726. VulkanRenderer *renderer = vulkanCommandBuffer->renderer;
  6727. VulkanGraphicsPipeline *pipeline = (VulkanGraphicsPipeline *)graphicsPipeline;
  6728. renderer->vkCmdBindPipeline(
  6729. vulkanCommandBuffer->commandBuffer,
  6730. VK_PIPELINE_BIND_POINT_GRAPHICS,
  6731. pipeline->pipeline);
  6732. vulkanCommandBuffer->currentGraphicsPipeline = pipeline;
  6733. VULKAN_INTERNAL_TrackGraphicsPipeline(vulkanCommandBuffer, pipeline);
  6734. // Acquire uniform buffers if necessary
  6735. for (Uint32 i = 0; i < pipeline->resourceLayout->vertexUniformBufferCount; i += 1) {
  6736. if (vulkanCommandBuffer->vertexUniformBuffers[i] == NULL) {
  6737. vulkanCommandBuffer->vertexUniformBuffers[i] = VULKAN_INTERNAL_AcquireUniformBufferFromPool(
  6738. vulkanCommandBuffer);
  6739. }
  6740. }
  6741. for (Uint32 i = 0; i < pipeline->resourceLayout->fragmentUniformBufferCount; i += 1) {
  6742. if (vulkanCommandBuffer->fragmentUniformBuffers[i] == NULL) {
  6743. vulkanCommandBuffer->fragmentUniformBuffers[i] = VULKAN_INTERNAL_AcquireUniformBufferFromPool(
  6744. vulkanCommandBuffer);
  6745. }
  6746. }
  6747. // Mark bindings as needed
  6748. vulkanCommandBuffer->needNewVertexResourceDescriptorSet = true;
  6749. vulkanCommandBuffer->needNewFragmentResourceDescriptorSet = true;
  6750. vulkanCommandBuffer->needNewVertexUniformDescriptorSet = true;
  6751. vulkanCommandBuffer->needNewFragmentUniformDescriptorSet = true;
  6752. vulkanCommandBuffer->needNewVertexUniformOffsets = true;
  6753. vulkanCommandBuffer->needNewFragmentUniformOffsets = true;
  6754. }
  6755. static void VULKAN_BindVertexBuffers(
  6756. SDL_GPUCommandBuffer *commandBuffer,
  6757. Uint32 firstSlot,
  6758. const SDL_GPUBufferBinding *bindings,
  6759. Uint32 numBindings)
  6760. {
  6761. VulkanCommandBuffer *vulkanCommandBuffer = (VulkanCommandBuffer *)commandBuffer;
  6762. for (Uint32 i = 0; i < numBindings; i += 1) {
  6763. VulkanBuffer *buffer = ((VulkanBufferContainer *)bindings[i].buffer)->activeBuffer;
  6764. if (vulkanCommandBuffer->vertexBuffers[firstSlot + i] != buffer->buffer || vulkanCommandBuffer->vertexBufferOffsets[firstSlot + i] != bindings[i].offset) {
  6765. VULKAN_INTERNAL_TrackBuffer(vulkanCommandBuffer, buffer);
  6766. vulkanCommandBuffer->vertexBuffers[firstSlot + i] = buffer->buffer;
  6767. vulkanCommandBuffer->vertexBufferOffsets[firstSlot + i] = bindings[i].offset;
  6768. vulkanCommandBuffer->needVertexBufferBind = true;
  6769. }
  6770. }
  6771. vulkanCommandBuffer->vertexBufferCount =
  6772. SDL_max(vulkanCommandBuffer->vertexBufferCount, firstSlot + numBindings);
  6773. }
  6774. static void VULKAN_BindIndexBuffer(
  6775. SDL_GPUCommandBuffer *commandBuffer,
  6776. const SDL_GPUBufferBinding *binding,
  6777. SDL_GPUIndexElementSize indexElementSize)
  6778. {
  6779. VulkanCommandBuffer *vulkanCommandBuffer = (VulkanCommandBuffer *)commandBuffer;
  6780. VulkanRenderer *renderer = vulkanCommandBuffer->renderer;
  6781. VulkanBuffer *vulkanBuffer = ((VulkanBufferContainer *)binding->buffer)->activeBuffer;
  6782. VULKAN_INTERNAL_TrackBuffer(vulkanCommandBuffer, vulkanBuffer);
  6783. renderer->vkCmdBindIndexBuffer(
  6784. vulkanCommandBuffer->commandBuffer,
  6785. vulkanBuffer->buffer,
  6786. (VkDeviceSize)binding->offset,
  6787. SDLToVK_IndexType[indexElementSize]);
  6788. }
  6789. static void VULKAN_PushVertexUniformData(
  6790. SDL_GPUCommandBuffer *commandBuffer,
  6791. Uint32 slotIndex,
  6792. const void *data,
  6793. Uint32 length)
  6794. {
  6795. VulkanCommandBuffer *vulkanCommandBuffer = (VulkanCommandBuffer *)commandBuffer;
  6796. VULKAN_INTERNAL_PushUniformData(
  6797. vulkanCommandBuffer,
  6798. VULKAN_UNIFORM_BUFFER_STAGE_VERTEX,
  6799. slotIndex,
  6800. data,
  6801. length);
  6802. }
  6803. static void VULKAN_PushFragmentUniformData(
  6804. SDL_GPUCommandBuffer *commandBuffer,
  6805. Uint32 slotIndex,
  6806. const void *data,
  6807. Uint32 length)
  6808. {
  6809. VulkanCommandBuffer *vulkanCommandBuffer = (VulkanCommandBuffer *)commandBuffer;
  6810. VULKAN_INTERNAL_PushUniformData(
  6811. vulkanCommandBuffer,
  6812. VULKAN_UNIFORM_BUFFER_STAGE_FRAGMENT,
  6813. slotIndex,
  6814. data,
  6815. length);
  6816. }
  6817. static void VULKAN_EndRenderPass(
  6818. SDL_GPUCommandBuffer *commandBuffer)
  6819. {
  6820. VulkanCommandBuffer *vulkanCommandBuffer = (VulkanCommandBuffer *)commandBuffer;
  6821. VulkanRenderer *renderer = vulkanCommandBuffer->renderer;
  6822. Uint32 i;
  6823. renderer->vkCmdEndRenderPass(
  6824. vulkanCommandBuffer->commandBuffer);
  6825. for (i = 0; i < vulkanCommandBuffer->colorAttachmentSubresourceCount; i += 1) {
  6826. VULKAN_INTERNAL_TextureSubresourceTransitionToDefaultUsage(
  6827. renderer,
  6828. vulkanCommandBuffer,
  6829. VULKAN_TEXTURE_USAGE_MODE_COLOR_ATTACHMENT,
  6830. vulkanCommandBuffer->colorAttachmentSubresources[i]);
  6831. }
  6832. vulkanCommandBuffer->colorAttachmentSubresourceCount = 0;
  6833. for (i = 0; i < vulkanCommandBuffer->resolveAttachmentSubresourceCount; i += 1) {
  6834. VULKAN_INTERNAL_TextureSubresourceTransitionToDefaultUsage(
  6835. renderer,
  6836. vulkanCommandBuffer,
  6837. VULKAN_TEXTURE_USAGE_MODE_COLOR_ATTACHMENT,
  6838. vulkanCommandBuffer->resolveAttachmentSubresources[i]);
  6839. }
  6840. vulkanCommandBuffer->resolveAttachmentSubresourceCount = 0;
  6841. if (vulkanCommandBuffer->depthStencilAttachmentSubresource != NULL) {
  6842. VULKAN_INTERNAL_TextureSubresourceTransitionToDefaultUsage(
  6843. renderer,
  6844. vulkanCommandBuffer,
  6845. VULKAN_TEXTURE_USAGE_MODE_DEPTH_STENCIL_ATTACHMENT,
  6846. vulkanCommandBuffer->depthStencilAttachmentSubresource);
  6847. vulkanCommandBuffer->depthStencilAttachmentSubresource = NULL;
  6848. }
  6849. vulkanCommandBuffer->currentGraphicsPipeline = NULL;
  6850. vulkanCommandBuffer->vertexResourceDescriptorSet = VK_NULL_HANDLE;
  6851. vulkanCommandBuffer->vertexUniformDescriptorSet = VK_NULL_HANDLE;
  6852. vulkanCommandBuffer->fragmentResourceDescriptorSet = VK_NULL_HANDLE;
  6853. vulkanCommandBuffer->fragmentUniformDescriptorSet = VK_NULL_HANDLE;
  6854. // Reset bind state
  6855. SDL_zeroa(vulkanCommandBuffer->colorAttachmentSubresources);
  6856. SDL_zeroa(vulkanCommandBuffer->resolveAttachmentSubresources);
  6857. vulkanCommandBuffer->depthStencilAttachmentSubresource = NULL;
  6858. SDL_zeroa(vulkanCommandBuffer->vertexBuffers);
  6859. SDL_zeroa(vulkanCommandBuffer->vertexBufferOffsets);
  6860. vulkanCommandBuffer->vertexBufferCount = 0;
  6861. SDL_zeroa(vulkanCommandBuffer->vertexSamplerBindings);
  6862. SDL_zeroa(vulkanCommandBuffer->vertexSamplerTextureViewBindings);
  6863. SDL_zeroa(vulkanCommandBuffer->vertexStorageTextureViewBindings);
  6864. SDL_zeroa(vulkanCommandBuffer->vertexStorageBufferBindings);
  6865. SDL_zeroa(vulkanCommandBuffer->fragmentSamplerBindings);
  6866. SDL_zeroa(vulkanCommandBuffer->fragmentSamplerTextureViewBindings);
  6867. SDL_zeroa(vulkanCommandBuffer->fragmentStorageTextureViewBindings);
  6868. SDL_zeroa(vulkanCommandBuffer->fragmentStorageBufferBindings);
  6869. }
  6870. static void VULKAN_BeginComputePass(
  6871. SDL_GPUCommandBuffer *commandBuffer,
  6872. const SDL_GPUStorageTextureReadWriteBinding *storageTextureBindings,
  6873. Uint32 numStorageTextureBindings,
  6874. const SDL_GPUStorageBufferReadWriteBinding *storageBufferBindings,
  6875. Uint32 numStorageBufferBindings)
  6876. {
  6877. VulkanCommandBuffer *vulkanCommandBuffer = (VulkanCommandBuffer *)commandBuffer;
  6878. VulkanRenderer *renderer = vulkanCommandBuffer->renderer;
  6879. VulkanBufferContainer *bufferContainer;
  6880. VulkanBuffer *buffer;
  6881. Uint32 i;
  6882. vulkanCommandBuffer->readWriteComputeStorageTextureSubresourceCount = numStorageTextureBindings;
  6883. for (i = 0; i < numStorageTextureBindings; i += 1) {
  6884. VulkanTextureContainer *textureContainer = (VulkanTextureContainer *)storageTextureBindings[i].texture;
  6885. VulkanTextureSubresource *subresource = VULKAN_INTERNAL_PrepareTextureSubresourceForWrite(
  6886. renderer,
  6887. vulkanCommandBuffer,
  6888. textureContainer,
  6889. storageTextureBindings[i].layer,
  6890. storageTextureBindings[i].mip_level,
  6891. storageTextureBindings[i].cycle,
  6892. VULKAN_TEXTURE_USAGE_MODE_COMPUTE_STORAGE_READ_WRITE);
  6893. vulkanCommandBuffer->readWriteComputeStorageTextureSubresources[i] = subresource;
  6894. vulkanCommandBuffer->readWriteComputeStorageTextureViewBindings[i] = subresource->computeWriteView;
  6895. VULKAN_INTERNAL_TrackTexture(
  6896. vulkanCommandBuffer,
  6897. subresource->parent);
  6898. }
  6899. for (i = 0; i < numStorageBufferBindings; i += 1) {
  6900. bufferContainer = (VulkanBufferContainer *)storageBufferBindings[i].buffer;
  6901. buffer = VULKAN_INTERNAL_PrepareBufferForWrite(
  6902. renderer,
  6903. vulkanCommandBuffer,
  6904. bufferContainer,
  6905. storageBufferBindings[i].cycle,
  6906. VULKAN_BUFFER_USAGE_MODE_COMPUTE_STORAGE_READ_WRITE);
  6907. vulkanCommandBuffer->readWriteComputeStorageBuffers[i] = buffer;
  6908. vulkanCommandBuffer->readWriteComputeStorageBufferBindings[i] = buffer->buffer;
  6909. VULKAN_INTERNAL_TrackBuffer(
  6910. vulkanCommandBuffer,
  6911. buffer);
  6912. }
  6913. }
  6914. static void VULKAN_BindComputePipeline(
  6915. SDL_GPUCommandBuffer *commandBuffer,
  6916. SDL_GPUComputePipeline *computePipeline)
  6917. {
  6918. VulkanCommandBuffer *vulkanCommandBuffer = (VulkanCommandBuffer *)commandBuffer;
  6919. VulkanRenderer *renderer = vulkanCommandBuffer->renderer;
  6920. VulkanComputePipeline *vulkanComputePipeline = (VulkanComputePipeline *)computePipeline;
  6921. renderer->vkCmdBindPipeline(
  6922. vulkanCommandBuffer->commandBuffer,
  6923. VK_PIPELINE_BIND_POINT_COMPUTE,
  6924. vulkanComputePipeline->pipeline);
  6925. vulkanCommandBuffer->currentComputePipeline = vulkanComputePipeline;
  6926. VULKAN_INTERNAL_TrackComputePipeline(vulkanCommandBuffer, vulkanComputePipeline);
  6927. // Acquire uniform buffers if necessary
  6928. for (Uint32 i = 0; i < vulkanComputePipeline->resourceLayout->numUniformBuffers; i += 1) {
  6929. if (vulkanCommandBuffer->computeUniformBuffers[i] == NULL) {
  6930. vulkanCommandBuffer->computeUniformBuffers[i] = VULKAN_INTERNAL_AcquireUniformBufferFromPool(
  6931. vulkanCommandBuffer);
  6932. }
  6933. }
  6934. // Mark binding as needed
  6935. vulkanCommandBuffer->needNewComputeReadWriteDescriptorSet = true;
  6936. vulkanCommandBuffer->needNewComputeReadOnlyDescriptorSet = true;
  6937. vulkanCommandBuffer->needNewComputeUniformDescriptorSet = true;
  6938. vulkanCommandBuffer->needNewComputeUniformOffsets = true;
  6939. }
  6940. static void VULKAN_BindComputeSamplers(
  6941. SDL_GPUCommandBuffer *commandBuffer,
  6942. Uint32 firstSlot,
  6943. const SDL_GPUTextureSamplerBinding *textureSamplerBindings,
  6944. Uint32 numBindings)
  6945. {
  6946. VulkanCommandBuffer *vulkanCommandBuffer = (VulkanCommandBuffer *)commandBuffer;
  6947. for (Uint32 i = 0; i < numBindings; i += 1) {
  6948. VulkanTextureContainer *textureContainer = (VulkanTextureContainer *)textureSamplerBindings[i].texture;
  6949. VulkanSampler *sampler = (VulkanSampler *)textureSamplerBindings[i].sampler;
  6950. if (vulkanCommandBuffer->computeSamplerBindings[firstSlot + i] != sampler->sampler) {
  6951. VULKAN_INTERNAL_TrackSampler(
  6952. vulkanCommandBuffer,
  6953. sampler);
  6954. vulkanCommandBuffer->computeSamplerBindings[firstSlot + i] = sampler->sampler;
  6955. vulkanCommandBuffer->needNewComputeReadOnlyDescriptorSet = true;
  6956. }
  6957. if (vulkanCommandBuffer->computeSamplerTextureViewBindings[firstSlot + i] != textureContainer->activeTexture->fullView) {
  6958. VULKAN_INTERNAL_TrackTexture(
  6959. vulkanCommandBuffer,
  6960. textureContainer->activeTexture);
  6961. vulkanCommandBuffer->computeSamplerTextureViewBindings[firstSlot + i] = textureContainer->activeTexture->fullView;
  6962. vulkanCommandBuffer->needNewComputeReadOnlyDescriptorSet = true;
  6963. }
  6964. }
  6965. }
  6966. static void VULKAN_BindComputeStorageTextures(
  6967. SDL_GPUCommandBuffer *commandBuffer,
  6968. Uint32 firstSlot,
  6969. SDL_GPUTexture *const *storageTextures,
  6970. Uint32 numBindings)
  6971. {
  6972. VulkanCommandBuffer *vulkanCommandBuffer = (VulkanCommandBuffer *)commandBuffer;
  6973. VulkanRenderer *renderer = vulkanCommandBuffer->renderer;
  6974. for (Uint32 i = 0; i < numBindings; i += 1) {
  6975. VulkanTextureContainer *textureContainer = (VulkanTextureContainer *)storageTextures[i];
  6976. if (vulkanCommandBuffer->readOnlyComputeStorageTextures[firstSlot + i] != textureContainer->activeTexture) {
  6977. /* If a different texture as in this slot, transition it back to its default usage */
  6978. if (vulkanCommandBuffer->readOnlyComputeStorageTextures[firstSlot + i] != NULL) {
  6979. VULKAN_INTERNAL_TextureTransitionToDefaultUsage(
  6980. renderer,
  6981. vulkanCommandBuffer,
  6982. VULKAN_TEXTURE_USAGE_MODE_COMPUTE_STORAGE_READ,
  6983. vulkanCommandBuffer->readOnlyComputeStorageTextures[firstSlot + i]);
  6984. }
  6985. /* Then transition the new texture and prepare it for binding */
  6986. VULKAN_INTERNAL_TextureTransitionFromDefaultUsage(
  6987. renderer,
  6988. vulkanCommandBuffer,
  6989. VULKAN_TEXTURE_USAGE_MODE_COMPUTE_STORAGE_READ,
  6990. textureContainer->activeTexture);
  6991. VULKAN_INTERNAL_TrackTexture(
  6992. vulkanCommandBuffer,
  6993. textureContainer->activeTexture);
  6994. vulkanCommandBuffer->readOnlyComputeStorageTextures[firstSlot + i] = textureContainer->activeTexture;
  6995. vulkanCommandBuffer->readOnlyComputeStorageTextureViewBindings[firstSlot + i] = textureContainer->activeTexture->fullView;
  6996. vulkanCommandBuffer->needNewComputeReadOnlyDescriptorSet = true;
  6997. }
  6998. }
  6999. }
  7000. static void VULKAN_BindComputeStorageBuffers(
  7001. SDL_GPUCommandBuffer *commandBuffer,
  7002. Uint32 firstSlot,
  7003. SDL_GPUBuffer *const *storageBuffers,
  7004. Uint32 numBindings)
  7005. {
  7006. VulkanCommandBuffer *vulkanCommandBuffer = (VulkanCommandBuffer *)commandBuffer;
  7007. VulkanRenderer *renderer = vulkanCommandBuffer->renderer;
  7008. for (Uint32 i = 0; i < numBindings; i += 1) {
  7009. VulkanBufferContainer *bufferContainer = (VulkanBufferContainer *)storageBuffers[i];
  7010. if (vulkanCommandBuffer->readOnlyComputeStorageBuffers[firstSlot + i] != bufferContainer->activeBuffer) {
  7011. /* If a different buffer was in this slot, transition it back to its default usage */
  7012. if (vulkanCommandBuffer->readOnlyComputeStorageBuffers[firstSlot + i] != NULL) {
  7013. VULKAN_INTERNAL_BufferTransitionToDefaultUsage(
  7014. renderer,
  7015. vulkanCommandBuffer,
  7016. VULKAN_BUFFER_USAGE_MODE_COMPUTE_STORAGE_READ,
  7017. vulkanCommandBuffer->readOnlyComputeStorageBuffers[firstSlot + i]);
  7018. }
  7019. /* Then transition the new buffer and prepare it for binding */
  7020. VULKAN_INTERNAL_BufferTransitionFromDefaultUsage(
  7021. renderer,
  7022. vulkanCommandBuffer,
  7023. VULKAN_BUFFER_USAGE_MODE_COMPUTE_STORAGE_READ,
  7024. bufferContainer->activeBuffer);
  7025. VULKAN_INTERNAL_TrackBuffer(
  7026. vulkanCommandBuffer,
  7027. bufferContainer->activeBuffer);
  7028. vulkanCommandBuffer->readOnlyComputeStorageBuffers[firstSlot + i] = bufferContainer->activeBuffer;
  7029. vulkanCommandBuffer->readOnlyComputeStorageBufferBindings[firstSlot + i] = bufferContainer->activeBuffer->buffer;
  7030. vulkanCommandBuffer->needNewComputeReadOnlyDescriptorSet = true;
  7031. }
  7032. }
  7033. }
  7034. static void VULKAN_PushComputeUniformData(
  7035. SDL_GPUCommandBuffer *commandBuffer,
  7036. Uint32 slotIndex,
  7037. const void *data,
  7038. Uint32 length)
  7039. {
  7040. VulkanCommandBuffer *vulkanCommandBuffer = (VulkanCommandBuffer *)commandBuffer;
  7041. VULKAN_INTERNAL_PushUniformData(
  7042. vulkanCommandBuffer,
  7043. VULKAN_UNIFORM_BUFFER_STAGE_COMPUTE,
  7044. slotIndex,
  7045. data,
  7046. length);
  7047. }
  7048. static void VULKAN_INTERNAL_BindComputeDescriptorSets(
  7049. VulkanRenderer *renderer,
  7050. VulkanCommandBuffer *commandBuffer)
  7051. {
  7052. VulkanComputePipelineResourceLayout *resourceLayout;
  7053. DescriptorSetLayout *descriptorSetLayout;
  7054. VkWriteDescriptorSet writeDescriptorSets[
  7055. MAX_TEXTURE_SAMPLERS_PER_STAGE +
  7056. MAX_STORAGE_TEXTURES_PER_STAGE +
  7057. MAX_STORAGE_BUFFERS_PER_STAGE +
  7058. MAX_COMPUTE_WRITE_TEXTURES +
  7059. MAX_COMPUTE_WRITE_BUFFERS +
  7060. MAX_UNIFORM_BUFFERS_PER_STAGE];
  7061. VkDescriptorBufferInfo bufferInfos[MAX_STORAGE_BUFFERS_PER_STAGE + MAX_COMPUTE_WRITE_BUFFERS + MAX_UNIFORM_BUFFERS_PER_STAGE];
  7062. VkDescriptorImageInfo imageInfos[MAX_TEXTURE_SAMPLERS_PER_STAGE + MAX_STORAGE_TEXTURES_PER_STAGE + MAX_COMPUTE_WRITE_TEXTURES];
  7063. Uint32 dynamicOffsets[MAX_UNIFORM_BUFFERS_PER_STAGE];
  7064. Uint32 writeCount = 0;
  7065. Uint32 bufferInfoCount = 0;
  7066. Uint32 imageInfoCount = 0;
  7067. Uint32 dynamicOffsetCount = 0;
  7068. if (
  7069. !commandBuffer->needNewComputeReadOnlyDescriptorSet &&
  7070. !commandBuffer->needNewComputeReadWriteDescriptorSet &&
  7071. !commandBuffer->needNewComputeUniformDescriptorSet &&
  7072. !commandBuffer->needNewComputeUniformOffsets
  7073. ) {
  7074. return;
  7075. }
  7076. resourceLayout = commandBuffer->currentComputePipeline->resourceLayout;
  7077. if (commandBuffer->needNewComputeReadOnlyDescriptorSet) {
  7078. descriptorSetLayout = resourceLayout->descriptorSetLayouts[0];
  7079. commandBuffer->computeReadOnlyDescriptorSet = VULKAN_INTERNAL_FetchDescriptorSet(
  7080. renderer,
  7081. commandBuffer,
  7082. descriptorSetLayout);
  7083. for (Uint32 i = 0; i < resourceLayout->numSamplers; i += 1) {
  7084. VkWriteDescriptorSet *currentWriteDescriptorSet = &writeDescriptorSets[writeCount];
  7085. currentWriteDescriptorSet->sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
  7086. currentWriteDescriptorSet->pNext = NULL;
  7087. currentWriteDescriptorSet->descriptorCount = 1;
  7088. currentWriteDescriptorSet->descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
  7089. currentWriteDescriptorSet->dstArrayElement = 0;
  7090. currentWriteDescriptorSet->dstBinding = i;
  7091. currentWriteDescriptorSet->dstSet = commandBuffer->computeReadOnlyDescriptorSet;
  7092. currentWriteDescriptorSet->pTexelBufferView = NULL;
  7093. currentWriteDescriptorSet->pBufferInfo = NULL;
  7094. imageInfos[imageInfoCount].sampler = commandBuffer->computeSamplerBindings[i];
  7095. imageInfos[imageInfoCount].imageView = commandBuffer->computeSamplerTextureViewBindings[i];
  7096. imageInfos[imageInfoCount].imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
  7097. currentWriteDescriptorSet->pImageInfo = &imageInfos[imageInfoCount];
  7098. writeCount += 1;
  7099. imageInfoCount += 1;
  7100. }
  7101. for (Uint32 i = 0; i < resourceLayout->numReadonlyStorageTextures; i += 1) {
  7102. VkWriteDescriptorSet *currentWriteDescriptorSet = &writeDescriptorSets[writeCount];
  7103. currentWriteDescriptorSet->sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
  7104. currentWriteDescriptorSet->pNext = NULL;
  7105. currentWriteDescriptorSet->descriptorCount = 1;
  7106. currentWriteDescriptorSet->descriptorType = VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE; // Yes, we are declaring the readonly storage texture as a sampled image, because shaders are stupid.
  7107. currentWriteDescriptorSet->dstArrayElement = 0;
  7108. currentWriteDescriptorSet->dstBinding = resourceLayout->numSamplers + i;
  7109. currentWriteDescriptorSet->dstSet = commandBuffer->computeReadOnlyDescriptorSet;
  7110. currentWriteDescriptorSet->pTexelBufferView = NULL;
  7111. currentWriteDescriptorSet->pBufferInfo = NULL;
  7112. imageInfos[imageInfoCount].sampler = VK_NULL_HANDLE;
  7113. imageInfos[imageInfoCount].imageView = commandBuffer->readOnlyComputeStorageTextureViewBindings[i];
  7114. imageInfos[imageInfoCount].imageLayout = VK_IMAGE_LAYOUT_GENERAL;
  7115. currentWriteDescriptorSet->pImageInfo = &imageInfos[imageInfoCount];
  7116. writeCount += 1;
  7117. imageInfoCount += 1;
  7118. }
  7119. for (Uint32 i = 0; i < resourceLayout->numReadonlyStorageBuffers; i += 1) {
  7120. VkWriteDescriptorSet *currentWriteDescriptorSet = &writeDescriptorSets[writeCount];
  7121. currentWriteDescriptorSet->sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
  7122. currentWriteDescriptorSet->pNext = NULL;
  7123. currentWriteDescriptorSet->descriptorCount = 1;
  7124. currentWriteDescriptorSet->descriptorType = VK_DESCRIPTOR_TYPE_STORAGE_BUFFER;
  7125. currentWriteDescriptorSet->dstArrayElement = 0;
  7126. currentWriteDescriptorSet->dstBinding = resourceLayout->numSamplers + resourceLayout->numReadonlyStorageTextures + i;
  7127. currentWriteDescriptorSet->dstSet = commandBuffer->computeReadOnlyDescriptorSet;
  7128. currentWriteDescriptorSet->pTexelBufferView = NULL;
  7129. currentWriteDescriptorSet->pImageInfo = NULL;
  7130. bufferInfos[bufferInfoCount].buffer = commandBuffer->readOnlyComputeStorageBufferBindings[i];
  7131. bufferInfos[bufferInfoCount].offset = 0;
  7132. bufferInfos[bufferInfoCount].range = VK_WHOLE_SIZE;
  7133. currentWriteDescriptorSet->pBufferInfo = &bufferInfos[bufferInfoCount];
  7134. writeCount += 1;
  7135. bufferInfoCount += 1;
  7136. }
  7137. commandBuffer->needNewComputeReadOnlyDescriptorSet = false;
  7138. }
  7139. if (commandBuffer->needNewComputeReadWriteDescriptorSet) {
  7140. descriptorSetLayout = resourceLayout->descriptorSetLayouts[1];
  7141. commandBuffer->computeReadWriteDescriptorSet = VULKAN_INTERNAL_FetchDescriptorSet(
  7142. renderer,
  7143. commandBuffer,
  7144. descriptorSetLayout);
  7145. for (Uint32 i = 0; i < resourceLayout->numReadWriteStorageTextures; i += 1) {
  7146. VkWriteDescriptorSet *currentWriteDescriptorSet = &writeDescriptorSets[writeCount];
  7147. currentWriteDescriptorSet->sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
  7148. currentWriteDescriptorSet->pNext = NULL;
  7149. currentWriteDescriptorSet->descriptorCount = 1;
  7150. currentWriteDescriptorSet->descriptorType = VK_DESCRIPTOR_TYPE_STORAGE_IMAGE;
  7151. currentWriteDescriptorSet->dstArrayElement = 0;
  7152. currentWriteDescriptorSet->dstBinding = i;
  7153. currentWriteDescriptorSet->dstSet = commandBuffer->computeReadWriteDescriptorSet;
  7154. currentWriteDescriptorSet->pTexelBufferView = NULL;
  7155. currentWriteDescriptorSet->pBufferInfo = NULL;
  7156. imageInfos[imageInfoCount].sampler = VK_NULL_HANDLE;
  7157. imageInfos[imageInfoCount].imageView = commandBuffer->readWriteComputeStorageTextureViewBindings[i];
  7158. imageInfos[imageInfoCount].imageLayout = VK_IMAGE_LAYOUT_GENERAL;
  7159. currentWriteDescriptorSet->pImageInfo = &imageInfos[imageInfoCount];
  7160. writeCount += 1;
  7161. imageInfoCount += 1;
  7162. }
  7163. for (Uint32 i = 0; i < resourceLayout->numReadWriteStorageBuffers; i += 1) {
  7164. VkWriteDescriptorSet *currentWriteDescriptorSet = &writeDescriptorSets[writeCount];
  7165. currentWriteDescriptorSet->sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
  7166. currentWriteDescriptorSet->pNext = NULL;
  7167. currentWriteDescriptorSet->descriptorCount = 1;
  7168. currentWriteDescriptorSet->descriptorType = VK_DESCRIPTOR_TYPE_STORAGE_BUFFER;
  7169. currentWriteDescriptorSet->dstArrayElement = 0;
  7170. currentWriteDescriptorSet->dstBinding = resourceLayout->numReadWriteStorageTextures + i;
  7171. currentWriteDescriptorSet->dstSet = commandBuffer->computeReadWriteDescriptorSet;
  7172. currentWriteDescriptorSet->pTexelBufferView = NULL;
  7173. currentWriteDescriptorSet->pImageInfo = NULL;
  7174. bufferInfos[bufferInfoCount].buffer = commandBuffer->readWriteComputeStorageBufferBindings[i];
  7175. bufferInfos[bufferInfoCount].offset = 0;
  7176. bufferInfos[bufferInfoCount].range = VK_WHOLE_SIZE;
  7177. currentWriteDescriptorSet->pBufferInfo = &bufferInfos[bufferInfoCount];
  7178. writeCount += 1;
  7179. bufferInfoCount += 1;
  7180. }
  7181. commandBuffer->needNewComputeReadWriteDescriptorSet = false;
  7182. }
  7183. if (commandBuffer->needNewComputeUniformDescriptorSet) {
  7184. descriptorSetLayout = resourceLayout->descriptorSetLayouts[2];
  7185. commandBuffer->computeUniformDescriptorSet = VULKAN_INTERNAL_FetchDescriptorSet(
  7186. renderer,
  7187. commandBuffer,
  7188. descriptorSetLayout);
  7189. for (Uint32 i = 0; i < resourceLayout->numUniformBuffers; i += 1) {
  7190. VkWriteDescriptorSet *currentWriteDescriptorSet = &writeDescriptorSets[writeCount];
  7191. currentWriteDescriptorSet->sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
  7192. currentWriteDescriptorSet->pNext = NULL;
  7193. currentWriteDescriptorSet->descriptorCount = 1;
  7194. currentWriteDescriptorSet->descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC;
  7195. currentWriteDescriptorSet->dstArrayElement = 0;
  7196. currentWriteDescriptorSet->dstBinding = i;
  7197. currentWriteDescriptorSet->dstSet = commandBuffer->computeUniformDescriptorSet;
  7198. currentWriteDescriptorSet->pTexelBufferView = NULL;
  7199. currentWriteDescriptorSet->pImageInfo = NULL;
  7200. bufferInfos[bufferInfoCount].buffer = commandBuffer->computeUniformBuffers[i]->buffer->buffer;
  7201. bufferInfos[bufferInfoCount].offset = 0;
  7202. bufferInfos[bufferInfoCount].range = MAX_UBO_SECTION_SIZE;
  7203. currentWriteDescriptorSet->pBufferInfo = &bufferInfos[bufferInfoCount];
  7204. writeCount += 1;
  7205. bufferInfoCount += 1;
  7206. }
  7207. commandBuffer->needNewComputeUniformDescriptorSet = false;
  7208. }
  7209. for (Uint32 i = 0; i < resourceLayout->numUniformBuffers; i += 1) {
  7210. dynamicOffsets[i] = commandBuffer->computeUniformBuffers[i]->drawOffset;
  7211. dynamicOffsetCount += 1;
  7212. }
  7213. renderer->vkUpdateDescriptorSets(
  7214. renderer->logicalDevice,
  7215. writeCount,
  7216. writeDescriptorSets,
  7217. 0,
  7218. NULL);
  7219. VkDescriptorSet sets[3];
  7220. sets[0] = commandBuffer->computeReadOnlyDescriptorSet;
  7221. sets[1] = commandBuffer->computeReadWriteDescriptorSet;
  7222. sets[2] = commandBuffer->computeUniformDescriptorSet;
  7223. renderer->vkCmdBindDescriptorSets(
  7224. commandBuffer->commandBuffer,
  7225. VK_PIPELINE_BIND_POINT_COMPUTE,
  7226. resourceLayout->pipelineLayout,
  7227. 0,
  7228. 3,
  7229. sets,
  7230. dynamicOffsetCount,
  7231. dynamicOffsets);
  7232. commandBuffer->needNewComputeUniformOffsets = false;
  7233. }
  7234. static void VULKAN_DispatchCompute(
  7235. SDL_GPUCommandBuffer *commandBuffer,
  7236. Uint32 groupcountX,
  7237. Uint32 groupcountY,
  7238. Uint32 groupcountZ)
  7239. {
  7240. VulkanCommandBuffer *vulkanCommandBuffer = (VulkanCommandBuffer *)commandBuffer;
  7241. VulkanRenderer *renderer = vulkanCommandBuffer->renderer;
  7242. VULKAN_INTERNAL_BindComputeDescriptorSets(renderer, vulkanCommandBuffer);
  7243. renderer->vkCmdDispatch(
  7244. vulkanCommandBuffer->commandBuffer,
  7245. groupcountX,
  7246. groupcountY,
  7247. groupcountZ);
  7248. }
  7249. static void VULKAN_DispatchComputeIndirect(
  7250. SDL_GPUCommandBuffer *commandBuffer,
  7251. SDL_GPUBuffer *buffer,
  7252. Uint32 offset)
  7253. {
  7254. VulkanCommandBuffer *vulkanCommandBuffer = (VulkanCommandBuffer *)commandBuffer;
  7255. VulkanRenderer *renderer = vulkanCommandBuffer->renderer;
  7256. VulkanBuffer *vulkanBuffer = ((VulkanBufferContainer *)buffer)->activeBuffer;
  7257. VULKAN_INTERNAL_BindComputeDescriptorSets(renderer, vulkanCommandBuffer);
  7258. renderer->vkCmdDispatchIndirect(
  7259. vulkanCommandBuffer->commandBuffer,
  7260. vulkanBuffer->buffer,
  7261. offset);
  7262. VULKAN_INTERNAL_TrackBuffer(vulkanCommandBuffer, vulkanBuffer);
  7263. }
  7264. static void VULKAN_EndComputePass(
  7265. SDL_GPUCommandBuffer *commandBuffer)
  7266. {
  7267. VulkanCommandBuffer *vulkanCommandBuffer = (VulkanCommandBuffer *)commandBuffer;
  7268. Uint32 i;
  7269. for (i = 0; i < vulkanCommandBuffer->readWriteComputeStorageTextureSubresourceCount; i += 1) {
  7270. VULKAN_INTERNAL_TextureSubresourceTransitionToDefaultUsage(
  7271. vulkanCommandBuffer->renderer,
  7272. vulkanCommandBuffer,
  7273. VULKAN_TEXTURE_USAGE_MODE_COMPUTE_STORAGE_READ_WRITE,
  7274. vulkanCommandBuffer->readWriteComputeStorageTextureSubresources[i]);
  7275. vulkanCommandBuffer->readWriteComputeStorageTextureSubresources[i] = NULL;
  7276. }
  7277. vulkanCommandBuffer->readWriteComputeStorageTextureSubresourceCount = 0;
  7278. for (i = 0; i < MAX_COMPUTE_WRITE_BUFFERS; i += 1) {
  7279. if (vulkanCommandBuffer->readWriteComputeStorageBuffers[i] != NULL) {
  7280. VULKAN_INTERNAL_BufferTransitionToDefaultUsage(
  7281. vulkanCommandBuffer->renderer,
  7282. vulkanCommandBuffer,
  7283. VULKAN_BUFFER_USAGE_MODE_COMPUTE_STORAGE_READ_WRITE,
  7284. vulkanCommandBuffer->readWriteComputeStorageBuffers[i]);
  7285. vulkanCommandBuffer->readWriteComputeStorageBuffers[i] = NULL;
  7286. }
  7287. }
  7288. for (i = 0; i < MAX_STORAGE_TEXTURES_PER_STAGE; i += 1) {
  7289. if (vulkanCommandBuffer->readOnlyComputeStorageTextures[i] != NULL) {
  7290. VULKAN_INTERNAL_TextureTransitionToDefaultUsage(
  7291. vulkanCommandBuffer->renderer,
  7292. vulkanCommandBuffer,
  7293. VULKAN_TEXTURE_USAGE_MODE_COMPUTE_STORAGE_READ,
  7294. vulkanCommandBuffer->readOnlyComputeStorageTextures[i]);
  7295. vulkanCommandBuffer->readOnlyComputeStorageTextures[i] = NULL;
  7296. }
  7297. }
  7298. for (i = 0; i < MAX_STORAGE_BUFFERS_PER_STAGE; i += 1) {
  7299. if (vulkanCommandBuffer->readOnlyComputeStorageBuffers[i] != NULL) {
  7300. VULKAN_INTERNAL_BufferTransitionToDefaultUsage(
  7301. vulkanCommandBuffer->renderer,
  7302. vulkanCommandBuffer,
  7303. VULKAN_BUFFER_USAGE_MODE_COMPUTE_STORAGE_READ,
  7304. vulkanCommandBuffer->readOnlyComputeStorageBuffers[i]);
  7305. vulkanCommandBuffer->readOnlyComputeStorageBuffers[i] = NULL;
  7306. }
  7307. }
  7308. // we don't need a barrier for sampler resources because sampler state is always the default if sampler bit is set
  7309. SDL_zeroa(vulkanCommandBuffer->computeSamplerTextureViewBindings);
  7310. SDL_zeroa(vulkanCommandBuffer->computeSamplerBindings);
  7311. SDL_zeroa(vulkanCommandBuffer->readWriteComputeStorageTextureViewBindings);
  7312. SDL_zeroa(vulkanCommandBuffer->readWriteComputeStorageBufferBindings);
  7313. vulkanCommandBuffer->currentComputePipeline = NULL;
  7314. vulkanCommandBuffer->computeReadOnlyDescriptorSet = VK_NULL_HANDLE;
  7315. vulkanCommandBuffer->computeReadWriteDescriptorSet = VK_NULL_HANDLE;
  7316. vulkanCommandBuffer->computeUniformDescriptorSet = VK_NULL_HANDLE;
  7317. }
  7318. static void *VULKAN_MapTransferBuffer(
  7319. SDL_GPURenderer *driverData,
  7320. SDL_GPUTransferBuffer *transferBuffer,
  7321. bool cycle)
  7322. {
  7323. VulkanRenderer *renderer = (VulkanRenderer *)driverData;
  7324. VulkanBufferContainer *transferBufferContainer = (VulkanBufferContainer *)transferBuffer;
  7325. if (
  7326. cycle &&
  7327. SDL_GetAtomicInt(&transferBufferContainer->activeBuffer->referenceCount) > 0) {
  7328. VULKAN_INTERNAL_CycleActiveBuffer(
  7329. renderer,
  7330. transferBufferContainer);
  7331. }
  7332. Uint8 *bufferPointer =
  7333. transferBufferContainer->activeBuffer->usedRegion->allocation->mapPointer +
  7334. transferBufferContainer->activeBuffer->usedRegion->resourceOffset;
  7335. return bufferPointer;
  7336. }
  7337. static void VULKAN_UnmapTransferBuffer(
  7338. SDL_GPURenderer *driverData,
  7339. SDL_GPUTransferBuffer *transferBuffer)
  7340. {
  7341. // no-op because transfer buffers are persistently mapped
  7342. (void)driverData;
  7343. (void)transferBuffer;
  7344. }
  7345. static void VULKAN_BeginCopyPass(
  7346. SDL_GPUCommandBuffer *commandBuffer)
  7347. {
  7348. // no-op
  7349. (void)commandBuffer;
  7350. }
  7351. static void VULKAN_UploadToTexture(
  7352. SDL_GPUCommandBuffer *commandBuffer,
  7353. const SDL_GPUTextureTransferInfo *source,
  7354. const SDL_GPUTextureRegion *destination,
  7355. bool cycle)
  7356. {
  7357. VulkanCommandBuffer *vulkanCommandBuffer = (VulkanCommandBuffer *)commandBuffer;
  7358. VulkanRenderer *renderer = vulkanCommandBuffer->renderer;
  7359. VulkanBufferContainer *transferBufferContainer = (VulkanBufferContainer *)source->transfer_buffer;
  7360. VulkanTextureContainer *vulkanTextureContainer = (VulkanTextureContainer *)destination->texture;
  7361. VulkanTextureSubresource *vulkanTextureSubresource;
  7362. VkBufferImageCopy imageCopy;
  7363. // Note that the transfer buffer does not need a barrier, as it is synced by the client
  7364. vulkanTextureSubresource = VULKAN_INTERNAL_PrepareTextureSubresourceForWrite(
  7365. renderer,
  7366. vulkanCommandBuffer,
  7367. vulkanTextureContainer,
  7368. destination->layer,
  7369. destination->mip_level,
  7370. cycle,
  7371. VULKAN_TEXTURE_USAGE_MODE_COPY_DESTINATION);
  7372. imageCopy.imageExtent.width = destination->w;
  7373. imageCopy.imageExtent.height = destination->h;
  7374. imageCopy.imageExtent.depth = destination->d;
  7375. imageCopy.imageOffset.x = destination->x;
  7376. imageCopy.imageOffset.y = destination->y;
  7377. imageCopy.imageOffset.z = destination->z;
  7378. imageCopy.imageSubresource.aspectMask = vulkanTextureSubresource->parent->aspectFlags;
  7379. imageCopy.imageSubresource.baseArrayLayer = destination->layer;
  7380. imageCopy.imageSubresource.layerCount = 1;
  7381. imageCopy.imageSubresource.mipLevel = destination->mip_level;
  7382. imageCopy.bufferOffset = source->offset;
  7383. imageCopy.bufferRowLength = source->pixels_per_row;
  7384. imageCopy.bufferImageHeight = source->rows_per_layer;
  7385. renderer->vkCmdCopyBufferToImage(
  7386. vulkanCommandBuffer->commandBuffer,
  7387. transferBufferContainer->activeBuffer->buffer,
  7388. vulkanTextureSubresource->parent->image,
  7389. VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
  7390. 1,
  7391. &imageCopy);
  7392. VULKAN_INTERNAL_TextureSubresourceTransitionToDefaultUsage(
  7393. renderer,
  7394. vulkanCommandBuffer,
  7395. VULKAN_TEXTURE_USAGE_MODE_COPY_DESTINATION,
  7396. vulkanTextureSubresource);
  7397. VULKAN_INTERNAL_TrackBuffer(vulkanCommandBuffer, transferBufferContainer->activeBuffer);
  7398. VULKAN_INTERNAL_TrackTexture(vulkanCommandBuffer, vulkanTextureSubresource->parent);
  7399. }
  7400. static void VULKAN_UploadToBuffer(
  7401. SDL_GPUCommandBuffer *commandBuffer,
  7402. const SDL_GPUTransferBufferLocation *source,
  7403. const SDL_GPUBufferRegion *destination,
  7404. bool cycle)
  7405. {
  7406. VulkanCommandBuffer *vulkanCommandBuffer = (VulkanCommandBuffer *)commandBuffer;
  7407. VulkanRenderer *renderer = vulkanCommandBuffer->renderer;
  7408. VulkanBufferContainer *transferBufferContainer = (VulkanBufferContainer *)source->transfer_buffer;
  7409. VulkanBufferContainer *bufferContainer = (VulkanBufferContainer *)destination->buffer;
  7410. VkBufferCopy bufferCopy;
  7411. // Note that the transfer buffer does not need a barrier, as it is synced by the client
  7412. VulkanBuffer *vulkanBuffer = VULKAN_INTERNAL_PrepareBufferForWrite(
  7413. renderer,
  7414. vulkanCommandBuffer,
  7415. bufferContainer,
  7416. cycle,
  7417. VULKAN_BUFFER_USAGE_MODE_COPY_DESTINATION);
  7418. bufferCopy.srcOffset = source->offset;
  7419. bufferCopy.dstOffset = destination->offset;
  7420. bufferCopy.size = destination->size;
  7421. renderer->vkCmdCopyBuffer(
  7422. vulkanCommandBuffer->commandBuffer,
  7423. transferBufferContainer->activeBuffer->buffer,
  7424. vulkanBuffer->buffer,
  7425. 1,
  7426. &bufferCopy);
  7427. VULKAN_INTERNAL_BufferTransitionToDefaultUsage(
  7428. renderer,
  7429. vulkanCommandBuffer,
  7430. VULKAN_BUFFER_USAGE_MODE_COPY_DESTINATION,
  7431. vulkanBuffer);
  7432. VULKAN_INTERNAL_TrackBuffer(vulkanCommandBuffer, transferBufferContainer->activeBuffer);
  7433. VULKAN_INTERNAL_TrackBuffer(vulkanCommandBuffer, vulkanBuffer);
  7434. }
  7435. // Readback
  7436. static void VULKAN_DownloadFromTexture(
  7437. SDL_GPUCommandBuffer *commandBuffer,
  7438. const SDL_GPUTextureRegion *source,
  7439. const SDL_GPUTextureTransferInfo *destination)
  7440. {
  7441. VulkanCommandBuffer *vulkanCommandBuffer = (VulkanCommandBuffer *)commandBuffer;
  7442. VulkanRenderer *renderer = vulkanCommandBuffer->renderer;
  7443. VulkanTextureContainer *textureContainer = (VulkanTextureContainer *)source->texture;
  7444. VulkanTextureSubresource *vulkanTextureSubresource;
  7445. VulkanBufferContainer *transferBufferContainer = (VulkanBufferContainer *)destination->transfer_buffer;
  7446. VkBufferImageCopy imageCopy;
  7447. vulkanTextureSubresource = VULKAN_INTERNAL_FetchTextureSubresource(
  7448. textureContainer,
  7449. source->layer,
  7450. source->mip_level);
  7451. // Note that the transfer buffer does not need a barrier, as it is synced by the client
  7452. VULKAN_INTERNAL_TextureSubresourceTransitionFromDefaultUsage(
  7453. renderer,
  7454. vulkanCommandBuffer,
  7455. VULKAN_TEXTURE_USAGE_MODE_COPY_SOURCE,
  7456. vulkanTextureSubresource);
  7457. imageCopy.imageExtent.width = source->w;
  7458. imageCopy.imageExtent.height = source->h;
  7459. imageCopy.imageExtent.depth = source->d;
  7460. imageCopy.imageOffset.x = source->x;
  7461. imageCopy.imageOffset.y = source->y;
  7462. imageCopy.imageOffset.z = source->z;
  7463. imageCopy.imageSubresource.aspectMask = vulkanTextureSubresource->parent->aspectFlags;
  7464. imageCopy.imageSubresource.baseArrayLayer = source->layer;
  7465. imageCopy.imageSubresource.layerCount = 1;
  7466. imageCopy.imageSubresource.mipLevel = source->mip_level;
  7467. imageCopy.bufferOffset = destination->offset;
  7468. imageCopy.bufferRowLength = destination->pixels_per_row;
  7469. imageCopy.bufferImageHeight = destination->rows_per_layer;
  7470. renderer->vkCmdCopyImageToBuffer(
  7471. vulkanCommandBuffer->commandBuffer,
  7472. vulkanTextureSubresource->parent->image,
  7473. VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
  7474. transferBufferContainer->activeBuffer->buffer,
  7475. 1,
  7476. &imageCopy);
  7477. VULKAN_INTERNAL_TextureSubresourceTransitionToDefaultUsage(
  7478. renderer,
  7479. vulkanCommandBuffer,
  7480. VULKAN_TEXTURE_USAGE_MODE_COPY_SOURCE,
  7481. vulkanTextureSubresource);
  7482. VULKAN_INTERNAL_TrackBuffer(vulkanCommandBuffer, transferBufferContainer->activeBuffer);
  7483. VULKAN_INTERNAL_TrackTexture(vulkanCommandBuffer, vulkanTextureSubresource->parent);
  7484. }
  7485. static void VULKAN_DownloadFromBuffer(
  7486. SDL_GPUCommandBuffer *commandBuffer,
  7487. const SDL_GPUBufferRegion *source,
  7488. const SDL_GPUTransferBufferLocation *destination)
  7489. {
  7490. VulkanCommandBuffer *vulkanCommandBuffer = (VulkanCommandBuffer *)commandBuffer;
  7491. VulkanRenderer *renderer = vulkanCommandBuffer->renderer;
  7492. VulkanBufferContainer *bufferContainer = (VulkanBufferContainer *)source->buffer;
  7493. VulkanBufferContainer *transferBufferContainer = (VulkanBufferContainer *)destination->transfer_buffer;
  7494. VkBufferCopy bufferCopy;
  7495. // Note that transfer buffer does not need a barrier, as it is synced by the client
  7496. VULKAN_INTERNAL_BufferTransitionFromDefaultUsage(
  7497. renderer,
  7498. vulkanCommandBuffer,
  7499. VULKAN_BUFFER_USAGE_MODE_COPY_SOURCE,
  7500. bufferContainer->activeBuffer);
  7501. bufferCopy.srcOffset = source->offset;
  7502. bufferCopy.dstOffset = destination->offset;
  7503. bufferCopy.size = source->size;
  7504. renderer->vkCmdCopyBuffer(
  7505. vulkanCommandBuffer->commandBuffer,
  7506. bufferContainer->activeBuffer->buffer,
  7507. transferBufferContainer->activeBuffer->buffer,
  7508. 1,
  7509. &bufferCopy);
  7510. VULKAN_INTERNAL_BufferTransitionToDefaultUsage(
  7511. renderer,
  7512. vulkanCommandBuffer,
  7513. VULKAN_BUFFER_USAGE_MODE_COPY_SOURCE,
  7514. bufferContainer->activeBuffer);
  7515. VULKAN_INTERNAL_TrackBuffer(vulkanCommandBuffer, transferBufferContainer->activeBuffer);
  7516. VULKAN_INTERNAL_TrackBuffer(vulkanCommandBuffer, bufferContainer->activeBuffer);
  7517. }
  7518. static void VULKAN_CopyTextureToTexture(
  7519. SDL_GPUCommandBuffer *commandBuffer,
  7520. const SDL_GPUTextureLocation *source,
  7521. const SDL_GPUTextureLocation *destination,
  7522. Uint32 w,
  7523. Uint32 h,
  7524. Uint32 d,
  7525. bool cycle)
  7526. {
  7527. VulkanCommandBuffer *vulkanCommandBuffer = (VulkanCommandBuffer *)commandBuffer;
  7528. VulkanRenderer *renderer = vulkanCommandBuffer->renderer;
  7529. VulkanTextureSubresource *srcSubresource;
  7530. VulkanTextureSubresource *dstSubresource;
  7531. VkImageCopy imageCopy;
  7532. srcSubresource = VULKAN_INTERNAL_FetchTextureSubresource(
  7533. (VulkanTextureContainer *)source->texture,
  7534. source->layer,
  7535. source->mip_level);
  7536. dstSubresource = VULKAN_INTERNAL_PrepareTextureSubresourceForWrite(
  7537. renderer,
  7538. vulkanCommandBuffer,
  7539. (VulkanTextureContainer *)destination->texture,
  7540. destination->layer,
  7541. destination->mip_level,
  7542. cycle,
  7543. VULKAN_TEXTURE_USAGE_MODE_COPY_DESTINATION);
  7544. VULKAN_INTERNAL_TextureSubresourceTransitionFromDefaultUsage(
  7545. renderer,
  7546. vulkanCommandBuffer,
  7547. VULKAN_TEXTURE_USAGE_MODE_COPY_SOURCE,
  7548. srcSubresource);
  7549. imageCopy.srcOffset.x = source->x;
  7550. imageCopy.srcOffset.y = source->y;
  7551. imageCopy.srcOffset.z = source->z;
  7552. imageCopy.srcSubresource.aspectMask = srcSubresource->parent->aspectFlags;
  7553. imageCopy.srcSubresource.baseArrayLayer = source->layer;
  7554. imageCopy.srcSubresource.layerCount = 1;
  7555. imageCopy.srcSubresource.mipLevel = source->mip_level;
  7556. imageCopy.dstOffset.x = destination->x;
  7557. imageCopy.dstOffset.y = destination->y;
  7558. imageCopy.dstOffset.z = destination->z;
  7559. imageCopy.dstSubresource.aspectMask = dstSubresource->parent->aspectFlags;
  7560. imageCopy.dstSubresource.baseArrayLayer = destination->layer;
  7561. imageCopy.dstSubresource.layerCount = 1;
  7562. imageCopy.dstSubresource.mipLevel = destination->mip_level;
  7563. imageCopy.extent.width = w;
  7564. imageCopy.extent.height = h;
  7565. imageCopy.extent.depth = d;
  7566. renderer->vkCmdCopyImage(
  7567. vulkanCommandBuffer->commandBuffer,
  7568. srcSubresource->parent->image,
  7569. VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
  7570. dstSubresource->parent->image,
  7571. VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
  7572. 1,
  7573. &imageCopy);
  7574. VULKAN_INTERNAL_TextureSubresourceTransitionToDefaultUsage(
  7575. renderer,
  7576. vulkanCommandBuffer,
  7577. VULKAN_TEXTURE_USAGE_MODE_COPY_SOURCE,
  7578. srcSubresource);
  7579. VULKAN_INTERNAL_TextureSubresourceTransitionToDefaultUsage(
  7580. renderer,
  7581. vulkanCommandBuffer,
  7582. VULKAN_TEXTURE_USAGE_MODE_COPY_DESTINATION,
  7583. dstSubresource);
  7584. VULKAN_INTERNAL_TrackTexture(vulkanCommandBuffer, srcSubresource->parent);
  7585. VULKAN_INTERNAL_TrackTexture(vulkanCommandBuffer, dstSubresource->parent);
  7586. }
  7587. static void VULKAN_CopyBufferToBuffer(
  7588. SDL_GPUCommandBuffer *commandBuffer,
  7589. const SDL_GPUBufferLocation *source,
  7590. const SDL_GPUBufferLocation *destination,
  7591. Uint32 size,
  7592. bool cycle)
  7593. {
  7594. VulkanCommandBuffer *vulkanCommandBuffer = (VulkanCommandBuffer *)commandBuffer;
  7595. VulkanRenderer *renderer = vulkanCommandBuffer->renderer;
  7596. VulkanBufferContainer *srcContainer = (VulkanBufferContainer *)source->buffer;
  7597. VulkanBufferContainer *dstContainer = (VulkanBufferContainer *)destination->buffer;
  7598. VkBufferCopy bufferCopy;
  7599. VulkanBuffer *dstBuffer = VULKAN_INTERNAL_PrepareBufferForWrite(
  7600. renderer,
  7601. vulkanCommandBuffer,
  7602. dstContainer,
  7603. cycle,
  7604. VULKAN_BUFFER_USAGE_MODE_COPY_DESTINATION);
  7605. VULKAN_INTERNAL_BufferTransitionFromDefaultUsage(
  7606. renderer,
  7607. vulkanCommandBuffer,
  7608. VULKAN_BUFFER_USAGE_MODE_COPY_SOURCE,
  7609. srcContainer->activeBuffer);
  7610. bufferCopy.srcOffset = source->offset;
  7611. bufferCopy.dstOffset = destination->offset;
  7612. bufferCopy.size = size;
  7613. renderer->vkCmdCopyBuffer(
  7614. vulkanCommandBuffer->commandBuffer,
  7615. srcContainer->activeBuffer->buffer,
  7616. dstBuffer->buffer,
  7617. 1,
  7618. &bufferCopy);
  7619. VULKAN_INTERNAL_BufferTransitionToDefaultUsage(
  7620. renderer,
  7621. vulkanCommandBuffer,
  7622. VULKAN_BUFFER_USAGE_MODE_COPY_SOURCE,
  7623. srcContainer->activeBuffer);
  7624. VULKAN_INTERNAL_BufferTransitionToDefaultUsage(
  7625. renderer,
  7626. vulkanCommandBuffer,
  7627. VULKAN_BUFFER_USAGE_MODE_COPY_DESTINATION,
  7628. dstBuffer);
  7629. VULKAN_INTERNAL_TrackBuffer(vulkanCommandBuffer, srcContainer->activeBuffer);
  7630. VULKAN_INTERNAL_TrackBuffer(vulkanCommandBuffer, dstBuffer);
  7631. }
  7632. static void VULKAN_GenerateMipmaps(
  7633. SDL_GPUCommandBuffer *commandBuffer,
  7634. SDL_GPUTexture *texture)
  7635. {
  7636. VulkanCommandBuffer *vulkanCommandBuffer = (VulkanCommandBuffer *)commandBuffer;
  7637. VulkanRenderer *renderer = vulkanCommandBuffer->renderer;
  7638. VulkanTextureContainer *container = (VulkanTextureContainer *)texture;
  7639. VulkanTextureSubresource *srcTextureSubresource;
  7640. VulkanTextureSubresource *dstTextureSubresource;
  7641. VkImageBlit blit;
  7642. // Blit each slice sequentially. Barriers, barriers everywhere!
  7643. for (Uint32 layerOrDepthIndex = 0; layerOrDepthIndex < container->header.info.layer_count_or_depth; layerOrDepthIndex += 1)
  7644. for (Uint32 level = 1; level < container->header.info.num_levels; level += 1) {
  7645. Uint32 layer = container->header.info.type == SDL_GPU_TEXTURETYPE_3D ? 0 : layerOrDepthIndex;
  7646. Uint32 depth = container->header.info.type == SDL_GPU_TEXTURETYPE_3D ? layerOrDepthIndex : 0;
  7647. Uint32 srcSubresourceIndex = VULKAN_INTERNAL_GetTextureSubresourceIndex(
  7648. level - 1,
  7649. layer,
  7650. container->header.info.num_levels);
  7651. Uint32 dstSubresourceIndex = VULKAN_INTERNAL_GetTextureSubresourceIndex(
  7652. level,
  7653. layer,
  7654. container->header.info.num_levels);
  7655. srcTextureSubresource = &container->activeTexture->subresources[srcSubresourceIndex];
  7656. dstTextureSubresource = &container->activeTexture->subresources[dstSubresourceIndex];
  7657. VULKAN_INTERNAL_TextureSubresourceTransitionFromDefaultUsage(
  7658. renderer,
  7659. vulkanCommandBuffer,
  7660. VULKAN_TEXTURE_USAGE_MODE_COPY_SOURCE,
  7661. srcTextureSubresource);
  7662. VULKAN_INTERNAL_TextureSubresourceTransitionFromDefaultUsage(
  7663. renderer,
  7664. vulkanCommandBuffer,
  7665. VULKAN_TEXTURE_USAGE_MODE_COPY_DESTINATION,
  7666. dstTextureSubresource);
  7667. blit.srcOffsets[0].x = 0;
  7668. blit.srcOffsets[0].y = 0;
  7669. blit.srcOffsets[0].z = depth;
  7670. blit.srcOffsets[1].x = container->header.info.width >> (level - 1);
  7671. blit.srcOffsets[1].y = container->header.info.height >> (level - 1);
  7672. blit.srcOffsets[1].z = depth + 1;
  7673. blit.dstOffsets[0].x = 0;
  7674. blit.dstOffsets[0].y = 0;
  7675. blit.dstOffsets[0].z = depth;
  7676. blit.dstOffsets[1].x = container->header.info.width >> level;
  7677. blit.dstOffsets[1].y = container->header.info.height >> level;
  7678. blit.dstOffsets[1].z = depth + 1;
  7679. blit.srcSubresource.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
  7680. blit.srcSubresource.baseArrayLayer = layer;
  7681. blit.srcSubresource.layerCount = 1;
  7682. blit.srcSubresource.mipLevel = level - 1;
  7683. blit.dstSubresource.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
  7684. blit.dstSubresource.baseArrayLayer = layer;
  7685. blit.dstSubresource.layerCount = 1;
  7686. blit.dstSubresource.mipLevel = level;
  7687. renderer->vkCmdBlitImage(
  7688. vulkanCommandBuffer->commandBuffer,
  7689. container->activeTexture->image,
  7690. VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
  7691. container->activeTexture->image,
  7692. VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
  7693. 1,
  7694. &blit,
  7695. VK_FILTER_LINEAR);
  7696. VULKAN_INTERNAL_TextureSubresourceTransitionToDefaultUsage(
  7697. renderer,
  7698. vulkanCommandBuffer,
  7699. VULKAN_TEXTURE_USAGE_MODE_COPY_SOURCE,
  7700. srcTextureSubresource);
  7701. VULKAN_INTERNAL_TextureSubresourceTransitionToDefaultUsage(
  7702. renderer,
  7703. vulkanCommandBuffer,
  7704. VULKAN_TEXTURE_USAGE_MODE_COPY_DESTINATION,
  7705. dstTextureSubresource);
  7706. VULKAN_INTERNAL_TrackTexture(vulkanCommandBuffer, srcTextureSubresource->parent);
  7707. VULKAN_INTERNAL_TrackTexture(vulkanCommandBuffer, dstTextureSubresource->parent);
  7708. }
  7709. }
  7710. static void VULKAN_EndCopyPass(
  7711. SDL_GPUCommandBuffer *commandBuffer)
  7712. {
  7713. // no-op
  7714. (void)commandBuffer;
  7715. }
  7716. static void VULKAN_Blit(
  7717. SDL_GPUCommandBuffer *commandBuffer,
  7718. const SDL_GPUBlitInfo *info)
  7719. {
  7720. VulkanCommandBuffer *vulkanCommandBuffer = (VulkanCommandBuffer *)commandBuffer;
  7721. VulkanRenderer *renderer = vulkanCommandBuffer->renderer;
  7722. TextureCommonHeader *srcHeader = (TextureCommonHeader *)info->source.texture;
  7723. TextureCommonHeader *dstHeader = (TextureCommonHeader *)info->destination.texture;
  7724. VkImageBlit region;
  7725. Uint32 srcLayer = srcHeader->info.type == SDL_GPU_TEXTURETYPE_3D ? 0 : info->source.layer_or_depth_plane;
  7726. Uint32 srcDepth = srcHeader->info.type == SDL_GPU_TEXTURETYPE_3D ? info->source.layer_or_depth_plane : 0;
  7727. Uint32 dstLayer = dstHeader->info.type == SDL_GPU_TEXTURETYPE_3D ? 0 : info->destination.layer_or_depth_plane;
  7728. Uint32 dstDepth = dstHeader->info.type == SDL_GPU_TEXTURETYPE_3D ? info->destination.layer_or_depth_plane : 0;
  7729. int32_t swap;
  7730. // Using BeginRenderPass to clear because vkCmdClearColorImage requires barriers anyway
  7731. if (info->load_op == SDL_GPU_LOADOP_CLEAR) {
  7732. SDL_GPUColorTargetInfo targetInfo;
  7733. SDL_zero(targetInfo);
  7734. targetInfo.texture = info->destination.texture;
  7735. targetInfo.mip_level = info->destination.mip_level;
  7736. targetInfo.layer_or_depth_plane = info->destination.layer_or_depth_plane;
  7737. targetInfo.load_op = SDL_GPU_LOADOP_CLEAR;
  7738. targetInfo.store_op = SDL_GPU_STOREOP_STORE;
  7739. targetInfo.clear_color = info->clear_color;
  7740. targetInfo.cycle = info->cycle;
  7741. VULKAN_BeginRenderPass(
  7742. commandBuffer,
  7743. &targetInfo,
  7744. 1,
  7745. NULL);
  7746. VULKAN_EndRenderPass(commandBuffer);
  7747. }
  7748. VulkanTextureSubresource *srcSubresource = VULKAN_INTERNAL_FetchTextureSubresource(
  7749. (VulkanTextureContainer *)info->source.texture,
  7750. srcLayer,
  7751. info->source.mip_level);
  7752. VulkanTextureSubresource *dstSubresource = VULKAN_INTERNAL_PrepareTextureSubresourceForWrite(
  7753. renderer,
  7754. vulkanCommandBuffer,
  7755. (VulkanTextureContainer *)info->destination.texture,
  7756. dstLayer,
  7757. info->destination.mip_level,
  7758. info->cycle,
  7759. VULKAN_TEXTURE_USAGE_MODE_COPY_DESTINATION);
  7760. VULKAN_INTERNAL_TextureSubresourceTransitionFromDefaultUsage(
  7761. renderer,
  7762. vulkanCommandBuffer,
  7763. VULKAN_TEXTURE_USAGE_MODE_COPY_SOURCE,
  7764. srcSubresource);
  7765. region.srcSubresource.aspectMask = srcSubresource->parent->aspectFlags;
  7766. region.srcSubresource.baseArrayLayer = srcSubresource->layer;
  7767. region.srcSubresource.layerCount = 1;
  7768. region.srcSubresource.mipLevel = srcSubresource->level;
  7769. region.srcOffsets[0].x = info->source.x;
  7770. region.srcOffsets[0].y = info->source.y;
  7771. region.srcOffsets[0].z = srcDepth;
  7772. region.srcOffsets[1].x = info->source.x + info->source.w;
  7773. region.srcOffsets[1].y = info->source.y + info->source.h;
  7774. region.srcOffsets[1].z = srcDepth + 1;
  7775. if (info->flip_mode & SDL_FLIP_HORIZONTAL) {
  7776. // flip the x positions
  7777. swap = region.srcOffsets[0].x;
  7778. region.srcOffsets[0].x = region.srcOffsets[1].x;
  7779. region.srcOffsets[1].x = swap;
  7780. }
  7781. if (info->flip_mode & SDL_FLIP_VERTICAL) {
  7782. // flip the y positions
  7783. swap = region.srcOffsets[0].y;
  7784. region.srcOffsets[0].y = region.srcOffsets[1].y;
  7785. region.srcOffsets[1].y = swap;
  7786. }
  7787. region.dstSubresource.aspectMask = dstSubresource->parent->aspectFlags;
  7788. region.dstSubresource.baseArrayLayer = dstSubresource->layer;
  7789. region.dstSubresource.layerCount = 1;
  7790. region.dstSubresource.mipLevel = dstSubresource->level;
  7791. region.dstOffsets[0].x = info->destination.x;
  7792. region.dstOffsets[0].y = info->destination.y;
  7793. region.dstOffsets[0].z = dstDepth;
  7794. region.dstOffsets[1].x = info->destination.x + info->destination.w;
  7795. region.dstOffsets[1].y = info->destination.y + info->destination.h;
  7796. region.dstOffsets[1].z = dstDepth + 1;
  7797. renderer->vkCmdBlitImage(
  7798. vulkanCommandBuffer->commandBuffer,
  7799. srcSubresource->parent->image,
  7800. VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
  7801. dstSubresource->parent->image,
  7802. VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
  7803. 1,
  7804. &region,
  7805. SDLToVK_Filter[info->filter]);
  7806. VULKAN_INTERNAL_TextureSubresourceTransitionToDefaultUsage(
  7807. renderer,
  7808. vulkanCommandBuffer,
  7809. VULKAN_TEXTURE_USAGE_MODE_COPY_SOURCE,
  7810. srcSubresource);
  7811. VULKAN_INTERNAL_TextureSubresourceTransitionToDefaultUsage(
  7812. renderer,
  7813. vulkanCommandBuffer,
  7814. VULKAN_TEXTURE_USAGE_MODE_COPY_DESTINATION,
  7815. dstSubresource);
  7816. VULKAN_INTERNAL_TrackTexture(vulkanCommandBuffer, srcSubresource->parent);
  7817. VULKAN_INTERNAL_TrackTexture(vulkanCommandBuffer, dstSubresource->parent);
  7818. }
  7819. static bool VULKAN_INTERNAL_AllocateCommandBuffer(
  7820. VulkanRenderer *renderer,
  7821. VulkanCommandPool *vulkanCommandPool)
  7822. {
  7823. VkCommandBufferAllocateInfo allocateInfo;
  7824. VkResult vulkanResult;
  7825. VkCommandBuffer commandBufferHandle;
  7826. VulkanCommandBuffer *commandBuffer;
  7827. vulkanCommandPool->inactiveCommandBufferCapacity += 1;
  7828. vulkanCommandPool->inactiveCommandBuffers = SDL_realloc(
  7829. vulkanCommandPool->inactiveCommandBuffers,
  7830. sizeof(VulkanCommandBuffer *) *
  7831. vulkanCommandPool->inactiveCommandBufferCapacity);
  7832. allocateInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO;
  7833. allocateInfo.pNext = NULL;
  7834. allocateInfo.commandPool = vulkanCommandPool->commandPool;
  7835. allocateInfo.commandBufferCount = 1;
  7836. allocateInfo.level = VK_COMMAND_BUFFER_LEVEL_PRIMARY;
  7837. vulkanResult = renderer->vkAllocateCommandBuffers(
  7838. renderer->logicalDevice,
  7839. &allocateInfo,
  7840. &commandBufferHandle);
  7841. CHECK_VULKAN_ERROR_AND_RETURN(vulkanResult, vkAllocateCommandBuffers, false);
  7842. commandBuffer = SDL_malloc(sizeof(VulkanCommandBuffer));
  7843. commandBuffer->renderer = renderer;
  7844. commandBuffer->commandPool = vulkanCommandPool;
  7845. commandBuffer->commandBuffer = commandBufferHandle;
  7846. commandBuffer->inFlightFence = VK_NULL_HANDLE;
  7847. // Presentation tracking
  7848. commandBuffer->presentDataCapacity = 1;
  7849. commandBuffer->presentDataCount = 0;
  7850. commandBuffer->presentDatas = SDL_malloc(
  7851. commandBuffer->presentDataCapacity * sizeof(VulkanPresentData));
  7852. commandBuffer->waitSemaphoreCapacity = 1;
  7853. commandBuffer->waitSemaphoreCount = 0;
  7854. commandBuffer->waitSemaphores = SDL_malloc(
  7855. commandBuffer->waitSemaphoreCapacity * sizeof(VkSemaphore));
  7856. commandBuffer->signalSemaphoreCapacity = 1;
  7857. commandBuffer->signalSemaphoreCount = 0;
  7858. commandBuffer->signalSemaphores = SDL_malloc(
  7859. commandBuffer->signalSemaphoreCapacity * sizeof(VkSemaphore));
  7860. // Resource bind tracking
  7861. commandBuffer->needVertexBufferBind = false;
  7862. commandBuffer->needNewVertexResourceDescriptorSet = true;
  7863. commandBuffer->needNewVertexUniformDescriptorSet = true;
  7864. commandBuffer->needNewVertexUniformOffsets = true;
  7865. commandBuffer->needNewFragmentResourceDescriptorSet = true;
  7866. commandBuffer->needNewFragmentUniformDescriptorSet = true;
  7867. commandBuffer->needNewFragmentUniformOffsets = true;
  7868. commandBuffer->needNewComputeReadWriteDescriptorSet = true;
  7869. commandBuffer->needNewComputeReadOnlyDescriptorSet = true;
  7870. commandBuffer->needNewComputeUniformDescriptorSet = true;
  7871. commandBuffer->needNewComputeUniformOffsets = true;
  7872. commandBuffer->vertexResourceDescriptorSet = VK_NULL_HANDLE;
  7873. commandBuffer->vertexUniformDescriptorSet = VK_NULL_HANDLE;
  7874. commandBuffer->fragmentResourceDescriptorSet = VK_NULL_HANDLE;
  7875. commandBuffer->fragmentUniformDescriptorSet = VK_NULL_HANDLE;
  7876. commandBuffer->computeReadOnlyDescriptorSet = VK_NULL_HANDLE;
  7877. commandBuffer->computeReadWriteDescriptorSet = VK_NULL_HANDLE;
  7878. commandBuffer->computeUniformDescriptorSet = VK_NULL_HANDLE;
  7879. // Resource tracking
  7880. commandBuffer->usedBufferCapacity = 4;
  7881. commandBuffer->usedBufferCount = 0;
  7882. commandBuffer->usedBuffers = SDL_malloc(
  7883. commandBuffer->usedBufferCapacity * sizeof(VulkanBuffer *));
  7884. commandBuffer->usedTextureCapacity = 4;
  7885. commandBuffer->usedTextureCount = 0;
  7886. commandBuffer->usedTextures = SDL_malloc(
  7887. commandBuffer->usedTextureCapacity * sizeof(VulkanTexture *));
  7888. commandBuffer->usedSamplerCapacity = 4;
  7889. commandBuffer->usedSamplerCount = 0;
  7890. commandBuffer->usedSamplers = SDL_malloc(
  7891. commandBuffer->usedSamplerCapacity * sizeof(VulkanSampler *));
  7892. commandBuffer->usedGraphicsPipelineCapacity = 4;
  7893. commandBuffer->usedGraphicsPipelineCount = 0;
  7894. commandBuffer->usedGraphicsPipelines = SDL_malloc(
  7895. commandBuffer->usedGraphicsPipelineCapacity * sizeof(VulkanGraphicsPipeline *));
  7896. commandBuffer->usedComputePipelineCapacity = 4;
  7897. commandBuffer->usedComputePipelineCount = 0;
  7898. commandBuffer->usedComputePipelines = SDL_malloc(
  7899. commandBuffer->usedComputePipelineCapacity * sizeof(VulkanComputePipeline *));
  7900. commandBuffer->usedFramebufferCapacity = 4;
  7901. commandBuffer->usedFramebufferCount = 0;
  7902. commandBuffer->usedFramebuffers = SDL_malloc(
  7903. commandBuffer->usedFramebufferCapacity * sizeof(VulkanFramebuffer *));
  7904. commandBuffer->usedUniformBufferCapacity = 4;
  7905. commandBuffer->usedUniformBufferCount = 0;
  7906. commandBuffer->usedUniformBuffers = SDL_malloc(
  7907. commandBuffer->usedUniformBufferCapacity * sizeof(VulkanUniformBuffer *));
  7908. commandBuffer->swapchainRequested = false;
  7909. // Pool it!
  7910. vulkanCommandPool->inactiveCommandBuffers[vulkanCommandPool->inactiveCommandBufferCount] = commandBuffer;
  7911. vulkanCommandPool->inactiveCommandBufferCount += 1;
  7912. return true;
  7913. }
  7914. static VulkanCommandPool *VULKAN_INTERNAL_FetchCommandPool(
  7915. VulkanRenderer *renderer,
  7916. SDL_ThreadID threadID)
  7917. {
  7918. VulkanCommandPool *vulkanCommandPool = NULL;
  7919. VkCommandPoolCreateInfo commandPoolCreateInfo;
  7920. VkResult vulkanResult;
  7921. CommandPoolHashTableKey key;
  7922. key.threadID = threadID;
  7923. bool result = SDL_FindInHashTable(
  7924. renderer->commandPoolHashTable,
  7925. (const void *)&key,
  7926. (const void **)&vulkanCommandPool);
  7927. if (result) {
  7928. return vulkanCommandPool;
  7929. }
  7930. vulkanCommandPool = (VulkanCommandPool *)SDL_malloc(sizeof(VulkanCommandPool));
  7931. commandPoolCreateInfo.sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO;
  7932. commandPoolCreateInfo.pNext = NULL;
  7933. commandPoolCreateInfo.flags = VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT;
  7934. commandPoolCreateInfo.queueFamilyIndex = renderer->queueFamilyIndex;
  7935. vulkanResult = renderer->vkCreateCommandPool(
  7936. renderer->logicalDevice,
  7937. &commandPoolCreateInfo,
  7938. NULL,
  7939. &vulkanCommandPool->commandPool);
  7940. if (vulkanResult != VK_SUCCESS) {
  7941. SDL_free(vulkanCommandPool);
  7942. CHECK_VULKAN_ERROR_AND_RETURN(vulkanResult, vkCreateCommandPool, NULL);
  7943. return NULL;
  7944. }
  7945. vulkanCommandPool->threadID = threadID;
  7946. vulkanCommandPool->inactiveCommandBufferCapacity = 0;
  7947. vulkanCommandPool->inactiveCommandBufferCount = 0;
  7948. vulkanCommandPool->inactiveCommandBuffers = NULL;
  7949. if (!VULKAN_INTERNAL_AllocateCommandBuffer(
  7950. renderer,
  7951. vulkanCommandPool)) {
  7952. VULKAN_INTERNAL_DestroyCommandPool(renderer, vulkanCommandPool);
  7953. return NULL;
  7954. }
  7955. CommandPoolHashTableKey *allocedKey = SDL_malloc(sizeof(CommandPoolHashTableKey));
  7956. allocedKey->threadID = threadID;
  7957. SDL_InsertIntoHashTable(
  7958. renderer->commandPoolHashTable,
  7959. (const void *)allocedKey,
  7960. (const void *)vulkanCommandPool, true);
  7961. return vulkanCommandPool;
  7962. }
  7963. static VulkanCommandBuffer *VULKAN_INTERNAL_GetInactiveCommandBufferFromPool(
  7964. VulkanRenderer *renderer,
  7965. SDL_ThreadID threadID)
  7966. {
  7967. VulkanCommandPool *commandPool =
  7968. VULKAN_INTERNAL_FetchCommandPool(renderer, threadID);
  7969. VulkanCommandBuffer *commandBuffer;
  7970. if (commandPool == NULL) {
  7971. return NULL;
  7972. }
  7973. if (commandPool->inactiveCommandBufferCount == 0) {
  7974. if (!VULKAN_INTERNAL_AllocateCommandBuffer(
  7975. renderer,
  7976. commandPool)) {
  7977. return NULL;
  7978. }
  7979. }
  7980. commandBuffer = commandPool->inactiveCommandBuffers[commandPool->inactiveCommandBufferCount - 1];
  7981. commandPool->inactiveCommandBufferCount -= 1;
  7982. return commandBuffer;
  7983. }
  7984. static SDL_GPUCommandBuffer *VULKAN_AcquireCommandBuffer(
  7985. SDL_GPURenderer *driverData)
  7986. {
  7987. VulkanRenderer *renderer = (VulkanRenderer *)driverData;
  7988. VkResult result;
  7989. Uint32 i;
  7990. SDL_ThreadID threadID = SDL_GetCurrentThreadID();
  7991. SDL_LockMutex(renderer->acquireCommandBufferLock);
  7992. VulkanCommandBuffer *commandBuffer =
  7993. VULKAN_INTERNAL_GetInactiveCommandBufferFromPool(renderer, threadID);
  7994. DescriptorSetCache *descriptorSetCache =
  7995. VULKAN_INTERNAL_AcquireDescriptorSetCache(renderer);
  7996. SDL_UnlockMutex(renderer->acquireCommandBufferLock);
  7997. if (commandBuffer == NULL) {
  7998. return NULL;
  7999. }
  8000. commandBuffer->descriptorSetCache = descriptorSetCache;
  8001. // Reset state
  8002. commandBuffer->currentComputePipeline = NULL;
  8003. commandBuffer->currentGraphicsPipeline = NULL;
  8004. SDL_zeroa(commandBuffer->colorAttachmentSubresources);
  8005. SDL_zeroa(commandBuffer->resolveAttachmentSubresources);
  8006. commandBuffer->depthStencilAttachmentSubresource = NULL;
  8007. commandBuffer->colorAttachmentSubresourceCount = 0;
  8008. commandBuffer->resolveAttachmentSubresourceCount = 0;
  8009. for (i = 0; i < MAX_UNIFORM_BUFFERS_PER_STAGE; i += 1) {
  8010. commandBuffer->vertexUniformBuffers[i] = NULL;
  8011. commandBuffer->fragmentUniformBuffers[i] = NULL;
  8012. commandBuffer->computeUniformBuffers[i] = NULL;
  8013. }
  8014. commandBuffer->needVertexBufferBind = false;
  8015. commandBuffer->needNewVertexResourceDescriptorSet = true;
  8016. commandBuffer->needNewVertexUniformDescriptorSet = true;
  8017. commandBuffer->needNewVertexUniformOffsets = true;
  8018. commandBuffer->needNewFragmentResourceDescriptorSet = true;
  8019. commandBuffer->needNewFragmentUniformDescriptorSet = true;
  8020. commandBuffer->needNewFragmentUniformOffsets = true;
  8021. commandBuffer->needNewComputeReadOnlyDescriptorSet = true;
  8022. commandBuffer->needNewComputeUniformDescriptorSet = true;
  8023. commandBuffer->needNewComputeUniformOffsets = true;
  8024. commandBuffer->vertexResourceDescriptorSet = VK_NULL_HANDLE;
  8025. commandBuffer->vertexUniformDescriptorSet = VK_NULL_HANDLE;
  8026. commandBuffer->fragmentResourceDescriptorSet = VK_NULL_HANDLE;
  8027. commandBuffer->fragmentUniformDescriptorSet = VK_NULL_HANDLE;
  8028. commandBuffer->computeReadOnlyDescriptorSet = VK_NULL_HANDLE;
  8029. commandBuffer->computeReadWriteDescriptorSet = VK_NULL_HANDLE;
  8030. commandBuffer->computeUniformDescriptorSet = VK_NULL_HANDLE;
  8031. SDL_zeroa(commandBuffer->vertexBuffers);
  8032. SDL_zeroa(commandBuffer->vertexBufferOffsets);
  8033. commandBuffer->vertexBufferCount = 0;
  8034. SDL_zeroa(commandBuffer->vertexSamplerTextureViewBindings);
  8035. SDL_zeroa(commandBuffer->vertexSamplerBindings);
  8036. SDL_zeroa(commandBuffer->vertexStorageTextureViewBindings);
  8037. SDL_zeroa(commandBuffer->vertexStorageBufferBindings);
  8038. SDL_zeroa(commandBuffer->fragmentSamplerTextureViewBindings);
  8039. SDL_zeroa(commandBuffer->fragmentSamplerBindings);
  8040. SDL_zeroa(commandBuffer->fragmentStorageTextureViewBindings);
  8041. SDL_zeroa(commandBuffer->fragmentStorageBufferBindings);
  8042. SDL_zeroa(commandBuffer->readWriteComputeStorageTextureSubresources);
  8043. commandBuffer->readWriteComputeStorageTextureSubresourceCount = 0;
  8044. SDL_zeroa(commandBuffer->readWriteComputeStorageBuffers);
  8045. SDL_zeroa(commandBuffer->computeSamplerTextureViewBindings);
  8046. SDL_zeroa(commandBuffer->computeSamplerBindings);
  8047. SDL_zeroa(commandBuffer->readOnlyComputeStorageTextureViewBindings);
  8048. SDL_zeroa(commandBuffer->readOnlyComputeStorageBufferBindings);
  8049. SDL_zeroa(commandBuffer->readOnlyComputeStorageTextures);
  8050. SDL_zeroa(commandBuffer->readOnlyComputeStorageBuffers);
  8051. commandBuffer->autoReleaseFence = true;
  8052. commandBuffer->swapchainRequested = false;
  8053. commandBuffer->isDefrag = 0;
  8054. /* Reset the command buffer here to avoid resets being called
  8055. * from a separate thread than where the command buffer was acquired
  8056. */
  8057. result = renderer->vkResetCommandBuffer(
  8058. commandBuffer->commandBuffer,
  8059. VK_COMMAND_BUFFER_RESET_RELEASE_RESOURCES_BIT);
  8060. CHECK_VULKAN_ERROR_AND_RETURN(result, vkResetCommandBuffer, NULL);
  8061. if (!VULKAN_INTERNAL_BeginCommandBuffer(renderer, commandBuffer)) {
  8062. return NULL;
  8063. }
  8064. return (SDL_GPUCommandBuffer *)commandBuffer;
  8065. }
  8066. static bool VULKAN_QueryFence(
  8067. SDL_GPURenderer *driverData,
  8068. SDL_GPUFence *fence)
  8069. {
  8070. VulkanRenderer *renderer = (VulkanRenderer *)driverData;
  8071. VkResult result;
  8072. result = renderer->vkGetFenceStatus(
  8073. renderer->logicalDevice,
  8074. ((VulkanFenceHandle *)fence)->fence);
  8075. if (result == VK_SUCCESS) {
  8076. return true;
  8077. } else if (result == VK_NOT_READY) {
  8078. return false;
  8079. } else {
  8080. SET_ERROR_AND_RETURN("vkGetFenceStatus: %s", VkErrorMessages(result), false);
  8081. }
  8082. }
  8083. static void VULKAN_INTERNAL_ReturnFenceToPool(
  8084. VulkanRenderer *renderer,
  8085. VulkanFenceHandle *fenceHandle)
  8086. {
  8087. SDL_LockMutex(renderer->fencePool.lock);
  8088. EXPAND_ARRAY_IF_NEEDED(
  8089. renderer->fencePool.availableFences,
  8090. VulkanFenceHandle *,
  8091. renderer->fencePool.availableFenceCount + 1,
  8092. renderer->fencePool.availableFenceCapacity,
  8093. renderer->fencePool.availableFenceCapacity * 2);
  8094. renderer->fencePool.availableFences[renderer->fencePool.availableFenceCount] = fenceHandle;
  8095. renderer->fencePool.availableFenceCount += 1;
  8096. SDL_UnlockMutex(renderer->fencePool.lock);
  8097. }
  8098. static void VULKAN_ReleaseFence(
  8099. SDL_GPURenderer *driverData,
  8100. SDL_GPUFence *fence)
  8101. {
  8102. VulkanFenceHandle *handle = (VulkanFenceHandle *)fence;
  8103. if (SDL_AtomicDecRef(&handle->referenceCount)) {
  8104. VULKAN_INTERNAL_ReturnFenceToPool((VulkanRenderer *)driverData, handle);
  8105. }
  8106. }
  8107. static WindowData *VULKAN_INTERNAL_FetchWindowData(
  8108. SDL_Window *window)
  8109. {
  8110. SDL_PropertiesID properties = SDL_GetWindowProperties(window);
  8111. return (WindowData *)SDL_GetPointerProperty(properties, WINDOW_PROPERTY_DATA, NULL);
  8112. }
  8113. static bool VULKAN_INTERNAL_OnWindowResize(void *userdata, SDL_Event *e)
  8114. {
  8115. SDL_Window *w = (SDL_Window *)userdata;
  8116. WindowData *data;
  8117. if (e->type == SDL_EVENT_WINDOW_PIXEL_SIZE_CHANGED && e->window.windowID == SDL_GetWindowID(w)) {
  8118. data = VULKAN_INTERNAL_FetchWindowData(w);
  8119. data->needsSwapchainRecreate = true;
  8120. data->swapchainCreateWidth = e->window.data1;
  8121. data->swapchainCreateHeight = e->window.data2;
  8122. }
  8123. #ifdef SDL_PLATFORM_ANDROID
  8124. if (e->type == SDL_EVENT_DID_ENTER_BACKGROUND) {
  8125. data = VULKAN_INTERNAL_FetchWindowData(w);
  8126. data->needsSwapchainRecreate = true;
  8127. }
  8128. #endif
  8129. return true;
  8130. }
  8131. static bool VULKAN_SupportsSwapchainComposition(
  8132. SDL_GPURenderer *driverData,
  8133. SDL_Window *window,
  8134. SDL_GPUSwapchainComposition swapchainComposition)
  8135. {
  8136. VulkanRenderer *renderer = (VulkanRenderer *)driverData;
  8137. WindowData *windowData = VULKAN_INTERNAL_FetchWindowData(window);
  8138. VkSurfaceKHR surface;
  8139. SwapchainSupportDetails supportDetails;
  8140. bool result = false;
  8141. if (windowData == NULL) {
  8142. SET_STRING_ERROR_AND_RETURN("Must claim window before querying swapchain composition support!", false);
  8143. }
  8144. surface = windowData->surface;
  8145. if (!surface) {
  8146. SET_STRING_ERROR_AND_RETURN("Window has no Vulkan surface", false);
  8147. }
  8148. if (VULKAN_INTERNAL_QuerySwapchainSupport(
  8149. renderer,
  8150. renderer->physicalDevice,
  8151. surface,
  8152. &supportDetails)) {
  8153. result = VULKAN_INTERNAL_VerifySwapSurfaceFormat(
  8154. SwapchainCompositionToFormat[swapchainComposition],
  8155. SwapchainCompositionToColorSpace[swapchainComposition],
  8156. supportDetails.formats,
  8157. supportDetails.formatsLength);
  8158. if (!result) {
  8159. // Let's try again with the fallback format...
  8160. result = VULKAN_INTERNAL_VerifySwapSurfaceFormat(
  8161. SwapchainCompositionToFallbackFormat[swapchainComposition],
  8162. SwapchainCompositionToColorSpace[swapchainComposition],
  8163. supportDetails.formats,
  8164. supportDetails.formatsLength);
  8165. }
  8166. SDL_free(supportDetails.formats);
  8167. SDL_free(supportDetails.presentModes);
  8168. }
  8169. return result;
  8170. }
  8171. static bool VULKAN_SupportsPresentMode(
  8172. SDL_GPURenderer *driverData,
  8173. SDL_Window *window,
  8174. SDL_GPUPresentMode presentMode)
  8175. {
  8176. VulkanRenderer *renderer = (VulkanRenderer *)driverData;
  8177. WindowData *windowData = VULKAN_INTERNAL_FetchWindowData(window);
  8178. VkSurfaceKHR surface;
  8179. SwapchainSupportDetails supportDetails;
  8180. bool result = false;
  8181. if (windowData == NULL) {
  8182. SET_STRING_ERROR_AND_RETURN("Must claim window before querying present mode support!", false);
  8183. }
  8184. surface = windowData->surface;
  8185. if (!surface) {
  8186. SET_STRING_ERROR_AND_RETURN("Window has no Vulkan surface", false);
  8187. }
  8188. if (VULKAN_INTERNAL_QuerySwapchainSupport(
  8189. renderer,
  8190. renderer->physicalDevice,
  8191. surface,
  8192. &supportDetails)) {
  8193. result = VULKAN_INTERNAL_VerifySwapPresentMode(
  8194. SDLToVK_PresentMode[presentMode],
  8195. supportDetails.presentModes,
  8196. supportDetails.presentModesLength);
  8197. SDL_free(supportDetails.formats);
  8198. SDL_free(supportDetails.presentModes);
  8199. }
  8200. return result;
  8201. }
  8202. static bool VULKAN_ClaimWindow(
  8203. SDL_GPURenderer *driverData,
  8204. SDL_Window *window)
  8205. {
  8206. VulkanRenderer *renderer = (VulkanRenderer *)driverData;
  8207. WindowData *windowData = VULKAN_INTERNAL_FetchWindowData(window);
  8208. if (windowData == NULL) {
  8209. windowData = SDL_calloc(1, sizeof(WindowData));
  8210. windowData->window = window;
  8211. windowData->presentMode = SDL_GPU_PRESENTMODE_VSYNC;
  8212. windowData->swapchainComposition = SDL_GPU_SWAPCHAINCOMPOSITION_SDR;
  8213. // On non-Apple platforms the swapchain capability currentExtent can be different from the window,
  8214. // so we have to query the window size.
  8215. #ifndef SDL_PLATFORM_APPLE
  8216. int w, h;
  8217. SDL_SyncWindow(window);
  8218. SDL_GetWindowSizeInPixels(window, &w, &h);
  8219. windowData->swapchainCreateWidth = w;
  8220. windowData->swapchainCreateHeight = h;
  8221. #endif
  8222. Uint32 createSwapchainResult = VULKAN_INTERNAL_CreateSwapchain(renderer, windowData);
  8223. if (createSwapchainResult == 1) {
  8224. SDL_SetPointerProperty(SDL_GetWindowProperties(window), WINDOW_PROPERTY_DATA, windowData);
  8225. SDL_LockMutex(renderer->windowLock);
  8226. if (renderer->claimedWindowCount >= renderer->claimedWindowCapacity) {
  8227. renderer->claimedWindowCapacity *= 2;
  8228. renderer->claimedWindows = SDL_realloc(
  8229. renderer->claimedWindows,
  8230. renderer->claimedWindowCapacity * sizeof(WindowData *));
  8231. }
  8232. renderer->claimedWindows[renderer->claimedWindowCount] = windowData;
  8233. renderer->claimedWindowCount += 1;
  8234. SDL_UnlockMutex(renderer->windowLock);
  8235. SDL_AddEventWatch(VULKAN_INTERNAL_OnWindowResize, window);
  8236. return true;
  8237. } else if (createSwapchainResult == VULKAN_INTERNAL_TRY_AGAIN) {
  8238. windowData->needsSwapchainRecreate = true;
  8239. return true;
  8240. } else {
  8241. SDL_free(windowData);
  8242. return false;
  8243. }
  8244. } else {
  8245. SET_STRING_ERROR_AND_RETURN("Window already claimed!", false);
  8246. }
  8247. }
  8248. static void VULKAN_ReleaseWindow(
  8249. SDL_GPURenderer *driverData,
  8250. SDL_Window *window)
  8251. {
  8252. VulkanRenderer *renderer = (VulkanRenderer *)driverData;
  8253. WindowData *windowData = VULKAN_INTERNAL_FetchWindowData(window);
  8254. Uint32 i;
  8255. if (windowData == NULL) {
  8256. return;
  8257. }
  8258. VULKAN_Wait(driverData);
  8259. for (i = 0; i < MAX_FRAMES_IN_FLIGHT; i += 1) {
  8260. if (windowData->inFlightFences[i] != NULL) {
  8261. VULKAN_ReleaseFence(
  8262. driverData,
  8263. windowData->inFlightFences[i]);
  8264. }
  8265. }
  8266. VULKAN_INTERNAL_DestroySwapchain(
  8267. (VulkanRenderer *)driverData,
  8268. windowData);
  8269. SDL_LockMutex(renderer->windowLock);
  8270. for (i = 0; i < renderer->claimedWindowCount; i += 1) {
  8271. if (renderer->claimedWindows[i]->window == window) {
  8272. renderer->claimedWindows[i] = renderer->claimedWindows[renderer->claimedWindowCount - 1];
  8273. renderer->claimedWindowCount -= 1;
  8274. break;
  8275. }
  8276. }
  8277. SDL_UnlockMutex(renderer->windowLock);
  8278. SDL_free(windowData);
  8279. SDL_ClearProperty(SDL_GetWindowProperties(window), WINDOW_PROPERTY_DATA);
  8280. SDL_RemoveEventWatch(VULKAN_INTERNAL_OnWindowResize, window);
  8281. }
  8282. static Uint32 VULKAN_INTERNAL_RecreateSwapchain(
  8283. VulkanRenderer *renderer,
  8284. WindowData *windowData)
  8285. {
  8286. Uint32 i;
  8287. if (!VULKAN_Wait((SDL_GPURenderer *)renderer)) {
  8288. return false;
  8289. }
  8290. for (i = 0; i < MAX_FRAMES_IN_FLIGHT; i += 1) {
  8291. if (windowData->inFlightFences[i] != NULL) {
  8292. VULKAN_ReleaseFence(
  8293. (SDL_GPURenderer *)renderer,
  8294. windowData->inFlightFences[i]);
  8295. windowData->inFlightFences[i] = NULL;
  8296. }
  8297. }
  8298. #ifdef SDL_VIDEO_DRIVER_PRIVATE
  8299. // Private platforms also invalidate the window, so don't try to preserve the surface/swapchain
  8300. VULKAN_INTERNAL_DestroySwapchain(renderer, windowData);
  8301. #else
  8302. VULKAN_INTERNAL_DestroySwapchainImage(renderer, windowData);
  8303. #endif
  8304. return VULKAN_INTERNAL_CreateSwapchain(renderer, windowData);
  8305. }
  8306. static bool VULKAN_WaitForSwapchain(
  8307. SDL_GPURenderer *driverData,
  8308. SDL_Window *window)
  8309. {
  8310. VulkanRenderer *renderer = (VulkanRenderer *)driverData;
  8311. WindowData *windowData = VULKAN_INTERNAL_FetchWindowData(window);
  8312. if (windowData == NULL) {
  8313. SET_STRING_ERROR_AND_RETURN("Cannot wait for a swapchain from an unclaimed window!", false);
  8314. }
  8315. if (windowData->inFlightFences[windowData->frameCounter] != NULL) {
  8316. if (!VULKAN_WaitForFences(
  8317. driverData,
  8318. true,
  8319. &windowData->inFlightFences[windowData->frameCounter],
  8320. 1)) {
  8321. return false;
  8322. }
  8323. }
  8324. return true;
  8325. }
  8326. static bool VULKAN_INTERNAL_AcquireSwapchainTexture(
  8327. bool block,
  8328. SDL_GPUCommandBuffer *commandBuffer,
  8329. SDL_Window *window,
  8330. SDL_GPUTexture **swapchainTexture,
  8331. Uint32 *swapchainTextureWidth,
  8332. Uint32 *swapchainTextureHeight)
  8333. {
  8334. VulkanCommandBuffer *vulkanCommandBuffer = (VulkanCommandBuffer *)commandBuffer;
  8335. VulkanRenderer *renderer = vulkanCommandBuffer->renderer;
  8336. Uint32 swapchainImageIndex;
  8337. WindowData *windowData;
  8338. VkResult acquireResult = VK_SUCCESS;
  8339. VulkanTextureContainer *swapchainTextureContainer = NULL;
  8340. VulkanPresentData *presentData;
  8341. *swapchainTexture = NULL;
  8342. if (swapchainTextureWidth) {
  8343. *swapchainTextureWidth = 0;
  8344. }
  8345. if (swapchainTextureHeight) {
  8346. *swapchainTextureHeight = 0;
  8347. }
  8348. windowData = VULKAN_INTERNAL_FetchWindowData(window);
  8349. if (windowData == NULL) {
  8350. SET_STRING_ERROR_AND_RETURN("Cannot acquire a swapchain texture from an unclaimed window!", false);
  8351. }
  8352. // The command buffer is flagged for cleanup when the swapchain is requested as a cleanup timing mechanism
  8353. vulkanCommandBuffer->swapchainRequested = true;
  8354. if (window->flags & SDL_WINDOW_HIDDEN) {
  8355. // Edge case, texture is filled in with NULL but not an error
  8356. return true;
  8357. }
  8358. // If window data marked as needing swapchain recreate, try to recreate
  8359. if (windowData->needsSwapchainRecreate) {
  8360. Uint32 recreateSwapchainResult = VULKAN_INTERNAL_RecreateSwapchain(renderer, windowData);
  8361. if (!recreateSwapchainResult) {
  8362. return false;
  8363. } else if (recreateSwapchainResult == VULKAN_INTERNAL_TRY_AGAIN) {
  8364. // Edge case, texture is filled in with NULL but not an error
  8365. if (windowData->inFlightFences[windowData->frameCounter] != NULL) {
  8366. VULKAN_ReleaseFence(
  8367. (SDL_GPURenderer *)renderer,
  8368. windowData->inFlightFences[windowData->frameCounter]);
  8369. windowData->inFlightFences[windowData->frameCounter] = NULL;
  8370. }
  8371. return true;
  8372. }
  8373. }
  8374. if (windowData->inFlightFences[windowData->frameCounter] != NULL) {
  8375. if (block) {
  8376. // If we are blocking, just wait for the fence!
  8377. if (!VULKAN_WaitForFences(
  8378. (SDL_GPURenderer *)renderer,
  8379. true,
  8380. &windowData->inFlightFences[windowData->frameCounter],
  8381. 1)) {
  8382. return false;
  8383. }
  8384. } else {
  8385. // If we are not blocking and the least recent fence is not signaled,
  8386. // return true to indicate that there is no error but rendering should be skipped.
  8387. if (!VULKAN_QueryFence(
  8388. (SDL_GPURenderer *)renderer,
  8389. windowData->inFlightFences[windowData->frameCounter])) {
  8390. return true;
  8391. }
  8392. }
  8393. VULKAN_ReleaseFence(
  8394. (SDL_GPURenderer *)renderer,
  8395. windowData->inFlightFences[windowData->frameCounter]);
  8396. windowData->inFlightFences[windowData->frameCounter] = NULL;
  8397. }
  8398. // Finally, try to acquire!
  8399. while (true) {
  8400. acquireResult = renderer->vkAcquireNextImageKHR(
  8401. renderer->logicalDevice,
  8402. windowData->swapchain,
  8403. SDL_MAX_UINT64,
  8404. windowData->imageAvailableSemaphore[windowData->frameCounter],
  8405. VK_NULL_HANDLE,
  8406. &swapchainImageIndex);
  8407. if (acquireResult == VK_SUCCESS || acquireResult == VK_SUBOPTIMAL_KHR) {
  8408. break; // we got the next image!
  8409. }
  8410. // If acquisition is invalid, let's try to recreate
  8411. Uint32 recreateSwapchainResult = VULKAN_INTERNAL_RecreateSwapchain(renderer, windowData);
  8412. if (!recreateSwapchainResult) {
  8413. return false;
  8414. } else if (recreateSwapchainResult == VULKAN_INTERNAL_TRY_AGAIN) {
  8415. // Edge case, texture is filled in with NULL but not an error
  8416. return true;
  8417. }
  8418. }
  8419. if (swapchainTextureWidth) {
  8420. *swapchainTextureWidth = windowData->width;
  8421. }
  8422. if (swapchainTextureHeight) {
  8423. *swapchainTextureHeight = windowData->height;
  8424. }
  8425. swapchainTextureContainer = &windowData->textureContainers[swapchainImageIndex];
  8426. // We need a special execution dependency with pWaitDstStageMask or image transition can start before acquire finishes
  8427. VkImageMemoryBarrier imageBarrier;
  8428. imageBarrier.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER;
  8429. imageBarrier.pNext = NULL;
  8430. imageBarrier.srcAccessMask = 0;
  8431. imageBarrier.dstAccessMask = VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT;
  8432. imageBarrier.oldLayout = VK_IMAGE_LAYOUT_UNDEFINED;
  8433. imageBarrier.newLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
  8434. imageBarrier.srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
  8435. imageBarrier.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
  8436. imageBarrier.image = swapchainTextureContainer->activeTexture->image;
  8437. imageBarrier.subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
  8438. imageBarrier.subresourceRange.baseMipLevel = 0;
  8439. imageBarrier.subresourceRange.levelCount = 1;
  8440. imageBarrier.subresourceRange.baseArrayLayer = 0;
  8441. imageBarrier.subresourceRange.layerCount = 1;
  8442. renderer->vkCmdPipelineBarrier(
  8443. vulkanCommandBuffer->commandBuffer,
  8444. VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT,
  8445. VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT,
  8446. 0,
  8447. 0,
  8448. NULL,
  8449. 0,
  8450. NULL,
  8451. 1,
  8452. &imageBarrier);
  8453. // Set up present struct
  8454. if (vulkanCommandBuffer->presentDataCount == vulkanCommandBuffer->presentDataCapacity) {
  8455. vulkanCommandBuffer->presentDataCapacity += 1;
  8456. vulkanCommandBuffer->presentDatas = SDL_realloc(
  8457. vulkanCommandBuffer->presentDatas,
  8458. vulkanCommandBuffer->presentDataCapacity * sizeof(VulkanPresentData));
  8459. }
  8460. presentData = &vulkanCommandBuffer->presentDatas[vulkanCommandBuffer->presentDataCount];
  8461. vulkanCommandBuffer->presentDataCount += 1;
  8462. presentData->windowData = windowData;
  8463. presentData->swapchainImageIndex = swapchainImageIndex;
  8464. // Set up present semaphores
  8465. if (vulkanCommandBuffer->waitSemaphoreCount == vulkanCommandBuffer->waitSemaphoreCapacity) {
  8466. vulkanCommandBuffer->waitSemaphoreCapacity += 1;
  8467. vulkanCommandBuffer->waitSemaphores = SDL_realloc(
  8468. vulkanCommandBuffer->waitSemaphores,
  8469. vulkanCommandBuffer->waitSemaphoreCapacity * sizeof(VkSemaphore));
  8470. }
  8471. vulkanCommandBuffer->waitSemaphores[vulkanCommandBuffer->waitSemaphoreCount] =
  8472. windowData->imageAvailableSemaphore[windowData->frameCounter];
  8473. vulkanCommandBuffer->waitSemaphoreCount += 1;
  8474. if (vulkanCommandBuffer->signalSemaphoreCount == vulkanCommandBuffer->signalSemaphoreCapacity) {
  8475. vulkanCommandBuffer->signalSemaphoreCapacity += 1;
  8476. vulkanCommandBuffer->signalSemaphores = SDL_realloc(
  8477. vulkanCommandBuffer->signalSemaphores,
  8478. vulkanCommandBuffer->signalSemaphoreCapacity * sizeof(VkSemaphore));
  8479. }
  8480. vulkanCommandBuffer->signalSemaphores[vulkanCommandBuffer->signalSemaphoreCount] =
  8481. windowData->renderFinishedSemaphore[swapchainImageIndex];
  8482. vulkanCommandBuffer->signalSemaphoreCount += 1;
  8483. *swapchainTexture = (SDL_GPUTexture *)swapchainTextureContainer;
  8484. return true;
  8485. }
  8486. static bool VULKAN_AcquireSwapchainTexture(
  8487. SDL_GPUCommandBuffer *command_buffer,
  8488. SDL_Window *window,
  8489. SDL_GPUTexture **swapchain_texture,
  8490. Uint32 *swapchain_texture_width,
  8491. Uint32 *swapchain_texture_height
  8492. ) {
  8493. return VULKAN_INTERNAL_AcquireSwapchainTexture(
  8494. false,
  8495. command_buffer,
  8496. window,
  8497. swapchain_texture,
  8498. swapchain_texture_width,
  8499. swapchain_texture_height);
  8500. }
  8501. static bool VULKAN_WaitAndAcquireSwapchainTexture(
  8502. SDL_GPUCommandBuffer *command_buffer,
  8503. SDL_Window *window,
  8504. SDL_GPUTexture **swapchain_texture,
  8505. Uint32 *swapchain_texture_width,
  8506. Uint32 *swapchain_texture_height
  8507. ) {
  8508. return VULKAN_INTERNAL_AcquireSwapchainTexture(
  8509. true,
  8510. command_buffer,
  8511. window,
  8512. swapchain_texture,
  8513. swapchain_texture_width,
  8514. swapchain_texture_height);
  8515. }
  8516. static SDL_GPUTextureFormat VULKAN_GetSwapchainTextureFormat(
  8517. SDL_GPURenderer *driverData,
  8518. SDL_Window *window)
  8519. {
  8520. VulkanRenderer *renderer = (VulkanRenderer *)driverData;
  8521. WindowData *windowData = VULKAN_INTERNAL_FetchWindowData(window);
  8522. if (windowData == NULL) {
  8523. SET_STRING_ERROR_AND_RETURN("Cannot get swapchain format, window has not been claimed!", SDL_GPU_TEXTUREFORMAT_INVALID);
  8524. }
  8525. return SwapchainCompositionToSDLFormat(
  8526. windowData->swapchainComposition,
  8527. windowData->usingFallbackFormat);
  8528. }
  8529. static bool VULKAN_SetSwapchainParameters(
  8530. SDL_GPURenderer *driverData,
  8531. SDL_Window *window,
  8532. SDL_GPUSwapchainComposition swapchainComposition,
  8533. SDL_GPUPresentMode presentMode)
  8534. {
  8535. VulkanRenderer *renderer = (VulkanRenderer *)driverData;
  8536. WindowData *windowData = VULKAN_INTERNAL_FetchWindowData(window);
  8537. if (windowData == NULL) {
  8538. SET_STRING_ERROR_AND_RETURN("Cannot set swapchain parameters on unclaimed window!", false);
  8539. }
  8540. if (!VULKAN_SupportsSwapchainComposition(driverData, window, swapchainComposition)) {
  8541. SET_STRING_ERROR_AND_RETURN("Swapchain composition not supported!", false);
  8542. }
  8543. if (!VULKAN_SupportsPresentMode(driverData, window, presentMode)) {
  8544. SET_STRING_ERROR_AND_RETURN("Present mode not supported!", false);
  8545. }
  8546. windowData->presentMode = presentMode;
  8547. windowData->swapchainComposition = swapchainComposition;
  8548. Uint32 recreateSwapchainResult = VULKAN_INTERNAL_RecreateSwapchain(renderer, windowData);
  8549. if (!recreateSwapchainResult) {
  8550. return false;
  8551. } else if (recreateSwapchainResult == VULKAN_INTERNAL_TRY_AGAIN) {
  8552. // Edge case, swapchain extent is (0, 0) but this is not an error
  8553. windowData->needsSwapchainRecreate = true;
  8554. return true;
  8555. }
  8556. return true;
  8557. }
  8558. static bool VULKAN_SetAllowedFramesInFlight(
  8559. SDL_GPURenderer *driverData,
  8560. Uint32 allowedFramesInFlight)
  8561. {
  8562. VulkanRenderer *renderer = (VulkanRenderer *)driverData;
  8563. renderer->allowedFramesInFlight = allowedFramesInFlight;
  8564. for (Uint32 i = 0; i < renderer->claimedWindowCount; i += 1) {
  8565. WindowData *windowData = renderer->claimedWindows[i];
  8566. Uint32 recreateResult = VULKAN_INTERNAL_RecreateSwapchain(renderer, windowData);
  8567. if (!recreateResult) {
  8568. return false;
  8569. } else if (recreateResult == VULKAN_INTERNAL_TRY_AGAIN) {
  8570. // Edge case, swapchain extent is (0, 0) but this is not an error
  8571. windowData->needsSwapchainRecreate = true;
  8572. }
  8573. }
  8574. return true;
  8575. }
  8576. // Submission structure
  8577. static VulkanFenceHandle *VULKAN_INTERNAL_AcquireFenceFromPool(
  8578. VulkanRenderer *renderer)
  8579. {
  8580. VulkanFenceHandle *handle;
  8581. VkFenceCreateInfo fenceCreateInfo;
  8582. VkFence fence;
  8583. VkResult vulkanResult;
  8584. if (renderer->fencePool.availableFenceCount == 0) {
  8585. // Create fence
  8586. fenceCreateInfo.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO;
  8587. fenceCreateInfo.pNext = NULL;
  8588. fenceCreateInfo.flags = 0;
  8589. vulkanResult = renderer->vkCreateFence(
  8590. renderer->logicalDevice,
  8591. &fenceCreateInfo,
  8592. NULL,
  8593. &fence);
  8594. CHECK_VULKAN_ERROR_AND_RETURN(vulkanResult, vkCreateFence, NULL);
  8595. handle = SDL_malloc(sizeof(VulkanFenceHandle));
  8596. handle->fence = fence;
  8597. SDL_SetAtomicInt(&handle->referenceCount, 0);
  8598. return handle;
  8599. }
  8600. SDL_LockMutex(renderer->fencePool.lock);
  8601. handle = renderer->fencePool.availableFences[renderer->fencePool.availableFenceCount - 1];
  8602. renderer->fencePool.availableFenceCount -= 1;
  8603. vulkanResult = renderer->vkResetFences(
  8604. renderer->logicalDevice,
  8605. 1,
  8606. &handle->fence);
  8607. SDL_UnlockMutex(renderer->fencePool.lock);
  8608. CHECK_VULKAN_ERROR_AND_RETURN(vulkanResult, vkResetFences, NULL);
  8609. return handle;
  8610. }
  8611. static void VULKAN_INTERNAL_PerformPendingDestroys(
  8612. VulkanRenderer *renderer)
  8613. {
  8614. SDL_LockMutex(renderer->disposeLock);
  8615. for (Sint32 i = renderer->texturesToDestroyCount - 1; i >= 0; i -= 1) {
  8616. if (SDL_GetAtomicInt(&renderer->texturesToDestroy[i]->referenceCount) == 0) {
  8617. VULKAN_INTERNAL_DestroyTexture(
  8618. renderer,
  8619. renderer->texturesToDestroy[i]);
  8620. renderer->texturesToDestroy[i] = renderer->texturesToDestroy[renderer->texturesToDestroyCount - 1];
  8621. renderer->texturesToDestroyCount -= 1;
  8622. }
  8623. }
  8624. for (Sint32 i = renderer->buffersToDestroyCount - 1; i >= 0; i -= 1) {
  8625. if (SDL_GetAtomicInt(&renderer->buffersToDestroy[i]->referenceCount) == 0) {
  8626. VULKAN_INTERNAL_DestroyBuffer(
  8627. renderer,
  8628. renderer->buffersToDestroy[i]);
  8629. renderer->buffersToDestroy[i] = renderer->buffersToDestroy[renderer->buffersToDestroyCount - 1];
  8630. renderer->buffersToDestroyCount -= 1;
  8631. }
  8632. }
  8633. for (Sint32 i = renderer->graphicsPipelinesToDestroyCount - 1; i >= 0; i -= 1) {
  8634. if (SDL_GetAtomicInt(&renderer->graphicsPipelinesToDestroy[i]->referenceCount) == 0) {
  8635. VULKAN_INTERNAL_DestroyGraphicsPipeline(
  8636. renderer,
  8637. renderer->graphicsPipelinesToDestroy[i]);
  8638. renderer->graphicsPipelinesToDestroy[i] = renderer->graphicsPipelinesToDestroy[renderer->graphicsPipelinesToDestroyCount - 1];
  8639. renderer->graphicsPipelinesToDestroyCount -= 1;
  8640. }
  8641. }
  8642. for (Sint32 i = renderer->computePipelinesToDestroyCount - 1; i >= 0; i -= 1) {
  8643. if (SDL_GetAtomicInt(&renderer->computePipelinesToDestroy[i]->referenceCount) == 0) {
  8644. VULKAN_INTERNAL_DestroyComputePipeline(
  8645. renderer,
  8646. renderer->computePipelinesToDestroy[i]);
  8647. renderer->computePipelinesToDestroy[i] = renderer->computePipelinesToDestroy[renderer->computePipelinesToDestroyCount - 1];
  8648. renderer->computePipelinesToDestroyCount -= 1;
  8649. }
  8650. }
  8651. for (Sint32 i = renderer->shadersToDestroyCount - 1; i >= 0; i -= 1) {
  8652. if (SDL_GetAtomicInt(&renderer->shadersToDestroy[i]->referenceCount) == 0) {
  8653. VULKAN_INTERNAL_DestroyShader(
  8654. renderer,
  8655. renderer->shadersToDestroy[i]);
  8656. renderer->shadersToDestroy[i] = renderer->shadersToDestroy[renderer->shadersToDestroyCount - 1];
  8657. renderer->shadersToDestroyCount -= 1;
  8658. }
  8659. }
  8660. for (Sint32 i = renderer->samplersToDestroyCount - 1; i >= 0; i -= 1) {
  8661. if (SDL_GetAtomicInt(&renderer->samplersToDestroy[i]->referenceCount) == 0) {
  8662. VULKAN_INTERNAL_DestroySampler(
  8663. renderer,
  8664. renderer->samplersToDestroy[i]);
  8665. renderer->samplersToDestroy[i] = renderer->samplersToDestroy[renderer->samplersToDestroyCount - 1];
  8666. renderer->samplersToDestroyCount -= 1;
  8667. }
  8668. }
  8669. for (Sint32 i = renderer->framebuffersToDestroyCount - 1; i >= 0; i -= 1) {
  8670. if (SDL_GetAtomicInt(&renderer->framebuffersToDestroy[i]->referenceCount) == 0) {
  8671. VULKAN_INTERNAL_DestroyFramebuffer(
  8672. renderer,
  8673. renderer->framebuffersToDestroy[i]);
  8674. renderer->framebuffersToDestroy[i] = renderer->framebuffersToDestroy[renderer->framebuffersToDestroyCount - 1];
  8675. renderer->framebuffersToDestroyCount -= 1;
  8676. }
  8677. }
  8678. SDL_UnlockMutex(renderer->disposeLock);
  8679. }
  8680. static void VULKAN_INTERNAL_CleanCommandBuffer(
  8681. VulkanRenderer *renderer,
  8682. VulkanCommandBuffer *commandBuffer,
  8683. bool cancel)
  8684. {
  8685. if (commandBuffer->autoReleaseFence) {
  8686. VULKAN_ReleaseFence(
  8687. (SDL_GPURenderer *)renderer,
  8688. (SDL_GPUFence *)commandBuffer->inFlightFence);
  8689. commandBuffer->inFlightFence = NULL;
  8690. }
  8691. // Uniform buffers are now available
  8692. SDL_LockMutex(renderer->acquireUniformBufferLock);
  8693. for (Sint32 i = 0; i < commandBuffer->usedUniformBufferCount; i += 1) {
  8694. VULKAN_INTERNAL_ReturnUniformBufferToPool(
  8695. renderer,
  8696. commandBuffer->usedUniformBuffers[i]);
  8697. }
  8698. commandBuffer->usedUniformBufferCount = 0;
  8699. SDL_UnlockMutex(renderer->acquireUniformBufferLock);
  8700. // Decrement reference counts
  8701. for (Sint32 i = 0; i < commandBuffer->usedBufferCount; i += 1) {
  8702. (void)SDL_AtomicDecRef(&commandBuffer->usedBuffers[i]->referenceCount);
  8703. }
  8704. commandBuffer->usedBufferCount = 0;
  8705. for (Sint32 i = 0; i < commandBuffer->usedTextureCount; i += 1) {
  8706. (void)SDL_AtomicDecRef(&commandBuffer->usedTextures[i]->referenceCount);
  8707. }
  8708. commandBuffer->usedTextureCount = 0;
  8709. for (Sint32 i = 0; i < commandBuffer->usedSamplerCount; i += 1) {
  8710. (void)SDL_AtomicDecRef(&commandBuffer->usedSamplers[i]->referenceCount);
  8711. }
  8712. commandBuffer->usedSamplerCount = 0;
  8713. for (Sint32 i = 0; i < commandBuffer->usedGraphicsPipelineCount; i += 1) {
  8714. (void)SDL_AtomicDecRef(&commandBuffer->usedGraphicsPipelines[i]->referenceCount);
  8715. }
  8716. commandBuffer->usedGraphicsPipelineCount = 0;
  8717. for (Sint32 i = 0; i < commandBuffer->usedComputePipelineCount; i += 1) {
  8718. (void)SDL_AtomicDecRef(&commandBuffer->usedComputePipelines[i]->referenceCount);
  8719. }
  8720. commandBuffer->usedComputePipelineCount = 0;
  8721. for (Sint32 i = 0; i < commandBuffer->usedFramebufferCount; i += 1) {
  8722. (void)SDL_AtomicDecRef(&commandBuffer->usedFramebuffers[i]->referenceCount);
  8723. }
  8724. commandBuffer->usedFramebufferCount = 0;
  8725. // Reset presentation data
  8726. commandBuffer->presentDataCount = 0;
  8727. commandBuffer->waitSemaphoreCount = 0;
  8728. commandBuffer->signalSemaphoreCount = 0;
  8729. commandBuffer->swapchainRequested = false;
  8730. // Reset defrag state
  8731. if (commandBuffer->isDefrag) {
  8732. renderer->defragInProgress = 0;
  8733. }
  8734. // Return command buffer to pool
  8735. SDL_LockMutex(renderer->acquireCommandBufferLock);
  8736. if (commandBuffer->commandPool->inactiveCommandBufferCount == commandBuffer->commandPool->inactiveCommandBufferCapacity) {
  8737. commandBuffer->commandPool->inactiveCommandBufferCapacity += 1;
  8738. commandBuffer->commandPool->inactiveCommandBuffers = SDL_realloc(
  8739. commandBuffer->commandPool->inactiveCommandBuffers,
  8740. commandBuffer->commandPool->inactiveCommandBufferCapacity * sizeof(VulkanCommandBuffer *));
  8741. }
  8742. commandBuffer->commandPool->inactiveCommandBuffers[commandBuffer->commandPool->inactiveCommandBufferCount] = commandBuffer;
  8743. commandBuffer->commandPool->inactiveCommandBufferCount += 1;
  8744. // Release descriptor set cache
  8745. VULKAN_INTERNAL_ReturnDescriptorSetCacheToPool(
  8746. renderer,
  8747. commandBuffer->descriptorSetCache);
  8748. commandBuffer->descriptorSetCache = NULL;
  8749. SDL_UnlockMutex(renderer->acquireCommandBufferLock);
  8750. // Remove this command buffer from the submitted list
  8751. if (!cancel) {
  8752. for (Uint32 i = 0; i < renderer->submittedCommandBufferCount; i += 1) {
  8753. if (renderer->submittedCommandBuffers[i] == commandBuffer) {
  8754. renderer->submittedCommandBuffers[i] = renderer->submittedCommandBuffers[renderer->submittedCommandBufferCount - 1];
  8755. renderer->submittedCommandBufferCount -= 1;
  8756. }
  8757. }
  8758. }
  8759. }
  8760. static bool VULKAN_WaitForFences(
  8761. SDL_GPURenderer *driverData,
  8762. bool waitAll,
  8763. SDL_GPUFence *const *fences,
  8764. Uint32 numFences)
  8765. {
  8766. VulkanRenderer *renderer = (VulkanRenderer *)driverData;
  8767. VkFence *vkFences = SDL_stack_alloc(VkFence, numFences);
  8768. VkResult result;
  8769. for (Uint32 i = 0; i < numFences; i += 1) {
  8770. vkFences[i] = ((VulkanFenceHandle *)fences[i])->fence;
  8771. }
  8772. result = renderer->vkWaitForFences(
  8773. renderer->logicalDevice,
  8774. numFences,
  8775. vkFences,
  8776. waitAll,
  8777. SDL_MAX_UINT64);
  8778. CHECK_VULKAN_ERROR_AND_RETURN(result, vkWaitForFences, false);
  8779. SDL_stack_free(vkFences);
  8780. SDL_LockMutex(renderer->submitLock);
  8781. for (Sint32 i = renderer->submittedCommandBufferCount - 1; i >= 0; i -= 1) {
  8782. result = renderer->vkGetFenceStatus(
  8783. renderer->logicalDevice,
  8784. renderer->submittedCommandBuffers[i]->inFlightFence->fence);
  8785. if (result == VK_SUCCESS) {
  8786. VULKAN_INTERNAL_CleanCommandBuffer(
  8787. renderer,
  8788. renderer->submittedCommandBuffers[i],
  8789. false);
  8790. }
  8791. }
  8792. VULKAN_INTERNAL_PerformPendingDestroys(renderer);
  8793. SDL_UnlockMutex(renderer->submitLock);
  8794. return true;
  8795. }
  8796. static bool VULKAN_Wait(
  8797. SDL_GPURenderer *driverData)
  8798. {
  8799. VulkanRenderer *renderer = (VulkanRenderer *)driverData;
  8800. VulkanCommandBuffer *commandBuffer;
  8801. VkResult result;
  8802. Sint32 i;
  8803. SDL_LockMutex(renderer->submitLock);
  8804. result = renderer->vkDeviceWaitIdle(renderer->logicalDevice);
  8805. if (result != VK_SUCCESS) {
  8806. if (renderer->debugMode) {
  8807. SDL_LogError(SDL_LOG_CATEGORY_GPU, "%s %s", "vkDeviceWaitIdle", VkErrorMessages(result));
  8808. }
  8809. SDL_SetError("%s %s", "vkDeviceWaitIdle", VkErrorMessages(result));
  8810. SDL_UnlockMutex(renderer->submitLock);
  8811. return false;
  8812. }
  8813. for (i = renderer->submittedCommandBufferCount - 1; i >= 0; i -= 1) {
  8814. commandBuffer = renderer->submittedCommandBuffers[i];
  8815. VULKAN_INTERNAL_CleanCommandBuffer(renderer, commandBuffer, false);
  8816. }
  8817. VULKAN_INTERNAL_PerformPendingDestroys(renderer);
  8818. SDL_UnlockMutex(renderer->submitLock);
  8819. return true;
  8820. }
  8821. static SDL_GPUFence *VULKAN_SubmitAndAcquireFence(
  8822. SDL_GPUCommandBuffer *commandBuffer)
  8823. {
  8824. VulkanCommandBuffer *vulkanCommandBuffer = (VulkanCommandBuffer *)commandBuffer;
  8825. vulkanCommandBuffer->autoReleaseFence = false;
  8826. if (!VULKAN_Submit(commandBuffer)) {
  8827. return NULL;
  8828. }
  8829. return (SDL_GPUFence *)vulkanCommandBuffer->inFlightFence;
  8830. }
  8831. static void VULKAN_INTERNAL_ReleaseCommandBuffer(VulkanCommandBuffer *vulkanCommandBuffer)
  8832. {
  8833. VulkanRenderer *renderer = vulkanCommandBuffer->renderer;
  8834. if (renderer->submittedCommandBufferCount + 1 >= renderer->submittedCommandBufferCapacity) {
  8835. renderer->submittedCommandBufferCapacity = renderer->submittedCommandBufferCount + 1;
  8836. renderer->submittedCommandBuffers = SDL_realloc(
  8837. renderer->submittedCommandBuffers,
  8838. sizeof(VulkanCommandBuffer *) * renderer->submittedCommandBufferCapacity);
  8839. }
  8840. renderer->submittedCommandBuffers[renderer->submittedCommandBufferCount] = vulkanCommandBuffer;
  8841. renderer->submittedCommandBufferCount += 1;
  8842. }
  8843. static bool VULKAN_Submit(
  8844. SDL_GPUCommandBuffer *commandBuffer)
  8845. {
  8846. VulkanCommandBuffer *vulkanCommandBuffer = (VulkanCommandBuffer *)commandBuffer;
  8847. VulkanRenderer *renderer = vulkanCommandBuffer->renderer;
  8848. VkSubmitInfo submitInfo;
  8849. VkPresentInfoKHR presentInfo;
  8850. VulkanPresentData *presentData;
  8851. VkResult vulkanResult, presentResult = VK_SUCCESS;
  8852. VkPipelineStageFlags waitStages[MAX_PRESENT_COUNT];
  8853. Uint32 swapchainImageIndex;
  8854. VulkanTextureSubresource *swapchainTextureSubresource;
  8855. VulkanMemorySubAllocator *allocator;
  8856. bool performCleanups =
  8857. (renderer->claimedWindowCount > 0 && vulkanCommandBuffer->swapchainRequested) ||
  8858. renderer->claimedWindowCount == 0;
  8859. SDL_LockMutex(renderer->submitLock);
  8860. // FIXME: Can this just be permanent?
  8861. for (Uint32 i = 0; i < MAX_PRESENT_COUNT; i += 1) {
  8862. waitStages[i] = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
  8863. }
  8864. for (Uint32 j = 0; j < vulkanCommandBuffer->presentDataCount; j += 1) {
  8865. swapchainImageIndex = vulkanCommandBuffer->presentDatas[j].swapchainImageIndex;
  8866. swapchainTextureSubresource = VULKAN_INTERNAL_FetchTextureSubresource(
  8867. &vulkanCommandBuffer->presentDatas[j].windowData->textureContainers[swapchainImageIndex],
  8868. 0,
  8869. 0);
  8870. VULKAN_INTERNAL_TextureSubresourceTransitionFromDefaultUsage(
  8871. renderer,
  8872. vulkanCommandBuffer,
  8873. VULKAN_TEXTURE_USAGE_MODE_PRESENT,
  8874. swapchainTextureSubresource);
  8875. }
  8876. if (performCleanups &&
  8877. renderer->allocationsToDefragCount > 0 &&
  8878. !renderer->defragInProgress) {
  8879. if (!VULKAN_INTERNAL_DefragmentMemory(renderer, vulkanCommandBuffer))
  8880. {
  8881. SDL_LogError(SDL_LOG_CATEGORY_GPU, "%s", "Failed to defragment memory, likely OOM!");
  8882. }
  8883. }
  8884. if (!VULKAN_INTERNAL_EndCommandBuffer(renderer, vulkanCommandBuffer)) {
  8885. SDL_UnlockMutex(renderer->submitLock);
  8886. return false;
  8887. }
  8888. vulkanCommandBuffer->inFlightFence = VULKAN_INTERNAL_AcquireFenceFromPool(renderer);
  8889. if (vulkanCommandBuffer->inFlightFence == NULL) {
  8890. SDL_UnlockMutex(renderer->submitLock);
  8891. return false;
  8892. }
  8893. // Command buffer has a reference to the in-flight fence
  8894. (void)SDL_AtomicIncRef(&vulkanCommandBuffer->inFlightFence->referenceCount);
  8895. submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
  8896. submitInfo.pNext = NULL;
  8897. submitInfo.commandBufferCount = 1;
  8898. submitInfo.pCommandBuffers = &vulkanCommandBuffer->commandBuffer;
  8899. submitInfo.pWaitDstStageMask = waitStages;
  8900. submitInfo.pWaitSemaphores = vulkanCommandBuffer->waitSemaphores;
  8901. submitInfo.waitSemaphoreCount = vulkanCommandBuffer->waitSemaphoreCount;
  8902. submitInfo.pSignalSemaphores = vulkanCommandBuffer->signalSemaphores;
  8903. submitInfo.signalSemaphoreCount = vulkanCommandBuffer->signalSemaphoreCount;
  8904. vulkanResult = renderer->vkQueueSubmit(
  8905. renderer->unifiedQueue,
  8906. 1,
  8907. &submitInfo,
  8908. vulkanCommandBuffer->inFlightFence->fence);
  8909. if (vulkanResult != VK_SUCCESS) {
  8910. SDL_UnlockMutex(renderer->submitLock);
  8911. CHECK_VULKAN_ERROR_AND_RETURN(vulkanResult, vkQueueSubmit, false);
  8912. }
  8913. // Present, if applicable
  8914. for (Uint32 j = 0; j < vulkanCommandBuffer->presentDataCount; j += 1) {
  8915. presentData = &vulkanCommandBuffer->presentDatas[j];
  8916. presentInfo.sType = VK_STRUCTURE_TYPE_PRESENT_INFO_KHR;
  8917. presentInfo.pNext = NULL;
  8918. presentInfo.pWaitSemaphores =
  8919. &presentData->windowData->renderFinishedSemaphore[presentData->swapchainImageIndex];
  8920. presentInfo.waitSemaphoreCount = 1;
  8921. presentInfo.pSwapchains = &presentData->windowData->swapchain;
  8922. presentInfo.swapchainCount = 1;
  8923. presentInfo.pImageIndices = &presentData->swapchainImageIndex;
  8924. presentInfo.pResults = NULL;
  8925. presentResult = renderer->vkQueuePresentKHR(
  8926. renderer->unifiedQueue,
  8927. &presentInfo);
  8928. if (presentResult == VK_SUCCESS || presentResult == VK_SUBOPTIMAL_KHR || presentResult == VK_ERROR_OUT_OF_DATE_KHR) {
  8929. // If presenting, the swapchain is using the in-flight fence
  8930. presentData->windowData->inFlightFences[presentData->windowData->frameCounter] = (SDL_GPUFence *)vulkanCommandBuffer->inFlightFence;
  8931. (void)SDL_AtomicIncRef(&vulkanCommandBuffer->inFlightFence->referenceCount);
  8932. // On the Android platform, VK_SUBOPTIMAL_KHR is returned whenever the device is rotated. We'll just ignore this for now.
  8933. #ifndef SDL_PLATFORM_ANDROID
  8934. if (presentResult == VK_SUBOPTIMAL_KHR) {
  8935. presentData->windowData->needsSwapchainRecreate = true;
  8936. }
  8937. #endif
  8938. if (presentResult == VK_ERROR_OUT_OF_DATE_KHR) {
  8939. presentData->windowData->needsSwapchainRecreate = true;
  8940. }
  8941. } else {
  8942. if (presentResult != VK_SUCCESS) {
  8943. VULKAN_INTERNAL_ReleaseCommandBuffer(vulkanCommandBuffer);
  8944. SDL_UnlockMutex(renderer->submitLock);
  8945. }
  8946. CHECK_VULKAN_ERROR_AND_RETURN(presentResult, vkQueuePresentKHR, false);
  8947. }
  8948. presentData->windowData->frameCounter =
  8949. (presentData->windowData->frameCounter + 1) % renderer->allowedFramesInFlight;
  8950. }
  8951. if (performCleanups) {
  8952. for (Sint32 i = renderer->submittedCommandBufferCount - 1; i >= 0; i -= 1) {
  8953. vulkanResult = renderer->vkGetFenceStatus(
  8954. renderer->logicalDevice,
  8955. renderer->submittedCommandBuffers[i]->inFlightFence->fence);
  8956. if (vulkanResult == VK_SUCCESS) {
  8957. VULKAN_INTERNAL_CleanCommandBuffer(
  8958. renderer,
  8959. renderer->submittedCommandBuffers[i],
  8960. false);
  8961. }
  8962. }
  8963. if (renderer->checkEmptyAllocations) {
  8964. SDL_LockMutex(renderer->allocatorLock);
  8965. for (Uint32 i = 0; i < VK_MAX_MEMORY_TYPES; i += 1) {
  8966. allocator = &renderer->memoryAllocator->subAllocators[i];
  8967. for (Sint32 j = allocator->allocationCount - 1; j >= 0; j -= 1) {
  8968. if (allocator->allocations[j]->usedRegionCount == 0) {
  8969. VULKAN_INTERNAL_DeallocateMemory(
  8970. renderer,
  8971. allocator,
  8972. j);
  8973. }
  8974. }
  8975. }
  8976. renderer->checkEmptyAllocations = false;
  8977. SDL_UnlockMutex(renderer->allocatorLock);
  8978. }
  8979. VULKAN_INTERNAL_PerformPendingDestroys(renderer);
  8980. }
  8981. // Mark command buffer as submitted
  8982. VULKAN_INTERNAL_ReleaseCommandBuffer(vulkanCommandBuffer);
  8983. SDL_UnlockMutex(renderer->submitLock);
  8984. return true;
  8985. }
  8986. static bool VULKAN_Cancel(
  8987. SDL_GPUCommandBuffer *commandBuffer)
  8988. {
  8989. VulkanRenderer *renderer;
  8990. VulkanCommandBuffer *vulkanCommandBuffer;
  8991. VkResult result;
  8992. vulkanCommandBuffer = (VulkanCommandBuffer *)commandBuffer;
  8993. renderer = vulkanCommandBuffer->renderer;
  8994. result = renderer->vkResetCommandBuffer(
  8995. vulkanCommandBuffer->commandBuffer,
  8996. VK_COMMAND_BUFFER_RESET_RELEASE_RESOURCES_BIT);
  8997. CHECK_VULKAN_ERROR_AND_RETURN(result, vkResetCommandBuffer, false);
  8998. vulkanCommandBuffer->autoReleaseFence = false;
  8999. SDL_LockMutex(renderer->submitLock);
  9000. VULKAN_INTERNAL_CleanCommandBuffer(renderer, vulkanCommandBuffer, true);
  9001. SDL_UnlockMutex(renderer->submitLock);
  9002. return true;
  9003. }
  9004. static bool VULKAN_INTERNAL_DefragmentMemory(
  9005. VulkanRenderer *renderer,
  9006. VulkanCommandBuffer *commandBuffer)
  9007. {
  9008. renderer->defragInProgress = 1;
  9009. commandBuffer->isDefrag = 1;
  9010. SDL_LockMutex(renderer->allocatorLock);
  9011. VulkanMemoryAllocation *allocation = renderer->allocationsToDefrag[renderer->allocationsToDefragCount - 1];
  9012. renderer->allocationsToDefragCount -= 1;
  9013. /* For each used region in the allocation
  9014. * create a new resource, copy the data
  9015. * and re-point the resource containers
  9016. */
  9017. for (Uint32 i = 0; i < allocation->usedRegionCount; i += 1) {
  9018. VulkanMemoryUsedRegion *currentRegion = allocation->usedRegions[i];
  9019. if (currentRegion->isBuffer && !currentRegion->vulkanBuffer->markedForDestroy) {
  9020. currentRegion->vulkanBuffer->usage |= VK_BUFFER_USAGE_TRANSFER_DST_BIT;
  9021. VulkanBuffer *newBuffer = VULKAN_INTERNAL_CreateBuffer(
  9022. renderer,
  9023. currentRegion->vulkanBuffer->size,
  9024. currentRegion->vulkanBuffer->usage,
  9025. currentRegion->vulkanBuffer->type,
  9026. false,
  9027. currentRegion->vulkanBuffer->container != NULL ? currentRegion->vulkanBuffer->container->debugName : NULL);
  9028. if (newBuffer == NULL) {
  9029. SDL_UnlockMutex(renderer->allocatorLock);
  9030. SDL_LogError(SDL_LOG_CATEGORY_GPU, "%s", "Failed to allocate defrag buffer!");
  9031. return false;
  9032. }
  9033. // Copy buffer contents if necessary
  9034. if (
  9035. currentRegion->vulkanBuffer->type == VULKAN_BUFFER_TYPE_GPU && currentRegion->vulkanBuffer->transitioned) {
  9036. VULKAN_INTERNAL_BufferTransitionFromDefaultUsage(
  9037. renderer,
  9038. commandBuffer,
  9039. VULKAN_BUFFER_USAGE_MODE_COPY_SOURCE,
  9040. currentRegion->vulkanBuffer);
  9041. VULKAN_INTERNAL_BufferTransitionFromDefaultUsage(
  9042. renderer,
  9043. commandBuffer,
  9044. VULKAN_BUFFER_USAGE_MODE_COPY_DESTINATION,
  9045. newBuffer);
  9046. VkBufferCopy bufferCopy;
  9047. bufferCopy.srcOffset = 0;
  9048. bufferCopy.dstOffset = 0;
  9049. bufferCopy.size = currentRegion->resourceSize;
  9050. renderer->vkCmdCopyBuffer(
  9051. commandBuffer->commandBuffer,
  9052. currentRegion->vulkanBuffer->buffer,
  9053. newBuffer->buffer,
  9054. 1,
  9055. &bufferCopy);
  9056. VULKAN_INTERNAL_BufferTransitionToDefaultUsage(
  9057. renderer,
  9058. commandBuffer,
  9059. VULKAN_BUFFER_USAGE_MODE_COPY_DESTINATION,
  9060. newBuffer);
  9061. VULKAN_INTERNAL_TrackBuffer(commandBuffer, currentRegion->vulkanBuffer);
  9062. VULKAN_INTERNAL_TrackBuffer(commandBuffer, newBuffer);
  9063. }
  9064. // re-point original container to new buffer
  9065. newBuffer->container = currentRegion->vulkanBuffer->container;
  9066. newBuffer->containerIndex = currentRegion->vulkanBuffer->containerIndex;
  9067. if (newBuffer->type == VULKAN_BUFFER_TYPE_UNIFORM) {
  9068. currentRegion->vulkanBuffer->uniformBufferForDefrag->buffer = newBuffer;
  9069. } else {
  9070. newBuffer->container->buffers[newBuffer->containerIndex] = newBuffer;
  9071. if (newBuffer->container->activeBuffer == currentRegion->vulkanBuffer) {
  9072. newBuffer->container->activeBuffer = newBuffer;
  9073. }
  9074. }
  9075. if (currentRegion->vulkanBuffer->uniformBufferForDefrag) {
  9076. newBuffer->uniformBufferForDefrag = currentRegion->vulkanBuffer->uniformBufferForDefrag;
  9077. }
  9078. VULKAN_INTERNAL_ReleaseBuffer(renderer, currentRegion->vulkanBuffer);
  9079. } else if (!currentRegion->isBuffer && !currentRegion->vulkanTexture->markedForDestroy) {
  9080. VulkanTexture *newTexture = VULKAN_INTERNAL_CreateTexture(
  9081. renderer,
  9082. false,
  9083. &currentRegion->vulkanTexture->container->header.info);
  9084. if (newTexture == NULL) {
  9085. SDL_UnlockMutex(renderer->allocatorLock);
  9086. SDL_LogError(SDL_LOG_CATEGORY_GPU, "%s", "Failed to allocate defrag buffer!");
  9087. return false;
  9088. }
  9089. SDL_GPUTextureCreateInfo info = currentRegion->vulkanTexture->container->header.info;
  9090. for (Uint32 subresourceIndex = 0; subresourceIndex < currentRegion->vulkanTexture->subresourceCount; subresourceIndex += 1) {
  9091. // copy subresource if necessary
  9092. VulkanTextureSubresource *srcSubresource = &currentRegion->vulkanTexture->subresources[subresourceIndex];
  9093. VulkanTextureSubresource *dstSubresource = &newTexture->subresources[subresourceIndex];
  9094. VULKAN_INTERNAL_TextureSubresourceTransitionFromDefaultUsage(
  9095. renderer,
  9096. commandBuffer,
  9097. VULKAN_TEXTURE_USAGE_MODE_COPY_SOURCE,
  9098. srcSubresource);
  9099. VULKAN_INTERNAL_TextureSubresourceMemoryBarrier(
  9100. renderer,
  9101. commandBuffer,
  9102. VULKAN_TEXTURE_USAGE_MODE_UNINITIALIZED,
  9103. VULKAN_TEXTURE_USAGE_MODE_COPY_DESTINATION,
  9104. dstSubresource);
  9105. VkImageCopy imageCopy;
  9106. imageCopy.srcOffset.x = 0;
  9107. imageCopy.srcOffset.y = 0;
  9108. imageCopy.srcOffset.z = 0;
  9109. imageCopy.srcSubresource.aspectMask = srcSubresource->parent->aspectFlags;
  9110. imageCopy.srcSubresource.baseArrayLayer = srcSubresource->layer;
  9111. imageCopy.srcSubresource.layerCount = 1;
  9112. imageCopy.srcSubresource.mipLevel = srcSubresource->level;
  9113. imageCopy.extent.width = SDL_max(1, info.width >> srcSubresource->level);
  9114. imageCopy.extent.height = SDL_max(1, info.height >> srcSubresource->level);
  9115. imageCopy.extent.depth = info.type == SDL_GPU_TEXTURETYPE_3D ? info.layer_count_or_depth : 1;
  9116. imageCopy.dstOffset.x = 0;
  9117. imageCopy.dstOffset.y = 0;
  9118. imageCopy.dstOffset.z = 0;
  9119. imageCopy.dstSubresource.aspectMask = dstSubresource->parent->aspectFlags;
  9120. imageCopy.dstSubresource.baseArrayLayer = dstSubresource->layer;
  9121. imageCopy.dstSubresource.layerCount = 1;
  9122. imageCopy.dstSubresource.mipLevel = dstSubresource->level;
  9123. renderer->vkCmdCopyImage(
  9124. commandBuffer->commandBuffer,
  9125. currentRegion->vulkanTexture->image,
  9126. VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
  9127. newTexture->image,
  9128. VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
  9129. 1,
  9130. &imageCopy);
  9131. VULKAN_INTERNAL_TextureSubresourceTransitionToDefaultUsage(
  9132. renderer,
  9133. commandBuffer,
  9134. VULKAN_TEXTURE_USAGE_MODE_COPY_DESTINATION,
  9135. dstSubresource);
  9136. VULKAN_INTERNAL_TrackTexture(commandBuffer, srcSubresource->parent);
  9137. VULKAN_INTERNAL_TrackTexture(commandBuffer, dstSubresource->parent);
  9138. }
  9139. // re-point original container to new texture
  9140. newTexture->container = currentRegion->vulkanTexture->container;
  9141. newTexture->containerIndex = currentRegion->vulkanTexture->containerIndex;
  9142. newTexture->container->textures[currentRegion->vulkanTexture->containerIndex] = newTexture;
  9143. if (currentRegion->vulkanTexture == currentRegion->vulkanTexture->container->activeTexture) {
  9144. newTexture->container->activeTexture = newTexture;
  9145. }
  9146. VULKAN_INTERNAL_ReleaseTexture(renderer, currentRegion->vulkanTexture);
  9147. }
  9148. }
  9149. SDL_UnlockMutex(renderer->allocatorLock);
  9150. return true;
  9151. }
  9152. // Format Info
  9153. static bool VULKAN_SupportsTextureFormat(
  9154. SDL_GPURenderer *driverData,
  9155. SDL_GPUTextureFormat format,
  9156. SDL_GPUTextureType type,
  9157. SDL_GPUTextureUsageFlags usage)
  9158. {
  9159. VulkanRenderer *renderer = (VulkanRenderer *)driverData;
  9160. VkFormat vulkanFormat = SDLToVK_TextureFormat[format];
  9161. VkImageUsageFlags vulkanUsage = 0;
  9162. VkImageCreateFlags createFlags = 0;
  9163. VkImageFormatProperties properties;
  9164. VkResult vulkanResult;
  9165. if (usage & SDL_GPU_TEXTUREUSAGE_SAMPLER) {
  9166. vulkanUsage |= VK_IMAGE_USAGE_SAMPLED_BIT;
  9167. }
  9168. if (usage & SDL_GPU_TEXTUREUSAGE_COLOR_TARGET) {
  9169. vulkanUsage |= VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT;
  9170. }
  9171. if (usage & SDL_GPU_TEXTUREUSAGE_DEPTH_STENCIL_TARGET) {
  9172. vulkanUsage |= VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT;
  9173. }
  9174. if (usage & (SDL_GPU_TEXTUREUSAGE_GRAPHICS_STORAGE_READ |
  9175. SDL_GPU_TEXTUREUSAGE_COMPUTE_STORAGE_READ |
  9176. SDL_GPU_TEXTUREUSAGE_COMPUTE_STORAGE_WRITE |
  9177. SDL_GPU_TEXTUREUSAGE_COMPUTE_STORAGE_SIMULTANEOUS_READ_WRITE)) {
  9178. vulkanUsage |= VK_IMAGE_USAGE_STORAGE_BIT;
  9179. }
  9180. if (type == SDL_GPU_TEXTURETYPE_CUBE || type == SDL_GPU_TEXTURETYPE_CUBE_ARRAY) {
  9181. createFlags = VK_IMAGE_CREATE_CUBE_COMPATIBLE_BIT;
  9182. }
  9183. vulkanResult = renderer->vkGetPhysicalDeviceImageFormatProperties(
  9184. renderer->physicalDevice,
  9185. vulkanFormat,
  9186. (type == SDL_GPU_TEXTURETYPE_3D) ? VK_IMAGE_TYPE_3D : VK_IMAGE_TYPE_2D,
  9187. VK_IMAGE_TILING_OPTIMAL,
  9188. vulkanUsage,
  9189. createFlags,
  9190. &properties);
  9191. return vulkanResult == VK_SUCCESS;
  9192. }
  9193. // Device instantiation
  9194. static inline Uint8 CheckDeviceExtensions(
  9195. VkExtensionProperties *extensions,
  9196. Uint32 numExtensions,
  9197. VulkanExtensions *supports)
  9198. {
  9199. Uint32 i;
  9200. SDL_memset(supports, '\0', sizeof(VulkanExtensions));
  9201. for (i = 0; i < numExtensions; i += 1) {
  9202. const char *name = extensions[i].extensionName;
  9203. #define CHECK(ext) \
  9204. if (SDL_strcmp(name, "VK_" #ext) == 0) { \
  9205. supports->ext = 1; \
  9206. }
  9207. CHECK(KHR_swapchain)
  9208. else CHECK(KHR_maintenance1) else CHECK(KHR_driver_properties) else CHECK(KHR_portability_subset) else CHECK(MSFT_layered_driver) else CHECK(EXT_texture_compression_astc_hdr)
  9209. #undef CHECK
  9210. }
  9211. return (supports->KHR_swapchain &&
  9212. supports->KHR_maintenance1);
  9213. }
  9214. static inline Uint32 GetDeviceExtensionCount(VulkanExtensions *supports)
  9215. {
  9216. return (
  9217. supports->KHR_swapchain +
  9218. supports->KHR_maintenance1 +
  9219. supports->KHR_driver_properties +
  9220. supports->KHR_portability_subset +
  9221. supports->MSFT_layered_driver +
  9222. supports->EXT_texture_compression_astc_hdr);
  9223. }
  9224. static inline void CreateDeviceExtensionArray(
  9225. VulkanExtensions *supports,
  9226. const char **extensions)
  9227. {
  9228. Uint8 cur = 0;
  9229. #define CHECK(ext) \
  9230. if (supports->ext) { \
  9231. extensions[cur++] = "VK_" #ext; \
  9232. }
  9233. CHECK(KHR_swapchain)
  9234. CHECK(KHR_maintenance1)
  9235. CHECK(KHR_driver_properties)
  9236. CHECK(KHR_portability_subset)
  9237. CHECK(MSFT_layered_driver)
  9238. CHECK(EXT_texture_compression_astc_hdr)
  9239. #undef CHECK
  9240. }
  9241. static inline Uint8 SupportsInstanceExtension(
  9242. const char *ext,
  9243. VkExtensionProperties *availableExtensions,
  9244. Uint32 numAvailableExtensions)
  9245. {
  9246. Uint32 i;
  9247. for (i = 0; i < numAvailableExtensions; i += 1) {
  9248. if (SDL_strcmp(ext, availableExtensions[i].extensionName) == 0) {
  9249. return 1;
  9250. }
  9251. }
  9252. return 0;
  9253. }
  9254. static Uint8 VULKAN_INTERNAL_CheckInstanceExtensions(
  9255. const char **requiredExtensions,
  9256. Uint32 requiredExtensionsLength,
  9257. bool *supportsDebugUtils,
  9258. bool *supportsColorspace,
  9259. bool *supportsPhysicalDeviceProperties2)
  9260. {
  9261. Uint32 extensionCount, i;
  9262. VkExtensionProperties *availableExtensions;
  9263. Uint8 allExtensionsSupported = 1;
  9264. vkEnumerateInstanceExtensionProperties(
  9265. NULL,
  9266. &extensionCount,
  9267. NULL);
  9268. availableExtensions = SDL_malloc(
  9269. extensionCount * sizeof(VkExtensionProperties));
  9270. vkEnumerateInstanceExtensionProperties(
  9271. NULL,
  9272. &extensionCount,
  9273. availableExtensions);
  9274. for (i = 0; i < requiredExtensionsLength; i += 1) {
  9275. if (!SupportsInstanceExtension(
  9276. requiredExtensions[i],
  9277. availableExtensions,
  9278. extensionCount)) {
  9279. allExtensionsSupported = 0;
  9280. break;
  9281. }
  9282. }
  9283. // This is optional, but nice to have!
  9284. *supportsDebugUtils = SupportsInstanceExtension(
  9285. VK_EXT_DEBUG_UTILS_EXTENSION_NAME,
  9286. availableExtensions,
  9287. extensionCount);
  9288. // Also optional and nice to have!
  9289. *supportsColorspace = SupportsInstanceExtension(
  9290. VK_EXT_SWAPCHAIN_COLOR_SPACE_EXTENSION_NAME,
  9291. availableExtensions,
  9292. extensionCount);
  9293. // Only needed for KHR_driver_properties!
  9294. *supportsPhysicalDeviceProperties2 = SupportsInstanceExtension(
  9295. VK_KHR_GET_PHYSICAL_DEVICE_PROPERTIES_2_EXTENSION_NAME,
  9296. availableExtensions,
  9297. extensionCount);
  9298. SDL_free(availableExtensions);
  9299. return allExtensionsSupported;
  9300. }
  9301. static Uint8 VULKAN_INTERNAL_CheckDeviceExtensions(
  9302. VulkanRenderer *renderer,
  9303. VkPhysicalDevice physicalDevice,
  9304. VulkanExtensions *physicalDeviceExtensions)
  9305. {
  9306. Uint32 extensionCount;
  9307. VkExtensionProperties *availableExtensions;
  9308. Uint8 allExtensionsSupported;
  9309. renderer->vkEnumerateDeviceExtensionProperties(
  9310. physicalDevice,
  9311. NULL,
  9312. &extensionCount,
  9313. NULL);
  9314. availableExtensions = (VkExtensionProperties *)SDL_malloc(
  9315. extensionCount * sizeof(VkExtensionProperties));
  9316. renderer->vkEnumerateDeviceExtensionProperties(
  9317. physicalDevice,
  9318. NULL,
  9319. &extensionCount,
  9320. availableExtensions);
  9321. allExtensionsSupported = CheckDeviceExtensions(
  9322. availableExtensions,
  9323. extensionCount,
  9324. physicalDeviceExtensions);
  9325. SDL_free(availableExtensions);
  9326. return allExtensionsSupported;
  9327. }
  9328. static Uint8 VULKAN_INTERNAL_CheckValidationLayers(
  9329. const char **validationLayers,
  9330. Uint32 validationLayersLength)
  9331. {
  9332. Uint32 layerCount;
  9333. VkLayerProperties *availableLayers;
  9334. Uint32 i, j;
  9335. Uint8 layerFound = 0;
  9336. vkEnumerateInstanceLayerProperties(&layerCount, NULL);
  9337. availableLayers = (VkLayerProperties *)SDL_malloc(
  9338. layerCount * sizeof(VkLayerProperties));
  9339. vkEnumerateInstanceLayerProperties(&layerCount, availableLayers);
  9340. for (i = 0; i < validationLayersLength; i += 1) {
  9341. layerFound = 0;
  9342. for (j = 0; j < layerCount; j += 1) {
  9343. if (SDL_strcmp(validationLayers[i], availableLayers[j].layerName) == 0) {
  9344. layerFound = 1;
  9345. break;
  9346. }
  9347. }
  9348. if (!layerFound) {
  9349. break;
  9350. }
  9351. }
  9352. SDL_free(availableLayers);
  9353. return layerFound;
  9354. }
  9355. static Uint8 VULKAN_INTERNAL_CreateInstance(VulkanRenderer *renderer)
  9356. {
  9357. VkResult vulkanResult;
  9358. VkApplicationInfo appInfo;
  9359. VkInstanceCreateFlags createFlags;
  9360. const char *const *originalInstanceExtensionNames;
  9361. const char **instanceExtensionNames;
  9362. Uint32 instanceExtensionCount;
  9363. VkInstanceCreateInfo createInfo;
  9364. static const char *layerNames[] = { "VK_LAYER_KHRONOS_validation" };
  9365. appInfo.sType = VK_STRUCTURE_TYPE_APPLICATION_INFO;
  9366. appInfo.pNext = NULL;
  9367. appInfo.pApplicationName = NULL;
  9368. appInfo.applicationVersion = 0;
  9369. appInfo.pEngineName = "SDLGPU";
  9370. appInfo.engineVersion = SDL_VERSION;
  9371. appInfo.apiVersion = VK_MAKE_VERSION(1, 0, 0);
  9372. createFlags = 0;
  9373. originalInstanceExtensionNames = SDL_Vulkan_GetInstanceExtensions(&instanceExtensionCount);
  9374. if (!originalInstanceExtensionNames) {
  9375. SDL_LogError(
  9376. SDL_LOG_CATEGORY_GPU,
  9377. "SDL_Vulkan_GetInstanceExtensions(): getExtensionCount: %s",
  9378. SDL_GetError());
  9379. return 0;
  9380. }
  9381. /* Extra space for the following extensions:
  9382. * VK_KHR_get_physical_device_properties2
  9383. * VK_EXT_swapchain_colorspace
  9384. * VK_EXT_debug_utils
  9385. * VK_KHR_portability_enumeration
  9386. */
  9387. instanceExtensionNames = SDL_stack_alloc(
  9388. const char *,
  9389. instanceExtensionCount + 4);
  9390. SDL_memcpy((void *)instanceExtensionNames, originalInstanceExtensionNames, instanceExtensionCount * sizeof(const char *));
  9391. #ifdef SDL_PLATFORM_APPLE
  9392. instanceExtensionNames[instanceExtensionCount++] =
  9393. VK_KHR_PORTABILITY_ENUMERATION_EXTENSION_NAME;
  9394. createFlags |= VK_INSTANCE_CREATE_ENUMERATE_PORTABILITY_BIT_KHR;
  9395. #endif
  9396. if (!VULKAN_INTERNAL_CheckInstanceExtensions(
  9397. instanceExtensionNames,
  9398. instanceExtensionCount,
  9399. &renderer->supportsDebugUtils,
  9400. &renderer->supportsColorspace,
  9401. &renderer->supportsPhysicalDeviceProperties2)) {
  9402. SDL_stack_free((char *)instanceExtensionNames);
  9403. SET_STRING_ERROR_AND_RETURN("Required Vulkan instance extensions not supported", false);
  9404. }
  9405. if (renderer->supportsDebugUtils) {
  9406. // Append the debug extension
  9407. instanceExtensionNames[instanceExtensionCount++] =
  9408. VK_EXT_DEBUG_UTILS_EXTENSION_NAME;
  9409. } else {
  9410. SDL_LogWarn(
  9411. SDL_LOG_CATEGORY_GPU,
  9412. "%s is not supported!",
  9413. VK_EXT_DEBUG_UTILS_EXTENSION_NAME);
  9414. }
  9415. if (renderer->supportsColorspace) {
  9416. // Append colorspace extension
  9417. instanceExtensionNames[instanceExtensionCount++] =
  9418. VK_EXT_SWAPCHAIN_COLOR_SPACE_EXTENSION_NAME;
  9419. }
  9420. if (renderer->supportsPhysicalDeviceProperties2) {
  9421. // Append KHR_physical_device_properties2 extension
  9422. instanceExtensionNames[instanceExtensionCount++] =
  9423. VK_KHR_GET_PHYSICAL_DEVICE_PROPERTIES_2_EXTENSION_NAME;
  9424. }
  9425. createInfo.sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO;
  9426. createInfo.pNext = NULL;
  9427. createInfo.flags = createFlags;
  9428. createInfo.pApplicationInfo = &appInfo;
  9429. createInfo.ppEnabledLayerNames = layerNames;
  9430. createInfo.enabledExtensionCount = instanceExtensionCount;
  9431. createInfo.ppEnabledExtensionNames = instanceExtensionNames;
  9432. if (renderer->debugMode) {
  9433. createInfo.enabledLayerCount = SDL_arraysize(layerNames);
  9434. if (!VULKAN_INTERNAL_CheckValidationLayers(
  9435. layerNames,
  9436. createInfo.enabledLayerCount)) {
  9437. SDL_LogWarn(SDL_LOG_CATEGORY_GPU, "Validation layers not found, continuing without validation");
  9438. createInfo.enabledLayerCount = 0;
  9439. } else {
  9440. SDL_LogInfo(SDL_LOG_CATEGORY_GPU, "Validation layers enabled, expect debug level performance!");
  9441. }
  9442. } else {
  9443. createInfo.enabledLayerCount = 0;
  9444. }
  9445. vulkanResult = vkCreateInstance(&createInfo, NULL, &renderer->instance);
  9446. SDL_stack_free((char *)instanceExtensionNames);
  9447. if (vulkanResult != VK_SUCCESS) {
  9448. CHECK_VULKAN_ERROR_AND_RETURN(vulkanResult, vkCreateInstance, 0);
  9449. }
  9450. return 1;
  9451. }
  9452. static bool VULKAN_INTERNAL_GetDeviceRank(
  9453. VulkanRenderer *renderer,
  9454. VkPhysicalDevice physicalDevice,
  9455. VulkanExtensions *physicalDeviceExtensions,
  9456. Uint64 *deviceRank)
  9457. {
  9458. static const Uint8 DEVICE_PRIORITY_HIGHPERFORMANCE[] = {
  9459. 0, // VK_PHYSICAL_DEVICE_TYPE_OTHER
  9460. 3, // VK_PHYSICAL_DEVICE_TYPE_INTEGRATED_GPU
  9461. 4, // VK_PHYSICAL_DEVICE_TYPE_DISCRETE_GPU
  9462. 2, // VK_PHYSICAL_DEVICE_TYPE_VIRTUAL_GPU
  9463. 1 // VK_PHYSICAL_DEVICE_TYPE_CPU
  9464. };
  9465. static const Uint8 DEVICE_PRIORITY_LOWPOWER[] = {
  9466. 0, // VK_PHYSICAL_DEVICE_TYPE_OTHER
  9467. 4, // VK_PHYSICAL_DEVICE_TYPE_INTEGRATED_GPU
  9468. 3, // VK_PHYSICAL_DEVICE_TYPE_DISCRETE_GPU
  9469. 2, // VK_PHYSICAL_DEVICE_TYPE_VIRTUAL_GPU
  9470. 1 // VK_PHYSICAL_DEVICE_TYPE_CPU
  9471. };
  9472. const Uint8 *devicePriority = renderer->preferLowPower ? DEVICE_PRIORITY_LOWPOWER : DEVICE_PRIORITY_HIGHPERFORMANCE;
  9473. VkPhysicalDeviceType deviceType;
  9474. if (physicalDeviceExtensions->MSFT_layered_driver) {
  9475. VkPhysicalDeviceProperties2KHR physicalDeviceProperties;
  9476. VkPhysicalDeviceLayeredDriverPropertiesMSFT physicalDeviceLayeredDriverProperties;
  9477. physicalDeviceProperties.sType =
  9478. VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PROPERTIES_2;
  9479. physicalDeviceProperties.pNext = &physicalDeviceLayeredDriverProperties;
  9480. physicalDeviceLayeredDriverProperties.sType =
  9481. VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_LAYERED_DRIVER_PROPERTIES_MSFT;
  9482. physicalDeviceLayeredDriverProperties.pNext = NULL;
  9483. renderer->vkGetPhysicalDeviceProperties2KHR(
  9484. physicalDevice,
  9485. &physicalDeviceProperties);
  9486. if (physicalDeviceLayeredDriverProperties.underlyingAPI != VK_LAYERED_DRIVER_UNDERLYING_API_NONE_MSFT) {
  9487. deviceType = VK_PHYSICAL_DEVICE_TYPE_OTHER;
  9488. } else {
  9489. deviceType = physicalDeviceProperties.properties.deviceType;
  9490. }
  9491. } else {
  9492. VkPhysicalDeviceProperties physicalDeviceProperties;
  9493. renderer->vkGetPhysicalDeviceProperties(
  9494. physicalDevice,
  9495. &physicalDeviceProperties);
  9496. deviceType = physicalDeviceProperties.deviceType;
  9497. }
  9498. if (renderer->requireHardwareAcceleration) {
  9499. if (deviceType != VK_PHYSICAL_DEVICE_TYPE_DISCRETE_GPU &&
  9500. deviceType != VK_PHYSICAL_DEVICE_TYPE_INTEGRATED_GPU &&
  9501. deviceType != VK_PHYSICAL_DEVICE_TYPE_VIRTUAL_GPU) {
  9502. // In addition to CPU, "Other" drivers (including layered drivers) don't count as hardware-accelerated
  9503. return 0;
  9504. }
  9505. }
  9506. /* Apply a large bias on the devicePriority so that we always respect the order in the priority arrays.
  9507. * We also rank by e.g. VRAM which should have less influence than the device type.
  9508. */
  9509. Uint64 devicePriorityValue = devicePriority[deviceType] * 1000000;
  9510. if (*deviceRank < devicePriorityValue) {
  9511. /* This device outranks the best device we've found so far!
  9512. * This includes a dedicated GPU that has less features than an
  9513. * integrated GPU, because this is a freak case that is almost
  9514. * never intentionally desired by the end user
  9515. */
  9516. *deviceRank = devicePriorityValue;
  9517. } else if (*deviceRank > devicePriorityValue) {
  9518. /* Device is outranked by a previous device, don't even try to
  9519. * run a query and reset the rank to avoid overwrites
  9520. */
  9521. *deviceRank = 0;
  9522. return false;
  9523. }
  9524. /* If we prefer high performance, sum up all device local memory (rounded to megabytes)
  9525. * to deviceRank. In the niche case of someone having multiple dedicated GPUs in the same
  9526. * system, this theoretically picks the most powerful one (or at least the one with the
  9527. * most memory!)
  9528. *
  9529. * We do this *after* discarding all non suitable devices, which means if this computer
  9530. * has multiple dedicated GPUs that all meet our criteria, *and* the user asked for high
  9531. * performance, then we always pick the GPU with more VRAM.
  9532. */
  9533. if (!renderer->preferLowPower) {
  9534. Uint32 i;
  9535. Uint64 videoMemory = 0;
  9536. VkPhysicalDeviceMemoryProperties deviceMemory;
  9537. renderer->vkGetPhysicalDeviceMemoryProperties(physicalDevice, &deviceMemory);
  9538. for (i = 0; i < deviceMemory.memoryHeapCount; i++) {
  9539. VkMemoryHeap heap = deviceMemory.memoryHeaps[i];
  9540. if (heap.flags & VK_MEMORY_HEAP_DEVICE_LOCAL_BIT) {
  9541. videoMemory += heap.size;
  9542. }
  9543. }
  9544. // Round it to megabytes (as per the vulkan spec videoMemory is in bytes)
  9545. Uint64 videoMemoryRounded = videoMemory / 1024 / 1024;
  9546. *deviceRank += videoMemoryRounded;
  9547. }
  9548. return true;
  9549. }
  9550. static Uint8 VULKAN_INTERNAL_IsDeviceSuitable(
  9551. VulkanRenderer *renderer,
  9552. VkPhysicalDevice physicalDevice,
  9553. VulkanExtensions *physicalDeviceExtensions,
  9554. Uint32 *queueFamilyIndex)
  9555. {
  9556. Uint32 queueFamilyCount, queueFamilyRank, queueFamilyBest;
  9557. VkQueueFamilyProperties *queueProps;
  9558. bool supportsPresent;
  9559. VkPhysicalDeviceFeatures deviceFeatures;
  9560. Uint32 i;
  9561. renderer->vkGetPhysicalDeviceFeatures(
  9562. physicalDevice,
  9563. &deviceFeatures);
  9564. if ((!deviceFeatures.independentBlend && renderer->desiredDeviceFeatures.independentBlend) ||
  9565. (!deviceFeatures.imageCubeArray && renderer->desiredDeviceFeatures.imageCubeArray) ||
  9566. (!deviceFeatures.depthClamp && renderer->desiredDeviceFeatures.depthClamp) ||
  9567. (!deviceFeatures.shaderClipDistance && renderer->desiredDeviceFeatures.shaderClipDistance) ||
  9568. (!deviceFeatures.drawIndirectFirstInstance && renderer->desiredDeviceFeatures.drawIndirectFirstInstance) ||
  9569. (!deviceFeatures.sampleRateShading && renderer->desiredDeviceFeatures.sampleRateShading) ||
  9570. (!deviceFeatures.samplerAnisotropy && renderer->desiredDeviceFeatures.samplerAnisotropy)) {
  9571. return 0;
  9572. }
  9573. if (!VULKAN_INTERNAL_CheckDeviceExtensions(
  9574. renderer,
  9575. physicalDevice,
  9576. physicalDeviceExtensions)) {
  9577. return 0;
  9578. }
  9579. renderer->vkGetPhysicalDeviceQueueFamilyProperties(
  9580. physicalDevice,
  9581. &queueFamilyCount,
  9582. NULL);
  9583. queueProps = SDL_stack_alloc(
  9584. VkQueueFamilyProperties,
  9585. queueFamilyCount);
  9586. renderer->vkGetPhysicalDeviceQueueFamilyProperties(
  9587. physicalDevice,
  9588. &queueFamilyCount,
  9589. queueProps);
  9590. queueFamilyBest = 0;
  9591. *queueFamilyIndex = SDL_MAX_UINT32;
  9592. for (i = 0; i < queueFamilyCount; i += 1) {
  9593. supportsPresent = SDL_Vulkan_GetPresentationSupport(
  9594. renderer->instance,
  9595. physicalDevice,
  9596. i);
  9597. if (!supportsPresent ||
  9598. !(queueProps[i].queueFlags & VK_QUEUE_GRAPHICS_BIT)) {
  9599. // Not a graphics family, ignore.
  9600. continue;
  9601. }
  9602. /* The queue family bitflags are kind of annoying.
  9603. *
  9604. * We of course need a graphics family, but we ideally want the
  9605. * _primary_ graphics family. The spec states that at least one
  9606. * graphics family must also be a compute family, so generally
  9607. * drivers make that the first one. But hey, maybe something
  9608. * genuinely can't do compute or something, and FNA doesn't
  9609. * need it, so we'll be open to a non-compute queue family.
  9610. *
  9611. * Additionally, it's common to see the primary queue family
  9612. * have the transfer bit set, which is great! But this is
  9613. * actually optional; it's impossible to NOT have transfers in
  9614. * graphics/compute but it _is_ possible for a graphics/compute
  9615. * family, even the primary one, to just decide not to set the
  9616. * bitflag. Admittedly, a driver may want to isolate transfer
  9617. * queues to a dedicated family so that queues made solely for
  9618. * transfers can have an optimized DMA queue.
  9619. *
  9620. * That, or the driver author got lazy and decided not to set
  9621. * the bit. Looking at you, Android.
  9622. *
  9623. * -flibit
  9624. */
  9625. if (queueProps[i].queueFlags & VK_QUEUE_COMPUTE_BIT) {
  9626. if (queueProps[i].queueFlags & VK_QUEUE_TRANSFER_BIT) {
  9627. // Has all attribs!
  9628. queueFamilyRank = 3;
  9629. } else {
  9630. // Probably has a DMA transfer queue family
  9631. queueFamilyRank = 2;
  9632. }
  9633. } else {
  9634. // Just a graphics family, probably has something better
  9635. queueFamilyRank = 1;
  9636. }
  9637. if (queueFamilyRank > queueFamilyBest) {
  9638. *queueFamilyIndex = i;
  9639. queueFamilyBest = queueFamilyRank;
  9640. }
  9641. }
  9642. SDL_stack_free(queueProps);
  9643. if (*queueFamilyIndex == SDL_MAX_UINT32) {
  9644. // Somehow no graphics queues existed. Compute-only device?
  9645. return 0;
  9646. }
  9647. // FIXME: Need better structure for checking vs storing swapchain support details
  9648. return 1;
  9649. }
  9650. static Uint8 VULKAN_INTERNAL_DeterminePhysicalDevice(VulkanRenderer *renderer)
  9651. {
  9652. VkResult vulkanResult;
  9653. VkPhysicalDevice *physicalDevices;
  9654. VulkanExtensions *physicalDeviceExtensions;
  9655. Uint32 i, physicalDeviceCount;
  9656. Sint32 suitableIndex;
  9657. Uint32 suitableQueueFamilyIndex;
  9658. Uint64 highestRank;
  9659. vulkanResult = renderer->vkEnumeratePhysicalDevices(
  9660. renderer->instance,
  9661. &physicalDeviceCount,
  9662. NULL);
  9663. CHECK_VULKAN_ERROR_AND_RETURN(vulkanResult, vkEnumeratePhysicalDevices, 0);
  9664. if (physicalDeviceCount == 0) {
  9665. SDL_LogInfo(SDL_LOG_CATEGORY_GPU, "Failed to find any GPUs with Vulkan support");
  9666. return 0;
  9667. }
  9668. physicalDevices = SDL_stack_alloc(VkPhysicalDevice, physicalDeviceCount);
  9669. physicalDeviceExtensions = SDL_stack_alloc(VulkanExtensions, physicalDeviceCount);
  9670. vulkanResult = renderer->vkEnumeratePhysicalDevices(
  9671. renderer->instance,
  9672. &physicalDeviceCount,
  9673. physicalDevices);
  9674. /* This should be impossible to hit, but from what I can tell this can
  9675. * be triggered not because the array is too small, but because there
  9676. * were drivers that turned out to be bogus, so this is the loader's way
  9677. * of telling us that the list is now smaller than expected :shrug:
  9678. */
  9679. if (vulkanResult == VK_INCOMPLETE) {
  9680. SDL_LogWarn(SDL_LOG_CATEGORY_GPU, "vkEnumeratePhysicalDevices returned VK_INCOMPLETE, will keep trying anyway...");
  9681. vulkanResult = VK_SUCCESS;
  9682. }
  9683. if (vulkanResult != VK_SUCCESS) {
  9684. SDL_LogWarn(
  9685. SDL_LOG_CATEGORY_GPU,
  9686. "vkEnumeratePhysicalDevices failed: %s",
  9687. VkErrorMessages(vulkanResult));
  9688. SDL_stack_free(physicalDevices);
  9689. SDL_stack_free(physicalDeviceExtensions);
  9690. return 0;
  9691. }
  9692. // Any suitable device will do, but we'd like the best
  9693. suitableIndex = -1;
  9694. suitableQueueFamilyIndex = 0;
  9695. highestRank = 0;
  9696. for (i = 0; i < physicalDeviceCount; i += 1) {
  9697. Uint32 queueFamilyIndex;
  9698. Uint64 deviceRank;
  9699. if (!VULKAN_INTERNAL_IsDeviceSuitable(
  9700. renderer,
  9701. physicalDevices[i],
  9702. &physicalDeviceExtensions[i],
  9703. &queueFamilyIndex)) {
  9704. // Device does not meet the minimum requirements, skip it entirely
  9705. continue;
  9706. }
  9707. deviceRank = highestRank;
  9708. if (VULKAN_INTERNAL_GetDeviceRank(
  9709. renderer,
  9710. physicalDevices[i],
  9711. &physicalDeviceExtensions[i],
  9712. &deviceRank)) {
  9713. /* Use this for rendering.
  9714. * Note that this may override a previous device that
  9715. * supports rendering, but shares the same device rank.
  9716. */
  9717. suitableIndex = i;
  9718. suitableQueueFamilyIndex = queueFamilyIndex;
  9719. highestRank = deviceRank;
  9720. }
  9721. }
  9722. if (suitableIndex != -1) {
  9723. renderer->supports = physicalDeviceExtensions[suitableIndex];
  9724. renderer->physicalDevice = physicalDevices[suitableIndex];
  9725. renderer->queueFamilyIndex = suitableQueueFamilyIndex;
  9726. } else {
  9727. SDL_stack_free(physicalDevices);
  9728. SDL_stack_free(physicalDeviceExtensions);
  9729. return 0;
  9730. }
  9731. renderer->physicalDeviceProperties.sType =
  9732. VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PROPERTIES_2;
  9733. if (renderer->supports.KHR_driver_properties) {
  9734. renderer->physicalDeviceDriverProperties.sType =
  9735. VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_DRIVER_PROPERTIES_KHR;
  9736. renderer->physicalDeviceDriverProperties.pNext = NULL;
  9737. renderer->physicalDeviceProperties.pNext =
  9738. &renderer->physicalDeviceDriverProperties;
  9739. renderer->vkGetPhysicalDeviceProperties2KHR(
  9740. renderer->physicalDevice,
  9741. &renderer->physicalDeviceProperties);
  9742. } else {
  9743. renderer->physicalDeviceProperties.pNext = NULL;
  9744. renderer->vkGetPhysicalDeviceProperties(
  9745. renderer->physicalDevice,
  9746. &renderer->physicalDeviceProperties.properties);
  9747. }
  9748. renderer->vkGetPhysicalDeviceMemoryProperties(
  9749. renderer->physicalDevice,
  9750. &renderer->memoryProperties);
  9751. SDL_stack_free(physicalDevices);
  9752. SDL_stack_free(physicalDeviceExtensions);
  9753. return 1;
  9754. }
  9755. static Uint8 VULKAN_INTERNAL_CreateLogicalDevice(
  9756. VulkanRenderer *renderer)
  9757. {
  9758. VkResult vulkanResult;
  9759. VkDeviceCreateInfo deviceCreateInfo;
  9760. VkPhysicalDeviceFeatures haveDeviceFeatures;
  9761. VkPhysicalDevicePortabilitySubsetFeaturesKHR portabilityFeatures;
  9762. const char **deviceExtensions;
  9763. VkDeviceQueueCreateInfo queueCreateInfo;
  9764. float queuePriority = 1.0f;
  9765. queueCreateInfo.sType =
  9766. VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO;
  9767. queueCreateInfo.pNext = NULL;
  9768. queueCreateInfo.flags = 0;
  9769. queueCreateInfo.queueFamilyIndex = renderer->queueFamilyIndex;
  9770. queueCreateInfo.queueCount = 1;
  9771. queueCreateInfo.pQueuePriorities = &queuePriority;
  9772. // check feature support
  9773. renderer->vkGetPhysicalDeviceFeatures(
  9774. renderer->physicalDevice,
  9775. &haveDeviceFeatures);
  9776. // specifying used device features
  9777. if (haveDeviceFeatures.fillModeNonSolid) {
  9778. renderer->desiredDeviceFeatures.fillModeNonSolid = VK_TRUE;
  9779. renderer->supportsFillModeNonSolid = true;
  9780. }
  9781. if (haveDeviceFeatures.multiDrawIndirect) {
  9782. renderer->desiredDeviceFeatures.multiDrawIndirect = VK_TRUE;
  9783. renderer->supportsMultiDrawIndirect = true;
  9784. }
  9785. // creating the logical device
  9786. deviceCreateInfo.sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO;
  9787. if (renderer->supports.KHR_portability_subset) {
  9788. portabilityFeatures.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PORTABILITY_SUBSET_FEATURES_KHR;
  9789. portabilityFeatures.pNext = NULL;
  9790. portabilityFeatures.constantAlphaColorBlendFactors = VK_FALSE;
  9791. portabilityFeatures.events = VK_FALSE;
  9792. portabilityFeatures.imageViewFormatReinterpretation = VK_FALSE;
  9793. portabilityFeatures.imageViewFormatSwizzle = VK_TRUE;
  9794. portabilityFeatures.imageView2DOn3DImage = VK_FALSE;
  9795. portabilityFeatures.multisampleArrayImage = VK_FALSE;
  9796. portabilityFeatures.mutableComparisonSamplers = VK_FALSE;
  9797. portabilityFeatures.pointPolygons = VK_FALSE;
  9798. portabilityFeatures.samplerMipLodBias = VK_FALSE; // Technically should be true, but eh
  9799. portabilityFeatures.separateStencilMaskRef = VK_FALSE;
  9800. portabilityFeatures.shaderSampleRateInterpolationFunctions = VK_FALSE;
  9801. portabilityFeatures.tessellationIsolines = VK_FALSE;
  9802. portabilityFeatures.tessellationPointMode = VK_FALSE;
  9803. portabilityFeatures.triangleFans = VK_FALSE;
  9804. portabilityFeatures.vertexAttributeAccessBeyondStride = VK_FALSE;
  9805. deviceCreateInfo.pNext = &portabilityFeatures;
  9806. } else {
  9807. deviceCreateInfo.pNext = NULL;
  9808. }
  9809. deviceCreateInfo.flags = 0;
  9810. deviceCreateInfo.queueCreateInfoCount = 1;
  9811. deviceCreateInfo.pQueueCreateInfos = &queueCreateInfo;
  9812. deviceCreateInfo.enabledLayerCount = 0;
  9813. deviceCreateInfo.ppEnabledLayerNames = NULL;
  9814. deviceCreateInfo.enabledExtensionCount = GetDeviceExtensionCount(
  9815. &renderer->supports);
  9816. deviceExtensions = SDL_stack_alloc(
  9817. const char *,
  9818. deviceCreateInfo.enabledExtensionCount);
  9819. CreateDeviceExtensionArray(&renderer->supports, deviceExtensions);
  9820. deviceCreateInfo.ppEnabledExtensionNames = deviceExtensions;
  9821. deviceCreateInfo.pEnabledFeatures = &renderer->desiredDeviceFeatures;
  9822. vulkanResult = renderer->vkCreateDevice(
  9823. renderer->physicalDevice,
  9824. &deviceCreateInfo,
  9825. NULL,
  9826. &renderer->logicalDevice);
  9827. SDL_stack_free((void *)deviceExtensions);
  9828. CHECK_VULKAN_ERROR_AND_RETURN(vulkanResult, vkCreateDevice, 0);
  9829. // Load vkDevice entry points
  9830. #define VULKAN_DEVICE_FUNCTION(func) \
  9831. renderer->func = (PFN_##func) \
  9832. renderer->vkGetDeviceProcAddr( \
  9833. renderer->logicalDevice, \
  9834. #func);
  9835. #include "SDL_gpu_vulkan_vkfuncs.h"
  9836. renderer->vkGetDeviceQueue(
  9837. renderer->logicalDevice,
  9838. renderer->queueFamilyIndex,
  9839. 0,
  9840. &renderer->unifiedQueue);
  9841. return 1;
  9842. }
  9843. static void VULKAN_INTERNAL_LoadEntryPoints(void)
  9844. {
  9845. // Required for MoltenVK support
  9846. SDL_setenv_unsafe("MVK_CONFIG_FULL_IMAGE_VIEW_SWIZZLE", "1", 1);
  9847. // Load Vulkan entry points
  9848. if (!SDL_Vulkan_LoadLibrary(NULL)) {
  9849. SDL_LogWarn(SDL_LOG_CATEGORY_GPU, "Vulkan: SDL_Vulkan_LoadLibrary failed!");
  9850. return;
  9851. }
  9852. #ifdef HAVE_GCC_DIAGNOSTIC_PRAGMA
  9853. #pragma GCC diagnostic push
  9854. #pragma GCC diagnostic ignored "-Wpedantic"
  9855. #endif
  9856. vkGetInstanceProcAddr = (PFN_vkGetInstanceProcAddr)SDL_Vulkan_GetVkGetInstanceProcAddr();
  9857. #ifdef HAVE_GCC_DIAGNOSTIC_PRAGMA
  9858. #pragma GCC diagnostic pop
  9859. #endif
  9860. if (vkGetInstanceProcAddr == NULL) {
  9861. SDL_LogWarn(
  9862. SDL_LOG_CATEGORY_GPU,
  9863. "SDL_Vulkan_GetVkGetInstanceProcAddr(): %s",
  9864. SDL_GetError());
  9865. return;
  9866. }
  9867. #define VULKAN_GLOBAL_FUNCTION(name) \
  9868. name = (PFN_##name)vkGetInstanceProcAddr(VK_NULL_HANDLE, #name); \
  9869. if (name == NULL) { \
  9870. SDL_LogWarn(SDL_LOG_CATEGORY_GPU, "vkGetInstanceProcAddr(VK_NULL_HANDLE, \"" #name "\") failed"); \
  9871. return; \
  9872. }
  9873. #include "SDL_gpu_vulkan_vkfuncs.h"
  9874. }
  9875. static bool VULKAN_INTERNAL_PrepareVulkan(
  9876. VulkanRenderer *renderer)
  9877. {
  9878. VULKAN_INTERNAL_LoadEntryPoints();
  9879. if (!VULKAN_INTERNAL_CreateInstance(renderer)) {
  9880. SDL_LogWarn(SDL_LOG_CATEGORY_GPU, "Vulkan: Could not create Vulkan instance");
  9881. return false;
  9882. }
  9883. #define VULKAN_INSTANCE_FUNCTION(func) \
  9884. renderer->func = (PFN_##func)vkGetInstanceProcAddr(renderer->instance, #func);
  9885. #include "SDL_gpu_vulkan_vkfuncs.h"
  9886. if (!VULKAN_INTERNAL_DeterminePhysicalDevice(renderer)) {
  9887. SDL_LogWarn(SDL_LOG_CATEGORY_GPU, "Vulkan: Failed to determine a suitable physical device");
  9888. return false;
  9889. }
  9890. return true;
  9891. }
  9892. static bool VULKAN_PrepareDriver(SDL_VideoDevice *_this, SDL_PropertiesID props)
  9893. {
  9894. // Set up dummy VulkanRenderer
  9895. VulkanRenderer *renderer;
  9896. bool result = false;
  9897. if (!SDL_GetBooleanProperty(props, SDL_PROP_GPU_DEVICE_CREATE_SHADERS_SPIRV_BOOLEAN, false)) {
  9898. return false;
  9899. }
  9900. if (_this->Vulkan_CreateSurface == NULL) {
  9901. return false;
  9902. }
  9903. if (!SDL_Vulkan_LoadLibrary(NULL)) {
  9904. return false;
  9905. }
  9906. renderer = (VulkanRenderer *)SDL_calloc(1, sizeof(*renderer));
  9907. if (renderer) {
  9908. // This needs to be set early for log filtering
  9909. renderer->debugMode = SDL_GetBooleanProperty(props, SDL_PROP_GPU_DEVICE_CREATE_DEBUGMODE_BOOLEAN, false);
  9910. // Opt out device features (higher compatibility in exchange for reduced functionality)
  9911. renderer->desiredDeviceFeatures.samplerAnisotropy = SDL_GetBooleanProperty(props, SDL_PROP_GPU_DEVICE_CREATE_FEATURE_ANISOTROPY_BOOLEAN, true) ? VK_TRUE : VK_FALSE;
  9912. renderer->desiredDeviceFeatures.depthClamp = SDL_GetBooleanProperty(props, SDL_PROP_GPU_DEVICE_CREATE_FEATURE_DEPTH_CLAMPING_BOOLEAN, true) ? VK_TRUE : VK_FALSE;
  9913. renderer->desiredDeviceFeatures.shaderClipDistance = SDL_GetBooleanProperty(props, SDL_PROP_GPU_DEVICE_CREATE_FEATURE_CLIP_DISTANCE_BOOLEAN, true) ? VK_TRUE : VK_FALSE;
  9914. renderer->desiredDeviceFeatures.drawIndirectFirstInstance = SDL_GetBooleanProperty(props, SDL_PROP_GPU_DEVICE_CREATE_FEATURE_INDIRECT_DRAW_FIRST_INSTANCE_BOOLEAN, true) ? VK_TRUE : VK_FALSE;
  9915. // These features have near universal support so they are always enabled
  9916. renderer->desiredDeviceFeatures.independentBlend = VK_TRUE;
  9917. renderer->desiredDeviceFeatures.sampleRateShading = VK_TRUE;
  9918. renderer->desiredDeviceFeatures.imageCubeArray = VK_TRUE;
  9919. renderer->requireHardwareAcceleration = SDL_GetBooleanProperty(props, SDL_PROP_GPU_DEVICE_CREATE_VULKAN_REQUIRE_HARDWARE_ACCELERATION_BOOLEAN, false);
  9920. result = VULKAN_INTERNAL_PrepareVulkan(renderer);
  9921. if (result) {
  9922. renderer->vkDestroyInstance(renderer->instance, NULL);
  9923. }
  9924. SDL_free(renderer);
  9925. }
  9926. SDL_Vulkan_UnloadLibrary();
  9927. return result;
  9928. }
  9929. static SDL_GPUDevice *VULKAN_CreateDevice(bool debugMode, bool preferLowPower, SDL_PropertiesID props)
  9930. {
  9931. VulkanRenderer *renderer;
  9932. SDL_GPUDevice *result;
  9933. Uint32 i;
  9934. bool verboseLogs = SDL_GetBooleanProperty(
  9935. props,
  9936. SDL_PROP_GPU_DEVICE_CREATE_VERBOSE_BOOLEAN,
  9937. true);
  9938. if (!SDL_Vulkan_LoadLibrary(NULL)) {
  9939. SDL_assert(!"This should have failed in PrepareDevice first!");
  9940. return NULL;
  9941. }
  9942. renderer = (VulkanRenderer *)SDL_calloc(1, sizeof(*renderer));
  9943. if (!renderer) {
  9944. SDL_Vulkan_UnloadLibrary();
  9945. return NULL;
  9946. }
  9947. renderer->debugMode = debugMode;
  9948. renderer->preferLowPower = preferLowPower;
  9949. renderer->allowedFramesInFlight = 2;
  9950. // Opt out device features (higher compatibility in exchange for reduced functionality)
  9951. renderer->desiredDeviceFeatures.samplerAnisotropy = SDL_GetBooleanProperty(props, SDL_PROP_GPU_DEVICE_CREATE_FEATURE_ANISOTROPY_BOOLEAN, true) ? VK_TRUE : VK_FALSE;
  9952. renderer->desiredDeviceFeatures.depthClamp = SDL_GetBooleanProperty(props, SDL_PROP_GPU_DEVICE_CREATE_FEATURE_DEPTH_CLAMPING_BOOLEAN, true) ? VK_TRUE : VK_FALSE;
  9953. renderer->desiredDeviceFeatures.shaderClipDistance = SDL_GetBooleanProperty(props, SDL_PROP_GPU_DEVICE_CREATE_FEATURE_CLIP_DISTANCE_BOOLEAN, true) ? VK_TRUE : VK_FALSE;
  9954. renderer->desiredDeviceFeatures.drawIndirectFirstInstance = SDL_GetBooleanProperty(props, SDL_PROP_GPU_DEVICE_CREATE_FEATURE_INDIRECT_DRAW_FIRST_INSTANCE_BOOLEAN, true) ? VK_TRUE : VK_FALSE;
  9955. // These features have near universal support so they are always enabled
  9956. renderer->desiredDeviceFeatures.independentBlend = VK_TRUE;
  9957. renderer->desiredDeviceFeatures.sampleRateShading = VK_TRUE;
  9958. renderer->desiredDeviceFeatures.imageCubeArray = VK_TRUE;
  9959. renderer->requireHardwareAcceleration = SDL_GetBooleanProperty(props, SDL_PROP_GPU_DEVICE_CREATE_VULKAN_REQUIRE_HARDWARE_ACCELERATION_BOOLEAN, false);
  9960. if (!VULKAN_INTERNAL_PrepareVulkan(renderer)) {
  9961. SET_STRING_ERROR("Failed to initialize Vulkan!");
  9962. SDL_free(renderer);
  9963. SDL_Vulkan_UnloadLibrary();
  9964. return NULL;
  9965. }
  9966. renderer->props = SDL_CreateProperties();
  9967. if (verboseLogs) {
  9968. SDL_LogInfo(SDL_LOG_CATEGORY_GPU, "SDL_GPU Driver: Vulkan");
  9969. }
  9970. // Record device name
  9971. const char *deviceName = renderer->physicalDeviceProperties.properties.deviceName;
  9972. SDL_SetStringProperty(
  9973. renderer->props,
  9974. SDL_PROP_GPU_DEVICE_NAME_STRING,
  9975. deviceName);
  9976. if (verboseLogs) {
  9977. SDL_LogInfo(SDL_LOG_CATEGORY_GPU, "Vulkan Device: %s", deviceName);
  9978. }
  9979. // Record driver version. This is provided as a backup if
  9980. // VK_KHR_driver_properties is not available but as most drivers support it
  9981. // this property should be rarely used.
  9982. //
  9983. // This uses a vendor-specific encoding and it isn't well documented. The
  9984. // vendor ID is the registered PCI ID of the vendor and can be found in
  9985. // online databases.
  9986. char driverVer[64];
  9987. Uint32 rawDriverVer = renderer->physicalDeviceProperties.properties.driverVersion;
  9988. Uint32 vendorId = renderer->physicalDeviceProperties.properties.vendorID;
  9989. if (vendorId == 0x10de) {
  9990. // Nvidia uses 10|8|8|6 encoding.
  9991. (void)SDL_snprintf(
  9992. driverVer,
  9993. SDL_arraysize(driverVer),
  9994. "%d.%d.%d.%d",
  9995. (rawDriverVer >> 22) & 0x3ff,
  9996. (rawDriverVer >> 14) & 0xff,
  9997. (rawDriverVer >> 6) & 0xff,
  9998. rawDriverVer & 0x3f);
  9999. }
  10000. #ifdef SDL_PLATFORM_WINDOWS
  10001. else if (vendorId == 0x8086) {
  10002. // Intel uses 18|14 encoding on Windows only.
  10003. (void)SDL_snprintf(
  10004. driverVer,
  10005. SDL_arraysize(driverVer),
  10006. "%d.%d",
  10007. (rawDriverVer >> 14) & 0x3ffff,
  10008. rawDriverVer & 0x3fff);
  10009. }
  10010. #endif
  10011. else {
  10012. // Assume standard Vulkan 10|10|12 encoding for everything else. AMD and
  10013. // Mesa are known to use this encoding.
  10014. (void)SDL_snprintf(
  10015. driverVer,
  10016. SDL_arraysize(driverVer),
  10017. "%d.%d.%d",
  10018. (rawDriverVer >> 22) & 0x3ff,
  10019. (rawDriverVer >> 12) & 0x3ff,
  10020. rawDriverVer & 0xfff);
  10021. }
  10022. SDL_SetStringProperty(
  10023. renderer->props,
  10024. SDL_PROP_GPU_DEVICE_DRIVER_VERSION_STRING,
  10025. driverVer);
  10026. // Log this only if VK_KHR_driver_properties is not available.
  10027. if (renderer->supports.KHR_driver_properties) {
  10028. // Record driver name and version
  10029. const char *driverName = renderer->physicalDeviceDriverProperties.driverName;
  10030. const char *driverInfo = renderer->physicalDeviceDriverProperties.driverInfo;
  10031. SDL_SetStringProperty(
  10032. renderer->props,
  10033. SDL_PROP_GPU_DEVICE_DRIVER_NAME_STRING,
  10034. driverName);
  10035. SDL_SetStringProperty(
  10036. renderer->props,
  10037. SDL_PROP_GPU_DEVICE_DRIVER_INFO_STRING,
  10038. driverInfo);
  10039. if (verboseLogs) {
  10040. // FIXME: driverInfo can be a multiline string.
  10041. SDL_LogInfo(SDL_LOG_CATEGORY_GPU, "Vulkan Driver: %s %s", driverName, driverInfo);
  10042. }
  10043. // Record conformance level
  10044. if (verboseLogs) {
  10045. char conformance[64];
  10046. (void)SDL_snprintf(
  10047. conformance,
  10048. SDL_arraysize(conformance),
  10049. "%u.%u.%u.%u",
  10050. renderer->physicalDeviceDriverProperties.conformanceVersion.major,
  10051. renderer->physicalDeviceDriverProperties.conformanceVersion.minor,
  10052. renderer->physicalDeviceDriverProperties.conformanceVersion.subminor,
  10053. renderer->physicalDeviceDriverProperties.conformanceVersion.patch);
  10054. SDL_LogInfo(SDL_LOG_CATEGORY_GPU, "Vulkan Conformance: %s", conformance);
  10055. }
  10056. } else {
  10057. if (verboseLogs) {
  10058. SDL_LogInfo(SDL_LOG_CATEGORY_GPU, "Vulkan Driver: %s", driverVer);
  10059. }
  10060. }
  10061. if (!VULKAN_INTERNAL_CreateLogicalDevice(
  10062. renderer)) {
  10063. SET_STRING_ERROR("Failed to create logical device!");
  10064. SDL_free(renderer);
  10065. SDL_Vulkan_UnloadLibrary();
  10066. return NULL;
  10067. }
  10068. // FIXME: just move this into this function
  10069. result = (SDL_GPUDevice *)SDL_calloc(1, sizeof(SDL_GPUDevice));
  10070. ASSIGN_DRIVER(VULKAN)
  10071. result->driverData = (SDL_GPURenderer *)renderer;
  10072. result->shader_formats = SDL_GPU_SHADERFORMAT_SPIRV;
  10073. /*
  10074. * Create initial swapchain array
  10075. */
  10076. renderer->claimedWindowCapacity = 1;
  10077. renderer->claimedWindowCount = 0;
  10078. renderer->claimedWindows = SDL_malloc(
  10079. renderer->claimedWindowCapacity * sizeof(WindowData *));
  10080. // Threading
  10081. renderer->allocatorLock = SDL_CreateMutex();
  10082. renderer->disposeLock = SDL_CreateMutex();
  10083. renderer->submitLock = SDL_CreateMutex();
  10084. renderer->acquireCommandBufferLock = SDL_CreateMutex();
  10085. renderer->acquireUniformBufferLock = SDL_CreateMutex();
  10086. renderer->renderPassFetchLock = SDL_CreateMutex();
  10087. renderer->framebufferFetchLock = SDL_CreateMutex();
  10088. renderer->graphicsPipelineLayoutFetchLock = SDL_CreateMutex();
  10089. renderer->computePipelineLayoutFetchLock = SDL_CreateMutex();
  10090. renderer->descriptorSetLayoutFetchLock = SDL_CreateMutex();
  10091. renderer->windowLock = SDL_CreateMutex();
  10092. /*
  10093. * Create submitted command buffer list
  10094. */
  10095. renderer->submittedCommandBufferCapacity = 16;
  10096. renderer->submittedCommandBufferCount = 0;
  10097. renderer->submittedCommandBuffers = SDL_malloc(sizeof(VulkanCommandBuffer *) * renderer->submittedCommandBufferCapacity);
  10098. // Memory Allocator
  10099. renderer->memoryAllocator = (VulkanMemoryAllocator *)SDL_malloc(
  10100. sizeof(VulkanMemoryAllocator));
  10101. for (i = 0; i < VK_MAX_MEMORY_TYPES; i += 1) {
  10102. renderer->memoryAllocator->subAllocators[i].memoryTypeIndex = i;
  10103. renderer->memoryAllocator->subAllocators[i].allocations = NULL;
  10104. renderer->memoryAllocator->subAllocators[i].allocationCount = 0;
  10105. renderer->memoryAllocator->subAllocators[i].sortedFreeRegions = SDL_malloc(
  10106. sizeof(VulkanMemoryFreeRegion *) * 4);
  10107. renderer->memoryAllocator->subAllocators[i].sortedFreeRegionCount = 0;
  10108. renderer->memoryAllocator->subAllocators[i].sortedFreeRegionCapacity = 4;
  10109. }
  10110. // Create uniform buffer pool
  10111. renderer->uniformBufferPoolCount = 32;
  10112. renderer->uniformBufferPoolCapacity = 32;
  10113. renderer->uniformBufferPool = SDL_malloc(
  10114. renderer->uniformBufferPoolCapacity * sizeof(VulkanUniformBuffer *));
  10115. for (i = 0; i < renderer->uniformBufferPoolCount; i += 1) {
  10116. renderer->uniformBufferPool[i] = VULKAN_INTERNAL_CreateUniformBuffer(
  10117. renderer,
  10118. UNIFORM_BUFFER_SIZE);
  10119. }
  10120. renderer->descriptorSetCachePoolCapacity = 8;
  10121. renderer->descriptorSetCachePoolCount = 0;
  10122. renderer->descriptorSetCachePool = SDL_calloc(renderer->descriptorSetCachePoolCapacity, sizeof(DescriptorSetCache *));
  10123. SDL_SetAtomicInt(&renderer->layoutResourceID, 0);
  10124. // Device limits
  10125. renderer->minUBOAlignment = (Uint32)renderer->physicalDeviceProperties.properties.limits.minUniformBufferOffsetAlignment;
  10126. // Initialize caches
  10127. renderer->commandPoolHashTable = SDL_CreateHashTable(
  10128. 0, // !!! FIXME: a real guess here, for a _minimum_ if not a maximum, could be useful.
  10129. false, // manually synchronized due to submission timing
  10130. VULKAN_INTERNAL_CommandPoolHashFunction,
  10131. VULKAN_INTERNAL_CommandPoolHashKeyMatch,
  10132. VULKAN_INTERNAL_CommandPoolHashDestroy,
  10133. (void *)renderer);
  10134. renderer->renderPassHashTable = SDL_CreateHashTable(
  10135. 0, // !!! FIXME: a real guess here, for a _minimum_ if not a maximum, could be useful.
  10136. false, // manually synchronized due to lookup timing
  10137. VULKAN_INTERNAL_RenderPassHashFunction,
  10138. VULKAN_INTERNAL_RenderPassHashKeyMatch,
  10139. VULKAN_INTERNAL_RenderPassHashDestroy,
  10140. (void *)renderer);
  10141. renderer->framebufferHashTable = SDL_CreateHashTable(
  10142. 0, // !!! FIXME: a real guess here, for a _minimum_ if not a maximum, could be useful.
  10143. false, // manually synchronized due to iteration
  10144. VULKAN_INTERNAL_FramebufferHashFunction,
  10145. VULKAN_INTERNAL_FramebufferHashKeyMatch,
  10146. VULKAN_INTERNAL_FramebufferHashDestroy,
  10147. (void *)renderer);
  10148. renderer->graphicsPipelineResourceLayoutHashTable = SDL_CreateHashTable(
  10149. 0, // !!! FIXME: a real guess here, for a _minimum_ if not a maximum, could be useful.
  10150. false, // manually synchronized due to lookup timing
  10151. VULKAN_INTERNAL_GraphicsPipelineResourceLayoutHashFunction,
  10152. VULKAN_INTERNAL_GraphicsPipelineResourceLayoutHashKeyMatch,
  10153. VULKAN_INTERNAL_GraphicsPipelineResourceLayoutHashDestroy,
  10154. (void *)renderer);
  10155. renderer->computePipelineResourceLayoutHashTable = SDL_CreateHashTable(
  10156. 0, // !!! FIXME: a real guess here, for a _minimum_ if not a maximum, could be useful.
  10157. false, // manually synchronized due to lookup timing
  10158. VULKAN_INTERNAL_ComputePipelineResourceLayoutHashFunction,
  10159. VULKAN_INTERNAL_ComputePipelineResourceLayoutHashKeyMatch,
  10160. VULKAN_INTERNAL_ComputePipelineResourceLayoutHashDestroy,
  10161. (void *)renderer);
  10162. renderer->descriptorSetLayoutHashTable = SDL_CreateHashTable(
  10163. 0, // !!! FIXME: a real guess here, for a _minimum_ if not a maximum, could be useful.
  10164. false, // manually synchronized due to lookup timing
  10165. VULKAN_INTERNAL_DescriptorSetLayoutHashFunction,
  10166. VULKAN_INTERNAL_DescriptorSetLayoutHashKeyMatch,
  10167. VULKAN_INTERNAL_DescriptorSetLayoutHashDestroy,
  10168. (void *)renderer);
  10169. // Initialize fence pool
  10170. renderer->fencePool.lock = SDL_CreateMutex();
  10171. renderer->fencePool.availableFenceCapacity = 4;
  10172. renderer->fencePool.availableFenceCount = 0;
  10173. renderer->fencePool.availableFences = SDL_malloc(
  10174. renderer->fencePool.availableFenceCapacity * sizeof(VulkanFenceHandle *));
  10175. // Deferred destroy storage
  10176. renderer->texturesToDestroyCapacity = 16;
  10177. renderer->texturesToDestroyCount = 0;
  10178. renderer->texturesToDestroy = (VulkanTexture **)SDL_malloc(
  10179. sizeof(VulkanTexture *) *
  10180. renderer->texturesToDestroyCapacity);
  10181. renderer->buffersToDestroyCapacity = 16;
  10182. renderer->buffersToDestroyCount = 0;
  10183. renderer->buffersToDestroy = SDL_malloc(
  10184. sizeof(VulkanBuffer *) *
  10185. renderer->buffersToDestroyCapacity);
  10186. renderer->samplersToDestroyCapacity = 16;
  10187. renderer->samplersToDestroyCount = 0;
  10188. renderer->samplersToDestroy = SDL_malloc(
  10189. sizeof(VulkanSampler *) *
  10190. renderer->samplersToDestroyCapacity);
  10191. renderer->graphicsPipelinesToDestroyCapacity = 16;
  10192. renderer->graphicsPipelinesToDestroyCount = 0;
  10193. renderer->graphicsPipelinesToDestroy = SDL_malloc(
  10194. sizeof(VulkanGraphicsPipeline *) *
  10195. renderer->graphicsPipelinesToDestroyCapacity);
  10196. renderer->computePipelinesToDestroyCapacity = 16;
  10197. renderer->computePipelinesToDestroyCount = 0;
  10198. renderer->computePipelinesToDestroy = SDL_malloc(
  10199. sizeof(VulkanComputePipeline *) *
  10200. renderer->computePipelinesToDestroyCapacity);
  10201. renderer->shadersToDestroyCapacity = 16;
  10202. renderer->shadersToDestroyCount = 0;
  10203. renderer->shadersToDestroy = SDL_malloc(
  10204. sizeof(VulkanShader *) *
  10205. renderer->shadersToDestroyCapacity);
  10206. renderer->framebuffersToDestroyCapacity = 16;
  10207. renderer->framebuffersToDestroyCount = 0;
  10208. renderer->framebuffersToDestroy = SDL_malloc(
  10209. sizeof(VulkanFramebuffer *) *
  10210. renderer->framebuffersToDestroyCapacity);
  10211. // Defrag state
  10212. renderer->defragInProgress = 0;
  10213. renderer->allocationsToDefragCount = 0;
  10214. renderer->allocationsToDefragCapacity = 4;
  10215. renderer->allocationsToDefrag = SDL_malloc(
  10216. renderer->allocationsToDefragCapacity * sizeof(VulkanMemoryAllocation *));
  10217. return result;
  10218. }
  10219. SDL_GPUBootstrap VulkanDriver = {
  10220. "vulkan",
  10221. VULKAN_PrepareDriver,
  10222. VULKAN_CreateDevice
  10223. };
  10224. #endif // SDL_GPU_VULKAN