rasterizer_scene_gles3.cpp 198 KB

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