SDL_gpu_d3d12.c 356 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2026 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_D3D12
  20. #include "../../events/SDL_windowevents_c.h"
  21. #include "../../core/windows/SDL_windows.h"
  22. #include "../../video/directx/SDL_d3d12.h"
  23. #include "../SDL_sysgpu.h"
  24. #ifdef __IDXGIInfoQueue_INTERFACE_DEFINED__
  25. #define HAVE_IDXGIINFOQUEUE
  26. #endif
  27. // Built-in shaders, compiled with compile_shaders.bat
  28. #define g_FullscreenVert D3D12_FullscreenVert
  29. #define g_BlitFrom2D D3D12_BlitFrom2D
  30. #define g_BlitFrom2DArray D3D12_BlitFrom2DArray
  31. #define g_BlitFrom3D D3D12_BlitFrom3D
  32. #define g_BlitFromCube D3D12_BlitFromCube
  33. #define g_BlitFromCubeArray D3D12_BlitFromCubeArray
  34. #if defined(SDL_PLATFORM_XBOXSERIES)
  35. #include "D3D12_Blit_Series.h"
  36. #elif defined(SDL_PLATFORM_XBOXONE)
  37. #include "D3D12_Blit_One.h"
  38. #else
  39. #include "D3D12_Blit.h"
  40. #endif
  41. #undef g_FullscreenVert
  42. #undef g_BlitFrom2D
  43. #undef g_BlitFrom2DArray
  44. #undef g_BlitFrom3D
  45. #undef g_BlitFromCube
  46. #undef g_BlitFromCubeArray
  47. // Macros
  48. #define SET_ERROR(fmt, msg) \
  49. do { \
  50. if (renderer->debug_mode) { \
  51. SDL_LogError(SDL_LOG_CATEGORY_GPU, fmt, msg); \
  52. } \
  53. SDL_SetError(fmt, msg); \
  54. } while (0)
  55. #define SET_ERROR_AND_RETURN(fmt, msg, ret) \
  56. do { \
  57. if (renderer->debug_mode) { \
  58. SDL_LogError(SDL_LOG_CATEGORY_GPU, fmt, msg); \
  59. } \
  60. SDL_SetError(fmt, msg); \
  61. return ret; \
  62. } while (0)
  63. #define SET_STRING_ERROR_AND_RETURN(msg, ret) SET_ERROR_AND_RETURN("%s", msg, ret)
  64. #define CHECK_D3D12_ERROR_AND_RETURN(msg, ret) \
  65. do { \
  66. if (FAILED(res)) { \
  67. D3D12_INTERNAL_SetError(renderer, msg, res); \
  68. return (ret); \
  69. } \
  70. } while (0)
  71. // Defines
  72. #if defined(_WIN32)
  73. #if defined(SDL_PLATFORM_XBOXSERIES)
  74. #define D3D12_DLL "d3d12_xs.dll"
  75. #elif defined(SDL_PLATFORM_XBOXONE)
  76. #define D3D12_DLL "d3d12_x.dll"
  77. #else
  78. #define D3D12_DLL "d3d12.dll"
  79. #define USE_PIX_RUNTIME
  80. #define WINPIXEVENTRUNTIME_DLL "WinPixEventRuntime.dll"
  81. #endif
  82. #define DXGI_DLL "dxgi.dll"
  83. #define DXGIDEBUG_DLL "dxgidebug.dll"
  84. #elif defined(__APPLE__)
  85. #define D3D12_DLL "libdxvk_d3d12.dylib"
  86. #define DXGI_DLL "libdxvk_dxgi.dylib"
  87. #define DXGIDEBUG_DLL "libdxvk_dxgidebug.dylib"
  88. #else
  89. #define D3D12_DLL "libdxvk_d3d12.so"
  90. #define DXGI_DLL "libdxvk_dxgi.so"
  91. #define DXGIDEBUG_DLL "libdxvk_dxgidebug.so"
  92. #endif
  93. #define D3D12_CREATE_DEVICE_FUNC "D3D12CreateDevice"
  94. #define D3D12_SERIALIZE_ROOT_SIGNATURE_FUNC "D3D12SerializeRootSignature"
  95. #define CREATE_DXGI_FACTORY1_FUNC "CreateDXGIFactory1"
  96. #define DXGI_GET_DEBUG_INTERFACE_FUNC "DXGIGetDebugInterface"
  97. #define D3D12_GET_DEBUG_INTERFACE_FUNC "D3D12GetDebugInterface"
  98. #define D3D12_GET_INTERFACE_FUNC "D3D12GetInterface"
  99. #define WINDOW_PROPERTY_DATA "SDL.internal.gpu.d3d12.data"
  100. #define D3D_FEATURE_LEVEL_CHOICE D3D_FEATURE_LEVEL_11_0
  101. #define D3D_FEATURE_LEVEL_CHOICE_STR "11_0"
  102. #define MAX_ROOT_SIGNATURE_PARAMETERS 64
  103. #define D3D12_FENCE_UNSIGNALED_VALUE 0
  104. #define D3D12_FENCE_SIGNAL_VALUE 1
  105. // TODO: do these need to be tuned?
  106. #define VIEW_GPU_DESCRIPTOR_COUNT 65536
  107. #define SAMPLER_GPU_DESCRIPTOR_COUNT 2048
  108. #define STAGING_HEAP_DESCRIPTOR_COUNT 1024
  109. #define SDL_GPU_SHADERSTAGE_COMPUTE (SDL_GPUShaderStage)2
  110. #ifdef _WIN32
  111. #define HRESULT_FMT "(0x%08lX)"
  112. #else
  113. #define HRESULT_FMT "(0x%08X)"
  114. #endif
  115. // Function Pointer Signatures
  116. typedef HRESULT (WINAPI *pfnCreateDXGIFactory1)(const GUID *riid, void **ppFactory);
  117. typedef HRESULT (WINAPI *pfnDXGIGetDebugInterface)(const GUID *riid, void **ppDebug);
  118. #ifdef USE_PIX_RUNTIME
  119. #define PIX_BEGIN_EVENT_ON_COMMAND_LIST_FUNC "PIXBeginEventOnCommandList"
  120. #define PIX_END_EVENT_ON_COMMAND_LIST_FUNC "PIXEndEventOnCommandList"
  121. #define PIX_SET_MARKER_ON_COMMAND_LIST_FUNC "PIXSetMarkerOnCommandList"
  122. typedef void(WINAPI* pfnBeginEventOnCommandList)(ID3D12GraphicsCommandList* commandList, UINT64 color, _In_ PCSTR formatString);
  123. typedef void(WINAPI* pfnEndEventOnCommandList)(ID3D12GraphicsCommandList* commandList);
  124. typedef void(WINAPI* pfnSetMarkerOnCommandList)(ID3D12GraphicsCommandList* commandList, UINT64 color, _In_ PCSTR formatString);
  125. #endif
  126. // IIDs (from https://www.magnumdb.com/)
  127. static const IID D3D_IID_IDXGIFactory1 = { 0x770aae78, 0xf26f, 0x4dba, { 0xa8, 0x29, 0x25, 0x3c, 0x83, 0xd1, 0xb3, 0x87 } };
  128. static const IID D3D_IID_IDXGIFactory4 = { 0x1bc6ea02, 0xef36, 0x464f, { 0xbf, 0x0c, 0x21, 0xca, 0x39, 0xe5, 0x16, 0x8a } };
  129. static const IID D3D_IID_IDXGIFactory5 = { 0x7632e1f5, 0xee65, 0x4dca, { 0x87, 0xfd, 0x84, 0xcd, 0x75, 0xf8, 0x83, 0x8d } };
  130. static const IID D3D_IID_IDXGIFactory6 = { 0xc1b6694f, 0xff09, 0x44a9, { 0xb0, 0x3c, 0x77, 0x90, 0x0a, 0x0a, 0x1d, 0x17 } };
  131. static const IID D3D_IID_IDXGIAdapter1 = { 0x29038f61, 0x3839, 0x4626, { 0x91, 0xfd, 0x08, 0x68, 0x79, 0x01, 0x1a, 0x05 } };
  132. #if (defined(SDL_PLATFORM_XBOXONE) || defined(SDL_PLATFORM_XBOXSERIES))
  133. static const IID D3D_IID_IDXGIDevice1 = { 0x77db970f, 0x6276, 0x48ba, { 0xba, 0x28, 0x07, 0x01, 0x43, 0xb4, 0x39, 0x2c } };
  134. #else
  135. static const IID D3D_IID_IDXGIDevice = { 0x54ec77fa, 0x1377, 0x44e6, { 0x8c, 0x32, 0x88, 0xfd, 0x5f, 0x44, 0xc8, 0x4c } };
  136. #endif
  137. static const IID D3D_IID_IDXGISwapChain3 = { 0x94d99bdb, 0xf1f8, 0x4ab0, { 0xb2, 0x36, 0x7d, 0xa0, 0x17, 0x0e, 0xda, 0xb1 } };
  138. #ifdef HAVE_IDXGIINFOQUEUE
  139. static const IID D3D_IID_IDXGIDebug = { 0x119e7452, 0xde9e, 0x40fe, { 0x88, 0x06, 0x88, 0xf9, 0x0c, 0x12, 0xb4, 0x41 } };
  140. static const IID D3D_IID_IDXGIInfoQueue = { 0xd67441c7, 0x672a, 0x476f, { 0x9e, 0x82, 0xcd, 0x55, 0xb4, 0x49, 0x49, 0xce } };
  141. #endif
  142. static const GUID D3D_IID_DXGI_DEBUG_ALL = { 0xe48ae283, 0xda80, 0x490b, { 0x87, 0xe6, 0x43, 0xe9, 0xa9, 0xcf, 0xda, 0x08 } };
  143. static const IID D3D_IID_ID3D12Device = { 0x189819f1, 0x1db6, 0x4b57, { 0xbe, 0x54, 0x18, 0x21, 0x33, 0x9b, 0x85, 0xf7 } };
  144. static const IID D3D_IID_ID3D12CommandQueue = { 0x0ec870a6, 0x5d7e, 0x4c22, { 0x8c, 0xfc, 0x5b, 0xaa, 0xe0, 0x76, 0x16, 0xed } };
  145. static const IID D3D_IID_ID3D12DescriptorHeap = { 0x8efb471d, 0x616c, 0x4f49, { 0x90, 0xf7, 0x12, 0x7b, 0xb7, 0x63, 0xfa, 0x51 } };
  146. static const IID D3D_IID_ID3D12Resource = { 0x696442be, 0xa72e, 0x4059, { 0xbc, 0x79, 0x5b, 0x5c, 0x98, 0x04, 0x0f, 0xad } };
  147. static const IID D3D_IID_ID3D12CommandAllocator = { 0x6102dee4, 0xaf59, 0x4b09, { 0xb9, 0x99, 0xb4, 0x4d, 0x73, 0xf0, 0x9b, 0x24 } };
  148. static const IID D3D_IID_ID3D12CommandList = { 0x7116d91c, 0xe7e4, 0x47ce, { 0xb8, 0xc6, 0xec, 0x81, 0x68, 0xf4, 0x37, 0xe5 } };
  149. static const IID D3D_IID_ID3D12GraphicsCommandList = { 0x5b160d0f, 0xac1b, 0x4185, { 0x8b, 0xa8, 0xb3, 0xae, 0x42, 0xa5, 0xa4, 0x55 } };
  150. static const IID D3D_IID_ID3D12Fence = { 0x0a753dcf, 0xc4d8, 0x4b91, { 0xad, 0xf6, 0xbe, 0x5a, 0x60, 0xd9, 0x5a, 0x76 } };
  151. static const IID D3D_IID_ID3D12RootSignature = { 0xc54a6b66, 0x72df, 0x4ee8, { 0x8b, 0xe5, 0xa9, 0x46, 0xa1, 0x42, 0x92, 0x14 } };
  152. static const IID D3D_IID_ID3D12CommandSignature = { 0xc36a797c, 0xec80, 0x4f0a, { 0x89, 0x85, 0xa7, 0xb2, 0x47, 0x50, 0x82, 0xd1 } };
  153. static const IID D3D_IID_ID3D12PipelineState = { 0x765a30f3, 0xf624, 0x4c6f, { 0xa8, 0x28, 0xac, 0xe9, 0x48, 0x62, 0x24, 0x45 } };
  154. static const IID D3D_IID_ID3D12Debug = { 0x344488b7, 0x6846, 0x474b, { 0xb9, 0x89, 0xf0, 0x27, 0x44, 0x82, 0x45, 0xe0 } };
  155. static const IID D3D_IID_ID3D12InfoQueue = { 0x0742a90b, 0xc387, 0x483f, { 0xb9, 0x46, 0x30, 0xa7, 0xe4, 0xe6, 0x14, 0x58 } };
  156. static const IID D3D_IID_ID3D12InfoQueue1 = { 0x2852dd88, 0xb484, 0x4c0c, { 0xb6, 0xb1, 0x67, 0x16, 0x85, 0x00, 0xe6, 0x00 } };
  157. static const GUID D3D_CLSID_ID3D12SDKConfiguration = { 0x7cda6aca, 0xa03e, 0x49c8, { 0x94, 0x58, 0x03, 0x34, 0xd2, 0x0e, 0x07, 0xce } };
  158. static const GUID D3D_CLSID_ID3D12Debug = { 0xf2352aeb, 0xdd84, 0x49fe, { 0xb9, 0x7b, 0xa9, 0xdc, 0xfd, 0xcc, 0x1b, 0x4f } };
  159. static const IID D3D_IID_ID3D12SDKConfiguration = { 0xe9eb5314, 0x33aa, 0x42b2, { 0xa7, 0x18, 0xd7, 0x7f, 0x58, 0xb1, 0xf1, 0xc7 } };
  160. static const IID D3D_IID_ID3D12SDKConfiguration1 = { 0x8aaf9303, 0xad25, 0x48b9, { 0x9a, 0x57, 0xd9, 0xc3, 0x7e, 0x00, 0x9d, 0x9f } };
  161. static const IID D3D_IID_ID3D12DeviceFactory = { 0x61f307d3, 0xd34e, 0x4e7c, { 0x83, 0x74, 0x3b, 0xa4, 0xde, 0x23, 0xcc, 0xcb } };
  162. // Enums
  163. typedef enum D3D12BufferType
  164. {
  165. D3D12_BUFFER_TYPE_GPU,
  166. D3D12_BUFFER_TYPE_UNIFORM,
  167. D3D12_BUFFER_TYPE_UPLOAD,
  168. D3D12_BUFFER_TYPE_DOWNLOAD
  169. } D3D12BufferType;
  170. // Conversions
  171. static SDL_GPUTextureFormat SwapchainCompositionToSDLTextureFormat[] = {
  172. SDL_GPU_TEXTUREFORMAT_B8G8R8A8_UNORM, // SDR
  173. SDL_GPU_TEXTUREFORMAT_B8G8R8A8_UNORM_SRGB, // SDR_LINEAR
  174. SDL_GPU_TEXTUREFORMAT_R16G16B16A16_FLOAT, // HDR_EXTENDED_LINEAR
  175. SDL_GPU_TEXTUREFORMAT_R10G10B10A2_UNORM, // HDR10_ST2084
  176. };
  177. static DXGI_FORMAT SwapchainCompositionToTextureFormat[] = {
  178. DXGI_FORMAT_B8G8R8A8_UNORM, // SDR
  179. DXGI_FORMAT_B8G8R8A8_UNORM, // SDR_LINEAR (NOTE: The RTV uses the sRGB format)
  180. DXGI_FORMAT_R16G16B16A16_FLOAT, // HDR_EXTENDED_LINEAR
  181. DXGI_FORMAT_R10G10B10A2_UNORM, // HDR10_ST2084
  182. };
  183. static DXGI_COLOR_SPACE_TYPE SwapchainCompositionToColorSpace[] = {
  184. DXGI_COLOR_SPACE_RGB_FULL_G22_NONE_P709, // SDR
  185. DXGI_COLOR_SPACE_RGB_FULL_G22_NONE_P709, // SDR_LINEAR
  186. DXGI_COLOR_SPACE_RGB_FULL_G10_NONE_P709, // HDR_EXTENDED_LINEAR
  187. DXGI_COLOR_SPACE_RGB_FULL_G2084_NONE_P2020 // HDR10_ST2084
  188. };
  189. static D3D12_BLEND SDLToD3D12_BlendFactor[] = {
  190. D3D12_BLEND_ZERO, // INVALID
  191. D3D12_BLEND_ZERO, // ZERO
  192. D3D12_BLEND_ONE, // ONE
  193. D3D12_BLEND_SRC_COLOR, // SRC_COLOR
  194. D3D12_BLEND_INV_SRC_COLOR, // ONE_MINUS_SRC_COLOR
  195. D3D12_BLEND_DEST_COLOR, // DST_COLOR
  196. D3D12_BLEND_INV_DEST_COLOR, // ONE_MINUS_DST_COLOR
  197. D3D12_BLEND_SRC_ALPHA, // SRC_ALPHA
  198. D3D12_BLEND_INV_SRC_ALPHA, // ONE_MINUS_SRC_ALPHA
  199. D3D12_BLEND_DEST_ALPHA, // DST_ALPHA
  200. D3D12_BLEND_INV_DEST_ALPHA, // ONE_MINUS_DST_ALPHA
  201. D3D12_BLEND_BLEND_FACTOR, // CONSTANT_COLOR
  202. D3D12_BLEND_INV_BLEND_FACTOR, // ONE_MINUS_CONSTANT_COLOR
  203. D3D12_BLEND_SRC_ALPHA_SAT, // SRC_ALPHA_SATURATE
  204. };
  205. SDL_COMPILE_TIME_ASSERT(SDLToD3D12_BlendFactor, SDL_arraysize(SDLToD3D12_BlendFactor) == SDL_GPU_BLENDFACTOR_MAX_ENUM_VALUE);
  206. static D3D12_BLEND SDLToD3D12_BlendFactorAlpha[] = {
  207. D3D12_BLEND_ZERO, // INVALID
  208. D3D12_BLEND_ZERO, // ZERO
  209. D3D12_BLEND_ONE, // ONE
  210. D3D12_BLEND_SRC_ALPHA, // SRC_COLOR
  211. D3D12_BLEND_INV_SRC_ALPHA, // ONE_MINUS_SRC_COLOR
  212. D3D12_BLEND_DEST_ALPHA, // DST_COLOR
  213. D3D12_BLEND_INV_DEST_ALPHA, // ONE_MINUS_DST_COLOR
  214. D3D12_BLEND_SRC_ALPHA, // SRC_ALPHA
  215. D3D12_BLEND_INV_SRC_ALPHA, // ONE_MINUS_SRC_ALPHA
  216. D3D12_BLEND_DEST_ALPHA, // DST_ALPHA
  217. D3D12_BLEND_INV_DEST_ALPHA, // ONE_MINUS_DST_ALPHA
  218. D3D12_BLEND_BLEND_FACTOR, // CONSTANT_COLOR
  219. D3D12_BLEND_INV_BLEND_FACTOR, // ONE_MINUS_CONSTANT_COLOR
  220. D3D12_BLEND_SRC_ALPHA_SAT, // SRC_ALPHA_SATURATE
  221. };
  222. SDL_COMPILE_TIME_ASSERT(SDLToD3D12_BlendFactorAlpha, SDL_arraysize(SDLToD3D12_BlendFactorAlpha) == SDL_GPU_BLENDFACTOR_MAX_ENUM_VALUE);
  223. static D3D12_BLEND_OP SDLToD3D12_BlendOp[] = {
  224. D3D12_BLEND_OP_ADD, // INVALID
  225. D3D12_BLEND_OP_ADD, // ADD
  226. D3D12_BLEND_OP_SUBTRACT, // SUBTRACT
  227. D3D12_BLEND_OP_REV_SUBTRACT, // REVERSE_SUBTRACT
  228. D3D12_BLEND_OP_MIN, // MIN
  229. D3D12_BLEND_OP_MAX // MAX
  230. };
  231. SDL_COMPILE_TIME_ASSERT(SDLToD3D12_BlendOp, SDL_arraysize(SDLToD3D12_BlendOp) == SDL_GPU_BLENDOP_MAX_ENUM_VALUE);
  232. // These are actually color formats.
  233. // For some genius reason, D3D12 splits format capabilities for depth-stencil views.
  234. static DXGI_FORMAT SDLToD3D12_TextureFormat[] = {
  235. DXGI_FORMAT_UNKNOWN, // INVALID
  236. DXGI_FORMAT_A8_UNORM, // A8_UNORM
  237. DXGI_FORMAT_R8_UNORM, // R8_UNORM
  238. DXGI_FORMAT_R8G8_UNORM, // R8G8_UNORM
  239. DXGI_FORMAT_R8G8B8A8_UNORM, // R8G8B8A8_UNORM
  240. DXGI_FORMAT_R16_UNORM, // R16_UNORM
  241. DXGI_FORMAT_R16G16_UNORM, // R16G16_UNORM
  242. DXGI_FORMAT_R16G16B16A16_UNORM, // R16G16B16A16_UNORM
  243. DXGI_FORMAT_R10G10B10A2_UNORM, // R10G10B10A2_UNORM
  244. DXGI_FORMAT_B5G6R5_UNORM, // B5G6R5_UNORM
  245. DXGI_FORMAT_B5G5R5A1_UNORM, // B5G5R5A1_UNORM
  246. DXGI_FORMAT_B4G4R4A4_UNORM, // B4G4R4A4_UNORM
  247. DXGI_FORMAT_B8G8R8A8_UNORM, // B8G8R8A8_UNORM
  248. DXGI_FORMAT_BC1_UNORM, // BC1_UNORM
  249. DXGI_FORMAT_BC2_UNORM, // BC2_UNORM
  250. DXGI_FORMAT_BC3_UNORM, // BC3_UNORM
  251. DXGI_FORMAT_BC4_UNORM, // BC4_UNORM
  252. DXGI_FORMAT_BC5_UNORM, // BC5_UNORM
  253. DXGI_FORMAT_BC7_UNORM, // BC7_UNORM
  254. DXGI_FORMAT_BC6H_SF16, // BC6H_FLOAT
  255. DXGI_FORMAT_BC6H_UF16, // BC6H_UFLOAT
  256. DXGI_FORMAT_R8_SNORM, // R8_SNORM
  257. DXGI_FORMAT_R8G8_SNORM, // R8G8_SNORM
  258. DXGI_FORMAT_R8G8B8A8_SNORM, // R8G8B8A8_SNORM
  259. DXGI_FORMAT_R16_SNORM, // R16_SNORM
  260. DXGI_FORMAT_R16G16_SNORM, // R16G16_SNORM
  261. DXGI_FORMAT_R16G16B16A16_SNORM, // R16G16B16A16_SNORM
  262. DXGI_FORMAT_R16_FLOAT, // R16_FLOAT
  263. DXGI_FORMAT_R16G16_FLOAT, // R16G16_FLOAT
  264. DXGI_FORMAT_R16G16B16A16_FLOAT, // R16G16B16A16_FLOAT
  265. DXGI_FORMAT_R32_FLOAT, // R32_FLOAT
  266. DXGI_FORMAT_R32G32_FLOAT, // R32G32_FLOAT
  267. DXGI_FORMAT_R32G32B32A32_FLOAT, // R32G32B32A32_FLOAT
  268. DXGI_FORMAT_R11G11B10_FLOAT, // R11G11B10_UFLOAT
  269. DXGI_FORMAT_R8_UINT, // R8_UINT
  270. DXGI_FORMAT_R8G8_UINT, // R8G8_UINT
  271. DXGI_FORMAT_R8G8B8A8_UINT, // R8G8B8A8_UINT
  272. DXGI_FORMAT_R16_UINT, // R16_UINT
  273. DXGI_FORMAT_R16G16_UINT, // R16G16_UINT
  274. DXGI_FORMAT_R16G16B16A16_UINT, // R16G16B16A16_UINT
  275. DXGI_FORMAT_R32_UINT, // R32_UINT
  276. DXGI_FORMAT_R32G32_UINT, // R32G32_UINT
  277. DXGI_FORMAT_R32G32B32A32_UINT, // R32G32B32A32_UINT
  278. DXGI_FORMAT_R8_SINT, // R8_INT
  279. DXGI_FORMAT_R8G8_SINT, // R8G8_INT
  280. DXGI_FORMAT_R8G8B8A8_SINT, // R8G8B8A8_INT
  281. DXGI_FORMAT_R16_SINT, // R16_INT
  282. DXGI_FORMAT_R16G16_SINT, // R16G16_INT
  283. DXGI_FORMAT_R16G16B16A16_SINT, // R16G16B16A16_INT
  284. DXGI_FORMAT_R32_SINT, // R32_INT
  285. DXGI_FORMAT_R32G32_SINT, // R32G32_INT
  286. DXGI_FORMAT_R32G32B32A32_SINT, // R32G32B32A32_INT
  287. DXGI_FORMAT_R8G8B8A8_UNORM_SRGB, // R8G8B8A8_UNORM_SRGB
  288. DXGI_FORMAT_B8G8R8A8_UNORM_SRGB, // B8G8R8A8_UNORM_SRGB
  289. DXGI_FORMAT_BC1_UNORM_SRGB, // BC1_UNORM_SRGB
  290. DXGI_FORMAT_BC2_UNORM_SRGB, // BC2_UNORM_SRGB
  291. DXGI_FORMAT_BC3_UNORM_SRGB, // BC3_UNORM_SRGB
  292. DXGI_FORMAT_BC7_UNORM_SRGB, // BC7_UNORM_SRGB
  293. DXGI_FORMAT_R16_UNORM, // D16_UNORM
  294. DXGI_FORMAT_R24_UNORM_X8_TYPELESS, // D24_UNORM
  295. DXGI_FORMAT_R32_FLOAT, // D32_FLOAT
  296. DXGI_FORMAT_R24_UNORM_X8_TYPELESS, // D24_UNORM_S8_UINT
  297. DXGI_FORMAT_R32_FLOAT_X8X24_TYPELESS, // D32_FLOAT_S8_UINT
  298. DXGI_FORMAT_UNKNOWN, // ASTC_4x4_UNORM
  299. DXGI_FORMAT_UNKNOWN, // ASTC_5x4_UNORM
  300. DXGI_FORMAT_UNKNOWN, // ASTC_5x5_UNORM
  301. DXGI_FORMAT_UNKNOWN, // ASTC_6x5_UNORM
  302. DXGI_FORMAT_UNKNOWN, // ASTC_6x6_UNORM
  303. DXGI_FORMAT_UNKNOWN, // ASTC_8x5_UNORM
  304. DXGI_FORMAT_UNKNOWN, // ASTC_8x6_UNORM
  305. DXGI_FORMAT_UNKNOWN, // ASTC_8x8_UNORM
  306. DXGI_FORMAT_UNKNOWN, // ASTC_10x5_UNORM
  307. DXGI_FORMAT_UNKNOWN, // ASTC_10x6_UNORM
  308. DXGI_FORMAT_UNKNOWN, // ASTC_10x8_UNORM
  309. DXGI_FORMAT_UNKNOWN, // ASTC_10x10_UNORM
  310. DXGI_FORMAT_UNKNOWN, // ASTC_12x10_UNORM
  311. DXGI_FORMAT_UNKNOWN, // ASTC_12x12_UNORM
  312. DXGI_FORMAT_UNKNOWN, // ASTC_4x4_UNORM_SRGB
  313. DXGI_FORMAT_UNKNOWN, // ASTC_5x4_UNORM_SRGB
  314. DXGI_FORMAT_UNKNOWN, // ASTC_5x5_UNORM_SRGB
  315. DXGI_FORMAT_UNKNOWN, // ASTC_6x5_UNORM_SRGB
  316. DXGI_FORMAT_UNKNOWN, // ASTC_6x6_UNORM_SRGB
  317. DXGI_FORMAT_UNKNOWN, // ASTC_8x5_UNORM_SRGB
  318. DXGI_FORMAT_UNKNOWN, // ASTC_8x6_UNORM_SRGB
  319. DXGI_FORMAT_UNKNOWN, // ASTC_8x8_UNORM_SRGB
  320. DXGI_FORMAT_UNKNOWN, // ASTC_10x5_UNORM_SRGB
  321. DXGI_FORMAT_UNKNOWN, // ASTC_10x6_UNORM_SRGB
  322. DXGI_FORMAT_UNKNOWN, // ASTC_10x8_UNORM_SRGB
  323. DXGI_FORMAT_UNKNOWN, // ASTC_10x10_UNORM_SRGB
  324. DXGI_FORMAT_UNKNOWN, // ASTC_12x10_UNORM_SRGB
  325. DXGI_FORMAT_UNKNOWN, // ASTC_12x12_UNORM_SRGB
  326. DXGI_FORMAT_UNKNOWN, // ASTC_4x4_FLOAT
  327. DXGI_FORMAT_UNKNOWN, // ASTC_5x4_FLOAT
  328. DXGI_FORMAT_UNKNOWN, // ASTC_5x5_FLOAT
  329. DXGI_FORMAT_UNKNOWN, // ASTC_6x5_FLOAT
  330. DXGI_FORMAT_UNKNOWN, // ASTC_6x6_FLOAT
  331. DXGI_FORMAT_UNKNOWN, // ASTC_8x5_FLOAT
  332. DXGI_FORMAT_UNKNOWN, // ASTC_8x6_FLOAT
  333. DXGI_FORMAT_UNKNOWN, // ASTC_8x8_FLOAT
  334. DXGI_FORMAT_UNKNOWN, // ASTC_10x5_FLOAT
  335. DXGI_FORMAT_UNKNOWN, // ASTC_10x6_FLOAT
  336. DXGI_FORMAT_UNKNOWN, // ASTC_10x8_FLOAT
  337. DXGI_FORMAT_UNKNOWN, // ASTC_10x10_FLOAT
  338. DXGI_FORMAT_UNKNOWN, // ASTC_12x10_FLOAT
  339. DXGI_FORMAT_UNKNOWN, // ASTC_12x12_FLOAT
  340. };
  341. SDL_COMPILE_TIME_ASSERT(SDLToD3D12_TextureFormat, SDL_arraysize(SDLToD3D12_TextureFormat) == SDL_GPU_TEXTUREFORMAT_MAX_ENUM_VALUE);
  342. static DXGI_FORMAT SDLToD3D12_DepthFormat[] = {
  343. DXGI_FORMAT_UNKNOWN, // INVALID
  344. DXGI_FORMAT_UNKNOWN, // A8_UNORM
  345. DXGI_FORMAT_UNKNOWN, // R8_UNORM
  346. DXGI_FORMAT_UNKNOWN, // R8G8_UNORM
  347. DXGI_FORMAT_UNKNOWN, // R8G8B8A8_UNORM
  348. DXGI_FORMAT_UNKNOWN, // R16_UNORM
  349. DXGI_FORMAT_UNKNOWN, // R16G16_UNORM
  350. DXGI_FORMAT_UNKNOWN, // R16G16B16A16_UNORM
  351. DXGI_FORMAT_UNKNOWN, // R10G10B10A2_UNORM
  352. DXGI_FORMAT_UNKNOWN, // B5G6R5_UNORM
  353. DXGI_FORMAT_UNKNOWN, // B5G5R5A1_UNORM
  354. DXGI_FORMAT_UNKNOWN, // B4G4R4A4_UNORM
  355. DXGI_FORMAT_UNKNOWN, // B8G8R8A8_UNORM
  356. DXGI_FORMAT_UNKNOWN, // BC1_UNORM
  357. DXGI_FORMAT_UNKNOWN, // BC2_UNORM
  358. DXGI_FORMAT_UNKNOWN, // BC3_UNORM
  359. DXGI_FORMAT_UNKNOWN, // BC4_UNORM
  360. DXGI_FORMAT_UNKNOWN, // BC5_UNORM
  361. DXGI_FORMAT_UNKNOWN, // BC7_UNORM
  362. DXGI_FORMAT_UNKNOWN, // BC6H_FLOAT
  363. DXGI_FORMAT_UNKNOWN, // BC6H_UFLOAT
  364. DXGI_FORMAT_UNKNOWN, // R8_SNORM
  365. DXGI_FORMAT_UNKNOWN, // R8G8_SNORM
  366. DXGI_FORMAT_UNKNOWN, // R8G8B8A8_SNORM
  367. DXGI_FORMAT_UNKNOWN, // R16_SNORM
  368. DXGI_FORMAT_UNKNOWN, // R16G16_SNORM
  369. DXGI_FORMAT_UNKNOWN, // R16G16B16A16_SNORM
  370. DXGI_FORMAT_UNKNOWN, // R16_FLOAT
  371. DXGI_FORMAT_UNKNOWN, // R16G16_FLOAT
  372. DXGI_FORMAT_UNKNOWN, // R16G16B16A16_FLOAT
  373. DXGI_FORMAT_UNKNOWN, // R32_FLOAT
  374. DXGI_FORMAT_UNKNOWN, // R32G32_FLOAT
  375. DXGI_FORMAT_UNKNOWN, // R32G32B32A32_FLOAT
  376. DXGI_FORMAT_UNKNOWN, // R11G11B10_UFLOAT
  377. DXGI_FORMAT_UNKNOWN, // R8_UINT
  378. DXGI_FORMAT_UNKNOWN, // R8G8_UINT
  379. DXGI_FORMAT_UNKNOWN, // R8G8B8A8_UINT
  380. DXGI_FORMAT_UNKNOWN, // R16_UINT
  381. DXGI_FORMAT_UNKNOWN, // R16G16_UINT
  382. DXGI_FORMAT_UNKNOWN, // R16G16B16A16_UINT
  383. DXGI_FORMAT_UNKNOWN, // R32_UINT
  384. DXGI_FORMAT_UNKNOWN, // R32G32_UINT
  385. DXGI_FORMAT_UNKNOWN, // R32G32B32A32_UINT
  386. DXGI_FORMAT_UNKNOWN, // R8_INT
  387. DXGI_FORMAT_UNKNOWN, // R8G8_INT
  388. DXGI_FORMAT_UNKNOWN, // R8G8B8A8_INT
  389. DXGI_FORMAT_UNKNOWN, // R16_INT
  390. DXGI_FORMAT_UNKNOWN, // R16G16_INT
  391. DXGI_FORMAT_UNKNOWN, // R16G16B16A16_INT
  392. DXGI_FORMAT_UNKNOWN, // R32_INT
  393. DXGI_FORMAT_UNKNOWN, // R32G32_INT
  394. DXGI_FORMAT_UNKNOWN, // R32G32B32A32_INT
  395. DXGI_FORMAT_UNKNOWN, // R8G8B8A8_UNORM_SRGB
  396. DXGI_FORMAT_UNKNOWN, // B8G8R8A8_UNORM_SRGB
  397. DXGI_FORMAT_UNKNOWN, // BC1_UNORM_SRGB
  398. DXGI_FORMAT_UNKNOWN, // BC2_UNORM_SRGB
  399. DXGI_FORMAT_UNKNOWN, // BC3_UNORM_SRGB
  400. DXGI_FORMAT_UNKNOWN, // BC7_UNORM_SRGB
  401. DXGI_FORMAT_D16_UNORM, // D16_UNORM
  402. DXGI_FORMAT_D24_UNORM_S8_UINT, // D24_UNORM
  403. DXGI_FORMAT_D32_FLOAT, // D32_FLOAT
  404. DXGI_FORMAT_D24_UNORM_S8_UINT, // D24_UNORM_S8_UINT
  405. DXGI_FORMAT_D32_FLOAT_S8X24_UINT, // D32_FLOAT_S8_UINT
  406. DXGI_FORMAT_UNKNOWN, // ASTC_4x4_UNORM
  407. DXGI_FORMAT_UNKNOWN, // ASTC_5x4_UNORM
  408. DXGI_FORMAT_UNKNOWN, // ASTC_5x5_UNORM
  409. DXGI_FORMAT_UNKNOWN, // ASTC_6x5_UNORM
  410. DXGI_FORMAT_UNKNOWN, // ASTC_6x6_UNORM
  411. DXGI_FORMAT_UNKNOWN, // ASTC_8x5_UNORM
  412. DXGI_FORMAT_UNKNOWN, // ASTC_8x6_UNORM
  413. DXGI_FORMAT_UNKNOWN, // ASTC_8x8_UNORM
  414. DXGI_FORMAT_UNKNOWN, // ASTC_10x5_UNORM
  415. DXGI_FORMAT_UNKNOWN, // ASTC_10x6_UNORM
  416. DXGI_FORMAT_UNKNOWN, // ASTC_10x8_UNORM
  417. DXGI_FORMAT_UNKNOWN, // ASTC_10x10_UNORM
  418. DXGI_FORMAT_UNKNOWN, // ASTC_12x10_UNORM
  419. DXGI_FORMAT_UNKNOWN, // ASTC_12x12_UNORM
  420. DXGI_FORMAT_UNKNOWN, // ASTC_4x4_UNORM_SRGB
  421. DXGI_FORMAT_UNKNOWN, // ASTC_5x4_UNORM_SRGB
  422. DXGI_FORMAT_UNKNOWN, // ASTC_5x5_UNORM_SRGB
  423. DXGI_FORMAT_UNKNOWN, // ASTC_6x5_UNORM_SRGB
  424. DXGI_FORMAT_UNKNOWN, // ASTC_6x6_UNORM_SRGB
  425. DXGI_FORMAT_UNKNOWN, // ASTC_8x5_UNORM_SRGB
  426. DXGI_FORMAT_UNKNOWN, // ASTC_8x6_UNORM_SRGB
  427. DXGI_FORMAT_UNKNOWN, // ASTC_8x8_UNORM_SRGB
  428. DXGI_FORMAT_UNKNOWN, // ASTC_10x5_UNORM_SRGB
  429. DXGI_FORMAT_UNKNOWN, // ASTC_10x6_UNORM_SRGB
  430. DXGI_FORMAT_UNKNOWN, // ASTC_10x8_UNORM_SRGB
  431. DXGI_FORMAT_UNKNOWN, // ASTC_10x10_UNORM_SRGB
  432. DXGI_FORMAT_UNKNOWN, // ASTC_12x10_UNORM_SRGB
  433. DXGI_FORMAT_UNKNOWN, // ASTC_12x12_UNORM_SRGB
  434. DXGI_FORMAT_UNKNOWN, // ASTC_4x4_FLOAT
  435. DXGI_FORMAT_UNKNOWN, // ASTC_5x4_FLOAT
  436. DXGI_FORMAT_UNKNOWN, // ASTC_5x5_FLOAT
  437. DXGI_FORMAT_UNKNOWN, // ASTC_6x5_FLOAT
  438. DXGI_FORMAT_UNKNOWN, // ASTC_6x6_FLOAT
  439. DXGI_FORMAT_UNKNOWN, // ASTC_8x5_FLOAT
  440. DXGI_FORMAT_UNKNOWN, // ASTC_8x6_FLOAT
  441. DXGI_FORMAT_UNKNOWN, // ASTC_8x8_FLOAT
  442. DXGI_FORMAT_UNKNOWN, // ASTC_10x5_FLOAT
  443. DXGI_FORMAT_UNKNOWN, // ASTC_10x6_FLOAT
  444. DXGI_FORMAT_UNKNOWN, // ASTC_10x8_FLOAT
  445. DXGI_FORMAT_UNKNOWN, // ASTC_10x10_FLOAT
  446. DXGI_FORMAT_UNKNOWN, // ASTC_12x10_FLOAT
  447. DXGI_FORMAT_UNKNOWN, // ASTC_12x12_FLOAT
  448. };
  449. SDL_COMPILE_TIME_ASSERT(SDLToD3D12_DepthFormat, SDL_arraysize(SDLToD3D12_DepthFormat) == SDL_GPU_TEXTUREFORMAT_MAX_ENUM_VALUE);
  450. static DXGI_FORMAT SDLToD3D12_TypelessFormat[] = {
  451. DXGI_FORMAT_UNKNOWN, // INVALID
  452. DXGI_FORMAT_UNKNOWN, // A8_UNORM
  453. DXGI_FORMAT_UNKNOWN, // R8_UNORM
  454. DXGI_FORMAT_UNKNOWN, // R8G8_UNORM
  455. DXGI_FORMAT_UNKNOWN, // R8G8B8A8_UNORM
  456. DXGI_FORMAT_UNKNOWN, // R16_UNORM
  457. DXGI_FORMAT_UNKNOWN, // R16G16_UNORM
  458. DXGI_FORMAT_UNKNOWN, // R16G16B16A16_UNORM
  459. DXGI_FORMAT_UNKNOWN, // R10G10B10A2_UNORM
  460. DXGI_FORMAT_UNKNOWN, // B5G6R5_UNORM
  461. DXGI_FORMAT_UNKNOWN, // B5G5R5A1_UNORM
  462. DXGI_FORMAT_UNKNOWN, // B4G4R4A4_UNORM
  463. DXGI_FORMAT_UNKNOWN, // B8G8R8A8_UNORM
  464. DXGI_FORMAT_UNKNOWN, // BC1_UNORM
  465. DXGI_FORMAT_UNKNOWN, // BC2_UNORM
  466. DXGI_FORMAT_UNKNOWN, // BC3_UNORM
  467. DXGI_FORMAT_UNKNOWN, // BC4_UNORM
  468. DXGI_FORMAT_UNKNOWN, // BC5_UNORM
  469. DXGI_FORMAT_UNKNOWN, // BC7_UNORM
  470. DXGI_FORMAT_UNKNOWN, // BC6H_FLOAT
  471. DXGI_FORMAT_UNKNOWN, // BC6H_UFLOAT
  472. DXGI_FORMAT_UNKNOWN, // R8_SNORM
  473. DXGI_FORMAT_UNKNOWN, // R8G8_SNORM
  474. DXGI_FORMAT_UNKNOWN, // R8G8B8A8_SNORM
  475. DXGI_FORMAT_UNKNOWN, // R16_SNORM
  476. DXGI_FORMAT_UNKNOWN, // R16G16_SNORM
  477. DXGI_FORMAT_UNKNOWN, // R16G16B16A16_SNORM
  478. DXGI_FORMAT_UNKNOWN, // R16_FLOAT
  479. DXGI_FORMAT_UNKNOWN, // R16G16_FLOAT
  480. DXGI_FORMAT_UNKNOWN, // R16G16B16A16_FLOAT
  481. DXGI_FORMAT_UNKNOWN, // R32_FLOAT
  482. DXGI_FORMAT_UNKNOWN, // R32G32_FLOAT
  483. DXGI_FORMAT_UNKNOWN, // R32G32B32A32_FLOAT
  484. DXGI_FORMAT_UNKNOWN, // R11G11B10_UFLOAT
  485. DXGI_FORMAT_UNKNOWN, // R8_UINT
  486. DXGI_FORMAT_UNKNOWN, // R8G8_UINT
  487. DXGI_FORMAT_UNKNOWN, // R8G8B8A8_UINT
  488. DXGI_FORMAT_UNKNOWN, // R16_UINT
  489. DXGI_FORMAT_UNKNOWN, // R16G16_UINT
  490. DXGI_FORMAT_UNKNOWN, // R16G16B16A16_UINT
  491. DXGI_FORMAT_UNKNOWN, // R32_UINT
  492. DXGI_FORMAT_UNKNOWN, // R32G32_UINT
  493. DXGI_FORMAT_UNKNOWN, // R32G32B32A32_UINT
  494. DXGI_FORMAT_UNKNOWN, // R8_INT
  495. DXGI_FORMAT_UNKNOWN, // R8G8_INT
  496. DXGI_FORMAT_UNKNOWN, // R8G8B8A8_INT
  497. DXGI_FORMAT_UNKNOWN, // R16_INT
  498. DXGI_FORMAT_UNKNOWN, // R16G16_INT
  499. DXGI_FORMAT_UNKNOWN, // R16G16B16A16_INT
  500. DXGI_FORMAT_UNKNOWN, // R32_INT
  501. DXGI_FORMAT_UNKNOWN, // R32G32_INT
  502. DXGI_FORMAT_UNKNOWN, // R32G32B32A32_INT
  503. DXGI_FORMAT_UNKNOWN, // R8G8B8A8_UNORM_SRGB
  504. DXGI_FORMAT_UNKNOWN, // B8G8R8A8_UNORM_SRGB
  505. DXGI_FORMAT_UNKNOWN, // BC1_UNORM_SRGB
  506. DXGI_FORMAT_UNKNOWN, // BC2_UNORM_SRGB
  507. DXGI_FORMAT_UNKNOWN, // BC3_UNORM_SRGB
  508. DXGI_FORMAT_UNKNOWN, // BC7_UNORM_SRGB
  509. DXGI_FORMAT_R16_TYPELESS, // D16_UNORM
  510. DXGI_FORMAT_R24G8_TYPELESS, // D24_UNORM
  511. DXGI_FORMAT_R32_TYPELESS, // D32_FLOAT
  512. DXGI_FORMAT_R24G8_TYPELESS, // D24_UNORM_S8_UINT
  513. DXGI_FORMAT_R32G8X24_TYPELESS, // D32_FLOAT_S8_UINT
  514. DXGI_FORMAT_UNKNOWN, // ASTC_4x4_UNORM
  515. DXGI_FORMAT_UNKNOWN, // ASTC_5x4_UNORM
  516. DXGI_FORMAT_UNKNOWN, // ASTC_5x5_UNORM
  517. DXGI_FORMAT_UNKNOWN, // ASTC_6x5_UNORM
  518. DXGI_FORMAT_UNKNOWN, // ASTC_6x6_UNORM
  519. DXGI_FORMAT_UNKNOWN, // ASTC_8x5_UNORM
  520. DXGI_FORMAT_UNKNOWN, // ASTC_8x6_UNORM
  521. DXGI_FORMAT_UNKNOWN, // ASTC_8x8_UNORM
  522. DXGI_FORMAT_UNKNOWN, // ASTC_10x5_UNORM
  523. DXGI_FORMAT_UNKNOWN, // ASTC_10x6_UNORM
  524. DXGI_FORMAT_UNKNOWN, // ASTC_10x8_UNORM
  525. DXGI_FORMAT_UNKNOWN, // ASTC_10x10_UNORM
  526. DXGI_FORMAT_UNKNOWN, // ASTC_12x10_UNORM
  527. DXGI_FORMAT_UNKNOWN, // ASTC_12x12_UNORM
  528. DXGI_FORMAT_UNKNOWN, // ASTC_4x4_UNORM_SRGB
  529. DXGI_FORMAT_UNKNOWN, // ASTC_5x4_UNORM_SRGB
  530. DXGI_FORMAT_UNKNOWN, // ASTC_5x5_UNORM_SRGB
  531. DXGI_FORMAT_UNKNOWN, // ASTC_6x5_UNORM_SRGB
  532. DXGI_FORMAT_UNKNOWN, // ASTC_6x6_UNORM_SRGB
  533. DXGI_FORMAT_UNKNOWN, // ASTC_8x5_UNORM_SRGB
  534. DXGI_FORMAT_UNKNOWN, // ASTC_8x6_UNORM_SRGB
  535. DXGI_FORMAT_UNKNOWN, // ASTC_8x8_UNORM_SRGB
  536. DXGI_FORMAT_UNKNOWN, // ASTC_10x5_UNORM_SRGB
  537. DXGI_FORMAT_UNKNOWN, // ASTC_10x6_UNORM_SRGB
  538. DXGI_FORMAT_UNKNOWN, // ASTC_10x8_UNORM_SRGB
  539. DXGI_FORMAT_UNKNOWN, // ASTC_10x10_UNORM_SRGB
  540. DXGI_FORMAT_UNKNOWN, // ASTC_12x10_UNORM_SRGB
  541. DXGI_FORMAT_UNKNOWN, // ASTC_12x12_UNORM_SRGB
  542. DXGI_FORMAT_UNKNOWN, // ASTC_4x4_FLOAT
  543. DXGI_FORMAT_UNKNOWN, // ASTC_5x4_FLOAT
  544. DXGI_FORMAT_UNKNOWN, // ASTC_5x5_FLOAT
  545. DXGI_FORMAT_UNKNOWN, // ASTC_6x5_FLOAT
  546. DXGI_FORMAT_UNKNOWN, // ASTC_6x6_FLOAT
  547. DXGI_FORMAT_UNKNOWN, // ASTC_8x5_FLOAT
  548. DXGI_FORMAT_UNKNOWN, // ASTC_8x6_FLOAT
  549. DXGI_FORMAT_UNKNOWN, // ASTC_8x8_FLOAT
  550. DXGI_FORMAT_UNKNOWN, // ASTC_10x5_FLOAT
  551. DXGI_FORMAT_UNKNOWN, // ASTC_10x6_FLOAT
  552. DXGI_FORMAT_UNKNOWN, // ASTC_10x8_FLOAT
  553. DXGI_FORMAT_UNKNOWN, // ASTC_10x10_FLOAT
  554. DXGI_FORMAT_UNKNOWN, // ASTC_12x10_FLOAT
  555. DXGI_FORMAT_UNKNOWN, // ASTC_12x12_FLOAT
  556. };
  557. SDL_COMPILE_TIME_ASSERT(SDLToD3D12_TypelessFormat, SDL_arraysize(SDLToD3D12_TypelessFormat) == SDL_GPU_TEXTUREFORMAT_MAX_ENUM_VALUE);
  558. static D3D12_COMPARISON_FUNC SDLToD3D12_CompareOp[] = {
  559. D3D12_COMPARISON_FUNC_NEVER, // INVALID
  560. D3D12_COMPARISON_FUNC_NEVER, // NEVER
  561. D3D12_COMPARISON_FUNC_LESS, // LESS
  562. D3D12_COMPARISON_FUNC_EQUAL, // EQUAL
  563. D3D12_COMPARISON_FUNC_LESS_EQUAL, // LESS_OR_EQUAL
  564. D3D12_COMPARISON_FUNC_GREATER, // GREATER
  565. D3D12_COMPARISON_FUNC_NOT_EQUAL, // NOT_EQUAL
  566. D3D12_COMPARISON_FUNC_GREATER_EQUAL, // GREATER_OR_EQUAL
  567. D3D12_COMPARISON_FUNC_ALWAYS // ALWAYS
  568. };
  569. SDL_COMPILE_TIME_ASSERT(SDLToD3D12_CompareOp, SDL_arraysize(SDLToD3D12_CompareOp) == SDL_GPU_COMPAREOP_MAX_ENUM_VALUE);
  570. static D3D12_STENCIL_OP SDLToD3D12_StencilOp[] = {
  571. D3D12_STENCIL_OP_KEEP, // INVALID
  572. D3D12_STENCIL_OP_KEEP, // KEEP
  573. D3D12_STENCIL_OP_ZERO, // ZERO
  574. D3D12_STENCIL_OP_REPLACE, // REPLACE
  575. D3D12_STENCIL_OP_INCR_SAT, // INCREMENT_AND_CLAMP
  576. D3D12_STENCIL_OP_DECR_SAT, // DECREMENT_AND_CLAMP
  577. D3D12_STENCIL_OP_INVERT, // INVERT
  578. D3D12_STENCIL_OP_INCR, // INCREMENT_AND_WRAP
  579. D3D12_STENCIL_OP_DECR // DECREMENT_AND_WRAP
  580. };
  581. SDL_COMPILE_TIME_ASSERT(SDLToD3D12_StencilOp, SDL_arraysize(SDLToD3D12_StencilOp) == SDL_GPU_STENCILOP_MAX_ENUM_VALUE);
  582. static D3D12_CULL_MODE SDLToD3D12_CullMode[] = {
  583. D3D12_CULL_MODE_NONE, // NONE
  584. D3D12_CULL_MODE_FRONT, // FRONT
  585. D3D12_CULL_MODE_BACK // BACK
  586. };
  587. static D3D12_FILL_MODE SDLToD3D12_FillMode[] = {
  588. D3D12_FILL_MODE_SOLID, // FILL
  589. D3D12_FILL_MODE_WIREFRAME // LINE
  590. };
  591. static D3D12_INPUT_CLASSIFICATION SDLToD3D12_InputRate[] = {
  592. D3D12_INPUT_CLASSIFICATION_PER_VERTEX_DATA, // VERTEX
  593. D3D12_INPUT_CLASSIFICATION_PER_INSTANCE_DATA // INSTANCE
  594. };
  595. static DXGI_FORMAT SDLToD3D12_VertexFormat[] = {
  596. DXGI_FORMAT_UNKNOWN, // UNKNOWN
  597. DXGI_FORMAT_R32_SINT, // INT
  598. DXGI_FORMAT_R32G32_SINT, // INT2
  599. DXGI_FORMAT_R32G32B32_SINT, // INT3
  600. DXGI_FORMAT_R32G32B32A32_SINT, // INT4
  601. DXGI_FORMAT_R32_UINT, // UINT
  602. DXGI_FORMAT_R32G32_UINT, // UINT2
  603. DXGI_FORMAT_R32G32B32_UINT, // UINT3
  604. DXGI_FORMAT_R32G32B32A32_UINT, // UINT4
  605. DXGI_FORMAT_R32_FLOAT, // FLOAT
  606. DXGI_FORMAT_R32G32_FLOAT, // FLOAT2
  607. DXGI_FORMAT_R32G32B32_FLOAT, // FLOAT3
  608. DXGI_FORMAT_R32G32B32A32_FLOAT, // FLOAT4
  609. DXGI_FORMAT_R8G8_SINT, // BYTE2
  610. DXGI_FORMAT_R8G8B8A8_SINT, // BYTE4
  611. DXGI_FORMAT_R8G8_UINT, // UBYTE2
  612. DXGI_FORMAT_R8G8B8A8_UINT, // UBYTE4
  613. DXGI_FORMAT_R8G8_SNORM, // BYTE2_NORM
  614. DXGI_FORMAT_R8G8B8A8_SNORM, // BYTE4_NORM
  615. DXGI_FORMAT_R8G8_UNORM, // UBYTE2_NORM
  616. DXGI_FORMAT_R8G8B8A8_UNORM, // UBYTE4_NORM
  617. DXGI_FORMAT_R16G16_SINT, // SHORT2
  618. DXGI_FORMAT_R16G16B16A16_SINT, // SHORT4
  619. DXGI_FORMAT_R16G16_UINT, // USHORT2
  620. DXGI_FORMAT_R16G16B16A16_UINT, // USHORT4
  621. DXGI_FORMAT_R16G16_SNORM, // SHORT2_NORM
  622. DXGI_FORMAT_R16G16B16A16_SNORM, // SHORT4_NORM
  623. DXGI_FORMAT_R16G16_UNORM, // USHORT2_NORM
  624. DXGI_FORMAT_R16G16B16A16_UNORM, // USHORT4_NORM
  625. DXGI_FORMAT_R16G16_FLOAT, // HALF2
  626. DXGI_FORMAT_R16G16B16A16_FLOAT // HALF4
  627. };
  628. SDL_COMPILE_TIME_ASSERT(SDLToD3D12_VertexFormat, SDL_arraysize(SDLToD3D12_VertexFormat) == SDL_GPU_VERTEXELEMENTFORMAT_MAX_ENUM_VALUE);
  629. static Uint32 SDLToD3D12_SampleCount[] = {
  630. 1, // SDL_GPU_SAMPLECOUNT_1
  631. 2, // SDL_GPU_SAMPLECOUNT_2
  632. 4, // SDL_GPU_SAMPLECOUNT_4
  633. 8, // SDL_GPU_SAMPLECOUNT_8
  634. };
  635. static D3D12_PRIMITIVE_TOPOLOGY SDLToD3D12_PrimitiveType[] = {
  636. D3D_PRIMITIVE_TOPOLOGY_TRIANGLELIST, // TRIANGLELIST
  637. D3D_PRIMITIVE_TOPOLOGY_TRIANGLESTRIP, // TRIANGLESTRIP
  638. D3D_PRIMITIVE_TOPOLOGY_LINELIST, // LINELIST
  639. D3D_PRIMITIVE_TOPOLOGY_LINESTRIP, // LINESTRIP
  640. D3D_PRIMITIVE_TOPOLOGY_POINTLIST // POINTLIST
  641. };
  642. static D3D12_PRIMITIVE_TOPOLOGY_TYPE SDLToD3D12_PrimitiveTopologyType[] = {
  643. D3D12_PRIMITIVE_TOPOLOGY_TYPE_TRIANGLE, // TRIANGLELIST
  644. D3D12_PRIMITIVE_TOPOLOGY_TYPE_TRIANGLE, // TRIANGLESTRIP
  645. D3D12_PRIMITIVE_TOPOLOGY_TYPE_LINE, // LINELIST
  646. D3D12_PRIMITIVE_TOPOLOGY_TYPE_LINE, // LINESTRIP
  647. D3D12_PRIMITIVE_TOPOLOGY_TYPE_POINT // POINTLIST
  648. };
  649. static D3D12_TEXTURE_ADDRESS_MODE SDLToD3D12_SamplerAddressMode[] = {
  650. D3D12_TEXTURE_ADDRESS_MODE_WRAP, // REPEAT
  651. D3D12_TEXTURE_ADDRESS_MODE_MIRROR, // MIRRORED_REPEAT
  652. D3D12_TEXTURE_ADDRESS_MODE_CLAMP // CLAMP_TO_EDGE
  653. };
  654. static D3D12_FILTER SDLToD3D12_Filter(
  655. SDL_GPUFilter minFilter,
  656. SDL_GPUFilter magFilter,
  657. SDL_GPUSamplerMipmapMode mipmapMode,
  658. bool comparisonEnabled,
  659. bool anisotropyEnabled)
  660. {
  661. D3D12_FILTER result = D3D12_ENCODE_BASIC_FILTER(
  662. (minFilter == SDL_GPU_FILTER_LINEAR) ? 1 : 0,
  663. (magFilter == SDL_GPU_FILTER_LINEAR) ? 1 : 0,
  664. (mipmapMode == SDL_GPU_SAMPLERMIPMAPMODE_LINEAR) ? 1 : 0,
  665. comparisonEnabled ? 1 : 0);
  666. if (anisotropyEnabled) {
  667. result = (D3D12_FILTER)(result | D3D12_ANISOTROPIC_FILTERING_BIT);
  668. }
  669. return result;
  670. }
  671. // Structures
  672. typedef struct D3D12Renderer D3D12Renderer;
  673. typedef struct D3D12CommandBufferPool D3D12CommandBufferPool;
  674. typedef struct D3D12CommandBuffer D3D12CommandBuffer;
  675. typedef struct D3D12Texture D3D12Texture;
  676. typedef struct D3D12Shader D3D12Shader;
  677. typedef struct D3D12GraphicsPipeline D3D12GraphicsPipeline;
  678. typedef struct D3D12ComputePipeline D3D12ComputePipeline;
  679. typedef struct D3D12Buffer D3D12Buffer;
  680. typedef struct D3D12BufferContainer D3D12BufferContainer;
  681. typedef struct D3D12UniformBuffer D3D12UniformBuffer;
  682. typedef struct D3D12DescriptorHeap D3D12DescriptorHeap;
  683. typedef struct D3D12StagingDescriptor D3D12StagingDescriptor;
  684. typedef struct D3D12TextureDownload D3D12TextureDownload;
  685. typedef struct D3D12Fence
  686. {
  687. ID3D12Fence *handle;
  688. HANDLE event; // used for blocking
  689. SDL_AtomicInt referenceCount;
  690. } D3D12Fence;
  691. struct D3D12DescriptorHeap
  692. {
  693. ID3D12DescriptorHeap *handle;
  694. D3D12_DESCRIPTOR_HEAP_TYPE heapType;
  695. D3D12_CPU_DESCRIPTOR_HANDLE descriptorHeapCPUStart;
  696. D3D12_GPU_DESCRIPTOR_HANDLE descriptorHeapGPUStart; // only used by GPU heaps
  697. Uint32 maxDescriptors;
  698. Uint32 descriptorSize;
  699. bool staging;
  700. Uint32 currentDescriptorIndex; // only used by GPU heaps
  701. };
  702. typedef struct D3D12GPUDescriptorHeapPool
  703. {
  704. Uint32 capacity;
  705. Uint32 count;
  706. D3D12DescriptorHeap **heaps;
  707. SDL_Mutex *lock;
  708. } D3D12GPUDescriptorHeapPool;
  709. // The only thing we care about with staging descriptors is being able to grab a free descriptor.
  710. typedef struct D3D12StagingDescriptorPool
  711. {
  712. Uint32 heapCount;
  713. D3D12DescriptorHeap **heaps;
  714. // Descriptor handles are owned by resources, so these can be thought of as descriptions of a free index within a heap.
  715. Uint32 freeDescriptorCapacity;
  716. Uint32 freeDescriptorCount;
  717. D3D12StagingDescriptor *freeDescriptors;
  718. SDL_Mutex *lock;
  719. } D3D12StagingDescriptorPool;
  720. struct D3D12StagingDescriptor
  721. {
  722. D3D12StagingDescriptorPool *pool;
  723. D3D12DescriptorHeap *heap;
  724. D3D12_CPU_DESCRIPTOR_HANDLE cpuHandle;
  725. Uint32 cpuHandleIndex;
  726. };
  727. typedef struct D3D12TextureContainer
  728. {
  729. TextureCommonHeader header;
  730. D3D12Texture *activeTexture;
  731. D3D12Texture **textures;
  732. Uint32 textureCapacity;
  733. Uint32 textureCount;
  734. // Swapchain images cannot be cycled
  735. bool canBeCycled;
  736. char *debugName;
  737. } D3D12TextureContainer;
  738. // Null views represent by heap = NULL
  739. typedef struct D3D12TextureSubresource
  740. {
  741. D3D12Texture *parent;
  742. Uint32 layer;
  743. Uint32 level;
  744. Uint32 depth;
  745. Uint32 index;
  746. // One per depth slice
  747. D3D12StagingDescriptor *rtvHandles; // NULL if not a color target
  748. D3D12StagingDescriptor uavHandle; // NULL if not a compute storage write texture
  749. D3D12StagingDescriptor dsvHandle; // NULL if not a depth stencil target
  750. } D3D12TextureSubresource;
  751. struct D3D12Texture
  752. {
  753. D3D12TextureContainer *container;
  754. Uint32 containerIndex;
  755. D3D12TextureSubresource *subresources;
  756. Uint32 subresourceCount; /* layerCount * num_levels */
  757. ID3D12Resource *resource;
  758. D3D12StagingDescriptor srvHandle;
  759. SDL_AtomicInt referenceCount;
  760. };
  761. typedef struct D3D12Sampler
  762. {
  763. SDL_GPUSamplerCreateInfo createInfo;
  764. D3D12StagingDescriptor handle;
  765. SDL_AtomicInt referenceCount;
  766. } D3D12Sampler;
  767. typedef struct D3D12WindowData
  768. {
  769. SDL_Window *window;
  770. D3D12Renderer *renderer;
  771. int refcount;
  772. #if (defined(SDL_PLATFORM_XBOXONE) || defined(SDL_PLATFORM_XBOXSERIES))
  773. D3D12XBOX_FRAME_PIPELINE_TOKEN frameToken;
  774. #else
  775. IDXGISwapChain3 *swapchain;
  776. #endif
  777. SDL_GPUPresentMode present_mode;
  778. SDL_GPUSwapchainComposition swapchainComposition;
  779. DXGI_COLOR_SPACE_TYPE swapchainColorSpace;
  780. Uint32 frameCounter;
  781. D3D12TextureContainer textureContainers[MAX_FRAMES_IN_FLIGHT];
  782. Uint32 swapchainTextureCount;
  783. SDL_GPUFence *inFlightFences[MAX_FRAMES_IN_FLIGHT];
  784. Uint32 width;
  785. Uint32 height;
  786. bool needsSwapchainRecreate;
  787. } D3D12WindowData;
  788. typedef struct D3D12PresentData
  789. {
  790. D3D12WindowData *windowData;
  791. Uint32 swapchainImageIndex;
  792. } D3D12PresentData;
  793. #ifdef USE_PIX_RUNTIME
  794. typedef struct WinPixEventRuntimeFns {
  795. pfnBeginEventOnCommandList pBeginEventOnCommandList;
  796. pfnEndEventOnCommandList pEndEventOnCommandList;
  797. pfnSetMarkerOnCommandList pSetMarkerOnCommandList;
  798. } WinPixEventRuntimeFns;
  799. #endif
  800. struct D3D12Renderer
  801. {
  802. // Reference to the parent device
  803. SDL_GPUDevice *sdlGPUDevice;
  804. #if !(defined(SDL_PLATFORM_XBOXONE) || defined(SDL_PLATFORM_XBOXSERIES))
  805. IDXGIDebug *dxgiDebug;
  806. IDXGIFactory4 *factory;
  807. #ifdef HAVE_IDXGIINFOQUEUE
  808. IDXGIInfoQueue *dxgiInfoQueue;
  809. #endif
  810. IDXGIAdapter1 *adapter;
  811. SDL_SharedObject *dxgi_dll;
  812. SDL_SharedObject *dxgidebug_dll;
  813. #endif
  814. #ifdef USE_PIX_RUNTIME
  815. SDL_SharedObject *winpixeventruntime_dll;
  816. WinPixEventRuntimeFns winpixeventruntimeFns;
  817. #endif
  818. ID3D12Debug *d3d12Debug;
  819. BOOL supportsTearing;
  820. SDL_SharedObject *d3d12_dll;
  821. ID3D12Device *device;
  822. PFN_D3D12_SERIALIZE_ROOT_SIGNATURE pD3D12SerializeRootSignature;
  823. char *semantic;
  824. SDL_iconv_t iconv;
  825. ID3D12CommandQueue *commandQueue;
  826. bool debug_mode;
  827. bool GPUUploadHeapSupported;
  828. bool UnrestrictedBufferTextureCopyPitchSupported;
  829. // FIXME: these might not be necessary since we're not using custom heaps
  830. bool UMA;
  831. bool UMACacheCoherent;
  832. SDL_PropertiesID props;
  833. Uint32 allowedFramesInFlight;
  834. // Indirect command signatures
  835. ID3D12CommandSignature *indirectDrawCommandSignature;
  836. ID3D12CommandSignature *indirectIndexedDrawCommandSignature;
  837. ID3D12CommandSignature *indirectDispatchCommandSignature;
  838. // Blit
  839. SDL_GPUShader *blitVertexShader;
  840. SDL_GPUShader *blitFrom2DShader;
  841. SDL_GPUShader *blitFrom2DArrayShader;
  842. SDL_GPUShader *blitFrom3DShader;
  843. SDL_GPUShader *blitFromCubeShader;
  844. SDL_GPUShader *blitFromCubeArrayShader;
  845. SDL_GPUSampler *blitNearestSampler;
  846. SDL_GPUSampler *blitLinearSampler;
  847. BlitPipelineCacheEntry *blitPipelines;
  848. Uint32 blitPipelineCount;
  849. Uint32 blitPipelineCapacity;
  850. // Resources
  851. D3D12CommandBuffer **availableCommandBuffers;
  852. Uint32 availableCommandBufferCount;
  853. Uint32 availableCommandBufferCapacity;
  854. D3D12CommandBuffer **submittedCommandBuffers;
  855. Uint32 submittedCommandBufferCount;
  856. Uint32 submittedCommandBufferCapacity;
  857. D3D12UniformBuffer **uniformBufferPool;
  858. Uint32 uniformBufferPoolCount;
  859. Uint32 uniformBufferPoolCapacity;
  860. D3D12WindowData **claimedWindows;
  861. Uint32 claimedWindowCount;
  862. Uint32 claimedWindowCapacity;
  863. D3D12Fence **availableFences;
  864. Uint32 availableFenceCount;
  865. Uint32 availableFenceCapacity;
  866. D3D12StagingDescriptorPool *stagingDescriptorPools[D3D12_DESCRIPTOR_HEAP_TYPE_NUM_TYPES];
  867. D3D12GPUDescriptorHeapPool gpuDescriptorHeapPools[2];
  868. // Deferred resource releasing
  869. D3D12Buffer **buffersToDestroy;
  870. Uint32 buffersToDestroyCount;
  871. Uint32 buffersToDestroyCapacity;
  872. D3D12Texture **texturesToDestroy;
  873. Uint32 texturesToDestroyCount;
  874. Uint32 texturesToDestroyCapacity;
  875. D3D12Sampler **samplersToDestroy;
  876. Uint32 samplersToDestroyCount;
  877. Uint32 samplersToDestroyCapacity;
  878. D3D12GraphicsPipeline **graphicsPipelinesToDestroy;
  879. Uint32 graphicsPipelinesToDestroyCount;
  880. Uint32 graphicsPipelinesToDestroyCapacity;
  881. D3D12ComputePipeline **computePipelinesToDestroy;
  882. Uint32 computePipelinesToDestroyCount;
  883. Uint32 computePipelinesToDestroyCapacity;
  884. // Locks
  885. SDL_Mutex *acquireCommandBufferLock;
  886. SDL_Mutex *acquireUniformBufferLock;
  887. SDL_Mutex *submitLock;
  888. SDL_Mutex *windowLock;
  889. SDL_Mutex *fenceLock;
  890. SDL_Mutex *disposeLock;
  891. };
  892. struct D3D12CommandBuffer
  893. {
  894. // reserved for SDL_gpu
  895. CommandBufferCommonHeader common;
  896. // non owning parent reference
  897. D3D12Renderer *renderer;
  898. ID3D12CommandAllocator *commandAllocator;
  899. ID3D12GraphicsCommandList *graphicsCommandList;
  900. D3D12Fence *inFlightFence;
  901. bool autoReleaseFence;
  902. // Presentation data
  903. D3D12PresentData *presentDatas;
  904. Uint32 presentDataCount;
  905. Uint32 presentDataCapacity;
  906. D3D12TextureSubresource *colorTargetSubresources[MAX_COLOR_TARGET_BINDINGS];
  907. D3D12TextureSubresource *colorResolveSubresources[MAX_COLOR_TARGET_BINDINGS];
  908. D3D12TextureSubresource *depthStencilTextureSubresource;
  909. D3D12GraphicsPipeline *currentGraphicsPipeline;
  910. D3D12ComputePipeline *currentComputePipeline;
  911. // Set at acquire time
  912. D3D12DescriptorHeap *gpuDescriptorHeaps[D3D12_DESCRIPTOR_HEAP_TYPE_SAMPLER + 1];
  913. D3D12DescriptorHeap **usedDescriptorHeaps;
  914. Uint32 usedDescriptorHeapCount;
  915. Uint32 usedDescriptorHeapCapacity;
  916. D3D12UniformBuffer **usedUniformBuffers;
  917. Uint32 usedUniformBufferCount;
  918. Uint32 usedUniformBufferCapacity;
  919. // Resource slot state
  920. bool needVertexBufferBind;
  921. bool needVertexSamplerBind;
  922. bool needVertexStorageTextureBind;
  923. bool needVertexStorageBufferBind;
  924. bool needVertexUniformBufferBind[MAX_UNIFORM_BUFFERS_PER_STAGE];
  925. bool needFragmentSamplerBind;
  926. bool needFragmentStorageTextureBind;
  927. bool needFragmentStorageBufferBind;
  928. bool needFragmentUniformBufferBind[MAX_UNIFORM_BUFFERS_PER_STAGE];
  929. bool needComputeSamplerBind;
  930. bool needComputeReadOnlyStorageTextureBind;
  931. bool needComputeReadOnlyStorageBufferBind;
  932. bool needComputeUniformBufferBind[MAX_UNIFORM_BUFFERS_PER_STAGE];
  933. D3D12Buffer *vertexBuffers[MAX_VERTEX_BUFFERS];
  934. Uint32 vertexBufferOffsets[MAX_VERTEX_BUFFERS];
  935. Uint32 vertexBufferCount;
  936. D3D12_CPU_DESCRIPTOR_HANDLE vertexSamplerTextureDescriptorHandles[MAX_TEXTURE_SAMPLERS_PER_STAGE];
  937. D3D12_CPU_DESCRIPTOR_HANDLE vertexSamplerDescriptorHandles[MAX_TEXTURE_SAMPLERS_PER_STAGE];
  938. D3D12_CPU_DESCRIPTOR_HANDLE vertexStorageTextureDescriptorHandles[MAX_STORAGE_TEXTURES_PER_STAGE];
  939. D3D12_CPU_DESCRIPTOR_HANDLE vertexStorageBufferDescriptorHandles[MAX_STORAGE_BUFFERS_PER_STAGE];
  940. D3D12UniformBuffer *vertexUniformBuffers[MAX_UNIFORM_BUFFERS_PER_STAGE];
  941. D3D12_CPU_DESCRIPTOR_HANDLE fragmentSamplerTextureDescriptorHandles[MAX_TEXTURE_SAMPLERS_PER_STAGE];
  942. D3D12_CPU_DESCRIPTOR_HANDLE fragmentSamplerDescriptorHandles[MAX_TEXTURE_SAMPLERS_PER_STAGE];
  943. D3D12_CPU_DESCRIPTOR_HANDLE fragmentStorageTextureDescriptorHandles[MAX_STORAGE_TEXTURES_PER_STAGE];
  944. D3D12_CPU_DESCRIPTOR_HANDLE fragmentStorageBufferDescriptorHandles[MAX_STORAGE_BUFFERS_PER_STAGE];
  945. D3D12UniformBuffer *fragmentUniformBuffers[MAX_UNIFORM_BUFFERS_PER_STAGE];
  946. D3D12_CPU_DESCRIPTOR_HANDLE computeSamplerTextureDescriptorHandles[MAX_TEXTURE_SAMPLERS_PER_STAGE];
  947. D3D12_CPU_DESCRIPTOR_HANDLE computeSamplerDescriptorHandles[MAX_TEXTURE_SAMPLERS_PER_STAGE];
  948. D3D12_CPU_DESCRIPTOR_HANDLE computeReadOnlyStorageTextureDescriptorHandles[MAX_STORAGE_TEXTURES_PER_STAGE];
  949. D3D12_CPU_DESCRIPTOR_HANDLE computeReadOnlyStorageBufferDescriptorHandles[MAX_STORAGE_BUFFERS_PER_STAGE];
  950. // Track these separately because barriers can happen mid compute pass
  951. D3D12Texture *computeReadOnlyStorageTextures[MAX_STORAGE_TEXTURES_PER_STAGE];
  952. D3D12Buffer *computeReadOnlyStorageBuffers[MAX_STORAGE_BUFFERS_PER_STAGE];
  953. D3D12_CPU_DESCRIPTOR_HANDLE computeReadWriteStorageTextureDescriptorHandles[MAX_COMPUTE_WRITE_TEXTURES];
  954. D3D12_CPU_DESCRIPTOR_HANDLE computeReadWriteStorageBufferDescriptorHandles[MAX_COMPUTE_WRITE_BUFFERS];
  955. // Track these separately because they are bound when the compute pass begins
  956. D3D12TextureSubresource *computeReadWriteStorageTextureSubresources[MAX_COMPUTE_WRITE_TEXTURES];
  957. Uint32 computeReadWriteStorageTextureSubresourceCount;
  958. D3D12Buffer *computeReadWriteStorageBuffers[MAX_COMPUTE_WRITE_BUFFERS];
  959. Uint32 computeReadWriteStorageBufferCount;
  960. D3D12UniformBuffer *computeUniformBuffers[MAX_UNIFORM_BUFFERS_PER_STAGE];
  961. // Resource tracking
  962. D3D12Texture **usedTextures;
  963. Uint32 usedTextureCount;
  964. Uint32 usedTextureCapacity;
  965. D3D12Buffer **usedBuffers;
  966. Uint32 usedBufferCount;
  967. Uint32 usedBufferCapacity;
  968. D3D12Sampler **usedSamplers;
  969. Uint32 usedSamplerCount;
  970. Uint32 usedSamplerCapacity;
  971. D3D12GraphicsPipeline **usedGraphicsPipelines;
  972. Uint32 usedGraphicsPipelineCount;
  973. Uint32 usedGraphicsPipelineCapacity;
  974. D3D12ComputePipeline **usedComputePipelines;
  975. Uint32 usedComputePipelineCount;
  976. Uint32 usedComputePipelineCapacity;
  977. // Used for texture pitch hack
  978. D3D12TextureDownload **textureDownloads;
  979. Uint32 textureDownloadCount;
  980. Uint32 textureDownloadCapacity;
  981. };
  982. struct D3D12Shader
  983. {
  984. // todo cleanup
  985. void *bytecode;
  986. size_t bytecodeSize;
  987. SDL_GPUShaderStage stage;
  988. Uint32 num_samplers;
  989. Uint32 numUniformBuffers;
  990. Uint32 numStorageBuffers;
  991. Uint32 numStorageTextures;
  992. };
  993. typedef struct D3D12GraphicsRootSignature
  994. {
  995. ID3D12RootSignature *handle;
  996. Sint32 vertexSamplerRootIndex;
  997. Sint32 vertexSamplerTextureRootIndex;
  998. Sint32 vertexStorageTextureRootIndex;
  999. Sint32 vertexStorageBufferRootIndex;
  1000. Sint32 vertexUniformBufferRootIndex[MAX_UNIFORM_BUFFERS_PER_STAGE];
  1001. Sint32 fragmentSamplerRootIndex;
  1002. Sint32 fragmentSamplerTextureRootIndex;
  1003. Sint32 fragmentStorageTextureRootIndex;
  1004. Sint32 fragmentStorageBufferRootIndex;
  1005. Sint32 fragmentUniformBufferRootIndex[MAX_UNIFORM_BUFFERS_PER_STAGE];
  1006. } D3D12GraphicsRootSignature;
  1007. struct D3D12GraphicsPipeline
  1008. {
  1009. GraphicsPipelineCommonHeader header;
  1010. ID3D12PipelineState *pipelineState;
  1011. D3D12GraphicsRootSignature *rootSignature;
  1012. SDL_GPUPrimitiveType primitiveType;
  1013. Uint32 vertexStrides[MAX_VERTEX_BUFFERS];
  1014. SDL_AtomicInt referenceCount;
  1015. };
  1016. typedef struct D3D12ComputeRootSignature
  1017. {
  1018. ID3D12RootSignature *handle;
  1019. Sint32 samplerRootIndex;
  1020. Sint32 samplerTextureRootIndex;
  1021. Sint32 readOnlyStorageTextureRootIndex;
  1022. Sint32 readOnlyStorageBufferRootIndex;
  1023. Sint32 readWriteStorageTextureRootIndex;
  1024. Sint32 readWriteStorageBufferRootIndex;
  1025. Sint32 uniformBufferRootIndex[MAX_UNIFORM_BUFFERS_PER_STAGE];
  1026. } D3D12ComputeRootSignature;
  1027. struct D3D12ComputePipeline
  1028. {
  1029. ComputePipelineCommonHeader header;
  1030. ID3D12PipelineState *pipelineState;
  1031. D3D12ComputeRootSignature *rootSignature;
  1032. SDL_AtomicInt referenceCount;
  1033. };
  1034. struct D3D12TextureDownload
  1035. {
  1036. D3D12Buffer *destinationBuffer;
  1037. D3D12Buffer *temporaryBuffer;
  1038. Uint32 width;
  1039. Uint32 height;
  1040. Uint32 depth;
  1041. Uint32 bufferOffset;
  1042. Uint32 bytesPerRow;
  1043. Uint32 bytesPerDepthSlice;
  1044. Uint32 alignedBytesPerRow;
  1045. };
  1046. struct D3D12Buffer
  1047. {
  1048. D3D12BufferContainer *container;
  1049. Uint32 containerIndex;
  1050. ID3D12Resource *handle;
  1051. D3D12StagingDescriptor uavDescriptor;
  1052. D3D12StagingDescriptor srvDescriptor;
  1053. D3D12StagingDescriptor cbvDescriptor;
  1054. D3D12_GPU_VIRTUAL_ADDRESS virtualAddress;
  1055. Uint8 *mapPointer; // NULL except for upload buffers and fast uniform buffers
  1056. SDL_AtomicInt referenceCount;
  1057. bool transitioned; // used for initial resource barrier
  1058. };
  1059. struct D3D12BufferContainer
  1060. {
  1061. SDL_GPUBufferUsageFlags usage;
  1062. Uint32 size;
  1063. D3D12BufferType type;
  1064. D3D12Buffer *activeBuffer;
  1065. D3D12Buffer **buffers;
  1066. Uint32 bufferCapacity;
  1067. Uint32 bufferCount;
  1068. D3D12_RESOURCE_DESC bufferDesc;
  1069. char *debugName;
  1070. };
  1071. struct D3D12UniformBuffer
  1072. {
  1073. D3D12Buffer *buffer;
  1074. Uint32 writeOffset;
  1075. Uint32 drawOffset;
  1076. };
  1077. // Forward function declarations
  1078. static void D3D12_ReleaseWindow(SDL_GPURenderer *driverData, SDL_Window *window);
  1079. static bool D3D12_Wait(SDL_GPURenderer *driverData);
  1080. static bool D3D12_WaitForFences(SDL_GPURenderer *driverData, bool waitAll, SDL_GPUFence *const *fences, Uint32 numFences);
  1081. static void D3D12_INTERNAL_ReleaseBlitPipelines(SDL_GPURenderer *driverData);
  1082. // Helpers
  1083. static Uint32 D3D12_INTERNAL_Align(Uint32 location, Uint32 alignment)
  1084. {
  1085. return (location + (alignment - 1)) & ~(alignment - 1);
  1086. }
  1087. // Xbox Hack
  1088. #if (defined(SDL_PLATFORM_XBOXONE) || defined(SDL_PLATFORM_XBOXSERIES))
  1089. // FIXME: This is purely to work around a presentation bug when recreating the device/command queue.
  1090. static ID3D12Device *s_Device;
  1091. static ID3D12CommandQueue *s_CommandQueue;
  1092. #endif
  1093. #if defined(SDL_PLATFORM_XBOXONE)
  1094. // These are not defined in d3d12_x.h.
  1095. typedef HRESULT (D3DAPI* PFN_D3D12_XBOX_CREATE_DEVICE)(_In_opt_ IGraphicsUnknown *, _In_ const D3D12XBOX_CREATE_DEVICE_PARAMETERS*, _In_ REFIID, _Outptr_opt_ void **);
  1096. #define D3D12_STANDARD_MULTISAMPLE_PATTERN DXGI_STANDARD_MULTISAMPLE_QUALITY_PATTERN
  1097. #endif
  1098. // Logging
  1099. static void D3D12_INTERNAL_SetError(
  1100. D3D12Renderer *renderer,
  1101. const char *msg,
  1102. HRESULT res)
  1103. {
  1104. #define MAX_ERROR_LEN 1024 // FIXME: Arbitrary!
  1105. // Buffer for text, ensure space for \0 terminator after buffer
  1106. char wszMsgBuff[MAX_ERROR_LEN + 1];
  1107. DWORD dwChars; // Number of chars returned.
  1108. if (res == DXGI_ERROR_DEVICE_REMOVED) {
  1109. if (renderer->device) {
  1110. res = ID3D12Device_GetDeviceRemovedReason(renderer->device);
  1111. }
  1112. }
  1113. // Try to get the message from the system errors.
  1114. dwChars = FormatMessageA(
  1115. FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
  1116. NULL,
  1117. res,
  1118. 0,
  1119. wszMsgBuff,
  1120. MAX_ERROR_LEN,
  1121. NULL);
  1122. // No message? Screw it, just post the code.
  1123. if (dwChars == 0) {
  1124. if (renderer->debug_mode) {
  1125. SDL_LogError(SDL_LOG_CATEGORY_GPU, "%s! Error Code: " HRESULT_FMT, msg, res);
  1126. }
  1127. SDL_SetError("%s! Error Code: " HRESULT_FMT, msg, res);
  1128. return;
  1129. }
  1130. // Ensure valid range
  1131. dwChars = SDL_min(dwChars, MAX_ERROR_LEN);
  1132. // Trim whitespace from tail of message
  1133. while (dwChars > 0) {
  1134. if (wszMsgBuff[dwChars - 1] <= ' ') {
  1135. dwChars--;
  1136. } else {
  1137. break;
  1138. }
  1139. }
  1140. // Ensure null-terminated string
  1141. wszMsgBuff[dwChars] = '\0';
  1142. if (renderer->debug_mode) {
  1143. SDL_LogError(SDL_LOG_CATEGORY_GPU, "%s! Error Code: %s " HRESULT_FMT, msg, wszMsgBuff, res);
  1144. }
  1145. SDL_SetError("%s! Error Code: %s " HRESULT_FMT, msg, wszMsgBuff, res);
  1146. }
  1147. // Release / Cleanup
  1148. static void D3D12_INTERNAL_ReleaseStagingDescriptorHandle(
  1149. D3D12StagingDescriptor *cpuDescriptor)
  1150. {
  1151. D3D12StagingDescriptorPool *pool = cpuDescriptor->pool;
  1152. if (pool != NULL) {
  1153. SDL_LockMutex(pool->lock);
  1154. SDL_memcpy(&pool->freeDescriptors[pool->freeDescriptorCount], cpuDescriptor, sizeof(D3D12StagingDescriptor));
  1155. pool->freeDescriptorCount += 1;
  1156. SDL_UnlockMutex(pool->lock);
  1157. }
  1158. }
  1159. static void D3D12_INTERNAL_DestroyBuffer(
  1160. D3D12Buffer *buffer)
  1161. {
  1162. if (!buffer) {
  1163. return;
  1164. }
  1165. if (buffer->mapPointer != NULL) {
  1166. ID3D12Resource_Unmap(
  1167. buffer->handle,
  1168. 0,
  1169. NULL);
  1170. }
  1171. D3D12_INTERNAL_ReleaseStagingDescriptorHandle(
  1172. &buffer->srvDescriptor);
  1173. D3D12_INTERNAL_ReleaseStagingDescriptorHandle(
  1174. &buffer->uavDescriptor);
  1175. D3D12_INTERNAL_ReleaseStagingDescriptorHandle(
  1176. &buffer->cbvDescriptor);
  1177. if (buffer->handle) {
  1178. ID3D12Resource_Release(buffer->handle);
  1179. }
  1180. SDL_free(buffer);
  1181. }
  1182. static void D3D12_INTERNAL_ReleaseBuffer(
  1183. D3D12Renderer *renderer,
  1184. D3D12Buffer *buffer)
  1185. {
  1186. SDL_LockMutex(renderer->disposeLock);
  1187. EXPAND_ARRAY_IF_NEEDED(
  1188. renderer->buffersToDestroy,
  1189. D3D12Buffer *,
  1190. renderer->buffersToDestroyCount + 1,
  1191. renderer->buffersToDestroyCapacity,
  1192. renderer->buffersToDestroyCapacity * 2);
  1193. renderer->buffersToDestroy[renderer->buffersToDestroyCount] = buffer;
  1194. renderer->buffersToDestroyCount += 1;
  1195. SDL_UnlockMutex(renderer->disposeLock);
  1196. }
  1197. static void D3D12_INTERNAL_ReleaseBufferContainer(
  1198. D3D12Renderer *renderer,
  1199. D3D12BufferContainer *container)
  1200. {
  1201. SDL_LockMutex(renderer->disposeLock);
  1202. for (Uint32 i = 0; i < container->bufferCount; i += 1) {
  1203. D3D12_INTERNAL_ReleaseBuffer(
  1204. renderer,
  1205. container->buffers[i]);
  1206. }
  1207. // Containers are just client handles, so we can free immediately
  1208. SDL_free(container->debugName);
  1209. SDL_free(container->buffers);
  1210. SDL_free(container);
  1211. SDL_UnlockMutex(renderer->disposeLock);
  1212. }
  1213. static void D3D12_INTERNAL_DestroyTexture(
  1214. D3D12Texture *texture)
  1215. {
  1216. if (!texture) {
  1217. return;
  1218. }
  1219. for (Uint32 i = 0; i < texture->subresourceCount; i += 1) {
  1220. D3D12TextureSubresource *subresource = &texture->subresources[i];
  1221. if (subresource->rtvHandles) {
  1222. for (Uint32 depthIndex = 0; depthIndex < subresource->depth; depthIndex += 1) {
  1223. D3D12_INTERNAL_ReleaseStagingDescriptorHandle(
  1224. &subresource->rtvHandles[depthIndex]);
  1225. }
  1226. SDL_free(subresource->rtvHandles);
  1227. }
  1228. D3D12_INTERNAL_ReleaseStagingDescriptorHandle(
  1229. &subresource->uavHandle);
  1230. D3D12_INTERNAL_ReleaseStagingDescriptorHandle(
  1231. &subresource->dsvHandle);
  1232. }
  1233. SDL_free(texture->subresources);
  1234. D3D12_INTERNAL_ReleaseStagingDescriptorHandle(
  1235. &texture->srvHandle);
  1236. if (texture->resource) {
  1237. ID3D12Resource_Release(texture->resource);
  1238. }
  1239. SDL_free(texture);
  1240. }
  1241. static void D3D12_INTERNAL_ReleaseTexture(
  1242. D3D12Renderer *renderer,
  1243. D3D12Texture *texture)
  1244. {
  1245. SDL_LockMutex(renderer->disposeLock);
  1246. EXPAND_ARRAY_IF_NEEDED(
  1247. renderer->texturesToDestroy,
  1248. D3D12Texture *,
  1249. renderer->texturesToDestroyCount + 1,
  1250. renderer->texturesToDestroyCapacity,
  1251. renderer->texturesToDestroyCapacity * 2);
  1252. renderer->texturesToDestroy[renderer->texturesToDestroyCount] = texture;
  1253. renderer->texturesToDestroyCount += 1;
  1254. SDL_UnlockMutex(renderer->disposeLock);
  1255. }
  1256. static void D3D12_INTERNAL_ReleaseTextureContainer(
  1257. D3D12Renderer *renderer,
  1258. D3D12TextureContainer *container)
  1259. {
  1260. SDL_LockMutex(renderer->disposeLock);
  1261. for (Uint32 i = 0; i < container->textureCount; i += 1) {
  1262. D3D12_INTERNAL_ReleaseTexture(
  1263. renderer,
  1264. container->textures[i]);
  1265. }
  1266. SDL_DestroyProperties(container->header.info.props);
  1267. // Containers are just client handles, so we can destroy immediately
  1268. SDL_free(container->debugName);
  1269. SDL_free(container->textures);
  1270. SDL_free(container);
  1271. SDL_UnlockMutex(renderer->disposeLock);
  1272. }
  1273. static void D3D12_INTERNAL_DestroySampler(
  1274. D3D12Sampler *sampler)
  1275. {
  1276. D3D12_INTERNAL_ReleaseStagingDescriptorHandle(
  1277. &sampler->handle);
  1278. SDL_free(sampler);
  1279. }
  1280. static void D3D12_INTERNAL_DestroyGraphicsRootSignature(
  1281. D3D12GraphicsRootSignature *rootSignature)
  1282. {
  1283. if (!rootSignature) {
  1284. return;
  1285. }
  1286. if (rootSignature->handle) {
  1287. ID3D12RootSignature_Release(rootSignature->handle);
  1288. }
  1289. SDL_free(rootSignature);
  1290. }
  1291. static void D3D12_INTERNAL_DestroyGraphicsPipeline(
  1292. D3D12GraphicsPipeline *graphicsPipeline)
  1293. {
  1294. if (graphicsPipeline->pipelineState) {
  1295. ID3D12PipelineState_Release(graphicsPipeline->pipelineState);
  1296. }
  1297. D3D12_INTERNAL_DestroyGraphicsRootSignature(graphicsPipeline->rootSignature);
  1298. SDL_free(graphicsPipeline);
  1299. }
  1300. static void D3D12_INTERNAL_DestroyComputeRootSignature(
  1301. D3D12ComputeRootSignature *rootSignature)
  1302. {
  1303. if (!rootSignature) {
  1304. return;
  1305. }
  1306. if (rootSignature->handle) {
  1307. ID3D12RootSignature_Release(rootSignature->handle);
  1308. }
  1309. SDL_free(rootSignature);
  1310. }
  1311. static void D3D12_INTERNAL_DestroyComputePipeline(
  1312. D3D12ComputePipeline *computePipeline)
  1313. {
  1314. if (computePipeline->pipelineState) {
  1315. ID3D12PipelineState_Release(computePipeline->pipelineState);
  1316. }
  1317. D3D12_INTERNAL_DestroyComputeRootSignature(computePipeline->rootSignature);
  1318. SDL_free(computePipeline);
  1319. }
  1320. static void D3D12_INTERNAL_ReleaseFenceToPool(
  1321. D3D12Renderer *renderer,
  1322. D3D12Fence *fence)
  1323. {
  1324. SDL_LockMutex(renderer->fenceLock);
  1325. EXPAND_ARRAY_IF_NEEDED(
  1326. renderer->availableFences,
  1327. D3D12Fence *,
  1328. renderer->availableFenceCount + 1,
  1329. renderer->availableFenceCapacity,
  1330. renderer->availableFenceCapacity * 2);
  1331. renderer->availableFences[renderer->availableFenceCount] = fence;
  1332. renderer->availableFenceCount += 1;
  1333. SDL_UnlockMutex(renderer->fenceLock);
  1334. }
  1335. static void D3D12_ReleaseFence(
  1336. SDL_GPURenderer *driverData,
  1337. SDL_GPUFence *fence)
  1338. {
  1339. D3D12Fence *d3d12Fence = (D3D12Fence *)fence;
  1340. if (SDL_AtomicDecRef(&d3d12Fence->referenceCount)) {
  1341. D3D12_INTERNAL_ReleaseFenceToPool(
  1342. (D3D12Renderer *)driverData,
  1343. d3d12Fence);
  1344. }
  1345. }
  1346. static bool D3D12_QueryFence(
  1347. SDL_GPURenderer *driverData,
  1348. SDL_GPUFence *fence)
  1349. {
  1350. D3D12Fence *d3d12Fence = (D3D12Fence *)fence;
  1351. return ID3D12Fence_GetCompletedValue(d3d12Fence->handle) == D3D12_FENCE_SIGNAL_VALUE;
  1352. }
  1353. static void D3D12_INTERNAL_DestroyDescriptorHeap(D3D12DescriptorHeap *descriptorHeap)
  1354. {
  1355. if (!descriptorHeap) {
  1356. return;
  1357. }
  1358. if (descriptorHeap->handle) {
  1359. ID3D12DescriptorHeap_Release(descriptorHeap->handle);
  1360. }
  1361. SDL_free(descriptorHeap);
  1362. }
  1363. static void D3D12_INTERNAL_DestroyStagingDescriptorPool(
  1364. D3D12StagingDescriptorPool *pool)
  1365. {
  1366. for (Uint32 i = 0; i < pool->heapCount; i += 1) {
  1367. D3D12_INTERNAL_DestroyDescriptorHeap(pool->heaps[i]);
  1368. }
  1369. SDL_free(pool->heaps);
  1370. SDL_free(pool->freeDescriptors);
  1371. SDL_DestroyMutex(pool->lock);
  1372. SDL_free(pool);
  1373. }
  1374. static void D3D12_INTERNAL_DestroyCommandBuffer(D3D12CommandBuffer *commandBuffer)
  1375. {
  1376. if (!commandBuffer) {
  1377. return;
  1378. }
  1379. if (commandBuffer->graphicsCommandList) {
  1380. ID3D12GraphicsCommandList_Release(commandBuffer->graphicsCommandList);
  1381. }
  1382. if (commandBuffer->commandAllocator) {
  1383. ID3D12CommandAllocator_Release(commandBuffer->commandAllocator);
  1384. }
  1385. SDL_free(commandBuffer->presentDatas);
  1386. SDL_free(commandBuffer->usedTextures);
  1387. SDL_free(commandBuffer->usedBuffers);
  1388. SDL_free(commandBuffer->usedSamplers);
  1389. SDL_free(commandBuffer->usedGraphicsPipelines);
  1390. SDL_free(commandBuffer->usedComputePipelines);
  1391. SDL_free(commandBuffer->usedDescriptorHeaps);
  1392. SDL_free(commandBuffer->usedUniformBuffers);
  1393. SDL_free(commandBuffer->textureDownloads);
  1394. SDL_free(commandBuffer);
  1395. }
  1396. static void D3D12_INTERNAL_DestroyFence(D3D12Fence *fence)
  1397. {
  1398. if (!fence) {
  1399. return;
  1400. }
  1401. if (fence->handle) {
  1402. ID3D12Fence_Release(fence->handle);
  1403. }
  1404. if (fence->event) {
  1405. CloseHandle(fence->event);
  1406. }
  1407. SDL_free(fence);
  1408. }
  1409. static void D3D12_INTERNAL_DestroyRenderer(D3D12Renderer *renderer)
  1410. {
  1411. // Release uniform buffers
  1412. for (Uint32 i = 0; i < renderer->uniformBufferPoolCount; i += 1) {
  1413. D3D12_INTERNAL_DestroyBuffer(
  1414. renderer->uniformBufferPool[i]->buffer);
  1415. SDL_free(renderer->uniformBufferPool[i]);
  1416. }
  1417. // Clean up descriptor heaps
  1418. for (Uint32 i = 0; i < D3D12_DESCRIPTOR_HEAP_TYPE_NUM_TYPES; i += 1) {
  1419. if (renderer->stagingDescriptorPools[i]) {
  1420. D3D12_INTERNAL_DestroyStagingDescriptorPool(renderer->stagingDescriptorPools[i]);
  1421. renderer->stagingDescriptorPools[i] = NULL;
  1422. }
  1423. }
  1424. for (Uint32 i = 0; i < 2; i += 1) {
  1425. if (renderer->gpuDescriptorHeapPools[i].heaps) {
  1426. for (Uint32 j = 0; j < renderer->gpuDescriptorHeapPools[i].count; j += 1) {
  1427. if (renderer->gpuDescriptorHeapPools[i].heaps[j]) {
  1428. D3D12_INTERNAL_DestroyDescriptorHeap(renderer->gpuDescriptorHeapPools[i].heaps[j]);
  1429. renderer->gpuDescriptorHeapPools[i].heaps[j] = NULL;
  1430. }
  1431. }
  1432. SDL_free(renderer->gpuDescriptorHeapPools[i].heaps);
  1433. }
  1434. if (renderer->gpuDescriptorHeapPools[i].lock) {
  1435. SDL_DestroyMutex(renderer->gpuDescriptorHeapPools[i].lock);
  1436. renderer->gpuDescriptorHeapPools[i].lock = NULL;
  1437. }
  1438. }
  1439. // Release command buffers
  1440. for (Uint32 i = 0; i < renderer->availableCommandBufferCount; i += 1) {
  1441. if (renderer->availableCommandBuffers[i]) {
  1442. D3D12_INTERNAL_DestroyCommandBuffer(renderer->availableCommandBuffers[i]);
  1443. renderer->availableCommandBuffers[i] = NULL;
  1444. }
  1445. }
  1446. // Release fences
  1447. for (Uint32 i = 0; i < renderer->availableFenceCount; i += 1) {
  1448. if (renderer->availableFences[i]) {
  1449. D3D12_INTERNAL_DestroyFence(renderer->availableFences[i]);
  1450. renderer->availableFences[i] = NULL;
  1451. }
  1452. }
  1453. // Clean up allocations
  1454. SDL_free(renderer->availableCommandBuffers);
  1455. SDL_free(renderer->submittedCommandBuffers);
  1456. SDL_free(renderer->uniformBufferPool);
  1457. SDL_free(renderer->claimedWindows);
  1458. SDL_free(renderer->availableFences);
  1459. SDL_free(renderer->buffersToDestroy);
  1460. SDL_free(renderer->texturesToDestroy);
  1461. SDL_free(renderer->samplersToDestroy);
  1462. SDL_free(renderer->graphicsPipelinesToDestroy);
  1463. SDL_free(renderer->computePipelinesToDestroy);
  1464. SDL_DestroyProperties(renderer->props);
  1465. // Tear down D3D12 objects
  1466. if (renderer->indirectDrawCommandSignature) {
  1467. ID3D12CommandSignature_Release(renderer->indirectDrawCommandSignature);
  1468. renderer->indirectDrawCommandSignature = NULL;
  1469. }
  1470. if (renderer->indirectIndexedDrawCommandSignature) {
  1471. ID3D12CommandSignature_Release(renderer->indirectIndexedDrawCommandSignature);
  1472. renderer->indirectIndexedDrawCommandSignature = NULL;
  1473. }
  1474. if (renderer->indirectDispatchCommandSignature) {
  1475. ID3D12CommandSignature_Release(renderer->indirectDispatchCommandSignature);
  1476. renderer->indirectDispatchCommandSignature = NULL;
  1477. }
  1478. #if !(defined(SDL_PLATFORM_XBOXONE) || defined(SDL_PLATFORM_XBOXSERIES))
  1479. if (renderer->commandQueue) {
  1480. ID3D12CommandQueue_Release(renderer->commandQueue);
  1481. renderer->commandQueue = NULL;
  1482. }
  1483. if (renderer->device) {
  1484. ID3D12Device_Release(renderer->device);
  1485. renderer->device = NULL;
  1486. }
  1487. if (renderer->adapter) {
  1488. IDXGIAdapter1_Release(renderer->adapter);
  1489. renderer->adapter = NULL;
  1490. }
  1491. if (renderer->factory) {
  1492. IDXGIFactory4_Release(renderer->factory);
  1493. renderer->factory = NULL;
  1494. }
  1495. if (renderer->dxgiDebug) {
  1496. IDXGIDebug_ReportLiveObjects(
  1497. renderer->dxgiDebug,
  1498. D3D_IID_DXGI_DEBUG_ALL,
  1499. (DXGI_DEBUG_RLO_FLAGS)(DXGI_DEBUG_RLO_SUMMARY | DXGI_DEBUG_RLO_DETAIL));
  1500. IDXGIDebug_Release(renderer->dxgiDebug);
  1501. renderer->dxgiDebug = NULL;
  1502. }
  1503. #endif
  1504. if (renderer->d3d12_dll) {
  1505. SDL_UnloadObject(renderer->d3d12_dll);
  1506. renderer->d3d12_dll = NULL;
  1507. }
  1508. #if !(defined(SDL_PLATFORM_XBOXONE) || defined(SDL_PLATFORM_XBOXSERIES))
  1509. if (renderer->dxgi_dll) {
  1510. SDL_UnloadObject(renderer->dxgi_dll);
  1511. renderer->dxgi_dll = NULL;
  1512. }
  1513. if (renderer->dxgidebug_dll) {
  1514. SDL_UnloadObject(renderer->dxgidebug_dll);
  1515. renderer->dxgidebug_dll = NULL;
  1516. }
  1517. #endif
  1518. #ifdef USE_PIX_RUNTIME
  1519. if (renderer->winpixeventruntime_dll) {
  1520. SDL_UnloadObject(renderer->winpixeventruntime_dll);
  1521. renderer->winpixeventruntime_dll = NULL;
  1522. }
  1523. #endif
  1524. renderer->pD3D12SerializeRootSignature = NULL;
  1525. if (renderer->iconv) {
  1526. SDL_iconv_close(renderer->iconv);
  1527. }
  1528. SDL_DestroyMutex(renderer->acquireCommandBufferLock);
  1529. SDL_DestroyMutex(renderer->acquireUniformBufferLock);
  1530. SDL_DestroyMutex(renderer->submitLock);
  1531. SDL_DestroyMutex(renderer->windowLock);
  1532. SDL_DestroyMutex(renderer->fenceLock);
  1533. SDL_DestroyMutex(renderer->disposeLock);
  1534. SDL_free(renderer->semantic);
  1535. SDL_free(renderer);
  1536. }
  1537. static void D3D12_DestroyDevice(SDL_GPUDevice *device)
  1538. {
  1539. D3D12Renderer *renderer = (D3D12Renderer *)device->driverData;
  1540. // Release blit pipeline structures
  1541. D3D12_INTERNAL_ReleaseBlitPipelines((SDL_GPURenderer *)renderer);
  1542. // Flush any remaining GPU work...
  1543. D3D12_Wait((SDL_GPURenderer *)renderer);
  1544. // Release window data
  1545. for (Sint32 i = renderer->claimedWindowCount - 1; i >= 0; i -= 1) {
  1546. D3D12_ReleaseWindow((SDL_GPURenderer *)renderer, renderer->claimedWindows[i]->window);
  1547. }
  1548. D3D12_INTERNAL_DestroyRenderer(renderer);
  1549. SDL_free(device);
  1550. }
  1551. static SDL_PropertiesID D3D12_GetDeviceProperties(SDL_GPUDevice *device)
  1552. {
  1553. D3D12Renderer *renderer = (D3D12Renderer *)device->driverData;
  1554. return renderer->props;
  1555. }
  1556. // Barriers
  1557. static inline Uint32 D3D12_INTERNAL_CalcSubresource(
  1558. Uint32 mipLevel,
  1559. Uint32 layer,
  1560. Uint32 numLevels)
  1561. {
  1562. return mipLevel + (layer * numLevels);
  1563. }
  1564. static void D3D12_INTERNAL_ResourceBarrier(
  1565. D3D12CommandBuffer *commandBuffer,
  1566. D3D12_RESOURCE_STATES sourceState,
  1567. D3D12_RESOURCE_STATES destinationState,
  1568. ID3D12Resource *resource,
  1569. Uint32 subresourceIndex,
  1570. bool needsUavBarrier)
  1571. {
  1572. D3D12_RESOURCE_BARRIER barrierDesc[2];
  1573. Uint32 numBarriers = 0;
  1574. // No transition barrier is needed if the state is not changing.
  1575. if (sourceState != destinationState) {
  1576. barrierDesc[numBarriers].Type = D3D12_RESOURCE_BARRIER_TYPE_TRANSITION;
  1577. barrierDesc[numBarriers].Flags = (D3D12_RESOURCE_BARRIER_FLAGS)0;
  1578. barrierDesc[numBarriers].Transition.StateBefore = sourceState;
  1579. barrierDesc[numBarriers].Transition.StateAfter = destinationState;
  1580. barrierDesc[numBarriers].Transition.pResource = resource;
  1581. barrierDesc[numBarriers].Transition.Subresource = subresourceIndex;
  1582. numBarriers += 1;
  1583. }
  1584. if (needsUavBarrier) {
  1585. barrierDesc[numBarriers].Type = D3D12_RESOURCE_BARRIER_TYPE_UAV;
  1586. barrierDesc[numBarriers].Flags = (D3D12_RESOURCE_BARRIER_FLAGS)0;
  1587. barrierDesc[numBarriers].UAV.pResource = resource;
  1588. numBarriers += 1;
  1589. }
  1590. if (numBarriers > 0) {
  1591. ID3D12GraphicsCommandList_ResourceBarrier(
  1592. commandBuffer->graphicsCommandList,
  1593. numBarriers,
  1594. barrierDesc);
  1595. }
  1596. }
  1597. static void D3D12_INTERNAL_TextureSubresourceBarrier(
  1598. D3D12CommandBuffer *commandBuffer,
  1599. D3D12_RESOURCE_STATES sourceState,
  1600. D3D12_RESOURCE_STATES destinationState,
  1601. D3D12TextureSubresource *textureSubresource)
  1602. {
  1603. bool needsUAVBarrier =
  1604. (textureSubresource->parent->container->header.info.usage & SDL_GPU_TEXTUREUSAGE_COMPUTE_STORAGE_WRITE) ||
  1605. (textureSubresource->parent->container->header.info.usage & SDL_GPU_TEXTUREUSAGE_COMPUTE_STORAGE_SIMULTANEOUS_READ_WRITE);
  1606. D3D12_INTERNAL_ResourceBarrier(
  1607. commandBuffer,
  1608. sourceState,
  1609. destinationState,
  1610. textureSubresource->parent->resource,
  1611. textureSubresource->index,
  1612. needsUAVBarrier);
  1613. }
  1614. static D3D12_RESOURCE_STATES D3D12_INTERNAL_DefaultTextureResourceState(
  1615. SDL_GPUTextureUsageFlags usageFlags)
  1616. {
  1617. // NOTE: order matters here!
  1618. if (usageFlags & SDL_GPU_TEXTUREUSAGE_SAMPLER) {
  1619. return D3D12_RESOURCE_STATE_ALL_SHADER_RESOURCE;
  1620. } else if (usageFlags & SDL_GPU_TEXTUREUSAGE_GRAPHICS_STORAGE_READ) {
  1621. return D3D12_RESOURCE_STATE_ALL_SHADER_RESOURCE;
  1622. } else if (usageFlags & SDL_GPU_TEXTUREUSAGE_COLOR_TARGET) {
  1623. return D3D12_RESOURCE_STATE_RENDER_TARGET;
  1624. } else if (usageFlags & SDL_GPU_TEXTUREUSAGE_DEPTH_STENCIL_TARGET) {
  1625. return D3D12_RESOURCE_STATE_DEPTH_WRITE;
  1626. } else if (usageFlags & SDL_GPU_TEXTUREUSAGE_COMPUTE_STORAGE_READ) {
  1627. return D3D12_RESOURCE_STATE_NON_PIXEL_SHADER_RESOURCE;
  1628. } else if (usageFlags & SDL_GPU_TEXTUREUSAGE_COMPUTE_STORAGE_WRITE) {
  1629. return D3D12_RESOURCE_STATE_UNORDERED_ACCESS;
  1630. } else if (usageFlags & SDL_GPU_TEXTUREUSAGE_COMPUTE_STORAGE_SIMULTANEOUS_READ_WRITE) {
  1631. return D3D12_RESOURCE_STATE_UNORDERED_ACCESS;
  1632. } else {
  1633. SDL_LogError(SDL_LOG_CATEGORY_GPU, "Texture has no default usage mode!");
  1634. return D3D12_RESOURCE_STATE_ALL_SHADER_RESOURCE;
  1635. }
  1636. }
  1637. static void D3D12_INTERNAL_TextureSubresourceTransitionFromDefaultUsage(
  1638. D3D12CommandBuffer *commandBuffer,
  1639. D3D12_RESOURCE_STATES destinationUsageMode,
  1640. D3D12TextureSubresource *textureSubresource)
  1641. {
  1642. D3D12_INTERNAL_TextureSubresourceBarrier(
  1643. commandBuffer,
  1644. D3D12_INTERNAL_DefaultTextureResourceState(textureSubresource->parent->container->header.info.usage),
  1645. destinationUsageMode,
  1646. textureSubresource);
  1647. }
  1648. static void D3D12_INTERNAL_TextureTransitionFromDefaultUsage(
  1649. D3D12CommandBuffer *commandBuffer,
  1650. D3D12_RESOURCE_STATES destinationUsageMode,
  1651. D3D12Texture *texture)
  1652. {
  1653. for (Uint32 i = 0; i < texture->subresourceCount; i += 1) {
  1654. D3D12_INTERNAL_TextureSubresourceTransitionFromDefaultUsage(
  1655. commandBuffer,
  1656. destinationUsageMode,
  1657. &texture->subresources[i]);
  1658. }
  1659. }
  1660. static void D3D12_INTERNAL_TextureSubresourceTransitionToDefaultUsage(
  1661. D3D12CommandBuffer *commandBuffer,
  1662. D3D12_RESOURCE_STATES sourceUsageMode,
  1663. D3D12TextureSubresource *textureSubresource)
  1664. {
  1665. D3D12_INTERNAL_TextureSubresourceBarrier(
  1666. commandBuffer,
  1667. sourceUsageMode,
  1668. D3D12_INTERNAL_DefaultTextureResourceState(textureSubresource->parent->container->header.info.usage),
  1669. textureSubresource);
  1670. }
  1671. static void D3D12_INTERNAL_TextureTransitionToDefaultUsage(
  1672. D3D12CommandBuffer *commandBuffer,
  1673. D3D12_RESOURCE_STATES sourceUsageMode,
  1674. D3D12Texture *texture)
  1675. {
  1676. for (Uint32 i = 0; i < texture->subresourceCount; i += 1) {
  1677. D3D12_INTERNAL_TextureSubresourceTransitionToDefaultUsage(
  1678. commandBuffer,
  1679. sourceUsageMode,
  1680. &texture->subresources[i]);
  1681. }
  1682. }
  1683. static D3D12_RESOURCE_STATES D3D12_INTERNAL_DefaultBufferResourceState(
  1684. D3D12Buffer *buffer)
  1685. {
  1686. if (buffer->container->usage & SDL_GPU_BUFFERUSAGE_VERTEX) {
  1687. return D3D12_RESOURCE_STATE_VERTEX_AND_CONSTANT_BUFFER;
  1688. } else if (buffer->container->usage & SDL_GPU_BUFFERUSAGE_INDEX) {
  1689. return D3D12_RESOURCE_STATE_INDEX_BUFFER;
  1690. } else if (buffer->container->usage & SDL_GPU_BUFFERUSAGE_INDIRECT) {
  1691. return D3D12_RESOURCE_STATE_INDIRECT_ARGUMENT;
  1692. } else if (buffer->container->usage & SDL_GPU_BUFFERUSAGE_GRAPHICS_STORAGE_READ) {
  1693. return D3D12_RESOURCE_STATE_ALL_SHADER_RESOURCE;
  1694. } else if (buffer->container->usage & SDL_GPU_BUFFERUSAGE_COMPUTE_STORAGE_READ) {
  1695. return D3D12_RESOURCE_STATE_NON_PIXEL_SHADER_RESOURCE;
  1696. } else if (buffer->container->usage & SDL_GPU_BUFFERUSAGE_COMPUTE_STORAGE_WRITE) {
  1697. return D3D12_RESOURCE_STATE_UNORDERED_ACCESS;
  1698. } else {
  1699. SDL_LogError(SDL_LOG_CATEGORY_GPU, "Buffer has no default usage mode!");
  1700. return D3D12_RESOURCE_STATE_VERTEX_AND_CONSTANT_BUFFER;
  1701. }
  1702. }
  1703. static void D3D12_INTERNAL_BufferBarrier(
  1704. D3D12CommandBuffer *commandBuffer,
  1705. D3D12_RESOURCE_STATES sourceState,
  1706. D3D12_RESOURCE_STATES destinationState,
  1707. D3D12Buffer *buffer)
  1708. {
  1709. D3D12_INTERNAL_ResourceBarrier(
  1710. commandBuffer,
  1711. buffer->transitioned ? sourceState : D3D12_RESOURCE_STATE_COMMON,
  1712. destinationState,
  1713. buffer->handle,
  1714. 0,
  1715. buffer->container->usage & SDL_GPU_BUFFERUSAGE_COMPUTE_STORAGE_WRITE);
  1716. buffer->transitioned = true;
  1717. }
  1718. static void D3D12_INTERNAL_BufferTransitionFromDefaultUsage(
  1719. D3D12CommandBuffer *commandBuffer,
  1720. D3D12_RESOURCE_STATES destinationState,
  1721. D3D12Buffer *buffer)
  1722. {
  1723. D3D12_INTERNAL_BufferBarrier(
  1724. commandBuffer,
  1725. D3D12_INTERNAL_DefaultBufferResourceState(buffer),
  1726. destinationState,
  1727. buffer);
  1728. }
  1729. static void D3D12_INTERNAL_BufferTransitionToDefaultUsage(
  1730. D3D12CommandBuffer *commandBuffer,
  1731. D3D12_RESOURCE_STATES sourceState,
  1732. D3D12Buffer *buffer)
  1733. {
  1734. D3D12_INTERNAL_BufferBarrier(
  1735. commandBuffer,
  1736. sourceState,
  1737. D3D12_INTERNAL_DefaultBufferResourceState(buffer),
  1738. buffer);
  1739. }
  1740. // Resource tracking
  1741. #define TRACK_RESOURCE(resource, type, array, count, capacity) \
  1742. Uint32 i; \
  1743. \
  1744. for (i = 0; i < commandBuffer->count; i += 1) { \
  1745. if (commandBuffer->array[i] == resource) { \
  1746. return; \
  1747. } \
  1748. } \
  1749. \
  1750. if (commandBuffer->count == commandBuffer->capacity) { \
  1751. commandBuffer->capacity += 1; \
  1752. commandBuffer->array = (type *)SDL_realloc( \
  1753. commandBuffer->array, \
  1754. commandBuffer->capacity * sizeof(type)); \
  1755. } \
  1756. commandBuffer->array[commandBuffer->count] = resource; \
  1757. commandBuffer->count += 1; \
  1758. SDL_AtomicIncRef(&resource->referenceCount);
  1759. static void D3D12_INTERNAL_TrackTexture(
  1760. D3D12CommandBuffer *commandBuffer,
  1761. D3D12Texture *texture)
  1762. {
  1763. TRACK_RESOURCE(
  1764. texture,
  1765. D3D12Texture *,
  1766. usedTextures,
  1767. usedTextureCount,
  1768. usedTextureCapacity)
  1769. }
  1770. static void D3D12_INTERNAL_TrackBuffer(
  1771. D3D12CommandBuffer *commandBuffer,
  1772. D3D12Buffer *buffer)
  1773. {
  1774. TRACK_RESOURCE(
  1775. buffer,
  1776. D3D12Buffer *,
  1777. usedBuffers,
  1778. usedBufferCount,
  1779. usedBufferCapacity)
  1780. }
  1781. static void D3D12_INTERNAL_TrackSampler(
  1782. D3D12CommandBuffer *commandBuffer,
  1783. D3D12Sampler *sampler)
  1784. {
  1785. TRACK_RESOURCE(
  1786. sampler,
  1787. D3D12Sampler *,
  1788. usedSamplers,
  1789. usedSamplerCount,
  1790. usedSamplerCapacity)
  1791. }
  1792. static void D3D12_INTERNAL_TrackGraphicsPipeline(
  1793. D3D12CommandBuffer *commandBuffer,
  1794. D3D12GraphicsPipeline *graphicsPipeline)
  1795. {
  1796. TRACK_RESOURCE(
  1797. graphicsPipeline,
  1798. D3D12GraphicsPipeline *,
  1799. usedGraphicsPipelines,
  1800. usedGraphicsPipelineCount,
  1801. usedGraphicsPipelineCapacity)
  1802. }
  1803. static void D3D12_INTERNAL_TrackComputePipeline(
  1804. D3D12CommandBuffer *commandBuffer,
  1805. D3D12ComputePipeline *computePipeline)
  1806. {
  1807. TRACK_RESOURCE(
  1808. computePipeline,
  1809. D3D12ComputePipeline *,
  1810. usedComputePipelines,
  1811. usedComputePipelineCount,
  1812. usedComputePipelineCapacity)
  1813. }
  1814. #undef TRACK_RESOURCE
  1815. // Debug Naming
  1816. static void D3D12_INTERNAL_SetPipelineStateName(
  1817. D3D12Renderer *renderer,
  1818. ID3D12PipelineState *pipelineState,
  1819. const char *text
  1820. ) {
  1821. if (renderer->debug_mode && text != NULL) {
  1822. WCHAR *wchar_text = WIN_UTF8ToStringW(text);
  1823. ID3D12PipelineState_SetName(
  1824. pipelineState,
  1825. wchar_text);
  1826. SDL_free(wchar_text);
  1827. }
  1828. }
  1829. static void D3D12_INTERNAL_SetResourceName(
  1830. D3D12Renderer *renderer,
  1831. ID3D12Resource *resource,
  1832. const char *text
  1833. ) {
  1834. if (renderer->debug_mode && text != NULL) {
  1835. WCHAR *wchar_text = WIN_UTF8ToStringW(text);
  1836. ID3D12Resource_SetName(
  1837. resource,
  1838. wchar_text);
  1839. SDL_free(wchar_text);
  1840. }
  1841. }
  1842. static void D3D12_SetBufferName(
  1843. SDL_GPURenderer *driverData,
  1844. SDL_GPUBuffer *buffer,
  1845. const char *text)
  1846. {
  1847. D3D12Renderer *renderer = (D3D12Renderer *)driverData;
  1848. D3D12BufferContainer *container = (D3D12BufferContainer *)buffer;
  1849. if (renderer->debug_mode && text != NULL) {
  1850. SDL_free(container->debugName);
  1851. container->debugName = SDL_strdup(text);
  1852. for (Uint32 i = 0; i < container->bufferCount; i += 1) {
  1853. D3D12_INTERNAL_SetResourceName(
  1854. renderer,
  1855. container->buffers[i]->handle,
  1856. text);
  1857. }
  1858. }
  1859. }
  1860. static void D3D12_SetTextureName(
  1861. SDL_GPURenderer *driverData,
  1862. SDL_GPUTexture *texture,
  1863. const char *text)
  1864. {
  1865. D3D12Renderer *renderer = (D3D12Renderer *)driverData;
  1866. D3D12TextureContainer *container = (D3D12TextureContainer *)texture;
  1867. if (renderer->debug_mode && text != NULL) {
  1868. SDL_free(container->debugName);
  1869. container->debugName = SDL_strdup(text);
  1870. for (Uint32 i = 0; i < container->textureCount; i += 1) {
  1871. D3D12_INTERNAL_SetResourceName(
  1872. renderer,
  1873. container->textures[i]->resource,
  1874. text);
  1875. }
  1876. }
  1877. }
  1878. /* These debug functions now require the PIX runtime under Windows to avoid validation
  1879. * layer errors. Calling them without the PIX runtime in your path is a no-op.
  1880. */
  1881. static void D3D12_InsertDebugLabel(
  1882. SDL_GPUCommandBuffer *commandBuffer,
  1883. const char *text)
  1884. {
  1885. D3D12CommandBuffer *d3d12CommandBuffer = (D3D12CommandBuffer *)commandBuffer;
  1886. #ifdef USE_PIX_RUNTIME
  1887. // Requires PIX runtime under Windows, no-op if DLL unavailable.
  1888. WinPixEventRuntimeFns *fns = &d3d12CommandBuffer->renderer->winpixeventruntimeFns;
  1889. if (fns->pSetMarkerOnCommandList) {
  1890. fns->pSetMarkerOnCommandList(d3d12CommandBuffer->graphicsCommandList, 0 /*default color*/, text);
  1891. }
  1892. #else
  1893. WCHAR *wchar_text = WIN_UTF8ToStringW(text);
  1894. ID3D12GraphicsCommandList_SetMarker(
  1895. d3d12CommandBuffer->graphicsCommandList,
  1896. 0,
  1897. wchar_text,
  1898. (UINT)SDL_wcslen(wchar_text) * sizeof(WCHAR));
  1899. SDL_free(wchar_text);
  1900. #endif
  1901. }
  1902. static void D3D12_PushDebugGroup(
  1903. SDL_GPUCommandBuffer *commandBuffer,
  1904. const char *name)
  1905. {
  1906. D3D12CommandBuffer *d3d12CommandBuffer = (D3D12CommandBuffer *)commandBuffer;
  1907. #ifdef USE_PIX_RUNTIME
  1908. // Requires PIX runtime under Windows, no-op if DLL unavailable.
  1909. WinPixEventRuntimeFns *fns = &d3d12CommandBuffer->renderer->winpixeventruntimeFns;
  1910. if (fns->pBeginEventOnCommandList) {
  1911. fns->pBeginEventOnCommandList(d3d12CommandBuffer->graphicsCommandList, 0 /*default color*/, name);
  1912. }
  1913. #else
  1914. WCHAR *wchar_text = WIN_UTF8ToStringW(name);
  1915. ID3D12GraphicsCommandList_BeginEvent(
  1916. d3d12CommandBuffer->graphicsCommandList,
  1917. 0,
  1918. wchar_text,
  1919. (UINT)SDL_wcslen(wchar_text) * sizeof(WCHAR));
  1920. SDL_free(wchar_text);
  1921. #endif
  1922. }
  1923. static void D3D12_PopDebugGroup(
  1924. SDL_GPUCommandBuffer *commandBuffer)
  1925. {
  1926. D3D12CommandBuffer *d3d12CommandBuffer = (D3D12CommandBuffer *)commandBuffer;
  1927. #ifdef USE_PIX_RUNTIME
  1928. // Requires PIX runtime under Windows, no-op if DLL unavailable.
  1929. WinPixEventRuntimeFns *fns = &d3d12CommandBuffer->renderer->winpixeventruntimeFns;
  1930. if (fns->pEndEventOnCommandList) {
  1931. fns->pEndEventOnCommandList(d3d12CommandBuffer->graphicsCommandList);
  1932. }
  1933. #else
  1934. ID3D12GraphicsCommandList_EndEvent(d3d12CommandBuffer->graphicsCommandList);
  1935. #endif
  1936. }
  1937. // State Creation
  1938. static D3D12DescriptorHeap *D3D12_INTERNAL_CreateDescriptorHeap(
  1939. D3D12Renderer *renderer,
  1940. D3D12_DESCRIPTOR_HEAP_TYPE type,
  1941. Uint32 descriptorCount,
  1942. bool staging)
  1943. {
  1944. D3D12DescriptorHeap *heap;
  1945. ID3D12DescriptorHeap *handle;
  1946. D3D12_DESCRIPTOR_HEAP_DESC heapDesc;
  1947. HRESULT res;
  1948. heap = (D3D12DescriptorHeap *)SDL_calloc(1, sizeof(D3D12DescriptorHeap));
  1949. if (!heap) {
  1950. return NULL;
  1951. }
  1952. heap->currentDescriptorIndex = 0;
  1953. heapDesc.NumDescriptors = descriptorCount;
  1954. heapDesc.Type = type;
  1955. heapDesc.Flags = staging ? D3D12_DESCRIPTOR_HEAP_FLAG_NONE : D3D12_DESCRIPTOR_HEAP_FLAG_SHADER_VISIBLE;
  1956. heapDesc.NodeMask = 0;
  1957. res = ID3D12Device_CreateDescriptorHeap(
  1958. renderer->device,
  1959. &heapDesc,
  1960. D3D_GUID(D3D_IID_ID3D12DescriptorHeap),
  1961. (void **)&handle);
  1962. if (FAILED(res)) {
  1963. D3D12_INTERNAL_SetError(renderer, "Failed to create descriptor heap!", res);
  1964. D3D12_INTERNAL_DestroyDescriptorHeap(heap);
  1965. return NULL;
  1966. }
  1967. heap->handle = handle;
  1968. heap->heapType = type;
  1969. heap->maxDescriptors = descriptorCount;
  1970. heap->staging = staging;
  1971. heap->descriptorSize = ID3D12Device_GetDescriptorHandleIncrementSize(renderer->device, type);
  1972. D3D_CALL_RET(handle, GetCPUDescriptorHandleForHeapStart, &heap->descriptorHeapCPUStart);
  1973. if (!staging) {
  1974. D3D_CALL_RET(handle, GetGPUDescriptorHandleForHeapStart, &heap->descriptorHeapGPUStart);
  1975. }
  1976. return heap;
  1977. }
  1978. static D3D12StagingDescriptorPool *D3D12_INTERNAL_CreateStagingDescriptorPool(
  1979. D3D12Renderer *renderer,
  1980. D3D12_DESCRIPTOR_HEAP_TYPE heapType
  1981. ) {
  1982. D3D12DescriptorHeap *heap = D3D12_INTERNAL_CreateDescriptorHeap(
  1983. renderer,
  1984. heapType,
  1985. STAGING_HEAP_DESCRIPTOR_COUNT,
  1986. true);
  1987. if (!heap) {
  1988. return NULL;
  1989. }
  1990. D3D12StagingDescriptorPool *pool = (D3D12StagingDescriptorPool *)SDL_calloc(1, sizeof(D3D12StagingDescriptorPool));
  1991. pool->heapCount = 1;
  1992. pool->heaps = (D3D12DescriptorHeap **)SDL_malloc(sizeof(D3D12DescriptorHeap *));
  1993. pool->heaps[0] = heap;
  1994. pool->freeDescriptorCapacity = STAGING_HEAP_DESCRIPTOR_COUNT;
  1995. pool->freeDescriptorCount = STAGING_HEAP_DESCRIPTOR_COUNT;
  1996. pool->freeDescriptors = (D3D12StagingDescriptor *)SDL_malloc(STAGING_HEAP_DESCRIPTOR_COUNT * sizeof(D3D12StagingDescriptor));
  1997. for (Uint32 i = 0; i < STAGING_HEAP_DESCRIPTOR_COUNT; i += 1) {
  1998. pool->freeDescriptors[i].pool = pool;
  1999. pool->freeDescriptors[i].heap = heap;
  2000. pool->freeDescriptors[i].cpuHandleIndex = i;
  2001. pool->freeDescriptors[i].cpuHandle.ptr = heap->descriptorHeapCPUStart.ptr + (i * heap->descriptorSize);
  2002. }
  2003. pool->lock = SDL_CreateMutex();
  2004. return pool;
  2005. }
  2006. /* If the pool is empty, we need to refill it! */
  2007. static bool D3D12_INTERNAL_ExpandStagingDescriptorPool(
  2008. D3D12Renderer *renderer,
  2009. D3D12StagingDescriptorPool *pool
  2010. ) {
  2011. D3D12DescriptorHeap *heap = D3D12_INTERNAL_CreateDescriptorHeap(
  2012. renderer,
  2013. pool->heaps[0]->heapType,
  2014. STAGING_HEAP_DESCRIPTOR_COUNT,
  2015. true);
  2016. if (!heap) {
  2017. return false;
  2018. }
  2019. pool->heapCount += 1;
  2020. pool->heaps = (D3D12DescriptorHeap **)SDL_realloc(pool->heaps, pool->heapCount * sizeof(D3D12DescriptorHeap *));
  2021. pool->heaps[pool->heapCount - 1] = heap;
  2022. pool->freeDescriptorCapacity += STAGING_HEAP_DESCRIPTOR_COUNT;
  2023. pool->freeDescriptorCount += STAGING_HEAP_DESCRIPTOR_COUNT;
  2024. pool->freeDescriptors = (D3D12StagingDescriptor *)SDL_realloc(pool->freeDescriptors, pool->freeDescriptorCapacity * sizeof(D3D12StagingDescriptor));
  2025. for (Uint32 i = 0; i < STAGING_HEAP_DESCRIPTOR_COUNT; i += 1) {
  2026. pool->freeDescriptors[i].pool = pool;
  2027. pool->freeDescriptors[i].heap = heap;
  2028. pool->freeDescriptors[i].cpuHandleIndex = i;
  2029. pool->freeDescriptors[i].cpuHandle.ptr = heap->descriptorHeapCPUStart.ptr + (i * heap->descriptorSize);
  2030. }
  2031. return true;
  2032. }
  2033. static void D3D12_INTERNAL_TrackGPUDescriptorHeap(
  2034. D3D12CommandBuffer *commandBuffer,
  2035. D3D12DescriptorHeap *descriptorHeap)
  2036. {
  2037. Uint32 i;
  2038. for (i = 0; i < commandBuffer->usedDescriptorHeapCount; i += 1) {
  2039. if (commandBuffer->usedDescriptorHeaps[i] == descriptorHeap) {
  2040. return;
  2041. }
  2042. }
  2043. if (commandBuffer->usedDescriptorHeapCount == commandBuffer->usedDescriptorHeapCapacity) {
  2044. commandBuffer->usedDescriptorHeapCapacity += 1;
  2045. commandBuffer->usedDescriptorHeaps = (D3D12DescriptorHeap **)SDL_realloc(
  2046. commandBuffer->usedDescriptorHeaps,
  2047. commandBuffer->usedDescriptorHeapCapacity * sizeof(D3D12DescriptorHeap *));
  2048. }
  2049. commandBuffer->usedDescriptorHeaps[commandBuffer->usedDescriptorHeapCount] = descriptorHeap;
  2050. commandBuffer->usedDescriptorHeapCount += 1;
  2051. }
  2052. static D3D12DescriptorHeap *D3D12_INTERNAL_AcquireGPUDescriptorHeapFromPool(
  2053. D3D12CommandBuffer *commandBuffer,
  2054. D3D12_DESCRIPTOR_HEAP_TYPE descriptorHeapType)
  2055. {
  2056. D3D12DescriptorHeap *result;
  2057. D3D12Renderer *renderer = commandBuffer->renderer;
  2058. D3D12GPUDescriptorHeapPool *pool = &renderer->gpuDescriptorHeapPools[descriptorHeapType];
  2059. SDL_LockMutex(pool->lock);
  2060. if (pool->count > 0) {
  2061. result = pool->heaps[pool->count - 1];
  2062. pool->count -= 1;
  2063. } else {
  2064. result = D3D12_INTERNAL_CreateDescriptorHeap(
  2065. renderer,
  2066. descriptorHeapType,
  2067. descriptorHeapType == D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV ? VIEW_GPU_DESCRIPTOR_COUNT : SAMPLER_GPU_DESCRIPTOR_COUNT,
  2068. false);
  2069. }
  2070. SDL_UnlockMutex(pool->lock);
  2071. D3D12_INTERNAL_TrackGPUDescriptorHeap(commandBuffer, result);
  2072. return result;
  2073. }
  2074. static void D3D12_INTERNAL_ReturnGPUDescriptorHeapToPool(
  2075. D3D12Renderer *renderer,
  2076. D3D12DescriptorHeap *heap)
  2077. {
  2078. if (heap == NULL) {
  2079. return;
  2080. }
  2081. D3D12GPUDescriptorHeapPool *pool = &renderer->gpuDescriptorHeapPools[heap->heapType];
  2082. heap->currentDescriptorIndex = 0;
  2083. SDL_LockMutex(pool->lock);
  2084. if (pool->count >= pool->capacity) {
  2085. pool->capacity *= 2;
  2086. pool->heaps = (D3D12DescriptorHeap **)SDL_realloc(
  2087. pool->heaps,
  2088. pool->capacity * sizeof(D3D12DescriptorHeap *));
  2089. }
  2090. pool->heaps[pool->count] = heap;
  2091. pool->count += 1;
  2092. SDL_UnlockMutex(pool->lock);
  2093. }
  2094. /*
  2095. * The root signature lets us define "root parameters" which are essentially bind points for resources.
  2096. * These let us define the register ranges as well as the register "space".
  2097. * The register space is akin to the descriptor set index in Vulkan, which allows us to group resources
  2098. * by stage so that the registers from the vertex and fragment shaders don't clobber each other.
  2099. *
  2100. * Most of our root parameters are implemented as "descriptor tables" so we can
  2101. * copy and then point to contiguous descriptor regions.
  2102. * Uniform buffers are the exception - these have to be implemented as raw "root descriptors" so
  2103. * that we can dynamically update the address that the constant buffer view points to.
  2104. *
  2105. * The root signature has a maximum size of 64 DWORDs.
  2106. * A descriptor table uses 1 DWORD.
  2107. * A root descriptor uses 2 DWORDS.
  2108. * This means our biggest root signature uses 24 DWORDs total, well under the limit.
  2109. *
  2110. * The root parameter indices are created dynamically and stored in the D3D12GraphicsRootSignature struct.
  2111. */
  2112. static D3D12GraphicsRootSignature *D3D12_INTERNAL_CreateGraphicsRootSignature(
  2113. D3D12Renderer *renderer,
  2114. D3D12Shader *vertexShader,
  2115. D3D12Shader *fragmentShader)
  2116. {
  2117. // FIXME: I think the max can be smaller...
  2118. D3D12_ROOT_PARAMETER rootParameters[MAX_ROOT_SIGNATURE_PARAMETERS];
  2119. D3D12_DESCRIPTOR_RANGE descriptorRanges[MAX_ROOT_SIGNATURE_PARAMETERS];
  2120. Uint32 parameterCount = 0;
  2121. Uint32 rangeCount = 0;
  2122. D3D12_DESCRIPTOR_RANGE descriptorRange;
  2123. D3D12_ROOT_PARAMETER rootParameter;
  2124. D3D12GraphicsRootSignature *d3d12GraphicsRootSignature =
  2125. (D3D12GraphicsRootSignature *)SDL_calloc(1, sizeof(D3D12GraphicsRootSignature));
  2126. if (!d3d12GraphicsRootSignature) {
  2127. return NULL;
  2128. }
  2129. SDL_zeroa(rootParameters);
  2130. SDL_zeroa(descriptorRanges);
  2131. SDL_zero(rootParameter);
  2132. d3d12GraphicsRootSignature->vertexSamplerRootIndex = -1;
  2133. d3d12GraphicsRootSignature->vertexSamplerTextureRootIndex = -1;
  2134. d3d12GraphicsRootSignature->vertexStorageTextureRootIndex = -1;
  2135. d3d12GraphicsRootSignature->vertexStorageBufferRootIndex = -1;
  2136. d3d12GraphicsRootSignature->fragmentSamplerRootIndex = -1;
  2137. d3d12GraphicsRootSignature->fragmentSamplerTextureRootIndex = -1;
  2138. d3d12GraphicsRootSignature->fragmentStorageTextureRootIndex = -1;
  2139. d3d12GraphicsRootSignature->fragmentStorageBufferRootIndex = -1;
  2140. for (Uint32 i = 0; i < MAX_UNIFORM_BUFFERS_PER_STAGE; i += 1) {
  2141. d3d12GraphicsRootSignature->vertexUniformBufferRootIndex[i] = -1;
  2142. d3d12GraphicsRootSignature->fragmentUniformBufferRootIndex[i] = -1;
  2143. }
  2144. if (vertexShader->num_samplers > 0) {
  2145. // Vertex Samplers
  2146. descriptorRange.RangeType = D3D12_DESCRIPTOR_RANGE_TYPE_SAMPLER;
  2147. descriptorRange.NumDescriptors = vertexShader->num_samplers;
  2148. descriptorRange.BaseShaderRegister = 0;
  2149. descriptorRange.RegisterSpace = 0;
  2150. descriptorRange.OffsetInDescriptorsFromTableStart = D3D12_DESCRIPTOR_RANGE_OFFSET_APPEND;
  2151. descriptorRanges[rangeCount] = descriptorRange;
  2152. rootParameter.ParameterType = D3D12_ROOT_PARAMETER_TYPE_DESCRIPTOR_TABLE;
  2153. rootParameter.DescriptorTable.NumDescriptorRanges = 1;
  2154. rootParameter.DescriptorTable.pDescriptorRanges = &descriptorRanges[rangeCount];
  2155. rootParameter.ShaderVisibility = D3D12_SHADER_VISIBILITY_VERTEX;
  2156. rootParameters[parameterCount] = rootParameter;
  2157. d3d12GraphicsRootSignature->vertexSamplerRootIndex = parameterCount;
  2158. rangeCount += 1;
  2159. parameterCount += 1;
  2160. descriptorRange.RangeType = D3D12_DESCRIPTOR_RANGE_TYPE_SRV;
  2161. descriptorRange.NumDescriptors = vertexShader->num_samplers;
  2162. descriptorRange.BaseShaderRegister = 0;
  2163. descriptorRange.RegisterSpace = 0;
  2164. descriptorRange.OffsetInDescriptorsFromTableStart = D3D12_DESCRIPTOR_RANGE_OFFSET_APPEND;
  2165. descriptorRanges[rangeCount] = descriptorRange;
  2166. rootParameter.ParameterType = D3D12_ROOT_PARAMETER_TYPE_DESCRIPTOR_TABLE;
  2167. rootParameter.DescriptorTable.NumDescriptorRanges = 1;
  2168. rootParameter.DescriptorTable.pDescriptorRanges = &descriptorRanges[rangeCount];
  2169. rootParameter.ShaderVisibility = D3D12_SHADER_VISIBILITY_VERTEX;
  2170. rootParameters[parameterCount] = rootParameter;
  2171. d3d12GraphicsRootSignature->vertexSamplerTextureRootIndex = parameterCount;
  2172. rangeCount += 1;
  2173. parameterCount += 1;
  2174. }
  2175. if (vertexShader->numStorageTextures) {
  2176. // Vertex storage textures
  2177. descriptorRange.RangeType = D3D12_DESCRIPTOR_RANGE_TYPE_SRV;
  2178. descriptorRange.NumDescriptors = vertexShader->numStorageTextures;
  2179. descriptorRange.BaseShaderRegister = vertexShader->num_samplers;
  2180. descriptorRange.RegisterSpace = 0;
  2181. descriptorRange.OffsetInDescriptorsFromTableStart = D3D12_DESCRIPTOR_RANGE_OFFSET_APPEND;
  2182. descriptorRanges[rangeCount] = descriptorRange;
  2183. rootParameter.ParameterType = D3D12_ROOT_PARAMETER_TYPE_DESCRIPTOR_TABLE;
  2184. rootParameter.DescriptorTable.NumDescriptorRanges = 1;
  2185. rootParameter.DescriptorTable.pDescriptorRanges = &descriptorRanges[rangeCount];
  2186. rootParameter.ShaderVisibility = D3D12_SHADER_VISIBILITY_VERTEX;
  2187. rootParameters[parameterCount] = rootParameter;
  2188. d3d12GraphicsRootSignature->vertexStorageTextureRootIndex = parameterCount;
  2189. rangeCount += 1;
  2190. parameterCount += 1;
  2191. }
  2192. if (vertexShader->numStorageBuffers) {
  2193. // Vertex storage buffers
  2194. descriptorRange.RangeType = D3D12_DESCRIPTOR_RANGE_TYPE_SRV;
  2195. descriptorRange.NumDescriptors = vertexShader->numStorageBuffers;
  2196. descriptorRange.BaseShaderRegister = vertexShader->num_samplers + vertexShader->numStorageTextures;
  2197. descriptorRange.RegisterSpace = 0;
  2198. descriptorRange.OffsetInDescriptorsFromTableStart = D3D12_DESCRIPTOR_RANGE_OFFSET_APPEND;
  2199. descriptorRanges[rangeCount] = descriptorRange;
  2200. rootParameter.ParameterType = D3D12_ROOT_PARAMETER_TYPE_DESCRIPTOR_TABLE;
  2201. rootParameter.DescriptorTable.NumDescriptorRanges = 1;
  2202. rootParameter.DescriptorTable.pDescriptorRanges = &descriptorRanges[rangeCount];
  2203. rootParameter.ShaderVisibility = D3D12_SHADER_VISIBILITY_VERTEX;
  2204. rootParameters[parameterCount] = rootParameter;
  2205. d3d12GraphicsRootSignature->vertexStorageBufferRootIndex = parameterCount;
  2206. rangeCount += 1;
  2207. parameterCount += 1;
  2208. }
  2209. // Vertex Uniforms
  2210. for (Uint32 i = 0; i < vertexShader->numUniformBuffers; i += 1) {
  2211. rootParameter.ParameterType = D3D12_ROOT_PARAMETER_TYPE_CBV;
  2212. rootParameter.Descriptor.ShaderRegister = i;
  2213. rootParameter.Descriptor.RegisterSpace = 1;
  2214. rootParameter.ShaderVisibility = D3D12_SHADER_VISIBILITY_VERTEX;
  2215. rootParameters[parameterCount] = rootParameter;
  2216. d3d12GraphicsRootSignature->vertexUniformBufferRootIndex[i] = parameterCount;
  2217. parameterCount += 1;
  2218. }
  2219. if (fragmentShader->num_samplers) {
  2220. // Fragment Samplers
  2221. descriptorRange.RangeType = D3D12_DESCRIPTOR_RANGE_TYPE_SAMPLER;
  2222. descriptorRange.NumDescriptors = fragmentShader->num_samplers;
  2223. descriptorRange.BaseShaderRegister = 0;
  2224. descriptorRange.RegisterSpace = 2;
  2225. descriptorRange.OffsetInDescriptorsFromTableStart = D3D12_DESCRIPTOR_RANGE_OFFSET_APPEND;
  2226. descriptorRanges[rangeCount] = descriptorRange;
  2227. rootParameter.ParameterType = D3D12_ROOT_PARAMETER_TYPE_DESCRIPTOR_TABLE;
  2228. rootParameter.DescriptorTable.NumDescriptorRanges = 1;
  2229. rootParameter.DescriptorTable.pDescriptorRanges = &descriptorRanges[rangeCount];
  2230. rootParameter.ShaderVisibility = D3D12_SHADER_VISIBILITY_PIXEL;
  2231. rootParameters[parameterCount] = rootParameter;
  2232. d3d12GraphicsRootSignature->fragmentSamplerRootIndex = parameterCount;
  2233. rangeCount += 1;
  2234. parameterCount += 1;
  2235. descriptorRange.RangeType = D3D12_DESCRIPTOR_RANGE_TYPE_SRV;
  2236. descriptorRange.NumDescriptors = fragmentShader->num_samplers;
  2237. descriptorRange.BaseShaderRegister = 0;
  2238. descriptorRange.RegisterSpace = 2;
  2239. descriptorRange.OffsetInDescriptorsFromTableStart = D3D12_DESCRIPTOR_RANGE_OFFSET_APPEND;
  2240. descriptorRanges[rangeCount] = descriptorRange;
  2241. rootParameter.ParameterType = D3D12_ROOT_PARAMETER_TYPE_DESCRIPTOR_TABLE;
  2242. rootParameter.DescriptorTable.NumDescriptorRanges = 1;
  2243. rootParameter.DescriptorTable.pDescriptorRanges = &descriptorRanges[rangeCount];
  2244. rootParameter.ShaderVisibility = D3D12_SHADER_VISIBILITY_PIXEL;
  2245. rootParameters[parameterCount] = rootParameter;
  2246. d3d12GraphicsRootSignature->fragmentSamplerTextureRootIndex = parameterCount;
  2247. rangeCount += 1;
  2248. parameterCount += 1;
  2249. }
  2250. if (fragmentShader->numStorageTextures) {
  2251. // Fragment Storage Textures
  2252. descriptorRange.RangeType = D3D12_DESCRIPTOR_RANGE_TYPE_SRV;
  2253. descriptorRange.NumDescriptors = fragmentShader->numStorageTextures;
  2254. descriptorRange.BaseShaderRegister = fragmentShader->num_samplers;
  2255. descriptorRange.RegisterSpace = 2;
  2256. descriptorRange.OffsetInDescriptorsFromTableStart = D3D12_DESCRIPTOR_RANGE_OFFSET_APPEND;
  2257. descriptorRanges[rangeCount] = descriptorRange;
  2258. rootParameter.ParameterType = D3D12_ROOT_PARAMETER_TYPE_DESCRIPTOR_TABLE;
  2259. rootParameter.DescriptorTable.NumDescriptorRanges = 1;
  2260. rootParameter.DescriptorTable.pDescriptorRanges = &descriptorRanges[rangeCount];
  2261. rootParameter.ShaderVisibility = D3D12_SHADER_VISIBILITY_PIXEL;
  2262. rootParameters[parameterCount] = rootParameter;
  2263. d3d12GraphicsRootSignature->fragmentStorageTextureRootIndex = parameterCount;
  2264. rangeCount += 1;
  2265. parameterCount += 1;
  2266. }
  2267. if (fragmentShader->numStorageBuffers) {
  2268. // Fragment Storage Buffers
  2269. descriptorRange.RangeType = D3D12_DESCRIPTOR_RANGE_TYPE_SRV;
  2270. descriptorRange.NumDescriptors = fragmentShader->numStorageBuffers;
  2271. descriptorRange.BaseShaderRegister = fragmentShader->num_samplers + fragmentShader->numStorageTextures;
  2272. descriptorRange.RegisterSpace = 2;
  2273. descriptorRange.OffsetInDescriptorsFromTableStart = D3D12_DESCRIPTOR_RANGE_OFFSET_APPEND;
  2274. descriptorRanges[rangeCount] = descriptorRange;
  2275. rootParameter.ParameterType = D3D12_ROOT_PARAMETER_TYPE_DESCRIPTOR_TABLE;
  2276. rootParameter.DescriptorTable.NumDescriptorRanges = 1;
  2277. rootParameter.DescriptorTable.pDescriptorRanges = &descriptorRanges[rangeCount];
  2278. rootParameter.ShaderVisibility = D3D12_SHADER_VISIBILITY_PIXEL;
  2279. rootParameters[parameterCount] = rootParameter;
  2280. d3d12GraphicsRootSignature->fragmentStorageBufferRootIndex = parameterCount;
  2281. rangeCount += 1;
  2282. parameterCount += 1;
  2283. }
  2284. // Fragment Uniforms
  2285. for (Uint32 i = 0; i < fragmentShader->numUniformBuffers; i += 1) {
  2286. rootParameter.ParameterType = D3D12_ROOT_PARAMETER_TYPE_CBV;
  2287. rootParameter.Descriptor.ShaderRegister = i;
  2288. rootParameter.Descriptor.RegisterSpace = 3;
  2289. rootParameter.ShaderVisibility = D3D12_SHADER_VISIBILITY_PIXEL;
  2290. rootParameters[parameterCount] = rootParameter;
  2291. d3d12GraphicsRootSignature->fragmentUniformBufferRootIndex[i] = parameterCount;
  2292. parameterCount += 1;
  2293. }
  2294. // FIXME: shouldn't have to assert here
  2295. SDL_assert(parameterCount <= MAX_ROOT_SIGNATURE_PARAMETERS);
  2296. SDL_assert(rangeCount <= MAX_ROOT_SIGNATURE_PARAMETERS);
  2297. // Create the root signature description
  2298. D3D12_ROOT_SIGNATURE_DESC rootSignatureDesc;
  2299. rootSignatureDesc.NumParameters = parameterCount;
  2300. rootSignatureDesc.pParameters = rootParameters;
  2301. rootSignatureDesc.NumStaticSamplers = 0;
  2302. rootSignatureDesc.pStaticSamplers = NULL;
  2303. rootSignatureDesc.Flags = D3D12_ROOT_SIGNATURE_FLAG_ALLOW_INPUT_ASSEMBLER_INPUT_LAYOUT;
  2304. // Serialize the root signature
  2305. ID3DBlob *serializedRootSignature;
  2306. ID3DBlob *errorBlob;
  2307. HRESULT res = renderer->pD3D12SerializeRootSignature(&rootSignatureDesc, D3D_ROOT_SIGNATURE_VERSION_1, &serializedRootSignature, &errorBlob);
  2308. if (FAILED(res)) {
  2309. if (errorBlob) {
  2310. SET_ERROR("Failed to serialize RootSignature: %s", (const char *)ID3D10Blob_GetBufferPointer(errorBlob));
  2311. ID3D10Blob_Release(errorBlob);
  2312. }
  2313. D3D12_INTERNAL_DestroyGraphicsRootSignature(d3d12GraphicsRootSignature);
  2314. return NULL;
  2315. }
  2316. // Create the root signature
  2317. ID3D12RootSignature *rootSignature;
  2318. res = ID3D12Device_CreateRootSignature(
  2319. renderer->device,
  2320. 0,
  2321. ID3D10Blob_GetBufferPointer(serializedRootSignature),
  2322. ID3D10Blob_GetBufferSize(serializedRootSignature),
  2323. D3D_GUID(D3D_IID_ID3D12RootSignature),
  2324. (void **)&rootSignature);
  2325. if (FAILED(res)) {
  2326. if (errorBlob) {
  2327. SET_ERROR("Failed to create RootSignature: %s", (const char *)ID3D10Blob_GetBufferPointer(errorBlob));
  2328. ID3D10Blob_Release(errorBlob);
  2329. }
  2330. D3D12_INTERNAL_DestroyGraphicsRootSignature(d3d12GraphicsRootSignature);
  2331. return NULL;
  2332. }
  2333. d3d12GraphicsRootSignature->handle = rootSignature;
  2334. return d3d12GraphicsRootSignature;
  2335. }
  2336. static bool D3D12_INTERNAL_IsValidShaderBytecode(
  2337. const Uint8 *code,
  2338. size_t codeSize)
  2339. {
  2340. // Both DXIL and DXBC bytecode have a 4 byte header containing `DXBC`.
  2341. if (codeSize < 4 || code == NULL) {
  2342. return false;
  2343. }
  2344. return SDL_memcmp(code, "DXBC", 4) == 0;
  2345. }
  2346. static bool D3D12_INTERNAL_CreateShaderBytecode(
  2347. D3D12Renderer *renderer,
  2348. const Uint8 *code,
  2349. size_t codeSize,
  2350. SDL_GPUShaderFormat format,
  2351. void **pBytecode,
  2352. size_t *pBytecodeSize)
  2353. {
  2354. if (!D3D12_INTERNAL_IsValidShaderBytecode(code, codeSize)) {
  2355. if (format == SDL_GPU_SHADERFORMAT_DXBC) {
  2356. SET_STRING_ERROR_AND_RETURN("The provided shader code is not valid DXBC!", false);
  2357. }
  2358. SET_STRING_ERROR_AND_RETURN("The provided shader code is not valid DXIL!", false);
  2359. }
  2360. if (pBytecode != NULL) {
  2361. *pBytecode = SDL_malloc(codeSize);
  2362. if (!*pBytecode) {
  2363. return false;
  2364. }
  2365. SDL_memcpy(*pBytecode, code, codeSize);
  2366. *pBytecodeSize = codeSize;
  2367. }
  2368. return true;
  2369. }
  2370. static D3D12ComputeRootSignature *D3D12_INTERNAL_CreateComputeRootSignature(
  2371. D3D12Renderer *renderer,
  2372. const SDL_GPUComputePipelineCreateInfo *createInfo)
  2373. {
  2374. // FIXME: I think the max can be smaller...
  2375. D3D12_ROOT_PARAMETER rootParameters[MAX_ROOT_SIGNATURE_PARAMETERS];
  2376. D3D12_DESCRIPTOR_RANGE descriptorRanges[MAX_ROOT_SIGNATURE_PARAMETERS];
  2377. Uint32 parameterCount = 0;
  2378. Uint32 rangeCount = 0;
  2379. D3D12_DESCRIPTOR_RANGE descriptorRange;
  2380. D3D12_ROOT_PARAMETER rootParameter;
  2381. D3D12ComputeRootSignature *d3d12ComputeRootSignature =
  2382. (D3D12ComputeRootSignature *)SDL_calloc(1, sizeof(D3D12ComputeRootSignature));
  2383. if (!d3d12ComputeRootSignature) {
  2384. return NULL;
  2385. }
  2386. SDL_zeroa(rootParameters);
  2387. SDL_zeroa(descriptorRanges);
  2388. SDL_zero(rootParameter);
  2389. d3d12ComputeRootSignature->samplerRootIndex = -1;
  2390. d3d12ComputeRootSignature->samplerTextureRootIndex = -1;
  2391. d3d12ComputeRootSignature->readOnlyStorageTextureRootIndex = -1;
  2392. d3d12ComputeRootSignature->readOnlyStorageBufferRootIndex = -1;
  2393. d3d12ComputeRootSignature->readWriteStorageTextureRootIndex = -1;
  2394. d3d12ComputeRootSignature->readWriteStorageBufferRootIndex = -1;
  2395. for (Uint32 i = 0; i < MAX_UNIFORM_BUFFERS_PER_STAGE; i += 1) {
  2396. d3d12ComputeRootSignature->uniformBufferRootIndex[i] = -1;
  2397. }
  2398. if (createInfo->num_samplers) {
  2399. descriptorRange.RangeType = D3D12_DESCRIPTOR_RANGE_TYPE_SAMPLER;
  2400. descriptorRange.NumDescriptors = createInfo->num_samplers;
  2401. descriptorRange.BaseShaderRegister = 0;
  2402. descriptorRange.RegisterSpace = 0;
  2403. descriptorRange.OffsetInDescriptorsFromTableStart = D3D12_DESCRIPTOR_RANGE_OFFSET_APPEND;
  2404. descriptorRanges[rangeCount] = descriptorRange;
  2405. rootParameter.ParameterType = D3D12_ROOT_PARAMETER_TYPE_DESCRIPTOR_TABLE;
  2406. rootParameter.DescriptorTable.NumDescriptorRanges = 1;
  2407. rootParameter.DescriptorTable.pDescriptorRanges = &descriptorRanges[rangeCount];
  2408. rootParameter.ShaderVisibility = D3D12_SHADER_VISIBILITY_ALL; // ALL is used for compute
  2409. rootParameters[parameterCount] = rootParameter;
  2410. d3d12ComputeRootSignature->samplerRootIndex = parameterCount;
  2411. rangeCount += 1;
  2412. parameterCount += 1;
  2413. descriptorRange.RangeType = D3D12_DESCRIPTOR_RANGE_TYPE_SRV;
  2414. descriptorRange.NumDescriptors = createInfo->num_samplers;
  2415. descriptorRange.BaseShaderRegister = 0;
  2416. descriptorRange.RegisterSpace = 0;
  2417. descriptorRange.OffsetInDescriptorsFromTableStart = D3D12_DESCRIPTOR_RANGE_OFFSET_APPEND;
  2418. descriptorRanges[rangeCount] = descriptorRange;
  2419. rootParameter.ParameterType = D3D12_ROOT_PARAMETER_TYPE_DESCRIPTOR_TABLE;
  2420. rootParameter.DescriptorTable.NumDescriptorRanges = 1;
  2421. rootParameter.DescriptorTable.pDescriptorRanges = &descriptorRanges[rangeCount];
  2422. rootParameter.ShaderVisibility = D3D12_SHADER_VISIBILITY_ALL; // ALL is used for compute
  2423. rootParameters[parameterCount] = rootParameter;
  2424. d3d12ComputeRootSignature->samplerTextureRootIndex = parameterCount;
  2425. rangeCount += 1;
  2426. parameterCount += 1;
  2427. }
  2428. if (createInfo->num_readonly_storage_textures) {
  2429. descriptorRange.RangeType = D3D12_DESCRIPTOR_RANGE_TYPE_SRV;
  2430. descriptorRange.NumDescriptors = createInfo->num_readonly_storage_textures;
  2431. descriptorRange.BaseShaderRegister = createInfo->num_samplers;
  2432. descriptorRange.RegisterSpace = 0;
  2433. descriptorRange.OffsetInDescriptorsFromTableStart = D3D12_DESCRIPTOR_RANGE_OFFSET_APPEND;
  2434. descriptorRanges[rangeCount] = descriptorRange;
  2435. rootParameter.ParameterType = D3D12_ROOT_PARAMETER_TYPE_DESCRIPTOR_TABLE;
  2436. rootParameter.DescriptorTable.NumDescriptorRanges = 1;
  2437. rootParameter.DescriptorTable.pDescriptorRanges = &descriptorRanges[rangeCount];
  2438. rootParameter.ShaderVisibility = D3D12_SHADER_VISIBILITY_ALL; // ALL is used for compute
  2439. rootParameters[parameterCount] = rootParameter;
  2440. d3d12ComputeRootSignature->readOnlyStorageTextureRootIndex = parameterCount;
  2441. rangeCount += 1;
  2442. parameterCount += 1;
  2443. }
  2444. if (createInfo->num_readonly_storage_buffers) {
  2445. descriptorRange.RangeType = D3D12_DESCRIPTOR_RANGE_TYPE_SRV;
  2446. descriptorRange.NumDescriptors = createInfo->num_readonly_storage_buffers;
  2447. descriptorRange.BaseShaderRegister = createInfo->num_samplers + createInfo->num_readonly_storage_textures;
  2448. descriptorRange.RegisterSpace = 0;
  2449. descriptorRange.OffsetInDescriptorsFromTableStart = D3D12_DESCRIPTOR_RANGE_OFFSET_APPEND;
  2450. descriptorRanges[rangeCount] = descriptorRange;
  2451. rootParameter.ParameterType = D3D12_ROOT_PARAMETER_TYPE_DESCRIPTOR_TABLE;
  2452. rootParameter.DescriptorTable.NumDescriptorRanges = 1;
  2453. rootParameter.DescriptorTable.pDescriptorRanges = &descriptorRanges[rangeCount];
  2454. rootParameter.ShaderVisibility = D3D12_SHADER_VISIBILITY_ALL; // ALL is used for compute
  2455. rootParameters[parameterCount] = rootParameter;
  2456. d3d12ComputeRootSignature->readOnlyStorageBufferRootIndex = parameterCount;
  2457. rangeCount += 1;
  2458. parameterCount += 1;
  2459. }
  2460. if (createInfo->num_readwrite_storage_textures) {
  2461. descriptorRange.RangeType = D3D12_DESCRIPTOR_RANGE_TYPE_UAV;
  2462. descriptorRange.NumDescriptors = createInfo->num_readwrite_storage_textures;
  2463. descriptorRange.BaseShaderRegister = 0;
  2464. descriptorRange.RegisterSpace = 1;
  2465. descriptorRange.OffsetInDescriptorsFromTableStart = D3D12_DESCRIPTOR_RANGE_OFFSET_APPEND;
  2466. descriptorRanges[rangeCount] = descriptorRange;
  2467. rootParameter.ParameterType = D3D12_ROOT_PARAMETER_TYPE_DESCRIPTOR_TABLE;
  2468. rootParameter.DescriptorTable.NumDescriptorRanges = 1;
  2469. rootParameter.DescriptorTable.pDescriptorRanges = &descriptorRanges[rangeCount];
  2470. rootParameter.ShaderVisibility = D3D12_SHADER_VISIBILITY_ALL; // ALL is used for compute
  2471. rootParameters[parameterCount] = rootParameter;
  2472. d3d12ComputeRootSignature->readWriteStorageTextureRootIndex = parameterCount;
  2473. rangeCount += 1;
  2474. parameterCount += 1;
  2475. }
  2476. if (createInfo->num_readwrite_storage_buffers) {
  2477. descriptorRange.RangeType = D3D12_DESCRIPTOR_RANGE_TYPE_UAV;
  2478. descriptorRange.NumDescriptors = createInfo->num_readwrite_storage_buffers;
  2479. descriptorRange.BaseShaderRegister = createInfo->num_readwrite_storage_textures;
  2480. descriptorRange.RegisterSpace = 1;
  2481. descriptorRange.OffsetInDescriptorsFromTableStart = D3D12_DESCRIPTOR_RANGE_OFFSET_APPEND;
  2482. descriptorRanges[rangeCount] = descriptorRange;
  2483. rootParameter.ParameterType = D3D12_ROOT_PARAMETER_TYPE_DESCRIPTOR_TABLE;
  2484. rootParameter.DescriptorTable.NumDescriptorRanges = 1;
  2485. rootParameter.DescriptorTable.pDescriptorRanges = &descriptorRanges[rangeCount];
  2486. rootParameter.ShaderVisibility = D3D12_SHADER_VISIBILITY_ALL; // ALL is used for compute
  2487. rootParameters[parameterCount] = rootParameter;
  2488. d3d12ComputeRootSignature->readWriteStorageBufferRootIndex = parameterCount;
  2489. rangeCount += 1;
  2490. parameterCount += 1;
  2491. }
  2492. for (Uint32 i = 0; i < createInfo->num_uniform_buffers; i += 1) {
  2493. rootParameter.ParameterType = D3D12_ROOT_PARAMETER_TYPE_CBV;
  2494. rootParameter.Descriptor.ShaderRegister = i;
  2495. rootParameter.Descriptor.RegisterSpace = 2;
  2496. rootParameter.ShaderVisibility = D3D12_SHADER_VISIBILITY_ALL; // ALL is used for compute
  2497. rootParameters[parameterCount] = rootParameter;
  2498. d3d12ComputeRootSignature->uniformBufferRootIndex[i] = parameterCount;
  2499. parameterCount += 1;
  2500. }
  2501. D3D12_ROOT_SIGNATURE_DESC rootSignatureDesc;
  2502. rootSignatureDesc.NumParameters = parameterCount;
  2503. rootSignatureDesc.pParameters = rootParameters;
  2504. rootSignatureDesc.NumStaticSamplers = 0;
  2505. rootSignatureDesc.pStaticSamplers = NULL;
  2506. rootSignatureDesc.Flags = (D3D12_ROOT_SIGNATURE_FLAGS)0;
  2507. ID3DBlob *serializedRootSignature;
  2508. ID3DBlob *errorBlob;
  2509. HRESULT res = renderer->pD3D12SerializeRootSignature(
  2510. &rootSignatureDesc,
  2511. D3D_ROOT_SIGNATURE_VERSION_1,
  2512. &serializedRootSignature,
  2513. &errorBlob);
  2514. if (FAILED(res)) {
  2515. if (errorBlob) {
  2516. SET_ERROR("Failed to serialize RootSignature: %s", (const char *)ID3D10Blob_GetBufferPointer(errorBlob));
  2517. ID3D10Blob_Release(errorBlob);
  2518. }
  2519. D3D12_INTERNAL_DestroyComputeRootSignature(d3d12ComputeRootSignature);
  2520. return NULL;
  2521. }
  2522. ID3D12RootSignature *rootSignature;
  2523. res = ID3D12Device_CreateRootSignature(
  2524. renderer->device,
  2525. 0,
  2526. ID3D10Blob_GetBufferPointer(serializedRootSignature),
  2527. ID3D10Blob_GetBufferSize(serializedRootSignature),
  2528. D3D_GUID(D3D_IID_ID3D12RootSignature),
  2529. (void **)&rootSignature);
  2530. if (FAILED(res)) {
  2531. if (errorBlob) {
  2532. SET_ERROR("Failed to create RootSignature: %s", (const char *)ID3D10Blob_GetBufferPointer(errorBlob));
  2533. ID3D10Blob_Release(errorBlob);
  2534. }
  2535. D3D12_INTERNAL_DestroyComputeRootSignature(d3d12ComputeRootSignature);
  2536. return NULL;
  2537. }
  2538. d3d12ComputeRootSignature->handle = rootSignature;
  2539. return d3d12ComputeRootSignature;
  2540. }
  2541. static SDL_GPUComputePipeline *D3D12_CreateComputePipeline(
  2542. SDL_GPURenderer *driverData,
  2543. const SDL_GPUComputePipelineCreateInfo *createinfo)
  2544. {
  2545. D3D12Renderer *renderer = (D3D12Renderer *)driverData;
  2546. ID3D12PipelineState *pipelineState;
  2547. if (!D3D12_INTERNAL_CreateShaderBytecode(
  2548. renderer,
  2549. createinfo->code,
  2550. createinfo->code_size,
  2551. createinfo->format,
  2552. NULL,
  2553. NULL)) {
  2554. return NULL;
  2555. }
  2556. D3D12ComputeRootSignature *rootSignature = D3D12_INTERNAL_CreateComputeRootSignature(
  2557. renderer,
  2558. createinfo);
  2559. if (rootSignature == NULL) {
  2560. SET_STRING_ERROR_AND_RETURN("Could not create root signature!", NULL);
  2561. }
  2562. D3D12_COMPUTE_PIPELINE_STATE_DESC pipelineDesc;
  2563. pipelineDesc.CS.pShaderBytecode = createinfo->code;
  2564. pipelineDesc.CS.BytecodeLength = createinfo->code_size;
  2565. pipelineDesc.pRootSignature = rootSignature->handle;
  2566. pipelineDesc.CachedPSO.CachedBlobSizeInBytes = 0;
  2567. pipelineDesc.CachedPSO.pCachedBlob = NULL;
  2568. pipelineDesc.Flags = D3D12_PIPELINE_STATE_FLAG_NONE;
  2569. pipelineDesc.NodeMask = 0;
  2570. HRESULT res = ID3D12Device_CreateComputePipelineState(
  2571. renderer->device,
  2572. &pipelineDesc,
  2573. D3D_GUID(D3D_IID_ID3D12PipelineState),
  2574. (void **)&pipelineState);
  2575. if (FAILED(res)) {
  2576. D3D12_INTERNAL_SetError(renderer, "Could not create compute pipeline state", res);
  2577. return NULL;
  2578. }
  2579. D3D12ComputePipeline *computePipeline =
  2580. (D3D12ComputePipeline *)SDL_calloc(1, sizeof(D3D12ComputePipeline));
  2581. if (!computePipeline) {
  2582. ID3D12PipelineState_Release(pipelineState);
  2583. return NULL;
  2584. }
  2585. computePipeline->pipelineState = pipelineState;
  2586. computePipeline->rootSignature = rootSignature;
  2587. computePipeline->header.numSamplers = createinfo->num_samplers;
  2588. computePipeline->header.numReadonlyStorageTextures = createinfo->num_readonly_storage_textures;
  2589. computePipeline->header.numReadonlyStorageBuffers = createinfo->num_readonly_storage_buffers;
  2590. computePipeline->header.numReadWriteStorageTextures = createinfo->num_readwrite_storage_textures;
  2591. computePipeline->header.numReadWriteStorageBuffers = createinfo->num_readwrite_storage_buffers;
  2592. computePipeline->header.numUniformBuffers = createinfo->num_uniform_buffers;
  2593. SDL_SetAtomicInt(&computePipeline->referenceCount, 0);
  2594. if (renderer->debug_mode && SDL_HasProperty(createinfo->props, SDL_PROP_GPU_COMPUTEPIPELINE_CREATE_NAME_STRING)) {
  2595. D3D12_INTERNAL_SetPipelineStateName(
  2596. renderer,
  2597. computePipeline->pipelineState,
  2598. SDL_GetStringProperty(createinfo->props, SDL_PROP_GPU_COMPUTEPIPELINE_CREATE_NAME_STRING, NULL));
  2599. }
  2600. return (SDL_GPUComputePipeline *)computePipeline;
  2601. }
  2602. static bool D3D12_INTERNAL_ConvertRasterizerState(SDL_GPURasterizerState rasterizerState, D3D12_RASTERIZER_DESC *desc)
  2603. {
  2604. if (!desc) {
  2605. return false;
  2606. }
  2607. desc->FillMode = SDLToD3D12_FillMode[rasterizerState.fill_mode];
  2608. desc->CullMode = SDLToD3D12_CullMode[rasterizerState.cull_mode];
  2609. switch (rasterizerState.front_face) {
  2610. case SDL_GPU_FRONTFACE_COUNTER_CLOCKWISE:
  2611. desc->FrontCounterClockwise = TRUE;
  2612. break;
  2613. case SDL_GPU_FRONTFACE_CLOCKWISE:
  2614. desc->FrontCounterClockwise = FALSE;
  2615. break;
  2616. default:
  2617. return false;
  2618. }
  2619. if (rasterizerState.enable_depth_bias) {
  2620. desc->DepthBias = SDL_lroundf(rasterizerState.depth_bias_constant_factor);
  2621. desc->DepthBiasClamp = rasterizerState.depth_bias_clamp;
  2622. desc->SlopeScaledDepthBias = rasterizerState.depth_bias_slope_factor;
  2623. } else {
  2624. desc->DepthBias = 0;
  2625. desc->DepthBiasClamp = 0.0f;
  2626. desc->SlopeScaledDepthBias = 0.0f;
  2627. }
  2628. desc->DepthClipEnable = rasterizerState.enable_depth_clip;
  2629. desc->MultisampleEnable = FALSE;
  2630. desc->AntialiasedLineEnable = FALSE;
  2631. desc->ForcedSampleCount = 0;
  2632. desc->ConservativeRaster = D3D12_CONSERVATIVE_RASTERIZATION_MODE_OFF;
  2633. return true;
  2634. }
  2635. static bool D3D12_INTERNAL_ConvertBlendState(
  2636. const SDL_GPUGraphicsPipelineCreateInfo *pipelineInfo,
  2637. D3D12_BLEND_DESC *blendDesc)
  2638. {
  2639. if (!blendDesc) {
  2640. return false;
  2641. }
  2642. SDL_zerop(blendDesc);
  2643. blendDesc->AlphaToCoverageEnable = pipelineInfo->multisample_state.enable_alpha_to_coverage;
  2644. blendDesc->IndependentBlendEnable = FALSE;
  2645. for (UINT i = 0; i < MAX_COLOR_TARGET_BINDINGS; i += 1) {
  2646. D3D12_RENDER_TARGET_BLEND_DESC rtBlendDesc;
  2647. rtBlendDesc.BlendEnable = FALSE;
  2648. rtBlendDesc.LogicOpEnable = FALSE;
  2649. rtBlendDesc.SrcBlend = D3D12_BLEND_ONE;
  2650. rtBlendDesc.DestBlend = D3D12_BLEND_ZERO;
  2651. rtBlendDesc.BlendOp = D3D12_BLEND_OP_ADD;
  2652. rtBlendDesc.SrcBlendAlpha = D3D12_BLEND_ONE;
  2653. rtBlendDesc.DestBlendAlpha = D3D12_BLEND_ZERO;
  2654. rtBlendDesc.BlendOpAlpha = D3D12_BLEND_OP_ADD;
  2655. rtBlendDesc.LogicOp = D3D12_LOGIC_OP_NOOP;
  2656. rtBlendDesc.RenderTargetWriteMask = D3D12_COLOR_WRITE_ENABLE_ALL;
  2657. // If target_info has more blend states, you can set IndependentBlendEnable to TRUE and assign different blend states to each render target slot
  2658. if (i < pipelineInfo->target_info.num_color_targets) {
  2659. SDL_GPUColorTargetBlendState sdlBlendState = pipelineInfo->target_info.color_target_descriptions[i].blend_state;
  2660. SDL_GPUColorComponentFlags colorWriteMask = sdlBlendState.enable_color_write_mask ?
  2661. sdlBlendState.color_write_mask :
  2662. 0xF;
  2663. rtBlendDesc.BlendEnable = sdlBlendState.enable_blend;
  2664. rtBlendDesc.SrcBlend = SDLToD3D12_BlendFactor[sdlBlendState.src_color_blendfactor];
  2665. rtBlendDesc.DestBlend = SDLToD3D12_BlendFactor[sdlBlendState.dst_color_blendfactor];
  2666. rtBlendDesc.BlendOp = SDLToD3D12_BlendOp[sdlBlendState.color_blend_op];
  2667. rtBlendDesc.SrcBlendAlpha = SDLToD3D12_BlendFactorAlpha[sdlBlendState.src_alpha_blendfactor];
  2668. rtBlendDesc.DestBlendAlpha = SDLToD3D12_BlendFactorAlpha[sdlBlendState.dst_alpha_blendfactor];
  2669. rtBlendDesc.BlendOpAlpha = SDLToD3D12_BlendOp[sdlBlendState.alpha_blend_op];
  2670. rtBlendDesc.RenderTargetWriteMask = colorWriteMask;
  2671. if (i > 0) {
  2672. blendDesc->IndependentBlendEnable = TRUE;
  2673. }
  2674. }
  2675. blendDesc->RenderTarget[i] = rtBlendDesc;
  2676. }
  2677. return true;
  2678. }
  2679. static bool D3D12_INTERNAL_ConvertDepthStencilState(SDL_GPUDepthStencilState depthStencilState, D3D12_DEPTH_STENCIL_DESC *desc)
  2680. {
  2681. if (desc == NULL) {
  2682. return false;
  2683. }
  2684. desc->DepthEnable = depthStencilState.enable_depth_test == true ? TRUE : FALSE;
  2685. desc->DepthWriteMask = depthStencilState.enable_depth_write == true ? D3D12_DEPTH_WRITE_MASK_ALL : D3D12_DEPTH_WRITE_MASK_ZERO;
  2686. desc->DepthFunc = SDLToD3D12_CompareOp[depthStencilState.compare_op];
  2687. desc->StencilEnable = depthStencilState.enable_stencil_test == true ? TRUE : FALSE;
  2688. desc->StencilReadMask = depthStencilState.compare_mask;
  2689. desc->StencilWriteMask = depthStencilState.write_mask;
  2690. desc->FrontFace.StencilFailOp = SDLToD3D12_StencilOp[depthStencilState.front_stencil_state.fail_op];
  2691. desc->FrontFace.StencilDepthFailOp = SDLToD3D12_StencilOp[depthStencilState.front_stencil_state.depth_fail_op];
  2692. desc->FrontFace.StencilPassOp = SDLToD3D12_StencilOp[depthStencilState.front_stencil_state.pass_op];
  2693. desc->FrontFace.StencilFunc = SDLToD3D12_CompareOp[depthStencilState.front_stencil_state.compare_op];
  2694. desc->BackFace.StencilFailOp = SDLToD3D12_StencilOp[depthStencilState.back_stencil_state.fail_op];
  2695. desc->BackFace.StencilDepthFailOp = SDLToD3D12_StencilOp[depthStencilState.back_stencil_state.depth_fail_op];
  2696. desc->BackFace.StencilPassOp = SDLToD3D12_StencilOp[depthStencilState.back_stencil_state.pass_op];
  2697. desc->BackFace.StencilFunc = SDLToD3D12_CompareOp[depthStencilState.back_stencil_state.compare_op];
  2698. return true;
  2699. }
  2700. static bool D3D12_INTERNAL_ConvertVertexInputState(SDL_GPUVertexInputState vertexInputState, D3D12_INPUT_ELEMENT_DESC *desc, const char *semantic)
  2701. {
  2702. if (desc == NULL || vertexInputState.num_vertex_attributes == 0) {
  2703. return false;
  2704. }
  2705. for (Uint32 i = 0; i < vertexInputState.num_vertex_attributes; i += 1) {
  2706. SDL_GPUVertexAttribute attribute = vertexInputState.vertex_attributes[i];
  2707. desc[i].SemanticName = semantic;
  2708. desc[i].SemanticIndex = attribute.location;
  2709. desc[i].Format = SDLToD3D12_VertexFormat[attribute.format];
  2710. desc[i].InputSlot = attribute.buffer_slot;
  2711. desc[i].AlignedByteOffset = attribute.offset;
  2712. desc[i].InputSlotClass = SDLToD3D12_InputRate[vertexInputState.vertex_buffer_descriptions[attribute.buffer_slot].input_rate];
  2713. desc[i].InstanceDataStepRate = (vertexInputState.vertex_buffer_descriptions[attribute.buffer_slot].input_rate == SDL_GPU_VERTEXINPUTRATE_INSTANCE)
  2714. ? 1
  2715. : 0;
  2716. }
  2717. return true;
  2718. }
  2719. static bool D3D12_INTERNAL_AssignStagingDescriptorHandle(
  2720. D3D12Renderer *renderer,
  2721. D3D12_DESCRIPTOR_HEAP_TYPE heapType,
  2722. D3D12StagingDescriptor *cpuDescriptor)
  2723. {
  2724. D3D12StagingDescriptor *descriptor;
  2725. D3D12StagingDescriptorPool *pool = renderer->stagingDescriptorPools[heapType];
  2726. SDL_LockMutex(pool->lock);
  2727. if (pool->freeDescriptorCount == 0) {
  2728. if (!D3D12_INTERNAL_ExpandStagingDescriptorPool(renderer, pool))
  2729. {
  2730. SDL_UnlockMutex(pool->lock);
  2731. return false;
  2732. }
  2733. }
  2734. descriptor = &pool->freeDescriptors[pool->freeDescriptorCount - 1];
  2735. SDL_memcpy(cpuDescriptor, descriptor, sizeof(D3D12StagingDescriptor));
  2736. pool->freeDescriptorCount -= 1;
  2737. SDL_UnlockMutex(pool->lock);
  2738. return true;
  2739. }
  2740. static SDL_GPUGraphicsPipeline *D3D12_CreateGraphicsPipeline(
  2741. SDL_GPURenderer *driverData,
  2742. const SDL_GPUGraphicsPipelineCreateInfo *createinfo)
  2743. {
  2744. D3D12Renderer *renderer = (D3D12Renderer *)driverData;
  2745. D3D12Shader *vertShader = (D3D12Shader *)createinfo->vertex_shader;
  2746. D3D12Shader *fragShader = (D3D12Shader *)createinfo->fragment_shader;
  2747. if (renderer->debug_mode) {
  2748. if (vertShader->stage != SDL_GPU_SHADERSTAGE_VERTEX) {
  2749. SDL_assert_release(!"CreateGraphicsPipeline was passed a fragment shader for the vertex stage");
  2750. }
  2751. if (fragShader->stage != SDL_GPU_SHADERSTAGE_FRAGMENT) {
  2752. SDL_assert_release(!"CreateGraphicsPipeline was passed a vertex shader for the fragment stage");
  2753. }
  2754. }
  2755. D3D12_GRAPHICS_PIPELINE_STATE_DESC psoDesc;
  2756. SDL_zero(psoDesc);
  2757. psoDesc.VS.pShaderBytecode = vertShader->bytecode;
  2758. psoDesc.VS.BytecodeLength = vertShader->bytecodeSize;
  2759. psoDesc.PS.pShaderBytecode = fragShader->bytecode;
  2760. psoDesc.PS.BytecodeLength = fragShader->bytecodeSize;
  2761. D3D12_INPUT_ELEMENT_DESC inputElementDescs[D3D12_IA_VERTEX_INPUT_STRUCTURE_ELEMENT_COUNT];
  2762. if (createinfo->vertex_input_state.num_vertex_attributes > 0) {
  2763. psoDesc.InputLayout.pInputElementDescs = inputElementDescs;
  2764. psoDesc.InputLayout.NumElements = createinfo->vertex_input_state.num_vertex_attributes;
  2765. D3D12_INTERNAL_ConvertVertexInputState(createinfo->vertex_input_state, inputElementDescs, renderer->semantic);
  2766. }
  2767. psoDesc.PrimitiveTopologyType = SDLToD3D12_PrimitiveTopologyType[createinfo->primitive_type];
  2768. if (!D3D12_INTERNAL_ConvertRasterizerState(createinfo->rasterizer_state, &psoDesc.RasterizerState)) {
  2769. return NULL;
  2770. }
  2771. if (!D3D12_INTERNAL_ConvertBlendState(createinfo, &psoDesc.BlendState)) {
  2772. return NULL;
  2773. }
  2774. if (!D3D12_INTERNAL_ConvertDepthStencilState(createinfo->depth_stencil_state, &psoDesc.DepthStencilState)) {
  2775. return NULL;
  2776. }
  2777. D3D12GraphicsPipeline *pipeline = (D3D12GraphicsPipeline *)SDL_calloc(1, sizeof(D3D12GraphicsPipeline));
  2778. if (!pipeline) {
  2779. return NULL;
  2780. }
  2781. psoDesc.SampleMask = 0xFFFFFFFF;
  2782. psoDesc.SampleDesc.Count = SDLToD3D12_SampleCount[createinfo->multisample_state.sample_count];
  2783. psoDesc.SampleDesc.Quality = (createinfo->multisample_state.sample_count > SDL_GPU_SAMPLECOUNT_1) ? D3D12_STANDARD_MULTISAMPLE_PATTERN : 0;
  2784. if (createinfo->target_info.has_depth_stencil_target) {
  2785. psoDesc.DSVFormat = SDLToD3D12_DepthFormat[createinfo->target_info.depth_stencil_format];
  2786. }
  2787. psoDesc.NumRenderTargets = createinfo->target_info.num_color_targets;
  2788. for (uint32_t i = 0; i < createinfo->target_info.num_color_targets; i += 1) {
  2789. psoDesc.RTVFormats[i] = SDLToD3D12_TextureFormat[createinfo->target_info.color_target_descriptions[i].format];
  2790. }
  2791. // Assuming some default values or further initialization
  2792. psoDesc.Flags = D3D12_PIPELINE_STATE_FLAG_NONE;
  2793. psoDesc.CachedPSO.CachedBlobSizeInBytes = 0;
  2794. psoDesc.CachedPSO.pCachedBlob = NULL;
  2795. psoDesc.NodeMask = 0;
  2796. D3D12GraphicsRootSignature *rootSignature = D3D12_INTERNAL_CreateGraphicsRootSignature(
  2797. renderer,
  2798. vertShader,
  2799. fragShader);
  2800. if (rootSignature == NULL) {
  2801. D3D12_INTERNAL_DestroyGraphicsPipeline(pipeline);
  2802. return NULL;
  2803. }
  2804. pipeline->rootSignature = rootSignature;
  2805. psoDesc.pRootSignature = rootSignature->handle;
  2806. ID3D12PipelineState *pipelineState;
  2807. HRESULT res = ID3D12Device_CreateGraphicsPipelineState(
  2808. renderer->device,
  2809. &psoDesc,
  2810. D3D_GUID(D3D_IID_ID3D12PipelineState),
  2811. (void **)&pipelineState);
  2812. if (FAILED(res)) {
  2813. D3D12_INTERNAL_SetError(renderer, "Could not create graphics pipeline state", res);
  2814. D3D12_INTERNAL_DestroyGraphicsPipeline(pipeline);
  2815. return NULL;
  2816. }
  2817. pipeline->pipelineState = pipelineState;
  2818. for (Uint32 i = 0; i < createinfo->vertex_input_state.num_vertex_buffers; i += 1) {
  2819. pipeline->vertexStrides[createinfo->vertex_input_state.vertex_buffer_descriptions[i].slot] =
  2820. createinfo->vertex_input_state.vertex_buffer_descriptions[i].pitch;
  2821. }
  2822. pipeline->primitiveType = createinfo->primitive_type;
  2823. pipeline->header.num_vertex_samplers = vertShader->num_samplers;
  2824. pipeline->header.num_vertex_storage_textures = vertShader->numStorageTextures;
  2825. pipeline->header.num_vertex_storage_buffers = vertShader->numStorageBuffers;
  2826. pipeline->header.num_vertex_uniform_buffers = vertShader->numUniformBuffers;
  2827. pipeline->header.num_fragment_samplers = fragShader->num_samplers;
  2828. pipeline->header.num_fragment_storage_textures = fragShader->numStorageTextures;
  2829. pipeline->header.num_fragment_storage_buffers = fragShader->numStorageBuffers;
  2830. pipeline->header.num_fragment_uniform_buffers = fragShader->numUniformBuffers;
  2831. SDL_SetAtomicInt(&pipeline->referenceCount, 0);
  2832. if (renderer->debug_mode && SDL_HasProperty(createinfo->props, SDL_PROP_GPU_GRAPHICSPIPELINE_CREATE_NAME_STRING)) {
  2833. D3D12_INTERNAL_SetPipelineStateName(
  2834. renderer,
  2835. pipeline->pipelineState,
  2836. SDL_GetStringProperty(createinfo->props, SDL_PROP_GPU_GRAPHICSPIPELINE_CREATE_NAME_STRING, NULL));
  2837. }
  2838. return (SDL_GPUGraphicsPipeline *)pipeline;
  2839. }
  2840. static SDL_GPUSampler *D3D12_CreateSampler(
  2841. SDL_GPURenderer *driverData,
  2842. const SDL_GPUSamplerCreateInfo *createinfo)
  2843. {
  2844. D3D12Renderer *renderer = (D3D12Renderer *)driverData;
  2845. D3D12Sampler *sampler = (D3D12Sampler *)SDL_calloc(1, sizeof(D3D12Sampler));
  2846. if (!sampler) {
  2847. return NULL;
  2848. }
  2849. D3D12_SAMPLER_DESC samplerDesc;
  2850. samplerDesc.Filter = SDLToD3D12_Filter(
  2851. createinfo->min_filter,
  2852. createinfo->mag_filter,
  2853. createinfo->mipmap_mode,
  2854. createinfo->enable_compare,
  2855. createinfo->enable_anisotropy);
  2856. samplerDesc.AddressU = SDLToD3D12_SamplerAddressMode[createinfo->address_mode_u];
  2857. samplerDesc.AddressV = SDLToD3D12_SamplerAddressMode[createinfo->address_mode_v];
  2858. samplerDesc.AddressW = SDLToD3D12_SamplerAddressMode[createinfo->address_mode_w];
  2859. samplerDesc.MaxAnisotropy = (Uint32)createinfo->max_anisotropy;
  2860. samplerDesc.ComparisonFunc = SDLToD3D12_CompareOp[createinfo->compare_op];
  2861. samplerDesc.MinLOD = createinfo->min_lod;
  2862. samplerDesc.MaxLOD = createinfo->max_lod;
  2863. samplerDesc.MipLODBias = createinfo->mip_lod_bias;
  2864. samplerDesc.BorderColor[0] = 0;
  2865. samplerDesc.BorderColor[1] = 0;
  2866. samplerDesc.BorderColor[2] = 0;
  2867. samplerDesc.BorderColor[3] = 0;
  2868. D3D12_INTERNAL_AssignStagingDescriptorHandle(
  2869. renderer,
  2870. D3D12_DESCRIPTOR_HEAP_TYPE_SAMPLER,
  2871. &sampler->handle);
  2872. ID3D12Device_CreateSampler(
  2873. renderer->device,
  2874. &samplerDesc,
  2875. sampler->handle.cpuHandle);
  2876. sampler->createInfo = *createinfo;
  2877. SDL_SetAtomicInt(&sampler->referenceCount, 0);
  2878. // Ignore name property because it is not applicable to D3D12.
  2879. return (SDL_GPUSampler *)sampler;
  2880. }
  2881. static SDL_GPUShader *D3D12_CreateShader(
  2882. SDL_GPURenderer *driverData,
  2883. const SDL_GPUShaderCreateInfo *createinfo)
  2884. {
  2885. D3D12Renderer *renderer = (D3D12Renderer *)driverData;
  2886. void *bytecode;
  2887. size_t bytecodeSize;
  2888. D3D12Shader *shader;
  2889. if (!D3D12_INTERNAL_CreateShaderBytecode(
  2890. renderer,
  2891. createinfo->code,
  2892. createinfo->code_size,
  2893. createinfo->format,
  2894. &bytecode,
  2895. &bytecodeSize)) {
  2896. return NULL;
  2897. }
  2898. shader = (D3D12Shader *)SDL_calloc(1, sizeof(D3D12Shader));
  2899. if (!shader) {
  2900. SDL_free(bytecode);
  2901. return NULL;
  2902. }
  2903. shader->stage = createinfo->stage;
  2904. shader->num_samplers = createinfo->num_samplers;
  2905. shader->numStorageBuffers = createinfo->num_storage_buffers;
  2906. shader->numStorageTextures = createinfo->num_storage_textures;
  2907. shader->numUniformBuffers = createinfo->num_uniform_buffers;
  2908. shader->bytecode = bytecode;
  2909. shader->bytecodeSize = bytecodeSize;
  2910. // Ignore name property because it is not applicable to D3D12.
  2911. return (SDL_GPUShader *)shader;
  2912. }
  2913. static D3D12Texture *D3D12_INTERNAL_CreateTexture(
  2914. D3D12Renderer *renderer,
  2915. const SDL_GPUTextureCreateInfo *createinfo,
  2916. bool isSwapchainTexture,
  2917. const char *debugName)
  2918. {
  2919. D3D12Texture *texture;
  2920. ID3D12Resource *handle;
  2921. D3D12_HEAP_PROPERTIES heapProperties;
  2922. D3D12_HEAP_FLAGS heapFlags = (D3D12_HEAP_FLAGS)0;
  2923. D3D12_RESOURCE_DESC desc;
  2924. D3D12_RESOURCE_FLAGS resourceFlags = (D3D12_RESOURCE_FLAGS)0;
  2925. D3D12_RESOURCE_STATES initialState = (D3D12_RESOURCE_STATES)0;
  2926. D3D12_CLEAR_VALUE clearValue;
  2927. DXGI_FORMAT format;
  2928. bool useClearValue = false;
  2929. bool needsSRV =
  2930. (createinfo->usage & SDL_GPU_TEXTUREUSAGE_SAMPLER) ||
  2931. (createinfo->usage & SDL_GPU_TEXTUREUSAGE_GRAPHICS_STORAGE_READ) ||
  2932. (createinfo->usage & SDL_GPU_TEXTUREUSAGE_COMPUTE_STORAGE_READ);
  2933. bool needsUAV =
  2934. (createinfo->usage & SDL_GPU_TEXTUREUSAGE_COMPUTE_STORAGE_WRITE) ||
  2935. (createinfo->usage & SDL_GPU_TEXTUREUSAGE_COMPUTE_STORAGE_SIMULTANEOUS_READ_WRITE);
  2936. HRESULT res;
  2937. texture = (D3D12Texture *)SDL_calloc(1, sizeof(D3D12Texture));
  2938. if (!texture) {
  2939. return NULL;
  2940. }
  2941. Uint32 layerCount = createinfo->type == SDL_GPU_TEXTURETYPE_3D ? 1 : createinfo->layer_count_or_depth;
  2942. Uint32 depth = createinfo->type == SDL_GPU_TEXTURETYPE_3D ? createinfo->layer_count_or_depth : 1;
  2943. bool isMultisample = createinfo->sample_count > SDL_GPU_SAMPLECOUNT_1;
  2944. format = SDLToD3D12_TextureFormat[createinfo->format];
  2945. if (createinfo->usage & SDL_GPU_TEXTUREUSAGE_COLOR_TARGET) {
  2946. resourceFlags |= D3D12_RESOURCE_FLAG_ALLOW_RENDER_TARGET;
  2947. useClearValue = true;
  2948. clearValue.Format = format;
  2949. clearValue.Color[0] = SDL_GetFloatProperty(createinfo->props, SDL_PROP_GPU_TEXTURE_CREATE_D3D12_CLEAR_R_FLOAT, 0);
  2950. clearValue.Color[1] = SDL_GetFloatProperty(createinfo->props, SDL_PROP_GPU_TEXTURE_CREATE_D3D12_CLEAR_G_FLOAT, 0);
  2951. clearValue.Color[2] = SDL_GetFloatProperty(createinfo->props, SDL_PROP_GPU_TEXTURE_CREATE_D3D12_CLEAR_B_FLOAT, 0);
  2952. clearValue.Color[3] = SDL_GetFloatProperty(createinfo->props, SDL_PROP_GPU_TEXTURE_CREATE_D3D12_CLEAR_A_FLOAT, 0);
  2953. }
  2954. if (createinfo->usage & SDL_GPU_TEXTUREUSAGE_DEPTH_STENCIL_TARGET) {
  2955. resourceFlags |= D3D12_RESOURCE_FLAG_ALLOW_DEPTH_STENCIL;
  2956. useClearValue = true;
  2957. clearValue.Format = SDLToD3D12_DepthFormat[createinfo->format];
  2958. clearValue.DepthStencil.Depth = SDL_GetFloatProperty(createinfo->props, SDL_PROP_GPU_TEXTURE_CREATE_D3D12_CLEAR_DEPTH_FLOAT, 0);
  2959. clearValue.DepthStencil.Stencil = (UINT8)SDL_GetNumberProperty(createinfo->props, SDL_PROP_GPU_TEXTURE_CREATE_D3D12_CLEAR_STENCIL_NUMBER, 0);
  2960. format = needsSRV ? SDLToD3D12_TypelessFormat[createinfo->format] : SDLToD3D12_DepthFormat[createinfo->format];
  2961. }
  2962. if (needsUAV) {
  2963. resourceFlags |= D3D12_RESOURCE_FLAG_ALLOW_UNORDERED_ACCESS;
  2964. }
  2965. heapProperties.Type = D3D12_HEAP_TYPE_DEFAULT;
  2966. heapProperties.CPUPageProperty = D3D12_CPU_PAGE_PROPERTY_UNKNOWN;
  2967. heapProperties.MemoryPoolPreference = D3D12_MEMORY_POOL_UNKNOWN;
  2968. heapProperties.CreationNodeMask = 0; // We don't do multi-adapter operation
  2969. heapProperties.VisibleNodeMask = 0; // We don't do multi-adapter operation
  2970. heapFlags = isSwapchainTexture ? D3D12_HEAP_FLAG_ALLOW_DISPLAY : D3D12_HEAP_FLAG_NONE;
  2971. if (createinfo->type != SDL_GPU_TEXTURETYPE_3D) {
  2972. desc.Dimension = D3D12_RESOURCE_DIMENSION_TEXTURE2D;
  2973. desc.Alignment = isSwapchainTexture ? 0 : isMultisample ? D3D12_DEFAULT_MSAA_RESOURCE_PLACEMENT_ALIGNMENT : D3D12_DEFAULT_RESOURCE_PLACEMENT_ALIGNMENT;
  2974. desc.Width = createinfo->width;
  2975. desc.Height = createinfo->height;
  2976. desc.DepthOrArraySize = (UINT16)createinfo->layer_count_or_depth;
  2977. desc.MipLevels = (UINT16)createinfo->num_levels;
  2978. desc.Format = format;
  2979. desc.SampleDesc.Count = SDLToD3D12_SampleCount[createinfo->sample_count];
  2980. desc.SampleDesc.Quality = isMultisample ? D3D12_STANDARD_MULTISAMPLE_PATTERN : 0;
  2981. desc.Layout = D3D12_TEXTURE_LAYOUT_UNKNOWN; // Apparently this is the most efficient choice
  2982. desc.Flags = resourceFlags;
  2983. } else {
  2984. desc.Dimension = D3D12_RESOURCE_DIMENSION_TEXTURE3D;
  2985. desc.Alignment = D3D12_DEFAULT_RESOURCE_PLACEMENT_ALIGNMENT;
  2986. desc.Width = createinfo->width;
  2987. desc.Height = createinfo->height;
  2988. desc.DepthOrArraySize = (UINT16)createinfo->layer_count_or_depth;
  2989. desc.MipLevels = (UINT16)createinfo->num_levels;
  2990. desc.Format = format;
  2991. desc.SampleDesc.Count = 1;
  2992. desc.SampleDesc.Quality = 0;
  2993. desc.Layout = D3D12_TEXTURE_LAYOUT_UNKNOWN;
  2994. desc.Flags = resourceFlags;
  2995. }
  2996. initialState = isSwapchainTexture ? D3D12_RESOURCE_STATE_PRESENT : D3D12_INTERNAL_DefaultTextureResourceState(createinfo->usage);
  2997. res = ID3D12Device_CreateCommittedResource(
  2998. renderer->device,
  2999. &heapProperties,
  3000. heapFlags,
  3001. &desc,
  3002. initialState,
  3003. useClearValue ? &clearValue : NULL,
  3004. D3D_GUID(D3D_IID_ID3D12Resource),
  3005. (void **)&handle);
  3006. if (FAILED(res)) {
  3007. D3D12_INTERNAL_SetError(renderer, "Failed to create texture!", res);
  3008. D3D12_INTERNAL_DestroyTexture(texture);
  3009. return NULL;
  3010. }
  3011. texture->resource = handle;
  3012. // Create the SRV if applicable
  3013. if (needsSRV) {
  3014. D3D12_SHADER_RESOURCE_VIEW_DESC srvDesc;
  3015. D3D12_INTERNAL_AssignStagingDescriptorHandle(
  3016. renderer,
  3017. D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV,
  3018. &texture->srvHandle);
  3019. srvDesc.Format = SDLToD3D12_TextureFormat[createinfo->format];
  3020. srvDesc.Shader4ComponentMapping = D3D12_DEFAULT_SHADER_4_COMPONENT_MAPPING;
  3021. if (createinfo->type == SDL_GPU_TEXTURETYPE_CUBE) {
  3022. srvDesc.ViewDimension = D3D12_SRV_DIMENSION_TEXTURECUBE;
  3023. srvDesc.TextureCube.MipLevels = createinfo->num_levels;
  3024. srvDesc.TextureCube.MostDetailedMip = 0;
  3025. srvDesc.TextureCube.ResourceMinLODClamp = 0;
  3026. } else if (createinfo->type == SDL_GPU_TEXTURETYPE_CUBE_ARRAY) {
  3027. srvDesc.ViewDimension = D3D12_SRV_DIMENSION_TEXTURECUBEARRAY;
  3028. srvDesc.TextureCubeArray.MipLevels = createinfo->num_levels;
  3029. srvDesc.TextureCubeArray.MostDetailedMip = 0;
  3030. srvDesc.TextureCubeArray.First2DArrayFace = 0;
  3031. srvDesc.TextureCubeArray.NumCubes = createinfo->layer_count_or_depth / 6;
  3032. srvDesc.TextureCubeArray.ResourceMinLODClamp = 0;
  3033. } else if (createinfo->type == SDL_GPU_TEXTURETYPE_2D_ARRAY) {
  3034. srvDesc.ViewDimension = D3D12_SRV_DIMENSION_TEXTURE2DARRAY;
  3035. srvDesc.Texture2DArray.MipLevels = createinfo->num_levels;
  3036. srvDesc.Texture2DArray.MostDetailedMip = 0;
  3037. srvDesc.Texture2DArray.FirstArraySlice = 0;
  3038. srvDesc.Texture2DArray.ArraySize = layerCount;
  3039. srvDesc.Texture2DArray.ResourceMinLODClamp = 0;
  3040. srvDesc.Texture2DArray.PlaneSlice = 0;
  3041. } else if (createinfo->type == SDL_GPU_TEXTURETYPE_3D) {
  3042. srvDesc.ViewDimension = D3D12_SRV_DIMENSION_TEXTURE3D;
  3043. srvDesc.Texture3D.MipLevels = createinfo->num_levels;
  3044. srvDesc.Texture3D.MostDetailedMip = 0;
  3045. srvDesc.Texture3D.ResourceMinLODClamp = 0; // default behavior
  3046. } else {
  3047. srvDesc.ViewDimension = D3D12_SRV_DIMENSION_TEXTURE2D;
  3048. srvDesc.Texture2D.MipLevels = createinfo->num_levels;
  3049. srvDesc.Texture2D.MostDetailedMip = 0;
  3050. srvDesc.Texture2D.PlaneSlice = 0;
  3051. srvDesc.Texture2D.ResourceMinLODClamp = 0; // default behavior
  3052. }
  3053. ID3D12Device_CreateShaderResourceView(
  3054. renderer->device,
  3055. handle,
  3056. &srvDesc,
  3057. texture->srvHandle.cpuHandle);
  3058. }
  3059. SDL_SetAtomicInt(&texture->referenceCount, 0);
  3060. texture->subresourceCount = createinfo->num_levels * layerCount;
  3061. texture->subresources = (D3D12TextureSubresource *)SDL_calloc(
  3062. texture->subresourceCount, sizeof(D3D12TextureSubresource));
  3063. if (!texture->subresources) {
  3064. D3D12_INTERNAL_DestroyTexture(texture);
  3065. return NULL;
  3066. }
  3067. for (Uint32 layerIndex = 0; layerIndex < layerCount; layerIndex += 1) {
  3068. for (Uint32 levelIndex = 0; levelIndex < createinfo->num_levels; levelIndex += 1) {
  3069. Uint32 subresourceIndex = D3D12_INTERNAL_CalcSubresource(
  3070. levelIndex,
  3071. layerIndex,
  3072. createinfo->num_levels);
  3073. texture->subresources[subresourceIndex].parent = texture;
  3074. texture->subresources[subresourceIndex].layer = layerIndex;
  3075. texture->subresources[subresourceIndex].level = levelIndex;
  3076. texture->subresources[subresourceIndex].depth = depth;
  3077. texture->subresources[subresourceIndex].index = subresourceIndex;
  3078. texture->subresources[subresourceIndex].rtvHandles = NULL;
  3079. texture->subresources[subresourceIndex].uavHandle.heap = NULL;
  3080. texture->subresources[subresourceIndex].dsvHandle.heap = NULL;
  3081. // Create RTV if needed
  3082. if (createinfo->usage & SDL_GPU_TEXTUREUSAGE_COLOR_TARGET) {
  3083. texture->subresources[subresourceIndex].rtvHandles = (D3D12StagingDescriptor *)SDL_calloc(depth, sizeof(D3D12StagingDescriptor));
  3084. for (Uint32 depthIndex = 0; depthIndex < depth; depthIndex += 1) {
  3085. D3D12_RENDER_TARGET_VIEW_DESC rtvDesc;
  3086. D3D12_INTERNAL_AssignStagingDescriptorHandle(
  3087. renderer,
  3088. D3D12_DESCRIPTOR_HEAP_TYPE_RTV,
  3089. &texture->subresources[subresourceIndex].rtvHandles[depthIndex]);
  3090. rtvDesc.Format = SDLToD3D12_TextureFormat[createinfo->format];
  3091. if (createinfo->type == SDL_GPU_TEXTURETYPE_2D_ARRAY || createinfo->type == SDL_GPU_TEXTURETYPE_CUBE || createinfo->type == SDL_GPU_TEXTURETYPE_CUBE_ARRAY) {
  3092. rtvDesc.ViewDimension = D3D12_RTV_DIMENSION_TEXTURE2DARRAY;
  3093. rtvDesc.Texture2DArray.MipSlice = levelIndex;
  3094. rtvDesc.Texture2DArray.FirstArraySlice = layerIndex;
  3095. rtvDesc.Texture2DArray.ArraySize = 1;
  3096. rtvDesc.Texture2DArray.PlaneSlice = 0;
  3097. } else if (createinfo->type == SDL_GPU_TEXTURETYPE_3D) {
  3098. rtvDesc.ViewDimension = D3D12_RTV_DIMENSION_TEXTURE3D;
  3099. rtvDesc.Texture3D.MipSlice = levelIndex;
  3100. rtvDesc.Texture3D.FirstWSlice = depthIndex;
  3101. rtvDesc.Texture3D.WSize = 1;
  3102. } else if (isMultisample) {
  3103. rtvDesc.ViewDimension = D3D12_RTV_DIMENSION_TEXTURE2DMS;
  3104. } else {
  3105. rtvDesc.ViewDimension = D3D12_RTV_DIMENSION_TEXTURE2D;
  3106. rtvDesc.Texture2D.MipSlice = levelIndex;
  3107. rtvDesc.Texture2D.PlaneSlice = 0;
  3108. }
  3109. ID3D12Device_CreateRenderTargetView(
  3110. renderer->device,
  3111. texture->resource,
  3112. &rtvDesc,
  3113. texture->subresources[subresourceIndex].rtvHandles[depthIndex].cpuHandle);
  3114. }
  3115. }
  3116. // Create DSV if needed
  3117. if (createinfo->usage & SDL_GPU_TEXTUREUSAGE_DEPTH_STENCIL_TARGET) {
  3118. D3D12_DEPTH_STENCIL_VIEW_DESC dsvDesc;
  3119. D3D12_INTERNAL_AssignStagingDescriptorHandle(
  3120. renderer,
  3121. D3D12_DESCRIPTOR_HEAP_TYPE_DSV,
  3122. &texture->subresources[subresourceIndex].dsvHandle);
  3123. dsvDesc.Format = SDLToD3D12_DepthFormat[createinfo->format];
  3124. dsvDesc.Flags = (D3D12_DSV_FLAGS)0;
  3125. if (createinfo->type == SDL_GPU_TEXTURETYPE_2D_ARRAY || createinfo->type == SDL_GPU_TEXTURETYPE_CUBE || createinfo->type == SDL_GPU_TEXTURETYPE_CUBE_ARRAY) {
  3126. dsvDesc.ViewDimension = D3D12_DSV_DIMENSION_TEXTURE2DARRAY;
  3127. dsvDesc.Texture2DArray.MipSlice = levelIndex;
  3128. dsvDesc.Texture2DArray.FirstArraySlice = layerIndex;
  3129. dsvDesc.Texture2DArray.ArraySize = 1;
  3130. } else if (isMultisample) {
  3131. dsvDesc.ViewDimension = D3D12_DSV_DIMENSION_TEXTURE2DMS;
  3132. } else {
  3133. dsvDesc.ViewDimension = D3D12_DSV_DIMENSION_TEXTURE2D;
  3134. dsvDesc.Texture2D.MipSlice = levelIndex;
  3135. }
  3136. ID3D12Device_CreateDepthStencilView(
  3137. renderer->device,
  3138. texture->resource,
  3139. &dsvDesc,
  3140. texture->subresources[subresourceIndex].dsvHandle.cpuHandle);
  3141. }
  3142. // Create subresource UAV if necessary
  3143. if (needsUAV) {
  3144. D3D12_UNORDERED_ACCESS_VIEW_DESC uavDesc;
  3145. D3D12_INTERNAL_AssignStagingDescriptorHandle(
  3146. renderer,
  3147. D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV,
  3148. &texture->subresources[subresourceIndex].uavHandle);
  3149. uavDesc.Format = SDLToD3D12_TextureFormat[createinfo->format];
  3150. if (createinfo->type == SDL_GPU_TEXTURETYPE_2D_ARRAY || createinfo->type == SDL_GPU_TEXTURETYPE_CUBE || createinfo->type == SDL_GPU_TEXTURETYPE_CUBE_ARRAY) {
  3151. uavDesc.ViewDimension = D3D12_UAV_DIMENSION_TEXTURE2DARRAY;
  3152. uavDesc.Texture2DArray.MipSlice = levelIndex;
  3153. uavDesc.Texture2DArray.FirstArraySlice = layerIndex;
  3154. uavDesc.Texture2DArray.ArraySize = 1;
  3155. uavDesc.Texture2DArray.PlaneSlice = 0;
  3156. } else if (createinfo->type == SDL_GPU_TEXTURETYPE_3D) {
  3157. uavDesc.ViewDimension = D3D12_UAV_DIMENSION_TEXTURE3D;
  3158. uavDesc.Texture3D.MipSlice = levelIndex;
  3159. uavDesc.Texture3D.FirstWSlice = 0;
  3160. uavDesc.Texture3D.WSize = depth;
  3161. } else {
  3162. uavDesc.ViewDimension = D3D12_UAV_DIMENSION_TEXTURE2D;
  3163. uavDesc.Texture2D.MipSlice = levelIndex;
  3164. uavDesc.Texture2D.PlaneSlice = 0;
  3165. }
  3166. ID3D12Device_CreateUnorderedAccessView(
  3167. renderer->device,
  3168. texture->resource,
  3169. NULL,
  3170. &uavDesc,
  3171. texture->subresources[subresourceIndex].uavHandle.cpuHandle);
  3172. }
  3173. }
  3174. }
  3175. D3D12_INTERNAL_SetResourceName(
  3176. renderer,
  3177. texture->resource,
  3178. debugName);
  3179. return texture;
  3180. }
  3181. static SDL_GPUTexture *D3D12_CreateTexture(
  3182. SDL_GPURenderer *driverData,
  3183. const SDL_GPUTextureCreateInfo *createinfo)
  3184. {
  3185. D3D12TextureContainer *container = (D3D12TextureContainer *)SDL_calloc(1, sizeof(D3D12TextureContainer));
  3186. if (!container) {
  3187. return NULL;
  3188. }
  3189. // Copy properties so we don't lose information when the client destroys them
  3190. container->header.info = *createinfo;
  3191. container->header.info.props = SDL_CreateProperties();
  3192. if (createinfo->props) {
  3193. SDL_CopyProperties(createinfo->props, container->header.info.props);
  3194. }
  3195. container->textureCapacity = 1;
  3196. container->textureCount = 1;
  3197. container->textures = (D3D12Texture **)SDL_calloc(
  3198. container->textureCapacity, sizeof(D3D12Texture *));
  3199. if (!container->textures) {
  3200. SDL_free(container);
  3201. return NULL;
  3202. }
  3203. container->debugName = NULL;
  3204. if (SDL_HasProperty(createinfo->props, SDL_PROP_GPU_TEXTURE_CREATE_NAME_STRING)) {
  3205. container->debugName = SDL_strdup(SDL_GetStringProperty(createinfo->props, SDL_PROP_GPU_TEXTURE_CREATE_NAME_STRING, NULL));
  3206. }
  3207. container->canBeCycled = true;
  3208. D3D12Texture *texture = D3D12_INTERNAL_CreateTexture(
  3209. (D3D12Renderer *)driverData,
  3210. createinfo,
  3211. false,
  3212. container->debugName);
  3213. if (!texture) {
  3214. SDL_free(container->textures);
  3215. SDL_free(container);
  3216. return NULL;
  3217. }
  3218. container->textures[0] = texture;
  3219. container->activeTexture = texture;
  3220. texture->container = container;
  3221. texture->containerIndex = 0;
  3222. return (SDL_GPUTexture *)container;
  3223. }
  3224. static D3D12Buffer *D3D12_INTERNAL_CreateBuffer(
  3225. D3D12Renderer *renderer,
  3226. SDL_GPUBufferUsageFlags usageFlags,
  3227. Uint32 size,
  3228. D3D12BufferType type,
  3229. const char *debugName)
  3230. {
  3231. D3D12Buffer *buffer;
  3232. ID3D12Resource *handle;
  3233. D3D12_UNORDERED_ACCESS_VIEW_DESC uavDesc;
  3234. D3D12_SHADER_RESOURCE_VIEW_DESC srvDesc;
  3235. D3D12_CONSTANT_BUFFER_VIEW_DESC cbvDesc;
  3236. D3D12_HEAP_PROPERTIES heapProperties;
  3237. D3D12_RESOURCE_DESC desc;
  3238. D3D12_HEAP_FLAGS heapFlags = (D3D12_HEAP_FLAGS)0;
  3239. D3D12_RESOURCE_FLAGS resourceFlags = (D3D12_RESOURCE_FLAGS)0;
  3240. D3D12_RESOURCE_STATES initialState = D3D12_RESOURCE_STATE_COMMON;
  3241. HRESULT res;
  3242. buffer = (D3D12Buffer *)SDL_calloc(1, sizeof(D3D12Buffer));
  3243. if (!buffer) {
  3244. return NULL;
  3245. }
  3246. if (usageFlags & SDL_GPU_BUFFERUSAGE_COMPUTE_STORAGE_WRITE) {
  3247. resourceFlags |= D3D12_RESOURCE_FLAG_ALLOW_UNORDERED_ACCESS;
  3248. }
  3249. #if (defined(SDL_PLATFORM_XBOXONE) || defined(SDL_PLATFORM_XBOXSERIES))
  3250. if (usageFlags & SDL_GPU_BUFFERUSAGE_INDIRECT) {
  3251. resourceFlags |= D3D12XBOX_RESOURCE_FLAG_ALLOW_INDIRECT_BUFFER;
  3252. }
  3253. #endif
  3254. heapProperties.CreationNodeMask = 0; // We don't do multi-adapter operation
  3255. heapProperties.VisibleNodeMask = 0; // We don't do multi-adapter operation
  3256. if (type == D3D12_BUFFER_TYPE_GPU) {
  3257. heapProperties.Type = D3D12_HEAP_TYPE_DEFAULT;
  3258. heapProperties.CPUPageProperty = D3D12_CPU_PAGE_PROPERTY_UNKNOWN;
  3259. heapProperties.MemoryPoolPreference = D3D12_MEMORY_POOL_UNKNOWN;
  3260. heapFlags = D3D12_HEAP_FLAG_NONE;
  3261. } else if (type == D3D12_BUFFER_TYPE_UPLOAD) {
  3262. heapProperties.Type = D3D12_HEAP_TYPE_UPLOAD;
  3263. heapProperties.CPUPageProperty = D3D12_CPU_PAGE_PROPERTY_UNKNOWN;
  3264. heapProperties.MemoryPoolPreference = D3D12_MEMORY_POOL_UNKNOWN;
  3265. heapFlags = D3D12_HEAP_FLAG_NONE;
  3266. initialState = D3D12_RESOURCE_STATE_GENERIC_READ;
  3267. } else if (type == D3D12_BUFFER_TYPE_DOWNLOAD) {
  3268. heapProperties.Type = D3D12_HEAP_TYPE_READBACK;
  3269. heapProperties.CPUPageProperty = D3D12_CPU_PAGE_PROPERTY_UNKNOWN;
  3270. heapProperties.MemoryPoolPreference = D3D12_MEMORY_POOL_UNKNOWN;
  3271. heapFlags = D3D12_HEAP_FLAG_NONE;
  3272. initialState = D3D12_RESOURCE_STATE_COPY_DEST;
  3273. } else if (type == D3D12_BUFFER_TYPE_UNIFORM) {
  3274. // D3D12 is badly designed, so we have to check if the fast path for uniform buffers is enabled
  3275. if (renderer->GPUUploadHeapSupported) {
  3276. heapProperties.Type = D3D12_HEAP_TYPE_GPU_UPLOAD;
  3277. heapProperties.CPUPageProperty = D3D12_CPU_PAGE_PROPERTY_UNKNOWN;
  3278. heapProperties.MemoryPoolPreference = D3D12_MEMORY_POOL_UNKNOWN;
  3279. } else {
  3280. heapProperties.Type = D3D12_HEAP_TYPE_UPLOAD;
  3281. heapProperties.CPUPageProperty = D3D12_CPU_PAGE_PROPERTY_UNKNOWN;
  3282. heapProperties.MemoryPoolPreference = D3D12_MEMORY_POOL_UNKNOWN;
  3283. initialState = D3D12_RESOURCE_STATE_GENERIC_READ;
  3284. }
  3285. heapFlags = D3D12_HEAP_FLAG_NONE;
  3286. } else {
  3287. SET_STRING_ERROR_AND_RETURN("Unrecognized buffer type!", NULL);
  3288. }
  3289. desc.Dimension = D3D12_RESOURCE_DIMENSION_BUFFER;
  3290. desc.Alignment = D3D12_DEFAULT_RESOURCE_PLACEMENT_ALIGNMENT;
  3291. desc.Width = size;
  3292. desc.Height = 1;
  3293. desc.DepthOrArraySize = 1;
  3294. desc.MipLevels = 1;
  3295. desc.Format = DXGI_FORMAT_UNKNOWN;
  3296. desc.SampleDesc.Count = 1;
  3297. desc.SampleDesc.Quality = 0;
  3298. desc.Layout = D3D12_TEXTURE_LAYOUT_ROW_MAJOR;
  3299. desc.Flags = resourceFlags;
  3300. res = ID3D12Device_CreateCommittedResource(
  3301. renderer->device,
  3302. &heapProperties,
  3303. heapFlags,
  3304. &desc,
  3305. initialState,
  3306. NULL,
  3307. D3D_GUID(D3D_IID_ID3D12Resource),
  3308. (void **)&handle);
  3309. if (FAILED(res)) {
  3310. D3D12_INTERNAL_SetError(renderer, "Could not create buffer!", res);
  3311. D3D12_INTERNAL_DestroyBuffer(buffer);
  3312. return NULL;
  3313. }
  3314. buffer->handle = handle;
  3315. SDL_SetAtomicInt(&buffer->referenceCount, 0);
  3316. buffer->uavDescriptor.heap = NULL;
  3317. buffer->srvDescriptor.heap = NULL;
  3318. buffer->cbvDescriptor.heap = NULL;
  3319. if (usageFlags & SDL_GPU_BUFFERUSAGE_COMPUTE_STORAGE_WRITE) {
  3320. D3D12_INTERNAL_AssignStagingDescriptorHandle(
  3321. renderer,
  3322. D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV,
  3323. &buffer->uavDescriptor);
  3324. uavDesc.ViewDimension = D3D12_UAV_DIMENSION_BUFFER;
  3325. uavDesc.Format = DXGI_FORMAT_R32_TYPELESS;
  3326. uavDesc.Buffer.FirstElement = 0;
  3327. uavDesc.Buffer.NumElements = size / sizeof(Uint32);
  3328. uavDesc.Buffer.Flags = D3D12_BUFFER_UAV_FLAG_RAW;
  3329. uavDesc.Buffer.CounterOffsetInBytes = 0; // TODO: support counters?
  3330. uavDesc.Buffer.StructureByteStride = 0;
  3331. // Create UAV
  3332. ID3D12Device_CreateUnorderedAccessView(
  3333. renderer->device,
  3334. handle,
  3335. NULL, // TODO: support counters?
  3336. &uavDesc,
  3337. buffer->uavDescriptor.cpuHandle);
  3338. }
  3339. if (
  3340. (usageFlags & SDL_GPU_BUFFERUSAGE_GRAPHICS_STORAGE_READ) ||
  3341. (usageFlags & SDL_GPU_BUFFERUSAGE_COMPUTE_STORAGE_READ)) {
  3342. D3D12_INTERNAL_AssignStagingDescriptorHandle(
  3343. renderer,
  3344. D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV,
  3345. &buffer->srvDescriptor);
  3346. srvDesc.Format = DXGI_FORMAT_R32_TYPELESS;
  3347. srvDesc.Shader4ComponentMapping = D3D12_DEFAULT_SHADER_4_COMPONENT_MAPPING;
  3348. srvDesc.ViewDimension = D3D12_SRV_DIMENSION_BUFFER;
  3349. srvDesc.Buffer.FirstElement = 0;
  3350. srvDesc.Buffer.NumElements = size / sizeof(Uint32);
  3351. srvDesc.Buffer.Flags = D3D12_BUFFER_SRV_FLAG_RAW;
  3352. srvDesc.Buffer.StructureByteStride = 0;
  3353. // Create SRV
  3354. ID3D12Device_CreateShaderResourceView(
  3355. renderer->device,
  3356. handle,
  3357. &srvDesc,
  3358. buffer->srvDescriptor.cpuHandle);
  3359. }
  3360. // FIXME: we may not need a CBV since we use root descriptors
  3361. if (type == D3D12_BUFFER_TYPE_UNIFORM) {
  3362. D3D12_INTERNAL_AssignStagingDescriptorHandle(
  3363. renderer,
  3364. D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV,
  3365. &buffer->cbvDescriptor);
  3366. cbvDesc.BufferLocation = ID3D12Resource_GetGPUVirtualAddress(handle);
  3367. cbvDesc.SizeInBytes = size;
  3368. // Create CBV
  3369. ID3D12Device_CreateConstantBufferView(
  3370. renderer->device,
  3371. &cbvDesc,
  3372. buffer->cbvDescriptor.cpuHandle);
  3373. }
  3374. buffer->virtualAddress = 0;
  3375. if (type == D3D12_BUFFER_TYPE_GPU || type == D3D12_BUFFER_TYPE_UNIFORM) {
  3376. buffer->virtualAddress = ID3D12Resource_GetGPUVirtualAddress(buffer->handle);
  3377. }
  3378. buffer->mapPointer = NULL;
  3379. // Persistently map upload buffers
  3380. if (type == D3D12_BUFFER_TYPE_UPLOAD) {
  3381. res = ID3D12Resource_Map(
  3382. buffer->handle,
  3383. 0,
  3384. NULL,
  3385. (void **)&buffer->mapPointer);
  3386. if (FAILED(res)) {
  3387. D3D12_INTERNAL_SetError(renderer, "Failed to map upload buffer!", res);
  3388. D3D12_INTERNAL_DestroyBuffer(buffer);
  3389. return NULL;
  3390. }
  3391. }
  3392. buffer->container = NULL;
  3393. buffer->containerIndex = 0;
  3394. buffer->transitioned = initialState != D3D12_RESOURCE_STATE_COMMON;
  3395. SDL_SetAtomicInt(&buffer->referenceCount, 0);
  3396. D3D12_INTERNAL_SetResourceName(
  3397. renderer,
  3398. buffer->handle,
  3399. debugName);
  3400. return buffer;
  3401. }
  3402. static D3D12BufferContainer *D3D12_INTERNAL_CreateBufferContainer(
  3403. D3D12Renderer *renderer,
  3404. SDL_GPUBufferUsageFlags usageFlags,
  3405. Uint32 size,
  3406. D3D12BufferType type,
  3407. const char *debugName)
  3408. {
  3409. D3D12BufferContainer *container;
  3410. D3D12Buffer *buffer;
  3411. container = (D3D12BufferContainer *)SDL_calloc(1, sizeof(D3D12BufferContainer));
  3412. if (!container) {
  3413. return NULL;
  3414. }
  3415. container->usage = usageFlags;
  3416. container->size = size;
  3417. container->type = type;
  3418. container->bufferCapacity = 1;
  3419. container->bufferCount = 1;
  3420. container->buffers = (D3D12Buffer **)SDL_calloc(
  3421. container->bufferCapacity, sizeof(D3D12Buffer *));
  3422. if (!container->buffers) {
  3423. SDL_free(container);
  3424. return NULL;
  3425. }
  3426. container->debugName = NULL;
  3427. buffer = D3D12_INTERNAL_CreateBuffer(
  3428. renderer,
  3429. usageFlags,
  3430. size,
  3431. type,
  3432. debugName);
  3433. if (buffer == NULL) {
  3434. SDL_free(container->buffers);
  3435. SDL_free(container);
  3436. return NULL;
  3437. }
  3438. container->activeBuffer = buffer;
  3439. container->buffers[0] = buffer;
  3440. buffer->container = container;
  3441. buffer->containerIndex = 0;
  3442. if (debugName != NULL) {
  3443. container->debugName = SDL_strdup(debugName);
  3444. }
  3445. return container;
  3446. }
  3447. static SDL_GPUBuffer *D3D12_CreateBuffer(
  3448. SDL_GPURenderer *driverData,
  3449. SDL_GPUBufferUsageFlags usageFlags,
  3450. Uint32 size,
  3451. const char *debugName)
  3452. {
  3453. return (SDL_GPUBuffer *)D3D12_INTERNAL_CreateBufferContainer(
  3454. (D3D12Renderer *)driverData,
  3455. usageFlags,
  3456. size,
  3457. D3D12_BUFFER_TYPE_GPU,
  3458. debugName);
  3459. }
  3460. static SDL_GPUTransferBuffer *D3D12_CreateTransferBuffer(
  3461. SDL_GPURenderer *driverData,
  3462. SDL_GPUTransferBufferUsage usage,
  3463. Uint32 size,
  3464. const char *debugName)
  3465. {
  3466. return (SDL_GPUTransferBuffer *)D3D12_INTERNAL_CreateBufferContainer(
  3467. (D3D12Renderer *)driverData,
  3468. 0,
  3469. size,
  3470. usage == SDL_GPU_TRANSFERBUFFERUSAGE_UPLOAD ? D3D12_BUFFER_TYPE_UPLOAD : D3D12_BUFFER_TYPE_DOWNLOAD,
  3471. debugName);
  3472. }
  3473. // Disposal
  3474. static void D3D12_ReleaseTexture(
  3475. SDL_GPURenderer *driverData,
  3476. SDL_GPUTexture *texture)
  3477. {
  3478. D3D12Renderer *renderer = (D3D12Renderer *)driverData;
  3479. D3D12TextureContainer *container = (D3D12TextureContainer *)texture;
  3480. D3D12_INTERNAL_ReleaseTextureContainer(
  3481. renderer,
  3482. container);
  3483. }
  3484. static void D3D12_ReleaseSampler(
  3485. SDL_GPURenderer *driverData,
  3486. SDL_GPUSampler *sampler)
  3487. {
  3488. D3D12Renderer *renderer = (D3D12Renderer *)driverData;
  3489. D3D12Sampler *d3d12Sampler = (D3D12Sampler *)sampler;
  3490. SDL_LockMutex(renderer->disposeLock);
  3491. EXPAND_ARRAY_IF_NEEDED(
  3492. renderer->samplersToDestroy,
  3493. D3D12Sampler *,
  3494. renderer->samplersToDestroyCount + 1,
  3495. renderer->samplersToDestroyCapacity,
  3496. renderer->samplersToDestroyCapacity * 2);
  3497. renderer->samplersToDestroy[renderer->samplersToDestroyCount] = d3d12Sampler;
  3498. renderer->samplersToDestroyCount += 1;
  3499. SDL_UnlockMutex(renderer->disposeLock);
  3500. }
  3501. static void D3D12_ReleaseBuffer(
  3502. SDL_GPURenderer *driverData,
  3503. SDL_GPUBuffer *buffer)
  3504. {
  3505. D3D12Renderer *renderer = (D3D12Renderer *)driverData;
  3506. D3D12BufferContainer *bufferContainer = (D3D12BufferContainer *)buffer;
  3507. D3D12_INTERNAL_ReleaseBufferContainer(
  3508. renderer,
  3509. bufferContainer);
  3510. }
  3511. static void D3D12_ReleaseTransferBuffer(
  3512. SDL_GPURenderer *driverData,
  3513. SDL_GPUTransferBuffer *transferBuffer)
  3514. {
  3515. D3D12Renderer *renderer = (D3D12Renderer *)driverData;
  3516. D3D12BufferContainer *transferBufferContainer = (D3D12BufferContainer *)transferBuffer;
  3517. D3D12_INTERNAL_ReleaseBufferContainer(
  3518. renderer,
  3519. transferBufferContainer);
  3520. }
  3521. static void D3D12_ReleaseShader(
  3522. SDL_GPURenderer *driverData,
  3523. SDL_GPUShader *shader)
  3524. {
  3525. /* D3D12Renderer *renderer = (D3D12Renderer *)driverData; */
  3526. D3D12Shader *d3d12shader = (D3D12Shader *)shader;
  3527. if (d3d12shader->bytecode) {
  3528. SDL_free(d3d12shader->bytecode);
  3529. d3d12shader->bytecode = NULL;
  3530. }
  3531. SDL_free(d3d12shader);
  3532. }
  3533. static void D3D12_ReleaseComputePipeline(
  3534. SDL_GPURenderer *driverData,
  3535. SDL_GPUComputePipeline *computePipeline)
  3536. {
  3537. D3D12Renderer *renderer = (D3D12Renderer *)driverData;
  3538. D3D12ComputePipeline *d3d12ComputePipeline = (D3D12ComputePipeline *)computePipeline;
  3539. SDL_LockMutex(renderer->disposeLock);
  3540. EXPAND_ARRAY_IF_NEEDED(
  3541. renderer->computePipelinesToDestroy,
  3542. D3D12ComputePipeline *,
  3543. renderer->computePipelinesToDestroyCount + 1,
  3544. renderer->computePipelinesToDestroyCapacity,
  3545. renderer->computePipelinesToDestroyCapacity * 2);
  3546. renderer->computePipelinesToDestroy[renderer->computePipelinesToDestroyCount] = d3d12ComputePipeline;
  3547. renderer->computePipelinesToDestroyCount += 1;
  3548. SDL_UnlockMutex(renderer->disposeLock);
  3549. }
  3550. static void D3D12_ReleaseGraphicsPipeline(
  3551. SDL_GPURenderer *driverData,
  3552. SDL_GPUGraphicsPipeline *graphicsPipeline)
  3553. {
  3554. D3D12Renderer *renderer = (D3D12Renderer *)driverData;
  3555. D3D12GraphicsPipeline *d3d12GraphicsPipeline = (D3D12GraphicsPipeline *)graphicsPipeline;
  3556. SDL_LockMutex(renderer->disposeLock);
  3557. EXPAND_ARRAY_IF_NEEDED(
  3558. renderer->graphicsPipelinesToDestroy,
  3559. D3D12GraphicsPipeline *,
  3560. renderer->graphicsPipelinesToDestroyCount + 1,
  3561. renderer->graphicsPipelinesToDestroyCapacity,
  3562. renderer->graphicsPipelinesToDestroyCapacity * 2);
  3563. renderer->graphicsPipelinesToDestroy[renderer->graphicsPipelinesToDestroyCount] = d3d12GraphicsPipeline;
  3564. renderer->graphicsPipelinesToDestroyCount += 1;
  3565. SDL_UnlockMutex(renderer->disposeLock);
  3566. }
  3567. static void D3D12_INTERNAL_ReleaseBlitPipelines(SDL_GPURenderer *driverData)
  3568. {
  3569. D3D12Renderer *renderer = (D3D12Renderer *)driverData;
  3570. D3D12_ReleaseSampler(driverData, renderer->blitLinearSampler);
  3571. D3D12_ReleaseSampler(driverData, renderer->blitNearestSampler);
  3572. D3D12_ReleaseShader(driverData, renderer->blitVertexShader);
  3573. D3D12_ReleaseShader(driverData, renderer->blitFrom2DShader);
  3574. D3D12_ReleaseShader(driverData, renderer->blitFrom2DArrayShader);
  3575. D3D12_ReleaseShader(driverData, renderer->blitFrom3DShader);
  3576. D3D12_ReleaseShader(driverData, renderer->blitFromCubeShader);
  3577. D3D12_ReleaseShader(driverData, renderer->blitFromCubeArrayShader);
  3578. for (Uint32 i = 0; i < renderer->blitPipelineCount; i += 1) {
  3579. D3D12_ReleaseGraphicsPipeline(driverData, renderer->blitPipelines[i].pipeline);
  3580. }
  3581. SDL_free(renderer->blitPipelines);
  3582. }
  3583. // Render Pass
  3584. static void D3D12_SetViewport(
  3585. SDL_GPUCommandBuffer *commandBuffer,
  3586. const SDL_GPUViewport *viewport)
  3587. {
  3588. D3D12CommandBuffer *d3d12CommandBuffer = (D3D12CommandBuffer *)commandBuffer;
  3589. D3D12_VIEWPORT d3d12Viewport;
  3590. d3d12Viewport.TopLeftX = viewport->x;
  3591. d3d12Viewport.TopLeftY = viewport->y;
  3592. d3d12Viewport.Width = viewport->w;
  3593. d3d12Viewport.Height = viewport->h;
  3594. d3d12Viewport.MinDepth = viewport->min_depth;
  3595. d3d12Viewport.MaxDepth = viewport->max_depth;
  3596. ID3D12GraphicsCommandList_RSSetViewports(d3d12CommandBuffer->graphicsCommandList, 1, &d3d12Viewport);
  3597. }
  3598. static void D3D12_SetScissor(
  3599. SDL_GPUCommandBuffer *commandBuffer,
  3600. const SDL_Rect *scissor)
  3601. {
  3602. D3D12CommandBuffer *d3d12CommandBuffer = (D3D12CommandBuffer *)commandBuffer;
  3603. D3D12_RECT scissorRect;
  3604. scissorRect.left = scissor->x;
  3605. scissorRect.top = scissor->y;
  3606. scissorRect.right = scissor->x + scissor->w;
  3607. scissorRect.bottom = scissor->y + scissor->h;
  3608. ID3D12GraphicsCommandList_RSSetScissorRects(d3d12CommandBuffer->graphicsCommandList, 1, &scissorRect);
  3609. }
  3610. static void D3D12_SetBlendConstants(
  3611. SDL_GPUCommandBuffer *commandBuffer,
  3612. SDL_FColor blendConstants)
  3613. {
  3614. D3D12CommandBuffer *d3d12CommandBuffer = (D3D12CommandBuffer *)commandBuffer;
  3615. FLOAT blendFactor[4] = { blendConstants.r, blendConstants.g, blendConstants.b, blendConstants.a };
  3616. ID3D12GraphicsCommandList_OMSetBlendFactor(d3d12CommandBuffer->graphicsCommandList, blendFactor);
  3617. }
  3618. static void D3D12_SetStencilReference(
  3619. SDL_GPUCommandBuffer *commandBuffer,
  3620. Uint8 reference
  3621. ) {
  3622. D3D12CommandBuffer *d3d12CommandBuffer = (D3D12CommandBuffer *)commandBuffer;
  3623. ID3D12GraphicsCommandList_OMSetStencilRef(d3d12CommandBuffer->graphicsCommandList, reference);
  3624. }
  3625. static D3D12TextureSubresource *D3D12_INTERNAL_FetchTextureSubresource(
  3626. D3D12TextureContainer *container,
  3627. Uint32 layer,
  3628. Uint32 level)
  3629. {
  3630. Uint32 index = D3D12_INTERNAL_CalcSubresource(
  3631. level,
  3632. layer,
  3633. container->header.info.num_levels);
  3634. return &container->activeTexture->subresources[index];
  3635. }
  3636. static void D3D12_INTERNAL_CycleActiveTexture(
  3637. D3D12Renderer *renderer,
  3638. D3D12TextureContainer *container)
  3639. {
  3640. D3D12Texture *texture;
  3641. // If a previously-cycled texture is available, we can use that.
  3642. for (Uint32 i = 0; i < container->textureCount; i += 1) {
  3643. texture = container->textures[i];
  3644. if (SDL_GetAtomicInt(&texture->referenceCount) == 0) {
  3645. container->activeTexture = texture;
  3646. return;
  3647. }
  3648. }
  3649. // No texture is available, generate a new one.
  3650. texture = D3D12_INTERNAL_CreateTexture(
  3651. renderer,
  3652. &container->header.info,
  3653. false,
  3654. container->debugName);
  3655. if (!texture) {
  3656. return;
  3657. }
  3658. EXPAND_ARRAY_IF_NEEDED(
  3659. container->textures,
  3660. D3D12Texture *,
  3661. container->textureCount + 1,
  3662. container->textureCapacity,
  3663. container->textureCapacity * 2);
  3664. container->textures[container->textureCount] = texture;
  3665. texture->container = container;
  3666. texture->containerIndex = container->textureCount;
  3667. container->textureCount += 1;
  3668. container->activeTexture = texture;
  3669. }
  3670. static D3D12TextureSubresource *D3D12_INTERNAL_PrepareTextureSubresourceForWrite(
  3671. D3D12CommandBuffer *commandBuffer,
  3672. D3D12TextureContainer *container,
  3673. Uint32 layer,
  3674. Uint32 level,
  3675. bool cycle,
  3676. D3D12_RESOURCE_STATES destinationUsageMode)
  3677. {
  3678. D3D12TextureSubresource *subresource = D3D12_INTERNAL_FetchTextureSubresource(
  3679. container,
  3680. layer,
  3681. level);
  3682. if (
  3683. container->canBeCycled &&
  3684. cycle &&
  3685. SDL_GetAtomicInt(&subresource->parent->referenceCount) > 0) {
  3686. D3D12_INTERNAL_CycleActiveTexture(
  3687. commandBuffer->renderer,
  3688. container);
  3689. subresource = D3D12_INTERNAL_FetchTextureSubresource(
  3690. container,
  3691. layer,
  3692. level);
  3693. }
  3694. D3D12_INTERNAL_TextureSubresourceTransitionFromDefaultUsage(
  3695. commandBuffer,
  3696. destinationUsageMode,
  3697. subresource);
  3698. return subresource;
  3699. }
  3700. static void D3D12_INTERNAL_CycleActiveBuffer(
  3701. D3D12Renderer *renderer,
  3702. D3D12BufferContainer *container)
  3703. {
  3704. // If a previously-cycled buffer is available, we can use that.
  3705. for (Uint32 i = 0; i < container->bufferCount; i += 1) {
  3706. D3D12Buffer *buffer = container->buffers[i];
  3707. if (SDL_GetAtomicInt(&buffer->referenceCount) == 0) {
  3708. container->activeBuffer = buffer;
  3709. return;
  3710. }
  3711. }
  3712. // No buffer handle is available, create a new one.
  3713. D3D12Buffer *buffer = D3D12_INTERNAL_CreateBuffer(
  3714. renderer,
  3715. container->usage,
  3716. container->size,
  3717. container->type,
  3718. container->debugName);
  3719. if (!buffer) {
  3720. return;
  3721. }
  3722. EXPAND_ARRAY_IF_NEEDED(
  3723. container->buffers,
  3724. D3D12Buffer *,
  3725. container->bufferCount + 1,
  3726. container->bufferCapacity,
  3727. container->bufferCapacity * 2);
  3728. container->buffers[container->bufferCount] = buffer;
  3729. buffer->container = container;
  3730. buffer->containerIndex = container->bufferCount;
  3731. container->bufferCount += 1;
  3732. container->activeBuffer = buffer;
  3733. if (renderer->debug_mode && container->debugName != NULL) {
  3734. D3D12_INTERNAL_SetResourceName(
  3735. renderer,
  3736. container->activeBuffer->handle,
  3737. container->debugName);
  3738. }
  3739. }
  3740. static D3D12Buffer *D3D12_INTERNAL_PrepareBufferForWrite(
  3741. D3D12CommandBuffer *commandBuffer,
  3742. D3D12BufferContainer *container,
  3743. bool cycle,
  3744. D3D12_RESOURCE_STATES destinationState)
  3745. {
  3746. if (
  3747. cycle &&
  3748. SDL_GetAtomicInt(&container->activeBuffer->referenceCount) > 0) {
  3749. D3D12_INTERNAL_CycleActiveBuffer(
  3750. commandBuffer->renderer,
  3751. container);
  3752. }
  3753. D3D12_INTERNAL_BufferTransitionFromDefaultUsage(
  3754. commandBuffer,
  3755. destinationState,
  3756. container->activeBuffer);
  3757. return container->activeBuffer;
  3758. }
  3759. static void D3D12_BeginRenderPass(
  3760. SDL_GPUCommandBuffer *commandBuffer,
  3761. const SDL_GPUColorTargetInfo *colorTargetInfos,
  3762. Uint32 numColorTargets,
  3763. const SDL_GPUDepthStencilTargetInfo *depthStencilTargetInfo)
  3764. {
  3765. D3D12CommandBuffer *d3d12CommandBuffer = (D3D12CommandBuffer *)commandBuffer;
  3766. Uint32 framebufferWidth = SDL_MAX_UINT32;
  3767. Uint32 framebufferHeight = SDL_MAX_UINT32;
  3768. for (Uint32 i = 0; i < numColorTargets; i += 1) {
  3769. D3D12TextureContainer *container = (D3D12TextureContainer *)colorTargetInfos[i].texture;
  3770. Uint32 h = container->header.info.height >> colorTargetInfos[i].mip_level;
  3771. Uint32 w = container->header.info.width >> colorTargetInfos[i].mip_level;
  3772. // The framebuffer cannot be larger than the smallest target.
  3773. if (w < framebufferWidth) {
  3774. framebufferWidth = w;
  3775. }
  3776. if (h < framebufferHeight) {
  3777. framebufferHeight = h;
  3778. }
  3779. }
  3780. if (depthStencilTargetInfo != NULL) {
  3781. D3D12TextureContainer *container = (D3D12TextureContainer *)depthStencilTargetInfo->texture;
  3782. Uint32 h = container->header.info.height >> depthStencilTargetInfo->mip_level;
  3783. Uint32 w = container->header.info.width >> depthStencilTargetInfo->mip_level;
  3784. // The framebuffer cannot be larger than the smallest target.
  3785. if (w < framebufferWidth) {
  3786. framebufferWidth = w;
  3787. }
  3788. if (h < framebufferHeight) {
  3789. framebufferHeight = h;
  3790. }
  3791. }
  3792. D3D12_CPU_DESCRIPTOR_HANDLE rtvs[MAX_COLOR_TARGET_BINDINGS];
  3793. for (Uint32 i = 0; i < numColorTargets; i += 1) {
  3794. D3D12TextureContainer *container = (D3D12TextureContainer *)colorTargetInfos[i].texture;
  3795. D3D12TextureSubresource *subresource = D3D12_INTERNAL_PrepareTextureSubresourceForWrite(
  3796. d3d12CommandBuffer,
  3797. container,
  3798. container->header.info.type == SDL_GPU_TEXTURETYPE_3D ? 0 : colorTargetInfos[i].layer_or_depth_plane,
  3799. colorTargetInfos[i].mip_level,
  3800. colorTargetInfos[i].cycle,
  3801. D3D12_RESOURCE_STATE_RENDER_TARGET);
  3802. Uint32 rtvIndex = container->header.info.type == SDL_GPU_TEXTURETYPE_3D ? colorTargetInfos[i].layer_or_depth_plane : 0;
  3803. D3D12_CPU_DESCRIPTOR_HANDLE rtv = subresource->rtvHandles[rtvIndex].cpuHandle;
  3804. if (colorTargetInfos[i].load_op == SDL_GPU_LOADOP_CLEAR) {
  3805. float clearColor[4];
  3806. clearColor[0] = colorTargetInfos[i].clear_color.r;
  3807. clearColor[1] = colorTargetInfos[i].clear_color.g;
  3808. clearColor[2] = colorTargetInfos[i].clear_color.b;
  3809. clearColor[3] = colorTargetInfos[i].clear_color.a;
  3810. ID3D12GraphicsCommandList_ClearRenderTargetView(
  3811. d3d12CommandBuffer->graphicsCommandList,
  3812. rtv,
  3813. clearColor,
  3814. 0,
  3815. NULL);
  3816. }
  3817. rtvs[i] = rtv;
  3818. d3d12CommandBuffer->colorTargetSubresources[i] = subresource;
  3819. D3D12_INTERNAL_TrackTexture(d3d12CommandBuffer, subresource->parent);
  3820. if (colorTargetInfos[i].store_op == SDL_GPU_STOREOP_RESOLVE || colorTargetInfos[i].store_op == SDL_GPU_STOREOP_RESOLVE_AND_STORE) {
  3821. D3D12TextureContainer *resolveContainer = (D3D12TextureContainer *)colorTargetInfos[i].resolve_texture;
  3822. D3D12TextureSubresource *resolveSubresource = D3D12_INTERNAL_PrepareTextureSubresourceForWrite(
  3823. d3d12CommandBuffer,
  3824. resolveContainer,
  3825. colorTargetInfos[i].resolve_layer,
  3826. colorTargetInfos[i].resolve_mip_level,
  3827. colorTargetInfos[i].cycle_resolve_texture,
  3828. D3D12_RESOURCE_STATE_RESOLVE_DEST);
  3829. d3d12CommandBuffer->colorResolveSubresources[i] = resolveSubresource;
  3830. D3D12_INTERNAL_TrackTexture(d3d12CommandBuffer, resolveSubresource->parent);
  3831. }
  3832. }
  3833. D3D12_CPU_DESCRIPTOR_HANDLE dsv;
  3834. if (depthStencilTargetInfo != NULL) {
  3835. D3D12TextureContainer *container = (D3D12TextureContainer *)depthStencilTargetInfo->texture;
  3836. D3D12TextureSubresource *subresource = D3D12_INTERNAL_PrepareTextureSubresourceForWrite(
  3837. d3d12CommandBuffer,
  3838. container,
  3839. depthStencilTargetInfo->layer,
  3840. depthStencilTargetInfo->mip_level,
  3841. depthStencilTargetInfo->cycle,
  3842. D3D12_RESOURCE_STATE_DEPTH_WRITE);
  3843. if (
  3844. depthStencilTargetInfo->load_op == SDL_GPU_LOADOP_CLEAR ||
  3845. depthStencilTargetInfo->stencil_load_op == SDL_GPU_LOADOP_CLEAR) {
  3846. D3D12_CLEAR_FLAGS clearFlags = (D3D12_CLEAR_FLAGS)0;
  3847. if (depthStencilTargetInfo->load_op == SDL_GPU_LOADOP_CLEAR) {
  3848. clearFlags |= D3D12_CLEAR_FLAG_DEPTH;
  3849. }
  3850. if (depthStencilTargetInfo->stencil_load_op == SDL_GPU_LOADOP_CLEAR) {
  3851. clearFlags |= D3D12_CLEAR_FLAG_STENCIL;
  3852. }
  3853. ID3D12GraphicsCommandList_ClearDepthStencilView(
  3854. d3d12CommandBuffer->graphicsCommandList,
  3855. subresource->dsvHandle.cpuHandle,
  3856. clearFlags,
  3857. depthStencilTargetInfo->clear_depth,
  3858. depthStencilTargetInfo->clear_stencil,
  3859. 0,
  3860. NULL);
  3861. }
  3862. dsv = subresource->dsvHandle.cpuHandle;
  3863. d3d12CommandBuffer->depthStencilTextureSubresource = subresource;
  3864. D3D12_INTERNAL_TrackTexture(d3d12CommandBuffer, subresource->parent);
  3865. }
  3866. ID3D12GraphicsCommandList_OMSetRenderTargets(
  3867. d3d12CommandBuffer->graphicsCommandList,
  3868. numColorTargets,
  3869. rtvs,
  3870. false,
  3871. (depthStencilTargetInfo == NULL) ? NULL : &dsv);
  3872. // Set sensible default states
  3873. SDL_GPUViewport defaultViewport;
  3874. defaultViewport.x = 0;
  3875. defaultViewport.y = 0;
  3876. defaultViewport.w = (float)framebufferWidth;
  3877. defaultViewport.h = (float)framebufferHeight;
  3878. defaultViewport.min_depth = 0;
  3879. defaultViewport.max_depth = 1;
  3880. D3D12_SetViewport(
  3881. commandBuffer,
  3882. &defaultViewport);
  3883. SDL_Rect defaultScissor;
  3884. defaultScissor.x = 0;
  3885. defaultScissor.y = 0;
  3886. defaultScissor.w = (Sint32)framebufferWidth;
  3887. defaultScissor.h = (Sint32)framebufferHeight;
  3888. D3D12_SetScissor(
  3889. commandBuffer,
  3890. &defaultScissor);
  3891. D3D12_SetStencilReference(
  3892. commandBuffer,
  3893. 0);
  3894. SDL_FColor blendConstants;
  3895. blendConstants.r = 1.0f;
  3896. blendConstants.g = 1.0f;
  3897. blendConstants.b = 1.0f;
  3898. blendConstants.a = 1.0f;
  3899. D3D12_SetBlendConstants(
  3900. commandBuffer,
  3901. blendConstants);
  3902. }
  3903. static void D3D12_INTERNAL_TrackUniformBuffer(
  3904. D3D12CommandBuffer *commandBuffer,
  3905. D3D12UniformBuffer *uniformBuffer)
  3906. {
  3907. Uint32 i;
  3908. for (i = 0; i < commandBuffer->usedUniformBufferCount; i += 1) {
  3909. if (commandBuffer->usedUniformBuffers[i] == uniformBuffer) {
  3910. return;
  3911. }
  3912. }
  3913. if (commandBuffer->usedUniformBufferCount == commandBuffer->usedUniformBufferCapacity) {
  3914. commandBuffer->usedUniformBufferCapacity += 1;
  3915. commandBuffer->usedUniformBuffers = (D3D12UniformBuffer **)SDL_realloc(
  3916. commandBuffer->usedUniformBuffers,
  3917. commandBuffer->usedUniformBufferCapacity * sizeof(D3D12UniformBuffer *));
  3918. }
  3919. commandBuffer->usedUniformBuffers[commandBuffer->usedUniformBufferCount] = uniformBuffer;
  3920. commandBuffer->usedUniformBufferCount += 1;
  3921. D3D12_INTERNAL_TrackBuffer(
  3922. commandBuffer,
  3923. uniformBuffer->buffer);
  3924. }
  3925. static D3D12UniformBuffer *D3D12_INTERNAL_AcquireUniformBufferFromPool(
  3926. D3D12CommandBuffer *commandBuffer)
  3927. {
  3928. D3D12Renderer *renderer = commandBuffer->renderer;
  3929. D3D12UniformBuffer *uniformBuffer;
  3930. SDL_LockMutex(renderer->acquireUniformBufferLock);
  3931. if (renderer->uniformBufferPoolCount > 0) {
  3932. uniformBuffer = renderer->uniformBufferPool[renderer->uniformBufferPoolCount - 1];
  3933. renderer->uniformBufferPoolCount -= 1;
  3934. } else {
  3935. uniformBuffer = (D3D12UniformBuffer *)SDL_calloc(1, sizeof(D3D12UniformBuffer));
  3936. if (!uniformBuffer) {
  3937. SDL_UnlockMutex(renderer->acquireUniformBufferLock);
  3938. return NULL;
  3939. }
  3940. uniformBuffer->buffer = D3D12_INTERNAL_CreateBuffer(
  3941. renderer,
  3942. 0,
  3943. UNIFORM_BUFFER_SIZE,
  3944. D3D12_BUFFER_TYPE_UNIFORM,
  3945. NULL);
  3946. if (!uniformBuffer->buffer) {
  3947. SDL_UnlockMutex(renderer->acquireUniformBufferLock);
  3948. return NULL;
  3949. }
  3950. }
  3951. SDL_UnlockMutex(renderer->acquireUniformBufferLock);
  3952. uniformBuffer->drawOffset = 0;
  3953. uniformBuffer->writeOffset = 0;
  3954. HRESULT res = ID3D12Resource_Map(
  3955. uniformBuffer->buffer->handle,
  3956. 0,
  3957. NULL,
  3958. (void **)&uniformBuffer->buffer->mapPointer);
  3959. CHECK_D3D12_ERROR_AND_RETURN("Failed to map buffer pool!", NULL);
  3960. D3D12_INTERNAL_TrackUniformBuffer(commandBuffer, uniformBuffer);
  3961. return uniformBuffer;
  3962. }
  3963. static void D3D12_INTERNAL_ReturnUniformBufferToPool(
  3964. D3D12Renderer *renderer,
  3965. D3D12UniformBuffer *uniformBuffer)
  3966. {
  3967. if (renderer->uniformBufferPoolCount >= renderer->uniformBufferPoolCapacity) {
  3968. renderer->uniformBufferPoolCapacity *= 2;
  3969. renderer->uniformBufferPool = (D3D12UniformBuffer **)SDL_realloc(
  3970. renderer->uniformBufferPool,
  3971. renderer->uniformBufferPoolCapacity * sizeof(D3D12UniformBuffer *));
  3972. }
  3973. renderer->uniformBufferPool[renderer->uniformBufferPoolCount] = uniformBuffer;
  3974. renderer->uniformBufferPoolCount += 1;
  3975. }
  3976. static void D3D12_INTERNAL_PushUniformData(
  3977. D3D12CommandBuffer *commandBuffer,
  3978. SDL_GPUShaderStage shaderStage,
  3979. Uint32 slotIndex,
  3980. const void *data,
  3981. Uint32 length)
  3982. {
  3983. D3D12UniformBuffer *uniformBuffer;
  3984. Uint32 blockSize;
  3985. if (shaderStage == SDL_GPU_SHADERSTAGE_VERTEX) {
  3986. if (commandBuffer->vertexUniformBuffers[slotIndex] == NULL) {
  3987. commandBuffer->vertexUniformBuffers[slotIndex] = D3D12_INTERNAL_AcquireUniformBufferFromPool(
  3988. commandBuffer);
  3989. }
  3990. uniformBuffer = commandBuffer->vertexUniformBuffers[slotIndex];
  3991. } else if (shaderStage == SDL_GPU_SHADERSTAGE_FRAGMENT) {
  3992. if (commandBuffer->fragmentUniformBuffers[slotIndex] == NULL) {
  3993. commandBuffer->fragmentUniformBuffers[slotIndex] = D3D12_INTERNAL_AcquireUniformBufferFromPool(
  3994. commandBuffer);
  3995. }
  3996. uniformBuffer = commandBuffer->fragmentUniformBuffers[slotIndex];
  3997. } else if (shaderStage == SDL_GPU_SHADERSTAGE_COMPUTE) {
  3998. if (commandBuffer->computeUniformBuffers[slotIndex] == NULL) {
  3999. commandBuffer->computeUniformBuffers[slotIndex] = D3D12_INTERNAL_AcquireUniformBufferFromPool(
  4000. commandBuffer);
  4001. }
  4002. uniformBuffer = commandBuffer->computeUniformBuffers[slotIndex];
  4003. } else {
  4004. SDL_LogError(SDL_LOG_CATEGORY_GPU, "Unrecognized shader stage!");
  4005. return;
  4006. }
  4007. blockSize =
  4008. D3D12_INTERNAL_Align(
  4009. length,
  4010. 256);
  4011. // If there is no more room, acquire a new uniform buffer
  4012. if (uniformBuffer->writeOffset + blockSize >= UNIFORM_BUFFER_SIZE) {
  4013. ID3D12Resource_Unmap(
  4014. uniformBuffer->buffer->handle,
  4015. 0,
  4016. NULL);
  4017. uniformBuffer->buffer->mapPointer = NULL;
  4018. uniformBuffer = D3D12_INTERNAL_AcquireUniformBufferFromPool(commandBuffer);
  4019. uniformBuffer->drawOffset = 0;
  4020. uniformBuffer->writeOffset = 0;
  4021. if (shaderStage == SDL_GPU_SHADERSTAGE_VERTEX) {
  4022. commandBuffer->vertexUniformBuffers[slotIndex] = uniformBuffer;
  4023. } else if (shaderStage == SDL_GPU_SHADERSTAGE_FRAGMENT) {
  4024. commandBuffer->fragmentUniformBuffers[slotIndex] = uniformBuffer;
  4025. } else if (shaderStage == SDL_GPU_SHADERSTAGE_COMPUTE) {
  4026. commandBuffer->computeUniformBuffers[slotIndex] = uniformBuffer;
  4027. } else {
  4028. SDL_LogError(SDL_LOG_CATEGORY_GPU, "Unrecognized shader stage!");
  4029. }
  4030. }
  4031. uniformBuffer->drawOffset = uniformBuffer->writeOffset;
  4032. SDL_memcpy(
  4033. (Uint8 *)uniformBuffer->buffer->mapPointer + uniformBuffer->writeOffset,
  4034. data,
  4035. length);
  4036. uniformBuffer->writeOffset += blockSize;
  4037. if (shaderStage == SDL_GPU_SHADERSTAGE_VERTEX) {
  4038. commandBuffer->needVertexUniformBufferBind[slotIndex] = true;
  4039. } else if (shaderStage == SDL_GPU_SHADERSTAGE_FRAGMENT) {
  4040. commandBuffer->needFragmentUniformBufferBind[slotIndex] = true;
  4041. } else if (shaderStage == SDL_GPU_SHADERSTAGE_COMPUTE) {
  4042. commandBuffer->needComputeUniformBufferBind[slotIndex] = true;
  4043. } else {
  4044. SDL_LogError(SDL_LOG_CATEGORY_GPU, "Unrecognized shader stage!");
  4045. }
  4046. }
  4047. static void D3D12_BindGraphicsPipeline(
  4048. SDL_GPUCommandBuffer *commandBuffer,
  4049. SDL_GPUGraphicsPipeline *graphicsPipeline)
  4050. {
  4051. D3D12CommandBuffer *d3d12CommandBuffer = (D3D12CommandBuffer *)commandBuffer;
  4052. D3D12GraphicsPipeline *pipeline = (D3D12GraphicsPipeline *)graphicsPipeline;
  4053. Uint32 i;
  4054. d3d12CommandBuffer->currentGraphicsPipeline = pipeline;
  4055. // Set the pipeline state
  4056. ID3D12GraphicsCommandList_SetPipelineState(d3d12CommandBuffer->graphicsCommandList, pipeline->pipelineState);
  4057. ID3D12GraphicsCommandList_SetGraphicsRootSignature(d3d12CommandBuffer->graphicsCommandList, pipeline->rootSignature->handle);
  4058. ID3D12GraphicsCommandList_IASetPrimitiveTopology(d3d12CommandBuffer->graphicsCommandList, SDLToD3D12_PrimitiveType[pipeline->primitiveType]);
  4059. // Mark that bindings are needed
  4060. d3d12CommandBuffer->needVertexSamplerBind = true;
  4061. d3d12CommandBuffer->needVertexStorageTextureBind = true;
  4062. d3d12CommandBuffer->needVertexStorageBufferBind = true;
  4063. d3d12CommandBuffer->needFragmentSamplerBind = true;
  4064. d3d12CommandBuffer->needFragmentStorageTextureBind = true;
  4065. d3d12CommandBuffer->needFragmentStorageBufferBind = true;
  4066. for (i = 0; i < MAX_UNIFORM_BUFFERS_PER_STAGE; i += 1) {
  4067. d3d12CommandBuffer->needVertexUniformBufferBind[i] = true;
  4068. d3d12CommandBuffer->needFragmentUniformBufferBind[i] = true;
  4069. }
  4070. for (i = 0; i < pipeline->header.num_vertex_uniform_buffers; i += 1) {
  4071. if (d3d12CommandBuffer->vertexUniformBuffers[i] == NULL) {
  4072. d3d12CommandBuffer->vertexUniformBuffers[i] = D3D12_INTERNAL_AcquireUniformBufferFromPool(
  4073. d3d12CommandBuffer);
  4074. }
  4075. }
  4076. for (i = 0; i < pipeline->header.num_fragment_uniform_buffers; i += 1) {
  4077. if (d3d12CommandBuffer->fragmentUniformBuffers[i] == NULL) {
  4078. d3d12CommandBuffer->fragmentUniformBuffers[i] = D3D12_INTERNAL_AcquireUniformBufferFromPool(
  4079. d3d12CommandBuffer);
  4080. }
  4081. }
  4082. D3D12_INTERNAL_TrackGraphicsPipeline(d3d12CommandBuffer, pipeline);
  4083. }
  4084. static void D3D12_BindVertexBuffers(
  4085. SDL_GPUCommandBuffer *commandBuffer,
  4086. Uint32 firstSlot,
  4087. const SDL_GPUBufferBinding *bindings,
  4088. Uint32 numBindings)
  4089. {
  4090. D3D12CommandBuffer *d3d12CommandBuffer = (D3D12CommandBuffer *)commandBuffer;
  4091. for (Uint32 i = 0; i < numBindings; i += 1) {
  4092. D3D12Buffer *currentBuffer = ((D3D12BufferContainer *)bindings[i].buffer)->activeBuffer;
  4093. if (d3d12CommandBuffer->vertexBuffers[firstSlot + i] != currentBuffer || d3d12CommandBuffer->vertexBufferOffsets[firstSlot + i] != bindings[i].offset) {
  4094. D3D12_INTERNAL_TrackBuffer(d3d12CommandBuffer, currentBuffer);
  4095. d3d12CommandBuffer->vertexBuffers[firstSlot + i] = currentBuffer;
  4096. d3d12CommandBuffer->vertexBufferOffsets[firstSlot + i] = bindings[i].offset;
  4097. d3d12CommandBuffer->needVertexBufferBind = true;
  4098. }
  4099. }
  4100. d3d12CommandBuffer->vertexBufferCount =
  4101. SDL_max(d3d12CommandBuffer->vertexBufferCount, firstSlot + numBindings);
  4102. }
  4103. static void D3D12_BindIndexBuffer(
  4104. SDL_GPUCommandBuffer *commandBuffer,
  4105. const SDL_GPUBufferBinding *binding,
  4106. SDL_GPUIndexElementSize indexElementSize)
  4107. {
  4108. D3D12CommandBuffer *d3d12CommandBuffer = (D3D12CommandBuffer *)commandBuffer;
  4109. D3D12Buffer *buffer = ((D3D12BufferContainer *)binding->buffer)->activeBuffer;
  4110. D3D12_INDEX_BUFFER_VIEW view;
  4111. D3D12_INTERNAL_TrackBuffer(d3d12CommandBuffer, buffer);
  4112. view.BufferLocation = buffer->virtualAddress + binding->offset;
  4113. view.SizeInBytes = buffer->container->size - binding->offset;
  4114. view.Format =
  4115. indexElementSize == SDL_GPU_INDEXELEMENTSIZE_16BIT ? DXGI_FORMAT_R16_UINT : DXGI_FORMAT_R32_UINT;
  4116. ID3D12GraphicsCommandList_IASetIndexBuffer(
  4117. d3d12CommandBuffer->graphicsCommandList,
  4118. &view);
  4119. }
  4120. static void D3D12_BindVertexSamplers(
  4121. SDL_GPUCommandBuffer *commandBuffer,
  4122. Uint32 firstSlot,
  4123. const SDL_GPUTextureSamplerBinding *textureSamplerBindings,
  4124. Uint32 numBindings)
  4125. {
  4126. D3D12CommandBuffer *d3d12CommandBuffer = (D3D12CommandBuffer *)commandBuffer;
  4127. for (Uint32 i = 0; i < numBindings; i += 1) {
  4128. D3D12TextureContainer *container = (D3D12TextureContainer *)textureSamplerBindings[i].texture;
  4129. D3D12Sampler *sampler = (D3D12Sampler *)textureSamplerBindings[i].sampler;
  4130. if (d3d12CommandBuffer->vertexSamplerDescriptorHandles[firstSlot + i].ptr != sampler->handle.cpuHandle.ptr) {
  4131. D3D12_INTERNAL_TrackSampler(
  4132. d3d12CommandBuffer,
  4133. sampler);
  4134. d3d12CommandBuffer->vertexSamplerDescriptorHandles[firstSlot + i] = sampler->handle.cpuHandle;
  4135. d3d12CommandBuffer->needVertexSamplerBind = true;
  4136. }
  4137. if (d3d12CommandBuffer->vertexSamplerTextureDescriptorHandles[firstSlot + i].ptr != container->activeTexture->srvHandle.cpuHandle.ptr) {
  4138. D3D12_INTERNAL_TrackTexture(
  4139. d3d12CommandBuffer,
  4140. container->activeTexture);
  4141. d3d12CommandBuffer->vertexSamplerTextureDescriptorHandles[firstSlot + i] = container->activeTexture->srvHandle.cpuHandle;
  4142. d3d12CommandBuffer->needVertexSamplerBind = true;
  4143. }
  4144. }
  4145. }
  4146. static void D3D12_BindVertexStorageTextures(
  4147. SDL_GPUCommandBuffer *commandBuffer,
  4148. Uint32 firstSlot,
  4149. SDL_GPUTexture *const *storageTextures,
  4150. Uint32 numBindings)
  4151. {
  4152. D3D12CommandBuffer *d3d12CommandBuffer = (D3D12CommandBuffer *)commandBuffer;
  4153. for (Uint32 i = 0; i < numBindings; i += 1) {
  4154. D3D12TextureContainer *container = (D3D12TextureContainer *)storageTextures[i];
  4155. D3D12Texture *texture = container->activeTexture;
  4156. if (d3d12CommandBuffer->vertexStorageTextureDescriptorHandles[firstSlot + i].ptr != texture->srvHandle.cpuHandle.ptr) {
  4157. D3D12_INTERNAL_TrackTexture(d3d12CommandBuffer, texture);
  4158. d3d12CommandBuffer->vertexStorageTextureDescriptorHandles[firstSlot + i] = texture->srvHandle.cpuHandle;
  4159. d3d12CommandBuffer->needVertexStorageTextureBind = true;
  4160. }
  4161. }
  4162. }
  4163. static void D3D12_BindVertexStorageBuffers(
  4164. SDL_GPUCommandBuffer *commandBuffer,
  4165. Uint32 firstSlot,
  4166. SDL_GPUBuffer *const *storageBuffers,
  4167. Uint32 numBindings)
  4168. {
  4169. D3D12CommandBuffer *d3d12CommandBuffer = (D3D12CommandBuffer *)commandBuffer;
  4170. for (Uint32 i = 0; i < numBindings; i += 1) {
  4171. D3D12BufferContainer *container = (D3D12BufferContainer *)storageBuffers[i];
  4172. if (d3d12CommandBuffer->vertexStorageBufferDescriptorHandles[firstSlot + i].ptr != container->activeBuffer->srvDescriptor.cpuHandle.ptr) {
  4173. D3D12_INTERNAL_TrackBuffer(
  4174. d3d12CommandBuffer,
  4175. container->activeBuffer);
  4176. d3d12CommandBuffer->vertexStorageBufferDescriptorHandles[firstSlot + i] = container->activeBuffer->srvDescriptor.cpuHandle;
  4177. d3d12CommandBuffer->needVertexStorageBufferBind = true;
  4178. }
  4179. }
  4180. }
  4181. static void D3D12_BindFragmentSamplers(
  4182. SDL_GPUCommandBuffer *commandBuffer,
  4183. Uint32 firstSlot,
  4184. const SDL_GPUTextureSamplerBinding *textureSamplerBindings,
  4185. Uint32 numBindings)
  4186. {
  4187. D3D12CommandBuffer *d3d12CommandBuffer = (D3D12CommandBuffer *)commandBuffer;
  4188. for (Uint32 i = 0; i < numBindings; i += 1) {
  4189. D3D12TextureContainer *container = (D3D12TextureContainer *)textureSamplerBindings[i].texture;
  4190. D3D12Sampler *sampler = (D3D12Sampler *)textureSamplerBindings[i].sampler;
  4191. if (d3d12CommandBuffer->fragmentSamplerDescriptorHandles[firstSlot + i].ptr != sampler->handle.cpuHandle.ptr) {
  4192. D3D12_INTERNAL_TrackSampler(
  4193. d3d12CommandBuffer,
  4194. sampler);
  4195. d3d12CommandBuffer->fragmentSamplerDescriptorHandles[firstSlot + i] = sampler->handle.cpuHandle;
  4196. d3d12CommandBuffer->needFragmentSamplerBind = true;
  4197. }
  4198. if (d3d12CommandBuffer->fragmentSamplerTextureDescriptorHandles[firstSlot + i].ptr != container->activeTexture->srvHandle.cpuHandle.ptr) {
  4199. D3D12_INTERNAL_TrackTexture(
  4200. d3d12CommandBuffer,
  4201. container->activeTexture);
  4202. d3d12CommandBuffer->fragmentSamplerTextureDescriptorHandles[firstSlot + i] = container->activeTexture->srvHandle.cpuHandle;
  4203. d3d12CommandBuffer->needFragmentSamplerBind = true;
  4204. }
  4205. }
  4206. }
  4207. static void D3D12_BindFragmentStorageTextures(
  4208. SDL_GPUCommandBuffer *commandBuffer,
  4209. Uint32 firstSlot,
  4210. SDL_GPUTexture *const *storageTextures,
  4211. Uint32 numBindings)
  4212. {
  4213. D3D12CommandBuffer *d3d12CommandBuffer = (D3D12CommandBuffer *)commandBuffer;
  4214. for (Uint32 i = 0; i < numBindings; i += 1) {
  4215. D3D12TextureContainer *container = (D3D12TextureContainer *)storageTextures[i];
  4216. D3D12Texture *texture = container->activeTexture;
  4217. if (d3d12CommandBuffer->fragmentStorageTextureDescriptorHandles[firstSlot + i].ptr != texture->srvHandle.cpuHandle.ptr) {
  4218. D3D12_INTERNAL_TrackTexture(d3d12CommandBuffer, texture);
  4219. d3d12CommandBuffer->fragmentStorageTextureDescriptorHandles[firstSlot + i] = texture->srvHandle.cpuHandle;
  4220. d3d12CommandBuffer->needFragmentStorageTextureBind = true;
  4221. }
  4222. }
  4223. }
  4224. static void D3D12_BindFragmentStorageBuffers(
  4225. SDL_GPUCommandBuffer *commandBuffer,
  4226. Uint32 firstSlot,
  4227. SDL_GPUBuffer *const *storageBuffers,
  4228. Uint32 numBindings)
  4229. {
  4230. D3D12CommandBuffer *d3d12CommandBuffer = (D3D12CommandBuffer *)commandBuffer;
  4231. for (Uint32 i = 0; i < numBindings; i += 1) {
  4232. D3D12BufferContainer *container = (D3D12BufferContainer *)storageBuffers[i];
  4233. if (d3d12CommandBuffer->fragmentStorageBufferDescriptorHandles[firstSlot + i].ptr != container->activeBuffer->srvDescriptor.cpuHandle.ptr) {
  4234. D3D12_INTERNAL_TrackBuffer(
  4235. d3d12CommandBuffer,
  4236. container->activeBuffer);
  4237. d3d12CommandBuffer->fragmentStorageBufferDescriptorHandles[firstSlot + i] = container->activeBuffer->srvDescriptor.cpuHandle;
  4238. d3d12CommandBuffer->needFragmentStorageBufferBind = true;
  4239. }
  4240. }
  4241. }
  4242. static void D3D12_PushVertexUniformData(
  4243. SDL_GPUCommandBuffer *commandBuffer,
  4244. Uint32 slotIndex,
  4245. const void *data,
  4246. Uint32 length)
  4247. {
  4248. D3D12CommandBuffer *d3d12CommandBuffer = (D3D12CommandBuffer *)commandBuffer;
  4249. D3D12_INTERNAL_PushUniformData(
  4250. d3d12CommandBuffer,
  4251. SDL_GPU_SHADERSTAGE_VERTEX,
  4252. slotIndex,
  4253. data,
  4254. length);
  4255. }
  4256. static void D3D12_PushFragmentUniformData(
  4257. SDL_GPUCommandBuffer *commandBuffer,
  4258. Uint32 slotIndex,
  4259. const void *data,
  4260. Uint32 length)
  4261. {
  4262. D3D12CommandBuffer *d3d12CommandBuffer = (D3D12CommandBuffer *)commandBuffer;
  4263. D3D12_INTERNAL_PushUniformData(
  4264. d3d12CommandBuffer,
  4265. SDL_GPU_SHADERSTAGE_FRAGMENT,
  4266. slotIndex,
  4267. data,
  4268. length);
  4269. }
  4270. static void D3D12_INTERNAL_SetGPUDescriptorHeaps(D3D12CommandBuffer *commandBuffer)
  4271. {
  4272. ID3D12DescriptorHeap *heaps[2];
  4273. D3D12DescriptorHeap *viewHeap;
  4274. D3D12DescriptorHeap *samplerHeap;
  4275. viewHeap = D3D12_INTERNAL_AcquireGPUDescriptorHeapFromPool(commandBuffer, D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV);
  4276. samplerHeap = D3D12_INTERNAL_AcquireGPUDescriptorHeapFromPool(commandBuffer, D3D12_DESCRIPTOR_HEAP_TYPE_SAMPLER);
  4277. commandBuffer->gpuDescriptorHeaps[0] = viewHeap;
  4278. commandBuffer->gpuDescriptorHeaps[1] = samplerHeap;
  4279. heaps[0] = viewHeap->handle;
  4280. heaps[1] = samplerHeap->handle;
  4281. ID3D12GraphicsCommandList_SetDescriptorHeaps(
  4282. commandBuffer->graphicsCommandList,
  4283. 2,
  4284. heaps);
  4285. }
  4286. static void D3D12_INTERNAL_WriteGPUDescriptors(
  4287. D3D12CommandBuffer *commandBuffer,
  4288. D3D12_DESCRIPTOR_HEAP_TYPE heapType,
  4289. D3D12_CPU_DESCRIPTOR_HANDLE *resourceDescriptorHandles,
  4290. Uint32 resourceHandleCount,
  4291. D3D12_GPU_DESCRIPTOR_HANDLE *gpuBaseDescriptor)
  4292. {
  4293. D3D12DescriptorHeap *heap;
  4294. D3D12_CPU_DESCRIPTOR_HANDLE gpuHeapCpuHandle;
  4295. /* Descriptor overflow, acquire new heaps */
  4296. if (commandBuffer->gpuDescriptorHeaps[heapType]->currentDescriptorIndex >= commandBuffer->gpuDescriptorHeaps[heapType]->maxDescriptors) {
  4297. D3D12_INTERNAL_SetGPUDescriptorHeaps(commandBuffer);
  4298. }
  4299. heap = commandBuffer->gpuDescriptorHeaps[heapType];
  4300. // FIXME: need to error on overflow
  4301. gpuHeapCpuHandle.ptr = heap->descriptorHeapCPUStart.ptr + (heap->currentDescriptorIndex * heap->descriptorSize);
  4302. gpuBaseDescriptor->ptr = heap->descriptorHeapGPUStart.ptr + (heap->currentDescriptorIndex * heap->descriptorSize);
  4303. for (Uint32 i = 0; i < resourceHandleCount; i += 1) {
  4304. // This will crash the driver if it gets a null handle! Cool!
  4305. if (resourceDescriptorHandles[i].ptr != 0)
  4306. {
  4307. ID3D12Device_CopyDescriptorsSimple(
  4308. commandBuffer->renderer->device,
  4309. 1,
  4310. gpuHeapCpuHandle,
  4311. resourceDescriptorHandles[i],
  4312. heapType);
  4313. heap->currentDescriptorIndex += 1;
  4314. gpuHeapCpuHandle.ptr += heap->descriptorSize;
  4315. }
  4316. }
  4317. }
  4318. static void D3D12_INTERNAL_BindGraphicsResources(
  4319. D3D12CommandBuffer *commandBuffer)
  4320. {
  4321. D3D12GraphicsPipeline *graphicsPipeline = commandBuffer->currentGraphicsPipeline;
  4322. /* Acquire GPU descriptor heaps if we haven't yet */
  4323. if (commandBuffer->gpuDescriptorHeaps[0] == NULL) {
  4324. D3D12_INTERNAL_SetGPUDescriptorHeaps(commandBuffer);
  4325. }
  4326. D3D12_CPU_DESCRIPTOR_HANDLE cpuHandles[MAX_TEXTURE_SAMPLERS_PER_STAGE];
  4327. D3D12_GPU_DESCRIPTOR_HANDLE gpuDescriptorHandle;
  4328. D3D12_VERTEX_BUFFER_VIEW vertexBufferViews[MAX_VERTEX_BUFFERS];
  4329. if (commandBuffer->needVertexBufferBind) {
  4330. for (Uint32 i = 0; i < commandBuffer->vertexBufferCount; i += 1) {
  4331. vertexBufferViews[i].BufferLocation = commandBuffer->vertexBuffers[i]->virtualAddress + commandBuffer->vertexBufferOffsets[i];
  4332. vertexBufferViews[i].SizeInBytes = commandBuffer->vertexBuffers[i]->container->size - commandBuffer->vertexBufferOffsets[i];
  4333. vertexBufferViews[i].StrideInBytes = graphicsPipeline->vertexStrides[i];
  4334. }
  4335. ID3D12GraphicsCommandList_IASetVertexBuffers(
  4336. commandBuffer->graphicsCommandList,
  4337. 0,
  4338. commandBuffer->vertexBufferCount,
  4339. vertexBufferViews);
  4340. commandBuffer->needVertexBufferBind = false;
  4341. }
  4342. if (commandBuffer->needVertexSamplerBind) {
  4343. if (graphicsPipeline->header.num_vertex_samplers > 0) {
  4344. for (Uint32 i = 0; i < graphicsPipeline->header.num_vertex_samplers; i += 1) {
  4345. cpuHandles[i] = commandBuffer->vertexSamplerDescriptorHandles[i];
  4346. }
  4347. D3D12_INTERNAL_WriteGPUDescriptors(
  4348. commandBuffer,
  4349. D3D12_DESCRIPTOR_HEAP_TYPE_SAMPLER,
  4350. cpuHandles,
  4351. graphicsPipeline->header.num_vertex_samplers,
  4352. &gpuDescriptorHandle);
  4353. ID3D12GraphicsCommandList_SetGraphicsRootDescriptorTable(
  4354. commandBuffer->graphicsCommandList,
  4355. graphicsPipeline->rootSignature->vertexSamplerRootIndex,
  4356. gpuDescriptorHandle);
  4357. for (Uint32 i = 0; i < graphicsPipeline->header.num_vertex_samplers; i += 1) {
  4358. cpuHandles[i] = commandBuffer->vertexSamplerTextureDescriptorHandles[i];
  4359. }
  4360. D3D12_INTERNAL_WriteGPUDescriptors(
  4361. commandBuffer,
  4362. D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV,
  4363. cpuHandles,
  4364. graphicsPipeline->header.num_vertex_samplers,
  4365. &gpuDescriptorHandle);
  4366. ID3D12GraphicsCommandList_SetGraphicsRootDescriptorTable(
  4367. commandBuffer->graphicsCommandList,
  4368. graphicsPipeline->rootSignature->vertexSamplerTextureRootIndex,
  4369. gpuDescriptorHandle);
  4370. }
  4371. commandBuffer->needVertexSamplerBind = false;
  4372. }
  4373. if (commandBuffer->needVertexStorageTextureBind) {
  4374. if (graphicsPipeline->header.num_vertex_storage_textures > 0) {
  4375. for (Uint32 i = 0; i < graphicsPipeline->header.num_vertex_storage_textures; i += 1) {
  4376. cpuHandles[i] = commandBuffer->vertexStorageTextureDescriptorHandles[i];
  4377. }
  4378. D3D12_INTERNAL_WriteGPUDescriptors(
  4379. commandBuffer,
  4380. D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV,
  4381. cpuHandles,
  4382. graphicsPipeline->header.num_vertex_storage_textures,
  4383. &gpuDescriptorHandle);
  4384. ID3D12GraphicsCommandList_SetGraphicsRootDescriptorTable(
  4385. commandBuffer->graphicsCommandList,
  4386. graphicsPipeline->rootSignature->vertexStorageTextureRootIndex,
  4387. gpuDescriptorHandle);
  4388. }
  4389. commandBuffer->needVertexStorageTextureBind = false;
  4390. }
  4391. if (commandBuffer->needVertexStorageBufferBind) {
  4392. if (graphicsPipeline->header.num_vertex_storage_buffers > 0) {
  4393. for (Uint32 i = 0; i < graphicsPipeline->header.num_vertex_storage_buffers; i += 1) {
  4394. cpuHandles[i] = commandBuffer->vertexStorageBufferDescriptorHandles[i];
  4395. }
  4396. D3D12_INTERNAL_WriteGPUDescriptors(
  4397. commandBuffer,
  4398. D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV,
  4399. cpuHandles,
  4400. graphicsPipeline->header.num_vertex_storage_buffers,
  4401. &gpuDescriptorHandle);
  4402. ID3D12GraphicsCommandList_SetGraphicsRootDescriptorTable(
  4403. commandBuffer->graphicsCommandList,
  4404. graphicsPipeline->rootSignature->vertexStorageBufferRootIndex,
  4405. gpuDescriptorHandle);
  4406. }
  4407. commandBuffer->needVertexStorageBufferBind = false;
  4408. }
  4409. for (Uint32 i = 0; i < MAX_UNIFORM_BUFFERS_PER_STAGE; i += 1) {
  4410. if (commandBuffer->needVertexUniformBufferBind[i]) {
  4411. if (graphicsPipeline->header.num_vertex_uniform_buffers > i) {
  4412. ID3D12GraphicsCommandList_SetGraphicsRootConstantBufferView(
  4413. commandBuffer->graphicsCommandList,
  4414. graphicsPipeline->rootSignature->vertexUniformBufferRootIndex[i],
  4415. commandBuffer->vertexUniformBuffers[i]->buffer->virtualAddress + commandBuffer->vertexUniformBuffers[i]->drawOffset);
  4416. }
  4417. commandBuffer->needVertexUniformBufferBind[i] = false;
  4418. }
  4419. }
  4420. if (commandBuffer->needFragmentSamplerBind) {
  4421. if (graphicsPipeline->header.num_fragment_samplers > 0) {
  4422. for (Uint32 i = 0; i < graphicsPipeline->header.num_fragment_samplers; i += 1) {
  4423. cpuHandles[i] = commandBuffer->fragmentSamplerDescriptorHandles[i];
  4424. }
  4425. D3D12_INTERNAL_WriteGPUDescriptors(
  4426. commandBuffer,
  4427. D3D12_DESCRIPTOR_HEAP_TYPE_SAMPLER,
  4428. cpuHandles,
  4429. graphicsPipeline->header.num_fragment_samplers,
  4430. &gpuDescriptorHandle);
  4431. ID3D12GraphicsCommandList_SetGraphicsRootDescriptorTable(
  4432. commandBuffer->graphicsCommandList,
  4433. graphicsPipeline->rootSignature->fragmentSamplerRootIndex,
  4434. gpuDescriptorHandle);
  4435. for (Uint32 i = 0; i < graphicsPipeline->header.num_fragment_samplers; i += 1) {
  4436. cpuHandles[i] = commandBuffer->fragmentSamplerTextureDescriptorHandles[i];
  4437. }
  4438. D3D12_INTERNAL_WriteGPUDescriptors(
  4439. commandBuffer,
  4440. D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV,
  4441. cpuHandles,
  4442. graphicsPipeline->header.num_fragment_samplers,
  4443. &gpuDescriptorHandle);
  4444. ID3D12GraphicsCommandList_SetGraphicsRootDescriptorTable(
  4445. commandBuffer->graphicsCommandList,
  4446. graphicsPipeline->rootSignature->fragmentSamplerTextureRootIndex,
  4447. gpuDescriptorHandle);
  4448. }
  4449. commandBuffer->needFragmentSamplerBind = false;
  4450. }
  4451. if (commandBuffer->needFragmentStorageTextureBind) {
  4452. if (graphicsPipeline->header.num_fragment_storage_textures > 0) {
  4453. for (Uint32 i = 0; i < graphicsPipeline->header.num_fragment_storage_textures; i += 1) {
  4454. cpuHandles[i] = commandBuffer->fragmentStorageTextureDescriptorHandles[i];
  4455. }
  4456. D3D12_INTERNAL_WriteGPUDescriptors(
  4457. commandBuffer,
  4458. D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV,
  4459. cpuHandles,
  4460. graphicsPipeline->header.num_fragment_storage_textures,
  4461. &gpuDescriptorHandle);
  4462. ID3D12GraphicsCommandList_SetGraphicsRootDescriptorTable(
  4463. commandBuffer->graphicsCommandList,
  4464. graphicsPipeline->rootSignature->fragmentStorageTextureRootIndex,
  4465. gpuDescriptorHandle);
  4466. }
  4467. commandBuffer->needFragmentStorageTextureBind = false;
  4468. }
  4469. if (commandBuffer->needFragmentStorageBufferBind) {
  4470. if (graphicsPipeline->header.num_fragment_storage_buffers > 0) {
  4471. for (Uint32 i = 0; i < graphicsPipeline->header.num_fragment_storage_buffers; i += 1) {
  4472. cpuHandles[i] = commandBuffer->fragmentStorageBufferDescriptorHandles[i];
  4473. }
  4474. D3D12_INTERNAL_WriteGPUDescriptors(
  4475. commandBuffer,
  4476. D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV,
  4477. cpuHandles,
  4478. graphicsPipeline->header.num_fragment_storage_buffers,
  4479. &gpuDescriptorHandle);
  4480. ID3D12GraphicsCommandList_SetGraphicsRootDescriptorTable(
  4481. commandBuffer->graphicsCommandList,
  4482. graphicsPipeline->rootSignature->fragmentStorageBufferRootIndex,
  4483. gpuDescriptorHandle);
  4484. }
  4485. commandBuffer->needFragmentStorageBufferBind = false;
  4486. }
  4487. for (Uint32 i = 0; i < MAX_UNIFORM_BUFFERS_PER_STAGE; i += 1) {
  4488. if (commandBuffer->needFragmentUniformBufferBind[i]) {
  4489. if (graphicsPipeline->header.num_fragment_uniform_buffers > i) {
  4490. ID3D12GraphicsCommandList_SetGraphicsRootConstantBufferView(
  4491. commandBuffer->graphicsCommandList,
  4492. graphicsPipeline->rootSignature->fragmentUniformBufferRootIndex[i],
  4493. commandBuffer->fragmentUniformBuffers[i]->buffer->virtualAddress + commandBuffer->fragmentUniformBuffers[i]->drawOffset);
  4494. }
  4495. commandBuffer->needFragmentUniformBufferBind[i] = false;
  4496. }
  4497. }
  4498. }
  4499. static void D3D12_DrawIndexedPrimitives(
  4500. SDL_GPUCommandBuffer *commandBuffer,
  4501. Uint32 numIndices,
  4502. Uint32 numInstances,
  4503. Uint32 firstIndex,
  4504. Sint32 vertexOffset,
  4505. Uint32 firstInstance)
  4506. {
  4507. D3D12CommandBuffer *d3d12CommandBuffer = (D3D12CommandBuffer *)commandBuffer;
  4508. D3D12_INTERNAL_BindGraphicsResources(d3d12CommandBuffer);
  4509. ID3D12GraphicsCommandList_DrawIndexedInstanced(
  4510. d3d12CommandBuffer->graphicsCommandList,
  4511. numIndices,
  4512. numInstances,
  4513. firstIndex,
  4514. vertexOffset,
  4515. firstInstance);
  4516. }
  4517. static void D3D12_DrawPrimitives(
  4518. SDL_GPUCommandBuffer *commandBuffer,
  4519. Uint32 numVertices,
  4520. Uint32 numInstances,
  4521. Uint32 firstVertex,
  4522. Uint32 firstInstance)
  4523. {
  4524. D3D12CommandBuffer *d3d12CommandBuffer = (D3D12CommandBuffer *)commandBuffer;
  4525. D3D12_INTERNAL_BindGraphicsResources(d3d12CommandBuffer);
  4526. ID3D12GraphicsCommandList_DrawInstanced(
  4527. d3d12CommandBuffer->graphicsCommandList,
  4528. numVertices,
  4529. numInstances,
  4530. firstVertex,
  4531. firstInstance);
  4532. }
  4533. static void D3D12_DrawPrimitivesIndirect(
  4534. SDL_GPUCommandBuffer *commandBuffer,
  4535. SDL_GPUBuffer *buffer,
  4536. Uint32 offset,
  4537. Uint32 drawCount)
  4538. {
  4539. D3D12CommandBuffer *d3d12CommandBuffer = (D3D12CommandBuffer *)commandBuffer;
  4540. D3D12Buffer *d3d12Buffer = ((D3D12BufferContainer *)buffer)->activeBuffer;
  4541. D3D12_INTERNAL_BindGraphicsResources(d3d12CommandBuffer);
  4542. ID3D12GraphicsCommandList_ExecuteIndirect(
  4543. d3d12CommandBuffer->graphicsCommandList,
  4544. d3d12CommandBuffer->renderer->indirectDrawCommandSignature,
  4545. drawCount,
  4546. d3d12Buffer->handle,
  4547. offset,
  4548. NULL,
  4549. 0);
  4550. D3D12_INTERNAL_TrackBuffer(d3d12CommandBuffer, d3d12Buffer);
  4551. }
  4552. static void D3D12_DrawIndexedPrimitivesIndirect(
  4553. SDL_GPUCommandBuffer *commandBuffer,
  4554. SDL_GPUBuffer *buffer,
  4555. Uint32 offset,
  4556. Uint32 drawCount)
  4557. {
  4558. D3D12CommandBuffer *d3d12CommandBuffer = (D3D12CommandBuffer *)commandBuffer;
  4559. D3D12Buffer *d3d12Buffer = ((D3D12BufferContainer *)buffer)->activeBuffer;
  4560. D3D12_INTERNAL_BindGraphicsResources(d3d12CommandBuffer);
  4561. ID3D12GraphicsCommandList_ExecuteIndirect(
  4562. d3d12CommandBuffer->graphicsCommandList,
  4563. d3d12CommandBuffer->renderer->indirectIndexedDrawCommandSignature,
  4564. drawCount,
  4565. d3d12Buffer->handle,
  4566. offset,
  4567. NULL,
  4568. 0);
  4569. D3D12_INTERNAL_TrackBuffer(d3d12CommandBuffer, d3d12Buffer);
  4570. }
  4571. static void D3D12_EndRenderPass(
  4572. SDL_GPUCommandBuffer *commandBuffer)
  4573. {
  4574. D3D12CommandBuffer *d3d12CommandBuffer = (D3D12CommandBuffer *)commandBuffer;
  4575. Uint32 i;
  4576. for (i = 0; i < MAX_COLOR_TARGET_BINDINGS; i += 1) {
  4577. if (d3d12CommandBuffer->colorTargetSubresources[i] != NULL) {
  4578. if (d3d12CommandBuffer->colorResolveSubresources[i] != NULL) {
  4579. // Resolving requires some extra barriers
  4580. D3D12_INTERNAL_TextureSubresourceBarrier(
  4581. d3d12CommandBuffer,
  4582. D3D12_RESOURCE_STATE_RENDER_TARGET,
  4583. D3D12_RESOURCE_STATE_RESOLVE_SOURCE,
  4584. d3d12CommandBuffer->colorTargetSubresources[i]
  4585. );
  4586. ID3D12GraphicsCommandList_ResolveSubresource(
  4587. d3d12CommandBuffer->graphicsCommandList,
  4588. d3d12CommandBuffer->colorResolveSubresources[i]->parent->resource,
  4589. d3d12CommandBuffer->colorResolveSubresources[i]->index,
  4590. d3d12CommandBuffer->colorTargetSubresources[i]->parent->resource,
  4591. d3d12CommandBuffer->colorTargetSubresources[i]->index,
  4592. SDLToD3D12_TextureFormat[d3d12CommandBuffer->colorTargetSubresources[i]->parent->container->header.info.format]);
  4593. D3D12_INTERNAL_TextureSubresourceTransitionToDefaultUsage(
  4594. d3d12CommandBuffer,
  4595. D3D12_RESOURCE_STATE_RESOLVE_SOURCE,
  4596. d3d12CommandBuffer->colorTargetSubresources[i]);
  4597. D3D12_INTERNAL_TextureSubresourceTransitionToDefaultUsage(
  4598. d3d12CommandBuffer,
  4599. D3D12_RESOURCE_STATE_RESOLVE_DEST,
  4600. d3d12CommandBuffer->colorResolveSubresources[i]);
  4601. } else {
  4602. D3D12_INTERNAL_TextureSubresourceTransitionToDefaultUsage(
  4603. d3d12CommandBuffer,
  4604. D3D12_RESOURCE_STATE_RENDER_TARGET,
  4605. d3d12CommandBuffer->colorTargetSubresources[i]);
  4606. }
  4607. }
  4608. }
  4609. if (d3d12CommandBuffer->depthStencilTextureSubresource != NULL) {
  4610. D3D12_INTERNAL_TextureSubresourceTransitionToDefaultUsage(
  4611. d3d12CommandBuffer,
  4612. D3D12_RESOURCE_STATE_DEPTH_WRITE,
  4613. d3d12CommandBuffer->depthStencilTextureSubresource);
  4614. d3d12CommandBuffer->depthStencilTextureSubresource = NULL;
  4615. }
  4616. d3d12CommandBuffer->currentGraphicsPipeline = NULL;
  4617. ID3D12GraphicsCommandList_OMSetRenderTargets(
  4618. d3d12CommandBuffer->graphicsCommandList,
  4619. 0,
  4620. NULL,
  4621. false,
  4622. NULL);
  4623. // Reset bind state
  4624. SDL_zeroa(d3d12CommandBuffer->colorTargetSubresources);
  4625. SDL_zeroa(d3d12CommandBuffer->colorResolveSubresources);
  4626. d3d12CommandBuffer->depthStencilTextureSubresource = NULL;
  4627. SDL_zeroa(d3d12CommandBuffer->vertexBuffers);
  4628. SDL_zeroa(d3d12CommandBuffer->vertexBufferOffsets);
  4629. d3d12CommandBuffer->vertexBufferCount = 0;
  4630. SDL_zeroa(d3d12CommandBuffer->vertexSamplerTextureDescriptorHandles);
  4631. SDL_zeroa(d3d12CommandBuffer->vertexSamplerDescriptorHandles);
  4632. SDL_zeroa(d3d12CommandBuffer->vertexStorageTextureDescriptorHandles);
  4633. SDL_zeroa(d3d12CommandBuffer->vertexStorageBufferDescriptorHandles);
  4634. SDL_zeroa(d3d12CommandBuffer->fragmentSamplerTextureDescriptorHandles);
  4635. SDL_zeroa(d3d12CommandBuffer->fragmentSamplerDescriptorHandles);
  4636. SDL_zeroa(d3d12CommandBuffer->fragmentStorageTextureDescriptorHandles);
  4637. SDL_zeroa(d3d12CommandBuffer->fragmentStorageBufferDescriptorHandles);
  4638. }
  4639. // Compute Pass
  4640. static void D3D12_BeginComputePass(
  4641. SDL_GPUCommandBuffer *commandBuffer,
  4642. const SDL_GPUStorageTextureReadWriteBinding *storageTextureBindings,
  4643. Uint32 numStorageTextureBindings,
  4644. const SDL_GPUStorageBufferReadWriteBinding *storageBufferBindings,
  4645. Uint32 numStorageBufferBindings)
  4646. {
  4647. D3D12CommandBuffer *d3d12CommandBuffer = (D3D12CommandBuffer *)commandBuffer;
  4648. d3d12CommandBuffer->computeReadWriteStorageTextureSubresourceCount = numStorageTextureBindings;
  4649. d3d12CommandBuffer->computeReadWriteStorageBufferCount = numStorageBufferBindings;
  4650. /* Read-write resources will be actually bound in BindComputePipeline
  4651. * after the root signature is set.
  4652. * We also have to scan to see which barriers we actually need because depth slices aren't separate subresources
  4653. */
  4654. if (numStorageTextureBindings > 0) {
  4655. for (Uint32 i = 0; i < numStorageTextureBindings; i += 1) {
  4656. D3D12TextureContainer *container = (D3D12TextureContainer *)storageTextureBindings[i].texture;
  4657. D3D12TextureSubresource *subresource = D3D12_INTERNAL_PrepareTextureSubresourceForWrite(
  4658. d3d12CommandBuffer,
  4659. container,
  4660. storageTextureBindings[i].layer,
  4661. storageTextureBindings[i].mip_level,
  4662. storageTextureBindings[i].cycle,
  4663. D3D12_RESOURCE_STATE_UNORDERED_ACCESS);
  4664. d3d12CommandBuffer->computeReadWriteStorageTextureSubresources[i] = subresource;
  4665. d3d12CommandBuffer->computeReadWriteStorageTextureDescriptorHandles[i] = subresource->uavHandle.cpuHandle;
  4666. D3D12_INTERNAL_TrackTexture(
  4667. d3d12CommandBuffer,
  4668. subresource->parent);
  4669. }
  4670. }
  4671. if (numStorageBufferBindings > 0) {
  4672. for (Uint32 i = 0; i < numStorageBufferBindings; i += 1) {
  4673. D3D12BufferContainer *container = (D3D12BufferContainer *)storageBufferBindings[i].buffer;
  4674. D3D12Buffer *buffer = D3D12_INTERNAL_PrepareBufferForWrite(
  4675. d3d12CommandBuffer,
  4676. container,
  4677. storageBufferBindings[i].cycle,
  4678. D3D12_RESOURCE_STATE_UNORDERED_ACCESS);
  4679. d3d12CommandBuffer->computeReadWriteStorageBuffers[i] = buffer;
  4680. d3d12CommandBuffer->computeReadWriteStorageBufferDescriptorHandles[i] = buffer->uavDescriptor.cpuHandle;
  4681. D3D12_INTERNAL_TrackBuffer(
  4682. d3d12CommandBuffer,
  4683. buffer);
  4684. }
  4685. }
  4686. }
  4687. static void D3D12_BindComputePipeline(
  4688. SDL_GPUCommandBuffer *commandBuffer,
  4689. SDL_GPUComputePipeline *computePipeline)
  4690. {
  4691. D3D12CommandBuffer *d3d12CommandBuffer = (D3D12CommandBuffer *)commandBuffer;
  4692. /* Acquire GPU descriptor heaps if we haven't yet */
  4693. if (d3d12CommandBuffer->gpuDescriptorHeaps[0] == NULL) {
  4694. D3D12_INTERNAL_SetGPUDescriptorHeaps(d3d12CommandBuffer);
  4695. }
  4696. D3D12ComputePipeline *pipeline = (D3D12ComputePipeline *)computePipeline;
  4697. D3D12_CPU_DESCRIPTOR_HANDLE cpuHandles[MAX_TEXTURE_SAMPLERS_PER_STAGE];
  4698. D3D12_GPU_DESCRIPTOR_HANDLE gpuDescriptorHandle;
  4699. ID3D12GraphicsCommandList_SetPipelineState(
  4700. d3d12CommandBuffer->graphicsCommandList,
  4701. pipeline->pipelineState);
  4702. ID3D12GraphicsCommandList_SetComputeRootSignature(
  4703. d3d12CommandBuffer->graphicsCommandList,
  4704. pipeline->rootSignature->handle);
  4705. d3d12CommandBuffer->currentComputePipeline = pipeline;
  4706. d3d12CommandBuffer->needComputeSamplerBind = true;
  4707. d3d12CommandBuffer->needComputeReadOnlyStorageTextureBind = true;
  4708. d3d12CommandBuffer->needComputeReadOnlyStorageBufferBind = true;
  4709. for (Uint32 i = 0; i < MAX_UNIFORM_BUFFERS_PER_STAGE; i += 1) {
  4710. d3d12CommandBuffer->needComputeUniformBufferBind[i] = true;
  4711. }
  4712. for (Uint32 i = 0; i < pipeline->header.numUniformBuffers; i += 1) {
  4713. if (d3d12CommandBuffer->computeUniformBuffers[i] == NULL) {
  4714. d3d12CommandBuffer->computeUniformBuffers[i] = D3D12_INTERNAL_AcquireUniformBufferFromPool(
  4715. d3d12CommandBuffer);
  4716. }
  4717. }
  4718. D3D12_INTERNAL_TrackComputePipeline(d3d12CommandBuffer, pipeline);
  4719. // Bind write-only resources after setting root signature
  4720. if (pipeline->header.numReadWriteStorageTextures > 0) {
  4721. for (Uint32 i = 0; i < pipeline->header.numReadWriteStorageTextures; i += 1) {
  4722. cpuHandles[i] = d3d12CommandBuffer->computeReadWriteStorageTextureDescriptorHandles[i];
  4723. }
  4724. D3D12_INTERNAL_WriteGPUDescriptors(
  4725. d3d12CommandBuffer,
  4726. D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV,
  4727. cpuHandles,
  4728. d3d12CommandBuffer->computeReadWriteStorageTextureSubresourceCount,
  4729. &gpuDescriptorHandle);
  4730. ID3D12GraphicsCommandList_SetComputeRootDescriptorTable(
  4731. d3d12CommandBuffer->graphicsCommandList,
  4732. d3d12CommandBuffer->currentComputePipeline->rootSignature->readWriteStorageTextureRootIndex,
  4733. gpuDescriptorHandle);
  4734. }
  4735. if (pipeline->header.numReadWriteStorageBuffers > 0) {
  4736. for (Uint32 i = 0; i < pipeline->header.numReadWriteStorageBuffers; i += 1) {
  4737. cpuHandles[i] = d3d12CommandBuffer->computeReadWriteStorageBufferDescriptorHandles[i];
  4738. }
  4739. D3D12_INTERNAL_WriteGPUDescriptors(
  4740. d3d12CommandBuffer,
  4741. D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV,
  4742. cpuHandles,
  4743. d3d12CommandBuffer->computeReadWriteStorageBufferCount,
  4744. &gpuDescriptorHandle);
  4745. ID3D12GraphicsCommandList_SetComputeRootDescriptorTable(
  4746. d3d12CommandBuffer->graphicsCommandList,
  4747. d3d12CommandBuffer->currentComputePipeline->rootSignature->readWriteStorageBufferRootIndex,
  4748. gpuDescriptorHandle);
  4749. }
  4750. }
  4751. static void D3D12_BindComputeSamplers(
  4752. SDL_GPUCommandBuffer *commandBuffer,
  4753. Uint32 firstSlot,
  4754. const SDL_GPUTextureSamplerBinding *textureSamplerBindings,
  4755. Uint32 numBindings)
  4756. {
  4757. D3D12CommandBuffer *d3d12CommandBuffer = (D3D12CommandBuffer *)commandBuffer;
  4758. for (Uint32 i = 0; i < numBindings; i += 1) {
  4759. D3D12TextureContainer *container = (D3D12TextureContainer *)textureSamplerBindings[i].texture;
  4760. D3D12Sampler *sampler = (D3D12Sampler *)textureSamplerBindings[i].sampler;
  4761. if (d3d12CommandBuffer->computeSamplerDescriptorHandles[firstSlot + i].ptr != sampler->handle.cpuHandle.ptr) {
  4762. D3D12_INTERNAL_TrackSampler(
  4763. d3d12CommandBuffer,
  4764. (D3D12Sampler *)textureSamplerBindings[i].sampler);
  4765. d3d12CommandBuffer->computeSamplerDescriptorHandles[firstSlot + i] = sampler->handle.cpuHandle;
  4766. d3d12CommandBuffer->needComputeSamplerBind = true;
  4767. }
  4768. if (d3d12CommandBuffer->computeSamplerTextureDescriptorHandles[firstSlot + i].ptr != container->activeTexture->srvHandle.cpuHandle.ptr) {
  4769. D3D12_INTERNAL_TrackTexture(
  4770. d3d12CommandBuffer,
  4771. container->activeTexture);
  4772. d3d12CommandBuffer->computeSamplerTextureDescriptorHandles[firstSlot + i] = container->activeTexture->srvHandle.cpuHandle;
  4773. d3d12CommandBuffer->needComputeSamplerBind = true;
  4774. }
  4775. }
  4776. }
  4777. static void D3D12_BindComputeStorageTextures(
  4778. SDL_GPUCommandBuffer *commandBuffer,
  4779. Uint32 firstSlot,
  4780. SDL_GPUTexture *const *storageTextures,
  4781. Uint32 numBindings)
  4782. {
  4783. D3D12CommandBuffer *d3d12CommandBuffer = (D3D12CommandBuffer *)commandBuffer;
  4784. for (Uint32 i = 0; i < numBindings; i += 1) {
  4785. D3D12TextureContainer *container = (D3D12TextureContainer *)storageTextures[i];
  4786. if (d3d12CommandBuffer->computeReadOnlyStorageTextures[firstSlot + i] != container->activeTexture) {
  4787. /* If a different texture was in this slot, transition it back to its default usage */
  4788. if (d3d12CommandBuffer->computeReadOnlyStorageTextures[firstSlot + i] != NULL) {
  4789. D3D12_INTERNAL_TextureTransitionToDefaultUsage(
  4790. d3d12CommandBuffer,
  4791. D3D12_RESOURCE_STATE_NON_PIXEL_SHADER_RESOURCE,
  4792. d3d12CommandBuffer->computeReadOnlyStorageTextures[firstSlot + i]);
  4793. }
  4794. /* Then transition the new texture and prepare it for binding */
  4795. D3D12_INTERNAL_TextureTransitionFromDefaultUsage(
  4796. d3d12CommandBuffer,
  4797. D3D12_RESOURCE_STATE_NON_PIXEL_SHADER_RESOURCE,
  4798. container->activeTexture);
  4799. D3D12_INTERNAL_TrackTexture(
  4800. d3d12CommandBuffer,
  4801. container->activeTexture);
  4802. d3d12CommandBuffer->computeReadOnlyStorageTextures[firstSlot + i] = container->activeTexture;
  4803. d3d12CommandBuffer->computeReadOnlyStorageTextureDescriptorHandles[firstSlot + i] = container->activeTexture->srvHandle.cpuHandle;
  4804. d3d12CommandBuffer->needComputeReadOnlyStorageTextureBind = true;
  4805. }
  4806. }
  4807. }
  4808. static void D3D12_BindComputeStorageBuffers(
  4809. SDL_GPUCommandBuffer *commandBuffer,
  4810. Uint32 firstSlot,
  4811. SDL_GPUBuffer *const *storageBuffers,
  4812. Uint32 numBindings)
  4813. {
  4814. D3D12CommandBuffer *d3d12CommandBuffer = (D3D12CommandBuffer *)commandBuffer;
  4815. for (Uint32 i = 0; i < numBindings; i += 1) {
  4816. D3D12BufferContainer *container = (D3D12BufferContainer *)storageBuffers[i];
  4817. D3D12Buffer *buffer = container->activeBuffer;
  4818. if (d3d12CommandBuffer->computeReadOnlyStorageBuffers[firstSlot + i] != buffer) {
  4819. /* If a different buffer was in this slot, transition it back to its default usage */
  4820. if (d3d12CommandBuffer->computeReadOnlyStorageBuffers[firstSlot + i] != NULL) {
  4821. D3D12_INTERNAL_BufferTransitionToDefaultUsage(
  4822. d3d12CommandBuffer,
  4823. D3D12_RESOURCE_STATE_NON_PIXEL_SHADER_RESOURCE,
  4824. d3d12CommandBuffer->computeReadOnlyStorageBuffers[firstSlot + i]);
  4825. }
  4826. /* Then transition the new buffer and prepare it for binding */
  4827. D3D12_INTERNAL_BufferTransitionFromDefaultUsage(
  4828. d3d12CommandBuffer,
  4829. D3D12_RESOURCE_STATE_NON_PIXEL_SHADER_RESOURCE,
  4830. buffer);
  4831. D3D12_INTERNAL_TrackBuffer(
  4832. d3d12CommandBuffer,
  4833. buffer);
  4834. d3d12CommandBuffer->computeReadOnlyStorageBuffers[firstSlot + i] = buffer;
  4835. d3d12CommandBuffer->computeReadOnlyStorageBufferDescriptorHandles[firstSlot + i] = buffer->srvDescriptor.cpuHandle;
  4836. d3d12CommandBuffer->needComputeReadOnlyStorageBufferBind = true;
  4837. }
  4838. }
  4839. }
  4840. static void D3D12_PushComputeUniformData(
  4841. SDL_GPUCommandBuffer *commandBuffer,
  4842. Uint32 slotIndex,
  4843. const void *data,
  4844. Uint32 length)
  4845. {
  4846. D3D12CommandBuffer *d3d12CommandBuffer = (D3D12CommandBuffer *)commandBuffer;
  4847. D3D12_INTERNAL_PushUniformData(
  4848. d3d12CommandBuffer,
  4849. SDL_GPU_SHADERSTAGE_COMPUTE,
  4850. slotIndex,
  4851. data,
  4852. length);
  4853. }
  4854. static void D3D12_INTERNAL_BindComputeResources(
  4855. D3D12CommandBuffer *commandBuffer)
  4856. {
  4857. D3D12ComputePipeline *computePipeline = commandBuffer->currentComputePipeline;
  4858. /* Acquire GPU descriptor heaps if we haven't yet */
  4859. if (commandBuffer->gpuDescriptorHeaps[0] == NULL) {
  4860. D3D12_INTERNAL_SetGPUDescriptorHeaps(commandBuffer);
  4861. }
  4862. D3D12_CPU_DESCRIPTOR_HANDLE cpuHandles[MAX_TEXTURE_SAMPLERS_PER_STAGE];
  4863. D3D12_GPU_DESCRIPTOR_HANDLE gpuDescriptorHandle;
  4864. if (commandBuffer->needComputeSamplerBind) {
  4865. if (computePipeline->header.numSamplers > 0) {
  4866. for (Uint32 i = 0; i < computePipeline->header.numSamplers; i += 1) {
  4867. cpuHandles[i] = commandBuffer->computeSamplerDescriptorHandles[i];
  4868. }
  4869. D3D12_INTERNAL_WriteGPUDescriptors(
  4870. commandBuffer,
  4871. D3D12_DESCRIPTOR_HEAP_TYPE_SAMPLER,
  4872. cpuHandles,
  4873. computePipeline->header.numSamplers,
  4874. &gpuDescriptorHandle);
  4875. ID3D12GraphicsCommandList_SetComputeRootDescriptorTable(
  4876. commandBuffer->graphicsCommandList,
  4877. computePipeline->rootSignature->samplerRootIndex,
  4878. gpuDescriptorHandle);
  4879. for (Uint32 i = 0; i < computePipeline->header.numSamplers; i += 1) {
  4880. cpuHandles[i] = commandBuffer->computeSamplerTextureDescriptorHandles[i];
  4881. }
  4882. D3D12_INTERNAL_WriteGPUDescriptors(
  4883. commandBuffer,
  4884. D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV,
  4885. cpuHandles,
  4886. computePipeline->header.numSamplers,
  4887. &gpuDescriptorHandle);
  4888. ID3D12GraphicsCommandList_SetComputeRootDescriptorTable(
  4889. commandBuffer->graphicsCommandList,
  4890. computePipeline->rootSignature->samplerTextureRootIndex,
  4891. gpuDescriptorHandle);
  4892. }
  4893. commandBuffer->needComputeSamplerBind = false;
  4894. }
  4895. if (commandBuffer->needComputeReadOnlyStorageTextureBind) {
  4896. if (computePipeline->header.numReadonlyStorageTextures > 0) {
  4897. for (Uint32 i = 0; i < computePipeline->header.numReadonlyStorageTextures; i += 1) {
  4898. cpuHandles[i] = commandBuffer->computeReadOnlyStorageTextureDescriptorHandles[i];
  4899. }
  4900. D3D12_INTERNAL_WriteGPUDescriptors(
  4901. commandBuffer,
  4902. D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV,
  4903. cpuHandles,
  4904. computePipeline->header.numReadonlyStorageTextures,
  4905. &gpuDescriptorHandle);
  4906. ID3D12GraphicsCommandList_SetComputeRootDescriptorTable(
  4907. commandBuffer->graphicsCommandList,
  4908. computePipeline->rootSignature->readOnlyStorageTextureRootIndex,
  4909. gpuDescriptorHandle);
  4910. }
  4911. commandBuffer->needComputeReadOnlyStorageTextureBind = false;
  4912. }
  4913. if (commandBuffer->needComputeReadOnlyStorageBufferBind) {
  4914. if (computePipeline->header.numReadonlyStorageBuffers > 0) {
  4915. for (Uint32 i = 0; i < computePipeline->header.numReadonlyStorageBuffers; i += 1) {
  4916. cpuHandles[i] = commandBuffer->computeReadOnlyStorageBufferDescriptorHandles[i];
  4917. }
  4918. D3D12_INTERNAL_WriteGPUDescriptors(
  4919. commandBuffer,
  4920. D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV,
  4921. cpuHandles,
  4922. computePipeline->header.numReadonlyStorageBuffers,
  4923. &gpuDescriptorHandle);
  4924. ID3D12GraphicsCommandList_SetComputeRootDescriptorTable(
  4925. commandBuffer->graphicsCommandList,
  4926. computePipeline->rootSignature->readOnlyStorageBufferRootIndex,
  4927. gpuDescriptorHandle);
  4928. }
  4929. commandBuffer->needComputeReadOnlyStorageBufferBind = false;
  4930. }
  4931. for (Uint32 i = 0; i < MAX_UNIFORM_BUFFERS_PER_STAGE; i += 1) {
  4932. if (commandBuffer->needComputeUniformBufferBind[i]) {
  4933. if (computePipeline->header.numUniformBuffers > i) {
  4934. ID3D12GraphicsCommandList_SetComputeRootConstantBufferView(
  4935. commandBuffer->graphicsCommandList,
  4936. computePipeline->rootSignature->uniformBufferRootIndex[i],
  4937. commandBuffer->computeUniformBuffers[i]->buffer->virtualAddress + commandBuffer->computeUniformBuffers[i]->drawOffset);
  4938. }
  4939. }
  4940. commandBuffer->needComputeUniformBufferBind[i] = false;
  4941. }
  4942. }
  4943. static void D3D12_DispatchCompute(
  4944. SDL_GPUCommandBuffer *commandBuffer,
  4945. Uint32 groupcountX,
  4946. Uint32 groupcountY,
  4947. Uint32 groupcountZ)
  4948. {
  4949. D3D12CommandBuffer *d3d12CommandBuffer = (D3D12CommandBuffer *)commandBuffer;
  4950. D3D12_INTERNAL_BindComputeResources(d3d12CommandBuffer);
  4951. ID3D12GraphicsCommandList_Dispatch(
  4952. d3d12CommandBuffer->graphicsCommandList,
  4953. groupcountX,
  4954. groupcountY,
  4955. groupcountZ);
  4956. }
  4957. static void D3D12_DispatchComputeIndirect(
  4958. SDL_GPUCommandBuffer *commandBuffer,
  4959. SDL_GPUBuffer *buffer,
  4960. Uint32 offset)
  4961. {
  4962. D3D12CommandBuffer *d3d12CommandBuffer = (D3D12CommandBuffer *)commandBuffer;
  4963. D3D12Buffer *d3d12Buffer = ((D3D12BufferContainer *)buffer)->activeBuffer;
  4964. D3D12_INTERNAL_BindComputeResources(d3d12CommandBuffer);
  4965. ID3D12GraphicsCommandList_ExecuteIndirect(
  4966. d3d12CommandBuffer->graphicsCommandList,
  4967. d3d12CommandBuffer->renderer->indirectDispatchCommandSignature,
  4968. 1,
  4969. d3d12Buffer->handle,
  4970. offset,
  4971. NULL,
  4972. 0);
  4973. D3D12_INTERNAL_TrackBuffer(d3d12CommandBuffer, d3d12Buffer);
  4974. }
  4975. static void D3D12_EndComputePass(
  4976. SDL_GPUCommandBuffer *commandBuffer)
  4977. {
  4978. D3D12CommandBuffer *d3d12CommandBuffer = (D3D12CommandBuffer *)commandBuffer;
  4979. for (Uint32 i = 0; i < d3d12CommandBuffer->computeReadWriteStorageTextureSubresourceCount; i += 1) {
  4980. if (d3d12CommandBuffer->computeReadWriteStorageTextureSubresources[i]) {
  4981. D3D12_INTERNAL_TextureSubresourceTransitionToDefaultUsage(
  4982. d3d12CommandBuffer,
  4983. D3D12_RESOURCE_STATE_UNORDERED_ACCESS,
  4984. d3d12CommandBuffer->computeReadWriteStorageTextureSubresources[i]);
  4985. d3d12CommandBuffer->computeReadWriteStorageTextureSubresources[i] = NULL;
  4986. }
  4987. }
  4988. d3d12CommandBuffer->computeReadWriteStorageTextureSubresourceCount = 0;
  4989. for (Uint32 i = 0; i < d3d12CommandBuffer->computeReadWriteStorageBufferCount; i += 1) {
  4990. if (d3d12CommandBuffer->computeReadWriteStorageBuffers[i]) {
  4991. D3D12_INTERNAL_BufferTransitionToDefaultUsage(
  4992. d3d12CommandBuffer,
  4993. D3D12_RESOURCE_STATE_UNORDERED_ACCESS,
  4994. d3d12CommandBuffer->computeReadWriteStorageBuffers[i]);
  4995. d3d12CommandBuffer->computeReadWriteStorageBuffers[i] = NULL;
  4996. }
  4997. }
  4998. d3d12CommandBuffer->computeReadWriteStorageBufferCount = 0;
  4999. for (Uint32 i = 0; i < MAX_STORAGE_TEXTURES_PER_STAGE; i += 1) {
  5000. if (d3d12CommandBuffer->computeReadOnlyStorageTextures[i]) {
  5001. D3D12_INTERNAL_TextureTransitionToDefaultUsage(
  5002. d3d12CommandBuffer,
  5003. D3D12_RESOURCE_STATE_NON_PIXEL_SHADER_RESOURCE,
  5004. d3d12CommandBuffer->computeReadOnlyStorageTextures[i]);
  5005. d3d12CommandBuffer->computeReadOnlyStorageTextures[i] = NULL;
  5006. }
  5007. }
  5008. for (Uint32 i = 0; i < MAX_STORAGE_BUFFERS_PER_STAGE; i += 1) {
  5009. if (d3d12CommandBuffer->computeReadOnlyStorageBuffers[i]) {
  5010. D3D12_INTERNAL_BufferTransitionToDefaultUsage(
  5011. d3d12CommandBuffer,
  5012. D3D12_RESOURCE_STATE_NON_PIXEL_SHADER_RESOURCE,
  5013. d3d12CommandBuffer->computeReadOnlyStorageBuffers[i]);
  5014. d3d12CommandBuffer->computeReadOnlyStorageBuffers[i] = NULL;
  5015. }
  5016. }
  5017. SDL_zeroa(d3d12CommandBuffer->computeSamplerTextureDescriptorHandles);
  5018. SDL_zeroa(d3d12CommandBuffer->computeSamplerDescriptorHandles);
  5019. SDL_zeroa(d3d12CommandBuffer->computeReadWriteStorageTextureDescriptorHandles);
  5020. SDL_zeroa(d3d12CommandBuffer->computeReadWriteStorageBufferDescriptorHandles);
  5021. d3d12CommandBuffer->currentComputePipeline = NULL;
  5022. }
  5023. // TransferBuffer Data
  5024. static void *D3D12_MapTransferBuffer(
  5025. SDL_GPURenderer *driverData,
  5026. SDL_GPUTransferBuffer *transferBuffer,
  5027. bool cycle)
  5028. {
  5029. D3D12Renderer *renderer = (D3D12Renderer *)driverData;
  5030. D3D12BufferContainer *container = (D3D12BufferContainer *)transferBuffer;
  5031. void *dataPointer;
  5032. if (
  5033. cycle &&
  5034. SDL_GetAtomicInt(&container->activeBuffer->referenceCount) > 0) {
  5035. D3D12_INTERNAL_CycleActiveBuffer(
  5036. renderer,
  5037. container);
  5038. }
  5039. // Upload buffers are persistently mapped, download buffers are not
  5040. if (container->type == D3D12_BUFFER_TYPE_UPLOAD) {
  5041. dataPointer = container->activeBuffer->mapPointer;
  5042. } else {
  5043. ID3D12Resource_Map(
  5044. container->activeBuffer->handle,
  5045. 0,
  5046. NULL,
  5047. (void **)&dataPointer);
  5048. }
  5049. return dataPointer;
  5050. }
  5051. static void D3D12_UnmapTransferBuffer(
  5052. SDL_GPURenderer *driverData,
  5053. SDL_GPUTransferBuffer *transferBuffer)
  5054. {
  5055. (void)driverData;
  5056. D3D12BufferContainer *container = (D3D12BufferContainer *)transferBuffer;
  5057. // Upload buffers are persistently mapped, download buffers are not
  5058. if (container->type == D3D12_BUFFER_TYPE_DOWNLOAD) {
  5059. ID3D12Resource_Unmap(
  5060. container->activeBuffer->handle,
  5061. 0,
  5062. NULL);
  5063. }
  5064. }
  5065. // Copy Pass
  5066. static void D3D12_BeginCopyPass(
  5067. SDL_GPUCommandBuffer *commandBuffer)
  5068. {
  5069. // no-op
  5070. (void)commandBuffer;
  5071. }
  5072. static void D3D12_UploadToTexture(
  5073. SDL_GPUCommandBuffer *commandBuffer,
  5074. const SDL_GPUTextureTransferInfo *source,
  5075. const SDL_GPUTextureRegion *destination,
  5076. bool cycle)
  5077. {
  5078. D3D12CommandBuffer *d3d12CommandBuffer = (D3D12CommandBuffer *)commandBuffer;
  5079. D3D12Renderer *renderer = (D3D12Renderer *)d3d12CommandBuffer->renderer;
  5080. D3D12BufferContainer *transferBufferContainer = (D3D12BufferContainer *)source->transfer_buffer;
  5081. D3D12Buffer *temporaryBuffer = NULL;
  5082. D3D12_TEXTURE_COPY_LOCATION sourceLocation;
  5083. D3D12_TEXTURE_COPY_LOCATION destinationLocation;
  5084. Uint32 pixelsPerRow = source->pixels_per_row;
  5085. Uint32 blockWidth;
  5086. Uint32 blockSize;
  5087. Uint32 rowPitch;
  5088. Uint32 blockHeight;
  5089. Uint32 alignedRowPitch;
  5090. Uint32 rowsPerSlice = source->rows_per_layer;
  5091. Uint32 bytesPerSlice;
  5092. Uint32 alignedBytesPerSlice;
  5093. bool needsRealignment;
  5094. bool needsPlacementCopy;
  5095. // Note that the transfer buffer does not need a barrier, as it is synced by the client.
  5096. D3D12TextureContainer *textureContainer = (D3D12TextureContainer *)destination->texture;
  5097. D3D12TextureSubresource *textureSubresource = D3D12_INTERNAL_PrepareTextureSubresourceForWrite(
  5098. d3d12CommandBuffer,
  5099. textureContainer,
  5100. destination->layer,
  5101. destination->mip_level,
  5102. cycle,
  5103. D3D12_RESOURCE_STATE_COPY_DEST);
  5104. /* Unless the UnrestrictedBufferTextureCopyPitchSupported feature is supported, D3D12 requires
  5105. * texture data row pitch to be 256 byte aligned, which is obviously insane. Instead of exposing
  5106. * that restriction to the client, which is a huge rake to step on, and a restriction that no
  5107. * other backend requires, we're going to copy data to a temporary buffer, copy THAT data to the
  5108. * texture, and then get rid of the temporary buffer ASAP. If we're lucky and the row pitch and
  5109. * depth pitch are already aligned, we can skip all of that.
  5110. *
  5111. * D3D12 also requires offsets to be 512 byte aligned. We'll fix that for the client and warn them as well.
  5112. *
  5113. * And just for some extra fun, D3D12 doesn't actually support depth pitch, so we have to realign that too!
  5114. */
  5115. if (pixelsPerRow == 0) {
  5116. pixelsPerRow = destination->w;
  5117. }
  5118. if (rowsPerSlice == 0) {
  5119. rowsPerSlice = destination->h;
  5120. }
  5121. blockWidth = Texture_GetBlockWidth(textureContainer->header.info.format);
  5122. blockSize = SDL_GPUTextureFormatTexelBlockSize(textureContainer->header.info.format);
  5123. rowPitch = (pixelsPerRow + (blockWidth - 1)) / blockWidth * blockSize;
  5124. blockHeight = (rowsPerSlice + (blockWidth - 1)) / blockWidth;
  5125. bytesPerSlice = rowsPerSlice * rowPitch;
  5126. if (renderer->UnrestrictedBufferTextureCopyPitchSupported) {
  5127. alignedRowPitch = rowPitch;
  5128. needsRealignment = false;
  5129. needsPlacementCopy = false;
  5130. } else {
  5131. alignedRowPitch = (destination->w + (blockWidth - 1)) / blockWidth * blockSize;
  5132. alignedRowPitch = D3D12_INTERNAL_Align(alignedRowPitch, D3D12_TEXTURE_DATA_PITCH_ALIGNMENT);
  5133. needsRealignment = rowsPerSlice != destination->h || rowPitch != alignedRowPitch;
  5134. needsPlacementCopy = source->offset % D3D12_TEXTURE_DATA_PLACEMENT_ALIGNMENT != 0;
  5135. }
  5136. alignedBytesPerSlice = alignedRowPitch * destination->h;
  5137. sourceLocation.Type = D3D12_TEXTURE_COPY_TYPE_PLACED_FOOTPRINT;
  5138. sourceLocation.PlacedFootprint.Footprint.Format = SDLToD3D12_TextureFormat[textureContainer->header.info.format];
  5139. sourceLocation.PlacedFootprint.Footprint.RowPitch = alignedRowPitch;
  5140. destinationLocation.pResource = textureContainer->activeTexture->resource;
  5141. destinationLocation.Type = D3D12_TEXTURE_COPY_TYPE_SUBRESOURCE_INDEX;
  5142. destinationLocation.SubresourceIndex = textureSubresource->index;
  5143. if (needsRealignment) {
  5144. temporaryBuffer = D3D12_INTERNAL_CreateBuffer(
  5145. d3d12CommandBuffer->renderer,
  5146. 0,
  5147. alignedRowPitch * blockHeight * destination->d,
  5148. D3D12_BUFFER_TYPE_UPLOAD,
  5149. NULL);
  5150. if (!temporaryBuffer) {
  5151. return;
  5152. }
  5153. sourceLocation.pResource = temporaryBuffer->handle;
  5154. for (Uint32 sliceIndex = 0; sliceIndex < destination->d; sliceIndex += 1) {
  5155. for (Uint32 rowIndex = 0; rowIndex < blockHeight; rowIndex += 1) {
  5156. SDL_memcpy(
  5157. temporaryBuffer->mapPointer + (sliceIndex * alignedBytesPerSlice) + (rowIndex * alignedRowPitch),
  5158. transferBufferContainer->activeBuffer->mapPointer + source->offset + (sliceIndex * bytesPerSlice) + (rowIndex * rowPitch),
  5159. rowPitch);
  5160. }
  5161. sourceLocation.PlacedFootprint.Footprint.Width = destination->w;
  5162. sourceLocation.PlacedFootprint.Footprint.Height = destination->h;
  5163. sourceLocation.PlacedFootprint.Footprint.Depth = 1;
  5164. sourceLocation.PlacedFootprint.Offset = (sliceIndex * alignedBytesPerSlice);
  5165. ID3D12GraphicsCommandList_CopyTextureRegion(
  5166. d3d12CommandBuffer->graphicsCommandList,
  5167. &destinationLocation,
  5168. destination->x,
  5169. destination->y,
  5170. destination->z + sliceIndex,
  5171. &sourceLocation,
  5172. NULL);
  5173. }
  5174. D3D12_INTERNAL_TrackBuffer(d3d12CommandBuffer, temporaryBuffer);
  5175. D3D12_INTERNAL_ReleaseBuffer(
  5176. d3d12CommandBuffer->renderer,
  5177. temporaryBuffer);
  5178. if (d3d12CommandBuffer->renderer->debug_mode) {
  5179. SDL_LogWarn(SDL_LOG_CATEGORY_GPU, "Texture upload row pitch not aligned to 256 bytes! This is suboptimal on D3D12!");
  5180. }
  5181. } else if (needsPlacementCopy) {
  5182. temporaryBuffer = D3D12_INTERNAL_CreateBuffer(
  5183. d3d12CommandBuffer->renderer,
  5184. 0,
  5185. alignedRowPitch * blockHeight * destination->d,
  5186. D3D12_BUFFER_TYPE_UPLOAD,
  5187. NULL);
  5188. if (!temporaryBuffer) {
  5189. return;
  5190. }
  5191. SDL_memcpy(
  5192. temporaryBuffer->mapPointer,
  5193. transferBufferContainer->activeBuffer->mapPointer + source->offset,
  5194. alignedRowPitch * blockHeight * destination->d);
  5195. sourceLocation.pResource = temporaryBuffer->handle;
  5196. sourceLocation.PlacedFootprint.Offset = 0;
  5197. sourceLocation.PlacedFootprint.Footprint.Width = destination->w;
  5198. sourceLocation.PlacedFootprint.Footprint.Height = destination->h;
  5199. sourceLocation.PlacedFootprint.Footprint.Depth = destination->d;
  5200. ID3D12GraphicsCommandList_CopyTextureRegion(
  5201. d3d12CommandBuffer->graphicsCommandList,
  5202. &destinationLocation,
  5203. destination->x,
  5204. destination->y,
  5205. destination->z,
  5206. &sourceLocation,
  5207. NULL);
  5208. D3D12_INTERNAL_TrackBuffer(d3d12CommandBuffer, temporaryBuffer);
  5209. D3D12_INTERNAL_ReleaseBuffer(
  5210. d3d12CommandBuffer->renderer,
  5211. temporaryBuffer);
  5212. if (d3d12CommandBuffer->renderer->debug_mode) {
  5213. SDL_LogWarn(SDL_LOG_CATEGORY_GPU, "Texture upload offset not aligned to 512 bytes! This is suboptimal on D3D12!");
  5214. }
  5215. } else {
  5216. sourceLocation.pResource = transferBufferContainer->activeBuffer->handle;
  5217. sourceLocation.PlacedFootprint.Offset = source->offset;
  5218. sourceLocation.PlacedFootprint.Footprint.Width = destination->w;
  5219. sourceLocation.PlacedFootprint.Footprint.Height = destination->h;
  5220. sourceLocation.PlacedFootprint.Footprint.Depth = destination->d;
  5221. ID3D12GraphicsCommandList_CopyTextureRegion(
  5222. d3d12CommandBuffer->graphicsCommandList,
  5223. &destinationLocation,
  5224. destination->x,
  5225. destination->y,
  5226. destination->z,
  5227. &sourceLocation,
  5228. NULL);
  5229. }
  5230. D3D12_INTERNAL_TextureSubresourceTransitionToDefaultUsage(
  5231. d3d12CommandBuffer,
  5232. D3D12_RESOURCE_STATE_COPY_DEST,
  5233. textureSubresource);
  5234. D3D12_INTERNAL_TrackBuffer(d3d12CommandBuffer, transferBufferContainer->activeBuffer);
  5235. D3D12_INTERNAL_TrackTexture(d3d12CommandBuffer, textureSubresource->parent);
  5236. }
  5237. static void D3D12_UploadToBuffer(
  5238. SDL_GPUCommandBuffer *commandBuffer,
  5239. const SDL_GPUTransferBufferLocation *source,
  5240. const SDL_GPUBufferRegion *destination,
  5241. bool cycle)
  5242. {
  5243. D3D12CommandBuffer *d3d12CommandBuffer = (D3D12CommandBuffer *)commandBuffer;
  5244. D3D12BufferContainer *transferBufferContainer = (D3D12BufferContainer *)source->transfer_buffer;
  5245. D3D12BufferContainer *bufferContainer = (D3D12BufferContainer *)destination->buffer;
  5246. // The transfer buffer does not need a barrier, it is synced by the client.
  5247. D3D12Buffer *buffer = D3D12_INTERNAL_PrepareBufferForWrite(
  5248. d3d12CommandBuffer,
  5249. bufferContainer,
  5250. cycle,
  5251. D3D12_RESOURCE_STATE_COPY_DEST);
  5252. ID3D12GraphicsCommandList_CopyBufferRegion(
  5253. d3d12CommandBuffer->graphicsCommandList,
  5254. buffer->handle,
  5255. destination->offset,
  5256. transferBufferContainer->activeBuffer->handle,
  5257. source->offset,
  5258. destination->size);
  5259. D3D12_INTERNAL_BufferTransitionToDefaultUsage(
  5260. d3d12CommandBuffer,
  5261. D3D12_RESOURCE_STATE_COPY_DEST,
  5262. buffer);
  5263. D3D12_INTERNAL_TrackBuffer(d3d12CommandBuffer, transferBufferContainer->activeBuffer);
  5264. D3D12_INTERNAL_TrackBuffer(d3d12CommandBuffer, buffer);
  5265. }
  5266. static void D3D12_CopyTextureToTexture(
  5267. SDL_GPUCommandBuffer *commandBuffer,
  5268. const SDL_GPUTextureLocation *source,
  5269. const SDL_GPUTextureLocation *destination,
  5270. Uint32 w,
  5271. Uint32 h,
  5272. Uint32 d,
  5273. bool cycle)
  5274. {
  5275. D3D12CommandBuffer *d3d12CommandBuffer = (D3D12CommandBuffer *)commandBuffer;
  5276. D3D12_TEXTURE_COPY_LOCATION sourceLocation;
  5277. D3D12_TEXTURE_COPY_LOCATION destinationLocation;
  5278. D3D12TextureSubresource *sourceSubresource = D3D12_INTERNAL_FetchTextureSubresource(
  5279. (D3D12TextureContainer *)source->texture,
  5280. source->layer,
  5281. source->mip_level);
  5282. D3D12TextureSubresource *destinationSubresource = D3D12_INTERNAL_PrepareTextureSubresourceForWrite(
  5283. d3d12CommandBuffer,
  5284. (D3D12TextureContainer *)destination->texture,
  5285. destination->layer,
  5286. destination->mip_level,
  5287. cycle,
  5288. D3D12_RESOURCE_STATE_COPY_DEST);
  5289. D3D12_INTERNAL_TextureSubresourceTransitionFromDefaultUsage(
  5290. d3d12CommandBuffer,
  5291. D3D12_RESOURCE_STATE_COPY_SOURCE,
  5292. sourceSubresource);
  5293. sourceLocation.Type = D3D12_TEXTURE_COPY_TYPE_SUBRESOURCE_INDEX;
  5294. sourceLocation.SubresourceIndex = sourceSubresource->index;
  5295. sourceLocation.pResource = sourceSubresource->parent->resource;
  5296. destinationLocation.Type = D3D12_TEXTURE_COPY_TYPE_SUBRESOURCE_INDEX;
  5297. destinationLocation.SubresourceIndex = destinationSubresource->index;
  5298. destinationLocation.pResource = destinationSubresource->parent->resource;
  5299. D3D12_BOX sourceBox = { source->x, source->y, source->z, source->x + w, source->y + h, source->z + d };
  5300. ID3D12GraphicsCommandList_CopyTextureRegion(
  5301. d3d12CommandBuffer->graphicsCommandList,
  5302. &destinationLocation,
  5303. destination->x,
  5304. destination->y,
  5305. destination->z,
  5306. &sourceLocation,
  5307. &sourceBox);
  5308. D3D12_INTERNAL_TextureSubresourceTransitionToDefaultUsage(
  5309. d3d12CommandBuffer,
  5310. D3D12_RESOURCE_STATE_COPY_SOURCE,
  5311. sourceSubresource);
  5312. D3D12_INTERNAL_TextureSubresourceTransitionToDefaultUsage(
  5313. d3d12CommandBuffer,
  5314. D3D12_RESOURCE_STATE_COPY_DEST,
  5315. destinationSubresource);
  5316. D3D12_INTERNAL_TrackTexture(
  5317. d3d12CommandBuffer,
  5318. sourceSubresource->parent);
  5319. D3D12_INTERNAL_TrackTexture(
  5320. d3d12CommandBuffer,
  5321. destinationSubresource->parent);
  5322. }
  5323. static void D3D12_CopyBufferToBuffer(
  5324. SDL_GPUCommandBuffer *commandBuffer,
  5325. const SDL_GPUBufferLocation *source,
  5326. const SDL_GPUBufferLocation *destination,
  5327. Uint32 size,
  5328. bool cycle)
  5329. {
  5330. D3D12CommandBuffer *d3d12CommandBuffer = (D3D12CommandBuffer *)commandBuffer;
  5331. D3D12BufferContainer *sourceContainer = (D3D12BufferContainer *)source->buffer;
  5332. D3D12BufferContainer *destinationContainer = (D3D12BufferContainer *)destination->buffer;
  5333. D3D12Buffer *sourceBuffer = sourceContainer->activeBuffer;
  5334. D3D12Buffer *destinationBuffer = D3D12_INTERNAL_PrepareBufferForWrite(
  5335. d3d12CommandBuffer,
  5336. destinationContainer,
  5337. cycle,
  5338. D3D12_RESOURCE_STATE_COPY_DEST);
  5339. D3D12_INTERNAL_BufferTransitionFromDefaultUsage(
  5340. d3d12CommandBuffer,
  5341. D3D12_RESOURCE_STATE_COPY_SOURCE,
  5342. sourceBuffer);
  5343. ID3D12GraphicsCommandList_CopyBufferRegion(
  5344. d3d12CommandBuffer->graphicsCommandList,
  5345. destinationBuffer->handle,
  5346. destination->offset,
  5347. sourceBuffer->handle,
  5348. source->offset,
  5349. size);
  5350. D3D12_INTERNAL_BufferTransitionToDefaultUsage(
  5351. d3d12CommandBuffer,
  5352. D3D12_RESOURCE_STATE_COPY_SOURCE,
  5353. sourceBuffer);
  5354. D3D12_INTERNAL_BufferTransitionToDefaultUsage(
  5355. d3d12CommandBuffer,
  5356. D3D12_RESOURCE_STATE_COPY_DEST,
  5357. destinationBuffer);
  5358. D3D12_INTERNAL_TrackBuffer(d3d12CommandBuffer, sourceBuffer);
  5359. D3D12_INTERNAL_TrackBuffer(d3d12CommandBuffer, destinationBuffer);
  5360. }
  5361. static void D3D12_DownloadFromTexture(
  5362. SDL_GPUCommandBuffer *commandBuffer,
  5363. const SDL_GPUTextureRegion *source,
  5364. const SDL_GPUTextureTransferInfo *destination)
  5365. {
  5366. D3D12CommandBuffer *d3d12CommandBuffer = (D3D12CommandBuffer *)commandBuffer;
  5367. D3D12Renderer *renderer = d3d12CommandBuffer->renderer;
  5368. D3D12_TEXTURE_COPY_LOCATION sourceLocation;
  5369. D3D12_TEXTURE_COPY_LOCATION destinationLocation;
  5370. Uint32 pixelsPerRow = destination->pixels_per_row;
  5371. Uint32 rowPitch;
  5372. Uint32 alignedRowPitch;
  5373. Uint32 rowsPerSlice = destination->rows_per_layer;
  5374. bool needsRealignment;
  5375. bool needsPlacementCopy;
  5376. D3D12TextureDownload *textureDownload = NULL;
  5377. D3D12TextureContainer *sourceContainer = (D3D12TextureContainer *)source->texture;
  5378. D3D12TextureSubresource *sourceSubresource = D3D12_INTERNAL_FetchTextureSubresource(
  5379. sourceContainer,
  5380. source->layer,
  5381. source->mip_level);
  5382. D3D12BufferContainer *destinationContainer = (D3D12BufferContainer *)destination->transfer_buffer;
  5383. D3D12Buffer *destinationBuffer = destinationContainer->activeBuffer;
  5384. /* Unless the UnrestrictedBufferTextureCopyPitchSupported feature is supported, D3D12 requires
  5385. * texture data row pitch to be 256 byte aligned, which is obviously insane. Instead of exposing
  5386. * that restriction to the client, which is a huge rake to step on, and a restriction that no
  5387. * other backend requires, we're going to copy data to a temporary buffer, copy THAT data to the
  5388. * texture, and then get rid of the temporary buffer ASAP. If we're lucky and the row pitch and
  5389. * depth pitch are already aligned, we can skip all of that.
  5390. *
  5391. * D3D12 also requires offsets to be 512 byte aligned. We'll fix that for the client and warn them as well.
  5392. *
  5393. * And just for some extra fun, D3D12 doesn't actually support depth pitch, so we have to realign that too!
  5394. *
  5395. * Since this is an async download we have to do all these fixups after the command is finished,
  5396. * so we'll cache the metadata and map and copy it when the command buffer is cleaned.
  5397. */
  5398. if (pixelsPerRow == 0) {
  5399. pixelsPerRow = source->w;
  5400. }
  5401. rowPitch = BytesPerRow(pixelsPerRow, sourceContainer->header.info.format);
  5402. if (rowsPerSlice == 0) {
  5403. rowsPerSlice = source->h;
  5404. }
  5405. if (renderer->UnrestrictedBufferTextureCopyPitchSupported) {
  5406. alignedRowPitch = rowPitch;
  5407. needsRealignment = false;
  5408. needsPlacementCopy = false;
  5409. } else {
  5410. alignedRowPitch = D3D12_INTERNAL_Align(rowPitch, D3D12_TEXTURE_DATA_PITCH_ALIGNMENT);
  5411. needsRealignment = rowsPerSlice != source->h || rowPitch != alignedRowPitch;
  5412. needsPlacementCopy = destination->offset % D3D12_TEXTURE_DATA_PLACEMENT_ALIGNMENT != 0;
  5413. }
  5414. sourceLocation.Type = D3D12_TEXTURE_COPY_TYPE_SUBRESOURCE_INDEX;
  5415. sourceLocation.SubresourceIndex = sourceSubresource->index;
  5416. sourceLocation.pResource = sourceSubresource->parent->resource;
  5417. D3D12_BOX sourceBox = { source->x, source->y, source->z, source->x + source->w, source->y + rowsPerSlice, source->z + source->d };
  5418. destinationLocation.Type = D3D12_TEXTURE_COPY_TYPE_PLACED_FOOTPRINT;
  5419. destinationLocation.PlacedFootprint.Footprint.Format = SDLToD3D12_TextureFormat[sourceContainer->header.info.format];
  5420. destinationLocation.PlacedFootprint.Footprint.Width = source->w;
  5421. destinationLocation.PlacedFootprint.Footprint.Height = rowsPerSlice;
  5422. destinationLocation.PlacedFootprint.Footprint.Depth = source->d;
  5423. destinationLocation.PlacedFootprint.Footprint.RowPitch = alignedRowPitch;
  5424. if (needsRealignment || needsPlacementCopy) {
  5425. textureDownload = (D3D12TextureDownload *)SDL_malloc(sizeof(D3D12TextureDownload));
  5426. if (!textureDownload) {
  5427. SDL_LogError(SDL_LOG_CATEGORY_GPU, "Failed to create texture download structure!");
  5428. return;
  5429. }
  5430. textureDownload->temporaryBuffer = D3D12_INTERNAL_CreateBuffer(
  5431. d3d12CommandBuffer->renderer,
  5432. 0,
  5433. alignedRowPitch * rowsPerSlice * source->d,
  5434. D3D12_BUFFER_TYPE_DOWNLOAD,
  5435. NULL);
  5436. if (!textureDownload->temporaryBuffer) {
  5437. SDL_free(textureDownload);
  5438. return;
  5439. }
  5440. textureDownload->destinationBuffer = destinationBuffer;
  5441. textureDownload->bufferOffset = destination->offset;
  5442. textureDownload->width = source->w;
  5443. textureDownload->height = rowsPerSlice;
  5444. textureDownload->depth = source->d;
  5445. textureDownload->bytesPerRow = rowPitch;
  5446. textureDownload->bytesPerDepthSlice = rowPitch * rowsPerSlice;
  5447. textureDownload->alignedBytesPerRow = alignedRowPitch;
  5448. destinationLocation.pResource = textureDownload->temporaryBuffer->handle;
  5449. destinationLocation.PlacedFootprint.Offset = 0;
  5450. if (d3d12CommandBuffer->renderer->debug_mode) {
  5451. SDL_LogWarn(SDL_LOG_CATEGORY_GPU, "Texture pitch or offset not aligned properly! This is suboptimal on D3D12!");
  5452. }
  5453. } else {
  5454. destinationLocation.pResource = destinationBuffer->handle;
  5455. destinationLocation.PlacedFootprint.Offset = destination->offset;
  5456. }
  5457. D3D12_INTERNAL_TextureSubresourceTransitionFromDefaultUsage(
  5458. d3d12CommandBuffer,
  5459. D3D12_RESOURCE_STATE_COPY_SOURCE,
  5460. sourceSubresource);
  5461. ID3D12GraphicsCommandList_CopyTextureRegion(
  5462. d3d12CommandBuffer->graphicsCommandList,
  5463. &destinationLocation,
  5464. 0,
  5465. 0,
  5466. 0,
  5467. &sourceLocation,
  5468. &sourceBox);
  5469. D3D12_INTERNAL_TextureSubresourceTransitionToDefaultUsage(
  5470. d3d12CommandBuffer,
  5471. D3D12_RESOURCE_STATE_COPY_SOURCE,
  5472. sourceSubresource);
  5473. D3D12_INTERNAL_TrackBuffer(d3d12CommandBuffer, destinationBuffer);
  5474. D3D12_INTERNAL_TrackTexture(d3d12CommandBuffer, sourceSubresource->parent);
  5475. if (textureDownload != NULL) {
  5476. D3D12_INTERNAL_TrackBuffer(d3d12CommandBuffer, textureDownload->temporaryBuffer);
  5477. if (d3d12CommandBuffer->textureDownloadCount >= d3d12CommandBuffer->textureDownloadCapacity) {
  5478. d3d12CommandBuffer->textureDownloadCapacity *= 2;
  5479. d3d12CommandBuffer->textureDownloads = (D3D12TextureDownload **)SDL_realloc(
  5480. d3d12CommandBuffer->textureDownloads,
  5481. d3d12CommandBuffer->textureDownloadCapacity * sizeof(D3D12TextureDownload *));
  5482. }
  5483. d3d12CommandBuffer->textureDownloads[d3d12CommandBuffer->textureDownloadCount] = textureDownload;
  5484. d3d12CommandBuffer->textureDownloadCount += 1;
  5485. D3D12_INTERNAL_ReleaseBuffer(d3d12CommandBuffer->renderer, textureDownload->temporaryBuffer);
  5486. }
  5487. }
  5488. static void D3D12_DownloadFromBuffer(
  5489. SDL_GPUCommandBuffer *commandBuffer,
  5490. const SDL_GPUBufferRegion *source,
  5491. const SDL_GPUTransferBufferLocation *destination)
  5492. {
  5493. D3D12CommandBuffer *d3d12CommandBuffer = (D3D12CommandBuffer *)commandBuffer;
  5494. D3D12BufferContainer *sourceContainer = (D3D12BufferContainer *)source->buffer;
  5495. D3D12BufferContainer *destinationContainer = (D3D12BufferContainer *)destination->transfer_buffer;
  5496. D3D12Buffer *sourceBuffer = sourceContainer->activeBuffer;
  5497. D3D12_INTERNAL_BufferTransitionFromDefaultUsage(
  5498. d3d12CommandBuffer,
  5499. D3D12_RESOURCE_STATE_COPY_SOURCE,
  5500. sourceBuffer);
  5501. D3D12Buffer *destinationBuffer = destinationContainer->activeBuffer;
  5502. ID3D12GraphicsCommandList_CopyBufferRegion(
  5503. d3d12CommandBuffer->graphicsCommandList,
  5504. destinationBuffer->handle,
  5505. destination->offset,
  5506. sourceBuffer->handle,
  5507. source->offset,
  5508. source->size);
  5509. D3D12_INTERNAL_BufferTransitionToDefaultUsage(
  5510. d3d12CommandBuffer,
  5511. D3D12_RESOURCE_STATE_COPY_SOURCE,
  5512. sourceBuffer);
  5513. D3D12_INTERNAL_TrackBuffer(d3d12CommandBuffer, sourceBuffer);
  5514. D3D12_INTERNAL_TrackBuffer(d3d12CommandBuffer, destinationBuffer);
  5515. }
  5516. static void D3D12_EndCopyPass(
  5517. SDL_GPUCommandBuffer *commandBuffer)
  5518. {
  5519. // no-op
  5520. (void)commandBuffer;
  5521. }
  5522. static void D3D12_GenerateMipmaps(
  5523. SDL_GPUCommandBuffer *commandBuffer,
  5524. SDL_GPUTexture *texture)
  5525. {
  5526. D3D12CommandBuffer *d3d12CommandBuffer = (D3D12CommandBuffer *)commandBuffer;
  5527. D3D12Renderer *renderer = d3d12CommandBuffer->renderer;
  5528. D3D12TextureContainer *container = (D3D12TextureContainer *)texture;
  5529. SDL_GPUGraphicsPipeline *blitPipeline;
  5530. blitPipeline = SDL_GPU_FetchBlitPipeline(
  5531. renderer->sdlGPUDevice,
  5532. container->header.info.type,
  5533. container->header.info.format,
  5534. renderer->blitVertexShader,
  5535. renderer->blitFrom2DShader,
  5536. renderer->blitFrom2DArrayShader,
  5537. renderer->blitFrom3DShader,
  5538. renderer->blitFromCubeShader,
  5539. renderer->blitFromCubeArrayShader,
  5540. &renderer->blitPipelines,
  5541. &renderer->blitPipelineCount,
  5542. &renderer->blitPipelineCapacity);
  5543. if (blitPipeline == NULL) {
  5544. SDL_LogWarn(SDL_LOG_CATEGORY_GPU, "Could not fetch blit pipeline");
  5545. return;
  5546. }
  5547. // We have to do this one subresource at a time
  5548. for (Uint32 layerOrDepthIndex = 0; layerOrDepthIndex < container->header.info.layer_count_or_depth; layerOrDepthIndex += 1) {
  5549. for (Uint32 levelIndex = 1; levelIndex < container->header.info.num_levels; levelIndex += 1) {
  5550. SDL_GPUBlitInfo blitInfo;
  5551. SDL_zero(blitInfo);
  5552. blitInfo.source.texture = texture;
  5553. blitInfo.source.mip_level = levelIndex - 1;
  5554. blitInfo.source.layer_or_depth_plane = layerOrDepthIndex;
  5555. blitInfo.source.x = 0;
  5556. blitInfo.source.y = 0;
  5557. blitInfo.source.w = SDL_max(container->header.info.width >> (levelIndex - 1), 1);
  5558. blitInfo.source.h = SDL_max(container->header.info.height >> (levelIndex - 1), 1);
  5559. blitInfo.destination.texture = texture;
  5560. blitInfo.destination.mip_level = levelIndex;
  5561. blitInfo.destination.layer_or_depth_plane = layerOrDepthIndex;
  5562. blitInfo.destination.x = 0;
  5563. blitInfo.destination.y = 0;
  5564. blitInfo.destination.w = SDL_max(container->header.info.width >> levelIndex, 1);
  5565. blitInfo.destination.h = SDL_max(container->header.info.height >> levelIndex, 1);
  5566. blitInfo.load_op = SDL_GPU_LOADOP_DONT_CARE;
  5567. blitInfo.filter = SDL_GPU_FILTER_LINEAR;
  5568. SDL_BlitGPUTexture(
  5569. commandBuffer,
  5570. &blitInfo);
  5571. }
  5572. }
  5573. D3D12_INTERNAL_TrackTexture(d3d12CommandBuffer, container->activeTexture);
  5574. }
  5575. static void D3D12_Blit(
  5576. SDL_GPUCommandBuffer *commandBuffer,
  5577. const SDL_GPUBlitInfo *info)
  5578. {
  5579. D3D12CommandBuffer *d3d12CommandBuffer = (D3D12CommandBuffer *)commandBuffer;
  5580. D3D12Renderer *renderer = (D3D12Renderer *)d3d12CommandBuffer->renderer;
  5581. SDL_GPU_BlitCommon(
  5582. commandBuffer,
  5583. info,
  5584. renderer->blitLinearSampler,
  5585. renderer->blitNearestSampler,
  5586. renderer->blitVertexShader,
  5587. renderer->blitFrom2DShader,
  5588. renderer->blitFrom2DArrayShader,
  5589. renderer->blitFrom3DShader,
  5590. renderer->blitFromCubeShader,
  5591. renderer->blitFromCubeArrayShader,
  5592. &renderer->blitPipelines,
  5593. &renderer->blitPipelineCount,
  5594. &renderer->blitPipelineCapacity);
  5595. }
  5596. // Submission/Presentation
  5597. static D3D12WindowData *D3D12_INTERNAL_FetchWindowData(
  5598. SDL_Window *window)
  5599. {
  5600. SDL_PropertiesID properties = SDL_GetWindowProperties(window);
  5601. return (D3D12WindowData *)SDL_GetPointerProperty(properties, WINDOW_PROPERTY_DATA, NULL);
  5602. }
  5603. static bool D3D12_INTERNAL_OnWindowResize(void *userdata, SDL_Event *e)
  5604. {
  5605. SDL_Window *w = (SDL_Window *)userdata;
  5606. D3D12WindowData *data;
  5607. if (e->type == SDL_EVENT_WINDOW_PIXEL_SIZE_CHANGED && e->window.windowID == SDL_GetWindowID(w)) {
  5608. data = D3D12_INTERNAL_FetchWindowData(w);
  5609. data->needsSwapchainRecreate = true;
  5610. }
  5611. return true;
  5612. }
  5613. static bool D3D12_SupportsSwapchainComposition(
  5614. SDL_GPURenderer *driverData,
  5615. SDL_Window *window,
  5616. SDL_GPUSwapchainComposition swapchainComposition)
  5617. {
  5618. #if defined(SDL_PLATFORM_XBOXONE) || defined(SDL_PLATFORM_XBOXSERIES)
  5619. // FIXME: HDR support would be nice to add, but it seems complicated...
  5620. return swapchainComposition == SDL_GPU_SWAPCHAINCOMPOSITION_SDR ||
  5621. swapchainComposition == SDL_GPU_SWAPCHAINCOMPOSITION_SDR_LINEAR;
  5622. #else
  5623. D3D12Renderer *renderer = (D3D12Renderer *)driverData;
  5624. DXGI_FORMAT format;
  5625. D3D12_FEATURE_DATA_FORMAT_SUPPORT formatSupport;
  5626. Uint32 colorSpaceSupport;
  5627. HRESULT res;
  5628. format = SwapchainCompositionToTextureFormat[swapchainComposition];
  5629. formatSupport.Format = format;
  5630. res = ID3D12Device_CheckFeatureSupport(
  5631. renderer->device,
  5632. D3D12_FEATURE_FORMAT_SUPPORT,
  5633. &formatSupport,
  5634. sizeof(formatSupport));
  5635. if (FAILED(res)) {
  5636. // Format is apparently unknown
  5637. return false;
  5638. }
  5639. if (!(formatSupport.Support1 & D3D12_FORMAT_SUPPORT1_DISPLAY)) {
  5640. return false;
  5641. }
  5642. D3D12WindowData *windowData = D3D12_INTERNAL_FetchWindowData(window);
  5643. if (windowData == NULL) {
  5644. SET_STRING_ERROR_AND_RETURN("Must claim window before querying swapchain composition support!", false);
  5645. }
  5646. // Check the color space support if necessary
  5647. if (swapchainComposition != SDL_GPU_SWAPCHAINCOMPOSITION_SDR) {
  5648. IDXGISwapChain3_CheckColorSpaceSupport(
  5649. windowData->swapchain,
  5650. SwapchainCompositionToColorSpace[swapchainComposition],
  5651. &colorSpaceSupport);
  5652. if (!(colorSpaceSupport & DXGI_SWAP_CHAIN_COLOR_SPACE_SUPPORT_FLAG_PRESENT)) {
  5653. return false;
  5654. }
  5655. }
  5656. #endif
  5657. return true;
  5658. }
  5659. static bool D3D12_SupportsPresentMode(
  5660. SDL_GPURenderer *driverData,
  5661. SDL_Window *window,
  5662. SDL_GPUPresentMode presentMode)
  5663. {
  5664. (void)driverData;
  5665. (void)window;
  5666. switch (presentMode) {
  5667. case SDL_GPU_PRESENTMODE_IMMEDIATE:
  5668. case SDL_GPU_PRESENTMODE_VSYNC:
  5669. return true;
  5670. case SDL_GPU_PRESENTMODE_MAILBOX:
  5671. #if (defined(SDL_PLATFORM_XBOXONE) || defined(SDL_PLATFORM_XBOXSERIES))
  5672. return false;
  5673. #else
  5674. return true;
  5675. #endif
  5676. default:
  5677. SDL_assert(!"Unrecognized present mode");
  5678. return false;
  5679. }
  5680. }
  5681. #if defined(SDL_PLATFORM_XBOXONE) || defined(SDL_PLATFORM_XBOXSERIES)
  5682. static bool D3D12_INTERNAL_CreateSwapchain(
  5683. D3D12Renderer *renderer,
  5684. D3D12WindowData *windowData,
  5685. SDL_GPUSwapchainComposition swapchain_composition,
  5686. SDL_GPUPresentMode present_mode)
  5687. {
  5688. int width, height;
  5689. SDL_GPUTextureCreateInfo createInfo;
  5690. D3D12Texture *texture;
  5691. // Get the swapchain size
  5692. SDL_SyncWindow(windowData->window);
  5693. SDL_GetWindowSizeInPixels(windowData->window, &width, &height);
  5694. // Min swapchain image count is 2
  5695. windowData->swapchainTextureCount = SDL_clamp(renderer->allowedFramesInFlight, 2, 3);
  5696. // Create the swapchain textures
  5697. SDL_zero(createInfo);
  5698. createInfo.type = SDL_GPU_TEXTURETYPE_2D;
  5699. createInfo.width = width;
  5700. createInfo.height = height;
  5701. createInfo.format = SwapchainCompositionToSDLTextureFormat[swapchain_composition];
  5702. createInfo.usage = SDL_GPU_TEXTUREUSAGE_COLOR_TARGET;
  5703. createInfo.layer_count_or_depth = 1;
  5704. createInfo.num_levels = 1;
  5705. for (Uint32 i = 0; i < windowData->swapchainTextureCount; i += 1) {
  5706. texture = D3D12_INTERNAL_CreateTexture(renderer, &createInfo, true, "Swapchain");
  5707. texture->container = &windowData->textureContainers[i];
  5708. windowData->textureContainers[i].activeTexture = texture;
  5709. windowData->textureContainers[i].canBeCycled = false;
  5710. windowData->textureContainers[i].header.info = createInfo;
  5711. windowData->textureContainers[i].textureCapacity = 1;
  5712. windowData->textureContainers[i].textureCount = 1;
  5713. windowData->textureContainers[i].textures = &windowData->textureContainers[i].activeTexture;
  5714. }
  5715. // Initialize the swapchain data
  5716. windowData->present_mode = present_mode;
  5717. windowData->swapchainComposition = swapchain_composition;
  5718. windowData->swapchainColorSpace = DXGI_COLOR_SPACE_RGB_FULL_G22_NONE_P709;
  5719. windowData->frameCounter = 0;
  5720. windowData->width = width;
  5721. windowData->height = height;
  5722. // Precache blit pipelines for the swapchain format
  5723. for (Uint32 i = 0; i < 5; i += 1) {
  5724. SDL_GPU_FetchBlitPipeline(
  5725. renderer->sdlGPUDevice,
  5726. (SDL_GPUTextureType)i,
  5727. createInfo.format,
  5728. renderer->blitVertexShader,
  5729. renderer->blitFrom2DShader,
  5730. renderer->blitFrom2DArrayShader,
  5731. renderer->blitFrom3DShader,
  5732. renderer->blitFromCubeShader,
  5733. renderer->blitFromCubeArrayShader,
  5734. &renderer->blitPipelines,
  5735. &renderer->blitPipelineCount,
  5736. &renderer->blitPipelineCapacity);
  5737. }
  5738. return true;
  5739. }
  5740. static void D3D12_INTERNAL_DestroySwapchain(
  5741. D3D12Renderer *renderer,
  5742. D3D12WindowData *windowData)
  5743. {
  5744. renderer->commandQueue->PresentX(0, NULL, NULL);
  5745. for (Uint32 i = 0; i < windowData->swapchainTextureCount; i += 1) {
  5746. D3D12_INTERNAL_DestroyTexture(windowData->textureContainers[i].activeTexture);
  5747. }
  5748. }
  5749. static bool D3D12_INTERNAL_ResizeSwapchain(
  5750. D3D12Renderer *renderer,
  5751. D3D12WindowData *windowData)
  5752. {
  5753. // Wait so we don't release in-flight views
  5754. D3D12_Wait((SDL_GPURenderer *)renderer);
  5755. // Present a black screen
  5756. renderer->commandQueue->PresentX(0, NULL, NULL);
  5757. // Clean up the previous swapchain textures
  5758. for (Uint32 i = 0; i < windowData->swapchainTextureCount; i += 1) {
  5759. D3D12_INTERNAL_DestroyTexture(windowData->textureContainers[i].activeTexture);
  5760. }
  5761. // Create a new swapchain
  5762. D3D12_INTERNAL_CreateSwapchain(
  5763. renderer,
  5764. windowData,
  5765. windowData->swapchainComposition,
  5766. windowData->present_mode);
  5767. windowData->needsSwapchainRecreate = false;
  5768. return true;
  5769. }
  5770. #else
  5771. static bool D3D12_INTERNAL_InitializeSwapchainTexture(
  5772. D3D12Renderer *renderer,
  5773. IDXGISwapChain3 *swapchain,
  5774. SDL_GPUSwapchainComposition composition,
  5775. Uint32 index,
  5776. D3D12TextureContainer *pTextureContainer)
  5777. {
  5778. D3D12Texture *pTexture;
  5779. ID3D12Resource *swapchainTexture;
  5780. D3D12_RESOURCE_DESC textureDesc;
  5781. D3D12_SHADER_RESOURCE_VIEW_DESC srvDesc;
  5782. D3D12_RENDER_TARGET_VIEW_DESC rtvDesc;
  5783. DXGI_FORMAT swapchainFormat = SwapchainCompositionToTextureFormat[composition];
  5784. HRESULT res;
  5785. res = IDXGISwapChain_GetBuffer(
  5786. swapchain,
  5787. index,
  5788. D3D_GUID(D3D_IID_ID3D12Resource),
  5789. (void **)&swapchainTexture);
  5790. CHECK_D3D12_ERROR_AND_RETURN("Could not get buffer from swapchain!", false);
  5791. pTexture = (D3D12Texture *)SDL_calloc(1, sizeof(D3D12Texture));
  5792. if (!pTexture) {
  5793. ID3D12Resource_Release(swapchainTexture);
  5794. return false;
  5795. }
  5796. pTexture->resource = NULL; // This will be set in AcquireSwapchainTexture
  5797. SDL_SetAtomicInt(&pTexture->referenceCount, 0);
  5798. pTexture->subresourceCount = 1;
  5799. pTexture->subresources = (D3D12TextureSubresource *)SDL_calloc(1, sizeof(D3D12TextureSubresource));
  5800. if (!pTexture->subresources) {
  5801. SDL_free(pTexture);
  5802. ID3D12Resource_Release(swapchainTexture);
  5803. return false;
  5804. }
  5805. pTexture->subresources[0].rtvHandles = SDL_calloc(1, sizeof(D3D12StagingDescriptor));
  5806. pTexture->subresources[0].uavHandle.heap = NULL;
  5807. pTexture->subresources[0].dsvHandle.heap = NULL;
  5808. pTexture->subresources[0].parent = pTexture;
  5809. pTexture->subresources[0].index = 0;
  5810. pTexture->subresources[0].layer = 0;
  5811. pTexture->subresources[0].depth = 1;
  5812. pTexture->subresources[0].level = 0;
  5813. ID3D12Resource_GetDesc(swapchainTexture, &textureDesc);
  5814. pTextureContainer->header.info.width = (Uint32)textureDesc.Width;
  5815. pTextureContainer->header.info.height = (Uint32)textureDesc.Height;
  5816. pTextureContainer->header.info.layer_count_or_depth = 1;
  5817. pTextureContainer->header.info.num_levels = 1;
  5818. pTextureContainer->header.info.type = SDL_GPU_TEXTURETYPE_2D;
  5819. pTextureContainer->header.info.usage = SDL_GPU_TEXTUREUSAGE_COLOR_TARGET;
  5820. pTextureContainer->header.info.sample_count = SDL_GPU_SAMPLECOUNT_1;
  5821. pTextureContainer->header.info.format = SwapchainCompositionToSDLTextureFormat[composition];
  5822. pTextureContainer->debugName = NULL;
  5823. pTextureContainer->textures = (D3D12Texture **)SDL_calloc(1, sizeof(D3D12Texture *));
  5824. if (!pTextureContainer->textures) {
  5825. SDL_free(pTexture->subresources);
  5826. SDL_free(pTexture);
  5827. ID3D12Resource_Release(swapchainTexture);
  5828. return false;
  5829. }
  5830. pTextureContainer->textureCapacity = 1;
  5831. pTextureContainer->textureCount = 1;
  5832. pTextureContainer->textures[0] = pTexture;
  5833. pTextureContainer->activeTexture = pTexture;
  5834. pTextureContainer->canBeCycled = false;
  5835. pTexture->container = pTextureContainer;
  5836. pTexture->containerIndex = 0;
  5837. // Create the SRV for the swapchain
  5838. D3D12_INTERNAL_AssignStagingDescriptorHandle(
  5839. renderer,
  5840. D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV,
  5841. &pTexture->srvHandle);
  5842. srvDesc.Format = SwapchainCompositionToTextureFormat[composition];
  5843. srvDesc.Shader4ComponentMapping = D3D12_DEFAULT_SHADER_4_COMPONENT_MAPPING;
  5844. srvDesc.ViewDimension = D3D12_SRV_DIMENSION_TEXTURE2D;
  5845. srvDesc.Texture2D.MipLevels = 1;
  5846. srvDesc.Texture2D.MostDetailedMip = 0;
  5847. srvDesc.Texture2D.ResourceMinLODClamp = 0;
  5848. srvDesc.Texture2D.PlaneSlice = 0;
  5849. ID3D12Device_CreateShaderResourceView(
  5850. renderer->device,
  5851. swapchainTexture,
  5852. &srvDesc,
  5853. pTexture->srvHandle.cpuHandle);
  5854. // Create the RTV for the swapchain
  5855. D3D12_INTERNAL_AssignStagingDescriptorHandle(
  5856. renderer,
  5857. D3D12_DESCRIPTOR_HEAP_TYPE_RTV,
  5858. &pTexture->subresources[0].rtvHandles[0]);
  5859. rtvDesc.Format = (composition == SDL_GPU_SWAPCHAINCOMPOSITION_SDR_LINEAR) ? DXGI_FORMAT_B8G8R8A8_UNORM_SRGB : swapchainFormat;
  5860. rtvDesc.ViewDimension = D3D12_RTV_DIMENSION_TEXTURE2D;
  5861. rtvDesc.Texture2D.MipSlice = 0;
  5862. rtvDesc.Texture2D.PlaneSlice = 0;
  5863. ID3D12Device_CreateRenderTargetView(
  5864. renderer->device,
  5865. swapchainTexture,
  5866. &rtvDesc,
  5867. pTexture->subresources[0].rtvHandles[0].cpuHandle);
  5868. ID3D12Resource_Release(swapchainTexture);
  5869. return true;
  5870. }
  5871. static bool D3D12_INTERNAL_ResizeSwapchain(
  5872. D3D12Renderer *renderer,
  5873. D3D12WindowData *windowData)
  5874. {
  5875. // Wait so we don't release in-flight views
  5876. D3D12_Wait((SDL_GPURenderer *)renderer);
  5877. // Release views and clean up
  5878. for (Uint32 i = 0; i < windowData->swapchainTextureCount; i += 1) {
  5879. D3D12_INTERNAL_ReleaseStagingDescriptorHandle(
  5880. &windowData->textureContainers[i].activeTexture->srvHandle);
  5881. D3D12_INTERNAL_ReleaseStagingDescriptorHandle(
  5882. &windowData->textureContainers[i].activeTexture->subresources[0].rtvHandles[0]);
  5883. SDL_free(windowData->textureContainers[i].activeTexture->subresources[0].rtvHandles);
  5884. SDL_free(windowData->textureContainers[i].activeTexture->subresources);
  5885. SDL_free(windowData->textureContainers[i].activeTexture);
  5886. SDL_free(windowData->textureContainers[i].textures);
  5887. }
  5888. // Resize the swapchain
  5889. HRESULT res = IDXGISwapChain_ResizeBuffers(
  5890. windowData->swapchain,
  5891. 0, // Keep buffer count the same
  5892. 0, // use client window width
  5893. 0, // use client window height
  5894. DXGI_FORMAT_UNKNOWN, // Keep the old format
  5895. renderer->supportsTearing ? DXGI_SWAP_CHAIN_FLAG_ALLOW_TEARING : 0);
  5896. CHECK_D3D12_ERROR_AND_RETURN("Could not resize swapchain buffers", false);
  5897. // Create texture object for the swapchain
  5898. for (Uint32 i = 0; i < windowData->swapchainTextureCount; i += 1) {
  5899. if (!D3D12_INTERNAL_InitializeSwapchainTexture(
  5900. renderer,
  5901. windowData->swapchain,
  5902. windowData->swapchainComposition,
  5903. i,
  5904. &windowData->textureContainers[i])) {
  5905. return false;
  5906. }
  5907. }
  5908. DXGI_SWAP_CHAIN_DESC1 swapchainDesc;
  5909. IDXGISwapChain3_GetDesc1(windowData->swapchain, &swapchainDesc);
  5910. CHECK_D3D12_ERROR_AND_RETURN("Failed to retrieve swapchain descriptor!", false);
  5911. windowData->width = swapchainDesc.Width;
  5912. windowData->height = swapchainDesc.Height;
  5913. windowData->needsSwapchainRecreate = false;
  5914. return true;
  5915. }
  5916. static void D3D12_INTERNAL_DestroySwapchain(
  5917. D3D12Renderer *renderer,
  5918. D3D12WindowData *windowData)
  5919. {
  5920. // Release views and clean up
  5921. for (Uint32 i = 0; i < windowData->swapchainTextureCount; i += 1) {
  5922. D3D12_INTERNAL_ReleaseStagingDescriptorHandle(
  5923. &windowData->textureContainers[i].activeTexture->srvHandle);
  5924. D3D12_INTERNAL_ReleaseStagingDescriptorHandle(
  5925. &windowData->textureContainers[i].activeTexture->subresources[0].rtvHandles[0]);
  5926. SDL_free(windowData->textureContainers[i].activeTexture->subresources[0].rtvHandles);
  5927. SDL_free(windowData->textureContainers[i].activeTexture->subresources);
  5928. SDL_free(windowData->textureContainers[i].activeTexture);
  5929. SDL_free(windowData->textureContainers[i].textures);
  5930. }
  5931. IDXGISwapChain_Release(windowData->swapchain);
  5932. windowData->swapchain = NULL;
  5933. }
  5934. static bool D3D12_INTERNAL_CreateSwapchain(
  5935. D3D12Renderer *renderer,
  5936. D3D12WindowData *windowData,
  5937. SDL_GPUSwapchainComposition swapchainComposition,
  5938. SDL_GPUPresentMode presentMode)
  5939. {
  5940. HWND dxgiHandle;
  5941. DXGI_SWAP_CHAIN_DESC1 swapchainDesc;
  5942. DXGI_SWAP_CHAIN_FULLSCREEN_DESC fullscreenDesc;
  5943. DXGI_FORMAT swapchainFormat;
  5944. IDXGIFactory1 *pParent;
  5945. IDXGISwapChain1 *swapchain;
  5946. IDXGISwapChain3 *swapchain3;
  5947. HRESULT res;
  5948. // Get the DXGI handle
  5949. #ifdef _WIN32
  5950. dxgiHandle = (HWND)SDL_GetPointerProperty(SDL_GetWindowProperties(windowData->window), SDL_PROP_WINDOW_WIN32_HWND_POINTER, NULL);
  5951. #else
  5952. dxgiHandle = (HWND)windowData->window;
  5953. #endif
  5954. swapchainFormat = SwapchainCompositionToTextureFormat[swapchainComposition];
  5955. // Min swapchain image count is 2
  5956. windowData->swapchainTextureCount = SDL_clamp(renderer->allowedFramesInFlight, 2, 3);
  5957. // Initialize the swapchain buffer descriptor
  5958. swapchainDesc.Width = 0; // use client window width
  5959. swapchainDesc.Height = 0; // use client window height
  5960. swapchainDesc.Format = swapchainFormat;
  5961. swapchainDesc.SampleDesc.Count = 1;
  5962. swapchainDesc.SampleDesc.Quality = 0;
  5963. swapchainDesc.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
  5964. swapchainDesc.BufferCount = windowData->swapchainTextureCount;
  5965. swapchainDesc.Scaling = DXGI_SCALING_NONE;
  5966. swapchainDesc.SwapEffect = DXGI_SWAP_EFFECT_FLIP_DISCARD;
  5967. swapchainDesc.AlphaMode = DXGI_ALPHA_MODE_UNSPECIFIED;
  5968. swapchainDesc.Flags = 0;
  5969. swapchainDesc.Stereo = 0;
  5970. // Initialize the fullscreen descriptor (if needed)
  5971. fullscreenDesc.RefreshRate.Numerator = 0;
  5972. fullscreenDesc.RefreshRate.Denominator = 0;
  5973. fullscreenDesc.ScanlineOrdering = DXGI_MODE_SCANLINE_ORDER_UNSPECIFIED;
  5974. fullscreenDesc.Scaling = DXGI_MODE_SCALING_UNSPECIFIED;
  5975. fullscreenDesc.Windowed = true;
  5976. if (renderer->supportsTearing) {
  5977. swapchainDesc.Flags = DXGI_SWAP_CHAIN_FLAG_ALLOW_TEARING;
  5978. } else {
  5979. swapchainDesc.Flags = 0;
  5980. }
  5981. if (!IsWindow(dxgiHandle)) {
  5982. return false;
  5983. }
  5984. // Create the swapchain!
  5985. res = IDXGIFactory4_CreateSwapChainForHwnd(
  5986. renderer->factory,
  5987. (IUnknown *)renderer->commandQueue,
  5988. dxgiHandle,
  5989. &swapchainDesc,
  5990. &fullscreenDesc,
  5991. NULL,
  5992. &swapchain);
  5993. CHECK_D3D12_ERROR_AND_RETURN("Could not create swapchain", false);
  5994. res = IDXGISwapChain1_QueryInterface(
  5995. swapchain,
  5996. D3D_GUID(D3D_IID_IDXGISwapChain3),
  5997. (void **)&swapchain3);
  5998. IDXGISwapChain1_Release(swapchain);
  5999. CHECK_D3D12_ERROR_AND_RETURN("Could not create IDXGISwapChain3", false);
  6000. if (swapchainComposition != SDL_GPU_SWAPCHAINCOMPOSITION_SDR) {
  6001. // Support already verified if we hit this block
  6002. IDXGISwapChain3_SetColorSpace1(
  6003. swapchain3,
  6004. SwapchainCompositionToColorSpace[swapchainComposition]);
  6005. }
  6006. /*
  6007. * The swapchain's parent is a separate factory from the factory that
  6008. * we used to create the swapchain, and only that parent can be used to
  6009. * set the window association. Trying to set an association on our factory
  6010. * will silently fail and doesn't even verify arguments or return errors.
  6011. * See https://gamedev.net/forums/topic/634235-dxgidisabling-altenter/4999955/
  6012. */
  6013. res = IDXGISwapChain3_GetParent(
  6014. swapchain3,
  6015. D3D_GUID(D3D_IID_IDXGIFactory1),
  6016. (void **)&pParent);
  6017. if (FAILED(res)) {
  6018. SDL_LogWarn(
  6019. SDL_LOG_CATEGORY_GPU,
  6020. "Could not get swapchain parent! Error Code: " HRESULT_FMT,
  6021. res);
  6022. } else {
  6023. // Disable DXGI window crap
  6024. res = IDXGIFactory1_MakeWindowAssociation(
  6025. pParent,
  6026. dxgiHandle,
  6027. DXGI_MWA_NO_WINDOW_CHANGES);
  6028. if (FAILED(res)) {
  6029. SDL_LogWarn(
  6030. SDL_LOG_CATEGORY_GPU,
  6031. "MakeWindowAssociation failed! Error Code: " HRESULT_FMT,
  6032. res);
  6033. }
  6034. // We're done with the parent now
  6035. IDXGIFactory1_Release(pParent);
  6036. }
  6037. IDXGISwapChain3_GetDesc1(swapchain3, &swapchainDesc);
  6038. CHECK_D3D12_ERROR_AND_RETURN("Failed to retrieve swapchain descriptor!", false);
  6039. // Initialize the swapchain data
  6040. windowData->swapchain = swapchain3;
  6041. windowData->present_mode = presentMode;
  6042. windowData->swapchainComposition = swapchainComposition;
  6043. windowData->swapchainColorSpace = SwapchainCompositionToColorSpace[swapchainComposition];
  6044. windowData->frameCounter = 0;
  6045. windowData->width = swapchainDesc.Width;
  6046. windowData->height = swapchainDesc.Height;
  6047. // Precache blit pipelines for the swapchain format
  6048. for (Uint32 i = 0; i < 5; i += 1) {
  6049. SDL_GPU_FetchBlitPipeline(
  6050. renderer->sdlGPUDevice,
  6051. (SDL_GPUTextureType)i,
  6052. SwapchainCompositionToSDLTextureFormat[swapchainComposition],
  6053. renderer->blitVertexShader,
  6054. renderer->blitFrom2DShader,
  6055. renderer->blitFrom2DArrayShader,
  6056. renderer->blitFrom3DShader,
  6057. renderer->blitFromCubeShader,
  6058. renderer->blitFromCubeArrayShader,
  6059. &renderer->blitPipelines,
  6060. &renderer->blitPipelineCount,
  6061. &renderer->blitPipelineCapacity);
  6062. }
  6063. /* If a you are using a FLIP model format you can't create the swapchain as DXGI_FORMAT_B8G8R8A8_UNORM_SRGB.
  6064. * You have to create the swapchain as DXGI_FORMAT_B8G8R8A8_UNORM and then set the render target view's format to DXGI_FORMAT_B8G8R8A8_UNORM_SRGB
  6065. */
  6066. for (Uint32 i = 0; i < windowData->swapchainTextureCount; i += 1) {
  6067. if (!D3D12_INTERNAL_InitializeSwapchainTexture(
  6068. renderer,
  6069. swapchain3,
  6070. swapchainComposition,
  6071. i,
  6072. &windowData->textureContainers[i])) {
  6073. IDXGISwapChain3_Release(swapchain3);
  6074. return false;
  6075. }
  6076. }
  6077. return true;
  6078. }
  6079. #endif
  6080. static bool D3D12_ClaimWindow(
  6081. SDL_GPURenderer *driverData,
  6082. SDL_Window *window)
  6083. {
  6084. D3D12Renderer *renderer = (D3D12Renderer *)driverData;
  6085. D3D12WindowData *windowData = D3D12_INTERNAL_FetchWindowData(window);
  6086. if (windowData == NULL) {
  6087. windowData = (D3D12WindowData *)SDL_calloc(1, sizeof(D3D12WindowData));
  6088. if (!windowData) {
  6089. return false;
  6090. }
  6091. windowData->window = window;
  6092. windowData->renderer = renderer;
  6093. windowData->refcount = 1;
  6094. if (D3D12_INTERNAL_CreateSwapchain(renderer, windowData, SDL_GPU_SWAPCHAINCOMPOSITION_SDR, SDL_GPU_PRESENTMODE_VSYNC)) {
  6095. SDL_SetPointerProperty(SDL_GetWindowProperties(window), WINDOW_PROPERTY_DATA, windowData);
  6096. SDL_LockMutex(renderer->windowLock);
  6097. if (renderer->claimedWindowCount >= renderer->claimedWindowCapacity) {
  6098. renderer->claimedWindowCapacity *= 2;
  6099. renderer->claimedWindows = (D3D12WindowData **)SDL_realloc(
  6100. renderer->claimedWindows,
  6101. renderer->claimedWindowCapacity * sizeof(D3D12WindowData *));
  6102. }
  6103. renderer->claimedWindows[renderer->claimedWindowCount] = windowData;
  6104. renderer->claimedWindowCount += 1;
  6105. SDL_UnlockMutex(renderer->windowLock);
  6106. SDL_AddWindowEventWatch(SDL_WINDOW_EVENT_WATCH_NORMAL, D3D12_INTERNAL_OnWindowResize, window);
  6107. return true;
  6108. } else {
  6109. SDL_free(windowData);
  6110. return false;
  6111. }
  6112. } else if (windowData->renderer == renderer) {
  6113. ++windowData->refcount;
  6114. return true;
  6115. } else {
  6116. SET_STRING_ERROR_AND_RETURN("Window already claimed", false);
  6117. }
  6118. }
  6119. static void D3D12_ReleaseWindow(
  6120. SDL_GPURenderer *driverData,
  6121. SDL_Window *window)
  6122. {
  6123. D3D12Renderer *renderer = (D3D12Renderer *)driverData;
  6124. D3D12WindowData *windowData = D3D12_INTERNAL_FetchWindowData(window);
  6125. if (windowData == NULL) {
  6126. return;
  6127. }
  6128. if (windowData->renderer != renderer) {
  6129. SDL_SetError("Window not claimed by this device");
  6130. return;
  6131. }
  6132. if (windowData->refcount > 1) {
  6133. --windowData->refcount;
  6134. return;
  6135. }
  6136. D3D12_Wait(driverData);
  6137. for (Uint32 i = 0; i < MAX_FRAMES_IN_FLIGHT; i += 1) {
  6138. if (windowData->inFlightFences[i] != NULL) {
  6139. D3D12_ReleaseFence(
  6140. driverData,
  6141. windowData->inFlightFences[i]);
  6142. windowData->inFlightFences[i] = NULL;
  6143. }
  6144. }
  6145. D3D12_INTERNAL_DestroySwapchain(renderer, windowData);
  6146. SDL_LockMutex(renderer->windowLock);
  6147. for (Uint32 i = 0; i < renderer->claimedWindowCount; i += 1) {
  6148. if (renderer->claimedWindows[i]->window == window) {
  6149. renderer->claimedWindows[i] = renderer->claimedWindows[renderer->claimedWindowCount - 1];
  6150. renderer->claimedWindowCount -= 1;
  6151. break;
  6152. }
  6153. }
  6154. SDL_UnlockMutex(renderer->windowLock);
  6155. SDL_free(windowData);
  6156. SDL_ClearProperty(SDL_GetWindowProperties(window), WINDOW_PROPERTY_DATA);
  6157. SDL_RemoveWindowEventWatch(SDL_WINDOW_EVENT_WATCH_NORMAL, D3D12_INTERNAL_OnWindowResize, window);
  6158. }
  6159. static bool D3D12_SetSwapchainParameters(
  6160. SDL_GPURenderer *driverData,
  6161. SDL_Window *window,
  6162. SDL_GPUSwapchainComposition swapchainComposition,
  6163. SDL_GPUPresentMode presentMode)
  6164. {
  6165. D3D12Renderer *renderer = (D3D12Renderer *)driverData;
  6166. D3D12WindowData *windowData = D3D12_INTERNAL_FetchWindowData(window);
  6167. if (windowData == NULL) {
  6168. SET_STRING_ERROR_AND_RETURN("Cannot set swapchain parameters on unclaimed window!", false);
  6169. }
  6170. if (!D3D12_SupportsSwapchainComposition(driverData, window, swapchainComposition)) {
  6171. SET_STRING_ERROR_AND_RETURN("Swapchain composition not supported!", false);
  6172. }
  6173. if (!D3D12_SupportsPresentMode(driverData, window, presentMode)) {
  6174. SET_STRING_ERROR_AND_RETURN("Present mode not supported!", false);
  6175. }
  6176. if (
  6177. swapchainComposition != windowData->swapchainComposition ||
  6178. presentMode != windowData->present_mode) {
  6179. D3D12_Wait(driverData);
  6180. // Recreate the swapchain
  6181. D3D12_INTERNAL_DestroySwapchain(
  6182. renderer,
  6183. windowData);
  6184. return D3D12_INTERNAL_CreateSwapchain(
  6185. renderer,
  6186. windowData,
  6187. swapchainComposition,
  6188. presentMode);
  6189. }
  6190. return true;
  6191. }
  6192. static bool D3D12_SetAllowedFramesInFlight(
  6193. SDL_GPURenderer *driverData,
  6194. Uint32 allowedFramesInFlight)
  6195. {
  6196. D3D12Renderer *renderer = (D3D12Renderer *)driverData;
  6197. if (!D3D12_Wait(driverData)) {
  6198. return false;
  6199. }
  6200. // Destroy all swapchains
  6201. for (Uint32 i = 0; i < renderer->claimedWindowCount; i += 1) {
  6202. D3D12WindowData *windowData = renderer->claimedWindows[i];
  6203. D3D12_INTERNAL_DestroySwapchain(renderer, windowData);
  6204. }
  6205. // Set the frames in flight value
  6206. renderer->allowedFramesInFlight = allowedFramesInFlight;
  6207. // Recreate all swapchains
  6208. for (Uint32 i = 0; i < renderer->claimedWindowCount; i += 1) {
  6209. D3D12WindowData *windowData = renderer->claimedWindows[i];
  6210. if (!D3D12_INTERNAL_CreateSwapchain(
  6211. renderer,
  6212. windowData,
  6213. windowData->swapchainComposition,
  6214. windowData->present_mode)) {
  6215. return false;
  6216. }
  6217. }
  6218. return true;
  6219. }
  6220. static SDL_GPUTextureFormat D3D12_GetSwapchainTextureFormat(
  6221. SDL_GPURenderer *driverData,
  6222. SDL_Window *window)
  6223. {
  6224. D3D12Renderer *renderer = (D3D12Renderer *)driverData;
  6225. D3D12WindowData *windowData = D3D12_INTERNAL_FetchWindowData(window);
  6226. if (windowData == NULL) {
  6227. SET_STRING_ERROR_AND_RETURN("Cannot get swapchain format, window has not been claimed!", SDL_GPU_TEXTUREFORMAT_INVALID);
  6228. }
  6229. return windowData->textureContainers[windowData->frameCounter].header.info.format;
  6230. }
  6231. static D3D12Fence *D3D12_INTERNAL_AcquireFence(
  6232. D3D12Renderer *renderer)
  6233. {
  6234. D3D12Fence *fence;
  6235. ID3D12Fence *handle;
  6236. HRESULT res;
  6237. SDL_LockMutex(renderer->fenceLock);
  6238. if (renderer->availableFenceCount == 0) {
  6239. res = ID3D12Device_CreateFence(
  6240. renderer->device,
  6241. D3D12_FENCE_UNSIGNALED_VALUE,
  6242. D3D12_FENCE_FLAG_NONE,
  6243. D3D_GUID(D3D_IID_ID3D12Fence),
  6244. (void **)&handle);
  6245. if (FAILED(res)) {
  6246. D3D12_INTERNAL_SetError(renderer, "Failed to create fence!", res);
  6247. SDL_UnlockMutex(renderer->fenceLock);
  6248. return NULL;
  6249. }
  6250. fence = (D3D12Fence *)SDL_calloc(1, sizeof(D3D12Fence));
  6251. if (!fence) {
  6252. ID3D12Fence_Release(handle);
  6253. SDL_UnlockMutex(renderer->fenceLock);
  6254. return NULL;
  6255. }
  6256. fence->handle = handle;
  6257. fence->event = CreateEvent(NULL, FALSE, FALSE, NULL);
  6258. SDL_SetAtomicInt(&fence->referenceCount, 0);
  6259. } else {
  6260. fence = renderer->availableFences[renderer->availableFenceCount - 1];
  6261. renderer->availableFenceCount -= 1;
  6262. ID3D12Fence_Signal(fence->handle, D3D12_FENCE_UNSIGNALED_VALUE);
  6263. }
  6264. SDL_UnlockMutex(renderer->fenceLock);
  6265. (void)SDL_AtomicIncRef(&fence->referenceCount);
  6266. return fence;
  6267. }
  6268. static bool D3D12_INTERNAL_AllocateCommandBuffer(
  6269. D3D12Renderer *renderer)
  6270. {
  6271. D3D12CommandBuffer *commandBuffer;
  6272. HRESULT res;
  6273. ID3D12CommandAllocator *commandAllocator;
  6274. ID3D12GraphicsCommandList *commandList;
  6275. commandBuffer = (D3D12CommandBuffer *)SDL_calloc(1, sizeof(D3D12CommandBuffer));
  6276. if (!commandBuffer) {
  6277. SET_STRING_ERROR_AND_RETURN("Failed to create ID3D12CommandList. Out of Memory", false);
  6278. }
  6279. res = ID3D12Device_CreateCommandAllocator(
  6280. renderer->device,
  6281. D3D12_COMMAND_LIST_TYPE_DIRECT,
  6282. D3D_GUID(D3D_IID_ID3D12CommandAllocator),
  6283. (void **)&commandAllocator);
  6284. if (FAILED(res)) {
  6285. D3D12_INTERNAL_SetError(renderer, "Failed to create ID3D12CommandAllocator", res);
  6286. D3D12_INTERNAL_DestroyCommandBuffer(commandBuffer);
  6287. return false;
  6288. }
  6289. commandBuffer->commandAllocator = commandAllocator;
  6290. res = ID3D12Device_CreateCommandList(
  6291. renderer->device,
  6292. 0,
  6293. D3D12_COMMAND_LIST_TYPE_DIRECT,
  6294. commandAllocator,
  6295. NULL,
  6296. D3D_GUID(D3D_IID_ID3D12GraphicsCommandList),
  6297. (void **)&commandList);
  6298. if (FAILED(res)) {
  6299. D3D12_INTERNAL_SetError(renderer, "Failed to create ID3D12CommandList", res);
  6300. D3D12_INTERNAL_DestroyCommandBuffer(commandBuffer);
  6301. return false;
  6302. }
  6303. commandBuffer->graphicsCommandList = commandList;
  6304. commandBuffer->renderer = renderer;
  6305. commandBuffer->inFlightFence = NULL;
  6306. // Window handling
  6307. commandBuffer->presentDataCapacity = 1;
  6308. commandBuffer->presentDataCount = 0;
  6309. commandBuffer->presentDatas = (D3D12PresentData *)SDL_calloc(
  6310. commandBuffer->presentDataCapacity, sizeof(D3D12PresentData));
  6311. // Resource tracking
  6312. commandBuffer->usedTextureCapacity = 4;
  6313. commandBuffer->usedTextureCount = 0;
  6314. commandBuffer->usedTextures = (D3D12Texture **)SDL_calloc(
  6315. commandBuffer->usedTextureCapacity, sizeof(D3D12Texture *));
  6316. commandBuffer->usedBufferCapacity = 4;
  6317. commandBuffer->usedBufferCount = 0;
  6318. commandBuffer->usedBuffers = (D3D12Buffer **)SDL_calloc(
  6319. commandBuffer->usedBufferCapacity, sizeof(D3D12Buffer *));
  6320. commandBuffer->usedSamplerCapacity = 4;
  6321. commandBuffer->usedSamplerCount = 0;
  6322. commandBuffer->usedSamplers = (D3D12Sampler **)SDL_calloc(
  6323. commandBuffer->usedSamplerCapacity, sizeof(D3D12Sampler *));
  6324. commandBuffer->usedGraphicsPipelineCapacity = 4;
  6325. commandBuffer->usedGraphicsPipelineCount = 0;
  6326. commandBuffer->usedGraphicsPipelines = (D3D12GraphicsPipeline **)SDL_calloc(
  6327. commandBuffer->usedGraphicsPipelineCapacity, sizeof(D3D12GraphicsPipeline *));
  6328. commandBuffer->usedComputePipelineCapacity = 4;
  6329. commandBuffer->usedComputePipelineCount = 0;
  6330. commandBuffer->usedComputePipelines = (D3D12ComputePipeline **)SDL_calloc(
  6331. commandBuffer->usedComputePipelineCapacity, sizeof(D3D12ComputePipeline *));
  6332. commandBuffer->usedDescriptorHeapCapacity = 4;
  6333. commandBuffer->usedDescriptorHeapCount = 0;
  6334. commandBuffer->usedDescriptorHeaps = (D3D12DescriptorHeap **)SDL_calloc(
  6335. commandBuffer->usedDescriptorHeapCapacity, sizeof(D3D12DescriptorHeap *));
  6336. commandBuffer->usedUniformBufferCapacity = 4;
  6337. commandBuffer->usedUniformBufferCount = 0;
  6338. commandBuffer->usedUniformBuffers = (D3D12UniformBuffer **)SDL_calloc(
  6339. commandBuffer->usedUniformBufferCapacity, sizeof(D3D12UniformBuffer *));
  6340. commandBuffer->textureDownloadCapacity = 4;
  6341. commandBuffer->textureDownloadCount = 0;
  6342. commandBuffer->textureDownloads = (D3D12TextureDownload **)SDL_calloc(
  6343. commandBuffer->textureDownloadCapacity, sizeof(D3D12TextureDownload *));
  6344. if (
  6345. (!commandBuffer->presentDatas) ||
  6346. (!commandBuffer->usedTextures) ||
  6347. (!commandBuffer->usedBuffers) ||
  6348. (!commandBuffer->usedSamplers) ||
  6349. (!commandBuffer->usedGraphicsPipelines) ||
  6350. (!commandBuffer->usedComputePipelines) ||
  6351. (!commandBuffer->usedUniformBuffers) ||
  6352. (!commandBuffer->textureDownloads)) {
  6353. D3D12_INTERNAL_DestroyCommandBuffer(commandBuffer);
  6354. SET_STRING_ERROR_AND_RETURN("Failed to create ID3D12CommandList. Out of Memory", false);
  6355. }
  6356. D3D12CommandBuffer **resizedAvailableCommandBuffers = (D3D12CommandBuffer **)SDL_realloc(
  6357. renderer->availableCommandBuffers,
  6358. sizeof(D3D12CommandBuffer *) * (renderer->availableCommandBufferCapacity + 1));
  6359. if (!resizedAvailableCommandBuffers) {
  6360. D3D12_INTERNAL_DestroyCommandBuffer(commandBuffer);
  6361. SET_STRING_ERROR_AND_RETURN("Failed to create ID3D12CommandList. Out of Memory", false);
  6362. }
  6363. // Add to inactive command buffer array
  6364. renderer->availableCommandBufferCapacity += 1;
  6365. renderer->availableCommandBuffers = resizedAvailableCommandBuffers;
  6366. renderer->availableCommandBuffers[renderer->availableCommandBufferCount] = commandBuffer;
  6367. renderer->availableCommandBufferCount += 1;
  6368. return true;
  6369. }
  6370. static D3D12CommandBuffer *D3D12_INTERNAL_AcquireCommandBufferFromPool(
  6371. D3D12Renderer *renderer)
  6372. {
  6373. D3D12CommandBuffer *commandBuffer;
  6374. if (renderer->availableCommandBufferCount == 0) {
  6375. if (!D3D12_INTERNAL_AllocateCommandBuffer(renderer)) {
  6376. return NULL;
  6377. }
  6378. }
  6379. commandBuffer = renderer->availableCommandBuffers[renderer->availableCommandBufferCount - 1];
  6380. renderer->availableCommandBufferCount -= 1;
  6381. return commandBuffer;
  6382. }
  6383. static SDL_GPUCommandBuffer *D3D12_AcquireCommandBuffer(
  6384. SDL_GPURenderer *driverData)
  6385. {
  6386. D3D12Renderer *renderer = (D3D12Renderer *)driverData;
  6387. D3D12CommandBuffer *commandBuffer;
  6388. ID3D12DescriptorHeap *heaps[2];
  6389. SDL_zeroa(heaps);
  6390. SDL_LockMutex(renderer->acquireCommandBufferLock);
  6391. commandBuffer = D3D12_INTERNAL_AcquireCommandBufferFromPool(renderer);
  6392. SDL_UnlockMutex(renderer->acquireCommandBufferLock);
  6393. if (commandBuffer == NULL) {
  6394. return NULL;
  6395. }
  6396. // Set the bind state
  6397. commandBuffer->currentGraphicsPipeline = NULL;
  6398. SDL_zeroa(commandBuffer->colorTargetSubresources);
  6399. SDL_zeroa(commandBuffer->colorResolveSubresources);
  6400. commandBuffer->depthStencilTextureSubresource = NULL;
  6401. SDL_zeroa(commandBuffer->vertexBuffers);
  6402. SDL_zeroa(commandBuffer->vertexBufferOffsets);
  6403. commandBuffer->vertexBufferCount = 0;
  6404. SDL_zeroa(commandBuffer->vertexSamplerTextureDescriptorHandles);
  6405. SDL_zeroa(commandBuffer->vertexSamplerDescriptorHandles);
  6406. SDL_zeroa(commandBuffer->vertexStorageTextureDescriptorHandles);
  6407. SDL_zeroa(commandBuffer->vertexStorageBufferDescriptorHandles);
  6408. SDL_zeroa(commandBuffer->vertexUniformBuffers);
  6409. SDL_zeroa(commandBuffer->fragmentSamplerTextureDescriptorHandles);
  6410. SDL_zeroa(commandBuffer->fragmentSamplerDescriptorHandles);
  6411. SDL_zeroa(commandBuffer->fragmentStorageTextureDescriptorHandles);
  6412. SDL_zeroa(commandBuffer->fragmentStorageBufferDescriptorHandles);
  6413. SDL_zeroa(commandBuffer->fragmentUniformBuffers);
  6414. SDL_zeroa(commandBuffer->computeSamplerTextureDescriptorHandles);
  6415. SDL_zeroa(commandBuffer->computeSamplerDescriptorHandles);
  6416. SDL_zeroa(commandBuffer->computeReadOnlyStorageTextureDescriptorHandles);
  6417. SDL_zeroa(commandBuffer->computeReadOnlyStorageBufferDescriptorHandles);
  6418. SDL_zeroa(commandBuffer->computeReadOnlyStorageTextures);
  6419. SDL_zeroa(commandBuffer->computeReadOnlyStorageBuffers);
  6420. SDL_zeroa(commandBuffer->computeReadWriteStorageTextureSubresources);
  6421. SDL_zeroa(commandBuffer->computeReadWriteStorageBuffers);
  6422. SDL_zeroa(commandBuffer->computeUniformBuffers);
  6423. commandBuffer->autoReleaseFence = true;
  6424. return (SDL_GPUCommandBuffer *)commandBuffer;
  6425. }
  6426. static bool D3D12_WaitForSwapchain(
  6427. SDL_GPURenderer *driverData,
  6428. SDL_Window *window)
  6429. {
  6430. D3D12Renderer *renderer = (D3D12Renderer *)driverData;
  6431. D3D12WindowData *windowData = D3D12_INTERNAL_FetchWindowData(window);
  6432. if (windowData == NULL) {
  6433. SET_STRING_ERROR_AND_RETURN("Cannot wait for a swapchain from an unclaimed window!", false);
  6434. }
  6435. if (windowData->inFlightFences[windowData->frameCounter] != NULL) {
  6436. if (!D3D12_WaitForFences(
  6437. driverData,
  6438. true,
  6439. &windowData->inFlightFences[windowData->frameCounter],
  6440. 1)) {
  6441. return false;
  6442. }
  6443. }
  6444. return true;
  6445. }
  6446. static bool D3D12_INTERNAL_AcquireSwapchainTexture(
  6447. bool block,
  6448. SDL_GPUCommandBuffer *commandBuffer,
  6449. SDL_Window *window,
  6450. SDL_GPUTexture **swapchainTexture,
  6451. Uint32 *swapchainTextureWidth,
  6452. Uint32 *swapchainTextureHeight)
  6453. {
  6454. D3D12CommandBuffer *d3d12CommandBuffer = (D3D12CommandBuffer *)commandBuffer;
  6455. D3D12Renderer *renderer = d3d12CommandBuffer->renderer;
  6456. D3D12WindowData *windowData;
  6457. Uint32 swapchainIndex;
  6458. HRESULT res;
  6459. *swapchainTexture = NULL;
  6460. if (swapchainTextureWidth) {
  6461. *swapchainTextureWidth = 0;
  6462. }
  6463. if (swapchainTextureHeight) {
  6464. *swapchainTextureHeight = 0;
  6465. }
  6466. windowData = D3D12_INTERNAL_FetchWindowData(window);
  6467. if (windowData == NULL) {
  6468. SET_STRING_ERROR_AND_RETURN("Cannot acquire swapchain texture from an unclaimed window!", false);
  6469. }
  6470. if (windowData->needsSwapchainRecreate) {
  6471. if (!D3D12_INTERNAL_ResizeSwapchain(renderer, windowData)) {
  6472. return false;
  6473. }
  6474. }
  6475. if (swapchainTextureWidth) {
  6476. *swapchainTextureWidth = windowData->width;
  6477. }
  6478. if (swapchainTextureHeight) {
  6479. *swapchainTextureHeight = windowData->height;
  6480. }
  6481. if (windowData->inFlightFences[windowData->frameCounter] != NULL) {
  6482. if (block) {
  6483. // In VSYNC mode, block until the least recent presented frame is done
  6484. if (!D3D12_WaitForFences(
  6485. (SDL_GPURenderer *)renderer,
  6486. true,
  6487. &windowData->inFlightFences[windowData->frameCounter],
  6488. 1)) {
  6489. return false;
  6490. }
  6491. } else {
  6492. // If we are not blocking and the least recent fence is not signaled,
  6493. // return true to indicate that there is no error but rendering should be skipped.
  6494. if (!D3D12_QueryFence(
  6495. (SDL_GPURenderer *)renderer,
  6496. windowData->inFlightFences[windowData->frameCounter])) {
  6497. return true;
  6498. }
  6499. }
  6500. D3D12_ReleaseFence(
  6501. (SDL_GPURenderer *)renderer,
  6502. windowData->inFlightFences[windowData->frameCounter]);
  6503. windowData->inFlightFences[windowData->frameCounter] = NULL;
  6504. }
  6505. #if (defined(SDL_PLATFORM_XBOXONE) || defined(SDL_PLATFORM_XBOXSERIES))
  6506. // FIXME: Should this happen before the inFlightFences stuff above?
  6507. windowData->frameToken = D3D12XBOX_FRAME_PIPELINE_TOKEN_NULL;
  6508. renderer->device->WaitFrameEventX(D3D12XBOX_FRAME_EVENT_ORIGIN, INFINITE, NULL, D3D12XBOX_WAIT_FRAME_EVENT_FLAG_NONE, &windowData->frameToken);
  6509. swapchainIndex = windowData->frameCounter;
  6510. #else
  6511. swapchainIndex = IDXGISwapChain3_GetCurrentBackBufferIndex(windowData->swapchain);
  6512. // Set the handle on the windowData texture data.
  6513. res = IDXGISwapChain_GetBuffer(
  6514. windowData->swapchain,
  6515. swapchainIndex,
  6516. D3D_GUID(D3D_IID_ID3D12Resource),
  6517. (void **)&windowData->textureContainers[swapchainIndex].activeTexture->resource);
  6518. CHECK_D3D12_ERROR_AND_RETURN("Could not acquire swapchain!", false);
  6519. #endif
  6520. // Set up presentation
  6521. if (d3d12CommandBuffer->presentDataCount == d3d12CommandBuffer->presentDataCapacity) {
  6522. d3d12CommandBuffer->presentDataCapacity += 1;
  6523. d3d12CommandBuffer->presentDatas = (D3D12PresentData *)SDL_realloc(
  6524. d3d12CommandBuffer->presentDatas,
  6525. d3d12CommandBuffer->presentDataCapacity * sizeof(D3D12PresentData));
  6526. }
  6527. d3d12CommandBuffer->presentDatas[d3d12CommandBuffer->presentDataCount].windowData = windowData;
  6528. d3d12CommandBuffer->presentDatas[d3d12CommandBuffer->presentDataCount].swapchainImageIndex = swapchainIndex;
  6529. d3d12CommandBuffer->presentDataCount += 1;
  6530. // Set up resource barrier
  6531. D3D12_RESOURCE_BARRIER barrierDesc;
  6532. barrierDesc.Type = D3D12_RESOURCE_BARRIER_TYPE_TRANSITION;
  6533. barrierDesc.Flags = D3D12_RESOURCE_BARRIER_FLAG_NONE;
  6534. barrierDesc.Transition.StateBefore = D3D12_RESOURCE_STATE_PRESENT;
  6535. barrierDesc.Transition.StateAfter = D3D12_RESOURCE_STATE_RENDER_TARGET;
  6536. barrierDesc.Transition.pResource = windowData->textureContainers[swapchainIndex].activeTexture->resource;
  6537. barrierDesc.Transition.Subresource = 0;
  6538. ID3D12GraphicsCommandList_ResourceBarrier(
  6539. d3d12CommandBuffer->graphicsCommandList,
  6540. 1,
  6541. &barrierDesc);
  6542. *swapchainTexture = (SDL_GPUTexture *)&windowData->textureContainers[swapchainIndex];
  6543. return true;
  6544. }
  6545. static bool D3D12_AcquireSwapchainTexture(
  6546. SDL_GPUCommandBuffer *command_buffer,
  6547. SDL_Window *window,
  6548. SDL_GPUTexture **swapchain_texture,
  6549. Uint32 *swapchain_texture_width,
  6550. Uint32 *swapchain_texture_height
  6551. ) {
  6552. return D3D12_INTERNAL_AcquireSwapchainTexture(
  6553. false,
  6554. command_buffer,
  6555. window,
  6556. swapchain_texture,
  6557. swapchain_texture_width,
  6558. swapchain_texture_height);
  6559. }
  6560. static bool D3D12_WaitAndAcquireSwapchainTexture(
  6561. SDL_GPUCommandBuffer *command_buffer,
  6562. SDL_Window *window,
  6563. SDL_GPUTexture **swapchain_texture,
  6564. Uint32 *swapchain_texture_width,
  6565. Uint32 *swapchain_texture_height
  6566. ) {
  6567. return D3D12_INTERNAL_AcquireSwapchainTexture(
  6568. true,
  6569. command_buffer,
  6570. window,
  6571. swapchain_texture,
  6572. swapchain_texture_width,
  6573. swapchain_texture_height);
  6574. }
  6575. static void D3D12_INTERNAL_PerformPendingDestroys(D3D12Renderer *renderer)
  6576. {
  6577. SDL_LockMutex(renderer->disposeLock);
  6578. for (Sint32 i = renderer->buffersToDestroyCount - 1; i >= 0; i -= 1) {
  6579. if (SDL_GetAtomicInt(&renderer->buffersToDestroy[i]->referenceCount) == 0) {
  6580. D3D12_INTERNAL_DestroyBuffer(
  6581. renderer->buffersToDestroy[i]);
  6582. renderer->buffersToDestroy[i] = renderer->buffersToDestroy[renderer->buffersToDestroyCount - 1];
  6583. renderer->buffersToDestroyCount -= 1;
  6584. }
  6585. }
  6586. for (Sint32 i = renderer->texturesToDestroyCount - 1; i >= 0; i -= 1) {
  6587. if (SDL_GetAtomicInt(&renderer->texturesToDestroy[i]->referenceCount) == 0) {
  6588. D3D12_INTERNAL_DestroyTexture(
  6589. renderer->texturesToDestroy[i]);
  6590. renderer->texturesToDestroy[i] = renderer->texturesToDestroy[renderer->texturesToDestroyCount - 1];
  6591. renderer->texturesToDestroyCount -= 1;
  6592. }
  6593. }
  6594. for (Sint32 i = renderer->samplersToDestroyCount - 1; i >= 0; i -= 1) {
  6595. if (SDL_GetAtomicInt(&renderer->samplersToDestroy[i]->referenceCount) == 0) {
  6596. D3D12_INTERNAL_DestroySampler(
  6597. renderer->samplersToDestroy[i]);
  6598. renderer->samplersToDestroy[i] = renderer->samplersToDestroy[renderer->samplersToDestroyCount - 1];
  6599. renderer->samplersToDestroyCount -= 1;
  6600. }
  6601. }
  6602. for (Sint32 i = renderer->graphicsPipelinesToDestroyCount - 1; i >= 0; i -= 1) {
  6603. if (SDL_GetAtomicInt(&renderer->graphicsPipelinesToDestroy[i]->referenceCount) == 0) {
  6604. D3D12_INTERNAL_DestroyGraphicsPipeline(
  6605. renderer->graphicsPipelinesToDestroy[i]);
  6606. renderer->graphicsPipelinesToDestroy[i] = renderer->graphicsPipelinesToDestroy[renderer->graphicsPipelinesToDestroyCount - 1];
  6607. renderer->graphicsPipelinesToDestroyCount -= 1;
  6608. }
  6609. }
  6610. for (Sint32 i = renderer->computePipelinesToDestroyCount - 1; i >= 0; i -= 1) {
  6611. if (SDL_GetAtomicInt(&renderer->computePipelinesToDestroy[i]->referenceCount) == 0) {
  6612. D3D12_INTERNAL_DestroyComputePipeline(
  6613. renderer->computePipelinesToDestroy[i]);
  6614. renderer->computePipelinesToDestroy[i] = renderer->computePipelinesToDestroy[renderer->computePipelinesToDestroyCount - 1];
  6615. renderer->computePipelinesToDestroyCount -= 1;
  6616. }
  6617. }
  6618. SDL_UnlockMutex(renderer->disposeLock);
  6619. }
  6620. static bool D3D12_INTERNAL_CopyTextureDownload(
  6621. D3D12CommandBuffer *commandBuffer,
  6622. D3D12TextureDownload *download)
  6623. {
  6624. D3D12Renderer *renderer = commandBuffer->renderer;
  6625. Uint8 *sourcePtr;
  6626. Uint8 *destPtr;
  6627. HRESULT res;
  6628. res = ID3D12Resource_Map(
  6629. download->temporaryBuffer->handle,
  6630. 0,
  6631. NULL,
  6632. (void **)&sourcePtr);
  6633. CHECK_D3D12_ERROR_AND_RETURN("Failed to map temporary buffer", false);
  6634. res = ID3D12Resource_Map(
  6635. download->destinationBuffer->handle,
  6636. 0,
  6637. NULL,
  6638. (void **)&destPtr);
  6639. CHECK_D3D12_ERROR_AND_RETURN("Failed to map destination buffer", false);
  6640. for (Uint32 sliceIndex = 0; sliceIndex < download->depth; sliceIndex += 1) {
  6641. for (Uint32 rowIndex = 0; rowIndex < download->height; rowIndex += 1) {
  6642. SDL_memcpy(
  6643. destPtr + download->bufferOffset + (sliceIndex * download->bytesPerDepthSlice) + (rowIndex * download->bytesPerRow),
  6644. sourcePtr + (sliceIndex * download->height) + (rowIndex * download->alignedBytesPerRow),
  6645. download->bytesPerRow);
  6646. }
  6647. }
  6648. ID3D12Resource_Unmap(
  6649. download->temporaryBuffer->handle,
  6650. 0,
  6651. NULL);
  6652. ID3D12Resource_Unmap(
  6653. download->destinationBuffer->handle,
  6654. 0,
  6655. NULL);
  6656. return true;
  6657. }
  6658. static bool D3D12_INTERNAL_CleanCommandBuffer(
  6659. D3D12Renderer *renderer,
  6660. D3D12CommandBuffer *commandBuffer,
  6661. bool cancel)
  6662. {
  6663. Uint32 i;
  6664. HRESULT res;
  6665. bool result = true;
  6666. // Perform deferred texture data copies
  6667. for (i = 0; i < commandBuffer->textureDownloadCount; i += 1) {
  6668. if (!cancel) {
  6669. result &= D3D12_INTERNAL_CopyTextureDownload(
  6670. commandBuffer,
  6671. commandBuffer->textureDownloads[i]);
  6672. }
  6673. SDL_free(commandBuffer->textureDownloads[i]);
  6674. }
  6675. commandBuffer->textureDownloadCount = 0;
  6676. if (!result) {
  6677. return false;
  6678. }
  6679. res = ID3D12CommandAllocator_Reset(commandBuffer->commandAllocator);
  6680. CHECK_D3D12_ERROR_AND_RETURN("Could not reset command allocator", false);
  6681. res = ID3D12GraphicsCommandList_Reset(
  6682. commandBuffer->graphicsCommandList,
  6683. commandBuffer->commandAllocator,
  6684. NULL);
  6685. CHECK_D3D12_ERROR_AND_RETURN("Could not reset command list", false);
  6686. // Return descriptor heaps to pool, pools own their own locks
  6687. for (i = 0; i < commandBuffer->usedDescriptorHeapCount; i += 1) {
  6688. D3D12_INTERNAL_ReturnGPUDescriptorHeapToPool(
  6689. renderer,
  6690. commandBuffer->usedDescriptorHeaps[i]);
  6691. }
  6692. commandBuffer->usedDescriptorHeapCount = 0;
  6693. commandBuffer->gpuDescriptorHeaps[D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV] = NULL;
  6694. commandBuffer->gpuDescriptorHeaps[D3D12_DESCRIPTOR_HEAP_TYPE_SAMPLER] = NULL;
  6695. // Uniform buffers are now available
  6696. SDL_LockMutex(renderer->acquireUniformBufferLock);
  6697. for (i = 0; i < commandBuffer->usedUniformBufferCount; i += 1) {
  6698. D3D12_INTERNAL_ReturnUniformBufferToPool(
  6699. renderer,
  6700. commandBuffer->usedUniformBuffers[i]);
  6701. }
  6702. commandBuffer->usedUniformBufferCount = 0;
  6703. SDL_UnlockMutex(renderer->acquireUniformBufferLock);
  6704. // TODO: More reference counting
  6705. for (i = 0; i < commandBuffer->usedTextureCount; i += 1) {
  6706. (void)SDL_AtomicDecRef(&commandBuffer->usedTextures[i]->referenceCount);
  6707. }
  6708. commandBuffer->usedTextureCount = 0;
  6709. for (i = 0; i < commandBuffer->usedBufferCount; i += 1) {
  6710. (void)SDL_AtomicDecRef(&commandBuffer->usedBuffers[i]->referenceCount);
  6711. }
  6712. commandBuffer->usedBufferCount = 0;
  6713. for (i = 0; i < commandBuffer->usedSamplerCount; i += 1) {
  6714. (void)SDL_AtomicDecRef(&commandBuffer->usedSamplers[i]->referenceCount);
  6715. }
  6716. commandBuffer->usedSamplerCount = 0;
  6717. for (i = 0; i < commandBuffer->usedGraphicsPipelineCount; i += 1) {
  6718. (void)SDL_AtomicDecRef(&commandBuffer->usedGraphicsPipelines[i]->referenceCount);
  6719. }
  6720. commandBuffer->usedGraphicsPipelineCount = 0;
  6721. for (i = 0; i < commandBuffer->usedComputePipelineCount; i += 1) {
  6722. (void)SDL_AtomicDecRef(&commandBuffer->usedComputePipelines[i]->referenceCount);
  6723. }
  6724. commandBuffer->usedComputePipelineCount = 0;
  6725. // Reset presentation
  6726. commandBuffer->presentDataCount = 0;
  6727. // The fence is now available (unless SubmitAndAcquireFence was called)
  6728. if (commandBuffer->autoReleaseFence) {
  6729. D3D12_ReleaseFence(
  6730. (SDL_GPURenderer *)renderer,
  6731. (SDL_GPUFence *)commandBuffer->inFlightFence);
  6732. commandBuffer->inFlightFence = NULL;
  6733. }
  6734. // Return command buffer to pool
  6735. SDL_LockMutex(renderer->acquireCommandBufferLock);
  6736. if (renderer->availableCommandBufferCount == renderer->availableCommandBufferCapacity) {
  6737. renderer->availableCommandBufferCapacity += 1;
  6738. renderer->availableCommandBuffers = (D3D12CommandBuffer **)SDL_realloc(
  6739. renderer->availableCommandBuffers,
  6740. renderer->availableCommandBufferCapacity * sizeof(D3D12CommandBuffer *));
  6741. }
  6742. renderer->availableCommandBuffers[renderer->availableCommandBufferCount] = commandBuffer;
  6743. renderer->availableCommandBufferCount += 1;
  6744. SDL_UnlockMutex(renderer->acquireCommandBufferLock);
  6745. // Remove this command buffer from the submitted list
  6746. if (!cancel) {
  6747. for (i = 0; i < renderer->submittedCommandBufferCount; i += 1) {
  6748. if (renderer->submittedCommandBuffers[i] == commandBuffer) {
  6749. renderer->submittedCommandBuffers[i] = renderer->submittedCommandBuffers[renderer->submittedCommandBufferCount - 1];
  6750. renderer->submittedCommandBufferCount -= 1;
  6751. }
  6752. }
  6753. }
  6754. return true;
  6755. }
  6756. static bool D3D12_Submit(
  6757. SDL_GPUCommandBuffer *commandBuffer)
  6758. {
  6759. D3D12CommandBuffer *d3d12CommandBuffer = (D3D12CommandBuffer *)commandBuffer;
  6760. D3D12Renderer *renderer = d3d12CommandBuffer->renderer;
  6761. ID3D12CommandList *commandLists[1];
  6762. HRESULT res;
  6763. SDL_LockMutex(renderer->submitLock);
  6764. // Unmap uniform buffers
  6765. for (Uint32 i = 0; i < MAX_UNIFORM_BUFFERS_PER_STAGE; i += 1) {
  6766. if (d3d12CommandBuffer->vertexUniformBuffers[i] != NULL) {
  6767. ID3D12Resource_Unmap(
  6768. d3d12CommandBuffer->vertexUniformBuffers[i]->buffer->handle,
  6769. 0,
  6770. NULL);
  6771. d3d12CommandBuffer->vertexUniformBuffers[i]->buffer->mapPointer = NULL;
  6772. }
  6773. if (d3d12CommandBuffer->fragmentUniformBuffers[i] != NULL) {
  6774. ID3D12Resource_Unmap(
  6775. d3d12CommandBuffer->fragmentUniformBuffers[i]->buffer->handle,
  6776. 0,
  6777. NULL);
  6778. d3d12CommandBuffer->fragmentUniformBuffers[i]->buffer->mapPointer = NULL;
  6779. }
  6780. // TODO: compute uniforms
  6781. }
  6782. // Transition present textures to present mode
  6783. for (Uint32 i = 0; i < d3d12CommandBuffer->presentDataCount; i += 1) {
  6784. Uint32 swapchainIndex = d3d12CommandBuffer->presentDatas[i].swapchainImageIndex;
  6785. D3D12TextureContainer *container = &d3d12CommandBuffer->presentDatas[i].windowData->textureContainers[swapchainIndex];
  6786. D3D12TextureSubresource *subresource = D3D12_INTERNAL_FetchTextureSubresource(container, 0, 0);
  6787. D3D12_RESOURCE_BARRIER barrierDesc;
  6788. barrierDesc.Type = D3D12_RESOURCE_BARRIER_TYPE_TRANSITION;
  6789. barrierDesc.Flags = D3D12_RESOURCE_BARRIER_FLAG_NONE;
  6790. barrierDesc.Transition.StateBefore = D3D12_RESOURCE_STATE_RENDER_TARGET;
  6791. barrierDesc.Transition.StateAfter = D3D12_RESOURCE_STATE_PRESENT;
  6792. barrierDesc.Transition.pResource = subresource->parent->resource;
  6793. barrierDesc.Transition.Subresource = subresource->index;
  6794. ID3D12GraphicsCommandList_ResourceBarrier(
  6795. d3d12CommandBuffer->graphicsCommandList,
  6796. 1,
  6797. &barrierDesc);
  6798. }
  6799. // Notify the command buffer that we have completed recording
  6800. res = ID3D12GraphicsCommandList_Close(d3d12CommandBuffer->graphicsCommandList);
  6801. CHECK_D3D12_ERROR_AND_RETURN("Failed to close command list!", false);
  6802. res = ID3D12GraphicsCommandList_QueryInterface(
  6803. d3d12CommandBuffer->graphicsCommandList,
  6804. D3D_GUID(D3D_IID_ID3D12CommandList),
  6805. (void **)&commandLists[0]);
  6806. if (FAILED(res)) {
  6807. SDL_UnlockMutex(renderer->submitLock);
  6808. CHECK_D3D12_ERROR_AND_RETURN("Failed to convert command list!", false);
  6809. }
  6810. // Submit the command list to the queue
  6811. ID3D12CommandQueue_ExecuteCommandLists(
  6812. renderer->commandQueue,
  6813. 1,
  6814. commandLists);
  6815. ID3D12CommandList_Release(commandLists[0]);
  6816. // Acquire a fence and set it to the in-flight fence
  6817. d3d12CommandBuffer->inFlightFence = D3D12_INTERNAL_AcquireFence(renderer);
  6818. if (!d3d12CommandBuffer->inFlightFence) {
  6819. SDL_UnlockMutex(renderer->submitLock);
  6820. return false;
  6821. }
  6822. // Mark that a fence should be signaled after command list execution
  6823. res = ID3D12CommandQueue_Signal(
  6824. renderer->commandQueue,
  6825. d3d12CommandBuffer->inFlightFence->handle,
  6826. D3D12_FENCE_SIGNAL_VALUE);
  6827. if (FAILED(res)) {
  6828. SDL_UnlockMutex(renderer->submitLock);
  6829. CHECK_D3D12_ERROR_AND_RETURN("Failed to enqueue fence signal!", false);
  6830. }
  6831. // Mark the command buffer as submitted
  6832. if (renderer->submittedCommandBufferCount + 1 >= renderer->submittedCommandBufferCapacity) {
  6833. renderer->submittedCommandBufferCapacity = renderer->submittedCommandBufferCount + 1;
  6834. renderer->submittedCommandBuffers = (D3D12CommandBuffer **)SDL_realloc(
  6835. renderer->submittedCommandBuffers,
  6836. sizeof(D3D12CommandBuffer *) * renderer->submittedCommandBufferCapacity);
  6837. }
  6838. renderer->submittedCommandBuffers[renderer->submittedCommandBufferCount] = d3d12CommandBuffer;
  6839. renderer->submittedCommandBufferCount += 1;
  6840. bool result = true;
  6841. // Present, if applicable
  6842. for (Uint32 i = 0; i < d3d12CommandBuffer->presentDataCount; i += 1) {
  6843. D3D12PresentData *presentData = &d3d12CommandBuffer->presentDatas[i];
  6844. D3D12WindowData *windowData = presentData->windowData;
  6845. #if defined(SDL_PLATFORM_XBOXONE) || defined(SDL_PLATFORM_XBOXSERIES)
  6846. D3D12XBOX_PRESENT_PLANE_PARAMETERS planeParams;
  6847. SDL_zero(planeParams);
  6848. planeParams.Token = windowData->frameToken;
  6849. planeParams.ResourceCount = 1;
  6850. planeParams.ppResources = &windowData->textureContainers[windowData->frameCounter].activeTexture->resource;
  6851. planeParams.ColorSpace = DXGI_COLOR_SPACE_RGB_FULL_G22_NONE_P709; // FIXME
  6852. D3D12XBOX_PRESENT_PARAMETERS presentParams;
  6853. SDL_zero(presentParams);
  6854. presentParams.Flags = (windowData->present_mode == SDL_GPU_PRESENTMODE_IMMEDIATE) ? D3D12XBOX_PRESENT_FLAG_IMMEDIATE : D3D12XBOX_PRESENT_FLAG_NONE;
  6855. renderer->commandQueue->PresentX(1, &planeParams, &presentParams);
  6856. if (FAILED(res)) {
  6857. result = false;
  6858. }
  6859. #else
  6860. // NOTE: flip discard always supported since DXGI 1.4 is required
  6861. Uint32 syncInterval = 1;
  6862. if (windowData->present_mode == SDL_GPU_PRESENTMODE_IMMEDIATE ||
  6863. windowData->present_mode == SDL_GPU_PRESENTMODE_MAILBOX) {
  6864. syncInterval = 0;
  6865. }
  6866. Uint32 presentFlags = 0;
  6867. if (renderer->supportsTearing &&
  6868. windowData->present_mode == SDL_GPU_PRESENTMODE_IMMEDIATE) {
  6869. presentFlags = DXGI_PRESENT_ALLOW_TEARING;
  6870. }
  6871. res = IDXGISwapChain_Present(
  6872. windowData->swapchain,
  6873. syncInterval,
  6874. presentFlags);
  6875. if (FAILED(res)) {
  6876. result = false;
  6877. }
  6878. ID3D12Resource_Release(windowData->textureContainers[presentData->swapchainImageIndex].activeTexture->resource);
  6879. #endif
  6880. windowData->inFlightFences[windowData->frameCounter] = (SDL_GPUFence *)d3d12CommandBuffer->inFlightFence;
  6881. (void)SDL_AtomicIncRef(&d3d12CommandBuffer->inFlightFence->referenceCount);
  6882. windowData->frameCounter = (windowData->frameCounter + 1) % renderer->allowedFramesInFlight;
  6883. }
  6884. // Check for cleanups
  6885. for (Sint32 i = renderer->submittedCommandBufferCount - 1; i >= 0; i -= 1) {
  6886. Uint64 fenceValue = ID3D12Fence_GetCompletedValue(
  6887. renderer->submittedCommandBuffers[i]->inFlightFence->handle);
  6888. if (fenceValue == D3D12_FENCE_SIGNAL_VALUE) {
  6889. result &= D3D12_INTERNAL_CleanCommandBuffer(
  6890. renderer,
  6891. renderer->submittedCommandBuffers[i],
  6892. false);
  6893. }
  6894. }
  6895. D3D12_INTERNAL_PerformPendingDestroys(renderer);
  6896. SDL_UnlockMutex(renderer->submitLock);
  6897. return result;
  6898. }
  6899. static SDL_GPUFence *D3D12_SubmitAndAcquireFence(
  6900. SDL_GPUCommandBuffer *commandBuffer)
  6901. {
  6902. D3D12CommandBuffer *d3d12CommandBuffer = (D3D12CommandBuffer *)commandBuffer;
  6903. d3d12CommandBuffer->autoReleaseFence = false;
  6904. if (!D3D12_Submit(commandBuffer)) {
  6905. return NULL;
  6906. }
  6907. return (SDL_GPUFence *)d3d12CommandBuffer->inFlightFence;
  6908. }
  6909. static bool D3D12_Cancel(
  6910. SDL_GPUCommandBuffer *commandBuffer)
  6911. {
  6912. D3D12CommandBuffer *d3d12CommandBuffer = (D3D12CommandBuffer *)commandBuffer;
  6913. D3D12Renderer *renderer = d3d12CommandBuffer->renderer;
  6914. bool result;
  6915. HRESULT res;
  6916. // Notify the command buffer that we have completed recording
  6917. res = ID3D12GraphicsCommandList_Close(d3d12CommandBuffer->graphicsCommandList);
  6918. CHECK_D3D12_ERROR_AND_RETURN("Failed to close command list!", false);
  6919. d3d12CommandBuffer->autoReleaseFence = false;
  6920. SDL_LockMutex(renderer->submitLock);
  6921. result = D3D12_INTERNAL_CleanCommandBuffer(renderer, d3d12CommandBuffer, true);
  6922. SDL_UnlockMutex(renderer->submitLock);
  6923. return result;
  6924. }
  6925. static bool D3D12_Wait(
  6926. SDL_GPURenderer *driverData)
  6927. {
  6928. D3D12Renderer *renderer = (D3D12Renderer *)driverData;
  6929. D3D12Fence *fence = D3D12_INTERNAL_AcquireFence(renderer);
  6930. if (!fence) {
  6931. return false;
  6932. }
  6933. HRESULT res;
  6934. SDL_LockMutex(renderer->submitLock);
  6935. if (renderer->commandQueue) {
  6936. // Insert a signal into the end of the command queue...
  6937. ID3D12CommandQueue_Signal(
  6938. renderer->commandQueue,
  6939. fence->handle,
  6940. D3D12_FENCE_SIGNAL_VALUE);
  6941. // ...and then block on it.
  6942. if (ID3D12Fence_GetCompletedValue(fence->handle) != D3D12_FENCE_SIGNAL_VALUE) {
  6943. res = ID3D12Fence_SetEventOnCompletion(
  6944. fence->handle,
  6945. D3D12_FENCE_SIGNAL_VALUE,
  6946. fence->event);
  6947. CHECK_D3D12_ERROR_AND_RETURN("Setting fence event failed", false);
  6948. DWORD waitResult = WaitForSingleObject(fence->event, INFINITE);
  6949. if (waitResult == WAIT_FAILED) {
  6950. SDL_UnlockMutex(renderer->submitLock);
  6951. SET_STRING_ERROR_AND_RETURN("Wait failed", false); // TODO: is there a better way to report this?
  6952. }
  6953. }
  6954. }
  6955. D3D12_ReleaseFence(
  6956. (SDL_GPURenderer *)renderer,
  6957. (SDL_GPUFence *)fence);
  6958. bool result = true;
  6959. // Clean up
  6960. for (Sint32 i = renderer->submittedCommandBufferCount - 1; i >= 0; i -= 1) {
  6961. result &= D3D12_INTERNAL_CleanCommandBuffer(renderer, renderer->submittedCommandBuffers[i], false);
  6962. }
  6963. D3D12_INTERNAL_PerformPendingDestroys(renderer);
  6964. SDL_UnlockMutex(renderer->submitLock);
  6965. return result;
  6966. }
  6967. static bool D3D12_WaitForFences(
  6968. SDL_GPURenderer *driverData,
  6969. bool waitAll,
  6970. SDL_GPUFence *const *fences,
  6971. Uint32 numFences)
  6972. {
  6973. D3D12Renderer *renderer = (D3D12Renderer *)driverData;
  6974. D3D12Fence *fence;
  6975. HANDLE *events = SDL_stack_alloc(HANDLE, numFences);
  6976. HRESULT res;
  6977. SDL_LockMutex(renderer->submitLock);
  6978. for (Uint32 i = 0; i < numFences; i += 1) {
  6979. fence = (D3D12Fence *)fences[i];
  6980. res = ID3D12Fence_SetEventOnCompletion(
  6981. fence->handle,
  6982. D3D12_FENCE_SIGNAL_VALUE,
  6983. fence->event);
  6984. CHECK_D3D12_ERROR_AND_RETURN("Setting fence event failed", false);
  6985. events[i] = fence->event;
  6986. }
  6987. DWORD waitResult = WaitForMultipleObjects(
  6988. numFences,
  6989. events,
  6990. waitAll,
  6991. INFINITE);
  6992. if (waitResult == WAIT_FAILED) {
  6993. SDL_UnlockMutex(renderer->submitLock);
  6994. SET_STRING_ERROR_AND_RETURN("Wait failed", false); // TODO: is there a better way to report this?
  6995. }
  6996. bool result = true;
  6997. // Check for cleanups
  6998. for (Sint32 i = renderer->submittedCommandBufferCount - 1; i >= 0; i -= 1) {
  6999. Uint64 fenceValue = ID3D12Fence_GetCompletedValue(
  7000. renderer->submittedCommandBuffers[i]->inFlightFence->handle);
  7001. if (fenceValue == D3D12_FENCE_SIGNAL_VALUE) {
  7002. result &= D3D12_INTERNAL_CleanCommandBuffer(
  7003. renderer,
  7004. renderer->submittedCommandBuffers[i],
  7005. false);
  7006. }
  7007. }
  7008. D3D12_INTERNAL_PerformPendingDestroys(renderer);
  7009. SDL_stack_free(events);
  7010. SDL_UnlockMutex(renderer->submitLock);
  7011. return result;
  7012. }
  7013. // Feature Queries
  7014. static bool D3D12_SupportsTextureFormat(
  7015. SDL_GPURenderer *driverData,
  7016. SDL_GPUTextureFormat format,
  7017. SDL_GPUTextureType type,
  7018. SDL_GPUTextureUsageFlags usage)
  7019. {
  7020. D3D12Renderer *renderer = (D3D12Renderer *)driverData;
  7021. DXGI_FORMAT dxgiFormat = SDLToD3D12_TextureFormat[format];
  7022. D3D12_FEATURE_DATA_FORMAT_SUPPORT formatSupport = { dxgiFormat, D3D12_FORMAT_SUPPORT1_NONE, D3D12_FORMAT_SUPPORT2_NONE };
  7023. HRESULT res;
  7024. res = ID3D12Device_CheckFeatureSupport(
  7025. renderer->device,
  7026. D3D12_FEATURE_FORMAT_SUPPORT,
  7027. &formatSupport,
  7028. sizeof(formatSupport));
  7029. if (FAILED(res)) {
  7030. // Format is apparently unknown
  7031. return false;
  7032. }
  7033. // Is the texture type supported?
  7034. if (type == SDL_GPU_TEXTURETYPE_2D && !(formatSupport.Support1 & D3D12_FORMAT_SUPPORT1_TEXTURE2D)) {
  7035. return false;
  7036. }
  7037. if (type == SDL_GPU_TEXTURETYPE_2D_ARRAY && !(formatSupport.Support1 & D3D12_FORMAT_SUPPORT1_TEXTURE2D)) {
  7038. return false;
  7039. }
  7040. if (type == SDL_GPU_TEXTURETYPE_3D && !(formatSupport.Support1 & D3D12_FORMAT_SUPPORT1_TEXTURE3D)) {
  7041. return false;
  7042. }
  7043. if (type == SDL_GPU_TEXTURETYPE_CUBE && !(formatSupport.Support1 & D3D12_FORMAT_SUPPORT1_TEXTURECUBE)) {
  7044. return false;
  7045. }
  7046. if (type == SDL_GPU_TEXTURETYPE_CUBE_ARRAY && !(formatSupport.Support1 & D3D12_FORMAT_SUPPORT1_TEXTURECUBE)) {
  7047. return false;
  7048. }
  7049. // Are the usage flags supported?
  7050. if ((usage & SDL_GPU_TEXTUREUSAGE_SAMPLER) && !(formatSupport.Support1 & D3D12_FORMAT_SUPPORT1_SHADER_SAMPLE)) {
  7051. return false;
  7052. }
  7053. if ((usage & (SDL_GPU_TEXTUREUSAGE_GRAPHICS_STORAGE_READ | SDL_GPU_TEXTUREUSAGE_COMPUTE_STORAGE_READ)) && !(formatSupport.Support1 & D3D12_FORMAT_SUPPORT1_SHADER_LOAD)) {
  7054. return false;
  7055. }
  7056. if ((usage & SDL_GPU_TEXTUREUSAGE_COMPUTE_STORAGE_WRITE) && !(formatSupport.Support2 & D3D12_FORMAT_SUPPORT2_UAV_TYPED_STORE)) {
  7057. return false;
  7058. }
  7059. if ((usage & SDL_GPU_TEXTUREUSAGE_COMPUTE_STORAGE_SIMULTANEOUS_READ_WRITE) && !(formatSupport.Support2 & D3D12_FORMAT_SUPPORT2_UAV_TYPED_LOAD)) {
  7060. return false;
  7061. }
  7062. if ((usage & SDL_GPU_TEXTUREUSAGE_COLOR_TARGET) && !(formatSupport.Support1 & D3D12_FORMAT_SUPPORT1_RENDER_TARGET)) {
  7063. return false;
  7064. }
  7065. // Special case check for depth, because D3D12 is great.
  7066. formatSupport.Format = SDLToD3D12_DepthFormat[format];
  7067. formatSupport.Support1 = D3D12_FORMAT_SUPPORT1_NONE;
  7068. formatSupport.Support2 = D3D12_FORMAT_SUPPORT2_NONE;
  7069. res = ID3D12Device_CheckFeatureSupport(
  7070. renderer->device,
  7071. D3D12_FEATURE_FORMAT_SUPPORT,
  7072. &formatSupport,
  7073. sizeof(formatSupport));
  7074. if (FAILED(res)) {
  7075. // Format is apparently unknown
  7076. return false;
  7077. }
  7078. if ((usage & SDL_GPU_TEXTUREUSAGE_DEPTH_STENCIL_TARGET) && !(formatSupport.Support1 & D3D12_FORMAT_SUPPORT1_DEPTH_STENCIL)) {
  7079. return false;
  7080. }
  7081. return true;
  7082. }
  7083. static bool D3D12_SupportsSampleCount(
  7084. SDL_GPURenderer *driverData,
  7085. SDL_GPUTextureFormat format,
  7086. SDL_GPUSampleCount sampleCount)
  7087. {
  7088. D3D12Renderer *renderer = (D3D12Renderer *)driverData;
  7089. D3D12_FEATURE_DATA_MULTISAMPLE_QUALITY_LEVELS featureData;
  7090. HRESULT res;
  7091. #if defined(SDL_PLATFORM_XBOXONE) || defined(SDL_PLATFORM_XBOXSERIES)
  7092. featureData.Flags = (D3D12_MULTISAMPLE_QUALITY_LEVELS_FLAG)0;
  7093. #else
  7094. featureData.Flags = (D3D12_MULTISAMPLE_QUALITY_LEVEL_FLAGS)0;
  7095. #endif
  7096. featureData.Format = SDLToD3D12_TextureFormat[format];
  7097. featureData.SampleCount = SDLToD3D12_SampleCount[sampleCount];
  7098. res = ID3D12Device_CheckFeatureSupport(
  7099. renderer->device,
  7100. D3D12_FEATURE_MULTISAMPLE_QUALITY_LEVELS,
  7101. &featureData,
  7102. sizeof(featureData));
  7103. return SUCCEEDED(res) && featureData.NumQualityLevels > 0;
  7104. }
  7105. static void D3D12_INTERNAL_InitBlitResources(
  7106. D3D12Renderer *renderer)
  7107. {
  7108. SDL_GPUShaderCreateInfo shaderCreateInfo;
  7109. SDL_GPUSamplerCreateInfo samplerCreateInfo;
  7110. renderer->blitPipelineCapacity = 2;
  7111. renderer->blitPipelineCount = 0;
  7112. renderer->blitPipelines = (BlitPipelineCacheEntry *)SDL_malloc(
  7113. renderer->blitPipelineCapacity * sizeof(BlitPipelineCacheEntry));
  7114. // Fullscreen vertex shader
  7115. SDL_zero(shaderCreateInfo);
  7116. shaderCreateInfo.code = (Uint8 *)D3D12_FullscreenVert;
  7117. shaderCreateInfo.code_size = sizeof(D3D12_FullscreenVert);
  7118. shaderCreateInfo.stage = SDL_GPU_SHADERSTAGE_VERTEX;
  7119. shaderCreateInfo.format = SDL_GPU_SHADERFORMAT_DXIL;
  7120. renderer->blitVertexShader = D3D12_CreateShader(
  7121. (SDL_GPURenderer *)renderer,
  7122. &shaderCreateInfo);
  7123. if (renderer->blitVertexShader == NULL) {
  7124. SDL_LogError(SDL_LOG_CATEGORY_GPU, "Failed to compile vertex shader for blit!");
  7125. }
  7126. // BlitFrom2D pixel shader
  7127. shaderCreateInfo.code = (Uint8 *)D3D12_BlitFrom2D;
  7128. shaderCreateInfo.code_size = sizeof(D3D12_BlitFrom2D);
  7129. shaderCreateInfo.stage = SDL_GPU_SHADERSTAGE_FRAGMENT;
  7130. shaderCreateInfo.num_samplers = 1;
  7131. shaderCreateInfo.num_uniform_buffers = 1;
  7132. renderer->blitFrom2DShader = D3D12_CreateShader(
  7133. (SDL_GPURenderer *)renderer,
  7134. &shaderCreateInfo);
  7135. if (renderer->blitFrom2DShader == NULL) {
  7136. SDL_LogError(SDL_LOG_CATEGORY_GPU, "Failed to compile BlitFrom2D pixel shader!");
  7137. }
  7138. // BlitFrom2DArray pixel shader
  7139. shaderCreateInfo.code = (Uint8 *)D3D12_BlitFrom2DArray;
  7140. shaderCreateInfo.code_size = sizeof(D3D12_BlitFrom2DArray);
  7141. renderer->blitFrom2DArrayShader = D3D12_CreateShader(
  7142. (SDL_GPURenderer *)renderer,
  7143. &shaderCreateInfo);
  7144. if (renderer->blitFrom2DArrayShader == NULL) {
  7145. SDL_LogError(SDL_LOG_CATEGORY_GPU, "Failed to compile BlitFrom2DArray pixel shader!");
  7146. }
  7147. // BlitFrom3D pixel shader
  7148. shaderCreateInfo.code = (Uint8 *)D3D12_BlitFrom3D;
  7149. shaderCreateInfo.code_size = sizeof(D3D12_BlitFrom3D);
  7150. renderer->blitFrom3DShader = D3D12_CreateShader(
  7151. (SDL_GPURenderer *)renderer,
  7152. &shaderCreateInfo);
  7153. if (renderer->blitFrom3DShader == NULL) {
  7154. SDL_LogError(SDL_LOG_CATEGORY_GPU, "Failed to compile BlitFrom3D pixel shader!");
  7155. }
  7156. // BlitFromCube pixel shader
  7157. shaderCreateInfo.code = (Uint8 *)D3D12_BlitFromCube;
  7158. shaderCreateInfo.code_size = sizeof(D3D12_BlitFromCube);
  7159. renderer->blitFromCubeShader = D3D12_CreateShader(
  7160. (SDL_GPURenderer *)renderer,
  7161. &shaderCreateInfo);
  7162. if (renderer->blitFromCubeShader == NULL) {
  7163. SDL_LogError(SDL_LOG_CATEGORY_GPU, "Failed to compile BlitFromCube pixel shader!");
  7164. }
  7165. // BlitFromCubeArray pixel shader
  7166. shaderCreateInfo.code = (Uint8 *)D3D12_BlitFromCubeArray;
  7167. shaderCreateInfo.code_size = sizeof(D3D12_BlitFromCubeArray);
  7168. renderer->blitFromCubeArrayShader = D3D12_CreateShader(
  7169. (SDL_GPURenderer *)renderer,
  7170. &shaderCreateInfo);
  7171. if (renderer->blitFromCubeArrayShader == NULL) {
  7172. SDL_LogError(SDL_LOG_CATEGORY_GPU, "Failed to compile BlitFromCubeArray pixel shader!");
  7173. }
  7174. // Create samplers
  7175. samplerCreateInfo.address_mode_u = SDL_GPU_SAMPLERADDRESSMODE_CLAMP_TO_EDGE;
  7176. samplerCreateInfo.address_mode_v = SDL_GPU_SAMPLERADDRESSMODE_CLAMP_TO_EDGE;
  7177. samplerCreateInfo.address_mode_w = SDL_GPU_SAMPLERADDRESSMODE_CLAMP_TO_EDGE;
  7178. samplerCreateInfo.enable_anisotropy = 0;
  7179. samplerCreateInfo.enable_compare = 0;
  7180. samplerCreateInfo.mag_filter = SDL_GPU_FILTER_NEAREST;
  7181. samplerCreateInfo.min_filter = SDL_GPU_FILTER_NEAREST;
  7182. samplerCreateInfo.mipmap_mode = SDL_GPU_SAMPLERMIPMAPMODE_NEAREST;
  7183. samplerCreateInfo.mip_lod_bias = 0.0f;
  7184. samplerCreateInfo.min_lod = 0;
  7185. samplerCreateInfo.max_lod = 1000;
  7186. samplerCreateInfo.max_anisotropy = 1.0f;
  7187. samplerCreateInfo.compare_op = SDL_GPU_COMPAREOP_NEVER;
  7188. renderer->blitNearestSampler = D3D12_CreateSampler(
  7189. (SDL_GPURenderer *)renderer,
  7190. &samplerCreateInfo);
  7191. if (renderer->blitNearestSampler == NULL) {
  7192. SDL_LogError(SDL_LOG_CATEGORY_GPU, "Failed to create blit nearest sampler!");
  7193. }
  7194. samplerCreateInfo.mag_filter = SDL_GPU_FILTER_LINEAR;
  7195. samplerCreateInfo.min_filter = SDL_GPU_FILTER_LINEAR;
  7196. samplerCreateInfo.mipmap_mode = SDL_GPU_SAMPLERMIPMAPMODE_LINEAR;
  7197. renderer->blitLinearSampler = D3D12_CreateSampler(
  7198. (SDL_GPURenderer *)renderer,
  7199. &samplerCreateInfo);
  7200. if (renderer->blitLinearSampler == NULL) {
  7201. SDL_LogError(SDL_LOG_CATEGORY_GPU, "Failed to create blit linear sampler!");
  7202. }
  7203. }
  7204. static bool D3D12_PrepareDriver(SDL_VideoDevice *_this, SDL_PropertiesID props)
  7205. {
  7206. #if defined(SDL_PLATFORM_XBOXONE) || defined(SDL_PLATFORM_XBOXSERIES)
  7207. return true;
  7208. #else
  7209. SDL_SharedObject *d3d12Dll;
  7210. SDL_SharedObject *dxgiDll;
  7211. PFN_D3D12_CREATE_DEVICE pD3D12CreateDevice;
  7212. pfnCreateDXGIFactory1 pCreateDXGIFactory1;
  7213. HRESULT res;
  7214. ID3D12Device *device;
  7215. IDXGIFactory1 *factory;
  7216. IDXGIFactory4 *factory4;
  7217. IDXGIFactory6 *factory6;
  7218. IDXGIAdapter1 *adapter;
  7219. bool supports_64UAVs = false;
  7220. bool needs_64UAVs = !SDL_GetBooleanProperty(props, SDL_PROP_GPU_DEVICE_CREATE_D3D12_ALLOW_FEWER_RESOURCE_SLOTS_BOOLEAN, false);
  7221. // Early check to see if the app has _any_ D3D12 formats, if not we don't
  7222. // have to fuss with loading D3D in the first place.
  7223. bool has_dxbc = SDL_GetBooleanProperty(props, SDL_PROP_GPU_DEVICE_CREATE_SHADERS_DXBC_BOOLEAN, false);
  7224. bool has_dxil = SDL_GetBooleanProperty(props, SDL_PROP_GPU_DEVICE_CREATE_SHADERS_DXIL_BOOLEAN, false);
  7225. bool supports_dxil = false;
  7226. // TODO SM7: bool has_spirv = SDL_GetBooleanProperty(props, SDL_PROP_GPU_DEVICE_CREATE_SHADERS_SPIRV_BOOLEAN, false);
  7227. // TODO SM7: bool supports_spirv = false;
  7228. if (!has_dxbc && !has_dxil) {
  7229. return false;
  7230. }
  7231. // Can we load D3D12?
  7232. d3d12Dll = SDL_LoadObject(D3D12_DLL);
  7233. if (d3d12Dll == NULL) {
  7234. SDL_LogWarn(SDL_LOG_CATEGORY_GPU, "D3D12: Could not find " D3D12_DLL);
  7235. return false;
  7236. }
  7237. pD3D12CreateDevice = (PFN_D3D12_CREATE_DEVICE)SDL_LoadFunction(
  7238. d3d12Dll,
  7239. D3D12_CREATE_DEVICE_FUNC);
  7240. if (pD3D12CreateDevice == NULL) {
  7241. SDL_LogWarn(SDL_LOG_CATEGORY_GPU, "D3D12: Could not find function " D3D12_CREATE_DEVICE_FUNC " in " D3D12_DLL);
  7242. SDL_UnloadObject(d3d12Dll);
  7243. return false;
  7244. }
  7245. // Can we load DXGI?
  7246. dxgiDll = SDL_LoadObject(DXGI_DLL);
  7247. if (dxgiDll == NULL) {
  7248. SDL_LogWarn(SDL_LOG_CATEGORY_GPU, "D3D12: Could not find " DXGI_DLL);
  7249. return false;
  7250. }
  7251. pCreateDXGIFactory1 = (pfnCreateDXGIFactory1)SDL_LoadFunction(
  7252. dxgiDll,
  7253. CREATE_DXGI_FACTORY1_FUNC);
  7254. if (pCreateDXGIFactory1 == NULL) {
  7255. SDL_LogWarn(SDL_LOG_CATEGORY_GPU, "D3D12: Could not find function " CREATE_DXGI_FACTORY1_FUNC " in " DXGI_DLL);
  7256. SDL_UnloadObject(dxgiDll);
  7257. return false;
  7258. }
  7259. // Can we create a device?
  7260. // Create the DXGI factory
  7261. res = pCreateDXGIFactory1(
  7262. &D3D_IID_IDXGIFactory1,
  7263. (void **)&factory);
  7264. if (FAILED(res)) {
  7265. SDL_LogWarn(SDL_LOG_CATEGORY_GPU, "D3D12: Could not create DXGIFactory");
  7266. SDL_UnloadObject(d3d12Dll);
  7267. SDL_UnloadObject(dxgiDll);
  7268. return false;
  7269. }
  7270. // Check for DXGI 1.4 support
  7271. res = IDXGIFactory1_QueryInterface(
  7272. factory,
  7273. D3D_GUID(D3D_IID_IDXGIFactory4),
  7274. (void **)&factory4);
  7275. if (FAILED(res)) {
  7276. IDXGIFactory1_Release(factory);
  7277. SDL_LogWarn(SDL_LOG_CATEGORY_GPU, "D3D12: Failed to find DXGI1.4 support, required for DX12");
  7278. SDL_UnloadObject(d3d12Dll);
  7279. SDL_UnloadObject(dxgiDll);
  7280. return false;
  7281. }
  7282. IDXGIFactory4_Release(factory4);
  7283. res = IDXGIFactory1_QueryInterface(
  7284. factory,
  7285. D3D_GUID(D3D_IID_IDXGIFactory6),
  7286. (void **)&factory6);
  7287. if (SUCCEEDED(res)) {
  7288. res = IDXGIFactory6_EnumAdapterByGpuPreference(
  7289. factory6,
  7290. 0,
  7291. DXGI_GPU_PREFERENCE_HIGH_PERFORMANCE,
  7292. D3D_GUID(D3D_IID_IDXGIAdapter1),
  7293. (void **)&adapter);
  7294. IDXGIFactory6_Release(factory6);
  7295. } else {
  7296. res = IDXGIFactory1_EnumAdapters1(
  7297. factory,
  7298. 0,
  7299. &adapter);
  7300. }
  7301. if (FAILED(res)) {
  7302. SDL_LogWarn(SDL_LOG_CATEGORY_GPU, "D3D12: Failed to find adapter for D3D12Device");
  7303. IDXGIFactory1_Release(factory);
  7304. SDL_UnloadObject(d3d12Dll);
  7305. SDL_UnloadObject(dxgiDll);
  7306. return false;
  7307. }
  7308. SDL_COMPILE_TIME_ASSERT(featurelevel, D3D_FEATURE_LEVEL_CHOICE < D3D_FEATURE_LEVEL_11_1);
  7309. /* If Windows 11 is running and the app has neither DXIL nor TIER2
  7310. * requirements, we can skip doing any device checks entirely
  7311. */
  7312. if (!needs_64UAVs && !has_dxil && WIN_IsWindows11OrGreater()) {
  7313. IDXGIAdapter1_Release(adapter);
  7314. IDXGIFactory1_Release(factory);
  7315. SDL_UnloadObject(d3d12Dll);
  7316. SDL_UnloadObject(dxgiDll);
  7317. return true;
  7318. }
  7319. res = pD3D12CreateDevice(
  7320. (IUnknown *)adapter,
  7321. D3D_FEATURE_LEVEL_CHOICE,
  7322. D3D_GUID(D3D_IID_ID3D12Device),
  7323. (void **)&device);
  7324. if (SUCCEEDED(res)) {
  7325. // Only check for Tier 2 resource binding if the app needs it
  7326. if (needs_64UAVs) {
  7327. D3D12_FEATURE_DATA_D3D12_OPTIONS featureOptions;
  7328. SDL_zero(featureOptions);
  7329. res = ID3D12Device_CheckFeatureSupport(
  7330. device,
  7331. D3D12_FEATURE_D3D12_OPTIONS,
  7332. &featureOptions,
  7333. sizeof(featureOptions));
  7334. if (SUCCEEDED(res) && featureOptions.ResourceBindingTier >= D3D12_RESOURCE_BINDING_TIER_2) {
  7335. supports_64UAVs = true;
  7336. }
  7337. }
  7338. // Only check for SM6 support if DXIL is provided
  7339. if (has_dxil) {
  7340. D3D12_FEATURE_DATA_SHADER_MODEL shaderModel;
  7341. shaderModel.HighestShaderModel = D3D_SHADER_MODEL_6_0;
  7342. res = ID3D12Device_CheckFeatureSupport(
  7343. device,
  7344. D3D12_FEATURE_SHADER_MODEL,
  7345. &shaderModel,
  7346. sizeof(shaderModel));
  7347. if (SUCCEEDED(res) && shaderModel.HighestShaderModel >= D3D_SHADER_MODEL_6_0) {
  7348. supports_dxil = true;
  7349. }
  7350. }
  7351. ID3D12Device_Release(device);
  7352. }
  7353. IDXGIAdapter1_Release(adapter);
  7354. IDXGIFactory1_Release(factory);
  7355. SDL_UnloadObject(d3d12Dll);
  7356. SDL_UnloadObject(dxgiDll);
  7357. if (!supports_64UAVs && needs_64UAVs) {
  7358. SDL_LogWarn(SDL_LOG_CATEGORY_GPU, "D3D12: Tier 2 Resource Binding is not supported");
  7359. return false;
  7360. }
  7361. if (!supports_dxil && !has_dxbc) {
  7362. SDL_LogWarn(SDL_LOG_CATEGORY_GPU, "D3D12: DXIL is not supported and DXBC is not being provided");
  7363. return false;
  7364. }
  7365. if (FAILED(res)) {
  7366. SDL_LogWarn(SDL_LOG_CATEGORY_GPU, "D3D12: Could not create D3D12Device with feature level " D3D_FEATURE_LEVEL_CHOICE_STR);
  7367. return false;
  7368. }
  7369. return true;
  7370. #endif
  7371. }
  7372. #if !(defined(SDL_PLATFORM_XBOXONE) || defined(SDL_PLATFORM_XBOXSERIES)) && defined(HAVE_IDXGIINFOQUEUE)
  7373. static bool D3D12_INTERNAL_TryInitializeDXGIDebug(D3D12Renderer *renderer)
  7374. {
  7375. pfnDXGIGetDebugInterface pDXGIGetDebugInterface;
  7376. HRESULT res;
  7377. renderer->dxgidebug_dll = SDL_LoadObject(DXGIDEBUG_DLL);
  7378. if (renderer->dxgidebug_dll == NULL) {
  7379. return false;
  7380. }
  7381. pDXGIGetDebugInterface = (pfnDXGIGetDebugInterface)SDL_LoadFunction(
  7382. renderer->dxgidebug_dll,
  7383. DXGI_GET_DEBUG_INTERFACE_FUNC);
  7384. if (pDXGIGetDebugInterface == NULL) {
  7385. return false;
  7386. }
  7387. res = pDXGIGetDebugInterface(&D3D_IID_IDXGIDebug, (void **)&renderer->dxgiDebug);
  7388. if (FAILED(res)) {
  7389. return false;
  7390. }
  7391. res = pDXGIGetDebugInterface(&D3D_IID_IDXGIInfoQueue, (void **)&renderer->dxgiInfoQueue);
  7392. if (FAILED(res)) {
  7393. return false;
  7394. }
  7395. return true;
  7396. }
  7397. #endif
  7398. static bool D3D12_INTERNAL_TryInitializeD3D12Debug(D3D12Renderer *renderer
  7399. #if !(defined(SDL_PLATFORM_XBOXONE) || defined(SDL_PLATFORM_XBOXSERIES))
  7400. , ID3D12DeviceFactory * factory
  7401. #endif
  7402. ) {
  7403. PFN_D3D12_GET_DEBUG_INTERFACE pD3D12GetDebugInterface;
  7404. HRESULT res;
  7405. #if !(defined(SDL_PLATFORM_XBOXONE) || defined(SDL_PLATFORM_XBOXSERIES))
  7406. if (factory) {
  7407. res = ID3D12DeviceFactory_GetConfigurationInterface(factory, &D3D_CLSID_ID3D12Debug, &D3D_IID_ID3D12Debug, (void **)&renderer->d3d12Debug);
  7408. if (FAILED(res)) {
  7409. return false;
  7410. }
  7411. ID3D12Debug_EnableDebugLayer(renderer->d3d12Debug);
  7412. return true;
  7413. }
  7414. #endif
  7415. pD3D12GetDebugInterface = (PFN_D3D12_GET_DEBUG_INTERFACE)SDL_LoadFunction(
  7416. renderer->d3d12_dll,
  7417. D3D12_GET_DEBUG_INTERFACE_FUNC);
  7418. if (pD3D12GetDebugInterface == NULL) {
  7419. return false;
  7420. }
  7421. res = pD3D12GetDebugInterface(D3D_GUID(D3D_IID_ID3D12Debug), (void **)&renderer->d3d12Debug);
  7422. if (FAILED(res)) {
  7423. return false;
  7424. }
  7425. ID3D12Debug_EnableDebugLayer(renderer->d3d12Debug);
  7426. return true;
  7427. }
  7428. #if !(defined(SDL_PLATFORM_XBOXONE) || defined(SDL_PLATFORM_XBOXSERIES))
  7429. static void D3D12_INTERNAL_TryInitializeD3D12DebugInfoQueue(D3D12Renderer *renderer)
  7430. {
  7431. ID3D12InfoQueue *infoQueue = NULL;
  7432. D3D12_MESSAGE_SEVERITY severities[] = { D3D12_MESSAGE_SEVERITY_INFO };
  7433. D3D12_INFO_QUEUE_FILTER filter;
  7434. HRESULT res;
  7435. res = ID3D12Device_QueryInterface(
  7436. renderer->device,
  7437. D3D_GUID(D3D_IID_ID3D12InfoQueue),
  7438. (void **)&infoQueue);
  7439. if (FAILED(res)) {
  7440. return;
  7441. }
  7442. SDL_zero(filter);
  7443. filter.DenyList.NumSeverities = 1;
  7444. filter.DenyList.pSeverityList = severities;
  7445. ID3D12InfoQueue_PushStorageFilter(
  7446. infoQueue,
  7447. &filter);
  7448. ID3D12InfoQueue_SetBreakOnSeverity(
  7449. infoQueue,
  7450. D3D12_MESSAGE_SEVERITY_CORRUPTION,
  7451. true);
  7452. ID3D12InfoQueue_Release(infoQueue);
  7453. }
  7454. static void WINAPI D3D12_INTERNAL_OnD3D12DebugInfoMsg(
  7455. D3D12_MESSAGE_CATEGORY category,
  7456. D3D12_MESSAGE_SEVERITY severity,
  7457. D3D12_MESSAGE_ID id,
  7458. LPCSTR description,
  7459. void *context)
  7460. {
  7461. char *catStr;
  7462. char *sevStr;
  7463. switch (category) {
  7464. case D3D12_MESSAGE_CATEGORY_APPLICATION_DEFINED:
  7465. catStr = "APPLICATION_DEFINED";
  7466. break;
  7467. case D3D12_MESSAGE_CATEGORY_MISCELLANEOUS:
  7468. catStr = "MISCELLANEOUS";
  7469. break;
  7470. case D3D12_MESSAGE_CATEGORY_INITIALIZATION:
  7471. catStr = "INITIALIZATION";
  7472. break;
  7473. case D3D12_MESSAGE_CATEGORY_CLEANUP:
  7474. catStr = "CLEANUP";
  7475. break;
  7476. case D3D12_MESSAGE_CATEGORY_COMPILATION:
  7477. catStr = "COMPILATION";
  7478. break;
  7479. case D3D12_MESSAGE_CATEGORY_STATE_CREATION:
  7480. catStr = "STATE_CREATION";
  7481. break;
  7482. case D3D12_MESSAGE_CATEGORY_STATE_SETTING:
  7483. catStr = "STATE_SETTING";
  7484. break;
  7485. case D3D12_MESSAGE_CATEGORY_STATE_GETTING:
  7486. catStr = "STATE_GETTING";
  7487. break;
  7488. case D3D12_MESSAGE_CATEGORY_RESOURCE_MANIPULATION:
  7489. catStr = "RESOURCE_MANIPULATION";
  7490. break;
  7491. case D3D12_MESSAGE_CATEGORY_EXECUTION:
  7492. catStr = "EXECUTION";
  7493. break;
  7494. case D3D12_MESSAGE_CATEGORY_SHADER:
  7495. catStr = "SHADER";
  7496. break;
  7497. default:
  7498. catStr = "UNKNOWN";
  7499. break;
  7500. }
  7501. switch (severity) {
  7502. case D3D12_MESSAGE_SEVERITY_CORRUPTION:
  7503. sevStr = "CORRUPTION";
  7504. break;
  7505. case D3D12_MESSAGE_SEVERITY_ERROR:
  7506. sevStr = "ERROR";
  7507. break;
  7508. case D3D12_MESSAGE_SEVERITY_WARNING:
  7509. sevStr = "WARNING";
  7510. break;
  7511. case D3D12_MESSAGE_SEVERITY_INFO:
  7512. sevStr = "INFO";
  7513. break;
  7514. case D3D12_MESSAGE_SEVERITY_MESSAGE:
  7515. sevStr = "MESSAGE";
  7516. break;
  7517. default:
  7518. sevStr = "UNKNOWN";
  7519. break;
  7520. }
  7521. if (severity <= D3D12_MESSAGE_SEVERITY_ERROR) {
  7522. SDL_LogError(
  7523. SDL_LOG_CATEGORY_GPU,
  7524. "D3D12 ERROR: %s [%s %s #%d]",
  7525. description,
  7526. catStr,
  7527. sevStr,
  7528. id);
  7529. } else {
  7530. SDL_LogWarn(
  7531. SDL_LOG_CATEGORY_GPU,
  7532. "D3D12 WARNING: %s [%s %s #%d]",
  7533. description,
  7534. catStr,
  7535. sevStr,
  7536. id);
  7537. }
  7538. }
  7539. static void D3D12_INTERNAL_TryInitializeD3D12DebugInfoLogger(D3D12Renderer *renderer)
  7540. {
  7541. ID3D12InfoQueue1 *infoQueue = NULL;
  7542. HRESULT res;
  7543. res = ID3D12Device_QueryInterface(
  7544. renderer->device,
  7545. D3D_GUID(D3D_IID_ID3D12InfoQueue1),
  7546. (void **)&infoQueue);
  7547. if (FAILED(res)) {
  7548. return;
  7549. }
  7550. ID3D12InfoQueue1_RegisterMessageCallback(
  7551. infoQueue,
  7552. D3D12_INTERNAL_OnD3D12DebugInfoMsg,
  7553. D3D12_MESSAGE_CALLBACK_FLAG_NONE,
  7554. NULL,
  7555. NULL);
  7556. ID3D12InfoQueue1_Release(infoQueue);
  7557. }
  7558. #endif
  7559. static SDL_GPUDevice *D3D12_CreateDevice(bool debugMode, bool preferLowPower, SDL_PropertiesID props)
  7560. {
  7561. SDL_GPUDevice *result;
  7562. D3D12Renderer *renderer;
  7563. HRESULT res;
  7564. #if (defined(SDL_PLATFORM_XBOXONE) || defined(SDL_PLATFORM_XBOXSERIES))
  7565. PFN_D3D12_XBOX_CREATE_DEVICE D3D12XboxCreateDeviceFunc;
  7566. D3D12XBOX_CREATE_DEVICE_PARAMETERS createDeviceParams;
  7567. #else
  7568. pfnCreateDXGIFactory1 pCreateDXGIFactory1;
  7569. IDXGIFactory1 *factory1;
  7570. IDXGIFactory5 *factory5;
  7571. IDXGIFactory6 *factory6;
  7572. DXGI_ADAPTER_DESC1 adapterDesc;
  7573. LARGE_INTEGER umdVersion;
  7574. PFN_D3D12_GET_INTERFACE pD3D12GetInterface;
  7575. PFN_D3D12_CREATE_DEVICE pD3D12CreateDevice;
  7576. #endif
  7577. D3D12_FEATURE_DATA_ARCHITECTURE architecture;
  7578. D3D12_COMMAND_QUEUE_DESC queueDesc;
  7579. bool verboseLogs = SDL_GetBooleanProperty(
  7580. props,
  7581. SDL_PROP_GPU_DEVICE_CREATE_VERBOSE_BOOLEAN,
  7582. true);
  7583. renderer = (D3D12Renderer *)SDL_calloc(1, sizeof(D3D12Renderer));
  7584. bool hasDxgiDebug = false;
  7585. #if !(defined(SDL_PLATFORM_XBOXONE) || defined(SDL_PLATFORM_XBOXSERIES))
  7586. // Load the DXGI library
  7587. renderer->dxgi_dll = SDL_LoadObject(DXGI_DLL);
  7588. if (renderer->dxgi_dll == NULL) {
  7589. D3D12_INTERNAL_DestroyRenderer(renderer);
  7590. SET_STRING_ERROR_AND_RETURN("Could not find " DXGI_DLL, NULL);
  7591. }
  7592. #ifdef HAVE_IDXGIINFOQUEUE
  7593. // Initialize the DXGI debug layer, if applicable
  7594. if (debugMode) {
  7595. hasDxgiDebug = D3D12_INTERNAL_TryInitializeDXGIDebug(renderer);
  7596. }
  7597. #else
  7598. hasDxgiDebug = true;
  7599. #endif
  7600. #ifdef USE_PIX_RUNTIME
  7601. // Load the PIX runtime DLL so that we can set D3D12 debug events on command lists
  7602. renderer->winpixeventruntime_dll = SDL_LoadObject(WINPIXEVENTRUNTIME_DLL);
  7603. WinPixEventRuntimeFns *fns = &renderer->winpixeventruntimeFns;
  7604. if (renderer->winpixeventruntime_dll) {
  7605. // Load the specific functions we need from the PIX runtime
  7606. fns->pBeginEventOnCommandList = (pfnBeginEventOnCommandList)SDL_LoadFunction(
  7607. renderer->winpixeventruntime_dll,
  7608. PIX_BEGIN_EVENT_ON_COMMAND_LIST_FUNC);
  7609. fns->pEndEventOnCommandList = (pfnEndEventOnCommandList)SDL_LoadFunction(
  7610. renderer->winpixeventruntime_dll,
  7611. PIX_END_EVENT_ON_COMMAND_LIST_FUNC);
  7612. fns->pSetMarkerOnCommandList = (pfnSetMarkerOnCommandList)SDL_LoadFunction(
  7613. renderer->winpixeventruntime_dll,
  7614. PIX_SET_MARKER_ON_COMMAND_LIST_FUNC);
  7615. } else {
  7616. SDL_LogWarn(SDL_LOG_CATEGORY_GPU,
  7617. "WinPixEventRuntime.dll is not available. "
  7618. "It is required for SDL_Push/PopGPUDebugGroup and SDL_InsertGPUDebugLabel to function correctly. "
  7619. "See here for instructions on how to obtain it: https://devblogs.microsoft.com/pix/winpixeventruntime/");
  7620. fns->pBeginEventOnCommandList = NULL;
  7621. fns->pEndEventOnCommandList = NULL;
  7622. fns->pSetMarkerOnCommandList = NULL;
  7623. }
  7624. #endif
  7625. // Load the CreateDXGIFactory1 function
  7626. pCreateDXGIFactory1 = (pfnCreateDXGIFactory1)SDL_LoadFunction(
  7627. renderer->dxgi_dll,
  7628. CREATE_DXGI_FACTORY1_FUNC);
  7629. if (pCreateDXGIFactory1 == NULL) {
  7630. D3D12_INTERNAL_DestroyRenderer(renderer);
  7631. SET_STRING_ERROR_AND_RETURN("Could not load function: " CREATE_DXGI_FACTORY1_FUNC, NULL);
  7632. }
  7633. // Create the DXGI factory
  7634. res = pCreateDXGIFactory1(
  7635. &D3D_IID_IDXGIFactory1,
  7636. (void **)&factory1);
  7637. if (FAILED(res)) {
  7638. D3D12_INTERNAL_DestroyRenderer(renderer);
  7639. CHECK_D3D12_ERROR_AND_RETURN("Could not create DXGIFactory", NULL);
  7640. }
  7641. // Check for DXGI 1.4 support
  7642. res = IDXGIFactory1_QueryInterface(
  7643. factory1,
  7644. D3D_GUID(D3D_IID_IDXGIFactory4),
  7645. (void **)&renderer->factory);
  7646. if (FAILED(res)) {
  7647. D3D12_INTERNAL_DestroyRenderer(renderer);
  7648. CHECK_D3D12_ERROR_AND_RETURN("DXGI1.4 support not found, required for DX12", NULL);
  7649. }
  7650. IDXGIFactory1_Release(factory1);
  7651. // Check for explicit tearing support
  7652. res = IDXGIFactory4_QueryInterface(
  7653. renderer->factory,
  7654. D3D_GUID(D3D_IID_IDXGIFactory5),
  7655. (void **)&factory5);
  7656. if (SUCCEEDED(res)) {
  7657. res = IDXGIFactory5_CheckFeatureSupport(
  7658. factory5,
  7659. DXGI_FEATURE_PRESENT_ALLOW_TEARING,
  7660. &renderer->supportsTearing,
  7661. sizeof(renderer->supportsTearing));
  7662. if (FAILED(res)) {
  7663. renderer->supportsTearing = false;
  7664. }
  7665. IDXGIFactory5_Release(factory5);
  7666. }
  7667. // Select the appropriate device for rendering
  7668. res = IDXGIFactory4_QueryInterface(
  7669. renderer->factory,
  7670. D3D_GUID(D3D_IID_IDXGIFactory6),
  7671. (void **)&factory6);
  7672. if (SUCCEEDED(res)) {
  7673. res = IDXGIFactory6_EnumAdapterByGpuPreference(
  7674. factory6,
  7675. 0,
  7676. preferLowPower ? DXGI_GPU_PREFERENCE_MINIMUM_POWER : DXGI_GPU_PREFERENCE_HIGH_PERFORMANCE,
  7677. D3D_GUID(D3D_IID_IDXGIAdapter1),
  7678. (void **)&renderer->adapter);
  7679. IDXGIFactory6_Release(factory6);
  7680. } else {
  7681. res = IDXGIFactory4_EnumAdapters1(
  7682. renderer->factory,
  7683. 0,
  7684. &renderer->adapter);
  7685. }
  7686. if (FAILED(res)) {
  7687. D3D12_INTERNAL_DestroyRenderer(renderer);
  7688. CHECK_D3D12_ERROR_AND_RETURN("Could not find adapter for D3D12Device", NULL);
  7689. }
  7690. // Get information about the selected adapter. Used for logging info.
  7691. res = IDXGIAdapter1_GetDesc1(renderer->adapter, &adapterDesc);
  7692. if (FAILED(res)) {
  7693. D3D12_INTERNAL_DestroyRenderer(renderer);
  7694. CHECK_D3D12_ERROR_AND_RETURN("Could not get adapter description", NULL);
  7695. }
  7696. res = IDXGIAdapter1_CheckInterfaceSupport(renderer->adapter, D3D_GUID(D3D_IID_IDXGIDevice), &umdVersion);
  7697. if (FAILED(res)) {
  7698. D3D12_INTERNAL_DestroyRenderer(renderer);
  7699. CHECK_D3D12_ERROR_AND_RETURN("Could not get adapter driver version", NULL);
  7700. }
  7701. renderer->props = SDL_CreateProperties();
  7702. if (verboseLogs) {
  7703. SDL_LogInfo(SDL_LOG_CATEGORY_GPU, "SDL_GPU Driver: D3D12");
  7704. }
  7705. // Record device name
  7706. char *deviceName = SDL_iconv_wchar_utf8(&adapterDesc.Description[0]);
  7707. SDL_SetStringProperty(
  7708. renderer->props,
  7709. SDL_PROP_GPU_DEVICE_NAME_STRING,
  7710. deviceName);
  7711. if (verboseLogs) {
  7712. SDL_LogInfo(SDL_LOG_CATEGORY_GPU, "D3D12 Adapter: %s", deviceName);
  7713. }
  7714. SDL_free(deviceName);
  7715. // Record driver version
  7716. char driverVer[64];
  7717. (void)SDL_snprintf(
  7718. driverVer,
  7719. SDL_arraysize(driverVer),
  7720. "%d.%d.%d.%d",
  7721. HIWORD(umdVersion.HighPart),
  7722. LOWORD(umdVersion.HighPart),
  7723. HIWORD(umdVersion.LowPart),
  7724. LOWORD(umdVersion.LowPart));
  7725. SDL_SetStringProperty(
  7726. renderer->props,
  7727. SDL_PROP_GPU_DEVICE_DRIVER_VERSION_STRING,
  7728. driverVer);
  7729. if (verboseLogs) {
  7730. SDL_LogInfo(SDL_LOG_CATEGORY_GPU, "D3D12 Driver: %s", driverVer);
  7731. }
  7732. #endif
  7733. // Load the D3D library
  7734. renderer->d3d12_dll = SDL_LoadObject(D3D12_DLL);
  7735. if (renderer->d3d12_dll == NULL) {
  7736. D3D12_INTERNAL_DestroyRenderer(renderer);
  7737. SET_STRING_ERROR_AND_RETURN("Could not find " D3D12_DLL, NULL);
  7738. }
  7739. // Load the CreateDevice function
  7740. #if (defined(SDL_PLATFORM_XBOXONE) || defined(SDL_PLATFORM_XBOXSERIES))
  7741. D3D12XboxCreateDeviceFunc = (PFN_D3D12_XBOX_CREATE_DEVICE)SDL_LoadFunction(
  7742. renderer->d3d12_dll,
  7743. "D3D12XboxCreateDevice");
  7744. if (D3D12XboxCreateDeviceFunc == NULL) {
  7745. D3D12_INTERNAL_DestroyRenderer(renderer);
  7746. SET_STRING_ERROR_AND_RETURN("Could not load function: D3D12XboxCreateDevice", NULL);
  7747. }
  7748. #else
  7749. pD3D12CreateDevice = (PFN_D3D12_CREATE_DEVICE)SDL_LoadFunction(
  7750. renderer->d3d12_dll,
  7751. D3D12_CREATE_DEVICE_FUNC);
  7752. if (pD3D12CreateDevice == NULL) {
  7753. D3D12_INTERNAL_DestroyRenderer(renderer);
  7754. SET_STRING_ERROR_AND_RETURN("Could not load function: " D3D12_CREATE_DEVICE_FUNC, NULL);
  7755. }
  7756. #endif
  7757. renderer->pD3D12SerializeRootSignature = (PFN_D3D12_SERIALIZE_ROOT_SIGNATURE)SDL_LoadFunction(
  7758. renderer->d3d12_dll,
  7759. D3D12_SERIALIZE_ROOT_SIGNATURE_FUNC);
  7760. if (renderer->pD3D12SerializeRootSignature == NULL) {
  7761. D3D12_INTERNAL_DestroyRenderer(renderer);
  7762. SET_STRING_ERROR_AND_RETURN("Could not load function: " D3D12_SERIALIZE_ROOT_SIGNATURE_FUNC, NULL);
  7763. }
  7764. #if !(defined(SDL_PLATFORM_XBOXONE) || defined(SDL_PLATFORM_XBOXSERIES))
  7765. // A device factory allows a D3D12 redistributable provided by the client to be loaded.
  7766. ID3D12DeviceFactory *factory = NULL;
  7767. if (SDL_HasProperty(props, SDL_PROP_GPU_DEVICE_CREATE_D3D12_AGILITY_SDK_PATH_STRING) && SDL_HasProperty(props, SDL_PROP_GPU_DEVICE_CREATE_D3D12_AGILITY_SDK_VERSION_NUMBER)) {
  7768. int d3d12SDKVersion = SDL_GetNumberProperty(props, SDL_PROP_GPU_DEVICE_CREATE_D3D12_AGILITY_SDK_VERSION_NUMBER, 0);
  7769. const char *d3d12SDKPath = SDL_GetStringProperty(props, SDL_PROP_GPU_DEVICE_CREATE_D3D12_AGILITY_SDK_PATH_STRING, ".\\D3D12\\");
  7770. pD3D12GetInterface = (PFN_D3D12_GET_INTERFACE)SDL_LoadFunction(
  7771. renderer->d3d12_dll,
  7772. D3D12_GET_INTERFACE_FUNC);
  7773. if (pD3D12GetInterface == NULL) {
  7774. SDL_LogWarn(SDL_LOG_CATEGORY_GPU, "Could not load D3D12GetInterface, custom D3D12 SDK will not load.");
  7775. }
  7776. ID3D12SDKConfiguration *sdk_config = NULL;
  7777. if (SUCCEEDED(pD3D12GetInterface(D3D_GUID(D3D_CLSID_ID3D12SDKConfiguration), D3D_GUID(D3D_IID_ID3D12SDKConfiguration), (void**) &sdk_config))) {
  7778. ID3D12SDKConfiguration1 *sdk_config1 = NULL;
  7779. if (SUCCEEDED(IUnknown_QueryInterface(sdk_config, &D3D_IID_ID3D12SDKConfiguration1, (void**) &sdk_config1))) {
  7780. if (SUCCEEDED(ID3D12SDKConfiguration1_CreateDeviceFactory(sdk_config1, d3d12SDKVersion, d3d12SDKPath, &D3D_IID_ID3D12DeviceFactory, (void**) &factory))) {
  7781. SDL_LogInfo(SDL_LOG_CATEGORY_GPU, "Loaded vendored D3D12Core.dll");
  7782. } else {
  7783. SDL_LogWarn(SDL_LOG_CATEGORY_GPU, "Failed to load vendored D3D12Core.dll");
  7784. }
  7785. ID3D12SDKConfiguration1_Release(sdk_config1);
  7786. }
  7787. ID3D12SDKConfiguration_Release(sdk_config);
  7788. } else {
  7789. SDL_LogWarn(SDL_LOG_CATEGORY_GPU, "Failed to load vendored D3D12 SDK Configuration");
  7790. }
  7791. }
  7792. #endif
  7793. // Initialize the D3D12 debug layer, if applicable
  7794. if (debugMode) {
  7795. bool hasD3d12Debug = D3D12_INTERNAL_TryInitializeD3D12Debug(renderer
  7796. #if !(defined(SDL_PLATFORM_XBOXONE) || defined(SDL_PLATFORM_XBOXSERIES))
  7797. , factory
  7798. #endif
  7799. );
  7800. #if (defined(SDL_PLATFORM_XBOXONE) || defined(SDL_PLATFORM_XBOXSERIES))
  7801. if (hasD3d12Debug) {
  7802. SDL_LogInfo(
  7803. SDL_LOG_CATEGORY_GPU,
  7804. "Validation layers enabled, expect debug level performance!");
  7805. #else
  7806. if (hasDxgiDebug && hasD3d12Debug) {
  7807. SDL_LogInfo(
  7808. SDL_LOG_CATEGORY_GPU,
  7809. "Validation layers enabled, expect debug level performance!");
  7810. } else if (hasDxgiDebug || hasD3d12Debug) {
  7811. SDL_LogWarn(
  7812. SDL_LOG_CATEGORY_GPU,
  7813. "Validation layers partially enabled, some warnings may not be available");
  7814. #endif
  7815. } else {
  7816. SDL_LogWarn(
  7817. SDL_LOG_CATEGORY_GPU,
  7818. "Validation layers not found, continuing without validation");
  7819. }
  7820. }
  7821. // Create the D3D12Device
  7822. #if (defined(SDL_PLATFORM_XBOXONE) || defined(SDL_PLATFORM_XBOXSERIES))
  7823. if (s_Device != NULL) {
  7824. renderer->device = s_Device;
  7825. } else {
  7826. SDL_zero(createDeviceParams);
  7827. createDeviceParams.Version = D3D12_SDK_VERSION;
  7828. createDeviceParams.GraphicsCommandQueueRingSizeBytes = D3D12XBOX_DEFAULT_SIZE_BYTES;
  7829. createDeviceParams.GraphicsScratchMemorySizeBytes = D3D12XBOX_DEFAULT_SIZE_BYTES;
  7830. createDeviceParams.ComputeScratchMemorySizeBytes = D3D12XBOX_DEFAULT_SIZE_BYTES;
  7831. createDeviceParams.DisableGeometryShaderAllocations = TRUE;
  7832. createDeviceParams.DisableTessellationShaderAllocations = TRUE;
  7833. #if defined(SDL_PLATFORM_XBOXSERIES)
  7834. createDeviceParams.DisableDXR = TRUE;
  7835. #endif
  7836. if (debugMode) {
  7837. createDeviceParams.ProcessDebugFlags = D3D12XBOX_PROCESS_DEBUG_FLAG_DEBUG;
  7838. }
  7839. res = D3D12XboxCreateDeviceFunc(
  7840. NULL,
  7841. &createDeviceParams,
  7842. IID_GRAPHICS_PPV_ARGS(&renderer->device));
  7843. if (FAILED(res)) {
  7844. D3D12_INTERNAL_DestroyRenderer(renderer);
  7845. CHECK_D3D12_ERROR_AND_RETURN("Could not create D3D12Device", NULL);
  7846. }
  7847. s_Device = renderer->device;
  7848. }
  7849. #else
  7850. if (factory) {
  7851. ID3D12DeviceFactory_SetFlags(factory, D3D12_DEVICE_FACTORY_FLAG_ALLOW_RETURNING_EXISTING_DEVICE);
  7852. res = ID3D12DeviceFactory_CreateDevice(
  7853. factory,
  7854. (IUnknown *)renderer->adapter,
  7855. D3D_FEATURE_LEVEL_CHOICE,
  7856. D3D_GUID(D3D_IID_ID3D12Device),
  7857. (void**)&renderer->device);
  7858. ID3D12DeviceFactory_Release(factory);
  7859. if (FAILED(res)) {
  7860. D3D12_INTERNAL_DestroyRenderer(renderer);
  7861. CHECK_D3D12_ERROR_AND_RETURN("Could not create D3D12Device", NULL);
  7862. }
  7863. } else {
  7864. res = pD3D12CreateDevice(
  7865. (IUnknown *)renderer->adapter,
  7866. D3D_FEATURE_LEVEL_CHOICE,
  7867. D3D_GUID(D3D_IID_ID3D12Device),
  7868. (void **)&renderer->device);
  7869. if (FAILED(res)) {
  7870. D3D12_INTERNAL_DestroyRenderer(renderer);
  7871. CHECK_D3D12_ERROR_AND_RETURN("Could not create D3D12Device", NULL);
  7872. }
  7873. }
  7874. // Initialize the D3D12 debug info queue, if applicable
  7875. if (debugMode) {
  7876. D3D12_INTERNAL_TryInitializeD3D12DebugInfoQueue(renderer);
  7877. D3D12_INTERNAL_TryInitializeD3D12DebugInfoLogger(renderer);
  7878. }
  7879. #endif
  7880. // Check UMA
  7881. architecture.NodeIndex = 0;
  7882. res = ID3D12Device_CheckFeatureSupport(
  7883. renderer->device,
  7884. D3D12_FEATURE_ARCHITECTURE,
  7885. &architecture,
  7886. sizeof(D3D12_FEATURE_DATA_ARCHITECTURE));
  7887. if (FAILED(res)) {
  7888. D3D12_INTERNAL_DestroyRenderer(renderer);
  7889. CHECK_D3D12_ERROR_AND_RETURN("Could not get device architecture", NULL);
  7890. }
  7891. renderer->UMA = (bool)architecture.UMA;
  7892. renderer->UMACacheCoherent = (bool)architecture.CacheCoherentUMA;
  7893. #if (defined(SDL_PLATFORM_XBOXONE) || defined(SDL_PLATFORM_XBOXSERIES))
  7894. renderer->GPUUploadHeapSupported = false;
  7895. #else
  7896. // Check "GPU Upload Heap" support (for fast uniform buffers)
  7897. D3D12_FEATURE_DATA_D3D12_OPTIONS16 options16; // 15 wasn't enough, huh?
  7898. renderer->GPUUploadHeapSupported = false;
  7899. res = ID3D12Device_CheckFeatureSupport(
  7900. renderer->device,
  7901. D3D12_FEATURE_D3D12_OPTIONS16,
  7902. &options16,
  7903. sizeof(options16));
  7904. if (SUCCEEDED(res)) {
  7905. renderer->GPUUploadHeapSupported = options16.GPUUploadHeapSupported;
  7906. }
  7907. #endif
  7908. // Check for unrestricted texture-buffer copy pitch support
  7909. D3D12_FEATURE_DATA_D3D12_OPTIONS13 options13;
  7910. res = ID3D12Device_CheckFeatureSupport(
  7911. renderer->device,
  7912. D3D12_FEATURE_D3D12_OPTIONS13,
  7913. &options13,
  7914. sizeof(options13));
  7915. if (SUCCEEDED(res)) {
  7916. renderer->UnrestrictedBufferTextureCopyPitchSupported = options13.UnrestrictedBufferTextureCopyPitchSupported;
  7917. }
  7918. else
  7919. {
  7920. SDL_LogWarn(SDL_LOG_CATEGORY_GPU, "CheckFeatureSupport for UnrestrictedBufferTextureCopyPitchSupported failed. You may need to provide a vendored D3D12Core.dll through the Agility SDK on older platforms.");
  7921. }
  7922. // Create command queue
  7923. #if (defined(SDL_PLATFORM_XBOXONE) || defined(SDL_PLATFORM_XBOXSERIES))
  7924. if (s_CommandQueue != NULL) {
  7925. renderer->commandQueue = s_CommandQueue;
  7926. } else {
  7927. #endif
  7928. queueDesc.Flags = D3D12_COMMAND_QUEUE_FLAG_NONE;
  7929. queueDesc.Type = D3D12_COMMAND_LIST_TYPE_DIRECT;
  7930. queueDesc.NodeMask = 0;
  7931. queueDesc.Priority = 0;
  7932. res = ID3D12Device_CreateCommandQueue(
  7933. renderer->device,
  7934. &queueDesc,
  7935. D3D_GUID(D3D_IID_ID3D12CommandQueue),
  7936. (void **)&renderer->commandQueue);
  7937. if (FAILED(res)) {
  7938. D3D12_INTERNAL_DestroyRenderer(renderer);
  7939. CHECK_D3D12_ERROR_AND_RETURN("Could not create D3D12CommandQueue", NULL);
  7940. }
  7941. #if (defined(SDL_PLATFORM_XBOXONE) || defined(SDL_PLATFORM_XBOXSERIES))
  7942. s_CommandQueue = renderer->commandQueue;
  7943. }
  7944. #endif
  7945. // Create indirect command signatures
  7946. D3D12_COMMAND_SIGNATURE_DESC commandSignatureDesc;
  7947. D3D12_INDIRECT_ARGUMENT_DESC indirectArgumentDesc;
  7948. SDL_zero(indirectArgumentDesc);
  7949. indirectArgumentDesc.Type = D3D12_INDIRECT_ARGUMENT_TYPE_DRAW;
  7950. commandSignatureDesc.NodeMask = 0;
  7951. commandSignatureDesc.ByteStride = sizeof(SDL_GPUIndirectDrawCommand);
  7952. commandSignatureDesc.NumArgumentDescs = 1;
  7953. commandSignatureDesc.pArgumentDescs = &indirectArgumentDesc;
  7954. res = ID3D12Device_CreateCommandSignature(
  7955. renderer->device,
  7956. &commandSignatureDesc,
  7957. NULL,
  7958. D3D_GUID(D3D_IID_ID3D12CommandSignature),
  7959. (void **)&renderer->indirectDrawCommandSignature);
  7960. if (FAILED(res)) {
  7961. D3D12_INTERNAL_DestroyRenderer(renderer);
  7962. CHECK_D3D12_ERROR_AND_RETURN("Could not create indirect draw command signature", NULL);
  7963. }
  7964. indirectArgumentDesc.Type = D3D12_INDIRECT_ARGUMENT_TYPE_DRAW_INDEXED;
  7965. commandSignatureDesc.ByteStride = sizeof(SDL_GPUIndexedIndirectDrawCommand);
  7966. commandSignatureDesc.pArgumentDescs = &indirectArgumentDesc;
  7967. res = ID3D12Device_CreateCommandSignature(
  7968. renderer->device,
  7969. &commandSignatureDesc,
  7970. NULL,
  7971. D3D_GUID(D3D_IID_ID3D12CommandSignature),
  7972. (void **)&renderer->indirectIndexedDrawCommandSignature);
  7973. if (FAILED(res)) {
  7974. D3D12_INTERNAL_DestroyRenderer(renderer);
  7975. CHECK_D3D12_ERROR_AND_RETURN("Could not create indirect indexed draw command signature", NULL);
  7976. }
  7977. indirectArgumentDesc.Type = D3D12_INDIRECT_ARGUMENT_TYPE_DISPATCH;
  7978. commandSignatureDesc.ByteStride = sizeof(SDL_GPUIndirectDispatchCommand);
  7979. commandSignatureDesc.pArgumentDescs = &indirectArgumentDesc;
  7980. res = ID3D12Device_CreateCommandSignature(
  7981. renderer->device,
  7982. &commandSignatureDesc,
  7983. NULL,
  7984. D3D_GUID(D3D_IID_ID3D12CommandSignature),
  7985. (void **)&renderer->indirectDispatchCommandSignature);
  7986. if (FAILED(res)) {
  7987. D3D12_INTERNAL_DestroyRenderer(renderer);
  7988. CHECK_D3D12_ERROR_AND_RETURN("Could not create indirect dispatch command signature", NULL);
  7989. }
  7990. // Initialize pools
  7991. renderer->submittedCommandBufferCapacity = 4;
  7992. renderer->submittedCommandBufferCount = 0;
  7993. renderer->submittedCommandBuffers = (D3D12CommandBuffer **)SDL_calloc(
  7994. renderer->submittedCommandBufferCapacity, sizeof(D3D12CommandBuffer *));
  7995. if (!renderer->submittedCommandBuffers) {
  7996. D3D12_INTERNAL_DestroyRenderer(renderer);
  7997. return NULL;
  7998. }
  7999. renderer->uniformBufferPoolCapacity = 4;
  8000. renderer->uniformBufferPoolCount = 0;
  8001. renderer->uniformBufferPool = (D3D12UniformBuffer **)SDL_calloc(
  8002. renderer->uniformBufferPoolCapacity, sizeof(D3D12UniformBuffer *));
  8003. if (!renderer->uniformBufferPool) {
  8004. D3D12_INTERNAL_DestroyRenderer(renderer);
  8005. return NULL;
  8006. }
  8007. renderer->claimedWindowCapacity = 4;
  8008. renderer->claimedWindowCount = 0;
  8009. renderer->claimedWindows = (D3D12WindowData **)SDL_calloc(
  8010. renderer->claimedWindowCapacity, sizeof(D3D12WindowData *));
  8011. if (!renderer->claimedWindows) {
  8012. D3D12_INTERNAL_DestroyRenderer(renderer);
  8013. return NULL;
  8014. }
  8015. renderer->availableFenceCapacity = 4;
  8016. renderer->availableFenceCount = 0;
  8017. renderer->availableFences = (D3D12Fence **)SDL_calloc(
  8018. renderer->availableFenceCapacity, sizeof(D3D12Fence *));
  8019. if (!renderer->availableFences) {
  8020. D3D12_INTERNAL_DestroyRenderer(renderer);
  8021. return NULL;
  8022. }
  8023. // Initialize staging descriptor pools
  8024. for (Uint32 i = 0; i < D3D12_DESCRIPTOR_HEAP_TYPE_NUM_TYPES; i += 1) {
  8025. renderer->stagingDescriptorPools[i] = D3D12_INTERNAL_CreateStagingDescriptorPool(
  8026. renderer,
  8027. (D3D12_DESCRIPTOR_HEAP_TYPE)i);
  8028. if (renderer->stagingDescriptorPools[i] == NULL) {
  8029. D3D12_INTERNAL_DestroyRenderer(renderer);
  8030. return NULL;
  8031. }
  8032. }
  8033. // Initialize GPU descriptor heaps
  8034. for (Uint32 i = 0; i < 2; i += 1) {
  8035. renderer->gpuDescriptorHeapPools[i].lock = SDL_CreateMutex();
  8036. renderer->gpuDescriptorHeapPools[i].capacity = 4;
  8037. renderer->gpuDescriptorHeapPools[i].count = 4;
  8038. renderer->gpuDescriptorHeapPools[i].heaps = (D3D12DescriptorHeap **)SDL_calloc(
  8039. renderer->gpuDescriptorHeapPools[i].capacity, sizeof(D3D12DescriptorHeap *));
  8040. for (Uint32 j = 0; j < renderer->gpuDescriptorHeapPools[i].capacity; j += 1) {
  8041. renderer->gpuDescriptorHeapPools[i].heaps[j] = D3D12_INTERNAL_CreateDescriptorHeap(
  8042. renderer,
  8043. (D3D12_DESCRIPTOR_HEAP_TYPE)i,
  8044. i == D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV ? VIEW_GPU_DESCRIPTOR_COUNT : SAMPLER_GPU_DESCRIPTOR_COUNT,
  8045. false);
  8046. if (renderer->gpuDescriptorHeapPools[i].heaps[j] == NULL) {
  8047. D3D12_INTERNAL_DestroyRenderer(renderer);
  8048. return NULL;
  8049. }
  8050. }
  8051. }
  8052. // Deferred resource releasing
  8053. renderer->buffersToDestroyCapacity = 4;
  8054. renderer->buffersToDestroyCount = 0;
  8055. renderer->buffersToDestroy = (D3D12Buffer **)SDL_calloc(
  8056. renderer->buffersToDestroyCapacity, sizeof(D3D12Buffer *));
  8057. if (!renderer->buffersToDestroy) {
  8058. D3D12_INTERNAL_DestroyRenderer(renderer);
  8059. return NULL;
  8060. }
  8061. renderer->texturesToDestroyCapacity = 4;
  8062. renderer->texturesToDestroyCount = 0;
  8063. renderer->texturesToDestroy = (D3D12Texture **)SDL_calloc(
  8064. renderer->texturesToDestroyCapacity, sizeof(D3D12Texture *));
  8065. if (!renderer->texturesToDestroy) {
  8066. D3D12_INTERNAL_DestroyRenderer(renderer);
  8067. return NULL;
  8068. }
  8069. renderer->samplersToDestroyCapacity = 4;
  8070. renderer->samplersToDestroyCount = 0;
  8071. renderer->samplersToDestroy = (D3D12Sampler **)SDL_calloc(
  8072. renderer->samplersToDestroyCapacity, sizeof(D3D12Sampler *));
  8073. if (!renderer->samplersToDestroy) {
  8074. D3D12_INTERNAL_DestroyRenderer(renderer);
  8075. return NULL;
  8076. }
  8077. renderer->graphicsPipelinesToDestroyCapacity = 4;
  8078. renderer->graphicsPipelinesToDestroyCount = 0;
  8079. renderer->graphicsPipelinesToDestroy = (D3D12GraphicsPipeline **)SDL_calloc(
  8080. renderer->graphicsPipelinesToDestroyCapacity, sizeof(D3D12GraphicsPipeline *));
  8081. if (!renderer->graphicsPipelinesToDestroy) {
  8082. D3D12_INTERNAL_DestroyRenderer(renderer);
  8083. return NULL;
  8084. }
  8085. renderer->computePipelinesToDestroyCapacity = 4;
  8086. renderer->computePipelinesToDestroyCount = 0;
  8087. renderer->computePipelinesToDestroy = (D3D12ComputePipeline **)SDL_calloc(
  8088. renderer->computePipelinesToDestroyCapacity, sizeof(D3D12ComputePipeline *));
  8089. if (!renderer->computePipelinesToDestroy) {
  8090. D3D12_INTERNAL_DestroyRenderer(renderer);
  8091. return NULL;
  8092. }
  8093. // Locks
  8094. renderer->acquireCommandBufferLock = SDL_CreateMutex();
  8095. renderer->acquireUniformBufferLock = SDL_CreateMutex();
  8096. renderer->submitLock = SDL_CreateMutex();
  8097. renderer->windowLock = SDL_CreateMutex();
  8098. renderer->fenceLock = SDL_CreateMutex();
  8099. renderer->disposeLock = SDL_CreateMutex();
  8100. renderer->debug_mode = debugMode;
  8101. renderer->allowedFramesInFlight = 2;
  8102. renderer->semantic = SDL_strdup(SDL_GetStringProperty(props, SDL_PROP_GPU_DEVICE_CREATE_D3D12_SEMANTIC_NAME_STRING, "TEXCOORD"));
  8103. // Blit resources
  8104. D3D12_INTERNAL_InitBlitResources(renderer);
  8105. #if (defined(SDL_PLATFORM_XBOXONE) || defined(SDL_PLATFORM_XBOXSERIES))
  8106. res = renderer->device->SetFrameIntervalX(
  8107. NULL,
  8108. D3D12XBOX_FRAME_INTERVAL_60_HZ,
  8109. renderer->allowedFramesInFlight - 1,
  8110. D3D12XBOX_FRAME_INTERVAL_FLAG_NONE);
  8111. if (FAILED(res)) {
  8112. D3D12_INTERNAL_DestroyRenderer(renderer);
  8113. CHECK_D3D12_ERROR_AND_RETURN("Could not get set frame interval", NULL);
  8114. }
  8115. res = renderer->device->ScheduleFrameEventX(
  8116. D3D12XBOX_FRAME_EVENT_ORIGIN,
  8117. 0,
  8118. NULL,
  8119. D3D12XBOX_SCHEDULE_FRAME_EVENT_FLAG_NONE);
  8120. if (FAILED(res)) {
  8121. D3D12_INTERNAL_DestroyRenderer(renderer);
  8122. CHECK_D3D12_ERROR_AND_RETURN("Could not schedule frame events", NULL);
  8123. }
  8124. #endif
  8125. // Create the SDL_GPU Device
  8126. result = (SDL_GPUDevice *)SDL_calloc(1, sizeof(SDL_GPUDevice));
  8127. if (!result) {
  8128. D3D12_INTERNAL_DestroyRenderer(renderer);
  8129. return NULL;
  8130. }
  8131. SDL_GPUShaderFormat shaderFormats = SDL_GPU_SHADERFORMAT_DXBC;
  8132. D3D12_FEATURE_DATA_SHADER_MODEL shaderModel;
  8133. shaderModel.HighestShaderModel = D3D_SHADER_MODEL_6_0;
  8134. res = ID3D12Device_CheckFeatureSupport(
  8135. renderer->device,
  8136. D3D12_FEATURE_SHADER_MODEL,
  8137. &shaderModel,
  8138. sizeof(shaderModel));
  8139. if (SUCCEEDED(res) && shaderModel.HighestShaderModel >= D3D_SHADER_MODEL_6_0) {
  8140. shaderFormats |= SDL_GPU_SHADERFORMAT_DXIL;
  8141. }
  8142. ASSIGN_DRIVER(D3D12)
  8143. result->driverData = (SDL_GPURenderer *)renderer;
  8144. result->shader_formats = shaderFormats;
  8145. result->debug_mode = debugMode;
  8146. renderer->sdlGPUDevice = result;
  8147. return result;
  8148. }
  8149. SDL_GPUBootstrap D3D12Driver = {
  8150. "direct3d12",
  8151. D3D12_PrepareDriver,
  8152. D3D12_CreateDevice
  8153. };
  8154. #endif // SDL_GPU_D3D12
  8155. // GDK-specific APIs
  8156. #ifdef SDL_PLATFORM_GDK
  8157. void SDL_GDKSuspendGPU(SDL_GPUDevice *device)
  8158. {
  8159. #if defined(SDL_GPU_D3D12) && (defined(SDL_PLATFORM_XBOXONE) || defined(SDL_PLATFORM_XBOXSERIES))
  8160. D3D12Renderer *renderer = (D3D12Renderer *)device->driverData;
  8161. HRESULT res;
  8162. if (device == NULL) {
  8163. SET_STRING_ERROR_AND_RETURN("Invalid GPU device", );
  8164. }
  8165. SDL_LockMutex(renderer->submitLock);
  8166. res = renderer->commandQueue->SuspendX(0);
  8167. if (FAILED(res)) {
  8168. SDL_LogError(SDL_LOG_CATEGORY_GPU, "SuspendX failed: %X", res);
  8169. }
  8170. SDL_UnlockMutex(renderer->submitLock);
  8171. #endif
  8172. }
  8173. void SDL_GDKResumeGPU(SDL_GPUDevice *device)
  8174. {
  8175. #if defined(SDL_GPU_D3D12) && (defined(SDL_PLATFORM_XBOXONE) || defined(SDL_PLATFORM_XBOXSERIES))
  8176. D3D12Renderer *renderer = (D3D12Renderer *)device->driverData;
  8177. HRESULT res;
  8178. if (device == NULL) {
  8179. SET_STRING_ERROR_AND_RETURN("Invalid GPU device", );
  8180. }
  8181. SDL_LockMutex(renderer->submitLock);
  8182. res = renderer->commandQueue->ResumeX();
  8183. if (FAILED(res)) {
  8184. SDL_LogError(SDL_LOG_CATEGORY_GPU, "ResumeX failed: %X", res);
  8185. }
  8186. SDL_UnlockMutex(renderer->submitLock);
  8187. res = renderer->device->SetFrameIntervalX(
  8188. NULL,
  8189. D3D12XBOX_FRAME_INTERVAL_60_HZ,
  8190. renderer->allowedFramesInFlight - 1,
  8191. D3D12XBOX_FRAME_INTERVAL_FLAG_NONE);
  8192. if (FAILED(res)) {
  8193. SDL_LogError(SDL_LOG_CATEGORY_GPU, "Could not set frame interval: %X", res);
  8194. }
  8195. res = renderer->device->ScheduleFrameEventX(
  8196. D3D12XBOX_FRAME_EVENT_ORIGIN,
  8197. 0,
  8198. NULL,
  8199. D3D12XBOX_SCHEDULE_FRAME_EVENT_FLAG_NONE);
  8200. if (FAILED(res)) {
  8201. SDL_LogError(SDL_LOG_CATEGORY_GPU, "Could not schedule frame events: %X", res);
  8202. }
  8203. #endif
  8204. }
  8205. #endif // SDL_PLATFORM_GDK