rasterizer_scene_gles2.cpp 147 KB

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