rasterizer_scene_gles2.cpp 151 KB

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