rasterizer_scene_gles3.cpp 192 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501
  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 "drivers/gles3/effects/copy_effects.h"
  32. #include "drivers/gles3/effects/feed_effects.h"
  33. #include "drivers/gles3/storage/material_storage.h"
  34. #include "rasterizer_gles3.h"
  35. #include "storage/config.h"
  36. #include "storage/mesh_storage.h"
  37. #include "storage/particles_storage.h"
  38. #include "storage/texture_storage.h"
  39. #include "core/config/project_settings.h"
  40. #include "core/templates/sort_array.h"
  41. #include "servers/camera/camera_feed.h"
  42. #include "servers/camera_server.h"
  43. #include "servers/rendering/rendering_server_default.h"
  44. #include "servers/rendering/rendering_server_globals.h"
  45. #ifdef GLES3_ENABLED
  46. RasterizerSceneGLES3 *RasterizerSceneGLES3::singleton = nullptr;
  47. RenderGeometryInstance *RasterizerSceneGLES3::geometry_instance_create(RID p_base) {
  48. RS::InstanceType type = RSG::utilities->get_base_type(p_base);
  49. ERR_FAIL_COND_V(!((1 << type) & RS::INSTANCE_GEOMETRY_MASK), nullptr);
  50. GeometryInstanceGLES3 *ginstance = geometry_instance_alloc.alloc();
  51. ginstance->data = memnew(GeometryInstanceGLES3::Data);
  52. ginstance->data->base = p_base;
  53. ginstance->data->base_type = type;
  54. ginstance->data->dependency_tracker.userdata = ginstance;
  55. ginstance->data->dependency_tracker.changed_callback = _geometry_instance_dependency_changed;
  56. ginstance->data->dependency_tracker.deleted_callback = _geometry_instance_dependency_deleted;
  57. ginstance->_mark_dirty();
  58. return ginstance;
  59. }
  60. uint32_t RasterizerSceneGLES3::geometry_instance_get_pair_mask() {
  61. return ((1 << RS::INSTANCE_LIGHT) | (1 << RS::INSTANCE_REFLECTION_PROBE));
  62. }
  63. void RasterizerSceneGLES3::GeometryInstanceGLES3::pair_light_instances(const RID *p_light_instances, uint32_t p_light_instance_count) {
  64. GLES3::Config *config = GLES3::Config::get_singleton();
  65. paired_omni_light_count = 0;
  66. paired_spot_light_count = 0;
  67. paired_omni_lights.clear();
  68. paired_spot_lights.clear();
  69. for (uint32_t i = 0; i < p_light_instance_count; i++) {
  70. RS::LightType type = GLES3::LightStorage::get_singleton()->light_instance_get_type(p_light_instances[i]);
  71. switch (type) {
  72. case RS::LIGHT_OMNI: {
  73. if (paired_omni_light_count < (uint32_t)config->max_lights_per_object) {
  74. paired_omni_lights.push_back(p_light_instances[i]);
  75. paired_omni_light_count++;
  76. }
  77. } break;
  78. case RS::LIGHT_SPOT: {
  79. if (paired_spot_light_count < (uint32_t)config->max_lights_per_object) {
  80. paired_spot_lights.push_back(p_light_instances[i]);
  81. paired_spot_light_count++;
  82. }
  83. } break;
  84. default:
  85. break;
  86. }
  87. }
  88. }
  89. void RasterizerSceneGLES3::GeometryInstanceGLES3::pair_reflection_probe_instances(const RID *p_reflection_probe_instances, uint32_t p_reflection_probe_instance_count) {
  90. paired_reflection_probes.clear();
  91. for (uint32_t i = 0; i < p_reflection_probe_instance_count; i++) {
  92. paired_reflection_probes.push_back(p_reflection_probe_instances[i]);
  93. }
  94. }
  95. void RasterizerSceneGLES3::geometry_instance_free(RenderGeometryInstance *p_geometry_instance) {
  96. GeometryInstanceGLES3 *ginstance = static_cast<GeometryInstanceGLES3 *>(p_geometry_instance);
  97. ERR_FAIL_NULL(ginstance);
  98. GeometryInstanceSurface *surf = ginstance->surface_caches;
  99. while (surf) {
  100. GeometryInstanceSurface *next = surf->next;
  101. geometry_instance_surface_alloc.free(surf);
  102. surf = next;
  103. }
  104. memdelete(ginstance->data);
  105. geometry_instance_alloc.free(ginstance);
  106. }
  107. void RasterizerSceneGLES3::GeometryInstanceGLES3::_mark_dirty() {
  108. if (dirty_list_element.in_list()) {
  109. return;
  110. }
  111. //clear surface caches
  112. GeometryInstanceSurface *surf = surface_caches;
  113. while (surf) {
  114. GeometryInstanceSurface *next = surf->next;
  115. RasterizerSceneGLES3::get_singleton()->geometry_instance_surface_alloc.free(surf);
  116. surf = next;
  117. }
  118. surface_caches = nullptr;
  119. RasterizerSceneGLES3::get_singleton()->geometry_instance_dirty_list.add(&dirty_list_element);
  120. }
  121. void RasterizerSceneGLES3::GeometryInstanceGLES3::set_use_lightmap(RID p_lightmap_instance, const Rect2 &p_lightmap_uv_scale, int p_lightmap_slice_index) {
  122. lightmap_instance = p_lightmap_instance;
  123. lightmap_uv_scale = p_lightmap_uv_scale;
  124. lightmap_slice_index = p_lightmap_slice_index;
  125. _mark_dirty();
  126. }
  127. void RasterizerSceneGLES3::GeometryInstanceGLES3::set_lightmap_capture(const Color *p_sh9) {
  128. if (p_sh9) {
  129. if (lightmap_sh == nullptr) {
  130. lightmap_sh = memnew(GeometryInstanceLightmapSH);
  131. }
  132. memcpy(lightmap_sh->sh, p_sh9, sizeof(Color) * 9);
  133. } else {
  134. if (lightmap_sh != nullptr) {
  135. memdelete(lightmap_sh);
  136. lightmap_sh = nullptr;
  137. }
  138. }
  139. _mark_dirty();
  140. }
  141. void RasterizerSceneGLES3::_update_dirty_geometry_instances() {
  142. while (geometry_instance_dirty_list.first()) {
  143. _geometry_instance_update(geometry_instance_dirty_list.first()->self());
  144. }
  145. }
  146. void RasterizerSceneGLES3::_geometry_instance_dependency_changed(Dependency::DependencyChangedNotification p_notification, DependencyTracker *p_tracker) {
  147. switch (p_notification) {
  148. case Dependency::DEPENDENCY_CHANGED_MATERIAL:
  149. case Dependency::DEPENDENCY_CHANGED_MESH:
  150. case Dependency::DEPENDENCY_CHANGED_PARTICLES:
  151. case Dependency::DEPENDENCY_CHANGED_MULTIMESH:
  152. case Dependency::DEPENDENCY_CHANGED_SKELETON_DATA: {
  153. static_cast<RenderGeometryInstance *>(p_tracker->userdata)->_mark_dirty();
  154. static_cast<GeometryInstanceGLES3 *>(p_tracker->userdata)->data->dirty_dependencies = true;
  155. } break;
  156. case Dependency::DEPENDENCY_CHANGED_MULTIMESH_VISIBLE_INSTANCES: {
  157. GeometryInstanceGLES3 *ginstance = static_cast<GeometryInstanceGLES3 *>(p_tracker->userdata);
  158. if (ginstance->data->base_type == RS::INSTANCE_MULTIMESH) {
  159. ginstance->instance_count = GLES3::MeshStorage::get_singleton()->multimesh_get_instances_to_draw(ginstance->data->base);
  160. }
  161. } break;
  162. default: {
  163. //rest of notifications of no interest
  164. } break;
  165. }
  166. }
  167. void RasterizerSceneGLES3::_geometry_instance_dependency_deleted(const RID &p_dependency, DependencyTracker *p_tracker) {
  168. static_cast<RenderGeometryInstance *>(p_tracker->userdata)->_mark_dirty();
  169. static_cast<GeometryInstanceGLES3 *>(p_tracker->userdata)->data->dirty_dependencies = true;
  170. }
  171. void RasterizerSceneGLES3::_geometry_instance_add_surface_with_material(GeometryInstanceGLES3 *ginstance, uint32_t p_surface, GLES3::SceneMaterialData *p_material, uint32_t p_material_id, uint32_t p_shader_id, RID p_mesh) {
  172. GLES3::MeshStorage *mesh_storage = GLES3::MeshStorage::get_singleton();
  173. bool has_read_screen_alpha = p_material->shader_data->uses_screen_texture || p_material->shader_data->uses_depth_texture || p_material->shader_data->uses_normal_texture;
  174. bool has_base_alpha = ((p_material->shader_data->uses_alpha && !p_material->shader_data->uses_alpha_clip) || has_read_screen_alpha);
  175. bool has_blend_alpha = p_material->shader_data->uses_blend_alpha;
  176. bool has_alpha = has_base_alpha || has_blend_alpha;
  177. uint32_t flags = 0;
  178. if (p_material->shader_data->uses_screen_texture) {
  179. flags |= GeometryInstanceSurface::FLAG_USES_SCREEN_TEXTURE;
  180. }
  181. if (p_material->shader_data->uses_depth_texture) {
  182. flags |= GeometryInstanceSurface::FLAG_USES_DEPTH_TEXTURE;
  183. }
  184. if (p_material->shader_data->uses_normal_texture) {
  185. flags |= GeometryInstanceSurface::FLAG_USES_NORMAL_TEXTURE;
  186. }
  187. if (ginstance->data->cast_double_sided_shadows) {
  188. flags |= GeometryInstanceSurface::FLAG_USES_DOUBLE_SIDED_SHADOWS;
  189. }
  190. if (p_material->shader_data->stencil_enabled) {
  191. flags |= GeometryInstanceSurface::FLAG_USES_STENCIL;
  192. }
  193. if (has_alpha || has_read_screen_alpha || p_material->shader_data->depth_draw == GLES3::SceneShaderData::DEPTH_DRAW_DISABLED || p_material->shader_data->depth_test != GLES3::SceneShaderData::DEPTH_TEST_ENABLED) {
  194. //material is only meant for alpha pass
  195. flags |= GeometryInstanceSurface::FLAG_PASS_ALPHA;
  196. if (p_material->shader_data->uses_depth_prepass_alpha && !(p_material->shader_data->depth_draw == GLES3::SceneShaderData::DEPTH_DRAW_DISABLED || p_material->shader_data->depth_test != GLES3::SceneShaderData::DEPTH_TEST_ENABLED)) {
  197. flags |= GeometryInstanceSurface::FLAG_PASS_DEPTH;
  198. flags |= GeometryInstanceSurface::FLAG_PASS_SHADOW;
  199. }
  200. } else {
  201. flags |= GeometryInstanceSurface::FLAG_PASS_OPAQUE;
  202. flags |= GeometryInstanceSurface::FLAG_PASS_DEPTH;
  203. flags |= GeometryInstanceSurface::FLAG_PASS_SHADOW;
  204. }
  205. if (p_material->shader_data->stencil_enabled) {
  206. if (p_material->shader_data->stencil_flags & GLES3::SceneShaderData::STENCIL_FLAG_READ) {
  207. // Stencil materials which read from the stencil buffer must be in the alpha pass.
  208. // This is critical to preserve compatibility once we'll have the compositor.
  209. if (!(flags & GeometryInstanceSurface::FLAG_PASS_ALPHA)) {
  210. String shader_path = p_material->shader_data->path.is_empty() ? "" : "(" + p_material->shader_data->path + ")";
  211. ERR_PRINT_ED(vformat("Attempting to use a shader %s that reads stencil but is not in the alpha queue. Ensure the material uses alpha blending or has depth_draw disabled or depth_test disabled.", shader_path));
  212. }
  213. }
  214. }
  215. GLES3::SceneMaterialData *material_shadow = nullptr;
  216. void *surface_shadow = nullptr;
  217. if (!p_material->shader_data->uses_particle_trails && !p_material->shader_data->writes_modelview_or_projection && !p_material->shader_data->uses_vertex && !p_material->shader_data->uses_discard && !p_material->shader_data->uses_depth_prepass_alpha && !p_material->shader_data->uses_alpha_clip && !p_material->shader_data->uses_world_coordinates && !p_material->shader_data->wireframe) {
  218. flags |= GeometryInstanceSurface::FLAG_USES_SHARED_SHADOW_MATERIAL;
  219. material_shadow = static_cast<GLES3::SceneMaterialData *>(GLES3::MaterialStorage::get_singleton()->material_get_data(scene_globals.default_material, RS::SHADER_SPATIAL));
  220. RID shadow_mesh = mesh_storage->mesh_get_shadow_mesh(p_mesh);
  221. if (shadow_mesh.is_valid()) {
  222. surface_shadow = mesh_storage->mesh_get_surface(shadow_mesh, p_surface);
  223. }
  224. } else {
  225. material_shadow = p_material;
  226. }
  227. GeometryInstanceSurface *sdcache = geometry_instance_surface_alloc.alloc();
  228. sdcache->flags = flags;
  229. sdcache->shader = p_material->shader_data;
  230. sdcache->material = p_material;
  231. sdcache->surface = mesh_storage->mesh_get_surface(p_mesh, p_surface);
  232. sdcache->primitive = mesh_storage->mesh_surface_get_primitive(sdcache->surface);
  233. sdcache->surface_index = p_surface;
  234. if (ginstance->data->dirty_dependencies) {
  235. RSG::utilities->base_update_dependency(p_mesh, &ginstance->data->dependency_tracker);
  236. }
  237. //shadow
  238. sdcache->shader_shadow = material_shadow->shader_data;
  239. sdcache->material_shadow = material_shadow;
  240. sdcache->surface_shadow = surface_shadow ? surface_shadow : sdcache->surface;
  241. sdcache->owner = ginstance;
  242. sdcache->next = ginstance->surface_caches;
  243. ginstance->surface_caches = sdcache;
  244. //sortkey
  245. sdcache->sort.sort_key1 = 0;
  246. sdcache->sort.sort_key2 = 0;
  247. sdcache->sort.surface_index = p_surface;
  248. sdcache->sort.material_id_low = p_material_id & 0x0000FFFF;
  249. sdcache->sort.material_id_hi = p_material_id >> 16;
  250. sdcache->sort.shader_id = p_shader_id;
  251. sdcache->sort.geometry_id = p_mesh.get_local_index();
  252. sdcache->sort.priority = p_material->priority;
  253. GLES3::Mesh::Surface *s = reinterpret_cast<GLES3::Mesh::Surface *>(sdcache->surface);
  254. if (p_material->shader_data->uses_tangent && !(s->format & RS::ARRAY_FORMAT_TANGENT)) {
  255. String shader_path = p_material->shader_data->path.is_empty() ? "" : "(" + p_material->shader_data->path + ")";
  256. String mesh_path = mesh_storage->mesh_get_path(p_mesh).is_empty() ? "" : "(" + mesh_storage->mesh_get_path(p_mesh) + ")";
  257. WARN_PRINT_ED(vformat("Attempting to use a shader %s that requires tangents with a mesh %s that doesn't contain tangents. Ensure that meshes are imported with the 'ensure_tangents' option. If creating your own meshes, add an `ARRAY_TANGENT` array (when using ArrayMesh) or call `generate_tangents()` (when using SurfaceTool).", shader_path, mesh_path));
  258. }
  259. }
  260. void RasterizerSceneGLES3::_geometry_instance_add_surface_with_material_chain(GeometryInstanceGLES3 *ginstance, uint32_t p_surface, GLES3::SceneMaterialData *p_material_data, RID p_mat_src, RID p_mesh) {
  261. GLES3::SceneMaterialData *material_data = p_material_data;
  262. GLES3::MaterialStorage *material_storage = GLES3::MaterialStorage::get_singleton();
  263. _geometry_instance_add_surface_with_material(ginstance, p_surface, material_data, p_mat_src.get_local_index(), material_storage->material_get_shader_id(p_mat_src), p_mesh);
  264. while (material_data->next_pass.is_valid()) {
  265. RID next_pass = material_data->next_pass;
  266. material_data = static_cast<GLES3::SceneMaterialData *>(material_storage->material_get_data(next_pass, RS::SHADER_SPATIAL));
  267. if (!material_data || !material_data->shader_data->valid) {
  268. break;
  269. }
  270. if (ginstance->data->dirty_dependencies) {
  271. material_storage->material_update_dependency(next_pass, &ginstance->data->dependency_tracker);
  272. }
  273. _geometry_instance_add_surface_with_material(ginstance, p_surface, material_data, next_pass.get_local_index(), material_storage->material_get_shader_id(next_pass), p_mesh);
  274. }
  275. }
  276. void RasterizerSceneGLES3::_geometry_instance_add_surface(GeometryInstanceGLES3 *ginstance, uint32_t p_surface, RID p_material, RID p_mesh) {
  277. GLES3::MaterialStorage *material_storage = GLES3::MaterialStorage::get_singleton();
  278. RID m_src;
  279. m_src = ginstance->data->material_override.is_valid() ? ginstance->data->material_override : p_material;
  280. GLES3::SceneMaterialData *material_data = nullptr;
  281. if (m_src.is_valid()) {
  282. material_data = static_cast<GLES3::SceneMaterialData *>(material_storage->material_get_data(m_src, RS::SHADER_SPATIAL));
  283. if (!material_data || !material_data->shader_data->valid) {
  284. material_data = nullptr;
  285. }
  286. }
  287. if (material_data) {
  288. if (ginstance->data->dirty_dependencies) {
  289. material_storage->material_update_dependency(m_src, &ginstance->data->dependency_tracker);
  290. }
  291. } else {
  292. material_data = static_cast<GLES3::SceneMaterialData *>(material_storage->material_get_data(scene_globals.default_material, RS::SHADER_SPATIAL));
  293. m_src = scene_globals.default_material;
  294. }
  295. ERR_FAIL_NULL(material_data);
  296. _geometry_instance_add_surface_with_material_chain(ginstance, p_surface, material_data, m_src, p_mesh);
  297. if (ginstance->data->material_overlay.is_valid()) {
  298. m_src = ginstance->data->material_overlay;
  299. material_data = static_cast<GLES3::SceneMaterialData *>(material_storage->material_get_data(m_src, RS::SHADER_SPATIAL));
  300. if (material_data && material_data->shader_data->valid) {
  301. if (ginstance->data->dirty_dependencies) {
  302. material_storage->material_update_dependency(m_src, &ginstance->data->dependency_tracker);
  303. }
  304. _geometry_instance_add_surface_with_material_chain(ginstance, p_surface, material_data, m_src, p_mesh);
  305. }
  306. }
  307. }
  308. void RasterizerSceneGLES3::_geometry_instance_update(RenderGeometryInstance *p_geometry_instance) {
  309. GLES3::MeshStorage *mesh_storage = GLES3::MeshStorage::get_singleton();
  310. GLES3::ParticlesStorage *particles_storage = GLES3::ParticlesStorage::get_singleton();
  311. GeometryInstanceGLES3 *ginstance = static_cast<GeometryInstanceGLES3 *>(p_geometry_instance);
  312. if (ginstance->data->dirty_dependencies) {
  313. ginstance->data->dependency_tracker.update_begin();
  314. }
  315. //add geometry for drawing
  316. switch (ginstance->data->base_type) {
  317. case RS::INSTANCE_MESH: {
  318. const RID *materials = nullptr;
  319. uint32_t surface_count;
  320. RID mesh = ginstance->data->base;
  321. materials = mesh_storage->mesh_get_surface_count_and_materials(mesh, surface_count);
  322. if (materials) {
  323. //if no materials, no surfaces.
  324. const RID *inst_materials = ginstance->data->surface_materials.ptr();
  325. uint32_t surf_mat_count = ginstance->data->surface_materials.size();
  326. for (uint32_t j = 0; j < surface_count; j++) {
  327. RID material = (j < surf_mat_count && inst_materials[j].is_valid()) ? inst_materials[j] : materials[j];
  328. _geometry_instance_add_surface(ginstance, j, material, mesh);
  329. }
  330. }
  331. ginstance->instance_count = -1;
  332. } break;
  333. case RS::INSTANCE_MULTIMESH: {
  334. RID mesh = mesh_storage->multimesh_get_mesh(ginstance->data->base);
  335. if (mesh.is_valid()) {
  336. const RID *materials = nullptr;
  337. uint32_t surface_count;
  338. materials = mesh_storage->mesh_get_surface_count_and_materials(mesh, surface_count);
  339. if (materials) {
  340. for (uint32_t j = 0; j < surface_count; j++) {
  341. _geometry_instance_add_surface(ginstance, j, materials[j], mesh);
  342. }
  343. }
  344. ginstance->instance_count = mesh_storage->multimesh_get_instances_to_draw(ginstance->data->base);
  345. }
  346. } break;
  347. case RS::INSTANCE_PARTICLES: {
  348. int draw_passes = particles_storage->particles_get_draw_passes(ginstance->data->base);
  349. for (int j = 0; j < draw_passes; j++) {
  350. RID mesh = particles_storage->particles_get_draw_pass_mesh(ginstance->data->base, j);
  351. if (!mesh.is_valid()) {
  352. continue;
  353. }
  354. const RID *materials = nullptr;
  355. uint32_t surface_count;
  356. materials = mesh_storage->mesh_get_surface_count_and_materials(mesh, surface_count);
  357. if (materials) {
  358. for (uint32_t k = 0; k < surface_count; k++) {
  359. _geometry_instance_add_surface(ginstance, k, materials[k], mesh);
  360. }
  361. }
  362. }
  363. ginstance->instance_count = particles_storage->particles_get_amount(ginstance->data->base);
  364. } break;
  365. default: {
  366. }
  367. }
  368. bool store_transform = true;
  369. ginstance->base_flags = 0;
  370. if (ginstance->data->base_type == RS::INSTANCE_MULTIMESH) {
  371. ginstance->base_flags |= INSTANCE_DATA_FLAG_MULTIMESH;
  372. if (mesh_storage->multimesh_get_transform_format(ginstance->data->base) == RS::MULTIMESH_TRANSFORM_2D) {
  373. ginstance->base_flags |= INSTANCE_DATA_FLAG_MULTIMESH_FORMAT_2D;
  374. }
  375. if (mesh_storage->multimesh_uses_colors(ginstance->data->base)) {
  376. ginstance->base_flags |= INSTANCE_DATA_FLAG_MULTIMESH_HAS_COLOR;
  377. }
  378. if (mesh_storage->multimesh_uses_custom_data(ginstance->data->base)) {
  379. ginstance->base_flags |= INSTANCE_DATA_FLAG_MULTIMESH_HAS_CUSTOM_DATA;
  380. }
  381. } else if (ginstance->data->base_type == RS::INSTANCE_PARTICLES) {
  382. ginstance->base_flags |= INSTANCE_DATA_FLAG_PARTICLES;
  383. ginstance->base_flags |= INSTANCE_DATA_FLAG_MULTIMESH;
  384. ginstance->base_flags |= INSTANCE_DATA_FLAG_MULTIMESH_HAS_COLOR;
  385. ginstance->base_flags |= INSTANCE_DATA_FLAG_MULTIMESH_HAS_CUSTOM_DATA;
  386. if (!particles_storage->particles_is_using_local_coords(ginstance->data->base)) {
  387. store_transform = false;
  388. }
  389. } else if (ginstance->data->base_type == RS::INSTANCE_MESH) {
  390. if (mesh_storage->skeleton_is_valid(ginstance->data->skeleton)) {
  391. if (ginstance->data->dirty_dependencies) {
  392. mesh_storage->skeleton_update_dependency(ginstance->data->skeleton, &ginstance->data->dependency_tracker);
  393. }
  394. }
  395. }
  396. ginstance->store_transform_cache = store_transform;
  397. if (ginstance->data->dirty_dependencies) {
  398. ginstance->data->dependency_tracker.update_end();
  399. ginstance->data->dirty_dependencies = false;
  400. }
  401. ginstance->dirty_list_element.remove_from_list();
  402. }
  403. /* SKY API */
  404. void RasterizerSceneGLES3::_free_sky_data(Sky *p_sky) {
  405. if (p_sky->radiance != 0) {
  406. GLES3::Utilities::get_singleton()->texture_free_data(p_sky->radiance);
  407. p_sky->radiance = 0;
  408. GLES3::Utilities::get_singleton()->texture_free_data(p_sky->raw_radiance);
  409. p_sky->raw_radiance = 0;
  410. glDeleteFramebuffers(1, &p_sky->radiance_framebuffer);
  411. p_sky->radiance_framebuffer = 0;
  412. }
  413. }
  414. RID RasterizerSceneGLES3::sky_allocate() {
  415. return sky_owner.allocate_rid();
  416. }
  417. void RasterizerSceneGLES3::sky_initialize(RID p_rid) {
  418. sky_owner.initialize_rid(p_rid);
  419. }
  420. void RasterizerSceneGLES3::sky_set_radiance_size(RID p_sky, int p_radiance_size) {
  421. Sky *sky = sky_owner.get_or_null(p_sky);
  422. ERR_FAIL_NULL(sky);
  423. ERR_FAIL_COND_MSG(p_radiance_size < 32 || p_radiance_size > 2048, "Sky radiance size must be between 32 and 2048");
  424. if (sky->radiance_size == p_radiance_size) {
  425. return; // No need to update
  426. }
  427. sky->radiance_size = p_radiance_size;
  428. _free_sky_data(sky);
  429. _invalidate_sky(sky);
  430. }
  431. void RasterizerSceneGLES3::sky_set_mode(RID p_sky, RS::SkyMode p_mode) {
  432. Sky *sky = sky_owner.get_or_null(p_sky);
  433. ERR_FAIL_NULL(sky);
  434. if (sky->mode == p_mode) {
  435. return;
  436. }
  437. sky->mode = p_mode;
  438. _invalidate_sky(sky);
  439. }
  440. void RasterizerSceneGLES3::sky_set_material(RID p_sky, RID p_material) {
  441. Sky *sky = sky_owner.get_or_null(p_sky);
  442. ERR_FAIL_NULL(sky);
  443. if (sky->material == p_material) {
  444. return;
  445. }
  446. sky->material = p_material;
  447. _invalidate_sky(sky);
  448. }
  449. float RasterizerSceneGLES3::sky_get_baked_exposure(RID p_sky) const {
  450. Sky *sky = sky_owner.get_or_null(p_sky);
  451. ERR_FAIL_NULL_V(sky, 1.0);
  452. return sky->baked_exposure;
  453. }
  454. void RasterizerSceneGLES3::_invalidate_sky(Sky *p_sky) {
  455. if (!p_sky->dirty) {
  456. p_sky->dirty = true;
  457. p_sky->dirty_list = dirty_sky_list;
  458. dirty_sky_list = p_sky;
  459. }
  460. }
  461. GLuint _init_radiance_texture(int p_size, int p_mipmaps, String p_name) {
  462. GLuint radiance_id = 0;
  463. glGenTextures(1, &radiance_id);
  464. glBindTexture(GL_TEXTURE_CUBE_MAP, radiance_id);
  465. #ifdef GL_API_ENABLED
  466. if (RasterizerGLES3::is_gles_over_gl()) {
  467. //TODO, on low-end compare this to allocating each face of each mip individually
  468. // see: https://www.khronos.org/registry/OpenGL-Refpages/es3.0/html/glTexStorage2D.xhtml
  469. for (int i = 0; i < 6; i++) {
  470. glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + i, 0, GL_RGB10_A2, p_size, p_size, 0, GL_RGBA, GL_UNSIGNED_INT_2_10_10_10_REV, nullptr);
  471. }
  472. glGenerateMipmap(GL_TEXTURE_CUBE_MAP);
  473. }
  474. #endif // GL_API_ENABLED
  475. #ifdef GLES_API_ENABLED
  476. if (!RasterizerGLES3::is_gles_over_gl()) {
  477. glTexStorage2D(GL_TEXTURE_CUBE_MAP, p_mipmaps, GL_RGB10_A2, p_size, p_size);
  478. }
  479. #endif // GLES_API_ENABLED
  480. glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
  481. glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  482. glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  483. glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  484. glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_BASE_LEVEL, 0);
  485. glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAX_LEVEL, p_mipmaps - 1);
  486. GLES3::Utilities::get_singleton()->texture_allocated_data(radiance_id, Image::get_image_data_size(p_size, p_size, Image::FORMAT_RGBA8, true), p_name);
  487. return radiance_id;
  488. }
  489. void RasterizerSceneGLES3::_update_dirty_skys() {
  490. Sky *sky = dirty_sky_list;
  491. while (sky) {
  492. if (sky->radiance == 0) {
  493. sky->mipmap_count = Image::get_image_required_mipmaps(sky->radiance_size, sky->radiance_size, Image::FORMAT_RGBA8) - 1;
  494. // Left uninitialized, will attach a texture at render time
  495. glGenFramebuffers(1, &sky->radiance_framebuffer);
  496. sky->radiance = _init_radiance_texture(sky->radiance_size, sky->mipmap_count, "Sky radiance texture");
  497. sky->raw_radiance = _init_radiance_texture(sky->radiance_size, sky->mipmap_count, "Sky raw radiance texture");
  498. }
  499. sky->reflection_dirty = true;
  500. sky->processing_layer = 0;
  501. Sky *next = sky->dirty_list;
  502. sky->dirty_list = nullptr;
  503. sky->dirty = false;
  504. sky = next;
  505. }
  506. dirty_sky_list = nullptr;
  507. }
  508. void RasterizerSceneGLES3::_setup_sky(const RenderDataGLES3 *p_render_data, const PagedArray<RID> &p_lights, const Projection &p_projection, const Transform3D &p_transform, const Size2i p_screen_size) {
  509. GLES3::LightStorage *light_storage = GLES3::LightStorage::get_singleton();
  510. GLES3::MaterialStorage *material_storage = GLES3::MaterialStorage::get_singleton();
  511. ERR_FAIL_COND(p_render_data->environment.is_null());
  512. GLES3::SkyMaterialData *material = nullptr;
  513. Sky *sky = sky_owner.get_or_null(environment_get_sky(p_render_data->environment));
  514. RID sky_material;
  515. GLES3::SkyShaderData *shader_data = nullptr;
  516. if (sky) {
  517. sky_material = sky->material;
  518. if (sky_material.is_valid()) {
  519. material = static_cast<GLES3::SkyMaterialData *>(material_storage->material_get_data(sky_material, RS::SHADER_SKY));
  520. if (!material || !material->shader_data->valid) {
  521. material = nullptr;
  522. }
  523. }
  524. }
  525. if (!material) {
  526. sky_material = sky_globals.default_material;
  527. material = static_cast<GLES3::SkyMaterialData *>(material_storage->material_get_data(sky_material, RS::SHADER_SKY));
  528. }
  529. ERR_FAIL_NULL(material);
  530. shader_data = material->shader_data;
  531. ERR_FAIL_NULL(shader_data);
  532. if (sky) {
  533. if (shader_data->uses_time && time - sky->prev_time > 0.00001) {
  534. sky->prev_time = time;
  535. sky->reflection_dirty = true;
  536. RenderingServerDefault::redraw_request();
  537. }
  538. if (material != sky->prev_material) {
  539. sky->prev_material = material;
  540. sky->reflection_dirty = true;
  541. }
  542. if (material->uniform_set_updated) {
  543. material->uniform_set_updated = false;
  544. sky->reflection_dirty = true;
  545. }
  546. if (!p_transform.origin.is_equal_approx(sky->prev_position) && shader_data->uses_position) {
  547. sky->prev_position = p_transform.origin;
  548. sky->reflection_dirty = true;
  549. }
  550. }
  551. glBindBufferBase(GL_UNIFORM_BUFFER, SKY_DIRECTIONAL_LIGHT_UNIFORM_LOCATION, sky_globals.directional_light_buffer);
  552. if (shader_data->uses_light || (environment_get_fog_enabled(p_render_data->environment) && environment_get_fog_sun_scatter(p_render_data->environment) > 0.001)) {
  553. sky_globals.directional_light_count = 0;
  554. for (int i = 0; i < (int)p_lights.size(); i++) {
  555. GLES3::LightInstance *li = GLES3::LightStorage::get_singleton()->get_light_instance(p_lights[i]);
  556. if (!li) {
  557. continue;
  558. }
  559. RID base = li->light;
  560. ERR_CONTINUE(base.is_null());
  561. RS::LightType type = light_storage->light_get_type(base);
  562. if (type == RS::LIGHT_DIRECTIONAL && light_storage->light_directional_get_sky_mode(base) != RS::LIGHT_DIRECTIONAL_SKY_MODE_LIGHT_ONLY) {
  563. DirectionalLightData &sky_light_data = sky_globals.directional_lights[sky_globals.directional_light_count];
  564. Transform3D light_transform = li->transform;
  565. Vector3 world_direction = light_transform.basis.xform(Vector3(0, 0, 1)).normalized();
  566. sky_light_data.direction[0] = world_direction.x;
  567. sky_light_data.direction[1] = world_direction.y;
  568. sky_light_data.direction[2] = world_direction.z;
  569. float sign = light_storage->light_is_negative(base) ? -1 : 1;
  570. sky_light_data.energy = sign * light_storage->light_get_param(base, RS::LIGHT_PARAM_ENERGY);
  571. if (is_using_physical_light_units()) {
  572. sky_light_data.energy *= light_storage->light_get_param(base, RS::LIGHT_PARAM_INTENSITY);
  573. }
  574. if (p_render_data->camera_attributes.is_valid()) {
  575. sky_light_data.energy *= RSG::camera_attributes->camera_attributes_get_exposure_normalization_factor(p_render_data->camera_attributes);
  576. }
  577. Color srgb_col = light_storage->light_get_color(base);
  578. sky_light_data.color[0] = srgb_col.r;
  579. sky_light_data.color[1] = srgb_col.g;
  580. sky_light_data.color[2] = srgb_col.b;
  581. sky_light_data.enabled = true;
  582. float angular_diameter = light_storage->light_get_param(base, RS::LIGHT_PARAM_SIZE);
  583. sky_light_data.size = Math::deg_to_rad(angular_diameter);
  584. sky_globals.directional_light_count++;
  585. if (sky_globals.directional_light_count >= sky_globals.max_directional_lights) {
  586. break;
  587. }
  588. }
  589. }
  590. // Check whether the directional_light_buffer changes
  591. bool light_data_dirty = false;
  592. // Light buffer is dirty if we have fewer or more lights
  593. // If we have fewer lights, make sure that old lights are disabled
  594. if (sky_globals.directional_light_count != sky_globals.last_frame_directional_light_count) {
  595. light_data_dirty = true;
  596. for (uint32_t i = sky_globals.directional_light_count; i < sky_globals.max_directional_lights; i++) {
  597. sky_globals.directional_lights[i].enabled = false;
  598. sky_globals.last_frame_directional_lights[i].enabled = false;
  599. }
  600. }
  601. if (!light_data_dirty) {
  602. for (uint32_t i = 0; i < sky_globals.directional_light_count; i++) {
  603. if (sky_globals.directional_lights[i].direction[0] != sky_globals.last_frame_directional_lights[i].direction[0] ||
  604. sky_globals.directional_lights[i].direction[1] != sky_globals.last_frame_directional_lights[i].direction[1] ||
  605. sky_globals.directional_lights[i].direction[2] != sky_globals.last_frame_directional_lights[i].direction[2] ||
  606. sky_globals.directional_lights[i].energy != sky_globals.last_frame_directional_lights[i].energy ||
  607. sky_globals.directional_lights[i].color[0] != sky_globals.last_frame_directional_lights[i].color[0] ||
  608. sky_globals.directional_lights[i].color[1] != sky_globals.last_frame_directional_lights[i].color[1] ||
  609. sky_globals.directional_lights[i].color[2] != sky_globals.last_frame_directional_lights[i].color[2] ||
  610. sky_globals.directional_lights[i].enabled != sky_globals.last_frame_directional_lights[i].enabled ||
  611. sky_globals.directional_lights[i].size != sky_globals.last_frame_directional_lights[i].size) {
  612. light_data_dirty = true;
  613. break;
  614. }
  615. }
  616. }
  617. if (light_data_dirty) {
  618. glBufferData(GL_UNIFORM_BUFFER, sizeof(DirectionalLightData) * sky_globals.max_directional_lights, sky_globals.directional_lights, GL_STREAM_DRAW);
  619. glBindBuffer(GL_UNIFORM_BUFFER, 0);
  620. DirectionalLightData *temp = sky_globals.last_frame_directional_lights;
  621. sky_globals.last_frame_directional_lights = sky_globals.directional_lights;
  622. sky_globals.directional_lights = temp;
  623. sky_globals.last_frame_directional_light_count = sky_globals.directional_light_count;
  624. if (sky) {
  625. sky->reflection_dirty = true;
  626. }
  627. }
  628. }
  629. if (p_render_data->view_count > 1) {
  630. glBindBufferBase(GL_UNIFORM_BUFFER, SKY_MULTIVIEW_UNIFORM_LOCATION, scene_state.multiview_buffer);
  631. glBindBuffer(GL_UNIFORM_BUFFER, 0);
  632. }
  633. if (sky && !sky->radiance) {
  634. _invalidate_sky(sky);
  635. _update_dirty_skys();
  636. }
  637. }
  638. void RasterizerSceneGLES3::_draw_sky(RID p_env, const Projection &p_projection, const Transform3D &p_transform, float p_sky_energy_multiplier, float p_luminance_multiplier, bool p_use_multiview, bool p_flip_y, bool p_apply_color_adjustments_in_post) {
  639. GLES3::MaterialStorage *material_storage = GLES3::MaterialStorage::get_singleton();
  640. ERR_FAIL_COND(p_env.is_null());
  641. Sky *sky = sky_owner.get_or_null(environment_get_sky(p_env));
  642. GLES3::SkyMaterialData *material_data = nullptr;
  643. RID sky_material;
  644. uint64_t spec_constants = p_use_multiview ? SkyShaderGLES3::USE_MULTIVIEW : 0;
  645. if (p_flip_y) {
  646. spec_constants |= SkyShaderGLES3::USE_INVERTED_Y;
  647. }
  648. if (!p_apply_color_adjustments_in_post) {
  649. spec_constants |= SkyShaderGLES3::APPLY_TONEMAPPING;
  650. }
  651. RS::EnvironmentBG background = environment_get_background(p_env);
  652. if (sky) {
  653. sky_material = sky->material;
  654. if (sky_material.is_valid()) {
  655. material_data = static_cast<GLES3::SkyMaterialData *>(material_storage->material_get_data(sky_material, RS::SHADER_SKY));
  656. if (!material_data || !material_data->shader_data->valid) {
  657. material_data = nullptr;
  658. }
  659. }
  660. if (!material_data) {
  661. sky_material = sky_globals.default_material;
  662. material_data = static_cast<GLES3::SkyMaterialData *>(material_storage->material_get_data(sky_material, RS::SHADER_SKY));
  663. }
  664. } else if (background == RS::ENV_BG_CLEAR_COLOR || background == RS::ENV_BG_COLOR) {
  665. sky_material = sky_globals.fog_material;
  666. material_data = static_cast<GLES3::SkyMaterialData *>(material_storage->material_get_data(sky_material, RS::SHADER_SKY));
  667. }
  668. ERR_FAIL_NULL(material_data);
  669. material_data->bind_uniforms();
  670. GLES3::SkyShaderData *shader_data = material_data->shader_data;
  671. ERR_FAIL_NULL(shader_data);
  672. // Camera
  673. Projection camera;
  674. if (environment_get_sky_custom_fov(p_env)) {
  675. float near_plane = p_projection.get_z_near();
  676. float far_plane = p_projection.get_z_far();
  677. float aspect = p_projection.get_aspect();
  678. camera.set_perspective(environment_get_sky_custom_fov(p_env), aspect, near_plane, far_plane);
  679. } else {
  680. camera = p_projection;
  681. }
  682. Projection correction;
  683. correction.set_depth_correction(false, true, false);
  684. camera = correction * camera;
  685. Basis sky_transform = environment_get_sky_orientation(p_env);
  686. sky_transform.invert();
  687. sky_transform = sky_transform * p_transform.basis;
  688. bool success = material_storage->shaders.sky_shader.version_bind_shader(shader_data->version, SkyShaderGLES3::MODE_BACKGROUND, spec_constants);
  689. if (!success) {
  690. return;
  691. }
  692. material_storage->shaders.sky_shader.version_set_uniform(SkyShaderGLES3::ORIENTATION, sky_transform, shader_data->version, SkyShaderGLES3::MODE_BACKGROUND, spec_constants);
  693. material_storage->shaders.sky_shader.version_set_uniform(SkyShaderGLES3::PROJECTION, camera.columns[2][0], camera.columns[0][0], camera.columns[2][1], camera.columns[1][1], shader_data->version, SkyShaderGLES3::MODE_BACKGROUND, spec_constants);
  694. material_storage->shaders.sky_shader.version_set_uniform(SkyShaderGLES3::POSITION, p_transform.origin, shader_data->version, SkyShaderGLES3::MODE_BACKGROUND, spec_constants);
  695. material_storage->shaders.sky_shader.version_set_uniform(SkyShaderGLES3::TIME, time, shader_data->version, SkyShaderGLES3::MODE_BACKGROUND, spec_constants);
  696. material_storage->shaders.sky_shader.version_set_uniform(SkyShaderGLES3::SKY_ENERGY_MULTIPLIER, p_sky_energy_multiplier, shader_data->version, SkyShaderGLES3::MODE_BACKGROUND, spec_constants);
  697. material_storage->shaders.sky_shader.version_set_uniform(SkyShaderGLES3::LUMINANCE_MULTIPLIER, p_luminance_multiplier, shader_data->version, SkyShaderGLES3::MODE_BACKGROUND, spec_constants);
  698. Color fog_color = environment_get_fog_light_color(p_env).srgb_to_linear() * environment_get_fog_light_energy(p_env);
  699. material_storage->shaders.sky_shader.version_set_uniform(SkyShaderGLES3::FOG_ENABLED, environment_get_fog_enabled(p_env), shader_data->version, SkyShaderGLES3::MODE_BACKGROUND, spec_constants);
  700. material_storage->shaders.sky_shader.version_set_uniform(SkyShaderGLES3::FOG_AERIAL_PERSPECTIVE, environment_get_fog_aerial_perspective(p_env), shader_data->version, SkyShaderGLES3::MODE_BACKGROUND, spec_constants);
  701. material_storage->shaders.sky_shader.version_set_uniform(SkyShaderGLES3::FOG_LIGHT_COLOR, fog_color, shader_data->version, SkyShaderGLES3::MODE_BACKGROUND, spec_constants);
  702. material_storage->shaders.sky_shader.version_set_uniform(SkyShaderGLES3::FOG_SUN_SCATTER, environment_get_fog_sun_scatter(p_env), shader_data->version, SkyShaderGLES3::MODE_BACKGROUND, spec_constants);
  703. material_storage->shaders.sky_shader.version_set_uniform(SkyShaderGLES3::FOG_DENSITY, environment_get_fog_density(p_env), shader_data->version, SkyShaderGLES3::MODE_BACKGROUND, spec_constants);
  704. material_storage->shaders.sky_shader.version_set_uniform(SkyShaderGLES3::FOG_SKY_AFFECT, environment_get_fog_sky_affect(p_env), shader_data->version, SkyShaderGLES3::MODE_BACKGROUND, spec_constants);
  705. material_storage->shaders.sky_shader.version_set_uniform(SkyShaderGLES3::DIRECTIONAL_LIGHT_COUNT, sky_globals.directional_light_count, shader_data->version, SkyShaderGLES3::MODE_BACKGROUND, spec_constants);
  706. if (p_use_multiview) {
  707. glBindBufferBase(GL_UNIFORM_BUFFER, SKY_MULTIVIEW_UNIFORM_LOCATION, scene_state.multiview_buffer);
  708. glBindBuffer(GL_UNIFORM_BUFFER, 0);
  709. }
  710. glBindVertexArray(sky_globals.screen_triangle_array);
  711. glDrawArrays(GL_TRIANGLES, 0, 3);
  712. }
  713. void RasterizerSceneGLES3::_update_sky_radiance(RID p_env, const Projection &p_projection, const Transform3D &p_transform, float p_sky_energy_multiplier) {
  714. GLES3::CubemapFilter *cubemap_filter = GLES3::CubemapFilter::get_singleton();
  715. GLES3::MaterialStorage *material_storage = GLES3::MaterialStorage::get_singleton();
  716. ERR_FAIL_COND(p_env.is_null());
  717. Sky *sky = sky_owner.get_or_null(environment_get_sky(p_env));
  718. ERR_FAIL_NULL(sky);
  719. GLES3::SkyMaterialData *material_data = nullptr;
  720. RID sky_material;
  721. RS::EnvironmentBG background = environment_get_background(p_env);
  722. if (sky) {
  723. ERR_FAIL_NULL(sky);
  724. sky_material = sky->material;
  725. if (sky_material.is_valid()) {
  726. material_data = static_cast<GLES3::SkyMaterialData *>(material_storage->material_get_data(sky_material, RS::SHADER_SKY));
  727. if (!material_data || !material_data->shader_data->valid) {
  728. material_data = nullptr;
  729. }
  730. }
  731. if (!material_data) {
  732. sky_material = sky_globals.default_material;
  733. material_data = static_cast<GLES3::SkyMaterialData *>(material_storage->material_get_data(sky_material, RS::SHADER_SKY));
  734. }
  735. } else if (background == RS::ENV_BG_CLEAR_COLOR || background == RS::ENV_BG_COLOR) {
  736. sky_material = sky_globals.fog_material;
  737. material_data = static_cast<GLES3::SkyMaterialData *>(material_storage->material_get_data(sky_material, RS::SHADER_SKY));
  738. }
  739. ERR_FAIL_NULL(material_data);
  740. material_data->bind_uniforms();
  741. GLES3::SkyShaderData *shader_data = material_data->shader_data;
  742. ERR_FAIL_NULL(shader_data);
  743. bool update_single_frame = sky->mode == RS::SKY_MODE_REALTIME || sky->mode == RS::SKY_MODE_QUALITY;
  744. RS::SkyMode sky_mode = sky->mode;
  745. if (sky_mode == RS::SKY_MODE_AUTOMATIC) {
  746. if ((shader_data->uses_time || shader_data->uses_position) && sky->radiance_size == 256) {
  747. update_single_frame = true;
  748. sky_mode = RS::SKY_MODE_REALTIME;
  749. } else if (shader_data->uses_light || shader_data->ubo_size > 0) {
  750. update_single_frame = false;
  751. sky_mode = RS::SKY_MODE_INCREMENTAL;
  752. } else {
  753. update_single_frame = true;
  754. sky_mode = RS::SKY_MODE_QUALITY;
  755. }
  756. }
  757. if (sky->processing_layer == 0 && sky_mode == RS::SKY_MODE_INCREMENTAL) {
  758. // On the first frame after creating sky, rebuild in single frame
  759. update_single_frame = true;
  760. sky_mode = RS::SKY_MODE_QUALITY;
  761. }
  762. int max_processing_layer = sky->mipmap_count;
  763. // Update radiance cubemap
  764. if (sky->reflection_dirty && (sky->processing_layer >= max_processing_layer || update_single_frame)) {
  765. static const Vector3 view_normals[6] = {
  766. Vector3(+1, 0, 0),
  767. Vector3(-1, 0, 0),
  768. Vector3(0, +1, 0),
  769. Vector3(0, -1, 0),
  770. Vector3(0, 0, +1),
  771. Vector3(0, 0, -1)
  772. };
  773. static const Vector3 view_up[6] = {
  774. Vector3(0, -1, 0),
  775. Vector3(0, -1, 0),
  776. Vector3(0, 0, +1),
  777. Vector3(0, 0, -1),
  778. Vector3(0, -1, 0),
  779. Vector3(0, -1, 0)
  780. };
  781. Projection cm;
  782. cm.set_perspective(90, 1, 0.01, 10.0);
  783. Projection correction;
  784. correction.set_depth_correction(true, true, false);
  785. cm = correction * cm;
  786. bool success = material_storage->shaders.sky_shader.version_bind_shader(shader_data->version, SkyShaderGLES3::MODE_CUBEMAP);
  787. if (!success) {
  788. return;
  789. }
  790. material_storage->shaders.sky_shader.version_set_uniform(SkyShaderGLES3::POSITION, p_transform.origin, shader_data->version, SkyShaderGLES3::MODE_CUBEMAP);
  791. material_storage->shaders.sky_shader.version_set_uniform(SkyShaderGLES3::TIME, time, shader_data->version, SkyShaderGLES3::MODE_CUBEMAP);
  792. material_storage->shaders.sky_shader.version_set_uniform(SkyShaderGLES3::PROJECTION, cm.columns[2][0], cm.columns[0][0], cm.columns[2][1], cm.columns[1][1], shader_data->version, SkyShaderGLES3::MODE_CUBEMAP);
  793. material_storage->shaders.sky_shader.version_set_uniform(SkyShaderGLES3::SKY_ENERGY_MULTIPLIER, p_sky_energy_multiplier, shader_data->version, SkyShaderGLES3::MODE_CUBEMAP);
  794. material_storage->shaders.sky_shader.version_set_uniform(SkyShaderGLES3::LUMINANCE_MULTIPLIER, 1.0, shader_data->version, SkyShaderGLES3::MODE_CUBEMAP);
  795. glBindVertexArray(sky_globals.screen_triangle_array);
  796. glViewport(0, 0, sky->radiance_size, sky->radiance_size);
  797. glBindFramebuffer(GL_FRAMEBUFFER, sky->radiance_framebuffer);
  798. scene_state.reset_gl_state();
  799. scene_state.set_gl_cull_mode(RS::CULL_MODE_DISABLED);
  800. scene_state.enable_gl_blend(false);
  801. for (int i = 0; i < 6; i++) {
  802. Basis local_view = Basis::looking_at(view_normals[i], view_up[i]);
  803. material_storage->shaders.sky_shader.version_set_uniform(SkyShaderGLES3::ORIENTATION, local_view, shader_data->version, SkyShaderGLES3::MODE_CUBEMAP);
  804. glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_CUBE_MAP_POSITIVE_X + i, sky->raw_radiance, 0);
  805. glDrawArrays(GL_TRIANGLES, 0, 3);
  806. }
  807. if (update_single_frame) {
  808. for (int i = 0; i < max_processing_layer; i++) {
  809. cubemap_filter->filter_radiance(sky->raw_radiance, sky->radiance, sky->radiance_framebuffer, sky->radiance_size, sky->mipmap_count, i);
  810. }
  811. } else {
  812. cubemap_filter->filter_radiance(sky->raw_radiance, sky->radiance, sky->radiance_framebuffer, sky->radiance_size, sky->mipmap_count, 0); // Just copy over the first mipmap.
  813. }
  814. sky->processing_layer = 1;
  815. sky->baked_exposure = p_sky_energy_multiplier;
  816. sky->reflection_dirty = false;
  817. } else {
  818. if (sky_mode == RS::SKY_MODE_INCREMENTAL && sky->processing_layer < max_processing_layer) {
  819. scene_state.reset_gl_state();
  820. scene_state.set_gl_cull_mode(RS::CULL_MODE_DISABLED);
  821. scene_state.enable_gl_blend(false);
  822. cubemap_filter->filter_radiance(sky->raw_radiance, sky->radiance, sky->radiance_framebuffer, sky->radiance_size, sky->mipmap_count, sky->processing_layer);
  823. sky->processing_layer++;
  824. }
  825. }
  826. glViewport(0, 0, sky->screen_size.x, sky->screen_size.y);
  827. }
  828. Ref<Image> RasterizerSceneGLES3::sky_bake_panorama(RID p_sky, float p_energy, bool p_bake_irradiance, const Size2i &p_size) {
  829. Sky *sky = sky_owner.get_or_null(p_sky);
  830. ERR_FAIL_NULL_V(sky, Ref<Image>());
  831. _update_dirty_skys();
  832. if (sky->radiance == 0) {
  833. return Ref<Image>();
  834. }
  835. GLES3::CopyEffects *copy_effects = GLES3::CopyEffects::get_singleton();
  836. GLES3::Config *config = GLES3::Config::get_singleton();
  837. GLuint rad_tex = 0;
  838. glGenTextures(1, &rad_tex);
  839. glBindTexture(GL_TEXTURE_2D, rad_tex);
  840. if (config->float_texture_supported) {
  841. glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA32F, p_size.width, p_size.height, 0, GL_RGBA, GL_FLOAT, nullptr);
  842. GLES3::Utilities::get_singleton()->texture_allocated_data(rad_tex, p_size.width * p_size.height * 16, "Temp sky panorama");
  843. } else {
  844. // Fallback to RGBA8 on devices that don't support rendering to floating point textures. This will look bad, but we have no choice.
  845. glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, p_size.width, p_size.height, 0, GL_RGBA, GL_UNSIGNED_BYTE, nullptr);
  846. GLES3::Utilities::get_singleton()->texture_allocated_data(rad_tex, p_size.width * p_size.height * 4, "Temp sky panorama");
  847. }
  848. GLuint rad_fbo = 0;
  849. glGenFramebuffers(1, &rad_fbo);
  850. glBindFramebuffer(GL_FRAMEBUFFER, rad_fbo);
  851. glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, rad_tex, 0);
  852. glActiveTexture(GL_TEXTURE0);
  853. glBindTexture(GL_TEXTURE_CUBE_MAP, sky->radiance);
  854. glViewport(0, 0, p_size.width, p_size.height);
  855. glClearColor(0.0, 0.0, 0.0, 1.0);
  856. glClear(GL_COLOR_BUFFER_BIT);
  857. copy_effects->copy_cube_to_panorama(p_bake_irradiance ? float(sky->mipmap_count) : 0.0);
  858. glBindFramebuffer(GL_FRAMEBUFFER, GLES3::TextureStorage::system_fbo);
  859. glDeleteFramebuffers(1, &rad_fbo);
  860. // Create a dummy texture so we can use texture_2d_get.
  861. RID tex_rid = GLES3::TextureStorage::get_singleton()->texture_allocate();
  862. {
  863. GLES3::Texture texture;
  864. texture.width = p_size.width;
  865. texture.height = p_size.height;
  866. texture.alloc_width = p_size.width;
  867. texture.alloc_height = p_size.height;
  868. texture.format = Image::FORMAT_RGBAF;
  869. texture.real_format = Image::FORMAT_RGBAF;
  870. texture.gl_format_cache = GL_RGBA;
  871. texture.gl_type_cache = GL_FLOAT;
  872. texture.type = GLES3::Texture::TYPE_2D;
  873. texture.target = GL_TEXTURE_2D;
  874. texture.active = true;
  875. texture.tex_id = rad_tex;
  876. texture.is_render_target = true; // HACK: Prevent TextureStorage from retaining a cached copy of the texture.
  877. GLES3::TextureStorage::get_singleton()->texture_2d_initialize_from_texture(tex_rid, texture);
  878. }
  879. Ref<Image> img = GLES3::TextureStorage::get_singleton()->texture_2d_get(tex_rid);
  880. GLES3::Utilities::get_singleton()->texture_free_data(rad_tex);
  881. GLES3::Texture &texture = *GLES3::TextureStorage::get_singleton()->get_texture(tex_rid);
  882. texture.is_render_target = false; // HACK: Avoid an error when freeing the texture.
  883. texture.tex_id = 0;
  884. GLES3::TextureStorage::get_singleton()->texture_free(tex_rid);
  885. for (int i = 0; i < p_size.width; i++) {
  886. for (int j = 0; j < p_size.height; j++) {
  887. Color c = img->get_pixel(i, j);
  888. c.r *= p_energy;
  889. c.g *= p_energy;
  890. c.b *= p_energy;
  891. img->set_pixel(i, j, c);
  892. }
  893. }
  894. return img;
  895. }
  896. /* ENVIRONMENT API */
  897. void RasterizerSceneGLES3::environment_glow_set_use_bicubic_upscale(bool p_enable) {
  898. glow_bicubic_upscale = p_enable;
  899. }
  900. void RasterizerSceneGLES3::environment_set_ssr_roughness_quality(RS::EnvironmentSSRRoughnessQuality p_quality) {
  901. }
  902. void RasterizerSceneGLES3::environment_set_ssao_quality(RS::EnvironmentSSAOQuality p_quality, bool p_half_size, float p_adaptive_target, int p_blur_passes, float p_fadeout_from, float p_fadeout_to) {
  903. }
  904. void RasterizerSceneGLES3::environment_set_ssil_quality(RS::EnvironmentSSILQuality p_quality, bool p_half_size, float p_adaptive_target, int p_blur_passes, float p_fadeout_from, float p_fadeout_to) {
  905. }
  906. void RasterizerSceneGLES3::environment_set_sdfgi_ray_count(RS::EnvironmentSDFGIRayCount p_ray_count) {
  907. }
  908. void RasterizerSceneGLES3::environment_set_sdfgi_frames_to_converge(RS::EnvironmentSDFGIFramesToConverge p_frames) {
  909. }
  910. void RasterizerSceneGLES3::environment_set_sdfgi_frames_to_update_light(RS::EnvironmentSDFGIFramesToUpdateLight p_update) {
  911. }
  912. void RasterizerSceneGLES3::environment_set_volumetric_fog_volume_size(int p_size, int p_depth) {
  913. }
  914. void RasterizerSceneGLES3::environment_set_volumetric_fog_filter_active(bool p_enable) {
  915. }
  916. Ref<Image> RasterizerSceneGLES3::environment_bake_panorama(RID p_env, bool p_bake_irradiance, const Size2i &p_size) {
  917. ERR_FAIL_COND_V(p_env.is_null(), Ref<Image>());
  918. RS::EnvironmentBG environment_background = environment_get_background(p_env);
  919. if (environment_background == RS::ENV_BG_CAMERA_FEED || environment_background == RS::ENV_BG_CANVAS || environment_background == RS::ENV_BG_KEEP) {
  920. return Ref<Image>(); // Nothing to bake.
  921. }
  922. RS::EnvironmentAmbientSource ambient_source = environment_get_ambient_source(p_env);
  923. bool use_ambient_light = false;
  924. bool use_cube_map = false;
  925. if (ambient_source == RS::ENV_AMBIENT_SOURCE_BG && (environment_background == RS::ENV_BG_CLEAR_COLOR || environment_background == RS::ENV_BG_COLOR)) {
  926. use_ambient_light = true;
  927. } else {
  928. use_cube_map = (ambient_source == RS::ENV_AMBIENT_SOURCE_BG && environment_background == RS::ENV_BG_SKY) || ambient_source == RS::ENV_AMBIENT_SOURCE_SKY;
  929. use_ambient_light = use_cube_map || ambient_source == RS::ENV_AMBIENT_SOURCE_COLOR;
  930. }
  931. use_cube_map = use_cube_map || (environment_background == RS::ENV_BG_SKY && environment_get_sky(p_env).is_valid());
  932. Color ambient_color;
  933. float ambient_color_sky_mix = 0.0;
  934. if (use_ambient_light) {
  935. ambient_color_sky_mix = environment_get_ambient_sky_contribution(p_env);
  936. const float ambient_energy = environment_get_ambient_light_energy(p_env);
  937. ambient_color = environment_get_ambient_light(p_env);
  938. ambient_color = ambient_color.srgb_to_linear();
  939. ambient_color.r *= ambient_energy;
  940. ambient_color.g *= ambient_energy;
  941. ambient_color.b *= ambient_energy;
  942. }
  943. if (use_cube_map) {
  944. Ref<Image> panorama = sky_bake_panorama(environment_get_sky(p_env), environment_get_bg_energy_multiplier(p_env), p_bake_irradiance, p_size);
  945. if (use_ambient_light) {
  946. for (int x = 0; x < p_size.width; x++) {
  947. for (int y = 0; y < p_size.height; y++) {
  948. panorama->set_pixel(x, y, ambient_color.lerp(panorama->get_pixel(x, y), ambient_color_sky_mix));
  949. }
  950. }
  951. }
  952. return panorama;
  953. } else {
  954. const float bg_energy_multiplier = environment_get_bg_energy_multiplier(p_env);
  955. Color panorama_color = ((environment_background == RS::ENV_BG_CLEAR_COLOR) ? RSG::texture_storage->get_default_clear_color() : environment_get_bg_color(p_env));
  956. panorama_color = panorama_color.srgb_to_linear();
  957. panorama_color.r *= bg_energy_multiplier;
  958. panorama_color.g *= bg_energy_multiplier;
  959. panorama_color.b *= bg_energy_multiplier;
  960. if (use_ambient_light) {
  961. panorama_color = ambient_color.lerp(panorama_color, ambient_color_sky_mix);
  962. }
  963. Ref<Image> panorama = Image::create_empty(p_size.width, p_size.height, false, Image::FORMAT_RGBAF);
  964. panorama->fill(panorama_color);
  965. return panorama;
  966. }
  967. }
  968. void RasterizerSceneGLES3::positional_soft_shadow_filter_set_quality(RS::ShadowQuality p_quality) {
  969. scene_state.positional_shadow_quality = p_quality;
  970. }
  971. void RasterizerSceneGLES3::directional_soft_shadow_filter_set_quality(RS::ShadowQuality p_quality) {
  972. scene_state.directional_shadow_quality = p_quality;
  973. }
  974. RID RasterizerSceneGLES3::fog_volume_instance_create(RID p_fog_volume) {
  975. return RID();
  976. }
  977. void RasterizerSceneGLES3::fog_volume_instance_set_transform(RID p_fog_volume_instance, const Transform3D &p_transform) {
  978. }
  979. void RasterizerSceneGLES3::fog_volume_instance_set_active(RID p_fog_volume_instance, bool p_active) {
  980. }
  981. RID RasterizerSceneGLES3::fog_volume_instance_get_volume(RID p_fog_volume_instance) const {
  982. return RID();
  983. }
  984. Vector3 RasterizerSceneGLES3::fog_volume_instance_get_position(RID p_fog_volume_instance) const {
  985. return Vector3();
  986. }
  987. RID RasterizerSceneGLES3::voxel_gi_instance_create(RID p_voxel_gi) {
  988. return RID();
  989. }
  990. void RasterizerSceneGLES3::voxel_gi_instance_set_transform_to_data(RID p_probe, const Transform3D &p_xform) {
  991. }
  992. bool RasterizerSceneGLES3::voxel_gi_needs_update(RID p_probe) const {
  993. return false;
  994. }
  995. void RasterizerSceneGLES3::voxel_gi_update(RID p_probe, bool p_update_light_instances, const Vector<RID> &p_light_instances, const PagedArray<RenderGeometryInstance *> &p_dynamic_objects) {
  996. }
  997. void RasterizerSceneGLES3::voxel_gi_set_quality(RS::VoxelGIQuality) {
  998. }
  999. _FORCE_INLINE_ static uint32_t _indices_to_primitives(RS::PrimitiveType p_primitive, uint32_t p_indices) {
  1000. static const uint32_t divisor[RS::PRIMITIVE_MAX] = { 1, 2, 1, 3, 1 };
  1001. static const uint32_t subtractor[RS::PRIMITIVE_MAX] = { 0, 0, 1, 0, 2 };
  1002. return (p_indices - subtractor[p_primitive]) / divisor[p_primitive];
  1003. }
  1004. void RasterizerSceneGLES3::_fill_render_list(RenderListType p_render_list, const RenderDataGLES3 *p_render_data, PassMode p_pass_mode, bool p_append) {
  1005. GLES3::MeshStorage *mesh_storage = GLES3::MeshStorage::get_singleton();
  1006. GLES3::LightStorage *light_storage = GLES3::LightStorage::get_singleton();
  1007. if (p_render_list == RENDER_LIST_OPAQUE) {
  1008. scene_state.used_screen_texture = false;
  1009. scene_state.used_normal_texture = false;
  1010. scene_state.used_depth_texture = false;
  1011. scene_state.used_opaque_stencil = false;
  1012. }
  1013. Plane near_plane;
  1014. if (p_render_data->cam_orthogonal) {
  1015. near_plane = Plane(-p_render_data->cam_transform.basis.get_column(Vector3::AXIS_Z), p_render_data->cam_transform.origin);
  1016. near_plane.d += p_render_data->cam_projection.get_z_near();
  1017. }
  1018. float z_max = p_render_data->cam_projection.get_z_far() - p_render_data->cam_projection.get_z_near();
  1019. RenderList *rl = &render_list[p_render_list];
  1020. // Parse any updates on our geometry, updates surface caches and such
  1021. _update_dirty_geometry_instances();
  1022. if (!p_append) {
  1023. rl->clear();
  1024. if (p_render_list == RENDER_LIST_OPAQUE) {
  1025. render_list[RENDER_LIST_ALPHA].clear(); //opaque fills alpha too
  1026. }
  1027. }
  1028. //fill list
  1029. for (int i = 0; i < (int)p_render_data->instances->size(); i++) {
  1030. GeometryInstanceGLES3 *inst = static_cast<GeometryInstanceGLES3 *>((*p_render_data->instances)[i]);
  1031. Vector3 center = inst->transform.origin;
  1032. if (p_render_data->cam_orthogonal) {
  1033. if (inst->use_aabb_center) {
  1034. center = inst->transformed_aabb.get_support(-near_plane.normal);
  1035. }
  1036. inst->depth = near_plane.distance_to(center) - inst->sorting_offset;
  1037. } else {
  1038. if (inst->use_aabb_center) {
  1039. center = inst->transformed_aabb.position + (inst->transformed_aabb.size * 0.5);
  1040. }
  1041. inst->depth = p_render_data->cam_transform.origin.distance_to(center) - inst->sorting_offset;
  1042. }
  1043. uint32_t depth_layer = CLAMP(int(inst->depth * 16 / z_max), 0, 15);
  1044. uint32_t flags = inst->base_flags; //fill flags if appropriate
  1045. if (inst->non_uniform_scale) {
  1046. flags |= INSTANCE_DATA_FLAGS_NON_UNIFORM_SCALE;
  1047. }
  1048. // Sets the index values for lookup in the shader
  1049. // This has to be done after _setup_lights was called this frame
  1050. if (p_pass_mode == PASS_MODE_COLOR) {
  1051. inst->light_passes.clear();
  1052. inst->spot_light_gl_cache.clear();
  1053. inst->omni_light_gl_cache.clear();
  1054. inst->reflection_probes_local_transform_cache.clear();
  1055. inst->reflection_probe_rid_cache.clear();
  1056. uint64_t current_frame = RSG::rasterizer->get_frame_number();
  1057. if (inst->paired_omni_light_count) {
  1058. for (uint32_t j = 0; j < inst->paired_omni_light_count; j++) {
  1059. RID light_instance = inst->paired_omni_lights[j];
  1060. if (light_storage->light_instance_get_render_pass(light_instance) != current_frame) {
  1061. continue;
  1062. }
  1063. RID light = light_storage->light_instance_get_base_light(light_instance);
  1064. int32_t shadow_id = light_storage->light_instance_get_shadow_id(light_instance);
  1065. if (light_storage->light_has_shadow(light) && shadow_id >= 0) {
  1066. GeometryInstanceGLES3::LightPass pass;
  1067. pass.light_id = light_storage->light_instance_get_gl_id(light_instance);
  1068. pass.shadow_id = shadow_id;
  1069. pass.light_instance_rid = light_instance;
  1070. pass.is_omni = true;
  1071. inst->light_passes.push_back(pass);
  1072. } else {
  1073. // Lights without shadow can all go in base pass.
  1074. inst->omni_light_gl_cache.push_back((uint32_t)light_storage->light_instance_get_gl_id(light_instance));
  1075. }
  1076. }
  1077. }
  1078. if (inst->paired_spot_light_count) {
  1079. for (uint32_t j = 0; j < inst->paired_spot_light_count; j++) {
  1080. RID light_instance = inst->paired_spot_lights[j];
  1081. if (light_storage->light_instance_get_render_pass(light_instance) != current_frame) {
  1082. continue;
  1083. }
  1084. RID light = light_storage->light_instance_get_base_light(light_instance);
  1085. int32_t shadow_id = light_storage->light_instance_get_shadow_id(light_instance);
  1086. if (light_storage->light_has_shadow(light) && shadow_id >= 0) {
  1087. GeometryInstanceGLES3::LightPass pass;
  1088. pass.light_id = light_storage->light_instance_get_gl_id(light_instance);
  1089. pass.shadow_id = shadow_id;
  1090. pass.light_instance_rid = light_instance;
  1091. inst->light_passes.push_back(pass);
  1092. } else {
  1093. // Lights without shadow can all go in base pass.
  1094. inst->spot_light_gl_cache.push_back((uint32_t)light_storage->light_instance_get_gl_id(light_instance));
  1095. }
  1096. }
  1097. }
  1098. if (p_render_data->reflection_probe.is_null() && inst->paired_reflection_probes.size() > 0) {
  1099. // Do not include if we're rendering reflection probes.
  1100. // We only support two probes for now and we handle them first come, first serve.
  1101. // This should be improved one day, at minimum the list should be sorted by priority.
  1102. for (uint32_t pi = 0; pi < inst->paired_reflection_probes.size(); pi++) {
  1103. RID probe_instance = inst->paired_reflection_probes[pi];
  1104. RID atlas = light_storage->reflection_probe_instance_get_atlas(probe_instance);
  1105. RID probe = light_storage->reflection_probe_instance_get_probe(probe_instance);
  1106. uint32_t reflection_mask = light_storage->reflection_probe_get_reflection_mask(probe);
  1107. if (atlas.is_valid() && (inst->layer_mask & reflection_mask)) {
  1108. Transform3D local_matrix = p_render_data->inv_cam_transform * light_storage->reflection_probe_instance_get_transform(probe_instance);
  1109. inst->reflection_probes_local_transform_cache.push_back(local_matrix.affine_inverse());
  1110. inst->reflection_probe_rid_cache.push_back(probe_instance);
  1111. }
  1112. }
  1113. }
  1114. }
  1115. inst->flags_cache = flags;
  1116. GeometryInstanceSurface *surf = inst->surface_caches;
  1117. float lod_distance = 0.0;
  1118. if (p_render_data->cam_orthogonal) {
  1119. lod_distance = 1.0;
  1120. } else {
  1121. Vector3 aabb_min = inst->transformed_aabb.position;
  1122. Vector3 aabb_max = inst->transformed_aabb.position + inst->transformed_aabb.size;
  1123. Vector3 camera_position = p_render_data->main_cam_transform.origin;
  1124. Vector3 surface_distance = Vector3(0.0, 0.0, 0.0).max(aabb_min - camera_position).max(camera_position - aabb_max);
  1125. lod_distance = surface_distance.length();
  1126. }
  1127. while (surf) {
  1128. // LOD
  1129. if (p_render_data->screen_mesh_lod_threshold > 0.0 && mesh_storage->mesh_surface_has_lod(surf->surface)) {
  1130. uint32_t indices = 0;
  1131. surf->lod_index = mesh_storage->mesh_surface_get_lod(surf->surface, inst->lod_model_scale * inst->lod_bias, lod_distance * p_render_data->lod_distance_multiplier, p_render_data->screen_mesh_lod_threshold, indices);
  1132. surf->index_count = indices;
  1133. if (p_render_data->render_info) {
  1134. indices = _indices_to_primitives(surf->primitive, indices);
  1135. if (p_render_list == RENDER_LIST_OPAQUE) { //opaque
  1136. p_render_data->render_info->info[RS::VIEWPORT_RENDER_INFO_TYPE_VISIBLE][RS::VIEWPORT_RENDER_INFO_PRIMITIVES_IN_FRAME] += indices;
  1137. } else if (p_render_list == RENDER_LIST_SECONDARY) { //shadow
  1138. p_render_data->render_info->info[RS::VIEWPORT_RENDER_INFO_TYPE_SHADOW][RS::VIEWPORT_RENDER_INFO_PRIMITIVES_IN_FRAME] += indices;
  1139. }
  1140. }
  1141. } else {
  1142. surf->lod_index = 0;
  1143. if (p_render_data->render_info) {
  1144. uint32_t to_draw = mesh_storage->mesh_surface_get_vertices_drawn_count(surf->surface);
  1145. to_draw = _indices_to_primitives(surf->primitive, to_draw);
  1146. to_draw *= inst->instance_count > 0 ? inst->instance_count : 1;
  1147. if (p_render_list == RENDER_LIST_OPAQUE) { //opaque
  1148. p_render_data->render_info->info[RS::VIEWPORT_RENDER_INFO_TYPE_VISIBLE][RS::VIEWPORT_RENDER_INFO_PRIMITIVES_IN_FRAME] += to_draw;
  1149. } else if (p_render_list == RENDER_LIST_SECONDARY) { //shadow
  1150. p_render_data->render_info->info[RS::VIEWPORT_RENDER_INFO_TYPE_SHADOW][RS::VIEWPORT_RENDER_INFO_PRIMITIVES_IN_FRAME] += to_draw;
  1151. }
  1152. }
  1153. }
  1154. // ADD Element
  1155. if (p_pass_mode == PASS_MODE_COLOR) {
  1156. #ifdef DEBUG_ENABLED
  1157. bool force_alpha = unlikely(get_debug_draw_mode() == RS::VIEWPORT_DEBUG_DRAW_OVERDRAW);
  1158. #else
  1159. bool force_alpha = false;
  1160. #endif
  1161. if (!force_alpha && (surf->flags & (GeometryInstanceSurface::FLAG_PASS_DEPTH | GeometryInstanceSurface::FLAG_PASS_OPAQUE))) {
  1162. rl->add_element(surf);
  1163. }
  1164. if (force_alpha || (surf->flags & GeometryInstanceSurface::FLAG_PASS_ALPHA)) {
  1165. render_list[RENDER_LIST_ALPHA].add_element(surf);
  1166. }
  1167. if (surf->flags & GeometryInstanceSurface::FLAG_USES_SCREEN_TEXTURE) {
  1168. scene_state.used_screen_texture = true;
  1169. }
  1170. if (surf->flags & GeometryInstanceSurface::FLAG_USES_NORMAL_TEXTURE) {
  1171. scene_state.used_normal_texture = true;
  1172. }
  1173. if (surf->flags & GeometryInstanceSurface::FLAG_USES_DEPTH_TEXTURE) {
  1174. scene_state.used_depth_texture = true;
  1175. }
  1176. if ((surf->flags & GeometryInstanceSurface::FLAG_USES_STENCIL) && !force_alpha && (surf->flags & (GeometryInstanceSurface::FLAG_PASS_DEPTH | GeometryInstanceSurface::FLAG_PASS_OPAQUE))) {
  1177. scene_state.used_opaque_stencil = true;
  1178. }
  1179. } else if (p_pass_mode == PASS_MODE_SHADOW) {
  1180. if (surf->flags & GeometryInstanceSurface::FLAG_PASS_SHADOW) {
  1181. rl->add_element(surf);
  1182. }
  1183. } else if (p_pass_mode == PASS_MODE_MATERIAL) {
  1184. if (surf->flags & (GeometryInstanceSurface::FLAG_PASS_DEPTH | GeometryInstanceSurface::FLAG_PASS_OPAQUE | GeometryInstanceSurface::FLAG_PASS_ALPHA)) {
  1185. rl->add_element(surf);
  1186. }
  1187. } else {
  1188. if (surf->flags & (GeometryInstanceSurface::FLAG_PASS_DEPTH | GeometryInstanceSurface::FLAG_PASS_OPAQUE)) {
  1189. rl->add_element(surf);
  1190. }
  1191. }
  1192. surf->sort.depth_layer = depth_layer;
  1193. surf->finished_base_pass = false;
  1194. surf->light_pass_index = 0;
  1195. surf = surf->next;
  1196. }
  1197. }
  1198. }
  1199. // Needs to be called after _setup_lights so that directional_light_count is accurate.
  1200. void RasterizerSceneGLES3::_setup_environment(const RenderDataGLES3 *p_render_data, bool p_no_fog, const Size2i &p_screen_size, bool p_flip_y, const Color &p_default_bg_color, bool p_pancake_shadows, float p_shadow_bias) {
  1201. Projection correction;
  1202. correction.set_depth_correction(p_flip_y, true, false);
  1203. Projection projection = correction * p_render_data->cam_projection;
  1204. //store camera into ubo
  1205. GLES3::MaterialStorage::store_camera(projection, scene_state.ubo.projection_matrix);
  1206. GLES3::MaterialStorage::store_camera(projection.inverse(), scene_state.ubo.inv_projection_matrix);
  1207. GLES3::MaterialStorage::store_transform(p_render_data->cam_transform, scene_state.ubo.inv_view_matrix);
  1208. GLES3::MaterialStorage::store_transform(p_render_data->inv_cam_transform, scene_state.ubo.view_matrix);
  1209. GLES3::MaterialStorage::store_transform(p_render_data->main_cam_transform, scene_state.ubo.main_cam_inv_view_matrix);
  1210. scene_state.ubo.camera_visible_layers = p_render_data->camera_visible_layers;
  1211. if (p_render_data->view_count > 1) {
  1212. for (uint32_t v = 0; v < p_render_data->view_count; v++) {
  1213. projection = correction * p_render_data->view_projection[v];
  1214. GLES3::MaterialStorage::store_camera(projection, scene_state.multiview_ubo.projection_matrix_view[v]);
  1215. GLES3::MaterialStorage::store_camera(projection.inverse(), scene_state.multiview_ubo.inv_projection_matrix_view[v]);
  1216. scene_state.multiview_ubo.eye_offset[v][0] = p_render_data->view_eye_offset[v].x;
  1217. scene_state.multiview_ubo.eye_offset[v][1] = p_render_data->view_eye_offset[v].y;
  1218. scene_state.multiview_ubo.eye_offset[v][2] = p_render_data->view_eye_offset[v].z;
  1219. scene_state.multiview_ubo.eye_offset[v][3] = 0.0;
  1220. }
  1221. }
  1222. // Only render the lights without shadows in the base pass.
  1223. scene_state.ubo.directional_light_count = p_render_data->directional_light_count - p_render_data->directional_shadow_count;
  1224. scene_state.ubo.z_far = p_render_data->z_far;
  1225. scene_state.ubo.z_near = p_render_data->z_near;
  1226. scene_state.ubo.viewport_size[0] = p_screen_size.x;
  1227. scene_state.ubo.viewport_size[1] = p_screen_size.y;
  1228. Size2 screen_pixel_size = Vector2(1.0, 1.0) / Size2(p_screen_size);
  1229. scene_state.ubo.screen_pixel_size[0] = screen_pixel_size.x;
  1230. scene_state.ubo.screen_pixel_size[1] = screen_pixel_size.y;
  1231. scene_state.ubo.luminance_multiplier = p_render_data->luminance_multiplier;
  1232. scene_state.ubo.shadow_bias = p_shadow_bias;
  1233. scene_state.ubo.pancake_shadows = p_pancake_shadows;
  1234. //time global variables
  1235. scene_state.ubo.time = time;
  1236. if (get_debug_draw_mode() == RS::VIEWPORT_DEBUG_DRAW_UNSHADED) {
  1237. scene_state.ubo.use_ambient_light = true;
  1238. scene_state.ubo.ambient_light_color_energy[0] = 1;
  1239. scene_state.ubo.ambient_light_color_energy[1] = 1;
  1240. scene_state.ubo.ambient_light_color_energy[2] = 1;
  1241. scene_state.ubo.ambient_light_color_energy[3] = 1.0;
  1242. scene_state.ubo.use_ambient_cubemap = false;
  1243. scene_state.ubo.use_reflection_cubemap = false;
  1244. } else if (is_environment(p_render_data->environment)) {
  1245. RS::EnvironmentBG env_bg = environment_get_background(p_render_data->environment);
  1246. RS::EnvironmentAmbientSource ambient_src = environment_get_ambient_source(p_render_data->environment);
  1247. float bg_energy_multiplier = environment_get_bg_energy_multiplier(p_render_data->environment);
  1248. scene_state.ubo.ambient_light_color_energy[3] = bg_energy_multiplier;
  1249. scene_state.ubo.ambient_color_sky_mix = environment_get_ambient_sky_contribution(p_render_data->environment);
  1250. //ambient
  1251. if (ambient_src == RS::ENV_AMBIENT_SOURCE_BG && (env_bg == RS::ENV_BG_CLEAR_COLOR || env_bg == RS::ENV_BG_COLOR)) {
  1252. Color color = env_bg == RS::ENV_BG_CLEAR_COLOR ? p_default_bg_color : environment_get_bg_color(p_render_data->environment);
  1253. color = color.srgb_to_linear();
  1254. scene_state.ubo.ambient_light_color_energy[0] = color.r * bg_energy_multiplier;
  1255. scene_state.ubo.ambient_light_color_energy[1] = color.g * bg_energy_multiplier;
  1256. scene_state.ubo.ambient_light_color_energy[2] = color.b * bg_energy_multiplier;
  1257. scene_state.ubo.use_ambient_light = true;
  1258. scene_state.ubo.use_ambient_cubemap = false;
  1259. } else {
  1260. float energy = environment_get_ambient_light_energy(p_render_data->environment);
  1261. Color color = environment_get_ambient_light(p_render_data->environment);
  1262. color = color.srgb_to_linear();
  1263. scene_state.ubo.ambient_light_color_energy[0] = color.r * energy;
  1264. scene_state.ubo.ambient_light_color_energy[1] = color.g * energy;
  1265. scene_state.ubo.ambient_light_color_energy[2] = color.b * energy;
  1266. Basis sky_transform = environment_get_sky_orientation(p_render_data->environment);
  1267. sky_transform = sky_transform.inverse() * p_render_data->cam_transform.basis;
  1268. GLES3::MaterialStorage::store_transform_3x3(sky_transform, scene_state.ubo.radiance_inverse_xform);
  1269. scene_state.ubo.use_ambient_cubemap = (ambient_src == RS::ENV_AMBIENT_SOURCE_BG && env_bg == RS::ENV_BG_SKY) || ambient_src == RS::ENV_AMBIENT_SOURCE_SKY;
  1270. scene_state.ubo.use_ambient_light = scene_state.ubo.use_ambient_cubemap || ambient_src == RS::ENV_AMBIENT_SOURCE_COLOR;
  1271. }
  1272. //specular
  1273. RS::EnvironmentReflectionSource ref_src = environment_get_reflection_source(p_render_data->environment);
  1274. if ((ref_src == RS::ENV_REFLECTION_SOURCE_BG && env_bg == RS::ENV_BG_SKY) || ref_src == RS::ENV_REFLECTION_SOURCE_SKY) {
  1275. scene_state.ubo.use_reflection_cubemap = true;
  1276. } else {
  1277. scene_state.ubo.use_reflection_cubemap = false;
  1278. }
  1279. scene_state.ubo.fog_enabled = environment_get_fog_enabled(p_render_data->environment);
  1280. scene_state.ubo.fog_mode = environment_get_fog_mode(p_render_data->environment);
  1281. scene_state.ubo.fog_density = environment_get_fog_density(p_render_data->environment);
  1282. scene_state.ubo.fog_height = environment_get_fog_height(p_render_data->environment);
  1283. scene_state.ubo.fog_depth_curve = environment_get_fog_depth_curve(p_render_data->environment);
  1284. scene_state.ubo.fog_depth_end = environment_get_fog_depth_end(p_render_data->environment) > 0.0 ? environment_get_fog_depth_end(p_render_data->environment) : scene_state.ubo.z_far;
  1285. scene_state.ubo.fog_depth_begin = MIN(environment_get_fog_depth_begin(p_render_data->environment), scene_state.ubo.fog_depth_end - 0.001);
  1286. scene_state.ubo.fog_height_density = environment_get_fog_height_density(p_render_data->environment);
  1287. scene_state.ubo.fog_aerial_perspective = environment_get_fog_aerial_perspective(p_render_data->environment);
  1288. Color fog_color = environment_get_fog_light_color(p_render_data->environment).srgb_to_linear();
  1289. float fog_energy = environment_get_fog_light_energy(p_render_data->environment);
  1290. scene_state.ubo.fog_light_color[0] = fog_color.r * fog_energy;
  1291. scene_state.ubo.fog_light_color[1] = fog_color.g * fog_energy;
  1292. scene_state.ubo.fog_light_color[2] = fog_color.b * fog_energy;
  1293. scene_state.ubo.fog_sun_scatter = environment_get_fog_sun_scatter(p_render_data->environment);
  1294. } else {
  1295. }
  1296. if (p_render_data->camera_attributes.is_valid()) {
  1297. scene_state.ubo.emissive_exposure_normalization = RSG::camera_attributes->camera_attributes_get_exposure_normalization_factor(p_render_data->camera_attributes);
  1298. scene_state.ubo.IBL_exposure_normalization = 1.0;
  1299. if (is_environment(p_render_data->environment)) {
  1300. RID sky_rid = environment_get_sky(p_render_data->environment);
  1301. if (sky_rid.is_valid()) {
  1302. float current_exposure = RSG::camera_attributes->camera_attributes_get_exposure_normalization_factor(p_render_data->camera_attributes) * environment_get_bg_intensity(p_render_data->environment);
  1303. scene_state.ubo.IBL_exposure_normalization = current_exposure / MAX(0.001, sky_get_baked_exposure(sky_rid));
  1304. }
  1305. }
  1306. } else if (scene_state.ubo.emissive_exposure_normalization > 0.0) {
  1307. // This branch is triggered when using render_material().
  1308. // Emissive is set outside the function, so don't set it.
  1309. // IBL isn't used don't set it.
  1310. } else {
  1311. scene_state.ubo.emissive_exposure_normalization = 1.0;
  1312. scene_state.ubo.IBL_exposure_normalization = 1.0;
  1313. }
  1314. if (scene_state.ubo_buffer == 0) {
  1315. glGenBuffers(1, &scene_state.ubo_buffer);
  1316. glBindBufferBase(GL_UNIFORM_BUFFER, SCENE_DATA_UNIFORM_LOCATION, scene_state.ubo_buffer);
  1317. GLES3::Utilities::get_singleton()->buffer_allocate_data(GL_UNIFORM_BUFFER, scene_state.ubo_buffer, sizeof(SceneState::UBO), &scene_state.ubo, GL_STREAM_DRAW, "Scene state UBO");
  1318. glBindBuffer(GL_UNIFORM_BUFFER, 0);
  1319. } else {
  1320. glBindBufferBase(GL_UNIFORM_BUFFER, SCENE_DATA_UNIFORM_LOCATION, scene_state.ubo_buffer);
  1321. glBufferData(GL_UNIFORM_BUFFER, sizeof(SceneState::UBO), &scene_state.ubo, GL_STREAM_DRAW);
  1322. }
  1323. glBindBuffer(GL_UNIFORM_BUFFER, 0);
  1324. if (p_render_data->view_count > 1) {
  1325. if (scene_state.multiview_buffer == 0) {
  1326. glGenBuffers(1, &scene_state.multiview_buffer);
  1327. glBindBufferBase(GL_UNIFORM_BUFFER, SCENE_MULTIVIEW_UNIFORM_LOCATION, scene_state.multiview_buffer);
  1328. GLES3::Utilities::get_singleton()->buffer_allocate_data(GL_UNIFORM_BUFFER, scene_state.multiview_buffer, sizeof(SceneState::MultiviewUBO), &scene_state.multiview_ubo, GL_STREAM_DRAW, "Multiview UBO");
  1329. } else {
  1330. glBindBufferBase(GL_UNIFORM_BUFFER, SCENE_MULTIVIEW_UNIFORM_LOCATION, scene_state.multiview_buffer);
  1331. glBufferData(GL_UNIFORM_BUFFER, sizeof(SceneState::MultiviewUBO), &scene_state.multiview_ubo, GL_STREAM_DRAW);
  1332. }
  1333. glBindBuffer(GL_UNIFORM_BUFFER, 0);
  1334. }
  1335. }
  1336. // Puts lights into Uniform Buffers. Needs to be called before _fill_list as this caches the index of each light in the Uniform Buffer
  1337. void RasterizerSceneGLES3::_setup_lights(const RenderDataGLES3 *p_render_data, bool p_using_shadows, uint32_t &r_directional_light_count, uint32_t &r_omni_light_count, uint32_t &r_spot_light_count, uint32_t &r_directional_shadow_count) {
  1338. GLES3::LightStorage *light_storage = GLES3::LightStorage::get_singleton();
  1339. GLES3::Config *config = GLES3::Config::get_singleton();
  1340. const Transform3D inverse_transform = p_render_data->inv_cam_transform;
  1341. const PagedArray<RID> &lights = *p_render_data->lights;
  1342. r_directional_light_count = 0;
  1343. r_omni_light_count = 0;
  1344. r_spot_light_count = 0;
  1345. r_directional_shadow_count = 0;
  1346. int num_lights = lights.size();
  1347. for (int i = 0; i < num_lights; i++) {
  1348. GLES3::LightInstance *li = GLES3::LightStorage::get_singleton()->get_light_instance(lights[i]);
  1349. if (!li) {
  1350. continue;
  1351. }
  1352. RID base = li->light;
  1353. ERR_CONTINUE(base.is_null());
  1354. RS::LightType type = light_storage->light_get_type(base);
  1355. switch (type) {
  1356. case RS::LIGHT_DIRECTIONAL: {
  1357. if (r_directional_light_count >= RendererSceneRender::MAX_DIRECTIONAL_LIGHTS || light_storage->light_directional_get_sky_mode(base) == RS::LIGHT_DIRECTIONAL_SKY_MODE_SKY_ONLY) {
  1358. continue;
  1359. }
  1360. // If a DirectionalLight has shadows, we will add it to the end of the array and work in.
  1361. bool has_shadow = light_storage->light_has_shadow(base);
  1362. int index = r_directional_light_count - r_directional_shadow_count;
  1363. if (has_shadow) {
  1364. // Lights with shadow are incremented from the end of the array.
  1365. index = MAX_DIRECTIONAL_LIGHTS - 1 - r_directional_shadow_count;
  1366. }
  1367. DirectionalLightData &light_data = scene_state.directional_lights[index];
  1368. Transform3D light_transform = li->transform;
  1369. Vector3 direction = inverse_transform.basis.xform(light_transform.basis.xform(Vector3(0, 0, 1))).normalized();
  1370. light_data.direction[0] = direction.x;
  1371. light_data.direction[1] = direction.y;
  1372. light_data.direction[2] = direction.z;
  1373. light_data.bake_mode = light_storage->light_get_bake_mode(base);
  1374. float sign = light_storage->light_is_negative(base) ? -1 : 1;
  1375. light_data.energy = sign * light_storage->light_get_param(base, RS::LIGHT_PARAM_ENERGY);
  1376. if (is_using_physical_light_units()) {
  1377. light_data.energy *= light_storage->light_get_param(base, RS::LIGHT_PARAM_INTENSITY);
  1378. } else {
  1379. light_data.energy *= Math::PI;
  1380. }
  1381. if (p_render_data->camera_attributes.is_valid()) {
  1382. light_data.energy *= RSG::camera_attributes->camera_attributes_get_exposure_normalization_factor(p_render_data->camera_attributes);
  1383. }
  1384. Color linear_col = light_storage->light_get_color(base).srgb_to_linear();
  1385. light_data.color[0] = linear_col.r;
  1386. light_data.color[1] = linear_col.g;
  1387. light_data.color[2] = linear_col.b;
  1388. float size = light_storage->light_get_param(base, RS::LIGHT_PARAM_SIZE);
  1389. light_data.size = 1.0 - Math::cos(Math::deg_to_rad(size)); //angle to cosine offset
  1390. light_data.specular = light_storage->light_get_param(base, RS::LIGHT_PARAM_SPECULAR);
  1391. light_data.shadow_opacity = (p_using_shadows && light_storage->light_has_shadow(base))
  1392. ? light_storage->light_get_param(base, RS::LIGHT_PARAM_SHADOW_OPACITY)
  1393. : 0.0;
  1394. if (has_shadow) {
  1395. DirectionalShadowData &shadow_data = scene_state.directional_shadows[MAX_DIRECTIONAL_LIGHTS - 1 - r_directional_shadow_count];
  1396. RS::LightDirectionalShadowMode shadow_mode = light_storage->light_directional_get_shadow_mode(base);
  1397. int limit = shadow_mode == RS::LIGHT_DIRECTIONAL_SHADOW_ORTHOGONAL ? 0 : (shadow_mode == RS::LIGHT_DIRECTIONAL_SHADOW_PARALLEL_2_SPLITS ? 1 : 3);
  1398. shadow_data.shadow_atlas_pixel_size = 1.0 / light_storage->directional_shadow_get_size();
  1399. shadow_data.blend_splits = uint32_t((shadow_mode != RS::LIGHT_DIRECTIONAL_SHADOW_ORTHOGONAL) && light_storage->light_directional_get_blend_splits(base));
  1400. for (int j = 0; j < 4; j++) {
  1401. Rect2 atlas_rect = li->shadow_transform[j].atlas_rect;
  1402. Projection correction;
  1403. correction.set_depth_correction(false, true, false);
  1404. Projection matrix = correction * li->shadow_transform[j].camera;
  1405. float split = li->shadow_transform[MIN(limit, j)].split;
  1406. Projection bias;
  1407. bias.set_light_bias();
  1408. Projection rectm;
  1409. rectm.set_light_atlas_rect(atlas_rect);
  1410. Transform3D modelview = (inverse_transform * li->shadow_transform[j].transform).inverse();
  1411. shadow_data.direction[0] = light_data.direction[0];
  1412. shadow_data.direction[1] = light_data.direction[1];
  1413. shadow_data.direction[2] = light_data.direction[2];
  1414. Projection shadow_mtx = rectm * bias * matrix * modelview;
  1415. shadow_data.shadow_split_offsets[j] = split;
  1416. shadow_data.shadow_normal_bias[j] = light_storage->light_get_param(base, RS::LIGHT_PARAM_SHADOW_NORMAL_BIAS) * li->shadow_transform[j].shadow_texel_size;
  1417. GLES3::MaterialStorage::store_camera(shadow_mtx, shadow_data.shadow_matrices[j]);
  1418. }
  1419. float fade_start = light_storage->light_get_param(base, RS::LIGHT_PARAM_SHADOW_FADE_START);
  1420. shadow_data.fade_from = -shadow_data.shadow_split_offsets[3] * MIN(fade_start, 0.999);
  1421. shadow_data.fade_to = -shadow_data.shadow_split_offsets[3];
  1422. r_directional_shadow_count++;
  1423. }
  1424. r_directional_light_count++;
  1425. } break;
  1426. case RS::LIGHT_OMNI: {
  1427. if (r_omni_light_count >= (uint32_t)config->max_renderable_lights) {
  1428. continue;
  1429. }
  1430. const real_t distance = p_render_data->cam_transform.origin.distance_to(li->transform.origin);
  1431. if (light_storage->light_is_distance_fade_enabled(li->light)) {
  1432. const float fade_begin = light_storage->light_get_distance_fade_begin(li->light);
  1433. const float fade_length = light_storage->light_get_distance_fade_length(li->light);
  1434. if (distance > fade_begin) {
  1435. if (distance > fade_begin + fade_length) {
  1436. // Out of range, don't draw this light to improve performance.
  1437. continue;
  1438. }
  1439. }
  1440. }
  1441. scene_state.omni_light_sort[r_omni_light_count].instance = li;
  1442. scene_state.omni_light_sort[r_omni_light_count].depth = distance;
  1443. r_omni_light_count++;
  1444. } break;
  1445. case RS::LIGHT_SPOT: {
  1446. if (r_spot_light_count >= (uint32_t)config->max_renderable_lights) {
  1447. continue;
  1448. }
  1449. const real_t distance = p_render_data->cam_transform.origin.distance_to(li->transform.origin);
  1450. if (light_storage->light_is_distance_fade_enabled(li->light)) {
  1451. const float fade_begin = light_storage->light_get_distance_fade_begin(li->light);
  1452. const float fade_length = light_storage->light_get_distance_fade_length(li->light);
  1453. if (distance > fade_begin) {
  1454. if (distance > fade_begin + fade_length) {
  1455. // Out of range, don't draw this light to improve performance.
  1456. continue;
  1457. }
  1458. }
  1459. }
  1460. scene_state.spot_light_sort[r_spot_light_count].instance = li;
  1461. scene_state.spot_light_sort[r_spot_light_count].depth = distance;
  1462. r_spot_light_count++;
  1463. } break;
  1464. }
  1465. li->last_pass = RSG::rasterizer->get_frame_number();
  1466. }
  1467. if (r_omni_light_count) {
  1468. SortArray<InstanceSort<GLES3::LightInstance>> sorter;
  1469. sorter.sort(scene_state.omni_light_sort, r_omni_light_count);
  1470. }
  1471. if (r_spot_light_count) {
  1472. SortArray<InstanceSort<GLES3::LightInstance>> sorter;
  1473. sorter.sort(scene_state.spot_light_sort, r_spot_light_count);
  1474. }
  1475. int num_positional_shadows = 0;
  1476. for (uint32_t i = 0; i < (r_omni_light_count + r_spot_light_count); i++) {
  1477. uint32_t index = (i < r_omni_light_count) ? i : i - (r_omni_light_count);
  1478. LightData &light_data = (i < r_omni_light_count) ? scene_state.omni_lights[index] : scene_state.spot_lights[index];
  1479. RS::LightType type = (i < r_omni_light_count) ? RS::LIGHT_OMNI : RS::LIGHT_SPOT;
  1480. GLES3::LightInstance *li = (i < r_omni_light_count) ? scene_state.omni_light_sort[index].instance : scene_state.spot_light_sort[index].instance;
  1481. real_t distance = (i < r_omni_light_count) ? scene_state.omni_light_sort[index].depth : scene_state.spot_light_sort[index].depth;
  1482. RID base = li->light;
  1483. li->gl_id = index;
  1484. Transform3D light_transform = li->transform;
  1485. Vector3 pos = inverse_transform.xform(light_transform.origin);
  1486. light_data.position[0] = pos.x;
  1487. light_data.position[1] = pos.y;
  1488. light_data.position[2] = pos.z;
  1489. light_data.bake_mode = light_storage->light_get_bake_mode(base);
  1490. float radius = MAX(0.001, light_storage->light_get_param(base, RS::LIGHT_PARAM_RANGE));
  1491. light_data.inv_radius = 1.0 / radius;
  1492. Vector3 direction = inverse_transform.basis.xform(light_transform.basis.xform(Vector3(0, 0, -1))).normalized();
  1493. light_data.direction[0] = direction.x;
  1494. light_data.direction[1] = direction.y;
  1495. light_data.direction[2] = direction.z;
  1496. float size = light_storage->light_get_param(base, RS::LIGHT_PARAM_SIZE);
  1497. light_data.size = size;
  1498. float sign = light_storage->light_is_negative(base) ? -1 : 1;
  1499. Color linear_col = light_storage->light_get_color(base).srgb_to_linear();
  1500. // Reuse fade begin, fade length and distance for shadow LOD determination later.
  1501. float fade_begin = 0.0;
  1502. float fade_shadow = 0.0;
  1503. float fade_length = 0.0;
  1504. float fade = 1.0;
  1505. float shadow_opacity_fade = 1.0;
  1506. if (light_storage->light_is_distance_fade_enabled(base)) {
  1507. fade_begin = light_storage->light_get_distance_fade_begin(base);
  1508. fade_shadow = light_storage->light_get_distance_fade_shadow(base);
  1509. fade_length = light_storage->light_get_distance_fade_length(base);
  1510. if (distance > fade_begin) {
  1511. // Use `smoothstep()` to make opacity changes more gradual and less noticeable to the player.
  1512. fade = Math::smoothstep(0.0f, 1.0f, 1.0f - float(distance - fade_begin) / fade_length);
  1513. }
  1514. if (distance > fade_shadow) {
  1515. shadow_opacity_fade = Math::smoothstep(0.0f, 1.0f, 1.0f - float(distance - fade_shadow) / fade_length);
  1516. }
  1517. }
  1518. float energy = sign * light_storage->light_get_param(base, RS::LIGHT_PARAM_ENERGY) * fade;
  1519. if (is_using_physical_light_units()) {
  1520. energy *= light_storage->light_get_param(base, RS::LIGHT_PARAM_INTENSITY);
  1521. // Convert from Luminous Power to Luminous Intensity
  1522. if (type == RS::LIGHT_OMNI) {
  1523. energy *= 1.0 / (Math::PI * 4.0);
  1524. } else {
  1525. // Spot Lights are not physically accurate, Luminous Intensity should change in relation to the cone angle.
  1526. // We make this assumption to keep them easy to control.
  1527. energy *= 1.0 / Math::PI;
  1528. }
  1529. } else {
  1530. energy *= Math::PI;
  1531. }
  1532. if (p_render_data->camera_attributes.is_valid()) {
  1533. energy *= RSG::camera_attributes->camera_attributes_get_exposure_normalization_factor(p_render_data->camera_attributes);
  1534. }
  1535. light_data.color[0] = linear_col.r * energy;
  1536. light_data.color[1] = linear_col.g * energy;
  1537. light_data.color[2] = linear_col.b * energy;
  1538. light_data.attenuation = light_storage->light_get_param(base, RS::LIGHT_PARAM_ATTENUATION);
  1539. light_data.inv_spot_attenuation = 1.0f / light_storage->light_get_param(base, RS::LIGHT_PARAM_SPOT_ATTENUATION);
  1540. float spot_angle = light_storage->light_get_param(base, RS::LIGHT_PARAM_SPOT_ANGLE);
  1541. light_data.cos_spot_angle = Math::cos(Math::deg_to_rad(spot_angle));
  1542. light_data.specular_amount = light_storage->light_get_param(base, RS::LIGHT_PARAM_SPECULAR) * 2.0;
  1543. // Setup shadows
  1544. const bool needs_shadow =
  1545. p_using_shadows &&
  1546. light_storage->owns_shadow_atlas(p_render_data->shadow_atlas) &&
  1547. light_storage->shadow_atlas_owns_light_instance(p_render_data->shadow_atlas, li->self) &&
  1548. light_storage->light_has_shadow(base);
  1549. bool in_shadow_range = true;
  1550. if (needs_shadow && light_storage->light_is_distance_fade_enabled(base)) {
  1551. if (distance > fade_shadow + fade_length) {
  1552. // Out of range, don't draw shadows to improve performance.
  1553. in_shadow_range = false;
  1554. }
  1555. }
  1556. // Fill in the shadow information.
  1557. if (needs_shadow && in_shadow_range) {
  1558. if (num_positional_shadows >= config->max_renderable_lights) {
  1559. continue;
  1560. }
  1561. ShadowData &shadow_data = scene_state.positional_shadows[num_positional_shadows];
  1562. li->shadow_id = num_positional_shadows;
  1563. num_positional_shadows++;
  1564. light_data.shadow_opacity = light_storage->light_get_param(base, RS::LIGHT_PARAM_SHADOW_OPACITY) * shadow_opacity_fade;
  1565. float shadow_texel_size = light_storage->light_instance_get_shadow_texel_size(li->self, p_render_data->shadow_atlas);
  1566. shadow_data.shadow_atlas_pixel_size = shadow_texel_size;
  1567. shadow_data.shadow_normal_bias = light_storage->light_get_param(base, RS::LIGHT_PARAM_SHADOW_NORMAL_BIAS) * shadow_texel_size * 10.0;
  1568. shadow_data.light_position[0] = light_data.position[0];
  1569. shadow_data.light_position[1] = light_data.position[1];
  1570. shadow_data.light_position[2] = light_data.position[2];
  1571. if (type == RS::LIGHT_OMNI) {
  1572. Transform3D proj = (inverse_transform * light_transform).inverse();
  1573. GLES3::MaterialStorage::store_transform(proj, shadow_data.shadow_matrix);
  1574. } else if (type == RS::LIGHT_SPOT) {
  1575. Transform3D modelview = (inverse_transform * light_transform).inverse();
  1576. Projection bias;
  1577. bias.set_light_bias();
  1578. Projection correction;
  1579. correction.set_depth_correction(false, true, false);
  1580. Projection cm = correction * li->shadow_transform[0].camera;
  1581. Projection shadow_mtx = bias * cm * modelview;
  1582. GLES3::MaterialStorage::store_camera(shadow_mtx, shadow_data.shadow_matrix);
  1583. }
  1584. }
  1585. }
  1586. // TODO, to avoid stalls, should rotate between 3 buffers based on frame index.
  1587. // TODO, consider mapping the buffer as in 2D
  1588. glBindBufferBase(GL_UNIFORM_BUFFER, SCENE_OMNILIGHT_UNIFORM_LOCATION, scene_state.omni_light_buffer);
  1589. if (r_omni_light_count) {
  1590. glBufferSubData(GL_UNIFORM_BUFFER, 0, sizeof(LightData) * r_omni_light_count, scene_state.omni_lights);
  1591. }
  1592. glBindBufferBase(GL_UNIFORM_BUFFER, SCENE_SPOTLIGHT_UNIFORM_LOCATION, scene_state.spot_light_buffer);
  1593. if (r_spot_light_count) {
  1594. glBufferSubData(GL_UNIFORM_BUFFER, 0, sizeof(LightData) * r_spot_light_count, scene_state.spot_lights);
  1595. }
  1596. glBindBufferBase(GL_UNIFORM_BUFFER, SCENE_DIRECTIONAL_LIGHT_UNIFORM_LOCATION, scene_state.directional_light_buffer);
  1597. if (r_directional_light_count) {
  1598. glBufferData(GL_UNIFORM_BUFFER, sizeof(DirectionalLightData) * MAX_DIRECTIONAL_LIGHTS, scene_state.directional_lights, GL_STREAM_DRAW);
  1599. }
  1600. glBindBufferBase(GL_UNIFORM_BUFFER, SCENE_POSITIONAL_SHADOW_UNIFORM_LOCATION, scene_state.positional_shadow_buffer);
  1601. if (num_positional_shadows) {
  1602. glBufferSubData(GL_UNIFORM_BUFFER, 0, sizeof(ShadowData) * num_positional_shadows, scene_state.positional_shadows);
  1603. }
  1604. glBindBufferBase(GL_UNIFORM_BUFFER, SCENE_DIRECTIONAL_SHADOW_UNIFORM_LOCATION, scene_state.directional_shadow_buffer);
  1605. if (r_directional_shadow_count) {
  1606. glBufferData(GL_UNIFORM_BUFFER, sizeof(DirectionalShadowData) * MAX_DIRECTIONAL_LIGHTS, scene_state.directional_shadows, GL_STREAM_DRAW);
  1607. }
  1608. glBindBuffer(GL_UNIFORM_BUFFER, 0);
  1609. }
  1610. // Render shadows
  1611. void RasterizerSceneGLES3::_render_shadows(const RenderDataGLES3 *p_render_data, const Size2i &p_viewport_size) {
  1612. GLES3::LightStorage *light_storage = GLES3::LightStorage::get_singleton();
  1613. LocalVector<int> cube_shadows;
  1614. LocalVector<int> shadows;
  1615. LocalVector<int> directional_shadows;
  1616. float lod_distance_multiplier = p_render_data->cam_projection.get_lod_multiplier();
  1617. // Put lights into buckets for omni (cube shadows), directional, and spot.
  1618. {
  1619. for (int i = 0; i < p_render_data->render_shadow_count; i++) {
  1620. RID li = p_render_data->render_shadows[i].light;
  1621. RID base = light_storage->light_instance_get_base_light(li);
  1622. if (light_storage->light_get_type(base) == RS::LIGHT_DIRECTIONAL) {
  1623. directional_shadows.push_back(i);
  1624. } else if (light_storage->light_get_type(base) == RS::LIGHT_OMNI && light_storage->light_omni_get_shadow_mode(base) == RS::LIGHT_OMNI_SHADOW_CUBE) {
  1625. cube_shadows.push_back(i);
  1626. } else {
  1627. shadows.push_back(i);
  1628. }
  1629. }
  1630. if (directional_shadows.size()) {
  1631. light_storage->update_directional_shadow_atlas();
  1632. }
  1633. }
  1634. bool render_shadows = directional_shadows.size() || shadows.size() || cube_shadows.size();
  1635. if (render_shadows) {
  1636. RENDER_TIMESTAMP("Render Shadows");
  1637. // Render cubemap shadows.
  1638. for (const int &index : cube_shadows) {
  1639. _render_shadow_pass(p_render_data->render_shadows[index].light, p_render_data->shadow_atlas, p_render_data->render_shadows[index].pass, p_render_data->render_shadows[index].instances, lod_distance_multiplier, p_render_data->screen_mesh_lod_threshold, p_render_data->render_info, p_viewport_size, p_render_data->cam_transform);
  1640. }
  1641. // Render directional shadows.
  1642. for (uint32_t i = 0; i < directional_shadows.size(); i++) {
  1643. _render_shadow_pass(p_render_data->render_shadows[directional_shadows[i]].light, p_render_data->shadow_atlas, p_render_data->render_shadows[directional_shadows[i]].pass, p_render_data->render_shadows[directional_shadows[i]].instances, lod_distance_multiplier, p_render_data->screen_mesh_lod_threshold, p_render_data->render_info, p_viewport_size, p_render_data->cam_transform);
  1644. }
  1645. // Render positional shadows (Spotlight and Omnilight with dual-paraboloid).
  1646. for (uint32_t i = 0; i < shadows.size(); i++) {
  1647. _render_shadow_pass(p_render_data->render_shadows[shadows[i]].light, p_render_data->shadow_atlas, p_render_data->render_shadows[shadows[i]].pass, p_render_data->render_shadows[shadows[i]].instances, lod_distance_multiplier, p_render_data->screen_mesh_lod_threshold, p_render_data->render_info, p_viewport_size, p_render_data->cam_transform);
  1648. }
  1649. }
  1650. }
  1651. void RasterizerSceneGLES3::_render_shadow_pass(RID p_light, RID p_shadow_atlas, int p_pass, const PagedArray<RenderGeometryInstance *> &p_instances, float p_lod_distance_multiplier, float p_screen_mesh_lod_threshold, RenderingMethod::RenderInfo *p_render_info, const Size2i &p_viewport_size, const Transform3D &p_main_cam_transform) {
  1652. GLES3::LightStorage *light_storage = GLES3::LightStorage::get_singleton();
  1653. ERR_FAIL_COND(!light_storage->owns_light_instance(p_light));
  1654. RID base = light_storage->light_instance_get_base_light(p_light);
  1655. float zfar = 0.0;
  1656. bool use_pancake = false;
  1657. float shadow_bias = 0.0;
  1658. bool reverse_cull = false;
  1659. bool needs_clear = false;
  1660. Projection light_projection;
  1661. Transform3D light_transform;
  1662. GLuint shadow_fb = 0;
  1663. Rect2i atlas_rect;
  1664. if (light_storage->light_get_type(base) == RS::LIGHT_DIRECTIONAL) {
  1665. // Set pssm stuff.
  1666. uint64_t last_scene_shadow_pass = light_storage->light_instance_get_shadow_pass(p_light);
  1667. if (last_scene_shadow_pass != get_scene_pass()) {
  1668. light_storage->light_instance_set_directional_rect(p_light, light_storage->get_directional_shadow_rect());
  1669. light_storage->directional_shadow_increase_current_light();
  1670. light_storage->light_instance_set_shadow_pass(p_light, get_scene_pass());
  1671. }
  1672. atlas_rect = light_storage->light_instance_get_directional_rect(p_light);
  1673. if (light_storage->light_directional_get_shadow_mode(base) == RS::LIGHT_DIRECTIONAL_SHADOW_PARALLEL_4_SPLITS) {
  1674. atlas_rect.size.width /= 2;
  1675. atlas_rect.size.height /= 2;
  1676. if (p_pass == 1) {
  1677. atlas_rect.position.x += atlas_rect.size.width;
  1678. } else if (p_pass == 2) {
  1679. atlas_rect.position.y += atlas_rect.size.height;
  1680. } else if (p_pass == 3) {
  1681. atlas_rect.position += atlas_rect.size;
  1682. }
  1683. } else if (light_storage->light_directional_get_shadow_mode(base) == RS::LIGHT_DIRECTIONAL_SHADOW_PARALLEL_2_SPLITS) {
  1684. atlas_rect.size.height /= 2;
  1685. if (p_pass == 0) {
  1686. } else {
  1687. atlas_rect.position.y += atlas_rect.size.height;
  1688. }
  1689. }
  1690. use_pancake = light_storage->light_get_param(base, RS::LIGHT_PARAM_SHADOW_PANCAKE_SIZE) > 0;
  1691. light_projection = light_storage->light_instance_get_shadow_camera(p_light, p_pass);
  1692. light_transform = light_storage->light_instance_get_shadow_transform(p_light, p_pass);
  1693. float directional_shadow_size = light_storage->directional_shadow_get_size();
  1694. Rect2 atlas_rect_norm = atlas_rect;
  1695. atlas_rect_norm.position /= directional_shadow_size;
  1696. atlas_rect_norm.size /= directional_shadow_size;
  1697. light_storage->light_instance_set_directional_shadow_atlas_rect(p_light, p_pass, atlas_rect_norm);
  1698. zfar = RSG::light_storage->light_get_param(base, RS::LIGHT_PARAM_RANGE);
  1699. shadow_fb = light_storage->direction_shadow_get_fb();
  1700. reverse_cull = !light_storage->light_get_reverse_cull_face_mode(base);
  1701. float bias_scale = light_storage->light_instance_get_shadow_bias_scale(p_light, p_pass);
  1702. shadow_bias = light_storage->light_get_param(base, RS::LIGHT_PARAM_SHADOW_BIAS) / 100.0 * bias_scale;
  1703. } else {
  1704. // Set from shadow atlas.
  1705. ERR_FAIL_COND(!light_storage->owns_shadow_atlas(p_shadow_atlas));
  1706. ERR_FAIL_COND(!light_storage->shadow_atlas_owns_light_instance(p_shadow_atlas, p_light));
  1707. uint32_t key = light_storage->shadow_atlas_get_light_instance_key(p_shadow_atlas, p_light);
  1708. uint32_t quadrant = (key >> GLES3::LightStorage::QUADRANT_SHIFT) & 0x3;
  1709. uint32_t shadow = key & GLES3::LightStorage::SHADOW_INDEX_MASK;
  1710. ERR_FAIL_INDEX((int)shadow, light_storage->shadow_atlas_get_quadrant_shadows_length(p_shadow_atlas, quadrant));
  1711. int shadow_size = light_storage->shadow_atlas_get_quadrant_shadow_size(p_shadow_atlas, quadrant);
  1712. shadow_fb = light_storage->shadow_atlas_get_quadrant_shadow_fb(p_shadow_atlas, quadrant, shadow);
  1713. zfar = light_storage->light_get_param(base, RS::LIGHT_PARAM_RANGE);
  1714. reverse_cull = !light_storage->light_get_reverse_cull_face_mode(base);
  1715. if (light_storage->light_get_type(base) == RS::LIGHT_OMNI) {
  1716. if (light_storage->light_omni_get_shadow_mode(base) == RS::LIGHT_OMNI_SHADOW_CUBE) {
  1717. GLuint shadow_texture = light_storage->shadow_atlas_get_quadrant_shadow_texture(p_shadow_atlas, quadrant, shadow);
  1718. glBindFramebuffer(GL_FRAMEBUFFER, shadow_fb);
  1719. static GLenum cube_map_faces[6] = {
  1720. GL_TEXTURE_CUBE_MAP_POSITIVE_X,
  1721. GL_TEXTURE_CUBE_MAP_NEGATIVE_X,
  1722. // Flipped order for Y to match what the RD renderer expects
  1723. // (and thus what is given to us by the Rendering Server).
  1724. GL_TEXTURE_CUBE_MAP_NEGATIVE_Y,
  1725. GL_TEXTURE_CUBE_MAP_POSITIVE_Y,
  1726. GL_TEXTURE_CUBE_MAP_POSITIVE_Z,
  1727. GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
  1728. };
  1729. glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, cube_map_faces[p_pass], shadow_texture, 0);
  1730. light_projection = light_storage->light_instance_get_shadow_camera(p_light, p_pass);
  1731. light_transform = light_storage->light_instance_get_shadow_transform(p_light, p_pass);
  1732. shadow_size = shadow_size / 2;
  1733. } else {
  1734. ERR_FAIL_MSG("Dual paraboloid shadow mode not supported in the Compatibility renderer. Please use CubeMap shadow mode instead.");
  1735. }
  1736. shadow_bias = light_storage->light_get_param(base, RS::LIGHT_PARAM_SHADOW_BIAS);
  1737. } else if (light_storage->light_get_type(base) == RS::LIGHT_SPOT) {
  1738. light_projection = light_storage->light_instance_get_shadow_camera(p_light, 0);
  1739. light_transform = light_storage->light_instance_get_shadow_transform(p_light, 0);
  1740. shadow_bias = light_storage->light_get_param(base, RS::LIGHT_PARAM_SHADOW_BIAS) / 10.0;
  1741. // Prebake range into bias so we can scale based on distance easily.
  1742. shadow_bias *= light_storage->light_get_param(base, RS::LIGHT_PARAM_RANGE);
  1743. }
  1744. atlas_rect.size.x = shadow_size;
  1745. atlas_rect.size.y = shadow_size;
  1746. needs_clear = true;
  1747. }
  1748. RenderDataGLES3 render_data;
  1749. render_data.cam_projection = light_projection;
  1750. render_data.cam_transform = light_transform;
  1751. render_data.inv_cam_transform = light_transform.affine_inverse();
  1752. render_data.z_far = zfar; // Only used by OmniLights.
  1753. render_data.z_near = 0.0;
  1754. render_data.lod_distance_multiplier = p_lod_distance_multiplier;
  1755. render_data.main_cam_transform = p_main_cam_transform;
  1756. render_data.instances = &p_instances;
  1757. render_data.render_info = p_render_info;
  1758. _setup_environment(&render_data, true, p_viewport_size, false, Color(), use_pancake, shadow_bias);
  1759. if (get_debug_draw_mode() == RS::VIEWPORT_DEBUG_DRAW_DISABLE_LOD) {
  1760. render_data.screen_mesh_lod_threshold = 0.0;
  1761. } else {
  1762. render_data.screen_mesh_lod_threshold = p_screen_mesh_lod_threshold;
  1763. }
  1764. _fill_render_list(RENDER_LIST_SECONDARY, &render_data, PASS_MODE_SHADOW);
  1765. render_list[RENDER_LIST_SECONDARY].sort_by_key();
  1766. glBindFramebuffer(GL_FRAMEBUFFER, shadow_fb);
  1767. glViewport(atlas_rect.position.x, atlas_rect.position.y, atlas_rect.size.x, atlas_rect.size.y);
  1768. GLuint global_buffer = GLES3::MaterialStorage::get_singleton()->global_shader_parameters_get_uniform_buffer();
  1769. glBindBufferBase(GL_UNIFORM_BUFFER, SCENE_GLOBALS_UNIFORM_LOCATION, global_buffer);
  1770. glBindBuffer(GL_UNIFORM_BUFFER, 0);
  1771. scene_state.reset_gl_state();
  1772. scene_state.enable_gl_depth_test(true);
  1773. scene_state.enable_gl_depth_draw(true);
  1774. scene_state.set_gl_depth_func(GL_GREATER);
  1775. glColorMask(0, 0, 0, 0);
  1776. glDrawBuffers(0, nullptr);
  1777. RasterizerGLES3::clear_depth(0.0);
  1778. if (needs_clear) {
  1779. glClear(GL_DEPTH_BUFFER_BIT);
  1780. }
  1781. uint64_t spec_constant_base_flags = SceneShaderGLES3::DISABLE_LIGHTMAP |
  1782. SceneShaderGLES3::DISABLE_LIGHT_DIRECTIONAL |
  1783. SceneShaderGLES3::DISABLE_LIGHT_OMNI |
  1784. SceneShaderGLES3::DISABLE_LIGHT_SPOT |
  1785. SceneShaderGLES3::DISABLE_FOG |
  1786. SceneShaderGLES3::RENDER_SHADOWS;
  1787. if (light_storage->light_get_type(base) == RS::LIGHT_OMNI) {
  1788. spec_constant_base_flags |= SceneShaderGLES3::RENDER_SHADOWS_LINEAR;
  1789. }
  1790. RenderListParameters render_list_params(render_list[RENDER_LIST_SECONDARY].elements.ptr(), render_list[RENDER_LIST_SECONDARY].elements.size(), reverse_cull, spec_constant_base_flags, false);
  1791. _render_list_template<PASS_MODE_SHADOW>(&render_list_params, &render_data, 0, render_list[RENDER_LIST_SECONDARY].elements.size());
  1792. glColorMask(1, 1, 1, 1);
  1793. scene_state.enable_gl_depth_test(false);
  1794. scene_state.enable_gl_depth_draw(true);
  1795. glDisable(GL_CULL_FACE);
  1796. scene_state.cull_mode = RS::CULL_MODE_DISABLED;
  1797. glBindFramebuffer(GL_FRAMEBUFFER, GLES3::TextureStorage::system_fbo);
  1798. }
  1799. void RasterizerSceneGLES3::render_scene(const Ref<RenderSceneBuffers> &p_render_buffers, const CameraData *p_camera_data, const CameraData *p_prev_camera_data, const PagedArray<RenderGeometryInstance *> &p_instances, const PagedArray<RID> &p_lights, const PagedArray<RID> &p_reflection_probes, const PagedArray<RID> &p_voxel_gi_instances, const PagedArray<RID> &p_decals, const PagedArray<RID> &p_lightmaps, const PagedArray<RID> &p_fog_volumes, RID p_environment, RID p_camera_attributes, RID p_compositor, RID p_shadow_atlas, RID p_occluder_debug_tex, RID p_reflection_atlas, RID p_reflection_probe, int p_reflection_probe_pass, float p_screen_mesh_lod_threshold, const RenderShadowData *p_render_shadows, int p_render_shadow_count, const RenderSDFGIData *p_render_sdfgi_regions, int p_render_sdfgi_region_count, const RenderSDFGIUpdateData *p_sdfgi_update_data, RenderingMethod::RenderInfo *r_render_info) {
  1800. GLES3::TextureStorage *texture_storage = GLES3::TextureStorage::get_singleton();
  1801. GLES3::Config *config = GLES3::Config::get_singleton();
  1802. RENDER_TIMESTAMP("Setup 3D Scene");
  1803. bool apply_color_adjustments_in_post = false;
  1804. bool is_reflection_probe = p_reflection_probe.is_valid();
  1805. Ref<RenderSceneBuffersGLES3> rb = p_render_buffers;
  1806. ERR_FAIL_COND(rb.is_null());
  1807. if (rb->get_scaling_3d_mode() != RS::VIEWPORT_SCALING_3D_MODE_OFF) {
  1808. // If we're scaling, we apply tonemapping etc. in post, so disable it during rendering
  1809. apply_color_adjustments_in_post = true;
  1810. }
  1811. GLES3::RenderTarget *rt = nullptr; // No render target for reflection probe
  1812. if (!is_reflection_probe) {
  1813. rt = texture_storage->get_render_target(rb->render_target);
  1814. ERR_FAIL_NULL(rt);
  1815. }
  1816. bool glow_enabled = false;
  1817. if (p_environment.is_valid()) {
  1818. glow_enabled = environment_get_glow_enabled(p_environment);
  1819. if (glow_enabled) {
  1820. // If glow is enabled, we apply tonemapping etc. in post, so disable it during rendering
  1821. apply_color_adjustments_in_post = true;
  1822. }
  1823. }
  1824. // Assign render data
  1825. // Use the format from rendererRD
  1826. RenderDataGLES3 render_data;
  1827. {
  1828. render_data.render_buffers = rb;
  1829. if (rt) {
  1830. render_data.transparent_bg = rt->is_transparent;
  1831. render_data.render_region = rt->render_region;
  1832. }
  1833. // Our first camera is used by default
  1834. render_data.cam_transform = p_camera_data->main_transform;
  1835. render_data.inv_cam_transform = render_data.cam_transform.affine_inverse();
  1836. render_data.cam_projection = p_camera_data->main_projection;
  1837. render_data.cam_orthogonal = p_camera_data->is_orthogonal;
  1838. render_data.cam_frustum = p_camera_data->is_frustum;
  1839. render_data.camera_visible_layers = p_camera_data->visible_layers;
  1840. render_data.main_cam_transform = p_camera_data->main_transform;
  1841. render_data.view_count = p_camera_data->view_count;
  1842. for (uint32_t v = 0; v < p_camera_data->view_count; v++) {
  1843. render_data.view_eye_offset[v] = p_camera_data->view_offset[v].origin;
  1844. render_data.view_projection[v] = p_camera_data->view_projection[v];
  1845. }
  1846. render_data.z_near = p_camera_data->main_projection.get_z_near();
  1847. render_data.z_far = p_camera_data->main_projection.get_z_far();
  1848. render_data.instances = &p_instances;
  1849. render_data.lights = &p_lights;
  1850. render_data.reflection_probes = &p_reflection_probes;
  1851. render_data.environment = p_environment;
  1852. render_data.camera_attributes = p_camera_attributes;
  1853. render_data.shadow_atlas = p_shadow_atlas;
  1854. render_data.reflection_probe = p_reflection_probe;
  1855. render_data.reflection_probe_pass = p_reflection_probe_pass;
  1856. // this should be the same for all cameras..
  1857. render_data.lod_distance_multiplier = p_camera_data->main_projection.get_lod_multiplier();
  1858. if (rt != nullptr && rt->color_type == GL_UNSIGNED_INT_2_10_10_10_REV && glow_enabled) {
  1859. // As our output is in sRGB and we're using 10bit color space, we can fake a little HDR to do glow...
  1860. render_data.luminance_multiplier = 0.25;
  1861. } else {
  1862. render_data.luminance_multiplier = 1.0;
  1863. }
  1864. if (get_debug_draw_mode() == RS::VIEWPORT_DEBUG_DRAW_DISABLE_LOD) {
  1865. render_data.screen_mesh_lod_threshold = 0.0;
  1866. } else {
  1867. render_data.screen_mesh_lod_threshold = p_screen_mesh_lod_threshold;
  1868. }
  1869. render_data.render_info = r_render_info;
  1870. render_data.render_shadows = p_render_shadows;
  1871. render_data.render_shadow_count = p_render_shadow_count;
  1872. }
  1873. PagedArray<RID> empty;
  1874. if (get_debug_draw_mode() == RS::VIEWPORT_DEBUG_DRAW_UNSHADED) {
  1875. render_data.lights = &empty;
  1876. render_data.reflection_probes = &empty;
  1877. }
  1878. bool reverse_cull = render_data.cam_transform.basis.determinant() < 0;
  1879. ///////////
  1880. // Fill Light lists here
  1881. //////////
  1882. GLuint global_buffer = GLES3::MaterialStorage::get_singleton()->global_shader_parameters_get_uniform_buffer();
  1883. glBindBufferBase(GL_UNIFORM_BUFFER, SCENE_GLOBALS_UNIFORM_LOCATION, global_buffer);
  1884. Color clear_color;
  1885. if (!is_reflection_probe && rb->render_target.is_valid()) {
  1886. clear_color = texture_storage->render_target_get_clear_request_color(rb->render_target);
  1887. } else {
  1888. clear_color = texture_storage->get_default_clear_color();
  1889. }
  1890. bool fb_cleared = false;
  1891. Size2i screen_size = rb->internal_size;
  1892. bool use_wireframe = get_debug_draw_mode() == RS::VIEWPORT_DEBUG_DRAW_WIREFRAME;
  1893. SceneState::TonemapUBO tonemap_ubo;
  1894. if (render_data.environment.is_valid()) {
  1895. bool use_bcs = environment_get_adjustments_enabled(render_data.environment);
  1896. if (use_bcs) {
  1897. apply_color_adjustments_in_post = true;
  1898. }
  1899. tonemap_ubo.exposure = environment_get_exposure(render_data.environment);
  1900. tonemap_ubo.white = environment_get_white(render_data.environment);
  1901. tonemap_ubo.tonemapper = int32_t(environment_get_tone_mapper(render_data.environment));
  1902. tonemap_ubo.brightness = environment_get_adjustments_brightness(render_data.environment);
  1903. tonemap_ubo.contrast = environment_get_adjustments_contrast(render_data.environment);
  1904. tonemap_ubo.saturation = environment_get_adjustments_saturation(render_data.environment);
  1905. }
  1906. if (scene_state.tonemap_buffer == 0) {
  1907. // Only create if using 3D
  1908. glGenBuffers(1, &scene_state.tonemap_buffer);
  1909. glBindBufferBase(GL_UNIFORM_BUFFER, SCENE_TONEMAP_UNIFORM_LOCATION, scene_state.tonemap_buffer);
  1910. GLES3::Utilities::get_singleton()->buffer_allocate_data(GL_UNIFORM_BUFFER, scene_state.tonemap_buffer, sizeof(SceneState::TonemapUBO), &tonemap_ubo, GL_STREAM_DRAW, "Tonemap UBO");
  1911. } else {
  1912. glBindBufferBase(GL_UNIFORM_BUFFER, SCENE_TONEMAP_UNIFORM_LOCATION, scene_state.tonemap_buffer);
  1913. glBufferData(GL_UNIFORM_BUFFER, sizeof(SceneState::TonemapUBO), &tonemap_ubo, GL_STREAM_DRAW);
  1914. }
  1915. glBindBuffer(GL_UNIFORM_BUFFER, 0);
  1916. scene_state.ubo.emissive_exposure_normalization = -1.0; // Use default exposure normalization.
  1917. bool flip_y = !is_reflection_probe;
  1918. if (rt && rt->overridden.color.is_valid()) {
  1919. // If we've overridden the render target's color texture, then don't render upside down.
  1920. // We're probably rendering directly to an XR device.
  1921. flip_y = false;
  1922. }
  1923. if (!flip_y) {
  1924. // If we're rendering right-side up, then we need to change the winding order.
  1925. glFrontFace(GL_CW);
  1926. }
  1927. _render_shadows(&render_data, screen_size);
  1928. _setup_lights(&render_data, true, render_data.directional_light_count, render_data.omni_light_count, render_data.spot_light_count, render_data.directional_shadow_count);
  1929. _setup_environment(&render_data, is_reflection_probe, screen_size, flip_y, clear_color, false);
  1930. _fill_render_list(RENDER_LIST_OPAQUE, &render_data, PASS_MODE_COLOR);
  1931. render_list[RENDER_LIST_OPAQUE].sort_by_key();
  1932. render_list[RENDER_LIST_ALPHA].sort_by_reverse_depth_and_priority();
  1933. bool draw_sky = false;
  1934. bool draw_sky_fog_only = false;
  1935. bool keep_color = false;
  1936. bool draw_canvas = false;
  1937. bool draw_feed = false;
  1938. float sky_energy_multiplier = 1.0;
  1939. int camera_feed_id = -1;
  1940. if (unlikely(get_debug_draw_mode() == RS::VIEWPORT_DEBUG_DRAW_OVERDRAW)) {
  1941. clear_color = Color(0, 0, 0, 1); //in overdraw mode, BG should always be black
  1942. } else if (render_data.environment.is_valid()) {
  1943. RS::EnvironmentBG bg_mode = environment_get_background(render_data.environment);
  1944. float bg_energy_multiplier = environment_get_bg_energy_multiplier(render_data.environment);
  1945. bg_energy_multiplier *= environment_get_bg_intensity(render_data.environment);
  1946. RS::EnvironmentReflectionSource reflection_source = environment_get_reflection_source(render_data.environment);
  1947. RS::EnvironmentAmbientSource ambient_source = environment_get_ambient_source(render_data.environment);
  1948. if (render_data.camera_attributes.is_valid()) {
  1949. bg_energy_multiplier *= RSG::camera_attributes->camera_attributes_get_exposure_normalization_factor(render_data.camera_attributes);
  1950. }
  1951. switch (bg_mode) {
  1952. case RS::ENV_BG_CLEAR_COLOR: {
  1953. clear_color.r *= bg_energy_multiplier;
  1954. clear_color.g *= bg_energy_multiplier;
  1955. clear_color.b *= bg_energy_multiplier;
  1956. if (!render_data.transparent_bg && environment_get_fog_enabled(render_data.environment)) {
  1957. draw_sky_fog_only = true;
  1958. GLES3::MaterialStorage::get_singleton()->material_set_param(sky_globals.fog_material, "clear_color", Variant(clear_color));
  1959. }
  1960. } break;
  1961. case RS::ENV_BG_COLOR: {
  1962. clear_color = environment_get_bg_color(render_data.environment);
  1963. clear_color.r *= bg_energy_multiplier;
  1964. clear_color.g *= bg_energy_multiplier;
  1965. clear_color.b *= bg_energy_multiplier;
  1966. if (!render_data.transparent_bg && environment_get_fog_enabled(render_data.environment)) {
  1967. draw_sky_fog_only = true;
  1968. GLES3::MaterialStorage::get_singleton()->material_set_param(sky_globals.fog_material, "clear_color", Variant(clear_color));
  1969. }
  1970. } break;
  1971. case RS::ENV_BG_SKY: {
  1972. draw_sky = !render_data.transparent_bg;
  1973. } break;
  1974. case RS::ENV_BG_CANVAS: {
  1975. draw_canvas = true;
  1976. } break;
  1977. case RS::ENV_BG_KEEP: {
  1978. keep_color = true;
  1979. } break;
  1980. case RS::ENV_BG_CAMERA_FEED: {
  1981. camera_feed_id = environment_get_camera_feed_id(render_data.environment);
  1982. draw_feed = true;
  1983. keep_color = true;
  1984. } break;
  1985. default: {
  1986. }
  1987. }
  1988. bool sky_reflections = reflection_source == RS::ENV_REFLECTION_SOURCE_SKY;
  1989. sky_reflections |= reflection_source == RS::ENV_REFLECTION_SOURCE_BG && bg_mode == RS::ENV_BG_SKY;
  1990. bool sky_ambient = ambient_source == RS::ENV_AMBIENT_SOURCE_SKY;
  1991. sky_ambient |= ambient_source == RS::ENV_AMBIENT_SOURCE_BG && bg_mode == RS::ENV_BG_SKY;
  1992. // setup sky if used for ambient, reflections, or background
  1993. if (draw_sky || draw_sky_fog_only || sky_reflections || sky_ambient) {
  1994. RENDER_TIMESTAMP("Setup Sky");
  1995. Projection projection = render_data.cam_projection;
  1996. if (is_reflection_probe) {
  1997. Projection correction;
  1998. correction.set_depth_correction(true, true, false);
  1999. projection = correction * render_data.cam_projection;
  2000. }
  2001. sky_energy_multiplier *= bg_energy_multiplier;
  2002. _setup_sky(&render_data, *render_data.lights, projection, render_data.cam_transform, screen_size);
  2003. if (environment_get_sky(render_data.environment).is_valid()) {
  2004. if (sky_reflections || sky_ambient) {
  2005. _update_sky_radiance(render_data.environment, projection, render_data.cam_transform, sky_energy_multiplier);
  2006. }
  2007. } else {
  2008. // do not try to draw sky if invalid
  2009. draw_sky = false;
  2010. }
  2011. }
  2012. }
  2013. GLuint fbo = 0;
  2014. if (is_reflection_probe && GLES3::LightStorage::get_singleton()->reflection_probe_has_atlas_index(render_data.reflection_probe)) {
  2015. fbo = GLES3::LightStorage::get_singleton()->reflection_probe_instance_get_framebuffer(render_data.reflection_probe, render_data.reflection_probe_pass);
  2016. } else {
  2017. rb->set_apply_color_adjustments_in_post(apply_color_adjustments_in_post);
  2018. fbo = rb->get_render_fbo();
  2019. }
  2020. glBindFramebuffer(GL_FRAMEBUFFER, fbo);
  2021. glViewport(0, 0, rb->internal_size.x, rb->internal_size.y);
  2022. scene_state.reset_gl_state();
  2023. // Do depth prepass if it's explicitly enabled
  2024. bool use_depth_prepass = config->use_depth_prepass;
  2025. // Forcibly enable depth prepass if opaque stencil writes are used.
  2026. use_depth_prepass = use_depth_prepass || scene_state.used_opaque_stencil;
  2027. // Don't do depth prepass we are rendering overdraw
  2028. use_depth_prepass = use_depth_prepass && get_debug_draw_mode() != RS::VIEWPORT_DEBUG_DRAW_OVERDRAW;
  2029. if (use_depth_prepass) {
  2030. RENDER_TIMESTAMP("Depth Prepass");
  2031. //pre z pass
  2032. if (render_data.render_region != Rect2i()) {
  2033. glViewport(render_data.render_region.position.x, render_data.render_region.position.y, render_data.render_region.size.width, render_data.render_region.size.height);
  2034. }
  2035. scene_state.enable_gl_depth_test(true);
  2036. scene_state.enable_gl_depth_draw(true);
  2037. scene_state.enable_gl_blend(false);
  2038. scene_state.set_gl_depth_func(GL_GEQUAL);
  2039. scene_state.enable_gl_scissor_test(false);
  2040. scene_state.enable_gl_stencil_test(false);
  2041. scene_state.set_gl_stencil_write_mask(255);
  2042. glColorMask(0, 0, 0, 0);
  2043. RasterizerGLES3::clear_depth(0.0);
  2044. RasterizerGLES3::clear_stencil(0);
  2045. glClear(GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
  2046. // Some desktop GL implementations fall apart when using Multiview with GL_NONE.
  2047. GLuint db = p_camera_data->view_count > 1 ? GL_COLOR_ATTACHMENT0 : GL_NONE;
  2048. glDrawBuffers(1, &db);
  2049. uint64_t spec_constant = SceneShaderGLES3::DISABLE_FOG | SceneShaderGLES3::DISABLE_LIGHT_DIRECTIONAL |
  2050. SceneShaderGLES3::DISABLE_LIGHTMAP | SceneShaderGLES3::DISABLE_LIGHT_OMNI |
  2051. SceneShaderGLES3::DISABLE_LIGHT_SPOT;
  2052. RenderListParameters render_list_params(render_list[RENDER_LIST_OPAQUE].elements.ptr(), render_list[RENDER_LIST_OPAQUE].elements.size(), reverse_cull, spec_constant, use_wireframe);
  2053. _render_list_template<PASS_MODE_DEPTH>(&render_list_params, &render_data, 0, render_list[RENDER_LIST_OPAQUE].elements.size());
  2054. glColorMask(1, 1, 1, 1);
  2055. fb_cleared = true;
  2056. scene_state.used_depth_prepass = true;
  2057. } else {
  2058. scene_state.used_depth_prepass = false;
  2059. }
  2060. glBlendEquation(GL_FUNC_ADD);
  2061. if (render_data.transparent_bg) {
  2062. glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
  2063. scene_state.enable_gl_blend(true);
  2064. } else {
  2065. glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_ZERO, GL_ONE);
  2066. scene_state.enable_gl_blend(false);
  2067. }
  2068. scene_state.current_blend_mode = GLES3::SceneShaderData::BLEND_MODE_MIX;
  2069. scene_state.enable_gl_scissor_test(false);
  2070. scene_state.enable_gl_depth_test(true);
  2071. scene_state.enable_gl_depth_draw(true);
  2072. scene_state.set_gl_depth_func(GL_GEQUAL);
  2073. {
  2074. GLuint db = GL_COLOR_ATTACHMENT0;
  2075. glDrawBuffers(1, &db);
  2076. }
  2077. scene_state.enable_gl_stencil_test(false);
  2078. if (!fb_cleared) {
  2079. RasterizerGLES3::clear_depth(0.0);
  2080. RasterizerGLES3::clear_stencil(0);
  2081. glClear(GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
  2082. }
  2083. // Need to clear framebuffer unless:
  2084. // a) We explicitly request not to (i.e. ENV_BG_KEEP).
  2085. // b) We are rendering to a non-intermediate framebuffer with ENV_BG_CANVAS (shared between 2D and 3D).
  2086. if (!keep_color && (!draw_canvas || fbo != rt->fbo)) {
  2087. clear_color.a = render_data.transparent_bg ? 0.0f : 1.0f;
  2088. glClearBufferfv(GL_COLOR, 0, clear_color.components);
  2089. }
  2090. if ((keep_color || draw_canvas) && fbo != rt->fbo) {
  2091. // Need to copy our current contents to our intermediate/MSAA buffer
  2092. GLES3::CopyEffects *copy_effects = GLES3::CopyEffects::get_singleton();
  2093. scene_state.enable_gl_depth_test(false);
  2094. scene_state.enable_gl_depth_draw(false);
  2095. glActiveTexture(GL_TEXTURE0);
  2096. glBindTexture(rt->view_count > 1 ? GL_TEXTURE_2D_ARRAY : GL_TEXTURE_2D, rt->color);
  2097. copy_effects->copy_screen(render_data.luminance_multiplier);
  2098. scene_state.enable_gl_depth_test(true);
  2099. scene_state.enable_gl_depth_draw(true);
  2100. }
  2101. RENDER_TIMESTAMP("Render Opaque Pass");
  2102. uint64_t spec_constant_base_flags = 0;
  2103. if (render_data.render_region != Rect2i()) {
  2104. glViewport(render_data.render_region.position.x, render_data.render_region.position.y, render_data.render_region.size.width, render_data.render_region.size.height);
  2105. }
  2106. {
  2107. // Specialization Constants that apply for entire rendering pass.
  2108. if (render_data.directional_light_count == 0) {
  2109. spec_constant_base_flags |= SceneShaderGLES3::DISABLE_LIGHT_DIRECTIONAL;
  2110. }
  2111. if (render_data.environment.is_null() || (render_data.environment.is_valid() && !environment_get_fog_enabled(render_data.environment))) {
  2112. spec_constant_base_flags |= SceneShaderGLES3::DISABLE_FOG;
  2113. }
  2114. if (render_data.environment.is_valid() && environment_get_fog_mode(render_data.environment) == RS::EnvironmentFogMode::ENV_FOG_MODE_DEPTH) {
  2115. spec_constant_base_flags |= SceneShaderGLES3::USE_DEPTH_FOG;
  2116. }
  2117. if (!apply_color_adjustments_in_post) {
  2118. spec_constant_base_flags |= SceneShaderGLES3::APPLY_TONEMAPPING;
  2119. }
  2120. }
  2121. if (draw_feed && camera_feed_id > -1) {
  2122. RENDER_TIMESTAMP("Render Camera feed");
  2123. scene_state.enable_gl_depth_draw(false);
  2124. scene_state.enable_gl_depth_test(false);
  2125. scene_state.enable_gl_blend(false);
  2126. scene_state.set_gl_cull_mode(RS::CULL_MODE_BACK);
  2127. Ref<CameraFeed> feed = CameraServer::get_singleton()->get_feed_by_id(camera_feed_id);
  2128. if (feed.is_valid()) {
  2129. RID camera_YCBCR = feed->get_texture(CameraServer::FEED_YCBCR_IMAGE);
  2130. GLES3::TextureStorage::get_singleton()->texture_bind(camera_YCBCR, 0);
  2131. GLES3::FeedEffects *feed_effects = GLES3::FeedEffects::get_singleton();
  2132. feed_effects->draw();
  2133. }
  2134. scene_state.enable_gl_depth_draw(true);
  2135. scene_state.enable_gl_depth_test(true);
  2136. scene_state.enable_gl_blend(true);
  2137. }
  2138. // Render Opaque Objects.
  2139. RenderListParameters render_list_params(render_list[RENDER_LIST_OPAQUE].elements.ptr(), render_list[RENDER_LIST_OPAQUE].elements.size(), reverse_cull, spec_constant_base_flags, use_wireframe);
  2140. _render_list_template<PASS_MODE_COLOR>(&render_list_params, &render_data, 0, render_list[RENDER_LIST_OPAQUE].elements.size());
  2141. scene_state.enable_gl_depth_draw(false);
  2142. scene_state.enable_gl_stencil_test(false);
  2143. if (draw_sky || draw_sky_fog_only) {
  2144. RENDER_TIMESTAMP("Render Sky");
  2145. scene_state.enable_gl_depth_test(true);
  2146. scene_state.set_gl_depth_func(GL_GEQUAL);
  2147. scene_state.enable_gl_blend(false);
  2148. scene_state.set_gl_cull_mode(RS::CULL_MODE_BACK);
  2149. Transform3D transform = render_data.cam_transform;
  2150. Projection projection = render_data.cam_projection;
  2151. if (is_reflection_probe) {
  2152. Projection correction;
  2153. correction.columns[1][1] = -1.0;
  2154. projection = correction * render_data.cam_projection;
  2155. } else if (render_data.cam_frustum) {
  2156. // Sky is drawn upside down, the frustum offset doesn't know the image is upside down so needs a flip.
  2157. projection[2].y = -projection[2].y;
  2158. }
  2159. _draw_sky(render_data.environment, projection, transform, sky_energy_multiplier, render_data.luminance_multiplier, p_camera_data->view_count > 1, flip_y, apply_color_adjustments_in_post);
  2160. }
  2161. if (rt && (scene_state.used_screen_texture || scene_state.used_depth_texture)) {
  2162. Size2i size;
  2163. GLuint backbuffer_fbo = 0;
  2164. GLuint backbuffer = 0;
  2165. GLuint backbuffer_depth = 0;
  2166. if (rb->get_scaling_3d_mode() == RS::VIEWPORT_SCALING_3D_MODE_OFF) {
  2167. texture_storage->check_backbuffer(rt, scene_state.used_screen_texture, scene_state.used_depth_texture); // note, badly names, this just allocates!
  2168. size = rt->size;
  2169. backbuffer_fbo = rt->backbuffer_fbo;
  2170. backbuffer = rt->backbuffer;
  2171. backbuffer_depth = rt->backbuffer_depth;
  2172. } else {
  2173. rb->check_backbuffer(scene_state.used_screen_texture, scene_state.used_depth_texture);
  2174. size = rb->get_internal_size();
  2175. backbuffer_fbo = rb->get_backbuffer_fbo();
  2176. backbuffer = rb->get_backbuffer();
  2177. backbuffer_depth = rb->get_backbuffer_depth();
  2178. }
  2179. if (backbuffer_fbo != 0) {
  2180. glBindFramebuffer(GL_READ_FRAMEBUFFER, fbo);
  2181. glReadBuffer(GL_COLOR_ATTACHMENT0);
  2182. glBindFramebuffer(GL_DRAW_FRAMEBUFFER, backbuffer_fbo);
  2183. if (scene_state.used_screen_texture) {
  2184. glBlitFramebuffer(0, 0, size.x, size.y,
  2185. 0, 0, size.x, size.y,
  2186. GL_COLOR_BUFFER_BIT, GL_NEAREST);
  2187. glActiveTexture(GL_TEXTURE0 + config->max_texture_image_units - 6);
  2188. glBindTexture(GL_TEXTURE_2D, backbuffer);
  2189. }
  2190. if (scene_state.used_depth_texture) {
  2191. glBlitFramebuffer(0, 0, size.x, size.y,
  2192. 0, 0, size.x, size.y,
  2193. GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT, GL_NEAREST);
  2194. glActiveTexture(GL_TEXTURE0 + config->max_texture_image_units - 7);
  2195. glBindTexture(GL_TEXTURE_2D, backbuffer_depth);
  2196. }
  2197. }
  2198. // Bound framebuffer may have changed, so change it back
  2199. glBindFramebuffer(GL_FRAMEBUFFER, fbo);
  2200. }
  2201. RENDER_TIMESTAMP("Render 3D Transparent Pass");
  2202. scene_state.enable_gl_blend(true);
  2203. //Render transparent pass
  2204. RenderListParameters render_list_params_alpha(render_list[RENDER_LIST_ALPHA].elements.ptr(), render_list[RENDER_LIST_ALPHA].elements.size(), reverse_cull, spec_constant_base_flags, use_wireframe);
  2205. _render_list_template<PASS_MODE_COLOR_TRANSPARENT>(&render_list_params_alpha, &render_data, 0, render_list[RENDER_LIST_ALPHA].elements.size(), true);
  2206. scene_state.enable_gl_stencil_test(false);
  2207. if (!flip_y) {
  2208. // Restore the default winding order.
  2209. glFrontFace(GL_CCW);
  2210. }
  2211. if (!is_reflection_probe && rb.is_valid()) {
  2212. _render_buffers_debug_draw(rb, p_shadow_atlas, fbo);
  2213. }
  2214. // Reset stuff that may trip up the next process.
  2215. scene_state.reset_gl_state();
  2216. glUseProgram(0);
  2217. if (!is_reflection_probe) {
  2218. _render_post_processing(&render_data);
  2219. texture_storage->render_target_disable_clear_request(rb->render_target);
  2220. }
  2221. glActiveTexture(GL_TEXTURE0);
  2222. }
  2223. void RasterizerSceneGLES3::_render_post_processing(const RenderDataGLES3 *p_render_data) {
  2224. GLES3::TextureStorage *texture_storage = GLES3::TextureStorage::get_singleton();
  2225. GLES3::Glow *glow = GLES3::Glow::get_singleton();
  2226. GLES3::PostEffects *post_effects = GLES3::PostEffects::get_singleton();
  2227. Ref<RenderSceneBuffersGLES3> rb = p_render_data->render_buffers;
  2228. ERR_FAIL_COND(rb.is_null());
  2229. RID render_target = rb->get_render_target();
  2230. Size2i internal_size = rb->get_internal_size();
  2231. Size2i target_size = rb->get_target_size();
  2232. uint32_t view_count = rb->get_view_count();
  2233. // bool msaa2d_needs_resolve = texture_storage->render_target_get_msaa(render_target) != RS::VIEWPORT_MSAA_DISABLED && !GLES3::Config::get_singleton()->rt_msaa_supported;
  2234. bool msaa3d_needs_resolve = rb->get_msaa_needs_resolve();
  2235. GLuint fbo_msaa_3d = rb->get_msaa3d_fbo();
  2236. GLuint fbo_int = rb->get_internal_fbo();
  2237. GLuint fbo_rt = texture_storage->render_target_get_fbo(render_target); // TODO if MSAA 2D is enabled and we're not using rt_msaa, get 2D render target here.
  2238. // Check if we have glow enabled and if so, check if our buffers were allocated
  2239. bool glow_enabled = false;
  2240. float glow_intensity = 1.0;
  2241. float glow_bloom = 0.0;
  2242. float glow_hdr_bleed_threshold = 1.0;
  2243. float glow_hdr_bleed_scale = 2.0;
  2244. float glow_hdr_luminance_cap = 12.0;
  2245. if (p_render_data->environment.is_valid()) {
  2246. glow_enabled = environment_get_glow_enabled(p_render_data->environment);
  2247. glow_intensity = environment_get_glow_intensity(p_render_data->environment);
  2248. glow_bloom = environment_get_glow_bloom(p_render_data->environment);
  2249. glow_hdr_bleed_threshold = environment_get_glow_hdr_bleed_threshold(p_render_data->environment);
  2250. glow_hdr_bleed_scale = environment_get_glow_hdr_bleed_scale(p_render_data->environment);
  2251. glow_hdr_luminance_cap = environment_get_glow_hdr_luminance_cap(p_render_data->environment);
  2252. }
  2253. if (glow_enabled) {
  2254. rb->check_glow_buffers();
  2255. }
  2256. uint64_t bcs_spec_constants = 0;
  2257. if (p_render_data->environment.is_valid()) {
  2258. bool use_bcs = environment_get_adjustments_enabled(p_render_data->environment);
  2259. RID color_correction_texture = environment_get_color_correction(p_render_data->environment);
  2260. if (use_bcs) {
  2261. bcs_spec_constants |= PostShaderGLES3::USE_BCS;
  2262. if (color_correction_texture.is_valid()) {
  2263. bcs_spec_constants |= PostShaderGLES3::USE_COLOR_CORRECTION;
  2264. bool use_1d_lut = environment_get_use_1d_color_correction(p_render_data->environment);
  2265. GLenum texture_target = GL_TEXTURE_3D;
  2266. if (use_1d_lut) {
  2267. bcs_spec_constants |= PostShaderGLES3::USE_1D_LUT;
  2268. texture_target = GL_TEXTURE_2D;
  2269. }
  2270. glActiveTexture(GL_TEXTURE2);
  2271. glBindTexture(texture_target, texture_storage->texture_get_texid(color_correction_texture));
  2272. glTexParameteri(texture_target, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  2273. glTexParameteri(texture_target, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  2274. glTexParameteri(texture_target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  2275. glTexParameteri(texture_target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  2276. glTexParameteri(texture_target, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
  2277. }
  2278. }
  2279. }
  2280. if (view_count == 1) {
  2281. // Resolve if needed.
  2282. if (fbo_msaa_3d != 0 && msaa3d_needs_resolve) {
  2283. // We can use blit to copy things over
  2284. glBindFramebuffer(GL_READ_FRAMEBUFFER, fbo_msaa_3d);
  2285. if (fbo_int != 0) {
  2286. // We can't combine resolve and scaling, so resolve into our internal buffer
  2287. glBindFramebuffer(GL_DRAW_FRAMEBUFFER, fbo_int);
  2288. } else {
  2289. glBindFramebuffer(GL_DRAW_FRAMEBUFFER, fbo_rt);
  2290. }
  2291. glBlitFramebuffer(0, 0, internal_size.x, internal_size.y, 0, 0, internal_size.x, internal_size.y, GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT, GL_NEAREST);
  2292. }
  2293. // Rendered to intermediate buffer, must copy to our render target
  2294. if (fbo_int != 0) {
  2295. // Apply glow/bloom if requested? then populate our glow buffers
  2296. GLuint color = fbo_int != 0 ? rb->get_internal_color() : texture_storage->render_target_get_color(render_target);
  2297. const GLES3::Glow::GLOWLEVEL *glow_buffers = nullptr;
  2298. if (glow_enabled) {
  2299. glow_buffers = rb->get_glow_buffers();
  2300. glow->set_luminance_multiplier(p_render_data->luminance_multiplier);
  2301. glow->set_intensity(glow_intensity);
  2302. glow->set_glow_bloom(glow_bloom);
  2303. glow->set_glow_hdr_bleed_threshold(glow_hdr_bleed_threshold);
  2304. glow->set_glow_hdr_bleed_scale(glow_hdr_bleed_scale);
  2305. glow->set_glow_hdr_luminance_cap(glow_hdr_luminance_cap);
  2306. glow->process_glow(color, internal_size, glow_buffers);
  2307. }
  2308. // Copy color buffer
  2309. post_effects->post_copy(fbo_rt, target_size, color, internal_size, p_render_data->luminance_multiplier, glow_buffers, glow_intensity, 0, false, bcs_spec_constants);
  2310. // Copy depth buffer
  2311. glBindFramebuffer(GL_READ_FRAMEBUFFER, fbo_int);
  2312. glBindFramebuffer(GL_DRAW_FRAMEBUFFER, fbo_rt);
  2313. glBlitFramebuffer(0, 0, internal_size.x, internal_size.y, 0, 0, target_size.x, target_size.y, GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT, GL_NEAREST);
  2314. }
  2315. glBindFramebuffer(GL_FRAMEBUFFER, fbo_rt);
  2316. } else if ((fbo_msaa_3d != 0 && msaa3d_needs_resolve) || (fbo_int != 0)) {
  2317. // TODO investigate if it's smarter to cache these FBOs
  2318. GLuint fbos[3]; // read, write and post
  2319. glGenFramebuffers(3, fbos);
  2320. // Resolve if needed.
  2321. if (fbo_msaa_3d != 0 && msaa3d_needs_resolve) {
  2322. GLuint read_color = rb->get_msaa3d_color();
  2323. GLuint read_depth = rb->get_msaa3d_depth();
  2324. GLuint write_color = 0;
  2325. GLuint write_depth = 0;
  2326. if (fbo_int != 0) {
  2327. write_color = rb->get_internal_color();
  2328. write_depth = rb->get_internal_depth();
  2329. } else {
  2330. write_color = texture_storage->render_target_get_color(render_target);
  2331. write_depth = texture_storage->render_target_get_depth(render_target);
  2332. }
  2333. glBindFramebuffer(GL_READ_FRAMEBUFFER, fbos[0]);
  2334. glBindFramebuffer(GL_DRAW_FRAMEBUFFER, fbos[1]);
  2335. for (uint32_t v = 0; v < view_count; v++) {
  2336. glFramebufferTextureLayer(GL_READ_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, read_color, 0, v);
  2337. glFramebufferTextureLayer(GL_READ_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT, read_depth, 0, v);
  2338. glFramebufferTextureLayer(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, write_color, 0, v);
  2339. glFramebufferTextureLayer(GL_DRAW_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT, write_depth, 0, v);
  2340. glBlitFramebuffer(0, 0, internal_size.x, internal_size.y, 0, 0, internal_size.x, internal_size.y, GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT, GL_NEAREST);
  2341. }
  2342. }
  2343. // Rendered to intermediate buffer, must copy to our render target
  2344. if (fbo_int != 0) {
  2345. // Apply glow/bloom if requested? then populate our glow buffers
  2346. const GLES3::Glow::GLOWLEVEL *glow_buffers = nullptr;
  2347. GLuint source_color = fbo_int != 0 ? rb->get_internal_color() : texture_storage->render_target_get_color(render_target);
  2348. if (glow_enabled) {
  2349. glow_buffers = rb->get_glow_buffers();
  2350. glow->set_luminance_multiplier(p_render_data->luminance_multiplier);
  2351. glow->set_intensity(glow_intensity);
  2352. glow->set_glow_bloom(glow_bloom);
  2353. glow->set_glow_hdr_bleed_threshold(glow_hdr_bleed_threshold);
  2354. glow->set_glow_hdr_bleed_scale(glow_hdr_bleed_scale);
  2355. glow->set_glow_hdr_luminance_cap(glow_hdr_luminance_cap);
  2356. }
  2357. GLuint write_color = texture_storage->render_target_get_color(render_target);
  2358. for (uint32_t v = 0; v < view_count; v++) {
  2359. if (glow_enabled) {
  2360. glow->process_glow(source_color, internal_size, glow_buffers, v, true);
  2361. }
  2362. glBindFramebuffer(GL_FRAMEBUFFER, fbos[2]);
  2363. glFramebufferTextureLayer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, write_color, 0, v);
  2364. post_effects->post_copy(fbos[2], target_size, source_color, internal_size, p_render_data->luminance_multiplier, glow_buffers, glow_intensity, v, true, bcs_spec_constants);
  2365. }
  2366. // Copy depth
  2367. GLuint read_depth = rb->get_internal_depth();
  2368. GLuint write_depth = texture_storage->render_target_get_depth(render_target);
  2369. glBindFramebuffer(GL_READ_FRAMEBUFFER, fbos[0]);
  2370. glBindFramebuffer(GL_DRAW_FRAMEBUFFER, fbos[1]);
  2371. for (uint32_t v = 0; v < view_count; v++) {
  2372. glFramebufferTextureLayer(GL_READ_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT, read_depth, 0, v);
  2373. glFramebufferTextureLayer(GL_DRAW_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT, write_depth, 0, v);
  2374. glBlitFramebuffer(0, 0, internal_size.x, internal_size.y, 0, 0, target_size.x, target_size.y, GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT, GL_NEAREST);
  2375. }
  2376. }
  2377. glBindFramebuffer(GL_FRAMEBUFFER, fbo_rt);
  2378. glDeleteFramebuffers(3, fbos);
  2379. }
  2380. glActiveTexture(GL_TEXTURE2);
  2381. glBindTexture(GL_TEXTURE_2D, 0);
  2382. }
  2383. template <PassMode p_pass_mode>
  2384. void RasterizerSceneGLES3::_render_list_template(RenderListParameters *p_params, const RenderDataGLES3 *p_render_data, uint32_t p_from_element, uint32_t p_to_element, bool p_alpha_pass) {
  2385. GLES3::MeshStorage *mesh_storage = GLES3::MeshStorage::get_singleton();
  2386. GLES3::ParticlesStorage *particles_storage = GLES3::ParticlesStorage::get_singleton();
  2387. GLES3::MaterialStorage *material_storage = GLES3::MaterialStorage::get_singleton();
  2388. GLuint prev_vertex_array_gl = 0;
  2389. GLuint prev_index_array_gl = 0;
  2390. GLES3::SceneMaterialData *prev_material_data = nullptr;
  2391. GLES3::SceneShaderData *prev_shader = nullptr;
  2392. GeometryInstanceGLES3 *prev_inst = nullptr;
  2393. SceneShaderGLES3::ShaderVariant prev_variant = SceneShaderGLES3::ShaderVariant::MODE_COLOR;
  2394. SceneShaderGLES3::ShaderVariant shader_variant = SceneShaderGLES3::MODE_COLOR; // Assigned to silence wrong -Wmaybe-initialized
  2395. uint64_t prev_spec_constants = 0;
  2396. // Specializations constants used by all instances in the scene.
  2397. uint64_t base_spec_constants = p_params->spec_constant_base_flags;
  2398. if constexpr (p_pass_mode == PASS_MODE_COLOR || p_pass_mode == PASS_MODE_COLOR_TRANSPARENT) {
  2399. GLES3::TextureStorage *texture_storage = GLES3::TextureStorage::get_singleton();
  2400. GLES3::Config *config = GLES3::Config::get_singleton();
  2401. glActiveTexture(GL_TEXTURE0 + config->max_texture_image_units - 2);
  2402. GLuint texture_to_bind = texture_storage->get_texture(texture_storage->texture_gl_get_default(GLES3::DEFAULT_GL_TEXTURE_CUBEMAP_BLACK))->tex_id;
  2403. if (p_render_data->environment.is_valid()) {
  2404. Sky *sky = sky_owner.get_or_null(environment_get_sky(p_render_data->environment));
  2405. if (sky && sky->radiance != 0) {
  2406. texture_to_bind = sky->radiance;
  2407. base_spec_constants |= SceneShaderGLES3::USE_RADIANCE_MAP;
  2408. }
  2409. glBindTexture(GL_TEXTURE_CUBE_MAP, texture_to_bind);
  2410. }
  2411. } else if constexpr (p_pass_mode == PASS_MODE_DEPTH || p_pass_mode == PASS_MODE_SHADOW) {
  2412. shader_variant = SceneShaderGLES3::MODE_DEPTH;
  2413. }
  2414. if (p_render_data->view_count > 1) {
  2415. base_spec_constants |= SceneShaderGLES3::USE_MULTIVIEW;
  2416. }
  2417. bool should_request_redraw = false;
  2418. if constexpr (p_pass_mode != PASS_MODE_DEPTH) {
  2419. // Don't count elements during depth pre-pass to match the RD renderers.
  2420. if (p_render_data->render_info) {
  2421. p_render_data->render_info->info[RS::VIEWPORT_RENDER_INFO_TYPE_VISIBLE][RS::VIEWPORT_RENDER_INFO_OBJECTS_IN_FRAME] += p_to_element - p_from_element;
  2422. }
  2423. }
  2424. for (uint32_t i = p_from_element; i < p_to_element; i++) {
  2425. GeometryInstanceSurface *surf = p_params->elements[i];
  2426. GeometryInstanceGLES3 *inst = surf->owner;
  2427. if (p_pass_mode == PASS_MODE_COLOR && !(surf->flags & GeometryInstanceSurface::FLAG_PASS_OPAQUE)) {
  2428. continue; // Objects with "Depth-prepass" transparency are included in both render lists, but should only be rendered in the transparent pass
  2429. }
  2430. if (inst->instance_count == 0) {
  2431. continue;
  2432. }
  2433. GLES3::SceneShaderData *shader;
  2434. GLES3::SceneMaterialData *material_data;
  2435. void *mesh_surface;
  2436. if constexpr (p_pass_mode == PASS_MODE_SHADOW) {
  2437. shader = surf->shader_shadow;
  2438. material_data = surf->material_shadow;
  2439. mesh_surface = surf->surface_shadow;
  2440. } else {
  2441. if (unlikely(get_debug_draw_mode() == RS::VIEWPORT_DEBUG_DRAW_OVERDRAW)) {
  2442. material_data = overdraw_material_data_ptr;
  2443. shader = material_data->shader_data;
  2444. } else if (unlikely(get_debug_draw_mode() == RS::VIEWPORT_DEBUG_DRAW_LIGHTING)) {
  2445. material_data = default_material_data_ptr;
  2446. shader = material_data->shader_data;
  2447. } else {
  2448. shader = surf->shader;
  2449. material_data = surf->material;
  2450. }
  2451. mesh_surface = surf->surface;
  2452. }
  2453. if (!mesh_surface) {
  2454. continue;
  2455. }
  2456. //request a redraw if one of the shaders uses TIME
  2457. if (shader->uses_time) {
  2458. should_request_redraw = true;
  2459. }
  2460. if constexpr (p_pass_mode == PASS_MODE_COLOR_TRANSPARENT) {
  2461. scene_state.enable_gl_depth_test(shader->depth_test != GLES3::SceneShaderData::DEPTH_TEST_DISABLED);
  2462. }
  2463. if (shader->depth_test == GLES3::SceneShaderData::DEPTH_TEST_ENABLED_INVERTED) {
  2464. scene_state.set_gl_depth_func(GL_LESS);
  2465. } else {
  2466. scene_state.set_gl_depth_func(GL_GEQUAL);
  2467. }
  2468. if constexpr (p_pass_mode != PASS_MODE_SHADOW) {
  2469. if (shader->depth_draw == GLES3::SceneShaderData::DEPTH_DRAW_OPAQUE) {
  2470. scene_state.enable_gl_depth_draw((p_pass_mode == PASS_MODE_COLOR && !GLES3::Config::get_singleton()->use_depth_prepass) || p_pass_mode == PASS_MODE_DEPTH);
  2471. } else {
  2472. scene_state.enable_gl_depth_draw(shader->depth_draw == GLES3::SceneShaderData::DEPTH_DRAW_ALWAYS);
  2473. }
  2474. }
  2475. bool uses_additive_lighting = (inst->light_passes.size() + p_render_data->directional_shadow_count) > 0;
  2476. uses_additive_lighting = uses_additive_lighting && !shader->unshaded;
  2477. // TODOS
  2478. /*
  2479. * Still a bug when atlas space is limited. Somehow need to evict light when it doesn't have a spot on the atlas, current check isn't enough
  2480. * Disable depth draw
  2481. */
  2482. for (int32_t pass = 0; pass < MAX(1, int32_t(inst->light_passes.size() + p_render_data->directional_shadow_count)); pass++) {
  2483. if constexpr (p_pass_mode == PASS_MODE_DEPTH || p_pass_mode == PASS_MODE_SHADOW) {
  2484. if (pass > 0) {
  2485. // Don't render shadow passes when doing depth or shadow pass.
  2486. break;
  2487. }
  2488. }
  2489. // Stencil.
  2490. if (shader->stencil_enabled) {
  2491. static const GLenum stencil_compare_table[GLES3::SceneShaderData::STENCIL_COMPARE_MAX] = {
  2492. GL_LESS,
  2493. GL_EQUAL,
  2494. GL_LEQUAL,
  2495. GL_GREATER,
  2496. GL_NOTEQUAL,
  2497. GL_GEQUAL,
  2498. GL_ALWAYS,
  2499. };
  2500. GLenum stencil_compare = stencil_compare_table[shader->stencil_compare];
  2501. GLuint stencil_compare_mask = 0;
  2502. GLuint stencil_write_mask = 0;
  2503. GLenum stencil_op_dpfail = GL_KEEP;
  2504. GLenum stencil_op_dppass = GL_KEEP;
  2505. if (shader->stencil_flags & GLES3::SceneShaderData::STENCIL_FLAG_READ) {
  2506. stencil_compare_mask = 255;
  2507. }
  2508. if (shader->stencil_flags & GLES3::SceneShaderData::STENCIL_FLAG_WRITE) {
  2509. stencil_op_dppass = GL_REPLACE;
  2510. stencil_write_mask = 255;
  2511. }
  2512. if (shader->stencil_flags & GLES3::SceneShaderData::STENCIL_FLAG_WRITE_DEPTH_FAIL) {
  2513. stencil_op_dpfail = GL_REPLACE;
  2514. stencil_write_mask = 255;
  2515. }
  2516. scene_state.enable_gl_stencil_test(true);
  2517. scene_state.set_gl_stencil_func(stencil_compare, shader->stencil_reference, stencil_compare_mask);
  2518. scene_state.set_gl_stencil_write_mask(stencil_write_mask);
  2519. scene_state.set_gl_stencil_op(GL_KEEP, stencil_op_dpfail, stencil_op_dppass);
  2520. } else {
  2521. scene_state.enable_gl_stencil_test(false);
  2522. scene_state.set_gl_stencil_write_mask(255);
  2523. }
  2524. if constexpr (p_pass_mode == PASS_MODE_COLOR || p_pass_mode == PASS_MODE_COLOR_TRANSPARENT) {
  2525. if (!uses_additive_lighting && pass == 1) {
  2526. // Don't render additive passes if not using additive lighting.
  2527. break;
  2528. }
  2529. if (uses_additive_lighting && pass == 1 && !p_render_data->transparent_bg) {
  2530. // Enable blending if in opaque pass and not already enabled.
  2531. scene_state.enable_gl_blend(true);
  2532. }
  2533. if (pass < int32_t(inst->light_passes.size())) {
  2534. RID light_instance_rid = inst->light_passes[pass].light_instance_rid;
  2535. if (!GLES3::LightStorage::get_singleton()->light_instance_has_shadow_atlas(light_instance_rid, p_render_data->shadow_atlas)) {
  2536. // Shadow wasn't able to get a spot on the atlas. So skip it.
  2537. continue;
  2538. }
  2539. } else if (pass > 0) {
  2540. uint32_t shadow_id = MAX_DIRECTIONAL_LIGHTS - 1 - (pass - int32_t(inst->light_passes.size()));
  2541. if (inst->lightmap_instance.is_valid() && scene_state.directional_lights[shadow_id].bake_mode == RenderingServer::LIGHT_BAKE_STATIC) {
  2542. // Skip shadows for static lights on meshes with a lightmap.
  2543. continue;
  2544. }
  2545. }
  2546. }
  2547. if constexpr (p_pass_mode == PASS_MODE_COLOR || p_pass_mode == PASS_MODE_COLOR_TRANSPARENT) {
  2548. GLES3::SceneShaderData::BlendMode desired_blend_mode;
  2549. if (pass > 0) {
  2550. desired_blend_mode = GLES3::SceneShaderData::BLEND_MODE_ADD;
  2551. } else {
  2552. desired_blend_mode = shader->blend_mode;
  2553. }
  2554. if (desired_blend_mode != scene_state.current_blend_mode) {
  2555. switch (desired_blend_mode) {
  2556. case GLES3::SceneShaderData::BLEND_MODE_MIX: {
  2557. glBlendEquation(GL_FUNC_ADD);
  2558. if (p_render_data->transparent_bg) {
  2559. glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
  2560. } else {
  2561. glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_ZERO, GL_ONE);
  2562. }
  2563. } break;
  2564. case GLES3::SceneShaderData::BLEND_MODE_ADD: {
  2565. glBlendEquation(GL_FUNC_ADD);
  2566. glBlendFunc(p_pass_mode == PASS_MODE_COLOR_TRANSPARENT ? GL_SRC_ALPHA : GL_ONE, GL_ONE);
  2567. } break;
  2568. case GLES3::SceneShaderData::BLEND_MODE_SUB: {
  2569. glBlendEquation(GL_FUNC_REVERSE_SUBTRACT);
  2570. glBlendFunc(GL_SRC_ALPHA, GL_ONE);
  2571. } break;
  2572. case GLES3::SceneShaderData::BLEND_MODE_MUL: {
  2573. glBlendEquation(GL_FUNC_ADD);
  2574. if (p_render_data->transparent_bg) {
  2575. glBlendFuncSeparate(GL_DST_COLOR, GL_ZERO, GL_DST_ALPHA, GL_ZERO);
  2576. } else {
  2577. glBlendFuncSeparate(GL_DST_COLOR, GL_ZERO, GL_ZERO, GL_ONE);
  2578. }
  2579. } break;
  2580. case GLES3::SceneShaderData::BLEND_MODE_PREMULT_ALPHA: {
  2581. glBlendEquation(GL_FUNC_ADD);
  2582. glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
  2583. } break;
  2584. case GLES3::SceneShaderData::BLEND_MODE_ALPHA_TO_COVERAGE: {
  2585. // Do nothing for now.
  2586. } break;
  2587. }
  2588. scene_state.current_blend_mode = desired_blend_mode;
  2589. }
  2590. }
  2591. // Find cull variant.
  2592. RS::CullMode cull_mode = shader->cull_mode;
  2593. if (p_pass_mode == PASS_MODE_MATERIAL || (surf->flags & GeometryInstanceSurface::FLAG_USES_DOUBLE_SIDED_SHADOWS)) {
  2594. cull_mode = RS::CULL_MODE_DISABLED;
  2595. } else {
  2596. bool mirror = inst->mirror;
  2597. if (p_params->reverse_cull) {
  2598. mirror = !mirror;
  2599. }
  2600. if (cull_mode == RS::CULL_MODE_FRONT && mirror) {
  2601. cull_mode = RS::CULL_MODE_BACK;
  2602. } else if (cull_mode == RS::CULL_MODE_BACK && mirror) {
  2603. cull_mode = RS::CULL_MODE_FRONT;
  2604. }
  2605. }
  2606. scene_state.set_gl_cull_mode(cull_mode);
  2607. RS::PrimitiveType primitive = surf->primitive;
  2608. if (shader->uses_point_size) {
  2609. primitive = RS::PRIMITIVE_POINTS;
  2610. }
  2611. static const GLenum prim[5] = { GL_POINTS, GL_LINES, GL_LINE_STRIP, GL_TRIANGLES, GL_TRIANGLE_STRIP };
  2612. GLenum primitive_gl = prim[int(primitive)];
  2613. GLuint vertex_array_gl = 0;
  2614. GLuint index_array_gl = 0;
  2615. uint64_t vertex_input_mask = shader->vertex_input_mask;
  2616. if (inst->lightmap_instance.is_valid() || p_pass_mode == PASS_MODE_MATERIAL) {
  2617. vertex_input_mask |= 1 << RS::ARRAY_TEX_UV2;
  2618. }
  2619. // Skeleton and blend shapes.
  2620. if (surf->owner->mesh_instance.is_valid()) {
  2621. mesh_storage->mesh_instance_surface_get_vertex_arrays_and_format(surf->owner->mesh_instance, surf->surface_index, vertex_input_mask, vertex_array_gl);
  2622. } else {
  2623. mesh_storage->mesh_surface_get_vertex_arrays_and_format(mesh_surface, vertex_input_mask, vertex_array_gl);
  2624. }
  2625. index_array_gl = mesh_storage->mesh_surface_get_index_buffer(mesh_surface, surf->lod_index);
  2626. if (prev_vertex_array_gl != vertex_array_gl) {
  2627. if (vertex_array_gl != 0) {
  2628. glBindVertexArray(vertex_array_gl);
  2629. }
  2630. prev_vertex_array_gl = vertex_array_gl;
  2631. // Invalidate the previous index array
  2632. prev_index_array_gl = 0;
  2633. }
  2634. bool use_wireframe = false;
  2635. if (p_params->force_wireframe || shader->wireframe) {
  2636. GLuint wireframe_index_array_gl = mesh_storage->mesh_surface_get_index_buffer_wireframe(mesh_surface);
  2637. if (wireframe_index_array_gl) {
  2638. index_array_gl = wireframe_index_array_gl;
  2639. use_wireframe = true;
  2640. }
  2641. }
  2642. bool use_index_buffer = index_array_gl != 0;
  2643. if (prev_index_array_gl != index_array_gl) {
  2644. if (index_array_gl != 0) {
  2645. // Bind index each time so we can use LODs
  2646. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, index_array_gl);
  2647. }
  2648. prev_index_array_gl = index_array_gl;
  2649. }
  2650. Transform3D world_transform;
  2651. if (inst->store_transform_cache) {
  2652. world_transform = inst->transform;
  2653. }
  2654. if (prev_material_data != material_data) {
  2655. material_data->bind_uniforms();
  2656. prev_material_data = material_data;
  2657. }
  2658. SceneShaderGLES3::ShaderVariant instance_variant = shader_variant;
  2659. if (inst->instance_count > 0) {
  2660. // Will need to use instancing to draw (either MultiMesh or Particles).
  2661. instance_variant = SceneShaderGLES3::ShaderVariant(1 + int(instance_variant));
  2662. }
  2663. uint64_t spec_constants = base_spec_constants;
  2664. // Set up spec constants for lighting.
  2665. if constexpr (p_pass_mode == PASS_MODE_COLOR || p_pass_mode == PASS_MODE_COLOR_TRANSPARENT) {
  2666. // Only check during color passes as light shader code is compiled out during depth-only pass anyway.
  2667. if (pass == 0) {
  2668. spec_constants |= SceneShaderGLES3::BASE_PASS;
  2669. if (get_debug_draw_mode() == RS::VIEWPORT_DEBUG_DRAW_UNSHADED) {
  2670. spec_constants |= SceneShaderGLES3::DISABLE_LIGHT_OMNI;
  2671. spec_constants |= SceneShaderGLES3::DISABLE_LIGHT_SPOT;
  2672. spec_constants |= SceneShaderGLES3::DISABLE_LIGHT_DIRECTIONAL;
  2673. spec_constants |= SceneShaderGLES3::DISABLE_LIGHTMAP;
  2674. } else {
  2675. if (inst->omni_light_gl_cache.is_empty()) {
  2676. spec_constants |= SceneShaderGLES3::DISABLE_LIGHT_OMNI;
  2677. }
  2678. if (inst->spot_light_gl_cache.is_empty()) {
  2679. spec_constants |= SceneShaderGLES3::DISABLE_LIGHT_SPOT;
  2680. }
  2681. if (p_render_data->directional_light_count == p_render_data->directional_shadow_count) {
  2682. spec_constants |= SceneShaderGLES3::DISABLE_LIGHT_DIRECTIONAL;
  2683. }
  2684. if (inst->reflection_probe_rid_cache.is_empty()) {
  2685. // We don't have any probes.
  2686. spec_constants |= SceneShaderGLES3::DISABLE_REFLECTION_PROBE;
  2687. } else if (inst->reflection_probe_rid_cache.size() > 1) {
  2688. // We have a second probe.
  2689. spec_constants |= SceneShaderGLES3::SECOND_REFLECTION_PROBE;
  2690. }
  2691. if (inst->lightmap_instance.is_valid()) {
  2692. spec_constants |= SceneShaderGLES3::USE_LIGHTMAP;
  2693. GLES3::LightmapInstance *li = GLES3::LightStorage::get_singleton()->get_lightmap_instance(inst->lightmap_instance);
  2694. GLES3::Lightmap *lm = GLES3::LightStorage::get_singleton()->get_lightmap(li->lightmap);
  2695. if (lm->uses_spherical_harmonics) {
  2696. spec_constants |= SceneShaderGLES3::USE_SH_LIGHTMAP;
  2697. }
  2698. if (lightmap_bicubic_upscale) {
  2699. spec_constants |= SceneShaderGLES3::LIGHTMAP_BICUBIC_FILTER;
  2700. }
  2701. } else if (inst->lightmap_sh) {
  2702. spec_constants |= SceneShaderGLES3::USE_LIGHTMAP_CAPTURE;
  2703. } else {
  2704. spec_constants |= SceneShaderGLES3::DISABLE_LIGHTMAP;
  2705. }
  2706. }
  2707. } else {
  2708. // Only base pass uses the radiance map.
  2709. spec_constants &= ~SceneShaderGLES3::USE_RADIANCE_MAP;
  2710. spec_constants |= SceneShaderGLES3::DISABLE_LIGHT_OMNI;
  2711. spec_constants |= SceneShaderGLES3::DISABLE_LIGHT_SPOT;
  2712. spec_constants |= SceneShaderGLES3::DISABLE_LIGHT_DIRECTIONAL;
  2713. spec_constants |= SceneShaderGLES3::DISABLE_REFLECTION_PROBE;
  2714. bool disable_lightmaps = true;
  2715. // Additive directional passes may use shadowmasks, so enable lightmaps for them.
  2716. if (pass >= int32_t(inst->light_passes.size()) && inst->lightmap_instance.is_valid()) {
  2717. GLES3::LightmapInstance *li = GLES3::LightStorage::get_singleton()->get_lightmap_instance(inst->lightmap_instance);
  2718. GLES3::Lightmap *lm = GLES3::LightStorage::get_singleton()->get_lightmap(li->lightmap);
  2719. if (lm->shadowmask_mode != RS::SHADOWMASK_MODE_NONE) {
  2720. spec_constants |= SceneShaderGLES3::USE_LIGHTMAP;
  2721. disable_lightmaps = false;
  2722. if (lightmap_bicubic_upscale) {
  2723. spec_constants |= SceneShaderGLES3::LIGHTMAP_BICUBIC_FILTER;
  2724. }
  2725. }
  2726. }
  2727. if (disable_lightmaps) {
  2728. spec_constants |= SceneShaderGLES3::DISABLE_LIGHTMAP;
  2729. }
  2730. }
  2731. if (uses_additive_lighting) {
  2732. spec_constants |= SceneShaderGLES3::USE_ADDITIVE_LIGHTING;
  2733. if (pass < int32_t(inst->light_passes.size())) {
  2734. // Rendering positional lights.
  2735. if (inst->light_passes[pass].is_omni) {
  2736. spec_constants |= SceneShaderGLES3::ADDITIVE_OMNI;
  2737. } else {
  2738. spec_constants |= SceneShaderGLES3::ADDITIVE_SPOT;
  2739. }
  2740. if (scene_state.positional_shadow_quality >= RS::SHADOW_QUALITY_SOFT_HIGH) {
  2741. spec_constants |= SceneShaderGLES3::SHADOW_MODE_PCF_13;
  2742. } else if (scene_state.positional_shadow_quality >= RS::SHADOW_QUALITY_SOFT_LOW) {
  2743. spec_constants |= SceneShaderGLES3::SHADOW_MODE_PCF_5;
  2744. }
  2745. } else {
  2746. // Render directional lights.
  2747. uint32_t shadow_id = MAX_DIRECTIONAL_LIGHTS - 1 - (pass - int32_t(inst->light_passes.size()));
  2748. if (pass == 0 && inst->lightmap_instance.is_valid() && scene_state.directional_lights[shadow_id].bake_mode == RenderingServer::LIGHT_BAKE_STATIC) {
  2749. // Disable additive lighting with a static light and a lightmap.
  2750. spec_constants &= ~SceneShaderGLES3::USE_ADDITIVE_LIGHTING;
  2751. }
  2752. if (scene_state.directional_shadows[shadow_id].shadow_split_offsets[0] == scene_state.directional_shadows[shadow_id].shadow_split_offsets[1]) {
  2753. // Orthogonal, do nothing.
  2754. } else if (scene_state.directional_shadows[shadow_id].shadow_split_offsets[1] == scene_state.directional_shadows[shadow_id].shadow_split_offsets[2]) {
  2755. spec_constants |= SceneShaderGLES3::LIGHT_USE_PSSM2;
  2756. } else {
  2757. spec_constants |= SceneShaderGLES3::LIGHT_USE_PSSM4;
  2758. }
  2759. if (scene_state.directional_shadows[shadow_id].blend_splits) {
  2760. spec_constants |= SceneShaderGLES3::LIGHT_USE_PSSM_BLEND;
  2761. }
  2762. if (scene_state.directional_shadow_quality >= RS::SHADOW_QUALITY_SOFT_HIGH) {
  2763. spec_constants |= SceneShaderGLES3::SHADOW_MODE_PCF_13;
  2764. } else if (scene_state.directional_shadow_quality >= RS::SHADOW_QUALITY_SOFT_LOW) {
  2765. spec_constants |= SceneShaderGLES3::SHADOW_MODE_PCF_5;
  2766. }
  2767. }
  2768. }
  2769. }
  2770. if (prev_shader != shader || prev_variant != instance_variant || spec_constants != prev_spec_constants) {
  2771. bool success = material_storage->shaders.scene_shader.version_bind_shader(shader->version, instance_variant, spec_constants);
  2772. if (!success) {
  2773. break;
  2774. }
  2775. float opaque_prepass_threshold = 0.0;
  2776. if constexpr (p_pass_mode == PASS_MODE_DEPTH) {
  2777. opaque_prepass_threshold = 0.99;
  2778. } else if constexpr (p_pass_mode == PASS_MODE_SHADOW) {
  2779. opaque_prepass_threshold = 0.1;
  2780. }
  2781. material_storage->shaders.scene_shader.version_set_uniform(SceneShaderGLES3::OPAQUE_PREPASS_THRESHOLD, opaque_prepass_threshold, shader->version, instance_variant, spec_constants);
  2782. }
  2783. // Pass in lighting uniforms.
  2784. if constexpr (p_pass_mode == PASS_MODE_COLOR || p_pass_mode == PASS_MODE_COLOR_TRANSPARENT) {
  2785. GLES3::Config *config = GLES3::Config::get_singleton();
  2786. // Pass light and shadow index and bind shadow texture.
  2787. if (uses_additive_lighting) {
  2788. if (pass < int32_t(inst->light_passes.size())) {
  2789. int32_t shadow_id = inst->light_passes[pass].shadow_id;
  2790. if (shadow_id >= 0) {
  2791. uint32_t light_id = inst->light_passes[pass].light_id;
  2792. bool is_omni = inst->light_passes[pass].is_omni;
  2793. SceneShaderGLES3::Uniforms uniform_name = is_omni ? SceneShaderGLES3::OMNI_LIGHT_INDEX : SceneShaderGLES3::SPOT_LIGHT_INDEX;
  2794. material_storage->shaders.scene_shader.version_set_uniform(uniform_name, uint32_t(light_id), shader->version, instance_variant, spec_constants);
  2795. material_storage->shaders.scene_shader.version_set_uniform(SceneShaderGLES3::POSITIONAL_SHADOW_INDEX, uint32_t(shadow_id), shader->version, instance_variant, spec_constants);
  2796. glActiveTexture(GL_TEXTURE0 + config->max_texture_image_units - 3);
  2797. RID light_instance_rid = inst->light_passes[pass].light_instance_rid;
  2798. GLuint tex = GLES3::LightStorage::get_singleton()->light_instance_get_shadow_texture(light_instance_rid, p_render_data->shadow_atlas);
  2799. if (is_omni) {
  2800. glBindTexture(GL_TEXTURE_CUBE_MAP, tex);
  2801. } else {
  2802. glBindTexture(GL_TEXTURE_2D, tex);
  2803. }
  2804. }
  2805. } else {
  2806. uint32_t shadow_id = MAX_DIRECTIONAL_LIGHTS - 1 - (pass - int32_t(inst->light_passes.size()));
  2807. material_storage->shaders.scene_shader.version_set_uniform(SceneShaderGLES3::DIRECTIONAL_SHADOW_INDEX, shadow_id, shader->version, instance_variant, spec_constants);
  2808. GLuint tex = GLES3::LightStorage::get_singleton()->directional_shadow_get_texture();
  2809. glActiveTexture(GL_TEXTURE0 + config->max_texture_image_units - 3);
  2810. glBindTexture(GL_TEXTURE_2D, tex);
  2811. if (inst->lightmap_instance.is_valid()) {
  2812. // Use shadowmasks for directional light passes.
  2813. GLES3::LightmapInstance *li = GLES3::LightStorage::get_singleton()->get_lightmap_instance(inst->lightmap_instance);
  2814. GLES3::Lightmap *lm = GLES3::LightStorage::get_singleton()->get_lightmap(li->lightmap);
  2815. material_storage->shaders.scene_shader.version_set_uniform(SceneShaderGLES3::LIGHTMAP_SLICE, inst->lightmap_slice_index, shader->version, instance_variant, spec_constants);
  2816. Vector4 uv_scale(inst->lightmap_uv_scale.position.x, inst->lightmap_uv_scale.position.y, inst->lightmap_uv_scale.size.x, inst->lightmap_uv_scale.size.y);
  2817. material_storage->shaders.scene_shader.version_set_uniform(SceneShaderGLES3::LIGHTMAP_UV_SCALE, uv_scale, shader->version, instance_variant, spec_constants);
  2818. if (lightmap_bicubic_upscale) {
  2819. Vector2 light_texture_size(lm->light_texture_size.x, lm->light_texture_size.y);
  2820. material_storage->shaders.scene_shader.version_set_uniform(SceneShaderGLES3::LIGHTMAP_TEXTURE_SIZE, light_texture_size, shader->version, instance_variant, spec_constants);
  2821. }
  2822. material_storage->shaders.scene_shader.version_set_uniform(SceneShaderGLES3::LIGHTMAP_SHADOWMASK_MODE, (uint32_t)lm->shadowmask_mode, shader->version, instance_variant, spec_constants);
  2823. if (lm->shadow_texture.is_valid()) {
  2824. tex = GLES3::TextureStorage::get_singleton()->texture_get_texid(lm->shadow_texture);
  2825. } else {
  2826. tex = GLES3::TextureStorage::get_singleton()->texture_get_texid(GLES3::TextureStorage::get_singleton()->texture_gl_get_default(GLES3::DEFAULT_GL_TEXTURE_2D_ARRAY_WHITE));
  2827. }
  2828. glActiveTexture(GL_TEXTURE0 + config->max_texture_image_units - 5);
  2829. glBindTexture(GL_TEXTURE_2D_ARRAY, tex);
  2830. }
  2831. }
  2832. }
  2833. // Pass light count and array of light indices for base pass.
  2834. if ((prev_inst != inst || prev_shader != shader || prev_variant != instance_variant || prev_spec_constants != spec_constants) && pass == 0) {
  2835. // Rebind the light indices.
  2836. material_storage->shaders.scene_shader.version_set_uniform(SceneShaderGLES3::OMNI_LIGHT_COUNT, inst->omni_light_gl_cache.size(), shader->version, instance_variant, spec_constants);
  2837. material_storage->shaders.scene_shader.version_set_uniform(SceneShaderGLES3::SPOT_LIGHT_COUNT, inst->spot_light_gl_cache.size(), shader->version, instance_variant, spec_constants);
  2838. if (inst->omni_light_gl_cache.size()) {
  2839. glUniform1uiv(material_storage->shaders.scene_shader.version_get_uniform(SceneShaderGLES3::OMNI_LIGHT_INDICES, shader->version, instance_variant, spec_constants), inst->omni_light_gl_cache.size(), inst->omni_light_gl_cache.ptr());
  2840. }
  2841. if (inst->spot_light_gl_cache.size()) {
  2842. glUniform1uiv(material_storage->shaders.scene_shader.version_get_uniform(SceneShaderGLES3::SPOT_LIGHT_INDICES, shader->version, instance_variant, spec_constants), inst->spot_light_gl_cache.size(), inst->spot_light_gl_cache.ptr());
  2843. }
  2844. if (inst->lightmap_instance.is_valid()) {
  2845. GLES3::LightmapInstance *li = GLES3::LightStorage::get_singleton()->get_lightmap_instance(inst->lightmap_instance);
  2846. GLES3::Lightmap *lm = GLES3::LightStorage::get_singleton()->get_lightmap(li->lightmap);
  2847. GLuint tex = GLES3::TextureStorage::get_singleton()->texture_get_texid(lm->light_texture);
  2848. glActiveTexture(GL_TEXTURE0 + config->max_texture_image_units - 4);
  2849. glBindTexture(GL_TEXTURE_2D_ARRAY, tex);
  2850. material_storage->shaders.scene_shader.version_set_uniform(SceneShaderGLES3::LIGHTMAP_SLICE, inst->lightmap_slice_index, shader->version, instance_variant, spec_constants);
  2851. Vector4 uv_scale(inst->lightmap_uv_scale.position.x, inst->lightmap_uv_scale.position.y, inst->lightmap_uv_scale.size.x, inst->lightmap_uv_scale.size.y);
  2852. material_storage->shaders.scene_shader.version_set_uniform(SceneShaderGLES3::LIGHTMAP_UV_SCALE, uv_scale, shader->version, instance_variant, spec_constants);
  2853. if (lightmap_bicubic_upscale) {
  2854. Vector2 light_texture_size(lm->light_texture_size.x, lm->light_texture_size.y);
  2855. material_storage->shaders.scene_shader.version_set_uniform(SceneShaderGLES3::LIGHTMAP_TEXTURE_SIZE, light_texture_size, shader->version, instance_variant, spec_constants);
  2856. }
  2857. float exposure_normalization = 1.0;
  2858. if (p_render_data->camera_attributes.is_valid()) {
  2859. float enf = RSG::camera_attributes->camera_attributes_get_exposure_normalization_factor(p_render_data->camera_attributes);
  2860. exposure_normalization = enf / lm->baked_exposure;
  2861. }
  2862. material_storage->shaders.scene_shader.version_set_uniform(SceneShaderGLES3::LIGHTMAP_EXPOSURE_NORMALIZATION, exposure_normalization, shader->version, instance_variant, spec_constants);
  2863. if (lm->uses_spherical_harmonics) {
  2864. Basis to_lm = li->transform.basis.inverse() * p_render_data->cam_transform.basis;
  2865. to_lm = to_lm.inverse().transposed();
  2866. GLfloat matrix[9] = {
  2867. (GLfloat)to_lm.rows[0][0],
  2868. (GLfloat)to_lm.rows[1][0],
  2869. (GLfloat)to_lm.rows[2][0],
  2870. (GLfloat)to_lm.rows[0][1],
  2871. (GLfloat)to_lm.rows[1][1],
  2872. (GLfloat)to_lm.rows[2][1],
  2873. (GLfloat)to_lm.rows[0][2],
  2874. (GLfloat)to_lm.rows[1][2],
  2875. (GLfloat)to_lm.rows[2][2],
  2876. };
  2877. glUniformMatrix3fv(material_storage->shaders.scene_shader.version_get_uniform(SceneShaderGLES3::LIGHTMAP_NORMAL_XFORM, shader->version, instance_variant, spec_constants), 1, GL_FALSE, matrix);
  2878. }
  2879. } else if (inst->lightmap_sh) {
  2880. glUniform4fv(material_storage->shaders.scene_shader.version_get_uniform(SceneShaderGLES3::LIGHTMAP_CAPTURES, shader->version, instance_variant, spec_constants), 9, reinterpret_cast<const GLfloat *>(inst->lightmap_sh->sh));
  2881. }
  2882. prev_inst = inst;
  2883. }
  2884. }
  2885. prev_shader = shader;
  2886. prev_variant = instance_variant;
  2887. prev_spec_constants = spec_constants;
  2888. // Pass in reflection probe data
  2889. if constexpr (p_pass_mode == PASS_MODE_COLOR || p_pass_mode == PASS_MODE_COLOR_TRANSPARENT) {
  2890. if (pass == 0 && inst->reflection_probe_rid_cache.size() > 0) {
  2891. GLES3::Config *config = GLES3::Config::get_singleton();
  2892. GLES3::LightStorage *light_storage = GLES3::LightStorage::get_singleton();
  2893. // Setup first probe.
  2894. {
  2895. RID probe_rid = light_storage->reflection_probe_instance_get_probe(inst->reflection_probe_rid_cache[0]);
  2896. GLES3::ReflectionProbe *probe = light_storage->get_reflection_probe(probe_rid);
  2897. material_storage->shaders.scene_shader.version_set_uniform(SceneShaderGLES3::REFPROBE1_USE_BOX_PROJECT, probe->box_projection, shader->version, instance_variant, spec_constants);
  2898. material_storage->shaders.scene_shader.version_set_uniform(SceneShaderGLES3::REFPROBE1_BOX_EXTENTS, probe->size * 0.5, shader->version, instance_variant, spec_constants);
  2899. material_storage->shaders.scene_shader.version_set_uniform(SceneShaderGLES3::REFPROBE1_BOX_OFFSET, probe->origin_offset, shader->version, instance_variant, spec_constants);
  2900. material_storage->shaders.scene_shader.version_set_uniform(SceneShaderGLES3::REFPROBE1_EXTERIOR, !probe->interior, shader->version, instance_variant, spec_constants);
  2901. material_storage->shaders.scene_shader.version_set_uniform(SceneShaderGLES3::REFPROBE1_INTENSITY, probe->intensity, shader->version, instance_variant, spec_constants);
  2902. material_storage->shaders.scene_shader.version_set_uniform(SceneShaderGLES3::REFPROBE1_AMBIENT_MODE, int(probe->ambient_mode), shader->version, instance_variant, spec_constants);
  2903. material_storage->shaders.scene_shader.version_set_uniform(SceneShaderGLES3::REFPROBE1_AMBIENT_COLOR, probe->ambient_color * probe->ambient_color_energy, shader->version, instance_variant, spec_constants);
  2904. material_storage->shaders.scene_shader.version_set_uniform(SceneShaderGLES3::REFPROBE1_LOCAL_MATRIX, inst->reflection_probes_local_transform_cache[0], shader->version, instance_variant, spec_constants);
  2905. material_storage->shaders.scene_shader.version_set_uniform(SceneShaderGLES3::REFPROBE1_BLEND_DISTANCE, probe->blend_distance, shader->version, instance_variant, spec_constants);
  2906. glActiveTexture(GL_TEXTURE0 + config->max_texture_image_units - 8);
  2907. glBindTexture(GL_TEXTURE_CUBE_MAP, light_storage->reflection_probe_instance_get_texture(inst->reflection_probe_rid_cache[0]));
  2908. }
  2909. if (inst->reflection_probe_rid_cache.size() > 1) {
  2910. // Setup second probe.
  2911. RID probe_rid = light_storage->reflection_probe_instance_get_probe(inst->reflection_probe_rid_cache[1]);
  2912. GLES3::ReflectionProbe *probe = light_storage->get_reflection_probe(probe_rid);
  2913. material_storage->shaders.scene_shader.version_set_uniform(SceneShaderGLES3::REFPROBE2_USE_BOX_PROJECT, probe->box_projection, shader->version, instance_variant, spec_constants);
  2914. material_storage->shaders.scene_shader.version_set_uniform(SceneShaderGLES3::REFPROBE2_BOX_EXTENTS, probe->size * 0.5, shader->version, instance_variant, spec_constants);
  2915. material_storage->shaders.scene_shader.version_set_uniform(SceneShaderGLES3::REFPROBE2_BOX_OFFSET, probe->origin_offset, shader->version, instance_variant, spec_constants);
  2916. material_storage->shaders.scene_shader.version_set_uniform(SceneShaderGLES3::REFPROBE2_EXTERIOR, !probe->interior, shader->version, instance_variant, spec_constants);
  2917. material_storage->shaders.scene_shader.version_set_uniform(SceneShaderGLES3::REFPROBE2_INTENSITY, probe->intensity, shader->version, instance_variant, spec_constants);
  2918. material_storage->shaders.scene_shader.version_set_uniform(SceneShaderGLES3::REFPROBE2_AMBIENT_MODE, int(probe->ambient_mode), shader->version, instance_variant, spec_constants);
  2919. material_storage->shaders.scene_shader.version_set_uniform(SceneShaderGLES3::REFPROBE2_AMBIENT_COLOR, probe->ambient_color * probe->ambient_color_energy, shader->version, instance_variant, spec_constants);
  2920. material_storage->shaders.scene_shader.version_set_uniform(SceneShaderGLES3::REFPROBE2_LOCAL_MATRIX, inst->reflection_probes_local_transform_cache[1], shader->version, instance_variant, spec_constants);
  2921. material_storage->shaders.scene_shader.version_set_uniform(SceneShaderGLES3::REFPROBE2_BLEND_DISTANCE, probe->blend_distance, shader->version, instance_variant, spec_constants);
  2922. glActiveTexture(GL_TEXTURE0 + config->max_texture_image_units - 9);
  2923. glBindTexture(GL_TEXTURE_CUBE_MAP, light_storage->reflection_probe_instance_get_texture(inst->reflection_probe_rid_cache[1]));
  2924. spec_constants |= SceneShaderGLES3::SECOND_REFLECTION_PROBE;
  2925. }
  2926. }
  2927. }
  2928. material_storage->shaders.scene_shader.version_set_uniform(SceneShaderGLES3::WORLD_TRANSFORM, world_transform, shader->version, instance_variant, spec_constants);
  2929. {
  2930. GLES3::Mesh::Surface *s = reinterpret_cast<GLES3::Mesh::Surface *>(surf->surface);
  2931. if (s->format & RS::ARRAY_FLAG_COMPRESS_ATTRIBUTES) {
  2932. material_storage->shaders.scene_shader.version_set_uniform(SceneShaderGLES3::COMPRESSED_AABB_POSITION, s->aabb.position, shader->version, instance_variant, spec_constants);
  2933. material_storage->shaders.scene_shader.version_set_uniform(SceneShaderGLES3::COMPRESSED_AABB_SIZE, s->aabb.size, shader->version, instance_variant, spec_constants);
  2934. material_storage->shaders.scene_shader.version_set_uniform(SceneShaderGLES3::UV_SCALE, s->uv_scale, shader->version, instance_variant, spec_constants);
  2935. } else {
  2936. material_storage->shaders.scene_shader.version_set_uniform(SceneShaderGLES3::COMPRESSED_AABB_POSITION, Vector3(0.0, 0.0, 0.0), shader->version, instance_variant, spec_constants);
  2937. material_storage->shaders.scene_shader.version_set_uniform(SceneShaderGLES3::COMPRESSED_AABB_SIZE, Vector3(1.0, 1.0, 1.0), shader->version, instance_variant, spec_constants);
  2938. material_storage->shaders.scene_shader.version_set_uniform(SceneShaderGLES3::UV_SCALE, Vector4(0.0, 0.0, 0.0, 0.0), shader->version, instance_variant, spec_constants);
  2939. }
  2940. }
  2941. material_storage->shaders.scene_shader.version_set_uniform(SceneShaderGLES3::MODEL_FLAGS, inst->flags_cache, shader->version, instance_variant, spec_constants);
  2942. material_storage->shaders.scene_shader.version_set_uniform(SceneShaderGLES3::INSTANCE_OFFSET, uint32_t(inst->shader_uniforms_offset), shader->version, instance_variant, spec_constants);
  2943. if (p_pass_mode == PASS_MODE_MATERIAL) {
  2944. material_storage->shaders.scene_shader.version_set_uniform(SceneShaderGLES3::UV_OFFSET, p_params->uv_offset, shader->version, instance_variant, spec_constants);
  2945. }
  2946. // Can be index count or vertex count
  2947. uint32_t count = 0;
  2948. if (surf->lod_index > 0) {
  2949. count = surf->index_count;
  2950. } else {
  2951. count = mesh_storage->mesh_surface_get_vertices_drawn_count(mesh_surface);
  2952. }
  2953. if (use_wireframe) {
  2954. // In this case we are using index count, and we need double the indices for the wireframe mesh.
  2955. count = count * 2;
  2956. }
  2957. if constexpr (p_pass_mode != PASS_MODE_DEPTH) {
  2958. // Don't count draw calls during depth pre-pass to match the RD renderers.
  2959. if (p_render_data->render_info) {
  2960. p_render_data->render_info->info[RS::VIEWPORT_RENDER_INFO_TYPE_VISIBLE][RS::VIEWPORT_RENDER_INFO_DRAW_CALLS_IN_FRAME]++;
  2961. }
  2962. }
  2963. if (inst->instance_count > 0) {
  2964. // Using MultiMesh or Particles.
  2965. // Bind instance buffers.
  2966. GLuint instance_buffer = 0;
  2967. uint32_t stride = 0;
  2968. if (inst->flags_cache & INSTANCE_DATA_FLAG_PARTICLES) {
  2969. instance_buffer = particles_storage->particles_get_gl_buffer(inst->data->base);
  2970. stride = 16; // 12 bytes for instance transform and 4 bytes for packed color and custom.
  2971. } else {
  2972. instance_buffer = mesh_storage->multimesh_get_gl_buffer(inst->data->base);
  2973. stride = mesh_storage->multimesh_get_stride(inst->data->base);
  2974. }
  2975. if (instance_buffer == 0) {
  2976. // Instance buffer not initialized yet. Skip rendering for now.
  2977. break;
  2978. }
  2979. glBindBuffer(GL_ARRAY_BUFFER, instance_buffer);
  2980. glEnableVertexAttribArray(12);
  2981. glVertexAttribPointer(12, 4, GL_FLOAT, GL_FALSE, stride * sizeof(float), CAST_INT_TO_UCHAR_PTR(0));
  2982. glVertexAttribDivisor(12, 1);
  2983. glEnableVertexAttribArray(13);
  2984. glVertexAttribPointer(13, 4, GL_FLOAT, GL_FALSE, stride * sizeof(float), CAST_INT_TO_UCHAR_PTR(sizeof(float) * 4));
  2985. glVertexAttribDivisor(13, 1);
  2986. if (!(inst->flags_cache & INSTANCE_DATA_FLAG_MULTIMESH_FORMAT_2D)) {
  2987. glEnableVertexAttribArray(14);
  2988. glVertexAttribPointer(14, 4, GL_FLOAT, GL_FALSE, stride * sizeof(float), CAST_INT_TO_UCHAR_PTR(sizeof(float) * 8));
  2989. glVertexAttribDivisor(14, 1);
  2990. }
  2991. if ((inst->flags_cache & INSTANCE_DATA_FLAG_MULTIMESH_HAS_COLOR) || (inst->flags_cache & INSTANCE_DATA_FLAG_MULTIMESH_HAS_CUSTOM_DATA)) {
  2992. uint32_t color_custom_offset = inst->flags_cache & INSTANCE_DATA_FLAG_MULTIMESH_FORMAT_2D ? 8 : 12;
  2993. glEnableVertexAttribArray(15);
  2994. glVertexAttribIPointer(15, 4, GL_UNSIGNED_INT, stride * sizeof(float), CAST_INT_TO_UCHAR_PTR(color_custom_offset * sizeof(float)));
  2995. glVertexAttribDivisor(15, 1);
  2996. } else {
  2997. // Set all default instance color and custom data values to 1.0 or 0.0 using a compressed format.
  2998. uint16_t zero = Math::make_half_float(0.0f);
  2999. uint16_t one = Math::make_half_float(1.0f);
  3000. GLuint default_color = (uint32_t(one) << 16) | one;
  3001. GLuint default_custom = (uint32_t(zero) << 16) | zero;
  3002. glVertexAttribI4ui(15, default_color, default_color, default_custom, default_custom);
  3003. }
  3004. if (use_wireframe) {
  3005. glDrawElementsInstanced(GL_LINES, count, GL_UNSIGNED_INT, nullptr, inst->instance_count);
  3006. } else {
  3007. if (use_index_buffer) {
  3008. glDrawElementsInstanced(primitive_gl, count, mesh_storage->mesh_surface_get_index_type(mesh_surface), nullptr, inst->instance_count);
  3009. } else {
  3010. glDrawArraysInstanced(primitive_gl, 0, count, inst->instance_count);
  3011. }
  3012. }
  3013. } else {
  3014. // Using regular Mesh.
  3015. if (use_wireframe) {
  3016. glDrawElements(GL_LINES, count, GL_UNSIGNED_INT, nullptr);
  3017. } else {
  3018. if (use_index_buffer) {
  3019. glDrawElements(primitive_gl, count, mesh_storage->mesh_surface_get_index_type(mesh_surface), nullptr);
  3020. } else {
  3021. glDrawArrays(primitive_gl, 0, count);
  3022. }
  3023. }
  3024. }
  3025. if (inst->instance_count > 0) {
  3026. glDisableVertexAttribArray(12);
  3027. glDisableVertexAttribArray(13);
  3028. glDisableVertexAttribArray(14);
  3029. glDisableVertexAttribArray(15);
  3030. }
  3031. }
  3032. if constexpr (p_pass_mode == PASS_MODE_COLOR) {
  3033. if (uses_additive_lighting && !p_render_data->transparent_bg) {
  3034. // Disable additive blending if enabled for additive lights.
  3035. scene_state.enable_gl_blend(false);
  3036. }
  3037. }
  3038. }
  3039. // Make the actual redraw request
  3040. if (should_request_redraw) {
  3041. RenderingServerDefault::redraw_request();
  3042. }
  3043. }
  3044. void RasterizerSceneGLES3::render_material(const Transform3D &p_cam_transform, const Projection &p_cam_projection, bool p_cam_orthogonal, const PagedArray<RenderGeometryInstance *> &p_instances, RID p_framebuffer, const Rect2i &p_region) {
  3045. }
  3046. void RasterizerSceneGLES3::render_particle_collider_heightfield(RID p_collider, const Transform3D &p_transform, const PagedArray<RenderGeometryInstance *> &p_instances) {
  3047. GLES3::ParticlesStorage *particles_storage = GLES3::ParticlesStorage::get_singleton();
  3048. ERR_FAIL_COND(!particles_storage->particles_collision_is_heightfield(p_collider));
  3049. Vector3 extents = particles_storage->particles_collision_get_extents(p_collider) * p_transform.basis.get_scale();
  3050. Projection cm;
  3051. cm.set_orthogonal(-extents.x, extents.x, -extents.z, extents.z, 0, extents.y * 2.0);
  3052. Vector3 cam_pos = p_transform.origin;
  3053. cam_pos.y += extents.y;
  3054. Transform3D cam_xform;
  3055. cam_xform.set_look_at(cam_pos, cam_pos - p_transform.basis.get_column(Vector3::AXIS_Y), -p_transform.basis.get_column(Vector3::AXIS_Z).normalized());
  3056. GLuint fb = particles_storage->particles_collision_get_heightfield_framebuffer(p_collider);
  3057. Size2i fb_size = particles_storage->particles_collision_get_heightfield_size(p_collider);
  3058. RENDER_TIMESTAMP("Setup GPUParticlesCollisionHeightField3D");
  3059. RenderDataGLES3 render_data;
  3060. render_data.cam_projection = cm;
  3061. render_data.cam_transform = cam_xform;
  3062. render_data.view_projection[0] = cm;
  3063. render_data.inv_cam_transform = render_data.cam_transform.affine_inverse();
  3064. render_data.cam_orthogonal = true;
  3065. render_data.z_near = 0.0;
  3066. render_data.z_far = cm.get_z_far();
  3067. render_data.main_cam_transform = cam_xform;
  3068. render_data.instances = &p_instances;
  3069. _setup_environment(&render_data, true, Vector2(fb_size), true, Color(), false);
  3070. PassMode pass_mode = PASS_MODE_SHADOW;
  3071. _fill_render_list(RENDER_LIST_SECONDARY, &render_data, pass_mode);
  3072. render_list[RENDER_LIST_SECONDARY].sort_by_key();
  3073. RENDER_TIMESTAMP("Render Collider Heightfield");
  3074. glBindFramebuffer(GL_FRAMEBUFFER, fb);
  3075. glViewport(0, 0, fb_size.width, fb_size.height);
  3076. GLuint global_buffer = GLES3::MaterialStorage::get_singleton()->global_shader_parameters_get_uniform_buffer();
  3077. glBindBufferBase(GL_UNIFORM_BUFFER, SCENE_GLOBALS_UNIFORM_LOCATION, global_buffer);
  3078. glBindBuffer(GL_UNIFORM_BUFFER, 0);
  3079. scene_state.reset_gl_state();
  3080. scene_state.enable_gl_depth_test(true);
  3081. scene_state.enable_gl_depth_draw(true);
  3082. scene_state.set_gl_depth_func(GL_GREATER);
  3083. glDrawBuffers(0, nullptr);
  3084. glColorMask(0, 0, 0, 0);
  3085. RasterizerGLES3::clear_depth(0.0);
  3086. glClear(GL_DEPTH_BUFFER_BIT);
  3087. RenderListParameters render_list_params(render_list[RENDER_LIST_SECONDARY].elements.ptr(), render_list[RENDER_LIST_SECONDARY].elements.size(), false, 31, false);
  3088. _render_list_template<PASS_MODE_SHADOW>(&render_list_params, &render_data, 0, render_list[RENDER_LIST_SECONDARY].elements.size());
  3089. glColorMask(1, 1, 1, 1);
  3090. glBindFramebuffer(GL_FRAMEBUFFER, GLES3::TextureStorage::system_fbo);
  3091. }
  3092. void RasterizerSceneGLES3::_render_uv2(const PagedArray<RenderGeometryInstance *> &p_instances, GLuint p_framebuffer, const Rect2i &p_region) {
  3093. RENDER_TIMESTAMP("Setup Rendering UV2");
  3094. RenderDataGLES3 render_data;
  3095. render_data.instances = &p_instances;
  3096. scene_state.ubo.emissive_exposure_normalization = -1.0; // Use default exposure normalization.
  3097. _setup_environment(&render_data, true, Vector2(1, 1), true, Color(), false);
  3098. PassMode pass_mode = PASS_MODE_MATERIAL;
  3099. _fill_render_list(RENDER_LIST_SECONDARY, &render_data, pass_mode);
  3100. render_list[RENDER_LIST_SECONDARY].sort_by_key();
  3101. RENDER_TIMESTAMP("Render 3D Material");
  3102. {
  3103. glBindFramebuffer(GL_FRAMEBUFFER, p_framebuffer);
  3104. glViewport(p_region.position.x, p_region.position.y, p_region.size.x, p_region.size.y);
  3105. GLuint global_buffer = GLES3::MaterialStorage::get_singleton()->global_shader_parameters_get_uniform_buffer();
  3106. glBindBufferBase(GL_UNIFORM_BUFFER, SCENE_GLOBALS_UNIFORM_LOCATION, global_buffer);
  3107. glBindBuffer(GL_UNIFORM_BUFFER, 0);
  3108. scene_state.reset_gl_state();
  3109. scene_state.enable_gl_depth_test(true);
  3110. scene_state.enable_gl_depth_draw(true);
  3111. scene_state.set_gl_depth_func(GL_GREATER);
  3112. TightLocalVector<GLenum> draw_buffers;
  3113. draw_buffers.push_back(GL_COLOR_ATTACHMENT0);
  3114. draw_buffers.push_back(GL_COLOR_ATTACHMENT1);
  3115. draw_buffers.push_back(GL_COLOR_ATTACHMENT2);
  3116. draw_buffers.push_back(GL_COLOR_ATTACHMENT3);
  3117. glDrawBuffers(draw_buffers.size(), draw_buffers.ptr());
  3118. glClearColor(0.0, 0.0, 0.0, 0.0);
  3119. RasterizerGLES3::clear_depth(0.0);
  3120. glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
  3121. uint64_t base_spec_constant = 0;
  3122. base_spec_constant |= SceneShaderGLES3::RENDER_MATERIAL;
  3123. base_spec_constant |= SceneShaderGLES3::DISABLE_FOG;
  3124. base_spec_constant |= SceneShaderGLES3::DISABLE_LIGHT_DIRECTIONAL;
  3125. base_spec_constant |= SceneShaderGLES3::DISABLE_LIGHT_OMNI;
  3126. base_spec_constant |= SceneShaderGLES3::DISABLE_LIGHT_SPOT;
  3127. base_spec_constant |= SceneShaderGLES3::DISABLE_LIGHTMAP;
  3128. RenderListParameters render_list_params(render_list[RENDER_LIST_SECONDARY].elements.ptr(), render_list[RENDER_LIST_SECONDARY].elements.size(), false, base_spec_constant, true, Vector2(0, 0));
  3129. const int uv_offset_count = 9;
  3130. static const Vector2 uv_offsets[uv_offset_count] = {
  3131. Vector2(-1, 1),
  3132. Vector2(1, 1),
  3133. Vector2(1, -1),
  3134. Vector2(-1, -1),
  3135. Vector2(-1, 0),
  3136. Vector2(1, 0),
  3137. Vector2(0, -1),
  3138. Vector2(0, 1),
  3139. Vector2(0, 0),
  3140. };
  3141. for (int i = 0; i < uv_offset_count; i++) {
  3142. Vector2 ofs = uv_offsets[i];
  3143. ofs.x /= p_region.size.width;
  3144. ofs.y /= p_region.size.height;
  3145. render_list_params.uv_offset = ofs;
  3146. _render_list_template<PASS_MODE_MATERIAL>(&render_list_params, &render_data, 0, render_list[RENDER_LIST_SECONDARY].elements.size());
  3147. }
  3148. render_list_params.uv_offset = Vector2(0, 0);
  3149. render_list_params.force_wireframe = false;
  3150. _render_list_template<PASS_MODE_MATERIAL>(&render_list_params, &render_data, 0, render_list[RENDER_LIST_SECONDARY].elements.size());
  3151. GLuint db = GL_COLOR_ATTACHMENT0;
  3152. glDrawBuffers(1, &db);
  3153. glBindFramebuffer(GL_FRAMEBUFFER, GLES3::TextureStorage::system_fbo);
  3154. }
  3155. }
  3156. void RasterizerSceneGLES3::set_time(double p_time, double p_step) {
  3157. time = p_time;
  3158. time_step = p_step;
  3159. }
  3160. void RasterizerSceneGLES3::set_debug_draw_mode(RS::ViewportDebugDraw p_debug_draw) {
  3161. debug_draw = p_debug_draw;
  3162. }
  3163. Ref<RenderSceneBuffers> RasterizerSceneGLES3::render_buffers_create() {
  3164. Ref<RenderSceneBuffersGLES3> rb;
  3165. rb.instantiate();
  3166. return rb;
  3167. }
  3168. void RasterizerSceneGLES3::_render_buffers_debug_draw(Ref<RenderSceneBuffersGLES3> p_render_buffers, RID p_shadow_atlas, GLuint p_fbo) {
  3169. GLES3::TextureStorage *texture_storage = GLES3::TextureStorage::get_singleton();
  3170. GLES3::LightStorage *light_storage = GLES3::LightStorage::get_singleton();
  3171. GLES3::CopyEffects *copy_effects = GLES3::CopyEffects::get_singleton();
  3172. ERR_FAIL_COND(p_render_buffers.is_null());
  3173. RID render_target = p_render_buffers->render_target;
  3174. GLES3::RenderTarget *rt = texture_storage->get_render_target(render_target);
  3175. ERR_FAIL_NULL(rt);
  3176. if (debug_draw == RS::VIEWPORT_DEBUG_DRAW_SHADOW_ATLAS) {
  3177. if (p_shadow_atlas.is_valid()) {
  3178. // Get or create debug textures to display shadow maps as an atlas.
  3179. GLuint shadow_atlas_texture = light_storage->shadow_atlas_get_debug_texture(p_shadow_atlas);
  3180. GLuint shadow_atlas_fb = light_storage->shadow_atlas_get_debug_fb(p_shadow_atlas);
  3181. uint32_t shadow_atlas_size = light_storage->shadow_atlas_get_size(p_shadow_atlas);
  3182. uint32_t quadrant_size = shadow_atlas_size >> 1;
  3183. glBindFramebuffer(GL_FRAMEBUFFER, shadow_atlas_fb);
  3184. glViewport(0, 0, shadow_atlas_size, shadow_atlas_size);
  3185. glActiveTexture(GL_TEXTURE0);
  3186. scene_state.enable_gl_depth_draw(true);
  3187. scene_state.set_gl_depth_func(GL_ALWAYS);
  3188. scene_state.set_gl_cull_mode(RS::CULL_MODE_DISABLED);
  3189. // Loop through quadrants and copy shadows over.
  3190. for (int quadrant = 0; quadrant < 4; quadrant++) {
  3191. uint32_t subdivision = light_storage->shadow_atlas_get_quadrant_subdivision(p_shadow_atlas, quadrant);
  3192. if (subdivision == 0) {
  3193. continue;
  3194. }
  3195. Rect2i atlas_rect;
  3196. Rect2 atlas_uv_rect;
  3197. uint32_t shadow_size = (quadrant_size / subdivision);
  3198. float size = float(shadow_size) / float(shadow_atlas_size);
  3199. uint32_t length = light_storage->shadow_atlas_get_quadrant_shadows_allocated(p_shadow_atlas, quadrant);
  3200. for (uint32_t shadow_idx = 0; shadow_idx < length; shadow_idx++) {
  3201. bool is_omni = light_storage->shadow_atlas_get_quadrant_shadow_is_omni(p_shadow_atlas, quadrant, shadow_idx);
  3202. // Calculate shadow's position in the debug atlas.
  3203. atlas_rect.position.x = (quadrant & 1) * quadrant_size;
  3204. atlas_rect.position.y = (quadrant >> 1) * quadrant_size;
  3205. atlas_rect.position.x += (shadow_idx % subdivision) * shadow_size;
  3206. atlas_rect.position.y += (shadow_idx / subdivision) * shadow_size;
  3207. atlas_uv_rect.position = Vector2(atlas_rect.position) / float(shadow_atlas_size);
  3208. atlas_uv_rect.size = Vector2(size, size);
  3209. GLuint shadow_tex = light_storage->shadow_atlas_get_quadrant_shadow_texture(p_shadow_atlas, quadrant, shadow_idx);
  3210. // Copy from shadowmap to debug atlas.
  3211. if (is_omni) {
  3212. glBindTexture(GL_TEXTURE_CUBE_MAP, shadow_tex);
  3213. glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_COMPARE_MODE, GL_NONE);
  3214. copy_effects->copy_cube_to_rect(atlas_uv_rect);
  3215. glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_COMPARE_MODE, GL_COMPARE_REF_TO_TEXTURE);
  3216. glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_COMPARE_FUNC, GL_GREATER);
  3217. } else {
  3218. glBindTexture(GL_TEXTURE_2D, shadow_tex);
  3219. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE, GL_NONE);
  3220. copy_effects->copy_to_rect(atlas_uv_rect);
  3221. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE, GL_COMPARE_REF_TO_TEXTURE);
  3222. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_FUNC, GL_GREATER);
  3223. }
  3224. }
  3225. }
  3226. // Set back to FBO
  3227. glBindFramebuffer(GL_FRAMEBUFFER, p_fbo);
  3228. Size2i size = p_render_buffers->get_internal_size();
  3229. glViewport(0, 0, size.width, size.height);
  3230. glBindTexture(GL_TEXTURE_2D, shadow_atlas_texture);
  3231. copy_effects->copy_to_rect(Rect2(Vector2(), Vector2(0.5, 0.5)));
  3232. glBindTexture(GL_TEXTURE_2D, 0);
  3233. glBindFramebuffer(GL_FRAMEBUFFER, GLES3::TextureStorage::system_fbo);
  3234. }
  3235. }
  3236. if (debug_draw == RS::VIEWPORT_DEBUG_DRAW_DIRECTIONAL_SHADOW_ATLAS) {
  3237. if (light_storage->directional_shadow_get_texture() != 0) {
  3238. GLuint shadow_atlas_texture = light_storage->directional_shadow_get_texture();
  3239. glActiveTexture(GL_TEXTURE0);
  3240. glBindTexture(GL_TEXTURE_2D, shadow_atlas_texture);
  3241. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE, GL_NONE);
  3242. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_SWIZZLE_R, GL_RED);
  3243. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_SWIZZLE_G, GL_RED);
  3244. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_SWIZZLE_B, GL_RED);
  3245. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_SWIZZLE_A, GL_ONE);
  3246. scene_state.enable_gl_depth_test(false);
  3247. scene_state.enable_gl_depth_draw(false);
  3248. copy_effects->copy_to_rect(Rect2(Vector2(), Vector2(0.5, 0.5)));
  3249. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_SWIZZLE_R, GL_RED);
  3250. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_SWIZZLE_G, GL_GREEN);
  3251. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_SWIZZLE_B, GL_BLUE);
  3252. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_SWIZZLE_A, GL_ALPHA);
  3253. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE, GL_COMPARE_REF_TO_TEXTURE);
  3254. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_FUNC, GL_GREATER);
  3255. glBindTexture(GL_TEXTURE_2D, 0);
  3256. }
  3257. }
  3258. }
  3259. void RasterizerSceneGLES3::gi_set_use_half_resolution(bool p_enable) {
  3260. }
  3261. void RasterizerSceneGLES3::screen_space_roughness_limiter_set_active(bool p_enable, float p_amount, float p_curve) {
  3262. }
  3263. bool RasterizerSceneGLES3::screen_space_roughness_limiter_is_active() const {
  3264. return false;
  3265. }
  3266. void RasterizerSceneGLES3::sub_surface_scattering_set_quality(RS::SubSurfaceScatteringQuality p_quality) {
  3267. }
  3268. void RasterizerSceneGLES3::sub_surface_scattering_set_scale(float p_scale, float p_depth_scale) {
  3269. }
  3270. TypedArray<Image> RasterizerSceneGLES3::bake_render_uv2(RID p_base, const TypedArray<RID> &p_material_overrides, const Size2i &p_image_size) {
  3271. GLES3::Config *config = GLES3::Config::get_singleton();
  3272. ERR_FAIL_COND_V_MSG(p_image_size.width <= 0, TypedArray<Image>(), "Image width must be greater than 0.");
  3273. ERR_FAIL_COND_V_MSG(p_image_size.height <= 0, TypedArray<Image>(), "Image height must be greater than 0.");
  3274. GLuint albedo_alpha_tex = 0;
  3275. GLuint normal_tex = 0;
  3276. GLuint orm_tex = 0;
  3277. GLuint emission_tex = 0;
  3278. GLuint depth_tex = 0;
  3279. glGenTextures(1, &albedo_alpha_tex);
  3280. glGenTextures(1, &normal_tex);
  3281. glGenTextures(1, &orm_tex);
  3282. glGenTextures(1, &emission_tex);
  3283. glGenTextures(1, &depth_tex);
  3284. glBindTexture(GL_TEXTURE_2D, albedo_alpha_tex);
  3285. glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, p_image_size.width, p_image_size.height, 0, GL_RGBA, GL_UNSIGNED_BYTE, nullptr);
  3286. GLES3::Utilities::get_singleton()->texture_allocated_data(albedo_alpha_tex, p_image_size.width * p_image_size.height * 4, "Lightmap albedo texture");
  3287. glBindTexture(GL_TEXTURE_2D, normal_tex);
  3288. glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, p_image_size.width, p_image_size.height, 0, GL_RGBA, GL_UNSIGNED_BYTE, nullptr);
  3289. GLES3::Utilities::get_singleton()->texture_allocated_data(normal_tex, p_image_size.width * p_image_size.height * 4, "Lightmap normal texture");
  3290. glBindTexture(GL_TEXTURE_2D, orm_tex);
  3291. glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, p_image_size.width, p_image_size.height, 0, GL_RGBA, GL_UNSIGNED_BYTE, nullptr);
  3292. GLES3::Utilities::get_singleton()->texture_allocated_data(orm_tex, p_image_size.width * p_image_size.height * 4, "Lightmap ORM texture");
  3293. // Consider rendering to RGBA8 encoded as RGBE, then manually convert to RGBAH on CPU.
  3294. glBindTexture(GL_TEXTURE_2D, emission_tex);
  3295. if (config->float_texture_supported) {
  3296. glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA16F, p_image_size.width, p_image_size.height, 0, GL_RGBA, GL_FLOAT, nullptr);
  3297. GLES3::Utilities::get_singleton()->texture_allocated_data(emission_tex, p_image_size.width * p_image_size.height * 16, "Lightmap emission texture");
  3298. } else {
  3299. // Fallback to RGBA8 on devices that don't support rendering to floating point textures. This will look bad, but we have no choice.
  3300. glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, p_image_size.width, p_image_size.height, 0, GL_RGBA, GL_UNSIGNED_BYTE, nullptr);
  3301. GLES3::Utilities::get_singleton()->texture_allocated_data(emission_tex, p_image_size.width * p_image_size.height * 4, "Lightmap emission texture");
  3302. }
  3303. glBindTexture(GL_TEXTURE_2D, depth_tex);
  3304. glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT24, p_image_size.width, p_image_size.height, 0, GL_DEPTH_COMPONENT, GL_UNSIGNED_INT, nullptr);
  3305. GLES3::Utilities::get_singleton()->texture_allocated_data(depth_tex, p_image_size.width * p_image_size.height * 3, "Lightmap depth texture");
  3306. GLuint fbo = 0;
  3307. glGenFramebuffers(1, &fbo);
  3308. glBindFramebuffer(GL_FRAMEBUFFER, fbo);
  3309. glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, albedo_alpha_tex, 0);
  3310. glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT1, GL_TEXTURE_2D, normal_tex, 0);
  3311. glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT2, GL_TEXTURE_2D, orm_tex, 0);
  3312. glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT3, GL_TEXTURE_2D, emission_tex, 0);
  3313. glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, depth_tex, 0);
  3314. GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
  3315. if (status != GL_FRAMEBUFFER_COMPLETE) {
  3316. glDeleteFramebuffers(1, &fbo);
  3317. GLES3::Utilities::get_singleton()->texture_free_data(albedo_alpha_tex);
  3318. GLES3::Utilities::get_singleton()->texture_free_data(normal_tex);
  3319. GLES3::Utilities::get_singleton()->texture_free_data(orm_tex);
  3320. GLES3::Utilities::get_singleton()->texture_free_data(emission_tex);
  3321. GLES3::Utilities::get_singleton()->texture_free_data(depth_tex);
  3322. WARN_PRINT("Could not create render target, status: " + GLES3::TextureStorage::get_singleton()->get_framebuffer_error(status));
  3323. return TypedArray<Image>();
  3324. }
  3325. RenderGeometryInstance *gi_inst = geometry_instance_create(p_base);
  3326. ERR_FAIL_NULL_V(gi_inst, TypedArray<Image>());
  3327. uint32_t sc = RSG::mesh_storage->mesh_get_surface_count(p_base);
  3328. Vector<RID> materials;
  3329. materials.resize(sc);
  3330. for (uint32_t i = 0; i < sc; i++) {
  3331. if (i < (uint32_t)p_material_overrides.size()) {
  3332. materials.write[i] = p_material_overrides[i];
  3333. }
  3334. }
  3335. gi_inst->set_surface_materials(materials);
  3336. if (cull_argument.size() == 0) {
  3337. cull_argument.push_back(nullptr);
  3338. }
  3339. cull_argument[0] = gi_inst;
  3340. _render_uv2(cull_argument, fbo, Rect2i(0, 0, p_image_size.width, p_image_size.height));
  3341. geometry_instance_free(gi_inst);
  3342. TypedArray<Image> ret;
  3343. // Create a dummy texture so we can use texture_2d_get.
  3344. RID tex_rid = GLES3::TextureStorage::get_singleton()->texture_allocate();
  3345. GLES3::Texture texture;
  3346. texture.width = p_image_size.width;
  3347. texture.height = p_image_size.height;
  3348. texture.alloc_width = p_image_size.width;
  3349. texture.alloc_height = p_image_size.height;
  3350. texture.format = Image::FORMAT_RGBA8;
  3351. texture.real_format = Image::FORMAT_RGBA8;
  3352. texture.gl_format_cache = GL_RGBA;
  3353. texture.gl_type_cache = GL_UNSIGNED_BYTE;
  3354. texture.type = GLES3::Texture::TYPE_2D;
  3355. texture.target = GL_TEXTURE_2D;
  3356. texture.active = true;
  3357. texture.is_render_target = true; // Enable this so the texture isn't cached in the editor.
  3358. GLES3::TextureStorage::get_singleton()->texture_2d_initialize_from_texture(tex_rid, texture);
  3359. GLES3::Texture *tex = GLES3::TextureStorage::get_singleton()->get_texture(tex_rid);
  3360. {
  3361. tex->tex_id = albedo_alpha_tex;
  3362. Ref<Image> img = GLES3::TextureStorage::get_singleton()->texture_2d_get(tex_rid);
  3363. GLES3::Utilities::get_singleton()->texture_free_data(albedo_alpha_tex);
  3364. ret.push_back(img);
  3365. }
  3366. {
  3367. tex->tex_id = normal_tex;
  3368. Ref<Image> img = GLES3::TextureStorage::get_singleton()->texture_2d_get(tex_rid);
  3369. GLES3::Utilities::get_singleton()->texture_free_data(normal_tex);
  3370. ret.push_back(img);
  3371. }
  3372. {
  3373. tex->tex_id = orm_tex;
  3374. Ref<Image> img = GLES3::TextureStorage::get_singleton()->texture_2d_get(tex_rid);
  3375. GLES3::Utilities::get_singleton()->texture_free_data(orm_tex);
  3376. ret.push_back(img);
  3377. }
  3378. {
  3379. tex->tex_id = emission_tex;
  3380. if (config->float_texture_supported) {
  3381. tex->format = Image::FORMAT_RGBAH;
  3382. tex->real_format = Image::FORMAT_RGBAH;
  3383. tex->gl_type_cache = GL_HALF_FLOAT;
  3384. }
  3385. Ref<Image> img = GLES3::TextureStorage::get_singleton()->texture_2d_get(tex_rid);
  3386. GLES3::Utilities::get_singleton()->texture_free_data(emission_tex);
  3387. ret.push_back(img);
  3388. }
  3389. tex->is_render_target = false;
  3390. tex->tex_id = 0;
  3391. GLES3::TextureStorage::get_singleton()->texture_free(tex_rid);
  3392. GLES3::Utilities::get_singleton()->texture_free_data(depth_tex);
  3393. glDeleteFramebuffers(1, &fbo);
  3394. return ret;
  3395. }
  3396. bool RasterizerSceneGLES3::free(RID p_rid) {
  3397. if (is_environment(p_rid)) {
  3398. environment_free(p_rid);
  3399. } else if (sky_owner.owns(p_rid)) {
  3400. Sky *sky = sky_owner.get_or_null(p_rid);
  3401. ERR_FAIL_NULL_V(sky, false);
  3402. _free_sky_data(sky);
  3403. sky_owner.free(p_rid);
  3404. } else if (GLES3::LightStorage::get_singleton()->owns_light_instance(p_rid)) {
  3405. GLES3::LightStorage::get_singleton()->light_instance_free(p_rid);
  3406. } else if (RSG::camera_attributes->owns_camera_attributes(p_rid)) {
  3407. //not much to delete, just free it
  3408. RSG::camera_attributes->camera_attributes_free(p_rid);
  3409. } else if (is_compositor(p_rid)) {
  3410. compositor_free(p_rid);
  3411. } else if (is_compositor_effect(p_rid)) {
  3412. compositor_effect_free(p_rid);
  3413. } else {
  3414. return false;
  3415. }
  3416. return true;
  3417. }
  3418. void RasterizerSceneGLES3::update() {
  3419. _update_dirty_skys();
  3420. }
  3421. void RasterizerSceneGLES3::sdfgi_set_debug_probe_select(const Vector3 &p_position, const Vector3 &p_dir) {
  3422. }
  3423. void RasterizerSceneGLES3::decals_set_filter(RS::DecalFilter p_filter) {
  3424. }
  3425. void RasterizerSceneGLES3::light_projectors_set_filter(RS::LightProjectorFilter p_filter) {
  3426. }
  3427. void RasterizerSceneGLES3::lightmaps_set_bicubic_filter(bool p_enable) {
  3428. lightmap_bicubic_upscale = p_enable;
  3429. }
  3430. RasterizerSceneGLES3::RasterizerSceneGLES3() {
  3431. singleton = this;
  3432. GLES3::MaterialStorage *material_storage = GLES3::MaterialStorage::get_singleton();
  3433. GLES3::Config *config = GLES3::Config::get_singleton();
  3434. cull_argument.set_page_pool(&cull_argument_pool);
  3435. // Quality settings.
  3436. use_physical_light_units = GLOBAL_GET("rendering/lights_and_shadows/use_physical_light_units");
  3437. positional_soft_shadow_filter_set_quality((RS::ShadowQuality)(int)GLOBAL_GET("rendering/lights_and_shadows/positional_shadow/soft_shadow_filter_quality"));
  3438. directional_soft_shadow_filter_set_quality((RS::ShadowQuality)(int)GLOBAL_GET("rendering/lights_and_shadows/directional_shadow/soft_shadow_filter_quality"));
  3439. lightmaps_set_bicubic_filter(GLOBAL_GET("rendering/lightmapping/lightmap_gi/use_bicubic_filter"));
  3440. {
  3441. // Setup Lights
  3442. config->max_renderable_lights = MIN(config->max_renderable_lights, config->max_uniform_buffer_size / (int)sizeof(RasterizerSceneGLES3::LightData));
  3443. config->max_lights_per_object = MIN(config->max_lights_per_object, config->max_renderable_lights);
  3444. uint32_t light_buffer_size = config->max_renderable_lights * sizeof(LightData);
  3445. scene_state.omni_lights = memnew_arr(LightData, config->max_renderable_lights);
  3446. scene_state.omni_light_sort = memnew_arr(InstanceSort<GLES3::LightInstance>, config->max_renderable_lights);
  3447. glGenBuffers(1, &scene_state.omni_light_buffer);
  3448. glBindBuffer(GL_UNIFORM_BUFFER, scene_state.omni_light_buffer);
  3449. GLES3::Utilities::get_singleton()->buffer_allocate_data(GL_UNIFORM_BUFFER, scene_state.omni_light_buffer, light_buffer_size, nullptr, GL_STREAM_DRAW, "OmniLight UBO");
  3450. scene_state.spot_lights = memnew_arr(LightData, config->max_renderable_lights);
  3451. scene_state.spot_light_sort = memnew_arr(InstanceSort<GLES3::LightInstance>, config->max_renderable_lights);
  3452. glGenBuffers(1, &scene_state.spot_light_buffer);
  3453. glBindBuffer(GL_UNIFORM_BUFFER, scene_state.spot_light_buffer);
  3454. GLES3::Utilities::get_singleton()->buffer_allocate_data(GL_UNIFORM_BUFFER, scene_state.spot_light_buffer, light_buffer_size, nullptr, GL_STREAM_DRAW, "SpotLight UBO");
  3455. uint32_t directional_light_buffer_size = MAX_DIRECTIONAL_LIGHTS * sizeof(DirectionalLightData);
  3456. scene_state.directional_lights = memnew_arr(DirectionalLightData, MAX_DIRECTIONAL_LIGHTS);
  3457. glGenBuffers(1, &scene_state.directional_light_buffer);
  3458. glBindBuffer(GL_UNIFORM_BUFFER, scene_state.directional_light_buffer);
  3459. GLES3::Utilities::get_singleton()->buffer_allocate_data(GL_UNIFORM_BUFFER, scene_state.directional_light_buffer, directional_light_buffer_size, nullptr, GL_STREAM_DRAW, "DirectionalLight UBO");
  3460. uint32_t shadow_buffer_size = config->max_renderable_lights * sizeof(ShadowData) * 2;
  3461. scene_state.positional_shadows = memnew_arr(ShadowData, config->max_renderable_lights * 2);
  3462. glGenBuffers(1, &scene_state.positional_shadow_buffer);
  3463. glBindBuffer(GL_UNIFORM_BUFFER, scene_state.positional_shadow_buffer);
  3464. GLES3::Utilities::get_singleton()->buffer_allocate_data(GL_UNIFORM_BUFFER, scene_state.positional_shadow_buffer, shadow_buffer_size, nullptr, GL_STREAM_DRAW, "Positional Shadow UBO");
  3465. uint32_t directional_shadow_buffer_size = MAX_DIRECTIONAL_LIGHTS * sizeof(DirectionalShadowData);
  3466. scene_state.directional_shadows = memnew_arr(DirectionalShadowData, MAX_DIRECTIONAL_LIGHTS);
  3467. glGenBuffers(1, &scene_state.directional_shadow_buffer);
  3468. glBindBuffer(GL_UNIFORM_BUFFER, scene_state.directional_shadow_buffer);
  3469. GLES3::Utilities::get_singleton()->buffer_allocate_data(GL_UNIFORM_BUFFER, scene_state.directional_shadow_buffer, directional_shadow_buffer_size, nullptr, GL_STREAM_DRAW, "Directional Shadow UBO");
  3470. glBindBuffer(GL_UNIFORM_BUFFER, 0);
  3471. }
  3472. {
  3473. sky_globals.max_directional_lights = 4;
  3474. uint32_t directional_light_buffer_size = sky_globals.max_directional_lights * sizeof(DirectionalLightData);
  3475. sky_globals.directional_lights = memnew_arr(DirectionalLightData, sky_globals.max_directional_lights);
  3476. sky_globals.last_frame_directional_lights = memnew_arr(DirectionalLightData, sky_globals.max_directional_lights);
  3477. sky_globals.last_frame_directional_light_count = sky_globals.max_directional_lights + 1;
  3478. glGenBuffers(1, &sky_globals.directional_light_buffer);
  3479. glBindBuffer(GL_UNIFORM_BUFFER, sky_globals.directional_light_buffer);
  3480. GLES3::Utilities::get_singleton()->buffer_allocate_data(GL_UNIFORM_BUFFER, sky_globals.directional_light_buffer, directional_light_buffer_size, nullptr, GL_STREAM_DRAW, "Sky DirectionalLight UBO");
  3481. glBindBuffer(GL_UNIFORM_BUFFER, 0);
  3482. }
  3483. {
  3484. String global_defines;
  3485. global_defines += "#define MAX_GLOBAL_SHADER_UNIFORMS 256\n"; // TODO: this is arbitrary for now
  3486. global_defines += "\n#define MAX_LIGHT_DATA_STRUCTS " + itos(config->max_renderable_lights) + "\n";
  3487. global_defines += "\n#define MAX_DIRECTIONAL_LIGHT_DATA_STRUCTS " + itos(MAX_DIRECTIONAL_LIGHTS) + "\n";
  3488. global_defines += "\n#define MAX_FORWARD_LIGHTS " + itos(config->max_lights_per_object) + "u\n";
  3489. global_defines += "\n#define MAX_ROUGHNESS_LOD " + itos(sky_globals.roughness_layers - 1) + ".0\n";
  3490. if (config->force_vertex_shading) {
  3491. global_defines += "\n#define USE_VERTEX_LIGHTING\n";
  3492. }
  3493. if (!config->specular_occlusion) {
  3494. global_defines += "\n#define SPECULAR_OCCLUSION_DISABLED\n";
  3495. }
  3496. material_storage->shaders.scene_shader.initialize(global_defines);
  3497. scene_globals.shader_default_version = material_storage->shaders.scene_shader.version_create();
  3498. material_storage->shaders.scene_shader.version_bind_shader(scene_globals.shader_default_version, SceneShaderGLES3::MODE_COLOR);
  3499. }
  3500. {
  3501. //default material and shader
  3502. scene_globals.default_shader = material_storage->shader_allocate();
  3503. material_storage->shader_initialize(scene_globals.default_shader);
  3504. material_storage->shader_set_code(scene_globals.default_shader, R"(
  3505. // Default 3D material shader (Compatibility).
  3506. shader_type spatial;
  3507. void vertex() {
  3508. ROUGHNESS = 0.8;
  3509. }
  3510. void fragment() {
  3511. ALBEDO = vec3(0.6);
  3512. ROUGHNESS = 0.8;
  3513. METALLIC = 0.2;
  3514. }
  3515. )");
  3516. scene_globals.default_material = material_storage->material_allocate();
  3517. material_storage->material_initialize(scene_globals.default_material);
  3518. material_storage->material_set_shader(scene_globals.default_material, scene_globals.default_shader);
  3519. default_material_data_ptr = static_cast<GLES3::SceneMaterialData *>(GLES3::MaterialStorage::get_singleton()->material_get_data(scene_globals.default_material, RS::SHADER_SPATIAL));
  3520. }
  3521. {
  3522. // Overdraw material and shader.
  3523. scene_globals.overdraw_shader = material_storage->shader_allocate();
  3524. material_storage->shader_initialize(scene_globals.overdraw_shader);
  3525. material_storage->shader_set_code(scene_globals.overdraw_shader, R"(
  3526. // 3D editor Overdraw debug draw mode shader (Compatibility).
  3527. shader_type spatial;
  3528. render_mode blend_add, unshaded, fog_disabled;
  3529. void fragment() {
  3530. ALBEDO = vec3(0.4, 0.8, 0.8);
  3531. ALPHA = 0.2;
  3532. }
  3533. )");
  3534. scene_globals.overdraw_material = material_storage->material_allocate();
  3535. material_storage->material_initialize(scene_globals.overdraw_material);
  3536. material_storage->material_set_shader(scene_globals.overdraw_material, scene_globals.overdraw_shader);
  3537. overdraw_material_data_ptr = static_cast<GLES3::SceneMaterialData *>(GLES3::MaterialStorage::get_singleton()->material_get_data(scene_globals.overdraw_material, RS::SHADER_SPATIAL));
  3538. }
  3539. {
  3540. // Initialize Sky stuff
  3541. sky_globals.roughness_layers = GLOBAL_GET("rendering/reflections/sky_reflections/roughness_layers");
  3542. String global_defines;
  3543. global_defines += "#define MAX_GLOBAL_SHADER_UNIFORMS 256\n"; // TODO: this is arbitrary for now
  3544. global_defines += "\n#define MAX_DIRECTIONAL_LIGHT_DATA_STRUCTS " + itos(sky_globals.max_directional_lights) + "\n";
  3545. material_storage->shaders.sky_shader.initialize(global_defines);
  3546. sky_globals.shader_default_version = material_storage->shaders.sky_shader.version_create();
  3547. }
  3548. {
  3549. sky_globals.default_shader = material_storage->shader_allocate();
  3550. material_storage->shader_initialize(sky_globals.default_shader);
  3551. material_storage->shader_set_code(sky_globals.default_shader, R"(
  3552. // Default sky shader.
  3553. shader_type sky;
  3554. void sky() {
  3555. COLOR = vec3(0.0);
  3556. }
  3557. )");
  3558. sky_globals.default_material = material_storage->material_allocate();
  3559. material_storage->material_initialize(sky_globals.default_material);
  3560. material_storage->material_set_shader(sky_globals.default_material, sky_globals.default_shader);
  3561. }
  3562. {
  3563. sky_globals.fog_shader = material_storage->shader_allocate();
  3564. material_storage->shader_initialize(sky_globals.fog_shader);
  3565. material_storage->shader_set_code(sky_globals.fog_shader, R"(
  3566. // Default clear color sky shader.
  3567. shader_type sky;
  3568. uniform vec4 clear_color;
  3569. void sky() {
  3570. COLOR = clear_color.rgb;
  3571. }
  3572. )");
  3573. sky_globals.fog_material = material_storage->material_allocate();
  3574. material_storage->material_initialize(sky_globals.fog_material);
  3575. material_storage->material_set_shader(sky_globals.fog_material, sky_globals.fog_shader);
  3576. }
  3577. {
  3578. glGenVertexArrays(1, &sky_globals.screen_triangle_array);
  3579. glBindVertexArray(sky_globals.screen_triangle_array);
  3580. glGenBuffers(1, &sky_globals.screen_triangle);
  3581. glBindBuffer(GL_ARRAY_BUFFER, sky_globals.screen_triangle);
  3582. const float qv[6] = {
  3583. -1.0f,
  3584. -1.0f,
  3585. 3.0f,
  3586. -1.0f,
  3587. -1.0f,
  3588. 3.0f,
  3589. };
  3590. GLES3::Utilities::get_singleton()->buffer_allocate_data(GL_ARRAY_BUFFER, sky_globals.screen_triangle, sizeof(float) * 6, qv, GL_STATIC_DRAW, "Screen triangle vertex buffer");
  3591. glVertexAttribPointer(RS::ARRAY_VERTEX, 2, GL_FLOAT, GL_FALSE, sizeof(float) * 2, nullptr);
  3592. glEnableVertexAttribArray(RS::ARRAY_VERTEX);
  3593. glBindVertexArray(0);
  3594. glBindBuffer(GL_ARRAY_BUFFER, 0); //unbind
  3595. }
  3596. #ifdef GL_API_ENABLED
  3597. if (RasterizerGLES3::is_gles_over_gl()) {
  3598. glEnable(_EXT_TEXTURE_CUBE_MAP_SEAMLESS);
  3599. }
  3600. #endif // GL_API_ENABLED
  3601. // MultiMesh may read from color when color is disabled, so make sure that the color defaults to white instead of black;
  3602. glVertexAttrib4f(RS::ARRAY_COLOR, 1.0, 1.0, 1.0, 1.0);
  3603. }
  3604. RasterizerSceneGLES3::~RasterizerSceneGLES3() {
  3605. GLES3::Utilities::get_singleton()->buffer_free_data(scene_state.directional_light_buffer);
  3606. GLES3::Utilities::get_singleton()->buffer_free_data(scene_state.omni_light_buffer);
  3607. GLES3::Utilities::get_singleton()->buffer_free_data(scene_state.spot_light_buffer);
  3608. GLES3::Utilities::get_singleton()->buffer_free_data(scene_state.positional_shadow_buffer);
  3609. GLES3::Utilities::get_singleton()->buffer_free_data(scene_state.directional_shadow_buffer);
  3610. memdelete_arr(scene_state.directional_lights);
  3611. memdelete_arr(scene_state.omni_lights);
  3612. memdelete_arr(scene_state.spot_lights);
  3613. memdelete_arr(scene_state.omni_light_sort);
  3614. memdelete_arr(scene_state.spot_light_sort);
  3615. memdelete_arr(scene_state.positional_shadows);
  3616. memdelete_arr(scene_state.directional_shadows);
  3617. // Scene Shader
  3618. GLES3::MaterialStorage::get_singleton()->shaders.scene_shader.version_free(scene_globals.shader_default_version);
  3619. RSG::material_storage->material_free(scene_globals.default_material);
  3620. RSG::material_storage->shader_free(scene_globals.default_shader);
  3621. // Overdraw Shader
  3622. RSG::material_storage->material_free(scene_globals.overdraw_material);
  3623. RSG::material_storage->shader_free(scene_globals.overdraw_shader);
  3624. // Sky Shader
  3625. GLES3::MaterialStorage::get_singleton()->shaders.sky_shader.version_free(sky_globals.shader_default_version);
  3626. RSG::material_storage->material_free(sky_globals.default_material);
  3627. RSG::material_storage->shader_free(sky_globals.default_shader);
  3628. RSG::material_storage->material_free(sky_globals.fog_material);
  3629. RSG::material_storage->shader_free(sky_globals.fog_shader);
  3630. GLES3::Utilities::get_singleton()->buffer_free_data(sky_globals.screen_triangle);
  3631. glDeleteVertexArrays(1, &sky_globals.screen_triangle_array);
  3632. GLES3::Utilities::get_singleton()->buffer_free_data(sky_globals.directional_light_buffer);
  3633. memdelete_arr(sky_globals.directional_lights);
  3634. memdelete_arr(sky_globals.last_frame_directional_lights);
  3635. // UBOs
  3636. if (scene_state.ubo_buffer != 0) {
  3637. GLES3::Utilities::get_singleton()->buffer_free_data(scene_state.ubo_buffer);
  3638. }
  3639. if (scene_state.multiview_buffer != 0) {
  3640. GLES3::Utilities::get_singleton()->buffer_free_data(scene_state.multiview_buffer);
  3641. }
  3642. if (scene_state.tonemap_buffer != 0) {
  3643. GLES3::Utilities::get_singleton()->buffer_free_data(scene_state.tonemap_buffer);
  3644. }
  3645. singleton = nullptr;
  3646. }
  3647. #endif // GLES3_ENABLED