rasterizer_scene_gles3.cpp 214 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379
  1. /**************************************************************************/
  2. /* rasterizer_scene_gles3.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #include "rasterizer_scene_gles3.h"
  31. #include "core/math/math_funcs.h"
  32. #include "core/os/os.h"
  33. #include "core/project_settings.h"
  34. #include "rasterizer_canvas_gles3.h"
  35. #include "servers/camera/camera_feed.h"
  36. #include "servers/visual/visual_server_raster.h"
  37. #ifndef GLES_OVER_GL
  38. #define glClearDepth glClearDepthf
  39. #endif
  40. static const GLenum _cube_side_enum[6] = {
  41. GL_TEXTURE_CUBE_MAP_NEGATIVE_X,
  42. GL_TEXTURE_CUBE_MAP_POSITIVE_X,
  43. GL_TEXTURE_CUBE_MAP_NEGATIVE_Y,
  44. GL_TEXTURE_CUBE_MAP_POSITIVE_Y,
  45. GL_TEXTURE_CUBE_MAP_NEGATIVE_Z,
  46. GL_TEXTURE_CUBE_MAP_POSITIVE_Z,
  47. };
  48. static _FORCE_INLINE_ void store_transform(const Transform &p_mtx, float *p_array) {
  49. p_array[0] = p_mtx.basis.elements[0][0];
  50. p_array[1] = p_mtx.basis.elements[1][0];
  51. p_array[2] = p_mtx.basis.elements[2][0];
  52. p_array[3] = 0;
  53. p_array[4] = p_mtx.basis.elements[0][1];
  54. p_array[5] = p_mtx.basis.elements[1][1];
  55. p_array[6] = p_mtx.basis.elements[2][1];
  56. p_array[7] = 0;
  57. p_array[8] = p_mtx.basis.elements[0][2];
  58. p_array[9] = p_mtx.basis.elements[1][2];
  59. p_array[10] = p_mtx.basis.elements[2][2];
  60. p_array[11] = 0;
  61. p_array[12] = p_mtx.origin.x;
  62. p_array[13] = p_mtx.origin.y;
  63. p_array[14] = p_mtx.origin.z;
  64. p_array[15] = 1;
  65. }
  66. static _FORCE_INLINE_ void store_camera(const CameraMatrix &p_mtx, float *p_array) {
  67. for (int i = 0; i < 4; i++) {
  68. for (int j = 0; j < 4; j++) {
  69. p_array[i * 4 + j] = p_mtx.matrix[i][j];
  70. }
  71. }
  72. }
  73. void RasterizerSceneGLES3::directional_shadow_create() {
  74. if (directional_shadow.fbo) {
  75. // Erase existing directional shadow texture to recreate it.
  76. glDeleteTextures(1, &directional_shadow.depth);
  77. glDeleteFramebuffers(1, &directional_shadow.fbo);
  78. directional_shadow.depth = 0;
  79. directional_shadow.fbo = 0;
  80. }
  81. directional_shadow.light_count = 0;
  82. directional_shadow.size = next_power_of_2(directional_shadow_size);
  83. glGenFramebuffers(1, &directional_shadow.fbo);
  84. glBindFramebuffer(GL_FRAMEBUFFER, directional_shadow.fbo);
  85. glGenTextures(1, &directional_shadow.depth);
  86. glBindTexture(GL_TEXTURE_2D, directional_shadow.depth);
  87. glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT24, directional_shadow.size, directional_shadow.size, 0, GL_DEPTH_COMPONENT, GL_UNSIGNED_INT, nullptr);
  88. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  89. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  90. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  91. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  92. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
  93. glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, directional_shadow.depth, 0);
  94. GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
  95. if (status != GL_FRAMEBUFFER_COMPLETE) {
  96. ERR_PRINT("Directional shadow framebuffer status invalid");
  97. }
  98. }
  99. /* SHADOW ATLAS API */
  100. RID RasterizerSceneGLES3::shadow_atlas_create() {
  101. ShadowAtlas *shadow_atlas = memnew(ShadowAtlas);
  102. shadow_atlas->fbo = 0;
  103. shadow_atlas->depth = 0;
  104. shadow_atlas->size = 0;
  105. shadow_atlas->smallest_subdiv = 0;
  106. for (int i = 0; i < 4; i++) {
  107. shadow_atlas->size_order[i] = i;
  108. }
  109. return shadow_atlas_owner.make_rid(shadow_atlas);
  110. }
  111. void RasterizerSceneGLES3::shadow_atlas_set_size(RID p_atlas, int p_size) {
  112. ShadowAtlas *shadow_atlas = shadow_atlas_owner.getornull(p_atlas);
  113. ERR_FAIL_COND(!shadow_atlas);
  114. ERR_FAIL_COND(p_size < 0);
  115. p_size = next_power_of_2(p_size);
  116. if (p_size == shadow_atlas->size) {
  117. return;
  118. }
  119. // erasing atlas
  120. if (shadow_atlas->fbo) {
  121. glDeleteTextures(1, &shadow_atlas->depth);
  122. glDeleteFramebuffers(1, &shadow_atlas->fbo);
  123. shadow_atlas->depth = 0;
  124. shadow_atlas->fbo = 0;
  125. }
  126. for (int i = 0; i < 4; i++) {
  127. //clear subdivisions
  128. shadow_atlas->quadrants[i].shadows.resize(0);
  129. shadow_atlas->quadrants[i].shadows.resize(1 << shadow_atlas->quadrants[i].subdivision);
  130. }
  131. //erase shadow atlas reference from lights
  132. for (Map<RID, uint32_t>::Element *E = shadow_atlas->shadow_owners.front(); E; E = E->next()) {
  133. LightInstance *li = light_instance_owner.getornull(E->key());
  134. ERR_CONTINUE(!li);
  135. li->shadow_atlases.erase(p_atlas);
  136. }
  137. //clear owners
  138. shadow_atlas->shadow_owners.clear();
  139. shadow_atlas->size = p_size;
  140. if (shadow_atlas->size) {
  141. glGenFramebuffers(1, &shadow_atlas->fbo);
  142. glBindFramebuffer(GL_FRAMEBUFFER, shadow_atlas->fbo);
  143. // Create a texture for storing the depth
  144. glActiveTexture(GL_TEXTURE0);
  145. glGenTextures(1, &shadow_atlas->depth);
  146. glBindTexture(GL_TEXTURE_2D, shadow_atlas->depth);
  147. glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT24, shadow_atlas->size, shadow_atlas->size, 0,
  148. GL_DEPTH_COMPONENT, GL_UNSIGNED_INT, nullptr);
  149. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  150. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  151. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  152. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  153. glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT,
  154. GL_TEXTURE_2D, shadow_atlas->depth, 0);
  155. glViewport(0, 0, shadow_atlas->size, shadow_atlas->size);
  156. glClearDepth(0.0f);
  157. glClear(GL_DEPTH_BUFFER_BIT);
  158. glBindFramebuffer(GL_FRAMEBUFFER, 0);
  159. }
  160. }
  161. void RasterizerSceneGLES3::shadow_atlas_set_quadrant_subdivision(RID p_atlas, int p_quadrant, int p_subdivision) {
  162. ShadowAtlas *shadow_atlas = shadow_atlas_owner.getornull(p_atlas);
  163. ERR_FAIL_COND(!shadow_atlas);
  164. ERR_FAIL_INDEX(p_quadrant, 4);
  165. ERR_FAIL_INDEX(p_subdivision, 16384);
  166. uint32_t subdiv = next_power_of_2(p_subdivision);
  167. if (subdiv & 0xaaaaaaaa) { //sqrt(subdiv) must be integer
  168. subdiv <<= 1;
  169. }
  170. subdiv = int(Math::sqrt((float)subdiv));
  171. //obtain the number that will be x*x
  172. if (shadow_atlas->quadrants[p_quadrant].subdivision == subdiv) {
  173. return;
  174. }
  175. //erase all data from quadrant
  176. for (int i = 0; i < shadow_atlas->quadrants[p_quadrant].shadows.size(); i++) {
  177. if (shadow_atlas->quadrants[p_quadrant].shadows[i].owner.is_valid()) {
  178. shadow_atlas->shadow_owners.erase(shadow_atlas->quadrants[p_quadrant].shadows[i].owner);
  179. LightInstance *li = light_instance_owner.getornull(shadow_atlas->quadrants[p_quadrant].shadows[i].owner);
  180. ERR_CONTINUE(!li);
  181. li->shadow_atlases.erase(p_atlas);
  182. }
  183. }
  184. shadow_atlas->quadrants[p_quadrant].shadows.resize(0);
  185. shadow_atlas->quadrants[p_quadrant].shadows.resize(subdiv * subdiv);
  186. shadow_atlas->quadrants[p_quadrant].subdivision = subdiv;
  187. //cache the smallest subdiv (for faster allocation in light update)
  188. shadow_atlas->smallest_subdiv = 1 << 30;
  189. for (int i = 0; i < 4; i++) {
  190. if (shadow_atlas->quadrants[i].subdivision) {
  191. shadow_atlas->smallest_subdiv = MIN(shadow_atlas->smallest_subdiv, shadow_atlas->quadrants[i].subdivision);
  192. }
  193. }
  194. if (shadow_atlas->smallest_subdiv == 1 << 30) {
  195. shadow_atlas->smallest_subdiv = 0;
  196. }
  197. //resort the size orders, simple bublesort for 4 elements..
  198. int swaps = 0;
  199. do {
  200. swaps = 0;
  201. for (int i = 0; i < 3; i++) {
  202. if (shadow_atlas->quadrants[shadow_atlas->size_order[i]].subdivision < shadow_atlas->quadrants[shadow_atlas->size_order[i + 1]].subdivision) {
  203. SWAP(shadow_atlas->size_order[i], shadow_atlas->size_order[i + 1]);
  204. swaps++;
  205. }
  206. }
  207. } while (swaps > 0);
  208. }
  209. bool RasterizerSceneGLES3::_shadow_atlas_find_shadow(ShadowAtlas *shadow_atlas, int *p_in_quadrants, int p_quadrant_count, int p_current_subdiv, uint64_t p_tick, int &r_quadrant, int &r_shadow) {
  210. for (int i = p_quadrant_count - 1; i >= 0; i--) {
  211. int qidx = p_in_quadrants[i];
  212. if (shadow_atlas->quadrants[qidx].subdivision == (uint32_t)p_current_subdiv) {
  213. return false;
  214. }
  215. //look for an empty space
  216. int sc = shadow_atlas->quadrants[qidx].shadows.size();
  217. ShadowAtlas::Quadrant::Shadow *sarr = shadow_atlas->quadrants[qidx].shadows.ptrw();
  218. int found_free_idx = -1; //found a free one
  219. int found_used_idx = -1; //found existing one, must steal it
  220. uint64_t min_pass = 0; // pass of the existing one, try to use the least recently used one (LRU fashion)
  221. for (int j = 0; j < sc; j++) {
  222. if (!sarr[j].owner.is_valid()) {
  223. found_free_idx = j;
  224. break;
  225. }
  226. LightInstance *sli = light_instance_owner.getornull(sarr[j].owner);
  227. ERR_CONTINUE(!sli);
  228. if (sli->last_scene_pass != scene_pass) {
  229. //was just allocated, don't kill it so soon, wait a bit..
  230. if (p_tick - sarr[j].alloc_tick < shadow_atlas_realloc_tolerance_msec) {
  231. continue;
  232. }
  233. if (found_used_idx == -1 || sli->last_scene_pass < min_pass) {
  234. found_used_idx = j;
  235. min_pass = sli->last_scene_pass;
  236. }
  237. }
  238. }
  239. if (found_free_idx == -1 && found_used_idx == -1) {
  240. continue; //nothing found
  241. }
  242. if (found_free_idx == -1 && found_used_idx != -1) {
  243. found_free_idx = found_used_idx;
  244. }
  245. r_quadrant = qidx;
  246. r_shadow = found_free_idx;
  247. return true;
  248. }
  249. return false;
  250. }
  251. bool RasterizerSceneGLES3::shadow_atlas_update_light(RID p_atlas, RID p_light_intance, float p_coverage, uint64_t p_light_version) {
  252. ShadowAtlas *shadow_atlas = shadow_atlas_owner.getornull(p_atlas);
  253. ERR_FAIL_COND_V(!shadow_atlas, false);
  254. LightInstance *li = light_instance_owner.getornull(p_light_intance);
  255. ERR_FAIL_COND_V(!li, false);
  256. if (shadow_atlas->size == 0 || shadow_atlas->smallest_subdiv == 0) {
  257. return false;
  258. }
  259. uint32_t quad_size = shadow_atlas->size >> 1;
  260. int desired_fit = MIN(quad_size / shadow_atlas->smallest_subdiv, next_power_of_2(quad_size * p_coverage));
  261. int valid_quadrants[4];
  262. int valid_quadrant_count = 0;
  263. int best_size = -1; //best size found
  264. int best_subdiv = -1; //subdiv for the best size
  265. //find the quadrants this fits into, and the best possible size it can fit into
  266. for (int i = 0; i < 4; i++) {
  267. int q = shadow_atlas->size_order[i];
  268. int sd = shadow_atlas->quadrants[q].subdivision;
  269. if (sd == 0) {
  270. continue; //unused
  271. }
  272. int max_fit = quad_size / sd;
  273. if (best_size != -1 && max_fit > best_size) {
  274. break; //too large
  275. }
  276. valid_quadrants[valid_quadrant_count++] = q;
  277. best_subdiv = sd;
  278. if (max_fit >= desired_fit) {
  279. best_size = max_fit;
  280. }
  281. }
  282. ERR_FAIL_COND_V(valid_quadrant_count == 0, false);
  283. uint64_t tick = OS::get_singleton()->get_ticks_msec();
  284. //see if it already exists
  285. if (shadow_atlas->shadow_owners.has(p_light_intance)) {
  286. //it does!
  287. uint32_t key = shadow_atlas->shadow_owners[p_light_intance];
  288. uint32_t q = (key >> ShadowAtlas::QUADRANT_SHIFT) & 0x3;
  289. uint32_t s = key & ShadowAtlas::SHADOW_INDEX_MASK;
  290. bool should_realloc = shadow_atlas->quadrants[q].subdivision != (uint32_t)best_subdiv && (shadow_atlas->quadrants[q].shadows[s].alloc_tick - tick > shadow_atlas_realloc_tolerance_msec);
  291. bool should_redraw = shadow_atlas->quadrants[q].shadows[s].version != p_light_version;
  292. if (!should_realloc) {
  293. shadow_atlas->quadrants[q].shadows.write[s].version = p_light_version;
  294. //already existing, see if it should redraw or it's just OK
  295. return should_redraw;
  296. }
  297. int new_quadrant, new_shadow;
  298. //find a better place
  299. if (_shadow_atlas_find_shadow(shadow_atlas, valid_quadrants, valid_quadrant_count, shadow_atlas->quadrants[q].subdivision, tick, new_quadrant, new_shadow)) {
  300. //found a better place!
  301. ShadowAtlas::Quadrant::Shadow *sh = &shadow_atlas->quadrants[new_quadrant].shadows.write[new_shadow];
  302. if (sh->owner.is_valid()) {
  303. //is taken, but is invalid, erasing it
  304. shadow_atlas->shadow_owners.erase(sh->owner);
  305. LightInstance *sli = light_instance_owner.get(sh->owner);
  306. sli->shadow_atlases.erase(p_atlas);
  307. }
  308. //erase previous
  309. shadow_atlas->quadrants[q].shadows.write[s].version = 0;
  310. shadow_atlas->quadrants[q].shadows.write[s].owner = RID();
  311. sh->owner = p_light_intance;
  312. sh->alloc_tick = tick;
  313. sh->version = p_light_version;
  314. li->shadow_atlases.insert(p_atlas);
  315. //make new key
  316. key = new_quadrant << ShadowAtlas::QUADRANT_SHIFT;
  317. key |= new_shadow;
  318. //update it in map
  319. shadow_atlas->shadow_owners[p_light_intance] = key;
  320. //make it dirty, as it should redraw anyway
  321. return true;
  322. }
  323. //no better place for this shadow found, keep current
  324. //already existing, see if it should redraw or it's just OK
  325. shadow_atlas->quadrants[q].shadows.write[s].version = p_light_version;
  326. return should_redraw;
  327. }
  328. int new_quadrant, new_shadow;
  329. //find a better place
  330. if (_shadow_atlas_find_shadow(shadow_atlas, valid_quadrants, valid_quadrant_count, -1, tick, new_quadrant, new_shadow)) {
  331. //found a better place!
  332. ShadowAtlas::Quadrant::Shadow *sh = &shadow_atlas->quadrants[new_quadrant].shadows.write[new_shadow];
  333. if (sh->owner.is_valid()) {
  334. //is taken, but is invalid, erasing it
  335. shadow_atlas->shadow_owners.erase(sh->owner);
  336. LightInstance *sli = light_instance_owner.get(sh->owner);
  337. sli->shadow_atlases.erase(p_atlas);
  338. }
  339. sh->owner = p_light_intance;
  340. sh->alloc_tick = tick;
  341. sh->version = p_light_version;
  342. li->shadow_atlases.insert(p_atlas);
  343. //make new key
  344. uint32_t key = new_quadrant << ShadowAtlas::QUADRANT_SHIFT;
  345. key |= new_shadow;
  346. //update it in map
  347. shadow_atlas->shadow_owners[p_light_intance] = key;
  348. //make it dirty, as it should redraw anyway
  349. return true;
  350. }
  351. //no place to allocate this light, apologies
  352. return false;
  353. }
  354. void RasterizerSceneGLES3::set_directional_shadow_count(int p_count) {
  355. directional_shadow.light_count = p_count;
  356. directional_shadow.current_light = 0;
  357. }
  358. int RasterizerSceneGLES3::get_directional_light_shadow_size(RID p_light_intance) {
  359. ERR_FAIL_COND_V(directional_shadow.light_count == 0, 0);
  360. int shadow_size;
  361. if (directional_shadow.light_count == 1) {
  362. shadow_size = directional_shadow.size;
  363. } else {
  364. shadow_size = directional_shadow.size / 2; //more than 4 not supported anyway
  365. }
  366. LightInstance *light_instance = light_instance_owner.getornull(p_light_intance);
  367. ERR_FAIL_COND_V(!light_instance, 0);
  368. switch (light_instance->light_ptr->directional_shadow_mode) {
  369. case VS::LIGHT_DIRECTIONAL_SHADOW_ORTHOGONAL:
  370. break; //none
  371. case VS::LIGHT_DIRECTIONAL_SHADOW_PARALLEL_2_SPLITS:
  372. case VS::LIGHT_DIRECTIONAL_SHADOW_PARALLEL_4_SPLITS:
  373. shadow_size /= 2;
  374. break;
  375. }
  376. return shadow_size;
  377. }
  378. //////////////////////////////////////////////////////
  379. RID RasterizerSceneGLES3::reflection_atlas_create() {
  380. ReflectionAtlas *reflection_atlas = memnew(ReflectionAtlas);
  381. reflection_atlas->subdiv = 0;
  382. reflection_atlas->color = 0;
  383. reflection_atlas->size = 0;
  384. for (int i = 0; i < 6; i++) {
  385. reflection_atlas->fbo[i] = 0;
  386. }
  387. return reflection_atlas_owner.make_rid(reflection_atlas);
  388. }
  389. void RasterizerSceneGLES3::reflection_atlas_set_size(RID p_ref_atlas, int p_size) {
  390. ReflectionAtlas *reflection_atlas = reflection_atlas_owner.getornull(p_ref_atlas);
  391. ERR_FAIL_COND(!reflection_atlas);
  392. int size = next_power_of_2(p_size);
  393. if (size == reflection_atlas->size) {
  394. return;
  395. }
  396. if (reflection_atlas->size) {
  397. for (int i = 0; i < 6; i++) {
  398. glDeleteFramebuffers(1, &reflection_atlas->fbo[i]);
  399. reflection_atlas->fbo[i] = 0;
  400. }
  401. glDeleteTextures(1, &reflection_atlas->color);
  402. reflection_atlas->color = 0;
  403. }
  404. reflection_atlas->size = size;
  405. for (int i = 0; i < reflection_atlas->reflections.size(); i++) {
  406. //erase probes reference to this
  407. if (reflection_atlas->reflections[i].owner.is_valid()) {
  408. ReflectionProbeInstance *reflection_probe_instance = reflection_probe_instance_owner.getornull(reflection_atlas->reflections[i].owner);
  409. reflection_atlas->reflections.write[i].owner = RID();
  410. ERR_CONTINUE(!reflection_probe_instance);
  411. reflection_probe_instance->reflection_atlas_index = -1;
  412. reflection_probe_instance->atlas = RID();
  413. reflection_probe_instance->render_step = -1;
  414. }
  415. }
  416. if (reflection_atlas->size) {
  417. bool use_float = true;
  418. GLenum internal_format = use_float ? GL_RGBA16F : GL_RGB10_A2;
  419. GLenum format = GL_RGBA;
  420. GLenum type = use_float ? GL_HALF_FLOAT : GL_UNSIGNED_INT_2_10_10_10_REV;
  421. // Create a texture for storing the color
  422. glActiveTexture(GL_TEXTURE0);
  423. glGenTextures(1, &reflection_atlas->color);
  424. glBindTexture(GL_TEXTURE_2D, reflection_atlas->color);
  425. int mmsize = reflection_atlas->size;
  426. glTexStorage2DCustom(GL_TEXTURE_2D, 6, internal_format, mmsize, mmsize, format, type);
  427. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
  428. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  429. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  430. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  431. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_BASE_LEVEL, 0);
  432. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, 5);
  433. for (int i = 0; i < 6; i++) {
  434. glGenFramebuffers(1, &reflection_atlas->fbo[i]);
  435. glBindFramebuffer(GL_FRAMEBUFFER, reflection_atlas->fbo[i]);
  436. glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, reflection_atlas->color, i);
  437. GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
  438. ERR_CONTINUE(status != GL_FRAMEBUFFER_COMPLETE);
  439. glDisable(GL_SCISSOR_TEST);
  440. glViewport(0, 0, mmsize, mmsize);
  441. glClearColor(0, 0, 0, 0);
  442. glClear(GL_COLOR_BUFFER_BIT); //it needs to be cleared, to avoid generating garbage
  443. mmsize >>= 1;
  444. }
  445. }
  446. }
  447. void RasterizerSceneGLES3::reflection_atlas_set_subdivision(RID p_ref_atlas, int p_subdiv) {
  448. ReflectionAtlas *reflection_atlas = reflection_atlas_owner.getornull(p_ref_atlas);
  449. ERR_FAIL_COND(!reflection_atlas);
  450. int subdiv = next_power_of_2(p_subdiv);
  451. if (subdiv & 0xaaaaaaaa) { //sqrt(subdiv) must be integer
  452. subdiv <<= 1;
  453. }
  454. subdiv = int(Math::sqrt((float)subdiv));
  455. if (reflection_atlas->subdiv == subdiv) {
  456. return;
  457. }
  458. if (subdiv) {
  459. for (int i = 0; i < reflection_atlas->reflections.size(); i++) {
  460. //erase probes reference to this
  461. if (reflection_atlas->reflections[i].owner.is_valid()) {
  462. ReflectionProbeInstance *reflection_probe_instance = reflection_probe_instance_owner.getornull(reflection_atlas->reflections[i].owner);
  463. reflection_atlas->reflections.write[i].owner = RID();
  464. ERR_CONTINUE(!reflection_probe_instance);
  465. reflection_probe_instance->reflection_atlas_index = -1;
  466. reflection_probe_instance->atlas = RID();
  467. reflection_probe_instance->render_step = -1;
  468. }
  469. }
  470. }
  471. reflection_atlas->subdiv = subdiv;
  472. reflection_atlas->reflections.resize(subdiv * subdiv);
  473. }
  474. ////////////////////////////////////////////////////
  475. RID RasterizerSceneGLES3::reflection_probe_instance_create(RID p_probe) {
  476. RasterizerStorageGLES3::ReflectionProbe *probe = storage->reflection_probe_owner.getornull(p_probe);
  477. ERR_FAIL_COND_V(!probe, RID());
  478. ReflectionProbeInstance *rpi = memnew(ReflectionProbeInstance);
  479. rpi->probe_ptr = probe;
  480. rpi->self = reflection_probe_instance_owner.make_rid(rpi);
  481. rpi->probe = p_probe;
  482. rpi->reflection_atlas_index = -1;
  483. rpi->render_step = -1;
  484. rpi->last_pass = 0;
  485. return rpi->self;
  486. }
  487. void RasterizerSceneGLES3::reflection_probe_instance_set_transform(RID p_instance, const Transform &p_transform) {
  488. ReflectionProbeInstance *rpi = reflection_probe_instance_owner.getornull(p_instance);
  489. ERR_FAIL_COND(!rpi);
  490. rpi->transform = p_transform;
  491. }
  492. void RasterizerSceneGLES3::reflection_probe_release_atlas_index(RID p_instance) {
  493. ReflectionProbeInstance *rpi = reflection_probe_instance_owner.getornull(p_instance);
  494. ERR_FAIL_COND(!rpi);
  495. if (rpi->reflection_atlas_index == -1) {
  496. return;
  497. }
  498. ReflectionAtlas *reflection_atlas = reflection_atlas_owner.getornull(rpi->atlas);
  499. ERR_FAIL_COND(!reflection_atlas);
  500. ERR_FAIL_INDEX(rpi->reflection_atlas_index, reflection_atlas->reflections.size());
  501. ERR_FAIL_COND(reflection_atlas->reflections[rpi->reflection_atlas_index].owner != rpi->self);
  502. reflection_atlas->reflections.write[rpi->reflection_atlas_index].owner = RID();
  503. rpi->reflection_atlas_index = -1;
  504. rpi->atlas = RID();
  505. rpi->render_step = -1;
  506. }
  507. bool RasterizerSceneGLES3::reflection_probe_instance_needs_redraw(RID p_instance) {
  508. ReflectionProbeInstance *rpi = reflection_probe_instance_owner.getornull(p_instance);
  509. ERR_FAIL_COND_V(!rpi, false);
  510. return rpi->reflection_atlas_index == -1 || rpi->probe_ptr->update_mode == VS::REFLECTION_PROBE_UPDATE_ALWAYS;
  511. }
  512. bool RasterizerSceneGLES3::reflection_probe_instance_has_reflection(RID p_instance) {
  513. ReflectionProbeInstance *rpi = reflection_probe_instance_owner.getornull(p_instance);
  514. ERR_FAIL_COND_V(!rpi, false);
  515. return rpi->reflection_atlas_index != -1;
  516. }
  517. bool RasterizerSceneGLES3::reflection_probe_instance_begin_render(RID p_instance, RID p_reflection_atlas) {
  518. ReflectionProbeInstance *rpi = reflection_probe_instance_owner.getornull(p_instance);
  519. ERR_FAIL_COND_V(!rpi, false);
  520. rpi->render_step = 0;
  521. if (rpi->reflection_atlas_index != -1) {
  522. return true; //got one already
  523. }
  524. ReflectionAtlas *reflection_atlas = reflection_atlas_owner.getornull(p_reflection_atlas);
  525. ERR_FAIL_COND_V(!reflection_atlas, false);
  526. if (reflection_atlas->size == 0 || reflection_atlas->subdiv == 0) {
  527. return false;
  528. }
  529. int best_free = -1;
  530. int best_used = -1;
  531. uint64_t best_used_frame = 0;
  532. for (int i = 0; i < reflection_atlas->reflections.size(); i++) {
  533. if (reflection_atlas->reflections[i].owner == RID()) {
  534. best_free = i;
  535. break;
  536. }
  537. if (rpi->render_step < 0 && reflection_atlas->reflections[i].last_frame < storage->frame.count &&
  538. (best_used == -1 || reflection_atlas->reflections[i].last_frame < best_used_frame)) {
  539. best_used = i;
  540. best_used_frame = reflection_atlas->reflections[i].last_frame;
  541. }
  542. }
  543. if (best_free == -1 && best_used == -1) {
  544. return false; // sorry, can not do. Try again next frame.
  545. }
  546. if (best_free == -1) {
  547. //find best from what is used
  548. best_free = best_used;
  549. ReflectionProbeInstance *victim_rpi = reflection_probe_instance_owner.getornull(reflection_atlas->reflections[best_free].owner);
  550. ERR_FAIL_COND_V(!victim_rpi, false);
  551. victim_rpi->atlas = RID();
  552. victim_rpi->reflection_atlas_index = -1;
  553. }
  554. reflection_atlas->reflections.write[best_free].owner = p_instance;
  555. reflection_atlas->reflections.write[best_free].last_frame = storage->frame.count;
  556. rpi->reflection_atlas_index = best_free;
  557. rpi->atlas = p_reflection_atlas;
  558. rpi->render_step = 0;
  559. return true;
  560. }
  561. bool RasterizerSceneGLES3::reflection_probe_instance_postprocess_step(RID p_instance) {
  562. ReflectionProbeInstance *rpi = reflection_probe_instance_owner.getornull(p_instance);
  563. ERR_FAIL_COND_V(!rpi, true);
  564. ReflectionAtlas *reflection_atlas = reflection_atlas_owner.getornull(rpi->atlas);
  565. ERR_FAIL_COND_V(!reflection_atlas, false);
  566. ERR_FAIL_COND_V(rpi->render_step >= 6, true);
  567. glBindFramebuffer(GL_FRAMEBUFFER, reflection_atlas->fbo[rpi->render_step]);
  568. state.cube_to_dp_shader.bind();
  569. int target_size = reflection_atlas->size / reflection_atlas->subdiv;
  570. int cubemap_index = reflection_cubemaps.size() - 1;
  571. for (int i = reflection_cubemaps.size() - 1; i >= 0; i--) {
  572. //find appropriate cubemap to render to
  573. if (reflection_cubemaps[i].size > target_size * 2) {
  574. break;
  575. }
  576. cubemap_index = i;
  577. }
  578. glDisable(GL_BLEND);
  579. glActiveTexture(GL_TEXTURE0);
  580. glBindTexture(GL_TEXTURE_CUBE_MAP, reflection_cubemaps[cubemap_index].cubemap);
  581. glDisable(GL_CULL_FACE);
  582. storage->shaders.cubemap_filter.set_conditional(CubemapFilterShaderGLES3::USE_DUAL_PARABOLOID, true);
  583. storage->shaders.cubemap_filter.bind();
  584. int cell_size = reflection_atlas->size / reflection_atlas->subdiv;
  585. for (int i = 0; i < rpi->render_step; i++) {
  586. cell_size >>= 1; //mipmaps!
  587. }
  588. int x = (rpi->reflection_atlas_index % reflection_atlas->subdiv) * cell_size;
  589. int y = (rpi->reflection_atlas_index / reflection_atlas->subdiv) * cell_size;
  590. int width = cell_size;
  591. int height = cell_size;
  592. storage->shaders.cubemap_filter.set_conditional(CubemapFilterShaderGLES3::USE_DIRECT_WRITE, rpi->render_step == 0);
  593. storage->shaders.cubemap_filter.set_conditional(CubemapFilterShaderGLES3::LOW_QUALITY, rpi->probe_ptr->update_mode == VS::REFLECTION_PROBE_UPDATE_ALWAYS);
  594. for (int i = 0; i < 2; i++) {
  595. storage->shaders.cubemap_filter.set_uniform(CubemapFilterShaderGLES3::Z_FLIP, i == 0);
  596. storage->shaders.cubemap_filter.set_uniform(CubemapFilterShaderGLES3::ROUGHNESS, rpi->render_step / 5.0);
  597. uint32_t local_width = width, local_height = height;
  598. uint32_t local_x = x, local_y = y;
  599. local_height /= 2;
  600. local_y += i * local_height;
  601. glViewport(local_x, local_y, local_width, local_height);
  602. _copy_screen();
  603. }
  604. storage->shaders.cubemap_filter.set_conditional(CubemapFilterShaderGLES3::USE_DIRECT_WRITE, false);
  605. storage->shaders.cubemap_filter.set_conditional(CubemapFilterShaderGLES3::LOW_QUALITY, false);
  606. rpi->render_step++;
  607. return rpi->render_step == 6;
  608. }
  609. /* ENVIRONMENT API */
  610. RID RasterizerSceneGLES3::environment_create() {
  611. Environment *env = memnew(Environment);
  612. return environment_owner.make_rid(env);
  613. }
  614. void RasterizerSceneGLES3::environment_set_background(RID p_env, VS::EnvironmentBG p_bg) {
  615. Environment *env = environment_owner.getornull(p_env);
  616. ERR_FAIL_COND(!env);
  617. env->bg_mode = p_bg;
  618. }
  619. void RasterizerSceneGLES3::environment_set_sky(RID p_env, RID p_sky) {
  620. Environment *env = environment_owner.getornull(p_env);
  621. ERR_FAIL_COND(!env);
  622. env->sky = p_sky;
  623. }
  624. void RasterizerSceneGLES3::environment_set_sky_custom_fov(RID p_env, float p_scale) {
  625. Environment *env = environment_owner.getornull(p_env);
  626. ERR_FAIL_COND(!env);
  627. env->sky_custom_fov = p_scale;
  628. }
  629. void RasterizerSceneGLES3::environment_set_sky_orientation(RID p_env, const Basis &p_orientation) {
  630. Environment *env = environment_owner.getornull(p_env);
  631. ERR_FAIL_COND(!env);
  632. env->sky_orientation = p_orientation;
  633. }
  634. void RasterizerSceneGLES3::environment_set_bg_color(RID p_env, const Color &p_color) {
  635. Environment *env = environment_owner.getornull(p_env);
  636. ERR_FAIL_COND(!env);
  637. env->bg_color = p_color;
  638. }
  639. void RasterizerSceneGLES3::environment_set_bg_energy(RID p_env, float p_energy) {
  640. Environment *env = environment_owner.getornull(p_env);
  641. ERR_FAIL_COND(!env);
  642. env->bg_energy = p_energy;
  643. }
  644. void RasterizerSceneGLES3::environment_set_canvas_max_layer(RID p_env, int p_max_layer) {
  645. Environment *env = environment_owner.getornull(p_env);
  646. ERR_FAIL_COND(!env);
  647. env->canvas_max_layer = p_max_layer;
  648. }
  649. void RasterizerSceneGLES3::environment_set_ambient_light(RID p_env, const Color &p_color, float p_energy, float p_sky_contribution) {
  650. Environment *env = environment_owner.getornull(p_env);
  651. ERR_FAIL_COND(!env);
  652. env->ambient_color = p_color;
  653. env->ambient_energy = p_energy;
  654. env->ambient_sky_contribution = p_sky_contribution;
  655. }
  656. void RasterizerSceneGLES3::environment_set_camera_feed_id(RID p_env, int p_camera_feed_id) {
  657. Environment *env = environment_owner.getornull(p_env);
  658. ERR_FAIL_COND(!env);
  659. env->camera_feed_id = p_camera_feed_id;
  660. }
  661. void RasterizerSceneGLES3::environment_set_dof_blur_far(RID p_env, bool p_enable, float p_distance, float p_transition, float p_amount, VS::EnvironmentDOFBlurQuality p_quality) {
  662. Environment *env = environment_owner.getornull(p_env);
  663. ERR_FAIL_COND(!env);
  664. env->dof_blur_far_enabled = p_enable;
  665. env->dof_blur_far_distance = p_distance;
  666. env->dof_blur_far_transition = p_transition;
  667. env->dof_blur_far_amount = p_amount;
  668. env->dof_blur_far_quality = p_quality;
  669. }
  670. void RasterizerSceneGLES3::environment_set_dof_blur_near(RID p_env, bool p_enable, float p_distance, float p_transition, float p_amount, VS::EnvironmentDOFBlurQuality p_quality) {
  671. Environment *env = environment_owner.getornull(p_env);
  672. ERR_FAIL_COND(!env);
  673. env->dof_blur_near_enabled = p_enable;
  674. env->dof_blur_near_distance = p_distance;
  675. env->dof_blur_near_transition = p_transition;
  676. env->dof_blur_near_amount = p_amount;
  677. env->dof_blur_near_quality = p_quality;
  678. }
  679. void RasterizerSceneGLES3::environment_set_glow(RID p_env, bool p_enable, int p_level_flags, float p_intensity, float p_strength, float p_bloom_threshold, VS::EnvironmentGlowBlendMode p_blend_mode, float p_hdr_bleed_threshold, float p_hdr_bleed_scale, float p_hdr_luminance_cap, bool p_bicubic_upscale, bool p_high_quality) {
  680. Environment *env = environment_owner.getornull(p_env);
  681. ERR_FAIL_COND(!env);
  682. env->glow_enabled = p_enable;
  683. env->glow_levels = p_level_flags;
  684. env->glow_intensity = p_intensity;
  685. env->glow_strength = p_strength;
  686. env->glow_bloom = p_bloom_threshold;
  687. env->glow_blend_mode = p_blend_mode;
  688. env->glow_hdr_bleed_threshold = p_hdr_bleed_threshold;
  689. env->glow_hdr_bleed_scale = p_hdr_bleed_scale;
  690. env->glow_hdr_luminance_cap = p_hdr_luminance_cap;
  691. env->glow_bicubic_upscale = p_bicubic_upscale;
  692. env->glow_high_quality = p_high_quality;
  693. }
  694. void RasterizerSceneGLES3::environment_set_fog(RID p_env, bool p_enable, float p_begin, float p_end, RID p_gradient_texture) {
  695. }
  696. void RasterizerSceneGLES3::environment_set_ssr(RID p_env, bool p_enable, int p_max_steps, float p_fade_in, float p_fade_out, float p_depth_tolerance, bool p_roughness) {
  697. Environment *env = environment_owner.getornull(p_env);
  698. ERR_FAIL_COND(!env);
  699. env->ssr_enabled = p_enable;
  700. env->ssr_max_steps = p_max_steps;
  701. env->ssr_fade_in = p_fade_in;
  702. env->ssr_fade_out = p_fade_out;
  703. env->ssr_depth_tolerance = p_depth_tolerance;
  704. env->ssr_roughness = p_roughness;
  705. }
  706. void RasterizerSceneGLES3::environment_set_ssao(RID p_env, bool p_enable, float p_radius, float p_intensity, float p_radius2, float p_intensity2, float p_bias, float p_light_affect, float p_ao_channel_affect, const Color &p_color, VS::EnvironmentSSAOQuality p_quality, VisualServer::EnvironmentSSAOBlur p_blur, float p_bilateral_sharpness) {
  707. Environment *env = environment_owner.getornull(p_env);
  708. ERR_FAIL_COND(!env);
  709. env->ssao_enabled = p_enable;
  710. env->ssao_radius = p_radius;
  711. env->ssao_intensity = p_intensity;
  712. env->ssao_radius2 = p_radius2;
  713. env->ssao_intensity2 = p_intensity2;
  714. env->ssao_bias = p_bias;
  715. env->ssao_light_affect = p_light_affect;
  716. env->ssao_ao_channel_affect = p_ao_channel_affect;
  717. env->ssao_color = p_color;
  718. env->ssao_filter = p_blur;
  719. env->ssao_quality = p_quality;
  720. env->ssao_bilateral_sharpness = p_bilateral_sharpness;
  721. }
  722. void RasterizerSceneGLES3::environment_set_tonemap(RID p_env, VS::EnvironmentToneMapper p_tone_mapper, float p_exposure, float p_white, bool p_auto_exposure, float p_min_luminance, float p_max_luminance, float p_auto_exp_speed, float p_auto_exp_scale) {
  723. Environment *env = environment_owner.getornull(p_env);
  724. ERR_FAIL_COND(!env);
  725. env->tone_mapper = p_tone_mapper;
  726. env->tone_mapper_exposure = p_exposure;
  727. env->tone_mapper_exposure_white = p_white;
  728. env->auto_exposure = p_auto_exposure;
  729. env->auto_exposure_speed = p_auto_exp_speed;
  730. env->auto_exposure_min = p_min_luminance;
  731. env->auto_exposure_max = p_max_luminance;
  732. env->auto_exposure_grey = p_auto_exp_scale;
  733. }
  734. void RasterizerSceneGLES3::environment_set_adjustment(RID p_env, bool p_enable, float p_brightness, float p_contrast, float p_saturation, RID p_ramp) {
  735. Environment *env = environment_owner.getornull(p_env);
  736. ERR_FAIL_COND(!env);
  737. env->adjustments_enabled = p_enable;
  738. env->adjustments_brightness = p_brightness;
  739. env->adjustments_contrast = p_contrast;
  740. env->adjustments_saturation = p_saturation;
  741. env->color_correction = p_ramp;
  742. }
  743. void RasterizerSceneGLES3::environment_set_fog(RID p_env, bool p_enable, const Color &p_color, const Color &p_sun_color, float p_sun_amount) {
  744. Environment *env = environment_owner.getornull(p_env);
  745. ERR_FAIL_COND(!env);
  746. env->fog_enabled = p_enable;
  747. env->fog_color = p_color;
  748. env->fog_sun_color = p_sun_color;
  749. env->fog_sun_amount = p_sun_amount;
  750. }
  751. void RasterizerSceneGLES3::environment_set_fog_depth(RID p_env, bool p_enable, float p_depth_begin, float p_depth_end, float p_depth_curve, bool p_transmit, float p_transmit_curve) {
  752. Environment *env = environment_owner.getornull(p_env);
  753. ERR_FAIL_COND(!env);
  754. env->fog_depth_enabled = p_enable;
  755. env->fog_depth_begin = p_depth_begin;
  756. env->fog_depth_end = p_depth_end;
  757. env->fog_depth_curve = p_depth_curve;
  758. env->fog_transmit_enabled = p_transmit;
  759. env->fog_transmit_curve = p_transmit_curve;
  760. }
  761. void RasterizerSceneGLES3::environment_set_fog_height(RID p_env, bool p_enable, float p_min_height, float p_max_height, float p_height_curve) {
  762. Environment *env = environment_owner.getornull(p_env);
  763. ERR_FAIL_COND(!env);
  764. env->fog_height_enabled = p_enable;
  765. env->fog_height_min = p_min_height;
  766. env->fog_height_max = p_max_height;
  767. env->fog_height_curve = p_height_curve;
  768. }
  769. bool RasterizerSceneGLES3::is_environment(RID p_env) {
  770. return environment_owner.owns(p_env);
  771. }
  772. VS::EnvironmentBG RasterizerSceneGLES3::environment_get_background(RID p_env) {
  773. const Environment *env = environment_owner.getornull(p_env);
  774. ERR_FAIL_COND_V(!env, VS::ENV_BG_MAX);
  775. return env->bg_mode;
  776. }
  777. int RasterizerSceneGLES3::environment_get_canvas_max_layer(RID p_env) {
  778. const Environment *env = environment_owner.getornull(p_env);
  779. ERR_FAIL_COND_V(!env, -1);
  780. return env->canvas_max_layer;
  781. }
  782. RID RasterizerSceneGLES3::light_instance_create(RID p_light) {
  783. LightInstance *light_instance = memnew(LightInstance);
  784. light_instance->last_pass = 0;
  785. light_instance->last_scene_pass = 0;
  786. light_instance->last_scene_shadow_pass = 0;
  787. light_instance->light = p_light;
  788. light_instance->light_ptr = storage->light_owner.getornull(p_light);
  789. if (!light_instance->light_ptr) {
  790. memdelete(light_instance);
  791. ERR_FAIL_V_MSG(RID(), "Condition ' !light_instance->light_ptr ' is true.");
  792. }
  793. light_instance->self = light_instance_owner.make_rid(light_instance);
  794. return light_instance->self;
  795. }
  796. void RasterizerSceneGLES3::light_instance_set_transform(RID p_light_instance, const Transform &p_transform) {
  797. LightInstance *light_instance = light_instance_owner.getornull(p_light_instance);
  798. ERR_FAIL_COND(!light_instance);
  799. light_instance->transform = p_transform;
  800. }
  801. void RasterizerSceneGLES3::light_instance_set_shadow_transform(RID p_light_instance, const CameraMatrix &p_projection, const Transform &p_transform, float p_far, float p_split, int p_pass, float p_bias_scale) {
  802. LightInstance *light_instance = light_instance_owner.getornull(p_light_instance);
  803. ERR_FAIL_COND(!light_instance);
  804. if (light_instance->light_ptr->type != VS::LIGHT_DIRECTIONAL) {
  805. p_pass = 0;
  806. }
  807. ERR_FAIL_INDEX(p_pass, 4);
  808. light_instance->shadow_transform[p_pass].camera = p_projection;
  809. light_instance->shadow_transform[p_pass].transform = p_transform;
  810. light_instance->shadow_transform[p_pass].farplane = p_far;
  811. light_instance->shadow_transform[p_pass].split = p_split;
  812. light_instance->shadow_transform[p_pass].bias_scale = p_bias_scale;
  813. }
  814. void RasterizerSceneGLES3::light_instance_mark_visible(RID p_light_instance) {
  815. LightInstance *light_instance = light_instance_owner.getornull(p_light_instance);
  816. ERR_FAIL_COND(!light_instance);
  817. light_instance->last_scene_pass = scene_pass;
  818. }
  819. //////////////////////
  820. RID RasterizerSceneGLES3::gi_probe_instance_create() {
  821. GIProbeInstance *gipi = memnew(GIProbeInstance);
  822. return gi_probe_instance_owner.make_rid(gipi);
  823. }
  824. void RasterizerSceneGLES3::gi_probe_instance_set_light_data(RID p_probe, RID p_base, RID p_data) {
  825. GIProbeInstance *gipi = gi_probe_instance_owner.getornull(p_probe);
  826. ERR_FAIL_COND(!gipi);
  827. gipi->data = p_data;
  828. gipi->probe = storage->gi_probe_owner.getornull(p_base);
  829. if (p_data.is_valid()) {
  830. RasterizerStorageGLES3::GIProbeData *gipd = storage->gi_probe_data_owner.getornull(p_data);
  831. ERR_FAIL_COND(!gipd);
  832. gipi->tex_cache = gipd->tex_id;
  833. gipi->cell_size_cache.x = 1.0 / gipd->width;
  834. gipi->cell_size_cache.y = 1.0 / gipd->height;
  835. gipi->cell_size_cache.z = 1.0 / gipd->depth;
  836. }
  837. }
  838. void RasterizerSceneGLES3::gi_probe_instance_set_transform_to_data(RID p_probe, const Transform &p_xform) {
  839. GIProbeInstance *gipi = gi_probe_instance_owner.getornull(p_probe);
  840. ERR_FAIL_COND(!gipi);
  841. gipi->transform_to_data = p_xform;
  842. }
  843. void RasterizerSceneGLES3::gi_probe_instance_set_bounds(RID p_probe, const Vector3 &p_bounds) {
  844. GIProbeInstance *gipi = gi_probe_instance_owner.getornull(p_probe);
  845. ERR_FAIL_COND(!gipi);
  846. gipi->bounds = p_bounds;
  847. }
  848. ////////////////////////////
  849. ////////////////////////////
  850. ////////////////////////////
  851. bool RasterizerSceneGLES3::_setup_material(RasterizerStorageGLES3::Material *p_material, bool p_depth_pass, bool p_alpha_pass) {
  852. /* this is handled outside
  853. if (p_material->shader->spatial.cull_mode == RasterizerStorageGLES3::Shader::Spatial::CULL_MODE_DISABLED) {
  854. glDisable(GL_CULL_FACE);
  855. } else {
  856. glEnable(GL_CULL_FACE);
  857. } */
  858. if (state.current_line_width != p_material->line_width) {
  859. //glLineWidth(MAX(p_material->line_width,1.0));
  860. state.current_line_width = p_material->line_width;
  861. }
  862. if (state.current_depth_test != (!p_material->shader->spatial.no_depth_test)) {
  863. if (p_material->shader->spatial.no_depth_test) {
  864. glDisable(GL_DEPTH_TEST);
  865. } else {
  866. glEnable(GL_DEPTH_TEST);
  867. }
  868. state.current_depth_test = !p_material->shader->spatial.no_depth_test;
  869. }
  870. if (state.current_depth_draw != p_material->shader->spatial.depth_draw_mode) {
  871. switch (p_material->shader->spatial.depth_draw_mode) {
  872. case RasterizerStorageGLES3::Shader::Spatial::DEPTH_DRAW_ALPHA_PREPASS: {
  873. glDepthMask(p_depth_pass);
  874. // If some transparent objects write to depth, we need to re-copy depth texture when we need it
  875. if (p_alpha_pass && !state.used_depth_prepass) {
  876. state.prepared_depth_texture = false;
  877. }
  878. } break;
  879. case RasterizerStorageGLES3::Shader::Spatial::DEPTH_DRAW_OPAQUE: {
  880. glDepthMask(!p_alpha_pass);
  881. } break;
  882. case RasterizerStorageGLES3::Shader::Spatial::DEPTH_DRAW_ALWAYS: {
  883. glDepthMask(GL_TRUE);
  884. // If some transparent objects write to depth, we need to re-copy depth texture when we need it
  885. if (p_alpha_pass) {
  886. state.prepared_depth_texture = false;
  887. }
  888. } break;
  889. case RasterizerStorageGLES3::Shader::Spatial::DEPTH_DRAW_NEVER: {
  890. glDepthMask(GL_FALSE);
  891. } break;
  892. }
  893. state.current_depth_draw = p_material->shader->spatial.depth_draw_mode;
  894. }
  895. //material parameters
  896. state.scene_shader.set_custom_shader(p_material->shader->custom_code_id);
  897. bool rebind = state.scene_shader.bind();
  898. if (!ShaderGLES3::get_active()) {
  899. return false;
  900. }
  901. if (p_material->ubo_id) {
  902. glBindBufferBase(GL_UNIFORM_BUFFER, 1, p_material->ubo_id);
  903. }
  904. int tc = p_material->textures.size();
  905. RID *textures = p_material->textures.ptrw();
  906. ShaderLanguage::ShaderNode::Uniform::Hint *texture_hints = p_material->shader->texture_hints.ptrw();
  907. const ShaderLanguage::DataType *texture_types = p_material->shader->texture_types.ptr();
  908. state.current_main_tex = 0;
  909. for (int i = 0; i < tc; i++) {
  910. glActiveTexture(GL_TEXTURE0 + i);
  911. GLenum target = GL_TEXTURE_2D;
  912. GLuint tex = 0;
  913. RasterizerStorageGLES3::Texture *t = storage->texture_owner.getptr(textures[i]);
  914. if (t) {
  915. if (t->redraw_if_visible) { //must check before proxy because this is often used with proxies
  916. VisualServerRaster::redraw_request(false);
  917. }
  918. t = t->get_ptr(); //resolve for proxies
  919. #ifdef TOOLS_ENABLED
  920. if (t->detect_3d) {
  921. t->detect_3d(t->detect_3d_ud);
  922. }
  923. #endif
  924. #ifdef TOOLS_ENABLED
  925. if (t->detect_normal && texture_hints[i] == ShaderLanguage::ShaderNode::Uniform::HINT_NORMAL) {
  926. t->detect_normal(t->detect_normal_ud);
  927. }
  928. #endif
  929. if (t->render_target) {
  930. t->render_target->used_in_frame = true;
  931. }
  932. target = t->target;
  933. tex = t->tex_id;
  934. } else {
  935. switch (texture_types[i]) {
  936. case ShaderLanguage::TYPE_ISAMPLER2D:
  937. case ShaderLanguage::TYPE_USAMPLER2D:
  938. case ShaderLanguage::TYPE_SAMPLER2D: {
  939. target = GL_TEXTURE_2D;
  940. switch (texture_hints[i]) {
  941. case ShaderLanguage::ShaderNode::Uniform::HINT_BLACK_ALBEDO:
  942. case ShaderLanguage::ShaderNode::Uniform::HINT_BLACK: {
  943. tex = storage->resources.black_tex;
  944. } break;
  945. case ShaderLanguage::ShaderNode::Uniform::HINT_ANISO: {
  946. tex = storage->resources.aniso_tex;
  947. } break;
  948. case ShaderLanguage::ShaderNode::Uniform::HINT_NORMAL: {
  949. tex = storage->resources.normal_tex;
  950. } break;
  951. default: {
  952. tex = storage->resources.white_tex;
  953. } break;
  954. }
  955. } break;
  956. case ShaderLanguage::TYPE_SAMPLERCUBE: {
  957. // TODO
  958. } break;
  959. case ShaderLanguage::TYPE_ISAMPLER3D:
  960. case ShaderLanguage::TYPE_USAMPLER3D:
  961. case ShaderLanguage::TYPE_SAMPLER3D: {
  962. target = GL_TEXTURE_3D;
  963. tex = storage->resources.white_tex_3d;
  964. //switch (texture_hints[i]) {
  965. // TODO
  966. //}
  967. } break;
  968. case ShaderLanguage::TYPE_ISAMPLER2DARRAY:
  969. case ShaderLanguage::TYPE_USAMPLER2DARRAY:
  970. case ShaderLanguage::TYPE_SAMPLER2DARRAY: {
  971. target = GL_TEXTURE_2D_ARRAY;
  972. tex = storage->resources.white_tex_array;
  973. //switch (texture_hints[i]) {
  974. // TODO
  975. //}
  976. } break;
  977. default: {
  978. }
  979. }
  980. }
  981. glBindTexture(target, tex);
  982. if (t && storage->config.srgb_decode_supported) {
  983. //if SRGB decode extension is present, simply switch the texture to whatever is needed
  984. bool must_srgb = false;
  985. if (t->srgb && (texture_hints[i] == ShaderLanguage::ShaderNode::Uniform::HINT_ALBEDO || texture_hints[i] == ShaderLanguage::ShaderNode::Uniform::HINT_BLACK_ALBEDO)) {
  986. must_srgb = true;
  987. }
  988. if (t->using_srgb != must_srgb) {
  989. if (must_srgb) {
  990. glTexParameteri(t->target, _TEXTURE_SRGB_DECODE_EXT, _DECODE_EXT);
  991. #ifdef TOOLS_ENABLED
  992. if (t->detect_srgb) {
  993. t->detect_srgb(t->detect_srgb_ud);
  994. }
  995. #endif
  996. } else {
  997. glTexParameteri(t->target, _TEXTURE_SRGB_DECODE_EXT, _SKIP_DECODE_EXT);
  998. }
  999. t->using_srgb = must_srgb;
  1000. }
  1001. }
  1002. if (i == 0) {
  1003. state.current_main_tex = tex;
  1004. }
  1005. }
  1006. return rebind;
  1007. }
  1008. struct RasterizerGLES3Particle {
  1009. float color[4];
  1010. float velocity_active[4];
  1011. float custom[4];
  1012. float xform_1[4];
  1013. float xform_2[4];
  1014. float xform_3[4];
  1015. };
  1016. struct RasterizerGLES3ParticleSort {
  1017. Vector3 z_dir;
  1018. bool operator()(const RasterizerGLES3Particle &p_a, const RasterizerGLES3Particle &p_b) const {
  1019. return z_dir.dot(Vector3(p_a.xform_1[3], p_a.xform_2[3], p_a.xform_3[3])) < z_dir.dot(Vector3(p_b.xform_1[3], p_b.xform_2[3], p_b.xform_3[3]));
  1020. }
  1021. };
  1022. void RasterizerSceneGLES3::_setup_geometry(RenderList::Element *e, const Transform &p_view_transform) {
  1023. switch (e->instance->base_type) {
  1024. case VS::INSTANCE_MESH: {
  1025. RasterizerStorageGLES3::Surface *s = static_cast<RasterizerStorageGLES3::Surface *>(e->geometry);
  1026. if (s->blend_shapes.size() && e->instance->blend_values.size()) {
  1027. //blend shapes, use transform feedback
  1028. storage->mesh_render_blend_shapes(s, e->instance->blend_values.read().ptr());
  1029. //rebind shader
  1030. state.scene_shader.bind();
  1031. #ifdef DEBUG_ENABLED
  1032. } else if (state.debug_draw == VS::VIEWPORT_DEBUG_DRAW_WIREFRAME && s->array_wireframe_id) {
  1033. glBindVertexArray(s->array_wireframe_id); // everything is so easy nowadays
  1034. #endif
  1035. } else {
  1036. glBindVertexArray(s->array_id); // everything is so easy nowadays
  1037. }
  1038. } break;
  1039. case VS::INSTANCE_MULTIMESH: {
  1040. RasterizerStorageGLES3::MultiMesh *multi_mesh = static_cast<RasterizerStorageGLES3::MultiMesh *>(e->owner);
  1041. RasterizerStorageGLES3::Surface *s = static_cast<RasterizerStorageGLES3::Surface *>(e->geometry);
  1042. #ifdef DEBUG_ENABLED
  1043. if (state.debug_draw == VS::VIEWPORT_DEBUG_DRAW_WIREFRAME && s->instancing_array_wireframe_id) {
  1044. glBindVertexArray(s->instancing_array_wireframe_id); // use the instancing array ID
  1045. } else
  1046. #endif
  1047. {
  1048. glBindVertexArray(s->instancing_array_id); // use the instancing array ID
  1049. }
  1050. glBindBuffer(GL_ARRAY_BUFFER, multi_mesh->buffer); //modify the buffer
  1051. int stride = (multi_mesh->xform_floats + multi_mesh->color_floats + multi_mesh->custom_data_floats) * 4;
  1052. glEnableVertexAttribArray(8);
  1053. glVertexAttribPointer(8, 4, GL_FLOAT, GL_FALSE, stride, nullptr);
  1054. glVertexAttribDivisor(8, 1);
  1055. glEnableVertexAttribArray(9);
  1056. glVertexAttribPointer(9, 4, GL_FLOAT, GL_FALSE, stride, CAST_INT_TO_UCHAR_PTR(4 * 4));
  1057. glVertexAttribDivisor(9, 1);
  1058. int color_ofs;
  1059. if (multi_mesh->transform_format == VS::MULTIMESH_TRANSFORM_3D) {
  1060. glEnableVertexAttribArray(10);
  1061. glVertexAttribPointer(10, 4, GL_FLOAT, GL_FALSE, stride, CAST_INT_TO_UCHAR_PTR(8 * 4));
  1062. glVertexAttribDivisor(10, 1);
  1063. color_ofs = 12 * 4;
  1064. } else {
  1065. glDisableVertexAttribArray(10);
  1066. glVertexAttrib4f(10, 0, 0, 1, 0);
  1067. color_ofs = 8 * 4;
  1068. }
  1069. int custom_data_ofs = color_ofs;
  1070. switch (multi_mesh->color_format) {
  1071. case VS::MULTIMESH_COLOR_MAX:
  1072. case VS::MULTIMESH_COLOR_NONE: {
  1073. glDisableVertexAttribArray(11);
  1074. glVertexAttrib4f(11, 1, 1, 1, 1);
  1075. } break;
  1076. case VS::MULTIMESH_COLOR_8BIT: {
  1077. glEnableVertexAttribArray(11);
  1078. glVertexAttribPointer(11, 4, GL_UNSIGNED_BYTE, GL_TRUE, stride, CAST_INT_TO_UCHAR_PTR(color_ofs));
  1079. glVertexAttribDivisor(11, 1);
  1080. custom_data_ofs += 4;
  1081. } break;
  1082. case VS::MULTIMESH_COLOR_FLOAT: {
  1083. glEnableVertexAttribArray(11);
  1084. glVertexAttribPointer(11, 4, GL_FLOAT, GL_FALSE, stride, CAST_INT_TO_UCHAR_PTR(color_ofs));
  1085. glVertexAttribDivisor(11, 1);
  1086. custom_data_ofs += 4 * 4;
  1087. } break;
  1088. }
  1089. switch (multi_mesh->custom_data_format) {
  1090. case VS::MULTIMESH_CUSTOM_DATA_MAX:
  1091. case VS::MULTIMESH_CUSTOM_DATA_NONE: {
  1092. glDisableVertexAttribArray(12);
  1093. glVertexAttrib4f(12, 1, 1, 1, 1);
  1094. } break;
  1095. case VS::MULTIMESH_CUSTOM_DATA_8BIT: {
  1096. glEnableVertexAttribArray(12);
  1097. glVertexAttribPointer(12, 4, GL_UNSIGNED_BYTE, GL_TRUE, stride, CAST_INT_TO_UCHAR_PTR(custom_data_ofs));
  1098. glVertexAttribDivisor(12, 1);
  1099. } break;
  1100. case VS::MULTIMESH_CUSTOM_DATA_FLOAT: {
  1101. glEnableVertexAttribArray(12);
  1102. glVertexAttribPointer(12, 4, GL_FLOAT, GL_FALSE, stride, CAST_INT_TO_UCHAR_PTR(custom_data_ofs));
  1103. glVertexAttribDivisor(12, 1);
  1104. } break;
  1105. }
  1106. } break;
  1107. case VS::INSTANCE_PARTICLES: {
  1108. RasterizerStorageGLES3::Particles *particles = static_cast<RasterizerStorageGLES3::Particles *>(e->owner);
  1109. RasterizerStorageGLES3::Surface *s = static_cast<RasterizerStorageGLES3::Surface *>(e->geometry);
  1110. if (particles->draw_order == VS::PARTICLES_DRAW_ORDER_VIEW_DEPTH && particles->particle_valid_histories[1]) {
  1111. glBindBuffer(GL_ARRAY_BUFFER, particles->particle_buffer_histories[1]); //modify the buffer, this was used 2 frames ago so it should be good enough for flushing
  1112. RasterizerGLES3Particle *particle_array;
  1113. #ifndef __EMSCRIPTEN__
  1114. particle_array = static_cast<RasterizerGLES3Particle *>(glMapBufferRange(GL_ARRAY_BUFFER, 0, particles->amount * 24 * sizeof(float), GL_MAP_READ_BIT | GL_MAP_WRITE_BIT));
  1115. #else
  1116. PoolVector<RasterizerGLES3Particle> particle_vector;
  1117. particle_vector.resize(particles->amount);
  1118. PoolVector<RasterizerGLES3Particle>::Write particle_writer = particle_vector.write();
  1119. particle_array = particle_writer.ptr();
  1120. glGetBufferSubData(GL_ARRAY_BUFFER, 0, particles->amount * sizeof(RasterizerGLES3Particle), particle_array);
  1121. #endif
  1122. SortArray<RasterizerGLES3Particle, RasterizerGLES3ParticleSort> sorter;
  1123. if (particles->use_local_coords) {
  1124. sorter.compare.z_dir = e->instance->transform.affine_inverse().xform(p_view_transform.basis.get_axis(2)).normalized();
  1125. } else {
  1126. sorter.compare.z_dir = p_view_transform.basis.get_axis(2).normalized();
  1127. }
  1128. sorter.sort(particle_array, particles->amount);
  1129. #ifndef __EMSCRIPTEN__
  1130. glUnmapBuffer(GL_ARRAY_BUFFER);
  1131. #else
  1132. particle_writer.release();
  1133. particle_array = NULL;
  1134. {
  1135. PoolVector<RasterizerGLES3Particle>::Read r = particle_vector.read();
  1136. glBufferSubData(GL_ARRAY_BUFFER, 0, particles->amount * sizeof(RasterizerGLES3Particle), r.ptr());
  1137. }
  1138. particle_vector = PoolVector<RasterizerGLES3Particle>();
  1139. #endif
  1140. #ifdef DEBUG_ENABLED
  1141. if (state.debug_draw == VS::VIEWPORT_DEBUG_DRAW_WIREFRAME && s->instancing_array_wireframe_id) {
  1142. glBindVertexArray(s->instancing_array_wireframe_id); // use the wireframe instancing array ID
  1143. } else
  1144. #endif
  1145. {
  1146. glBindVertexArray(s->instancing_array_id); // use the instancing array ID
  1147. }
  1148. glBindBuffer(GL_ARRAY_BUFFER, particles->particle_buffer_histories[1]); //modify the buffer
  1149. } else {
  1150. #ifdef DEBUG_ENABLED
  1151. if (state.debug_draw == VS::VIEWPORT_DEBUG_DRAW_WIREFRAME && s->instancing_array_wireframe_id) {
  1152. glBindVertexArray(s->instancing_array_wireframe_id); // use the wireframe instancing array ID
  1153. } else
  1154. #endif
  1155. {
  1156. glBindVertexArray(s->instancing_array_id); // use the instancing array ID
  1157. }
  1158. glBindBuffer(GL_ARRAY_BUFFER, particles->particle_buffers[0]); //modify the buffer
  1159. }
  1160. int stride = sizeof(float) * 4 * 6;
  1161. //transform
  1162. if (particles->draw_order != VS::PARTICLES_DRAW_ORDER_LIFETIME) {
  1163. glEnableVertexAttribArray(8); //xform x
  1164. glVertexAttribPointer(8, 4, GL_FLOAT, GL_FALSE, stride, CAST_INT_TO_UCHAR_PTR(sizeof(float) * 4 * 3));
  1165. glVertexAttribDivisor(8, 1);
  1166. glEnableVertexAttribArray(9); //xform y
  1167. glVertexAttribPointer(9, 4, GL_FLOAT, GL_FALSE, stride, CAST_INT_TO_UCHAR_PTR(sizeof(float) * 4 * 4));
  1168. glVertexAttribDivisor(9, 1);
  1169. glEnableVertexAttribArray(10); //xform z
  1170. glVertexAttribPointer(10, 4, GL_FLOAT, GL_FALSE, stride, CAST_INT_TO_UCHAR_PTR(sizeof(float) * 4 * 5));
  1171. glVertexAttribDivisor(10, 1);
  1172. glEnableVertexAttribArray(11); //color
  1173. glVertexAttribPointer(11, 4, GL_FLOAT, GL_FALSE, stride, nullptr);
  1174. glVertexAttribDivisor(11, 1);
  1175. glEnableVertexAttribArray(12); //custom
  1176. glVertexAttribPointer(12, 4, GL_FLOAT, GL_FALSE, stride, CAST_INT_TO_UCHAR_PTR(sizeof(float) * 4 * 2));
  1177. glVertexAttribDivisor(12, 1);
  1178. }
  1179. } break;
  1180. default: {
  1181. }
  1182. }
  1183. }
  1184. static const GLenum gl_primitive[] = {
  1185. GL_POINTS,
  1186. GL_LINES,
  1187. GL_LINE_STRIP,
  1188. GL_LINE_LOOP,
  1189. GL_TRIANGLES,
  1190. GL_TRIANGLE_STRIP,
  1191. GL_TRIANGLE_FAN
  1192. };
  1193. void RasterizerSceneGLES3::_render_geometry(RenderList::Element *e) {
  1194. switch (e->instance->base_type) {
  1195. case VS::INSTANCE_MESH: {
  1196. RasterizerStorageGLES3::Surface *s = static_cast<RasterizerStorageGLES3::Surface *>(e->geometry);
  1197. #ifdef DEBUG_ENABLED
  1198. if (state.debug_draw == VS::VIEWPORT_DEBUG_DRAW_WIREFRAME && s->array_wireframe_id) {
  1199. glDrawElements(GL_LINES, s->index_wireframe_len, GL_UNSIGNED_INT, nullptr);
  1200. storage->info.render.vertices_count += s->index_array_len;
  1201. } else
  1202. #endif
  1203. if (s->index_array_len > 0) {
  1204. glDrawElements(gl_primitive[s->primitive], s->index_array_len, (s->array_len >= (1 << 16)) ? GL_UNSIGNED_INT : GL_UNSIGNED_SHORT, nullptr);
  1205. storage->info.render.vertices_count += s->index_array_len;
  1206. } else {
  1207. glDrawArrays(gl_primitive[s->primitive], 0, s->array_len);
  1208. storage->info.render.vertices_count += s->array_len;
  1209. }
  1210. } break;
  1211. case VS::INSTANCE_MULTIMESH: {
  1212. RasterizerStorageGLES3::MultiMesh *multi_mesh = static_cast<RasterizerStorageGLES3::MultiMesh *>(e->owner);
  1213. RasterizerStorageGLES3::Surface *s = static_cast<RasterizerStorageGLES3::Surface *>(e->geometry);
  1214. int amount = MIN(multi_mesh->size, multi_mesh->visible_instances);
  1215. if (amount == -1) {
  1216. amount = multi_mesh->size;
  1217. }
  1218. if (!amount) {
  1219. return;
  1220. }
  1221. #ifdef DEBUG_ENABLED
  1222. if (state.debug_draw == VS::VIEWPORT_DEBUG_DRAW_WIREFRAME && s->array_wireframe_id) {
  1223. glDrawElementsInstanced(GL_LINES, s->index_wireframe_len, GL_UNSIGNED_INT, nullptr, amount);
  1224. storage->info.render.vertices_count += s->index_array_len * amount;
  1225. } else
  1226. #endif
  1227. if (s->index_array_len > 0) {
  1228. glDrawElementsInstanced(gl_primitive[s->primitive], s->index_array_len, (s->array_len >= (1 << 16)) ? GL_UNSIGNED_INT : GL_UNSIGNED_SHORT, nullptr, amount);
  1229. storage->info.render.vertices_count += s->index_array_len * amount;
  1230. } else {
  1231. glDrawArraysInstanced(gl_primitive[s->primitive], 0, s->array_len, amount);
  1232. storage->info.render.vertices_count += s->array_len * amount;
  1233. }
  1234. } break;
  1235. case VS::INSTANCE_IMMEDIATE: {
  1236. bool restore_tex = false;
  1237. const RasterizerStorageGLES3::Immediate *im = static_cast<const RasterizerStorageGLES3::Immediate *>(e->geometry);
  1238. if (im->building) {
  1239. return;
  1240. }
  1241. glBindBuffer(GL_ARRAY_BUFFER, state.immediate_buffer);
  1242. glBindVertexArray(state.immediate_array);
  1243. for (const List<RasterizerStorageGLES3::Immediate::Chunk>::Element *E = im->chunks.front(); E; E = E->next()) {
  1244. const RasterizerStorageGLES3::Immediate::Chunk &c = E->get();
  1245. if (c.vertices.empty()) {
  1246. continue;
  1247. }
  1248. int vertices = c.vertices.size();
  1249. uint32_t buf_ofs = 0;
  1250. storage->info.render.vertices_count += vertices;
  1251. if (c.texture.is_valid() && storage->texture_owner.owns(c.texture)) {
  1252. RasterizerStorageGLES3::Texture *t = storage->texture_owner.get(c.texture);
  1253. if (t->redraw_if_visible) {
  1254. VisualServerRaster::redraw_request(false);
  1255. }
  1256. t = t->get_ptr(); //resolve for proxies
  1257. #ifdef TOOLS_ENABLED
  1258. if (t->detect_3d) {
  1259. t->detect_3d(t->detect_3d_ud);
  1260. }
  1261. #endif
  1262. if (t->render_target) {
  1263. t->render_target->used_in_frame = true;
  1264. }
  1265. glActiveTexture(GL_TEXTURE0);
  1266. glBindTexture(t->target, t->tex_id);
  1267. restore_tex = true;
  1268. } else if (restore_tex) {
  1269. glActiveTexture(GL_TEXTURE0);
  1270. glBindTexture(GL_TEXTURE_2D, state.current_main_tex);
  1271. restore_tex = false;
  1272. }
  1273. if (!c.normals.empty()) {
  1274. glEnableVertexAttribArray(VS::ARRAY_NORMAL);
  1275. glBufferSubData(GL_ARRAY_BUFFER, buf_ofs, sizeof(Vector3) * vertices, c.normals.ptr());
  1276. glVertexAttribPointer(VS::ARRAY_NORMAL, 3, GL_FLOAT, false, sizeof(Vector3), CAST_INT_TO_UCHAR_PTR(buf_ofs));
  1277. buf_ofs += sizeof(Vector3) * vertices;
  1278. } else {
  1279. glDisableVertexAttribArray(VS::ARRAY_NORMAL);
  1280. }
  1281. if (!c.tangents.empty()) {
  1282. glEnableVertexAttribArray(VS::ARRAY_TANGENT);
  1283. glBufferSubData(GL_ARRAY_BUFFER, buf_ofs, sizeof(Plane) * vertices, c.tangents.ptr());
  1284. glVertexAttribPointer(VS::ARRAY_TANGENT, 4, GL_FLOAT, false, sizeof(Plane), CAST_INT_TO_UCHAR_PTR(buf_ofs));
  1285. buf_ofs += sizeof(Plane) * vertices;
  1286. } else {
  1287. glDisableVertexAttribArray(VS::ARRAY_TANGENT);
  1288. }
  1289. if (!c.colors.empty()) {
  1290. glEnableVertexAttribArray(VS::ARRAY_COLOR);
  1291. glBufferSubData(GL_ARRAY_BUFFER, buf_ofs, sizeof(Color) * vertices, c.colors.ptr());
  1292. glVertexAttribPointer(VS::ARRAY_COLOR, 4, GL_FLOAT, false, sizeof(Color), CAST_INT_TO_UCHAR_PTR(buf_ofs));
  1293. buf_ofs += sizeof(Color) * vertices;
  1294. } else {
  1295. glDisableVertexAttribArray(VS::ARRAY_COLOR);
  1296. glVertexAttrib4f(VS::ARRAY_COLOR, 1, 1, 1, 1);
  1297. }
  1298. if (!c.uvs.empty()) {
  1299. glEnableVertexAttribArray(VS::ARRAY_TEX_UV);
  1300. glBufferSubData(GL_ARRAY_BUFFER, buf_ofs, sizeof(Vector2) * vertices, c.uvs.ptr());
  1301. glVertexAttribPointer(VS::ARRAY_TEX_UV, 2, GL_FLOAT, false, sizeof(Vector2), CAST_INT_TO_UCHAR_PTR(buf_ofs));
  1302. buf_ofs += sizeof(Vector2) * vertices;
  1303. } else {
  1304. glDisableVertexAttribArray(VS::ARRAY_TEX_UV);
  1305. }
  1306. if (!c.uvs2.empty()) {
  1307. glEnableVertexAttribArray(VS::ARRAY_TEX_UV2);
  1308. glBufferSubData(GL_ARRAY_BUFFER, buf_ofs, sizeof(Vector2) * vertices, c.uvs2.ptr());
  1309. glVertexAttribPointer(VS::ARRAY_TEX_UV2, 2, GL_FLOAT, false, sizeof(Vector2), CAST_INT_TO_UCHAR_PTR(buf_ofs));
  1310. buf_ofs += sizeof(Vector2) * vertices;
  1311. } else {
  1312. glDisableVertexAttribArray(VS::ARRAY_TEX_UV2);
  1313. }
  1314. glEnableVertexAttribArray(VS::ARRAY_VERTEX);
  1315. glBufferSubData(GL_ARRAY_BUFFER, buf_ofs, sizeof(Vector3) * vertices, c.vertices.ptr());
  1316. glVertexAttribPointer(VS::ARRAY_VERTEX, 3, GL_FLOAT, false, sizeof(Vector3), CAST_INT_TO_UCHAR_PTR(buf_ofs));
  1317. glDrawArrays(gl_primitive[c.primitive], 0, c.vertices.size());
  1318. }
  1319. if (restore_tex) {
  1320. glActiveTexture(GL_TEXTURE0);
  1321. glBindTexture(GL_TEXTURE_2D, state.current_main_tex);
  1322. restore_tex = false;
  1323. }
  1324. } break;
  1325. case VS::INSTANCE_PARTICLES: {
  1326. RasterizerStorageGLES3::Particles *particles = static_cast<RasterizerStorageGLES3::Particles *>(e->owner);
  1327. RasterizerStorageGLES3::Surface *s = static_cast<RasterizerStorageGLES3::Surface *>(e->geometry);
  1328. if (!particles->use_local_coords) { //not using local coordinates? then clear transform..
  1329. state.scene_shader.set_uniform(SceneShaderGLES3::WORLD_TRANSFORM, Transform());
  1330. }
  1331. int amount = particles->amount;
  1332. if (particles->draw_order == VS::PARTICLES_DRAW_ORDER_LIFETIME) {
  1333. //split
  1334. int stride = sizeof(float) * 4 * 6;
  1335. int split = int(Math::ceil(particles->phase * particles->amount));
  1336. if (amount - split > 0) {
  1337. glEnableVertexAttribArray(8); //xform x
  1338. glVertexAttribPointer(8, 4, GL_FLOAT, GL_FALSE, stride, CAST_INT_TO_UCHAR_PTR(stride * split + sizeof(float) * 4 * 3));
  1339. glVertexAttribDivisor(8, 1);
  1340. glEnableVertexAttribArray(9); //xform y
  1341. glVertexAttribPointer(9, 4, GL_FLOAT, GL_FALSE, stride, CAST_INT_TO_UCHAR_PTR(stride * split + sizeof(float) * 4 * 4));
  1342. glVertexAttribDivisor(9, 1);
  1343. glEnableVertexAttribArray(10); //xform z
  1344. glVertexAttribPointer(10, 4, GL_FLOAT, GL_FALSE, stride, CAST_INT_TO_UCHAR_PTR(stride * split + sizeof(float) * 4 * 5));
  1345. glVertexAttribDivisor(10, 1);
  1346. glEnableVertexAttribArray(11); //color
  1347. glVertexAttribPointer(11, 4, GL_FLOAT, GL_FALSE, stride, CAST_INT_TO_UCHAR_PTR(stride * split + 0));
  1348. glVertexAttribDivisor(11, 1);
  1349. glEnableVertexAttribArray(12); //custom
  1350. glVertexAttribPointer(12, 4, GL_FLOAT, GL_FALSE, stride, CAST_INT_TO_UCHAR_PTR(stride * split + sizeof(float) * 4 * 2));
  1351. glVertexAttribDivisor(12, 1);
  1352. #ifdef DEBUG_ENABLED
  1353. if (state.debug_draw == VS::VIEWPORT_DEBUG_DRAW_WIREFRAME && s->array_wireframe_id) {
  1354. glDrawElementsInstanced(GL_LINES, s->index_wireframe_len, GL_UNSIGNED_INT, nullptr, amount - split);
  1355. storage->info.render.vertices_count += s->index_array_len * (amount - split);
  1356. } else
  1357. #endif
  1358. if (s->index_array_len > 0) {
  1359. glDrawElementsInstanced(gl_primitive[s->primitive], s->index_array_len, (s->array_len >= (1 << 16)) ? GL_UNSIGNED_INT : GL_UNSIGNED_SHORT, nullptr, amount - split);
  1360. storage->info.render.vertices_count += s->index_array_len * (amount - split);
  1361. } else {
  1362. glDrawArraysInstanced(gl_primitive[s->primitive], 0, s->array_len, amount - split);
  1363. storage->info.render.vertices_count += s->array_len * (amount - split);
  1364. }
  1365. }
  1366. if (split > 0) {
  1367. glEnableVertexAttribArray(8); //xform x
  1368. glVertexAttribPointer(8, 4, GL_FLOAT, GL_FALSE, stride, CAST_INT_TO_UCHAR_PTR(sizeof(float) * 4 * 3));
  1369. glVertexAttribDivisor(8, 1);
  1370. glEnableVertexAttribArray(9); //xform y
  1371. glVertexAttribPointer(9, 4, GL_FLOAT, GL_FALSE, stride, CAST_INT_TO_UCHAR_PTR(sizeof(float) * 4 * 4));
  1372. glVertexAttribDivisor(9, 1);
  1373. glEnableVertexAttribArray(10); //xform z
  1374. glVertexAttribPointer(10, 4, GL_FLOAT, GL_FALSE, stride, CAST_INT_TO_UCHAR_PTR(sizeof(float) * 4 * 5));
  1375. glVertexAttribDivisor(10, 1);
  1376. glEnableVertexAttribArray(11); //color
  1377. glVertexAttribPointer(11, 4, GL_FLOAT, GL_FALSE, stride, nullptr);
  1378. glVertexAttribDivisor(11, 1);
  1379. glEnableVertexAttribArray(12); //custom
  1380. glVertexAttribPointer(12, 4, GL_FLOAT, GL_FALSE, stride, CAST_INT_TO_UCHAR_PTR(sizeof(float) * 4 * 2));
  1381. glVertexAttribDivisor(12, 1);
  1382. #ifdef DEBUG_ENABLED
  1383. if (state.debug_draw == VS::VIEWPORT_DEBUG_DRAW_WIREFRAME && s->array_wireframe_id) {
  1384. glDrawElementsInstanced(GL_LINES, s->index_wireframe_len, GL_UNSIGNED_INT, nullptr, split);
  1385. storage->info.render.vertices_count += s->index_array_len * split;
  1386. } else
  1387. #endif
  1388. if (s->index_array_len > 0) {
  1389. glDrawElementsInstanced(gl_primitive[s->primitive], s->index_array_len, (s->array_len >= (1 << 16)) ? GL_UNSIGNED_INT : GL_UNSIGNED_SHORT, nullptr, split);
  1390. storage->info.render.vertices_count += s->index_array_len * split;
  1391. } else {
  1392. glDrawArraysInstanced(gl_primitive[s->primitive], 0, s->array_len, split);
  1393. storage->info.render.vertices_count += s->array_len * split;
  1394. }
  1395. }
  1396. } else {
  1397. #ifdef DEBUG_ENABLED
  1398. if (state.debug_draw == VS::VIEWPORT_DEBUG_DRAW_WIREFRAME && s->array_wireframe_id) {
  1399. glDrawElementsInstanced(GL_LINES, s->index_wireframe_len, GL_UNSIGNED_INT, nullptr, amount);
  1400. storage->info.render.vertices_count += s->index_array_len * amount;
  1401. } else
  1402. #endif
  1403. if (s->index_array_len > 0) {
  1404. glDrawElementsInstanced(gl_primitive[s->primitive], s->index_array_len, (s->array_len >= (1 << 16)) ? GL_UNSIGNED_INT : GL_UNSIGNED_SHORT, nullptr, amount);
  1405. storage->info.render.vertices_count += s->index_array_len * amount;
  1406. } else {
  1407. glDrawArraysInstanced(gl_primitive[s->primitive], 0, s->array_len, amount);
  1408. storage->info.render.vertices_count += s->array_len * amount;
  1409. }
  1410. }
  1411. } break;
  1412. default: {
  1413. }
  1414. }
  1415. }
  1416. void RasterizerSceneGLES3::_setup_light(RenderList::Element *e, const Transform &p_view_transform) {
  1417. int maxobj = state.max_forward_lights_per_object;
  1418. int *omni_indices = (int *)alloca(maxobj * sizeof(int));
  1419. int omni_count = 0;
  1420. int *spot_indices = (int *)alloca(maxobj * sizeof(int));
  1421. int spot_count = 0;
  1422. int reflection_indices[16];
  1423. int reflection_count = 0;
  1424. int lc = e->instance->light_instances.size();
  1425. if (lc) {
  1426. const RID *lights = e->instance->light_instances.ptr();
  1427. for (int i = 0; i < lc; i++) {
  1428. LightInstance *li = light_instance_owner.getornull(lights[i]);
  1429. if (!li || li->last_pass != render_pass) {
  1430. continue; // Not visible
  1431. }
  1432. if (e->instance->baked_light && li->light_ptr->bake_mode == VS::LightBakeMode::LIGHT_BAKE_ALL) {
  1433. continue; // This light is already included in the lightmap
  1434. }
  1435. if (li && li->light_ptr->type == VS::LIGHT_OMNI) {
  1436. if (omni_count < maxobj && e->instance->layer_mask & li->light_ptr->cull_mask) {
  1437. omni_indices[omni_count++] = li->light_index;
  1438. }
  1439. }
  1440. if (li && li->light_ptr->type == VS::LIGHT_SPOT) {
  1441. if (spot_count < maxobj && e->instance->layer_mask & li->light_ptr->cull_mask) {
  1442. spot_indices[spot_count++] = li->light_index;
  1443. }
  1444. }
  1445. }
  1446. }
  1447. state.scene_shader.set_uniform(SceneShaderGLES3::OMNI_LIGHT_COUNT, omni_count);
  1448. if (omni_count) {
  1449. glUniform1iv(state.scene_shader.get_uniform(SceneShaderGLES3::OMNI_LIGHT_INDICES), omni_count, omni_indices);
  1450. }
  1451. state.scene_shader.set_uniform(SceneShaderGLES3::SPOT_LIGHT_COUNT, spot_count);
  1452. if (spot_count) {
  1453. glUniform1iv(state.scene_shader.get_uniform(SceneShaderGLES3::SPOT_LIGHT_INDICES), spot_count, spot_indices);
  1454. }
  1455. int rc = e->instance->reflection_probe_instances.size();
  1456. if (rc) {
  1457. const RID *reflections = e->instance->reflection_probe_instances.ptr();
  1458. for (int i = 0; i < rc; i++) {
  1459. ReflectionProbeInstance *rpi = reflection_probe_instance_owner.getptr(reflections[i]);
  1460. if (rpi->last_pass != render_pass) { //not visible
  1461. continue;
  1462. }
  1463. if (reflection_count < maxobj) {
  1464. reflection_indices[reflection_count++] = rpi->reflection_index;
  1465. }
  1466. }
  1467. }
  1468. state.scene_shader.set_uniform(SceneShaderGLES3::REFLECTION_COUNT, reflection_count);
  1469. if (reflection_count) {
  1470. glUniform1iv(state.scene_shader.get_uniform(SceneShaderGLES3::REFLECTION_INDICES), reflection_count, reflection_indices);
  1471. }
  1472. int gi_probe_count = e->instance->gi_probe_instances.size();
  1473. if (gi_probe_count) {
  1474. const RID *ridp = e->instance->gi_probe_instances.ptr();
  1475. GIProbeInstance *gipi = gi_probe_instance_owner.getptr(ridp[0]);
  1476. float bias_scale = e->instance->baked_light ? 1 : 0;
  1477. // Normally, lightmapping uses the same texturing units than the GI probes; however, in the case of the ubershader
  1478. // that's not a good idea because some hardware/drivers (Android/Intel) may fail to render if a single texturing unit
  1479. // is used through multiple kinds of samplers in the same shader.
  1480. // Moreover, since we don't know at this point if we are going to consume these textures from the ubershader or
  1481. // a conditioned one, the fact that async compilation is enabled is enough for us to switch to the alternative
  1482. // arrangement of texturing units.
  1483. if (storage->config.async_compilation_enabled) {
  1484. glActiveTexture(GL_TEXTURE0 + storage->config.max_texture_image_units - 12);
  1485. } else {
  1486. glActiveTexture(GL_TEXTURE0 + storage->config.max_texture_image_units - 10);
  1487. }
  1488. glBindTexture(GL_TEXTURE_3D, gipi->tex_cache);
  1489. state.scene_shader.set_uniform(SceneShaderGLES3::GI_PROBE_XFORM1, gipi->transform_to_data * p_view_transform);
  1490. state.scene_shader.set_uniform(SceneShaderGLES3::GI_PROBE_BOUNDS1, gipi->bounds);
  1491. state.scene_shader.set_uniform(SceneShaderGLES3::GI_PROBE_MULTIPLIER1, gipi->probe ? gipi->probe->dynamic_range * gipi->probe->energy : 0.0);
  1492. state.scene_shader.set_uniform(SceneShaderGLES3::GI_PROBE_BIAS1, gipi->probe ? gipi->probe->bias * bias_scale : 0.0);
  1493. state.scene_shader.set_uniform(SceneShaderGLES3::GI_PROBE_NORMAL_BIAS1, gipi->probe ? gipi->probe->normal_bias * bias_scale : 0.0);
  1494. state.scene_shader.set_uniform(SceneShaderGLES3::GI_PROBE_BLEND_AMBIENT1, gipi->probe ? !gipi->probe->interior : false);
  1495. state.scene_shader.set_uniform(SceneShaderGLES3::GI_PROBE_CELL_SIZE1, gipi->cell_size_cache);
  1496. if (gi_probe_count > 1) {
  1497. GIProbeInstance *gipi2 = gi_probe_instance_owner.getptr(ridp[1]);
  1498. if (storage->config.async_compilation_enabled) {
  1499. glActiveTexture(GL_TEXTURE0 + storage->config.max_texture_image_units - 13);
  1500. } else {
  1501. glActiveTexture(GL_TEXTURE0 + storage->config.max_texture_image_units - 11);
  1502. }
  1503. glBindTexture(GL_TEXTURE_3D, gipi2->tex_cache);
  1504. state.scene_shader.set_uniform(SceneShaderGLES3::GI_PROBE_XFORM2, gipi2->transform_to_data * p_view_transform);
  1505. state.scene_shader.set_uniform(SceneShaderGLES3::GI_PROBE_BOUNDS2, gipi2->bounds);
  1506. state.scene_shader.set_uniform(SceneShaderGLES3::GI_PROBE_CELL_SIZE2, gipi2->cell_size_cache);
  1507. state.scene_shader.set_uniform(SceneShaderGLES3::GI_PROBE_MULTIPLIER2, gipi2->probe ? gipi2->probe->dynamic_range * gipi2->probe->energy : 0.0);
  1508. state.scene_shader.set_uniform(SceneShaderGLES3::GI_PROBE_BIAS2, gipi2->probe ? gipi2->probe->bias * bias_scale : 0.0);
  1509. state.scene_shader.set_uniform(SceneShaderGLES3::GI_PROBE_NORMAL_BIAS2, gipi2->probe ? gipi2->probe->normal_bias * bias_scale : 0.0);
  1510. state.scene_shader.set_uniform(SceneShaderGLES3::GI_PROBE_BLEND_AMBIENT2, gipi2->probe ? !gipi2->probe->interior : false);
  1511. state.scene_shader.set_uniform(SceneShaderGLES3::GI_PROBE2_ENABLED, true);
  1512. } else {
  1513. state.scene_shader.set_uniform(SceneShaderGLES3::GI_PROBE2_ENABLED, false);
  1514. }
  1515. } else if (!e->instance->lightmap_capture_data.empty()) {
  1516. glUniform4fv(state.scene_shader.get_uniform_location(SceneShaderGLES3::LIGHTMAP_CAPTURES), 12, (const GLfloat *)e->instance->lightmap_capture_data.ptr());
  1517. } else if (e->instance->lightmap.is_valid()) {
  1518. RasterizerStorageGLES3::Texture *lightmap = storage->texture_owner.getornull(e->instance->lightmap);
  1519. RasterizerStorageGLES3::LightmapCapture *capture = storage->lightmap_capture_data_owner.getornull(e->instance->lightmap_capture->base);
  1520. if (lightmap && capture) {
  1521. if (e->instance->lightmap_slice == -1) {
  1522. glActiveTexture(GL_TEXTURE0 + storage->config.max_texture_image_units - 10);
  1523. glBindTexture(GL_TEXTURE_2D, lightmap->tex_id);
  1524. } else {
  1525. glActiveTexture(GL_TEXTURE0 + storage->config.max_texture_image_units - 11);
  1526. glBindTexture(GL_TEXTURE_2D_ARRAY, lightmap->tex_id);
  1527. state.scene_shader.set_uniform(SceneShaderGLES3::LIGHTMAP_LAYER, e->instance->lightmap_slice);
  1528. }
  1529. const Rect2 &uvr = e->instance->lightmap_uv_rect;
  1530. state.scene_shader.set_uniform(SceneShaderGLES3::LIGHTMAP_UV_RECT, Color(uvr.get_position().x, uvr.get_position().y, uvr.get_size().x, uvr.get_size().y));
  1531. if (storage->config.use_lightmap_filter_bicubic) {
  1532. state.scene_shader.set_uniform(SceneShaderGLES3::LIGHTMAP_TEXTURE_SIZE, Vector2(lightmap->width, lightmap->height));
  1533. }
  1534. state.scene_shader.set_uniform(SceneShaderGLES3::LIGHTMAP_ENERGY, capture->energy);
  1535. }
  1536. }
  1537. }
  1538. void RasterizerSceneGLES3::_set_cull(bool p_front, bool p_disabled, bool p_reverse_cull) {
  1539. bool front = p_front;
  1540. if (p_reverse_cull) {
  1541. front = !front;
  1542. }
  1543. if (p_disabled != state.cull_disabled) {
  1544. if (p_disabled) {
  1545. glDisable(GL_CULL_FACE);
  1546. } else {
  1547. glEnable(GL_CULL_FACE);
  1548. }
  1549. state.cull_disabled = p_disabled;
  1550. }
  1551. if (front != state.cull_front) {
  1552. glCullFace(front ? GL_FRONT : GL_BACK);
  1553. state.cull_front = front;
  1554. }
  1555. }
  1556. void RasterizerSceneGLES3::_render_list(RenderList::Element **p_elements, int p_element_count, const Transform &p_view_transform, const CameraMatrix &p_projection, RasterizerStorageGLES3::Sky *p_sky, bool p_reverse_cull, bool p_alpha_pass, bool p_shadow, bool p_directional_add, bool p_directional_shadows) {
  1557. glBindBufferBase(GL_UNIFORM_BUFFER, 0, state.scene_ubo); //bind globals ubo
  1558. bool use_radiance_map = false;
  1559. if (!p_shadow && !p_directional_add) {
  1560. glBindBufferBase(GL_UNIFORM_BUFFER, 2, state.env_radiance_ubo); //bind environment radiance info
  1561. if (p_sky != nullptr) {
  1562. if (storage->config.use_texture_array_environment) {
  1563. glActiveTexture(GL_TEXTURE0 + storage->config.max_texture_image_units - 3);
  1564. glBindTexture(GL_TEXTURE_2D_ARRAY, p_sky->radiance);
  1565. } else {
  1566. glActiveTexture(GL_TEXTURE0 + storage->config.max_texture_image_units - 2);
  1567. glBindTexture(GL_TEXTURE_2D, p_sky->radiance);
  1568. }
  1569. glActiveTexture(GL_TEXTURE0 + storage->config.max_texture_image_units - 7);
  1570. glBindTexture(GL_TEXTURE_2D, p_sky->irradiance);
  1571. state.scene_shader.set_conditional(SceneShaderGLES3::USE_RADIANCE_MAP, true);
  1572. state.scene_shader.set_conditional(SceneShaderGLES3::USE_RADIANCE_MAP_ARRAY, storage->config.use_texture_array_environment);
  1573. use_radiance_map = true;
  1574. } else {
  1575. state.scene_shader.set_conditional(SceneShaderGLES3::USE_RADIANCE_MAP, false);
  1576. state.scene_shader.set_conditional(SceneShaderGLES3::USE_RADIANCE_MAP_ARRAY, false);
  1577. }
  1578. } else {
  1579. state.scene_shader.set_conditional(SceneShaderGLES3::USE_RADIANCE_MAP, false);
  1580. state.scene_shader.set_conditional(SceneShaderGLES3::USE_RADIANCE_MAP_ARRAY, false);
  1581. }
  1582. state.cull_front = false;
  1583. state.cull_disabled = false;
  1584. glCullFace(GL_BACK);
  1585. glEnable(GL_CULL_FACE);
  1586. state.current_depth_test = true;
  1587. glEnable(GL_DEPTH_TEST);
  1588. state.current_blend_mode = -1;
  1589. state.current_line_width = -1;
  1590. state.current_depth_draw = -1;
  1591. RasterizerStorageGLES3::Material *prev_material = nullptr;
  1592. RasterizerStorageGLES3::Geometry *prev_geometry = nullptr;
  1593. RasterizerStorageGLES3::GeometryOwner *prev_owner = nullptr;
  1594. VS::InstanceType prev_base_type = VS::INSTANCE_MAX;
  1595. int current_blend_mode = -1;
  1596. uint32_t prev_shading = 0xFFFFFFFF;
  1597. state.scene_shader.set_conditional(SceneShaderGLES3::SHADELESS, true); //by default unshaded (easier to set)
  1598. RasterizerStorageGLES3::Skeleton *prev_skeleton = nullptr;
  1599. state.scene_shader.set_conditional(SceneShaderGLES3::USE_SKELETON, false);
  1600. bool first = true;
  1601. bool prev_use_instancing = false;
  1602. state.scene_shader.set_conditional(SceneShaderGLES3::USE_INSTANCING, false);
  1603. bool prev_octahedral_compression = false;
  1604. state.scene_shader.set_conditional(SceneShaderGLES3::ENABLE_OCTAHEDRAL_COMPRESSION, false);
  1605. storage->info.render.draw_call_count += p_element_count;
  1606. bool prev_opaque_prepass = false;
  1607. state.scene_shader.set_conditional(SceneShaderGLES3::USE_OPAQUE_PREPASS, false);
  1608. for (int i = 0; i < p_element_count; i++) {
  1609. RenderList::Element *e = p_elements[i];
  1610. RasterizerStorageGLES3::Material *material = e->material;
  1611. RasterizerStorageGLES3::Skeleton *skeleton = nullptr;
  1612. if (e->instance->skeleton.is_valid()) {
  1613. skeleton = storage->skeleton_owner.getornull(e->instance->skeleton);
  1614. }
  1615. bool rebind = first;
  1616. uint32_t shading = (e->sort_key >> RenderList::SORT_KEY_SHADING_SHIFT) & RenderList::SORT_KEY_SHADING_MASK;
  1617. if (!p_shadow) {
  1618. bool use_directional = directional_light != nullptr;
  1619. if (p_directional_add) {
  1620. use_directional = use_directional && !(e->instance->baked_light && directional_light->light_ptr->bake_mode == VS::LightBakeMode::LIGHT_BAKE_ALL);
  1621. use_directional = use_directional && ((e->instance->layer_mask & directional_light->light_ptr->cull_mask) != 0);
  1622. use_directional = use_directional && ((e->sort_key & SORT_KEY_UNSHADED_FLAG) == 0);
  1623. if (!use_directional) {
  1624. continue; // It's a directional-only pass and the directional light is disabled
  1625. }
  1626. } else {
  1627. use_directional = use_directional && (e->sort_key & SORT_KEY_NO_DIRECTIONAL_FLAG) == 0;
  1628. }
  1629. if (shading != prev_shading) {
  1630. if (e->sort_key & SORT_KEY_UNSHADED_FLAG) {
  1631. state.scene_shader.set_conditional(SceneShaderGLES3::SHADELESS, true);
  1632. state.scene_shader.set_conditional(SceneShaderGLES3::USE_FORWARD_LIGHTING, false);
  1633. state.scene_shader.set_conditional(SceneShaderGLES3::USE_VERTEX_LIGHTING, false);
  1634. state.scene_shader.set_conditional(SceneShaderGLES3::USE_LIGHT_DIRECTIONAL, false);
  1635. state.scene_shader.set_conditional(SceneShaderGLES3::LIGHT_DIRECTIONAL_SHADOW, false);
  1636. state.scene_shader.set_conditional(SceneShaderGLES3::LIGHT_USE_PSSM4, false);
  1637. state.scene_shader.set_conditional(SceneShaderGLES3::LIGHT_USE_PSSM2, false);
  1638. state.scene_shader.set_conditional(SceneShaderGLES3::LIGHT_USE_PSSM_BLEND, false);
  1639. state.scene_shader.set_conditional(SceneShaderGLES3::LIGHT_USE_PSSM_BLEND, false);
  1640. state.scene_shader.set_conditional(SceneShaderGLES3::SHADOW_MODE_PCF_5, false);
  1641. state.scene_shader.set_conditional(SceneShaderGLES3::SHADOW_MODE_PCF_13, false);
  1642. state.scene_shader.set_conditional(SceneShaderGLES3::USE_GI_PROBES, false);
  1643. state.scene_shader.set_conditional(SceneShaderGLES3::USE_LIGHTMAP_CAPTURE, false);
  1644. state.scene_shader.set_conditional(SceneShaderGLES3::USE_LIGHTMAP, false);
  1645. state.scene_shader.set_conditional(SceneShaderGLES3::USE_LIGHTMAP_LAYERED, false);
  1646. state.scene_shader.set_conditional(SceneShaderGLES3::USE_RADIANCE_MAP, false);
  1647. state.scene_shader.set_conditional(SceneShaderGLES3::USE_CONTACT_SHADOWS, false);
  1648. //state.scene_shader.set_conditional(SceneShaderGLES3::SHADELESS,true);
  1649. } else {
  1650. state.scene_shader.set_conditional(SceneShaderGLES3::USE_GI_PROBES, e->instance->gi_probe_instances.size() > 0);
  1651. state.scene_shader.set_conditional(SceneShaderGLES3::USE_LIGHTMAP, e->instance->lightmap.is_valid() && e->instance->gi_probe_instances.size() == 0);
  1652. state.scene_shader.set_conditional(SceneShaderGLES3::USE_LIGHTMAP_LAYERED, e->instance->lightmap_slice != -1);
  1653. state.scene_shader.set_conditional(SceneShaderGLES3::USE_LIGHTMAP_CAPTURE, !e->instance->lightmap_capture_data.empty() && !e->instance->lightmap.is_valid() && e->instance->gi_probe_instances.size() == 0);
  1654. state.scene_shader.set_conditional(SceneShaderGLES3::SHADELESS, false);
  1655. state.scene_shader.set_conditional(SceneShaderGLES3::USE_FORWARD_LIGHTING, !p_directional_add);
  1656. state.scene_shader.set_conditional(SceneShaderGLES3::USE_VERTEX_LIGHTING, (e->sort_key & SORT_KEY_VERTEX_LIT_FLAG));
  1657. state.scene_shader.set_conditional(SceneShaderGLES3::USE_LIGHT_DIRECTIONAL, use_directional);
  1658. state.scene_shader.set_conditional(SceneShaderGLES3::LIGHT_DIRECTIONAL_SHADOW, false);
  1659. state.scene_shader.set_conditional(SceneShaderGLES3::LIGHT_USE_PSSM4, false);
  1660. state.scene_shader.set_conditional(SceneShaderGLES3::LIGHT_USE_PSSM2, false);
  1661. state.scene_shader.set_conditional(SceneShaderGLES3::LIGHT_USE_PSSM_BLEND, false);
  1662. state.scene_shader.set_conditional(SceneShaderGLES3::SHADOW_MODE_PCF_5, shadow_filter_mode == SHADOW_FILTER_PCF5);
  1663. state.scene_shader.set_conditional(SceneShaderGLES3::SHADOW_MODE_PCF_13, shadow_filter_mode == SHADOW_FILTER_PCF13);
  1664. state.scene_shader.set_conditional(SceneShaderGLES3::USE_RADIANCE_MAP, use_radiance_map);
  1665. state.scene_shader.set_conditional(SceneShaderGLES3::USE_CONTACT_SHADOWS, state.used_contact_shadows);
  1666. if (use_directional) {
  1667. if (p_directional_shadows && directional_light->light_ptr->shadow) {
  1668. state.scene_shader.set_conditional(SceneShaderGLES3::LIGHT_DIRECTIONAL_SHADOW, true);
  1669. switch (directional_light->light_ptr->directional_shadow_mode) {
  1670. case VS::LIGHT_DIRECTIONAL_SHADOW_ORTHOGONAL:
  1671. break; //none
  1672. case VS::LIGHT_DIRECTIONAL_SHADOW_PARALLEL_2_SPLITS:
  1673. state.scene_shader.set_conditional(SceneShaderGLES3::LIGHT_USE_PSSM2, true);
  1674. state.scene_shader.set_conditional(SceneShaderGLES3::LIGHT_USE_PSSM_BLEND, directional_light->light_ptr->directional_blend_splits);
  1675. break;
  1676. case VS::LIGHT_DIRECTIONAL_SHADOW_PARALLEL_4_SPLITS:
  1677. state.scene_shader.set_conditional(SceneShaderGLES3::LIGHT_USE_PSSM4, true);
  1678. state.scene_shader.set_conditional(SceneShaderGLES3::LIGHT_USE_PSSM_BLEND, directional_light->light_ptr->directional_blend_splits);
  1679. break;
  1680. }
  1681. }
  1682. }
  1683. }
  1684. rebind = true;
  1685. }
  1686. if (p_alpha_pass || p_directional_add) {
  1687. int desired_blend_mode;
  1688. if (p_directional_add) {
  1689. desired_blend_mode = RasterizerStorageGLES3::Shader::Spatial::BLEND_MODE_ADD;
  1690. } else {
  1691. desired_blend_mode = material->shader->spatial.blend_mode;
  1692. }
  1693. if (desired_blend_mode != current_blend_mode) {
  1694. switch (desired_blend_mode) {
  1695. case RasterizerStorageGLES3::Shader::Spatial::BLEND_MODE_MIX: {
  1696. glBlendEquation(GL_FUNC_ADD);
  1697. if (storage->frame.current_rt && storage->frame.current_rt->flags[RasterizerStorage::RENDER_TARGET_TRANSPARENT]) {
  1698. glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
  1699. } else {
  1700. glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_ZERO, GL_ONE);
  1701. }
  1702. } break;
  1703. case RasterizerStorageGLES3::Shader::Spatial::BLEND_MODE_ADD: {
  1704. glBlendEquation(GL_FUNC_ADD);
  1705. glBlendFunc(p_alpha_pass ? GL_SRC_ALPHA : GL_ONE, GL_ONE);
  1706. } break;
  1707. case RasterizerStorageGLES3::Shader::Spatial::BLEND_MODE_SUB: {
  1708. glBlendEquation(GL_FUNC_REVERSE_SUBTRACT);
  1709. glBlendFunc(GL_SRC_ALPHA, GL_ONE);
  1710. } break;
  1711. case RasterizerStorageGLES3::Shader::Spatial::BLEND_MODE_MUL: {
  1712. glBlendEquation(GL_FUNC_ADD);
  1713. if (storage->frame.current_rt && storage->frame.current_rt->flags[RasterizerStorage::RENDER_TARGET_TRANSPARENT]) {
  1714. glBlendFuncSeparate(GL_DST_COLOR, GL_ZERO, GL_DST_ALPHA, GL_ZERO);
  1715. } else {
  1716. glBlendFuncSeparate(GL_DST_COLOR, GL_ZERO, GL_ZERO, GL_ONE);
  1717. }
  1718. } break;
  1719. }
  1720. current_blend_mode = desired_blend_mode;
  1721. }
  1722. }
  1723. }
  1724. bool use_opaque_prepass = e->sort_key & RenderList::SORT_KEY_OPAQUE_PRE_PASS;
  1725. if (use_opaque_prepass != prev_opaque_prepass) {
  1726. state.scene_shader.set_conditional(SceneShaderGLES3::USE_OPAQUE_PREPASS, use_opaque_prepass);
  1727. rebind = true;
  1728. }
  1729. bool use_instancing = e->instance->base_type == VS::INSTANCE_MULTIMESH || e->instance->base_type == VS::INSTANCE_PARTICLES;
  1730. if (use_instancing != prev_use_instancing) {
  1731. state.scene_shader.set_conditional(SceneShaderGLES3::USE_INSTANCING, use_instancing);
  1732. rebind = true;
  1733. }
  1734. if (prev_skeleton != skeleton) {
  1735. if ((prev_skeleton == nullptr) != (skeleton == nullptr)) {
  1736. state.scene_shader.set_conditional(SceneShaderGLES3::USE_SKELETON, skeleton != nullptr);
  1737. rebind = true;
  1738. }
  1739. if (skeleton) {
  1740. glActiveTexture(GL_TEXTURE0 + storage->config.max_texture_image_units - 1);
  1741. glBindTexture(GL_TEXTURE_2D, skeleton->texture);
  1742. }
  1743. }
  1744. state.scene_shader.set_conditional(SceneShaderGLES3::USE_PHYSICAL_LIGHT_ATTENUATION, storage->config.use_physical_light_attenuation);
  1745. bool octahedral_compression = e->instance->base_type != VS::INSTANCE_IMMEDIATE &&
  1746. ((RasterizerStorageGLES3::Surface *)e->geometry)->format & VisualServer::ArrayFormat::ARRAY_FLAG_USE_OCTAHEDRAL_COMPRESSION &&
  1747. !(((RasterizerStorageGLES3::Surface *)e->geometry)->blend_shapes.size() && e->instance->blend_values.size());
  1748. if (octahedral_compression != prev_octahedral_compression) {
  1749. state.scene_shader.set_conditional(SceneShaderGLES3::ENABLE_OCTAHEDRAL_COMPRESSION, octahedral_compression);
  1750. rebind = true;
  1751. }
  1752. if (material != prev_material || rebind) {
  1753. storage->info.render.material_switch_count++;
  1754. rebind = _setup_material(material, use_opaque_prepass, p_alpha_pass);
  1755. if (rebind) {
  1756. storage->info.render.shader_rebind_count++;
  1757. }
  1758. }
  1759. if (!ShaderGLES3::get_active()) {
  1760. continue;
  1761. }
  1762. if (!(e->sort_key & SORT_KEY_UNSHADED_FLAG) && !p_directional_add && !p_shadow) {
  1763. _setup_light(e, p_view_transform);
  1764. }
  1765. if (e->owner != prev_owner || prev_base_type != e->instance->base_type || prev_geometry != e->geometry) {
  1766. _setup_geometry(e, p_view_transform);
  1767. storage->info.render.surface_switch_count++;
  1768. }
  1769. _set_cull(e->sort_key & RenderList::SORT_KEY_MIRROR_FLAG, e->sort_key & RenderList::SORT_KEY_CULL_DISABLED_FLAG, p_reverse_cull);
  1770. state.scene_shader.set_uniform(SceneShaderGLES3::WORLD_TRANSFORM, e->instance->transform);
  1771. _render_geometry(e);
  1772. prev_material = material;
  1773. prev_base_type = e->instance->base_type;
  1774. prev_geometry = e->geometry;
  1775. prev_owner = e->owner;
  1776. prev_shading = shading;
  1777. prev_skeleton = skeleton;
  1778. prev_use_instancing = use_instancing;
  1779. prev_octahedral_compression = octahedral_compression;
  1780. prev_opaque_prepass = use_opaque_prepass;
  1781. first = false;
  1782. }
  1783. glBindVertexArray(0);
  1784. state.scene_shader.set_conditional(SceneShaderGLES3::ENABLE_OCTAHEDRAL_COMPRESSION, false);
  1785. state.scene_shader.set_conditional(SceneShaderGLES3::USE_INSTANCING, false);
  1786. state.scene_shader.set_conditional(SceneShaderGLES3::USE_SKELETON, false);
  1787. state.scene_shader.set_conditional(SceneShaderGLES3::USE_RADIANCE_MAP, false);
  1788. state.scene_shader.set_conditional(SceneShaderGLES3::USE_FORWARD_LIGHTING, false);
  1789. state.scene_shader.set_conditional(SceneShaderGLES3::USE_LIGHT_DIRECTIONAL, false);
  1790. state.scene_shader.set_conditional(SceneShaderGLES3::LIGHT_DIRECTIONAL_SHADOW, false);
  1791. state.scene_shader.set_conditional(SceneShaderGLES3::LIGHT_USE_PSSM4, false);
  1792. state.scene_shader.set_conditional(SceneShaderGLES3::LIGHT_USE_PSSM2, false);
  1793. state.scene_shader.set_conditional(SceneShaderGLES3::LIGHT_USE_PSSM_BLEND, false);
  1794. state.scene_shader.set_conditional(SceneShaderGLES3::SHADELESS, false);
  1795. state.scene_shader.set_conditional(SceneShaderGLES3::SHADOW_MODE_PCF_5, false);
  1796. state.scene_shader.set_conditional(SceneShaderGLES3::SHADOW_MODE_PCF_13, false);
  1797. state.scene_shader.set_conditional(SceneShaderGLES3::USE_GI_PROBES, false);
  1798. state.scene_shader.set_conditional(SceneShaderGLES3::USE_LIGHTMAP, false);
  1799. state.scene_shader.set_conditional(SceneShaderGLES3::USE_LIGHTMAP_LAYERED, false);
  1800. state.scene_shader.set_conditional(SceneShaderGLES3::USE_LIGHTMAP_CAPTURE, false);
  1801. state.scene_shader.set_conditional(SceneShaderGLES3::USE_CONTACT_SHADOWS, false);
  1802. state.scene_shader.set_conditional(SceneShaderGLES3::USE_VERTEX_LIGHTING, false);
  1803. state.scene_shader.set_conditional(SceneShaderGLES3::USE_OPAQUE_PREPASS, false);
  1804. }
  1805. void RasterizerSceneGLES3::_add_geometry(RasterizerStorageGLES3::Geometry *p_geometry, InstanceBase *p_instance, RasterizerStorageGLES3::GeometryOwner *p_owner, int p_material, bool p_depth_pass, bool p_shadow_pass) {
  1806. RasterizerStorageGLES3::Material *m = nullptr;
  1807. RID m_src = p_instance->material_override.is_valid() ? p_instance->material_override : (p_material >= 0 ? p_instance->materials[p_material] : p_geometry->material);
  1808. if (state.debug_draw == VS::VIEWPORT_DEBUG_DRAW_OVERDRAW) {
  1809. m_src = default_overdraw_material;
  1810. }
  1811. /*
  1812. #ifdef DEBUG_ENABLED
  1813. if (current_debug==VS::SCENARIO_DEBUG_OVERDRAW) {
  1814. m_src=overdraw_material;
  1815. }
  1816. #endif
  1817. */
  1818. if (m_src.is_valid()) {
  1819. m = storage->material_owner.getornull(m_src);
  1820. if (!m->shader || !m->shader->valid) {
  1821. m = nullptr;
  1822. }
  1823. }
  1824. if (!m) {
  1825. m = storage->material_owner.getptr(default_material);
  1826. }
  1827. ERR_FAIL_COND(!m);
  1828. _add_geometry_with_material(p_geometry, p_instance, p_owner, m, p_depth_pass, p_shadow_pass);
  1829. while (m->next_pass.is_valid()) {
  1830. m = storage->material_owner.getornull(m->next_pass);
  1831. if (!m || !m->shader || !m->shader->valid) {
  1832. break;
  1833. }
  1834. _add_geometry_with_material(p_geometry, p_instance, p_owner, m, p_depth_pass, p_shadow_pass);
  1835. }
  1836. // Repeat the "nested chain" logic also for the overlay
  1837. if (p_instance->material_overlay.is_valid()) {
  1838. m = storage->material_owner.getornull(p_instance->material_overlay);
  1839. if (!m || !m->shader || !m->shader->valid) {
  1840. return;
  1841. }
  1842. _add_geometry_with_material(p_geometry, p_instance, p_owner, m, p_depth_pass, p_shadow_pass);
  1843. while (m->next_pass.is_valid()) {
  1844. m = storage->material_owner.getornull(m->next_pass);
  1845. if (!m || !m->shader || !m->shader->valid) {
  1846. break;
  1847. }
  1848. _add_geometry_with_material(p_geometry, p_instance, p_owner, m, p_depth_pass, p_shadow_pass);
  1849. }
  1850. }
  1851. }
  1852. void RasterizerSceneGLES3::_add_geometry_with_material(RasterizerStorageGLES3::Geometry *p_geometry, InstanceBase *p_instance, RasterizerStorageGLES3::GeometryOwner *p_owner, RasterizerStorageGLES3::Material *p_material, bool p_depth_pass, bool p_shadow_pass) {
  1853. bool has_base_alpha = (p_material->shader->spatial.uses_alpha && !p_material->shader->spatial.uses_alpha_scissor) || p_material->shader->spatial.uses_screen_texture || p_material->shader->spatial.uses_depth_texture;
  1854. bool has_blend_alpha = p_material->shader->spatial.blend_mode != RasterizerStorageGLES3::Shader::Spatial::BLEND_MODE_MIX;
  1855. bool has_alpha = has_base_alpha || has_blend_alpha;
  1856. bool mirror = p_instance->mirror;
  1857. bool no_cull = false;
  1858. if (p_material->shader->spatial.cull_mode == RasterizerStorageGLES3::Shader::Spatial::CULL_MODE_DISABLED) {
  1859. no_cull = true;
  1860. mirror = false;
  1861. } else if (p_material->shader->spatial.cull_mode == RasterizerStorageGLES3::Shader::Spatial::CULL_MODE_FRONT) {
  1862. mirror = !mirror;
  1863. }
  1864. if (p_material->shader->spatial.uses_sss) {
  1865. state.used_sss = true;
  1866. }
  1867. if (p_material->shader->spatial.uses_screen_texture) {
  1868. state.used_screen_texture = true;
  1869. }
  1870. if (p_material->shader->spatial.uses_depth_texture) {
  1871. state.used_depth_texture = true;
  1872. }
  1873. if (p_depth_pass) {
  1874. if (has_blend_alpha || p_material->shader->spatial.uses_depth_texture || ((has_base_alpha || p_instance->cast_shadows == VS::SHADOW_CASTING_SETTING_OFF) && p_material->shader->spatial.depth_draw_mode != RasterizerStorageGLES3::Shader::Spatial::DEPTH_DRAW_ALPHA_PREPASS) || p_material->shader->spatial.depth_draw_mode == RasterizerStorageGLES3::Shader::Spatial::DEPTH_DRAW_NEVER || p_material->shader->spatial.no_depth_test) {
  1875. return; //bye
  1876. }
  1877. if (!p_shadow_pass && !p_material->shader->shader->is_custom_code_ready_for_render(p_material->shader->custom_code_id)) {
  1878. // The shader is not guaranteed to be able to render (i.e., a not yet ready async hidden one);
  1879. // skip depth rendering because otherwise we risk masking out pixels that won't get written to at the actual render pass
  1880. return;
  1881. }
  1882. if (!p_material->shader->spatial.uses_alpha_scissor && !p_material->shader->spatial.writes_modelview_or_projection && !p_material->shader->spatial.uses_vertex && !p_material->shader->spatial.uses_discard && p_material->shader->spatial.depth_draw_mode != RasterizerStorageGLES3::Shader::Spatial::DEPTH_DRAW_ALPHA_PREPASS) {
  1883. //shader does not use discard and does not write a vertex position, use generic material
  1884. if (p_instance->cast_shadows == VS::SHADOW_CASTING_SETTING_DOUBLE_SIDED) {
  1885. p_material = storage->material_owner.getptr(!p_shadow_pass && p_material->shader->spatial.uses_world_coordinates ? default_worldcoord_material_twosided : default_material_twosided);
  1886. no_cull = true;
  1887. mirror = false;
  1888. } else {
  1889. p_material = storage->material_owner.getptr(!p_shadow_pass && p_material->shader->spatial.uses_world_coordinates ? default_worldcoord_material : default_material);
  1890. }
  1891. }
  1892. has_alpha = false;
  1893. }
  1894. RenderList::Element *e = (has_alpha || p_material->shader->spatial.no_depth_test) ? render_list.add_alpha_element() : render_list.add_element();
  1895. if (!e) {
  1896. return;
  1897. }
  1898. e->geometry = p_geometry;
  1899. e->material = p_material;
  1900. e->instance = p_instance;
  1901. e->owner = p_owner;
  1902. e->sort_key = 0;
  1903. if (e->geometry->last_pass != render_pass) {
  1904. e->geometry->last_pass = render_pass;
  1905. e->geometry->index = current_geometry_index++;
  1906. }
  1907. // We sort only by the first directional light. The rest of directional lights will be drawn in additive passes that are skipped if disabled.
  1908. if (first_directional_light.is_valid() && light_instance_owner.owns(first_directional_light)) {
  1909. RasterizerStorageGLES3::Light *directional = light_instance_owner.getptr(first_directional_light)->light_ptr;
  1910. if ((e->instance->layer_mask & directional->cull_mask) == 0 || (e->instance->baked_light && directional->bake_mode == VS::LightBakeMode::LIGHT_BAKE_ALL)) {
  1911. e->sort_key |= SORT_KEY_NO_DIRECTIONAL_FLAG;
  1912. }
  1913. }
  1914. e->sort_key |= uint64_t(e->geometry->index) << RenderList::SORT_KEY_GEOMETRY_INDEX_SHIFT;
  1915. e->sort_key |= uint64_t(e->instance->base_type) << RenderList::SORT_KEY_GEOMETRY_TYPE_SHIFT;
  1916. if (e->material->last_pass != render_pass) {
  1917. e->material->last_pass = render_pass;
  1918. e->material->index = current_material_index++;
  1919. }
  1920. e->sort_key |= uint64_t(e->material->index) << RenderList::SORT_KEY_MATERIAL_INDEX_SHIFT;
  1921. e->sort_key |= uint64_t(e->instance->depth_layer) << RenderList::SORT_KEY_OPAQUE_DEPTH_LAYER_SHIFT;
  1922. if (!p_depth_pass) {
  1923. if (e->instance->gi_probe_instances.size()) {
  1924. e->sort_key |= SORT_KEY_GI_PROBES_FLAG;
  1925. }
  1926. if (e->instance->lightmap.is_valid()) {
  1927. e->sort_key |= SORT_KEY_LIGHTMAP_FLAG;
  1928. if (e->instance->lightmap_slice != -1) {
  1929. e->sort_key |= SORT_KEY_LIGHTMAP_LAYERED_FLAG;
  1930. }
  1931. }
  1932. if (!e->instance->lightmap_capture_data.empty()) {
  1933. e->sort_key |= SORT_KEY_LIGHTMAP_CAPTURE_FLAG;
  1934. }
  1935. e->sort_key |= (uint64_t(p_material->render_priority) + 128) << RenderList::SORT_KEY_PRIORITY_SHIFT;
  1936. }
  1937. /*
  1938. if (e->geometry->type==RasterizerStorageGLES3::Geometry::GEOMETRY_MULTISURFACE)
  1939. e->sort_flags|=RenderList::SORT_FLAG_INSTANCING;
  1940. */
  1941. if (mirror) {
  1942. e->sort_key |= RenderList::SORT_KEY_MIRROR_FLAG;
  1943. }
  1944. if (no_cull) {
  1945. e->sort_key |= RenderList::SORT_KEY_CULL_DISABLED_FLAG;
  1946. }
  1947. //e->light_type=0xFF; // no lights!
  1948. if (p_depth_pass || p_material->shader->spatial.unshaded || state.debug_draw == VS::VIEWPORT_DEBUG_DRAW_UNSHADED) {
  1949. e->sort_key |= SORT_KEY_UNSHADED_FLAG;
  1950. }
  1951. if (p_depth_pass && p_material->shader->spatial.depth_draw_mode == RasterizerStorageGLES3::Shader::Spatial::DEPTH_DRAW_ALPHA_PREPASS) {
  1952. e->sort_key |= RenderList::SORT_KEY_OPAQUE_PRE_PASS;
  1953. }
  1954. if (!p_depth_pass && (p_material->shader->spatial.uses_vertex_lighting || storage->config.force_vertex_shading)) {
  1955. e->sort_key |= SORT_KEY_VERTEX_LIT_FLAG;
  1956. }
  1957. if (p_material->shader->spatial.uses_time) {
  1958. VisualServerRaster::redraw_request(false);
  1959. }
  1960. }
  1961. void RasterizerSceneGLES3::_draw_sky(RasterizerStorageGLES3::Sky *p_sky, const CameraMatrix &p_projection, const Transform &p_transform, bool p_vflip, float p_custom_fov, float p_energy, const Basis &p_sky_orientation) {
  1962. ERR_FAIL_COND(!p_sky);
  1963. RasterizerStorageGLES3::Texture *tex = storage->texture_owner.getornull(p_sky->panorama);
  1964. ERR_FAIL_COND(!tex);
  1965. glActiveTexture(GL_TEXTURE0);
  1966. tex = tex->get_ptr(); //resolve for proxies
  1967. glBindTexture(tex->target, tex->tex_id);
  1968. if (storage->config.srgb_decode_supported && tex->srgb && !tex->using_srgb) {
  1969. glTexParameteri(tex->target, _TEXTURE_SRGB_DECODE_EXT, _DECODE_EXT);
  1970. tex->using_srgb = true;
  1971. #ifdef TOOLS_ENABLED
  1972. if (!(tex->flags & VS::TEXTURE_FLAG_CONVERT_TO_LINEAR)) {
  1973. tex->flags |= VS::TEXTURE_FLAG_CONVERT_TO_LINEAR;
  1974. //notify that texture must be set to linear beforehand, so it works in other platforms when exported
  1975. }
  1976. #endif
  1977. }
  1978. glDepthMask(GL_TRUE);
  1979. glEnable(GL_DEPTH_TEST);
  1980. glDisable(GL_CULL_FACE);
  1981. glDisable(GL_BLEND);
  1982. glDepthFunc(GL_LEQUAL);
  1983. glColorMask(1, 1, 1, 1);
  1984. // Camera
  1985. CameraMatrix camera;
  1986. if (p_custom_fov) {
  1987. float near_plane = p_projection.get_z_near();
  1988. float far_plane = p_projection.get_z_far();
  1989. float aspect = p_projection.get_aspect();
  1990. camera.set_perspective(p_custom_fov, aspect, near_plane, far_plane);
  1991. } else {
  1992. camera = p_projection;
  1993. }
  1994. float flip_sign = p_vflip ? -1 : 1;
  1995. /*
  1996. If matrix[2][0] or matrix[2][1] we're dealing with an asymmetrical projection matrix. This is the case for stereoscopic rendering (i.e. VR).
  1997. To ensure the image rendered is perspective correct we need to move some logic into the shader. For this the USE_ASYM_PANO option is introduced.
  1998. It also means the uv coordinates are ignored in this mode and we don't need our loop.
  1999. */
  2000. bool asymmetrical = ((camera.matrix[2][0] != 0.0) || (camera.matrix[2][1] != 0.0));
  2001. Vector3 vertices[8] = {
  2002. Vector3(-1, -1 * flip_sign, 1),
  2003. Vector3(0, 1, 0),
  2004. Vector3(1, -1 * flip_sign, 1),
  2005. Vector3(1, 1, 0),
  2006. Vector3(1, 1 * flip_sign, 1),
  2007. Vector3(1, 0, 0),
  2008. Vector3(-1, 1 * flip_sign, 1),
  2009. Vector3(0, 0, 0)
  2010. };
  2011. if (!asymmetrical) {
  2012. Vector2 vp_he = camera.get_viewport_half_extents();
  2013. float zn;
  2014. zn = p_projection.get_z_near();
  2015. for (int i = 0; i < 4; i++) {
  2016. Vector3 uv = vertices[i * 2 + 1];
  2017. uv.x = (uv.x * 2.0 - 1.0) * vp_he.x;
  2018. uv.y = -(uv.y * 2.0 - 1.0) * vp_he.y;
  2019. uv.z = -zn;
  2020. vertices[i * 2 + 1] = p_transform.basis.xform(uv).normalized();
  2021. vertices[i * 2 + 1].z = -vertices[i * 2 + 1].z;
  2022. }
  2023. }
  2024. glBindBuffer(GL_ARRAY_BUFFER, state.sky_verts);
  2025. glBufferData(GL_ARRAY_BUFFER, sizeof(Vector3) * 8, vertices, GL_DYNAMIC_DRAW);
  2026. glBindBuffer(GL_ARRAY_BUFFER, 0); //unbind
  2027. glBindVertexArray(state.sky_array);
  2028. storage->shaders.copy.set_conditional(CopyShaderGLES3::USE_ASYM_PANO, asymmetrical);
  2029. storage->shaders.copy.set_conditional(CopyShaderGLES3::USE_PANORAMA, !asymmetrical);
  2030. storage->shaders.copy.set_conditional(CopyShaderGLES3::USE_MULTIPLIER, true);
  2031. storage->shaders.copy.bind();
  2032. storage->shaders.copy.set_uniform(CopyShaderGLES3::MULTIPLIER, p_energy);
  2033. // don't know why but I always have problems setting a uniform mat3, so we're using a transform
  2034. storage->shaders.copy.set_uniform(CopyShaderGLES3::SKY_TRANSFORM, Transform(p_sky_orientation, Vector3(0.0, 0.0, 0.0)).affine_inverse());
  2035. if (asymmetrical) {
  2036. // pack the bits we need from our projection matrix
  2037. storage->shaders.copy.set_uniform(CopyShaderGLES3::ASYM_PROJ, camera.matrix[2][0], camera.matrix[0][0], camera.matrix[2][1], camera.matrix[1][1]);
  2038. ///@TODO I couldn't get mat3 + p_transform.basis to work, that would be better here.
  2039. storage->shaders.copy.set_uniform(CopyShaderGLES3::PANO_TRANSFORM, p_transform);
  2040. }
  2041. glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
  2042. glBindVertexArray(0);
  2043. glColorMask(1, 1, 1, 1);
  2044. storage->shaders.copy.set_conditional(CopyShaderGLES3::USE_ASYM_PANO, false);
  2045. storage->shaders.copy.set_conditional(CopyShaderGLES3::USE_MULTIPLIER, false);
  2046. storage->shaders.copy.set_conditional(CopyShaderGLES3::USE_PANORAMA, false);
  2047. }
  2048. void RasterizerSceneGLES3::_setup_environment(Environment *env, const CameraMatrix &p_cam_projection, const Transform &p_cam_transform, const int p_eye, bool p_no_fog) {
  2049. Transform sky_orientation;
  2050. //store camera into ubo
  2051. store_camera(p_cam_projection, state.ubo_data.projection_matrix);
  2052. store_camera(p_cam_projection.inverse(), state.ubo_data.inv_projection_matrix);
  2053. store_transform(p_cam_transform, state.ubo_data.camera_matrix);
  2054. store_transform(p_cam_transform.affine_inverse(), state.ubo_data.camera_inverse_matrix);
  2055. //time global variables
  2056. state.ubo_data.time = storage->frame.time[0];
  2057. // eye we are rendering
  2058. state.ubo_data.view_index = p_eye == 2 ? 1 : 0;
  2059. state.ubo_data.z_far = p_cam_projection.get_z_far();
  2060. //bg and ambient
  2061. if (env) {
  2062. state.ubo_data.bg_energy = env->bg_energy;
  2063. state.ubo_data.ambient_energy = env->ambient_energy;
  2064. Color linear_ambient_color = env->ambient_color.to_linear();
  2065. state.ubo_data.ambient_light_color[0] = linear_ambient_color.r;
  2066. state.ubo_data.ambient_light_color[1] = linear_ambient_color.g;
  2067. state.ubo_data.ambient_light_color[2] = linear_ambient_color.b;
  2068. state.ubo_data.ambient_light_color[3] = linear_ambient_color.a;
  2069. Color bg_color;
  2070. switch (env->bg_mode) {
  2071. case VS::ENV_BG_CLEAR_COLOR: {
  2072. bg_color = storage->frame.clear_request_color.to_linear();
  2073. } break;
  2074. case VS::ENV_BG_COLOR: {
  2075. bg_color = env->bg_color.to_linear();
  2076. } break;
  2077. default: {
  2078. bg_color = Color(0, 0, 0, 1);
  2079. } break;
  2080. }
  2081. state.ubo_data.bg_color[0] = bg_color.r;
  2082. state.ubo_data.bg_color[1] = bg_color.g;
  2083. state.ubo_data.bg_color[2] = bg_color.b;
  2084. state.ubo_data.bg_color[3] = bg_color.a;
  2085. //use the inverse of our sky_orientation, we may need to skip this if we're using a reflection probe?
  2086. sky_orientation = Transform(env->sky_orientation, Vector3(0.0, 0.0, 0.0)).affine_inverse();
  2087. state.env_radiance_data.ambient_contribution = env->ambient_sky_contribution;
  2088. state.ubo_data.ambient_occlusion_affect_light = env->ssao_light_affect;
  2089. state.ubo_data.ambient_occlusion_affect_ssao = env->ssao_ao_channel_affect;
  2090. //fog
  2091. Color linear_fog = env->fog_color.to_linear();
  2092. state.ubo_data.fog_color_enabled[0] = linear_fog.r;
  2093. state.ubo_data.fog_color_enabled[1] = linear_fog.g;
  2094. state.ubo_data.fog_color_enabled[2] = linear_fog.b;
  2095. state.ubo_data.fog_color_enabled[3] = (!p_no_fog && env->fog_enabled) ? 1.0 : 0.0;
  2096. state.ubo_data.fog_density = linear_fog.a;
  2097. Color linear_sun = env->fog_sun_color.to_linear();
  2098. state.ubo_data.fog_sun_color_amount[0] = linear_sun.r;
  2099. state.ubo_data.fog_sun_color_amount[1] = linear_sun.g;
  2100. state.ubo_data.fog_sun_color_amount[2] = linear_sun.b;
  2101. state.ubo_data.fog_sun_color_amount[3] = env->fog_sun_amount;
  2102. state.ubo_data.fog_depth_enabled = env->fog_depth_enabled;
  2103. state.ubo_data.fog_depth_begin = env->fog_depth_begin;
  2104. state.ubo_data.fog_depth_end = env->fog_depth_end;
  2105. state.ubo_data.fog_depth_curve = env->fog_depth_curve;
  2106. state.ubo_data.fog_transmit_enabled = env->fog_transmit_enabled;
  2107. state.ubo_data.fog_transmit_curve = env->fog_transmit_curve;
  2108. state.ubo_data.fog_height_enabled = env->fog_height_enabled;
  2109. state.ubo_data.fog_height_min = env->fog_height_min;
  2110. state.ubo_data.fog_height_max = env->fog_height_max;
  2111. state.ubo_data.fog_height_curve = env->fog_height_curve;
  2112. } else {
  2113. state.ubo_data.bg_energy = 1.0;
  2114. state.ubo_data.ambient_energy = 1.0;
  2115. //use from clear color instead, since there is no ambient
  2116. Color linear_ambient_color = storage->frame.clear_request_color.to_linear();
  2117. state.ubo_data.ambient_light_color[0] = linear_ambient_color.r;
  2118. state.ubo_data.ambient_light_color[1] = linear_ambient_color.g;
  2119. state.ubo_data.ambient_light_color[2] = linear_ambient_color.b;
  2120. state.ubo_data.ambient_light_color[3] = linear_ambient_color.a;
  2121. state.ubo_data.bg_color[0] = linear_ambient_color.r;
  2122. state.ubo_data.bg_color[1] = linear_ambient_color.g;
  2123. state.ubo_data.bg_color[2] = linear_ambient_color.b;
  2124. state.ubo_data.bg_color[3] = linear_ambient_color.a;
  2125. state.env_radiance_data.ambient_contribution = 0;
  2126. state.ubo_data.ambient_occlusion_affect_light = 0;
  2127. state.ubo_data.fog_color_enabled[3] = 0.0;
  2128. }
  2129. {
  2130. //directional shadow
  2131. state.ubo_data.shadow_directional_pixel_size[0] = 1.0 / directional_shadow.size;
  2132. state.ubo_data.shadow_directional_pixel_size[1] = 1.0 / directional_shadow.size;
  2133. glActiveTexture(GL_TEXTURE0 + storage->config.max_texture_image_units - 5);
  2134. glBindTexture(GL_TEXTURE_2D, directional_shadow.depth);
  2135. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE, GL_COMPARE_REF_TO_TEXTURE);
  2136. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_FUNC, GL_LESS);
  2137. }
  2138. glBindBuffer(GL_UNIFORM_BUFFER, state.scene_ubo);
  2139. glBufferData(GL_UNIFORM_BUFFER, sizeof(State::SceneDataUBO), &state.ubo_data, GL_DYNAMIC_DRAW);
  2140. glBindBuffer(GL_UNIFORM_BUFFER, 0);
  2141. //fill up environment
  2142. store_transform(sky_orientation * p_cam_transform, state.env_radiance_data.transform);
  2143. glBindBuffer(GL_UNIFORM_BUFFER, state.env_radiance_ubo);
  2144. glBufferData(GL_UNIFORM_BUFFER, sizeof(State::EnvironmentRadianceUBO), &state.env_radiance_data, GL_DYNAMIC_DRAW);
  2145. glBindBuffer(GL_UNIFORM_BUFFER, 0);
  2146. }
  2147. void RasterizerSceneGLES3::_setup_directional_light(int p_index, const Transform &p_camera_inverse_transform, bool p_use_shadows) {
  2148. LightInstance *li = directional_lights[p_index];
  2149. LightDataUBO ubo_data; //used for filling
  2150. float sign = li->light_ptr->negative ? -1 : 1;
  2151. Color linear_col = li->light_ptr->color.to_linear();
  2152. //compensate normalized diffuse range by multiplying by PI
  2153. ubo_data.light_color_energy[0] = linear_col.r * sign * li->light_ptr->param[VS::LIGHT_PARAM_ENERGY] * Math_PI;
  2154. ubo_data.light_color_energy[1] = linear_col.g * sign * li->light_ptr->param[VS::LIGHT_PARAM_ENERGY] * Math_PI;
  2155. ubo_data.light_color_energy[2] = linear_col.b * sign * li->light_ptr->param[VS::LIGHT_PARAM_ENERGY] * Math_PI;
  2156. ubo_data.light_color_energy[3] = 0;
  2157. //omni, keep at 0
  2158. ubo_data.light_pos_inv_radius[0] = 0.0;
  2159. ubo_data.light_pos_inv_radius[1] = 0.0;
  2160. ubo_data.light_pos_inv_radius[2] = 0.0;
  2161. ubo_data.light_pos_inv_radius[3] = 0.0;
  2162. Vector3 direction = p_camera_inverse_transform.basis.xform(li->transform.basis.xform(Vector3(0, 0, -1))).normalized();
  2163. ubo_data.light_direction_attenuation[0] = direction.x;
  2164. ubo_data.light_direction_attenuation[1] = direction.y;
  2165. ubo_data.light_direction_attenuation[2] = direction.z;
  2166. ubo_data.light_direction_attenuation[3] = 1.0;
  2167. ubo_data.light_params[0] = 0;
  2168. ubo_data.light_params[1] = 0;
  2169. ubo_data.light_params[2] = li->light_ptr->param[VS::LIGHT_PARAM_SPECULAR];
  2170. ubo_data.light_params[3] = 0;
  2171. Color shadow_color = li->light_ptr->shadow_color.to_linear();
  2172. ubo_data.light_shadow_color_contact[0] = shadow_color.r;
  2173. ubo_data.light_shadow_color_contact[1] = shadow_color.g;
  2174. ubo_data.light_shadow_color_contact[2] = shadow_color.b;
  2175. ubo_data.light_shadow_color_contact[3] = li->light_ptr->param[VS::LIGHT_PARAM_CONTACT_SHADOW_SIZE];
  2176. if (p_use_shadows && li->light_ptr->shadow) {
  2177. int shadow_count = 0;
  2178. switch (li->light_ptr->directional_shadow_mode) {
  2179. case VS::LIGHT_DIRECTIONAL_SHADOW_ORTHOGONAL: {
  2180. shadow_count = 1;
  2181. } break;
  2182. case VS::LIGHT_DIRECTIONAL_SHADOW_PARALLEL_2_SPLITS: {
  2183. shadow_count = 2;
  2184. } break;
  2185. case VS::LIGHT_DIRECTIONAL_SHADOW_PARALLEL_4_SPLITS: {
  2186. shadow_count = 4;
  2187. } break;
  2188. }
  2189. for (int j = 0; j < shadow_count; j++) {
  2190. uint32_t x = li->directional_rect.position.x;
  2191. uint32_t y = li->directional_rect.position.y;
  2192. uint32_t width = li->directional_rect.size.x;
  2193. uint32_t height = li->directional_rect.size.y;
  2194. if (li->light_ptr->directional_shadow_mode == VS::LIGHT_DIRECTIONAL_SHADOW_PARALLEL_4_SPLITS) {
  2195. width /= 2;
  2196. height /= 2;
  2197. if (j == 1) {
  2198. x += width;
  2199. } else if (j == 2) {
  2200. y += height;
  2201. } else if (j == 3) {
  2202. x += width;
  2203. y += height;
  2204. }
  2205. } else if (li->light_ptr->directional_shadow_mode == VS::LIGHT_DIRECTIONAL_SHADOW_PARALLEL_2_SPLITS) {
  2206. height /= 2;
  2207. if (j != 0) {
  2208. y += height;
  2209. }
  2210. }
  2211. ubo_data.shadow_split_offsets[j] = li->shadow_transform[j].split;
  2212. Transform modelview = (p_camera_inverse_transform * li->shadow_transform[j].transform).affine_inverse();
  2213. CameraMatrix bias;
  2214. bias.set_light_bias();
  2215. CameraMatrix rectm;
  2216. Rect2 atlas_rect = Rect2(float(x) / directional_shadow.size, float(y) / directional_shadow.size, float(width) / directional_shadow.size, float(height) / directional_shadow.size);
  2217. rectm.set_light_atlas_rect(atlas_rect);
  2218. CameraMatrix shadow_mtx = rectm * bias * li->shadow_transform[j].camera * modelview;
  2219. store_camera(shadow_mtx, &ubo_data.shadow.matrix[16 * j]);
  2220. ubo_data.light_clamp[0] = atlas_rect.position.x;
  2221. ubo_data.light_clamp[1] = atlas_rect.position.y;
  2222. ubo_data.light_clamp[2] = atlas_rect.size.x;
  2223. ubo_data.light_clamp[3] = atlas_rect.size.y;
  2224. }
  2225. }
  2226. glBindBuffer(GL_UNIFORM_BUFFER, state.directional_ubo);
  2227. glBufferData(GL_UNIFORM_BUFFER, sizeof(LightDataUBO), &ubo_data, GL_DYNAMIC_DRAW);
  2228. glBindBuffer(GL_UNIFORM_BUFFER, 0);
  2229. directional_light = li;
  2230. glBindBufferBase(GL_UNIFORM_BUFFER, 3, state.directional_ubo);
  2231. }
  2232. void RasterizerSceneGLES3::_setup_lights(RID *p_light_cull_result, int p_light_cull_count, const Transform &p_camera_inverse_transform, const CameraMatrix &p_camera_projection, RID p_shadow_atlas) {
  2233. state.omni_light_count = 0;
  2234. state.spot_light_count = 0;
  2235. state.directional_light_count = 0;
  2236. directional_light = nullptr;
  2237. first_directional_light = RID();
  2238. ShadowAtlas *shadow_atlas = shadow_atlas_owner.getornull(p_shadow_atlas);
  2239. for (int i = 0; i < p_light_cull_count; i++) {
  2240. ERR_BREAK(i >= render_list.max_lights);
  2241. LightInstance *li = light_instance_owner.getptr(p_light_cull_result[i]);
  2242. LightDataUBO ubo_data; //used for filling
  2243. switch (li->light_ptr->type) {
  2244. case VS::LIGHT_DIRECTIONAL: {
  2245. if (state.directional_light_count == 0) {
  2246. first_directional_light = p_light_cull_result[i];
  2247. }
  2248. if (state.directional_light_count < RenderList::MAX_DIRECTIONAL_LIGHTS) {
  2249. directional_lights[state.directional_light_count++] = li;
  2250. }
  2251. } break;
  2252. case VS::LIGHT_OMNI: {
  2253. float sign = li->light_ptr->negative ? -1 : 1;
  2254. Color linear_col = li->light_ptr->color.to_linear();
  2255. ubo_data.light_color_energy[0] = linear_col.r * sign * li->light_ptr->param[VS::LIGHT_PARAM_ENERGY] * Math_PI;
  2256. ubo_data.light_color_energy[1] = linear_col.g * sign * li->light_ptr->param[VS::LIGHT_PARAM_ENERGY] * Math_PI;
  2257. ubo_data.light_color_energy[2] = linear_col.b * sign * li->light_ptr->param[VS::LIGHT_PARAM_ENERGY] * Math_PI;
  2258. ubo_data.light_color_energy[3] = 0;
  2259. Vector3 pos = p_camera_inverse_transform.xform(li->transform.origin);
  2260. //directional, keep at 0
  2261. ubo_data.light_pos_inv_radius[0] = pos.x;
  2262. ubo_data.light_pos_inv_radius[1] = pos.y;
  2263. ubo_data.light_pos_inv_radius[2] = pos.z;
  2264. ubo_data.light_pos_inv_radius[3] = 1.0 / MAX(0.001, li->light_ptr->param[VS::LIGHT_PARAM_RANGE]);
  2265. ubo_data.light_direction_attenuation[0] = 0;
  2266. ubo_data.light_direction_attenuation[1] = 0;
  2267. ubo_data.light_direction_attenuation[2] = 0;
  2268. ubo_data.light_direction_attenuation[3] = li->light_ptr->param[VS::LIGHT_PARAM_ATTENUATION];
  2269. ubo_data.light_params[0] = 0;
  2270. ubo_data.light_params[1] = 0;
  2271. ubo_data.light_params[2] = li->light_ptr->param[VS::LIGHT_PARAM_SPECULAR];
  2272. ubo_data.light_params[3] = 0;
  2273. Color shadow_color = li->light_ptr->shadow_color.to_linear();
  2274. ubo_data.light_shadow_color_contact[0] = shadow_color.r;
  2275. ubo_data.light_shadow_color_contact[1] = shadow_color.g;
  2276. ubo_data.light_shadow_color_contact[2] = shadow_color.b;
  2277. ubo_data.light_shadow_color_contact[3] = li->light_ptr->param[VS::LIGHT_PARAM_CONTACT_SHADOW_SIZE];
  2278. if (li->light_ptr->shadow && shadow_atlas && shadow_atlas->shadow_owners.has(li->self)) {
  2279. // fill in the shadow information
  2280. uint32_t key = shadow_atlas->shadow_owners[li->self];
  2281. uint32_t quadrant = (key >> ShadowAtlas::QUADRANT_SHIFT) & 0x3;
  2282. uint32_t shadow = key & ShadowAtlas::SHADOW_INDEX_MASK;
  2283. ERR_CONTINUE(shadow >= (uint32_t)shadow_atlas->quadrants[quadrant].shadows.size());
  2284. uint32_t atlas_size = shadow_atlas->size;
  2285. uint32_t quadrant_size = atlas_size >> 1;
  2286. uint32_t x = (quadrant & 1) * quadrant_size;
  2287. uint32_t y = (quadrant >> 1) * quadrant_size;
  2288. uint32_t shadow_size = (quadrant_size / shadow_atlas->quadrants[quadrant].subdivision);
  2289. x += (shadow % shadow_atlas->quadrants[quadrant].subdivision) * shadow_size;
  2290. y += (shadow / shadow_atlas->quadrants[quadrant].subdivision) * shadow_size;
  2291. uint32_t width = shadow_size;
  2292. uint32_t height = shadow_size;
  2293. if (li->light_ptr->omni_shadow_detail == VS::LIGHT_OMNI_SHADOW_DETAIL_HORIZONTAL) {
  2294. height /= 2;
  2295. } else {
  2296. width /= 2;
  2297. }
  2298. Transform proj = (p_camera_inverse_transform * li->transform).inverse();
  2299. store_transform(proj, ubo_data.shadow.matrix1);
  2300. ubo_data.light_params[3] = 1.0; //means it has shadow
  2301. ubo_data.light_clamp[0] = float(x) / atlas_size;
  2302. ubo_data.light_clamp[1] = float(y) / atlas_size;
  2303. ubo_data.light_clamp[2] = float(width) / atlas_size;
  2304. ubo_data.light_clamp[3] = float(height) / atlas_size;
  2305. }
  2306. li->light_index = state.omni_light_count;
  2307. memcpy(&state.omni_array_tmp[li->light_index * state.ubo_light_size], &ubo_data, state.ubo_light_size);
  2308. state.omni_light_count++;
  2309. } break;
  2310. case VS::LIGHT_SPOT: {
  2311. float sign = li->light_ptr->negative ? -1 : 1;
  2312. Color linear_col = li->light_ptr->color.to_linear();
  2313. ubo_data.light_color_energy[0] = linear_col.r * sign * li->light_ptr->param[VS::LIGHT_PARAM_ENERGY] * Math_PI;
  2314. ubo_data.light_color_energy[1] = linear_col.g * sign * li->light_ptr->param[VS::LIGHT_PARAM_ENERGY] * Math_PI;
  2315. ubo_data.light_color_energy[2] = linear_col.b * sign * li->light_ptr->param[VS::LIGHT_PARAM_ENERGY] * Math_PI;
  2316. ubo_data.light_color_energy[3] = 0;
  2317. Vector3 pos = p_camera_inverse_transform.xform(li->transform.origin);
  2318. //directional, keep at 0
  2319. ubo_data.light_pos_inv_radius[0] = pos.x;
  2320. ubo_data.light_pos_inv_radius[1] = pos.y;
  2321. ubo_data.light_pos_inv_radius[2] = pos.z;
  2322. ubo_data.light_pos_inv_radius[3] = 1.0 / MAX(0.001, li->light_ptr->param[VS::LIGHT_PARAM_RANGE]);
  2323. Vector3 direction = p_camera_inverse_transform.basis.xform(li->transform.basis.xform(Vector3(0, 0, -1))).normalized();
  2324. ubo_data.light_direction_attenuation[0] = direction.x;
  2325. ubo_data.light_direction_attenuation[1] = direction.y;
  2326. ubo_data.light_direction_attenuation[2] = direction.z;
  2327. ubo_data.light_direction_attenuation[3] = li->light_ptr->param[VS::LIGHT_PARAM_ATTENUATION];
  2328. ubo_data.light_params[0] = li->light_ptr->param[VS::LIGHT_PARAM_SPOT_ATTENUATION];
  2329. ubo_data.light_params[1] = Math::cos(Math::deg2rad(li->light_ptr->param[VS::LIGHT_PARAM_SPOT_ANGLE]));
  2330. ubo_data.light_params[2] = li->light_ptr->param[VS::LIGHT_PARAM_SPECULAR];
  2331. ubo_data.light_params[3] = 0;
  2332. Color shadow_color = li->light_ptr->shadow_color.to_linear();
  2333. ubo_data.light_shadow_color_contact[0] = shadow_color.r;
  2334. ubo_data.light_shadow_color_contact[1] = shadow_color.g;
  2335. ubo_data.light_shadow_color_contact[2] = shadow_color.b;
  2336. ubo_data.light_shadow_color_contact[3] = li->light_ptr->param[VS::LIGHT_PARAM_CONTACT_SHADOW_SIZE];
  2337. if (li->light_ptr->shadow && shadow_atlas && shadow_atlas->shadow_owners.has(li->self)) {
  2338. // fill in the shadow information
  2339. uint32_t key = shadow_atlas->shadow_owners[li->self];
  2340. uint32_t quadrant = (key >> ShadowAtlas::QUADRANT_SHIFT) & 0x3;
  2341. uint32_t shadow = key & ShadowAtlas::SHADOW_INDEX_MASK;
  2342. ERR_CONTINUE(shadow >= (uint32_t)shadow_atlas->quadrants[quadrant].shadows.size());
  2343. uint32_t atlas_size = shadow_atlas->size;
  2344. uint32_t quadrant_size = atlas_size >> 1;
  2345. uint32_t x = (quadrant & 1) * quadrant_size;
  2346. uint32_t y = (quadrant >> 1) * quadrant_size;
  2347. uint32_t shadow_size = (quadrant_size / shadow_atlas->quadrants[quadrant].subdivision);
  2348. x += (shadow % shadow_atlas->quadrants[quadrant].subdivision) * shadow_size;
  2349. y += (shadow / shadow_atlas->quadrants[quadrant].subdivision) * shadow_size;
  2350. uint32_t width = shadow_size;
  2351. uint32_t height = shadow_size;
  2352. Rect2 rect(float(x) / atlas_size, float(y) / atlas_size, float(width) / atlas_size, float(height) / atlas_size);
  2353. ubo_data.light_params[3] = 1.0; //means it has shadow
  2354. ubo_data.light_clamp[0] = rect.position.x;
  2355. ubo_data.light_clamp[1] = rect.position.y;
  2356. ubo_data.light_clamp[2] = rect.size.x;
  2357. ubo_data.light_clamp[3] = rect.size.y;
  2358. Transform modelview = (p_camera_inverse_transform * li->transform).inverse();
  2359. CameraMatrix bias;
  2360. bias.set_light_bias();
  2361. CameraMatrix rectm;
  2362. rectm.set_light_atlas_rect(rect);
  2363. CameraMatrix shadow_mtx = rectm * bias * li->shadow_transform[0].camera * modelview;
  2364. store_camera(shadow_mtx, ubo_data.shadow.matrix1);
  2365. }
  2366. li->light_index = state.spot_light_count;
  2367. memcpy(&state.spot_array_tmp[li->light_index * state.ubo_light_size], &ubo_data, state.ubo_light_size);
  2368. state.spot_light_count++;
  2369. } break;
  2370. }
  2371. li->last_pass = render_pass;
  2372. //update UBO for forward rendering, blit to texture for clustered
  2373. }
  2374. if (state.omni_light_count) {
  2375. glBindBuffer(GL_UNIFORM_BUFFER, state.omni_array_ubo);
  2376. glBufferSubData(GL_UNIFORM_BUFFER, 0, state.omni_light_count * state.ubo_light_size, state.omni_array_tmp);
  2377. glBindBuffer(GL_UNIFORM_BUFFER, 0);
  2378. }
  2379. glBindBufferBase(GL_UNIFORM_BUFFER, 4, state.omni_array_ubo);
  2380. if (state.spot_light_count) {
  2381. glBindBuffer(GL_UNIFORM_BUFFER, state.spot_array_ubo);
  2382. glBufferSubData(GL_UNIFORM_BUFFER, 0, state.spot_light_count * state.ubo_light_size, state.spot_array_tmp);
  2383. glBindBuffer(GL_UNIFORM_BUFFER, 0);
  2384. }
  2385. glBindBufferBase(GL_UNIFORM_BUFFER, 5, state.spot_array_ubo);
  2386. }
  2387. void RasterizerSceneGLES3::_setup_reflections(RID *p_reflection_probe_cull_result, int p_reflection_probe_cull_count, const Transform &p_camera_inverse_transform, const CameraMatrix &p_camera_projection, RID p_reflection_atlas, Environment *p_env) {
  2388. state.reflection_probe_count = 0;
  2389. for (int i = 0; i < p_reflection_probe_cull_count; i++) {
  2390. ReflectionProbeInstance *rpi = reflection_probe_instance_owner.getornull(p_reflection_probe_cull_result[i]);
  2391. ERR_CONTINUE(!rpi);
  2392. ReflectionAtlas *reflection_atlas = reflection_atlas_owner.getornull(p_reflection_atlas);
  2393. ERR_CONTINUE(!reflection_atlas);
  2394. ERR_CONTINUE(rpi->reflection_atlas_index < 0);
  2395. if (state.reflection_probe_count >= state.max_ubo_reflections) {
  2396. break;
  2397. }
  2398. rpi->last_pass = render_pass;
  2399. ReflectionProbeDataUBO reflection_ubo;
  2400. reflection_ubo.box_extents[0] = rpi->probe_ptr->extents.x;
  2401. reflection_ubo.box_extents[1] = rpi->probe_ptr->extents.y;
  2402. reflection_ubo.box_extents[2] = rpi->probe_ptr->extents.z;
  2403. reflection_ubo.box_extents[3] = 0;
  2404. reflection_ubo.box_ofs[0] = rpi->probe_ptr->origin_offset.x;
  2405. reflection_ubo.box_ofs[1] = rpi->probe_ptr->origin_offset.y;
  2406. reflection_ubo.box_ofs[2] = rpi->probe_ptr->origin_offset.z;
  2407. reflection_ubo.box_ofs[3] = 0;
  2408. reflection_ubo.params[0] = rpi->probe_ptr->intensity;
  2409. reflection_ubo.params[1] = 0;
  2410. reflection_ubo.params[2] = rpi->probe_ptr->interior ? 1.0 : 0.0;
  2411. reflection_ubo.params[3] = rpi->probe_ptr->box_projection ? 1.0 : 0.0;
  2412. if (rpi->probe_ptr->interior) {
  2413. Color ambient_linear = rpi->probe_ptr->interior_ambient.to_linear();
  2414. reflection_ubo.ambient[0] = ambient_linear.r * rpi->probe_ptr->interior_ambient_energy;
  2415. reflection_ubo.ambient[1] = ambient_linear.g * rpi->probe_ptr->interior_ambient_energy;
  2416. reflection_ubo.ambient[2] = ambient_linear.b * rpi->probe_ptr->interior_ambient_energy;
  2417. reflection_ubo.ambient[3] = rpi->probe_ptr->interior_ambient_probe_contrib;
  2418. } else {
  2419. Color ambient_linear;
  2420. if (p_env) {
  2421. ambient_linear = p_env->ambient_color.to_linear();
  2422. ambient_linear.r *= p_env->ambient_energy;
  2423. ambient_linear.g *= p_env->ambient_energy;
  2424. ambient_linear.b *= p_env->ambient_energy;
  2425. }
  2426. reflection_ubo.ambient[0] = ambient_linear.r;
  2427. reflection_ubo.ambient[1] = ambient_linear.g;
  2428. reflection_ubo.ambient[2] = ambient_linear.b;
  2429. reflection_ubo.ambient[3] = 0; //not used in exterior mode, since it just blends with regular ambient light
  2430. }
  2431. int cell_size = reflection_atlas->size / reflection_atlas->subdiv;
  2432. int x = (rpi->reflection_atlas_index % reflection_atlas->subdiv) * cell_size;
  2433. int y = (rpi->reflection_atlas_index / reflection_atlas->subdiv) * cell_size;
  2434. int width = cell_size;
  2435. int height = cell_size;
  2436. reflection_ubo.atlas_clamp[0] = float(x) / reflection_atlas->size;
  2437. reflection_ubo.atlas_clamp[1] = float(y) / reflection_atlas->size;
  2438. reflection_ubo.atlas_clamp[2] = float(width) / reflection_atlas->size;
  2439. reflection_ubo.atlas_clamp[3] = float(height) / reflection_atlas->size;
  2440. Transform proj = (p_camera_inverse_transform * rpi->transform).inverse();
  2441. store_transform(proj, reflection_ubo.local_matrix);
  2442. rpi->reflection_index = state.reflection_probe_count;
  2443. memcpy(&state.reflection_array_tmp[rpi->reflection_index * sizeof(ReflectionProbeDataUBO)], &reflection_ubo, sizeof(ReflectionProbeDataUBO));
  2444. state.reflection_probe_count++;
  2445. }
  2446. if (state.reflection_probe_count) {
  2447. glBindBuffer(GL_UNIFORM_BUFFER, state.reflection_array_ubo);
  2448. glBufferSubData(GL_UNIFORM_BUFFER, 0, state.reflection_probe_count * sizeof(ReflectionProbeDataUBO), state.reflection_array_tmp);
  2449. glBindBuffer(GL_UNIFORM_BUFFER, 0);
  2450. }
  2451. glBindBufferBase(GL_UNIFORM_BUFFER, 6, state.reflection_array_ubo);
  2452. }
  2453. void RasterizerSceneGLES3::_copy_screen(bool p_invalidate_color, bool p_invalidate_depth) {
  2454. #ifndef GLES_OVER_GL
  2455. if (p_invalidate_color) {
  2456. GLenum attachments[2] = {
  2457. GL_COLOR_ATTACHMENT0,
  2458. GL_DEPTH_ATTACHMENT
  2459. };
  2460. glInvalidateFramebuffer(GL_FRAMEBUFFER, p_invalidate_depth ? 2 : 1, attachments);
  2461. }
  2462. #endif
  2463. glBindVertexArray(storage->resources.quadie_array);
  2464. glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
  2465. glBindVertexArray(0);
  2466. }
  2467. void RasterizerSceneGLES3::_copy_texture_to_front_buffer(GLuint p_texture) {
  2468. //copy to front buffer
  2469. glBindFramebuffer(GL_FRAMEBUFFER, storage->frame.current_rt->fbo);
  2470. glDepthMask(GL_FALSE);
  2471. glDisable(GL_DEPTH_TEST);
  2472. glDisable(GL_CULL_FACE);
  2473. glDisable(GL_BLEND);
  2474. glDepthFunc(GL_LEQUAL);
  2475. glColorMask(1, 1, 1, 1);
  2476. glActiveTexture(GL_TEXTURE0);
  2477. glBindTexture(GL_TEXTURE_2D, p_texture);
  2478. glViewport(0, 0, storage->frame.current_rt->width * 0.5, storage->frame.current_rt->height * 0.5);
  2479. storage->shaders.copy.set_conditional(CopyShaderGLES3::DISABLE_ALPHA, true);
  2480. storage->shaders.copy.bind();
  2481. _copy_screen();
  2482. //turn off everything used
  2483. storage->shaders.copy.set_conditional(CopyShaderGLES3::LINEAR_TO_SRGB, false);
  2484. storage->shaders.copy.set_conditional(CopyShaderGLES3::DISABLE_ALPHA, false);
  2485. }
  2486. void RasterizerSceneGLES3::_fill_render_list(InstanceBase **p_cull_result, int p_cull_count, bool p_depth_pass, bool p_shadow_pass) {
  2487. current_geometry_index = 0;
  2488. current_material_index = 0;
  2489. state.used_sss = false;
  2490. state.used_screen_texture = false;
  2491. state.used_depth_texture = false;
  2492. //fill list
  2493. for (int i = 0; i < p_cull_count; i++) {
  2494. InstanceBase *inst = p_cull_result[i];
  2495. switch (inst->base_type) {
  2496. case VS::INSTANCE_MESH: {
  2497. RasterizerStorageGLES3::Mesh *mesh = storage->mesh_owner.getptr(inst->base);
  2498. ERR_CONTINUE(!mesh);
  2499. int ssize = mesh->surfaces.size();
  2500. for (int j = 0; j < ssize; j++) {
  2501. int mat_idx = inst->materials[j].is_valid() ? j : -1;
  2502. RasterizerStorageGLES3::Surface *s = mesh->surfaces[j];
  2503. _add_geometry(s, inst, nullptr, mat_idx, p_depth_pass, p_shadow_pass);
  2504. }
  2505. //mesh->last_pass=frame;
  2506. } break;
  2507. case VS::INSTANCE_MULTIMESH: {
  2508. RasterizerStorageGLES3::MultiMesh *multi_mesh = storage->multimesh_owner.getptr(inst->base);
  2509. ERR_CONTINUE(!multi_mesh);
  2510. if (multi_mesh->size == 0 || multi_mesh->visible_instances == 0) {
  2511. continue;
  2512. }
  2513. RasterizerStorageGLES3::Mesh *mesh = storage->mesh_owner.getptr(multi_mesh->mesh);
  2514. if (!mesh) {
  2515. continue; //mesh not assigned
  2516. }
  2517. int ssize = mesh->surfaces.size();
  2518. for (int j = 0; j < ssize; j++) {
  2519. RasterizerStorageGLES3::Surface *s = mesh->surfaces[j];
  2520. _add_geometry(s, inst, multi_mesh, -1, p_depth_pass, p_shadow_pass);
  2521. }
  2522. } break;
  2523. case VS::INSTANCE_IMMEDIATE: {
  2524. RasterizerStorageGLES3::Immediate *immediate = storage->immediate_owner.getptr(inst->base);
  2525. ERR_CONTINUE(!immediate);
  2526. _add_geometry(immediate, inst, nullptr, -1, p_depth_pass, p_shadow_pass);
  2527. } break;
  2528. case VS::INSTANCE_PARTICLES: {
  2529. RasterizerStorageGLES3::Particles *particles = storage->particles_owner.getptr(inst->base);
  2530. ERR_CONTINUE(!particles);
  2531. for (int j = 0; j < particles->draw_passes.size(); j++) {
  2532. RID pmesh = particles->draw_passes[j];
  2533. if (!pmesh.is_valid()) {
  2534. continue;
  2535. }
  2536. RasterizerStorageGLES3::Mesh *mesh = storage->mesh_owner.get(pmesh);
  2537. if (!mesh) {
  2538. continue; //mesh not assigned
  2539. }
  2540. int ssize = mesh->surfaces.size();
  2541. for (int k = 0; k < ssize; k++) {
  2542. RasterizerStorageGLES3::Surface *s = mesh->surfaces[k];
  2543. _add_geometry(s, inst, particles, -1, p_depth_pass, p_shadow_pass);
  2544. }
  2545. }
  2546. } break;
  2547. default: {
  2548. }
  2549. }
  2550. }
  2551. }
  2552. void RasterizerSceneGLES3::_blur_effect_buffer() {
  2553. //blur diffuse into effect mipmaps using separatable convolution
  2554. //storage->shaders.copy.set_conditional(CopyShaderGLES3::GAUSSIAN_HORIZONTAL,true);
  2555. for (int i = 0; i < storage->frame.current_rt->effects.mip_maps[1].sizes.size(); i++) {
  2556. int vp_w = storage->frame.current_rt->effects.mip_maps[1].sizes[i].width;
  2557. int vp_h = storage->frame.current_rt->effects.mip_maps[1].sizes[i].height;
  2558. glViewport(0, 0, vp_w, vp_h);
  2559. //horizontal pass
  2560. state.effect_blur_shader.set_conditional(EffectBlurShaderGLES3::GAUSSIAN_HORIZONTAL, true);
  2561. state.effect_blur_shader.bind();
  2562. state.effect_blur_shader.set_uniform(EffectBlurShaderGLES3::PIXEL_SIZE, Vector2(1.0 / vp_w, 1.0 / vp_h));
  2563. state.effect_blur_shader.set_uniform(EffectBlurShaderGLES3::LOD, float(i));
  2564. glActiveTexture(GL_TEXTURE0);
  2565. glBindTexture(GL_TEXTURE_2D, storage->frame.current_rt->effects.mip_maps[0].color); //previous level, since mipmaps[0] starts one level bigger
  2566. glBindFramebuffer(GL_FRAMEBUFFER, storage->frame.current_rt->effects.mip_maps[1].sizes[i].fbo);
  2567. _copy_screen(true);
  2568. state.effect_blur_shader.set_conditional(EffectBlurShaderGLES3::GAUSSIAN_HORIZONTAL, false);
  2569. //vertical pass
  2570. state.effect_blur_shader.set_conditional(EffectBlurShaderGLES3::GAUSSIAN_VERTICAL, true);
  2571. state.effect_blur_shader.bind();
  2572. state.effect_blur_shader.set_uniform(EffectBlurShaderGLES3::PIXEL_SIZE, Vector2(1.0 / vp_w, 1.0 / vp_h));
  2573. state.effect_blur_shader.set_uniform(EffectBlurShaderGLES3::LOD, float(i));
  2574. glActiveTexture(GL_TEXTURE0);
  2575. glBindTexture(GL_TEXTURE_2D, storage->frame.current_rt->effects.mip_maps[1].color);
  2576. glBindFramebuffer(GL_FRAMEBUFFER, storage->frame.current_rt->effects.mip_maps[0].sizes[i + 1].fbo); //next level, since mipmaps[0] starts one level bigger
  2577. _copy_screen(true);
  2578. state.effect_blur_shader.set_conditional(EffectBlurShaderGLES3::GAUSSIAN_VERTICAL, false);
  2579. }
  2580. }
  2581. void RasterizerSceneGLES3::_prepare_depth_texture() {
  2582. if (!state.prepared_depth_texture) {
  2583. //resolve depth buffer
  2584. glBindFramebuffer(GL_READ_FRAMEBUFFER, storage->frame.current_rt->buffers.fbo);
  2585. glReadBuffer(GL_COLOR_ATTACHMENT0);
  2586. glBindFramebuffer(GL_DRAW_FRAMEBUFFER, storage->frame.current_rt->fbo);
  2587. glBlitFramebuffer(0, 0, storage->frame.current_rt->width, storage->frame.current_rt->height, 0, 0, storage->frame.current_rt->width, storage->frame.current_rt->height, GL_DEPTH_BUFFER_BIT, GL_NEAREST);
  2588. glBindFramebuffer(GL_READ_FRAMEBUFFER, 0);
  2589. glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0);
  2590. state.prepared_depth_texture = true;
  2591. }
  2592. }
  2593. void RasterizerSceneGLES3::_bind_depth_texture() {
  2594. if (!state.bound_depth_texture) {
  2595. ERR_FAIL_COND(!state.prepared_depth_texture);
  2596. //bind depth for read
  2597. glActiveTexture(GL_TEXTURE0 + storage->config.max_texture_image_units - 9);
  2598. glBindTexture(GL_TEXTURE_2D, storage->frame.current_rt->depth);
  2599. state.bound_depth_texture = true;
  2600. }
  2601. }
  2602. void RasterizerSceneGLES3::_render_mrts(Environment *env, const CameraMatrix &p_cam_projection) {
  2603. glDepthMask(GL_FALSE);
  2604. glDisable(GL_DEPTH_TEST);
  2605. glDisable(GL_CULL_FACE);
  2606. glDisable(GL_BLEND);
  2607. _prepare_depth_texture();
  2608. if (env->ssao_enabled || env->ssr_enabled) {
  2609. //copy normal and roughness to effect buffer
  2610. glBindFramebuffer(GL_READ_FRAMEBUFFER, storage->frame.current_rt->buffers.fbo);
  2611. glReadBuffer(GL_COLOR_ATTACHMENT2);
  2612. glBindFramebuffer(GL_DRAW_FRAMEBUFFER, storage->frame.current_rt->buffers.effect_fbo);
  2613. glBlitFramebuffer(0, 0, storage->frame.current_rt->width, storage->frame.current_rt->height, 0, 0, storage->frame.current_rt->width, storage->frame.current_rt->height, GL_COLOR_BUFFER_BIT, GL_NEAREST);
  2614. }
  2615. if (env->ssao_enabled) {
  2616. //copy diffuse to front buffer
  2617. glBindFramebuffer(GL_READ_FRAMEBUFFER, storage->frame.current_rt->buffers.fbo);
  2618. glReadBuffer(GL_COLOR_ATTACHMENT0);
  2619. glBindFramebuffer(GL_DRAW_FRAMEBUFFER, storage->frame.current_rt->fbo);
  2620. glBlitFramebuffer(0, 0, storage->frame.current_rt->width, storage->frame.current_rt->height, 0, 0, storage->frame.current_rt->width, storage->frame.current_rt->height, GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT, GL_NEAREST);
  2621. glBindFramebuffer(GL_READ_FRAMEBUFFER, 0);
  2622. glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0);
  2623. //copy from depth, convert to linear
  2624. GLint ss[2];
  2625. ss[0] = storage->frame.current_rt->width;
  2626. ss[1] = storage->frame.current_rt->height;
  2627. for (int i = 0; i < storage->frame.current_rt->effects.ssao.depth_mipmap_fbos.size(); i++) {
  2628. state.ssao_minify_shader.set_conditional(SsaoMinifyShaderGLES3::MINIFY_START, i == 0);
  2629. state.ssao_minify_shader.set_conditional(SsaoMinifyShaderGLES3::USE_ORTHOGONAL_PROJECTION, p_cam_projection.is_orthogonal());
  2630. state.ssao_minify_shader.bind();
  2631. state.ssao_minify_shader.set_uniform(SsaoMinifyShaderGLES3::CAMERA_Z_FAR, p_cam_projection.get_z_far());
  2632. state.ssao_minify_shader.set_uniform(SsaoMinifyShaderGLES3::CAMERA_Z_NEAR, p_cam_projection.get_z_near());
  2633. state.ssao_minify_shader.set_uniform(SsaoMinifyShaderGLES3::SOURCE_MIPMAP, MAX(0, i - 1));
  2634. glUniform2iv(state.ssao_minify_shader.get_uniform(SsaoMinifyShaderGLES3::FROM_SIZE), 1, ss);
  2635. ss[0] >>= 1;
  2636. ss[1] >>= 1;
  2637. glActiveTexture(GL_TEXTURE0);
  2638. if (i == 0) {
  2639. glBindTexture(GL_TEXTURE_2D, storage->frame.current_rt->depth);
  2640. } else {
  2641. glBindTexture(GL_TEXTURE_2D, storage->frame.current_rt->effects.ssao.linear_depth);
  2642. }
  2643. glBindFramebuffer(GL_FRAMEBUFFER, storage->frame.current_rt->effects.ssao.depth_mipmap_fbos[i]); //copy to front first
  2644. glViewport(0, 0, ss[0], ss[1]);
  2645. _copy_screen(true);
  2646. }
  2647. ss[0] = storage->frame.current_rt->width;
  2648. ss[1] = storage->frame.current_rt->height;
  2649. glViewport(0, 0, ss[0], ss[1]);
  2650. glEnable(GL_DEPTH_TEST);
  2651. glDepthFunc(GL_GREATER);
  2652. // do SSAO!
  2653. state.ssao_shader.set_conditional(SsaoShaderGLES3::ENABLE_RADIUS2, env->ssao_radius2 > 0.001);
  2654. state.ssao_shader.set_conditional(SsaoShaderGLES3::USE_ORTHOGONAL_PROJECTION, p_cam_projection.is_orthogonal());
  2655. state.ssao_shader.set_conditional(SsaoShaderGLES3::SSAO_QUALITY_LOW, env->ssao_quality == VS::ENV_SSAO_QUALITY_LOW);
  2656. state.ssao_shader.set_conditional(SsaoShaderGLES3::SSAO_QUALITY_HIGH, env->ssao_quality == VS::ENV_SSAO_QUALITY_HIGH);
  2657. state.ssao_shader.bind();
  2658. state.ssao_shader.set_uniform(SsaoShaderGLES3::CAMERA_Z_FAR, p_cam_projection.get_z_far());
  2659. state.ssao_shader.set_uniform(SsaoShaderGLES3::CAMERA_Z_NEAR, p_cam_projection.get_z_near());
  2660. glUniform2iv(state.ssao_shader.get_uniform(SsaoShaderGLES3::SCREEN_SIZE), 1, ss);
  2661. float radius = env->ssao_radius;
  2662. state.ssao_shader.set_uniform(SsaoShaderGLES3::RADIUS, radius);
  2663. float intensity = env->ssao_intensity;
  2664. state.ssao_shader.set_uniform(SsaoShaderGLES3::INTENSITY_DIV_R6, intensity / pow(radius, 6.0f));
  2665. if (env->ssao_radius2 > 0.001) {
  2666. float radius2 = env->ssao_radius2;
  2667. state.ssao_shader.set_uniform(SsaoShaderGLES3::RADIUS2, radius2);
  2668. float intensity2 = env->ssao_intensity2;
  2669. state.ssao_shader.set_uniform(SsaoShaderGLES3::INTENSITY_DIV_R62, intensity2 / pow(radius2, 6.0f));
  2670. }
  2671. float proj_info[4] = {
  2672. -2.0f / (ss[0] * p_cam_projection.matrix[0][0]),
  2673. -2.0f / (ss[1] * p_cam_projection.matrix[1][1]),
  2674. (1.0f - p_cam_projection.matrix[0][2]) / p_cam_projection.matrix[0][0],
  2675. (1.0f + p_cam_projection.matrix[1][2]) / p_cam_projection.matrix[1][1]
  2676. };
  2677. glUniform4fv(state.ssao_shader.get_uniform(SsaoShaderGLES3::PROJ_INFO), 1, proj_info);
  2678. float pixels_per_meter = float(p_cam_projection.get_pixels_per_meter(ss[0]));
  2679. state.ssao_shader.set_uniform(SsaoShaderGLES3::PROJ_SCALE, pixels_per_meter);
  2680. state.ssao_shader.set_uniform(SsaoShaderGLES3::BIAS, env->ssao_bias);
  2681. glActiveTexture(GL_TEXTURE0);
  2682. glBindTexture(GL_TEXTURE_2D, storage->frame.current_rt->depth);
  2683. glActiveTexture(GL_TEXTURE1);
  2684. glBindTexture(GL_TEXTURE_2D, storage->frame.current_rt->effects.ssao.linear_depth);
  2685. glActiveTexture(GL_TEXTURE2);
  2686. glBindTexture(GL_TEXTURE_2D, storage->frame.current_rt->buffers.effect);
  2687. glBindFramebuffer(GL_FRAMEBUFFER, storage->frame.current_rt->effects.ssao.blur_fbo[0]); //copy to front first
  2688. Color white(1, 1, 1, 1);
  2689. glClearBufferfv(GL_COLOR, 0, white.components); // specular
  2690. _copy_screen(true);
  2691. //do the batm, i mean blur
  2692. state.ssao_blur_shader.bind();
  2693. if (env->ssao_filter) {
  2694. for (int i = 0; i < 2; i++) {
  2695. state.ssao_blur_shader.set_uniform(SsaoBlurShaderGLES3::CAMERA_Z_FAR, p_cam_projection.get_z_far());
  2696. state.ssao_blur_shader.set_uniform(SsaoBlurShaderGLES3::CAMERA_Z_NEAR, p_cam_projection.get_z_near());
  2697. state.ssao_blur_shader.set_uniform(SsaoBlurShaderGLES3::EDGE_SHARPNESS, env->ssao_bilateral_sharpness);
  2698. state.ssao_blur_shader.set_uniform(SsaoBlurShaderGLES3::FILTER_SCALE, int(env->ssao_filter));
  2699. GLint axis[2] = { i, 1 - i };
  2700. glUniform2iv(state.ssao_blur_shader.get_uniform(SsaoBlurShaderGLES3::AXIS), 1, axis);
  2701. glUniform2iv(state.ssao_blur_shader.get_uniform(SsaoBlurShaderGLES3::SCREEN_SIZE), 1, ss);
  2702. glActiveTexture(GL_TEXTURE0);
  2703. glBindTexture(GL_TEXTURE_2D, storage->frame.current_rt->effects.ssao.blur_red[i]);
  2704. glActiveTexture(GL_TEXTURE1);
  2705. glBindTexture(GL_TEXTURE_2D, storage->frame.current_rt->depth);
  2706. glActiveTexture(GL_TEXTURE2);
  2707. glBindTexture(GL_TEXTURE_2D, storage->frame.current_rt->buffers.effect);
  2708. glBindFramebuffer(GL_FRAMEBUFFER, storage->frame.current_rt->effects.ssao.blur_fbo[1 - i]);
  2709. if (i == 0) {
  2710. glClearBufferfv(GL_COLOR, 0, white.components); // specular
  2711. }
  2712. _copy_screen(true);
  2713. }
  2714. }
  2715. glDisable(GL_DEPTH_TEST);
  2716. glDepthFunc(GL_LEQUAL);
  2717. // just copy diffuse while applying SSAO
  2718. state.effect_blur_shader.set_conditional(EffectBlurShaderGLES3::SSAO_MERGE, true);
  2719. state.effect_blur_shader.bind();
  2720. state.effect_blur_shader.set_uniform(EffectBlurShaderGLES3::SSAO_COLOR, env->ssao_color);
  2721. glActiveTexture(GL_TEXTURE0);
  2722. glBindTexture(GL_TEXTURE_2D, storage->frame.current_rt->color); //previous level, since mipmaps[0] starts one level bigger
  2723. glActiveTexture(GL_TEXTURE1);
  2724. glBindTexture(GL_TEXTURE_2D, storage->frame.current_rt->effects.ssao.blur_red[0]); //previous level, since mipmaps[0] starts one level bigger
  2725. glBindFramebuffer(GL_FRAMEBUFFER, storage->frame.current_rt->effects.mip_maps[0].sizes[0].fbo); // copy to base level
  2726. _copy_screen(true);
  2727. state.effect_blur_shader.set_conditional(EffectBlurShaderGLES3::SSAO_MERGE, false);
  2728. } else {
  2729. //copy diffuse to effect buffer
  2730. glBindFramebuffer(GL_READ_FRAMEBUFFER, storage->frame.current_rt->buffers.fbo);
  2731. glReadBuffer(GL_COLOR_ATTACHMENT0);
  2732. glBindFramebuffer(GL_DRAW_FRAMEBUFFER, storage->frame.current_rt->effects.mip_maps[0].sizes[0].fbo);
  2733. glBlitFramebuffer(0, 0, storage->frame.current_rt->width, storage->frame.current_rt->height, 0, 0, storage->frame.current_rt->width, storage->frame.current_rt->height, GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT, GL_NEAREST);
  2734. glBindFramebuffer(GL_READ_FRAMEBUFFER, 0);
  2735. glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0);
  2736. }
  2737. if (state.used_sss) { //sss enabled
  2738. //copy diffuse while performing sss
  2739. Plane p = p_cam_projection.xform4(Plane(1, 0, -1, 1));
  2740. p.normal /= p.d;
  2741. float unit_size = p.normal.x;
  2742. //copy normal and roughness to effect buffer
  2743. glBindFramebuffer(GL_READ_FRAMEBUFFER, storage->frame.current_rt->buffers.fbo);
  2744. glReadBuffer(GL_COLOR_ATTACHMENT3);
  2745. glBindFramebuffer(GL_DRAW_FRAMEBUFFER, storage->frame.current_rt->effects.ssao.blur_fbo[0]);
  2746. glBlitFramebuffer(0, 0, storage->frame.current_rt->width, storage->frame.current_rt->height, 0, 0, storage->frame.current_rt->width, storage->frame.current_rt->height, GL_COLOR_BUFFER_BIT, GL_LINEAR);
  2747. state.sss_shader.set_conditional(SubsurfScatteringShaderGLES3::USE_ORTHOGONAL_PROJECTION, p_cam_projection.is_orthogonal());
  2748. state.sss_shader.set_conditional(SubsurfScatteringShaderGLES3::USE_11_SAMPLES, subsurface_scatter_quality == SSS_QUALITY_LOW);
  2749. state.sss_shader.set_conditional(SubsurfScatteringShaderGLES3::USE_17_SAMPLES, subsurface_scatter_quality == SSS_QUALITY_MEDIUM);
  2750. state.sss_shader.set_conditional(SubsurfScatteringShaderGLES3::USE_25_SAMPLES, subsurface_scatter_quality == SSS_QUALITY_HIGH);
  2751. state.sss_shader.set_conditional(SubsurfScatteringShaderGLES3::ENABLE_FOLLOW_SURFACE, subsurface_scatter_follow_surface);
  2752. state.sss_shader.set_conditional(SubsurfScatteringShaderGLES3::ENABLE_STRENGTH_WEIGHTING, subsurface_scatter_weight_samples);
  2753. state.sss_shader.bind();
  2754. state.sss_shader.set_uniform(SubsurfScatteringShaderGLES3::MAX_RADIUS, subsurface_scatter_size);
  2755. state.sss_shader.set_uniform(SubsurfScatteringShaderGLES3::UNIT_SIZE, unit_size);
  2756. state.sss_shader.set_uniform(SubsurfScatteringShaderGLES3::CAMERA_Z_NEAR, p_cam_projection.get_z_near());
  2757. state.sss_shader.set_uniform(SubsurfScatteringShaderGLES3::CAMERA_Z_FAR, p_cam_projection.get_z_far());
  2758. state.sss_shader.set_uniform(SubsurfScatteringShaderGLES3::DIR, Vector2(1, 0));
  2759. glActiveTexture(GL_TEXTURE0);
  2760. glBindTexture(GL_TEXTURE_2D, storage->frame.current_rt->effects.mip_maps[0].color);
  2761. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); //disable filter (fixes bugs on AMD)
  2762. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  2763. glActiveTexture(GL_TEXTURE1);
  2764. glBindTexture(GL_TEXTURE_2D, storage->frame.current_rt->effects.ssao.blur_red[0]);
  2765. glActiveTexture(GL_TEXTURE2);
  2766. glBindTexture(GL_TEXTURE_2D, storage->frame.current_rt->depth);
  2767. //glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE, GL_NONE);
  2768. glBindFramebuffer(GL_FRAMEBUFFER, storage->frame.current_rt->fbo); //copy to front first
  2769. _copy_screen(true);
  2770. glActiveTexture(GL_TEXTURE0);
  2771. glBindTexture(GL_TEXTURE_2D, storage->frame.current_rt->color);
  2772. state.sss_shader.set_uniform(SubsurfScatteringShaderGLES3::DIR, Vector2(0, 1));
  2773. glBindFramebuffer(GL_FRAMEBUFFER, storage->frame.current_rt->effects.mip_maps[0].sizes[0].fbo); // copy to base level
  2774. _copy_screen(true);
  2775. glBindTexture(GL_TEXTURE_2D, storage->frame.current_rt->effects.mip_maps[0].color); //restore filter
  2776. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  2777. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
  2778. }
  2779. if (env->ssr_enabled) {
  2780. //blur diffuse into effect mipmaps using separatable convolution
  2781. //storage->shaders.copy.set_conditional(CopyShaderGLES3::GAUSSIAN_HORIZONTAL,true);
  2782. _blur_effect_buffer();
  2783. //perform SSR
  2784. state.ssr_shader.set_conditional(ScreenSpaceReflectionShaderGLES3::REFLECT_ROUGHNESS, env->ssr_roughness);
  2785. state.ssr_shader.set_conditional(ScreenSpaceReflectionShaderGLES3::USE_ORTHOGONAL_PROJECTION, p_cam_projection.is_orthogonal());
  2786. state.ssr_shader.bind();
  2787. int ssr_w = storage->frame.current_rt->effects.mip_maps[1].sizes[0].width;
  2788. int ssr_h = storage->frame.current_rt->effects.mip_maps[1].sizes[0].height;
  2789. state.ssr_shader.set_uniform(ScreenSpaceReflectionShaderGLES3::PIXEL_SIZE, Vector2(1.0 / (ssr_w * 0.5), 1.0 / (ssr_h * 0.5)));
  2790. state.ssr_shader.set_uniform(ScreenSpaceReflectionShaderGLES3::CAMERA_Z_NEAR, p_cam_projection.get_z_near());
  2791. state.ssr_shader.set_uniform(ScreenSpaceReflectionShaderGLES3::CAMERA_Z_FAR, p_cam_projection.get_z_far());
  2792. state.ssr_shader.set_uniform(ScreenSpaceReflectionShaderGLES3::PROJECTION, p_cam_projection);
  2793. state.ssr_shader.set_uniform(ScreenSpaceReflectionShaderGLES3::INVERSE_PROJECTION, p_cam_projection.inverse());
  2794. state.ssr_shader.set_uniform(ScreenSpaceReflectionShaderGLES3::VIEWPORT_SIZE, Size2(ssr_w, ssr_h));
  2795. //state.ssr_shader.set_uniform(ScreenSpaceReflectionShaderGLES3::FRAME_INDEX,int(render_pass));
  2796. state.ssr_shader.set_uniform(ScreenSpaceReflectionShaderGLES3::FILTER_MIPMAP_LEVELS, float(storage->frame.current_rt->effects.mip_maps[0].sizes.size()));
  2797. state.ssr_shader.set_uniform(ScreenSpaceReflectionShaderGLES3::NUM_STEPS, env->ssr_max_steps);
  2798. state.ssr_shader.set_uniform(ScreenSpaceReflectionShaderGLES3::DEPTH_TOLERANCE, env->ssr_depth_tolerance);
  2799. state.ssr_shader.set_uniform(ScreenSpaceReflectionShaderGLES3::DISTANCE_FADE, env->ssr_fade_out);
  2800. state.ssr_shader.set_uniform(ScreenSpaceReflectionShaderGLES3::CURVE_FADE_IN, env->ssr_fade_in);
  2801. glActiveTexture(GL_TEXTURE0);
  2802. glBindTexture(GL_TEXTURE_2D, storage->frame.current_rt->effects.mip_maps[0].color);
  2803. glActiveTexture(GL_TEXTURE1);
  2804. glBindTexture(GL_TEXTURE_2D, storage->frame.current_rt->buffers.effect);
  2805. glActiveTexture(GL_TEXTURE2);
  2806. glBindTexture(GL_TEXTURE_2D, storage->frame.current_rt->depth);
  2807. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE, GL_NONE);
  2808. glBindFramebuffer(GL_FRAMEBUFFER, storage->frame.current_rt->effects.mip_maps[1].sizes[0].fbo);
  2809. glViewport(0, 0, ssr_w, ssr_h);
  2810. _copy_screen(true);
  2811. glViewport(0, 0, storage->frame.current_rt->width, storage->frame.current_rt->height);
  2812. }
  2813. glBindFramebuffer(GL_READ_FRAMEBUFFER, storage->frame.current_rt->buffers.fbo);
  2814. glReadBuffer(GL_COLOR_ATTACHMENT1);
  2815. glBindFramebuffer(GL_DRAW_FRAMEBUFFER, storage->frame.current_rt->fbo);
  2816. //glDrawBuffer(GL_COLOR_ATTACHMENT0);
  2817. glBlitFramebuffer(0, 0, storage->frame.current_rt->width, storage->frame.current_rt->height, 0, 0, storage->frame.current_rt->width, storage->frame.current_rt->height, GL_COLOR_BUFFER_BIT, GL_NEAREST);
  2818. glReadBuffer(GL_COLOR_ATTACHMENT0);
  2819. glBindFramebuffer(GL_READ_FRAMEBUFFER, 0);
  2820. glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0);
  2821. //copy reflection over diffuse, resolving SSR if needed
  2822. state.resolve_shader.set_conditional(ResolveShaderGLES3::USE_SSR, env->ssr_enabled);
  2823. state.resolve_shader.bind();
  2824. state.resolve_shader.set_uniform(ResolveShaderGLES3::PIXEL_SIZE, Vector2(1.0 / storage->frame.current_rt->width, 1.0 / storage->frame.current_rt->height));
  2825. glActiveTexture(GL_TEXTURE0);
  2826. glBindTexture(GL_TEXTURE_2D, storage->frame.current_rt->color);
  2827. if (env->ssr_enabled) {
  2828. glActiveTexture(GL_TEXTURE1);
  2829. glBindTexture(GL_TEXTURE_2D, storage->frame.current_rt->effects.mip_maps[1].color);
  2830. }
  2831. glBindFramebuffer(GL_FRAMEBUFFER, storage->frame.current_rt->effects.mip_maps[0].sizes[0].fbo);
  2832. glEnable(GL_BLEND);
  2833. glBlendEquation(GL_FUNC_ADD);
  2834. glBlendFunc(GL_ONE, GL_ONE); //use additive to accumulate one over the other
  2835. _copy_screen(true);
  2836. glDisable(GL_BLEND); //end additive
  2837. if (state.used_screen_texture) {
  2838. _blur_effect_buffer();
  2839. //restored framebuffer
  2840. glBindFramebuffer(GL_FRAMEBUFFER, storage->frame.current_rt->effects.mip_maps[0].sizes[0].fbo);
  2841. glViewport(0, 0, storage->frame.current_rt->width, storage->frame.current_rt->height);
  2842. }
  2843. state.effect_blur_shader.set_conditional(EffectBlurShaderGLES3::SIMPLE_COPY, true);
  2844. state.effect_blur_shader.bind();
  2845. state.effect_blur_shader.set_uniform(EffectBlurShaderGLES3::LOD, float(0));
  2846. {
  2847. GLuint db = GL_COLOR_ATTACHMENT0;
  2848. glDrawBuffers(1, &db);
  2849. }
  2850. glBindFramebuffer(GL_FRAMEBUFFER, storage->frame.current_rt->buffers.fbo);
  2851. glActiveTexture(GL_TEXTURE0);
  2852. glBindTexture(GL_TEXTURE_2D, storage->frame.current_rt->effects.mip_maps[0].color);
  2853. _copy_screen(true);
  2854. state.effect_blur_shader.set_conditional(EffectBlurShaderGLES3::SIMPLE_COPY, false);
  2855. }
  2856. void RasterizerSceneGLES3::_post_process(Environment *env, const CameraMatrix &p_cam_projection) {
  2857. //copy to front buffer
  2858. glDepthMask(GL_FALSE);
  2859. glDisable(GL_DEPTH_TEST);
  2860. glDisable(GL_CULL_FACE);
  2861. glDisable(GL_BLEND);
  2862. glDepthFunc(GL_LEQUAL);
  2863. glColorMask(1, 1, 1, 1);
  2864. //turn off everything used
  2865. //copy specular to front buffer
  2866. //copy diffuse to effect buffer
  2867. if (storage->frame.current_rt->buffers.active) {
  2868. //transfer to effect buffer if using buffers, also resolve MSAA
  2869. glBindFramebuffer(GL_READ_FRAMEBUFFER, storage->frame.current_rt->buffers.fbo);
  2870. glBindFramebuffer(GL_DRAW_FRAMEBUFFER, storage->frame.current_rt->effects.mip_maps[0].sizes[0].fbo);
  2871. glBlitFramebuffer(0, 0, storage->frame.current_rt->width, storage->frame.current_rt->height, 0, 0, storage->frame.current_rt->width, storage->frame.current_rt->height, GL_COLOR_BUFFER_BIT, GL_NEAREST);
  2872. glBindFramebuffer(GL_READ_FRAMEBUFFER, 0);
  2873. glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0);
  2874. }
  2875. if ((!env || storage->frame.current_rt->width < 4 || storage->frame.current_rt->height < 4) && !storage->frame.current_rt->use_fxaa && !storage->frame.current_rt->use_debanding && storage->frame.current_rt->sharpen_intensity < 0.001) { //no post process on small render targets
  2876. //no environment or transparent render, simply return and convert to SRGB
  2877. if (storage->frame.current_rt->external.fbo != 0) {
  2878. glBindFramebuffer(GL_FRAMEBUFFER, storage->frame.current_rt->external.fbo);
  2879. } else {
  2880. glBindFramebuffer(GL_FRAMEBUFFER, storage->frame.current_rt->fbo);
  2881. }
  2882. glActiveTexture(GL_TEXTURE0);
  2883. glBindTexture(GL_TEXTURE_2D, storage->frame.current_rt->effects.mip_maps[0].color);
  2884. storage->shaders.copy.set_conditional(CopyShaderGLES3::LINEAR_TO_SRGB, !storage->frame.current_rt->flags[RasterizerStorage::RENDER_TARGET_KEEP_3D_LINEAR]);
  2885. storage->shaders.copy.set_conditional(CopyShaderGLES3::V_FLIP, storage->frame.current_rt->flags[RasterizerStorage::RENDER_TARGET_VFLIP]);
  2886. storage->shaders.copy.set_conditional(CopyShaderGLES3::DISABLE_ALPHA, !storage->frame.current_rt->flags[RasterizerStorage::RENDER_TARGET_TRANSPARENT]);
  2887. storage->shaders.copy.bind();
  2888. _copy_screen(true);
  2889. storage->shaders.copy.set_conditional(CopyShaderGLES3::LINEAR_TO_SRGB, false);
  2890. storage->shaders.copy.set_conditional(CopyShaderGLES3::DISABLE_ALPHA, false); //compute luminance
  2891. storage->shaders.copy.set_conditional(CopyShaderGLES3::V_FLIP, false);
  2892. return;
  2893. }
  2894. //order of operation
  2895. //1) DOF Blur (first blur, then copy to buffer applying the blur)
  2896. //2) FXAA
  2897. //3) Bloom (Glow)
  2898. //4) Tonemap
  2899. //5) Adjustments
  2900. GLuint composite_from = storage->frame.current_rt->effects.mip_maps[0].color;
  2901. if (env && env->dof_blur_far_enabled) {
  2902. //blur diffuse into effect mipmaps using separatable convolution
  2903. //storage->shaders.copy.set_conditional(CopyShaderGLES3::GAUSSIAN_HORIZONTAL,true);
  2904. int vp_h = storage->frame.current_rt->height;
  2905. int vp_w = storage->frame.current_rt->width;
  2906. state.effect_blur_shader.set_conditional(EffectBlurShaderGLES3::USE_ORTHOGONAL_PROJECTION, p_cam_projection.is_orthogonal());
  2907. state.effect_blur_shader.set_conditional(EffectBlurShaderGLES3::DOF_FAR_BLUR, true);
  2908. state.effect_blur_shader.set_conditional(EffectBlurShaderGLES3::DOF_QUALITY_LOW, env->dof_blur_far_quality == VS::ENV_DOF_BLUR_QUALITY_LOW);
  2909. state.effect_blur_shader.set_conditional(EffectBlurShaderGLES3::DOF_QUALITY_MEDIUM, env->dof_blur_far_quality == VS::ENV_DOF_BLUR_QUALITY_MEDIUM);
  2910. state.effect_blur_shader.set_conditional(EffectBlurShaderGLES3::DOF_QUALITY_HIGH, env->dof_blur_far_quality == VS::ENV_DOF_BLUR_QUALITY_HIGH);
  2911. state.effect_blur_shader.bind();
  2912. int qsteps[3] = { 4, 10, 20 };
  2913. float radius = (env->dof_blur_far_amount * env->dof_blur_far_amount) / qsteps[env->dof_blur_far_quality];
  2914. state.effect_blur_shader.set_uniform(EffectBlurShaderGLES3::DOF_BEGIN, env->dof_blur_far_distance);
  2915. state.effect_blur_shader.set_uniform(EffectBlurShaderGLES3::DOF_END, env->dof_blur_far_distance + env->dof_blur_far_transition);
  2916. state.effect_blur_shader.set_uniform(EffectBlurShaderGLES3::DOF_DIR, Vector2(1, 0));
  2917. state.effect_blur_shader.set_uniform(EffectBlurShaderGLES3::DOF_RADIUS, radius);
  2918. state.effect_blur_shader.set_uniform(EffectBlurShaderGLES3::PIXEL_SIZE, Vector2(1.0 / vp_w, 1.0 / vp_h));
  2919. state.effect_blur_shader.set_uniform(EffectBlurShaderGLES3::CAMERA_Z_NEAR, p_cam_projection.get_z_near());
  2920. state.effect_blur_shader.set_uniform(EffectBlurShaderGLES3::CAMERA_Z_FAR, p_cam_projection.get_z_far());
  2921. glActiveTexture(GL_TEXTURE1);
  2922. glBindTexture(GL_TEXTURE_2D, storage->frame.current_rt->depth);
  2923. glActiveTexture(GL_TEXTURE0);
  2924. glBindTexture(GL_TEXTURE_2D, composite_from);
  2925. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  2926. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  2927. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  2928. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  2929. glBindFramebuffer(GL_FRAMEBUFFER, storage->frame.current_rt->fbo); //copy to front first
  2930. _copy_screen(true);
  2931. glActiveTexture(GL_TEXTURE0);
  2932. glBindTexture(GL_TEXTURE_2D, storage->frame.current_rt->color);
  2933. state.effect_blur_shader.set_uniform(EffectBlurShaderGLES3::DOF_DIR, Vector2(0, 1));
  2934. glBindFramebuffer(GL_FRAMEBUFFER, storage->frame.current_rt->effects.mip_maps[0].sizes[0].fbo); // copy to base level
  2935. _copy_screen();
  2936. state.effect_blur_shader.set_conditional(EffectBlurShaderGLES3::DOF_FAR_BLUR, false);
  2937. state.effect_blur_shader.set_conditional(EffectBlurShaderGLES3::DOF_QUALITY_LOW, false);
  2938. state.effect_blur_shader.set_conditional(EffectBlurShaderGLES3::DOF_QUALITY_MEDIUM, false);
  2939. state.effect_blur_shader.set_conditional(EffectBlurShaderGLES3::DOF_QUALITY_HIGH, false);
  2940. state.effect_blur_shader.set_conditional(EffectBlurShaderGLES3::USE_ORTHOGONAL_PROJECTION, false);
  2941. composite_from = storage->frame.current_rt->effects.mip_maps[0].color;
  2942. }
  2943. if (env && env->dof_blur_near_enabled) {
  2944. //blur diffuse into effect mipmaps using separatable convolution
  2945. //storage->shaders.copy.set_conditional(CopyShaderGLES3::GAUSSIAN_HORIZONTAL,true);
  2946. int vp_h = storage->frame.current_rt->height;
  2947. int vp_w = storage->frame.current_rt->width;
  2948. state.effect_blur_shader.set_conditional(EffectBlurShaderGLES3::USE_ORTHOGONAL_PROJECTION, p_cam_projection.is_orthogonal());
  2949. state.effect_blur_shader.set_conditional(EffectBlurShaderGLES3::DOF_NEAR_BLUR, true);
  2950. state.effect_blur_shader.set_conditional(EffectBlurShaderGLES3::DOF_NEAR_FIRST_TAP, true);
  2951. state.effect_blur_shader.set_conditional(EffectBlurShaderGLES3::DOF_QUALITY_LOW, env->dof_blur_near_quality == VS::ENV_DOF_BLUR_QUALITY_LOW);
  2952. state.effect_blur_shader.set_conditional(EffectBlurShaderGLES3::DOF_QUALITY_MEDIUM, env->dof_blur_near_quality == VS::ENV_DOF_BLUR_QUALITY_MEDIUM);
  2953. state.effect_blur_shader.set_conditional(EffectBlurShaderGLES3::DOF_QUALITY_HIGH, env->dof_blur_near_quality == VS::ENV_DOF_BLUR_QUALITY_HIGH);
  2954. state.effect_blur_shader.bind();
  2955. int qsteps[3] = { 4, 10, 20 };
  2956. float radius = (env->dof_blur_near_amount * env->dof_blur_near_amount) / qsteps[env->dof_blur_near_quality];
  2957. state.effect_blur_shader.set_uniform(EffectBlurShaderGLES3::DOF_BEGIN, env->dof_blur_near_distance);
  2958. state.effect_blur_shader.set_uniform(EffectBlurShaderGLES3::DOF_END, env->dof_blur_near_distance - env->dof_blur_near_transition);
  2959. state.effect_blur_shader.set_uniform(EffectBlurShaderGLES3::DOF_DIR, Vector2(1, 0));
  2960. state.effect_blur_shader.set_uniform(EffectBlurShaderGLES3::DOF_RADIUS, radius);
  2961. state.effect_blur_shader.set_uniform(EffectBlurShaderGLES3::PIXEL_SIZE, Vector2(1.0 / vp_w, 1.0 / vp_h));
  2962. state.effect_blur_shader.set_uniform(EffectBlurShaderGLES3::CAMERA_Z_NEAR, p_cam_projection.get_z_near());
  2963. state.effect_blur_shader.set_uniform(EffectBlurShaderGLES3::CAMERA_Z_FAR, p_cam_projection.get_z_far());
  2964. glActiveTexture(GL_TEXTURE1);
  2965. glBindTexture(GL_TEXTURE_2D, storage->frame.current_rt->depth);
  2966. glActiveTexture(GL_TEXTURE0);
  2967. glBindTexture(GL_TEXTURE_2D, composite_from);
  2968. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  2969. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  2970. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  2971. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  2972. glBindFramebuffer(GL_FRAMEBUFFER, storage->frame.current_rt->fbo); //copy to front first
  2973. _copy_screen();
  2974. //manually do the blend if this is the first operation resolving from the diffuse buffer
  2975. state.effect_blur_shader.set_conditional(EffectBlurShaderGLES3::DOF_NEAR_BLUR_MERGE, composite_from == storage->frame.current_rt->buffers.diffuse);
  2976. state.effect_blur_shader.set_conditional(EffectBlurShaderGLES3::DOF_NEAR_FIRST_TAP, false);
  2977. state.effect_blur_shader.bind();
  2978. state.effect_blur_shader.set_uniform(EffectBlurShaderGLES3::DOF_BEGIN, env->dof_blur_near_distance);
  2979. state.effect_blur_shader.set_uniform(EffectBlurShaderGLES3::DOF_END, env->dof_blur_near_distance - env->dof_blur_near_transition);
  2980. state.effect_blur_shader.set_uniform(EffectBlurShaderGLES3::DOF_DIR, Vector2(0, 1));
  2981. state.effect_blur_shader.set_uniform(EffectBlurShaderGLES3::DOF_RADIUS, radius);
  2982. state.effect_blur_shader.set_uniform(EffectBlurShaderGLES3::PIXEL_SIZE, Vector2(1.0 / vp_w, 1.0 / vp_h));
  2983. state.effect_blur_shader.set_uniform(EffectBlurShaderGLES3::CAMERA_Z_NEAR, p_cam_projection.get_z_near());
  2984. state.effect_blur_shader.set_uniform(EffectBlurShaderGLES3::CAMERA_Z_FAR, p_cam_projection.get_z_far());
  2985. glActiveTexture(GL_TEXTURE0);
  2986. glBindTexture(GL_TEXTURE_2D, storage->frame.current_rt->color);
  2987. glBindFramebuffer(GL_FRAMEBUFFER, storage->frame.current_rt->effects.mip_maps[0].sizes[0].fbo); // copy to base level
  2988. if (composite_from != storage->frame.current_rt->buffers.diffuse) {
  2989. glEnable(GL_BLEND);
  2990. glBlendEquation(GL_FUNC_ADD);
  2991. // Alpha was used by the horizontal pass, it should not carry over.
  2992. glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_ZERO, GL_ONE);
  2993. } else {
  2994. glActiveTexture(GL_TEXTURE2);
  2995. glBindTexture(GL_TEXTURE_2D, storage->frame.current_rt->buffers.diffuse);
  2996. }
  2997. _copy_screen(true);
  2998. if (composite_from != storage->frame.current_rt->buffers.diffuse) {
  2999. glDisable(GL_BLEND);
  3000. }
  3001. state.effect_blur_shader.set_conditional(EffectBlurShaderGLES3::DOF_NEAR_BLUR, false);
  3002. state.effect_blur_shader.set_conditional(EffectBlurShaderGLES3::DOF_NEAR_FIRST_TAP, false);
  3003. state.effect_blur_shader.set_conditional(EffectBlurShaderGLES3::DOF_NEAR_BLUR_MERGE, false);
  3004. state.effect_blur_shader.set_conditional(EffectBlurShaderGLES3::DOF_QUALITY_LOW, false);
  3005. state.effect_blur_shader.set_conditional(EffectBlurShaderGLES3::DOF_QUALITY_MEDIUM, false);
  3006. state.effect_blur_shader.set_conditional(EffectBlurShaderGLES3::DOF_QUALITY_HIGH, false);
  3007. state.effect_blur_shader.set_conditional(EffectBlurShaderGLES3::USE_ORTHOGONAL_PROJECTION, false);
  3008. composite_from = storage->frame.current_rt->effects.mip_maps[0].color;
  3009. }
  3010. if (env && (env->dof_blur_near_enabled || env->dof_blur_far_enabled)) {
  3011. //these needed to disable filtering, reenamble
  3012. glActiveTexture(GL_TEXTURE0);
  3013. glBindTexture(GL_TEXTURE_2D, storage->frame.current_rt->effects.mip_maps[0].color);
  3014. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  3015. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
  3016. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  3017. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  3018. }
  3019. if (env && env->auto_exposure) {
  3020. //compute auto exposure
  3021. //first step, copy from image to luminance buffer
  3022. state.exposure_shader.set_conditional(ExposureShaderGLES3::EXPOSURE_BEGIN, true);
  3023. state.exposure_shader.bind();
  3024. int ss[2] = {
  3025. storage->frame.current_rt->width,
  3026. storage->frame.current_rt->height,
  3027. };
  3028. int ds[2] = {
  3029. exposure_shrink_size,
  3030. exposure_shrink_size,
  3031. };
  3032. glUniform2iv(state.exposure_shader.get_uniform(ExposureShaderGLES3::SOURCE_RENDER_SIZE), 1, ss);
  3033. glUniform2iv(state.exposure_shader.get_uniform(ExposureShaderGLES3::TARGET_SIZE), 1, ds);
  3034. glActiveTexture(GL_TEXTURE0);
  3035. glBindTexture(GL_TEXTURE_2D, composite_from);
  3036. glBindFramebuffer(GL_FRAMEBUFFER, exposure_shrink[0].fbo);
  3037. glViewport(0, 0, exposure_shrink_size, exposure_shrink_size);
  3038. _copy_screen(true);
  3039. //second step, shrink to 2x2 pixels
  3040. state.exposure_shader.set_conditional(ExposureShaderGLES3::EXPOSURE_BEGIN, false);
  3041. state.exposure_shader.bind();
  3042. //shrink from second to previous to last level
  3043. int s_size = exposure_shrink_size / 3;
  3044. for (int i = 1; i < exposure_shrink.size() - 1; i++) {
  3045. glBindFramebuffer(GL_FRAMEBUFFER, exposure_shrink[i].fbo);
  3046. glActiveTexture(GL_TEXTURE0);
  3047. glBindTexture(GL_TEXTURE_2D, exposure_shrink[i - 1].color);
  3048. _copy_screen();
  3049. glViewport(0, 0, s_size, s_size);
  3050. s_size /= 3;
  3051. }
  3052. //third step, shrink to 1x1 pixel taking in consideration the previous exposure
  3053. state.exposure_shader.set_conditional(ExposureShaderGLES3::EXPOSURE_END, true);
  3054. uint64_t tick = OS::get_singleton()->get_ticks_usec();
  3055. uint64_t tick_diff = storage->frame.current_rt->last_exposure_tick == 0 ? 0 : tick - storage->frame.current_rt->last_exposure_tick;
  3056. storage->frame.current_rt->last_exposure_tick = tick;
  3057. if (tick_diff == 0 || tick_diff > 1000000) {
  3058. state.exposure_shader.set_conditional(ExposureShaderGLES3::EXPOSURE_FORCE_SET, true);
  3059. }
  3060. state.exposure_shader.bind();
  3061. glBindFramebuffer(GL_FRAMEBUFFER, exposure_shrink[exposure_shrink.size() - 1].fbo);
  3062. glViewport(0, 0, 1, 1);
  3063. glActiveTexture(GL_TEXTURE0);
  3064. glBindTexture(GL_TEXTURE_2D, exposure_shrink[exposure_shrink.size() - 2].color);
  3065. glActiveTexture(GL_TEXTURE1);
  3066. glBindTexture(GL_TEXTURE_2D, storage->frame.current_rt->exposure.color); //read from previous
  3067. state.exposure_shader.set_uniform(ExposureShaderGLES3::EXPOSURE_ADJUST, env->auto_exposure_speed * (tick_diff / 1000000.0));
  3068. state.exposure_shader.set_uniform(ExposureShaderGLES3::MAX_LUMINANCE, env->auto_exposure_max);
  3069. state.exposure_shader.set_uniform(ExposureShaderGLES3::MIN_LUMINANCE, env->auto_exposure_min);
  3070. _copy_screen(true);
  3071. state.exposure_shader.set_conditional(ExposureShaderGLES3::EXPOSURE_FORCE_SET, false);
  3072. state.exposure_shader.set_conditional(ExposureShaderGLES3::EXPOSURE_END, false);
  3073. //last step, swap with the framebuffer exposure, so the right exposure is kept int he framebuffer
  3074. SWAP(exposure_shrink.write[exposure_shrink.size() - 1].fbo, storage->frame.current_rt->exposure.fbo);
  3075. SWAP(exposure_shrink.write[exposure_shrink.size() - 1].color, storage->frame.current_rt->exposure.color);
  3076. glViewport(0, 0, storage->frame.current_rt->width, storage->frame.current_rt->height);
  3077. VisualServerRaster::redraw_request(); //if using auto exposure, redraw must happen
  3078. }
  3079. int max_glow_level = -1;
  3080. int glow_mask = 0;
  3081. if (env && env->glow_enabled) {
  3082. for (int i = 0; i < VS::MAX_GLOW_LEVELS; i++) {
  3083. if (env->glow_levels & (1 << i)) {
  3084. if (i >= storage->frame.current_rt->effects.mip_maps[1].sizes.size()) {
  3085. max_glow_level = storage->frame.current_rt->effects.mip_maps[1].sizes.size() - 1;
  3086. glow_mask |= 1 << max_glow_level;
  3087. } else {
  3088. max_glow_level = i;
  3089. glow_mask |= (1 << i);
  3090. }
  3091. }
  3092. }
  3093. //blur diffuse into effect mipmaps using separatable convolution
  3094. //storage->shaders.copy.set_conditional(CopyShaderGLES3::GAUSSIAN_HORIZONTAL,true);
  3095. for (int i = 0; i < (max_glow_level + 1); i++) {
  3096. int vp_w = storage->frame.current_rt->effects.mip_maps[1].sizes[i].width;
  3097. int vp_h = storage->frame.current_rt->effects.mip_maps[1].sizes[i].height;
  3098. glViewport(0, 0, vp_w, vp_h);
  3099. //horizontal pass
  3100. if (i == 0) {
  3101. state.effect_blur_shader.set_conditional(EffectBlurShaderGLES3::GLOW_FIRST_PASS, true);
  3102. state.effect_blur_shader.set_conditional(EffectBlurShaderGLES3::GLOW_USE_AUTO_EXPOSURE, env->auto_exposure);
  3103. }
  3104. state.effect_blur_shader.set_conditional(EffectBlurShaderGLES3::GLOW_GAUSSIAN_HORIZONTAL, true);
  3105. state.effect_blur_shader.set_conditional(EffectBlurShaderGLES3::USE_GLOW_HIGH_QUALITY, env->glow_high_quality);
  3106. state.effect_blur_shader.bind();
  3107. state.effect_blur_shader.set_uniform(EffectBlurShaderGLES3::PIXEL_SIZE, Vector2(1.0 / vp_w, 1.0 / vp_h));
  3108. state.effect_blur_shader.set_uniform(EffectBlurShaderGLES3::LOD, float(i));
  3109. state.effect_blur_shader.set_uniform(EffectBlurShaderGLES3::GLOW_STRENGTH, env->glow_strength);
  3110. state.effect_blur_shader.set_uniform(EffectBlurShaderGLES3::LUMINANCE_CAP, env->glow_hdr_luminance_cap);
  3111. glActiveTexture(GL_TEXTURE0);
  3112. if (i == 0) {
  3113. glBindTexture(GL_TEXTURE_2D, composite_from);
  3114. state.effect_blur_shader.set_uniform(EffectBlurShaderGLES3::EXPOSURE, env->tone_mapper_exposure);
  3115. if (env->auto_exposure) {
  3116. state.effect_blur_shader.set_uniform(EffectBlurShaderGLES3::AUTO_EXPOSURE_GREY, env->auto_exposure_grey);
  3117. }
  3118. glActiveTexture(GL_TEXTURE1);
  3119. glBindTexture(GL_TEXTURE_2D, storage->frame.current_rt->exposure.color);
  3120. state.effect_blur_shader.set_uniform(EffectBlurShaderGLES3::GLOW_BLOOM, env->glow_bloom);
  3121. state.effect_blur_shader.set_uniform(EffectBlurShaderGLES3::GLOW_HDR_THRESHOLD, env->glow_hdr_bleed_threshold);
  3122. state.effect_blur_shader.set_uniform(EffectBlurShaderGLES3::GLOW_HDR_SCALE, env->glow_hdr_bleed_scale);
  3123. } else {
  3124. glBindTexture(GL_TEXTURE_2D, storage->frame.current_rt->effects.mip_maps[0].color); //previous level, since mipmaps[0] starts one level bigger
  3125. }
  3126. glBindFramebuffer(GL_FRAMEBUFFER, storage->frame.current_rt->effects.mip_maps[1].sizes[i].fbo);
  3127. _copy_screen(true);
  3128. state.effect_blur_shader.set_conditional(EffectBlurShaderGLES3::GLOW_GAUSSIAN_HORIZONTAL, false);
  3129. state.effect_blur_shader.set_conditional(EffectBlurShaderGLES3::GLOW_FIRST_PASS, false);
  3130. state.effect_blur_shader.set_conditional(EffectBlurShaderGLES3::GLOW_USE_AUTO_EXPOSURE, false);
  3131. //vertical pass
  3132. state.effect_blur_shader.set_conditional(EffectBlurShaderGLES3::GLOW_GAUSSIAN_VERTICAL, true);
  3133. state.effect_blur_shader.bind();
  3134. state.effect_blur_shader.set_uniform(EffectBlurShaderGLES3::PIXEL_SIZE, Vector2(1.0 / vp_w, 1.0 / vp_h));
  3135. state.effect_blur_shader.set_uniform(EffectBlurShaderGLES3::LOD, float(i));
  3136. state.effect_blur_shader.set_uniform(EffectBlurShaderGLES3::GLOW_STRENGTH, env->glow_strength);
  3137. glActiveTexture(GL_TEXTURE0);
  3138. glBindTexture(GL_TEXTURE_2D, storage->frame.current_rt->effects.mip_maps[1].color);
  3139. glBindFramebuffer(GL_FRAMEBUFFER, storage->frame.current_rt->effects.mip_maps[0].sizes[i + 1].fbo); //next level, since mipmaps[0] starts one level bigger
  3140. _copy_screen();
  3141. state.effect_blur_shader.set_conditional(EffectBlurShaderGLES3::GLOW_GAUSSIAN_VERTICAL, false);
  3142. }
  3143. glViewport(0, 0, storage->frame.current_rt->width, storage->frame.current_rt->height);
  3144. }
  3145. if (storage->frame.current_rt->external.fbo != 0) {
  3146. glBindFramebuffer(GL_FRAMEBUFFER, storage->frame.current_rt->external.fbo);
  3147. } else {
  3148. glBindFramebuffer(GL_FRAMEBUFFER, storage->frame.current_rt->fbo);
  3149. }
  3150. glActiveTexture(GL_TEXTURE0);
  3151. glBindTexture(GL_TEXTURE_2D, composite_from);
  3152. if (env) {
  3153. state.tonemap_shader.set_conditional(TonemapShaderGLES3::USE_FILMIC_TONEMAPPER, env->tone_mapper == VS::ENV_TONE_MAPPER_FILMIC);
  3154. state.tonemap_shader.set_conditional(TonemapShaderGLES3::USE_ACES_TONEMAPPER, env->tone_mapper == VS::ENV_TONE_MAPPER_ACES);
  3155. state.tonemap_shader.set_conditional(TonemapShaderGLES3::USE_ACES_FITTED_TONEMAPPER, env->tone_mapper == VS::ENV_TONE_MAPPER_ACES_FITTED);
  3156. state.tonemap_shader.set_conditional(TonemapShaderGLES3::USE_REINHARD_TONEMAPPER, env->tone_mapper == VS::ENV_TONE_MAPPER_REINHARD);
  3157. state.tonemap_shader.set_conditional(TonemapShaderGLES3::USE_AUTO_EXPOSURE, env->auto_exposure);
  3158. state.tonemap_shader.set_conditional(TonemapShaderGLES3::USE_GLOW_FILTER_BICUBIC, env->glow_bicubic_upscale);
  3159. }
  3160. state.tonemap_shader.set_conditional(TonemapShaderGLES3::KEEP_3D_LINEAR, storage->frame.current_rt->flags[RasterizerStorage::RENDER_TARGET_KEEP_3D_LINEAR]);
  3161. state.tonemap_shader.set_conditional(TonemapShaderGLES3::USE_FXAA, storage->frame.current_rt->use_fxaa);
  3162. state.tonemap_shader.set_conditional(TonemapShaderGLES3::USE_DEBANDING, storage->frame.current_rt->use_debanding);
  3163. state.tonemap_shader.set_conditional(TonemapShaderGLES3::USE_SHARPENING, storage->frame.current_rt->sharpen_intensity >= 0.001);
  3164. if (env && max_glow_level >= 0) {
  3165. for (int i = 0; i < (max_glow_level + 1); i++) {
  3166. if (glow_mask & (1 << i)) {
  3167. if (i == 0) {
  3168. state.tonemap_shader.set_conditional(TonemapShaderGLES3::USE_GLOW_LEVEL1, true);
  3169. }
  3170. if (i == 1) {
  3171. state.tonemap_shader.set_conditional(TonemapShaderGLES3::USE_GLOW_LEVEL2, true);
  3172. }
  3173. if (i == 2) {
  3174. state.tonemap_shader.set_conditional(TonemapShaderGLES3::USE_GLOW_LEVEL3, true);
  3175. }
  3176. if (i == 3) {
  3177. state.tonemap_shader.set_conditional(TonemapShaderGLES3::USE_GLOW_LEVEL4, true);
  3178. }
  3179. if (i == 4) {
  3180. state.tonemap_shader.set_conditional(TonemapShaderGLES3::USE_GLOW_LEVEL5, true);
  3181. }
  3182. if (i == 5) {
  3183. state.tonemap_shader.set_conditional(TonemapShaderGLES3::USE_GLOW_LEVEL6, true);
  3184. }
  3185. if (i == 6) {
  3186. state.tonemap_shader.set_conditional(TonemapShaderGLES3::USE_GLOW_LEVEL7, true);
  3187. }
  3188. }
  3189. }
  3190. state.tonemap_shader.set_conditional(TonemapShaderGLES3::USE_GLOW_SCREEN, env->glow_blend_mode == VS::GLOW_BLEND_MODE_SCREEN);
  3191. state.tonemap_shader.set_conditional(TonemapShaderGLES3::USE_GLOW_SOFTLIGHT, env->glow_blend_mode == VS::GLOW_BLEND_MODE_SOFTLIGHT);
  3192. state.tonemap_shader.set_conditional(TonemapShaderGLES3::USE_GLOW_REPLACE, env->glow_blend_mode == VS::GLOW_BLEND_MODE_REPLACE);
  3193. glActiveTexture(GL_TEXTURE2);
  3194. glBindTexture(GL_TEXTURE_2D, storage->frame.current_rt->effects.mip_maps[0].color);
  3195. }
  3196. if (env && env->adjustments_enabled) {
  3197. state.tonemap_shader.set_conditional(TonemapShaderGLES3::USE_BCS, true);
  3198. RasterizerStorageGLES3::Texture *tex = storage->texture_owner.getornull(env->color_correction);
  3199. if (tex) {
  3200. state.tonemap_shader.set_conditional(TonemapShaderGLES3::USE_COLOR_CORRECTION, true);
  3201. glActiveTexture(GL_TEXTURE3);
  3202. glBindTexture(tex->target, tex->tex_id);
  3203. }
  3204. }
  3205. state.tonemap_shader.set_conditional(TonemapShaderGLES3::DISABLE_ALPHA, !storage->frame.current_rt->flags[RasterizerStorage::RENDER_TARGET_TRANSPARENT]);
  3206. state.tonemap_shader.set_conditional(TonemapShaderGLES3::V_FLIP, storage->frame.current_rt->flags[RasterizerStorage::RENDER_TARGET_VFLIP]);
  3207. state.tonemap_shader.bind();
  3208. if (env) {
  3209. state.tonemap_shader.set_uniform(TonemapShaderGLES3::EXPOSURE, env->tone_mapper_exposure);
  3210. state.tonemap_shader.set_uniform(TonemapShaderGLES3::WHITE, env->tone_mapper_exposure_white);
  3211. if (max_glow_level >= 0) {
  3212. state.tonemap_shader.set_uniform(TonemapShaderGLES3::GLOW_INTENSITY, env->glow_intensity);
  3213. int ss[2] = {
  3214. storage->frame.current_rt->width,
  3215. storage->frame.current_rt->height,
  3216. };
  3217. glUniform2iv(state.tonemap_shader.get_uniform(TonemapShaderGLES3::GLOW_TEXTURE_SIZE), 1, ss);
  3218. }
  3219. if (env->auto_exposure) {
  3220. glActiveTexture(GL_TEXTURE1);
  3221. glBindTexture(GL_TEXTURE_2D, storage->frame.current_rt->exposure.color);
  3222. state.tonemap_shader.set_uniform(TonemapShaderGLES3::AUTO_EXPOSURE_GREY, env->auto_exposure_grey);
  3223. }
  3224. if (env->adjustments_enabled) {
  3225. state.tonemap_shader.set_uniform(TonemapShaderGLES3::BCS, Vector3(env->adjustments_brightness, env->adjustments_contrast, env->adjustments_saturation));
  3226. }
  3227. } else {
  3228. // No environment, so no exposure.
  3229. state.tonemap_shader.set_uniform(TonemapShaderGLES3::EXPOSURE, 1.0);
  3230. }
  3231. if (storage->frame.current_rt->use_fxaa) {
  3232. state.tonemap_shader.set_uniform(TonemapShaderGLES3::PIXEL_SIZE, Vector2(1.0 / storage->frame.current_rt->width, 1.0 / storage->frame.current_rt->height));
  3233. }
  3234. if (storage->frame.current_rt->sharpen_intensity >= 0.001) {
  3235. state.tonemap_shader.set_uniform(TonemapShaderGLES3::SHARPEN_INTENSITY, storage->frame.current_rt->sharpen_intensity);
  3236. }
  3237. _copy_screen(true, true);
  3238. //turn off everything used
  3239. state.tonemap_shader.set_conditional(TonemapShaderGLES3::USE_FXAA, false);
  3240. state.tonemap_shader.set_conditional(TonemapShaderGLES3::USE_DEBANDING, false);
  3241. state.tonemap_shader.set_conditional(TonemapShaderGLES3::USE_SHARPENING, false);
  3242. state.tonemap_shader.set_conditional(TonemapShaderGLES3::USE_AUTO_EXPOSURE, false);
  3243. state.tonemap_shader.set_conditional(TonemapShaderGLES3::USE_FILMIC_TONEMAPPER, false);
  3244. state.tonemap_shader.set_conditional(TonemapShaderGLES3::USE_ACES_TONEMAPPER, false);
  3245. state.tonemap_shader.set_conditional(TonemapShaderGLES3::USE_ACES_FITTED_TONEMAPPER, false);
  3246. state.tonemap_shader.set_conditional(TonemapShaderGLES3::USE_REINHARD_TONEMAPPER, false);
  3247. state.tonemap_shader.set_conditional(TonemapShaderGLES3::USE_GLOW_LEVEL1, false);
  3248. state.tonemap_shader.set_conditional(TonemapShaderGLES3::USE_GLOW_LEVEL2, false);
  3249. state.tonemap_shader.set_conditional(TonemapShaderGLES3::USE_GLOW_LEVEL3, false);
  3250. state.tonemap_shader.set_conditional(TonemapShaderGLES3::USE_GLOW_LEVEL4, false);
  3251. state.tonemap_shader.set_conditional(TonemapShaderGLES3::USE_GLOW_LEVEL5, false);
  3252. state.tonemap_shader.set_conditional(TonemapShaderGLES3::USE_GLOW_LEVEL6, false);
  3253. state.tonemap_shader.set_conditional(TonemapShaderGLES3::USE_GLOW_LEVEL7, false);
  3254. state.tonemap_shader.set_conditional(TonemapShaderGLES3::USE_GLOW_REPLACE, false);
  3255. state.tonemap_shader.set_conditional(TonemapShaderGLES3::USE_GLOW_SCREEN, false);
  3256. state.tonemap_shader.set_conditional(TonemapShaderGLES3::USE_GLOW_SOFTLIGHT, false);
  3257. state.tonemap_shader.set_conditional(TonemapShaderGLES3::USE_GLOW_FILTER_BICUBIC, false);
  3258. state.tonemap_shader.set_conditional(TonemapShaderGLES3::USE_BCS, false);
  3259. state.tonemap_shader.set_conditional(TonemapShaderGLES3::USE_COLOR_CORRECTION, false);
  3260. state.tonemap_shader.set_conditional(TonemapShaderGLES3::V_FLIP, false);
  3261. state.tonemap_shader.set_conditional(TonemapShaderGLES3::DISABLE_ALPHA, false);
  3262. }
  3263. bool RasterizerSceneGLES3::_element_needs_directional_add(RenderList::Element *e) {
  3264. // return whether this element should take part in directional add
  3265. if (e->sort_key & SORT_KEY_UNSHADED_FLAG) {
  3266. return false;
  3267. }
  3268. for (int i = 0; i < state.directional_light_count; i++) {
  3269. LightInstance *l = directional_lights[i];
  3270. // any unbaked and unculled light?
  3271. if (e->instance->baked_light && l->light_ptr->bake_mode == VS::LightBakeMode::LIGHT_BAKE_ALL) {
  3272. continue;
  3273. }
  3274. if ((e->instance->layer_mask & l->light_ptr->cull_mask) == 0) {
  3275. continue;
  3276. }
  3277. return true;
  3278. }
  3279. return false; // no visible unbaked light
  3280. }
  3281. void RasterizerSceneGLES3::render_scene(const Transform &p_cam_transform, const CameraMatrix &p_cam_projection, const int p_eye, bool p_cam_ortogonal, InstanceBase **p_cull_result, int p_cull_count, RID *p_light_cull_result, int p_light_cull_count, RID *p_reflection_probe_cull_result, int p_reflection_probe_cull_count, RID p_environment, RID p_shadow_atlas, RID p_reflection_atlas, RID p_reflection_probe, int p_reflection_probe_pass) {
  3282. //first of all, make a new render pass
  3283. render_pass++;
  3284. //fill up ubo
  3285. storage->info.render.object_count += p_cull_count;
  3286. Environment *env = environment_owner.getornull(p_environment);
  3287. ShadowAtlas *shadow_atlas = shadow_atlas_owner.getornull(p_shadow_atlas);
  3288. ReflectionAtlas *reflection_atlas = reflection_atlas_owner.getornull(p_reflection_atlas);
  3289. bool use_shadows = shadow_atlas && shadow_atlas->size;
  3290. state.scene_shader.set_conditional(SceneShaderGLES3::USE_SHADOW, use_shadows);
  3291. if (use_shadows) {
  3292. glActiveTexture(GL_TEXTURE0 + storage->config.max_texture_image_units - 6);
  3293. glBindTexture(GL_TEXTURE_2D, shadow_atlas->depth);
  3294. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE, GL_COMPARE_REF_TO_TEXTURE);
  3295. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_FUNC, GL_LESS);
  3296. state.ubo_data.shadow_atlas_pixel_size[0] = 1.0 / shadow_atlas->size;
  3297. state.ubo_data.shadow_atlas_pixel_size[1] = 1.0 / shadow_atlas->size;
  3298. } else {
  3299. if (storage->config.async_compilation_enabled) {
  3300. // Avoid GL UB message id 131222 caused by shadow samplers not properly set up in the ubershader
  3301. glActiveTexture(GL_TEXTURE0 + storage->config.max_texture_image_units - 6);
  3302. glBindTexture(GL_TEXTURE_2D, storage->resources.depth_tex);
  3303. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE, GL_COMPARE_REF_TO_TEXTURE);
  3304. }
  3305. }
  3306. if (reflection_atlas && reflection_atlas->size) {
  3307. glActiveTexture(GL_TEXTURE0 + storage->config.max_texture_image_units - 4);
  3308. glBindTexture(GL_TEXTURE_2D, reflection_atlas->color);
  3309. }
  3310. if (p_reflection_probe.is_valid()) {
  3311. state.ubo_data.reflection_multiplier = 0.0;
  3312. } else {
  3313. state.ubo_data.reflection_multiplier = 1.0;
  3314. }
  3315. state.ubo_data.subsurface_scatter_width = subsurface_scatter_size;
  3316. state.ubo_data.z_offset = 0;
  3317. state.ubo_data.z_slope_scale = 0;
  3318. state.ubo_data.shadow_dual_paraboloid_render_side = 0;
  3319. state.ubo_data.shadow_dual_paraboloid_render_zfar = 0;
  3320. state.ubo_data.opaque_prepass_threshold = 0.99;
  3321. if (storage->frame.current_rt) {
  3322. int viewport_width_pixels = storage->frame.current_rt->width;
  3323. int viewport_height_pixels = storage->frame.current_rt->height;
  3324. state.ubo_data.viewport_size[0] = viewport_width_pixels;
  3325. state.ubo_data.viewport_size[1] = viewport_height_pixels;
  3326. state.ubo_data.screen_pixel_size[0] = 1.0 / viewport_width_pixels;
  3327. state.ubo_data.screen_pixel_size[1] = 1.0 / viewport_height_pixels;
  3328. }
  3329. _setup_environment(env, p_cam_projection, p_cam_transform, p_eye, p_reflection_probe.is_valid());
  3330. bool fb_cleared = false;
  3331. glDepthFunc(GL_LEQUAL);
  3332. state.used_contact_shadows = false;
  3333. state.prepared_depth_texture = false;
  3334. state.bound_depth_texture = false;
  3335. for (int i = 0; i < p_light_cull_count; i++) {
  3336. ERR_BREAK(i >= render_list.max_lights);
  3337. LightInstance *li = light_instance_owner.getptr(p_light_cull_result[i]);
  3338. if (li->light_ptr->param[VS::LIGHT_PARAM_CONTACT_SHADOW_SIZE] > CMP_EPSILON) {
  3339. state.used_contact_shadows = true;
  3340. }
  3341. }
  3342. // Do depth prepass if it's explicitly enabled
  3343. bool use_depth_prepass = storage->config.use_depth_prepass;
  3344. // If contact shadows are used then we need to do depth prepass even if it's otherwise disabled
  3345. use_depth_prepass = use_depth_prepass || state.used_contact_shadows;
  3346. // Never do depth prepass if effects are disabled or if we render overdraws
  3347. use_depth_prepass = use_depth_prepass && storage->frame.current_rt && !storage->frame.current_rt->flags[RasterizerStorage::RENDER_TARGET_NO_3D_EFFECTS];
  3348. use_depth_prepass = use_depth_prepass && state.debug_draw != VS::VIEWPORT_DEBUG_DRAW_OVERDRAW;
  3349. if (use_depth_prepass) {
  3350. //pre z pass
  3351. glDisable(GL_BLEND);
  3352. glDepthMask(GL_TRUE);
  3353. glEnable(GL_DEPTH_TEST);
  3354. glDisable(GL_SCISSOR_TEST);
  3355. glBindFramebuffer(GL_FRAMEBUFFER, storage->frame.current_rt->buffers.fbo);
  3356. glDrawBuffers(0, nullptr);
  3357. glViewport(0, 0, storage->frame.current_rt->width, storage->frame.current_rt->height);
  3358. glColorMask(0, 0, 0, 0);
  3359. glClearDepth(1.0f);
  3360. glClear(GL_DEPTH_BUFFER_BIT);
  3361. render_list.clear();
  3362. _fill_render_list(p_cull_result, p_cull_count, true, false);
  3363. render_list.sort_by_key(false);
  3364. state.scene_shader.set_conditional(SceneShaderGLES3::RENDER_DEPTH, true);
  3365. _render_list(render_list.elements, render_list.element_count, p_cam_transform, p_cam_projection, nullptr, false, false, true, false, false);
  3366. state.scene_shader.set_conditional(SceneShaderGLES3::RENDER_DEPTH, false);
  3367. glColorMask(1, 1, 1, 1);
  3368. if (state.used_contact_shadows) {
  3369. _prepare_depth_texture();
  3370. _bind_depth_texture();
  3371. }
  3372. fb_cleared = true;
  3373. render_pass++;
  3374. state.used_depth_prepass = true;
  3375. } else {
  3376. state.used_depth_prepass = false;
  3377. }
  3378. _setup_lights(p_light_cull_result, p_light_cull_count, p_cam_transform.affine_inverse(), p_cam_projection, p_shadow_atlas);
  3379. _setup_reflections(p_reflection_probe_cull_result, p_reflection_probe_cull_count, p_cam_transform.affine_inverse(), p_cam_projection, p_reflection_atlas, env);
  3380. bool use_mrt = false;
  3381. render_list.clear();
  3382. _fill_render_list(p_cull_result, p_cull_count, false, false);
  3383. //
  3384. glEnable(GL_BLEND);
  3385. glDepthMask(GL_TRUE);
  3386. glEnable(GL_DEPTH_TEST);
  3387. glDisable(GL_SCISSOR_TEST);
  3388. //rendering to a probe cubemap side
  3389. ReflectionProbeInstance *probe = reflection_probe_instance_owner.getornull(p_reflection_probe);
  3390. GLuint current_fbo;
  3391. if (probe) {
  3392. ReflectionAtlas *ref_atlas = reflection_atlas_owner.getptr(probe->atlas);
  3393. ERR_FAIL_COND(!ref_atlas);
  3394. int target_size = ref_atlas->size / ref_atlas->subdiv;
  3395. int cubemap_index = reflection_cubemaps.size() - 1;
  3396. for (int i = reflection_cubemaps.size() - 1; i >= 0; i--) {
  3397. //find appropriate cubemap to render to
  3398. if (reflection_cubemaps[i].size > target_size * 2) {
  3399. break;
  3400. }
  3401. cubemap_index = i;
  3402. }
  3403. current_fbo = reflection_cubemaps[cubemap_index].fbo_id[p_reflection_probe_pass];
  3404. use_mrt = false;
  3405. state.scene_shader.set_conditional(SceneShaderGLES3::USE_MULTIPLE_RENDER_TARGETS, false);
  3406. glViewport(0, 0, reflection_cubemaps[cubemap_index].size, reflection_cubemaps[cubemap_index].size);
  3407. glBindFramebuffer(GL_FRAMEBUFFER, current_fbo);
  3408. } else {
  3409. use_mrt = env && (state.used_sss || env->ssao_enabled || env->ssr_enabled || env->dof_blur_far_enabled || env->dof_blur_near_enabled); //only enable MRT rendering if any of these is enabled
  3410. //effects disabled and transparency also prevent using MRTs
  3411. use_mrt = use_mrt && !storage->frame.current_rt->flags[RasterizerStorage::RENDER_TARGET_TRANSPARENT];
  3412. use_mrt = use_mrt && !storage->frame.current_rt->flags[RasterizerStorage::RENDER_TARGET_NO_3D_EFFECTS];
  3413. use_mrt = use_mrt && state.debug_draw != VS::VIEWPORT_DEBUG_DRAW_OVERDRAW;
  3414. use_mrt = use_mrt && (env->bg_mode != VS::ENV_BG_KEEP && env->bg_mode != VS::ENV_BG_CANVAS);
  3415. glViewport(0, 0, storage->frame.current_rt->width, storage->frame.current_rt->height);
  3416. if (use_mrt) {
  3417. current_fbo = storage->frame.current_rt->buffers.fbo;
  3418. glBindFramebuffer(GL_FRAMEBUFFER, storage->frame.current_rt->buffers.fbo);
  3419. state.scene_shader.set_conditional(SceneShaderGLES3::USE_MULTIPLE_RENDER_TARGETS, true);
  3420. Vector<GLenum> draw_buffers;
  3421. draw_buffers.push_back(GL_COLOR_ATTACHMENT0);
  3422. draw_buffers.push_back(GL_COLOR_ATTACHMENT1);
  3423. draw_buffers.push_back(GL_COLOR_ATTACHMENT2);
  3424. if (state.used_sss) {
  3425. draw_buffers.push_back(GL_COLOR_ATTACHMENT3);
  3426. }
  3427. glDrawBuffers(draw_buffers.size(), draw_buffers.ptr());
  3428. Color black(0, 0, 0, 0);
  3429. glClearBufferfv(GL_COLOR, 1, black.components); // specular
  3430. glClearBufferfv(GL_COLOR, 2, black.components); // normal metal rough
  3431. if (state.used_sss) {
  3432. glClearBufferfv(GL_COLOR, 3, black.components); // normal metal rough
  3433. }
  3434. } else {
  3435. if (storage->frame.current_rt->buffers.active) {
  3436. current_fbo = storage->frame.current_rt->buffers.fbo;
  3437. } else {
  3438. if (storage->frame.current_rt->effects.mip_maps[0].sizes.size() == 0) {
  3439. ERR_PRINT_ONCE("Can't use canvas background mode in a render target configured without sampling");
  3440. return;
  3441. }
  3442. current_fbo = storage->frame.current_rt->effects.mip_maps[0].sizes[0].fbo;
  3443. }
  3444. glBindFramebuffer(GL_FRAMEBUFFER, current_fbo);
  3445. state.scene_shader.set_conditional(SceneShaderGLES3::USE_MULTIPLE_RENDER_TARGETS, false);
  3446. Vector<GLenum> draw_buffers;
  3447. draw_buffers.push_back(GL_COLOR_ATTACHMENT0);
  3448. glDrawBuffers(draw_buffers.size(), draw_buffers.ptr());
  3449. }
  3450. }
  3451. if (!fb_cleared) {
  3452. glClearDepth(1.0f);
  3453. glClear(GL_DEPTH_BUFFER_BIT);
  3454. }
  3455. Color clear_color(0, 0, 0, 0);
  3456. RasterizerStorageGLES3::Sky *sky = nullptr;
  3457. Ref<CameraFeed> feed;
  3458. if (state.debug_draw == VS::VIEWPORT_DEBUG_DRAW_OVERDRAW) {
  3459. clear_color = Color(0, 0, 0, 0);
  3460. storage->frame.clear_request = false;
  3461. } else if (!probe && storage->frame.current_rt->flags[RasterizerStorage::RENDER_TARGET_TRANSPARENT]) {
  3462. clear_color = Color(0, 0, 0, 0);
  3463. storage->frame.clear_request = false;
  3464. } else if (!env || env->bg_mode == VS::ENV_BG_CLEAR_COLOR) {
  3465. if (storage->frame.clear_request) {
  3466. clear_color = storage->frame.clear_request_color.to_linear();
  3467. storage->frame.clear_request = false;
  3468. }
  3469. } else if (env->bg_mode == VS::ENV_BG_CANVAS) {
  3470. clear_color = env->bg_color.to_linear();
  3471. storage->frame.clear_request = false;
  3472. } else if (env->bg_mode == VS::ENV_BG_COLOR) {
  3473. clear_color = env->bg_color.to_linear();
  3474. storage->frame.clear_request = false;
  3475. } else if (env->bg_mode == VS::ENV_BG_SKY) {
  3476. storage->frame.clear_request = false;
  3477. } else if (env->bg_mode == VS::ENV_BG_COLOR_SKY) {
  3478. clear_color = env->bg_color.to_linear();
  3479. storage->frame.clear_request = false;
  3480. } else if (env->bg_mode == VS::ENV_BG_CAMERA_FEED) {
  3481. feed = CameraServer::get_singleton()->get_feed_by_id(env->camera_feed_id);
  3482. storage->frame.clear_request = false;
  3483. } else {
  3484. storage->frame.clear_request = false;
  3485. }
  3486. if (!env || env->bg_mode != VS::ENV_BG_KEEP) {
  3487. glClearBufferfv(GL_COLOR, 0, clear_color.components); // specular
  3488. }
  3489. VS::EnvironmentBG bg_mode = (!env || (probe && env->bg_mode == VS::ENV_BG_CANVAS)) ? VS::ENV_BG_CLEAR_COLOR : env->bg_mode; //if no environment, or canvas while rendering a probe (invalid use case), use color.
  3490. if (env) {
  3491. switch (bg_mode) {
  3492. case VS::ENV_BG_COLOR_SKY:
  3493. case VS::ENV_BG_SKY:
  3494. sky = storage->sky_owner.getornull(env->sky);
  3495. break;
  3496. case VS::ENV_BG_CANVAS:
  3497. //copy canvas to 3d buffer and convert it to linear
  3498. glDisable(GL_BLEND);
  3499. glDepthMask(GL_FALSE);
  3500. glDisable(GL_DEPTH_TEST);
  3501. glDisable(GL_CULL_FACE);
  3502. glActiveTexture(GL_TEXTURE0);
  3503. glBindTexture(GL_TEXTURE_2D, storage->frame.current_rt->color);
  3504. storage->shaders.copy.set_conditional(CopyShaderGLES3::DISABLE_ALPHA, true);
  3505. storage->shaders.copy.set_conditional(CopyShaderGLES3::SRGB_TO_LINEAR, true);
  3506. storage->shaders.copy.bind();
  3507. _copy_screen(true, true);
  3508. //turn off everything used
  3509. storage->shaders.copy.set_conditional(CopyShaderGLES3::SRGB_TO_LINEAR, false);
  3510. storage->shaders.copy.set_conditional(CopyShaderGLES3::DISABLE_ALPHA, false);
  3511. //restore
  3512. glEnable(GL_BLEND);
  3513. glDepthMask(GL_TRUE);
  3514. glEnable(GL_DEPTH_TEST);
  3515. glEnable(GL_CULL_FACE);
  3516. break;
  3517. case VS::ENV_BG_CAMERA_FEED:
  3518. if (feed.is_valid() && (feed->get_base_width() > 0) && (feed->get_base_height() > 0)) {
  3519. // copy our camera feed to our background
  3520. glDisable(GL_BLEND);
  3521. glDepthMask(GL_FALSE);
  3522. glDisable(GL_DEPTH_TEST);
  3523. glDisable(GL_CULL_FACE);
  3524. storage->shaders.copy.set_conditional(CopyShaderGLES3::USE_DISPLAY_TRANSFORM, true);
  3525. storage->shaders.copy.set_conditional(CopyShaderGLES3::DISABLE_ALPHA, true);
  3526. storage->shaders.copy.set_conditional(CopyShaderGLES3::SRGB_TO_LINEAR, true);
  3527. if (feed->get_datatype() == CameraFeed::FEED_RGB) {
  3528. RID camera_RGBA = feed->get_texture(CameraServer::FEED_RGBA_IMAGE);
  3529. VS::get_singleton()->texture_bind(camera_RGBA, 0);
  3530. } else if (feed->get_datatype() == CameraFeed::FEED_YCBCR) {
  3531. RID camera_YCbCr = feed->get_texture(CameraServer::FEED_YCBCR_IMAGE);
  3532. VS::get_singleton()->texture_bind(camera_YCbCr, 0);
  3533. storage->shaders.copy.set_conditional(CopyShaderGLES3::YCBCR_TO_SRGB, true);
  3534. } else if (feed->get_datatype() == CameraFeed::FEED_YCBCR_SEP) {
  3535. RID camera_Y = feed->get_texture(CameraServer::FEED_Y_IMAGE);
  3536. RID camera_CbCr = feed->get_texture(CameraServer::FEED_CBCR_IMAGE);
  3537. VS::get_singleton()->texture_bind(camera_Y, 0);
  3538. VS::get_singleton()->texture_bind(camera_CbCr, 1);
  3539. storage->shaders.copy.set_conditional(CopyShaderGLES3::SEP_CBCR_TEXTURE, true);
  3540. storage->shaders.copy.set_conditional(CopyShaderGLES3::YCBCR_TO_SRGB, true);
  3541. };
  3542. storage->shaders.copy.bind();
  3543. storage->shaders.copy.set_uniform(CopyShaderGLES3::DISPLAY_TRANSFORM, feed->get_transform());
  3544. _copy_screen(true, true);
  3545. //turn off everything used
  3546. storage->shaders.copy.set_conditional(CopyShaderGLES3::USE_DISPLAY_TRANSFORM, false);
  3547. storage->shaders.copy.set_conditional(CopyShaderGLES3::DISABLE_ALPHA, false);
  3548. storage->shaders.copy.set_conditional(CopyShaderGLES3::SRGB_TO_LINEAR, false);
  3549. storage->shaders.copy.set_conditional(CopyShaderGLES3::SEP_CBCR_TEXTURE, false);
  3550. storage->shaders.copy.set_conditional(CopyShaderGLES3::YCBCR_TO_SRGB, false);
  3551. //restore
  3552. glEnable(GL_BLEND);
  3553. glDepthMask(GL_TRUE);
  3554. glEnable(GL_DEPTH_TEST);
  3555. glEnable(GL_CULL_FACE);
  3556. } else {
  3557. // don't have a feed, just show greenscreen :)
  3558. clear_color = Color(0.0, 1.0, 0.0, 1.0);
  3559. }
  3560. break;
  3561. default: {
  3562. }
  3563. }
  3564. }
  3565. if (probe && probe->probe_ptr->interior) {
  3566. sky = nullptr; //for rendering probe interiors, radiance must not be used.
  3567. }
  3568. state.texscreen_copied = false;
  3569. glBlendEquation(GL_FUNC_ADD);
  3570. if (storage->frame.current_rt && storage->frame.current_rt->flags[RasterizerStorage::RENDER_TARGET_TRANSPARENT]) {
  3571. glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
  3572. glEnable(GL_BLEND);
  3573. } else {
  3574. glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_ZERO, GL_ONE);
  3575. glDisable(GL_BLEND);
  3576. }
  3577. render_list.sort_by_key(false);
  3578. if (state.directional_light_count == 0) {
  3579. directional_light = nullptr;
  3580. _render_list(render_list.elements, render_list.element_count, p_cam_transform, p_cam_projection, sky, false, false, false, false, use_shadows);
  3581. } else {
  3582. for (int i = 0; i < state.directional_light_count; i++) {
  3583. directional_light = directional_lights[i];
  3584. if (i > 0) {
  3585. glEnable(GL_BLEND);
  3586. }
  3587. _setup_directional_light(i, p_cam_transform.affine_inverse(), use_shadows);
  3588. _render_list(render_list.elements, render_list.element_count, p_cam_transform, p_cam_projection, sky, false, false, false, i > 0, use_shadows);
  3589. }
  3590. }
  3591. state.scene_shader.set_conditional(SceneShaderGLES3::USE_MULTIPLE_RENDER_TARGETS, false);
  3592. if (use_mrt) {
  3593. GLenum gldb = GL_COLOR_ATTACHMENT0;
  3594. glDrawBuffers(1, &gldb);
  3595. }
  3596. if (env && env->bg_mode == VS::ENV_BG_SKY && (!storage->frame.current_rt || (!storage->frame.current_rt->flags[RasterizerStorage::RENDER_TARGET_TRANSPARENT] && state.debug_draw != VS::VIEWPORT_DEBUG_DRAW_OVERDRAW))) {
  3597. /*
  3598. if (use_mrt) {
  3599. glBindFramebuffer(GL_FRAMEBUFFER,storage->frame.current_rt->buffers.fbo); //switch to alpha fbo for sky, only diffuse/ambient matters
  3600. */
  3601. if (sky && sky->panorama.is_valid()) {
  3602. _draw_sky(sky, p_cam_projection, p_cam_transform, false, env->sky_custom_fov, env->bg_energy, env->sky_orientation);
  3603. }
  3604. }
  3605. //_render_list_forward(&alpha_render_list,camera_transform,camera_transform_inverse,camera_projection,false,fragment_lighting,true);
  3606. //glColorMask(1,1,1,1);
  3607. //state.scene_shader.set_conditional( SceneShaderGLES3::USE_FOG,false);
  3608. if (use_mrt) {
  3609. _render_mrts(env, p_cam_projection);
  3610. } else {
  3611. // Here we have to do the blits/resolves that otherwise are done in the MRT rendering, in particular
  3612. // - prepare screen texture for any geometry that uses a shader with screen texture
  3613. // - prepare depth texture for any geometry that uses a shader with depth texture
  3614. bool framebuffer_dirty = false;
  3615. if (storage->frame.current_rt && storage->frame.current_rt->buffers.active && state.used_screen_texture) {
  3616. glBindFramebuffer(GL_READ_FRAMEBUFFER, storage->frame.current_rt->buffers.fbo);
  3617. glReadBuffer(GL_COLOR_ATTACHMENT0);
  3618. glBindFramebuffer(GL_DRAW_FRAMEBUFFER, storage->frame.current_rt->effects.mip_maps[0].sizes[0].fbo);
  3619. glBlitFramebuffer(0, 0, storage->frame.current_rt->width, storage->frame.current_rt->height, 0, 0, storage->frame.current_rt->width, storage->frame.current_rt->height, GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT, GL_NEAREST);
  3620. glBindFramebuffer(GL_READ_FRAMEBUFFER, 0);
  3621. glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0);
  3622. _blur_effect_buffer();
  3623. framebuffer_dirty = true;
  3624. }
  3625. if (storage->frame.current_rt && storage->frame.current_rt->buffers.active && state.used_depth_texture) {
  3626. _prepare_depth_texture();
  3627. framebuffer_dirty = true;
  3628. }
  3629. if (framebuffer_dirty) {
  3630. // Restore framebuffer
  3631. glBindFramebuffer(GL_FRAMEBUFFER, storage->frame.current_rt->buffers.fbo);
  3632. glViewport(0, 0, storage->frame.current_rt->width, storage->frame.current_rt->height);
  3633. }
  3634. }
  3635. if (storage->frame.current_rt && state.used_depth_texture && storage->frame.current_rt->buffers.active) {
  3636. _bind_depth_texture();
  3637. }
  3638. if (storage->frame.current_rt && state.used_screen_texture && storage->frame.current_rt->buffers.active) {
  3639. glActiveTexture(GL_TEXTURE0 + storage->config.max_texture_image_units - 8);
  3640. glBindTexture(GL_TEXTURE_2D, storage->frame.current_rt->effects.mip_maps[0].color);
  3641. }
  3642. glEnable(GL_BLEND);
  3643. glDepthMask(GL_TRUE);
  3644. glEnable(GL_DEPTH_TEST);
  3645. glDisable(GL_SCISSOR_TEST);
  3646. render_list.sort_by_reverse_depth_and_priority(true);
  3647. if (state.directional_light_count <= 1) {
  3648. if (state.directional_light_count == 1) {
  3649. directional_light = directional_lights[0];
  3650. _setup_directional_light(0, p_cam_transform.affine_inverse(), use_shadows);
  3651. } else {
  3652. directional_light = nullptr;
  3653. }
  3654. _render_list(&render_list.elements[render_list.max_elements - render_list.alpha_element_count], render_list.alpha_element_count, p_cam_transform, p_cam_projection, sky, false, true, false, false, use_shadows);
  3655. } else {
  3656. // special handling for multiple directional lights
  3657. // first chunk_start
  3658. int chunk_split = render_list.max_elements - render_list.alpha_element_count;
  3659. while (chunk_split < render_list.max_elements) {
  3660. int chunk_start = chunk_split;
  3661. bool first = true;
  3662. bool chunk_directional_add = false;
  3663. uint32_t chunk_priority = 0;
  3664. // determine chunk end
  3665. for (; chunk_split < render_list.max_elements; chunk_split++) {
  3666. bool directional_add = _element_needs_directional_add(render_list.elements[chunk_split]);
  3667. uint32_t priority = uint32_t(render_list.elements[chunk_split]->sort_key >> RenderList::SORT_KEY_PRIORITY_SHIFT);
  3668. if (first) {
  3669. chunk_directional_add = directional_add;
  3670. chunk_priority = priority;
  3671. first = false;
  3672. }
  3673. if ((directional_add != chunk_directional_add) || (priority != chunk_priority)) {
  3674. break;
  3675. }
  3676. }
  3677. if (chunk_directional_add) {
  3678. for (int i = 0; i < state.directional_light_count; i++) {
  3679. directional_light = directional_lights[i];
  3680. _setup_directional_light(i, p_cam_transform.affine_inverse(), use_shadows);
  3681. _render_list(&render_list.elements[chunk_start], chunk_split - chunk_start, p_cam_transform, p_cam_projection, sky, false, true, false, i > 0, use_shadows);
  3682. }
  3683. } else {
  3684. directional_light = nullptr;
  3685. _render_list(&render_list.elements[chunk_start], chunk_split - chunk_start, p_cam_transform, p_cam_projection, sky, false, true, false, false, use_shadows);
  3686. }
  3687. }
  3688. }
  3689. if (probe) {
  3690. //rendering a probe, do no more!
  3691. return;
  3692. }
  3693. if (env && (env->dof_blur_far_enabled || env->dof_blur_near_enabled) && storage->frame.current_rt && storage->frame.current_rt->buffers.active) {
  3694. _prepare_depth_texture();
  3695. }
  3696. _post_process(env, p_cam_projection);
  3697. // Needed only for debugging
  3698. /* if (shadow_atlas && storage->frame.current_rt) {
  3699. //_copy_texture_to_front_buffer(shadow_atlas->depth);
  3700. storage->canvas->canvas_begin();
  3701. glActiveTexture(GL_TEXTURE0);
  3702. glBindTexture(GL_TEXTURE_2D, shadow_atlas->depth);
  3703. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE, GL_NONE);
  3704. storage->canvas->draw_generic_textured_rect(Rect2(0, 0, storage->frame.current_rt->width / 2, storage->frame.current_rt->height / 2), Rect2(0, 0, 1, 1));
  3705. }
  3706. if (storage->frame.current_rt) {
  3707. //_copy_texture_to_front_buffer(shadow_atlas->depth);
  3708. storage->canvas->canvas_begin();
  3709. glActiveTexture(GL_TEXTURE0);
  3710. glBindTexture(GL_TEXTURE_2D, exposure_shrink[4].color);
  3711. //glBindTexture(GL_TEXTURE_2D,storage->frame.current_rt->exposure.color);
  3712. storage->canvas->draw_generic_textured_rect(Rect2(0, 0, storage->frame.current_rt->width / 16, storage->frame.current_rt->height / 16), Rect2(0, 0, 1, 1));
  3713. }
  3714. if (reflection_atlas && storage->frame.current_rt) {
  3715. //_copy_texture_to_front_buffer(shadow_atlas->depth);
  3716. storage->canvas->canvas_begin();
  3717. glActiveTexture(GL_TEXTURE0);
  3718. glBindTexture(GL_TEXTURE_2D, reflection_atlas->color);
  3719. storage->canvas->draw_generic_textured_rect(Rect2(0, 0, storage->frame.current_rt->width / 2, storage->frame.current_rt->height / 2), Rect2(0, 0, 1, 1));
  3720. }
  3721. if (directional_shadow.fbo) {
  3722. //_copy_texture_to_front_buffer(shadow_atlas->depth);
  3723. storage->canvas->canvas_begin();
  3724. glActiveTexture(GL_TEXTURE0);
  3725. glBindTexture(GL_TEXTURE_2D, directional_shadow.depth);
  3726. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE, GL_NONE);
  3727. storage->canvas->draw_generic_textured_rect(Rect2(0, 0, storage->frame.current_rt->width / 2, storage->frame.current_rt->height / 2), Rect2(0, 0, 1, 1));
  3728. }
  3729. if ( env_radiance_tex) {
  3730. //_copy_texture_to_front_buffer(shadow_atlas->depth);
  3731. storage->canvas->canvas_begin();
  3732. glActiveTexture(GL_TEXTURE0);
  3733. glBindTexture(GL_TEXTURE_2D, env_radiance_tex);
  3734. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  3735. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  3736. storage->canvas->draw_generic_textured_rect(Rect2(0, 0, storage->frame.current_rt->width / 2, storage->frame.current_rt->height / 2), Rect2(0, 0, 1, 1));
  3737. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
  3738. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  3739. }*/
  3740. //disable all stuff
  3741. }
  3742. void RasterizerSceneGLES3::render_shadow(RID p_light, RID p_shadow_atlas, int p_pass, InstanceBase **p_cull_result, int p_cull_count) {
  3743. render_pass++;
  3744. directional_light = nullptr;
  3745. LightInstance *light_instance = light_instance_owner.getornull(p_light);
  3746. ERR_FAIL_COND(!light_instance);
  3747. RasterizerStorageGLES3::Light *light = storage->light_owner.getornull(light_instance->light);
  3748. ERR_FAIL_COND(!light);
  3749. uint32_t x, y, width, height;
  3750. float dp_direction = 0.0;
  3751. float zfar = 0;
  3752. bool flip_facing = false;
  3753. int custom_vp_size = 0;
  3754. GLuint fbo;
  3755. int current_cubemap = -1;
  3756. float bias = 0;
  3757. float normal_bias = 0;
  3758. state.used_depth_prepass = false;
  3759. CameraMatrix light_projection;
  3760. Transform light_transform;
  3761. if (light->type == VS::LIGHT_DIRECTIONAL) {
  3762. //set pssm stuff
  3763. if (light_instance->last_scene_shadow_pass != scene_pass) {
  3764. //assign rect if unassigned
  3765. light_instance->light_directional_index = directional_shadow.current_light;
  3766. light_instance->last_scene_shadow_pass = scene_pass;
  3767. directional_shadow.current_light++;
  3768. if (directional_shadow.light_count == 1) {
  3769. light_instance->directional_rect = Rect2(0, 0, directional_shadow.size, directional_shadow.size);
  3770. } else if (directional_shadow.light_count == 2) {
  3771. light_instance->directional_rect = Rect2(0, 0, directional_shadow.size, directional_shadow.size / 2);
  3772. if (light_instance->light_directional_index == 1) {
  3773. light_instance->directional_rect.position.y += light_instance->directional_rect.size.y;
  3774. }
  3775. } else { //3 and 4
  3776. light_instance->directional_rect = Rect2(0, 0, directional_shadow.size / 2, directional_shadow.size / 2);
  3777. if (light_instance->light_directional_index & 1) {
  3778. light_instance->directional_rect.position.x += light_instance->directional_rect.size.x;
  3779. }
  3780. if (light_instance->light_directional_index / 2) {
  3781. light_instance->directional_rect.position.y += light_instance->directional_rect.size.y;
  3782. }
  3783. }
  3784. }
  3785. light_projection = light_instance->shadow_transform[p_pass].camera;
  3786. light_transform = light_instance->shadow_transform[p_pass].transform;
  3787. x = light_instance->directional_rect.position.x;
  3788. y = light_instance->directional_rect.position.y;
  3789. width = light_instance->directional_rect.size.x;
  3790. height = light_instance->directional_rect.size.y;
  3791. if (light->directional_shadow_mode == VS::LIGHT_DIRECTIONAL_SHADOW_PARALLEL_4_SPLITS) {
  3792. width /= 2;
  3793. height /= 2;
  3794. if (p_pass == 1) {
  3795. x += width;
  3796. } else if (p_pass == 2) {
  3797. y += height;
  3798. } else if (p_pass == 3) {
  3799. x += width;
  3800. y += height;
  3801. }
  3802. } else if (light->directional_shadow_mode == VS::LIGHT_DIRECTIONAL_SHADOW_PARALLEL_2_SPLITS) {
  3803. height /= 2;
  3804. if (p_pass == 0) {
  3805. } else {
  3806. y += height;
  3807. }
  3808. }
  3809. float bias_mult = Math::lerp(1.0f, light_instance->shadow_transform[p_pass].bias_scale, light->param[VS::LIGHT_PARAM_SHADOW_BIAS_SPLIT_SCALE]);
  3810. zfar = light->param[VS::LIGHT_PARAM_RANGE];
  3811. bias = light->param[VS::LIGHT_PARAM_SHADOW_BIAS] * bias_mult;
  3812. normal_bias = light->param[VS::LIGHT_PARAM_SHADOW_NORMAL_BIAS] * bias_mult;
  3813. fbo = directional_shadow.fbo;
  3814. } else {
  3815. //set from shadow atlas
  3816. ShadowAtlas *shadow_atlas = shadow_atlas_owner.getornull(p_shadow_atlas);
  3817. ERR_FAIL_COND(!shadow_atlas);
  3818. ERR_FAIL_COND(!shadow_atlas->shadow_owners.has(p_light));
  3819. fbo = shadow_atlas->fbo;
  3820. uint32_t key = shadow_atlas->shadow_owners[p_light];
  3821. uint32_t quadrant = (key >> ShadowAtlas::QUADRANT_SHIFT) & 0x3;
  3822. uint32_t shadow = key & ShadowAtlas::SHADOW_INDEX_MASK;
  3823. ERR_FAIL_INDEX((int)shadow, shadow_atlas->quadrants[quadrant].shadows.size());
  3824. uint32_t quadrant_size = shadow_atlas->size >> 1;
  3825. x = (quadrant & 1) * quadrant_size;
  3826. y = (quadrant >> 1) * quadrant_size;
  3827. uint32_t shadow_size = (quadrant_size / shadow_atlas->quadrants[quadrant].subdivision);
  3828. x += (shadow % shadow_atlas->quadrants[quadrant].subdivision) * shadow_size;
  3829. y += (shadow / shadow_atlas->quadrants[quadrant].subdivision) * shadow_size;
  3830. width = shadow_size;
  3831. height = shadow_size;
  3832. if (light->type == VS::LIGHT_OMNI) {
  3833. if (light->omni_shadow_mode == VS::LIGHT_OMNI_SHADOW_CUBE) {
  3834. int cubemap_index = shadow_cubemaps.size() - 1;
  3835. for (int i = shadow_cubemaps.size() - 1; i >= 0; i--) {
  3836. //find appropriate cubemap to render to
  3837. if (shadow_cubemaps[i].size > shadow_size) {
  3838. break;
  3839. }
  3840. cubemap_index = i;
  3841. }
  3842. fbo = shadow_cubemaps[cubemap_index].fbo_id[p_pass];
  3843. light_projection = light_instance->shadow_transform[0].camera;
  3844. light_transform = light_instance->shadow_transform[0].transform;
  3845. custom_vp_size = shadow_cubemaps[cubemap_index].size;
  3846. zfar = light->param[VS::LIGHT_PARAM_RANGE];
  3847. current_cubemap = cubemap_index;
  3848. } else {
  3849. light_projection = light_instance->shadow_transform[0].camera;
  3850. light_transform = light_instance->shadow_transform[0].transform;
  3851. if (light->omni_shadow_detail == VS::LIGHT_OMNI_SHADOW_DETAIL_HORIZONTAL) {
  3852. height /= 2;
  3853. y += p_pass * height;
  3854. } else {
  3855. width /= 2;
  3856. x += p_pass * width;
  3857. }
  3858. dp_direction = p_pass == 0 ? 1.0 : -1.0;
  3859. flip_facing = (p_pass == 1);
  3860. zfar = light->param[VS::LIGHT_PARAM_RANGE];
  3861. bias = light->param[VS::LIGHT_PARAM_SHADOW_BIAS];
  3862. state.scene_shader.set_conditional(SceneShaderGLES3::RENDER_DEPTH_DUAL_PARABOLOID, true);
  3863. }
  3864. } else if (light->type == VS::LIGHT_SPOT) {
  3865. light_projection = light_instance->shadow_transform[0].camera;
  3866. light_transform = light_instance->shadow_transform[0].transform;
  3867. dp_direction = 1.0;
  3868. flip_facing = false;
  3869. zfar = light->param[VS::LIGHT_PARAM_RANGE];
  3870. bias = light->param[VS::LIGHT_PARAM_SHADOW_BIAS];
  3871. normal_bias = light->param[VS::LIGHT_PARAM_SHADOW_NORMAL_BIAS];
  3872. }
  3873. }
  3874. render_list.clear();
  3875. _fill_render_list(p_cull_result, p_cull_count, true, true);
  3876. render_list.sort_by_depth(false); //shadow is front to back for performance
  3877. glDisable(GL_BLEND);
  3878. glDisable(GL_DITHER);
  3879. glEnable(GL_DEPTH_TEST);
  3880. glBindFramebuffer(GL_FRAMEBUFFER, fbo);
  3881. glDepthMask(true);
  3882. glColorMask(0, 0, 0, 0);
  3883. if (custom_vp_size) {
  3884. glViewport(0, 0, custom_vp_size, custom_vp_size);
  3885. glScissor(0, 0, custom_vp_size, custom_vp_size);
  3886. } else {
  3887. glViewport(x, y, width, height);
  3888. glScissor(x, y, width, height);
  3889. }
  3890. glEnable(GL_SCISSOR_TEST);
  3891. glClearDepth(1.0f);
  3892. glClear(GL_DEPTH_BUFFER_BIT);
  3893. glDisable(GL_SCISSOR_TEST);
  3894. state.ubo_data.z_offset = bias;
  3895. state.ubo_data.z_slope_scale = normal_bias;
  3896. state.ubo_data.shadow_dual_paraboloid_render_side = dp_direction;
  3897. state.ubo_data.shadow_dual_paraboloid_render_zfar = zfar;
  3898. state.ubo_data.opaque_prepass_threshold = 0.1;
  3899. if (storage->config.async_compilation_enabled) {
  3900. // Avoid GL UB message id 131222 caused by shadow samplers not properly set up in the ubershader
  3901. glActiveTexture(GL_TEXTURE0 + storage->config.max_texture_image_units - 6);
  3902. glBindTexture(GL_TEXTURE_2D, storage->resources.depth_tex);
  3903. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE, GL_COMPARE_REF_TO_TEXTURE);
  3904. }
  3905. _setup_environment(nullptr, light_projection, light_transform);
  3906. state.scene_shader.set_conditional(SceneShaderGLES3::RENDER_DEPTH, true);
  3907. if (light->reverse_cull) {
  3908. flip_facing = !flip_facing;
  3909. }
  3910. _render_list(render_list.elements, render_list.element_count, light_transform, light_projection, nullptr, flip_facing, false, true, false, false);
  3911. state.scene_shader.set_conditional(SceneShaderGLES3::RENDER_DEPTH, false);
  3912. state.scene_shader.set_conditional(SceneShaderGLES3::RENDER_DEPTH_DUAL_PARABOLOID, false);
  3913. if (light->type == VS::LIGHT_OMNI && light->omni_shadow_mode == VS::LIGHT_OMNI_SHADOW_CUBE && p_pass == 5) {
  3914. //convert the chosen cubemap to dual paraboloid!
  3915. ShadowAtlas *shadow_atlas = shadow_atlas_owner.getornull(p_shadow_atlas);
  3916. glBindFramebuffer(GL_FRAMEBUFFER, shadow_atlas->fbo);
  3917. state.cube_to_dp_shader.bind();
  3918. glActiveTexture(GL_TEXTURE0);
  3919. glBindTexture(GL_TEXTURE_CUBE_MAP, shadow_cubemaps[current_cubemap].cubemap);
  3920. glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_COMPARE_MODE, GL_NONE);
  3921. glDisable(GL_CULL_FACE);
  3922. for (int i = 0; i < 2; i++) {
  3923. state.cube_to_dp_shader.set_uniform(CubeToDpShaderGLES3::Z_FLIP, i == 1);
  3924. state.cube_to_dp_shader.set_uniform(CubeToDpShaderGLES3::Z_NEAR, light_projection.get_z_near());
  3925. state.cube_to_dp_shader.set_uniform(CubeToDpShaderGLES3::Z_FAR, light_projection.get_z_far());
  3926. state.cube_to_dp_shader.set_uniform(CubeToDpShaderGLES3::BIAS, light->param[VS::LIGHT_PARAM_SHADOW_BIAS]);
  3927. uint32_t local_width = width, local_height = height;
  3928. uint32_t local_x = x, local_y = y;
  3929. if (light->omni_shadow_detail == VS::LIGHT_OMNI_SHADOW_DETAIL_HORIZONTAL) {
  3930. local_height /= 2;
  3931. local_y += i * local_height;
  3932. } else {
  3933. local_width /= 2;
  3934. local_x += i * local_width;
  3935. }
  3936. glViewport(local_x, local_y, local_width, local_height);
  3937. glScissor(local_x, local_y, local_width, local_height);
  3938. glEnable(GL_SCISSOR_TEST);
  3939. glClearDepth(1.0f);
  3940. glClear(GL_DEPTH_BUFFER_BIT);
  3941. glDisable(GL_SCISSOR_TEST);
  3942. //glDisable(GL_DEPTH_TEST);
  3943. glDisable(GL_BLEND);
  3944. _copy_screen();
  3945. }
  3946. }
  3947. glColorMask(1, 1, 1, 1);
  3948. }
  3949. void RasterizerSceneGLES3::set_scene_pass(uint64_t p_pass) {
  3950. scene_pass = p_pass;
  3951. }
  3952. bool RasterizerSceneGLES3::free(RID p_rid) {
  3953. if (light_instance_owner.owns(p_rid)) {
  3954. LightInstance *light_instance = light_instance_owner.getptr(p_rid);
  3955. // Make sure first_directional_light is invalidated
  3956. if (p_rid == first_directional_light) {
  3957. first_directional_light = RID();
  3958. }
  3959. //remove from shadow atlases..
  3960. for (Set<RID>::Element *E = light_instance->shadow_atlases.front(); E; E = E->next()) {
  3961. ShadowAtlas *shadow_atlas = shadow_atlas_owner.get(E->get());
  3962. ERR_CONTINUE(!shadow_atlas->shadow_owners.has(p_rid));
  3963. uint32_t key = shadow_atlas->shadow_owners[p_rid];
  3964. uint32_t q = (key >> ShadowAtlas::QUADRANT_SHIFT) & 0x3;
  3965. uint32_t s = key & ShadowAtlas::SHADOW_INDEX_MASK;
  3966. shadow_atlas->quadrants[q].shadows.write[s].owner = RID();
  3967. shadow_atlas->shadow_owners.erase(p_rid);
  3968. }
  3969. light_instance_owner.free(p_rid);
  3970. memdelete(light_instance);
  3971. } else if (shadow_atlas_owner.owns(p_rid)) {
  3972. ShadowAtlas *shadow_atlas = shadow_atlas_owner.get(p_rid);
  3973. shadow_atlas_set_size(p_rid, 0);
  3974. shadow_atlas_owner.free(p_rid);
  3975. memdelete(shadow_atlas);
  3976. } else if (reflection_atlas_owner.owns(p_rid)) {
  3977. ReflectionAtlas *reflection_atlas = reflection_atlas_owner.get(p_rid);
  3978. reflection_atlas_set_size(p_rid, 0);
  3979. reflection_atlas_owner.free(p_rid);
  3980. memdelete(reflection_atlas);
  3981. } else if (reflection_probe_instance_owner.owns(p_rid)) {
  3982. ReflectionProbeInstance *reflection_instance = reflection_probe_instance_owner.get(p_rid);
  3983. reflection_probe_release_atlas_index(p_rid);
  3984. reflection_probe_instance_owner.free(p_rid);
  3985. memdelete(reflection_instance);
  3986. } else if (environment_owner.owns(p_rid)) {
  3987. Environment *environment = environment_owner.get(p_rid);
  3988. environment_owner.free(p_rid);
  3989. memdelete(environment);
  3990. } else if (gi_probe_instance_owner.owns(p_rid)) {
  3991. GIProbeInstance *gi_probe_instance = gi_probe_instance_owner.get(p_rid);
  3992. gi_probe_instance_owner.free(p_rid);
  3993. memdelete(gi_probe_instance);
  3994. } else {
  3995. return false;
  3996. }
  3997. return true;
  3998. }
  3999. void RasterizerSceneGLES3::set_debug_draw_mode(VS::ViewportDebugDraw p_debug_draw) {
  4000. state.debug_draw = p_debug_draw;
  4001. }
  4002. void RasterizerSceneGLES3::initialize() {
  4003. render_pass = 0;
  4004. state.scene_shader.init();
  4005. {
  4006. //default material and shader
  4007. default_shader = RID_PRIME(storage->shader_create());
  4008. storage->shader_set_code(default_shader, "shader_type spatial;\n");
  4009. default_material = RID_PRIME(storage->material_create());
  4010. storage->material_set_shader(default_material, default_shader);
  4011. default_shader_twosided = RID_PRIME(storage->shader_create());
  4012. default_material_twosided = RID_PRIME(storage->material_create());
  4013. storage->shader_set_code(default_shader_twosided, "shader_type spatial; render_mode cull_disabled;\n");
  4014. storage->material_set_shader(default_material_twosided, default_shader_twosided);
  4015. //default for shaders using world coordinates (typical for triplanar)
  4016. default_worldcoord_shader = RID_PRIME(storage->shader_create());
  4017. storage->shader_set_code(default_worldcoord_shader, "shader_type spatial; render_mode world_vertex_coords;\n");
  4018. default_worldcoord_material = RID_PRIME(storage->material_create());
  4019. storage->material_set_shader(default_worldcoord_material, default_worldcoord_shader);
  4020. default_worldcoord_shader_twosided = RID_PRIME(storage->shader_create());
  4021. default_worldcoord_material_twosided = RID_PRIME(storage->material_create());
  4022. storage->shader_set_code(default_worldcoord_shader_twosided, "shader_type spatial; render_mode cull_disabled,world_vertex_coords;\n");
  4023. storage->material_set_shader(default_worldcoord_material_twosided, default_worldcoord_shader_twosided);
  4024. }
  4025. {
  4026. //default material and shader
  4027. default_overdraw_shader = RID_PRIME(storage->shader_create());
  4028. // Use relatively low opacity so that more "layers" of overlapping objects can be distinguished.
  4029. storage->shader_set_code(default_overdraw_shader, "shader_type spatial;\nrender_mode blend_add,unshaded;\n void fragment() { ALBEDO=vec3(0.4,0.8,0.8); ALPHA=0.1; }");
  4030. default_overdraw_material = RID_PRIME(storage->material_create());
  4031. storage->material_set_shader(default_overdraw_material, default_overdraw_shader);
  4032. }
  4033. glGenBuffers(1, &state.scene_ubo);
  4034. glBindBuffer(GL_UNIFORM_BUFFER, state.scene_ubo);
  4035. glBufferData(GL_UNIFORM_BUFFER, sizeof(State::SceneDataUBO), &state.scene_ubo, GL_DYNAMIC_DRAW);
  4036. glBindBuffer(GL_UNIFORM_BUFFER, 0);
  4037. glGenBuffers(1, &state.env_radiance_ubo);
  4038. glBindBuffer(GL_UNIFORM_BUFFER, state.env_radiance_ubo);
  4039. glBufferData(GL_UNIFORM_BUFFER, sizeof(State::EnvironmentRadianceUBO), &state.env_radiance_ubo, GL_DYNAMIC_DRAW);
  4040. glBindBuffer(GL_UNIFORM_BUFFER, 0);
  4041. render_list.max_elements = GLOBAL_DEF_RST("rendering/limits/rendering/max_renderable_elements", (int)RenderList::DEFAULT_MAX_ELEMENTS);
  4042. ProjectSettings::get_singleton()->set_custom_property_info("rendering/limits/rendering/max_renderable_elements", PropertyInfo(Variant::INT, "rendering/limits/rendering/max_renderable_elements", PROPERTY_HINT_RANGE, "1024,65536,1"));
  4043. render_list.max_lights = GLOBAL_DEF("rendering/limits/rendering/max_renderable_lights", (int)RenderList::DEFAULT_MAX_LIGHTS);
  4044. ProjectSettings::get_singleton()->set_custom_property_info("rendering/limits/rendering/max_renderable_lights", PropertyInfo(Variant::INT, "rendering/limits/rendering/max_renderable_lights", PROPERTY_HINT_RANGE, "16,4096,1"));
  4045. render_list.max_reflections = GLOBAL_DEF("rendering/limits/rendering/max_renderable_reflections", (int)RenderList::DEFAULT_MAX_REFLECTIONS);
  4046. ProjectSettings::get_singleton()->set_custom_property_info("rendering/limits/rendering/max_renderable_reflections", PropertyInfo(Variant::INT, "rendering/limits/rendering/max_renderable_reflections", PROPERTY_HINT_RANGE, "8,1024,1"));
  4047. render_list.max_lights_per_object = GLOBAL_DEF_RST("rendering/limits/rendering/max_lights_per_object", (int)RenderList::DEFAULT_MAX_LIGHTS_PER_OBJECT);
  4048. ProjectSettings::get_singleton()->set_custom_property_info("rendering/limits/rendering/max_lights_per_object", PropertyInfo(Variant::INT, "rendering/limits/rendering/max_lights_per_object", PROPERTY_HINT_RANGE, "8,1024,1"));
  4049. {
  4050. //quad buffers
  4051. glGenBuffers(1, &state.sky_verts);
  4052. glBindBuffer(GL_ARRAY_BUFFER, state.sky_verts);
  4053. glBufferData(GL_ARRAY_BUFFER, sizeof(Vector3) * 8, nullptr, GL_DYNAMIC_DRAW);
  4054. glBindBuffer(GL_ARRAY_BUFFER, 0); //unbind
  4055. glGenVertexArrays(1, &state.sky_array);
  4056. glBindVertexArray(state.sky_array);
  4057. glBindBuffer(GL_ARRAY_BUFFER, state.sky_verts);
  4058. glVertexAttribPointer(VS::ARRAY_VERTEX, 3, GL_FLOAT, GL_FALSE, sizeof(Vector3) * 2, nullptr);
  4059. glEnableVertexAttribArray(VS::ARRAY_VERTEX);
  4060. glVertexAttribPointer(VS::ARRAY_TEX_UV, 3, GL_FLOAT, GL_FALSE, sizeof(Vector3) * 2, CAST_INT_TO_UCHAR_PTR(sizeof(Vector3)));
  4061. glEnableVertexAttribArray(VS::ARRAY_TEX_UV);
  4062. glBindVertexArray(0);
  4063. glBindBuffer(GL_ARRAY_BUFFER, 0); //unbind
  4064. }
  4065. render_list.init();
  4066. state.cube_to_dp_shader.init();
  4067. shadow_atlas_realloc_tolerance_msec = 500;
  4068. int max_shadow_cubemap_sampler_size = MIN(int(GLOBAL_GET("rendering/quality/shadow_atlas/cubemap_size")), storage->config.max_cubemap_texture_size);
  4069. int cube_size = max_shadow_cubemap_sampler_size;
  4070. glActiveTexture(GL_TEXTURE0);
  4071. while (cube_size >= 32) {
  4072. ShadowCubeMap cube;
  4073. cube.size = cube_size;
  4074. glGenTextures(1, &cube.cubemap);
  4075. glBindTexture(GL_TEXTURE_CUBE_MAP, cube.cubemap);
  4076. //gen cubemap first
  4077. for (int i = 0; i < 6; i++) {
  4078. glTexImage2D(_cube_side_enum[i], 0, GL_DEPTH_COMPONENT24, cube.size, cube.size, 0, GL_DEPTH_COMPONENT, GL_UNSIGNED_INT, nullptr);
  4079. }
  4080. glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  4081. glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  4082. // Remove artifact on the edges of the shadowmap
  4083. glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  4084. glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  4085. glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
  4086. //gen renderbuffers second, because it needs a complete cubemap
  4087. for (int i = 0; i < 6; i++) {
  4088. glGenFramebuffers(1, &cube.fbo_id[i]);
  4089. glBindFramebuffer(GL_FRAMEBUFFER, cube.fbo_id[i]);
  4090. glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, _cube_side_enum[i], cube.cubemap, 0);
  4091. GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
  4092. ERR_CONTINUE(status != GL_FRAMEBUFFER_COMPLETE);
  4093. }
  4094. shadow_cubemaps.push_back(cube);
  4095. cube_size >>= 1;
  4096. }
  4097. directional_shadow_create();
  4098. {
  4099. //spot and omni ubos
  4100. int max_ubo_size;
  4101. glGetIntegerv(GL_MAX_UNIFORM_BLOCK_SIZE, &max_ubo_size);
  4102. const int ubo_light_size = 160;
  4103. state.ubo_light_size = ubo_light_size;
  4104. state.max_ubo_lights = MIN(render_list.max_lights, max_ubo_size / ubo_light_size);
  4105. state.spot_array_tmp = (uint8_t *)memalloc(ubo_light_size * state.max_ubo_lights);
  4106. state.omni_array_tmp = (uint8_t *)memalloc(ubo_light_size * state.max_ubo_lights);
  4107. glGenBuffers(1, &state.spot_array_ubo);
  4108. glBindBuffer(GL_UNIFORM_BUFFER, state.spot_array_ubo);
  4109. glBufferData(GL_UNIFORM_BUFFER, ubo_light_size * state.max_ubo_lights, nullptr, GL_DYNAMIC_DRAW);
  4110. glBindBuffer(GL_UNIFORM_BUFFER, 0);
  4111. glGenBuffers(1, &state.omni_array_ubo);
  4112. glBindBuffer(GL_UNIFORM_BUFFER, state.omni_array_ubo);
  4113. glBufferData(GL_UNIFORM_BUFFER, ubo_light_size * state.max_ubo_lights, nullptr, GL_DYNAMIC_DRAW);
  4114. glBindBuffer(GL_UNIFORM_BUFFER, 0);
  4115. glGenBuffers(1, &state.directional_ubo);
  4116. glBindBuffer(GL_UNIFORM_BUFFER, state.directional_ubo);
  4117. glBufferData(GL_UNIFORM_BUFFER, sizeof(LightDataUBO), nullptr, GL_DYNAMIC_DRAW);
  4118. glBindBuffer(GL_UNIFORM_BUFFER, 0);
  4119. state.max_forward_lights_per_object = MIN(state.max_ubo_lights, render_list.max_lights_per_object);
  4120. state.scene_shader.add_custom_define("#define MAX_LIGHT_DATA_STRUCTS " + itos(state.max_ubo_lights) + "\n");
  4121. state.scene_shader.add_custom_define("#define MAX_FORWARD_LIGHTS " + itos(state.max_forward_lights_per_object) + "\n");
  4122. state.max_ubo_reflections = MIN(render_list.max_reflections, max_ubo_size / (int)sizeof(ReflectionProbeDataUBO));
  4123. state.reflection_array_tmp = (uint8_t *)memalloc(sizeof(ReflectionProbeDataUBO) * state.max_ubo_reflections);
  4124. glGenBuffers(1, &state.reflection_array_ubo);
  4125. glBindBuffer(GL_UNIFORM_BUFFER, state.reflection_array_ubo);
  4126. glBufferData(GL_UNIFORM_BUFFER, sizeof(ReflectionProbeDataUBO) * state.max_ubo_reflections, nullptr, GL_DYNAMIC_DRAW);
  4127. glBindBuffer(GL_UNIFORM_BUFFER, 0);
  4128. state.scene_shader.add_custom_define("#define MAX_REFLECTION_DATA_STRUCTS " + itos(state.max_ubo_reflections) + "\n");
  4129. state.max_skeleton_bones = MIN(2048, max_ubo_size / (12 * sizeof(float)));
  4130. state.scene_shader.add_custom_define("#define MAX_SKELETON_BONES " + itos(state.max_skeleton_bones) + "\n");
  4131. }
  4132. shadow_filter_mode = SHADOW_FILTER_NEAREST;
  4133. { //reflection cubemaps
  4134. int max_reflection_cubemap_sampler_size = 512;
  4135. int rcube_size = max_reflection_cubemap_sampler_size;
  4136. glActiveTexture(GL_TEXTURE0);
  4137. bool use_float = true;
  4138. GLenum internal_format = use_float ? GL_RGBA16F : GL_RGB10_A2;
  4139. GLenum format = GL_RGBA;
  4140. GLenum type = use_float ? GL_HALF_FLOAT : GL_UNSIGNED_INT_2_10_10_10_REV;
  4141. while (rcube_size >= 32) {
  4142. ReflectionCubeMap cube;
  4143. cube.size = rcube_size;
  4144. glGenTextures(1, &cube.depth);
  4145. glBindTexture(GL_TEXTURE_2D, cube.depth);
  4146. glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT24, cube.size, cube.size, 0, GL_DEPTH_COMPONENT, GL_UNSIGNED_INT, nullptr);
  4147. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  4148. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  4149. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  4150. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  4151. glGenTextures(1, &cube.cubemap);
  4152. glBindTexture(GL_TEXTURE_CUBE_MAP, cube.cubemap);
  4153. //gen cubemap first
  4154. for (int i = 0; i < 6; i++) {
  4155. glTexImage2D(_cube_side_enum[i], 0, internal_format, cube.size, cube.size, 0, format, type, nullptr);
  4156. }
  4157. glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  4158. glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  4159. // Remove artifact on the edges of the reflectionmap
  4160. glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  4161. glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  4162. glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
  4163. //gen renderbuffers second, because it needs a complete cubemap
  4164. for (int i = 0; i < 6; i++) {
  4165. glGenFramebuffers(1, &cube.fbo_id[i]);
  4166. glBindFramebuffer(GL_FRAMEBUFFER, cube.fbo_id[i]);
  4167. glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, _cube_side_enum[i], cube.cubemap, 0);
  4168. glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, cube.depth, 0);
  4169. GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
  4170. ERR_CONTINUE(status != GL_FRAMEBUFFER_COMPLETE);
  4171. }
  4172. reflection_cubemaps.push_back(cube);
  4173. rcube_size >>= 1;
  4174. }
  4175. }
  4176. {
  4177. uint32_t immediate_buffer_size = GLOBAL_DEF("rendering/limits/buffers/immediate_buffer_size_kb", 2048);
  4178. ProjectSettings::get_singleton()->set_custom_property_info("rendering/limits/buffers/immediate_buffer_size_kb", PropertyInfo(Variant::INT, "rendering/limits/buffers/immediate_buffer_size_kb", PROPERTY_HINT_RANGE, "0,8192,1,or_greater"));
  4179. glGenBuffers(1, &state.immediate_buffer);
  4180. glBindBuffer(GL_ARRAY_BUFFER, state.immediate_buffer);
  4181. glBufferData(GL_ARRAY_BUFFER, immediate_buffer_size * 1024, nullptr, GL_DYNAMIC_DRAW);
  4182. glBindBuffer(GL_ARRAY_BUFFER, 0);
  4183. glGenVertexArrays(1, &state.immediate_array);
  4184. }
  4185. #ifdef GLES_OVER_GL
  4186. //"desktop" opengl needs this.
  4187. glEnable(GL_PROGRAM_POINT_SIZE);
  4188. #endif
  4189. state.resolve_shader.init();
  4190. state.ssr_shader.init();
  4191. state.effect_blur_shader.init();
  4192. state.sss_shader.init();
  4193. state.ssao_minify_shader.init();
  4194. state.ssao_shader.init();
  4195. state.ssao_blur_shader.init();
  4196. state.exposure_shader.init();
  4197. state.tonemap_shader.init();
  4198. {
  4199. GLOBAL_DEF("rendering/quality/subsurface_scattering/quality", 1);
  4200. ProjectSettings::get_singleton()->set_custom_property_info("rendering/quality/subsurface_scattering/quality", PropertyInfo(Variant::INT, "rendering/quality/subsurface_scattering/quality", PROPERTY_HINT_ENUM, "Low,Medium,High"));
  4201. GLOBAL_DEF("rendering/quality/subsurface_scattering/scale", 1.0);
  4202. ProjectSettings::get_singleton()->set_custom_property_info("rendering/quality/subsurface_scattering/scale", PropertyInfo(Variant::INT, "rendering/quality/subsurface_scattering/scale", PROPERTY_HINT_RANGE, "0.01,8,0.01"));
  4203. GLOBAL_DEF("rendering/quality/subsurface_scattering/follow_surface", false);
  4204. GLOBAL_DEF("rendering/quality/subsurface_scattering/weight_samples", true);
  4205. GLOBAL_DEF("rendering/quality/voxel_cone_tracing/high_quality", false);
  4206. }
  4207. exposure_shrink_size = 243;
  4208. int max_exposure_shrink_size = exposure_shrink_size;
  4209. while (max_exposure_shrink_size > 0) {
  4210. RasterizerStorageGLES3::RenderTarget::Exposure e;
  4211. glGenFramebuffers(1, &e.fbo);
  4212. glBindFramebuffer(GL_FRAMEBUFFER, e.fbo);
  4213. glGenTextures(1, &e.color);
  4214. glBindTexture(GL_TEXTURE_2D, e.color);
  4215. if (storage->config.framebuffer_float_supported) {
  4216. glTexImage2D(GL_TEXTURE_2D, 0, GL_R32F, max_exposure_shrink_size, max_exposure_shrink_size, 0, GL_RED, GL_FLOAT, nullptr);
  4217. } else if (storage->config.framebuffer_half_float_supported) {
  4218. glTexImage2D(GL_TEXTURE_2D, 0, GL_R16F, max_exposure_shrink_size, max_exposure_shrink_size, 0, GL_RED, GL_HALF_FLOAT, nullptr);
  4219. } else {
  4220. glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB10_A2, max_exposure_shrink_size, max_exposure_shrink_size, 0, GL_RED, GL_UNSIGNED_INT_2_10_10_10_REV, nullptr);
  4221. }
  4222. glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, e.color, 0);
  4223. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  4224. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  4225. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  4226. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  4227. exposure_shrink.push_back(e);
  4228. max_exposure_shrink_size /= 3;
  4229. GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
  4230. ERR_CONTINUE(status != GL_FRAMEBUFFER_COMPLETE);
  4231. }
  4232. state.debug_draw = VS::VIEWPORT_DEBUG_DRAW_DISABLED;
  4233. glFrontFace(GL_CW);
  4234. if (storage->config.async_compilation_enabled) {
  4235. state.scene_shader.init_async_compilation();
  4236. }
  4237. }
  4238. void RasterizerSceneGLES3::iteration() {
  4239. shadow_filter_mode = ShadowFilterMode(int(GLOBAL_GET("rendering/quality/shadows/filter_mode")));
  4240. const int directional_shadow_size_new = next_power_of_2(int(GLOBAL_GET("rendering/quality/directional_shadow/size")));
  4241. if (directional_shadow_size != directional_shadow_size_new) {
  4242. directional_shadow_size = directional_shadow_size_new;
  4243. directional_shadow_create();
  4244. }
  4245. subsurface_scatter_follow_surface = GLOBAL_GET("rendering/quality/subsurface_scattering/follow_surface");
  4246. subsurface_scatter_weight_samples = GLOBAL_GET("rendering/quality/subsurface_scattering/weight_samples");
  4247. subsurface_scatter_quality = SubSurfaceScatterQuality(int(GLOBAL_GET("rendering/quality/subsurface_scattering/quality")));
  4248. subsurface_scatter_size = GLOBAL_GET("rendering/quality/subsurface_scattering/scale");
  4249. storage->config.use_lightmap_filter_bicubic = GLOBAL_GET("rendering/quality/lightmapping/use_bicubic_sampling");
  4250. state.scene_shader.set_conditional(SceneShaderGLES3::USE_LIGHTMAP_FILTER_BICUBIC, storage->config.use_lightmap_filter_bicubic);
  4251. state.scene_shader.set_conditional(SceneShaderGLES3::VCT_QUALITY_HIGH, GLOBAL_GET("rendering/quality/voxel_cone_tracing/high_quality"));
  4252. }
  4253. void RasterizerSceneGLES3::finalize() {
  4254. }
  4255. RasterizerSceneGLES3::RasterizerSceneGLES3() {
  4256. directional_shadow_size = next_power_of_2(int(GLOBAL_GET("rendering/quality/directional_shadow/size")));
  4257. }
  4258. RasterizerSceneGLES3::~RasterizerSceneGLES3() {
  4259. storage->free(default_material);
  4260. default_material = RID();
  4261. storage->free(default_material_twosided);
  4262. default_material_twosided = RID();
  4263. storage->free(default_shader);
  4264. default_shader = RID();
  4265. storage->free(default_shader_twosided);
  4266. default_shader_twosided = RID();
  4267. storage->free(default_worldcoord_material);
  4268. default_worldcoord_material = RID();
  4269. storage->free(default_worldcoord_material_twosided);
  4270. default_worldcoord_material_twosided = RID();
  4271. storage->free(default_worldcoord_shader);
  4272. default_worldcoord_shader = RID();
  4273. storage->free(default_worldcoord_shader_twosided);
  4274. default_worldcoord_shader_twosided = RID();
  4275. storage->free(default_overdraw_material);
  4276. default_overdraw_material = RID();
  4277. storage->free(default_overdraw_shader);
  4278. default_overdraw_shader = RID();
  4279. memfree(state.spot_array_tmp);
  4280. memfree(state.omni_array_tmp);
  4281. memfree(state.reflection_array_tmp);
  4282. }