texture_storage.cpp 106 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246
  1. /**************************************************************************/
  2. /* texture_storage.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  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. #ifdef GLES3_ENABLED
  31. #include "texture_storage.h"
  32. #include "../effects/copy_effects.h"
  33. #include "../rasterizer_gles3.h"
  34. #include "config.h"
  35. #include "utilities.h"
  36. #ifdef ANDROID_ENABLED
  37. #define glFramebufferTextureMultiviewOVR GLES3::Config::get_singleton()->eglFramebufferTextureMultiviewOVR
  38. #endif
  39. using namespace GLES3;
  40. TextureStorage *TextureStorage::singleton = nullptr;
  41. TextureStorage *TextureStorage::get_singleton() {
  42. return singleton;
  43. }
  44. static const GLenum _cube_side_enum[6] = {
  45. GL_TEXTURE_CUBE_MAP_NEGATIVE_X,
  46. GL_TEXTURE_CUBE_MAP_POSITIVE_X,
  47. GL_TEXTURE_CUBE_MAP_NEGATIVE_Y,
  48. GL_TEXTURE_CUBE_MAP_POSITIVE_Y,
  49. GL_TEXTURE_CUBE_MAP_NEGATIVE_Z,
  50. GL_TEXTURE_CUBE_MAP_POSITIVE_Z,
  51. };
  52. TextureStorage::TextureStorage() {
  53. singleton = this;
  54. { //create default textures
  55. { // White Textures
  56. Ref<Image> image = Image::create_empty(4, 4, true, Image::FORMAT_RGBA8);
  57. image->fill(Color(1, 1, 1, 1));
  58. image->generate_mipmaps();
  59. default_gl_textures[DEFAULT_GL_TEXTURE_WHITE] = texture_allocate();
  60. texture_2d_initialize(default_gl_textures[DEFAULT_GL_TEXTURE_WHITE], image);
  61. Vector<Ref<Image>> images;
  62. images.push_back(image);
  63. default_gl_textures[DEFAULT_GL_TEXTURE_2D_ARRAY_WHITE] = texture_allocate();
  64. texture_2d_layered_initialize(default_gl_textures[DEFAULT_GL_TEXTURE_2D_ARRAY_WHITE], images, RS::TEXTURE_LAYERED_2D_ARRAY);
  65. for (int i = 0; i < 5; i++) {
  66. images.push_back(image);
  67. }
  68. default_gl_textures[DEFAULT_GL_TEXTURE_CUBEMAP_WHITE] = texture_allocate();
  69. texture_2d_layered_initialize(default_gl_textures[DEFAULT_GL_TEXTURE_CUBEMAP_WHITE], images, RS::TEXTURE_LAYERED_CUBEMAP);
  70. }
  71. {
  72. Ref<Image> image = Image::create_empty(4, 4, false, Image::FORMAT_RGBA8);
  73. image->fill(Color(1, 1, 1, 1));
  74. Vector<Ref<Image>> images;
  75. for (int i = 0; i < 4; i++) {
  76. images.push_back(image);
  77. }
  78. default_gl_textures[DEFAULT_GL_TEXTURE_3D_WHITE] = texture_allocate();
  79. texture_3d_initialize(default_gl_textures[DEFAULT_GL_TEXTURE_3D_WHITE], image->get_format(), 4, 4, 4, false, images);
  80. }
  81. { // black
  82. Ref<Image> image = Image::create_empty(4, 4, true, Image::FORMAT_RGBA8);
  83. image->fill(Color(0, 0, 0, 1));
  84. image->generate_mipmaps();
  85. default_gl_textures[DEFAULT_GL_TEXTURE_BLACK] = texture_allocate();
  86. texture_2d_initialize(default_gl_textures[DEFAULT_GL_TEXTURE_BLACK], image);
  87. Vector<Ref<Image>> images;
  88. for (int i = 0; i < 6; i++) {
  89. images.push_back(image);
  90. }
  91. default_gl_textures[DEFAULT_GL_TEXTURE_CUBEMAP_BLACK] = texture_allocate();
  92. texture_2d_layered_initialize(default_gl_textures[DEFAULT_GL_TEXTURE_CUBEMAP_BLACK], images, RS::TEXTURE_LAYERED_CUBEMAP);
  93. }
  94. {
  95. Ref<Image> image = Image::create_empty(4, 4, false, Image::FORMAT_RGBA8);
  96. image->fill(Color());
  97. Vector<Ref<Image>> images;
  98. for (int i = 0; i < 4; i++) {
  99. images.push_back(image);
  100. }
  101. default_gl_textures[DEFAULT_GL_TEXTURE_3D_BLACK] = texture_allocate();
  102. texture_3d_initialize(default_gl_textures[DEFAULT_GL_TEXTURE_3D_BLACK], image->get_format(), 4, 4, 4, false, images);
  103. }
  104. { // transparent black
  105. Ref<Image> image = Image::create_empty(4, 4, true, Image::FORMAT_RGBA8);
  106. image->fill(Color(0, 0, 0, 0));
  107. image->generate_mipmaps();
  108. default_gl_textures[DEFAULT_GL_TEXTURE_TRANSPARENT] = texture_allocate();
  109. texture_2d_initialize(default_gl_textures[DEFAULT_GL_TEXTURE_TRANSPARENT], image);
  110. }
  111. {
  112. Ref<Image> image = Image::create_empty(4, 4, true, Image::FORMAT_RGBA8);
  113. image->fill(Color(0.5, 0.5, 1, 1));
  114. image->generate_mipmaps();
  115. default_gl_textures[DEFAULT_GL_TEXTURE_NORMAL] = texture_allocate();
  116. texture_2d_initialize(default_gl_textures[DEFAULT_GL_TEXTURE_NORMAL], image);
  117. }
  118. {
  119. Ref<Image> image = Image::create_empty(4, 4, true, Image::FORMAT_RGBA8);
  120. image->fill(Color(1.0, 0.5, 1, 1));
  121. image->generate_mipmaps();
  122. default_gl_textures[DEFAULT_GL_TEXTURE_ANISO] = texture_allocate();
  123. texture_2d_initialize(default_gl_textures[DEFAULT_GL_TEXTURE_ANISO], image);
  124. }
  125. {
  126. default_gl_textures[DEFAULT_GL_TEXTURE_EXT] = texture_allocate();
  127. texture_external_initialize(default_gl_textures[DEFAULT_GL_TEXTURE_EXT], 1, 1, 0);
  128. }
  129. {
  130. unsigned char pixel_data[4 * 4 * 4];
  131. for (int i = 0; i < 16; i++) {
  132. pixel_data[i * 4 + 0] = 0;
  133. pixel_data[i * 4 + 1] = 0;
  134. pixel_data[i * 4 + 2] = 0;
  135. pixel_data[i * 4 + 3] = 0;
  136. }
  137. default_gl_textures[DEFAULT_GL_TEXTURE_2D_UINT] = texture_allocate();
  138. Texture texture;
  139. texture.width = 4;
  140. texture.height = 4;
  141. texture.format = Image::FORMAT_RGBA8;
  142. texture.type = Texture::TYPE_2D;
  143. texture.target = GL_TEXTURE_2D;
  144. texture.active = true;
  145. glGenTextures(1, &texture.tex_id);
  146. texture_owner.initialize_rid(default_gl_textures[DEFAULT_GL_TEXTURE_2D_UINT], texture);
  147. glBindTexture(GL_TEXTURE_2D, texture.tex_id);
  148. glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8UI, 4, 4, 0, GL_RGBA_INTEGER, GL_UNSIGNED_BYTE, pixel_data);
  149. GLES3::Utilities::get_singleton()->texture_allocated_data(texture.tex_id, 4 * 4 * 4, "Default uint texture");
  150. texture.gl_set_filter(RS::CANVAS_ITEM_TEXTURE_FILTER_NEAREST);
  151. }
  152. {
  153. uint16_t pixel_data[4 * 4];
  154. for (int i = 0; i < 16; i++) {
  155. pixel_data[i] = Math::make_half_float(1.0f);
  156. }
  157. default_gl_textures[DEFAULT_GL_TEXTURE_DEPTH] = texture_allocate();
  158. Texture texture;
  159. texture.width = 4;
  160. texture.height = 4;
  161. texture.format = Image::FORMAT_RGBA8;
  162. texture.type = Texture::TYPE_2D;
  163. texture.target = GL_TEXTURE_2D;
  164. texture.active = true;
  165. glGenTextures(1, &texture.tex_id);
  166. texture_owner.initialize_rid(default_gl_textures[DEFAULT_GL_TEXTURE_DEPTH], texture);
  167. glBindTexture(GL_TEXTURE_2D, texture.tex_id);
  168. glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT16, 4, 4, 0, GL_DEPTH_COMPONENT, GL_UNSIGNED_SHORT, pixel_data);
  169. GLES3::Utilities::get_singleton()->texture_allocated_data(texture.tex_id, 4 * 4 * 2, "Default depth texture");
  170. texture.gl_set_filter(RS::CANVAS_ITEM_TEXTURE_FILTER_NEAREST);
  171. }
  172. }
  173. glBindTexture(GL_TEXTURE_2D, 0);
  174. { // Atlas Texture initialize.
  175. uint8_t pixel_data[4 * 4 * 4];
  176. for (int i = 0; i < 16; i++) {
  177. pixel_data[i * 4 + 0] = 0;
  178. pixel_data[i * 4 + 1] = 0;
  179. pixel_data[i * 4 + 2] = 0;
  180. pixel_data[i * 4 + 3] = 255;
  181. }
  182. glGenTextures(1, &texture_atlas.texture);
  183. glBindTexture(GL_TEXTURE_2D, texture_atlas.texture);
  184. glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, 4, 4, 0, GL_RGBA, GL_UNSIGNED_BYTE, pixel_data);
  185. GLES3::Utilities::get_singleton()->texture_allocated_data(texture_atlas.texture, 4 * 4 * 4, "Texture atlas (Default)");
  186. }
  187. glBindTexture(GL_TEXTURE_2D, 0);
  188. {
  189. sdf_shader.shader.initialize();
  190. sdf_shader.shader_version = sdf_shader.shader.version_create();
  191. }
  192. // Initialize texture placeholder data for the `texture_*_placeholder_initialize()` methods.
  193. constexpr int placeholder_size = 4;
  194. texture_2d_placeholder = Image::create_empty(placeholder_size, placeholder_size, false, Image::FORMAT_RGBA8);
  195. // Draw a magenta/black checkerboard pattern.
  196. for (int i = 0; i < placeholder_size * placeholder_size; i++) {
  197. const int x = i % placeholder_size;
  198. const int y = i / placeholder_size;
  199. texture_2d_placeholder->set_pixel(x, y, (x + y) % 2 == 0 ? Color(1, 0, 1) : Color(0, 0, 0));
  200. }
  201. texture_2d_array_placeholder.push_back(texture_2d_placeholder);
  202. for (int i = 0; i < 6; i++) {
  203. cubemap_placeholder.push_back(texture_2d_placeholder);
  204. }
  205. Ref<Image> texture_2d_placeholder_rotated;
  206. texture_2d_placeholder_rotated.instantiate();
  207. texture_2d_placeholder_rotated->copy_from(texture_2d_placeholder);
  208. texture_2d_placeholder_rotated->rotate_90(CLOCKWISE);
  209. for (int i = 0; i < 4; i++) {
  210. // Alternate checkerboard pattern on odd layers (by using a copy that is rotated 90 degrees).
  211. texture_3d_placeholder.push_back(i % 2 == 0 ? texture_2d_placeholder : texture_2d_placeholder_rotated);
  212. }
  213. #ifdef GL_API_ENABLED
  214. if (RasterizerGLES3::is_gles_over_gl()) {
  215. glEnable(GL_PROGRAM_POINT_SIZE);
  216. }
  217. #endif // GL_API_ENABLED
  218. }
  219. TextureStorage::~TextureStorage() {
  220. singleton = nullptr;
  221. for (int i = 0; i < DEFAULT_GL_TEXTURE_MAX; i++) {
  222. texture_free(default_gl_textures[i]);
  223. }
  224. if (texture_atlas.texture != 0) {
  225. GLES3::Utilities::get_singleton()->texture_free_data(texture_atlas.texture);
  226. }
  227. texture_atlas.texture = 0;
  228. glDeleteFramebuffers(1, &texture_atlas.framebuffer);
  229. texture_atlas.framebuffer = 0;
  230. sdf_shader.shader.version_free(sdf_shader.shader_version);
  231. }
  232. /* Canvas Texture API */
  233. RID TextureStorage::canvas_texture_allocate() {
  234. return canvas_texture_owner.allocate_rid();
  235. }
  236. void TextureStorage::canvas_texture_initialize(RID p_rid) {
  237. canvas_texture_owner.initialize_rid(p_rid);
  238. }
  239. void TextureStorage::canvas_texture_free(RID p_rid) {
  240. canvas_texture_owner.free(p_rid);
  241. }
  242. void TextureStorage::canvas_texture_set_channel(RID p_canvas_texture, RS::CanvasTextureChannel p_channel, RID p_texture) {
  243. CanvasTexture *ct = canvas_texture_owner.get_or_null(p_canvas_texture);
  244. ERR_FAIL_NULL(ct);
  245. switch (p_channel) {
  246. case RS::CANVAS_TEXTURE_CHANNEL_DIFFUSE: {
  247. ct->diffuse = p_texture;
  248. } break;
  249. case RS::CANVAS_TEXTURE_CHANNEL_NORMAL: {
  250. ct->normal_map = p_texture;
  251. } break;
  252. case RS::CANVAS_TEXTURE_CHANNEL_SPECULAR: {
  253. ct->specular = p_texture;
  254. } break;
  255. }
  256. }
  257. void TextureStorage::canvas_texture_set_shading_parameters(RID p_canvas_texture, const Color &p_specular_color, float p_shininess) {
  258. CanvasTexture *ct = canvas_texture_owner.get_or_null(p_canvas_texture);
  259. ERR_FAIL_NULL(ct);
  260. ct->specular_color.r = p_specular_color.r;
  261. ct->specular_color.g = p_specular_color.g;
  262. ct->specular_color.b = p_specular_color.b;
  263. ct->specular_color.a = p_shininess;
  264. }
  265. void TextureStorage::canvas_texture_set_texture_filter(RID p_canvas_texture, RS::CanvasItemTextureFilter p_filter) {
  266. CanvasTexture *ct = canvas_texture_owner.get_or_null(p_canvas_texture);
  267. ERR_FAIL_NULL(ct);
  268. ct->texture_filter = p_filter;
  269. }
  270. void TextureStorage::canvas_texture_set_texture_repeat(RID p_canvas_texture, RS::CanvasItemTextureRepeat p_repeat) {
  271. CanvasTexture *ct = canvas_texture_owner.get_or_null(p_canvas_texture);
  272. ERR_FAIL_NULL(ct);
  273. ct->texture_repeat = p_repeat;
  274. }
  275. /* Texture API */
  276. static inline Error _get_gl_uncompressed_format(const Ref<Image> &p_image, Image::Format p_format, Image::Format &r_real_format, GLenum &r_gl_format, GLenum &r_gl_internal_format, GLenum &r_gl_type) {
  277. Config *config = Config::get_singleton();
  278. switch (p_format) {
  279. case Image::FORMAT_L8: {
  280. if (RasterizerGLES3::is_gles_over_gl()) {
  281. r_gl_internal_format = GL_R8;
  282. r_gl_format = GL_RED;
  283. r_gl_type = GL_UNSIGNED_BYTE;
  284. } else {
  285. r_gl_internal_format = GL_LUMINANCE;
  286. r_gl_format = GL_LUMINANCE;
  287. r_gl_type = GL_UNSIGNED_BYTE;
  288. }
  289. } break;
  290. case Image::FORMAT_LA8: {
  291. if (RasterizerGLES3::is_gles_over_gl()) {
  292. r_gl_internal_format = GL_RG8;
  293. r_gl_format = GL_RG;
  294. r_gl_type = GL_UNSIGNED_BYTE;
  295. } else {
  296. r_gl_internal_format = GL_LUMINANCE_ALPHA;
  297. r_gl_format = GL_LUMINANCE_ALPHA;
  298. r_gl_type = GL_UNSIGNED_BYTE;
  299. }
  300. } break;
  301. case Image::FORMAT_R8: {
  302. r_gl_internal_format = GL_R8;
  303. r_gl_format = GL_RED;
  304. r_gl_type = GL_UNSIGNED_BYTE;
  305. } break;
  306. case Image::FORMAT_RG8: {
  307. r_gl_internal_format = GL_RG8;
  308. r_gl_format = GL_RG;
  309. r_gl_type = GL_UNSIGNED_BYTE;
  310. } break;
  311. case Image::FORMAT_RGB8: {
  312. r_gl_internal_format = GL_RGB8;
  313. r_gl_format = GL_RGB;
  314. r_gl_type = GL_UNSIGNED_BYTE;
  315. } break;
  316. case Image::FORMAT_RGBA8: {
  317. r_gl_internal_format = GL_RGBA8;
  318. r_gl_format = GL_RGBA;
  319. r_gl_type = GL_UNSIGNED_BYTE;
  320. } break;
  321. case Image::FORMAT_RGBA4444: {
  322. r_gl_internal_format = GL_RGBA4;
  323. r_gl_format = GL_RGBA;
  324. r_gl_type = GL_UNSIGNED_SHORT_4_4_4_4;
  325. } break;
  326. case Image::FORMAT_RGB565: {
  327. r_gl_internal_format = GL_RGB565;
  328. r_gl_format = GL_RGB;
  329. r_gl_type = GL_UNSIGNED_SHORT_5_6_5;
  330. } break;
  331. case Image::FORMAT_RF: {
  332. if (config->float_texture_linear_supported) {
  333. r_gl_internal_format = GL_R32F;
  334. r_gl_format = GL_RED;
  335. r_gl_type = GL_FLOAT;
  336. } else {
  337. if (p_image.is_valid()) {
  338. p_image->convert(Image::FORMAT_RH);
  339. }
  340. r_real_format = Image::FORMAT_RH;
  341. r_gl_internal_format = GL_R16F;
  342. r_gl_format = GL_RED;
  343. r_gl_type = GL_HALF_FLOAT;
  344. }
  345. } break;
  346. case Image::FORMAT_RGF: {
  347. if (config->float_texture_linear_supported) {
  348. r_gl_internal_format = GL_RG32F;
  349. r_gl_format = GL_RG;
  350. r_gl_type = GL_FLOAT;
  351. } else {
  352. if (p_image.is_valid()) {
  353. p_image->convert(Image::FORMAT_RGH);
  354. }
  355. r_real_format = Image::FORMAT_RGH;
  356. r_gl_internal_format = GL_RG16F;
  357. r_gl_format = GL_RG;
  358. r_gl_type = GL_HALF_FLOAT;
  359. }
  360. } break;
  361. case Image::FORMAT_RGBF: {
  362. if (config->float_texture_linear_supported) {
  363. r_gl_internal_format = GL_RGB32F;
  364. r_gl_format = GL_RGB;
  365. r_gl_type = GL_FLOAT;
  366. } else {
  367. if (p_image.is_valid()) {
  368. p_image->convert(Image::FORMAT_RGBH);
  369. }
  370. r_real_format = Image::FORMAT_RGBH;
  371. r_gl_internal_format = GL_RGB16F;
  372. r_gl_format = GL_RGB;
  373. r_gl_type = GL_HALF_FLOAT;
  374. }
  375. } break;
  376. case Image::FORMAT_RGBAF: {
  377. if (config->float_texture_linear_supported) {
  378. r_gl_internal_format = GL_RGBA32F;
  379. r_gl_format = GL_RGBA;
  380. r_gl_type = GL_FLOAT;
  381. } else {
  382. if (p_image.is_valid()) {
  383. p_image->convert(Image::FORMAT_RGBAH);
  384. }
  385. r_real_format = Image::FORMAT_RGBAH;
  386. r_gl_internal_format = GL_RGBA16F;
  387. r_gl_format = GL_RGBA;
  388. r_gl_type = GL_HALF_FLOAT;
  389. }
  390. } break;
  391. case Image::FORMAT_RH: {
  392. r_gl_internal_format = GL_R16F;
  393. r_gl_format = GL_RED;
  394. r_gl_type = GL_HALF_FLOAT;
  395. } break;
  396. case Image::FORMAT_RGH: {
  397. r_gl_internal_format = GL_RG16F;
  398. r_gl_format = GL_RG;
  399. r_gl_type = GL_HALF_FLOAT;
  400. } break;
  401. case Image::FORMAT_RGBH: {
  402. r_gl_internal_format = GL_RGB16F;
  403. r_gl_format = GL_RGB;
  404. r_gl_type = GL_HALF_FLOAT;
  405. } break;
  406. case Image::FORMAT_RGBAH: {
  407. r_gl_internal_format = GL_RGBA16F;
  408. r_gl_format = GL_RGBA;
  409. r_gl_type = GL_HALF_FLOAT;
  410. } break;
  411. case Image::FORMAT_RGBE9995: {
  412. r_gl_internal_format = GL_RGB9_E5;
  413. r_gl_format = GL_RGB;
  414. r_gl_type = GL_UNSIGNED_INT_5_9_9_9_REV;
  415. } break;
  416. default: {
  417. return ERR_UNAVAILABLE;
  418. }
  419. }
  420. return OK;
  421. }
  422. Ref<Image> TextureStorage::_get_gl_image_and_format(const Ref<Image> &p_image, Image::Format p_format, Image::Format &r_real_format, GLenum &r_gl_format, GLenum &r_gl_internal_format, GLenum &r_gl_type, bool &r_compressed, bool p_force_decompress) const {
  423. Config *config = Config::get_singleton();
  424. r_gl_format = 0;
  425. Ref<Image> image = p_image;
  426. r_compressed = false;
  427. r_real_format = p_format;
  428. if (!Image::is_format_compressed(p_format)) {
  429. Error err = _get_gl_uncompressed_format(p_image, p_format, r_real_format, r_gl_format, r_gl_internal_format, r_gl_type);
  430. ERR_FAIL_COND_V_MSG(err != OK, Ref<Image>(), vformat("The image format %d is not supported by the Compatibility renderer.", p_format));
  431. if (p_format != r_real_format) {
  432. WARN_PRINT(vformat("Image format %s not supported by hardware, converting to %s.", Image::get_format_name(p_format), Image::get_format_name(r_real_format)));
  433. }
  434. return p_image;
  435. }
  436. // For compressed images, some formats may not be supported by the current device and will require decompression.
  437. bool need_decompress = false;
  438. bool decompress_ra_to_rg = false;
  439. switch (p_format) {
  440. case Image::FORMAT_DXT1: {
  441. if (config->s3tc_supported) {
  442. r_gl_internal_format = _EXT_COMPRESSED_RGBA_S3TC_DXT1_EXT;
  443. r_gl_format = GL_RGBA;
  444. r_gl_type = GL_UNSIGNED_BYTE;
  445. r_compressed = true;
  446. } else {
  447. need_decompress = true;
  448. }
  449. } break;
  450. case Image::FORMAT_DXT3: {
  451. if (config->s3tc_supported) {
  452. r_gl_internal_format = _EXT_COMPRESSED_RGBA_S3TC_DXT3_EXT;
  453. r_gl_format = GL_RGBA;
  454. r_gl_type = GL_UNSIGNED_BYTE;
  455. r_compressed = true;
  456. } else {
  457. need_decompress = true;
  458. }
  459. } break;
  460. case Image::FORMAT_DXT5: {
  461. if (config->s3tc_supported) {
  462. r_gl_internal_format = _EXT_COMPRESSED_RGBA_S3TC_DXT5_EXT;
  463. r_gl_format = GL_RGBA;
  464. r_gl_type = GL_UNSIGNED_BYTE;
  465. r_compressed = true;
  466. } else {
  467. need_decompress = true;
  468. }
  469. } break;
  470. case Image::FORMAT_RGTC_R: {
  471. if (config->rgtc_supported) {
  472. r_gl_internal_format = _EXT_COMPRESSED_RED_RGTC1_EXT;
  473. r_gl_format = GL_RGBA;
  474. r_gl_type = GL_UNSIGNED_BYTE;
  475. r_compressed = true;
  476. } else {
  477. need_decompress = true;
  478. }
  479. } break;
  480. case Image::FORMAT_RGTC_RG: {
  481. if (config->rgtc_supported) {
  482. r_gl_internal_format = _EXT_COMPRESSED_RED_GREEN_RGTC2_EXT;
  483. r_gl_format = GL_RGBA;
  484. r_gl_type = GL_UNSIGNED_BYTE;
  485. r_compressed = true;
  486. } else {
  487. need_decompress = true;
  488. }
  489. } break;
  490. case Image::FORMAT_BPTC_RGBA: {
  491. if (config->bptc_supported) {
  492. r_gl_internal_format = _EXT_COMPRESSED_RGBA_BPTC_UNORM;
  493. r_gl_format = GL_RGBA;
  494. r_gl_type = GL_UNSIGNED_BYTE;
  495. r_compressed = true;
  496. } else {
  497. need_decompress = true;
  498. }
  499. } break;
  500. case Image::FORMAT_BPTC_RGBF: {
  501. if (config->bptc_supported) {
  502. r_gl_internal_format = _EXT_COMPRESSED_RGB_BPTC_SIGNED_FLOAT;
  503. r_gl_format = GL_RGB;
  504. r_gl_type = GL_FLOAT;
  505. r_compressed = true;
  506. } else {
  507. need_decompress = true;
  508. }
  509. } break;
  510. case Image::FORMAT_BPTC_RGBFU: {
  511. if (config->bptc_supported) {
  512. r_gl_internal_format = _EXT_COMPRESSED_RGB_BPTC_UNSIGNED_FLOAT;
  513. r_gl_format = GL_RGB;
  514. r_gl_type = GL_FLOAT;
  515. r_compressed = true;
  516. } else {
  517. need_decompress = true;
  518. }
  519. } break;
  520. case Image::FORMAT_ETC2_R11: {
  521. if (config->etc2_supported) {
  522. r_gl_internal_format = _EXT_COMPRESSED_R11_EAC;
  523. r_gl_format = GL_RED;
  524. r_gl_type = GL_UNSIGNED_BYTE;
  525. r_compressed = true;
  526. } else {
  527. need_decompress = true;
  528. }
  529. } break;
  530. case Image::FORMAT_ETC2_R11S: {
  531. if (config->etc2_supported) {
  532. r_gl_internal_format = _EXT_COMPRESSED_SIGNED_R11_EAC;
  533. r_gl_format = GL_RED;
  534. r_gl_type = GL_UNSIGNED_BYTE;
  535. r_compressed = true;
  536. } else {
  537. need_decompress = true;
  538. }
  539. } break;
  540. case Image::FORMAT_ETC2_RG11: {
  541. if (config->etc2_supported) {
  542. r_gl_internal_format = _EXT_COMPRESSED_RG11_EAC;
  543. r_gl_format = GL_RG;
  544. r_gl_type = GL_UNSIGNED_BYTE;
  545. r_compressed = true;
  546. } else {
  547. need_decompress = true;
  548. }
  549. } break;
  550. case Image::FORMAT_ETC2_RG11S: {
  551. if (config->etc2_supported) {
  552. r_gl_internal_format = _EXT_COMPRESSED_SIGNED_RG11_EAC;
  553. r_gl_format = GL_RG;
  554. r_gl_type = GL_UNSIGNED_BYTE;
  555. r_compressed = true;
  556. } else {
  557. need_decompress = true;
  558. }
  559. } break;
  560. case Image::FORMAT_ETC:
  561. case Image::FORMAT_ETC2_RGB8: {
  562. if (config->etc2_supported) {
  563. r_gl_internal_format = _EXT_COMPRESSED_RGB8_ETC2;
  564. r_gl_format = GL_RGB;
  565. r_gl_type = GL_UNSIGNED_BYTE;
  566. r_compressed = true;
  567. } else {
  568. need_decompress = true;
  569. }
  570. } break;
  571. case Image::FORMAT_ETC2_RGBA8: {
  572. if (config->etc2_supported) {
  573. r_gl_internal_format = _EXT_COMPRESSED_RGBA8_ETC2_EAC;
  574. r_gl_format = GL_RGBA;
  575. r_gl_type = GL_UNSIGNED_BYTE;
  576. r_compressed = true;
  577. } else {
  578. need_decompress = true;
  579. }
  580. } break;
  581. case Image::FORMAT_ETC2_RGB8A1: {
  582. if (config->etc2_supported) {
  583. r_gl_internal_format = _EXT_COMPRESSED_RGB8_PUNCHTHROUGH_ALPHA1_ETC2;
  584. r_gl_format = GL_RGBA;
  585. r_gl_type = GL_UNSIGNED_BYTE;
  586. r_compressed = true;
  587. } else {
  588. need_decompress = true;
  589. }
  590. } break;
  591. case Image::FORMAT_ETC2_RA_AS_RG: {
  592. #ifndef WEB_ENABLED
  593. if (config->etc2_supported) {
  594. r_gl_internal_format = _EXT_COMPRESSED_RGBA8_ETC2_EAC;
  595. r_gl_format = GL_RGBA;
  596. r_gl_type = GL_UNSIGNED_BYTE;
  597. r_compressed = true;
  598. } else
  599. #endif
  600. {
  601. need_decompress = true;
  602. }
  603. decompress_ra_to_rg = true;
  604. } break;
  605. case Image::FORMAT_DXT5_RA_AS_RG: {
  606. #ifndef WEB_ENABLED
  607. if (config->s3tc_supported) {
  608. r_gl_internal_format = _EXT_COMPRESSED_RGBA_S3TC_DXT5_EXT;
  609. r_gl_format = GL_RGBA;
  610. r_gl_type = GL_UNSIGNED_BYTE;
  611. r_compressed = true;
  612. } else
  613. #endif
  614. {
  615. need_decompress = true;
  616. }
  617. decompress_ra_to_rg = true;
  618. } break;
  619. case Image::FORMAT_ASTC_4x4: {
  620. if (config->astc_supported) {
  621. r_gl_internal_format = _EXT_COMPRESSED_RGBA_ASTC_4x4_KHR;
  622. r_gl_format = GL_RGBA;
  623. r_gl_type = GL_UNSIGNED_BYTE;
  624. r_compressed = true;
  625. } else {
  626. need_decompress = true;
  627. }
  628. } break;
  629. case Image::FORMAT_ASTC_4x4_HDR: {
  630. if (config->astc_hdr_supported) {
  631. r_gl_internal_format = _EXT_COMPRESSED_RGBA_ASTC_4x4_KHR;
  632. r_gl_format = GL_RGBA;
  633. r_gl_type = GL_UNSIGNED_BYTE;
  634. r_compressed = true;
  635. } else {
  636. need_decompress = true;
  637. }
  638. } break;
  639. case Image::FORMAT_ASTC_8x8: {
  640. if (config->astc_supported) {
  641. r_gl_internal_format = _EXT_COMPRESSED_RGBA_ASTC_8x8_KHR;
  642. r_gl_format = GL_RGBA;
  643. r_gl_type = GL_UNSIGNED_BYTE;
  644. r_compressed = true;
  645. } else {
  646. need_decompress = true;
  647. }
  648. } break;
  649. case Image::FORMAT_ASTC_8x8_HDR: {
  650. if (config->astc_hdr_supported) {
  651. r_gl_internal_format = _EXT_COMPRESSED_RGBA_ASTC_8x8_KHR;
  652. r_gl_format = GL_RGBA;
  653. r_gl_type = GL_UNSIGNED_BYTE;
  654. r_compressed = true;
  655. } else {
  656. need_decompress = true;
  657. }
  658. } break;
  659. default: {
  660. ERR_FAIL_V_MSG(Ref<Image>(), vformat("The image format %d is not supported by the Compatibility renderer.", p_format));
  661. }
  662. }
  663. if (need_decompress || p_force_decompress) {
  664. if (image.is_valid()) {
  665. image = image->duplicate();
  666. image->decompress();
  667. ERR_FAIL_COND_V(image->is_compressed(), image);
  668. if (decompress_ra_to_rg) {
  669. image->convert_ra_rgba8_to_rg();
  670. image->convert(Image::FORMAT_RG8);
  671. }
  672. Error err = _get_gl_uncompressed_format(image, image->get_format(), r_real_format, r_gl_format, r_gl_internal_format, r_gl_type);
  673. ERR_FAIL_COND_V_MSG(err != OK, Ref<Image>(), vformat("The image format %d is not supported by the Compatibility renderer.", image->get_format()));
  674. r_real_format = image->get_format();
  675. r_compressed = false;
  676. if (p_format != image->get_format()) {
  677. WARN_PRINT(vformat("Image format %s not supported by hardware, converting to %s.", Image::get_format_name(p_format), Image::get_format_name(image->get_format())));
  678. }
  679. }
  680. return image;
  681. }
  682. return p_image;
  683. }
  684. RID TextureStorage::texture_allocate() {
  685. return texture_owner.allocate_rid();
  686. }
  687. void TextureStorage::texture_free(RID p_texture) {
  688. Texture *t = texture_owner.get_or_null(p_texture);
  689. ERR_FAIL_NULL(t);
  690. ERR_FAIL_COND(t->is_render_target);
  691. if (t->canvas_texture) {
  692. memdelete(t->canvas_texture);
  693. }
  694. bool must_free_data = false;
  695. if (t->is_proxy) {
  696. if (t->proxy_to.is_valid()) {
  697. Texture *proxy_to = texture_owner.get_or_null(t->proxy_to);
  698. if (proxy_to) {
  699. proxy_to->proxies.erase(p_texture);
  700. }
  701. }
  702. } else {
  703. must_free_data = t->tex_id != 0 && !t->is_from_native_handle;
  704. }
  705. if (must_free_data) {
  706. GLES3::Utilities::get_singleton()->texture_free_data(t->tex_id);
  707. t->tex_id = 0;
  708. }
  709. texture_atlas_remove_texture(p_texture);
  710. for (int i = 0; i < t->proxies.size(); i++) {
  711. Texture *p = texture_owner.get_or_null(t->proxies[i]);
  712. ERR_CONTINUE(!p);
  713. p->proxy_to = RID();
  714. p->tex_id = 0;
  715. }
  716. texture_owner.free(p_texture);
  717. }
  718. void TextureStorage::texture_2d_initialize(RID p_texture, const Ref<Image> &p_image) {
  719. ERR_FAIL_COND(p_image.is_null());
  720. Texture texture;
  721. texture.width = p_image->get_width();
  722. texture.height = p_image->get_height();
  723. texture.alloc_width = texture.width;
  724. texture.alloc_height = texture.height;
  725. texture.mipmaps = p_image->get_mipmap_count() + 1;
  726. texture.format = p_image->get_format();
  727. texture.type = Texture::TYPE_2D;
  728. texture.target = GL_TEXTURE_2D;
  729. _get_gl_image_and_format(Ref<Image>(), texture.format, texture.real_format, texture.gl_format_cache, texture.gl_internal_format_cache, texture.gl_type_cache, texture.compressed, false);
  730. texture.total_data_size = p_image->get_image_data_size(texture.width, texture.height, texture.format, texture.mipmaps);
  731. texture.active = true;
  732. glGenTextures(1, &texture.tex_id);
  733. GLES3::Utilities::get_singleton()->texture_allocated_data(texture.tex_id, texture.total_data_size, "Texture 2D");
  734. texture_owner.initialize_rid(p_texture, texture);
  735. texture_set_data(p_texture, p_image);
  736. }
  737. void TextureStorage::texture_external_initialize(RID p_texture, int p_width, int p_height, uint64_t p_external_buffer) {
  738. Texture texture;
  739. texture.active = true;
  740. texture.alloc_width = texture.width = p_width;
  741. texture.alloc_height = texture.height = p_height;
  742. texture.real_format = texture.format = Image::FORMAT_RGB8;
  743. texture.type = Texture::TYPE_2D;
  744. if (GLES3::Config::get_singleton()->external_texture_supported) {
  745. texture.target = _GL_TEXTURE_EXTERNAL_OES;
  746. } else {
  747. texture.target = GL_TEXTURE_2D;
  748. }
  749. glGenTextures(1, &texture.tex_id);
  750. glBindTexture(texture.target, texture.tex_id);
  751. #ifdef ANDROID_ENABLED
  752. if (texture.target == _GL_TEXTURE_EXTERNAL_OES) {
  753. if (p_external_buffer) {
  754. GLES3::Config::get_singleton()->eglEGLImageTargetTexture2DOES(_GL_TEXTURE_EXTERNAL_OES, reinterpret_cast<void *>(p_external_buffer));
  755. }
  756. texture.total_data_size = 0;
  757. } else
  758. #endif
  759. {
  760. // If external textures aren't supported, allocate an empty 1x1 texture.
  761. glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 1, 1, 0, GL_RGB, GL_UNSIGNED_BYTE, nullptr);
  762. texture.total_data_size = 3;
  763. }
  764. glTexParameteri(texture.target, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  765. glTexParameteri(texture.target, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  766. glTexParameteri(texture.target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  767. glTexParameteri(texture.target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  768. GLES3::Utilities::get_singleton()->texture_allocated_data(texture.tex_id, texture.total_data_size, "Texture External");
  769. texture_owner.initialize_rid(p_texture, texture);
  770. glBindTexture(texture.target, 0);
  771. }
  772. void TextureStorage::texture_2d_layered_initialize(RID p_texture, const Vector<Ref<Image>> &p_layers, RS::TextureLayeredType p_layered_type) {
  773. ERR_FAIL_COND(p_layers.is_empty());
  774. ERR_FAIL_COND(p_layered_type == RS::TEXTURE_LAYERED_CUBEMAP && p_layers.size() != 6);
  775. ERR_FAIL_COND_MSG(p_layered_type == RS::TEXTURE_LAYERED_CUBEMAP_ARRAY, "Cubemap Arrays are not supported in the Compatibility renderer.");
  776. const Ref<Image> &image = p_layers[0];
  777. {
  778. int valid_width = 0;
  779. int valid_height = 0;
  780. bool valid_mipmaps = false;
  781. Image::Format valid_format = Image::FORMAT_MAX;
  782. for (int i = 0; i < p_layers.size(); i++) {
  783. ERR_FAIL_COND(p_layers[i]->is_empty());
  784. if (i == 0) {
  785. valid_width = p_layers[i]->get_width();
  786. valid_height = p_layers[i]->get_height();
  787. valid_format = p_layers[i]->get_format();
  788. valid_mipmaps = p_layers[i]->has_mipmaps();
  789. } else {
  790. ERR_FAIL_COND(p_layers[i]->get_width() != valid_width);
  791. ERR_FAIL_COND(p_layers[i]->get_height() != valid_height);
  792. ERR_FAIL_COND(p_layers[i]->get_format() != valid_format);
  793. ERR_FAIL_COND(p_layers[i]->has_mipmaps() != valid_mipmaps);
  794. }
  795. }
  796. }
  797. Texture texture;
  798. texture.width = image->get_width();
  799. texture.height = image->get_height();
  800. texture.alloc_width = texture.width;
  801. texture.alloc_height = texture.height;
  802. texture.mipmaps = image->get_mipmap_count() + 1;
  803. texture.format = image->get_format();
  804. texture.type = Texture::TYPE_LAYERED;
  805. texture.layered_type = p_layered_type;
  806. texture.target = p_layered_type == RS::TEXTURE_LAYERED_CUBEMAP ? GL_TEXTURE_CUBE_MAP : GL_TEXTURE_2D_ARRAY;
  807. texture.layers = p_layers.size();
  808. _get_gl_image_and_format(Ref<Image>(), texture.format, texture.real_format, texture.gl_format_cache, texture.gl_internal_format_cache, texture.gl_type_cache, texture.compressed, false);
  809. texture.total_data_size = p_layers[0]->get_image_data_size(texture.width, texture.height, texture.format, texture.mipmaps) * texture.layers;
  810. texture.active = true;
  811. glGenTextures(1, &texture.tex_id);
  812. GLES3::Utilities::get_singleton()->texture_allocated_data(texture.tex_id, texture.total_data_size, "Texture Layered");
  813. texture_owner.initialize_rid(p_texture, texture);
  814. for (int i = 0; i < p_layers.size(); i++) {
  815. _texture_set_data(p_texture, p_layers[i], i, i == 0);
  816. }
  817. }
  818. void TextureStorage::texture_3d_initialize(RID p_texture, Image::Format p_format, int p_width, int p_height, int p_depth, bool p_mipmaps, const Vector<Ref<Image>> &p_data) {
  819. ERR_FAIL_COND(p_data.is_empty());
  820. Image::Image3DValidateError verr = Image::validate_3d_image(p_format, p_width, p_height, p_depth, p_mipmaps, p_data);
  821. ERR_FAIL_COND_MSG(verr != Image::VALIDATE_3D_OK, Image::get_3d_image_validation_error_text(verr));
  822. Ref<Image> image = p_data[0];
  823. int mipmap_count = 0;
  824. {
  825. Size2i prev_size;
  826. for (int i = 0; i < p_data.size(); i++) {
  827. Size2i img_size(p_data[i]->get_width(), p_data[i]->get_height());
  828. if (img_size != prev_size) {
  829. mipmap_count++;
  830. }
  831. prev_size = img_size;
  832. }
  833. }
  834. Texture texture;
  835. texture.width = p_width;
  836. texture.height = p_height;
  837. texture.depth = p_depth;
  838. texture.alloc_width = texture.width;
  839. texture.alloc_height = texture.height;
  840. texture.mipmaps = mipmap_count;
  841. texture.format = image->get_format();
  842. texture.type = Texture::TYPE_3D;
  843. texture.target = GL_TEXTURE_3D;
  844. _get_gl_image_and_format(Ref<Image>(), texture.format, texture.real_format, texture.gl_format_cache, texture.gl_internal_format_cache, texture.gl_type_cache, texture.compressed, false);
  845. texture.total_data_size = p_data[0]->get_image_data_size(texture.width, texture.height, texture.format, texture.mipmaps) * texture.depth;
  846. texture.active = true;
  847. glGenTextures(1, &texture.tex_id);
  848. GLES3::Utilities::get_singleton()->texture_allocated_data(texture.tex_id, texture.total_data_size, "Texture 3D");
  849. texture_owner.initialize_rid(p_texture, texture);
  850. _texture_set_3d_data(p_texture, p_data, true);
  851. }
  852. // Called internally when texture_proxy_create(p_base) is called.
  853. // Note: p_base is the root and p_texture is the proxy.
  854. void TextureStorage::texture_proxy_initialize(RID p_texture, RID p_base) {
  855. Texture *texture = texture_owner.get_or_null(p_base);
  856. ERR_FAIL_NULL(texture);
  857. Texture proxy_tex;
  858. proxy_tex.copy_from(*texture);
  859. proxy_tex.proxy_to = p_base;
  860. proxy_tex.is_render_target = false;
  861. proxy_tex.is_proxy = true;
  862. proxy_tex.proxies.clear();
  863. texture->proxies.push_back(p_texture);
  864. texture_owner.initialize_rid(p_texture, proxy_tex);
  865. }
  866. RID TextureStorage::texture_create_from_native_handle(RS::TextureType p_type, Image::Format p_format, uint64_t p_native_handle, int p_width, int p_height, int p_depth, int p_layers, RS::TextureLayeredType p_layered_type) {
  867. Texture texture;
  868. texture.active = true;
  869. texture.is_from_native_handle = true;
  870. switch (p_type) {
  871. case RS::TEXTURE_TYPE_2D: {
  872. texture.type = Texture::TYPE_2D;
  873. texture.target = GL_TEXTURE_2D;
  874. } break;
  875. case RS::TEXTURE_TYPE_3D: {
  876. texture.type = Texture::TYPE_3D;
  877. texture.target = GL_TEXTURE_3D;
  878. } break;
  879. case RS::TEXTURE_TYPE_LAYERED: {
  880. texture.type = Texture::TYPE_LAYERED;
  881. texture.target = GL_TEXTURE_2D_ARRAY;
  882. } break;
  883. }
  884. texture.real_format = texture.format = p_format;
  885. texture.tex_id = p_native_handle;
  886. texture.alloc_width = texture.width = p_width;
  887. texture.alloc_height = texture.height = p_height;
  888. texture.depth = p_depth;
  889. texture.layers = p_layers;
  890. texture.layered_type = p_layered_type;
  891. return texture_owner.make_rid(texture);
  892. }
  893. void TextureStorage::texture_2d_update(RID p_texture, const Ref<Image> &p_image, int p_layer) {
  894. texture_set_data(p_texture, p_image, p_layer);
  895. Texture *tex = texture_owner.get_or_null(p_texture);
  896. ERR_FAIL_NULL(tex);
  897. GLES3::Utilities::get_singleton()->texture_resize_data(tex->tex_id, tex->total_data_size);
  898. #ifdef TOOLS_ENABLED
  899. tex->image_cache_2d.unref();
  900. #endif
  901. }
  902. void TextureStorage::texture_3d_update(RID p_texture, const Vector<Ref<Image>> &p_data) {
  903. Texture *tex = texture_owner.get_or_null(p_texture);
  904. ERR_FAIL_NULL(tex);
  905. ERR_FAIL_COND(tex->type != Texture::TYPE_3D);
  906. Image::Image3DValidateError verr = Image::validate_3d_image(tex->format, tex->width, tex->height, tex->depth, tex->mipmaps > 1, p_data);
  907. ERR_FAIL_COND_MSG(verr != Image::VALIDATE_3D_OK, Image::get_3d_image_validation_error_text(verr));
  908. _texture_set_3d_data(p_texture, p_data, false);
  909. GLES3::Utilities::get_singleton()->texture_resize_data(tex->tex_id, tex->total_data_size);
  910. }
  911. void TextureStorage::texture_external_update(RID p_texture, int p_width, int p_height, uint64_t p_external_buffer) {
  912. Texture *tex = texture_owner.get_or_null(p_texture);
  913. ERR_FAIL_NULL(tex);
  914. tex->alloc_width = tex->width = p_width;
  915. tex->alloc_height = tex->height = p_height;
  916. #ifdef ANDROID_ENABLED
  917. if (tex->target == _GL_TEXTURE_EXTERNAL_OES && p_external_buffer) {
  918. glBindTexture(_GL_TEXTURE_EXTERNAL_OES, tex->tex_id);
  919. GLES3::Config::get_singleton()->eglEGLImageTargetTexture2DOES(_GL_TEXTURE_EXTERNAL_OES, reinterpret_cast<void *>(p_external_buffer));
  920. glBindTexture(_GL_TEXTURE_EXTERNAL_OES, 0);
  921. }
  922. #endif
  923. }
  924. void TextureStorage::texture_proxy_update(RID p_texture, RID p_proxy_to) {
  925. Texture *tex = texture_owner.get_or_null(p_texture);
  926. ERR_FAIL_NULL(tex);
  927. ERR_FAIL_COND(!tex->is_proxy);
  928. Texture *proxy_to = texture_owner.get_or_null(p_proxy_to);
  929. ERR_FAIL_NULL(proxy_to);
  930. ERR_FAIL_COND(proxy_to->is_proxy);
  931. if (tex->proxy_to.is_valid()) {
  932. Texture *prev_tex = texture_owner.get_or_null(tex->proxy_to);
  933. ERR_FAIL_NULL(prev_tex);
  934. prev_tex->proxies.erase(p_texture);
  935. }
  936. *tex = *proxy_to;
  937. tex->proxy_to = p_proxy_to;
  938. tex->is_render_target = false;
  939. tex->is_proxy = true;
  940. tex->proxies.clear();
  941. tex->canvas_texture = nullptr;
  942. tex->tex_id = 0;
  943. proxy_to->proxies.push_back(p_texture);
  944. }
  945. void TextureStorage::texture_2d_placeholder_initialize(RID p_texture) {
  946. texture_2d_initialize(p_texture, texture_2d_placeholder);
  947. }
  948. void TextureStorage::texture_2d_layered_placeholder_initialize(RID p_texture, RS::TextureLayeredType p_layered_type) {
  949. if (p_layered_type == RS::TEXTURE_LAYERED_2D_ARRAY) {
  950. texture_2d_layered_initialize(p_texture, texture_2d_array_placeholder, p_layered_type);
  951. } else {
  952. texture_2d_layered_initialize(p_texture, cubemap_placeholder, p_layered_type);
  953. }
  954. }
  955. void TextureStorage::texture_3d_placeholder_initialize(RID p_texture) {
  956. texture_3d_initialize(p_texture, Image::FORMAT_RGBA8, 4, 4, 4, false, texture_3d_placeholder);
  957. }
  958. Ref<Image> TextureStorage::texture_2d_get(RID p_texture) const {
  959. Texture *texture = texture_owner.get_or_null(p_texture);
  960. ERR_FAIL_NULL_V(texture, Ref<Image>());
  961. #ifdef TOOLS_ENABLED
  962. if (texture->image_cache_2d.is_valid() && !texture->is_render_target) {
  963. return texture->image_cache_2d;
  964. }
  965. #endif
  966. Ref<Image> image;
  967. #ifdef GL_API_ENABLED
  968. if (RasterizerGLES3::is_gles_over_gl()) {
  969. // OpenGL 3.3 supports glGetTexImage which is faster and simpler than glReadPixels.
  970. // It also allows for reading compressed textures, mipmaps, and more formats.
  971. Vector<uint8_t> data;
  972. int64_t data_size = Image::get_image_data_size(texture->alloc_width, texture->alloc_height, texture->real_format, texture->mipmaps > 1);
  973. data.resize(data_size * 2); // Add some memory at the end, just in case for buggy drivers.
  974. uint8_t *w = data.ptrw();
  975. glActiveTexture(GL_TEXTURE0);
  976. glBindTexture(texture->target, texture->tex_id);
  977. glBindBuffer(GL_PIXEL_PACK_BUFFER, 0);
  978. for (int i = 0; i < texture->mipmaps; i++) {
  979. int64_t ofs = Image::get_image_mipmap_offset(texture->alloc_width, texture->alloc_height, texture->real_format, i);
  980. if (texture->compressed) {
  981. glPixelStorei(GL_PACK_ALIGNMENT, 4);
  982. glGetCompressedTexImage(texture->target, i, &w[ofs]);
  983. } else {
  984. glPixelStorei(GL_PACK_ALIGNMENT, 1);
  985. glGetTexImage(texture->target, i, texture->gl_format_cache, texture->gl_type_cache, &w[ofs]);
  986. }
  987. }
  988. data.resize(data_size);
  989. ERR_FAIL_COND_V(data.is_empty(), Ref<Image>());
  990. image = Image::create_from_data(texture->alloc_width, texture->alloc_height, texture->mipmaps > 1, texture->real_format, data);
  991. if (image->is_empty()) {
  992. const String &path_str = texture->path.is_empty() ? "with no path" : vformat("with path '%s'", texture->path);
  993. ERR_FAIL_V_MSG(Ref<Image>(), vformat("Texture %s has no data.", path_str));
  994. }
  995. if (texture->format != texture->real_format && !Image::is_format_compressed(texture->real_format)) {
  996. image->convert(texture->format);
  997. }
  998. }
  999. #endif // GL_API_ENABLED
  1000. #ifdef GLES_API_ENABLED
  1001. if (!RasterizerGLES3::is_gles_over_gl()) {
  1002. Vector<uint8_t> data;
  1003. // On web and mobile we always read an RGBA8 image with no mipmaps.
  1004. int64_t data_size = Image::get_image_data_size(texture->alloc_width, texture->alloc_height, Image::FORMAT_RGBA8, false);
  1005. data.resize(data_size * 2); // Add some memory at the end, just in case for buggy drivers.
  1006. uint8_t *w = data.ptrw();
  1007. GLuint temp_framebuffer;
  1008. glGenFramebuffers(1, &temp_framebuffer);
  1009. GLuint temp_color_texture;
  1010. glGenTextures(1, &temp_color_texture);
  1011. glBindFramebuffer(GL_FRAMEBUFFER, temp_framebuffer);
  1012. glBindTexture(GL_TEXTURE_2D, temp_color_texture);
  1013. glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, texture->alloc_width, texture->alloc_height, 0, GL_RGBA, GL_UNSIGNED_BYTE, nullptr);
  1014. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  1015. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  1016. glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, temp_color_texture, 0);
  1017. glDepthMask(GL_FALSE);
  1018. glDisable(GL_DEPTH_TEST);
  1019. glDisable(GL_CULL_FACE);
  1020. glDisable(GL_BLEND);
  1021. glDepthFunc(GL_GEQUAL);
  1022. glColorMask(1, 1, 1, 1);
  1023. glActiveTexture(GL_TEXTURE0);
  1024. glBindTexture(GL_TEXTURE_2D, texture->tex_id);
  1025. glViewport(0, 0, texture->alloc_width, texture->alloc_height);
  1026. glClearColor(0.0, 0.0, 0.0, 0.0);
  1027. glClear(GL_COLOR_BUFFER_BIT);
  1028. CopyEffects::get_singleton()->copy_to_rect(Rect2i(0, 0, 1.0, 1.0));
  1029. glReadPixels(0, 0, texture->alloc_width, texture->alloc_height, GL_RGBA, GL_UNSIGNED_BYTE, &w[0]);
  1030. glBindFramebuffer(GL_FRAMEBUFFER, GLES3::TextureStorage::system_fbo);
  1031. glDeleteTextures(1, &temp_color_texture);
  1032. glDeleteFramebuffers(1, &temp_framebuffer);
  1033. data.resize(data_size);
  1034. ERR_FAIL_COND_V(data.is_empty(), Ref<Image>());
  1035. image = Image::create_from_data(texture->alloc_width, texture->alloc_height, false, Image::FORMAT_RGBA8, data);
  1036. if (image->is_empty()) {
  1037. const String &path_str = texture->path.is_empty() ? "with no path" : vformat("with path '%s'", texture->path);
  1038. ERR_FAIL_V_MSG(Ref<Image>(), vformat("Texture %s has no data.", path_str));
  1039. }
  1040. if (texture->format != Image::FORMAT_RGBA8 && !Image::is_format_compressed(texture->format)) {
  1041. image->convert(texture->format);
  1042. }
  1043. if (texture->mipmaps > 1) {
  1044. image->generate_mipmaps();
  1045. }
  1046. }
  1047. #endif // GLES_API_ENABLED
  1048. #ifdef TOOLS_ENABLED
  1049. if (Engine::get_singleton()->is_editor_hint() && !texture->is_render_target) {
  1050. texture->image_cache_2d = image;
  1051. }
  1052. #endif
  1053. return image;
  1054. }
  1055. Ref<Image> TextureStorage::texture_2d_layer_get(RID p_texture, int p_layer) const {
  1056. Texture *texture = texture_owner.get_or_null(p_texture);
  1057. ERR_FAIL_NULL_V(texture, Ref<Image>());
  1058. Vector<uint8_t> data;
  1059. int64_t data_size = Image::get_image_data_size(texture->alloc_width, texture->alloc_height, Image::FORMAT_RGBA8, false);
  1060. data.resize(data_size * 2); //add some memory at the end, just in case for buggy drivers
  1061. uint8_t *w = data.ptrw();
  1062. GLuint temp_framebuffer;
  1063. glGenFramebuffers(1, &temp_framebuffer);
  1064. GLuint temp_color_texture;
  1065. glGenTextures(1, &temp_color_texture);
  1066. glBindFramebuffer(GL_FRAMEBUFFER, temp_framebuffer);
  1067. glBindTexture(GL_TEXTURE_2D, temp_color_texture);
  1068. glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, texture->alloc_width, texture->alloc_height, 0, GL_RGBA, GL_UNSIGNED_BYTE, nullptr);
  1069. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  1070. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  1071. glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, temp_color_texture, 0);
  1072. glDepthMask(GL_FALSE);
  1073. glDisable(GL_DEPTH_TEST);
  1074. glDisable(GL_CULL_FACE);
  1075. glDisable(GL_BLEND);
  1076. glDepthFunc(GL_LEQUAL);
  1077. glColorMask(1, 1, 1, 1);
  1078. glActiveTexture(GL_TEXTURE0);
  1079. glBindTexture(GL_TEXTURE_2D_ARRAY, texture->tex_id);
  1080. glViewport(0, 0, texture->alloc_width, texture->alloc_height);
  1081. glClearColor(0.0, 0.0, 0.0, 0.0);
  1082. glClear(GL_COLOR_BUFFER_BIT);
  1083. CopyEffects::get_singleton()->copy_to_rect_3d(Rect2i(0, 0, 1, 1), p_layer, Texture::TYPE_LAYERED);
  1084. glReadPixels(0, 0, texture->alloc_width, texture->alloc_height, GL_RGBA, GL_UNSIGNED_BYTE, &w[0]);
  1085. glBindFramebuffer(GL_FRAMEBUFFER, GLES3::TextureStorage::system_fbo);
  1086. glDeleteTextures(1, &temp_color_texture);
  1087. glDeleteFramebuffers(1, &temp_framebuffer);
  1088. data.resize(data_size);
  1089. ERR_FAIL_COND_V(data.is_empty(), Ref<Image>());
  1090. Ref<Image> image = Image::create_from_data(texture->width, texture->height, false, Image::FORMAT_RGBA8, data);
  1091. if (image->is_empty()) {
  1092. const String &path_str = texture->path.is_empty() ? "with no path" : vformat("with path '%s'", texture->path);
  1093. ERR_FAIL_V_MSG(Ref<Image>(), vformat("Texture %s has no data.", path_str));
  1094. }
  1095. if (texture->format != Image::FORMAT_RGBA8 && !Image::is_format_compressed(texture->format)) {
  1096. image->convert(texture->format);
  1097. }
  1098. if (texture->mipmaps > 1) {
  1099. image->generate_mipmaps();
  1100. }
  1101. return image;
  1102. }
  1103. Vector<Ref<Image>> TextureStorage::_texture_3d_read_framebuffer(GLES3::Texture *p_texture) const {
  1104. ERR_FAIL_NULL_V(p_texture, Vector<Ref<Image>>());
  1105. Vector<Ref<Image>> ret;
  1106. Vector<uint8_t> data;
  1107. int width = p_texture->width;
  1108. int height = p_texture->height;
  1109. int depth = p_texture->depth;
  1110. for (int mipmap_level = 0; mipmap_level < p_texture->mipmaps; mipmap_level++) {
  1111. int64_t data_size = Image::get_image_data_size(width, height, Image::FORMAT_RGBA8, false);
  1112. glViewport(0, 0, width, height);
  1113. glClearColor(0.0, 0.0, 0.0, 0.0);
  1114. glClear(GL_COLOR_BUFFER_BIT);
  1115. for (int layer = 0; layer < depth; layer++) {
  1116. data.resize(data_size * 2); //add some memory at the end, just in case for buggy drivers
  1117. uint8_t *w = data.ptrw();
  1118. float layer_f = layer / float(depth);
  1119. CopyEffects::get_singleton()->copy_to_rect_3d(Rect2i(0, 0, 1, 1), layer_f, Texture::TYPE_3D, mipmap_level);
  1120. glReadPixels(0, 0, width, height, GL_RGBA, GL_UNSIGNED_BYTE, &w[0]);
  1121. data.resize(data_size);
  1122. ERR_FAIL_COND_V(data.is_empty(), Vector<Ref<Image>>());
  1123. Ref<Image> img = Image::create_from_data(width, height, false, Image::FORMAT_RGBA8, data);
  1124. ERR_FAIL_COND_V(img->is_empty(), Vector<Ref<Image>>());
  1125. if (p_texture->format != Image::FORMAT_RGBA8 && !Image::is_format_compressed(p_texture->format)) {
  1126. img->convert(p_texture->format);
  1127. }
  1128. ret.push_back(img);
  1129. }
  1130. width = MAX(1, width >> 1);
  1131. height = MAX(1, height >> 1);
  1132. depth = MAX(1, depth >> 1);
  1133. }
  1134. return ret;
  1135. }
  1136. Vector<Ref<Image>> TextureStorage::texture_3d_get(RID p_texture) const {
  1137. Texture *texture = texture_owner.get_or_null(p_texture);
  1138. ERR_FAIL_NULL_V(texture, Vector<Ref<Image>>());
  1139. ERR_FAIL_COND_V(texture->type != Texture::TYPE_3D, Vector<Ref<Image>>());
  1140. #ifdef TOOLS_ENABLED
  1141. if (!texture->image_cache_3d.is_empty() && !texture->is_render_target) {
  1142. return texture->image_cache_3d;
  1143. }
  1144. #endif
  1145. GLuint temp_framebuffer;
  1146. glGenFramebuffers(1, &temp_framebuffer);
  1147. GLuint temp_color_texture;
  1148. glGenTextures(1, &temp_color_texture);
  1149. glBindFramebuffer(GL_FRAMEBUFFER, temp_framebuffer);
  1150. glBindTexture(GL_TEXTURE_2D, temp_color_texture);
  1151. glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, texture->alloc_width, texture->alloc_height, 0, GL_RGBA, GL_UNSIGNED_BYTE, nullptr);
  1152. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  1153. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  1154. glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, temp_color_texture, 0);
  1155. glDepthMask(GL_FALSE);
  1156. glDisable(GL_DEPTH_TEST);
  1157. glDisable(GL_CULL_FACE);
  1158. glDisable(GL_BLEND);
  1159. glDepthFunc(GL_LEQUAL);
  1160. glColorMask(1, 1, 1, 1);
  1161. glActiveTexture(GL_TEXTURE0);
  1162. glBindTexture(GL_TEXTURE_3D, texture->tex_id);
  1163. Vector<Ref<Image>> ret = _texture_3d_read_framebuffer(texture);
  1164. glBindFramebuffer(GL_FRAMEBUFFER, GLES3::TextureStorage::system_fbo);
  1165. glDeleteTextures(1, &temp_color_texture);
  1166. glDeleteFramebuffers(1, &temp_framebuffer);
  1167. #ifdef TOOLS_ENABLED
  1168. if (Engine::get_singleton()->is_editor_hint() && !texture->is_render_target) {
  1169. texture->image_cache_3d = ret;
  1170. }
  1171. #endif
  1172. return ret;
  1173. }
  1174. void TextureStorage::texture_replace(RID p_texture, RID p_by_texture) {
  1175. Texture *tex_to = texture_owner.get_or_null(p_texture);
  1176. ERR_FAIL_NULL(tex_to);
  1177. ERR_FAIL_COND(tex_to->is_proxy); //can't replace proxy
  1178. Texture *tex_from = texture_owner.get_or_null(p_by_texture);
  1179. ERR_FAIL_NULL(tex_from);
  1180. ERR_FAIL_COND(tex_from->is_proxy); //can't replace proxy
  1181. if (tex_to == tex_from) {
  1182. return;
  1183. }
  1184. if (tex_to->canvas_texture) {
  1185. memdelete(tex_to->canvas_texture);
  1186. tex_to->canvas_texture = nullptr;
  1187. }
  1188. if (tex_to->tex_id) {
  1189. GLES3::Utilities::get_singleton()->texture_free_data(tex_to->tex_id);
  1190. tex_to->tex_id = 0;
  1191. }
  1192. Vector<RID> proxies_to_update = tex_to->proxies;
  1193. Vector<RID> proxies_to_redirect = tex_from->proxies;
  1194. *tex_to = *tex_from;
  1195. tex_to->proxies = proxies_to_update; //restore proxies, so they can be updated
  1196. if (tex_to->canvas_texture) {
  1197. tex_to->canvas_texture->diffuse = p_texture; //update
  1198. }
  1199. for (int i = 0; i < proxies_to_update.size(); i++) {
  1200. texture_proxy_update(proxies_to_update[i], p_texture);
  1201. }
  1202. for (int i = 0; i < proxies_to_redirect.size(); i++) {
  1203. texture_proxy_update(proxies_to_redirect[i], p_texture);
  1204. }
  1205. //delete last, so proxies can be updated
  1206. texture_owner.free(p_by_texture);
  1207. texture_atlas_mark_dirty_on_texture(p_texture);
  1208. }
  1209. void TextureStorage::texture_set_size_override(RID p_texture, int p_width, int p_height) {
  1210. Texture *texture = texture_owner.get_or_null(p_texture);
  1211. ERR_FAIL_NULL(texture);
  1212. ERR_FAIL_COND(texture->is_render_target);
  1213. ERR_FAIL_COND(p_width <= 0 || p_width > 16384);
  1214. ERR_FAIL_COND(p_height <= 0 || p_height > 16384);
  1215. //real texture size is in alloc width and height
  1216. texture->width = p_width;
  1217. texture->height = p_height;
  1218. }
  1219. void TextureStorage::texture_set_path(RID p_texture, const String &p_path) {
  1220. Texture *texture = texture_owner.get_or_null(p_texture);
  1221. ERR_FAIL_NULL(texture);
  1222. texture->path = p_path;
  1223. }
  1224. String TextureStorage::texture_get_path(RID p_texture) const {
  1225. Texture *texture = texture_owner.get_or_null(p_texture);
  1226. ERR_FAIL_NULL_V(texture, "");
  1227. return texture->path;
  1228. }
  1229. void TextureStorage::texture_set_detect_3d_callback(RID p_texture, RS::TextureDetectCallback p_callback, void *p_userdata) {
  1230. Texture *texture = texture_owner.get_or_null(p_texture);
  1231. ERR_FAIL_NULL(texture);
  1232. texture->detect_3d_callback = p_callback;
  1233. texture->detect_3d_callback_ud = p_userdata;
  1234. }
  1235. void TextureStorage::texture_set_detect_srgb_callback(RID p_texture, RS::TextureDetectCallback p_callback, void *p_userdata) {
  1236. }
  1237. void TextureStorage::texture_set_detect_normal_callback(RID p_texture, RS::TextureDetectCallback p_callback, void *p_userdata) {
  1238. Texture *texture = texture_owner.get_or_null(p_texture);
  1239. ERR_FAIL_NULL(texture);
  1240. texture->detect_normal_callback = p_callback;
  1241. texture->detect_normal_callback_ud = p_userdata;
  1242. }
  1243. void TextureStorage::texture_set_detect_roughness_callback(RID p_texture, RS::TextureDetectRoughnessCallback p_callback, void *p_userdata) {
  1244. Texture *texture = texture_owner.get_or_null(p_texture);
  1245. ERR_FAIL_NULL(texture);
  1246. texture->detect_roughness_callback = p_callback;
  1247. texture->detect_roughness_callback_ud = p_userdata;
  1248. }
  1249. void TextureStorage::texture_debug_usage(List<RS::TextureInfo> *r_info) {
  1250. for (const RID &rid : texture_owner.get_owned_list()) {
  1251. Texture *t = texture_owner.get_or_null(rid);
  1252. if (!t) {
  1253. continue;
  1254. }
  1255. RS::TextureInfo tinfo;
  1256. tinfo.path = t->path;
  1257. tinfo.format = t->format;
  1258. tinfo.width = t->alloc_width;
  1259. tinfo.height = t->alloc_height;
  1260. tinfo.bytes = t->total_data_size;
  1261. tinfo.type = static_cast<RenderingServer::TextureType>(t->type);
  1262. switch (t->type) {
  1263. case Texture::TYPE_3D:
  1264. tinfo.depth = t->depth;
  1265. break;
  1266. case Texture::TYPE_LAYERED:
  1267. tinfo.depth = t->layers;
  1268. break;
  1269. default:
  1270. tinfo.depth = 0;
  1271. break;
  1272. }
  1273. r_info->push_back(tinfo);
  1274. }
  1275. }
  1276. void TextureStorage::texture_set_force_redraw_if_visible(RID p_texture, bool p_enable) {
  1277. Texture *texture = texture_owner.get_or_null(p_texture);
  1278. ERR_FAIL_NULL(texture);
  1279. texture->redraw_if_visible = p_enable;
  1280. }
  1281. Size2 TextureStorage::texture_size_with_proxy(RID p_texture) {
  1282. const Texture *texture = texture_owner.get_or_null(p_texture);
  1283. ERR_FAIL_NULL_V(texture, Size2());
  1284. if (texture->is_proxy) {
  1285. const Texture *proxy = texture_owner.get_or_null(texture->proxy_to);
  1286. return Size2(proxy->width, proxy->height);
  1287. } else {
  1288. return Size2(texture->width, texture->height);
  1289. }
  1290. }
  1291. void TextureStorage::texture_rd_initialize(RID p_texture, const RID &p_rd_texture, const RS::TextureLayeredType p_layer_type) {
  1292. }
  1293. RID TextureStorage::texture_get_rd_texture(RID p_texture, bool p_srgb) const {
  1294. return RID();
  1295. }
  1296. uint64_t TextureStorage::texture_get_native_handle(RID p_texture, bool p_srgb) const {
  1297. const Texture *texture = texture_owner.get_or_null(p_texture);
  1298. ERR_FAIL_NULL_V(texture, 0);
  1299. return texture->tex_id;
  1300. }
  1301. void TextureStorage::texture_set_data(RID p_texture, const Ref<Image> &p_image, int p_layer) {
  1302. _texture_set_data(p_texture, p_image, p_layer, false);
  1303. }
  1304. void TextureStorage::_texture_set_data(RID p_texture, const Ref<Image> &p_image, int p_layer, bool p_initialize) {
  1305. Texture *texture = texture_owner.get_or_null(p_texture);
  1306. ERR_FAIL_NULL(texture);
  1307. if (texture->target == GL_TEXTURE_3D) {
  1308. // Target is set to a 3D texture or array texture, exit early to avoid spamming errors
  1309. return;
  1310. }
  1311. ERR_FAIL_COND(!texture->active);
  1312. ERR_FAIL_COND(texture->is_render_target);
  1313. ERR_FAIL_COND(p_image.is_null());
  1314. ERR_FAIL_COND(texture->format != p_image->get_format());
  1315. ERR_FAIL_COND(!p_image->get_width());
  1316. ERR_FAIL_COND(!p_image->get_height());
  1317. GLenum type;
  1318. GLenum format;
  1319. GLenum internal_format;
  1320. bool compressed = false;
  1321. bool needs_decompress = texture->resize_to_po2;
  1322. // Support for RGTC-compressed Texture Arrays isn't mandated by GLES3/WebGL.
  1323. if (!RasterizerGLES3::is_gles_over_gl() && texture->target == GL_TEXTURE_2D_ARRAY) {
  1324. if (p_image->get_format() == Image::FORMAT_RGTC_R || p_image->get_format() == Image::FORMAT_RGTC_RG) {
  1325. needs_decompress = true;
  1326. }
  1327. }
  1328. Image::Format real_format;
  1329. Ref<Image> img = _get_gl_image_and_format(p_image, p_image->get_format(), real_format, format, internal_format, type, compressed, needs_decompress);
  1330. ERR_FAIL_COND(img.is_null());
  1331. if (texture->resize_to_po2) {
  1332. if (p_image->is_compressed()) {
  1333. ERR_PRINT("Texture '" + texture->path + "' is required to be a power of 2 because it uses either mipmaps or repeat, so it was decompressed. This will hurt performance and memory usage.");
  1334. }
  1335. if (img == p_image) {
  1336. img = img->duplicate();
  1337. }
  1338. img->resize_to_po2(false);
  1339. }
  1340. GLenum blit_target = (texture->target == GL_TEXTURE_CUBE_MAP) ? _cube_side_enum[p_layer] : texture->target;
  1341. Vector<uint8_t> read = img->get_data();
  1342. glActiveTexture(GL_TEXTURE0);
  1343. glBindTexture(texture->target, texture->tex_id);
  1344. _texture_set_swizzle(texture, real_format);
  1345. int mipmaps = img->has_mipmaps() ? img->get_mipmap_count() + 1 : 1;
  1346. // Set filtering and repeat state to default.
  1347. if (mipmaps > 1) {
  1348. texture->gl_set_filter(RS::CANVAS_ITEM_TEXTURE_FILTER_NEAREST_WITH_MIPMAPS);
  1349. } else {
  1350. texture->gl_set_filter(RS::CANVAS_ITEM_TEXTURE_FILTER_NEAREST);
  1351. }
  1352. texture->gl_set_repeat(RS::CANVAS_ITEM_TEXTURE_REPEAT_ENABLED);
  1353. int w = img->get_width();
  1354. int h = img->get_height();
  1355. int tsize = 0;
  1356. for (int i = 0; i < mipmaps; i++) {
  1357. int64_t size, ofs;
  1358. img->get_mipmap_offset_and_size(i, ofs, size);
  1359. if (compressed) {
  1360. glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
  1361. if (texture->target == GL_TEXTURE_2D_ARRAY) {
  1362. if (p_initialize) {
  1363. glCompressedTexImage3D(GL_TEXTURE_2D_ARRAY, i, internal_format, w, h, texture->layers, 0, size * texture->layers, nullptr);
  1364. }
  1365. glCompressedTexSubImage3D(GL_TEXTURE_2D_ARRAY, i, 0, 0, p_layer, w, h, 1, internal_format, size, &read[ofs]);
  1366. } else {
  1367. glCompressedTexImage2D(blit_target, i, internal_format, w, h, 0, size, &read[ofs]);
  1368. }
  1369. } else {
  1370. glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
  1371. if (texture->target == GL_TEXTURE_2D_ARRAY) {
  1372. if (p_initialize) {
  1373. glTexImage3D(GL_TEXTURE_2D_ARRAY, i, internal_format, w, h, texture->layers, 0, format, type, nullptr);
  1374. }
  1375. glTexSubImage3D(GL_TEXTURE_2D_ARRAY, i, 0, 0, p_layer, w, h, 1, format, type, &read[ofs]);
  1376. } else {
  1377. glTexImage2D(blit_target, i, internal_format, w, h, 0, format, type, &read[ofs]);
  1378. }
  1379. }
  1380. tsize += size;
  1381. w = MAX(1, w >> 1);
  1382. h = MAX(1, h >> 1);
  1383. }
  1384. if (texture->target == GL_TEXTURE_CUBE_MAP || texture->target == GL_TEXTURE_2D_ARRAY) {
  1385. texture->total_data_size = tsize * texture->layers;
  1386. } else {
  1387. texture->total_data_size = tsize;
  1388. }
  1389. texture->stored_cube_sides |= (1 << p_layer);
  1390. texture->mipmaps = mipmaps;
  1391. }
  1392. void TextureStorage::_texture_set_3d_data(RID p_texture, const Vector<Ref<Image>> &p_data, bool p_initialize) {
  1393. Texture *texture = texture_owner.get_or_null(p_texture);
  1394. ERR_FAIL_NULL(texture);
  1395. ERR_FAIL_COND(!texture->active);
  1396. ERR_FAIL_COND(texture->is_render_target);
  1397. ERR_FAIL_COND(texture->target != GL_TEXTURE_3D);
  1398. ERR_FAIL_COND(p_data.is_empty());
  1399. GLenum type;
  1400. GLenum format;
  1401. GLenum internal_format;
  1402. bool compressed = false;
  1403. Image::Format real_format;
  1404. Ref<Image> img = _get_gl_image_and_format(p_data[0], p_data[0]->get_format(), real_format, format, internal_format, type, compressed, texture->resize_to_po2);
  1405. ERR_FAIL_COND(img.is_null());
  1406. ERR_FAIL_COND_MSG(compressed, "Compressed 3D textures are not supported in the Compatibility renderer.");
  1407. glActiveTexture(GL_TEXTURE0);
  1408. glBindTexture(texture->target, texture->tex_id);
  1409. _texture_set_swizzle(texture, texture->real_format);
  1410. // Set filtering and repeat state to default.
  1411. if (texture->mipmaps > 1) {
  1412. texture->gl_set_filter(RS::CANVAS_ITEM_TEXTURE_FILTER_NEAREST_WITH_MIPMAPS);
  1413. } else {
  1414. texture->gl_set_filter(RS::CANVAS_ITEM_TEXTURE_FILTER_NEAREST);
  1415. }
  1416. texture->gl_set_repeat(RS::CANVAS_ITEM_TEXTURE_REPEAT_ENABLED);
  1417. Vector<Ref<Image>> images;
  1418. images.resize(p_data.size());
  1419. for (int i = 0; i < p_data.size(); i++) {
  1420. Ref<Image> image = p_data[i];
  1421. if (image->get_format() != texture->format) {
  1422. image = image->duplicate();
  1423. image->convert(texture->format);
  1424. }
  1425. images.write[i] = image;
  1426. }
  1427. glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
  1428. int all_data_size = 0;
  1429. int mipmap_level = 0;
  1430. int layer = 0;
  1431. int depth = texture->depth;
  1432. Size2i prev_size(images[0]->get_width(), images[0]->get_height());
  1433. for (int i = 0; i < images.size(); i++) {
  1434. Ref<Image> image = images[i];
  1435. Size2i img_size(image->get_width(), image->get_height());
  1436. if (img_size != prev_size) {
  1437. mipmap_level++;
  1438. depth = MAX(1, depth >> 1);
  1439. layer = 0;
  1440. }
  1441. prev_size = img_size;
  1442. all_data_size += image->get_data().size();
  1443. if (layer == 0 && p_initialize) {
  1444. glTexImage3D(GL_TEXTURE_3D, mipmap_level, internal_format, img_size.width, img_size.height, depth, 0, format, type, nullptr);
  1445. }
  1446. glTexSubImage3D(GL_TEXTURE_3D, mipmap_level, 0, 0, layer, img_size.width, img_size.height, 1, format, type, image->get_data().ptr());
  1447. layer++;
  1448. }
  1449. texture->total_data_size = all_data_size;
  1450. texture->mipmaps = mipmap_level + 1;
  1451. #ifdef TOOLS_ENABLED
  1452. if (Engine::get_singleton()->is_editor_hint() && !texture->is_render_target) {
  1453. texture->image_cache_3d = images;
  1454. }
  1455. #endif
  1456. }
  1457. void TextureStorage::_texture_set_swizzle(GLES3::Texture *p_texture, Image::Format p_real_format) {
  1458. #ifndef WEB_ENABLED
  1459. switch (p_texture->format) {
  1460. case Image::FORMAT_L8: {
  1461. if (RasterizerGLES3::is_gles_over_gl()) {
  1462. glTexParameteri(p_texture->target, GL_TEXTURE_SWIZZLE_R, GL_RED);
  1463. glTexParameteri(p_texture->target, GL_TEXTURE_SWIZZLE_G, GL_RED);
  1464. glTexParameteri(p_texture->target, GL_TEXTURE_SWIZZLE_B, GL_RED);
  1465. glTexParameteri(p_texture->target, GL_TEXTURE_SWIZZLE_A, GL_ONE);
  1466. } else {
  1467. glTexParameteri(p_texture->target, GL_TEXTURE_SWIZZLE_R, GL_RED);
  1468. glTexParameteri(p_texture->target, GL_TEXTURE_SWIZZLE_G, GL_GREEN);
  1469. glTexParameteri(p_texture->target, GL_TEXTURE_SWIZZLE_B, GL_BLUE);
  1470. glTexParameteri(p_texture->target, GL_TEXTURE_SWIZZLE_A, GL_ALPHA);
  1471. }
  1472. } break;
  1473. case Image::FORMAT_LA8: {
  1474. if (RasterizerGLES3::is_gles_over_gl()) {
  1475. glTexParameteri(p_texture->target, GL_TEXTURE_SWIZZLE_R, GL_RED);
  1476. glTexParameteri(p_texture->target, GL_TEXTURE_SWIZZLE_G, GL_RED);
  1477. glTexParameteri(p_texture->target, GL_TEXTURE_SWIZZLE_B, GL_RED);
  1478. glTexParameteri(p_texture->target, GL_TEXTURE_SWIZZLE_A, GL_GREEN);
  1479. } else {
  1480. glTexParameteri(p_texture->target, GL_TEXTURE_SWIZZLE_R, GL_RED);
  1481. glTexParameteri(p_texture->target, GL_TEXTURE_SWIZZLE_G, GL_GREEN);
  1482. glTexParameteri(p_texture->target, GL_TEXTURE_SWIZZLE_B, GL_BLUE);
  1483. glTexParameteri(p_texture->target, GL_TEXTURE_SWIZZLE_A, GL_ALPHA);
  1484. }
  1485. } break;
  1486. case Image::FORMAT_ETC2_RA_AS_RG:
  1487. case Image::FORMAT_DXT5_RA_AS_RG: {
  1488. glTexParameteri(p_texture->target, GL_TEXTURE_SWIZZLE_R, GL_RED);
  1489. if (p_texture->format == p_real_format) {
  1490. // Swizzle RA from compressed texture into RG.
  1491. glTexParameteri(p_texture->target, GL_TEXTURE_SWIZZLE_G, GL_ALPHA);
  1492. } else {
  1493. // Converted textures are already in RG, leave as-is.
  1494. glTexParameteri(p_texture->target, GL_TEXTURE_SWIZZLE_G, GL_GREEN);
  1495. }
  1496. glTexParameteri(p_texture->target, GL_TEXTURE_SWIZZLE_B, GL_ZERO);
  1497. glTexParameteri(p_texture->target, GL_TEXTURE_SWIZZLE_A, GL_ONE);
  1498. } break;
  1499. default: {
  1500. glTexParameteri(p_texture->target, GL_TEXTURE_SWIZZLE_R, GL_RED);
  1501. glTexParameteri(p_texture->target, GL_TEXTURE_SWIZZLE_G, GL_GREEN);
  1502. glTexParameteri(p_texture->target, GL_TEXTURE_SWIZZLE_B, GL_BLUE);
  1503. glTexParameteri(p_texture->target, GL_TEXTURE_SWIZZLE_A, GL_ALPHA);
  1504. } break;
  1505. }
  1506. #endif // WEB_ENABLED
  1507. }
  1508. Image::Format TextureStorage::texture_get_format(RID p_texture) const {
  1509. Texture *texture = texture_owner.get_or_null(p_texture);
  1510. ERR_FAIL_NULL_V(texture, Image::FORMAT_L8);
  1511. return texture->format;
  1512. }
  1513. uint32_t TextureStorage::texture_get_texid(RID p_texture) const {
  1514. Texture *texture = texture_owner.get_or_null(p_texture);
  1515. ERR_FAIL_NULL_V(texture, 0);
  1516. return texture->tex_id;
  1517. }
  1518. Vector3i TextureStorage::texture_get_size(RID p_texture) const {
  1519. Texture *texture = texture_owner.get_or_null(p_texture);
  1520. ERR_FAIL_NULL_V(texture, Vector3i(0, 0, 0));
  1521. return Vector3i(texture->width, texture->height, texture->depth);
  1522. }
  1523. uint32_t TextureStorage::texture_get_width(RID p_texture) const {
  1524. Texture *texture = texture_owner.get_or_null(p_texture);
  1525. ERR_FAIL_NULL_V(texture, 0);
  1526. return texture->width;
  1527. }
  1528. uint32_t TextureStorage::texture_get_height(RID p_texture) const {
  1529. Texture *texture = texture_owner.get_or_null(p_texture);
  1530. ERR_FAIL_NULL_V(texture, 0);
  1531. return texture->height;
  1532. }
  1533. uint32_t TextureStorage::texture_get_depth(RID p_texture) const {
  1534. Texture *texture = texture_owner.get_or_null(p_texture);
  1535. ERR_FAIL_NULL_V(texture, 0);
  1536. return texture->depth;
  1537. }
  1538. void TextureStorage::texture_bind(RID p_texture, uint32_t p_texture_no) {
  1539. Texture *texture = texture_owner.get_or_null(p_texture);
  1540. ERR_FAIL_NULL(texture);
  1541. glActiveTexture(GL_TEXTURE0 + p_texture_no);
  1542. glBindTexture(texture->target, texture->tex_id);
  1543. }
  1544. /* TEXTURE ATLAS API */
  1545. void TextureStorage::texture_add_to_texture_atlas(RID p_texture) {
  1546. if (!texture_atlas.textures.has(p_texture)) {
  1547. TextureAtlas::Texture t;
  1548. t.users = 1;
  1549. texture_atlas.textures[p_texture] = t;
  1550. texture_atlas.dirty = true;
  1551. } else {
  1552. TextureAtlas::Texture *t = texture_atlas.textures.getptr(p_texture);
  1553. t->users++;
  1554. }
  1555. }
  1556. void TextureStorage::texture_remove_from_texture_atlas(RID p_texture) {
  1557. TextureAtlas::Texture *t = texture_atlas.textures.getptr(p_texture);
  1558. ERR_FAIL_NULL(t);
  1559. t->users--;
  1560. if (t->users == 0) {
  1561. texture_atlas.textures.erase(p_texture);
  1562. // Do not mark it dirty, there is no need to since it remains working.
  1563. }
  1564. }
  1565. void TextureStorage::texture_atlas_mark_dirty_on_texture(RID p_texture) {
  1566. if (texture_atlas.textures.has(p_texture)) {
  1567. texture_atlas.dirty = true; // Mark it dirty since it was most likely modified.
  1568. }
  1569. }
  1570. void TextureStorage::texture_atlas_remove_texture(RID p_texture) {
  1571. if (texture_atlas.textures.has(p_texture)) {
  1572. texture_atlas.textures.erase(p_texture);
  1573. // There is not much a point of making it dirty, texture can be removed next time the atlas is updated.
  1574. }
  1575. }
  1576. GLuint TextureStorage::texture_atlas_get_texture() const {
  1577. return texture_atlas.texture;
  1578. }
  1579. void TextureStorage::update_texture_atlas() {
  1580. CopyEffects *copy_effects = CopyEffects::get_singleton();
  1581. ERR_FAIL_NULL(copy_effects);
  1582. if (!texture_atlas.dirty) {
  1583. return; //nothing to do
  1584. }
  1585. texture_atlas.dirty = false;
  1586. if (texture_atlas.texture != 0) {
  1587. GLES3::Utilities::get_singleton()->texture_free_data(texture_atlas.texture);
  1588. texture_atlas.texture = 0;
  1589. glDeleteFramebuffers(1, &texture_atlas.framebuffer);
  1590. texture_atlas.framebuffer = 0;
  1591. }
  1592. const int border = 2;
  1593. if (texture_atlas.textures.size()) {
  1594. //generate atlas
  1595. Vector<TextureAtlas::SortItem> itemsv;
  1596. itemsv.resize(texture_atlas.textures.size());
  1597. uint32_t base_size = 8;
  1598. int idx = 0;
  1599. for (const KeyValue<RID, TextureAtlas::Texture> &E : texture_atlas.textures) {
  1600. TextureAtlas::SortItem &si = itemsv.write[idx];
  1601. Texture *src_tex = get_texture(E.key);
  1602. si.size.width = (src_tex->width / border) + 1;
  1603. si.size.height = (src_tex->height / border) + 1;
  1604. si.pixel_size = Size2i(src_tex->width, src_tex->height);
  1605. if (base_size < (uint32_t)si.size.width) {
  1606. base_size = nearest_power_of_2_templated(si.size.width);
  1607. }
  1608. si.texture = E.key;
  1609. idx++;
  1610. }
  1611. //sort items by size
  1612. itemsv.sort();
  1613. //attempt to create atlas
  1614. int item_count = itemsv.size();
  1615. TextureAtlas::SortItem *items = itemsv.ptrw();
  1616. int atlas_height = 0;
  1617. while (true) {
  1618. Vector<int> v_offsetsv;
  1619. v_offsetsv.resize(base_size);
  1620. int *v_offsets = v_offsetsv.ptrw();
  1621. memset(v_offsets, 0, sizeof(int) * base_size);
  1622. int max_height = 0;
  1623. for (int i = 0; i < item_count; i++) {
  1624. //best fit
  1625. TextureAtlas::SortItem &si = items[i];
  1626. int best_idx = -1;
  1627. int best_height = 0x7FFFFFFF;
  1628. for (uint32_t j = 0; j <= base_size - si.size.width; j++) {
  1629. int height = 0;
  1630. for (int k = 0; k < si.size.width; k++) {
  1631. int h = v_offsets[k + j];
  1632. if (h > height) {
  1633. height = h;
  1634. if (height > best_height) {
  1635. break; //already bad
  1636. }
  1637. }
  1638. }
  1639. if (height < best_height) {
  1640. best_height = height;
  1641. best_idx = j;
  1642. }
  1643. }
  1644. //update
  1645. for (int k = 0; k < si.size.width; k++) {
  1646. v_offsets[k + best_idx] = best_height + si.size.height;
  1647. }
  1648. si.pos.x = best_idx;
  1649. si.pos.y = best_height;
  1650. if (si.pos.y + si.size.height > max_height) {
  1651. max_height = si.pos.y + si.size.height;
  1652. }
  1653. }
  1654. if ((uint32_t)max_height <= base_size * 2) {
  1655. atlas_height = max_height;
  1656. break; //good ratio, break;
  1657. }
  1658. base_size *= 2;
  1659. }
  1660. texture_atlas.size.width = base_size * border;
  1661. texture_atlas.size.height = nearest_power_of_2_templated(atlas_height * border);
  1662. for (int i = 0; i < item_count; i++) {
  1663. TextureAtlas::Texture *t = texture_atlas.textures.getptr(items[i].texture);
  1664. t->uv_rect.position = items[i].pos * border + Vector2i(border / 2, border / 2);
  1665. t->uv_rect.size = items[i].pixel_size;
  1666. t->uv_rect.position /= Size2(texture_atlas.size);
  1667. t->uv_rect.size /= Size2(texture_atlas.size);
  1668. }
  1669. } else {
  1670. texture_atlas.size.width = 4;
  1671. texture_atlas.size.height = 4;
  1672. }
  1673. { // Atlas Texture initialize.
  1674. // TODO validate texture atlas size with maximum texture size
  1675. glGenTextures(1, &texture_atlas.texture);
  1676. glActiveTexture(GL_TEXTURE0);
  1677. glBindTexture(GL_TEXTURE_2D, texture_atlas.texture);
  1678. glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, texture_atlas.size.width, texture_atlas.size.height, 0, GL_RGBA, GL_UNSIGNED_BYTE, nullptr);
  1679. GLES3::Utilities::get_singleton()->texture_allocated_data(texture_atlas.texture, texture_atlas.size.width * texture_atlas.size.height * 4, "Texture atlas");
  1680. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  1681. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  1682. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  1683. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  1684. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_BASE_LEVEL, 0);
  1685. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, 1);
  1686. glGenFramebuffers(1, &texture_atlas.framebuffer);
  1687. glBindFramebuffer(GL_FRAMEBUFFER, texture_atlas.framebuffer);
  1688. glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, texture_atlas.texture, 0);
  1689. GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
  1690. if (status != GL_FRAMEBUFFER_COMPLETE) {
  1691. glDeleteFramebuffers(1, &texture_atlas.framebuffer);
  1692. texture_atlas.framebuffer = 0;
  1693. GLES3::Utilities::get_singleton()->texture_free_data(texture_atlas.texture);
  1694. texture_atlas.texture = 0;
  1695. WARN_PRINT("Could not create texture atlas, status: " + get_framebuffer_error(status));
  1696. return;
  1697. }
  1698. glViewport(0, 0, texture_atlas.size.width, texture_atlas.size.height);
  1699. glClearColor(0.0, 0.0, 0.0, 0.0);
  1700. glClear(GL_COLOR_BUFFER_BIT);
  1701. glBindTexture(GL_TEXTURE_2D, 0);
  1702. }
  1703. glDisable(GL_BLEND);
  1704. if (texture_atlas.textures.size()) {
  1705. for (const KeyValue<RID, TextureAtlas::Texture> &E : texture_atlas.textures) {
  1706. TextureAtlas::Texture *t = texture_atlas.textures.getptr(E.key);
  1707. Texture *src_tex = get_texture(E.key);
  1708. glActiveTexture(GL_TEXTURE0);
  1709. glBindTexture(GL_TEXTURE_2D, src_tex->tex_id);
  1710. copy_effects->copy_to_rect(t->uv_rect);
  1711. }
  1712. }
  1713. glBindFramebuffer(GL_FRAMEBUFFER, GLES3::TextureStorage::system_fbo);
  1714. }
  1715. /* DECAL API */
  1716. RID TextureStorage::decal_allocate() {
  1717. return RID();
  1718. }
  1719. void TextureStorage::decal_initialize(RID p_rid) {
  1720. }
  1721. void TextureStorage::decal_set_size(RID p_decal, const Vector3 &p_size) {
  1722. }
  1723. void TextureStorage::decal_set_texture(RID p_decal, RS::DecalTexture p_type, RID p_texture) {
  1724. }
  1725. void TextureStorage::decal_set_emission_energy(RID p_decal, float p_energy) {
  1726. }
  1727. void TextureStorage::decal_set_albedo_mix(RID p_decal, float p_mix) {
  1728. }
  1729. void TextureStorage::decal_set_modulate(RID p_decal, const Color &p_modulate) {
  1730. }
  1731. void TextureStorage::decal_set_cull_mask(RID p_decal, uint32_t p_layers) {
  1732. }
  1733. void TextureStorage::decal_set_distance_fade(RID p_decal, bool p_enabled, float p_begin, float p_length) {
  1734. }
  1735. void TextureStorage::decal_set_fade(RID p_decal, float p_above, float p_below) {
  1736. }
  1737. void TextureStorage::decal_set_normal_fade(RID p_decal, float p_fade) {
  1738. }
  1739. AABB TextureStorage::decal_get_aabb(RID p_decal) const {
  1740. return AABB();
  1741. }
  1742. /* RENDER TARGET API */
  1743. GLuint TextureStorage::system_fbo = 0;
  1744. void TextureStorage::_update_render_target(RenderTarget *rt) {
  1745. // do not allocate a render target with no size
  1746. if (rt->size.x <= 0 || rt->size.y <= 0) {
  1747. return;
  1748. }
  1749. // do not allocate a render target that is attached to the screen
  1750. if (rt->direct_to_screen) {
  1751. rt->fbo = system_fbo;
  1752. return;
  1753. }
  1754. Config *config = Config::get_singleton();
  1755. if (rt->hdr) {
  1756. rt->color_internal_format = GL_RGBA16F;
  1757. rt->color_format = GL_RGBA;
  1758. rt->color_type = GL_FLOAT;
  1759. rt->color_format_size = 8;
  1760. rt->image_format = Image::FORMAT_RGBAF;
  1761. } else if (rt->is_transparent) {
  1762. rt->color_internal_format = GL_RGBA8;
  1763. rt->color_format = GL_RGBA;
  1764. rt->color_type = GL_UNSIGNED_BYTE;
  1765. rt->color_format_size = 4;
  1766. rt->image_format = Image::FORMAT_RGBA8;
  1767. } else {
  1768. rt->color_internal_format = GL_RGB10_A2;
  1769. rt->color_format = GL_RGBA;
  1770. rt->color_type = GL_UNSIGNED_INT_2_10_10_10_REV;
  1771. rt->color_format_size = 4;
  1772. rt->image_format = Image::FORMAT_RGBA8;
  1773. }
  1774. glDisable(GL_SCISSOR_TEST);
  1775. glColorMask(1, 1, 1, 1);
  1776. glDepthMask(GL_FALSE);
  1777. {
  1778. Texture *texture;
  1779. bool use_multiview = rt->view_count > 1 && config->multiview_supported;
  1780. GLenum texture_target = use_multiview ? GL_TEXTURE_2D_ARRAY : GL_TEXTURE_2D;
  1781. /* Front FBO */
  1782. glGenFramebuffers(1, &rt->fbo);
  1783. glBindFramebuffer(GL_FRAMEBUFFER, rt->fbo);
  1784. // color
  1785. if (rt->overridden.color.is_valid()) {
  1786. texture = get_texture(rt->overridden.color);
  1787. ERR_FAIL_NULL(texture);
  1788. rt->color = texture->tex_id;
  1789. rt->size = Size2i(texture->width, texture->height);
  1790. } else {
  1791. texture = get_texture(rt->texture);
  1792. ERR_FAIL_NULL(texture);
  1793. glGenTextures(1, &rt->color);
  1794. glBindTexture(texture_target, rt->color);
  1795. if (use_multiview) {
  1796. glTexImage3D(texture_target, 0, rt->color_internal_format, rt->size.x, rt->size.y, rt->view_count, 0, rt->color_format, rt->color_type, nullptr);
  1797. } else {
  1798. glTexImage2D(texture_target, 0, rt->color_internal_format, rt->size.x, rt->size.y, 0, rt->color_format, rt->color_type, nullptr);
  1799. }
  1800. texture->gl_set_filter(RS::CANVAS_ITEM_TEXTURE_FILTER_NEAREST);
  1801. texture->gl_set_repeat(RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED);
  1802. GLES3::Utilities::get_singleton()->texture_allocated_data(rt->color, rt->size.x * rt->size.y * rt->view_count * rt->color_format_size, "Render target color texture");
  1803. }
  1804. #ifndef IOS_ENABLED
  1805. if (use_multiview) {
  1806. glFramebufferTextureMultiviewOVR(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, rt->color, 0, 0, rt->view_count);
  1807. } else {
  1808. #else
  1809. {
  1810. #endif
  1811. glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, texture_target, rt->color, 0);
  1812. }
  1813. // depth
  1814. if (rt->overridden.depth.is_valid()) {
  1815. texture = get_texture(rt->overridden.depth);
  1816. ERR_FAIL_NULL(texture);
  1817. rt->depth = texture->tex_id;
  1818. rt->depth_has_stencil = rt->overridden.depth_has_stencil;
  1819. } else {
  1820. glGenTextures(1, &rt->depth);
  1821. glBindTexture(texture_target, rt->depth);
  1822. if (use_multiview) {
  1823. glTexImage3D(texture_target, 0, GL_DEPTH24_STENCIL8, rt->size.x, rt->size.y, rt->view_count, 0, GL_DEPTH_STENCIL, GL_UNSIGNED_INT_24_8, nullptr);
  1824. } else {
  1825. glTexImage2D(texture_target, 0, GL_DEPTH24_STENCIL8, rt->size.x, rt->size.y, 0, GL_DEPTH_STENCIL, GL_UNSIGNED_INT_24_8, nullptr);
  1826. }
  1827. glTexParameteri(texture_target, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  1828. glTexParameteri(texture_target, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  1829. glTexParameteri(texture_target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  1830. glTexParameteri(texture_target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  1831. rt->depth_has_stencil = true;
  1832. GLES3::Utilities::get_singleton()->texture_allocated_data(rt->depth, rt->size.x * rt->size.y * rt->view_count * 4, "Render target depth texture");
  1833. }
  1834. #ifndef IOS_ENABLED
  1835. if (use_multiview) {
  1836. glFramebufferTextureMultiviewOVR(GL_FRAMEBUFFER, rt->depth_has_stencil ? GL_DEPTH_STENCIL_ATTACHMENT : GL_DEPTH_ATTACHMENT, rt->depth, 0, 0, rt->view_count);
  1837. } else {
  1838. #else
  1839. {
  1840. #endif
  1841. glFramebufferTexture2D(GL_FRAMEBUFFER, rt->depth_has_stencil ? GL_DEPTH_STENCIL_ATTACHMENT : GL_DEPTH_ATTACHMENT, texture_target, rt->depth, 0);
  1842. }
  1843. GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
  1844. if (status != GL_FRAMEBUFFER_COMPLETE) {
  1845. glDeleteFramebuffers(1, &rt->fbo);
  1846. if (rt->overridden.color.is_null()) {
  1847. GLES3::Utilities::get_singleton()->texture_free_data(rt->color);
  1848. }
  1849. if (rt->overridden.depth.is_null()) {
  1850. GLES3::Utilities::get_singleton()->texture_free_data(rt->depth);
  1851. }
  1852. rt->fbo = 0;
  1853. rt->size.x = 0;
  1854. rt->size.y = 0;
  1855. rt->color = 0;
  1856. rt->depth = 0;
  1857. if (rt->overridden.color.is_null()) {
  1858. texture->tex_id = 0;
  1859. texture->active = false;
  1860. }
  1861. WARN_PRINT("Could not create render target, status: " + get_framebuffer_error(status));
  1862. return;
  1863. }
  1864. texture->is_render_target = true;
  1865. texture->render_target = rt;
  1866. if (rt->overridden.color.is_null()) {
  1867. texture->format = rt->image_format;
  1868. texture->real_format = rt->image_format;
  1869. texture->target = texture_target;
  1870. if (rt->view_count > 1 && config->multiview_supported) {
  1871. texture->type = Texture::TYPE_LAYERED;
  1872. texture->layers = rt->view_count;
  1873. } else {
  1874. texture->type = Texture::TYPE_2D;
  1875. texture->layers = 1;
  1876. }
  1877. texture->gl_format_cache = rt->color_format;
  1878. texture->gl_type_cache = !rt->hdr ? GL_UNSIGNED_BYTE : GL_FLOAT; // to set HDR format size to 8 and keep 4 for LDR format
  1879. texture->gl_internal_format_cache = rt->color_internal_format;
  1880. texture->tex_id = rt->color;
  1881. texture->width = rt->size.x;
  1882. texture->alloc_width = rt->size.x;
  1883. texture->height = rt->size.y;
  1884. texture->alloc_height = rt->size.y;
  1885. texture->active = true;
  1886. }
  1887. }
  1888. glClearColor(0, 0, 0, 0);
  1889. glClear(GL_COLOR_BUFFER_BIT);
  1890. glBindFramebuffer(GL_FRAMEBUFFER, system_fbo);
  1891. }
  1892. void TextureStorage::_create_render_target_backbuffer(RenderTarget *rt) {
  1893. ERR_FAIL_COND_MSG(rt->backbuffer_fbo != 0, "Cannot allocate RenderTarget backbuffer: already initialized.");
  1894. ERR_FAIL_COND(rt->direct_to_screen);
  1895. // Allocate mipmap chains for full screen blur
  1896. // Limit mipmaps so smallest is 32x32 to avoid unnecessary framebuffer switches
  1897. int count = MAX(1, Image::get_image_required_mipmaps(rt->size.x, rt->size.y, Image::FORMAT_RGBA8) - 4);
  1898. if (rt->size.x > 40 && rt->size.y > 40) {
  1899. GLsizei width = rt->size.x;
  1900. GLsizei height = rt->size.y;
  1901. rt->mipmap_count = count;
  1902. glGenTextures(1, &rt->backbuffer);
  1903. glBindTexture(GL_TEXTURE_2D, rt->backbuffer);
  1904. uint32_t texture_size_bytes = 0;
  1905. for (int l = 0; l < count; l++) {
  1906. texture_size_bytes += width * height * 4;
  1907. glTexImage2D(GL_TEXTURE_2D, l, rt->color_internal_format, width, height, 0, rt->color_format, rt->color_type, nullptr);
  1908. width = MAX(1, (width / 2));
  1909. height = MAX(1, (height / 2));
  1910. }
  1911. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_BASE_LEVEL, 0);
  1912. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, count - 1);
  1913. glGenFramebuffers(1, &rt->backbuffer_fbo);
  1914. glBindFramebuffer(GL_FRAMEBUFFER, rt->backbuffer_fbo);
  1915. glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, rt->backbuffer, 0);
  1916. GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
  1917. if (status != GL_FRAMEBUFFER_COMPLETE) {
  1918. WARN_PRINT_ONCE("Cannot allocate mipmaps for canvas screen blur. Status: " + get_framebuffer_error(status));
  1919. glBindFramebuffer(GL_FRAMEBUFFER, system_fbo);
  1920. return;
  1921. }
  1922. GLES3::Utilities::get_singleton()->texture_allocated_data(rt->backbuffer, texture_size_bytes, "Render target backbuffer color texture");
  1923. // Initialize all levels to clear black.
  1924. for (int j = 0; j < count; j++) {
  1925. glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, rt->backbuffer, j);
  1926. glClearColor(0.0, 0.0, 0.0, 0.0);
  1927. glClear(GL_COLOR_BUFFER_BIT);
  1928. }
  1929. glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, rt->backbuffer, 0);
  1930. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  1931. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
  1932. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  1933. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  1934. }
  1935. }
  1936. void GLES3::TextureStorage::check_backbuffer(RenderTarget *rt, const bool uses_screen_texture, const bool uses_depth_texture) {
  1937. if (rt->backbuffer != 0 && rt->backbuffer_depth != 0) {
  1938. return;
  1939. }
  1940. Config *config = Config::get_singleton();
  1941. bool use_multiview = rt->view_count > 1 && config->multiview_supported;
  1942. GLenum texture_target = use_multiview ? GL_TEXTURE_2D_ARRAY : GL_TEXTURE_2D;
  1943. if (rt->backbuffer_fbo == 0) {
  1944. glGenFramebuffers(1, &rt->backbuffer_fbo);
  1945. }
  1946. glBindFramebuffer(GL_FRAMEBUFFER, rt->backbuffer_fbo);
  1947. if (rt->backbuffer == 0 && uses_screen_texture) {
  1948. glGenTextures(1, &rt->backbuffer);
  1949. glBindTexture(texture_target, rt->backbuffer);
  1950. if (use_multiview) {
  1951. glTexImage3D(texture_target, 0, rt->color_internal_format, rt->size.x, rt->size.y, rt->view_count, 0, rt->color_format, rt->color_type, nullptr);
  1952. } else {
  1953. glTexImage2D(texture_target, 0, rt->color_internal_format, rt->size.x, rt->size.y, 0, rt->color_format, rt->color_type, nullptr);
  1954. }
  1955. GLES3::Utilities::get_singleton()->texture_allocated_data(rt->backbuffer, rt->size.x * rt->size.y * rt->view_count * rt->color_format_size, "Render target backbuffer color texture (3D)");
  1956. glTexParameteri(texture_target, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  1957. glTexParameteri(texture_target, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  1958. glTexParameteri(texture_target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  1959. glTexParameteri(texture_target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  1960. #ifndef IOS_ENABLED
  1961. if (use_multiview) {
  1962. glFramebufferTextureMultiviewOVR(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, rt->backbuffer, 0, 0, rt->view_count);
  1963. } else {
  1964. #else
  1965. {
  1966. #endif
  1967. glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, rt->backbuffer, 0);
  1968. }
  1969. }
  1970. if (rt->backbuffer_depth == 0 && uses_depth_texture) {
  1971. glGenTextures(1, &rt->backbuffer_depth);
  1972. glBindTexture(texture_target, rt->backbuffer_depth);
  1973. GLint internal_format;
  1974. GLenum format;
  1975. GLenum type;
  1976. GLenum attachment;
  1977. int element_size;
  1978. if (rt->depth_has_stencil) {
  1979. internal_format = GL_DEPTH24_STENCIL8;
  1980. format = GL_DEPTH_STENCIL;
  1981. type = GL_UNSIGNED_INT_24_8;
  1982. attachment = GL_DEPTH_STENCIL_ATTACHMENT;
  1983. element_size = 4;
  1984. } else {
  1985. internal_format = GL_DEPTH_COMPONENT24;
  1986. format = GL_DEPTH_COMPONENT;
  1987. type = GL_UNSIGNED_INT;
  1988. attachment = GL_DEPTH_ATTACHMENT;
  1989. element_size = 3;
  1990. }
  1991. if (use_multiview) {
  1992. glTexImage3D(texture_target, 0, internal_format, rt->size.x, rt->size.y, rt->view_count, 0, format, type, nullptr);
  1993. } else {
  1994. glTexImage2D(texture_target, 0, internal_format, rt->size.x, rt->size.y, 0, format, type, nullptr);
  1995. }
  1996. GLES3::Utilities::get_singleton()->texture_allocated_data(rt->backbuffer_depth, rt->size.x * rt->size.y * rt->view_count * element_size, "Render target backbuffer depth texture");
  1997. glTexParameteri(texture_target, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  1998. glTexParameteri(texture_target, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  1999. glTexParameteri(texture_target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  2000. glTexParameteri(texture_target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  2001. #ifndef IOS_ENABLED
  2002. if (use_multiview) {
  2003. glFramebufferTextureMultiviewOVR(GL_FRAMEBUFFER, attachment, rt->backbuffer_depth, 0, 0, rt->view_count);
  2004. } else {
  2005. #else
  2006. {
  2007. #endif
  2008. glFramebufferTexture2D(GL_FRAMEBUFFER, attachment, GL_TEXTURE_2D, rt->backbuffer_depth, 0);
  2009. }
  2010. }
  2011. }
  2012. void TextureStorage::_clear_render_target(RenderTarget *rt) {
  2013. // there is nothing else to clear when DIRECT_TO_SCREEN is used
  2014. if (rt->direct_to_screen) {
  2015. return;
  2016. }
  2017. // Dispose of the cached fbo's and the allocated textures
  2018. for (KeyValue<uint32_t, RenderTarget::RTOverridden::FBOCacheEntry> &E : rt->overridden.fbo_cache) {
  2019. glDeleteTextures(E.value.allocated_textures.size(), E.value.allocated_textures.ptr());
  2020. // Don't delete the current FBO, we'll do that a couple lines down.
  2021. if (E.value.fbo != rt->fbo) {
  2022. glDeleteFramebuffers(1, &E.value.fbo);
  2023. }
  2024. }
  2025. rt->overridden.fbo_cache.clear();
  2026. if (rt->fbo) {
  2027. glDeleteFramebuffers(1, &rt->fbo);
  2028. rt->fbo = 0;
  2029. }
  2030. if (rt->overridden.color.is_null()) {
  2031. if (rt->texture.is_valid()) {
  2032. Texture *tex = get_texture(rt->texture);
  2033. tex->alloc_height = 0;
  2034. tex->alloc_width = 0;
  2035. tex->width = 0;
  2036. tex->height = 0;
  2037. tex->active = false;
  2038. tex->render_target = nullptr;
  2039. tex->is_render_target = false;
  2040. tex->gl_set_filter(RS::CANVAS_ITEM_TEXTURE_FILTER_MAX);
  2041. tex->gl_set_repeat(RS::CANVAS_ITEM_TEXTURE_REPEAT_MAX);
  2042. }
  2043. } else {
  2044. Texture *tex = get_texture(rt->overridden.color);
  2045. tex->render_target = nullptr;
  2046. tex->is_render_target = false;
  2047. }
  2048. if (rt->overridden.color.is_valid()) {
  2049. rt->overridden.color = RID();
  2050. } else if (rt->color) {
  2051. GLES3::Utilities::get_singleton()->texture_free_data(rt->color);
  2052. if (rt->texture.is_valid()) {
  2053. Texture *tex = get_texture(rt->texture);
  2054. tex->tex_id = 0;
  2055. }
  2056. }
  2057. rt->color = 0;
  2058. if (rt->overridden.depth.is_valid()) {
  2059. rt->overridden.depth = RID();
  2060. } else if (rt->depth) {
  2061. GLES3::Utilities::get_singleton()->texture_free_data(rt->depth);
  2062. }
  2063. rt->depth = 0;
  2064. rt->overridden.velocity = RID();
  2065. rt->overridden.is_overridden = false;
  2066. if (rt->backbuffer_fbo != 0) {
  2067. glDeleteFramebuffers(1, &rt->backbuffer_fbo);
  2068. rt->backbuffer_fbo = 0;
  2069. }
  2070. if (rt->backbuffer != 0) {
  2071. GLES3::Utilities::get_singleton()->texture_free_data(rt->backbuffer);
  2072. rt->backbuffer = 0;
  2073. }
  2074. if (rt->backbuffer_depth != 0) {
  2075. GLES3::Utilities::get_singleton()->texture_free_data(rt->backbuffer_depth);
  2076. rt->backbuffer_depth = 0;
  2077. }
  2078. _render_target_clear_sdf(rt);
  2079. }
  2080. RID TextureStorage::render_target_create() {
  2081. RenderTarget render_target;
  2082. render_target.used_in_frame = false;
  2083. render_target.clear_requested = false;
  2084. Texture t;
  2085. t.active = true;
  2086. t.render_target = &render_target;
  2087. t.is_render_target = true;
  2088. render_target.texture = texture_owner.make_rid(t);
  2089. _update_render_target(&render_target);
  2090. return render_target_owner.make_rid(render_target);
  2091. }
  2092. void TextureStorage::render_target_free(RID p_rid) {
  2093. RenderTarget *rt = render_target_owner.get_or_null(p_rid);
  2094. _clear_render_target(rt);
  2095. Texture *t = get_texture(rt->texture);
  2096. if (t) {
  2097. t->is_render_target = false;
  2098. if (rt->overridden.color.is_null()) {
  2099. texture_free(rt->texture);
  2100. }
  2101. //memdelete(t);
  2102. }
  2103. render_target_owner.free(p_rid);
  2104. }
  2105. void TextureStorage::render_target_set_position(RID p_render_target, int p_x, int p_y) {
  2106. RenderTarget *rt = render_target_owner.get_or_null(p_render_target);
  2107. ERR_FAIL_NULL(rt);
  2108. rt->position = Point2i(p_x, p_y);
  2109. }
  2110. Point2i TextureStorage::render_target_get_position(RID p_render_target) const {
  2111. RenderTarget *rt = render_target_owner.get_or_null(p_render_target);
  2112. ERR_FAIL_NULL_V(rt, Point2i());
  2113. return rt->position;
  2114. }
  2115. void TextureStorage::render_target_set_size(RID p_render_target, int p_width, int p_height, uint32_t p_view_count) {
  2116. RenderTarget *rt = render_target_owner.get_or_null(p_render_target);
  2117. ERR_FAIL_NULL(rt);
  2118. if (p_width == rt->size.x && p_height == rt->size.y && p_view_count == rt->view_count) {
  2119. return;
  2120. }
  2121. if (rt->overridden.color.is_valid()) {
  2122. return;
  2123. }
  2124. _clear_render_target(rt);
  2125. rt->size = Size2i(p_width, p_height);
  2126. rt->view_count = p_view_count;
  2127. _update_render_target(rt);
  2128. }
  2129. // TODO: convert to Size2i internally
  2130. Size2i TextureStorage::render_target_get_size(RID p_render_target) const {
  2131. RenderTarget *rt = render_target_owner.get_or_null(p_render_target);
  2132. ERR_FAIL_NULL_V(rt, Size2i());
  2133. return rt->size;
  2134. }
  2135. void TextureStorage::render_target_set_override(RID p_render_target, RID p_color_texture, RID p_depth_texture, RID p_velocity_texture, RID p_velocity_depth_texture) {
  2136. RenderTarget *rt = render_target_owner.get_or_null(p_render_target);
  2137. ERR_FAIL_NULL(rt);
  2138. ERR_FAIL_COND(rt->direct_to_screen);
  2139. rt->overridden.velocity = p_velocity_texture;
  2140. if (rt->overridden.color == p_color_texture && rt->overridden.depth == p_depth_texture) {
  2141. return;
  2142. }
  2143. if (p_color_texture.is_null() && p_depth_texture.is_null()) {
  2144. _clear_render_target(rt);
  2145. _update_render_target(rt);
  2146. return;
  2147. }
  2148. if (!rt->overridden.is_overridden) {
  2149. _clear_render_target(rt);
  2150. }
  2151. rt->overridden.color = p_color_texture;
  2152. rt->overridden.depth = p_depth_texture;
  2153. rt->overridden.depth_has_stencil = p_depth_texture.is_null();
  2154. rt->overridden.is_overridden = true;
  2155. uint32_t hash_key = hash_murmur3_one_64(p_color_texture.get_id());
  2156. hash_key = hash_murmur3_one_64(p_depth_texture.get_id(), hash_key);
  2157. hash_key = hash_fmix32(hash_key);
  2158. RBMap<uint32_t, RenderTarget::RTOverridden::FBOCacheEntry>::Element *cache;
  2159. if ((cache = rt->overridden.fbo_cache.find(hash_key)) != nullptr) {
  2160. rt->fbo = cache->get().fbo;
  2161. rt->color = cache->get().color;
  2162. rt->depth = cache->get().depth;
  2163. rt->depth_has_stencil = cache->get().depth_has_stencil;
  2164. rt->size = cache->get().size;
  2165. rt->texture = p_color_texture;
  2166. return;
  2167. }
  2168. _update_render_target(rt);
  2169. RenderTarget::RTOverridden::FBOCacheEntry new_entry;
  2170. new_entry.fbo = rt->fbo;
  2171. new_entry.color = rt->color;
  2172. new_entry.depth = rt->depth;
  2173. new_entry.depth_has_stencil = rt->depth_has_stencil;
  2174. new_entry.size = rt->size;
  2175. // Keep track of any textures we had to allocate because they weren't overridden.
  2176. if (p_color_texture.is_null()) {
  2177. new_entry.allocated_textures.push_back(rt->color);
  2178. }
  2179. if (p_depth_texture.is_null()) {
  2180. new_entry.allocated_textures.push_back(rt->depth);
  2181. }
  2182. rt->overridden.fbo_cache.insert(hash_key, new_entry);
  2183. }
  2184. RID TextureStorage::render_target_get_override_color(RID p_render_target) const {
  2185. RenderTarget *rt = render_target_owner.get_or_null(p_render_target);
  2186. ERR_FAIL_NULL_V(rt, RID());
  2187. return rt->overridden.color;
  2188. }
  2189. RID TextureStorage::render_target_get_override_depth(RID p_render_target) const {
  2190. RenderTarget *rt = render_target_owner.get_or_null(p_render_target);
  2191. ERR_FAIL_NULL_V(rt, RID());
  2192. return rt->overridden.depth;
  2193. }
  2194. RID TextureStorage::render_target_get_override_velocity(RID p_render_target) const {
  2195. RenderTarget *rt = render_target_owner.get_or_null(p_render_target);
  2196. ERR_FAIL_NULL_V(rt, RID());
  2197. return rt->overridden.velocity;
  2198. }
  2199. void TextureStorage::render_target_set_render_region(RID p_render_target, const Rect2i &p_render_region) {
  2200. RenderTarget *rt = render_target_owner.get_or_null(p_render_target);
  2201. ERR_FAIL_NULL(rt);
  2202. rt->render_region = p_render_region;
  2203. }
  2204. Rect2i TextureStorage::render_target_get_render_region(RID p_render_target) const {
  2205. RenderTarget *rt = render_target_owner.get_or_null(p_render_target);
  2206. ERR_FAIL_NULL_V(rt, Rect2i());
  2207. return rt->render_region;
  2208. }
  2209. RID TextureStorage::render_target_get_texture(RID p_render_target) {
  2210. RenderTarget *rt = render_target_owner.get_or_null(p_render_target);
  2211. ERR_FAIL_NULL_V(rt, RID());
  2212. if (rt->overridden.color.is_valid()) {
  2213. return rt->overridden.color;
  2214. }
  2215. return rt->texture;
  2216. }
  2217. void TextureStorage::render_target_set_transparent(RID p_render_target, bool p_transparent) {
  2218. RenderTarget *rt = render_target_owner.get_or_null(p_render_target);
  2219. ERR_FAIL_NULL(rt);
  2220. rt->is_transparent = p_transparent;
  2221. if (rt->overridden.color.is_null()) {
  2222. _clear_render_target(rt);
  2223. _update_render_target(rt);
  2224. }
  2225. }
  2226. bool TextureStorage::render_target_get_transparent(RID p_render_target) const {
  2227. RenderTarget *rt = render_target_owner.get_or_null(p_render_target);
  2228. ERR_FAIL_NULL_V(rt, false);
  2229. return rt->is_transparent;
  2230. }
  2231. void TextureStorage::render_target_set_direct_to_screen(RID p_render_target, bool p_direct_to_screen) {
  2232. RenderTarget *rt = render_target_owner.get_or_null(p_render_target);
  2233. ERR_FAIL_NULL(rt);
  2234. if (p_direct_to_screen == rt->direct_to_screen) {
  2235. return;
  2236. }
  2237. // When setting DIRECT_TO_SCREEN, you need to clear before the value is set, but allocate after as
  2238. // those functions change how they operate depending on the value of DIRECT_TO_SCREEN
  2239. _clear_render_target(rt);
  2240. rt->direct_to_screen = p_direct_to_screen;
  2241. if (rt->direct_to_screen) {
  2242. rt->overridden.color = RID();
  2243. rt->overridden.depth = RID();
  2244. rt->overridden.velocity = RID();
  2245. }
  2246. _update_render_target(rt);
  2247. }
  2248. bool TextureStorage::render_target_get_direct_to_screen(RID p_render_target) const {
  2249. RenderTarget *rt = render_target_owner.get_or_null(p_render_target);
  2250. ERR_FAIL_NULL_V(rt, false);
  2251. return rt->direct_to_screen;
  2252. }
  2253. bool TextureStorage::render_target_was_used(RID p_render_target) const {
  2254. RenderTarget *rt = render_target_owner.get_or_null(p_render_target);
  2255. ERR_FAIL_NULL_V(rt, false);
  2256. return rt->used_in_frame;
  2257. }
  2258. void TextureStorage::render_target_clear_used(RID p_render_target) {
  2259. RenderTarget *rt = render_target_owner.get_or_null(p_render_target);
  2260. ERR_FAIL_NULL(rt);
  2261. rt->used_in_frame = false;
  2262. }
  2263. void TextureStorage::render_target_set_msaa(RID p_render_target, RS::ViewportMSAA p_msaa) {
  2264. RenderTarget *rt = render_target_owner.get_or_null(p_render_target);
  2265. ERR_FAIL_NULL(rt);
  2266. ERR_FAIL_COND(rt->direct_to_screen);
  2267. if (p_msaa == rt->msaa) {
  2268. return;
  2269. }
  2270. WARN_PRINT("2D MSAA is not yet supported for GLES3.");
  2271. _clear_render_target(rt);
  2272. rt->msaa = p_msaa;
  2273. _update_render_target(rt);
  2274. }
  2275. RS::ViewportMSAA TextureStorage::render_target_get_msaa(RID p_render_target) const {
  2276. RenderTarget *rt = render_target_owner.get_or_null(p_render_target);
  2277. ERR_FAIL_NULL_V(rt, RS::VIEWPORT_MSAA_DISABLED);
  2278. return rt->msaa;
  2279. }
  2280. void TextureStorage::render_target_set_use_hdr(RID p_render_target, bool p_use_hdr_2d) {
  2281. RenderTarget *rt = render_target_owner.get_or_null(p_render_target);
  2282. ERR_FAIL_NULL(rt);
  2283. ERR_FAIL_COND(rt->direct_to_screen);
  2284. if (p_use_hdr_2d == rt->hdr) {
  2285. return;
  2286. }
  2287. _clear_render_target(rt);
  2288. rt->hdr = p_use_hdr_2d;
  2289. _update_render_target(rt);
  2290. }
  2291. bool TextureStorage::render_target_is_using_hdr(RID p_render_target) const {
  2292. RenderTarget *rt = render_target_owner.get_or_null(p_render_target);
  2293. ERR_FAIL_NULL_V(rt, false);
  2294. return rt->hdr;
  2295. }
  2296. GLuint TextureStorage::render_target_get_color_internal_format(RID p_render_target) const {
  2297. RenderTarget *rt = render_target_owner.get_or_null(p_render_target);
  2298. ERR_FAIL_NULL_V(rt, GL_RGBA8);
  2299. return rt->color_internal_format;
  2300. }
  2301. GLuint TextureStorage::render_target_get_color_format(RID p_render_target) const {
  2302. RenderTarget *rt = render_target_owner.get_or_null(p_render_target);
  2303. ERR_FAIL_NULL_V(rt, GL_RGBA);
  2304. return rt->color_format;
  2305. }
  2306. GLuint TextureStorage::render_target_get_color_type(RID p_render_target) const {
  2307. RenderTarget *rt = render_target_owner.get_or_null(p_render_target);
  2308. ERR_FAIL_NULL_V(rt, GL_UNSIGNED_BYTE);
  2309. return rt->color_type;
  2310. }
  2311. uint32_t TextureStorage::render_target_get_color_format_size(RID p_render_target) const {
  2312. RenderTarget *rt = render_target_owner.get_or_null(p_render_target);
  2313. ERR_FAIL_NULL_V(rt, 4);
  2314. return rt->color_format_size;
  2315. }
  2316. void TextureStorage::render_target_request_clear(RID p_render_target, const Color &p_clear_color) {
  2317. RenderTarget *rt = render_target_owner.get_or_null(p_render_target);
  2318. ERR_FAIL_NULL(rt);
  2319. rt->clear_requested = true;
  2320. rt->clear_color = p_clear_color;
  2321. }
  2322. bool TextureStorage::render_target_is_clear_requested(RID p_render_target) {
  2323. RenderTarget *rt = render_target_owner.get_or_null(p_render_target);
  2324. ERR_FAIL_NULL_V(rt, false);
  2325. return rt->clear_requested;
  2326. }
  2327. Color TextureStorage::render_target_get_clear_request_color(RID p_render_target) {
  2328. RenderTarget *rt = render_target_owner.get_or_null(p_render_target);
  2329. ERR_FAIL_NULL_V(rt, Color());
  2330. return rt->clear_color;
  2331. }
  2332. void TextureStorage::render_target_disable_clear_request(RID p_render_target) {
  2333. RenderTarget *rt = render_target_owner.get_or_null(p_render_target);
  2334. ERR_FAIL_NULL(rt);
  2335. rt->clear_requested = false;
  2336. }
  2337. void TextureStorage::render_target_do_clear_request(RID p_render_target) {
  2338. RenderTarget *rt = render_target_owner.get_or_null(p_render_target);
  2339. ERR_FAIL_NULL(rt);
  2340. if (!rt->clear_requested) {
  2341. return;
  2342. }
  2343. glBindFramebuffer(GL_FRAMEBUFFER, rt->fbo);
  2344. glClearBufferfv(GL_COLOR, 0, rt->clear_color.components);
  2345. rt->clear_requested = false;
  2346. glBindFramebuffer(GL_FRAMEBUFFER, system_fbo);
  2347. }
  2348. GLuint TextureStorage::render_target_get_fbo(RID p_render_target) const {
  2349. RenderTarget *rt = render_target_owner.get_or_null(p_render_target);
  2350. ERR_FAIL_NULL_V(rt, 0);
  2351. return rt->fbo;
  2352. }
  2353. GLuint TextureStorage::render_target_get_color(RID p_render_target) const {
  2354. RenderTarget *rt = render_target_owner.get_or_null(p_render_target);
  2355. ERR_FAIL_NULL_V(rt, 0);
  2356. if (rt->overridden.color.is_valid()) {
  2357. Texture *texture = get_texture(rt->overridden.color);
  2358. ERR_FAIL_NULL_V(texture, 0);
  2359. return texture->tex_id;
  2360. } else {
  2361. return rt->color;
  2362. }
  2363. }
  2364. GLuint TextureStorage::render_target_get_depth(RID p_render_target) const {
  2365. RenderTarget *rt = render_target_owner.get_or_null(p_render_target);
  2366. ERR_FAIL_NULL_V(rt, 0);
  2367. if (rt->overridden.depth.is_valid()) {
  2368. Texture *texture = get_texture(rt->overridden.depth);
  2369. ERR_FAIL_NULL_V(texture, 0);
  2370. return texture->tex_id;
  2371. } else {
  2372. return rt->depth;
  2373. }
  2374. }
  2375. void TextureStorage::render_target_set_reattach_textures(RID p_render_target, bool p_reattach_textures) const {
  2376. RenderTarget *rt = render_target_owner.get_or_null(p_render_target);
  2377. ERR_FAIL_NULL(rt);
  2378. rt->reattach_textures = p_reattach_textures;
  2379. }
  2380. bool TextureStorage::render_target_is_reattach_textures(RID p_render_target) const {
  2381. RenderTarget *rt = render_target_owner.get_or_null(p_render_target);
  2382. ERR_FAIL_NULL_V(rt, false);
  2383. return rt->reattach_textures;
  2384. }
  2385. void TextureStorage::render_target_set_sdf_size_and_scale(RID p_render_target, RS::ViewportSDFOversize p_size, RS::ViewportSDFScale p_scale) {
  2386. RenderTarget *rt = render_target_owner.get_or_null(p_render_target);
  2387. ERR_FAIL_NULL(rt);
  2388. if (rt->sdf_oversize == p_size && rt->sdf_scale == p_scale) {
  2389. return;
  2390. }
  2391. rt->sdf_oversize = p_size;
  2392. rt->sdf_scale = p_scale;
  2393. _render_target_clear_sdf(rt);
  2394. }
  2395. Rect2i TextureStorage::_render_target_get_sdf_rect(const RenderTarget *rt) const {
  2396. Size2i margin;
  2397. int scale;
  2398. switch (rt->sdf_oversize) {
  2399. case RS::VIEWPORT_SDF_OVERSIZE_100_PERCENT: {
  2400. scale = 100;
  2401. } break;
  2402. case RS::VIEWPORT_SDF_OVERSIZE_120_PERCENT: {
  2403. scale = 120;
  2404. } break;
  2405. case RS::VIEWPORT_SDF_OVERSIZE_150_PERCENT: {
  2406. scale = 150;
  2407. } break;
  2408. case RS::VIEWPORT_SDF_OVERSIZE_200_PERCENT: {
  2409. scale = 200;
  2410. } break;
  2411. default: {
  2412. ERR_PRINT("Invalid viewport SDF oversize, defaulting to 100%.");
  2413. scale = 100;
  2414. } break;
  2415. }
  2416. margin = (rt->size * scale / 100) - rt->size;
  2417. Rect2i r(Vector2i(), rt->size);
  2418. r.position -= margin;
  2419. r.size += margin * 2;
  2420. return r;
  2421. }
  2422. Rect2i TextureStorage::render_target_get_sdf_rect(RID p_render_target) const {
  2423. const RenderTarget *rt = render_target_owner.get_or_null(p_render_target);
  2424. ERR_FAIL_NULL_V(rt, Rect2i());
  2425. return _render_target_get_sdf_rect(rt);
  2426. }
  2427. void TextureStorage::render_target_mark_sdf_enabled(RID p_render_target, bool p_enabled) {
  2428. RenderTarget *rt = render_target_owner.get_or_null(p_render_target);
  2429. ERR_FAIL_NULL(rt);
  2430. rt->sdf_enabled = p_enabled;
  2431. }
  2432. bool TextureStorage::render_target_is_sdf_enabled(RID p_render_target) const {
  2433. const RenderTarget *rt = render_target_owner.get_or_null(p_render_target);
  2434. ERR_FAIL_NULL_V(rt, false);
  2435. return rt->sdf_enabled;
  2436. }
  2437. GLuint TextureStorage::render_target_get_sdf_texture(RID p_render_target) {
  2438. RenderTarget *rt = render_target_owner.get_or_null(p_render_target);
  2439. ERR_FAIL_NULL_V(rt, 0);
  2440. if (rt->sdf_texture_read == 0) {
  2441. Texture *texture = texture_owner.get_or_null(default_gl_textures[DEFAULT_GL_TEXTURE_BLACK]);
  2442. return texture->tex_id;
  2443. }
  2444. return rt->sdf_texture_read;
  2445. }
  2446. void TextureStorage::_render_target_allocate_sdf(RenderTarget *rt) {
  2447. ERR_FAIL_COND(rt->sdf_texture_write_fb != 0);
  2448. Size2i size = _render_target_get_sdf_rect(rt).size;
  2449. glGenTextures(1, &rt->sdf_texture_write);
  2450. glActiveTexture(GL_TEXTURE0);
  2451. glBindTexture(GL_TEXTURE_2D, rt->sdf_texture_write);
  2452. glTexImage2D(GL_TEXTURE_2D, 0, GL_R8, size.width, size.height, 0, GL_RED, GL_UNSIGNED_BYTE, nullptr);
  2453. GLES3::Utilities::get_singleton()->texture_allocated_data(rt->sdf_texture_write, size.width * size.height, "SDF texture");
  2454. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  2455. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  2456. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_BASE_LEVEL, 0);
  2457. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, 1);
  2458. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  2459. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  2460. glGenFramebuffers(1, &rt->sdf_texture_write_fb);
  2461. glBindFramebuffer(GL_FRAMEBUFFER, rt->sdf_texture_write_fb);
  2462. glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, rt->sdf_texture_write, 0);
  2463. int scale;
  2464. switch (rt->sdf_scale) {
  2465. case RS::VIEWPORT_SDF_SCALE_100_PERCENT: {
  2466. scale = 100;
  2467. } break;
  2468. case RS::VIEWPORT_SDF_SCALE_50_PERCENT: {
  2469. scale = 50;
  2470. } break;
  2471. case RS::VIEWPORT_SDF_SCALE_25_PERCENT: {
  2472. scale = 25;
  2473. } break;
  2474. default: {
  2475. ERR_PRINT("Invalid viewport SDF scale, defaulting to 100%.");
  2476. scale = 100;
  2477. } break;
  2478. }
  2479. rt->process_size = size * scale / 100;
  2480. rt->process_size = rt->process_size.maxi(1);
  2481. glGenTextures(2, rt->sdf_texture_process);
  2482. glBindTexture(GL_TEXTURE_2D, rt->sdf_texture_process[0]);
  2483. glTexImage2D(GL_TEXTURE_2D, 0, GL_RG16I, rt->process_size.width, rt->process_size.height, 0, GL_RG_INTEGER, GL_SHORT, nullptr);
  2484. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  2485. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  2486. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_BASE_LEVEL, 0);
  2487. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, 1);
  2488. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  2489. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  2490. GLES3::Utilities::get_singleton()->texture_allocated_data(rt->sdf_texture_process[0], rt->process_size.width * rt->process_size.height * 4, "SDF process texture[0]");
  2491. glBindTexture(GL_TEXTURE_2D, rt->sdf_texture_process[1]);
  2492. glTexImage2D(GL_TEXTURE_2D, 0, GL_RG16I, rt->process_size.width, rt->process_size.height, 0, GL_RG_INTEGER, GL_SHORT, nullptr);
  2493. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  2494. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  2495. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_BASE_LEVEL, 0);
  2496. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, 1);
  2497. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  2498. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  2499. GLES3::Utilities::get_singleton()->texture_allocated_data(rt->sdf_texture_process[1], rt->process_size.width * rt->process_size.height * 4, "SDF process texture[1]");
  2500. glGenTextures(1, &rt->sdf_texture_read);
  2501. glBindTexture(GL_TEXTURE_2D, rt->sdf_texture_read);
  2502. glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, rt->process_size.width, rt->process_size.height, 0, GL_RGBA, GL_UNSIGNED_BYTE, nullptr);
  2503. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  2504. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  2505. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_BASE_LEVEL, 0);
  2506. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, 1);
  2507. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  2508. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  2509. GLES3::Utilities::get_singleton()->texture_allocated_data(rt->sdf_texture_read, rt->process_size.width * rt->process_size.height * 4, "SDF texture (read)");
  2510. }
  2511. void TextureStorage::_render_target_clear_sdf(RenderTarget *rt) {
  2512. if (rt->sdf_texture_write_fb != 0) {
  2513. GLES3::Utilities::get_singleton()->texture_free_data(rt->sdf_texture_read);
  2514. GLES3::Utilities::get_singleton()->texture_free_data(rt->sdf_texture_write);
  2515. GLES3::Utilities::get_singleton()->texture_free_data(rt->sdf_texture_process[0]);
  2516. GLES3::Utilities::get_singleton()->texture_free_data(rt->sdf_texture_process[1]);
  2517. glDeleteFramebuffers(1, &rt->sdf_texture_write_fb);
  2518. rt->sdf_texture_read = 0;
  2519. rt->sdf_texture_write = 0;
  2520. rt->sdf_texture_process[0] = 0;
  2521. rt->sdf_texture_process[1] = 0;
  2522. rt->sdf_texture_write_fb = 0;
  2523. }
  2524. }
  2525. GLuint TextureStorage::render_target_get_sdf_framebuffer(RID p_render_target) {
  2526. RenderTarget *rt = render_target_owner.get_or_null(p_render_target);
  2527. ERR_FAIL_NULL_V(rt, 0);
  2528. if (rt->sdf_texture_write_fb == 0) {
  2529. _render_target_allocate_sdf(rt);
  2530. }
  2531. return rt->sdf_texture_write_fb;
  2532. }
  2533. void TextureStorage::render_target_sdf_process(RID p_render_target) {
  2534. CopyEffects *copy_effects = CopyEffects::get_singleton();
  2535. RenderTarget *rt = render_target_owner.get_or_null(p_render_target);
  2536. ERR_FAIL_NULL(rt);
  2537. ERR_FAIL_COND(rt->sdf_texture_write_fb == 0);
  2538. Rect2i r = _render_target_get_sdf_rect(rt);
  2539. Size2i size = r.size;
  2540. int32_t shift = 0;
  2541. bool shrink = false;
  2542. switch (rt->sdf_scale) {
  2543. case RS::VIEWPORT_SDF_SCALE_50_PERCENT: {
  2544. size[0] >>= 1;
  2545. size[1] >>= 1;
  2546. shift = 1;
  2547. shrink = true;
  2548. } break;
  2549. case RS::VIEWPORT_SDF_SCALE_25_PERCENT: {
  2550. size[0] >>= 2;
  2551. size[1] >>= 2;
  2552. shift = 2;
  2553. shrink = true;
  2554. } break;
  2555. default: {
  2556. };
  2557. }
  2558. GLuint temp_fb;
  2559. glGenFramebuffers(1, &temp_fb);
  2560. glBindFramebuffer(GL_FRAMEBUFFER, temp_fb);
  2561. // Load
  2562. CanvasSdfShaderGLES3::ShaderVariant variant = shrink ? CanvasSdfShaderGLES3::MODE_LOAD_SHRINK : CanvasSdfShaderGLES3::MODE_LOAD;
  2563. bool success = sdf_shader.shader.version_bind_shader(sdf_shader.shader_version, variant);
  2564. if (!success) {
  2565. return;
  2566. }
  2567. sdf_shader.shader.version_set_uniform(CanvasSdfShaderGLES3::BASE_SIZE, r.size, sdf_shader.shader_version, variant);
  2568. sdf_shader.shader.version_set_uniform(CanvasSdfShaderGLES3::SIZE, size, sdf_shader.shader_version, variant);
  2569. sdf_shader.shader.version_set_uniform(CanvasSdfShaderGLES3::STRIDE, 0, sdf_shader.shader_version, variant);
  2570. sdf_shader.shader.version_set_uniform(CanvasSdfShaderGLES3::SHIFT, shift, sdf_shader.shader_version, variant);
  2571. glActiveTexture(GL_TEXTURE0);
  2572. glBindTexture(GL_TEXTURE_2D, rt->sdf_texture_write);
  2573. glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, rt->sdf_texture_process[0], 0);
  2574. glViewport(0, 0, size.width, size.height);
  2575. glEnable(GL_SCISSOR_TEST);
  2576. glScissor(0, 0, size.width, size.height);
  2577. copy_effects->draw_screen_triangle();
  2578. // Process
  2579. int stride = nearest_power_of_2_templated(MAX(size.width, size.height) / 2);
  2580. variant = CanvasSdfShaderGLES3::MODE_PROCESS;
  2581. success = sdf_shader.shader.version_bind_shader(sdf_shader.shader_version, variant);
  2582. if (!success) {
  2583. return;
  2584. }
  2585. sdf_shader.shader.version_set_uniform(CanvasSdfShaderGLES3::BASE_SIZE, r.size, sdf_shader.shader_version, variant);
  2586. sdf_shader.shader.version_set_uniform(CanvasSdfShaderGLES3::SIZE, size, sdf_shader.shader_version, variant);
  2587. sdf_shader.shader.version_set_uniform(CanvasSdfShaderGLES3::STRIDE, stride, sdf_shader.shader_version, variant);
  2588. sdf_shader.shader.version_set_uniform(CanvasSdfShaderGLES3::SHIFT, shift, sdf_shader.shader_version, variant);
  2589. bool swap = false;
  2590. //jumpflood
  2591. while (stride > 0) {
  2592. glBindTexture(GL_TEXTURE_2D, 0);
  2593. glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, rt->sdf_texture_process[swap ? 0 : 1], 0);
  2594. glBindTexture(GL_TEXTURE_2D, rt->sdf_texture_process[swap ? 1 : 0]);
  2595. sdf_shader.shader.version_set_uniform(CanvasSdfShaderGLES3::STRIDE, stride, sdf_shader.shader_version, variant);
  2596. copy_effects->draw_screen_triangle();
  2597. stride /= 2;
  2598. swap = !swap;
  2599. }
  2600. // Store
  2601. variant = shrink ? CanvasSdfShaderGLES3::MODE_STORE_SHRINK : CanvasSdfShaderGLES3::MODE_STORE;
  2602. success = sdf_shader.shader.version_bind_shader(sdf_shader.shader_version, variant);
  2603. if (!success) {
  2604. return;
  2605. }
  2606. sdf_shader.shader.version_set_uniform(CanvasSdfShaderGLES3::BASE_SIZE, r.size, sdf_shader.shader_version, variant);
  2607. sdf_shader.shader.version_set_uniform(CanvasSdfShaderGLES3::SIZE, size, sdf_shader.shader_version, variant);
  2608. sdf_shader.shader.version_set_uniform(CanvasSdfShaderGLES3::STRIDE, stride, sdf_shader.shader_version, variant);
  2609. sdf_shader.shader.version_set_uniform(CanvasSdfShaderGLES3::SHIFT, shift, sdf_shader.shader_version, variant);
  2610. glBindTexture(GL_TEXTURE_2D, 0);
  2611. glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, rt->sdf_texture_read, 0);
  2612. glBindTexture(GL_TEXTURE_2D, rt->sdf_texture_process[swap ? 1 : 0]);
  2613. copy_effects->draw_screen_triangle();
  2614. glBindTexture(GL_TEXTURE_2D, 0);
  2615. glBindFramebuffer(GL_FRAMEBUFFER, system_fbo);
  2616. glDeleteFramebuffers(1, &temp_fb);
  2617. glDisable(GL_SCISSOR_TEST);
  2618. }
  2619. void TextureStorage::render_target_copy_to_back_buffer(RID p_render_target, const Rect2i &p_region, bool p_gen_mipmaps) {
  2620. RenderTarget *rt = render_target_owner.get_or_null(p_render_target);
  2621. ERR_FAIL_NULL(rt);
  2622. ERR_FAIL_COND(rt->direct_to_screen);
  2623. if (rt->backbuffer_fbo == 0) {
  2624. _create_render_target_backbuffer(rt);
  2625. }
  2626. Rect2i region;
  2627. if (p_region == Rect2i()) {
  2628. region.size = rt->size;
  2629. } else {
  2630. region = Rect2i(Size2i(), rt->size).intersection(p_region);
  2631. if (region.size == Size2i()) {
  2632. return; //nothing to do
  2633. }
  2634. }
  2635. glDisable(GL_BLEND);
  2636. // Single texture copy for backbuffer.
  2637. glBindFramebuffer(GL_FRAMEBUFFER, rt->backbuffer_fbo);
  2638. glActiveTexture(GL_TEXTURE0);
  2639. glBindTexture(GL_TEXTURE_2D, rt->color);
  2640. Rect2 normalized_region = region;
  2641. normalized_region.position = normalized_region.position / Size2(rt->size);
  2642. normalized_region.size = normalized_region.size / Size2(rt->size);
  2643. GLES3::CopyEffects::get_singleton()->copy_to_and_from_rect(normalized_region);
  2644. if (p_gen_mipmaps) {
  2645. GLES3::CopyEffects::get_singleton()->gaussian_blur(rt->backbuffer, rt->mipmap_count, region, rt->size);
  2646. glBindFramebuffer(GL_FRAMEBUFFER, rt->backbuffer_fbo);
  2647. }
  2648. glEnable(GL_BLEND); // 2D starts with blend enabled.
  2649. }
  2650. void TextureStorage::render_target_clear_back_buffer(RID p_render_target, const Rect2i &p_region, const Color &p_color) {
  2651. RenderTarget *rt = render_target_owner.get_or_null(p_render_target);
  2652. ERR_FAIL_NULL(rt);
  2653. ERR_FAIL_COND(rt->direct_to_screen);
  2654. if (rt->backbuffer_fbo == 0) {
  2655. _create_render_target_backbuffer(rt);
  2656. }
  2657. Rect2i region;
  2658. if (p_region == Rect2i()) {
  2659. // Just do a full screen clear;
  2660. glBindFramebuffer(GL_FRAMEBUFFER, rt->backbuffer_fbo);
  2661. glClearColor(p_color.r, p_color.g, p_color.b, p_color.a);
  2662. glClear(GL_COLOR_BUFFER_BIT);
  2663. } else {
  2664. region = Rect2i(Size2i(), rt->size).intersection(p_region);
  2665. if (region.size == Size2i()) {
  2666. return; //nothing to do
  2667. }
  2668. glBindFramebuffer(GL_FRAMEBUFFER, rt->backbuffer_fbo);
  2669. GLES3::CopyEffects::get_singleton()->set_color(p_color, region);
  2670. }
  2671. }
  2672. void TextureStorage::render_target_gen_back_buffer_mipmaps(RID p_render_target, const Rect2i &p_region) {
  2673. RenderTarget *rt = render_target_owner.get_or_null(p_render_target);
  2674. ERR_FAIL_NULL(rt);
  2675. if (rt->backbuffer_fbo == 0) {
  2676. _create_render_target_backbuffer(rt);
  2677. }
  2678. Rect2i region;
  2679. if (p_region == Rect2i()) {
  2680. region.size = rt->size;
  2681. } else {
  2682. region = Rect2i(Size2i(), rt->size).intersection(p_region);
  2683. if (region.size == Size2i()) {
  2684. return; //nothing to do
  2685. }
  2686. }
  2687. glDisable(GL_BLEND);
  2688. GLES3::CopyEffects::get_singleton()->gaussian_blur(rt->backbuffer, rt->mipmap_count, region, rt->size);
  2689. glEnable(GL_BLEND); // 2D starts with blend enabled.
  2690. glBindFramebuffer(GL_FRAMEBUFFER, rt->backbuffer_fbo);
  2691. }
  2692. #endif // GLES3_ENABLED