rasterizer_scene_gles3.cpp 199 KB

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