rasterizer_storage_gles2.cpp 140 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824
  1. /*************************************************************************/
  2. /* rasterizer_storage_gles2.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_storage_gles2.h"
  31. #include "core/math/transform.h"
  32. #include "core/project_settings.h"
  33. #include "rasterizer_canvas_gles2.h"
  34. #include "rasterizer_scene_gles2.h"
  35. #include "servers/visual/shader_language.h"
  36. GLuint RasterizerStorageGLES2::system_fbo = 0;
  37. /* TEXTURE API */
  38. #define _EXT_COMPRESSED_RGBA_S3TC_DXT1_EXT 0x83F1
  39. #define _EXT_COMPRESSED_RGBA_S3TC_DXT3_EXT 0x83F2
  40. #define _EXT_COMPRESSED_RGBA_S3TC_DXT5_EXT 0x83F3
  41. #define _EXT_ETC1_RGB8_OES 0x8D64
  42. #ifdef GLES_OVER_GL
  43. #define _GL_HALF_FLOAT_OES 0x140B
  44. #else
  45. #define _GL_HALF_FLOAT_OES 0x8D61
  46. #endif
  47. #define _EXT_TEXTURE_CUBE_MAP_SEAMLESS 0x884F
  48. #define _DEPTH_COMPONENT24_OES 0x81A6
  49. #define _RED_OES 0x1903
  50. void RasterizerStorageGLES2::bind_quad_array() const {
  51. glBindBuffer(GL_ARRAY_BUFFER, resources.quadie);
  52. glVertexAttribPointer(VS::ARRAY_VERTEX, 2, GL_FLOAT, GL_FALSE, sizeof(float) * 4, 0);
  53. glVertexAttribPointer(VS::ARRAY_TEX_UV, 2, GL_FLOAT, GL_FALSE, sizeof(float) * 4, ((uint8_t *)NULL) + 8);
  54. glEnableVertexAttribArray(VS::ARRAY_VERTEX);
  55. glEnableVertexAttribArray(VS::ARRAY_TEX_UV);
  56. }
  57. Ref<Image> RasterizerStorageGLES2::_get_gl_image_and_format(const Ref<Image> &p_image, Image::Format p_format, uint32_t p_flags, Image::Format &r_real_format, GLenum &r_gl_format, GLenum &r_gl_internal_format, GLenum &r_gl_type, bool &r_compressed) const {
  58. r_gl_format = 0;
  59. Ref<Image> image = p_image;
  60. r_compressed = false;
  61. r_real_format = p_format;
  62. bool need_decompress = false;
  63. switch (p_format) {
  64. case Image::FORMAT_L8: {
  65. r_gl_internal_format = GL_LUMINANCE;
  66. r_gl_format = GL_LUMINANCE;
  67. r_gl_type = GL_UNSIGNED_BYTE;
  68. } break;
  69. case Image::FORMAT_LA8: {
  70. r_gl_internal_format = GL_LUMINANCE_ALPHA;
  71. r_gl_format = GL_LUMINANCE_ALPHA;
  72. r_gl_type = GL_UNSIGNED_BYTE;
  73. } break;
  74. case Image::FORMAT_R8: {
  75. r_gl_internal_format = GL_ALPHA;
  76. r_gl_format = GL_ALPHA;
  77. r_gl_type = GL_UNSIGNED_BYTE;
  78. } break;
  79. case Image::FORMAT_RG8: {
  80. ERR_EXPLAIN("RG texture not supported");
  81. ERR_FAIL_V(image);
  82. } break;
  83. case Image::FORMAT_RGB8: {
  84. r_gl_internal_format = GL_RGB;
  85. r_gl_format = GL_RGB;
  86. r_gl_type = GL_UNSIGNED_BYTE;
  87. } break;
  88. case Image::FORMAT_RGBA8: {
  89. r_gl_format = GL_RGBA;
  90. r_gl_internal_format = GL_RGBA;
  91. r_gl_type = GL_UNSIGNED_BYTE;
  92. } break;
  93. case Image::FORMAT_RGBA4444: {
  94. r_gl_internal_format = GL_RGBA;
  95. r_gl_format = GL_RGBA;
  96. r_gl_type = GL_UNSIGNED_SHORT_4_4_4_4;
  97. } break;
  98. case Image::FORMAT_RGBA5551: {
  99. r_gl_internal_format = GL_RGB5_A1;
  100. r_gl_format = GL_RGBA;
  101. r_gl_type = GL_UNSIGNED_SHORT_5_5_5_1;
  102. } break;
  103. case Image::FORMAT_RF: {
  104. if (!config.float_texture_supported) {
  105. ERR_EXPLAIN("R float texture not supported");
  106. ERR_FAIL_V(image);
  107. }
  108. r_gl_internal_format = GL_ALPHA;
  109. r_gl_format = GL_ALPHA;
  110. r_gl_type = GL_FLOAT;
  111. } break;
  112. case Image::FORMAT_RGF: {
  113. ERR_EXPLAIN("RG float texture not supported");
  114. ERR_FAIL_V(image);
  115. } break;
  116. case Image::FORMAT_RGBF: {
  117. if (!config.float_texture_supported) {
  118. ERR_EXPLAIN("RGB float texture not supported");
  119. ERR_FAIL_V(image);
  120. }
  121. r_gl_internal_format = GL_RGB;
  122. r_gl_format = GL_RGB;
  123. r_gl_type = GL_FLOAT;
  124. } break;
  125. case Image::FORMAT_RGBAF: {
  126. if (!config.float_texture_supported) {
  127. ERR_EXPLAIN("RGBA float texture not supported");
  128. ERR_FAIL_V(image);
  129. }
  130. r_gl_internal_format = GL_RGBA;
  131. r_gl_format = GL_RGBA;
  132. r_gl_type = GL_FLOAT;
  133. } break;
  134. case Image::FORMAT_RH: {
  135. need_decompress = true;
  136. } break;
  137. case Image::FORMAT_RGH: {
  138. need_decompress = true;
  139. } break;
  140. case Image::FORMAT_RGBH: {
  141. need_decompress = true;
  142. } break;
  143. case Image::FORMAT_RGBAH: {
  144. need_decompress = true;
  145. } break;
  146. case Image::FORMAT_RGBE9995: {
  147. r_gl_internal_format = GL_RGB;
  148. r_gl_format = GL_RGB;
  149. r_gl_type = GL_UNSIGNED_BYTE;
  150. if (image.is_valid())
  151. image = image->rgbe_to_srgb();
  152. return image;
  153. } break;
  154. case Image::FORMAT_DXT1: {
  155. r_compressed = true;
  156. if (config.s3tc_supported) {
  157. r_gl_internal_format = _EXT_COMPRESSED_RGBA_S3TC_DXT1_EXT;
  158. r_gl_format = GL_RGBA;
  159. r_gl_type = GL_UNSIGNED_BYTE;
  160. } else {
  161. need_decompress = true;
  162. }
  163. } break;
  164. case Image::FORMAT_DXT3: {
  165. if (config.s3tc_supported) {
  166. r_gl_internal_format = _EXT_COMPRESSED_RGBA_S3TC_DXT3_EXT;
  167. r_gl_format = GL_RGBA;
  168. r_gl_type = GL_UNSIGNED_BYTE;
  169. r_compressed = true;
  170. } else {
  171. need_decompress = true;
  172. }
  173. } break;
  174. case Image::FORMAT_DXT5: {
  175. if (config.s3tc_supported) {
  176. r_gl_internal_format = _EXT_COMPRESSED_RGBA_S3TC_DXT5_EXT;
  177. r_gl_format = GL_RGBA;
  178. r_gl_type = GL_UNSIGNED_BYTE;
  179. r_compressed = true;
  180. } else {
  181. need_decompress = true;
  182. }
  183. } break;
  184. case Image::FORMAT_RGTC_R: {
  185. need_decompress = true;
  186. } break;
  187. case Image::FORMAT_RGTC_RG: {
  188. need_decompress = true;
  189. } break;
  190. case Image::FORMAT_BPTC_RGBA: {
  191. need_decompress = true;
  192. } break;
  193. case Image::FORMAT_BPTC_RGBF: {
  194. need_decompress = true;
  195. } break;
  196. case Image::FORMAT_BPTC_RGBFU: {
  197. need_decompress = true;
  198. } break;
  199. case Image::FORMAT_PVRTC2: {
  200. need_decompress = true;
  201. } break;
  202. case Image::FORMAT_PVRTC2A: {
  203. need_decompress = true;
  204. } break;
  205. case Image::FORMAT_PVRTC4: {
  206. need_decompress = true;
  207. } break;
  208. case Image::FORMAT_PVRTC4A: {
  209. need_decompress = true;
  210. } break;
  211. case Image::FORMAT_ETC: {
  212. if (config.etc1_supported) {
  213. r_gl_internal_format = _EXT_ETC1_RGB8_OES;
  214. r_gl_format = GL_RGBA;
  215. r_gl_type = GL_UNSIGNED_BYTE;
  216. r_compressed = true;
  217. } else {
  218. need_decompress = true;
  219. }
  220. } break;
  221. case Image::FORMAT_ETC2_R11: {
  222. need_decompress = true;
  223. } break;
  224. case Image::FORMAT_ETC2_R11S: {
  225. need_decompress = true;
  226. } break;
  227. case Image::FORMAT_ETC2_RG11: {
  228. need_decompress = true;
  229. } break;
  230. case Image::FORMAT_ETC2_RG11S: {
  231. need_decompress = true;
  232. } break;
  233. case Image::FORMAT_ETC2_RGB8: {
  234. need_decompress = true;
  235. } break;
  236. case Image::FORMAT_ETC2_RGBA8: {
  237. need_decompress = true;
  238. } break;
  239. case Image::FORMAT_ETC2_RGB8A1: {
  240. need_decompress = true;
  241. } break;
  242. default: {
  243. ERR_FAIL_V(Ref<Image>());
  244. }
  245. }
  246. if (need_decompress) {
  247. if (!image.is_null()) {
  248. image = image->duplicate();
  249. image->decompress();
  250. ERR_FAIL_COND_V(image->is_compressed(), image);
  251. image->convert(Image::FORMAT_RGBA8);
  252. }
  253. r_gl_format = GL_RGBA;
  254. r_gl_internal_format = GL_RGBA;
  255. r_gl_type = GL_UNSIGNED_BYTE;
  256. r_real_format = Image::FORMAT_RGBA8;
  257. return image;
  258. }
  259. return p_image;
  260. }
  261. static const GLenum _cube_side_enum[6] = {
  262. GL_TEXTURE_CUBE_MAP_NEGATIVE_X,
  263. GL_TEXTURE_CUBE_MAP_POSITIVE_X,
  264. GL_TEXTURE_CUBE_MAP_NEGATIVE_Y,
  265. GL_TEXTURE_CUBE_MAP_POSITIVE_Y,
  266. GL_TEXTURE_CUBE_MAP_NEGATIVE_Z,
  267. GL_TEXTURE_CUBE_MAP_POSITIVE_Z,
  268. };
  269. RID RasterizerStorageGLES2::texture_create() {
  270. Texture *texture = memnew(Texture);
  271. ERR_FAIL_COND_V(!texture, RID());
  272. glGenTextures(1, &texture->tex_id);
  273. texture->active = false;
  274. texture->total_data_size = 0;
  275. return texture_owner.make_rid(texture);
  276. }
  277. void RasterizerStorageGLES2::texture_allocate(RID p_texture, int p_width, int p_height, int p_depth_3d, Image::Format p_format, VisualServer::TextureType p_type, uint32_t p_flags) {
  278. GLenum format;
  279. GLenum internal_format;
  280. GLenum type;
  281. bool compressed = false;
  282. if (p_flags & VS::TEXTURE_FLAG_USED_FOR_STREAMING) {
  283. p_flags &= ~VS::TEXTURE_FLAG_MIPMAPS; // no mipies for video
  284. }
  285. Texture *texture = texture_owner.getornull(p_texture);
  286. ERR_FAIL_COND(!texture);
  287. texture->width = p_width;
  288. texture->height = p_height;
  289. texture->format = p_format;
  290. texture->flags = p_flags;
  291. texture->stored_cube_sides = 0;
  292. texture->type = p_type;
  293. switch (p_type) {
  294. case VS::TEXTURE_TYPE_2D: {
  295. texture->target = GL_TEXTURE_2D;
  296. texture->images.resize(1);
  297. } break;
  298. case VS::TEXTURE_TYPE_CUBEMAP: {
  299. texture->target = GL_TEXTURE_CUBE_MAP;
  300. texture->images.resize(6);
  301. } break;
  302. case VS::TEXTURE_TYPE_2D_ARRAY: {
  303. texture->images.resize(p_depth_3d);
  304. } break;
  305. case VS::TEXTURE_TYPE_3D: {
  306. texture->images.resize(p_depth_3d);
  307. } break;
  308. default: {
  309. ERR_PRINT("Unknown texture type!");
  310. return;
  311. }
  312. }
  313. Image::Format real_format;
  314. _get_gl_image_and_format(Ref<Image>(), texture->format, texture->flags, real_format, format, internal_format, type, compressed);
  315. texture->alloc_width = texture->width;
  316. texture->alloc_height = texture->height;
  317. texture->gl_format_cache = format;
  318. texture->gl_type_cache = type;
  319. texture->gl_internal_format_cache = internal_format;
  320. texture->data_size = 0;
  321. texture->mipmaps = 1;
  322. texture->compressed = compressed;
  323. glActiveTexture(GL_TEXTURE0);
  324. glBindTexture(texture->target, texture->tex_id);
  325. if (p_flags & VS::TEXTURE_FLAG_USED_FOR_STREAMING) {
  326. //prealloc if video
  327. glTexImage2D(texture->target, 0, internal_format, p_width, p_height, 0, format, type, NULL);
  328. }
  329. texture->active = true;
  330. }
  331. void RasterizerStorageGLES2::texture_set_data(RID p_texture, const Ref<Image> &p_image, int p_layer) {
  332. Texture *texture = texture_owner.getornull(p_texture);
  333. ERR_FAIL_COND(!texture);
  334. ERR_FAIL_COND(!texture->active);
  335. ERR_FAIL_COND(texture->render_target);
  336. ERR_FAIL_COND(texture->format != p_image->get_format());
  337. ERR_FAIL_COND(p_image.is_null());
  338. GLenum type;
  339. GLenum format;
  340. GLenum internal_format;
  341. bool compressed = false;
  342. if (config.keep_original_textures && !(texture->flags & VS::TEXTURE_FLAG_USED_FOR_STREAMING)) {
  343. texture->images.write[p_layer] = p_image;
  344. }
  345. Image::Format real_format;
  346. Ref<Image> img = _get_gl_image_and_format(p_image, p_image->get_format(), texture->flags, real_format, format, internal_format, type, compressed);
  347. if (config.shrink_textures_x2 && (p_image->has_mipmaps() || !p_image->is_compressed()) && !(texture->flags & VS::TEXTURE_FLAG_USED_FOR_STREAMING)) {
  348. texture->alloc_height = MAX(1, texture->alloc_height / 2);
  349. texture->alloc_width = MAX(1, texture->alloc_width / 2);
  350. if (texture->alloc_width == img->get_width() / 2 && texture->alloc_height == img->get_height() / 2) {
  351. img->shrink_x2();
  352. } else if (img->get_format() <= Image::FORMAT_RGBA8) {
  353. img->resize(texture->alloc_width, texture->alloc_height, Image::INTERPOLATE_BILINEAR);
  354. }
  355. };
  356. GLenum blit_target = (texture->target == GL_TEXTURE_CUBE_MAP) ? _cube_side_enum[p_layer] : GL_TEXTURE_2D;
  357. texture->data_size = img->get_data().size();
  358. PoolVector<uint8_t>::Read read = img->get_data().read();
  359. glActiveTexture(GL_TEXTURE0);
  360. glBindTexture(texture->target, texture->tex_id);
  361. texture->ignore_mipmaps = compressed && !img->has_mipmaps();
  362. if ((texture->flags & VS::TEXTURE_FLAG_MIPMAPS) && !texture->ignore_mipmaps)
  363. glTexParameteri(texture->target, GL_TEXTURE_MIN_FILTER, config.use_fast_texture_filter ? GL_LINEAR_MIPMAP_NEAREST : GL_LINEAR_MIPMAP_LINEAR);
  364. else {
  365. if (texture->flags & VS::TEXTURE_FLAG_FILTER) {
  366. glTexParameteri(texture->target, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  367. } else {
  368. glTexParameteri(texture->target, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  369. }
  370. }
  371. if (texture->flags & VS::TEXTURE_FLAG_FILTER) {
  372. glTexParameteri(texture->target, GL_TEXTURE_MAG_FILTER, GL_LINEAR); // Linear Filtering
  373. } else {
  374. glTexParameteri(texture->target, GL_TEXTURE_MAG_FILTER, GL_NEAREST); // raw Filtering
  375. }
  376. if (((texture->flags & VS::TEXTURE_FLAG_REPEAT) || (texture->flags & VS::TEXTURE_FLAG_MIRRORED_REPEAT)) && texture->target != GL_TEXTURE_CUBE_MAP) {
  377. if (texture->flags & VS::TEXTURE_FLAG_MIRRORED_REPEAT) {
  378. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_MIRRORED_REPEAT);
  379. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_MIRRORED_REPEAT);
  380. } else {
  381. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
  382. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
  383. }
  384. } else {
  385. //glTexParameterf( texture->target, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE );
  386. glTexParameterf(texture->target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  387. glTexParameterf(texture->target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  388. }
  389. int mipmaps = ((texture->flags & VS::TEXTURE_FLAG_MIPMAPS) && img->has_mipmaps()) ? img->get_mipmap_count() + 1 : 1;
  390. int w = img->get_width();
  391. int h = img->get_height();
  392. int tsize = 0;
  393. for (int i = 0; i < mipmaps; i++) {
  394. int size, ofs;
  395. img->get_mipmap_offset_and_size(i, ofs, size);
  396. if (texture->compressed) {
  397. glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
  398. int bw = w;
  399. int bh = h;
  400. glCompressedTexImage2D(blit_target, i, internal_format, bw, bh, 0, size, &read[ofs]);
  401. } else {
  402. glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
  403. if (texture->flags & VS::TEXTURE_FLAG_USED_FOR_STREAMING) {
  404. glTexSubImage2D(blit_target, i, 0, 0, w, h, format, type, &read[ofs]);
  405. } else {
  406. glTexImage2D(blit_target, i, internal_format, w, h, 0, format, type, &read[ofs]);
  407. }
  408. }
  409. tsize += size;
  410. w = MAX(1, w >> 1);
  411. h = MAX(1, h >> 1);
  412. }
  413. info.texture_mem -= texture->total_data_size;
  414. texture->total_data_size = tsize;
  415. info.texture_mem += texture->total_data_size;
  416. // printf("texture: %i x %i - size: %i - total: %i\n", texture->width, texture->height, tsize, info.texture_mem);
  417. texture->stored_cube_sides |= (1 << p_layer);
  418. if ((texture->flags & VS::TEXTURE_FLAG_MIPMAPS) && mipmaps == 1 && !texture->ignore_mipmaps && (texture->type != VS::TEXTURE_TYPE_CUBEMAP || texture->stored_cube_sides == (1 << 6) - 1)) {
  419. //generate mipmaps if they were requested and the image does not contain them
  420. glGenerateMipmap(texture->target);
  421. }
  422. texture->mipmaps = mipmaps;
  423. }
  424. void RasterizerStorageGLES2::texture_set_data_partial(RID p_texture, const Ref<Image> &p_image, int src_x, int src_y, int src_w, int src_h, int dst_x, int dst_y, int p_dst_mip, int p_layer) {
  425. // TODO
  426. ERR_PRINT("Not implemented (ask Karroffel to do it :p)");
  427. }
  428. Ref<Image> RasterizerStorageGLES2::texture_get_data(RID p_texture, int p_layer) const {
  429. Texture *texture = texture_owner.getornull(p_texture);
  430. ERR_FAIL_COND_V(!texture, Ref<Image>());
  431. ERR_FAIL_COND_V(!texture->active, Ref<Image>());
  432. ERR_FAIL_COND_V(texture->data_size == 0 && !texture->render_target, Ref<Image>());
  433. if (texture->type == VS::TEXTURE_TYPE_CUBEMAP && p_layer < 6 && p_layer >= 0 && !texture->images[p_layer].is_null()) {
  434. return texture->images[p_layer];
  435. }
  436. #ifdef GLES_OVER_GL
  437. Image::Format real_format;
  438. GLenum gl_format;
  439. GLenum gl_internal_format;
  440. GLenum gl_type;
  441. bool compressed;
  442. _get_gl_image_and_format(Ref<Image>(), texture->format, texture->flags, real_format, gl_format, gl_internal_format, gl_type, compressed);
  443. PoolVector<uint8_t> data;
  444. int data_size = Image::get_image_data_size(texture->alloc_width, texture->alloc_height, real_format, texture->mipmaps > 1);
  445. data.resize(data_size * 2); //add some memory at the end, just in case for buggy drivers
  446. PoolVector<uint8_t>::Write wb = data.write();
  447. glActiveTexture(GL_TEXTURE0);
  448. glBindTexture(texture->target, texture->tex_id);
  449. glBindBuffer(GL_PIXEL_PACK_BUFFER, 0);
  450. for (int i = 0; i < texture->mipmaps; i++) {
  451. int ofs = 0;
  452. if (i > 0) {
  453. ofs = Image::get_image_data_size(texture->alloc_width, texture->alloc_height, real_format, i - 1);
  454. }
  455. if (texture->compressed) {
  456. glPixelStorei(GL_PACK_ALIGNMENT, 4);
  457. glGetCompressedTexImage(texture->target, i, &wb[ofs]);
  458. } else {
  459. glPixelStorei(GL_PACK_ALIGNMENT, 1);
  460. glGetTexImage(texture->target, i, texture->gl_format_cache, texture->gl_type_cache, &wb[ofs]);
  461. }
  462. }
  463. wb = PoolVector<uint8_t>::Write();
  464. data.resize(data_size);
  465. Image *img = memnew(Image(texture->alloc_width, texture->alloc_height, texture->mipmaps > 1 ? true : false, real_format, data));
  466. return Ref<Image>(img);
  467. #else
  468. Image::Format real_format;
  469. GLenum gl_format;
  470. GLenum gl_internal_format;
  471. GLenum gl_type;
  472. bool compressed;
  473. _get_gl_image_and_format(Ref<Image>(), texture->format, texture->flags, real_format, gl_format, gl_internal_format, gl_type, compressed);
  474. PoolVector<uint8_t> data;
  475. int data_size = Image::get_image_data_size(texture->alloc_width, texture->alloc_height, Image::FORMAT_RGBA8, false);
  476. data.resize(data_size * 2); //add some memory at the end, just in case for buggy drivers
  477. PoolVector<uint8_t>::Write wb = data.write();
  478. GLuint temp_framebuffer;
  479. glGenFramebuffers(1, &temp_framebuffer);
  480. GLuint temp_color_texture;
  481. glGenTextures(1, &temp_color_texture);
  482. glBindFramebuffer(GL_FRAMEBUFFER, temp_framebuffer);
  483. glBindTexture(GL_TEXTURE_2D, temp_color_texture);
  484. glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, texture->alloc_width, texture->alloc_height, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
  485. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  486. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  487. glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, temp_color_texture, 0);
  488. glDepthMask(GL_FALSE);
  489. glDisable(GL_DEPTH_TEST);
  490. glDisable(GL_CULL_FACE);
  491. glDisable(GL_BLEND);
  492. glDepthFunc(GL_LEQUAL);
  493. glColorMask(1, 1, 1, 1);
  494. glActiveTexture(GL_TEXTURE0);
  495. glBindTexture(GL_TEXTURE_2D, texture->tex_id);
  496. glViewport(0, 0, texture->alloc_width, texture->alloc_height);
  497. shaders.copy.bind();
  498. glClearColor(0.0, 0.0, 0.0, 0.0);
  499. glClear(GL_COLOR_BUFFER_BIT);
  500. bind_quad_array();
  501. glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
  502. glBindBuffer(GL_ARRAY_BUFFER, 0);
  503. glReadPixels(0, 0, texture->alloc_width, texture->alloc_height, GL_RGBA, GL_UNSIGNED_BYTE, &wb[0]);
  504. glDeleteTextures(1, &temp_color_texture);
  505. glBindFramebuffer(GL_FRAMEBUFFER, 0);
  506. glDeleteFramebuffers(1, &temp_framebuffer);
  507. wb = PoolVector<uint8_t>::Write();
  508. data.resize(data_size);
  509. Image *img = memnew(Image(texture->alloc_width, texture->alloc_height, false, Image::FORMAT_RGBA8, data));
  510. if (!texture->compressed) {
  511. img->convert(real_format);
  512. }
  513. return Ref<Image>(img);
  514. #endif
  515. }
  516. void RasterizerStorageGLES2::texture_set_flags(RID p_texture, uint32_t p_flags) {
  517. Texture *texture = texture_owner.getornull(p_texture);
  518. ERR_FAIL_COND(!texture);
  519. bool had_mipmaps = texture->flags & VS::TEXTURE_FLAG_MIPMAPS;
  520. texture->flags = p_flags;
  521. glActiveTexture(GL_TEXTURE0);
  522. glBindTexture(texture->target, texture->tex_id);
  523. if (((texture->flags & VS::TEXTURE_FLAG_REPEAT) || (texture->flags & VS::TEXTURE_FLAG_MIRRORED_REPEAT)) && texture->target != GL_TEXTURE_CUBE_MAP) {
  524. if (texture->flags & VS::TEXTURE_FLAG_MIRRORED_REPEAT) {
  525. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_MIRRORED_REPEAT);
  526. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_MIRRORED_REPEAT);
  527. } else {
  528. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
  529. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
  530. }
  531. } else {
  532. //glTexParameterf( texture->target, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE );
  533. glTexParameterf(texture->target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  534. glTexParameterf(texture->target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  535. }
  536. if ((texture->flags & VS::TEXTURE_FLAG_MIPMAPS) && !texture->ignore_mipmaps) {
  537. if (!had_mipmaps && texture->mipmaps == 1) {
  538. glGenerateMipmap(texture->target);
  539. }
  540. glTexParameteri(texture->target, GL_TEXTURE_MIN_FILTER, config.use_fast_texture_filter ? GL_LINEAR_MIPMAP_NEAREST : GL_LINEAR_MIPMAP_LINEAR);
  541. } else {
  542. if (texture->flags & VS::TEXTURE_FLAG_FILTER) {
  543. glTexParameteri(texture->target, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  544. } else {
  545. glTexParameteri(texture->target, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  546. }
  547. }
  548. if (texture->flags & VS::TEXTURE_FLAG_FILTER) {
  549. glTexParameteri(texture->target, GL_TEXTURE_MAG_FILTER, GL_LINEAR); // Linear Filtering
  550. } else {
  551. glTexParameteri(texture->target, GL_TEXTURE_MAG_FILTER, GL_NEAREST); // raw Filtering
  552. }
  553. }
  554. uint32_t RasterizerStorageGLES2::texture_get_flags(RID p_texture) const {
  555. Texture *texture = texture_owner.getornull(p_texture);
  556. ERR_FAIL_COND_V(!texture, 0);
  557. return texture->flags;
  558. }
  559. Image::Format RasterizerStorageGLES2::texture_get_format(RID p_texture) const {
  560. Texture *texture = texture_owner.getornull(p_texture);
  561. ERR_FAIL_COND_V(!texture, Image::FORMAT_L8);
  562. return texture->format;
  563. }
  564. VisualServer::TextureType RasterizerStorageGLES2::texture_get_type(RID p_texture) const {
  565. Texture *texture = texture_owner.getornull(p_texture);
  566. ERR_FAIL_COND_V(!texture, VS::TEXTURE_TYPE_2D);
  567. return texture->type;
  568. }
  569. uint32_t RasterizerStorageGLES2::texture_get_texid(RID p_texture) const {
  570. Texture *texture = texture_owner.getornull(p_texture);
  571. ERR_FAIL_COND_V(!texture, 0);
  572. return texture->tex_id;
  573. }
  574. uint32_t RasterizerStorageGLES2::texture_get_width(RID p_texture) const {
  575. Texture *texture = texture_owner.getornull(p_texture);
  576. ERR_FAIL_COND_V(!texture, 0);
  577. return texture->width;
  578. }
  579. uint32_t RasterizerStorageGLES2::texture_get_height(RID p_texture) const {
  580. Texture *texture = texture_owner.getornull(p_texture);
  581. ERR_FAIL_COND_V(!texture, 0);
  582. return texture->height;
  583. }
  584. uint32_t RasterizerStorageGLES2::texture_get_depth(RID p_texture) const {
  585. Texture *texture = texture_owner.getornull(p_texture);
  586. ERR_FAIL_COND_V(!texture, 0);
  587. return texture->depth;
  588. }
  589. void RasterizerStorageGLES2::texture_set_size_override(RID p_texture, int p_width, int p_height, int p_depth) {
  590. Texture *texture = texture_owner.getornull(p_texture);
  591. ERR_FAIL_COND(!texture);
  592. ERR_FAIL_COND(texture->render_target);
  593. ERR_FAIL_COND(p_width <= 0 || p_width > 16384);
  594. ERR_FAIL_COND(p_height <= 0 || p_height > 16384);
  595. //real texture size is in alloc width and height
  596. texture->width = p_width;
  597. texture->height = p_height;
  598. }
  599. void RasterizerStorageGLES2::texture_set_path(RID p_texture, const String &p_path) {
  600. Texture *texture = texture_owner.getornull(p_texture);
  601. ERR_FAIL_COND(!texture);
  602. texture->path = p_path;
  603. }
  604. String RasterizerStorageGLES2::texture_get_path(RID p_texture) const {
  605. Texture *texture = texture_owner.getornull(p_texture);
  606. ERR_FAIL_COND_V(!texture, "");
  607. return texture->path;
  608. }
  609. void RasterizerStorageGLES2::texture_debug_usage(List<VS::TextureInfo> *r_info) {
  610. List<RID> textures;
  611. texture_owner.get_owned_list(&textures);
  612. for (List<RID>::Element *E = textures.front(); E; E = E->next()) {
  613. Texture *t = texture_owner.getornull(E->get());
  614. if (!t)
  615. continue;
  616. VS::TextureInfo tinfo;
  617. tinfo.path = t->path;
  618. tinfo.format = t->format;
  619. tinfo.width = t->alloc_width;
  620. tinfo.height = t->alloc_height;
  621. tinfo.depth = 0;
  622. tinfo.bytes = t->total_data_size;
  623. r_info->push_back(tinfo);
  624. }
  625. }
  626. void RasterizerStorageGLES2::texture_set_shrink_all_x2_on_set_data(bool p_enable) {
  627. config.shrink_textures_x2 = p_enable;
  628. }
  629. void RasterizerStorageGLES2::textures_keep_original(bool p_enable) {
  630. config.keep_original_textures = p_enable;
  631. }
  632. void RasterizerStorageGLES2::texture_set_proxy(RID p_texture, RID p_proxy) {
  633. Texture *texture = texture_owner.getornull(p_texture);
  634. ERR_FAIL_COND(!texture);
  635. if (texture->proxy) {
  636. texture->proxy->proxy_owners.erase(texture);
  637. texture->proxy = NULL;
  638. }
  639. if (p_proxy.is_valid()) {
  640. Texture *proxy = texture_owner.get(p_proxy);
  641. ERR_FAIL_COND(!proxy);
  642. ERR_FAIL_COND(proxy == texture);
  643. proxy->proxy_owners.insert(texture);
  644. texture->proxy = proxy;
  645. }
  646. }
  647. void RasterizerStorageGLES2::texture_set_force_redraw_if_visible(RID p_texture, bool p_enable) {
  648. Texture *texture = texture_owner.getornull(p_texture);
  649. ERR_FAIL_COND(!texture);
  650. texture->redraw_if_visible = p_enable;
  651. }
  652. void RasterizerStorageGLES2::texture_set_detect_3d_callback(RID p_texture, VisualServer::TextureDetectCallback p_callback, void *p_userdata) {
  653. Texture *texture = texture_owner.get(p_texture);
  654. ERR_FAIL_COND(!texture);
  655. texture->detect_3d = p_callback;
  656. texture->detect_3d_ud = p_userdata;
  657. }
  658. void RasterizerStorageGLES2::texture_set_detect_srgb_callback(RID p_texture, VisualServer::TextureDetectCallback p_callback, void *p_userdata) {
  659. Texture *texture = texture_owner.get(p_texture);
  660. ERR_FAIL_COND(!texture);
  661. texture->detect_srgb = p_callback;
  662. texture->detect_srgb_ud = p_userdata;
  663. }
  664. void RasterizerStorageGLES2::texture_set_detect_normal_callback(RID p_texture, VisualServer::TextureDetectCallback p_callback, void *p_userdata) {
  665. Texture *texture = texture_owner.get(p_texture);
  666. ERR_FAIL_COND(!texture);
  667. texture->detect_normal = p_callback;
  668. texture->detect_normal_ud = p_userdata;
  669. }
  670. RID RasterizerStorageGLES2::texture_create_radiance_cubemap(RID p_source, int p_resolution) const {
  671. return RID();
  672. }
  673. RID RasterizerStorageGLES2::sky_create() {
  674. Sky *sky = memnew(Sky);
  675. sky->radiance = 0;
  676. return sky_owner.make_rid(sky);
  677. }
  678. void RasterizerStorageGLES2::sky_set_texture(RID p_sky, RID p_panorama, int p_radiance_size) {
  679. Sky *sky = sky_owner.getornull(p_sky);
  680. ERR_FAIL_COND(!sky);
  681. if (sky->panorama.is_valid()) {
  682. sky->panorama = RID();
  683. glDeleteTextures(1, &sky->radiance);
  684. sky->radiance = 0;
  685. }
  686. sky->panorama = p_panorama;
  687. if (!sky->panorama.is_valid()) {
  688. return; // the panorama was cleared
  689. }
  690. Texture *texture = texture_owner.getornull(sky->panorama);
  691. if (!texture) {
  692. sky->panorama = RID();
  693. ERR_FAIL_COND(!texture);
  694. }
  695. // glBindVertexArray(0) and more
  696. {
  697. glBindBuffer(GL_ARRAY_BUFFER, 0);
  698. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
  699. glDisable(GL_CULL_FACE);
  700. glDisable(GL_DEPTH_TEST);
  701. glDisable(GL_SCISSOR_TEST);
  702. glDisable(GL_BLEND);
  703. for (int i = 0; i < VS::ARRAY_MAX - 1; i++) {
  704. glDisableVertexAttribArray(i);
  705. }
  706. }
  707. glActiveTexture(GL_TEXTURE0);
  708. glBindTexture(texture->target, texture->tex_id);
  709. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  710. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  711. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  712. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); //need this for proper sampling
  713. glActiveTexture(GL_TEXTURE1);
  714. glBindTexture(GL_TEXTURE_2D, resources.radical_inverse_vdc_cache_tex);
  715. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  716. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  717. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  718. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  719. // New cubemap that will hold the mipmaps with different roughness values
  720. glActiveTexture(GL_TEXTURE2);
  721. glGenTextures(1, &sky->radiance);
  722. glBindTexture(GL_TEXTURE_CUBE_MAP, sky->radiance);
  723. // Now we create a new framebuffer. The new cubemap images will be used as
  724. // attachements for it, so we can fill them by issuing draw calls.
  725. GLuint tmp_fb;
  726. int size = p_radiance_size / 4; //divide by four because its a cubemap (this is an approximation because GLES3 uses a dual paraboloid)
  727. int lod = 0;
  728. int mipmaps = 6;
  729. int mm_level = mipmaps;
  730. GLenum internal_format = GL_RGB;
  731. GLenum format = GL_RGB;
  732. GLenum type = GL_UNSIGNED_BYTE;
  733. // Set the initial (empty) mipmaps
  734. #if 1
  735. //Mobile hardware (PowerVR specially) prefers this approach, the other one kills the game
  736. for (int i = 0; i < 6; i++) {
  737. glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + i, 0, internal_format, size, size, 0, format, type, NULL);
  738. }
  739. glGenerateMipmap(GL_TEXTURE_CUBE_MAP);
  740. #else
  741. while (size >= 1) {
  742. for (int i = 0; i < 6; i++) {
  743. glTexImage2D(_cube_side_enum[i], lod, internal_format, size, size, 0, format, type, NULL);
  744. }
  745. lod++;
  746. size >>= 1;
  747. }
  748. #endif
  749. //framebuffer
  750. glGenFramebuffers(1, &tmp_fb);
  751. glBindFramebuffer(GL_FRAMEBUFFER, tmp_fb);
  752. shaders.cubemap_filter.set_conditional(CubemapFilterShaderGLES2::USE_SOURCE_PANORAMA, texture->target == GL_TEXTURE_2D);
  753. shaders.cubemap_filter.bind();
  754. lod = 0;
  755. mm_level = mipmaps;
  756. size = p_radiance_size;
  757. // now render to the framebuffer, mipmap level for mipmap level
  758. while (size >= 1) {
  759. for (int i = 0; i < 6; i++) {
  760. glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, _cube_side_enum[i], sky->radiance, lod);
  761. glViewport(0, 0, size, size);
  762. bind_quad_array();
  763. shaders.cubemap_filter.set_uniform(CubemapFilterShaderGLES2::FACE_ID, i);
  764. float roughness = mm_level ? lod / (float)(mipmaps - 1) : 1;
  765. roughness = MIN(1.0, roughness); //keep max at 1
  766. shaders.cubemap_filter.set_uniform(CubemapFilterShaderGLES2::ROUGHNESS, roughness);
  767. glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
  768. }
  769. size >>= 1;
  770. mm_level--;
  771. lod++;
  772. }
  773. // restore ranges
  774. glTexParameterf(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
  775. glTexParameterf(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  776. glTexParameterf(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  777. glTexParameterf(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  778. // Framebuffer did its job. thank mr framebuffer
  779. glBindFramebuffer(GL_FRAMEBUFFER, RasterizerStorageGLES2::system_fbo);
  780. glDeleteFramebuffers(1, &tmp_fb);
  781. }
  782. /* SHADER API */
  783. RID RasterizerStorageGLES2::shader_create() {
  784. Shader *shader = memnew(Shader);
  785. shader->mode = VS::SHADER_SPATIAL;
  786. shader->shader = &scene->state.scene_shader;
  787. RID rid = shader_owner.make_rid(shader);
  788. _shader_make_dirty(shader);
  789. shader->self = rid;
  790. return rid;
  791. }
  792. void RasterizerStorageGLES2::_shader_make_dirty(Shader *p_shader) {
  793. if (p_shader->dirty_list.in_list())
  794. return;
  795. _shader_dirty_list.add(&p_shader->dirty_list);
  796. }
  797. void RasterizerStorageGLES2::shader_set_code(RID p_shader, const String &p_code) {
  798. Shader *shader = shader_owner.getornull(p_shader);
  799. ERR_FAIL_COND(!shader);
  800. shader->code = p_code;
  801. String mode_string = ShaderLanguage::get_shader_type(p_code);
  802. VS::ShaderMode mode;
  803. if (mode_string == "canvas_item")
  804. mode = VS::SHADER_CANVAS_ITEM;
  805. else if (mode_string == "particles")
  806. mode = VS::SHADER_PARTICLES;
  807. else
  808. mode = VS::SHADER_SPATIAL;
  809. if (shader->custom_code_id && mode != shader->mode) {
  810. shader->shader->free_custom_shader(shader->custom_code_id);
  811. shader->custom_code_id = 0;
  812. }
  813. shader->mode = mode;
  814. // TODO handle all shader types
  815. if (mode == VS::SHADER_CANVAS_ITEM) {
  816. shader->shader = &canvas->state.canvas_shader;
  817. } else if (mode == VS::SHADER_SPATIAL) {
  818. shader->shader = &scene->state.scene_shader;
  819. } else {
  820. return;
  821. }
  822. if (shader->custom_code_id == 0) {
  823. shader->custom_code_id = shader->shader->create_custom_shader();
  824. }
  825. _shader_make_dirty(shader);
  826. }
  827. String RasterizerStorageGLES2::shader_get_code(RID p_shader) const {
  828. const Shader *shader = shader_owner.get(p_shader);
  829. ERR_FAIL_COND_V(!shader, "");
  830. return shader->code;
  831. }
  832. void RasterizerStorageGLES2::_update_shader(Shader *p_shader) const {
  833. _shader_dirty_list.remove(&p_shader->dirty_list);
  834. p_shader->valid = false;
  835. p_shader->uniforms.clear();
  836. if (p_shader->code == String()) {
  837. return; //just invalid, but no error
  838. }
  839. ShaderCompilerGLES2::GeneratedCode gen_code;
  840. ShaderCompilerGLES2::IdentifierActions *actions = NULL;
  841. switch (p_shader->mode) {
  842. // TODO
  843. case VS::SHADER_CANVAS_ITEM: {
  844. p_shader->canvas_item.light_mode = Shader::CanvasItem::LIGHT_MODE_NORMAL;
  845. p_shader->canvas_item.blend_mode = Shader::CanvasItem::BLEND_MODE_MIX;
  846. p_shader->canvas_item.uses_screen_texture = false;
  847. p_shader->canvas_item.uses_screen_uv = false;
  848. p_shader->canvas_item.uses_time = false;
  849. shaders.actions_canvas.render_mode_values["blend_add"] = Pair<int *, int>(&p_shader->canvas_item.blend_mode, Shader::CanvasItem::BLEND_MODE_ADD);
  850. shaders.actions_canvas.render_mode_values["blend_mix"] = Pair<int *, int>(&p_shader->canvas_item.blend_mode, Shader::CanvasItem::BLEND_MODE_MIX);
  851. shaders.actions_canvas.render_mode_values["blend_sub"] = Pair<int *, int>(&p_shader->canvas_item.blend_mode, Shader::CanvasItem::BLEND_MODE_SUB);
  852. shaders.actions_canvas.render_mode_values["blend_mul"] = Pair<int *, int>(&p_shader->canvas_item.blend_mode, Shader::CanvasItem::BLEND_MODE_MUL);
  853. shaders.actions_canvas.render_mode_values["blend_premul_alpha"] = Pair<int *, int>(&p_shader->canvas_item.blend_mode, Shader::CanvasItem::BLEND_MODE_PMALPHA);
  854. shaders.actions_canvas.render_mode_values["unshaded"] = Pair<int *, int>(&p_shader->canvas_item.light_mode, Shader::CanvasItem::LIGHT_MODE_UNSHADED);
  855. shaders.actions_canvas.render_mode_values["light_only"] = Pair<int *, int>(&p_shader->canvas_item.light_mode, Shader::CanvasItem::LIGHT_MODE_LIGHT_ONLY);
  856. shaders.actions_canvas.usage_flag_pointers["SCREEN_UV"] = &p_shader->canvas_item.uses_screen_uv;
  857. shaders.actions_canvas.usage_flag_pointers["SCREEN_PIXEL_SIZE"] = &p_shader->canvas_item.uses_screen_uv;
  858. shaders.actions_canvas.usage_flag_pointers["SCREEN_TEXTURE"] = &p_shader->canvas_item.uses_screen_texture;
  859. shaders.actions_canvas.usage_flag_pointers["TIME"] = &p_shader->canvas_item.uses_time;
  860. actions = &shaders.actions_canvas;
  861. actions->uniforms = &p_shader->uniforms;
  862. } break;
  863. case VS::SHADER_SPATIAL: {
  864. p_shader->spatial.blend_mode = Shader::Spatial::BLEND_MODE_MIX;
  865. p_shader->spatial.depth_draw_mode = Shader::Spatial::DEPTH_DRAW_OPAQUE;
  866. p_shader->spatial.cull_mode = Shader::Spatial::CULL_MODE_BACK;
  867. p_shader->spatial.uses_alpha = false;
  868. p_shader->spatial.uses_alpha_scissor = false;
  869. p_shader->spatial.uses_discard = false;
  870. p_shader->spatial.unshaded = false;
  871. p_shader->spatial.no_depth_test = false;
  872. p_shader->spatial.uses_sss = false;
  873. p_shader->spatial.uses_time = false;
  874. p_shader->spatial.uses_vertex_lighting = false;
  875. p_shader->spatial.uses_screen_texture = false;
  876. p_shader->spatial.uses_depth_texture = false;
  877. p_shader->spatial.uses_vertex = false;
  878. p_shader->spatial.writes_modelview_or_projection = false;
  879. p_shader->spatial.uses_world_coordinates = false;
  880. shaders.actions_scene.render_mode_values["blend_add"] = Pair<int *, int>(&p_shader->spatial.blend_mode, Shader::Spatial::BLEND_MODE_ADD);
  881. shaders.actions_scene.render_mode_values["blend_mix"] = Pair<int *, int>(&p_shader->spatial.blend_mode, Shader::Spatial::BLEND_MODE_MIX);
  882. shaders.actions_scene.render_mode_values["blend_sub"] = Pair<int *, int>(&p_shader->spatial.blend_mode, Shader::Spatial::BLEND_MODE_SUB);
  883. shaders.actions_scene.render_mode_values["blend_mul"] = Pair<int *, int>(&p_shader->spatial.blend_mode, Shader::Spatial::BLEND_MODE_MUL);
  884. shaders.actions_scene.render_mode_values["depth_draw_opaque"] = Pair<int *, int>(&p_shader->spatial.depth_draw_mode, Shader::Spatial::DEPTH_DRAW_OPAQUE);
  885. shaders.actions_scene.render_mode_values["depth_draw_always"] = Pair<int *, int>(&p_shader->spatial.depth_draw_mode, Shader::Spatial::DEPTH_DRAW_ALWAYS);
  886. shaders.actions_scene.render_mode_values["depth_draw_never"] = Pair<int *, int>(&p_shader->spatial.depth_draw_mode, Shader::Spatial::DEPTH_DRAW_NEVER);
  887. shaders.actions_scene.render_mode_values["depth_draw_alpha_prepass"] = Pair<int *, int>(&p_shader->spatial.depth_draw_mode, Shader::Spatial::DEPTH_DRAW_ALPHA_PREPASS);
  888. shaders.actions_scene.render_mode_values["cull_front"] = Pair<int *, int>(&p_shader->spatial.cull_mode, Shader::Spatial::CULL_MODE_FRONT);
  889. shaders.actions_scene.render_mode_values["cull_back"] = Pair<int *, int>(&p_shader->spatial.cull_mode, Shader::Spatial::CULL_MODE_BACK);
  890. shaders.actions_scene.render_mode_values["cull_disabled"] = Pair<int *, int>(&p_shader->spatial.cull_mode, Shader::Spatial::CULL_MODE_DISABLED);
  891. shaders.actions_scene.render_mode_flags["unshaded"] = &p_shader->spatial.unshaded;
  892. shaders.actions_scene.render_mode_flags["depth_test_disable"] = &p_shader->spatial.no_depth_test;
  893. shaders.actions_scene.render_mode_flags["vertex_lighting"] = &p_shader->spatial.uses_vertex_lighting;
  894. shaders.actions_scene.render_mode_flags["world_vertex_coords"] = &p_shader->spatial.uses_world_coordinates;
  895. shaders.actions_scene.usage_flag_pointers["ALPHA"] = &p_shader->spatial.uses_alpha;
  896. shaders.actions_scene.usage_flag_pointers["ALPHA_SCISSOR"] = &p_shader->spatial.uses_alpha_scissor;
  897. shaders.actions_scene.usage_flag_pointers["SSS_STRENGTH"] = &p_shader->spatial.uses_sss;
  898. shaders.actions_scene.usage_flag_pointers["DISCARD"] = &p_shader->spatial.uses_discard;
  899. shaders.actions_scene.usage_flag_pointers["SCREEN_TEXTURE"] = &p_shader->spatial.uses_screen_texture;
  900. shaders.actions_scene.usage_flag_pointers["DEPTH_TEXTURE"] = &p_shader->spatial.uses_depth_texture;
  901. shaders.actions_scene.usage_flag_pointers["TIME"] = &p_shader->spatial.uses_time;
  902. shaders.actions_scene.write_flag_pointers["MODELVIEW_MATRIX"] = &p_shader->spatial.writes_modelview_or_projection;
  903. shaders.actions_scene.write_flag_pointers["PROJECTION_MATRIX"] = &p_shader->spatial.writes_modelview_or_projection;
  904. shaders.actions_scene.write_flag_pointers["VERTEX"] = &p_shader->spatial.uses_vertex;
  905. actions = &shaders.actions_scene;
  906. actions->uniforms = &p_shader->uniforms;
  907. } break;
  908. default: {
  909. return;
  910. } break;
  911. }
  912. Error err = shaders.compiler.compile(p_shader->mode, p_shader->code, actions, p_shader->path, gen_code);
  913. ERR_FAIL_COND(err != OK);
  914. p_shader->shader->set_custom_shader_code(p_shader->custom_code_id, gen_code.vertex, gen_code.vertex_global, gen_code.fragment, gen_code.light, gen_code.fragment_global, gen_code.uniforms, gen_code.texture_uniforms, gen_code.custom_defines);
  915. p_shader->texture_count = gen_code.texture_uniforms.size();
  916. p_shader->texture_hints = gen_code.texture_hints;
  917. p_shader->uses_vertex_time = gen_code.uses_vertex_time;
  918. p_shader->uses_fragment_time = gen_code.uses_fragment_time;
  919. p_shader->shader->set_custom_shader(p_shader->custom_code_id);
  920. p_shader->shader->bind();
  921. // cache uniform locations
  922. for (SelfList<Material> *E = p_shader->materials.first(); E; E = E->next()) {
  923. _material_make_dirty(E->self());
  924. }
  925. p_shader->valid = true;
  926. p_shader->version++;
  927. }
  928. void RasterizerStorageGLES2::update_dirty_shaders() {
  929. while (_shader_dirty_list.first()) {
  930. _update_shader(_shader_dirty_list.first()->self());
  931. }
  932. }
  933. void RasterizerStorageGLES2::shader_get_param_list(RID p_shader, List<PropertyInfo> *p_param_list) const {
  934. Shader *shader = shader_owner.get(p_shader);
  935. ERR_FAIL_COND(!shader);
  936. if (shader->dirty_list.in_list()) {
  937. _update_shader(shader);
  938. }
  939. Map<int, StringName> order;
  940. for (Map<StringName, ShaderLanguage::ShaderNode::Uniform>::Element *E = shader->uniforms.front(); E; E = E->next()) {
  941. if (E->get().texture_order >= 0) {
  942. order[E->get().texture_order + 100000] = E->key();
  943. } else {
  944. order[E->get().order] = E->key();
  945. }
  946. }
  947. for (Map<int, StringName>::Element *E = order.front(); E; E = E->next()) {
  948. PropertyInfo pi;
  949. ShaderLanguage::ShaderNode::Uniform &u = shader->uniforms[E->get()];
  950. pi.name = E->get();
  951. switch (u.type) {
  952. case ShaderLanguage::TYPE_VOID: {
  953. pi.type = Variant::NIL;
  954. } break;
  955. case ShaderLanguage::TYPE_BOOL: {
  956. pi.type = Variant::BOOL;
  957. } break;
  958. // bool vectors
  959. case ShaderLanguage::TYPE_BVEC2: {
  960. pi.type = Variant::INT;
  961. pi.hint = PROPERTY_HINT_FLAGS;
  962. pi.hint_string = "x,y";
  963. } break;
  964. case ShaderLanguage::TYPE_BVEC3: {
  965. pi.type = Variant::INT;
  966. pi.hint = PROPERTY_HINT_FLAGS;
  967. pi.hint_string = "x,y,z";
  968. } break;
  969. case ShaderLanguage::TYPE_BVEC4: {
  970. pi.type = Variant::INT;
  971. pi.hint = PROPERTY_HINT_FLAGS;
  972. pi.hint_string = "x,y,z,w";
  973. } break;
  974. // int stuff
  975. case ShaderLanguage::TYPE_UINT:
  976. case ShaderLanguage::TYPE_INT: {
  977. pi.type = Variant::INT;
  978. if (u.hint == ShaderLanguage::ShaderNode::Uniform::HINT_RANGE) {
  979. pi.hint = PROPERTY_HINT_RANGE;
  980. pi.hint_string = rtos(u.hint_range[0]) + "," + rtos(u.hint_range[1]);
  981. }
  982. } break;
  983. case ShaderLanguage::TYPE_IVEC2:
  984. case ShaderLanguage::TYPE_UVEC2:
  985. case ShaderLanguage::TYPE_IVEC3:
  986. case ShaderLanguage::TYPE_UVEC3:
  987. case ShaderLanguage::TYPE_IVEC4:
  988. case ShaderLanguage::TYPE_UVEC4: {
  989. pi.type = Variant::POOL_INT_ARRAY;
  990. } break;
  991. case ShaderLanguage::TYPE_FLOAT: {
  992. pi.type = Variant::REAL;
  993. } break;
  994. case ShaderLanguage::TYPE_VEC2: {
  995. pi.type = Variant::VECTOR2;
  996. } break;
  997. case ShaderLanguage::TYPE_VEC3: {
  998. pi.type = Variant::VECTOR3;
  999. } break;
  1000. case ShaderLanguage::TYPE_VEC4: {
  1001. if (u.hint == ShaderLanguage::ShaderNode::Uniform::HINT_COLOR) {
  1002. pi.type = Variant::COLOR;
  1003. } else {
  1004. pi.type = Variant::PLANE;
  1005. }
  1006. } break;
  1007. case ShaderLanguage::TYPE_MAT2: {
  1008. pi.type = Variant::TRANSFORM2D;
  1009. } break;
  1010. case ShaderLanguage::TYPE_MAT3: {
  1011. pi.type = Variant::BASIS;
  1012. } break;
  1013. case ShaderLanguage::TYPE_MAT4: {
  1014. pi.type = Variant::TRANSFORM;
  1015. } break;
  1016. case ShaderLanguage::TYPE_SAMPLER2D:
  1017. case ShaderLanguage::TYPE_ISAMPLER2D:
  1018. case ShaderLanguage::TYPE_USAMPLER2D: {
  1019. pi.type = Variant::OBJECT;
  1020. pi.hint = PROPERTY_HINT_RESOURCE_TYPE;
  1021. pi.hint_string = "Texture";
  1022. } break;
  1023. case ShaderLanguage::TYPE_SAMPLERCUBE: {
  1024. pi.type = Variant::OBJECT;
  1025. pi.hint = PROPERTY_HINT_RESOURCE_TYPE;
  1026. pi.hint_string = "CubeMap";
  1027. } break;
  1028. case ShaderLanguage::TYPE_SAMPLER2DARRAY:
  1029. case ShaderLanguage::TYPE_ISAMPLER2DARRAY:
  1030. case ShaderLanguage::TYPE_USAMPLER2DARRAY:
  1031. case ShaderLanguage::TYPE_SAMPLER3D:
  1032. case ShaderLanguage::TYPE_ISAMPLER3D:
  1033. case ShaderLanguage::TYPE_USAMPLER3D: {
  1034. // Not implemented in GLES2
  1035. } break;
  1036. }
  1037. p_param_list->push_back(pi);
  1038. }
  1039. }
  1040. void RasterizerStorageGLES2::shader_set_default_texture_param(RID p_shader, const StringName &p_name, RID p_texture) {
  1041. Shader *shader = shader_owner.get(p_shader);
  1042. ERR_FAIL_COND(!shader);
  1043. ERR_FAIL_COND(p_texture.is_valid() && !texture_owner.owns(p_texture));
  1044. if (p_texture.is_valid()) {
  1045. shader->default_textures[p_name] = p_texture;
  1046. } else {
  1047. shader->default_textures.erase(p_name);
  1048. }
  1049. _shader_make_dirty(shader);
  1050. }
  1051. RID RasterizerStorageGLES2::shader_get_default_texture_param(RID p_shader, const StringName &p_name) const {
  1052. const Shader *shader = shader_owner.get(p_shader);
  1053. ERR_FAIL_COND_V(!shader, RID());
  1054. const Map<StringName, RID>::Element *E = shader->default_textures.find(p_name);
  1055. if (!E) {
  1056. return RID();
  1057. }
  1058. return E->get();
  1059. }
  1060. /* COMMON MATERIAL API */
  1061. void RasterizerStorageGLES2::_material_make_dirty(Material *p_material) const {
  1062. if (p_material->dirty_list.in_list())
  1063. return;
  1064. _material_dirty_list.add(&p_material->dirty_list);
  1065. }
  1066. RID RasterizerStorageGLES2::material_create() {
  1067. Material *material = memnew(Material);
  1068. return material_owner.make_rid(material);
  1069. }
  1070. void RasterizerStorageGLES2::material_set_shader(RID p_material, RID p_shader) {
  1071. Material *material = material_owner.get(p_material);
  1072. ERR_FAIL_COND(!material);
  1073. Shader *shader = shader_owner.getornull(p_shader);
  1074. if (material->shader) {
  1075. // if a shader is present, remove the old shader
  1076. material->shader->materials.remove(&material->list);
  1077. }
  1078. material->shader = shader;
  1079. if (shader) {
  1080. shader->materials.add(&material->list);
  1081. }
  1082. _material_make_dirty(material);
  1083. }
  1084. RID RasterizerStorageGLES2::material_get_shader(RID p_material) const {
  1085. const Material *material = material_owner.get(p_material);
  1086. ERR_FAIL_COND_V(!material, RID());
  1087. if (material->shader) {
  1088. return material->shader->self;
  1089. }
  1090. return RID();
  1091. }
  1092. void RasterizerStorageGLES2::material_set_param(RID p_material, const StringName &p_param, const Variant &p_value) {
  1093. Material *material = material_owner.get(p_material);
  1094. ERR_FAIL_COND(!material);
  1095. if (p_value.get_type() == Variant::NIL) {
  1096. material->params.erase(p_param);
  1097. } else {
  1098. material->params[p_param] = p_value;
  1099. }
  1100. _material_make_dirty(material);
  1101. }
  1102. Variant RasterizerStorageGLES2::material_get_param(RID p_material, const StringName &p_param) const {
  1103. const Material *material = material_owner.get(p_material);
  1104. ERR_FAIL_COND_V(!material, RID());
  1105. if (material->params.has(p_param)) {
  1106. return material->params[p_param];
  1107. }
  1108. return material_get_param_default(p_material, p_param);
  1109. }
  1110. Variant RasterizerStorageGLES2::material_get_param_default(RID p_material, const StringName &p_param) const {
  1111. const Material *material = material_owner.get(p_material);
  1112. ERR_FAIL_COND_V(!material, Variant());
  1113. if (material->shader) {
  1114. if (material->shader->uniforms.has(p_param)) {
  1115. ShaderLanguage::ShaderNode::Uniform uniform = material->shader->uniforms[p_param];
  1116. Vector<ShaderLanguage::ConstantNode::Value> default_value = uniform.default_value;
  1117. return ShaderLanguage::constant_value_to_variant(default_value, uniform.type, uniform.hint);
  1118. }
  1119. }
  1120. return Variant();
  1121. }
  1122. void RasterizerStorageGLES2::material_set_line_width(RID p_material, float p_width) {
  1123. Material *material = material_owner.getornull(p_material);
  1124. ERR_FAIL_COND(!material);
  1125. material->line_width = p_width;
  1126. }
  1127. void RasterizerStorageGLES2::material_set_next_pass(RID p_material, RID p_next_material) {
  1128. Material *material = material_owner.get(p_material);
  1129. ERR_FAIL_COND(!material);
  1130. material->next_pass = p_next_material;
  1131. }
  1132. bool RasterizerStorageGLES2::material_is_animated(RID p_material) {
  1133. Material *material = material_owner.get(p_material);
  1134. ERR_FAIL_COND_V(!material, false);
  1135. if (material->dirty_list.in_list()) {
  1136. _update_material(material);
  1137. }
  1138. bool animated = material->is_animated_cache;
  1139. if (!animated && material->next_pass.is_valid()) {
  1140. animated = material_is_animated(material->next_pass);
  1141. }
  1142. return animated;
  1143. }
  1144. bool RasterizerStorageGLES2::material_casts_shadows(RID p_material) {
  1145. Material *material = material_owner.get(p_material);
  1146. ERR_FAIL_COND_V(!material, false);
  1147. if (material->dirty_list.in_list()) {
  1148. _update_material(material);
  1149. }
  1150. bool casts_shadows = material->can_cast_shadow_cache;
  1151. if (!casts_shadows && material->next_pass.is_valid()) {
  1152. casts_shadows = material_casts_shadows(material->next_pass);
  1153. }
  1154. return casts_shadows;
  1155. }
  1156. void RasterizerStorageGLES2::material_add_instance_owner(RID p_material, RasterizerScene::InstanceBase *p_instance) {
  1157. Material *material = material_owner.getornull(p_material);
  1158. ERR_FAIL_COND(!material);
  1159. Map<RasterizerScene::InstanceBase *, int>::Element *E = material->instance_owners.find(p_instance);
  1160. if (E) {
  1161. E->get()++;
  1162. } else {
  1163. material->instance_owners[p_instance] = 1;
  1164. }
  1165. }
  1166. void RasterizerStorageGLES2::material_remove_instance_owner(RID p_material, RasterizerScene::InstanceBase *p_instance) {
  1167. Material *material = material_owner.getornull(p_material);
  1168. ERR_FAIL_COND(!material);
  1169. Map<RasterizerScene::InstanceBase *, int>::Element *E = material->instance_owners.find(p_instance);
  1170. ERR_FAIL_COND(!E);
  1171. E->get()--;
  1172. if (E->get() == 0) {
  1173. material->instance_owners.erase(E);
  1174. }
  1175. }
  1176. void RasterizerStorageGLES2::material_set_render_priority(RID p_material, int priority) {
  1177. ERR_FAIL_COND(priority < VS::MATERIAL_RENDER_PRIORITY_MIN);
  1178. ERR_FAIL_COND(priority > VS::MATERIAL_RENDER_PRIORITY_MAX);
  1179. Material *material = material_owner.get(p_material);
  1180. ERR_FAIL_COND(!material);
  1181. material->render_priority = priority;
  1182. }
  1183. void RasterizerStorageGLES2::_update_material(Material *p_material) {
  1184. if (p_material->dirty_list.in_list()) {
  1185. _material_dirty_list.remove(&p_material->dirty_list);
  1186. }
  1187. if (p_material->shader && p_material->shader->dirty_list.in_list()) {
  1188. _update_shader(p_material->shader);
  1189. }
  1190. if (p_material->shader && !p_material->shader->valid) {
  1191. return;
  1192. }
  1193. {
  1194. bool can_cast_shadow = false;
  1195. bool is_animated = false;
  1196. if (p_material->shader && p_material->shader->mode == VS::SHADER_SPATIAL) {
  1197. if (p_material->shader->spatial.blend_mode == Shader::Spatial::BLEND_MODE_MIX &&
  1198. (!p_material->shader->spatial.uses_alpha || p_material->shader->spatial.depth_draw_mode == Shader::Spatial::DEPTH_DRAW_ALPHA_PREPASS)) {
  1199. can_cast_shadow = true;
  1200. }
  1201. if (p_material->shader->spatial.uses_discard && p_material->shader->uses_fragment_time) {
  1202. is_animated = true;
  1203. }
  1204. if (p_material->shader->spatial.uses_vertex && p_material->shader->uses_vertex_time) {
  1205. is_animated = true;
  1206. }
  1207. if (can_cast_shadow != p_material->can_cast_shadow_cache || is_animated != p_material->is_animated_cache) {
  1208. p_material->can_cast_shadow_cache = can_cast_shadow;
  1209. p_material->is_animated_cache = is_animated;
  1210. for (Map<Geometry *, int>::Element *E = p_material->geometry_owners.front(); E; E = E->next()) {
  1211. E->key()->material_changed_notify();
  1212. }
  1213. for (Map<RasterizerScene::InstanceBase *, int>::Element *E = p_material->instance_owners.front(); E; E = E->next()) {
  1214. E->key()->base_changed(false, true);
  1215. }
  1216. }
  1217. }
  1218. }
  1219. // uniforms and other things will be set in the use_material method in ShaderGLES2
  1220. if (p_material->shader && p_material->shader->texture_count > 0) {
  1221. p_material->textures.resize(p_material->shader->texture_count);
  1222. for (Map<StringName, ShaderLanguage::ShaderNode::Uniform>::Element *E = p_material->shader->uniforms.front(); E; E = E->next()) {
  1223. if (E->get().texture_order < 0)
  1224. continue; // not a texture, does not go here
  1225. RID texture;
  1226. Map<StringName, Variant>::Element *V = p_material->params.find(E->key());
  1227. if (V) {
  1228. texture = V->get();
  1229. }
  1230. if (!texture.is_valid()) {
  1231. Map<StringName, RID>::Element *W = p_material->shader->default_textures.find(E->key());
  1232. if (W) {
  1233. texture = W->get();
  1234. }
  1235. }
  1236. p_material->textures.write[E->get().texture_order] = Pair<StringName, RID>(E->key(), texture);
  1237. }
  1238. } else {
  1239. p_material->textures.clear();
  1240. }
  1241. }
  1242. void RasterizerStorageGLES2::_material_add_geometry(RID p_material, Geometry *p_geometry) {
  1243. Material *material = material_owner.getornull(p_material);
  1244. ERR_FAIL_COND(!material);
  1245. Map<Geometry *, int>::Element *I = material->geometry_owners.find(p_geometry);
  1246. if (I) {
  1247. I->get()++;
  1248. } else {
  1249. material->geometry_owners[p_geometry] = 1;
  1250. }
  1251. }
  1252. void RasterizerStorageGLES2::_material_remove_geometry(RID p_material, Geometry *p_geometry) {
  1253. Material *material = material_owner.getornull(p_material);
  1254. ERR_FAIL_COND(!material);
  1255. Map<Geometry *, int>::Element *I = material->geometry_owners.find(p_geometry);
  1256. ERR_FAIL_COND(!I);
  1257. I->get()--;
  1258. if (I->get() == 0) {
  1259. material->geometry_owners.erase(I);
  1260. }
  1261. }
  1262. void RasterizerStorageGLES2::update_dirty_materials() {
  1263. while (_material_dirty_list.first()) {
  1264. Material *material = _material_dirty_list.first()->self();
  1265. _update_material(material);
  1266. }
  1267. }
  1268. /* MESH API */
  1269. RID RasterizerStorageGLES2::mesh_create() {
  1270. Mesh *mesh = memnew(Mesh);
  1271. return mesh_owner.make_rid(mesh);
  1272. }
  1273. void RasterizerStorageGLES2::mesh_add_surface(RID p_mesh, uint32_t p_format, VS::PrimitiveType p_primitive, const PoolVector<uint8_t> &p_array, int p_vertex_count, const PoolVector<uint8_t> &p_index_array, int p_index_count, const AABB &p_aabb, const Vector<PoolVector<uint8_t> > &p_blend_shapes, const Vector<AABB> &p_bone_aabbs) {
  1274. PoolVector<uint8_t> array = p_array;
  1275. Mesh *mesh = mesh_owner.getornull(p_mesh);
  1276. ERR_FAIL_COND(!mesh);
  1277. ERR_FAIL_COND(!(p_format & VS::ARRAY_FORMAT_VERTEX));
  1278. //must have index and bones, both.
  1279. {
  1280. uint32_t bones_weight = VS::ARRAY_FORMAT_BONES | VS::ARRAY_FORMAT_WEIGHTS;
  1281. ERR_EXPLAIN("Array must have both bones and weights in format or none.");
  1282. ERR_FAIL_COND((p_format & bones_weight) && (p_format & bones_weight) != bones_weight);
  1283. }
  1284. //bool has_morph = p_blend_shapes.size();
  1285. Surface::Attrib attribs[VS::ARRAY_MAX];
  1286. int stride = 0;
  1287. for (int i = 0; i < VS::ARRAY_MAX; i++) {
  1288. attribs[i].index = i;
  1289. if (!(p_format & (1 << i))) {
  1290. attribs[i].enabled = false;
  1291. attribs[i].integer = false;
  1292. continue;
  1293. }
  1294. attribs[i].enabled = true;
  1295. attribs[i].offset = stride;
  1296. attribs[i].integer = false;
  1297. switch (i) {
  1298. case VS::ARRAY_VERTEX: {
  1299. if (p_format & VS::ARRAY_FLAG_USE_2D_VERTICES) {
  1300. attribs[i].size = 2;
  1301. } else {
  1302. attribs[i].size = (p_format & VS::ARRAY_COMPRESS_VERTEX) ? 4 : 3;
  1303. }
  1304. if (p_format & VS::ARRAY_COMPRESS_VERTEX) {
  1305. attribs[i].type = _GL_HALF_FLOAT_OES;
  1306. stride += attribs[i].size * 2;
  1307. } else {
  1308. attribs[i].type = GL_FLOAT;
  1309. stride += attribs[i].size * 4;
  1310. }
  1311. attribs[i].normalized = GL_FALSE;
  1312. } break;
  1313. case VS::ARRAY_NORMAL: {
  1314. attribs[i].size = 3;
  1315. if (p_format & VS::ARRAY_COMPRESS_NORMAL) {
  1316. attribs[i].type = GL_BYTE;
  1317. stride += 4; //pad extra byte
  1318. attribs[i].normalized = GL_TRUE;
  1319. } else {
  1320. attribs[i].type = GL_FLOAT;
  1321. stride += 12;
  1322. attribs[i].normalized = GL_FALSE;
  1323. }
  1324. } break;
  1325. case VS::ARRAY_TANGENT: {
  1326. attribs[i].size = 4;
  1327. if (p_format & VS::ARRAY_COMPRESS_TANGENT) {
  1328. attribs[i].type = GL_BYTE;
  1329. stride += 4;
  1330. attribs[i].normalized = GL_TRUE;
  1331. } else {
  1332. attribs[i].type = GL_FLOAT;
  1333. stride += 16;
  1334. attribs[i].normalized = GL_FALSE;
  1335. }
  1336. } break;
  1337. case VS::ARRAY_COLOR: {
  1338. attribs[i].size = 4;
  1339. if (p_format & VS::ARRAY_COMPRESS_COLOR) {
  1340. attribs[i].type = GL_UNSIGNED_BYTE;
  1341. stride += 4;
  1342. attribs[i].normalized = GL_TRUE;
  1343. } else {
  1344. attribs[i].type = GL_FLOAT;
  1345. stride += 16;
  1346. attribs[i].normalized = GL_FALSE;
  1347. }
  1348. } break;
  1349. case VS::ARRAY_TEX_UV: {
  1350. attribs[i].size = 2;
  1351. if (p_format & VS::ARRAY_COMPRESS_TEX_UV) {
  1352. attribs[i].type = _GL_HALF_FLOAT_OES;
  1353. stride += 4;
  1354. } else {
  1355. attribs[i].type = GL_FLOAT;
  1356. stride += 8;
  1357. }
  1358. attribs[i].normalized = GL_FALSE;
  1359. } break;
  1360. case VS::ARRAY_TEX_UV2: {
  1361. attribs[i].size = 2;
  1362. if (p_format & VS::ARRAY_COMPRESS_TEX_UV2) {
  1363. attribs[i].type = _GL_HALF_FLOAT_OES;
  1364. stride += 4;
  1365. } else {
  1366. attribs[i].type = GL_FLOAT;
  1367. stride += 8;
  1368. }
  1369. attribs[i].normalized = GL_FALSE;
  1370. } break;
  1371. case VS::ARRAY_BONES: {
  1372. attribs[i].size = 4;
  1373. if (p_format & VS::ARRAY_FLAG_USE_16_BIT_BONES) {
  1374. attribs[i].type = GL_UNSIGNED_SHORT;
  1375. stride += 8;
  1376. } else {
  1377. attribs[i].type = GL_UNSIGNED_BYTE;
  1378. stride += 4;
  1379. }
  1380. attribs[i].normalized = GL_FALSE;
  1381. attribs[i].integer = true;
  1382. } break;
  1383. case VS::ARRAY_WEIGHTS: {
  1384. attribs[i].size = 4;
  1385. if (p_format & VS::ARRAY_COMPRESS_WEIGHTS) {
  1386. attribs[i].type = GL_UNSIGNED_SHORT;
  1387. stride += 8;
  1388. attribs[i].normalized = GL_TRUE;
  1389. } else {
  1390. attribs[i].type = GL_FLOAT;
  1391. stride += 16;
  1392. attribs[i].normalized = GL_FALSE;
  1393. }
  1394. } break;
  1395. case VS::ARRAY_INDEX: {
  1396. attribs[i].size = 1;
  1397. if (p_vertex_count >= (1 << 16)) {
  1398. attribs[i].type = GL_UNSIGNED_INT;
  1399. attribs[i].stride = 4;
  1400. } else {
  1401. attribs[i].type = GL_UNSIGNED_SHORT;
  1402. attribs[i].stride = 2;
  1403. }
  1404. attribs[i].normalized = GL_FALSE;
  1405. } break;
  1406. }
  1407. }
  1408. for (int i = 0; i < VS::ARRAY_MAX - 1; i++) {
  1409. attribs[i].stride = stride;
  1410. }
  1411. //validate sizes
  1412. int array_size = stride * p_vertex_count;
  1413. int index_array_size = 0;
  1414. if (array.size() != array_size && array.size() + p_vertex_count * 2 == array_size) {
  1415. //old format, convert
  1416. array = PoolVector<uint8_t>();
  1417. array.resize(p_array.size() + p_vertex_count * 2);
  1418. PoolVector<uint8_t>::Write w = array.write();
  1419. PoolVector<uint8_t>::Read r = p_array.read();
  1420. uint16_t *w16 = (uint16_t *)w.ptr();
  1421. const uint16_t *r16 = (uint16_t *)r.ptr();
  1422. uint16_t one = Math::make_half_float(1);
  1423. for (int i = 0; i < p_vertex_count; i++) {
  1424. *w16++ = *r16++;
  1425. *w16++ = *r16++;
  1426. *w16++ = *r16++;
  1427. *w16++ = one;
  1428. for (int j = 0; j < (stride / 2) - 4; j++) {
  1429. *w16++ = *r16++;
  1430. }
  1431. }
  1432. }
  1433. ERR_FAIL_COND(array.size() != array_size);
  1434. if (p_format & VS::ARRAY_FORMAT_INDEX) {
  1435. index_array_size = attribs[VS::ARRAY_INDEX].stride * p_index_count;
  1436. }
  1437. ERR_FAIL_COND(p_index_array.size() != index_array_size);
  1438. ERR_FAIL_COND(p_blend_shapes.size() != mesh->blend_shape_count);
  1439. for (int i = 0; i < p_blend_shapes.size(); i++) {
  1440. ERR_FAIL_COND(p_blend_shapes[i].size() != array_size);
  1441. }
  1442. // all valid, create stuff
  1443. Surface *surface = memnew(Surface);
  1444. surface->active = true;
  1445. surface->array_len = p_vertex_count;
  1446. surface->index_array_len = p_index_count;
  1447. surface->array_byte_size = array.size();
  1448. surface->index_array_byte_size = p_index_array.size();
  1449. surface->primitive = p_primitive;
  1450. surface->mesh = mesh;
  1451. surface->format = p_format;
  1452. surface->skeleton_bone_aabb = p_bone_aabbs;
  1453. surface->skeleton_bone_used.resize(surface->skeleton_bone_aabb.size());
  1454. surface->aabb = p_aabb;
  1455. surface->max_bone = p_bone_aabbs.size();
  1456. surface->data = array;
  1457. surface->index_data = p_index_array;
  1458. surface->total_data_size += surface->array_byte_size + surface->index_array_byte_size;
  1459. for (int i = 0; i < surface->skeleton_bone_used.size(); i++) {
  1460. surface->skeleton_bone_used.write[i] = surface->skeleton_bone_aabb[i].size.x < 0 || surface->skeleton_bone_aabb[i].size.y < 0 || surface->skeleton_bone_aabb[i].size.z < 0;
  1461. }
  1462. for (int i = 0; i < VS::ARRAY_MAX; i++) {
  1463. surface->attribs[i] = attribs[i];
  1464. }
  1465. // Okay, now the OpenGL stuff, wheeeeey \o/
  1466. {
  1467. PoolVector<uint8_t>::Read vr = array.read();
  1468. glGenBuffers(1, &surface->vertex_id);
  1469. glBindBuffer(GL_ARRAY_BUFFER, surface->vertex_id);
  1470. glBufferData(GL_ARRAY_BUFFER, array_size, vr.ptr(), (p_format & VS::ARRAY_FLAG_USE_DYNAMIC_UPDATE) ? GL_DYNAMIC_DRAW : GL_STATIC_DRAW);
  1471. glBindBuffer(GL_ARRAY_BUFFER, 0);
  1472. if (p_format & VS::ARRAY_FORMAT_INDEX) {
  1473. PoolVector<uint8_t>::Read ir = p_index_array.read();
  1474. glGenBuffers(1, &surface->index_id);
  1475. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, surface->index_id);
  1476. glBufferData(GL_ELEMENT_ARRAY_BUFFER, index_array_size, ir.ptr(), GL_STATIC_DRAW);
  1477. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
  1478. }
  1479. // TODO generate wireframes
  1480. }
  1481. {
  1482. // blend shapes
  1483. for (int i = 0; i < p_blend_shapes.size(); i++) {
  1484. Surface::BlendShape mt;
  1485. PoolVector<uint8_t>::Read vr = p_blend_shapes[i].read();
  1486. surface->total_data_size += array_size;
  1487. glGenBuffers(1, &mt.vertex_id);
  1488. glBindBuffer(GL_ARRAY_BUFFER, mt.vertex_id);
  1489. glBufferData(GL_ARRAY_BUFFER, array_size, vr.ptr(), GL_STATIC_DRAW);
  1490. glBindBuffer(GL_ARRAY_BUFFER, 0);
  1491. surface->blend_shapes.push_back(mt);
  1492. }
  1493. }
  1494. mesh->surfaces.push_back(surface);
  1495. mesh->instance_change_notify(true, false);
  1496. info.vertex_mem += surface->total_data_size;
  1497. }
  1498. void RasterizerStorageGLES2::mesh_set_blend_shape_count(RID p_mesh, int p_amount) {
  1499. Mesh *mesh = mesh_owner.getornull(p_mesh);
  1500. ERR_FAIL_COND(!mesh);
  1501. ERR_FAIL_COND(mesh->surfaces.size() != 0);
  1502. ERR_FAIL_COND(p_amount < 0);
  1503. mesh->blend_shape_count = p_amount;
  1504. }
  1505. int RasterizerStorageGLES2::mesh_get_blend_shape_count(RID p_mesh) const {
  1506. const Mesh *mesh = mesh_owner.getornull(p_mesh);
  1507. ERR_FAIL_COND_V(!mesh, 0);
  1508. return mesh->blend_shape_count;
  1509. }
  1510. void RasterizerStorageGLES2::mesh_set_blend_shape_mode(RID p_mesh, VS::BlendShapeMode p_mode) {
  1511. Mesh *mesh = mesh_owner.getornull(p_mesh);
  1512. ERR_FAIL_COND(!mesh);
  1513. mesh->blend_shape_mode = p_mode;
  1514. }
  1515. VS::BlendShapeMode RasterizerStorageGLES2::mesh_get_blend_shape_mode(RID p_mesh) const {
  1516. const Mesh *mesh = mesh_owner.getornull(p_mesh);
  1517. ERR_FAIL_COND_V(!mesh, VS::BLEND_SHAPE_MODE_NORMALIZED);
  1518. return mesh->blend_shape_mode;
  1519. }
  1520. void RasterizerStorageGLES2::mesh_surface_update_region(RID p_mesh, int p_surface, int p_offset, const PoolVector<uint8_t> &p_data) {
  1521. Mesh *mesh = mesh_owner.getornull(p_mesh);
  1522. ERR_FAIL_COND(!mesh);
  1523. ERR_FAIL_INDEX(p_surface, mesh->surfaces.size());
  1524. int total_size = p_data.size();
  1525. ERR_FAIL_COND(p_offset + total_size > mesh->surfaces[p_surface]->array_byte_size);
  1526. PoolVector<uint8_t>::Read r = p_data.read();
  1527. glBindBuffer(GL_ARRAY_BUFFER, mesh->surfaces[p_surface]->vertex_id);
  1528. glBufferSubData(GL_ARRAY_BUFFER, p_offset, total_size, r.ptr());
  1529. glBindBuffer(GL_ARRAY_BUFFER, 0); //unbind
  1530. }
  1531. void RasterizerStorageGLES2::mesh_surface_set_material(RID p_mesh, int p_surface, RID p_material) {
  1532. Mesh *mesh = mesh_owner.getornull(p_mesh);
  1533. ERR_FAIL_COND(!mesh);
  1534. ERR_FAIL_INDEX(p_surface, mesh->surfaces.size());
  1535. if (mesh->surfaces[p_surface]->material == p_material)
  1536. return;
  1537. if (mesh->surfaces[p_surface]->material.is_valid()) {
  1538. _material_remove_geometry(mesh->surfaces[p_surface]->material, mesh->surfaces[p_surface]);
  1539. }
  1540. mesh->surfaces[p_surface]->material = p_material;
  1541. if (mesh->surfaces[p_surface]->material.is_valid()) {
  1542. _material_add_geometry(mesh->surfaces[p_surface]->material, mesh->surfaces[p_surface]);
  1543. }
  1544. mesh->instance_change_notify(false, true);
  1545. }
  1546. RID RasterizerStorageGLES2::mesh_surface_get_material(RID p_mesh, int p_surface) const {
  1547. const Mesh *mesh = mesh_owner.getornull(p_mesh);
  1548. ERR_FAIL_COND_V(!mesh, RID());
  1549. ERR_FAIL_INDEX_V(p_surface, mesh->surfaces.size(), RID());
  1550. return mesh->surfaces[p_surface]->material;
  1551. }
  1552. int RasterizerStorageGLES2::mesh_surface_get_array_len(RID p_mesh, int p_surface) const {
  1553. const Mesh *mesh = mesh_owner.getornull(p_mesh);
  1554. ERR_FAIL_COND_V(!mesh, 0);
  1555. ERR_FAIL_INDEX_V(p_surface, mesh->surfaces.size(), 0);
  1556. return mesh->surfaces[p_surface]->array_len;
  1557. }
  1558. int RasterizerStorageGLES2::mesh_surface_get_array_index_len(RID p_mesh, int p_surface) const {
  1559. const Mesh *mesh = mesh_owner.getornull(p_mesh);
  1560. ERR_FAIL_COND_V(!mesh, 0);
  1561. ERR_FAIL_INDEX_V(p_surface, mesh->surfaces.size(), 0);
  1562. return mesh->surfaces[p_surface]->index_array_len;
  1563. }
  1564. PoolVector<uint8_t> RasterizerStorageGLES2::mesh_surface_get_array(RID p_mesh, int p_surface) const {
  1565. const Mesh *mesh = mesh_owner.getornull(p_mesh);
  1566. ERR_FAIL_COND_V(!mesh, PoolVector<uint8_t>());
  1567. ERR_FAIL_INDEX_V(p_surface, mesh->surfaces.size(), PoolVector<uint8_t>());
  1568. Surface *surface = mesh->surfaces[p_surface];
  1569. return surface->data;
  1570. }
  1571. PoolVector<uint8_t> RasterizerStorageGLES2::mesh_surface_get_index_array(RID p_mesh, int p_surface) const {
  1572. const Mesh *mesh = mesh_owner.getornull(p_mesh);
  1573. ERR_FAIL_COND_V(!mesh, PoolVector<uint8_t>());
  1574. ERR_FAIL_INDEX_V(p_surface, mesh->surfaces.size(), PoolVector<uint8_t>());
  1575. Surface *surface = mesh->surfaces[p_surface];
  1576. return surface->index_data;
  1577. }
  1578. uint32_t RasterizerStorageGLES2::mesh_surface_get_format(RID p_mesh, int p_surface) const {
  1579. const Mesh *mesh = mesh_owner.getornull(p_mesh);
  1580. ERR_FAIL_COND_V(!mesh, 0);
  1581. ERR_FAIL_INDEX_V(p_surface, mesh->surfaces.size(), 0);
  1582. return mesh->surfaces[p_surface]->format;
  1583. }
  1584. VS::PrimitiveType RasterizerStorageGLES2::mesh_surface_get_primitive_type(RID p_mesh, int p_surface) const {
  1585. const Mesh *mesh = mesh_owner.getornull(p_mesh);
  1586. ERR_FAIL_COND_V(!mesh, VS::PRIMITIVE_MAX);
  1587. ERR_FAIL_INDEX_V(p_surface, mesh->surfaces.size(), VS::PRIMITIVE_MAX);
  1588. return mesh->surfaces[p_surface]->primitive;
  1589. }
  1590. AABB RasterizerStorageGLES2::mesh_surface_get_aabb(RID p_mesh, int p_surface) const {
  1591. const Mesh *mesh = mesh_owner.getornull(p_mesh);
  1592. ERR_FAIL_COND_V(!mesh, AABB());
  1593. ERR_FAIL_INDEX_V(p_surface, mesh->surfaces.size(), AABB());
  1594. return mesh->surfaces[p_surface]->aabb;
  1595. }
  1596. Vector<PoolVector<uint8_t> > RasterizerStorageGLES2::mesh_surface_get_blend_shapes(RID p_mesh, int p_surface) const {
  1597. WARN_PRINT("GLES2 mesh_surface_get_blend_shapes is not implemented");
  1598. return Vector<PoolVector<uint8_t> >();
  1599. }
  1600. Vector<AABB> RasterizerStorageGLES2::mesh_surface_get_skeleton_aabb(RID p_mesh, int p_surface) const {
  1601. const Mesh *mesh = mesh_owner.getornull(p_mesh);
  1602. ERR_FAIL_COND_V(!mesh, Vector<AABB>());
  1603. ERR_FAIL_INDEX_V(p_surface, mesh->surfaces.size(), Vector<AABB>());
  1604. return mesh->surfaces[p_surface]->skeleton_bone_aabb;
  1605. }
  1606. void RasterizerStorageGLES2::mesh_remove_surface(RID p_mesh, int p_surface) {
  1607. Mesh *mesh = mesh_owner.getornull(p_mesh);
  1608. ERR_FAIL_COND(!mesh);
  1609. ERR_FAIL_INDEX(p_surface, mesh->surfaces.size());
  1610. Surface *surface = mesh->surfaces[p_surface];
  1611. if (surface->material.is_valid()) {
  1612. // TODO _material_remove_geometry(surface->material, mesh->surfaces[p_surface]);
  1613. }
  1614. glDeleteBuffers(1, &surface->vertex_id);
  1615. if (surface->index_id) {
  1616. glDeleteBuffers(1, &surface->index_id);
  1617. }
  1618. for (int i = 0; i < surface->blend_shapes.size(); i++) {
  1619. glDeleteBuffers(1, &surface->blend_shapes[i].vertex_id);
  1620. }
  1621. info.vertex_mem -= surface->total_data_size;
  1622. memdelete(surface);
  1623. mesh->surfaces.remove(p_surface);
  1624. mesh->instance_change_notify(true, true);
  1625. }
  1626. int RasterizerStorageGLES2::mesh_get_surface_count(RID p_mesh) const {
  1627. const Mesh *mesh = mesh_owner.getornull(p_mesh);
  1628. ERR_FAIL_COND_V(!mesh, 0);
  1629. return mesh->surfaces.size();
  1630. }
  1631. void RasterizerStorageGLES2::mesh_set_custom_aabb(RID p_mesh, const AABB &p_aabb) {
  1632. Mesh *mesh = mesh_owner.getornull(p_mesh);
  1633. ERR_FAIL_COND(!mesh);
  1634. mesh->custom_aabb = p_aabb;
  1635. }
  1636. AABB RasterizerStorageGLES2::mesh_get_custom_aabb(RID p_mesh) const {
  1637. const Mesh *mesh = mesh_owner.getornull(p_mesh);
  1638. ERR_FAIL_COND_V(!mesh, AABB());
  1639. return mesh->custom_aabb;
  1640. }
  1641. AABB RasterizerStorageGLES2::mesh_get_aabb(RID p_mesh, RID p_skeleton) const {
  1642. Mesh *mesh = mesh_owner.get(p_mesh);
  1643. ERR_FAIL_COND_V(!mesh, AABB());
  1644. if (mesh->custom_aabb != AABB())
  1645. return mesh->custom_aabb;
  1646. // TODO handle skeletons
  1647. AABB aabb;
  1648. if (mesh->surfaces.size() >= 1) {
  1649. aabb = mesh->surfaces[0]->aabb;
  1650. }
  1651. for (int i = 0; i < mesh->surfaces.size(); i++) {
  1652. aabb.merge_with(mesh->surfaces[i]->aabb);
  1653. }
  1654. return aabb;
  1655. }
  1656. void RasterizerStorageGLES2::mesh_clear(RID p_mesh) {
  1657. Mesh *mesh = mesh_owner.getornull(p_mesh);
  1658. ERR_FAIL_COND(!mesh);
  1659. while (mesh->surfaces.size()) {
  1660. mesh_remove_surface(p_mesh, 0);
  1661. }
  1662. }
  1663. /* MULTIMESH API */
  1664. RID RasterizerStorageGLES2::multimesh_create() {
  1665. MultiMesh *multimesh = memnew(MultiMesh);
  1666. return multimesh_owner.make_rid(multimesh);
  1667. }
  1668. void RasterizerStorageGLES2::multimesh_allocate(RID p_multimesh, int p_instances, VS::MultimeshTransformFormat p_transform_format, VS::MultimeshColorFormat p_color_format, VS::MultimeshCustomDataFormat p_data) {
  1669. MultiMesh *multimesh = multimesh_owner.getornull(p_multimesh);
  1670. ERR_FAIL_COND(!multimesh);
  1671. if (multimesh->size == p_instances && multimesh->transform_format == p_transform_format && multimesh->color_format == p_color_format && multimesh->custom_data_format == p_data) {
  1672. return;
  1673. }
  1674. multimesh->size = p_instances;
  1675. multimesh->color_format = p_color_format;
  1676. multimesh->transform_format = p_transform_format;
  1677. multimesh->custom_data_format = p_data;
  1678. if (multimesh->size) {
  1679. multimesh->data.resize(0);
  1680. }
  1681. if (multimesh->transform_format == VS::MULTIMESH_TRANSFORM_2D) {
  1682. multimesh->xform_floats = 8;
  1683. } else {
  1684. multimesh->xform_floats = 12;
  1685. }
  1686. if (multimesh->color_format == VS::MULTIMESH_COLOR_NONE) {
  1687. multimesh->color_floats = 0;
  1688. } else if (multimesh->color_format == VS::MULTIMESH_COLOR_8BIT) {
  1689. multimesh->color_floats = 1;
  1690. } else if (multimesh->color_format == VS::MULTIMESH_COLOR_FLOAT) {
  1691. multimesh->color_floats = 4;
  1692. }
  1693. if (multimesh->custom_data_format == VS::MULTIMESH_CUSTOM_DATA_NONE) {
  1694. multimesh->custom_data_floats = 0;
  1695. } else if (multimesh->custom_data_format == VS::MULTIMESH_CUSTOM_DATA_8BIT) {
  1696. multimesh->custom_data_floats = 1;
  1697. } else if (multimesh->custom_data_format == VS::MULTIMESH_CUSTOM_DATA_FLOAT) {
  1698. multimesh->custom_data_floats = 4;
  1699. }
  1700. int format_floats = multimesh->color_floats + multimesh->xform_floats + multimesh->custom_data_floats;
  1701. multimesh->data.resize(format_floats * p_instances);
  1702. for (int i = 0; i < p_instances * format_floats; i += format_floats) {
  1703. int color_from = 0;
  1704. int custom_data_from = 0;
  1705. if (multimesh->transform_format == VS::MULTIMESH_TRANSFORM_2D) {
  1706. multimesh->data.write[i + 0] = 1.0;
  1707. multimesh->data.write[i + 1] = 0.0;
  1708. multimesh->data.write[i + 2] = 0.0;
  1709. multimesh->data.write[i + 3] = 0.0;
  1710. multimesh->data.write[i + 4] = 0.0;
  1711. multimesh->data.write[i + 5] = 1.0;
  1712. multimesh->data.write[i + 6] = 0.0;
  1713. multimesh->data.write[i + 7] = 0.0;
  1714. color_from = 8;
  1715. custom_data_from = 8;
  1716. } else {
  1717. multimesh->data.write[i + 0] = 1.0;
  1718. multimesh->data.write[i + 1] = 0.0;
  1719. multimesh->data.write[i + 2] = 0.0;
  1720. multimesh->data.write[i + 3] = 0.0;
  1721. multimesh->data.write[i + 4] = 0.0;
  1722. multimesh->data.write[i + 5] = 1.0;
  1723. multimesh->data.write[i + 6] = 0.0;
  1724. multimesh->data.write[i + 7] = 0.0;
  1725. multimesh->data.write[i + 8] = 0.0;
  1726. multimesh->data.write[i + 9] = 0.0;
  1727. multimesh->data.write[i + 10] = 1.0;
  1728. multimesh->data.write[i + 11] = 0.0;
  1729. color_from = 12;
  1730. custom_data_from = 12;
  1731. }
  1732. if (multimesh->color_format == VS::MULTIMESH_COLOR_8BIT) {
  1733. union {
  1734. uint32_t colu;
  1735. float colf;
  1736. } cu;
  1737. cu.colu = 0xFFFFFFFF;
  1738. multimesh->data.write[i + color_from + 0] = cu.colf;
  1739. custom_data_from = color_from + 1;
  1740. } else if (multimesh->color_format == VS::MULTIMESH_COLOR_FLOAT) {
  1741. multimesh->data.write[i + color_from + 0] = 1.0;
  1742. multimesh->data.write[i + color_from + 1] = 1.0;
  1743. multimesh->data.write[i + color_from + 2] = 1.0;
  1744. multimesh->data.write[i + color_from + 3] = 1.0;
  1745. custom_data_from = color_from + 4;
  1746. }
  1747. if (multimesh->custom_data_format == VS::MULTIMESH_CUSTOM_DATA_8BIT) {
  1748. union {
  1749. uint32_t colu;
  1750. float colf;
  1751. } cu;
  1752. cu.colu = 0;
  1753. multimesh->data.write[i + custom_data_from + 0] = cu.colf;
  1754. } else if (multimesh->custom_data_format == VS::MULTIMESH_CUSTOM_DATA_FLOAT) {
  1755. multimesh->data.write[i + custom_data_from + 0] = 0.0;
  1756. multimesh->data.write[i + custom_data_from + 1] = 0.0;
  1757. multimesh->data.write[i + custom_data_from + 2] = 0.0;
  1758. multimesh->data.write[i + custom_data_from + 3] = 0.0;
  1759. }
  1760. }
  1761. multimesh->dirty_aabb = true;
  1762. multimesh->dirty_data = true;
  1763. if (!multimesh->update_list.in_list()) {
  1764. multimesh_update_list.add(&multimesh->update_list);
  1765. }
  1766. }
  1767. int RasterizerStorageGLES2::multimesh_get_instance_count(RID p_multimesh) const {
  1768. MultiMesh *multimesh = multimesh_owner.getornull(p_multimesh);
  1769. ERR_FAIL_COND_V(!multimesh, 0);
  1770. return multimesh->size;
  1771. }
  1772. void RasterizerStorageGLES2::multimesh_set_mesh(RID p_multimesh, RID p_mesh) {
  1773. MultiMesh *multimesh = multimesh_owner.getornull(p_multimesh);
  1774. ERR_FAIL_COND(!multimesh);
  1775. if (multimesh->mesh.is_valid()) {
  1776. Mesh *mesh = mesh_owner.getornull(multimesh->mesh);
  1777. if (mesh) {
  1778. mesh->multimeshes.remove(&multimesh->mesh_list);
  1779. }
  1780. }
  1781. multimesh->mesh = p_mesh;
  1782. if (multimesh->mesh.is_valid()) {
  1783. Mesh *mesh = mesh_owner.getornull(multimesh->mesh);
  1784. if (mesh) {
  1785. mesh->multimeshes.add(&multimesh->mesh_list);
  1786. }
  1787. }
  1788. multimesh->dirty_aabb = true;
  1789. if (!multimesh->update_list.in_list()) {
  1790. multimesh_update_list.add(&multimesh->update_list);
  1791. }
  1792. }
  1793. void RasterizerStorageGLES2::multimesh_instance_set_transform(RID p_multimesh, int p_index, const Transform &p_transform) {
  1794. MultiMesh *multimesh = multimesh_owner.getornull(p_multimesh);
  1795. ERR_FAIL_COND(!multimesh);
  1796. ERR_FAIL_INDEX(p_index, multimesh->size);
  1797. ERR_FAIL_COND(multimesh->transform_format == VS::MULTIMESH_TRANSFORM_2D);
  1798. int stride = multimesh->color_floats + multimesh->custom_data_floats + multimesh->xform_floats;
  1799. float *dataptr = &multimesh->data.write[stride * p_index];
  1800. dataptr[0] = p_transform.basis.elements[0][0];
  1801. dataptr[1] = p_transform.basis.elements[0][1];
  1802. dataptr[2] = p_transform.basis.elements[0][2];
  1803. dataptr[3] = p_transform.origin.x;
  1804. dataptr[4] = p_transform.basis.elements[1][0];
  1805. dataptr[5] = p_transform.basis.elements[1][1];
  1806. dataptr[6] = p_transform.basis.elements[1][2];
  1807. dataptr[7] = p_transform.origin.y;
  1808. dataptr[8] = p_transform.basis.elements[2][0];
  1809. dataptr[9] = p_transform.basis.elements[2][1];
  1810. dataptr[10] = p_transform.basis.elements[2][2];
  1811. dataptr[11] = p_transform.origin.z;
  1812. multimesh->dirty_data = true;
  1813. multimesh->dirty_aabb = true;
  1814. if (!multimesh->update_list.in_list()) {
  1815. multimesh_update_list.add(&multimesh->update_list);
  1816. }
  1817. }
  1818. void RasterizerStorageGLES2::multimesh_instance_set_transform_2d(RID p_multimesh, int p_index, const Transform2D &p_transform) {
  1819. MultiMesh *multimesh = multimesh_owner.getornull(p_multimesh);
  1820. ERR_FAIL_COND(!multimesh);
  1821. ERR_FAIL_INDEX(p_index, multimesh->size);
  1822. ERR_FAIL_COND(multimesh->transform_format == VS::MULTIMESH_TRANSFORM_3D);
  1823. int stride = multimesh->color_floats + multimesh->xform_floats + multimesh->custom_data_floats;
  1824. float *dataptr = &multimesh->data.write[stride * p_index];
  1825. dataptr[0] = p_transform.elements[0][0];
  1826. dataptr[1] = p_transform.elements[1][0];
  1827. dataptr[2] = 0;
  1828. dataptr[3] = p_transform.elements[2][0];
  1829. dataptr[4] = p_transform.elements[0][1];
  1830. dataptr[5] = p_transform.elements[1][1];
  1831. dataptr[6] = 0;
  1832. dataptr[7] = p_transform.elements[2][1];
  1833. multimesh->dirty_data = true;
  1834. multimesh->dirty_aabb = true;
  1835. if (!multimesh->update_list.in_list()) {
  1836. multimesh_update_list.add(&multimesh->update_list);
  1837. }
  1838. }
  1839. void RasterizerStorageGLES2::multimesh_instance_set_color(RID p_multimesh, int p_index, const Color &p_color) {
  1840. MultiMesh *multimesh = multimesh_owner.getornull(p_multimesh);
  1841. ERR_FAIL_COND(!multimesh);
  1842. ERR_FAIL_INDEX(p_index, multimesh->size);
  1843. ERR_FAIL_COND(multimesh->color_format == VS::MULTIMESH_COLOR_NONE);
  1844. int stride = multimesh->color_floats + multimesh->xform_floats + multimesh->custom_data_floats;
  1845. float *dataptr = &multimesh->data.write[stride * p_index + multimesh->xform_floats];
  1846. if (multimesh->color_format == VS::MULTIMESH_COLOR_8BIT) {
  1847. uint8_t *data8 = (uint8_t *)dataptr;
  1848. data8[0] = CLAMP(p_color.r * 255.0, 0, 255);
  1849. data8[1] = CLAMP(p_color.g * 255.0, 0, 255);
  1850. data8[2] = CLAMP(p_color.b * 255.0, 0, 255);
  1851. data8[3] = CLAMP(p_color.a * 255.0, 0, 255);
  1852. } else if (multimesh->color_format == VS::MULTIMESH_COLOR_FLOAT) {
  1853. dataptr[0] = p_color.r;
  1854. dataptr[1] = p_color.g;
  1855. dataptr[2] = p_color.b;
  1856. dataptr[3] = p_color.a;
  1857. }
  1858. multimesh->dirty_data = true;
  1859. multimesh->dirty_aabb = true;
  1860. if (!multimesh->update_list.in_list()) {
  1861. multimesh_update_list.add(&multimesh->update_list);
  1862. }
  1863. }
  1864. void RasterizerStorageGLES2::multimesh_instance_set_custom_data(RID p_multimesh, int p_index, const Color &p_custom_data) {
  1865. MultiMesh *multimesh = multimesh_owner.getornull(p_multimesh);
  1866. ERR_FAIL_COND(!multimesh);
  1867. ERR_FAIL_INDEX(p_index, multimesh->size);
  1868. ERR_FAIL_COND(multimesh->custom_data_format == VS::MULTIMESH_CUSTOM_DATA_NONE);
  1869. int stride = multimesh->color_floats + multimesh->xform_floats + multimesh->custom_data_floats;
  1870. float *dataptr = &multimesh->data.write[stride * p_index + multimesh->xform_floats + multimesh->color_floats];
  1871. if (multimesh->custom_data_format == VS::MULTIMESH_CUSTOM_DATA_8BIT) {
  1872. uint8_t *data8 = (uint8_t *)dataptr;
  1873. data8[0] = CLAMP(p_custom_data.r * 255.0, 0, 255);
  1874. data8[1] = CLAMP(p_custom_data.g * 255.0, 0, 255);
  1875. data8[2] = CLAMP(p_custom_data.b * 255.0, 0, 255);
  1876. data8[3] = CLAMP(p_custom_data.a * 255.0, 0, 255);
  1877. } else if (multimesh->custom_data_format == VS::MULTIMESH_CUSTOM_DATA_FLOAT) {
  1878. dataptr[0] = p_custom_data.r;
  1879. dataptr[1] = p_custom_data.g;
  1880. dataptr[2] = p_custom_data.b;
  1881. dataptr[3] = p_custom_data.a;
  1882. }
  1883. multimesh->dirty_data = true;
  1884. multimesh->dirty_aabb = true;
  1885. if (!multimesh->update_list.in_list()) {
  1886. multimesh_update_list.add(&multimesh->update_list);
  1887. }
  1888. }
  1889. RID RasterizerStorageGLES2::multimesh_get_mesh(RID p_multimesh) const {
  1890. MultiMesh *multimesh = multimesh_owner.getornull(p_multimesh);
  1891. ERR_FAIL_COND_V(!multimesh, RID());
  1892. return multimesh->mesh;
  1893. }
  1894. Transform RasterizerStorageGLES2::multimesh_instance_get_transform(RID p_multimesh, int p_index) const {
  1895. MultiMesh *multimesh = multimesh_owner.getornull(p_multimesh);
  1896. ERR_FAIL_COND_V(!multimesh, Transform());
  1897. ERR_FAIL_INDEX_V(p_index, multimesh->size, Transform());
  1898. ERR_FAIL_COND_V(multimesh->transform_format == VS::MULTIMESH_TRANSFORM_2D, Transform());
  1899. int stride = multimesh->color_floats + multimesh->xform_floats + multimesh->custom_data_floats;
  1900. float *dataptr = &multimesh->data.write[stride * p_index];
  1901. Transform xform;
  1902. xform.basis.elements[0][0] = dataptr[0];
  1903. xform.basis.elements[0][1] = dataptr[1];
  1904. xform.basis.elements[0][2] = dataptr[2];
  1905. xform.origin.x = dataptr[3];
  1906. xform.basis.elements[1][0] = dataptr[4];
  1907. xform.basis.elements[1][1] = dataptr[5];
  1908. xform.basis.elements[1][2] = dataptr[6];
  1909. xform.origin.y = dataptr[7];
  1910. xform.basis.elements[2][0] = dataptr[8];
  1911. xform.basis.elements[2][1] = dataptr[9];
  1912. xform.basis.elements[2][2] = dataptr[10];
  1913. xform.origin.z = dataptr[11];
  1914. return xform;
  1915. }
  1916. Transform2D RasterizerStorageGLES2::multimesh_instance_get_transform_2d(RID p_multimesh, int p_index) const {
  1917. MultiMesh *multimesh = multimesh_owner.getornull(p_multimesh);
  1918. ERR_FAIL_COND_V(!multimesh, Transform2D());
  1919. ERR_FAIL_INDEX_V(p_index, multimesh->size, Transform2D());
  1920. ERR_FAIL_COND_V(multimesh->transform_format == VS::MULTIMESH_TRANSFORM_3D, Transform2D());
  1921. int stride = multimesh->color_floats + multimesh->xform_floats + multimesh->custom_data_floats;
  1922. float *dataptr = &multimesh->data.write[stride * p_index];
  1923. Transform2D xform;
  1924. xform.elements[0][0] = dataptr[0];
  1925. xform.elements[1][0] = dataptr[1];
  1926. xform.elements[2][0] = dataptr[3];
  1927. xform.elements[0][1] = dataptr[4];
  1928. xform.elements[1][1] = dataptr[5];
  1929. xform.elements[2][1] = dataptr[7];
  1930. return xform;
  1931. }
  1932. Color RasterizerStorageGLES2::multimesh_instance_get_color(RID p_multimesh, int p_index) const {
  1933. MultiMesh *multimesh = multimesh_owner.getornull(p_multimesh);
  1934. ERR_FAIL_COND_V(!multimesh, Color());
  1935. ERR_FAIL_INDEX_V(p_index, multimesh->size, Color());
  1936. ERR_FAIL_COND_V(multimesh->color_format == VS::MULTIMESH_COLOR_NONE, Color());
  1937. int stride = multimesh->color_floats + multimesh->xform_floats + multimesh->custom_data_floats;
  1938. float *dataptr = &multimesh->data.write[stride * p_index + multimesh->xform_floats];
  1939. if (multimesh->color_format == VS::MULTIMESH_COLOR_8BIT) {
  1940. union {
  1941. uint32_t colu;
  1942. float colf;
  1943. } cu;
  1944. cu.colf = dataptr[0];
  1945. return Color::hex(BSWAP32(cu.colu));
  1946. } else if (multimesh->color_format == VS::MULTIMESH_COLOR_FLOAT) {
  1947. Color c;
  1948. c.r = dataptr[0];
  1949. c.g = dataptr[1];
  1950. c.b = dataptr[2];
  1951. c.a = dataptr[3];
  1952. return c;
  1953. }
  1954. return Color();
  1955. }
  1956. Color RasterizerStorageGLES2::multimesh_instance_get_custom_data(RID p_multimesh, int p_index) const {
  1957. MultiMesh *multimesh = multimesh_owner.getornull(p_multimesh);
  1958. ERR_FAIL_COND_V(!multimesh, Color());
  1959. ERR_FAIL_INDEX_V(p_index, multimesh->size, Color());
  1960. ERR_FAIL_COND_V(multimesh->custom_data_format == VS::MULTIMESH_CUSTOM_DATA_NONE, Color());
  1961. int stride = multimesh->color_floats + multimesh->xform_floats + multimesh->custom_data_floats;
  1962. float *dataptr = &multimesh->data.write[stride * p_index + multimesh->xform_floats + multimesh->color_floats];
  1963. if (multimesh->custom_data_format == VS::MULTIMESH_CUSTOM_DATA_8BIT) {
  1964. union {
  1965. uint32_t colu;
  1966. float colf;
  1967. } cu;
  1968. cu.colf = dataptr[0];
  1969. return Color::hex(BSWAP32(cu.colu));
  1970. } else if (multimesh->custom_data_format == VS::MULTIMESH_CUSTOM_DATA_FLOAT) {
  1971. Color c;
  1972. c.r = dataptr[0];
  1973. c.g = dataptr[1];
  1974. c.b = dataptr[2];
  1975. c.a = dataptr[3];
  1976. return c;
  1977. }
  1978. return Color();
  1979. }
  1980. void RasterizerStorageGLES2::multimesh_set_as_bulk_array(RID p_multimesh, const PoolVector<float> &p_array) {
  1981. MultiMesh *multimesh = multimesh_owner.getornull(p_multimesh);
  1982. ERR_FAIL_COND(!multimesh);
  1983. int dsize = multimesh->data.size();
  1984. ERR_FAIL_COND(dsize != p_array.size());
  1985. PoolVector<float>::Read r = p_array.read();
  1986. copymem(multimesh->data.ptrw(), r.ptr(), dsize * sizeof(float));
  1987. multimesh->dirty_data = true;
  1988. multimesh->dirty_aabb = true;
  1989. if (!multimesh->update_list.in_list()) {
  1990. multimesh_update_list.add(&multimesh->update_list);
  1991. }
  1992. }
  1993. void RasterizerStorageGLES2::multimesh_set_visible_instances(RID p_multimesh, int p_visible) {
  1994. MultiMesh *multimesh = multimesh_owner.getornull(p_multimesh);
  1995. ERR_FAIL_COND(!multimesh);
  1996. multimesh->visible_instances = p_visible;
  1997. }
  1998. int RasterizerStorageGLES2::multimesh_get_visible_instances(RID p_multimesh) const {
  1999. MultiMesh *multimesh = multimesh_owner.getornull(p_multimesh);
  2000. ERR_FAIL_COND_V(!multimesh, -1);
  2001. return multimesh->visible_instances;
  2002. }
  2003. AABB RasterizerStorageGLES2::multimesh_get_aabb(RID p_multimesh) const {
  2004. MultiMesh *multimesh = multimesh_owner.getornull(p_multimesh);
  2005. ERR_FAIL_COND_V(!multimesh, AABB());
  2006. const_cast<RasterizerStorageGLES2 *>(this)->update_dirty_multimeshes();
  2007. return multimesh->aabb;
  2008. }
  2009. void RasterizerStorageGLES2::update_dirty_multimeshes() {
  2010. while (multimesh_update_list.first()) {
  2011. MultiMesh *multimesh = multimesh_update_list.first()->self();
  2012. if (multimesh->size && multimesh->dirty_aabb) {
  2013. AABB mesh_aabb;
  2014. if (multimesh->mesh.is_valid()) {
  2015. mesh_aabb = mesh_get_aabb(multimesh->mesh, RID());
  2016. }
  2017. mesh_aabb.size += Vector3(0.001, 0.001, 0.001); //in case mesh is empty in one of the sides
  2018. int stride = multimesh->color_floats + multimesh->xform_floats + multimesh->custom_data_floats;
  2019. int count = multimesh->data.size();
  2020. float *data = multimesh->data.ptrw();
  2021. AABB aabb;
  2022. if (multimesh->transform_format == VS::MULTIMESH_TRANSFORM_2D) {
  2023. for (int i = 0; i < count; i += stride) {
  2024. float *dataptr = &data[i];
  2025. Transform xform;
  2026. xform.basis[0][0] = dataptr[0];
  2027. xform.basis[0][1] = dataptr[1];
  2028. xform.origin[0] = dataptr[3];
  2029. xform.basis[1][0] = dataptr[4];
  2030. xform.basis[1][1] = dataptr[5];
  2031. xform.origin[1] = dataptr[7];
  2032. AABB laabb = xform.xform(mesh_aabb);
  2033. if (i == 0) {
  2034. aabb = laabb;
  2035. } else {
  2036. aabb.merge_with(laabb);
  2037. }
  2038. }
  2039. } else {
  2040. for (int i = 0; i < count; i += stride) {
  2041. float *dataptr = &data[i];
  2042. Transform xform;
  2043. xform.basis.elements[0][0] = dataptr[0];
  2044. xform.basis.elements[0][1] = dataptr[1];
  2045. xform.basis.elements[0][2] = dataptr[2];
  2046. xform.origin.x = dataptr[3];
  2047. xform.basis.elements[1][0] = dataptr[4];
  2048. xform.basis.elements[1][1] = dataptr[5];
  2049. xform.basis.elements[1][2] = dataptr[6];
  2050. xform.origin.y = dataptr[7];
  2051. xform.basis.elements[2][0] = dataptr[8];
  2052. xform.basis.elements[2][1] = dataptr[9];
  2053. xform.basis.elements[2][2] = dataptr[10];
  2054. xform.origin.z = dataptr[11];
  2055. AABB laabb = xform.xform(mesh_aabb);
  2056. if (i == 0) {
  2057. aabb = laabb;
  2058. } else {
  2059. aabb.merge_with(laabb);
  2060. }
  2061. }
  2062. }
  2063. multimesh->aabb = aabb;
  2064. }
  2065. multimesh->dirty_aabb = false;
  2066. multimesh->dirty_data = false;
  2067. multimesh->instance_change_notify(true, false);
  2068. multimesh_update_list.remove(multimesh_update_list.first());
  2069. }
  2070. }
  2071. /* IMMEDIATE API */
  2072. RID RasterizerStorageGLES2::immediate_create() {
  2073. Immediate *im = memnew(Immediate);
  2074. return immediate_owner.make_rid(im);
  2075. }
  2076. void RasterizerStorageGLES2::immediate_begin(RID p_immediate, VS::PrimitiveType p_primitive, RID p_texture) {
  2077. Immediate *im = immediate_owner.get(p_immediate);
  2078. ERR_FAIL_COND(!im);
  2079. ERR_FAIL_COND(im->building);
  2080. Immediate::Chunk ic;
  2081. ic.texture = p_texture;
  2082. ic.primitive = p_primitive;
  2083. im->chunks.push_back(ic);
  2084. im->mask = 0;
  2085. im->building = true;
  2086. }
  2087. void RasterizerStorageGLES2::immediate_vertex(RID p_immediate, const Vector3 &p_vertex) {
  2088. Immediate *im = immediate_owner.get(p_immediate);
  2089. ERR_FAIL_COND(!im);
  2090. ERR_FAIL_COND(!im->building);
  2091. Immediate::Chunk *c = &im->chunks.back()->get();
  2092. if (c->vertices.empty() && im->chunks.size() == 1) {
  2093. im->aabb.position = p_vertex;
  2094. im->aabb.size = Vector3();
  2095. } else {
  2096. im->aabb.expand_to(p_vertex);
  2097. }
  2098. if (im->mask & VS::ARRAY_FORMAT_NORMAL)
  2099. c->normals.push_back(chunk_normal);
  2100. if (im->mask & VS::ARRAY_FORMAT_TANGENT)
  2101. c->tangents.push_back(chunk_tangent);
  2102. if (im->mask & VS::ARRAY_FORMAT_COLOR)
  2103. c->colors.push_back(chunk_color);
  2104. if (im->mask & VS::ARRAY_FORMAT_TEX_UV)
  2105. c->uvs.push_back(chunk_uv);
  2106. if (im->mask & VS::ARRAY_FORMAT_TEX_UV2)
  2107. c->uv2s.push_back(chunk_uv2);
  2108. im->mask |= VS::ARRAY_FORMAT_VERTEX;
  2109. c->vertices.push_back(p_vertex);
  2110. }
  2111. void RasterizerStorageGLES2::immediate_normal(RID p_immediate, const Vector3 &p_normal) {
  2112. Immediate *im = immediate_owner.get(p_immediate);
  2113. ERR_FAIL_COND(!im);
  2114. ERR_FAIL_COND(!im->building);
  2115. im->mask |= VS::ARRAY_FORMAT_NORMAL;
  2116. chunk_normal = p_normal;
  2117. }
  2118. void RasterizerStorageGLES2::immediate_tangent(RID p_immediate, const Plane &p_tangent) {
  2119. Immediate *im = immediate_owner.get(p_immediate);
  2120. ERR_FAIL_COND(!im);
  2121. ERR_FAIL_COND(!im->building);
  2122. im->mask |= VS::ARRAY_FORMAT_TANGENT;
  2123. chunk_tangent = p_tangent;
  2124. }
  2125. void RasterizerStorageGLES2::immediate_color(RID p_immediate, const Color &p_color) {
  2126. Immediate *im = immediate_owner.get(p_immediate);
  2127. ERR_FAIL_COND(!im);
  2128. ERR_FAIL_COND(!im->building);
  2129. im->mask |= VS::ARRAY_FORMAT_COLOR;
  2130. chunk_color = p_color;
  2131. }
  2132. void RasterizerStorageGLES2::immediate_uv(RID p_immediate, const Vector2 &tex_uv) {
  2133. Immediate *im = immediate_owner.get(p_immediate);
  2134. ERR_FAIL_COND(!im);
  2135. ERR_FAIL_COND(!im->building);
  2136. im->mask |= VS::ARRAY_FORMAT_TEX_UV;
  2137. chunk_uv = tex_uv;
  2138. }
  2139. void RasterizerStorageGLES2::immediate_uv2(RID p_immediate, const Vector2 &tex_uv) {
  2140. Immediate *im = immediate_owner.get(p_immediate);
  2141. ERR_FAIL_COND(!im);
  2142. ERR_FAIL_COND(!im->building);
  2143. im->mask |= VS::ARRAY_FORMAT_TEX_UV2;
  2144. chunk_uv2 = tex_uv;
  2145. }
  2146. void RasterizerStorageGLES2::immediate_end(RID p_immediate) {
  2147. Immediate *im = immediate_owner.get(p_immediate);
  2148. ERR_FAIL_COND(!im);
  2149. ERR_FAIL_COND(!im->building);
  2150. im->building = false;
  2151. im->instance_change_notify(true, false);
  2152. }
  2153. void RasterizerStorageGLES2::immediate_clear(RID p_immediate) {
  2154. Immediate *im = immediate_owner.get(p_immediate);
  2155. ERR_FAIL_COND(!im);
  2156. ERR_FAIL_COND(im->building);
  2157. im->chunks.clear();
  2158. im->instance_change_notify(true, false);
  2159. }
  2160. AABB RasterizerStorageGLES2::immediate_get_aabb(RID p_immediate) const {
  2161. Immediate *im = immediate_owner.get(p_immediate);
  2162. ERR_FAIL_COND_V(!im, AABB());
  2163. return im->aabb;
  2164. }
  2165. void RasterizerStorageGLES2::immediate_set_material(RID p_immediate, RID p_material) {
  2166. Immediate *im = immediate_owner.get(p_immediate);
  2167. ERR_FAIL_COND(!im);
  2168. im->material = p_material;
  2169. im->instance_change_notify(false, true);
  2170. }
  2171. RID RasterizerStorageGLES2::immediate_get_material(RID p_immediate) const {
  2172. const Immediate *im = immediate_owner.get(p_immediate);
  2173. ERR_FAIL_COND_V(!im, RID());
  2174. return im->material;
  2175. }
  2176. /* SKELETON API */
  2177. RID RasterizerStorageGLES2::skeleton_create() {
  2178. Skeleton *skeleton = memnew(Skeleton);
  2179. return skeleton_owner.make_rid(skeleton);
  2180. }
  2181. void RasterizerStorageGLES2::skeleton_allocate(RID p_skeleton, int p_bones, bool p_2d_skeleton) {
  2182. Skeleton *skeleton = skeleton_owner.getornull(p_skeleton);
  2183. ERR_FAIL_COND(!skeleton);
  2184. ERR_FAIL_COND(p_bones < 0);
  2185. if (skeleton->size == p_bones && skeleton->use_2d == p_2d_skeleton) {
  2186. return;
  2187. }
  2188. skeleton->size = p_bones;
  2189. skeleton->use_2d = p_2d_skeleton;
  2190. // TODO use float texture for vertex shader
  2191. if (config.float_texture_supported) {
  2192. glGenTextures(1, &skeleton->tex_id);
  2193. glActiveTexture(GL_TEXTURE0);
  2194. glBindTexture(GL_TEXTURE_2D, skeleton->tex_id);
  2195. #ifdef GLES_OVER_GL
  2196. glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA32F, p_bones * (skeleton->use_2d ? 2 : 3), 1, 0, GL_RGBA, GL_FLOAT, NULL);
  2197. #else
  2198. glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, p_bones * (skeleton->use_2d ? 2 : 3), 1, 0, GL_RGBA, GL_FLOAT, NULL);
  2199. #endif
  2200. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  2201. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  2202. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  2203. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  2204. glBindTexture(GL_TEXTURE_2D, 0);
  2205. }
  2206. if (skeleton->use_2d) {
  2207. skeleton->bone_data.resize(p_bones * 4 * 2);
  2208. } else {
  2209. skeleton->bone_data.resize(p_bones * 4 * 3);
  2210. }
  2211. }
  2212. int RasterizerStorageGLES2::skeleton_get_bone_count(RID p_skeleton) const {
  2213. Skeleton *skeleton = skeleton_owner.getornull(p_skeleton);
  2214. ERR_FAIL_COND_V(!skeleton, 0);
  2215. return skeleton->size;
  2216. }
  2217. void RasterizerStorageGLES2::skeleton_bone_set_transform(RID p_skeleton, int p_bone, const Transform &p_transform) {
  2218. Skeleton *skeleton = skeleton_owner.getornull(p_skeleton);
  2219. ERR_FAIL_COND(!skeleton);
  2220. ERR_FAIL_INDEX(p_bone, skeleton->size);
  2221. ERR_FAIL_COND(skeleton->use_2d);
  2222. float *bone_data = skeleton->bone_data.ptrw();
  2223. int base_offset = p_bone * 4 * 3;
  2224. bone_data[base_offset + 0] = p_transform.basis[0].x;
  2225. bone_data[base_offset + 1] = p_transform.basis[0].y;
  2226. bone_data[base_offset + 2] = p_transform.basis[0].z;
  2227. bone_data[base_offset + 3] = p_transform.origin.x;
  2228. bone_data[base_offset + 4] = p_transform.basis[1].x;
  2229. bone_data[base_offset + 5] = p_transform.basis[1].y;
  2230. bone_data[base_offset + 6] = p_transform.basis[1].z;
  2231. bone_data[base_offset + 7] = p_transform.origin.y;
  2232. bone_data[base_offset + 8] = p_transform.basis[2].x;
  2233. bone_data[base_offset + 9] = p_transform.basis[2].y;
  2234. bone_data[base_offset + 10] = p_transform.basis[2].z;
  2235. bone_data[base_offset + 11] = p_transform.origin.z;
  2236. if (!skeleton->update_list.in_list()) {
  2237. skeleton_update_list.add(&skeleton->update_list);
  2238. }
  2239. }
  2240. Transform RasterizerStorageGLES2::skeleton_bone_get_transform(RID p_skeleton, int p_bone) const {
  2241. Skeleton *skeleton = skeleton_owner.getornull(p_skeleton);
  2242. ERR_FAIL_COND_V(!skeleton, Transform());
  2243. ERR_FAIL_INDEX_V(p_bone, skeleton->size, Transform());
  2244. ERR_FAIL_COND_V(skeleton->use_2d, Transform());
  2245. const float *bone_data = skeleton->bone_data.ptr();
  2246. Transform ret;
  2247. int base_offset = p_bone * 4 * 3;
  2248. ret.basis[0].x = bone_data[base_offset + 0];
  2249. ret.basis[0].y = bone_data[base_offset + 1];
  2250. ret.basis[0].z = bone_data[base_offset + 2];
  2251. ret.origin.x = bone_data[base_offset + 3];
  2252. ret.basis[1].x = bone_data[base_offset + 4];
  2253. ret.basis[1].y = bone_data[base_offset + 5];
  2254. ret.basis[1].z = bone_data[base_offset + 6];
  2255. ret.origin.y = bone_data[base_offset + 7];
  2256. ret.basis[2].x = bone_data[base_offset + 8];
  2257. ret.basis[2].y = bone_data[base_offset + 9];
  2258. ret.basis[2].z = bone_data[base_offset + 10];
  2259. ret.origin.z = bone_data[base_offset + 11];
  2260. return ret;
  2261. }
  2262. void RasterizerStorageGLES2::skeleton_bone_set_transform_2d(RID p_skeleton, int p_bone, const Transform2D &p_transform) {
  2263. Skeleton *skeleton = skeleton_owner.getornull(p_skeleton);
  2264. ERR_FAIL_COND(!skeleton);
  2265. ERR_FAIL_INDEX(p_bone, skeleton->size);
  2266. ERR_FAIL_COND(!skeleton->use_2d);
  2267. float *bone_data = skeleton->bone_data.ptrw();
  2268. int base_offset = p_bone * 4 * 2;
  2269. bone_data[base_offset + 0] = p_transform[0][0];
  2270. bone_data[base_offset + 1] = p_transform[1][0];
  2271. bone_data[base_offset + 2] = 0;
  2272. bone_data[base_offset + 3] = p_transform[2][0];
  2273. bone_data[base_offset + 4] = p_transform[0][1];
  2274. bone_data[base_offset + 5] = p_transform[1][1];
  2275. bone_data[base_offset + 6] = 0;
  2276. bone_data[base_offset + 7] = p_transform[2][1];
  2277. if (!skeleton->update_list.in_list()) {
  2278. skeleton_update_list.add(&skeleton->update_list);
  2279. }
  2280. }
  2281. Transform2D RasterizerStorageGLES2::skeleton_bone_get_transform_2d(RID p_skeleton, int p_bone) const {
  2282. Skeleton *skeleton = skeleton_owner.getornull(p_skeleton);
  2283. ERR_FAIL_COND_V(!skeleton, Transform2D());
  2284. ERR_FAIL_INDEX_V(p_bone, skeleton->size, Transform2D());
  2285. ERR_FAIL_COND_V(!skeleton->use_2d, Transform2D());
  2286. const float *bone_data = skeleton->bone_data.ptr();
  2287. Transform2D ret;
  2288. int base_offset = p_bone * 4 * 2;
  2289. ret[0][0] = bone_data[base_offset + 0];
  2290. ret[1][0] = bone_data[base_offset + 1];
  2291. ret[2][0] = bone_data[base_offset + 3];
  2292. ret[0][1] = bone_data[base_offset + 4];
  2293. ret[1][1] = bone_data[base_offset + 5];
  2294. ret[2][1] = bone_data[base_offset + 7];
  2295. return ret;
  2296. }
  2297. void RasterizerStorageGLES2::skeleton_set_base_transform_2d(RID p_skeleton, const Transform2D &p_base_transform) {
  2298. Skeleton *skeleton = skeleton_owner.getornull(p_skeleton);
  2299. ERR_FAIL_COND(!skeleton);
  2300. skeleton->base_transform_2d = p_base_transform;
  2301. }
  2302. void RasterizerStorageGLES2::_update_skeleton_transform_buffer(const PoolVector<float> &p_data, size_t p_size) {
  2303. glBindBuffer(GL_ARRAY_BUFFER, resources.skeleton_transform_buffer);
  2304. if (p_size > resources.skeleton_transform_buffer_size) {
  2305. // new requested buffer is bigger, so resizing the GPU buffer
  2306. resources.skeleton_transform_buffer_size = p_size;
  2307. glBufferData(GL_ARRAY_BUFFER, p_size * sizeof(float), p_data.read().ptr(), GL_DYNAMIC_DRAW);
  2308. } else {
  2309. glBufferSubData(GL_ARRAY_BUFFER, 0, p_size * sizeof(float), p_data.read().ptr());
  2310. }
  2311. glBindBuffer(GL_ARRAY_BUFFER, 0);
  2312. }
  2313. void RasterizerStorageGLES2::update_dirty_skeletons() {
  2314. if (!config.float_texture_supported)
  2315. return;
  2316. glActiveTexture(GL_TEXTURE0);
  2317. while (skeleton_update_list.first()) {
  2318. Skeleton *skeleton = skeleton_update_list.first()->self();
  2319. if (skeleton->size) {
  2320. glBindTexture(GL_TEXTURE_2D, skeleton->tex_id);
  2321. glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, skeleton->size * (skeleton->use_2d ? 2 : 3), 1, GL_RGBA, GL_FLOAT, skeleton->bone_data.ptr());
  2322. }
  2323. for (Set<RasterizerScene::InstanceBase *>::Element *E = skeleton->instances.front(); E; E = E->next()) {
  2324. E->get()->base_changed(true, false);
  2325. }
  2326. skeleton_update_list.remove(skeleton_update_list.first());
  2327. }
  2328. }
  2329. /* Light API */
  2330. RID RasterizerStorageGLES2::light_create(VS::LightType p_type) {
  2331. Light *light = memnew(Light);
  2332. light->type = p_type;
  2333. light->param[VS::LIGHT_PARAM_ENERGY] = 1.0;
  2334. light->param[VS::LIGHT_PARAM_INDIRECT_ENERGY] = 1.0;
  2335. light->param[VS::LIGHT_PARAM_SPECULAR] = 0.5;
  2336. light->param[VS::LIGHT_PARAM_RANGE] = 1.0;
  2337. light->param[VS::LIGHT_PARAM_SPOT_ANGLE] = 45;
  2338. light->param[VS::LIGHT_PARAM_CONTACT_SHADOW_SIZE] = 45;
  2339. light->param[VS::LIGHT_PARAM_SHADOW_MAX_DISTANCE] = 0;
  2340. light->param[VS::LIGHT_PARAM_SHADOW_SPLIT_1_OFFSET] = 0.1;
  2341. light->param[VS::LIGHT_PARAM_SHADOW_SPLIT_2_OFFSET] = 0.3;
  2342. light->param[VS::LIGHT_PARAM_SHADOW_SPLIT_3_OFFSET] = 0.6;
  2343. light->param[VS::LIGHT_PARAM_SHADOW_NORMAL_BIAS] = 0.1;
  2344. light->param[VS::LIGHT_PARAM_SHADOW_BIAS_SPLIT_SCALE] = 0.1;
  2345. light->color = Color(1, 1, 1, 1);
  2346. light->shadow = false;
  2347. light->negative = false;
  2348. light->cull_mask = 0xFFFFFFFF;
  2349. light->directional_shadow_mode = VS::LIGHT_DIRECTIONAL_SHADOW_ORTHOGONAL;
  2350. light->omni_shadow_mode = VS::LIGHT_OMNI_SHADOW_DUAL_PARABOLOID;
  2351. light->omni_shadow_detail = VS::LIGHT_OMNI_SHADOW_DETAIL_VERTICAL;
  2352. light->directional_blend_splits = false;
  2353. light->directional_range_mode = VS::LIGHT_DIRECTIONAL_SHADOW_DEPTH_RANGE_STABLE;
  2354. light->reverse_cull = false;
  2355. light->version = 0;
  2356. return light_owner.make_rid(light);
  2357. }
  2358. void RasterizerStorageGLES2::light_set_color(RID p_light, const Color &p_color) {
  2359. Light *light = light_owner.getornull(p_light);
  2360. ERR_FAIL_COND(!light);
  2361. light->color = p_color;
  2362. }
  2363. void RasterizerStorageGLES2::light_set_param(RID p_light, VS::LightParam p_param, float p_value) {
  2364. Light *light = light_owner.getornull(p_light);
  2365. ERR_FAIL_COND(!light);
  2366. ERR_FAIL_INDEX(p_param, VS::LIGHT_PARAM_MAX);
  2367. switch (p_param) {
  2368. case VS::LIGHT_PARAM_RANGE:
  2369. case VS::LIGHT_PARAM_SPOT_ANGLE:
  2370. case VS::LIGHT_PARAM_SHADOW_MAX_DISTANCE:
  2371. case VS::LIGHT_PARAM_SHADOW_SPLIT_1_OFFSET:
  2372. case VS::LIGHT_PARAM_SHADOW_SPLIT_2_OFFSET:
  2373. case VS::LIGHT_PARAM_SHADOW_SPLIT_3_OFFSET:
  2374. case VS::LIGHT_PARAM_SHADOW_NORMAL_BIAS:
  2375. case VS::LIGHT_PARAM_SHADOW_BIAS: {
  2376. light->version++;
  2377. light->instance_change_notify(true, false);
  2378. } break;
  2379. default: {}
  2380. }
  2381. light->param[p_param] = p_value;
  2382. }
  2383. void RasterizerStorageGLES2::light_set_shadow(RID p_light, bool p_enabled) {
  2384. Light *light = light_owner.getornull(p_light);
  2385. ERR_FAIL_COND(!light);
  2386. light->shadow = p_enabled;
  2387. light->version++;
  2388. light->instance_change_notify(true, false);
  2389. }
  2390. void RasterizerStorageGLES2::light_set_shadow_color(RID p_light, const Color &p_color) {
  2391. Light *light = light_owner.getornull(p_light);
  2392. ERR_FAIL_COND(!light);
  2393. light->shadow_color = p_color;
  2394. }
  2395. void RasterizerStorageGLES2::light_set_projector(RID p_light, RID p_texture) {
  2396. Light *light = light_owner.getornull(p_light);
  2397. ERR_FAIL_COND(!light);
  2398. light->projector = p_texture;
  2399. }
  2400. void RasterizerStorageGLES2::light_set_negative(RID p_light, bool p_enable) {
  2401. Light *light = light_owner.getornull(p_light);
  2402. ERR_FAIL_COND(!light);
  2403. light->negative = p_enable;
  2404. }
  2405. void RasterizerStorageGLES2::light_set_cull_mask(RID p_light, uint32_t p_mask) {
  2406. Light *light = light_owner.getornull(p_light);
  2407. ERR_FAIL_COND(!light);
  2408. light->cull_mask = p_mask;
  2409. light->version++;
  2410. light->instance_change_notify(true, false);
  2411. }
  2412. void RasterizerStorageGLES2::light_set_reverse_cull_face_mode(RID p_light, bool p_enabled) {
  2413. Light *light = light_owner.getornull(p_light);
  2414. ERR_FAIL_COND(!light);
  2415. light->reverse_cull = p_enabled;
  2416. light->version++;
  2417. light->instance_change_notify(true, false);
  2418. }
  2419. void RasterizerStorageGLES2::light_omni_set_shadow_mode(RID p_light, VS::LightOmniShadowMode p_mode) {
  2420. Light *light = light_owner.getornull(p_light);
  2421. ERR_FAIL_COND(!light);
  2422. light->omni_shadow_mode = p_mode;
  2423. light->version++;
  2424. light->instance_change_notify(true, false);
  2425. }
  2426. VS::LightOmniShadowMode RasterizerStorageGLES2::light_omni_get_shadow_mode(RID p_light) {
  2427. Light *light = light_owner.getornull(p_light);
  2428. ERR_FAIL_COND_V(!light, VS::LIGHT_OMNI_SHADOW_CUBE);
  2429. return light->omni_shadow_mode;
  2430. }
  2431. void RasterizerStorageGLES2::light_omni_set_shadow_detail(RID p_light, VS::LightOmniShadowDetail p_detail) {
  2432. Light *light = light_owner.getornull(p_light);
  2433. ERR_FAIL_COND(!light);
  2434. light->omni_shadow_detail = p_detail;
  2435. light->version++;
  2436. light->instance_change_notify(true, false);
  2437. }
  2438. void RasterizerStorageGLES2::light_directional_set_shadow_mode(RID p_light, VS::LightDirectionalShadowMode p_mode) {
  2439. Light *light = light_owner.getornull(p_light);
  2440. ERR_FAIL_COND(!light);
  2441. light->directional_shadow_mode = p_mode;
  2442. light->version++;
  2443. light->instance_change_notify(true, false);
  2444. }
  2445. void RasterizerStorageGLES2::light_directional_set_blend_splits(RID p_light, bool p_enable) {
  2446. Light *light = light_owner.getornull(p_light);
  2447. ERR_FAIL_COND(!light);
  2448. light->directional_blend_splits = p_enable;
  2449. light->version++;
  2450. light->instance_change_notify(true, false);
  2451. }
  2452. bool RasterizerStorageGLES2::light_directional_get_blend_splits(RID p_light) const {
  2453. Light *light = light_owner.getornull(p_light);
  2454. ERR_FAIL_COND_V(!light, false);
  2455. return light->directional_blend_splits;
  2456. }
  2457. VS::LightDirectionalShadowMode RasterizerStorageGLES2::light_directional_get_shadow_mode(RID p_light) {
  2458. Light *light = light_owner.getornull(p_light);
  2459. ERR_FAIL_COND_V(!light, VS::LIGHT_DIRECTIONAL_SHADOW_ORTHOGONAL);
  2460. return light->directional_shadow_mode;
  2461. }
  2462. void RasterizerStorageGLES2::light_directional_set_shadow_depth_range_mode(RID p_light, VS::LightDirectionalShadowDepthRangeMode p_range_mode) {
  2463. Light *light = light_owner.getornull(p_light);
  2464. ERR_FAIL_COND(!light);
  2465. light->directional_range_mode = p_range_mode;
  2466. }
  2467. VS::LightDirectionalShadowDepthRangeMode RasterizerStorageGLES2::light_directional_get_shadow_depth_range_mode(RID p_light) const {
  2468. Light *light = light_owner.getornull(p_light);
  2469. ERR_FAIL_COND_V(!light, VS::LIGHT_DIRECTIONAL_SHADOW_DEPTH_RANGE_STABLE);
  2470. return light->directional_range_mode;
  2471. }
  2472. VS::LightType RasterizerStorageGLES2::light_get_type(RID p_light) const {
  2473. Light *light = light_owner.getornull(p_light);
  2474. ERR_FAIL_COND_V(!light, VS::LIGHT_DIRECTIONAL);
  2475. return light->type;
  2476. }
  2477. float RasterizerStorageGLES2::light_get_param(RID p_light, VS::LightParam p_param) {
  2478. Light *light = light_owner.getornull(p_light);
  2479. ERR_FAIL_COND_V(!light, 0.0);
  2480. ERR_FAIL_INDEX_V(p_param, VS::LIGHT_PARAM_MAX, 0.0);
  2481. return light->param[p_param];
  2482. }
  2483. Color RasterizerStorageGLES2::light_get_color(RID p_light) {
  2484. Light *light = light_owner.getornull(p_light);
  2485. ERR_FAIL_COND_V(!light, Color());
  2486. return light->color;
  2487. }
  2488. bool RasterizerStorageGLES2::light_has_shadow(RID p_light) const {
  2489. Light *light = light_owner.getornull(p_light);
  2490. ERR_FAIL_COND_V(!light, false);
  2491. return light->shadow;
  2492. }
  2493. uint64_t RasterizerStorageGLES2::light_get_version(RID p_light) const {
  2494. Light *light = light_owner.getornull(p_light);
  2495. ERR_FAIL_COND_V(!light, 0);
  2496. return light->version;
  2497. }
  2498. AABB RasterizerStorageGLES2::light_get_aabb(RID p_light) const {
  2499. Light *light = light_owner.getornull(p_light);
  2500. ERR_FAIL_COND_V(!light, AABB());
  2501. switch (light->type) {
  2502. case VS::LIGHT_SPOT: {
  2503. float len = light->param[VS::LIGHT_PARAM_RANGE];
  2504. float size = Math::tan(Math::deg2rad(light->param[VS::LIGHT_PARAM_SPOT_ANGLE])) * len;
  2505. return AABB(Vector3(-size, -size, -len), Vector3(size * 2, size * 2, len));
  2506. } break;
  2507. case VS::LIGHT_OMNI: {
  2508. float r = light->param[VS::LIGHT_PARAM_RANGE];
  2509. return AABB(-Vector3(r, r, r), Vector3(r, r, r) * 2);
  2510. } break;
  2511. case VS::LIGHT_DIRECTIONAL: {
  2512. return AABB();
  2513. } break;
  2514. }
  2515. ERR_FAIL_V(AABB());
  2516. return AABB();
  2517. }
  2518. /* PROBE API */
  2519. RID RasterizerStorageGLES2::reflection_probe_create() {
  2520. ReflectionProbe *reflection_probe = memnew(ReflectionProbe);
  2521. reflection_probe->intensity = 1.0;
  2522. reflection_probe->interior_ambient = Color();
  2523. reflection_probe->interior_ambient_energy = 1.0;
  2524. reflection_probe->max_distance = 0;
  2525. reflection_probe->extents = Vector3(1, 1, 1);
  2526. reflection_probe->origin_offset = Vector3(0, 0, 0);
  2527. reflection_probe->interior = false;
  2528. reflection_probe->box_projection = false;
  2529. reflection_probe->enable_shadows = false;
  2530. reflection_probe->cull_mask = (1 << 20) - 1;
  2531. reflection_probe->update_mode = VS::REFLECTION_PROBE_UPDATE_ONCE;
  2532. reflection_probe->resolution = 128;
  2533. return reflection_probe_owner.make_rid(reflection_probe);
  2534. }
  2535. void RasterizerStorageGLES2::reflection_probe_set_update_mode(RID p_probe, VS::ReflectionProbeUpdateMode p_mode) {
  2536. ReflectionProbe *reflection_probe = reflection_probe_owner.getornull(p_probe);
  2537. ERR_FAIL_COND(!reflection_probe);
  2538. reflection_probe->update_mode = p_mode;
  2539. reflection_probe->instance_change_notify(true, false);
  2540. }
  2541. void RasterizerStorageGLES2::reflection_probe_set_intensity(RID p_probe, float p_intensity) {
  2542. ReflectionProbe *reflection_probe = reflection_probe_owner.getornull(p_probe);
  2543. ERR_FAIL_COND(!reflection_probe);
  2544. reflection_probe->intensity = p_intensity;
  2545. }
  2546. void RasterizerStorageGLES2::reflection_probe_set_interior_ambient(RID p_probe, const Color &p_ambient) {
  2547. ReflectionProbe *reflection_probe = reflection_probe_owner.getornull(p_probe);
  2548. ERR_FAIL_COND(!reflection_probe);
  2549. reflection_probe->interior_ambient = p_ambient;
  2550. }
  2551. void RasterizerStorageGLES2::reflection_probe_set_interior_ambient_energy(RID p_probe, float p_energy) {
  2552. ReflectionProbe *reflection_probe = reflection_probe_owner.getornull(p_probe);
  2553. ERR_FAIL_COND(!reflection_probe);
  2554. reflection_probe->interior_ambient_energy = p_energy;
  2555. }
  2556. void RasterizerStorageGLES2::reflection_probe_set_interior_ambient_probe_contribution(RID p_probe, float p_contrib) {
  2557. ReflectionProbe *reflection_probe = reflection_probe_owner.getornull(p_probe);
  2558. ERR_FAIL_COND(!reflection_probe);
  2559. reflection_probe->interior_ambient_probe_contrib = p_contrib;
  2560. }
  2561. void RasterizerStorageGLES2::reflection_probe_set_max_distance(RID p_probe, float p_distance) {
  2562. ReflectionProbe *reflection_probe = reflection_probe_owner.getornull(p_probe);
  2563. ERR_FAIL_COND(!reflection_probe);
  2564. reflection_probe->max_distance = p_distance;
  2565. reflection_probe->instance_change_notify(true, false);
  2566. }
  2567. void RasterizerStorageGLES2::reflection_probe_set_extents(RID p_probe, const Vector3 &p_extents) {
  2568. ReflectionProbe *reflection_probe = reflection_probe_owner.getornull(p_probe);
  2569. ERR_FAIL_COND(!reflection_probe);
  2570. reflection_probe->extents = p_extents;
  2571. reflection_probe->instance_change_notify(true, false);
  2572. }
  2573. void RasterizerStorageGLES2::reflection_probe_set_origin_offset(RID p_probe, const Vector3 &p_offset) {
  2574. ReflectionProbe *reflection_probe = reflection_probe_owner.getornull(p_probe);
  2575. ERR_FAIL_COND(!reflection_probe);
  2576. reflection_probe->origin_offset = p_offset;
  2577. reflection_probe->instance_change_notify(true, false);
  2578. }
  2579. void RasterizerStorageGLES2::reflection_probe_set_as_interior(RID p_probe, bool p_enable) {
  2580. ReflectionProbe *reflection_probe = reflection_probe_owner.getornull(p_probe);
  2581. ERR_FAIL_COND(!reflection_probe);
  2582. reflection_probe->interior = p_enable;
  2583. }
  2584. void RasterizerStorageGLES2::reflection_probe_set_enable_box_projection(RID p_probe, bool p_enable) {
  2585. ReflectionProbe *reflection_probe = reflection_probe_owner.getornull(p_probe);
  2586. ERR_FAIL_COND(!reflection_probe);
  2587. reflection_probe->box_projection = p_enable;
  2588. }
  2589. void RasterizerStorageGLES2::reflection_probe_set_enable_shadows(RID p_probe, bool p_enable) {
  2590. ReflectionProbe *reflection_probe = reflection_probe_owner.getornull(p_probe);
  2591. ERR_FAIL_COND(!reflection_probe);
  2592. reflection_probe->enable_shadows = p_enable;
  2593. reflection_probe->instance_change_notify(true, false);
  2594. }
  2595. void RasterizerStorageGLES2::reflection_probe_set_cull_mask(RID p_probe, uint32_t p_layers) {
  2596. ReflectionProbe *reflection_probe = reflection_probe_owner.getornull(p_probe);
  2597. ERR_FAIL_COND(!reflection_probe);
  2598. reflection_probe->cull_mask = p_layers;
  2599. reflection_probe->instance_change_notify(true, false);
  2600. }
  2601. void RasterizerStorageGLES2::reflection_probe_set_resolution(RID p_probe, int p_resolution) {
  2602. ReflectionProbe *reflection_probe = reflection_probe_owner.getornull(p_probe);
  2603. ERR_FAIL_COND(!reflection_probe);
  2604. reflection_probe->resolution = p_resolution;
  2605. }
  2606. AABB RasterizerStorageGLES2::reflection_probe_get_aabb(RID p_probe) const {
  2607. const ReflectionProbe *reflection_probe = reflection_probe_owner.getornull(p_probe);
  2608. ERR_FAIL_COND_V(!reflection_probe, AABB());
  2609. AABB aabb;
  2610. aabb.position = -reflection_probe->extents;
  2611. aabb.size = reflection_probe->extents * 2.0;
  2612. return aabb;
  2613. }
  2614. VS::ReflectionProbeUpdateMode RasterizerStorageGLES2::reflection_probe_get_update_mode(RID p_probe) const {
  2615. const ReflectionProbe *reflection_probe = reflection_probe_owner.getornull(p_probe);
  2616. ERR_FAIL_COND_V(!reflection_probe, VS::REFLECTION_PROBE_UPDATE_ALWAYS);
  2617. return reflection_probe->update_mode;
  2618. }
  2619. uint32_t RasterizerStorageGLES2::reflection_probe_get_cull_mask(RID p_probe) const {
  2620. const ReflectionProbe *reflection_probe = reflection_probe_owner.getornull(p_probe);
  2621. ERR_FAIL_COND_V(!reflection_probe, 0);
  2622. return reflection_probe->cull_mask;
  2623. }
  2624. Vector3 RasterizerStorageGLES2::reflection_probe_get_extents(RID p_probe) const {
  2625. const ReflectionProbe *reflection_probe = reflection_probe_owner.getornull(p_probe);
  2626. ERR_FAIL_COND_V(!reflection_probe, Vector3());
  2627. return reflection_probe->extents;
  2628. }
  2629. Vector3 RasterizerStorageGLES2::reflection_probe_get_origin_offset(RID p_probe) const {
  2630. const ReflectionProbe *reflection_probe = reflection_probe_owner.getornull(p_probe);
  2631. ERR_FAIL_COND_V(!reflection_probe, Vector3());
  2632. return reflection_probe->origin_offset;
  2633. }
  2634. bool RasterizerStorageGLES2::reflection_probe_renders_shadows(RID p_probe) const {
  2635. const ReflectionProbe *reflection_probe = reflection_probe_owner.getornull(p_probe);
  2636. ERR_FAIL_COND_V(!reflection_probe, false);
  2637. return reflection_probe->enable_shadows;
  2638. }
  2639. float RasterizerStorageGLES2::reflection_probe_get_origin_max_distance(RID p_probe) const {
  2640. const ReflectionProbe *reflection_probe = reflection_probe_owner.getornull(p_probe);
  2641. ERR_FAIL_COND_V(!reflection_probe, 0);
  2642. return reflection_probe->max_distance;
  2643. }
  2644. int RasterizerStorageGLES2::reflection_probe_get_resolution(RID p_probe) const {
  2645. const ReflectionProbe *reflection_probe = reflection_probe_owner.getornull(p_probe);
  2646. ERR_FAIL_COND_V(!reflection_probe, 0);
  2647. return reflection_probe->resolution;
  2648. }
  2649. RID RasterizerStorageGLES2::gi_probe_create() {
  2650. return RID();
  2651. }
  2652. void RasterizerStorageGLES2::gi_probe_set_bounds(RID p_probe, const AABB &p_bounds) {
  2653. }
  2654. AABB RasterizerStorageGLES2::gi_probe_get_bounds(RID p_probe) const {
  2655. return AABB();
  2656. }
  2657. void RasterizerStorageGLES2::gi_probe_set_cell_size(RID p_probe, float p_size) {
  2658. }
  2659. float RasterizerStorageGLES2::gi_probe_get_cell_size(RID p_probe) const {
  2660. return 0.0;
  2661. }
  2662. void RasterizerStorageGLES2::gi_probe_set_to_cell_xform(RID p_probe, const Transform &p_xform) {
  2663. }
  2664. Transform RasterizerStorageGLES2::gi_probe_get_to_cell_xform(RID p_probe) const {
  2665. return Transform();
  2666. }
  2667. void RasterizerStorageGLES2::gi_probe_set_dynamic_data(RID p_probe, const PoolVector<int> &p_data) {
  2668. }
  2669. PoolVector<int> RasterizerStorageGLES2::gi_probe_get_dynamic_data(RID p_probe) const {
  2670. return PoolVector<int>();
  2671. }
  2672. void RasterizerStorageGLES2::gi_probe_set_dynamic_range(RID p_probe, int p_range) {
  2673. }
  2674. int RasterizerStorageGLES2::gi_probe_get_dynamic_range(RID p_probe) const {
  2675. return 0;
  2676. }
  2677. void RasterizerStorageGLES2::gi_probe_set_energy(RID p_probe, float p_range) {
  2678. }
  2679. void RasterizerStorageGLES2::gi_probe_set_bias(RID p_probe, float p_range) {
  2680. }
  2681. void RasterizerStorageGLES2::gi_probe_set_normal_bias(RID p_probe, float p_range) {
  2682. }
  2683. void RasterizerStorageGLES2::gi_probe_set_propagation(RID p_probe, float p_range) {
  2684. }
  2685. void RasterizerStorageGLES2::gi_probe_set_interior(RID p_probe, bool p_enable) {
  2686. }
  2687. bool RasterizerStorageGLES2::gi_probe_is_interior(RID p_probe) const {
  2688. return false;
  2689. }
  2690. void RasterizerStorageGLES2::gi_probe_set_compress(RID p_probe, bool p_enable) {
  2691. }
  2692. bool RasterizerStorageGLES2::gi_probe_is_compressed(RID p_probe) const {
  2693. return false;
  2694. }
  2695. float RasterizerStorageGLES2::gi_probe_get_energy(RID p_probe) const {
  2696. return 0;
  2697. }
  2698. float RasterizerStorageGLES2::gi_probe_get_bias(RID p_probe) const {
  2699. return 0;
  2700. }
  2701. float RasterizerStorageGLES2::gi_probe_get_normal_bias(RID p_probe) const {
  2702. return 0;
  2703. }
  2704. float RasterizerStorageGLES2::gi_probe_get_propagation(RID p_probe) const {
  2705. return 0;
  2706. }
  2707. uint32_t RasterizerStorageGLES2::gi_probe_get_version(RID p_probe) {
  2708. return 0;
  2709. }
  2710. RasterizerStorage::GIProbeCompression RasterizerStorageGLES2::gi_probe_get_dynamic_data_get_preferred_compression() const {
  2711. return GI_PROBE_UNCOMPRESSED;
  2712. }
  2713. RID RasterizerStorageGLES2::gi_probe_dynamic_data_create(int p_width, int p_height, int p_depth, GIProbeCompression p_compression) {
  2714. return RID();
  2715. }
  2716. void RasterizerStorageGLES2::gi_probe_dynamic_data_update(RID p_gi_probe_data, int p_depth_slice, int p_slice_count, int p_mipmap, const void *p_data) {
  2717. }
  2718. ///////
  2719. RID RasterizerStorageGLES2::lightmap_capture_create() {
  2720. LightmapCapture *capture = memnew(LightmapCapture);
  2721. return lightmap_capture_data_owner.make_rid(capture);
  2722. }
  2723. void RasterizerStorageGLES2::lightmap_capture_set_bounds(RID p_capture, const AABB &p_bounds) {
  2724. LightmapCapture *capture = lightmap_capture_data_owner.getornull(p_capture);
  2725. ERR_FAIL_COND(!capture);
  2726. capture->bounds = p_bounds;
  2727. capture->instance_change_notify(true, false);
  2728. }
  2729. AABB RasterizerStorageGLES2::lightmap_capture_get_bounds(RID p_capture) const {
  2730. const LightmapCapture *capture = lightmap_capture_data_owner.getornull(p_capture);
  2731. ERR_FAIL_COND_V(!capture, AABB());
  2732. return capture->bounds;
  2733. }
  2734. void RasterizerStorageGLES2::lightmap_capture_set_octree(RID p_capture, const PoolVector<uint8_t> &p_octree) {
  2735. LightmapCapture *capture = lightmap_capture_data_owner.getornull(p_capture);
  2736. ERR_FAIL_COND(!capture);
  2737. ERR_FAIL_COND(p_octree.size() == 0 || (p_octree.size() % sizeof(LightmapCaptureOctree)) != 0);
  2738. capture->octree.resize(p_octree.size() / sizeof(LightmapCaptureOctree));
  2739. if (p_octree.size()) {
  2740. PoolVector<LightmapCaptureOctree>::Write w = capture->octree.write();
  2741. PoolVector<uint8_t>::Read r = p_octree.read();
  2742. copymem(w.ptr(), r.ptr(), p_octree.size());
  2743. }
  2744. capture->instance_change_notify(true, false);
  2745. }
  2746. PoolVector<uint8_t> RasterizerStorageGLES2::lightmap_capture_get_octree(RID p_capture) const {
  2747. const LightmapCapture *capture = lightmap_capture_data_owner.getornull(p_capture);
  2748. ERR_FAIL_COND_V(!capture, PoolVector<uint8_t>());
  2749. if (capture->octree.size() == 0)
  2750. return PoolVector<uint8_t>();
  2751. PoolVector<uint8_t> ret;
  2752. ret.resize(capture->octree.size() * sizeof(LightmapCaptureOctree));
  2753. {
  2754. PoolVector<LightmapCaptureOctree>::Read r = capture->octree.read();
  2755. PoolVector<uint8_t>::Write w = ret.write();
  2756. copymem(w.ptr(), r.ptr(), ret.size());
  2757. }
  2758. return ret;
  2759. }
  2760. void RasterizerStorageGLES2::lightmap_capture_set_octree_cell_transform(RID p_capture, const Transform &p_xform) {
  2761. LightmapCapture *capture = lightmap_capture_data_owner.getornull(p_capture);
  2762. ERR_FAIL_COND(!capture);
  2763. capture->cell_xform = p_xform;
  2764. }
  2765. Transform RasterizerStorageGLES2::lightmap_capture_get_octree_cell_transform(RID p_capture) const {
  2766. const LightmapCapture *capture = lightmap_capture_data_owner.getornull(p_capture);
  2767. ERR_FAIL_COND_V(!capture, Transform());
  2768. return capture->cell_xform;
  2769. }
  2770. void RasterizerStorageGLES2::lightmap_capture_set_octree_cell_subdiv(RID p_capture, int p_subdiv) {
  2771. LightmapCapture *capture = lightmap_capture_data_owner.getornull(p_capture);
  2772. ERR_FAIL_COND(!capture);
  2773. capture->cell_subdiv = p_subdiv;
  2774. }
  2775. int RasterizerStorageGLES2::lightmap_capture_get_octree_cell_subdiv(RID p_capture) const {
  2776. const LightmapCapture *capture = lightmap_capture_data_owner.getornull(p_capture);
  2777. ERR_FAIL_COND_V(!capture, 0);
  2778. return capture->cell_subdiv;
  2779. }
  2780. void RasterizerStorageGLES2::lightmap_capture_set_energy(RID p_capture, float p_energy) {
  2781. LightmapCapture *capture = lightmap_capture_data_owner.getornull(p_capture);
  2782. ERR_FAIL_COND(!capture);
  2783. capture->energy = p_energy;
  2784. }
  2785. float RasterizerStorageGLES2::lightmap_capture_get_energy(RID p_capture) const {
  2786. const LightmapCapture *capture = lightmap_capture_data_owner.getornull(p_capture);
  2787. ERR_FAIL_COND_V(!capture, 0);
  2788. return capture->energy;
  2789. }
  2790. const PoolVector<RasterizerStorage::LightmapCaptureOctree> *RasterizerStorageGLES2::lightmap_capture_get_octree_ptr(RID p_capture) const {
  2791. const LightmapCapture *capture = lightmap_capture_data_owner.getornull(p_capture);
  2792. ERR_FAIL_COND_V(!capture, NULL);
  2793. return &capture->octree;
  2794. }
  2795. ///////
  2796. RID RasterizerStorageGLES2::particles_create() {
  2797. return RID();
  2798. }
  2799. void RasterizerStorageGLES2::particles_set_emitting(RID p_particles, bool p_emitting) {
  2800. }
  2801. bool RasterizerStorageGLES2::particles_get_emitting(RID p_particles) {
  2802. return false;
  2803. }
  2804. void RasterizerStorageGLES2::particles_set_amount(RID p_particles, int p_amount) {
  2805. }
  2806. void RasterizerStorageGLES2::particles_set_lifetime(RID p_particles, float p_lifetime) {
  2807. }
  2808. void RasterizerStorageGLES2::particles_set_one_shot(RID p_particles, bool p_one_shot) {
  2809. }
  2810. void RasterizerStorageGLES2::particles_set_pre_process_time(RID p_particles, float p_time) {
  2811. }
  2812. void RasterizerStorageGLES2::particles_set_explosiveness_ratio(RID p_particles, float p_ratio) {
  2813. }
  2814. void RasterizerStorageGLES2::particles_set_randomness_ratio(RID p_particles, float p_ratio) {
  2815. }
  2816. void RasterizerStorageGLES2::particles_set_custom_aabb(RID p_particles, const AABB &p_aabb) {
  2817. }
  2818. void RasterizerStorageGLES2::particles_set_speed_scale(RID p_particles, float p_scale) {
  2819. }
  2820. void RasterizerStorageGLES2::particles_set_use_local_coordinates(RID p_particles, bool p_enable) {
  2821. }
  2822. void RasterizerStorageGLES2::particles_set_fixed_fps(RID p_particles, int p_fps) {
  2823. }
  2824. void RasterizerStorageGLES2::particles_set_fractional_delta(RID p_particles, bool p_enable) {
  2825. }
  2826. void RasterizerStorageGLES2::particles_set_process_material(RID p_particles, RID p_material) {
  2827. }
  2828. void RasterizerStorageGLES2::particles_set_draw_order(RID p_particles, VS::ParticlesDrawOrder p_order) {
  2829. }
  2830. void RasterizerStorageGLES2::particles_set_draw_passes(RID p_particles, int p_passes) {
  2831. }
  2832. void RasterizerStorageGLES2::particles_set_draw_pass_mesh(RID p_particles, int p_pass, RID p_mesh) {
  2833. }
  2834. void RasterizerStorageGLES2::particles_restart(RID p_particles) {
  2835. }
  2836. void RasterizerStorageGLES2::particles_request_process(RID p_particles) {
  2837. }
  2838. AABB RasterizerStorageGLES2::particles_get_current_aabb(RID p_particles) {
  2839. return AABB();
  2840. }
  2841. AABB RasterizerStorageGLES2::particles_get_aabb(RID p_particles) const {
  2842. return AABB();
  2843. }
  2844. void RasterizerStorageGLES2::particles_set_emission_transform(RID p_particles, const Transform &p_transform) {
  2845. }
  2846. int RasterizerStorageGLES2::particles_get_draw_passes(RID p_particles) const {
  2847. return 0;
  2848. }
  2849. RID RasterizerStorageGLES2::particles_get_draw_pass_mesh(RID p_particles, int p_pass) const {
  2850. return RID();
  2851. }
  2852. void RasterizerStorageGLES2::update_particles() {
  2853. }
  2854. bool RasterizerStorageGLES2::particles_is_inactive(RID p_particles) const {
  2855. return true;
  2856. }
  2857. ////////
  2858. void RasterizerStorageGLES2::instance_add_skeleton(RID p_skeleton, RasterizerScene::InstanceBase *p_instance) {
  2859. Skeleton *skeleton = skeleton_owner.getornull(p_skeleton);
  2860. ERR_FAIL_COND(!skeleton);
  2861. skeleton->instances.insert(p_instance);
  2862. }
  2863. void RasterizerStorageGLES2::instance_remove_skeleton(RID p_skeleton, RasterizerScene::InstanceBase *p_instance) {
  2864. Skeleton *skeleton = skeleton_owner.getornull(p_skeleton);
  2865. ERR_FAIL_COND(!skeleton);
  2866. skeleton->instances.erase(p_instance);
  2867. }
  2868. void RasterizerStorageGLES2::instance_add_dependency(RID p_base, RasterizerScene::InstanceBase *p_instance) {
  2869. Instantiable *inst = NULL;
  2870. switch (p_instance->base_type) {
  2871. case VS::INSTANCE_MESH: {
  2872. inst = mesh_owner.getornull(p_base);
  2873. ERR_FAIL_COND(!inst);
  2874. } break;
  2875. case VS::INSTANCE_MULTIMESH: {
  2876. inst = multimesh_owner.getornull(p_base);
  2877. ERR_FAIL_COND(!inst);
  2878. } break;
  2879. case VS::INSTANCE_IMMEDIATE: {
  2880. inst = immediate_owner.getornull(p_base);
  2881. ERR_FAIL_COND(!inst);
  2882. } break;
  2883. /*case VS::INSTANCE_PARTICLES: {
  2884. inst = particles_owner.getornull(p_base);
  2885. ERR_FAIL_COND(!inst);
  2886. } break;*/
  2887. case VS::INSTANCE_REFLECTION_PROBE: {
  2888. inst = reflection_probe_owner.getornull(p_base);
  2889. ERR_FAIL_COND(!inst);
  2890. } break;
  2891. case VS::INSTANCE_LIGHT: {
  2892. inst = light_owner.getornull(p_base);
  2893. ERR_FAIL_COND(!inst);
  2894. } break;
  2895. /*case VS::INSTANCE_GI_PROBE: {
  2896. inst = gi_probe_owner.getornull(p_base);
  2897. ERR_FAIL_COND(!inst);
  2898. } break;*/
  2899. case VS::INSTANCE_LIGHTMAP_CAPTURE: {
  2900. inst = lightmap_capture_data_owner.getornull(p_base);
  2901. ERR_FAIL_COND(!inst);
  2902. } break;
  2903. default: {
  2904. if (!inst) {
  2905. ERR_FAIL();
  2906. }
  2907. }
  2908. }
  2909. inst->instance_list.add(&p_instance->dependency_item);
  2910. }
  2911. void RasterizerStorageGLES2::instance_remove_dependency(RID p_base, RasterizerScene::InstanceBase *p_instance) {
  2912. Instantiable *inst = NULL;
  2913. switch (p_instance->base_type) {
  2914. case VS::INSTANCE_MESH: {
  2915. inst = mesh_owner.getornull(p_base);
  2916. ERR_FAIL_COND(!inst);
  2917. } break;
  2918. case VS::INSTANCE_MULTIMESH: {
  2919. inst = multimesh_owner.getornull(p_base);
  2920. ERR_FAIL_COND(!inst);
  2921. } break;
  2922. case VS::INSTANCE_IMMEDIATE: {
  2923. inst = immediate_owner.getornull(p_base);
  2924. ERR_FAIL_COND(!inst);
  2925. } break;
  2926. /*case VS::INSTANCE_PARTICLES: {
  2927. inst = particles_owner.getornull(p_base);
  2928. ERR_FAIL_COND(!inst);
  2929. } break;*/
  2930. case VS::INSTANCE_REFLECTION_PROBE: {
  2931. inst = reflection_probe_owner.getornull(p_base);
  2932. ERR_FAIL_COND(!inst);
  2933. } break;
  2934. case VS::INSTANCE_LIGHT: {
  2935. inst = light_owner.getornull(p_base);
  2936. ERR_FAIL_COND(!inst);
  2937. } break;
  2938. /*case VS::INSTANCE_GI_PROBE: {
  2939. inst = gi_probe_owner.getornull(p_base);
  2940. ERR_FAIL_COND(!inst);
  2941. } break; */
  2942. case VS::INSTANCE_LIGHTMAP_CAPTURE: {
  2943. inst = lightmap_capture_data_owner.getornull(p_base);
  2944. ERR_FAIL_COND(!inst);
  2945. } break;
  2946. default: {
  2947. if (!inst) {
  2948. ERR_FAIL();
  2949. }
  2950. }
  2951. }
  2952. ERR_FAIL_COND(!inst);
  2953. inst->instance_list.remove(&p_instance->dependency_item);
  2954. }
  2955. /* RENDER TARGET */
  2956. void RasterizerStorageGLES2::_render_target_allocate(RenderTarget *rt) {
  2957. if (rt->width <= 0 || rt->height <= 0)
  2958. return;
  2959. Texture *texture = texture_owner.getornull(rt->texture);
  2960. ERR_FAIL_COND(!texture);
  2961. // create fbo
  2962. glGenFramebuffers(1, &rt->fbo);
  2963. glBindFramebuffer(GL_FRAMEBUFFER, rt->fbo);
  2964. // color
  2965. glGenTextures(1, &rt->color);
  2966. glBindTexture(GL_TEXTURE_2D, rt->color);
  2967. glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, rt->width, rt->height, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
  2968. if (texture->flags & VS::TEXTURE_FLAG_FILTER) {
  2969. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  2970. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  2971. } else {
  2972. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  2973. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  2974. }
  2975. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  2976. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  2977. glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, rt->color, 0);
  2978. // depth
  2979. glGenRenderbuffers(1, &rt->depth);
  2980. glBindRenderbuffer(GL_RENDERBUFFER, rt->depth);
  2981. glRenderbufferStorage(GL_RENDERBUFFER, _DEPTH_COMPONENT24_OES, rt->width, rt->height);
  2982. glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, rt->depth);
  2983. GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
  2984. if (status != GL_FRAMEBUFFER_COMPLETE) {
  2985. glDeleteRenderbuffers(1, &rt->fbo);
  2986. glDeleteTextures(1, &rt->depth);
  2987. glDeleteTextures(1, &rt->color);
  2988. rt->fbo = 0;
  2989. rt->width = 0;
  2990. rt->height = 0;
  2991. rt->color = 0;
  2992. rt->depth = 0;
  2993. texture->tex_id = 0;
  2994. texture->active = false;
  2995. WARN_PRINT("Could not create framebuffer!!");
  2996. return;
  2997. }
  2998. texture->format = Image::FORMAT_RGBA8;
  2999. texture->gl_format_cache = GL_RGBA;
  3000. texture->gl_type_cache = GL_UNSIGNED_BYTE;
  3001. texture->gl_internal_format_cache = GL_RGBA;
  3002. texture->tex_id = rt->color;
  3003. texture->width = rt->width;
  3004. texture->alloc_width = rt->width;
  3005. texture->height = rt->height;
  3006. texture->alloc_height = rt->height;
  3007. texture->active = true;
  3008. texture_set_flags(rt->texture, texture->flags);
  3009. glClearColor(0, 0, 0, 0);
  3010. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  3011. // copy texscreen buffers
  3012. {
  3013. int w = rt->width;
  3014. int h = rt->height;
  3015. glGenTextures(1, &rt->copy_screen_effect.color);
  3016. glBindTexture(GL_TEXTURE_2D, rt->copy_screen_effect.color);
  3017. glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
  3018. glGenFramebuffers(1, &rt->copy_screen_effect.fbo);
  3019. glBindFramebuffer(GL_FRAMEBUFFER, rt->copy_screen_effect.fbo);
  3020. glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, rt->color, 0);
  3021. GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
  3022. if (status != GL_FRAMEBUFFER_COMPLETE) {
  3023. _render_target_clear(rt);
  3024. ERR_FAIL_COND(status != GL_FRAMEBUFFER_COMPLETE);
  3025. }
  3026. }
  3027. glBindFramebuffer(GL_FRAMEBUFFER, RasterizerStorageGLES2::system_fbo);
  3028. }
  3029. void RasterizerStorageGLES2::_render_target_clear(RenderTarget *rt) {
  3030. if (rt->fbo) {
  3031. glDeleteFramebuffers(1, &rt->fbo);
  3032. glDeleteTextures(1, &rt->color);
  3033. rt->fbo = 0;
  3034. }
  3035. if (rt->depth) {
  3036. glDeleteRenderbuffers(1, &rt->depth);
  3037. rt->depth = 0;
  3038. }
  3039. Texture *tex = texture_owner.get(rt->texture);
  3040. tex->alloc_height = 0;
  3041. tex->alloc_width = 0;
  3042. tex->width = 0;
  3043. tex->height = 0;
  3044. tex->active = false;
  3045. // TODO hardcoded texscreen copy effect
  3046. if (rt->copy_screen_effect.color) {
  3047. glDeleteFramebuffers(1, &rt->copy_screen_effect.fbo);
  3048. rt->copy_screen_effect.fbo = 0;
  3049. glDeleteTextures(1, &rt->copy_screen_effect.color);
  3050. rt->copy_screen_effect.color = 0;
  3051. }
  3052. }
  3053. RID RasterizerStorageGLES2::render_target_create() {
  3054. RenderTarget *rt = memnew(RenderTarget);
  3055. Texture *t = memnew(Texture);
  3056. t->type = VS::TEXTURE_TYPE_2D;
  3057. t->flags = 0;
  3058. t->width = 0;
  3059. t->height = 0;
  3060. t->alloc_height = 0;
  3061. t->alloc_width = 0;
  3062. t->format = Image::FORMAT_R8;
  3063. t->target = GL_TEXTURE_2D;
  3064. t->gl_format_cache = 0;
  3065. t->gl_internal_format_cache = 0;
  3066. t->gl_type_cache = 0;
  3067. t->data_size = 0;
  3068. t->total_data_size = 0;
  3069. t->ignore_mipmaps = false;
  3070. t->compressed = false;
  3071. t->mipmaps = 1;
  3072. t->active = true;
  3073. t->tex_id = 0;
  3074. t->render_target = rt;
  3075. rt->texture = texture_owner.make_rid(t);
  3076. return render_target_owner.make_rid(rt);
  3077. }
  3078. void RasterizerStorageGLES2::render_target_set_size(RID p_render_target, int p_width, int p_height) {
  3079. RenderTarget *rt = render_target_owner.getornull(p_render_target);
  3080. ERR_FAIL_COND(!rt);
  3081. if (p_width == rt->width && p_height == rt->height)
  3082. return;
  3083. _render_target_clear(rt);
  3084. rt->width = p_width;
  3085. rt->height = p_height;
  3086. _render_target_allocate(rt);
  3087. }
  3088. RID RasterizerStorageGLES2::render_target_get_texture(RID p_render_target) const {
  3089. RenderTarget *rt = render_target_owner.getornull(p_render_target);
  3090. ERR_FAIL_COND_V(!rt, RID());
  3091. return rt->texture;
  3092. }
  3093. void RasterizerStorageGLES2::render_target_set_flag(RID p_render_target, RenderTargetFlags p_flag, bool p_value) {
  3094. RenderTarget *rt = render_target_owner.getornull(p_render_target);
  3095. ERR_FAIL_COND(!rt);
  3096. rt->flags[p_flag] = p_value;
  3097. switch (p_flag) {
  3098. case RENDER_TARGET_HDR:
  3099. case RENDER_TARGET_NO_3D:
  3100. case RENDER_TARGET_NO_SAMPLING:
  3101. case RENDER_TARGET_NO_3D_EFFECTS: {
  3102. //must reset for these formats
  3103. _render_target_clear(rt);
  3104. _render_target_allocate(rt);
  3105. } break;
  3106. default: {}
  3107. }
  3108. }
  3109. bool RasterizerStorageGLES2::render_target_was_used(RID p_render_target) {
  3110. RenderTarget *rt = render_target_owner.getornull(p_render_target);
  3111. ERR_FAIL_COND_V(!rt, false);
  3112. return rt->used_in_frame;
  3113. }
  3114. void RasterizerStorageGLES2::render_target_clear_used(RID p_render_target) {
  3115. RenderTarget *rt = render_target_owner.getornull(p_render_target);
  3116. ERR_FAIL_COND(!rt);
  3117. rt->used_in_frame = false;
  3118. }
  3119. void RasterizerStorageGLES2::render_target_set_msaa(RID p_render_target, VS::ViewportMSAA p_msaa) {
  3120. RenderTarget *rt = render_target_owner.getornull(p_render_target);
  3121. ERR_FAIL_COND(!rt);
  3122. if (rt->msaa == p_msaa)
  3123. return;
  3124. _render_target_clear(rt);
  3125. rt->msaa = p_msaa;
  3126. _render_target_allocate(rt);
  3127. }
  3128. /* CANVAS SHADOW */
  3129. RID RasterizerStorageGLES2::canvas_light_shadow_buffer_create(int p_width) {
  3130. CanvasLightShadow *cls = memnew(CanvasLightShadow);
  3131. if (p_width > config.max_texture_size)
  3132. p_width = config.max_texture_size;
  3133. cls->size = p_width;
  3134. cls->height = 16;
  3135. glActiveTexture(GL_TEXTURE0);
  3136. glGenFramebuffers(1, &cls->fbo);
  3137. glBindFramebuffer(GL_FRAMEBUFFER, cls->fbo);
  3138. glGenRenderbuffers(1, &cls->depth);
  3139. glBindRenderbuffer(GL_RENDERBUFFER, cls->depth);
  3140. glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT, cls->size, cls->height);
  3141. glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, cls->depth);
  3142. glBindRenderbuffer(GL_RENDERBUFFER, 0);
  3143. glGenTextures(1, &cls->distance);
  3144. glBindTexture(GL_TEXTURE_2D, cls->distance);
  3145. if (config.use_rgba_2d_shadows) {
  3146. glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, cls->size, cls->height, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
  3147. } else {
  3148. #ifdef GLES_OVER_GL
  3149. glTexImage2D(GL_TEXTURE_2D, 0, GL_R32F, cls->size, cls->height, 0, _RED_OES, GL_FLOAT, NULL);
  3150. #else
  3151. glTexImage2D(GL_TEXTURE_2D, 0, GL_FLOAT, cls->size, cls->height, 0, _RED_OES, GL_FLOAT, NULL);
  3152. #endif
  3153. }
  3154. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  3155. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  3156. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  3157. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  3158. glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, cls->distance, 0);
  3159. GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
  3160. //printf("errnum: %x\n",status);
  3161. glBindFramebuffer(GL_FRAMEBUFFER, RasterizerStorageGLES2::system_fbo);
  3162. if (status != GL_FRAMEBUFFER_COMPLETE) {
  3163. memdelete(cls);
  3164. ERR_FAIL_COND_V(status != GL_FRAMEBUFFER_COMPLETE, RID());
  3165. }
  3166. return canvas_light_shadow_owner.make_rid(cls);
  3167. }
  3168. /* LIGHT SHADOW MAPPING */
  3169. RID RasterizerStorageGLES2::canvas_light_occluder_create() {
  3170. CanvasOccluder *co = memnew(CanvasOccluder);
  3171. co->index_id = 0;
  3172. co->vertex_id = 0;
  3173. co->len = 0;
  3174. return canvas_occluder_owner.make_rid(co);
  3175. }
  3176. void RasterizerStorageGLES2::canvas_light_occluder_set_polylines(RID p_occluder, const PoolVector<Vector2> &p_lines) {
  3177. CanvasOccluder *co = canvas_occluder_owner.get(p_occluder);
  3178. ERR_FAIL_COND(!co);
  3179. co->lines = p_lines;
  3180. if (p_lines.size() != co->len) {
  3181. if (co->index_id)
  3182. glDeleteBuffers(1, &co->index_id);
  3183. if (co->vertex_id)
  3184. glDeleteBuffers(1, &co->vertex_id);
  3185. co->index_id = 0;
  3186. co->vertex_id = 0;
  3187. co->len = 0;
  3188. }
  3189. if (p_lines.size()) {
  3190. PoolVector<float> geometry;
  3191. PoolVector<uint16_t> indices;
  3192. int lc = p_lines.size();
  3193. geometry.resize(lc * 6);
  3194. indices.resize(lc * 3);
  3195. PoolVector<float>::Write vw = geometry.write();
  3196. PoolVector<uint16_t>::Write iw = indices.write();
  3197. PoolVector<Vector2>::Read lr = p_lines.read();
  3198. const int POLY_HEIGHT = 16384;
  3199. for (int i = 0; i < lc / 2; i++) {
  3200. vw[i * 12 + 0] = lr[i * 2 + 0].x;
  3201. vw[i * 12 + 1] = lr[i * 2 + 0].y;
  3202. vw[i * 12 + 2] = POLY_HEIGHT;
  3203. vw[i * 12 + 3] = lr[i * 2 + 1].x;
  3204. vw[i * 12 + 4] = lr[i * 2 + 1].y;
  3205. vw[i * 12 + 5] = POLY_HEIGHT;
  3206. vw[i * 12 + 6] = lr[i * 2 + 1].x;
  3207. vw[i * 12 + 7] = lr[i * 2 + 1].y;
  3208. vw[i * 12 + 8] = -POLY_HEIGHT;
  3209. vw[i * 12 + 9] = lr[i * 2 + 0].x;
  3210. vw[i * 12 + 10] = lr[i * 2 + 0].y;
  3211. vw[i * 12 + 11] = -POLY_HEIGHT;
  3212. iw[i * 6 + 0] = i * 4 + 0;
  3213. iw[i * 6 + 1] = i * 4 + 1;
  3214. iw[i * 6 + 2] = i * 4 + 2;
  3215. iw[i * 6 + 3] = i * 4 + 2;
  3216. iw[i * 6 + 4] = i * 4 + 3;
  3217. iw[i * 6 + 5] = i * 4 + 0;
  3218. }
  3219. //if same buffer len is being set, just use BufferSubData to avoid a pipeline flush
  3220. if (!co->vertex_id) {
  3221. glGenBuffers(1, &co->vertex_id);
  3222. glBindBuffer(GL_ARRAY_BUFFER, co->vertex_id);
  3223. glBufferData(GL_ARRAY_BUFFER, lc * 6 * sizeof(real_t), vw.ptr(), GL_STATIC_DRAW);
  3224. } else {
  3225. glBindBuffer(GL_ARRAY_BUFFER, co->vertex_id);
  3226. glBufferSubData(GL_ARRAY_BUFFER, 0, lc * 6 * sizeof(real_t), vw.ptr());
  3227. }
  3228. glBindBuffer(GL_ARRAY_BUFFER, 0); //unbind
  3229. if (!co->index_id) {
  3230. glGenBuffers(1, &co->index_id);
  3231. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, co->index_id);
  3232. glBufferData(GL_ELEMENT_ARRAY_BUFFER, lc * 3 * sizeof(uint16_t), iw.ptr(), GL_DYNAMIC_DRAW);
  3233. } else {
  3234. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, co->index_id);
  3235. glBufferSubData(GL_ELEMENT_ARRAY_BUFFER, 0, lc * 3 * sizeof(uint16_t), iw.ptr());
  3236. }
  3237. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); //unbind
  3238. co->len = lc;
  3239. }
  3240. }
  3241. VS::InstanceType RasterizerStorageGLES2::get_base_type(RID p_rid) const {
  3242. if (mesh_owner.owns(p_rid)) {
  3243. return VS::INSTANCE_MESH;
  3244. } else if (light_owner.owns(p_rid)) {
  3245. return VS::INSTANCE_LIGHT;
  3246. } else if (multimesh_owner.owns(p_rid)) {
  3247. return VS::INSTANCE_MULTIMESH;
  3248. } else if (immediate_owner.owns(p_rid)) {
  3249. return VS::INSTANCE_IMMEDIATE;
  3250. } else if (reflection_probe_owner.owns(p_rid)) {
  3251. return VS::INSTANCE_REFLECTION_PROBE;
  3252. } else if (lightmap_capture_data_owner.owns(p_rid)) {
  3253. return VS::INSTANCE_LIGHTMAP_CAPTURE;
  3254. } else {
  3255. return VS::INSTANCE_NONE;
  3256. }
  3257. }
  3258. bool RasterizerStorageGLES2::free(RID p_rid) {
  3259. if (render_target_owner.owns(p_rid)) {
  3260. RenderTarget *rt = render_target_owner.getornull(p_rid);
  3261. _render_target_clear(rt);
  3262. Texture *t = texture_owner.get(rt->texture);
  3263. texture_owner.free(rt->texture);
  3264. memdelete(t);
  3265. render_target_owner.free(p_rid);
  3266. memdelete(rt);
  3267. return true;
  3268. } else if (texture_owner.owns(p_rid)) {
  3269. Texture *t = texture_owner.get(p_rid);
  3270. // can't free a render target texture
  3271. ERR_FAIL_COND_V(t->render_target, true);
  3272. info.texture_mem -= t->total_data_size;
  3273. texture_owner.free(p_rid);
  3274. memdelete(t);
  3275. return true;
  3276. } else if (sky_owner.owns(p_rid)) {
  3277. Sky *sky = sky_owner.get(p_rid);
  3278. sky_set_texture(p_rid, RID(), 256);
  3279. sky_owner.free(p_rid);
  3280. memdelete(sky);
  3281. return true;
  3282. } else if (shader_owner.owns(p_rid)) {
  3283. Shader *shader = shader_owner.get(p_rid);
  3284. if (shader->shader) {
  3285. shader->shader->free_custom_shader(shader->custom_code_id);
  3286. }
  3287. if (shader->dirty_list.in_list()) {
  3288. _shader_dirty_list.remove(&shader->dirty_list);
  3289. }
  3290. while (shader->materials.first()) {
  3291. Material *m = shader->materials.first()->self();
  3292. m->shader = NULL;
  3293. _material_make_dirty(m);
  3294. shader->materials.remove(shader->materials.first());
  3295. }
  3296. shader_owner.free(p_rid);
  3297. memdelete(shader);
  3298. return true;
  3299. } else if (material_owner.owns(p_rid)) {
  3300. Material *m = material_owner.get(p_rid);
  3301. if (m->shader) {
  3302. m->shader->materials.remove(&m->list);
  3303. }
  3304. for (Map<Geometry *, int>::Element *E = m->geometry_owners.front(); E; E = E->next()) {
  3305. Geometry *g = E->key();
  3306. g->material = RID();
  3307. }
  3308. for (Map<RasterizerScene::InstanceBase *, int>::Element *E = m->instance_owners.front(); E; E = E->next()) {
  3309. RasterizerScene::InstanceBase *ins = E->key();
  3310. if (ins->material_override == p_rid) {
  3311. ins->material_override = RID();
  3312. }
  3313. for (int i = 0; i < ins->materials.size(); i++) {
  3314. if (ins->materials[i] == p_rid) {
  3315. ins->materials.write[i] = RID();
  3316. }
  3317. }
  3318. }
  3319. material_owner.free(p_rid);
  3320. memdelete(m);
  3321. return true;
  3322. } else if (skeleton_owner.owns(p_rid)) {
  3323. Skeleton *s = skeleton_owner.get(p_rid);
  3324. if (s->update_list.in_list()) {
  3325. skeleton_update_list.remove(&s->update_list);
  3326. }
  3327. for (Set<RasterizerScene::InstanceBase *>::Element *E = s->instances.front(); E; E = E->next()) {
  3328. E->get()->skeleton = RID();
  3329. }
  3330. skeleton_allocate(p_rid, 0, false);
  3331. if (s->tex_id) {
  3332. glDeleteTextures(1, &s->tex_id);
  3333. }
  3334. skeleton_owner.free(p_rid);
  3335. memdelete(s);
  3336. return true;
  3337. } else if (mesh_owner.owns(p_rid)) {
  3338. Mesh *mesh = mesh_owner.get(p_rid);
  3339. mesh->instance_remove_deps();
  3340. mesh_clear(p_rid);
  3341. while (mesh->multimeshes.first()) {
  3342. MultiMesh *multimesh = mesh->multimeshes.first()->self();
  3343. multimesh->mesh = RID();
  3344. multimesh->dirty_aabb = true;
  3345. mesh->multimeshes.remove(mesh->multimeshes.first());
  3346. if (!multimesh->update_list.in_list()) {
  3347. multimesh_update_list.add(&multimesh->update_list);
  3348. }
  3349. }
  3350. mesh_owner.free(p_rid);
  3351. memdelete(mesh);
  3352. return true;
  3353. } else if (multimesh_owner.owns(p_rid)) {
  3354. MultiMesh *multimesh = multimesh_owner.get(p_rid);
  3355. multimesh->instance_remove_deps();
  3356. if (multimesh->mesh.is_valid()) {
  3357. Mesh *mesh = mesh_owner.getornull(multimesh->mesh);
  3358. if (mesh) {
  3359. mesh->multimeshes.remove(&multimesh->mesh_list);
  3360. }
  3361. }
  3362. multimesh_allocate(p_rid, 0, VS::MULTIMESH_TRANSFORM_3D, VS::MULTIMESH_COLOR_NONE);
  3363. update_dirty_multimeshes();
  3364. multimesh_owner.free(p_rid);
  3365. memdelete(multimesh);
  3366. return true;
  3367. } else if (immediate_owner.owns(p_rid)) {
  3368. Immediate *im = immediate_owner.get(p_rid);
  3369. im->instance_remove_deps();
  3370. immediate_owner.free(p_rid);
  3371. memdelete(im);
  3372. return true;
  3373. } else if (light_owner.owns(p_rid)) {
  3374. Light *light = light_owner.get(p_rid);
  3375. light->instance_remove_deps();
  3376. light_owner.free(p_rid);
  3377. memdelete(light);
  3378. return true;
  3379. } else if (reflection_probe_owner.owns(p_rid)) {
  3380. // delete the texture
  3381. ReflectionProbe *reflection_probe = reflection_probe_owner.get(p_rid);
  3382. reflection_probe->instance_remove_deps();
  3383. reflection_probe_owner.free(p_rid);
  3384. memdelete(reflection_probe);
  3385. return true;
  3386. } else if (lightmap_capture_data_owner.owns(p_rid)) {
  3387. // delete the texture
  3388. LightmapCapture *lightmap_capture = lightmap_capture_data_owner.get(p_rid);
  3389. lightmap_capture->instance_remove_deps();
  3390. lightmap_capture_data_owner.free(p_rid);
  3391. memdelete(lightmap_capture);
  3392. return true;
  3393. } else if (canvas_occluder_owner.owns(p_rid)) {
  3394. CanvasOccluder *co = canvas_occluder_owner.get(p_rid);
  3395. if (co->index_id)
  3396. glDeleteBuffers(1, &co->index_id);
  3397. if (co->vertex_id)
  3398. glDeleteBuffers(1, &co->vertex_id);
  3399. canvas_occluder_owner.free(p_rid);
  3400. memdelete(co);
  3401. return true;
  3402. } else if (canvas_light_shadow_owner.owns(p_rid)) {
  3403. CanvasLightShadow *cls = canvas_light_shadow_owner.get(p_rid);
  3404. glDeleteFramebuffers(1, &cls->fbo);
  3405. glDeleteRenderbuffers(1, &cls->depth);
  3406. glDeleteTextures(1, &cls->distance);
  3407. canvas_light_shadow_owner.free(p_rid);
  3408. memdelete(cls);
  3409. return true;
  3410. } else {
  3411. return false;
  3412. }
  3413. }
  3414. bool RasterizerStorageGLES2::has_os_feature(const String &p_feature) const {
  3415. if (p_feature == "s3tc")
  3416. return config.s3tc_supported;
  3417. if (p_feature == "etc")
  3418. return config.etc1_supported;
  3419. return false;
  3420. }
  3421. ////////////////////////////////////////////
  3422. void RasterizerStorageGLES2::set_debug_generate_wireframes(bool p_generate) {
  3423. }
  3424. void RasterizerStorageGLES2::render_info_begin_capture() {
  3425. }
  3426. void RasterizerStorageGLES2::render_info_end_capture() {
  3427. }
  3428. int RasterizerStorageGLES2::get_captured_render_info(VS::RenderInfo p_info) {
  3429. return get_render_info(p_info);
  3430. }
  3431. int RasterizerStorageGLES2::get_render_info(VS::RenderInfo p_info) {
  3432. return 0;
  3433. }
  3434. void RasterizerStorageGLES2::initialize() {
  3435. RasterizerStorageGLES2::system_fbo = 0;
  3436. {
  3437. const GLubyte *extension_string = glGetString(GL_EXTENSIONS);
  3438. Vector<String> extensions = String((const char *)extension_string).split(" ");
  3439. for (int i = 0; i < extensions.size(); i++) {
  3440. config.extensions.insert(extensions[i]);
  3441. }
  3442. }
  3443. config.keep_original_textures = false;
  3444. config.shrink_textures_x2 = false;
  3445. #ifdef GLES_OVER_GL
  3446. config.float_texture_supported = true;
  3447. config.s3tc_supported = true;
  3448. config.etc1_supported = false;
  3449. #else
  3450. config.float_texture_supported = config.extensions.has("GL_ARB_texture_float") || config.extensions.has("GL_OES_texture_float");
  3451. config.s3tc_supported = config.extensions.has("GL_EXT_texture_compression_s3tc");
  3452. config.etc1_supported = config.extensions.has("GL_OES_compressed_ETC1_RGB8_texture");
  3453. #endif
  3454. #ifdef GLES_OVER_GL
  3455. config.use_rgba_2d_shadows = false;
  3456. #else
  3457. config.use_rgba_2d_shadows = !(config.float_texture_supported && config.extensions.has("GL_EXT_texture_rg"));
  3458. #endif
  3459. frame.count = 0;
  3460. frame.delta = 0;
  3461. frame.current_rt = NULL;
  3462. frame.clear_request = false;
  3463. glGetIntegerv(GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS, &config.max_texture_image_units);
  3464. glGetIntegerv(GL_MAX_TEXTURE_SIZE, &config.max_texture_size);
  3465. shaders.copy.init();
  3466. shaders.cubemap_filter.init();
  3467. bool ggx_hq = GLOBAL_GET("rendering/quality/reflections/high_quality_ggx.mobile");
  3468. shaders.cubemap_filter.set_conditional(CubemapFilterShaderGLES2::LOW_QUALITY, !ggx_hq);
  3469. {
  3470. // quad for copying stuff
  3471. glGenBuffers(1, &resources.quadie);
  3472. glBindBuffer(GL_ARRAY_BUFFER, resources.quadie);
  3473. {
  3474. const float qv[16] = {
  3475. -1,
  3476. -1,
  3477. 0,
  3478. 0,
  3479. -1,
  3480. 1,
  3481. 0,
  3482. 1,
  3483. 1,
  3484. 1,
  3485. 1,
  3486. 1,
  3487. 1,
  3488. -1,
  3489. 1,
  3490. 0,
  3491. };
  3492. glBufferData(GL_ARRAY_BUFFER, sizeof(float) * 16, qv, GL_STATIC_DRAW);
  3493. }
  3494. glBindBuffer(GL_ARRAY_BUFFER, 0);
  3495. }
  3496. {
  3497. //default textures
  3498. glGenTextures(1, &resources.white_tex);
  3499. unsigned char whitetexdata[8 * 8 * 3];
  3500. for (int i = 0; i < 8 * 8 * 3; i++) {
  3501. whitetexdata[i] = 255;
  3502. }
  3503. glActiveTexture(GL_TEXTURE0);
  3504. glBindTexture(GL_TEXTURE_2D, resources.white_tex);
  3505. glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 8, 8, 0, GL_RGB, GL_UNSIGNED_BYTE, whitetexdata);
  3506. glGenerateMipmap(GL_TEXTURE_2D);
  3507. glBindTexture(GL_TEXTURE_2D, 0);
  3508. glGenTextures(1, &resources.black_tex);
  3509. unsigned char blacktexdata[8 * 8 * 3];
  3510. for (int i = 0; i < 8 * 8 * 3; i++) {
  3511. blacktexdata[i] = 0;
  3512. }
  3513. glActiveTexture(GL_TEXTURE0);
  3514. glBindTexture(GL_TEXTURE_2D, resources.black_tex);
  3515. glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 8, 8, 0, GL_RGB, GL_UNSIGNED_BYTE, blacktexdata);
  3516. glGenerateMipmap(GL_TEXTURE_2D);
  3517. glBindTexture(GL_TEXTURE_2D, 0);
  3518. glGenTextures(1, &resources.normal_tex);
  3519. unsigned char normaltexdata[8 * 8 * 3];
  3520. for (int i = 0; i < 8 * 8 * 3; i += 3) {
  3521. normaltexdata[i + 0] = 128;
  3522. normaltexdata[i + 1] = 128;
  3523. normaltexdata[i + 2] = 255;
  3524. }
  3525. glActiveTexture(GL_TEXTURE0);
  3526. glBindTexture(GL_TEXTURE_2D, resources.normal_tex);
  3527. glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 8, 8, 0, GL_RGB, GL_UNSIGNED_BYTE, normaltexdata);
  3528. glGenerateMipmap(GL_TEXTURE_2D);
  3529. glBindTexture(GL_TEXTURE_2D, 0);
  3530. glGenTextures(1, &resources.aniso_tex);
  3531. unsigned char anisotexdata[8 * 8 * 3];
  3532. for (int i = 0; i < 8 * 8 * 3; i += 3) {
  3533. anisotexdata[i + 0] = 255;
  3534. anisotexdata[i + 1] = 128;
  3535. anisotexdata[i + 2] = 0;
  3536. }
  3537. glActiveTexture(GL_TEXTURE0);
  3538. glBindTexture(GL_TEXTURE_2D, resources.aniso_tex);
  3539. glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 8, 8, 0, GL_RGB, GL_UNSIGNED_BYTE, anisotexdata);
  3540. glGenerateMipmap(GL_TEXTURE_2D);
  3541. glBindTexture(GL_TEXTURE_2D, 0);
  3542. }
  3543. // skeleton buffer
  3544. {
  3545. resources.skeleton_transform_buffer_size = 0;
  3546. glGenBuffers(1, &resources.skeleton_transform_buffer);
  3547. }
  3548. // radical inverse vdc cache texture
  3549. // used for cubemap filtering
  3550. if (true /*||config.float_texture_supported*/) { //uint8 is similar and works everywhere
  3551. glGenTextures(1, &resources.radical_inverse_vdc_cache_tex);
  3552. glActiveTexture(GL_TEXTURE0);
  3553. glBindTexture(GL_TEXTURE_2D, resources.radical_inverse_vdc_cache_tex);
  3554. uint8_t radical_inverse[512];
  3555. for (uint32_t i = 0; i < 512; i++) {
  3556. uint32_t bits = i;
  3557. bits = (bits << 16) | (bits >> 16);
  3558. bits = ((bits & 0x55555555) << 1) | ((bits & 0xAAAAAAAA) >> 1);
  3559. bits = ((bits & 0x33333333) << 2) | ((bits & 0xCCCCCCCC) >> 2);
  3560. bits = ((bits & 0x0F0F0F0F) << 4) | ((bits & 0xF0F0F0F0) >> 4);
  3561. bits = ((bits & 0x00FF00FF) << 8) | ((bits & 0xFF00FF00) >> 8);
  3562. float value = float(bits) * 2.3283064365386963e-10;
  3563. radical_inverse[i] = uint8_t(CLAMP(value * 255.0, 0, 255));
  3564. }
  3565. glTexImage2D(GL_TEXTURE_2D, 0, GL_LUMINANCE, 512, 1, 0, GL_LUMINANCE, GL_UNSIGNED_BYTE, radical_inverse);
  3566. glBindTexture(GL_TEXTURE_2D, 0);
  3567. }
  3568. #ifdef GLES_OVER_GL
  3569. //this needs to be enabled manually in OpenGL 2.1
  3570. glEnable(_EXT_TEXTURE_CUBE_MAP_SEAMLESS);
  3571. glEnable(GL_POINT_SPRITE);
  3572. glEnable(GL_VERTEX_PROGRAM_POINT_SIZE);
  3573. #endif
  3574. config.force_vertex_shading = GLOBAL_GET("rendering/quality/shading/force_vertex_shading");
  3575. config.use_fast_texture_filter = GLOBAL_GET("rendering/quality/filters/use_nearest_mipmap_filter");
  3576. }
  3577. void RasterizerStorageGLES2::finalize() {
  3578. }
  3579. void RasterizerStorageGLES2::_copy_screen() {
  3580. bind_quad_array();
  3581. glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
  3582. }
  3583. void RasterizerStorageGLES2::update_dirty_resources() {
  3584. update_dirty_shaders();
  3585. update_dirty_materials();
  3586. update_dirty_skeletons();
  3587. update_dirty_multimeshes();
  3588. }
  3589. RasterizerStorageGLES2::RasterizerStorageGLES2() {
  3590. RasterizerStorageGLES2::system_fbo = 0;
  3591. }