rasterizer_scene_gles3.cpp 196 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207
  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. t = t->get_ptr(); //resolve for proxies
  892. if (t->redraw_if_visible) { //must check before proxy because this is often used with proxies
  893. VisualServerRaster::redraw_request();
  894. }
  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. // TODO
  947. } break;
  948. default: {}
  949. }
  950. }
  951. glBindTexture(target, tex);
  952. if (t && storage->config.srgb_decode_supported) {
  953. //if SRGB decode extension is present, simply switch the texture to whathever is needed
  954. bool must_srgb = false;
  955. if (t->srgb && (texture_hints[i] == ShaderLanguage::ShaderNode::Uniform::HINT_ALBEDO || texture_hints[i] == ShaderLanguage::ShaderNode::Uniform::HINT_BLACK_ALBEDO)) {
  956. must_srgb = true;
  957. }
  958. if (t->using_srgb != must_srgb) {
  959. if (must_srgb) {
  960. glTexParameteri(t->target, _TEXTURE_SRGB_DECODE_EXT, _DECODE_EXT);
  961. #ifdef TOOLS_ENABLED
  962. if (t->detect_srgb) {
  963. t->detect_srgb(t->detect_srgb_ud);
  964. }
  965. #endif
  966. } else {
  967. glTexParameteri(t->target, _TEXTURE_SRGB_DECODE_EXT, _SKIP_DECODE_EXT);
  968. }
  969. t->using_srgb = must_srgb;
  970. }
  971. }
  972. if (i == 0) {
  973. state.current_main_tex = tex;
  974. }
  975. }
  976. return rebind;
  977. }
  978. struct RasterizerGLES3Particle {
  979. float color[4];
  980. float velocity_active[4];
  981. float custom[4];
  982. float xform_1[4];
  983. float xform_2[4];
  984. float xform_3[4];
  985. };
  986. struct RasterizerGLES3ParticleSort {
  987. Vector3 z_dir;
  988. bool operator()(const RasterizerGLES3Particle &p_a, const RasterizerGLES3Particle &p_b) const {
  989. 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]));
  990. }
  991. };
  992. void RasterizerSceneGLES3::_setup_geometry(RenderList::Element *e, const Transform &p_view_transform) {
  993. switch (e->instance->base_type) {
  994. case VS::INSTANCE_MESH: {
  995. RasterizerStorageGLES3::Surface *s = static_cast<RasterizerStorageGLES3::Surface *>(e->geometry);
  996. if (s->blend_shapes.size() && e->instance->blend_values.size()) {
  997. //blend shapes, use transform feedback
  998. storage->mesh_render_blend_shapes(s, e->instance->blend_values.ptr());
  999. //rebind shader
  1000. state.scene_shader.bind();
  1001. #ifdef DEBUG_ENABLED
  1002. } else if (state.debug_draw == VS::VIEWPORT_DEBUG_DRAW_WIREFRAME && s->array_wireframe_id) {
  1003. glBindVertexArray(s->array_wireframe_id); // everything is so easy nowadays
  1004. #endif
  1005. } else {
  1006. glBindVertexArray(s->array_id); // everything is so easy nowadays
  1007. }
  1008. } break;
  1009. case VS::INSTANCE_MULTIMESH: {
  1010. RasterizerStorageGLES3::MultiMesh *multi_mesh = static_cast<RasterizerStorageGLES3::MultiMesh *>(e->owner);
  1011. RasterizerStorageGLES3::Surface *s = static_cast<RasterizerStorageGLES3::Surface *>(e->geometry);
  1012. #ifdef DEBUG_ENABLED
  1013. if (state.debug_draw == VS::VIEWPORT_DEBUG_DRAW_WIREFRAME && s->instancing_array_wireframe_id) {
  1014. glBindVertexArray(s->instancing_array_wireframe_id); // use the instancing array ID
  1015. } else
  1016. #endif
  1017. {
  1018. glBindVertexArray(s->instancing_array_id); // use the instancing array ID
  1019. }
  1020. glBindBuffer(GL_ARRAY_BUFFER, multi_mesh->buffer); //modify the buffer
  1021. int stride = (multi_mesh->xform_floats + multi_mesh->color_floats + multi_mesh->custom_data_floats) * 4;
  1022. glEnableVertexAttribArray(8);
  1023. glVertexAttribPointer(8, 4, GL_FLOAT, GL_FALSE, stride, ((uint8_t *)NULL) + 0);
  1024. glVertexAttribDivisor(8, 1);
  1025. glEnableVertexAttribArray(9);
  1026. glVertexAttribPointer(9, 4, GL_FLOAT, GL_FALSE, stride, ((uint8_t *)NULL) + 4 * 4);
  1027. glVertexAttribDivisor(9, 1);
  1028. int color_ofs;
  1029. if (multi_mesh->transform_format == VS::MULTIMESH_TRANSFORM_3D) {
  1030. glEnableVertexAttribArray(10);
  1031. glVertexAttribPointer(10, 4, GL_FLOAT, GL_FALSE, stride, ((uint8_t *)NULL) + 8 * 4);
  1032. glVertexAttribDivisor(10, 1);
  1033. color_ofs = 12 * 4;
  1034. } else {
  1035. glDisableVertexAttribArray(10);
  1036. glVertexAttrib4f(10, 0, 0, 1, 0);
  1037. color_ofs = 8 * 4;
  1038. }
  1039. int custom_data_ofs = color_ofs;
  1040. switch (multi_mesh->color_format) {
  1041. case VS::MULTIMESH_COLOR_NONE: {
  1042. glDisableVertexAttribArray(11);
  1043. glVertexAttrib4f(11, 1, 1, 1, 1);
  1044. } break;
  1045. case VS::MULTIMESH_COLOR_8BIT: {
  1046. glEnableVertexAttribArray(11);
  1047. glVertexAttribPointer(11, 4, GL_UNSIGNED_BYTE, GL_TRUE, stride, ((uint8_t *)NULL) + color_ofs);
  1048. glVertexAttribDivisor(11, 1);
  1049. custom_data_ofs += 4;
  1050. } break;
  1051. case VS::MULTIMESH_COLOR_FLOAT: {
  1052. glEnableVertexAttribArray(11);
  1053. glVertexAttribPointer(11, 4, GL_FLOAT, GL_FALSE, stride, ((uint8_t *)NULL) + color_ofs);
  1054. glVertexAttribDivisor(11, 1);
  1055. custom_data_ofs += 4 * 4;
  1056. } break;
  1057. }
  1058. switch (multi_mesh->custom_data_format) {
  1059. case VS::MULTIMESH_CUSTOM_DATA_NONE: {
  1060. glDisableVertexAttribArray(12);
  1061. glVertexAttrib4f(12, 1, 1, 1, 1);
  1062. } break;
  1063. case VS::MULTIMESH_CUSTOM_DATA_8BIT: {
  1064. glEnableVertexAttribArray(12);
  1065. glVertexAttribPointer(12, 4, GL_UNSIGNED_BYTE, GL_TRUE, stride, ((uint8_t *)NULL) + custom_data_ofs);
  1066. glVertexAttribDivisor(12, 1);
  1067. } break;
  1068. case VS::MULTIMESH_CUSTOM_DATA_FLOAT: {
  1069. glEnableVertexAttribArray(12);
  1070. glVertexAttribPointer(12, 4, GL_FLOAT, GL_FALSE, stride, ((uint8_t *)NULL) + custom_data_ofs);
  1071. glVertexAttribDivisor(12, 1);
  1072. } break;
  1073. }
  1074. } break;
  1075. case VS::INSTANCE_PARTICLES: {
  1076. RasterizerStorageGLES3::Particles *particles = static_cast<RasterizerStorageGLES3::Particles *>(e->owner);
  1077. RasterizerStorageGLES3::Surface *s = static_cast<RasterizerStorageGLES3::Surface *>(e->geometry);
  1078. if (particles->draw_order == VS::PARTICLES_DRAW_ORDER_VIEW_DEPTH && particles->particle_valid_histories[1]) {
  1079. 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
  1080. RasterizerGLES3Particle *particle_array;
  1081. #ifndef __EMSCRIPTEN__
  1082. particle_array = static_cast<RasterizerGLES3Particle *>(glMapBufferRange(GL_ARRAY_BUFFER, 0, particles->amount * 24 * sizeof(float), GL_MAP_READ_BIT | GL_MAP_WRITE_BIT));
  1083. #else
  1084. PoolVector<RasterizerGLES3Particle> particle_vector;
  1085. particle_vector.resize(particles->amount);
  1086. PoolVector<RasterizerGLES3Particle>::Write w = particle_vector.write();
  1087. particle_array = w.ptr();
  1088. glGetBufferSubData(GL_ARRAY_BUFFER, 0, particles->amount * sizeof(RasterizerGLES3Particle), particle_array);
  1089. #endif
  1090. SortArray<RasterizerGLES3Particle, RasterizerGLES3ParticleSort> sorter;
  1091. if (particles->use_local_coords) {
  1092. sorter.compare.z_dir = e->instance->transform.affine_inverse().xform(p_view_transform.basis.get_axis(2)).normalized();
  1093. } else {
  1094. sorter.compare.z_dir = p_view_transform.basis.get_axis(2).normalized();
  1095. }
  1096. sorter.sort(particle_array, particles->amount);
  1097. #ifndef __EMSCRIPTEN__
  1098. glUnmapBuffer(GL_ARRAY_BUFFER);
  1099. #else
  1100. w = PoolVector<RasterizerGLES3Particle>::Write();
  1101. particle_array = NULL;
  1102. {
  1103. PoolVector<RasterizerGLES3Particle>::Read r = particle_vector.read();
  1104. glBufferSubData(GL_ARRAY_BUFFER, 0, particles->amount * sizeof(RasterizerGLES3Particle), r.ptr());
  1105. }
  1106. particle_vector = PoolVector<RasterizerGLES3Particle>();
  1107. #endif
  1108. #ifdef DEBUG_ENABLED
  1109. if (state.debug_draw == VS::VIEWPORT_DEBUG_DRAW_WIREFRAME && s->instancing_array_wireframe_id) {
  1110. glBindVertexArray(s->instancing_array_wireframe_id); // use the wireframe instancing array ID
  1111. } else
  1112. #endif
  1113. {
  1114. glBindVertexArray(s->instancing_array_id); // use the instancing array ID
  1115. }
  1116. glBindBuffer(GL_ARRAY_BUFFER, particles->particle_buffer_histories[1]); //modify the buffer
  1117. } else {
  1118. #ifdef DEBUG_ENABLED
  1119. if (state.debug_draw == VS::VIEWPORT_DEBUG_DRAW_WIREFRAME && s->instancing_array_wireframe_id) {
  1120. glBindVertexArray(s->instancing_array_wireframe_id); // use the wireframe instancing array ID
  1121. } else
  1122. #endif
  1123. {
  1124. glBindVertexArray(s->instancing_array_id); // use the instancing array ID
  1125. }
  1126. glBindBuffer(GL_ARRAY_BUFFER, particles->particle_buffers[0]); //modify the buffer
  1127. }
  1128. int stride = sizeof(float) * 4 * 6;
  1129. //transform
  1130. if (particles->draw_order != VS::PARTICLES_DRAW_ORDER_LIFETIME) {
  1131. glEnableVertexAttribArray(8); //xform x
  1132. glVertexAttribPointer(8, 4, GL_FLOAT, GL_FALSE, stride, ((uint8_t *)NULL) + sizeof(float) * 4 * 3);
  1133. glVertexAttribDivisor(8, 1);
  1134. glEnableVertexAttribArray(9); //xform y
  1135. glVertexAttribPointer(9, 4, GL_FLOAT, GL_FALSE, stride, ((uint8_t *)NULL) + sizeof(float) * 4 * 4);
  1136. glVertexAttribDivisor(9, 1);
  1137. glEnableVertexAttribArray(10); //xform z
  1138. glVertexAttribPointer(10, 4, GL_FLOAT, GL_FALSE, stride, ((uint8_t *)NULL) + sizeof(float) * 4 * 5);
  1139. glVertexAttribDivisor(10, 1);
  1140. glEnableVertexAttribArray(11); //color
  1141. glVertexAttribPointer(11, 4, GL_FLOAT, GL_FALSE, stride, ((uint8_t *)NULL) + 0);
  1142. glVertexAttribDivisor(11, 1);
  1143. glEnableVertexAttribArray(12); //custom
  1144. glVertexAttribPointer(12, 4, GL_FLOAT, GL_FALSE, stride, ((uint8_t *)NULL) + sizeof(float) * 4 * 2);
  1145. glVertexAttribDivisor(12, 1);
  1146. }
  1147. } break;
  1148. default: {}
  1149. }
  1150. }
  1151. static const GLenum gl_primitive[] = {
  1152. GL_POINTS,
  1153. GL_LINES,
  1154. GL_LINE_STRIP,
  1155. GL_LINE_LOOP,
  1156. GL_TRIANGLES,
  1157. GL_TRIANGLE_STRIP,
  1158. GL_TRIANGLE_FAN
  1159. };
  1160. void RasterizerSceneGLES3::_render_geometry(RenderList::Element *e) {
  1161. switch (e->instance->base_type) {
  1162. case VS::INSTANCE_MESH: {
  1163. RasterizerStorageGLES3::Surface *s = static_cast<RasterizerStorageGLES3::Surface *>(e->geometry);
  1164. #ifdef DEBUG_ENABLED
  1165. if (state.debug_draw == VS::VIEWPORT_DEBUG_DRAW_WIREFRAME && s->array_wireframe_id) {
  1166. glDrawElements(GL_LINES, s->index_wireframe_len, GL_UNSIGNED_INT, 0);
  1167. storage->info.render.vertices_count += s->index_array_len;
  1168. } else
  1169. #endif
  1170. if (s->index_array_len > 0) {
  1171. glDrawElements(gl_primitive[s->primitive], s->index_array_len, (s->array_len >= (1 << 16)) ? GL_UNSIGNED_INT : GL_UNSIGNED_SHORT, 0);
  1172. storage->info.render.vertices_count += s->index_array_len;
  1173. } else {
  1174. glDrawArrays(gl_primitive[s->primitive], 0, s->array_len);
  1175. storage->info.render.vertices_count += s->array_len;
  1176. }
  1177. } break;
  1178. case VS::INSTANCE_MULTIMESH: {
  1179. RasterizerStorageGLES3::MultiMesh *multi_mesh = static_cast<RasterizerStorageGLES3::MultiMesh *>(e->owner);
  1180. RasterizerStorageGLES3::Surface *s = static_cast<RasterizerStorageGLES3::Surface *>(e->geometry);
  1181. int amount = MIN(multi_mesh->size, multi_mesh->visible_instances);
  1182. if (amount == -1) {
  1183. amount = multi_mesh->size;
  1184. }
  1185. #ifdef DEBUG_ENABLED
  1186. if (state.debug_draw == VS::VIEWPORT_DEBUG_DRAW_WIREFRAME && s->array_wireframe_id) {
  1187. glDrawElementsInstanced(GL_LINES, s->index_wireframe_len, GL_UNSIGNED_INT, 0, amount);
  1188. storage->info.render.vertices_count += s->index_array_len * amount;
  1189. } else
  1190. #endif
  1191. if (s->index_array_len > 0) {
  1192. glDrawElementsInstanced(gl_primitive[s->primitive], s->index_array_len, (s->array_len >= (1 << 16)) ? GL_UNSIGNED_INT : GL_UNSIGNED_SHORT, 0, amount);
  1193. storage->info.render.vertices_count += s->index_array_len * amount;
  1194. } else {
  1195. glDrawArraysInstanced(gl_primitive[s->primitive], 0, s->array_len, amount);
  1196. storage->info.render.vertices_count += s->array_len * amount;
  1197. }
  1198. } break;
  1199. case VS::INSTANCE_IMMEDIATE: {
  1200. bool restore_tex = false;
  1201. const RasterizerStorageGLES3::Immediate *im = static_cast<const RasterizerStorageGLES3::Immediate *>(e->geometry);
  1202. if (im->building) {
  1203. return;
  1204. }
  1205. glBindBuffer(GL_ARRAY_BUFFER, state.immediate_buffer);
  1206. glBindVertexArray(state.immediate_array);
  1207. for (const List<RasterizerStorageGLES3::Immediate::Chunk>::Element *E = im->chunks.front(); E; E = E->next()) {
  1208. const RasterizerStorageGLES3::Immediate::Chunk &c = E->get();
  1209. if (c.vertices.empty()) {
  1210. continue;
  1211. }
  1212. int vertices = c.vertices.size();
  1213. uint32_t buf_ofs = 0;
  1214. storage->info.render.vertices_count += vertices;
  1215. if (c.texture.is_valid() && storage->texture_owner.owns(c.texture)) {
  1216. RasterizerStorageGLES3::Texture *t = storage->texture_owner.get(c.texture);
  1217. t = t->get_ptr(); //resolve for proxies
  1218. if (t->redraw_if_visible) {
  1219. VisualServerRaster::redraw_request();
  1220. }
  1221. #ifdef TOOLS_ENABLED
  1222. if (t->detect_3d) {
  1223. t->detect_3d(t->detect_3d_ud);
  1224. }
  1225. #endif
  1226. if (t->render_target) {
  1227. t->render_target->used_in_frame = true;
  1228. }
  1229. glActiveTexture(GL_TEXTURE0);
  1230. glBindTexture(t->target, t->tex_id);
  1231. restore_tex = true;
  1232. } else if (restore_tex) {
  1233. glActiveTexture(GL_TEXTURE0);
  1234. glBindTexture(GL_TEXTURE_2D, state.current_main_tex);
  1235. restore_tex = false;
  1236. }
  1237. if (!c.normals.empty()) {
  1238. glEnableVertexAttribArray(VS::ARRAY_NORMAL);
  1239. glBufferSubData(GL_ARRAY_BUFFER, buf_ofs, sizeof(Vector3) * vertices, c.normals.ptr());
  1240. glVertexAttribPointer(VS::ARRAY_NORMAL, 3, GL_FLOAT, false, sizeof(Vector3), ((uint8_t *)NULL) + buf_ofs);
  1241. buf_ofs += sizeof(Vector3) * vertices;
  1242. } else {
  1243. glDisableVertexAttribArray(VS::ARRAY_NORMAL);
  1244. }
  1245. if (!c.tangents.empty()) {
  1246. glEnableVertexAttribArray(VS::ARRAY_TANGENT);
  1247. glBufferSubData(GL_ARRAY_BUFFER, buf_ofs, sizeof(Plane) * vertices, c.tangents.ptr());
  1248. glVertexAttribPointer(VS::ARRAY_TANGENT, 4, GL_FLOAT, false, sizeof(Plane), ((uint8_t *)NULL) + buf_ofs);
  1249. buf_ofs += sizeof(Plane) * vertices;
  1250. } else {
  1251. glDisableVertexAttribArray(VS::ARRAY_TANGENT);
  1252. }
  1253. if (!c.colors.empty()) {
  1254. glEnableVertexAttribArray(VS::ARRAY_COLOR);
  1255. glBufferSubData(GL_ARRAY_BUFFER, buf_ofs, sizeof(Color) * vertices, c.colors.ptr());
  1256. glVertexAttribPointer(VS::ARRAY_COLOR, 4, GL_FLOAT, false, sizeof(Color), ((uint8_t *)NULL) + buf_ofs);
  1257. buf_ofs += sizeof(Color) * vertices;
  1258. } else {
  1259. glDisableVertexAttribArray(VS::ARRAY_COLOR);
  1260. glVertexAttrib4f(VS::ARRAY_COLOR, 1, 1, 1, 1);
  1261. }
  1262. if (!c.uvs.empty()) {
  1263. glEnableVertexAttribArray(VS::ARRAY_TEX_UV);
  1264. glBufferSubData(GL_ARRAY_BUFFER, buf_ofs, sizeof(Vector2) * vertices, c.uvs.ptr());
  1265. glVertexAttribPointer(VS::ARRAY_TEX_UV, 2, GL_FLOAT, false, sizeof(Vector2), ((uint8_t *)NULL) + buf_ofs);
  1266. buf_ofs += sizeof(Vector2) * vertices;
  1267. } else {
  1268. glDisableVertexAttribArray(VS::ARRAY_TEX_UV);
  1269. }
  1270. if (!c.uvs2.empty()) {
  1271. glEnableVertexAttribArray(VS::ARRAY_TEX_UV2);
  1272. glBufferSubData(GL_ARRAY_BUFFER, buf_ofs, sizeof(Vector2) * vertices, c.uvs2.ptr());
  1273. glVertexAttribPointer(VS::ARRAY_TEX_UV2, 2, GL_FLOAT, false, sizeof(Vector2), ((uint8_t *)NULL) + buf_ofs);
  1274. buf_ofs += sizeof(Vector2) * vertices;
  1275. } else {
  1276. glDisableVertexAttribArray(VS::ARRAY_TEX_UV2);
  1277. }
  1278. glEnableVertexAttribArray(VS::ARRAY_VERTEX);
  1279. glBufferSubData(GL_ARRAY_BUFFER, buf_ofs, sizeof(Vector3) * vertices, c.vertices.ptr());
  1280. glVertexAttribPointer(VS::ARRAY_VERTEX, 3, GL_FLOAT, false, sizeof(Vector3), ((uint8_t *)NULL) + buf_ofs);
  1281. glDrawArrays(gl_primitive[c.primitive], 0, c.vertices.size());
  1282. }
  1283. if (restore_tex) {
  1284. glActiveTexture(GL_TEXTURE0);
  1285. glBindTexture(GL_TEXTURE_2D, state.current_main_tex);
  1286. restore_tex = false;
  1287. }
  1288. } break;
  1289. case VS::INSTANCE_PARTICLES: {
  1290. RasterizerStorageGLES3::Particles *particles = static_cast<RasterizerStorageGLES3::Particles *>(e->owner);
  1291. RasterizerStorageGLES3::Surface *s = static_cast<RasterizerStorageGLES3::Surface *>(e->geometry);
  1292. if (!particles->use_local_coords) //not using local coordinates? then clear transform..
  1293. state.scene_shader.set_uniform(SceneShaderGLES3::WORLD_TRANSFORM, Transform());
  1294. int amount = particles->amount;
  1295. if (particles->draw_order == VS::PARTICLES_DRAW_ORDER_LIFETIME) {
  1296. //split
  1297. int stride = sizeof(float) * 4 * 6;
  1298. int split = int(Math::ceil(particles->phase * particles->amount));
  1299. if (amount - split > 0) {
  1300. glEnableVertexAttribArray(8); //xform x
  1301. glVertexAttribPointer(8, 4, GL_FLOAT, GL_FALSE, stride, ((uint8_t *)NULL) + stride * split + sizeof(float) * 4 * 3);
  1302. glVertexAttribDivisor(8, 1);
  1303. glEnableVertexAttribArray(9); //xform y
  1304. glVertexAttribPointer(9, 4, GL_FLOAT, GL_FALSE, stride, ((uint8_t *)NULL) + stride * split + sizeof(float) * 4 * 4);
  1305. glVertexAttribDivisor(9, 1);
  1306. glEnableVertexAttribArray(10); //xform z
  1307. glVertexAttribPointer(10, 4, GL_FLOAT, GL_FALSE, stride, ((uint8_t *)NULL) + stride * split + sizeof(float) * 4 * 5);
  1308. glVertexAttribDivisor(10, 1);
  1309. glEnableVertexAttribArray(11); //color
  1310. glVertexAttribPointer(11, 4, GL_FLOAT, GL_FALSE, stride, ((uint8_t *)NULL) + stride * split + 0);
  1311. glVertexAttribDivisor(11, 1);
  1312. glEnableVertexAttribArray(12); //custom
  1313. glVertexAttribPointer(12, 4, GL_FLOAT, GL_FALSE, stride, ((uint8_t *)NULL) + stride * split + sizeof(float) * 4 * 2);
  1314. glVertexAttribDivisor(12, 1);
  1315. #ifdef DEBUG_ENABLED
  1316. if (state.debug_draw == VS::VIEWPORT_DEBUG_DRAW_WIREFRAME && s->array_wireframe_id) {
  1317. glDrawElementsInstanced(GL_LINES, s->index_wireframe_len, GL_UNSIGNED_INT, 0, amount - split);
  1318. storage->info.render.vertices_count += s->index_array_len * (amount - split);
  1319. } else
  1320. #endif
  1321. if (s->index_array_len > 0) {
  1322. glDrawElementsInstanced(gl_primitive[s->primitive], s->index_array_len, (s->array_len >= (1 << 16)) ? GL_UNSIGNED_INT : GL_UNSIGNED_SHORT, 0, amount - split);
  1323. storage->info.render.vertices_count += s->index_array_len * (amount - split);
  1324. } else {
  1325. glDrawArraysInstanced(gl_primitive[s->primitive], 0, s->array_len, amount - split);
  1326. storage->info.render.vertices_count += s->array_len * (amount - split);
  1327. }
  1328. }
  1329. if (split > 0) {
  1330. glEnableVertexAttribArray(8); //xform x
  1331. glVertexAttribPointer(8, 4, GL_FLOAT, GL_FALSE, stride, ((uint8_t *)NULL) + sizeof(float) * 4 * 3);
  1332. glVertexAttribDivisor(8, 1);
  1333. glEnableVertexAttribArray(9); //xform y
  1334. glVertexAttribPointer(9, 4, GL_FLOAT, GL_FALSE, stride, ((uint8_t *)NULL) + sizeof(float) * 4 * 4);
  1335. glVertexAttribDivisor(9, 1);
  1336. glEnableVertexAttribArray(10); //xform z
  1337. glVertexAttribPointer(10, 4, GL_FLOAT, GL_FALSE, stride, ((uint8_t *)NULL) + sizeof(float) * 4 * 5);
  1338. glVertexAttribDivisor(10, 1);
  1339. glEnableVertexAttribArray(11); //color
  1340. glVertexAttribPointer(11, 4, GL_FLOAT, GL_FALSE, stride, ((uint8_t *)NULL) + 0);
  1341. glVertexAttribDivisor(11, 1);
  1342. glEnableVertexAttribArray(12); //custom
  1343. glVertexAttribPointer(12, 4, GL_FLOAT, GL_FALSE, stride, ((uint8_t *)NULL) + sizeof(float) * 4 * 2);
  1344. glVertexAttribDivisor(12, 1);
  1345. #ifdef DEBUG_ENABLED
  1346. if (state.debug_draw == VS::VIEWPORT_DEBUG_DRAW_WIREFRAME && s->array_wireframe_id) {
  1347. glDrawElementsInstanced(GL_LINES, s->index_wireframe_len, GL_UNSIGNED_INT, 0, split);
  1348. storage->info.render.vertices_count += s->index_array_len * split;
  1349. } else
  1350. #endif
  1351. if (s->index_array_len > 0) {
  1352. glDrawElementsInstanced(gl_primitive[s->primitive], s->index_array_len, (s->array_len >= (1 << 16)) ? GL_UNSIGNED_INT : GL_UNSIGNED_SHORT, 0, split);
  1353. storage->info.render.vertices_count += s->index_array_len * split;
  1354. } else {
  1355. glDrawArraysInstanced(gl_primitive[s->primitive], 0, s->array_len, split);
  1356. storage->info.render.vertices_count += s->array_len * split;
  1357. }
  1358. }
  1359. } else {
  1360. #ifdef DEBUG_ENABLED
  1361. if (state.debug_draw == VS::VIEWPORT_DEBUG_DRAW_WIREFRAME && s->array_wireframe_id) {
  1362. glDrawElementsInstanced(GL_LINES, s->index_wireframe_len, GL_UNSIGNED_INT, 0, amount);
  1363. storage->info.render.vertices_count += s->index_array_len * amount;
  1364. } else
  1365. #endif
  1366. if (s->index_array_len > 0) {
  1367. glDrawElementsInstanced(gl_primitive[s->primitive], s->index_array_len, (s->array_len >= (1 << 16)) ? GL_UNSIGNED_INT : GL_UNSIGNED_SHORT, 0, amount);
  1368. storage->info.render.vertices_count += s->index_array_len * amount;
  1369. } else {
  1370. glDrawArraysInstanced(gl_primitive[s->primitive], 0, s->array_len, amount);
  1371. storage->info.render.vertices_count += s->array_len * amount;
  1372. }
  1373. }
  1374. } break;
  1375. default: {}
  1376. }
  1377. }
  1378. void RasterizerSceneGLES3::_setup_light(RenderList::Element *e, const Transform &p_view_transform) {
  1379. int omni_indices[16];
  1380. int omni_count = 0;
  1381. int spot_indices[16];
  1382. int spot_count = 0;
  1383. int reflection_indices[16];
  1384. int reflection_count = 0;
  1385. int maxobj = MIN(16, state.max_forward_lights_per_object);
  1386. int lc = e->instance->light_instances.size();
  1387. if (lc) {
  1388. const RID *lights = e->instance->light_instances.ptr();
  1389. for (int i = 0; i < lc; i++) {
  1390. LightInstance *li = light_instance_owner.getptr(lights[i]);
  1391. if (li->last_pass != render_pass) //not visible
  1392. continue;
  1393. if (li->light_ptr->type == VS::LIGHT_OMNI) {
  1394. if (omni_count < maxobj && e->instance->layer_mask & li->light_ptr->cull_mask) {
  1395. omni_indices[omni_count++] = li->light_index;
  1396. }
  1397. }
  1398. if (li->light_ptr->type == VS::LIGHT_SPOT) {
  1399. if (spot_count < maxobj && e->instance->layer_mask & li->light_ptr->cull_mask) {
  1400. spot_indices[spot_count++] = li->light_index;
  1401. }
  1402. }
  1403. }
  1404. }
  1405. state.scene_shader.set_uniform(SceneShaderGLES3::OMNI_LIGHT_COUNT, omni_count);
  1406. if (omni_count) {
  1407. glUniform1iv(state.scene_shader.get_uniform(SceneShaderGLES3::OMNI_LIGHT_INDICES), omni_count, omni_indices);
  1408. }
  1409. state.scene_shader.set_uniform(SceneShaderGLES3::SPOT_LIGHT_COUNT, spot_count);
  1410. if (spot_count) {
  1411. glUniform1iv(state.scene_shader.get_uniform(SceneShaderGLES3::SPOT_LIGHT_INDICES), spot_count, spot_indices);
  1412. }
  1413. int rc = e->instance->reflection_probe_instances.size();
  1414. if (rc) {
  1415. const RID *reflections = e->instance->reflection_probe_instances.ptr();
  1416. for (int i = 0; i < rc; i++) {
  1417. ReflectionProbeInstance *rpi = reflection_probe_instance_owner.getptr(reflections[i]);
  1418. if (rpi->last_pass != render_pass) //not visible
  1419. continue;
  1420. if (reflection_count < maxobj) {
  1421. reflection_indices[reflection_count++] = rpi->reflection_index;
  1422. }
  1423. }
  1424. }
  1425. state.scene_shader.set_uniform(SceneShaderGLES3::REFLECTION_COUNT, reflection_count);
  1426. if (reflection_count) {
  1427. glUniform1iv(state.scene_shader.get_uniform(SceneShaderGLES3::REFLECTION_INDICES), reflection_count, reflection_indices);
  1428. }
  1429. int gi_probe_count = e->instance->gi_probe_instances.size();
  1430. if (gi_probe_count) {
  1431. const RID *ridp = e->instance->gi_probe_instances.ptr();
  1432. GIProbeInstance *gipi = gi_probe_instance_owner.getptr(ridp[0]);
  1433. float bias_scale = e->instance->baked_light ? 1 : 0;
  1434. glActiveTexture(GL_TEXTURE0 + storage->config.max_texture_image_units - 9);
  1435. glBindTexture(GL_TEXTURE_3D, gipi->tex_cache);
  1436. state.scene_shader.set_uniform(SceneShaderGLES3::GI_PROBE_XFORM1, gipi->transform_to_data * p_view_transform);
  1437. state.scene_shader.set_uniform(SceneShaderGLES3::GI_PROBE_BOUNDS1, gipi->bounds);
  1438. state.scene_shader.set_uniform(SceneShaderGLES3::GI_PROBE_MULTIPLIER1, gipi->probe ? gipi->probe->dynamic_range * gipi->probe->energy : 0.0);
  1439. state.scene_shader.set_uniform(SceneShaderGLES3::GI_PROBE_BIAS1, gipi->probe ? gipi->probe->bias * bias_scale : 0.0);
  1440. state.scene_shader.set_uniform(SceneShaderGLES3::GI_PROBE_NORMAL_BIAS1, gipi->probe ? gipi->probe->normal_bias * bias_scale : 0.0);
  1441. state.scene_shader.set_uniform(SceneShaderGLES3::GI_PROBE_BLEND_AMBIENT1, gipi->probe ? !gipi->probe->interior : false);
  1442. state.scene_shader.set_uniform(SceneShaderGLES3::GI_PROBE_CELL_SIZE1, gipi->cell_size_cache);
  1443. if (gi_probe_count > 1) {
  1444. GIProbeInstance *gipi2 = gi_probe_instance_owner.getptr(ridp[1]);
  1445. glActiveTexture(GL_TEXTURE0 + storage->config.max_texture_image_units - 10);
  1446. glBindTexture(GL_TEXTURE_3D, gipi2->tex_cache);
  1447. state.scene_shader.set_uniform(SceneShaderGLES3::GI_PROBE_XFORM2, gipi2->transform_to_data * p_view_transform);
  1448. state.scene_shader.set_uniform(SceneShaderGLES3::GI_PROBE_BOUNDS2, gipi2->bounds);
  1449. state.scene_shader.set_uniform(SceneShaderGLES3::GI_PROBE_CELL_SIZE2, gipi2->cell_size_cache);
  1450. state.scene_shader.set_uniform(SceneShaderGLES3::GI_PROBE_MULTIPLIER2, gipi2->probe ? gipi2->probe->dynamic_range * gipi2->probe->energy : 0.0);
  1451. state.scene_shader.set_uniform(SceneShaderGLES3::GI_PROBE_BIAS2, gipi2->probe ? gipi2->probe->bias * bias_scale : 0.0);
  1452. state.scene_shader.set_uniform(SceneShaderGLES3::GI_PROBE_NORMAL_BIAS2, gipi2->probe ? gipi2->probe->normal_bias * bias_scale : 0.0);
  1453. state.scene_shader.set_uniform(SceneShaderGLES3::GI_PROBE_BLEND_AMBIENT2, gipi2->probe ? !gipi2->probe->interior : false);
  1454. state.scene_shader.set_uniform(SceneShaderGLES3::GI_PROBE2_ENABLED, true);
  1455. } else {
  1456. state.scene_shader.set_uniform(SceneShaderGLES3::GI_PROBE2_ENABLED, false);
  1457. }
  1458. } else if (!e->instance->lightmap_capture_data.empty()) {
  1459. glUniform4fv(state.scene_shader.get_uniform_location(SceneShaderGLES3::LIGHTMAP_CAPTURES), 12, (const GLfloat *)e->instance->lightmap_capture_data.ptr());
  1460. state.scene_shader.set_uniform(SceneShaderGLES3::LIGHTMAP_CAPTURE_SKY, false);
  1461. } else if (e->instance->lightmap.is_valid()) {
  1462. RasterizerStorageGLES3::Texture *lightmap = storage->texture_owner.getornull(e->instance->lightmap);
  1463. RasterizerStorageGLES3::LightmapCapture *capture = storage->lightmap_capture_data_owner.getornull(e->instance->lightmap_capture->base);
  1464. if (lightmap && capture) {
  1465. glActiveTexture(GL_TEXTURE0 + storage->config.max_texture_image_units - 9);
  1466. glBindTexture(GL_TEXTURE_2D, lightmap->tex_id);
  1467. state.scene_shader.set_uniform(SceneShaderGLES3::LIGHTMAP_ENERGY, capture->energy);
  1468. }
  1469. }
  1470. }
  1471. void RasterizerSceneGLES3::_set_cull(bool p_front, bool p_disabled, bool p_reverse_cull) {
  1472. bool front = p_front;
  1473. if (p_reverse_cull)
  1474. front = !front;
  1475. if (p_disabled != state.cull_disabled) {
  1476. if (p_disabled)
  1477. glDisable(GL_CULL_FACE);
  1478. else
  1479. glEnable(GL_CULL_FACE);
  1480. state.cull_disabled = p_disabled;
  1481. }
  1482. if (front != state.cull_front) {
  1483. glCullFace(front ? GL_FRONT : GL_BACK);
  1484. state.cull_front = front;
  1485. }
  1486. }
  1487. 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) {
  1488. glBindBufferBase(GL_UNIFORM_BUFFER, 0, state.scene_ubo); //bind globals ubo
  1489. bool use_radiance_map = false;
  1490. if (!p_shadow && !p_directional_add) {
  1491. glBindBufferBase(GL_UNIFORM_BUFFER, 2, state.env_radiance_ubo); //bind environment radiance info
  1492. if (p_base_env) {
  1493. glActiveTexture(GL_TEXTURE0 + storage->config.max_texture_image_units - 2);
  1494. if (storage->config.use_texture_array_environment) {
  1495. glBindTexture(GL_TEXTURE_2D_ARRAY, p_base_env);
  1496. } else {
  1497. glBindTexture(GL_TEXTURE_2D, p_base_env);
  1498. }
  1499. state.scene_shader.set_conditional(SceneShaderGLES3::USE_RADIANCE_MAP, true);
  1500. state.scene_shader.set_conditional(SceneShaderGLES3::USE_RADIANCE_MAP_ARRAY, storage->config.use_texture_array_environment);
  1501. use_radiance_map = true;
  1502. } else {
  1503. state.scene_shader.set_conditional(SceneShaderGLES3::USE_RADIANCE_MAP, false);
  1504. state.scene_shader.set_conditional(SceneShaderGLES3::USE_RADIANCE_MAP_ARRAY, false);
  1505. }
  1506. } else {
  1507. state.scene_shader.set_conditional(SceneShaderGLES3::USE_RADIANCE_MAP, false);
  1508. state.scene_shader.set_conditional(SceneShaderGLES3::USE_RADIANCE_MAP_ARRAY, false);
  1509. }
  1510. state.cull_front = false;
  1511. state.cull_disabled = false;
  1512. glCullFace(GL_BACK);
  1513. glEnable(GL_CULL_FACE);
  1514. state.current_depth_test = true;
  1515. glEnable(GL_DEPTH_TEST);
  1516. state.scene_shader.set_conditional(SceneShaderGLES3::USE_SKELETON, false);
  1517. state.current_blend_mode = -1;
  1518. state.current_line_width = -1;
  1519. state.current_depth_draw = -1;
  1520. RasterizerStorageGLES3::Material *prev_material = NULL;
  1521. RasterizerStorageGLES3::Geometry *prev_geometry = NULL;
  1522. RasterizerStorageGLES3::GeometryOwner *prev_owner = NULL;
  1523. VS::InstanceType prev_base_type = VS::INSTANCE_MAX;
  1524. int current_blend_mode = -1;
  1525. int prev_shading = -1;
  1526. RID prev_skeleton;
  1527. state.scene_shader.set_conditional(SceneShaderGLES3::SHADELESS, true); //by default unshaded (easier to set)
  1528. bool first = true;
  1529. bool prev_use_instancing = false;
  1530. storage->info.render.draw_call_count += p_element_count;
  1531. bool prev_opaque_prepass = false;
  1532. for (int i = 0; i < p_element_count; i++) {
  1533. RenderList::Element *e = p_elements[i];
  1534. RasterizerStorageGLES3::Material *material = e->material;
  1535. RID skeleton = e->instance->skeleton;
  1536. bool rebind = first;
  1537. int shading = (e->sort_key >> RenderList::SORT_KEY_SHADING_SHIFT) & RenderList::SORT_KEY_SHADING_MASK;
  1538. if (!p_shadow) {
  1539. if (p_directional_add) {
  1540. if (e->sort_key & SORT_KEY_UNSHADED_FLAG || !(e->instance->layer_mask & directional_light->light_ptr->cull_mask)) {
  1541. continue;
  1542. }
  1543. shading &= ~1; //ignore the ignore directional for base pass
  1544. }
  1545. if (shading != prev_shading) {
  1546. if (e->sort_key & SORT_KEY_UNSHADED_FLAG) {
  1547. state.scene_shader.set_conditional(SceneShaderGLES3::SHADELESS, true);
  1548. state.scene_shader.set_conditional(SceneShaderGLES3::USE_FORWARD_LIGHTING, false);
  1549. state.scene_shader.set_conditional(SceneShaderGLES3::USE_VERTEX_LIGHTING, false);
  1550. state.scene_shader.set_conditional(SceneShaderGLES3::USE_LIGHT_DIRECTIONAL, false);
  1551. state.scene_shader.set_conditional(SceneShaderGLES3::LIGHT_DIRECTIONAL_SHADOW, false);
  1552. state.scene_shader.set_conditional(SceneShaderGLES3::LIGHT_USE_PSSM4, false);
  1553. state.scene_shader.set_conditional(SceneShaderGLES3::LIGHT_USE_PSSM2, false);
  1554. state.scene_shader.set_conditional(SceneShaderGLES3::LIGHT_USE_PSSM_BLEND, false);
  1555. state.scene_shader.set_conditional(SceneShaderGLES3::LIGHT_USE_PSSM_BLEND, false);
  1556. state.scene_shader.set_conditional(SceneShaderGLES3::SHADOW_MODE_PCF_5, false);
  1557. state.scene_shader.set_conditional(SceneShaderGLES3::SHADOW_MODE_PCF_13, false);
  1558. state.scene_shader.set_conditional(SceneShaderGLES3::USE_GI_PROBES, false);
  1559. state.scene_shader.set_conditional(SceneShaderGLES3::USE_LIGHTMAP_CAPTURE, false);
  1560. state.scene_shader.set_conditional(SceneShaderGLES3::USE_LIGHTMAP, false);
  1561. state.scene_shader.set_conditional(SceneShaderGLES3::USE_RADIANCE_MAP, false);
  1562. state.scene_shader.set_conditional(SceneShaderGLES3::USE_CONTACT_SHADOWS, false);
  1563. //state.scene_shader.set_conditional(SceneShaderGLES3::SHADELESS,true);
  1564. } else {
  1565. state.scene_shader.set_conditional(SceneShaderGLES3::USE_GI_PROBES, e->instance->gi_probe_instances.size() > 0);
  1566. state.scene_shader.set_conditional(SceneShaderGLES3::USE_LIGHTMAP, e->instance->lightmap.is_valid() && e->instance->gi_probe_instances.size() == 0);
  1567. 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);
  1568. state.scene_shader.set_conditional(SceneShaderGLES3::SHADELESS, false);
  1569. state.scene_shader.set_conditional(SceneShaderGLES3::USE_FORWARD_LIGHTING, !p_directional_add);
  1570. state.scene_shader.set_conditional(SceneShaderGLES3::USE_VERTEX_LIGHTING, (e->sort_key & SORT_KEY_VERTEX_LIT_FLAG));
  1571. state.scene_shader.set_conditional(SceneShaderGLES3::USE_LIGHT_DIRECTIONAL, false);
  1572. state.scene_shader.set_conditional(SceneShaderGLES3::LIGHT_DIRECTIONAL_SHADOW, false);
  1573. state.scene_shader.set_conditional(SceneShaderGLES3::LIGHT_USE_PSSM4, false);
  1574. state.scene_shader.set_conditional(SceneShaderGLES3::LIGHT_USE_PSSM2, false);
  1575. state.scene_shader.set_conditional(SceneShaderGLES3::LIGHT_USE_PSSM_BLEND, false);
  1576. state.scene_shader.set_conditional(SceneShaderGLES3::SHADOW_MODE_PCF_5, shadow_filter_mode == SHADOW_FILTER_PCF5);
  1577. state.scene_shader.set_conditional(SceneShaderGLES3::SHADOW_MODE_PCF_13, shadow_filter_mode == SHADOW_FILTER_PCF13);
  1578. state.scene_shader.set_conditional(SceneShaderGLES3::USE_RADIANCE_MAP, use_radiance_map);
  1579. state.scene_shader.set_conditional(SceneShaderGLES3::USE_CONTACT_SHADOWS, state.used_contact_shadows);
  1580. if (p_directional_add || (directional_light && (e->sort_key & SORT_KEY_NO_DIRECTIONAL_FLAG) == 0)) {
  1581. state.scene_shader.set_conditional(SceneShaderGLES3::USE_LIGHT_DIRECTIONAL, true);
  1582. if (p_directional_shadows && directional_light->light_ptr->shadow) {
  1583. state.scene_shader.set_conditional(SceneShaderGLES3::LIGHT_DIRECTIONAL_SHADOW, true);
  1584. switch (directional_light->light_ptr->directional_shadow_mode) {
  1585. case VS::LIGHT_DIRECTIONAL_SHADOW_ORTHOGONAL:
  1586. break; //none
  1587. case VS::LIGHT_DIRECTIONAL_SHADOW_PARALLEL_2_SPLITS:
  1588. state.scene_shader.set_conditional(SceneShaderGLES3::LIGHT_USE_PSSM2, true);
  1589. state.scene_shader.set_conditional(SceneShaderGLES3::LIGHT_USE_PSSM_BLEND, directional_light->light_ptr->directional_blend_splits);
  1590. break;
  1591. case VS::LIGHT_DIRECTIONAL_SHADOW_PARALLEL_4_SPLITS:
  1592. state.scene_shader.set_conditional(SceneShaderGLES3::LIGHT_USE_PSSM4, true);
  1593. state.scene_shader.set_conditional(SceneShaderGLES3::LIGHT_USE_PSSM_BLEND, directional_light->light_ptr->directional_blend_splits);
  1594. break;
  1595. }
  1596. }
  1597. }
  1598. }
  1599. rebind = true;
  1600. }
  1601. if (p_alpha_pass || p_directional_add) {
  1602. int desired_blend_mode;
  1603. if (p_directional_add) {
  1604. desired_blend_mode = RasterizerStorageGLES3::Shader::Spatial::BLEND_MODE_ADD;
  1605. } else {
  1606. desired_blend_mode = material->shader->spatial.blend_mode;
  1607. }
  1608. if (desired_blend_mode != current_blend_mode) {
  1609. switch (desired_blend_mode) {
  1610. case RasterizerStorageGLES3::Shader::Spatial::BLEND_MODE_MIX: {
  1611. glBlendEquation(GL_FUNC_ADD);
  1612. if (storage->frame.current_rt && storage->frame.current_rt->flags[RasterizerStorage::RENDER_TARGET_TRANSPARENT]) {
  1613. glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
  1614. } else {
  1615. glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  1616. }
  1617. } break;
  1618. case RasterizerStorageGLES3::Shader::Spatial::BLEND_MODE_ADD: {
  1619. glBlendEquation(GL_FUNC_ADD);
  1620. glBlendFunc(p_alpha_pass ? GL_SRC_ALPHA : GL_ONE, GL_ONE);
  1621. } break;
  1622. case RasterizerStorageGLES3::Shader::Spatial::BLEND_MODE_SUB: {
  1623. glBlendEquation(GL_FUNC_REVERSE_SUBTRACT);
  1624. glBlendFunc(GL_SRC_ALPHA, GL_ONE);
  1625. } break;
  1626. case RasterizerStorageGLES3::Shader::Spatial::BLEND_MODE_MUL: {
  1627. glBlendEquation(GL_FUNC_ADD);
  1628. if (storage->frame.current_rt && storage->frame.current_rt->flags[RasterizerStorage::RENDER_TARGET_TRANSPARENT]) {
  1629. glBlendFuncSeparate(GL_DST_COLOR, GL_ZERO, GL_DST_ALPHA, GL_ZERO);
  1630. } else {
  1631. glBlendFuncSeparate(GL_DST_COLOR, GL_ZERO, GL_ZERO, GL_ONE);
  1632. }
  1633. } break;
  1634. }
  1635. current_blend_mode = desired_blend_mode;
  1636. }
  1637. }
  1638. }
  1639. bool use_opaque_prepass = e->sort_key & RenderList::SORT_KEY_OPAQUE_PRE_PASS;
  1640. if (use_opaque_prepass != prev_opaque_prepass) {
  1641. state.scene_shader.set_conditional(SceneShaderGLES3::USE_OPAQUE_PREPASS, use_opaque_prepass);
  1642. rebind = true;
  1643. }
  1644. bool use_instancing = e->instance->base_type == VS::INSTANCE_MULTIMESH || e->instance->base_type == VS::INSTANCE_PARTICLES;
  1645. if (use_instancing != prev_use_instancing) {
  1646. state.scene_shader.set_conditional(SceneShaderGLES3::USE_INSTANCING, use_instancing);
  1647. rebind = true;
  1648. }
  1649. if (prev_skeleton != skeleton) {
  1650. if (prev_skeleton.is_valid() != skeleton.is_valid()) {
  1651. state.scene_shader.set_conditional(SceneShaderGLES3::USE_SKELETON, skeleton.is_valid());
  1652. rebind = true;
  1653. }
  1654. if (skeleton.is_valid()) {
  1655. RasterizerStorageGLES3::Skeleton *sk = storage->skeleton_owner.getornull(skeleton);
  1656. glActiveTexture(GL_TEXTURE0 + storage->config.max_texture_image_units - 1);
  1657. glBindTexture(GL_TEXTURE_2D, sk->texture);
  1658. }
  1659. }
  1660. if (material != prev_material || rebind) {
  1661. storage->info.render.material_switch_count++;
  1662. rebind = _setup_material(material, p_alpha_pass);
  1663. if (rebind) {
  1664. storage->info.render.shader_rebind_count++;
  1665. }
  1666. }
  1667. if (!(e->sort_key & SORT_KEY_UNSHADED_FLAG) && !p_directional_add && !p_shadow) {
  1668. _setup_light(e, p_view_transform);
  1669. }
  1670. if (e->owner != prev_owner || prev_base_type != e->instance->base_type || prev_geometry != e->geometry) {
  1671. _setup_geometry(e, p_view_transform);
  1672. storage->info.render.surface_switch_count++;
  1673. }
  1674. _set_cull(e->sort_key & RenderList::SORT_KEY_MIRROR_FLAG, e->sort_key & RenderList::SORT_KEY_CULL_DISABLED_FLAG, p_reverse_cull);
  1675. state.scene_shader.set_uniform(SceneShaderGLES3::WORLD_TRANSFORM, e->instance->transform);
  1676. _render_geometry(e);
  1677. prev_material = material;
  1678. prev_base_type = e->instance->base_type;
  1679. prev_geometry = e->geometry;
  1680. prev_owner = e->owner;
  1681. prev_shading = shading;
  1682. prev_skeleton = skeleton;
  1683. prev_use_instancing = use_instancing;
  1684. prev_opaque_prepass = use_opaque_prepass;
  1685. first = false;
  1686. }
  1687. glBindVertexArray(0);
  1688. state.scene_shader.set_conditional(SceneShaderGLES3::USE_INSTANCING, false);
  1689. state.scene_shader.set_conditional(SceneShaderGLES3::USE_SKELETON, false);
  1690. state.scene_shader.set_conditional(SceneShaderGLES3::USE_RADIANCE_MAP, false);
  1691. state.scene_shader.set_conditional(SceneShaderGLES3::USE_FORWARD_LIGHTING, false);
  1692. state.scene_shader.set_conditional(SceneShaderGLES3::USE_LIGHT_DIRECTIONAL, false);
  1693. state.scene_shader.set_conditional(SceneShaderGLES3::LIGHT_DIRECTIONAL_SHADOW, false);
  1694. state.scene_shader.set_conditional(SceneShaderGLES3::LIGHT_USE_PSSM4, false);
  1695. state.scene_shader.set_conditional(SceneShaderGLES3::LIGHT_USE_PSSM2, false);
  1696. state.scene_shader.set_conditional(SceneShaderGLES3::LIGHT_USE_PSSM_BLEND, false);
  1697. state.scene_shader.set_conditional(SceneShaderGLES3::SHADELESS, false);
  1698. state.scene_shader.set_conditional(SceneShaderGLES3::SHADOW_MODE_PCF_5, false);
  1699. state.scene_shader.set_conditional(SceneShaderGLES3::SHADOW_MODE_PCF_13, false);
  1700. state.scene_shader.set_conditional(SceneShaderGLES3::USE_GI_PROBES, false);
  1701. state.scene_shader.set_conditional(SceneShaderGLES3::USE_LIGHTMAP, false);
  1702. state.scene_shader.set_conditional(SceneShaderGLES3::USE_LIGHTMAP_CAPTURE, false);
  1703. state.scene_shader.set_conditional(SceneShaderGLES3::USE_CONTACT_SHADOWS, false);
  1704. state.scene_shader.set_conditional(SceneShaderGLES3::USE_VERTEX_LIGHTING, false);
  1705. state.scene_shader.set_conditional(SceneShaderGLES3::USE_OPAQUE_PREPASS, false);
  1706. }
  1707. 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) {
  1708. RasterizerStorageGLES3::Material *m = NULL;
  1709. RID m_src = p_instance->material_override.is_valid() ? p_instance->material_override : (p_material >= 0 ? p_instance->materials[p_material] : p_geometry->material);
  1710. if (state.debug_draw == VS::VIEWPORT_DEBUG_DRAW_OVERDRAW) {
  1711. m_src = default_overdraw_material;
  1712. }
  1713. /*
  1714. #ifdef DEBUG_ENABLED
  1715. if (current_debug==VS::SCENARIO_DEBUG_OVERDRAW) {
  1716. m_src=overdraw_material;
  1717. }
  1718. #endif
  1719. */
  1720. if (m_src.is_valid()) {
  1721. m = storage->material_owner.getornull(m_src);
  1722. if (!m->shader || !m->shader->valid) {
  1723. m = NULL;
  1724. }
  1725. }
  1726. if (!m) {
  1727. m = storage->material_owner.getptr(default_material);
  1728. }
  1729. ERR_FAIL_COND(!m);
  1730. _add_geometry_with_material(p_geometry, p_instance, p_owner, m, p_depth_pass, p_shadow_pass);
  1731. while (m->next_pass.is_valid()) {
  1732. m = storage->material_owner.getornull(m->next_pass);
  1733. if (!m || !m->shader || !m->shader->valid)
  1734. break;
  1735. _add_geometry_with_material(p_geometry, p_instance, p_owner, m, p_depth_pass, p_shadow_pass);
  1736. }
  1737. }
  1738. 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) {
  1739. 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;
  1740. bool has_blend_alpha = p_material->shader->spatial.blend_mode != RasterizerStorageGLES3::Shader::Spatial::BLEND_MODE_MIX;
  1741. bool has_alpha = has_base_alpha || has_blend_alpha;
  1742. bool mirror = p_instance->mirror;
  1743. bool no_cull = false;
  1744. if (p_material->shader->spatial.cull_mode == RasterizerStorageGLES3::Shader::Spatial::CULL_MODE_DISABLED) {
  1745. no_cull = true;
  1746. mirror = false;
  1747. } else if (p_material->shader->spatial.cull_mode == RasterizerStorageGLES3::Shader::Spatial::CULL_MODE_FRONT) {
  1748. mirror = !mirror;
  1749. }
  1750. if (p_material->shader->spatial.uses_sss) {
  1751. state.used_sss = true;
  1752. }
  1753. if (p_material->shader->spatial.uses_screen_texture) {
  1754. state.used_screen_texture = true;
  1755. }
  1756. if (p_depth_pass) {
  1757. 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))
  1758. return; //bye
  1759. 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) {
  1760. //shader does not use discard and does not write a vertex position, use generic material
  1761. if (p_instance->cast_shadows == VS::SHADOW_CASTING_SETTING_DOUBLE_SIDED) {
  1762. p_material = storage->material_owner.getptr(!p_shadow_pass && p_material->shader->spatial.uses_world_coordinates ? default_worldcoord_material_twosided : default_material_twosided);
  1763. no_cull = true;
  1764. mirror = false;
  1765. } else {
  1766. p_material = storage->material_owner.getptr(!p_shadow_pass && p_material->shader->spatial.uses_world_coordinates ? default_worldcoord_material : default_material);
  1767. }
  1768. }
  1769. has_alpha = false;
  1770. }
  1771. RenderList::Element *e = has_alpha ? render_list.add_alpha_element() : render_list.add_element();
  1772. if (!e)
  1773. return;
  1774. e->geometry = p_geometry;
  1775. e->material = p_material;
  1776. e->instance = p_instance;
  1777. e->owner = p_owner;
  1778. e->sort_key = 0;
  1779. if (e->geometry->last_pass != render_pass) {
  1780. e->geometry->last_pass = render_pass;
  1781. e->geometry->index = current_geometry_index++;
  1782. }
  1783. if (!p_depth_pass && directional_light && (directional_light->light_ptr->cull_mask & e->instance->layer_mask) == 0) {
  1784. e->sort_key |= SORT_KEY_NO_DIRECTIONAL_FLAG;
  1785. }
  1786. e->sort_key |= uint64_t(e->geometry->index) << RenderList::SORT_KEY_GEOMETRY_INDEX_SHIFT;
  1787. e->sort_key |= uint64_t(e->instance->base_type) << RenderList::SORT_KEY_GEOMETRY_TYPE_SHIFT;
  1788. if (e->material->last_pass != render_pass) {
  1789. e->material->last_pass = render_pass;
  1790. e->material->index = current_material_index++;
  1791. }
  1792. e->sort_key |= uint64_t(e->material->index) << RenderList::SORT_KEY_MATERIAL_INDEX_SHIFT;
  1793. e->sort_key |= uint64_t(e->instance->depth_layer) << RenderList::SORT_KEY_OPAQUE_DEPTH_LAYER_SHIFT;
  1794. if (!p_depth_pass) {
  1795. if (e->instance->gi_probe_instances.size()) {
  1796. e->sort_key |= SORT_KEY_GI_PROBES_FLAG;
  1797. }
  1798. if (e->instance->lightmap.is_valid()) {
  1799. e->sort_key |= SORT_KEY_LIGHTMAP_FLAG;
  1800. }
  1801. if (!e->instance->lightmap_capture_data.empty()) {
  1802. e->sort_key |= SORT_KEY_LIGHTMAP_CAPTURE_FLAG;
  1803. }
  1804. e->sort_key |= uint64_t(p_material->render_priority + 128) << RenderList::SORT_KEY_PRIORITY_SHIFT;
  1805. }
  1806. /*
  1807. if (e->geometry->type==RasterizerStorageGLES3::Geometry::GEOMETRY_MULTISURFACE)
  1808. e->sort_flags|=RenderList::SORT_FLAG_INSTANCING;
  1809. */
  1810. if (mirror) {
  1811. e->sort_key |= RenderList::SORT_KEY_MIRROR_FLAG;
  1812. }
  1813. if (no_cull) {
  1814. e->sort_key |= RenderList::SORT_KEY_CULL_DISABLED_FLAG;
  1815. }
  1816. //e->light_type=0xFF; // no lights!
  1817. if (p_depth_pass || p_material->shader->spatial.unshaded || state.debug_draw == VS::VIEWPORT_DEBUG_DRAW_UNSHADED) {
  1818. e->sort_key |= SORT_KEY_UNSHADED_FLAG;
  1819. }
  1820. if (p_depth_pass && p_material->shader->spatial.depth_draw_mode == RasterizerStorageGLES3::Shader::Spatial::DEPTH_DRAW_ALPHA_PREPASS) {
  1821. e->sort_key |= RenderList::SORT_KEY_OPAQUE_PRE_PASS;
  1822. }
  1823. if (!p_depth_pass && (p_material->shader->spatial.uses_vertex_lighting || storage->config.force_vertex_shading)) {
  1824. e->sort_key |= SORT_KEY_VERTEX_LIT_FLAG;
  1825. }
  1826. if (p_material->shader->spatial.uses_time) {
  1827. VisualServerRaster::redraw_request();
  1828. }
  1829. }
  1830. 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) {
  1831. ERR_FAIL_COND(!p_sky);
  1832. RasterizerStorageGLES3::Texture *tex = storage->texture_owner.getornull(p_sky->panorama);
  1833. ERR_FAIL_COND(!tex);
  1834. glActiveTexture(GL_TEXTURE0);
  1835. tex = tex->get_ptr(); //resolve for proxies
  1836. glBindTexture(tex->target, tex->tex_id);
  1837. if (storage->config.srgb_decode_supported && tex->srgb && !tex->using_srgb) {
  1838. glTexParameteri(tex->target, _TEXTURE_SRGB_DECODE_EXT, _DECODE_EXT);
  1839. tex->using_srgb = true;
  1840. #ifdef TOOLS_ENABLED
  1841. if (!(tex->flags & VS::TEXTURE_FLAG_CONVERT_TO_LINEAR)) {
  1842. tex->flags |= VS::TEXTURE_FLAG_CONVERT_TO_LINEAR;
  1843. //notify that texture must be set to linear beforehand, so it works in other platforms when exported
  1844. }
  1845. #endif
  1846. }
  1847. glDepthMask(GL_TRUE);
  1848. glEnable(GL_DEPTH_TEST);
  1849. glDisable(GL_CULL_FACE);
  1850. glDisable(GL_BLEND);
  1851. glDepthFunc(GL_LEQUAL);
  1852. glColorMask(1, 1, 1, 1);
  1853. // Camera
  1854. CameraMatrix camera;
  1855. if (p_custom_fov) {
  1856. float near_plane = p_projection.get_z_near();
  1857. float far_plane = p_projection.get_z_far();
  1858. float aspect = p_projection.get_aspect();
  1859. camera.set_perspective(p_custom_fov, aspect, near_plane, far_plane);
  1860. } else {
  1861. camera = p_projection;
  1862. }
  1863. float flip_sign = p_vflip ? -1 : 1;
  1864. /*
  1865. 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).
  1866. 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.
  1867. It also means the uv coordinates are ignored in this mode and we don't need our loop.
  1868. */
  1869. bool asymmetrical = ((camera.matrix[2][0] != 0.0) || (camera.matrix[2][1] != 0.0));
  1870. Vector3 vertices[8] = {
  1871. Vector3(-1, -1 * flip_sign, 1),
  1872. Vector3(0, 1, 0),
  1873. Vector3(1, -1 * flip_sign, 1),
  1874. Vector3(1, 1, 0),
  1875. Vector3(1, 1 * flip_sign, 1),
  1876. Vector3(1, 0, 0),
  1877. Vector3(-1, 1 * flip_sign, 1),
  1878. Vector3(0, 0, 0)
  1879. };
  1880. if (!asymmetrical) {
  1881. float vw, vh, zn;
  1882. camera.get_viewport_size(vw, vh);
  1883. zn = p_projection.get_z_near();
  1884. for (int i = 0; i < 4; i++) {
  1885. Vector3 uv = vertices[i * 2 + 1];
  1886. uv.x = (uv.x * 2.0 - 1.0) * vw;
  1887. uv.y = -(uv.y * 2.0 - 1.0) * vh;
  1888. uv.z = -zn;
  1889. vertices[i * 2 + 1] = p_transform.basis.xform(uv).normalized();
  1890. vertices[i * 2 + 1].z = -vertices[i * 2 + 1].z;
  1891. }
  1892. }
  1893. glBindBuffer(GL_ARRAY_BUFFER, state.sky_verts);
  1894. glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(Vector3) * 8, vertices);
  1895. glBindBuffer(GL_ARRAY_BUFFER, 0); //unbind
  1896. glBindVertexArray(state.sky_array);
  1897. storage->shaders.copy.set_conditional(CopyShaderGLES3::USE_ASYM_PANO, asymmetrical);
  1898. storage->shaders.copy.set_conditional(CopyShaderGLES3::USE_PANORAMA, !asymmetrical);
  1899. storage->shaders.copy.set_conditional(CopyShaderGLES3::USE_MULTIPLIER, true);
  1900. storage->shaders.copy.bind();
  1901. storage->shaders.copy.set_uniform(CopyShaderGLES3::MULTIPLIER, p_energy);
  1902. // don't know why but I always have problems setting a uniform mat3, so we're using a transform
  1903. storage->shaders.copy.set_uniform(CopyShaderGLES3::SKY_TRANSFORM, Transform(p_sky_orientation, Vector3(0.0, 0.0, 0.0)).affine_inverse());
  1904. if (asymmetrical) {
  1905. // pack the bits we need from our projection matrix
  1906. storage->shaders.copy.set_uniform(CopyShaderGLES3::ASYM_PROJ, camera.matrix[2][0], camera.matrix[0][0], camera.matrix[2][1], camera.matrix[1][1]);
  1907. ///@TODO I couldn't get mat3 + p_transform.basis to work, that would be better here.
  1908. storage->shaders.copy.set_uniform(CopyShaderGLES3::PANO_TRANSFORM, p_transform);
  1909. }
  1910. glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
  1911. glBindVertexArray(0);
  1912. glColorMask(1, 1, 1, 1);
  1913. storage->shaders.copy.set_conditional(CopyShaderGLES3::USE_ASYM_PANO, false);
  1914. storage->shaders.copy.set_conditional(CopyShaderGLES3::USE_MULTIPLIER, false);
  1915. storage->shaders.copy.set_conditional(CopyShaderGLES3::USE_PANORAMA, false);
  1916. }
  1917. void RasterizerSceneGLES3::_setup_environment(Environment *env, const CameraMatrix &p_cam_projection, const Transform &p_cam_transform, bool p_no_fog) {
  1918. Transform sky_orientation;
  1919. //store camera into ubo
  1920. store_camera(p_cam_projection, state.ubo_data.projection_matrix);
  1921. store_camera(p_cam_projection.inverse(), state.ubo_data.inv_projection_matrix);
  1922. store_transform(p_cam_transform, state.ubo_data.camera_matrix);
  1923. store_transform(p_cam_transform.affine_inverse(), state.ubo_data.camera_inverse_matrix);
  1924. //time global variables
  1925. state.ubo_data.time = storage->frame.time[0];
  1926. state.ubo_data.z_far = p_cam_projection.get_z_far();
  1927. //bg and ambient
  1928. if (env) {
  1929. state.ubo_data.bg_energy = env->bg_energy;
  1930. state.ubo_data.ambient_energy = env->ambient_energy;
  1931. Color linear_ambient_color = env->ambient_color.to_linear();
  1932. state.ubo_data.ambient_light_color[0] = linear_ambient_color.r;
  1933. state.ubo_data.ambient_light_color[1] = linear_ambient_color.g;
  1934. state.ubo_data.ambient_light_color[2] = linear_ambient_color.b;
  1935. state.ubo_data.ambient_light_color[3] = linear_ambient_color.a;
  1936. Color bg_color;
  1937. switch (env->bg_mode) {
  1938. case VS::ENV_BG_CLEAR_COLOR: {
  1939. bg_color = storage->frame.clear_request_color.to_linear();
  1940. } break;
  1941. case VS::ENV_BG_COLOR: {
  1942. bg_color = env->bg_color.to_linear();
  1943. } break;
  1944. default: {
  1945. bg_color = Color(0, 0, 0, 1);
  1946. } break;
  1947. }
  1948. state.ubo_data.bg_color[0] = bg_color.r;
  1949. state.ubo_data.bg_color[1] = bg_color.g;
  1950. state.ubo_data.bg_color[2] = bg_color.b;
  1951. state.ubo_data.bg_color[3] = bg_color.a;
  1952. //use the inverse of our sky_orientation, we may need to skip this if we're using a reflection probe?
  1953. sky_orientation = Transform(env->sky_orientation, Vector3(0.0, 0.0, 0.0)).affine_inverse();
  1954. state.env_radiance_data.ambient_contribution = env->ambient_sky_contribution;
  1955. state.ubo_data.ambient_occlusion_affect_light = env->ssao_light_affect;
  1956. state.ubo_data.ambient_occlusion_affect_ssao = env->ssao_ao_channel_affect;
  1957. //fog
  1958. Color linear_fog = env->fog_color.to_linear();
  1959. state.ubo_data.fog_color_enabled[0] = linear_fog.r;
  1960. state.ubo_data.fog_color_enabled[1] = linear_fog.g;
  1961. state.ubo_data.fog_color_enabled[2] = linear_fog.b;
  1962. state.ubo_data.fog_color_enabled[3] = (!p_no_fog && env->fog_enabled) ? 1.0 : 0.0;
  1963. state.ubo_data.fog_density = linear_fog.a;
  1964. Color linear_sun = env->fog_sun_color.to_linear();
  1965. state.ubo_data.fog_sun_color_amount[0] = linear_sun.r;
  1966. state.ubo_data.fog_sun_color_amount[1] = linear_sun.g;
  1967. state.ubo_data.fog_sun_color_amount[2] = linear_sun.b;
  1968. state.ubo_data.fog_sun_color_amount[3] = env->fog_sun_amount;
  1969. state.ubo_data.fog_depth_enabled = env->fog_depth_enabled;
  1970. state.ubo_data.fog_depth_begin = env->fog_depth_begin;
  1971. state.ubo_data.fog_depth_end = env->fog_depth_end;
  1972. state.ubo_data.fog_depth_curve = env->fog_depth_curve;
  1973. state.ubo_data.fog_transmit_enabled = env->fog_transmit_enabled;
  1974. state.ubo_data.fog_transmit_curve = env->fog_transmit_curve;
  1975. state.ubo_data.fog_height_enabled = env->fog_height_enabled;
  1976. state.ubo_data.fog_height_min = env->fog_height_min;
  1977. state.ubo_data.fog_height_max = env->fog_height_max;
  1978. state.ubo_data.fog_height_curve = env->fog_height_curve;
  1979. } else {
  1980. state.ubo_data.bg_energy = 1.0;
  1981. state.ubo_data.ambient_energy = 1.0;
  1982. //use from clear color instead, since there is no ambient
  1983. Color linear_ambient_color = storage->frame.clear_request_color.to_linear();
  1984. state.ubo_data.ambient_light_color[0] = linear_ambient_color.r;
  1985. state.ubo_data.ambient_light_color[1] = linear_ambient_color.g;
  1986. state.ubo_data.ambient_light_color[2] = linear_ambient_color.b;
  1987. state.ubo_data.ambient_light_color[3] = linear_ambient_color.a;
  1988. state.ubo_data.bg_color[0] = linear_ambient_color.r;
  1989. state.ubo_data.bg_color[1] = linear_ambient_color.g;
  1990. state.ubo_data.bg_color[2] = linear_ambient_color.b;
  1991. state.ubo_data.bg_color[3] = linear_ambient_color.a;
  1992. state.env_radiance_data.ambient_contribution = 0;
  1993. state.ubo_data.ambient_occlusion_affect_light = 0;
  1994. state.ubo_data.fog_color_enabled[3] = 0.0;
  1995. }
  1996. {
  1997. //directional shadow
  1998. state.ubo_data.shadow_directional_pixel_size[0] = 1.0 / directional_shadow.size;
  1999. state.ubo_data.shadow_directional_pixel_size[1] = 1.0 / directional_shadow.size;
  2000. glActiveTexture(GL_TEXTURE0 + storage->config.max_texture_image_units - 4);
  2001. glBindTexture(GL_TEXTURE_2D, directional_shadow.depth);
  2002. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE, GL_COMPARE_REF_TO_TEXTURE);
  2003. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_FUNC, GL_LESS);
  2004. }
  2005. glBindBuffer(GL_UNIFORM_BUFFER, state.scene_ubo);
  2006. glBufferSubData(GL_UNIFORM_BUFFER, 0, sizeof(State::SceneDataUBO), &state.ubo_data);
  2007. glBindBuffer(GL_UNIFORM_BUFFER, 0);
  2008. //fill up environment
  2009. store_transform(sky_orientation * p_cam_transform, state.env_radiance_data.transform);
  2010. glBindBuffer(GL_UNIFORM_BUFFER, state.env_radiance_ubo);
  2011. glBufferSubData(GL_UNIFORM_BUFFER, 0, sizeof(State::EnvironmentRadianceUBO), &state.env_radiance_data);
  2012. glBindBuffer(GL_UNIFORM_BUFFER, 0);
  2013. }
  2014. void RasterizerSceneGLES3::_setup_directional_light(int p_index, const Transform &p_camera_inverse_transform, bool p_use_shadows) {
  2015. LightInstance *li = directional_lights[p_index];
  2016. LightDataUBO ubo_data; //used for filling
  2017. float sign = li->light_ptr->negative ? -1 : 1;
  2018. Color linear_col = li->light_ptr->color.to_linear();
  2019. //compensate normalized diffuse range by multiplying by PI
  2020. ubo_data.light_color_energy[0] = linear_col.r * sign * li->light_ptr->param[VS::LIGHT_PARAM_ENERGY] * Math_PI;
  2021. ubo_data.light_color_energy[1] = linear_col.g * sign * li->light_ptr->param[VS::LIGHT_PARAM_ENERGY] * Math_PI;
  2022. ubo_data.light_color_energy[2] = linear_col.b * sign * li->light_ptr->param[VS::LIGHT_PARAM_ENERGY] * Math_PI;
  2023. ubo_data.light_color_energy[3] = 0;
  2024. //omni, keep at 0
  2025. ubo_data.light_pos_inv_radius[0] = 0.0;
  2026. ubo_data.light_pos_inv_radius[1] = 0.0;
  2027. ubo_data.light_pos_inv_radius[2] = 0.0;
  2028. ubo_data.light_pos_inv_radius[3] = 0.0;
  2029. Vector3 direction = p_camera_inverse_transform.basis.xform(li->transform.basis.xform(Vector3(0, 0, -1))).normalized();
  2030. ubo_data.light_direction_attenuation[0] = direction.x;
  2031. ubo_data.light_direction_attenuation[1] = direction.y;
  2032. ubo_data.light_direction_attenuation[2] = direction.z;
  2033. ubo_data.light_direction_attenuation[3] = 1.0;
  2034. ubo_data.light_params[0] = 0;
  2035. ubo_data.light_params[1] = 0;
  2036. ubo_data.light_params[2] = li->light_ptr->param[VS::LIGHT_PARAM_SPECULAR];
  2037. ubo_data.light_params[3] = 0;
  2038. Color shadow_color = li->light_ptr->shadow_color.to_linear();
  2039. ubo_data.light_shadow_color_contact[0] = shadow_color.r;
  2040. ubo_data.light_shadow_color_contact[1] = shadow_color.g;
  2041. ubo_data.light_shadow_color_contact[2] = shadow_color.b;
  2042. ubo_data.light_shadow_color_contact[3] = li->light_ptr->param[VS::LIGHT_PARAM_CONTACT_SHADOW_SIZE];
  2043. if (p_use_shadows && li->light_ptr->shadow) {
  2044. int shadow_count = 0;
  2045. switch (li->light_ptr->directional_shadow_mode) {
  2046. case VS::LIGHT_DIRECTIONAL_SHADOW_ORTHOGONAL: {
  2047. shadow_count = 1;
  2048. } break;
  2049. case VS::LIGHT_DIRECTIONAL_SHADOW_PARALLEL_2_SPLITS: {
  2050. shadow_count = 2;
  2051. } break;
  2052. case VS::LIGHT_DIRECTIONAL_SHADOW_PARALLEL_4_SPLITS: {
  2053. shadow_count = 4;
  2054. } break;
  2055. }
  2056. for (int j = 0; j < shadow_count; j++) {
  2057. uint32_t x = li->directional_rect.position.x;
  2058. uint32_t y = li->directional_rect.position.y;
  2059. uint32_t width = li->directional_rect.size.x;
  2060. uint32_t height = li->directional_rect.size.y;
  2061. if (li->light_ptr->directional_shadow_mode == VS::LIGHT_DIRECTIONAL_SHADOW_PARALLEL_4_SPLITS) {
  2062. width /= 2;
  2063. height /= 2;
  2064. if (j == 0) {
  2065. } else if (j == 1) {
  2066. x += width;
  2067. } else if (j == 2) {
  2068. y += height;
  2069. } else if (j == 3) {
  2070. x += width;
  2071. y += height;
  2072. }
  2073. } else if (li->light_ptr->directional_shadow_mode == VS::LIGHT_DIRECTIONAL_SHADOW_PARALLEL_2_SPLITS) {
  2074. height /= 2;
  2075. if (j == 0) {
  2076. } else {
  2077. y += height;
  2078. }
  2079. }
  2080. ubo_data.shadow_split_offsets[j] = li->shadow_transform[j].split;
  2081. Transform modelview = (p_camera_inverse_transform * li->shadow_transform[j].transform).affine_inverse();
  2082. CameraMatrix bias;
  2083. bias.set_light_bias();
  2084. CameraMatrix rectm;
  2085. Rect2 atlas_rect = Rect2(float(x) / directional_shadow.size, float(y) / directional_shadow.size, float(width) / directional_shadow.size, float(height) / directional_shadow.size);
  2086. rectm.set_light_atlas_rect(atlas_rect);
  2087. CameraMatrix shadow_mtx = rectm * bias * li->shadow_transform[j].camera * modelview;
  2088. store_camera(shadow_mtx, &ubo_data.shadow_matrix1[16 * j]);
  2089. ubo_data.light_clamp[0] = atlas_rect.position.x;
  2090. ubo_data.light_clamp[1] = atlas_rect.position.y;
  2091. ubo_data.light_clamp[2] = atlas_rect.size.x;
  2092. ubo_data.light_clamp[3] = atlas_rect.size.y;
  2093. }
  2094. }
  2095. glBindBuffer(GL_UNIFORM_BUFFER, state.directional_ubo);
  2096. glBufferSubData(GL_UNIFORM_BUFFER, 0, sizeof(LightDataUBO), &ubo_data);
  2097. glBindBuffer(GL_UNIFORM_BUFFER, 0);
  2098. directional_light = li;
  2099. glBindBufferBase(GL_UNIFORM_BUFFER, 3, state.directional_ubo);
  2100. }
  2101. 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) {
  2102. state.omni_light_count = 0;
  2103. state.spot_light_count = 0;
  2104. state.directional_light_count = 0;
  2105. directional_light = NULL;
  2106. ShadowAtlas *shadow_atlas = shadow_atlas_owner.getornull(p_shadow_atlas);
  2107. for (int i = 0; i < p_light_cull_count; i++) {
  2108. ERR_BREAK(i >= RenderList::MAX_LIGHTS);
  2109. LightInstance *li = light_instance_owner.getptr(p_light_cull_result[i]);
  2110. LightDataUBO ubo_data; //used for filling
  2111. switch (li->light_ptr->type) {
  2112. case VS::LIGHT_DIRECTIONAL: {
  2113. if (state.directional_light_count < RenderList::MAX_DIRECTIONAL_LIGHTS) {
  2114. directional_lights[state.directional_light_count++] = li;
  2115. }
  2116. } break;
  2117. case VS::LIGHT_OMNI: {
  2118. float sign = li->light_ptr->negative ? -1 : 1;
  2119. Color linear_col = li->light_ptr->color.to_linear();
  2120. ubo_data.light_color_energy[0] = linear_col.r * sign * li->light_ptr->param[VS::LIGHT_PARAM_ENERGY] * Math_PI;
  2121. ubo_data.light_color_energy[1] = linear_col.g * sign * li->light_ptr->param[VS::LIGHT_PARAM_ENERGY] * Math_PI;
  2122. ubo_data.light_color_energy[2] = linear_col.b * sign * li->light_ptr->param[VS::LIGHT_PARAM_ENERGY] * Math_PI;
  2123. ubo_data.light_color_energy[3] = 0;
  2124. Vector3 pos = p_camera_inverse_transform.xform(li->transform.origin);
  2125. //directional, keep at 0
  2126. ubo_data.light_pos_inv_radius[0] = pos.x;
  2127. ubo_data.light_pos_inv_radius[1] = pos.y;
  2128. ubo_data.light_pos_inv_radius[2] = pos.z;
  2129. ubo_data.light_pos_inv_radius[3] = 1.0 / MAX(0.001, li->light_ptr->param[VS::LIGHT_PARAM_RANGE]);
  2130. ubo_data.light_direction_attenuation[0] = 0;
  2131. ubo_data.light_direction_attenuation[1] = 0;
  2132. ubo_data.light_direction_attenuation[2] = 0;
  2133. ubo_data.light_direction_attenuation[3] = li->light_ptr->param[VS::LIGHT_PARAM_ATTENUATION];
  2134. ubo_data.light_params[0] = 0;
  2135. ubo_data.light_params[1] = 0;
  2136. ubo_data.light_params[2] = li->light_ptr->param[VS::LIGHT_PARAM_SPECULAR];
  2137. ubo_data.light_params[3] = 0;
  2138. Color shadow_color = li->light_ptr->shadow_color.to_linear();
  2139. ubo_data.light_shadow_color_contact[0] = shadow_color.r;
  2140. ubo_data.light_shadow_color_contact[1] = shadow_color.g;
  2141. ubo_data.light_shadow_color_contact[2] = shadow_color.b;
  2142. ubo_data.light_shadow_color_contact[3] = li->light_ptr->param[VS::LIGHT_PARAM_CONTACT_SHADOW_SIZE];
  2143. if (li->light_ptr->shadow && shadow_atlas && shadow_atlas->shadow_owners.has(li->self)) {
  2144. // fill in the shadow information
  2145. uint32_t key = shadow_atlas->shadow_owners[li->self];
  2146. uint32_t quadrant = (key >> ShadowAtlas::QUADRANT_SHIFT) & 0x3;
  2147. uint32_t shadow = key & ShadowAtlas::SHADOW_INDEX_MASK;
  2148. ERR_CONTINUE(shadow >= (uint32_t)shadow_atlas->quadrants[quadrant].shadows.size());
  2149. uint32_t atlas_size = shadow_atlas->size;
  2150. uint32_t quadrant_size = atlas_size >> 1;
  2151. uint32_t x = (quadrant & 1) * quadrant_size;
  2152. uint32_t y = (quadrant >> 1) * quadrant_size;
  2153. uint32_t shadow_size = (quadrant_size / shadow_atlas->quadrants[quadrant].subdivision);
  2154. x += (shadow % shadow_atlas->quadrants[quadrant].subdivision) * shadow_size;
  2155. y += (shadow / shadow_atlas->quadrants[quadrant].subdivision) * shadow_size;
  2156. uint32_t width = shadow_size;
  2157. uint32_t height = shadow_size;
  2158. if (li->light_ptr->omni_shadow_detail == VS::LIGHT_OMNI_SHADOW_DETAIL_HORIZONTAL) {
  2159. height /= 2;
  2160. } else {
  2161. width /= 2;
  2162. }
  2163. Transform proj = (p_camera_inverse_transform * li->transform).inverse();
  2164. store_transform(proj, ubo_data.shadow_matrix1);
  2165. ubo_data.light_params[3] = 1.0; //means it has shadow
  2166. ubo_data.light_clamp[0] = float(x) / atlas_size;
  2167. ubo_data.light_clamp[1] = float(y) / atlas_size;
  2168. ubo_data.light_clamp[2] = float(width) / atlas_size;
  2169. ubo_data.light_clamp[3] = float(height) / atlas_size;
  2170. }
  2171. li->light_index = state.omni_light_count;
  2172. copymem(&state.omni_array_tmp[li->light_index * state.ubo_light_size], &ubo_data, state.ubo_light_size);
  2173. state.omni_light_count++;
  2174. } break;
  2175. case VS::LIGHT_SPOT: {
  2176. float sign = li->light_ptr->negative ? -1 : 1;
  2177. Color linear_col = li->light_ptr->color.to_linear();
  2178. ubo_data.light_color_energy[0] = linear_col.r * sign * li->light_ptr->param[VS::LIGHT_PARAM_ENERGY] * Math_PI;
  2179. ubo_data.light_color_energy[1] = linear_col.g * sign * li->light_ptr->param[VS::LIGHT_PARAM_ENERGY] * Math_PI;
  2180. ubo_data.light_color_energy[2] = linear_col.b * sign * li->light_ptr->param[VS::LIGHT_PARAM_ENERGY] * Math_PI;
  2181. ubo_data.light_color_energy[3] = 0;
  2182. Vector3 pos = p_camera_inverse_transform.xform(li->transform.origin);
  2183. //directional, keep at 0
  2184. ubo_data.light_pos_inv_radius[0] = pos.x;
  2185. ubo_data.light_pos_inv_radius[1] = pos.y;
  2186. ubo_data.light_pos_inv_radius[2] = pos.z;
  2187. ubo_data.light_pos_inv_radius[3] = 1.0 / MAX(0.001, li->light_ptr->param[VS::LIGHT_PARAM_RANGE]);
  2188. Vector3 direction = p_camera_inverse_transform.basis.xform(li->transform.basis.xform(Vector3(0, 0, -1))).normalized();
  2189. ubo_data.light_direction_attenuation[0] = direction.x;
  2190. ubo_data.light_direction_attenuation[1] = direction.y;
  2191. ubo_data.light_direction_attenuation[2] = direction.z;
  2192. ubo_data.light_direction_attenuation[3] = li->light_ptr->param[VS::LIGHT_PARAM_ATTENUATION];
  2193. ubo_data.light_params[0] = li->light_ptr->param[VS::LIGHT_PARAM_SPOT_ATTENUATION];
  2194. ubo_data.light_params[1] = Math::cos(Math::deg2rad(li->light_ptr->param[VS::LIGHT_PARAM_SPOT_ANGLE]));
  2195. ubo_data.light_params[2] = li->light_ptr->param[VS::LIGHT_PARAM_SPECULAR];
  2196. ubo_data.light_params[3] = 0;
  2197. Color shadow_color = li->light_ptr->shadow_color.to_linear();
  2198. ubo_data.light_shadow_color_contact[0] = shadow_color.r;
  2199. ubo_data.light_shadow_color_contact[1] = shadow_color.g;
  2200. ubo_data.light_shadow_color_contact[2] = shadow_color.b;
  2201. ubo_data.light_shadow_color_contact[3] = li->light_ptr->param[VS::LIGHT_PARAM_CONTACT_SHADOW_SIZE];
  2202. if (li->light_ptr->shadow && shadow_atlas && shadow_atlas->shadow_owners.has(li->self)) {
  2203. // fill in the shadow information
  2204. uint32_t key = shadow_atlas->shadow_owners[li->self];
  2205. uint32_t quadrant = (key >> ShadowAtlas::QUADRANT_SHIFT) & 0x3;
  2206. uint32_t shadow = key & ShadowAtlas::SHADOW_INDEX_MASK;
  2207. ERR_CONTINUE(shadow >= (uint32_t)shadow_atlas->quadrants[quadrant].shadows.size());
  2208. uint32_t atlas_size = shadow_atlas->size;
  2209. uint32_t quadrant_size = atlas_size >> 1;
  2210. uint32_t x = (quadrant & 1) * quadrant_size;
  2211. uint32_t y = (quadrant >> 1) * quadrant_size;
  2212. uint32_t shadow_size = (quadrant_size / shadow_atlas->quadrants[quadrant].subdivision);
  2213. x += (shadow % shadow_atlas->quadrants[quadrant].subdivision) * shadow_size;
  2214. y += (shadow / shadow_atlas->quadrants[quadrant].subdivision) * shadow_size;
  2215. uint32_t width = shadow_size;
  2216. uint32_t height = shadow_size;
  2217. Rect2 rect(float(x) / atlas_size, float(y) / atlas_size, float(width) / atlas_size, float(height) / atlas_size);
  2218. ubo_data.light_params[3] = 1.0; //means it has shadow
  2219. ubo_data.light_clamp[0] = rect.position.x;
  2220. ubo_data.light_clamp[1] = rect.position.y;
  2221. ubo_data.light_clamp[2] = rect.size.x;
  2222. ubo_data.light_clamp[3] = rect.size.y;
  2223. Transform modelview = (p_camera_inverse_transform * li->transform).inverse();
  2224. CameraMatrix bias;
  2225. bias.set_light_bias();
  2226. CameraMatrix rectm;
  2227. rectm.set_light_atlas_rect(rect);
  2228. CameraMatrix shadow_mtx = rectm * bias * li->shadow_transform[0].camera * modelview;
  2229. store_camera(shadow_mtx, ubo_data.shadow_matrix1);
  2230. }
  2231. li->light_index = state.spot_light_count;
  2232. copymem(&state.spot_array_tmp[li->light_index * state.ubo_light_size], &ubo_data, state.ubo_light_size);
  2233. state.spot_light_count++;
  2234. #if 0
  2235. if (li->light_ptr->shadow_enabled) {
  2236. CameraMatrix bias;
  2237. bias.set_light_bias();
  2238. Transform modelview=Transform(camera_transform_inverse * li->transform).inverse();
  2239. li->shadow_projection[0] = bias * li->projection * modelview;
  2240. lights_use_shadow=true;
  2241. }
  2242. #endif
  2243. } break;
  2244. }
  2245. li->last_pass = render_pass;
  2246. //update UBO for forward rendering, blit to texture for clustered
  2247. }
  2248. if (state.omni_light_count) {
  2249. glBindBuffer(GL_UNIFORM_BUFFER, state.omni_array_ubo);
  2250. glBufferSubData(GL_UNIFORM_BUFFER, 0, state.omni_light_count * state.ubo_light_size, state.omni_array_tmp);
  2251. glBindBuffer(GL_UNIFORM_BUFFER, 0);
  2252. }
  2253. glBindBufferBase(GL_UNIFORM_BUFFER, 4, state.omni_array_ubo);
  2254. if (state.spot_light_count) {
  2255. glBindBuffer(GL_UNIFORM_BUFFER, state.spot_array_ubo);
  2256. glBufferSubData(GL_UNIFORM_BUFFER, 0, state.spot_light_count * state.ubo_light_size, state.spot_array_tmp);
  2257. glBindBuffer(GL_UNIFORM_BUFFER, 0);
  2258. }
  2259. glBindBufferBase(GL_UNIFORM_BUFFER, 5, state.spot_array_ubo);
  2260. }
  2261. 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) {
  2262. state.reflection_probe_count = 0;
  2263. for (int i = 0; i < p_reflection_probe_cull_count; i++) {
  2264. ReflectionProbeInstance *rpi = reflection_probe_instance_owner.getornull(p_reflection_probe_cull_result[i]);
  2265. ERR_CONTINUE(!rpi);
  2266. ReflectionAtlas *reflection_atlas = reflection_atlas_owner.getornull(p_reflection_atlas);
  2267. ERR_CONTINUE(!reflection_atlas);
  2268. ERR_CONTINUE(rpi->reflection_atlas_index < 0);
  2269. if (state.reflection_probe_count >= state.max_ubo_reflections)
  2270. break;
  2271. rpi->last_pass = render_pass;
  2272. ReflectionProbeDataUBO reflection_ubo;
  2273. reflection_ubo.box_extents[0] = rpi->probe_ptr->extents.x;
  2274. reflection_ubo.box_extents[1] = rpi->probe_ptr->extents.y;
  2275. reflection_ubo.box_extents[2] = rpi->probe_ptr->extents.z;
  2276. reflection_ubo.box_extents[3] = 0;
  2277. reflection_ubo.box_ofs[0] = rpi->probe_ptr->origin_offset.x;
  2278. reflection_ubo.box_ofs[1] = rpi->probe_ptr->origin_offset.y;
  2279. reflection_ubo.box_ofs[2] = rpi->probe_ptr->origin_offset.z;
  2280. reflection_ubo.box_ofs[3] = 0;
  2281. reflection_ubo.params[0] = rpi->probe_ptr->intensity;
  2282. reflection_ubo.params[1] = 0;
  2283. reflection_ubo.params[2] = rpi->probe_ptr->interior ? 1.0 : 0.0;
  2284. reflection_ubo.params[3] = rpi->probe_ptr->box_projection ? 1.0 : 0.0;
  2285. if (rpi->probe_ptr->interior) {
  2286. Color ambient_linear = rpi->probe_ptr->interior_ambient.to_linear();
  2287. reflection_ubo.ambient[0] = ambient_linear.r * rpi->probe_ptr->interior_ambient_energy;
  2288. reflection_ubo.ambient[1] = ambient_linear.g * rpi->probe_ptr->interior_ambient_energy;
  2289. reflection_ubo.ambient[2] = ambient_linear.b * rpi->probe_ptr->interior_ambient_energy;
  2290. reflection_ubo.ambient[3] = rpi->probe_ptr->interior_ambient_probe_contrib;
  2291. } else {
  2292. Color ambient_linear;
  2293. if (p_env) {
  2294. ambient_linear = p_env->ambient_color.to_linear();
  2295. ambient_linear.r *= p_env->ambient_energy;
  2296. ambient_linear.g *= p_env->ambient_energy;
  2297. ambient_linear.b *= p_env->ambient_energy;
  2298. }
  2299. reflection_ubo.ambient[0] = ambient_linear.r;
  2300. reflection_ubo.ambient[1] = ambient_linear.g;
  2301. reflection_ubo.ambient[2] = ambient_linear.b;
  2302. reflection_ubo.ambient[3] = 0; //not used in exterior mode, since it just blends with regular ambient light
  2303. }
  2304. int cell_size = reflection_atlas->size / reflection_atlas->subdiv;
  2305. int x = (rpi->reflection_atlas_index % reflection_atlas->subdiv) * cell_size;
  2306. int y = (rpi->reflection_atlas_index / reflection_atlas->subdiv) * cell_size;
  2307. int width = cell_size;
  2308. int height = cell_size;
  2309. reflection_ubo.atlas_clamp[0] = float(x) / reflection_atlas->size;
  2310. reflection_ubo.atlas_clamp[1] = float(y) / reflection_atlas->size;
  2311. reflection_ubo.atlas_clamp[2] = float(width) / reflection_atlas->size;
  2312. reflection_ubo.atlas_clamp[3] = float(height) / reflection_atlas->size;
  2313. Transform proj = (p_camera_inverse_transform * rpi->transform).inverse();
  2314. store_transform(proj, reflection_ubo.local_matrix);
  2315. rpi->reflection_index = state.reflection_probe_count;
  2316. copymem(&state.reflection_array_tmp[rpi->reflection_index * sizeof(ReflectionProbeDataUBO)], &reflection_ubo, sizeof(ReflectionProbeDataUBO));
  2317. state.reflection_probe_count++;
  2318. }
  2319. if (state.reflection_probe_count) {
  2320. glBindBuffer(GL_UNIFORM_BUFFER, state.reflection_array_ubo);
  2321. glBufferSubData(GL_UNIFORM_BUFFER, 0, state.reflection_probe_count * sizeof(ReflectionProbeDataUBO), state.reflection_array_tmp);
  2322. glBindBuffer(GL_UNIFORM_BUFFER, 0);
  2323. }
  2324. glBindBufferBase(GL_UNIFORM_BUFFER, 6, state.reflection_array_ubo);
  2325. }
  2326. void RasterizerSceneGLES3::_copy_screen(bool p_invalidate_color, bool p_invalidate_depth) {
  2327. #ifndef GLES_OVER_GL
  2328. if (p_invalidate_color) {
  2329. GLenum attachments[2] = {
  2330. GL_COLOR_ATTACHMENT0,
  2331. GL_DEPTH_STENCIL_ATTACHMENT
  2332. };
  2333. glInvalidateFramebuffer(GL_FRAMEBUFFER, p_invalidate_depth ? 2 : 1, attachments);
  2334. }
  2335. #endif
  2336. glBindVertexArray(storage->resources.quadie_array);
  2337. glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
  2338. glBindVertexArray(0);
  2339. }
  2340. void RasterizerSceneGLES3::_copy_texture_to_front_buffer(GLuint p_texture) {
  2341. //copy to front buffer
  2342. glBindFramebuffer(GL_FRAMEBUFFER, storage->frame.current_rt->fbo);
  2343. glDepthMask(GL_FALSE);
  2344. glDisable(GL_DEPTH_TEST);
  2345. glDisable(GL_CULL_FACE);
  2346. glDisable(GL_BLEND);
  2347. glDepthFunc(GL_LEQUAL);
  2348. glColorMask(1, 1, 1, 1);
  2349. glActiveTexture(GL_TEXTURE0);
  2350. glBindTexture(GL_TEXTURE_2D, p_texture);
  2351. glViewport(0, 0, storage->frame.current_rt->width * 0.5, storage->frame.current_rt->height * 0.5);
  2352. storage->shaders.copy.set_conditional(CopyShaderGLES3::DISABLE_ALPHA, true);
  2353. storage->shaders.copy.bind();
  2354. _copy_screen();
  2355. //turn off everything used
  2356. storage->shaders.copy.set_conditional(CopyShaderGLES3::LINEAR_TO_SRGB, false);
  2357. storage->shaders.copy.set_conditional(CopyShaderGLES3::DISABLE_ALPHA, false);
  2358. }
  2359. void RasterizerSceneGLES3::_fill_render_list(InstanceBase **p_cull_result, int p_cull_count, bool p_depth_pass, bool p_shadow_pass) {
  2360. current_geometry_index = 0;
  2361. current_material_index = 0;
  2362. state.used_sss = false;
  2363. state.used_screen_texture = false;
  2364. //fill list
  2365. for (int i = 0; i < p_cull_count; i++) {
  2366. InstanceBase *inst = p_cull_result[i];
  2367. switch (inst->base_type) {
  2368. case VS::INSTANCE_MESH: {
  2369. RasterizerStorageGLES3::Mesh *mesh = storage->mesh_owner.getptr(inst->base);
  2370. ERR_CONTINUE(!mesh);
  2371. int ssize = mesh->surfaces.size();
  2372. for (int i = 0; i < ssize; i++) {
  2373. int mat_idx = inst->materials[i].is_valid() ? i : -1;
  2374. RasterizerStorageGLES3::Surface *s = mesh->surfaces[i];
  2375. _add_geometry(s, inst, NULL, mat_idx, p_depth_pass, p_shadow_pass);
  2376. }
  2377. //mesh->last_pass=frame;
  2378. } break;
  2379. case VS::INSTANCE_MULTIMESH: {
  2380. RasterizerStorageGLES3::MultiMesh *multi_mesh = storage->multimesh_owner.getptr(inst->base);
  2381. ERR_CONTINUE(!multi_mesh);
  2382. if (multi_mesh->size == 0 || multi_mesh->visible_instances == 0)
  2383. continue;
  2384. RasterizerStorageGLES3::Mesh *mesh = storage->mesh_owner.getptr(multi_mesh->mesh);
  2385. if (!mesh)
  2386. continue; //mesh not assigned
  2387. int ssize = mesh->surfaces.size();
  2388. for (int i = 0; i < ssize; i++) {
  2389. RasterizerStorageGLES3::Surface *s = mesh->surfaces[i];
  2390. _add_geometry(s, inst, multi_mesh, -1, p_depth_pass, p_shadow_pass);
  2391. }
  2392. } break;
  2393. case VS::INSTANCE_IMMEDIATE: {
  2394. RasterizerStorageGLES3::Immediate *immediate = storage->immediate_owner.getptr(inst->base);
  2395. ERR_CONTINUE(!immediate);
  2396. _add_geometry(immediate, inst, NULL, -1, p_depth_pass, p_shadow_pass);
  2397. } break;
  2398. case VS::INSTANCE_PARTICLES: {
  2399. RasterizerStorageGLES3::Particles *particles = storage->particles_owner.getptr(inst->base);
  2400. ERR_CONTINUE(!particles);
  2401. for (int i = 0; i < particles->draw_passes.size(); i++) {
  2402. RID pmesh = particles->draw_passes[i];
  2403. if (!pmesh.is_valid())
  2404. continue;
  2405. RasterizerStorageGLES3::Mesh *mesh = storage->mesh_owner.get(pmesh);
  2406. if (!mesh)
  2407. continue; //mesh not assigned
  2408. int ssize = mesh->surfaces.size();
  2409. for (int j = 0; j < ssize; j++) {
  2410. RasterizerStorageGLES3::Surface *s = mesh->surfaces[j];
  2411. _add_geometry(s, inst, particles, -1, p_depth_pass, p_shadow_pass);
  2412. }
  2413. }
  2414. } break;
  2415. default: {}
  2416. }
  2417. }
  2418. }
  2419. void RasterizerSceneGLES3::_blur_effect_buffer() {
  2420. //blur diffuse into effect mipmaps using separatable convolution
  2421. //storage->shaders.copy.set_conditional(CopyShaderGLES3::GAUSSIAN_HORIZONTAL,true);
  2422. for (int i = 0; i < storage->frame.current_rt->effects.mip_maps[1].sizes.size(); i++) {
  2423. int vp_w = storage->frame.current_rt->effects.mip_maps[1].sizes[i].width;
  2424. int vp_h = storage->frame.current_rt->effects.mip_maps[1].sizes[i].height;
  2425. glViewport(0, 0, vp_w, vp_h);
  2426. //horizontal pass
  2427. state.effect_blur_shader.set_conditional(EffectBlurShaderGLES3::GAUSSIAN_HORIZONTAL, true);
  2428. state.effect_blur_shader.bind();
  2429. state.effect_blur_shader.set_uniform(EffectBlurShaderGLES3::PIXEL_SIZE, Vector2(1.0 / vp_w, 1.0 / vp_h));
  2430. state.effect_blur_shader.set_uniform(EffectBlurShaderGLES3::LOD, float(i));
  2431. glActiveTexture(GL_TEXTURE0);
  2432. glBindTexture(GL_TEXTURE_2D, storage->frame.current_rt->effects.mip_maps[0].color); //previous level, since mipmaps[0] starts one level bigger
  2433. glBindFramebuffer(GL_FRAMEBUFFER, storage->frame.current_rt->effects.mip_maps[1].sizes[i].fbo);
  2434. _copy_screen(true);
  2435. state.effect_blur_shader.set_conditional(EffectBlurShaderGLES3::GAUSSIAN_HORIZONTAL, false);
  2436. //vertical pass
  2437. state.effect_blur_shader.set_conditional(EffectBlurShaderGLES3::GAUSSIAN_VERTICAL, true);
  2438. state.effect_blur_shader.bind();
  2439. state.effect_blur_shader.set_uniform(EffectBlurShaderGLES3::PIXEL_SIZE, Vector2(1.0 / vp_w, 1.0 / vp_h));
  2440. state.effect_blur_shader.set_uniform(EffectBlurShaderGLES3::LOD, float(i));
  2441. glActiveTexture(GL_TEXTURE0);
  2442. glBindTexture(GL_TEXTURE_2D, storage->frame.current_rt->effects.mip_maps[1].color);
  2443. glBindFramebuffer(GL_FRAMEBUFFER, storage->frame.current_rt->effects.mip_maps[0].sizes[i + 1].fbo); //next level, since mipmaps[0] starts one level bigger
  2444. _copy_screen(true);
  2445. state.effect_blur_shader.set_conditional(EffectBlurShaderGLES3::GAUSSIAN_VERTICAL, false);
  2446. }
  2447. }
  2448. void RasterizerSceneGLES3::_render_mrts(Environment *env, const CameraMatrix &p_cam_projection) {
  2449. glDepthMask(GL_FALSE);
  2450. glDisable(GL_DEPTH_TEST);
  2451. glDisable(GL_CULL_FACE);
  2452. glDisable(GL_BLEND);
  2453. if (env->ssao_enabled || env->ssr_enabled) {
  2454. //copy normal and roughness to effect buffer
  2455. glBindFramebuffer(GL_READ_FRAMEBUFFER, storage->frame.current_rt->buffers.fbo);
  2456. glReadBuffer(GL_COLOR_ATTACHMENT2);
  2457. glBindFramebuffer(GL_DRAW_FRAMEBUFFER, storage->frame.current_rt->buffers.effect_fbo);
  2458. 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);
  2459. }
  2460. if (env->ssao_enabled) {
  2461. //copy diffuse to front buffer
  2462. glBindFramebuffer(GL_READ_FRAMEBUFFER, storage->frame.current_rt->buffers.fbo);
  2463. glReadBuffer(GL_COLOR_ATTACHMENT0);
  2464. glBindFramebuffer(GL_DRAW_FRAMEBUFFER, storage->frame.current_rt->fbo);
  2465. 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);
  2466. glBindFramebuffer(GL_READ_FRAMEBUFFER, 0);
  2467. glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0);
  2468. //copy from depth, convert to linear
  2469. GLint ss[2];
  2470. ss[0] = storage->frame.current_rt->width;
  2471. ss[1] = storage->frame.current_rt->height;
  2472. for (int i = 0; i < storage->frame.current_rt->effects.ssao.depth_mipmap_fbos.size(); i++) {
  2473. state.ssao_minify_shader.set_conditional(SsaoMinifyShaderGLES3::MINIFY_START, i == 0);
  2474. state.ssao_minify_shader.set_conditional(SsaoMinifyShaderGLES3::USE_ORTHOGONAL_PROJECTION, p_cam_projection.is_orthogonal());
  2475. state.ssao_minify_shader.bind();
  2476. state.ssao_minify_shader.set_uniform(SsaoMinifyShaderGLES3::CAMERA_Z_FAR, p_cam_projection.get_z_far());
  2477. state.ssao_minify_shader.set_uniform(SsaoMinifyShaderGLES3::CAMERA_Z_NEAR, p_cam_projection.get_z_near());
  2478. state.ssao_minify_shader.set_uniform(SsaoMinifyShaderGLES3::SOURCE_MIPMAP, MAX(0, i - 1));
  2479. glUniform2iv(state.ssao_minify_shader.get_uniform(SsaoMinifyShaderGLES3::FROM_SIZE), 1, ss);
  2480. ss[0] >>= 1;
  2481. ss[1] >>= 1;
  2482. glActiveTexture(GL_TEXTURE0);
  2483. if (i == 0) {
  2484. glBindTexture(GL_TEXTURE_2D, storage->frame.current_rt->depth);
  2485. } else {
  2486. glBindTexture(GL_TEXTURE_2D, storage->frame.current_rt->effects.ssao.linear_depth);
  2487. }
  2488. glBindFramebuffer(GL_FRAMEBUFFER, storage->frame.current_rt->effects.ssao.depth_mipmap_fbos[i]); //copy to front first
  2489. glViewport(0, 0, ss[0], ss[1]);
  2490. _copy_screen(true);
  2491. }
  2492. ss[0] = storage->frame.current_rt->width;
  2493. ss[1] = storage->frame.current_rt->height;
  2494. glViewport(0, 0, ss[0], ss[1]);
  2495. glEnable(GL_DEPTH_TEST);
  2496. glDepthFunc(GL_GREATER);
  2497. // do SSAO!
  2498. state.ssao_shader.set_conditional(SsaoShaderGLES3::ENABLE_RADIUS2, env->ssao_radius2 > 0.001);
  2499. state.ssao_shader.set_conditional(SsaoShaderGLES3::USE_ORTHOGONAL_PROJECTION, p_cam_projection.is_orthogonal());
  2500. state.ssao_shader.set_conditional(SsaoShaderGLES3::SSAO_QUALITY_LOW, env->ssao_quality == VS::ENV_SSAO_QUALITY_LOW);
  2501. state.ssao_shader.set_conditional(SsaoShaderGLES3::SSAO_QUALITY_HIGH, env->ssao_quality == VS::ENV_SSAO_QUALITY_HIGH);
  2502. state.ssao_shader.bind();
  2503. state.ssao_shader.set_uniform(SsaoShaderGLES3::CAMERA_Z_FAR, p_cam_projection.get_z_far());
  2504. state.ssao_shader.set_uniform(SsaoShaderGLES3::CAMERA_Z_NEAR, p_cam_projection.get_z_near());
  2505. glUniform2iv(state.ssao_shader.get_uniform(SsaoShaderGLES3::SCREEN_SIZE), 1, ss);
  2506. float radius = env->ssao_radius;
  2507. state.ssao_shader.set_uniform(SsaoShaderGLES3::RADIUS, radius);
  2508. float intensity = env->ssao_intensity;
  2509. state.ssao_shader.set_uniform(SsaoShaderGLES3::INTENSITY_DIV_R6, intensity / pow(radius, 6.0f));
  2510. if (env->ssao_radius2 > 0.001) {
  2511. float radius2 = env->ssao_radius2;
  2512. state.ssao_shader.set_uniform(SsaoShaderGLES3::RADIUS2, radius2);
  2513. float intensity2 = env->ssao_intensity2;
  2514. state.ssao_shader.set_uniform(SsaoShaderGLES3::INTENSITY_DIV_R62, intensity2 / pow(radius2, 6.0f));
  2515. }
  2516. float proj_info[4] = {
  2517. -2.0f / (ss[0] * p_cam_projection.matrix[0][0]),
  2518. -2.0f / (ss[1] * p_cam_projection.matrix[1][1]),
  2519. (1.0f - p_cam_projection.matrix[0][2]) / p_cam_projection.matrix[0][0],
  2520. (1.0f + p_cam_projection.matrix[1][2]) / p_cam_projection.matrix[1][1]
  2521. };
  2522. glUniform4fv(state.ssao_shader.get_uniform(SsaoShaderGLES3::PROJ_INFO), 1, proj_info);
  2523. float pixels_per_meter = float(p_cam_projection.get_pixels_per_meter(ss[0]));
  2524. state.ssao_shader.set_uniform(SsaoShaderGLES3::PROJ_SCALE, pixels_per_meter);
  2525. state.ssao_shader.set_uniform(SsaoShaderGLES3::BIAS, env->ssao_bias);
  2526. glActiveTexture(GL_TEXTURE0);
  2527. glBindTexture(GL_TEXTURE_2D, storage->frame.current_rt->depth);
  2528. glActiveTexture(GL_TEXTURE1);
  2529. glBindTexture(GL_TEXTURE_2D, storage->frame.current_rt->effects.ssao.linear_depth);
  2530. glActiveTexture(GL_TEXTURE2);
  2531. glBindTexture(GL_TEXTURE_2D, storage->frame.current_rt->buffers.effect);
  2532. glBindFramebuffer(GL_FRAMEBUFFER, storage->frame.current_rt->effects.ssao.blur_fbo[0]); //copy to front first
  2533. Color white(1, 1, 1, 1);
  2534. glClearBufferfv(GL_COLOR, 0, white.components); // specular
  2535. _copy_screen(true);
  2536. //do the batm, i mean blur
  2537. state.ssao_blur_shader.bind();
  2538. if (env->ssao_filter) {
  2539. for (int i = 0; i < 2; i++) {
  2540. state.ssao_blur_shader.set_uniform(SsaoBlurShaderGLES3::CAMERA_Z_FAR, p_cam_projection.get_z_far());
  2541. state.ssao_blur_shader.set_uniform(SsaoBlurShaderGLES3::CAMERA_Z_NEAR, p_cam_projection.get_z_near());
  2542. state.ssao_blur_shader.set_uniform(SsaoBlurShaderGLES3::EDGE_SHARPNESS, env->ssao_bilateral_sharpness);
  2543. state.ssao_blur_shader.set_uniform(SsaoBlurShaderGLES3::FILTER_SCALE, int(env->ssao_filter));
  2544. GLint axis[2] = { i, 1 - i };
  2545. glUniform2iv(state.ssao_blur_shader.get_uniform(SsaoBlurShaderGLES3::AXIS), 1, axis);
  2546. glUniform2iv(state.ssao_blur_shader.get_uniform(SsaoBlurShaderGLES3::SCREEN_SIZE), 1, ss);
  2547. glActiveTexture(GL_TEXTURE0);
  2548. glBindTexture(GL_TEXTURE_2D, storage->frame.current_rt->effects.ssao.blur_red[i]);
  2549. glActiveTexture(GL_TEXTURE1);
  2550. glBindTexture(GL_TEXTURE_2D, storage->frame.current_rt->depth);
  2551. glActiveTexture(GL_TEXTURE2);
  2552. glBindTexture(GL_TEXTURE_2D, storage->frame.current_rt->buffers.effect);
  2553. glBindFramebuffer(GL_FRAMEBUFFER, storage->frame.current_rt->effects.ssao.blur_fbo[1 - i]);
  2554. if (i == 0) {
  2555. glClearBufferfv(GL_COLOR, 0, white.components); // specular
  2556. }
  2557. _copy_screen(true);
  2558. }
  2559. }
  2560. glDisable(GL_DEPTH_TEST);
  2561. glDepthFunc(GL_LEQUAL);
  2562. // just copy diffuse while applying SSAO
  2563. state.effect_blur_shader.set_conditional(EffectBlurShaderGLES3::SSAO_MERGE, true);
  2564. state.effect_blur_shader.bind();
  2565. state.effect_blur_shader.set_uniform(EffectBlurShaderGLES3::SSAO_COLOR, env->ssao_color);
  2566. glActiveTexture(GL_TEXTURE0);
  2567. glBindTexture(GL_TEXTURE_2D, storage->frame.current_rt->color); //previous level, since mipmaps[0] starts one level bigger
  2568. glActiveTexture(GL_TEXTURE1);
  2569. glBindTexture(GL_TEXTURE_2D, storage->frame.current_rt->effects.ssao.blur_red[0]); //previous level, since mipmaps[0] starts one level bigger
  2570. glBindFramebuffer(GL_FRAMEBUFFER, storage->frame.current_rt->effects.mip_maps[0].sizes[0].fbo); // copy to base level
  2571. _copy_screen(true);
  2572. state.effect_blur_shader.set_conditional(EffectBlurShaderGLES3::SSAO_MERGE, false);
  2573. } else {
  2574. //copy diffuse to effect buffer
  2575. glBindFramebuffer(GL_READ_FRAMEBUFFER, storage->frame.current_rt->buffers.fbo);
  2576. glReadBuffer(GL_COLOR_ATTACHMENT0);
  2577. glBindFramebuffer(GL_DRAW_FRAMEBUFFER, storage->frame.current_rt->effects.mip_maps[0].sizes[0].fbo);
  2578. 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);
  2579. glBindFramebuffer(GL_READ_FRAMEBUFFER, 0);
  2580. glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0);
  2581. }
  2582. if (state.used_sss) { //sss enabled
  2583. //copy diffuse while performing sss
  2584. Plane p = p_cam_projection.xform4(Plane(1, 0, -1, 1));
  2585. p.normal /= p.d;
  2586. float unit_size = p.normal.x;
  2587. //copy normal and roughness to effect buffer
  2588. glBindFramebuffer(GL_READ_FRAMEBUFFER, storage->frame.current_rt->buffers.fbo);
  2589. glReadBuffer(GL_COLOR_ATTACHMENT3);
  2590. glBindFramebuffer(GL_DRAW_FRAMEBUFFER, storage->frame.current_rt->effects.ssao.blur_fbo[0]);
  2591. 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);
  2592. state.sss_shader.set_conditional(SubsurfScatteringShaderGLES3::USE_ORTHOGONAL_PROJECTION, p_cam_projection.is_orthogonal());
  2593. state.sss_shader.set_conditional(SubsurfScatteringShaderGLES3::USE_11_SAMPLES, subsurface_scatter_quality == SSS_QUALITY_LOW);
  2594. state.sss_shader.set_conditional(SubsurfScatteringShaderGLES3::USE_17_SAMPLES, subsurface_scatter_quality == SSS_QUALITY_MEDIUM);
  2595. state.sss_shader.set_conditional(SubsurfScatteringShaderGLES3::USE_25_SAMPLES, subsurface_scatter_quality == SSS_QUALITY_HIGH);
  2596. state.sss_shader.set_conditional(SubsurfScatteringShaderGLES3::ENABLE_FOLLOW_SURFACE, subsurface_scatter_follow_surface);
  2597. state.sss_shader.set_conditional(SubsurfScatteringShaderGLES3::ENABLE_STRENGTH_WEIGHTING, subsurface_scatter_weight_samples);
  2598. state.sss_shader.bind();
  2599. state.sss_shader.set_uniform(SubsurfScatteringShaderGLES3::MAX_RADIUS, subsurface_scatter_size);
  2600. state.sss_shader.set_uniform(SubsurfScatteringShaderGLES3::UNIT_SIZE, unit_size);
  2601. state.sss_shader.set_uniform(SubsurfScatteringShaderGLES3::CAMERA_Z_NEAR, p_cam_projection.get_z_near());
  2602. state.sss_shader.set_uniform(SubsurfScatteringShaderGLES3::CAMERA_Z_FAR, p_cam_projection.get_z_far());
  2603. state.sss_shader.set_uniform(SubsurfScatteringShaderGLES3::DIR, Vector2(1, 0));
  2604. glActiveTexture(GL_TEXTURE0);
  2605. glBindTexture(GL_TEXTURE_2D, storage->frame.current_rt->effects.mip_maps[0].color);
  2606. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); //disable filter (fixes bugs on AMD)
  2607. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  2608. glActiveTexture(GL_TEXTURE1);
  2609. glBindTexture(GL_TEXTURE_2D, storage->frame.current_rt->effects.ssao.blur_red[0]);
  2610. glActiveTexture(GL_TEXTURE2);
  2611. glBindTexture(GL_TEXTURE_2D, storage->frame.current_rt->depth);
  2612. //glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE, GL_NONE);
  2613. glBindFramebuffer(GL_FRAMEBUFFER, storage->frame.current_rt->fbo); //copy to front first
  2614. _copy_screen(true);
  2615. glActiveTexture(GL_TEXTURE0);
  2616. glBindTexture(GL_TEXTURE_2D, storage->frame.current_rt->color);
  2617. state.sss_shader.set_uniform(SubsurfScatteringShaderGLES3::DIR, Vector2(0, 1));
  2618. glBindFramebuffer(GL_FRAMEBUFFER, storage->frame.current_rt->effects.mip_maps[0].sizes[0].fbo); // copy to base level
  2619. _copy_screen(true);
  2620. glBindTexture(GL_TEXTURE_2D, storage->frame.current_rt->effects.mip_maps[0].color); //restore filter
  2621. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  2622. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
  2623. }
  2624. if (env->ssr_enabled) {
  2625. //blur diffuse into effect mipmaps using separatable convolution
  2626. //storage->shaders.copy.set_conditional(CopyShaderGLES3::GAUSSIAN_HORIZONTAL,true);
  2627. _blur_effect_buffer();
  2628. //perform SSR
  2629. state.ssr_shader.set_conditional(ScreenSpaceReflectionShaderGLES3::REFLECT_ROUGHNESS, env->ssr_roughness);
  2630. state.ssr_shader.set_conditional(ScreenSpaceReflectionShaderGLES3::USE_ORTHOGONAL_PROJECTION, p_cam_projection.is_orthogonal());
  2631. state.ssr_shader.bind();
  2632. int ssr_w = storage->frame.current_rt->effects.mip_maps[1].sizes[0].width;
  2633. int ssr_h = storage->frame.current_rt->effects.mip_maps[1].sizes[0].height;
  2634. state.ssr_shader.set_uniform(ScreenSpaceReflectionShaderGLES3::PIXEL_SIZE, Vector2(1.0 / (ssr_w * 0.5), 1.0 / (ssr_h * 0.5)));
  2635. state.ssr_shader.set_uniform(ScreenSpaceReflectionShaderGLES3::CAMERA_Z_NEAR, p_cam_projection.get_z_near());
  2636. state.ssr_shader.set_uniform(ScreenSpaceReflectionShaderGLES3::CAMERA_Z_FAR, p_cam_projection.get_z_far());
  2637. state.ssr_shader.set_uniform(ScreenSpaceReflectionShaderGLES3::PROJECTION, p_cam_projection);
  2638. state.ssr_shader.set_uniform(ScreenSpaceReflectionShaderGLES3::INVERSE_PROJECTION, p_cam_projection.inverse());
  2639. state.ssr_shader.set_uniform(ScreenSpaceReflectionShaderGLES3::VIEWPORT_SIZE, Size2(ssr_w, ssr_h));
  2640. //state.ssr_shader.set_uniform(ScreenSpaceReflectionShaderGLES3::FRAME_INDEX,int(render_pass));
  2641. state.ssr_shader.set_uniform(ScreenSpaceReflectionShaderGLES3::FILTER_MIPMAP_LEVELS, float(storage->frame.current_rt->effects.mip_maps[0].sizes.size()));
  2642. state.ssr_shader.set_uniform(ScreenSpaceReflectionShaderGLES3::NUM_STEPS, env->ssr_max_steps);
  2643. state.ssr_shader.set_uniform(ScreenSpaceReflectionShaderGLES3::DEPTH_TOLERANCE, env->ssr_depth_tolerance);
  2644. state.ssr_shader.set_uniform(ScreenSpaceReflectionShaderGLES3::DISTANCE_FADE, env->ssr_fade_out);
  2645. state.ssr_shader.set_uniform(ScreenSpaceReflectionShaderGLES3::CURVE_FADE_IN, env->ssr_fade_in);
  2646. glActiveTexture(GL_TEXTURE0);
  2647. glBindTexture(GL_TEXTURE_2D, storage->frame.current_rt->effects.mip_maps[0].color);
  2648. glActiveTexture(GL_TEXTURE1);
  2649. glBindTexture(GL_TEXTURE_2D, storage->frame.current_rt->buffers.effect);
  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->effects.mip_maps[1].sizes[0].fbo);
  2654. glViewport(0, 0, ssr_w, ssr_h);
  2655. _copy_screen(true);
  2656. glViewport(0, 0, storage->frame.current_rt->width, storage->frame.current_rt->height);
  2657. }
  2658. glBindFramebuffer(GL_READ_FRAMEBUFFER, storage->frame.current_rt->buffers.fbo);
  2659. glReadBuffer(GL_COLOR_ATTACHMENT1);
  2660. glBindFramebuffer(GL_DRAW_FRAMEBUFFER, storage->frame.current_rt->fbo);
  2661. //glDrawBuffer(GL_COLOR_ATTACHMENT0);
  2662. 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);
  2663. glReadBuffer(GL_COLOR_ATTACHMENT0);
  2664. glBindFramebuffer(GL_READ_FRAMEBUFFER, 0);
  2665. glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0);
  2666. //copy reflection over diffuse, resolving SSR if needed
  2667. state.resolve_shader.set_conditional(ResolveShaderGLES3::USE_SSR, env->ssr_enabled);
  2668. state.resolve_shader.bind();
  2669. state.resolve_shader.set_uniform(ResolveShaderGLES3::PIXEL_SIZE, Vector2(1.0 / storage->frame.current_rt->width, 1.0 / storage->frame.current_rt->height));
  2670. glActiveTexture(GL_TEXTURE0);
  2671. glBindTexture(GL_TEXTURE_2D, storage->frame.current_rt->color);
  2672. if (env->ssr_enabled) {
  2673. glActiveTexture(GL_TEXTURE1);
  2674. glBindTexture(GL_TEXTURE_2D, storage->frame.current_rt->effects.mip_maps[1].color);
  2675. }
  2676. glBindFramebuffer(GL_FRAMEBUFFER, storage->frame.current_rt->effects.mip_maps[0].sizes[0].fbo);
  2677. glEnable(GL_BLEND);
  2678. glBlendEquation(GL_FUNC_ADD);
  2679. glBlendFunc(GL_ONE, GL_ONE); //use additive to accumulate one over the other
  2680. _copy_screen(true);
  2681. glDisable(GL_BLEND); //end additive
  2682. if (state.used_screen_texture) {
  2683. _blur_effect_buffer();
  2684. //restored framebuffer
  2685. glBindFramebuffer(GL_FRAMEBUFFER, storage->frame.current_rt->effects.mip_maps[0].sizes[0].fbo);
  2686. glViewport(0, 0, storage->frame.current_rt->width, storage->frame.current_rt->height);
  2687. }
  2688. state.effect_blur_shader.set_conditional(EffectBlurShaderGLES3::SIMPLE_COPY, true);
  2689. state.effect_blur_shader.bind();
  2690. state.effect_blur_shader.set_uniform(EffectBlurShaderGLES3::LOD, float(0));
  2691. {
  2692. GLuint db = GL_COLOR_ATTACHMENT0;
  2693. glDrawBuffers(1, &db);
  2694. }
  2695. glBindFramebuffer(GL_FRAMEBUFFER, storage->frame.current_rt->buffers.fbo);
  2696. glActiveTexture(GL_TEXTURE0);
  2697. glBindTexture(GL_TEXTURE_2D, storage->frame.current_rt->effects.mip_maps[0].color);
  2698. _copy_screen(true);
  2699. state.effect_blur_shader.set_conditional(EffectBlurShaderGLES3::SIMPLE_COPY, false);
  2700. }
  2701. void RasterizerSceneGLES3::_post_process(Environment *env, const CameraMatrix &p_cam_projection) {
  2702. //copy to front buffer
  2703. glDepthMask(GL_FALSE);
  2704. glDisable(GL_DEPTH_TEST);
  2705. glDisable(GL_CULL_FACE);
  2706. glDisable(GL_BLEND);
  2707. glDepthFunc(GL_LEQUAL);
  2708. glColorMask(1, 1, 1, 1);
  2709. //turn off everything used
  2710. //copy specular to front buffer
  2711. //copy diffuse to effect buffer
  2712. if (storage->frame.current_rt->buffers.active) {
  2713. //transfer to effect buffer if using buffers, also resolve MSAA
  2714. glReadBuffer(GL_COLOR_ATTACHMENT0);
  2715. glBindFramebuffer(GL_READ_FRAMEBUFFER, storage->frame.current_rt->buffers.fbo);
  2716. glBindFramebuffer(GL_DRAW_FRAMEBUFFER, storage->frame.current_rt->effects.mip_maps[0].sizes[0].fbo);
  2717. 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);
  2718. glBindFramebuffer(GL_READ_FRAMEBUFFER, 0);
  2719. glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0);
  2720. }
  2721. 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
  2722. //no environment or transparent render, simply return and convert to SRGB
  2723. glBindFramebuffer(GL_FRAMEBUFFER, storage->frame.current_rt->fbo);
  2724. glActiveTexture(GL_TEXTURE0);
  2725. glBindTexture(GL_TEXTURE_2D, storage->frame.current_rt->effects.mip_maps[0].color);
  2726. storage->shaders.copy.set_conditional(CopyShaderGLES3::LINEAR_TO_SRGB, true);
  2727. storage->shaders.copy.set_conditional(CopyShaderGLES3::V_FLIP, storage->frame.current_rt->flags[RasterizerStorage::RENDER_TARGET_VFLIP]);
  2728. storage->shaders.copy.set_conditional(CopyShaderGLES3::DISABLE_ALPHA, !storage->frame.current_rt->flags[RasterizerStorage::RENDER_TARGET_TRANSPARENT]);
  2729. storage->shaders.copy.bind();
  2730. _copy_screen(true);
  2731. storage->shaders.copy.set_conditional(CopyShaderGLES3::LINEAR_TO_SRGB, false);
  2732. storage->shaders.copy.set_conditional(CopyShaderGLES3::DISABLE_ALPHA, false); //compute luminance
  2733. storage->shaders.copy.set_conditional(CopyShaderGLES3::V_FLIP, false);
  2734. return;
  2735. }
  2736. //order of operation
  2737. //1) DOF Blur (first blur, then copy to buffer applying the blur)
  2738. //2) Motion Blur
  2739. //3) Bloom
  2740. //4) Tonemap
  2741. //5) Adjustments
  2742. GLuint composite_from = storage->frame.current_rt->effects.mip_maps[0].color;
  2743. if (env->dof_blur_far_enabled) {
  2744. //blur diffuse into effect mipmaps using separatable convolution
  2745. //storage->shaders.copy.set_conditional(CopyShaderGLES3::GAUSSIAN_HORIZONTAL,true);
  2746. int vp_h = storage->frame.current_rt->height;
  2747. int vp_w = storage->frame.current_rt->width;
  2748. state.effect_blur_shader.set_conditional(EffectBlurShaderGLES3::USE_ORTHOGONAL_PROJECTION, p_cam_projection.is_orthogonal());
  2749. state.effect_blur_shader.set_conditional(EffectBlurShaderGLES3::DOF_FAR_BLUR, true);
  2750. state.effect_blur_shader.set_conditional(EffectBlurShaderGLES3::DOF_QUALITY_LOW, env->dof_blur_far_quality == VS::ENV_DOF_BLUR_QUALITY_LOW);
  2751. state.effect_blur_shader.set_conditional(EffectBlurShaderGLES3::DOF_QUALITY_MEDIUM, env->dof_blur_far_quality == VS::ENV_DOF_BLUR_QUALITY_MEDIUM);
  2752. state.effect_blur_shader.set_conditional(EffectBlurShaderGLES3::DOF_QUALITY_HIGH, env->dof_blur_far_quality == VS::ENV_DOF_BLUR_QUALITY_HIGH);
  2753. state.effect_blur_shader.bind();
  2754. int qsteps[3] = { 4, 10, 20 };
  2755. float radius = (env->dof_blur_far_amount * env->dof_blur_far_amount) / qsteps[env->dof_blur_far_quality];
  2756. state.effect_blur_shader.set_uniform(EffectBlurShaderGLES3::DOF_BEGIN, env->dof_blur_far_distance);
  2757. state.effect_blur_shader.set_uniform(EffectBlurShaderGLES3::DOF_END, env->dof_blur_far_distance + env->dof_blur_far_transition);
  2758. state.effect_blur_shader.set_uniform(EffectBlurShaderGLES3::DOF_DIR, Vector2(1, 0));
  2759. state.effect_blur_shader.set_uniform(EffectBlurShaderGLES3::DOF_RADIUS, radius);
  2760. state.effect_blur_shader.set_uniform(EffectBlurShaderGLES3::PIXEL_SIZE, Vector2(1.0 / vp_w, 1.0 / vp_h));
  2761. state.effect_blur_shader.set_uniform(EffectBlurShaderGLES3::CAMERA_Z_NEAR, p_cam_projection.get_z_near());
  2762. state.effect_blur_shader.set_uniform(EffectBlurShaderGLES3::CAMERA_Z_FAR, p_cam_projection.get_z_far());
  2763. glActiveTexture(GL_TEXTURE1);
  2764. glBindTexture(GL_TEXTURE_2D, storage->frame.current_rt->depth);
  2765. glActiveTexture(GL_TEXTURE0);
  2766. glBindTexture(GL_TEXTURE_2D, composite_from);
  2767. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  2768. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  2769. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  2770. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  2771. glBindFramebuffer(GL_FRAMEBUFFER, storage->frame.current_rt->fbo); //copy to front first
  2772. _copy_screen(true);
  2773. glActiveTexture(GL_TEXTURE0);
  2774. glBindTexture(GL_TEXTURE_2D, storage->frame.current_rt->color);
  2775. state.effect_blur_shader.set_uniform(EffectBlurShaderGLES3::DOF_DIR, Vector2(0, 1));
  2776. glBindFramebuffer(GL_FRAMEBUFFER, storage->frame.current_rt->effects.mip_maps[0].sizes[0].fbo); // copy to base level
  2777. _copy_screen();
  2778. state.effect_blur_shader.set_conditional(EffectBlurShaderGLES3::DOF_FAR_BLUR, false);
  2779. state.effect_blur_shader.set_conditional(EffectBlurShaderGLES3::DOF_QUALITY_LOW, false);
  2780. state.effect_blur_shader.set_conditional(EffectBlurShaderGLES3::DOF_QUALITY_MEDIUM, false);
  2781. state.effect_blur_shader.set_conditional(EffectBlurShaderGLES3::DOF_QUALITY_HIGH, false);
  2782. state.effect_blur_shader.set_conditional(EffectBlurShaderGLES3::USE_ORTHOGONAL_PROJECTION, false);
  2783. composite_from = storage->frame.current_rt->effects.mip_maps[0].color;
  2784. }
  2785. if (env->dof_blur_near_enabled) {
  2786. //blur diffuse into effect mipmaps using separatable convolution
  2787. //storage->shaders.copy.set_conditional(CopyShaderGLES3::GAUSSIAN_HORIZONTAL,true);
  2788. int vp_h = storage->frame.current_rt->height;
  2789. int vp_w = storage->frame.current_rt->width;
  2790. state.effect_blur_shader.set_conditional(EffectBlurShaderGLES3::USE_ORTHOGONAL_PROJECTION, p_cam_projection.is_orthogonal());
  2791. state.effect_blur_shader.set_conditional(EffectBlurShaderGLES3::DOF_NEAR_BLUR, true);
  2792. state.effect_blur_shader.set_conditional(EffectBlurShaderGLES3::DOF_NEAR_FIRST_TAP, true);
  2793. state.effect_blur_shader.set_conditional(EffectBlurShaderGLES3::DOF_QUALITY_LOW, env->dof_blur_near_quality == VS::ENV_DOF_BLUR_QUALITY_LOW);
  2794. state.effect_blur_shader.set_conditional(EffectBlurShaderGLES3::DOF_QUALITY_MEDIUM, env->dof_blur_near_quality == VS::ENV_DOF_BLUR_QUALITY_MEDIUM);
  2795. state.effect_blur_shader.set_conditional(EffectBlurShaderGLES3::DOF_QUALITY_HIGH, env->dof_blur_near_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_near_amount * env->dof_blur_near_amount) / qsteps[env->dof_blur_near_quality];
  2799. state.effect_blur_shader.set_uniform(EffectBlurShaderGLES3::DOF_BEGIN, env->dof_blur_near_distance);
  2800. state.effect_blur_shader.set_uniform(EffectBlurShaderGLES3::DOF_END, env->dof_blur_near_distance - env->dof_blur_near_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();
  2816. //manually do the blend if this is the first operation resolving from the diffuse buffer
  2817. state.effect_blur_shader.set_conditional(EffectBlurShaderGLES3::DOF_NEAR_BLUR_MERGE, composite_from == storage->frame.current_rt->buffers.diffuse);
  2818. state.effect_blur_shader.set_conditional(EffectBlurShaderGLES3::DOF_NEAR_FIRST_TAP, false);
  2819. state.effect_blur_shader.bind();
  2820. state.effect_blur_shader.set_uniform(EffectBlurShaderGLES3::DOF_BEGIN, env->dof_blur_near_distance);
  2821. state.effect_blur_shader.set_uniform(EffectBlurShaderGLES3::DOF_END, env->dof_blur_near_distance - env->dof_blur_near_transition);
  2822. state.effect_blur_shader.set_uniform(EffectBlurShaderGLES3::DOF_DIR, Vector2(0, 1));
  2823. state.effect_blur_shader.set_uniform(EffectBlurShaderGLES3::DOF_RADIUS, radius);
  2824. state.effect_blur_shader.set_uniform(EffectBlurShaderGLES3::PIXEL_SIZE, Vector2(1.0 / vp_w, 1.0 / vp_h));
  2825. state.effect_blur_shader.set_uniform(EffectBlurShaderGLES3::CAMERA_Z_NEAR, p_cam_projection.get_z_near());
  2826. state.effect_blur_shader.set_uniform(EffectBlurShaderGLES3::CAMERA_Z_FAR, p_cam_projection.get_z_far());
  2827. glActiveTexture(GL_TEXTURE0);
  2828. glBindTexture(GL_TEXTURE_2D, storage->frame.current_rt->color);
  2829. glBindFramebuffer(GL_FRAMEBUFFER, storage->frame.current_rt->effects.mip_maps[0].sizes[0].fbo); // copy to base level
  2830. if (composite_from != storage->frame.current_rt->buffers.diffuse) {
  2831. glEnable(GL_BLEND);
  2832. glBlendEquation(GL_FUNC_ADD);
  2833. glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  2834. } else {
  2835. glActiveTexture(GL_TEXTURE2);
  2836. glBindTexture(GL_TEXTURE_2D, storage->frame.current_rt->buffers.diffuse);
  2837. }
  2838. _copy_screen(true);
  2839. if (composite_from != storage->frame.current_rt->buffers.diffuse) {
  2840. glDisable(GL_BLEND);
  2841. }
  2842. state.effect_blur_shader.set_conditional(EffectBlurShaderGLES3::DOF_NEAR_BLUR, false);
  2843. state.effect_blur_shader.set_conditional(EffectBlurShaderGLES3::DOF_NEAR_FIRST_TAP, false);
  2844. state.effect_blur_shader.set_conditional(EffectBlurShaderGLES3::DOF_NEAR_BLUR_MERGE, false);
  2845. state.effect_blur_shader.set_conditional(EffectBlurShaderGLES3::DOF_QUALITY_LOW, false);
  2846. state.effect_blur_shader.set_conditional(EffectBlurShaderGLES3::DOF_QUALITY_MEDIUM, false);
  2847. state.effect_blur_shader.set_conditional(EffectBlurShaderGLES3::DOF_QUALITY_HIGH, false);
  2848. state.effect_blur_shader.set_conditional(EffectBlurShaderGLES3::USE_ORTHOGONAL_PROJECTION, false);
  2849. composite_from = storage->frame.current_rt->effects.mip_maps[0].color;
  2850. }
  2851. if (env->dof_blur_near_enabled || env->dof_blur_far_enabled) {
  2852. //these needed to disable filtering, reenamble
  2853. glActiveTexture(GL_TEXTURE0);
  2854. glBindTexture(GL_TEXTURE_2D, storage->frame.current_rt->effects.mip_maps[0].color);
  2855. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  2856. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
  2857. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  2858. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  2859. }
  2860. if (env->auto_exposure) {
  2861. //compute auto exposure
  2862. //first step, copy from image to luminance buffer
  2863. state.exposure_shader.set_conditional(ExposureShaderGLES3::EXPOSURE_BEGIN, true);
  2864. state.exposure_shader.bind();
  2865. int ss[2] = {
  2866. storage->frame.current_rt->width,
  2867. storage->frame.current_rt->height,
  2868. };
  2869. int ds[2] = {
  2870. exposure_shrink_size,
  2871. exposure_shrink_size,
  2872. };
  2873. glUniform2iv(state.exposure_shader.get_uniform(ExposureShaderGLES3::SOURCE_RENDER_SIZE), 1, ss);
  2874. glUniform2iv(state.exposure_shader.get_uniform(ExposureShaderGLES3::TARGET_SIZE), 1, ds);
  2875. glActiveTexture(GL_TEXTURE0);
  2876. glBindTexture(GL_TEXTURE_2D, composite_from);
  2877. glBindFramebuffer(GL_FRAMEBUFFER, exposure_shrink[0].fbo);
  2878. glViewport(0, 0, exposure_shrink_size, exposure_shrink_size);
  2879. _copy_screen(true);
  2880. //second step, shrink to 2x2 pixels
  2881. state.exposure_shader.set_conditional(ExposureShaderGLES3::EXPOSURE_BEGIN, false);
  2882. state.exposure_shader.bind();
  2883. //shrink from second to previous to last level
  2884. int s_size = exposure_shrink_size / 3;
  2885. for (int i = 1; i < exposure_shrink.size() - 1; i++) {
  2886. glBindFramebuffer(GL_FRAMEBUFFER, exposure_shrink[i].fbo);
  2887. glActiveTexture(GL_TEXTURE0);
  2888. glBindTexture(GL_TEXTURE_2D, exposure_shrink[i - 1].color);
  2889. _copy_screen();
  2890. glViewport(0, 0, s_size, s_size);
  2891. s_size /= 3;
  2892. }
  2893. //third step, shrink to 1x1 pixel taking in consideration the previous exposure
  2894. state.exposure_shader.set_conditional(ExposureShaderGLES3::EXPOSURE_END, true);
  2895. uint64_t tick = OS::get_singleton()->get_ticks_usec();
  2896. uint64_t tick_diff = storage->frame.current_rt->last_exposure_tick == 0 ? 0 : tick - storage->frame.current_rt->last_exposure_tick;
  2897. storage->frame.current_rt->last_exposure_tick = tick;
  2898. if (tick_diff == 0 || tick_diff > 1000000) {
  2899. state.exposure_shader.set_conditional(ExposureShaderGLES3::EXPOSURE_FORCE_SET, true);
  2900. }
  2901. state.exposure_shader.bind();
  2902. glBindFramebuffer(GL_FRAMEBUFFER, exposure_shrink[exposure_shrink.size() - 1].fbo);
  2903. glViewport(0, 0, 1, 1);
  2904. glActiveTexture(GL_TEXTURE0);
  2905. glBindTexture(GL_TEXTURE_2D, exposure_shrink[exposure_shrink.size() - 2].color);
  2906. glActiveTexture(GL_TEXTURE1);
  2907. glBindTexture(GL_TEXTURE_2D, storage->frame.current_rt->exposure.color); //read from previous
  2908. state.exposure_shader.set_uniform(ExposureShaderGLES3::EXPOSURE_ADJUST, env->auto_exposure_speed * (tick_diff / 1000000.0));
  2909. state.exposure_shader.set_uniform(ExposureShaderGLES3::MAX_LUMINANCE, env->auto_exposure_max);
  2910. state.exposure_shader.set_uniform(ExposureShaderGLES3::MIN_LUMINANCE, env->auto_exposure_min);
  2911. _copy_screen(true);
  2912. state.exposure_shader.set_conditional(ExposureShaderGLES3::EXPOSURE_FORCE_SET, false);
  2913. state.exposure_shader.set_conditional(ExposureShaderGLES3::EXPOSURE_END, false);
  2914. //last step, swap with the framebuffer exposure, so the right exposure is kept int he framebuffer
  2915. SWAP(exposure_shrink.write[exposure_shrink.size() - 1].fbo, storage->frame.current_rt->exposure.fbo);
  2916. SWAP(exposure_shrink.write[exposure_shrink.size() - 1].color, storage->frame.current_rt->exposure.color);
  2917. glViewport(0, 0, storage->frame.current_rt->width, storage->frame.current_rt->height);
  2918. VisualServerRaster::redraw_request(); //if using auto exposure, redraw must happen
  2919. }
  2920. int max_glow_level = -1;
  2921. int glow_mask = 0;
  2922. if (env->glow_enabled) {
  2923. for (int i = 0; i < VS::MAX_GLOW_LEVELS; i++) {
  2924. if (env->glow_levels & (1 << i)) {
  2925. if (i >= storage->frame.current_rt->effects.mip_maps[1].sizes.size()) {
  2926. max_glow_level = storage->frame.current_rt->effects.mip_maps[1].sizes.size() - 1;
  2927. glow_mask |= 1 << max_glow_level;
  2928. } else {
  2929. max_glow_level = i;
  2930. glow_mask |= (1 << i);
  2931. }
  2932. }
  2933. }
  2934. //blur diffuse into effect mipmaps using separatable convolution
  2935. //storage->shaders.copy.set_conditional(CopyShaderGLES3::GAUSSIAN_HORIZONTAL,true);
  2936. for (int i = 0; i < (max_glow_level + 1); i++) {
  2937. int vp_w = storage->frame.current_rt->effects.mip_maps[1].sizes[i].width;
  2938. int vp_h = storage->frame.current_rt->effects.mip_maps[1].sizes[i].height;
  2939. glViewport(0, 0, vp_w, vp_h);
  2940. //horizontal pass
  2941. if (i == 0) {
  2942. state.effect_blur_shader.set_conditional(EffectBlurShaderGLES3::GLOW_FIRST_PASS, true);
  2943. state.effect_blur_shader.set_conditional(EffectBlurShaderGLES3::GLOW_USE_AUTO_EXPOSURE, env->auto_exposure);
  2944. }
  2945. state.effect_blur_shader.set_conditional(EffectBlurShaderGLES3::GLOW_GAUSSIAN_HORIZONTAL, true);
  2946. state.effect_blur_shader.bind();
  2947. state.effect_blur_shader.set_uniform(EffectBlurShaderGLES3::PIXEL_SIZE, Vector2(1.0 / vp_w, 1.0 / vp_h));
  2948. state.effect_blur_shader.set_uniform(EffectBlurShaderGLES3::LOD, float(i));
  2949. state.effect_blur_shader.set_uniform(EffectBlurShaderGLES3::GLOW_STRENGTH, env->glow_strength);
  2950. state.effect_blur_shader.set_uniform(EffectBlurShaderGLES3::LUMINANCE_CAP, env->glow_hdr_luminance_cap);
  2951. glActiveTexture(GL_TEXTURE0);
  2952. if (i == 0) {
  2953. glBindTexture(GL_TEXTURE_2D, composite_from);
  2954. state.effect_blur_shader.set_uniform(EffectBlurShaderGLES3::EXPOSURE, env->tone_mapper_exposure);
  2955. if (env->auto_exposure) {
  2956. state.effect_blur_shader.set_uniform(EffectBlurShaderGLES3::AUTO_EXPOSURE_GREY, env->auto_exposure_grey);
  2957. }
  2958. glActiveTexture(GL_TEXTURE1);
  2959. glBindTexture(GL_TEXTURE_2D, storage->frame.current_rt->exposure.color);
  2960. state.effect_blur_shader.set_uniform(EffectBlurShaderGLES3::GLOW_BLOOM, env->glow_bloom);
  2961. state.effect_blur_shader.set_uniform(EffectBlurShaderGLES3::GLOW_HDR_THRESHOLD, env->glow_hdr_bleed_threshold);
  2962. state.effect_blur_shader.set_uniform(EffectBlurShaderGLES3::GLOW_HDR_SCALE, env->glow_hdr_bleed_scale);
  2963. } else {
  2964. glBindTexture(GL_TEXTURE_2D, storage->frame.current_rt->effects.mip_maps[0].color); //previous level, since mipmaps[0] starts one level bigger
  2965. }
  2966. glBindFramebuffer(GL_FRAMEBUFFER, storage->frame.current_rt->effects.mip_maps[1].sizes[i].fbo);
  2967. _copy_screen(true);
  2968. state.effect_blur_shader.set_conditional(EffectBlurShaderGLES3::GLOW_GAUSSIAN_HORIZONTAL, false);
  2969. state.effect_blur_shader.set_conditional(EffectBlurShaderGLES3::GLOW_FIRST_PASS, false);
  2970. state.effect_blur_shader.set_conditional(EffectBlurShaderGLES3::GLOW_USE_AUTO_EXPOSURE, false);
  2971. //vertical pass
  2972. state.effect_blur_shader.set_conditional(EffectBlurShaderGLES3::GLOW_GAUSSIAN_VERTICAL, true);
  2973. state.effect_blur_shader.bind();
  2974. state.effect_blur_shader.set_uniform(EffectBlurShaderGLES3::PIXEL_SIZE, Vector2(1.0 / vp_w, 1.0 / vp_h));
  2975. state.effect_blur_shader.set_uniform(EffectBlurShaderGLES3::LOD, float(i));
  2976. state.effect_blur_shader.set_uniform(EffectBlurShaderGLES3::GLOW_STRENGTH, env->glow_strength);
  2977. glActiveTexture(GL_TEXTURE0);
  2978. glBindTexture(GL_TEXTURE_2D, storage->frame.current_rt->effects.mip_maps[1].color);
  2979. glBindFramebuffer(GL_FRAMEBUFFER, storage->frame.current_rt->effects.mip_maps[0].sizes[i + 1].fbo); //next level, since mipmaps[0] starts one level bigger
  2980. _copy_screen();
  2981. state.effect_blur_shader.set_conditional(EffectBlurShaderGLES3::GLOW_GAUSSIAN_VERTICAL, false);
  2982. }
  2983. glViewport(0, 0, storage->frame.current_rt->width, storage->frame.current_rt->height);
  2984. }
  2985. glBindFramebuffer(GL_FRAMEBUFFER, storage->frame.current_rt->fbo);
  2986. glActiveTexture(GL_TEXTURE0);
  2987. glBindTexture(GL_TEXTURE_2D, composite_from);
  2988. state.tonemap_shader.set_conditional(TonemapShaderGLES3::USE_FILMIC_TONEMAPPER, env->tone_mapper == VS::ENV_TONE_MAPPER_FILMIC);
  2989. state.tonemap_shader.set_conditional(TonemapShaderGLES3::USE_ACES_TONEMAPPER, env->tone_mapper == VS::ENV_TONE_MAPPER_ACES);
  2990. state.tonemap_shader.set_conditional(TonemapShaderGLES3::USE_REINHARD_TONEMAPPER, env->tone_mapper == VS::ENV_TONE_MAPPER_REINHARD);
  2991. state.tonemap_shader.set_conditional(TonemapShaderGLES3::KEEP_3D_LINEAR, storage->frame.current_rt->flags[RasterizerStorage::RENDER_TARGET_KEEP_3D_LINEAR]);
  2992. state.tonemap_shader.set_conditional(TonemapShaderGLES3::USE_AUTO_EXPOSURE, env->auto_exposure);
  2993. state.tonemap_shader.set_conditional(TonemapShaderGLES3::USE_GLOW_FILTER_BICUBIC, env->glow_bicubic_upscale);
  2994. if (max_glow_level >= 0) {
  2995. for (int i = 0; i < (max_glow_level + 1); i++) {
  2996. if (glow_mask & (1 << i)) {
  2997. if (i == 0) {
  2998. state.tonemap_shader.set_conditional(TonemapShaderGLES3::USE_GLOW_LEVEL1, true);
  2999. }
  3000. if (i == 1) {
  3001. state.tonemap_shader.set_conditional(TonemapShaderGLES3::USE_GLOW_LEVEL2, true);
  3002. }
  3003. if (i == 2) {
  3004. state.tonemap_shader.set_conditional(TonemapShaderGLES3::USE_GLOW_LEVEL3, true);
  3005. }
  3006. if (i == 3) {
  3007. state.tonemap_shader.set_conditional(TonemapShaderGLES3::USE_GLOW_LEVEL4, true);
  3008. }
  3009. if (i == 4) {
  3010. state.tonemap_shader.set_conditional(TonemapShaderGLES3::USE_GLOW_LEVEL5, true);
  3011. }
  3012. if (i == 5) {
  3013. state.tonemap_shader.set_conditional(TonemapShaderGLES3::USE_GLOW_LEVEL6, true);
  3014. }
  3015. if (i == 6) {
  3016. state.tonemap_shader.set_conditional(TonemapShaderGLES3::USE_GLOW_LEVEL7, true);
  3017. }
  3018. }
  3019. }
  3020. state.tonemap_shader.set_conditional(TonemapShaderGLES3::USE_GLOW_SCREEN, env->glow_blend_mode == VS::GLOW_BLEND_MODE_SCREEN);
  3021. state.tonemap_shader.set_conditional(TonemapShaderGLES3::USE_GLOW_SOFTLIGHT, env->glow_blend_mode == VS::GLOW_BLEND_MODE_SOFTLIGHT);
  3022. state.tonemap_shader.set_conditional(TonemapShaderGLES3::USE_GLOW_REPLACE, env->glow_blend_mode == VS::GLOW_BLEND_MODE_REPLACE);
  3023. glActiveTexture(GL_TEXTURE2);
  3024. glBindTexture(GL_TEXTURE_2D, storage->frame.current_rt->effects.mip_maps[0].color);
  3025. }
  3026. if (env->adjustments_enabled) {
  3027. state.tonemap_shader.set_conditional(TonemapShaderGLES3::USE_BCS, true);
  3028. RasterizerStorageGLES3::Texture *tex = storage->texture_owner.getornull(env->color_correction);
  3029. if (tex) {
  3030. state.tonemap_shader.set_conditional(TonemapShaderGLES3::USE_COLOR_CORRECTION, true);
  3031. glActiveTexture(GL_TEXTURE3);
  3032. glBindTexture(tex->target, tex->tex_id);
  3033. }
  3034. }
  3035. state.tonemap_shader.set_conditional(TonemapShaderGLES3::V_FLIP, storage->frame.current_rt->flags[RasterizerStorage::RENDER_TARGET_VFLIP]);
  3036. state.tonemap_shader.bind();
  3037. state.tonemap_shader.set_uniform(TonemapShaderGLES3::EXPOSURE, env->tone_mapper_exposure);
  3038. state.tonemap_shader.set_uniform(TonemapShaderGLES3::WHITE, env->tone_mapper_exposure_white);
  3039. if (max_glow_level >= 0) {
  3040. state.tonemap_shader.set_uniform(TonemapShaderGLES3::GLOW_INTENSITY, env->glow_intensity);
  3041. int ss[2] = {
  3042. storage->frame.current_rt->width,
  3043. storage->frame.current_rt->height,
  3044. };
  3045. glUniform2iv(state.tonemap_shader.get_uniform(TonemapShaderGLES3::GLOW_TEXTURE_SIZE), 1, ss);
  3046. }
  3047. if (env->auto_exposure) {
  3048. glActiveTexture(GL_TEXTURE1);
  3049. glBindTexture(GL_TEXTURE_2D, storage->frame.current_rt->exposure.color);
  3050. state.tonemap_shader.set_uniform(TonemapShaderGLES3::AUTO_EXPOSURE_GREY, env->auto_exposure_grey);
  3051. }
  3052. if (env->adjustments_enabled) {
  3053. state.tonemap_shader.set_uniform(TonemapShaderGLES3::BCS, Vector3(env->adjustments_brightness, env->adjustments_contrast, env->adjustments_saturation));
  3054. }
  3055. _copy_screen(true, true);
  3056. //turn off everything used
  3057. state.tonemap_shader.set_conditional(TonemapShaderGLES3::USE_AUTO_EXPOSURE, false);
  3058. state.tonemap_shader.set_conditional(TonemapShaderGLES3::USE_FILMIC_TONEMAPPER, false);
  3059. state.tonemap_shader.set_conditional(TonemapShaderGLES3::USE_ACES_TONEMAPPER, false);
  3060. state.tonemap_shader.set_conditional(TonemapShaderGLES3::USE_REINHARD_TONEMAPPER, false);
  3061. state.tonemap_shader.set_conditional(TonemapShaderGLES3::USE_GLOW_LEVEL1, false);
  3062. state.tonemap_shader.set_conditional(TonemapShaderGLES3::USE_GLOW_LEVEL2, false);
  3063. state.tonemap_shader.set_conditional(TonemapShaderGLES3::USE_GLOW_LEVEL3, false);
  3064. state.tonemap_shader.set_conditional(TonemapShaderGLES3::USE_GLOW_LEVEL4, false);
  3065. state.tonemap_shader.set_conditional(TonemapShaderGLES3::USE_GLOW_LEVEL5, false);
  3066. state.tonemap_shader.set_conditional(TonemapShaderGLES3::USE_GLOW_LEVEL6, false);
  3067. state.tonemap_shader.set_conditional(TonemapShaderGLES3::USE_GLOW_LEVEL7, false);
  3068. state.tonemap_shader.set_conditional(TonemapShaderGLES3::USE_GLOW_REPLACE, false);
  3069. state.tonemap_shader.set_conditional(TonemapShaderGLES3::USE_GLOW_SCREEN, false);
  3070. state.tonemap_shader.set_conditional(TonemapShaderGLES3::USE_GLOW_SOFTLIGHT, false);
  3071. state.tonemap_shader.set_conditional(TonemapShaderGLES3::USE_GLOW_FILTER_BICUBIC, false);
  3072. state.tonemap_shader.set_conditional(TonemapShaderGLES3::USE_BCS, false);
  3073. state.tonemap_shader.set_conditional(TonemapShaderGLES3::USE_COLOR_CORRECTION, false);
  3074. state.tonemap_shader.set_conditional(TonemapShaderGLES3::V_FLIP, false);
  3075. }
  3076. 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) {
  3077. //first of all, make a new render pass
  3078. render_pass++;
  3079. //fill up ubo
  3080. storage->info.render.object_count += p_cull_count;
  3081. Environment *env = environment_owner.getornull(p_environment);
  3082. ShadowAtlas *shadow_atlas = shadow_atlas_owner.getornull(p_shadow_atlas);
  3083. ReflectionAtlas *reflection_atlas = reflection_atlas_owner.getornull(p_reflection_atlas);
  3084. if (shadow_atlas && shadow_atlas->size) {
  3085. glActiveTexture(GL_TEXTURE0 + storage->config.max_texture_image_units - 5);
  3086. glBindTexture(GL_TEXTURE_2D, shadow_atlas->depth);
  3087. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE, GL_COMPARE_REF_TO_TEXTURE);
  3088. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_FUNC, GL_LESS);
  3089. state.ubo_data.shadow_atlas_pixel_size[0] = 1.0 / shadow_atlas->size;
  3090. state.ubo_data.shadow_atlas_pixel_size[1] = 1.0 / shadow_atlas->size;
  3091. }
  3092. if (reflection_atlas && reflection_atlas->size) {
  3093. glActiveTexture(GL_TEXTURE0 + storage->config.max_texture_image_units - 3);
  3094. glBindTexture(GL_TEXTURE_2D, reflection_atlas->color);
  3095. }
  3096. if (p_reflection_probe.is_valid()) {
  3097. state.ubo_data.reflection_multiplier = 0.0;
  3098. } else {
  3099. state.ubo_data.reflection_multiplier = 1.0;
  3100. }
  3101. state.ubo_data.subsurface_scatter_width = subsurface_scatter_size;
  3102. state.ubo_data.z_offset = 0;
  3103. state.ubo_data.z_slope_scale = 0;
  3104. state.ubo_data.shadow_dual_paraboloid_render_side = 0;
  3105. state.ubo_data.shadow_dual_paraboloid_render_zfar = 0;
  3106. state.ubo_data.opaque_prepass_threshold = 0.99;
  3107. p_cam_projection.get_viewport_size(state.ubo_data.viewport_size[0], state.ubo_data.viewport_size[1]);
  3108. if (storage->frame.current_rt) {
  3109. state.ubo_data.screen_pixel_size[0] = 1.0 / storage->frame.current_rt->width;
  3110. state.ubo_data.screen_pixel_size[1] = 1.0 / storage->frame.current_rt->height;
  3111. }
  3112. _setup_environment(env, p_cam_projection, p_cam_transform, p_reflection_probe.is_valid());
  3113. bool fb_cleared = false;
  3114. glDepthFunc(GL_LEQUAL);
  3115. state.used_contact_shadows = true;
  3116. if (!storage->config.no_depth_prepass && storage->frame.current_rt && state.debug_draw != VS::VIEWPORT_DEBUG_DRAW_OVERDRAW) { //detect with state.used_contact_shadows too
  3117. //pre z pass
  3118. glDisable(GL_BLEND);
  3119. glDepthMask(GL_TRUE);
  3120. glEnable(GL_DEPTH_TEST);
  3121. glDisable(GL_SCISSOR_TEST);
  3122. glBindFramebuffer(GL_FRAMEBUFFER, storage->frame.current_rt->buffers.fbo);
  3123. glDrawBuffers(0, NULL);
  3124. glViewport(0, 0, storage->frame.current_rt->width, storage->frame.current_rt->height);
  3125. glColorMask(0, 0, 0, 0);
  3126. glClearDepth(1.0f);
  3127. glClear(GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
  3128. render_list.clear();
  3129. _fill_render_list(p_cull_result, p_cull_count, true, false);
  3130. render_list.sort_by_key(false);
  3131. state.scene_shader.set_conditional(SceneShaderGLES3::RENDER_DEPTH, true);
  3132. _render_list(render_list.elements, render_list.element_count, p_cam_transform, p_cam_projection, 0, false, false, true, false, false);
  3133. state.scene_shader.set_conditional(SceneShaderGLES3::RENDER_DEPTH, false);
  3134. glColorMask(1, 1, 1, 1);
  3135. if (state.used_contact_shadows) {
  3136. glBindFramebuffer(GL_READ_FRAMEBUFFER, storage->frame.current_rt->buffers.fbo);
  3137. glReadBuffer(GL_COLOR_ATTACHMENT0);
  3138. glBindFramebuffer(GL_DRAW_FRAMEBUFFER, storage->frame.current_rt->fbo);
  3139. 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);
  3140. glBindFramebuffer(GL_READ_FRAMEBUFFER, 0);
  3141. glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0);
  3142. //bind depth for read
  3143. glActiveTexture(GL_TEXTURE0 + storage->config.max_texture_image_units - 8);
  3144. glBindTexture(GL_TEXTURE_2D, storage->frame.current_rt->depth);
  3145. }
  3146. fb_cleared = true;
  3147. render_pass++;
  3148. state.using_contact_shadows = true;
  3149. } else {
  3150. state.using_contact_shadows = false;
  3151. }
  3152. _setup_lights(p_light_cull_result, p_light_cull_count, p_cam_transform.affine_inverse(), p_cam_projection, p_shadow_atlas);
  3153. _setup_reflections(p_reflection_probe_cull_result, p_reflection_probe_cull_count, p_cam_transform.affine_inverse(), p_cam_projection, p_reflection_atlas, env);
  3154. bool use_mrt = false;
  3155. render_list.clear();
  3156. _fill_render_list(p_cull_result, p_cull_count, false, false);
  3157. //
  3158. glEnable(GL_BLEND);
  3159. glDepthMask(GL_TRUE);
  3160. glEnable(GL_DEPTH_TEST);
  3161. glDisable(GL_SCISSOR_TEST);
  3162. //rendering to a probe cubemap side
  3163. ReflectionProbeInstance *probe = reflection_probe_instance_owner.getornull(p_reflection_probe);
  3164. GLuint current_fbo;
  3165. if (probe) {
  3166. ReflectionAtlas *ref_atlas = reflection_atlas_owner.getptr(probe->atlas);
  3167. ERR_FAIL_COND(!ref_atlas);
  3168. int target_size = ref_atlas->size / ref_atlas->subdiv;
  3169. int cubemap_index = reflection_cubemaps.size() - 1;
  3170. for (int i = reflection_cubemaps.size() - 1; i >= 0; i--) {
  3171. //find appropriate cubemap to render to
  3172. if (reflection_cubemaps[i].size > target_size * 2)
  3173. break;
  3174. cubemap_index = i;
  3175. }
  3176. current_fbo = reflection_cubemaps[cubemap_index].fbo_id[p_reflection_probe_pass];
  3177. use_mrt = false;
  3178. state.scene_shader.set_conditional(SceneShaderGLES3::USE_MULTIPLE_RENDER_TARGETS, false);
  3179. glViewport(0, 0, reflection_cubemaps[cubemap_index].size, reflection_cubemaps[cubemap_index].size);
  3180. glBindFramebuffer(GL_FRAMEBUFFER, current_fbo);
  3181. } else {
  3182. use_mrt = env && (state.used_sss || env->ssao_enabled || env->ssr_enabled); //only enable MRT rendering if any of these is enabled
  3183. //effects disabled and transparency also prevent using MRTs
  3184. use_mrt = use_mrt && !storage->frame.current_rt->flags[RasterizerStorage::RENDER_TARGET_TRANSPARENT];
  3185. use_mrt = use_mrt && !storage->frame.current_rt->flags[RasterizerStorage::RENDER_TARGET_NO_3D_EFFECTS];
  3186. use_mrt = use_mrt && state.debug_draw != VS::VIEWPORT_DEBUG_DRAW_OVERDRAW;
  3187. use_mrt = use_mrt && env && (env->bg_mode != VS::ENV_BG_KEEP && env->bg_mode != VS::ENV_BG_CANVAS);
  3188. glViewport(0, 0, storage->frame.current_rt->width, storage->frame.current_rt->height);
  3189. if (use_mrt) {
  3190. current_fbo = storage->frame.current_rt->buffers.fbo;
  3191. glBindFramebuffer(GL_FRAMEBUFFER, storage->frame.current_rt->buffers.fbo);
  3192. state.scene_shader.set_conditional(SceneShaderGLES3::USE_MULTIPLE_RENDER_TARGETS, true);
  3193. Vector<GLenum> draw_buffers;
  3194. draw_buffers.push_back(GL_COLOR_ATTACHMENT0);
  3195. draw_buffers.push_back(GL_COLOR_ATTACHMENT1);
  3196. draw_buffers.push_back(GL_COLOR_ATTACHMENT2);
  3197. if (state.used_sss) {
  3198. draw_buffers.push_back(GL_COLOR_ATTACHMENT3);
  3199. }
  3200. glDrawBuffers(draw_buffers.size(), draw_buffers.ptr());
  3201. Color black(0, 0, 0, 0);
  3202. glClearBufferfv(GL_COLOR, 1, black.components); // specular
  3203. glClearBufferfv(GL_COLOR, 2, black.components); // normal metal rough
  3204. if (state.used_sss) {
  3205. glClearBufferfv(GL_COLOR, 3, black.components); // normal metal rough
  3206. }
  3207. } else {
  3208. if (storage->frame.current_rt->buffers.active) {
  3209. current_fbo = storage->frame.current_rt->buffers.fbo;
  3210. } else {
  3211. current_fbo = storage->frame.current_rt->effects.mip_maps[0].sizes[0].fbo;
  3212. }
  3213. glBindFramebuffer(GL_FRAMEBUFFER, current_fbo);
  3214. state.scene_shader.set_conditional(SceneShaderGLES3::USE_MULTIPLE_RENDER_TARGETS, false);
  3215. Vector<GLenum> draw_buffers;
  3216. draw_buffers.push_back(GL_COLOR_ATTACHMENT0);
  3217. glDrawBuffers(draw_buffers.size(), draw_buffers.ptr());
  3218. }
  3219. }
  3220. if (!fb_cleared) {
  3221. glClearBufferfi(GL_DEPTH_STENCIL, 0, 1.0, 0);
  3222. }
  3223. Color clear_color(0, 0, 0, 0);
  3224. RasterizerStorageGLES3::Sky *sky = NULL;
  3225. GLuint env_radiance_tex = 0;
  3226. if (state.debug_draw == VS::VIEWPORT_DEBUG_DRAW_OVERDRAW) {
  3227. clear_color = Color(0, 0, 0, 0);
  3228. storage->frame.clear_request = false;
  3229. } else if (!probe && storage->frame.current_rt->flags[RasterizerStorage::RENDER_TARGET_TRANSPARENT]) {
  3230. clear_color = Color(0, 0, 0, 0);
  3231. storage->frame.clear_request = false;
  3232. } else if (!env || env->bg_mode == VS::ENV_BG_CLEAR_COLOR) {
  3233. if (storage->frame.clear_request) {
  3234. clear_color = storage->frame.clear_request_color.to_linear();
  3235. storage->frame.clear_request = false;
  3236. }
  3237. } else if (env->bg_mode == VS::ENV_BG_CANVAS) {
  3238. clear_color = env->bg_color.to_linear();
  3239. storage->frame.clear_request = false;
  3240. } else if (env->bg_mode == VS::ENV_BG_COLOR) {
  3241. clear_color = env->bg_color.to_linear();
  3242. storage->frame.clear_request = false;
  3243. } else if (env->bg_mode == VS::ENV_BG_SKY) {
  3244. storage->frame.clear_request = false;
  3245. } else if (env->bg_mode == VS::ENV_BG_COLOR_SKY) {
  3246. clear_color = env->bg_color.to_linear();
  3247. storage->frame.clear_request = false;
  3248. } else {
  3249. storage->frame.clear_request = false;
  3250. }
  3251. if (!env || env->bg_mode != VS::ENV_BG_KEEP) {
  3252. glClearBufferfv(GL_COLOR, 0, clear_color.components); // specular
  3253. }
  3254. 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.
  3255. if (env) {
  3256. switch (bg_mode) {
  3257. case VS::ENV_BG_COLOR_SKY:
  3258. case VS::ENV_BG_SKY:
  3259. sky = storage->sky_owner.getornull(env->sky);
  3260. if (sky) {
  3261. env_radiance_tex = sky->radiance;
  3262. }
  3263. break;
  3264. case VS::ENV_BG_CANVAS:
  3265. //copy canvas to 3d buffer and convert it to linear
  3266. glDisable(GL_BLEND);
  3267. glDepthMask(GL_FALSE);
  3268. glDisable(GL_DEPTH_TEST);
  3269. glDisable(GL_CULL_FACE);
  3270. glActiveTexture(GL_TEXTURE0);
  3271. glBindTexture(GL_TEXTURE_2D, storage->frame.current_rt->color);
  3272. storage->shaders.copy.set_conditional(CopyShaderGLES3::DISABLE_ALPHA, true);
  3273. storage->shaders.copy.set_conditional(CopyShaderGLES3::SRGB_TO_LINEAR, true);
  3274. storage->shaders.copy.bind();
  3275. _copy_screen(true, true);
  3276. //turn off everything used
  3277. storage->shaders.copy.set_conditional(CopyShaderGLES3::SRGB_TO_LINEAR, false);
  3278. storage->shaders.copy.set_conditional(CopyShaderGLES3::DISABLE_ALPHA, false);
  3279. //restore
  3280. glEnable(GL_BLEND);
  3281. glDepthMask(GL_TRUE);
  3282. glEnable(GL_DEPTH_TEST);
  3283. glEnable(GL_CULL_FACE);
  3284. break;
  3285. default: {}
  3286. }
  3287. }
  3288. if (probe && probe->probe_ptr->interior) {
  3289. env_radiance_tex = 0; //for rendering probe interiors, radiance must not be used.
  3290. }
  3291. state.texscreen_copied = false;
  3292. glBlendEquation(GL_FUNC_ADD);
  3293. if (storage->frame.current_rt && storage->frame.current_rt->flags[RasterizerStorage::RENDER_TARGET_TRANSPARENT]) {
  3294. glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
  3295. glEnable(GL_BLEND);
  3296. } else {
  3297. glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  3298. glDisable(GL_BLEND);
  3299. }
  3300. render_list.sort_by_key(false);
  3301. if (state.directional_light_count == 0) {
  3302. directional_light = NULL;
  3303. _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);
  3304. } else {
  3305. for (int i = 0; i < state.directional_light_count; i++) {
  3306. directional_light = directional_lights[i];
  3307. if (i > 0) {
  3308. glEnable(GL_BLEND);
  3309. }
  3310. _setup_directional_light(i, p_cam_transform.affine_inverse(), shadow_atlas != NULL && shadow_atlas->size > 0);
  3311. _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);
  3312. }
  3313. }
  3314. state.scene_shader.set_conditional(SceneShaderGLES3::USE_MULTIPLE_RENDER_TARGETS, false);
  3315. if (use_mrt) {
  3316. GLenum gldb = GL_COLOR_ATTACHMENT0;
  3317. glDrawBuffers(1, &gldb);
  3318. }
  3319. 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))) {
  3320. /*
  3321. if (use_mrt) {
  3322. glBindFramebuffer(GL_FRAMEBUFFER,storage->frame.current_rt->buffers.fbo); //switch to alpha fbo for sky, only diffuse/ambient matters
  3323. */
  3324. if (sky && sky->panorama.is_valid())
  3325. _draw_sky(sky, p_cam_projection, p_cam_transform, false, env->sky_custom_fov, env->bg_energy, env->sky_orientation);
  3326. }
  3327. //_render_list_forward(&alpha_render_list,camera_transform,camera_transform_inverse,camera_projection,false,fragment_lighting,true);
  3328. //glColorMask(1,1,1,1);
  3329. //state.scene_shader.set_conditional( SceneShaderGLES3::USE_FOG,false);
  3330. if (use_mrt) {
  3331. _render_mrts(env, p_cam_projection);
  3332. } else {
  3333. //FIXME: check that this is possible to use
  3334. if (storage->frame.current_rt && storage->frame.current_rt->buffers.active && state.used_screen_texture) {
  3335. glBindFramebuffer(GL_READ_FRAMEBUFFER, storage->frame.current_rt->buffers.fbo);
  3336. glReadBuffer(GL_COLOR_ATTACHMENT0);
  3337. glBindFramebuffer(GL_DRAW_FRAMEBUFFER, storage->frame.current_rt->effects.mip_maps[0].sizes[0].fbo);
  3338. 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);
  3339. glBindFramebuffer(GL_READ_FRAMEBUFFER, 0);
  3340. glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0);
  3341. _blur_effect_buffer();
  3342. //restored framebuffer
  3343. glBindFramebuffer(GL_FRAMEBUFFER, storage->frame.current_rt->buffers.fbo);
  3344. glViewport(0, 0, storage->frame.current_rt->width, storage->frame.current_rt->height);
  3345. }
  3346. }
  3347. if (storage->frame.current_rt && state.used_screen_texture && storage->frame.current_rt->buffers.active) {
  3348. glActiveTexture(GL_TEXTURE0 + storage->config.max_texture_image_units - 7);
  3349. glBindTexture(GL_TEXTURE_2D, storage->frame.current_rt->effects.mip_maps[0].color);
  3350. }
  3351. glEnable(GL_BLEND);
  3352. glDepthMask(GL_TRUE);
  3353. glEnable(GL_DEPTH_TEST);
  3354. glDisable(GL_SCISSOR_TEST);
  3355. render_list.sort_by_reverse_depth_and_priority(true);
  3356. if (state.directional_light_count == 0) {
  3357. directional_light = NULL;
  3358. _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);
  3359. } else {
  3360. for (int i = 0; i < state.directional_light_count; i++) {
  3361. directional_light = directional_lights[i];
  3362. _setup_directional_light(i, p_cam_transform.affine_inverse(), shadow_atlas != NULL && shadow_atlas->size > 0);
  3363. _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);
  3364. }
  3365. }
  3366. if (probe) {
  3367. //rendering a probe, do no more!
  3368. return;
  3369. }
  3370. _post_process(env, p_cam_projection);
  3371. if (false && shadow_atlas) {
  3372. //_copy_texture_to_front_buffer(shadow_atlas->depth);
  3373. storage->canvas->canvas_begin();
  3374. glActiveTexture(GL_TEXTURE0);
  3375. glBindTexture(GL_TEXTURE_2D, shadow_atlas->depth);
  3376. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE, GL_NONE);
  3377. 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));
  3378. }
  3379. if (false && storage->frame.current_rt) {
  3380. //_copy_texture_to_front_buffer(shadow_atlas->depth);
  3381. storage->canvas->canvas_begin();
  3382. glActiveTexture(GL_TEXTURE0);
  3383. glBindTexture(GL_TEXTURE_2D, exposure_shrink[4].color);
  3384. //glBindTexture(GL_TEXTURE_2D,storage->frame.current_rt->exposure.color);
  3385. 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));
  3386. }
  3387. if (false && reflection_atlas && storage->frame.current_rt) {
  3388. //_copy_texture_to_front_buffer(shadow_atlas->depth);
  3389. storage->canvas->canvas_begin();
  3390. glActiveTexture(GL_TEXTURE0);
  3391. glBindTexture(GL_TEXTURE_2D, reflection_atlas->color);
  3392. 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));
  3393. }
  3394. if (false && directional_shadow.fbo) {
  3395. //_copy_texture_to_front_buffer(shadow_atlas->depth);
  3396. storage->canvas->canvas_begin();
  3397. glActiveTexture(GL_TEXTURE0);
  3398. glBindTexture(GL_TEXTURE_2D, directional_shadow.depth);
  3399. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE, GL_NONE);
  3400. 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));
  3401. }
  3402. if (false && env_radiance_tex) {
  3403. //_copy_texture_to_front_buffer(shadow_atlas->depth);
  3404. storage->canvas->canvas_begin();
  3405. glActiveTexture(GL_TEXTURE0);
  3406. glBindTexture(GL_TEXTURE_2D, env_radiance_tex);
  3407. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  3408. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  3409. 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));
  3410. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
  3411. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  3412. }
  3413. //disable all stuff
  3414. }
  3415. void RasterizerSceneGLES3::render_shadow(RID p_light, RID p_shadow_atlas, int p_pass, InstanceBase **p_cull_result, int p_cull_count) {
  3416. render_pass++;
  3417. directional_light = NULL;
  3418. LightInstance *light_instance = light_instance_owner.getornull(p_light);
  3419. ERR_FAIL_COND(!light_instance);
  3420. RasterizerStorageGLES3::Light *light = storage->light_owner.getornull(light_instance->light);
  3421. ERR_FAIL_COND(!light);
  3422. uint32_t x, y, width, height;
  3423. float dp_direction = 0.0;
  3424. float zfar = 0;
  3425. bool flip_facing = false;
  3426. int custom_vp_size = 0;
  3427. GLuint fbo;
  3428. int current_cubemap = -1;
  3429. float bias = 0;
  3430. float normal_bias = 0;
  3431. state.using_contact_shadows = false;
  3432. CameraMatrix light_projection;
  3433. Transform light_transform;
  3434. if (light->type == VS::LIGHT_DIRECTIONAL) {
  3435. //set pssm stuff
  3436. if (light_instance->last_scene_shadow_pass != scene_pass) {
  3437. //assign rect if unassigned
  3438. light_instance->light_directional_index = directional_shadow.current_light;
  3439. light_instance->last_scene_shadow_pass = scene_pass;
  3440. directional_shadow.current_light++;
  3441. if (directional_shadow.light_count == 1) {
  3442. light_instance->directional_rect = Rect2(0, 0, directional_shadow.size, directional_shadow.size);
  3443. } else if (directional_shadow.light_count == 2) {
  3444. light_instance->directional_rect = Rect2(0, 0, directional_shadow.size, directional_shadow.size / 2);
  3445. if (light_instance->light_directional_index == 1) {
  3446. light_instance->directional_rect.position.x += light_instance->directional_rect.size.x;
  3447. }
  3448. } else { //3 and 4
  3449. light_instance->directional_rect = Rect2(0, 0, directional_shadow.size / 2, directional_shadow.size / 2);
  3450. if (light_instance->light_directional_index & 1) {
  3451. light_instance->directional_rect.position.x += light_instance->directional_rect.size.x;
  3452. }
  3453. if (light_instance->light_directional_index / 2) {
  3454. light_instance->directional_rect.position.y += light_instance->directional_rect.size.y;
  3455. }
  3456. }
  3457. }
  3458. light_projection = light_instance->shadow_transform[p_pass].camera;
  3459. light_transform = light_instance->shadow_transform[p_pass].transform;
  3460. x = light_instance->directional_rect.position.x;
  3461. y = light_instance->directional_rect.position.y;
  3462. width = light_instance->directional_rect.size.x;
  3463. height = light_instance->directional_rect.size.y;
  3464. if (light->directional_shadow_mode == VS::LIGHT_DIRECTIONAL_SHADOW_PARALLEL_4_SPLITS) {
  3465. width /= 2;
  3466. height /= 2;
  3467. if (p_pass == 0) {
  3468. } else if (p_pass == 1) {
  3469. x += width;
  3470. } else if (p_pass == 2) {
  3471. y += height;
  3472. } else if (p_pass == 3) {
  3473. x += width;
  3474. y += height;
  3475. }
  3476. } else if (light->directional_shadow_mode == VS::LIGHT_DIRECTIONAL_SHADOW_PARALLEL_2_SPLITS) {
  3477. height /= 2;
  3478. if (p_pass == 0) {
  3479. } else {
  3480. y += height;
  3481. }
  3482. }
  3483. float bias_mult = Math::lerp(1.0f, light_instance->shadow_transform[p_pass].bias_scale, light->param[VS::LIGHT_PARAM_SHADOW_BIAS_SPLIT_SCALE]);
  3484. zfar = light->param[VS::LIGHT_PARAM_RANGE];
  3485. bias = light->param[VS::LIGHT_PARAM_SHADOW_BIAS] * bias_mult;
  3486. normal_bias = light->param[VS::LIGHT_PARAM_SHADOW_NORMAL_BIAS] * bias_mult;
  3487. fbo = directional_shadow.fbo;
  3488. } else {
  3489. //set from shadow atlas
  3490. ShadowAtlas *shadow_atlas = shadow_atlas_owner.getornull(p_shadow_atlas);
  3491. ERR_FAIL_COND(!shadow_atlas);
  3492. ERR_FAIL_COND(!shadow_atlas->shadow_owners.has(p_light));
  3493. fbo = shadow_atlas->fbo;
  3494. uint32_t key = shadow_atlas->shadow_owners[p_light];
  3495. uint32_t quadrant = (key >> ShadowAtlas::QUADRANT_SHIFT) & 0x3;
  3496. uint32_t shadow = key & ShadowAtlas::SHADOW_INDEX_MASK;
  3497. ERR_FAIL_INDEX((int)shadow, shadow_atlas->quadrants[quadrant].shadows.size());
  3498. uint32_t quadrant_size = shadow_atlas->size >> 1;
  3499. x = (quadrant & 1) * quadrant_size;
  3500. y = (quadrant >> 1) * quadrant_size;
  3501. uint32_t shadow_size = (quadrant_size / shadow_atlas->quadrants[quadrant].subdivision);
  3502. x += (shadow % shadow_atlas->quadrants[quadrant].subdivision) * shadow_size;
  3503. y += (shadow / shadow_atlas->quadrants[quadrant].subdivision) * shadow_size;
  3504. width = shadow_size;
  3505. height = shadow_size;
  3506. if (light->type == VS::LIGHT_OMNI) {
  3507. if (light->omni_shadow_mode == VS::LIGHT_OMNI_SHADOW_CUBE) {
  3508. int cubemap_index = shadow_cubemaps.size() - 1;
  3509. for (int i = shadow_cubemaps.size() - 1; i >= 0; i--) {
  3510. //find appropriate cubemap to render to
  3511. if (shadow_cubemaps[i].size > shadow_size * 2)
  3512. break;
  3513. cubemap_index = i;
  3514. }
  3515. fbo = shadow_cubemaps[cubemap_index].fbo_id[p_pass];
  3516. light_projection = light_instance->shadow_transform[0].camera;
  3517. light_transform = light_instance->shadow_transform[0].transform;
  3518. custom_vp_size = shadow_cubemaps[cubemap_index].size;
  3519. zfar = light->param[VS::LIGHT_PARAM_RANGE];
  3520. current_cubemap = cubemap_index;
  3521. } else {
  3522. light_projection = light_instance->shadow_transform[0].camera;
  3523. light_transform = light_instance->shadow_transform[0].transform;
  3524. if (light->omni_shadow_detail == VS::LIGHT_OMNI_SHADOW_DETAIL_HORIZONTAL) {
  3525. height /= 2;
  3526. y += p_pass * height;
  3527. } else {
  3528. width /= 2;
  3529. x += p_pass * width;
  3530. }
  3531. dp_direction = p_pass == 0 ? 1.0 : -1.0;
  3532. flip_facing = (p_pass == 1);
  3533. zfar = light->param[VS::LIGHT_PARAM_RANGE];
  3534. bias = light->param[VS::LIGHT_PARAM_SHADOW_BIAS];
  3535. state.scene_shader.set_conditional(SceneShaderGLES3::RENDER_DEPTH_DUAL_PARABOLOID, true);
  3536. }
  3537. } else if (light->type == VS::LIGHT_SPOT) {
  3538. light_projection = light_instance->shadow_transform[0].camera;
  3539. light_transform = light_instance->shadow_transform[0].transform;
  3540. dp_direction = 1.0;
  3541. flip_facing = false;
  3542. zfar = light->param[VS::LIGHT_PARAM_RANGE];
  3543. bias = light->param[VS::LIGHT_PARAM_SHADOW_BIAS];
  3544. normal_bias = light->param[VS::LIGHT_PARAM_SHADOW_NORMAL_BIAS];
  3545. }
  3546. }
  3547. render_list.clear();
  3548. _fill_render_list(p_cull_result, p_cull_count, true, true);
  3549. render_list.sort_by_depth(false); //shadow is front to back for performance
  3550. glDisable(GL_BLEND);
  3551. glDisable(GL_DITHER);
  3552. glEnable(GL_DEPTH_TEST);
  3553. glBindFramebuffer(GL_FRAMEBUFFER, fbo);
  3554. glDepthMask(true);
  3555. glColorMask(0, 0, 0, 0);
  3556. if (custom_vp_size) {
  3557. glViewport(0, 0, custom_vp_size, custom_vp_size);
  3558. glScissor(0, 0, custom_vp_size, custom_vp_size);
  3559. } else {
  3560. glViewport(x, y, width, height);
  3561. glScissor(x, y, width, height);
  3562. }
  3563. glEnable(GL_SCISSOR_TEST);
  3564. glClearDepth(1.0f);
  3565. glClear(GL_DEPTH_BUFFER_BIT);
  3566. glDisable(GL_SCISSOR_TEST);
  3567. state.ubo_data.z_offset = bias;
  3568. state.ubo_data.z_slope_scale = normal_bias;
  3569. state.ubo_data.shadow_dual_paraboloid_render_side = dp_direction;
  3570. state.ubo_data.shadow_dual_paraboloid_render_zfar = zfar;
  3571. state.ubo_data.opaque_prepass_threshold = 0.1;
  3572. _setup_environment(NULL, light_projection, light_transform);
  3573. state.scene_shader.set_conditional(SceneShaderGLES3::RENDER_DEPTH, true);
  3574. if (light->reverse_cull) {
  3575. flip_facing = !flip_facing;
  3576. }
  3577. _render_list(render_list.elements, render_list.element_count, light_transform, light_projection, 0, flip_facing, false, true, false, false);
  3578. state.scene_shader.set_conditional(SceneShaderGLES3::RENDER_DEPTH, false);
  3579. state.scene_shader.set_conditional(SceneShaderGLES3::RENDER_DEPTH_DUAL_PARABOLOID, false);
  3580. if (light->type == VS::LIGHT_OMNI && light->omni_shadow_mode == VS::LIGHT_OMNI_SHADOW_CUBE && p_pass == 5) {
  3581. //convert the chosen cubemap to dual paraboloid!
  3582. ShadowAtlas *shadow_atlas = shadow_atlas_owner.getornull(p_shadow_atlas);
  3583. glBindFramebuffer(GL_FRAMEBUFFER, shadow_atlas->fbo);
  3584. state.cube_to_dp_shader.bind();
  3585. glActiveTexture(GL_TEXTURE0);
  3586. glBindTexture(GL_TEXTURE_CUBE_MAP, shadow_cubemaps[current_cubemap].cubemap);
  3587. glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_COMPARE_MODE, GL_NONE);
  3588. glDisable(GL_CULL_FACE);
  3589. for (int i = 0; i < 2; i++) {
  3590. state.cube_to_dp_shader.set_uniform(CubeToDpShaderGLES3::Z_FLIP, i == 1);
  3591. state.cube_to_dp_shader.set_uniform(CubeToDpShaderGLES3::Z_NEAR, light_projection.get_z_near());
  3592. state.cube_to_dp_shader.set_uniform(CubeToDpShaderGLES3::Z_FAR, light_projection.get_z_far());
  3593. state.cube_to_dp_shader.set_uniform(CubeToDpShaderGLES3::BIAS, light->param[VS::LIGHT_PARAM_SHADOW_BIAS]);
  3594. uint32_t local_width = width, local_height = height;
  3595. uint32_t local_x = x, local_y = y;
  3596. if (light->omni_shadow_detail == VS::LIGHT_OMNI_SHADOW_DETAIL_HORIZONTAL) {
  3597. local_height /= 2;
  3598. local_y += i * local_height;
  3599. } else {
  3600. local_width /= 2;
  3601. local_x += i * local_width;
  3602. }
  3603. glViewport(local_x, local_y, local_width, local_height);
  3604. glScissor(local_x, local_y, local_width, local_height);
  3605. glEnable(GL_SCISSOR_TEST);
  3606. glClearDepth(1.0f);
  3607. glClear(GL_DEPTH_BUFFER_BIT);
  3608. glDisable(GL_SCISSOR_TEST);
  3609. //glDisable(GL_DEPTH_TEST);
  3610. glDisable(GL_BLEND);
  3611. _copy_screen();
  3612. }
  3613. }
  3614. glColorMask(1, 1, 1, 1);
  3615. }
  3616. void RasterizerSceneGLES3::set_scene_pass(uint64_t p_pass) {
  3617. scene_pass = p_pass;
  3618. }
  3619. bool RasterizerSceneGLES3::free(RID p_rid) {
  3620. if (light_instance_owner.owns(p_rid)) {
  3621. LightInstance *light_instance = light_instance_owner.getptr(p_rid);
  3622. //remove from shadow atlases..
  3623. for (Set<RID>::Element *E = light_instance->shadow_atlases.front(); E; E = E->next()) {
  3624. ShadowAtlas *shadow_atlas = shadow_atlas_owner.get(E->get());
  3625. ERR_CONTINUE(!shadow_atlas->shadow_owners.has(p_rid));
  3626. uint32_t key = shadow_atlas->shadow_owners[p_rid];
  3627. uint32_t q = (key >> ShadowAtlas::QUADRANT_SHIFT) & 0x3;
  3628. uint32_t s = key & ShadowAtlas::SHADOW_INDEX_MASK;
  3629. shadow_atlas->quadrants[q].shadows.write[s].owner = RID();
  3630. shadow_atlas->shadow_owners.erase(p_rid);
  3631. }
  3632. light_instance_owner.free(p_rid);
  3633. memdelete(light_instance);
  3634. } else if (shadow_atlas_owner.owns(p_rid)) {
  3635. ShadowAtlas *shadow_atlas = shadow_atlas_owner.get(p_rid);
  3636. shadow_atlas_set_size(p_rid, 0);
  3637. shadow_atlas_owner.free(p_rid);
  3638. memdelete(shadow_atlas);
  3639. } else if (reflection_atlas_owner.owns(p_rid)) {
  3640. ReflectionAtlas *reflection_atlas = reflection_atlas_owner.get(p_rid);
  3641. reflection_atlas_set_size(p_rid, 0);
  3642. reflection_atlas_owner.free(p_rid);
  3643. memdelete(reflection_atlas);
  3644. } else if (reflection_probe_instance_owner.owns(p_rid)) {
  3645. ReflectionProbeInstance *reflection_instance = reflection_probe_instance_owner.get(p_rid);
  3646. reflection_probe_release_atlas_index(p_rid);
  3647. reflection_probe_instance_owner.free(p_rid);
  3648. memdelete(reflection_instance);
  3649. } else {
  3650. return false;
  3651. }
  3652. return true;
  3653. }
  3654. void RasterizerSceneGLES3::set_debug_draw_mode(VS::ViewportDebugDraw p_debug_draw) {
  3655. state.debug_draw = p_debug_draw;
  3656. }
  3657. void RasterizerSceneGLES3::initialize() {
  3658. render_pass = 0;
  3659. state.scene_shader.init();
  3660. {
  3661. //default material and shader
  3662. default_shader = storage->shader_create();
  3663. storage->shader_set_code(default_shader, "shader_type spatial;\n");
  3664. default_material = storage->material_create();
  3665. storage->material_set_shader(default_material, default_shader);
  3666. default_shader_twosided = storage->shader_create();
  3667. default_material_twosided = storage->material_create();
  3668. storage->shader_set_code(default_shader_twosided, "shader_type spatial; render_mode cull_disabled;\n");
  3669. storage->material_set_shader(default_material_twosided, default_shader_twosided);
  3670. //default for shaders using world coordinates (typical for triplanar)
  3671. default_worldcoord_shader = storage->shader_create();
  3672. storage->shader_set_code(default_worldcoord_shader, "shader_type spatial; render_mode world_vertex_coords;\n");
  3673. default_worldcoord_material = storage->material_create();
  3674. storage->material_set_shader(default_worldcoord_material, default_worldcoord_shader);
  3675. default_worldcoord_shader_twosided = storage->shader_create();
  3676. default_worldcoord_material_twosided = storage->material_create();
  3677. storage->shader_set_code(default_worldcoord_shader_twosided, "shader_type spatial; render_mode cull_disabled,world_vertex_coords;\n");
  3678. storage->material_set_shader(default_worldcoord_material_twosided, default_worldcoord_shader_twosided);
  3679. }
  3680. {
  3681. //default material and shader
  3682. default_overdraw_shader = storage->shader_create();
  3683. 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; }");
  3684. default_overdraw_material = storage->material_create();
  3685. storage->material_set_shader(default_overdraw_material, default_overdraw_shader);
  3686. }
  3687. glGenBuffers(1, &state.scene_ubo);
  3688. glBindBuffer(GL_UNIFORM_BUFFER, state.scene_ubo);
  3689. glBufferData(GL_UNIFORM_BUFFER, sizeof(State::SceneDataUBO), &state.scene_ubo, GL_DYNAMIC_DRAW);
  3690. glBindBuffer(GL_UNIFORM_BUFFER, 0);
  3691. glGenBuffers(1, &state.env_radiance_ubo);
  3692. glBindBuffer(GL_UNIFORM_BUFFER, state.env_radiance_ubo);
  3693. glBufferData(GL_UNIFORM_BUFFER, sizeof(State::EnvironmentRadianceUBO), &state.env_radiance_ubo, GL_DYNAMIC_DRAW);
  3694. glBindBuffer(GL_UNIFORM_BUFFER, 0);
  3695. render_list.max_elements = GLOBAL_DEF_RST("rendering/limits/rendering/max_renderable_elements", (int)RenderList::DEFAULT_MAX_ELEMENTS);
  3696. 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"));
  3697. {
  3698. //quad buffers
  3699. glGenBuffers(1, &state.sky_verts);
  3700. glBindBuffer(GL_ARRAY_BUFFER, state.sky_verts);
  3701. glBufferData(GL_ARRAY_BUFFER, sizeof(Vector3) * 8, NULL, GL_DYNAMIC_DRAW);
  3702. glBindBuffer(GL_ARRAY_BUFFER, 0); //unbind
  3703. glGenVertexArrays(1, &state.sky_array);
  3704. glBindVertexArray(state.sky_array);
  3705. glBindBuffer(GL_ARRAY_BUFFER, state.sky_verts);
  3706. glVertexAttribPointer(VS::ARRAY_VERTEX, 3, GL_FLOAT, GL_FALSE, sizeof(Vector3) * 2, 0);
  3707. glEnableVertexAttribArray(VS::ARRAY_VERTEX);
  3708. glVertexAttribPointer(VS::ARRAY_TEX_UV, 3, GL_FLOAT, GL_FALSE, sizeof(Vector3) * 2, ((uint8_t *)NULL) + sizeof(Vector3));
  3709. glEnableVertexAttribArray(VS::ARRAY_TEX_UV);
  3710. glBindVertexArray(0);
  3711. glBindBuffer(GL_ARRAY_BUFFER, 0); //unbind
  3712. }
  3713. render_list.init();
  3714. state.cube_to_dp_shader.init();
  3715. shadow_atlas_realloc_tolerance_msec = 500;
  3716. int max_shadow_cubemap_sampler_size = 512;
  3717. int cube_size = max_shadow_cubemap_sampler_size;
  3718. glActiveTexture(GL_TEXTURE0);
  3719. while (cube_size >= 32) {
  3720. ShadowCubeMap cube;
  3721. cube.size = cube_size;
  3722. glGenTextures(1, &cube.cubemap);
  3723. glBindTexture(GL_TEXTURE_CUBE_MAP, cube.cubemap);
  3724. //gen cubemap first
  3725. for (int i = 0; i < 6; i++) {
  3726. glTexImage2D(_cube_side_enum[i], 0, GL_DEPTH_COMPONENT24, cube.size, cube.size, 0, GL_DEPTH_COMPONENT, GL_UNSIGNED_INT, NULL);
  3727. }
  3728. glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  3729. glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  3730. // Remove artifact on the edges of the shadowmap
  3731. glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  3732. glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  3733. glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
  3734. //gen renderbuffers second, because it needs a complete cubemap
  3735. for (int i = 0; i < 6; i++) {
  3736. glGenFramebuffers(1, &cube.fbo_id[i]);
  3737. glBindFramebuffer(GL_FRAMEBUFFER, cube.fbo_id[i]);
  3738. glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, _cube_side_enum[i], cube.cubemap, 0);
  3739. GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
  3740. ERR_CONTINUE(status != GL_FRAMEBUFFER_COMPLETE);
  3741. }
  3742. shadow_cubemaps.push_back(cube);
  3743. cube_size >>= 1;
  3744. }
  3745. {
  3746. //directional light shadow
  3747. directional_shadow.light_count = 0;
  3748. directional_shadow.size = next_power_of_2(GLOBAL_GET("rendering/quality/directional_shadow/size"));
  3749. glGenFramebuffers(1, &directional_shadow.fbo);
  3750. glBindFramebuffer(GL_FRAMEBUFFER, directional_shadow.fbo);
  3751. glGenTextures(1, &directional_shadow.depth);
  3752. glBindTexture(GL_TEXTURE_2D, directional_shadow.depth);
  3753. glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT24, directional_shadow.size, directional_shadow.size, 0, GL_DEPTH_COMPONENT, GL_UNSIGNED_INT, NULL);
  3754. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  3755. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  3756. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  3757. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  3758. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
  3759. glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, directional_shadow.depth, 0);
  3760. GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
  3761. if (status != GL_FRAMEBUFFER_COMPLETE) {
  3762. ERR_PRINT("Directional shadow framebuffer status invalid");
  3763. }
  3764. }
  3765. {
  3766. //spot and omni ubos
  3767. int max_ubo_size;
  3768. glGetIntegerv(GL_MAX_UNIFORM_BLOCK_SIZE, &max_ubo_size);
  3769. const int ubo_light_size = 160;
  3770. state.ubo_light_size = ubo_light_size;
  3771. state.max_ubo_lights = MIN(RenderList::MAX_LIGHTS, max_ubo_size / ubo_light_size);
  3772. state.spot_array_tmp = (uint8_t *)memalloc(ubo_light_size * state.max_ubo_lights);
  3773. state.omni_array_tmp = (uint8_t *)memalloc(ubo_light_size * state.max_ubo_lights);
  3774. glGenBuffers(1, &state.spot_array_ubo);
  3775. glBindBuffer(GL_UNIFORM_BUFFER, state.spot_array_ubo);
  3776. glBufferData(GL_UNIFORM_BUFFER, ubo_light_size * state.max_ubo_lights, NULL, GL_DYNAMIC_DRAW);
  3777. glBindBuffer(GL_UNIFORM_BUFFER, 0);
  3778. glGenBuffers(1, &state.omni_array_ubo);
  3779. glBindBuffer(GL_UNIFORM_BUFFER, state.omni_array_ubo);
  3780. glBufferData(GL_UNIFORM_BUFFER, ubo_light_size * state.max_ubo_lights, NULL, GL_DYNAMIC_DRAW);
  3781. glBindBuffer(GL_UNIFORM_BUFFER, 0);
  3782. glGenBuffers(1, &state.directional_ubo);
  3783. glBindBuffer(GL_UNIFORM_BUFFER, state.directional_ubo);
  3784. glBufferData(GL_UNIFORM_BUFFER, sizeof(LightDataUBO), NULL, GL_DYNAMIC_DRAW);
  3785. glBindBuffer(GL_UNIFORM_BUFFER, 0);
  3786. state.max_forward_lights_per_object = 8;
  3787. state.scene_shader.add_custom_define("#define MAX_LIGHT_DATA_STRUCTS " + itos(state.max_ubo_lights) + "\n");
  3788. state.scene_shader.add_custom_define("#define MAX_FORWARD_LIGHTS " + itos(state.max_forward_lights_per_object) + "\n");
  3789. state.max_ubo_reflections = MIN(RenderList::MAX_REFLECTIONS, max_ubo_size / sizeof(ReflectionProbeDataUBO));
  3790. state.reflection_array_tmp = (uint8_t *)memalloc(sizeof(ReflectionProbeDataUBO) * state.max_ubo_reflections);
  3791. glGenBuffers(1, &state.reflection_array_ubo);
  3792. glBindBuffer(GL_UNIFORM_BUFFER, state.reflection_array_ubo);
  3793. glBufferData(GL_UNIFORM_BUFFER, sizeof(ReflectionProbeDataUBO) * state.max_ubo_reflections, NULL, GL_DYNAMIC_DRAW);
  3794. glBindBuffer(GL_UNIFORM_BUFFER, 0);
  3795. state.scene_shader.add_custom_define("#define MAX_REFLECTION_DATA_STRUCTS " + itos(state.max_ubo_reflections) + "\n");
  3796. state.max_skeleton_bones = MIN(2048, max_ubo_size / (12 * sizeof(float)));
  3797. state.scene_shader.add_custom_define("#define MAX_SKELETON_BONES " + itos(state.max_skeleton_bones) + "\n");
  3798. }
  3799. shadow_filter_mode = SHADOW_FILTER_NEAREST;
  3800. { //reflection cubemaps
  3801. int max_reflection_cubemap_sampler_size = 512;
  3802. int cube_size = max_reflection_cubemap_sampler_size;
  3803. glActiveTexture(GL_TEXTURE0);
  3804. bool use_float = true;
  3805. GLenum internal_format = use_float ? GL_RGBA16F : GL_RGB10_A2;
  3806. GLenum format = GL_RGBA;
  3807. GLenum type = use_float ? GL_HALF_FLOAT : GL_UNSIGNED_INT_2_10_10_10_REV;
  3808. while (cube_size >= 32) {
  3809. ReflectionCubeMap cube;
  3810. cube.size = cube_size;
  3811. glGenTextures(1, &cube.depth);
  3812. glBindTexture(GL_TEXTURE_2D, cube.depth);
  3813. glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT24, cube.size, cube.size, 0, GL_DEPTH_COMPONENT, GL_UNSIGNED_INT, NULL);
  3814. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  3815. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  3816. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  3817. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  3818. glGenTextures(1, &cube.cubemap);
  3819. glBindTexture(GL_TEXTURE_CUBE_MAP, cube.cubemap);
  3820. //gen cubemap first
  3821. for (int i = 0; i < 6; i++) {
  3822. glTexImage2D(_cube_side_enum[i], 0, internal_format, cube.size, cube.size, 0, format, type, NULL);
  3823. }
  3824. glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  3825. glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  3826. // Remove artifact on the edges of the reflectionmap
  3827. glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  3828. glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  3829. glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
  3830. //gen renderbuffers second, because it needs a complete cubemap
  3831. for (int i = 0; i < 6; i++) {
  3832. glGenFramebuffers(1, &cube.fbo_id[i]);
  3833. glBindFramebuffer(GL_FRAMEBUFFER, cube.fbo_id[i]);
  3834. glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, _cube_side_enum[i], cube.cubemap, 0);
  3835. glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, cube.depth, 0);
  3836. GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
  3837. ERR_CONTINUE(status != GL_FRAMEBUFFER_COMPLETE);
  3838. }
  3839. reflection_cubemaps.push_back(cube);
  3840. cube_size >>= 1;
  3841. }
  3842. }
  3843. {
  3844. uint32_t immediate_buffer_size = GLOBAL_DEF("rendering/limits/buffers/immediate_buffer_size_kb", 2048);
  3845. 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"));
  3846. glGenBuffers(1, &state.immediate_buffer);
  3847. glBindBuffer(GL_ARRAY_BUFFER, state.immediate_buffer);
  3848. glBufferData(GL_ARRAY_BUFFER, immediate_buffer_size * 1024, NULL, GL_DYNAMIC_DRAW);
  3849. glBindBuffer(GL_ARRAY_BUFFER, 0);
  3850. glGenVertexArrays(1, &state.immediate_array);
  3851. }
  3852. #ifdef GLES_OVER_GL
  3853. //"desktop" opengl needs this.
  3854. glEnable(GL_PROGRAM_POINT_SIZE);
  3855. #endif
  3856. state.resolve_shader.init();
  3857. state.ssr_shader.init();
  3858. state.effect_blur_shader.init();
  3859. state.sss_shader.init();
  3860. state.ssao_minify_shader.init();
  3861. state.ssao_shader.init();
  3862. state.ssao_blur_shader.init();
  3863. state.exposure_shader.init();
  3864. state.tonemap_shader.init();
  3865. {
  3866. GLOBAL_DEF("rendering/quality/subsurface_scattering/quality", 1);
  3867. 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"));
  3868. GLOBAL_DEF("rendering/quality/subsurface_scattering/scale", 1.0);
  3869. 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"));
  3870. GLOBAL_DEF("rendering/quality/subsurface_scattering/follow_surface", false);
  3871. GLOBAL_DEF("rendering/quality/subsurface_scattering/weight_samples", true);
  3872. GLOBAL_DEF("rendering/quality/voxel_cone_tracing/high_quality", true);
  3873. }
  3874. exposure_shrink_size = 243;
  3875. int max_exposure_shrink_size = exposure_shrink_size;
  3876. while (max_exposure_shrink_size > 0) {
  3877. RasterizerStorageGLES3::RenderTarget::Exposure e;
  3878. glGenFramebuffers(1, &e.fbo);
  3879. glBindFramebuffer(GL_FRAMEBUFFER, e.fbo);
  3880. glGenTextures(1, &e.color);
  3881. glBindTexture(GL_TEXTURE_2D, e.color);
  3882. #ifdef IPHONE_ENABLED
  3883. ///@TODO ugly hack to get around iOS not supporting 32bit single channel floating point textures...
  3884. glTexImage2D(GL_TEXTURE_2D, 0, GL_R16F, max_exposure_shrink_size, max_exposure_shrink_size, 0, GL_RED, GL_FLOAT, NULL);
  3885. #else
  3886. glTexImage2D(GL_TEXTURE_2D, 0, GL_R32F, max_exposure_shrink_size, max_exposure_shrink_size, 0, GL_RED, GL_FLOAT, NULL);
  3887. #endif
  3888. glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, e.color, 0);
  3889. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  3890. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  3891. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  3892. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  3893. exposure_shrink.push_back(e);
  3894. max_exposure_shrink_size /= 3;
  3895. GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
  3896. ERR_CONTINUE(status != GL_FRAMEBUFFER_COMPLETE);
  3897. }
  3898. state.debug_draw = VS::VIEWPORT_DEBUG_DRAW_DISABLED;
  3899. glFrontFace(GL_CW);
  3900. }
  3901. void RasterizerSceneGLES3::iteration() {
  3902. shadow_filter_mode = ShadowFilterMode(int(GLOBAL_GET("rendering/quality/shadows/filter_mode")));
  3903. subsurface_scatter_follow_surface = GLOBAL_GET("rendering/quality/subsurface_scattering/follow_surface");
  3904. subsurface_scatter_weight_samples = GLOBAL_GET("rendering/quality/subsurface_scattering/weight_samples");
  3905. subsurface_scatter_quality = SubSurfaceScatterQuality(int(GLOBAL_GET("rendering/quality/subsurface_scattering/quality")));
  3906. subsurface_scatter_size = GLOBAL_GET("rendering/quality/subsurface_scattering/scale");
  3907. state.scene_shader.set_conditional(SceneShaderGLES3::VCT_QUALITY_HIGH, GLOBAL_GET("rendering/quality/voxel_cone_tracing/high_quality"));
  3908. }
  3909. void RasterizerSceneGLES3::finalize() {
  3910. }
  3911. RasterizerSceneGLES3::RasterizerSceneGLES3() {
  3912. }
  3913. RasterizerSceneGLES3::~RasterizerSceneGLES3() {
  3914. memdelete(default_material.get_data());
  3915. memdelete(default_material_twosided.get_data());
  3916. memdelete(default_shader.get_data());
  3917. memdelete(default_shader_twosided.get_data());
  3918. memdelete(default_worldcoord_material.get_data());
  3919. memdelete(default_worldcoord_material_twosided.get_data());
  3920. memdelete(default_worldcoord_shader.get_data());
  3921. memdelete(default_worldcoord_shader_twosided.get_data());
  3922. memdelete(default_overdraw_material.get_data());
  3923. memdelete(default_overdraw_shader.get_data());
  3924. memfree(state.spot_array_tmp);
  3925. memfree(state.omni_array_tmp);
  3926. memfree(state.reflection_array_tmp);
  3927. }