rendering_device.cpp 307 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071
  1. /**************************************************************************/
  2. /* rendering_device.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #include "rendering_device.h"
  31. #include "rendering_device.compat.inc"
  32. #include "rendering_device_binds.h"
  33. #include "core/config/project_settings.h"
  34. #include "core/io/dir_access.h"
  35. #include "servers/rendering/renderer_rd/api_context_rd.h"
  36. //#define FORCE_FULL_BARRIER
  37. RenderingDevice *RenderingDevice::singleton = nullptr;
  38. RenderingDevice *RenderingDevice::get_singleton() {
  39. return singleton;
  40. }
  41. RenderingDevice::ShaderCompileToSPIRVFunction RenderingDevice::compile_to_spirv_function = nullptr;
  42. RenderingDevice::ShaderCacheFunction RenderingDevice::cache_function = nullptr;
  43. RenderingDevice::ShaderSPIRVGetCacheKeyFunction RenderingDevice::get_spirv_cache_key_function = nullptr;
  44. /***************************/
  45. /**** ID INFRASTRUCTURE ****/
  46. /***************************/
  47. void RenderingDevice::_add_dependency(RID p_id, RID p_depends_on) {
  48. if (!dependency_map.has(p_depends_on)) {
  49. dependency_map[p_depends_on] = HashSet<RID>();
  50. }
  51. dependency_map[p_depends_on].insert(p_id);
  52. if (!reverse_dependency_map.has(p_id)) {
  53. reverse_dependency_map[p_id] = HashSet<RID>();
  54. }
  55. reverse_dependency_map[p_id].insert(p_depends_on);
  56. }
  57. void RenderingDevice::_free_dependencies(RID p_id) {
  58. // Direct dependencies must be freed.
  59. HashMap<RID, HashSet<RID>>::Iterator E = dependency_map.find(p_id);
  60. if (E) {
  61. while (E->value.size()) {
  62. free(*E->value.begin());
  63. }
  64. dependency_map.remove(E);
  65. }
  66. // Reverse dependencies must be unreferenced.
  67. E = reverse_dependency_map.find(p_id);
  68. if (E) {
  69. for (const RID &F : E->value) {
  70. HashMap<RID, HashSet<RID>>::Iterator G = dependency_map.find(F);
  71. ERR_CONTINUE(!G);
  72. ERR_CONTINUE(!G->value.has(p_id));
  73. G->value.erase(p_id);
  74. }
  75. reverse_dependency_map.remove(E);
  76. }
  77. }
  78. void RenderingDevice::shader_set_compile_to_spirv_function(ShaderCompileToSPIRVFunction p_function) {
  79. compile_to_spirv_function = p_function;
  80. }
  81. void RenderingDevice::shader_set_spirv_cache_function(ShaderCacheFunction p_function) {
  82. cache_function = p_function;
  83. }
  84. void RenderingDevice::shader_set_get_cache_key_function(ShaderSPIRVGetCacheKeyFunction p_function) {
  85. get_spirv_cache_key_function = p_function;
  86. }
  87. Vector<uint8_t> RenderingDevice::shader_compile_spirv_from_source(ShaderStage p_stage, const String &p_source_code, ShaderLanguage p_language, String *r_error, bool p_allow_cache) {
  88. if (p_allow_cache && cache_function) {
  89. Vector<uint8_t> cache = cache_function(p_stage, p_source_code, p_language);
  90. if (cache.size()) {
  91. return cache;
  92. }
  93. }
  94. ERR_FAIL_NULL_V(compile_to_spirv_function, Vector<uint8_t>());
  95. return compile_to_spirv_function(p_stage, p_source_code, p_language, r_error, this);
  96. }
  97. String RenderingDevice::shader_get_spirv_cache_key() const {
  98. if (get_spirv_cache_key_function) {
  99. return get_spirv_cache_key_function(this);
  100. }
  101. return String();
  102. }
  103. RID RenderingDevice::shader_create_from_spirv(const Vector<ShaderStageSPIRVData> &p_spirv, const String &p_shader_name) {
  104. Vector<uint8_t> bytecode = shader_compile_binary_from_spirv(p_spirv, p_shader_name);
  105. ERR_FAIL_COND_V(bytecode.size() == 0, RID());
  106. return shader_create_from_bytecode(bytecode);
  107. }
  108. /******************/
  109. /**** BARRIERS ****/
  110. /******************/
  111. void RenderingDevice::_full_barrier(bool p_sync_with_draw) {
  112. // Used for debug.
  113. RDD::MemoryBarrier mb;
  114. mb.src_access = (RDD::BARRIER_ACCESS_INDIRECT_COMMAND_READ_BIT |
  115. RDD::BARRIER_ACCESS_INDEX_READ_BIT |
  116. RDD::BARRIER_ACCESS_VERTEX_ATTRIBUTE_READ_BIT |
  117. RDD::BARRIER_ACCESS_UNIFORM_READ_BIT |
  118. RDD::BARRIER_ACCESS_INPUT_ATTACHMENT_READ_BIT |
  119. RDD::BARRIER_ACCESS_SHADER_READ_BIT |
  120. RDD::BARRIER_ACCESS_SHADER_WRITE_BIT |
  121. RDD::BARRIER_ACCESS_COLOR_ATTACHMENT_READ_BIT |
  122. RDD::BARRIER_ACCESS_COLOR_ATTACHMENT_WRITE_BIT |
  123. RDD::BARRIER_ACCESS_DEPTH_STENCIL_ATTACHMENT_READ_BIT |
  124. RDD::BARRIER_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT |
  125. RDD::BARRIER_ACCESS_TRANSFER_READ_BIT |
  126. RDD::BARRIER_ACCESS_TRANSFER_WRITE_BIT |
  127. RDD::BARRIER_ACCESS_HOST_READ_BIT |
  128. RDD::BARRIER_ACCESS_HOST_WRITE_BIT);
  129. mb.dst_access = (RDD::BARRIER_ACCESS_INDIRECT_COMMAND_READ_BIT |
  130. RDD::BARRIER_ACCESS_INDEX_READ_BIT |
  131. RDD::BARRIER_ACCESS_VERTEX_ATTRIBUTE_READ_BIT |
  132. RDD::BARRIER_ACCESS_UNIFORM_READ_BIT |
  133. RDD::BARRIER_ACCESS_INPUT_ATTACHMENT_READ_BIT |
  134. RDD::BARRIER_ACCESS_SHADER_READ_BIT |
  135. RDD::BARRIER_ACCESS_SHADER_WRITE_BIT |
  136. RDD::BARRIER_ACCESS_COLOR_ATTACHMENT_READ_BIT |
  137. RDD::BARRIER_ACCESS_COLOR_ATTACHMENT_WRITE_BIT |
  138. RDD::BARRIER_ACCESS_DEPTH_STENCIL_ATTACHMENT_READ_BIT |
  139. RDD::BARRIER_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT |
  140. RDD::BARRIER_ACCESS_TRANSFER_READ_BIT |
  141. RDD::BARRIER_ACCESS_TRANSFER_WRITE_BIT |
  142. RDD::BARRIER_ACCESS_HOST_READ_BIT |
  143. RDD::BARRIER_ACCESS_HOST_WRITE_BIT);
  144. RDD::CommandBufferID cmd_buffer = p_sync_with_draw ? frames[frame].draw_command_buffer : frames[frame].setup_command_buffer;
  145. driver->command_pipeline_barrier(cmd_buffer, RDD::PIPELINE_STAGE_ALL_COMMANDS_BIT, RDD::PIPELINE_STAGE_ALL_COMMANDS_BIT, mb, {}, {});
  146. }
  147. /***************************/
  148. /**** BUFFER MANAGEMENT ****/
  149. /***************************/
  150. RenderingDevice::Buffer *RenderingDevice::_get_buffer_from_owner(RID p_buffer, BitField<RDD::PipelineStageBits> &r_stages, BitField<RDD::BarrierAccessBits> &r_access, BitField<BarrierMask> p_post_barrier) {
  151. Buffer *buffer = nullptr;
  152. r_stages.clear();
  153. r_access.clear();
  154. if (vertex_buffer_owner.owns(p_buffer)) {
  155. buffer = vertex_buffer_owner.get_or_null(p_buffer);
  156. r_stages.set_flag(RDD::PIPELINE_STAGE_VERTEX_INPUT_BIT);
  157. r_access.set_flag(RDD::BARRIER_ACCESS_VERTEX_ATTRIBUTE_READ_BIT);
  158. if (buffer->usage & RDD::BUFFER_USAGE_STORAGE_BIT) {
  159. if (p_post_barrier.has_flag(BARRIER_MASK_VERTEX)) {
  160. r_access.set_flag(RDD::BARRIER_ACCESS_SHADER_READ_BIT).set_flag(RDD::BARRIER_ACCESS_SHADER_WRITE_BIT);
  161. r_stages.set_flag(RDD::PIPELINE_STAGE_VERTEX_SHADER_BIT);
  162. }
  163. if (p_post_barrier.has_flag(BARRIER_MASK_FRAGMENT)) {
  164. r_access.set_flag(RDD::BARRIER_ACCESS_SHADER_READ_BIT).set_flag(RDD::BARRIER_ACCESS_SHADER_WRITE_BIT);
  165. r_stages.set_flag(RDD::PIPELINE_STAGE_FRAGMENT_SHADER_BIT);
  166. }
  167. if (p_post_barrier.has_flag(BARRIER_MASK_COMPUTE)) {
  168. r_access.set_flag(RDD::BARRIER_ACCESS_SHADER_READ_BIT).set_flag(RDD::BARRIER_ACCESS_SHADER_WRITE_BIT);
  169. r_stages.set_flag(RDD::PIPELINE_STAGE_COMPUTE_SHADER_BIT);
  170. }
  171. }
  172. } else if (index_buffer_owner.owns(p_buffer)) {
  173. r_stages.set_flag(RDD::PIPELINE_STAGE_VERTEX_INPUT_BIT);
  174. r_access.set_flag(RDD::BARRIER_ACCESS_INDEX_READ_BIT);
  175. buffer = index_buffer_owner.get_or_null(p_buffer);
  176. } else if (uniform_buffer_owner.owns(p_buffer)) {
  177. if (p_post_barrier.has_flag(BARRIER_MASK_VERTEX)) {
  178. r_stages.set_flag(RDD::PIPELINE_STAGE_VERTEX_SHADER_BIT);
  179. }
  180. if (p_post_barrier.has_flag(BARRIER_MASK_FRAGMENT)) {
  181. r_stages.set_flag(RDD::PIPELINE_STAGE_FRAGMENT_SHADER_BIT);
  182. }
  183. if (p_post_barrier.has_flag(BARRIER_MASK_COMPUTE)) {
  184. r_stages.set_flag(RDD::PIPELINE_STAGE_COMPUTE_SHADER_BIT);
  185. }
  186. r_access.set_flag(RDD::BARRIER_ACCESS_UNIFORM_READ_BIT);
  187. buffer = uniform_buffer_owner.get_or_null(p_buffer);
  188. } else if (texture_buffer_owner.owns(p_buffer)) {
  189. if (p_post_barrier.has_flag(BARRIER_MASK_VERTEX)) {
  190. r_stages.set_flag(RDD::PIPELINE_STAGE_VERTEX_SHADER_BIT);
  191. r_access.set_flag(RDD::BARRIER_ACCESS_SHADER_READ_BIT);
  192. }
  193. if (p_post_barrier.has_flag(BARRIER_MASK_FRAGMENT)) {
  194. r_stages.set_flag(RDD::PIPELINE_STAGE_FRAGMENT_SHADER_BIT);
  195. r_access.set_flag(RDD::BARRIER_ACCESS_SHADER_READ_BIT);
  196. }
  197. if (p_post_barrier.has_flag(BARRIER_MASK_COMPUTE)) {
  198. r_stages.set_flag(RDD::PIPELINE_STAGE_COMPUTE_SHADER_BIT);
  199. r_access.set_flag(RDD::BARRIER_ACCESS_SHADER_READ_BIT);
  200. }
  201. // FIXME: Broken.
  202. //buffer = texture_buffer_owner.get_or_null(p_buffer)->buffer;
  203. } else if (storage_buffer_owner.owns(p_buffer)) {
  204. buffer = storage_buffer_owner.get_or_null(p_buffer);
  205. if (p_post_barrier.has_flag(BARRIER_MASK_VERTEX)) {
  206. r_stages.set_flag(RDD::PIPELINE_STAGE_VERTEX_SHADER_BIT);
  207. r_access.set_flag(RDD::BARRIER_ACCESS_SHADER_READ_BIT).set_flag(RDD::BARRIER_ACCESS_SHADER_WRITE_BIT);
  208. }
  209. if (p_post_barrier.has_flag(BARRIER_MASK_FRAGMENT)) {
  210. r_stages.set_flag(RDD::PIPELINE_STAGE_FRAGMENT_SHADER_BIT);
  211. r_access.set_flag(RDD::BARRIER_ACCESS_SHADER_READ_BIT).set_flag(RDD::BARRIER_ACCESS_SHADER_WRITE_BIT);
  212. }
  213. if (p_post_barrier.has_flag(BARRIER_MASK_COMPUTE)) {
  214. r_stages.set_flag(RDD::PIPELINE_STAGE_COMPUTE_SHADER_BIT);
  215. r_access.set_flag(RDD::BARRIER_ACCESS_SHADER_READ_BIT).set_flag(RDD::BARRIER_ACCESS_SHADER_WRITE_BIT);
  216. }
  217. if (buffer->usage.has_flag(RDD::BUFFER_USAGE_INDIRECT_BIT)) {
  218. r_stages.set_flag(RDD::PIPELINE_STAGE_DRAW_INDIRECT_BIT);
  219. r_access.set_flag(RDD::BARRIER_ACCESS_INDIRECT_COMMAND_READ_BIT);
  220. }
  221. }
  222. return buffer;
  223. }
  224. Error RenderingDevice::_insert_staging_block() {
  225. StagingBufferBlock block;
  226. block.driver_id = driver->buffer_create(staging_buffer_block_size, RDD::BUFFER_USAGE_TRANSFER_FROM_BIT, RDD::MEMORY_ALLOCATION_TYPE_CPU);
  227. ERR_FAIL_COND_V(!block.driver_id, ERR_CANT_CREATE);
  228. block.frame_used = 0;
  229. block.fill_amount = 0;
  230. staging_buffer_blocks.insert(staging_buffer_current, block);
  231. return OK;
  232. }
  233. Error RenderingDevice::_staging_buffer_allocate(uint32_t p_amount, uint32_t p_required_align, uint32_t &r_alloc_offset, uint32_t &r_alloc_size, bool p_can_segment) {
  234. // Determine a block to use.
  235. r_alloc_size = p_amount;
  236. while (true) {
  237. r_alloc_offset = 0;
  238. // See if we can use current block.
  239. if (staging_buffer_blocks[staging_buffer_current].frame_used == frames_drawn) {
  240. // We used this block this frame, let's see if there is still room.
  241. uint32_t write_from = staging_buffer_blocks[staging_buffer_current].fill_amount;
  242. {
  243. uint32_t align_remainder = write_from % p_required_align;
  244. if (align_remainder != 0) {
  245. write_from += p_required_align - align_remainder;
  246. }
  247. }
  248. int32_t available_bytes = int32_t(staging_buffer_block_size) - int32_t(write_from);
  249. if ((int32_t)p_amount < available_bytes) {
  250. // All is good, we should be ok, all will fit.
  251. r_alloc_offset = write_from;
  252. } else if (p_can_segment && available_bytes >= (int32_t)p_required_align) {
  253. // Ok all won't fit but at least we can fit a chunkie.
  254. // All is good, update what needs to be written to.
  255. r_alloc_offset = write_from;
  256. r_alloc_size = available_bytes - (available_bytes % p_required_align);
  257. } else {
  258. // Can't fit it into this buffer.
  259. // Will need to try next buffer.
  260. staging_buffer_current = (staging_buffer_current + 1) % staging_buffer_blocks.size();
  261. // Before doing anything, though, let's check that we didn't manage to fill all blocks.
  262. // Possible in a single frame.
  263. if (staging_buffer_blocks[staging_buffer_current].frame_used == frames_drawn) {
  264. // Guess we did.. ok, let's see if we can insert a new block.
  265. if ((uint64_t)staging_buffer_blocks.size() * staging_buffer_block_size < staging_buffer_max_size) {
  266. // We can, so we are safe.
  267. Error err = _insert_staging_block();
  268. if (err) {
  269. return err;
  270. }
  271. // Claim for this frame.
  272. staging_buffer_blocks.write[staging_buffer_current].frame_used = frames_drawn;
  273. } else {
  274. // Ok, worst case scenario, all the staging buffers belong to this frame
  275. // and this frame is not even done.
  276. // If this is the main thread, it means the user is likely loading a lot of resources at once,.
  277. // Otherwise, the thread should just be blocked until the next frame (currently unimplemented).
  278. if (false) { // Separate thread from render.
  279. //block_until_next_frame()
  280. continue;
  281. } else {
  282. // Flush EVERYTHING including setup commands. IF not immediate, also need to flush the draw commands.
  283. _flush(true);
  284. // Clear the whole staging buffer.
  285. for (int i = 0; i < staging_buffer_blocks.size(); i++) {
  286. staging_buffer_blocks.write[i].frame_used = 0;
  287. staging_buffer_blocks.write[i].fill_amount = 0;
  288. }
  289. // Claim current.
  290. staging_buffer_blocks.write[staging_buffer_current].frame_used = frames_drawn;
  291. }
  292. }
  293. } else {
  294. // Not from current frame, so continue and try again.
  295. continue;
  296. }
  297. }
  298. } else if (staging_buffer_blocks[staging_buffer_current].frame_used <= frames_drawn - frame_count) {
  299. // This is an old block, which was already processed, let's reuse.
  300. staging_buffer_blocks.write[staging_buffer_current].frame_used = frames_drawn;
  301. staging_buffer_blocks.write[staging_buffer_current].fill_amount = 0;
  302. } else {
  303. // This block may still be in use, let's not touch it unless we have to, so.. can we create a new one?
  304. if ((uint64_t)staging_buffer_blocks.size() * staging_buffer_block_size < staging_buffer_max_size) {
  305. // We are still allowed to create a new block, so let's do that and insert it for current pos.
  306. Error err = _insert_staging_block();
  307. if (err) {
  308. return err;
  309. }
  310. // Claim for this frame.
  311. staging_buffer_blocks.write[staging_buffer_current].frame_used = frames_drawn;
  312. } else {
  313. // Oops, we are out of room and we can't create more.
  314. // Let's flush older frames.
  315. // The logic here is that if a game is loading a lot of data from the main thread, it will need to be stalled anyway.
  316. // If loading from a separate thread, we can block that thread until next frame when more room is made (not currently implemented, though).
  317. if (false) {
  318. // Separate thread from render.
  319. //block_until_next_frame()
  320. continue; // And try again.
  321. } else {
  322. _flush(false);
  323. for (int i = 0; i < staging_buffer_blocks.size(); i++) {
  324. // Clear all blocks but the ones from this frame.
  325. int block_idx = (i + staging_buffer_current) % staging_buffer_blocks.size();
  326. if (staging_buffer_blocks[block_idx].frame_used == frames_drawn) {
  327. break; // Ok, we reached something from this frame, abort.
  328. }
  329. staging_buffer_blocks.write[block_idx].frame_used = 0;
  330. staging_buffer_blocks.write[block_idx].fill_amount = 0;
  331. }
  332. // Claim for current frame.
  333. staging_buffer_blocks.write[staging_buffer_current].frame_used = frames_drawn;
  334. }
  335. }
  336. }
  337. // All was good, break.
  338. break;
  339. }
  340. staging_buffer_used = true;
  341. return OK;
  342. }
  343. Error RenderingDevice::_buffer_update(Buffer *p_buffer, size_t p_offset, const uint8_t *p_data, size_t p_data_size, bool p_use_draw_command_buffer, uint32_t p_required_align) {
  344. // Submitting may get chunked for various reasons, so convert this to a task.
  345. size_t to_submit = p_data_size;
  346. size_t submit_from = 0;
  347. while (to_submit > 0) {
  348. uint32_t block_write_offset;
  349. uint32_t block_write_amount;
  350. Error err = _staging_buffer_allocate(MIN(to_submit, staging_buffer_block_size), p_required_align, block_write_offset, block_write_amount);
  351. if (err) {
  352. return err;
  353. }
  354. // Map staging buffer (It's CPU and coherent).
  355. uint8_t *data_ptr = driver->buffer_map(staging_buffer_blocks[staging_buffer_current].driver_id);
  356. ERR_FAIL_NULL_V(data_ptr, ERR_CANT_CREATE);
  357. // Copy to staging buffer.
  358. memcpy(data_ptr + block_write_offset, p_data + submit_from, block_write_amount);
  359. // Unmap.
  360. driver->buffer_unmap(staging_buffer_blocks[staging_buffer_current].driver_id);
  361. // Insert a command to copy this.
  362. RDD::BufferCopyRegion region;
  363. region.src_offset = block_write_offset;
  364. region.dst_offset = submit_from + p_offset;
  365. region.size = block_write_amount;
  366. driver->command_copy_buffer(p_use_draw_command_buffer ? frames[frame].draw_command_buffer : frames[frame].setup_command_buffer, staging_buffer_blocks[staging_buffer_current].driver_id, p_buffer->driver_id, region);
  367. staging_buffer_blocks.write[staging_buffer_current].fill_amount = block_write_offset + block_write_amount;
  368. to_submit -= block_write_amount;
  369. submit_from += block_write_amount;
  370. }
  371. return OK;
  372. }
  373. Error RenderingDevice::buffer_copy(RID p_src_buffer, RID p_dst_buffer, uint32_t p_src_offset, uint32_t p_dst_offset, uint32_t p_size, BitField<BarrierMask> p_post_barrier) {
  374. _THREAD_SAFE_METHOD_
  375. ERR_FAIL_COND_V_MSG(draw_list, ERR_INVALID_PARAMETER,
  376. "Copying buffers is forbidden during creation of a draw list");
  377. ERR_FAIL_COND_V_MSG(compute_list, ERR_INVALID_PARAMETER,
  378. "Copying buffers is forbidden during creation of a compute list");
  379. // This method assumes the barriers have been pushed prior to being called, therefore no barriers are pushed
  380. // for the source or destination buffers before performing the copy. These masks are effectively ignored.
  381. BitField<RDD::PipelineStageBits> src_stages;
  382. BitField<RDD::BarrierAccessBits> src_access;
  383. Buffer *src_buffer = _get_buffer_from_owner(p_src_buffer, src_stages, src_access, BARRIER_MASK_NO_BARRIER);
  384. if (!src_buffer) {
  385. ERR_FAIL_V_MSG(ERR_INVALID_PARAMETER, "Source buffer argument is not a valid buffer of any type.");
  386. }
  387. BitField<RDD::PipelineStageBits> dst_stages;
  388. BitField<RDD::BarrierAccessBits> dst_access;
  389. if (p_post_barrier.has_flag(BARRIER_MASK_TRANSFER)) {
  390. // If the post barrier mask defines it, we indicate the destination buffer will require a barrier with these flags set
  391. // after the copy command is queued.
  392. dst_stages.set_flag(RDD::PIPELINE_STAGE_TRANSFER_BIT);
  393. dst_access.set_flag(RDD::BARRIER_ACCESS_TRANSFER_WRITE_BIT);
  394. }
  395. Buffer *dst_buffer = _get_buffer_from_owner(p_dst_buffer, dst_stages, dst_access, p_post_barrier);
  396. if (!dst_buffer) {
  397. ERR_FAIL_V_MSG(ERR_INVALID_PARAMETER, "Destination buffer argument is not a valid buffer of any type.");
  398. }
  399. // Validate the copy's dimensions for both buffers.
  400. ERR_FAIL_COND_V_MSG((p_size + p_src_offset) > src_buffer->size, ERR_INVALID_PARAMETER, "Size is larger than the source buffer.");
  401. ERR_FAIL_COND_V_MSG((p_size + p_dst_offset) > dst_buffer->size, ERR_INVALID_PARAMETER, "Size is larger than the destination buffer.");
  402. // Perform the copy.
  403. RDD::BufferCopyRegion region;
  404. region.src_offset = p_src_offset;
  405. region.dst_offset = p_dst_offset;
  406. region.size = p_size;
  407. driver->command_copy_buffer(frames[frame].draw_command_buffer, src_buffer->driver_id, dst_buffer->driver_id, region);
  408. #ifdef FORCE_FULL_BARRIER
  409. _full_barrier(true);
  410. #else
  411. if (driver->api_trait_get(RDD::API_TRAIT_HONORS_PIPELINE_BARRIERS) && p_post_barrier != RD::BARRIER_MASK_NO_BARRIER) {
  412. if (dst_stages.is_empty()) {
  413. dst_stages = RDD::PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT;
  414. }
  415. // As indicated by the post barrier mask, push a new barrier.
  416. RDD::BufferBarrier bb;
  417. bb.buffer = dst_buffer->driver_id;
  418. bb.src_access = RDD::BARRIER_ACCESS_TRANSFER_WRITE_BIT;
  419. bb.dst_access = dst_access;
  420. bb.offset = p_dst_offset;
  421. bb.size = p_size;
  422. driver->command_pipeline_barrier(frames[frame].draw_command_buffer, RDD::PIPELINE_STAGE_TRANSFER_BIT, dst_stages, {}, bb, {});
  423. }
  424. #endif
  425. return OK;
  426. }
  427. Error RenderingDevice::buffer_update(RID p_buffer, uint32_t p_offset, uint32_t p_size, const void *p_data, BitField<BarrierMask> p_post_barrier) {
  428. _THREAD_SAFE_METHOD_
  429. ERR_FAIL_COND_V_MSG(draw_list, ERR_INVALID_PARAMETER,
  430. "Updating buffers is forbidden during creation of a draw list");
  431. ERR_FAIL_COND_V_MSG(compute_list, ERR_INVALID_PARAMETER,
  432. "Updating buffers is forbidden during creation of a compute list");
  433. BitField<RDD::PipelineStageBits> dst_stages;
  434. BitField<RDD::BarrierAccessBits> dst_access;
  435. if (p_post_barrier.has_flag(BARRIER_MASK_TRANSFER)) {
  436. // Protect subsequent updates.
  437. dst_stages.set_flag(RDD::PIPELINE_STAGE_TRANSFER_BIT);
  438. dst_access.set_flag(RDD::BARRIER_ACCESS_TRANSFER_WRITE_BIT);
  439. }
  440. Buffer *buffer = _get_buffer_from_owner(p_buffer, dst_stages, dst_access, p_post_barrier);
  441. if (!buffer) {
  442. ERR_FAIL_V_MSG(ERR_INVALID_PARAMETER, "Buffer argument is not a valid buffer of any type.");
  443. }
  444. ERR_FAIL_COND_V_MSG(p_offset + p_size > buffer->size, ERR_INVALID_PARAMETER,
  445. "Attempted to write buffer (" + itos((p_offset + p_size) - buffer->size) + " bytes) past the end.");
  446. Error err = _buffer_update(buffer, p_offset, (uint8_t *)p_data, p_size, true);
  447. if (err) {
  448. return err;
  449. }
  450. #ifdef FORCE_FULL_BARRIER
  451. _full_barrier(true);
  452. #else
  453. if (dst_stages.is_empty()) {
  454. dst_stages.set_flag(RDD::PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT);
  455. }
  456. if (driver->api_trait_get(RDD::API_TRAIT_HONORS_PIPELINE_BARRIERS) && p_post_barrier != RD::BARRIER_MASK_NO_BARRIER) {
  457. RDD::BufferBarrier bb;
  458. bb.buffer = buffer->driver_id;
  459. bb.src_access = RDD::BARRIER_ACCESS_TRANSFER_WRITE_BIT;
  460. bb.dst_access = dst_access;
  461. bb.offset = p_offset;
  462. bb.size = p_size;
  463. driver->command_pipeline_barrier(frames[frame].draw_command_buffer, RDD::PIPELINE_STAGE_TRANSFER_BIT, dst_stages, {}, bb, {});
  464. }
  465. #endif
  466. return err;
  467. }
  468. Error RenderingDevice::buffer_clear(RID p_buffer, uint32_t p_offset, uint32_t p_size, BitField<BarrierMask> p_post_barrier) {
  469. _THREAD_SAFE_METHOD_
  470. ERR_FAIL_COND_V_MSG((p_size % 4) != 0, ERR_INVALID_PARAMETER,
  471. "Size must be a multiple of four");
  472. ERR_FAIL_COND_V_MSG(draw_list, ERR_INVALID_PARAMETER,
  473. "Updating buffers in is forbidden during creation of a draw list");
  474. ERR_FAIL_COND_V_MSG(compute_list, ERR_INVALID_PARAMETER,
  475. "Updating buffers is forbidden during creation of a compute list");
  476. BitField<RDD::PipelineStageBits> dst_stages;
  477. BitField<RDD::BarrierAccessBits> dst_access;
  478. if (p_post_barrier.has_flag(BARRIER_MASK_TRANSFER)) {
  479. // Protect subsequent updates.
  480. dst_stages.set_flag(RDD::PIPELINE_STAGE_TRANSFER_BIT);
  481. dst_access.set_flag(RDD::BARRIER_ACCESS_TRANSFER_WRITE_BIT);
  482. }
  483. Buffer *buffer = _get_buffer_from_owner(p_buffer, dst_stages, dst_access, p_post_barrier);
  484. if (!buffer) {
  485. ERR_FAIL_V_MSG(ERR_INVALID_PARAMETER, "Buffer argument is not a valid buffer of any type.");
  486. }
  487. ERR_FAIL_COND_V_MSG(p_offset + p_size > buffer->size, ERR_INVALID_PARAMETER,
  488. "Attempted to write buffer (" + itos((p_offset + p_size) - buffer->size) + " bytes) past the end.");
  489. driver->command_clear_buffer(frames[frame].draw_command_buffer, buffer->driver_id, p_offset, p_size);
  490. #ifdef FORCE_FULL_BARRIER
  491. _full_barrier(true);
  492. #else
  493. if (dst_stages.is_empty()) {
  494. dst_stages.set_flag(RDD::PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT);
  495. }
  496. if (driver->api_trait_get(RDD::API_TRAIT_HONORS_PIPELINE_BARRIERS)) {
  497. RDD::BufferBarrier bb;
  498. bb.buffer = buffer->driver_id;
  499. bb.src_access = RDD::BARRIER_ACCESS_TRANSFER_WRITE_BIT;
  500. bb.dst_access = dst_access;
  501. bb.offset = p_offset;
  502. bb.size = p_size;
  503. driver->command_pipeline_barrier(frames[frame].draw_command_buffer, RDD::PIPELINE_STAGE_TRANSFER_BIT, dst_stages, {}, bb, {});
  504. }
  505. #endif
  506. return OK;
  507. }
  508. Vector<uint8_t> RenderingDevice::buffer_get_data(RID p_buffer, uint32_t p_offset, uint32_t p_size) {
  509. _THREAD_SAFE_METHOD_
  510. // It could be this buffer was just created.
  511. BitField<RDD::PipelineStageBits> src_stages = RDD::PIPELINE_STAGE_TRANSFER_BIT;
  512. BitField<RDD::BarrierAccessBits> src_access = RDD::BARRIER_ACCESS_TRANSFER_WRITE_BIT;
  513. // Get the vulkan buffer and the potential stage/access possible.
  514. Buffer *buffer = _get_buffer_from_owner(p_buffer, src_stages, src_access, BARRIER_MASK_ALL_BARRIERS);
  515. if (!buffer) {
  516. ERR_FAIL_V_MSG(Vector<uint8_t>(), "Buffer is either invalid or this type of buffer can't be retrieved. Only Index and Vertex buffers allow retrieving.");
  517. }
  518. if (driver->api_trait_get(RDD::API_TRAIT_HONORS_PIPELINE_BARRIERS)) {
  519. // Make sure no one is using the buffer -- the "true" gets us to the same command buffer as below.
  520. RDD::BufferBarrier bb;
  521. bb.buffer = buffer->driver_id;
  522. bb.src_access = src_access;
  523. bb.dst_access = RDD::BARRIER_ACCESS_TRANSFER_READ_BIT;
  524. bb.size = buffer->size;
  525. driver->command_pipeline_barrier(frames[frame].draw_command_buffer, src_stages, RDD::PIPELINE_STAGE_TRANSFER_BIT, {}, bb, {});
  526. }
  527. // Size of buffer to retrieve.
  528. if (!p_size) {
  529. p_size = buffer->size;
  530. } else {
  531. ERR_FAIL_COND_V_MSG(p_size + p_offset > buffer->size, Vector<uint8_t>(),
  532. "Size is larger than the buffer.");
  533. }
  534. RDD::BufferID tmp_buffer = driver->buffer_create(buffer->size, RDD::BUFFER_USAGE_TRANSFER_TO_BIT, RDD::MEMORY_ALLOCATION_TYPE_CPU);
  535. ERR_FAIL_COND_V(!tmp_buffer, Vector<uint8_t>());
  536. RDD::BufferCopyRegion region;
  537. region.src_offset = p_offset;
  538. region.size = p_size;
  539. driver->command_copy_buffer(frames[frame].draw_command_buffer, buffer->driver_id, tmp_buffer, region);
  540. // Flush everything so memory can be safely mapped.
  541. _flush(true);
  542. uint8_t *buffer_mem = driver->buffer_map(tmp_buffer);
  543. ERR_FAIL_COND_V(!buffer_mem, Vector<uint8_t>());
  544. Vector<uint8_t> buffer_data;
  545. {
  546. buffer_data.resize(p_size);
  547. uint8_t *w = buffer_data.ptrw();
  548. memcpy(w, buffer_mem, p_size);
  549. }
  550. driver->buffer_unmap(tmp_buffer);
  551. driver->buffer_free(tmp_buffer);
  552. return buffer_data;
  553. }
  554. RID RenderingDevice::storage_buffer_create(uint32_t p_size_bytes, const Vector<uint8_t> &p_data, BitField<StorageBufferUsage> p_usage) {
  555. _THREAD_SAFE_METHOD_
  556. ERR_FAIL_COND_V(p_data.size() && (uint32_t)p_data.size() != p_size_bytes, RID());
  557. Buffer buffer;
  558. buffer.size = p_size_bytes;
  559. buffer.usage = (RDD::BUFFER_USAGE_TRANSFER_FROM_BIT | RDD::BUFFER_USAGE_TRANSFER_TO_BIT | RDD::BUFFER_USAGE_STORAGE_BIT);
  560. if (p_usage.has_flag(STORAGE_BUFFER_USAGE_DISPATCH_INDIRECT)) {
  561. buffer.usage.set_flag(RDD::BUFFER_USAGE_INDIRECT_BIT);
  562. }
  563. buffer.driver_id = driver->buffer_create(buffer.size, buffer.usage, RDD::MEMORY_ALLOCATION_TYPE_GPU);
  564. ERR_FAIL_COND_V(!buffer.driver_id, RID());
  565. if (p_data.size()) {
  566. uint64_t data_size = p_data.size();
  567. const uint8_t *r = p_data.ptr();
  568. _buffer_update(&buffer, 0, r, data_size);
  569. if (driver->api_trait_get(RDD::API_TRAIT_HONORS_PIPELINE_BARRIERS)) {
  570. RDD::BufferBarrier bb;
  571. bb.buffer = buffer.driver_id;
  572. bb.src_access = RDD::BARRIER_ACCESS_TRANSFER_WRITE_BIT;
  573. bb.dst_access = (RDD::BARRIER_ACCESS_SHADER_READ_BIT | RDD::BARRIER_ACCESS_SHADER_WRITE_BIT);
  574. bb.size = data_size;
  575. driver->command_pipeline_barrier(frames[frame].setup_command_buffer, RDD::PIPELINE_STAGE_TRANSFER_BIT, RDD::PIPELINE_STAGE_VERTEX_SHADER_BIT | RDD::PIPELINE_STAGE_FRAGMENT_SHADER_BIT | RDD::PIPELINE_STAGE_COMPUTE_SHADER_BIT, {}, bb, {});
  576. }
  577. }
  578. buffer_memory += buffer.size;
  579. return storage_buffer_owner.make_rid(buffer);
  580. }
  581. RID RenderingDevice::texture_buffer_create(uint32_t p_size_elements, DataFormat p_format, const Vector<uint8_t> &p_data) {
  582. _THREAD_SAFE_METHOD_
  583. uint32_t element_size = get_format_vertex_size(p_format);
  584. ERR_FAIL_COND_V_MSG(element_size == 0, RID(), "Format requested is not supported for texture buffers");
  585. uint64_t size_bytes = uint64_t(element_size) * p_size_elements;
  586. ERR_FAIL_COND_V(p_data.size() && (uint32_t)p_data.size() != size_bytes, RID());
  587. Buffer texture_buffer;
  588. texture_buffer.size = size_bytes;
  589. BitField<RDD::BufferUsageBits> usage = (RDD::BUFFER_USAGE_TRANSFER_FROM_BIT | RDD::BUFFER_USAGE_TRANSFER_TO_BIT | RDD::BUFFER_USAGE_TEXEL_BIT);
  590. texture_buffer.driver_id = driver->buffer_create(size_bytes, usage, RDD::MEMORY_ALLOCATION_TYPE_GPU);
  591. ERR_FAIL_COND_V(!texture_buffer.driver_id, RID());
  592. bool ok = driver->buffer_set_texel_format(texture_buffer.driver_id, p_format);
  593. if (!ok) {
  594. driver->buffer_free(texture_buffer.driver_id);
  595. ERR_FAIL_V(RID());
  596. }
  597. if (p_data.size()) {
  598. _buffer_update(&texture_buffer, 0, p_data.ptr(), p_data.size());
  599. if (driver->api_trait_get(RDD::API_TRAIT_HONORS_PIPELINE_BARRIERS)) {
  600. RDD::BufferBarrier bb;
  601. bb.buffer = texture_buffer.driver_id;
  602. bb.src_access = RDD::BARRIER_ACCESS_TRANSFER_WRITE_BIT;
  603. bb.dst_access = RDD::BARRIER_ACCESS_SHADER_READ_BIT;
  604. bb.size = size_bytes;
  605. driver->command_pipeline_barrier(frames[frame].setup_command_buffer, RDD::PIPELINE_STAGE_TRANSFER_BIT, (RDD::PIPELINE_STAGE_VERTEX_SHADER_BIT | RDD::PIPELINE_STAGE_FRAGMENT_SHADER_BIT | RDD::PIPELINE_STAGE_COMPUTE_SHADER_BIT), {}, bb, {});
  606. }
  607. }
  608. buffer_memory += size_bytes;
  609. RID id = texture_buffer_owner.make_rid(texture_buffer);
  610. #ifdef DEV_ENABLED
  611. set_resource_name(id, "RID:" + itos(id.get_id()));
  612. #endif
  613. return id;
  614. }
  615. /*****************/
  616. /**** TEXTURE ****/
  617. /*****************/
  618. RID RenderingDevice::texture_create(const TextureFormat &p_format, const TextureView &p_view, const Vector<Vector<uint8_t>> &p_data) {
  619. _THREAD_SAFE_METHOD_
  620. // Some adjustments will happen.
  621. TextureFormat format = p_format;
  622. if (format.shareable_formats.size()) {
  623. ERR_FAIL_COND_V_MSG(format.shareable_formats.find(format.format) == -1, RID(),
  624. "If supplied a list of shareable formats, the current format must be present in the list");
  625. ERR_FAIL_COND_V_MSG(p_view.format_override != DATA_FORMAT_MAX && format.shareable_formats.find(p_view.format_override) == -1, RID(),
  626. "If supplied a list of shareable formats, the current view format override must be present in the list");
  627. }
  628. ERR_FAIL_INDEX_V(format.texture_type, RDD::TEXTURE_TYPE_MAX, RID());
  629. ERR_FAIL_COND_V_MSG(format.width < 1, RID(), "Width must be equal or greater than 1 for all textures");
  630. if (format.texture_type != TEXTURE_TYPE_1D && format.texture_type != TEXTURE_TYPE_1D_ARRAY) {
  631. ERR_FAIL_COND_V_MSG(format.height < 1, RID(), "Height must be equal or greater than 1 for 2D and 3D textures");
  632. }
  633. if (format.texture_type == TEXTURE_TYPE_3D) {
  634. ERR_FAIL_COND_V_MSG(format.depth < 1, RID(), "Depth must be equal or greater than 1 for 3D textures");
  635. }
  636. ERR_FAIL_COND_V(format.mipmaps < 1, RID());
  637. if (format.texture_type == TEXTURE_TYPE_1D_ARRAY || format.texture_type == TEXTURE_TYPE_2D_ARRAY || format.texture_type == TEXTURE_TYPE_CUBE_ARRAY || format.texture_type == TEXTURE_TYPE_CUBE) {
  638. ERR_FAIL_COND_V_MSG(format.array_layers < 1, RID(),
  639. "Amount of layers must be equal or greater than 1 for arrays and cubemaps.");
  640. ERR_FAIL_COND_V_MSG((format.texture_type == TEXTURE_TYPE_CUBE_ARRAY || format.texture_type == TEXTURE_TYPE_CUBE) && (format.array_layers % 6) != 0, RID(),
  641. "Cubemap and cubemap array textures must provide a layer number that is multiple of 6");
  642. } else {
  643. format.array_layers = 1;
  644. }
  645. ERR_FAIL_INDEX_V(format.samples, TEXTURE_SAMPLES_MAX, RID());
  646. format.height = format.texture_type != TEXTURE_TYPE_1D && format.texture_type != TEXTURE_TYPE_1D_ARRAY ? format.height : 1;
  647. format.depth = format.texture_type == TEXTURE_TYPE_3D ? format.depth : 1;
  648. uint32_t required_mipmaps = get_image_required_mipmaps(format.width, format.height, format.depth);
  649. ERR_FAIL_COND_V_MSG(required_mipmaps < format.mipmaps, RID(),
  650. "Too many mipmaps requested for texture format and dimensions (" + itos(format.mipmaps) + "), maximum allowed: (" + itos(required_mipmaps) + ").");
  651. if (p_data.size()) {
  652. ERR_FAIL_COND_V_MSG(!(format.usage_bits & TEXTURE_USAGE_CAN_UPDATE_BIT), RID(),
  653. "Texture needs the TEXTURE_USAGE_CAN_UPDATE_BIT usage flag in order to be updated at initialization or later");
  654. ERR_FAIL_COND_V_MSG(p_data.size() != (int)format.array_layers, RID(),
  655. "Default supplied data for image format is of invalid length (" + itos(p_data.size()) + "), should be (" + itos(format.array_layers) + ").");
  656. for (uint32_t i = 0; i < format.array_layers; i++) {
  657. uint32_t required_size = get_image_format_required_size(format.format, format.width, format.height, format.depth, format.mipmaps);
  658. ERR_FAIL_COND_V_MSG((uint32_t)p_data[i].size() != required_size, RID(),
  659. "Data for slice index " + itos(i) + " (mapped to layer " + itos(i) + ") differs in size (supplied: " + itos(p_data[i].size()) + ") than what is required by the format (" + itos(required_size) + ").");
  660. }
  661. }
  662. {
  663. // Validate that this image is supported for the intended use.
  664. bool cpu_readable = (format.usage_bits & RDD::TEXTURE_USAGE_CPU_READ_BIT);
  665. BitField<RDD::TextureUsageBits> supported_usage = driver->texture_get_usages_supported_by_format(format.format, cpu_readable);
  666. String format_text = "'" + String(FORMAT_NAMES[format.format]) + "'";
  667. if ((format.usage_bits & TEXTURE_USAGE_SAMPLING_BIT) && !supported_usage.has_flag(TEXTURE_USAGE_SAMPLING_BIT)) {
  668. ERR_FAIL_V_MSG(RID(), "Format " + format_text + " does not support usage as sampling texture.");
  669. }
  670. if ((format.usage_bits & TEXTURE_USAGE_COLOR_ATTACHMENT_BIT) && !supported_usage.has_flag(TEXTURE_USAGE_COLOR_ATTACHMENT_BIT)) {
  671. ERR_FAIL_V_MSG(RID(), "Format " + format_text + " does not support usage as color attachment.");
  672. }
  673. if ((format.usage_bits & TEXTURE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT) && !supported_usage.has_flag(TEXTURE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT)) {
  674. ERR_FAIL_V_MSG(RID(), "Format " + format_text + " does not support usage as depth-stencil attachment.");
  675. }
  676. if ((format.usage_bits & TEXTURE_USAGE_STORAGE_BIT) && !supported_usage.has_flag(TEXTURE_USAGE_STORAGE_BIT)) {
  677. ERR_FAIL_V_MSG(RID(), "Format " + format_text + " does not support usage as storage image.");
  678. }
  679. if ((format.usage_bits & TEXTURE_USAGE_STORAGE_ATOMIC_BIT) && !supported_usage.has_flag(TEXTURE_USAGE_STORAGE_ATOMIC_BIT)) {
  680. ERR_FAIL_V_MSG(RID(), "Format " + format_text + " does not support usage as atomic storage image.");
  681. }
  682. if ((format.usage_bits & TEXTURE_USAGE_VRS_ATTACHMENT_BIT) && !supported_usage.has_flag(TEXTURE_USAGE_VRS_ATTACHMENT_BIT)) {
  683. ERR_FAIL_V_MSG(RID(), "Format " + format_text + " does not support usage as VRS attachment.");
  684. }
  685. }
  686. // Transfer and validate view info.
  687. RDD::TextureView tv;
  688. if (p_view.format_override == DATA_FORMAT_MAX) {
  689. tv.format = format.format;
  690. } else {
  691. ERR_FAIL_INDEX_V(p_view.format_override, DATA_FORMAT_MAX, RID());
  692. tv.format = p_view.format_override;
  693. }
  694. ERR_FAIL_INDEX_V(p_view.swizzle_r, TEXTURE_SWIZZLE_MAX, RID());
  695. ERR_FAIL_INDEX_V(p_view.swizzle_g, TEXTURE_SWIZZLE_MAX, RID());
  696. ERR_FAIL_INDEX_V(p_view.swizzle_b, TEXTURE_SWIZZLE_MAX, RID());
  697. ERR_FAIL_INDEX_V(p_view.swizzle_a, TEXTURE_SWIZZLE_MAX, RID());
  698. tv.swizzle_r = p_view.swizzle_r;
  699. tv.swizzle_g = p_view.swizzle_g;
  700. tv.swizzle_b = p_view.swizzle_b;
  701. tv.swizzle_a = p_view.swizzle_a;
  702. // Create.
  703. Texture texture;
  704. texture.driver_id = driver->texture_create(format, tv);
  705. ERR_FAIL_COND_V(!texture.driver_id, RID());
  706. texture.type = format.texture_type;
  707. texture.format = format.format;
  708. texture.width = format.width;
  709. texture.height = format.height;
  710. texture.depth = format.depth;
  711. texture.layers = format.array_layers;
  712. texture.mipmaps = format.mipmaps;
  713. texture.base_mipmap = 0;
  714. texture.base_layer = 0;
  715. texture.is_resolve_buffer = format.is_resolve_buffer;
  716. texture.usage_flags = format.usage_bits;
  717. texture.samples = format.samples;
  718. texture.allowed_shared_formats = format.shareable_formats;
  719. // Set base layout based on usage priority.
  720. if ((format.usage_bits & TEXTURE_USAGE_SAMPLING_BIT)) {
  721. // First priority, readable.
  722. texture.layout = RDD::TEXTURE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
  723. } else if ((format.usage_bits & TEXTURE_USAGE_STORAGE_BIT)) {
  724. // Second priority, storage.
  725. texture.layout = RDD::TEXTURE_LAYOUT_GENERAL;
  726. } else if ((format.usage_bits & TEXTURE_USAGE_COLOR_ATTACHMENT_BIT)) {
  727. // Third priority, color or depth.
  728. texture.layout = RDD::TEXTURE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
  729. } else if ((format.usage_bits & TEXTURE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT)) {
  730. texture.layout = RDD::TEXTURE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL;
  731. } else {
  732. texture.layout = RDD::TEXTURE_LAYOUT_GENERAL;
  733. }
  734. if ((format.usage_bits & TEXTURE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT)) {
  735. texture.read_aspect_flags.set_flag(RDD::TEXTURE_ASPECT_DEPTH_BIT);
  736. texture.barrier_aspect_flags.set_flag(RDD::TEXTURE_ASPECT_DEPTH_BIT);
  737. if (format_has_stencil(format.format)) {
  738. texture.barrier_aspect_flags.set_flag(RDD::TEXTURE_ASPECT_STENCIL_BIT);
  739. }
  740. } else {
  741. texture.read_aspect_flags.set_flag(RDD::TEXTURE_ASPECT_COLOR_BIT);
  742. texture.barrier_aspect_flags.set_flag(RDD::TEXTURE_ASPECT_COLOR_BIT);
  743. }
  744. texture.bound = false;
  745. // Barrier to set layout.
  746. if (driver->api_trait_get(RDD::API_TRAIT_HONORS_PIPELINE_BARRIERS)) {
  747. RDD::TextureBarrier tb;
  748. tb.texture = texture.driver_id;
  749. tb.dst_access = RDD::BARRIER_ACCESS_SHADER_READ_BIT;
  750. tb.prev_layout = RDD::TEXTURE_LAYOUT_UNDEFINED;
  751. tb.next_layout = texture.layout;
  752. tb.subresources.aspect = texture.barrier_aspect_flags;
  753. tb.subresources.mipmap_count = format.mipmaps;
  754. tb.subresources.layer_count = format.array_layers;
  755. driver->command_pipeline_barrier(frames[frame].setup_command_buffer, RDD::PIPELINE_STAGE_TOP_OF_PIPE_BIT, RDD::PIPELINE_STAGE_VERTEX_SHADER_BIT | RDD::PIPELINE_STAGE_FRAGMENT_SHADER_BIT | RDD::PIPELINE_STAGE_COMPUTE_SHADER_BIT, {}, {}, tb);
  756. }
  757. texture_memory += driver->texture_get_allocation_size(texture.driver_id);
  758. RID id = texture_owner.make_rid(texture);
  759. #ifdef DEV_ENABLED
  760. set_resource_name(id, "RID:" + itos(id.get_id()));
  761. #endif
  762. if (p_data.size()) {
  763. for (uint32_t i = 0; i < p_format.array_layers; i++) {
  764. _texture_update(id, i, p_data[i], BARRIER_MASK_ALL_BARRIERS, true);
  765. }
  766. }
  767. return id;
  768. }
  769. RID RenderingDevice::texture_create_shared(const TextureView &p_view, RID p_with_texture) {
  770. _THREAD_SAFE_METHOD_
  771. Texture *src_texture = texture_owner.get_or_null(p_with_texture);
  772. ERR_FAIL_COND_V(!src_texture, RID());
  773. if (src_texture->owner.is_valid()) { // Ahh this is a share. The RenderingDeviceDriver needs the actual owner.
  774. p_with_texture = src_texture->owner;
  775. src_texture = texture_owner.get_or_null(src_texture->owner);
  776. ERR_FAIL_COND_V(!src_texture, RID()); // This is a bug.
  777. }
  778. // Create view.
  779. Texture texture = *src_texture;
  780. RDD::TextureView tv;
  781. if (p_view.format_override == DATA_FORMAT_MAX || p_view.format_override == texture.format) {
  782. tv.format = texture.format;
  783. } else {
  784. ERR_FAIL_INDEX_V(p_view.format_override, DATA_FORMAT_MAX, RID());
  785. ERR_FAIL_COND_V_MSG(texture.allowed_shared_formats.find(p_view.format_override) == -1, RID(),
  786. "Format override is not in the list of allowed shareable formats for original texture.");
  787. tv.format = p_view.format_override;
  788. }
  789. tv.swizzle_r = p_view.swizzle_r;
  790. tv.swizzle_g = p_view.swizzle_g;
  791. tv.swizzle_b = p_view.swizzle_b;
  792. tv.swizzle_a = p_view.swizzle_a;
  793. texture.driver_id = driver->texture_create_shared(texture.driver_id, tv);
  794. ERR_FAIL_COND_V(!texture.driver_id, RID());
  795. texture.owner = p_with_texture;
  796. RID id = texture_owner.make_rid(texture);
  797. #ifdef DEV_ENABLED
  798. set_resource_name(id, "RID:" + itos(id.get_id()));
  799. #endif
  800. _add_dependency(id, p_with_texture);
  801. return id;
  802. }
  803. RID RenderingDevice::texture_create_from_extension(TextureType p_type, DataFormat p_format, TextureSamples p_samples, BitField<RenderingDevice::TextureUsageBits> p_usage, uint64_t p_image, uint64_t p_width, uint64_t p_height, uint64_t p_depth, uint64_t p_layers) {
  804. _THREAD_SAFE_METHOD_
  805. // This method creates a texture object using a VkImage created by an extension, module or other external source (OpenXR uses this).
  806. Texture texture;
  807. texture.type = p_type;
  808. texture.format = p_format;
  809. texture.samples = p_samples;
  810. texture.width = p_width;
  811. texture.height = p_height;
  812. texture.depth = p_depth;
  813. texture.layers = p_layers;
  814. texture.mipmaps = 1;
  815. texture.usage_flags = p_usage;
  816. texture.base_mipmap = 0;
  817. texture.base_layer = 0;
  818. texture.allowed_shared_formats.push_back(RD::DATA_FORMAT_R8G8B8A8_UNORM);
  819. texture.allowed_shared_formats.push_back(RD::DATA_FORMAT_R8G8B8A8_SRGB);
  820. // Set base layout based on usage priority.
  821. if (p_usage.has_flag(TEXTURE_USAGE_SAMPLING_BIT)) {
  822. // First priority, readable.
  823. texture.layout = RDD::TEXTURE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
  824. } else if (p_usage.has_flag(TEXTURE_USAGE_STORAGE_BIT)) {
  825. // Second priority, storage.
  826. texture.layout = RDD::TEXTURE_LAYOUT_GENERAL;
  827. } else if (p_usage.has_flag(TEXTURE_USAGE_COLOR_ATTACHMENT_BIT)) {
  828. // Third priority, color or depth.
  829. texture.layout = RDD::TEXTURE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
  830. } else if (p_usage.has_flag(TEXTURE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT)) {
  831. texture.layout = RDD::TEXTURE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL;
  832. } else {
  833. texture.layout = RDD::TEXTURE_LAYOUT_GENERAL;
  834. }
  835. if (p_usage.has_flag(TEXTURE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT)) {
  836. texture.read_aspect_flags.set_flag(RDD::TEXTURE_ASPECT_DEPTH_BIT);
  837. texture.barrier_aspect_flags.set_flag(RDD::TEXTURE_ASPECT_DEPTH_BIT);
  838. /*if (format_has_stencil(p_format.format)) {
  839. texture.barrier_aspect_flags.set_flag(RDD::TEXTURE_ASPECT_STENCIL_BIT);
  840. }*/
  841. } else {
  842. texture.read_aspect_flags.set_flag(RDD::TEXTURE_ASPECT_COLOR_BIT);
  843. texture.barrier_aspect_flags.set_flag(RDD::TEXTURE_ASPECT_COLOR_BIT);
  844. }
  845. texture.driver_id = driver->texture_create_from_extension(p_image, p_type, p_format, p_layers, (texture.usage_flags & TEXTURE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT));
  846. ERR_FAIL_COND_V(!texture.driver_id, RID());
  847. // Barrier to set layout.
  848. if (driver->api_trait_get(RDD::API_TRAIT_HONORS_PIPELINE_BARRIERS)) {
  849. RDD::TextureBarrier tb;
  850. tb.texture = texture.driver_id;
  851. tb.dst_access = RDD::BARRIER_ACCESS_SHADER_READ_BIT;
  852. tb.prev_layout = RDD::TEXTURE_LAYOUT_UNDEFINED;
  853. tb.next_layout = texture.layout;
  854. tb.subresources.aspect = texture.barrier_aspect_flags;
  855. tb.subresources.mipmap_count = texture.mipmaps;
  856. tb.subresources.layer_count = texture.layers;
  857. driver->command_pipeline_barrier(frames[frame].setup_command_buffer, RDD::PIPELINE_STAGE_TOP_OF_PIPE_BIT, RDD::PIPELINE_STAGE_VERTEX_SHADER_BIT, {}, {}, tb);
  858. }
  859. RID id = texture_owner.make_rid(texture);
  860. #ifdef DEV_ENABLED
  861. set_resource_name(id, "RID:" + itos(id.get_id()));
  862. #endif
  863. return id;
  864. }
  865. RID RenderingDevice::texture_create_shared_from_slice(const TextureView &p_view, RID p_with_texture, uint32_t p_layer, uint32_t p_mipmap, uint32_t p_mipmaps, TextureSliceType p_slice_type, uint32_t p_layers) {
  866. _THREAD_SAFE_METHOD_
  867. Texture *src_texture = texture_owner.get_or_null(p_with_texture);
  868. ERR_FAIL_COND_V(!src_texture, RID());
  869. if (src_texture->owner.is_valid()) { // // Ahh this is a share. The RenderingDeviceDriver needs the actual owner.
  870. p_with_texture = src_texture->owner;
  871. src_texture = texture_owner.get_or_null(src_texture->owner);
  872. ERR_FAIL_COND_V(!src_texture, RID()); // This is a bug.
  873. }
  874. ERR_FAIL_COND_V_MSG(p_slice_type == TEXTURE_SLICE_CUBEMAP && (src_texture->type != TEXTURE_TYPE_CUBE && src_texture->type != TEXTURE_TYPE_CUBE_ARRAY), RID(),
  875. "Can only create a cubemap slice from a cubemap or cubemap array mipmap");
  876. ERR_FAIL_COND_V_MSG(p_slice_type == TEXTURE_SLICE_3D && src_texture->type != TEXTURE_TYPE_3D, RID(),
  877. "Can only create a 3D slice from a 3D texture");
  878. ERR_FAIL_COND_V_MSG(p_slice_type == TEXTURE_SLICE_2D_ARRAY && (src_texture->type != TEXTURE_TYPE_2D_ARRAY), RID(),
  879. "Can only create an array slice from a 2D array mipmap");
  880. // Create view.
  881. ERR_FAIL_UNSIGNED_INDEX_V(p_mipmap, src_texture->mipmaps, RID());
  882. ERR_FAIL_COND_V(p_mipmap + p_mipmaps > src_texture->mipmaps, RID());
  883. ERR_FAIL_UNSIGNED_INDEX_V(p_layer, src_texture->layers, RID());
  884. int slice_layers = 1;
  885. if (p_layers != 0) {
  886. ERR_FAIL_COND_V_MSG(p_layers > 1 && p_slice_type != TEXTURE_SLICE_2D_ARRAY, RID(), "layer slicing only supported for 2D arrays");
  887. ERR_FAIL_COND_V_MSG(p_layer + p_layers > src_texture->layers, RID(), "layer slice is out of bounds");
  888. slice_layers = p_layers;
  889. } else if (p_slice_type == TEXTURE_SLICE_2D_ARRAY) {
  890. ERR_FAIL_COND_V_MSG(p_layer != 0, RID(), "layer must be 0 when obtaining a 2D array mipmap slice");
  891. slice_layers = src_texture->layers;
  892. } else if (p_slice_type == TEXTURE_SLICE_CUBEMAP) {
  893. slice_layers = 6;
  894. }
  895. Texture texture = *src_texture;
  896. get_image_format_required_size(texture.format, texture.width, texture.height, texture.depth, p_mipmap + 1, &texture.width, &texture.height);
  897. texture.mipmaps = p_mipmaps;
  898. texture.layers = slice_layers;
  899. texture.base_mipmap = p_mipmap;
  900. texture.base_layer = p_layer;
  901. if (p_slice_type == TEXTURE_SLICE_2D) {
  902. texture.type = TEXTURE_TYPE_2D;
  903. } else if (p_slice_type == TEXTURE_SLICE_3D) {
  904. texture.type = TEXTURE_TYPE_3D;
  905. }
  906. RDD::TextureView tv;
  907. if (p_view.format_override == DATA_FORMAT_MAX || p_view.format_override == texture.format) {
  908. tv.format = texture.format;
  909. } else {
  910. ERR_FAIL_INDEX_V(p_view.format_override, DATA_FORMAT_MAX, RID());
  911. ERR_FAIL_COND_V_MSG(texture.allowed_shared_formats.find(p_view.format_override) == -1, RID(),
  912. "Format override is not in the list of allowed shareable formats for original texture.");
  913. tv.format = p_view.format_override;
  914. }
  915. tv.swizzle_r = p_view.swizzle_r;
  916. tv.swizzle_g = p_view.swizzle_g;
  917. tv.swizzle_b = p_view.swizzle_b;
  918. tv.swizzle_a = p_view.swizzle_a;
  919. if (p_slice_type == TEXTURE_SLICE_CUBEMAP) {
  920. ERR_FAIL_COND_V_MSG(p_layer >= src_texture->layers, RID(),
  921. "Specified layer is invalid for cubemap");
  922. ERR_FAIL_COND_V_MSG((p_layer % 6) != 0, RID(),
  923. "Specified layer must be a multiple of 6.");
  924. }
  925. texture.driver_id = driver->texture_create_shared_from_slice(src_texture->driver_id, tv, p_slice_type, p_layer, slice_layers, p_mipmap, p_mipmaps);
  926. ERR_FAIL_COND_V(!texture.driver_id, RID());
  927. texture.owner = p_with_texture;
  928. RID id = texture_owner.make_rid(texture);
  929. #ifdef DEV_ENABLED
  930. set_resource_name(id, "RID:" + itos(id.get_id()));
  931. #endif
  932. _add_dependency(id, p_with_texture);
  933. return id;
  934. }
  935. Error RenderingDevice::texture_update(RID p_texture, uint32_t p_layer, const Vector<uint8_t> &p_data, BitField<BarrierMask> p_post_barrier) {
  936. return _texture_update(p_texture, p_layer, p_data, p_post_barrier, false);
  937. }
  938. static _ALWAYS_INLINE_ void _copy_region(uint8_t const *__restrict p_src, uint8_t *__restrict p_dst, uint32_t p_src_x, uint32_t p_src_y, uint32_t p_src_w, uint32_t p_src_h, uint32_t p_src_full_w, uint32_t p_dst_pitch, uint32_t p_unit_size) {
  939. uint32_t src_offset = (p_src_y * p_src_full_w + p_src_x) * p_unit_size;
  940. uint32_t dst_offset = 0;
  941. for (uint32_t y = p_src_h; y > 0; y--) {
  942. uint8_t const *__restrict src = p_src + src_offset;
  943. uint8_t *__restrict dst = p_dst + dst_offset;
  944. for (uint32_t x = p_src_w * p_unit_size; x > 0; x--) {
  945. *dst = *src;
  946. src++;
  947. dst++;
  948. }
  949. src_offset += p_src_full_w * p_unit_size;
  950. dst_offset += p_dst_pitch;
  951. }
  952. }
  953. Error RenderingDevice::_texture_update(RID p_texture, uint32_t p_layer, const Vector<uint8_t> &p_data, BitField<BarrierMask> p_post_barrier, bool p_use_setup_queue) {
  954. _THREAD_SAFE_METHOD_
  955. ERR_FAIL_COND_V_MSG((draw_list || compute_list) && !p_use_setup_queue, ERR_INVALID_PARAMETER,
  956. "Updating textures is forbidden during creation of a draw or compute list");
  957. Texture *texture = texture_owner.get_or_null(p_texture);
  958. ERR_FAIL_NULL_V(texture, ERR_INVALID_PARAMETER);
  959. if (texture->owner != RID()) {
  960. p_texture = texture->owner;
  961. texture = texture_owner.get_or_null(texture->owner);
  962. ERR_FAIL_NULL_V(texture, ERR_BUG); // This is a bug.
  963. }
  964. ERR_FAIL_COND_V_MSG(texture->bound, ERR_CANT_ACQUIRE_RESOURCE,
  965. "Texture can't be updated while a draw list that uses it as part of a framebuffer is being created. Ensure the draw list is finalized (and that the color/depth texture using it is not set to `RenderingDevice.FINAL_ACTION_CONTINUE`) to update this texture.");
  966. ERR_FAIL_COND_V_MSG(!(texture->usage_flags & TEXTURE_USAGE_CAN_UPDATE_BIT), ERR_INVALID_PARAMETER,
  967. "Texture requires the `RenderingDevice.TEXTURE_USAGE_CAN_UPDATE_BIT` to be set to be updatable.");
  968. uint32_t layer_count = texture->layers;
  969. if (texture->type == TEXTURE_TYPE_CUBE || texture->type == TEXTURE_TYPE_CUBE_ARRAY) {
  970. layer_count *= 6;
  971. }
  972. ERR_FAIL_COND_V(p_layer >= layer_count, ERR_INVALID_PARAMETER);
  973. uint32_t width, height;
  974. uint32_t tight_mip_size = get_image_format_required_size(texture->format, texture->width, texture->height, texture->depth, texture->mipmaps, &width, &height);
  975. uint32_t required_size = tight_mip_size;
  976. uint32_t required_align = get_compressed_image_format_block_byte_size(texture->format);
  977. if (required_align == 1) {
  978. required_align = get_image_format_pixel_size(texture->format);
  979. }
  980. required_align = STEPIFY(required_align, driver->api_trait_get(RDD::API_TRAIT_TEXTURE_TRANSFER_ALIGNMENT));
  981. ERR_FAIL_COND_V_MSG(required_size != (uint32_t)p_data.size(), ERR_INVALID_PARAMETER,
  982. "Required size for texture update (" + itos(required_size) + ") does not match data supplied size (" + itos(p_data.size()) + ").");
  983. uint32_t region_size = texture_upload_region_size_px;
  984. const uint8_t *r = p_data.ptr();
  985. RDD::CommandBufferID command_buffer = p_use_setup_queue ? frames[frame].setup_command_buffer : frames[frame].draw_command_buffer;
  986. // Barrier to transfer.
  987. if (driver->api_trait_get(RDD::API_TRAIT_HONORS_PIPELINE_BARRIERS)) {
  988. RDD::TextureBarrier tb;
  989. tb.texture = texture->driver_id;
  990. tb.dst_access = RDD::BARRIER_ACCESS_TRANSFER_WRITE_BIT;
  991. tb.prev_layout = texture->layout;
  992. tb.next_layout = RDD::TEXTURE_LAYOUT_TRANSFER_DST_OPTIMAL;
  993. tb.subresources.aspect = texture->barrier_aspect_flags;
  994. tb.subresources.mipmap_count = texture->mipmaps;
  995. tb.subresources.base_layer = p_layer;
  996. tb.subresources.layer_count = 1;
  997. driver->command_pipeline_barrier(command_buffer, RDD::PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT, RDD::PIPELINE_STAGE_TRANSFER_BIT, {}, {}, tb);
  998. }
  999. uint32_t mipmap_offset = 0;
  1000. uint32_t logic_width = texture->width;
  1001. uint32_t logic_height = texture->height;
  1002. for (uint32_t mm_i = 0; mm_i < texture->mipmaps; mm_i++) {
  1003. uint32_t depth = 0;
  1004. uint32_t image_total = get_image_format_required_size(texture->format, texture->width, texture->height, texture->depth, mm_i + 1, &width, &height, &depth);
  1005. const uint8_t *read_ptr_mipmap = r + mipmap_offset;
  1006. tight_mip_size = image_total - mipmap_offset;
  1007. for (uint32_t z = 0; z < depth; z++) { // For 3D textures, depth may be > 0.
  1008. const uint8_t *read_ptr = read_ptr_mipmap + (tight_mip_size / depth) * z;
  1009. for (uint32_t y = 0; y < height; y += region_size) {
  1010. for (uint32_t x = 0; x < width; x += region_size) {
  1011. uint32_t region_w = MIN(region_size, width - x);
  1012. uint32_t region_h = MIN(region_size, height - y);
  1013. uint32_t region_logic_w = MIN(region_size, logic_width - x);
  1014. uint32_t region_logic_h = MIN(region_size, logic_height - y);
  1015. uint32_t pixel_size = get_image_format_pixel_size(texture->format);
  1016. uint32_t block_w = 0, block_h = 0;
  1017. get_compressed_image_format_block_dimensions(texture->format, block_w, block_h);
  1018. uint32_t region_pitch = (region_w * pixel_size * block_w) >> get_compressed_image_format_pixel_rshift(texture->format);
  1019. uint32_t pitch_step = driver->api_trait_get(RDD::API_TRAIT_TEXTURE_DATA_ROW_PITCH_STEP);
  1020. region_pitch = STEPIFY(region_pitch, pitch_step);
  1021. uint32_t to_allocate = region_pitch * region_h;
  1022. uint32_t alloc_offset = 0, alloc_size = 0;
  1023. Error err = _staging_buffer_allocate(to_allocate, required_align, alloc_offset, alloc_size, false);
  1024. ERR_FAIL_COND_V(err, ERR_CANT_CREATE);
  1025. uint8_t *write_ptr = nullptr;
  1026. { // Map.
  1027. uint8_t *data_ptr = driver->buffer_map(staging_buffer_blocks[staging_buffer_current].driver_id);
  1028. ERR_FAIL_NULL_V(data_ptr, ERR_CANT_CREATE);
  1029. write_ptr = data_ptr;
  1030. write_ptr += alloc_offset;
  1031. }
  1032. ERR_FAIL_COND_V(region_w % block_w, ERR_BUG);
  1033. ERR_FAIL_COND_V(region_h % block_h, ERR_BUG);
  1034. if (block_w != 1 || block_h != 1) {
  1035. // Compressed image (blocks).
  1036. // Must copy a block region.
  1037. uint32_t block_size = get_compressed_image_format_block_byte_size(texture->format);
  1038. // Re-create current variables in blocky format.
  1039. uint32_t xb = x / block_w;
  1040. uint32_t yb = y / block_h;
  1041. uint32_t wb = width / block_w;
  1042. //uint32_t hb = height / block_h;
  1043. uint32_t region_wb = region_w / block_w;
  1044. uint32_t region_hb = region_h / block_h;
  1045. _copy_region(read_ptr, write_ptr, xb, yb, region_wb, region_hb, wb, region_pitch, block_size);
  1046. } else {
  1047. // Regular image (pixels).
  1048. // Must copy a pixel region.
  1049. _copy_region(read_ptr, write_ptr, x, y, region_w, region_h, width, region_pitch, pixel_size);
  1050. }
  1051. { // Unmap.
  1052. driver->buffer_unmap(staging_buffer_blocks[staging_buffer_current].driver_id);
  1053. }
  1054. RDD::BufferTextureCopyRegion copy_region;
  1055. copy_region.buffer_offset = alloc_offset;
  1056. copy_region.texture_subresources.aspect = texture->read_aspect_flags;
  1057. copy_region.texture_subresources.mipmap = mm_i;
  1058. copy_region.texture_subresources.base_layer = p_layer;
  1059. copy_region.texture_subresources.layer_count = 1;
  1060. copy_region.texture_offset = Vector3i(x, y, z);
  1061. copy_region.texture_region_size = Vector3i(region_logic_w, region_logic_h, 1);
  1062. driver->command_copy_buffer_to_texture(command_buffer, staging_buffer_blocks[staging_buffer_current].driver_id, texture->driver_id, RDD::TEXTURE_LAYOUT_TRANSFER_DST_OPTIMAL, copy_region);
  1063. staging_buffer_blocks.write[staging_buffer_current].fill_amount = alloc_offset + alloc_size;
  1064. }
  1065. }
  1066. }
  1067. mipmap_offset = image_total;
  1068. logic_width = MAX(1u, logic_width >> 1);
  1069. logic_height = MAX(1u, logic_height >> 1);
  1070. }
  1071. // Barrier to restore layout.
  1072. if (driver->api_trait_get(RDD::API_TRAIT_HONORS_PIPELINE_BARRIERS)) {
  1073. BitField<RDD::PipelineStageBits> stages;
  1074. BitField<RDD::BarrierAccessBits> access;
  1075. if (p_post_barrier.has_flag(BARRIER_MASK_COMPUTE)) {
  1076. stages.set_flag(RDD::PIPELINE_STAGE_COMPUTE_SHADER_BIT);
  1077. access.set_flag(RDD::BARRIER_ACCESS_SHADER_READ_BIT).set_flag(RDD::BARRIER_ACCESS_SHADER_WRITE_BIT);
  1078. }
  1079. if (p_post_barrier.has_flag(BARRIER_MASK_VERTEX)) {
  1080. stages.set_flag(RDD::PIPELINE_STAGE_VERTEX_SHADER_BIT);
  1081. access.set_flag(RDD::BARRIER_ACCESS_SHADER_READ_BIT).set_flag(RDD::BARRIER_ACCESS_SHADER_WRITE_BIT);
  1082. }
  1083. if (p_post_barrier.has_flag(BARRIER_MASK_FRAGMENT)) {
  1084. stages.set_flag(RDD::PIPELINE_STAGE_FRAGMENT_SHADER_BIT);
  1085. access.set_flag(RDD::BARRIER_ACCESS_SHADER_READ_BIT).set_flag(RDD::BARRIER_ACCESS_SHADER_WRITE_BIT);
  1086. }
  1087. if (p_post_barrier.has_flag(BARRIER_MASK_TRANSFER)) {
  1088. stages.set_flag(RDD::PIPELINE_STAGE_TRANSFER_BIT);
  1089. access.set_flag(RDD::BARRIER_ACCESS_TRANSFER_WRITE_BIT);
  1090. }
  1091. if (stages.is_empty()) {
  1092. stages.set_flag(RDD::PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT);
  1093. }
  1094. RDD::TextureBarrier tb;
  1095. tb.texture = texture->driver_id;
  1096. tb.src_access = RDD::BARRIER_ACCESS_TRANSFER_WRITE_BIT;
  1097. tb.dst_access = access;
  1098. tb.prev_layout = RDD::TEXTURE_LAYOUT_TRANSFER_DST_OPTIMAL;
  1099. tb.next_layout = texture->layout;
  1100. tb.subresources.aspect = texture->barrier_aspect_flags;
  1101. tb.subresources.mipmap_count = texture->mipmaps;
  1102. tb.subresources.base_layer = p_layer;
  1103. tb.subresources.layer_count = 1;
  1104. driver->command_pipeline_barrier(command_buffer, RDD::PIPELINE_STAGE_TRANSFER_BIT, stages, {}, {}, tb);
  1105. if (texture->used_in_frame != frames_drawn) {
  1106. texture->used_in_raster = false;
  1107. texture->used_in_compute = false;
  1108. texture->used_in_frame = frames_drawn;
  1109. }
  1110. texture->used_in_transfer = true;
  1111. }
  1112. return OK;
  1113. }
  1114. Vector<uint8_t> RenderingDevice::_texture_get_data(Texture *tex, uint32_t p_layer, bool p_2d) {
  1115. uint32_t width, height, depth;
  1116. uint32_t tight_mip_size = get_image_format_required_size(tex->format, tex->width, tex->height, p_2d ? 1 : tex->depth, tex->mipmaps, &width, &height, &depth);
  1117. Vector<uint8_t> image_data;
  1118. image_data.resize(tight_mip_size);
  1119. uint32_t blockw, blockh;
  1120. get_compressed_image_format_block_dimensions(tex->format, blockw, blockh);
  1121. uint32_t block_size = get_compressed_image_format_block_byte_size(tex->format);
  1122. uint32_t pixel_size = get_image_format_pixel_size(tex->format);
  1123. {
  1124. uint8_t *w = image_data.ptrw();
  1125. uint32_t mipmap_offset = 0;
  1126. for (uint32_t mm_i = 0; mm_i < tex->mipmaps; mm_i++) {
  1127. uint32_t image_total = get_image_format_required_size(tex->format, tex->width, tex->height, p_2d ? 1 : tex->depth, mm_i + 1, &width, &height, &depth);
  1128. uint8_t *write_ptr_mipmap = w + mipmap_offset;
  1129. tight_mip_size = image_total - mipmap_offset;
  1130. RDD::TextureSubresource subres;
  1131. subres.aspect = RDD::TEXTURE_ASPECT_COLOR;
  1132. subres.layer = p_layer;
  1133. subres.mipmap = mm_i;
  1134. RDD::TextureCopyableLayout layout;
  1135. driver->texture_get_copyable_layout(tex->driver_id, subres, &layout);
  1136. uint8_t *img_mem = driver->texture_map(tex->driver_id, subres);
  1137. ERR_FAIL_NULL_V(img_mem, Vector<uint8_t>());
  1138. for (uint32_t z = 0; z < depth; z++) {
  1139. uint8_t *write_ptr = write_ptr_mipmap + z * tight_mip_size / depth;
  1140. const uint8_t *slice_read_ptr = img_mem + z * layout.depth_pitch;
  1141. if (block_size > 1) {
  1142. // Compressed.
  1143. uint32_t line_width = (block_size * (width / blockw));
  1144. for (uint32_t y = 0; y < height / blockh; y++) {
  1145. const uint8_t *rptr = slice_read_ptr + y * layout.row_pitch;
  1146. uint8_t *wptr = write_ptr + y * line_width;
  1147. memcpy(wptr, rptr, line_width);
  1148. }
  1149. } else {
  1150. // Uncompressed.
  1151. for (uint32_t y = 0; y < height; y++) {
  1152. const uint8_t *rptr = slice_read_ptr + y * layout.row_pitch;
  1153. uint8_t *wptr = write_ptr + y * pixel_size * width;
  1154. memcpy(wptr, rptr, (uint64_t)pixel_size * width);
  1155. }
  1156. }
  1157. }
  1158. driver->texture_unmap(tex->driver_id);
  1159. mipmap_offset = image_total;
  1160. }
  1161. }
  1162. return image_data;
  1163. }
  1164. Vector<uint8_t> RenderingDevice::texture_get_data(RID p_texture, uint32_t p_layer) {
  1165. _THREAD_SAFE_METHOD_
  1166. Texture *tex = texture_owner.get_or_null(p_texture);
  1167. ERR_FAIL_COND_V(!tex, Vector<uint8_t>());
  1168. ERR_FAIL_COND_V_MSG(tex->bound, Vector<uint8_t>(),
  1169. "Texture can't be retrieved while a draw list that uses it as part of a framebuffer is being created. Ensure the draw list is finalized (and that the color/depth texture using it is not set to `RenderingDevice.FINAL_ACTION_CONTINUE`) to retrieve this texture.");
  1170. ERR_FAIL_COND_V_MSG(!(tex->usage_flags & TEXTURE_USAGE_CAN_COPY_FROM_BIT), Vector<uint8_t>(),
  1171. "Texture requires the `RenderingDevice.TEXTURE_USAGE_CAN_COPY_FROM_BIT` to be set to be retrieved.");
  1172. uint32_t layer_count = tex->layers;
  1173. if (tex->type == TEXTURE_TYPE_CUBE || tex->type == TEXTURE_TYPE_CUBE_ARRAY) {
  1174. layer_count *= 6;
  1175. }
  1176. ERR_FAIL_COND_V(p_layer >= layer_count, Vector<uint8_t>());
  1177. if ((tex->usage_flags & TEXTURE_USAGE_CPU_READ_BIT)) {
  1178. // Does not need anything fancy, map and read.
  1179. return _texture_get_data(tex, p_layer);
  1180. } else {
  1181. LocalVector<RDD::TextureCopyableLayout> mip_layouts;
  1182. uint32_t work_mip_alignment = driver->api_trait_get(RDD::API_TRAIT_TEXTURE_TRANSFER_ALIGNMENT);
  1183. uint32_t work_buffer_size = 0;
  1184. mip_layouts.resize(tex->mipmaps);
  1185. for (uint32_t i = 0; i < tex->mipmaps; i++) {
  1186. RDD::TextureSubresource subres;
  1187. subres.aspect = RDD::TEXTURE_ASPECT_COLOR;
  1188. subres.layer = p_layer;
  1189. subres.mipmap = i;
  1190. driver->texture_get_copyable_layout(tex->driver_id, subres, &mip_layouts[i]);
  1191. // Assuming layers are tightly packed. If this is not true on some driver, we must modify the copy algorithm.
  1192. DEV_ASSERT(mip_layouts[i].layer_pitch == mip_layouts[i].size / layer_count);
  1193. work_buffer_size = STEPIFY(work_buffer_size, work_mip_alignment) + mip_layouts[i].size;
  1194. }
  1195. RDD::BufferID tmp_buffer = driver->buffer_create(work_buffer_size, RDD::BUFFER_USAGE_TRANSFER_TO_BIT, RDD::MEMORY_ALLOCATION_TYPE_CPU);
  1196. ERR_FAIL_COND_V(!tmp_buffer, Vector<uint8_t>());
  1197. RDD::CommandBufferID command_buffer = frames[frame].draw_command_buffer; // Makes more sense to retrieve.
  1198. // Pre-copy barrier.
  1199. if (driver->api_trait_get(RDD::API_TRAIT_HONORS_PIPELINE_BARRIERS)) {
  1200. RDD::TextureBarrier tb;
  1201. tb.texture = tex->driver_id;
  1202. tb.dst_access = RDD::BARRIER_ACCESS_TRANSFER_READ_BIT;
  1203. tb.prev_layout = tex->layout;
  1204. tb.next_layout = RDD::TEXTURE_LAYOUT_TRANSFER_SRC_OPTIMAL;
  1205. tb.subresources.aspect = tex->barrier_aspect_flags;
  1206. tb.subresources.mipmap_count = tex->mipmaps;
  1207. tb.subresources.base_layer = p_layer;
  1208. tb.subresources.layer_count = 1;
  1209. driver->command_pipeline_barrier(command_buffer, RDD::PIPELINE_STAGE_TOP_OF_PIPE_BIT, RDD::PIPELINE_STAGE_TRANSFER_BIT, {}, {}, tb);
  1210. }
  1211. {
  1212. uint32_t w = tex->width;
  1213. uint32_t h = tex->height;
  1214. uint32_t d = tex->depth;
  1215. for (uint32_t i = 0; i < tex->mipmaps; i++) {
  1216. RDD::BufferTextureCopyRegion copy_region;
  1217. copy_region.buffer_offset = mip_layouts[i].offset;
  1218. copy_region.texture_subresources.aspect = tex->read_aspect_flags;
  1219. copy_region.texture_subresources.mipmap = i;
  1220. copy_region.texture_subresources.base_layer = p_layer;
  1221. copy_region.texture_subresources.layer_count = 1;
  1222. copy_region.texture_region_size.x = w;
  1223. copy_region.texture_region_size.y = h;
  1224. copy_region.texture_region_size.z = d;
  1225. driver->command_copy_texture_to_buffer(command_buffer, tex->driver_id, RDD::TEXTURE_LAYOUT_TRANSFER_SRC_OPTIMAL, tmp_buffer, copy_region);
  1226. w = MAX(1u, w >> 1);
  1227. h = MAX(1u, h >> 1);
  1228. d = MAX(1u, d >> 1);
  1229. }
  1230. }
  1231. // Post-copy barrier.
  1232. if (driver->api_trait_get(RDD::API_TRAIT_HONORS_PIPELINE_BARRIERS)) {
  1233. RDD::TextureBarrier tb;
  1234. tb.texture = tex->driver_id;
  1235. tb.src_access = RDD::BARRIER_ACCESS_TRANSFER_READ_BIT;
  1236. tb.dst_access = RDD::BARRIER_ACCESS_SHADER_READ_BIT;
  1237. if ((tex->usage_flags & TEXTURE_USAGE_STORAGE_BIT)) {
  1238. tb.dst_access.set_flag(RDD::BARRIER_ACCESS_SHADER_WRITE_BIT);
  1239. }
  1240. tb.prev_layout = RDD::TEXTURE_LAYOUT_TRANSFER_SRC_OPTIMAL;
  1241. tb.next_layout = tex->layout;
  1242. tb.subresources.aspect = tex->barrier_aspect_flags;
  1243. tb.subresources.mipmap_count = tex->mipmaps;
  1244. tb.subresources.base_layer = p_layer;
  1245. tb.subresources.layer_count = 1;
  1246. driver->command_pipeline_barrier(command_buffer, RDD::PIPELINE_STAGE_TRANSFER_BIT, RDD::PIPELINE_STAGE_VERTEX_SHADER_BIT | RDD::PIPELINE_STAGE_FRAGMENT_SHADER_BIT | RDD::PIPELINE_STAGE_COMPUTE_SHADER_BIT, {}, {}, tb);
  1247. }
  1248. _flush(true);
  1249. const uint8_t *read_ptr = driver->buffer_map(tmp_buffer);
  1250. ERR_FAIL_NULL_V(read_ptr, Vector<uint8_t>());
  1251. Vector<uint8_t> buffer_data;
  1252. {
  1253. uint32_t tight_buffer_size = get_image_format_required_size(tex->format, tex->width, tex->height, tex->depth, tex->mipmaps);
  1254. buffer_data.resize(tight_buffer_size);
  1255. uint8_t *write_ptr = buffer_data.ptrw();
  1256. uint32_t w = tex->width;
  1257. uint32_t h = tex->height;
  1258. uint32_t d = tex->depth;
  1259. for (uint32_t i = 0; i < tex->mipmaps; i++) {
  1260. uint32_t width = 0, height = 0, depth = 0;
  1261. uint32_t tight_mip_size = get_image_format_required_size(tex->format, w, h, d, 1, &width, &height, &depth);
  1262. uint32_t block_w = 0, block_h = 0;
  1263. get_compressed_image_format_block_dimensions(tex->format, block_w, block_h);
  1264. uint32_t tight_row_pitch = tight_mip_size / ((height / block_h) * depth);
  1265. {
  1266. // Copy row-by-row to erase padding due to alignments.
  1267. const uint8_t *rp = read_ptr;
  1268. uint8_t *wp = write_ptr;
  1269. for (uint32_t row = h * d / block_h; row != 0; row--) {
  1270. memcpy(wp, rp, tight_row_pitch);
  1271. rp += mip_layouts[i].row_pitch;
  1272. wp += tight_row_pitch;
  1273. }
  1274. }
  1275. w = MAX(1u, w >> 1);
  1276. h = MAX(1u, h >> 1);
  1277. d = MAX(1u, d >> 1);
  1278. read_ptr += mip_layouts[i].size;
  1279. write_ptr += tight_mip_size;
  1280. }
  1281. }
  1282. driver->buffer_unmap(tmp_buffer);
  1283. driver->buffer_free(tmp_buffer);
  1284. return buffer_data;
  1285. }
  1286. }
  1287. bool RenderingDevice::texture_is_shared(RID p_texture) {
  1288. _THREAD_SAFE_METHOD_
  1289. Texture *tex = texture_owner.get_or_null(p_texture);
  1290. ERR_FAIL_COND_V(!tex, false);
  1291. return tex->owner.is_valid();
  1292. }
  1293. bool RenderingDevice::texture_is_valid(RID p_texture) {
  1294. return texture_owner.owns(p_texture);
  1295. }
  1296. RD::TextureFormat RenderingDevice::texture_get_format(RID p_texture) {
  1297. _THREAD_SAFE_METHOD_
  1298. Texture *tex = texture_owner.get_or_null(p_texture);
  1299. ERR_FAIL_COND_V(!tex, TextureFormat());
  1300. TextureFormat tf;
  1301. tf.format = tex->format;
  1302. tf.width = tex->width;
  1303. tf.height = tex->height;
  1304. tf.depth = tex->depth;
  1305. tf.array_layers = tex->layers;
  1306. tf.mipmaps = tex->mipmaps;
  1307. tf.texture_type = tex->type;
  1308. tf.samples = tex->samples;
  1309. tf.usage_bits = tex->usage_flags;
  1310. tf.shareable_formats = tex->allowed_shared_formats;
  1311. tf.is_resolve_buffer = tex->is_resolve_buffer;
  1312. return tf;
  1313. }
  1314. Size2i RenderingDevice::texture_size(RID p_texture) {
  1315. _THREAD_SAFE_METHOD_
  1316. Texture *tex = texture_owner.get_or_null(p_texture);
  1317. ERR_FAIL_COND_V(!tex, Size2i());
  1318. return Size2i(tex->width, tex->height);
  1319. }
  1320. #ifndef DISABLE_DEPRECATED
  1321. uint64_t RenderingDevice::texture_get_native_handle(RID p_texture) {
  1322. return get_driver_resource(DRIVER_RESOURCE_TEXTURE, p_texture);
  1323. }
  1324. #endif
  1325. Error RenderingDevice::texture_copy(RID p_from_texture, RID p_to_texture, const Vector3 &p_from, const Vector3 &p_to, const Vector3 &p_size, uint32_t p_src_mipmap, uint32_t p_dst_mipmap, uint32_t p_src_layer, uint32_t p_dst_layer, BitField<BarrierMask> p_post_barrier) {
  1326. _THREAD_SAFE_METHOD_
  1327. Texture *src_tex = texture_owner.get_or_null(p_from_texture);
  1328. ERR_FAIL_NULL_V(src_tex, ERR_INVALID_PARAMETER);
  1329. ERR_FAIL_COND_V_MSG(src_tex->bound, ERR_INVALID_PARAMETER,
  1330. "Source texture can't be copied while a draw list that uses it as part of a framebuffer is being created. Ensure the draw list is finalized (and that the color/depth texture using it is not set to `RenderingDevice.FINAL_ACTION_CONTINUE`) to copy this texture.");
  1331. ERR_FAIL_COND_V_MSG(!(src_tex->usage_flags & TEXTURE_USAGE_CAN_COPY_FROM_BIT), ERR_INVALID_PARAMETER,
  1332. "Source texture requires the `RenderingDevice.TEXTURE_USAGE_CAN_COPY_FROM_BIT` to be set to be retrieved.");
  1333. uint32_t src_layer_count = src_tex->layers;
  1334. uint32_t src_width, src_height, src_depth;
  1335. get_image_format_required_size(src_tex->format, src_tex->width, src_tex->height, src_tex->depth, p_src_mipmap + 1, &src_width, &src_height, &src_depth);
  1336. if (src_tex->type == TEXTURE_TYPE_CUBE || src_tex->type == TEXTURE_TYPE_CUBE_ARRAY) {
  1337. src_layer_count *= 6;
  1338. }
  1339. ERR_FAIL_COND_V(p_from.x < 0 || p_from.x + p_size.x > src_width, ERR_INVALID_PARAMETER);
  1340. ERR_FAIL_COND_V(p_from.y < 0 || p_from.y + p_size.y > src_height, ERR_INVALID_PARAMETER);
  1341. ERR_FAIL_COND_V(p_from.z < 0 || p_from.z + p_size.z > src_depth, ERR_INVALID_PARAMETER);
  1342. ERR_FAIL_COND_V(p_src_mipmap >= src_tex->mipmaps, ERR_INVALID_PARAMETER);
  1343. ERR_FAIL_COND_V(p_src_layer >= src_layer_count, ERR_INVALID_PARAMETER);
  1344. Texture *dst_tex = texture_owner.get_or_null(p_to_texture);
  1345. ERR_FAIL_NULL_V(dst_tex, ERR_INVALID_PARAMETER);
  1346. ERR_FAIL_COND_V_MSG(dst_tex->bound, ERR_INVALID_PARAMETER,
  1347. "Destination texture can't be copied while a draw list that uses it as part of a framebuffer is being created. Ensure the draw list is finalized (and that the color/depth texture using it is not set to `RenderingDevice.FINAL_ACTION_CONTINUE`) to copy this texture.");
  1348. ERR_FAIL_COND_V_MSG(!(dst_tex->usage_flags & TEXTURE_USAGE_CAN_COPY_TO_BIT), ERR_INVALID_PARAMETER,
  1349. "Destination texture requires the `RenderingDevice.TEXTURE_USAGE_CAN_COPY_TO_BIT` to be set to be retrieved.");
  1350. uint32_t dst_layer_count = dst_tex->layers;
  1351. uint32_t dst_width, dst_height, dst_depth;
  1352. get_image_format_required_size(dst_tex->format, dst_tex->width, dst_tex->height, dst_tex->depth, p_dst_mipmap + 1, &dst_width, &dst_height, &dst_depth);
  1353. if (dst_tex->type == TEXTURE_TYPE_CUBE || dst_tex->type == TEXTURE_TYPE_CUBE_ARRAY) {
  1354. dst_layer_count *= 6;
  1355. }
  1356. ERR_FAIL_COND_V(p_to.x < 0 || p_to.x + p_size.x > dst_width, ERR_INVALID_PARAMETER);
  1357. ERR_FAIL_COND_V(p_to.y < 0 || p_to.y + p_size.y > dst_height, ERR_INVALID_PARAMETER);
  1358. ERR_FAIL_COND_V(p_to.z < 0 || p_to.z + p_size.z > dst_depth, ERR_INVALID_PARAMETER);
  1359. ERR_FAIL_COND_V(p_dst_mipmap >= dst_tex->mipmaps, ERR_INVALID_PARAMETER);
  1360. ERR_FAIL_COND_V(p_dst_layer >= dst_layer_count, ERR_INVALID_PARAMETER);
  1361. ERR_FAIL_COND_V_MSG(src_tex->read_aspect_flags != dst_tex->read_aspect_flags, ERR_INVALID_PARAMETER,
  1362. "Source and destination texture must be of the same type (color or depth).");
  1363. RDD::CommandBufferID command_buffer = frames[frame].draw_command_buffer;
  1364. // PRE Copy the image.
  1365. if (driver->api_trait_get(RDD::API_TRAIT_HONORS_PIPELINE_BARRIERS)) {
  1366. { // Source.
  1367. RDD::TextureBarrier tb;
  1368. tb.texture = src_tex->driver_id;
  1369. tb.dst_access = RDD::BARRIER_ACCESS_TRANSFER_READ_BIT;
  1370. tb.prev_layout = src_tex->layout;
  1371. tb.next_layout = RDD::TEXTURE_LAYOUT_TRANSFER_SRC_OPTIMAL;
  1372. tb.subresources.aspect = src_tex->barrier_aspect_flags;
  1373. tb.subresources.base_mipmap = p_src_mipmap;
  1374. tb.subresources.mipmap_count = 1;
  1375. tb.subresources.base_layer = p_src_layer;
  1376. tb.subresources.layer_count = 1;
  1377. driver->command_pipeline_barrier(command_buffer, RDD::PIPELINE_STAGE_TOP_OF_PIPE_BIT, RDD::PIPELINE_STAGE_TRANSFER_BIT, {}, {}, tb);
  1378. }
  1379. { // Dest.
  1380. RDD::TextureBarrier tb;
  1381. tb.texture = dst_tex->driver_id;
  1382. tb.dst_access = RDD::BARRIER_ACCESS_TRANSFER_WRITE_BIT;
  1383. tb.prev_layout = dst_tex->layout;
  1384. tb.next_layout = RDD::TEXTURE_LAYOUT_TRANSFER_DST_OPTIMAL;
  1385. tb.subresources.aspect = dst_tex->read_aspect_flags;
  1386. tb.subresources.base_mipmap = p_dst_mipmap;
  1387. tb.subresources.mipmap_count = 1;
  1388. tb.subresources.base_layer = p_dst_layer;
  1389. tb.subresources.layer_count = 1;
  1390. driver->command_pipeline_barrier(command_buffer, RDD::PIPELINE_STAGE_TOP_OF_PIPE_BIT, RDD::PIPELINE_STAGE_TRANSFER_BIT, {}, {}, tb);
  1391. }
  1392. }
  1393. // COPY.
  1394. {
  1395. RDD::TextureCopyRegion copy_region;
  1396. copy_region.src_subresources.aspect = src_tex->read_aspect_flags;
  1397. copy_region.src_subresources.mipmap = p_src_mipmap;
  1398. copy_region.src_subresources.base_layer = p_src_layer;
  1399. copy_region.src_subresources.layer_count = 1;
  1400. copy_region.src_offset = p_from;
  1401. copy_region.dst_subresources.aspect = dst_tex->read_aspect_flags;
  1402. copy_region.dst_subresources.mipmap = p_dst_mipmap;
  1403. copy_region.dst_subresources.base_layer = p_dst_layer;
  1404. copy_region.dst_subresources.layer_count = 1;
  1405. copy_region.dst_offset = p_to;
  1406. copy_region.size = p_size;
  1407. driver->command_copy_texture(command_buffer, src_tex->driver_id, RDD::TEXTURE_LAYOUT_TRANSFER_SRC_OPTIMAL, dst_tex->driver_id, RDD::TEXTURE_LAYOUT_TRANSFER_DST_OPTIMAL, copy_region);
  1408. }
  1409. if (driver->api_trait_get(RDD::API_TRAIT_HONORS_PIPELINE_BARRIERS)) {
  1410. // RESTORE LAYOUT for SRC and DST.
  1411. BitField<RDD::PipelineStageBits> stages;
  1412. BitField<RDD::BarrierAccessBits> access;
  1413. if (p_post_barrier.has_flag(BARRIER_MASK_COMPUTE)) {
  1414. stages.set_flag(RDD::PIPELINE_STAGE_COMPUTE_SHADER_BIT);
  1415. access.set_flag(RDD::BARRIER_ACCESS_SHADER_READ_BIT).set_flag(RDD::BARRIER_ACCESS_SHADER_WRITE_BIT);
  1416. }
  1417. if (p_post_barrier.has_flag(BARRIER_MASK_VERTEX)) {
  1418. stages.set_flag(RDD::PIPELINE_STAGE_VERTEX_SHADER_BIT);
  1419. access.set_flag(RDD::BARRIER_ACCESS_SHADER_READ_BIT).set_flag(RDD::BARRIER_ACCESS_SHADER_WRITE_BIT);
  1420. }
  1421. if (p_post_barrier.has_flag(BARRIER_MASK_FRAGMENT)) {
  1422. stages.set_flag(RDD::PIPELINE_STAGE_FRAGMENT_SHADER_BIT);
  1423. access.set_flag(RDD::BARRIER_ACCESS_SHADER_READ_BIT).set_flag(RDD::BARRIER_ACCESS_SHADER_WRITE_BIT);
  1424. }
  1425. if (p_post_barrier.has_flag(BARRIER_MASK_TRANSFER)) {
  1426. stages.set_flag(RDD::PIPELINE_STAGE_TRANSFER_BIT);
  1427. access.set_flag(RDD::BARRIER_ACCESS_TRANSFER_WRITE_BIT);
  1428. }
  1429. if (stages.is_empty()) {
  1430. stages.set_flag(RDD::PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT);
  1431. }
  1432. { // Restore src.
  1433. RDD::TextureBarrier tb;
  1434. tb.texture = src_tex->driver_id;
  1435. tb.src_access = RDD::BARRIER_ACCESS_TRANSFER_READ_BIT;
  1436. tb.dst_access = access;
  1437. tb.prev_layout = RDD::TEXTURE_LAYOUT_TRANSFER_SRC_OPTIMAL;
  1438. tb.next_layout = src_tex->layout;
  1439. tb.subresources.aspect = src_tex->barrier_aspect_flags;
  1440. tb.subresources.base_mipmap = p_src_mipmap;
  1441. tb.subresources.mipmap_count = 1;
  1442. tb.subresources.base_layer = p_src_layer;
  1443. tb.subresources.layer_count = 1;
  1444. driver->command_pipeline_barrier(command_buffer, RDD::PIPELINE_STAGE_TRANSFER_BIT, stages, {}, {}, tb);
  1445. }
  1446. { // Make dst readable.
  1447. RDD::TextureBarrier tb;
  1448. tb.texture = dst_tex->driver_id;
  1449. tb.src_access = RDD::BARRIER_ACCESS_TRANSFER_WRITE_BIT;
  1450. tb.dst_access = access;
  1451. tb.prev_layout = RDD::TEXTURE_LAYOUT_TRANSFER_DST_OPTIMAL;
  1452. tb.next_layout = dst_tex->layout;
  1453. tb.subresources.aspect = dst_tex->read_aspect_flags;
  1454. tb.subresources.base_mipmap = p_dst_mipmap;
  1455. tb.subresources.mipmap_count = 1;
  1456. tb.subresources.base_layer = p_dst_layer;
  1457. tb.subresources.layer_count = 1;
  1458. driver->command_pipeline_barrier(command_buffer, RDD::PIPELINE_STAGE_TRANSFER_BIT, stages, {}, {}, tb);
  1459. }
  1460. if (dst_tex->used_in_frame != frames_drawn) {
  1461. dst_tex->used_in_raster = false;
  1462. dst_tex->used_in_compute = false;
  1463. dst_tex->used_in_frame = frames_drawn;
  1464. }
  1465. dst_tex->used_in_transfer = true;
  1466. }
  1467. return OK;
  1468. }
  1469. Error RenderingDevice::texture_resolve_multisample(RID p_from_texture, RID p_to_texture, BitField<BarrierMask> p_post_barrier) {
  1470. _THREAD_SAFE_METHOD_
  1471. Texture *src_tex = texture_owner.get_or_null(p_from_texture);
  1472. ERR_FAIL_NULL_V(src_tex, ERR_INVALID_PARAMETER);
  1473. ERR_FAIL_COND_V_MSG(src_tex->bound, ERR_INVALID_PARAMETER,
  1474. "Source texture can't be copied while a draw list that uses it as part of a framebuffer is being created. Ensure the draw list is finalized (and that the color/depth texture using it is not set to `RenderingDevice.FINAL_ACTION_CONTINUE`) to copy this texture.");
  1475. ERR_FAIL_COND_V_MSG(!(src_tex->usage_flags & TEXTURE_USAGE_CAN_COPY_FROM_BIT), ERR_INVALID_PARAMETER,
  1476. "Source texture requires the `RenderingDevice.TEXTURE_USAGE_CAN_COPY_FROM_BIT` to be set to be retrieved.");
  1477. ERR_FAIL_COND_V_MSG(src_tex->type != TEXTURE_TYPE_2D, ERR_INVALID_PARAMETER, "Source texture must be 2D (or a slice of a 3D/Cube texture)");
  1478. ERR_FAIL_COND_V_MSG(src_tex->samples == TEXTURE_SAMPLES_1, ERR_INVALID_PARAMETER, "Source texture must be multisampled.");
  1479. Texture *dst_tex = texture_owner.get_or_null(p_to_texture);
  1480. ERR_FAIL_NULL_V(dst_tex, ERR_INVALID_PARAMETER);
  1481. ERR_FAIL_COND_V_MSG(dst_tex->bound, ERR_INVALID_PARAMETER,
  1482. "Destination texture can't be copied while a draw list that uses it as part of a framebuffer is being created. Ensure the draw list is finalized (and that the color/depth texture using it is not set to `RenderingDevice.FINAL_ACTION_CONTINUE`) to copy this texture.");
  1483. ERR_FAIL_COND_V_MSG(!(dst_tex->usage_flags & TEXTURE_USAGE_CAN_COPY_TO_BIT), ERR_INVALID_PARAMETER,
  1484. "Destination texture requires the `RenderingDevice.TEXTURE_USAGE_CAN_COPY_TO_BIT` to be set to be retrieved.");
  1485. ERR_FAIL_COND_V_MSG(dst_tex->type != TEXTURE_TYPE_2D, ERR_INVALID_PARAMETER, "Destination texture must be 2D (or a slice of a 3D/Cube texture).");
  1486. ERR_FAIL_COND_V_MSG(dst_tex->samples != TEXTURE_SAMPLES_1, ERR_INVALID_PARAMETER, "Destination texture must not be multisampled.");
  1487. ERR_FAIL_COND_V_MSG(src_tex->format != dst_tex->format, ERR_INVALID_PARAMETER, "Source and Destination textures must be the same format.");
  1488. ERR_FAIL_COND_V_MSG(src_tex->width != dst_tex->width && src_tex->height != dst_tex->height && src_tex->depth != dst_tex->depth, ERR_INVALID_PARAMETER, "Source and Destination textures must have the same dimensions.");
  1489. ERR_FAIL_COND_V_MSG(src_tex->read_aspect_flags != dst_tex->read_aspect_flags, ERR_INVALID_PARAMETER,
  1490. "Source and destination texture must be of the same type (color or depth).");
  1491. RDD::CommandBufferID command_buffer = frames[frame].draw_command_buffer;
  1492. if (driver->api_trait_get(RDD::API_TRAIT_HONORS_PIPELINE_BARRIERS)) {
  1493. // PRE Copy the image.
  1494. { // Source.
  1495. RDD::TextureBarrier tb;
  1496. tb.texture = src_tex->driver_id;
  1497. tb.dst_access = RDD::BARRIER_ACCESS_TRANSFER_READ_BIT;
  1498. tb.prev_layout = src_tex->layout;
  1499. tb.next_layout = RDD::TEXTURE_LAYOUT_TRANSFER_SRC_OPTIMAL;
  1500. tb.subresources.aspect = src_tex->barrier_aspect_flags;
  1501. tb.subresources.base_mipmap = src_tex->base_mipmap;
  1502. tb.subresources.mipmap_count = 1;
  1503. tb.subresources.base_layer = src_tex->base_layer;
  1504. tb.subresources.layer_count = 1;
  1505. driver->command_pipeline_barrier(command_buffer, RDD::PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT, RDD::PIPELINE_STAGE_TRANSFER_BIT, {}, {}, tb);
  1506. }
  1507. { // Dest.
  1508. RDD::TextureBarrier tb;
  1509. tb.texture = dst_tex->driver_id;
  1510. tb.dst_access = RDD::BARRIER_ACCESS_TRANSFER_WRITE_BIT;
  1511. tb.prev_layout = dst_tex->layout;
  1512. tb.next_layout = RDD::TEXTURE_LAYOUT_TRANSFER_DST_OPTIMAL;
  1513. tb.subresources.aspect = dst_tex->barrier_aspect_flags;
  1514. tb.subresources.base_mipmap = dst_tex->base_mipmap;
  1515. tb.subresources.mipmap_count = 1;
  1516. tb.subresources.base_layer = dst_tex->base_layer;
  1517. tb.subresources.layer_count = 1;
  1518. driver->command_pipeline_barrier(command_buffer, RDD::PIPELINE_STAGE_TOP_OF_PIPE_BIT, RDD::PIPELINE_STAGE_TRANSFER_BIT, {}, {}, tb);
  1519. }
  1520. }
  1521. // RESOLVE.
  1522. driver->command_resolve_texture(command_buffer, src_tex->driver_id, RDD::TEXTURE_LAYOUT_TRANSFER_SRC_OPTIMAL, src_tex->base_layer, src_tex->base_mipmap, dst_tex->driver_id, RDD::TEXTURE_LAYOUT_TRANSFER_DST_OPTIMAL, dst_tex->base_layer, dst_tex->base_mipmap);
  1523. if (driver->api_trait_get(RDD::API_TRAIT_HONORS_PIPELINE_BARRIERS)) {
  1524. // RESTORE LAYOUT for SRC and DST.
  1525. BitField<RDD::PipelineStageBits> stages;
  1526. BitField<RDD::BarrierAccessBits> access;
  1527. if (p_post_barrier.has_flag(BARRIER_MASK_COMPUTE)) {
  1528. stages.set_flag(RDD::PIPELINE_STAGE_COMPUTE_SHADER_BIT);
  1529. access.set_flag(RDD::BARRIER_ACCESS_SHADER_READ_BIT).set_flag(RDD::BARRIER_ACCESS_SHADER_WRITE_BIT);
  1530. }
  1531. if (p_post_barrier.has_flag(BARRIER_MASK_VERTEX)) {
  1532. stages.set_flag(RDD::PIPELINE_STAGE_VERTEX_SHADER_BIT);
  1533. access.set_flag(RDD::BARRIER_ACCESS_SHADER_READ_BIT).set_flag(RDD::BARRIER_ACCESS_SHADER_WRITE_BIT);
  1534. }
  1535. if (p_post_barrier.has_flag(BARRIER_MASK_FRAGMENT)) {
  1536. stages.set_flag(RDD::PIPELINE_STAGE_FRAGMENT_SHADER_BIT);
  1537. access.set_flag(RDD::BARRIER_ACCESS_SHADER_READ_BIT).set_flag(RDD::BARRIER_ACCESS_SHADER_WRITE_BIT);
  1538. }
  1539. if (p_post_barrier.has_flag(BARRIER_MASK_TRANSFER)) {
  1540. stages.set_flag(RDD::PIPELINE_STAGE_TRANSFER_BIT);
  1541. access.set_flag(RDD::BARRIER_ACCESS_TRANSFER_WRITE_BIT);
  1542. }
  1543. if (stages.is_empty()) {
  1544. stages.set_flag(RDD::PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT);
  1545. }
  1546. { // Restore src.
  1547. RDD::TextureBarrier tb;
  1548. tb.texture = src_tex->driver_id;
  1549. tb.src_access = RDD::BARRIER_ACCESS_TRANSFER_READ_BIT;
  1550. tb.dst_access = access;
  1551. tb.prev_layout = RDD::TEXTURE_LAYOUT_TRANSFER_SRC_OPTIMAL;
  1552. tb.next_layout = src_tex->layout;
  1553. tb.subresources.aspect = src_tex->barrier_aspect_flags;
  1554. tb.subresources.base_mipmap = src_tex->base_mipmap;
  1555. tb.subresources.mipmap_count = 1;
  1556. tb.subresources.base_layer = src_tex->base_layer;
  1557. tb.subresources.layer_count = 1;
  1558. driver->command_pipeline_barrier(command_buffer, RDD::BARRIER_ACCESS_TRANSFER_WRITE_BIT, stages, {}, {}, tb);
  1559. }
  1560. { // Make dst readable.
  1561. RDD::TextureBarrier tb;
  1562. tb.texture = dst_tex->driver_id;
  1563. tb.src_access = RDD::BARRIER_ACCESS_TRANSFER_WRITE_BIT;
  1564. tb.dst_access = access;
  1565. tb.prev_layout = RDD::TEXTURE_LAYOUT_TRANSFER_DST_OPTIMAL;
  1566. tb.next_layout = dst_tex->layout;
  1567. tb.subresources.aspect = RDD::TEXTURE_ASPECT_COLOR_BIT;
  1568. tb.subresources.base_mipmap = dst_tex->base_mipmap;
  1569. tb.subresources.mipmap_count = 1;
  1570. tb.subresources.base_layer = dst_tex->base_layer;
  1571. tb.subresources.layer_count = 1;
  1572. driver->command_pipeline_barrier(command_buffer, RDD::PIPELINE_STAGE_TRANSFER_BIT, stages, {}, {}, tb);
  1573. }
  1574. }
  1575. return OK;
  1576. }
  1577. Error RenderingDevice::texture_clear(RID p_texture, const Color &p_color, uint32_t p_base_mipmap, uint32_t p_mipmaps, uint32_t p_base_layer, uint32_t p_layers, BitField<BarrierMask> p_post_barrier) {
  1578. _THREAD_SAFE_METHOD_
  1579. Texture *src_tex = texture_owner.get_or_null(p_texture);
  1580. ERR_FAIL_NULL_V(src_tex, ERR_INVALID_PARAMETER);
  1581. ERR_FAIL_COND_V_MSG(src_tex->bound, ERR_INVALID_PARAMETER,
  1582. "Source texture can't be cleared while a draw list that uses it as part of a framebuffer is being created. Ensure the draw list is finalized (and that the color/depth texture using it is not set to `RenderingDevice.FINAL_ACTION_CONTINUE`) to clear this texture.");
  1583. ERR_FAIL_COND_V(p_layers == 0, ERR_INVALID_PARAMETER);
  1584. ERR_FAIL_COND_V(p_mipmaps == 0, ERR_INVALID_PARAMETER);
  1585. ERR_FAIL_COND_V_MSG(!(src_tex->usage_flags & TEXTURE_USAGE_CAN_COPY_TO_BIT), ERR_INVALID_PARAMETER,
  1586. "Source texture requires the `RenderingDevice.TEXTURE_USAGE_CAN_COPY_TO_BIT` to be set to be cleared.");
  1587. uint32_t src_layer_count = src_tex->layers;
  1588. if (src_tex->type == TEXTURE_TYPE_CUBE || src_tex->type == TEXTURE_TYPE_CUBE_ARRAY) {
  1589. src_layer_count *= 6;
  1590. }
  1591. ERR_FAIL_COND_V(p_base_mipmap + p_mipmaps > src_tex->mipmaps, ERR_INVALID_PARAMETER);
  1592. ERR_FAIL_COND_V(p_base_layer + p_layers > src_layer_count, ERR_INVALID_PARAMETER);
  1593. RDD::CommandBufferID command_buffer = frames[frame].draw_command_buffer;
  1594. RDD::TextureLayout clear_layout = (src_tex->layout == RDD::TEXTURE_LAYOUT_GENERAL) ? RDD::TEXTURE_LAYOUT_GENERAL : RDD::TEXTURE_LAYOUT_TRANSFER_DST_OPTIMAL;
  1595. // NOTE: Perhaps the valid stages/accesses for a given owner should be a property of the owner. (Here and places like _get_buffer_from_owner.)
  1596. const BitField<RDD::PipelineStageBits> valid_texture_stages = RDD::PIPELINE_STAGE_VERTEX_SHADER_BIT | RDD::PIPELINE_STAGE_FRAGMENT_SHADER_BIT | RDD::PIPELINE_STAGE_COMPUTE_SHADER_BIT;
  1597. constexpr BitField<RDD::BarrierAccessBits> read_access = RDD::BARRIER_ACCESS_SHADER_READ_BIT;
  1598. constexpr BitField<RDD::BarrierAccessBits> read_write_access = RDD::BARRIER_ACCESS_SHADER_READ_BIT | RDD::BARRIER_ACCESS_SHADER_WRITE_BIT;
  1599. const BitField<RDD::BarrierAccessBits> valid_texture_access = (src_tex->usage_flags & TEXTURE_USAGE_STORAGE_BIT) ? read_write_access : read_access;
  1600. // Barrier from previous access with optional layout change (see clear_layout logic above).
  1601. if (driver->api_trait_get(RDD::API_TRAIT_HONORS_PIPELINE_BARRIERS)) {
  1602. RDD::TextureBarrier tb;
  1603. tb.texture = src_tex->driver_id;
  1604. tb.src_access = valid_texture_access;
  1605. tb.dst_access = RDD::BARRIER_ACCESS_TRANSFER_WRITE_BIT;
  1606. tb.prev_layout = src_tex->layout;
  1607. tb.next_layout = clear_layout;
  1608. tb.subresources.aspect = src_tex->read_aspect_flags;
  1609. tb.subresources.base_mipmap = src_tex->base_mipmap + p_base_mipmap;
  1610. tb.subresources.mipmap_count = p_mipmaps;
  1611. tb.subresources.base_layer = src_tex->base_layer + p_base_layer;
  1612. tb.subresources.layer_count = p_layers;
  1613. driver->command_pipeline_barrier(command_buffer, valid_texture_stages, RDD::PIPELINE_STAGE_TRANSFER_BIT, {}, {}, tb);
  1614. }
  1615. RDD::TextureSubresourceRange range;
  1616. range.aspect = src_tex->read_aspect_flags;
  1617. range.base_mipmap = src_tex->base_mipmap + p_base_mipmap;
  1618. range.mipmap_count = p_mipmaps;
  1619. range.base_layer = src_tex->base_layer + p_base_layer;
  1620. range.layer_count = p_layers;
  1621. driver->command_clear_color_texture(command_buffer, src_tex->driver_id, clear_layout, p_color, range);
  1622. // Barrier to post clear accesses (changing back the layout if needed).
  1623. if (driver->api_trait_get(RDD::API_TRAIT_HONORS_PIPELINE_BARRIERS)) {
  1624. BitField<RDD::PipelineStageBits> stages;
  1625. BitField<RDD::BarrierAccessBits> access;
  1626. if (p_post_barrier.has_flag(BARRIER_MASK_COMPUTE)) {
  1627. stages.set_flag(RDD::PIPELINE_STAGE_COMPUTE_SHADER_BIT);
  1628. access.set_flag(RDD::BARRIER_ACCESS_SHADER_READ_BIT).set_flag(RDD::BARRIER_ACCESS_SHADER_WRITE_BIT);
  1629. }
  1630. if (p_post_barrier.has_flag(BARRIER_MASK_VERTEX)) {
  1631. stages.set_flag(RDD::PIPELINE_STAGE_VERTEX_SHADER_BIT);
  1632. access.set_flag(RDD::BARRIER_ACCESS_SHADER_READ_BIT).set_flag(RDD::BARRIER_ACCESS_SHADER_WRITE_BIT);
  1633. }
  1634. if (p_post_barrier.has_flag(BARRIER_MASK_FRAGMENT)) {
  1635. stages.set_flag(RDD::PIPELINE_STAGE_FRAGMENT_SHADER_BIT);
  1636. access.set_flag(RDD::BARRIER_ACCESS_SHADER_READ_BIT).set_flag(RDD::BARRIER_ACCESS_SHADER_WRITE_BIT);
  1637. }
  1638. if (p_post_barrier.has_flag(BARRIER_MASK_TRANSFER)) {
  1639. stages.set_flag(RDD::PIPELINE_STAGE_TRANSFER_BIT);
  1640. access.set_flag(RDD::BARRIER_ACCESS_TRANSFER_WRITE_BIT);
  1641. }
  1642. if (stages.is_empty()) {
  1643. stages.set_flag(RDD::PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT);
  1644. }
  1645. RDD::TextureBarrier tb;
  1646. tb.texture = src_tex->driver_id;
  1647. tb.src_access = RDD::BARRIER_ACCESS_TRANSFER_WRITE_BIT;
  1648. tb.dst_access = access;
  1649. tb.prev_layout = clear_layout;
  1650. tb.next_layout = src_tex->layout;
  1651. tb.subresources.aspect = src_tex->read_aspect_flags;
  1652. tb.subresources.base_mipmap = src_tex->base_mipmap + p_base_mipmap;
  1653. tb.subresources.mipmap_count = p_mipmaps;
  1654. tb.subresources.base_layer = src_tex->base_layer + p_base_layer;
  1655. tb.subresources.layer_count = p_layers;
  1656. driver->command_pipeline_barrier(command_buffer, RDD::PIPELINE_STAGE_TRANSFER_BIT, stages, {}, {}, tb);
  1657. if (src_tex->used_in_frame != frames_drawn) {
  1658. src_tex->used_in_raster = false;
  1659. src_tex->used_in_compute = false;
  1660. src_tex->used_in_frame = frames_drawn;
  1661. }
  1662. src_tex->used_in_transfer = true;
  1663. }
  1664. return OK;
  1665. }
  1666. bool RenderingDevice::texture_is_format_supported_for_usage(DataFormat p_format, BitField<RenderingDevice::TextureUsageBits> p_usage) const {
  1667. ERR_FAIL_INDEX_V(p_format, DATA_FORMAT_MAX, false);
  1668. _THREAD_SAFE_METHOD_
  1669. bool cpu_readable = (p_usage & RDD::TEXTURE_USAGE_CPU_READ_BIT);
  1670. BitField<TextureUsageBits> supported = driver->texture_get_usages_supported_by_format(p_format, cpu_readable);
  1671. bool any_unsupported = (((int64_t)supported) | ((int64_t)p_usage)) != ((int64_t)supported);
  1672. return !any_unsupported;
  1673. }
  1674. /*********************/
  1675. /**** FRAMEBUFFER ****/
  1676. /*********************/
  1677. RDD::RenderPassID RenderingDevice::_render_pass_create(const Vector<AttachmentFormat> &p_attachments, const Vector<FramebufferPass> &p_passes, InitialAction p_initial_action, FinalAction p_final_action, InitialAction p_initial_depth_action, FinalAction p_final_depth_action, uint32_t p_view_count, Vector<TextureSamples> *r_samples) {
  1678. // NOTE:
  1679. // Before the refactor to RenderingDevice-RenderingDeviceDriver, there was commented out code to
  1680. // specify dependencies to external subpasses. Since it had been unused for a long timel it wasn't ported
  1681. // to the new architecture.
  1682. LocalVector<int32_t> attachment_last_pass;
  1683. attachment_last_pass.resize(p_attachments.size());
  1684. if (p_view_count > 1) {
  1685. const RDD::MultiviewCapabilities &capabilities = driver->get_multiview_capabilities();
  1686. // This only works with multiview!
  1687. ERR_FAIL_COND_V_MSG(!capabilities.is_supported, RDD::RenderPassID(), "Multiview not supported");
  1688. // Make sure we limit this to the number of views we support.
  1689. ERR_FAIL_COND_V_MSG(p_view_count > capabilities.max_view_count, RDD::RenderPassID(), "Hardware does not support requested number of views for Multiview render pass");
  1690. }
  1691. LocalVector<RDD::Attachment> attachments;
  1692. LocalVector<int> attachment_remap;
  1693. for (int i = 0; i < p_attachments.size(); i++) {
  1694. if (p_attachments[i].usage_flags == AttachmentFormat::UNUSED_ATTACHMENT) {
  1695. attachment_remap.push_back(RDD::AttachmentReference::UNUSED);
  1696. continue;
  1697. }
  1698. ERR_FAIL_INDEX_V(p_attachments[i].format, DATA_FORMAT_MAX, RDD::RenderPassID());
  1699. ERR_FAIL_INDEX_V(p_attachments[i].samples, TEXTURE_SAMPLES_MAX, RDD::RenderPassID());
  1700. ERR_FAIL_COND_V_MSG(!(p_attachments[i].usage_flags & (TEXTURE_USAGE_COLOR_ATTACHMENT_BIT | TEXTURE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT | TEXTURE_USAGE_INPUT_ATTACHMENT_BIT | TEXTURE_USAGE_VRS_ATTACHMENT_BIT)),
  1701. RDD::RenderPassID(), "Texture format for index (" + itos(i) + ") requires an attachment (color, depth-stencil, input or VRS) bit set.");
  1702. RDD::Attachment description;
  1703. description.format = p_attachments[i].format;
  1704. description.samples = p_attachments[i].samples;
  1705. bool is_sampled = (p_attachments[i].usage_flags & TEXTURE_USAGE_SAMPLING_BIT);
  1706. bool is_storage = (p_attachments[i].usage_flags & TEXTURE_USAGE_STORAGE_BIT);
  1707. bool is_depth = (p_attachments[i].usage_flags & TEXTURE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT);
  1708. // We can setup a framebuffer where we write to our VRS texture to set it up.
  1709. // We make the assumption here that if our texture is actually used as our VRS attachment.
  1710. // It is used as such for each subpass. This is fairly certain seeing the restrictions on subpasses.
  1711. bool is_vrs = (p_attachments[i].usage_flags & TEXTURE_USAGE_VRS_ATTACHMENT_BIT) && i == p_passes[0].vrs_attachment;
  1712. if (is_vrs) {
  1713. // For VRS we only read, there is no writing to this texture.
  1714. description.load_op = RDD::ATTACHMENT_LOAD_OP_LOAD;
  1715. description.initial_layout = RDD::TEXTURE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
  1716. description.stencil_load_op = RDD::ATTACHMENT_LOAD_OP_LOAD;
  1717. } else {
  1718. // For each UNDEFINED, assume the prior use was a *read*, as we'd be discarding the output of a write.
  1719. // Also, each UNDEFINED will do an immediate layout transition (write), s.t. we must ensure execution synchronization vs
  1720. // the read. If this is a performance issue, one could track the actual last accessor of each resource, adding only that
  1721. // stage.
  1722. switch (is_depth ? p_initial_depth_action : p_initial_action) {
  1723. case INITIAL_ACTION_CLEAR_REGION:
  1724. case INITIAL_ACTION_CLEAR: {
  1725. if ((p_attachments[i].usage_flags & TEXTURE_USAGE_COLOR_ATTACHMENT_BIT)) {
  1726. description.load_op = RDD::ATTACHMENT_LOAD_OP_CLEAR;
  1727. description.initial_layout = is_sampled ? RDD::TEXTURE_LAYOUT_SHADER_READ_ONLY_OPTIMAL : (is_storage ? RDD::TEXTURE_LAYOUT_GENERAL : RDD::TEXTURE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL);
  1728. description.stencil_load_op = RDD::ATTACHMENT_LOAD_OP_DONT_CARE;
  1729. } else if ((p_attachments[i].usage_flags & TEXTURE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT)) {
  1730. description.load_op = RDD::ATTACHMENT_LOAD_OP_CLEAR;
  1731. description.initial_layout = is_sampled ? RDD::TEXTURE_LAYOUT_SHADER_READ_ONLY_OPTIMAL : (is_storage ? RDD::TEXTURE_LAYOUT_GENERAL : RDD::TEXTURE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL);
  1732. description.stencil_load_op = RDD::ATTACHMENT_LOAD_OP_CLEAR;
  1733. } else {
  1734. description.load_op = RDD::ATTACHMENT_LOAD_OP_DONT_CARE;
  1735. description.stencil_load_op = RDD::ATTACHMENT_LOAD_OP_DONT_CARE;
  1736. description.initial_layout = RDD::TEXTURE_LAYOUT_UNDEFINED; // Don't care what is there.
  1737. }
  1738. } break;
  1739. case INITIAL_ACTION_KEEP: {
  1740. if ((p_attachments[i].usage_flags & TEXTURE_USAGE_COLOR_ATTACHMENT_BIT)) {
  1741. description.load_op = RDD::ATTACHMENT_LOAD_OP_LOAD;
  1742. description.initial_layout = is_sampled ? RDD::TEXTURE_LAYOUT_SHADER_READ_ONLY_OPTIMAL : (is_storage ? RDD::TEXTURE_LAYOUT_GENERAL : RDD::TEXTURE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL);
  1743. description.stencil_load_op = RDD::ATTACHMENT_LOAD_OP_DONT_CARE;
  1744. } else if ((p_attachments[i].usage_flags & TEXTURE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT)) {
  1745. description.load_op = RDD::ATTACHMENT_LOAD_OP_LOAD;
  1746. description.initial_layout = is_sampled ? RDD::TEXTURE_LAYOUT_SHADER_READ_ONLY_OPTIMAL : (is_storage ? RDD::TEXTURE_LAYOUT_GENERAL : RDD::TEXTURE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL);
  1747. description.stencil_load_op = RDD::ATTACHMENT_LOAD_OP_LOAD;
  1748. } else {
  1749. description.load_op = RDD::ATTACHMENT_LOAD_OP_DONT_CARE;
  1750. description.stencil_load_op = RDD::ATTACHMENT_LOAD_OP_DONT_CARE;
  1751. description.initial_layout = RDD::TEXTURE_LAYOUT_UNDEFINED; // Don't care what is there.
  1752. }
  1753. } break;
  1754. case INITIAL_ACTION_DROP: {
  1755. if ((p_attachments[i].usage_flags & TEXTURE_USAGE_COLOR_ATTACHMENT_BIT)) {
  1756. description.load_op = RDD::ATTACHMENT_LOAD_OP_DONT_CARE;
  1757. description.initial_layout = is_sampled ? RDD::TEXTURE_LAYOUT_SHADER_READ_ONLY_OPTIMAL : (is_storage ? RDD::TEXTURE_LAYOUT_GENERAL : RDD::TEXTURE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL);
  1758. description.stencil_load_op = RDD::ATTACHMENT_LOAD_OP_DONT_CARE;
  1759. } else if ((p_attachments[i].usage_flags & TEXTURE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT)) {
  1760. description.load_op = RDD::ATTACHMENT_LOAD_OP_DONT_CARE;
  1761. description.initial_layout = RDD::TEXTURE_LAYOUT_UNDEFINED; // Don't care what is there.
  1762. description.stencil_load_op = RDD::ATTACHMENT_LOAD_OP_DONT_CARE;
  1763. } else {
  1764. description.load_op = RDD::ATTACHMENT_LOAD_OP_DONT_CARE;
  1765. description.stencil_load_op = RDD::ATTACHMENT_LOAD_OP_DONT_CARE;
  1766. description.initial_layout = RDD::TEXTURE_LAYOUT_UNDEFINED; // Don't care what is there.
  1767. }
  1768. } break;
  1769. case INITIAL_ACTION_CLEAR_REGION_CONTINUE:
  1770. case INITIAL_ACTION_CONTINUE: {
  1771. if ((p_attachments[i].usage_flags & TEXTURE_USAGE_COLOR_ATTACHMENT_BIT)) {
  1772. description.load_op = RDD::ATTACHMENT_LOAD_OP_LOAD;
  1773. description.initial_layout = RDD::TEXTURE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
  1774. description.stencil_load_op = RDD::ATTACHMENT_LOAD_OP_DONT_CARE;
  1775. } else if ((p_attachments[i].usage_flags & TEXTURE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT)) {
  1776. description.load_op = RDD::ATTACHMENT_LOAD_OP_LOAD;
  1777. description.initial_layout = RDD::TEXTURE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL;
  1778. description.stencil_load_op = RDD::ATTACHMENT_LOAD_OP_LOAD;
  1779. } else {
  1780. description.load_op = RDD::ATTACHMENT_LOAD_OP_DONT_CARE;
  1781. description.stencil_load_op = RDD::ATTACHMENT_LOAD_OP_DONT_CARE;
  1782. description.initial_layout = RDD::TEXTURE_LAYOUT_UNDEFINED; // Don't care what is there.
  1783. }
  1784. } break;
  1785. default: {
  1786. ERR_FAIL_V(RDD::RenderPassID()); // Should never reach here.
  1787. }
  1788. }
  1789. }
  1790. bool used_last = false;
  1791. {
  1792. int last_pass = p_passes.size() - 1;
  1793. if (is_depth) {
  1794. // Likely missing depth resolve?
  1795. if (p_passes[last_pass].depth_attachment == i) {
  1796. used_last = true;
  1797. }
  1798. } else if (is_vrs) {
  1799. if (p_passes[last_pass].vrs_attachment == i) {
  1800. used_last = true;
  1801. }
  1802. } else {
  1803. if (p_passes[last_pass].resolve_attachments.size()) {
  1804. // If using resolve attachments, check resolve attachments.
  1805. for (int j = 0; j < p_passes[last_pass].resolve_attachments.size(); j++) {
  1806. if (p_passes[last_pass].resolve_attachments[j] == i) {
  1807. used_last = true;
  1808. break;
  1809. }
  1810. }
  1811. }
  1812. if (!used_last) {
  1813. for (int j = 0; j < p_passes[last_pass].color_attachments.size(); j++) {
  1814. if (p_passes[last_pass].color_attachments[j] == i) {
  1815. used_last = true;
  1816. break;
  1817. }
  1818. }
  1819. }
  1820. }
  1821. if (!used_last) {
  1822. for (int j = 0; j < p_passes[last_pass].preserve_attachments.size(); j++) {
  1823. if (p_passes[last_pass].preserve_attachments[j] == i) {
  1824. used_last = true;
  1825. break;
  1826. }
  1827. }
  1828. }
  1829. }
  1830. FinalAction final_action = p_final_action;
  1831. FinalAction final_depth_action = p_final_depth_action;
  1832. if (!used_last) {
  1833. if (is_depth) {
  1834. final_depth_action = FINAL_ACTION_DISCARD;
  1835. } else {
  1836. final_action = FINAL_ACTION_DISCARD;
  1837. }
  1838. }
  1839. if (is_vrs) {
  1840. // We don't change our VRS texture during this process.
  1841. description.store_op = RDD::ATTACHMENT_STORE_OP_DONT_CARE;
  1842. description.stencil_store_op = RDD::ATTACHMENT_STORE_OP_DONT_CARE;
  1843. description.final_layout = RDD::TEXTURE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
  1844. } else {
  1845. switch (is_depth ? final_depth_action : final_action) {
  1846. case FINAL_ACTION_READ: {
  1847. if ((p_attachments[i].usage_flags & TEXTURE_USAGE_COLOR_ATTACHMENT_BIT)) {
  1848. description.store_op = RDD::ATTACHMENT_STORE_OP_STORE;
  1849. description.stencil_store_op = RDD::ATTACHMENT_STORE_OP_DONT_CARE;
  1850. description.final_layout = is_sampled ? RDD::TEXTURE_LAYOUT_SHADER_READ_ONLY_OPTIMAL : (is_storage ? RDD::TEXTURE_LAYOUT_GENERAL : RDD::TEXTURE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL);
  1851. } else if ((p_attachments[i].usage_flags & TEXTURE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT)) {
  1852. description.store_op = RDD::ATTACHMENT_STORE_OP_STORE;
  1853. description.stencil_store_op = RDD::ATTACHMENT_STORE_OP_STORE;
  1854. description.final_layout = is_sampled ? RDD::TEXTURE_LAYOUT_SHADER_READ_ONLY_OPTIMAL : (is_storage ? RDD::TEXTURE_LAYOUT_GENERAL : RDD::TEXTURE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL);
  1855. } else {
  1856. description.load_op = RDD::ATTACHMENT_LOAD_OP_DONT_CARE;
  1857. description.stencil_load_op = RDD::ATTACHMENT_LOAD_OP_DONT_CARE;
  1858. description.final_layout = RDD::TEXTURE_LAYOUT_UNDEFINED; // Don't care what is there.
  1859. // TODO: What does this mean about the next usage (and thus appropriate dependency masks.
  1860. }
  1861. } break;
  1862. case FINAL_ACTION_DISCARD: {
  1863. if ((p_attachments[i].usage_flags & TEXTURE_USAGE_COLOR_ATTACHMENT_BIT)) {
  1864. description.store_op = RDD::ATTACHMENT_STORE_OP_DONT_CARE;
  1865. description.stencil_store_op = RDD::ATTACHMENT_STORE_OP_DONT_CARE;
  1866. description.final_layout = is_sampled ? RDD::TEXTURE_LAYOUT_SHADER_READ_ONLY_OPTIMAL : (is_storage ? RDD::TEXTURE_LAYOUT_GENERAL : RDD::TEXTURE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL);
  1867. } else if ((p_attachments[i].usage_flags & TEXTURE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT)) {
  1868. description.store_op = RDD::ATTACHMENT_STORE_OP_DONT_CARE;
  1869. description.stencil_store_op = RDD::ATTACHMENT_STORE_OP_DONT_CARE;
  1870. description.final_layout = is_sampled ? RDD::TEXTURE_LAYOUT_SHADER_READ_ONLY_OPTIMAL : (is_storage ? RDD::TEXTURE_LAYOUT_GENERAL : RDD::TEXTURE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL);
  1871. } else {
  1872. description.store_op = RDD::ATTACHMENT_STORE_OP_DONT_CARE;
  1873. description.stencil_store_op = RDD::ATTACHMENT_STORE_OP_DONT_CARE;
  1874. description.final_layout = RDD::TEXTURE_LAYOUT_UNDEFINED; // Don't care what is there.
  1875. }
  1876. } break;
  1877. case FINAL_ACTION_CONTINUE: {
  1878. if ((p_attachments[i].usage_flags & TEXTURE_USAGE_COLOR_ATTACHMENT_BIT)) {
  1879. description.store_op = RDD::ATTACHMENT_STORE_OP_STORE;
  1880. description.stencil_store_op = RDD::ATTACHMENT_STORE_OP_DONT_CARE;
  1881. description.final_layout = RDD::TEXTURE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
  1882. } else if ((p_attachments[i].usage_flags & TEXTURE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT)) {
  1883. description.store_op = RDD::ATTACHMENT_STORE_OP_STORE;
  1884. description.stencil_store_op = RDD::ATTACHMENT_STORE_OP_STORE;
  1885. description.final_layout = RDD::TEXTURE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL;
  1886. } else {
  1887. description.store_op = RDD::ATTACHMENT_STORE_OP_DONT_CARE;
  1888. description.stencil_store_op = RDD::ATTACHMENT_STORE_OP_DONT_CARE;
  1889. description.final_layout = RDD::TEXTURE_LAYOUT_UNDEFINED; // Don't care what is there.
  1890. }
  1891. } break;
  1892. default: {
  1893. ERR_FAIL_V(RDD::RenderPassID()); // Should never reach here.
  1894. }
  1895. }
  1896. }
  1897. attachment_last_pass[i] = -1;
  1898. attachment_remap.push_back(attachments.size());
  1899. attachments.push_back(description);
  1900. }
  1901. LocalVector<RDD::Subpass> subpasses;
  1902. subpasses.resize(p_passes.size());
  1903. LocalVector<RDD::SubpassDependency> subpass_dependencies;
  1904. for (int i = 0; i < p_passes.size(); i++) {
  1905. const FramebufferPass *pass = &p_passes[i];
  1906. RDD::Subpass &subpass = subpasses[i];
  1907. TextureSamples texture_samples = TEXTURE_SAMPLES_1;
  1908. bool is_multisample_first = true;
  1909. for (int j = 0; j < pass->color_attachments.size(); j++) {
  1910. int32_t attachment = pass->color_attachments[j];
  1911. RDD::AttachmentReference reference;
  1912. if (attachment == ATTACHMENT_UNUSED) {
  1913. reference.attachment = RDD::AttachmentReference::UNUSED;
  1914. reference.layout = RDD::TEXTURE_LAYOUT_UNDEFINED;
  1915. } else {
  1916. ERR_FAIL_INDEX_V_MSG(attachment, p_attachments.size(), RDD::RenderPassID(), "Invalid framebuffer format attachment(" + itos(attachment) + "), in pass (" + itos(i) + "), color attachment (" + itos(j) + ").");
  1917. ERR_FAIL_COND_V_MSG(!(p_attachments[attachment].usage_flags & TEXTURE_USAGE_COLOR_ATTACHMENT_BIT), RDD::RenderPassID(), "Invalid framebuffer format attachment(" + itos(attachment) + "), in pass (" + itos(i) + "), it's marked as depth, but it's not usable as color attachment.");
  1918. ERR_FAIL_COND_V_MSG(attachment_last_pass[attachment] == i, RDD::RenderPassID(), "Invalid framebuffer format attachment(" + itos(attachment) + "), in pass (" + itos(i) + "), it already was used for something else before in this pass.");
  1919. if (is_multisample_first) {
  1920. texture_samples = p_attachments[attachment].samples;
  1921. is_multisample_first = false;
  1922. } else {
  1923. ERR_FAIL_COND_V_MSG(texture_samples != p_attachments[attachment].samples, RDD::RenderPassID(), "Invalid framebuffer format attachment(" + itos(attachment) + "), in pass (" + itos(i) + "), if an attachment is marked as multisample, all of them should be multisample and use the same number of samples.");
  1924. }
  1925. reference.attachment = attachment_remap[attachment];
  1926. reference.layout = RDD::TEXTURE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
  1927. attachment_last_pass[attachment] = i;
  1928. }
  1929. reference.aspect = RDD::TEXTURE_ASPECT_COLOR_BIT;
  1930. subpass.color_references.push_back(reference);
  1931. }
  1932. for (int j = 0; j < pass->input_attachments.size(); j++) {
  1933. int32_t attachment = pass->input_attachments[j];
  1934. RDD::AttachmentReference reference;
  1935. if (attachment == ATTACHMENT_UNUSED) {
  1936. reference.attachment = RDD::AttachmentReference::UNUSED;
  1937. reference.layout = RDD::TEXTURE_LAYOUT_UNDEFINED;
  1938. } else {
  1939. ERR_FAIL_INDEX_V_MSG(attachment, p_attachments.size(), RDD::RenderPassID(), "Invalid framebuffer format attachment(" + itos(attachment) + "), in pass (" + itos(i) + "), input attachment (" + itos(j) + ").");
  1940. ERR_FAIL_COND_V_MSG(!(p_attachments[attachment].usage_flags & TEXTURE_USAGE_INPUT_ATTACHMENT_BIT), RDD::RenderPassID(), "Invalid framebuffer format attachment(" + itos(attachment) + "), in pass (" + itos(i) + "), it isn't marked as an input texture.");
  1941. ERR_FAIL_COND_V_MSG(attachment_last_pass[attachment] == i, RDD::RenderPassID(), "Invalid framebuffer format attachment(" + itos(attachment) + "), in pass (" + itos(i) + "), it already was used for something else before in this pass.");
  1942. reference.attachment = attachment_remap[attachment];
  1943. reference.layout = RDD::TEXTURE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
  1944. attachment_last_pass[attachment] = i;
  1945. }
  1946. reference.aspect = RDD::TEXTURE_ASPECT_COLOR_BIT;
  1947. subpass.input_references.push_back(reference);
  1948. }
  1949. if (pass->resolve_attachments.size() > 0) {
  1950. ERR_FAIL_COND_V_MSG(pass->resolve_attachments.size() != pass->color_attachments.size(), RDD::RenderPassID(), "The amount of resolve attachments (" + itos(pass->resolve_attachments.size()) + ") must match the number of color attachments (" + itos(pass->color_attachments.size()) + ").");
  1951. ERR_FAIL_COND_V_MSG(texture_samples == TEXTURE_SAMPLES_1, RDD::RenderPassID(), "Resolve attachments specified, but color attachments are not multisample.");
  1952. }
  1953. for (int j = 0; j < pass->resolve_attachments.size(); j++) {
  1954. int32_t attachment = pass->resolve_attachments[j];
  1955. RDD::AttachmentReference reference;
  1956. if (attachment == ATTACHMENT_UNUSED) {
  1957. reference.attachment = RDD::AttachmentReference::UNUSED;
  1958. reference.layout = RDD::TEXTURE_LAYOUT_UNDEFINED;
  1959. } else {
  1960. ERR_FAIL_INDEX_V_MSG(attachment, p_attachments.size(), RDD::RenderPassID(), "Invalid framebuffer format attachment(" + itos(attachment) + "), in pass (" + itos(i) + "), resolve attachment (" + itos(j) + ").");
  1961. ERR_FAIL_COND_V_MSG(pass->color_attachments[j] == ATTACHMENT_UNUSED, RDD::RenderPassID(), "Invalid framebuffer format attachment(" + itos(attachment) + "), in pass (" + itos(i) + "), resolve attachment (" + itos(j) + "), the respective color attachment is marked as unused.");
  1962. ERR_FAIL_COND_V_MSG(!(p_attachments[attachment].usage_flags & TEXTURE_USAGE_COLOR_ATTACHMENT_BIT), RDD::RenderPassID(), "Invalid framebuffer format attachment(" + itos(attachment) + "), in pass (" + itos(i) + "), resolve attachment, it isn't marked as a color texture.");
  1963. ERR_FAIL_COND_V_MSG(attachment_last_pass[attachment] == i, RDD::RenderPassID(), "Invalid framebuffer format attachment(" + itos(attachment) + "), in pass (" + itos(i) + "), it already was used for something else before in this pass.");
  1964. bool multisample = p_attachments[attachment].samples > TEXTURE_SAMPLES_1;
  1965. ERR_FAIL_COND_V_MSG(multisample, RDD::RenderPassID(), "Invalid framebuffer format attachment(" + itos(attachment) + "), in pass (" + itos(i) + "), resolve attachments can't be multisample.");
  1966. reference.attachment = attachment_remap[attachment];
  1967. reference.layout = RDD::TEXTURE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL; // RDD::TEXTURE_LAYOUT_SHADER_READ_ONLY_OPTIMAL
  1968. attachment_last_pass[attachment] = i;
  1969. }
  1970. reference.aspect = RDD::TEXTURE_ASPECT_COLOR_BIT;
  1971. subpass.resolve_references.push_back(reference);
  1972. }
  1973. if (pass->depth_attachment != ATTACHMENT_UNUSED) {
  1974. int32_t attachment = pass->depth_attachment;
  1975. ERR_FAIL_INDEX_V_MSG(attachment, p_attachments.size(), RDD::RenderPassID(), "Invalid framebuffer depth format attachment(" + itos(attachment) + "), in pass (" + itos(i) + "), depth attachment.");
  1976. ERR_FAIL_COND_V_MSG(!(p_attachments[attachment].usage_flags & TEXTURE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT), RDD::RenderPassID(), "Invalid framebuffer depth format attachment(" + itos(attachment) + "), in pass (" + itos(i) + "), it's marked as depth, but it's not a depth attachment.");
  1977. ERR_FAIL_COND_V_MSG(attachment_last_pass[attachment] == i, RDD::RenderPassID(), "Invalid framebuffer depth format attachment(" + itos(attachment) + "), in pass (" + itos(i) + "), it already was used for something else before in this pass.");
  1978. subpass.depth_stencil_reference.attachment = attachment_remap[attachment];
  1979. subpass.depth_stencil_reference.layout = RDD::TEXTURE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL;
  1980. attachment_last_pass[attachment] = i;
  1981. if (is_multisample_first) {
  1982. texture_samples = p_attachments[attachment].samples;
  1983. is_multisample_first = false;
  1984. } else {
  1985. ERR_FAIL_COND_V_MSG(texture_samples != p_attachments[attachment].samples, RDD::RenderPassID(), "Invalid framebuffer depth format attachment(" + itos(attachment) + "), in pass (" + itos(i) + "), if an attachment is marked as multisample, all of them should be multisample and use the same number of samples including the depth.");
  1986. }
  1987. } else {
  1988. subpass.depth_stencil_reference.attachment = RDD::AttachmentReference::UNUSED;
  1989. subpass.depth_stencil_reference.layout = RDD::TEXTURE_LAYOUT_UNDEFINED;
  1990. }
  1991. if (pass->vrs_attachment != ATTACHMENT_UNUSED) {
  1992. int32_t attachment = pass->vrs_attachment;
  1993. ERR_FAIL_INDEX_V_MSG(attachment, p_attachments.size(), RDD::RenderPassID(), "Invalid framebuffer VRS format attachment(" + itos(attachment) + "), in pass (" + itos(i) + "), VRS attachment.");
  1994. ERR_FAIL_COND_V_MSG(!(p_attachments[attachment].usage_flags & TEXTURE_USAGE_VRS_ATTACHMENT_BIT), RDD::RenderPassID(), "Invalid framebuffer VRS format attachment(" + itos(attachment) + "), in pass (" + itos(i) + "), it's marked as VRS, but it's not a VRS attachment.");
  1995. ERR_FAIL_COND_V_MSG(attachment_last_pass[attachment] == i, RDD::RenderPassID(), "Invalid framebuffer VRS attachment(" + itos(attachment) + "), in pass (" + itos(i) + "), it already was used for something else before in this pass.");
  1996. subpass.vrs_reference.attachment = attachment_remap[attachment];
  1997. subpass.vrs_reference.layout = RDD::TEXTURE_LAYOUT_VRS_ATTACHMENT_OPTIMAL;
  1998. attachment_last_pass[attachment] = i;
  1999. }
  2000. for (int j = 0; j < pass->preserve_attachments.size(); j++) {
  2001. int32_t attachment = pass->preserve_attachments[j];
  2002. ERR_FAIL_COND_V_MSG(attachment == ATTACHMENT_UNUSED, RDD::RenderPassID(), "Invalid framebuffer format attachment(" + itos(attachment) + "), in pass (" + itos(i) + "), preserve attachment (" + itos(j) + "). Preserve attachments can't be unused.");
  2003. ERR_FAIL_INDEX_V_MSG(attachment, p_attachments.size(), RDD::RenderPassID(), "Invalid framebuffer format attachment(" + itos(attachment) + "), in pass (" + itos(i) + "), preserve attachment (" + itos(j) + ").");
  2004. if (attachment_last_pass[attachment] != i) {
  2005. // Preserve can still be used to keep depth or color from being discarded after use.
  2006. attachment_last_pass[attachment] = i;
  2007. subpasses[i].preserve_attachments.push_back(attachment);
  2008. }
  2009. }
  2010. if (r_samples) {
  2011. r_samples->push_back(texture_samples);
  2012. }
  2013. if (i > 0) {
  2014. RDD::SubpassDependency dependency;
  2015. dependency.src_subpass = i - 1;
  2016. dependency.dst_subpass = i;
  2017. dependency.src_stages = (RDD::PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT | RDD::PIPELINE_STAGE_EARLY_FRAGMENT_TESTS_BIT | RDD::PIPELINE_STAGE_LATE_FRAGMENT_TESTS_BIT);
  2018. dependency.dst_stages = (RDD::PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT | RDD::PIPELINE_STAGE_EARLY_FRAGMENT_TESTS_BIT | RDD::PIPELINE_STAGE_LATE_FRAGMENT_TESTS_BIT | RDD::PIPELINE_STAGE_FRAGMENT_SHADER_BIT);
  2019. dependency.src_access = (RDD::BARRIER_ACCESS_COLOR_ATTACHMENT_WRITE_BIT | RDD::BARRIER_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT);
  2020. dependency.dst_access = (RDD::BARRIER_ACCESS_COLOR_ATTACHMENT_READ_BIT | RDD::BARRIER_ACCESS_COLOR_ATTACHMENT_WRITE_BIT | RDD::BARRIER_ACCESS_DEPTH_STENCIL_ATTACHMENT_READ_BIT | RDD::BARRIER_ACCESS_INPUT_ATTACHMENT_READ_BIT);
  2021. subpass_dependencies.push_back(dependency);
  2022. }
  2023. }
  2024. RDD::RenderPassID render_pass = driver->render_pass_create(attachments, subpasses, subpass_dependencies, p_view_count);
  2025. ERR_FAIL_COND_V(!render_pass, RDD::RenderPassID());
  2026. return render_pass;
  2027. }
  2028. RenderingDevice::FramebufferFormatID RenderingDevice::framebuffer_format_create(const Vector<AttachmentFormat> &p_format, uint32_t p_view_count) {
  2029. FramebufferPass pass;
  2030. for (int i = 0; i < p_format.size(); i++) {
  2031. if (p_format[i].usage_flags & TEXTURE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT) {
  2032. pass.depth_attachment = i;
  2033. } else {
  2034. pass.color_attachments.push_back(i);
  2035. }
  2036. }
  2037. Vector<FramebufferPass> passes;
  2038. passes.push_back(pass);
  2039. return framebuffer_format_create_multipass(p_format, passes, p_view_count);
  2040. }
  2041. RenderingDevice::FramebufferFormatID RenderingDevice::framebuffer_format_create_multipass(const Vector<AttachmentFormat> &p_attachments, const Vector<FramebufferPass> &p_passes, uint32_t p_view_count) {
  2042. _THREAD_SAFE_METHOD_
  2043. FramebufferFormatKey key;
  2044. key.attachments = p_attachments;
  2045. key.passes = p_passes;
  2046. key.view_count = p_view_count;
  2047. const RBMap<FramebufferFormatKey, FramebufferFormatID>::Element *E = framebuffer_format_cache.find(key);
  2048. if (E) {
  2049. // Exists, return.
  2050. return E->get();
  2051. }
  2052. Vector<TextureSamples> samples;
  2053. RDD::RenderPassID render_pass = _render_pass_create(p_attachments, p_passes, INITIAL_ACTION_CLEAR, FINAL_ACTION_READ, INITIAL_ACTION_CLEAR, FINAL_ACTION_READ, p_view_count, &samples); // Actions don't matter for this use case.
  2054. if (!render_pass) { // Was likely invalid.
  2055. return INVALID_ID;
  2056. }
  2057. FramebufferFormatID id = FramebufferFormatID(framebuffer_format_cache.size()) | (FramebufferFormatID(ID_TYPE_FRAMEBUFFER_FORMAT) << FramebufferFormatID(ID_BASE_SHIFT));
  2058. E = framebuffer_format_cache.insert(key, id);
  2059. FramebufferFormat fb_format;
  2060. fb_format.E = E;
  2061. fb_format.render_pass = render_pass;
  2062. fb_format.pass_samples = samples;
  2063. fb_format.view_count = p_view_count;
  2064. framebuffer_formats[id] = fb_format;
  2065. return id;
  2066. }
  2067. RenderingDevice::FramebufferFormatID RenderingDevice::framebuffer_format_create_empty(TextureSamples p_samples) {
  2068. FramebufferFormatKey key;
  2069. key.passes.push_back(FramebufferPass());
  2070. const RBMap<FramebufferFormatKey, FramebufferFormatID>::Element *E = framebuffer_format_cache.find(key);
  2071. if (E) {
  2072. // Exists, return.
  2073. return E->get();
  2074. }
  2075. LocalVector<RDD::Subpass> subpass;
  2076. subpass.resize(1);
  2077. RDD::RenderPassID render_pass = driver->render_pass_create({}, subpass, {}, 1);
  2078. ERR_FAIL_COND_V(!render_pass, FramebufferFormatID());
  2079. FramebufferFormatID id = FramebufferFormatID(framebuffer_format_cache.size()) | (FramebufferFormatID(ID_TYPE_FRAMEBUFFER_FORMAT) << FramebufferFormatID(ID_BASE_SHIFT));
  2080. E = framebuffer_format_cache.insert(key, id);
  2081. FramebufferFormat fb_format;
  2082. fb_format.E = E;
  2083. fb_format.render_pass = render_pass;
  2084. fb_format.pass_samples.push_back(p_samples);
  2085. framebuffer_formats[id] = fb_format;
  2086. return id;
  2087. }
  2088. RenderingDevice::TextureSamples RenderingDevice::framebuffer_format_get_texture_samples(FramebufferFormatID p_format, uint32_t p_pass) {
  2089. HashMap<FramebufferFormatID, FramebufferFormat>::Iterator E = framebuffer_formats.find(p_format);
  2090. ERR_FAIL_COND_V(!E, TEXTURE_SAMPLES_1);
  2091. ERR_FAIL_COND_V(p_pass >= uint32_t(E->value.pass_samples.size()), TEXTURE_SAMPLES_1);
  2092. return E->value.pass_samples[p_pass];
  2093. }
  2094. RID RenderingDevice::framebuffer_create_empty(const Size2i &p_size, TextureSamples p_samples, FramebufferFormatID p_format_check) {
  2095. _THREAD_SAFE_METHOD_
  2096. Framebuffer framebuffer;
  2097. framebuffer.format_id = framebuffer_format_create_empty(p_samples);
  2098. ERR_FAIL_COND_V(p_format_check != INVALID_FORMAT_ID && framebuffer.format_id != p_format_check, RID());
  2099. framebuffer.size = p_size;
  2100. framebuffer.view_count = 1;
  2101. RID id = framebuffer_owner.make_rid(framebuffer);
  2102. #ifdef DEV_ENABLED
  2103. set_resource_name(id, "RID:" + itos(id.get_id()));
  2104. #endif
  2105. return id;
  2106. }
  2107. RID RenderingDevice::framebuffer_create(const Vector<RID> &p_texture_attachments, FramebufferFormatID p_format_check, uint32_t p_view_count) {
  2108. _THREAD_SAFE_METHOD_
  2109. FramebufferPass pass;
  2110. for (int i = 0; i < p_texture_attachments.size(); i++) {
  2111. Texture *texture = texture_owner.get_or_null(p_texture_attachments[i]);
  2112. ERR_FAIL_COND_V_MSG(texture && texture->layers != p_view_count, RID(), "Layers of our texture doesn't match view count for this framebuffer");
  2113. if (texture && texture->usage_flags & TEXTURE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT) {
  2114. pass.depth_attachment = i;
  2115. } else if (texture && texture->usage_flags & TEXTURE_USAGE_VRS_ATTACHMENT_BIT) {
  2116. pass.vrs_attachment = i;
  2117. } else {
  2118. if (texture && texture->is_resolve_buffer) {
  2119. pass.resolve_attachments.push_back(i);
  2120. } else {
  2121. pass.color_attachments.push_back(texture ? i : ATTACHMENT_UNUSED);
  2122. }
  2123. }
  2124. }
  2125. Vector<FramebufferPass> passes;
  2126. passes.push_back(pass);
  2127. return framebuffer_create_multipass(p_texture_attachments, passes, p_format_check, p_view_count);
  2128. }
  2129. RID RenderingDevice::framebuffer_create_multipass(const Vector<RID> &p_texture_attachments, const Vector<FramebufferPass> &p_passes, FramebufferFormatID p_format_check, uint32_t p_view_count) {
  2130. _THREAD_SAFE_METHOD_
  2131. Vector<AttachmentFormat> attachments;
  2132. attachments.resize(p_texture_attachments.size());
  2133. Size2i size;
  2134. bool size_set = false;
  2135. for (int i = 0; i < p_texture_attachments.size(); i++) {
  2136. AttachmentFormat af;
  2137. Texture *texture = texture_owner.get_or_null(p_texture_attachments[i]);
  2138. if (!texture) {
  2139. af.usage_flags = AttachmentFormat::UNUSED_ATTACHMENT;
  2140. } else {
  2141. ERR_FAIL_COND_V_MSG(texture->layers != p_view_count, RID(), "Layers of our texture doesn't match view count for this framebuffer");
  2142. if (!size_set) {
  2143. size.width = texture->width;
  2144. size.height = texture->height;
  2145. size_set = true;
  2146. } else if (texture->usage_flags & TEXTURE_USAGE_VRS_ATTACHMENT_BIT) {
  2147. // If this is not the first attachment we assume this is used as the VRS attachment.
  2148. // In this case this texture will be 1/16th the size of the color attachment.
  2149. // So we skip the size check.
  2150. } else {
  2151. ERR_FAIL_COND_V_MSG((uint32_t)size.width != texture->width || (uint32_t)size.height != texture->height, RID(),
  2152. "All textures in a framebuffer should be the same size.");
  2153. }
  2154. af.format = texture->format;
  2155. af.samples = texture->samples;
  2156. af.usage_flags = texture->usage_flags;
  2157. }
  2158. attachments.write[i] = af;
  2159. }
  2160. ERR_FAIL_COND_V_MSG(!size_set, RID(), "All attachments unused.");
  2161. FramebufferFormatID format_id = framebuffer_format_create_multipass(attachments, p_passes, p_view_count);
  2162. if (format_id == INVALID_ID) {
  2163. return RID();
  2164. }
  2165. ERR_FAIL_COND_V_MSG(p_format_check != INVALID_ID && format_id != p_format_check, RID(),
  2166. "The format used to check this framebuffer differs from the intended framebuffer format.");
  2167. Framebuffer framebuffer;
  2168. framebuffer.format_id = format_id;
  2169. framebuffer.texture_ids = p_texture_attachments;
  2170. framebuffer.size = size;
  2171. framebuffer.view_count = p_view_count;
  2172. RID id = framebuffer_owner.make_rid(framebuffer);
  2173. #ifdef DEV_ENABLED
  2174. set_resource_name(id, "RID:" + itos(id.get_id()));
  2175. #endif
  2176. for (int i = 0; i < p_texture_attachments.size(); i++) {
  2177. if (p_texture_attachments[i].is_valid()) {
  2178. _add_dependency(id, p_texture_attachments[i]);
  2179. }
  2180. }
  2181. return id;
  2182. }
  2183. RenderingDevice::FramebufferFormatID RenderingDevice::framebuffer_get_format(RID p_framebuffer) {
  2184. _THREAD_SAFE_METHOD_
  2185. Framebuffer *framebuffer = framebuffer_owner.get_or_null(p_framebuffer);
  2186. ERR_FAIL_NULL_V(framebuffer, INVALID_ID);
  2187. return framebuffer->format_id;
  2188. }
  2189. bool RenderingDevice::framebuffer_is_valid(RID p_framebuffer) const {
  2190. _THREAD_SAFE_METHOD_
  2191. return framebuffer_owner.owns(p_framebuffer);
  2192. }
  2193. void RenderingDevice::framebuffer_set_invalidation_callback(RID p_framebuffer, InvalidationCallback p_callback, void *p_userdata) {
  2194. _THREAD_SAFE_METHOD_
  2195. Framebuffer *framebuffer = framebuffer_owner.get_or_null(p_framebuffer);
  2196. ERR_FAIL_COND(!framebuffer);
  2197. framebuffer->invalidated_callback = p_callback;
  2198. framebuffer->invalidated_callback_userdata = p_userdata;
  2199. }
  2200. /*****************/
  2201. /**** SAMPLER ****/
  2202. /*****************/
  2203. RID RenderingDevice::sampler_create(const SamplerState &p_state) {
  2204. _THREAD_SAFE_METHOD_
  2205. ERR_FAIL_INDEX_V(p_state.repeat_u, SAMPLER_REPEAT_MODE_MAX, RID());
  2206. ERR_FAIL_INDEX_V(p_state.repeat_v, SAMPLER_REPEAT_MODE_MAX, RID());
  2207. ERR_FAIL_INDEX_V(p_state.repeat_w, SAMPLER_REPEAT_MODE_MAX, RID());
  2208. ERR_FAIL_INDEX_V(p_state.compare_op, COMPARE_OP_MAX, RID());
  2209. ERR_FAIL_INDEX_V(p_state.border_color, SAMPLER_BORDER_COLOR_MAX, RID());
  2210. RDD::SamplerID sampler = driver->sampler_create(p_state);
  2211. ERR_FAIL_COND_V(!sampler, RID());
  2212. RID id = sampler_owner.make_rid(sampler);
  2213. #ifdef DEV_ENABLED
  2214. set_resource_name(id, "RID:" + itos(id.get_id()));
  2215. #endif
  2216. return id;
  2217. }
  2218. bool RenderingDevice::sampler_is_format_supported_for_filter(DataFormat p_format, SamplerFilter p_sampler_filter) const {
  2219. ERR_FAIL_INDEX_V(p_format, DATA_FORMAT_MAX, false);
  2220. _THREAD_SAFE_METHOD_
  2221. return driver->sampler_is_format_supported_for_filter(p_format, p_sampler_filter);
  2222. }
  2223. /***********************/
  2224. /**** VERTEX BUFFER ****/
  2225. /***********************/
  2226. RID RenderingDevice::vertex_buffer_create(uint32_t p_size_bytes, const Vector<uint8_t> &p_data, bool p_use_as_storage) {
  2227. _THREAD_SAFE_METHOD_
  2228. ERR_FAIL_COND_V(p_data.size() && (uint32_t)p_data.size() != p_size_bytes, RID());
  2229. Buffer buffer;
  2230. buffer.size = p_size_bytes;
  2231. buffer.usage = RDD::BUFFER_USAGE_TRANSFER_FROM_BIT | RDD::BUFFER_USAGE_TRANSFER_TO_BIT | RDD::BUFFER_USAGE_VERTEX_BIT;
  2232. if (p_use_as_storage) {
  2233. buffer.usage.set_flag(RDD::BUFFER_USAGE_STORAGE_BIT);
  2234. }
  2235. buffer.driver_id = driver->buffer_create(buffer.size, buffer.usage, RDD::MEMORY_ALLOCATION_TYPE_GPU);
  2236. ERR_FAIL_COND_V(!buffer.driver_id, RID());
  2237. if (p_data.size()) {
  2238. uint64_t data_size = p_data.size();
  2239. const uint8_t *r = p_data.ptr();
  2240. _buffer_update(&buffer, 0, r, data_size);
  2241. if (driver->api_trait_get(RDD::API_TRAIT_HONORS_PIPELINE_BARRIERS)) {
  2242. RDD::BufferBarrier bb;
  2243. bb.buffer = buffer.driver_id;
  2244. bb.src_access = RDD::BARRIER_ACCESS_TRANSFER_WRITE_BIT;
  2245. bb.dst_access = RDD::BARRIER_ACCESS_VERTEX_ATTRIBUTE_READ_BIT;
  2246. bb.size = data_size;
  2247. driver->command_pipeline_barrier(frames[frame].setup_command_buffer, RDD::PIPELINE_STAGE_TRANSFER_BIT, RDD::PIPELINE_STAGE_VERTEX_INPUT_BIT, {}, bb, {});
  2248. }
  2249. }
  2250. buffer_memory += buffer.size;
  2251. RID id = vertex_buffer_owner.make_rid(buffer);
  2252. #ifdef DEV_ENABLED
  2253. set_resource_name(id, "RID:" + itos(id.get_id()));
  2254. #endif
  2255. return id;
  2256. }
  2257. // Internally reference counted, this ID is warranted to be unique for the same description, but needs to be freed as many times as it was allocated.
  2258. RenderingDevice::VertexFormatID RenderingDevice::vertex_format_create(const Vector<VertexAttribute> &p_vertex_descriptions) {
  2259. _THREAD_SAFE_METHOD_
  2260. VertexDescriptionKey key;
  2261. key.vertex_formats = p_vertex_descriptions;
  2262. VertexFormatID *idptr = vertex_format_cache.getptr(key);
  2263. if (idptr) {
  2264. return *idptr;
  2265. }
  2266. HashSet<int> used_locations;
  2267. for (int i = 0; i < p_vertex_descriptions.size(); i++) {
  2268. ERR_CONTINUE(p_vertex_descriptions[i].format >= DATA_FORMAT_MAX);
  2269. ERR_FAIL_COND_V(used_locations.has(p_vertex_descriptions[i].location), INVALID_ID);
  2270. ERR_FAIL_COND_V_MSG(get_format_vertex_size(p_vertex_descriptions[i].format) == 0, INVALID_ID,
  2271. "Data format for attachment (" + itos(i) + "), '" + FORMAT_NAMES[p_vertex_descriptions[i].format] + "', is not valid for a vertex array.");
  2272. used_locations.insert(p_vertex_descriptions[i].location);
  2273. }
  2274. RDD::VertexFormatID driver_id = driver->vertex_format_create(p_vertex_descriptions);
  2275. ERR_FAIL_COND_V(!driver_id, 0);
  2276. VertexFormatID id = (vertex_format_cache.size() | ((int64_t)ID_TYPE_VERTEX_FORMAT << ID_BASE_SHIFT));
  2277. vertex_format_cache[key] = id;
  2278. vertex_formats[id].vertex_formats = p_vertex_descriptions;
  2279. vertex_formats[id].driver_id = driver_id;
  2280. return id;
  2281. }
  2282. RID RenderingDevice::vertex_array_create(uint32_t p_vertex_count, VertexFormatID p_vertex_format, const Vector<RID> &p_src_buffers, const Vector<uint64_t> &p_offsets) {
  2283. _THREAD_SAFE_METHOD_
  2284. ERR_FAIL_COND_V(!vertex_formats.has(p_vertex_format), RID());
  2285. const VertexDescriptionCache &vd = vertex_formats[p_vertex_format];
  2286. ERR_FAIL_COND_V(vd.vertex_formats.size() != p_src_buffers.size(), RID());
  2287. for (int i = 0; i < p_src_buffers.size(); i++) {
  2288. ERR_FAIL_COND_V(!vertex_buffer_owner.owns(p_src_buffers[i]), RID());
  2289. }
  2290. VertexArray vertex_array;
  2291. if (p_offsets.is_empty()) {
  2292. vertex_array.offsets.resize_zeroed(p_src_buffers.size());
  2293. } else {
  2294. ERR_FAIL_COND_V(p_offsets.size() != p_src_buffers.size(), RID());
  2295. vertex_array.offsets = p_offsets;
  2296. }
  2297. vertex_array.vertex_count = p_vertex_count;
  2298. vertex_array.description = p_vertex_format;
  2299. vertex_array.max_instances_allowed = 0xFFFFFFFF; // By default as many as you want.
  2300. for (int i = 0; i < p_src_buffers.size(); i++) {
  2301. Buffer *buffer = vertex_buffer_owner.get_or_null(p_src_buffers[i]);
  2302. // Validate with buffer.
  2303. {
  2304. const VertexAttribute &atf = vd.vertex_formats[i];
  2305. uint32_t element_size = get_format_vertex_size(atf.format);
  2306. ERR_FAIL_COND_V(element_size == 0, RID()); // Should never happens since this was prevalidated.
  2307. if (atf.frequency == VERTEX_FREQUENCY_VERTEX) {
  2308. // Validate size for regular drawing.
  2309. uint64_t total_size = uint64_t(atf.stride) * (p_vertex_count - 1) + atf.offset + element_size;
  2310. ERR_FAIL_COND_V_MSG(total_size > buffer->size, RID(),
  2311. "Attachment (" + itos(i) + ") will read past the end of the buffer.");
  2312. } else {
  2313. // Validate size for instances drawing.
  2314. uint64_t available = buffer->size - atf.offset;
  2315. ERR_FAIL_COND_V_MSG(available < element_size, RID(),
  2316. "Attachment (" + itos(i) + ") uses instancing, but it's just too small.");
  2317. uint32_t instances_allowed = available / atf.stride;
  2318. vertex_array.max_instances_allowed = MIN(instances_allowed, vertex_array.max_instances_allowed);
  2319. }
  2320. }
  2321. vertex_array.buffers.push_back(buffer->driver_id);
  2322. }
  2323. RID id = vertex_array_owner.make_rid(vertex_array);
  2324. for (int i = 0; i < p_src_buffers.size(); i++) {
  2325. _add_dependency(id, p_src_buffers[i]);
  2326. }
  2327. return id;
  2328. }
  2329. RID RenderingDevice::index_buffer_create(uint32_t p_index_count, IndexBufferFormat p_format, const Vector<uint8_t> &p_data, bool p_use_restart_indices) {
  2330. _THREAD_SAFE_METHOD_
  2331. ERR_FAIL_COND_V(p_index_count == 0, RID());
  2332. IndexBuffer index_buffer;
  2333. index_buffer.format = p_format;
  2334. index_buffer.supports_restart_indices = p_use_restart_indices;
  2335. index_buffer.index_count = p_index_count;
  2336. uint32_t size_bytes = p_index_count * ((p_format == INDEX_BUFFER_FORMAT_UINT16) ? 2 : 4);
  2337. #ifdef DEBUG_ENABLED
  2338. if (p_data.size()) {
  2339. index_buffer.max_index = 0;
  2340. ERR_FAIL_COND_V_MSG((uint32_t)p_data.size() != size_bytes, RID(),
  2341. "Default index buffer initializer array size (" + itos(p_data.size()) + ") does not match format required size (" + itos(size_bytes) + ").");
  2342. const uint8_t *r = p_data.ptr();
  2343. if (p_format == INDEX_BUFFER_FORMAT_UINT16) {
  2344. const uint16_t *index16 = (const uint16_t *)r;
  2345. for (uint32_t i = 0; i < p_index_count; i++) {
  2346. if (p_use_restart_indices && index16[i] == 0xFFFF) {
  2347. continue; // Restart index, ignore.
  2348. }
  2349. index_buffer.max_index = MAX(index16[i], index_buffer.max_index);
  2350. }
  2351. } else {
  2352. const uint32_t *index32 = (const uint32_t *)r;
  2353. for (uint32_t i = 0; i < p_index_count; i++) {
  2354. if (p_use_restart_indices && index32[i] == 0xFFFFFFFF) {
  2355. continue; // Restart index, ignore.
  2356. }
  2357. index_buffer.max_index = MAX(index32[i], index_buffer.max_index);
  2358. }
  2359. }
  2360. } else {
  2361. index_buffer.max_index = 0xFFFFFFFF;
  2362. }
  2363. #else
  2364. index_buffer.max_index = 0xFFFFFFFF;
  2365. #endif
  2366. index_buffer.size = size_bytes;
  2367. index_buffer.usage = (RDD::BUFFER_USAGE_TRANSFER_FROM_BIT | RDD::BUFFER_USAGE_TRANSFER_TO_BIT | RDD::BUFFER_USAGE_INDEX_BIT);
  2368. index_buffer.driver_id = driver->buffer_create(index_buffer.size, index_buffer.usage, RDD::MEMORY_ALLOCATION_TYPE_GPU);
  2369. ERR_FAIL_COND_V(!index_buffer.driver_id, RID());
  2370. if (p_data.size()) {
  2371. uint64_t data_size = p_data.size();
  2372. const uint8_t *r = p_data.ptr();
  2373. _buffer_update(&index_buffer, 0, r, data_size);
  2374. if (driver->api_trait_get(RDD::API_TRAIT_HONORS_PIPELINE_BARRIERS)) {
  2375. RDD::BufferBarrier bb;
  2376. bb.buffer = index_buffer.driver_id;
  2377. bb.src_access = RDD::BARRIER_ACCESS_TRANSFER_WRITE_BIT;
  2378. bb.dst_access = RDD::BARRIER_ACCESS_INDEX_READ_BIT;
  2379. bb.size = data_size;
  2380. driver->command_pipeline_barrier(frames[frame].setup_command_buffer, RDD::PIPELINE_STAGE_TRANSFER_BIT, RDD::PIPELINE_STAGE_VERTEX_INPUT_BIT, {}, bb, {});
  2381. }
  2382. }
  2383. buffer_memory += index_buffer.size;
  2384. RID id = index_buffer_owner.make_rid(index_buffer);
  2385. #ifdef DEV_ENABLED
  2386. set_resource_name(id, "RID:" + itos(id.get_id()));
  2387. #endif
  2388. return id;
  2389. }
  2390. RID RenderingDevice::index_array_create(RID p_index_buffer, uint32_t p_index_offset, uint32_t p_index_count) {
  2391. _THREAD_SAFE_METHOD_
  2392. ERR_FAIL_COND_V(!index_buffer_owner.owns(p_index_buffer), RID());
  2393. IndexBuffer *index_buffer = index_buffer_owner.get_or_null(p_index_buffer);
  2394. ERR_FAIL_COND_V(p_index_count == 0, RID());
  2395. ERR_FAIL_COND_V(p_index_offset + p_index_count > index_buffer->index_count, RID());
  2396. IndexArray index_array;
  2397. index_array.max_index = index_buffer->max_index;
  2398. index_array.driver_id = index_buffer->driver_id;
  2399. index_array.offset = p_index_offset;
  2400. index_array.indices = p_index_count;
  2401. index_array.format = index_buffer->format;
  2402. index_array.supports_restart_indices = index_buffer->supports_restart_indices;
  2403. RID id = index_array_owner.make_rid(index_array);
  2404. _add_dependency(id, p_index_buffer);
  2405. return id;
  2406. }
  2407. /****************/
  2408. /**** SHADER ****/
  2409. /****************/
  2410. static const char *SHADER_UNIFORM_NAMES[RenderingDevice::UNIFORM_TYPE_MAX] = {
  2411. "Sampler", "CombinedSampler", "Texture", "Image", "TextureBuffer", "SamplerTextureBuffer", "ImageBuffer", "UniformBuffer", "StorageBuffer", "InputAttachment"
  2412. };
  2413. String RenderingDevice::_shader_uniform_debug(RID p_shader, int p_set) {
  2414. String ret;
  2415. const Shader *shader = shader_owner.get_or_null(p_shader);
  2416. ERR_FAIL_NULL_V(shader, String());
  2417. for (int i = 0; i < shader->uniform_sets.size(); i++) {
  2418. if (p_set >= 0 && i != p_set) {
  2419. continue;
  2420. }
  2421. for (int j = 0; j < shader->uniform_sets[i].size(); j++) {
  2422. const ShaderUniform &ui = shader->uniform_sets[i][j];
  2423. if (!ret.is_empty()) {
  2424. ret += "\n";
  2425. }
  2426. ret += "Set: " + itos(i) + " Binding: " + itos(ui.binding) + " Type: " + SHADER_UNIFORM_NAMES[ui.type] + " Writable: " + (ui.writable ? "Y" : "N") + " Length: " + itos(ui.length);
  2427. }
  2428. }
  2429. return ret;
  2430. }
  2431. String RenderingDevice::shader_get_binary_cache_key() const {
  2432. return driver->shader_get_binary_cache_key();
  2433. }
  2434. Vector<uint8_t> RenderingDevice::shader_compile_binary_from_spirv(const Vector<ShaderStageSPIRVData> &p_spirv, const String &p_shader_name) {
  2435. return driver->shader_compile_binary_from_spirv(p_spirv, p_shader_name);
  2436. }
  2437. RID RenderingDevice::shader_create_from_bytecode(const Vector<uint8_t> &p_shader_binary, RID p_placeholder) {
  2438. _THREAD_SAFE_METHOD_
  2439. ShaderDescription shader_desc;
  2440. String name;
  2441. RDD::ShaderID shader_id = driver->shader_create_from_bytecode(p_shader_binary, shader_desc, name);
  2442. ERR_FAIL_COND_V(!shader_id, RID());
  2443. // All good, let's create modules.
  2444. RID id;
  2445. if (p_placeholder.is_null()) {
  2446. id = shader_owner.make_rid();
  2447. } else {
  2448. id = p_placeholder;
  2449. }
  2450. Shader *shader = shader_owner.get_or_null(id);
  2451. ERR_FAIL_NULL_V(shader, RID());
  2452. *((ShaderDescription *)shader) = shader_desc; // ShaderDescription bundle.
  2453. shader->name = name;
  2454. shader->driver_id = shader_id;
  2455. shader->layout_hash = driver->shader_get_layout_hash(shader_id);
  2456. for (int i = 0; i < shader->uniform_sets.size(); i++) {
  2457. uint32_t format = 0; // No format, default.
  2458. if (shader->uniform_sets[i].size()) {
  2459. // Sort and hash.
  2460. shader->uniform_sets.write[i].sort();
  2461. UniformSetFormat usformat;
  2462. usformat.uniforms = shader->uniform_sets[i];
  2463. RBMap<UniformSetFormat, uint32_t>::Element *E = uniform_set_format_cache.find(usformat);
  2464. if (E) {
  2465. format = E->get();
  2466. } else {
  2467. format = uniform_set_format_cache.size() + 1;
  2468. uniform_set_format_cache.insert(usformat, format);
  2469. }
  2470. }
  2471. shader->set_formats.push_back(format);
  2472. }
  2473. #ifdef DEV_ENABLED
  2474. set_resource_name(id, "RID:" + itos(id.get_id()));
  2475. #endif
  2476. return id;
  2477. }
  2478. RID RenderingDevice::shader_create_placeholder() {
  2479. Shader shader;
  2480. return shader_owner.make_rid(shader);
  2481. }
  2482. uint64_t RenderingDevice::shader_get_vertex_input_attribute_mask(RID p_shader) {
  2483. _THREAD_SAFE_METHOD_
  2484. const Shader *shader = shader_owner.get_or_null(p_shader);
  2485. ERR_FAIL_NULL_V(shader, 0);
  2486. return shader->vertex_input_mask;
  2487. }
  2488. /******************/
  2489. /**** UNIFORMS ****/
  2490. /******************/
  2491. RID RenderingDevice::uniform_buffer_create(uint32_t p_size_bytes, const Vector<uint8_t> &p_data) {
  2492. _THREAD_SAFE_METHOD_
  2493. ERR_FAIL_COND_V(p_data.size() && (uint32_t)p_data.size() != p_size_bytes, RID());
  2494. Buffer buffer;
  2495. buffer.size = p_size_bytes;
  2496. buffer.usage = (RDD::BUFFER_USAGE_TRANSFER_TO_BIT | RDD::BUFFER_USAGE_UNIFORM_BIT);
  2497. buffer.driver_id = driver->buffer_create(buffer.size, buffer.usage, RDD::MEMORY_ALLOCATION_TYPE_GPU);
  2498. ERR_FAIL_COND_V(!buffer.driver_id, RID());
  2499. if (p_data.size()) {
  2500. uint64_t data_size = p_data.size();
  2501. const uint8_t *r = p_data.ptr();
  2502. _buffer_update(&buffer, 0, r, data_size);
  2503. if (driver->api_trait_get(RDD::API_TRAIT_HONORS_PIPELINE_BARRIERS)) {
  2504. RDD::BufferBarrier bb;
  2505. bb.buffer = buffer.driver_id;
  2506. bb.src_access = RDD::BARRIER_ACCESS_TRANSFER_WRITE_BIT;
  2507. bb.dst_access = RDD::BARRIER_ACCESS_UNIFORM_READ_BIT;
  2508. bb.size = data_size;
  2509. driver->command_pipeline_barrier(frames[frame].setup_command_buffer, RDD::PIPELINE_STAGE_TRANSFER_BIT, RDD::PIPELINE_STAGE_VERTEX_SHADER_BIT | RDD::PIPELINE_STAGE_FRAGMENT_SHADER_BIT | RDD::PIPELINE_STAGE_COMPUTE_SHADER_BIT, {}, bb, {});
  2510. }
  2511. }
  2512. buffer_memory += buffer.size;
  2513. RID id = uniform_buffer_owner.make_rid(buffer);
  2514. #ifdef DEV_ENABLED
  2515. set_resource_name(id, "RID:" + itos(id.get_id()));
  2516. #endif
  2517. return id;
  2518. }
  2519. RID RenderingDevice::uniform_set_create(const Vector<Uniform> &p_uniforms, RID p_shader, uint32_t p_shader_set) {
  2520. _THREAD_SAFE_METHOD_
  2521. ERR_FAIL_COND_V(p_uniforms.size() == 0, RID());
  2522. Shader *shader = shader_owner.get_or_null(p_shader);
  2523. ERR_FAIL_NULL_V(shader, RID());
  2524. ERR_FAIL_COND_V_MSG(p_shader_set >= (uint32_t)shader->uniform_sets.size() || shader->uniform_sets[p_shader_set].size() == 0, RID(),
  2525. "Desired set (" + itos(p_shader_set) + ") not used by shader.");
  2526. // See that all sets in shader are satisfied.
  2527. const Vector<ShaderUniform> &set = shader->uniform_sets[p_shader_set];
  2528. uint32_t uniform_count = p_uniforms.size();
  2529. const Uniform *uniforms = p_uniforms.ptr();
  2530. uint32_t set_uniform_count = set.size();
  2531. const ShaderUniform *set_uniforms = set.ptr();
  2532. LocalVector<RDD::BoundUniform> driver_uniforms;
  2533. driver_uniforms.resize(set_uniform_count);
  2534. // Used for verification to make sure a uniform set does not use a framebuffer bound texture.
  2535. LocalVector<UniformSet::AttachableTexture> attachable_textures;
  2536. Vector<Texture *> mutable_sampled_textures;
  2537. Vector<Texture *> mutable_storage_textures;
  2538. for (uint32_t i = 0; i < set_uniform_count; i++) {
  2539. const ShaderUniform &set_uniform = set_uniforms[i];
  2540. int uniform_idx = -1;
  2541. for (int j = 0; j < (int)uniform_count; j++) {
  2542. if (uniforms[j].binding == set_uniform.binding) {
  2543. uniform_idx = j;
  2544. }
  2545. }
  2546. ERR_FAIL_COND_V_MSG(uniform_idx == -1, RID(),
  2547. "All the shader bindings for the given set must be covered by the uniforms provided. Binding (" + itos(set_uniform.binding) + "), set (" + itos(p_shader_set) + ") was not provided.");
  2548. const Uniform &uniform = uniforms[uniform_idx];
  2549. ERR_FAIL_COND_V_MSG(uniform.uniform_type != set_uniform.type, RID(),
  2550. "Mismatch uniform type for binding (" + itos(set_uniform.binding) + "), set (" + itos(p_shader_set) + "). Expected '" + SHADER_UNIFORM_NAMES[set_uniform.type] + "', supplied: '" + SHADER_UNIFORM_NAMES[uniform.uniform_type] + "'.");
  2551. RDD::BoundUniform &driver_uniform = driver_uniforms[i];
  2552. driver_uniform.type = uniform.uniform_type;
  2553. driver_uniform.binding = uniform.binding;
  2554. switch (uniform.uniform_type) {
  2555. case UNIFORM_TYPE_SAMPLER: {
  2556. if (uniform.get_id_count() != (uint32_t)set_uniform.length) {
  2557. if (set_uniform.length > 1) {
  2558. ERR_FAIL_V_MSG(RID(), "Sampler (binding: " + itos(uniform.binding) + ") is an array of (" + itos(set_uniform.length) + ") sampler elements, so it should be provided equal number of sampler IDs to satisfy it (IDs provided: " + itos(uniform.get_id_count()) + ").");
  2559. } else {
  2560. ERR_FAIL_V_MSG(RID(), "Sampler (binding: " + itos(uniform.binding) + ") should provide one ID referencing a sampler (IDs provided: " + itos(uniform.get_id_count()) + ").");
  2561. }
  2562. }
  2563. for (uint32_t j = 0; j < uniform.get_id_count(); j++) {
  2564. RDD::SamplerID *sampler_driver_id = sampler_owner.get_or_null(uniform.get_id(j));
  2565. ERR_FAIL_COND_V_MSG(!sampler_driver_id, RID(), "Sampler (binding: " + itos(uniform.binding) + ", index " + itos(j) + ") is not a valid sampler.");
  2566. driver_uniform.ids.push_back(*sampler_driver_id);
  2567. }
  2568. } break;
  2569. case UNIFORM_TYPE_SAMPLER_WITH_TEXTURE: {
  2570. if (uniform.get_id_count() != (uint32_t)set_uniform.length * 2) {
  2571. if (set_uniform.length > 1) {
  2572. ERR_FAIL_V_MSG(RID(), "SamplerTexture (binding: " + itos(uniform.binding) + ") is an array of (" + itos(set_uniform.length) + ") sampler&texture elements, so it should provided twice the amount of IDs (sampler,texture pairs) to satisfy it (IDs provided: " + itos(uniform.get_id_count()) + ").");
  2573. } else {
  2574. ERR_FAIL_V_MSG(RID(), "SamplerTexture (binding: " + itos(uniform.binding) + ") should provide two IDs referencing a sampler and then a texture (IDs provided: " + itos(uniform.get_id_count()) + ").");
  2575. }
  2576. }
  2577. for (uint32_t j = 0; j < uniform.get_id_count(); j += 2) {
  2578. RDD::SamplerID *sampler_driver_id = sampler_owner.get_or_null(uniform.get_id(j + 0));
  2579. ERR_FAIL_COND_V_MSG(!sampler_driver_id, RID(), "SamplerBuffer (binding: " + itos(uniform.binding) + ", index " + itos(j + 1) + ") is not a valid sampler.");
  2580. Texture *texture = texture_owner.get_or_null(uniform.get_id(j + 1));
  2581. ERR_FAIL_NULL_V_MSG(texture, RID(), "Texture (binding: " + itos(uniform.binding) + ", index " + itos(j) + ") is not a valid texture.");
  2582. ERR_FAIL_COND_V_MSG(!(texture->usage_flags & TEXTURE_USAGE_SAMPLING_BIT), RID(),
  2583. "Texture (binding: " + itos(uniform.binding) + ", index " + itos(j) + ") needs the TEXTURE_USAGE_SAMPLING_BIT usage flag set in order to be used as uniform.");
  2584. if ((texture->usage_flags & (TEXTURE_USAGE_COLOR_ATTACHMENT_BIT | TEXTURE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT | TEXTURE_USAGE_INPUT_ATTACHMENT_BIT))) {
  2585. UniformSet::AttachableTexture attachable_texture;
  2586. attachable_texture.bind = set_uniform.binding;
  2587. attachable_texture.texture = texture->owner.is_valid() ? texture->owner : uniform.get_id(j + 1);
  2588. attachable_textures.push_back(attachable_texture);
  2589. }
  2590. if ((texture->usage_flags & TEXTURE_USAGE_STORAGE_BIT)) {
  2591. mutable_sampled_textures.push_back(texture);
  2592. }
  2593. DEV_ASSERT(!texture->owner.is_valid() || texture_owner.get_or_null(texture->owner));
  2594. driver_uniform.ids.push_back(*sampler_driver_id);
  2595. driver_uniform.ids.push_back(texture->driver_id);
  2596. }
  2597. } break;
  2598. case UNIFORM_TYPE_TEXTURE: {
  2599. if (uniform.get_id_count() != (uint32_t)set_uniform.length) {
  2600. if (set_uniform.length > 1) {
  2601. ERR_FAIL_V_MSG(RID(), "Texture (binding: " + itos(uniform.binding) + ") is an array of (" + itos(set_uniform.length) + ") textures, so it should be provided equal number of texture IDs to satisfy it (IDs provided: " + itos(uniform.get_id_count()) + ").");
  2602. } else {
  2603. ERR_FAIL_V_MSG(RID(), "Texture (binding: " + itos(uniform.binding) + ") should provide one ID referencing a texture (IDs provided: " + itos(uniform.get_id_count()) + ").");
  2604. }
  2605. }
  2606. for (uint32_t j = 0; j < uniform.get_id_count(); j++) {
  2607. Texture *texture = texture_owner.get_or_null(uniform.get_id(j));
  2608. ERR_FAIL_NULL_V_MSG(texture, RID(), "Texture (binding: " + itos(uniform.binding) + ", index " + itos(j) + ") is not a valid texture.");
  2609. ERR_FAIL_COND_V_MSG(!(texture->usage_flags & TEXTURE_USAGE_SAMPLING_BIT), RID(),
  2610. "Texture (binding: " + itos(uniform.binding) + ", index " + itos(j) + ") needs the TEXTURE_USAGE_SAMPLING_BIT usage flag set in order to be used as uniform.");
  2611. if ((texture->usage_flags & (TEXTURE_USAGE_COLOR_ATTACHMENT_BIT | TEXTURE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT | TEXTURE_USAGE_INPUT_ATTACHMENT_BIT))) {
  2612. UniformSet::AttachableTexture attachable_texture;
  2613. attachable_texture.bind = set_uniform.binding;
  2614. attachable_texture.texture = texture->owner.is_valid() ? texture->owner : uniform.get_id(j);
  2615. attachable_textures.push_back(attachable_texture);
  2616. }
  2617. if ((texture->usage_flags & TEXTURE_USAGE_STORAGE_BIT)) {
  2618. mutable_sampled_textures.push_back(texture);
  2619. }
  2620. DEV_ASSERT(!texture->owner.is_valid() || texture_owner.get_or_null(texture->owner));
  2621. driver_uniform.ids.push_back(texture->driver_id);
  2622. }
  2623. } break;
  2624. case UNIFORM_TYPE_IMAGE: {
  2625. if (uniform.get_id_count() != (uint32_t)set_uniform.length) {
  2626. if (set_uniform.length > 1) {
  2627. ERR_FAIL_V_MSG(RID(), "Image (binding: " + itos(uniform.binding) + ") is an array of (" + itos(set_uniform.length) + ") textures, so it should be provided equal number of texture IDs to satisfy it (IDs provided: " + itos(uniform.get_id_count()) + ").");
  2628. } else {
  2629. ERR_FAIL_V_MSG(RID(), "Image (binding: " + itos(uniform.binding) + ") should provide one ID referencing a texture (IDs provided: " + itos(uniform.get_id_count()) + ").");
  2630. }
  2631. }
  2632. for (uint32_t j = 0; j < uniform.get_id_count(); j++) {
  2633. Texture *texture = texture_owner.get_or_null(uniform.get_id(j));
  2634. ERR_FAIL_NULL_V_MSG(texture, RID(),
  2635. "Image (binding: " + itos(uniform.binding) + ", index " + itos(j) + ") is not a valid texture.");
  2636. ERR_FAIL_COND_V_MSG(!(texture->usage_flags & TEXTURE_USAGE_STORAGE_BIT), RID(),
  2637. "Image (binding: " + itos(uniform.binding) + ", index " + itos(j) + ") needs the TEXTURE_USAGE_STORAGE_BIT usage flag set in order to be used as uniform.");
  2638. if ((texture->usage_flags & TEXTURE_USAGE_SAMPLING_BIT)) {
  2639. mutable_storage_textures.push_back(texture);
  2640. }
  2641. DEV_ASSERT(!texture->owner.is_valid() || texture_owner.get_or_null(texture->owner));
  2642. driver_uniform.ids.push_back(texture->driver_id);
  2643. }
  2644. } break;
  2645. case UNIFORM_TYPE_TEXTURE_BUFFER: {
  2646. if (uniform.get_id_count() != (uint32_t)set_uniform.length) {
  2647. if (set_uniform.length > 1) {
  2648. ERR_FAIL_V_MSG(RID(), "Buffer (binding: " + itos(uniform.binding) + ") is an array of (" + itos(set_uniform.length) + ") texture buffer elements, so it should be provided equal number of texture buffer IDs to satisfy it (IDs provided: " + itos(uniform.get_id_count()) + ").");
  2649. } else {
  2650. ERR_FAIL_V_MSG(RID(), "Buffer (binding: " + itos(uniform.binding) + ") should provide one ID referencing a texture buffer (IDs provided: " + itos(uniform.get_id_count()) + ").");
  2651. }
  2652. }
  2653. for (uint32_t j = 0; j < uniform.get_id_count(); j++) {
  2654. Buffer *buffer = texture_buffer_owner.get_or_null(uniform.get_id(j));
  2655. ERR_FAIL_NULL_V_MSG(buffer, RID(), "Texture Buffer (binding: " + itos(uniform.binding) + ", index " + itos(j) + ") is not a valid texture buffer.");
  2656. driver_uniform.ids.push_back(buffer->driver_id);
  2657. }
  2658. } break;
  2659. case UNIFORM_TYPE_SAMPLER_WITH_TEXTURE_BUFFER: {
  2660. if (uniform.get_id_count() != (uint32_t)set_uniform.length * 2) {
  2661. if (set_uniform.length > 1) {
  2662. ERR_FAIL_V_MSG(RID(), "SamplerBuffer (binding: " + itos(uniform.binding) + ") is an array of (" + itos(set_uniform.length) + ") sampler buffer elements, so it should provided twice the amount of IDs (sampler,buffer pairs) to satisfy it (IDs provided: " + itos(uniform.get_id_count()) + ").");
  2663. } else {
  2664. ERR_FAIL_V_MSG(RID(), "SamplerBuffer (binding: " + itos(uniform.binding) + ") should provide two IDs referencing a sampler and then a texture buffer (IDs provided: " + itos(uniform.get_id_count()) + ").");
  2665. }
  2666. }
  2667. for (uint32_t j = 0; j < uniform.get_id_count(); j += 2) {
  2668. RDD::SamplerID *sampler_driver_id = sampler_owner.get_or_null(uniform.get_id(j + 0));
  2669. ERR_FAIL_COND_V_MSG(!sampler_driver_id, RID(), "SamplerBuffer (binding: " + itos(uniform.binding) + ", index " + itos(j + 1) + ") is not a valid sampler.");
  2670. Buffer *buffer = texture_buffer_owner.get_or_null(uniform.get_id(j + 1));
  2671. ERR_FAIL_NULL_V_MSG(buffer, RID(), "SamplerBuffer (binding: " + itos(uniform.binding) + ", index " + itos(j + 1) + ") is not a valid texture buffer.");
  2672. driver_uniform.ids.push_back(*sampler_driver_id);
  2673. driver_uniform.ids.push_back(buffer->driver_id);
  2674. }
  2675. } break;
  2676. case UNIFORM_TYPE_IMAGE_BUFFER: {
  2677. // Todo.
  2678. } break;
  2679. case UNIFORM_TYPE_UNIFORM_BUFFER: {
  2680. ERR_FAIL_COND_V_MSG(uniform.get_id_count() != 1, RID(),
  2681. "Uniform buffer supplied (binding: " + itos(uniform.binding) + ") must provide one ID (" + itos(uniform.get_id_count()) + " provided).");
  2682. Buffer *buffer = uniform_buffer_owner.get_or_null(uniform.get_id(0));
  2683. ERR_FAIL_NULL_V_MSG(buffer, RID(), "Uniform buffer supplied (binding: " + itos(uniform.binding) + ") is invalid.");
  2684. ERR_FAIL_COND_V_MSG(buffer->size < (uint32_t)set_uniform.length, RID(),
  2685. "Uniform buffer supplied (binding: " + itos(uniform.binding) + ") size (" + itos(buffer->size) + " is smaller than size of shader uniform: (" + itos(set_uniform.length) + ").");
  2686. driver_uniform.ids.push_back(buffer->driver_id);
  2687. } break;
  2688. case UNIFORM_TYPE_STORAGE_BUFFER: {
  2689. ERR_FAIL_COND_V_MSG(uniform.get_id_count() != 1, RID(),
  2690. "Storage buffer supplied (binding: " + itos(uniform.binding) + ") must provide one ID (" + itos(uniform.get_id_count()) + " provided).");
  2691. Buffer *buffer = nullptr;
  2692. if (storage_buffer_owner.owns(uniform.get_id(0))) {
  2693. buffer = storage_buffer_owner.get_or_null(uniform.get_id(0));
  2694. } else if (vertex_buffer_owner.owns(uniform.get_id(0))) {
  2695. buffer = vertex_buffer_owner.get_or_null(uniform.get_id(0));
  2696. ERR_FAIL_COND_V_MSG(!(buffer->usage.has_flag(RDD::BUFFER_USAGE_STORAGE_BIT)), RID(), "Vertex buffer supplied (binding: " + itos(uniform.binding) + ") was not created with storage flag.");
  2697. }
  2698. ERR_FAIL_NULL_V_MSG(buffer, RID(), "Storage buffer supplied (binding: " + itos(uniform.binding) + ") is invalid.");
  2699. // If 0, then it's sized on link time.
  2700. ERR_FAIL_COND_V_MSG(set_uniform.length > 0 && buffer->size != (uint32_t)set_uniform.length, RID(),
  2701. "Storage buffer supplied (binding: " + itos(uniform.binding) + ") size (" + itos(buffer->size) + " does not match size of shader uniform: (" + itos(set_uniform.length) + ").");
  2702. driver_uniform.ids.push_back(buffer->driver_id);
  2703. } break;
  2704. case UNIFORM_TYPE_INPUT_ATTACHMENT: {
  2705. ERR_FAIL_COND_V_MSG(shader->is_compute, RID(), "InputAttachment (binding: " + itos(uniform.binding) + ") supplied for compute shader (this is not allowed).");
  2706. if (uniform.get_id_count() != (uint32_t)set_uniform.length) {
  2707. if (set_uniform.length > 1) {
  2708. ERR_FAIL_V_MSG(RID(), "InputAttachment (binding: " + itos(uniform.binding) + ") is an array of (" + itos(set_uniform.length) + ") textures, so it should be provided equal number of texture IDs to satisfy it (IDs provided: " + itos(uniform.get_id_count()) + ").");
  2709. } else {
  2710. ERR_FAIL_V_MSG(RID(), "InputAttachment (binding: " + itos(uniform.binding) + ") should provide one ID referencing a texture (IDs provided: " + itos(uniform.get_id_count()) + ").");
  2711. }
  2712. }
  2713. for (uint32_t j = 0; j < uniform.get_id_count(); j++) {
  2714. Texture *texture = texture_owner.get_or_null(uniform.get_id(j));
  2715. ERR_FAIL_NULL_V_MSG(texture, RID(),
  2716. "InputAttachment (binding: " + itos(uniform.binding) + ", index " + itos(j) + ") is not a valid texture.");
  2717. ERR_FAIL_COND_V_MSG(!(texture->usage_flags & TEXTURE_USAGE_SAMPLING_BIT), RID(),
  2718. "InputAttachment (binding: " + itos(uniform.binding) + ", index " + itos(j) + ") needs the TEXTURE_USAGE_SAMPLING_BIT usage flag set in order to be used as uniform.");
  2719. DEV_ASSERT(!texture->owner.is_valid() || texture_owner.get_or_null(texture->owner));
  2720. driver_uniform.ids.push_back(texture->driver_id);
  2721. }
  2722. } break;
  2723. default: {
  2724. }
  2725. }
  2726. }
  2727. RDD::UniformSetID driver_uniform_set = driver->uniform_set_create(driver_uniforms, shader->driver_id, p_shader_set);
  2728. ERR_FAIL_COND_V(!driver_uniform_set, RID());
  2729. UniformSet uniform_set;
  2730. uniform_set.driver_id = driver_uniform_set;
  2731. uniform_set.format = shader->set_formats[p_shader_set];
  2732. uniform_set.attachable_textures = attachable_textures;
  2733. uniform_set.mutable_sampled_textures = mutable_sampled_textures;
  2734. uniform_set.mutable_storage_textures = mutable_storage_textures;
  2735. uniform_set.shader_set = p_shader_set;
  2736. uniform_set.shader_id = p_shader;
  2737. RID id = uniform_set_owner.make_rid(uniform_set);
  2738. #ifdef DEV_ENABLED
  2739. set_resource_name(id, "RID:" + itos(id.get_id()));
  2740. #endif
  2741. // Add dependencies.
  2742. _add_dependency(id, p_shader);
  2743. for (uint32_t i = 0; i < uniform_count; i++) {
  2744. const Uniform &uniform = uniforms[i];
  2745. int id_count = uniform.get_id_count();
  2746. for (int j = 0; j < id_count; j++) {
  2747. _add_dependency(id, uniform.get_id(j));
  2748. }
  2749. }
  2750. return id;
  2751. }
  2752. bool RenderingDevice::uniform_set_is_valid(RID p_uniform_set) {
  2753. return uniform_set_owner.owns(p_uniform_set);
  2754. }
  2755. void RenderingDevice::uniform_set_set_invalidation_callback(RID p_uniform_set, InvalidationCallback p_callback, void *p_userdata) {
  2756. UniformSet *us = uniform_set_owner.get_or_null(p_uniform_set);
  2757. ERR_FAIL_NULL(us);
  2758. us->invalidated_callback = p_callback;
  2759. us->invalidated_callback_userdata = p_userdata;
  2760. }
  2761. /*******************/
  2762. /**** PIPELINES ****/
  2763. /*******************/
  2764. RID RenderingDevice::render_pipeline_create(RID p_shader, FramebufferFormatID p_framebuffer_format, VertexFormatID p_vertex_format, RenderPrimitive p_render_primitive, const PipelineRasterizationState &p_rasterization_state, const PipelineMultisampleState &p_multisample_state, const PipelineDepthStencilState &p_depth_stencil_state, const PipelineColorBlendState &p_blend_state, BitField<PipelineDynamicStateFlags> p_dynamic_state_flags, uint32_t p_for_render_pass, const Vector<PipelineSpecializationConstant> &p_specialization_constants) {
  2765. _THREAD_SAFE_METHOD_
  2766. // Needs a shader.
  2767. Shader *shader = shader_owner.get_or_null(p_shader);
  2768. ERR_FAIL_NULL_V(shader, RID());
  2769. ERR_FAIL_COND_V_MSG(shader->is_compute, RID(),
  2770. "Compute shaders can't be used in render pipelines");
  2771. if (p_framebuffer_format == INVALID_ID) {
  2772. // If nothing provided, use an empty one (no attachments).
  2773. p_framebuffer_format = framebuffer_format_create(Vector<AttachmentFormat>());
  2774. }
  2775. ERR_FAIL_COND_V(!framebuffer_formats.has(p_framebuffer_format), RID());
  2776. const FramebufferFormat &fb_format = framebuffer_formats[p_framebuffer_format];
  2777. // Validate shader vs. framebuffer.
  2778. {
  2779. ERR_FAIL_COND_V_MSG(p_for_render_pass >= uint32_t(fb_format.E->key().passes.size()), RID(), "Render pass requested for pipeline creation (" + itos(p_for_render_pass) + ") is out of bounds");
  2780. const FramebufferPass &pass = fb_format.E->key().passes[p_for_render_pass];
  2781. uint32_t output_mask = 0;
  2782. for (int i = 0; i < pass.color_attachments.size(); i++) {
  2783. if (pass.color_attachments[i] != ATTACHMENT_UNUSED) {
  2784. output_mask |= 1 << i;
  2785. }
  2786. }
  2787. ERR_FAIL_COND_V_MSG(shader->fragment_output_mask != output_mask, RID(),
  2788. "Mismatch fragment shader output mask (" + itos(shader->fragment_output_mask) + ") and framebuffer color output mask (" + itos(output_mask) + ") when binding both in render pipeline.");
  2789. }
  2790. RDD::VertexFormatID driver_vertex_format;
  2791. if (p_vertex_format != INVALID_ID) {
  2792. // Uses vertices, else it does not.
  2793. ERR_FAIL_COND_V(!vertex_formats.has(p_vertex_format), RID());
  2794. const VertexDescriptionCache &vd = vertex_formats[p_vertex_format];
  2795. driver_vertex_format = vertex_formats[p_vertex_format].driver_id;
  2796. // Validate with inputs.
  2797. for (uint32_t i = 0; i < 64; i++) {
  2798. if (!(shader->vertex_input_mask & ((uint64_t)1) << i)) {
  2799. continue;
  2800. }
  2801. bool found = false;
  2802. for (int j = 0; j < vd.vertex_formats.size(); j++) {
  2803. if (vd.vertex_formats[j].location == i) {
  2804. found = true;
  2805. }
  2806. }
  2807. ERR_FAIL_COND_V_MSG(!found, RID(),
  2808. "Shader vertex input location (" + itos(i) + ") not provided in vertex input description for pipeline creation.");
  2809. }
  2810. } else {
  2811. ERR_FAIL_COND_V_MSG(shader->vertex_input_mask != 0, RID(),
  2812. "Shader contains vertex inputs, but no vertex input description was provided for pipeline creation.");
  2813. }
  2814. ERR_FAIL_INDEX_V(p_render_primitive, RENDER_PRIMITIVE_MAX, RID());
  2815. ERR_FAIL_INDEX_V(p_rasterization_state.cull_mode, 3, RID());
  2816. if (p_multisample_state.sample_mask.size()) {
  2817. // Use sample mask.
  2818. ERR_FAIL_COND_V((int)TEXTURE_SAMPLES_COUNT[p_multisample_state.sample_count] != p_multisample_state.sample_mask.size(), RID());
  2819. }
  2820. ERR_FAIL_INDEX_V(p_depth_stencil_state.depth_compare_operator, COMPARE_OP_MAX, RID());
  2821. ERR_FAIL_INDEX_V(p_depth_stencil_state.front_op.fail, STENCIL_OP_MAX, RID());
  2822. ERR_FAIL_INDEX_V(p_depth_stencil_state.front_op.pass, STENCIL_OP_MAX, RID());
  2823. ERR_FAIL_INDEX_V(p_depth_stencil_state.front_op.depth_fail, STENCIL_OP_MAX, RID());
  2824. ERR_FAIL_INDEX_V(p_depth_stencil_state.front_op.compare, COMPARE_OP_MAX, RID());
  2825. ERR_FAIL_INDEX_V(p_depth_stencil_state.back_op.fail, STENCIL_OP_MAX, RID());
  2826. ERR_FAIL_INDEX_V(p_depth_stencil_state.back_op.pass, STENCIL_OP_MAX, RID());
  2827. ERR_FAIL_INDEX_V(p_depth_stencil_state.back_op.depth_fail, STENCIL_OP_MAX, RID());
  2828. ERR_FAIL_INDEX_V(p_depth_stencil_state.back_op.compare, COMPARE_OP_MAX, RID());
  2829. ERR_FAIL_INDEX_V(p_blend_state.logic_op, LOGIC_OP_MAX, RID());
  2830. const FramebufferPass &pass = fb_format.E->key().passes[p_for_render_pass];
  2831. ERR_FAIL_COND_V(p_blend_state.attachments.size() < pass.color_attachments.size(), RID());
  2832. for (int i = 0; i < pass.color_attachments.size(); i++) {
  2833. if (pass.color_attachments[i] != ATTACHMENT_UNUSED) {
  2834. ERR_FAIL_INDEX_V(p_blend_state.attachments[i].src_color_blend_factor, BLEND_FACTOR_MAX, RID());
  2835. ERR_FAIL_INDEX_V(p_blend_state.attachments[i].dst_color_blend_factor, BLEND_FACTOR_MAX, RID());
  2836. ERR_FAIL_INDEX_V(p_blend_state.attachments[i].color_blend_op, BLEND_OP_MAX, RID());
  2837. ERR_FAIL_INDEX_V(p_blend_state.attachments[i].src_alpha_blend_factor, BLEND_FACTOR_MAX, RID());
  2838. ERR_FAIL_INDEX_V(p_blend_state.attachments[i].dst_alpha_blend_factor, BLEND_FACTOR_MAX, RID());
  2839. ERR_FAIL_INDEX_V(p_blend_state.attachments[i].alpha_blend_op, BLEND_OP_MAX, RID());
  2840. }
  2841. }
  2842. for (int i = 0; i < shader->specialization_constants.size(); i++) {
  2843. const ShaderSpecializationConstant &sc = shader->specialization_constants[i];
  2844. for (int j = 0; j < p_specialization_constants.size(); j++) {
  2845. const PipelineSpecializationConstant &psc = p_specialization_constants[j];
  2846. if (psc.constant_id == sc.constant_id) {
  2847. ERR_FAIL_COND_V_MSG(psc.type != sc.type, RID(), "Specialization constant provided for id (" + itos(sc.constant_id) + ") is of the wrong type.");
  2848. break;
  2849. }
  2850. }
  2851. }
  2852. RenderPipeline pipeline;
  2853. pipeline.driver_id = driver->render_pipeline_create(
  2854. shader->driver_id,
  2855. driver_vertex_format,
  2856. p_render_primitive,
  2857. p_rasterization_state,
  2858. p_multisample_state,
  2859. p_depth_stencil_state,
  2860. p_blend_state,
  2861. pass.color_attachments,
  2862. p_dynamic_state_flags,
  2863. fb_format.render_pass,
  2864. p_for_render_pass,
  2865. p_specialization_constants);
  2866. ERR_FAIL_COND_V(!pipeline.driver_id, RID());
  2867. if (pipelines_cache_enabled) {
  2868. _update_pipeline_cache();
  2869. }
  2870. pipeline.shader = p_shader;
  2871. pipeline.shader_driver_id = shader->driver_id;
  2872. pipeline.shader_layout_hash = shader->layout_hash;
  2873. pipeline.set_formats = shader->set_formats;
  2874. pipeline.push_constant_size = shader->push_constant_size;
  2875. #ifdef DEBUG_ENABLED
  2876. pipeline.validation.dynamic_state = p_dynamic_state_flags;
  2877. pipeline.validation.framebuffer_format = p_framebuffer_format;
  2878. pipeline.validation.render_pass = p_for_render_pass;
  2879. pipeline.validation.vertex_format = p_vertex_format;
  2880. pipeline.validation.uses_restart_indices = p_render_primitive == RENDER_PRIMITIVE_TRIANGLE_STRIPS_WITH_RESTART_INDEX;
  2881. static const uint32_t primitive_divisor[RENDER_PRIMITIVE_MAX] = {
  2882. 1, 2, 1, 1, 1, 3, 1, 1, 1, 1, 1
  2883. };
  2884. pipeline.validation.primitive_divisor = primitive_divisor[p_render_primitive];
  2885. static const uint32_t primitive_minimum[RENDER_PRIMITIVE_MAX] = {
  2886. 1,
  2887. 2,
  2888. 2,
  2889. 2,
  2890. 2,
  2891. 3,
  2892. 3,
  2893. 3,
  2894. 3,
  2895. 3,
  2896. 1,
  2897. };
  2898. pipeline.validation.primitive_minimum = primitive_minimum[p_render_primitive];
  2899. #endif
  2900. // Create ID to associate with this pipeline.
  2901. RID id = render_pipeline_owner.make_rid(pipeline);
  2902. #ifdef DEV_ENABLED
  2903. set_resource_name(id, "RID:" + itos(id.get_id()));
  2904. #endif
  2905. // Now add all the dependencies.
  2906. _add_dependency(id, p_shader);
  2907. return id;
  2908. }
  2909. bool RenderingDevice::render_pipeline_is_valid(RID p_pipeline) {
  2910. _THREAD_SAFE_METHOD_
  2911. return render_pipeline_owner.owns(p_pipeline);
  2912. }
  2913. RID RenderingDevice::compute_pipeline_create(RID p_shader, const Vector<PipelineSpecializationConstant> &p_specialization_constants) {
  2914. _THREAD_SAFE_METHOD_
  2915. // Needs a shader.
  2916. Shader *shader = shader_owner.get_or_null(p_shader);
  2917. ERR_FAIL_NULL_V(shader, RID());
  2918. ERR_FAIL_COND_V_MSG(!shader->is_compute, RID(),
  2919. "Non-compute shaders can't be used in compute pipelines");
  2920. for (int i = 0; i < shader->specialization_constants.size(); i++) {
  2921. const ShaderSpecializationConstant &sc = shader->specialization_constants[i];
  2922. for (int j = 0; j < p_specialization_constants.size(); j++) {
  2923. const PipelineSpecializationConstant &psc = p_specialization_constants[j];
  2924. if (psc.constant_id == sc.constant_id) {
  2925. ERR_FAIL_COND_V_MSG(psc.type != sc.type, RID(), "Specialization constant provided for id (" + itos(sc.constant_id) + ") is of the wrong type.");
  2926. break;
  2927. }
  2928. }
  2929. }
  2930. ComputePipeline pipeline;
  2931. pipeline.driver_id = driver->compute_pipeline_create(shader->driver_id, p_specialization_constants);
  2932. ERR_FAIL_COND_V(!pipeline.driver_id, RID());
  2933. if (pipelines_cache_enabled) {
  2934. _update_pipeline_cache();
  2935. }
  2936. pipeline.shader = p_shader;
  2937. pipeline.shader_driver_id = shader->driver_id;
  2938. pipeline.shader_layout_hash = shader->layout_hash;
  2939. pipeline.set_formats = shader->set_formats;
  2940. pipeline.push_constant_size = shader->push_constant_size;
  2941. pipeline.local_group_size[0] = shader->compute_local_size[0];
  2942. pipeline.local_group_size[1] = shader->compute_local_size[1];
  2943. pipeline.local_group_size[2] = shader->compute_local_size[2];
  2944. // Create ID to associate with this pipeline.
  2945. RID id = compute_pipeline_owner.make_rid(pipeline);
  2946. #ifdef DEV_ENABLED
  2947. set_resource_name(id, "RID:" + itos(id.get_id()));
  2948. #endif
  2949. // Now add all the dependencies.
  2950. _add_dependency(id, p_shader);
  2951. return id;
  2952. }
  2953. bool RenderingDevice::compute_pipeline_is_valid(RID p_pipeline) {
  2954. return compute_pipeline_owner.owns(p_pipeline);
  2955. }
  2956. /****************/
  2957. /**** SCREEN ****/
  2958. /****************/
  2959. int RenderingDevice::screen_get_width(DisplayServer::WindowID p_screen) const {
  2960. _THREAD_SAFE_METHOD_
  2961. ERR_FAIL_COND_V_MSG(local_device.is_valid(), -1, "Local devices have no screen");
  2962. return context->window_get_width(p_screen);
  2963. }
  2964. int RenderingDevice::screen_get_height(DisplayServer::WindowID p_screen) const {
  2965. _THREAD_SAFE_METHOD_
  2966. ERR_FAIL_COND_V_MSG(local_device.is_valid(), -1, "Local devices have no screen");
  2967. return context->window_get_height(p_screen);
  2968. }
  2969. RenderingDevice::FramebufferFormatID RenderingDevice::screen_get_framebuffer_format() const {
  2970. _THREAD_SAFE_METHOD_
  2971. ERR_FAIL_COND_V_MSG(local_device.is_valid(), INVALID_ID, "Local devices have no screen");
  2972. DataFormat format = driver->screen_get_format();
  2973. ERR_FAIL_COND_V(format == DATA_FORMAT_MAX, INVALID_ID);
  2974. AttachmentFormat attachment;
  2975. attachment.format = format;
  2976. attachment.samples = TEXTURE_SAMPLES_1;
  2977. attachment.usage_flags = TEXTURE_USAGE_COLOR_ATTACHMENT_BIT;
  2978. Vector<AttachmentFormat> screen_attachment;
  2979. screen_attachment.push_back(attachment);
  2980. return const_cast<RenderingDevice *>(this)->framebuffer_format_create(screen_attachment);
  2981. }
  2982. /*******************/
  2983. /**** DRAW LIST ****/
  2984. /*******************/
  2985. RenderingDevice::DrawListID RenderingDevice::draw_list_begin_for_screen(DisplayServer::WindowID p_screen, const Color &p_clear_color) {
  2986. _THREAD_SAFE_METHOD_
  2987. ERR_FAIL_COND_V_MSG(local_device.is_valid(), INVALID_ID, "Local devices have no screen");
  2988. ERR_FAIL_COND_V_MSG(draw_list != nullptr, INVALID_ID, "Only one draw list can be active at the same time.");
  2989. ERR_FAIL_COND_V_MSG(compute_list != nullptr, INVALID_ID, "Only one draw/compute list can be active at the same time.");
  2990. RDD::CommandBufferID command_buffer = frames[frame].draw_command_buffer;
  2991. if (!context->window_is_valid_swapchain(p_screen)) {
  2992. return INVALID_ID;
  2993. }
  2994. Size2i size = Size2i(context->window_get_width(p_screen), context->window_get_height(p_screen));
  2995. _draw_list_allocate(Rect2i(Vector2i(), size), 0, 0);
  2996. #ifdef DEBUG_ENABLED
  2997. draw_list_framebuffer_format = screen_get_framebuffer_format();
  2998. #endif
  2999. draw_list_subpass_count = 1;
  3000. RDD::RenderPassClearValue clear_value;
  3001. clear_value.color = p_clear_color;
  3002. driver->command_begin_render_pass(
  3003. command_buffer,
  3004. context->window_get_render_pass(p_screen),
  3005. context->window_get_framebuffer(p_screen),
  3006. RDD::COMMAND_BUFFER_TYPE_PRIMARY,
  3007. Rect2i(0, 0, size.width, size.height),
  3008. VectorView(&clear_value, 1));
  3009. driver->command_render_set_viewport(command_buffer, Rect2i(Point2i(), size));
  3010. driver->command_render_set_scissor(command_buffer, Rect2i(Point2i(), size));
  3011. return int64_t(ID_TYPE_DRAW_LIST) << ID_BASE_SHIFT;
  3012. }
  3013. Error RenderingDevice::_draw_list_setup_framebuffer(Framebuffer *p_framebuffer, InitialAction p_initial_color_action, FinalAction p_final_color_action, InitialAction p_initial_depth_action, FinalAction p_final_depth_action, RDD::FramebufferID *r_framebuffer, RDD::RenderPassID *r_render_pass, uint32_t *r_subpass_count) {
  3014. Framebuffer::VersionKey vk;
  3015. vk.initial_color_action = p_initial_color_action;
  3016. vk.final_color_action = p_final_color_action;
  3017. vk.initial_depth_action = p_initial_depth_action;
  3018. vk.final_depth_action = p_final_depth_action;
  3019. vk.view_count = p_framebuffer->view_count;
  3020. if (!p_framebuffer->framebuffers.has(vk)) {
  3021. // Need to create this version.
  3022. Framebuffer::Version version;
  3023. version.render_pass = _render_pass_create(framebuffer_formats[p_framebuffer->format_id].E->key().attachments, framebuffer_formats[p_framebuffer->format_id].E->key().passes, p_initial_color_action, p_final_color_action, p_initial_depth_action, p_final_depth_action, p_framebuffer->view_count);
  3024. LocalVector<RDD::TextureID> attachments;
  3025. for (int i = 0; i < p_framebuffer->texture_ids.size(); i++) {
  3026. Texture *texture = texture_owner.get_or_null(p_framebuffer->texture_ids[i]);
  3027. if (texture) {
  3028. attachments.push_back(texture->driver_id);
  3029. if (!(texture->usage_flags & TEXTURE_USAGE_VRS_ATTACHMENT_BIT)) { // VRS attachment will be a different size.
  3030. ERR_FAIL_COND_V(texture->width != p_framebuffer->size.width, ERR_BUG);
  3031. ERR_FAIL_COND_V(texture->height != p_framebuffer->size.height, ERR_BUG);
  3032. }
  3033. }
  3034. }
  3035. version.framebuffer = driver->framebuffer_create(version.render_pass, attachments, p_framebuffer->size.width, p_framebuffer->size.height);
  3036. ERR_FAIL_COND_V(!version.framebuffer, ERR_CANT_CREATE);
  3037. version.subpass_count = framebuffer_formats[p_framebuffer->format_id].E->key().passes.size();
  3038. p_framebuffer->framebuffers.insert(vk, version);
  3039. }
  3040. const Framebuffer::Version &version = p_framebuffer->framebuffers[vk];
  3041. *r_framebuffer = version.framebuffer;
  3042. *r_render_pass = version.render_pass;
  3043. *r_subpass_count = version.subpass_count;
  3044. return OK;
  3045. }
  3046. Error RenderingDevice::_draw_list_render_pass_begin(Framebuffer *p_framebuffer, InitialAction p_initial_color_action, FinalAction p_final_color_action, InitialAction p_initial_depth_action, FinalAction p_final_depth_action, const Vector<Color> &p_clear_colors, float p_clear_depth, uint32_t p_clear_stencil, Point2i p_viewport_offset, Point2i p_viewport_size, RDD::FramebufferID p_framebuffer_driver_id, RDD::RenderPassID p_render_pass, RDD::CommandBufferID p_command_buffer, RDD::CommandBufferType p_cmd_buffer_mode, const Vector<RID> &p_storage_textures, bool p_constrained_to_region) {
  3047. LocalVector<RDD::RenderPassClearValue> clear_values;
  3048. clear_values.resize(p_framebuffer->texture_ids.size());
  3049. int clear_values_count = 0;
  3050. {
  3051. int color_index = 0;
  3052. for (int i = 0; i < p_framebuffer->texture_ids.size(); i++) {
  3053. RDD::RenderPassClearValue clear_value;
  3054. Texture *texture = texture_owner.get_or_null(p_framebuffer->texture_ids[i]);
  3055. if (!texture) {
  3056. color_index++;
  3057. continue;
  3058. }
  3059. if (color_index < p_clear_colors.size() && texture->usage_flags & TEXTURE_USAGE_COLOR_ATTACHMENT_BIT) {
  3060. ERR_FAIL_INDEX_V(color_index, p_clear_colors.size(), ERR_BUG); // A bug.
  3061. clear_value.color = p_clear_colors[color_index];
  3062. color_index++;
  3063. } else if (texture->usage_flags & TEXTURE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT) {
  3064. clear_value.depth = p_clear_depth;
  3065. clear_value.stencil = p_clear_stencil;
  3066. }
  3067. clear_values[clear_values_count++] = clear_value;
  3068. }
  3069. }
  3070. for (int i = 0; i < p_storage_textures.size(); i++) {
  3071. Texture *texture = texture_owner.get_or_null(p_storage_textures[i]);
  3072. if (!texture) {
  3073. continue;
  3074. }
  3075. ERR_CONTINUE_MSG(!(texture->usage_flags & TEXTURE_USAGE_STORAGE_BIT), "Supplied storage texture " + itos(i) + " for draw list is not set to be used for storage.");
  3076. if (texture->usage_flags & TEXTURE_USAGE_SAMPLING_BIT) {
  3077. if (driver->api_trait_get(RDD::API_TRAIT_HONORS_PIPELINE_BARRIERS)) {
  3078. // Must change layout to general.
  3079. RDD::TextureBarrier tb;
  3080. tb.texture = texture->driver_id;
  3081. tb.src_access = (RDD::BARRIER_ACCESS_SHADER_READ_BIT | RDD::BARRIER_ACCESS_SHADER_WRITE_BIT);
  3082. tb.dst_access = (RDD::BARRIER_ACCESS_SHADER_READ_BIT | RDD::BARRIER_ACCESS_SHADER_WRITE_BIT);
  3083. tb.prev_layout = texture->layout;
  3084. tb.next_layout = RDD::TEXTURE_LAYOUT_GENERAL;
  3085. tb.subresources.aspect = texture->read_aspect_flags;
  3086. tb.subresources.base_mipmap = texture->base_mipmap;
  3087. tb.subresources.mipmap_count = texture->mipmaps;
  3088. tb.subresources.base_layer = texture->base_layer;
  3089. tb.subresources.layer_count = texture->layers;
  3090. driver->command_pipeline_barrier(p_command_buffer, RDD::PIPELINE_STAGE_COMPUTE_SHADER_BIT | RDD::PIPELINE_STAGE_FRAGMENT_SHADER_BIT, RDD::PIPELINE_STAGE_VERTEX_INPUT_BIT | RDD::PIPELINE_STAGE_FRAGMENT_SHADER_BIT, {}, {}, tb);
  3091. texture->layout = RDD::TEXTURE_LAYOUT_GENERAL;
  3092. }
  3093. draw_list_storage_textures.push_back(p_storage_textures[i]);
  3094. }
  3095. }
  3096. Rect2i region;
  3097. if (p_constrained_to_region) {
  3098. region = Rect2i(p_viewport_offset, p_viewport_size);
  3099. } else {
  3100. region = Rect2i(Point2i(), p_framebuffer->size);
  3101. }
  3102. driver->command_begin_render_pass(
  3103. p_command_buffer,
  3104. p_render_pass,
  3105. p_framebuffer_driver_id,
  3106. p_cmd_buffer_mode,
  3107. region,
  3108. clear_values);
  3109. // Mark textures as bound.
  3110. draw_list_bound_textures.clear();
  3111. draw_list_unbind_color_textures = p_final_color_action != FINAL_ACTION_CONTINUE;
  3112. draw_list_unbind_depth_textures = p_final_depth_action != FINAL_ACTION_CONTINUE;
  3113. for (int i = 0; i < p_framebuffer->texture_ids.size(); i++) {
  3114. Texture *texture = texture_owner.get_or_null(p_framebuffer->texture_ids[i]);
  3115. if (!texture) {
  3116. continue;
  3117. }
  3118. texture->bound = true;
  3119. draw_list_bound_textures.push_back(p_framebuffer->texture_ids[i]);
  3120. }
  3121. return OK;
  3122. }
  3123. void RenderingDevice::_draw_list_insert_clear_region(DrawList *p_draw_list, Framebuffer *p_framebuffer, Point2i p_viewport_offset, Point2i p_viewport_size, bool p_clear_color, const Vector<Color> &p_clear_colors, bool p_clear_depth, float p_depth, uint32_t p_stencil) {
  3124. LocalVector<RDD::AttachmentClear> clear_attachments;
  3125. int color_index = 0;
  3126. int texture_index = 0;
  3127. for (int i = 0; i < p_framebuffer->texture_ids.size(); i++) {
  3128. Texture *texture = texture_owner.get_or_null(p_framebuffer->texture_ids[i]);
  3129. if (!texture) {
  3130. texture_index++;
  3131. continue;
  3132. }
  3133. RDD::AttachmentClear clear_at;
  3134. if (p_clear_color && (texture->usage_flags & TEXTURE_USAGE_COLOR_ATTACHMENT_BIT)) {
  3135. Color clear_color = p_clear_colors[texture_index++];
  3136. clear_at.value.color = clear_color;
  3137. clear_at.color_attachment = color_index++;
  3138. clear_at.aspect = RDD::TEXTURE_ASPECT_COLOR_BIT;
  3139. } else if (p_clear_depth && (texture->usage_flags & TEXTURE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT)) {
  3140. clear_at.value.depth = p_depth;
  3141. clear_at.value.stencil = p_stencil;
  3142. clear_at.color_attachment = 0;
  3143. clear_at.aspect = RDD::TEXTURE_ASPECT_DEPTH_BIT;
  3144. if (format_has_stencil(texture->format)) {
  3145. clear_at.aspect.set_flag(RDD::TEXTURE_ASPECT_STENCIL_BIT);
  3146. }
  3147. } else {
  3148. ERR_CONTINUE(true);
  3149. }
  3150. clear_attachments.push_back(clear_at);
  3151. }
  3152. Rect2i rect = Rect2i(p_viewport_offset, p_viewport_size);
  3153. driver->command_render_clear_attachments(p_draw_list->command_buffer, clear_attachments, rect);
  3154. }
  3155. RenderingDevice::DrawListID RenderingDevice::draw_list_begin(RID p_framebuffer, InitialAction p_initial_color_action, FinalAction p_final_color_action, InitialAction p_initial_depth_action, FinalAction p_final_depth_action, const Vector<Color> &p_clear_color_values, float p_clear_depth, uint32_t p_clear_stencil, const Rect2 &p_region, const Vector<RID> &p_storage_textures) {
  3156. _THREAD_SAFE_METHOD_
  3157. ERR_FAIL_COND_V_MSG(draw_list != nullptr, INVALID_ID, "Only one draw list can be active at the same time.");
  3158. ERR_FAIL_COND_V_MSG(compute_list != nullptr && !compute_list->state.allow_draw_overlap, INVALID_ID, "Only one draw/compute list can be active at the same time.");
  3159. Framebuffer *framebuffer = framebuffer_owner.get_or_null(p_framebuffer);
  3160. ERR_FAIL_NULL_V(framebuffer, INVALID_ID);
  3161. Point2i viewport_offset;
  3162. Point2i viewport_size = framebuffer->size;
  3163. bool constrained_to_region = false;
  3164. bool needs_clear_color = false;
  3165. bool needs_clear_depth = false;
  3166. if (p_region != Rect2() && p_region != Rect2(Vector2(), viewport_size)) { // Check custom region.
  3167. Rect2i viewport(viewport_offset, viewport_size);
  3168. Rect2i regioni = p_region;
  3169. if (!(regioni.position.x >= viewport.position.x) && (regioni.position.y >= viewport.position.y) &&
  3170. ((regioni.position.x + regioni.size.x) <= (viewport.position.x + viewport.size.x)) &&
  3171. ((regioni.position.y + regioni.size.y) <= (viewport.position.y + viewport.size.y))) {
  3172. ERR_FAIL_V_MSG(INVALID_ID, "When supplying a custom region, it must be contained within the framebuffer rectangle");
  3173. }
  3174. viewport_offset = regioni.position;
  3175. viewport_size = regioni.size;
  3176. // If clearing regions both in color and depth, we can switch to a fast path where we let Vulkan to the clears
  3177. // and we constrain the render area to the region.
  3178. if (p_initial_color_action == INITIAL_ACTION_CLEAR_REGION && p_initial_depth_action == INITIAL_ACTION_CLEAR_REGION) {
  3179. constrained_to_region = true;
  3180. p_initial_color_action = INITIAL_ACTION_CLEAR;
  3181. p_initial_depth_action = INITIAL_ACTION_CLEAR;
  3182. } else {
  3183. if (p_initial_color_action == INITIAL_ACTION_CLEAR_REGION_CONTINUE) {
  3184. needs_clear_color = true;
  3185. p_initial_color_action = INITIAL_ACTION_CONTINUE;
  3186. }
  3187. if (p_initial_depth_action == INITIAL_ACTION_CLEAR_REGION_CONTINUE) {
  3188. needs_clear_depth = true;
  3189. p_initial_depth_action = INITIAL_ACTION_CONTINUE;
  3190. }
  3191. if (p_initial_color_action == INITIAL_ACTION_CLEAR_REGION) {
  3192. needs_clear_color = true;
  3193. p_initial_color_action = INITIAL_ACTION_KEEP;
  3194. }
  3195. if (p_initial_depth_action == INITIAL_ACTION_CLEAR_REGION) {
  3196. needs_clear_depth = true;
  3197. p_initial_depth_action = INITIAL_ACTION_KEEP;
  3198. }
  3199. }
  3200. }
  3201. if (p_initial_color_action == INITIAL_ACTION_CLEAR || needs_clear_color) { // Check clear values.
  3202. int color_count = 0;
  3203. for (int i = 0; i < framebuffer->texture_ids.size(); i++) {
  3204. Texture *texture = texture_owner.get_or_null(framebuffer->texture_ids[i]);
  3205. // We only check for our VRS usage bit if this is not the first texture id.
  3206. // If it is the first we're likely populating our VRS texture.
  3207. // Bit dirty but...
  3208. if (!texture || (!(texture->usage_flags & TEXTURE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT) && !(i != 0 && texture->usage_flags & TEXTURE_USAGE_VRS_ATTACHMENT_BIT))) {
  3209. if (!texture || !texture->is_resolve_buffer) {
  3210. color_count++;
  3211. }
  3212. }
  3213. }
  3214. ERR_FAIL_COND_V_MSG(p_clear_color_values.size() != color_count, INVALID_ID, "Clear color values supplied (" + itos(p_clear_color_values.size()) + ") differ from the amount required for framebuffer color attachments (" + itos(color_count) + ").");
  3215. }
  3216. RDD::FramebufferID fb_driver_id;
  3217. RDD::RenderPassID render_pass;
  3218. Error err = _draw_list_setup_framebuffer(framebuffer, p_initial_color_action, p_final_color_action, p_initial_depth_action, p_final_depth_action, &fb_driver_id, &render_pass, &draw_list_subpass_count);
  3219. ERR_FAIL_COND_V(err != OK, INVALID_ID);
  3220. RDD::CommandBufferID command_buffer = frames[frame].draw_command_buffer;
  3221. err = _draw_list_render_pass_begin(framebuffer, p_initial_color_action, p_final_color_action, p_initial_depth_action, p_final_depth_action, p_clear_color_values, p_clear_depth, p_clear_stencil, viewport_offset, viewport_size, fb_driver_id, render_pass, command_buffer, RDD::COMMAND_BUFFER_TYPE_PRIMARY, p_storage_textures, constrained_to_region);
  3222. if (err != OK) {
  3223. return INVALID_ID;
  3224. }
  3225. draw_list_render_pass = render_pass;
  3226. draw_list_vkframebuffer = fb_driver_id;
  3227. _draw_list_allocate(Rect2i(viewport_offset, viewport_size), 0, 0);
  3228. #ifdef DEBUG_ENABLED
  3229. draw_list_framebuffer_format = framebuffer->format_id;
  3230. #endif
  3231. draw_list_current_subpass = 0;
  3232. if (needs_clear_color || needs_clear_depth) {
  3233. DEV_ASSERT(!constrained_to_region);
  3234. _draw_list_insert_clear_region(draw_list, framebuffer, viewport_offset, viewport_size, needs_clear_color, p_clear_color_values, needs_clear_depth, p_clear_depth, p_clear_stencil);
  3235. }
  3236. driver->command_render_set_viewport(command_buffer, Rect2i(viewport_offset, viewport_size));
  3237. driver->command_render_set_scissor(command_buffer, Rect2i(viewport_offset, viewport_size));
  3238. return int64_t(ID_TYPE_DRAW_LIST) << ID_BASE_SHIFT;
  3239. }
  3240. Error RenderingDevice::draw_list_begin_split(RID p_framebuffer, uint32_t p_splits, DrawListID *r_split_ids, InitialAction p_initial_color_action, FinalAction p_final_color_action, InitialAction p_initial_depth_action, FinalAction p_final_depth_action, const Vector<Color> &p_clear_color_values, float p_clear_depth, uint32_t p_clear_stencil, const Rect2 &p_region, const Vector<RID> &p_storage_textures) {
  3241. _THREAD_SAFE_METHOD_
  3242. ERR_FAIL_COND_V_MSG(draw_list != nullptr, ERR_BUSY, "Only one draw list can be active at the same time.");
  3243. ERR_FAIL_COND_V_MSG(compute_list != nullptr && !compute_list->state.allow_draw_overlap, ERR_BUSY, "Only one draw/compute list can be active at the same time.");
  3244. ERR_FAIL_COND_V(p_splits < 1, ERR_INVALID_DECLARATION);
  3245. Framebuffer *framebuffer = framebuffer_owner.get_or_null(p_framebuffer);
  3246. ERR_FAIL_NULL_V(framebuffer, ERR_INVALID_DECLARATION);
  3247. Point2i viewport_offset;
  3248. Point2i viewport_size = framebuffer->size;
  3249. bool constrained_to_region = false;
  3250. bool needs_clear_color = false;
  3251. bool needs_clear_depth = false;
  3252. if (p_region != Rect2() && p_region != Rect2(Vector2(), viewport_size)) { // Check custom region.
  3253. Rect2i viewport(viewport_offset, viewport_size);
  3254. Rect2i regioni = p_region;
  3255. if (!(regioni.position.x >= viewport.position.x) && (regioni.position.y >= viewport.position.y) &&
  3256. ((regioni.position.x + regioni.size.x) <= (viewport.position.x + viewport.size.x)) &&
  3257. ((regioni.position.y + regioni.size.y) <= (viewport.position.y + viewport.size.y))) {
  3258. ERR_FAIL_V_MSG(ERR_INVALID_PARAMETER, "When supplying a custom region, it must be contained within the framebuffer rectangle");
  3259. }
  3260. viewport_offset = regioni.position;
  3261. viewport_size = regioni.size;
  3262. // If clearing regions both in color and depth, we can switch to a fast path where we let Vulkan to the clears
  3263. // and we constrain the render area to the region.
  3264. if (p_initial_color_action == INITIAL_ACTION_CLEAR_REGION && p_initial_depth_action == INITIAL_ACTION_CLEAR_REGION) {
  3265. constrained_to_region = true;
  3266. p_initial_color_action = INITIAL_ACTION_CLEAR;
  3267. p_initial_depth_action = INITIAL_ACTION_CLEAR;
  3268. } else {
  3269. if (p_initial_color_action == INITIAL_ACTION_CLEAR_REGION_CONTINUE) {
  3270. needs_clear_color = true;
  3271. p_initial_color_action = INITIAL_ACTION_CONTINUE;
  3272. }
  3273. if (p_initial_depth_action == INITIAL_ACTION_CLEAR_REGION_CONTINUE) {
  3274. needs_clear_depth = true;
  3275. p_initial_depth_action = INITIAL_ACTION_CONTINUE;
  3276. }
  3277. if (p_initial_color_action == INITIAL_ACTION_CLEAR_REGION) {
  3278. needs_clear_color = true;
  3279. p_initial_color_action = INITIAL_ACTION_KEEP;
  3280. }
  3281. if (p_initial_depth_action == INITIAL_ACTION_CLEAR_REGION) {
  3282. needs_clear_depth = true;
  3283. p_initial_depth_action = INITIAL_ACTION_KEEP;
  3284. }
  3285. }
  3286. }
  3287. if (p_initial_color_action == INITIAL_ACTION_CLEAR || needs_clear_color) { // Check clear values.
  3288. int color_count = 0;
  3289. for (int i = 0; i < framebuffer->texture_ids.size(); i++) {
  3290. Texture *texture = texture_owner.get_or_null(framebuffer->texture_ids[i]);
  3291. if (!texture || !(texture->usage_flags & TEXTURE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT)) {
  3292. color_count++;
  3293. }
  3294. }
  3295. ERR_FAIL_COND_V_MSG(p_clear_color_values.size() != color_count, ERR_INVALID_PARAMETER,
  3296. "Clear color values supplied (" + itos(p_clear_color_values.size()) + ") differ from the amount required for framebuffer (" + itos(color_count) + ").");
  3297. }
  3298. RDD::FramebufferID fb_driver_id;
  3299. RDD::RenderPassID render_pass;
  3300. Error err = _draw_list_setup_framebuffer(framebuffer, p_initial_color_action, p_final_color_action, p_initial_depth_action, p_final_depth_action, &fb_driver_id, &render_pass, &draw_list_subpass_count);
  3301. ERR_FAIL_COND_V(err != OK, ERR_CANT_CREATE);
  3302. RDD::CommandBufferID frame_command_buffer = frames[frame].draw_command_buffer;
  3303. err = _draw_list_render_pass_begin(framebuffer, p_initial_color_action, p_final_color_action, p_initial_depth_action, p_final_depth_action, p_clear_color_values, p_clear_depth, p_clear_stencil, viewport_offset, viewport_size, fb_driver_id, render_pass, frame_command_buffer, RDD::COMMAND_BUFFER_TYPE_SECONDARY, p_storage_textures, constrained_to_region);
  3304. if (err != OK) {
  3305. return ERR_CANT_CREATE;
  3306. }
  3307. draw_list_current_subpass = 0;
  3308. #ifdef DEBUG_ENABLED
  3309. draw_list_framebuffer_format = framebuffer->format_id;
  3310. #endif
  3311. draw_list_render_pass = render_pass;
  3312. draw_list_vkframebuffer = fb_driver_id;
  3313. err = _draw_list_allocate(Rect2i(viewport_offset, viewport_size), p_splits, 0);
  3314. if (err != OK) {
  3315. return err;
  3316. }
  3317. if (needs_clear_color || needs_clear_depth) {
  3318. DEV_ASSERT(!constrained_to_region);
  3319. _draw_list_insert_clear_region(&draw_list[0], framebuffer, viewport_offset, viewport_size, needs_clear_color, p_clear_color_values, needs_clear_depth, p_clear_depth, p_clear_stencil);
  3320. }
  3321. bool secondary_viewport_scissor = driver->api_trait_get(RDD::API_TRAIT_SECONDARY_VIEWPORT_SCISSOR);
  3322. for (uint32_t i = 0; i < p_splits; i++) {
  3323. if (secondary_viewport_scissor) {
  3324. driver->command_render_set_viewport(draw_list[i].command_buffer, Rect2i(viewport_offset, viewport_size));
  3325. driver->command_render_set_scissor(draw_list[i].command_buffer, Rect2i(viewport_offset, viewport_size));
  3326. }
  3327. r_split_ids[i] = (int64_t(ID_TYPE_SPLIT_DRAW_LIST) << ID_BASE_SHIFT) + i;
  3328. }
  3329. return OK;
  3330. }
  3331. RenderingDevice::DrawList *RenderingDevice::_get_draw_list_ptr(DrawListID p_id) {
  3332. if (p_id < 0) {
  3333. return nullptr;
  3334. }
  3335. if (!draw_list) {
  3336. return nullptr;
  3337. } else if (p_id == (int64_t(ID_TYPE_DRAW_LIST) << ID_BASE_SHIFT)) {
  3338. if (draw_list_split) {
  3339. return nullptr;
  3340. }
  3341. return draw_list;
  3342. } else if (p_id >> DrawListID(ID_BASE_SHIFT) == ID_TYPE_SPLIT_DRAW_LIST) {
  3343. if (!draw_list_split) {
  3344. return nullptr;
  3345. }
  3346. uint64_t index = p_id & ((DrawListID(1) << DrawListID(ID_BASE_SHIFT)) - 1); // Mask.
  3347. if (index >= draw_list_count) {
  3348. return nullptr;
  3349. }
  3350. return &draw_list[index];
  3351. } else {
  3352. return nullptr;
  3353. }
  3354. }
  3355. void RenderingDevice::draw_list_set_blend_constants(DrawListID p_list, const Color &p_color) {
  3356. DrawList *dl = _get_draw_list_ptr(p_list);
  3357. ERR_FAIL_NULL(dl);
  3358. #ifdef DEBUG_ENABLED
  3359. ERR_FAIL_COND_MSG(!dl->validation.active, "Submitted Draw Lists can no longer be modified.");
  3360. #endif
  3361. driver->command_render_set_blend_constants(dl->command_buffer, p_color);
  3362. }
  3363. void RenderingDevice::draw_list_bind_render_pipeline(DrawListID p_list, RID p_render_pipeline) {
  3364. DrawList *dl = _get_draw_list_ptr(p_list);
  3365. ERR_FAIL_NULL(dl);
  3366. #ifdef DEBUG_ENABLED
  3367. ERR_FAIL_COND_MSG(!dl->validation.active, "Submitted Draw Lists can no longer be modified.");
  3368. #endif
  3369. const RenderPipeline *pipeline = render_pipeline_owner.get_or_null(p_render_pipeline);
  3370. ERR_FAIL_NULL(pipeline);
  3371. #ifdef DEBUG_ENABLED
  3372. ERR_FAIL_COND(pipeline->validation.framebuffer_format != draw_list_framebuffer_format && pipeline->validation.render_pass != draw_list_current_subpass);
  3373. #endif
  3374. if (p_render_pipeline == dl->state.pipeline) {
  3375. return; // Redundant state, return.
  3376. }
  3377. dl->state.pipeline = p_render_pipeline;
  3378. driver->command_bind_render_pipeline(dl->command_buffer, pipeline->driver_id);
  3379. if (dl->state.pipeline_shader != pipeline->shader) {
  3380. // Shader changed, so descriptor sets may become incompatible.
  3381. uint32_t pcount = pipeline->set_formats.size(); // Formats count in this pipeline.
  3382. dl->state.set_count = MAX(dl->state.set_count, pcount);
  3383. const uint32_t *pformats = pipeline->set_formats.ptr(); // Pipeline set formats.
  3384. uint32_t first_invalid_set = UINT32_MAX; // All valid by default.
  3385. switch (driver->api_trait_get(RDD::API_TRAIT_SHADER_CHANGE_INVALIDATION)) {
  3386. case RDD::SHADER_CHANGE_INVALIDATION_ALL_BOUND_UNIFORM_SETS: {
  3387. first_invalid_set = 0;
  3388. } break;
  3389. case RDD::SHADER_CHANGE_INVALIDATION_INCOMPATIBLE_SETS_PLUS_CASCADE: {
  3390. for (uint32_t i = 0; i < pcount; i++) {
  3391. if (dl->state.sets[i].pipeline_expected_format != pformats[i]) {
  3392. first_invalid_set = i;
  3393. break;
  3394. }
  3395. }
  3396. } break;
  3397. case RDD::SHADER_CHANGE_INVALIDATION_ALL_OR_NONE_ACCORDING_TO_LAYOUT_HASH: {
  3398. if (dl->state.pipeline_shader_layout_hash != pipeline->shader_layout_hash) {
  3399. first_invalid_set = 0;
  3400. }
  3401. } break;
  3402. }
  3403. for (uint32_t i = 0; i < pcount; i++) {
  3404. dl->state.sets[i].bound = dl->state.sets[i].bound && i < first_invalid_set;
  3405. dl->state.sets[i].pipeline_expected_format = pformats[i];
  3406. }
  3407. for (uint32_t i = pcount; i < dl->state.set_count; i++) {
  3408. // Unbind the ones above (not used) if exist.
  3409. dl->state.sets[i].bound = false;
  3410. }
  3411. dl->state.set_count = pcount; // Update set count.
  3412. if (pipeline->push_constant_size) {
  3413. #ifdef DEBUG_ENABLED
  3414. dl->validation.pipeline_push_constant_supplied = false;
  3415. #endif
  3416. }
  3417. dl->state.pipeline_shader = pipeline->shader;
  3418. dl->state.pipeline_shader_driver_id = pipeline->shader_driver_id;
  3419. dl->state.pipeline_shader_layout_hash = pipeline->shader_layout_hash;
  3420. }
  3421. #ifdef DEBUG_ENABLED
  3422. // Update render pass pipeline info.
  3423. dl->validation.pipeline_active = true;
  3424. dl->validation.pipeline_dynamic_state = pipeline->validation.dynamic_state;
  3425. dl->validation.pipeline_vertex_format = pipeline->validation.vertex_format;
  3426. dl->validation.pipeline_uses_restart_indices = pipeline->validation.uses_restart_indices;
  3427. dl->validation.pipeline_primitive_divisor = pipeline->validation.primitive_divisor;
  3428. dl->validation.pipeline_primitive_minimum = pipeline->validation.primitive_minimum;
  3429. dl->validation.pipeline_push_constant_size = pipeline->push_constant_size;
  3430. #endif
  3431. }
  3432. void RenderingDevice::draw_list_bind_uniform_set(DrawListID p_list, RID p_uniform_set, uint32_t p_index) {
  3433. #ifdef DEBUG_ENABLED
  3434. ERR_FAIL_COND_MSG(p_index >= driver->limit_get(LIMIT_MAX_BOUND_UNIFORM_SETS) || p_index >= MAX_UNIFORM_SETS,
  3435. "Attempting to bind a descriptor set (" + itos(p_index) + ") greater than what the hardware supports (" + itos(driver->limit_get(LIMIT_MAX_BOUND_UNIFORM_SETS)) + ").");
  3436. #endif
  3437. DrawList *dl = _get_draw_list_ptr(p_list);
  3438. ERR_FAIL_NULL(dl);
  3439. #ifdef DEBUG_ENABLED
  3440. ERR_FAIL_COND_MSG(!dl->validation.active, "Submitted Draw Lists can no longer be modified.");
  3441. #endif
  3442. const UniformSet *uniform_set = uniform_set_owner.get_or_null(p_uniform_set);
  3443. ERR_FAIL_NULL(uniform_set);
  3444. if (p_index > dl->state.set_count) {
  3445. dl->state.set_count = p_index;
  3446. }
  3447. dl->state.sets[p_index].uniform_set_driver_id = uniform_set->driver_id; // Update set pointer.
  3448. dl->state.sets[p_index].bound = false; // Needs rebind.
  3449. dl->state.sets[p_index].uniform_set_format = uniform_set->format;
  3450. dl->state.sets[p_index].uniform_set = p_uniform_set;
  3451. if (driver->api_trait_get(RDD::API_TRAIT_HONORS_PIPELINE_BARRIERS)) {
  3452. uint32_t mst_count = uniform_set->mutable_storage_textures.size();
  3453. if (mst_count) {
  3454. Texture **mst_textures = const_cast<UniformSet *>(uniform_set)->mutable_storage_textures.ptrw();
  3455. for (uint32_t i = 0; i < mst_count; i++) {
  3456. if (mst_textures[i]->used_in_frame != frames_drawn) {
  3457. mst_textures[i]->used_in_frame = frames_drawn;
  3458. mst_textures[i]->used_in_transfer = false;
  3459. mst_textures[i]->used_in_compute = false;
  3460. }
  3461. mst_textures[i]->used_in_raster = true;
  3462. }
  3463. }
  3464. }
  3465. #ifdef DEBUG_ENABLED
  3466. { // Validate that textures bound are not attached as framebuffer bindings.
  3467. uint32_t attachable_count = uniform_set->attachable_textures.size();
  3468. const UniformSet::AttachableTexture *attachable_ptr = uniform_set->attachable_textures.ptr();
  3469. uint32_t bound_count = draw_list_bound_textures.size();
  3470. const RID *bound_ptr = draw_list_bound_textures.ptr();
  3471. for (uint32_t i = 0; i < attachable_count; i++) {
  3472. for (uint32_t j = 0; j < bound_count; j++) {
  3473. ERR_FAIL_COND_MSG(attachable_ptr[i].texture == bound_ptr[j],
  3474. "Attempted to use the same texture in framebuffer attachment and a uniform (set: " + itos(p_index) + ", binding: " + itos(attachable_ptr[i].bind) + "), this is not allowed.");
  3475. }
  3476. }
  3477. }
  3478. #endif
  3479. }
  3480. void RenderingDevice::draw_list_bind_vertex_array(DrawListID p_list, RID p_vertex_array) {
  3481. DrawList *dl = _get_draw_list_ptr(p_list);
  3482. ERR_FAIL_NULL(dl);
  3483. #ifdef DEBUG_ENABLED
  3484. ERR_FAIL_COND_MSG(!dl->validation.active, "Submitted Draw Lists can no longer be modified.");
  3485. #endif
  3486. const VertexArray *vertex_array = vertex_array_owner.get_or_null(p_vertex_array);
  3487. ERR_FAIL_NULL(vertex_array);
  3488. if (dl->state.vertex_array == p_vertex_array) {
  3489. return; // Already set.
  3490. }
  3491. dl->state.vertex_array = p_vertex_array;
  3492. #ifdef DEBUG_ENABLED
  3493. dl->validation.vertex_format = vertex_array->description;
  3494. dl->validation.vertex_max_instances_allowed = vertex_array->max_instances_allowed;
  3495. #endif
  3496. dl->validation.vertex_array_size = vertex_array->vertex_count;
  3497. driver->command_render_bind_vertex_buffers(dl->command_buffer, vertex_array->buffers.size(), vertex_array->buffers.ptr(), vertex_array->offsets.ptr());
  3498. }
  3499. void RenderingDevice::draw_list_bind_index_array(DrawListID p_list, RID p_index_array) {
  3500. DrawList *dl = _get_draw_list_ptr(p_list);
  3501. ERR_FAIL_NULL(dl);
  3502. #ifdef DEBUG_ENABLED
  3503. ERR_FAIL_COND_MSG(!dl->validation.active, "Submitted Draw Lists can no longer be modified.");
  3504. #endif
  3505. const IndexArray *index_array = index_array_owner.get_or_null(p_index_array);
  3506. ERR_FAIL_NULL(index_array);
  3507. if (dl->state.index_array == p_index_array) {
  3508. return; // Already set.
  3509. }
  3510. dl->state.index_array = p_index_array;
  3511. #ifdef DEBUG_ENABLED
  3512. dl->validation.index_array_max_index = index_array->max_index;
  3513. #endif
  3514. dl->validation.index_array_size = index_array->indices;
  3515. dl->validation.index_array_offset = index_array->offset;
  3516. driver->command_render_bind_index_buffer(dl->command_buffer, index_array->driver_id, index_array->format, index_array->offset);
  3517. }
  3518. void RenderingDevice::draw_list_set_line_width(DrawListID p_list, float p_width) {
  3519. DrawList *dl = _get_draw_list_ptr(p_list);
  3520. ERR_FAIL_NULL(dl);
  3521. #ifdef DEBUG_ENABLED
  3522. ERR_FAIL_COND_MSG(!dl->validation.active, "Submitted Draw Lists can no longer be modified.");
  3523. #endif
  3524. driver->command_render_set_line_width(dl->command_buffer, p_width);
  3525. }
  3526. void RenderingDevice::draw_list_set_push_constant(DrawListID p_list, const void *p_data, uint32_t p_data_size) {
  3527. DrawList *dl = _get_draw_list_ptr(p_list);
  3528. ERR_FAIL_NULL(dl);
  3529. #ifdef DEBUG_ENABLED
  3530. ERR_FAIL_COND_MSG(!dl->validation.active, "Submitted Draw Lists can no longer be modified.");
  3531. #endif
  3532. #ifdef DEBUG_ENABLED
  3533. ERR_FAIL_COND_MSG(p_data_size != dl->validation.pipeline_push_constant_size,
  3534. "This render pipeline requires (" + itos(dl->validation.pipeline_push_constant_size) + ") bytes of push constant data, supplied: (" + itos(p_data_size) + ")");
  3535. #endif
  3536. driver->command_bind_push_constants(dl->command_buffer, dl->state.pipeline_shader_driver_id, 0, VectorView((const uint32_t *)p_data, p_data_size / sizeof(uint32_t)));
  3537. #ifdef DEBUG_ENABLED
  3538. dl->validation.pipeline_push_constant_supplied = true;
  3539. #endif
  3540. }
  3541. void RenderingDevice::draw_list_draw(DrawListID p_list, bool p_use_indices, uint32_t p_instances, uint32_t p_procedural_vertices) {
  3542. DrawList *dl = _get_draw_list_ptr(p_list);
  3543. ERR_FAIL_NULL(dl);
  3544. #ifdef DEBUG_ENABLED
  3545. ERR_FAIL_COND_MSG(!dl->validation.active, "Submitted Draw Lists can no longer be modified.");
  3546. #endif
  3547. #ifdef DEBUG_ENABLED
  3548. ERR_FAIL_COND_MSG(!dl->validation.pipeline_active,
  3549. "No render pipeline was set before attempting to draw.");
  3550. if (dl->validation.pipeline_vertex_format != INVALID_ID) {
  3551. // Pipeline uses vertices, validate format.
  3552. ERR_FAIL_COND_MSG(dl->validation.vertex_format == INVALID_ID,
  3553. "No vertex array was bound, and render pipeline expects vertices.");
  3554. // Make sure format is right.
  3555. ERR_FAIL_COND_MSG(dl->validation.pipeline_vertex_format != dl->validation.vertex_format,
  3556. "The vertex format used to create the pipeline does not match the vertex format bound.");
  3557. // Make sure number of instances is valid.
  3558. ERR_FAIL_COND_MSG(p_instances > dl->validation.vertex_max_instances_allowed,
  3559. "Number of instances requested (" + itos(p_instances) + " is larger than the maximum number supported by the bound vertex array (" + itos(dl->validation.vertex_max_instances_allowed) + ").");
  3560. }
  3561. if (dl->validation.pipeline_push_constant_size > 0) {
  3562. // Using push constants, check that they were supplied.
  3563. ERR_FAIL_COND_MSG(!dl->validation.pipeline_push_constant_supplied,
  3564. "The shader in this pipeline requires a push constant to be set before drawing, but it's not present.");
  3565. }
  3566. #endif
  3567. // Bind descriptor sets.
  3568. for (uint32_t i = 0; i < dl->state.set_count; i++) {
  3569. if (dl->state.sets[i].pipeline_expected_format == 0) {
  3570. continue; // Nothing expected by this pipeline.
  3571. }
  3572. #ifdef DEBUG_ENABLED
  3573. if (dl->state.sets[i].pipeline_expected_format != dl->state.sets[i].uniform_set_format) {
  3574. if (dl->state.sets[i].uniform_set_format == 0) {
  3575. ERR_FAIL_MSG("Uniforms were never supplied for set (" + itos(i) + ") at the time of drawing, which are required by the pipeline");
  3576. } else if (uniform_set_owner.owns(dl->state.sets[i].uniform_set)) {
  3577. UniformSet *us = uniform_set_owner.get_or_null(dl->state.sets[i].uniform_set);
  3578. ERR_FAIL_MSG("Uniforms supplied for set (" + itos(i) + "):\n" + _shader_uniform_debug(us->shader_id, us->shader_set) + "\nare not the same format as required by the pipeline shader. Pipeline shader requires the following bindings:\n" + _shader_uniform_debug(dl->state.pipeline_shader));
  3579. } else {
  3580. ERR_FAIL_MSG("Uniforms supplied for set (" + itos(i) + ", which was was just freed) are not the same format as required by the pipeline shader. Pipeline shader requires the following bindings:\n" + _shader_uniform_debug(dl->state.pipeline_shader));
  3581. }
  3582. }
  3583. #endif
  3584. driver->command_uniform_set_prepare_for_use(dl->command_buffer, dl->state.sets[i].uniform_set_driver_id, dl->state.pipeline_shader_driver_id, i);
  3585. }
  3586. for (uint32_t i = 0; i < dl->state.set_count; i++) {
  3587. if (dl->state.sets[i].pipeline_expected_format == 0) {
  3588. continue; // Nothing expected by this pipeline.
  3589. }
  3590. if (!dl->state.sets[i].bound) {
  3591. driver->command_bind_render_uniform_set(dl->command_buffer, dl->state.sets[i].uniform_set_driver_id, dl->state.pipeline_shader_driver_id, i);
  3592. dl->state.sets[i].bound = true;
  3593. }
  3594. }
  3595. if (p_use_indices) {
  3596. #ifdef DEBUG_ENABLED
  3597. ERR_FAIL_COND_MSG(p_procedural_vertices > 0,
  3598. "Procedural vertices can't be used together with indices.");
  3599. ERR_FAIL_COND_MSG(!dl->validation.index_array_size,
  3600. "Draw command requested indices, but no index buffer was set.");
  3601. ERR_FAIL_COND_MSG(dl->validation.pipeline_uses_restart_indices != dl->validation.index_buffer_uses_restart_indices,
  3602. "The usage of restart indices in index buffer does not match the render primitive in the pipeline.");
  3603. #endif
  3604. uint32_t to_draw = dl->validation.index_array_size;
  3605. #ifdef DEBUG_ENABLED
  3606. ERR_FAIL_COND_MSG(to_draw < dl->validation.pipeline_primitive_minimum,
  3607. "Too few indices (" + itos(to_draw) + ") for the render primitive set in the render pipeline (" + itos(dl->validation.pipeline_primitive_minimum) + ").");
  3608. ERR_FAIL_COND_MSG((to_draw % dl->validation.pipeline_primitive_divisor) != 0,
  3609. "Index amount (" + itos(to_draw) + ") must be a multiple of the amount of indices required by the render primitive (" + itos(dl->validation.pipeline_primitive_divisor) + ").");
  3610. #endif
  3611. driver->command_render_draw_indexed(dl->command_buffer, to_draw, p_instances, dl->validation.index_array_offset, 0, 0);
  3612. } else {
  3613. uint32_t to_draw;
  3614. if (p_procedural_vertices > 0) {
  3615. #ifdef DEBUG_ENABLED
  3616. ERR_FAIL_COND_MSG(dl->validation.pipeline_vertex_format != INVALID_ID,
  3617. "Procedural vertices requested, but pipeline expects a vertex array.");
  3618. #endif
  3619. to_draw = p_procedural_vertices;
  3620. } else {
  3621. #ifdef DEBUG_ENABLED
  3622. ERR_FAIL_COND_MSG(dl->validation.pipeline_vertex_format == INVALID_ID,
  3623. "Draw command lacks indices, but pipeline format does not use vertices.");
  3624. #endif
  3625. to_draw = dl->validation.vertex_array_size;
  3626. }
  3627. #ifdef DEBUG_ENABLED
  3628. ERR_FAIL_COND_MSG(to_draw < dl->validation.pipeline_primitive_minimum,
  3629. "Too few vertices (" + itos(to_draw) + ") for the render primitive set in the render pipeline (" + itos(dl->validation.pipeline_primitive_minimum) + ").");
  3630. ERR_FAIL_COND_MSG((to_draw % dl->validation.pipeline_primitive_divisor) != 0,
  3631. "Vertex amount (" + itos(to_draw) + ") must be a multiple of the amount of vertices required by the render primitive (" + itos(dl->validation.pipeline_primitive_divisor) + ").");
  3632. #endif
  3633. driver->command_render_draw(dl->command_buffer, to_draw, p_instances, 0, 0);
  3634. }
  3635. }
  3636. void RenderingDevice::draw_list_enable_scissor(DrawListID p_list, const Rect2 &p_rect) {
  3637. DrawList *dl = _get_draw_list_ptr(p_list);
  3638. ERR_FAIL_NULL(dl);
  3639. #ifdef DEBUG_ENABLED
  3640. ERR_FAIL_COND_MSG(!dl->validation.active, "Submitted Draw Lists can no longer be modified.");
  3641. #endif
  3642. Rect2i rect = p_rect;
  3643. rect.position += dl->viewport.position;
  3644. rect = dl->viewport.intersection(rect);
  3645. if (rect.get_area() == 0) {
  3646. return;
  3647. }
  3648. driver->command_render_set_scissor(dl->command_buffer, rect);
  3649. }
  3650. void RenderingDevice::draw_list_disable_scissor(DrawListID p_list) {
  3651. DrawList *dl = _get_draw_list_ptr(p_list);
  3652. ERR_FAIL_NULL(dl);
  3653. #ifdef DEBUG_ENABLED
  3654. ERR_FAIL_COND_MSG(!dl->validation.active, "Submitted Draw Lists can no longer be modified.");
  3655. #endif
  3656. driver->command_render_set_scissor(dl->command_buffer, dl->viewport);
  3657. }
  3658. uint32_t RenderingDevice::draw_list_get_current_pass() {
  3659. return draw_list_current_subpass;
  3660. }
  3661. RenderingDevice::DrawListID RenderingDevice::draw_list_switch_to_next_pass() {
  3662. _THREAD_SAFE_METHOD_
  3663. ERR_FAIL_COND_V(draw_list == nullptr, INVALID_ID);
  3664. ERR_FAIL_COND_V(draw_list_current_subpass >= draw_list_subpass_count - 1, INVALID_FORMAT_ID);
  3665. draw_list_current_subpass++;
  3666. Rect2i viewport;
  3667. _draw_list_free(&viewport);
  3668. driver->command_next_render_subpass(frames[frame].draw_command_buffer, RDD::COMMAND_BUFFER_TYPE_PRIMARY);
  3669. _draw_list_allocate(viewport, 0, draw_list_current_subpass);
  3670. return int64_t(ID_TYPE_DRAW_LIST) << ID_BASE_SHIFT;
  3671. }
  3672. Error RenderingDevice::draw_list_switch_to_next_pass_split(uint32_t p_splits, DrawListID *r_split_ids) {
  3673. _THREAD_SAFE_METHOD_
  3674. ERR_FAIL_COND_V(draw_list == nullptr, ERR_INVALID_PARAMETER);
  3675. ERR_FAIL_COND_V(draw_list_current_subpass >= draw_list_subpass_count - 1, ERR_INVALID_PARAMETER);
  3676. draw_list_current_subpass++;
  3677. Rect2i viewport;
  3678. _draw_list_free(&viewport);
  3679. driver->command_next_render_subpass(frames[frame].draw_command_buffer, RDD::COMMAND_BUFFER_TYPE_PRIMARY);
  3680. _draw_list_allocate(viewport, p_splits, draw_list_current_subpass);
  3681. for (uint32_t i = 0; i < p_splits; i++) {
  3682. r_split_ids[i] = (int64_t(ID_TYPE_SPLIT_DRAW_LIST) << ID_BASE_SHIFT) + i;
  3683. }
  3684. return OK;
  3685. }
  3686. Error RenderingDevice::_draw_list_allocate(const Rect2i &p_viewport, uint32_t p_splits, uint32_t p_subpass) {
  3687. // Lock while draw_list is active.
  3688. _THREAD_SAFE_LOCK_
  3689. if (p_splits == 0) {
  3690. draw_list = memnew(DrawList);
  3691. draw_list->command_buffer = frames[frame].draw_command_buffer;
  3692. draw_list->viewport = p_viewport;
  3693. draw_list_count = 0;
  3694. draw_list_split = false;
  3695. } else {
  3696. if (p_splits > (uint32_t)split_draw_list_allocators.size()) {
  3697. uint32_t from = split_draw_list_allocators.size();
  3698. split_draw_list_allocators.resize(p_splits);
  3699. for (uint32_t i = from; i < p_splits; i++) {
  3700. RDD::CommandPoolID cmd_pool = driver->command_pool_create(RDD::COMMAND_BUFFER_TYPE_SECONDARY);
  3701. ERR_FAIL_COND_V(!cmd_pool, ERR_CANT_CREATE);
  3702. split_draw_list_allocators.write[i].command_pool = cmd_pool;
  3703. for (int j = 0; j < frame_count; j++) {
  3704. RDD::CommandBufferID cmd_buffer = driver->command_buffer_create(RDD::COMMAND_BUFFER_TYPE_SECONDARY, cmd_pool);
  3705. ERR_FAIL_COND_V(!cmd_buffer, ERR_CANT_CREATE);
  3706. split_draw_list_allocators.write[i].command_buffers.push_back(cmd_buffer);
  3707. }
  3708. }
  3709. }
  3710. draw_list = memnew_arr(DrawList, p_splits);
  3711. draw_list_count = p_splits;
  3712. draw_list_split = true;
  3713. for (uint32_t i = 0; i < p_splits; i++) {
  3714. // Take a command buffer and initialize it.
  3715. RDD::CommandBufferID cmd_buffer = split_draw_list_allocators[i].command_buffers[frame];
  3716. bool ok = driver->command_buffer_begin_secondary(cmd_buffer, draw_list_render_pass, p_subpass, draw_list_vkframebuffer);
  3717. if (!ok) {
  3718. memdelete_arr(draw_list);
  3719. draw_list = nullptr;
  3720. ERR_FAIL_V(ERR_CANT_CREATE);
  3721. }
  3722. draw_list[i].command_buffer = cmd_buffer;
  3723. draw_list[i].viewport = p_viewport;
  3724. }
  3725. }
  3726. return OK;
  3727. }
  3728. void RenderingDevice::_draw_list_free(Rect2i *r_last_viewport) {
  3729. if (draw_list_split) {
  3730. // Send all command buffers.
  3731. RDD::CommandBufferID *command_buffers = (RDD::CommandBufferID *)alloca(sizeof(RDD::CommandBufferID) * draw_list_count);
  3732. for (uint32_t i = 0; i < draw_list_count; i++) {
  3733. driver->command_buffer_end(draw_list[i].command_buffer);
  3734. command_buffers[i] = draw_list[i].command_buffer;
  3735. if (r_last_viewport) {
  3736. if (i == 0 || draw_list[i].viewport_set) {
  3737. *r_last_viewport = draw_list[i].viewport;
  3738. }
  3739. }
  3740. }
  3741. driver->command_buffer_execute_secondary(frames[frame].draw_command_buffer, VectorView(command_buffers, draw_list_count));
  3742. memdelete_arr(draw_list);
  3743. draw_list = nullptr;
  3744. } else {
  3745. if (r_last_viewport) {
  3746. *r_last_viewport = draw_list->viewport;
  3747. }
  3748. // Just end the list.
  3749. memdelete(draw_list);
  3750. draw_list = nullptr;
  3751. }
  3752. // Draw_list is no longer active.
  3753. _THREAD_SAFE_UNLOCK_
  3754. }
  3755. void RenderingDevice::draw_list_end(BitField<BarrierMask> p_post_barrier) {
  3756. _THREAD_SAFE_METHOD_
  3757. ERR_FAIL_COND_MSG(!draw_list, "Immediate draw list is already inactive.");
  3758. _draw_list_free();
  3759. driver->command_end_render_pass(frames[frame].draw_command_buffer);
  3760. for (int i = 0; i < draw_list_bound_textures.size(); i++) {
  3761. Texture *texture = texture_owner.get_or_null(draw_list_bound_textures[i]);
  3762. ERR_CONTINUE(!texture); // Wtf.
  3763. if (draw_list_unbind_color_textures && (texture->usage_flags & TEXTURE_USAGE_COLOR_ATTACHMENT_BIT)) {
  3764. texture->bound = false;
  3765. }
  3766. if (draw_list_unbind_depth_textures && (texture->usage_flags & TEXTURE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT)) {
  3767. texture->bound = false;
  3768. }
  3769. }
  3770. draw_list_bound_textures.clear();
  3771. if (driver->api_trait_get(RDD::API_TRAIT_HONORS_PIPELINE_BARRIERS)) {
  3772. BitField<RDD::PipelineStageBits> dst_stages;
  3773. BitField<RDD::BarrierAccessBits> dst_access;
  3774. if (p_post_barrier.has_flag(BARRIER_MASK_COMPUTE)) {
  3775. dst_stages.set_flag(RDD::PIPELINE_STAGE_COMPUTE_SHADER_BIT);
  3776. dst_access.set_flag(RDD::BARRIER_ACCESS_SHADER_READ_BIT).set_flag(RDD::BARRIER_ACCESS_SHADER_WRITE_BIT);
  3777. }
  3778. if (p_post_barrier.has_flag(BARRIER_MASK_VERTEX)) {
  3779. dst_stages.set_flag(RDD::PIPELINE_STAGE_VERTEX_INPUT_BIT).set_flag(RDD::PIPELINE_STAGE_VERTEX_SHADER_BIT); // RDD::PIPELINE_STAGE_DRAW_INDIRECT_BIT
  3780. dst_access.set_flag(RDD::BARRIER_ACCESS_SHADER_READ_BIT).set_flag(RDD::BARRIER_ACCESS_SHADER_WRITE_BIT).set_flag(RDD::BARRIER_ACCESS_INDEX_READ_BIT).set_flag(RDD::BARRIER_ACCESS_VERTEX_ATTRIBUTE_READ_BIT); // RDD::BARRIER_ACCESS_INDIRECT_COMMAND_READ_BIT
  3781. }
  3782. if (p_post_barrier.has_flag(BARRIER_MASK_FRAGMENT)) {
  3783. dst_stages.set_flag(RDD::PIPELINE_STAGE_FRAGMENT_SHADER_BIT); // RDD::PIPELINE_STAGE_DRAW_INDIRECT_BIT
  3784. dst_access.set_flag(RDD::BARRIER_ACCESS_SHADER_READ_BIT).set_flag(RDD::BARRIER_ACCESS_SHADER_WRITE_BIT); // RDD::BARRIER_ACCESS_INDIRECT_COMMAND_READ_BIT
  3785. }
  3786. if (p_post_barrier.has_flag(BARRIER_MASK_TRANSFER)) {
  3787. dst_stages.set_flag(RDD::PIPELINE_STAGE_TRANSFER_BIT);
  3788. dst_access.set_flag(RDD::BARRIER_ACCESS_TRANSFER_WRITE_BIT).set_flag(RDD::BARRIER_ACCESS_TRANSFER_READ_BIT);
  3789. }
  3790. if (dst_stages.is_empty()) {
  3791. dst_stages.set_flag(RDD::PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT);
  3792. }
  3793. RDD::TextureBarrier *texture_barriers = nullptr;
  3794. uint32_t texture_barrier_count = draw_list_storage_textures.size();
  3795. if (texture_barrier_count) {
  3796. texture_barriers = (RDD::TextureBarrier *)alloca(sizeof(RDD::TextureBarrier) * draw_list_storage_textures.size());
  3797. }
  3798. BitField<RDD::PipelineStageBits> src_stage(RDD::PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT |
  3799. RDD::PIPELINE_STAGE_EARLY_FRAGMENT_TESTS_BIT | RDD::PIPELINE_STAGE_LATE_FRAGMENT_TESTS_BIT);
  3800. BitField<RDD::BarrierAccessBits> src_access(
  3801. RDD::BARRIER_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT | RDD::BARRIER_ACCESS_COLOR_ATTACHMENT_WRITE_BIT);
  3802. if (texture_barrier_count) {
  3803. src_stage.set_flag(RDD::PIPELINE_STAGE_VERTEX_SHADER_BIT).set_flag(RDD::PIPELINE_STAGE_FRAGMENT_SHADER_BIT);
  3804. src_access.set_flag(RDD::BARRIER_ACCESS_SHADER_WRITE_BIT);
  3805. }
  3806. for (uint32_t i = 0; i < texture_barrier_count; i++) {
  3807. Texture *texture = texture_owner.get_or_null(draw_list_storage_textures[i]);
  3808. RDD::TextureBarrier &tb = texture_barriers[i];
  3809. tb.texture = texture->driver_id;
  3810. tb.src_access = src_access;
  3811. tb.dst_access = dst_access;
  3812. tb.prev_layout = texture->layout;
  3813. tb.next_layout = RDD::TEXTURE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
  3814. tb.subresources.aspect = texture->read_aspect_flags;
  3815. tb.subresources.base_mipmap = texture->base_mipmap;
  3816. tb.subresources.mipmap_count = texture->mipmaps;
  3817. tb.subresources.base_layer = texture->base_layer;
  3818. tb.subresources.layer_count = texture->layers;
  3819. texture->layout = RDD::TEXTURE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
  3820. }
  3821. // To ensure proper synchronization, we must make sure rendering is done before:
  3822. // * Some buffer is copied.
  3823. // * Another render pass happens (since we may be done).
  3824. RDD::MemoryBarrier mb;
  3825. mb.src_access = src_access;
  3826. mb.dst_access = dst_access;
  3827. if (texture_barrier_count > 0 || p_post_barrier != BARRIER_MASK_NO_BARRIER) {
  3828. driver->command_pipeline_barrier(frames[frame].draw_command_buffer, src_stage, dst_stages, mb, {}, VectorView(texture_barriers, texture_barrier_count));
  3829. }
  3830. }
  3831. draw_list_storage_textures.clear();
  3832. #ifdef FORCE_FULL_BARRIER
  3833. _full_barrier(true);
  3834. #endif
  3835. }
  3836. /***********************/
  3837. /**** COMPUTE LISTS ****/
  3838. /***********************/
  3839. RenderingDevice::ComputeListID RenderingDevice::compute_list_begin(bool p_allow_draw_overlap) {
  3840. _THREAD_SAFE_METHOD_
  3841. ERR_FAIL_COND_V_MSG(!p_allow_draw_overlap && draw_list != nullptr, INVALID_ID, "Only one draw list can be active at the same time.");
  3842. ERR_FAIL_COND_V_MSG(compute_list != nullptr, INVALID_ID, "Only one draw/compute list can be active at the same time.");
  3843. // Lock while compute_list is active.
  3844. _THREAD_SAFE_LOCK_
  3845. compute_list = memnew(ComputeList);
  3846. compute_list->command_buffer = frames[frame].draw_command_buffer;
  3847. compute_list->state.allow_draw_overlap = p_allow_draw_overlap;
  3848. return ID_TYPE_COMPUTE_LIST;
  3849. }
  3850. void RenderingDevice::compute_list_bind_compute_pipeline(ComputeListID p_list, RID p_compute_pipeline) {
  3851. // Must be called within a compute list, the class mutex is locked during that time
  3852. ERR_FAIL_COND(p_list != ID_TYPE_COMPUTE_LIST);
  3853. ERR_FAIL_NULL(compute_list);
  3854. ComputeList *cl = compute_list;
  3855. const ComputePipeline *pipeline = compute_pipeline_owner.get_or_null(p_compute_pipeline);
  3856. ERR_FAIL_NULL(pipeline);
  3857. if (p_compute_pipeline == cl->state.pipeline) {
  3858. return; // Redundant state, return.
  3859. }
  3860. cl->state.pipeline = p_compute_pipeline;
  3861. driver->command_bind_compute_pipeline(cl->command_buffer, pipeline->driver_id);
  3862. if (cl->state.pipeline_shader != pipeline->shader) {
  3863. // Shader changed, so descriptor sets may become incompatible.
  3864. uint32_t pcount = pipeline->set_formats.size(); // Formats count in this pipeline.
  3865. cl->state.set_count = MAX(cl->state.set_count, pcount);
  3866. const uint32_t *pformats = pipeline->set_formats.ptr(); // Pipeline set formats.
  3867. uint32_t first_invalid_set = UINT32_MAX; // All valid by default.
  3868. switch (driver->api_trait_get(RDD::API_TRAIT_SHADER_CHANGE_INVALIDATION)) {
  3869. case RDD::SHADER_CHANGE_INVALIDATION_ALL_BOUND_UNIFORM_SETS: {
  3870. first_invalid_set = 0;
  3871. } break;
  3872. case RDD::SHADER_CHANGE_INVALIDATION_INCOMPATIBLE_SETS_PLUS_CASCADE: {
  3873. for (uint32_t i = 0; i < pcount; i++) {
  3874. if (cl->state.sets[i].pipeline_expected_format != pformats[i]) {
  3875. first_invalid_set = i;
  3876. break;
  3877. }
  3878. }
  3879. } break;
  3880. case RDD::SHADER_CHANGE_INVALIDATION_ALL_OR_NONE_ACCORDING_TO_LAYOUT_HASH: {
  3881. if (cl->state.pipeline_shader_layout_hash != pipeline->shader_layout_hash) {
  3882. first_invalid_set = 0;
  3883. }
  3884. } break;
  3885. }
  3886. for (uint32_t i = 0; i < pcount; i++) {
  3887. cl->state.sets[i].bound = cl->state.sets[i].bound && i < first_invalid_set;
  3888. cl->state.sets[i].pipeline_expected_format = pformats[i];
  3889. }
  3890. for (uint32_t i = pcount; i < cl->state.set_count; i++) {
  3891. // Unbind the ones above (not used) if exist.
  3892. cl->state.sets[i].bound = false;
  3893. }
  3894. cl->state.set_count = pcount; // Update set count.
  3895. if (pipeline->push_constant_size) {
  3896. #ifdef DEBUG_ENABLED
  3897. cl->validation.pipeline_push_constant_supplied = false;
  3898. #endif
  3899. }
  3900. cl->state.pipeline_shader = pipeline->shader;
  3901. cl->state.pipeline_shader_driver_id = pipeline->shader_driver_id;
  3902. cl->state.pipeline_shader_layout_hash = pipeline->shader_layout_hash;
  3903. cl->state.local_group_size[0] = pipeline->local_group_size[0];
  3904. cl->state.local_group_size[1] = pipeline->local_group_size[1];
  3905. cl->state.local_group_size[2] = pipeline->local_group_size[2];
  3906. }
  3907. #ifdef DEBUG_ENABLED
  3908. // Update compute pass pipeline info.
  3909. cl->validation.pipeline_active = true;
  3910. cl->validation.pipeline_push_constant_size = pipeline->push_constant_size;
  3911. #endif
  3912. }
  3913. void RenderingDevice::compute_list_bind_uniform_set(ComputeListID p_list, RID p_uniform_set, uint32_t p_index) {
  3914. // Must be called within a compute list, the class mutex is locked during that time
  3915. ERR_FAIL_COND(p_list != ID_TYPE_COMPUTE_LIST);
  3916. ERR_FAIL_NULL(compute_list);
  3917. ComputeList *cl = compute_list;
  3918. #ifdef DEBUG_ENABLED
  3919. ERR_FAIL_COND_MSG(p_index >= driver->limit_get(LIMIT_MAX_BOUND_UNIFORM_SETS) || p_index >= MAX_UNIFORM_SETS,
  3920. "Attempting to bind a descriptor set (" + itos(p_index) + ") greater than what the hardware supports (" + itos(driver->limit_get(LIMIT_MAX_BOUND_UNIFORM_SETS)) + ").");
  3921. #endif
  3922. #ifdef DEBUG_ENABLED
  3923. ERR_FAIL_COND_MSG(!cl->validation.active, "Submitted Compute Lists can no longer be modified.");
  3924. #endif
  3925. UniformSet *uniform_set = uniform_set_owner.get_or_null(p_uniform_set);
  3926. ERR_FAIL_NULL(uniform_set);
  3927. if (p_index > cl->state.set_count) {
  3928. cl->state.set_count = p_index;
  3929. }
  3930. cl->state.sets[p_index].uniform_set_driver_id = uniform_set->driver_id; // Update set pointer.
  3931. cl->state.sets[p_index].bound = false; // Needs rebind.
  3932. cl->state.sets[p_index].uniform_set_format = uniform_set->format;
  3933. cl->state.sets[p_index].uniform_set = p_uniform_set;
  3934. if (driver->api_trait_get(RDD::API_TRAIT_HONORS_PIPELINE_BARRIERS)) {
  3935. uint32_t textures_to_sampled_count = uniform_set->mutable_sampled_textures.size();
  3936. uint32_t textures_to_storage_count = uniform_set->mutable_storage_textures.size();
  3937. Texture **textures_to_sampled = uniform_set->mutable_sampled_textures.ptrw();
  3938. RDD::TextureBarrier *texture_barriers = nullptr;
  3939. if (textures_to_sampled_count + textures_to_storage_count) {
  3940. texture_barriers = (RDD::TextureBarrier *)alloca(sizeof(RDD::TextureBarrier) * (textures_to_sampled_count + textures_to_storage_count));
  3941. }
  3942. uint32_t texture_barrier_count = 0;
  3943. BitField<RDD::PipelineStageBits> src_stages;
  3944. for (uint32_t i = 0; i < textures_to_sampled_count; i++) {
  3945. if (textures_to_sampled[i]->layout != RDD::TEXTURE_LAYOUT_SHADER_READ_ONLY_OPTIMAL) {
  3946. src_stages.set_flag(RDD::PIPELINE_STAGE_COMPUTE_SHADER_BIT);
  3947. RDD::TextureBarrier &tb = texture_barriers[texture_barrier_count++];
  3948. tb.texture = textures_to_sampled[i]->driver_id;
  3949. tb.src_access = (RDD::BARRIER_ACCESS_SHADER_READ_BIT | RDD::BARRIER_ACCESS_SHADER_WRITE_BIT);
  3950. tb.dst_access = (RDD::BARRIER_ACCESS_SHADER_READ_BIT | RDD::BARRIER_ACCESS_SHADER_WRITE_BIT);
  3951. tb.prev_layout = textures_to_sampled[i]->layout;
  3952. tb.next_layout = RDD::TEXTURE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
  3953. tb.subresources.aspect = textures_to_sampled[i]->read_aspect_flags;
  3954. tb.subresources.base_mipmap = textures_to_sampled[i]->base_mipmap;
  3955. tb.subresources.mipmap_count = textures_to_sampled[i]->mipmaps;
  3956. tb.subresources.base_layer = textures_to_sampled[i]->base_layer;
  3957. tb.subresources.layer_count = textures_to_sampled[i]->layers;
  3958. textures_to_sampled[i]->layout = RDD::TEXTURE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
  3959. cl->state.textures_to_sampled_layout.erase(textures_to_sampled[i]);
  3960. }
  3961. if (textures_to_sampled[i]->used_in_frame != frames_drawn) {
  3962. textures_to_sampled[i]->used_in_frame = frames_drawn;
  3963. textures_to_sampled[i]->used_in_transfer = false;
  3964. textures_to_sampled[i]->used_in_raster = false;
  3965. }
  3966. textures_to_sampled[i]->used_in_compute = true;
  3967. }
  3968. Texture **textures_to_storage = uniform_set->mutable_storage_textures.ptrw();
  3969. for (uint32_t i = 0; i < textures_to_storage_count; i++) {
  3970. if (textures_to_storage[i]->layout != RDD::TEXTURE_LAYOUT_GENERAL) {
  3971. BitField<RDD::BarrierAccessBits> src_access;
  3972. if (textures_to_storage[i]->used_in_frame == frames_drawn) {
  3973. if (textures_to_storage[i]->used_in_compute) {
  3974. src_stages.set_flag(RDD::PIPELINE_STAGE_COMPUTE_SHADER_BIT);
  3975. src_access.set_flag(RDD::BARRIER_ACCESS_SHADER_READ_BIT).set_flag(RDD::BARRIER_ACCESS_SHADER_WRITE_BIT);
  3976. }
  3977. if (textures_to_storage[i]->used_in_raster) {
  3978. src_stages.set_flag(RDD::PIPELINE_STAGE_FRAGMENT_SHADER_BIT).set_flag(RDD::PIPELINE_STAGE_VERTEX_SHADER_BIT);
  3979. src_access.set_flag(RDD::BARRIER_ACCESS_SHADER_READ_BIT).set_flag(RDD::BARRIER_ACCESS_SHADER_WRITE_BIT);
  3980. }
  3981. if (textures_to_storage[i]->used_in_transfer) {
  3982. src_stages.set_flag(RDD::PIPELINE_STAGE_TRANSFER_BIT);
  3983. src_access.set_flag(RDD::BARRIER_ACCESS_TRANSFER_WRITE_BIT).set_flag(RDD::BARRIER_ACCESS_TRANSFER_READ_BIT);
  3984. }
  3985. textures_to_storage[i]->used_in_compute = false;
  3986. textures_to_storage[i]->used_in_raster = false;
  3987. textures_to_storage[i]->used_in_transfer = false;
  3988. } else {
  3989. src_access.clear();
  3990. textures_to_storage[i]->used_in_compute = false;
  3991. textures_to_storage[i]->used_in_raster = false;
  3992. textures_to_storage[i]->used_in_transfer = false;
  3993. textures_to_storage[i]->used_in_frame = frames_drawn;
  3994. }
  3995. RDD::TextureBarrier &tb = texture_barriers[texture_barrier_count++];
  3996. tb.texture = textures_to_storage[i]->driver_id;
  3997. tb.src_access = src_access;
  3998. tb.dst_access = (RDD::BARRIER_ACCESS_SHADER_READ_BIT | RDD::BARRIER_ACCESS_SHADER_WRITE_BIT);
  3999. tb.prev_layout = textures_to_storage[i]->layout;
  4000. tb.next_layout = RDD::TEXTURE_LAYOUT_GENERAL;
  4001. tb.subresources.aspect = textures_to_storage[i]->read_aspect_flags;
  4002. tb.subresources.base_mipmap = textures_to_storage[i]->base_mipmap;
  4003. tb.subresources.mipmap_count = textures_to_storage[i]->mipmaps;
  4004. tb.subresources.base_layer = textures_to_storage[i]->base_layer;
  4005. tb.subresources.layer_count = textures_to_storage[i]->layers;
  4006. textures_to_storage[i]->layout = RDD::TEXTURE_LAYOUT_GENERAL;
  4007. cl->state.textures_to_sampled_layout.insert(textures_to_storage[i]); // Needs to go back to sampled layout afterwards.
  4008. }
  4009. }
  4010. if (texture_barrier_count) {
  4011. if (src_stages.is_empty()) {
  4012. src_stages.set_flag(RDD::PIPELINE_STAGE_TOP_OF_PIPE_BIT);
  4013. }
  4014. driver->command_pipeline_barrier(cl->command_buffer, src_stages, RDD::PIPELINE_STAGE_COMPUTE_SHADER_BIT, {}, {}, VectorView(texture_barriers, texture_barrier_count));
  4015. }
  4016. }
  4017. #if 0
  4018. { // Validate that textures bound are not attached as framebuffer bindings.
  4019. uint32_t attachable_count = uniform_set->attachable_textures.size();
  4020. const RID *attachable_ptr = uniform_set->attachable_textures.ptr();
  4021. uint32_t bound_count = draw_list_bound_textures.size();
  4022. const RID *bound_ptr = draw_list_bound_textures.ptr();
  4023. for (uint32_t i = 0; i < attachable_count; i++) {
  4024. for (uint32_t j = 0; j < bound_count; j++) {
  4025. ERR_FAIL_COND_MSG(attachable_ptr[i] == bound_ptr[j],
  4026. "Attempted to use the same texture in framebuffer attachment and a uniform set, this is not allowed.");
  4027. }
  4028. }
  4029. }
  4030. #endif
  4031. }
  4032. void RenderingDevice::compute_list_set_push_constant(ComputeListID p_list, const void *p_data, uint32_t p_data_size) {
  4033. ERR_FAIL_COND(p_list != ID_TYPE_COMPUTE_LIST);
  4034. ERR_FAIL_NULL(compute_list);
  4035. ComputeList *cl = compute_list;
  4036. #ifdef DEBUG_ENABLED
  4037. ERR_FAIL_COND_MSG(!cl->validation.active, "Submitted Compute Lists can no longer be modified.");
  4038. #endif
  4039. #ifdef DEBUG_ENABLED
  4040. ERR_FAIL_COND_MSG(p_data_size != cl->validation.pipeline_push_constant_size,
  4041. "This compute pipeline requires (" + itos(cl->validation.pipeline_push_constant_size) + ") bytes of push constant data, supplied: (" + itos(p_data_size) + ")");
  4042. #endif
  4043. driver->command_bind_push_constants(cl->command_buffer, cl->state.pipeline_shader_driver_id, 0, VectorView((const uint32_t *)p_data, p_data_size / sizeof(uint32_t)));
  4044. #ifdef DEBUG_ENABLED
  4045. cl->validation.pipeline_push_constant_supplied = true;
  4046. #endif
  4047. }
  4048. void RenderingDevice::compute_list_dispatch(ComputeListID p_list, uint32_t p_x_groups, uint32_t p_y_groups, uint32_t p_z_groups) {
  4049. // Must be called within a compute list, the class mutex is locked during that time
  4050. ERR_FAIL_COND(p_list != ID_TYPE_COMPUTE_LIST);
  4051. ERR_FAIL_NULL(compute_list);
  4052. ComputeList *cl = compute_list;
  4053. #ifdef DEBUG_ENABLED
  4054. ERR_FAIL_COND_MSG(p_x_groups == 0, "Dispatch amount of X compute groups (" + itos(p_x_groups) + ") is zero.");
  4055. ERR_FAIL_COND_MSG(p_z_groups == 0, "Dispatch amount of Z compute groups (" + itos(p_z_groups) + ") is zero.");
  4056. ERR_FAIL_COND_MSG(p_y_groups == 0, "Dispatch amount of Y compute groups (" + itos(p_y_groups) + ") is zero.");
  4057. ERR_FAIL_COND_MSG(p_x_groups > driver->limit_get(LIMIT_MAX_COMPUTE_WORKGROUP_COUNT_X),
  4058. "Dispatch amount of X compute groups (" + itos(p_x_groups) + ") is larger than device limit (" + itos(driver->limit_get(LIMIT_MAX_COMPUTE_WORKGROUP_COUNT_X)) + ")");
  4059. ERR_FAIL_COND_MSG(p_y_groups > driver->limit_get(LIMIT_MAX_COMPUTE_WORKGROUP_COUNT_Y),
  4060. "Dispatch amount of Y compute groups (" + itos(p_y_groups) + ") is larger than device limit (" + itos(driver->limit_get(LIMIT_MAX_COMPUTE_WORKGROUP_COUNT_Y)) + ")");
  4061. ERR_FAIL_COND_MSG(p_z_groups > driver->limit_get(LIMIT_MAX_COMPUTE_WORKGROUP_COUNT_Z),
  4062. "Dispatch amount of Z compute groups (" + itos(p_z_groups) + ") is larger than device limit (" + itos(driver->limit_get(LIMIT_MAX_COMPUTE_WORKGROUP_COUNT_Z)) + ")");
  4063. ERR_FAIL_COND_MSG(!cl->validation.active, "Submitted Compute Lists can no longer be modified.");
  4064. #endif
  4065. #ifdef DEBUG_ENABLED
  4066. ERR_FAIL_COND_MSG(!cl->validation.pipeline_active, "No compute pipeline was set before attempting to draw.");
  4067. if (cl->validation.pipeline_push_constant_size > 0) {
  4068. // Using push constants, check that they were supplied.
  4069. ERR_FAIL_COND_MSG(!cl->validation.pipeline_push_constant_supplied,
  4070. "The shader in this pipeline requires a push constant to be set before drawing, but it's not present.");
  4071. }
  4072. #endif
  4073. // Bind descriptor sets.
  4074. for (uint32_t i = 0; i < cl->state.set_count; i++) {
  4075. if (cl->state.sets[i].pipeline_expected_format == 0) {
  4076. continue; // Nothing expected by this pipeline.
  4077. }
  4078. #ifdef DEBUG_ENABLED
  4079. if (cl->state.sets[i].pipeline_expected_format != cl->state.sets[i].uniform_set_format) {
  4080. if (cl->state.sets[i].uniform_set_format == 0) {
  4081. ERR_FAIL_MSG("Uniforms were never supplied for set (" + itos(i) + ") at the time of drawing, which are required by the pipeline");
  4082. } else if (uniform_set_owner.owns(cl->state.sets[i].uniform_set)) {
  4083. UniformSet *us = uniform_set_owner.get_or_null(cl->state.sets[i].uniform_set);
  4084. ERR_FAIL_MSG("Uniforms supplied for set (" + itos(i) + "):\n" + _shader_uniform_debug(us->shader_id, us->shader_set) + "\nare not the same format as required by the pipeline shader. Pipeline shader requires the following bindings:\n" + _shader_uniform_debug(cl->state.pipeline_shader));
  4085. } else {
  4086. ERR_FAIL_MSG("Uniforms supplied for set (" + itos(i) + ", which was was just freed) are not the same format as required by the pipeline shader. Pipeline shader requires the following bindings:\n" + _shader_uniform_debug(cl->state.pipeline_shader));
  4087. }
  4088. }
  4089. #endif
  4090. driver->command_uniform_set_prepare_for_use(cl->command_buffer, cl->state.sets[i].uniform_set_driver_id, cl->state.pipeline_shader_driver_id, i);
  4091. }
  4092. for (uint32_t i = 0; i < cl->state.set_count; i++) {
  4093. if (cl->state.sets[i].pipeline_expected_format == 0) {
  4094. continue; // Nothing expected by this pipeline.
  4095. }
  4096. if (!cl->state.sets[i].bound) {
  4097. driver->command_bind_compute_uniform_set(cl->command_buffer, cl->state.sets[i].uniform_set_driver_id, cl->state.pipeline_shader_driver_id, i);
  4098. cl->state.sets[i].bound = true;
  4099. }
  4100. }
  4101. driver->command_compute_dispatch(cl->command_buffer, p_x_groups, p_y_groups, p_z_groups);
  4102. }
  4103. void RenderingDevice::compute_list_dispatch_threads(ComputeListID p_list, uint32_t p_x_threads, uint32_t p_y_threads, uint32_t p_z_threads) {
  4104. ERR_FAIL_COND(p_list != ID_TYPE_COMPUTE_LIST);
  4105. ERR_FAIL_NULL(compute_list);
  4106. #ifdef DEBUG_ENABLED
  4107. ERR_FAIL_COND_MSG(p_x_threads == 0, "Dispatch amount of X compute threads (" + itos(p_x_threads) + ") is zero.");
  4108. ERR_FAIL_COND_MSG(p_y_threads == 0, "Dispatch amount of Y compute threads (" + itos(p_y_threads) + ") is zero.");
  4109. ERR_FAIL_COND_MSG(p_z_threads == 0, "Dispatch amount of Z compute threads (" + itos(p_z_threads) + ") is zero.");
  4110. #endif
  4111. ComputeList *cl = compute_list;
  4112. #ifdef DEBUG_ENABLED
  4113. ERR_FAIL_COND_MSG(!cl->validation.pipeline_active, "No compute pipeline was set before attempting to draw.");
  4114. if (cl->validation.pipeline_push_constant_size > 0) {
  4115. // Using push constants, check that they were supplied.
  4116. ERR_FAIL_COND_MSG(!cl->validation.pipeline_push_constant_supplied,
  4117. "The shader in this pipeline requires a push constant to be set before drawing, but it's not present.");
  4118. }
  4119. #endif
  4120. compute_list_dispatch(p_list, Math::division_round_up(p_x_threads, cl->state.local_group_size[0]), Math::division_round_up(p_y_threads, cl->state.local_group_size[1]), Math::division_round_up(p_z_threads, cl->state.local_group_size[2]));
  4121. }
  4122. void RenderingDevice::compute_list_dispatch_indirect(ComputeListID p_list, RID p_buffer, uint32_t p_offset) {
  4123. ERR_FAIL_COND(p_list != ID_TYPE_COMPUTE_LIST);
  4124. ERR_FAIL_NULL(compute_list);
  4125. ComputeList *cl = compute_list;
  4126. Buffer *buffer = storage_buffer_owner.get_or_null(p_buffer);
  4127. ERR_FAIL_COND(!buffer);
  4128. ERR_FAIL_COND_MSG(!buffer->usage.has_flag(RDD::BUFFER_USAGE_INDIRECT_BIT), "Buffer provided was not created to do indirect dispatch.");
  4129. ERR_FAIL_COND_MSG(p_offset + 12 > buffer->size, "Offset provided (+12) is past the end of buffer.");
  4130. #ifdef DEBUG_ENABLED
  4131. ERR_FAIL_COND_MSG(!cl->validation.active, "Submitted Compute Lists can no longer be modified.");
  4132. #endif
  4133. #ifdef DEBUG_ENABLED
  4134. ERR_FAIL_COND_MSG(!cl->validation.pipeline_active, "No compute pipeline was set before attempting to draw.");
  4135. if (cl->validation.pipeline_push_constant_size > 0) {
  4136. // Using push constants, check that they were supplied.
  4137. ERR_FAIL_COND_MSG(!cl->validation.pipeline_push_constant_supplied,
  4138. "The shader in this pipeline requires a push constant to be set before drawing, but it's not present.");
  4139. }
  4140. #endif
  4141. // Bind descriptor sets.
  4142. for (uint32_t i = 0; i < cl->state.set_count; i++) {
  4143. if (cl->state.sets[i].pipeline_expected_format == 0) {
  4144. continue; // Nothing expected by this pipeline.
  4145. }
  4146. #ifdef DEBUG_ENABLED
  4147. if (cl->state.sets[i].pipeline_expected_format != cl->state.sets[i].uniform_set_format) {
  4148. if (cl->state.sets[i].uniform_set_format == 0) {
  4149. ERR_FAIL_MSG("Uniforms were never supplied for set (" + itos(i) + ") at the time of drawing, which are required by the pipeline");
  4150. } else if (uniform_set_owner.owns(cl->state.sets[i].uniform_set)) {
  4151. UniformSet *us = uniform_set_owner.get_or_null(cl->state.sets[i].uniform_set);
  4152. ERR_FAIL_MSG("Uniforms supplied for set (" + itos(i) + "):\n" + _shader_uniform_debug(us->shader_id, us->shader_set) + "\nare not the same format as required by the pipeline shader. Pipeline shader requires the following bindings:\n" + _shader_uniform_debug(cl->state.pipeline_shader));
  4153. } else {
  4154. ERR_FAIL_MSG("Uniforms supplied for set (" + itos(i) + ", which was was just freed) are not the same format as required by the pipeline shader. Pipeline shader requires the following bindings:\n" + _shader_uniform_debug(cl->state.pipeline_shader));
  4155. }
  4156. }
  4157. #endif
  4158. driver->command_uniform_set_prepare_for_use(cl->command_buffer, cl->state.sets[i].uniform_set_driver_id, cl->state.pipeline_shader_driver_id, i);
  4159. }
  4160. for (uint32_t i = 0; i < cl->state.set_count; i++) {
  4161. if (cl->state.sets[i].pipeline_expected_format == 0) {
  4162. continue; // Nothing expected by this pipeline.
  4163. }
  4164. if (!cl->state.sets[i].bound) {
  4165. driver->command_bind_compute_uniform_set(cl->command_buffer, cl->state.sets[i].uniform_set_driver_id, cl->state.pipeline_shader_driver_id, i);
  4166. cl->state.sets[i].bound = true;
  4167. }
  4168. }
  4169. driver->command_compute_dispatch_indirect(cl->command_buffer, buffer->driver_id, p_offset);
  4170. }
  4171. void RenderingDevice::compute_list_add_barrier(ComputeListID p_list) {
  4172. // Must be called within a compute list, the class mutex is locked during that time
  4173. BitField<RDD::PipelineStageBits> stages(RDD::PIPELINE_STAGE_COMPUTE_SHADER_BIT);
  4174. BitField<RDD::BarrierAccessBits> access(RDD::BARRIER_ACCESS_SHADER_READ_BIT);
  4175. _compute_list_add_barrier(BARRIER_MASK_COMPUTE, stages, access);
  4176. }
  4177. void RenderingDevice::_compute_list_add_barrier(BitField<BarrierMask> p_post_barrier, BitField<RDD::PipelineStageBits> p_stages, BitField<RDD::BarrierAccessBits> p_access) {
  4178. ERR_FAIL_NULL(compute_list);
  4179. if (driver->api_trait_get(RDD::API_TRAIT_HONORS_PIPELINE_BARRIERS)) {
  4180. RDD::TextureBarrier *texture_barriers = nullptr;
  4181. uint32_t texture_barrier_count = compute_list->state.textures_to_sampled_layout.size();
  4182. if (texture_barrier_count) {
  4183. texture_barriers = (RDD::TextureBarrier *)alloca(sizeof(RDD::TextureBarrier) * texture_barrier_count);
  4184. }
  4185. texture_barrier_count = 0; // We'll count how many we end up issuing.
  4186. for (Texture *E : compute_list->state.textures_to_sampled_layout) {
  4187. if (E->layout != RDD::TEXTURE_LAYOUT_SHADER_READ_ONLY_OPTIMAL) {
  4188. RDD::TextureBarrier &tb = texture_barriers[texture_barrier_count++];
  4189. tb.texture = E->driver_id;
  4190. tb.src_access = RDD::BARRIER_ACCESS_SHADER_READ_BIT | RDD::BARRIER_ACCESS_SHADER_WRITE_BIT;
  4191. tb.dst_access = p_access;
  4192. tb.prev_layout = E->layout;
  4193. tb.next_layout = RDD::TEXTURE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
  4194. tb.subresources.aspect = E->read_aspect_flags;
  4195. tb.subresources.base_mipmap = E->base_mipmap;
  4196. tb.subresources.mipmap_count = E->mipmaps;
  4197. tb.subresources.base_layer = E->base_layer;
  4198. tb.subresources.layer_count = E->layers;
  4199. E->layout = RDD::TEXTURE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
  4200. }
  4201. if (E->used_in_frame != frames_drawn) {
  4202. E->used_in_transfer = false;
  4203. E->used_in_raster = false;
  4204. E->used_in_compute = false;
  4205. E->used_in_frame = frames_drawn;
  4206. }
  4207. }
  4208. if (p_stages) {
  4209. RDD::MemoryBarrier mb;
  4210. mb.src_access = RDD::BARRIER_ACCESS_SHADER_WRITE_BIT;
  4211. mb.dst_access = p_access;
  4212. driver->command_pipeline_barrier(compute_list->command_buffer, RDD::PIPELINE_STAGE_COMPUTE_SHADER_BIT, p_stages, mb, {}, VectorView(texture_barriers, texture_barrier_count));
  4213. } else if (texture_barrier_count) {
  4214. driver->command_pipeline_barrier(compute_list->command_buffer, RDD::PIPELINE_STAGE_COMPUTE_SHADER_BIT, RDD::PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT, {}, {}, VectorView(texture_barriers, texture_barrier_count));
  4215. }
  4216. }
  4217. #ifdef FORCE_FULL_BARRIER
  4218. _full_barrier(true);
  4219. #endif
  4220. }
  4221. void RenderingDevice::compute_list_end(BitField<BarrierMask> p_post_barrier) {
  4222. ERR_FAIL_NULL(compute_list);
  4223. BitField<RDD::PipelineStageBits> stages;
  4224. BitField<RDD::BarrierAccessBits> access;
  4225. if (p_post_barrier.has_flag(BARRIER_MASK_COMPUTE)) {
  4226. stages.set_flag(RDD::PIPELINE_STAGE_COMPUTE_SHADER_BIT);
  4227. access.set_flag(RDD::BARRIER_ACCESS_SHADER_READ_BIT).set_flag(RDD::BARRIER_ACCESS_SHADER_WRITE_BIT);
  4228. }
  4229. if (p_post_barrier.has_flag(BARRIER_MASK_VERTEX)) {
  4230. stages.set_flag(RDD::PIPELINE_STAGE_VERTEX_INPUT_BIT).set_flag(RDD::PIPELINE_STAGE_VERTEX_SHADER_BIT).set_flag(RDD::PIPELINE_STAGE_DRAW_INDIRECT_BIT);
  4231. access.set_flag(RDD::BARRIER_ACCESS_SHADER_READ_BIT).set_flag(RDD::BARRIER_ACCESS_SHADER_WRITE_BIT).set_flag(RDD::BARRIER_ACCESS_INDEX_READ_BIT).set_flag(RDD::BARRIER_ACCESS_VERTEX_ATTRIBUTE_READ_BIT).set_flag(RDD::BARRIER_ACCESS_INDIRECT_COMMAND_READ_BIT);
  4232. }
  4233. if (p_post_barrier.has_flag(BARRIER_MASK_FRAGMENT)) {
  4234. stages.set_flag(RDD::PIPELINE_STAGE_FRAGMENT_SHADER_BIT).set_flag(RDD::PIPELINE_STAGE_DRAW_INDIRECT_BIT);
  4235. access.set_flag(RDD::BARRIER_ACCESS_SHADER_READ_BIT).set_flag(RDD::BARRIER_ACCESS_SHADER_WRITE_BIT).set_flag(RDD::BARRIER_ACCESS_INDIRECT_COMMAND_READ_BIT);
  4236. }
  4237. if (p_post_barrier.has_flag(BARRIER_MASK_TRANSFER)) {
  4238. stages.set_flag(RDD::PIPELINE_STAGE_TRANSFER_BIT);
  4239. access.set_flag(RDD::BARRIER_ACCESS_TRANSFER_WRITE_BIT).set_flag(RDD::BARRIER_ACCESS_TRANSFER_READ_BIT);
  4240. }
  4241. _compute_list_add_barrier(p_post_barrier, stages, access);
  4242. memdelete(compute_list);
  4243. compute_list = nullptr;
  4244. // Compute_list is no longer active.
  4245. _THREAD_SAFE_UNLOCK_
  4246. }
  4247. void RenderingDevice::barrier(BitField<BarrierMask> p_from, BitField<BarrierMask> p_to) {
  4248. if (!driver->api_trait_get(RDD::API_TRAIT_HONORS_PIPELINE_BARRIERS)) {
  4249. return;
  4250. }
  4251. BitField<RDD::PipelineStageBits> src_stages;
  4252. BitField<RDD::BarrierAccessBits> src_access;
  4253. if (p_from == 0) {
  4254. src_stages.set_flag(RDD::PIPELINE_STAGE_TOP_OF_PIPE_BIT);
  4255. } else {
  4256. if (p_from.has_flag(BARRIER_MASK_COMPUTE)) {
  4257. src_stages.set_flag(RDD::PIPELINE_STAGE_COMPUTE_SHADER_BIT);
  4258. src_access.set_flag(RDD::BARRIER_ACCESS_SHADER_WRITE_BIT);
  4259. }
  4260. if (p_from.has_flag(BARRIER_MASK_FRAGMENT)) {
  4261. src_stages.set_flag(RDD::PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT).set_flag(RDD::PIPELINE_STAGE_EARLY_FRAGMENT_TESTS_BIT).set_flag(RDD::PIPELINE_STAGE_LATE_FRAGMENT_TESTS_BIT);
  4262. src_access.set_flag(RDD::BARRIER_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT).set_flag(RDD::BARRIER_ACCESS_COLOR_ATTACHMENT_WRITE_BIT);
  4263. }
  4264. if (p_from.has_flag(BARRIER_MASK_TRANSFER)) {
  4265. src_stages.set_flag(RDD::PIPELINE_STAGE_TRANSFER_BIT);
  4266. src_access.set_flag(RDD::BARRIER_ACCESS_TRANSFER_WRITE_BIT);
  4267. }
  4268. }
  4269. BitField<RDD::PipelineStageBits> dst_stages;
  4270. BitField<RDD::BarrierAccessBits> dst_access;
  4271. if (p_to == 0) {
  4272. dst_stages.set_flag(RDD::PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT);
  4273. } else {
  4274. if (p_to.has_flag(BARRIER_MASK_COMPUTE)) {
  4275. dst_stages.set_flag(RDD::PIPELINE_STAGE_COMPUTE_SHADER_BIT);
  4276. dst_access.set_flag(RDD::BARRIER_ACCESS_SHADER_READ_BIT).set_flag(RDD::BARRIER_ACCESS_SHADER_WRITE_BIT);
  4277. }
  4278. if (p_to.has_flag(BARRIER_MASK_VERTEX)) {
  4279. dst_stages.set_flag(RDD::PIPELINE_STAGE_VERTEX_INPUT_BIT).set_flag(RDD::PIPELINE_STAGE_VERTEX_SHADER_BIT).set_flag(RDD::PIPELINE_STAGE_DRAW_INDIRECT_BIT);
  4280. dst_access.set_flag(RDD::BARRIER_ACCESS_SHADER_READ_BIT).set_flag(RDD::BARRIER_ACCESS_SHADER_WRITE_BIT).set_flag(RDD::BARRIER_ACCESS_INDEX_READ_BIT).set_flag(RDD::BARRIER_ACCESS_VERTEX_ATTRIBUTE_READ_BIT).set_flag(RDD::BARRIER_ACCESS_INDIRECT_COMMAND_READ_BIT);
  4281. }
  4282. if (p_to.has_flag(BARRIER_MASK_FRAGMENT)) {
  4283. dst_stages.set_flag(RDD::PIPELINE_STAGE_FRAGMENT_SHADER_BIT).set_flag(RDD::PIPELINE_STAGE_DRAW_INDIRECT_BIT);
  4284. dst_access.set_flag(RDD::BARRIER_ACCESS_SHADER_READ_BIT).set_flag(RDD::BARRIER_ACCESS_SHADER_WRITE_BIT).set_flag(RDD::BARRIER_ACCESS_INDIRECT_COMMAND_READ_BIT);
  4285. }
  4286. if (p_to.has_flag(BARRIER_MASK_TRANSFER)) {
  4287. dst_stages.set_flag(RDD::PIPELINE_STAGE_TRANSFER_BIT);
  4288. dst_access.set_flag(RDD::BARRIER_ACCESS_TRANSFER_WRITE_BIT).set_flag(RDD::BARRIER_ACCESS_TRANSFER_READ_BIT);
  4289. }
  4290. }
  4291. RDD::MemoryBarrier mb;
  4292. mb.src_access = src_access;
  4293. mb.dst_access = dst_access;
  4294. driver->command_pipeline_barrier(frames[frame].draw_command_buffer, src_stages, dst_stages, mb, {}, {});
  4295. }
  4296. void RenderingDevice::full_barrier() {
  4297. #ifndef DEBUG_ENABLED
  4298. ERR_PRINT("Full barrier is debug-only, should not be used in production");
  4299. #endif
  4300. _full_barrier(true);
  4301. }
  4302. /**************************/
  4303. /**** FRAME MANAGEMENT ****/
  4304. /**************************/
  4305. void RenderingDevice::free(RID p_id) {
  4306. _THREAD_SAFE_METHOD_
  4307. _free_dependencies(p_id); // Recursively erase dependencies first, to avoid potential API problems.
  4308. _free_internal(p_id);
  4309. }
  4310. void RenderingDevice::_free_internal(RID p_id) {
  4311. #ifdef DEV_ENABLED
  4312. String resource_name;
  4313. if (resource_names.has(p_id)) {
  4314. resource_name = resource_names[p_id];
  4315. resource_names.erase(p_id);
  4316. }
  4317. #endif
  4318. // Push everything so it's disposed of next time this frame index is processed (means, it's safe to do it).
  4319. if (texture_owner.owns(p_id)) {
  4320. Texture *texture = texture_owner.get_or_null(p_id);
  4321. frames[frame].textures_to_dispose_of.push_back(*texture);
  4322. texture_owner.free(p_id);
  4323. } else if (framebuffer_owner.owns(p_id)) {
  4324. Framebuffer *framebuffer = framebuffer_owner.get_or_null(p_id);
  4325. frames[frame].framebuffers_to_dispose_of.push_back(*framebuffer);
  4326. if (framebuffer->invalidated_callback != nullptr) {
  4327. framebuffer->invalidated_callback(framebuffer->invalidated_callback_userdata);
  4328. }
  4329. framebuffer_owner.free(p_id);
  4330. } else if (sampler_owner.owns(p_id)) {
  4331. RDD::SamplerID sampler_driver_id = *sampler_owner.get_or_null(p_id);
  4332. frames[frame].samplers_to_dispose_of.push_back(sampler_driver_id);
  4333. sampler_owner.free(p_id);
  4334. } else if (vertex_buffer_owner.owns(p_id)) {
  4335. Buffer *vertex_buffer = vertex_buffer_owner.get_or_null(p_id);
  4336. frames[frame].buffers_to_dispose_of.push_back(*vertex_buffer);
  4337. vertex_buffer_owner.free(p_id);
  4338. } else if (vertex_array_owner.owns(p_id)) {
  4339. vertex_array_owner.free(p_id);
  4340. } else if (index_buffer_owner.owns(p_id)) {
  4341. IndexBuffer *index_buffer = index_buffer_owner.get_or_null(p_id);
  4342. frames[frame].buffers_to_dispose_of.push_back(*index_buffer);
  4343. index_buffer_owner.free(p_id);
  4344. } else if (index_array_owner.owns(p_id)) {
  4345. index_array_owner.free(p_id);
  4346. } else if (shader_owner.owns(p_id)) {
  4347. Shader *shader = shader_owner.get_or_null(p_id);
  4348. if (shader->driver_id) { // Not placeholder?
  4349. frames[frame].shaders_to_dispose_of.push_back(*shader);
  4350. }
  4351. shader_owner.free(p_id);
  4352. } else if (uniform_buffer_owner.owns(p_id)) {
  4353. Buffer *uniform_buffer = uniform_buffer_owner.get_or_null(p_id);
  4354. frames[frame].buffers_to_dispose_of.push_back(*uniform_buffer);
  4355. uniform_buffer_owner.free(p_id);
  4356. } else if (texture_buffer_owner.owns(p_id)) {
  4357. Buffer *texture_buffer = texture_buffer_owner.get_or_null(p_id);
  4358. frames[frame].buffers_to_dispose_of.push_back(*texture_buffer);
  4359. texture_buffer_owner.free(p_id);
  4360. } else if (storage_buffer_owner.owns(p_id)) {
  4361. Buffer *storage_buffer = storage_buffer_owner.get_or_null(p_id);
  4362. frames[frame].buffers_to_dispose_of.push_back(*storage_buffer);
  4363. storage_buffer_owner.free(p_id);
  4364. } else if (uniform_set_owner.owns(p_id)) {
  4365. UniformSet *uniform_set = uniform_set_owner.get_or_null(p_id);
  4366. frames[frame].uniform_sets_to_dispose_of.push_back(*uniform_set);
  4367. uniform_set_owner.free(p_id);
  4368. if (uniform_set->invalidated_callback != nullptr) {
  4369. uniform_set->invalidated_callback(uniform_set->invalidated_callback_userdata);
  4370. }
  4371. } else if (render_pipeline_owner.owns(p_id)) {
  4372. RenderPipeline *pipeline = render_pipeline_owner.get_or_null(p_id);
  4373. frames[frame].render_pipelines_to_dispose_of.push_back(*pipeline);
  4374. render_pipeline_owner.free(p_id);
  4375. } else if (compute_pipeline_owner.owns(p_id)) {
  4376. ComputePipeline *pipeline = compute_pipeline_owner.get_or_null(p_id);
  4377. frames[frame].compute_pipelines_to_dispose_of.push_back(*pipeline);
  4378. compute_pipeline_owner.free(p_id);
  4379. } else {
  4380. #ifdef DEV_ENABLED
  4381. ERR_PRINT("Attempted to free invalid ID: " + itos(p_id.get_id()) + " " + resource_name);
  4382. #else
  4383. ERR_PRINT("Attempted to free invalid ID: " + itos(p_id.get_id()));
  4384. #endif
  4385. }
  4386. }
  4387. // The full list of resources that can be named is in the VkObjectType enum.
  4388. // We just expose the resources that are owned and can be accessed easily.
  4389. void RenderingDevice::set_resource_name(RID p_id, const String &p_name) {
  4390. if (texture_owner.owns(p_id)) {
  4391. Texture *texture = texture_owner.get_or_null(p_id);
  4392. driver->set_object_name(RDD::OBJECT_TYPE_TEXTURE, texture->driver_id, p_name);
  4393. } else if (framebuffer_owner.owns(p_id)) {
  4394. //Framebuffer *framebuffer = framebuffer_owner.get_or_null(p_id);
  4395. // Not implemented for now as the relationship between Framebuffer and RenderPass is very complex.
  4396. } else if (sampler_owner.owns(p_id)) {
  4397. RDD::SamplerID sampler_driver_id = *sampler_owner.get_or_null(p_id);
  4398. driver->set_object_name(RDD::OBJECT_TYPE_SAMPLER, sampler_driver_id, p_name);
  4399. } else if (vertex_buffer_owner.owns(p_id)) {
  4400. Buffer *vertex_buffer = vertex_buffer_owner.get_or_null(p_id);
  4401. driver->set_object_name(RDD::OBJECT_TYPE_BUFFER, vertex_buffer->driver_id, p_name);
  4402. } else if (index_buffer_owner.owns(p_id)) {
  4403. IndexBuffer *index_buffer = index_buffer_owner.get_or_null(p_id);
  4404. driver->set_object_name(RDD::OBJECT_TYPE_BUFFER, index_buffer->driver_id, p_name);
  4405. } else if (shader_owner.owns(p_id)) {
  4406. Shader *shader = shader_owner.get_or_null(p_id);
  4407. driver->set_object_name(RDD::OBJECT_TYPE_SHADER, shader->driver_id, p_name);
  4408. } else if (uniform_buffer_owner.owns(p_id)) {
  4409. Buffer *uniform_buffer = uniform_buffer_owner.get_or_null(p_id);
  4410. driver->set_object_name(RDD::OBJECT_TYPE_BUFFER, uniform_buffer->driver_id, p_name);
  4411. } else if (texture_buffer_owner.owns(p_id)) {
  4412. Buffer *texture_buffer = texture_buffer_owner.get_or_null(p_id);
  4413. driver->set_object_name(RDD::OBJECT_TYPE_BUFFER, texture_buffer->driver_id, p_name);
  4414. } else if (storage_buffer_owner.owns(p_id)) {
  4415. Buffer *storage_buffer = storage_buffer_owner.get_or_null(p_id);
  4416. driver->set_object_name(RDD::OBJECT_TYPE_BUFFER, storage_buffer->driver_id, p_name);
  4417. } else if (uniform_set_owner.owns(p_id)) {
  4418. UniformSet *uniform_set = uniform_set_owner.get_or_null(p_id);
  4419. driver->set_object_name(RDD::OBJECT_TYPE_UNIFORM_SET, uniform_set->driver_id, p_name);
  4420. } else if (render_pipeline_owner.owns(p_id)) {
  4421. RenderPipeline *pipeline = render_pipeline_owner.get_or_null(p_id);
  4422. driver->set_object_name(RDD::OBJECT_TYPE_PIPELINE, pipeline->driver_id, p_name);
  4423. } else if (compute_pipeline_owner.owns(p_id)) {
  4424. ComputePipeline *pipeline = compute_pipeline_owner.get_or_null(p_id);
  4425. driver->set_object_name(RDD::OBJECT_TYPE_PIPELINE, pipeline->driver_id, p_name);
  4426. } else {
  4427. ERR_PRINT("Attempted to name invalid ID: " + itos(p_id.get_id()));
  4428. return;
  4429. }
  4430. #ifdef DEV_ENABLED
  4431. resource_names[p_id] = p_name;
  4432. #endif
  4433. }
  4434. void RenderingDevice::draw_command_begin_label(String p_label_name, const Color &p_color) {
  4435. _THREAD_SAFE_METHOD_
  4436. context->command_begin_label(frames[frame].draw_command_buffer, p_label_name, p_color);
  4437. }
  4438. void RenderingDevice::draw_command_insert_label(String p_label_name, const Color &p_color) {
  4439. _THREAD_SAFE_METHOD_
  4440. context->command_insert_label(frames[frame].draw_command_buffer, p_label_name, p_color);
  4441. }
  4442. void RenderingDevice::draw_command_end_label() {
  4443. _THREAD_SAFE_METHOD_
  4444. context->command_end_label(frames[frame].draw_command_buffer);
  4445. }
  4446. String RenderingDevice::get_device_vendor_name() const {
  4447. return context->get_device_vendor_name();
  4448. }
  4449. String RenderingDevice::get_device_name() const {
  4450. return context->get_device_name();
  4451. }
  4452. RenderingDevice::DeviceType RenderingDevice::get_device_type() const {
  4453. return context->get_device_type();
  4454. }
  4455. String RenderingDevice::get_device_api_version() const {
  4456. return context->get_device_api_version();
  4457. }
  4458. String RenderingDevice::get_device_pipeline_cache_uuid() const {
  4459. return context->get_device_pipeline_cache_uuid();
  4460. }
  4461. void RenderingDevice::_finalize_command_bufers() {
  4462. if (draw_list) {
  4463. ERR_PRINT("Found open draw list at the end of the frame, this should never happen (further drawing will likely not work).");
  4464. }
  4465. if (compute_list) {
  4466. ERR_PRINT("Found open compute list at the end of the frame, this should never happen (further compute will likely not work).");
  4467. }
  4468. {
  4469. driver->end_segment();
  4470. driver->command_buffer_end(frames[frame].setup_command_buffer);
  4471. driver->command_buffer_end(frames[frame].draw_command_buffer);
  4472. }
  4473. }
  4474. void RenderingDevice::_begin_frame() {
  4475. // Erase pending resources.
  4476. _free_pending_resources(frame);
  4477. // Create setup command buffer and set as the setup buffer.
  4478. {
  4479. bool ok = driver->command_buffer_begin(frames[frame].setup_command_buffer);
  4480. ERR_FAIL_COND(!ok);
  4481. ok = driver->command_buffer_begin(frames[frame].draw_command_buffer);
  4482. ERR_FAIL_COND(!ok);
  4483. if (local_device.is_null()) {
  4484. context->append_command_buffer(frames[frame].draw_command_buffer);
  4485. context->set_setup_buffer(frames[frame].setup_command_buffer); // Append now so it's added before everything else.
  4486. }
  4487. driver->begin_segment(frames[frame].draw_command_buffer, frame, frames_drawn);
  4488. }
  4489. // Advance current frame.
  4490. frames_drawn++;
  4491. // Advance staging buffer if used.
  4492. if (staging_buffer_used) {
  4493. staging_buffer_current = (staging_buffer_current + 1) % staging_buffer_blocks.size();
  4494. staging_buffer_used = false;
  4495. }
  4496. if (frames[frame].timestamp_count) {
  4497. driver->timestamp_query_pool_get_results(frames[frame].timestamp_pool, frames[frame].timestamp_count, frames[frame].timestamp_result_values.ptr());
  4498. driver->command_timestamp_query_pool_reset(frames[frame].setup_command_buffer, frames[frame].timestamp_pool, frames[frame].timestamp_count);
  4499. SWAP(frames[frame].timestamp_names, frames[frame].timestamp_result_names);
  4500. SWAP(frames[frame].timestamp_cpu_values, frames[frame].timestamp_cpu_result_values);
  4501. }
  4502. frames[frame].timestamp_result_count = frames[frame].timestamp_count;
  4503. frames[frame].timestamp_count = 0;
  4504. frames[frame].index = Engine::get_singleton()->get_frames_drawn();
  4505. }
  4506. void RenderingDevice::swap_buffers() {
  4507. ERR_FAIL_COND_MSG(local_device.is_valid(), "Local devices can't swap buffers.");
  4508. _THREAD_SAFE_METHOD_
  4509. context->postpare_buffers(frames[frame].draw_command_buffer);
  4510. _finalize_command_bufers();
  4511. screen_prepared = false;
  4512. // Swap buffers.
  4513. context->swap_buffers();
  4514. frame = (frame + 1) % frame_count;
  4515. _begin_frame();
  4516. }
  4517. void RenderingDevice::submit() {
  4518. _THREAD_SAFE_METHOD_
  4519. ERR_FAIL_COND_MSG(local_device.is_null(), "Only local devices can submit and sync.");
  4520. ERR_FAIL_COND_MSG(local_device_processing, "device already submitted, call sync to wait until done.");
  4521. _finalize_command_bufers();
  4522. RDD::CommandBufferID command_buffers[2] = { frames[frame].setup_command_buffer, frames[frame].draw_command_buffer };
  4523. context->local_device_push_command_buffers(local_device, command_buffers, 2);
  4524. local_device_processing = true;
  4525. }
  4526. void RenderingDevice::sync() {
  4527. _THREAD_SAFE_METHOD_
  4528. ERR_FAIL_COND_MSG(local_device.is_null(), "Only local devices can submit and sync.");
  4529. ERR_FAIL_COND_MSG(!local_device_processing, "sync can only be called after a submit");
  4530. context->local_device_sync(local_device);
  4531. _begin_frame();
  4532. local_device_processing = false;
  4533. }
  4534. void RenderingDevice::_free_pending_resources(int p_frame) {
  4535. // Free in dependency usage order, so nothing weird happens.
  4536. // Pipelines.
  4537. while (frames[p_frame].render_pipelines_to_dispose_of.front()) {
  4538. RenderPipeline *pipeline = &frames[p_frame].render_pipelines_to_dispose_of.front()->get();
  4539. driver->pipeline_free(pipeline->driver_id);
  4540. frames[p_frame].render_pipelines_to_dispose_of.pop_front();
  4541. }
  4542. while (frames[p_frame].compute_pipelines_to_dispose_of.front()) {
  4543. ComputePipeline *pipeline = &frames[p_frame].compute_pipelines_to_dispose_of.front()->get();
  4544. driver->pipeline_free(pipeline->driver_id);
  4545. frames[p_frame].compute_pipelines_to_dispose_of.pop_front();
  4546. }
  4547. // Uniform sets.
  4548. while (frames[p_frame].uniform_sets_to_dispose_of.front()) {
  4549. UniformSet *uniform_set = &frames[p_frame].uniform_sets_to_dispose_of.front()->get();
  4550. driver->uniform_set_free(uniform_set->driver_id);
  4551. frames[p_frame].uniform_sets_to_dispose_of.pop_front();
  4552. }
  4553. // Shaders.
  4554. while (frames[p_frame].shaders_to_dispose_of.front()) {
  4555. Shader *shader = &frames[p_frame].shaders_to_dispose_of.front()->get();
  4556. driver->shader_free(shader->driver_id);
  4557. frames[p_frame].shaders_to_dispose_of.pop_front();
  4558. }
  4559. // Samplers.
  4560. while (frames[p_frame].samplers_to_dispose_of.front()) {
  4561. RDD::SamplerID sampler = frames[p_frame].samplers_to_dispose_of.front()->get();
  4562. driver->sampler_free(sampler);
  4563. frames[p_frame].samplers_to_dispose_of.pop_front();
  4564. }
  4565. // Framebuffers.
  4566. while (frames[p_frame].framebuffers_to_dispose_of.front()) {
  4567. Framebuffer *framebuffer = &frames[p_frame].framebuffers_to_dispose_of.front()->get();
  4568. for (const KeyValue<Framebuffer::VersionKey, Framebuffer::Version> &E : framebuffer->framebuffers) {
  4569. // First framebuffer, then render pass because it depends on it.
  4570. driver->framebuffer_free(E.value.framebuffer);
  4571. driver->render_pass_free(E.value.render_pass);
  4572. }
  4573. frames[p_frame].framebuffers_to_dispose_of.pop_front();
  4574. }
  4575. // Textures.
  4576. while (frames[p_frame].textures_to_dispose_of.front()) {
  4577. Texture *texture = &frames[p_frame].textures_to_dispose_of.front()->get();
  4578. if (texture->bound) {
  4579. WARN_PRINT("Deleted a texture while it was bound.");
  4580. }
  4581. texture_memory -= driver->texture_get_allocation_size(texture->driver_id);
  4582. driver->texture_free(texture->driver_id);
  4583. frames[p_frame].textures_to_dispose_of.pop_front();
  4584. }
  4585. // Buffers.
  4586. while (frames[p_frame].buffers_to_dispose_of.front()) {
  4587. Buffer &buffer = frames[p_frame].buffers_to_dispose_of.front()->get();
  4588. driver->buffer_free(buffer.driver_id);
  4589. buffer_memory -= buffer.size;
  4590. frames[p_frame].buffers_to_dispose_of.pop_front();
  4591. }
  4592. }
  4593. void RenderingDevice::prepare_screen_for_drawing() {
  4594. _THREAD_SAFE_METHOD_
  4595. context->prepare_buffers(frames[frame].draw_command_buffer);
  4596. screen_prepared = true;
  4597. }
  4598. uint32_t RenderingDevice::get_frame_delay() const {
  4599. return frame_count;
  4600. }
  4601. uint64_t RenderingDevice::get_memory_usage(MemoryType p_type) const {
  4602. switch (p_type) {
  4603. case MEMORY_BUFFERS: {
  4604. return buffer_memory;
  4605. }
  4606. case MEMORY_TEXTURES: {
  4607. return texture_memory;
  4608. }
  4609. case MEMORY_TOTAL: {
  4610. return driver->get_total_memory_used();
  4611. }
  4612. default: {
  4613. DEV_ASSERT(false);
  4614. return 0;
  4615. }
  4616. }
  4617. }
  4618. void RenderingDevice::_flush(bool p_current_frame) {
  4619. if (local_device.is_valid() && !p_current_frame) {
  4620. return; // Flushing previous frames has no effect with local device.
  4621. }
  4622. // Not doing this crashes RADV (undefined behavior).
  4623. if (p_current_frame) {
  4624. driver->end_segment();
  4625. driver->command_buffer_end(frames[frame].setup_command_buffer);
  4626. driver->command_buffer_end(frames[frame].draw_command_buffer);
  4627. }
  4628. if (local_device.is_valid()) {
  4629. RDD::CommandBufferID command_buffers[2] = { frames[frame].setup_command_buffer, frames[frame].draw_command_buffer };
  4630. context->local_device_push_command_buffers(local_device, command_buffers, 2);
  4631. context->local_device_sync(local_device);
  4632. bool ok = driver->command_buffer_begin(frames[frame].setup_command_buffer);
  4633. ERR_FAIL_COND(!ok);
  4634. ok = driver->command_buffer_begin(frames[frame].draw_command_buffer);
  4635. ERR_FAIL_COND(!ok);
  4636. driver->begin_segment(frames[frame].draw_command_buffer, frame, frames_drawn);
  4637. } else {
  4638. context->flush(p_current_frame, p_current_frame);
  4639. // Re-create the setup command.
  4640. if (p_current_frame) {
  4641. bool ok = driver->command_buffer_begin(frames[frame].setup_command_buffer);
  4642. ERR_FAIL_COND(!ok);
  4643. context->set_setup_buffer(frames[frame].setup_command_buffer); // Append now so it's added before everything else.
  4644. ok = driver->command_buffer_begin(frames[frame].draw_command_buffer);
  4645. ERR_FAIL_COND(!ok);
  4646. context->append_command_buffer(frames[frame].draw_command_buffer);
  4647. driver->begin_segment(frames[frame].draw_command_buffer, frame, frames_drawn);
  4648. }
  4649. }
  4650. }
  4651. void RenderingDevice::initialize(ApiContextRD *p_context, bool p_local_device) {
  4652. context = p_context;
  4653. device_capabilities = p_context->get_device_capabilities();
  4654. if (p_local_device) {
  4655. frame_count = 1;
  4656. local_device = context->local_device_create();
  4657. } else {
  4658. frame_count = context->get_swapchain_image_count() + 1; // Always need one extra to ensure it's unused at any time, without having to use a fence for this.
  4659. }
  4660. driver = context->get_driver(local_device);
  4661. max_timestamp_query_elements = 256;
  4662. frames.resize(frame_count);
  4663. frame = 0;
  4664. // Create setup and frame buffers.
  4665. for (int i = 0; i < frame_count; i++) {
  4666. frames[i].index = 0;
  4667. // Create command pool, one per frame is recommended.
  4668. frames[i].command_pool = driver->command_pool_create(RDD::COMMAND_BUFFER_TYPE_PRIMARY);
  4669. ERR_FAIL_COND(!frames[i].command_pool);
  4670. // Create command buffers.
  4671. frames[i].setup_command_buffer = driver->command_buffer_create(RDD::COMMAND_BUFFER_TYPE_PRIMARY, frames[i].command_pool);
  4672. ERR_CONTINUE(!frames[i].setup_command_buffer);
  4673. frames[i].draw_command_buffer = driver->command_buffer_create(RDD::COMMAND_BUFFER_TYPE_PRIMARY, frames[i].command_pool);
  4674. ERR_CONTINUE(!frames[i].draw_command_buffer);
  4675. {
  4676. // Create query pool.
  4677. frames[i].timestamp_pool = driver->timestamp_query_pool_create(max_timestamp_query_elements);
  4678. frames[i].timestamp_names.resize(max_timestamp_query_elements);
  4679. frames[i].timestamp_cpu_values.resize(max_timestamp_query_elements);
  4680. frames[i].timestamp_count = 0;
  4681. frames[i].timestamp_result_names.resize(max_timestamp_query_elements);
  4682. frames[i].timestamp_cpu_result_values.resize(max_timestamp_query_elements);
  4683. frames[i].timestamp_result_values.resize(max_timestamp_query_elements);
  4684. frames[i].timestamp_result_count = 0;
  4685. }
  4686. }
  4687. {
  4688. // Begin the first command buffer for the first frame, so
  4689. // setting up things can be done in the meantime until swap_buffers(), which is called before advance.
  4690. bool ok = driver->command_buffer_begin(frames[0].setup_command_buffer);
  4691. ERR_FAIL_COND(!ok);
  4692. ok = driver->command_buffer_begin(frames[0].draw_command_buffer);
  4693. ERR_FAIL_COND(!ok);
  4694. if (local_device.is_null()) {
  4695. context->set_setup_buffer(frames[0].setup_command_buffer); // Append now so it's added before everything else.
  4696. context->append_command_buffer(frames[0].draw_command_buffer);
  4697. }
  4698. }
  4699. for (int i = 0; i < frame_count; i++) {
  4700. // Reset all queries in a query pool before doing any operations with them.
  4701. driver->command_timestamp_query_pool_reset(frames[0].setup_command_buffer, frames[i].timestamp_pool, max_timestamp_query_elements);
  4702. }
  4703. staging_buffer_block_size = GLOBAL_GET("rendering/rendering_device/staging_buffer/block_size_kb");
  4704. staging_buffer_block_size = MAX(4u, staging_buffer_block_size);
  4705. staging_buffer_block_size *= 1024; // Kb -> bytes.
  4706. staging_buffer_max_size = GLOBAL_GET("rendering/rendering_device/staging_buffer/max_size_mb");
  4707. staging_buffer_max_size = MAX(1u, staging_buffer_max_size);
  4708. staging_buffer_max_size *= 1024 * 1024;
  4709. if (staging_buffer_max_size < staging_buffer_block_size * 4) {
  4710. // Validate enough blocks.
  4711. staging_buffer_max_size = staging_buffer_block_size * 4;
  4712. }
  4713. texture_upload_region_size_px = GLOBAL_GET("rendering/rendering_device/staging_buffer/texture_upload_region_size_px");
  4714. texture_upload_region_size_px = nearest_power_of_2_templated(texture_upload_region_size_px);
  4715. frames_drawn = frame_count; // Start from frame count, so everything else is immediately old.
  4716. // Ensure current staging block is valid and at least one per frame exists.
  4717. staging_buffer_current = 0;
  4718. staging_buffer_used = false;
  4719. for (int i = 0; i < frame_count; i++) {
  4720. // Staging was never used, create a block.
  4721. Error err = _insert_staging_block();
  4722. ERR_CONTINUE(err != OK);
  4723. }
  4724. draw_list = nullptr;
  4725. draw_list_count = 0;
  4726. draw_list_split = false;
  4727. compute_list = nullptr;
  4728. pipelines_cache_file_path = "user://vulkan/pipelines";
  4729. pipelines_cache_file_path += "." + context->get_device_name().validate_filename().replace(" ", "_").to_lower();
  4730. if (Engine::get_singleton()->is_editor_hint()) {
  4731. pipelines_cache_file_path += ".editor";
  4732. }
  4733. pipelines_cache_file_path += ".cache";
  4734. Vector<uint8_t> cache_data = _load_pipeline_cache();
  4735. pipelines_cache_enabled = driver->pipeline_cache_create(cache_data);
  4736. if (pipelines_cache_enabled) {
  4737. pipelines_cache_size = driver->pipeline_cache_query_size();
  4738. print_verbose(vformat("Startup PSO cache (%.1f MiB)", pipelines_cache_size / (1024.0f * 1024.0f)));
  4739. }
  4740. }
  4741. Vector<uint8_t> RenderingDevice::_load_pipeline_cache() {
  4742. DirAccess::make_dir_recursive_absolute(pipelines_cache_file_path.get_base_dir());
  4743. if (FileAccess::exists(pipelines_cache_file_path)) {
  4744. Error file_error;
  4745. Vector<uint8_t> file_data = FileAccess::get_file_as_bytes(pipelines_cache_file_path, &file_error);
  4746. return file_data;
  4747. } else {
  4748. return Vector<uint8_t>();
  4749. }
  4750. }
  4751. void RenderingDevice::_update_pipeline_cache(bool p_closing) {
  4752. {
  4753. bool still_saving = pipelines_cache_save_task != WorkerThreadPool::INVALID_TASK_ID && !WorkerThreadPool::get_singleton()->is_task_completed(pipelines_cache_save_task);
  4754. if (still_saving) {
  4755. if (p_closing) {
  4756. WorkerThreadPool::get_singleton()->wait_for_task_completion(pipelines_cache_save_task);
  4757. pipelines_cache_save_task = WorkerThreadPool::INVALID_TASK_ID;
  4758. } else {
  4759. // We can't save until the currently running save is done. We'll retry next time; worst case, we'll save when exiting.
  4760. return;
  4761. }
  4762. }
  4763. }
  4764. {
  4765. size_t new_pipelines_cache_size = driver->pipeline_cache_query_size();
  4766. ERR_FAIL_COND(!new_pipelines_cache_size);
  4767. size_t difference = new_pipelines_cache_size - pipelines_cache_size;
  4768. bool must_save = false;
  4769. if (p_closing) {
  4770. must_save = difference > 0;
  4771. } else {
  4772. float save_interval = GLOBAL_GET("rendering/rendering_device/pipeline_cache/save_chunk_size_mb");
  4773. must_save = difference > 0 && difference / (1024.0f * 1024.0f) >= save_interval;
  4774. }
  4775. if (must_save) {
  4776. pipelines_cache_size = new_pipelines_cache_size;
  4777. } else {
  4778. return;
  4779. }
  4780. }
  4781. if (p_closing) {
  4782. _save_pipeline_cache(this);
  4783. } else {
  4784. pipelines_cache_save_task = WorkerThreadPool::get_singleton()->add_native_task(&_save_pipeline_cache, this, false, "PipelineCacheSave");
  4785. }
  4786. }
  4787. void RenderingDevice::_save_pipeline_cache(void *p_data) {
  4788. RenderingDevice *self = static_cast<RenderingDevice *>(p_data);
  4789. self->_thread_safe_.lock();
  4790. Vector<uint8_t> cache_blob = self->driver->pipeline_cache_serialize();
  4791. self->_thread_safe_.unlock();
  4792. if (cache_blob.size() == 0) {
  4793. return;
  4794. }
  4795. print_verbose(vformat("Updated PSO cache (%.1f MiB)", cache_blob.size() / (1024.0f * 1024.0f)));
  4796. Ref<FileAccess> f = FileAccess::open(self->pipelines_cache_file_path, FileAccess::WRITE, nullptr);
  4797. if (f.is_valid()) {
  4798. f->store_buffer(cache_blob);
  4799. }
  4800. }
  4801. template <class T>
  4802. void RenderingDevice::_free_rids(T &p_owner, const char *p_type) {
  4803. List<RID> owned;
  4804. p_owner.get_owned_list(&owned);
  4805. if (owned.size()) {
  4806. if (owned.size() == 1) {
  4807. WARN_PRINT(vformat("1 RID of type \"%s\" was leaked.", p_type));
  4808. } else {
  4809. WARN_PRINT(vformat("%d RIDs of type \"%s\" were leaked.", owned.size(), p_type));
  4810. }
  4811. for (const RID &E : owned) {
  4812. #ifdef DEV_ENABLED
  4813. if (resource_names.has(E)) {
  4814. print_line(String(" - ") + resource_names[E]);
  4815. }
  4816. #endif
  4817. free(E);
  4818. }
  4819. }
  4820. }
  4821. void RenderingDevice::capture_timestamp(const String &p_name) {
  4822. ERR_FAIL_COND_MSG(draw_list != nullptr, "Capturing timestamps during draw list creation is not allowed. Offending timestamp was: " + p_name);
  4823. ERR_FAIL_COND(frames[frame].timestamp_count >= max_timestamp_query_elements);
  4824. // This should be optional for profiling, else it will slow things down.
  4825. if (driver->api_trait_get(RDD::API_TRAIT_HONORS_PIPELINE_BARRIERS)) {
  4826. RDD::MemoryBarrier mb;
  4827. mb.src_access = (RDD::BARRIER_ACCESS_INDIRECT_COMMAND_READ_BIT |
  4828. RDD::BARRIER_ACCESS_INDEX_READ_BIT |
  4829. RDD::BARRIER_ACCESS_VERTEX_ATTRIBUTE_READ_BIT |
  4830. RDD::BARRIER_ACCESS_UNIFORM_READ_BIT |
  4831. RDD::BARRIER_ACCESS_INPUT_ATTACHMENT_READ_BIT |
  4832. RDD::BARRIER_ACCESS_SHADER_READ_BIT |
  4833. RDD::BARRIER_ACCESS_SHADER_WRITE_BIT |
  4834. RDD::BARRIER_ACCESS_COLOR_ATTACHMENT_READ_BIT |
  4835. RDD::BARRIER_ACCESS_COLOR_ATTACHMENT_WRITE_BIT |
  4836. RDD::BARRIER_ACCESS_DEPTH_STENCIL_ATTACHMENT_READ_BIT |
  4837. RDD::BARRIER_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT |
  4838. RDD::BARRIER_ACCESS_TRANSFER_READ_BIT |
  4839. RDD::BARRIER_ACCESS_TRANSFER_WRITE_BIT |
  4840. RDD::BARRIER_ACCESS_HOST_READ_BIT |
  4841. RDD::BARRIER_ACCESS_HOST_WRITE_BIT);
  4842. mb.dst_access = (RDD::BARRIER_ACCESS_INDIRECT_COMMAND_READ_BIT |
  4843. RDD::BARRIER_ACCESS_INDEX_READ_BIT |
  4844. RDD::BARRIER_ACCESS_VERTEX_ATTRIBUTE_READ_BIT |
  4845. RDD::BARRIER_ACCESS_UNIFORM_READ_BIT |
  4846. RDD::BARRIER_ACCESS_INPUT_ATTACHMENT_READ_BIT |
  4847. RDD::BARRIER_ACCESS_SHADER_READ_BIT |
  4848. RDD::BARRIER_ACCESS_SHADER_WRITE_BIT |
  4849. RDD::BARRIER_ACCESS_COLOR_ATTACHMENT_READ_BIT |
  4850. RDD::BARRIER_ACCESS_COLOR_ATTACHMENT_WRITE_BIT |
  4851. RDD::BARRIER_ACCESS_DEPTH_STENCIL_ATTACHMENT_READ_BIT |
  4852. RDD::BARRIER_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT |
  4853. RDD::BARRIER_ACCESS_TRANSFER_READ_BIT |
  4854. RDD::BARRIER_ACCESS_TRANSFER_WRITE_BIT |
  4855. RDD::BARRIER_ACCESS_HOST_READ_BIT |
  4856. RDD::BARRIER_ACCESS_HOST_WRITE_BIT);
  4857. driver->command_pipeline_barrier(frames[frame].draw_command_buffer, RDD::PIPELINE_STAGE_ALL_COMMANDS_BIT, RDD::PIPELINE_STAGE_ALL_COMMANDS_BIT, mb, {}, {});
  4858. }
  4859. driver->command_timestamp_write(frames[frame].draw_command_buffer, frames[frame].timestamp_pool, frames[frame].timestamp_count);
  4860. frames[frame].timestamp_names[frames[frame].timestamp_count] = p_name;
  4861. frames[frame].timestamp_cpu_values[frames[frame].timestamp_count] = OS::get_singleton()->get_ticks_usec();
  4862. frames[frame].timestamp_count++;
  4863. }
  4864. uint64_t RenderingDevice::get_driver_resource(DriverResource p_resource, RID p_rid, uint64_t p_index) {
  4865. _THREAD_SAFE_METHOD_
  4866. uint64_t driver_id = 0;
  4867. switch (p_resource) {
  4868. case DRIVER_RESOURCE_LOGICAL_DEVICE:
  4869. case DRIVER_RESOURCE_PHYSICAL_DEVICE:
  4870. case DRIVER_RESOURCE_TOPMOST_OBJECT:
  4871. case DRIVER_RESOURCE_COMMAND_QUEUE:
  4872. case DRIVER_RESOURCE_QUEUE_FAMILY:
  4873. break;
  4874. case DRIVER_RESOURCE_TEXTURE:
  4875. case DRIVER_RESOURCE_TEXTURE_VIEW:
  4876. case DRIVER_RESOURCE_TEXTURE_DATA_FORMAT: {
  4877. Texture *tex = texture_owner.get_or_null(p_rid);
  4878. ERR_FAIL_NULL_V(tex, 0);
  4879. driver_id = tex->driver_id;
  4880. } break;
  4881. case DRIVER_RESOURCE_SAMPLER: {
  4882. RDD::SamplerID *sampler_driver_id = sampler_owner.get_or_null(p_rid);
  4883. ERR_FAIL_NULL_V(sampler_driver_id, 0);
  4884. driver_id = *sampler_driver_id;
  4885. } break;
  4886. case DRIVER_RESOURCE_UNIFORM_SET: {
  4887. UniformSet *uniform_set = uniform_set_owner.get_or_null(p_rid);
  4888. ERR_FAIL_NULL_V(uniform_set, 0);
  4889. driver_id = uniform_set->driver_id;
  4890. } break;
  4891. case DRIVER_RESOURCE_BUFFER: {
  4892. Buffer *buffer = nullptr;
  4893. if (vertex_buffer_owner.owns(p_rid)) {
  4894. buffer = vertex_buffer_owner.get_or_null(p_rid);
  4895. } else if (index_buffer_owner.owns(p_rid)) {
  4896. buffer = index_buffer_owner.get_or_null(p_rid);
  4897. } else if (uniform_buffer_owner.owns(p_rid)) {
  4898. buffer = uniform_buffer_owner.get_or_null(p_rid);
  4899. } else if (texture_buffer_owner.owns(p_rid)) {
  4900. buffer = texture_buffer_owner.get_or_null(p_rid);
  4901. } else if (storage_buffer_owner.owns(p_rid)) {
  4902. buffer = storage_buffer_owner.get_or_null(p_rid);
  4903. }
  4904. ERR_FAIL_NULL_V(buffer, 0);
  4905. driver_id = buffer->driver_id;
  4906. } break;
  4907. case DRIVER_RESOURCE_COMPUTE_PIPELINE: {
  4908. ComputePipeline *compute_pipeline = compute_pipeline_owner.get_or_null(p_rid);
  4909. ERR_FAIL_NULL_V(compute_pipeline, 0);
  4910. driver_id = compute_pipeline->driver_id;
  4911. } break;
  4912. case DRIVER_RESOURCE_RENDER_PIPELINE: {
  4913. RenderPipeline *render_pipeline = render_pipeline_owner.get_or_null(p_rid);
  4914. ERR_FAIL_NULL_V(render_pipeline, 0);
  4915. driver_id = render_pipeline->driver_id;
  4916. } break;
  4917. default: {
  4918. ERR_FAIL_V(0);
  4919. } break;
  4920. }
  4921. return driver->get_resource_native_handle(p_resource, driver_id);
  4922. }
  4923. uint32_t RenderingDevice::get_captured_timestamps_count() const {
  4924. return frames[frame].timestamp_result_count;
  4925. }
  4926. uint64_t RenderingDevice::get_captured_timestamps_frame() const {
  4927. return frames[frame].index;
  4928. }
  4929. uint64_t RenderingDevice::get_captured_timestamp_gpu_time(uint32_t p_index) const {
  4930. ERR_FAIL_UNSIGNED_INDEX_V(p_index, frames[frame].timestamp_result_count, 0);
  4931. return driver->timestamp_query_result_to_time(frames[frame].timestamp_result_values[p_index]);
  4932. }
  4933. uint64_t RenderingDevice::get_captured_timestamp_cpu_time(uint32_t p_index) const {
  4934. ERR_FAIL_UNSIGNED_INDEX_V(p_index, frames[frame].timestamp_result_count, 0);
  4935. return frames[frame].timestamp_cpu_result_values[p_index];
  4936. }
  4937. String RenderingDevice::get_captured_timestamp_name(uint32_t p_index) const {
  4938. ERR_FAIL_UNSIGNED_INDEX_V(p_index, frames[frame].timestamp_result_count, String());
  4939. return frames[frame].timestamp_result_names[p_index];
  4940. }
  4941. uint64_t RenderingDevice::limit_get(Limit p_limit) const {
  4942. return driver->limit_get(p_limit);
  4943. }
  4944. void RenderingDevice::finalize() {
  4945. // Free all resources.
  4946. _flush(false);
  4947. _free_rids(render_pipeline_owner, "Pipeline");
  4948. _free_rids(compute_pipeline_owner, "Compute");
  4949. _free_rids(uniform_set_owner, "UniformSet");
  4950. _free_rids(texture_buffer_owner, "TextureBuffer");
  4951. _free_rids(storage_buffer_owner, "StorageBuffer");
  4952. _free_rids(uniform_buffer_owner, "UniformBuffer");
  4953. _free_rids(shader_owner, "Shader");
  4954. _free_rids(index_array_owner, "IndexArray");
  4955. _free_rids(index_buffer_owner, "IndexBuffer");
  4956. _free_rids(vertex_array_owner, "VertexArray");
  4957. _free_rids(vertex_buffer_owner, "VertexBuffer");
  4958. _free_rids(framebuffer_owner, "Framebuffer");
  4959. _free_rids(sampler_owner, "Sampler");
  4960. {
  4961. // For textures it's a bit more difficult because they may be shared.
  4962. List<RID> owned;
  4963. texture_owner.get_owned_list(&owned);
  4964. if (owned.size()) {
  4965. if (owned.size() == 1) {
  4966. WARN_PRINT("1 RID of type \"Texture\" was leaked.");
  4967. } else {
  4968. WARN_PRINT(vformat("%d RIDs of type \"Texture\" were leaked.", owned.size()));
  4969. }
  4970. // Free shared first.
  4971. for (List<RID>::Element *E = owned.front(); E;) {
  4972. List<RID>::Element *N = E->next();
  4973. if (texture_is_shared(E->get())) {
  4974. #ifdef DEV_ENABLED
  4975. if (resource_names.has(E->get())) {
  4976. print_line(String(" - ") + resource_names[E->get()]);
  4977. }
  4978. #endif
  4979. free(E->get());
  4980. owned.erase(E);
  4981. }
  4982. E = N;
  4983. }
  4984. // Free non shared second, this will avoid an error trying to free unexisting textures due to dependencies.
  4985. for (const RID &E : owned) {
  4986. #ifdef DEV_ENABLED
  4987. if (resource_names.has(E)) {
  4988. print_line(String(" - ") + resource_names[E]);
  4989. }
  4990. #endif
  4991. free(E);
  4992. }
  4993. }
  4994. }
  4995. // Free everything pending.
  4996. for (uint32_t i = 0; i < frames.size(); i++) {
  4997. int f = (frame + i) % frames.size();
  4998. _free_pending_resources(f);
  4999. driver->command_pool_free(frames[i].command_pool);
  5000. driver->timestamp_query_pool_free(frames[i].timestamp_pool);
  5001. }
  5002. if (pipelines_cache_enabled) {
  5003. _update_pipeline_cache(true);
  5004. driver->pipeline_cache_free();
  5005. }
  5006. for (int i = 0; i < split_draw_list_allocators.size(); i++) {
  5007. driver->command_pool_free(split_draw_list_allocators[i].command_pool);
  5008. }
  5009. frames.clear();
  5010. for (int i = 0; i < staging_buffer_blocks.size(); i++) {
  5011. driver->buffer_free(staging_buffer_blocks[i].driver_id);
  5012. }
  5013. while (vertex_formats.size()) {
  5014. HashMap<VertexFormatID, VertexDescriptionCache>::Iterator temp = vertex_formats.begin();
  5015. driver->vertex_format_free(temp->value.driver_id);
  5016. vertex_formats.remove(temp);
  5017. }
  5018. for (KeyValue<FramebufferFormatID, FramebufferFormat> &E : framebuffer_formats) {
  5019. driver->render_pass_free(E.value.render_pass);
  5020. }
  5021. framebuffer_formats.clear();
  5022. // All these should be clear at this point.
  5023. ERR_FAIL_COND(dependency_map.size());
  5024. ERR_FAIL_COND(reverse_dependency_map.size());
  5025. }
  5026. RenderingDevice *RenderingDevice::create_local_device() {
  5027. RenderingDevice *rd = memnew(RenderingDevice);
  5028. rd->initialize(context, true);
  5029. return rd;
  5030. }
  5031. bool RenderingDevice::has_feature(const Features p_feature) const {
  5032. return driver->has_feature(p_feature);
  5033. }
  5034. void RenderingDevice::_bind_methods() {
  5035. ClassDB::bind_method(D_METHOD("texture_create", "format", "view", "data"), &RenderingDevice::_texture_create, DEFVAL(Array()));
  5036. ClassDB::bind_method(D_METHOD("texture_create_shared", "view", "with_texture"), &RenderingDevice::_texture_create_shared);
  5037. ClassDB::bind_method(D_METHOD("texture_create_shared_from_slice", "view", "with_texture", "layer", "mipmap", "mipmaps", "slice_type"), &RenderingDevice::_texture_create_shared_from_slice, DEFVAL(1), DEFVAL(TEXTURE_SLICE_2D));
  5038. ClassDB::bind_method(D_METHOD("texture_create_from_extension", "type", "format", "samples", "usage_flags", "image", "width", "height", "depth", "layers"), &RenderingDevice::texture_create_from_extension);
  5039. ClassDB::bind_method(D_METHOD("texture_update", "texture", "layer", "data", "post_barrier"), &RenderingDevice::texture_update, DEFVAL(BARRIER_MASK_ALL_BARRIERS));
  5040. ClassDB::bind_method(D_METHOD("texture_get_data", "texture", "layer"), &RenderingDevice::texture_get_data);
  5041. ClassDB::bind_method(D_METHOD("texture_is_format_supported_for_usage", "format", "usage_flags"), &RenderingDevice::texture_is_format_supported_for_usage);
  5042. ClassDB::bind_method(D_METHOD("texture_is_shared", "texture"), &RenderingDevice::texture_is_shared);
  5043. ClassDB::bind_method(D_METHOD("texture_is_valid", "texture"), &RenderingDevice::texture_is_valid);
  5044. ClassDB::bind_method(D_METHOD("texture_copy", "from_texture", "to_texture", "from_pos", "to_pos", "size", "src_mipmap", "dst_mipmap", "src_layer", "dst_layer", "post_barrier"), &RenderingDevice::texture_copy, DEFVAL(BARRIER_MASK_ALL_BARRIERS));
  5045. ClassDB::bind_method(D_METHOD("texture_clear", "texture", "color", "base_mipmap", "mipmap_count", "base_layer", "layer_count", "post_barrier"), &RenderingDevice::texture_clear, DEFVAL(BARRIER_MASK_ALL_BARRIERS));
  5046. ClassDB::bind_method(D_METHOD("texture_resolve_multisample", "from_texture", "to_texture", "post_barrier"), &RenderingDevice::texture_resolve_multisample, DEFVAL(BARRIER_MASK_ALL_BARRIERS));
  5047. ClassDB::bind_method(D_METHOD("texture_get_format", "texture"), &RenderingDevice::_texture_get_format);
  5048. #ifndef DISABLE_DEPRECATED
  5049. ClassDB::bind_method(D_METHOD("texture_get_native_handle", "texture"), &RenderingDevice::texture_get_native_handle);
  5050. #endif
  5051. ClassDB::bind_method(D_METHOD("framebuffer_format_create", "attachments", "view_count"), &RenderingDevice::_framebuffer_format_create, DEFVAL(1));
  5052. ClassDB::bind_method(D_METHOD("framebuffer_format_create_multipass", "attachments", "passes", "view_count"), &RenderingDevice::_framebuffer_format_create_multipass, DEFVAL(1));
  5053. ClassDB::bind_method(D_METHOD("framebuffer_format_create_empty", "samples"), &RenderingDevice::framebuffer_format_create_empty, DEFVAL(TEXTURE_SAMPLES_1));
  5054. ClassDB::bind_method(D_METHOD("framebuffer_format_get_texture_samples", "format", "render_pass"), &RenderingDevice::framebuffer_format_get_texture_samples, DEFVAL(0));
  5055. ClassDB::bind_method(D_METHOD("framebuffer_create", "textures", "validate_with_format", "view_count"), &RenderingDevice::_framebuffer_create, DEFVAL(INVALID_FORMAT_ID), DEFVAL(1));
  5056. ClassDB::bind_method(D_METHOD("framebuffer_create_multipass", "textures", "passes", "validate_with_format", "view_count"), &RenderingDevice::_framebuffer_create_multipass, DEFVAL(INVALID_FORMAT_ID), DEFVAL(1));
  5057. ClassDB::bind_method(D_METHOD("framebuffer_create_empty", "size", "samples", "validate_with_format"), &RenderingDevice::framebuffer_create_empty, DEFVAL(TEXTURE_SAMPLES_1), DEFVAL(INVALID_FORMAT_ID));
  5058. ClassDB::bind_method(D_METHOD("framebuffer_get_format", "framebuffer"), &RenderingDevice::framebuffer_get_format);
  5059. ClassDB::bind_method(D_METHOD("framebuffer_is_valid", "framebuffer"), &RenderingDevice::framebuffer_is_valid);
  5060. ClassDB::bind_method(D_METHOD("sampler_create", "state"), &RenderingDevice::_sampler_create);
  5061. ClassDB::bind_method(D_METHOD("sampler_is_format_supported_for_filter", "format", "sampler_filter"), &RenderingDevice::sampler_is_format_supported_for_filter);
  5062. ClassDB::bind_method(D_METHOD("vertex_buffer_create", "size_bytes", "data", "use_as_storage"), &RenderingDevice::vertex_buffer_create, DEFVAL(Vector<uint8_t>()), DEFVAL(false));
  5063. ClassDB::bind_method(D_METHOD("vertex_format_create", "vertex_descriptions"), &RenderingDevice::_vertex_format_create);
  5064. ClassDB::bind_method(D_METHOD("vertex_array_create", "vertex_count", "vertex_format", "src_buffers", "offsets"), &RenderingDevice::_vertex_array_create, DEFVAL(Vector<int64_t>()));
  5065. ClassDB::bind_method(D_METHOD("index_buffer_create", "size_indices", "format", "data", "use_restart_indices"), &RenderingDevice::index_buffer_create, DEFVAL(Vector<uint8_t>()), DEFVAL(false));
  5066. ClassDB::bind_method(D_METHOD("index_array_create", "index_buffer", "index_offset", "index_count"), &RenderingDevice::index_array_create);
  5067. ClassDB::bind_method(D_METHOD("shader_compile_spirv_from_source", "shader_source", "allow_cache"), &RenderingDevice::_shader_compile_spirv_from_source, DEFVAL(true));
  5068. ClassDB::bind_method(D_METHOD("shader_compile_binary_from_spirv", "spirv_data", "name"), &RenderingDevice::_shader_compile_binary_from_spirv, DEFVAL(""));
  5069. ClassDB::bind_method(D_METHOD("shader_create_from_spirv", "spirv_data", "name"), &RenderingDevice::_shader_create_from_spirv, DEFVAL(""));
  5070. ClassDB::bind_method(D_METHOD("shader_create_from_bytecode", "binary_data", "placeholder_rid"), &RenderingDevice::shader_create_from_bytecode, DEFVAL(RID()));
  5071. ClassDB::bind_method(D_METHOD("shader_create_placeholder"), &RenderingDevice::shader_create_placeholder);
  5072. ClassDB::bind_method(D_METHOD("shader_get_vertex_input_attribute_mask", "shader"), &RenderingDevice::shader_get_vertex_input_attribute_mask);
  5073. ClassDB::bind_method(D_METHOD("uniform_buffer_create", "size_bytes", "data"), &RenderingDevice::uniform_buffer_create, DEFVAL(Vector<uint8_t>()));
  5074. ClassDB::bind_method(D_METHOD("storage_buffer_create", "size_bytes", "data", "usage"), &RenderingDevice::storage_buffer_create, DEFVAL(Vector<uint8_t>()), DEFVAL(0));
  5075. ClassDB::bind_method(D_METHOD("texture_buffer_create", "size_bytes", "format", "data"), &RenderingDevice::texture_buffer_create, DEFVAL(Vector<uint8_t>()));
  5076. ClassDB::bind_method(D_METHOD("uniform_set_create", "uniforms", "shader", "shader_set"), &RenderingDevice::_uniform_set_create);
  5077. ClassDB::bind_method(D_METHOD("uniform_set_is_valid", "uniform_set"), &RenderingDevice::uniform_set_is_valid);
  5078. ClassDB::bind_method(D_METHOD("buffer_update", "buffer", "offset", "size_bytes", "data", "post_barrier"), &RenderingDevice::_buffer_update_bind, DEFVAL(BARRIER_MASK_ALL_BARRIERS));
  5079. ClassDB::bind_method(D_METHOD("buffer_clear", "buffer", "offset", "size_bytes", "post_barrier"), &RenderingDevice::buffer_clear, DEFVAL(BARRIER_MASK_ALL_BARRIERS));
  5080. ClassDB::bind_method(D_METHOD("buffer_get_data", "buffer", "offset_bytes", "size_bytes"), &RenderingDevice::buffer_get_data, DEFVAL(0), DEFVAL(0));
  5081. ClassDB::bind_method(D_METHOD("render_pipeline_create", "shader", "framebuffer_format", "vertex_format", "primitive", "rasterization_state", "multisample_state", "stencil_state", "color_blend_state", "dynamic_state_flags", "for_render_pass", "specialization_constants"), &RenderingDevice::_render_pipeline_create, DEFVAL(0), DEFVAL(0), DEFVAL(TypedArray<RDPipelineSpecializationConstant>()));
  5082. ClassDB::bind_method(D_METHOD("render_pipeline_is_valid", "render_pipeline"), &RenderingDevice::render_pipeline_is_valid);
  5083. ClassDB::bind_method(D_METHOD("compute_pipeline_create", "shader", "specialization_constants"), &RenderingDevice::_compute_pipeline_create, DEFVAL(TypedArray<RDPipelineSpecializationConstant>()));
  5084. ClassDB::bind_method(D_METHOD("compute_pipeline_is_valid", "compute_pipeline"), &RenderingDevice::compute_pipeline_is_valid);
  5085. ClassDB::bind_method(D_METHOD("screen_get_width", "screen"), &RenderingDevice::screen_get_width, DEFVAL(DisplayServer::MAIN_WINDOW_ID));
  5086. ClassDB::bind_method(D_METHOD("screen_get_height", "screen"), &RenderingDevice::screen_get_height, DEFVAL(DisplayServer::MAIN_WINDOW_ID));
  5087. ClassDB::bind_method(D_METHOD("screen_get_framebuffer_format"), &RenderingDevice::screen_get_framebuffer_format);
  5088. ClassDB::bind_method(D_METHOD("draw_list_begin_for_screen", "screen", "clear_color"), &RenderingDevice::draw_list_begin_for_screen, DEFVAL(DisplayServer::MAIN_WINDOW_ID), DEFVAL(Color()));
  5089. ClassDB::bind_method(D_METHOD("draw_list_begin", "framebuffer", "initial_color_action", "final_color_action", "initial_depth_action", "final_depth_action", "clear_color_values", "clear_depth", "clear_stencil", "region", "storage_textures"), &RenderingDevice::_draw_list_begin, DEFVAL(Vector<Color>()), DEFVAL(1.0), DEFVAL(0), DEFVAL(Rect2()), DEFVAL(TypedArray<RID>()));
  5090. ClassDB::bind_method(D_METHOD("draw_list_begin_split", "framebuffer", "splits", "initial_color_action", "final_color_action", "initial_depth_action", "final_depth_action", "clear_color_values", "clear_depth", "clear_stencil", "region", "storage_textures"), &RenderingDevice::_draw_list_begin_split, DEFVAL(Vector<Color>()), DEFVAL(1.0), DEFVAL(0), DEFVAL(Rect2()), DEFVAL(TypedArray<RID>()));
  5091. ClassDB::bind_method(D_METHOD("draw_list_set_blend_constants", "draw_list", "color"), &RenderingDevice::draw_list_set_blend_constants);
  5092. ClassDB::bind_method(D_METHOD("draw_list_bind_render_pipeline", "draw_list", "render_pipeline"), &RenderingDevice::draw_list_bind_render_pipeline);
  5093. ClassDB::bind_method(D_METHOD("draw_list_bind_uniform_set", "draw_list", "uniform_set", "set_index"), &RenderingDevice::draw_list_bind_uniform_set);
  5094. ClassDB::bind_method(D_METHOD("draw_list_bind_vertex_array", "draw_list", "vertex_array"), &RenderingDevice::draw_list_bind_vertex_array);
  5095. ClassDB::bind_method(D_METHOD("draw_list_bind_index_array", "draw_list", "index_array"), &RenderingDevice::draw_list_bind_index_array);
  5096. ClassDB::bind_method(D_METHOD("draw_list_set_push_constant", "draw_list", "buffer", "size_bytes"), &RenderingDevice::_draw_list_set_push_constant);
  5097. ClassDB::bind_method(D_METHOD("draw_list_draw", "draw_list", "use_indices", "instances", "procedural_vertex_count"), &RenderingDevice::draw_list_draw, DEFVAL(0));
  5098. ClassDB::bind_method(D_METHOD("draw_list_enable_scissor", "draw_list", "rect"), &RenderingDevice::draw_list_enable_scissor, DEFVAL(Rect2()));
  5099. ClassDB::bind_method(D_METHOD("draw_list_disable_scissor", "draw_list"), &RenderingDevice::draw_list_disable_scissor);
  5100. ClassDB::bind_method(D_METHOD("draw_list_switch_to_next_pass"), &RenderingDevice::draw_list_switch_to_next_pass);
  5101. ClassDB::bind_method(D_METHOD("draw_list_switch_to_next_pass_split", "splits"), &RenderingDevice::_draw_list_switch_to_next_pass_split);
  5102. ClassDB::bind_method(D_METHOD("draw_list_end", "post_barrier"), &RenderingDevice::draw_list_end, DEFVAL(BARRIER_MASK_ALL_BARRIERS));
  5103. ClassDB::bind_method(D_METHOD("compute_list_begin", "allow_draw_overlap"), &RenderingDevice::compute_list_begin, DEFVAL(false));
  5104. ClassDB::bind_method(D_METHOD("compute_list_bind_compute_pipeline", "compute_list", "compute_pipeline"), &RenderingDevice::compute_list_bind_compute_pipeline);
  5105. ClassDB::bind_method(D_METHOD("compute_list_set_push_constant", "compute_list", "buffer", "size_bytes"), &RenderingDevice::_compute_list_set_push_constant);
  5106. ClassDB::bind_method(D_METHOD("compute_list_bind_uniform_set", "compute_list", "uniform_set", "set_index"), &RenderingDevice::compute_list_bind_uniform_set);
  5107. ClassDB::bind_method(D_METHOD("compute_list_dispatch", "compute_list", "x_groups", "y_groups", "z_groups"), &RenderingDevice::compute_list_dispatch);
  5108. ClassDB::bind_method(D_METHOD("compute_list_add_barrier", "compute_list"), &RenderingDevice::compute_list_add_barrier);
  5109. ClassDB::bind_method(D_METHOD("compute_list_end", "post_barrier"), &RenderingDevice::compute_list_end, DEFVAL(BARRIER_MASK_ALL_BARRIERS));
  5110. ClassDB::bind_method(D_METHOD("free_rid", "rid"), &RenderingDevice::free);
  5111. ClassDB::bind_method(D_METHOD("capture_timestamp", "name"), &RenderingDevice::capture_timestamp);
  5112. ClassDB::bind_method(D_METHOD("get_captured_timestamps_count"), &RenderingDevice::get_captured_timestamps_count);
  5113. ClassDB::bind_method(D_METHOD("get_captured_timestamps_frame"), &RenderingDevice::get_captured_timestamps_frame);
  5114. ClassDB::bind_method(D_METHOD("get_captured_timestamp_gpu_time", "index"), &RenderingDevice::get_captured_timestamp_gpu_time);
  5115. ClassDB::bind_method(D_METHOD("get_captured_timestamp_cpu_time", "index"), &RenderingDevice::get_captured_timestamp_cpu_time);
  5116. ClassDB::bind_method(D_METHOD("get_captured_timestamp_name", "index"), &RenderingDevice::get_captured_timestamp_name);
  5117. ClassDB::bind_method(D_METHOD("limit_get", "limit"), &RenderingDevice::limit_get);
  5118. ClassDB::bind_method(D_METHOD("get_frame_delay"), &RenderingDevice::get_frame_delay);
  5119. ClassDB::bind_method(D_METHOD("submit"), &RenderingDevice::submit);
  5120. ClassDB::bind_method(D_METHOD("sync"), &RenderingDevice::sync);
  5121. ClassDB::bind_method(D_METHOD("barrier", "from", "to"), &RenderingDevice::barrier, DEFVAL(BARRIER_MASK_ALL_BARRIERS), DEFVAL(BARRIER_MASK_ALL_BARRIERS));
  5122. ClassDB::bind_method(D_METHOD("full_barrier"), &RenderingDevice::full_barrier);
  5123. ClassDB::bind_method(D_METHOD("create_local_device"), &RenderingDevice::create_local_device);
  5124. ClassDB::bind_method(D_METHOD("set_resource_name", "id", "name"), &RenderingDevice::set_resource_name);
  5125. ClassDB::bind_method(D_METHOD("draw_command_begin_label", "name", "color"), &RenderingDevice::draw_command_begin_label);
  5126. ClassDB::bind_method(D_METHOD("draw_command_insert_label", "name", "color"), &RenderingDevice::draw_command_insert_label);
  5127. ClassDB::bind_method(D_METHOD("draw_command_end_label"), &RenderingDevice::draw_command_end_label);
  5128. ClassDB::bind_method(D_METHOD("get_device_vendor_name"), &RenderingDevice::get_device_vendor_name);
  5129. ClassDB::bind_method(D_METHOD("get_device_name"), &RenderingDevice::get_device_name);
  5130. ClassDB::bind_method(D_METHOD("get_device_pipeline_cache_uuid"), &RenderingDevice::get_device_pipeline_cache_uuid);
  5131. ClassDB::bind_method(D_METHOD("get_memory_usage", "type"), &RenderingDevice::get_memory_usage);
  5132. ClassDB::bind_method(D_METHOD("get_driver_resource", "resource", "rid", "index"), &RenderingDevice::get_driver_resource);
  5133. BIND_ENUM_CONSTANT(DEVICE_TYPE_OTHER);
  5134. BIND_ENUM_CONSTANT(DEVICE_TYPE_INTEGRATED_GPU);
  5135. BIND_ENUM_CONSTANT(DEVICE_TYPE_DISCRETE_GPU);
  5136. BIND_ENUM_CONSTANT(DEVICE_TYPE_VIRTUAL_GPU);
  5137. BIND_ENUM_CONSTANT(DEVICE_TYPE_CPU);
  5138. BIND_ENUM_CONSTANT(DEVICE_TYPE_MAX);
  5139. BIND_ENUM_CONSTANT(DRIVER_RESOURCE_LOGICAL_DEVICE);
  5140. BIND_ENUM_CONSTANT(DRIVER_RESOURCE_PHYSICAL_DEVICE);
  5141. BIND_ENUM_CONSTANT(DRIVER_RESOURCE_TOPMOST_OBJECT);
  5142. BIND_ENUM_CONSTANT(DRIVER_RESOURCE_COMMAND_QUEUE);
  5143. BIND_ENUM_CONSTANT(DRIVER_RESOURCE_QUEUE_FAMILY);
  5144. BIND_ENUM_CONSTANT(DRIVER_RESOURCE_TEXTURE);
  5145. BIND_ENUM_CONSTANT(DRIVER_RESOURCE_TEXTURE_VIEW);
  5146. BIND_ENUM_CONSTANT(DRIVER_RESOURCE_TEXTURE_DATA_FORMAT);
  5147. BIND_ENUM_CONSTANT(DRIVER_RESOURCE_SAMPLER);
  5148. BIND_ENUM_CONSTANT(DRIVER_RESOURCE_UNIFORM_SET);
  5149. BIND_ENUM_CONSTANT(DRIVER_RESOURCE_BUFFER);
  5150. BIND_ENUM_CONSTANT(DRIVER_RESOURCE_COMPUTE_PIPELINE);
  5151. BIND_ENUM_CONSTANT(DRIVER_RESOURCE_RENDER_PIPELINE);
  5152. #ifndef DISABLE_DEPRECATED
  5153. BIND_ENUM_CONSTANT(DRIVER_RESOURCE_VULKAN_DEVICE);
  5154. BIND_ENUM_CONSTANT(DRIVER_RESOURCE_VULKAN_PHYSICAL_DEVICE);
  5155. BIND_ENUM_CONSTANT(DRIVER_RESOURCE_VULKAN_INSTANCE);
  5156. BIND_ENUM_CONSTANT(DRIVER_RESOURCE_VULKAN_QUEUE);
  5157. BIND_ENUM_CONSTANT(DRIVER_RESOURCE_VULKAN_QUEUE_FAMILY_INDEX);
  5158. BIND_ENUM_CONSTANT(DRIVER_RESOURCE_VULKAN_IMAGE);
  5159. BIND_ENUM_CONSTANT(DRIVER_RESOURCE_VULKAN_IMAGE_VIEW);
  5160. BIND_ENUM_CONSTANT(DRIVER_RESOURCE_VULKAN_IMAGE_NATIVE_TEXTURE_FORMAT);
  5161. BIND_ENUM_CONSTANT(DRIVER_RESOURCE_VULKAN_SAMPLER);
  5162. BIND_ENUM_CONSTANT(DRIVER_RESOURCE_VULKAN_DESCRIPTOR_SET);
  5163. BIND_ENUM_CONSTANT(DRIVER_RESOURCE_VULKAN_BUFFER);
  5164. BIND_ENUM_CONSTANT(DRIVER_RESOURCE_VULKAN_COMPUTE_PIPELINE);
  5165. BIND_ENUM_CONSTANT(DRIVER_RESOURCE_VULKAN_RENDER_PIPELINE);
  5166. #endif
  5167. BIND_ENUM_CONSTANT(DATA_FORMAT_R4G4_UNORM_PACK8);
  5168. BIND_ENUM_CONSTANT(DATA_FORMAT_R4G4B4A4_UNORM_PACK16);
  5169. BIND_ENUM_CONSTANT(DATA_FORMAT_B4G4R4A4_UNORM_PACK16);
  5170. BIND_ENUM_CONSTANT(DATA_FORMAT_R5G6B5_UNORM_PACK16);
  5171. BIND_ENUM_CONSTANT(DATA_FORMAT_B5G6R5_UNORM_PACK16);
  5172. BIND_ENUM_CONSTANT(DATA_FORMAT_R5G5B5A1_UNORM_PACK16);
  5173. BIND_ENUM_CONSTANT(DATA_FORMAT_B5G5R5A1_UNORM_PACK16);
  5174. BIND_ENUM_CONSTANT(DATA_FORMAT_A1R5G5B5_UNORM_PACK16);
  5175. BIND_ENUM_CONSTANT(DATA_FORMAT_R8_UNORM);
  5176. BIND_ENUM_CONSTANT(DATA_FORMAT_R8_SNORM);
  5177. BIND_ENUM_CONSTANT(DATA_FORMAT_R8_USCALED);
  5178. BIND_ENUM_CONSTANT(DATA_FORMAT_R8_SSCALED);
  5179. BIND_ENUM_CONSTANT(DATA_FORMAT_R8_UINT);
  5180. BIND_ENUM_CONSTANT(DATA_FORMAT_R8_SINT);
  5181. BIND_ENUM_CONSTANT(DATA_FORMAT_R8_SRGB);
  5182. BIND_ENUM_CONSTANT(DATA_FORMAT_R8G8_UNORM);
  5183. BIND_ENUM_CONSTANT(DATA_FORMAT_R8G8_SNORM);
  5184. BIND_ENUM_CONSTANT(DATA_FORMAT_R8G8_USCALED);
  5185. BIND_ENUM_CONSTANT(DATA_FORMAT_R8G8_SSCALED);
  5186. BIND_ENUM_CONSTANT(DATA_FORMAT_R8G8_UINT);
  5187. BIND_ENUM_CONSTANT(DATA_FORMAT_R8G8_SINT);
  5188. BIND_ENUM_CONSTANT(DATA_FORMAT_R8G8_SRGB);
  5189. BIND_ENUM_CONSTANT(DATA_FORMAT_R8G8B8_UNORM);
  5190. BIND_ENUM_CONSTANT(DATA_FORMAT_R8G8B8_SNORM);
  5191. BIND_ENUM_CONSTANT(DATA_FORMAT_R8G8B8_USCALED);
  5192. BIND_ENUM_CONSTANT(DATA_FORMAT_R8G8B8_SSCALED);
  5193. BIND_ENUM_CONSTANT(DATA_FORMAT_R8G8B8_UINT);
  5194. BIND_ENUM_CONSTANT(DATA_FORMAT_R8G8B8_SINT);
  5195. BIND_ENUM_CONSTANT(DATA_FORMAT_R8G8B8_SRGB);
  5196. BIND_ENUM_CONSTANT(DATA_FORMAT_B8G8R8_UNORM);
  5197. BIND_ENUM_CONSTANT(DATA_FORMAT_B8G8R8_SNORM);
  5198. BIND_ENUM_CONSTANT(DATA_FORMAT_B8G8R8_USCALED);
  5199. BIND_ENUM_CONSTANT(DATA_FORMAT_B8G8R8_SSCALED);
  5200. BIND_ENUM_CONSTANT(DATA_FORMAT_B8G8R8_UINT);
  5201. BIND_ENUM_CONSTANT(DATA_FORMAT_B8G8R8_SINT);
  5202. BIND_ENUM_CONSTANT(DATA_FORMAT_B8G8R8_SRGB);
  5203. BIND_ENUM_CONSTANT(DATA_FORMAT_R8G8B8A8_UNORM);
  5204. BIND_ENUM_CONSTANT(DATA_FORMAT_R8G8B8A8_SNORM);
  5205. BIND_ENUM_CONSTANT(DATA_FORMAT_R8G8B8A8_USCALED);
  5206. BIND_ENUM_CONSTANT(DATA_FORMAT_R8G8B8A8_SSCALED);
  5207. BIND_ENUM_CONSTANT(DATA_FORMAT_R8G8B8A8_UINT);
  5208. BIND_ENUM_CONSTANT(DATA_FORMAT_R8G8B8A8_SINT);
  5209. BIND_ENUM_CONSTANT(DATA_FORMAT_R8G8B8A8_SRGB);
  5210. BIND_ENUM_CONSTANT(DATA_FORMAT_B8G8R8A8_UNORM);
  5211. BIND_ENUM_CONSTANT(DATA_FORMAT_B8G8R8A8_SNORM);
  5212. BIND_ENUM_CONSTANT(DATA_FORMAT_B8G8R8A8_USCALED);
  5213. BIND_ENUM_CONSTANT(DATA_FORMAT_B8G8R8A8_SSCALED);
  5214. BIND_ENUM_CONSTANT(DATA_FORMAT_B8G8R8A8_UINT);
  5215. BIND_ENUM_CONSTANT(DATA_FORMAT_B8G8R8A8_SINT);
  5216. BIND_ENUM_CONSTANT(DATA_FORMAT_B8G8R8A8_SRGB);
  5217. BIND_ENUM_CONSTANT(DATA_FORMAT_A8B8G8R8_UNORM_PACK32);
  5218. BIND_ENUM_CONSTANT(DATA_FORMAT_A8B8G8R8_SNORM_PACK32);
  5219. BIND_ENUM_CONSTANT(DATA_FORMAT_A8B8G8R8_USCALED_PACK32);
  5220. BIND_ENUM_CONSTANT(DATA_FORMAT_A8B8G8R8_SSCALED_PACK32);
  5221. BIND_ENUM_CONSTANT(DATA_FORMAT_A8B8G8R8_UINT_PACK32);
  5222. BIND_ENUM_CONSTANT(DATA_FORMAT_A8B8G8R8_SINT_PACK32);
  5223. BIND_ENUM_CONSTANT(DATA_FORMAT_A8B8G8R8_SRGB_PACK32);
  5224. BIND_ENUM_CONSTANT(DATA_FORMAT_A2R10G10B10_UNORM_PACK32);
  5225. BIND_ENUM_CONSTANT(DATA_FORMAT_A2R10G10B10_SNORM_PACK32);
  5226. BIND_ENUM_CONSTANT(DATA_FORMAT_A2R10G10B10_USCALED_PACK32);
  5227. BIND_ENUM_CONSTANT(DATA_FORMAT_A2R10G10B10_SSCALED_PACK32);
  5228. BIND_ENUM_CONSTANT(DATA_FORMAT_A2R10G10B10_UINT_PACK32);
  5229. BIND_ENUM_CONSTANT(DATA_FORMAT_A2R10G10B10_SINT_PACK32);
  5230. BIND_ENUM_CONSTANT(DATA_FORMAT_A2B10G10R10_UNORM_PACK32);
  5231. BIND_ENUM_CONSTANT(DATA_FORMAT_A2B10G10R10_SNORM_PACK32);
  5232. BIND_ENUM_CONSTANT(DATA_FORMAT_A2B10G10R10_USCALED_PACK32);
  5233. BIND_ENUM_CONSTANT(DATA_FORMAT_A2B10G10R10_SSCALED_PACK32);
  5234. BIND_ENUM_CONSTANT(DATA_FORMAT_A2B10G10R10_UINT_PACK32);
  5235. BIND_ENUM_CONSTANT(DATA_FORMAT_A2B10G10R10_SINT_PACK32);
  5236. BIND_ENUM_CONSTANT(DATA_FORMAT_R16_UNORM);
  5237. BIND_ENUM_CONSTANT(DATA_FORMAT_R16_SNORM);
  5238. BIND_ENUM_CONSTANT(DATA_FORMAT_R16_USCALED);
  5239. BIND_ENUM_CONSTANT(DATA_FORMAT_R16_SSCALED);
  5240. BIND_ENUM_CONSTANT(DATA_FORMAT_R16_UINT);
  5241. BIND_ENUM_CONSTANT(DATA_FORMAT_R16_SINT);
  5242. BIND_ENUM_CONSTANT(DATA_FORMAT_R16_SFLOAT);
  5243. BIND_ENUM_CONSTANT(DATA_FORMAT_R16G16_UNORM);
  5244. BIND_ENUM_CONSTANT(DATA_FORMAT_R16G16_SNORM);
  5245. BIND_ENUM_CONSTANT(DATA_FORMAT_R16G16_USCALED);
  5246. BIND_ENUM_CONSTANT(DATA_FORMAT_R16G16_SSCALED);
  5247. BIND_ENUM_CONSTANT(DATA_FORMAT_R16G16_UINT);
  5248. BIND_ENUM_CONSTANT(DATA_FORMAT_R16G16_SINT);
  5249. BIND_ENUM_CONSTANT(DATA_FORMAT_R16G16_SFLOAT);
  5250. BIND_ENUM_CONSTANT(DATA_FORMAT_R16G16B16_UNORM);
  5251. BIND_ENUM_CONSTANT(DATA_FORMAT_R16G16B16_SNORM);
  5252. BIND_ENUM_CONSTANT(DATA_FORMAT_R16G16B16_USCALED);
  5253. BIND_ENUM_CONSTANT(DATA_FORMAT_R16G16B16_SSCALED);
  5254. BIND_ENUM_CONSTANT(DATA_FORMAT_R16G16B16_UINT);
  5255. BIND_ENUM_CONSTANT(DATA_FORMAT_R16G16B16_SINT);
  5256. BIND_ENUM_CONSTANT(DATA_FORMAT_R16G16B16_SFLOAT);
  5257. BIND_ENUM_CONSTANT(DATA_FORMAT_R16G16B16A16_UNORM);
  5258. BIND_ENUM_CONSTANT(DATA_FORMAT_R16G16B16A16_SNORM);
  5259. BIND_ENUM_CONSTANT(DATA_FORMAT_R16G16B16A16_USCALED);
  5260. BIND_ENUM_CONSTANT(DATA_FORMAT_R16G16B16A16_SSCALED);
  5261. BIND_ENUM_CONSTANT(DATA_FORMAT_R16G16B16A16_UINT);
  5262. BIND_ENUM_CONSTANT(DATA_FORMAT_R16G16B16A16_SINT);
  5263. BIND_ENUM_CONSTANT(DATA_FORMAT_R16G16B16A16_SFLOAT);
  5264. BIND_ENUM_CONSTANT(DATA_FORMAT_R32_UINT);
  5265. BIND_ENUM_CONSTANT(DATA_FORMAT_R32_SINT);
  5266. BIND_ENUM_CONSTANT(DATA_FORMAT_R32_SFLOAT);
  5267. BIND_ENUM_CONSTANT(DATA_FORMAT_R32G32_UINT);
  5268. BIND_ENUM_CONSTANT(DATA_FORMAT_R32G32_SINT);
  5269. BIND_ENUM_CONSTANT(DATA_FORMAT_R32G32_SFLOAT);
  5270. BIND_ENUM_CONSTANT(DATA_FORMAT_R32G32B32_UINT);
  5271. BIND_ENUM_CONSTANT(DATA_FORMAT_R32G32B32_SINT);
  5272. BIND_ENUM_CONSTANT(DATA_FORMAT_R32G32B32_SFLOAT);
  5273. BIND_ENUM_CONSTANT(DATA_FORMAT_R32G32B32A32_UINT);
  5274. BIND_ENUM_CONSTANT(DATA_FORMAT_R32G32B32A32_SINT);
  5275. BIND_ENUM_CONSTANT(DATA_FORMAT_R32G32B32A32_SFLOAT);
  5276. BIND_ENUM_CONSTANT(DATA_FORMAT_R64_UINT);
  5277. BIND_ENUM_CONSTANT(DATA_FORMAT_R64_SINT);
  5278. BIND_ENUM_CONSTANT(DATA_FORMAT_R64_SFLOAT);
  5279. BIND_ENUM_CONSTANT(DATA_FORMAT_R64G64_UINT);
  5280. BIND_ENUM_CONSTANT(DATA_FORMAT_R64G64_SINT);
  5281. BIND_ENUM_CONSTANT(DATA_FORMAT_R64G64_SFLOAT);
  5282. BIND_ENUM_CONSTANT(DATA_FORMAT_R64G64B64_UINT);
  5283. BIND_ENUM_CONSTANT(DATA_FORMAT_R64G64B64_SINT);
  5284. BIND_ENUM_CONSTANT(DATA_FORMAT_R64G64B64_SFLOAT);
  5285. BIND_ENUM_CONSTANT(DATA_FORMAT_R64G64B64A64_UINT);
  5286. BIND_ENUM_CONSTANT(DATA_FORMAT_R64G64B64A64_SINT);
  5287. BIND_ENUM_CONSTANT(DATA_FORMAT_R64G64B64A64_SFLOAT);
  5288. BIND_ENUM_CONSTANT(DATA_FORMAT_B10G11R11_UFLOAT_PACK32);
  5289. BIND_ENUM_CONSTANT(DATA_FORMAT_E5B9G9R9_UFLOAT_PACK32);
  5290. BIND_ENUM_CONSTANT(DATA_FORMAT_D16_UNORM);
  5291. BIND_ENUM_CONSTANT(DATA_FORMAT_X8_D24_UNORM_PACK32);
  5292. BIND_ENUM_CONSTANT(DATA_FORMAT_D32_SFLOAT);
  5293. BIND_ENUM_CONSTANT(DATA_FORMAT_S8_UINT);
  5294. BIND_ENUM_CONSTANT(DATA_FORMAT_D16_UNORM_S8_UINT);
  5295. BIND_ENUM_CONSTANT(DATA_FORMAT_D24_UNORM_S8_UINT);
  5296. BIND_ENUM_CONSTANT(DATA_FORMAT_D32_SFLOAT_S8_UINT);
  5297. BIND_ENUM_CONSTANT(DATA_FORMAT_BC1_RGB_UNORM_BLOCK);
  5298. BIND_ENUM_CONSTANT(DATA_FORMAT_BC1_RGB_SRGB_BLOCK);
  5299. BIND_ENUM_CONSTANT(DATA_FORMAT_BC1_RGBA_UNORM_BLOCK);
  5300. BIND_ENUM_CONSTANT(DATA_FORMAT_BC1_RGBA_SRGB_BLOCK);
  5301. BIND_ENUM_CONSTANT(DATA_FORMAT_BC2_UNORM_BLOCK);
  5302. BIND_ENUM_CONSTANT(DATA_FORMAT_BC2_SRGB_BLOCK);
  5303. BIND_ENUM_CONSTANT(DATA_FORMAT_BC3_UNORM_BLOCK);
  5304. BIND_ENUM_CONSTANT(DATA_FORMAT_BC3_SRGB_BLOCK);
  5305. BIND_ENUM_CONSTANT(DATA_FORMAT_BC4_UNORM_BLOCK);
  5306. BIND_ENUM_CONSTANT(DATA_FORMAT_BC4_SNORM_BLOCK);
  5307. BIND_ENUM_CONSTANT(DATA_FORMAT_BC5_UNORM_BLOCK);
  5308. BIND_ENUM_CONSTANT(DATA_FORMAT_BC5_SNORM_BLOCK);
  5309. BIND_ENUM_CONSTANT(DATA_FORMAT_BC6H_UFLOAT_BLOCK);
  5310. BIND_ENUM_CONSTANT(DATA_FORMAT_BC6H_SFLOAT_BLOCK);
  5311. BIND_ENUM_CONSTANT(DATA_FORMAT_BC7_UNORM_BLOCK);
  5312. BIND_ENUM_CONSTANT(DATA_FORMAT_BC7_SRGB_BLOCK);
  5313. BIND_ENUM_CONSTANT(DATA_FORMAT_ETC2_R8G8B8_UNORM_BLOCK);
  5314. BIND_ENUM_CONSTANT(DATA_FORMAT_ETC2_R8G8B8_SRGB_BLOCK);
  5315. BIND_ENUM_CONSTANT(DATA_FORMAT_ETC2_R8G8B8A1_UNORM_BLOCK);
  5316. BIND_ENUM_CONSTANT(DATA_FORMAT_ETC2_R8G8B8A1_SRGB_BLOCK);
  5317. BIND_ENUM_CONSTANT(DATA_FORMAT_ETC2_R8G8B8A8_UNORM_BLOCK);
  5318. BIND_ENUM_CONSTANT(DATA_FORMAT_ETC2_R8G8B8A8_SRGB_BLOCK);
  5319. BIND_ENUM_CONSTANT(DATA_FORMAT_EAC_R11_UNORM_BLOCK);
  5320. BIND_ENUM_CONSTANT(DATA_FORMAT_EAC_R11_SNORM_BLOCK);
  5321. BIND_ENUM_CONSTANT(DATA_FORMAT_EAC_R11G11_UNORM_BLOCK);
  5322. BIND_ENUM_CONSTANT(DATA_FORMAT_EAC_R11G11_SNORM_BLOCK);
  5323. BIND_ENUM_CONSTANT(DATA_FORMAT_ASTC_4x4_UNORM_BLOCK);
  5324. BIND_ENUM_CONSTANT(DATA_FORMAT_ASTC_4x4_SRGB_BLOCK);
  5325. BIND_ENUM_CONSTANT(DATA_FORMAT_ASTC_5x4_UNORM_BLOCK);
  5326. BIND_ENUM_CONSTANT(DATA_FORMAT_ASTC_5x4_SRGB_BLOCK);
  5327. BIND_ENUM_CONSTANT(DATA_FORMAT_ASTC_5x5_UNORM_BLOCK);
  5328. BIND_ENUM_CONSTANT(DATA_FORMAT_ASTC_5x5_SRGB_BLOCK);
  5329. BIND_ENUM_CONSTANT(DATA_FORMAT_ASTC_6x5_UNORM_BLOCK);
  5330. BIND_ENUM_CONSTANT(DATA_FORMAT_ASTC_6x5_SRGB_BLOCK);
  5331. BIND_ENUM_CONSTANT(DATA_FORMAT_ASTC_6x6_UNORM_BLOCK);
  5332. BIND_ENUM_CONSTANT(DATA_FORMAT_ASTC_6x6_SRGB_BLOCK);
  5333. BIND_ENUM_CONSTANT(DATA_FORMAT_ASTC_8x5_UNORM_BLOCK);
  5334. BIND_ENUM_CONSTANT(DATA_FORMAT_ASTC_8x5_SRGB_BLOCK);
  5335. BIND_ENUM_CONSTANT(DATA_FORMAT_ASTC_8x6_UNORM_BLOCK);
  5336. BIND_ENUM_CONSTANT(DATA_FORMAT_ASTC_8x6_SRGB_BLOCK);
  5337. BIND_ENUM_CONSTANT(DATA_FORMAT_ASTC_8x8_UNORM_BLOCK);
  5338. BIND_ENUM_CONSTANT(DATA_FORMAT_ASTC_8x8_SRGB_BLOCK);
  5339. BIND_ENUM_CONSTANT(DATA_FORMAT_ASTC_10x5_UNORM_BLOCK);
  5340. BIND_ENUM_CONSTANT(DATA_FORMAT_ASTC_10x5_SRGB_BLOCK);
  5341. BIND_ENUM_CONSTANT(DATA_FORMAT_ASTC_10x6_UNORM_BLOCK);
  5342. BIND_ENUM_CONSTANT(DATA_FORMAT_ASTC_10x6_SRGB_BLOCK);
  5343. BIND_ENUM_CONSTANT(DATA_FORMAT_ASTC_10x8_UNORM_BLOCK);
  5344. BIND_ENUM_CONSTANT(DATA_FORMAT_ASTC_10x8_SRGB_BLOCK);
  5345. BIND_ENUM_CONSTANT(DATA_FORMAT_ASTC_10x10_UNORM_BLOCK);
  5346. BIND_ENUM_CONSTANT(DATA_FORMAT_ASTC_10x10_SRGB_BLOCK);
  5347. BIND_ENUM_CONSTANT(DATA_FORMAT_ASTC_12x10_UNORM_BLOCK);
  5348. BIND_ENUM_CONSTANT(DATA_FORMAT_ASTC_12x10_SRGB_BLOCK);
  5349. BIND_ENUM_CONSTANT(DATA_FORMAT_ASTC_12x12_UNORM_BLOCK);
  5350. BIND_ENUM_CONSTANT(DATA_FORMAT_ASTC_12x12_SRGB_BLOCK);
  5351. BIND_ENUM_CONSTANT(DATA_FORMAT_G8B8G8R8_422_UNORM);
  5352. BIND_ENUM_CONSTANT(DATA_FORMAT_B8G8R8G8_422_UNORM);
  5353. BIND_ENUM_CONSTANT(DATA_FORMAT_G8_B8_R8_3PLANE_420_UNORM);
  5354. BIND_ENUM_CONSTANT(DATA_FORMAT_G8_B8R8_2PLANE_420_UNORM);
  5355. BIND_ENUM_CONSTANT(DATA_FORMAT_G8_B8_R8_3PLANE_422_UNORM);
  5356. BIND_ENUM_CONSTANT(DATA_FORMAT_G8_B8R8_2PLANE_422_UNORM);
  5357. BIND_ENUM_CONSTANT(DATA_FORMAT_G8_B8_R8_3PLANE_444_UNORM);
  5358. BIND_ENUM_CONSTANT(DATA_FORMAT_R10X6_UNORM_PACK16);
  5359. BIND_ENUM_CONSTANT(DATA_FORMAT_R10X6G10X6_UNORM_2PACK16);
  5360. BIND_ENUM_CONSTANT(DATA_FORMAT_R10X6G10X6B10X6A10X6_UNORM_4PACK16);
  5361. BIND_ENUM_CONSTANT(DATA_FORMAT_G10X6B10X6G10X6R10X6_422_UNORM_4PACK16);
  5362. BIND_ENUM_CONSTANT(DATA_FORMAT_B10X6G10X6R10X6G10X6_422_UNORM_4PACK16);
  5363. BIND_ENUM_CONSTANT(DATA_FORMAT_G10X6_B10X6_R10X6_3PLANE_420_UNORM_3PACK16);
  5364. BIND_ENUM_CONSTANT(DATA_FORMAT_G10X6_B10X6R10X6_2PLANE_420_UNORM_3PACK16);
  5365. BIND_ENUM_CONSTANT(DATA_FORMAT_G10X6_B10X6_R10X6_3PLANE_422_UNORM_3PACK16);
  5366. BIND_ENUM_CONSTANT(DATA_FORMAT_G10X6_B10X6R10X6_2PLANE_422_UNORM_3PACK16);
  5367. BIND_ENUM_CONSTANT(DATA_FORMAT_G10X6_B10X6_R10X6_3PLANE_444_UNORM_3PACK16);
  5368. BIND_ENUM_CONSTANT(DATA_FORMAT_R12X4_UNORM_PACK16);
  5369. BIND_ENUM_CONSTANT(DATA_FORMAT_R12X4G12X4_UNORM_2PACK16);
  5370. BIND_ENUM_CONSTANT(DATA_FORMAT_R12X4G12X4B12X4A12X4_UNORM_4PACK16);
  5371. BIND_ENUM_CONSTANT(DATA_FORMAT_G12X4B12X4G12X4R12X4_422_UNORM_4PACK16);
  5372. BIND_ENUM_CONSTANT(DATA_FORMAT_B12X4G12X4R12X4G12X4_422_UNORM_4PACK16);
  5373. BIND_ENUM_CONSTANT(DATA_FORMAT_G12X4_B12X4_R12X4_3PLANE_420_UNORM_3PACK16);
  5374. BIND_ENUM_CONSTANT(DATA_FORMAT_G12X4_B12X4R12X4_2PLANE_420_UNORM_3PACK16);
  5375. BIND_ENUM_CONSTANT(DATA_FORMAT_G12X4_B12X4_R12X4_3PLANE_422_UNORM_3PACK16);
  5376. BIND_ENUM_CONSTANT(DATA_FORMAT_G12X4_B12X4R12X4_2PLANE_422_UNORM_3PACK16);
  5377. BIND_ENUM_CONSTANT(DATA_FORMAT_G12X4_B12X4_R12X4_3PLANE_444_UNORM_3PACK16);
  5378. BIND_ENUM_CONSTANT(DATA_FORMAT_G16B16G16R16_422_UNORM);
  5379. BIND_ENUM_CONSTANT(DATA_FORMAT_B16G16R16G16_422_UNORM);
  5380. BIND_ENUM_CONSTANT(DATA_FORMAT_G16_B16_R16_3PLANE_420_UNORM);
  5381. BIND_ENUM_CONSTANT(DATA_FORMAT_G16_B16R16_2PLANE_420_UNORM);
  5382. BIND_ENUM_CONSTANT(DATA_FORMAT_G16_B16_R16_3PLANE_422_UNORM);
  5383. BIND_ENUM_CONSTANT(DATA_FORMAT_G16_B16R16_2PLANE_422_UNORM);
  5384. BIND_ENUM_CONSTANT(DATA_FORMAT_G16_B16_R16_3PLANE_444_UNORM);
  5385. BIND_ENUM_CONSTANT(DATA_FORMAT_MAX);
  5386. BIND_BITFIELD_FLAG(BARRIER_MASK_VERTEX);
  5387. BIND_BITFIELD_FLAG(BARRIER_MASK_FRAGMENT);
  5388. BIND_BITFIELD_FLAG(BARRIER_MASK_COMPUTE);
  5389. BIND_BITFIELD_FLAG(BARRIER_MASK_TRANSFER);
  5390. BIND_BITFIELD_FLAG(BARRIER_MASK_RASTER);
  5391. BIND_BITFIELD_FLAG(BARRIER_MASK_ALL_BARRIERS);
  5392. BIND_BITFIELD_FLAG(BARRIER_MASK_NO_BARRIER);
  5393. BIND_ENUM_CONSTANT(TEXTURE_TYPE_1D);
  5394. BIND_ENUM_CONSTANT(TEXTURE_TYPE_2D);
  5395. BIND_ENUM_CONSTANT(TEXTURE_TYPE_3D);
  5396. BIND_ENUM_CONSTANT(TEXTURE_TYPE_CUBE);
  5397. BIND_ENUM_CONSTANT(TEXTURE_TYPE_1D_ARRAY);
  5398. BIND_ENUM_CONSTANT(TEXTURE_TYPE_2D_ARRAY);
  5399. BIND_ENUM_CONSTANT(TEXTURE_TYPE_CUBE_ARRAY);
  5400. BIND_ENUM_CONSTANT(TEXTURE_TYPE_MAX);
  5401. BIND_ENUM_CONSTANT(TEXTURE_SAMPLES_1);
  5402. BIND_ENUM_CONSTANT(TEXTURE_SAMPLES_2);
  5403. BIND_ENUM_CONSTANT(TEXTURE_SAMPLES_4);
  5404. BIND_ENUM_CONSTANT(TEXTURE_SAMPLES_8);
  5405. BIND_ENUM_CONSTANT(TEXTURE_SAMPLES_16);
  5406. BIND_ENUM_CONSTANT(TEXTURE_SAMPLES_32);
  5407. BIND_ENUM_CONSTANT(TEXTURE_SAMPLES_64);
  5408. BIND_ENUM_CONSTANT(TEXTURE_SAMPLES_MAX);
  5409. BIND_BITFIELD_FLAG(TEXTURE_USAGE_SAMPLING_BIT);
  5410. BIND_BITFIELD_FLAG(TEXTURE_USAGE_COLOR_ATTACHMENT_BIT);
  5411. BIND_BITFIELD_FLAG(TEXTURE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT);
  5412. BIND_BITFIELD_FLAG(TEXTURE_USAGE_STORAGE_BIT);
  5413. BIND_BITFIELD_FLAG(TEXTURE_USAGE_STORAGE_ATOMIC_BIT);
  5414. BIND_BITFIELD_FLAG(TEXTURE_USAGE_CPU_READ_BIT);
  5415. BIND_BITFIELD_FLAG(TEXTURE_USAGE_CAN_UPDATE_BIT);
  5416. BIND_BITFIELD_FLAG(TEXTURE_USAGE_CAN_COPY_FROM_BIT);
  5417. BIND_BITFIELD_FLAG(TEXTURE_USAGE_CAN_COPY_TO_BIT);
  5418. BIND_BITFIELD_FLAG(TEXTURE_USAGE_INPUT_ATTACHMENT_BIT);
  5419. BIND_ENUM_CONSTANT(TEXTURE_SWIZZLE_IDENTITY);
  5420. BIND_ENUM_CONSTANT(TEXTURE_SWIZZLE_ZERO);
  5421. BIND_ENUM_CONSTANT(TEXTURE_SWIZZLE_ONE);
  5422. BIND_ENUM_CONSTANT(TEXTURE_SWIZZLE_R);
  5423. BIND_ENUM_CONSTANT(TEXTURE_SWIZZLE_G);
  5424. BIND_ENUM_CONSTANT(TEXTURE_SWIZZLE_B);
  5425. BIND_ENUM_CONSTANT(TEXTURE_SWIZZLE_A);
  5426. BIND_ENUM_CONSTANT(TEXTURE_SWIZZLE_MAX);
  5427. BIND_ENUM_CONSTANT(TEXTURE_SLICE_2D);
  5428. BIND_ENUM_CONSTANT(TEXTURE_SLICE_CUBEMAP);
  5429. BIND_ENUM_CONSTANT(TEXTURE_SLICE_3D);
  5430. BIND_ENUM_CONSTANT(SAMPLER_FILTER_NEAREST);
  5431. BIND_ENUM_CONSTANT(SAMPLER_FILTER_LINEAR);
  5432. BIND_ENUM_CONSTANT(SAMPLER_REPEAT_MODE_REPEAT);
  5433. BIND_ENUM_CONSTANT(SAMPLER_REPEAT_MODE_MIRRORED_REPEAT);
  5434. BIND_ENUM_CONSTANT(SAMPLER_REPEAT_MODE_CLAMP_TO_EDGE);
  5435. BIND_ENUM_CONSTANT(SAMPLER_REPEAT_MODE_CLAMP_TO_BORDER);
  5436. BIND_ENUM_CONSTANT(SAMPLER_REPEAT_MODE_MIRROR_CLAMP_TO_EDGE);
  5437. BIND_ENUM_CONSTANT(SAMPLER_REPEAT_MODE_MAX);
  5438. BIND_ENUM_CONSTANT(SAMPLER_BORDER_COLOR_FLOAT_TRANSPARENT_BLACK);
  5439. BIND_ENUM_CONSTANT(SAMPLER_BORDER_COLOR_INT_TRANSPARENT_BLACK);
  5440. BIND_ENUM_CONSTANT(SAMPLER_BORDER_COLOR_FLOAT_OPAQUE_BLACK);
  5441. BIND_ENUM_CONSTANT(SAMPLER_BORDER_COLOR_INT_OPAQUE_BLACK);
  5442. BIND_ENUM_CONSTANT(SAMPLER_BORDER_COLOR_FLOAT_OPAQUE_WHITE);
  5443. BIND_ENUM_CONSTANT(SAMPLER_BORDER_COLOR_INT_OPAQUE_WHITE);
  5444. BIND_ENUM_CONSTANT(SAMPLER_BORDER_COLOR_MAX);
  5445. BIND_ENUM_CONSTANT(VERTEX_FREQUENCY_VERTEX);
  5446. BIND_ENUM_CONSTANT(VERTEX_FREQUENCY_INSTANCE);
  5447. BIND_ENUM_CONSTANT(INDEX_BUFFER_FORMAT_UINT16);
  5448. BIND_ENUM_CONSTANT(INDEX_BUFFER_FORMAT_UINT32);
  5449. BIND_BITFIELD_FLAG(STORAGE_BUFFER_USAGE_DISPATCH_INDIRECT);
  5450. BIND_ENUM_CONSTANT(UNIFORM_TYPE_SAMPLER); //for sampling only (sampler GLSL type)
  5451. BIND_ENUM_CONSTANT(UNIFORM_TYPE_SAMPLER_WITH_TEXTURE); // for sampling only); but includes a texture); (samplerXX GLSL type)); first a sampler then a texture
  5452. BIND_ENUM_CONSTANT(UNIFORM_TYPE_TEXTURE); //only texture); (textureXX GLSL type)
  5453. BIND_ENUM_CONSTANT(UNIFORM_TYPE_IMAGE); // storage image (imageXX GLSL type)); for compute mostly
  5454. BIND_ENUM_CONSTANT(UNIFORM_TYPE_TEXTURE_BUFFER); // buffer texture (or TBO); textureBuffer type)
  5455. BIND_ENUM_CONSTANT(UNIFORM_TYPE_SAMPLER_WITH_TEXTURE_BUFFER); // buffer texture with a sampler(or TBO); samplerBuffer type)
  5456. BIND_ENUM_CONSTANT(UNIFORM_TYPE_IMAGE_BUFFER); //texel buffer); (imageBuffer type)); for compute mostly
  5457. BIND_ENUM_CONSTANT(UNIFORM_TYPE_UNIFORM_BUFFER); //regular uniform buffer (or UBO).
  5458. BIND_ENUM_CONSTANT(UNIFORM_TYPE_STORAGE_BUFFER); //storage buffer ("buffer" qualifier) like UBO); but supports storage); for compute mostly
  5459. BIND_ENUM_CONSTANT(UNIFORM_TYPE_INPUT_ATTACHMENT); //used for sub-pass read/write); for mobile mostly
  5460. BIND_ENUM_CONSTANT(UNIFORM_TYPE_MAX);
  5461. BIND_ENUM_CONSTANT(RENDER_PRIMITIVE_POINTS);
  5462. BIND_ENUM_CONSTANT(RENDER_PRIMITIVE_LINES);
  5463. BIND_ENUM_CONSTANT(RENDER_PRIMITIVE_LINES_WITH_ADJACENCY);
  5464. BIND_ENUM_CONSTANT(RENDER_PRIMITIVE_LINESTRIPS);
  5465. BIND_ENUM_CONSTANT(RENDER_PRIMITIVE_LINESTRIPS_WITH_ADJACENCY);
  5466. BIND_ENUM_CONSTANT(RENDER_PRIMITIVE_TRIANGLES);
  5467. BIND_ENUM_CONSTANT(RENDER_PRIMITIVE_TRIANGLES_WITH_ADJACENCY);
  5468. BIND_ENUM_CONSTANT(RENDER_PRIMITIVE_TRIANGLE_STRIPS);
  5469. BIND_ENUM_CONSTANT(RENDER_PRIMITIVE_TRIANGLE_STRIPS_WITH_AJACENCY);
  5470. BIND_ENUM_CONSTANT(RENDER_PRIMITIVE_TRIANGLE_STRIPS_WITH_RESTART_INDEX);
  5471. BIND_ENUM_CONSTANT(RENDER_PRIMITIVE_TESSELATION_PATCH);
  5472. BIND_ENUM_CONSTANT(RENDER_PRIMITIVE_MAX);
  5473. BIND_ENUM_CONSTANT(POLYGON_CULL_DISABLED);
  5474. BIND_ENUM_CONSTANT(POLYGON_CULL_FRONT);
  5475. BIND_ENUM_CONSTANT(POLYGON_CULL_BACK);
  5476. BIND_ENUM_CONSTANT(POLYGON_FRONT_FACE_CLOCKWISE);
  5477. BIND_ENUM_CONSTANT(POLYGON_FRONT_FACE_COUNTER_CLOCKWISE);
  5478. BIND_ENUM_CONSTANT(STENCIL_OP_KEEP);
  5479. BIND_ENUM_CONSTANT(STENCIL_OP_ZERO);
  5480. BIND_ENUM_CONSTANT(STENCIL_OP_REPLACE);
  5481. BIND_ENUM_CONSTANT(STENCIL_OP_INCREMENT_AND_CLAMP);
  5482. BIND_ENUM_CONSTANT(STENCIL_OP_DECREMENT_AND_CLAMP);
  5483. BIND_ENUM_CONSTANT(STENCIL_OP_INVERT);
  5484. BIND_ENUM_CONSTANT(STENCIL_OP_INCREMENT_AND_WRAP);
  5485. BIND_ENUM_CONSTANT(STENCIL_OP_DECREMENT_AND_WRAP);
  5486. BIND_ENUM_CONSTANT(STENCIL_OP_MAX); //not an actual operator); just the amount of operators :D
  5487. BIND_ENUM_CONSTANT(COMPARE_OP_NEVER);
  5488. BIND_ENUM_CONSTANT(COMPARE_OP_LESS);
  5489. BIND_ENUM_CONSTANT(COMPARE_OP_EQUAL);
  5490. BIND_ENUM_CONSTANT(COMPARE_OP_LESS_OR_EQUAL);
  5491. BIND_ENUM_CONSTANT(COMPARE_OP_GREATER);
  5492. BIND_ENUM_CONSTANT(COMPARE_OP_NOT_EQUAL);
  5493. BIND_ENUM_CONSTANT(COMPARE_OP_GREATER_OR_EQUAL);
  5494. BIND_ENUM_CONSTANT(COMPARE_OP_ALWAYS);
  5495. BIND_ENUM_CONSTANT(COMPARE_OP_MAX);
  5496. BIND_ENUM_CONSTANT(LOGIC_OP_CLEAR);
  5497. BIND_ENUM_CONSTANT(LOGIC_OP_AND);
  5498. BIND_ENUM_CONSTANT(LOGIC_OP_AND_REVERSE);
  5499. BIND_ENUM_CONSTANT(LOGIC_OP_COPY);
  5500. BIND_ENUM_CONSTANT(LOGIC_OP_AND_INVERTED);
  5501. BIND_ENUM_CONSTANT(LOGIC_OP_NO_OP);
  5502. BIND_ENUM_CONSTANT(LOGIC_OP_XOR);
  5503. BIND_ENUM_CONSTANT(LOGIC_OP_OR);
  5504. BIND_ENUM_CONSTANT(LOGIC_OP_NOR);
  5505. BIND_ENUM_CONSTANT(LOGIC_OP_EQUIVALENT);
  5506. BIND_ENUM_CONSTANT(LOGIC_OP_INVERT);
  5507. BIND_ENUM_CONSTANT(LOGIC_OP_OR_REVERSE);
  5508. BIND_ENUM_CONSTANT(LOGIC_OP_COPY_INVERTED);
  5509. BIND_ENUM_CONSTANT(LOGIC_OP_OR_INVERTED);
  5510. BIND_ENUM_CONSTANT(LOGIC_OP_NAND);
  5511. BIND_ENUM_CONSTANT(LOGIC_OP_SET);
  5512. BIND_ENUM_CONSTANT(LOGIC_OP_MAX); //not an actual operator); just the amount of operators :D
  5513. BIND_ENUM_CONSTANT(BLEND_FACTOR_ZERO);
  5514. BIND_ENUM_CONSTANT(BLEND_FACTOR_ONE);
  5515. BIND_ENUM_CONSTANT(BLEND_FACTOR_SRC_COLOR);
  5516. BIND_ENUM_CONSTANT(BLEND_FACTOR_ONE_MINUS_SRC_COLOR);
  5517. BIND_ENUM_CONSTANT(BLEND_FACTOR_DST_COLOR);
  5518. BIND_ENUM_CONSTANT(BLEND_FACTOR_ONE_MINUS_DST_COLOR);
  5519. BIND_ENUM_CONSTANT(BLEND_FACTOR_SRC_ALPHA);
  5520. BIND_ENUM_CONSTANT(BLEND_FACTOR_ONE_MINUS_SRC_ALPHA);
  5521. BIND_ENUM_CONSTANT(BLEND_FACTOR_DST_ALPHA);
  5522. BIND_ENUM_CONSTANT(BLEND_FACTOR_ONE_MINUS_DST_ALPHA);
  5523. BIND_ENUM_CONSTANT(BLEND_FACTOR_CONSTANT_COLOR);
  5524. BIND_ENUM_CONSTANT(BLEND_FACTOR_ONE_MINUS_CONSTANT_COLOR);
  5525. BIND_ENUM_CONSTANT(BLEND_FACTOR_CONSTANT_ALPHA);
  5526. BIND_ENUM_CONSTANT(BLEND_FACTOR_ONE_MINUS_CONSTANT_ALPHA);
  5527. BIND_ENUM_CONSTANT(BLEND_FACTOR_SRC_ALPHA_SATURATE);
  5528. BIND_ENUM_CONSTANT(BLEND_FACTOR_SRC1_COLOR);
  5529. BIND_ENUM_CONSTANT(BLEND_FACTOR_ONE_MINUS_SRC1_COLOR);
  5530. BIND_ENUM_CONSTANT(BLEND_FACTOR_SRC1_ALPHA);
  5531. BIND_ENUM_CONSTANT(BLEND_FACTOR_ONE_MINUS_SRC1_ALPHA);
  5532. BIND_ENUM_CONSTANT(BLEND_FACTOR_MAX);
  5533. BIND_ENUM_CONSTANT(BLEND_OP_ADD);
  5534. BIND_ENUM_CONSTANT(BLEND_OP_SUBTRACT);
  5535. BIND_ENUM_CONSTANT(BLEND_OP_REVERSE_SUBTRACT);
  5536. BIND_ENUM_CONSTANT(BLEND_OP_MINIMUM);
  5537. BIND_ENUM_CONSTANT(BLEND_OP_MAXIMUM);
  5538. BIND_ENUM_CONSTANT(BLEND_OP_MAX);
  5539. BIND_BITFIELD_FLAG(DYNAMIC_STATE_LINE_WIDTH);
  5540. BIND_BITFIELD_FLAG(DYNAMIC_STATE_DEPTH_BIAS);
  5541. BIND_BITFIELD_FLAG(DYNAMIC_STATE_BLEND_CONSTANTS);
  5542. BIND_BITFIELD_FLAG(DYNAMIC_STATE_DEPTH_BOUNDS);
  5543. BIND_BITFIELD_FLAG(DYNAMIC_STATE_STENCIL_COMPARE_MASK);
  5544. BIND_BITFIELD_FLAG(DYNAMIC_STATE_STENCIL_WRITE_MASK);
  5545. BIND_BITFIELD_FLAG(DYNAMIC_STATE_STENCIL_REFERENCE);
  5546. BIND_ENUM_CONSTANT(INITIAL_ACTION_CLEAR); //start rendering and clear the framebuffer (supply params)
  5547. BIND_ENUM_CONSTANT(INITIAL_ACTION_CLEAR_REGION); //start rendering and clear the framebuffer (supply params)
  5548. BIND_ENUM_CONSTANT(INITIAL_ACTION_CLEAR_REGION_CONTINUE); //continue rendering and clear the framebuffer (supply params)
  5549. BIND_ENUM_CONSTANT(INITIAL_ACTION_KEEP); //start rendering); but keep attached color texture contents (depth will be cleared)
  5550. BIND_ENUM_CONSTANT(INITIAL_ACTION_DROP); //start rendering); ignore what is there); just write above it
  5551. BIND_ENUM_CONSTANT(INITIAL_ACTION_CONTINUE); //continue rendering (framebuffer must have been left in "continue" state as final action previously)
  5552. BIND_ENUM_CONSTANT(INITIAL_ACTION_MAX);
  5553. BIND_ENUM_CONSTANT(FINAL_ACTION_READ); //will no longer render to it); allows attached textures to be read again); but depth buffer contents will be dropped (Can't be read from)
  5554. BIND_ENUM_CONSTANT(FINAL_ACTION_DISCARD); // discard contents after rendering
  5555. BIND_ENUM_CONSTANT(FINAL_ACTION_CONTINUE); //will continue rendering later); attached textures can't be read until re-bound with "finish"
  5556. BIND_ENUM_CONSTANT(FINAL_ACTION_MAX);
  5557. BIND_ENUM_CONSTANT(SHADER_STAGE_VERTEX);
  5558. BIND_ENUM_CONSTANT(SHADER_STAGE_FRAGMENT);
  5559. BIND_ENUM_CONSTANT(SHADER_STAGE_TESSELATION_CONTROL);
  5560. BIND_ENUM_CONSTANT(SHADER_STAGE_TESSELATION_EVALUATION);
  5561. BIND_ENUM_CONSTANT(SHADER_STAGE_COMPUTE);
  5562. BIND_ENUM_CONSTANT(SHADER_STAGE_MAX);
  5563. BIND_ENUM_CONSTANT(SHADER_STAGE_VERTEX_BIT);
  5564. BIND_ENUM_CONSTANT(SHADER_STAGE_FRAGMENT_BIT);
  5565. BIND_ENUM_CONSTANT(SHADER_STAGE_TESSELATION_CONTROL_BIT);
  5566. BIND_ENUM_CONSTANT(SHADER_STAGE_TESSELATION_EVALUATION_BIT);
  5567. BIND_ENUM_CONSTANT(SHADER_STAGE_COMPUTE_BIT);
  5568. BIND_ENUM_CONSTANT(SHADER_LANGUAGE_GLSL);
  5569. BIND_ENUM_CONSTANT(SHADER_LANGUAGE_HLSL);
  5570. BIND_ENUM_CONSTANT(PIPELINE_SPECIALIZATION_CONSTANT_TYPE_BOOL);
  5571. BIND_ENUM_CONSTANT(PIPELINE_SPECIALIZATION_CONSTANT_TYPE_INT);
  5572. BIND_ENUM_CONSTANT(PIPELINE_SPECIALIZATION_CONSTANT_TYPE_FLOAT);
  5573. BIND_ENUM_CONSTANT(LIMIT_MAX_BOUND_UNIFORM_SETS);
  5574. BIND_ENUM_CONSTANT(LIMIT_MAX_FRAMEBUFFER_COLOR_ATTACHMENTS);
  5575. BIND_ENUM_CONSTANT(LIMIT_MAX_TEXTURES_PER_UNIFORM_SET);
  5576. BIND_ENUM_CONSTANT(LIMIT_MAX_SAMPLERS_PER_UNIFORM_SET);
  5577. BIND_ENUM_CONSTANT(LIMIT_MAX_STORAGE_BUFFERS_PER_UNIFORM_SET);
  5578. BIND_ENUM_CONSTANT(LIMIT_MAX_STORAGE_IMAGES_PER_UNIFORM_SET);
  5579. BIND_ENUM_CONSTANT(LIMIT_MAX_UNIFORM_BUFFERS_PER_UNIFORM_SET);
  5580. BIND_ENUM_CONSTANT(LIMIT_MAX_DRAW_INDEXED_INDEX);
  5581. BIND_ENUM_CONSTANT(LIMIT_MAX_FRAMEBUFFER_HEIGHT);
  5582. BIND_ENUM_CONSTANT(LIMIT_MAX_FRAMEBUFFER_WIDTH);
  5583. BIND_ENUM_CONSTANT(LIMIT_MAX_TEXTURE_ARRAY_LAYERS);
  5584. BIND_ENUM_CONSTANT(LIMIT_MAX_TEXTURE_SIZE_1D);
  5585. BIND_ENUM_CONSTANT(LIMIT_MAX_TEXTURE_SIZE_2D);
  5586. BIND_ENUM_CONSTANT(LIMIT_MAX_TEXTURE_SIZE_3D);
  5587. BIND_ENUM_CONSTANT(LIMIT_MAX_TEXTURE_SIZE_CUBE);
  5588. BIND_ENUM_CONSTANT(LIMIT_MAX_TEXTURES_PER_SHADER_STAGE);
  5589. BIND_ENUM_CONSTANT(LIMIT_MAX_SAMPLERS_PER_SHADER_STAGE);
  5590. BIND_ENUM_CONSTANT(LIMIT_MAX_STORAGE_BUFFERS_PER_SHADER_STAGE);
  5591. BIND_ENUM_CONSTANT(LIMIT_MAX_STORAGE_IMAGES_PER_SHADER_STAGE);
  5592. BIND_ENUM_CONSTANT(LIMIT_MAX_UNIFORM_BUFFERS_PER_SHADER_STAGE);
  5593. BIND_ENUM_CONSTANT(LIMIT_MAX_PUSH_CONSTANT_SIZE);
  5594. BIND_ENUM_CONSTANT(LIMIT_MAX_UNIFORM_BUFFER_SIZE);
  5595. BIND_ENUM_CONSTANT(LIMIT_MAX_VERTEX_INPUT_ATTRIBUTE_OFFSET);
  5596. BIND_ENUM_CONSTANT(LIMIT_MAX_VERTEX_INPUT_ATTRIBUTES);
  5597. BIND_ENUM_CONSTANT(LIMIT_MAX_VERTEX_INPUT_BINDINGS);
  5598. BIND_ENUM_CONSTANT(LIMIT_MAX_VERTEX_INPUT_BINDING_STRIDE);
  5599. BIND_ENUM_CONSTANT(LIMIT_MIN_UNIFORM_BUFFER_OFFSET_ALIGNMENT);
  5600. BIND_ENUM_CONSTANT(LIMIT_MAX_COMPUTE_SHARED_MEMORY_SIZE);
  5601. BIND_ENUM_CONSTANT(LIMIT_MAX_COMPUTE_WORKGROUP_COUNT_X);
  5602. BIND_ENUM_CONSTANT(LIMIT_MAX_COMPUTE_WORKGROUP_COUNT_Y);
  5603. BIND_ENUM_CONSTANT(LIMIT_MAX_COMPUTE_WORKGROUP_COUNT_Z);
  5604. BIND_ENUM_CONSTANT(LIMIT_MAX_COMPUTE_WORKGROUP_INVOCATIONS);
  5605. BIND_ENUM_CONSTANT(LIMIT_MAX_COMPUTE_WORKGROUP_SIZE_X);
  5606. BIND_ENUM_CONSTANT(LIMIT_MAX_COMPUTE_WORKGROUP_SIZE_Y);
  5607. BIND_ENUM_CONSTANT(LIMIT_MAX_COMPUTE_WORKGROUP_SIZE_Z);
  5608. BIND_ENUM_CONSTANT(LIMIT_MAX_VIEWPORT_DIMENSIONS_X);
  5609. BIND_ENUM_CONSTANT(LIMIT_MAX_VIEWPORT_DIMENSIONS_Y);
  5610. BIND_ENUM_CONSTANT(MEMORY_TEXTURES);
  5611. BIND_ENUM_CONSTANT(MEMORY_BUFFERS);
  5612. BIND_ENUM_CONSTANT(MEMORY_TOTAL);
  5613. BIND_CONSTANT(INVALID_ID);
  5614. BIND_CONSTANT(INVALID_FORMAT_ID);
  5615. }
  5616. RenderingDevice::~RenderingDevice() {
  5617. if (local_device.is_valid()) {
  5618. finalize();
  5619. context->local_device_free(local_device);
  5620. }
  5621. if (singleton == this) {
  5622. singleton = nullptr;
  5623. }
  5624. }
  5625. RenderingDevice::RenderingDevice() {
  5626. if (singleton == nullptr) { // there may be more rendering devices later
  5627. singleton = this;
  5628. }
  5629. }
  5630. /*****************/
  5631. /**** BINDERS ****/
  5632. /*****************/
  5633. RID RenderingDevice::_texture_create(const Ref<RDTextureFormat> &p_format, const Ref<RDTextureView> &p_view, const TypedArray<PackedByteArray> &p_data) {
  5634. ERR_FAIL_COND_V(p_format.is_null(), RID());
  5635. ERR_FAIL_COND_V(p_view.is_null(), RID());
  5636. Vector<Vector<uint8_t>> data;
  5637. for (int i = 0; i < p_data.size(); i++) {
  5638. Vector<uint8_t> byte_slice = p_data[i];
  5639. ERR_FAIL_COND_V(byte_slice.is_empty(), RID());
  5640. data.push_back(byte_slice);
  5641. }
  5642. return texture_create(p_format->base, p_view->base, data);
  5643. }
  5644. RID RenderingDevice::_texture_create_shared(const Ref<RDTextureView> &p_view, RID p_with_texture) {
  5645. ERR_FAIL_COND_V(p_view.is_null(), RID());
  5646. return texture_create_shared(p_view->base, p_with_texture);
  5647. }
  5648. RID RenderingDevice::_texture_create_shared_from_slice(const Ref<RDTextureView> &p_view, RID p_with_texture, uint32_t p_layer, uint32_t p_mipmap, uint32_t p_mipmaps, TextureSliceType p_slice_type) {
  5649. ERR_FAIL_COND_V(p_view.is_null(), RID());
  5650. return texture_create_shared_from_slice(p_view->base, p_with_texture, p_layer, p_mipmap, p_mipmaps, p_slice_type);
  5651. }
  5652. Ref<RDTextureFormat> RenderingDevice::_texture_get_format(RID p_rd_texture) {
  5653. Ref<RDTextureFormat> rtf;
  5654. rtf.instantiate();
  5655. rtf->base = texture_get_format(p_rd_texture);
  5656. return rtf;
  5657. }
  5658. RenderingDevice::FramebufferFormatID RenderingDevice::_framebuffer_format_create(const TypedArray<RDAttachmentFormat> &p_attachments, uint32_t p_view_count) {
  5659. Vector<AttachmentFormat> attachments;
  5660. attachments.resize(p_attachments.size());
  5661. for (int i = 0; i < p_attachments.size(); i++) {
  5662. Ref<RDAttachmentFormat> af = p_attachments[i];
  5663. ERR_FAIL_COND_V(af.is_null(), INVALID_FORMAT_ID);
  5664. attachments.write[i] = af->base;
  5665. }
  5666. return framebuffer_format_create(attachments, p_view_count);
  5667. }
  5668. RenderingDevice::FramebufferFormatID RenderingDevice::_framebuffer_format_create_multipass(const TypedArray<RDAttachmentFormat> &p_attachments, const TypedArray<RDFramebufferPass> &p_passes, uint32_t p_view_count) {
  5669. Vector<AttachmentFormat> attachments;
  5670. attachments.resize(p_attachments.size());
  5671. for (int i = 0; i < p_attachments.size(); i++) {
  5672. Ref<RDAttachmentFormat> af = p_attachments[i];
  5673. ERR_FAIL_COND_V(af.is_null(), INVALID_FORMAT_ID);
  5674. attachments.write[i] = af->base;
  5675. }
  5676. Vector<FramebufferPass> passes;
  5677. for (int i = 0; i < p_passes.size(); i++) {
  5678. Ref<RDFramebufferPass> pass = p_passes[i];
  5679. ERR_CONTINUE(pass.is_null());
  5680. passes.push_back(pass->base);
  5681. }
  5682. return framebuffer_format_create_multipass(attachments, passes, p_view_count);
  5683. }
  5684. RID RenderingDevice::_framebuffer_create(const TypedArray<RID> &p_textures, FramebufferFormatID p_format_check, uint32_t p_view_count) {
  5685. Vector<RID> textures = Variant(p_textures);
  5686. return framebuffer_create(textures, p_format_check, p_view_count);
  5687. }
  5688. RID RenderingDevice::_framebuffer_create_multipass(const TypedArray<RID> &p_textures, const TypedArray<RDFramebufferPass> &p_passes, FramebufferFormatID p_format_check, uint32_t p_view_count) {
  5689. Vector<RID> textures = Variant(p_textures);
  5690. Vector<FramebufferPass> passes;
  5691. for (int i = 0; i < p_passes.size(); i++) {
  5692. Ref<RDFramebufferPass> pass = p_passes[i];
  5693. ERR_CONTINUE(pass.is_null());
  5694. passes.push_back(pass->base);
  5695. }
  5696. return framebuffer_create_multipass(textures, passes, p_format_check, p_view_count);
  5697. }
  5698. RID RenderingDevice::_sampler_create(const Ref<RDSamplerState> &p_state) {
  5699. ERR_FAIL_COND_V(p_state.is_null(), RID());
  5700. return sampler_create(p_state->base);
  5701. }
  5702. RenderingDevice::VertexFormatID RenderingDevice::_vertex_format_create(const TypedArray<RDVertexAttribute> &p_vertex_formats) {
  5703. Vector<VertexAttribute> descriptions;
  5704. descriptions.resize(p_vertex_formats.size());
  5705. for (int i = 0; i < p_vertex_formats.size(); i++) {
  5706. Ref<RDVertexAttribute> af = p_vertex_formats[i];
  5707. ERR_FAIL_COND_V(af.is_null(), INVALID_FORMAT_ID);
  5708. descriptions.write[i] = af->base;
  5709. }
  5710. return vertex_format_create(descriptions);
  5711. }
  5712. RID RenderingDevice::_vertex_array_create(uint32_t p_vertex_count, VertexFormatID p_vertex_format, const TypedArray<RID> &p_src_buffers, const Vector<int64_t> &p_offsets) {
  5713. Vector<RID> buffers = Variant(p_src_buffers);
  5714. Vector<uint64_t> offsets;
  5715. offsets.resize(p_offsets.size());
  5716. for (int i = 0; i < p_offsets.size(); i++) {
  5717. offsets.write[i] = p_offsets[i];
  5718. }
  5719. return vertex_array_create(p_vertex_count, p_vertex_format, buffers, offsets);
  5720. }
  5721. Ref<RDShaderSPIRV> RenderingDevice::_shader_compile_spirv_from_source(const Ref<RDShaderSource> &p_source, bool p_allow_cache) {
  5722. ERR_FAIL_COND_V(p_source.is_null(), Ref<RDShaderSPIRV>());
  5723. Ref<RDShaderSPIRV> bytecode;
  5724. bytecode.instantiate();
  5725. for (int i = 0; i < RD::SHADER_STAGE_MAX; i++) {
  5726. String error;
  5727. ShaderStage stage = ShaderStage(i);
  5728. String source = p_source->get_stage_source(stage);
  5729. if (!source.is_empty()) {
  5730. Vector<uint8_t> spirv = shader_compile_spirv_from_source(stage, source, p_source->get_language(), &error, p_allow_cache);
  5731. bytecode->set_stage_bytecode(stage, spirv);
  5732. bytecode->set_stage_compile_error(stage, error);
  5733. }
  5734. }
  5735. return bytecode;
  5736. }
  5737. Vector<uint8_t> RenderingDevice::_shader_compile_binary_from_spirv(const Ref<RDShaderSPIRV> &p_spirv, const String &p_shader_name) {
  5738. ERR_FAIL_COND_V(p_spirv.is_null(), Vector<uint8_t>());
  5739. Vector<ShaderStageSPIRVData> stage_data;
  5740. for (int i = 0; i < RD::SHADER_STAGE_MAX; i++) {
  5741. ShaderStage stage = ShaderStage(i);
  5742. ShaderStageSPIRVData sd;
  5743. sd.shader_stage = stage;
  5744. String error = p_spirv->get_stage_compile_error(stage);
  5745. ERR_FAIL_COND_V_MSG(!error.is_empty(), Vector<uint8_t>(), "Can't create a shader from an errored bytecode. Check errors in source bytecode.");
  5746. sd.spirv = p_spirv->get_stage_bytecode(stage);
  5747. if (sd.spirv.is_empty()) {
  5748. continue;
  5749. }
  5750. stage_data.push_back(sd);
  5751. }
  5752. return shader_compile_binary_from_spirv(stage_data, p_shader_name);
  5753. }
  5754. RID RenderingDevice::_shader_create_from_spirv(const Ref<RDShaderSPIRV> &p_spirv, const String &p_shader_name) {
  5755. ERR_FAIL_COND_V(p_spirv.is_null(), RID());
  5756. Vector<ShaderStageSPIRVData> stage_data;
  5757. for (int i = 0; i < RD::SHADER_STAGE_MAX; i++) {
  5758. ShaderStage stage = ShaderStage(i);
  5759. ShaderStageSPIRVData sd;
  5760. sd.shader_stage = stage;
  5761. String error = p_spirv->get_stage_compile_error(stage);
  5762. ERR_FAIL_COND_V_MSG(!error.is_empty(), RID(), "Can't create a shader from an errored bytecode. Check errors in source bytecode.");
  5763. sd.spirv = p_spirv->get_stage_bytecode(stage);
  5764. if (sd.spirv.is_empty()) {
  5765. continue;
  5766. }
  5767. stage_data.push_back(sd);
  5768. }
  5769. return shader_create_from_spirv(stage_data);
  5770. }
  5771. RID RenderingDevice::_uniform_set_create(const TypedArray<RDUniform> &p_uniforms, RID p_shader, uint32_t p_shader_set) {
  5772. Vector<Uniform> uniforms;
  5773. uniforms.resize(p_uniforms.size());
  5774. for (int i = 0; i < p_uniforms.size(); i++) {
  5775. Ref<RDUniform> uniform = p_uniforms[i];
  5776. ERR_FAIL_COND_V(!uniform.is_valid(), RID());
  5777. uniforms.write[i] = uniform->base;
  5778. }
  5779. return uniform_set_create(uniforms, p_shader, p_shader_set);
  5780. }
  5781. Error RenderingDevice::_buffer_update_bind(RID p_buffer, uint32_t p_offset, uint32_t p_size, const Vector<uint8_t> &p_data, BitField<BarrierMask> p_post_barrier) {
  5782. return buffer_update(p_buffer, p_offset, p_size, p_data.ptr(), p_post_barrier);
  5783. }
  5784. static Vector<RenderingDevice::PipelineSpecializationConstant> _get_spec_constants(const TypedArray<RDPipelineSpecializationConstant> &p_constants) {
  5785. Vector<RenderingDevice::PipelineSpecializationConstant> ret;
  5786. ret.resize(p_constants.size());
  5787. for (int i = 0; i < p_constants.size(); i++) {
  5788. Ref<RDPipelineSpecializationConstant> c = p_constants[i];
  5789. ERR_CONTINUE(c.is_null());
  5790. RenderingDevice::PipelineSpecializationConstant &sc = ret.write[i];
  5791. Variant value = c->get_value();
  5792. switch (value.get_type()) {
  5793. case Variant::BOOL: {
  5794. sc.type = RD::PIPELINE_SPECIALIZATION_CONSTANT_TYPE_BOOL;
  5795. sc.bool_value = value;
  5796. } break;
  5797. case Variant::INT: {
  5798. sc.type = RD::PIPELINE_SPECIALIZATION_CONSTANT_TYPE_INT;
  5799. sc.int_value = value;
  5800. } break;
  5801. case Variant::FLOAT: {
  5802. sc.type = RD::PIPELINE_SPECIALIZATION_CONSTANT_TYPE_FLOAT;
  5803. sc.float_value = value;
  5804. } break;
  5805. default: {
  5806. }
  5807. }
  5808. sc.constant_id = c->get_constant_id();
  5809. }
  5810. return ret;
  5811. }
  5812. RID RenderingDevice::_render_pipeline_create(RID p_shader, FramebufferFormatID p_framebuffer_format, VertexFormatID p_vertex_format, RenderPrimitive p_render_primitive, const Ref<RDPipelineRasterizationState> &p_rasterization_state, const Ref<RDPipelineMultisampleState> &p_multisample_state, const Ref<RDPipelineDepthStencilState> &p_depth_stencil_state, const Ref<RDPipelineColorBlendState> &p_blend_state, BitField<PipelineDynamicStateFlags> p_dynamic_state_flags, uint32_t p_for_render_pass, const TypedArray<RDPipelineSpecializationConstant> &p_specialization_constants) {
  5813. PipelineRasterizationState rasterization_state;
  5814. if (p_rasterization_state.is_valid()) {
  5815. rasterization_state = p_rasterization_state->base;
  5816. }
  5817. PipelineMultisampleState multisample_state;
  5818. if (p_multisample_state.is_valid()) {
  5819. multisample_state = p_multisample_state->base;
  5820. for (int i = 0; i < p_multisample_state->sample_masks.size(); i++) {
  5821. int64_t mask = p_multisample_state->sample_masks[i];
  5822. multisample_state.sample_mask.push_back(mask);
  5823. }
  5824. }
  5825. PipelineDepthStencilState depth_stencil_state;
  5826. if (p_depth_stencil_state.is_valid()) {
  5827. depth_stencil_state = p_depth_stencil_state->base;
  5828. }
  5829. PipelineColorBlendState color_blend_state;
  5830. if (p_blend_state.is_valid()) {
  5831. color_blend_state = p_blend_state->base;
  5832. for (int i = 0; i < p_blend_state->attachments.size(); i++) {
  5833. Ref<RDPipelineColorBlendStateAttachment> attachment = p_blend_state->attachments[i];
  5834. if (attachment.is_valid()) {
  5835. color_blend_state.attachments.push_back(attachment->base);
  5836. }
  5837. }
  5838. }
  5839. return render_pipeline_create(p_shader, p_framebuffer_format, p_vertex_format, p_render_primitive, rasterization_state, multisample_state, depth_stencil_state, color_blend_state, p_dynamic_state_flags, p_for_render_pass, _get_spec_constants(p_specialization_constants));
  5840. }
  5841. RID RenderingDevice::_compute_pipeline_create(RID p_shader, const TypedArray<RDPipelineSpecializationConstant> &p_specialization_constants = TypedArray<RDPipelineSpecializationConstant>()) {
  5842. return compute_pipeline_create(p_shader, _get_spec_constants(p_specialization_constants));
  5843. }
  5844. RenderingDevice::DrawListID RenderingDevice::_draw_list_begin(RID p_framebuffer, InitialAction p_initial_color_action, FinalAction p_final_color_action, InitialAction p_initial_depth_action, FinalAction p_final_depth_action, const Vector<Color> &p_clear_color_values, float p_clear_depth, uint32_t p_clear_stencil, const Rect2 &p_region, const TypedArray<RID> &p_storage_textures) {
  5845. Vector<RID> stextures;
  5846. for (int i = 0; i < p_storage_textures.size(); i++) {
  5847. stextures.push_back(p_storage_textures[i]);
  5848. }
  5849. return draw_list_begin(p_framebuffer, p_initial_color_action, p_final_color_action, p_initial_depth_action, p_final_depth_action, p_clear_color_values, p_clear_depth, p_clear_stencil, p_region, stextures);
  5850. }
  5851. Vector<int64_t> RenderingDevice::_draw_list_begin_split(RID p_framebuffer, uint32_t p_splits, InitialAction p_initial_color_action, FinalAction p_final_color_action, InitialAction p_initial_depth_action, FinalAction p_final_depth_action, const Vector<Color> &p_clear_color_values, float p_clear_depth, uint32_t p_clear_stencil, const Rect2 &p_region, const TypedArray<RID> &p_storage_textures) {
  5852. Vector<DrawListID> splits;
  5853. splits.resize(p_splits);
  5854. Vector<RID> stextures;
  5855. for (int i = 0; i < p_storage_textures.size(); i++) {
  5856. stextures.push_back(p_storage_textures[i]);
  5857. }
  5858. draw_list_begin_split(p_framebuffer, p_splits, splits.ptrw(), p_initial_color_action, p_final_color_action, p_initial_depth_action, p_final_depth_action, p_clear_color_values, p_clear_depth, p_clear_stencil, p_region, stextures);
  5859. Vector<int64_t> split_ids;
  5860. split_ids.resize(splits.size());
  5861. for (int i = 0; i < splits.size(); i++) {
  5862. split_ids.write[i] = splits[i];
  5863. }
  5864. return split_ids;
  5865. }
  5866. Vector<int64_t> RenderingDevice::_draw_list_switch_to_next_pass_split(uint32_t p_splits) {
  5867. Vector<DrawListID> splits;
  5868. splits.resize(p_splits);
  5869. Error err = draw_list_switch_to_next_pass_split(p_splits, splits.ptrw());
  5870. ERR_FAIL_COND_V(err != OK, Vector<int64_t>());
  5871. Vector<int64_t> split_ids;
  5872. split_ids.resize(splits.size());
  5873. for (int i = 0; i < splits.size(); i++) {
  5874. split_ids.write[i] = splits[i];
  5875. }
  5876. return split_ids;
  5877. }
  5878. void RenderingDevice::_draw_list_set_push_constant(DrawListID p_list, const Vector<uint8_t> &p_data, uint32_t p_data_size) {
  5879. ERR_FAIL_COND((uint32_t)p_data.size() > p_data_size);
  5880. draw_list_set_push_constant(p_list, p_data.ptr(), p_data_size);
  5881. }
  5882. void RenderingDevice::_compute_list_set_push_constant(ComputeListID p_list, const Vector<uint8_t> &p_data, uint32_t p_data_size) {
  5883. ERR_FAIL_COND((uint32_t)p_data.size() > p_data_size);
  5884. compute_list_set_push_constant(p_list, p_data.ptr(), p_data_size);
  5885. }