rendering_device.cpp 273 KB

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