rasterizer_scene_gles2.cpp 148 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093
  1. /*************************************************************************/
  2. /* rasterizer_scene_gles2.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #include "rasterizer_scene_gles2.h"
  31. #include "core/math/math_funcs.h"
  32. #include "core/math/transform.h"
  33. #include "core/os/os.h"
  34. #include "core/project_settings.h"
  35. #include "core/vmap.h"
  36. #include "rasterizer_canvas_gles2.h"
  37. #include "servers/camera/camera_feed.h"
  38. #include "servers/rendering/rendering_server_raster.h"
  39. #ifndef GLES_OVER_GL
  40. #define glClearDepth glClearDepthf
  41. #endif
  42. #ifndef GLES_OVER_GL
  43. #ifdef IPHONE_ENABLED
  44. #include <OpenGLES/ES2/glext.h>
  45. //void *glResolveMultisampleFramebufferAPPLE;
  46. #define GL_READ_FRAMEBUFFER 0x8CA8
  47. #define GL_DRAW_FRAMEBUFFER 0x8CA9
  48. #endif
  49. #endif
  50. #if !defined(GLES_OVER_GL)
  51. #define GL_TEXTURE_2D_ARRAY 0x8C1A
  52. #define GL_TEXTURE_3D 0x806F
  53. #endif
  54. static const GLenum _cube_side_enum[6] = {
  55. GL_TEXTURE_CUBE_MAP_NEGATIVE_X,
  56. GL_TEXTURE_CUBE_MAP_POSITIVE_X,
  57. GL_TEXTURE_CUBE_MAP_NEGATIVE_Y,
  58. GL_TEXTURE_CUBE_MAP_POSITIVE_Y,
  59. GL_TEXTURE_CUBE_MAP_NEGATIVE_Z,
  60. GL_TEXTURE_CUBE_MAP_POSITIVE_Z,
  61. };
  62. /* SHADOW ATLAS API */
  63. RID RasterizerSceneGLES2::shadow_atlas_create() {
  64. ShadowAtlas *shadow_atlas = memnew(ShadowAtlas);
  65. shadow_atlas->fbo = 0;
  66. shadow_atlas->depth = 0;
  67. shadow_atlas->color = 0;
  68. shadow_atlas->size = 0;
  69. shadow_atlas->smallest_subdiv = 0;
  70. for (int i = 0; i < 4; i++) {
  71. shadow_atlas->size_order[i] = i;
  72. }
  73. return shadow_atlas_owner.make_rid(shadow_atlas);
  74. }
  75. void RasterizerSceneGLES2::shadow_atlas_set_size(RID p_atlas, int p_size) {
  76. ShadowAtlas *shadow_atlas = shadow_atlas_owner.getornull(p_atlas);
  77. ERR_FAIL_COND(!shadow_atlas);
  78. ERR_FAIL_COND(p_size < 0);
  79. p_size = next_power_of_2(p_size);
  80. if (p_size == shadow_atlas->size)
  81. return;
  82. // erase the old atlast
  83. if (shadow_atlas->fbo) {
  84. if (storage->config.use_rgba_3d_shadows) {
  85. glDeleteRenderbuffers(1, &shadow_atlas->depth);
  86. } else {
  87. glDeleteTextures(1, &shadow_atlas->depth);
  88. }
  89. glDeleteFramebuffers(1, &shadow_atlas->fbo);
  90. if (shadow_atlas->color) {
  91. glDeleteTextures(1, &shadow_atlas->color);
  92. }
  93. shadow_atlas->fbo = 0;
  94. shadow_atlas->depth = 0;
  95. shadow_atlas->color = 0;
  96. }
  97. // erase shadow atlast references from lights
  98. for (Map<RID, uint32_t>::Element *E = shadow_atlas->shadow_owners.front(); E; E = E->next()) {
  99. LightInstance *li = light_instance_owner.getornull(E->key());
  100. ERR_CONTINUE(!li);
  101. li->shadow_atlases.erase(p_atlas);
  102. }
  103. shadow_atlas->shadow_owners.clear();
  104. shadow_atlas->size = p_size;
  105. if (shadow_atlas->size) {
  106. glGenFramebuffers(1, &shadow_atlas->fbo);
  107. glBindFramebuffer(GL_FRAMEBUFFER, shadow_atlas->fbo);
  108. // create a depth texture
  109. glActiveTexture(GL_TEXTURE0);
  110. if (storage->config.use_rgba_3d_shadows) {
  111. //maximum compatibility, renderbuffer and RGBA shadow
  112. glGenRenderbuffers(1, &shadow_atlas->depth);
  113. glBindRenderbuffer(GL_RENDERBUFFER, shadow_atlas->depth);
  114. glRenderbufferStorage(GL_RENDERBUFFER, storage->config.depth_internalformat, shadow_atlas->size, shadow_atlas->size);
  115. glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, shadow_atlas->depth);
  116. glGenTextures(1, &shadow_atlas->color);
  117. glBindTexture(GL_TEXTURE_2D, shadow_atlas->color);
  118. glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, shadow_atlas->size, shadow_atlas->size, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
  119. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  120. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  121. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  122. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  123. glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, shadow_atlas->color, 0);
  124. } else {
  125. //just depth texture
  126. glGenTextures(1, &shadow_atlas->depth);
  127. glBindTexture(GL_TEXTURE_2D, shadow_atlas->depth);
  128. glTexImage2D(GL_TEXTURE_2D, 0, storage->config.depth_internalformat, shadow_atlas->size, shadow_atlas->size, 0, GL_DEPTH_COMPONENT, storage->config.depth_type, NULL);
  129. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  130. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  131. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  132. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  133. glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, shadow_atlas->depth, 0);
  134. }
  135. glViewport(0, 0, shadow_atlas->size, shadow_atlas->size);
  136. glDepthMask(GL_TRUE);
  137. glClearDepth(0.0f);
  138. glClear(GL_DEPTH_BUFFER_BIT);
  139. glBindFramebuffer(GL_FRAMEBUFFER, 0);
  140. }
  141. }
  142. void RasterizerSceneGLES2::shadow_atlas_set_quadrant_subdivision(RID p_atlas, int p_quadrant, int p_subdivision) {
  143. ShadowAtlas *shadow_atlas = shadow_atlas_owner.getornull(p_atlas);
  144. ERR_FAIL_COND(!shadow_atlas);
  145. ERR_FAIL_INDEX(p_quadrant, 4);
  146. ERR_FAIL_INDEX(p_subdivision, 16384);
  147. uint32_t subdiv = next_power_of_2(p_subdivision);
  148. if (subdiv & 0xaaaaaaaa) { // sqrt(subdiv) must be integer
  149. subdiv <<= 1;
  150. }
  151. subdiv = int(Math::sqrt((float)subdiv));
  152. if (shadow_atlas->quadrants[p_quadrant].shadows.size() == (int)subdiv)
  153. return;
  154. // erase all data from the quadrant
  155. for (int i = 0; i < shadow_atlas->quadrants[p_quadrant].shadows.size(); i++) {
  156. if (shadow_atlas->quadrants[p_quadrant].shadows[i].owner.is_valid()) {
  157. shadow_atlas->shadow_owners.erase(shadow_atlas->quadrants[p_quadrant].shadows[i].owner);
  158. LightInstance *li = light_instance_owner.getornull(shadow_atlas->quadrants[p_quadrant].shadows[i].owner);
  159. ERR_CONTINUE(!li);
  160. li->shadow_atlases.erase(p_atlas);
  161. }
  162. }
  163. shadow_atlas->quadrants[p_quadrant].shadows.resize(0);
  164. shadow_atlas->quadrants[p_quadrant].shadows.resize(subdiv);
  165. shadow_atlas->quadrants[p_quadrant].subdivision = subdiv;
  166. // cache the smallest subdivision for faster allocations
  167. shadow_atlas->smallest_subdiv = 1 << 30;
  168. for (int i = 0; i < 4; i++) {
  169. if (shadow_atlas->quadrants[i].subdivision) {
  170. shadow_atlas->smallest_subdiv = MIN(shadow_atlas->smallest_subdiv, shadow_atlas->quadrants[i].subdivision);
  171. }
  172. }
  173. if (shadow_atlas->smallest_subdiv == 1 << 30) {
  174. shadow_atlas->smallest_subdiv = 0;
  175. }
  176. // re-sort the quadrants
  177. int swaps = 0;
  178. do {
  179. swaps = 0;
  180. for (int i = 0; i < 3; i++) {
  181. if (shadow_atlas->quadrants[shadow_atlas->size_order[i]].subdivision < shadow_atlas->quadrants[shadow_atlas->size_order[i + 1]].subdivision) {
  182. SWAP(shadow_atlas->size_order[i], shadow_atlas->size_order[i + 1]);
  183. swaps++;
  184. }
  185. }
  186. } while (swaps > 0);
  187. }
  188. bool RasterizerSceneGLES2::_shadow_atlas_find_shadow(ShadowAtlas *shadow_atlas, int *p_in_quadrants, int p_quadrant_count, int p_current_subdiv, uint64_t p_tick, int &r_quadrant, int &r_shadow) {
  189. for (int i = p_quadrant_count - 1; i >= 0; i--) {
  190. int qidx = p_in_quadrants[i];
  191. if (shadow_atlas->quadrants[qidx].subdivision == (uint32_t)p_current_subdiv) {
  192. return false;
  193. }
  194. // look for an empty space
  195. int sc = shadow_atlas->quadrants[qidx].shadows.size();
  196. ShadowAtlas::Quadrant::Shadow *sarr = shadow_atlas->quadrants[qidx].shadows.ptrw();
  197. int found_free_idx = -1; // found a free one
  198. int found_used_idx = -1; // found an existing one, must steal it
  199. uint64_t min_pass = 0; // pass of the existing one, try to use the least recently
  200. for (int j = 0; j < sc; j++) {
  201. if (!sarr[j].owner.is_valid()) {
  202. found_free_idx = j;
  203. break;
  204. }
  205. LightInstance *sli = light_instance_owner.getornull(sarr[j].owner);
  206. ERR_CONTINUE(!sli);
  207. if (sli->last_scene_pass != scene_pass) {
  208. // was just allocated, don't kill it so soon, wait a bit...
  209. if (p_tick - sarr[j].alloc_tick < shadow_atlas_realloc_tolerance_msec) {
  210. continue;
  211. }
  212. if (found_used_idx == -1 || sli->last_scene_pass < min_pass) {
  213. found_used_idx = j;
  214. min_pass = sli->last_scene_pass;
  215. }
  216. }
  217. }
  218. if (found_free_idx == -1 && found_used_idx == -1) {
  219. continue; // nothing found
  220. }
  221. if (found_free_idx == -1 && found_used_idx != -1) {
  222. found_free_idx = found_used_idx;
  223. }
  224. r_quadrant = qidx;
  225. r_shadow = found_free_idx;
  226. return true;
  227. }
  228. return false;
  229. }
  230. bool RasterizerSceneGLES2::shadow_atlas_update_light(RID p_atlas, RID p_light_intance, float p_coverage, uint64_t p_light_version) {
  231. ShadowAtlas *shadow_atlas = shadow_atlas_owner.getornull(p_atlas);
  232. ERR_FAIL_COND_V(!shadow_atlas, false);
  233. LightInstance *li = light_instance_owner.getornull(p_light_intance);
  234. ERR_FAIL_COND_V(!li, false);
  235. if (shadow_atlas->size == 0 || shadow_atlas->smallest_subdiv == 0) {
  236. return false;
  237. }
  238. uint32_t quad_size = shadow_atlas->size >> 1;
  239. int desired_fit = MIN(quad_size / shadow_atlas->smallest_subdiv, next_power_of_2(quad_size * p_coverage));
  240. int valid_quadrants[4];
  241. int valid_quadrant_count = 0;
  242. int best_size = -1;
  243. int best_subdiv = -1;
  244. for (int i = 0; i < 4; i++) {
  245. int q = shadow_atlas->size_order[i];
  246. int sd = shadow_atlas->quadrants[q].subdivision;
  247. if (sd == 0) {
  248. continue;
  249. }
  250. int max_fit = quad_size / sd;
  251. if (best_size != -1 && max_fit > best_size) {
  252. break; // what we asked for is bigger than this.
  253. }
  254. valid_quadrants[valid_quadrant_count] = q;
  255. valid_quadrant_count++;
  256. best_subdiv = sd;
  257. if (max_fit >= desired_fit) {
  258. best_size = max_fit;
  259. }
  260. }
  261. ERR_FAIL_COND_V(valid_quadrant_count == 0, false); // no suitable block available
  262. uint64_t tick = OS::get_singleton()->get_ticks_msec();
  263. if (shadow_atlas->shadow_owners.has(p_light_intance)) {
  264. // light was already known!
  265. uint32_t key = shadow_atlas->shadow_owners[p_light_intance];
  266. uint32_t q = (key >> ShadowAtlas::QUADRANT_SHIFT) & 0x3;
  267. uint32_t s = key & ShadowAtlas::SHADOW_INDEX_MASK;
  268. bool should_realloc = shadow_atlas->quadrants[q].subdivision != (uint32_t)best_subdiv && (shadow_atlas->quadrants[q].shadows[s].alloc_tick - tick > shadow_atlas_realloc_tolerance_msec);
  269. bool should_redraw = shadow_atlas->quadrants[q].shadows[s].version != p_light_version;
  270. if (!should_realloc) {
  271. shadow_atlas->quadrants[q].shadows.write[s].version = p_light_version;
  272. return should_redraw;
  273. }
  274. int new_quadrant;
  275. int new_shadow;
  276. // find a better place
  277. if (_shadow_atlas_find_shadow(shadow_atlas, valid_quadrants, valid_quadrant_count, shadow_atlas->quadrants[q].subdivision, tick, new_quadrant, new_shadow)) {
  278. // found a better place
  279. ShadowAtlas::Quadrant::Shadow *sh = &shadow_atlas->quadrants[new_quadrant].shadows.write[new_shadow];
  280. if (sh->owner.is_valid()) {
  281. // it is take but invalid, so we can take it
  282. shadow_atlas->shadow_owners.erase(sh->owner);
  283. LightInstance *sli = light_instance_owner.getornull(sh->owner);
  284. sli->shadow_atlases.erase(p_atlas);
  285. }
  286. // erase previous
  287. shadow_atlas->quadrants[q].shadows.write[s].version = 0;
  288. shadow_atlas->quadrants[q].shadows.write[s].owner = RID();
  289. sh->owner = p_light_intance;
  290. sh->alloc_tick = tick;
  291. sh->version = p_light_version;
  292. li->shadow_atlases.insert(p_atlas);
  293. // make a new key
  294. key = new_quadrant << ShadowAtlas::QUADRANT_SHIFT;
  295. key |= new_shadow;
  296. // update it in the map
  297. shadow_atlas->shadow_owners[p_light_intance] = key;
  298. // make it dirty, so we redraw
  299. return true;
  300. }
  301. // no better place found, so we keep the current place
  302. shadow_atlas->quadrants[q].shadows.write[s].version = p_light_version;
  303. return should_redraw;
  304. }
  305. int new_quadrant;
  306. int new_shadow;
  307. if (_shadow_atlas_find_shadow(shadow_atlas, valid_quadrants, valid_quadrant_count, -1, tick, new_quadrant, new_shadow)) {
  308. // found a better place
  309. ShadowAtlas::Quadrant::Shadow *sh = &shadow_atlas->quadrants[new_quadrant].shadows.write[new_shadow];
  310. if (sh->owner.is_valid()) {
  311. // it is take but invalid, so we can take it
  312. shadow_atlas->shadow_owners.erase(sh->owner);
  313. LightInstance *sli = light_instance_owner.getornull(sh->owner);
  314. sli->shadow_atlases.erase(p_atlas);
  315. }
  316. sh->owner = p_light_intance;
  317. sh->alloc_tick = tick;
  318. sh->version = p_light_version;
  319. li->shadow_atlases.insert(p_atlas);
  320. // make a new key
  321. uint32_t key = new_quadrant << ShadowAtlas::QUADRANT_SHIFT;
  322. key |= new_shadow;
  323. // update it in the map
  324. shadow_atlas->shadow_owners[p_light_intance] = key;
  325. // make it dirty, so we redraw
  326. return true;
  327. }
  328. return false;
  329. }
  330. void RasterizerSceneGLES2::set_directional_shadow_count(int p_count) {
  331. directional_shadow.light_count = p_count;
  332. directional_shadow.current_light = 0;
  333. }
  334. int RasterizerSceneGLES2::get_directional_light_shadow_size(RID p_light_intance) {
  335. ERR_FAIL_COND_V(directional_shadow.light_count == 0, 0);
  336. int shadow_size;
  337. if (directional_shadow.light_count == 1) {
  338. shadow_size = directional_shadow.size;
  339. } else {
  340. shadow_size = directional_shadow.size / 2; //more than 4 not supported anyway
  341. }
  342. LightInstance *light_instance = light_instance_owner.getornull(p_light_intance);
  343. ERR_FAIL_COND_V(!light_instance, 0);
  344. switch (light_instance->light_ptr->directional_shadow_mode) {
  345. case RS::LIGHT_DIRECTIONAL_SHADOW_ORTHOGONAL:
  346. break; //none
  347. case RS::LIGHT_DIRECTIONAL_SHADOW_PARALLEL_2_SPLITS:
  348. case RS::LIGHT_DIRECTIONAL_SHADOW_PARALLEL_4_SPLITS:
  349. shadow_size /= 2;
  350. break;
  351. }
  352. return shadow_size;
  353. }
  354. //////////////////////////////////////////////////////
  355. RID RasterizerSceneGLES2::reflection_atlas_create() {
  356. return RID();
  357. }
  358. void RasterizerSceneGLES2::reflection_atlas_set_size(RID p_ref_atlas, int p_size) {
  359. }
  360. void RasterizerSceneGLES2::reflection_atlas_set_subdivision(RID p_ref_atlas, int p_subdiv) {
  361. }
  362. ////////////////////////////////////////////////////
  363. RID RasterizerSceneGLES2::reflection_probe_instance_create(RID p_probe) {
  364. RasterizerStorageGLES2::ReflectionProbe *probe = storage->reflection_probe_owner.getornull(p_probe);
  365. ERR_FAIL_COND_V(!probe, RID());
  366. ReflectionProbeInstance *rpi = memnew(ReflectionProbeInstance);
  367. rpi->probe_ptr = probe;
  368. rpi->self = reflection_probe_instance_owner.make_rid(rpi);
  369. rpi->probe = p_probe;
  370. rpi->reflection_atlas_index = -1;
  371. rpi->render_step = -1;
  372. rpi->last_pass = 0;
  373. rpi->current_resolution = 0;
  374. rpi->dirty = true;
  375. rpi->index = 0;
  376. for (int i = 0; i < 6; i++) {
  377. glGenFramebuffers(1, &rpi->fbo[i]);
  378. glGenTextures(1, &rpi->color[i]);
  379. }
  380. glGenRenderbuffers(1, &rpi->depth);
  381. rpi->cubemap = 0;
  382. //glGenTextures(1, &rpi->cubemap);
  383. return rpi->self;
  384. }
  385. void RasterizerSceneGLES2::reflection_probe_instance_set_transform(RID p_instance, const Transform &p_transform) {
  386. ReflectionProbeInstance *rpi = reflection_probe_instance_owner.getornull(p_instance);
  387. ERR_FAIL_COND(!rpi);
  388. rpi->transform = p_transform;
  389. }
  390. void RasterizerSceneGLES2::reflection_probe_release_atlas_index(RID p_instance) {
  391. }
  392. bool RasterizerSceneGLES2::reflection_probe_instance_needs_redraw(RID p_instance) {
  393. const ReflectionProbeInstance *rpi = reflection_probe_instance_owner.getornull(p_instance);
  394. ERR_FAIL_COND_V(!rpi, false);
  395. bool need_redraw = rpi->probe_ptr->resolution != rpi->current_resolution || rpi->dirty || rpi->probe_ptr->update_mode == RS::REFLECTION_PROBE_UPDATE_ALWAYS;
  396. rpi->dirty = false;
  397. return need_redraw;
  398. }
  399. bool RasterizerSceneGLES2::reflection_probe_instance_has_reflection(RID p_instance) {
  400. return true;
  401. }
  402. bool RasterizerSceneGLES2::reflection_probe_instance_begin_render(RID p_instance, RID p_reflection_atlas) {
  403. ReflectionProbeInstance *rpi = reflection_probe_instance_owner.getornull(p_instance);
  404. ERR_FAIL_COND_V(!rpi, false);
  405. rpi->render_step = 0;
  406. if (rpi->probe_ptr->resolution != rpi->current_resolution) {
  407. //update cubemap if resolution changed
  408. int size = rpi->probe_ptr->resolution;
  409. rpi->current_resolution = size;
  410. GLenum internal_format = GL_RGB;
  411. GLenum format = GL_RGB;
  412. GLenum type = GL_UNSIGNED_BYTE;
  413. glActiveTexture(GL_TEXTURE0);
  414. glBindRenderbuffer(GL_RENDERBUFFER, rpi->depth);
  415. glRenderbufferStorage(GL_RENDERBUFFER, storage->config.depth_internalformat, size, size);
  416. if (rpi->cubemap != 0) {
  417. glDeleteTextures(1, &rpi->cubemap);
  418. }
  419. glGenTextures(1, &rpi->cubemap);
  420. glBindTexture(GL_TEXTURE_CUBE_MAP, rpi->cubemap);
  421. // Mobile hardware (PowerVR specially) prefers this approach,
  422. // the previous approach with manual lod levels kills the game.
  423. for (int i = 0; i < 6; i++) {
  424. glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + i, 0, internal_format, size, size, 0, format, type, NULL);
  425. }
  426. glGenerateMipmap(GL_TEXTURE_CUBE_MAP);
  427. // Generate framebuffers for rendering
  428. for (int i = 0; i < 6; i++) {
  429. glBindFramebuffer(GL_FRAMEBUFFER, rpi->fbo[i]);
  430. glBindTexture(GL_TEXTURE_2D, rpi->color[i]);
  431. glTexImage2D(GL_TEXTURE_2D, 0, internal_format, size, size, 0, format, type, NULL);
  432. glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, rpi->color[i], 0);
  433. glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, rpi->depth);
  434. GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
  435. ERR_CONTINUE(status != GL_FRAMEBUFFER_COMPLETE);
  436. }
  437. glTexParameterf(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
  438. glTexParameterf(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  439. glTexParameterf(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  440. glTexParameterf(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  441. glBindFramebuffer(GL_FRAMEBUFFER, RasterizerStorageGLES2::system_fbo);
  442. }
  443. return true;
  444. }
  445. bool RasterizerSceneGLES2::reflection_probe_instance_postprocess_step(RID p_instance) {
  446. ReflectionProbeInstance *rpi = reflection_probe_instance_owner.getornull(p_instance);
  447. ERR_FAIL_COND_V(!rpi, false);
  448. ERR_FAIL_COND_V(rpi->current_resolution == 0, false);
  449. int size = rpi->probe_ptr->resolution;
  450. {
  451. glBindBuffer(GL_ARRAY_BUFFER, 0);
  452. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
  453. glDisable(GL_CULL_FACE);
  454. glDisable(GL_DEPTH_TEST);
  455. glDisable(GL_SCISSOR_TEST);
  456. glDisable(GL_BLEND);
  457. glDepthMask(GL_FALSE);
  458. for (int i = 0; i < RS::ARRAY_MAX - 1; i++) {
  459. glDisableVertexAttribArray(i);
  460. }
  461. }
  462. glActiveTexture(GL_TEXTURE0);
  463. glBindTexture(GL_TEXTURE_CUBE_MAP, rpi->cubemap);
  464. glTexParameterf(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_LINEAR); //use linear, no mipmaps so it does not read from what is being written to
  465. //first of all, copy rendered textures to cubemap
  466. for (int i = 0; i < 6; i++) {
  467. glBindFramebuffer(GL_FRAMEBUFFER, rpi->fbo[i]);
  468. glViewport(0, 0, size, size);
  469. glCopyTexSubImage2D(_cube_side_enum[i], 0, 0, 0, 0, 0, size, size);
  470. }
  471. //do filtering
  472. //vdc cache
  473. glActiveTexture(GL_TEXTURE1);
  474. glBindTexture(GL_TEXTURE_2D, storage->resources.radical_inverse_vdc_cache_tex);
  475. // now render to the framebuffer, mipmap level for mipmap level
  476. int lod = 1;
  477. size >>= 1;
  478. int mipmaps = 6;
  479. storage->shaders.cubemap_filter.set_conditional(CubemapFilterShaderGLES2::USE_SOURCE_PANORAMA, false);
  480. storage->shaders.cubemap_filter.bind();
  481. glBindFramebuffer(GL_FRAMEBUFFER, storage->resources.mipmap_blur_fbo);
  482. //blur
  483. while (size >= 1) {
  484. glActiveTexture(GL_TEXTURE3);
  485. glBindTexture(GL_TEXTURE_2D, storage->resources.mipmap_blur_color);
  486. glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, size, size, 0, GL_RGB, GL_UNSIGNED_BYTE, NULL);
  487. glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, storage->resources.mipmap_blur_color, 0);
  488. glViewport(0, 0, size, size);
  489. glActiveTexture(GL_TEXTURE0);
  490. for (int i = 0; i < 6; i++) {
  491. storage->bind_quad_array();
  492. storage->shaders.cubemap_filter.set_uniform(CubemapFilterShaderGLES2::FACE_ID, i);
  493. float roughness = CLAMP(lod / (float)(mipmaps - 1), 0, 1);
  494. storage->shaders.cubemap_filter.set_uniform(CubemapFilterShaderGLES2::ROUGHNESS, roughness);
  495. storage->shaders.cubemap_filter.set_uniform(CubemapFilterShaderGLES2::Z_FLIP, false);
  496. glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
  497. glCopyTexSubImage2D(_cube_side_enum[i], lod, 0, 0, 0, 0, size, size);
  498. }
  499. size >>= 1;
  500. lod++;
  501. }
  502. // restore ranges
  503. glActiveTexture(GL_TEXTURE0);
  504. glTexParameterf(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
  505. glBindTexture(GL_TEXTURE_2D, 0);
  506. glActiveTexture(GL_TEXTURE3); //back to panorama
  507. glBindTexture(GL_TEXTURE_2D, 0);
  508. glActiveTexture(GL_TEXTURE1);
  509. glBindTexture(GL_TEXTURE_2D, 0);
  510. glBindFramebuffer(GL_FRAMEBUFFER, RasterizerStorageGLES2::system_fbo);
  511. return true;
  512. }
  513. /* ENVIRONMENT API */
  514. RID RasterizerSceneGLES2::environment_create() {
  515. Environment *env = memnew(Environment);
  516. return environment_owner.make_rid(env);
  517. }
  518. void RasterizerSceneGLES2::environment_set_background(RID p_env, RS::EnvironmentBG p_bg) {
  519. Environment *env = environment_owner.getornull(p_env);
  520. ERR_FAIL_COND(!env);
  521. env->bg_mode = p_bg;
  522. }
  523. void RasterizerSceneGLES2::environment_set_sky(RID p_env, RID p_sky) {
  524. Environment *env = environment_owner.getornull(p_env);
  525. ERR_FAIL_COND(!env);
  526. env->sky = p_sky;
  527. }
  528. void RasterizerSceneGLES2::environment_set_sky_custom_fov(RID p_env, float p_scale) {
  529. Environment *env = environment_owner.getornull(p_env);
  530. ERR_FAIL_COND(!env);
  531. env->sky_custom_fov = p_scale;
  532. }
  533. void RasterizerSceneGLES2::environment_set_sky_orientation(RID p_env, const Basis &p_orientation) {
  534. Environment *env = environment_owner.getornull(p_env);
  535. ERR_FAIL_COND(!env);
  536. env->sky_orientation = p_orientation;
  537. }
  538. void RasterizerSceneGLES2::environment_set_bg_color(RID p_env, const Color &p_color) {
  539. Environment *env = environment_owner.getornull(p_env);
  540. ERR_FAIL_COND(!env);
  541. env->bg_color = p_color;
  542. }
  543. void RasterizerSceneGLES2::environment_set_bg_energy(RID p_env, float p_energy) {
  544. Environment *env = environment_owner.getornull(p_env);
  545. ERR_FAIL_COND(!env);
  546. env->bg_energy = p_energy;
  547. }
  548. void RasterizerSceneGLES2::environment_set_canvas_max_layer(RID p_env, int p_max_layer) {
  549. Environment *env = environment_owner.getornull(p_env);
  550. ERR_FAIL_COND(!env);
  551. env->canvas_max_layer = p_max_layer;
  552. }
  553. void RasterizerSceneGLES2::environment_set_ambient_light(RID p_env, const Color &p_color, float p_energy, float p_sky_contribution) {
  554. Environment *env = environment_owner.getornull(p_env);
  555. ERR_FAIL_COND(!env);
  556. env->ambient_color = p_color;
  557. env->ambient_energy = p_energy;
  558. env->ambient_sky_contribution = p_sky_contribution;
  559. }
  560. void RasterizerSceneGLES2::environment_set_camera_feed_id(RID p_env, int p_camera_feed_id) {
  561. Environment *env = environment_owner.getornull(p_env);
  562. ERR_FAIL_COND(!env);
  563. env->camera_feed_id = p_camera_feed_id;
  564. }
  565. void RasterizerSceneGLES2::environment_set_dof_blur_far(RID p_env, bool p_enable, float p_distance, float p_transition, float p_amount, RS::EnvironmentDOFBlurQuality p_quality) {
  566. Environment *env = environment_owner.getornull(p_env);
  567. ERR_FAIL_COND(!env);
  568. env->dof_blur_far_enabled = p_enable;
  569. env->dof_blur_far_distance = p_distance;
  570. env->dof_blur_far_transition = p_transition;
  571. env->dof_blur_far_amount = p_amount;
  572. env->dof_blur_far_quality = p_quality;
  573. }
  574. void RasterizerSceneGLES2::environment_set_dof_blur_near(RID p_env, bool p_enable, float p_distance, float p_transition, float p_amount, RS::EnvironmentDOFBlurQuality p_quality) {
  575. Environment *env = environment_owner.getornull(p_env);
  576. ERR_FAIL_COND(!env);
  577. env->dof_blur_near_enabled = p_enable;
  578. env->dof_blur_near_distance = p_distance;
  579. env->dof_blur_near_transition = p_transition;
  580. env->dof_blur_near_amount = p_amount;
  581. env->dof_blur_near_quality = p_quality;
  582. }
  583. void RasterizerSceneGLES2::environment_set_glow(RID p_env, bool p_enable, int p_level_flags, float p_intensity, float p_strength, float p_bloom_threshold, RS::EnvironmentGlowBlendMode p_blend_mode, float p_hdr_bleed_threshold, float p_hdr_bleed_scale, float p_hdr_luminance_cap, bool p_bicubic_upscale) {
  584. Environment *env = environment_owner.getornull(p_env);
  585. ERR_FAIL_COND(!env);
  586. env->glow_enabled = p_enable;
  587. env->glow_levels = p_level_flags;
  588. env->glow_intensity = p_intensity;
  589. env->glow_strength = p_strength;
  590. env->glow_bloom = p_bloom_threshold;
  591. env->glow_blend_mode = p_blend_mode;
  592. env->glow_hdr_bleed_threshold = p_hdr_bleed_threshold;
  593. env->glow_hdr_bleed_scale = p_hdr_bleed_scale;
  594. env->glow_hdr_luminance_cap = p_hdr_luminance_cap;
  595. env->glow_bicubic_upscale = p_bicubic_upscale;
  596. }
  597. void RasterizerSceneGLES2::environment_set_fog(RID p_env, bool p_enable, float p_begin, float p_end, RID p_gradient_texture) {
  598. Environment *env = environment_owner.getornull(p_env);
  599. ERR_FAIL_COND(!env);
  600. }
  601. void RasterizerSceneGLES2::environment_set_ssr(RID p_env, bool p_enable, int p_max_steps, float p_fade_in, float p_fade_out, float p_depth_tolerance, bool p_roughness) {
  602. Environment *env = environment_owner.getornull(p_env);
  603. ERR_FAIL_COND(!env);
  604. }
  605. void RasterizerSceneGLES2::environment_set_ssao(RID p_env, bool p_enable, float p_radius, float p_intensity, float p_radius2, float p_intensity2, float p_bias, float p_light_affect, float p_ao_channel_affect, const Color &p_color, RS::EnvironmentSSAOQuality p_quality, RenderingServer::EnvironmentSSAOBlur p_blur, float p_bilateral_sharpness) {
  606. Environment *env = environment_owner.getornull(p_env);
  607. ERR_FAIL_COND(!env);
  608. }
  609. void RasterizerSceneGLES2::environment_set_tonemap(RID p_env, RS::EnvironmentToneMapper p_tone_mapper, float p_exposure, float p_white, bool p_auto_exposure, float p_min_luminance, float p_max_luminance, float p_auto_exp_speed, float p_auto_exp_scale) {
  610. Environment *env = environment_owner.getornull(p_env);
  611. ERR_FAIL_COND(!env);
  612. }
  613. void RasterizerSceneGLES2::environment_set_adjustment(RID p_env, bool p_enable, float p_brightness, float p_contrast, float p_saturation, RID p_ramp) {
  614. Environment *env = environment_owner.getornull(p_env);
  615. ERR_FAIL_COND(!env);
  616. env->adjustments_enabled = p_enable;
  617. env->adjustments_brightness = p_brightness;
  618. env->adjustments_contrast = p_contrast;
  619. env->adjustments_saturation = p_saturation;
  620. env->color_correction = p_ramp;
  621. }
  622. void RasterizerSceneGLES2::environment_set_fog(RID p_env, bool p_enable, const Color &p_color, const Color &p_sun_color, float p_sun_amount) {
  623. Environment *env = environment_owner.getornull(p_env);
  624. ERR_FAIL_COND(!env);
  625. env->fog_enabled = p_enable;
  626. env->fog_color = p_color;
  627. env->fog_sun_color = p_sun_color;
  628. env->fog_sun_amount = p_sun_amount;
  629. }
  630. void RasterizerSceneGLES2::environment_set_fog_depth(RID p_env, bool p_enable, float p_depth_begin, float p_depth_end, float p_depth_curve, bool p_transmit, float p_transmit_curve) {
  631. Environment *env = environment_owner.getornull(p_env);
  632. ERR_FAIL_COND(!env);
  633. env->fog_depth_enabled = p_enable;
  634. env->fog_depth_begin = p_depth_begin;
  635. env->fog_depth_end = p_depth_end;
  636. env->fog_depth_curve = p_depth_curve;
  637. env->fog_transmit_enabled = p_transmit;
  638. env->fog_transmit_curve = p_transmit_curve;
  639. }
  640. void RasterizerSceneGLES2::environment_set_fog_height(RID p_env, bool p_enable, float p_min_height, float p_max_height, float p_height_curve) {
  641. Environment *env = environment_owner.getornull(p_env);
  642. ERR_FAIL_COND(!env);
  643. env->fog_height_enabled = p_enable;
  644. env->fog_height_min = p_min_height;
  645. env->fog_height_max = p_max_height;
  646. env->fog_height_curve = p_height_curve;
  647. }
  648. bool RasterizerSceneGLES2::is_environment(RID p_env) {
  649. return environment_owner.owns(p_env);
  650. }
  651. RS::EnvironmentBG RasterizerSceneGLES2::environment_get_background(RID p_env) {
  652. const Environment *env = environment_owner.getornull(p_env);
  653. ERR_FAIL_COND_V(!env, RS::ENV_BG_MAX);
  654. return env->bg_mode;
  655. }
  656. int RasterizerSceneGLES2::environment_get_canvas_max_layer(RID p_env) {
  657. const Environment *env = environment_owner.getornull(p_env);
  658. ERR_FAIL_COND_V(!env, -1);
  659. return env->canvas_max_layer;
  660. }
  661. RID RasterizerSceneGLES2::light_instance_create(RID p_light) {
  662. LightInstance *light_instance = memnew(LightInstance);
  663. light_instance->last_scene_pass = 0;
  664. light_instance->light = p_light;
  665. light_instance->light_ptr = storage->light_owner.getornull(p_light);
  666. light_instance->light_index = 0xFFFF;
  667. if (!light_instance->light_ptr) {
  668. memdelete(light_instance);
  669. ERR_FAIL_V_MSG(RID(), "Condition ' !light_instance->light_ptr ' is true.");
  670. }
  671. light_instance->self = light_instance_owner.make_rid(light_instance);
  672. return light_instance->self;
  673. }
  674. void RasterizerSceneGLES2::light_instance_set_transform(RID p_light_instance, const Transform &p_transform) {
  675. LightInstance *light_instance = light_instance_owner.getornull(p_light_instance);
  676. ERR_FAIL_COND(!light_instance);
  677. light_instance->transform = p_transform;
  678. }
  679. void RasterizerSceneGLES2::light_instance_set_shadow_transform(RID p_light_instance, const CameraMatrix &p_projection, const Transform &p_transform, float p_far, float p_split, int p_pass, float p_bias_scale) {
  680. LightInstance *light_instance = light_instance_owner.getornull(p_light_instance);
  681. ERR_FAIL_COND(!light_instance);
  682. if (light_instance->light_ptr->type != RS::LIGHT_DIRECTIONAL) {
  683. p_pass = 0;
  684. }
  685. ERR_FAIL_INDEX(p_pass, 4);
  686. light_instance->shadow_transform[p_pass].camera = p_projection;
  687. light_instance->shadow_transform[p_pass].transform = p_transform;
  688. light_instance->shadow_transform[p_pass].farplane = p_far;
  689. light_instance->shadow_transform[p_pass].split = p_split;
  690. light_instance->shadow_transform[p_pass].bias_scale = p_bias_scale;
  691. }
  692. void RasterizerSceneGLES2::light_instance_mark_visible(RID p_light_instance) {
  693. LightInstance *light_instance = light_instance_owner.getornull(p_light_instance);
  694. ERR_FAIL_COND(!light_instance);
  695. light_instance->last_scene_pass = scene_pass;
  696. }
  697. //////////////////////
  698. RID RasterizerSceneGLES2::gi_probe_instance_create() {
  699. return RID();
  700. }
  701. void RasterizerSceneGLES2::gi_probe_instance_set_light_data(RID p_probe, RID p_base, RID p_data) {
  702. }
  703. void RasterizerSceneGLES2::gi_probe_instance_set_transform_to_data(RID p_probe, const Transform &p_xform) {
  704. }
  705. void RasterizerSceneGLES2::gi_probe_instance_set_bounds(RID p_probe, const Vector3 &p_bounds) {
  706. }
  707. ////////////////////////////
  708. ////////////////////////////
  709. ////////////////////////////
  710. void RasterizerSceneGLES2::_add_geometry(RasterizerStorageGLES2::Geometry *p_geometry, InstanceBase *p_instance, RasterizerStorageGLES2::GeometryOwner *p_owner, int p_material, bool p_depth_pass, bool p_shadow_pass) {
  711. RasterizerStorageGLES2::Material *material = NULL;
  712. RID material_src;
  713. if (p_instance->material_override.is_valid()) {
  714. material_src = p_instance->material_override;
  715. } else if (p_material >= 0) {
  716. material_src = p_instance->materials[p_material];
  717. } else {
  718. material_src = p_geometry->material;
  719. }
  720. if (material_src.is_valid()) {
  721. material = storage->material_owner.getornull(material_src);
  722. if (!material->shader || !material->shader->valid) {
  723. material = NULL;
  724. }
  725. }
  726. if (!material) {
  727. material = storage->material_owner.getornull(default_material);
  728. }
  729. ERR_FAIL_COND(!material);
  730. _add_geometry_with_material(p_geometry, p_instance, p_owner, material, p_depth_pass, p_shadow_pass);
  731. while (material->next_pass.is_valid()) {
  732. material = storage->material_owner.getornull(material->next_pass);
  733. if (!material || !material->shader || !material->shader->valid) {
  734. break;
  735. }
  736. _add_geometry_with_material(p_geometry, p_instance, p_owner, material, p_depth_pass, p_shadow_pass);
  737. }
  738. }
  739. void RasterizerSceneGLES2::_add_geometry_with_material(RasterizerStorageGLES2::Geometry *p_geometry, InstanceBase *p_instance, RasterizerStorageGLES2::GeometryOwner *p_owner, RasterizerStorageGLES2::Material *p_material, bool p_depth_pass, bool p_shadow_pass) {
  740. bool has_base_alpha = (p_material->shader->spatial.uses_alpha && !p_material->shader->spatial.uses_alpha_scissor) || p_material->shader->spatial.uses_screen_texture || p_material->shader->spatial.uses_depth_texture;
  741. bool has_blend_alpha = p_material->shader->spatial.blend_mode != RasterizerStorageGLES2::Shader::Spatial::BLEND_MODE_MIX;
  742. bool has_alpha = has_base_alpha || has_blend_alpha;
  743. bool mirror = p_instance->mirror;
  744. if (p_material->shader->spatial.cull_mode == RasterizerStorageGLES2::Shader::Spatial::CULL_MODE_DISABLED) {
  745. mirror = false;
  746. } else if (p_material->shader->spatial.cull_mode == RasterizerStorageGLES2::Shader::Spatial::CULL_MODE_FRONT) {
  747. mirror = !mirror;
  748. }
  749. //if (p_material->shader->spatial.uses_sss) {
  750. // state.used_sss = true;
  751. //}
  752. if (p_material->shader->spatial.uses_screen_texture) {
  753. state.used_screen_texture = true;
  754. }
  755. if (p_depth_pass) {
  756. if (has_blend_alpha || p_material->shader->spatial.uses_depth_texture || (has_base_alpha && p_material->shader->spatial.depth_draw_mode != RasterizerStorageGLES2::Shader::Spatial::DEPTH_DRAW_ALPHA_PREPASS))
  757. return; //bye
  758. if (!p_material->shader->spatial.uses_alpha_scissor && !p_material->shader->spatial.writes_modelview_or_projection && !p_material->shader->spatial.uses_vertex && !p_material->shader->spatial.uses_discard && p_material->shader->spatial.depth_draw_mode != RasterizerStorageGLES2::Shader::Spatial::DEPTH_DRAW_ALPHA_PREPASS) {
  759. //shader does not use discard and does not write a vertex position, use generic material
  760. if (p_instance->cast_shadows == RS::SHADOW_CASTING_SETTING_DOUBLE_SIDED) {
  761. p_material = storage->material_owner.getornull(!p_shadow_pass && p_material->shader->spatial.uses_world_coordinates ? default_worldcoord_material_twosided : default_material_twosided);
  762. mirror = false;
  763. } else {
  764. p_material = storage->material_owner.getornull(!p_shadow_pass && p_material->shader->spatial.uses_world_coordinates ? default_worldcoord_material : default_material);
  765. }
  766. }
  767. has_alpha = false;
  768. }
  769. RenderList::Element *e = (has_alpha || p_material->shader->spatial.no_depth_test) ? render_list.add_alpha_element() : render_list.add_element();
  770. if (!e) {
  771. return;
  772. }
  773. e->geometry = p_geometry;
  774. e->material = p_material;
  775. e->instance = p_instance;
  776. e->owner = p_owner;
  777. e->sort_key = 0;
  778. e->depth_key = 0;
  779. e->use_accum = false;
  780. e->light_index = RenderList::MAX_LIGHTS;
  781. e->use_accum_ptr = &e->use_accum;
  782. e->instancing = (e->instance->base_type == RS::INSTANCE_MULTIMESH) ? 1 : 0;
  783. e->front_facing = false;
  784. if (e->geometry->last_pass != render_pass) {
  785. e->geometry->last_pass = render_pass;
  786. e->geometry->index = current_geometry_index++;
  787. }
  788. e->geometry_index = e->geometry->index;
  789. if (e->material->last_pass != render_pass) {
  790. e->material->last_pass = render_pass;
  791. e->material->index = current_material_index++;
  792. if (e->material->shader->last_pass != render_pass) {
  793. e->material->shader->index = current_shader_index++;
  794. }
  795. }
  796. e->material_index = e->material->index;
  797. if (mirror) {
  798. e->front_facing = true;
  799. }
  800. e->refprobe_0_index = RenderList::MAX_REFLECTION_PROBES; //refprobe disabled by default
  801. e->refprobe_1_index = RenderList::MAX_REFLECTION_PROBES; //refprobe disabled by default
  802. if (!p_depth_pass) {
  803. e->depth_layer = e->instance->depth_layer;
  804. e->priority = p_material->render_priority;
  805. if (has_alpha && p_material->shader->spatial.depth_draw_mode == RasterizerStorageGLES2::Shader::Spatial::DEPTH_DRAW_ALPHA_PREPASS) {
  806. //add element to opaque
  807. RenderList::Element *eo = render_list.add_element();
  808. *eo = *e;
  809. eo->use_accum_ptr = &eo->use_accum;
  810. }
  811. int rpsize = e->instance->reflection_probe_instances.size();
  812. if (rpsize > 0) {
  813. bool first = true;
  814. rpsize = MIN(rpsize, 2); //more than 2 per object are not supported, this keeps it stable
  815. for (int i = 0; i < rpsize; i++) {
  816. ReflectionProbeInstance *rpi = reflection_probe_instance_owner.getornull(e->instance->reflection_probe_instances[i]);
  817. if (rpi->last_pass != render_pass) {
  818. continue;
  819. }
  820. if (first) {
  821. e->refprobe_0_index = rpi->index;
  822. first = false;
  823. } else {
  824. e->refprobe_1_index = rpi->index;
  825. break;
  826. }
  827. }
  828. /* if (e->refprobe_0_index > e->refprobe_1_index) { //if both are valid, swap them to keep order as best as possible
  829. uint64_t tmp = e->refprobe_0_index;
  830. e->refprobe_0_index = e->refprobe_1_index;
  831. e->refprobe_1_index = tmp;
  832. }*/
  833. }
  834. //add directional lights
  835. if (p_material->shader->spatial.unshaded) {
  836. e->light_mode = LIGHTMODE_UNSHADED;
  837. } else {
  838. bool copy = false;
  839. for (int i = 0; i < render_directional_lights; i++) {
  840. if (copy) {
  841. RenderList::Element *e2 = has_alpha ? render_list.add_alpha_element() : render_list.add_element();
  842. if (!e2) {
  843. break;
  844. }
  845. *e2 = *e; //this includes accum ptr :)
  846. e = e2;
  847. }
  848. //directional sort key
  849. e->light_type1 = 0;
  850. e->light_type2 = 1;
  851. e->light_index = i;
  852. copy = true;
  853. }
  854. //add omni / spots
  855. for (int i = 0; i < e->instance->light_instances.size(); i++) {
  856. LightInstance *li = light_instance_owner.getornull(e->instance->light_instances[i]);
  857. if (!li || li->light_index >= render_light_instance_count || render_light_instances[li->light_index] != li) {
  858. continue; // too many or light_index did not correspond to the light instances to be rendered
  859. }
  860. if (copy) {
  861. RenderList::Element *e2 = has_alpha ? render_list.add_alpha_element() : render_list.add_element();
  862. if (!e2) {
  863. break;
  864. }
  865. *e2 = *e; //this includes accum ptr :)
  866. e = e2;
  867. }
  868. //directional sort key
  869. e->light_type1 = 1;
  870. e->light_type2 = li->light_ptr->type == RenderingServer::LIGHT_OMNI ? 0 : 1;
  871. e->light_index = li->light_index;
  872. copy = true;
  873. }
  874. if (e->instance->lightmap.is_valid()) {
  875. e->light_mode = LIGHTMODE_LIGHTMAP;
  876. } else if (!e->instance->lightmap_capture_data.empty()) {
  877. e->light_mode = LIGHTMODE_LIGHTMAP_CAPTURE;
  878. } else {
  879. e->light_mode = LIGHTMODE_NORMAL;
  880. }
  881. }
  882. }
  883. // do not add anything here, as lights are duplicated elements..
  884. if (p_material->shader->spatial.uses_time) {
  885. RenderingServerRaster::redraw_request();
  886. }
  887. }
  888. void RasterizerSceneGLES2::_copy_texture_to_buffer(GLuint p_texture, GLuint p_buffer) {
  889. //copy to front buffer
  890. glBindFramebuffer(GL_FRAMEBUFFER, p_buffer);
  891. glDepthMask(GL_FALSE);
  892. glDisable(GL_DEPTH_TEST);
  893. glDisable(GL_CULL_FACE);
  894. glDisable(GL_BLEND);
  895. glDepthFunc(GL_LEQUAL);
  896. glColorMask(1, 1, 1, 1);
  897. glActiveTexture(GL_TEXTURE0);
  898. glBindTexture(GL_TEXTURE_2D, p_texture);
  899. glViewport(0, 0, storage->frame.current_rt->width, storage->frame.current_rt->height);
  900. storage->shaders.copy.bind();
  901. storage->bind_quad_array();
  902. glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
  903. glBindBuffer(GL_ARRAY_BUFFER, 0);
  904. }
  905. void RasterizerSceneGLES2::_fill_render_list(InstanceBase **p_cull_result, int p_cull_count, bool p_depth_pass, bool p_shadow_pass) {
  906. render_pass++;
  907. current_material_index = 0;
  908. current_geometry_index = 0;
  909. current_light_index = 0;
  910. current_refprobe_index = 0;
  911. current_shader_index = 0;
  912. for (int i = 0; i < p_cull_count; i++) {
  913. InstanceBase *instance = p_cull_result[i];
  914. switch (instance->base_type) {
  915. case RS::INSTANCE_MESH: {
  916. RasterizerStorageGLES2::Mesh *mesh = storage->mesh_owner.getornull(instance->base);
  917. ERR_CONTINUE(!mesh);
  918. int num_surfaces = mesh->surfaces.size();
  919. for (int j = 0; j < num_surfaces; j++) {
  920. int material_index = instance->materials[j].is_valid() ? j : -1;
  921. RasterizerStorageGLES2::Surface *surface = mesh->surfaces[j];
  922. _add_geometry(surface, instance, NULL, material_index, p_depth_pass, p_shadow_pass);
  923. }
  924. } break;
  925. case RS::INSTANCE_MULTIMESH: {
  926. RasterizerStorageGLES2::MultiMesh *multi_mesh = storage->multimesh_owner.getornull(instance->base);
  927. ERR_CONTINUE(!multi_mesh);
  928. if (multi_mesh->size == 0 || multi_mesh->visible_instances == 0)
  929. continue;
  930. RasterizerStorageGLES2::Mesh *mesh = storage->mesh_owner.getornull(multi_mesh->mesh);
  931. if (!mesh)
  932. continue;
  933. int ssize = mesh->surfaces.size();
  934. for (int j = 0; j < ssize; j++) {
  935. RasterizerStorageGLES2::Surface *s = mesh->surfaces[j];
  936. _add_geometry(s, instance, multi_mesh, -1, p_depth_pass, p_shadow_pass);
  937. }
  938. } break;
  939. case RS::INSTANCE_IMMEDIATE: {
  940. RasterizerStorageGLES2::Immediate *im = storage->immediate_owner.getornull(instance->base);
  941. ERR_CONTINUE(!im);
  942. _add_geometry(im, instance, NULL, -1, p_depth_pass, p_shadow_pass);
  943. } break;
  944. default: {
  945. }
  946. }
  947. }
  948. }
  949. static const GLenum gl_primitive[] = {
  950. GL_POINTS,
  951. GL_LINES,
  952. GL_LINE_STRIP,
  953. GL_LINE_LOOP,
  954. GL_TRIANGLES,
  955. GL_TRIANGLE_STRIP,
  956. GL_TRIANGLE_FAN
  957. };
  958. void RasterizerSceneGLES2::_set_cull(bool p_front, bool p_disabled, bool p_reverse_cull) {
  959. bool front = p_front;
  960. if (p_reverse_cull)
  961. front = !front;
  962. if (p_disabled != state.cull_disabled) {
  963. if (p_disabled)
  964. glDisable(GL_CULL_FACE);
  965. else
  966. glEnable(GL_CULL_FACE);
  967. state.cull_disabled = p_disabled;
  968. }
  969. if (front != state.cull_front) {
  970. glCullFace(front ? GL_FRONT : GL_BACK);
  971. state.cull_front = front;
  972. }
  973. }
  974. bool RasterizerSceneGLES2::_setup_material(RasterizerStorageGLES2::Material *p_material, bool p_alpha_pass, Size2i p_skeleton_tex_size) {
  975. // material parameters
  976. state.scene_shader.set_custom_shader(p_material->shader->custom_code_id);
  977. if (p_material->shader->spatial.uses_screen_texture && storage->frame.current_rt) {
  978. glActiveTexture(GL_TEXTURE0 + storage->config.max_texture_image_units - 4);
  979. glBindTexture(GL_TEXTURE_2D, storage->frame.current_rt->copy_screen_effect.color);
  980. }
  981. if (p_material->shader->spatial.uses_depth_texture && storage->frame.current_rt) {
  982. glActiveTexture(GL_TEXTURE0 + storage->config.max_texture_image_units - 4);
  983. glBindTexture(GL_TEXTURE_2D, storage->frame.current_rt->depth);
  984. }
  985. bool shader_rebind = state.scene_shader.bind();
  986. if (p_material->shader->spatial.no_depth_test || p_material->shader->spatial.uses_depth_texture) {
  987. glDisable(GL_DEPTH_TEST);
  988. } else {
  989. glEnable(GL_DEPTH_TEST);
  990. }
  991. switch (p_material->shader->spatial.depth_draw_mode) {
  992. case RasterizerStorageGLES2::Shader::Spatial::DEPTH_DRAW_ALPHA_PREPASS:
  993. case RasterizerStorageGLES2::Shader::Spatial::DEPTH_DRAW_OPAQUE: {
  994. glDepthMask(!p_alpha_pass && !p_material->shader->spatial.uses_depth_texture);
  995. } break;
  996. case RasterizerStorageGLES2::Shader::Spatial::DEPTH_DRAW_ALWAYS: {
  997. glDepthMask(GL_TRUE && !p_material->shader->spatial.uses_depth_texture);
  998. } break;
  999. case RasterizerStorageGLES2::Shader::Spatial::DEPTH_DRAW_NEVER: {
  1000. glDepthMask(GL_FALSE);
  1001. } break;
  1002. }
  1003. int tc = p_material->textures.size();
  1004. const Pair<StringName, RID> *textures = p_material->textures.ptr();
  1005. const ShaderLanguage::ShaderNode::Uniform::Hint *texture_hints = p_material->shader->texture_hints.ptr();
  1006. const ShaderLanguage::DataType *texture_types = p_material->shader->texture_types.ptr();
  1007. state.scene_shader.set_uniform(SceneShaderGLES2::SKELETON_TEXTURE_SIZE, p_skeleton_tex_size);
  1008. state.current_main_tex = 0;
  1009. for (int i = 0; i < tc; i++) {
  1010. glActiveTexture(GL_TEXTURE0 + i);
  1011. RasterizerStorageGLES2::Texture *t = storage->texture_owner.getornull(textures[i].second);
  1012. if (!t) {
  1013. GLenum target = GL_TEXTURE_2D;
  1014. GLuint tex = 0;
  1015. switch (texture_types[i]) {
  1016. case ShaderLanguage::TYPE_ISAMPLER2D:
  1017. case ShaderLanguage::TYPE_USAMPLER2D:
  1018. case ShaderLanguage::TYPE_SAMPLER2D: {
  1019. switch (texture_hints[i]) {
  1020. case ShaderLanguage::ShaderNode::Uniform::HINT_BLACK_ALBEDO:
  1021. case ShaderLanguage::ShaderNode::Uniform::HINT_BLACK: {
  1022. tex = storage->resources.black_tex;
  1023. } break;
  1024. case ShaderLanguage::ShaderNode::Uniform::HINT_ANISO: {
  1025. tex = storage->resources.aniso_tex;
  1026. } break;
  1027. case ShaderLanguage::ShaderNode::Uniform::HINT_NORMAL: {
  1028. tex = storage->resources.normal_tex;
  1029. } break;
  1030. default: {
  1031. tex = storage->resources.white_tex;
  1032. } break;
  1033. }
  1034. } break;
  1035. case ShaderLanguage::TYPE_SAMPLERCUBE: {
  1036. // TODO
  1037. } break;
  1038. case ShaderLanguage::TYPE_ISAMPLER3D:
  1039. case ShaderLanguage::TYPE_USAMPLER3D:
  1040. case ShaderLanguage::TYPE_SAMPLER3D: {
  1041. target = GL_TEXTURE_3D;
  1042. tex = storage->resources.white_tex_3d;
  1043. //switch (texture_hints[i]) {
  1044. // TODO
  1045. //}
  1046. } break;
  1047. case ShaderLanguage::TYPE_ISAMPLER2DARRAY:
  1048. case ShaderLanguage::TYPE_USAMPLER2DARRAY:
  1049. case ShaderLanguage::TYPE_SAMPLER2DARRAY: {
  1050. target = GL_TEXTURE_2D_ARRAY;
  1051. tex = storage->resources.white_tex_array;
  1052. //switch (texture_hints[i]) {
  1053. // TODO
  1054. //}
  1055. } break;
  1056. default: {
  1057. }
  1058. }
  1059. glBindTexture(target, tex);
  1060. continue;
  1061. }
  1062. if (t->redraw_if_visible) { //must check before proxy because this is often used with proxies
  1063. RenderingServerRaster::redraw_request();
  1064. }
  1065. t = t->get_ptr();
  1066. #ifdef TOOLS_ENABLED
  1067. if (t->detect_3d) {
  1068. t->detect_3d(t->detect_3d_ud);
  1069. }
  1070. #endif
  1071. #ifdef TOOLS_ENABLED
  1072. if (t->detect_normal && texture_hints[i] == ShaderLanguage::ShaderNode::Uniform::HINT_NORMAL) {
  1073. t->detect_normal(t->detect_normal_ud);
  1074. }
  1075. #endif
  1076. if (t->render_target)
  1077. t->render_target->used_in_frame = true;
  1078. glBindTexture(t->target, t->tex_id);
  1079. if (i == 0) {
  1080. state.current_main_tex = t->tex_id;
  1081. }
  1082. }
  1083. state.scene_shader.use_material((void *)p_material);
  1084. return shader_rebind;
  1085. }
  1086. void RasterizerSceneGLES2::_setup_geometry(RenderList::Element *p_element, RasterizerStorageGLES2::Skeleton *p_skeleton) {
  1087. switch (p_element->instance->base_type) {
  1088. case RS::INSTANCE_MESH: {
  1089. RasterizerStorageGLES2::Surface *s = static_cast<RasterizerStorageGLES2::Surface *>(p_element->geometry);
  1090. glBindBuffer(GL_ARRAY_BUFFER, s->vertex_id);
  1091. if (s->index_array_len > 0) {
  1092. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, s->index_id);
  1093. }
  1094. for (int i = 0; i < RS::ARRAY_MAX - 1; i++) {
  1095. if (s->attribs[i].enabled) {
  1096. glEnableVertexAttribArray(i);
  1097. glVertexAttribPointer(s->attribs[i].index, s->attribs[i].size, s->attribs[i].type, s->attribs[i].normalized, s->attribs[i].stride, CAST_INT_TO_UCHAR_PTR(s->attribs[i].offset));
  1098. } else {
  1099. glDisableVertexAttribArray(i);
  1100. switch (i) {
  1101. case RS::ARRAY_NORMAL: {
  1102. glVertexAttrib4f(RS::ARRAY_NORMAL, 0.0, 0.0, 1, 1);
  1103. } break;
  1104. case RS::ARRAY_COLOR: {
  1105. glVertexAttrib4f(RS::ARRAY_COLOR, 1, 1, 1, 1);
  1106. } break;
  1107. default: {
  1108. }
  1109. }
  1110. }
  1111. }
  1112. bool clear_skeleton_buffer = storage->config.use_skeleton_software;
  1113. if (p_skeleton) {
  1114. if (!storage->config.use_skeleton_software) {
  1115. //use float texture workflow
  1116. glActiveTexture(GL_TEXTURE0 + storage->config.max_texture_image_units - 1);
  1117. glBindTexture(GL_TEXTURE_2D, p_skeleton->tex_id);
  1118. } else {
  1119. //use transform buffer workflow
  1120. ERR_FAIL_COND(p_skeleton->use_2d);
  1121. Vector<float> &transform_buffer = storage->resources.skeleton_transform_cpu_buffer;
  1122. if (!s->attribs[RS::ARRAY_BONES].enabled || !s->attribs[RS::ARRAY_WEIGHTS].enabled) {
  1123. break; // the whole instance has a skeleton, but this surface is not affected by it.
  1124. }
  1125. // 3 * vec4 per vertex
  1126. if (transform_buffer.size() < s->array_len * 12) {
  1127. transform_buffer.resize(s->array_len * 12);
  1128. }
  1129. const size_t bones_offset = s->attribs[RS::ARRAY_BONES].offset;
  1130. const size_t bones_stride = s->attribs[RS::ARRAY_BONES].stride;
  1131. const size_t bone_weight_offset = s->attribs[RS::ARRAY_WEIGHTS].offset;
  1132. const size_t bone_weight_stride = s->attribs[RS::ARRAY_WEIGHTS].stride;
  1133. {
  1134. float *write = transform_buffer.ptrw();
  1135. float *buffer = write.ptr();
  1136. const uint8_t *vertex_array_read = s->data.ptr();
  1137. const uint8_t *vertex_data = vertex_array_read.ptr();
  1138. for (int i = 0; i < s->array_len; i++) {
  1139. // do magic
  1140. size_t bones[4];
  1141. float bone_weight[4];
  1142. if (s->attribs[RS::ARRAY_BONES].type == GL_UNSIGNED_BYTE) {
  1143. // read as byte
  1144. const uint8_t *bones_ptr = vertex_data + bones_offset + (i * bones_stride);
  1145. bones[0] = bones_ptr[0];
  1146. bones[1] = bones_ptr[1];
  1147. bones[2] = bones_ptr[2];
  1148. bones[3] = bones_ptr[3];
  1149. } else {
  1150. // read as short
  1151. const uint16_t *bones_ptr = (const uint16_t *)(vertex_data + bones_offset + (i * bones_stride));
  1152. bones[0] = bones_ptr[0];
  1153. bones[1] = bones_ptr[1];
  1154. bones[2] = bones_ptr[2];
  1155. bones[3] = bones_ptr[3];
  1156. }
  1157. if (s->attribs[RS::ARRAY_WEIGHTS].type == GL_FLOAT) {
  1158. // read as float
  1159. const float *weight_ptr = (const float *)(vertex_data + bone_weight_offset + (i * bone_weight_stride));
  1160. bone_weight[0] = weight_ptr[0];
  1161. bone_weight[1] = weight_ptr[1];
  1162. bone_weight[2] = weight_ptr[2];
  1163. bone_weight[3] = weight_ptr[3];
  1164. } else {
  1165. // read as half
  1166. const uint16_t *weight_ptr = (const uint16_t *)(vertex_data + bone_weight_offset + (i * bone_weight_stride));
  1167. bone_weight[0] = (weight_ptr[0] / (float)0xFFFF);
  1168. bone_weight[1] = (weight_ptr[1] / (float)0xFFFF);
  1169. bone_weight[2] = (weight_ptr[2] / (float)0xFFFF);
  1170. bone_weight[3] = (weight_ptr[3] / (float)0xFFFF);
  1171. }
  1172. Transform transform;
  1173. Transform bone_transforms[4] = {
  1174. storage->skeleton_bone_get_transform(p_element->instance->skeleton, bones[0]),
  1175. storage->skeleton_bone_get_transform(p_element->instance->skeleton, bones[1]),
  1176. storage->skeleton_bone_get_transform(p_element->instance->skeleton, bones[2]),
  1177. storage->skeleton_bone_get_transform(p_element->instance->skeleton, bones[3]),
  1178. };
  1179. transform.origin =
  1180. bone_weight[0] * bone_transforms[0].origin +
  1181. bone_weight[1] * bone_transforms[1].origin +
  1182. bone_weight[2] * bone_transforms[2].origin +
  1183. bone_weight[3] * bone_transforms[3].origin;
  1184. transform.basis =
  1185. bone_transforms[0].basis * bone_weight[0] +
  1186. bone_transforms[1].basis * bone_weight[1] +
  1187. bone_transforms[2].basis * bone_weight[2] +
  1188. bone_transforms[3].basis * bone_weight[3];
  1189. float row[3][4] = {
  1190. { transform.basis[0][0], transform.basis[0][1], transform.basis[0][2], transform.origin[0] },
  1191. { transform.basis[1][0], transform.basis[1][1], transform.basis[1][2], transform.origin[1] },
  1192. { transform.basis[2][0], transform.basis[2][1], transform.basis[2][2], transform.origin[2] },
  1193. };
  1194. size_t transform_buffer_offset = i * 12;
  1195. copymem(&buffer[transform_buffer_offset], row, sizeof(row));
  1196. }
  1197. }
  1198. storage->_update_skeleton_transform_buffer(transform_buffer, s->array_len * 12);
  1199. //enable transform buffer and bind it
  1200. glBindBuffer(GL_ARRAY_BUFFER, storage->resources.skeleton_transform_buffer);
  1201. glEnableVertexAttribArray(INSTANCE_BONE_BASE + 0);
  1202. glEnableVertexAttribArray(INSTANCE_BONE_BASE + 1);
  1203. glEnableVertexAttribArray(INSTANCE_BONE_BASE + 2);
  1204. glVertexAttribPointer(INSTANCE_BONE_BASE + 0, 4, GL_FLOAT, GL_FALSE, sizeof(float) * 12, (const void *)(sizeof(float) * 4 * 0));
  1205. glVertexAttribPointer(INSTANCE_BONE_BASE + 1, 4, GL_FLOAT, GL_FALSE, sizeof(float) * 12, (const void *)(sizeof(float) * 4 * 1));
  1206. glVertexAttribPointer(INSTANCE_BONE_BASE + 2, 4, GL_FLOAT, GL_FALSE, sizeof(float) * 12, (const void *)(sizeof(float) * 4 * 2));
  1207. clear_skeleton_buffer = false;
  1208. }
  1209. }
  1210. if (clear_skeleton_buffer) {
  1211. glDisableVertexAttribArray(INSTANCE_BONE_BASE + 0);
  1212. glDisableVertexAttribArray(INSTANCE_BONE_BASE + 1);
  1213. glDisableVertexAttribArray(INSTANCE_BONE_BASE + 2);
  1214. }
  1215. } break;
  1216. case RS::INSTANCE_MULTIMESH: {
  1217. RasterizerStorageGLES2::Surface *s = static_cast<RasterizerStorageGLES2::Surface *>(p_element->geometry);
  1218. glBindBuffer(GL_ARRAY_BUFFER, s->vertex_id);
  1219. if (s->index_array_len > 0) {
  1220. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, s->index_id);
  1221. }
  1222. for (int i = 0; i < RS::ARRAY_MAX - 1; i++) {
  1223. if (s->attribs[i].enabled) {
  1224. glEnableVertexAttribArray(i);
  1225. glVertexAttribPointer(s->attribs[i].index, s->attribs[i].size, s->attribs[i].type, s->attribs[i].normalized, s->attribs[i].stride, CAST_INT_TO_UCHAR_PTR(s->attribs[i].offset));
  1226. } else {
  1227. glDisableVertexAttribArray(i);
  1228. switch (i) {
  1229. case RS::ARRAY_NORMAL: {
  1230. glVertexAttrib4f(RS::ARRAY_NORMAL, 0.0, 0.0, 1, 1);
  1231. } break;
  1232. case RS::ARRAY_COLOR: {
  1233. glVertexAttrib4f(RS::ARRAY_COLOR, 1, 1, 1, 1);
  1234. } break;
  1235. default: {
  1236. }
  1237. }
  1238. }
  1239. }
  1240. // prepare multimesh (disable)
  1241. glDisableVertexAttribArray(INSTANCE_ATTRIB_BASE + 0);
  1242. glDisableVertexAttribArray(INSTANCE_ATTRIB_BASE + 1);
  1243. glDisableVertexAttribArray(INSTANCE_ATTRIB_BASE + 2);
  1244. glDisableVertexAttribArray(INSTANCE_ATTRIB_BASE + 3);
  1245. glDisableVertexAttribArray(INSTANCE_ATTRIB_BASE + 4);
  1246. glDisableVertexAttribArray(INSTANCE_BONE_BASE + 0);
  1247. glDisableVertexAttribArray(INSTANCE_BONE_BASE + 1);
  1248. glDisableVertexAttribArray(INSTANCE_BONE_BASE + 2);
  1249. } break;
  1250. case RS::INSTANCE_IMMEDIATE: {
  1251. } break;
  1252. default: {
  1253. }
  1254. }
  1255. }
  1256. void RasterizerSceneGLES2::_render_geometry(RenderList::Element *p_element) {
  1257. switch (p_element->instance->base_type) {
  1258. case RS::INSTANCE_MESH: {
  1259. RasterizerStorageGLES2::Surface *s = static_cast<RasterizerStorageGLES2::Surface *>(p_element->geometry);
  1260. // drawing
  1261. if (s->index_array_len > 0) {
  1262. glDrawElements(gl_primitive[s->primitive], s->index_array_len, (s->array_len >= (1 << 16)) ? GL_UNSIGNED_INT : GL_UNSIGNED_SHORT, 0);
  1263. storage->info.render.vertices_count += s->index_array_len;
  1264. } else {
  1265. glDrawArrays(gl_primitive[s->primitive], 0, s->array_len);
  1266. storage->info.render.vertices_count += s->array_len;
  1267. }
  1268. /*
  1269. if (p_element->instance->skeleton.is_valid() && s->attribs[RS::ARRAY_BONES].enabled && s->attribs[RS::ARRAY_WEIGHTS].enabled) {
  1270. //clean up after skeleton
  1271. glBindBuffer(GL_ARRAY_BUFFER, storage->resources.skeleton_transform_buffer);
  1272. glDisableVertexAttribArray(RS::ARRAY_MAX + 0);
  1273. glDisableVertexAttribArray(RS::ARRAY_MAX + 1);
  1274. glDisableVertexAttribArray(RS::ARRAY_MAX + 2);
  1275. glVertexAttrib4f(RS::ARRAY_MAX + 0, 1, 0, 0, 0);
  1276. glVertexAttrib4f(RS::ARRAY_MAX + 1, 0, 1, 0, 0);
  1277. glVertexAttrib4f(RS::ARRAY_MAX + 2, 0, 0, 1, 0);
  1278. }
  1279. */
  1280. } break;
  1281. case RS::INSTANCE_MULTIMESH: {
  1282. RasterizerStorageGLES2::MultiMesh *multi_mesh = static_cast<RasterizerStorageGLES2::MultiMesh *>(p_element->owner);
  1283. RasterizerStorageGLES2::Surface *s = static_cast<RasterizerStorageGLES2::Surface *>(p_element->geometry);
  1284. int amount = MIN(multi_mesh->size, multi_mesh->visible_instances);
  1285. if (amount == -1) {
  1286. amount = multi_mesh->size;
  1287. }
  1288. int stride = multi_mesh->color_floats + multi_mesh->custom_data_floats + multi_mesh->xform_floats;
  1289. int color_ofs = multi_mesh->xform_floats;
  1290. int custom_data_ofs = color_ofs + multi_mesh->color_floats;
  1291. // drawing
  1292. const float *base_buffer = multi_mesh->data.ptr();
  1293. for (int i = 0; i < amount; i++) {
  1294. const float *buffer = base_buffer + i * stride;
  1295. {
  1296. glVertexAttrib4fv(INSTANCE_ATTRIB_BASE + 0, &buffer[0]);
  1297. glVertexAttrib4fv(INSTANCE_ATTRIB_BASE + 1, &buffer[4]);
  1298. glVertexAttrib4fv(INSTANCE_ATTRIB_BASE + 2, &buffer[8]);
  1299. }
  1300. if (multi_mesh->color_floats) {
  1301. if (multi_mesh->color_format == RS::MULTIMESH_COLOR_8BIT) {
  1302. uint8_t *color_data = (uint8_t *)(buffer + color_ofs);
  1303. glVertexAttrib4f(INSTANCE_ATTRIB_BASE + 3, color_data[0] / 255.0, color_data[1] / 255.0, color_data[2] / 255.0, color_data[3] / 255.0);
  1304. } else {
  1305. glVertexAttrib4fv(INSTANCE_ATTRIB_BASE + 3, buffer + color_ofs);
  1306. }
  1307. } else {
  1308. glVertexAttrib4f(INSTANCE_ATTRIB_BASE + 3, 1.0, 1.0, 1.0, 1.0);
  1309. }
  1310. if (multi_mesh->custom_data_floats) {
  1311. if (multi_mesh->custom_data_format == RS::MULTIMESH_CUSTOM_DATA_8BIT) {
  1312. uint8_t *custom_data = (uint8_t *)(buffer + custom_data_ofs);
  1313. glVertexAttrib4f(INSTANCE_ATTRIB_BASE + 4, custom_data[0] / 255.0, custom_data[1] / 255.0, custom_data[2] / 255.0, custom_data[3] / 255.0);
  1314. } else {
  1315. glVertexAttrib4fv(INSTANCE_ATTRIB_BASE + 4, buffer + custom_data_ofs);
  1316. }
  1317. }
  1318. if (s->index_array_len > 0) {
  1319. glDrawElements(gl_primitive[s->primitive], s->index_array_len, (s->array_len >= (1 << 16)) ? GL_UNSIGNED_INT : GL_UNSIGNED_SHORT, 0);
  1320. storage->info.render.vertices_count += s->index_array_len;
  1321. } else {
  1322. glDrawArrays(gl_primitive[s->primitive], 0, s->array_len);
  1323. storage->info.render.vertices_count += s->array_len;
  1324. }
  1325. }
  1326. } break;
  1327. case RS::INSTANCE_IMMEDIATE: {
  1328. const RasterizerStorageGLES2::Immediate *im = static_cast<const RasterizerStorageGLES2::Immediate *>(p_element->geometry);
  1329. if (im->building) {
  1330. return;
  1331. }
  1332. bool restore_tex = false;
  1333. glBindBuffer(GL_ARRAY_BUFFER, state.immediate_buffer);
  1334. for (const List<RasterizerStorageGLES2::Immediate::Chunk>::Element *E = im->chunks.front(); E; E = E->next()) {
  1335. const RasterizerStorageGLES2::Immediate::Chunk &c = E->get();
  1336. if (c.vertices.empty()) {
  1337. continue;
  1338. }
  1339. int vertices = c.vertices.size();
  1340. uint32_t buf_ofs = 0;
  1341. storage->info.render.vertices_count += vertices;
  1342. if (c.texture.is_valid() && storage->texture_owner.owns(c.texture)) {
  1343. RasterizerStorageGLES2::Texture *t = storage->texture_owner.getornull(c.texture);
  1344. if (t->redraw_if_visible) {
  1345. RenderingServerRaster::redraw_request();
  1346. }
  1347. t = t->get_ptr();
  1348. #ifdef TOOLS_ENABLED
  1349. if (t->detect_3d) {
  1350. t->detect_3d(t->detect_3d_ud);
  1351. }
  1352. #endif
  1353. if (t->render_target) {
  1354. t->render_target->used_in_frame = true;
  1355. }
  1356. glActiveTexture(GL_TEXTURE0);
  1357. glBindTexture(t->target, t->tex_id);
  1358. restore_tex = true;
  1359. } else if (restore_tex) {
  1360. glActiveTexture(GL_TEXTURE0);
  1361. glBindTexture(GL_TEXTURE_2D, state.current_main_tex);
  1362. restore_tex = false;
  1363. }
  1364. if (!c.normals.empty()) {
  1365. glEnableVertexAttribArray(RS::ARRAY_NORMAL);
  1366. glBufferSubData(GL_ARRAY_BUFFER, buf_ofs, sizeof(Vector3) * vertices, c.normals.ptr());
  1367. glVertexAttribPointer(RS::ARRAY_NORMAL, 3, GL_FLOAT, GL_FALSE, sizeof(Vector3), CAST_INT_TO_UCHAR_PTR(buf_ofs));
  1368. buf_ofs += sizeof(Vector3) * vertices;
  1369. } else {
  1370. glDisableVertexAttribArray(RS::ARRAY_NORMAL);
  1371. }
  1372. if (!c.tangents.empty()) {
  1373. glEnableVertexAttribArray(RS::ARRAY_TANGENT);
  1374. glBufferSubData(GL_ARRAY_BUFFER, buf_ofs, sizeof(Plane) * vertices, c.tangents.ptr());
  1375. glVertexAttribPointer(RS::ARRAY_TANGENT, 4, GL_FLOAT, GL_FALSE, sizeof(Plane), CAST_INT_TO_UCHAR_PTR(buf_ofs));
  1376. buf_ofs += sizeof(Plane) * vertices;
  1377. } else {
  1378. glDisableVertexAttribArray(RS::ARRAY_TANGENT);
  1379. }
  1380. if (!c.colors.empty()) {
  1381. glEnableVertexAttribArray(RS::ARRAY_COLOR);
  1382. glBufferSubData(GL_ARRAY_BUFFER, buf_ofs, sizeof(Color) * vertices, c.colors.ptr());
  1383. glVertexAttribPointer(RS::ARRAY_COLOR, 4, GL_FLOAT, GL_FALSE, sizeof(Color), CAST_INT_TO_UCHAR_PTR(buf_ofs));
  1384. buf_ofs += sizeof(Color) * vertices;
  1385. } else {
  1386. glDisableVertexAttribArray(RS::ARRAY_COLOR);
  1387. }
  1388. if (!c.uvs.empty()) {
  1389. glEnableVertexAttribArray(RS::ARRAY_TEX_UV);
  1390. glBufferSubData(GL_ARRAY_BUFFER, buf_ofs, sizeof(Vector2) * vertices, c.uvs.ptr());
  1391. glVertexAttribPointer(RS::ARRAY_TEX_UV, 2, GL_FLOAT, GL_FALSE, sizeof(Vector2), CAST_INT_TO_UCHAR_PTR(buf_ofs));
  1392. buf_ofs += sizeof(Vector2) * vertices;
  1393. } else {
  1394. glDisableVertexAttribArray(RS::ARRAY_TEX_UV);
  1395. }
  1396. if (!c.uv2s.empty()) {
  1397. glEnableVertexAttribArray(RS::ARRAY_TEX_UV2);
  1398. glBufferSubData(GL_ARRAY_BUFFER, buf_ofs, sizeof(Vector2) * vertices, c.uv2s.ptr());
  1399. glVertexAttribPointer(RS::ARRAY_TEX_UV2, 2, GL_FLOAT, GL_FALSE, sizeof(Vector2), CAST_INT_TO_UCHAR_PTR(buf_ofs));
  1400. buf_ofs += sizeof(Vector2) * vertices;
  1401. } else {
  1402. glDisableVertexAttribArray(RS::ARRAY_TEX_UV2);
  1403. }
  1404. glEnableVertexAttribArray(RS::ARRAY_VERTEX);
  1405. glBufferSubData(GL_ARRAY_BUFFER, buf_ofs, sizeof(Vector3) * vertices, c.vertices.ptr());
  1406. glVertexAttribPointer(RS::ARRAY_VERTEX, 3, GL_FLOAT, GL_FALSE, sizeof(Vector3), CAST_INT_TO_UCHAR_PTR(buf_ofs));
  1407. glDrawArrays(gl_primitive[c.primitive], 0, c.vertices.size());
  1408. }
  1409. if (restore_tex) {
  1410. glActiveTexture(GL_TEXTURE0);
  1411. glBindTexture(GL_TEXTURE_2D, state.current_main_tex);
  1412. restore_tex = false;
  1413. }
  1414. } break;
  1415. default: {
  1416. }
  1417. }
  1418. }
  1419. void RasterizerSceneGLES2::_setup_light_type(LightInstance *p_light, ShadowAtlas *shadow_atlas) {
  1420. //turn off all by default
  1421. state.scene_shader.set_conditional(SceneShaderGLES2::USE_LIGHTING, false);
  1422. state.scene_shader.set_conditional(SceneShaderGLES2::USE_SHADOW, false);
  1423. state.scene_shader.set_conditional(SceneShaderGLES2::SHADOW_MODE_PCF_5, false);
  1424. state.scene_shader.set_conditional(SceneShaderGLES2::SHADOW_MODE_PCF_13, false);
  1425. state.scene_shader.set_conditional(SceneShaderGLES2::LIGHT_MODE_DIRECTIONAL, false);
  1426. state.scene_shader.set_conditional(SceneShaderGLES2::LIGHT_MODE_OMNI, false);
  1427. state.scene_shader.set_conditional(SceneShaderGLES2::LIGHT_MODE_SPOT, false);
  1428. state.scene_shader.set_conditional(SceneShaderGLES2::LIGHT_USE_PSSM2, false);
  1429. state.scene_shader.set_conditional(SceneShaderGLES2::LIGHT_USE_PSSM4, false);
  1430. state.scene_shader.set_conditional(SceneShaderGLES2::LIGHT_USE_PSSM_BLEND, false);
  1431. if (!p_light) { //no light, return off
  1432. return;
  1433. }
  1434. //turn on lighting
  1435. state.scene_shader.set_conditional(SceneShaderGLES2::USE_LIGHTING, true);
  1436. switch (p_light->light_ptr->type) {
  1437. case RS::LIGHT_DIRECTIONAL: {
  1438. state.scene_shader.set_conditional(SceneShaderGLES2::LIGHT_MODE_DIRECTIONAL, true);
  1439. switch (p_light->light_ptr->directional_shadow_mode) {
  1440. case RS::LIGHT_DIRECTIONAL_SHADOW_ORTHOGONAL: {
  1441. //no need
  1442. } break;
  1443. case RS::LIGHT_DIRECTIONAL_SHADOW_PARALLEL_2_SPLITS: {
  1444. state.scene_shader.set_conditional(SceneShaderGLES2::LIGHT_USE_PSSM2, true);
  1445. } break;
  1446. case RS::LIGHT_DIRECTIONAL_SHADOW_PARALLEL_4_SPLITS: {
  1447. state.scene_shader.set_conditional(SceneShaderGLES2::LIGHT_USE_PSSM4, true);
  1448. } break;
  1449. }
  1450. state.scene_shader.set_conditional(SceneShaderGLES2::LIGHT_USE_PSSM_BLEND, p_light->light_ptr->directional_blend_splits);
  1451. if (!state.render_no_shadows && p_light->light_ptr->shadow) {
  1452. state.scene_shader.set_conditional(SceneShaderGLES2::USE_SHADOW, true);
  1453. glActiveTexture(GL_TEXTURE0 + storage->config.max_texture_image_units - 3);
  1454. if (storage->config.use_rgba_3d_shadows) {
  1455. glBindTexture(GL_TEXTURE_2D, directional_shadow.color);
  1456. } else {
  1457. glBindTexture(GL_TEXTURE_2D, directional_shadow.depth);
  1458. }
  1459. state.scene_shader.set_conditional(SceneShaderGLES2::SHADOW_MODE_PCF_5, shadow_filter_mode == SHADOW_FILTER_PCF5);
  1460. state.scene_shader.set_conditional(SceneShaderGLES2::SHADOW_MODE_PCF_13, shadow_filter_mode == SHADOW_FILTER_PCF13);
  1461. }
  1462. } break;
  1463. case RS::LIGHT_OMNI: {
  1464. state.scene_shader.set_conditional(SceneShaderGLES2::LIGHT_MODE_OMNI, true);
  1465. if (!state.render_no_shadows && shadow_atlas && p_light->light_ptr->shadow) {
  1466. state.scene_shader.set_conditional(SceneShaderGLES2::USE_SHADOW, true);
  1467. glActiveTexture(GL_TEXTURE0 + storage->config.max_texture_image_units - 3);
  1468. if (storage->config.use_rgba_3d_shadows) {
  1469. glBindTexture(GL_TEXTURE_2D, shadow_atlas->color);
  1470. } else {
  1471. glBindTexture(GL_TEXTURE_2D, shadow_atlas->depth);
  1472. }
  1473. state.scene_shader.set_conditional(SceneShaderGLES2::SHADOW_MODE_PCF_5, shadow_filter_mode == SHADOW_FILTER_PCF5);
  1474. state.scene_shader.set_conditional(SceneShaderGLES2::SHADOW_MODE_PCF_13, shadow_filter_mode == SHADOW_FILTER_PCF13);
  1475. }
  1476. } break;
  1477. case RS::LIGHT_SPOT: {
  1478. state.scene_shader.set_conditional(SceneShaderGLES2::LIGHT_MODE_SPOT, true);
  1479. if (!state.render_no_shadows && shadow_atlas && p_light->light_ptr->shadow) {
  1480. state.scene_shader.set_conditional(SceneShaderGLES2::USE_SHADOW, true);
  1481. glActiveTexture(GL_TEXTURE0 + storage->config.max_texture_image_units - 3);
  1482. if (storage->config.use_rgba_3d_shadows) {
  1483. glBindTexture(GL_TEXTURE_2D, shadow_atlas->color);
  1484. } else {
  1485. glBindTexture(GL_TEXTURE_2D, shadow_atlas->depth);
  1486. }
  1487. state.scene_shader.set_conditional(SceneShaderGLES2::SHADOW_MODE_PCF_5, shadow_filter_mode == SHADOW_FILTER_PCF5);
  1488. state.scene_shader.set_conditional(SceneShaderGLES2::SHADOW_MODE_PCF_13, shadow_filter_mode == SHADOW_FILTER_PCF13);
  1489. }
  1490. } break;
  1491. }
  1492. }
  1493. void RasterizerSceneGLES2::_setup_light(LightInstance *light, ShadowAtlas *shadow_atlas, const Transform &p_view_transform, bool accum_pass) {
  1494. RasterizerStorageGLES2::Light *light_ptr = light->light_ptr;
  1495. //common parameters
  1496. float energy = light_ptr->param[RS::LIGHT_PARAM_ENERGY];
  1497. float specular = light_ptr->param[RS::LIGHT_PARAM_SPECULAR];
  1498. float sign = (light_ptr->negative && !accum_pass) ? -1 : 1; //inverse color for base pass lights only
  1499. state.scene_shader.set_uniform(SceneShaderGLES2::LIGHT_SPECULAR, specular);
  1500. Color color = light_ptr->color * sign * energy * Math_PI;
  1501. state.scene_shader.set_uniform(SceneShaderGLES2::LIGHT_COLOR, color);
  1502. state.scene_shader.set_uniform(SceneShaderGLES2::SHADOW_COLOR, light_ptr->shadow_color);
  1503. //specific parameters
  1504. switch (light_ptr->type) {
  1505. case RS::LIGHT_DIRECTIONAL: {
  1506. //not using inverse for performance, view should be normalized anyway
  1507. Vector3 direction = p_view_transform.basis.xform_inv(light->transform.basis.xform(Vector3(0, 0, -1))).normalized();
  1508. state.scene_shader.set_uniform(SceneShaderGLES2::LIGHT_DIRECTION, direction);
  1509. CameraMatrix matrices[4];
  1510. if (!state.render_no_shadows && light_ptr->shadow && directional_shadow.depth) {
  1511. int shadow_count = 0;
  1512. Color split_offsets;
  1513. switch (light_ptr->directional_shadow_mode) {
  1514. case RS::LIGHT_DIRECTIONAL_SHADOW_ORTHOGONAL: {
  1515. shadow_count = 1;
  1516. } break;
  1517. case RS::LIGHT_DIRECTIONAL_SHADOW_PARALLEL_2_SPLITS: {
  1518. shadow_count = 2;
  1519. } break;
  1520. case RS::LIGHT_DIRECTIONAL_SHADOW_PARALLEL_4_SPLITS: {
  1521. shadow_count = 4;
  1522. } break;
  1523. }
  1524. for (int k = 0; k < shadow_count; k++) {
  1525. uint32_t x = light->directional_rect.position.x;
  1526. uint32_t y = light->directional_rect.position.y;
  1527. uint32_t width = light->directional_rect.size.x;
  1528. uint32_t height = light->directional_rect.size.y;
  1529. if (light_ptr->directional_shadow_mode == RS::LIGHT_DIRECTIONAL_SHADOW_PARALLEL_4_SPLITS) {
  1530. width /= 2;
  1531. height /= 2;
  1532. if (k == 1) {
  1533. x += width;
  1534. } else if (k == 2) {
  1535. y += height;
  1536. } else if (k == 3) {
  1537. x += width;
  1538. y += height;
  1539. }
  1540. } else if (light_ptr->directional_shadow_mode == RS::LIGHT_DIRECTIONAL_SHADOW_PARALLEL_2_SPLITS) {
  1541. height /= 2;
  1542. if (k != 0) {
  1543. y += height;
  1544. }
  1545. }
  1546. split_offsets[k] = light->shadow_transform[k].split;
  1547. Transform modelview = (p_view_transform.inverse() * light->shadow_transform[k].transform).affine_inverse();
  1548. CameraMatrix bias;
  1549. bias.set_light_bias();
  1550. CameraMatrix rectm;
  1551. Rect2 atlas_rect = Rect2(float(x) / directional_shadow.size, float(y) / directional_shadow.size, float(width) / directional_shadow.size, float(height) / directional_shadow.size);
  1552. rectm.set_light_atlas_rect(atlas_rect);
  1553. CameraMatrix shadow_mtx = rectm * bias * light->shadow_transform[k].camera * modelview;
  1554. matrices[k] = shadow_mtx;
  1555. /*Color light_clamp;
  1556. light_clamp[0] = atlas_rect.position.x;
  1557. light_clamp[1] = atlas_rect.position.y;
  1558. light_clamp[2] = atlas_rect.size.x;
  1559. light_clamp[3] = atlas_rect.size.y;*/
  1560. }
  1561. // state.scene_shader.set_uniform(SceneShaderGLES2::LIGHT_CLAMP, light_clamp);
  1562. state.scene_shader.set_uniform(SceneShaderGLES2::SHADOW_PIXEL_SIZE, Size2(1.0 / directional_shadow.size, 1.0 / directional_shadow.size));
  1563. state.scene_shader.set_uniform(SceneShaderGLES2::LIGHT_SPLIT_OFFSETS, split_offsets);
  1564. state.scene_shader.set_uniform(SceneShaderGLES2::LIGHT_SHADOW_MATRIX, matrices[0]);
  1565. state.scene_shader.set_uniform(SceneShaderGLES2::LIGHT_SHADOW_MATRIX2, matrices[1]);
  1566. state.scene_shader.set_uniform(SceneShaderGLES2::LIGHT_SHADOW_MATRIX3, matrices[2]);
  1567. state.scene_shader.set_uniform(SceneShaderGLES2::LIGHT_SHADOW_MATRIX4, matrices[3]);
  1568. }
  1569. } break;
  1570. case RS::LIGHT_OMNI: {
  1571. Vector3 position = p_view_transform.xform_inv(light->transform.origin);
  1572. state.scene_shader.set_uniform(SceneShaderGLES2::LIGHT_POSITION, position);
  1573. float range = light_ptr->param[RS::LIGHT_PARAM_RANGE];
  1574. state.scene_shader.set_uniform(SceneShaderGLES2::LIGHT_RANGE, range);
  1575. float attenuation = light_ptr->param[RS::LIGHT_PARAM_ATTENUATION];
  1576. state.scene_shader.set_uniform(SceneShaderGLES2::LIGHT_ATTENUATION, attenuation);
  1577. if (!state.render_no_shadows && light_ptr->shadow && shadow_atlas && shadow_atlas->shadow_owners.has(light->self)) {
  1578. uint32_t key = shadow_atlas->shadow_owners[light->self];
  1579. uint32_t quadrant = (key >> ShadowAtlas::QUADRANT_SHIFT) & 0x03;
  1580. uint32_t shadow = key & ShadowAtlas::SHADOW_INDEX_MASK;
  1581. ERR_BREAK(shadow >= (uint32_t)shadow_atlas->quadrants[quadrant].shadows.size());
  1582. uint32_t atlas_size = shadow_atlas->size;
  1583. uint32_t quadrant_size = atlas_size >> 1;
  1584. uint32_t x = (quadrant & 1) * quadrant_size;
  1585. uint32_t y = (quadrant >> 1) * quadrant_size;
  1586. uint32_t shadow_size = (quadrant_size / shadow_atlas->quadrants[quadrant].subdivision);
  1587. x += (shadow % shadow_atlas->quadrants[quadrant].subdivision) * shadow_size;
  1588. y += (shadow / shadow_atlas->quadrants[quadrant].subdivision) * shadow_size;
  1589. uint32_t width = shadow_size;
  1590. uint32_t height = shadow_size;
  1591. if (light->light_ptr->omni_shadow_detail == RS::LIGHT_OMNI_SHADOW_DETAIL_HORIZONTAL) {
  1592. height /= 2;
  1593. } else {
  1594. width /= 2;
  1595. }
  1596. Transform proj = (p_view_transform.inverse() * light->transform).inverse();
  1597. Color light_clamp;
  1598. light_clamp[0] = float(x) / atlas_size;
  1599. light_clamp[1] = float(y) / atlas_size;
  1600. light_clamp[2] = float(width) / atlas_size;
  1601. light_clamp[3] = float(height) / atlas_size;
  1602. state.scene_shader.set_uniform(SceneShaderGLES2::SHADOW_PIXEL_SIZE, Size2(1.0 / shadow_atlas->size, 1.0 / shadow_atlas->size));
  1603. state.scene_shader.set_uniform(SceneShaderGLES2::LIGHT_SHADOW_MATRIX, proj);
  1604. state.scene_shader.set_uniform(SceneShaderGLES2::LIGHT_CLAMP, light_clamp);
  1605. }
  1606. } break;
  1607. case RS::LIGHT_SPOT: {
  1608. Vector3 position = p_view_transform.xform_inv(light->transform.origin);
  1609. state.scene_shader.set_uniform(SceneShaderGLES2::LIGHT_POSITION, position);
  1610. Vector3 direction = p_view_transform.inverse().basis.xform(light->transform.basis.xform(Vector3(0, 0, -1))).normalized();
  1611. state.scene_shader.set_uniform(SceneShaderGLES2::LIGHT_DIRECTION, direction);
  1612. float attenuation = light_ptr->param[RS::LIGHT_PARAM_ATTENUATION];
  1613. float range = light_ptr->param[RS::LIGHT_PARAM_RANGE];
  1614. float spot_attenuation = light_ptr->param[RS::LIGHT_PARAM_SPOT_ATTENUATION];
  1615. float angle = light_ptr->param[RS::LIGHT_PARAM_SPOT_ANGLE];
  1616. angle = Math::cos(Math::deg2rad(angle));
  1617. state.scene_shader.set_uniform(SceneShaderGLES2::LIGHT_ATTENUATION, attenuation);
  1618. state.scene_shader.set_uniform(SceneShaderGLES2::LIGHT_SPOT_ATTENUATION, spot_attenuation);
  1619. state.scene_shader.set_uniform(SceneShaderGLES2::LIGHT_SPOT_RANGE, spot_attenuation);
  1620. state.scene_shader.set_uniform(SceneShaderGLES2::LIGHT_SPOT_ANGLE, angle);
  1621. state.scene_shader.set_uniform(SceneShaderGLES2::LIGHT_RANGE, range);
  1622. if (!state.render_no_shadows && light->light_ptr->shadow && shadow_atlas && shadow_atlas->shadow_owners.has(light->self)) {
  1623. uint32_t key = shadow_atlas->shadow_owners[light->self];
  1624. uint32_t quadrant = (key >> ShadowAtlas::QUADRANT_SHIFT) & 0x03;
  1625. uint32_t shadow = key & ShadowAtlas::SHADOW_INDEX_MASK;
  1626. ERR_BREAK(shadow >= (uint32_t)shadow_atlas->quadrants[quadrant].shadows.size());
  1627. uint32_t atlas_size = shadow_atlas->size;
  1628. uint32_t quadrant_size = atlas_size >> 1;
  1629. uint32_t x = (quadrant & 1) * quadrant_size;
  1630. uint32_t y = (quadrant >> 1) * quadrant_size;
  1631. uint32_t shadow_size = (quadrant_size / shadow_atlas->quadrants[quadrant].subdivision);
  1632. x += (shadow % shadow_atlas->quadrants[quadrant].subdivision) * shadow_size;
  1633. y += (shadow / shadow_atlas->quadrants[quadrant].subdivision) * shadow_size;
  1634. uint32_t width = shadow_size;
  1635. uint32_t height = shadow_size;
  1636. Rect2 rect(float(x) / atlas_size, float(y) / atlas_size, float(width) / atlas_size, float(height) / atlas_size);
  1637. Color light_clamp;
  1638. light_clamp[0] = rect.position.x;
  1639. light_clamp[1] = rect.position.y;
  1640. light_clamp[2] = rect.size.x;
  1641. light_clamp[3] = rect.size.y;
  1642. Transform modelview = (p_view_transform.inverse() * light->transform).inverse();
  1643. CameraMatrix bias;
  1644. bias.set_light_bias();
  1645. CameraMatrix rectm;
  1646. rectm.set_light_atlas_rect(rect);
  1647. CameraMatrix shadow_matrix = rectm * bias * light->shadow_transform[0].camera * modelview;
  1648. state.scene_shader.set_uniform(SceneShaderGLES2::SHADOW_PIXEL_SIZE, Size2(1.0 / shadow_atlas->size, 1.0 / shadow_atlas->size));
  1649. state.scene_shader.set_uniform(SceneShaderGLES2::LIGHT_SHADOW_MATRIX, shadow_matrix);
  1650. state.scene_shader.set_uniform(SceneShaderGLES2::LIGHT_CLAMP, light_clamp);
  1651. }
  1652. } break;
  1653. default: {
  1654. }
  1655. }
  1656. }
  1657. void RasterizerSceneGLES2::_setup_refprobes(ReflectionProbeInstance *p_refprobe1, ReflectionProbeInstance *p_refprobe2, const Transform &p_view_transform, Environment *p_env) {
  1658. if (p_refprobe1) {
  1659. state.scene_shader.set_uniform(SceneShaderGLES2::REFPROBE1_USE_BOX_PROJECT, p_refprobe1->probe_ptr->box_projection);
  1660. state.scene_shader.set_uniform(SceneShaderGLES2::REFPROBE1_BOX_EXTENTS, p_refprobe1->probe_ptr->extents);
  1661. state.scene_shader.set_uniform(SceneShaderGLES2::REFPROBE1_BOX_OFFSET, p_refprobe1->probe_ptr->origin_offset);
  1662. state.scene_shader.set_uniform(SceneShaderGLES2::REFPROBE1_EXTERIOR, !p_refprobe1->probe_ptr->interior);
  1663. state.scene_shader.set_uniform(SceneShaderGLES2::REFPROBE1_INTENSITY, p_refprobe1->probe_ptr->intensity);
  1664. Color ambient;
  1665. if (p_refprobe1->probe_ptr->interior) {
  1666. ambient = p_refprobe1->probe_ptr->interior_ambient * p_refprobe1->probe_ptr->interior_ambient_energy;
  1667. ambient.a = p_refprobe1->probe_ptr->interior_ambient_probe_contrib;
  1668. } else if (p_env) {
  1669. ambient = p_env->ambient_color * p_env->ambient_energy;
  1670. ambient.a = p_env->ambient_sky_contribution;
  1671. }
  1672. state.scene_shader.set_uniform(SceneShaderGLES2::REFPROBE1_AMBIENT, ambient);
  1673. Transform proj = (p_view_transform.inverse() * p_refprobe1->transform).affine_inverse();
  1674. state.scene_shader.set_uniform(SceneShaderGLES2::REFPROBE1_LOCAL_MATRIX, proj);
  1675. }
  1676. if (p_refprobe2) {
  1677. state.scene_shader.set_uniform(SceneShaderGLES2::REFPROBE2_USE_BOX_PROJECT, p_refprobe2->probe_ptr->box_projection);
  1678. state.scene_shader.set_uniform(SceneShaderGLES2::REFPROBE2_BOX_EXTENTS, p_refprobe2->probe_ptr->extents);
  1679. state.scene_shader.set_uniform(SceneShaderGLES2::REFPROBE2_BOX_OFFSET, p_refprobe2->probe_ptr->origin_offset);
  1680. state.scene_shader.set_uniform(SceneShaderGLES2::REFPROBE2_EXTERIOR, p_refprobe2->probe_ptr->interior);
  1681. state.scene_shader.set_uniform(SceneShaderGLES2::REFPROBE2_INTENSITY, p_refprobe2->probe_ptr->intensity);
  1682. Color ambient;
  1683. if (p_refprobe2->probe_ptr->interior) {
  1684. ambient = p_refprobe2->probe_ptr->interior_ambient * p_refprobe2->probe_ptr->interior_ambient_energy;
  1685. ambient.a = p_refprobe2->probe_ptr->interior_ambient_probe_contrib;
  1686. } else if (p_env) {
  1687. ambient = p_env->ambient_color * p_env->ambient_energy;
  1688. ambient.a = p_env->ambient_sky_contribution;
  1689. }
  1690. state.scene_shader.set_uniform(SceneShaderGLES2::REFPROBE2_AMBIENT, ambient);
  1691. Transform proj = (p_view_transform.inverse() * p_refprobe2->transform).affine_inverse();
  1692. state.scene_shader.set_uniform(SceneShaderGLES2::REFPROBE2_LOCAL_MATRIX, proj);
  1693. }
  1694. }
  1695. void RasterizerSceneGLES2::_render_render_list(RenderList::Element **p_elements, int p_element_count, const Transform &p_view_transform, const CameraMatrix &p_projection, RID p_shadow_atlas, Environment *p_env, GLuint p_base_env, float p_shadow_bias, float p_shadow_normal_bias, bool p_reverse_cull, bool p_alpha_pass, bool p_shadow) {
  1696. ShadowAtlas *shadow_atlas = shadow_atlas_owner.getornull(p_shadow_atlas);
  1697. Vector2 viewport_size = state.viewport_size;
  1698. Vector2 screen_pixel_size = state.screen_pixel_size;
  1699. bool use_radiance_map = false;
  1700. if (!p_shadow && p_base_env) {
  1701. glActiveTexture(GL_TEXTURE0 + storage->config.max_texture_image_units - 2);
  1702. glBindTexture(GL_TEXTURE_CUBE_MAP, p_base_env);
  1703. use_radiance_map = true;
  1704. state.scene_shader.set_conditional(SceneShaderGLES2::USE_RADIANCE_MAP, true); //since prev unshaded is false, this needs to be true if exists
  1705. }
  1706. bool prev_unshaded = false;
  1707. bool prev_instancing = false;
  1708. bool prev_depth_prepass = false;
  1709. state.scene_shader.set_conditional(SceneShaderGLES2::SHADELESS, false);
  1710. RasterizerStorageGLES2::Material *prev_material = NULL;
  1711. RasterizerStorageGLES2::Geometry *prev_geometry = NULL;
  1712. RasterizerStorageGLES2::Skeleton *prev_skeleton = NULL;
  1713. RasterizerStorageGLES2::GeometryOwner *prev_owner = NULL;
  1714. Transform view_transform_inverse = p_view_transform.inverse();
  1715. CameraMatrix projection_inverse = p_projection.inverse();
  1716. bool prev_base_pass = false;
  1717. LightInstance *prev_light = NULL;
  1718. bool prev_vertex_lit = false;
  1719. ReflectionProbeInstance *prev_refprobe_1 = NULL;
  1720. ReflectionProbeInstance *prev_refprobe_2 = NULL;
  1721. int prev_blend_mode = -2; //will always catch the first go
  1722. state.cull_front = false;
  1723. state.cull_disabled = false;
  1724. glCullFace(GL_BACK);
  1725. glEnable(GL_CULL_FACE);
  1726. if (p_alpha_pass) {
  1727. glEnable(GL_BLEND);
  1728. } else {
  1729. glDisable(GL_BLEND);
  1730. }
  1731. float fog_max_distance = 0;
  1732. bool using_fog = false;
  1733. if (p_env && !p_shadow && p_env->fog_enabled && (p_env->fog_depth_enabled || p_env->fog_height_enabled)) {
  1734. state.scene_shader.set_conditional(SceneShaderGLES2::FOG_DEPTH_ENABLED, p_env->fog_depth_enabled);
  1735. state.scene_shader.set_conditional(SceneShaderGLES2::FOG_HEIGHT_ENABLED, p_env->fog_height_enabled);
  1736. if (p_env->fog_depth_end > 0) {
  1737. fog_max_distance = p_env->fog_depth_end;
  1738. } else {
  1739. fog_max_distance = p_projection.get_z_far();
  1740. }
  1741. using_fog = true;
  1742. }
  1743. RasterizerStorageGLES2::Texture *prev_lightmap = NULL;
  1744. float lightmap_energy = 1.0;
  1745. bool prev_use_lightmap_capture = false;
  1746. storage->info.render.draw_call_count += p_element_count;
  1747. for (int i = 0; i < p_element_count; i++) {
  1748. RenderList::Element *e = p_elements[i];
  1749. RasterizerStorageGLES2::Material *material = e->material;
  1750. bool rebind = false;
  1751. bool accum_pass = *e->use_accum_ptr;
  1752. *e->use_accum_ptr = true; //set to accum for next time this is found
  1753. LightInstance *light = NULL;
  1754. ReflectionProbeInstance *refprobe_1 = NULL;
  1755. ReflectionProbeInstance *refprobe_2 = NULL;
  1756. RasterizerStorageGLES2::Texture *lightmap = NULL;
  1757. bool use_lightmap_capture = false;
  1758. bool rebind_light = false;
  1759. bool rebind_reflection = false;
  1760. bool rebind_lightmap = false;
  1761. if (!p_shadow && material->shader) {
  1762. bool unshaded = material->shader->spatial.unshaded;
  1763. if (unshaded != prev_unshaded) {
  1764. rebind = true;
  1765. if (unshaded) {
  1766. state.scene_shader.set_conditional(SceneShaderGLES2::SHADELESS, true);
  1767. state.scene_shader.set_conditional(SceneShaderGLES2::USE_RADIANCE_MAP, false);
  1768. state.scene_shader.set_conditional(SceneShaderGLES2::USE_LIGHTING, false);
  1769. } else {
  1770. state.scene_shader.set_conditional(SceneShaderGLES2::SHADELESS, false);
  1771. state.scene_shader.set_conditional(SceneShaderGLES2::USE_RADIANCE_MAP, use_radiance_map);
  1772. }
  1773. prev_unshaded = unshaded;
  1774. }
  1775. bool base_pass = !accum_pass && !unshaded; //conditions for a base pass
  1776. if (base_pass != prev_base_pass) {
  1777. state.scene_shader.set_conditional(SceneShaderGLES2::BASE_PASS, base_pass);
  1778. rebind = true;
  1779. prev_base_pass = base_pass;
  1780. }
  1781. if (!unshaded && e->light_index < RenderList::MAX_LIGHTS) {
  1782. light = render_light_instances[e->light_index];
  1783. }
  1784. if (light != prev_light) {
  1785. _setup_light_type(light, shadow_atlas);
  1786. rebind = true;
  1787. rebind_light = true;
  1788. }
  1789. int blend_mode = p_alpha_pass ? material->shader->spatial.blend_mode : -1; // -1 no blend, no mix
  1790. if (accum_pass) { //accum pass force pass
  1791. blend_mode = RasterizerStorageGLES2::Shader::Spatial::BLEND_MODE_ADD;
  1792. if (light && light->light_ptr->negative) {
  1793. blend_mode = RasterizerStorageGLES2::Shader::Spatial::BLEND_MODE_SUB;
  1794. }
  1795. }
  1796. if (prev_blend_mode != blend_mode) {
  1797. if (prev_blend_mode == -1 && blend_mode != -1) {
  1798. //does blend
  1799. glEnable(GL_BLEND);
  1800. } else if (blend_mode == -1 && prev_blend_mode != -1) {
  1801. //do not blend
  1802. glDisable(GL_BLEND);
  1803. }
  1804. switch (blend_mode) {
  1805. //-1 not handled because not blend is enabled anyway
  1806. case RasterizerStorageGLES2::Shader::Spatial::BLEND_MODE_MIX: {
  1807. glBlendEquation(GL_FUNC_ADD);
  1808. if (storage->frame.current_rt && storage->frame.current_rt->flags[RasterizerStorage::RENDER_TARGET_TRANSPARENT]) {
  1809. glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
  1810. } else {
  1811. glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  1812. }
  1813. } break;
  1814. case RasterizerStorageGLES2::Shader::Spatial::BLEND_MODE_ADD: {
  1815. glBlendEquation(GL_FUNC_ADD);
  1816. glBlendFunc(p_alpha_pass ? GL_SRC_ALPHA : GL_ONE, GL_ONE);
  1817. } break;
  1818. case RasterizerStorageGLES2::Shader::Spatial::BLEND_MODE_SUB: {
  1819. glBlendEquation(GL_FUNC_REVERSE_SUBTRACT);
  1820. glBlendFunc(GL_SRC_ALPHA, GL_ONE);
  1821. } break;
  1822. case RasterizerStorageGLES2::Shader::Spatial::BLEND_MODE_MUL: {
  1823. glBlendEquation(GL_FUNC_ADD);
  1824. if (storage->frame.current_rt && storage->frame.current_rt->flags[RasterizerStorage::RENDER_TARGET_TRANSPARENT]) {
  1825. glBlendFuncSeparate(GL_DST_COLOR, GL_ZERO, GL_DST_ALPHA, GL_ZERO);
  1826. } else {
  1827. glBlendFuncSeparate(GL_DST_COLOR, GL_ZERO, GL_ZERO, GL_ONE);
  1828. }
  1829. } break;
  1830. }
  1831. prev_blend_mode = blend_mode;
  1832. }
  1833. //condition to enable vertex lighting on this object
  1834. bool vertex_lit = (material->shader->spatial.uses_vertex_lighting || storage->config.force_vertex_shading) && ((!unshaded && light) || using_fog); //fog forces vertex lighting because it still applies even if unshaded or no fog
  1835. if (vertex_lit != prev_vertex_lit) {
  1836. state.scene_shader.set_conditional(SceneShaderGLES2::USE_VERTEX_LIGHTING, vertex_lit);
  1837. prev_vertex_lit = vertex_lit;
  1838. }
  1839. if (!unshaded && !accum_pass && e->refprobe_0_index != RenderList::MAX_REFLECTION_PROBES) {
  1840. ERR_FAIL_INDEX(e->refprobe_0_index, reflection_probe_count);
  1841. refprobe_1 = reflection_probe_instances[e->refprobe_0_index];
  1842. }
  1843. if (!unshaded && !accum_pass && e->refprobe_1_index != RenderList::MAX_REFLECTION_PROBES) {
  1844. ERR_FAIL_INDEX(e->refprobe_1_index, reflection_probe_count);
  1845. refprobe_2 = reflection_probe_instances[e->refprobe_1_index];
  1846. }
  1847. if (refprobe_1 != prev_refprobe_1 || refprobe_2 != prev_refprobe_2) {
  1848. state.scene_shader.set_conditional(SceneShaderGLES2::USE_REFLECTION_PROBE1, refprobe_1 != NULL);
  1849. state.scene_shader.set_conditional(SceneShaderGLES2::USE_REFLECTION_PROBE2, refprobe_2 != NULL);
  1850. if (refprobe_1 != NULL && refprobe_1 != prev_refprobe_1) {
  1851. glActiveTexture(GL_TEXTURE0 + storage->config.max_texture_image_units - 5);
  1852. glBindTexture(GL_TEXTURE_CUBE_MAP, refprobe_1->cubemap);
  1853. }
  1854. if (refprobe_2 != NULL && refprobe_2 != prev_refprobe_2) {
  1855. glActiveTexture(GL_TEXTURE0 + storage->config.max_texture_image_units - 6);
  1856. glBindTexture(GL_TEXTURE_CUBE_MAP, refprobe_2->cubemap);
  1857. }
  1858. rebind = true;
  1859. rebind_reflection = true;
  1860. }
  1861. use_lightmap_capture = !unshaded && !accum_pass && !e->instance->lightmap_capture_data.empty();
  1862. if (use_lightmap_capture != prev_use_lightmap_capture) {
  1863. state.scene_shader.set_conditional(SceneShaderGLES2::USE_LIGHTMAP_CAPTURE, use_lightmap_capture);
  1864. rebind = true;
  1865. }
  1866. if (!unshaded && !accum_pass && e->instance->lightmap.is_valid()) {
  1867. lightmap = storage->texture_owner.getornull(e->instance->lightmap);
  1868. lightmap_energy = 1.0;
  1869. if (lightmap) {
  1870. RasterizerStorageGLES2::LightmapCapture *capture = storage->lightmap_capture_data_owner.getornull(e->instance->lightmap_capture->base);
  1871. if (capture) {
  1872. lightmap_energy = capture->energy;
  1873. }
  1874. }
  1875. }
  1876. if (lightmap != prev_lightmap) {
  1877. state.scene_shader.set_conditional(SceneShaderGLES2::USE_LIGHTMAP, lightmap != NULL);
  1878. if (lightmap != NULL) {
  1879. glActiveTexture(GL_TEXTURE0 + storage->config.max_texture_image_units - 4);
  1880. glBindTexture(GL_TEXTURE_2D, lightmap->tex_id);
  1881. }
  1882. rebind = true;
  1883. rebind_lightmap = true;
  1884. }
  1885. }
  1886. bool depth_prepass = false;
  1887. if (!p_alpha_pass && material->shader->spatial.depth_draw_mode == RasterizerStorageGLES2::Shader::Spatial::DEPTH_DRAW_ALPHA_PREPASS) {
  1888. depth_prepass = true;
  1889. }
  1890. if (depth_prepass != prev_depth_prepass) {
  1891. state.scene_shader.set_conditional(SceneShaderGLES2::USE_DEPTH_PREPASS, depth_prepass);
  1892. prev_depth_prepass = depth_prepass;
  1893. rebind = true;
  1894. }
  1895. bool instancing = e->instance->base_type == RS::INSTANCE_MULTIMESH;
  1896. if (instancing != prev_instancing) {
  1897. state.scene_shader.set_conditional(SceneShaderGLES2::USE_INSTANCING, instancing);
  1898. rebind = true;
  1899. }
  1900. RasterizerStorageGLES2::Skeleton *skeleton = storage->skeleton_owner.getornull(e->instance->skeleton);
  1901. if (skeleton != prev_skeleton) {
  1902. if (skeleton) {
  1903. state.scene_shader.set_conditional(SceneShaderGLES2::USE_SKELETON, true);
  1904. state.scene_shader.set_conditional(SceneShaderGLES2::USE_SKELETON_SOFTWARE, storage->config.use_skeleton_software);
  1905. } else {
  1906. state.scene_shader.set_conditional(SceneShaderGLES2::USE_SKELETON, false);
  1907. state.scene_shader.set_conditional(SceneShaderGLES2::USE_SKELETON_SOFTWARE, false);
  1908. }
  1909. rebind = true;
  1910. }
  1911. if (e->owner != prev_owner || e->geometry != prev_geometry || skeleton != prev_skeleton) {
  1912. _setup_geometry(e, skeleton);
  1913. storage->info.render.surface_switch_count++;
  1914. }
  1915. bool shader_rebind = false;
  1916. if (rebind || material != prev_material) {
  1917. storage->info.render.material_switch_count++;
  1918. shader_rebind = _setup_material(material, p_alpha_pass, Size2i(skeleton ? skeleton->size * 3 : 0, 0));
  1919. if (shader_rebind) {
  1920. storage->info.render.shader_rebind_count++;
  1921. }
  1922. }
  1923. _set_cull(e->front_facing, material->shader->spatial.cull_mode == RasterizerStorageGLES2::Shader::Spatial::CULL_MODE_DISABLED, p_reverse_cull);
  1924. if (i == 0 || shader_rebind) { //first time must rebind
  1925. if (p_shadow) {
  1926. state.scene_shader.set_uniform(SceneShaderGLES2::LIGHT_BIAS, p_shadow_bias);
  1927. state.scene_shader.set_uniform(SceneShaderGLES2::LIGHT_NORMAL_BIAS, p_shadow_normal_bias);
  1928. if (state.shadow_is_dual_parabolloid) {
  1929. state.scene_shader.set_uniform(SceneShaderGLES2::SHADOW_DUAL_PARABOLOID_RENDER_SIDE, state.dual_parbolloid_direction);
  1930. state.scene_shader.set_uniform(SceneShaderGLES2::SHADOW_DUAL_PARABOLOID_RENDER_ZFAR, state.dual_parbolloid_zfar);
  1931. }
  1932. } else {
  1933. if (use_radiance_map) {
  1934. if (p_env) {
  1935. Transform sky_orientation(p_env->sky_orientation, Vector3(0.0, 0.0, 0.0));
  1936. state.scene_shader.set_uniform(SceneShaderGLES2::RADIANCE_INVERSE_XFORM, sky_orientation.affine_inverse() * p_view_transform);
  1937. } else {
  1938. // would be a bit weird if we don't have this...
  1939. state.scene_shader.set_uniform(SceneShaderGLES2::RADIANCE_INVERSE_XFORM, p_view_transform);
  1940. }
  1941. }
  1942. if (p_env) {
  1943. state.scene_shader.set_uniform(SceneShaderGLES2::BG_ENERGY, p_env->bg_energy);
  1944. state.scene_shader.set_uniform(SceneShaderGLES2::BG_COLOR, p_env->bg_color);
  1945. state.scene_shader.set_uniform(SceneShaderGLES2::AMBIENT_SKY_CONTRIBUTION, p_env->ambient_sky_contribution);
  1946. state.scene_shader.set_uniform(SceneShaderGLES2::AMBIENT_COLOR, p_env->ambient_color);
  1947. state.scene_shader.set_uniform(SceneShaderGLES2::AMBIENT_ENERGY, p_env->ambient_energy);
  1948. } else {
  1949. state.scene_shader.set_uniform(SceneShaderGLES2::BG_ENERGY, 1.0);
  1950. state.scene_shader.set_uniform(SceneShaderGLES2::BG_COLOR, state.default_bg);
  1951. state.scene_shader.set_uniform(SceneShaderGLES2::AMBIENT_SKY_CONTRIBUTION, 1.0);
  1952. state.scene_shader.set_uniform(SceneShaderGLES2::AMBIENT_COLOR, state.default_ambient);
  1953. state.scene_shader.set_uniform(SceneShaderGLES2::AMBIENT_ENERGY, 1.0);
  1954. }
  1955. //rebind all these
  1956. rebind_light = true;
  1957. rebind_reflection = true;
  1958. rebind_lightmap = true;
  1959. if (using_fog) {
  1960. state.scene_shader.set_uniform(SceneShaderGLES2::FOG_COLOR_BASE, p_env->fog_color);
  1961. Color sun_color_amount = p_env->fog_sun_color;
  1962. sun_color_amount.a = p_env->fog_sun_amount;
  1963. state.scene_shader.set_uniform(SceneShaderGLES2::FOG_SUN_COLOR_AMOUNT, sun_color_amount);
  1964. state.scene_shader.set_uniform(SceneShaderGLES2::FOG_TRANSMIT_ENABLED, p_env->fog_transmit_enabled);
  1965. state.scene_shader.set_uniform(SceneShaderGLES2::FOG_TRANSMIT_CURVE, p_env->fog_transmit_curve);
  1966. if (p_env->fog_depth_enabled) {
  1967. state.scene_shader.set_uniform(SceneShaderGLES2::FOG_DEPTH_BEGIN, p_env->fog_depth_begin);
  1968. state.scene_shader.set_uniform(SceneShaderGLES2::FOG_DEPTH_CURVE, p_env->fog_depth_curve);
  1969. state.scene_shader.set_uniform(SceneShaderGLES2::FOG_MAX_DISTANCE, fog_max_distance);
  1970. }
  1971. if (p_env->fog_height_enabled) {
  1972. state.scene_shader.set_uniform(SceneShaderGLES2::FOG_HEIGHT_MIN, p_env->fog_height_min);
  1973. state.scene_shader.set_uniform(SceneShaderGLES2::FOG_HEIGHT_MAX, p_env->fog_height_max);
  1974. state.scene_shader.set_uniform(SceneShaderGLES2::FOG_HEIGHT_MAX, p_env->fog_height_max);
  1975. state.scene_shader.set_uniform(SceneShaderGLES2::FOG_HEIGHT_CURVE, p_env->fog_height_curve);
  1976. }
  1977. }
  1978. }
  1979. state.scene_shader.set_uniform(SceneShaderGLES2::CAMERA_MATRIX, p_view_transform);
  1980. state.scene_shader.set_uniform(SceneShaderGLES2::CAMERA_INVERSE_MATRIX, view_transform_inverse);
  1981. state.scene_shader.set_uniform(SceneShaderGLES2::PROJECTION_MATRIX, p_projection);
  1982. state.scene_shader.set_uniform(SceneShaderGLES2::PROJECTION_INVERSE_MATRIX, projection_inverse);
  1983. state.scene_shader.set_uniform(SceneShaderGLES2::TIME, storage->frame.time[0]);
  1984. state.scene_shader.set_uniform(SceneShaderGLES2::VIEWPORT_SIZE, viewport_size);
  1985. state.scene_shader.set_uniform(SceneShaderGLES2::SCREEN_PIXEL_SIZE, screen_pixel_size);
  1986. }
  1987. if (rebind_light && light) {
  1988. _setup_light(light, shadow_atlas, p_view_transform, accum_pass);
  1989. }
  1990. if (rebind_reflection && (refprobe_1 || refprobe_2)) {
  1991. _setup_refprobes(refprobe_1, refprobe_2, p_view_transform, p_env);
  1992. }
  1993. if (rebind_lightmap && lightmap) {
  1994. state.scene_shader.set_uniform(SceneShaderGLES2::LIGHTMAP_ENERGY, lightmap_energy);
  1995. }
  1996. state.scene_shader.set_uniform(SceneShaderGLES2::WORLD_TRANSFORM, e->instance->transform);
  1997. if (use_lightmap_capture) { //this is per instance, must be set always if present
  1998. glUniform4fv(state.scene_shader.get_uniform_location(SceneShaderGLES2::LIGHTMAP_CAPTURES), 12, (const GLfloat *)e->instance->lightmap_capture_data.ptr());
  1999. state.scene_shader.set_uniform(SceneShaderGLES2::LIGHTMAP_CAPTURE_SKY, false);
  2000. }
  2001. _render_geometry(e);
  2002. prev_geometry = e->geometry;
  2003. prev_owner = e->owner;
  2004. prev_material = material;
  2005. prev_skeleton = skeleton;
  2006. prev_instancing = instancing;
  2007. prev_light = light;
  2008. prev_refprobe_1 = refprobe_1;
  2009. prev_refprobe_2 = refprobe_2;
  2010. prev_lightmap = lightmap;
  2011. prev_use_lightmap_capture = use_lightmap_capture;
  2012. }
  2013. _setup_light_type(NULL, NULL); //clear light stuff
  2014. state.scene_shader.set_conditional(SceneShaderGLES2::USE_SKELETON, false);
  2015. state.scene_shader.set_conditional(SceneShaderGLES2::SHADELESS, false);
  2016. state.scene_shader.set_conditional(SceneShaderGLES2::BASE_PASS, false);
  2017. state.scene_shader.set_conditional(SceneShaderGLES2::USE_INSTANCING, false);
  2018. state.scene_shader.set_conditional(SceneShaderGLES2::USE_RADIANCE_MAP, false);
  2019. state.scene_shader.set_conditional(SceneShaderGLES2::LIGHT_USE_PSSM4, false);
  2020. state.scene_shader.set_conditional(SceneShaderGLES2::LIGHT_USE_PSSM2, false);
  2021. state.scene_shader.set_conditional(SceneShaderGLES2::LIGHT_USE_PSSM_BLEND, false);
  2022. state.scene_shader.set_conditional(SceneShaderGLES2::USE_VERTEX_LIGHTING, false);
  2023. state.scene_shader.set_conditional(SceneShaderGLES2::USE_REFLECTION_PROBE1, false);
  2024. state.scene_shader.set_conditional(SceneShaderGLES2::USE_REFLECTION_PROBE2, false);
  2025. state.scene_shader.set_conditional(SceneShaderGLES2::USE_LIGHTMAP, false);
  2026. state.scene_shader.set_conditional(SceneShaderGLES2::USE_LIGHTMAP_CAPTURE, false);
  2027. state.scene_shader.set_conditional(SceneShaderGLES2::FOG_DEPTH_ENABLED, false);
  2028. state.scene_shader.set_conditional(SceneShaderGLES2::FOG_HEIGHT_ENABLED, false);
  2029. state.scene_shader.set_conditional(SceneShaderGLES2::USE_DEPTH_PREPASS, false);
  2030. }
  2031. void RasterizerSceneGLES2::_draw_sky(RasterizerStorageGLES2::Sky *p_sky, const CameraMatrix &p_projection, const Transform &p_transform, bool p_vflip, float p_custom_fov, float p_energy, const Basis &p_sky_orientation) {
  2032. ERR_FAIL_COND(!p_sky);
  2033. RasterizerStorageGLES2::Texture *tex = storage->texture_owner.getornull(p_sky->panorama);
  2034. ERR_FAIL_COND(!tex);
  2035. glActiveTexture(GL_TEXTURE0);
  2036. glBindTexture(tex->target, tex->tex_id);
  2037. glDepthMask(GL_TRUE);
  2038. glEnable(GL_DEPTH_TEST);
  2039. glDisable(GL_CULL_FACE);
  2040. glDisable(GL_BLEND);
  2041. glDepthFunc(GL_LEQUAL);
  2042. // Camera
  2043. CameraMatrix camera;
  2044. if (p_custom_fov) {
  2045. float near_plane = p_projection.get_z_near();
  2046. float far_plane = p_projection.get_z_far();
  2047. float aspect = p_projection.get_aspect();
  2048. camera.set_perspective(p_custom_fov, aspect, near_plane, far_plane);
  2049. } else {
  2050. camera = p_projection;
  2051. }
  2052. float flip_sign = p_vflip ? -1 : 1;
  2053. // If matrix[2][0] or matrix[2][1] we're dealing with an asymmetrical projection matrix. This is the case for stereoscopic rendering (i.e. VR).
  2054. // To ensure the image rendered is perspective correct we need to move some logic into the shader. For this the USE_ASYM_PANO option is introduced.
  2055. // It also means the uv coordinates are ignored in this mode and we don't need our loop.
  2056. bool asymmetrical = ((camera.matrix[2][0] != 0.0) || (camera.matrix[2][1] != 0.0));
  2057. Vector3 vertices[8] = {
  2058. Vector3(-1, -1 * flip_sign, 1),
  2059. Vector3(0, 1, 0),
  2060. Vector3(1, -1 * flip_sign, 1),
  2061. Vector3(1, 1, 0),
  2062. Vector3(1, 1 * flip_sign, 1),
  2063. Vector3(1, 0, 0),
  2064. Vector3(-1, 1 * flip_sign, 1),
  2065. Vector3(0, 0, 0),
  2066. };
  2067. if (!asymmetrical) {
  2068. Vector2 vp_he = camera.get_viewport_half_extents();
  2069. float zn;
  2070. zn = p_projection.get_z_near();
  2071. for (int i = 0; i < 4; i++) {
  2072. Vector3 uv = vertices[i * 2 + 1];
  2073. uv.x = (uv.x * 2.0 - 1.0) * vp_he.x;
  2074. uv.y = -(uv.y * 2.0 - 1.0) * vp_he.y;
  2075. uv.z = -zn;
  2076. vertices[i * 2 + 1] = p_transform.basis.xform(uv).normalized();
  2077. vertices[i * 2 + 1].z = -vertices[i * 2 + 1].z;
  2078. }
  2079. }
  2080. glBindBuffer(GL_ARRAY_BUFFER, state.sky_verts);
  2081. glBufferData(GL_ARRAY_BUFFER, sizeof(Vector3) * 8, vertices, GL_DYNAMIC_DRAW);
  2082. // bind sky vertex array....
  2083. glVertexAttribPointer(RS::ARRAY_VERTEX, 3, GL_FLOAT, GL_FALSE, sizeof(Vector3) * 2, 0);
  2084. glVertexAttribPointer(RS::ARRAY_TEX_UV, 3, GL_FLOAT, GL_FALSE, sizeof(Vector3) * 2, CAST_INT_TO_UCHAR_PTR(sizeof(Vector3)));
  2085. glEnableVertexAttribArray(RS::ARRAY_VERTEX);
  2086. glEnableVertexAttribArray(RS::ARRAY_TEX_UV);
  2087. storage->shaders.copy.set_conditional(CopyShaderGLES2::USE_ASYM_PANO, asymmetrical);
  2088. storage->shaders.copy.set_conditional(CopyShaderGLES2::USE_PANORAMA, !asymmetrical);
  2089. storage->shaders.copy.set_conditional(CopyShaderGLES2::USE_MULTIPLIER, true);
  2090. storage->shaders.copy.set_conditional(CopyShaderGLES2::USE_CUBEMAP, false);
  2091. storage->shaders.copy.set_conditional(CopyShaderGLES2::USE_COPY_SECTION, false);
  2092. storage->shaders.copy.set_conditional(CopyShaderGLES2::USE_CUSTOM_ALPHA, false);
  2093. storage->shaders.copy.bind();
  2094. storage->shaders.copy.set_uniform(CopyShaderGLES2::MULTIPLIER, p_energy);
  2095. // don't know why but I always have problems setting a uniform mat3, so we're using a transform
  2096. storage->shaders.copy.set_uniform(CopyShaderGLES2::SKY_TRANSFORM, Transform(p_sky_orientation, Vector3(0.0, 0.0, 0.0)).affine_inverse());
  2097. if (asymmetrical) {
  2098. // pack the bits we need from our projection matrix
  2099. storage->shaders.copy.set_uniform(CopyShaderGLES2::ASYM_PROJ, camera.matrix[2][0], camera.matrix[0][0], camera.matrix[2][1], camera.matrix[1][1]);
  2100. ///@TODO I couldn't get mat3 + p_transform.basis to work, that would be better here.
  2101. storage->shaders.copy.set_uniform(CopyShaderGLES2::PANO_TRANSFORM, p_transform);
  2102. }
  2103. glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
  2104. glDisableVertexAttribArray(RS::ARRAY_VERTEX);
  2105. glDisableVertexAttribArray(RS::ARRAY_TEX_UV);
  2106. glBindBuffer(GL_ARRAY_BUFFER, 0);
  2107. storage->shaders.copy.set_conditional(CopyShaderGLES2::USE_ASYM_PANO, false);
  2108. storage->shaders.copy.set_conditional(CopyShaderGLES2::USE_PANORAMA, false);
  2109. storage->shaders.copy.set_conditional(CopyShaderGLES2::USE_MULTIPLIER, false);
  2110. storage->shaders.copy.set_conditional(CopyShaderGLES2::USE_CUBEMAP, false);
  2111. }
  2112. void RasterizerSceneGLES2::_post_process(Environment *env, const CameraMatrix &p_cam_projection) {
  2113. //copy to front buffer
  2114. glDepthMask(GL_FALSE);
  2115. glDisable(GL_DEPTH_TEST);
  2116. glDisable(GL_CULL_FACE);
  2117. glDisable(GL_BLEND);
  2118. glDepthFunc(GL_LEQUAL);
  2119. glColorMask(1, 1, 1, 1);
  2120. //no post process on small, transparent or render targets without an env
  2121. bool use_post_process = env && !storage->frame.current_rt->flags[RasterizerStorage::RENDER_TARGET_TRANSPARENT];
  2122. use_post_process = use_post_process && storage->frame.current_rt->width >= 4 && storage->frame.current_rt->height >= 4;
  2123. use_post_process = use_post_process && storage->frame.current_rt->mip_maps_allocated;
  2124. if (env) {
  2125. use_post_process = use_post_process && (env->adjustments_enabled || env->glow_enabled || env->dof_blur_far_enabled || env->dof_blur_near_enabled);
  2126. }
  2127. GLuint next_buffer;
  2128. if (use_post_process) {
  2129. next_buffer = storage->frame.current_rt->mip_maps[0].sizes[0].fbo;
  2130. } else if (storage->frame.current_rt->external.fbo != 0) {
  2131. next_buffer = storage->frame.current_rt->external.fbo;
  2132. } else {
  2133. // set next_buffer to front buffer so multisample blit can happen if needed
  2134. next_buffer = storage->frame.current_rt->fbo;
  2135. }
  2136. // If using multisample buffer, resolve to post_process_effect buffer or to front buffer
  2137. if (storage->frame.current_rt && storage->frame.current_rt->multisample_active) {
  2138. #ifdef GLES_OVER_GL
  2139. glBindFramebuffer(GL_READ_FRAMEBUFFER, storage->frame.current_rt->multisample_fbo);
  2140. glReadBuffer(GL_COLOR_ATTACHMENT0);
  2141. glBindFramebuffer(GL_DRAW_FRAMEBUFFER, next_buffer);
  2142. glBlitFramebuffer(0, 0, storage->frame.current_rt->width, storage->frame.current_rt->height, 0, 0, storage->frame.current_rt->width, storage->frame.current_rt->height, GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT, GL_NEAREST);
  2143. glBindFramebuffer(GL_READ_FRAMEBUFFER, 0);
  2144. glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0);
  2145. #elif IPHONE_ENABLED
  2146. glBindFramebuffer(GL_READ_FRAMEBUFFER, storage->frame.current_rt->multisample_fbo);
  2147. glBindFramebuffer(GL_DRAW_FRAMEBUFFER, next_buffer);
  2148. glResolveMultisampleFramebufferAPPLE();
  2149. glBindFramebuffer(GL_READ_FRAMEBUFFER, 0);
  2150. glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0);
  2151. #elif ANDROID_ENABLED
  2152. // In GLES2 Android Blit is not available, so just copy color texture manually
  2153. _copy_texture_to_buffer(storage->frame.current_rt->multisample_color, next_buffer);
  2154. #else
  2155. // TODO: any other platform not supported? this will fail.. maybe we should just call _copy_texture_to_buffer here as well?
  2156. #endif
  2157. } else if (use_post_process) {
  2158. if (storage->frame.current_rt->external.fbo != 0) {
  2159. _copy_texture_to_buffer(storage->frame.current_rt->external.color, storage->frame.current_rt->mip_maps[0].sizes[0].fbo);
  2160. } else {
  2161. _copy_texture_to_buffer(storage->frame.current_rt->color, storage->frame.current_rt->mip_maps[0].sizes[0].fbo);
  2162. }
  2163. }
  2164. if (!use_post_process) {
  2165. return;
  2166. }
  2167. // Order of operation
  2168. //1) DOF Blur (first blur, then copy to buffer applying the blur) //only on desktop
  2169. //2) Bloom (Glow) //only on desktop
  2170. //3) Adjustments
  2171. // DOF Blur
  2172. if (env->dof_blur_far_enabled) {
  2173. int vp_h = storage->frame.current_rt->height;
  2174. int vp_w = storage->frame.current_rt->width;
  2175. state.effect_blur_shader.set_conditional(EffectBlurShaderGLES2::USE_ORTHOGONAL_PROJECTION, p_cam_projection.is_orthogonal());
  2176. state.effect_blur_shader.set_conditional(EffectBlurShaderGLES2::DOF_FAR_BLUR, true);
  2177. state.effect_blur_shader.set_conditional(EffectBlurShaderGLES2::DOF_QUALITY_LOW, env->dof_blur_far_quality == RS::ENV_DOF_BLUR_QUALITY_LOW);
  2178. state.effect_blur_shader.set_conditional(EffectBlurShaderGLES2::DOF_QUALITY_MEDIUM, env->dof_blur_far_quality == RS::ENV_DOF_BLUR_QUALITY_MEDIUM);
  2179. state.effect_blur_shader.set_conditional(EffectBlurShaderGLES2::DOF_QUALITY_HIGH, env->dof_blur_far_quality == RS::ENV_DOF_BLUR_QUALITY_HIGH);
  2180. state.effect_blur_shader.bind();
  2181. int qsteps[3] = { 4, 10, 20 };
  2182. float radius = (env->dof_blur_far_amount * env->dof_blur_far_amount) / qsteps[env->dof_blur_far_quality];
  2183. state.effect_blur_shader.set_uniform(EffectBlurShaderGLES2::DOF_BEGIN, env->dof_blur_far_distance);
  2184. state.effect_blur_shader.set_uniform(EffectBlurShaderGLES2::DOF_END, env->dof_blur_far_distance + env->dof_blur_far_transition);
  2185. state.effect_blur_shader.set_uniform(EffectBlurShaderGLES2::DOF_DIR, Vector2(1, 0));
  2186. state.effect_blur_shader.set_uniform(EffectBlurShaderGLES2::DOF_RADIUS, radius);
  2187. state.effect_blur_shader.set_uniform(EffectBlurShaderGLES2::PIXEL_SIZE, Vector2(1.0 / vp_w, 1.0 / vp_h));
  2188. state.effect_blur_shader.set_uniform(EffectBlurShaderGLES2::CAMERA_Z_NEAR, p_cam_projection.get_z_near());
  2189. state.effect_blur_shader.set_uniform(EffectBlurShaderGLES2::CAMERA_Z_FAR, p_cam_projection.get_z_far());
  2190. glActiveTexture(GL_TEXTURE1);
  2191. glBindTexture(GL_TEXTURE_2D, storage->frame.current_rt->depth);
  2192. glActiveTexture(GL_TEXTURE0);
  2193. if (storage->frame.current_rt->mip_maps[0].color) {
  2194. glBindTexture(GL_TEXTURE_2D, storage->frame.current_rt->mip_maps[0].color);
  2195. } else {
  2196. glBindTexture(GL_TEXTURE_2D, storage->frame.current_rt->mip_maps[0].sizes[0].color);
  2197. }
  2198. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  2199. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  2200. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  2201. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  2202. glBindFramebuffer(GL_FRAMEBUFFER, storage->frame.current_rt->fbo); //copy to front first
  2203. storage->_copy_screen();
  2204. glActiveTexture(GL_TEXTURE0);
  2205. glBindTexture(GL_TEXTURE_2D, storage->frame.current_rt->color);
  2206. state.effect_blur_shader.set_uniform(EffectBlurShaderGLES2::DOF_DIR, Vector2(0, 1));
  2207. glBindFramebuffer(GL_FRAMEBUFFER, storage->frame.current_rt->mip_maps[0].sizes[0].fbo); // copy to base level
  2208. storage->_copy_screen();
  2209. state.effect_blur_shader.set_conditional(EffectBlurShaderGLES2::DOF_FAR_BLUR, false);
  2210. state.effect_blur_shader.set_conditional(EffectBlurShaderGLES2::DOF_QUALITY_LOW, false);
  2211. state.effect_blur_shader.set_conditional(EffectBlurShaderGLES2::DOF_QUALITY_MEDIUM, false);
  2212. state.effect_blur_shader.set_conditional(EffectBlurShaderGLES2::DOF_QUALITY_HIGH, false);
  2213. state.effect_blur_shader.set_conditional(EffectBlurShaderGLES2::USE_ORTHOGONAL_PROJECTION, false);
  2214. }
  2215. if (env->dof_blur_near_enabled) {
  2216. //convert texture to RGBA format if not already
  2217. if (!storage->frame.current_rt->used_dof_blur_near) {
  2218. glActiveTexture(GL_TEXTURE0);
  2219. glBindTexture(GL_TEXTURE_2D, storage->frame.current_rt->color);
  2220. glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, storage->frame.current_rt->width, storage->frame.current_rt->height, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
  2221. }
  2222. int vp_h = storage->frame.current_rt->height;
  2223. int vp_w = storage->frame.current_rt->width;
  2224. state.effect_blur_shader.set_conditional(EffectBlurShaderGLES2::USE_ORTHOGONAL_PROJECTION, p_cam_projection.is_orthogonal());
  2225. state.effect_blur_shader.set_conditional(EffectBlurShaderGLES2::DOF_NEAR_BLUR, true);
  2226. state.effect_blur_shader.set_conditional(EffectBlurShaderGLES2::DOF_NEAR_FIRST_TAP, true);
  2227. state.effect_blur_shader.set_conditional(EffectBlurShaderGLES2::DOF_QUALITY_LOW, env->dof_blur_near_quality == RS::ENV_DOF_BLUR_QUALITY_LOW);
  2228. state.effect_blur_shader.set_conditional(EffectBlurShaderGLES2::DOF_QUALITY_MEDIUM, env->dof_blur_near_quality == RS::ENV_DOF_BLUR_QUALITY_MEDIUM);
  2229. state.effect_blur_shader.set_conditional(EffectBlurShaderGLES2::DOF_QUALITY_HIGH, env->dof_blur_near_quality == RS::ENV_DOF_BLUR_QUALITY_HIGH);
  2230. state.effect_blur_shader.bind();
  2231. int qsteps[3] = { 4, 10, 20 };
  2232. float radius = (env->dof_blur_near_amount * env->dof_blur_near_amount) / qsteps[env->dof_blur_near_quality];
  2233. state.effect_blur_shader.set_uniform(EffectBlurShaderGLES2::DOF_BEGIN, env->dof_blur_near_distance);
  2234. state.effect_blur_shader.set_uniform(EffectBlurShaderGLES2::DOF_END, env->dof_blur_near_distance - env->dof_blur_near_transition);
  2235. state.effect_blur_shader.set_uniform(EffectBlurShaderGLES2::DOF_DIR, Vector2(1, 0));
  2236. state.effect_blur_shader.set_uniform(EffectBlurShaderGLES2::DOF_RADIUS, radius);
  2237. state.effect_blur_shader.set_uniform(EffectBlurShaderGLES2::PIXEL_SIZE, Vector2(1.0 / vp_w, 1.0 / vp_h));
  2238. state.effect_blur_shader.set_uniform(EffectBlurShaderGLES2::CAMERA_Z_NEAR, p_cam_projection.get_z_near());
  2239. state.effect_blur_shader.set_uniform(EffectBlurShaderGLES2::CAMERA_Z_FAR, p_cam_projection.get_z_far());
  2240. glActiveTexture(GL_TEXTURE1);
  2241. glBindTexture(GL_TEXTURE_2D, storage->frame.current_rt->depth);
  2242. glActiveTexture(GL_TEXTURE0);
  2243. if (storage->frame.current_rt->mip_maps[0].color) {
  2244. glBindTexture(GL_TEXTURE_2D, storage->frame.current_rt->mip_maps[0].color);
  2245. } else {
  2246. glBindTexture(GL_TEXTURE_2D, storage->frame.current_rt->mip_maps[0].sizes[0].color);
  2247. }
  2248. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  2249. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  2250. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  2251. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  2252. glBindFramebuffer(GL_FRAMEBUFFER, storage->frame.current_rt->fbo); //copy to front first
  2253. storage->_copy_screen();
  2254. state.effect_blur_shader.set_conditional(EffectBlurShaderGLES2::DOF_NEAR_FIRST_TAP, false);
  2255. state.effect_blur_shader.bind();
  2256. state.effect_blur_shader.set_uniform(EffectBlurShaderGLES2::DOF_BEGIN, env->dof_blur_near_distance);
  2257. state.effect_blur_shader.set_uniform(EffectBlurShaderGLES2::DOF_END, env->dof_blur_near_distance - env->dof_blur_near_transition);
  2258. state.effect_blur_shader.set_uniform(EffectBlurShaderGLES2::DOF_DIR, Vector2(0, 1));
  2259. state.effect_blur_shader.set_uniform(EffectBlurShaderGLES2::DOF_RADIUS, radius);
  2260. state.effect_blur_shader.set_uniform(EffectBlurShaderGLES2::PIXEL_SIZE, Vector2(1.0 / vp_w, 1.0 / vp_h));
  2261. state.effect_blur_shader.set_uniform(EffectBlurShaderGLES2::CAMERA_Z_NEAR, p_cam_projection.get_z_near());
  2262. state.effect_blur_shader.set_uniform(EffectBlurShaderGLES2::CAMERA_Z_FAR, p_cam_projection.get_z_far());
  2263. glActiveTexture(GL_TEXTURE0);
  2264. glBindTexture(GL_TEXTURE_2D, storage->frame.current_rt->color);
  2265. glBindFramebuffer(GL_FRAMEBUFFER, storage->frame.current_rt->mip_maps[0].sizes[0].fbo); // copy to base level
  2266. glEnable(GL_BLEND);
  2267. glBlendEquation(GL_FUNC_ADD);
  2268. glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  2269. storage->_copy_screen();
  2270. glDisable(GL_BLEND);
  2271. state.effect_blur_shader.set_conditional(EffectBlurShaderGLES2::DOF_NEAR_BLUR, false);
  2272. state.effect_blur_shader.set_conditional(EffectBlurShaderGLES2::DOF_NEAR_FIRST_TAP, false);
  2273. state.effect_blur_shader.set_conditional(EffectBlurShaderGLES2::DOF_QUALITY_LOW, false);
  2274. state.effect_blur_shader.set_conditional(EffectBlurShaderGLES2::DOF_QUALITY_MEDIUM, false);
  2275. state.effect_blur_shader.set_conditional(EffectBlurShaderGLES2::DOF_QUALITY_HIGH, false);
  2276. state.effect_blur_shader.set_conditional(EffectBlurShaderGLES2::USE_ORTHOGONAL_PROJECTION, false);
  2277. storage->frame.current_rt->used_dof_blur_near = true;
  2278. }
  2279. if (env->dof_blur_near_enabled || env->dof_blur_far_enabled) {
  2280. //these needed to disable filtering, reenamble
  2281. glActiveTexture(GL_TEXTURE0);
  2282. if (storage->frame.current_rt->mip_maps[0].color) {
  2283. glBindTexture(GL_TEXTURE_2D, storage->frame.current_rt->mip_maps[0].color);
  2284. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
  2285. } else {
  2286. glBindTexture(GL_TEXTURE_2D, storage->frame.current_rt->mip_maps[0].sizes[0].color);
  2287. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  2288. }
  2289. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  2290. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  2291. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  2292. }
  2293. //glow
  2294. int max_glow_level = -1;
  2295. int glow_mask = 0;
  2296. if (env->glow_enabled) {
  2297. for (int i = 0; i < RS::MAX_GLOW_LEVELS; i++) {
  2298. if (env->glow_levels & (1 << i)) {
  2299. if (i >= storage->frame.current_rt->mip_maps[1].sizes.size()) {
  2300. max_glow_level = storage->frame.current_rt->mip_maps[1].sizes.size() - 1;
  2301. glow_mask |= 1 << max_glow_level;
  2302. } else {
  2303. max_glow_level = i;
  2304. glow_mask |= (1 << i);
  2305. }
  2306. }
  2307. }
  2308. for (int i = 0; i < (max_glow_level + 1); i++) {
  2309. int vp_w = storage->frame.current_rt->mip_maps[1].sizes[i].width;
  2310. int vp_h = storage->frame.current_rt->mip_maps[1].sizes[i].height;
  2311. glViewport(0, 0, vp_w, vp_h);
  2312. //horizontal pass
  2313. if (i == 0) {
  2314. state.effect_blur_shader.set_conditional(EffectBlurShaderGLES2::GLOW_FIRST_PASS, true);
  2315. }
  2316. state.effect_blur_shader.set_conditional(EffectBlurShaderGLES2::GLOW_GAUSSIAN_HORIZONTAL, true);
  2317. state.effect_blur_shader.bind();
  2318. state.effect_blur_shader.set_uniform(EffectBlurShaderGLES2::PIXEL_SIZE, Vector2(1.0 / vp_w, 1.0 / vp_h));
  2319. state.effect_blur_shader.set_uniform(EffectBlurShaderGLES2::LOD, storage->frame.current_rt->mip_maps[0].color ? float(i) : 0.0);
  2320. state.effect_blur_shader.set_uniform(EffectBlurShaderGLES2::GLOW_STRENGTH, env->glow_strength);
  2321. state.effect_blur_shader.set_uniform(EffectBlurShaderGLES2::LUMINANCE_CAP, env->glow_hdr_luminance_cap);
  2322. glActiveTexture(GL_TEXTURE0);
  2323. if (storage->frame.current_rt->mip_maps[0].color) {
  2324. glBindTexture(GL_TEXTURE_2D, storage->frame.current_rt->mip_maps[0].color);
  2325. } else {
  2326. glBindTexture(GL_TEXTURE_2D, storage->frame.current_rt->mip_maps[0].sizes[i].color);
  2327. }
  2328. if (i == 0) {
  2329. state.effect_blur_shader.set_uniform(EffectBlurShaderGLES2::GLOW_BLOOM, env->glow_bloom);
  2330. state.effect_blur_shader.set_uniform(EffectBlurShaderGLES2::GLOW_HDR_THRESHOLD, env->glow_hdr_bleed_threshold);
  2331. state.effect_blur_shader.set_uniform(EffectBlurShaderGLES2::GLOW_HDR_SCALE, env->glow_hdr_bleed_scale);
  2332. }
  2333. glBindFramebuffer(GL_FRAMEBUFFER, storage->frame.current_rt->mip_maps[1].sizes[i].fbo);
  2334. storage->_copy_screen();
  2335. state.effect_blur_shader.set_conditional(EffectBlurShaderGLES2::GLOW_GAUSSIAN_HORIZONTAL, false);
  2336. state.effect_blur_shader.set_conditional(EffectBlurShaderGLES2::GLOW_FIRST_PASS, false);
  2337. //vertical pass
  2338. state.effect_blur_shader.set_conditional(EffectBlurShaderGLES2::GLOW_GAUSSIAN_VERTICAL, true);
  2339. state.effect_blur_shader.bind();
  2340. state.effect_blur_shader.set_uniform(EffectBlurShaderGLES2::PIXEL_SIZE, Vector2(1.0 / vp_w, 1.0 / vp_h));
  2341. state.effect_blur_shader.set_uniform(EffectBlurShaderGLES2::LOD, storage->frame.current_rt->mip_maps[0].color ? float(i) : 0.0);
  2342. state.effect_blur_shader.set_uniform(EffectBlurShaderGLES2::GLOW_STRENGTH, env->glow_strength);
  2343. glActiveTexture(GL_TEXTURE0);
  2344. if (storage->frame.current_rt->mip_maps[0].color) {
  2345. glBindTexture(GL_TEXTURE_2D, storage->frame.current_rt->mip_maps[1].color);
  2346. } else {
  2347. glBindTexture(GL_TEXTURE_2D, storage->frame.current_rt->mip_maps[1].sizes[i].color);
  2348. }
  2349. glBindFramebuffer(GL_FRAMEBUFFER, storage->frame.current_rt->mip_maps[0].sizes[i + 1].fbo); //next level, since mipmaps[0] starts one level bigger
  2350. storage->_copy_screen();
  2351. state.effect_blur_shader.set_conditional(EffectBlurShaderGLES2::GLOW_GAUSSIAN_VERTICAL, false);
  2352. }
  2353. glViewport(0, 0, storage->frame.current_rt->width, storage->frame.current_rt->height);
  2354. }
  2355. if (storage->frame.current_rt->external.fbo != 0) {
  2356. glBindFramebuffer(GL_FRAMEBUFFER, storage->frame.current_rt->external.fbo);
  2357. } else {
  2358. glBindFramebuffer(GL_FRAMEBUFFER, storage->frame.current_rt->fbo);
  2359. }
  2360. glActiveTexture(GL_TEXTURE0);
  2361. if (storage->frame.current_rt->mip_maps[0].color) {
  2362. glBindTexture(GL_TEXTURE_2D, storage->frame.current_rt->mip_maps[0].color);
  2363. } else {
  2364. glBindTexture(GL_TEXTURE_2D, storage->frame.current_rt->mip_maps[0].sizes[0].color);
  2365. }
  2366. state.tonemap_shader.set_conditional(TonemapShaderGLES2::USE_GLOW_FILTER_BICUBIC, env->glow_bicubic_upscale);
  2367. if (max_glow_level >= 0) {
  2368. if (storage->frame.current_rt->mip_maps[0].color) {
  2369. for (int i = 0; i < (max_glow_level + 1); i++) {
  2370. if (glow_mask & (1 << i)) {
  2371. if (i == 0) {
  2372. state.tonemap_shader.set_conditional(TonemapShaderGLES2::USE_GLOW_LEVEL1, true);
  2373. }
  2374. if (i == 1) {
  2375. state.tonemap_shader.set_conditional(TonemapShaderGLES2::USE_GLOW_LEVEL2, true);
  2376. }
  2377. if (i == 2) {
  2378. state.tonemap_shader.set_conditional(TonemapShaderGLES2::USE_GLOW_LEVEL3, true);
  2379. }
  2380. if (i == 3) {
  2381. state.tonemap_shader.set_conditional(TonemapShaderGLES2::USE_GLOW_LEVEL4, true);
  2382. }
  2383. if (i == 4) {
  2384. state.tonemap_shader.set_conditional(TonemapShaderGLES2::USE_GLOW_LEVEL5, true);
  2385. }
  2386. if (i == 5) {
  2387. state.tonemap_shader.set_conditional(TonemapShaderGLES2::USE_GLOW_LEVEL6, true);
  2388. }
  2389. if (i == 6) {
  2390. state.tonemap_shader.set_conditional(TonemapShaderGLES2::USE_GLOW_LEVEL7, true);
  2391. }
  2392. }
  2393. }
  2394. glActiveTexture(GL_TEXTURE1);
  2395. glBindTexture(GL_TEXTURE_2D, storage->frame.current_rt->mip_maps[0].color);
  2396. } else {
  2397. state.tonemap_shader.set_conditional(TonemapShaderGLES2::USE_MULTI_TEXTURE_GLOW, true);
  2398. int active_glow_level = 0;
  2399. for (int i = 0; i < (max_glow_level + 1); i++) {
  2400. if (glow_mask & (1 << i)) {
  2401. active_glow_level++;
  2402. glActiveTexture(GL_TEXTURE0 + active_glow_level);
  2403. glBindTexture(GL_TEXTURE_2D, storage->frame.current_rt->mip_maps[0].sizes[i + 1].color);
  2404. if (active_glow_level == 1) {
  2405. state.tonemap_shader.set_conditional(TonemapShaderGLES2::USE_GLOW_LEVEL1, true);
  2406. }
  2407. if (active_glow_level == 2) {
  2408. state.tonemap_shader.set_conditional(TonemapShaderGLES2::USE_GLOW_LEVEL2, true);
  2409. }
  2410. if (active_glow_level == 3) {
  2411. state.tonemap_shader.set_conditional(TonemapShaderGLES2::USE_GLOW_LEVEL3, true);
  2412. }
  2413. if (active_glow_level == 4) {
  2414. state.tonemap_shader.set_conditional(TonemapShaderGLES2::USE_GLOW_LEVEL4, true);
  2415. }
  2416. if (active_glow_level == 5) {
  2417. state.tonemap_shader.set_conditional(TonemapShaderGLES2::USE_GLOW_LEVEL5, true);
  2418. }
  2419. if (active_glow_level == 6) {
  2420. state.tonemap_shader.set_conditional(TonemapShaderGLES2::USE_GLOW_LEVEL6, true);
  2421. }
  2422. if (active_glow_level == 7) {
  2423. state.tonemap_shader.set_conditional(TonemapShaderGLES2::USE_GLOW_LEVEL7, true);
  2424. }
  2425. }
  2426. }
  2427. }
  2428. state.tonemap_shader.set_conditional(TonemapShaderGLES2::USE_GLOW_SCREEN, env->glow_blend_mode == RS::ENV_GLOW_BLEND_MODE_SCREEN);
  2429. state.tonemap_shader.set_conditional(TonemapShaderGLES2::USE_GLOW_SOFTLIGHT, env->glow_blend_mode == RS::ENV_GLOW_BLEND_MODE_SOFTLIGHT);
  2430. state.tonemap_shader.set_conditional(TonemapShaderGLES2::USE_GLOW_REPLACE, env->glow_blend_mode == RS::ENV_GLOW_BLEND_MODE_REPLACE);
  2431. }
  2432. //Adjustments
  2433. if (env->adjustments_enabled) {
  2434. state.tonemap_shader.set_conditional(TonemapShaderGLES2::USE_BCS, true);
  2435. RasterizerStorageGLES2::Texture *tex = storage->texture_owner.getornull(env->color_correction);
  2436. if (tex) {
  2437. state.tonemap_shader.set_conditional(TonemapShaderGLES2::USE_COLOR_CORRECTION, true);
  2438. glActiveTexture(GL_TEXTURE2);
  2439. glBindTexture(tex->target, tex->tex_id);
  2440. }
  2441. }
  2442. state.tonemap_shader.bind();
  2443. if (max_glow_level >= 0) {
  2444. state.tonemap_shader.set_uniform(TonemapShaderGLES2::GLOW_INTENSITY, env->glow_intensity);
  2445. int ss[2] = {
  2446. storage->frame.current_rt->width,
  2447. storage->frame.current_rt->height,
  2448. };
  2449. glUniform2iv(state.tonemap_shader.get_uniform(TonemapShaderGLES2::GLOW_TEXTURE_SIZE), 1, ss);
  2450. }
  2451. if (env->adjustments_enabled) {
  2452. state.tonemap_shader.set_uniform(TonemapShaderGLES2::BCS, Vector3(env->adjustments_brightness, env->adjustments_contrast, env->adjustments_saturation));
  2453. }
  2454. storage->_copy_screen();
  2455. //turn off everything used
  2456. state.tonemap_shader.set_conditional(TonemapShaderGLES2::USE_GLOW_LEVEL1, false);
  2457. state.tonemap_shader.set_conditional(TonemapShaderGLES2::USE_GLOW_LEVEL2, false);
  2458. state.tonemap_shader.set_conditional(TonemapShaderGLES2::USE_GLOW_LEVEL3, false);
  2459. state.tonemap_shader.set_conditional(TonemapShaderGLES2::USE_GLOW_LEVEL4, false);
  2460. state.tonemap_shader.set_conditional(TonemapShaderGLES2::USE_GLOW_LEVEL5, false);
  2461. state.tonemap_shader.set_conditional(TonemapShaderGLES2::USE_GLOW_LEVEL6, false);
  2462. state.tonemap_shader.set_conditional(TonemapShaderGLES2::USE_GLOW_LEVEL7, false);
  2463. state.tonemap_shader.set_conditional(TonemapShaderGLES2::USE_GLOW_REPLACE, false);
  2464. state.tonemap_shader.set_conditional(TonemapShaderGLES2::USE_GLOW_SCREEN, false);
  2465. state.tonemap_shader.set_conditional(TonemapShaderGLES2::USE_GLOW_SOFTLIGHT, false);
  2466. state.tonemap_shader.set_conditional(TonemapShaderGLES2::USE_GLOW_FILTER_BICUBIC, false);
  2467. state.tonemap_shader.set_conditional(TonemapShaderGLES2::USE_MULTI_TEXTURE_GLOW, false);
  2468. state.tonemap_shader.set_conditional(TonemapShaderGLES2::USE_BCS, false);
  2469. state.tonemap_shader.set_conditional(TonemapShaderGLES2::USE_COLOR_CORRECTION, false);
  2470. }
  2471. void RasterizerSceneGLES2::render_scene(const Transform &p_cam_transform, const CameraMatrix &p_cam_projection, bool p_cam_ortogonal, InstanceBase **p_cull_result, int p_cull_count, RID *p_light_cull_result, int p_light_cull_count, RID *p_reflection_probe_cull_result, int p_reflection_probe_cull_count, RID p_environment, RID p_shadow_atlas, RID p_reflection_atlas, RID p_reflection_probe, int p_reflection_probe_pass) {
  2472. Transform cam_transform = p_cam_transform;
  2473. storage->info.render.object_count += p_cull_count;
  2474. GLuint current_fb = 0;
  2475. Environment *env = NULL;
  2476. int viewport_width, viewport_height;
  2477. int viewport_x = 0;
  2478. int viewport_y = 0;
  2479. bool probe_interior = false;
  2480. bool reverse_cull = false;
  2481. if (storage->frame.current_rt && storage->frame.current_rt->flags[RasterizerStorage::RENDER_TARGET_VFLIP]) {
  2482. cam_transform.basis.set_axis(1, -cam_transform.basis.get_axis(1));
  2483. reverse_cull = true;
  2484. }
  2485. if (p_reflection_probe.is_valid()) {
  2486. ReflectionProbeInstance *probe = reflection_probe_instance_owner.getornull(p_reflection_probe);
  2487. ERR_FAIL_COND(!probe);
  2488. state.render_no_shadows = !probe->probe_ptr->enable_shadows;
  2489. if (!probe->probe_ptr->interior) { //use env only if not interior
  2490. env = environment_owner.getornull(p_environment);
  2491. }
  2492. current_fb = probe->fbo[p_reflection_probe_pass];
  2493. viewport_width = probe->probe_ptr->resolution;
  2494. viewport_height = probe->probe_ptr->resolution;
  2495. probe_interior = probe->probe_ptr->interior;
  2496. } else {
  2497. state.render_no_shadows = false;
  2498. if (storage->frame.current_rt->multisample_active) {
  2499. current_fb = storage->frame.current_rt->multisample_fbo;
  2500. } else if (storage->frame.current_rt->external.fbo != 0) {
  2501. current_fb = storage->frame.current_rt->external.fbo;
  2502. } else {
  2503. current_fb = storage->frame.current_rt->fbo;
  2504. }
  2505. env = environment_owner.getornull(p_environment);
  2506. viewport_width = storage->frame.current_rt->width;
  2507. viewport_height = storage->frame.current_rt->height;
  2508. viewport_x = storage->frame.current_rt->x;
  2509. if (storage->frame.current_rt->flags[RasterizerStorage::RENDER_TARGET_DIRECT_TO_SCREEN]) {
  2510. viewport_y = DisplayServer::get_singleton()->window_get_size().height - viewport_height - storage->frame.current_rt->y;
  2511. } else {
  2512. viewport_y = storage->frame.current_rt->y;
  2513. }
  2514. }
  2515. state.used_screen_texture = false;
  2516. state.viewport_size.x = viewport_width;
  2517. state.viewport_size.y = viewport_height;
  2518. state.screen_pixel_size.x = 1.0 / viewport_width;
  2519. state.screen_pixel_size.y = 1.0 / viewport_height;
  2520. //push back the directional lights
  2521. if (p_light_cull_count) {
  2522. //hardcoded limit of 256 lights
  2523. render_light_instance_count = MIN(RenderList::MAX_LIGHTS, p_light_cull_count);
  2524. render_light_instances = (LightInstance **)alloca(sizeof(LightInstance *) * render_light_instance_count);
  2525. render_directional_lights = 0;
  2526. //doing this because directional lights are at the end, put them at the beginning
  2527. int index = 0;
  2528. for (int i = render_light_instance_count - 1; i >= 0; i--) {
  2529. RID light_rid = p_light_cull_result[i];
  2530. LightInstance *light = light_instance_owner.getornull(light_rid);
  2531. if (light->light_ptr->type == RS::LIGHT_DIRECTIONAL) {
  2532. render_directional_lights++;
  2533. //as going in reverse, directional lights are always first anyway
  2534. }
  2535. light->light_index = index;
  2536. render_light_instances[index] = light;
  2537. index++;
  2538. }
  2539. } else {
  2540. render_light_instances = NULL;
  2541. render_directional_lights = 0;
  2542. render_light_instance_count = 0;
  2543. }
  2544. if (p_reflection_probe_cull_count) {
  2545. reflection_probe_instances = (ReflectionProbeInstance **)alloca(sizeof(ReflectionProbeInstance *) * p_reflection_probe_cull_count);
  2546. reflection_probe_count = p_reflection_probe_cull_count;
  2547. for (int i = 0; i < p_reflection_probe_cull_count; i++) {
  2548. ReflectionProbeInstance *rpi = reflection_probe_instance_owner.getornull(p_reflection_probe_cull_result[i]);
  2549. ERR_CONTINUE(!rpi);
  2550. rpi->last_pass = render_pass + 1; //will be incremented later
  2551. rpi->index = i;
  2552. reflection_probe_instances[i] = rpi;
  2553. }
  2554. } else {
  2555. reflection_probe_instances = NULL;
  2556. reflection_probe_count = 0;
  2557. }
  2558. if (env && env->bg_mode == RS::ENV_BG_CANVAS) {
  2559. // If using canvas background, copy 2d to screen copy texture
  2560. // TODO: When GLES2 renders to current_rt->mip_maps[], this copy will no longer be needed
  2561. _copy_texture_to_buffer(storage->frame.current_rt->color, storage->frame.current_rt->copy_screen_effect.fbo);
  2562. }
  2563. // render list stuff
  2564. render_list.clear();
  2565. _fill_render_list(p_cull_result, p_cull_count, false, false);
  2566. // other stuff
  2567. glBindFramebuffer(GL_FRAMEBUFFER, current_fb);
  2568. glViewport(viewport_x, viewport_y, viewport_width, viewport_height);
  2569. if (storage->frame.current_rt && storage->frame.current_rt->flags[RasterizerStorage::RENDER_TARGET_DIRECT_TO_SCREEN]) {
  2570. glScissor(viewport_x, viewport_y, viewport_width, viewport_height);
  2571. glEnable(GL_SCISSOR_TEST);
  2572. }
  2573. glDepthFunc(GL_LEQUAL);
  2574. glDepthMask(GL_TRUE);
  2575. glClearDepth(1.0f);
  2576. glEnable(GL_DEPTH_TEST);
  2577. glClear(GL_DEPTH_BUFFER_BIT);
  2578. // clear color
  2579. Color clear_color(0, 0, 0, 1);
  2580. Ref<CameraFeed> feed;
  2581. if (storage->frame.current_rt && storage->frame.current_rt->flags[RasterizerStorage::RENDER_TARGET_TRANSPARENT]) {
  2582. clear_color = Color(0, 0, 0, 0);
  2583. storage->frame.clear_request = false;
  2584. } else if (!env || env->bg_mode == RS::ENV_BG_CLEAR_COLOR || env->bg_mode == RS::ENV_BG_SKY) {
  2585. if (storage->frame.clear_request) {
  2586. clear_color = storage->frame.clear_request_color;
  2587. storage->frame.clear_request = false;
  2588. }
  2589. } else if (env->bg_mode == RS::ENV_BG_CANVAS || env->bg_mode == RS::ENV_BG_COLOR || env->bg_mode == RS::ENV_BG_COLOR_SKY) {
  2590. clear_color = env->bg_color;
  2591. storage->frame.clear_request = false;
  2592. } else if (env->bg_mode == RS::ENV_BG_CAMERA_FEED) {
  2593. feed = CameraServer::get_singleton()->get_feed_by_id(env->camera_feed_id);
  2594. storage->frame.clear_request = false;
  2595. } else {
  2596. storage->frame.clear_request = false;
  2597. }
  2598. if (!env || env->bg_mode != RS::ENV_BG_KEEP) {
  2599. glClearColor(clear_color.r, clear_color.g, clear_color.b, clear_color.a);
  2600. glClear(GL_COLOR_BUFFER_BIT);
  2601. }
  2602. state.default_ambient = Color(clear_color.r, clear_color.g, clear_color.b, 1.0);
  2603. state.default_bg = Color(clear_color.r, clear_color.g, clear_color.b, 1.0);
  2604. if (storage->frame.current_rt && storage->frame.current_rt->flags[RasterizerStorage::RENDER_TARGET_DIRECT_TO_SCREEN]) {
  2605. glDisable(GL_SCISSOR_TEST);
  2606. }
  2607. glVertexAttrib4f(RS::ARRAY_COLOR, 1, 1, 1, 1);
  2608. glBlendEquation(GL_FUNC_ADD);
  2609. glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  2610. // render sky
  2611. RasterizerStorageGLES2::Sky *sky = NULL;
  2612. GLuint env_radiance_tex = 0;
  2613. if (env) {
  2614. switch (env->bg_mode) {
  2615. case RS::ENV_BG_COLOR_SKY:
  2616. case RS::ENV_BG_SKY: {
  2617. sky = storage->sky_owner.getornull(env->sky);
  2618. if (sky) {
  2619. env_radiance_tex = sky->radiance;
  2620. }
  2621. } break;
  2622. case RS::ENV_BG_CAMERA_FEED: {
  2623. if (feed.is_valid() && (feed->get_base_width() > 0) && (feed->get_base_height() > 0)) {
  2624. // copy our camera feed to our background
  2625. glDisable(GL_BLEND);
  2626. glDepthMask(GL_FALSE);
  2627. glDisable(GL_DEPTH_TEST);
  2628. glDisable(GL_CULL_FACE);
  2629. storage->shaders.copy.set_conditional(CopyShaderGLES2::USE_NO_ALPHA, true);
  2630. storage->shaders.copy.set_conditional(CopyShaderGLES2::USE_DISPLAY_TRANSFORM, true);
  2631. if (feed->get_datatype() == CameraFeed::FEED_RGB) {
  2632. RID camera_RGBA = feed->get_texture(CameraServer::FEED_RGBA_IMAGE);
  2633. RS::get_singleton()->texture_bind(camera_RGBA, 0);
  2634. } else if (feed->get_datatype() == CameraFeed::FEED_YCBCR) {
  2635. RID camera_YCbCr = feed->get_texture(CameraServer::FEED_YCBCR_IMAGE);
  2636. RS::get_singleton()->texture_bind(camera_YCbCr, 0);
  2637. storage->shaders.copy.set_conditional(CopyShaderGLES2::YCBCR_TO_RGB, true);
  2638. } else if (feed->get_datatype() == CameraFeed::FEED_YCBCR_SEP) {
  2639. RID camera_Y = feed->get_texture(CameraServer::FEED_Y_IMAGE);
  2640. RID camera_CbCr = feed->get_texture(CameraServer::FEED_CBCR_IMAGE);
  2641. RS::get_singleton()->texture_bind(camera_Y, 0);
  2642. RS::get_singleton()->texture_bind(camera_CbCr, 1);
  2643. storage->shaders.copy.set_conditional(CopyShaderGLES2::SEP_CBCR_TEXTURE, true);
  2644. storage->shaders.copy.set_conditional(CopyShaderGLES2::YCBCR_TO_RGB, true);
  2645. };
  2646. storage->shaders.copy.bind();
  2647. storage->shaders.copy.set_uniform(CopyShaderGLES2::DISPLAY_TRANSFORM, feed->get_transform());
  2648. storage->bind_quad_array();
  2649. glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
  2650. glDisableVertexAttribArray(RS::ARRAY_VERTEX);
  2651. glDisableVertexAttribArray(RS::ARRAY_TEX_UV);
  2652. glBindBuffer(GL_ARRAY_BUFFER, 0);
  2653. // turn off everything used
  2654. storage->shaders.copy.set_conditional(CopyShaderGLES2::SEP_CBCR_TEXTURE, false);
  2655. storage->shaders.copy.set_conditional(CopyShaderGLES2::YCBCR_TO_RGB, false);
  2656. storage->shaders.copy.set_conditional(CopyShaderGLES2::USE_NO_ALPHA, false);
  2657. storage->shaders.copy.set_conditional(CopyShaderGLES2::USE_DISPLAY_TRANSFORM, false);
  2658. //restore
  2659. glEnable(GL_BLEND);
  2660. glDepthMask(GL_TRUE);
  2661. glEnable(GL_DEPTH_TEST);
  2662. glEnable(GL_CULL_FACE);
  2663. } else {
  2664. // don't have a feed, just show greenscreen :)
  2665. clear_color = Color(0.0, 1.0, 0.0, 1.0);
  2666. }
  2667. } break;
  2668. case RS::ENV_BG_CANVAS: {
  2669. // use screen copy as background
  2670. _copy_texture_to_buffer(storage->frame.current_rt->copy_screen_effect.color, current_fb);
  2671. } break;
  2672. default: {
  2673. } break;
  2674. }
  2675. }
  2676. if (probe_interior) {
  2677. env_radiance_tex = 0; //do not use radiance texture on interiors
  2678. state.default_ambient = Color(0, 0, 0, 1); //black as default ambient for interior
  2679. state.default_bg = Color(0, 0, 0, 1); //black as default background for interior
  2680. }
  2681. // render opaque things first
  2682. render_list.sort_by_key(false);
  2683. _render_render_list(render_list.elements, render_list.element_count, cam_transform, p_cam_projection, p_shadow_atlas, env, env_radiance_tex, 0.0, 0.0, reverse_cull, false, false);
  2684. // then draw the sky after
  2685. if (env && env->bg_mode == RS::ENV_BG_SKY && (!storage->frame.current_rt || !storage->frame.current_rt->flags[RasterizerStorage::RENDER_TARGET_TRANSPARENT])) {
  2686. if (sky && sky->panorama.is_valid()) {
  2687. _draw_sky(sky, p_cam_projection, cam_transform, false, env->sky_custom_fov, env->bg_energy, env->sky_orientation);
  2688. }
  2689. }
  2690. if (storage->frame.current_rt && state.used_screen_texture) {
  2691. //copy screen texture
  2692. if (storage->frame.current_rt->multisample_active) {
  2693. // Resolve framebuffer to front buffer before copying
  2694. #ifdef GLES_OVER_GL
  2695. glBindFramebuffer(GL_READ_FRAMEBUFFER, storage->frame.current_rt->multisample_fbo);
  2696. glReadBuffer(GL_COLOR_ATTACHMENT0);
  2697. glBindFramebuffer(GL_DRAW_FRAMEBUFFER, storage->frame.current_rt->fbo);
  2698. glBlitFramebuffer(0, 0, storage->frame.current_rt->width, storage->frame.current_rt->height, 0, 0, storage->frame.current_rt->width, storage->frame.current_rt->height, GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT, GL_NEAREST);
  2699. glBindFramebuffer(GL_READ_FRAMEBUFFER, 0);
  2700. glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0);
  2701. #elif IPHONE_ENABLED
  2702. glBindFramebuffer(GL_READ_FRAMEBUFFER, storage->frame.current_rt->multisample_fbo);
  2703. glBindFramebuffer(GL_DRAW_FRAMEBUFFER, storage->frame.current_rt->fbo);
  2704. glResolveMultisampleFramebufferAPPLE();
  2705. glBindFramebuffer(GL_READ_FRAMEBUFFER, 0);
  2706. glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0);
  2707. #elif ANDROID_ENABLED
  2708. // In GLES2 AndroidBlit is not available, so just copy color texture manually
  2709. _copy_texture_to_buffer(storage->frame.current_rt->multisample_color, storage->frame.current_rt->fbo);
  2710. #endif
  2711. }
  2712. storage->canvas->_copy_screen(Rect2());
  2713. if (storage->frame.current_rt && storage->frame.current_rt->multisample_active) {
  2714. // Rebind the current framebuffer
  2715. glBindFramebuffer(GL_FRAMEBUFFER, current_fb);
  2716. glViewport(0, 0, viewport_width, viewport_height);
  2717. }
  2718. }
  2719. // alpha pass
  2720. glBlendEquation(GL_FUNC_ADD);
  2721. glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
  2722. render_list.sort_by_reverse_depth_and_priority(true);
  2723. _render_render_list(&render_list.elements[render_list.max_elements - render_list.alpha_element_count], render_list.alpha_element_count, cam_transform, p_cam_projection, p_shadow_atlas, env, env_radiance_tex, 0.0, 0.0, reverse_cull, true, false);
  2724. if (p_reflection_probe.is_valid()) {
  2725. // Rendering to a probe so no need for post_processing
  2726. return;
  2727. }
  2728. //post process
  2729. _post_process(env, p_cam_projection);
  2730. //#define GLES2_SHADOW_ATLAS_DEBUG_VIEW
  2731. #ifdef GLES2_SHADOW_ATLAS_DEBUG_VIEW
  2732. ShadowAtlas *shadow_atlas = shadow_atlas_owner.getornull(p_shadow_atlas);
  2733. if (shadow_atlas) {
  2734. glActiveTexture(GL_TEXTURE0);
  2735. glBindTexture(GL_TEXTURE_2D, shadow_atlas->depth);
  2736. glViewport(0, 0, storage->frame.current_rt->width / 4, storage->frame.current_rt->height / 4);
  2737. storage->shaders.copy.set_conditional(CopyShaderGLES2::USE_CUBEMAP, false);
  2738. storage->shaders.copy.set_conditional(CopyShaderGLES2::USE_COPY_SECTION, false);
  2739. storage->shaders.copy.set_conditional(CopyShaderGLES2::USE_CUSTOM_ALPHA, false);
  2740. storage->shaders.copy.set_conditional(CopyShaderGLES2::USE_MULTIPLIER, false);
  2741. storage->shaders.copy.set_conditional(CopyShaderGLES2::USE_PANORAMA, false);
  2742. storage->shaders.copy.bind();
  2743. storage->_copy_screen();
  2744. }
  2745. #endif
  2746. //#define GLES2_SHADOW_DIRECTIONAL_DEBUG_VIEW
  2747. #ifdef GLES2_SHADOW_DIRECTIONAL_DEBUG_VIEW
  2748. if (true) {
  2749. glActiveTexture(GL_TEXTURE0);
  2750. glBindTexture(GL_TEXTURE_2D, directional_shadow.depth);
  2751. glViewport(0, 0, storage->frame.current_rt->width / 4, storage->frame.current_rt->height / 4);
  2752. storage->shaders.copy.set_conditional(CopyShaderGLES2::USE_CUBEMAP, false);
  2753. storage->shaders.copy.set_conditional(CopyShaderGLES2::USE_COPY_SECTION, false);
  2754. storage->shaders.copy.set_conditional(CopyShaderGLES2::USE_CUSTOM_ALPHA, false);
  2755. storage->shaders.copy.set_conditional(CopyShaderGLES2::USE_MULTIPLIER, false);
  2756. storage->shaders.copy.set_conditional(CopyShaderGLES2::USE_PANORAMA, false);
  2757. storage->shaders.copy.bind();
  2758. storage->_copy_screen();
  2759. }
  2760. #endif
  2761. }
  2762. void RasterizerSceneGLES2::render_shadow(RID p_light, RID p_shadow_atlas, int p_pass, InstanceBase **p_cull_result, int p_cull_count) {
  2763. state.render_no_shadows = false;
  2764. LightInstance *light_instance = light_instance_owner.getornull(p_light);
  2765. ERR_FAIL_COND(!light_instance);
  2766. RasterizerStorageGLES2::Light *light = light_instance->light_ptr;
  2767. ERR_FAIL_COND(!light);
  2768. uint32_t x;
  2769. uint32_t y;
  2770. uint32_t width;
  2771. uint32_t height;
  2772. float zfar = 0;
  2773. bool flip_facing = false;
  2774. int custom_vp_size = 0;
  2775. GLuint fbo = 0;
  2776. state.shadow_is_dual_parabolloid = false;
  2777. state.dual_parbolloid_direction = 0.0;
  2778. int current_cubemap = -1;
  2779. float bias = 0;
  2780. float normal_bias = 0;
  2781. CameraMatrix light_projection;
  2782. Transform light_transform;
  2783. // TODO directional light
  2784. if (light->type == RS::LIGHT_DIRECTIONAL) {
  2785. // set pssm stuff
  2786. // TODO set this only when changed
  2787. light_instance->light_directional_index = directional_shadow.current_light;
  2788. light_instance->last_scene_shadow_pass = scene_pass;
  2789. directional_shadow.current_light++;
  2790. if (directional_shadow.light_count == 1) {
  2791. light_instance->directional_rect = Rect2(0, 0, directional_shadow.size, directional_shadow.size);
  2792. } else if (directional_shadow.light_count == 2) {
  2793. light_instance->directional_rect = Rect2(0, 0, directional_shadow.size, directional_shadow.size / 2);
  2794. if (light_instance->light_directional_index == 1) {
  2795. light_instance->directional_rect.position.x += light_instance->directional_rect.size.x;
  2796. }
  2797. } else { //3 and 4
  2798. light_instance->directional_rect = Rect2(0, 0, directional_shadow.size / 2, directional_shadow.size / 2);
  2799. if (light_instance->light_directional_index & 1) {
  2800. light_instance->directional_rect.position.x += light_instance->directional_rect.size.x;
  2801. }
  2802. if (light_instance->light_directional_index / 2) {
  2803. light_instance->directional_rect.position.y += light_instance->directional_rect.size.y;
  2804. }
  2805. }
  2806. light_projection = light_instance->shadow_transform[p_pass].camera;
  2807. light_transform = light_instance->shadow_transform[p_pass].transform;
  2808. x = light_instance->directional_rect.position.x;
  2809. y = light_instance->directional_rect.position.y;
  2810. width = light_instance->directional_rect.size.width;
  2811. height = light_instance->directional_rect.size.height;
  2812. if (light->directional_shadow_mode == RS::LIGHT_DIRECTIONAL_SHADOW_PARALLEL_4_SPLITS) {
  2813. width /= 2;
  2814. height /= 2;
  2815. if (p_pass == 1) {
  2816. x += width;
  2817. } else if (p_pass == 2) {
  2818. y += height;
  2819. } else if (p_pass == 3) {
  2820. x += width;
  2821. y += height;
  2822. }
  2823. } else if (light->directional_shadow_mode == RS::LIGHT_DIRECTIONAL_SHADOW_PARALLEL_2_SPLITS) {
  2824. height /= 2;
  2825. if (p_pass == 0) {
  2826. } else {
  2827. y += height;
  2828. }
  2829. }
  2830. float bias_mult = Math::lerp(1.0f, light_instance->shadow_transform[p_pass].bias_scale, light->param[RS::LIGHT_PARAM_SHADOW_BIAS_SPLIT_SCALE]);
  2831. zfar = light->param[RS::LIGHT_PARAM_RANGE];
  2832. bias = light->param[RS::LIGHT_PARAM_SHADOW_BIAS] * bias_mult;
  2833. normal_bias = light->param[RS::LIGHT_PARAM_SHADOW_NORMAL_BIAS] * bias_mult;
  2834. fbo = directional_shadow.fbo;
  2835. } else {
  2836. ShadowAtlas *shadow_atlas = shadow_atlas_owner.getornull(p_shadow_atlas);
  2837. ERR_FAIL_COND(!shadow_atlas);
  2838. ERR_FAIL_COND(!shadow_atlas->shadow_owners.has(p_light));
  2839. fbo = shadow_atlas->fbo;
  2840. uint32_t key = shadow_atlas->shadow_owners[p_light];
  2841. uint32_t quadrant = (key >> ShadowAtlas::QUADRANT_SHIFT) & 0x03;
  2842. uint32_t shadow = key & ShadowAtlas::SHADOW_INDEX_MASK;
  2843. ERR_FAIL_INDEX((int)shadow, shadow_atlas->quadrants[quadrant].shadows.size());
  2844. uint32_t quadrant_size = shadow_atlas->size >> 1;
  2845. x = (quadrant & 1) * quadrant_size;
  2846. y = (quadrant >> 1) * quadrant_size;
  2847. uint32_t shadow_size = (quadrant_size / shadow_atlas->quadrants[quadrant].subdivision);
  2848. x += (shadow % shadow_atlas->quadrants[quadrant].subdivision) * shadow_size;
  2849. y += (shadow / shadow_atlas->quadrants[quadrant].subdivision) * shadow_size;
  2850. width = shadow_size;
  2851. height = shadow_size;
  2852. if (light->type == RS::LIGHT_OMNI) {
  2853. // cubemap only
  2854. if (light->omni_shadow_mode == RS::LIGHT_OMNI_SHADOW_CUBE && storage->config.support_shadow_cubemaps) {
  2855. int cubemap_index = shadow_cubemaps.size() - 1;
  2856. // find an appropriate cubemap to render to
  2857. for (int i = shadow_cubemaps.size() - 1; i >= 0; i--) {
  2858. if (shadow_cubemaps[i].size > shadow_size * 2) {
  2859. break;
  2860. }
  2861. cubemap_index = i;
  2862. }
  2863. fbo = shadow_cubemaps[cubemap_index].fbo[p_pass];
  2864. light_projection = light_instance->shadow_transform[0].camera;
  2865. light_transform = light_instance->shadow_transform[0].transform;
  2866. custom_vp_size = shadow_cubemaps[cubemap_index].size;
  2867. zfar = light->param[RS::LIGHT_PARAM_RANGE];
  2868. current_cubemap = cubemap_index;
  2869. } else {
  2870. //dual parabolloid
  2871. state.shadow_is_dual_parabolloid = true;
  2872. light_projection = light_instance->shadow_transform[0].camera;
  2873. light_transform = light_instance->shadow_transform[0].transform;
  2874. if (light->omni_shadow_detail == RS::LIGHT_OMNI_SHADOW_DETAIL_HORIZONTAL) {
  2875. height /= 2;
  2876. y += p_pass * height;
  2877. } else {
  2878. width /= 2;
  2879. x += p_pass * width;
  2880. }
  2881. state.dual_parbolloid_direction = p_pass == 0 ? 1.0 : -1.0;
  2882. flip_facing = (p_pass == 1);
  2883. zfar = light->param[RS::LIGHT_PARAM_RANGE];
  2884. bias = light->param[RS::LIGHT_PARAM_SHADOW_BIAS];
  2885. state.dual_parbolloid_zfar = zfar;
  2886. state.scene_shader.set_conditional(SceneShaderGLES2::RENDER_DEPTH_DUAL_PARABOLOID, true);
  2887. }
  2888. } else if (light->type == RS::LIGHT_SPOT) {
  2889. light_projection = light_instance->shadow_transform[0].camera;
  2890. light_transform = light_instance->shadow_transform[0].transform;
  2891. flip_facing = false;
  2892. zfar = light->param[RS::LIGHT_PARAM_RANGE];
  2893. bias = light->param[RS::LIGHT_PARAM_SHADOW_BIAS];
  2894. normal_bias = light->param[RS::LIGHT_PARAM_SHADOW_NORMAL_BIAS];
  2895. }
  2896. }
  2897. render_list.clear();
  2898. _fill_render_list(p_cull_result, p_cull_count, true, true);
  2899. render_list.sort_by_depth(false);
  2900. glDisable(GL_BLEND);
  2901. glDisable(GL_DITHER);
  2902. glEnable(GL_DEPTH_TEST);
  2903. glBindFramebuffer(GL_FRAMEBUFFER, fbo);
  2904. glDepthMask(GL_TRUE);
  2905. if (!storage->config.use_rgba_3d_shadows) {
  2906. glColorMask(0, 0, 0, 0);
  2907. }
  2908. if (custom_vp_size) {
  2909. glViewport(0, 0, custom_vp_size, custom_vp_size);
  2910. glScissor(0, 0, custom_vp_size, custom_vp_size);
  2911. } else {
  2912. glViewport(x, y, width, height);
  2913. glScissor(x, y, width, height);
  2914. }
  2915. glEnable(GL_SCISSOR_TEST);
  2916. glClearDepth(1.0f);
  2917. glClear(GL_DEPTH_BUFFER_BIT);
  2918. if (storage->config.use_rgba_3d_shadows) {
  2919. glClearColor(1.0, 1.0, 1.0, 1.0);
  2920. glClear(GL_COLOR_BUFFER_BIT);
  2921. }
  2922. glDisable(GL_SCISSOR_TEST);
  2923. if (light->reverse_cull) {
  2924. flip_facing = !flip_facing;
  2925. }
  2926. state.scene_shader.set_conditional(SceneShaderGLES2::RENDER_DEPTH, true);
  2927. _render_render_list(render_list.elements, render_list.element_count, light_transform, light_projection, RID(), NULL, 0, bias, normal_bias, flip_facing, false, true);
  2928. state.scene_shader.set_conditional(SceneShaderGLES2::RENDER_DEPTH, false);
  2929. state.scene_shader.set_conditional(SceneShaderGLES2::RENDER_DEPTH_DUAL_PARABOLOID, false);
  2930. // convert cubemap to dual paraboloid if needed
  2931. if (light->type == RS::LIGHT_OMNI && (light->omni_shadow_mode == RS::LIGHT_OMNI_SHADOW_CUBE && storage->config.support_shadow_cubemaps) && p_pass == 5) {
  2932. ShadowAtlas *shadow_atlas = shadow_atlas_owner.getornull(p_shadow_atlas);
  2933. glBindFramebuffer(GL_FRAMEBUFFER, shadow_atlas->fbo);
  2934. state.cube_to_dp_shader.bind();
  2935. glActiveTexture(GL_TEXTURE0);
  2936. glBindTexture(GL_TEXTURE_CUBE_MAP, shadow_cubemaps[current_cubemap].cubemap);
  2937. glDisable(GL_CULL_FACE);
  2938. for (int i = 0; i < 2; i++) {
  2939. state.cube_to_dp_shader.set_uniform(CubeToDpShaderGLES2::Z_FLIP, i == 1);
  2940. state.cube_to_dp_shader.set_uniform(CubeToDpShaderGLES2::Z_NEAR, light_projection.get_z_near());
  2941. state.cube_to_dp_shader.set_uniform(CubeToDpShaderGLES2::Z_FAR, light_projection.get_z_far());
  2942. state.cube_to_dp_shader.set_uniform(CubeToDpShaderGLES2::BIAS, light->param[RS::LIGHT_PARAM_SHADOW_BIAS]);
  2943. uint32_t local_width = width;
  2944. uint32_t local_height = height;
  2945. uint32_t local_x = x;
  2946. uint32_t local_y = y;
  2947. if (light->omni_shadow_detail == RS::LIGHT_OMNI_SHADOW_DETAIL_HORIZONTAL) {
  2948. local_height /= 2;
  2949. local_y += i * local_height;
  2950. } else {
  2951. local_width /= 2;
  2952. local_x += i * local_width;
  2953. }
  2954. glViewport(local_x, local_y, local_width, local_height);
  2955. glScissor(local_x, local_y, local_width, local_height);
  2956. glEnable(GL_SCISSOR_TEST);
  2957. glClearDepth(1.0f);
  2958. glClear(GL_DEPTH_BUFFER_BIT);
  2959. glDisable(GL_SCISSOR_TEST);
  2960. glDisable(GL_BLEND);
  2961. storage->_copy_screen();
  2962. }
  2963. }
  2964. if (storage->frame.current_rt) {
  2965. glViewport(0, 0, storage->frame.current_rt->width, storage->frame.current_rt->height);
  2966. }
  2967. if (!storage->config.use_rgba_3d_shadows) {
  2968. glColorMask(1, 1, 1, 1);
  2969. }
  2970. }
  2971. void RasterizerSceneGLES2::set_scene_pass(uint64_t p_pass) {
  2972. scene_pass = p_pass;
  2973. }
  2974. bool RasterizerSceneGLES2::free(RID p_rid) {
  2975. if (light_instance_owner.owns(p_rid)) {
  2976. LightInstance *light_instance = light_instance_owner.getornull(p_rid);
  2977. //remove from shadow atlases..
  2978. for (Set<RID>::Element *E = light_instance->shadow_atlases.front(); E; E = E->next()) {
  2979. ShadowAtlas *shadow_atlas = shadow_atlas_owner.getornull(E->get());
  2980. ERR_CONTINUE(!shadow_atlas->shadow_owners.has(p_rid));
  2981. uint32_t key = shadow_atlas->shadow_owners[p_rid];
  2982. uint32_t q = (key >> ShadowAtlas::QUADRANT_SHIFT) & 0x3;
  2983. uint32_t s = key & ShadowAtlas::SHADOW_INDEX_MASK;
  2984. shadow_atlas->quadrants[q].shadows.write[s].owner = RID();
  2985. shadow_atlas->shadow_owners.erase(p_rid);
  2986. }
  2987. light_instance_owner.free(p_rid);
  2988. memdelete(light_instance);
  2989. } else if (shadow_atlas_owner.owns(p_rid)) {
  2990. ShadowAtlas *shadow_atlas = shadow_atlas_owner.getornull(p_rid);
  2991. shadow_atlas_set_size(p_rid, 0);
  2992. shadow_atlas_owner.free(p_rid);
  2993. memdelete(shadow_atlas);
  2994. } else if (reflection_probe_instance_owner.owns(p_rid)) {
  2995. ReflectionProbeInstance *reflection_instance = reflection_probe_instance_owner.getornull(p_rid);
  2996. for (int i = 0; i < 6; i++) {
  2997. glDeleteFramebuffers(1, &reflection_instance->fbo[i]);
  2998. glDeleteTextures(1, &reflection_instance->color[i]);
  2999. }
  3000. if (reflection_instance->cubemap != 0) {
  3001. glDeleteTextures(1, &reflection_instance->cubemap);
  3002. }
  3003. glDeleteRenderbuffers(1, &reflection_instance->depth);
  3004. reflection_probe_release_atlas_index(p_rid);
  3005. reflection_probe_instance_owner.free(p_rid);
  3006. memdelete(reflection_instance);
  3007. } else {
  3008. return false;
  3009. }
  3010. return true;
  3011. }
  3012. void RasterizerSceneGLES2::set_debug_draw_mode(RS::ViewportDebugDraw p_debug_draw) {
  3013. }
  3014. void RasterizerSceneGLES2::initialize() {
  3015. state.scene_shader.init();
  3016. state.scene_shader.set_conditional(SceneShaderGLES2::USE_RGBA_SHADOWS, storage->config.use_rgba_3d_shadows);
  3017. state.cube_to_dp_shader.init();
  3018. state.effect_blur_shader.init();
  3019. state.tonemap_shader.init();
  3020. render_list.init();
  3021. render_pass = 1;
  3022. shadow_atlas_realloc_tolerance_msec = 500;
  3023. {
  3024. //default material and shader
  3025. default_shader = storage->shader_create();
  3026. storage->shader_set_code(default_shader, "shader_type spatial;\n");
  3027. default_material = storage->material_create();
  3028. storage->material_set_shader(default_material, default_shader);
  3029. default_shader_twosided = storage->shader_create();
  3030. default_material_twosided = storage->material_create();
  3031. storage->shader_set_code(default_shader_twosided, "shader_type spatial; render_mode cull_disabled;\n");
  3032. storage->material_set_shader(default_material_twosided, default_shader_twosided);
  3033. }
  3034. {
  3035. default_worldcoord_shader = storage->shader_create();
  3036. storage->shader_set_code(default_worldcoord_shader, "shader_type spatial; render_mode world_vertex_coords;\n");
  3037. default_worldcoord_material = storage->material_create();
  3038. storage->material_set_shader(default_worldcoord_material, default_worldcoord_shader);
  3039. default_worldcoord_shader_twosided = storage->shader_create();
  3040. default_worldcoord_material_twosided = storage->material_create();
  3041. storage->shader_set_code(default_worldcoord_shader_twosided, "shader_type spatial; render_mode cull_disabled,world_vertex_coords;\n");
  3042. storage->material_set_shader(default_worldcoord_material_twosided, default_worldcoord_shader_twosided);
  3043. }
  3044. {
  3045. //default material and shader
  3046. default_overdraw_shader = storage->shader_create();
  3047. storage->shader_set_code(default_overdraw_shader, "shader_type spatial;\nrender_mode blend_add,unshaded;\n void fragment() { ALBEDO=vec3(0.4,0.8,0.8); ALPHA=0.2; }");
  3048. default_overdraw_material = storage->material_create();
  3049. storage->material_set_shader(default_overdraw_material, default_overdraw_shader);
  3050. }
  3051. {
  3052. glGenBuffers(1, &state.sky_verts);
  3053. glBindBuffer(GL_ARRAY_BUFFER, state.sky_verts);
  3054. glBufferData(GL_ARRAY_BUFFER, sizeof(Vector3) * 8, NULL, GL_DYNAMIC_DRAW);
  3055. glBindBuffer(GL_ARRAY_BUFFER, 0);
  3056. }
  3057. {
  3058. uint32_t immediate_buffer_size = GLOBAL_DEF("rendering/limits/buffers/immediate_buffer_size_kb", 2048);
  3059. ProjectSettings::get_singleton()->set_custom_property_info("rendering/limits/buffers/immediate_buffer_size_kb", PropertyInfo(Variant::INT, "rendering/limits/buffers/immediate_buffer_size_kb", PROPERTY_HINT_RANGE, "0,8192,1,or_greater"));
  3060. glGenBuffers(1, &state.immediate_buffer);
  3061. glBindBuffer(GL_ARRAY_BUFFER, state.immediate_buffer);
  3062. glBufferData(GL_ARRAY_BUFFER, immediate_buffer_size * 1024, NULL, GL_DYNAMIC_DRAW);
  3063. glBindBuffer(GL_ARRAY_BUFFER, 0);
  3064. }
  3065. // cubemaps for shadows
  3066. if (storage->config.support_shadow_cubemaps) { //not going to be used
  3067. int max_shadow_cubemap_sampler_size = 512;
  3068. int cube_size = max_shadow_cubemap_sampler_size;
  3069. glActiveTexture(GL_TEXTURE0);
  3070. while (cube_size >= 32) {
  3071. ShadowCubeMap cube;
  3072. cube.size = cube_size;
  3073. glGenTextures(1, &cube.cubemap);
  3074. glBindTexture(GL_TEXTURE_CUBE_MAP, cube.cubemap);
  3075. for (int i = 0; i < 6; i++) {
  3076. glTexImage2D(_cube_side_enum[i], 0, storage->config.depth_internalformat, cube_size, cube_size, 0, GL_DEPTH_COMPONENT, storage->config.depth_type, NULL);
  3077. }
  3078. glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  3079. glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  3080. glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  3081. glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  3082. glGenFramebuffers(6, cube.fbo);
  3083. for (int i = 0; i < 6; i++) {
  3084. glBindFramebuffer(GL_FRAMEBUFFER, cube.fbo[i]);
  3085. glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, _cube_side_enum[i], cube.cubemap, 0);
  3086. }
  3087. shadow_cubemaps.push_back(cube);
  3088. cube_size >>= 1;
  3089. }
  3090. }
  3091. {
  3092. // directional shadows
  3093. directional_shadow.light_count = 0;
  3094. directional_shadow.size = next_power_of_2(GLOBAL_GET("rendering/quality/directional_shadow/size"));
  3095. glGenFramebuffers(1, &directional_shadow.fbo);
  3096. glBindFramebuffer(GL_FRAMEBUFFER, directional_shadow.fbo);
  3097. if (storage->config.use_rgba_3d_shadows) {
  3098. //maximum compatibility, renderbuffer and RGBA shadow
  3099. glGenRenderbuffers(1, &directional_shadow.depth);
  3100. glBindRenderbuffer(GL_RENDERBUFFER, directional_shadow.depth);
  3101. glRenderbufferStorage(GL_RENDERBUFFER, storage->config.depth_internalformat, directional_shadow.size, directional_shadow.size);
  3102. glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, directional_shadow.depth);
  3103. glGenTextures(1, &directional_shadow.color);
  3104. glBindTexture(GL_TEXTURE_2D, directional_shadow.color);
  3105. glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, directional_shadow.size, directional_shadow.size, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
  3106. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  3107. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  3108. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  3109. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  3110. glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, directional_shadow.color, 0);
  3111. } else {
  3112. //just a depth buffer
  3113. glGenTextures(1, &directional_shadow.depth);
  3114. glBindTexture(GL_TEXTURE_2D, directional_shadow.depth);
  3115. glTexImage2D(GL_TEXTURE_2D, 0, storage->config.depth_internalformat, directional_shadow.size, directional_shadow.size, 0, GL_DEPTH_COMPONENT, storage->config.depth_type, NULL);
  3116. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  3117. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  3118. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  3119. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  3120. glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, directional_shadow.depth, 0);
  3121. }
  3122. GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
  3123. if (status != GL_FRAMEBUFFER_COMPLETE) {
  3124. ERR_PRINT("Directional shadow framebuffer status invalid");
  3125. }
  3126. }
  3127. shadow_filter_mode = SHADOW_FILTER_NEAREST;
  3128. glFrontFace(GL_CW);
  3129. }
  3130. void RasterizerSceneGLES2::iteration() {
  3131. shadow_filter_mode = ShadowFilterMode(int(GLOBAL_GET("rendering/quality/shadows/filter_mode")));
  3132. }
  3133. void RasterizerSceneGLES2::finalize() {
  3134. }
  3135. RasterizerSceneGLES2::RasterizerSceneGLES2() {
  3136. }