rasterizer_storage_rd.cpp 259 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898
  1. /*************************************************************************/
  2. /* rasterizer_storage_rd.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
  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 "rasterizer_storage_rd.h"
  31. #include "core/engine.h"
  32. #include "core/io/resource_loader.h"
  33. #include "core/project_settings.h"
  34. #include "rasterizer_rd.h"
  35. #include "servers/rendering/shader_language.h"
  36. Ref<Image> RasterizerStorageRD::_validate_texture_format(const Ref<Image> &p_image, TextureToRDFormat &r_format) {
  37. Ref<Image> image = p_image->duplicate();
  38. switch (p_image->get_format()) {
  39. case Image::FORMAT_L8: {
  40. r_format.format = RD::DATA_FORMAT_R8_UNORM;
  41. r_format.swizzle_r = RD::TEXTURE_SWIZZLE_R;
  42. r_format.swizzle_g = RD::TEXTURE_SWIZZLE_R;
  43. r_format.swizzle_b = RD::TEXTURE_SWIZZLE_R;
  44. r_format.swizzle_a = RD::TEXTURE_SWIZZLE_ONE;
  45. } break; //luminance
  46. case Image::FORMAT_LA8: {
  47. r_format.format = RD::DATA_FORMAT_R8G8_UNORM;
  48. r_format.swizzle_r = RD::TEXTURE_SWIZZLE_R;
  49. r_format.swizzle_g = RD::TEXTURE_SWIZZLE_R;
  50. r_format.swizzle_b = RD::TEXTURE_SWIZZLE_R;
  51. r_format.swizzle_a = RD::TEXTURE_SWIZZLE_G;
  52. } break; //luminance-alpha
  53. case Image::FORMAT_R8: {
  54. r_format.format = RD::DATA_FORMAT_R8_UNORM;
  55. r_format.swizzle_r = RD::TEXTURE_SWIZZLE_R;
  56. r_format.swizzle_g = RD::TEXTURE_SWIZZLE_ZERO;
  57. r_format.swizzle_b = RD::TEXTURE_SWIZZLE_ZERO;
  58. r_format.swizzle_a = RD::TEXTURE_SWIZZLE_ONE;
  59. } break;
  60. case Image::FORMAT_RG8: {
  61. r_format.format = RD::DATA_FORMAT_R8G8_UNORM;
  62. r_format.swizzle_r = RD::TEXTURE_SWIZZLE_R;
  63. r_format.swizzle_g = RD::TEXTURE_SWIZZLE_G;
  64. r_format.swizzle_b = RD::TEXTURE_SWIZZLE_ZERO;
  65. r_format.swizzle_a = RD::TEXTURE_SWIZZLE_ONE;
  66. } break;
  67. case Image::FORMAT_RGB8: {
  68. //this format is not mandatory for specification, check if supported first
  69. if (false && RD::get_singleton()->texture_is_format_supported_for_usage(RD::DATA_FORMAT_R8G8B8_UNORM, RD::TEXTURE_USAGE_SAMPLING_BIT | RD::TEXTURE_USAGE_CAN_UPDATE_BIT) && RD::get_singleton()->texture_is_format_supported_for_usage(RD::DATA_FORMAT_R8G8B8_SRGB, RD::TEXTURE_USAGE_SAMPLING_BIT | RD::TEXTURE_USAGE_CAN_UPDATE_BIT)) {
  70. r_format.format = RD::DATA_FORMAT_R8G8B8_UNORM;
  71. r_format.format_srgb = RD::DATA_FORMAT_R8G8B8_SRGB;
  72. } else {
  73. //not supported, reconvert
  74. r_format.format = RD::DATA_FORMAT_R8G8B8A8_UNORM;
  75. r_format.format_srgb = RD::DATA_FORMAT_R8G8B8A8_SRGB;
  76. image->convert(Image::FORMAT_RGBA8);
  77. }
  78. r_format.swizzle_r = RD::TEXTURE_SWIZZLE_R;
  79. r_format.swizzle_g = RD::TEXTURE_SWIZZLE_G;
  80. r_format.swizzle_b = RD::TEXTURE_SWIZZLE_B;
  81. r_format.swizzle_a = RD::TEXTURE_SWIZZLE_ONE;
  82. } break;
  83. case Image::FORMAT_RGBA8: {
  84. r_format.format = RD::DATA_FORMAT_R8G8B8A8_UNORM;
  85. r_format.format_srgb = RD::DATA_FORMAT_R8G8B8A8_SRGB;
  86. r_format.swizzle_r = RD::TEXTURE_SWIZZLE_R;
  87. r_format.swizzle_g = RD::TEXTURE_SWIZZLE_G;
  88. r_format.swizzle_b = RD::TEXTURE_SWIZZLE_B;
  89. r_format.swizzle_a = RD::TEXTURE_SWIZZLE_A;
  90. } break;
  91. case Image::FORMAT_RGBA4444: {
  92. r_format.format = RD::DATA_FORMAT_B4G4R4A4_UNORM_PACK16;
  93. r_format.swizzle_r = RD::TEXTURE_SWIZZLE_B; //needs swizzle
  94. r_format.swizzle_g = RD::TEXTURE_SWIZZLE_G;
  95. r_format.swizzle_b = RD::TEXTURE_SWIZZLE_R;
  96. r_format.swizzle_a = RD::TEXTURE_SWIZZLE_A;
  97. } break;
  98. case Image::FORMAT_RGB565: {
  99. r_format.format = RD::DATA_FORMAT_B5G6R5_UNORM_PACK16;
  100. r_format.swizzle_r = RD::TEXTURE_SWIZZLE_B;
  101. r_format.swizzle_g = RD::TEXTURE_SWIZZLE_G;
  102. r_format.swizzle_b = RD::TEXTURE_SWIZZLE_R;
  103. r_format.swizzle_a = RD::TEXTURE_SWIZZLE_A;
  104. } break;
  105. case Image::FORMAT_RF: {
  106. r_format.format = RD::DATA_FORMAT_R32_SFLOAT;
  107. r_format.swizzle_r = RD::TEXTURE_SWIZZLE_R;
  108. r_format.swizzle_g = RD::TEXTURE_SWIZZLE_ZERO;
  109. r_format.swizzle_b = RD::TEXTURE_SWIZZLE_ZERO;
  110. r_format.swizzle_a = RD::TEXTURE_SWIZZLE_ONE;
  111. } break; //float
  112. case Image::FORMAT_RGF: {
  113. r_format.format = RD::DATA_FORMAT_R32G32_SFLOAT;
  114. r_format.swizzle_r = RD::TEXTURE_SWIZZLE_R;
  115. r_format.swizzle_g = RD::TEXTURE_SWIZZLE_G;
  116. r_format.swizzle_b = RD::TEXTURE_SWIZZLE_ZERO;
  117. r_format.swizzle_a = RD::TEXTURE_SWIZZLE_ONE;
  118. } break;
  119. case Image::FORMAT_RGBF: {
  120. //this format is not mandatory for specification, check if supported first
  121. if (RD::get_singleton()->texture_is_format_supported_for_usage(RD::DATA_FORMAT_R32G32B32_SFLOAT, RD::TEXTURE_USAGE_SAMPLING_BIT | RD::TEXTURE_USAGE_CAN_UPDATE_BIT)) {
  122. r_format.format = RD::DATA_FORMAT_R32G32B32_SFLOAT;
  123. } else {
  124. //not supported, reconvert
  125. r_format.format = RD::DATA_FORMAT_R32G32B32A32_SFLOAT;
  126. image->convert(Image::FORMAT_RGBAF);
  127. }
  128. r_format.swizzle_r = RD::TEXTURE_SWIZZLE_R;
  129. r_format.swizzle_g = RD::TEXTURE_SWIZZLE_G;
  130. r_format.swizzle_b = RD::TEXTURE_SWIZZLE_B;
  131. r_format.swizzle_a = RD::TEXTURE_SWIZZLE_ONE;
  132. } break;
  133. case Image::FORMAT_RGBAF: {
  134. r_format.format = RD::DATA_FORMAT_R32G32B32A32_SFLOAT;
  135. r_format.swizzle_r = RD::TEXTURE_SWIZZLE_R;
  136. r_format.swizzle_g = RD::TEXTURE_SWIZZLE_G;
  137. r_format.swizzle_b = RD::TEXTURE_SWIZZLE_B;
  138. r_format.swizzle_a = RD::TEXTURE_SWIZZLE_A;
  139. } break;
  140. case Image::FORMAT_RH: {
  141. r_format.format = RD::DATA_FORMAT_R16_SFLOAT;
  142. r_format.swizzle_r = RD::TEXTURE_SWIZZLE_R;
  143. r_format.swizzle_g = RD::TEXTURE_SWIZZLE_ZERO;
  144. r_format.swizzle_b = RD::TEXTURE_SWIZZLE_ZERO;
  145. r_format.swizzle_a = RD::TEXTURE_SWIZZLE_ONE;
  146. } break; //half float
  147. case Image::FORMAT_RGH: {
  148. r_format.format = RD::DATA_FORMAT_R16G16_SFLOAT;
  149. r_format.swizzle_r = RD::TEXTURE_SWIZZLE_R;
  150. r_format.swizzle_g = RD::TEXTURE_SWIZZLE_G;
  151. r_format.swizzle_b = RD::TEXTURE_SWIZZLE_ZERO;
  152. r_format.swizzle_a = RD::TEXTURE_SWIZZLE_ONE;
  153. } break;
  154. case Image::FORMAT_RGBH: {
  155. //this format is not mandatory for specification, check if supported first
  156. if (RD::get_singleton()->texture_is_format_supported_for_usage(RD::DATA_FORMAT_R16G16B16_SFLOAT, RD::TEXTURE_USAGE_SAMPLING_BIT | RD::TEXTURE_USAGE_CAN_UPDATE_BIT)) {
  157. r_format.format = RD::DATA_FORMAT_R16G16B16_SFLOAT;
  158. } else {
  159. //not supported, reconvert
  160. r_format.format = RD::DATA_FORMAT_R16G16B16A16_SFLOAT;
  161. image->convert(Image::FORMAT_RGBAH);
  162. }
  163. r_format.swizzle_r = RD::TEXTURE_SWIZZLE_R;
  164. r_format.swizzle_g = RD::TEXTURE_SWIZZLE_G;
  165. r_format.swizzle_b = RD::TEXTURE_SWIZZLE_B;
  166. r_format.swizzle_a = RD::TEXTURE_SWIZZLE_ONE;
  167. } break;
  168. case Image::FORMAT_RGBAH: {
  169. r_format.format = RD::DATA_FORMAT_R16G16B16A16_SFLOAT;
  170. r_format.swizzle_r = RD::TEXTURE_SWIZZLE_R;
  171. r_format.swizzle_g = RD::TEXTURE_SWIZZLE_G;
  172. r_format.swizzle_b = RD::TEXTURE_SWIZZLE_B;
  173. r_format.swizzle_a = RD::TEXTURE_SWIZZLE_A;
  174. } break;
  175. case Image::FORMAT_RGBE9995: {
  176. r_format.format = RD::DATA_FORMAT_E5B9G9R9_UFLOAT_PACK32;
  177. #ifndef _MSC_VER
  178. #warning TODO need to make a function in Image to swap bits for this
  179. #endif
  180. r_format.swizzle_r = RD::TEXTURE_SWIZZLE_IDENTITY;
  181. r_format.swizzle_g = RD::TEXTURE_SWIZZLE_IDENTITY;
  182. r_format.swizzle_b = RD::TEXTURE_SWIZZLE_IDENTITY;
  183. r_format.swizzle_a = RD::TEXTURE_SWIZZLE_IDENTITY;
  184. } break;
  185. case Image::FORMAT_DXT1: {
  186. if (RD::get_singleton()->texture_is_format_supported_for_usage(RD::DATA_FORMAT_BC1_RGB_UNORM_BLOCK, RD::TEXTURE_USAGE_SAMPLING_BIT | RD::TEXTURE_USAGE_CAN_UPDATE_BIT)) {
  187. r_format.format = RD::DATA_FORMAT_BC1_RGB_UNORM_BLOCK;
  188. r_format.format_srgb = RD::DATA_FORMAT_BC1_RGB_SRGB_BLOCK;
  189. } else {
  190. //not supported, reconvert
  191. r_format.format = RD::DATA_FORMAT_R8G8B8A8_UNORM;
  192. r_format.format_srgb = RD::DATA_FORMAT_R8G8B8A8_SRGB;
  193. image->decompress();
  194. image->convert(Image::FORMAT_RGBA8);
  195. }
  196. r_format.swizzle_r = RD::TEXTURE_SWIZZLE_R;
  197. r_format.swizzle_g = RD::TEXTURE_SWIZZLE_G;
  198. r_format.swizzle_b = RD::TEXTURE_SWIZZLE_B;
  199. r_format.swizzle_a = RD::TEXTURE_SWIZZLE_ONE;
  200. } break; //s3tc bc1
  201. case Image::FORMAT_DXT3: {
  202. if (RD::get_singleton()->texture_is_format_supported_for_usage(RD::DATA_FORMAT_BC2_UNORM_BLOCK, RD::TEXTURE_USAGE_SAMPLING_BIT | RD::TEXTURE_USAGE_CAN_UPDATE_BIT)) {
  203. r_format.format = RD::DATA_FORMAT_BC2_UNORM_BLOCK;
  204. r_format.format_srgb = RD::DATA_FORMAT_BC2_SRGB_BLOCK;
  205. } else {
  206. //not supported, reconvert
  207. r_format.format = RD::DATA_FORMAT_R8G8B8A8_UNORM;
  208. r_format.format_srgb = RD::DATA_FORMAT_R8G8B8A8_SRGB;
  209. image->decompress();
  210. image->convert(Image::FORMAT_RGBA8);
  211. }
  212. r_format.swizzle_r = RD::TEXTURE_SWIZZLE_R;
  213. r_format.swizzle_g = RD::TEXTURE_SWIZZLE_G;
  214. r_format.swizzle_b = RD::TEXTURE_SWIZZLE_B;
  215. r_format.swizzle_a = RD::TEXTURE_SWIZZLE_A;
  216. } break; //bc2
  217. case Image::FORMAT_DXT5: {
  218. if (RD::get_singleton()->texture_is_format_supported_for_usage(RD::DATA_FORMAT_BC3_UNORM_BLOCK, RD::TEXTURE_USAGE_SAMPLING_BIT | RD::TEXTURE_USAGE_CAN_UPDATE_BIT)) {
  219. r_format.format = RD::DATA_FORMAT_BC3_UNORM_BLOCK;
  220. r_format.format_srgb = RD::DATA_FORMAT_BC3_SRGB_BLOCK;
  221. } else {
  222. //not supported, reconvert
  223. r_format.format = RD::DATA_FORMAT_R8G8B8A8_UNORM;
  224. r_format.format_srgb = RD::DATA_FORMAT_R8G8B8A8_SRGB;
  225. image->decompress();
  226. image->convert(Image::FORMAT_RGBA8);
  227. }
  228. r_format.swizzle_r = RD::TEXTURE_SWIZZLE_R;
  229. r_format.swizzle_g = RD::TEXTURE_SWIZZLE_G;
  230. r_format.swizzle_b = RD::TEXTURE_SWIZZLE_B;
  231. r_format.swizzle_a = RD::TEXTURE_SWIZZLE_A;
  232. } break; //bc3
  233. case Image::FORMAT_RGTC_R: {
  234. if (RD::get_singleton()->texture_is_format_supported_for_usage(RD::DATA_FORMAT_BC4_UNORM_BLOCK, RD::TEXTURE_USAGE_SAMPLING_BIT | RD::TEXTURE_USAGE_CAN_UPDATE_BIT)) {
  235. r_format.format = RD::DATA_FORMAT_BC4_UNORM_BLOCK;
  236. } else {
  237. //not supported, reconvert
  238. r_format.format = RD::DATA_FORMAT_R8_UNORM;
  239. image->decompress();
  240. image->convert(Image::FORMAT_R8);
  241. }
  242. r_format.swizzle_r = RD::TEXTURE_SWIZZLE_R;
  243. r_format.swizzle_g = RD::TEXTURE_SWIZZLE_ZERO;
  244. r_format.swizzle_b = RD::TEXTURE_SWIZZLE_ZERO;
  245. r_format.swizzle_a = RD::TEXTURE_SWIZZLE_ONE;
  246. } break;
  247. case Image::FORMAT_RGTC_RG: {
  248. if (RD::get_singleton()->texture_is_format_supported_for_usage(RD::DATA_FORMAT_BC5_UNORM_BLOCK, RD::TEXTURE_USAGE_SAMPLING_BIT | RD::TEXTURE_USAGE_CAN_UPDATE_BIT)) {
  249. r_format.format = RD::DATA_FORMAT_BC5_UNORM_BLOCK;
  250. } else {
  251. //not supported, reconvert
  252. r_format.format = RD::DATA_FORMAT_R8G8_UNORM;
  253. image->decompress();
  254. image->convert(Image::FORMAT_RG8);
  255. }
  256. r_format.swizzle_r = RD::TEXTURE_SWIZZLE_R;
  257. r_format.swizzle_g = RD::TEXTURE_SWIZZLE_G;
  258. r_format.swizzle_b = RD::TEXTURE_SWIZZLE_ZERO;
  259. r_format.swizzle_a = RD::TEXTURE_SWIZZLE_ONE;
  260. } break;
  261. case Image::FORMAT_BPTC_RGBA: {
  262. if (RD::get_singleton()->texture_is_format_supported_for_usage(RD::DATA_FORMAT_BC7_UNORM_BLOCK, RD::TEXTURE_USAGE_SAMPLING_BIT | RD::TEXTURE_USAGE_CAN_UPDATE_BIT)) {
  263. r_format.format = RD::DATA_FORMAT_BC7_UNORM_BLOCK;
  264. r_format.format_srgb = RD::DATA_FORMAT_BC7_SRGB_BLOCK;
  265. } else {
  266. //not supported, reconvert
  267. r_format.format = RD::DATA_FORMAT_R8G8B8A8_UNORM;
  268. r_format.format_srgb = RD::DATA_FORMAT_R8G8B8A8_SRGB;
  269. image->decompress();
  270. image->convert(Image::FORMAT_RGBA8);
  271. }
  272. r_format.swizzle_r = RD::TEXTURE_SWIZZLE_R;
  273. r_format.swizzle_g = RD::TEXTURE_SWIZZLE_G;
  274. r_format.swizzle_b = RD::TEXTURE_SWIZZLE_B;
  275. r_format.swizzle_a = RD::TEXTURE_SWIZZLE_A;
  276. } break; //btpc bc7
  277. case Image::FORMAT_BPTC_RGBF: {
  278. if (RD::get_singleton()->texture_is_format_supported_for_usage(RD::DATA_FORMAT_BC6H_SFLOAT_BLOCK, RD::TEXTURE_USAGE_SAMPLING_BIT | RD::TEXTURE_USAGE_CAN_UPDATE_BIT)) {
  279. r_format.format = RD::DATA_FORMAT_BC6H_SFLOAT_BLOCK;
  280. } else {
  281. //not supported, reconvert
  282. r_format.format = RD::DATA_FORMAT_R16G16B16A16_SFLOAT;
  283. image->decompress();
  284. image->convert(Image::FORMAT_RGBAH);
  285. }
  286. r_format.swizzle_r = RD::TEXTURE_SWIZZLE_R;
  287. r_format.swizzle_g = RD::TEXTURE_SWIZZLE_G;
  288. r_format.swizzle_b = RD::TEXTURE_SWIZZLE_B;
  289. r_format.swizzle_a = RD::TEXTURE_SWIZZLE_ONE;
  290. } break; //float bc6h
  291. case Image::FORMAT_BPTC_RGBFU: {
  292. if (RD::get_singleton()->texture_is_format_supported_for_usage(RD::DATA_FORMAT_BC6H_UFLOAT_BLOCK, RD::TEXTURE_USAGE_SAMPLING_BIT | RD::TEXTURE_USAGE_CAN_UPDATE_BIT)) {
  293. r_format.format = RD::DATA_FORMAT_BC6H_UFLOAT_BLOCK;
  294. } else {
  295. //not supported, reconvert
  296. r_format.format = RD::DATA_FORMAT_R16G16B16A16_SFLOAT;
  297. image->decompress();
  298. image->convert(Image::FORMAT_RGBAH);
  299. }
  300. r_format.swizzle_r = RD::TEXTURE_SWIZZLE_R;
  301. r_format.swizzle_g = RD::TEXTURE_SWIZZLE_G;
  302. r_format.swizzle_b = RD::TEXTURE_SWIZZLE_B;
  303. r_format.swizzle_a = RD::TEXTURE_SWIZZLE_ONE;
  304. } break; //unsigned float bc6hu
  305. case Image::FORMAT_PVRTC2: {
  306. //this is not properly supported by MoltekVK it seems, so best to use ETC2
  307. if (RD::get_singleton()->texture_is_format_supported_for_usage(RD::DATA_FORMAT_PVRTC1_2BPP_UNORM_BLOCK_IMG, RD::TEXTURE_USAGE_SAMPLING_BIT | RD::TEXTURE_USAGE_CAN_UPDATE_BIT)) {
  308. r_format.format = RD::DATA_FORMAT_PVRTC1_2BPP_UNORM_BLOCK_IMG;
  309. r_format.format_srgb = RD::DATA_FORMAT_PVRTC1_2BPP_SRGB_BLOCK_IMG;
  310. } else {
  311. //not supported, reconvert
  312. r_format.format = RD::DATA_FORMAT_R8G8B8A8_UNORM;
  313. r_format.format_srgb = RD::DATA_FORMAT_R8G8B8A8_SRGB;
  314. image->decompress();
  315. image->convert(Image::FORMAT_RGBA8);
  316. }
  317. r_format.swizzle_r = RD::TEXTURE_SWIZZLE_R;
  318. r_format.swizzle_g = RD::TEXTURE_SWIZZLE_G;
  319. r_format.swizzle_b = RD::TEXTURE_SWIZZLE_B;
  320. r_format.swizzle_a = RD::TEXTURE_SWIZZLE_ONE;
  321. } break; //pvrtc
  322. case Image::FORMAT_PVRTC2A: {
  323. //this is not properly supported by MoltekVK it seems, so best to use ETC2
  324. if (RD::get_singleton()->texture_is_format_supported_for_usage(RD::DATA_FORMAT_PVRTC1_2BPP_UNORM_BLOCK_IMG, RD::TEXTURE_USAGE_SAMPLING_BIT | RD::TEXTURE_USAGE_CAN_UPDATE_BIT)) {
  325. r_format.format = RD::DATA_FORMAT_PVRTC1_2BPP_UNORM_BLOCK_IMG;
  326. r_format.format_srgb = RD::DATA_FORMAT_PVRTC1_2BPP_SRGB_BLOCK_IMG;
  327. } else {
  328. //not supported, reconvert
  329. r_format.format = RD::DATA_FORMAT_R8G8B8A8_UNORM;
  330. r_format.format_srgb = RD::DATA_FORMAT_R8G8B8A8_SRGB;
  331. image->decompress();
  332. image->convert(Image::FORMAT_RGBA8);
  333. }
  334. r_format.swizzle_r = RD::TEXTURE_SWIZZLE_R;
  335. r_format.swizzle_g = RD::TEXTURE_SWIZZLE_G;
  336. r_format.swizzle_b = RD::TEXTURE_SWIZZLE_B;
  337. r_format.swizzle_a = RD::TEXTURE_SWIZZLE_A;
  338. } break;
  339. case Image::FORMAT_PVRTC4: {
  340. //this is not properly supported by MoltekVK it seems, so best to use ETC2
  341. if (RD::get_singleton()->texture_is_format_supported_for_usage(RD::DATA_FORMAT_PVRTC1_4BPP_UNORM_BLOCK_IMG, RD::TEXTURE_USAGE_SAMPLING_BIT | RD::TEXTURE_USAGE_CAN_UPDATE_BIT)) {
  342. r_format.format = RD::DATA_FORMAT_PVRTC1_4BPP_UNORM_BLOCK_IMG;
  343. r_format.format_srgb = RD::DATA_FORMAT_PVRTC1_4BPP_SRGB_BLOCK_IMG;
  344. } else {
  345. //not supported, reconvert
  346. r_format.format = RD::DATA_FORMAT_R8G8B8A8_UNORM;
  347. r_format.format_srgb = RD::DATA_FORMAT_R8G8B8A8_SRGB;
  348. image->decompress();
  349. image->convert(Image::FORMAT_RGBA8);
  350. }
  351. r_format.swizzle_r = RD::TEXTURE_SWIZZLE_R;
  352. r_format.swizzle_g = RD::TEXTURE_SWIZZLE_G;
  353. r_format.swizzle_b = RD::TEXTURE_SWIZZLE_B;
  354. r_format.swizzle_a = RD::TEXTURE_SWIZZLE_ONE;
  355. } break;
  356. case Image::FORMAT_PVRTC4A: {
  357. //this is not properly supported by MoltekVK it seems, so best to use ETC2
  358. if (RD::get_singleton()->texture_is_format_supported_for_usage(RD::DATA_FORMAT_PVRTC1_4BPP_UNORM_BLOCK_IMG, RD::TEXTURE_USAGE_SAMPLING_BIT | RD::TEXTURE_USAGE_CAN_UPDATE_BIT)) {
  359. r_format.format = RD::DATA_FORMAT_PVRTC1_4BPP_UNORM_BLOCK_IMG;
  360. r_format.format_srgb = RD::DATA_FORMAT_PVRTC1_4BPP_SRGB_BLOCK_IMG;
  361. } else {
  362. //not supported, reconvert
  363. r_format.format = RD::DATA_FORMAT_R8G8B8A8_UNORM;
  364. r_format.format_srgb = RD::DATA_FORMAT_R8G8B8A8_SRGB;
  365. image->decompress();
  366. image->convert(Image::FORMAT_RGBA8);
  367. }
  368. r_format.swizzle_r = RD::TEXTURE_SWIZZLE_R;
  369. r_format.swizzle_g = RD::TEXTURE_SWIZZLE_G;
  370. r_format.swizzle_b = RD::TEXTURE_SWIZZLE_B;
  371. r_format.swizzle_a = RD::TEXTURE_SWIZZLE_A;
  372. } break;
  373. case Image::FORMAT_ETC2_R11: {
  374. if (RD::get_singleton()->texture_is_format_supported_for_usage(RD::DATA_FORMAT_EAC_R11_UNORM_BLOCK, RD::TEXTURE_USAGE_SAMPLING_BIT | RD::TEXTURE_USAGE_CAN_UPDATE_BIT)) {
  375. r_format.format = RD::DATA_FORMAT_EAC_R11_UNORM_BLOCK;
  376. } else {
  377. //not supported, reconvert
  378. r_format.format = RD::DATA_FORMAT_R8_UNORM;
  379. image->decompress();
  380. image->convert(Image::FORMAT_R8);
  381. }
  382. r_format.swizzle_r = RD::TEXTURE_SWIZZLE_R;
  383. r_format.swizzle_g = RD::TEXTURE_SWIZZLE_ZERO;
  384. r_format.swizzle_b = RD::TEXTURE_SWIZZLE_ZERO;
  385. r_format.swizzle_a = RD::TEXTURE_SWIZZLE_ONE;
  386. } break; //etc2
  387. case Image::FORMAT_ETC2_R11S: {
  388. if (RD::get_singleton()->texture_is_format_supported_for_usage(RD::DATA_FORMAT_EAC_R11_SNORM_BLOCK, RD::TEXTURE_USAGE_SAMPLING_BIT | RD::TEXTURE_USAGE_CAN_UPDATE_BIT)) {
  389. r_format.format = RD::DATA_FORMAT_EAC_R11_SNORM_BLOCK;
  390. } else {
  391. //not supported, reconvert
  392. r_format.format = RD::DATA_FORMAT_R8_SNORM;
  393. image->decompress();
  394. image->convert(Image::FORMAT_R8);
  395. }
  396. r_format.swizzle_r = RD::TEXTURE_SWIZZLE_R;
  397. r_format.swizzle_g = RD::TEXTURE_SWIZZLE_ZERO;
  398. r_format.swizzle_b = RD::TEXTURE_SWIZZLE_ZERO;
  399. r_format.swizzle_a = RD::TEXTURE_SWIZZLE_ONE;
  400. } break; //signed: {} break; NOT srgb.
  401. case Image::FORMAT_ETC2_RG11: {
  402. if (RD::get_singleton()->texture_is_format_supported_for_usage(RD::DATA_FORMAT_EAC_R11G11_UNORM_BLOCK, RD::TEXTURE_USAGE_SAMPLING_BIT | RD::TEXTURE_USAGE_CAN_UPDATE_BIT)) {
  403. r_format.format = RD::DATA_FORMAT_EAC_R11G11_UNORM_BLOCK;
  404. } else {
  405. //not supported, reconvert
  406. r_format.format = RD::DATA_FORMAT_R8G8_UNORM;
  407. image->decompress();
  408. image->convert(Image::FORMAT_RG8);
  409. }
  410. r_format.swizzle_r = RD::TEXTURE_SWIZZLE_R;
  411. r_format.swizzle_g = RD::TEXTURE_SWIZZLE_G;
  412. r_format.swizzle_b = RD::TEXTURE_SWIZZLE_ZERO;
  413. r_format.swizzle_a = RD::TEXTURE_SWIZZLE_ONE;
  414. } break;
  415. case Image::FORMAT_ETC2_RG11S: {
  416. if (RD::get_singleton()->texture_is_format_supported_for_usage(RD::DATA_FORMAT_EAC_R11G11_SNORM_BLOCK, RD::TEXTURE_USAGE_SAMPLING_BIT | RD::TEXTURE_USAGE_CAN_UPDATE_BIT)) {
  417. r_format.format = RD::DATA_FORMAT_EAC_R11G11_SNORM_BLOCK;
  418. } else {
  419. //not supported, reconvert
  420. r_format.format = RD::DATA_FORMAT_R8G8_SNORM;
  421. image->decompress();
  422. image->convert(Image::FORMAT_RG8);
  423. }
  424. r_format.swizzle_r = RD::TEXTURE_SWIZZLE_R;
  425. r_format.swizzle_g = RD::TEXTURE_SWIZZLE_G;
  426. r_format.swizzle_b = RD::TEXTURE_SWIZZLE_ZERO;
  427. r_format.swizzle_a = RD::TEXTURE_SWIZZLE_ONE;
  428. } break;
  429. case Image::FORMAT_ETC:
  430. case Image::FORMAT_ETC2_RGB8: {
  431. //ETC2 is backwards compatible with ETC1, and all modern platforms support it
  432. if (RD::get_singleton()->texture_is_format_supported_for_usage(RD::DATA_FORMAT_ETC2_R8G8B8_UNORM_BLOCK, RD::TEXTURE_USAGE_SAMPLING_BIT | RD::TEXTURE_USAGE_CAN_UPDATE_BIT)) {
  433. r_format.format = RD::DATA_FORMAT_ETC2_R8G8B8_UNORM_BLOCK;
  434. r_format.format_srgb = RD::DATA_FORMAT_ETC2_R8G8B8_SRGB_BLOCK;
  435. } else {
  436. //not supported, reconvert
  437. r_format.format = RD::DATA_FORMAT_R8G8B8A8_UNORM;
  438. r_format.format_srgb = RD::DATA_FORMAT_R8G8B8A8_SRGB;
  439. image->decompress();
  440. image->convert(Image::FORMAT_RGBA8);
  441. }
  442. r_format.swizzle_r = RD::TEXTURE_SWIZZLE_R;
  443. r_format.swizzle_g = RD::TEXTURE_SWIZZLE_G;
  444. r_format.swizzle_b = RD::TEXTURE_SWIZZLE_B;
  445. r_format.swizzle_a = RD::TEXTURE_SWIZZLE_ONE;
  446. } break;
  447. case Image::FORMAT_ETC2_RGBA8: {
  448. if (RD::get_singleton()->texture_is_format_supported_for_usage(RD::DATA_FORMAT_ETC2_R8G8B8A8_UNORM_BLOCK, RD::TEXTURE_USAGE_SAMPLING_BIT | RD::TEXTURE_USAGE_CAN_UPDATE_BIT)) {
  449. r_format.format = RD::DATA_FORMAT_ETC2_R8G8B8A8_UNORM_BLOCK;
  450. r_format.format_srgb = RD::DATA_FORMAT_ETC2_R8G8B8A8_SRGB_BLOCK;
  451. } else {
  452. //not supported, reconvert
  453. r_format.format = RD::DATA_FORMAT_R8G8B8A8_UNORM;
  454. r_format.format_srgb = RD::DATA_FORMAT_R8G8B8A8_SRGB;
  455. image->decompress();
  456. image->convert(Image::FORMAT_RGBA8);
  457. }
  458. r_format.swizzle_r = RD::TEXTURE_SWIZZLE_R;
  459. r_format.swizzle_g = RD::TEXTURE_SWIZZLE_G;
  460. r_format.swizzle_b = RD::TEXTURE_SWIZZLE_B;
  461. r_format.swizzle_a = RD::TEXTURE_SWIZZLE_A;
  462. } break;
  463. case Image::FORMAT_ETC2_RGB8A1: {
  464. if (RD::get_singleton()->texture_is_format_supported_for_usage(RD::DATA_FORMAT_ETC2_R8G8B8A1_UNORM_BLOCK, RD::TEXTURE_USAGE_SAMPLING_BIT | RD::TEXTURE_USAGE_CAN_UPDATE_BIT)) {
  465. r_format.format = RD::DATA_FORMAT_ETC2_R8G8B8A1_UNORM_BLOCK;
  466. r_format.format_srgb = RD::DATA_FORMAT_ETC2_R8G8B8A1_SRGB_BLOCK;
  467. } else {
  468. //not supported, reconvert
  469. r_format.format = RD::DATA_FORMAT_R8G8B8A8_UNORM;
  470. r_format.format_srgb = RD::DATA_FORMAT_R8G8B8A8_SRGB;
  471. image->decompress();
  472. image->convert(Image::FORMAT_RGBA8);
  473. }
  474. r_format.swizzle_r = RD::TEXTURE_SWIZZLE_R;
  475. r_format.swizzle_g = RD::TEXTURE_SWIZZLE_G;
  476. r_format.swizzle_b = RD::TEXTURE_SWIZZLE_B;
  477. r_format.swizzle_a = RD::TEXTURE_SWIZZLE_A;
  478. } break;
  479. case Image::FORMAT_ETC2_RA_AS_RG: {
  480. if (RD::get_singleton()->texture_is_format_supported_for_usage(RD::DATA_FORMAT_ETC2_R8G8B8A8_UNORM_BLOCK, RD::TEXTURE_USAGE_SAMPLING_BIT | RD::TEXTURE_USAGE_CAN_UPDATE_BIT)) {
  481. r_format.format = RD::DATA_FORMAT_ETC2_R8G8B8A8_UNORM_BLOCK;
  482. r_format.format_srgb = RD::DATA_FORMAT_ETC2_R8G8B8A8_SRGB_BLOCK;
  483. } else {
  484. //not supported, reconvert
  485. r_format.format = RD::DATA_FORMAT_R8G8B8A8_UNORM;
  486. r_format.format_srgb = RD::DATA_FORMAT_R8G8B8A8_SRGB;
  487. image->decompress();
  488. image->convert(Image::FORMAT_RGBA8);
  489. }
  490. r_format.swizzle_r = RD::TEXTURE_SWIZZLE_R;
  491. r_format.swizzle_g = RD::TEXTURE_SWIZZLE_A;
  492. r_format.swizzle_b = RD::TEXTURE_SWIZZLE_ZERO;
  493. r_format.swizzle_a = RD::TEXTURE_SWIZZLE_ONE;
  494. } break;
  495. case Image::FORMAT_DXT5_RA_AS_RG: {
  496. if (RD::get_singleton()->texture_is_format_supported_for_usage(RD::DATA_FORMAT_BC3_UNORM_BLOCK, RD::TEXTURE_USAGE_SAMPLING_BIT | RD::TEXTURE_USAGE_CAN_UPDATE_BIT)) {
  497. r_format.format = RD::DATA_FORMAT_BC3_UNORM_BLOCK;
  498. r_format.format_srgb = RD::DATA_FORMAT_BC3_SRGB_BLOCK;
  499. } else {
  500. //not supported, reconvert
  501. r_format.format = RD::DATA_FORMAT_R8G8B8A8_UNORM;
  502. r_format.format_srgb = RD::DATA_FORMAT_R8G8B8A8_SRGB;
  503. image->decompress();
  504. image->convert(Image::FORMAT_RGBA8);
  505. }
  506. r_format.swizzle_r = RD::TEXTURE_SWIZZLE_R;
  507. r_format.swizzle_g = RD::TEXTURE_SWIZZLE_A;
  508. r_format.swizzle_b = RD::TEXTURE_SWIZZLE_ZERO;
  509. r_format.swizzle_a = RD::TEXTURE_SWIZZLE_ONE;
  510. } break;
  511. default: {
  512. }
  513. }
  514. return image;
  515. }
  516. RID RasterizerStorageRD::texture_2d_create(const Ref<Image> &p_image) {
  517. ERR_FAIL_COND_V(p_image.is_null(), RID());
  518. ERR_FAIL_COND_V(p_image->empty(), RID());
  519. TextureToRDFormat ret_format;
  520. Ref<Image> image = _validate_texture_format(p_image, ret_format);
  521. Texture texture;
  522. texture.type = Texture::TYPE_2D;
  523. texture.width = p_image->get_width();
  524. texture.height = p_image->get_height();
  525. texture.layers = 1;
  526. texture.mipmaps = p_image->get_mipmap_count() + 1;
  527. texture.depth = 1;
  528. texture.format = p_image->get_format();
  529. texture.validated_format = image->get_format();
  530. texture.rd_type = RD::TEXTURE_TYPE_2D;
  531. texture.rd_format = ret_format.format;
  532. texture.rd_format_srgb = ret_format.format_srgb;
  533. RD::TextureFormat rd_format;
  534. RD::TextureView rd_view;
  535. { //attempt register
  536. rd_format.format = texture.rd_format;
  537. rd_format.width = texture.width;
  538. rd_format.height = texture.height;
  539. rd_format.depth = 1;
  540. rd_format.array_layers = 1;
  541. rd_format.mipmaps = texture.mipmaps;
  542. rd_format.type = texture.rd_type;
  543. rd_format.samples = RD::TEXTURE_SAMPLES_1;
  544. rd_format.usage_bits = RD::TEXTURE_USAGE_SAMPLING_BIT | RD::TEXTURE_USAGE_CAN_UPDATE_BIT | RD::TEXTURE_USAGE_CAN_COPY_FROM_BIT;
  545. if (texture.rd_format_srgb != RD::DATA_FORMAT_MAX) {
  546. rd_format.shareable_formats.push_back(texture.rd_format);
  547. rd_format.shareable_formats.push_back(texture.rd_format_srgb);
  548. }
  549. }
  550. {
  551. rd_view.swizzle_r = ret_format.swizzle_r;
  552. rd_view.swizzle_g = ret_format.swizzle_g;
  553. rd_view.swizzle_b = ret_format.swizzle_b;
  554. rd_view.swizzle_a = ret_format.swizzle_a;
  555. }
  556. Vector<uint8_t> data = image->get_data(); //use image data
  557. Vector<Vector<uint8_t>> data_slices;
  558. data_slices.push_back(data);
  559. texture.rd_texture = RD::get_singleton()->texture_create(rd_format, rd_view, data_slices);
  560. ERR_FAIL_COND_V(texture.rd_texture.is_null(), RID());
  561. if (texture.rd_format_srgb != RD::DATA_FORMAT_MAX) {
  562. rd_view.format_override = texture.rd_format_srgb;
  563. texture.rd_texture_srgb = RD::get_singleton()->texture_create_shared(rd_view, texture.rd_texture);
  564. if (texture.rd_texture_srgb.is_null()) {
  565. RD::get_singleton()->free(texture.rd_texture);
  566. ERR_FAIL_COND_V(texture.rd_texture_srgb.is_null(), RID());
  567. }
  568. }
  569. //used for 2D, overridable
  570. texture.width_2d = texture.width;
  571. texture.height_2d = texture.height;
  572. texture.is_render_target = false;
  573. texture.rd_view = rd_view;
  574. texture.is_proxy = false;
  575. return texture_owner.make_rid(texture);
  576. }
  577. RID RasterizerStorageRD::texture_2d_layered_create(const Vector<Ref<Image>> &p_layers, RS::TextureLayeredType p_layered_type) {
  578. ERR_FAIL_COND_V(p_layers.size() == 0, RID());
  579. ERR_FAIL_COND_V(p_layered_type == RS::TEXTURE_LAYERED_CUBEMAP && p_layers.size() != 6, RID());
  580. ERR_FAIL_COND_V(p_layered_type == RS::TEXTURE_LAYERED_CUBEMAP_ARRAY && (p_layers.size() < 6 || (p_layers.size() % 6) != 0), RID());
  581. TextureToRDFormat ret_format;
  582. Vector<Ref<Image>> images;
  583. {
  584. int valid_width = 0;
  585. int valid_height = 0;
  586. bool valid_mipmaps = false;
  587. Image::Format valid_format = Image::FORMAT_MAX;
  588. for (int i = 0; i < p_layers.size(); i++) {
  589. ERR_FAIL_COND_V(p_layers[i]->empty(), RID());
  590. if (i == 0) {
  591. valid_width = p_layers[i]->get_width();
  592. valid_height = p_layers[i]->get_height();
  593. valid_format = p_layers[i]->get_format();
  594. valid_mipmaps = p_layers[i]->has_mipmaps();
  595. } else {
  596. ERR_FAIL_COND_V(p_layers[i]->get_width() != valid_width, RID());
  597. ERR_FAIL_COND_V(p_layers[i]->get_height() != valid_height, RID());
  598. ERR_FAIL_COND_V(p_layers[i]->get_format() != valid_format, RID());
  599. ERR_FAIL_COND_V(p_layers[i]->has_mipmaps() != valid_mipmaps, RID());
  600. }
  601. images.push_back(_validate_texture_format(p_layers[i], ret_format));
  602. }
  603. }
  604. Texture texture;
  605. texture.type = Texture::TYPE_LAYERED;
  606. texture.layered_type = p_layered_type;
  607. texture.width = p_layers[0]->get_width();
  608. texture.height = p_layers[0]->get_height();
  609. texture.layers = p_layers.size();
  610. texture.mipmaps = p_layers[0]->get_mipmap_count() + 1;
  611. texture.depth = 1;
  612. texture.format = p_layers[0]->get_format();
  613. texture.validated_format = images[0]->get_format();
  614. switch (p_layered_type) {
  615. case RS::TEXTURE_LAYERED_2D_ARRAY: {
  616. texture.rd_type = RD::TEXTURE_TYPE_2D_ARRAY;
  617. } break;
  618. case RS::TEXTURE_LAYERED_CUBEMAP: {
  619. texture.rd_type = RD::TEXTURE_TYPE_CUBE;
  620. } break;
  621. case RS::TEXTURE_LAYERED_CUBEMAP_ARRAY: {
  622. texture.rd_type = RD::TEXTURE_TYPE_CUBE_ARRAY;
  623. } break;
  624. }
  625. texture.rd_format = ret_format.format;
  626. texture.rd_format_srgb = ret_format.format_srgb;
  627. RD::TextureFormat rd_format;
  628. RD::TextureView rd_view;
  629. { //attempt register
  630. rd_format.format = texture.rd_format;
  631. rd_format.width = texture.width;
  632. rd_format.height = texture.height;
  633. rd_format.depth = 1;
  634. rd_format.array_layers = texture.layers;
  635. rd_format.mipmaps = texture.mipmaps;
  636. rd_format.type = texture.rd_type;
  637. rd_format.samples = RD::TEXTURE_SAMPLES_1;
  638. rd_format.usage_bits = RD::TEXTURE_USAGE_SAMPLING_BIT | RD::TEXTURE_USAGE_CAN_UPDATE_BIT | RD::TEXTURE_USAGE_CAN_COPY_FROM_BIT;
  639. if (texture.rd_format_srgb != RD::DATA_FORMAT_MAX) {
  640. rd_format.shareable_formats.push_back(texture.rd_format);
  641. rd_format.shareable_formats.push_back(texture.rd_format_srgb);
  642. }
  643. }
  644. {
  645. rd_view.swizzle_r = ret_format.swizzle_r;
  646. rd_view.swizzle_g = ret_format.swizzle_g;
  647. rd_view.swizzle_b = ret_format.swizzle_b;
  648. rd_view.swizzle_a = ret_format.swizzle_a;
  649. }
  650. Vector<Vector<uint8_t>> data_slices;
  651. for (int i = 0; i < images.size(); i++) {
  652. Vector<uint8_t> data = images[i]->get_data(); //use image data
  653. data_slices.push_back(data);
  654. }
  655. texture.rd_texture = RD::get_singleton()->texture_create(rd_format, rd_view, data_slices);
  656. ERR_FAIL_COND_V(texture.rd_texture.is_null(), RID());
  657. if (texture.rd_format_srgb != RD::DATA_FORMAT_MAX) {
  658. rd_view.format_override = texture.rd_format_srgb;
  659. texture.rd_texture_srgb = RD::get_singleton()->texture_create_shared(rd_view, texture.rd_texture);
  660. if (texture.rd_texture_srgb.is_null()) {
  661. RD::get_singleton()->free(texture.rd_texture);
  662. ERR_FAIL_COND_V(texture.rd_texture_srgb.is_null(), RID());
  663. }
  664. }
  665. //used for 2D, overridable
  666. texture.width_2d = texture.width;
  667. texture.height_2d = texture.height;
  668. texture.is_render_target = false;
  669. texture.rd_view = rd_view;
  670. texture.is_proxy = false;
  671. return texture_owner.make_rid(texture);
  672. }
  673. RID RasterizerStorageRD::texture_3d_create(Image::Format p_format, int p_width, int p_height, int p_depth, bool p_mipmaps, const Vector<Ref<Image>> &p_data) {
  674. ERR_FAIL_COND_V(p_data.size() == 0, RID());
  675. Image::Image3DValidateError verr = Image::validate_3d_image(p_format, p_width, p_height, p_depth, p_mipmaps, p_data);
  676. if (verr != Image::VALIDATE_3D_OK) {
  677. ERR_FAIL_V_MSG(RID(), Image::get_3d_image_validation_error_text(verr));
  678. }
  679. TextureToRDFormat ret_format;
  680. Image::Format validated_format = Image::FORMAT_MAX;
  681. Vector<uint8_t> all_data;
  682. uint32_t mipmap_count = 0;
  683. Vector<Texture::BufferSlice3D> slices;
  684. {
  685. Vector<Ref<Image>> images;
  686. uint32_t all_data_size = 0;
  687. images.resize(p_data.size());
  688. for (int i = 0; i < p_data.size(); i++) {
  689. TextureToRDFormat f;
  690. images.write[i] = _validate_texture_format(p_data[i], f);
  691. if (i == 0) {
  692. ret_format = f;
  693. validated_format = images[0]->get_format();
  694. }
  695. all_data_size += images[i]->get_data().size();
  696. }
  697. all_data.resize(all_data_size); //consolidate all data here
  698. uint32_t offset = 0;
  699. Size2i prev_size;
  700. for (int i = 0; i < p_data.size(); i++) {
  701. uint32_t s = images[i]->get_data().size();
  702. copymem(&all_data.write[offset], images[i]->get_data().ptr(), s);
  703. {
  704. Texture::BufferSlice3D slice;
  705. slice.size.width = images[i]->get_width();
  706. slice.size.height = images[i]->get_height();
  707. slice.offset = offset;
  708. slice.buffer_size = s;
  709. slices.push_back(slice);
  710. }
  711. offset += s;
  712. Size2i img_size(images[i]->get_width(), images[i]->get_height());
  713. if (img_size != prev_size) {
  714. mipmap_count++;
  715. }
  716. prev_size = img_size;
  717. }
  718. }
  719. Texture texture;
  720. texture.type = Texture::TYPE_3D;
  721. texture.width = p_width;
  722. texture.height = p_height;
  723. texture.depth = p_depth;
  724. texture.mipmaps = mipmap_count;
  725. texture.format = p_data[0]->get_format();
  726. texture.validated_format = validated_format;
  727. texture.buffer_size_3d = all_data.size();
  728. texture.buffer_slices_3d = slices;
  729. texture.rd_type = RD::TEXTURE_TYPE_3D;
  730. texture.rd_format = ret_format.format;
  731. texture.rd_format_srgb = ret_format.format_srgb;
  732. RD::TextureFormat rd_format;
  733. RD::TextureView rd_view;
  734. { //attempt register
  735. rd_format.format = texture.rd_format;
  736. rd_format.width = texture.width;
  737. rd_format.height = texture.height;
  738. rd_format.depth = texture.depth;
  739. rd_format.array_layers = 1;
  740. rd_format.mipmaps = texture.mipmaps;
  741. rd_format.type = texture.rd_type;
  742. rd_format.samples = RD::TEXTURE_SAMPLES_1;
  743. rd_format.usage_bits = RD::TEXTURE_USAGE_SAMPLING_BIT | RD::TEXTURE_USAGE_CAN_UPDATE_BIT | RD::TEXTURE_USAGE_CAN_COPY_FROM_BIT;
  744. if (texture.rd_format_srgb != RD::DATA_FORMAT_MAX) {
  745. rd_format.shareable_formats.push_back(texture.rd_format);
  746. rd_format.shareable_formats.push_back(texture.rd_format_srgb);
  747. }
  748. }
  749. {
  750. rd_view.swizzle_r = ret_format.swizzle_r;
  751. rd_view.swizzle_g = ret_format.swizzle_g;
  752. rd_view.swizzle_b = ret_format.swizzle_b;
  753. rd_view.swizzle_a = ret_format.swizzle_a;
  754. }
  755. Vector<Vector<uint8_t>> data_slices;
  756. data_slices.push_back(all_data); //one slice
  757. texture.rd_texture = RD::get_singleton()->texture_create(rd_format, rd_view, data_slices);
  758. ERR_FAIL_COND_V(texture.rd_texture.is_null(), RID());
  759. if (texture.rd_format_srgb != RD::DATA_FORMAT_MAX) {
  760. rd_view.format_override = texture.rd_format_srgb;
  761. texture.rd_texture_srgb = RD::get_singleton()->texture_create_shared(rd_view, texture.rd_texture);
  762. if (texture.rd_texture_srgb.is_null()) {
  763. RD::get_singleton()->free(texture.rd_texture);
  764. ERR_FAIL_COND_V(texture.rd_texture_srgb.is_null(), RID());
  765. }
  766. }
  767. //used for 2D, overridable
  768. texture.width_2d = texture.width;
  769. texture.height_2d = texture.height;
  770. texture.is_render_target = false;
  771. texture.rd_view = rd_view;
  772. texture.is_proxy = false;
  773. return texture_owner.make_rid(texture);
  774. }
  775. RID RasterizerStorageRD::texture_proxy_create(RID p_base) {
  776. Texture *tex = texture_owner.getornull(p_base);
  777. ERR_FAIL_COND_V(!tex, RID());
  778. Texture proxy_tex = *tex;
  779. proxy_tex.rd_view.format_override = tex->rd_format;
  780. proxy_tex.rd_texture = RD::get_singleton()->texture_create_shared(proxy_tex.rd_view, tex->rd_texture);
  781. if (proxy_tex.rd_texture_srgb.is_valid()) {
  782. proxy_tex.rd_view.format_override = tex->rd_format_srgb;
  783. proxy_tex.rd_texture_srgb = RD::get_singleton()->texture_create_shared(proxy_tex.rd_view, tex->rd_texture);
  784. }
  785. proxy_tex.proxy_to = p_base;
  786. proxy_tex.is_render_target = false;
  787. proxy_tex.is_proxy = true;
  788. proxy_tex.proxies.clear();
  789. RID rid = texture_owner.make_rid(proxy_tex);
  790. tex->proxies.push_back(rid);
  791. return rid;
  792. }
  793. void RasterizerStorageRD::_texture_2d_update(RID p_texture, const Ref<Image> &p_image, int p_layer, bool p_immediate) {
  794. ERR_FAIL_COND(p_image.is_null() || p_image->empty());
  795. Texture *tex = texture_owner.getornull(p_texture);
  796. ERR_FAIL_COND(!tex);
  797. ERR_FAIL_COND(tex->is_render_target);
  798. ERR_FAIL_COND(p_image->get_width() != tex->width || p_image->get_height() != tex->height);
  799. ERR_FAIL_COND(p_image->get_format() != tex->format);
  800. if (tex->type == Texture::TYPE_LAYERED) {
  801. ERR_FAIL_INDEX(p_layer, tex->layers);
  802. }
  803. #ifdef TOOLS_ENABLED
  804. tex->image_cache_2d.unref();
  805. #endif
  806. TextureToRDFormat f;
  807. Ref<Image> validated = _validate_texture_format(p_image, f);
  808. RD::get_singleton()->texture_update(tex->rd_texture, p_layer, validated->get_data(), !p_immediate);
  809. }
  810. void RasterizerStorageRD::texture_2d_update_immediate(RID p_texture, const Ref<Image> &p_image, int p_layer) {
  811. _texture_2d_update(p_texture, p_image, p_layer, true);
  812. }
  813. void RasterizerStorageRD::texture_2d_update(RID p_texture, const Ref<Image> &p_image, int p_layer) {
  814. _texture_2d_update(p_texture, p_image, p_layer, false);
  815. }
  816. void RasterizerStorageRD::texture_3d_update(RID p_texture, const Vector<Ref<Image>> &p_data) {
  817. Texture *tex = texture_owner.getornull(p_texture);
  818. ERR_FAIL_COND(!tex);
  819. ERR_FAIL_COND(tex->type != Texture::TYPE_3D);
  820. Image::Image3DValidateError verr = Image::validate_3d_image(tex->format, tex->width, tex->height, tex->depth, tex->mipmaps > 1, p_data);
  821. if (verr != Image::VALIDATE_3D_OK) {
  822. ERR_FAIL_MSG(Image::get_3d_image_validation_error_text(verr));
  823. }
  824. Vector<uint8_t> all_data;
  825. {
  826. Vector<Ref<Image>> images;
  827. uint32_t all_data_size = 0;
  828. images.resize(p_data.size());
  829. for (int i = 0; i < p_data.size(); i++) {
  830. Ref<Image> image = p_data[i];
  831. if (image->get_format() != tex->validated_format) {
  832. image = image->duplicate();
  833. image->convert(tex->validated_format);
  834. }
  835. all_data_size += images[i]->get_data().size();
  836. images.push_back(image);
  837. }
  838. all_data.resize(all_data_size); //consolidate all data here
  839. uint32_t offset = 0;
  840. for (int i = 0; i < p_data.size(); i++) {
  841. uint32_t s = images[i]->get_data().size();
  842. copymem(&all_data.write[offset], images[i]->get_data().ptr(), s);
  843. offset += s;
  844. }
  845. }
  846. RD::get_singleton()->texture_update(tex->rd_texture, 0, all_data, true);
  847. }
  848. void RasterizerStorageRD::texture_proxy_update(RID p_texture, RID p_proxy_to) {
  849. Texture *tex = texture_owner.getornull(p_texture);
  850. ERR_FAIL_COND(!tex);
  851. ERR_FAIL_COND(!tex->is_proxy);
  852. Texture *proxy_to = texture_owner.getornull(p_proxy_to);
  853. ERR_FAIL_COND(!proxy_to);
  854. ERR_FAIL_COND(proxy_to->is_proxy);
  855. if (tex->proxy_to.is_valid()) {
  856. //unlink proxy
  857. if (RD::get_singleton()->texture_is_valid(tex->rd_texture)) {
  858. RD::get_singleton()->free(tex->rd_texture);
  859. tex->rd_texture = RID();
  860. }
  861. if (RD::get_singleton()->texture_is_valid(tex->rd_texture_srgb)) {
  862. RD::get_singleton()->free(tex->rd_texture_srgb);
  863. tex->rd_texture_srgb = RID();
  864. }
  865. Texture *prev_tex = texture_owner.getornull(tex->proxy_to);
  866. ERR_FAIL_COND(!prev_tex);
  867. prev_tex->proxies.erase(p_texture);
  868. }
  869. *tex = *proxy_to;
  870. tex->proxy_to = p_proxy_to;
  871. tex->is_render_target = false;
  872. tex->is_proxy = true;
  873. tex->proxies.clear();
  874. proxy_to->proxies.push_back(p_texture);
  875. tex->rd_view.format_override = tex->rd_format;
  876. tex->rd_texture = RD::get_singleton()->texture_create_shared(tex->rd_view, proxy_to->rd_texture);
  877. if (tex->rd_texture_srgb.is_valid()) {
  878. tex->rd_view.format_override = tex->rd_format_srgb;
  879. tex->rd_texture_srgb = RD::get_singleton()->texture_create_shared(tex->rd_view, proxy_to->rd_texture);
  880. }
  881. }
  882. //these two APIs can be used together or in combination with the others.
  883. RID RasterizerStorageRD::texture_2d_placeholder_create() {
  884. //this could be better optimized to reuse an existing image , done this way
  885. //for now to get it working
  886. Ref<Image> image;
  887. image.instance();
  888. image->create(4, 4, false, Image::FORMAT_RGBA8);
  889. for (int i = 0; i < 4; i++) {
  890. for (int j = 0; j < 4; j++) {
  891. image->set_pixel(i, j, Color(1, 0, 1, 1));
  892. }
  893. }
  894. return texture_2d_create(image);
  895. }
  896. RID RasterizerStorageRD::texture_2d_layered_placeholder_create(RS::TextureLayeredType p_layered_type) {
  897. //this could be better optimized to reuse an existing image , done this way
  898. //for now to get it working
  899. Ref<Image> image;
  900. image.instance();
  901. image->create(4, 4, false, Image::FORMAT_RGBA8);
  902. for (int i = 0; i < 4; i++) {
  903. for (int j = 0; j < 4; j++) {
  904. image->set_pixel(i, j, Color(1, 0, 1, 1));
  905. }
  906. }
  907. Vector<Ref<Image>> images;
  908. if (p_layered_type == RS::TEXTURE_LAYERED_2D_ARRAY) {
  909. images.push_back(image);
  910. } else {
  911. //cube
  912. for (int i = 0; i < 6; i++) {
  913. images.push_back(image);
  914. }
  915. }
  916. return texture_2d_layered_create(images, p_layered_type);
  917. }
  918. RID RasterizerStorageRD::texture_3d_placeholder_create() {
  919. //this could be better optimized to reuse an existing image , done this way
  920. //for now to get it working
  921. Ref<Image> image;
  922. image.instance();
  923. image->create(4, 4, false, Image::FORMAT_RGBA8);
  924. for (int i = 0; i < 4; i++) {
  925. for (int j = 0; j < 4; j++) {
  926. image->set_pixel(i, j, Color(1, 0, 1, 1));
  927. }
  928. }
  929. Vector<Ref<Image>> images;
  930. //cube
  931. for (int i = 0; i < 4; i++) {
  932. images.push_back(image);
  933. }
  934. return texture_3d_create(Image::FORMAT_RGBA8, 4, 4, 4, false, images);
  935. }
  936. Ref<Image> RasterizerStorageRD::texture_2d_get(RID p_texture) const {
  937. Texture *tex = texture_owner.getornull(p_texture);
  938. ERR_FAIL_COND_V(!tex, Ref<Image>());
  939. #ifdef TOOLS_ENABLED
  940. if (tex->image_cache_2d.is_valid()) {
  941. return tex->image_cache_2d;
  942. }
  943. #endif
  944. Vector<uint8_t> data = RD::get_singleton()->texture_get_data(tex->rd_texture, 0);
  945. ERR_FAIL_COND_V(data.size() == 0, Ref<Image>());
  946. Ref<Image> image;
  947. image.instance();
  948. image->create(tex->width, tex->height, tex->mipmaps > 1, tex->validated_format, data);
  949. ERR_FAIL_COND_V(image->empty(), Ref<Image>());
  950. if (tex->format != tex->validated_format) {
  951. image->convert(tex->format);
  952. }
  953. #ifdef TOOLS_ENABLED
  954. if (Engine::get_singleton()->is_editor_hint()) {
  955. tex->image_cache_2d = image;
  956. }
  957. #endif
  958. return image;
  959. }
  960. Ref<Image> RasterizerStorageRD::texture_2d_layer_get(RID p_texture, int p_layer) const {
  961. Texture *tex = texture_owner.getornull(p_texture);
  962. ERR_FAIL_COND_V(!tex, Ref<Image>());
  963. Vector<uint8_t> data = RD::get_singleton()->texture_get_data(tex->rd_texture, p_layer);
  964. ERR_FAIL_COND_V(data.size() == 0, Ref<Image>());
  965. Ref<Image> image;
  966. image.instance();
  967. image->create(tex->width, tex->height, tex->mipmaps > 1, tex->validated_format, data);
  968. ERR_FAIL_COND_V(image->empty(), Ref<Image>());
  969. if (tex->format != tex->validated_format) {
  970. image->convert(tex->format);
  971. }
  972. return image;
  973. }
  974. Vector<Ref<Image>> RasterizerStorageRD::texture_3d_get(RID p_texture) const {
  975. Texture *tex = texture_owner.getornull(p_texture);
  976. ERR_FAIL_COND_V(!tex, Vector<Ref<Image>>());
  977. ERR_FAIL_COND_V(tex->type != Texture::TYPE_3D, Vector<Ref<Image>>());
  978. Vector<uint8_t> all_data = RD::get_singleton()->texture_get_data(tex->rd_texture, 0);
  979. ERR_FAIL_COND_V(all_data.size() != (int)tex->buffer_size_3d, Vector<Ref<Image>>());
  980. Vector<Ref<Image>> ret;
  981. for (int i = 0; i < tex->buffer_slices_3d.size(); i++) {
  982. const Texture::BufferSlice3D &bs = tex->buffer_slices_3d[i];
  983. ERR_FAIL_COND_V(bs.offset >= (uint32_t)all_data.size(), Vector<Ref<Image>>());
  984. ERR_FAIL_COND_V(bs.offset + bs.buffer_size > (uint32_t)all_data.size(), Vector<Ref<Image>>());
  985. Vector<uint8_t> sub_region = all_data.subarray(bs.offset, bs.offset + bs.buffer_size - 1);
  986. Ref<Image> img;
  987. img.instance();
  988. img->create(bs.size.width, bs.size.height, false, tex->validated_format, sub_region);
  989. ERR_FAIL_COND_V(img->empty(), Vector<Ref<Image>>());
  990. if (tex->format != tex->validated_format) {
  991. img->convert(tex->format);
  992. }
  993. ret.push_back(img);
  994. }
  995. return ret;
  996. }
  997. void RasterizerStorageRD::texture_replace(RID p_texture, RID p_by_texture) {
  998. Texture *tex = texture_owner.getornull(p_texture);
  999. ERR_FAIL_COND(!tex);
  1000. ERR_FAIL_COND(tex->proxy_to.is_valid()); //can't replace proxy
  1001. Texture *by_tex = texture_owner.getornull(p_by_texture);
  1002. ERR_FAIL_COND(!by_tex);
  1003. ERR_FAIL_COND(by_tex->proxy_to.is_valid()); //can't replace proxy
  1004. if (tex == by_tex) {
  1005. return;
  1006. }
  1007. if (tex->rd_texture_srgb.is_valid()) {
  1008. RD::get_singleton()->free(tex->rd_texture_srgb);
  1009. }
  1010. RD::get_singleton()->free(tex->rd_texture);
  1011. Vector<RID> proxies_to_update = tex->proxies;
  1012. Vector<RID> proxies_to_redirect = by_tex->proxies;
  1013. *tex = *by_tex;
  1014. tex->proxies = proxies_to_update; //restore proxies, so they can be updated
  1015. for (int i = 0; i < proxies_to_update.size(); i++) {
  1016. texture_proxy_update(proxies_to_update[i], p_texture);
  1017. }
  1018. for (int i = 0; i < proxies_to_redirect.size(); i++) {
  1019. texture_proxy_update(proxies_to_redirect[i], p_texture);
  1020. }
  1021. //delete last, so proxies can be updated
  1022. texture_owner.free(p_by_texture);
  1023. if (decal_atlas.textures.has(p_texture)) {
  1024. //belongs to decal atlas..
  1025. decal_atlas.dirty = true; //mark it dirty since it was most likely modified
  1026. }
  1027. }
  1028. void RasterizerStorageRD::texture_set_size_override(RID p_texture, int p_width, int p_height) {
  1029. Texture *tex = texture_owner.getornull(p_texture);
  1030. ERR_FAIL_COND(!tex);
  1031. ERR_FAIL_COND(tex->type != Texture::TYPE_2D);
  1032. tex->width_2d = p_width;
  1033. tex->height_2d = p_height;
  1034. }
  1035. void RasterizerStorageRD::texture_set_path(RID p_texture, const String &p_path) {
  1036. Texture *tex = texture_owner.getornull(p_texture);
  1037. ERR_FAIL_COND(!tex);
  1038. tex->path = p_path;
  1039. }
  1040. String RasterizerStorageRD::texture_get_path(RID p_texture) const {
  1041. return String();
  1042. }
  1043. void RasterizerStorageRD::texture_set_detect_3d_callback(RID p_texture, RS::TextureDetectCallback p_callback, void *p_userdata) {
  1044. Texture *tex = texture_owner.getornull(p_texture);
  1045. ERR_FAIL_COND(!tex);
  1046. tex->detect_3d_callback_ud = p_userdata;
  1047. tex->detect_3d_callback = p_callback;
  1048. }
  1049. void RasterizerStorageRD::texture_set_detect_normal_callback(RID p_texture, RS::TextureDetectCallback p_callback, void *p_userdata) {
  1050. Texture *tex = texture_owner.getornull(p_texture);
  1051. ERR_FAIL_COND(!tex);
  1052. tex->detect_normal_callback_ud = p_userdata;
  1053. tex->detect_normal_callback = p_callback;
  1054. }
  1055. void RasterizerStorageRD::texture_set_detect_roughness_callback(RID p_texture, RS::TextureDetectRoughnessCallback p_callback, void *p_userdata) {
  1056. Texture *tex = texture_owner.getornull(p_texture);
  1057. ERR_FAIL_COND(!tex);
  1058. tex->detect_roughness_callback_ud = p_userdata;
  1059. tex->detect_roughness_callback = p_callback;
  1060. }
  1061. void RasterizerStorageRD::texture_debug_usage(List<RS::TextureInfo> *r_info) {
  1062. }
  1063. void RasterizerStorageRD::texture_set_proxy(RID p_proxy, RID p_base) {
  1064. }
  1065. void RasterizerStorageRD::texture_set_force_redraw_if_visible(RID p_texture, bool p_enable) {
  1066. }
  1067. Size2 RasterizerStorageRD::texture_size_with_proxy(RID p_proxy) {
  1068. return texture_2d_get_size(p_proxy);
  1069. }
  1070. /* SHADER API */
  1071. RID RasterizerStorageRD::shader_create() {
  1072. Shader shader;
  1073. shader.data = nullptr;
  1074. shader.type = SHADER_TYPE_MAX;
  1075. return shader_owner.make_rid(shader);
  1076. }
  1077. void RasterizerStorageRD::shader_set_code(RID p_shader, const String &p_code) {
  1078. Shader *shader = shader_owner.getornull(p_shader);
  1079. ERR_FAIL_COND(!shader);
  1080. shader->code = p_code;
  1081. String mode_string = ShaderLanguage::get_shader_type(p_code);
  1082. ShaderType new_type;
  1083. if (mode_string == "canvas_item") {
  1084. new_type = SHADER_TYPE_2D;
  1085. } else if (mode_string == "particles") {
  1086. new_type = SHADER_TYPE_PARTICLES;
  1087. } else if (mode_string == "spatial") {
  1088. new_type = SHADER_TYPE_3D;
  1089. } else if (mode_string == "sky") {
  1090. new_type = SHADER_TYPE_SKY;
  1091. } else {
  1092. new_type = SHADER_TYPE_MAX;
  1093. }
  1094. if (new_type != shader->type) {
  1095. if (shader->data) {
  1096. memdelete(shader->data);
  1097. shader->data = nullptr;
  1098. }
  1099. for (Set<Material *>::Element *E = shader->owners.front(); E; E = E->next()) {
  1100. Material *material = E->get();
  1101. material->shader_type = new_type;
  1102. if (material->data) {
  1103. memdelete(material->data);
  1104. material->data = nullptr;
  1105. }
  1106. }
  1107. shader->type = new_type;
  1108. if (new_type < SHADER_TYPE_MAX && shader_data_request_func[new_type]) {
  1109. shader->data = shader_data_request_func[new_type]();
  1110. } else {
  1111. shader->type = SHADER_TYPE_MAX; //invalid
  1112. }
  1113. for (Set<Material *>::Element *E = shader->owners.front(); E; E = E->next()) {
  1114. Material *material = E->get();
  1115. if (shader->data) {
  1116. material->data = material_data_request_func[new_type](shader->data);
  1117. material->data->self = material->self;
  1118. material->data->set_next_pass(material->next_pass);
  1119. material->data->set_render_priority(material->priority);
  1120. }
  1121. material->shader_type = new_type;
  1122. }
  1123. }
  1124. if (shader->data) {
  1125. shader->data->set_code(p_code);
  1126. }
  1127. for (Set<Material *>::Element *E = shader->owners.front(); E; E = E->next()) {
  1128. Material *material = E->get();
  1129. material->instance_dependency.instance_notify_changed(false, true);
  1130. _material_queue_update(material, true, true);
  1131. }
  1132. }
  1133. String RasterizerStorageRD::shader_get_code(RID p_shader) const {
  1134. Shader *shader = shader_owner.getornull(p_shader);
  1135. ERR_FAIL_COND_V(!shader, String());
  1136. return shader->code;
  1137. }
  1138. void RasterizerStorageRD::shader_get_param_list(RID p_shader, List<PropertyInfo> *p_param_list) const {
  1139. Shader *shader = shader_owner.getornull(p_shader);
  1140. ERR_FAIL_COND(!shader);
  1141. if (shader->data) {
  1142. return shader->data->get_param_list(p_param_list);
  1143. }
  1144. }
  1145. void RasterizerStorageRD::shader_set_default_texture_param(RID p_shader, const StringName &p_name, RID p_texture) {
  1146. Shader *shader = shader_owner.getornull(p_shader);
  1147. ERR_FAIL_COND(!shader);
  1148. if (p_texture.is_valid() && texture_owner.owns(p_texture)) {
  1149. shader->default_texture_parameter[p_name] = p_texture;
  1150. } else {
  1151. shader->default_texture_parameter.erase(p_name);
  1152. }
  1153. for (Set<Material *>::Element *E = shader->owners.front(); E; E = E->next()) {
  1154. Material *material = E->get();
  1155. _material_queue_update(material, false, true);
  1156. }
  1157. }
  1158. RID RasterizerStorageRD::shader_get_default_texture_param(RID p_shader, const StringName &p_name) const {
  1159. Shader *shader = shader_owner.getornull(p_shader);
  1160. ERR_FAIL_COND_V(!shader, RID());
  1161. if (shader->default_texture_parameter.has(p_name)) {
  1162. return shader->default_texture_parameter[p_name];
  1163. }
  1164. return RID();
  1165. }
  1166. Variant RasterizerStorageRD::shader_get_param_default(RID p_shader, const StringName &p_param) const {
  1167. Shader *shader = shader_owner.getornull(p_shader);
  1168. ERR_FAIL_COND_V(!shader, Variant());
  1169. if (shader->data) {
  1170. return shader->data->get_default_parameter(p_param);
  1171. }
  1172. return Variant();
  1173. }
  1174. void RasterizerStorageRD::shader_set_data_request_function(ShaderType p_shader_type, ShaderDataRequestFunction p_function) {
  1175. ERR_FAIL_INDEX(p_shader_type, SHADER_TYPE_MAX);
  1176. shader_data_request_func[p_shader_type] = p_function;
  1177. }
  1178. /* COMMON MATERIAL API */
  1179. RID RasterizerStorageRD::material_create() {
  1180. Material material;
  1181. material.data = nullptr;
  1182. material.shader = nullptr;
  1183. material.shader_type = SHADER_TYPE_MAX;
  1184. material.update_next = nullptr;
  1185. material.update_requested = false;
  1186. material.uniform_dirty = false;
  1187. material.texture_dirty = false;
  1188. material.priority = 0;
  1189. RID id = material_owner.make_rid(material);
  1190. {
  1191. Material *material_ptr = material_owner.getornull(id);
  1192. material_ptr->self = id;
  1193. }
  1194. return id;
  1195. }
  1196. void RasterizerStorageRD::_material_queue_update(Material *material, bool p_uniform, bool p_texture) {
  1197. if (material->update_requested) {
  1198. return;
  1199. }
  1200. material->update_next = material_update_list;
  1201. material_update_list = material;
  1202. material->update_requested = true;
  1203. material->uniform_dirty = material->uniform_dirty || p_uniform;
  1204. material->texture_dirty = material->texture_dirty || p_texture;
  1205. }
  1206. void RasterizerStorageRD::material_set_shader(RID p_material, RID p_shader) {
  1207. Material *material = material_owner.getornull(p_material);
  1208. ERR_FAIL_COND(!material);
  1209. if (material->data) {
  1210. memdelete(material->data);
  1211. material->data = nullptr;
  1212. }
  1213. if (material->shader) {
  1214. material->shader->owners.erase(material);
  1215. material->shader = nullptr;
  1216. material->shader_type = SHADER_TYPE_MAX;
  1217. }
  1218. if (p_shader.is_null()) {
  1219. material->instance_dependency.instance_notify_changed(false, true);
  1220. return;
  1221. }
  1222. Shader *shader = shader_owner.getornull(p_shader);
  1223. ERR_FAIL_COND(!shader);
  1224. material->shader = shader;
  1225. material->shader_type = shader->type;
  1226. shader->owners.insert(material);
  1227. if (shader->type == SHADER_TYPE_MAX) {
  1228. return;
  1229. }
  1230. ERR_FAIL_COND(shader->data == nullptr);
  1231. material->data = material_data_request_func[shader->type](shader->data);
  1232. material->data->self = p_material;
  1233. material->data->set_next_pass(material->next_pass);
  1234. material->data->set_render_priority(material->priority);
  1235. //updating happens later
  1236. material->instance_dependency.instance_notify_changed(false, true);
  1237. _material_queue_update(material, true, true);
  1238. }
  1239. void RasterizerStorageRD::material_set_param(RID p_material, const StringName &p_param, const Variant &p_value) {
  1240. Material *material = material_owner.getornull(p_material);
  1241. ERR_FAIL_COND(!material);
  1242. if (p_value.get_type() == Variant::NIL) {
  1243. material->params.erase(p_param);
  1244. } else {
  1245. material->params[p_param] = p_value;
  1246. }
  1247. if (material->shader && material->shader->data) { //shader is valid
  1248. bool is_texture = material->shader->data->is_param_texture(p_param);
  1249. _material_queue_update(material, !is_texture, is_texture);
  1250. } else {
  1251. _material_queue_update(material, true, true);
  1252. }
  1253. }
  1254. Variant RasterizerStorageRD::material_get_param(RID p_material, const StringName &p_param) const {
  1255. Material *material = material_owner.getornull(p_material);
  1256. ERR_FAIL_COND_V(!material, Variant());
  1257. if (material->params.has(p_param)) {
  1258. return material->params[p_param];
  1259. } else {
  1260. return Variant();
  1261. }
  1262. }
  1263. void RasterizerStorageRD::material_set_next_pass(RID p_material, RID p_next_material) {
  1264. Material *material = material_owner.getornull(p_material);
  1265. ERR_FAIL_COND(!material);
  1266. if (material->next_pass == p_next_material) {
  1267. return;
  1268. }
  1269. material->next_pass = p_next_material;
  1270. if (material->data) {
  1271. material->data->set_next_pass(p_next_material);
  1272. }
  1273. material->instance_dependency.instance_notify_changed(false, true);
  1274. }
  1275. void RasterizerStorageRD::material_set_render_priority(RID p_material, int priority) {
  1276. Material *material = material_owner.getornull(p_material);
  1277. ERR_FAIL_COND(!material);
  1278. material->priority = priority;
  1279. if (material->data) {
  1280. material->data->set_render_priority(priority);
  1281. }
  1282. }
  1283. bool RasterizerStorageRD::material_is_animated(RID p_material) {
  1284. Material *material = material_owner.getornull(p_material);
  1285. ERR_FAIL_COND_V(!material, false);
  1286. if (material->shader && material->shader->data) {
  1287. if (material->shader->data->is_animated()) {
  1288. return true;
  1289. } else if (material->next_pass.is_valid()) {
  1290. return material_is_animated(material->next_pass);
  1291. }
  1292. }
  1293. return false; //by default nothing is animated
  1294. }
  1295. bool RasterizerStorageRD::material_casts_shadows(RID p_material) {
  1296. Material *material = material_owner.getornull(p_material);
  1297. ERR_FAIL_COND_V(!material, true);
  1298. if (material->shader && material->shader->data) {
  1299. if (material->shader->data->casts_shadows()) {
  1300. return true;
  1301. } else if (material->next_pass.is_valid()) {
  1302. return material_casts_shadows(material->next_pass);
  1303. }
  1304. }
  1305. return true; //by default everything casts shadows
  1306. }
  1307. void RasterizerStorageRD::material_get_instance_shader_parameters(RID p_material, List<InstanceShaderParam> *r_parameters) {
  1308. Material *material = material_owner.getornull(p_material);
  1309. ERR_FAIL_COND(!material);
  1310. if (material->shader && material->shader->data) {
  1311. material->shader->data->get_instance_param_list(r_parameters);
  1312. if (material->next_pass.is_valid()) {
  1313. material_get_instance_shader_parameters(material->next_pass, r_parameters);
  1314. }
  1315. }
  1316. }
  1317. void RasterizerStorageRD::material_update_dependency(RID p_material, RasterizerScene::InstanceBase *p_instance) {
  1318. Material *material = material_owner.getornull(p_material);
  1319. ERR_FAIL_COND(!material);
  1320. p_instance->update_dependency(&material->instance_dependency);
  1321. if (material->next_pass.is_valid()) {
  1322. material_update_dependency(material->next_pass, p_instance);
  1323. }
  1324. }
  1325. void RasterizerStorageRD::material_set_data_request_function(ShaderType p_shader_type, MaterialDataRequestFunction p_function) {
  1326. ERR_FAIL_INDEX(p_shader_type, SHADER_TYPE_MAX);
  1327. material_data_request_func[p_shader_type] = p_function;
  1328. }
  1329. _FORCE_INLINE_ static void _fill_std140_variant_ubo_value(ShaderLanguage::DataType type, const Variant &value, uint8_t *data, bool p_linear_color) {
  1330. switch (type) {
  1331. case ShaderLanguage::TYPE_BOOL: {
  1332. bool v = value;
  1333. uint32_t *gui = (uint32_t *)data;
  1334. *gui = v ? 1 : 0;
  1335. } break;
  1336. case ShaderLanguage::TYPE_BVEC2: {
  1337. int v = value;
  1338. uint32_t *gui = (uint32_t *)data;
  1339. gui[0] = v & 1 ? 1 : 0;
  1340. gui[1] = v & 2 ? 1 : 0;
  1341. } break;
  1342. case ShaderLanguage::TYPE_BVEC3: {
  1343. int v = value;
  1344. uint32_t *gui = (uint32_t *)data;
  1345. gui[0] = (v & 1) ? 1 : 0;
  1346. gui[1] = (v & 2) ? 1 : 0;
  1347. gui[2] = (v & 4) ? 1 : 0;
  1348. } break;
  1349. case ShaderLanguage::TYPE_BVEC4: {
  1350. int v = value;
  1351. uint32_t *gui = (uint32_t *)data;
  1352. gui[0] = (v & 1) ? 1 : 0;
  1353. gui[1] = (v & 2) ? 1 : 0;
  1354. gui[2] = (v & 4) ? 1 : 0;
  1355. gui[3] = (v & 8) ? 1 : 0;
  1356. } break;
  1357. case ShaderLanguage::TYPE_INT: {
  1358. int v = value;
  1359. int32_t *gui = (int32_t *)data;
  1360. gui[0] = v;
  1361. } break;
  1362. case ShaderLanguage::TYPE_IVEC2: {
  1363. Vector<int> iv = value;
  1364. int s = iv.size();
  1365. int32_t *gui = (int32_t *)data;
  1366. const int *r = iv.ptr();
  1367. for (int i = 0; i < 2; i++) {
  1368. if (i < s) {
  1369. gui[i] = r[i];
  1370. } else {
  1371. gui[i] = 0;
  1372. }
  1373. }
  1374. } break;
  1375. case ShaderLanguage::TYPE_IVEC3: {
  1376. Vector<int> iv = value;
  1377. int s = iv.size();
  1378. int32_t *gui = (int32_t *)data;
  1379. const int *r = iv.ptr();
  1380. for (int i = 0; i < 3; i++) {
  1381. if (i < s) {
  1382. gui[i] = r[i];
  1383. } else {
  1384. gui[i] = 0;
  1385. }
  1386. }
  1387. } break;
  1388. case ShaderLanguage::TYPE_IVEC4: {
  1389. Vector<int> iv = value;
  1390. int s = iv.size();
  1391. int32_t *gui = (int32_t *)data;
  1392. const int *r = iv.ptr();
  1393. for (int i = 0; i < 4; i++) {
  1394. if (i < s) {
  1395. gui[i] = r[i];
  1396. } else {
  1397. gui[i] = 0;
  1398. }
  1399. }
  1400. } break;
  1401. case ShaderLanguage::TYPE_UINT: {
  1402. int v = value;
  1403. uint32_t *gui = (uint32_t *)data;
  1404. gui[0] = v;
  1405. } break;
  1406. case ShaderLanguage::TYPE_UVEC2: {
  1407. Vector<int> iv = value;
  1408. int s = iv.size();
  1409. uint32_t *gui = (uint32_t *)data;
  1410. const int *r = iv.ptr();
  1411. for (int i = 0; i < 2; i++) {
  1412. if (i < s) {
  1413. gui[i] = r[i];
  1414. } else {
  1415. gui[i] = 0;
  1416. }
  1417. }
  1418. } break;
  1419. case ShaderLanguage::TYPE_UVEC3: {
  1420. Vector<int> iv = value;
  1421. int s = iv.size();
  1422. uint32_t *gui = (uint32_t *)data;
  1423. const int *r = iv.ptr();
  1424. for (int i = 0; i < 3; i++) {
  1425. if (i < s) {
  1426. gui[i] = r[i];
  1427. } else {
  1428. gui[i] = 0;
  1429. }
  1430. }
  1431. } break;
  1432. case ShaderLanguage::TYPE_UVEC4: {
  1433. Vector<int> iv = value;
  1434. int s = iv.size();
  1435. uint32_t *gui = (uint32_t *)data;
  1436. const int *r = iv.ptr();
  1437. for (int i = 0; i < 4; i++) {
  1438. if (i < s) {
  1439. gui[i] = r[i];
  1440. } else {
  1441. gui[i] = 0;
  1442. }
  1443. }
  1444. } break;
  1445. case ShaderLanguage::TYPE_FLOAT: {
  1446. float v = value;
  1447. float *gui = (float *)data;
  1448. gui[0] = v;
  1449. } break;
  1450. case ShaderLanguage::TYPE_VEC2: {
  1451. Vector2 v = value;
  1452. float *gui = (float *)data;
  1453. gui[0] = v.x;
  1454. gui[1] = v.y;
  1455. } break;
  1456. case ShaderLanguage::TYPE_VEC3: {
  1457. Vector3 v = value;
  1458. float *gui = (float *)data;
  1459. gui[0] = v.x;
  1460. gui[1] = v.y;
  1461. gui[2] = v.z;
  1462. } break;
  1463. case ShaderLanguage::TYPE_VEC4: {
  1464. float *gui = (float *)data;
  1465. if (value.get_type() == Variant::COLOR) {
  1466. Color v = value;
  1467. if (p_linear_color) {
  1468. v = v.to_linear();
  1469. }
  1470. gui[0] = v.r;
  1471. gui[1] = v.g;
  1472. gui[2] = v.b;
  1473. gui[3] = v.a;
  1474. } else if (value.get_type() == Variant::RECT2) {
  1475. Rect2 v = value;
  1476. gui[0] = v.position.x;
  1477. gui[1] = v.position.y;
  1478. gui[2] = v.size.x;
  1479. gui[3] = v.size.y;
  1480. } else if (value.get_type() == Variant::QUAT) {
  1481. Quat v = value;
  1482. gui[0] = v.x;
  1483. gui[1] = v.y;
  1484. gui[2] = v.z;
  1485. gui[3] = v.w;
  1486. } else {
  1487. Plane v = value;
  1488. gui[0] = v.normal.x;
  1489. gui[1] = v.normal.y;
  1490. gui[2] = v.normal.z;
  1491. gui[3] = v.d;
  1492. }
  1493. } break;
  1494. case ShaderLanguage::TYPE_MAT2: {
  1495. Transform2D v = value;
  1496. float *gui = (float *)data;
  1497. //in std140 members of mat2 are treated as vec4s
  1498. gui[0] = v.elements[0][0];
  1499. gui[1] = v.elements[0][1];
  1500. gui[2] = 0;
  1501. gui[3] = 0;
  1502. gui[4] = v.elements[1][0];
  1503. gui[5] = v.elements[1][1];
  1504. gui[6] = 0;
  1505. gui[7] = 0;
  1506. } break;
  1507. case ShaderLanguage::TYPE_MAT3: {
  1508. Basis v = value;
  1509. float *gui = (float *)data;
  1510. gui[0] = v.elements[0][0];
  1511. gui[1] = v.elements[1][0];
  1512. gui[2] = v.elements[2][0];
  1513. gui[3] = 0;
  1514. gui[4] = v.elements[0][1];
  1515. gui[5] = v.elements[1][1];
  1516. gui[6] = v.elements[2][1];
  1517. gui[7] = 0;
  1518. gui[8] = v.elements[0][2];
  1519. gui[9] = v.elements[1][2];
  1520. gui[10] = v.elements[2][2];
  1521. gui[11] = 0;
  1522. } break;
  1523. case ShaderLanguage::TYPE_MAT4: {
  1524. Transform v = value;
  1525. float *gui = (float *)data;
  1526. gui[0] = v.basis.elements[0][0];
  1527. gui[1] = v.basis.elements[1][0];
  1528. gui[2] = v.basis.elements[2][0];
  1529. gui[3] = 0;
  1530. gui[4] = v.basis.elements[0][1];
  1531. gui[5] = v.basis.elements[1][1];
  1532. gui[6] = v.basis.elements[2][1];
  1533. gui[7] = 0;
  1534. gui[8] = v.basis.elements[0][2];
  1535. gui[9] = v.basis.elements[1][2];
  1536. gui[10] = v.basis.elements[2][2];
  1537. gui[11] = 0;
  1538. gui[12] = v.origin.x;
  1539. gui[13] = v.origin.y;
  1540. gui[14] = v.origin.z;
  1541. gui[15] = 1;
  1542. } break;
  1543. default: {
  1544. }
  1545. }
  1546. }
  1547. _FORCE_INLINE_ static void _fill_std140_ubo_value(ShaderLanguage::DataType type, const Vector<ShaderLanguage::ConstantNode::Value> &value, uint8_t *data) {
  1548. switch (type) {
  1549. case ShaderLanguage::TYPE_BOOL: {
  1550. uint32_t *gui = (uint32_t *)data;
  1551. *gui = value[0].boolean ? 1 : 0;
  1552. } break;
  1553. case ShaderLanguage::TYPE_BVEC2: {
  1554. uint32_t *gui = (uint32_t *)data;
  1555. gui[0] = value[0].boolean ? 1 : 0;
  1556. gui[1] = value[1].boolean ? 1 : 0;
  1557. } break;
  1558. case ShaderLanguage::TYPE_BVEC3: {
  1559. uint32_t *gui = (uint32_t *)data;
  1560. gui[0] = value[0].boolean ? 1 : 0;
  1561. gui[1] = value[1].boolean ? 1 : 0;
  1562. gui[2] = value[2].boolean ? 1 : 0;
  1563. } break;
  1564. case ShaderLanguage::TYPE_BVEC4: {
  1565. uint32_t *gui = (uint32_t *)data;
  1566. gui[0] = value[0].boolean ? 1 : 0;
  1567. gui[1] = value[1].boolean ? 1 : 0;
  1568. gui[2] = value[2].boolean ? 1 : 0;
  1569. gui[3] = value[3].boolean ? 1 : 0;
  1570. } break;
  1571. case ShaderLanguage::TYPE_INT: {
  1572. int32_t *gui = (int32_t *)data;
  1573. gui[0] = value[0].sint;
  1574. } break;
  1575. case ShaderLanguage::TYPE_IVEC2: {
  1576. int32_t *gui = (int32_t *)data;
  1577. for (int i = 0; i < 2; i++) {
  1578. gui[i] = value[i].sint;
  1579. }
  1580. } break;
  1581. case ShaderLanguage::TYPE_IVEC3: {
  1582. int32_t *gui = (int32_t *)data;
  1583. for (int i = 0; i < 3; i++) {
  1584. gui[i] = value[i].sint;
  1585. }
  1586. } break;
  1587. case ShaderLanguage::TYPE_IVEC4: {
  1588. int32_t *gui = (int32_t *)data;
  1589. for (int i = 0; i < 4; i++) {
  1590. gui[i] = value[i].sint;
  1591. }
  1592. } break;
  1593. case ShaderLanguage::TYPE_UINT: {
  1594. uint32_t *gui = (uint32_t *)data;
  1595. gui[0] = value[0].uint;
  1596. } break;
  1597. case ShaderLanguage::TYPE_UVEC2: {
  1598. int32_t *gui = (int32_t *)data;
  1599. for (int i = 0; i < 2; i++) {
  1600. gui[i] = value[i].uint;
  1601. }
  1602. } break;
  1603. case ShaderLanguage::TYPE_UVEC3: {
  1604. int32_t *gui = (int32_t *)data;
  1605. for (int i = 0; i < 3; i++) {
  1606. gui[i] = value[i].uint;
  1607. }
  1608. } break;
  1609. case ShaderLanguage::TYPE_UVEC4: {
  1610. int32_t *gui = (int32_t *)data;
  1611. for (int i = 0; i < 4; i++) {
  1612. gui[i] = value[i].uint;
  1613. }
  1614. } break;
  1615. case ShaderLanguage::TYPE_FLOAT: {
  1616. float *gui = (float *)data;
  1617. gui[0] = value[0].real;
  1618. } break;
  1619. case ShaderLanguage::TYPE_VEC2: {
  1620. float *gui = (float *)data;
  1621. for (int i = 0; i < 2; i++) {
  1622. gui[i] = value[i].real;
  1623. }
  1624. } break;
  1625. case ShaderLanguage::TYPE_VEC3: {
  1626. float *gui = (float *)data;
  1627. for (int i = 0; i < 3; i++) {
  1628. gui[i] = value[i].real;
  1629. }
  1630. } break;
  1631. case ShaderLanguage::TYPE_VEC4: {
  1632. float *gui = (float *)data;
  1633. for (int i = 0; i < 4; i++) {
  1634. gui[i] = value[i].real;
  1635. }
  1636. } break;
  1637. case ShaderLanguage::TYPE_MAT2: {
  1638. float *gui = (float *)data;
  1639. //in std140 members of mat2 are treated as vec4s
  1640. gui[0] = value[0].real;
  1641. gui[1] = value[1].real;
  1642. gui[2] = 0;
  1643. gui[3] = 0;
  1644. gui[4] = value[2].real;
  1645. gui[5] = value[3].real;
  1646. gui[6] = 0;
  1647. gui[7] = 0;
  1648. } break;
  1649. case ShaderLanguage::TYPE_MAT3: {
  1650. float *gui = (float *)data;
  1651. gui[0] = value[0].real;
  1652. gui[1] = value[1].real;
  1653. gui[2] = value[2].real;
  1654. gui[3] = 0;
  1655. gui[4] = value[3].real;
  1656. gui[5] = value[4].real;
  1657. gui[6] = value[5].real;
  1658. gui[7] = 0;
  1659. gui[8] = value[6].real;
  1660. gui[9] = value[7].real;
  1661. gui[10] = value[8].real;
  1662. gui[11] = 0;
  1663. } break;
  1664. case ShaderLanguage::TYPE_MAT4: {
  1665. float *gui = (float *)data;
  1666. for (int i = 0; i < 16; i++) {
  1667. gui[i] = value[i].real;
  1668. }
  1669. } break;
  1670. default: {
  1671. }
  1672. }
  1673. }
  1674. _FORCE_INLINE_ static void _fill_std140_ubo_empty(ShaderLanguage::DataType type, uint8_t *data) {
  1675. switch (type) {
  1676. case ShaderLanguage::TYPE_BOOL:
  1677. case ShaderLanguage::TYPE_INT:
  1678. case ShaderLanguage::TYPE_UINT:
  1679. case ShaderLanguage::TYPE_FLOAT: {
  1680. zeromem(data, 4);
  1681. } break;
  1682. case ShaderLanguage::TYPE_BVEC2:
  1683. case ShaderLanguage::TYPE_IVEC2:
  1684. case ShaderLanguage::TYPE_UVEC2:
  1685. case ShaderLanguage::TYPE_VEC2: {
  1686. zeromem(data, 8);
  1687. } break;
  1688. case ShaderLanguage::TYPE_BVEC3:
  1689. case ShaderLanguage::TYPE_IVEC3:
  1690. case ShaderLanguage::TYPE_UVEC3:
  1691. case ShaderLanguage::TYPE_VEC3:
  1692. case ShaderLanguage::TYPE_BVEC4:
  1693. case ShaderLanguage::TYPE_IVEC4:
  1694. case ShaderLanguage::TYPE_UVEC4:
  1695. case ShaderLanguage::TYPE_VEC4: {
  1696. zeromem(data, 16);
  1697. } break;
  1698. case ShaderLanguage::TYPE_MAT2: {
  1699. zeromem(data, 32);
  1700. } break;
  1701. case ShaderLanguage::TYPE_MAT3: {
  1702. zeromem(data, 48);
  1703. } break;
  1704. case ShaderLanguage::TYPE_MAT4: {
  1705. zeromem(data, 64);
  1706. } break;
  1707. default: {
  1708. }
  1709. }
  1710. }
  1711. void RasterizerStorageRD::MaterialData::update_uniform_buffer(const Map<StringName, ShaderLanguage::ShaderNode::Uniform> &p_uniforms, const uint32_t *p_uniform_offsets, const Map<StringName, Variant> &p_parameters, uint8_t *p_buffer, uint32_t p_buffer_size, bool p_use_linear_color) {
  1712. bool uses_global_buffer = false;
  1713. for (Map<StringName, ShaderLanguage::ShaderNode::Uniform>::Element *E = p_uniforms.front(); E; E = E->next()) {
  1714. if (E->get().order < 0) {
  1715. continue; // texture, does not go here
  1716. }
  1717. if (E->get().scope == ShaderLanguage::ShaderNode::Uniform::SCOPE_INSTANCE) {
  1718. continue; //instance uniforms don't appear in the bufferr
  1719. }
  1720. if (E->get().scope == ShaderLanguage::ShaderNode::Uniform::SCOPE_GLOBAL) {
  1721. //this is a global variable, get the index to it
  1722. RasterizerStorageRD *rs = base_singleton;
  1723. GlobalVariables::Variable *gv = rs->global_variables.variables.getptr(E->key());
  1724. uint32_t index = 0;
  1725. if (gv) {
  1726. index = gv->buffer_index;
  1727. } else {
  1728. WARN_PRINT("Shader uses global uniform '" + E->key() + "', but it was removed at some point. Material will not display correctly.");
  1729. }
  1730. uint32_t offset = p_uniform_offsets[E->get().order];
  1731. uint32_t *intptr = (uint32_t *)&p_buffer[offset];
  1732. *intptr = index;
  1733. uses_global_buffer = true;
  1734. continue;
  1735. }
  1736. //regular uniform
  1737. uint32_t offset = p_uniform_offsets[E->get().order];
  1738. #ifdef DEBUG_ENABLED
  1739. uint32_t size = ShaderLanguage::get_type_size(E->get().type);
  1740. ERR_CONTINUE(offset + size > p_buffer_size);
  1741. #endif
  1742. uint8_t *data = &p_buffer[offset];
  1743. const Map<StringName, Variant>::Element *V = p_parameters.find(E->key());
  1744. if (V) {
  1745. //user provided
  1746. _fill_std140_variant_ubo_value(E->get().type, V->get(), data, p_use_linear_color);
  1747. } else if (E->get().default_value.size()) {
  1748. //default value
  1749. _fill_std140_ubo_value(E->get().type, E->get().default_value, data);
  1750. //value=E->get().default_value;
  1751. } else {
  1752. //zero because it was not provided
  1753. if (E->get().type == ShaderLanguage::TYPE_VEC4 && E->get().hint == ShaderLanguage::ShaderNode::Uniform::HINT_COLOR) {
  1754. //colors must be set as black, with alpha as 1.0
  1755. _fill_std140_variant_ubo_value(E->get().type, Color(0, 0, 0, 1), data, p_use_linear_color);
  1756. } else {
  1757. //else just zero it out
  1758. _fill_std140_ubo_empty(E->get().type, data);
  1759. }
  1760. }
  1761. }
  1762. if (uses_global_buffer != (global_buffer_E != nullptr)) {
  1763. RasterizerStorageRD *rs = base_singleton;
  1764. if (uses_global_buffer) {
  1765. global_buffer_E = rs->global_variables.materials_using_buffer.push_back(self);
  1766. } else {
  1767. rs->global_variables.materials_using_buffer.erase(global_buffer_E);
  1768. global_buffer_E = nullptr;
  1769. }
  1770. }
  1771. }
  1772. RasterizerStorageRD::MaterialData::~MaterialData() {
  1773. if (global_buffer_E) {
  1774. //unregister global buffers
  1775. RasterizerStorageRD *rs = base_singleton;
  1776. rs->global_variables.materials_using_buffer.erase(global_buffer_E);
  1777. }
  1778. if (global_texture_E) {
  1779. //unregister global textures
  1780. RasterizerStorageRD *rs = base_singleton;
  1781. for (Map<StringName, uint64_t>::Element *E = used_global_textures.front(); E; E = E->next()) {
  1782. GlobalVariables::Variable *v = rs->global_variables.variables.getptr(E->key());
  1783. if (v) {
  1784. v->texture_materials.erase(self);
  1785. }
  1786. }
  1787. //unregister material from those using global textures
  1788. rs->global_variables.materials_using_texture.erase(global_texture_E);
  1789. }
  1790. }
  1791. void RasterizerStorageRD::MaterialData::update_textures(const Map<StringName, Variant> &p_parameters, const Map<StringName, RID> &p_default_textures, const Vector<ShaderCompilerRD::GeneratedCode::Texture> &p_texture_uniforms, RID *p_textures, bool p_use_linear_color) {
  1792. RasterizerStorageRD *singleton = (RasterizerStorageRD *)RasterizerStorage::base_singleton;
  1793. #ifdef TOOLS_ENABLED
  1794. Texture *roughness_detect_texture = nullptr;
  1795. RS::TextureDetectRoughnessChannel roughness_channel = RS::TEXTURE_DETECT_ROUGNHESS_R;
  1796. Texture *normal_detect_texture = nullptr;
  1797. #endif
  1798. bool uses_global_textures = false;
  1799. global_textures_pass++;
  1800. for (int i = 0; i < p_texture_uniforms.size(); i++) {
  1801. const StringName &uniform_name = p_texture_uniforms[i].name;
  1802. RID texture;
  1803. if (p_texture_uniforms[i].global) {
  1804. RasterizerStorageRD *rs = base_singleton;
  1805. uses_global_textures = true;
  1806. GlobalVariables::Variable *v = rs->global_variables.variables.getptr(uniform_name);
  1807. if (v) {
  1808. if (v->buffer_index >= 0) {
  1809. WARN_PRINT("Shader uses global uniform texture '" + String(uniform_name) + "', but it changed type and is no longer a texture!.");
  1810. } else {
  1811. Map<StringName, uint64_t>::Element *E = used_global_textures.find(uniform_name);
  1812. if (!E) {
  1813. E = used_global_textures.insert(uniform_name, global_textures_pass);
  1814. v->texture_materials.insert(self);
  1815. } else {
  1816. E->get() = global_textures_pass;
  1817. }
  1818. texture = v->override.get_type() != Variant::NIL ? v->override : v->value;
  1819. }
  1820. } else {
  1821. WARN_PRINT("Shader uses global uniform texture '" + String(uniform_name) + "', but it was removed at some point. Material will not display correctly.");
  1822. }
  1823. } else {
  1824. if (!texture.is_valid()) {
  1825. const Map<StringName, Variant>::Element *V = p_parameters.find(uniform_name);
  1826. if (V) {
  1827. texture = V->get();
  1828. }
  1829. }
  1830. if (!texture.is_valid()) {
  1831. const Map<StringName, RID>::Element *W = p_default_textures.find(uniform_name);
  1832. if (W) {
  1833. texture = W->get();
  1834. }
  1835. }
  1836. }
  1837. RID rd_texture;
  1838. if (texture.is_null()) {
  1839. //check default usage
  1840. switch (p_texture_uniforms[i].hint) {
  1841. case ShaderLanguage::ShaderNode::Uniform::HINT_BLACK:
  1842. case ShaderLanguage::ShaderNode::Uniform::HINT_BLACK_ALBEDO: {
  1843. rd_texture = singleton->texture_rd_get_default(DEFAULT_RD_TEXTURE_BLACK);
  1844. } break;
  1845. case ShaderLanguage::ShaderNode::Uniform::HINT_NONE: {
  1846. rd_texture = singleton->texture_rd_get_default(DEFAULT_RD_TEXTURE_NORMAL);
  1847. } break;
  1848. case ShaderLanguage::ShaderNode::Uniform::HINT_ANISO: {
  1849. rd_texture = singleton->texture_rd_get_default(DEFAULT_RD_TEXTURE_ANISO);
  1850. } break;
  1851. default: {
  1852. rd_texture = singleton->texture_rd_get_default(DEFAULT_RD_TEXTURE_WHITE);
  1853. } break;
  1854. }
  1855. } else {
  1856. bool srgb = p_use_linear_color && (p_texture_uniforms[i].hint == ShaderLanguage::ShaderNode::Uniform::HINT_ALBEDO || p_texture_uniforms[i].hint == ShaderLanguage::ShaderNode::Uniform::HINT_BLACK_ALBEDO);
  1857. Texture *tex = singleton->texture_owner.getornull(texture);
  1858. if (tex) {
  1859. rd_texture = (srgb && tex->rd_texture_srgb.is_valid()) ? tex->rd_texture_srgb : tex->rd_texture;
  1860. #ifdef TOOLS_ENABLED
  1861. if (tex->detect_3d_callback && p_use_linear_color) {
  1862. tex->detect_3d_callback(tex->detect_3d_callback_ud);
  1863. }
  1864. if (tex->detect_normal_callback && (p_texture_uniforms[i].hint == ShaderLanguage::ShaderNode::Uniform::HINT_NORMAL || p_texture_uniforms[i].hint == ShaderLanguage::ShaderNode::Uniform::HINT_ROUGHNESS_NORMAL)) {
  1865. if (p_texture_uniforms[i].hint == ShaderLanguage::ShaderNode::Uniform::HINT_ROUGHNESS_NORMAL) {
  1866. normal_detect_texture = tex;
  1867. }
  1868. tex->detect_normal_callback(tex->detect_normal_callback_ud);
  1869. }
  1870. if (tex->detect_roughness_callback && (p_texture_uniforms[i].hint >= ShaderLanguage::ShaderNode::Uniform::HINT_ROUGHNESS_R || p_texture_uniforms[i].hint <= ShaderLanguage::ShaderNode::Uniform::HINT_ROUGHNESS_GRAY)) {
  1871. //find the normal texture
  1872. roughness_detect_texture = tex;
  1873. roughness_channel = RS::TextureDetectRoughnessChannel(p_texture_uniforms[i].hint - ShaderLanguage::ShaderNode::Uniform::HINT_ROUGHNESS_R);
  1874. }
  1875. #endif
  1876. }
  1877. if (rd_texture.is_null()) {
  1878. //wtf
  1879. rd_texture = singleton->texture_rd_get_default(DEFAULT_RD_TEXTURE_WHITE);
  1880. }
  1881. }
  1882. p_textures[i] = rd_texture;
  1883. }
  1884. #ifdef TOOLS_ENABLED
  1885. if (roughness_detect_texture && normal_detect_texture && normal_detect_texture->path != String()) {
  1886. roughness_detect_texture->detect_roughness_callback(roughness_detect_texture->detect_roughness_callback_ud, normal_detect_texture->path, roughness_channel);
  1887. }
  1888. #endif
  1889. {
  1890. //for textures no longer used, unregister them
  1891. List<Map<StringName, uint64_t>::Element *> to_delete;
  1892. RasterizerStorageRD *rs = base_singleton;
  1893. for (Map<StringName, uint64_t>::Element *E = used_global_textures.front(); E; E = E->next()) {
  1894. if (E->get() != global_textures_pass) {
  1895. to_delete.push_back(E);
  1896. GlobalVariables::Variable *v = rs->global_variables.variables.getptr(E->key());
  1897. if (v) {
  1898. v->texture_materials.erase(self);
  1899. }
  1900. }
  1901. }
  1902. while (to_delete.front()) {
  1903. used_global_textures.erase(to_delete.front()->get());
  1904. to_delete.pop_front();
  1905. }
  1906. //handle registering/unregistering global textures
  1907. if (uses_global_textures != (global_texture_E != nullptr)) {
  1908. if (uses_global_textures) {
  1909. global_texture_E = rs->global_variables.materials_using_texture.push_back(self);
  1910. } else {
  1911. rs->global_variables.materials_using_texture.erase(global_texture_E);
  1912. global_texture_E = nullptr;
  1913. }
  1914. }
  1915. }
  1916. }
  1917. void RasterizerStorageRD::material_force_update_textures(RID p_material, ShaderType p_shader_type) {
  1918. Material *material = material_owner.getornull(p_material);
  1919. if (material->shader_type != p_shader_type) {
  1920. return;
  1921. }
  1922. if (material->data) {
  1923. material->data->update_parameters(material->params, false, true);
  1924. }
  1925. }
  1926. void RasterizerStorageRD::_update_queued_materials() {
  1927. Material *material = material_update_list;
  1928. while (material) {
  1929. Material *next = material->update_next;
  1930. if (material->data) {
  1931. material->data->update_parameters(material->params, material->uniform_dirty, material->texture_dirty);
  1932. }
  1933. material->update_requested = false;
  1934. material->texture_dirty = false;
  1935. material->uniform_dirty = false;
  1936. material->update_next = nullptr;
  1937. material = next;
  1938. }
  1939. material_update_list = nullptr;
  1940. }
  1941. /* MESH API */
  1942. RID RasterizerStorageRD::mesh_create() {
  1943. return mesh_owner.make_rid(Mesh());
  1944. }
  1945. /// Returns stride
  1946. void RasterizerStorageRD::mesh_add_surface(RID p_mesh, const RS::SurfaceData &p_surface) {
  1947. Mesh *mesh = mesh_owner.getornull(p_mesh);
  1948. ERR_FAIL_COND(!mesh);
  1949. //ensure blend shape consistency
  1950. ERR_FAIL_COND(mesh->blend_shape_count && p_surface.blend_shapes.size() != (int)mesh->blend_shape_count);
  1951. ERR_FAIL_COND(mesh->blend_shape_count && p_surface.bone_aabbs.size() != mesh->bone_aabbs.size());
  1952. #ifdef DEBUG_ENABLED
  1953. //do a validation, to catch errors first
  1954. {
  1955. uint32_t stride = 0;
  1956. for (int i = 0; i < RS::ARRAY_WEIGHTS; i++) {
  1957. if ((p_surface.format & (1 << i))) {
  1958. switch (i) {
  1959. case RS::ARRAY_VERTEX: {
  1960. if (p_surface.format & RS::ARRAY_FLAG_USE_2D_VERTICES) {
  1961. stride += sizeof(float) * 2;
  1962. } else {
  1963. stride += sizeof(float) * 3;
  1964. }
  1965. } break;
  1966. case RS::ARRAY_NORMAL: {
  1967. if (p_surface.format & RS::ARRAY_COMPRESS_NORMAL) {
  1968. stride += sizeof(int8_t) * 4;
  1969. } else {
  1970. stride += sizeof(float) * 4;
  1971. }
  1972. } break;
  1973. case RS::ARRAY_TANGENT: {
  1974. if (p_surface.format & RS::ARRAY_COMPRESS_TANGENT) {
  1975. stride += sizeof(int8_t) * 4;
  1976. } else {
  1977. stride += sizeof(float) * 4;
  1978. }
  1979. } break;
  1980. case RS::ARRAY_COLOR: {
  1981. if (p_surface.format & RS::ARRAY_COMPRESS_COLOR) {
  1982. stride += sizeof(int8_t) * 4;
  1983. } else {
  1984. stride += sizeof(float) * 4;
  1985. }
  1986. } break;
  1987. case RS::ARRAY_TEX_UV: {
  1988. if (p_surface.format & RS::ARRAY_COMPRESS_TEX_UV) {
  1989. stride += sizeof(int16_t) * 2;
  1990. } else {
  1991. stride += sizeof(float) * 2;
  1992. }
  1993. } break;
  1994. case RS::ARRAY_TEX_UV2: {
  1995. if (p_surface.format & RS::ARRAY_COMPRESS_TEX_UV2) {
  1996. stride += sizeof(int16_t) * 2;
  1997. } else {
  1998. stride += sizeof(float) * 2;
  1999. }
  2000. } break;
  2001. case RS::ARRAY_BONES: {
  2002. //assumed weights too
  2003. //unique format, internally 16 bits, exposed as single array for 32
  2004. stride += sizeof(int32_t) * 4;
  2005. } break;
  2006. }
  2007. }
  2008. }
  2009. int expected_size = stride * p_surface.vertex_count;
  2010. ERR_FAIL_COND_MSG(expected_size != p_surface.vertex_data.size(), "Size of data provided (" + itos(p_surface.vertex_data.size()) + ") does not match expected (" + itos(expected_size) + ")");
  2011. }
  2012. #endif
  2013. Mesh::Surface *s = memnew(Mesh::Surface);
  2014. s->format = p_surface.format;
  2015. s->primitive = p_surface.primitive;
  2016. s->vertex_buffer = RD::get_singleton()->vertex_buffer_create(p_surface.vertex_data.size(), p_surface.vertex_data);
  2017. s->vertex_count = p_surface.vertex_count;
  2018. if (p_surface.index_count) {
  2019. bool is_index_16 = p_surface.vertex_count <= 65536;
  2020. s->index_buffer = RD::get_singleton()->index_buffer_create(p_surface.index_count, is_index_16 ? RD::INDEX_BUFFER_FORMAT_UINT16 : RD::INDEX_BUFFER_FORMAT_UINT32, p_surface.index_data, false);
  2021. s->index_count = p_surface.index_count;
  2022. s->index_array = RD::get_singleton()->index_array_create(s->index_buffer, 0, s->index_count);
  2023. if (p_surface.lods.size()) {
  2024. s->lods = memnew_arr(Mesh::Surface::LOD, p_surface.lods.size());
  2025. s->lod_count = p_surface.lods.size();
  2026. for (int i = 0; i < p_surface.lods.size(); i++) {
  2027. uint32_t indices = p_surface.lods[i].index_data.size() / (is_index_16 ? 2 : 4);
  2028. s->lods[i].index_buffer = RD::get_singleton()->index_buffer_create(indices, is_index_16 ? RD::INDEX_BUFFER_FORMAT_UINT16 : RD::INDEX_BUFFER_FORMAT_UINT32, p_surface.lods[i].index_data);
  2029. s->lods[i].index_array = RD::get_singleton()->index_array_create(s->lods[i].index_buffer, 0, indices);
  2030. s->lods[i].edge_length = p_surface.lods[i].edge_length;
  2031. }
  2032. }
  2033. }
  2034. s->aabb = p_surface.aabb;
  2035. s->bone_aabbs = p_surface.bone_aabbs; //only really useful for returning them.
  2036. for (int i = 0; i < p_surface.blend_shapes.size(); i++) {
  2037. if (p_surface.blend_shapes[i].size() != p_surface.vertex_data.size()) {
  2038. memdelete(s);
  2039. ERR_FAIL_COND(p_surface.blend_shapes[i].size() != p_surface.vertex_data.size());
  2040. }
  2041. RID vertex_buffer = RD::get_singleton()->vertex_buffer_create(p_surface.blend_shapes[i].size(), p_surface.blend_shapes[i]);
  2042. s->blend_shapes.push_back(vertex_buffer);
  2043. }
  2044. mesh->blend_shape_count = p_surface.blend_shapes.size();
  2045. if (mesh->surface_count == 0) {
  2046. mesh->bone_aabbs = p_surface.bone_aabbs;
  2047. mesh->aabb = p_surface.aabb;
  2048. } else {
  2049. for (int i = 0; i < p_surface.bone_aabbs.size(); i++) {
  2050. mesh->bone_aabbs.write[i].merge_with(p_surface.bone_aabbs[i]);
  2051. }
  2052. mesh->aabb.merge_with(p_surface.aabb);
  2053. }
  2054. s->material = p_surface.material;
  2055. mesh->surfaces = (Mesh::Surface **)memrealloc(mesh->surfaces, sizeof(Mesh::Surface *) * (mesh->surface_count + 1));
  2056. mesh->surfaces[mesh->surface_count] = s;
  2057. mesh->surface_count++;
  2058. mesh->instance_dependency.instance_notify_changed(true, true);
  2059. mesh->material_cache.clear();
  2060. }
  2061. int RasterizerStorageRD::mesh_get_blend_shape_count(RID p_mesh) const {
  2062. const Mesh *mesh = mesh_owner.getornull(p_mesh);
  2063. ERR_FAIL_COND_V(!mesh, -1);
  2064. return mesh->blend_shape_count;
  2065. }
  2066. void RasterizerStorageRD::mesh_set_blend_shape_mode(RID p_mesh, RS::BlendShapeMode p_mode) {
  2067. Mesh *mesh = mesh_owner.getornull(p_mesh);
  2068. ERR_FAIL_COND(!mesh);
  2069. ERR_FAIL_INDEX((int)p_mode, 2);
  2070. mesh->blend_shape_mode = p_mode;
  2071. }
  2072. RS::BlendShapeMode RasterizerStorageRD::mesh_get_blend_shape_mode(RID p_mesh) const {
  2073. Mesh *mesh = mesh_owner.getornull(p_mesh);
  2074. ERR_FAIL_COND_V(!mesh, RS::BLEND_SHAPE_MODE_NORMALIZED);
  2075. return mesh->blend_shape_mode;
  2076. }
  2077. void RasterizerStorageRD::mesh_surface_update_region(RID p_mesh, int p_surface, int p_offset, const Vector<uint8_t> &p_data) {
  2078. Mesh *mesh = mesh_owner.getornull(p_mesh);
  2079. ERR_FAIL_COND(!mesh);
  2080. ERR_FAIL_UNSIGNED_INDEX((uint32_t)p_surface, mesh->surface_count);
  2081. ERR_FAIL_COND(p_data.size() == 0);
  2082. uint64_t data_size = p_data.size();
  2083. const uint8_t *r = p_data.ptr();
  2084. RD::get_singleton()->buffer_update(mesh->surfaces[p_surface]->vertex_buffer, p_offset, data_size, r);
  2085. }
  2086. void RasterizerStorageRD::mesh_surface_set_material(RID p_mesh, int p_surface, RID p_material) {
  2087. Mesh *mesh = mesh_owner.getornull(p_mesh);
  2088. ERR_FAIL_COND(!mesh);
  2089. ERR_FAIL_UNSIGNED_INDEX((uint32_t)p_surface, mesh->surface_count);
  2090. mesh->surfaces[p_surface]->material = p_material;
  2091. mesh->instance_dependency.instance_notify_changed(false, true);
  2092. mesh->material_cache.clear();
  2093. }
  2094. RID RasterizerStorageRD::mesh_surface_get_material(RID p_mesh, int p_surface) const {
  2095. Mesh *mesh = mesh_owner.getornull(p_mesh);
  2096. ERR_FAIL_COND_V(!mesh, RID());
  2097. ERR_FAIL_UNSIGNED_INDEX_V((uint32_t)p_surface, mesh->surface_count, RID());
  2098. return mesh->surfaces[p_surface]->material;
  2099. }
  2100. RS::SurfaceData RasterizerStorageRD::mesh_get_surface(RID p_mesh, int p_surface) const {
  2101. Mesh *mesh = mesh_owner.getornull(p_mesh);
  2102. ERR_FAIL_COND_V(!mesh, RS::SurfaceData());
  2103. ERR_FAIL_UNSIGNED_INDEX_V((uint32_t)p_surface, mesh->surface_count, RS::SurfaceData());
  2104. Mesh::Surface &s = *mesh->surfaces[p_surface];
  2105. RS::SurfaceData sd;
  2106. sd.format = s.format;
  2107. sd.vertex_data = RD::get_singleton()->buffer_get_data(s.vertex_buffer);
  2108. sd.vertex_count = s.vertex_count;
  2109. sd.index_count = s.index_count;
  2110. sd.primitive = s.primitive;
  2111. if (sd.index_count) {
  2112. sd.index_data = RD::get_singleton()->buffer_get_data(s.index_buffer);
  2113. }
  2114. sd.aabb = s.aabb;
  2115. for (uint32_t i = 0; i < s.lod_count; i++) {
  2116. RS::SurfaceData::LOD lod;
  2117. lod.edge_length = s.lods[i].edge_length;
  2118. lod.index_data = RD::get_singleton()->buffer_get_data(s.lods[i].index_buffer);
  2119. sd.lods.push_back(lod);
  2120. }
  2121. sd.bone_aabbs = s.bone_aabbs;
  2122. for (int i = 0; i < s.blend_shapes.size(); i++) {
  2123. Vector<uint8_t> bs = RD::get_singleton()->buffer_get_data(s.blend_shapes[i]);
  2124. sd.blend_shapes.push_back(bs);
  2125. }
  2126. return sd;
  2127. }
  2128. int RasterizerStorageRD::mesh_get_surface_count(RID p_mesh) const {
  2129. Mesh *mesh = mesh_owner.getornull(p_mesh);
  2130. ERR_FAIL_COND_V(!mesh, 0);
  2131. return mesh->surface_count;
  2132. }
  2133. void RasterizerStorageRD::mesh_set_custom_aabb(RID p_mesh, const AABB &p_aabb) {
  2134. Mesh *mesh = mesh_owner.getornull(p_mesh);
  2135. ERR_FAIL_COND(!mesh);
  2136. mesh->custom_aabb = p_aabb;
  2137. }
  2138. AABB RasterizerStorageRD::mesh_get_custom_aabb(RID p_mesh) const {
  2139. Mesh *mesh = mesh_owner.getornull(p_mesh);
  2140. ERR_FAIL_COND_V(!mesh, AABB());
  2141. return mesh->custom_aabb;
  2142. }
  2143. AABB RasterizerStorageRD::mesh_get_aabb(RID p_mesh, RID p_skeleton) {
  2144. Mesh *mesh = mesh_owner.getornull(p_mesh);
  2145. ERR_FAIL_COND_V(!mesh, AABB());
  2146. if (mesh->custom_aabb != AABB()) {
  2147. return mesh->custom_aabb;
  2148. }
  2149. Skeleton *skeleton = skeleton_owner.getornull(p_skeleton);
  2150. if (!skeleton || skeleton->size == 0) {
  2151. return mesh->aabb;
  2152. }
  2153. AABB aabb;
  2154. for (uint32_t i = 0; i < mesh->surface_count; i++) {
  2155. AABB laabb;
  2156. if ((mesh->surfaces[i]->format & RS::ARRAY_FORMAT_BONES) && mesh->surfaces[i]->bone_aabbs.size()) {
  2157. int bs = mesh->surfaces[i]->bone_aabbs.size();
  2158. const AABB *skbones = mesh->surfaces[i]->bone_aabbs.ptr();
  2159. int sbs = skeleton->size;
  2160. ERR_CONTINUE(bs > sbs);
  2161. const float *baseptr = skeleton->data.ptr();
  2162. bool first = true;
  2163. if (skeleton->use_2d) {
  2164. for (int j = 0; j < bs; j++) {
  2165. if (skbones[0].size == Vector3()) {
  2166. continue; //bone is unused
  2167. }
  2168. const float *dataptr = baseptr + j * 8;
  2169. Transform mtx;
  2170. mtx.basis.elements[0].x = dataptr[0];
  2171. mtx.basis.elements[1].x = dataptr[1];
  2172. mtx.origin.x = dataptr[3];
  2173. mtx.basis.elements[0].y = dataptr[4];
  2174. mtx.basis.elements[1].y = dataptr[5];
  2175. mtx.origin.y = dataptr[7];
  2176. AABB baabb = mtx.xform(skbones[j]);
  2177. if (first) {
  2178. laabb = baabb;
  2179. first = false;
  2180. } else {
  2181. laabb.merge_with(baabb);
  2182. }
  2183. }
  2184. } else {
  2185. for (int j = 0; j < bs; j++) {
  2186. if (skbones[0].size == Vector3()) {
  2187. continue; //bone is unused
  2188. }
  2189. const float *dataptr = baseptr + j * 12;
  2190. Transform mtx;
  2191. mtx.basis.elements[0][0] = dataptr[0];
  2192. mtx.basis.elements[0][1] = dataptr[1];
  2193. mtx.basis.elements[0][2] = dataptr[2];
  2194. mtx.origin.x = dataptr[3];
  2195. mtx.basis.elements[1][0] = dataptr[4];
  2196. mtx.basis.elements[1][1] = dataptr[5];
  2197. mtx.basis.elements[1][2] = dataptr[6];
  2198. mtx.origin.y = dataptr[7];
  2199. mtx.basis.elements[2][0] = dataptr[8];
  2200. mtx.basis.elements[2][1] = dataptr[9];
  2201. mtx.basis.elements[2][2] = dataptr[10];
  2202. mtx.origin.z = dataptr[11];
  2203. AABB baabb = mtx.xform(skbones[j]);
  2204. if (first) {
  2205. laabb = baabb;
  2206. first = false;
  2207. } else {
  2208. laabb.merge_with(baabb);
  2209. }
  2210. }
  2211. }
  2212. if (laabb.size == Vector3()) {
  2213. laabb = mesh->surfaces[i]->aabb;
  2214. }
  2215. } else {
  2216. laabb = mesh->surfaces[i]->aabb;
  2217. }
  2218. if (i == 0) {
  2219. aabb = laabb;
  2220. } else {
  2221. aabb.merge_with(laabb);
  2222. }
  2223. }
  2224. return aabb;
  2225. }
  2226. void RasterizerStorageRD::mesh_clear(RID p_mesh) {
  2227. Mesh *mesh = mesh_owner.getornull(p_mesh);
  2228. ERR_FAIL_COND(!mesh);
  2229. for (uint32_t i = 0; i < mesh->surface_count; i++) {
  2230. Mesh::Surface &s = *mesh->surfaces[i];
  2231. RD::get_singleton()->free(s.vertex_buffer); //clears arrays as dependency automatically, including all versions
  2232. if (s.versions) {
  2233. memfree(s.versions); //reallocs, so free with memfree.
  2234. }
  2235. if (s.index_buffer.is_valid()) {
  2236. RD::get_singleton()->free(s.index_buffer);
  2237. }
  2238. if (s.lod_count) {
  2239. for (uint32_t j = 0; j < s.lod_count; j++) {
  2240. RD::get_singleton()->free(s.lods[j].index_buffer);
  2241. }
  2242. memdelete_arr(s.lods);
  2243. }
  2244. for (int32_t j = 0; j < s.blend_shapes.size(); j++) {
  2245. RD::get_singleton()->free(s.blend_shapes[j]);
  2246. }
  2247. if (s.blend_shape_base_buffer.is_valid()) {
  2248. RD::get_singleton()->free(s.blend_shape_base_buffer);
  2249. }
  2250. memdelete(mesh->surfaces[i]);
  2251. }
  2252. if (mesh->surfaces) {
  2253. memfree(mesh->surfaces);
  2254. }
  2255. mesh->surfaces = nullptr;
  2256. mesh->surface_count = 0;
  2257. mesh->material_cache.clear();
  2258. mesh->instance_dependency.instance_notify_changed(true, true);
  2259. }
  2260. void RasterizerStorageRD::_mesh_surface_generate_version_for_input_mask(Mesh::Surface *s, uint32_t p_input_mask) {
  2261. uint32_t version = s->version_count;
  2262. s->version_count++;
  2263. s->versions = (Mesh::Surface::Version *)memrealloc(s->versions, sizeof(Mesh::Surface::Version) * s->version_count);
  2264. Mesh::Surface::Version &v = s->versions[version];
  2265. Vector<RD::VertexAttribute> attributes;
  2266. Vector<RID> buffers;
  2267. uint32_t stride = 0;
  2268. for (int i = 0; i < RS::ARRAY_WEIGHTS; i++) {
  2269. RD::VertexAttribute vd;
  2270. RID buffer;
  2271. vd.location = i;
  2272. if (!(s->format & (1 << i))) {
  2273. // Not supplied by surface, use default value
  2274. buffer = mesh_default_rd_buffers[i];
  2275. switch (i) {
  2276. case RS::ARRAY_VERTEX: {
  2277. vd.format = RD::DATA_FORMAT_R32G32B32_SFLOAT;
  2278. } break;
  2279. case RS::ARRAY_NORMAL: {
  2280. vd.format = RD::DATA_FORMAT_R32G32B32_SFLOAT;
  2281. } break;
  2282. case RS::ARRAY_TANGENT: {
  2283. vd.format = RD::DATA_FORMAT_R32G32B32A32_SFLOAT;
  2284. } break;
  2285. case RS::ARRAY_COLOR: {
  2286. vd.format = RD::DATA_FORMAT_R32G32B32A32_SFLOAT;
  2287. } break;
  2288. case RS::ARRAY_TEX_UV: {
  2289. vd.format = RD::DATA_FORMAT_R32G32_SFLOAT;
  2290. } break;
  2291. case RS::ARRAY_TEX_UV2: {
  2292. vd.format = RD::DATA_FORMAT_R32G32_SFLOAT;
  2293. } break;
  2294. case RS::ARRAY_BONES: {
  2295. //assumed weights too
  2296. vd.format = RD::DATA_FORMAT_R32G32B32A32_UINT;
  2297. } break;
  2298. }
  2299. } else {
  2300. //Supplied, use it
  2301. vd.offset = stride;
  2302. vd.stride = 1; //mark that it needs a stride set
  2303. buffer = s->vertex_buffer;
  2304. switch (i) {
  2305. case RS::ARRAY_VERTEX: {
  2306. if (s->format & RS::ARRAY_FLAG_USE_2D_VERTICES) {
  2307. vd.format = RD::DATA_FORMAT_R32G32_SFLOAT;
  2308. stride += sizeof(float) * 2;
  2309. } else {
  2310. vd.format = RD::DATA_FORMAT_R32G32B32_SFLOAT;
  2311. stride += sizeof(float) * 3;
  2312. }
  2313. } break;
  2314. case RS::ARRAY_NORMAL: {
  2315. if (s->format & RS::ARRAY_COMPRESS_NORMAL) {
  2316. vd.format = RD::DATA_FORMAT_R8G8B8A8_SNORM;
  2317. stride += sizeof(int8_t) * 4;
  2318. } else {
  2319. vd.format = RD::DATA_FORMAT_R32G32B32A32_SFLOAT;
  2320. stride += sizeof(float) * 4;
  2321. }
  2322. } break;
  2323. case RS::ARRAY_TANGENT: {
  2324. if (s->format & RS::ARRAY_COMPRESS_TANGENT) {
  2325. vd.format = RD::DATA_FORMAT_R8G8B8A8_SNORM;
  2326. stride += sizeof(int8_t) * 4;
  2327. } else {
  2328. vd.format = RD::DATA_FORMAT_R32G32B32A32_SFLOAT;
  2329. stride += sizeof(float) * 4;
  2330. }
  2331. } break;
  2332. case RS::ARRAY_COLOR: {
  2333. if (s->format & RS::ARRAY_COMPRESS_COLOR) {
  2334. vd.format = RD::DATA_FORMAT_R8G8B8A8_UNORM;
  2335. stride += sizeof(int8_t) * 4;
  2336. } else {
  2337. vd.format = RD::DATA_FORMAT_R32G32B32A32_SFLOAT;
  2338. stride += sizeof(float) * 4;
  2339. }
  2340. } break;
  2341. case RS::ARRAY_TEX_UV: {
  2342. if (s->format & RS::ARRAY_COMPRESS_TEX_UV) {
  2343. vd.format = RD::DATA_FORMAT_R16G16_SFLOAT;
  2344. stride += sizeof(int16_t) * 2;
  2345. } else {
  2346. vd.format = RD::DATA_FORMAT_R32G32_SFLOAT;
  2347. stride += sizeof(float) * 2;
  2348. }
  2349. } break;
  2350. case RS::ARRAY_TEX_UV2: {
  2351. if (s->format & RS::ARRAY_COMPRESS_TEX_UV2) {
  2352. vd.format = RD::DATA_FORMAT_R16G16_SFLOAT;
  2353. stride += sizeof(int16_t) * 2;
  2354. } else {
  2355. vd.format = RD::DATA_FORMAT_R32G32_SFLOAT;
  2356. stride += sizeof(float) * 2;
  2357. }
  2358. } break;
  2359. case RS::ARRAY_BONES: {
  2360. //assumed weights too
  2361. //unique format, internally 16 bits, exposed as single array for 32
  2362. vd.format = RD::DATA_FORMAT_R32G32B32A32_UINT;
  2363. stride += sizeof(int32_t) * 4;
  2364. } break;
  2365. }
  2366. }
  2367. if (!(p_input_mask & (1 << i))) {
  2368. continue; // Shader does not need this, skip it
  2369. }
  2370. attributes.push_back(vd);
  2371. buffers.push_back(buffer);
  2372. }
  2373. //update final stride
  2374. for (int i = 0; i < attributes.size(); i++) {
  2375. if (attributes[i].stride == 1) {
  2376. attributes.write[i].stride = stride;
  2377. }
  2378. }
  2379. v.input_mask = p_input_mask;
  2380. v.vertex_format = RD::get_singleton()->vertex_format_create(attributes);
  2381. v.vertex_array = RD::get_singleton()->vertex_array_create(s->vertex_count, v.vertex_format, buffers);
  2382. }
  2383. ////////////////// MULTIMESH
  2384. RID RasterizerStorageRD::multimesh_create() {
  2385. return multimesh_owner.make_rid(MultiMesh());
  2386. }
  2387. void RasterizerStorageRD::multimesh_allocate(RID p_multimesh, int p_instances, RS::MultimeshTransformFormat p_transform_format, bool p_use_colors, bool p_use_custom_data) {
  2388. MultiMesh *multimesh = multimesh_owner.getornull(p_multimesh);
  2389. ERR_FAIL_COND(!multimesh);
  2390. if (multimesh->instances == p_instances && multimesh->xform_format == p_transform_format && multimesh->uses_colors == p_use_colors && multimesh->uses_custom_data == p_use_custom_data) {
  2391. return;
  2392. }
  2393. if (multimesh->buffer.is_valid()) {
  2394. RD::get_singleton()->free(multimesh->buffer);
  2395. multimesh->buffer = RID();
  2396. multimesh->uniform_set_3d = RID(); //cleared by dependency
  2397. }
  2398. if (multimesh->data_cache_dirty_regions) {
  2399. memdelete_arr(multimesh->data_cache_dirty_regions);
  2400. multimesh->data_cache_dirty_regions = nullptr;
  2401. multimesh->data_cache_used_dirty_regions = 0;
  2402. }
  2403. multimesh->instances = p_instances;
  2404. multimesh->xform_format = p_transform_format;
  2405. multimesh->uses_colors = p_use_colors;
  2406. multimesh->color_offset_cache = p_transform_format == RS::MULTIMESH_TRANSFORM_2D ? 8 : 12;
  2407. multimesh->uses_custom_data = p_use_custom_data;
  2408. multimesh->custom_data_offset_cache = multimesh->color_offset_cache + (p_use_colors ? 4 : 0);
  2409. multimesh->stride_cache = multimesh->custom_data_offset_cache + (p_use_custom_data ? 4 : 0);
  2410. multimesh->buffer_set = false;
  2411. //print_line("allocate, elements: " + itos(p_instances) + " 2D: " + itos(p_transform_format == RS::MULTIMESH_TRANSFORM_2D) + " colors " + itos(multimesh->uses_colors) + " data " + itos(multimesh->uses_custom_data) + " stride " + itos(multimesh->stride_cache) + " total size " + itos(multimesh->stride_cache * multimesh->instances));
  2412. multimesh->data_cache = Vector<float>();
  2413. multimesh->aabb = AABB();
  2414. multimesh->aabb_dirty = false;
  2415. multimesh->visible_instances = MIN(multimesh->visible_instances, multimesh->instances);
  2416. if (multimesh->instances) {
  2417. multimesh->buffer = RD::get_singleton()->storage_buffer_create(multimesh->instances * multimesh->stride_cache * 4);
  2418. }
  2419. }
  2420. int RasterizerStorageRD::multimesh_get_instance_count(RID p_multimesh) const {
  2421. MultiMesh *multimesh = multimesh_owner.getornull(p_multimesh);
  2422. ERR_FAIL_COND_V(!multimesh, 0);
  2423. return multimesh->instances;
  2424. }
  2425. void RasterizerStorageRD::multimesh_set_mesh(RID p_multimesh, RID p_mesh) {
  2426. MultiMesh *multimesh = multimesh_owner.getornull(p_multimesh);
  2427. ERR_FAIL_COND(!multimesh);
  2428. if (multimesh->mesh == p_mesh) {
  2429. return;
  2430. }
  2431. multimesh->mesh = p_mesh;
  2432. if (multimesh->instances == 0) {
  2433. return;
  2434. }
  2435. if (multimesh->data_cache.size()) {
  2436. //we have a data cache, just mark it dirt
  2437. _multimesh_mark_all_dirty(multimesh, false, true);
  2438. } else if (multimesh->instances) {
  2439. //need to re-create AABB unfortunately, calling this has a penalty
  2440. if (multimesh->buffer_set) {
  2441. Vector<uint8_t> buffer = RD::get_singleton()->buffer_get_data(multimesh->buffer);
  2442. const uint8_t *r = buffer.ptr();
  2443. const float *data = (const float *)r;
  2444. _multimesh_re_create_aabb(multimesh, data, multimesh->instances);
  2445. }
  2446. }
  2447. multimesh->instance_dependency.instance_notify_changed(true, true);
  2448. }
  2449. #define MULTIMESH_DIRTY_REGION_SIZE 512
  2450. void RasterizerStorageRD::_multimesh_make_local(MultiMesh *multimesh) const {
  2451. if (multimesh->data_cache.size() > 0) {
  2452. return; //already local
  2453. }
  2454. ERR_FAIL_COND(multimesh->data_cache.size() > 0);
  2455. // this means that the user wants to load/save individual elements,
  2456. // for this, the data must reside on CPU, so just copy it there.
  2457. multimesh->data_cache.resize(multimesh->instances * multimesh->stride_cache);
  2458. {
  2459. float *w = multimesh->data_cache.ptrw();
  2460. if (multimesh->buffer_set) {
  2461. Vector<uint8_t> buffer = RD::get_singleton()->buffer_get_data(multimesh->buffer);
  2462. {
  2463. const uint8_t *r = buffer.ptr();
  2464. copymem(w, r, buffer.size());
  2465. }
  2466. } else {
  2467. zeromem(w, multimesh->instances * multimesh->stride_cache * sizeof(float));
  2468. }
  2469. }
  2470. uint32_t data_cache_dirty_region_count = (multimesh->instances - 1) / MULTIMESH_DIRTY_REGION_SIZE + 1;
  2471. multimesh->data_cache_dirty_regions = memnew_arr(bool, data_cache_dirty_region_count);
  2472. for (uint32_t i = 0; i < data_cache_dirty_region_count; i++) {
  2473. multimesh->data_cache_dirty_regions[i] = false;
  2474. }
  2475. multimesh->data_cache_used_dirty_regions = 0;
  2476. }
  2477. void RasterizerStorageRD::_multimesh_mark_dirty(MultiMesh *multimesh, int p_index, bool p_aabb) {
  2478. uint32_t region_index = p_index / MULTIMESH_DIRTY_REGION_SIZE;
  2479. #ifdef DEBUG_ENABLED
  2480. uint32_t data_cache_dirty_region_count = (multimesh->instances - 1) / MULTIMESH_DIRTY_REGION_SIZE + 1;
  2481. ERR_FAIL_UNSIGNED_INDEX(region_index, data_cache_dirty_region_count); //bug
  2482. #endif
  2483. if (!multimesh->data_cache_dirty_regions[region_index]) {
  2484. multimesh->data_cache_dirty_regions[region_index] = true;
  2485. multimesh->data_cache_used_dirty_regions++;
  2486. }
  2487. if (p_aabb) {
  2488. multimesh->aabb_dirty = true;
  2489. }
  2490. if (!multimesh->dirty) {
  2491. multimesh->dirty_list = multimesh_dirty_list;
  2492. multimesh_dirty_list = multimesh;
  2493. multimesh->dirty = true;
  2494. }
  2495. }
  2496. void RasterizerStorageRD::_multimesh_mark_all_dirty(MultiMesh *multimesh, bool p_data, bool p_aabb) {
  2497. if (p_data) {
  2498. uint32_t data_cache_dirty_region_count = (multimesh->instances - 1) / MULTIMESH_DIRTY_REGION_SIZE + 1;
  2499. for (uint32_t i = 0; i < data_cache_dirty_region_count; i++) {
  2500. if (!multimesh->data_cache_dirty_regions[i]) {
  2501. multimesh->data_cache_dirty_regions[i] = true;
  2502. multimesh->data_cache_used_dirty_regions++;
  2503. }
  2504. }
  2505. }
  2506. if (p_aabb) {
  2507. multimesh->aabb_dirty = true;
  2508. }
  2509. if (!multimesh->dirty) {
  2510. multimesh->dirty_list = multimesh_dirty_list;
  2511. multimesh_dirty_list = multimesh;
  2512. multimesh->dirty = true;
  2513. }
  2514. }
  2515. void RasterizerStorageRD::_multimesh_re_create_aabb(MultiMesh *multimesh, const float *p_data, int p_instances) {
  2516. ERR_FAIL_COND(multimesh->mesh.is_null());
  2517. AABB aabb;
  2518. AABB mesh_aabb = mesh_get_aabb(multimesh->mesh);
  2519. for (int i = 0; i < p_instances; i++) {
  2520. const float *data = p_data + multimesh->stride_cache * i;
  2521. Transform t;
  2522. if (multimesh->xform_format == RS::MULTIMESH_TRANSFORM_3D) {
  2523. t.basis.elements[0][0] = data[0];
  2524. t.basis.elements[0][1] = data[1];
  2525. t.basis.elements[0][2] = data[2];
  2526. t.origin.x = data[3];
  2527. t.basis.elements[1][0] = data[4];
  2528. t.basis.elements[1][1] = data[5];
  2529. t.basis.elements[1][2] = data[6];
  2530. t.origin.y = data[7];
  2531. t.basis.elements[2][0] = data[8];
  2532. t.basis.elements[2][1] = data[9];
  2533. t.basis.elements[2][2] = data[10];
  2534. t.origin.z = data[11];
  2535. } else {
  2536. t.basis.elements[0].x = data[0];
  2537. t.basis.elements[1].x = data[1];
  2538. t.origin.x = data[3];
  2539. t.basis.elements[0].y = data[4];
  2540. t.basis.elements[1].y = data[5];
  2541. t.origin.y = data[7];
  2542. }
  2543. if (i == 0) {
  2544. aabb = t.xform(mesh_aabb);
  2545. } else {
  2546. aabb.merge_with(t.xform(mesh_aabb));
  2547. }
  2548. }
  2549. multimesh->aabb = aabb;
  2550. }
  2551. void RasterizerStorageRD::multimesh_instance_set_transform(RID p_multimesh, int p_index, const Transform &p_transform) {
  2552. MultiMesh *multimesh = multimesh_owner.getornull(p_multimesh);
  2553. ERR_FAIL_COND(!multimesh);
  2554. ERR_FAIL_INDEX(p_index, multimesh->instances);
  2555. ERR_FAIL_COND(multimesh->xform_format != RS::MULTIMESH_TRANSFORM_3D);
  2556. _multimesh_make_local(multimesh);
  2557. {
  2558. float *w = multimesh->data_cache.ptrw();
  2559. float *dataptr = w + p_index * multimesh->stride_cache;
  2560. dataptr[0] = p_transform.basis.elements[0][0];
  2561. dataptr[1] = p_transform.basis.elements[0][1];
  2562. dataptr[2] = p_transform.basis.elements[0][2];
  2563. dataptr[3] = p_transform.origin.x;
  2564. dataptr[4] = p_transform.basis.elements[1][0];
  2565. dataptr[5] = p_transform.basis.elements[1][1];
  2566. dataptr[6] = p_transform.basis.elements[1][2];
  2567. dataptr[7] = p_transform.origin.y;
  2568. dataptr[8] = p_transform.basis.elements[2][0];
  2569. dataptr[9] = p_transform.basis.elements[2][1];
  2570. dataptr[10] = p_transform.basis.elements[2][2];
  2571. dataptr[11] = p_transform.origin.z;
  2572. }
  2573. _multimesh_mark_dirty(multimesh, p_index, true);
  2574. }
  2575. void RasterizerStorageRD::multimesh_instance_set_transform_2d(RID p_multimesh, int p_index, const Transform2D &p_transform) {
  2576. MultiMesh *multimesh = multimesh_owner.getornull(p_multimesh);
  2577. ERR_FAIL_COND(!multimesh);
  2578. ERR_FAIL_INDEX(p_index, multimesh->instances);
  2579. ERR_FAIL_COND(multimesh->xform_format != RS::MULTIMESH_TRANSFORM_2D);
  2580. _multimesh_make_local(multimesh);
  2581. {
  2582. float *w = multimesh->data_cache.ptrw();
  2583. float *dataptr = w + p_index * multimesh->stride_cache;
  2584. dataptr[0] = p_transform.elements[0][0];
  2585. dataptr[1] = p_transform.elements[1][0];
  2586. dataptr[2] = 0;
  2587. dataptr[3] = p_transform.elements[2][0];
  2588. dataptr[4] = p_transform.elements[0][1];
  2589. dataptr[5] = p_transform.elements[1][1];
  2590. dataptr[6] = 0;
  2591. dataptr[7] = p_transform.elements[2][1];
  2592. }
  2593. _multimesh_mark_dirty(multimesh, p_index, true);
  2594. }
  2595. void RasterizerStorageRD::multimesh_instance_set_color(RID p_multimesh, int p_index, const Color &p_color) {
  2596. MultiMesh *multimesh = multimesh_owner.getornull(p_multimesh);
  2597. ERR_FAIL_COND(!multimesh);
  2598. ERR_FAIL_INDEX(p_index, multimesh->instances);
  2599. ERR_FAIL_COND(!multimesh->uses_colors);
  2600. _multimesh_make_local(multimesh);
  2601. {
  2602. float *w = multimesh->data_cache.ptrw();
  2603. float *dataptr = w + p_index * multimesh->stride_cache + multimesh->color_offset_cache;
  2604. dataptr[0] = p_color.r;
  2605. dataptr[1] = p_color.g;
  2606. dataptr[2] = p_color.b;
  2607. dataptr[3] = p_color.a;
  2608. }
  2609. _multimesh_mark_dirty(multimesh, p_index, false);
  2610. }
  2611. void RasterizerStorageRD::multimesh_instance_set_custom_data(RID p_multimesh, int p_index, const Color &p_color) {
  2612. MultiMesh *multimesh = multimesh_owner.getornull(p_multimesh);
  2613. ERR_FAIL_COND(!multimesh);
  2614. ERR_FAIL_INDEX(p_index, multimesh->instances);
  2615. ERR_FAIL_COND(!multimesh->uses_custom_data);
  2616. _multimesh_make_local(multimesh);
  2617. {
  2618. float *w = multimesh->data_cache.ptrw();
  2619. float *dataptr = w + p_index * multimesh->stride_cache + multimesh->custom_data_offset_cache;
  2620. dataptr[0] = p_color.r;
  2621. dataptr[1] = p_color.g;
  2622. dataptr[2] = p_color.b;
  2623. dataptr[3] = p_color.a;
  2624. }
  2625. _multimesh_mark_dirty(multimesh, p_index, false);
  2626. }
  2627. RID RasterizerStorageRD::multimesh_get_mesh(RID p_multimesh) const {
  2628. MultiMesh *multimesh = multimesh_owner.getornull(p_multimesh);
  2629. ERR_FAIL_COND_V(!multimesh, RID());
  2630. return multimesh->mesh;
  2631. }
  2632. Transform RasterizerStorageRD::multimesh_instance_get_transform(RID p_multimesh, int p_index) const {
  2633. MultiMesh *multimesh = multimesh_owner.getornull(p_multimesh);
  2634. ERR_FAIL_COND_V(!multimesh, Transform());
  2635. ERR_FAIL_INDEX_V(p_index, multimesh->instances, Transform());
  2636. ERR_FAIL_COND_V(multimesh->xform_format != RS::MULTIMESH_TRANSFORM_3D, Transform());
  2637. _multimesh_make_local(multimesh);
  2638. Transform t;
  2639. {
  2640. const float *r = multimesh->data_cache.ptr();
  2641. const float *dataptr = r + p_index * multimesh->stride_cache;
  2642. t.basis.elements[0][0] = dataptr[0];
  2643. t.basis.elements[0][1] = dataptr[1];
  2644. t.basis.elements[0][2] = dataptr[2];
  2645. t.origin.x = dataptr[3];
  2646. t.basis.elements[1][0] = dataptr[4];
  2647. t.basis.elements[1][1] = dataptr[5];
  2648. t.basis.elements[1][2] = dataptr[6];
  2649. t.origin.y = dataptr[7];
  2650. t.basis.elements[2][0] = dataptr[8];
  2651. t.basis.elements[2][1] = dataptr[9];
  2652. t.basis.elements[2][2] = dataptr[10];
  2653. t.origin.z = dataptr[11];
  2654. }
  2655. return t;
  2656. }
  2657. Transform2D RasterizerStorageRD::multimesh_instance_get_transform_2d(RID p_multimesh, int p_index) const {
  2658. MultiMesh *multimesh = multimesh_owner.getornull(p_multimesh);
  2659. ERR_FAIL_COND_V(!multimesh, Transform2D());
  2660. ERR_FAIL_INDEX_V(p_index, multimesh->instances, Transform2D());
  2661. ERR_FAIL_COND_V(multimesh->xform_format != RS::MULTIMESH_TRANSFORM_2D, Transform2D());
  2662. _multimesh_make_local(multimesh);
  2663. Transform2D t;
  2664. {
  2665. const float *r = multimesh->data_cache.ptr();
  2666. const float *dataptr = r + p_index * multimesh->stride_cache;
  2667. t.elements[0][0] = dataptr[0];
  2668. t.elements[1][0] = dataptr[1];
  2669. t.elements[2][0] = dataptr[3];
  2670. t.elements[0][1] = dataptr[4];
  2671. t.elements[1][1] = dataptr[5];
  2672. t.elements[2][1] = dataptr[7];
  2673. }
  2674. return t;
  2675. }
  2676. Color RasterizerStorageRD::multimesh_instance_get_color(RID p_multimesh, int p_index) const {
  2677. MultiMesh *multimesh = multimesh_owner.getornull(p_multimesh);
  2678. ERR_FAIL_COND_V(!multimesh, Color());
  2679. ERR_FAIL_INDEX_V(p_index, multimesh->instances, Color());
  2680. ERR_FAIL_COND_V(!multimesh->uses_colors, Color());
  2681. _multimesh_make_local(multimesh);
  2682. Color c;
  2683. {
  2684. const float *r = multimesh->data_cache.ptr();
  2685. const float *dataptr = r + p_index * multimesh->stride_cache + multimesh->color_offset_cache;
  2686. c.r = dataptr[0];
  2687. c.g = dataptr[1];
  2688. c.b = dataptr[2];
  2689. c.a = dataptr[3];
  2690. }
  2691. return c;
  2692. }
  2693. Color RasterizerStorageRD::multimesh_instance_get_custom_data(RID p_multimesh, int p_index) const {
  2694. MultiMesh *multimesh = multimesh_owner.getornull(p_multimesh);
  2695. ERR_FAIL_COND_V(!multimesh, Color());
  2696. ERR_FAIL_INDEX_V(p_index, multimesh->instances, Color());
  2697. ERR_FAIL_COND_V(!multimesh->uses_custom_data, Color());
  2698. _multimesh_make_local(multimesh);
  2699. Color c;
  2700. {
  2701. const float *r = multimesh->data_cache.ptr();
  2702. const float *dataptr = r + p_index * multimesh->stride_cache + multimesh->custom_data_offset_cache;
  2703. c.r = dataptr[0];
  2704. c.g = dataptr[1];
  2705. c.b = dataptr[2];
  2706. c.a = dataptr[3];
  2707. }
  2708. return c;
  2709. }
  2710. void RasterizerStorageRD::multimesh_set_buffer(RID p_multimesh, const Vector<float> &p_buffer) {
  2711. MultiMesh *multimesh = multimesh_owner.getornull(p_multimesh);
  2712. ERR_FAIL_COND(!multimesh);
  2713. ERR_FAIL_COND(p_buffer.size() != (multimesh->instances * (int)multimesh->stride_cache));
  2714. {
  2715. const float *r = p_buffer.ptr();
  2716. RD::get_singleton()->buffer_update(multimesh->buffer, 0, p_buffer.size() * sizeof(float), r, false);
  2717. multimesh->buffer_set = true;
  2718. }
  2719. if (multimesh->data_cache.size()) {
  2720. //if we have a data cache, just update it
  2721. multimesh->data_cache = p_buffer;
  2722. {
  2723. //clear dirty since nothing will be dirty anymore
  2724. uint32_t data_cache_dirty_region_count = (multimesh->instances - 1) / MULTIMESH_DIRTY_REGION_SIZE + 1;
  2725. for (uint32_t i = 0; i < data_cache_dirty_region_count; i++) {
  2726. multimesh->data_cache_dirty_regions[i] = false;
  2727. }
  2728. multimesh->data_cache_used_dirty_regions = 0;
  2729. }
  2730. _multimesh_mark_all_dirty(multimesh, false, true); //update AABB
  2731. } else if (multimesh->mesh.is_valid()) {
  2732. //if we have a mesh set, we need to re-generate the AABB from the new data
  2733. const float *data = p_buffer.ptr();
  2734. _multimesh_re_create_aabb(multimesh, data, multimesh->instances);
  2735. multimesh->instance_dependency.instance_notify_changed(true, false);
  2736. }
  2737. }
  2738. Vector<float> RasterizerStorageRD::multimesh_get_buffer(RID p_multimesh) const {
  2739. MultiMesh *multimesh = multimesh_owner.getornull(p_multimesh);
  2740. ERR_FAIL_COND_V(!multimesh, Vector<float>());
  2741. if (multimesh->buffer.is_null()) {
  2742. return Vector<float>();
  2743. } else if (multimesh->data_cache.size()) {
  2744. return multimesh->data_cache;
  2745. } else {
  2746. //get from memory
  2747. Vector<uint8_t> buffer = RD::get_singleton()->buffer_get_data(multimesh->buffer);
  2748. Vector<float> ret;
  2749. ret.resize(multimesh->instances * multimesh->stride_cache);
  2750. {
  2751. float *w = ret.ptrw();
  2752. const uint8_t *r = buffer.ptr();
  2753. copymem(w, r, buffer.size());
  2754. }
  2755. return ret;
  2756. }
  2757. }
  2758. void RasterizerStorageRD::multimesh_set_visible_instances(RID p_multimesh, int p_visible) {
  2759. MultiMesh *multimesh = multimesh_owner.getornull(p_multimesh);
  2760. ERR_FAIL_COND(!multimesh);
  2761. ERR_FAIL_COND(p_visible < -1 || p_visible > multimesh->instances);
  2762. if (multimesh->visible_instances == p_visible) {
  2763. return;
  2764. }
  2765. if (multimesh->data_cache.size()) {
  2766. //there is a data cache..
  2767. _multimesh_mark_all_dirty(multimesh, false, true);
  2768. }
  2769. multimesh->visible_instances = p_visible;
  2770. }
  2771. int RasterizerStorageRD::multimesh_get_visible_instances(RID p_multimesh) const {
  2772. MultiMesh *multimesh = multimesh_owner.getornull(p_multimesh);
  2773. ERR_FAIL_COND_V(!multimesh, 0);
  2774. return multimesh->visible_instances;
  2775. }
  2776. AABB RasterizerStorageRD::multimesh_get_aabb(RID p_multimesh) const {
  2777. MultiMesh *multimesh = multimesh_owner.getornull(p_multimesh);
  2778. ERR_FAIL_COND_V(!multimesh, AABB());
  2779. if (multimesh->aabb_dirty) {
  2780. const_cast<RasterizerStorageRD *>(this)->_update_dirty_multimeshes();
  2781. }
  2782. return multimesh->aabb;
  2783. }
  2784. void RasterizerStorageRD::_update_dirty_multimeshes() {
  2785. while (multimesh_dirty_list) {
  2786. MultiMesh *multimesh = multimesh_dirty_list;
  2787. if (multimesh->data_cache.size()) { //may have been cleared, so only process if it exists
  2788. const float *data = multimesh->data_cache.ptr();
  2789. uint32_t visible_instances = multimesh->visible_instances >= 0 ? multimesh->visible_instances : multimesh->instances;
  2790. if (multimesh->data_cache_used_dirty_regions) {
  2791. uint32_t data_cache_dirty_region_count = (multimesh->instances - 1) / MULTIMESH_DIRTY_REGION_SIZE + 1;
  2792. uint32_t visible_region_count = (visible_instances - 1) / MULTIMESH_DIRTY_REGION_SIZE + 1;
  2793. uint32_t region_size = multimesh->stride_cache * MULTIMESH_DIRTY_REGION_SIZE * sizeof(float);
  2794. if (multimesh->data_cache_used_dirty_regions > 32 || multimesh->data_cache_used_dirty_regions > visible_region_count / 2) {
  2795. //if there too many dirty regions, or represent the majority of regions, just copy all, else transfer cost piles up too much
  2796. RD::get_singleton()->buffer_update(multimesh->buffer, 0, MIN(visible_region_count * region_size, multimesh->instances * multimesh->stride_cache * sizeof(float)), data, false);
  2797. } else {
  2798. //not that many regions? update them all
  2799. for (uint32_t i = 0; i < visible_region_count; i++) {
  2800. if (multimesh->data_cache_dirty_regions[i]) {
  2801. uint64_t offset = i * region_size;
  2802. uint64_t size = multimesh->stride_cache * multimesh->instances * sizeof(float);
  2803. RD::get_singleton()->buffer_update(multimesh->buffer, offset, MIN(region_size, size - offset), &data[i * region_size], false);
  2804. }
  2805. }
  2806. }
  2807. for (uint32_t i = 0; i < data_cache_dirty_region_count; i++) {
  2808. multimesh->data_cache_dirty_regions[i] = false;
  2809. }
  2810. multimesh->data_cache_used_dirty_regions = 0;
  2811. }
  2812. if (multimesh->aabb_dirty) {
  2813. //aabb is dirty..
  2814. _multimesh_re_create_aabb(multimesh, data, visible_instances);
  2815. multimesh->aabb_dirty = false;
  2816. multimesh->instance_dependency.instance_notify_changed(true, false);
  2817. }
  2818. }
  2819. multimesh_dirty_list = multimesh->dirty_list;
  2820. multimesh->dirty_list = nullptr;
  2821. multimesh->dirty = false;
  2822. }
  2823. multimesh_dirty_list = nullptr;
  2824. }
  2825. /* PARTICLES */
  2826. RID RasterizerStorageRD::particles_create() {
  2827. return particles_owner.make_rid(Particles());
  2828. }
  2829. void RasterizerStorageRD::particles_set_emitting(RID p_particles, bool p_emitting) {
  2830. Particles *particles = particles_owner.getornull(p_particles);
  2831. ERR_FAIL_COND(!particles);
  2832. particles->emitting = p_emitting;
  2833. }
  2834. bool RasterizerStorageRD::particles_get_emitting(RID p_particles) {
  2835. Particles *particles = particles_owner.getornull(p_particles);
  2836. ERR_FAIL_COND_V(!particles, false);
  2837. return particles->emitting;
  2838. }
  2839. void RasterizerStorageRD::_particles_free_data(Particles *particles) {
  2840. if (!particles->particle_buffer.is_valid()) {
  2841. return;
  2842. }
  2843. RD::get_singleton()->free(particles->particle_buffer);
  2844. RD::get_singleton()->free(particles->frame_params_buffer);
  2845. RD::get_singleton()->free(particles->particle_instance_buffer);
  2846. particles->particles_transforms_buffer_uniform_set = RID();
  2847. particles->particle_buffer = RID();
  2848. if (RD::get_singleton()->uniform_set_is_valid(particles->collision_textures_uniform_set)) {
  2849. RD::get_singleton()->free(particles->collision_textures_uniform_set);
  2850. }
  2851. if (particles->particles_sort_buffer.is_valid()) {
  2852. RD::get_singleton()->free(particles->particles_sort_buffer);
  2853. particles->particles_sort_buffer = RID();
  2854. }
  2855. if (particles->emission_buffer != nullptr) {
  2856. particles->emission_buffer = nullptr;
  2857. particles->emission_buffer_data.clear();
  2858. RD::get_singleton()->free(particles->emission_storage_buffer);
  2859. particles->emission_storage_buffer = RID();
  2860. }
  2861. }
  2862. void RasterizerStorageRD::particles_set_amount(RID p_particles, int p_amount) {
  2863. Particles *particles = particles_owner.getornull(p_particles);
  2864. ERR_FAIL_COND(!particles);
  2865. if (particles->amount == p_amount) {
  2866. return;
  2867. }
  2868. _particles_free_data(particles);
  2869. particles->amount = p_amount;
  2870. if (particles->amount > 0) {
  2871. particles->particle_buffer = RD::get_singleton()->storage_buffer_create(sizeof(ParticleData) * p_amount);
  2872. particles->frame_params_buffer = RD::get_singleton()->storage_buffer_create(sizeof(ParticlesFrameParams) * 1);
  2873. particles->particle_instance_buffer = RD::get_singleton()->storage_buffer_create(sizeof(float) * 4 * (3 + 1 + 1) * p_amount);
  2874. //needs to clear it
  2875. {
  2876. Vector<RD::Uniform> uniforms;
  2877. {
  2878. RD::Uniform u;
  2879. u.type = RD::UNIFORM_TYPE_STORAGE_BUFFER;
  2880. u.binding = 1;
  2881. u.ids.push_back(particles->particle_buffer);
  2882. uniforms.push_back(u);
  2883. }
  2884. {
  2885. RD::Uniform u;
  2886. u.type = RD::UNIFORM_TYPE_STORAGE_BUFFER;
  2887. u.binding = 2;
  2888. u.ids.push_back(particles->particle_instance_buffer);
  2889. uniforms.push_back(u);
  2890. }
  2891. particles->particles_copy_uniform_set = RD::get_singleton()->uniform_set_create(uniforms, particles_shader.copy_shader.version_get_shader(particles_shader.copy_shader_version, 0), 0);
  2892. }
  2893. }
  2894. particles->prev_ticks = 0;
  2895. particles->phase = 0;
  2896. particles->prev_phase = 0;
  2897. particles->clear = true;
  2898. }
  2899. void RasterizerStorageRD::particles_set_lifetime(RID p_particles, float p_lifetime) {
  2900. Particles *particles = particles_owner.getornull(p_particles);
  2901. ERR_FAIL_COND(!particles);
  2902. particles->lifetime = p_lifetime;
  2903. }
  2904. void RasterizerStorageRD::particles_set_one_shot(RID p_particles, bool p_one_shot) {
  2905. Particles *particles = particles_owner.getornull(p_particles);
  2906. ERR_FAIL_COND(!particles);
  2907. particles->one_shot = p_one_shot;
  2908. }
  2909. void RasterizerStorageRD::particles_set_pre_process_time(RID p_particles, float p_time) {
  2910. Particles *particles = particles_owner.getornull(p_particles);
  2911. ERR_FAIL_COND(!particles);
  2912. particles->pre_process_time = p_time;
  2913. }
  2914. void RasterizerStorageRD::particles_set_explosiveness_ratio(RID p_particles, float p_ratio) {
  2915. Particles *particles = particles_owner.getornull(p_particles);
  2916. ERR_FAIL_COND(!particles);
  2917. particles->explosiveness = p_ratio;
  2918. }
  2919. void RasterizerStorageRD::particles_set_randomness_ratio(RID p_particles, float p_ratio) {
  2920. Particles *particles = particles_owner.getornull(p_particles);
  2921. ERR_FAIL_COND(!particles);
  2922. particles->randomness = p_ratio;
  2923. }
  2924. void RasterizerStorageRD::particles_set_custom_aabb(RID p_particles, const AABB &p_aabb) {
  2925. Particles *particles = particles_owner.getornull(p_particles);
  2926. ERR_FAIL_COND(!particles);
  2927. particles->custom_aabb = p_aabb;
  2928. particles->instance_dependency.instance_notify_changed(true, false);
  2929. }
  2930. void RasterizerStorageRD::particles_set_speed_scale(RID p_particles, float p_scale) {
  2931. Particles *particles = particles_owner.getornull(p_particles);
  2932. ERR_FAIL_COND(!particles);
  2933. particles->speed_scale = p_scale;
  2934. }
  2935. void RasterizerStorageRD::particles_set_use_local_coordinates(RID p_particles, bool p_enable) {
  2936. Particles *particles = particles_owner.getornull(p_particles);
  2937. ERR_FAIL_COND(!particles);
  2938. particles->use_local_coords = p_enable;
  2939. }
  2940. void RasterizerStorageRD::particles_set_fixed_fps(RID p_particles, int p_fps) {
  2941. Particles *particles = particles_owner.getornull(p_particles);
  2942. ERR_FAIL_COND(!particles);
  2943. particles->fixed_fps = p_fps;
  2944. }
  2945. void RasterizerStorageRD::particles_set_fractional_delta(RID p_particles, bool p_enable) {
  2946. Particles *particles = particles_owner.getornull(p_particles);
  2947. ERR_FAIL_COND(!particles);
  2948. particles->fractional_delta = p_enable;
  2949. }
  2950. void RasterizerStorageRD::particles_set_collision_base_size(RID p_particles, float p_size) {
  2951. Particles *particles = particles_owner.getornull(p_particles);
  2952. ERR_FAIL_COND(!particles);
  2953. particles->collision_base_size = p_size;
  2954. }
  2955. void RasterizerStorageRD::particles_set_process_material(RID p_particles, RID p_material) {
  2956. Particles *particles = particles_owner.getornull(p_particles);
  2957. ERR_FAIL_COND(!particles);
  2958. particles->process_material = p_material;
  2959. }
  2960. void RasterizerStorageRD::particles_set_draw_order(RID p_particles, RS::ParticlesDrawOrder p_order) {
  2961. Particles *particles = particles_owner.getornull(p_particles);
  2962. ERR_FAIL_COND(!particles);
  2963. particles->draw_order = p_order;
  2964. }
  2965. void RasterizerStorageRD::particles_set_draw_passes(RID p_particles, int p_passes) {
  2966. Particles *particles = particles_owner.getornull(p_particles);
  2967. ERR_FAIL_COND(!particles);
  2968. particles->draw_passes.resize(p_passes);
  2969. }
  2970. void RasterizerStorageRD::particles_set_draw_pass_mesh(RID p_particles, int p_pass, RID p_mesh) {
  2971. Particles *particles = particles_owner.getornull(p_particles);
  2972. ERR_FAIL_COND(!particles);
  2973. ERR_FAIL_INDEX(p_pass, particles->draw_passes.size());
  2974. particles->draw_passes.write[p_pass] = p_mesh;
  2975. }
  2976. void RasterizerStorageRD::particles_restart(RID p_particles) {
  2977. Particles *particles = particles_owner.getornull(p_particles);
  2978. ERR_FAIL_COND(!particles);
  2979. particles->restart_request = true;
  2980. }
  2981. void RasterizerStorageRD::_particles_allocate_emission_buffer(Particles *particles) {
  2982. ERR_FAIL_COND(particles->emission_buffer != nullptr);
  2983. particles->emission_buffer_data.resize(sizeof(ParticleEmissionBuffer::Data) * particles->amount + sizeof(uint32_t) * 4);
  2984. zeromem(particles->emission_buffer_data.ptrw(), particles->emission_buffer_data.size());
  2985. particles->emission_buffer = (ParticleEmissionBuffer *)particles->emission_buffer_data.ptrw();
  2986. particles->emission_buffer->particle_max = particles->amount;
  2987. particles->emission_storage_buffer = RD::get_singleton()->storage_buffer_create(particles->emission_buffer_data.size(), particles->emission_buffer_data);
  2988. if (RD::get_singleton()->uniform_set_is_valid(particles->particles_material_uniform_set)) {
  2989. //will need to be re-created
  2990. RD::get_singleton()->free(particles->particles_material_uniform_set);
  2991. particles->particles_material_uniform_set = RID();
  2992. }
  2993. }
  2994. void RasterizerStorageRD::particles_set_subemitter(RID p_particles, RID p_subemitter_particles) {
  2995. Particles *particles = particles_owner.getornull(p_particles);
  2996. ERR_FAIL_COND(!particles);
  2997. ERR_FAIL_COND(p_particles == p_subemitter_particles);
  2998. particles->sub_emitter = p_subemitter_particles;
  2999. if (RD::get_singleton()->uniform_set_is_valid(particles->particles_material_uniform_set)) {
  3000. RD::get_singleton()->free(particles->particles_material_uniform_set);
  3001. particles->particles_material_uniform_set = RID(); //clear and force to re create sub emitting
  3002. }
  3003. }
  3004. void RasterizerStorageRD::particles_emit(RID p_particles, const Transform &p_transform, const Vector3 &p_velocity, const Color &p_color, const Color &p_custom, uint32_t p_emit_flags) {
  3005. Particles *particles = particles_owner.getornull(p_particles);
  3006. ERR_FAIL_COND(!particles);
  3007. ERR_FAIL_COND(particles->amount == 0);
  3008. if (particles->emitting) {
  3009. particles->clear = true;
  3010. particles->emitting = false;
  3011. }
  3012. if (particles->emission_buffer == nullptr) {
  3013. _particles_allocate_emission_buffer(particles);
  3014. }
  3015. if (particles->inactive) {
  3016. //in case it was inactive, make active again
  3017. particles->inactive = false;
  3018. particles->inactive_time = 0;
  3019. }
  3020. int32_t idx = particles->emission_buffer->particle_count;
  3021. if (idx < particles->emission_buffer->particle_max) {
  3022. store_transform(p_transform, particles->emission_buffer->data[idx].xform);
  3023. particles->emission_buffer->data[idx].velocity[0] = p_velocity.x;
  3024. particles->emission_buffer->data[idx].velocity[1] = p_velocity.y;
  3025. particles->emission_buffer->data[idx].velocity[2] = p_velocity.z;
  3026. particles->emission_buffer->data[idx].custom[0] = p_custom.r;
  3027. particles->emission_buffer->data[idx].custom[1] = p_custom.g;
  3028. particles->emission_buffer->data[idx].custom[2] = p_custom.b;
  3029. particles->emission_buffer->data[idx].custom[3] = p_custom.a;
  3030. particles->emission_buffer->data[idx].color[0] = p_color.r;
  3031. particles->emission_buffer->data[idx].color[1] = p_color.g;
  3032. particles->emission_buffer->data[idx].color[2] = p_color.b;
  3033. particles->emission_buffer->data[idx].color[3] = p_color.a;
  3034. particles->emission_buffer->data[idx].flags = p_emit_flags;
  3035. particles->emission_buffer->particle_count++;
  3036. }
  3037. }
  3038. void RasterizerStorageRD::particles_request_process(RID p_particles) {
  3039. Particles *particles = particles_owner.getornull(p_particles);
  3040. ERR_FAIL_COND(!particles);
  3041. if (!particles->dirty) {
  3042. particles->dirty = true;
  3043. particles->update_list = particle_update_list;
  3044. particle_update_list = particles;
  3045. }
  3046. }
  3047. AABB RasterizerStorageRD::particles_get_current_aabb(RID p_particles) {
  3048. const Particles *particles = particles_owner.getornull(p_particles);
  3049. ERR_FAIL_COND_V(!particles, AABB());
  3050. Vector<ParticleData> data;
  3051. data.resize(particles->amount);
  3052. Vector<uint8_t> buffer = RD::get_singleton()->buffer_get_data(particles->particle_buffer);
  3053. Transform inv = particles->emission_transform.affine_inverse();
  3054. AABB aabb;
  3055. if (buffer.size()) {
  3056. bool first = true;
  3057. const ParticleData *particle_data = (const ParticleData *)data.ptr();
  3058. for (int i = 0; i < particles->amount; i++) {
  3059. if (particle_data[i].active) {
  3060. Vector3 pos = Vector3(particle_data[i].xform[12], particle_data[i].xform[13], particle_data[i].xform[14]);
  3061. if (!particles->use_local_coords) {
  3062. pos = inv.xform(pos);
  3063. }
  3064. if (first) {
  3065. aabb.position = pos;
  3066. first = false;
  3067. } else {
  3068. aabb.expand_to(pos);
  3069. }
  3070. }
  3071. }
  3072. }
  3073. float longest_axis_size = 0;
  3074. for (int i = 0; i < particles->draw_passes.size(); i++) {
  3075. if (particles->draw_passes[i].is_valid()) {
  3076. AABB maabb = mesh_get_aabb(particles->draw_passes[i], RID());
  3077. longest_axis_size = MAX(maabb.get_longest_axis_size(), longest_axis_size);
  3078. }
  3079. }
  3080. aabb.grow_by(longest_axis_size);
  3081. return aabb;
  3082. }
  3083. AABB RasterizerStorageRD::particles_get_aabb(RID p_particles) const {
  3084. const Particles *particles = particles_owner.getornull(p_particles);
  3085. ERR_FAIL_COND_V(!particles, AABB());
  3086. return particles->custom_aabb;
  3087. }
  3088. void RasterizerStorageRD::particles_set_emission_transform(RID p_particles, const Transform &p_transform) {
  3089. Particles *particles = particles_owner.getornull(p_particles);
  3090. ERR_FAIL_COND(!particles);
  3091. particles->emission_transform = p_transform;
  3092. }
  3093. int RasterizerStorageRD::particles_get_draw_passes(RID p_particles) const {
  3094. const Particles *particles = particles_owner.getornull(p_particles);
  3095. ERR_FAIL_COND_V(!particles, 0);
  3096. return particles->draw_passes.size();
  3097. }
  3098. RID RasterizerStorageRD::particles_get_draw_pass_mesh(RID p_particles, int p_pass) const {
  3099. const Particles *particles = particles_owner.getornull(p_particles);
  3100. ERR_FAIL_COND_V(!particles, RID());
  3101. ERR_FAIL_INDEX_V(p_pass, particles->draw_passes.size(), RID());
  3102. return particles->draw_passes[p_pass];
  3103. }
  3104. void RasterizerStorageRD::particles_add_collision(RID p_particles, RasterizerScene::InstanceBase *p_instance) {
  3105. Particles *particles = particles_owner.getornull(p_particles);
  3106. ERR_FAIL_COND(!particles);
  3107. ERR_FAIL_COND(p_instance->base_type != RS::INSTANCE_PARTICLES_COLLISION);
  3108. particles->collisions.insert(p_instance);
  3109. }
  3110. void RasterizerStorageRD::particles_remove_collision(RID p_particles, RasterizerScene::InstanceBase *p_instance) {
  3111. Particles *particles = particles_owner.getornull(p_particles);
  3112. ERR_FAIL_COND(!particles);
  3113. particles->collisions.erase(p_instance);
  3114. }
  3115. void RasterizerStorageRD::_particles_process(Particles *p_particles, float p_delta) {
  3116. if (p_particles->particles_material_uniform_set.is_null() || !RD::get_singleton()->uniform_set_is_valid(p_particles->particles_material_uniform_set)) {
  3117. Vector<RD::Uniform> uniforms;
  3118. {
  3119. RD::Uniform u;
  3120. u.type = RD::UNIFORM_TYPE_STORAGE_BUFFER;
  3121. u.binding = 0;
  3122. u.ids.push_back(p_particles->frame_params_buffer);
  3123. uniforms.push_back(u);
  3124. }
  3125. {
  3126. RD::Uniform u;
  3127. u.type = RD::UNIFORM_TYPE_STORAGE_BUFFER;
  3128. u.binding = 1;
  3129. u.ids.push_back(p_particles->particle_buffer);
  3130. uniforms.push_back(u);
  3131. }
  3132. {
  3133. RD::Uniform u;
  3134. u.type = RD::UNIFORM_TYPE_STORAGE_BUFFER;
  3135. u.binding = 2;
  3136. if (p_particles->emission_storage_buffer.is_valid()) {
  3137. u.ids.push_back(p_particles->emission_storage_buffer);
  3138. } else {
  3139. u.ids.push_back(default_rd_storage_buffer);
  3140. }
  3141. uniforms.push_back(u);
  3142. }
  3143. {
  3144. RD::Uniform u;
  3145. u.type = RD::UNIFORM_TYPE_STORAGE_BUFFER;
  3146. u.binding = 3;
  3147. Particles *sub_emitter = particles_owner.getornull(p_particles->sub_emitter);
  3148. if (sub_emitter) {
  3149. if (sub_emitter->emission_buffer == nullptr) { //no emission buffer, allocate emission buffer
  3150. _particles_allocate_emission_buffer(sub_emitter);
  3151. }
  3152. u.ids.push_back(sub_emitter->emission_storage_buffer);
  3153. } else {
  3154. u.ids.push_back(default_rd_storage_buffer);
  3155. }
  3156. uniforms.push_back(u);
  3157. }
  3158. p_particles->particles_material_uniform_set = RD::get_singleton()->uniform_set_create(uniforms, particles_shader.default_shader_rd, 1);
  3159. }
  3160. float new_phase = Math::fmod((float)p_particles->phase + (p_delta / p_particles->lifetime) * p_particles->speed_scale, (float)1.0);
  3161. ParticlesFrameParams &frame_params = p_particles->frame_params;
  3162. if (p_particles->clear) {
  3163. p_particles->cycle_number = 0;
  3164. p_particles->random_seed = Math::rand();
  3165. } else if (new_phase < p_particles->phase) {
  3166. if (p_particles->one_shot) {
  3167. p_particles->emitting = false;
  3168. }
  3169. p_particles->cycle_number++;
  3170. }
  3171. frame_params.emitting = p_particles->emitting;
  3172. frame_params.system_phase = new_phase;
  3173. frame_params.prev_system_phase = p_particles->phase;
  3174. p_particles->phase = new_phase;
  3175. frame_params.time = RasterizerRD::singleton->get_total_time();
  3176. frame_params.delta = p_delta * p_particles->speed_scale;
  3177. frame_params.random_seed = p_particles->random_seed;
  3178. frame_params.explosiveness = p_particles->explosiveness;
  3179. frame_params.randomness = p_particles->randomness;
  3180. if (p_particles->use_local_coords) {
  3181. store_transform(Transform(), frame_params.emission_transform);
  3182. } else {
  3183. store_transform(p_particles->emission_transform, frame_params.emission_transform);
  3184. }
  3185. frame_params.cycle = p_particles->cycle_number;
  3186. { //collision and attractors
  3187. frame_params.collider_count = 0;
  3188. frame_params.attractor_count = 0;
  3189. frame_params.particle_size = p_particles->collision_base_size;
  3190. RID collision_3d_textures[ParticlesFrameParams::MAX_3D_TEXTURES];
  3191. RID collision_heightmap_texture;
  3192. Transform to_particles;
  3193. if (p_particles->use_local_coords) {
  3194. to_particles = p_particles->emission_transform.affine_inverse();
  3195. }
  3196. uint32_t collision_3d_textures_used = 0;
  3197. for (const Set<RasterizerScene::InstanceBase *>::Element *E = p_particles->collisions.front(); E; E = E->next()) {
  3198. ParticlesCollision *pc = particles_collision_owner.getornull(E->get()->base);
  3199. Transform to_collider = E->get()->transform;
  3200. if (p_particles->use_local_coords) {
  3201. to_collider = to_particles * to_collider;
  3202. }
  3203. Vector3 scale = to_collider.basis.get_scale();
  3204. to_collider.basis.orthonormalize();
  3205. if (pc->type <= RS::PARTICLES_COLLISION_TYPE_VECTOR_FIELD_ATTRACT) {
  3206. //attractor
  3207. if (frame_params.attractor_count >= ParticlesFrameParams::MAX_ATTRACTORS) {
  3208. continue;
  3209. }
  3210. ParticlesFrameParams::Attractor &attr = frame_params.attractors[frame_params.attractor_count];
  3211. store_transform(to_collider, attr.transform);
  3212. attr.strength = pc->attractor_strength;
  3213. attr.attenuation = pc->attractor_attenuation;
  3214. attr.directionality = pc->attractor_directionality;
  3215. switch (pc->type) {
  3216. case RS::PARTICLES_COLLISION_TYPE_SPHERE_ATTRACT: {
  3217. attr.type = ParticlesFrameParams::ATTRACTOR_TYPE_SPHERE;
  3218. float radius = pc->radius;
  3219. radius *= (scale.x + scale.y + scale.z) / 3.0;
  3220. attr.extents[0] = radius;
  3221. attr.extents[1] = radius;
  3222. attr.extents[2] = radius;
  3223. } break;
  3224. case RS::PARTICLES_COLLISION_TYPE_BOX_ATTRACT: {
  3225. attr.type = ParticlesFrameParams::ATTRACTOR_TYPE_BOX;
  3226. Vector3 extents = pc->extents * scale;
  3227. attr.extents[0] = extents.x;
  3228. attr.extents[1] = extents.y;
  3229. attr.extents[2] = extents.z;
  3230. } break;
  3231. case RS::PARTICLES_COLLISION_TYPE_VECTOR_FIELD_ATTRACT: {
  3232. if (collision_3d_textures_used >= ParticlesFrameParams::MAX_3D_TEXTURES) {
  3233. continue;
  3234. }
  3235. attr.type = ParticlesFrameParams::ATTRACTOR_TYPE_VECTOR_FIELD;
  3236. Vector3 extents = pc->extents * scale;
  3237. attr.extents[0] = extents.x;
  3238. attr.extents[1] = extents.y;
  3239. attr.extents[2] = extents.z;
  3240. attr.texture_index = collision_3d_textures_used;
  3241. collision_3d_textures[collision_3d_textures_used] = pc->field_texture;
  3242. collision_3d_textures_used++;
  3243. } break;
  3244. default: {
  3245. }
  3246. }
  3247. frame_params.attractor_count++;
  3248. } else {
  3249. //collider
  3250. if (frame_params.collider_count >= ParticlesFrameParams::MAX_COLLIDERS) {
  3251. continue;
  3252. }
  3253. ParticlesFrameParams::Collider &col = frame_params.colliders[frame_params.collider_count];
  3254. store_transform(to_collider, col.transform);
  3255. switch (pc->type) {
  3256. case RS::PARTICLES_COLLISION_TYPE_SPHERE_COLLIDE: {
  3257. col.type = ParticlesFrameParams::COLLISION_TYPE_SPHERE;
  3258. float radius = pc->radius;
  3259. radius *= (scale.x + scale.y + scale.z) / 3.0;
  3260. col.extents[0] = radius;
  3261. col.extents[1] = radius;
  3262. col.extents[2] = radius;
  3263. } break;
  3264. case RS::PARTICLES_COLLISION_TYPE_BOX_COLLIDE: {
  3265. col.type = ParticlesFrameParams::COLLISION_TYPE_BOX;
  3266. Vector3 extents = pc->extents * scale;
  3267. col.extents[0] = extents.x;
  3268. col.extents[1] = extents.y;
  3269. col.extents[2] = extents.z;
  3270. } break;
  3271. case RS::PARTICLES_COLLISION_TYPE_SDF_COLLIDE: {
  3272. if (collision_3d_textures_used >= ParticlesFrameParams::MAX_3D_TEXTURES) {
  3273. continue;
  3274. }
  3275. col.type = ParticlesFrameParams::COLLISION_TYPE_SDF;
  3276. Vector3 extents = pc->extents * scale;
  3277. col.extents[0] = extents.x;
  3278. col.extents[1] = extents.y;
  3279. col.extents[2] = extents.z;
  3280. col.texture_index = collision_3d_textures_used;
  3281. col.scale = (scale.x + scale.y + scale.z) * 0.333333333333; //non uniform scale non supported
  3282. collision_3d_textures[collision_3d_textures_used] = pc->field_texture;
  3283. collision_3d_textures_used++;
  3284. } break;
  3285. case RS::PARTICLES_COLLISION_TYPE_HEIGHTFIELD_COLLIDE: {
  3286. if (collision_heightmap_texture != RID()) { //already taken
  3287. continue;
  3288. }
  3289. col.type = ParticlesFrameParams::COLLISION_TYPE_HEIGHT_FIELD;
  3290. Vector3 extents = pc->extents * scale;
  3291. col.extents[0] = extents.x;
  3292. col.extents[1] = extents.y;
  3293. col.extents[2] = extents.z;
  3294. collision_heightmap_texture = pc->heightfield_texture;
  3295. } break;
  3296. default: {
  3297. }
  3298. }
  3299. frame_params.collider_count++;
  3300. }
  3301. }
  3302. bool different = false;
  3303. if (collision_3d_textures_used == p_particles->collision_3d_textures_used) {
  3304. for (int i = 0; i < ParticlesFrameParams::MAX_3D_TEXTURES; i++) {
  3305. if (p_particles->collision_3d_textures[i] != collision_3d_textures[i]) {
  3306. different = true;
  3307. break;
  3308. }
  3309. }
  3310. }
  3311. if (collision_heightmap_texture != p_particles->collision_heightmap_texture) {
  3312. different = true;
  3313. }
  3314. bool uniform_set_valid = RD::get_singleton()->uniform_set_is_valid(p_particles->collision_textures_uniform_set);
  3315. if (different || !uniform_set_valid) {
  3316. if (uniform_set_valid) {
  3317. RD::get_singleton()->free(p_particles->collision_textures_uniform_set);
  3318. }
  3319. Vector<RD::Uniform> uniforms;
  3320. {
  3321. RD::Uniform u;
  3322. u.type = RD::UNIFORM_TYPE_TEXTURE;
  3323. u.binding = 0;
  3324. for (uint32_t i = 0; i < ParticlesFrameParams::MAX_3D_TEXTURES; i++) {
  3325. RID rd_tex;
  3326. if (i < collision_3d_textures_used) {
  3327. Texture *t = texture_owner.getornull(collision_3d_textures[i]);
  3328. if (t && t->type == Texture::TYPE_3D) {
  3329. rd_tex = t->rd_texture;
  3330. }
  3331. }
  3332. if (rd_tex == RID()) {
  3333. rd_tex = default_rd_textures[DEFAULT_RD_TEXTURE_3D_WHITE];
  3334. }
  3335. u.ids.push_back(rd_tex);
  3336. }
  3337. uniforms.push_back(u);
  3338. }
  3339. {
  3340. RD::Uniform u;
  3341. u.type = RD::UNIFORM_TYPE_TEXTURE;
  3342. u.binding = 1;
  3343. if (collision_heightmap_texture.is_valid()) {
  3344. u.ids.push_back(collision_heightmap_texture);
  3345. } else {
  3346. u.ids.push_back(default_rd_textures[DEFAULT_RD_TEXTURE_BLACK]);
  3347. }
  3348. uniforms.push_back(u);
  3349. }
  3350. p_particles->collision_textures_uniform_set = RD::get_singleton()->uniform_set_create(uniforms, particles_shader.default_shader_rd, 2);
  3351. }
  3352. }
  3353. ParticlesShader::PushConstant push_constant;
  3354. push_constant.clear = p_particles->clear;
  3355. push_constant.total_particles = p_particles->amount;
  3356. push_constant.lifetime = p_particles->lifetime;
  3357. push_constant.trail_size = 1;
  3358. push_constant.use_fractional_delta = p_particles->fractional_delta;
  3359. push_constant.sub_emitter_mode = !p_particles->emitting && p_particles->emission_buffer && (p_particles->emission_buffer->particle_count > 0 || p_particles->force_sub_emit);
  3360. p_particles->force_sub_emit = false; //reset
  3361. Particles *sub_emitter = particles_owner.getornull(p_particles->sub_emitter);
  3362. if (sub_emitter && sub_emitter->emission_storage_buffer.is_valid()) {
  3363. // print_line("updating subemitter buffer");
  3364. int32_t zero[4] = { 0, sub_emitter->amount, 0, 0 };
  3365. RD::get_singleton()->buffer_update(sub_emitter->emission_storage_buffer, 0, sizeof(uint32_t) * 4, zero, true);
  3366. push_constant.can_emit = true;
  3367. if (sub_emitter->emitting) {
  3368. sub_emitter->emitting = false;
  3369. sub_emitter->clear = true; //will need to clear if it was emitting, sorry
  3370. }
  3371. //make sure the sub emitter processes particles too
  3372. sub_emitter->inactive = false;
  3373. sub_emitter->inactive_time = 0;
  3374. sub_emitter->force_sub_emit = true;
  3375. } else {
  3376. push_constant.can_emit = false;
  3377. }
  3378. if (p_particles->emission_buffer && p_particles->emission_buffer->particle_count) {
  3379. RD::get_singleton()->buffer_update(p_particles->emission_storage_buffer, 0, sizeof(uint32_t) * 4 + sizeof(ParticleEmissionBuffer::Data) * p_particles->emission_buffer->particle_count, p_particles->emission_buffer, true);
  3380. p_particles->emission_buffer->particle_count = 0;
  3381. }
  3382. p_particles->clear = false;
  3383. RD::get_singleton()->buffer_update(p_particles->frame_params_buffer, 0, sizeof(ParticlesFrameParams), &frame_params, true);
  3384. ParticlesMaterialData *m = (ParticlesMaterialData *)material_get_data(p_particles->process_material, SHADER_TYPE_PARTICLES);
  3385. if (!m) {
  3386. m = (ParticlesMaterialData *)material_get_data(particles_shader.default_material, SHADER_TYPE_PARTICLES);
  3387. }
  3388. ERR_FAIL_COND(!m);
  3389. //todo should maybe compute all particle systems together?
  3390. RD::ComputeListID compute_list = RD::get_singleton()->compute_list_begin();
  3391. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, m->shader_data->pipeline);
  3392. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, particles_shader.base_uniform_set, 0);
  3393. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, p_particles->particles_material_uniform_set, 1);
  3394. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, p_particles->collision_textures_uniform_set, 2);
  3395. if (m->uniform_set.is_valid()) {
  3396. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, m->uniform_set, 3);
  3397. }
  3398. RD::get_singleton()->compute_list_set_push_constant(compute_list, &push_constant, sizeof(ParticlesShader::PushConstant));
  3399. RD::get_singleton()->compute_list_dispatch_threads(compute_list, p_particles->amount, 1, 1, 64, 1, 1);
  3400. RD::get_singleton()->compute_list_end();
  3401. }
  3402. void RasterizerStorageRD::particles_set_view_axis(RID p_particles, const Vector3 &p_axis) {
  3403. Particles *particles = particles_owner.getornull(p_particles);
  3404. ERR_FAIL_COND(!particles);
  3405. if (particles->draw_order != RS::PARTICLES_DRAW_ORDER_VIEW_DEPTH) {
  3406. return; //uninteresting for other modes
  3407. }
  3408. //copy to sort buffer
  3409. if (particles->particles_sort_buffer == RID()) {
  3410. uint32_t size = particles->amount;
  3411. if (size & 1) {
  3412. size++; //make multiple of 16
  3413. }
  3414. size *= sizeof(float) * 2;
  3415. particles->particles_sort_buffer = RD::get_singleton()->storage_buffer_create(size);
  3416. {
  3417. Vector<RD::Uniform> uniforms;
  3418. {
  3419. RD::Uniform u;
  3420. u.type = RD::UNIFORM_TYPE_STORAGE_BUFFER;
  3421. u.binding = 0;
  3422. u.ids.push_back(particles->particles_sort_buffer);
  3423. uniforms.push_back(u);
  3424. }
  3425. particles->particles_sort_uniform_set = RD::get_singleton()->uniform_set_create(uniforms, particles_shader.copy_shader.version_get_shader(particles_shader.copy_shader_version, ParticlesShader::COPY_MODE_FILL_SORT_BUFFER), 1);
  3426. }
  3427. }
  3428. Vector3 axis = -p_axis; // cameras look to z negative
  3429. if (particles->use_local_coords) {
  3430. axis = particles->emission_transform.basis.xform_inv(axis).normalized();
  3431. }
  3432. ParticlesShader::CopyPushConstant copy_push_constant;
  3433. copy_push_constant.total_particles = particles->amount;
  3434. copy_push_constant.sort_direction[0] = axis.x;
  3435. copy_push_constant.sort_direction[1] = axis.y;
  3436. copy_push_constant.sort_direction[2] = axis.z;
  3437. RD::ComputeListID compute_list = RD::get_singleton()->compute_list_begin();
  3438. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, particles_shader.copy_pipelines[ParticlesShader::COPY_MODE_FILL_SORT_BUFFER]);
  3439. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, particles->particles_copy_uniform_set, 0);
  3440. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, particles->particles_sort_uniform_set, 1);
  3441. RD::get_singleton()->compute_list_set_push_constant(compute_list, &copy_push_constant, sizeof(ParticlesShader::CopyPushConstant));
  3442. RD::get_singleton()->compute_list_dispatch_threads(compute_list, particles->amount, 1, 1, 64, 1, 1);
  3443. RD::get_singleton()->compute_list_end();
  3444. effects.sort_buffer(particles->particles_sort_uniform_set, particles->amount);
  3445. compute_list = RD::get_singleton()->compute_list_begin();
  3446. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, particles_shader.copy_pipelines[ParticlesShader::COPY_MODE_FILL_INSTANCES_WITH_SORT_BUFFER]);
  3447. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, particles->particles_copy_uniform_set, 0);
  3448. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, particles->particles_sort_uniform_set, 1);
  3449. RD::get_singleton()->compute_list_set_push_constant(compute_list, &copy_push_constant, sizeof(ParticlesShader::CopyPushConstant));
  3450. RD::get_singleton()->compute_list_dispatch_threads(compute_list, particles->amount, 1, 1, 64, 1, 1);
  3451. RD::get_singleton()->compute_list_end();
  3452. }
  3453. void RasterizerStorageRD::update_particles() {
  3454. while (particle_update_list) {
  3455. //use transform feedback to process particles
  3456. Particles *particles = particle_update_list;
  3457. //take and remove
  3458. particle_update_list = particles->update_list;
  3459. particles->update_list = nullptr;
  3460. particles->dirty = false;
  3461. if (particles->restart_request) {
  3462. particles->prev_ticks = 0;
  3463. particles->phase = 0;
  3464. particles->prev_phase = 0;
  3465. particles->clear = true;
  3466. particles->restart_request = false;
  3467. }
  3468. if (particles->inactive && !particles->emitting) {
  3469. //go next
  3470. continue;
  3471. }
  3472. if (particles->emitting) {
  3473. if (particles->inactive) {
  3474. //restart system from scratch
  3475. particles->prev_ticks = 0;
  3476. particles->phase = 0;
  3477. particles->prev_phase = 0;
  3478. particles->clear = true;
  3479. }
  3480. particles->inactive = false;
  3481. particles->inactive_time = 0;
  3482. } else {
  3483. particles->inactive_time += particles->speed_scale * RasterizerRD::singleton->get_frame_delta_time();
  3484. if (particles->inactive_time > particles->lifetime * 1.2) {
  3485. particles->inactive = true;
  3486. continue;
  3487. }
  3488. }
  3489. bool zero_time_scale = Engine::get_singleton()->get_time_scale() <= 0.0;
  3490. if (particles->clear && particles->pre_process_time > 0.0) {
  3491. float frame_time;
  3492. if (particles->fixed_fps > 0)
  3493. frame_time = 1.0 / particles->fixed_fps;
  3494. else
  3495. frame_time = 1.0 / 30.0;
  3496. float todo = particles->pre_process_time;
  3497. while (todo >= 0) {
  3498. _particles_process(particles, frame_time);
  3499. todo -= frame_time;
  3500. }
  3501. }
  3502. if (particles->fixed_fps > 0) {
  3503. float frame_time;
  3504. float decr;
  3505. if (zero_time_scale) {
  3506. frame_time = 0.0;
  3507. decr = 1.0 / particles->fixed_fps;
  3508. } else {
  3509. frame_time = 1.0 / particles->fixed_fps;
  3510. decr = frame_time;
  3511. }
  3512. float delta = RasterizerRD::singleton->get_frame_delta_time();
  3513. if (delta > 0.1) { //avoid recursive stalls if fps goes below 10
  3514. delta = 0.1;
  3515. } else if (delta <= 0.0) { //unlikely but..
  3516. delta = 0.001;
  3517. }
  3518. float todo = particles->frame_remainder + delta;
  3519. while (todo >= frame_time) {
  3520. _particles_process(particles, frame_time);
  3521. todo -= decr;
  3522. }
  3523. particles->frame_remainder = todo;
  3524. } else {
  3525. if (zero_time_scale)
  3526. _particles_process(particles, 0.0);
  3527. else
  3528. _particles_process(particles, RasterizerRD::singleton->get_frame_delta_time());
  3529. }
  3530. //copy particles to instance buffer
  3531. if (particles->draw_order != RS::PARTICLES_DRAW_ORDER_VIEW_DEPTH) {
  3532. ParticlesShader::CopyPushConstant copy_push_constant;
  3533. copy_push_constant.total_particles = particles->amount;
  3534. RD::ComputeListID compute_list = RD::get_singleton()->compute_list_begin();
  3535. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, particles_shader.copy_pipelines[ParticlesShader::COPY_MODE_FILL_INSTANCES]);
  3536. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, particles->particles_copy_uniform_set, 0);
  3537. RD::get_singleton()->compute_list_set_push_constant(compute_list, &copy_push_constant, sizeof(ParticlesShader::CopyPushConstant));
  3538. RD::get_singleton()->compute_list_dispatch_threads(compute_list, particles->amount, 1, 1, 64, 1, 1);
  3539. RD::get_singleton()->compute_list_end();
  3540. }
  3541. particles->instance_dependency.instance_notify_changed(true, false); //make sure shadows are updated
  3542. }
  3543. }
  3544. bool RasterizerStorageRD::particles_is_inactive(RID p_particles) const {
  3545. const Particles *particles = particles_owner.getornull(p_particles);
  3546. ERR_FAIL_COND_V(!particles, false);
  3547. return !particles->emitting && particles->inactive;
  3548. }
  3549. /* SKY SHADER */
  3550. void RasterizerStorageRD::ParticlesShaderData::set_code(const String &p_code) {
  3551. //compile
  3552. code = p_code;
  3553. valid = false;
  3554. ubo_size = 0;
  3555. uniforms.clear();
  3556. if (code == String()) {
  3557. return; //just invalid, but no error
  3558. }
  3559. ShaderCompilerRD::GeneratedCode gen_code;
  3560. ShaderCompilerRD::IdentifierActions actions;
  3561. /*
  3562. uses_time = false;
  3563. actions.render_mode_flags["use_half_res_pass"] = &uses_half_res;
  3564. actions.render_mode_flags["use_quarter_res_pass"] = &uses_quarter_res;
  3565. actions.usage_flag_pointers["TIME"] = &uses_time;
  3566. */
  3567. actions.uniforms = &uniforms;
  3568. Error err = base_singleton->particles_shader.compiler.compile(RS::SHADER_PARTICLES, code, &actions, path, gen_code);
  3569. ERR_FAIL_COND(err != OK);
  3570. if (version.is_null()) {
  3571. version = base_singleton->particles_shader.shader.version_create();
  3572. }
  3573. base_singleton->particles_shader.shader.version_set_compute_code(version, gen_code.uniforms, gen_code.compute_global, gen_code.compute, gen_code.defines);
  3574. ERR_FAIL_COND(!base_singleton->particles_shader.shader.version_is_valid(version));
  3575. ubo_size = gen_code.uniform_total_size;
  3576. ubo_offsets = gen_code.uniform_offsets;
  3577. texture_uniforms = gen_code.texture_uniforms;
  3578. //update pipelines
  3579. pipeline = RD::get_singleton()->compute_pipeline_create(base_singleton->particles_shader.shader.version_get_shader(version, 0));
  3580. valid = true;
  3581. }
  3582. void RasterizerStorageRD::ParticlesShaderData::set_default_texture_param(const StringName &p_name, RID p_texture) {
  3583. if (!p_texture.is_valid()) {
  3584. default_texture_params.erase(p_name);
  3585. } else {
  3586. default_texture_params[p_name] = p_texture;
  3587. }
  3588. }
  3589. void RasterizerStorageRD::ParticlesShaderData::get_param_list(List<PropertyInfo> *p_param_list) const {
  3590. Map<int, StringName> order;
  3591. for (Map<StringName, ShaderLanguage::ShaderNode::Uniform>::Element *E = uniforms.front(); E; E = E->next()) {
  3592. if (E->get().scope == ShaderLanguage::ShaderNode::Uniform::SCOPE_GLOBAL || E->get().scope == ShaderLanguage::ShaderNode::Uniform::SCOPE_INSTANCE) {
  3593. continue;
  3594. }
  3595. if (E->get().texture_order >= 0) {
  3596. order[E->get().texture_order + 100000] = E->key();
  3597. } else {
  3598. order[E->get().order] = E->key();
  3599. }
  3600. }
  3601. for (Map<int, StringName>::Element *E = order.front(); E; E = E->next()) {
  3602. PropertyInfo pi = ShaderLanguage::uniform_to_property_info(uniforms[E->get()]);
  3603. pi.name = E->get();
  3604. p_param_list->push_back(pi);
  3605. }
  3606. }
  3607. void RasterizerStorageRD::ParticlesShaderData::get_instance_param_list(List<RasterizerStorage::InstanceShaderParam> *p_param_list) const {
  3608. for (Map<StringName, ShaderLanguage::ShaderNode::Uniform>::Element *E = uniforms.front(); E; E = E->next()) {
  3609. if (E->get().scope != ShaderLanguage::ShaderNode::Uniform::SCOPE_INSTANCE) {
  3610. continue;
  3611. }
  3612. RasterizerStorage::InstanceShaderParam p;
  3613. p.info = ShaderLanguage::uniform_to_property_info(E->get());
  3614. p.info.name = E->key(); //supply name
  3615. p.index = E->get().instance_index;
  3616. p.default_value = ShaderLanguage::constant_value_to_variant(E->get().default_value, E->get().type, E->get().hint);
  3617. p_param_list->push_back(p);
  3618. }
  3619. }
  3620. bool RasterizerStorageRD::ParticlesShaderData::is_param_texture(const StringName &p_param) const {
  3621. if (!uniforms.has(p_param)) {
  3622. return false;
  3623. }
  3624. return uniforms[p_param].texture_order >= 0;
  3625. }
  3626. bool RasterizerStorageRD::ParticlesShaderData::is_animated() const {
  3627. return false;
  3628. }
  3629. bool RasterizerStorageRD::ParticlesShaderData::casts_shadows() const {
  3630. return false;
  3631. }
  3632. Variant RasterizerStorageRD::ParticlesShaderData::get_default_parameter(const StringName &p_parameter) const {
  3633. if (uniforms.has(p_parameter)) {
  3634. ShaderLanguage::ShaderNode::Uniform uniform = uniforms[p_parameter];
  3635. Vector<ShaderLanguage::ConstantNode::Value> default_value = uniform.default_value;
  3636. return ShaderLanguage::constant_value_to_variant(default_value, uniform.type, uniform.hint);
  3637. }
  3638. return Variant();
  3639. }
  3640. RasterizerStorageRD::ParticlesShaderData::ParticlesShaderData() {
  3641. valid = false;
  3642. }
  3643. RasterizerStorageRD::ParticlesShaderData::~ParticlesShaderData() {
  3644. //pipeline variants will clear themselves if shader is gone
  3645. if (version.is_valid()) {
  3646. base_singleton->particles_shader.shader.version_free(version);
  3647. }
  3648. }
  3649. RasterizerStorageRD::ShaderData *RasterizerStorageRD::_create_particles_shader_func() {
  3650. ParticlesShaderData *shader_data = memnew(ParticlesShaderData);
  3651. return shader_data;
  3652. }
  3653. void RasterizerStorageRD::ParticlesMaterialData::update_parameters(const Map<StringName, Variant> &p_parameters, bool p_uniform_dirty, bool p_textures_dirty) {
  3654. uniform_set_updated = true;
  3655. if ((uint32_t)ubo_data.size() != shader_data->ubo_size) {
  3656. p_uniform_dirty = true;
  3657. if (uniform_buffer.is_valid()) {
  3658. RD::get_singleton()->free(uniform_buffer);
  3659. uniform_buffer = RID();
  3660. }
  3661. ubo_data.resize(shader_data->ubo_size);
  3662. if (ubo_data.size()) {
  3663. uniform_buffer = RD::get_singleton()->uniform_buffer_create(ubo_data.size());
  3664. memset(ubo_data.ptrw(), 0, ubo_data.size()); //clear
  3665. }
  3666. //clear previous uniform set
  3667. if (uniform_set.is_valid() && RD::get_singleton()->uniform_set_is_valid(uniform_set)) {
  3668. RD::get_singleton()->free(uniform_set);
  3669. uniform_set = RID();
  3670. }
  3671. }
  3672. //check whether buffer changed
  3673. if (p_uniform_dirty && ubo_data.size()) {
  3674. update_uniform_buffer(shader_data->uniforms, shader_data->ubo_offsets.ptr(), p_parameters, ubo_data.ptrw(), ubo_data.size(), false);
  3675. RD::get_singleton()->buffer_update(uniform_buffer, 0, ubo_data.size(), ubo_data.ptrw());
  3676. }
  3677. uint32_t tex_uniform_count = shader_data->texture_uniforms.size();
  3678. if ((uint32_t)texture_cache.size() != tex_uniform_count) {
  3679. texture_cache.resize(tex_uniform_count);
  3680. p_textures_dirty = true;
  3681. //clear previous uniform set
  3682. if (uniform_set.is_valid() && RD::get_singleton()->uniform_set_is_valid(uniform_set)) {
  3683. RD::get_singleton()->free(uniform_set);
  3684. uniform_set = RID();
  3685. }
  3686. }
  3687. if (p_textures_dirty && tex_uniform_count) {
  3688. update_textures(p_parameters, shader_data->default_texture_params, shader_data->texture_uniforms, texture_cache.ptrw(), true);
  3689. }
  3690. if (shader_data->ubo_size == 0 && shader_data->texture_uniforms.size() == 0) {
  3691. // This material does not require an uniform set, so don't create it.
  3692. return;
  3693. }
  3694. if (!p_textures_dirty && uniform_set.is_valid() && RD::get_singleton()->uniform_set_is_valid(uniform_set)) {
  3695. //no reason to update uniform set, only UBO (or nothing) was needed to update
  3696. return;
  3697. }
  3698. Vector<RD::Uniform> uniforms;
  3699. {
  3700. if (shader_data->ubo_size) {
  3701. RD::Uniform u;
  3702. u.type = RD::UNIFORM_TYPE_UNIFORM_BUFFER;
  3703. u.binding = 0;
  3704. u.ids.push_back(uniform_buffer);
  3705. uniforms.push_back(u);
  3706. }
  3707. const RID *textures = texture_cache.ptrw();
  3708. for (uint32_t i = 0; i < tex_uniform_count; i++) {
  3709. RD::Uniform u;
  3710. u.type = RD::UNIFORM_TYPE_TEXTURE;
  3711. u.binding = 1 + i;
  3712. u.ids.push_back(textures[i]);
  3713. uniforms.push_back(u);
  3714. }
  3715. }
  3716. uniform_set = RD::get_singleton()->uniform_set_create(uniforms, base_singleton->particles_shader.shader.version_get_shader(shader_data->version, 0), 3);
  3717. }
  3718. RasterizerStorageRD::ParticlesMaterialData::~ParticlesMaterialData() {
  3719. if (uniform_set.is_valid() && RD::get_singleton()->uniform_set_is_valid(uniform_set)) {
  3720. RD::get_singleton()->free(uniform_set);
  3721. }
  3722. if (uniform_buffer.is_valid()) {
  3723. RD::get_singleton()->free(uniform_buffer);
  3724. }
  3725. }
  3726. RasterizerStorageRD::MaterialData *RasterizerStorageRD::_create_particles_material_func(ParticlesShaderData *p_shader) {
  3727. ParticlesMaterialData *material_data = memnew(ParticlesMaterialData);
  3728. material_data->shader_data = p_shader;
  3729. material_data->last_frame = false;
  3730. //update will happen later anyway so do nothing.
  3731. return material_data;
  3732. }
  3733. ////////
  3734. /* PARTICLES COLLISION API */
  3735. RID RasterizerStorageRD::particles_collision_create() {
  3736. return particles_collision_owner.make_rid(ParticlesCollision());
  3737. }
  3738. RID RasterizerStorageRD::particles_collision_get_heightfield_framebuffer(RID p_particles_collision) const {
  3739. ParticlesCollision *particles_collision = particles_collision_owner.getornull(p_particles_collision);
  3740. ERR_FAIL_COND_V(!particles_collision, RID());
  3741. ERR_FAIL_COND_V(particles_collision->type != RS::PARTICLES_COLLISION_TYPE_HEIGHTFIELD_COLLIDE, RID());
  3742. if (particles_collision->heightfield_texture == RID()) {
  3743. //create
  3744. int resolutions[RS::PARTICLES_COLLISION_HEIGHTFIELD_RESOLUTION_MAX] = { 256, 512, 1024, 2048, 4096, 8192 };
  3745. Size2i size;
  3746. if (particles_collision->extents.x > particles_collision->extents.z) {
  3747. size.x = resolutions[particles_collision->heightfield_resolution];
  3748. size.y = int32_t(particles_collision->extents.z / particles_collision->extents.x * size.x);
  3749. } else {
  3750. size.y = resolutions[particles_collision->heightfield_resolution];
  3751. size.x = int32_t(particles_collision->extents.x / particles_collision->extents.z * size.y);
  3752. }
  3753. RD::TextureFormat tf;
  3754. tf.format = RD::DATA_FORMAT_D32_SFLOAT;
  3755. tf.width = size.x;
  3756. tf.height = size.y;
  3757. tf.type = RD::TEXTURE_TYPE_2D;
  3758. tf.usage_bits = RD::TEXTURE_USAGE_SAMPLING_BIT | RD::TEXTURE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT;
  3759. particles_collision->heightfield_texture = RD::get_singleton()->texture_create(tf, RD::TextureView());
  3760. Vector<RID> fb_tex;
  3761. fb_tex.push_back(particles_collision->heightfield_texture);
  3762. particles_collision->heightfield_fb = RD::get_singleton()->framebuffer_create(fb_tex);
  3763. particles_collision->heightfield_fb_size = size;
  3764. }
  3765. return particles_collision->heightfield_fb;
  3766. }
  3767. void RasterizerStorageRD::particles_collision_set_collision_type(RID p_particles_collision, RS::ParticlesCollisionType p_type) {
  3768. ParticlesCollision *particles_collision = particles_collision_owner.getornull(p_particles_collision);
  3769. ERR_FAIL_COND(!particles_collision);
  3770. if (p_type == particles_collision->type) {
  3771. return;
  3772. }
  3773. if (particles_collision->heightfield_texture.is_valid()) {
  3774. RD::get_singleton()->free(particles_collision->heightfield_texture);
  3775. particles_collision->heightfield_texture = RID();
  3776. }
  3777. particles_collision->type = p_type;
  3778. particles_collision->instance_dependency.instance_notify_changed(true, false);
  3779. }
  3780. void RasterizerStorageRD::particles_collision_set_cull_mask(RID p_particles_collision, uint32_t p_cull_mask) {
  3781. ParticlesCollision *particles_collision = particles_collision_owner.getornull(p_particles_collision);
  3782. ERR_FAIL_COND(!particles_collision);
  3783. particles_collision->cull_mask = p_cull_mask;
  3784. }
  3785. void RasterizerStorageRD::particles_collision_set_sphere_radius(RID p_particles_collision, float p_radius) {
  3786. ParticlesCollision *particles_collision = particles_collision_owner.getornull(p_particles_collision);
  3787. ERR_FAIL_COND(!particles_collision);
  3788. particles_collision->radius = p_radius;
  3789. particles_collision->instance_dependency.instance_notify_changed(true, false);
  3790. }
  3791. void RasterizerStorageRD::particles_collision_set_box_extents(RID p_particles_collision, const Vector3 &p_extents) {
  3792. ParticlesCollision *particles_collision = particles_collision_owner.getornull(p_particles_collision);
  3793. ERR_FAIL_COND(!particles_collision);
  3794. particles_collision->extents = p_extents;
  3795. particles_collision->instance_dependency.instance_notify_changed(true, false);
  3796. }
  3797. void RasterizerStorageRD::particles_collision_set_attractor_strength(RID p_particles_collision, float p_strength) {
  3798. ParticlesCollision *particles_collision = particles_collision_owner.getornull(p_particles_collision);
  3799. ERR_FAIL_COND(!particles_collision);
  3800. particles_collision->attractor_strength = p_strength;
  3801. }
  3802. void RasterizerStorageRD::particles_collision_set_attractor_directionality(RID p_particles_collision, float p_directionality) {
  3803. ParticlesCollision *particles_collision = particles_collision_owner.getornull(p_particles_collision);
  3804. ERR_FAIL_COND(!particles_collision);
  3805. particles_collision->attractor_directionality = p_directionality;
  3806. }
  3807. void RasterizerStorageRD::particles_collision_set_attractor_attenuation(RID p_particles_collision, float p_curve) {
  3808. ParticlesCollision *particles_collision = particles_collision_owner.getornull(p_particles_collision);
  3809. ERR_FAIL_COND(!particles_collision);
  3810. particles_collision->attractor_attenuation = p_curve;
  3811. }
  3812. void RasterizerStorageRD::particles_collision_set_field_texture(RID p_particles_collision, RID p_texture) {
  3813. ParticlesCollision *particles_collision = particles_collision_owner.getornull(p_particles_collision);
  3814. ERR_FAIL_COND(!particles_collision);
  3815. particles_collision->field_texture = p_texture;
  3816. }
  3817. void RasterizerStorageRD::particles_collision_height_field_update(RID p_particles_collision) {
  3818. ParticlesCollision *particles_collision = particles_collision_owner.getornull(p_particles_collision);
  3819. ERR_FAIL_COND(!particles_collision);
  3820. particles_collision->instance_dependency.instance_notify_changed(true, false);
  3821. }
  3822. void RasterizerStorageRD::particles_collision_set_height_field_resolution(RID p_particles_collision, RS::ParticlesCollisionHeightfieldResolution p_resolution) {
  3823. ParticlesCollision *particles_collision = particles_collision_owner.getornull(p_particles_collision);
  3824. ERR_FAIL_COND(!particles_collision);
  3825. if (particles_collision->heightfield_resolution == p_resolution) {
  3826. return;
  3827. }
  3828. particles_collision->heightfield_resolution = p_resolution;
  3829. if (particles_collision->heightfield_texture.is_valid()) {
  3830. RD::get_singleton()->free(particles_collision->heightfield_texture);
  3831. particles_collision->heightfield_texture = RID();
  3832. }
  3833. }
  3834. AABB RasterizerStorageRD::particles_collision_get_aabb(RID p_particles_collision) const {
  3835. ParticlesCollision *particles_collision = particles_collision_owner.getornull(p_particles_collision);
  3836. ERR_FAIL_COND_V(!particles_collision, AABB());
  3837. switch (particles_collision->type) {
  3838. case RS::PARTICLES_COLLISION_TYPE_SPHERE_ATTRACT:
  3839. case RS::PARTICLES_COLLISION_TYPE_SPHERE_COLLIDE: {
  3840. AABB aabb;
  3841. aabb.position = -Vector3(1, 1, 1) * particles_collision->radius;
  3842. aabb.size = Vector3(2, 2, 2) * particles_collision->radius;
  3843. return aabb;
  3844. }
  3845. default: {
  3846. AABB aabb;
  3847. aabb.position = -particles_collision->extents;
  3848. aabb.size = particles_collision->extents * 2;
  3849. return aabb;
  3850. }
  3851. }
  3852. return AABB();
  3853. }
  3854. Vector3 RasterizerStorageRD::particles_collision_get_extents(RID p_particles_collision) const {
  3855. const ParticlesCollision *particles_collision = particles_collision_owner.getornull(p_particles_collision);
  3856. ERR_FAIL_COND_V(!particles_collision, Vector3());
  3857. return particles_collision->extents;
  3858. }
  3859. bool RasterizerStorageRD::particles_collision_is_heightfield(RID p_particles_collision) const {
  3860. const ParticlesCollision *particles_collision = particles_collision_owner.getornull(p_particles_collision);
  3861. ERR_FAIL_COND_V(!particles_collision, false);
  3862. return particles_collision->type == RS::PARTICLES_COLLISION_TYPE_HEIGHTFIELD_COLLIDE;
  3863. }
  3864. /* SKELETON API */
  3865. RID RasterizerStorageRD::skeleton_create() {
  3866. return skeleton_owner.make_rid(Skeleton());
  3867. }
  3868. void RasterizerStorageRD::_skeleton_make_dirty(Skeleton *skeleton) {
  3869. if (!skeleton->dirty) {
  3870. skeleton->dirty = true;
  3871. skeleton->dirty_list = skeleton_dirty_list;
  3872. skeleton_dirty_list = skeleton;
  3873. }
  3874. }
  3875. void RasterizerStorageRD::skeleton_allocate(RID p_skeleton, int p_bones, bool p_2d_skeleton) {
  3876. Skeleton *skeleton = skeleton_owner.getornull(p_skeleton);
  3877. ERR_FAIL_COND(!skeleton);
  3878. ERR_FAIL_COND(p_bones < 0);
  3879. if (skeleton->size == p_bones && skeleton->use_2d == p_2d_skeleton) {
  3880. return;
  3881. }
  3882. skeleton->size = p_bones;
  3883. skeleton->use_2d = p_2d_skeleton;
  3884. skeleton->uniform_set_3d = RID();
  3885. if (skeleton->buffer.is_valid()) {
  3886. RD::get_singleton()->free(skeleton->buffer);
  3887. skeleton->buffer = RID();
  3888. skeleton->data.resize(0);
  3889. }
  3890. if (skeleton->size) {
  3891. skeleton->data.resize(skeleton->size * (skeleton->use_2d ? 8 : 12));
  3892. skeleton->buffer = RD::get_singleton()->storage_buffer_create(skeleton->data.size() * sizeof(float));
  3893. zeromem(skeleton->data.ptrw(), skeleton->data.size() * sizeof(float));
  3894. _skeleton_make_dirty(skeleton);
  3895. }
  3896. }
  3897. int RasterizerStorageRD::skeleton_get_bone_count(RID p_skeleton) const {
  3898. Skeleton *skeleton = skeleton_owner.getornull(p_skeleton);
  3899. ERR_FAIL_COND_V(!skeleton, 0);
  3900. return skeleton->size;
  3901. }
  3902. void RasterizerStorageRD::skeleton_bone_set_transform(RID p_skeleton, int p_bone, const Transform &p_transform) {
  3903. Skeleton *skeleton = skeleton_owner.getornull(p_skeleton);
  3904. ERR_FAIL_COND(!skeleton);
  3905. ERR_FAIL_INDEX(p_bone, skeleton->size);
  3906. ERR_FAIL_COND(skeleton->use_2d);
  3907. float *dataptr = skeleton->data.ptrw() + p_bone * 12;
  3908. dataptr[0] = p_transform.basis.elements[0][0];
  3909. dataptr[1] = p_transform.basis.elements[0][1];
  3910. dataptr[2] = p_transform.basis.elements[0][2];
  3911. dataptr[3] = p_transform.origin.x;
  3912. dataptr[4] = p_transform.basis.elements[1][0];
  3913. dataptr[5] = p_transform.basis.elements[1][1];
  3914. dataptr[6] = p_transform.basis.elements[1][2];
  3915. dataptr[7] = p_transform.origin.y;
  3916. dataptr[8] = p_transform.basis.elements[2][0];
  3917. dataptr[9] = p_transform.basis.elements[2][1];
  3918. dataptr[10] = p_transform.basis.elements[2][2];
  3919. dataptr[11] = p_transform.origin.z;
  3920. _skeleton_make_dirty(skeleton);
  3921. }
  3922. Transform RasterizerStorageRD::skeleton_bone_get_transform(RID p_skeleton, int p_bone) const {
  3923. Skeleton *skeleton = skeleton_owner.getornull(p_skeleton);
  3924. ERR_FAIL_COND_V(!skeleton, Transform());
  3925. ERR_FAIL_INDEX_V(p_bone, skeleton->size, Transform());
  3926. ERR_FAIL_COND_V(skeleton->use_2d, Transform());
  3927. const float *dataptr = skeleton->data.ptr() + p_bone * 12;
  3928. Transform t;
  3929. t.basis.elements[0][0] = dataptr[0];
  3930. t.basis.elements[0][1] = dataptr[1];
  3931. t.basis.elements[0][2] = dataptr[2];
  3932. t.origin.x = dataptr[3];
  3933. t.basis.elements[1][0] = dataptr[4];
  3934. t.basis.elements[1][1] = dataptr[5];
  3935. t.basis.elements[1][2] = dataptr[6];
  3936. t.origin.y = dataptr[7];
  3937. t.basis.elements[2][0] = dataptr[8];
  3938. t.basis.elements[2][1] = dataptr[9];
  3939. t.basis.elements[2][2] = dataptr[10];
  3940. t.origin.z = dataptr[11];
  3941. return t;
  3942. }
  3943. void RasterizerStorageRD::skeleton_bone_set_transform_2d(RID p_skeleton, int p_bone, const Transform2D &p_transform) {
  3944. Skeleton *skeleton = skeleton_owner.getornull(p_skeleton);
  3945. ERR_FAIL_COND(!skeleton);
  3946. ERR_FAIL_INDEX(p_bone, skeleton->size);
  3947. ERR_FAIL_COND(!skeleton->use_2d);
  3948. float *dataptr = skeleton->data.ptrw() + p_bone * 8;
  3949. dataptr[0] = p_transform.elements[0][0];
  3950. dataptr[1] = p_transform.elements[1][0];
  3951. dataptr[2] = 0;
  3952. dataptr[3] = p_transform.elements[2][0];
  3953. dataptr[4] = p_transform.elements[0][1];
  3954. dataptr[5] = p_transform.elements[1][1];
  3955. dataptr[6] = 0;
  3956. dataptr[7] = p_transform.elements[2][1];
  3957. _skeleton_make_dirty(skeleton);
  3958. }
  3959. Transform2D RasterizerStorageRD::skeleton_bone_get_transform_2d(RID p_skeleton, int p_bone) const {
  3960. Skeleton *skeleton = skeleton_owner.getornull(p_skeleton);
  3961. ERR_FAIL_COND_V(!skeleton, Transform2D());
  3962. ERR_FAIL_INDEX_V(p_bone, skeleton->size, Transform2D());
  3963. ERR_FAIL_COND_V(!skeleton->use_2d, Transform2D());
  3964. const float *dataptr = skeleton->data.ptr() + p_bone * 8;
  3965. Transform2D t;
  3966. t.elements[0][0] = dataptr[0];
  3967. t.elements[1][0] = dataptr[1];
  3968. t.elements[2][0] = dataptr[3];
  3969. t.elements[0][1] = dataptr[4];
  3970. t.elements[1][1] = dataptr[5];
  3971. t.elements[2][1] = dataptr[7];
  3972. return t;
  3973. }
  3974. void RasterizerStorageRD::skeleton_set_base_transform_2d(RID p_skeleton, const Transform2D &p_base_transform) {
  3975. Skeleton *skeleton = skeleton_owner.getornull(p_skeleton);
  3976. ERR_FAIL_COND(!skeleton->use_2d);
  3977. skeleton->base_transform_2d = p_base_transform;
  3978. }
  3979. void RasterizerStorageRD::_update_dirty_skeletons() {
  3980. while (skeleton_dirty_list) {
  3981. Skeleton *skeleton = skeleton_dirty_list;
  3982. if (skeleton->size) {
  3983. RD::get_singleton()->buffer_update(skeleton->buffer, 0, skeleton->data.size() * sizeof(float), skeleton->data.ptr(), false);
  3984. }
  3985. skeleton_dirty_list = skeleton->dirty_list;
  3986. skeleton->instance_dependency.instance_notify_changed(true, false);
  3987. skeleton->dirty = false;
  3988. skeleton->dirty_list = nullptr;
  3989. }
  3990. skeleton_dirty_list = nullptr;
  3991. }
  3992. /* LIGHT */
  3993. RID RasterizerStorageRD::light_create(RS::LightType p_type) {
  3994. Light light;
  3995. light.type = p_type;
  3996. light.param[RS::LIGHT_PARAM_ENERGY] = 1.0;
  3997. light.param[RS::LIGHT_PARAM_INDIRECT_ENERGY] = 1.0;
  3998. light.param[RS::LIGHT_PARAM_SPECULAR] = 0.5;
  3999. light.param[RS::LIGHT_PARAM_RANGE] = 1.0;
  4000. light.param[RS::LIGHT_PARAM_SIZE] = 0.0;
  4001. light.param[RS::LIGHT_PARAM_SPOT_ANGLE] = 45;
  4002. light.param[RS::LIGHT_PARAM_SHADOW_MAX_DISTANCE] = 0;
  4003. light.param[RS::LIGHT_PARAM_SHADOW_SPLIT_1_OFFSET] = 0.1;
  4004. light.param[RS::LIGHT_PARAM_SHADOW_SPLIT_2_OFFSET] = 0.3;
  4005. light.param[RS::LIGHT_PARAM_SHADOW_SPLIT_3_OFFSET] = 0.6;
  4006. light.param[RS::LIGHT_PARAM_SHADOW_FADE_START] = 0.8;
  4007. light.param[RS::LIGHT_PARAM_SHADOW_BIAS] = 0.02;
  4008. light.param[RS::LIGHT_PARAM_SHADOW_NORMAL_BIAS] = 1.0;
  4009. light.param[RS::LIGHT_PARAM_SHADOW_PANCAKE_SIZE] = 20.0;
  4010. light.param[RS::LIGHT_PARAM_TRANSMITTANCE_BIAS] = 0.05;
  4011. light.param[RS::LIGHT_PARAM_SHADOW_VOLUMETRIC_FOG_FADE] = 1.0;
  4012. return light_owner.make_rid(light);
  4013. }
  4014. void RasterizerStorageRD::light_set_color(RID p_light, const Color &p_color) {
  4015. Light *light = light_owner.getornull(p_light);
  4016. ERR_FAIL_COND(!light);
  4017. light->color = p_color;
  4018. }
  4019. void RasterizerStorageRD::light_set_param(RID p_light, RS::LightParam p_param, float p_value) {
  4020. Light *light = light_owner.getornull(p_light);
  4021. ERR_FAIL_COND(!light);
  4022. ERR_FAIL_INDEX(p_param, RS::LIGHT_PARAM_MAX);
  4023. switch (p_param) {
  4024. case RS::LIGHT_PARAM_RANGE:
  4025. case RS::LIGHT_PARAM_SPOT_ANGLE:
  4026. case RS::LIGHT_PARAM_SHADOW_MAX_DISTANCE:
  4027. case RS::LIGHT_PARAM_SHADOW_SPLIT_1_OFFSET:
  4028. case RS::LIGHT_PARAM_SHADOW_SPLIT_2_OFFSET:
  4029. case RS::LIGHT_PARAM_SHADOW_SPLIT_3_OFFSET:
  4030. case RS::LIGHT_PARAM_SHADOW_NORMAL_BIAS:
  4031. case RS::LIGHT_PARAM_SHADOW_PANCAKE_SIZE:
  4032. case RS::LIGHT_PARAM_SHADOW_BIAS: {
  4033. light->version++;
  4034. light->instance_dependency.instance_notify_changed(true, false);
  4035. } break;
  4036. default: {
  4037. }
  4038. }
  4039. light->param[p_param] = p_value;
  4040. }
  4041. void RasterizerStorageRD::light_set_shadow(RID p_light, bool p_enabled) {
  4042. Light *light = light_owner.getornull(p_light);
  4043. ERR_FAIL_COND(!light);
  4044. light->shadow = p_enabled;
  4045. light->version++;
  4046. light->instance_dependency.instance_notify_changed(true, false);
  4047. }
  4048. void RasterizerStorageRD::light_set_shadow_color(RID p_light, const Color &p_color) {
  4049. Light *light = light_owner.getornull(p_light);
  4050. ERR_FAIL_COND(!light);
  4051. light->shadow_color = p_color;
  4052. }
  4053. void RasterizerStorageRD::light_set_projector(RID p_light, RID p_texture) {
  4054. Light *light = light_owner.getornull(p_light);
  4055. ERR_FAIL_COND(!light);
  4056. if (light->projector == p_texture) {
  4057. return;
  4058. }
  4059. if (light->type != RS::LIGHT_DIRECTIONAL && light->projector.is_valid()) {
  4060. texture_remove_from_decal_atlas(light->projector, light->type == RS::LIGHT_OMNI);
  4061. }
  4062. light->projector = p_texture;
  4063. if (light->type != RS::LIGHT_DIRECTIONAL && light->projector.is_valid()) {
  4064. texture_add_to_decal_atlas(light->projector, light->type == RS::LIGHT_OMNI);
  4065. }
  4066. }
  4067. void RasterizerStorageRD::light_set_negative(RID p_light, bool p_enable) {
  4068. Light *light = light_owner.getornull(p_light);
  4069. ERR_FAIL_COND(!light);
  4070. light->negative = p_enable;
  4071. }
  4072. void RasterizerStorageRD::light_set_cull_mask(RID p_light, uint32_t p_mask) {
  4073. Light *light = light_owner.getornull(p_light);
  4074. ERR_FAIL_COND(!light);
  4075. light->cull_mask = p_mask;
  4076. light->version++;
  4077. light->instance_dependency.instance_notify_changed(true, false);
  4078. }
  4079. void RasterizerStorageRD::light_set_reverse_cull_face_mode(RID p_light, bool p_enabled) {
  4080. Light *light = light_owner.getornull(p_light);
  4081. ERR_FAIL_COND(!light);
  4082. light->reverse_cull = p_enabled;
  4083. light->version++;
  4084. light->instance_dependency.instance_notify_changed(true, false);
  4085. }
  4086. void RasterizerStorageRD::light_set_bake_mode(RID p_light, RS::LightBakeMode p_bake_mode) {
  4087. Light *light = light_owner.getornull(p_light);
  4088. ERR_FAIL_COND(!light);
  4089. light->bake_mode = p_bake_mode;
  4090. light->version++;
  4091. light->instance_dependency.instance_notify_changed(true, false);
  4092. }
  4093. void RasterizerStorageRD::light_set_max_sdfgi_cascade(RID p_light, uint32_t p_cascade) {
  4094. Light *light = light_owner.getornull(p_light);
  4095. ERR_FAIL_COND(!light);
  4096. light->max_sdfgi_cascade = p_cascade;
  4097. light->version++;
  4098. light->instance_dependency.instance_notify_changed(true, false);
  4099. }
  4100. void RasterizerStorageRD::light_omni_set_shadow_mode(RID p_light, RS::LightOmniShadowMode p_mode) {
  4101. Light *light = light_owner.getornull(p_light);
  4102. ERR_FAIL_COND(!light);
  4103. light->omni_shadow_mode = p_mode;
  4104. light->version++;
  4105. light->instance_dependency.instance_notify_changed(true, false);
  4106. }
  4107. RS::LightOmniShadowMode RasterizerStorageRD::light_omni_get_shadow_mode(RID p_light) {
  4108. const Light *light = light_owner.getornull(p_light);
  4109. ERR_FAIL_COND_V(!light, RS::LIGHT_OMNI_SHADOW_CUBE);
  4110. return light->omni_shadow_mode;
  4111. }
  4112. void RasterizerStorageRD::light_directional_set_shadow_mode(RID p_light, RS::LightDirectionalShadowMode p_mode) {
  4113. Light *light = light_owner.getornull(p_light);
  4114. ERR_FAIL_COND(!light);
  4115. light->directional_shadow_mode = p_mode;
  4116. light->version++;
  4117. light->instance_dependency.instance_notify_changed(true, false);
  4118. }
  4119. void RasterizerStorageRD::light_directional_set_blend_splits(RID p_light, bool p_enable) {
  4120. Light *light = light_owner.getornull(p_light);
  4121. ERR_FAIL_COND(!light);
  4122. light->directional_blend_splits = p_enable;
  4123. light->version++;
  4124. light->instance_dependency.instance_notify_changed(true, false);
  4125. }
  4126. bool RasterizerStorageRD::light_directional_get_blend_splits(RID p_light) const {
  4127. const Light *light = light_owner.getornull(p_light);
  4128. ERR_FAIL_COND_V(!light, false);
  4129. return light->directional_blend_splits;
  4130. }
  4131. RS::LightDirectionalShadowMode RasterizerStorageRD::light_directional_get_shadow_mode(RID p_light) {
  4132. const Light *light = light_owner.getornull(p_light);
  4133. ERR_FAIL_COND_V(!light, RS::LIGHT_DIRECTIONAL_SHADOW_ORTHOGONAL);
  4134. return light->directional_shadow_mode;
  4135. }
  4136. void RasterizerStorageRD::light_directional_set_shadow_depth_range_mode(RID p_light, RS::LightDirectionalShadowDepthRangeMode p_range_mode) {
  4137. Light *light = light_owner.getornull(p_light);
  4138. ERR_FAIL_COND(!light);
  4139. light->directional_range_mode = p_range_mode;
  4140. }
  4141. RS::LightDirectionalShadowDepthRangeMode RasterizerStorageRD::light_directional_get_shadow_depth_range_mode(RID p_light) const {
  4142. const Light *light = light_owner.getornull(p_light);
  4143. ERR_FAIL_COND_V(!light, RS::LIGHT_DIRECTIONAL_SHADOW_DEPTH_RANGE_STABLE);
  4144. return light->directional_range_mode;
  4145. }
  4146. uint32_t RasterizerStorageRD::light_get_max_sdfgi_cascade(RID p_light) {
  4147. const Light *light = light_owner.getornull(p_light);
  4148. ERR_FAIL_COND_V(!light, 0);
  4149. return light->max_sdfgi_cascade;
  4150. }
  4151. RS::LightBakeMode RasterizerStorageRD::light_get_bake_mode(RID p_light) {
  4152. const Light *light = light_owner.getornull(p_light);
  4153. ERR_FAIL_COND_V(!light, RS::LIGHT_BAKE_DISABLED);
  4154. return light->bake_mode;
  4155. }
  4156. uint64_t RasterizerStorageRD::light_get_version(RID p_light) const {
  4157. const Light *light = light_owner.getornull(p_light);
  4158. ERR_FAIL_COND_V(!light, 0);
  4159. return light->version;
  4160. }
  4161. AABB RasterizerStorageRD::light_get_aabb(RID p_light) const {
  4162. const Light *light = light_owner.getornull(p_light);
  4163. ERR_FAIL_COND_V(!light, AABB());
  4164. switch (light->type) {
  4165. case RS::LIGHT_SPOT: {
  4166. float len = light->param[RS::LIGHT_PARAM_RANGE];
  4167. float size = Math::tan(Math::deg2rad(light->param[RS::LIGHT_PARAM_SPOT_ANGLE])) * len;
  4168. return AABB(Vector3(-size, -size, -len), Vector3(size * 2, size * 2, len));
  4169. };
  4170. case RS::LIGHT_OMNI: {
  4171. float r = light->param[RS::LIGHT_PARAM_RANGE];
  4172. return AABB(-Vector3(r, r, r), Vector3(r, r, r) * 2);
  4173. };
  4174. case RS::LIGHT_DIRECTIONAL: {
  4175. return AABB();
  4176. };
  4177. }
  4178. ERR_FAIL_V(AABB());
  4179. }
  4180. /* REFLECTION PROBE */
  4181. RID RasterizerStorageRD::reflection_probe_create() {
  4182. return reflection_probe_owner.make_rid(ReflectionProbe());
  4183. }
  4184. void RasterizerStorageRD::reflection_probe_set_update_mode(RID p_probe, RS::ReflectionProbeUpdateMode p_mode) {
  4185. ReflectionProbe *reflection_probe = reflection_probe_owner.getornull(p_probe);
  4186. ERR_FAIL_COND(!reflection_probe);
  4187. reflection_probe->update_mode = p_mode;
  4188. reflection_probe->instance_dependency.instance_notify_changed(true, false);
  4189. }
  4190. void RasterizerStorageRD::reflection_probe_set_intensity(RID p_probe, float p_intensity) {
  4191. ReflectionProbe *reflection_probe = reflection_probe_owner.getornull(p_probe);
  4192. ERR_FAIL_COND(!reflection_probe);
  4193. reflection_probe->intensity = p_intensity;
  4194. }
  4195. void RasterizerStorageRD::reflection_probe_set_ambient_mode(RID p_probe, RS::ReflectionProbeAmbientMode p_mode) {
  4196. ReflectionProbe *reflection_probe = reflection_probe_owner.getornull(p_probe);
  4197. ERR_FAIL_COND(!reflection_probe);
  4198. reflection_probe->ambient_mode = p_mode;
  4199. }
  4200. void RasterizerStorageRD::reflection_probe_set_ambient_color(RID p_probe, const Color &p_color) {
  4201. ReflectionProbe *reflection_probe = reflection_probe_owner.getornull(p_probe);
  4202. ERR_FAIL_COND(!reflection_probe);
  4203. reflection_probe->ambient_color = p_color;
  4204. }
  4205. void RasterizerStorageRD::reflection_probe_set_ambient_energy(RID p_probe, float p_energy) {
  4206. ReflectionProbe *reflection_probe = reflection_probe_owner.getornull(p_probe);
  4207. ERR_FAIL_COND(!reflection_probe);
  4208. reflection_probe->ambient_color_energy = p_energy;
  4209. }
  4210. void RasterizerStorageRD::reflection_probe_set_max_distance(RID p_probe, float p_distance) {
  4211. ReflectionProbe *reflection_probe = reflection_probe_owner.getornull(p_probe);
  4212. ERR_FAIL_COND(!reflection_probe);
  4213. reflection_probe->max_distance = p_distance;
  4214. reflection_probe->instance_dependency.instance_notify_changed(true, false);
  4215. }
  4216. void RasterizerStorageRD::reflection_probe_set_extents(RID p_probe, const Vector3 &p_extents) {
  4217. ReflectionProbe *reflection_probe = reflection_probe_owner.getornull(p_probe);
  4218. ERR_FAIL_COND(!reflection_probe);
  4219. if (reflection_probe->extents == p_extents) {
  4220. return;
  4221. }
  4222. reflection_probe->extents = p_extents;
  4223. reflection_probe->instance_dependency.instance_notify_changed(true, false);
  4224. }
  4225. void RasterizerStorageRD::reflection_probe_set_origin_offset(RID p_probe, const Vector3 &p_offset) {
  4226. ReflectionProbe *reflection_probe = reflection_probe_owner.getornull(p_probe);
  4227. ERR_FAIL_COND(!reflection_probe);
  4228. reflection_probe->origin_offset = p_offset;
  4229. reflection_probe->instance_dependency.instance_notify_changed(true, false);
  4230. }
  4231. void RasterizerStorageRD::reflection_probe_set_as_interior(RID p_probe, bool p_enable) {
  4232. ReflectionProbe *reflection_probe = reflection_probe_owner.getornull(p_probe);
  4233. ERR_FAIL_COND(!reflection_probe);
  4234. reflection_probe->interior = p_enable;
  4235. reflection_probe->instance_dependency.instance_notify_changed(true, false);
  4236. }
  4237. void RasterizerStorageRD::reflection_probe_set_enable_box_projection(RID p_probe, bool p_enable) {
  4238. ReflectionProbe *reflection_probe = reflection_probe_owner.getornull(p_probe);
  4239. ERR_FAIL_COND(!reflection_probe);
  4240. reflection_probe->box_projection = p_enable;
  4241. }
  4242. void RasterizerStorageRD::reflection_probe_set_enable_shadows(RID p_probe, bool p_enable) {
  4243. ReflectionProbe *reflection_probe = reflection_probe_owner.getornull(p_probe);
  4244. ERR_FAIL_COND(!reflection_probe);
  4245. reflection_probe->enable_shadows = p_enable;
  4246. reflection_probe->instance_dependency.instance_notify_changed(true, false);
  4247. }
  4248. void RasterizerStorageRD::reflection_probe_set_cull_mask(RID p_probe, uint32_t p_layers) {
  4249. ReflectionProbe *reflection_probe = reflection_probe_owner.getornull(p_probe);
  4250. ERR_FAIL_COND(!reflection_probe);
  4251. reflection_probe->cull_mask = p_layers;
  4252. reflection_probe->instance_dependency.instance_notify_changed(true, false);
  4253. }
  4254. void RasterizerStorageRD::reflection_probe_set_resolution(RID p_probe, int p_resolution) {
  4255. ReflectionProbe *reflection_probe = reflection_probe_owner.getornull(p_probe);
  4256. ERR_FAIL_COND(!reflection_probe);
  4257. ERR_FAIL_COND(p_resolution < 32);
  4258. reflection_probe->resolution = p_resolution;
  4259. }
  4260. AABB RasterizerStorageRD::reflection_probe_get_aabb(RID p_probe) const {
  4261. const ReflectionProbe *reflection_probe = reflection_probe_owner.getornull(p_probe);
  4262. ERR_FAIL_COND_V(!reflection_probe, AABB());
  4263. AABB aabb;
  4264. aabb.position = -reflection_probe->extents;
  4265. aabb.size = reflection_probe->extents * 2.0;
  4266. return aabb;
  4267. }
  4268. RS::ReflectionProbeUpdateMode RasterizerStorageRD::reflection_probe_get_update_mode(RID p_probe) const {
  4269. const ReflectionProbe *reflection_probe = reflection_probe_owner.getornull(p_probe);
  4270. ERR_FAIL_COND_V(!reflection_probe, RS::REFLECTION_PROBE_UPDATE_ALWAYS);
  4271. return reflection_probe->update_mode;
  4272. }
  4273. uint32_t RasterizerStorageRD::reflection_probe_get_cull_mask(RID p_probe) const {
  4274. const ReflectionProbe *reflection_probe = reflection_probe_owner.getornull(p_probe);
  4275. ERR_FAIL_COND_V(!reflection_probe, 0);
  4276. return reflection_probe->cull_mask;
  4277. }
  4278. Vector3 RasterizerStorageRD::reflection_probe_get_extents(RID p_probe) const {
  4279. const ReflectionProbe *reflection_probe = reflection_probe_owner.getornull(p_probe);
  4280. ERR_FAIL_COND_V(!reflection_probe, Vector3());
  4281. return reflection_probe->extents;
  4282. }
  4283. Vector3 RasterizerStorageRD::reflection_probe_get_origin_offset(RID p_probe) const {
  4284. const ReflectionProbe *reflection_probe = reflection_probe_owner.getornull(p_probe);
  4285. ERR_FAIL_COND_V(!reflection_probe, Vector3());
  4286. return reflection_probe->origin_offset;
  4287. }
  4288. bool RasterizerStorageRD::reflection_probe_renders_shadows(RID p_probe) const {
  4289. const ReflectionProbe *reflection_probe = reflection_probe_owner.getornull(p_probe);
  4290. ERR_FAIL_COND_V(!reflection_probe, false);
  4291. return reflection_probe->enable_shadows;
  4292. }
  4293. float RasterizerStorageRD::reflection_probe_get_origin_max_distance(RID p_probe) const {
  4294. const ReflectionProbe *reflection_probe = reflection_probe_owner.getornull(p_probe);
  4295. ERR_FAIL_COND_V(!reflection_probe, 0);
  4296. return reflection_probe->max_distance;
  4297. }
  4298. int RasterizerStorageRD::reflection_probe_get_resolution(RID p_probe) const {
  4299. const ReflectionProbe *reflection_probe = reflection_probe_owner.getornull(p_probe);
  4300. ERR_FAIL_COND_V(!reflection_probe, 0);
  4301. return reflection_probe->resolution;
  4302. }
  4303. float RasterizerStorageRD::reflection_probe_get_intensity(RID p_probe) const {
  4304. const ReflectionProbe *reflection_probe = reflection_probe_owner.getornull(p_probe);
  4305. ERR_FAIL_COND_V(!reflection_probe, 0);
  4306. return reflection_probe->intensity;
  4307. }
  4308. bool RasterizerStorageRD::reflection_probe_is_interior(RID p_probe) const {
  4309. const ReflectionProbe *reflection_probe = reflection_probe_owner.getornull(p_probe);
  4310. ERR_FAIL_COND_V(!reflection_probe, false);
  4311. return reflection_probe->interior;
  4312. }
  4313. bool RasterizerStorageRD::reflection_probe_is_box_projection(RID p_probe) const {
  4314. const ReflectionProbe *reflection_probe = reflection_probe_owner.getornull(p_probe);
  4315. ERR_FAIL_COND_V(!reflection_probe, false);
  4316. return reflection_probe->box_projection;
  4317. }
  4318. RS::ReflectionProbeAmbientMode RasterizerStorageRD::reflection_probe_get_ambient_mode(RID p_probe) const {
  4319. const ReflectionProbe *reflection_probe = reflection_probe_owner.getornull(p_probe);
  4320. ERR_FAIL_COND_V(!reflection_probe, RS::REFLECTION_PROBE_AMBIENT_DISABLED);
  4321. return reflection_probe->ambient_mode;
  4322. }
  4323. Color RasterizerStorageRD::reflection_probe_get_ambient_color(RID p_probe) const {
  4324. const ReflectionProbe *reflection_probe = reflection_probe_owner.getornull(p_probe);
  4325. ERR_FAIL_COND_V(!reflection_probe, Color());
  4326. return reflection_probe->ambient_color;
  4327. }
  4328. float RasterizerStorageRD::reflection_probe_get_ambient_color_energy(RID p_probe) const {
  4329. const ReflectionProbe *reflection_probe = reflection_probe_owner.getornull(p_probe);
  4330. ERR_FAIL_COND_V(!reflection_probe, 0);
  4331. return reflection_probe->ambient_color_energy;
  4332. }
  4333. RID RasterizerStorageRD::decal_create() {
  4334. return decal_owner.make_rid(Decal());
  4335. }
  4336. void RasterizerStorageRD::decal_set_extents(RID p_decal, const Vector3 &p_extents) {
  4337. Decal *decal = decal_owner.getornull(p_decal);
  4338. ERR_FAIL_COND(!decal);
  4339. decal->extents = p_extents;
  4340. decal->instance_dependency.instance_notify_changed(true, false);
  4341. }
  4342. void RasterizerStorageRD::decal_set_texture(RID p_decal, RS::DecalTexture p_type, RID p_texture) {
  4343. Decal *decal = decal_owner.getornull(p_decal);
  4344. ERR_FAIL_COND(!decal);
  4345. ERR_FAIL_INDEX(p_type, RS::DECAL_TEXTURE_MAX);
  4346. if (decal->textures[p_type] == p_texture) {
  4347. return;
  4348. }
  4349. ERR_FAIL_COND(p_texture.is_valid() && !texture_owner.owns(p_texture));
  4350. if (decal->textures[p_type].is_valid() && texture_owner.owns(decal->textures[p_type])) {
  4351. texture_remove_from_decal_atlas(decal->textures[p_type]);
  4352. }
  4353. decal->textures[p_type] = p_texture;
  4354. if (decal->textures[p_type].is_valid()) {
  4355. texture_add_to_decal_atlas(decal->textures[p_type]);
  4356. }
  4357. decal->instance_dependency.instance_notify_changed(false, true);
  4358. }
  4359. void RasterizerStorageRD::decal_set_emission_energy(RID p_decal, float p_energy) {
  4360. Decal *decal = decal_owner.getornull(p_decal);
  4361. ERR_FAIL_COND(!decal);
  4362. decal->emission_energy = p_energy;
  4363. }
  4364. void RasterizerStorageRD::decal_set_albedo_mix(RID p_decal, float p_mix) {
  4365. Decal *decal = decal_owner.getornull(p_decal);
  4366. ERR_FAIL_COND(!decal);
  4367. decal->albedo_mix = p_mix;
  4368. }
  4369. void RasterizerStorageRD::decal_set_modulate(RID p_decal, const Color &p_modulate) {
  4370. Decal *decal = decal_owner.getornull(p_decal);
  4371. ERR_FAIL_COND(!decal);
  4372. decal->modulate = p_modulate;
  4373. }
  4374. void RasterizerStorageRD::decal_set_cull_mask(RID p_decal, uint32_t p_layers) {
  4375. Decal *decal = decal_owner.getornull(p_decal);
  4376. ERR_FAIL_COND(!decal);
  4377. decal->cull_mask = p_layers;
  4378. decal->instance_dependency.instance_notify_changed(true, false);
  4379. }
  4380. void RasterizerStorageRD::decal_set_distance_fade(RID p_decal, bool p_enabled, float p_begin, float p_length) {
  4381. Decal *decal = decal_owner.getornull(p_decal);
  4382. ERR_FAIL_COND(!decal);
  4383. decal->distance_fade = p_enabled;
  4384. decal->distance_fade_begin = p_begin;
  4385. decal->distance_fade_length = p_length;
  4386. }
  4387. void RasterizerStorageRD::decal_set_fade(RID p_decal, float p_above, float p_below) {
  4388. Decal *decal = decal_owner.getornull(p_decal);
  4389. ERR_FAIL_COND(!decal);
  4390. decal->upper_fade = p_above;
  4391. decal->lower_fade = p_below;
  4392. }
  4393. void RasterizerStorageRD::decal_set_normal_fade(RID p_decal, float p_fade) {
  4394. Decal *decal = decal_owner.getornull(p_decal);
  4395. ERR_FAIL_COND(!decal);
  4396. decal->normal_fade = p_fade;
  4397. }
  4398. AABB RasterizerStorageRD::decal_get_aabb(RID p_decal) const {
  4399. Decal *decal = decal_owner.getornull(p_decal);
  4400. ERR_FAIL_COND_V(!decal, AABB());
  4401. return AABB(-decal->extents, decal->extents * 2.0);
  4402. }
  4403. RID RasterizerStorageRD::gi_probe_create() {
  4404. return gi_probe_owner.make_rid(GIProbe());
  4405. }
  4406. void RasterizerStorageRD::gi_probe_allocate(RID p_gi_probe, const Transform &p_to_cell_xform, const AABB &p_aabb, const Vector3i &p_octree_size, const Vector<uint8_t> &p_octree_cells, const Vector<uint8_t> &p_data_cells, const Vector<uint8_t> &p_distance_field, const Vector<int> &p_level_counts) {
  4407. GIProbe *gi_probe = gi_probe_owner.getornull(p_gi_probe);
  4408. ERR_FAIL_COND(!gi_probe);
  4409. if (gi_probe->octree_buffer.is_valid()) {
  4410. RD::get_singleton()->free(gi_probe->octree_buffer);
  4411. RD::get_singleton()->free(gi_probe->data_buffer);
  4412. if (gi_probe->sdf_texture.is_valid()) {
  4413. RD::get_singleton()->free(gi_probe->sdf_texture);
  4414. }
  4415. gi_probe->sdf_texture = RID();
  4416. gi_probe->octree_buffer = RID();
  4417. gi_probe->data_buffer = RID();
  4418. gi_probe->octree_buffer_size = 0;
  4419. gi_probe->data_buffer_size = 0;
  4420. gi_probe->cell_count = 0;
  4421. }
  4422. gi_probe->to_cell_xform = p_to_cell_xform;
  4423. gi_probe->bounds = p_aabb;
  4424. gi_probe->octree_size = p_octree_size;
  4425. gi_probe->level_counts = p_level_counts;
  4426. if (p_octree_cells.size()) {
  4427. ERR_FAIL_COND(p_octree_cells.size() % 32 != 0); //cells size must be a multiple of 32
  4428. uint32_t cell_count = p_octree_cells.size() / 32;
  4429. ERR_FAIL_COND(p_data_cells.size() != (int)cell_count * 16); //see that data size matches
  4430. gi_probe->cell_count = cell_count;
  4431. gi_probe->octree_buffer = RD::get_singleton()->storage_buffer_create(p_octree_cells.size(), p_octree_cells);
  4432. gi_probe->octree_buffer_size = p_octree_cells.size();
  4433. gi_probe->data_buffer = RD::get_singleton()->storage_buffer_create(p_data_cells.size(), p_data_cells);
  4434. gi_probe->data_buffer_size = p_data_cells.size();
  4435. if (p_distance_field.size()) {
  4436. RD::TextureFormat tf;
  4437. tf.format = RD::DATA_FORMAT_R8_UNORM;
  4438. tf.width = gi_probe->octree_size.x;
  4439. tf.height = gi_probe->octree_size.y;
  4440. tf.depth = gi_probe->octree_size.z;
  4441. tf.type = RD::TEXTURE_TYPE_3D;
  4442. tf.usage_bits = RD::TEXTURE_USAGE_SAMPLING_BIT | RD::TEXTURE_USAGE_CAN_UPDATE_BIT | RD::TEXTURE_USAGE_CAN_COPY_FROM_BIT;
  4443. Vector<Vector<uint8_t>> s;
  4444. s.push_back(p_distance_field);
  4445. gi_probe->sdf_texture = RD::get_singleton()->texture_create(tf, RD::TextureView(), s);
  4446. }
  4447. #if 0
  4448. {
  4449. RD::TextureFormat tf;
  4450. tf.format = RD::DATA_FORMAT_R8_UNORM;
  4451. tf.width = gi_probe->octree_size.x;
  4452. tf.height = gi_probe->octree_size.y;
  4453. tf.depth = gi_probe->octree_size.z;
  4454. tf.type = RD::TEXTURE_TYPE_3D;
  4455. tf.usage_bits = RD::TEXTURE_USAGE_SAMPLING_BIT | RD::TEXTURE_USAGE_STORAGE_BIT | RD::TEXTURE_USAGE_CAN_COPY_TO_BIT;
  4456. tf.shareable_formats.push_back(RD::DATA_FORMAT_R8_UNORM);
  4457. tf.shareable_formats.push_back(RD::DATA_FORMAT_R8_UINT);
  4458. gi_probe->sdf_texture = RD::get_singleton()->texture_create(tf, RD::TextureView());
  4459. }
  4460. RID shared_tex;
  4461. {
  4462. RD::TextureView tv;
  4463. tv.format_override = RD::DATA_FORMAT_R8_UINT;
  4464. shared_tex = RD::get_singleton()->texture_create_shared(tv, gi_probe->sdf_texture);
  4465. }
  4466. //update SDF texture
  4467. Vector<RD::Uniform> uniforms;
  4468. {
  4469. RD::Uniform u;
  4470. u.type = RD::UNIFORM_TYPE_STORAGE_BUFFER;
  4471. u.binding = 1;
  4472. u.ids.push_back(gi_probe->octree_buffer);
  4473. uniforms.push_back(u);
  4474. }
  4475. {
  4476. RD::Uniform u;
  4477. u.type = RD::UNIFORM_TYPE_STORAGE_BUFFER;
  4478. u.binding = 2;
  4479. u.ids.push_back(gi_probe->data_buffer);
  4480. uniforms.push_back(u);
  4481. }
  4482. {
  4483. RD::Uniform u;
  4484. u.type = RD::UNIFORM_TYPE_IMAGE;
  4485. u.binding = 3;
  4486. u.ids.push_back(shared_tex);
  4487. uniforms.push_back(u);
  4488. }
  4489. RID uniform_set = RD::get_singleton()->uniform_set_create(uniforms, giprobe_sdf_shader_version_shader, 0);
  4490. {
  4491. uint32_t push_constant[4] = { 0, 0, 0, 0 };
  4492. for (int i = 0; i < gi_probe->level_counts.size() - 1; i++) {
  4493. push_constant[0] += gi_probe->level_counts[i];
  4494. }
  4495. push_constant[1] = push_constant[0] + gi_probe->level_counts[gi_probe->level_counts.size() - 1];
  4496. print_line("offset: " + itos(push_constant[0]));
  4497. print_line("size: " + itos(push_constant[1]));
  4498. //create SDF
  4499. RD::ComputeListID compute_list = RD::get_singleton()->compute_list_begin();
  4500. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, giprobe_sdf_shader_pipeline);
  4501. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set, 0);
  4502. RD::get_singleton()->compute_list_set_push_constant(compute_list, push_constant, sizeof(uint32_t) * 4);
  4503. RD::get_singleton()->compute_list_dispatch(compute_list, gi_probe->octree_size.x / 4, gi_probe->octree_size.y / 4, gi_probe->octree_size.z / 4);
  4504. RD::get_singleton()->compute_list_end();
  4505. }
  4506. RD::get_singleton()->free(uniform_set);
  4507. RD::get_singleton()->free(shared_tex);
  4508. }
  4509. #endif
  4510. }
  4511. gi_probe->version++;
  4512. gi_probe->data_version++;
  4513. gi_probe->instance_dependency.instance_notify_changed(true, false);
  4514. }
  4515. AABB RasterizerStorageRD::gi_probe_get_bounds(RID p_gi_probe) const {
  4516. GIProbe *gi_probe = gi_probe_owner.getornull(p_gi_probe);
  4517. ERR_FAIL_COND_V(!gi_probe, AABB());
  4518. return gi_probe->bounds;
  4519. }
  4520. Vector3i RasterizerStorageRD::gi_probe_get_octree_size(RID p_gi_probe) const {
  4521. GIProbe *gi_probe = gi_probe_owner.getornull(p_gi_probe);
  4522. ERR_FAIL_COND_V(!gi_probe, Vector3i());
  4523. return gi_probe->octree_size;
  4524. }
  4525. Vector<uint8_t> RasterizerStorageRD::gi_probe_get_octree_cells(RID p_gi_probe) const {
  4526. GIProbe *gi_probe = gi_probe_owner.getornull(p_gi_probe);
  4527. ERR_FAIL_COND_V(!gi_probe, Vector<uint8_t>());
  4528. if (gi_probe->octree_buffer.is_valid()) {
  4529. return RD::get_singleton()->buffer_get_data(gi_probe->octree_buffer);
  4530. }
  4531. return Vector<uint8_t>();
  4532. }
  4533. Vector<uint8_t> RasterizerStorageRD::gi_probe_get_data_cells(RID p_gi_probe) const {
  4534. GIProbe *gi_probe = gi_probe_owner.getornull(p_gi_probe);
  4535. ERR_FAIL_COND_V(!gi_probe, Vector<uint8_t>());
  4536. if (gi_probe->data_buffer.is_valid()) {
  4537. return RD::get_singleton()->buffer_get_data(gi_probe->data_buffer);
  4538. }
  4539. return Vector<uint8_t>();
  4540. }
  4541. Vector<uint8_t> RasterizerStorageRD::gi_probe_get_distance_field(RID p_gi_probe) const {
  4542. GIProbe *gi_probe = gi_probe_owner.getornull(p_gi_probe);
  4543. ERR_FAIL_COND_V(!gi_probe, Vector<uint8_t>());
  4544. if (gi_probe->data_buffer.is_valid()) {
  4545. return RD::get_singleton()->texture_get_data(gi_probe->sdf_texture, 0);
  4546. }
  4547. return Vector<uint8_t>();
  4548. }
  4549. Vector<int> RasterizerStorageRD::gi_probe_get_level_counts(RID p_gi_probe) const {
  4550. GIProbe *gi_probe = gi_probe_owner.getornull(p_gi_probe);
  4551. ERR_FAIL_COND_V(!gi_probe, Vector<int>());
  4552. return gi_probe->level_counts;
  4553. }
  4554. Transform RasterizerStorageRD::gi_probe_get_to_cell_xform(RID p_gi_probe) const {
  4555. GIProbe *gi_probe = gi_probe_owner.getornull(p_gi_probe);
  4556. ERR_FAIL_COND_V(!gi_probe, Transform());
  4557. return gi_probe->to_cell_xform;
  4558. }
  4559. void RasterizerStorageRD::gi_probe_set_dynamic_range(RID p_gi_probe, float p_range) {
  4560. GIProbe *gi_probe = gi_probe_owner.getornull(p_gi_probe);
  4561. ERR_FAIL_COND(!gi_probe);
  4562. gi_probe->dynamic_range = p_range;
  4563. gi_probe->version++;
  4564. }
  4565. float RasterizerStorageRD::gi_probe_get_dynamic_range(RID p_gi_probe) const {
  4566. GIProbe *gi_probe = gi_probe_owner.getornull(p_gi_probe);
  4567. ERR_FAIL_COND_V(!gi_probe, 0);
  4568. return gi_probe->dynamic_range;
  4569. }
  4570. void RasterizerStorageRD::gi_probe_set_propagation(RID p_gi_probe, float p_range) {
  4571. GIProbe *gi_probe = gi_probe_owner.getornull(p_gi_probe);
  4572. ERR_FAIL_COND(!gi_probe);
  4573. gi_probe->propagation = p_range;
  4574. gi_probe->version++;
  4575. }
  4576. float RasterizerStorageRD::gi_probe_get_propagation(RID p_gi_probe) const {
  4577. GIProbe *gi_probe = gi_probe_owner.getornull(p_gi_probe);
  4578. ERR_FAIL_COND_V(!gi_probe, 0);
  4579. return gi_probe->propagation;
  4580. }
  4581. void RasterizerStorageRD::gi_probe_set_energy(RID p_gi_probe, float p_energy) {
  4582. GIProbe *gi_probe = gi_probe_owner.getornull(p_gi_probe);
  4583. ERR_FAIL_COND(!gi_probe);
  4584. gi_probe->energy = p_energy;
  4585. }
  4586. float RasterizerStorageRD::gi_probe_get_energy(RID p_gi_probe) const {
  4587. GIProbe *gi_probe = gi_probe_owner.getornull(p_gi_probe);
  4588. ERR_FAIL_COND_V(!gi_probe, 0);
  4589. return gi_probe->energy;
  4590. }
  4591. void RasterizerStorageRD::gi_probe_set_ao(RID p_gi_probe, float p_ao) {
  4592. GIProbe *gi_probe = gi_probe_owner.getornull(p_gi_probe);
  4593. ERR_FAIL_COND(!gi_probe);
  4594. gi_probe->ao = p_ao;
  4595. }
  4596. float RasterizerStorageRD::gi_probe_get_ao(RID p_gi_probe) const {
  4597. GIProbe *gi_probe = gi_probe_owner.getornull(p_gi_probe);
  4598. ERR_FAIL_COND_V(!gi_probe, 0);
  4599. return gi_probe->ao;
  4600. }
  4601. void RasterizerStorageRD::gi_probe_set_ao_size(RID p_gi_probe, float p_strength) {
  4602. GIProbe *gi_probe = gi_probe_owner.getornull(p_gi_probe);
  4603. ERR_FAIL_COND(!gi_probe);
  4604. gi_probe->ao_size = p_strength;
  4605. }
  4606. float RasterizerStorageRD::gi_probe_get_ao_size(RID p_gi_probe) const {
  4607. GIProbe *gi_probe = gi_probe_owner.getornull(p_gi_probe);
  4608. ERR_FAIL_COND_V(!gi_probe, 0);
  4609. return gi_probe->ao_size;
  4610. }
  4611. void RasterizerStorageRD::gi_probe_set_bias(RID p_gi_probe, float p_bias) {
  4612. GIProbe *gi_probe = gi_probe_owner.getornull(p_gi_probe);
  4613. ERR_FAIL_COND(!gi_probe);
  4614. gi_probe->bias = p_bias;
  4615. }
  4616. float RasterizerStorageRD::gi_probe_get_bias(RID p_gi_probe) const {
  4617. GIProbe *gi_probe = gi_probe_owner.getornull(p_gi_probe);
  4618. ERR_FAIL_COND_V(!gi_probe, 0);
  4619. return gi_probe->bias;
  4620. }
  4621. void RasterizerStorageRD::gi_probe_set_normal_bias(RID p_gi_probe, float p_normal_bias) {
  4622. GIProbe *gi_probe = gi_probe_owner.getornull(p_gi_probe);
  4623. ERR_FAIL_COND(!gi_probe);
  4624. gi_probe->normal_bias = p_normal_bias;
  4625. }
  4626. float RasterizerStorageRD::gi_probe_get_normal_bias(RID p_gi_probe) const {
  4627. GIProbe *gi_probe = gi_probe_owner.getornull(p_gi_probe);
  4628. ERR_FAIL_COND_V(!gi_probe, 0);
  4629. return gi_probe->normal_bias;
  4630. }
  4631. void RasterizerStorageRD::gi_probe_set_anisotropy_strength(RID p_gi_probe, float p_strength) {
  4632. GIProbe *gi_probe = gi_probe_owner.getornull(p_gi_probe);
  4633. ERR_FAIL_COND(!gi_probe);
  4634. gi_probe->anisotropy_strength = p_strength;
  4635. }
  4636. float RasterizerStorageRD::gi_probe_get_anisotropy_strength(RID p_gi_probe) const {
  4637. GIProbe *gi_probe = gi_probe_owner.getornull(p_gi_probe);
  4638. ERR_FAIL_COND_V(!gi_probe, 0);
  4639. return gi_probe->anisotropy_strength;
  4640. }
  4641. void RasterizerStorageRD::gi_probe_set_interior(RID p_gi_probe, bool p_enable) {
  4642. GIProbe *gi_probe = gi_probe_owner.getornull(p_gi_probe);
  4643. ERR_FAIL_COND(!gi_probe);
  4644. gi_probe->interior = p_enable;
  4645. }
  4646. void RasterizerStorageRD::gi_probe_set_use_two_bounces(RID p_gi_probe, bool p_enable) {
  4647. GIProbe *gi_probe = gi_probe_owner.getornull(p_gi_probe);
  4648. ERR_FAIL_COND(!gi_probe);
  4649. gi_probe->use_two_bounces = p_enable;
  4650. gi_probe->version++;
  4651. }
  4652. bool RasterizerStorageRD::gi_probe_is_using_two_bounces(RID p_gi_probe) const {
  4653. GIProbe *gi_probe = gi_probe_owner.getornull(p_gi_probe);
  4654. ERR_FAIL_COND_V(!gi_probe, false);
  4655. return gi_probe->use_two_bounces;
  4656. }
  4657. bool RasterizerStorageRD::gi_probe_is_interior(RID p_gi_probe) const {
  4658. GIProbe *gi_probe = gi_probe_owner.getornull(p_gi_probe);
  4659. ERR_FAIL_COND_V(!gi_probe, 0);
  4660. return gi_probe->interior;
  4661. }
  4662. uint32_t RasterizerStorageRD::gi_probe_get_version(RID p_gi_probe) {
  4663. GIProbe *gi_probe = gi_probe_owner.getornull(p_gi_probe);
  4664. ERR_FAIL_COND_V(!gi_probe, 0);
  4665. return gi_probe->version;
  4666. }
  4667. uint32_t RasterizerStorageRD::gi_probe_get_data_version(RID p_gi_probe) {
  4668. GIProbe *gi_probe = gi_probe_owner.getornull(p_gi_probe);
  4669. ERR_FAIL_COND_V(!gi_probe, 0);
  4670. return gi_probe->data_version;
  4671. }
  4672. RID RasterizerStorageRD::gi_probe_get_octree_buffer(RID p_gi_probe) const {
  4673. GIProbe *gi_probe = gi_probe_owner.getornull(p_gi_probe);
  4674. ERR_FAIL_COND_V(!gi_probe, RID());
  4675. return gi_probe->octree_buffer;
  4676. }
  4677. RID RasterizerStorageRD::gi_probe_get_data_buffer(RID p_gi_probe) const {
  4678. GIProbe *gi_probe = gi_probe_owner.getornull(p_gi_probe);
  4679. ERR_FAIL_COND_V(!gi_probe, RID());
  4680. return gi_probe->data_buffer;
  4681. }
  4682. RID RasterizerStorageRD::gi_probe_get_sdf_texture(RID p_gi_probe) {
  4683. GIProbe *gi_probe = gi_probe_owner.getornull(p_gi_probe);
  4684. ERR_FAIL_COND_V(!gi_probe, RID());
  4685. return gi_probe->sdf_texture;
  4686. }
  4687. /* LIGHTMAP API */
  4688. RID RasterizerStorageRD::lightmap_create() {
  4689. return lightmap_owner.make_rid(Lightmap());
  4690. }
  4691. void RasterizerStorageRD::lightmap_set_textures(RID p_lightmap, RID p_light, bool p_uses_spherical_haromics) {
  4692. Lightmap *lm = lightmap_owner.getornull(p_lightmap);
  4693. ERR_FAIL_COND(!lm);
  4694. lightmap_array_version++;
  4695. //erase lightmap users
  4696. if (lm->light_texture.is_valid()) {
  4697. Texture *t = texture_owner.getornull(lm->light_texture);
  4698. if (t) {
  4699. t->lightmap_users.erase(p_lightmap);
  4700. }
  4701. }
  4702. Texture *t = texture_owner.getornull(p_light);
  4703. lm->light_texture = p_light;
  4704. lm->uses_spherical_harmonics = p_uses_spherical_haromics;
  4705. RID default_2d_array = default_rd_textures[DEFAULT_RD_TEXTURE_2D_ARRAY_WHITE];
  4706. if (!t) {
  4707. if (using_lightmap_array) {
  4708. if (lm->array_index >= 0) {
  4709. lightmap_textures.write[lm->array_index] = default_2d_array;
  4710. lm->array_index = -1;
  4711. }
  4712. }
  4713. return;
  4714. }
  4715. t->lightmap_users.insert(p_lightmap);
  4716. if (using_lightmap_array) {
  4717. if (lm->array_index < 0) {
  4718. //not in array, try to put in array
  4719. for (int i = 0; i < lightmap_textures.size(); i++) {
  4720. if (lightmap_textures[i] == default_2d_array) {
  4721. lm->array_index = i;
  4722. break;
  4723. }
  4724. }
  4725. }
  4726. ERR_FAIL_COND_MSG(lm->array_index < 0, "Maximum amount of lightmaps in use (" + itos(lightmap_textures.size()) + ") has been exceeded, lightmap will nod display properly.");
  4727. lightmap_textures.write[lm->array_index] = t->rd_texture;
  4728. }
  4729. }
  4730. void RasterizerStorageRD::lightmap_set_probe_bounds(RID p_lightmap, const AABB &p_bounds) {
  4731. Lightmap *lm = lightmap_owner.getornull(p_lightmap);
  4732. ERR_FAIL_COND(!lm);
  4733. lm->bounds = p_bounds;
  4734. }
  4735. void RasterizerStorageRD::lightmap_set_probe_interior(RID p_lightmap, bool p_interior) {
  4736. Lightmap *lm = lightmap_owner.getornull(p_lightmap);
  4737. ERR_FAIL_COND(!lm);
  4738. lm->interior = p_interior;
  4739. }
  4740. void RasterizerStorageRD::lightmap_set_probe_capture_data(RID p_lightmap, const PackedVector3Array &p_points, const PackedColorArray &p_point_sh, const PackedInt32Array &p_tetrahedra, const PackedInt32Array &p_bsp_tree) {
  4741. Lightmap *lm = lightmap_owner.getornull(p_lightmap);
  4742. ERR_FAIL_COND(!lm);
  4743. if (p_points.size()) {
  4744. ERR_FAIL_COND(p_points.size() * 9 != p_point_sh.size());
  4745. ERR_FAIL_COND((p_tetrahedra.size() % 4) != 0);
  4746. ERR_FAIL_COND((p_bsp_tree.size() % 6) != 0);
  4747. }
  4748. lm->points = p_points;
  4749. lm->bsp_tree = p_bsp_tree;
  4750. lm->point_sh = p_point_sh;
  4751. lm->tetrahedra = p_tetrahedra;
  4752. }
  4753. PackedVector3Array RasterizerStorageRD::lightmap_get_probe_capture_points(RID p_lightmap) const {
  4754. Lightmap *lm = lightmap_owner.getornull(p_lightmap);
  4755. ERR_FAIL_COND_V(!lm, PackedVector3Array());
  4756. return lm->points;
  4757. }
  4758. PackedColorArray RasterizerStorageRD::lightmap_get_probe_capture_sh(RID p_lightmap) const {
  4759. Lightmap *lm = lightmap_owner.getornull(p_lightmap);
  4760. ERR_FAIL_COND_V(!lm, PackedColorArray());
  4761. return lm->point_sh;
  4762. }
  4763. PackedInt32Array RasterizerStorageRD::lightmap_get_probe_capture_tetrahedra(RID p_lightmap) const {
  4764. Lightmap *lm = lightmap_owner.getornull(p_lightmap);
  4765. ERR_FAIL_COND_V(!lm, PackedInt32Array());
  4766. return lm->tetrahedra;
  4767. }
  4768. PackedInt32Array RasterizerStorageRD::lightmap_get_probe_capture_bsp_tree(RID p_lightmap) const {
  4769. Lightmap *lm = lightmap_owner.getornull(p_lightmap);
  4770. ERR_FAIL_COND_V(!lm, PackedInt32Array());
  4771. return lm->bsp_tree;
  4772. }
  4773. void RasterizerStorageRD::lightmap_set_probe_capture_update_speed(float p_speed) {
  4774. lightmap_probe_capture_update_speed = p_speed;
  4775. }
  4776. void RasterizerStorageRD::lightmap_tap_sh_light(RID p_lightmap, const Vector3 &p_point, Color *r_sh) {
  4777. Lightmap *lm = lightmap_owner.getornull(p_lightmap);
  4778. ERR_FAIL_COND(!lm);
  4779. for (int i = 0; i < 9; i++) {
  4780. r_sh[i] = Color(0, 0, 0, 0);
  4781. }
  4782. if (!lm->points.size() || !lm->bsp_tree.size() || !lm->tetrahedra.size()) {
  4783. return;
  4784. }
  4785. static_assert(sizeof(Lightmap::BSP) == 24);
  4786. const Lightmap::BSP *bsp = (const Lightmap::BSP *)lm->bsp_tree.ptr();
  4787. int32_t node = 0;
  4788. while (node >= 0) {
  4789. if (Plane(bsp[node].plane[0], bsp[node].plane[1], bsp[node].plane[2], bsp[node].plane[3]).is_point_over(p_point)) {
  4790. #ifdef DEBUG_ENABLED
  4791. ERR_FAIL_COND(bsp[node].over >= 0 && bsp[node].over < node);
  4792. #endif
  4793. node = bsp[node].over;
  4794. } else {
  4795. #ifdef DEBUG_ENABLED
  4796. ERR_FAIL_COND(bsp[node].under >= 0 && bsp[node].under < node);
  4797. #endif
  4798. node = bsp[node].under;
  4799. }
  4800. }
  4801. if (node == Lightmap::BSP::EMPTY_LEAF) {
  4802. return; //nothing could be done
  4803. }
  4804. node = ABS(node) - 1;
  4805. uint32_t *tetrahedron = (uint32_t *)&lm->tetrahedra[node * 4];
  4806. Vector3 points[4] = { lm->points[tetrahedron[0]], lm->points[tetrahedron[1]], lm->points[tetrahedron[2]], lm->points[tetrahedron[3]] };
  4807. const Color *sh_colors[4]{ &lm->point_sh[tetrahedron[0] * 9], &lm->point_sh[tetrahedron[1] * 9], &lm->point_sh[tetrahedron[2] * 9], &lm->point_sh[tetrahedron[3] * 9] };
  4808. Color barycentric = Geometry3D::tetrahedron_get_barycentric_coords(points[0], points[1], points[2], points[3], p_point);
  4809. for (int i = 0; i < 4; i++) {
  4810. float c = CLAMP(barycentric[i], 0.0, 1.0);
  4811. for (int j = 0; j < 9; j++) {
  4812. r_sh[j] += sh_colors[i][j] * c;
  4813. }
  4814. }
  4815. }
  4816. bool RasterizerStorageRD::lightmap_is_interior(RID p_lightmap) const {
  4817. const Lightmap *lm = lightmap_owner.getornull(p_lightmap);
  4818. ERR_FAIL_COND_V(!lm, false);
  4819. return lm->interior;
  4820. }
  4821. AABB RasterizerStorageRD::lightmap_get_aabb(RID p_lightmap) const {
  4822. const Lightmap *lm = lightmap_owner.getornull(p_lightmap);
  4823. ERR_FAIL_COND_V(!lm, AABB());
  4824. return lm->bounds;
  4825. }
  4826. /* RENDER TARGET API */
  4827. void RasterizerStorageRD::_clear_render_target(RenderTarget *rt) {
  4828. //free in reverse dependency order
  4829. if (rt->framebuffer.is_valid()) {
  4830. RD::get_singleton()->free(rt->framebuffer);
  4831. }
  4832. if (rt->color.is_valid()) {
  4833. RD::get_singleton()->free(rt->color);
  4834. }
  4835. if (rt->backbuffer.is_valid()) {
  4836. RD::get_singleton()->free(rt->backbuffer);
  4837. rt->backbuffer = RID();
  4838. for (int i = 0; i < rt->backbuffer_mipmaps.size(); i++) {
  4839. //just erase copies, since the rest are erased by dependency
  4840. RD::get_singleton()->free(rt->backbuffer_mipmaps[i].mipmap_copy);
  4841. }
  4842. rt->backbuffer_mipmaps.clear();
  4843. if (rt->backbuffer_uniform_set.is_valid() && RD::get_singleton()->uniform_set_is_valid(rt->backbuffer_uniform_set)) {
  4844. RD::get_singleton()->free(rt->backbuffer_uniform_set);
  4845. }
  4846. rt->backbuffer_uniform_set = RID();
  4847. }
  4848. rt->framebuffer = RID();
  4849. rt->color = RID();
  4850. }
  4851. void RasterizerStorageRD::_update_render_target(RenderTarget *rt) {
  4852. if (rt->texture.is_null()) {
  4853. //create a placeholder until updated
  4854. rt->texture = texture_2d_placeholder_create();
  4855. Texture *tex = texture_owner.getornull(rt->texture);
  4856. tex->is_render_target = true;
  4857. }
  4858. _clear_render_target(rt);
  4859. if (rt->size.width == 0 || rt->size.height == 0) {
  4860. return;
  4861. }
  4862. //until we implement support for HDR monitors (and render target is attached to screen), this is enough.
  4863. rt->color_format = RD::DATA_FORMAT_R8G8B8A8_UNORM;
  4864. rt->color_format_srgb = RD::DATA_FORMAT_R8G8B8A8_SRGB;
  4865. rt->image_format = rt->flags[RENDER_TARGET_TRANSPARENT] ? Image::FORMAT_RGBA8 : Image::FORMAT_RGB8;
  4866. RD::TextureFormat rd_format;
  4867. RD::TextureView rd_view;
  4868. { //attempt register
  4869. rd_format.format = rt->color_format;
  4870. rd_format.width = rt->size.width;
  4871. rd_format.height = rt->size.height;
  4872. rd_format.depth = 1;
  4873. rd_format.array_layers = 1;
  4874. rd_format.mipmaps = 1;
  4875. rd_format.type = RD::TEXTURE_TYPE_2D;
  4876. rd_format.samples = RD::TEXTURE_SAMPLES_1;
  4877. rd_format.usage_bits = RD::TEXTURE_USAGE_SAMPLING_BIT | RD::TEXTURE_USAGE_COLOR_ATTACHMENT_BIT | RD::TEXTURE_USAGE_CAN_COPY_FROM_BIT;
  4878. rd_format.shareable_formats.push_back(rt->color_format);
  4879. rd_format.shareable_formats.push_back(rt->color_format_srgb);
  4880. }
  4881. rt->color = RD::get_singleton()->texture_create(rd_format, rd_view);
  4882. ERR_FAIL_COND(rt->color.is_null());
  4883. Vector<RID> fb_textures;
  4884. fb_textures.push_back(rt->color);
  4885. rt->framebuffer = RD::get_singleton()->framebuffer_create(fb_textures);
  4886. if (rt->framebuffer.is_null()) {
  4887. _clear_render_target(rt);
  4888. ERR_FAIL_COND(rt->framebuffer.is_null());
  4889. }
  4890. { //update texture
  4891. Texture *tex = texture_owner.getornull(rt->texture);
  4892. //free existing textures
  4893. if (RD::get_singleton()->texture_is_valid(tex->rd_texture)) {
  4894. RD::get_singleton()->free(tex->rd_texture);
  4895. }
  4896. if (RD::get_singleton()->texture_is_valid(tex->rd_texture_srgb)) {
  4897. RD::get_singleton()->free(tex->rd_texture_srgb);
  4898. }
  4899. tex->rd_texture = RID();
  4900. tex->rd_texture_srgb = RID();
  4901. //create shared textures to the color buffer,
  4902. //so transparent can be supported
  4903. RD::TextureView view;
  4904. view.format_override = rt->color_format;
  4905. if (!rt->flags[RENDER_TARGET_TRANSPARENT]) {
  4906. view.swizzle_a = RD::TEXTURE_SWIZZLE_ONE;
  4907. }
  4908. tex->rd_texture = RD::get_singleton()->texture_create_shared(view, rt->color);
  4909. if (rt->color_format_srgb != RD::DATA_FORMAT_MAX) {
  4910. view.format_override = rt->color_format_srgb;
  4911. tex->rd_texture_srgb = RD::get_singleton()->texture_create_shared(view, rt->color);
  4912. }
  4913. tex->rd_view = view;
  4914. tex->width = rt->size.width;
  4915. tex->height = rt->size.height;
  4916. tex->width_2d = rt->size.width;
  4917. tex->height_2d = rt->size.height;
  4918. tex->rd_format = rt->color_format;
  4919. tex->rd_format_srgb = rt->color_format_srgb;
  4920. tex->format = rt->image_format;
  4921. Vector<RID> proxies = tex->proxies; //make a copy, since update may change it
  4922. for (int i = 0; i < proxies.size(); i++) {
  4923. texture_proxy_update(proxies[i], rt->texture);
  4924. }
  4925. }
  4926. }
  4927. void RasterizerStorageRD::_create_render_target_backbuffer(RenderTarget *rt) {
  4928. ERR_FAIL_COND(rt->backbuffer.is_valid());
  4929. uint32_t mipmaps_required = Image::get_image_required_mipmaps(rt->size.width, rt->size.height, Image::FORMAT_RGBA8);
  4930. RD::TextureFormat tf;
  4931. tf.format = rt->color_format;
  4932. tf.width = rt->size.width;
  4933. tf.height = rt->size.height;
  4934. tf.type = RD::TEXTURE_TYPE_2D;
  4935. tf.usage_bits = RD::TEXTURE_USAGE_STORAGE_BIT | RD::TEXTURE_USAGE_SAMPLING_BIT | RD::TEXTURE_USAGE_CAN_COPY_TO_BIT;
  4936. tf.mipmaps = mipmaps_required;
  4937. rt->backbuffer = RD::get_singleton()->texture_create(tf, RD::TextureView());
  4938. rt->backbuffer_mipmap0 = RD::get_singleton()->texture_create_shared_from_slice(RD::TextureView(), rt->backbuffer, 0, 0);
  4939. //create mipmaps
  4940. for (uint32_t i = 1; i < mipmaps_required; i++) {
  4941. RenderTarget::BackbufferMipmap mm;
  4942. {
  4943. mm.mipmap = RD::get_singleton()->texture_create_shared_from_slice(RD::TextureView(), rt->backbuffer, 0, i);
  4944. }
  4945. {
  4946. Size2 mm_size = Image::get_image_mipmap_size(tf.width, tf.height, Image::FORMAT_RGBA8, i);
  4947. RD::TextureFormat mmtf = tf;
  4948. mmtf.width = mm_size.width;
  4949. mmtf.height = mm_size.height;
  4950. mmtf.mipmaps = 1;
  4951. mm.mipmap_copy = RD::get_singleton()->texture_create(mmtf, RD::TextureView());
  4952. }
  4953. rt->backbuffer_mipmaps.push_back(mm);
  4954. }
  4955. }
  4956. RID RasterizerStorageRD::render_target_create() {
  4957. RenderTarget render_target;
  4958. render_target.was_used = false;
  4959. render_target.clear_requested = false;
  4960. for (int i = 0; i < RENDER_TARGET_FLAG_MAX; i++) {
  4961. render_target.flags[i] = false;
  4962. }
  4963. _update_render_target(&render_target);
  4964. return render_target_owner.make_rid(render_target);
  4965. }
  4966. void RasterizerStorageRD::render_target_set_position(RID p_render_target, int p_x, int p_y) {
  4967. //unused for this render target
  4968. }
  4969. void RasterizerStorageRD::render_target_set_size(RID p_render_target, int p_width, int p_height) {
  4970. RenderTarget *rt = render_target_owner.getornull(p_render_target);
  4971. ERR_FAIL_COND(!rt);
  4972. rt->size.x = p_width;
  4973. rt->size.y = p_height;
  4974. _update_render_target(rt);
  4975. }
  4976. RID RasterizerStorageRD::render_target_get_texture(RID p_render_target) {
  4977. RenderTarget *rt = render_target_owner.getornull(p_render_target);
  4978. ERR_FAIL_COND_V(!rt, RID());
  4979. return rt->texture;
  4980. }
  4981. void RasterizerStorageRD::render_target_set_external_texture(RID p_render_target, unsigned int p_texture_id) {
  4982. }
  4983. void RasterizerStorageRD::render_target_set_flag(RID p_render_target, RenderTargetFlags p_flag, bool p_value) {
  4984. RenderTarget *rt = render_target_owner.getornull(p_render_target);
  4985. ERR_FAIL_COND(!rt);
  4986. rt->flags[p_flag] = p_value;
  4987. _update_render_target(rt);
  4988. }
  4989. bool RasterizerStorageRD::render_target_was_used(RID p_render_target) {
  4990. RenderTarget *rt = render_target_owner.getornull(p_render_target);
  4991. ERR_FAIL_COND_V(!rt, false);
  4992. return rt->was_used;
  4993. }
  4994. void RasterizerStorageRD::render_target_set_as_unused(RID p_render_target) {
  4995. RenderTarget *rt = render_target_owner.getornull(p_render_target);
  4996. ERR_FAIL_COND(!rt);
  4997. rt->was_used = false;
  4998. }
  4999. Size2 RasterizerStorageRD::render_target_get_size(RID p_render_target) {
  5000. RenderTarget *rt = render_target_owner.getornull(p_render_target);
  5001. ERR_FAIL_COND_V(!rt, Size2());
  5002. return rt->size;
  5003. }
  5004. RID RasterizerStorageRD::render_target_get_rd_framebuffer(RID p_render_target) {
  5005. RenderTarget *rt = render_target_owner.getornull(p_render_target);
  5006. ERR_FAIL_COND_V(!rt, RID());
  5007. return rt->framebuffer;
  5008. }
  5009. RID RasterizerStorageRD::render_target_get_rd_texture(RID p_render_target) {
  5010. RenderTarget *rt = render_target_owner.getornull(p_render_target);
  5011. ERR_FAIL_COND_V(!rt, RID());
  5012. return rt->color;
  5013. }
  5014. void RasterizerStorageRD::render_target_request_clear(RID p_render_target, const Color &p_clear_color) {
  5015. RenderTarget *rt = render_target_owner.getornull(p_render_target);
  5016. ERR_FAIL_COND(!rt);
  5017. rt->clear_requested = true;
  5018. rt->clear_color = p_clear_color;
  5019. }
  5020. bool RasterizerStorageRD::render_target_is_clear_requested(RID p_render_target) {
  5021. RenderTarget *rt = render_target_owner.getornull(p_render_target);
  5022. ERR_FAIL_COND_V(!rt, false);
  5023. return rt->clear_requested;
  5024. }
  5025. Color RasterizerStorageRD::render_target_get_clear_request_color(RID p_render_target) {
  5026. RenderTarget *rt = render_target_owner.getornull(p_render_target);
  5027. ERR_FAIL_COND_V(!rt, Color());
  5028. return rt->clear_color;
  5029. }
  5030. void RasterizerStorageRD::render_target_disable_clear_request(RID p_render_target) {
  5031. RenderTarget *rt = render_target_owner.getornull(p_render_target);
  5032. ERR_FAIL_COND(!rt);
  5033. rt->clear_requested = false;
  5034. }
  5035. void RasterizerStorageRD::render_target_do_clear_request(RID p_render_target) {
  5036. RenderTarget *rt = render_target_owner.getornull(p_render_target);
  5037. ERR_FAIL_COND(!rt);
  5038. if (!rt->clear_requested) {
  5039. return;
  5040. }
  5041. Vector<Color> clear_colors;
  5042. clear_colors.push_back(rt->clear_color);
  5043. RD::get_singleton()->draw_list_begin(rt->framebuffer, RD::INITIAL_ACTION_CLEAR, RD::FINAL_ACTION_READ, RD::INITIAL_ACTION_KEEP, RD::FINAL_ACTION_DISCARD, clear_colors);
  5044. RD::get_singleton()->draw_list_end();
  5045. rt->clear_requested = false;
  5046. }
  5047. void RasterizerStorageRD::render_target_copy_to_back_buffer(RID p_render_target, const Rect2i &p_region) {
  5048. RenderTarget *rt = render_target_owner.getornull(p_render_target);
  5049. ERR_FAIL_COND(!rt);
  5050. if (!rt->backbuffer.is_valid()) {
  5051. _create_render_target_backbuffer(rt);
  5052. }
  5053. Rect2i region = p_region;
  5054. if (region == Rect2i()) {
  5055. region.size = rt->size;
  5056. }
  5057. //single texture copy for backbuffer
  5058. RD::get_singleton()->texture_copy(rt->color, rt->backbuffer_mipmap0, Vector3(region.position.x, region.position.y, 0), Vector3(region.position.x, region.position.y, 0), Vector3(region.size.x, region.size.y, 1), 0, 0, 0, 0, true);
  5059. //effects.copy(rt->color, rt->backbuffer_fb, blur_region);
  5060. //then mipmap blur
  5061. RID prev_texture = rt->color; //use color, not backbuffer, as bb has mipmaps.
  5062. for (int i = 0; i < rt->backbuffer_mipmaps.size(); i++) {
  5063. region.position.x >>= 1;
  5064. region.position.y >>= 1;
  5065. region.size.x = MAX(1, region.size.x >> 1);
  5066. region.size.y = MAX(1, region.size.y >> 1);
  5067. const RenderTarget::BackbufferMipmap &mm = rt->backbuffer_mipmaps[i];
  5068. effects.gaussian_blur(prev_texture, mm.mipmap, mm.mipmap_copy, region, true);
  5069. prev_texture = mm.mipmap;
  5070. }
  5071. }
  5072. RID RasterizerStorageRD::render_target_get_back_buffer_uniform_set(RID p_render_target, RID p_base_shader) {
  5073. RenderTarget *rt = render_target_owner.getornull(p_render_target);
  5074. ERR_FAIL_COND_V(!rt, RID());
  5075. if (!rt->backbuffer.is_valid()) {
  5076. _create_render_target_backbuffer(rt);
  5077. }
  5078. if (rt->backbuffer_uniform_set.is_valid() && RD::get_singleton()->uniform_set_is_valid(rt->backbuffer_uniform_set)) {
  5079. return rt->backbuffer_uniform_set; //if still valid, return/reuse it.
  5080. }
  5081. //create otherwise
  5082. Vector<RD::Uniform> uniforms;
  5083. RD::Uniform u;
  5084. u.type = RD::UNIFORM_TYPE_TEXTURE;
  5085. u.binding = 0;
  5086. u.ids.push_back(rt->backbuffer);
  5087. uniforms.push_back(u);
  5088. rt->backbuffer_uniform_set = RD::get_singleton()->uniform_set_create(uniforms, p_base_shader, 3);
  5089. ERR_FAIL_COND_V(!rt->backbuffer_uniform_set.is_valid(), RID());
  5090. return rt->backbuffer_uniform_set;
  5091. }
  5092. void RasterizerStorageRD::base_update_dependency(RID p_base, RasterizerScene::InstanceBase *p_instance) {
  5093. if (mesh_owner.owns(p_base)) {
  5094. Mesh *mesh = mesh_owner.getornull(p_base);
  5095. p_instance->update_dependency(&mesh->instance_dependency);
  5096. } else if (multimesh_owner.owns(p_base)) {
  5097. MultiMesh *multimesh = multimesh_owner.getornull(p_base);
  5098. p_instance->update_dependency(&multimesh->instance_dependency);
  5099. if (multimesh->mesh.is_valid()) {
  5100. base_update_dependency(multimesh->mesh, p_instance);
  5101. }
  5102. } else if (reflection_probe_owner.owns(p_base)) {
  5103. ReflectionProbe *rp = reflection_probe_owner.getornull(p_base);
  5104. p_instance->update_dependency(&rp->instance_dependency);
  5105. } else if (decal_owner.owns(p_base)) {
  5106. Decal *decal = decal_owner.getornull(p_base);
  5107. p_instance->update_dependency(&decal->instance_dependency);
  5108. } else if (gi_probe_owner.owns(p_base)) {
  5109. GIProbe *gip = gi_probe_owner.getornull(p_base);
  5110. p_instance->update_dependency(&gip->instance_dependency);
  5111. } else if (lightmap_owner.owns(p_base)) {
  5112. Lightmap *lm = lightmap_owner.getornull(p_base);
  5113. p_instance->update_dependency(&lm->instance_dependency);
  5114. } else if (light_owner.owns(p_base)) {
  5115. Light *l = light_owner.getornull(p_base);
  5116. p_instance->update_dependency(&l->instance_dependency);
  5117. } else if (particles_owner.owns(p_base)) {
  5118. Particles *p = particles_owner.getornull(p_base);
  5119. p_instance->update_dependency(&p->instance_dependency);
  5120. } else if (particles_collision_owner.owns(p_base)) {
  5121. ParticlesCollision *pc = particles_collision_owner.getornull(p_base);
  5122. p_instance->update_dependency(&pc->instance_dependency);
  5123. }
  5124. }
  5125. void RasterizerStorageRD::skeleton_update_dependency(RID p_skeleton, RasterizerScene::InstanceBase *p_instance) {
  5126. Skeleton *skeleton = skeleton_owner.getornull(p_skeleton);
  5127. ERR_FAIL_COND(!skeleton);
  5128. p_instance->update_dependency(&skeleton->instance_dependency);
  5129. }
  5130. RS::InstanceType RasterizerStorageRD::get_base_type(RID p_rid) const {
  5131. if (mesh_owner.owns(p_rid)) {
  5132. return RS::INSTANCE_MESH;
  5133. }
  5134. if (multimesh_owner.owns(p_rid)) {
  5135. return RS::INSTANCE_MULTIMESH;
  5136. }
  5137. if (reflection_probe_owner.owns(p_rid)) {
  5138. return RS::INSTANCE_REFLECTION_PROBE;
  5139. }
  5140. if (decal_owner.owns(p_rid)) {
  5141. return RS::INSTANCE_DECAL;
  5142. }
  5143. if (gi_probe_owner.owns(p_rid)) {
  5144. return RS::INSTANCE_GI_PROBE;
  5145. }
  5146. if (light_owner.owns(p_rid)) {
  5147. return RS::INSTANCE_LIGHT;
  5148. }
  5149. if (lightmap_owner.owns(p_rid)) {
  5150. return RS::INSTANCE_LIGHTMAP;
  5151. }
  5152. if (particles_owner.owns(p_rid)) {
  5153. return RS::INSTANCE_PARTICLES;
  5154. }
  5155. if (particles_collision_owner.owns(p_rid)) {
  5156. return RS::INSTANCE_PARTICLES_COLLISION;
  5157. }
  5158. return RS::INSTANCE_NONE;
  5159. }
  5160. void RasterizerStorageRD::texture_add_to_decal_atlas(RID p_texture, bool p_panorama_to_dp) {
  5161. if (!decal_atlas.textures.has(p_texture)) {
  5162. DecalAtlas::Texture t;
  5163. t.users = 1;
  5164. t.panorama_to_dp_users = p_panorama_to_dp ? 1 : 0;
  5165. decal_atlas.textures[p_texture] = t;
  5166. decal_atlas.dirty = true;
  5167. } else {
  5168. DecalAtlas::Texture *t = decal_atlas.textures.getptr(p_texture);
  5169. t->users++;
  5170. if (p_panorama_to_dp) {
  5171. t->panorama_to_dp_users++;
  5172. }
  5173. }
  5174. }
  5175. void RasterizerStorageRD::texture_remove_from_decal_atlas(RID p_texture, bool p_panorama_to_dp) {
  5176. DecalAtlas::Texture *t = decal_atlas.textures.getptr(p_texture);
  5177. ERR_FAIL_COND(!t);
  5178. t->users--;
  5179. if (p_panorama_to_dp) {
  5180. ERR_FAIL_COND(t->panorama_to_dp_users == 0);
  5181. t->panorama_to_dp_users--;
  5182. }
  5183. if (t->users == 0) {
  5184. decal_atlas.textures.erase(p_texture);
  5185. //do not mark it dirty, there is no need to since it remains working
  5186. }
  5187. }
  5188. RID RasterizerStorageRD::decal_atlas_get_texture() const {
  5189. return decal_atlas.texture;
  5190. }
  5191. RID RasterizerStorageRD::decal_atlas_get_texture_srgb() const {
  5192. return decal_atlas.texture_srgb;
  5193. }
  5194. void RasterizerStorageRD::_update_decal_atlas() {
  5195. if (!decal_atlas.dirty) {
  5196. return; //nothing to do
  5197. }
  5198. decal_atlas.dirty = false;
  5199. if (decal_atlas.texture.is_valid()) {
  5200. RD::get_singleton()->free(decal_atlas.texture);
  5201. decal_atlas.texture = RID();
  5202. decal_atlas.texture_srgb = RID();
  5203. decal_atlas.texture_mipmaps.clear();
  5204. }
  5205. int border = 1 << decal_atlas.mipmaps;
  5206. if (decal_atlas.textures.size()) {
  5207. //generate atlas
  5208. Vector<DecalAtlas::SortItem> itemsv;
  5209. itemsv.resize(decal_atlas.textures.size());
  5210. int base_size = 8;
  5211. const RID *K = nullptr;
  5212. int idx = 0;
  5213. while ((K = decal_atlas.textures.next(K))) {
  5214. DecalAtlas::SortItem &si = itemsv.write[idx];
  5215. Texture *src_tex = texture_owner.getornull(*K);
  5216. si.size.width = (src_tex->width / border) + 1;
  5217. si.size.height = (src_tex->height / border) + 1;
  5218. si.pixel_size = Size2i(src_tex->width, src_tex->height);
  5219. if (base_size < si.size.width) {
  5220. base_size = nearest_power_of_2_templated(si.size.width);
  5221. }
  5222. si.texture = *K;
  5223. idx++;
  5224. }
  5225. //sort items by size
  5226. itemsv.sort();
  5227. //attempt to create atlas
  5228. int item_count = itemsv.size();
  5229. DecalAtlas::SortItem *items = itemsv.ptrw();
  5230. int atlas_height = 0;
  5231. while (true) {
  5232. Vector<int> v_offsetsv;
  5233. v_offsetsv.resize(base_size);
  5234. int *v_offsets = v_offsetsv.ptrw();
  5235. zeromem(v_offsets, sizeof(int) * base_size);
  5236. int max_height = 0;
  5237. for (int i = 0; i < item_count; i++) {
  5238. //best fit
  5239. DecalAtlas::SortItem &si = items[i];
  5240. int best_idx = -1;
  5241. int best_height = 0x7FFFFFFF;
  5242. for (int j = 0; j <= base_size - si.size.width; j++) {
  5243. int height = 0;
  5244. for (int k = 0; k < si.size.width; k++) {
  5245. int h = v_offsets[k + j];
  5246. if (h > height) {
  5247. height = h;
  5248. if (height > best_height) {
  5249. break; //already bad
  5250. }
  5251. }
  5252. }
  5253. if (height < best_height) {
  5254. best_height = height;
  5255. best_idx = j;
  5256. }
  5257. }
  5258. //update
  5259. for (int k = 0; k < si.size.width; k++) {
  5260. v_offsets[k + best_idx] = best_height + si.size.height;
  5261. }
  5262. si.pos.x = best_idx;
  5263. si.pos.y = best_height;
  5264. if (si.pos.y + si.size.height > max_height) {
  5265. max_height = si.pos.y + si.size.height;
  5266. }
  5267. }
  5268. if (max_height <= base_size * 2) {
  5269. atlas_height = max_height;
  5270. break; //good ratio, break;
  5271. }
  5272. base_size *= 2;
  5273. }
  5274. decal_atlas.size.width = base_size * border;
  5275. decal_atlas.size.height = nearest_power_of_2_templated(atlas_height * border);
  5276. for (int i = 0; i < item_count; i++) {
  5277. DecalAtlas::Texture *t = decal_atlas.textures.getptr(items[i].texture);
  5278. t->uv_rect.position = items[i].pos * border + Vector2i(border / 2, border / 2);
  5279. t->uv_rect.size = items[i].pixel_size;
  5280. t->uv_rect.position /= Size2(decal_atlas.size);
  5281. t->uv_rect.size /= Size2(decal_atlas.size);
  5282. }
  5283. } else {
  5284. //use border as size, so it at least has enough mipmaps
  5285. decal_atlas.size.width = border;
  5286. decal_atlas.size.height = border;
  5287. }
  5288. //blit textures
  5289. RD::TextureFormat tformat;
  5290. tformat.format = RD::DATA_FORMAT_R8G8B8A8_UNORM;
  5291. tformat.width = decal_atlas.size.width;
  5292. tformat.height = decal_atlas.size.height;
  5293. tformat.usage_bits = RD::TEXTURE_USAGE_SAMPLING_BIT | RD::TEXTURE_USAGE_COLOR_ATTACHMENT_BIT | RD::TEXTURE_USAGE_CAN_COPY_TO_BIT;
  5294. tformat.type = RD::TEXTURE_TYPE_2D;
  5295. tformat.mipmaps = decal_atlas.mipmaps;
  5296. tformat.shareable_formats.push_back(RD::DATA_FORMAT_R8G8B8A8_UNORM);
  5297. tformat.shareable_formats.push_back(RD::DATA_FORMAT_R8G8B8A8_SRGB);
  5298. decal_atlas.texture = RD::get_singleton()->texture_create(tformat, RD::TextureView());
  5299. {
  5300. //create the framebuffer
  5301. Size2i s = decal_atlas.size;
  5302. for (int i = 0; i < decal_atlas.mipmaps; i++) {
  5303. DecalAtlas::MipMap mm;
  5304. mm.texture = RD::get_singleton()->texture_create_shared_from_slice(RD::TextureView(), decal_atlas.texture, 0, i);
  5305. Vector<RID> fb;
  5306. fb.push_back(mm.texture);
  5307. mm.fb = RD::get_singleton()->framebuffer_create(fb);
  5308. mm.size = s;
  5309. decal_atlas.texture_mipmaps.push_back(mm);
  5310. s.width = MAX(1, s.width >> 1);
  5311. s.height = MAX(1, s.height >> 1);
  5312. }
  5313. {
  5314. //create the SRGB variant
  5315. RD::TextureView rd_view;
  5316. rd_view.format_override = RD::DATA_FORMAT_R8G8B8A8_SRGB;
  5317. decal_atlas.texture_srgb = RD::get_singleton()->texture_create_shared(rd_view, decal_atlas.texture);
  5318. }
  5319. }
  5320. RID prev_texture;
  5321. for (int i = 0; i < decal_atlas.texture_mipmaps.size(); i++) {
  5322. const DecalAtlas::MipMap &mm = decal_atlas.texture_mipmaps[i];
  5323. Color clear_color(0, 0, 0, 0);
  5324. if (decal_atlas.textures.size()) {
  5325. if (i == 0) {
  5326. Vector<Color> cc;
  5327. cc.push_back(clear_color);
  5328. RD::DrawListID draw_list = RD::get_singleton()->draw_list_begin(mm.fb, RD::INITIAL_ACTION_CLEAR, RD::FINAL_ACTION_READ, RD::INITIAL_ACTION_DROP, RD::FINAL_ACTION_DISCARD, cc);
  5329. const RID *K = nullptr;
  5330. while ((K = decal_atlas.textures.next(K))) {
  5331. DecalAtlas::Texture *t = decal_atlas.textures.getptr(*K);
  5332. Texture *src_tex = texture_owner.getornull(*K);
  5333. effects.copy_to_atlas_fb(src_tex->rd_texture, mm.fb, t->uv_rect, draw_list, false, t->panorama_to_dp_users > 0);
  5334. }
  5335. RD::get_singleton()->draw_list_end();
  5336. prev_texture = mm.texture;
  5337. } else {
  5338. effects.copy_to_fb_rect(prev_texture, mm.fb, Rect2i(Point2i(), mm.size));
  5339. prev_texture = mm.texture;
  5340. }
  5341. } else {
  5342. RD::get_singleton()->texture_clear(mm.texture, clear_color, 0, 1, 0, 1, false);
  5343. }
  5344. }
  5345. }
  5346. int32_t RasterizerStorageRD::_global_variable_allocate(uint32_t p_elements) {
  5347. int32_t idx = 0;
  5348. while (idx + p_elements <= global_variables.buffer_size) {
  5349. if (global_variables.buffer_usage[idx].elements == 0) {
  5350. bool valid = true;
  5351. for (uint32_t i = 1; i < p_elements; i++) {
  5352. if (global_variables.buffer_usage[idx + i].elements > 0) {
  5353. valid = false;
  5354. idx += i + global_variables.buffer_usage[idx + i].elements;
  5355. break;
  5356. }
  5357. }
  5358. if (!valid) {
  5359. continue; //if not valid, idx is in new position
  5360. }
  5361. return idx;
  5362. } else {
  5363. idx += global_variables.buffer_usage[idx].elements;
  5364. }
  5365. }
  5366. return -1;
  5367. }
  5368. void RasterizerStorageRD::_global_variable_store_in_buffer(int32_t p_index, RS::GlobalVariableType p_type, const Variant &p_value) {
  5369. switch (p_type) {
  5370. case RS::GLOBAL_VAR_TYPE_BOOL: {
  5371. GlobalVariables::Value &bv = global_variables.buffer_values[p_index];
  5372. bool b = p_value;
  5373. bv.x = b ? 1.0 : 0.0;
  5374. bv.y = 0.0;
  5375. bv.z = 0.0;
  5376. bv.w = 0.0;
  5377. } break;
  5378. case RS::GLOBAL_VAR_TYPE_BVEC2: {
  5379. GlobalVariables::Value &bv = global_variables.buffer_values[p_index];
  5380. uint32_t bvec = p_value;
  5381. bv.x = (bvec & 1) ? 1.0 : 0.0;
  5382. bv.y = (bvec & 2) ? 1.0 : 0.0;
  5383. bv.z = 0.0;
  5384. bv.w = 0.0;
  5385. } break;
  5386. case RS::GLOBAL_VAR_TYPE_BVEC3: {
  5387. GlobalVariables::Value &bv = global_variables.buffer_values[p_index];
  5388. uint32_t bvec = p_value;
  5389. bv.x = (bvec & 1) ? 1.0 : 0.0;
  5390. bv.y = (bvec & 2) ? 1.0 : 0.0;
  5391. bv.z = (bvec & 4) ? 1.0 : 0.0;
  5392. bv.w = 0.0;
  5393. } break;
  5394. case RS::GLOBAL_VAR_TYPE_BVEC4: {
  5395. GlobalVariables::Value &bv = global_variables.buffer_values[p_index];
  5396. uint32_t bvec = p_value;
  5397. bv.x = (bvec & 1) ? 1.0 : 0.0;
  5398. bv.y = (bvec & 2) ? 1.0 : 0.0;
  5399. bv.z = (bvec & 4) ? 1.0 : 0.0;
  5400. bv.w = (bvec & 8) ? 1.0 : 0.0;
  5401. } break;
  5402. case RS::GLOBAL_VAR_TYPE_INT: {
  5403. GlobalVariables::ValueInt &bv = *(GlobalVariables::ValueInt *)&global_variables.buffer_values[p_index];
  5404. int32_t v = p_value;
  5405. bv.x = v;
  5406. bv.y = 0;
  5407. bv.z = 0;
  5408. bv.w = 0;
  5409. } break;
  5410. case RS::GLOBAL_VAR_TYPE_IVEC2: {
  5411. GlobalVariables::ValueInt &bv = *(GlobalVariables::ValueInt *)&global_variables.buffer_values[p_index];
  5412. Vector2i v = p_value;
  5413. bv.x = v.x;
  5414. bv.y = v.y;
  5415. bv.z = 0;
  5416. bv.w = 0;
  5417. } break;
  5418. case RS::GLOBAL_VAR_TYPE_IVEC3: {
  5419. GlobalVariables::ValueInt &bv = *(GlobalVariables::ValueInt *)&global_variables.buffer_values[p_index];
  5420. Vector3i v = p_value;
  5421. bv.x = v.x;
  5422. bv.y = v.y;
  5423. bv.z = v.z;
  5424. bv.w = 0;
  5425. } break;
  5426. case RS::GLOBAL_VAR_TYPE_IVEC4: {
  5427. GlobalVariables::ValueInt &bv = *(GlobalVariables::ValueInt *)&global_variables.buffer_values[p_index];
  5428. Vector<int32_t> v = p_value;
  5429. bv.x = v.size() >= 1 ? v[0] : 0;
  5430. bv.y = v.size() >= 2 ? v[1] : 0;
  5431. bv.z = v.size() >= 3 ? v[2] : 0;
  5432. bv.w = v.size() >= 4 ? v[3] : 0;
  5433. } break;
  5434. case RS::GLOBAL_VAR_TYPE_RECT2I: {
  5435. GlobalVariables::ValueInt &bv = *(GlobalVariables::ValueInt *)&global_variables.buffer_values[p_index];
  5436. Rect2i v = p_value;
  5437. bv.x = v.position.x;
  5438. bv.y = v.position.y;
  5439. bv.z = v.size.x;
  5440. bv.w = v.size.y;
  5441. } break;
  5442. case RS::GLOBAL_VAR_TYPE_UINT: {
  5443. GlobalVariables::ValueUInt &bv = *(GlobalVariables::ValueUInt *)&global_variables.buffer_values[p_index];
  5444. uint32_t v = p_value;
  5445. bv.x = v;
  5446. bv.y = 0;
  5447. bv.z = 0;
  5448. bv.w = 0;
  5449. } break;
  5450. case RS::GLOBAL_VAR_TYPE_UVEC2: {
  5451. GlobalVariables::ValueUInt &bv = *(GlobalVariables::ValueUInt *)&global_variables.buffer_values[p_index];
  5452. Vector2i v = p_value;
  5453. bv.x = v.x;
  5454. bv.y = v.y;
  5455. bv.z = 0;
  5456. bv.w = 0;
  5457. } break;
  5458. case RS::GLOBAL_VAR_TYPE_UVEC3: {
  5459. GlobalVariables::ValueUInt &bv = *(GlobalVariables::ValueUInt *)&global_variables.buffer_values[p_index];
  5460. Vector3i v = p_value;
  5461. bv.x = v.x;
  5462. bv.y = v.y;
  5463. bv.z = v.z;
  5464. bv.w = 0;
  5465. } break;
  5466. case RS::GLOBAL_VAR_TYPE_UVEC4: {
  5467. GlobalVariables::ValueUInt &bv = *(GlobalVariables::ValueUInt *)&global_variables.buffer_values[p_index];
  5468. Vector<int32_t> v = p_value;
  5469. bv.x = v.size() >= 1 ? v[0] : 0;
  5470. bv.y = v.size() >= 2 ? v[1] : 0;
  5471. bv.z = v.size() >= 3 ? v[2] : 0;
  5472. bv.w = v.size() >= 4 ? v[3] : 0;
  5473. } break;
  5474. case RS::GLOBAL_VAR_TYPE_FLOAT: {
  5475. GlobalVariables::Value &bv = global_variables.buffer_values[p_index];
  5476. float v = p_value;
  5477. bv.x = v;
  5478. bv.y = 0;
  5479. bv.z = 0;
  5480. bv.w = 0;
  5481. } break;
  5482. case RS::GLOBAL_VAR_TYPE_VEC2: {
  5483. GlobalVariables::Value &bv = global_variables.buffer_values[p_index];
  5484. Vector2 v = p_value;
  5485. bv.x = v.x;
  5486. bv.y = v.y;
  5487. bv.z = 0;
  5488. bv.w = 0;
  5489. } break;
  5490. case RS::GLOBAL_VAR_TYPE_VEC3: {
  5491. GlobalVariables::Value &bv = global_variables.buffer_values[p_index];
  5492. Vector3 v = p_value;
  5493. bv.x = v.x;
  5494. bv.y = v.y;
  5495. bv.z = v.z;
  5496. bv.w = 0;
  5497. } break;
  5498. case RS::GLOBAL_VAR_TYPE_VEC4: {
  5499. GlobalVariables::Value &bv = global_variables.buffer_values[p_index];
  5500. Plane v = p_value;
  5501. bv.x = v.normal.x;
  5502. bv.y = v.normal.y;
  5503. bv.z = v.normal.z;
  5504. bv.w = v.d;
  5505. } break;
  5506. case RS::GLOBAL_VAR_TYPE_COLOR: {
  5507. GlobalVariables::Value &bv = global_variables.buffer_values[p_index];
  5508. Color v = p_value;
  5509. bv.x = v.r;
  5510. bv.y = v.g;
  5511. bv.z = v.b;
  5512. bv.w = v.a;
  5513. GlobalVariables::Value &bv_linear = global_variables.buffer_values[p_index + 1];
  5514. v = v.to_linear();
  5515. bv_linear.x = v.r;
  5516. bv_linear.y = v.g;
  5517. bv_linear.z = v.b;
  5518. bv_linear.w = v.a;
  5519. } break;
  5520. case RS::GLOBAL_VAR_TYPE_RECT2: {
  5521. GlobalVariables::Value &bv = global_variables.buffer_values[p_index];
  5522. Rect2 v = p_value;
  5523. bv.x = v.position.x;
  5524. bv.y = v.position.y;
  5525. bv.z = v.size.x;
  5526. bv.w = v.size.y;
  5527. } break;
  5528. case RS::GLOBAL_VAR_TYPE_MAT2: {
  5529. GlobalVariables::Value *bv = &global_variables.buffer_values[p_index];
  5530. Vector<float> m2 = p_value;
  5531. if (m2.size() < 4) {
  5532. m2.resize(4);
  5533. }
  5534. bv[0].x = m2[0];
  5535. bv[0].y = m2[1];
  5536. bv[0].z = 0;
  5537. bv[0].w = 0;
  5538. bv[1].x = m2[2];
  5539. bv[1].y = m2[3];
  5540. bv[1].z = 0;
  5541. bv[1].w = 0;
  5542. } break;
  5543. case RS::GLOBAL_VAR_TYPE_MAT3: {
  5544. GlobalVariables::Value *bv = &global_variables.buffer_values[p_index];
  5545. Basis v = p_value;
  5546. bv[0].x = v.elements[0][0];
  5547. bv[0].y = v.elements[1][0];
  5548. bv[0].z = v.elements[2][0];
  5549. bv[0].w = 0;
  5550. bv[1].x = v.elements[0][1];
  5551. bv[1].y = v.elements[1][1];
  5552. bv[1].z = v.elements[2][1];
  5553. bv[1].w = 0;
  5554. bv[2].x = v.elements[0][2];
  5555. bv[2].y = v.elements[1][2];
  5556. bv[2].z = v.elements[2][2];
  5557. bv[2].w = 0;
  5558. } break;
  5559. case RS::GLOBAL_VAR_TYPE_MAT4: {
  5560. GlobalVariables::Value *bv = &global_variables.buffer_values[p_index];
  5561. Vector<float> m2 = p_value;
  5562. if (m2.size() < 16) {
  5563. m2.resize(16);
  5564. }
  5565. bv[0].x = m2[0];
  5566. bv[0].y = m2[1];
  5567. bv[0].z = m2[2];
  5568. bv[0].w = m2[3];
  5569. bv[1].x = m2[4];
  5570. bv[1].y = m2[5];
  5571. bv[1].z = m2[6];
  5572. bv[1].w = m2[7];
  5573. bv[2].x = m2[8];
  5574. bv[2].y = m2[9];
  5575. bv[2].z = m2[10];
  5576. bv[2].w = m2[11];
  5577. bv[3].x = m2[12];
  5578. bv[3].y = m2[13];
  5579. bv[3].z = m2[14];
  5580. bv[3].w = m2[15];
  5581. } break;
  5582. case RS::GLOBAL_VAR_TYPE_TRANSFORM_2D: {
  5583. GlobalVariables::Value *bv = &global_variables.buffer_values[p_index];
  5584. Transform2D v = p_value;
  5585. bv[0].x = v.elements[0][0];
  5586. bv[0].y = v.elements[0][1];
  5587. bv[0].z = 0;
  5588. bv[0].w = 0;
  5589. bv[1].x = v.elements[1][0];
  5590. bv[1].y = v.elements[1][1];
  5591. bv[1].z = 0;
  5592. bv[1].w = 0;
  5593. bv[2].x = v.elements[2][0];
  5594. bv[2].y = v.elements[2][1];
  5595. bv[2].z = 1;
  5596. bv[2].w = 0;
  5597. } break;
  5598. case RS::GLOBAL_VAR_TYPE_TRANSFORM: {
  5599. GlobalVariables::Value *bv = &global_variables.buffer_values[p_index];
  5600. Transform v = p_value;
  5601. bv[0].x = v.basis.elements[0][0];
  5602. bv[0].y = v.basis.elements[1][0];
  5603. bv[0].z = v.basis.elements[2][0];
  5604. bv[0].w = 0;
  5605. bv[1].x = v.basis.elements[0][1];
  5606. bv[1].y = v.basis.elements[1][1];
  5607. bv[1].z = v.basis.elements[2][1];
  5608. bv[1].w = 0;
  5609. bv[2].x = v.basis.elements[0][2];
  5610. bv[2].y = v.basis.elements[1][2];
  5611. bv[2].z = v.basis.elements[2][2];
  5612. bv[2].w = 0;
  5613. bv[3].x = v.origin.x;
  5614. bv[3].y = v.origin.y;
  5615. bv[3].z = v.origin.z;
  5616. bv[3].w = 1;
  5617. } break;
  5618. default: {
  5619. ERR_FAIL();
  5620. }
  5621. }
  5622. }
  5623. void RasterizerStorageRD::_global_variable_mark_buffer_dirty(int32_t p_index, int32_t p_elements) {
  5624. int32_t prev_chunk = -1;
  5625. for (int32_t i = 0; i < p_elements; i++) {
  5626. int32_t chunk = (p_index + i) / GlobalVariables::BUFFER_DIRTY_REGION_SIZE;
  5627. if (chunk != prev_chunk) {
  5628. if (!global_variables.buffer_dirty_regions[chunk]) {
  5629. global_variables.buffer_dirty_regions[chunk] = true;
  5630. global_variables.buffer_dirty_region_count++;
  5631. }
  5632. }
  5633. prev_chunk = chunk;
  5634. }
  5635. }
  5636. void RasterizerStorageRD::global_variable_add(const StringName &p_name, RS::GlobalVariableType p_type, const Variant &p_value) {
  5637. ERR_FAIL_COND(global_variables.variables.has(p_name));
  5638. GlobalVariables::Variable gv;
  5639. gv.type = p_type;
  5640. gv.value = p_value;
  5641. gv.buffer_index = -1;
  5642. if (p_type >= RS::GLOBAL_VAR_TYPE_SAMPLER2D) {
  5643. //is texture
  5644. global_variables.must_update_texture_materials = true; //normally there are none
  5645. } else {
  5646. gv.buffer_elements = 1;
  5647. if (p_type == RS::GLOBAL_VAR_TYPE_COLOR || p_type == RS::GLOBAL_VAR_TYPE_MAT2) {
  5648. //color needs to elements to store srgb and linear
  5649. gv.buffer_elements = 2;
  5650. }
  5651. if (p_type == RS::GLOBAL_VAR_TYPE_MAT3 || p_type == RS::GLOBAL_VAR_TYPE_TRANSFORM_2D) {
  5652. //color needs to elements to store srgb and linear
  5653. gv.buffer_elements = 3;
  5654. }
  5655. if (p_type == RS::GLOBAL_VAR_TYPE_MAT4 || p_type == RS::GLOBAL_VAR_TYPE_TRANSFORM) {
  5656. //color needs to elements to store srgb and linear
  5657. gv.buffer_elements = 4;
  5658. }
  5659. //is vector, allocate in buffer and update index
  5660. gv.buffer_index = _global_variable_allocate(gv.buffer_elements);
  5661. ERR_FAIL_COND_MSG(gv.buffer_index < 0, vformat("Failed allocating global variable '%s' out of buffer memory. Consider increasing it in the Project Settings.", String(p_name)));
  5662. global_variables.buffer_usage[gv.buffer_index].elements = gv.buffer_elements;
  5663. _global_variable_store_in_buffer(gv.buffer_index, gv.type, gv.value);
  5664. _global_variable_mark_buffer_dirty(gv.buffer_index, gv.buffer_elements);
  5665. global_variables.must_update_buffer_materials = true; //normally there are none
  5666. }
  5667. global_variables.variables[p_name] = gv;
  5668. }
  5669. void RasterizerStorageRD::global_variable_remove(const StringName &p_name) {
  5670. if (!global_variables.variables.has(p_name)) {
  5671. return;
  5672. }
  5673. GlobalVariables::Variable &gv = global_variables.variables[p_name];
  5674. if (gv.buffer_index >= 0) {
  5675. global_variables.buffer_usage[gv.buffer_index].elements = 0;
  5676. global_variables.must_update_buffer_materials = true;
  5677. } else {
  5678. global_variables.must_update_texture_materials = true;
  5679. }
  5680. global_variables.variables.erase(p_name);
  5681. }
  5682. Vector<StringName> RasterizerStorageRD::global_variable_get_list() const {
  5683. if (!Engine::get_singleton()->is_editor_hint()) {
  5684. ERR_FAIL_V_MSG(Vector<StringName>(), "This function should never be used outside the editor, it can severely damage performance.");
  5685. }
  5686. const StringName *K = nullptr;
  5687. Vector<StringName> names;
  5688. while ((K = global_variables.variables.next(K))) {
  5689. names.push_back(*K);
  5690. }
  5691. names.sort_custom<StringName::AlphCompare>();
  5692. return names;
  5693. }
  5694. void RasterizerStorageRD::global_variable_set(const StringName &p_name, const Variant &p_value) {
  5695. ERR_FAIL_COND(!global_variables.variables.has(p_name));
  5696. GlobalVariables::Variable &gv = global_variables.variables[p_name];
  5697. gv.value = p_value;
  5698. if (gv.override.get_type() == Variant::NIL) {
  5699. if (gv.buffer_index >= 0) {
  5700. //buffer
  5701. _global_variable_store_in_buffer(gv.buffer_index, gv.type, gv.value);
  5702. _global_variable_mark_buffer_dirty(gv.buffer_index, gv.buffer_elements);
  5703. } else {
  5704. //texture
  5705. for (Set<RID>::Element *E = gv.texture_materials.front(); E; E = E->next()) {
  5706. Material *material = material_owner.getornull(E->get());
  5707. ERR_CONTINUE(!material);
  5708. _material_queue_update(material, false, true);
  5709. }
  5710. }
  5711. }
  5712. }
  5713. void RasterizerStorageRD::global_variable_set_override(const StringName &p_name, const Variant &p_value) {
  5714. if (!global_variables.variables.has(p_name)) {
  5715. return; //variable may not exist
  5716. }
  5717. GlobalVariables::Variable &gv = global_variables.variables[p_name];
  5718. gv.override = p_value;
  5719. if (gv.buffer_index >= 0) {
  5720. //buffer
  5721. if (gv.override.get_type() == Variant::NIL) {
  5722. _global_variable_store_in_buffer(gv.buffer_index, gv.type, gv.value);
  5723. } else {
  5724. _global_variable_store_in_buffer(gv.buffer_index, gv.type, gv.override);
  5725. }
  5726. _global_variable_mark_buffer_dirty(gv.buffer_index, gv.buffer_elements);
  5727. } else {
  5728. //texture
  5729. //texture
  5730. for (Set<RID>::Element *E = gv.texture_materials.front(); E; E = E->next()) {
  5731. Material *material = material_owner.getornull(E->get());
  5732. ERR_CONTINUE(!material);
  5733. _material_queue_update(material, false, true);
  5734. }
  5735. }
  5736. }
  5737. Variant RasterizerStorageRD::global_variable_get(const StringName &p_name) const {
  5738. if (!Engine::get_singleton()->is_editor_hint()) {
  5739. ERR_FAIL_V_MSG(Variant(), "This function should never be used outside the editor, it can severely damage performance.");
  5740. }
  5741. if (!global_variables.variables.has(p_name)) {
  5742. return Variant();
  5743. }
  5744. return global_variables.variables[p_name].value;
  5745. }
  5746. RS::GlobalVariableType RasterizerStorageRD::global_variable_get_type_internal(const StringName &p_name) const {
  5747. if (!global_variables.variables.has(p_name)) {
  5748. return RS::GLOBAL_VAR_TYPE_MAX;
  5749. }
  5750. return global_variables.variables[p_name].type;
  5751. }
  5752. RS::GlobalVariableType RasterizerStorageRD::global_variable_get_type(const StringName &p_name) const {
  5753. if (!Engine::get_singleton()->is_editor_hint()) {
  5754. ERR_FAIL_V_MSG(RS::GLOBAL_VAR_TYPE_MAX, "This function should never be used outside the editor, it can severely damage performance.");
  5755. }
  5756. return global_variable_get_type_internal(p_name);
  5757. }
  5758. void RasterizerStorageRD::global_variables_load_settings(bool p_load_textures) {
  5759. List<PropertyInfo> settings;
  5760. ProjectSettings::get_singleton()->get_property_list(&settings);
  5761. for (List<PropertyInfo>::Element *E = settings.front(); E; E = E->next()) {
  5762. if (E->get().name.begins_with("shader_globals/")) {
  5763. StringName name = E->get().name.get_slice("/", 1);
  5764. Dictionary d = ProjectSettings::get_singleton()->get(E->get().name);
  5765. ERR_CONTINUE(!d.has("type"));
  5766. ERR_CONTINUE(!d.has("value"));
  5767. String type = d["type"];
  5768. static const char *global_var_type_names[RS::GLOBAL_VAR_TYPE_MAX] = {
  5769. "bool",
  5770. "bvec2",
  5771. "bvec3",
  5772. "bvec4",
  5773. "int",
  5774. "ivec2",
  5775. "ivec3",
  5776. "ivec4",
  5777. "rect2i",
  5778. "uint",
  5779. "uvec2",
  5780. "uvec3",
  5781. "uvec4",
  5782. "float",
  5783. "vec2",
  5784. "vec3",
  5785. "vec4",
  5786. "color",
  5787. "rect2",
  5788. "mat2",
  5789. "mat3",
  5790. "mat4",
  5791. "transform_2d",
  5792. "transform",
  5793. "sampler2D",
  5794. "sampler2DArray",
  5795. "sampler3D",
  5796. "samplerCube",
  5797. };
  5798. RS::GlobalVariableType gvtype = RS::GLOBAL_VAR_TYPE_MAX;
  5799. for (int i = 0; i < RS::GLOBAL_VAR_TYPE_MAX; i++) {
  5800. if (global_var_type_names[i] == type) {
  5801. gvtype = RS::GlobalVariableType(i);
  5802. break;
  5803. }
  5804. }
  5805. ERR_CONTINUE(gvtype == RS::GLOBAL_VAR_TYPE_MAX); //type invalid
  5806. Variant value = d["value"];
  5807. if (gvtype >= RS::GLOBAL_VAR_TYPE_SAMPLER2D) {
  5808. //textire
  5809. if (!p_load_textures) {
  5810. value = RID();
  5811. continue;
  5812. }
  5813. String path = value;
  5814. RES resource = ResourceLoader::load(path);
  5815. ERR_CONTINUE(resource.is_null());
  5816. value = resource;
  5817. }
  5818. if (global_variables.variables.has(name)) {
  5819. //has it, update it
  5820. global_variable_set(name, value);
  5821. } else {
  5822. global_variable_add(name, gvtype, value);
  5823. }
  5824. }
  5825. }
  5826. }
  5827. void RasterizerStorageRD::global_variables_clear() {
  5828. global_variables.variables.clear(); //not right but for now enough
  5829. }
  5830. RID RasterizerStorageRD::global_variables_get_storage_buffer() const {
  5831. return global_variables.buffer;
  5832. }
  5833. int32_t RasterizerStorageRD::global_variables_instance_allocate(RID p_instance) {
  5834. ERR_FAIL_COND_V(global_variables.instance_buffer_pos.has(p_instance), -1);
  5835. int32_t pos = _global_variable_allocate(ShaderLanguage::MAX_INSTANCE_UNIFORM_INDICES);
  5836. global_variables.instance_buffer_pos[p_instance] = pos; //save anyway
  5837. ERR_FAIL_COND_V_MSG(pos < 0, -1, "Too many instances using shader instance variables. Increase buffer size in Project Settings.");
  5838. global_variables.buffer_usage[pos].elements = ShaderLanguage::MAX_INSTANCE_UNIFORM_INDICES;
  5839. return pos;
  5840. }
  5841. void RasterizerStorageRD::global_variables_instance_free(RID p_instance) {
  5842. ERR_FAIL_COND(!global_variables.instance_buffer_pos.has(p_instance));
  5843. int32_t pos = global_variables.instance_buffer_pos[p_instance];
  5844. if (pos >= 0) {
  5845. global_variables.buffer_usage[pos].elements = 0;
  5846. }
  5847. global_variables.instance_buffer_pos.erase(p_instance);
  5848. }
  5849. void RasterizerStorageRD::global_variables_instance_update(RID p_instance, int p_index, const Variant &p_value) {
  5850. if (!global_variables.instance_buffer_pos.has(p_instance)) {
  5851. return; //just not allocated, ignore
  5852. }
  5853. int32_t pos = global_variables.instance_buffer_pos[p_instance];
  5854. if (pos < 0) {
  5855. return; //again, not allocated, ignore
  5856. }
  5857. ERR_FAIL_INDEX(p_index, ShaderLanguage::MAX_INSTANCE_UNIFORM_INDICES);
  5858. ERR_FAIL_COND_MSG(p_value.get_type() > Variant::COLOR, "Unsupported variant type for instance parameter: " + Variant::get_type_name(p_value.get_type())); //anything greater not supported
  5859. ShaderLanguage::DataType datatype_from_value[Variant::COLOR + 1] = {
  5860. ShaderLanguage::TYPE_MAX, //nil
  5861. ShaderLanguage::TYPE_BOOL, //bool
  5862. ShaderLanguage::TYPE_INT, //int
  5863. ShaderLanguage::TYPE_FLOAT, //float
  5864. ShaderLanguage::TYPE_MAX, //string
  5865. ShaderLanguage::TYPE_VEC2, //vec2
  5866. ShaderLanguage::TYPE_IVEC2, //vec2i
  5867. ShaderLanguage::TYPE_VEC4, //rect2
  5868. ShaderLanguage::TYPE_IVEC4, //rect2i
  5869. ShaderLanguage::TYPE_VEC3, // vec3
  5870. ShaderLanguage::TYPE_IVEC3, //vec3i
  5871. ShaderLanguage::TYPE_MAX, //xform2d not supported here
  5872. ShaderLanguage::TYPE_VEC4, //plane
  5873. ShaderLanguage::TYPE_VEC4, //quat
  5874. ShaderLanguage::TYPE_MAX, //aabb not supported here
  5875. ShaderLanguage::TYPE_MAX, //basis not supported here
  5876. ShaderLanguage::TYPE_MAX, //xform not supported here
  5877. ShaderLanguage::TYPE_VEC4 //color
  5878. };
  5879. ShaderLanguage::DataType datatype = datatype_from_value[p_value.get_type()];
  5880. ERR_FAIL_COND_MSG(datatype == ShaderLanguage::TYPE_MAX, "Unsupported variant type for instance parameter: " + Variant::get_type_name(p_value.get_type())); //anything greater not supported
  5881. pos += p_index;
  5882. _fill_std140_variant_ubo_value(datatype, p_value, (uint8_t *)&global_variables.buffer_values[pos], true); //instances always use linear color in this renderer
  5883. _global_variable_mark_buffer_dirty(pos, 1);
  5884. }
  5885. void RasterizerStorageRD::_update_global_variables() {
  5886. if (global_variables.buffer_dirty_region_count > 0) {
  5887. uint32_t total_regions = global_variables.buffer_size / GlobalVariables::BUFFER_DIRTY_REGION_SIZE;
  5888. if (total_regions / global_variables.buffer_dirty_region_count <= 4) {
  5889. // 25% of regions dirty, just update all buffer
  5890. RD::get_singleton()->buffer_update(global_variables.buffer, 0, sizeof(GlobalVariables::Value) * global_variables.buffer_size, global_variables.buffer_values);
  5891. zeromem(global_variables.buffer_dirty_regions, sizeof(bool) * total_regions);
  5892. } else {
  5893. uint32_t region_byte_size = sizeof(GlobalVariables::Value) * GlobalVariables::BUFFER_DIRTY_REGION_SIZE;
  5894. for (uint32_t i = 0; i < total_regions; i++) {
  5895. if (global_variables.buffer_dirty_regions[i]) {
  5896. RD::get_singleton()->buffer_update(global_variables.buffer, i * region_byte_size, region_byte_size, global_variables.buffer_values);
  5897. global_variables.buffer_dirty_regions[i] = false;
  5898. }
  5899. }
  5900. }
  5901. global_variables.buffer_dirty_region_count = 0;
  5902. }
  5903. if (global_variables.must_update_buffer_materials) {
  5904. // only happens in the case of a buffer variable added or removed,
  5905. // so not often.
  5906. for (List<RID>::Element *E = global_variables.materials_using_buffer.front(); E; E = E->next()) {
  5907. Material *material = material_owner.getornull(E->get());
  5908. ERR_CONTINUE(!material); //wtf
  5909. _material_queue_update(material, true, false);
  5910. }
  5911. global_variables.must_update_buffer_materials = false;
  5912. }
  5913. if (global_variables.must_update_texture_materials) {
  5914. // only happens in the case of a buffer variable added or removed,
  5915. // so not often.
  5916. for (List<RID>::Element *E = global_variables.materials_using_texture.front(); E; E = E->next()) {
  5917. Material *material = material_owner.getornull(E->get());
  5918. ERR_CONTINUE(!material); //wtf
  5919. _material_queue_update(material, false, true);
  5920. print_line("update material texture?");
  5921. }
  5922. global_variables.must_update_texture_materials = false;
  5923. }
  5924. }
  5925. void RasterizerStorageRD::update_dirty_resources() {
  5926. _update_global_variables(); //must do before materials, so it can queue them for update
  5927. _update_queued_materials();
  5928. _update_dirty_multimeshes();
  5929. _update_dirty_skeletons();
  5930. _update_decal_atlas();
  5931. }
  5932. bool RasterizerStorageRD::has_os_feature(const String &p_feature) const {
  5933. if (p_feature == "rgtc" && RD::get_singleton()->texture_is_format_supported_for_usage(RD::DATA_FORMAT_BC5_UNORM_BLOCK, RD::TEXTURE_USAGE_SAMPLING_BIT)) {
  5934. return true;
  5935. }
  5936. if (p_feature == "s3tc" && RD::get_singleton()->texture_is_format_supported_for_usage(RD::DATA_FORMAT_BC1_RGB_UNORM_BLOCK, RD::TEXTURE_USAGE_SAMPLING_BIT)) {
  5937. return true;
  5938. }
  5939. if (p_feature == "bptc" && RD::get_singleton()->texture_is_format_supported_for_usage(RD::DATA_FORMAT_BC7_UNORM_BLOCK, RD::TEXTURE_USAGE_SAMPLING_BIT)) {
  5940. return true;
  5941. }
  5942. if ((p_feature == "etc" || p_feature == "etc2") && RD::get_singleton()->texture_is_format_supported_for_usage(RD::DATA_FORMAT_ETC2_R8G8B8_UNORM_BLOCK, RD::TEXTURE_USAGE_SAMPLING_BIT)) {
  5943. return true;
  5944. }
  5945. if (p_feature == "pvrtc" && RD::get_singleton()->texture_is_format_supported_for_usage(RD::DATA_FORMAT_PVRTC1_2BPP_UNORM_BLOCK_IMG, RD::TEXTURE_USAGE_SAMPLING_BIT)) {
  5946. return true;
  5947. }
  5948. return false;
  5949. }
  5950. bool RasterizerStorageRD::free(RID p_rid) {
  5951. if (texture_owner.owns(p_rid)) {
  5952. Texture *t = texture_owner.getornull(p_rid);
  5953. ERR_FAIL_COND_V(t->is_render_target, false);
  5954. if (RD::get_singleton()->texture_is_valid(t->rd_texture_srgb)) {
  5955. //erase this first, as it's a dependency of the one below
  5956. RD::get_singleton()->free(t->rd_texture_srgb);
  5957. }
  5958. if (RD::get_singleton()->texture_is_valid(t->rd_texture)) {
  5959. RD::get_singleton()->free(t->rd_texture);
  5960. }
  5961. if (t->is_proxy && t->proxy_to.is_valid()) {
  5962. Texture *proxy_to = texture_owner.getornull(t->proxy_to);
  5963. if (proxy_to) {
  5964. proxy_to->proxies.erase(p_rid);
  5965. }
  5966. }
  5967. if (decal_atlas.textures.has(p_rid)) {
  5968. decal_atlas.textures.erase(p_rid);
  5969. //there is not much a point of making it dirty, just let it be.
  5970. }
  5971. for (int i = 0; i < t->proxies.size(); i++) {
  5972. Texture *p = texture_owner.getornull(t->proxies[i]);
  5973. ERR_CONTINUE(!p);
  5974. p->proxy_to = RID();
  5975. p->rd_texture = RID();
  5976. p->rd_texture_srgb = RID();
  5977. }
  5978. texture_owner.free(p_rid);
  5979. } else if (shader_owner.owns(p_rid)) {
  5980. Shader *shader = shader_owner.getornull(p_rid);
  5981. //make material unreference this
  5982. while (shader->owners.size()) {
  5983. material_set_shader(shader->owners.front()->get()->self, RID());
  5984. }
  5985. //clear data if exists
  5986. if (shader->data) {
  5987. memdelete(shader->data);
  5988. }
  5989. shader_owner.free(p_rid);
  5990. } else if (material_owner.owns(p_rid)) {
  5991. Material *material = material_owner.getornull(p_rid);
  5992. if (material->update_requested) {
  5993. _update_queued_materials();
  5994. }
  5995. material_set_shader(p_rid, RID()); //clean up shader
  5996. material->instance_dependency.instance_notify_deleted(p_rid);
  5997. material_owner.free(p_rid);
  5998. } else if (mesh_owner.owns(p_rid)) {
  5999. mesh_clear(p_rid);
  6000. Mesh *mesh = mesh_owner.getornull(p_rid);
  6001. mesh->instance_dependency.instance_notify_deleted(p_rid);
  6002. mesh_owner.free(p_rid);
  6003. } else if (multimesh_owner.owns(p_rid)) {
  6004. _update_dirty_multimeshes();
  6005. multimesh_allocate(p_rid, 0, RS::MULTIMESH_TRANSFORM_2D);
  6006. MultiMesh *multimesh = multimesh_owner.getornull(p_rid);
  6007. multimesh->instance_dependency.instance_notify_deleted(p_rid);
  6008. multimesh_owner.free(p_rid);
  6009. } else if (skeleton_owner.owns(p_rid)) {
  6010. _update_dirty_skeletons();
  6011. skeleton_allocate(p_rid, 0);
  6012. Skeleton *skeleton = skeleton_owner.getornull(p_rid);
  6013. skeleton->instance_dependency.instance_notify_deleted(p_rid);
  6014. skeleton_owner.free(p_rid);
  6015. } else if (reflection_probe_owner.owns(p_rid)) {
  6016. ReflectionProbe *reflection_probe = reflection_probe_owner.getornull(p_rid);
  6017. reflection_probe->instance_dependency.instance_notify_deleted(p_rid);
  6018. reflection_probe_owner.free(p_rid);
  6019. } else if (decal_owner.owns(p_rid)) {
  6020. Decal *decal = decal_owner.getornull(p_rid);
  6021. for (int i = 0; i < RS::DECAL_TEXTURE_MAX; i++) {
  6022. if (decal->textures[i].is_valid() && texture_owner.owns(decal->textures[i])) {
  6023. texture_remove_from_decal_atlas(decal->textures[i]);
  6024. }
  6025. }
  6026. decal->instance_dependency.instance_notify_deleted(p_rid);
  6027. decal_owner.free(p_rid);
  6028. } else if (gi_probe_owner.owns(p_rid)) {
  6029. gi_probe_allocate(p_rid, Transform(), AABB(), Vector3i(), Vector<uint8_t>(), Vector<uint8_t>(), Vector<uint8_t>(), Vector<int>()); //deallocate
  6030. GIProbe *gi_probe = gi_probe_owner.getornull(p_rid);
  6031. gi_probe->instance_dependency.instance_notify_deleted(p_rid);
  6032. gi_probe_owner.free(p_rid);
  6033. } else if (lightmap_owner.owns(p_rid)) {
  6034. lightmap_set_textures(p_rid, RID(), false);
  6035. Lightmap *lightmap = lightmap_owner.getornull(p_rid);
  6036. lightmap->instance_dependency.instance_notify_deleted(p_rid);
  6037. lightmap_owner.free(p_rid);
  6038. } else if (light_owner.owns(p_rid)) {
  6039. light_set_projector(p_rid, RID()); //clear projector
  6040. // delete the texture
  6041. Light *light = light_owner.getornull(p_rid);
  6042. light->instance_dependency.instance_notify_deleted(p_rid);
  6043. light_owner.free(p_rid);
  6044. } else if (particles_owner.owns(p_rid)) {
  6045. Particles *particles = particles_owner.getornull(p_rid);
  6046. _particles_free_data(particles);
  6047. particles->instance_dependency.instance_notify_deleted(p_rid);
  6048. particles_owner.free(p_rid);
  6049. } else if (particles_collision_owner.owns(p_rid)) {
  6050. ParticlesCollision *particles_collision = particles_collision_owner.getornull(p_rid);
  6051. if (particles_collision->heightfield_texture.is_valid()) {
  6052. RD::get_singleton()->free(particles_collision->heightfield_texture);
  6053. }
  6054. particles_collision->instance_dependency.instance_notify_deleted(p_rid);
  6055. particles_collision_owner.free(p_rid);
  6056. } else if (render_target_owner.owns(p_rid)) {
  6057. RenderTarget *rt = render_target_owner.getornull(p_rid);
  6058. _clear_render_target(rt);
  6059. if (rt->texture.is_valid()) {
  6060. Texture *tex = texture_owner.getornull(rt->texture);
  6061. tex->is_render_target = false;
  6062. free(rt->texture);
  6063. }
  6064. render_target_owner.free(p_rid);
  6065. } else {
  6066. return false;
  6067. }
  6068. return true;
  6069. }
  6070. RasterizerEffectsRD *RasterizerStorageRD::get_effects() {
  6071. return &effects;
  6072. }
  6073. void RasterizerStorageRD::capture_timestamps_begin() {
  6074. RD::get_singleton()->capture_timestamp("Frame Begin", false);
  6075. }
  6076. void RasterizerStorageRD::capture_timestamp(const String &p_name) {
  6077. RD::get_singleton()->capture_timestamp(p_name, true);
  6078. }
  6079. uint32_t RasterizerStorageRD::get_captured_timestamps_count() const {
  6080. return RD::get_singleton()->get_captured_timestamps_count();
  6081. }
  6082. uint64_t RasterizerStorageRD::get_captured_timestamps_frame() const {
  6083. return RD::get_singleton()->get_captured_timestamps_frame();
  6084. }
  6085. uint64_t RasterizerStorageRD::get_captured_timestamp_gpu_time(uint32_t p_index) const {
  6086. return RD::get_singleton()->get_captured_timestamp_gpu_time(p_index);
  6087. }
  6088. uint64_t RasterizerStorageRD::get_captured_timestamp_cpu_time(uint32_t p_index) const {
  6089. return RD::get_singleton()->get_captured_timestamp_cpu_time(p_index);
  6090. }
  6091. String RasterizerStorageRD::get_captured_timestamp_name(uint32_t p_index) const {
  6092. return RD::get_singleton()->get_captured_timestamp_name(p_index);
  6093. }
  6094. RasterizerStorageRD *RasterizerStorageRD::base_singleton = nullptr;
  6095. RasterizerStorageRD::RasterizerStorageRD() {
  6096. base_singleton = this;
  6097. for (int i = 0; i < SHADER_TYPE_MAX; i++) {
  6098. shader_data_request_func[i] = nullptr;
  6099. }
  6100. static_assert(sizeof(GlobalVariables::Value) == 16);
  6101. global_variables.buffer_size = GLOBAL_GET("rendering/high_end/global_shader_variables_buffer_size");
  6102. global_variables.buffer_size = MAX(4096, global_variables.buffer_size);
  6103. global_variables.buffer_values = memnew_arr(GlobalVariables::Value, global_variables.buffer_size);
  6104. zeromem(global_variables.buffer_values, sizeof(GlobalVariables::Value) * global_variables.buffer_size);
  6105. global_variables.buffer_usage = memnew_arr(GlobalVariables::ValueUsage, global_variables.buffer_size);
  6106. global_variables.buffer_dirty_regions = memnew_arr(bool, global_variables.buffer_size / GlobalVariables::BUFFER_DIRTY_REGION_SIZE);
  6107. zeromem(global_variables.buffer_dirty_regions, sizeof(bool) * global_variables.buffer_size / GlobalVariables::BUFFER_DIRTY_REGION_SIZE);
  6108. global_variables.buffer = RD::get_singleton()->storage_buffer_create(sizeof(GlobalVariables::Value) * global_variables.buffer_size);
  6109. material_update_list = nullptr;
  6110. { //create default textures
  6111. RD::TextureFormat tformat;
  6112. tformat.format = RD::DATA_FORMAT_R8G8B8A8_UNORM;
  6113. tformat.width = 4;
  6114. tformat.height = 4;
  6115. tformat.usage_bits = RD::TEXTURE_USAGE_SAMPLING_BIT | RD::TEXTURE_USAGE_CAN_UPDATE_BIT;
  6116. tformat.type = RD::TEXTURE_TYPE_2D;
  6117. Vector<uint8_t> pv;
  6118. pv.resize(16 * 4);
  6119. for (int i = 0; i < 16; i++) {
  6120. pv.set(i * 4 + 0, 255);
  6121. pv.set(i * 4 + 1, 255);
  6122. pv.set(i * 4 + 2, 255);
  6123. pv.set(i * 4 + 3, 255);
  6124. }
  6125. {
  6126. Vector<Vector<uint8_t>> vpv;
  6127. vpv.push_back(pv);
  6128. default_rd_textures[DEFAULT_RD_TEXTURE_WHITE] = RD::get_singleton()->texture_create(tformat, RD::TextureView(), vpv);
  6129. }
  6130. for (int i = 0; i < 16; i++) {
  6131. pv.set(i * 4 + 0, 0);
  6132. pv.set(i * 4 + 1, 0);
  6133. pv.set(i * 4 + 2, 0);
  6134. pv.set(i * 4 + 3, 255);
  6135. }
  6136. {
  6137. Vector<Vector<uint8_t>> vpv;
  6138. vpv.push_back(pv);
  6139. default_rd_textures[DEFAULT_RD_TEXTURE_BLACK] = RD::get_singleton()->texture_create(tformat, RD::TextureView(), vpv);
  6140. //take the chance and initialize decal atlas to something
  6141. decal_atlas.texture = RD::get_singleton()->texture_create(tformat, RD::TextureView(), vpv);
  6142. decal_atlas.texture_srgb = decal_atlas.texture;
  6143. }
  6144. for (int i = 0; i < 16; i++) {
  6145. pv.set(i * 4 + 0, 128);
  6146. pv.set(i * 4 + 1, 128);
  6147. pv.set(i * 4 + 2, 255);
  6148. pv.set(i * 4 + 3, 255);
  6149. }
  6150. {
  6151. Vector<Vector<uint8_t>> vpv;
  6152. vpv.push_back(pv);
  6153. default_rd_textures[DEFAULT_RD_TEXTURE_NORMAL] = RD::get_singleton()->texture_create(tformat, RD::TextureView(), vpv);
  6154. }
  6155. for (int i = 0; i < 16; i++) {
  6156. pv.set(i * 4 + 0, 255);
  6157. pv.set(i * 4 + 1, 128);
  6158. pv.set(i * 4 + 2, 255);
  6159. pv.set(i * 4 + 3, 255);
  6160. }
  6161. {
  6162. Vector<Vector<uint8_t>> vpv;
  6163. vpv.push_back(pv);
  6164. default_rd_textures[DEFAULT_RD_TEXTURE_ANISO] = RD::get_singleton()->texture_create(tformat, RD::TextureView(), vpv);
  6165. }
  6166. for (int i = 0; i < 16; i++) {
  6167. pv.set(i * 4 + 0, 0);
  6168. pv.set(i * 4 + 1, 0);
  6169. pv.set(i * 4 + 2, 0);
  6170. pv.set(i * 4 + 3, 0);
  6171. }
  6172. default_rd_textures[DEFAULT_RD_TEXTURE_MULTIMESH_BUFFER] = RD::get_singleton()->texture_buffer_create(16, RD::DATA_FORMAT_R8G8B8A8_UNORM, pv);
  6173. for (int i = 0; i < 16; i++) {
  6174. pv.set(i * 4 + 0, 0);
  6175. pv.set(i * 4 + 1, 0);
  6176. pv.set(i * 4 + 2, 0);
  6177. pv.set(i * 4 + 3, 0);
  6178. }
  6179. {
  6180. tformat.format = RD::DATA_FORMAT_R8G8B8A8_UINT;
  6181. Vector<Vector<uint8_t>> vpv;
  6182. vpv.push_back(pv);
  6183. default_rd_textures[DEFAULT_RD_TEXTURE_2D_UINT] = RD::get_singleton()->texture_create(tformat, RD::TextureView(), vpv);
  6184. }
  6185. }
  6186. { //create default cubemap
  6187. RD::TextureFormat tformat;
  6188. tformat.format = RD::DATA_FORMAT_R8G8B8A8_UNORM;
  6189. tformat.width = 4;
  6190. tformat.height = 4;
  6191. tformat.array_layers = 6;
  6192. tformat.usage_bits = RD::TEXTURE_USAGE_SAMPLING_BIT | RD::TEXTURE_USAGE_CAN_UPDATE_BIT;
  6193. tformat.type = RD::TEXTURE_TYPE_CUBE_ARRAY;
  6194. Vector<uint8_t> pv;
  6195. pv.resize(16 * 4);
  6196. for (int i = 0; i < 16; i++) {
  6197. pv.set(i * 4 + 0, 0);
  6198. pv.set(i * 4 + 1, 0);
  6199. pv.set(i * 4 + 2, 0);
  6200. pv.set(i * 4 + 3, 0);
  6201. }
  6202. {
  6203. Vector<Vector<uint8_t>> vpv;
  6204. for (int i = 0; i < 6; i++) {
  6205. vpv.push_back(pv);
  6206. }
  6207. default_rd_textures[DEFAULT_RD_TEXTURE_CUBEMAP_ARRAY_BLACK] = RD::get_singleton()->texture_create(tformat, RD::TextureView(), vpv);
  6208. }
  6209. }
  6210. { //create default cubemap array
  6211. RD::TextureFormat tformat;
  6212. tformat.format = RD::DATA_FORMAT_R8G8B8A8_UNORM;
  6213. tformat.width = 4;
  6214. tformat.height = 4;
  6215. tformat.array_layers = 6;
  6216. tformat.usage_bits = RD::TEXTURE_USAGE_SAMPLING_BIT | RD::TEXTURE_USAGE_CAN_UPDATE_BIT;
  6217. tformat.type = RD::TEXTURE_TYPE_CUBE;
  6218. Vector<uint8_t> pv;
  6219. pv.resize(16 * 4);
  6220. for (int i = 0; i < 16; i++) {
  6221. pv.set(i * 4 + 0, 0);
  6222. pv.set(i * 4 + 1, 0);
  6223. pv.set(i * 4 + 2, 0);
  6224. pv.set(i * 4 + 3, 0);
  6225. }
  6226. {
  6227. Vector<Vector<uint8_t>> vpv;
  6228. for (int i = 0; i < 6; i++) {
  6229. vpv.push_back(pv);
  6230. }
  6231. default_rd_textures[DEFAULT_RD_TEXTURE_CUBEMAP_BLACK] = RD::get_singleton()->texture_create(tformat, RD::TextureView(), vpv);
  6232. }
  6233. }
  6234. { //create default 3D
  6235. RD::TextureFormat tformat;
  6236. tformat.format = RD::DATA_FORMAT_R8G8B8A8_UNORM;
  6237. tformat.width = 4;
  6238. tformat.height = 4;
  6239. tformat.depth = 4;
  6240. tformat.usage_bits = RD::TEXTURE_USAGE_SAMPLING_BIT | RD::TEXTURE_USAGE_CAN_UPDATE_BIT;
  6241. tformat.type = RD::TEXTURE_TYPE_3D;
  6242. Vector<uint8_t> pv;
  6243. pv.resize(64 * 4);
  6244. for (int i = 0; i < 64; i++) {
  6245. pv.set(i * 4 + 0, 0);
  6246. pv.set(i * 4 + 1, 0);
  6247. pv.set(i * 4 + 2, 0);
  6248. pv.set(i * 4 + 3, 0);
  6249. }
  6250. {
  6251. Vector<Vector<uint8_t>> vpv;
  6252. vpv.push_back(pv);
  6253. default_rd_textures[DEFAULT_RD_TEXTURE_3D_WHITE] = RD::get_singleton()->texture_create(tformat, RD::TextureView(), vpv);
  6254. }
  6255. }
  6256. { //create default array
  6257. RD::TextureFormat tformat;
  6258. tformat.format = RD::DATA_FORMAT_R8G8B8A8_UNORM;
  6259. tformat.width = 4;
  6260. tformat.height = 4;
  6261. tformat.array_layers = 1;
  6262. tformat.usage_bits = RD::TEXTURE_USAGE_SAMPLING_BIT | RD::TEXTURE_USAGE_CAN_UPDATE_BIT;
  6263. tformat.type = RD::TEXTURE_TYPE_2D_ARRAY;
  6264. Vector<uint8_t> pv;
  6265. pv.resize(16 * 4);
  6266. for (int i = 0; i < 16; i++) {
  6267. pv.set(i * 4 + 0, 255);
  6268. pv.set(i * 4 + 1, 255);
  6269. pv.set(i * 4 + 2, 255);
  6270. pv.set(i * 4 + 3, 255);
  6271. }
  6272. {
  6273. Vector<Vector<uint8_t>> vpv;
  6274. vpv.push_back(pv);
  6275. default_rd_textures[DEFAULT_RD_TEXTURE_2D_ARRAY_WHITE] = RD::get_singleton()->texture_create(tformat, RD::TextureView(), vpv);
  6276. }
  6277. }
  6278. //default samplers
  6279. for (int i = 1; i < RS::CANVAS_ITEM_TEXTURE_FILTER_MAX; i++) {
  6280. for (int j = 1; j < RS::CANVAS_ITEM_TEXTURE_REPEAT_MAX; j++) {
  6281. RD::SamplerState sampler_state;
  6282. switch (i) {
  6283. case RS::CANVAS_ITEM_TEXTURE_FILTER_NEAREST: {
  6284. sampler_state.mag_filter = RD::SAMPLER_FILTER_NEAREST;
  6285. sampler_state.min_filter = RD::SAMPLER_FILTER_NEAREST;
  6286. sampler_state.max_lod = 0;
  6287. } break;
  6288. case RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR: {
  6289. sampler_state.mag_filter = RD::SAMPLER_FILTER_LINEAR;
  6290. sampler_state.min_filter = RD::SAMPLER_FILTER_LINEAR;
  6291. sampler_state.max_lod = 0;
  6292. } break;
  6293. case RS::CANVAS_ITEM_TEXTURE_FILTER_NEAREST_WITH_MIPMAPS: {
  6294. sampler_state.mag_filter = RD::SAMPLER_FILTER_NEAREST;
  6295. sampler_state.min_filter = RD::SAMPLER_FILTER_LINEAR;
  6296. sampler_state.mip_filter = RD::SAMPLER_FILTER_LINEAR;
  6297. } break;
  6298. case RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR_WITH_MIPMAPS: {
  6299. sampler_state.mag_filter = RD::SAMPLER_FILTER_LINEAR;
  6300. sampler_state.min_filter = RD::SAMPLER_FILTER_LINEAR;
  6301. sampler_state.mip_filter = RD::SAMPLER_FILTER_LINEAR;
  6302. } break;
  6303. case RS::CANVAS_ITEM_TEXTURE_FILTER_NEAREST_WITH_MIPMAPS_ANISOTROPIC: {
  6304. sampler_state.mag_filter = RD::SAMPLER_FILTER_NEAREST;
  6305. sampler_state.min_filter = RD::SAMPLER_FILTER_LINEAR;
  6306. sampler_state.mip_filter = RD::SAMPLER_FILTER_LINEAR;
  6307. sampler_state.use_anisotropy = true;
  6308. sampler_state.anisotropy_max = 1 << int(GLOBAL_GET("rendering/quality/texture_filters/anisotropic_filtering_level"));
  6309. } break;
  6310. case RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR_WITH_MIPMAPS_ANISOTROPIC: {
  6311. sampler_state.mag_filter = RD::SAMPLER_FILTER_LINEAR;
  6312. sampler_state.min_filter = RD::SAMPLER_FILTER_LINEAR;
  6313. sampler_state.mip_filter = RD::SAMPLER_FILTER_LINEAR;
  6314. sampler_state.use_anisotropy = true;
  6315. sampler_state.anisotropy_max = 1 << int(GLOBAL_GET("rendering/quality/texture_filters/anisotropic_filtering_level"));
  6316. } break;
  6317. default: {
  6318. }
  6319. }
  6320. switch (j) {
  6321. case RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED: {
  6322. sampler_state.repeat_u = RD::SAMPLER_REPEAT_MODE_CLAMP_TO_EDGE;
  6323. sampler_state.repeat_v = RD::SAMPLER_REPEAT_MODE_CLAMP_TO_EDGE;
  6324. sampler_state.repeat_w = RD::SAMPLER_REPEAT_MODE_CLAMP_TO_EDGE;
  6325. } break;
  6326. case RS::CANVAS_ITEM_TEXTURE_REPEAT_ENABLED: {
  6327. sampler_state.repeat_u = RD::SAMPLER_REPEAT_MODE_REPEAT;
  6328. sampler_state.repeat_v = RD::SAMPLER_REPEAT_MODE_REPEAT;
  6329. sampler_state.repeat_w = RD::SAMPLER_REPEAT_MODE_REPEAT;
  6330. } break;
  6331. case RS::CANVAS_ITEM_TEXTURE_REPEAT_MIRROR: {
  6332. sampler_state.repeat_u = RD::SAMPLER_REPEAT_MODE_MIRRORED_REPEAT;
  6333. sampler_state.repeat_v = RD::SAMPLER_REPEAT_MODE_MIRRORED_REPEAT;
  6334. sampler_state.repeat_w = RD::SAMPLER_REPEAT_MODE_MIRRORED_REPEAT;
  6335. } break;
  6336. default: {
  6337. }
  6338. }
  6339. default_rd_samplers[i][j] = RD::get_singleton()->sampler_create(sampler_state);
  6340. }
  6341. }
  6342. //default rd buffers
  6343. {
  6344. Vector<uint8_t> buffer;
  6345. {
  6346. buffer.resize(sizeof(float) * 3);
  6347. {
  6348. uint8_t *w = buffer.ptrw();
  6349. float *fptr = (float *)w;
  6350. fptr[0] = 0.0;
  6351. fptr[1] = 0.0;
  6352. fptr[2] = 0.0;
  6353. }
  6354. mesh_default_rd_buffers[DEFAULT_RD_BUFFER_VERTEX] = RD::get_singleton()->vertex_buffer_create(buffer.size(), buffer);
  6355. }
  6356. { //normal
  6357. buffer.resize(sizeof(float) * 3);
  6358. {
  6359. uint8_t *w = buffer.ptrw();
  6360. float *fptr = (float *)w;
  6361. fptr[0] = 1.0;
  6362. fptr[1] = 0.0;
  6363. fptr[2] = 0.0;
  6364. }
  6365. mesh_default_rd_buffers[DEFAULT_RD_BUFFER_NORMAL] = RD::get_singleton()->vertex_buffer_create(buffer.size(), buffer);
  6366. }
  6367. { //tangent
  6368. buffer.resize(sizeof(float) * 4);
  6369. {
  6370. uint8_t *w = buffer.ptrw();
  6371. float *fptr = (float *)w;
  6372. fptr[0] = 1.0;
  6373. fptr[1] = 0.0;
  6374. fptr[2] = 0.0;
  6375. fptr[3] = 0.0;
  6376. }
  6377. mesh_default_rd_buffers[DEFAULT_RD_BUFFER_TANGENT] = RD::get_singleton()->vertex_buffer_create(buffer.size(), buffer);
  6378. }
  6379. { //color
  6380. buffer.resize(sizeof(float) * 4);
  6381. {
  6382. uint8_t *w = buffer.ptrw();
  6383. float *fptr = (float *)w;
  6384. fptr[0] = 1.0;
  6385. fptr[1] = 1.0;
  6386. fptr[2] = 1.0;
  6387. fptr[3] = 1.0;
  6388. }
  6389. mesh_default_rd_buffers[DEFAULT_RD_BUFFER_COLOR] = RD::get_singleton()->vertex_buffer_create(buffer.size(), buffer);
  6390. }
  6391. { //tex uv 1
  6392. buffer.resize(sizeof(float) * 2);
  6393. {
  6394. uint8_t *w = buffer.ptrw();
  6395. float *fptr = (float *)w;
  6396. fptr[0] = 0.0;
  6397. fptr[1] = 0.0;
  6398. }
  6399. mesh_default_rd_buffers[DEFAULT_RD_BUFFER_TEX_UV] = RD::get_singleton()->vertex_buffer_create(buffer.size(), buffer);
  6400. }
  6401. { //tex uv 2
  6402. buffer.resize(sizeof(float) * 2);
  6403. {
  6404. uint8_t *w = buffer.ptrw();
  6405. float *fptr = (float *)w;
  6406. fptr[0] = 0.0;
  6407. fptr[1] = 0.0;
  6408. }
  6409. mesh_default_rd_buffers[DEFAULT_RD_BUFFER_TEX_UV2] = RD::get_singleton()->vertex_buffer_create(buffer.size(), buffer);
  6410. }
  6411. { //bones
  6412. buffer.resize(sizeof(uint32_t) * 4);
  6413. {
  6414. uint8_t *w = buffer.ptrw();
  6415. uint32_t *fptr = (uint32_t *)w;
  6416. fptr[0] = 0;
  6417. fptr[1] = 0;
  6418. fptr[2] = 0;
  6419. fptr[3] = 0;
  6420. }
  6421. mesh_default_rd_buffers[DEFAULT_RD_BUFFER_BONES] = RD::get_singleton()->vertex_buffer_create(buffer.size(), buffer);
  6422. }
  6423. { //weights
  6424. buffer.resize(sizeof(float) * 4);
  6425. {
  6426. uint8_t *w = buffer.ptrw();
  6427. float *fptr = (float *)w;
  6428. fptr[0] = 0.0;
  6429. fptr[1] = 0.0;
  6430. fptr[2] = 0.0;
  6431. fptr[3] = 0.0;
  6432. }
  6433. mesh_default_rd_buffers[DEFAULT_RD_BUFFER_WEIGHTS] = RD::get_singleton()->vertex_buffer_create(buffer.size(), buffer);
  6434. }
  6435. }
  6436. {
  6437. Vector<String> sdf_versions;
  6438. sdf_versions.push_back(""); //one only
  6439. giprobe_sdf_shader.initialize(sdf_versions);
  6440. giprobe_sdf_shader_version = giprobe_sdf_shader.version_create();
  6441. giprobe_sdf_shader.version_set_compute_code(giprobe_sdf_shader_version, "", "", "", Vector<String>());
  6442. giprobe_sdf_shader_version_shader = giprobe_sdf_shader.version_get_shader(giprobe_sdf_shader_version, 0);
  6443. giprobe_sdf_shader_pipeline = RD::get_singleton()->compute_pipeline_create(giprobe_sdf_shader_version_shader);
  6444. }
  6445. using_lightmap_array = true; // high end
  6446. if (using_lightmap_array) {
  6447. uint32_t textures_per_stage = RD::get_singleton()->limit_get(RD::LIMIT_MAX_TEXTURES_PER_SHADER_STAGE);
  6448. if (textures_per_stage <= 256) {
  6449. lightmap_textures.resize(32);
  6450. } else {
  6451. lightmap_textures.resize(1024);
  6452. }
  6453. for (int i = 0; i < lightmap_textures.size(); i++) {
  6454. lightmap_textures.write[i] = default_rd_textures[DEFAULT_RD_TEXTURE_2D_ARRAY_WHITE];
  6455. }
  6456. }
  6457. lightmap_probe_capture_update_speed = GLOBAL_GET("rendering/lightmapper/probe_capture_update_speed");
  6458. /* Particles */
  6459. {
  6460. // Initialize particles
  6461. Vector<String> particles_modes;
  6462. particles_modes.push_back("");
  6463. particles_shader.shader.initialize(particles_modes, String());
  6464. }
  6465. shader_set_data_request_function(RasterizerStorageRD::SHADER_TYPE_PARTICLES, _create_particles_shader_funcs);
  6466. material_set_data_request_function(RasterizerStorageRD::SHADER_TYPE_PARTICLES, _create_particles_material_funcs);
  6467. {
  6468. ShaderCompilerRD::DefaultIdentifierActions actions;
  6469. actions.renames["COLOR"] = "PARTICLE.color";
  6470. actions.renames["VELOCITY"] = "PARTICLE.velocity";
  6471. //actions.renames["MASS"] = "mass"; ?
  6472. actions.renames["ACTIVE"] = "PARTICLE.is_active";
  6473. actions.renames["RESTART"] = "restart";
  6474. actions.renames["CUSTOM"] = "PARTICLE.custom";
  6475. actions.renames["TRANSFORM"] = "PARTICLE.xform";
  6476. actions.renames["TIME"] = "FRAME.time";
  6477. actions.renames["LIFETIME"] = "params.lifetime";
  6478. actions.renames["DELTA"] = "local_delta";
  6479. actions.renames["NUMBER"] = "particle";
  6480. actions.renames["INDEX"] = "index";
  6481. //actions.renames["GRAVITY"] = "current_gravity";
  6482. actions.renames["EMISSION_TRANSFORM"] = "FRAME.emission_transform";
  6483. actions.renames["RANDOM_SEED"] = "FRAME.random_seed";
  6484. actions.renames["FLAG_EMIT_POSITION"] = "EMISSION_FLAG_HAS_POSITION";
  6485. actions.renames["FLAG_EMIT_ROT_SCALE"] = "EMISSION_FLAG_HAS_ROTATION_SCALE";
  6486. actions.renames["FLAG_EMIT_VELOCITY"] = "EMISSION_FLAG_HAS_VELOCITY";
  6487. actions.renames["FLAG_EMIT_COLOR"] = "EMISSION_FLAG_HAS_COLOR";
  6488. actions.renames["FLAG_EMIT_CUSTOM"] = "EMISSION_FLAG_HAS_CUSTOM";
  6489. actions.renames["RESTART_POSITION"] = "restart_position";
  6490. actions.renames["RESTART_ROT_SCALE"] = "restart_rotation_scale";
  6491. actions.renames["RESTART_VELOCITY"] = "restart_velocity";
  6492. actions.renames["RESTART_COLOR"] = "restart_color";
  6493. actions.renames["RESTART_CUSTOM"] = "restart_custom";
  6494. actions.renames["emit_particle"] = "emit_particle";
  6495. actions.renames["COLLIDED"] = "collided";
  6496. actions.renames["COLLISION_NORMAL"] = "collision_normal";
  6497. actions.renames["COLLISION_DEPTH"] = "collision_depth";
  6498. actions.renames["ATTRACTOR_FORCE"] = "attractor_force";
  6499. actions.render_mode_defines["disable_force"] = "#define DISABLE_FORCE\n";
  6500. actions.render_mode_defines["disable_velocity"] = "#define DISABLE_VELOCITY\n";
  6501. actions.render_mode_defines["keep_data"] = "#define ENABLE_KEEP_DATA\n";
  6502. actions.render_mode_defines["collision_use_scale"] = "#define USE_COLLISON_SCALE\n";
  6503. actions.sampler_array_name = "material_samplers";
  6504. actions.base_texture_binding_index = 1;
  6505. actions.texture_layout_set = 3;
  6506. actions.base_uniform_string = "material.";
  6507. actions.base_varying_index = 10;
  6508. actions.default_filter = ShaderLanguage::FILTER_LINEAR_MIPMAP;
  6509. actions.default_repeat = ShaderLanguage::REPEAT_ENABLE;
  6510. actions.global_buffer_array_variable = "global_variables.data";
  6511. particles_shader.compiler.initialize(actions);
  6512. }
  6513. {
  6514. // default material and shader for particles shader
  6515. particles_shader.default_shader = shader_create();
  6516. shader_set_code(particles_shader.default_shader, "shader_type particles; void compute() { COLOR = vec4(1.0); } \n");
  6517. particles_shader.default_material = material_create();
  6518. material_set_shader(particles_shader.default_material, particles_shader.default_shader);
  6519. ParticlesMaterialData *md = (ParticlesMaterialData *)material_get_data(particles_shader.default_material, RasterizerStorageRD::SHADER_TYPE_PARTICLES);
  6520. particles_shader.default_shader_rd = particles_shader.shader.version_get_shader(md->shader_data->version, 0);
  6521. Vector<RD::Uniform> uniforms;
  6522. {
  6523. RD::Uniform u;
  6524. u.type = RD::UNIFORM_TYPE_SAMPLER;
  6525. u.binding = 1;
  6526. u.ids.resize(12);
  6527. RID *ids_ptr = u.ids.ptrw();
  6528. ids_ptr[0] = sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_NEAREST, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED);
  6529. ids_ptr[1] = sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED);
  6530. ids_ptr[2] = sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_NEAREST_WITH_MIPMAPS, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED);
  6531. ids_ptr[3] = sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR_WITH_MIPMAPS, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED);
  6532. ids_ptr[4] = sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_NEAREST_WITH_MIPMAPS_ANISOTROPIC, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED);
  6533. ids_ptr[5] = sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR_WITH_MIPMAPS_ANISOTROPIC, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED);
  6534. ids_ptr[6] = sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_NEAREST, RS::CANVAS_ITEM_TEXTURE_REPEAT_ENABLED);
  6535. ids_ptr[7] = sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR, RS::CANVAS_ITEM_TEXTURE_REPEAT_ENABLED);
  6536. ids_ptr[8] = sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_NEAREST_WITH_MIPMAPS, RS::CANVAS_ITEM_TEXTURE_REPEAT_ENABLED);
  6537. ids_ptr[9] = sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR_WITH_MIPMAPS, RS::CANVAS_ITEM_TEXTURE_REPEAT_ENABLED);
  6538. ids_ptr[10] = sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_NEAREST_WITH_MIPMAPS_ANISOTROPIC, RS::CANVAS_ITEM_TEXTURE_REPEAT_ENABLED);
  6539. ids_ptr[11] = sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR_WITH_MIPMAPS_ANISOTROPIC, RS::CANVAS_ITEM_TEXTURE_REPEAT_ENABLED);
  6540. uniforms.push_back(u);
  6541. }
  6542. {
  6543. RD::Uniform u;
  6544. u.type = RD::UNIFORM_TYPE_STORAGE_BUFFER;
  6545. u.binding = 2;
  6546. u.ids.push_back(global_variables_get_storage_buffer());
  6547. uniforms.push_back(u);
  6548. }
  6549. particles_shader.base_uniform_set = RD::get_singleton()->uniform_set_create(uniforms, particles_shader.default_shader_rd, 0);
  6550. }
  6551. default_rd_storage_buffer = RD::get_singleton()->storage_buffer_create(sizeof(uint32_t) * 4);
  6552. {
  6553. Vector<String> copy_modes;
  6554. copy_modes.push_back("\n#define MODE_FILL_INSTANCES\n");
  6555. copy_modes.push_back("\n#define MODE_FILL_SORT_BUFFER\n#define USE_SORT_BUFFER\n");
  6556. copy_modes.push_back("\n#define MODE_FILL_INSTANCES\n#define USE_SORT_BUFFER\n");
  6557. particles_shader.copy_shader.initialize(copy_modes);
  6558. particles_shader.copy_shader_version = particles_shader.copy_shader.version_create();
  6559. for (int i = 0; i < ParticlesShader::COPY_MODE_MAX; i++) {
  6560. particles_shader.copy_pipelines[i] = RD::get_singleton()->compute_pipeline_create(particles_shader.copy_shader.version_get_shader(particles_shader.copy_shader_version, i));
  6561. }
  6562. }
  6563. }
  6564. RasterizerStorageRD::~RasterizerStorageRD() {
  6565. memdelete_arr(global_variables.buffer_values);
  6566. memdelete_arr(global_variables.buffer_usage);
  6567. memdelete_arr(global_variables.buffer_dirty_regions);
  6568. RD::get_singleton()->free(global_variables.buffer);
  6569. //def textures
  6570. for (int i = 0; i < DEFAULT_RD_TEXTURE_MAX; i++) {
  6571. RD::get_singleton()->free(default_rd_textures[i]);
  6572. }
  6573. //def samplers
  6574. for (int i = 1; i < RS::CANVAS_ITEM_TEXTURE_FILTER_MAX; i++) {
  6575. for (int j = 1; j < RS::CANVAS_ITEM_TEXTURE_REPEAT_MAX; j++) {
  6576. RD::get_singleton()->free(default_rd_samplers[i][j]);
  6577. }
  6578. }
  6579. //def buffers
  6580. for (int i = 0; i < DEFAULT_RD_BUFFER_MAX; i++) {
  6581. RD::get_singleton()->free(mesh_default_rd_buffers[i]);
  6582. }
  6583. giprobe_sdf_shader.version_free(giprobe_sdf_shader_version);
  6584. RD::get_singleton()->free(default_rd_storage_buffer);
  6585. if (decal_atlas.textures.size()) {
  6586. ERR_PRINT("Decal Atlas: " + itos(decal_atlas.textures.size()) + " textures were not removed from the atlas.");
  6587. }
  6588. if (decal_atlas.texture.is_valid()) {
  6589. RD::get_singleton()->free(decal_atlas.texture);
  6590. }
  6591. }