SDL_gpu_d3d12.c 298 KB

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