rendering_device.cpp 272 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404
  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.find(format.format) == -1, 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.find(p_view.format_override) == -1, 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.find(p_view.format_override) == -1, 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.find(p_view.format_override) == -1, 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 w = tex->width;
  1086. uint32_t h = tex->height;
  1087. uint32_t d = tex->depth;
  1088. for (uint32_t i = 0; i < tex->mipmaps; i++) {
  1089. RDD::BufferTextureCopyRegion copy_region;
  1090. copy_region.buffer_offset = mip_layouts[i].offset;
  1091. copy_region.texture_subresources.aspect = tex->read_aspect_flags;
  1092. copy_region.texture_subresources.mipmap = i;
  1093. copy_region.texture_subresources.base_layer = p_layer;
  1094. copy_region.texture_subresources.layer_count = 1;
  1095. copy_region.texture_region_size.x = w;
  1096. copy_region.texture_region_size.y = h;
  1097. copy_region.texture_region_size.z = d;
  1098. command_buffer_texture_copy_regions_vector.push_back(copy_region);
  1099. w = MAX(1u, w >> 1);
  1100. h = MAX(1u, h >> 1);
  1101. d = MAX(1u, d >> 1);
  1102. }
  1103. if (_texture_make_mutable(tex, p_texture)) {
  1104. // The texture must be mutable to be used as a copy source due to layout transitions.
  1105. draw_graph.add_synchronization();
  1106. }
  1107. draw_graph.add_texture_get_data(tex->driver_id, tex->draw_tracker, tmp_buffer, command_buffer_texture_copy_regions_vector);
  1108. // Flush everything so memory can be safely mapped.
  1109. _flush_and_stall_for_all_frames();
  1110. const uint8_t *read_ptr = driver->buffer_map(tmp_buffer);
  1111. ERR_FAIL_NULL_V(read_ptr, Vector<uint8_t>());
  1112. Vector<uint8_t> buffer_data;
  1113. uint32_t tight_buffer_size = get_image_format_required_size(tex->format, tex->width, tex->height, tex->depth, tex->mipmaps);
  1114. buffer_data.resize(tight_buffer_size);
  1115. uint8_t *write_ptr = buffer_data.ptrw();
  1116. w = tex->width;
  1117. h = tex->height;
  1118. d = tex->depth;
  1119. for (uint32_t i = 0; i < tex->mipmaps; i++) {
  1120. uint32_t width = 0, height = 0, depth = 0;
  1121. uint32_t tight_mip_size = get_image_format_required_size(tex->format, w, h, d, 1, &width, &height, &depth);
  1122. uint32_t block_w = 0, block_h = 0;
  1123. get_compressed_image_format_block_dimensions(tex->format, block_w, block_h);
  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(1u, w >> 1);
  1134. h = MAX(1u, 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. }
  3120. void RenderingDevice::draw_list_enable_scissor(DrawListID p_list, const Rect2 &p_rect) {
  3121. DrawList *dl = _get_draw_list_ptr(p_list);
  3122. ERR_FAIL_NULL(dl);
  3123. #ifdef DEBUG_ENABLED
  3124. ERR_FAIL_COND_MSG(!dl->validation.active, "Submitted Draw Lists can no longer be modified.");
  3125. #endif
  3126. Rect2i rect = p_rect;
  3127. rect.position += dl->viewport.position;
  3128. rect = dl->viewport.intersection(rect);
  3129. if (rect.get_area() == 0) {
  3130. return;
  3131. }
  3132. _draw_list_set_scissor(rect);
  3133. }
  3134. void RenderingDevice::draw_list_disable_scissor(DrawListID p_list) {
  3135. DrawList *dl = _get_draw_list_ptr(p_list);
  3136. ERR_FAIL_NULL(dl);
  3137. #ifdef DEBUG_ENABLED
  3138. ERR_FAIL_COND_MSG(!dl->validation.active, "Submitted Draw Lists can no longer be modified.");
  3139. #endif
  3140. _draw_list_set_scissor(dl->viewport);
  3141. }
  3142. uint32_t RenderingDevice::draw_list_get_current_pass() {
  3143. return draw_list_current_subpass;
  3144. }
  3145. RenderingDevice::DrawListID RenderingDevice::draw_list_switch_to_next_pass() {
  3146. _THREAD_SAFE_METHOD_
  3147. ERR_FAIL_NULL_V(draw_list, INVALID_ID);
  3148. ERR_FAIL_COND_V(draw_list_current_subpass >= draw_list_subpass_count - 1, INVALID_FORMAT_ID);
  3149. draw_list_current_subpass++;
  3150. Rect2i viewport;
  3151. _draw_list_free(&viewport);
  3152. draw_graph.add_draw_list_next_subpass(RDD::COMMAND_BUFFER_TYPE_PRIMARY);
  3153. _draw_list_allocate(viewport, draw_list_current_subpass);
  3154. return int64_t(ID_TYPE_DRAW_LIST) << ID_BASE_SHIFT;
  3155. }
  3156. #ifndef DISABLE_DEPRECATED
  3157. Error RenderingDevice::draw_list_switch_to_next_pass_split(uint32_t p_splits, DrawListID *r_split_ids) {
  3158. ERR_FAIL_V_MSG(ERR_UNAVAILABLE, "Deprecated. Split draw lists are used automatically by RenderingDevice.");
  3159. }
  3160. #endif
  3161. Error RenderingDevice::_draw_list_allocate(const Rect2i &p_viewport, uint32_t p_subpass) {
  3162. // Lock while draw_list is active.
  3163. _THREAD_SAFE_LOCK_
  3164. draw_list = memnew(DrawList);
  3165. draw_list->viewport = p_viewport;
  3166. return OK;
  3167. }
  3168. void RenderingDevice::_draw_list_free(Rect2i *r_last_viewport) {
  3169. if (r_last_viewport) {
  3170. *r_last_viewport = draw_list->viewport;
  3171. }
  3172. // Just end the list.
  3173. memdelete(draw_list);
  3174. draw_list = nullptr;
  3175. // Draw_list is no longer active.
  3176. _THREAD_SAFE_UNLOCK_
  3177. }
  3178. void RenderingDevice::draw_list_end() {
  3179. _THREAD_SAFE_METHOD_
  3180. ERR_FAIL_NULL_MSG(draw_list, "Immediate draw list is already inactive.");
  3181. draw_graph.add_draw_list_end();
  3182. _draw_list_free();
  3183. for (int i = 0; i < draw_list_bound_textures.size(); i++) {
  3184. Texture *texture = texture_owner.get_or_null(draw_list_bound_textures[i]);
  3185. ERR_CONTINUE(!texture); // Wtf.
  3186. if (texture->usage_flags & TEXTURE_USAGE_COLOR_ATTACHMENT_BIT) {
  3187. texture->bound = false;
  3188. }
  3189. if (texture->usage_flags & TEXTURE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT) {
  3190. texture->bound = false;
  3191. }
  3192. }
  3193. draw_list_bound_textures.clear();
  3194. }
  3195. /***********************/
  3196. /**** COMPUTE LISTS ****/
  3197. /***********************/
  3198. RenderingDevice::ComputeListID RenderingDevice::compute_list_begin() {
  3199. _THREAD_SAFE_METHOD_
  3200. ERR_FAIL_COND_V_MSG(compute_list != nullptr, INVALID_ID, "Only one draw/compute list can be active at the same time.");
  3201. // Lock while compute_list is active.
  3202. _THREAD_SAFE_LOCK_
  3203. compute_list = memnew(ComputeList);
  3204. draw_graph.add_compute_list_begin();
  3205. return ID_TYPE_COMPUTE_LIST;
  3206. }
  3207. void RenderingDevice::compute_list_bind_compute_pipeline(ComputeListID p_list, RID p_compute_pipeline) {
  3208. // Must be called within a compute list, the class mutex is locked during that time
  3209. ERR_FAIL_COND(p_list != ID_TYPE_COMPUTE_LIST);
  3210. ERR_FAIL_NULL(compute_list);
  3211. ComputeList *cl = compute_list;
  3212. const ComputePipeline *pipeline = compute_pipeline_owner.get_or_null(p_compute_pipeline);
  3213. ERR_FAIL_NULL(pipeline);
  3214. if (p_compute_pipeline == cl->state.pipeline) {
  3215. return; // Redundant state, return.
  3216. }
  3217. cl->state.pipeline = p_compute_pipeline;
  3218. draw_graph.add_compute_list_bind_pipeline(pipeline->driver_id);
  3219. if (cl->state.pipeline_shader != pipeline->shader) {
  3220. // Shader changed, so descriptor sets may become incompatible.
  3221. uint32_t pcount = pipeline->set_formats.size(); // Formats count in this pipeline.
  3222. cl->state.set_count = MAX(cl->state.set_count, pcount);
  3223. const uint32_t *pformats = pipeline->set_formats.ptr(); // Pipeline set formats.
  3224. uint32_t first_invalid_set = UINT32_MAX; // All valid by default.
  3225. switch (driver->api_trait_get(RDD::API_TRAIT_SHADER_CHANGE_INVALIDATION)) {
  3226. case RDD::SHADER_CHANGE_INVALIDATION_ALL_BOUND_UNIFORM_SETS: {
  3227. first_invalid_set = 0;
  3228. } break;
  3229. case RDD::SHADER_CHANGE_INVALIDATION_INCOMPATIBLE_SETS_PLUS_CASCADE: {
  3230. for (uint32_t i = 0; i < pcount; i++) {
  3231. if (cl->state.sets[i].pipeline_expected_format != pformats[i]) {
  3232. first_invalid_set = i;
  3233. break;
  3234. }
  3235. }
  3236. } break;
  3237. case RDD::SHADER_CHANGE_INVALIDATION_ALL_OR_NONE_ACCORDING_TO_LAYOUT_HASH: {
  3238. if (cl->state.pipeline_shader_layout_hash != pipeline->shader_layout_hash) {
  3239. first_invalid_set = 0;
  3240. }
  3241. } break;
  3242. }
  3243. for (uint32_t i = 0; i < pcount; i++) {
  3244. cl->state.sets[i].bound = cl->state.sets[i].bound && i < first_invalid_set;
  3245. cl->state.sets[i].pipeline_expected_format = pformats[i];
  3246. }
  3247. for (uint32_t i = pcount; i < cl->state.set_count; i++) {
  3248. // Unbind the ones above (not used) if exist.
  3249. cl->state.sets[i].bound = false;
  3250. }
  3251. cl->state.set_count = pcount; // Update set count.
  3252. if (pipeline->push_constant_size) {
  3253. #ifdef DEBUG_ENABLED
  3254. cl->validation.pipeline_push_constant_supplied = false;
  3255. #endif
  3256. }
  3257. cl->state.pipeline_shader = pipeline->shader;
  3258. cl->state.pipeline_shader_driver_id = pipeline->shader_driver_id;
  3259. cl->state.pipeline_shader_layout_hash = pipeline->shader_layout_hash;
  3260. cl->state.local_group_size[0] = pipeline->local_group_size[0];
  3261. cl->state.local_group_size[1] = pipeline->local_group_size[1];
  3262. cl->state.local_group_size[2] = pipeline->local_group_size[2];
  3263. }
  3264. #ifdef DEBUG_ENABLED
  3265. // Update compute pass pipeline info.
  3266. cl->validation.pipeline_active = true;
  3267. cl->validation.pipeline_push_constant_size = pipeline->push_constant_size;
  3268. #endif
  3269. }
  3270. void RenderingDevice::compute_list_bind_uniform_set(ComputeListID p_list, RID p_uniform_set, uint32_t p_index) {
  3271. // Must be called within a compute list, the class mutex is locked during that time
  3272. ERR_FAIL_COND(p_list != ID_TYPE_COMPUTE_LIST);
  3273. ERR_FAIL_NULL(compute_list);
  3274. ComputeList *cl = compute_list;
  3275. #ifdef DEBUG_ENABLED
  3276. ERR_FAIL_COND_MSG(p_index >= driver->limit_get(LIMIT_MAX_BOUND_UNIFORM_SETS) || p_index >= MAX_UNIFORM_SETS,
  3277. "Attempting to bind a descriptor set (" + itos(p_index) + ") greater than what the hardware supports (" + itos(driver->limit_get(LIMIT_MAX_BOUND_UNIFORM_SETS)) + ").");
  3278. #endif
  3279. #ifdef DEBUG_ENABLED
  3280. ERR_FAIL_COND_MSG(!cl->validation.active, "Submitted Compute Lists can no longer be modified.");
  3281. #endif
  3282. UniformSet *uniform_set = uniform_set_owner.get_or_null(p_uniform_set);
  3283. ERR_FAIL_NULL(uniform_set);
  3284. if (p_index > cl->state.set_count) {
  3285. cl->state.set_count = p_index;
  3286. }
  3287. cl->state.sets[p_index].uniform_set_driver_id = uniform_set->driver_id; // Update set pointer.
  3288. cl->state.sets[p_index].bound = false; // Needs rebind.
  3289. cl->state.sets[p_index].uniform_set_format = uniform_set->format;
  3290. cl->state.sets[p_index].uniform_set = p_uniform_set;
  3291. #if 0
  3292. { // Validate that textures bound are not attached as framebuffer bindings.
  3293. uint32_t attachable_count = uniform_set->attachable_textures.size();
  3294. const RID *attachable_ptr = uniform_set->attachable_textures.ptr();
  3295. uint32_t bound_count = draw_list_bound_textures.size();
  3296. const RID *bound_ptr = draw_list_bound_textures.ptr();
  3297. for (uint32_t i = 0; i < attachable_count; i++) {
  3298. for (uint32_t j = 0; j < bound_count; j++) {
  3299. ERR_FAIL_COND_MSG(attachable_ptr[i] == bound_ptr[j],
  3300. "Attempted to use the same texture in framebuffer attachment and a uniform set, this is not allowed.");
  3301. }
  3302. }
  3303. }
  3304. #endif
  3305. }
  3306. void RenderingDevice::compute_list_set_push_constant(ComputeListID p_list, const void *p_data, uint32_t p_data_size) {
  3307. ERR_FAIL_COND(p_list != ID_TYPE_COMPUTE_LIST);
  3308. ERR_FAIL_NULL(compute_list);
  3309. ERR_FAIL_COND_MSG(p_data_size > MAX_PUSH_CONSTANT_SIZE, "Push constants can't be bigger than 128 bytes to maintain compatibility.");
  3310. ComputeList *cl = compute_list;
  3311. #ifdef DEBUG_ENABLED
  3312. ERR_FAIL_COND_MSG(!cl->validation.active, "Submitted Compute Lists can no longer be modified.");
  3313. #endif
  3314. #ifdef DEBUG_ENABLED
  3315. ERR_FAIL_COND_MSG(p_data_size != cl->validation.pipeline_push_constant_size,
  3316. "This compute pipeline requires (" + itos(cl->validation.pipeline_push_constant_size) + ") bytes of push constant data, supplied: (" + itos(p_data_size) + ")");
  3317. #endif
  3318. draw_graph.add_compute_list_set_push_constant(cl->state.pipeline_shader_driver_id, p_data, p_data_size);
  3319. // Store it in the state in case we need to restart the compute list.
  3320. memcpy(cl->state.push_constant_data, p_data, p_data_size);
  3321. cl->state.push_constant_size = p_data_size;
  3322. #ifdef DEBUG_ENABLED
  3323. cl->validation.pipeline_push_constant_supplied = true;
  3324. #endif
  3325. }
  3326. void RenderingDevice::compute_list_dispatch(ComputeListID p_list, uint32_t p_x_groups, uint32_t p_y_groups, uint32_t p_z_groups) {
  3327. // Must be called within a compute list, the class mutex is locked during that time
  3328. ERR_FAIL_COND(p_list != ID_TYPE_COMPUTE_LIST);
  3329. ERR_FAIL_NULL(compute_list);
  3330. ComputeList *cl = compute_list;
  3331. #ifdef DEBUG_ENABLED
  3332. ERR_FAIL_COND_MSG(p_x_groups == 0, "Dispatch amount of X compute groups (" + itos(p_x_groups) + ") is zero.");
  3333. ERR_FAIL_COND_MSG(p_z_groups == 0, "Dispatch amount of Z compute groups (" + itos(p_z_groups) + ") is zero.");
  3334. ERR_FAIL_COND_MSG(p_y_groups == 0, "Dispatch amount of Y compute groups (" + itos(p_y_groups) + ") is zero.");
  3335. ERR_FAIL_COND_MSG(p_x_groups > driver->limit_get(LIMIT_MAX_COMPUTE_WORKGROUP_COUNT_X),
  3336. "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)) + ")");
  3337. ERR_FAIL_COND_MSG(p_y_groups > driver->limit_get(LIMIT_MAX_COMPUTE_WORKGROUP_COUNT_Y),
  3338. "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)) + ")");
  3339. ERR_FAIL_COND_MSG(p_z_groups > driver->limit_get(LIMIT_MAX_COMPUTE_WORKGROUP_COUNT_Z),
  3340. "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)) + ")");
  3341. ERR_FAIL_COND_MSG(!cl->validation.active, "Submitted Compute Lists can no longer be modified.");
  3342. #endif
  3343. #ifdef DEBUG_ENABLED
  3344. ERR_FAIL_COND_MSG(!cl->validation.pipeline_active, "No compute pipeline was set before attempting to draw.");
  3345. if (cl->validation.pipeline_push_constant_size > 0) {
  3346. // Using push constants, check that they were supplied.
  3347. ERR_FAIL_COND_MSG(!cl->validation.pipeline_push_constant_supplied,
  3348. "The shader in this pipeline requires a push constant to be set before drawing, but it's not present.");
  3349. }
  3350. #endif
  3351. // Bind descriptor sets.
  3352. for (uint32_t i = 0; i < cl->state.set_count; i++) {
  3353. if (cl->state.sets[i].pipeline_expected_format == 0) {
  3354. continue; // Nothing expected by this pipeline.
  3355. }
  3356. #ifdef DEBUG_ENABLED
  3357. if (cl->state.sets[i].pipeline_expected_format != cl->state.sets[i].uniform_set_format) {
  3358. if (cl->state.sets[i].uniform_set_format == 0) {
  3359. ERR_FAIL_MSG("Uniforms were never supplied for set (" + itos(i) + ") at the time of drawing, which are required by the pipeline.");
  3360. } else if (uniform_set_owner.owns(cl->state.sets[i].uniform_set)) {
  3361. UniformSet *us = uniform_set_owner.get_or_null(cl->state.sets[i].uniform_set);
  3362. 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));
  3363. } else {
  3364. 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));
  3365. }
  3366. }
  3367. #endif
  3368. 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);
  3369. }
  3370. for (uint32_t i = 0; i < cl->state.set_count; i++) {
  3371. if (cl->state.sets[i].pipeline_expected_format == 0) {
  3372. continue; // Nothing expected by this pipeline.
  3373. }
  3374. if (!cl->state.sets[i].bound) {
  3375. // All good, see if this requires re-binding.
  3376. draw_graph.add_compute_list_bind_uniform_set(cl->state.pipeline_shader_driver_id, cl->state.sets[i].uniform_set_driver_id, i);
  3377. UniformSet *uniform_set = uniform_set_owner.get_or_null(cl->state.sets[i].uniform_set);
  3378. draw_graph.add_compute_list_usages(uniform_set->draw_trackers, uniform_set->draw_trackers_usage);
  3379. cl->state.sets[i].bound = true;
  3380. }
  3381. }
  3382. draw_graph.add_compute_list_dispatch(p_x_groups, p_y_groups, p_z_groups);
  3383. }
  3384. void RenderingDevice::compute_list_dispatch_threads(ComputeListID p_list, uint32_t p_x_threads, uint32_t p_y_threads, uint32_t p_z_threads) {
  3385. ERR_FAIL_COND(p_list != ID_TYPE_COMPUTE_LIST);
  3386. ERR_FAIL_NULL(compute_list);
  3387. #ifdef DEBUG_ENABLED
  3388. ERR_FAIL_COND_MSG(p_x_threads == 0, "Dispatch amount of X compute threads (" + itos(p_x_threads) + ") is zero.");
  3389. ERR_FAIL_COND_MSG(p_y_threads == 0, "Dispatch amount of Y compute threads (" + itos(p_y_threads) + ") is zero.");
  3390. ERR_FAIL_COND_MSG(p_z_threads == 0, "Dispatch amount of Z compute threads (" + itos(p_z_threads) + ") is zero.");
  3391. #endif
  3392. ComputeList *cl = compute_list;
  3393. #ifdef DEBUG_ENABLED
  3394. ERR_FAIL_COND_MSG(!cl->validation.pipeline_active, "No compute pipeline was set before attempting to draw.");
  3395. if (cl->validation.pipeline_push_constant_size > 0) {
  3396. // Using push constants, check that they were supplied.
  3397. ERR_FAIL_COND_MSG(!cl->validation.pipeline_push_constant_supplied,
  3398. "The shader in this pipeline requires a push constant to be set before drawing, but it's not present.");
  3399. }
  3400. #endif
  3401. 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]));
  3402. }
  3403. void RenderingDevice::compute_list_dispatch_indirect(ComputeListID p_list, RID p_buffer, uint32_t p_offset) {
  3404. ERR_FAIL_COND(p_list != ID_TYPE_COMPUTE_LIST);
  3405. ERR_FAIL_NULL(compute_list);
  3406. ComputeList *cl = compute_list;
  3407. Buffer *buffer = storage_buffer_owner.get_or_null(p_buffer);
  3408. ERR_FAIL_NULL(buffer);
  3409. ERR_FAIL_COND_MSG(!buffer->usage.has_flag(RDD::BUFFER_USAGE_INDIRECT_BIT), "Buffer provided was not created to do indirect dispatch.");
  3410. ERR_FAIL_COND_MSG(p_offset + 12 > buffer->size, "Offset provided (+12) is past the end of buffer.");
  3411. #ifdef DEBUG_ENABLED
  3412. ERR_FAIL_COND_MSG(!cl->validation.active, "Submitted Compute Lists can no longer be modified.");
  3413. #endif
  3414. #ifdef DEBUG_ENABLED
  3415. ERR_FAIL_COND_MSG(!cl->validation.pipeline_active, "No compute pipeline was set before attempting to draw.");
  3416. if (cl->validation.pipeline_push_constant_size > 0) {
  3417. // Using push constants, check that they were supplied.
  3418. ERR_FAIL_COND_MSG(!cl->validation.pipeline_push_constant_supplied,
  3419. "The shader in this pipeline requires a push constant to be set before drawing, but it's not present.");
  3420. }
  3421. #endif
  3422. // Bind descriptor sets.
  3423. for (uint32_t i = 0; i < cl->state.set_count; i++) {
  3424. if (cl->state.sets[i].pipeline_expected_format == 0) {
  3425. continue; // Nothing expected by this pipeline.
  3426. }
  3427. #ifdef DEBUG_ENABLED
  3428. if (cl->state.sets[i].pipeline_expected_format != cl->state.sets[i].uniform_set_format) {
  3429. if (cl->state.sets[i].uniform_set_format == 0) {
  3430. ERR_FAIL_MSG("Uniforms were never supplied for set (" + itos(i) + ") at the time of drawing, which are required by the pipeline");
  3431. } else if (uniform_set_owner.owns(cl->state.sets[i].uniform_set)) {
  3432. UniformSet *us = uniform_set_owner.get_or_null(cl->state.sets[i].uniform_set);
  3433. 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));
  3434. } else {
  3435. 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));
  3436. }
  3437. }
  3438. #endif
  3439. 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);
  3440. }
  3441. for (uint32_t i = 0; i < cl->state.set_count; i++) {
  3442. if (cl->state.sets[i].pipeline_expected_format == 0) {
  3443. continue; // Nothing expected by this pipeline.
  3444. }
  3445. if (!cl->state.sets[i].bound) {
  3446. // All good, see if this requires re-binding.
  3447. draw_graph.add_compute_list_bind_uniform_set(cl->state.pipeline_shader_driver_id, cl->state.sets[i].uniform_set_driver_id, i);
  3448. UniformSet *uniform_set = uniform_set_owner.get_or_null(cl->state.sets[i].uniform_set);
  3449. draw_graph.add_compute_list_usages(uniform_set->draw_trackers, uniform_set->draw_trackers_usage);
  3450. cl->state.sets[i].bound = true;
  3451. }
  3452. }
  3453. draw_graph.add_compute_list_dispatch_indirect(buffer->driver_id, p_offset);
  3454. if (buffer->draw_tracker != nullptr) {
  3455. draw_graph.add_compute_list_usage(buffer->draw_tracker, RDG::RESOURCE_USAGE_INDIRECT_BUFFER_READ);
  3456. }
  3457. }
  3458. void RenderingDevice::compute_list_add_barrier(ComputeListID p_list) {
  3459. // Must be called within a compute list, the class mutex is locked during that time
  3460. compute_list_barrier_state = compute_list->state;
  3461. compute_list_end();
  3462. compute_list_begin();
  3463. if (compute_list_barrier_state.pipeline.is_valid()) {
  3464. compute_list_bind_compute_pipeline(p_list, compute_list_barrier_state.pipeline);
  3465. }
  3466. for (uint32_t i = 0; i < compute_list_barrier_state.set_count; i++) {
  3467. if (compute_list_barrier_state.sets[i].uniform_set.is_valid()) {
  3468. compute_list_bind_uniform_set(p_list, compute_list_barrier_state.sets[i].uniform_set, i);
  3469. }
  3470. }
  3471. if (compute_list_barrier_state.push_constant_size > 0) {
  3472. compute_list_set_push_constant(p_list, compute_list_barrier_state.push_constant_data, compute_list_barrier_state.push_constant_size);
  3473. }
  3474. }
  3475. void RenderingDevice::compute_list_end() {
  3476. ERR_FAIL_NULL(compute_list);
  3477. draw_graph.add_compute_list_end();
  3478. memdelete(compute_list);
  3479. compute_list = nullptr;
  3480. // Compute_list is no longer active.
  3481. _THREAD_SAFE_UNLOCK_
  3482. }
  3483. #ifndef DISABLE_DEPRECATED
  3484. void RenderingDevice::barrier(BitField<BarrierMask> p_from, BitField<BarrierMask> p_to) {
  3485. WARN_PRINT("Deprecated. Barriers are automatically inserted by RenderingDevice.");
  3486. }
  3487. void RenderingDevice::full_barrier() {
  3488. WARN_PRINT("Deprecated. Barriers are automatically inserted by RenderingDevice.");
  3489. }
  3490. #endif
  3491. /***********************/
  3492. /**** COMMAND GRAPH ****/
  3493. /***********************/
  3494. bool RenderingDevice::_texture_make_mutable(Texture *p_texture, RID p_texture_id) {
  3495. if (p_texture->draw_tracker != nullptr) {
  3496. // Texture already has a tracker.
  3497. return false;
  3498. } else {
  3499. if (p_texture->owner.is_valid()) {
  3500. // Texture has an owner.
  3501. Texture *owner_texture = texture_owner.get_or_null(p_texture->owner);
  3502. ERR_FAIL_NULL_V(owner_texture, false);
  3503. if (owner_texture->draw_tracker != nullptr) {
  3504. // Create a tracker for this dependency in particular.
  3505. if (p_texture->slice_type == TEXTURE_SLICE_MAX) {
  3506. // Shared texture.
  3507. p_texture->draw_tracker = owner_texture->draw_tracker;
  3508. p_texture->draw_tracker->reference_count++;
  3509. } else {
  3510. // Slice texture.
  3511. HashMap<Rect2i, RDG::ResourceTracker *>::ConstIterator draw_tracker_iterator = owner_texture->slice_trackers.find(p_texture->slice_rect);
  3512. RDG::ResourceTracker *draw_tracker = nullptr;
  3513. if (draw_tracker_iterator != owner_texture->slice_trackers.end()) {
  3514. // Reuse the tracker at the matching rectangle.
  3515. draw_tracker = draw_tracker_iterator->value;
  3516. } else {
  3517. // Create a new tracker and store it on the map.
  3518. draw_tracker = RDG::resource_tracker_create();
  3519. draw_tracker->parent = owner_texture->draw_tracker;
  3520. draw_tracker->texture_driver_id = p_texture->driver_id;
  3521. draw_tracker->texture_subresources = p_texture->barrier_range();
  3522. draw_tracker->texture_slice_or_dirty_rect = p_texture->slice_rect;
  3523. owner_texture->slice_trackers[p_texture->slice_rect] = draw_tracker;
  3524. }
  3525. p_texture->slice_trackers.clear();
  3526. p_texture->draw_tracker = draw_tracker;
  3527. p_texture->draw_tracker->reference_count++;
  3528. }
  3529. if (p_texture_id.is_valid()) {
  3530. _dependencies_make_mutable(p_texture_id, p_texture->draw_tracker);
  3531. }
  3532. } else {
  3533. // Delegate this to the owner instead, as it'll make all its dependencies mutable.
  3534. _texture_make_mutable(owner_texture, p_texture->owner);
  3535. }
  3536. } else {
  3537. // Regular texture.
  3538. p_texture->draw_tracker = RDG::resource_tracker_create();
  3539. p_texture->draw_tracker->texture_driver_id = p_texture->driver_id;
  3540. p_texture->draw_tracker->texture_subresources = p_texture->barrier_range();
  3541. p_texture->draw_tracker->reference_count = 1;
  3542. if (p_texture_id.is_valid()) {
  3543. if (p_texture->has_initial_data) {
  3544. // If the texture was initialized with initial data but wasn't made mutable from the start, assume the texture sampling usage.
  3545. p_texture->draw_tracker->usage = RDG::RESOURCE_USAGE_TEXTURE_SAMPLE;
  3546. }
  3547. _dependencies_make_mutable(p_texture_id, p_texture->draw_tracker);
  3548. }
  3549. }
  3550. return true;
  3551. }
  3552. }
  3553. bool RenderingDevice::_buffer_make_mutable(Buffer *p_buffer, RID p_buffer_id) {
  3554. if (p_buffer->draw_tracker != nullptr) {
  3555. // Buffer already has a tracker.
  3556. return false;
  3557. } else {
  3558. // Create a tracker for the buffer and make all its dependencies mutable.
  3559. p_buffer->draw_tracker = RDG::resource_tracker_create();
  3560. p_buffer->draw_tracker->buffer_driver_id = p_buffer->driver_id;
  3561. if (p_buffer_id.is_valid()) {
  3562. _dependencies_make_mutable(p_buffer_id, p_buffer->draw_tracker);
  3563. }
  3564. return true;
  3565. }
  3566. }
  3567. bool RenderingDevice::_vertex_array_make_mutable(VertexArray *p_vertex_array, RID p_resource_id, RDG::ResourceTracker *p_resource_tracker) {
  3568. if (!p_vertex_array->untracked_buffers.has(p_resource_id)) {
  3569. // Vertex array thinks the buffer is already tracked or does not use it.
  3570. return false;
  3571. } else {
  3572. // Vertex array is aware of the buffer but it isn't being tracked.
  3573. p_vertex_array->draw_trackers.push_back(p_resource_tracker);
  3574. p_vertex_array->untracked_buffers.erase(p_resource_id);
  3575. return true;
  3576. }
  3577. }
  3578. bool RenderingDevice::_index_array_make_mutable(IndexArray *p_index_array, RDG::ResourceTracker *p_resource_tracker) {
  3579. if (p_index_array->draw_tracker != nullptr) {
  3580. // Index array already has a tracker.
  3581. return false;
  3582. } else {
  3583. // Index array should assign the tracker from the buffer.
  3584. p_index_array->draw_tracker = p_resource_tracker;
  3585. return true;
  3586. }
  3587. }
  3588. bool RenderingDevice::_uniform_set_make_mutable(UniformSet *p_uniform_set, RID p_resource_id, RDG::ResourceTracker *p_resource_tracker) {
  3589. HashMap<RID, RDG::ResourceUsage>::Iterator E = p_uniform_set->untracked_usage.find(p_resource_id);
  3590. if (!E) {
  3591. // Uniform set thinks the resource is already tracked or does not use it.
  3592. return false;
  3593. } else {
  3594. // Uniform set has seen the resource but hasn't added its tracker yet.
  3595. p_uniform_set->draw_trackers.push_back(p_resource_tracker);
  3596. p_uniform_set->draw_trackers_usage.push_back(E->value);
  3597. p_uniform_set->untracked_usage.remove(E);
  3598. return true;
  3599. }
  3600. }
  3601. bool RenderingDevice::_dependency_make_mutable(RID p_id, RID p_resource_id, RDG::ResourceTracker *p_resource_tracker) {
  3602. if (texture_owner.owns(p_id)) {
  3603. Texture *texture = texture_owner.get_or_null(p_id);
  3604. return _texture_make_mutable(texture, p_id);
  3605. } else if (vertex_array_owner.owns(p_id)) {
  3606. VertexArray *vertex_array = vertex_array_owner.get_or_null(p_id);
  3607. return _vertex_array_make_mutable(vertex_array, p_resource_id, p_resource_tracker);
  3608. } else if (index_array_owner.owns(p_id)) {
  3609. IndexArray *index_array = index_array_owner.get_or_null(p_id);
  3610. return _index_array_make_mutable(index_array, p_resource_tracker);
  3611. } else if (uniform_set_owner.owns(p_id)) {
  3612. UniformSet *uniform_set = uniform_set_owner.get_or_null(p_id);
  3613. return _uniform_set_make_mutable(uniform_set, p_resource_id, p_resource_tracker);
  3614. } else {
  3615. DEV_ASSERT(false && "Unknown resource type to make mutable.");
  3616. return false;
  3617. }
  3618. }
  3619. bool RenderingDevice::_dependencies_make_mutable(RID p_id, RDG::ResourceTracker *p_resource_tracker) {
  3620. bool made_mutable = false;
  3621. HashMap<RID, HashSet<RID>>::Iterator E = dependency_map.find(p_id);
  3622. if (E) {
  3623. for (RID rid : E->value) {
  3624. made_mutable = _dependency_make_mutable(rid, p_id, p_resource_tracker) || made_mutable;
  3625. }
  3626. }
  3627. return made_mutable;
  3628. }
  3629. /**************************/
  3630. /**** FRAME MANAGEMENT ****/
  3631. /**************************/
  3632. void RenderingDevice::free(RID p_id) {
  3633. _THREAD_SAFE_METHOD_
  3634. _free_dependencies(p_id); // Recursively erase dependencies first, to avoid potential API problems.
  3635. _free_internal(p_id);
  3636. }
  3637. void RenderingDevice::_free_internal(RID p_id) {
  3638. #ifdef DEV_ENABLED
  3639. String resource_name;
  3640. if (resource_names.has(p_id)) {
  3641. resource_name = resource_names[p_id];
  3642. resource_names.erase(p_id);
  3643. }
  3644. #endif
  3645. // Push everything so it's disposed of next time this frame index is processed (means, it's safe to do it).
  3646. if (texture_owner.owns(p_id)) {
  3647. Texture *texture = texture_owner.get_or_null(p_id);
  3648. RDG::ResourceTracker *draw_tracker = texture->draw_tracker;
  3649. if (draw_tracker != nullptr) {
  3650. draw_tracker->reference_count--;
  3651. if (draw_tracker->reference_count == 0) {
  3652. RDG::resource_tracker_free(draw_tracker);
  3653. if (texture->owner.is_valid() && (texture->slice_type != TEXTURE_SLICE_MAX)) {
  3654. // If this was a texture slice, erase the tracker from the map.
  3655. Texture *owner_texture = texture_owner.get_or_null(texture->owner);
  3656. if (owner_texture != nullptr) {
  3657. owner_texture->slice_trackers.erase(texture->slice_rect);
  3658. }
  3659. }
  3660. }
  3661. }
  3662. frames[frame].textures_to_dispose_of.push_back(*texture);
  3663. texture_owner.free(p_id);
  3664. } else if (framebuffer_owner.owns(p_id)) {
  3665. Framebuffer *framebuffer = framebuffer_owner.get_or_null(p_id);
  3666. frames[frame].framebuffers_to_dispose_of.push_back(*framebuffer);
  3667. if (framebuffer->invalidated_callback != nullptr) {
  3668. framebuffer->invalidated_callback(framebuffer->invalidated_callback_userdata);
  3669. }
  3670. framebuffer_owner.free(p_id);
  3671. } else if (sampler_owner.owns(p_id)) {
  3672. RDD::SamplerID sampler_driver_id = *sampler_owner.get_or_null(p_id);
  3673. frames[frame].samplers_to_dispose_of.push_back(sampler_driver_id);
  3674. sampler_owner.free(p_id);
  3675. } else if (vertex_buffer_owner.owns(p_id)) {
  3676. Buffer *vertex_buffer = vertex_buffer_owner.get_or_null(p_id);
  3677. RDG::resource_tracker_free(vertex_buffer->draw_tracker);
  3678. frames[frame].buffers_to_dispose_of.push_back(*vertex_buffer);
  3679. vertex_buffer_owner.free(p_id);
  3680. } else if (vertex_array_owner.owns(p_id)) {
  3681. vertex_array_owner.free(p_id);
  3682. } else if (index_buffer_owner.owns(p_id)) {
  3683. IndexBuffer *index_buffer = index_buffer_owner.get_or_null(p_id);
  3684. RDG::resource_tracker_free(index_buffer->draw_tracker);
  3685. frames[frame].buffers_to_dispose_of.push_back(*index_buffer);
  3686. index_buffer_owner.free(p_id);
  3687. } else if (index_array_owner.owns(p_id)) {
  3688. index_array_owner.free(p_id);
  3689. } else if (shader_owner.owns(p_id)) {
  3690. Shader *shader = shader_owner.get_or_null(p_id);
  3691. if (shader->driver_id) { // Not placeholder?
  3692. frames[frame].shaders_to_dispose_of.push_back(*shader);
  3693. }
  3694. shader_owner.free(p_id);
  3695. } else if (uniform_buffer_owner.owns(p_id)) {
  3696. Buffer *uniform_buffer = uniform_buffer_owner.get_or_null(p_id);
  3697. RDG::resource_tracker_free(uniform_buffer->draw_tracker);
  3698. frames[frame].buffers_to_dispose_of.push_back(*uniform_buffer);
  3699. uniform_buffer_owner.free(p_id);
  3700. } else if (texture_buffer_owner.owns(p_id)) {
  3701. Buffer *texture_buffer = texture_buffer_owner.get_or_null(p_id);
  3702. RDG::resource_tracker_free(texture_buffer->draw_tracker);
  3703. frames[frame].buffers_to_dispose_of.push_back(*texture_buffer);
  3704. texture_buffer_owner.free(p_id);
  3705. } else if (storage_buffer_owner.owns(p_id)) {
  3706. Buffer *storage_buffer = storage_buffer_owner.get_or_null(p_id);
  3707. RDG::resource_tracker_free(storage_buffer->draw_tracker);
  3708. frames[frame].buffers_to_dispose_of.push_back(*storage_buffer);
  3709. storage_buffer_owner.free(p_id);
  3710. } else if (uniform_set_owner.owns(p_id)) {
  3711. UniformSet *uniform_set = uniform_set_owner.get_or_null(p_id);
  3712. frames[frame].uniform_sets_to_dispose_of.push_back(*uniform_set);
  3713. uniform_set_owner.free(p_id);
  3714. if (uniform_set->invalidated_callback != nullptr) {
  3715. uniform_set->invalidated_callback(uniform_set->invalidated_callback_userdata);
  3716. }
  3717. } else if (render_pipeline_owner.owns(p_id)) {
  3718. RenderPipeline *pipeline = render_pipeline_owner.get_or_null(p_id);
  3719. frames[frame].render_pipelines_to_dispose_of.push_back(*pipeline);
  3720. render_pipeline_owner.free(p_id);
  3721. } else if (compute_pipeline_owner.owns(p_id)) {
  3722. ComputePipeline *pipeline = compute_pipeline_owner.get_or_null(p_id);
  3723. frames[frame].compute_pipelines_to_dispose_of.push_back(*pipeline);
  3724. compute_pipeline_owner.free(p_id);
  3725. } else {
  3726. #ifdef DEV_ENABLED
  3727. ERR_PRINT("Attempted to free invalid ID: " + itos(p_id.get_id()) + " " + resource_name);
  3728. #else
  3729. ERR_PRINT("Attempted to free invalid ID: " + itos(p_id.get_id()));
  3730. #endif
  3731. }
  3732. }
  3733. // The full list of resources that can be named is in the VkObjectType enum.
  3734. // We just expose the resources that are owned and can be accessed easily.
  3735. void RenderingDevice::set_resource_name(RID p_id, const String &p_name) {
  3736. if (texture_owner.owns(p_id)) {
  3737. Texture *texture = texture_owner.get_or_null(p_id);
  3738. driver->set_object_name(RDD::OBJECT_TYPE_TEXTURE, texture->driver_id, p_name);
  3739. } else if (framebuffer_owner.owns(p_id)) {
  3740. //Framebuffer *framebuffer = framebuffer_owner.get_or_null(p_id);
  3741. // Not implemented for now as the relationship between Framebuffer and RenderPass is very complex.
  3742. } else if (sampler_owner.owns(p_id)) {
  3743. RDD::SamplerID sampler_driver_id = *sampler_owner.get_or_null(p_id);
  3744. driver->set_object_name(RDD::OBJECT_TYPE_SAMPLER, sampler_driver_id, p_name);
  3745. } else if (vertex_buffer_owner.owns(p_id)) {
  3746. Buffer *vertex_buffer = vertex_buffer_owner.get_or_null(p_id);
  3747. driver->set_object_name(RDD::OBJECT_TYPE_BUFFER, vertex_buffer->driver_id, p_name);
  3748. } else if (index_buffer_owner.owns(p_id)) {
  3749. IndexBuffer *index_buffer = index_buffer_owner.get_or_null(p_id);
  3750. driver->set_object_name(RDD::OBJECT_TYPE_BUFFER, index_buffer->driver_id, p_name);
  3751. } else if (shader_owner.owns(p_id)) {
  3752. Shader *shader = shader_owner.get_or_null(p_id);
  3753. driver->set_object_name(RDD::OBJECT_TYPE_SHADER, shader->driver_id, p_name);
  3754. } else if (uniform_buffer_owner.owns(p_id)) {
  3755. Buffer *uniform_buffer = uniform_buffer_owner.get_or_null(p_id);
  3756. driver->set_object_name(RDD::OBJECT_TYPE_BUFFER, uniform_buffer->driver_id, p_name);
  3757. } else if (texture_buffer_owner.owns(p_id)) {
  3758. Buffer *texture_buffer = texture_buffer_owner.get_or_null(p_id);
  3759. driver->set_object_name(RDD::OBJECT_TYPE_BUFFER, texture_buffer->driver_id, p_name);
  3760. } else if (storage_buffer_owner.owns(p_id)) {
  3761. Buffer *storage_buffer = storage_buffer_owner.get_or_null(p_id);
  3762. driver->set_object_name(RDD::OBJECT_TYPE_BUFFER, storage_buffer->driver_id, p_name);
  3763. } else if (uniform_set_owner.owns(p_id)) {
  3764. UniformSet *uniform_set = uniform_set_owner.get_or_null(p_id);
  3765. driver->set_object_name(RDD::OBJECT_TYPE_UNIFORM_SET, uniform_set->driver_id, p_name);
  3766. } else if (render_pipeline_owner.owns(p_id)) {
  3767. RenderPipeline *pipeline = render_pipeline_owner.get_or_null(p_id);
  3768. driver->set_object_name(RDD::OBJECT_TYPE_PIPELINE, pipeline->driver_id, p_name);
  3769. } else if (compute_pipeline_owner.owns(p_id)) {
  3770. ComputePipeline *pipeline = compute_pipeline_owner.get_or_null(p_id);
  3771. driver->set_object_name(RDD::OBJECT_TYPE_PIPELINE, pipeline->driver_id, p_name);
  3772. } else {
  3773. ERR_PRINT("Attempted to name invalid ID: " + itos(p_id.get_id()));
  3774. return;
  3775. }
  3776. #ifdef DEV_ENABLED
  3777. resource_names[p_id] = p_name;
  3778. #endif
  3779. }
  3780. void RenderingDevice::draw_command_begin_label(String p_label_name, const Color &p_color) {
  3781. if (!context->is_debug_utils_enabled()) {
  3782. return;
  3783. }
  3784. draw_graph.begin_label(p_label_name, p_color);
  3785. }
  3786. #ifndef DISABLE_DEPRECATED
  3787. void RenderingDevice::draw_command_insert_label(String p_label_name, const Color &p_color) {
  3788. WARN_PRINT("Deprecated. Inserting labels no longer applies due to command reordering.");
  3789. }
  3790. #endif
  3791. void RenderingDevice::draw_command_end_label() {
  3792. draw_graph.end_label();
  3793. }
  3794. String RenderingDevice::get_device_vendor_name() const {
  3795. return _get_device_vendor_name(device);
  3796. }
  3797. String RenderingDevice::get_device_name() const {
  3798. return device.name;
  3799. }
  3800. RenderingDevice::DeviceType RenderingDevice::get_device_type() const {
  3801. return DeviceType(device.type);
  3802. }
  3803. String RenderingDevice::get_device_api_name() const {
  3804. return driver->get_api_name();
  3805. }
  3806. String RenderingDevice::get_device_api_version() const {
  3807. return driver->get_api_version();
  3808. }
  3809. String RenderingDevice::get_device_pipeline_cache_uuid() const {
  3810. return driver->get_pipeline_cache_uuid();
  3811. }
  3812. void RenderingDevice::swap_buffers() {
  3813. _THREAD_SAFE_METHOD_
  3814. _end_frame();
  3815. _execute_frame(true);
  3816. // Advance to the next frame and begin recording again.
  3817. frame = (frame + 1) % frames.size();
  3818. _begin_frame();
  3819. }
  3820. void RenderingDevice::submit() {
  3821. _THREAD_SAFE_METHOD_
  3822. _end_frame();
  3823. _execute_frame(false);
  3824. }
  3825. void RenderingDevice::sync() {
  3826. _THREAD_SAFE_METHOD_
  3827. _begin_frame();
  3828. }
  3829. void RenderingDevice::_free_pending_resources(int p_frame) {
  3830. // Free in dependency usage order, so nothing weird happens.
  3831. // Pipelines.
  3832. while (frames[p_frame].render_pipelines_to_dispose_of.front()) {
  3833. RenderPipeline *pipeline = &frames[p_frame].render_pipelines_to_dispose_of.front()->get();
  3834. driver->pipeline_free(pipeline->driver_id);
  3835. frames[p_frame].render_pipelines_to_dispose_of.pop_front();
  3836. }
  3837. while (frames[p_frame].compute_pipelines_to_dispose_of.front()) {
  3838. ComputePipeline *pipeline = &frames[p_frame].compute_pipelines_to_dispose_of.front()->get();
  3839. driver->pipeline_free(pipeline->driver_id);
  3840. frames[p_frame].compute_pipelines_to_dispose_of.pop_front();
  3841. }
  3842. // Uniform sets.
  3843. while (frames[p_frame].uniform_sets_to_dispose_of.front()) {
  3844. UniformSet *uniform_set = &frames[p_frame].uniform_sets_to_dispose_of.front()->get();
  3845. driver->uniform_set_free(uniform_set->driver_id);
  3846. frames[p_frame].uniform_sets_to_dispose_of.pop_front();
  3847. }
  3848. // Shaders.
  3849. while (frames[p_frame].shaders_to_dispose_of.front()) {
  3850. Shader *shader = &frames[p_frame].shaders_to_dispose_of.front()->get();
  3851. driver->shader_free(shader->driver_id);
  3852. frames[p_frame].shaders_to_dispose_of.pop_front();
  3853. }
  3854. // Samplers.
  3855. while (frames[p_frame].samplers_to_dispose_of.front()) {
  3856. RDD::SamplerID sampler = frames[p_frame].samplers_to_dispose_of.front()->get();
  3857. driver->sampler_free(sampler);
  3858. frames[p_frame].samplers_to_dispose_of.pop_front();
  3859. }
  3860. // Framebuffers.
  3861. while (frames[p_frame].framebuffers_to_dispose_of.front()) {
  3862. Framebuffer *framebuffer = &frames[p_frame].framebuffers_to_dispose_of.front()->get();
  3863. for (const KeyValue<Framebuffer::VersionKey, Framebuffer::Version> &E : framebuffer->framebuffers) {
  3864. // First framebuffer, then render pass because it depends on it.
  3865. driver->framebuffer_free(E.value.framebuffer);
  3866. driver->render_pass_free(E.value.render_pass);
  3867. }
  3868. frames[p_frame].framebuffers_to_dispose_of.pop_front();
  3869. }
  3870. // Textures.
  3871. while (frames[p_frame].textures_to_dispose_of.front()) {
  3872. Texture *texture = &frames[p_frame].textures_to_dispose_of.front()->get();
  3873. if (texture->bound) {
  3874. WARN_PRINT("Deleted a texture while it was bound.");
  3875. }
  3876. texture_memory -= driver->texture_get_allocation_size(texture->driver_id);
  3877. driver->texture_free(texture->driver_id);
  3878. frames[p_frame].textures_to_dispose_of.pop_front();
  3879. }
  3880. // Buffers.
  3881. while (frames[p_frame].buffers_to_dispose_of.front()) {
  3882. Buffer &buffer = frames[p_frame].buffers_to_dispose_of.front()->get();
  3883. driver->buffer_free(buffer.driver_id);
  3884. buffer_memory -= buffer.size;
  3885. frames[p_frame].buffers_to_dispose_of.pop_front();
  3886. }
  3887. }
  3888. uint32_t RenderingDevice::get_frame_delay() const {
  3889. return frames.size();
  3890. }
  3891. uint64_t RenderingDevice::get_memory_usage(MemoryType p_type) const {
  3892. switch (p_type) {
  3893. case MEMORY_BUFFERS: {
  3894. return buffer_memory;
  3895. }
  3896. case MEMORY_TEXTURES: {
  3897. return texture_memory;
  3898. }
  3899. case MEMORY_TOTAL: {
  3900. return driver->get_total_memory_used();
  3901. }
  3902. default: {
  3903. DEV_ASSERT(false);
  3904. return 0;
  3905. }
  3906. }
  3907. }
  3908. void RenderingDevice::_begin_frame() {
  3909. // Before beginning this frame, wait on the fence if it was signaled to make sure its work is finished.
  3910. if (frames[frame].draw_fence_signaled) {
  3911. driver->fence_wait(frames[frame].draw_fence);
  3912. frames[frame].draw_fence_signaled = false;
  3913. }
  3914. // Begin recording on the frame's command buffers.
  3915. driver->begin_segment(frame, frames_drawn++);
  3916. driver->command_buffer_begin(frames[frame].setup_command_buffer);
  3917. driver->command_buffer_begin(frames[frame].draw_command_buffer);
  3918. // Reset the graph.
  3919. draw_graph.begin();
  3920. // Erase pending resources.
  3921. _free_pending_resources(frame);
  3922. // Advance staging buffer if used.
  3923. if (staging_buffer_used) {
  3924. staging_buffer_current = (staging_buffer_current + 1) % staging_buffer_blocks.size();
  3925. staging_buffer_used = false;
  3926. }
  3927. if (frames[frame].timestamp_count) {
  3928. driver->timestamp_query_pool_get_results(frames[frame].timestamp_pool, frames[frame].timestamp_count, frames[frame].timestamp_result_values.ptr());
  3929. driver->command_timestamp_query_pool_reset(frames[frame].setup_command_buffer, frames[frame].timestamp_pool, frames[frame].timestamp_count);
  3930. SWAP(frames[frame].timestamp_names, frames[frame].timestamp_result_names);
  3931. SWAP(frames[frame].timestamp_cpu_values, frames[frame].timestamp_cpu_result_values);
  3932. }
  3933. frames[frame].timestamp_result_count = frames[frame].timestamp_count;
  3934. frames[frame].timestamp_count = 0;
  3935. frames[frame].index = Engine::get_singleton()->get_frames_drawn();
  3936. }
  3937. void RenderingDevice::_end_frame() {
  3938. if (draw_list) {
  3939. ERR_PRINT("Found open draw list at the end of the frame, this should never happen (further drawing will likely not work).");
  3940. }
  3941. if (compute_list) {
  3942. ERR_PRINT("Found open compute list at the end of the frame, this should never happen (further compute will likely not work).");
  3943. }
  3944. draw_graph.end(frames[frame].draw_command_buffer, RENDER_GRAPH_REORDER, RENDER_GRAPH_FULL_BARRIERS);
  3945. driver->command_buffer_end(frames[frame].setup_command_buffer);
  3946. driver->command_buffer_end(frames[frame].draw_command_buffer);
  3947. driver->end_segment();
  3948. }
  3949. void RenderingDevice::_execute_frame(bool p_present) {
  3950. const bool frame_can_present = p_present && !frames[frame].swap_chains_to_present.is_empty();
  3951. const bool separate_present_queue = main_queue != present_queue;
  3952. const VectorView<RDD::SemaphoreID> execute_draw_semaphore = frame_can_present && separate_present_queue ? frames[frame].draw_semaphore : VectorView<RDD::SemaphoreID>();
  3953. const VectorView<RDD::SwapChainID> execute_draw_swap_chains = frame_can_present && !separate_present_queue ? frames[frame].swap_chains_to_present : VectorView<RDD::SwapChainID>();
  3954. driver->command_queue_execute_and_present(main_queue, {}, frames[frame].setup_command_buffer, frames[frame].setup_semaphore, {}, {});
  3955. 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);
  3956. frames[frame].draw_fence_signaled = true;
  3957. if (frame_can_present) {
  3958. if (separate_present_queue) {
  3959. // Issue the presentation separately if the presentation queue is different from the main queue.
  3960. driver->command_queue_execute_and_present(present_queue, frames[frame].draw_semaphore, {}, {}, {}, frames[frame].swap_chains_to_present);
  3961. }
  3962. frames[frame].swap_chains_to_present.clear();
  3963. }
  3964. }
  3965. void RenderingDevice::_stall_for_previous_frames() {
  3966. for (uint32_t i = 0; i < frames.size(); i++) {
  3967. if (frames[i].draw_fence_signaled) {
  3968. driver->fence_wait(frames[i].draw_fence);
  3969. frames[i].draw_fence_signaled = false;
  3970. }
  3971. }
  3972. }
  3973. void RenderingDevice::_flush_and_stall_for_all_frames() {
  3974. _stall_for_previous_frames();
  3975. _end_frame();
  3976. _execute_frame(false);
  3977. _begin_frame();
  3978. }
  3979. Error RenderingDevice::initialize(RenderingContextDriver *p_context, DisplayServer::WindowID p_main_window) {
  3980. Error err;
  3981. RenderingContextDriver::SurfaceID main_surface = 0;
  3982. const bool main_instance = (singleton == this) && (p_main_window != DisplayServer::INVALID_WINDOW_ID);
  3983. if (p_main_window != DisplayServer::INVALID_WINDOW_ID) {
  3984. // Retrieve the surface from the main window if it was specified.
  3985. main_surface = p_context->surface_get_from_window(p_main_window);
  3986. ERR_FAIL_COND_V(main_surface == 0, FAILED);
  3987. }
  3988. context = p_context;
  3989. driver = context->driver_create();
  3990. print_verbose("Devices:");
  3991. int32_t device_index = Engine::get_singleton()->get_gpu_index();
  3992. const uint32_t device_count = context->device_get_count();
  3993. const bool detect_device = (device_index < 0) || (device_index >= int32_t(device_count));
  3994. uint32_t device_type_score = 0;
  3995. for (uint32_t i = 0; i < device_count; i++) {
  3996. RenderingContextDriver::Device device_option = context->device_get(i);
  3997. String name = device_option.name;
  3998. String vendor = _get_device_vendor_name(device_option);
  3999. String type = _get_device_type_name(device_option);
  4000. bool present_supported = main_surface != 0 ? context->device_supports_present(i, main_surface) : false;
  4001. print_verbose(" #" + itos(i) + ": " + vendor + " " + name + " - " + (present_supported ? "Supported" : "Unsupported") + ", " + type);
  4002. if (detect_device && (present_supported || main_surface == 0)) {
  4003. // If a window was specified, present must be supported by the device to be available as an option.
  4004. // Assign a score for each type of device and prefer the device with the higher score.
  4005. uint32_t option_score = _get_device_type_score(device_option);
  4006. if (option_score > device_type_score) {
  4007. device_index = i;
  4008. device_type_score = option_score;
  4009. }
  4010. }
  4011. }
  4012. 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.");
  4013. uint32_t frame_count = 1;
  4014. if (main_surface != 0) {
  4015. frame_count = MAX(2U, uint32_t(GLOBAL_GET("rendering/rendering_device/vsync/frame_queue_size")));
  4016. }
  4017. frame = 0;
  4018. frames.resize(frame_count);
  4019. max_timestamp_query_elements = 256;
  4020. device = context->device_get(device_index);
  4021. err = driver->initialize(device_index, frame_count);
  4022. ERR_FAIL_COND_V_MSG(err != OK, FAILED, "Failed to initialize driver for device.");
  4023. if (main_instance) {
  4024. // Only the singleton instance with a display should print this information.
  4025. String rendering_method;
  4026. if (OS::get_singleton()->get_current_rendering_method() == "mobile") {
  4027. rendering_method = "Forward Mobile";
  4028. } else {
  4029. rendering_method = "Forward+";
  4030. }
  4031. // Output our device version.
  4032. 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));
  4033. }
  4034. // Pick the main queue family. It is worth noting we explicitly do not request the transfer bit, as apparently the specification defines
  4035. // that the existence of either the graphics or compute bit implies that the queue can also do transfer operations, but it is optional
  4036. // to indicate whether it supports them or not with the dedicated transfer bit if either is set.
  4037. BitField<RDD::CommandQueueFamilyBits> main_queue_bits;
  4038. main_queue_bits.set_flag(RDD::COMMAND_QUEUE_FAMILY_GRAPHICS_BIT);
  4039. main_queue_bits.set_flag(RDD::COMMAND_QUEUE_FAMILY_COMPUTE_BIT);
  4040. #if !FORCE_SEPARATE_PRESENT_QUEUE
  4041. // Needing to use a separate queue for presentation is an edge case that remains to be seen what hardware triggers it at all.
  4042. main_queue_family = driver->command_queue_family_get(main_queue_bits, main_surface);
  4043. if (!main_queue_family && (main_surface != 0))
  4044. #endif
  4045. {
  4046. // If it was not possible to find a main queue that supports the surface, we attempt to get two different queues instead.
  4047. main_queue_family = driver->command_queue_family_get(main_queue_bits);
  4048. present_queue_family = driver->command_queue_family_get(BitField<RDD::CommandQueueFamilyBits>(), main_surface);
  4049. ERR_FAIL_COND_V(!present_queue_family, FAILED);
  4050. }
  4051. ERR_FAIL_COND_V(!main_queue_family, FAILED);
  4052. // Create the main queue.
  4053. main_queue = driver->command_queue_create(main_queue_family, true);
  4054. ERR_FAIL_COND_V(!main_queue, FAILED);
  4055. if (present_queue_family) {
  4056. // Create the presentation queue.
  4057. present_queue = driver->command_queue_create(present_queue_family);
  4058. ERR_FAIL_COND_V(!present_queue, FAILED);
  4059. } else {
  4060. present_queue = main_queue;
  4061. }
  4062. // Create data for all the frames.
  4063. for (uint32_t i = 0; i < frames.size(); i++) {
  4064. frames[i].index = 0;
  4065. // Create command pool, command buffers, semaphores and fences.
  4066. frames[i].command_pool = driver->command_pool_create(main_queue_family, RDD::COMMAND_BUFFER_TYPE_PRIMARY);
  4067. ERR_FAIL_COND_V(!frames[i].command_pool, FAILED);
  4068. frames[i].setup_command_buffer = driver->command_buffer_create(frames[i].command_pool);
  4069. ERR_FAIL_COND_V(!frames[i].setup_command_buffer, FAILED);
  4070. frames[i].draw_command_buffer = driver->command_buffer_create(frames[i].command_pool);
  4071. ERR_FAIL_COND_V(!frames[i].draw_command_buffer, FAILED);
  4072. frames[i].setup_semaphore = driver->semaphore_create();
  4073. ERR_FAIL_COND_V(!frames[i].setup_semaphore, FAILED);
  4074. frames[i].draw_semaphore = driver->semaphore_create();
  4075. ERR_FAIL_COND_V(!frames[i].draw_semaphore, FAILED);
  4076. frames[i].draw_fence = driver->fence_create();
  4077. ERR_FAIL_COND_V(!frames[i].draw_fence, FAILED);
  4078. frames[i].draw_fence_signaled = false;
  4079. // Create query pool.
  4080. frames[i].timestamp_pool = driver->timestamp_query_pool_create(max_timestamp_query_elements);
  4081. frames[i].timestamp_names.resize(max_timestamp_query_elements);
  4082. frames[i].timestamp_cpu_values.resize(max_timestamp_query_elements);
  4083. frames[i].timestamp_count = 0;
  4084. frames[i].timestamp_result_names.resize(max_timestamp_query_elements);
  4085. frames[i].timestamp_cpu_result_values.resize(max_timestamp_query_elements);
  4086. frames[i].timestamp_result_values.resize(max_timestamp_query_elements);
  4087. frames[i].timestamp_result_count = 0;
  4088. }
  4089. // Start from frame count, so everything else is immediately old.
  4090. frames_drawn = frames.size();
  4091. // Initialize recording on the first frame.
  4092. driver->begin_segment(frame, frames_drawn++);
  4093. driver->command_buffer_begin(frames[0].setup_command_buffer);
  4094. driver->command_buffer_begin(frames[0].draw_command_buffer);
  4095. // Create draw graph and start it initialized as well.
  4096. draw_graph.initialize(driver, frames.size(), main_queue_family, SECONDARY_COMMAND_BUFFERS_PER_FRAME);
  4097. draw_graph.begin();
  4098. for (uint32_t i = 0; i < frames.size(); i++) {
  4099. // Reset all queries in a query pool before doing any operations with them..
  4100. driver->command_timestamp_query_pool_reset(frames[0].setup_command_buffer, frames[i].timestamp_pool, max_timestamp_query_elements);
  4101. }
  4102. // Convert block size from KB.
  4103. staging_buffer_block_size = GLOBAL_GET("rendering/rendering_device/staging_buffer/block_size_kb");
  4104. staging_buffer_block_size = MAX(4u, staging_buffer_block_size);
  4105. staging_buffer_block_size *= 1024;
  4106. // Convert staging buffer size from MB.
  4107. staging_buffer_max_size = GLOBAL_GET("rendering/rendering_device/staging_buffer/max_size_mb");
  4108. staging_buffer_max_size = MAX(1u, staging_buffer_max_size);
  4109. staging_buffer_max_size *= 1024 * 1024;
  4110. if (staging_buffer_max_size < staging_buffer_block_size * 4) {
  4111. // Validate enough blocks.
  4112. staging_buffer_max_size = staging_buffer_block_size * 4;
  4113. }
  4114. texture_upload_region_size_px = GLOBAL_GET("rendering/rendering_device/staging_buffer/texture_upload_region_size_px");
  4115. texture_upload_region_size_px = nearest_power_of_2_templated(texture_upload_region_size_px);
  4116. // Ensure current staging block is valid and at least one per frame exists.
  4117. staging_buffer_current = 0;
  4118. staging_buffer_used = false;
  4119. for (uint32_t i = 0; i < frames.size(); i++) {
  4120. // Staging was never used, create a block.
  4121. err = _insert_staging_block();
  4122. ERR_CONTINUE(err != OK);
  4123. }
  4124. draw_list = nullptr;
  4125. compute_list = nullptr;
  4126. if (main_instance) {
  4127. // Only the instance that is not a local device and is also the singleton is allowed to manage a pipeline cache.
  4128. pipeline_cache_file_path = vformat("user://vulkan/pipelines.%s.%s",
  4129. OS::get_singleton()->get_current_rendering_method(),
  4130. device.name.validate_filename().replace(" ", "_").to_lower());
  4131. if (Engine::get_singleton()->is_editor_hint()) {
  4132. pipeline_cache_file_path += ".editor";
  4133. }
  4134. pipeline_cache_file_path += ".cache";
  4135. Vector<uint8_t> cache_data = _load_pipeline_cache();
  4136. pipeline_cache_enabled = driver->pipeline_cache_create(cache_data);
  4137. if (pipeline_cache_enabled) {
  4138. pipeline_cache_size = driver->pipeline_cache_query_size();
  4139. print_verbose(vformat("Startup PSO cache (%.1f MiB)", pipeline_cache_size / (1024.0f * 1024.0f)));
  4140. }
  4141. }
  4142. return OK;
  4143. }
  4144. Vector<uint8_t> RenderingDevice::_load_pipeline_cache() {
  4145. DirAccess::make_dir_recursive_absolute(pipeline_cache_file_path.get_base_dir());
  4146. if (FileAccess::exists(pipeline_cache_file_path)) {
  4147. Error file_error;
  4148. Vector<uint8_t> file_data = FileAccess::get_file_as_bytes(pipeline_cache_file_path, &file_error);
  4149. return file_data;
  4150. } else {
  4151. return Vector<uint8_t>();
  4152. }
  4153. }
  4154. void RenderingDevice::_update_pipeline_cache(bool p_closing) {
  4155. {
  4156. bool still_saving = pipeline_cache_save_task != WorkerThreadPool::INVALID_TASK_ID && !WorkerThreadPool::get_singleton()->is_task_completed(pipeline_cache_save_task);
  4157. if (still_saving) {
  4158. if (p_closing) {
  4159. WorkerThreadPool::get_singleton()->wait_for_task_completion(pipeline_cache_save_task);
  4160. pipeline_cache_save_task = WorkerThreadPool::INVALID_TASK_ID;
  4161. } else {
  4162. // We can't save until the currently running save is done. We'll retry next time; worst case, we'll save when exiting.
  4163. return;
  4164. }
  4165. }
  4166. }
  4167. {
  4168. size_t new_pipelines_cache_size = driver->pipeline_cache_query_size();
  4169. ERR_FAIL_COND(!new_pipelines_cache_size);
  4170. size_t difference = new_pipelines_cache_size - pipeline_cache_size;
  4171. bool must_save = false;
  4172. if (p_closing) {
  4173. must_save = difference > 0;
  4174. } else {
  4175. float save_interval = GLOBAL_GET("rendering/rendering_device/pipeline_cache/save_chunk_size_mb");
  4176. must_save = difference > 0 && difference / (1024.0f * 1024.0f) >= save_interval;
  4177. }
  4178. if (must_save) {
  4179. pipeline_cache_size = new_pipelines_cache_size;
  4180. } else {
  4181. return;
  4182. }
  4183. }
  4184. if (p_closing) {
  4185. _save_pipeline_cache(this);
  4186. } else {
  4187. pipeline_cache_save_task = WorkerThreadPool::get_singleton()->add_native_task(&_save_pipeline_cache, this, false, "PipelineCacheSave");
  4188. }
  4189. }
  4190. void RenderingDevice::_save_pipeline_cache(void *p_data) {
  4191. RenderingDevice *self = static_cast<RenderingDevice *>(p_data);
  4192. self->_thread_safe_.lock();
  4193. Vector<uint8_t> cache_blob = self->driver->pipeline_cache_serialize();
  4194. self->_thread_safe_.unlock();
  4195. if (cache_blob.size() == 0) {
  4196. return;
  4197. }
  4198. print_verbose(vformat("Updated PSO cache (%.1f MiB)", cache_blob.size() / (1024.0f * 1024.0f)));
  4199. Ref<FileAccess> f = FileAccess::open(self->pipeline_cache_file_path, FileAccess::WRITE, nullptr);
  4200. if (f.is_valid()) {
  4201. f->store_buffer(cache_blob);
  4202. }
  4203. }
  4204. template <typename T>
  4205. void RenderingDevice::_free_rids(T &p_owner, const char *p_type) {
  4206. List<RID> owned;
  4207. p_owner.get_owned_list(&owned);
  4208. if (owned.size()) {
  4209. if (owned.size() == 1) {
  4210. WARN_PRINT(vformat("1 RID of type \"%s\" was leaked.", p_type));
  4211. } else {
  4212. WARN_PRINT(vformat("%d RIDs of type \"%s\" were leaked.", owned.size(), p_type));
  4213. }
  4214. for (const RID &E : owned) {
  4215. #ifdef DEV_ENABLED
  4216. if (resource_names.has(E)) {
  4217. print_line(String(" - ") + resource_names[E]);
  4218. }
  4219. #endif
  4220. free(E);
  4221. }
  4222. }
  4223. }
  4224. void RenderingDevice::capture_timestamp(const String &p_name) {
  4225. ERR_FAIL_COND_MSG(draw_list != nullptr, "Capturing timestamps during draw list creation is not allowed. Offending timestamp was: " + p_name);
  4226. ERR_FAIL_COND_MSG(compute_list != nullptr, "Capturing timestamps during compute list creation is not allowed. Offending timestamp was: " + p_name);
  4227. ERR_FAIL_COND(frames[frame].timestamp_count >= max_timestamp_query_elements);
  4228. draw_graph.add_capture_timestamp(frames[frame].timestamp_pool, frames[frame].timestamp_count);
  4229. frames[frame].timestamp_names[frames[frame].timestamp_count] = p_name;
  4230. frames[frame].timestamp_cpu_values[frames[frame].timestamp_count] = OS::get_singleton()->get_ticks_usec();
  4231. frames[frame].timestamp_count++;
  4232. }
  4233. uint64_t RenderingDevice::get_driver_resource(DriverResource p_resource, RID p_rid, uint64_t p_index) {
  4234. _THREAD_SAFE_METHOD_
  4235. uint64_t driver_id = 0;
  4236. switch (p_resource) {
  4237. case DRIVER_RESOURCE_LOGICAL_DEVICE:
  4238. case DRIVER_RESOURCE_PHYSICAL_DEVICE:
  4239. case DRIVER_RESOURCE_TOPMOST_OBJECT:
  4240. case DRIVER_RESOURCE_COMMAND_QUEUE:
  4241. case DRIVER_RESOURCE_QUEUE_FAMILY:
  4242. break;
  4243. case DRIVER_RESOURCE_TEXTURE:
  4244. case DRIVER_RESOURCE_TEXTURE_VIEW:
  4245. case DRIVER_RESOURCE_TEXTURE_DATA_FORMAT: {
  4246. Texture *tex = texture_owner.get_or_null(p_rid);
  4247. ERR_FAIL_NULL_V(tex, 0);
  4248. driver_id = tex->driver_id;
  4249. } break;
  4250. case DRIVER_RESOURCE_SAMPLER: {
  4251. RDD::SamplerID *sampler_driver_id = sampler_owner.get_or_null(p_rid);
  4252. ERR_FAIL_NULL_V(sampler_driver_id, 0);
  4253. driver_id = *sampler_driver_id;
  4254. } break;
  4255. case DRIVER_RESOURCE_UNIFORM_SET: {
  4256. UniformSet *uniform_set = uniform_set_owner.get_or_null(p_rid);
  4257. ERR_FAIL_NULL_V(uniform_set, 0);
  4258. driver_id = uniform_set->driver_id;
  4259. } break;
  4260. case DRIVER_RESOURCE_BUFFER: {
  4261. Buffer *buffer = nullptr;
  4262. if (vertex_buffer_owner.owns(p_rid)) {
  4263. buffer = vertex_buffer_owner.get_or_null(p_rid);
  4264. } else if (index_buffer_owner.owns(p_rid)) {
  4265. buffer = index_buffer_owner.get_or_null(p_rid);
  4266. } else if (uniform_buffer_owner.owns(p_rid)) {
  4267. buffer = uniform_buffer_owner.get_or_null(p_rid);
  4268. } else if (texture_buffer_owner.owns(p_rid)) {
  4269. buffer = texture_buffer_owner.get_or_null(p_rid);
  4270. } else if (storage_buffer_owner.owns(p_rid)) {
  4271. buffer = storage_buffer_owner.get_or_null(p_rid);
  4272. }
  4273. ERR_FAIL_NULL_V(buffer, 0);
  4274. driver_id = buffer->driver_id;
  4275. } break;
  4276. case DRIVER_RESOURCE_COMPUTE_PIPELINE: {
  4277. ComputePipeline *compute_pipeline = compute_pipeline_owner.get_or_null(p_rid);
  4278. ERR_FAIL_NULL_V(compute_pipeline, 0);
  4279. driver_id = compute_pipeline->driver_id;
  4280. } break;
  4281. case DRIVER_RESOURCE_RENDER_PIPELINE: {
  4282. RenderPipeline *render_pipeline = render_pipeline_owner.get_or_null(p_rid);
  4283. ERR_FAIL_NULL_V(render_pipeline, 0);
  4284. driver_id = render_pipeline->driver_id;
  4285. } break;
  4286. default: {
  4287. ERR_FAIL_V(0);
  4288. } break;
  4289. }
  4290. return driver->get_resource_native_handle(p_resource, driver_id);
  4291. }
  4292. uint32_t RenderingDevice::get_captured_timestamps_count() const {
  4293. return frames[frame].timestamp_result_count;
  4294. }
  4295. uint64_t RenderingDevice::get_captured_timestamps_frame() const {
  4296. return frames[frame].index;
  4297. }
  4298. uint64_t RenderingDevice::get_captured_timestamp_gpu_time(uint32_t p_index) const {
  4299. ERR_FAIL_UNSIGNED_INDEX_V(p_index, frames[frame].timestamp_result_count, 0);
  4300. return driver->timestamp_query_result_to_time(frames[frame].timestamp_result_values[p_index]);
  4301. }
  4302. uint64_t RenderingDevice::get_captured_timestamp_cpu_time(uint32_t p_index) const {
  4303. ERR_FAIL_UNSIGNED_INDEX_V(p_index, frames[frame].timestamp_result_count, 0);
  4304. return frames[frame].timestamp_cpu_result_values[p_index];
  4305. }
  4306. String RenderingDevice::get_captured_timestamp_name(uint32_t p_index) const {
  4307. ERR_FAIL_UNSIGNED_INDEX_V(p_index, frames[frame].timestamp_result_count, String());
  4308. return frames[frame].timestamp_result_names[p_index];
  4309. }
  4310. uint64_t RenderingDevice::limit_get(Limit p_limit) const {
  4311. return driver->limit_get(p_limit);
  4312. }
  4313. void RenderingDevice::finalize() {
  4314. if (!frames.is_empty()) {
  4315. // Wait for all frames to have finished rendering.
  4316. _flush_and_stall_for_all_frames();
  4317. }
  4318. // Delete everything the graph has created.
  4319. draw_graph.finalize();
  4320. // Free all resources.
  4321. _free_rids(render_pipeline_owner, "Pipeline");
  4322. _free_rids(compute_pipeline_owner, "Compute");
  4323. _free_rids(uniform_set_owner, "UniformSet");
  4324. _free_rids(texture_buffer_owner, "TextureBuffer");
  4325. _free_rids(storage_buffer_owner, "StorageBuffer");
  4326. _free_rids(uniform_buffer_owner, "UniformBuffer");
  4327. _free_rids(shader_owner, "Shader");
  4328. _free_rids(index_array_owner, "IndexArray");
  4329. _free_rids(index_buffer_owner, "IndexBuffer");
  4330. _free_rids(vertex_array_owner, "VertexArray");
  4331. _free_rids(vertex_buffer_owner, "VertexBuffer");
  4332. _free_rids(framebuffer_owner, "Framebuffer");
  4333. _free_rids(sampler_owner, "Sampler");
  4334. {
  4335. // For textures it's a bit more difficult because they may be shared.
  4336. List<RID> owned;
  4337. texture_owner.get_owned_list(&owned);
  4338. if (owned.size()) {
  4339. if (owned.size() == 1) {
  4340. WARN_PRINT("1 RID of type \"Texture\" was leaked.");
  4341. } else {
  4342. WARN_PRINT(vformat("%d RIDs of type \"Texture\" were leaked.", owned.size()));
  4343. }
  4344. // Free shared first.
  4345. for (List<RID>::Element *E = owned.front(); E;) {
  4346. List<RID>::Element *N = E->next();
  4347. if (texture_is_shared(E->get())) {
  4348. #ifdef DEV_ENABLED
  4349. if (resource_names.has(E->get())) {
  4350. print_line(String(" - ") + resource_names[E->get()]);
  4351. }
  4352. #endif
  4353. free(E->get());
  4354. owned.erase(E);
  4355. }
  4356. E = N;
  4357. }
  4358. // Free non shared second, this will avoid an error trying to free unexisting textures due to dependencies.
  4359. for (const RID &E : owned) {
  4360. #ifdef DEV_ENABLED
  4361. if (resource_names.has(E)) {
  4362. print_line(String(" - ") + resource_names[E]);
  4363. }
  4364. #endif
  4365. free(E);
  4366. }
  4367. }
  4368. }
  4369. // Free everything pending.
  4370. for (uint32_t i = 0; i < frames.size(); i++) {
  4371. int f = (frame + i) % frames.size();
  4372. _free_pending_resources(f);
  4373. driver->command_pool_free(frames[i].command_pool);
  4374. driver->timestamp_query_pool_free(frames[i].timestamp_pool);
  4375. driver->semaphore_free(frames[i].setup_semaphore);
  4376. driver->semaphore_free(frames[i].draw_semaphore);
  4377. driver->fence_free(frames[i].draw_fence);
  4378. }
  4379. if (pipeline_cache_enabled) {
  4380. _update_pipeline_cache(true);
  4381. driver->pipeline_cache_free();
  4382. }
  4383. frames.clear();
  4384. for (int i = 0; i < staging_buffer_blocks.size(); i++) {
  4385. driver->buffer_free(staging_buffer_blocks[i].driver_id);
  4386. }
  4387. while (vertex_formats.size()) {
  4388. HashMap<VertexFormatID, VertexDescriptionCache>::Iterator temp = vertex_formats.begin();
  4389. driver->vertex_format_free(temp->value.driver_id);
  4390. vertex_formats.remove(temp);
  4391. }
  4392. for (KeyValue<FramebufferFormatID, FramebufferFormat> &E : framebuffer_formats) {
  4393. driver->render_pass_free(E.value.render_pass);
  4394. }
  4395. framebuffer_formats.clear();
  4396. // Delete the swap chains created for the screens.
  4397. for (const KeyValue<DisplayServer::WindowID, RDD::SwapChainID> &it : screen_swap_chains) {
  4398. driver->swap_chain_free(it.value);
  4399. }
  4400. screen_swap_chains.clear();
  4401. // Delete the command queues.
  4402. if (present_queue) {
  4403. if (main_queue != present_queue) {
  4404. // Only delete the present queue if it's unique.
  4405. driver->command_queue_free(present_queue);
  4406. }
  4407. present_queue = RDD::CommandQueueID();
  4408. }
  4409. if (main_queue) {
  4410. driver->command_queue_free(main_queue);
  4411. main_queue = RDD::CommandQueueID();
  4412. }
  4413. // Delete the driver once everything else has been deleted.
  4414. if (driver != nullptr) {
  4415. context->driver_free(driver);
  4416. driver = nullptr;
  4417. }
  4418. // All these should be clear at this point.
  4419. ERR_FAIL_COND(dependency_map.size());
  4420. ERR_FAIL_COND(reverse_dependency_map.size());
  4421. }
  4422. RenderingDevice *RenderingDevice::create_local_device() {
  4423. RenderingDevice *rd = memnew(RenderingDevice);
  4424. rd->initialize(context);
  4425. return rd;
  4426. }
  4427. bool RenderingDevice::has_feature(const Features p_feature) const {
  4428. return driver->has_feature(p_feature);
  4429. }
  4430. void RenderingDevice::_bind_methods() {
  4431. ClassDB::bind_method(D_METHOD("texture_create", "format", "view", "data"), &RenderingDevice::_texture_create, DEFVAL(Array()));
  4432. ClassDB::bind_method(D_METHOD("texture_create_shared", "view", "with_texture"), &RenderingDevice::_texture_create_shared);
  4433. 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));
  4434. ClassDB::bind_method(D_METHOD("texture_create_from_extension", "type", "format", "samples", "usage_flags", "image", "width", "height", "depth", "layers"), &RenderingDevice::texture_create_from_extension);
  4435. ClassDB::bind_method(D_METHOD("texture_update", "texture", "layer", "data"), &RenderingDevice::texture_update);
  4436. ClassDB::bind_method(D_METHOD("texture_get_data", "texture", "layer"), &RenderingDevice::texture_get_data);
  4437. ClassDB::bind_method(D_METHOD("texture_is_format_supported_for_usage", "format", "usage_flags"), &RenderingDevice::texture_is_format_supported_for_usage);
  4438. ClassDB::bind_method(D_METHOD("texture_is_shared", "texture"), &RenderingDevice::texture_is_shared);
  4439. ClassDB::bind_method(D_METHOD("texture_is_valid", "texture"), &RenderingDevice::texture_is_valid);
  4440. 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);
  4441. ClassDB::bind_method(D_METHOD("texture_clear", "texture", "color", "base_mipmap", "mipmap_count", "base_layer", "layer_count"), &RenderingDevice::texture_clear);
  4442. ClassDB::bind_method(D_METHOD("texture_resolve_multisample", "from_texture", "to_texture"), &RenderingDevice::texture_resolve_multisample);
  4443. ClassDB::bind_method(D_METHOD("texture_get_format", "texture"), &RenderingDevice::_texture_get_format);
  4444. #ifndef DISABLE_DEPRECATED
  4445. ClassDB::bind_method(D_METHOD("texture_get_native_handle", "texture"), &RenderingDevice::texture_get_native_handle);
  4446. #endif
  4447. ClassDB::bind_method(D_METHOD("framebuffer_format_create", "attachments", "view_count"), &RenderingDevice::_framebuffer_format_create, DEFVAL(1));
  4448. ClassDB::bind_method(D_METHOD("framebuffer_format_create_multipass", "attachments", "passes", "view_count"), &RenderingDevice::_framebuffer_format_create_multipass, DEFVAL(1));
  4449. ClassDB::bind_method(D_METHOD("framebuffer_format_create_empty", "samples"), &RenderingDevice::framebuffer_format_create_empty, DEFVAL(TEXTURE_SAMPLES_1));
  4450. ClassDB::bind_method(D_METHOD("framebuffer_format_get_texture_samples", "format", "render_pass"), &RenderingDevice::framebuffer_format_get_texture_samples, DEFVAL(0));
  4451. ClassDB::bind_method(D_METHOD("framebuffer_create", "textures", "validate_with_format", "view_count"), &RenderingDevice::_framebuffer_create, DEFVAL(INVALID_FORMAT_ID), DEFVAL(1));
  4452. 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));
  4453. 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));
  4454. ClassDB::bind_method(D_METHOD("framebuffer_get_format", "framebuffer"), &RenderingDevice::framebuffer_get_format);
  4455. ClassDB::bind_method(D_METHOD("framebuffer_is_valid", "framebuffer"), &RenderingDevice::framebuffer_is_valid);
  4456. ClassDB::bind_method(D_METHOD("sampler_create", "state"), &RenderingDevice::_sampler_create);
  4457. ClassDB::bind_method(D_METHOD("sampler_is_format_supported_for_filter", "format", "sampler_filter"), &RenderingDevice::sampler_is_format_supported_for_filter);
  4458. ClassDB::bind_method(D_METHOD("vertex_buffer_create", "size_bytes", "data", "use_as_storage"), &RenderingDevice::vertex_buffer_create, DEFVAL(Vector<uint8_t>()), DEFVAL(false));
  4459. ClassDB::bind_method(D_METHOD("vertex_format_create", "vertex_descriptions"), &RenderingDevice::_vertex_format_create);
  4460. ClassDB::bind_method(D_METHOD("vertex_array_create", "vertex_count", "vertex_format", "src_buffers", "offsets"), &RenderingDevice::_vertex_array_create, DEFVAL(Vector<int64_t>()));
  4461. 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));
  4462. ClassDB::bind_method(D_METHOD("index_array_create", "index_buffer", "index_offset", "index_count"), &RenderingDevice::index_array_create);
  4463. ClassDB::bind_method(D_METHOD("shader_compile_spirv_from_source", "shader_source", "allow_cache"), &RenderingDevice::_shader_compile_spirv_from_source, DEFVAL(true));
  4464. ClassDB::bind_method(D_METHOD("shader_compile_binary_from_spirv", "spirv_data", "name"), &RenderingDevice::_shader_compile_binary_from_spirv, DEFVAL(""));
  4465. ClassDB::bind_method(D_METHOD("shader_create_from_spirv", "spirv_data", "name"), &RenderingDevice::_shader_create_from_spirv, DEFVAL(""));
  4466. ClassDB::bind_method(D_METHOD("shader_create_from_bytecode", "binary_data", "placeholder_rid"), &RenderingDevice::shader_create_from_bytecode, DEFVAL(RID()));
  4467. ClassDB::bind_method(D_METHOD("shader_create_placeholder"), &RenderingDevice::shader_create_placeholder);
  4468. ClassDB::bind_method(D_METHOD("shader_get_vertex_input_attribute_mask", "shader"), &RenderingDevice::shader_get_vertex_input_attribute_mask);
  4469. ClassDB::bind_method(D_METHOD("uniform_buffer_create", "size_bytes", "data"), &RenderingDevice::uniform_buffer_create, DEFVAL(Vector<uint8_t>()));
  4470. ClassDB::bind_method(D_METHOD("storage_buffer_create", "size_bytes", "data", "usage"), &RenderingDevice::storage_buffer_create, DEFVAL(Vector<uint8_t>()), DEFVAL(0));
  4471. ClassDB::bind_method(D_METHOD("texture_buffer_create", "size_bytes", "format", "data"), &RenderingDevice::texture_buffer_create, DEFVAL(Vector<uint8_t>()));
  4472. ClassDB::bind_method(D_METHOD("uniform_set_create", "uniforms", "shader", "shader_set"), &RenderingDevice::_uniform_set_create);
  4473. ClassDB::bind_method(D_METHOD("uniform_set_is_valid", "uniform_set"), &RenderingDevice::uniform_set_is_valid);
  4474. ClassDB::bind_method(D_METHOD("buffer_copy", "src_buffer", "dst_buffer", "src_offset", "dst_offset", "size"), &RenderingDevice::buffer_copy);
  4475. ClassDB::bind_method(D_METHOD("buffer_update", "buffer", "offset", "size_bytes", "data"), &RenderingDevice::_buffer_update_bind);
  4476. ClassDB::bind_method(D_METHOD("buffer_clear", "buffer", "offset", "size_bytes"), &RenderingDevice::buffer_clear);
  4477. ClassDB::bind_method(D_METHOD("buffer_get_data", "buffer", "offset_bytes", "size_bytes"), &RenderingDevice::buffer_get_data, DEFVAL(0), DEFVAL(0));
  4478. 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>()));
  4479. ClassDB::bind_method(D_METHOD("render_pipeline_is_valid", "render_pipeline"), &RenderingDevice::render_pipeline_is_valid);
  4480. ClassDB::bind_method(D_METHOD("compute_pipeline_create", "shader", "specialization_constants"), &RenderingDevice::_compute_pipeline_create, DEFVAL(TypedArray<RDPipelineSpecializationConstant>()));
  4481. ClassDB::bind_method(D_METHOD("compute_pipeline_is_valid", "compute_pipeline"), &RenderingDevice::compute_pipeline_is_valid);
  4482. ClassDB::bind_method(D_METHOD("screen_get_width", "screen"), &RenderingDevice::screen_get_width, DEFVAL(DisplayServer::MAIN_WINDOW_ID));
  4483. ClassDB::bind_method(D_METHOD("screen_get_height", "screen"), &RenderingDevice::screen_get_height, DEFVAL(DisplayServer::MAIN_WINDOW_ID));
  4484. ClassDB::bind_method(D_METHOD("screen_get_framebuffer_format", "screen"), &RenderingDevice::screen_get_framebuffer_format, DEFVAL(DisplayServer::MAIN_WINDOW_ID));
  4485. 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()));
  4486. 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()));
  4487. #ifndef DISABLE_DEPRECATED
  4488. 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>()));
  4489. #endif
  4490. ClassDB::bind_method(D_METHOD("draw_list_set_blend_constants", "draw_list", "color"), &RenderingDevice::draw_list_set_blend_constants);
  4491. ClassDB::bind_method(D_METHOD("draw_list_bind_render_pipeline", "draw_list", "render_pipeline"), &RenderingDevice::draw_list_bind_render_pipeline);
  4492. ClassDB::bind_method(D_METHOD("draw_list_bind_uniform_set", "draw_list", "uniform_set", "set_index"), &RenderingDevice::draw_list_bind_uniform_set);
  4493. ClassDB::bind_method(D_METHOD("draw_list_bind_vertex_array", "draw_list", "vertex_array"), &RenderingDevice::draw_list_bind_vertex_array);
  4494. ClassDB::bind_method(D_METHOD("draw_list_bind_index_array", "draw_list", "index_array"), &RenderingDevice::draw_list_bind_index_array);
  4495. ClassDB::bind_method(D_METHOD("draw_list_set_push_constant", "draw_list", "buffer", "size_bytes"), &RenderingDevice::_draw_list_set_push_constant);
  4496. ClassDB::bind_method(D_METHOD("draw_list_draw", "draw_list", "use_indices", "instances", "procedural_vertex_count"), &RenderingDevice::draw_list_draw, DEFVAL(0));
  4497. ClassDB::bind_method(D_METHOD("draw_list_enable_scissor", "draw_list", "rect"), &RenderingDevice::draw_list_enable_scissor, DEFVAL(Rect2()));
  4498. ClassDB::bind_method(D_METHOD("draw_list_disable_scissor", "draw_list"), &RenderingDevice::draw_list_disable_scissor);
  4499. ClassDB::bind_method(D_METHOD("draw_list_switch_to_next_pass"), &RenderingDevice::draw_list_switch_to_next_pass);
  4500. #ifndef DISABLE_DEPRECATED
  4501. ClassDB::bind_method(D_METHOD("draw_list_switch_to_next_pass_split", "splits"), &RenderingDevice::_draw_list_switch_to_next_pass_split);
  4502. #endif
  4503. ClassDB::bind_method(D_METHOD("draw_list_end"), &RenderingDevice::draw_list_end);
  4504. ClassDB::bind_method(D_METHOD("compute_list_begin"), &RenderingDevice::compute_list_begin);
  4505. ClassDB::bind_method(D_METHOD("compute_list_bind_compute_pipeline", "compute_list", "compute_pipeline"), &RenderingDevice::compute_list_bind_compute_pipeline);
  4506. ClassDB::bind_method(D_METHOD("compute_list_set_push_constant", "compute_list", "buffer", "size_bytes"), &RenderingDevice::_compute_list_set_push_constant);
  4507. ClassDB::bind_method(D_METHOD("compute_list_bind_uniform_set", "compute_list", "uniform_set", "set_index"), &RenderingDevice::compute_list_bind_uniform_set);
  4508. ClassDB::bind_method(D_METHOD("compute_list_dispatch", "compute_list", "x_groups", "y_groups", "z_groups"), &RenderingDevice::compute_list_dispatch);
  4509. ClassDB::bind_method(D_METHOD("compute_list_add_barrier", "compute_list"), &RenderingDevice::compute_list_add_barrier);
  4510. ClassDB::bind_method(D_METHOD("compute_list_end"), &RenderingDevice::compute_list_end);
  4511. ClassDB::bind_method(D_METHOD("free_rid", "rid"), &RenderingDevice::free);
  4512. ClassDB::bind_method(D_METHOD("capture_timestamp", "name"), &RenderingDevice::capture_timestamp);
  4513. ClassDB::bind_method(D_METHOD("get_captured_timestamps_count"), &RenderingDevice::get_captured_timestamps_count);
  4514. ClassDB::bind_method(D_METHOD("get_captured_timestamps_frame"), &RenderingDevice::get_captured_timestamps_frame);
  4515. ClassDB::bind_method(D_METHOD("get_captured_timestamp_gpu_time", "index"), &RenderingDevice::get_captured_timestamp_gpu_time);
  4516. ClassDB::bind_method(D_METHOD("get_captured_timestamp_cpu_time", "index"), &RenderingDevice::get_captured_timestamp_cpu_time);
  4517. ClassDB::bind_method(D_METHOD("get_captured_timestamp_name", "index"), &RenderingDevice::get_captured_timestamp_name);
  4518. ClassDB::bind_method(D_METHOD("limit_get", "limit"), &RenderingDevice::limit_get);
  4519. ClassDB::bind_method(D_METHOD("get_frame_delay"), &RenderingDevice::get_frame_delay);
  4520. ClassDB::bind_method(D_METHOD("submit"), &RenderingDevice::submit);
  4521. ClassDB::bind_method(D_METHOD("sync"), &RenderingDevice::sync);
  4522. #ifndef DISABLE_DEPRECATED
  4523. ClassDB::bind_method(D_METHOD("barrier", "from", "to"), &RenderingDevice::barrier, DEFVAL(BARRIER_MASK_ALL_BARRIERS), DEFVAL(BARRIER_MASK_ALL_BARRIERS));
  4524. ClassDB::bind_method(D_METHOD("full_barrier"), &RenderingDevice::full_barrier);
  4525. #endif
  4526. ClassDB::bind_method(D_METHOD("create_local_device"), &RenderingDevice::create_local_device);
  4527. ClassDB::bind_method(D_METHOD("set_resource_name", "id", "name"), &RenderingDevice::set_resource_name);
  4528. ClassDB::bind_method(D_METHOD("draw_command_begin_label", "name", "color"), &RenderingDevice::draw_command_begin_label);
  4529. #ifndef DISABLE_DEPRECATED
  4530. ClassDB::bind_method(D_METHOD("draw_command_insert_label", "name", "color"), &RenderingDevice::draw_command_insert_label);
  4531. #endif
  4532. ClassDB::bind_method(D_METHOD("draw_command_end_label"), &RenderingDevice::draw_command_end_label);
  4533. ClassDB::bind_method(D_METHOD("get_device_vendor_name"), &RenderingDevice::get_device_vendor_name);
  4534. ClassDB::bind_method(D_METHOD("get_device_name"), &RenderingDevice::get_device_name);
  4535. ClassDB::bind_method(D_METHOD("get_device_pipeline_cache_uuid"), &RenderingDevice::get_device_pipeline_cache_uuid);
  4536. ClassDB::bind_method(D_METHOD("get_memory_usage", "type"), &RenderingDevice::get_memory_usage);
  4537. ClassDB::bind_method(D_METHOD("get_driver_resource", "resource", "rid", "index"), &RenderingDevice::get_driver_resource);
  4538. BIND_ENUM_CONSTANT(DEVICE_TYPE_OTHER);
  4539. BIND_ENUM_CONSTANT(DEVICE_TYPE_INTEGRATED_GPU);
  4540. BIND_ENUM_CONSTANT(DEVICE_TYPE_DISCRETE_GPU);
  4541. BIND_ENUM_CONSTANT(DEVICE_TYPE_VIRTUAL_GPU);
  4542. BIND_ENUM_CONSTANT(DEVICE_TYPE_CPU);
  4543. BIND_ENUM_CONSTANT(DEVICE_TYPE_MAX);
  4544. BIND_ENUM_CONSTANT(DRIVER_RESOURCE_LOGICAL_DEVICE);
  4545. BIND_ENUM_CONSTANT(DRIVER_RESOURCE_PHYSICAL_DEVICE);
  4546. BIND_ENUM_CONSTANT(DRIVER_RESOURCE_TOPMOST_OBJECT);
  4547. BIND_ENUM_CONSTANT(DRIVER_RESOURCE_COMMAND_QUEUE);
  4548. BIND_ENUM_CONSTANT(DRIVER_RESOURCE_QUEUE_FAMILY);
  4549. BIND_ENUM_CONSTANT(DRIVER_RESOURCE_TEXTURE);
  4550. BIND_ENUM_CONSTANT(DRIVER_RESOURCE_TEXTURE_VIEW);
  4551. BIND_ENUM_CONSTANT(DRIVER_RESOURCE_TEXTURE_DATA_FORMAT);
  4552. BIND_ENUM_CONSTANT(DRIVER_RESOURCE_SAMPLER);
  4553. BIND_ENUM_CONSTANT(DRIVER_RESOURCE_UNIFORM_SET);
  4554. BIND_ENUM_CONSTANT(DRIVER_RESOURCE_BUFFER);
  4555. BIND_ENUM_CONSTANT(DRIVER_RESOURCE_COMPUTE_PIPELINE);
  4556. BIND_ENUM_CONSTANT(DRIVER_RESOURCE_RENDER_PIPELINE);
  4557. #ifndef DISABLE_DEPRECATED
  4558. BIND_ENUM_CONSTANT(DRIVER_RESOURCE_VULKAN_DEVICE);
  4559. BIND_ENUM_CONSTANT(DRIVER_RESOURCE_VULKAN_PHYSICAL_DEVICE);
  4560. BIND_ENUM_CONSTANT(DRIVER_RESOURCE_VULKAN_INSTANCE);
  4561. BIND_ENUM_CONSTANT(DRIVER_RESOURCE_VULKAN_QUEUE);
  4562. BIND_ENUM_CONSTANT(DRIVER_RESOURCE_VULKAN_QUEUE_FAMILY_INDEX);
  4563. BIND_ENUM_CONSTANT(DRIVER_RESOURCE_VULKAN_IMAGE);
  4564. BIND_ENUM_CONSTANT(DRIVER_RESOURCE_VULKAN_IMAGE_VIEW);
  4565. BIND_ENUM_CONSTANT(DRIVER_RESOURCE_VULKAN_IMAGE_NATIVE_TEXTURE_FORMAT);
  4566. BIND_ENUM_CONSTANT(DRIVER_RESOURCE_VULKAN_SAMPLER);
  4567. BIND_ENUM_CONSTANT(DRIVER_RESOURCE_VULKAN_DESCRIPTOR_SET);
  4568. BIND_ENUM_CONSTANT(DRIVER_RESOURCE_VULKAN_BUFFER);
  4569. BIND_ENUM_CONSTANT(DRIVER_RESOURCE_VULKAN_COMPUTE_PIPELINE);
  4570. BIND_ENUM_CONSTANT(DRIVER_RESOURCE_VULKAN_RENDER_PIPELINE);
  4571. #endif
  4572. BIND_ENUM_CONSTANT(DATA_FORMAT_R4G4_UNORM_PACK8);
  4573. BIND_ENUM_CONSTANT(DATA_FORMAT_R4G4B4A4_UNORM_PACK16);
  4574. BIND_ENUM_CONSTANT(DATA_FORMAT_B4G4R4A4_UNORM_PACK16);
  4575. BIND_ENUM_CONSTANT(DATA_FORMAT_R5G6B5_UNORM_PACK16);
  4576. BIND_ENUM_CONSTANT(DATA_FORMAT_B5G6R5_UNORM_PACK16);
  4577. BIND_ENUM_CONSTANT(DATA_FORMAT_R5G5B5A1_UNORM_PACK16);
  4578. BIND_ENUM_CONSTANT(DATA_FORMAT_B5G5R5A1_UNORM_PACK16);
  4579. BIND_ENUM_CONSTANT(DATA_FORMAT_A1R5G5B5_UNORM_PACK16);
  4580. BIND_ENUM_CONSTANT(DATA_FORMAT_R8_UNORM);
  4581. BIND_ENUM_CONSTANT(DATA_FORMAT_R8_SNORM);
  4582. BIND_ENUM_CONSTANT(DATA_FORMAT_R8_USCALED);
  4583. BIND_ENUM_CONSTANT(DATA_FORMAT_R8_SSCALED);
  4584. BIND_ENUM_CONSTANT(DATA_FORMAT_R8_UINT);
  4585. BIND_ENUM_CONSTANT(DATA_FORMAT_R8_SINT);
  4586. BIND_ENUM_CONSTANT(DATA_FORMAT_R8_SRGB);
  4587. BIND_ENUM_CONSTANT(DATA_FORMAT_R8G8_UNORM);
  4588. BIND_ENUM_CONSTANT(DATA_FORMAT_R8G8_SNORM);
  4589. BIND_ENUM_CONSTANT(DATA_FORMAT_R8G8_USCALED);
  4590. BIND_ENUM_CONSTANT(DATA_FORMAT_R8G8_SSCALED);
  4591. BIND_ENUM_CONSTANT(DATA_FORMAT_R8G8_UINT);
  4592. BIND_ENUM_CONSTANT(DATA_FORMAT_R8G8_SINT);
  4593. BIND_ENUM_CONSTANT(DATA_FORMAT_R8G8_SRGB);
  4594. BIND_ENUM_CONSTANT(DATA_FORMAT_R8G8B8_UNORM);
  4595. BIND_ENUM_CONSTANT(DATA_FORMAT_R8G8B8_SNORM);
  4596. BIND_ENUM_CONSTANT(DATA_FORMAT_R8G8B8_USCALED);
  4597. BIND_ENUM_CONSTANT(DATA_FORMAT_R8G8B8_SSCALED);
  4598. BIND_ENUM_CONSTANT(DATA_FORMAT_R8G8B8_UINT);
  4599. BIND_ENUM_CONSTANT(DATA_FORMAT_R8G8B8_SINT);
  4600. BIND_ENUM_CONSTANT(DATA_FORMAT_R8G8B8_SRGB);
  4601. BIND_ENUM_CONSTANT(DATA_FORMAT_B8G8R8_UNORM);
  4602. BIND_ENUM_CONSTANT(DATA_FORMAT_B8G8R8_SNORM);
  4603. BIND_ENUM_CONSTANT(DATA_FORMAT_B8G8R8_USCALED);
  4604. BIND_ENUM_CONSTANT(DATA_FORMAT_B8G8R8_SSCALED);
  4605. BIND_ENUM_CONSTANT(DATA_FORMAT_B8G8R8_UINT);
  4606. BIND_ENUM_CONSTANT(DATA_FORMAT_B8G8R8_SINT);
  4607. BIND_ENUM_CONSTANT(DATA_FORMAT_B8G8R8_SRGB);
  4608. BIND_ENUM_CONSTANT(DATA_FORMAT_R8G8B8A8_UNORM);
  4609. BIND_ENUM_CONSTANT(DATA_FORMAT_R8G8B8A8_SNORM);
  4610. BIND_ENUM_CONSTANT(DATA_FORMAT_R8G8B8A8_USCALED);
  4611. BIND_ENUM_CONSTANT(DATA_FORMAT_R8G8B8A8_SSCALED);
  4612. BIND_ENUM_CONSTANT(DATA_FORMAT_R8G8B8A8_UINT);
  4613. BIND_ENUM_CONSTANT(DATA_FORMAT_R8G8B8A8_SINT);
  4614. BIND_ENUM_CONSTANT(DATA_FORMAT_R8G8B8A8_SRGB);
  4615. BIND_ENUM_CONSTANT(DATA_FORMAT_B8G8R8A8_UNORM);
  4616. BIND_ENUM_CONSTANT(DATA_FORMAT_B8G8R8A8_SNORM);
  4617. BIND_ENUM_CONSTANT(DATA_FORMAT_B8G8R8A8_USCALED);
  4618. BIND_ENUM_CONSTANT(DATA_FORMAT_B8G8R8A8_SSCALED);
  4619. BIND_ENUM_CONSTANT(DATA_FORMAT_B8G8R8A8_UINT);
  4620. BIND_ENUM_CONSTANT(DATA_FORMAT_B8G8R8A8_SINT);
  4621. BIND_ENUM_CONSTANT(DATA_FORMAT_B8G8R8A8_SRGB);
  4622. BIND_ENUM_CONSTANT(DATA_FORMAT_A8B8G8R8_UNORM_PACK32);
  4623. BIND_ENUM_CONSTANT(DATA_FORMAT_A8B8G8R8_SNORM_PACK32);
  4624. BIND_ENUM_CONSTANT(DATA_FORMAT_A8B8G8R8_USCALED_PACK32);
  4625. BIND_ENUM_CONSTANT(DATA_FORMAT_A8B8G8R8_SSCALED_PACK32);
  4626. BIND_ENUM_CONSTANT(DATA_FORMAT_A8B8G8R8_UINT_PACK32);
  4627. BIND_ENUM_CONSTANT(DATA_FORMAT_A8B8G8R8_SINT_PACK32);
  4628. BIND_ENUM_CONSTANT(DATA_FORMAT_A8B8G8R8_SRGB_PACK32);
  4629. BIND_ENUM_CONSTANT(DATA_FORMAT_A2R10G10B10_UNORM_PACK32);
  4630. BIND_ENUM_CONSTANT(DATA_FORMAT_A2R10G10B10_SNORM_PACK32);
  4631. BIND_ENUM_CONSTANT(DATA_FORMAT_A2R10G10B10_USCALED_PACK32);
  4632. BIND_ENUM_CONSTANT(DATA_FORMAT_A2R10G10B10_SSCALED_PACK32);
  4633. BIND_ENUM_CONSTANT(DATA_FORMAT_A2R10G10B10_UINT_PACK32);
  4634. BIND_ENUM_CONSTANT(DATA_FORMAT_A2R10G10B10_SINT_PACK32);
  4635. BIND_ENUM_CONSTANT(DATA_FORMAT_A2B10G10R10_UNORM_PACK32);
  4636. BIND_ENUM_CONSTANT(DATA_FORMAT_A2B10G10R10_SNORM_PACK32);
  4637. BIND_ENUM_CONSTANT(DATA_FORMAT_A2B10G10R10_USCALED_PACK32);
  4638. BIND_ENUM_CONSTANT(DATA_FORMAT_A2B10G10R10_SSCALED_PACK32);
  4639. BIND_ENUM_CONSTANT(DATA_FORMAT_A2B10G10R10_UINT_PACK32);
  4640. BIND_ENUM_CONSTANT(DATA_FORMAT_A2B10G10R10_SINT_PACK32);
  4641. BIND_ENUM_CONSTANT(DATA_FORMAT_R16_UNORM);
  4642. BIND_ENUM_CONSTANT(DATA_FORMAT_R16_SNORM);
  4643. BIND_ENUM_CONSTANT(DATA_FORMAT_R16_USCALED);
  4644. BIND_ENUM_CONSTANT(DATA_FORMAT_R16_SSCALED);
  4645. BIND_ENUM_CONSTANT(DATA_FORMAT_R16_UINT);
  4646. BIND_ENUM_CONSTANT(DATA_FORMAT_R16_SINT);
  4647. BIND_ENUM_CONSTANT(DATA_FORMAT_R16_SFLOAT);
  4648. BIND_ENUM_CONSTANT(DATA_FORMAT_R16G16_UNORM);
  4649. BIND_ENUM_CONSTANT(DATA_FORMAT_R16G16_SNORM);
  4650. BIND_ENUM_CONSTANT(DATA_FORMAT_R16G16_USCALED);
  4651. BIND_ENUM_CONSTANT(DATA_FORMAT_R16G16_SSCALED);
  4652. BIND_ENUM_CONSTANT(DATA_FORMAT_R16G16_UINT);
  4653. BIND_ENUM_CONSTANT(DATA_FORMAT_R16G16_SINT);
  4654. BIND_ENUM_CONSTANT(DATA_FORMAT_R16G16_SFLOAT);
  4655. BIND_ENUM_CONSTANT(DATA_FORMAT_R16G16B16_UNORM);
  4656. BIND_ENUM_CONSTANT(DATA_FORMAT_R16G16B16_SNORM);
  4657. BIND_ENUM_CONSTANT(DATA_FORMAT_R16G16B16_USCALED);
  4658. BIND_ENUM_CONSTANT(DATA_FORMAT_R16G16B16_SSCALED);
  4659. BIND_ENUM_CONSTANT(DATA_FORMAT_R16G16B16_UINT);
  4660. BIND_ENUM_CONSTANT(DATA_FORMAT_R16G16B16_SINT);
  4661. BIND_ENUM_CONSTANT(DATA_FORMAT_R16G16B16_SFLOAT);
  4662. BIND_ENUM_CONSTANT(DATA_FORMAT_R16G16B16A16_UNORM);
  4663. BIND_ENUM_CONSTANT(DATA_FORMAT_R16G16B16A16_SNORM);
  4664. BIND_ENUM_CONSTANT(DATA_FORMAT_R16G16B16A16_USCALED);
  4665. BIND_ENUM_CONSTANT(DATA_FORMAT_R16G16B16A16_SSCALED);
  4666. BIND_ENUM_CONSTANT(DATA_FORMAT_R16G16B16A16_UINT);
  4667. BIND_ENUM_CONSTANT(DATA_FORMAT_R16G16B16A16_SINT);
  4668. BIND_ENUM_CONSTANT(DATA_FORMAT_R16G16B16A16_SFLOAT);
  4669. BIND_ENUM_CONSTANT(DATA_FORMAT_R32_UINT);
  4670. BIND_ENUM_CONSTANT(DATA_FORMAT_R32_SINT);
  4671. BIND_ENUM_CONSTANT(DATA_FORMAT_R32_SFLOAT);
  4672. BIND_ENUM_CONSTANT(DATA_FORMAT_R32G32_UINT);
  4673. BIND_ENUM_CONSTANT(DATA_FORMAT_R32G32_SINT);
  4674. BIND_ENUM_CONSTANT(DATA_FORMAT_R32G32_SFLOAT);
  4675. BIND_ENUM_CONSTANT(DATA_FORMAT_R32G32B32_UINT);
  4676. BIND_ENUM_CONSTANT(DATA_FORMAT_R32G32B32_SINT);
  4677. BIND_ENUM_CONSTANT(DATA_FORMAT_R32G32B32_SFLOAT);
  4678. BIND_ENUM_CONSTANT(DATA_FORMAT_R32G32B32A32_UINT);
  4679. BIND_ENUM_CONSTANT(DATA_FORMAT_R32G32B32A32_SINT);
  4680. BIND_ENUM_CONSTANT(DATA_FORMAT_R32G32B32A32_SFLOAT);
  4681. BIND_ENUM_CONSTANT(DATA_FORMAT_R64_UINT);
  4682. BIND_ENUM_CONSTANT(DATA_FORMAT_R64_SINT);
  4683. BIND_ENUM_CONSTANT(DATA_FORMAT_R64_SFLOAT);
  4684. BIND_ENUM_CONSTANT(DATA_FORMAT_R64G64_UINT);
  4685. BIND_ENUM_CONSTANT(DATA_FORMAT_R64G64_SINT);
  4686. BIND_ENUM_CONSTANT(DATA_FORMAT_R64G64_SFLOAT);
  4687. BIND_ENUM_CONSTANT(DATA_FORMAT_R64G64B64_UINT);
  4688. BIND_ENUM_CONSTANT(DATA_FORMAT_R64G64B64_SINT);
  4689. BIND_ENUM_CONSTANT(DATA_FORMAT_R64G64B64_SFLOAT);
  4690. BIND_ENUM_CONSTANT(DATA_FORMAT_R64G64B64A64_UINT);
  4691. BIND_ENUM_CONSTANT(DATA_FORMAT_R64G64B64A64_SINT);
  4692. BIND_ENUM_CONSTANT(DATA_FORMAT_R64G64B64A64_SFLOAT);
  4693. BIND_ENUM_CONSTANT(DATA_FORMAT_B10G11R11_UFLOAT_PACK32);
  4694. BIND_ENUM_CONSTANT(DATA_FORMAT_E5B9G9R9_UFLOAT_PACK32);
  4695. BIND_ENUM_CONSTANT(DATA_FORMAT_D16_UNORM);
  4696. BIND_ENUM_CONSTANT(DATA_FORMAT_X8_D24_UNORM_PACK32);
  4697. BIND_ENUM_CONSTANT(DATA_FORMAT_D32_SFLOAT);
  4698. BIND_ENUM_CONSTANT(DATA_FORMAT_S8_UINT);
  4699. BIND_ENUM_CONSTANT(DATA_FORMAT_D16_UNORM_S8_UINT);
  4700. BIND_ENUM_CONSTANT(DATA_FORMAT_D24_UNORM_S8_UINT);
  4701. BIND_ENUM_CONSTANT(DATA_FORMAT_D32_SFLOAT_S8_UINT);
  4702. BIND_ENUM_CONSTANT(DATA_FORMAT_BC1_RGB_UNORM_BLOCK);
  4703. BIND_ENUM_CONSTANT(DATA_FORMAT_BC1_RGB_SRGB_BLOCK);
  4704. BIND_ENUM_CONSTANT(DATA_FORMAT_BC1_RGBA_UNORM_BLOCK);
  4705. BIND_ENUM_CONSTANT(DATA_FORMAT_BC1_RGBA_SRGB_BLOCK);
  4706. BIND_ENUM_CONSTANT(DATA_FORMAT_BC2_UNORM_BLOCK);
  4707. BIND_ENUM_CONSTANT(DATA_FORMAT_BC2_SRGB_BLOCK);
  4708. BIND_ENUM_CONSTANT(DATA_FORMAT_BC3_UNORM_BLOCK);
  4709. BIND_ENUM_CONSTANT(DATA_FORMAT_BC3_SRGB_BLOCK);
  4710. BIND_ENUM_CONSTANT(DATA_FORMAT_BC4_UNORM_BLOCK);
  4711. BIND_ENUM_CONSTANT(DATA_FORMAT_BC4_SNORM_BLOCK);
  4712. BIND_ENUM_CONSTANT(DATA_FORMAT_BC5_UNORM_BLOCK);
  4713. BIND_ENUM_CONSTANT(DATA_FORMAT_BC5_SNORM_BLOCK);
  4714. BIND_ENUM_CONSTANT(DATA_FORMAT_BC6H_UFLOAT_BLOCK);
  4715. BIND_ENUM_CONSTANT(DATA_FORMAT_BC6H_SFLOAT_BLOCK);
  4716. BIND_ENUM_CONSTANT(DATA_FORMAT_BC7_UNORM_BLOCK);
  4717. BIND_ENUM_CONSTANT(DATA_FORMAT_BC7_SRGB_BLOCK);
  4718. BIND_ENUM_CONSTANT(DATA_FORMAT_ETC2_R8G8B8_UNORM_BLOCK);
  4719. BIND_ENUM_CONSTANT(DATA_FORMAT_ETC2_R8G8B8_SRGB_BLOCK);
  4720. BIND_ENUM_CONSTANT(DATA_FORMAT_ETC2_R8G8B8A1_UNORM_BLOCK);
  4721. BIND_ENUM_CONSTANT(DATA_FORMAT_ETC2_R8G8B8A1_SRGB_BLOCK);
  4722. BIND_ENUM_CONSTANT(DATA_FORMAT_ETC2_R8G8B8A8_UNORM_BLOCK);
  4723. BIND_ENUM_CONSTANT(DATA_FORMAT_ETC2_R8G8B8A8_SRGB_BLOCK);
  4724. BIND_ENUM_CONSTANT(DATA_FORMAT_EAC_R11_UNORM_BLOCK);
  4725. BIND_ENUM_CONSTANT(DATA_FORMAT_EAC_R11_SNORM_BLOCK);
  4726. BIND_ENUM_CONSTANT(DATA_FORMAT_EAC_R11G11_UNORM_BLOCK);
  4727. BIND_ENUM_CONSTANT(DATA_FORMAT_EAC_R11G11_SNORM_BLOCK);
  4728. BIND_ENUM_CONSTANT(DATA_FORMAT_ASTC_4x4_UNORM_BLOCK);
  4729. BIND_ENUM_CONSTANT(DATA_FORMAT_ASTC_4x4_SRGB_BLOCK);
  4730. BIND_ENUM_CONSTANT(DATA_FORMAT_ASTC_5x4_UNORM_BLOCK);
  4731. BIND_ENUM_CONSTANT(DATA_FORMAT_ASTC_5x4_SRGB_BLOCK);
  4732. BIND_ENUM_CONSTANT(DATA_FORMAT_ASTC_5x5_UNORM_BLOCK);
  4733. BIND_ENUM_CONSTANT(DATA_FORMAT_ASTC_5x5_SRGB_BLOCK);
  4734. BIND_ENUM_CONSTANT(DATA_FORMAT_ASTC_6x5_UNORM_BLOCK);
  4735. BIND_ENUM_CONSTANT(DATA_FORMAT_ASTC_6x5_SRGB_BLOCK);
  4736. BIND_ENUM_CONSTANT(DATA_FORMAT_ASTC_6x6_UNORM_BLOCK);
  4737. BIND_ENUM_CONSTANT(DATA_FORMAT_ASTC_6x6_SRGB_BLOCK);
  4738. BIND_ENUM_CONSTANT(DATA_FORMAT_ASTC_8x5_UNORM_BLOCK);
  4739. BIND_ENUM_CONSTANT(DATA_FORMAT_ASTC_8x5_SRGB_BLOCK);
  4740. BIND_ENUM_CONSTANT(DATA_FORMAT_ASTC_8x6_UNORM_BLOCK);
  4741. BIND_ENUM_CONSTANT(DATA_FORMAT_ASTC_8x6_SRGB_BLOCK);
  4742. BIND_ENUM_CONSTANT(DATA_FORMAT_ASTC_8x8_UNORM_BLOCK);
  4743. BIND_ENUM_CONSTANT(DATA_FORMAT_ASTC_8x8_SRGB_BLOCK);
  4744. BIND_ENUM_CONSTANT(DATA_FORMAT_ASTC_10x5_UNORM_BLOCK);
  4745. BIND_ENUM_CONSTANT(DATA_FORMAT_ASTC_10x5_SRGB_BLOCK);
  4746. BIND_ENUM_CONSTANT(DATA_FORMAT_ASTC_10x6_UNORM_BLOCK);
  4747. BIND_ENUM_CONSTANT(DATA_FORMAT_ASTC_10x6_SRGB_BLOCK);
  4748. BIND_ENUM_CONSTANT(DATA_FORMAT_ASTC_10x8_UNORM_BLOCK);
  4749. BIND_ENUM_CONSTANT(DATA_FORMAT_ASTC_10x8_SRGB_BLOCK);
  4750. BIND_ENUM_CONSTANT(DATA_FORMAT_ASTC_10x10_UNORM_BLOCK);
  4751. BIND_ENUM_CONSTANT(DATA_FORMAT_ASTC_10x10_SRGB_BLOCK);
  4752. BIND_ENUM_CONSTANT(DATA_FORMAT_ASTC_12x10_UNORM_BLOCK);
  4753. BIND_ENUM_CONSTANT(DATA_FORMAT_ASTC_12x10_SRGB_BLOCK);
  4754. BIND_ENUM_CONSTANT(DATA_FORMAT_ASTC_12x12_UNORM_BLOCK);
  4755. BIND_ENUM_CONSTANT(DATA_FORMAT_ASTC_12x12_SRGB_BLOCK);
  4756. BIND_ENUM_CONSTANT(DATA_FORMAT_G8B8G8R8_422_UNORM);
  4757. BIND_ENUM_CONSTANT(DATA_FORMAT_B8G8R8G8_422_UNORM);
  4758. BIND_ENUM_CONSTANT(DATA_FORMAT_G8_B8_R8_3PLANE_420_UNORM);
  4759. BIND_ENUM_CONSTANT(DATA_FORMAT_G8_B8R8_2PLANE_420_UNORM);
  4760. BIND_ENUM_CONSTANT(DATA_FORMAT_G8_B8_R8_3PLANE_422_UNORM);
  4761. BIND_ENUM_CONSTANT(DATA_FORMAT_G8_B8R8_2PLANE_422_UNORM);
  4762. BIND_ENUM_CONSTANT(DATA_FORMAT_G8_B8_R8_3PLANE_444_UNORM);
  4763. BIND_ENUM_CONSTANT(DATA_FORMAT_R10X6_UNORM_PACK16);
  4764. BIND_ENUM_CONSTANT(DATA_FORMAT_R10X6G10X6_UNORM_2PACK16);
  4765. BIND_ENUM_CONSTANT(DATA_FORMAT_R10X6G10X6B10X6A10X6_UNORM_4PACK16);
  4766. BIND_ENUM_CONSTANT(DATA_FORMAT_G10X6B10X6G10X6R10X6_422_UNORM_4PACK16);
  4767. BIND_ENUM_CONSTANT(DATA_FORMAT_B10X6G10X6R10X6G10X6_422_UNORM_4PACK16);
  4768. BIND_ENUM_CONSTANT(DATA_FORMAT_G10X6_B10X6_R10X6_3PLANE_420_UNORM_3PACK16);
  4769. BIND_ENUM_CONSTANT(DATA_FORMAT_G10X6_B10X6R10X6_2PLANE_420_UNORM_3PACK16);
  4770. BIND_ENUM_CONSTANT(DATA_FORMAT_G10X6_B10X6_R10X6_3PLANE_422_UNORM_3PACK16);
  4771. BIND_ENUM_CONSTANT(DATA_FORMAT_G10X6_B10X6R10X6_2PLANE_422_UNORM_3PACK16);
  4772. BIND_ENUM_CONSTANT(DATA_FORMAT_G10X6_B10X6_R10X6_3PLANE_444_UNORM_3PACK16);
  4773. BIND_ENUM_CONSTANT(DATA_FORMAT_R12X4_UNORM_PACK16);
  4774. BIND_ENUM_CONSTANT(DATA_FORMAT_R12X4G12X4_UNORM_2PACK16);
  4775. BIND_ENUM_CONSTANT(DATA_FORMAT_R12X4G12X4B12X4A12X4_UNORM_4PACK16);
  4776. BIND_ENUM_CONSTANT(DATA_FORMAT_G12X4B12X4G12X4R12X4_422_UNORM_4PACK16);
  4777. BIND_ENUM_CONSTANT(DATA_FORMAT_B12X4G12X4R12X4G12X4_422_UNORM_4PACK16);
  4778. BIND_ENUM_CONSTANT(DATA_FORMAT_G12X4_B12X4_R12X4_3PLANE_420_UNORM_3PACK16);
  4779. BIND_ENUM_CONSTANT(DATA_FORMAT_G12X4_B12X4R12X4_2PLANE_420_UNORM_3PACK16);
  4780. BIND_ENUM_CONSTANT(DATA_FORMAT_G12X4_B12X4_R12X4_3PLANE_422_UNORM_3PACK16);
  4781. BIND_ENUM_CONSTANT(DATA_FORMAT_G12X4_B12X4R12X4_2PLANE_422_UNORM_3PACK16);
  4782. BIND_ENUM_CONSTANT(DATA_FORMAT_G12X4_B12X4_R12X4_3PLANE_444_UNORM_3PACK16);
  4783. BIND_ENUM_CONSTANT(DATA_FORMAT_G16B16G16R16_422_UNORM);
  4784. BIND_ENUM_CONSTANT(DATA_FORMAT_B16G16R16G16_422_UNORM);
  4785. BIND_ENUM_CONSTANT(DATA_FORMAT_G16_B16_R16_3PLANE_420_UNORM);
  4786. BIND_ENUM_CONSTANT(DATA_FORMAT_G16_B16R16_2PLANE_420_UNORM);
  4787. BIND_ENUM_CONSTANT(DATA_FORMAT_G16_B16_R16_3PLANE_422_UNORM);
  4788. BIND_ENUM_CONSTANT(DATA_FORMAT_G16_B16R16_2PLANE_422_UNORM);
  4789. BIND_ENUM_CONSTANT(DATA_FORMAT_G16_B16_R16_3PLANE_444_UNORM);
  4790. BIND_ENUM_CONSTANT(DATA_FORMAT_MAX);
  4791. #ifndef DISABLE_DEPRECATED
  4792. BIND_BITFIELD_FLAG(BARRIER_MASK_VERTEX);
  4793. BIND_BITFIELD_FLAG(BARRIER_MASK_FRAGMENT);
  4794. BIND_BITFIELD_FLAG(BARRIER_MASK_COMPUTE);
  4795. BIND_BITFIELD_FLAG(BARRIER_MASK_TRANSFER);
  4796. BIND_BITFIELD_FLAG(BARRIER_MASK_RASTER);
  4797. BIND_BITFIELD_FLAG(BARRIER_MASK_ALL_BARRIERS);
  4798. BIND_BITFIELD_FLAG(BARRIER_MASK_NO_BARRIER);
  4799. #endif
  4800. BIND_ENUM_CONSTANT(TEXTURE_TYPE_1D);
  4801. BIND_ENUM_CONSTANT(TEXTURE_TYPE_2D);
  4802. BIND_ENUM_CONSTANT(TEXTURE_TYPE_3D);
  4803. BIND_ENUM_CONSTANT(TEXTURE_TYPE_CUBE);
  4804. BIND_ENUM_CONSTANT(TEXTURE_TYPE_1D_ARRAY);
  4805. BIND_ENUM_CONSTANT(TEXTURE_TYPE_2D_ARRAY);
  4806. BIND_ENUM_CONSTANT(TEXTURE_TYPE_CUBE_ARRAY);
  4807. BIND_ENUM_CONSTANT(TEXTURE_TYPE_MAX);
  4808. BIND_ENUM_CONSTANT(TEXTURE_SAMPLES_1);
  4809. BIND_ENUM_CONSTANT(TEXTURE_SAMPLES_2);
  4810. BIND_ENUM_CONSTANT(TEXTURE_SAMPLES_4);
  4811. BIND_ENUM_CONSTANT(TEXTURE_SAMPLES_8);
  4812. BIND_ENUM_CONSTANT(TEXTURE_SAMPLES_16);
  4813. BIND_ENUM_CONSTANT(TEXTURE_SAMPLES_32);
  4814. BIND_ENUM_CONSTANT(TEXTURE_SAMPLES_64);
  4815. BIND_ENUM_CONSTANT(TEXTURE_SAMPLES_MAX);
  4816. BIND_BITFIELD_FLAG(TEXTURE_USAGE_SAMPLING_BIT);
  4817. BIND_BITFIELD_FLAG(TEXTURE_USAGE_COLOR_ATTACHMENT_BIT);
  4818. BIND_BITFIELD_FLAG(TEXTURE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT);
  4819. BIND_BITFIELD_FLAG(TEXTURE_USAGE_STORAGE_BIT);
  4820. BIND_BITFIELD_FLAG(TEXTURE_USAGE_STORAGE_ATOMIC_BIT);
  4821. BIND_BITFIELD_FLAG(TEXTURE_USAGE_CPU_READ_BIT);
  4822. BIND_BITFIELD_FLAG(TEXTURE_USAGE_CAN_UPDATE_BIT);
  4823. BIND_BITFIELD_FLAG(TEXTURE_USAGE_CAN_COPY_FROM_BIT);
  4824. BIND_BITFIELD_FLAG(TEXTURE_USAGE_CAN_COPY_TO_BIT);
  4825. BIND_BITFIELD_FLAG(TEXTURE_USAGE_INPUT_ATTACHMENT_BIT);
  4826. BIND_ENUM_CONSTANT(TEXTURE_SWIZZLE_IDENTITY);
  4827. BIND_ENUM_CONSTANT(TEXTURE_SWIZZLE_ZERO);
  4828. BIND_ENUM_CONSTANT(TEXTURE_SWIZZLE_ONE);
  4829. BIND_ENUM_CONSTANT(TEXTURE_SWIZZLE_R);
  4830. BIND_ENUM_CONSTANT(TEXTURE_SWIZZLE_G);
  4831. BIND_ENUM_CONSTANT(TEXTURE_SWIZZLE_B);
  4832. BIND_ENUM_CONSTANT(TEXTURE_SWIZZLE_A);
  4833. BIND_ENUM_CONSTANT(TEXTURE_SWIZZLE_MAX);
  4834. BIND_ENUM_CONSTANT(TEXTURE_SLICE_2D);
  4835. BIND_ENUM_CONSTANT(TEXTURE_SLICE_CUBEMAP);
  4836. BIND_ENUM_CONSTANT(TEXTURE_SLICE_3D);
  4837. BIND_ENUM_CONSTANT(SAMPLER_FILTER_NEAREST);
  4838. BIND_ENUM_CONSTANT(SAMPLER_FILTER_LINEAR);
  4839. BIND_ENUM_CONSTANT(SAMPLER_REPEAT_MODE_REPEAT);
  4840. BIND_ENUM_CONSTANT(SAMPLER_REPEAT_MODE_MIRRORED_REPEAT);
  4841. BIND_ENUM_CONSTANT(SAMPLER_REPEAT_MODE_CLAMP_TO_EDGE);
  4842. BIND_ENUM_CONSTANT(SAMPLER_REPEAT_MODE_CLAMP_TO_BORDER);
  4843. BIND_ENUM_CONSTANT(SAMPLER_REPEAT_MODE_MIRROR_CLAMP_TO_EDGE);
  4844. BIND_ENUM_CONSTANT(SAMPLER_REPEAT_MODE_MAX);
  4845. BIND_ENUM_CONSTANT(SAMPLER_BORDER_COLOR_FLOAT_TRANSPARENT_BLACK);
  4846. BIND_ENUM_CONSTANT(SAMPLER_BORDER_COLOR_INT_TRANSPARENT_BLACK);
  4847. BIND_ENUM_CONSTANT(SAMPLER_BORDER_COLOR_FLOAT_OPAQUE_BLACK);
  4848. BIND_ENUM_CONSTANT(SAMPLER_BORDER_COLOR_INT_OPAQUE_BLACK);
  4849. BIND_ENUM_CONSTANT(SAMPLER_BORDER_COLOR_FLOAT_OPAQUE_WHITE);
  4850. BIND_ENUM_CONSTANT(SAMPLER_BORDER_COLOR_INT_OPAQUE_WHITE);
  4851. BIND_ENUM_CONSTANT(SAMPLER_BORDER_COLOR_MAX);
  4852. BIND_ENUM_CONSTANT(VERTEX_FREQUENCY_VERTEX);
  4853. BIND_ENUM_CONSTANT(VERTEX_FREQUENCY_INSTANCE);
  4854. BIND_ENUM_CONSTANT(INDEX_BUFFER_FORMAT_UINT16);
  4855. BIND_ENUM_CONSTANT(INDEX_BUFFER_FORMAT_UINT32);
  4856. BIND_BITFIELD_FLAG(STORAGE_BUFFER_USAGE_DISPATCH_INDIRECT);
  4857. BIND_ENUM_CONSTANT(UNIFORM_TYPE_SAMPLER); //for sampling only (sampler GLSL type)
  4858. BIND_ENUM_CONSTANT(UNIFORM_TYPE_SAMPLER_WITH_TEXTURE); // for sampling only); but includes a texture); (samplerXX GLSL type)); first a sampler then a texture
  4859. BIND_ENUM_CONSTANT(UNIFORM_TYPE_TEXTURE); //only texture); (textureXX GLSL type)
  4860. BIND_ENUM_CONSTANT(UNIFORM_TYPE_IMAGE); // storage image (imageXX GLSL type)); for compute mostly
  4861. BIND_ENUM_CONSTANT(UNIFORM_TYPE_TEXTURE_BUFFER); // buffer texture (or TBO); textureBuffer type)
  4862. BIND_ENUM_CONSTANT(UNIFORM_TYPE_SAMPLER_WITH_TEXTURE_BUFFER); // buffer texture with a sampler(or TBO); samplerBuffer type)
  4863. BIND_ENUM_CONSTANT(UNIFORM_TYPE_IMAGE_BUFFER); //texel buffer); (imageBuffer type)); for compute mostly
  4864. BIND_ENUM_CONSTANT(UNIFORM_TYPE_UNIFORM_BUFFER); //regular uniform buffer (or UBO).
  4865. BIND_ENUM_CONSTANT(UNIFORM_TYPE_STORAGE_BUFFER); //storage buffer ("buffer" qualifier) like UBO); but supports storage); for compute mostly
  4866. BIND_ENUM_CONSTANT(UNIFORM_TYPE_INPUT_ATTACHMENT); //used for sub-pass read/write); for mobile mostly
  4867. BIND_ENUM_CONSTANT(UNIFORM_TYPE_MAX);
  4868. BIND_ENUM_CONSTANT(RENDER_PRIMITIVE_POINTS);
  4869. BIND_ENUM_CONSTANT(RENDER_PRIMITIVE_LINES);
  4870. BIND_ENUM_CONSTANT(RENDER_PRIMITIVE_LINES_WITH_ADJACENCY);
  4871. BIND_ENUM_CONSTANT(RENDER_PRIMITIVE_LINESTRIPS);
  4872. BIND_ENUM_CONSTANT(RENDER_PRIMITIVE_LINESTRIPS_WITH_ADJACENCY);
  4873. BIND_ENUM_CONSTANT(RENDER_PRIMITIVE_TRIANGLES);
  4874. BIND_ENUM_CONSTANT(RENDER_PRIMITIVE_TRIANGLES_WITH_ADJACENCY);
  4875. BIND_ENUM_CONSTANT(RENDER_PRIMITIVE_TRIANGLE_STRIPS);
  4876. BIND_ENUM_CONSTANT(RENDER_PRIMITIVE_TRIANGLE_STRIPS_WITH_AJACENCY);
  4877. BIND_ENUM_CONSTANT(RENDER_PRIMITIVE_TRIANGLE_STRIPS_WITH_RESTART_INDEX);
  4878. BIND_ENUM_CONSTANT(RENDER_PRIMITIVE_TESSELATION_PATCH);
  4879. BIND_ENUM_CONSTANT(RENDER_PRIMITIVE_MAX);
  4880. BIND_ENUM_CONSTANT(POLYGON_CULL_DISABLED);
  4881. BIND_ENUM_CONSTANT(POLYGON_CULL_FRONT);
  4882. BIND_ENUM_CONSTANT(POLYGON_CULL_BACK);
  4883. BIND_ENUM_CONSTANT(POLYGON_FRONT_FACE_CLOCKWISE);
  4884. BIND_ENUM_CONSTANT(POLYGON_FRONT_FACE_COUNTER_CLOCKWISE);
  4885. BIND_ENUM_CONSTANT(STENCIL_OP_KEEP);
  4886. BIND_ENUM_CONSTANT(STENCIL_OP_ZERO);
  4887. BIND_ENUM_CONSTANT(STENCIL_OP_REPLACE);
  4888. BIND_ENUM_CONSTANT(STENCIL_OP_INCREMENT_AND_CLAMP);
  4889. BIND_ENUM_CONSTANT(STENCIL_OP_DECREMENT_AND_CLAMP);
  4890. BIND_ENUM_CONSTANT(STENCIL_OP_INVERT);
  4891. BIND_ENUM_CONSTANT(STENCIL_OP_INCREMENT_AND_WRAP);
  4892. BIND_ENUM_CONSTANT(STENCIL_OP_DECREMENT_AND_WRAP);
  4893. BIND_ENUM_CONSTANT(STENCIL_OP_MAX); //not an actual operator); just the amount of operators :D
  4894. BIND_ENUM_CONSTANT(COMPARE_OP_NEVER);
  4895. BIND_ENUM_CONSTANT(COMPARE_OP_LESS);
  4896. BIND_ENUM_CONSTANT(COMPARE_OP_EQUAL);
  4897. BIND_ENUM_CONSTANT(COMPARE_OP_LESS_OR_EQUAL);
  4898. BIND_ENUM_CONSTANT(COMPARE_OP_GREATER);
  4899. BIND_ENUM_CONSTANT(COMPARE_OP_NOT_EQUAL);
  4900. BIND_ENUM_CONSTANT(COMPARE_OP_GREATER_OR_EQUAL);
  4901. BIND_ENUM_CONSTANT(COMPARE_OP_ALWAYS);
  4902. BIND_ENUM_CONSTANT(COMPARE_OP_MAX);
  4903. BIND_ENUM_CONSTANT(LOGIC_OP_CLEAR);
  4904. BIND_ENUM_CONSTANT(LOGIC_OP_AND);
  4905. BIND_ENUM_CONSTANT(LOGIC_OP_AND_REVERSE);
  4906. BIND_ENUM_CONSTANT(LOGIC_OP_COPY);
  4907. BIND_ENUM_CONSTANT(LOGIC_OP_AND_INVERTED);
  4908. BIND_ENUM_CONSTANT(LOGIC_OP_NO_OP);
  4909. BIND_ENUM_CONSTANT(LOGIC_OP_XOR);
  4910. BIND_ENUM_CONSTANT(LOGIC_OP_OR);
  4911. BIND_ENUM_CONSTANT(LOGIC_OP_NOR);
  4912. BIND_ENUM_CONSTANT(LOGIC_OP_EQUIVALENT);
  4913. BIND_ENUM_CONSTANT(LOGIC_OP_INVERT);
  4914. BIND_ENUM_CONSTANT(LOGIC_OP_OR_REVERSE);
  4915. BIND_ENUM_CONSTANT(LOGIC_OP_COPY_INVERTED);
  4916. BIND_ENUM_CONSTANT(LOGIC_OP_OR_INVERTED);
  4917. BIND_ENUM_CONSTANT(LOGIC_OP_NAND);
  4918. BIND_ENUM_CONSTANT(LOGIC_OP_SET);
  4919. BIND_ENUM_CONSTANT(LOGIC_OP_MAX); //not an actual operator); just the amount of operators :D
  4920. BIND_ENUM_CONSTANT(BLEND_FACTOR_ZERO);
  4921. BIND_ENUM_CONSTANT(BLEND_FACTOR_ONE);
  4922. BIND_ENUM_CONSTANT(BLEND_FACTOR_SRC_COLOR);
  4923. BIND_ENUM_CONSTANT(BLEND_FACTOR_ONE_MINUS_SRC_COLOR);
  4924. BIND_ENUM_CONSTANT(BLEND_FACTOR_DST_COLOR);
  4925. BIND_ENUM_CONSTANT(BLEND_FACTOR_ONE_MINUS_DST_COLOR);
  4926. BIND_ENUM_CONSTANT(BLEND_FACTOR_SRC_ALPHA);
  4927. BIND_ENUM_CONSTANT(BLEND_FACTOR_ONE_MINUS_SRC_ALPHA);
  4928. BIND_ENUM_CONSTANT(BLEND_FACTOR_DST_ALPHA);
  4929. BIND_ENUM_CONSTANT(BLEND_FACTOR_ONE_MINUS_DST_ALPHA);
  4930. BIND_ENUM_CONSTANT(BLEND_FACTOR_CONSTANT_COLOR);
  4931. BIND_ENUM_CONSTANT(BLEND_FACTOR_ONE_MINUS_CONSTANT_COLOR);
  4932. BIND_ENUM_CONSTANT(BLEND_FACTOR_CONSTANT_ALPHA);
  4933. BIND_ENUM_CONSTANT(BLEND_FACTOR_ONE_MINUS_CONSTANT_ALPHA);
  4934. BIND_ENUM_CONSTANT(BLEND_FACTOR_SRC_ALPHA_SATURATE);
  4935. BIND_ENUM_CONSTANT(BLEND_FACTOR_SRC1_COLOR);
  4936. BIND_ENUM_CONSTANT(BLEND_FACTOR_ONE_MINUS_SRC1_COLOR);
  4937. BIND_ENUM_CONSTANT(BLEND_FACTOR_SRC1_ALPHA);
  4938. BIND_ENUM_CONSTANT(BLEND_FACTOR_ONE_MINUS_SRC1_ALPHA);
  4939. BIND_ENUM_CONSTANT(BLEND_FACTOR_MAX);
  4940. BIND_ENUM_CONSTANT(BLEND_OP_ADD);
  4941. BIND_ENUM_CONSTANT(BLEND_OP_SUBTRACT);
  4942. BIND_ENUM_CONSTANT(BLEND_OP_REVERSE_SUBTRACT);
  4943. BIND_ENUM_CONSTANT(BLEND_OP_MINIMUM);
  4944. BIND_ENUM_CONSTANT(BLEND_OP_MAXIMUM);
  4945. BIND_ENUM_CONSTANT(BLEND_OP_MAX);
  4946. BIND_BITFIELD_FLAG(DYNAMIC_STATE_LINE_WIDTH);
  4947. BIND_BITFIELD_FLAG(DYNAMIC_STATE_DEPTH_BIAS);
  4948. BIND_BITFIELD_FLAG(DYNAMIC_STATE_BLEND_CONSTANTS);
  4949. BIND_BITFIELD_FLAG(DYNAMIC_STATE_DEPTH_BOUNDS);
  4950. BIND_BITFIELD_FLAG(DYNAMIC_STATE_STENCIL_COMPARE_MASK);
  4951. BIND_BITFIELD_FLAG(DYNAMIC_STATE_STENCIL_WRITE_MASK);
  4952. BIND_BITFIELD_FLAG(DYNAMIC_STATE_STENCIL_REFERENCE);
  4953. BIND_ENUM_CONSTANT(INITIAL_ACTION_LOAD);
  4954. BIND_ENUM_CONSTANT(INITIAL_ACTION_CLEAR);
  4955. BIND_ENUM_CONSTANT(INITIAL_ACTION_DISCARD);
  4956. BIND_ENUM_CONSTANT(INITIAL_ACTION_MAX);
  4957. #ifndef DISABLE_DEPRECATED
  4958. BIND_ENUM_CONSTANT(INITIAL_ACTION_CLEAR_REGION);
  4959. BIND_ENUM_CONSTANT(INITIAL_ACTION_CLEAR_REGION_CONTINUE);
  4960. BIND_ENUM_CONSTANT(INITIAL_ACTION_KEEP);
  4961. BIND_ENUM_CONSTANT(INITIAL_ACTION_DROP);
  4962. BIND_ENUM_CONSTANT(INITIAL_ACTION_CONTINUE);
  4963. #endif
  4964. BIND_ENUM_CONSTANT(FINAL_ACTION_STORE);
  4965. BIND_ENUM_CONSTANT(FINAL_ACTION_DISCARD);
  4966. BIND_ENUM_CONSTANT(FINAL_ACTION_MAX);
  4967. #ifndef DISABLE_DEPRECATED
  4968. BIND_ENUM_CONSTANT(FINAL_ACTION_READ);
  4969. BIND_ENUM_CONSTANT(FINAL_ACTION_CONTINUE);
  4970. #endif
  4971. BIND_ENUM_CONSTANT(SHADER_STAGE_VERTEX);
  4972. BIND_ENUM_CONSTANT(SHADER_STAGE_FRAGMENT);
  4973. BIND_ENUM_CONSTANT(SHADER_STAGE_TESSELATION_CONTROL);
  4974. BIND_ENUM_CONSTANT(SHADER_STAGE_TESSELATION_EVALUATION);
  4975. BIND_ENUM_CONSTANT(SHADER_STAGE_COMPUTE);
  4976. BIND_ENUM_CONSTANT(SHADER_STAGE_MAX);
  4977. BIND_ENUM_CONSTANT(SHADER_STAGE_VERTEX_BIT);
  4978. BIND_ENUM_CONSTANT(SHADER_STAGE_FRAGMENT_BIT);
  4979. BIND_ENUM_CONSTANT(SHADER_STAGE_TESSELATION_CONTROL_BIT);
  4980. BIND_ENUM_CONSTANT(SHADER_STAGE_TESSELATION_EVALUATION_BIT);
  4981. BIND_ENUM_CONSTANT(SHADER_STAGE_COMPUTE_BIT);
  4982. BIND_ENUM_CONSTANT(SHADER_LANGUAGE_GLSL);
  4983. BIND_ENUM_CONSTANT(SHADER_LANGUAGE_HLSL);
  4984. BIND_ENUM_CONSTANT(PIPELINE_SPECIALIZATION_CONSTANT_TYPE_BOOL);
  4985. BIND_ENUM_CONSTANT(PIPELINE_SPECIALIZATION_CONSTANT_TYPE_INT);
  4986. BIND_ENUM_CONSTANT(PIPELINE_SPECIALIZATION_CONSTANT_TYPE_FLOAT);
  4987. BIND_ENUM_CONSTANT(LIMIT_MAX_BOUND_UNIFORM_SETS);
  4988. BIND_ENUM_CONSTANT(LIMIT_MAX_FRAMEBUFFER_COLOR_ATTACHMENTS);
  4989. BIND_ENUM_CONSTANT(LIMIT_MAX_TEXTURES_PER_UNIFORM_SET);
  4990. BIND_ENUM_CONSTANT(LIMIT_MAX_SAMPLERS_PER_UNIFORM_SET);
  4991. BIND_ENUM_CONSTANT(LIMIT_MAX_STORAGE_BUFFERS_PER_UNIFORM_SET);
  4992. BIND_ENUM_CONSTANT(LIMIT_MAX_STORAGE_IMAGES_PER_UNIFORM_SET);
  4993. BIND_ENUM_CONSTANT(LIMIT_MAX_UNIFORM_BUFFERS_PER_UNIFORM_SET);
  4994. BIND_ENUM_CONSTANT(LIMIT_MAX_DRAW_INDEXED_INDEX);
  4995. BIND_ENUM_CONSTANT(LIMIT_MAX_FRAMEBUFFER_HEIGHT);
  4996. BIND_ENUM_CONSTANT(LIMIT_MAX_FRAMEBUFFER_WIDTH);
  4997. BIND_ENUM_CONSTANT(LIMIT_MAX_TEXTURE_ARRAY_LAYERS);
  4998. BIND_ENUM_CONSTANT(LIMIT_MAX_TEXTURE_SIZE_1D);
  4999. BIND_ENUM_CONSTANT(LIMIT_MAX_TEXTURE_SIZE_2D);
  5000. BIND_ENUM_CONSTANT(LIMIT_MAX_TEXTURE_SIZE_3D);
  5001. BIND_ENUM_CONSTANT(LIMIT_MAX_TEXTURE_SIZE_CUBE);
  5002. BIND_ENUM_CONSTANT(LIMIT_MAX_TEXTURES_PER_SHADER_STAGE);
  5003. BIND_ENUM_CONSTANT(LIMIT_MAX_SAMPLERS_PER_SHADER_STAGE);
  5004. BIND_ENUM_CONSTANT(LIMIT_MAX_STORAGE_BUFFERS_PER_SHADER_STAGE);
  5005. BIND_ENUM_CONSTANT(LIMIT_MAX_STORAGE_IMAGES_PER_SHADER_STAGE);
  5006. BIND_ENUM_CONSTANT(LIMIT_MAX_UNIFORM_BUFFERS_PER_SHADER_STAGE);
  5007. BIND_ENUM_CONSTANT(LIMIT_MAX_PUSH_CONSTANT_SIZE);
  5008. BIND_ENUM_CONSTANT(LIMIT_MAX_UNIFORM_BUFFER_SIZE);
  5009. BIND_ENUM_CONSTANT(LIMIT_MAX_VERTEX_INPUT_ATTRIBUTE_OFFSET);
  5010. BIND_ENUM_CONSTANT(LIMIT_MAX_VERTEX_INPUT_ATTRIBUTES);
  5011. BIND_ENUM_CONSTANT(LIMIT_MAX_VERTEX_INPUT_BINDINGS);
  5012. BIND_ENUM_CONSTANT(LIMIT_MAX_VERTEX_INPUT_BINDING_STRIDE);
  5013. BIND_ENUM_CONSTANT(LIMIT_MIN_UNIFORM_BUFFER_OFFSET_ALIGNMENT);
  5014. BIND_ENUM_CONSTANT(LIMIT_MAX_COMPUTE_SHARED_MEMORY_SIZE);
  5015. BIND_ENUM_CONSTANT(LIMIT_MAX_COMPUTE_WORKGROUP_COUNT_X);
  5016. BIND_ENUM_CONSTANT(LIMIT_MAX_COMPUTE_WORKGROUP_COUNT_Y);
  5017. BIND_ENUM_CONSTANT(LIMIT_MAX_COMPUTE_WORKGROUP_COUNT_Z);
  5018. BIND_ENUM_CONSTANT(LIMIT_MAX_COMPUTE_WORKGROUP_INVOCATIONS);
  5019. BIND_ENUM_CONSTANT(LIMIT_MAX_COMPUTE_WORKGROUP_SIZE_X);
  5020. BIND_ENUM_CONSTANT(LIMIT_MAX_COMPUTE_WORKGROUP_SIZE_Y);
  5021. BIND_ENUM_CONSTANT(LIMIT_MAX_COMPUTE_WORKGROUP_SIZE_Z);
  5022. BIND_ENUM_CONSTANT(LIMIT_MAX_VIEWPORT_DIMENSIONS_X);
  5023. BIND_ENUM_CONSTANT(LIMIT_MAX_VIEWPORT_DIMENSIONS_Y);
  5024. BIND_ENUM_CONSTANT(MEMORY_TEXTURES);
  5025. BIND_ENUM_CONSTANT(MEMORY_BUFFERS);
  5026. BIND_ENUM_CONSTANT(MEMORY_TOTAL);
  5027. BIND_CONSTANT(INVALID_ID);
  5028. BIND_CONSTANT(INVALID_FORMAT_ID);
  5029. }
  5030. RenderingDevice::~RenderingDevice() {
  5031. finalize();
  5032. if (singleton == this) {
  5033. singleton = nullptr;
  5034. }
  5035. }
  5036. RenderingDevice::RenderingDevice() {
  5037. if (singleton == nullptr) {
  5038. singleton = this;
  5039. }
  5040. }
  5041. /*****************/
  5042. /**** BINDERS ****/
  5043. /*****************/
  5044. RID RenderingDevice::_texture_create(const Ref<RDTextureFormat> &p_format, const Ref<RDTextureView> &p_view, const TypedArray<PackedByteArray> &p_data) {
  5045. ERR_FAIL_COND_V(p_format.is_null(), RID());
  5046. ERR_FAIL_COND_V(p_view.is_null(), RID());
  5047. Vector<Vector<uint8_t>> data;
  5048. for (int i = 0; i < p_data.size(); i++) {
  5049. Vector<uint8_t> byte_slice = p_data[i];
  5050. ERR_FAIL_COND_V(byte_slice.is_empty(), RID());
  5051. data.push_back(byte_slice);
  5052. }
  5053. return texture_create(p_format->base, p_view->base, data);
  5054. }
  5055. RID RenderingDevice::_texture_create_shared(const Ref<RDTextureView> &p_view, RID p_with_texture) {
  5056. ERR_FAIL_COND_V(p_view.is_null(), RID());
  5057. return texture_create_shared(p_view->base, p_with_texture);
  5058. }
  5059. 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) {
  5060. ERR_FAIL_COND_V(p_view.is_null(), RID());
  5061. return texture_create_shared_from_slice(p_view->base, p_with_texture, p_layer, p_mipmap, p_mipmaps, p_slice_type);
  5062. }
  5063. Ref<RDTextureFormat> RenderingDevice::_texture_get_format(RID p_rd_texture) {
  5064. Ref<RDTextureFormat> rtf;
  5065. rtf.instantiate();
  5066. rtf->base = texture_get_format(p_rd_texture);
  5067. return rtf;
  5068. }
  5069. RenderingDevice::FramebufferFormatID RenderingDevice::_framebuffer_format_create(const TypedArray<RDAttachmentFormat> &p_attachments, uint32_t p_view_count) {
  5070. Vector<AttachmentFormat> attachments;
  5071. attachments.resize(p_attachments.size());
  5072. for (int i = 0; i < p_attachments.size(); i++) {
  5073. Ref<RDAttachmentFormat> af = p_attachments[i];
  5074. ERR_FAIL_COND_V(af.is_null(), INVALID_FORMAT_ID);
  5075. attachments.write[i] = af->base;
  5076. }
  5077. return framebuffer_format_create(attachments, p_view_count);
  5078. }
  5079. RenderingDevice::FramebufferFormatID RenderingDevice::_framebuffer_format_create_multipass(const TypedArray<RDAttachmentFormat> &p_attachments, const TypedArray<RDFramebufferPass> &p_passes, uint32_t p_view_count) {
  5080. Vector<AttachmentFormat> attachments;
  5081. attachments.resize(p_attachments.size());
  5082. for (int i = 0; i < p_attachments.size(); i++) {
  5083. Ref<RDAttachmentFormat> af = p_attachments[i];
  5084. ERR_FAIL_COND_V(af.is_null(), INVALID_FORMAT_ID);
  5085. attachments.write[i] = af->base;
  5086. }
  5087. Vector<FramebufferPass> passes;
  5088. for (int i = 0; i < p_passes.size(); i++) {
  5089. Ref<RDFramebufferPass> pass = p_passes[i];
  5090. ERR_CONTINUE(pass.is_null());
  5091. passes.push_back(pass->base);
  5092. }
  5093. return framebuffer_format_create_multipass(attachments, passes, p_view_count);
  5094. }
  5095. RID RenderingDevice::_framebuffer_create(const TypedArray<RID> &p_textures, FramebufferFormatID p_format_check, uint32_t p_view_count) {
  5096. Vector<RID> textures = Variant(p_textures);
  5097. return framebuffer_create(textures, p_format_check, p_view_count);
  5098. }
  5099. RID RenderingDevice::_framebuffer_create_multipass(const TypedArray<RID> &p_textures, const TypedArray<RDFramebufferPass> &p_passes, FramebufferFormatID p_format_check, uint32_t p_view_count) {
  5100. Vector<RID> textures = Variant(p_textures);
  5101. Vector<FramebufferPass> passes;
  5102. for (int i = 0; i < p_passes.size(); i++) {
  5103. Ref<RDFramebufferPass> pass = p_passes[i];
  5104. ERR_CONTINUE(pass.is_null());
  5105. passes.push_back(pass->base);
  5106. }
  5107. return framebuffer_create_multipass(textures, passes, p_format_check, p_view_count);
  5108. }
  5109. RID RenderingDevice::_sampler_create(const Ref<RDSamplerState> &p_state) {
  5110. ERR_FAIL_COND_V(p_state.is_null(), RID());
  5111. return sampler_create(p_state->base);
  5112. }
  5113. RenderingDevice::VertexFormatID RenderingDevice::_vertex_format_create(const TypedArray<RDVertexAttribute> &p_vertex_formats) {
  5114. Vector<VertexAttribute> descriptions;
  5115. descriptions.resize(p_vertex_formats.size());
  5116. for (int i = 0; i < p_vertex_formats.size(); i++) {
  5117. Ref<RDVertexAttribute> af = p_vertex_formats[i];
  5118. ERR_FAIL_COND_V(af.is_null(), INVALID_FORMAT_ID);
  5119. descriptions.write[i] = af->base;
  5120. }
  5121. return vertex_format_create(descriptions);
  5122. }
  5123. 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) {
  5124. Vector<RID> buffers = Variant(p_src_buffers);
  5125. Vector<uint64_t> offsets;
  5126. offsets.resize(p_offsets.size());
  5127. for (int i = 0; i < p_offsets.size(); i++) {
  5128. offsets.write[i] = p_offsets[i];
  5129. }
  5130. return vertex_array_create(p_vertex_count, p_vertex_format, buffers, offsets);
  5131. }
  5132. Ref<RDShaderSPIRV> RenderingDevice::_shader_compile_spirv_from_source(const Ref<RDShaderSource> &p_source, bool p_allow_cache) {
  5133. ERR_FAIL_COND_V(p_source.is_null(), Ref<RDShaderSPIRV>());
  5134. Ref<RDShaderSPIRV> bytecode;
  5135. bytecode.instantiate();
  5136. for (int i = 0; i < RD::SHADER_STAGE_MAX; i++) {
  5137. String error;
  5138. ShaderStage stage = ShaderStage(i);
  5139. String source = p_source->get_stage_source(stage);
  5140. if (!source.is_empty()) {
  5141. Vector<uint8_t> spirv = shader_compile_spirv_from_source(stage, source, p_source->get_language(), &error, p_allow_cache);
  5142. bytecode->set_stage_bytecode(stage, spirv);
  5143. bytecode->set_stage_compile_error(stage, error);
  5144. }
  5145. }
  5146. return bytecode;
  5147. }
  5148. Vector<uint8_t> RenderingDevice::_shader_compile_binary_from_spirv(const Ref<RDShaderSPIRV> &p_spirv, const String &p_shader_name) {
  5149. ERR_FAIL_COND_V(p_spirv.is_null(), Vector<uint8_t>());
  5150. Vector<ShaderStageSPIRVData> stage_data;
  5151. for (int i = 0; i < RD::SHADER_STAGE_MAX; i++) {
  5152. ShaderStage stage = ShaderStage(i);
  5153. ShaderStageSPIRVData sd;
  5154. sd.shader_stage = stage;
  5155. String error = p_spirv->get_stage_compile_error(stage);
  5156. 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.");
  5157. sd.spirv = p_spirv->get_stage_bytecode(stage);
  5158. if (sd.spirv.is_empty()) {
  5159. continue;
  5160. }
  5161. stage_data.push_back(sd);
  5162. }
  5163. return shader_compile_binary_from_spirv(stage_data, p_shader_name);
  5164. }
  5165. RID RenderingDevice::_shader_create_from_spirv(const Ref<RDShaderSPIRV> &p_spirv, const String &p_shader_name) {
  5166. ERR_FAIL_COND_V(p_spirv.is_null(), RID());
  5167. Vector<ShaderStageSPIRVData> stage_data;
  5168. for (int i = 0; i < RD::SHADER_STAGE_MAX; i++) {
  5169. ShaderStage stage = ShaderStage(i);
  5170. ShaderStageSPIRVData sd;
  5171. sd.shader_stage = stage;
  5172. String error = p_spirv->get_stage_compile_error(stage);
  5173. ERR_FAIL_COND_V_MSG(!error.is_empty(), RID(), "Can't create a shader from an errored bytecode. Check errors in source bytecode.");
  5174. sd.spirv = p_spirv->get_stage_bytecode(stage);
  5175. if (sd.spirv.is_empty()) {
  5176. continue;
  5177. }
  5178. stage_data.push_back(sd);
  5179. }
  5180. return shader_create_from_spirv(stage_data);
  5181. }
  5182. RID RenderingDevice::_uniform_set_create(const TypedArray<RDUniform> &p_uniforms, RID p_shader, uint32_t p_shader_set) {
  5183. Vector<Uniform> uniforms;
  5184. uniforms.resize(p_uniforms.size());
  5185. for (int i = 0; i < p_uniforms.size(); i++) {
  5186. Ref<RDUniform> uniform = p_uniforms[i];
  5187. ERR_FAIL_COND_V(!uniform.is_valid(), RID());
  5188. uniforms.write[i] = uniform->base;
  5189. }
  5190. return uniform_set_create(uniforms, p_shader, p_shader_set);
  5191. }
  5192. Error RenderingDevice::_buffer_update_bind(RID p_buffer, uint32_t p_offset, uint32_t p_size, const Vector<uint8_t> &p_data) {
  5193. return buffer_update(p_buffer, p_offset, p_size, p_data.ptr());
  5194. }
  5195. static Vector<RenderingDevice::PipelineSpecializationConstant> _get_spec_constants(const TypedArray<RDPipelineSpecializationConstant> &p_constants) {
  5196. Vector<RenderingDevice::PipelineSpecializationConstant> ret;
  5197. ret.resize(p_constants.size());
  5198. for (int i = 0; i < p_constants.size(); i++) {
  5199. Ref<RDPipelineSpecializationConstant> c = p_constants[i];
  5200. ERR_CONTINUE(c.is_null());
  5201. RenderingDevice::PipelineSpecializationConstant &sc = ret.write[i];
  5202. Variant value = c->get_value();
  5203. switch (value.get_type()) {
  5204. case Variant::BOOL: {
  5205. sc.type = RD::PIPELINE_SPECIALIZATION_CONSTANT_TYPE_BOOL;
  5206. sc.bool_value = value;
  5207. } break;
  5208. case Variant::INT: {
  5209. sc.type = RD::PIPELINE_SPECIALIZATION_CONSTANT_TYPE_INT;
  5210. sc.int_value = value;
  5211. } break;
  5212. case Variant::FLOAT: {
  5213. sc.type = RD::PIPELINE_SPECIALIZATION_CONSTANT_TYPE_FLOAT;
  5214. sc.float_value = value;
  5215. } break;
  5216. default: {
  5217. }
  5218. }
  5219. sc.constant_id = c->get_constant_id();
  5220. }
  5221. return ret;
  5222. }
  5223. 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) {
  5224. PipelineRasterizationState rasterization_state;
  5225. if (p_rasterization_state.is_valid()) {
  5226. rasterization_state = p_rasterization_state->base;
  5227. }
  5228. PipelineMultisampleState multisample_state;
  5229. if (p_multisample_state.is_valid()) {
  5230. multisample_state = p_multisample_state->base;
  5231. for (int i = 0; i < p_multisample_state->sample_masks.size(); i++) {
  5232. int64_t mask = p_multisample_state->sample_masks[i];
  5233. multisample_state.sample_mask.push_back(mask);
  5234. }
  5235. }
  5236. PipelineDepthStencilState depth_stencil_state;
  5237. if (p_depth_stencil_state.is_valid()) {
  5238. depth_stencil_state = p_depth_stencil_state->base;
  5239. }
  5240. PipelineColorBlendState color_blend_state;
  5241. if (p_blend_state.is_valid()) {
  5242. color_blend_state = p_blend_state->base;
  5243. for (int i = 0; i < p_blend_state->attachments.size(); i++) {
  5244. Ref<RDPipelineColorBlendStateAttachment> attachment = p_blend_state->attachments[i];
  5245. if (attachment.is_valid()) {
  5246. color_blend_state.attachments.push_back(attachment->base);
  5247. }
  5248. }
  5249. }
  5250. 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));
  5251. }
  5252. RID RenderingDevice::_compute_pipeline_create(RID p_shader, const TypedArray<RDPipelineSpecializationConstant> &p_specialization_constants = TypedArray<RDPipelineSpecializationConstant>()) {
  5253. return compute_pipeline_create(p_shader, _get_spec_constants(p_specialization_constants));
  5254. }
  5255. #ifndef DISABLE_DEPRECATED
  5256. 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) {
  5257. ERR_FAIL_V_MSG(Vector<int64_t>(), "Deprecated. Split draw lists are used automatically by RenderingDevice.");
  5258. }
  5259. Vector<int64_t> RenderingDevice::_draw_list_switch_to_next_pass_split(uint32_t p_splits) {
  5260. ERR_FAIL_V_MSG(Vector<int64_t>(), "Deprecated. Split draw lists are used automatically by RenderingDevice.");
  5261. }
  5262. #endif
  5263. void RenderingDevice::_draw_list_set_push_constant(DrawListID p_list, const Vector<uint8_t> &p_data, uint32_t p_data_size) {
  5264. ERR_FAIL_COND((uint32_t)p_data.size() > p_data_size);
  5265. draw_list_set_push_constant(p_list, p_data.ptr(), p_data_size);
  5266. }
  5267. void RenderingDevice::_compute_list_set_push_constant(ComputeListID p_list, const Vector<uint8_t> &p_data, uint32_t p_data_size) {
  5268. ERR_FAIL_COND((uint32_t)p_data.size() > p_data_size);
  5269. compute_list_set_push_constant(p_list, p_data.ptr(), p_data_size);
  5270. }