texture_storage.cpp 79 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553
  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 "config.h"
  33. #include "drivers/gles3/effects/copy_effects.h"
  34. #ifdef ANDROID_ENABLED
  35. #define glFramebufferTextureMultiviewOVR GLES3::Config::get_singleton()->eglFramebufferTextureMultiviewOVR
  36. #endif
  37. using namespace GLES3;
  38. TextureStorage *TextureStorage::singleton = nullptr;
  39. TextureStorage *TextureStorage::get_singleton() {
  40. return singleton;
  41. }
  42. static const GLenum _cube_side_enum[6] = {
  43. GL_TEXTURE_CUBE_MAP_NEGATIVE_X,
  44. GL_TEXTURE_CUBE_MAP_POSITIVE_X,
  45. GL_TEXTURE_CUBE_MAP_NEGATIVE_Y,
  46. GL_TEXTURE_CUBE_MAP_POSITIVE_Y,
  47. GL_TEXTURE_CUBE_MAP_NEGATIVE_Z,
  48. GL_TEXTURE_CUBE_MAP_POSITIVE_Z,
  49. };
  50. TextureStorage::TextureStorage() {
  51. singleton = this;
  52. system_fbo = 0;
  53. { //create default textures
  54. { // White Textures
  55. Ref<Image> image = Image::create_empty(4, 4, true, Image::FORMAT_RGBA8);
  56. image->fill(Color(1, 1, 1, 1));
  57. image->generate_mipmaps();
  58. default_gl_textures[DEFAULT_GL_TEXTURE_WHITE] = texture_allocate();
  59. texture_2d_initialize(default_gl_textures[DEFAULT_GL_TEXTURE_WHITE], image);
  60. Vector<Ref<Image>> images;
  61. images.push_back(image);
  62. default_gl_textures[DEFAULT_GL_TEXTURE_2D_ARRAY_WHITE] = texture_allocate();
  63. texture_2d_layered_initialize(default_gl_textures[DEFAULT_GL_TEXTURE_2D_ARRAY_WHITE], images, RS::TEXTURE_LAYERED_2D_ARRAY);
  64. for (int i = 0; i < 3; i++) {
  65. images.push_back(image);
  66. }
  67. default_gl_textures[DEFAULT_GL_TEXTURE_3D_WHITE] = texture_allocate();
  68. texture_3d_initialize(default_gl_textures[DEFAULT_GL_TEXTURE_3D_WHITE], image->get_format(), 4, 4, 4, false, images);
  69. for (int i = 0; i < 2; i++) {
  70. images.push_back(image);
  71. }
  72. default_gl_textures[DEFAULT_GL_TEXTURE_CUBEMAP_WHITE] = texture_allocate();
  73. texture_2d_layered_initialize(default_gl_textures[DEFAULT_GL_TEXTURE_CUBEMAP_WHITE], images, RS::TEXTURE_LAYERED_CUBEMAP);
  74. }
  75. { // black
  76. Ref<Image> image = Image::create_empty(4, 4, true, Image::FORMAT_RGBA8);
  77. image->fill(Color(0, 0, 0, 1));
  78. image->generate_mipmaps();
  79. default_gl_textures[DEFAULT_GL_TEXTURE_BLACK] = texture_allocate();
  80. texture_2d_initialize(default_gl_textures[DEFAULT_GL_TEXTURE_BLACK], image);
  81. Vector<Ref<Image>> images;
  82. for (int i = 0; i < 4; i++) {
  83. images.push_back(image);
  84. }
  85. default_gl_textures[DEFAULT_GL_TEXTURE_3D_BLACK] = texture_allocate();
  86. texture_3d_initialize(default_gl_textures[DEFAULT_GL_TEXTURE_3D_BLACK], image->get_format(), 4, 4, 4, false, images);
  87. for (int i = 0; i < 2; i++) {
  88. images.push_back(image);
  89. }
  90. default_gl_textures[DEFAULT_GL_TEXTURE_CUBEMAP_BLACK] = texture_allocate();
  91. texture_2d_layered_initialize(default_gl_textures[DEFAULT_GL_TEXTURE_CUBEMAP_BLACK], images, RS::TEXTURE_LAYERED_CUBEMAP);
  92. }
  93. { // transparent black
  94. Ref<Image> image = Image::create_empty(4, 4, true, Image::FORMAT_RGBA8);
  95. image->fill(Color(0, 0, 0, 0));
  96. image->generate_mipmaps();
  97. default_gl_textures[DEFAULT_GL_TEXTURE_TRANSPARENT] = texture_allocate();
  98. texture_2d_initialize(default_gl_textures[DEFAULT_GL_TEXTURE_TRANSPARENT], image);
  99. }
  100. {
  101. Ref<Image> image = Image::create_empty(4, 4, true, Image::FORMAT_RGBA8);
  102. image->fill(Color(0.5, 0.5, 1, 1));
  103. image->generate_mipmaps();
  104. default_gl_textures[DEFAULT_GL_TEXTURE_NORMAL] = texture_allocate();
  105. texture_2d_initialize(default_gl_textures[DEFAULT_GL_TEXTURE_NORMAL], image);
  106. }
  107. {
  108. Ref<Image> image = Image::create_empty(4, 4, true, Image::FORMAT_RGBA8);
  109. image->fill(Color(1.0, 0.5, 1, 1));
  110. image->generate_mipmaps();
  111. default_gl_textures[DEFAULT_GL_TEXTURE_ANISO] = texture_allocate();
  112. texture_2d_initialize(default_gl_textures[DEFAULT_GL_TEXTURE_ANISO], image);
  113. }
  114. {
  115. unsigned char pixel_data[4 * 4 * 4];
  116. for (int i = 0; i < 16; i++) {
  117. pixel_data[i * 4 + 0] = 0;
  118. pixel_data[i * 4 + 1] = 0;
  119. pixel_data[i * 4 + 2] = 0;
  120. pixel_data[i * 4 + 3] = 0;
  121. }
  122. default_gl_textures[DEFAULT_GL_TEXTURE_2D_UINT] = texture_allocate();
  123. Texture texture;
  124. texture.width = 4;
  125. texture.height = 4;
  126. texture.format = Image::FORMAT_RGBA8;
  127. texture.type = Texture::TYPE_2D;
  128. texture.target = GL_TEXTURE_2D;
  129. texture.active = true;
  130. glGenTextures(1, &texture.tex_id);
  131. texture_owner.initialize_rid(default_gl_textures[DEFAULT_GL_TEXTURE_2D_UINT], texture);
  132. glBindTexture(GL_TEXTURE_2D, texture.tex_id);
  133. glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8UI, 4, 4, 0, GL_RGBA_INTEGER, GL_UNSIGNED_BYTE, pixel_data);
  134. texture.gl_set_filter(RS::CANVAS_ITEM_TEXTURE_FILTER_NEAREST);
  135. }
  136. {
  137. uint16_t pixel_data[4 * 4];
  138. for (int i = 0; i < 16; i++) {
  139. pixel_data[i] = Math::make_half_float(1.0f);
  140. }
  141. default_gl_textures[DEFAULT_GL_TEXTURE_DEPTH] = texture_allocate();
  142. Texture texture;
  143. texture.width = 4;
  144. texture.height = 4;
  145. texture.format = Image::FORMAT_RGBA8;
  146. texture.type = Texture::TYPE_2D;
  147. texture.target = GL_TEXTURE_2D;
  148. texture.active = true;
  149. glGenTextures(1, &texture.tex_id);
  150. texture_owner.initialize_rid(default_gl_textures[DEFAULT_GL_TEXTURE_DEPTH], texture);
  151. glBindTexture(GL_TEXTURE_2D, texture.tex_id);
  152. glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT16, 4, 4, 0, GL_DEPTH_COMPONENT, GL_UNSIGNED_SHORT, pixel_data);
  153. texture.gl_set_filter(RS::CANVAS_ITEM_TEXTURE_FILTER_NEAREST);
  154. }
  155. }
  156. glBindTexture(GL_TEXTURE_2D, 0);
  157. { // Atlas Texture initialize.
  158. uint8_t pixel_data[4 * 4 * 4];
  159. for (int i = 0; i < 16; i++) {
  160. pixel_data[i * 4 + 0] = 0;
  161. pixel_data[i * 4 + 1] = 0;
  162. pixel_data[i * 4 + 2] = 0;
  163. pixel_data[i * 4 + 3] = 255;
  164. }
  165. glGenTextures(1, &texture_atlas.texture);
  166. glBindTexture(GL_TEXTURE_2D, texture_atlas.texture);
  167. glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, 4, 4, 0, GL_RGBA, GL_UNSIGNED_BYTE, pixel_data);
  168. }
  169. glBindTexture(GL_TEXTURE_2D, 0);
  170. {
  171. sdf_shader.shader.initialize();
  172. sdf_shader.shader_version = sdf_shader.shader.version_create();
  173. }
  174. #ifdef GLES_OVER_GL
  175. glEnable(GL_PROGRAM_POINT_SIZE);
  176. #endif
  177. }
  178. TextureStorage::~TextureStorage() {
  179. singleton = nullptr;
  180. for (int i = 0; i < DEFAULT_GL_TEXTURE_MAX; i++) {
  181. texture_free(default_gl_textures[i]);
  182. }
  183. glDeleteTextures(1, &texture_atlas.texture);
  184. texture_atlas.texture = 0;
  185. glDeleteFramebuffers(1, &texture_atlas.framebuffer);
  186. texture_atlas.framebuffer = 0;
  187. sdf_shader.shader.version_free(sdf_shader.shader_version);
  188. }
  189. //TODO, move back to storage
  190. bool TextureStorage::can_create_resources_async() const {
  191. return false;
  192. }
  193. /* Canvas Texture API */
  194. RID TextureStorage::canvas_texture_allocate() {
  195. return canvas_texture_owner.allocate_rid();
  196. }
  197. void TextureStorage::canvas_texture_initialize(RID p_rid) {
  198. canvas_texture_owner.initialize_rid(p_rid);
  199. }
  200. void TextureStorage::canvas_texture_free(RID p_rid) {
  201. canvas_texture_owner.free(p_rid);
  202. }
  203. void TextureStorage::canvas_texture_set_channel(RID p_canvas_texture, RS::CanvasTextureChannel p_channel, RID p_texture) {
  204. CanvasTexture *ct = canvas_texture_owner.get_or_null(p_canvas_texture);
  205. ERR_FAIL_NULL(ct);
  206. switch (p_channel) {
  207. case RS::CANVAS_TEXTURE_CHANNEL_DIFFUSE: {
  208. ct->diffuse = p_texture;
  209. } break;
  210. case RS::CANVAS_TEXTURE_CHANNEL_NORMAL: {
  211. ct->normal_map = p_texture;
  212. } break;
  213. case RS::CANVAS_TEXTURE_CHANNEL_SPECULAR: {
  214. ct->specular = p_texture;
  215. } break;
  216. }
  217. }
  218. void TextureStorage::canvas_texture_set_shading_parameters(RID p_canvas_texture, const Color &p_specular_color, float p_shininess) {
  219. CanvasTexture *ct = canvas_texture_owner.get_or_null(p_canvas_texture);
  220. ERR_FAIL_NULL(ct);
  221. ct->specular_color.r = p_specular_color.r;
  222. ct->specular_color.g = p_specular_color.g;
  223. ct->specular_color.b = p_specular_color.b;
  224. ct->specular_color.a = p_shininess;
  225. }
  226. void TextureStorage::canvas_texture_set_texture_filter(RID p_canvas_texture, RS::CanvasItemTextureFilter p_filter) {
  227. CanvasTexture *ct = canvas_texture_owner.get_or_null(p_canvas_texture);
  228. ERR_FAIL_NULL(ct);
  229. ct->texture_filter = p_filter;
  230. }
  231. void TextureStorage::canvas_texture_set_texture_repeat(RID p_canvas_texture, RS::CanvasItemTextureRepeat p_repeat) {
  232. CanvasTexture *ct = canvas_texture_owner.get_or_null(p_canvas_texture);
  233. ERR_FAIL_NULL(ct);
  234. ct->texture_repeat = p_repeat;
  235. }
  236. /* Texture API */
  237. 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 {
  238. Config *config = Config::get_singleton();
  239. r_gl_format = 0;
  240. Ref<Image> image = p_image;
  241. r_compressed = false;
  242. r_real_format = p_format;
  243. bool need_decompress = false;
  244. bool decompress_ra_to_rg = false;
  245. switch (p_format) {
  246. case Image::FORMAT_L8: {
  247. #ifdef GLES_OVER_GL
  248. r_gl_internal_format = GL_R8;
  249. r_gl_format = GL_RED;
  250. r_gl_type = GL_UNSIGNED_BYTE;
  251. #else
  252. r_gl_internal_format = GL_LUMINANCE;
  253. r_gl_format = GL_LUMINANCE;
  254. r_gl_type = GL_UNSIGNED_BYTE;
  255. #endif
  256. } break;
  257. case Image::FORMAT_LA8: {
  258. #ifdef GLES_OVER_GL
  259. r_gl_internal_format = GL_RG8;
  260. r_gl_format = GL_RG;
  261. r_gl_type = GL_UNSIGNED_BYTE;
  262. #else
  263. r_gl_internal_format = GL_LUMINANCE_ALPHA;
  264. r_gl_format = GL_LUMINANCE_ALPHA;
  265. r_gl_type = GL_UNSIGNED_BYTE;
  266. #endif
  267. } break;
  268. case Image::FORMAT_R8: {
  269. r_gl_internal_format = GL_R8;
  270. r_gl_format = GL_RED;
  271. r_gl_type = GL_UNSIGNED_BYTE;
  272. } break;
  273. case Image::FORMAT_RG8: {
  274. r_gl_internal_format = GL_RG8;
  275. r_gl_format = GL_RG;
  276. r_gl_type = GL_UNSIGNED_BYTE;
  277. } break;
  278. case Image::FORMAT_RGB8: {
  279. r_gl_internal_format = GL_RGB8;
  280. r_gl_format = GL_RGB;
  281. r_gl_type = GL_UNSIGNED_BYTE;
  282. } break;
  283. case Image::FORMAT_RGBA8: {
  284. r_gl_format = GL_RGBA;
  285. r_gl_internal_format = GL_RGBA8;
  286. r_gl_type = GL_UNSIGNED_BYTE;
  287. } break;
  288. case Image::FORMAT_RGBA4444: {
  289. r_gl_internal_format = GL_RGBA4;
  290. r_gl_format = GL_RGBA;
  291. r_gl_type = GL_UNSIGNED_SHORT_4_4_4_4;
  292. } break;
  293. case Image::FORMAT_RF: {
  294. r_gl_internal_format = GL_R32F;
  295. r_gl_format = GL_RED;
  296. r_gl_type = GL_FLOAT;
  297. } break;
  298. case Image::FORMAT_RGF: {
  299. r_gl_internal_format = GL_RG32F;
  300. r_gl_format = GL_RG;
  301. r_gl_type = GL_FLOAT;
  302. } break;
  303. case Image::FORMAT_RGBF: {
  304. r_gl_internal_format = GL_RGB32F;
  305. r_gl_format = GL_RGB;
  306. r_gl_type = GL_FLOAT;
  307. } break;
  308. case Image::FORMAT_RGBAF: {
  309. r_gl_internal_format = GL_RGBA32F;
  310. r_gl_format = GL_RGBA;
  311. r_gl_type = GL_FLOAT;
  312. } break;
  313. case Image::FORMAT_RH: {
  314. r_gl_internal_format = GL_R16F;
  315. r_gl_format = GL_RED;
  316. r_gl_type = GL_HALF_FLOAT;
  317. } break;
  318. case Image::FORMAT_RGH: {
  319. r_gl_internal_format = GL_RG16F;
  320. r_gl_format = GL_RG;
  321. r_gl_type = GL_HALF_FLOAT;
  322. } break;
  323. case Image::FORMAT_RGBH: {
  324. r_gl_internal_format = GL_RGB16F;
  325. r_gl_format = GL_RGB;
  326. r_gl_type = GL_HALF_FLOAT;
  327. } break;
  328. case Image::FORMAT_RGBAH: {
  329. r_gl_internal_format = GL_RGBA16F;
  330. r_gl_format = GL_RGBA;
  331. r_gl_type = GL_HALF_FLOAT;
  332. } break;
  333. case Image::FORMAT_RGBE9995: {
  334. r_gl_internal_format = GL_RGB9_E5;
  335. r_gl_format = GL_RGB;
  336. r_gl_type = GL_UNSIGNED_INT_5_9_9_9_REV;
  337. } break;
  338. case Image::FORMAT_DXT1: {
  339. if (config->s3tc_supported) {
  340. r_gl_internal_format = _EXT_COMPRESSED_RGBA_S3TC_DXT1_EXT;
  341. r_gl_format = GL_RGBA;
  342. r_gl_type = GL_UNSIGNED_BYTE;
  343. r_compressed = true;
  344. } else {
  345. need_decompress = true;
  346. }
  347. } break;
  348. case Image::FORMAT_DXT3: {
  349. if (config->s3tc_supported) {
  350. r_gl_internal_format = _EXT_COMPRESSED_RGBA_S3TC_DXT3_EXT;
  351. r_gl_format = GL_RGBA;
  352. r_gl_type = GL_UNSIGNED_BYTE;
  353. r_compressed = true;
  354. } else {
  355. need_decompress = true;
  356. }
  357. } break;
  358. case Image::FORMAT_DXT5: {
  359. if (config->s3tc_supported) {
  360. r_gl_internal_format = _EXT_COMPRESSED_RGBA_S3TC_DXT5_EXT;
  361. r_gl_format = GL_RGBA;
  362. r_gl_type = GL_UNSIGNED_BYTE;
  363. r_compressed = true;
  364. } else {
  365. need_decompress = true;
  366. }
  367. } break;
  368. case Image::FORMAT_RGTC_R: {
  369. if (config->rgtc_supported) {
  370. r_gl_internal_format = _EXT_COMPRESSED_RED_RGTC1_EXT;
  371. r_gl_format = GL_RGBA;
  372. r_gl_type = GL_UNSIGNED_BYTE;
  373. r_compressed = true;
  374. } else {
  375. need_decompress = true;
  376. }
  377. } break;
  378. case Image::FORMAT_RGTC_RG: {
  379. if (config->rgtc_supported) {
  380. r_gl_internal_format = _EXT_COMPRESSED_RED_GREEN_RGTC2_EXT;
  381. r_gl_format = GL_RGBA;
  382. r_gl_type = GL_UNSIGNED_BYTE;
  383. r_compressed = true;
  384. } else {
  385. need_decompress = true;
  386. }
  387. } break;
  388. case Image::FORMAT_BPTC_RGBA: {
  389. if (config->bptc_supported) {
  390. r_gl_internal_format = _EXT_COMPRESSED_RGBA_BPTC_UNORM;
  391. r_gl_format = GL_RGBA;
  392. r_gl_type = GL_UNSIGNED_BYTE;
  393. r_compressed = true;
  394. } else {
  395. need_decompress = true;
  396. }
  397. } break;
  398. case Image::FORMAT_BPTC_RGBF: {
  399. if (config->bptc_supported) {
  400. r_gl_internal_format = _EXT_COMPRESSED_RGB_BPTC_SIGNED_FLOAT;
  401. r_gl_format = GL_RGB;
  402. r_gl_type = GL_FLOAT;
  403. r_compressed = true;
  404. } else {
  405. need_decompress = true;
  406. }
  407. } break;
  408. case Image::FORMAT_BPTC_RGBFU: {
  409. if (config->bptc_supported) {
  410. r_gl_internal_format = _EXT_COMPRESSED_RGB_BPTC_UNSIGNED_FLOAT;
  411. r_gl_format = GL_RGB;
  412. r_gl_type = GL_FLOAT;
  413. r_compressed = true;
  414. } else {
  415. need_decompress = true;
  416. }
  417. } break;
  418. case Image::FORMAT_ETC2_R11: {
  419. if (config->etc2_supported) {
  420. r_gl_internal_format = _EXT_COMPRESSED_R11_EAC;
  421. r_gl_format = GL_RED;
  422. r_gl_type = GL_UNSIGNED_BYTE;
  423. r_compressed = true;
  424. } else {
  425. need_decompress = true;
  426. }
  427. } break;
  428. case Image::FORMAT_ETC2_R11S: {
  429. if (config->etc2_supported) {
  430. r_gl_internal_format = _EXT_COMPRESSED_SIGNED_R11_EAC;
  431. r_gl_format = GL_RED;
  432. r_gl_type = GL_UNSIGNED_BYTE;
  433. r_compressed = true;
  434. } else {
  435. need_decompress = true;
  436. }
  437. } break;
  438. case Image::FORMAT_ETC2_RG11: {
  439. if (config->etc2_supported) {
  440. r_gl_internal_format = _EXT_COMPRESSED_RG11_EAC;
  441. r_gl_format = GL_RG;
  442. r_gl_type = GL_UNSIGNED_BYTE;
  443. r_compressed = true;
  444. } else {
  445. need_decompress = true;
  446. }
  447. } break;
  448. case Image::FORMAT_ETC2_RG11S: {
  449. if (config->etc2_supported) {
  450. r_gl_internal_format = _EXT_COMPRESSED_SIGNED_RG11_EAC;
  451. r_gl_format = GL_RG;
  452. r_gl_type = GL_UNSIGNED_BYTE;
  453. r_compressed = true;
  454. } else {
  455. need_decompress = true;
  456. }
  457. } break;
  458. case Image::FORMAT_ETC:
  459. case Image::FORMAT_ETC2_RGB8: {
  460. if (config->etc2_supported) {
  461. r_gl_internal_format = _EXT_COMPRESSED_RGB8_ETC2;
  462. r_gl_format = GL_RGB;
  463. r_gl_type = GL_UNSIGNED_BYTE;
  464. r_compressed = true;
  465. } else {
  466. need_decompress = true;
  467. }
  468. } break;
  469. case Image::FORMAT_ETC2_RGBA8: {
  470. if (config->etc2_supported) {
  471. r_gl_internal_format = _EXT_COMPRESSED_RGBA8_ETC2_EAC;
  472. r_gl_format = GL_RGBA;
  473. r_gl_type = GL_UNSIGNED_BYTE;
  474. r_compressed = true;
  475. } else {
  476. need_decompress = true;
  477. }
  478. } break;
  479. case Image::FORMAT_ETC2_RGB8A1: {
  480. if (config->etc2_supported) {
  481. r_gl_internal_format = _EXT_COMPRESSED_RGB8_PUNCHTHROUGH_ALPHA1_ETC2;
  482. r_gl_format = GL_RGBA;
  483. r_gl_type = GL_UNSIGNED_BYTE;
  484. r_compressed = true;
  485. } else {
  486. need_decompress = true;
  487. }
  488. } break;
  489. case Image::FORMAT_ETC2_RA_AS_RG: {
  490. #ifndef WEB_ENABLED
  491. if (config->etc2_supported) {
  492. r_gl_internal_format = _EXT_COMPRESSED_RGBA8_ETC2_EAC;
  493. r_gl_format = GL_RGBA;
  494. r_gl_type = GL_UNSIGNED_BYTE;
  495. r_compressed = true;
  496. } else
  497. #endif
  498. {
  499. need_decompress = true;
  500. }
  501. decompress_ra_to_rg = true;
  502. } break;
  503. case Image::FORMAT_DXT5_RA_AS_RG: {
  504. #ifndef WEB_ENABLED
  505. if (config->s3tc_supported) {
  506. r_gl_internal_format = _EXT_COMPRESSED_RGBA_S3TC_DXT5_EXT;
  507. r_gl_format = GL_RGBA;
  508. r_gl_type = GL_UNSIGNED_BYTE;
  509. r_compressed = true;
  510. } else
  511. #endif
  512. {
  513. need_decompress = true;
  514. }
  515. decompress_ra_to_rg = true;
  516. } break;
  517. case Image::FORMAT_ASTC_4x4: {
  518. if (config->astc_supported) {
  519. r_gl_internal_format = _EXT_COMPRESSED_RGBA_ASTC_4x4_KHR;
  520. r_gl_format = GL_RGBA;
  521. r_gl_type = GL_UNSIGNED_BYTE;
  522. r_compressed = true;
  523. } else {
  524. need_decompress = true;
  525. }
  526. } break;
  527. case Image::FORMAT_ASTC_4x4_HDR: {
  528. if (config->astc_hdr_supported) {
  529. r_gl_internal_format = _EXT_COMPRESSED_RGBA_ASTC_4x4_KHR;
  530. r_gl_format = GL_RGBA;
  531. r_gl_type = GL_UNSIGNED_BYTE;
  532. r_compressed = true;
  533. } else {
  534. need_decompress = true;
  535. }
  536. } break;
  537. case Image::FORMAT_ASTC_8x8: {
  538. if (config->astc_supported) {
  539. r_gl_internal_format = _EXT_COMPRESSED_RGBA_ASTC_8x8_KHR;
  540. r_gl_format = GL_RGBA;
  541. r_gl_type = GL_UNSIGNED_BYTE;
  542. r_compressed = true;
  543. } else {
  544. need_decompress = true;
  545. }
  546. } break;
  547. case Image::FORMAT_ASTC_8x8_HDR: {
  548. if (config->astc_hdr_supported) {
  549. r_gl_internal_format = _EXT_COMPRESSED_RGBA_ASTC_8x8_KHR;
  550. r_gl_format = GL_RGBA;
  551. r_gl_type = GL_UNSIGNED_BYTE;
  552. r_compressed = true;
  553. } else {
  554. need_decompress = true;
  555. }
  556. } break;
  557. default: {
  558. ERR_FAIL_V_MSG(Ref<Image>(), "Image Format: " + itos(p_format) + " is not supported by the OpenGL3 Renderer");
  559. }
  560. }
  561. if (need_decompress || p_force_decompress) {
  562. if (!image.is_null()) {
  563. image = image->duplicate();
  564. image->decompress();
  565. ERR_FAIL_COND_V(image->is_compressed(), image);
  566. if (decompress_ra_to_rg) {
  567. image->convert_ra_rgba8_to_rg();
  568. image->convert(Image::FORMAT_RG8);
  569. }
  570. switch (image->get_format()) {
  571. case Image::FORMAT_RG8: {
  572. r_gl_format = GL_RG;
  573. r_gl_internal_format = GL_RG8;
  574. r_gl_type = GL_UNSIGNED_BYTE;
  575. r_real_format = Image::FORMAT_RG8;
  576. r_compressed = false;
  577. } break;
  578. case Image::FORMAT_RGB8: {
  579. r_gl_format = GL_RGB;
  580. r_gl_internal_format = GL_RGB;
  581. r_gl_type = GL_UNSIGNED_BYTE;
  582. r_real_format = Image::FORMAT_RGB8;
  583. r_compressed = false;
  584. } break;
  585. case Image::FORMAT_RGBA8: {
  586. r_gl_format = GL_RGBA;
  587. r_gl_internal_format = GL_RGBA;
  588. r_gl_type = GL_UNSIGNED_BYTE;
  589. r_real_format = Image::FORMAT_RGBA8;
  590. r_compressed = false;
  591. } break;
  592. default: {
  593. image->convert(Image::FORMAT_RGBA8);
  594. r_gl_format = GL_RGBA;
  595. r_gl_internal_format = GL_RGBA;
  596. r_gl_type = GL_UNSIGNED_BYTE;
  597. r_real_format = Image::FORMAT_RGBA8;
  598. r_compressed = false;
  599. } break;
  600. }
  601. }
  602. return image;
  603. }
  604. return p_image;
  605. }
  606. RID TextureStorage::texture_allocate() {
  607. return texture_owner.allocate_rid();
  608. }
  609. void TextureStorage::texture_free(RID p_texture) {
  610. Texture *t = texture_owner.get_or_null(p_texture);
  611. ERR_FAIL_COND(!t);
  612. ERR_FAIL_COND(t->is_render_target);
  613. if (t->canvas_texture) {
  614. memdelete(t->canvas_texture);
  615. }
  616. if (t->tex_id != 0) {
  617. if (!t->is_external) {
  618. glDeleteTextures(1, &t->tex_id);
  619. }
  620. t->tex_id = 0;
  621. }
  622. if (t->is_proxy && t->proxy_to.is_valid()) {
  623. Texture *proxy_to = texture_owner.get_or_null(t->proxy_to);
  624. if (proxy_to) {
  625. proxy_to->proxies.erase(p_texture);
  626. }
  627. }
  628. texture_atlas_remove_texture(p_texture);
  629. for (int i = 0; i < t->proxies.size(); i++) {
  630. Texture *p = texture_owner.get_or_null(t->proxies[i]);
  631. ERR_CONTINUE(!p);
  632. p->proxy_to = RID();
  633. p->tex_id = 0;
  634. }
  635. texture_owner.free(p_texture);
  636. }
  637. void TextureStorage::texture_2d_initialize(RID p_texture, const Ref<Image> &p_image) {
  638. ERR_FAIL_COND(p_image.is_null());
  639. Texture texture;
  640. texture.width = p_image->get_width();
  641. texture.height = p_image->get_height();
  642. texture.alloc_width = texture.width;
  643. texture.alloc_height = texture.height;
  644. texture.mipmaps = p_image->get_mipmap_count() + 1;
  645. texture.format = p_image->get_format();
  646. texture.type = Texture::TYPE_2D;
  647. texture.target = GL_TEXTURE_2D;
  648. _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);
  649. //texture.total_data_size = p_image->get_image_data_size(); // verify that this returns size in bytes
  650. texture.active = true;
  651. glGenTextures(1, &texture.tex_id);
  652. texture_owner.initialize_rid(p_texture, texture);
  653. texture_set_data(p_texture, p_image);
  654. }
  655. void TextureStorage::texture_2d_layered_initialize(RID p_texture, const Vector<Ref<Image>> &p_layers, RS::TextureLayeredType p_layered_type) {
  656. texture_owner.initialize_rid(p_texture, Texture());
  657. }
  658. void TextureStorage::texture_3d_initialize(RID p_texture, Image::Format, int p_width, int p_height, int p_depth, bool p_mipmaps, const Vector<Ref<Image>> &p_data) {
  659. texture_owner.initialize_rid(p_texture, Texture());
  660. }
  661. // Called internally when texture_proxy_create(p_base) is called.
  662. // Note: p_base is the root and p_texture is the proxy.
  663. void TextureStorage::texture_proxy_initialize(RID p_texture, RID p_base) {
  664. Texture *texture = texture_owner.get_or_null(p_base);
  665. ERR_FAIL_COND(!texture);
  666. Texture proxy_tex;
  667. proxy_tex.copy_from(*texture);
  668. proxy_tex.proxy_to = p_base;
  669. proxy_tex.is_render_target = false;
  670. proxy_tex.is_proxy = true;
  671. proxy_tex.proxies.clear();
  672. texture->proxies.push_back(p_texture);
  673. texture_owner.initialize_rid(p_texture, proxy_tex);
  674. }
  675. RID TextureStorage::texture_create_external(Texture::Type p_type, Image::Format p_format, unsigned int p_image, int p_width, int p_height, int p_depth, int p_layers, RS::TextureLayeredType p_layered_type) {
  676. Texture texture;
  677. texture.active = true;
  678. texture.is_external = true;
  679. texture.type = p_type;
  680. switch (p_type) {
  681. case Texture::TYPE_2D: {
  682. texture.target = GL_TEXTURE_2D;
  683. } break;
  684. case Texture::TYPE_3D: {
  685. texture.target = GL_TEXTURE_3D;
  686. } break;
  687. case Texture::TYPE_LAYERED: {
  688. texture.target = GL_TEXTURE_2D_ARRAY;
  689. } break;
  690. }
  691. texture.real_format = texture.format = p_format;
  692. texture.tex_id = p_image;
  693. texture.alloc_width = texture.width = p_width;
  694. texture.alloc_height = texture.height = p_height;
  695. texture.depth = p_depth;
  696. texture.layers = p_layers;
  697. texture.layered_type = p_layered_type;
  698. return texture_owner.make_rid(texture);
  699. }
  700. void TextureStorage::texture_2d_update(RID p_texture, const Ref<Image> &p_image, int p_layer) {
  701. texture_set_data(p_texture, p_image, p_layer);
  702. #ifdef TOOLS_ENABLED
  703. Texture *tex = texture_owner.get_or_null(p_texture);
  704. ERR_FAIL_COND(!tex);
  705. tex->image_cache_2d.unref();
  706. #endif
  707. }
  708. void TextureStorage::texture_proxy_update(RID p_texture, RID p_proxy_to) {
  709. Texture *tex = texture_owner.get_or_null(p_texture);
  710. ERR_FAIL_COND(!tex);
  711. ERR_FAIL_COND(!tex->is_proxy);
  712. Texture *proxy_to = texture_owner.get_or_null(p_proxy_to);
  713. ERR_FAIL_COND(!proxy_to);
  714. ERR_FAIL_COND(proxy_to->is_proxy);
  715. if (tex->proxy_to.is_valid()) {
  716. Texture *prev_tex = texture_owner.get_or_null(tex->proxy_to);
  717. ERR_FAIL_COND(!prev_tex);
  718. prev_tex->proxies.erase(p_texture);
  719. }
  720. *tex = *proxy_to;
  721. tex->proxy_to = p_proxy_to;
  722. tex->is_render_target = false;
  723. tex->is_proxy = true;
  724. tex->proxies.clear();
  725. tex->canvas_texture = nullptr;
  726. tex->tex_id = 0;
  727. proxy_to->proxies.push_back(p_texture);
  728. }
  729. void TextureStorage::texture_2d_placeholder_initialize(RID p_texture) {
  730. //this could be better optimized to reuse an existing image , done this way
  731. //for now to get it working
  732. Ref<Image> image = Image::create_empty(4, 4, false, Image::FORMAT_RGBA8);
  733. image->fill(Color(1, 0, 1, 1));
  734. texture_2d_initialize(p_texture, image);
  735. }
  736. void TextureStorage::texture_2d_layered_placeholder_initialize(RID p_texture, RenderingServer::TextureLayeredType p_layered_type) {
  737. //this could be better optimized to reuse an existing image , done this way
  738. //for now to get it working
  739. Ref<Image> image = Image::create_empty(4, 4, false, Image::FORMAT_RGBA8);
  740. image->fill(Color(1, 0, 1, 1));
  741. Vector<Ref<Image>> images;
  742. if (p_layered_type == RS::TEXTURE_LAYERED_2D_ARRAY) {
  743. images.push_back(image);
  744. } else {
  745. //cube
  746. for (int i = 0; i < 6; i++) {
  747. images.push_back(image);
  748. }
  749. }
  750. texture_2d_layered_initialize(p_texture, images, p_layered_type);
  751. }
  752. void TextureStorage::texture_3d_placeholder_initialize(RID p_texture) {
  753. //this could be better optimized to reuse an existing image , done this way
  754. //for now to get it working
  755. Ref<Image> image = Image::create_empty(4, 4, false, Image::FORMAT_RGBA8);
  756. image->fill(Color(1, 0, 1, 1));
  757. Vector<Ref<Image>> images;
  758. //cube
  759. for (int i = 0; i < 4; i++) {
  760. images.push_back(image);
  761. }
  762. texture_3d_initialize(p_texture, Image::FORMAT_RGBA8, 4, 4, 4, false, images);
  763. }
  764. Ref<Image> TextureStorage::texture_2d_get(RID p_texture) const {
  765. Texture *texture = texture_owner.get_or_null(p_texture);
  766. ERR_FAIL_COND_V(!texture, Ref<Image>());
  767. #ifdef TOOLS_ENABLED
  768. if (texture->image_cache_2d.is_valid() && !texture->is_render_target) {
  769. return texture->image_cache_2d;
  770. }
  771. #endif
  772. #ifdef GLES_OVER_GL
  773. // OpenGL 3.3 supports glGetTexImage which is faster and simpler than glReadPixels.
  774. // It also allows for reading compressed textures, mipmaps, and more formats.
  775. Vector<uint8_t> data;
  776. int data_size = Image::get_image_data_size(texture->alloc_width, texture->alloc_height, texture->real_format, texture->mipmaps > 1);
  777. data.resize(data_size * 2); //add some memory at the end, just in case for buggy drivers
  778. uint8_t *w = data.ptrw();
  779. glActiveTexture(GL_TEXTURE0);
  780. glBindTexture(texture->target, texture->tex_id);
  781. glBindBuffer(GL_PIXEL_PACK_BUFFER, 0);
  782. for (int i = 0; i < texture->mipmaps; i++) {
  783. int ofs = Image::get_image_mipmap_offset(texture->alloc_width, texture->alloc_height, texture->real_format, i);
  784. if (texture->compressed) {
  785. glPixelStorei(GL_PACK_ALIGNMENT, 4);
  786. glGetCompressedTexImage(texture->target, i, &w[ofs]);
  787. } else {
  788. glPixelStorei(GL_PACK_ALIGNMENT, 1);
  789. glGetTexImage(texture->target, i, texture->gl_format_cache, texture->gl_type_cache, &w[ofs]);
  790. }
  791. }
  792. data.resize(data_size);
  793. ERR_FAIL_COND_V(data.size() == 0, Ref<Image>());
  794. Ref<Image> image = Image::create_from_data(texture->width, texture->height, texture->mipmaps > 1, texture->real_format, data);
  795. ERR_FAIL_COND_V(image->is_empty(), Ref<Image>());
  796. if (texture->format != texture->real_format) {
  797. image->convert(texture->format);
  798. }
  799. #else
  800. Vector<uint8_t> data;
  801. // On web and mobile we always read an RGBA8 image with no mipmaps.
  802. int data_size = Image::get_image_data_size(texture->alloc_width, texture->alloc_height, Image::FORMAT_RGBA8, false);
  803. data.resize(data_size * 2); //add some memory at the end, just in case for buggy drivers
  804. uint8_t *w = data.ptrw();
  805. GLuint temp_framebuffer;
  806. glGenFramebuffers(1, &temp_framebuffer);
  807. GLuint temp_color_texture;
  808. glGenTextures(1, &temp_color_texture);
  809. glBindFramebuffer(GL_FRAMEBUFFER, temp_framebuffer);
  810. glBindTexture(GL_TEXTURE_2D, temp_color_texture);
  811. glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, texture->alloc_width, texture->alloc_height, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
  812. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  813. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  814. glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, temp_color_texture, 0);
  815. glDepthMask(GL_FALSE);
  816. glDisable(GL_DEPTH_TEST);
  817. glDisable(GL_CULL_FACE);
  818. glDisable(GL_BLEND);
  819. glDepthFunc(GL_LEQUAL);
  820. glColorMask(1, 1, 1, 1);
  821. glActiveTexture(GL_TEXTURE0);
  822. glBindTexture(GL_TEXTURE_2D, texture->tex_id);
  823. glViewport(0, 0, texture->alloc_width, texture->alloc_height);
  824. glClearColor(0.0, 0.0, 0.0, 0.0);
  825. glClear(GL_COLOR_BUFFER_BIT);
  826. CopyEffects::get_singleton()->copy_to_rect(Rect2i(0, 0, 1.0, 1.0));
  827. glReadPixels(0, 0, texture->alloc_width, texture->alloc_height, GL_RGBA, GL_UNSIGNED_BYTE, &w[0]);
  828. glBindFramebuffer(GL_FRAMEBUFFER, 0);
  829. glDeleteTextures(1, &temp_color_texture);
  830. glDeleteFramebuffers(1, &temp_framebuffer);
  831. data.resize(data_size);
  832. ERR_FAIL_COND_V(data.size() == 0, Ref<Image>());
  833. Ref<Image> image = Image::create_from_data(texture->width, texture->height, false, Image::FORMAT_RGBA8, data);
  834. ERR_FAIL_COND_V(image->is_empty(), Ref<Image>());
  835. if (texture->format != Image::FORMAT_RGBA8) {
  836. image->convert(texture->format);
  837. }
  838. if (texture->mipmaps > 1) {
  839. image->generate_mipmaps();
  840. }
  841. #endif
  842. #ifdef TOOLS_ENABLED
  843. if (Engine::get_singleton()->is_editor_hint() && !texture->is_render_target) {
  844. texture->image_cache_2d = image;
  845. }
  846. #endif
  847. return image;
  848. }
  849. void TextureStorage::texture_replace(RID p_texture, RID p_by_texture) {
  850. Texture *tex_to = texture_owner.get_or_null(p_texture);
  851. ERR_FAIL_COND(!tex_to);
  852. ERR_FAIL_COND(tex_to->is_proxy); //can't replace proxy
  853. Texture *tex_from = texture_owner.get_or_null(p_by_texture);
  854. ERR_FAIL_COND(!tex_from);
  855. ERR_FAIL_COND(tex_from->is_proxy); //can't replace proxy
  856. if (tex_to == tex_from) {
  857. return;
  858. }
  859. if (tex_to->canvas_texture) {
  860. memdelete(tex_to->canvas_texture);
  861. tex_to->canvas_texture = nullptr;
  862. }
  863. if (tex_to->tex_id) {
  864. glDeleteTextures(1, &tex_to->tex_id);
  865. tex_to->tex_id = 0;
  866. }
  867. Vector<RID> proxies_to_update = tex_to->proxies;
  868. Vector<RID> proxies_to_redirect = tex_from->proxies;
  869. tex_to->copy_from(*tex_from);
  870. tex_to->proxies = proxies_to_update; //restore proxies, so they can be updated
  871. if (tex_to->canvas_texture) {
  872. tex_to->canvas_texture->diffuse = p_texture; //update
  873. }
  874. for (int i = 0; i < proxies_to_update.size(); i++) {
  875. texture_proxy_update(proxies_to_update[i], p_texture);
  876. }
  877. for (int i = 0; i < proxies_to_redirect.size(); i++) {
  878. texture_proxy_update(proxies_to_redirect[i], p_texture);
  879. }
  880. //delete last, so proxies can be updated
  881. texture_owner.free(p_by_texture);
  882. texture_atlas_mark_dirty_on_texture(p_texture);
  883. }
  884. void TextureStorage::texture_set_size_override(RID p_texture, int p_width, int p_height) {
  885. Texture *texture = texture_owner.get_or_null(p_texture);
  886. ERR_FAIL_COND(!texture);
  887. ERR_FAIL_COND(texture->is_render_target);
  888. ERR_FAIL_COND(p_width <= 0 || p_width > 16384);
  889. ERR_FAIL_COND(p_height <= 0 || p_height > 16384);
  890. //real texture size is in alloc width and height
  891. texture->width = p_width;
  892. texture->height = p_height;
  893. }
  894. void TextureStorage::texture_set_path(RID p_texture, const String &p_path) {
  895. Texture *texture = texture_owner.get_or_null(p_texture);
  896. ERR_FAIL_COND(!texture);
  897. texture->path = p_path;
  898. }
  899. String TextureStorage::texture_get_path(RID p_texture) const {
  900. Texture *texture = texture_owner.get_or_null(p_texture);
  901. ERR_FAIL_COND_V(!texture, "");
  902. return texture->path;
  903. }
  904. void TextureStorage::texture_set_detect_3d_callback(RID p_texture, RS::TextureDetectCallback p_callback, void *p_userdata) {
  905. Texture *texture = texture_owner.get_or_null(p_texture);
  906. ERR_FAIL_COND(!texture);
  907. texture->detect_3d_callback = p_callback;
  908. texture->detect_3d_callback_ud = p_userdata;
  909. }
  910. void TextureStorage::texture_set_detect_srgb_callback(RID p_texture, RS::TextureDetectCallback p_callback, void *p_userdata) {
  911. }
  912. void TextureStorage::texture_set_detect_normal_callback(RID p_texture, RS::TextureDetectCallback p_callback, void *p_userdata) {
  913. Texture *texture = texture_owner.get_or_null(p_texture);
  914. ERR_FAIL_COND(!texture);
  915. texture->detect_normal_callback = p_callback;
  916. texture->detect_normal_callback_ud = p_userdata;
  917. }
  918. void TextureStorage::texture_set_detect_roughness_callback(RID p_texture, RS::TextureDetectRoughnessCallback p_callback, void *p_userdata) {
  919. Texture *texture = texture_owner.get_or_null(p_texture);
  920. ERR_FAIL_COND(!texture);
  921. texture->detect_roughness_callback = p_callback;
  922. texture->detect_roughness_callback_ud = p_userdata;
  923. }
  924. void TextureStorage::texture_debug_usage(List<RS::TextureInfo> *r_info) {
  925. List<RID> textures;
  926. texture_owner.get_owned_list(&textures);
  927. for (List<RID>::Element *E = textures.front(); E; E = E->next()) {
  928. Texture *t = texture_owner.get_or_null(E->get());
  929. if (!t) {
  930. continue;
  931. }
  932. RS::TextureInfo tinfo;
  933. tinfo.path = t->path;
  934. tinfo.format = t->format;
  935. tinfo.width = t->alloc_width;
  936. tinfo.height = t->alloc_height;
  937. tinfo.depth = 0;
  938. tinfo.bytes = t->total_data_size;
  939. r_info->push_back(tinfo);
  940. }
  941. }
  942. void TextureStorage::texture_set_force_redraw_if_visible(RID p_texture, bool p_enable) {
  943. Texture *texture = texture_owner.get_or_null(p_texture);
  944. ERR_FAIL_COND(!texture);
  945. texture->redraw_if_visible = p_enable;
  946. }
  947. Size2 TextureStorage::texture_size_with_proxy(RID p_texture) {
  948. const Texture *texture = texture_owner.get_or_null(p_texture);
  949. ERR_FAIL_COND_V(!texture, Size2());
  950. if (texture->is_proxy) {
  951. const Texture *proxy = texture_owner.get_or_null(texture->proxy_to);
  952. return Size2(proxy->width, proxy->height);
  953. } else {
  954. return Size2(texture->width, texture->height);
  955. }
  956. }
  957. RID TextureStorage::texture_get_rd_texture_rid(RID p_texture, bool p_srgb) const {
  958. return RID();
  959. }
  960. void TextureStorage::texture_set_data(RID p_texture, const Ref<Image> &p_image, int p_layer) {
  961. Texture *texture = texture_owner.get_or_null(p_texture);
  962. ERR_FAIL_COND(!texture);
  963. if (texture->target == GL_TEXTURE_3D) {
  964. // Target is set to a 3D texture or array texture, exit early to avoid spamming errors
  965. return;
  966. }
  967. ERR_FAIL_COND(!texture->active);
  968. ERR_FAIL_COND(texture->is_render_target);
  969. ERR_FAIL_COND(p_image.is_null());
  970. ERR_FAIL_COND(texture->format != p_image->get_format());
  971. ERR_FAIL_COND(!p_image->get_width());
  972. ERR_FAIL_COND(!p_image->get_height());
  973. // ERR_FAIL_COND(texture->type == RS::TEXTURE_TYPE_EXTERNAL);
  974. GLenum type;
  975. GLenum format;
  976. GLenum internal_format;
  977. bool compressed = false;
  978. // print_line("texture_set_data width " + itos (p_image->get_width()) + " height " + itos(p_image->get_height()));
  979. Image::Format real_format;
  980. Ref<Image> img = _get_gl_image_and_format(p_image, p_image->get_format(), real_format, format, internal_format, type, compressed, texture->resize_to_po2);
  981. ERR_FAIL_COND(img.is_null());
  982. if (texture->resize_to_po2) {
  983. if (p_image->is_compressed()) {
  984. 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.");
  985. }
  986. if (img == p_image) {
  987. img = img->duplicate();
  988. }
  989. img->resize_to_po2(false);
  990. }
  991. GLenum blit_target = (texture->target == GL_TEXTURE_CUBE_MAP) ? _cube_side_enum[p_layer] : texture->target;
  992. Vector<uint8_t> read = img->get_data();
  993. glActiveTexture(GL_TEXTURE0);
  994. glBindTexture(texture->target, texture->tex_id);
  995. // set filtering and repeat state to default
  996. texture->gl_set_filter(RS::CANVAS_ITEM_TEXTURE_FILTER_NEAREST);
  997. texture->gl_set_repeat(RS::CANVAS_ITEM_TEXTURE_REPEAT_ENABLED);
  998. #ifndef WEB_ENABLED
  999. switch (texture->format) {
  1000. #ifdef GLES_OVER_GL
  1001. case Image::FORMAT_L8: {
  1002. glTexParameteri(texture->target, GL_TEXTURE_SWIZZLE_R, GL_RED);
  1003. glTexParameteri(texture->target, GL_TEXTURE_SWIZZLE_G, GL_RED);
  1004. glTexParameteri(texture->target, GL_TEXTURE_SWIZZLE_B, GL_RED);
  1005. glTexParameteri(texture->target, GL_TEXTURE_SWIZZLE_A, GL_ONE);
  1006. } break;
  1007. case Image::FORMAT_LA8: {
  1008. glTexParameteri(texture->target, GL_TEXTURE_SWIZZLE_R, GL_RED);
  1009. glTexParameteri(texture->target, GL_TEXTURE_SWIZZLE_G, GL_RED);
  1010. glTexParameteri(texture->target, GL_TEXTURE_SWIZZLE_B, GL_RED);
  1011. glTexParameteri(texture->target, GL_TEXTURE_SWIZZLE_A, GL_GREEN);
  1012. } break;
  1013. #endif // GLES3_OVER_GL
  1014. case Image::FORMAT_ETC2_RA_AS_RG:
  1015. case Image::FORMAT_DXT5_RA_AS_RG: {
  1016. glTexParameteri(texture->target, GL_TEXTURE_SWIZZLE_R, GL_RED);
  1017. if (texture->format == real_format) {
  1018. // Swizzle RA from compressed texture into RG
  1019. glTexParameteri(texture->target, GL_TEXTURE_SWIZZLE_G, GL_ALPHA);
  1020. } else {
  1021. // Converted textures are already in RG, leave as-is
  1022. glTexParameteri(texture->target, GL_TEXTURE_SWIZZLE_G, GL_GREEN);
  1023. }
  1024. glTexParameteri(texture->target, GL_TEXTURE_SWIZZLE_B, GL_ZERO);
  1025. glTexParameteri(texture->target, GL_TEXTURE_SWIZZLE_A, GL_ONE);
  1026. } break;
  1027. default: {
  1028. glTexParameteri(texture->target, GL_TEXTURE_SWIZZLE_R, GL_RED);
  1029. glTexParameteri(texture->target, GL_TEXTURE_SWIZZLE_G, GL_GREEN);
  1030. glTexParameteri(texture->target, GL_TEXTURE_SWIZZLE_B, GL_BLUE);
  1031. glTexParameteri(texture->target, GL_TEXTURE_SWIZZLE_A, GL_ALPHA);
  1032. } break;
  1033. }
  1034. #endif // WEB_ENABLED
  1035. int mipmaps = img->has_mipmaps() ? img->get_mipmap_count() + 1 : 1;
  1036. int w = img->get_width();
  1037. int h = img->get_height();
  1038. int tsize = 0;
  1039. for (int i = 0; i < mipmaps; i++) {
  1040. int size, ofs;
  1041. img->get_mipmap_offset_and_size(i, ofs, size);
  1042. if (compressed) {
  1043. glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
  1044. int bw = w;
  1045. int bh = h;
  1046. glCompressedTexImage2D(blit_target, i, internal_format, bw, bh, 0, size, &read[ofs]);
  1047. } else {
  1048. glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
  1049. if (texture->target == GL_TEXTURE_2D_ARRAY) {
  1050. glTexSubImage3D(GL_TEXTURE_2D_ARRAY, i, 0, 0, p_layer, w, h, 0, format, type, &read[ofs]);
  1051. } else {
  1052. glTexImage2D(blit_target, i, internal_format, w, h, 0, format, type, &read[ofs]);
  1053. }
  1054. }
  1055. tsize += size;
  1056. w = MAX(1, w >> 1);
  1057. h = MAX(1, h >> 1);
  1058. }
  1059. // info.texture_mem -= texture->total_data_size; // TODO make this work again!!
  1060. texture->total_data_size = tsize;
  1061. // info.texture_mem += texture->total_data_size; // TODO make this work again!!
  1062. // printf("texture: %i x %i - size: %i - total: %i\n", texture->width, texture->height, tsize, info.texture_mem);
  1063. texture->stored_cube_sides |= (1 << p_layer);
  1064. texture->mipmaps = mipmaps;
  1065. }
  1066. void TextureStorage::texture_set_data_partial(RID p_texture, const Ref<Image> &p_image, int src_x, int src_y, int src_w, int src_h, int dst_x, int dst_y, int p_dst_mip, int p_layer) {
  1067. ERR_PRINT("Not implemented yet, sorry :(");
  1068. }
  1069. Image::Format TextureStorage::texture_get_format(RID p_texture) const {
  1070. Texture *texture = texture_owner.get_or_null(p_texture);
  1071. ERR_FAIL_COND_V(!texture, Image::FORMAT_L8);
  1072. return texture->format;
  1073. }
  1074. uint32_t TextureStorage::texture_get_texid(RID p_texture) const {
  1075. Texture *texture = texture_owner.get_or_null(p_texture);
  1076. ERR_FAIL_COND_V(!texture, 0);
  1077. return texture->tex_id;
  1078. }
  1079. uint32_t TextureStorage::texture_get_width(RID p_texture) const {
  1080. Texture *texture = texture_owner.get_or_null(p_texture);
  1081. ERR_FAIL_COND_V(!texture, 0);
  1082. return texture->width;
  1083. }
  1084. uint32_t TextureStorage::texture_get_height(RID p_texture) const {
  1085. Texture *texture = texture_owner.get_or_null(p_texture);
  1086. ERR_FAIL_COND_V(!texture, 0);
  1087. return texture->height;
  1088. }
  1089. uint32_t TextureStorage::texture_get_depth(RID p_texture) const {
  1090. Texture *texture = texture_owner.get_or_null(p_texture);
  1091. ERR_FAIL_COND_V(!texture, 0);
  1092. return texture->depth;
  1093. }
  1094. void TextureStorage::texture_bind(RID p_texture, uint32_t p_texture_no) {
  1095. Texture *texture = texture_owner.get_or_null(p_texture);
  1096. ERR_FAIL_COND(!texture);
  1097. glActiveTexture(GL_TEXTURE0 + p_texture_no);
  1098. glBindTexture(texture->target, texture->tex_id);
  1099. }
  1100. RID TextureStorage::texture_create_radiance_cubemap(RID p_source, int p_resolution) const {
  1101. return RID();
  1102. }
  1103. /* TEXTURE ATLAS API */
  1104. void TextureStorage::texture_add_to_texture_atlas(RID p_texture) {
  1105. if (!texture_atlas.textures.has(p_texture)) {
  1106. TextureAtlas::Texture t;
  1107. t.users = 1;
  1108. texture_atlas.textures[p_texture] = t;
  1109. texture_atlas.dirty = true;
  1110. } else {
  1111. TextureAtlas::Texture *t = texture_atlas.textures.getptr(p_texture);
  1112. t->users++;
  1113. }
  1114. }
  1115. void TextureStorage::texture_remove_from_texture_atlas(RID p_texture) {
  1116. TextureAtlas::Texture *t = texture_atlas.textures.getptr(p_texture);
  1117. ERR_FAIL_COND(!t);
  1118. t->users--;
  1119. if (t->users == 0) {
  1120. texture_atlas.textures.erase(p_texture);
  1121. // Do not mark it dirty, there is no need to since it remains working.
  1122. }
  1123. }
  1124. void TextureStorage::texture_atlas_mark_dirty_on_texture(RID p_texture) {
  1125. if (texture_atlas.textures.has(p_texture)) {
  1126. texture_atlas.dirty = true; // Mark it dirty since it was most likely modified.
  1127. }
  1128. }
  1129. void TextureStorage::texture_atlas_remove_texture(RID p_texture) {
  1130. if (texture_atlas.textures.has(p_texture)) {
  1131. texture_atlas.textures.erase(p_texture);
  1132. // There is not much a point of making it dirty, texture can be removed next time the atlas is updated.
  1133. }
  1134. }
  1135. GLuint TextureStorage::texture_atlas_get_texture() const {
  1136. return texture_atlas.texture;
  1137. }
  1138. void TextureStorage::update_texture_atlas() {
  1139. CopyEffects *copy_effects = CopyEffects::get_singleton();
  1140. ERR_FAIL_NULL(copy_effects);
  1141. if (!texture_atlas.dirty) {
  1142. return; //nothing to do
  1143. }
  1144. texture_atlas.dirty = false;
  1145. if (texture_atlas.texture != 0) {
  1146. glDeleteTextures(1, &texture_atlas.texture);
  1147. texture_atlas.texture = 0;
  1148. glDeleteFramebuffers(1, &texture_atlas.framebuffer);
  1149. texture_atlas.framebuffer = 0;
  1150. }
  1151. const int border = 2;
  1152. if (texture_atlas.textures.size()) {
  1153. //generate atlas
  1154. Vector<TextureAtlas::SortItem> itemsv;
  1155. itemsv.resize(texture_atlas.textures.size());
  1156. int base_size = 8;
  1157. int idx = 0;
  1158. for (const KeyValue<RID, TextureAtlas::Texture> &E : texture_atlas.textures) {
  1159. TextureAtlas::SortItem &si = itemsv.write[idx];
  1160. Texture *src_tex = get_texture(E.key);
  1161. si.size.width = (src_tex->width / border) + 1;
  1162. si.size.height = (src_tex->height / border) + 1;
  1163. si.pixel_size = Size2i(src_tex->width, src_tex->height);
  1164. if (base_size < si.size.width) {
  1165. base_size = nearest_power_of_2_templated(si.size.width);
  1166. }
  1167. si.texture = E.key;
  1168. idx++;
  1169. }
  1170. //sort items by size
  1171. itemsv.sort();
  1172. //attempt to create atlas
  1173. int item_count = itemsv.size();
  1174. TextureAtlas::SortItem *items = itemsv.ptrw();
  1175. int atlas_height = 0;
  1176. while (true) {
  1177. Vector<int> v_offsetsv;
  1178. v_offsetsv.resize(base_size);
  1179. int *v_offsets = v_offsetsv.ptrw();
  1180. memset(v_offsets, 0, sizeof(int) * base_size);
  1181. int max_height = 0;
  1182. for (int i = 0; i < item_count; i++) {
  1183. //best fit
  1184. TextureAtlas::SortItem &si = items[i];
  1185. int best_idx = -1;
  1186. int best_height = 0x7FFFFFFF;
  1187. for (int j = 0; j <= base_size - si.size.width; j++) {
  1188. int height = 0;
  1189. for (int k = 0; k < si.size.width; k++) {
  1190. int h = v_offsets[k + j];
  1191. if (h > height) {
  1192. height = h;
  1193. if (height > best_height) {
  1194. break; //already bad
  1195. }
  1196. }
  1197. }
  1198. if (height < best_height) {
  1199. best_height = height;
  1200. best_idx = j;
  1201. }
  1202. }
  1203. //update
  1204. for (int k = 0; k < si.size.width; k++) {
  1205. v_offsets[k + best_idx] = best_height + si.size.height;
  1206. }
  1207. si.pos.x = best_idx;
  1208. si.pos.y = best_height;
  1209. if (si.pos.y + si.size.height > max_height) {
  1210. max_height = si.pos.y + si.size.height;
  1211. }
  1212. }
  1213. if (max_height <= base_size * 2) {
  1214. atlas_height = max_height;
  1215. break; //good ratio, break;
  1216. }
  1217. base_size *= 2;
  1218. }
  1219. texture_atlas.size.width = base_size * border;
  1220. texture_atlas.size.height = nearest_power_of_2_templated(atlas_height * border);
  1221. for (int i = 0; i < item_count; i++) {
  1222. TextureAtlas::Texture *t = texture_atlas.textures.getptr(items[i].texture);
  1223. t->uv_rect.position = items[i].pos * border + Vector2i(border / 2, border / 2);
  1224. t->uv_rect.size = items[i].pixel_size;
  1225. t->uv_rect.position /= Size2(texture_atlas.size);
  1226. t->uv_rect.size /= Size2(texture_atlas.size);
  1227. }
  1228. } else {
  1229. texture_atlas.size.width = 4;
  1230. texture_atlas.size.height = 4;
  1231. }
  1232. { // Atlas Texture initialize.
  1233. // TODO validate texture atlas size with maximum texture size
  1234. glGenTextures(1, &texture_atlas.texture);
  1235. glActiveTexture(GL_TEXTURE0);
  1236. glBindTexture(GL_TEXTURE_2D, texture_atlas.texture);
  1237. glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, texture_atlas.size.width, texture_atlas.size.height, 0, GL_RGBA, GL_UNSIGNED_BYTE, nullptr);
  1238. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  1239. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  1240. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  1241. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  1242. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_BASE_LEVEL, 0);
  1243. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, 1);
  1244. glGenFramebuffers(1, &texture_atlas.framebuffer);
  1245. glBindFramebuffer(GL_FRAMEBUFFER, texture_atlas.framebuffer);
  1246. glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, texture_atlas.texture, 0);
  1247. GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
  1248. if (status != GL_FRAMEBUFFER_COMPLETE) {
  1249. glDeleteFramebuffers(1, &texture_atlas.framebuffer);
  1250. texture_atlas.framebuffer = 0;
  1251. glDeleteTextures(1, &texture_atlas.texture);
  1252. texture_atlas.texture = 0;
  1253. WARN_PRINT("Could not create texture atlas, status: " + get_framebuffer_error(status));
  1254. return;
  1255. }
  1256. glViewport(0, 0, texture_atlas.size.width, texture_atlas.size.height);
  1257. glClearColor(0.0, 0.0, 0.0, 0.0);
  1258. glClear(GL_COLOR_BUFFER_BIT);
  1259. glBindTexture(GL_TEXTURE_2D, 0);
  1260. }
  1261. glDisable(GL_BLEND);
  1262. if (texture_atlas.textures.size()) {
  1263. for (const KeyValue<RID, TextureAtlas::Texture> &E : texture_atlas.textures) {
  1264. TextureAtlas::Texture *t = texture_atlas.textures.getptr(E.key);
  1265. Texture *src_tex = get_texture(E.key);
  1266. glActiveTexture(GL_TEXTURE0);
  1267. glBindTexture(GL_TEXTURE_2D, src_tex->tex_id);
  1268. copy_effects->copy_to_rect(t->uv_rect);
  1269. }
  1270. }
  1271. glBindFramebuffer(GL_FRAMEBUFFER, 0);
  1272. }
  1273. /* DECAL API */
  1274. RID TextureStorage::decal_allocate() {
  1275. return RID();
  1276. }
  1277. void TextureStorage::decal_initialize(RID p_rid) {
  1278. }
  1279. void TextureStorage::decal_set_size(RID p_decal, const Vector3 &p_size) {
  1280. }
  1281. void TextureStorage::decal_set_texture(RID p_decal, RS::DecalTexture p_type, RID p_texture) {
  1282. }
  1283. void TextureStorage::decal_set_emission_energy(RID p_decal, float p_energy) {
  1284. }
  1285. void TextureStorage::decal_set_albedo_mix(RID p_decal, float p_mix) {
  1286. }
  1287. void TextureStorage::decal_set_modulate(RID p_decal, const Color &p_modulate) {
  1288. }
  1289. void TextureStorage::decal_set_cull_mask(RID p_decal, uint32_t p_layers) {
  1290. }
  1291. void TextureStorage::decal_set_distance_fade(RID p_decal, bool p_enabled, float p_begin, float p_length) {
  1292. }
  1293. void TextureStorage::decal_set_fade(RID p_decal, float p_above, float p_below) {
  1294. }
  1295. void TextureStorage::decal_set_normal_fade(RID p_decal, float p_fade) {
  1296. }
  1297. AABB TextureStorage::decal_get_aabb(RID p_decal) const {
  1298. return AABB();
  1299. }
  1300. /* RENDER TARGET API */
  1301. GLuint TextureStorage::system_fbo = 0;
  1302. void TextureStorage::_update_render_target(RenderTarget *rt) {
  1303. // do not allocate a render target with no size
  1304. if (rt->size.x <= 0 || rt->size.y <= 0) {
  1305. return;
  1306. }
  1307. // do not allocate a render target that is attached to the screen
  1308. if (rt->direct_to_screen) {
  1309. rt->fbo = system_fbo;
  1310. return;
  1311. }
  1312. Config *config = Config::get_singleton();
  1313. rt->color_internal_format = rt->is_transparent ? GL_RGBA8 : GL_RGB10_A2;
  1314. rt->color_format = GL_RGBA;
  1315. rt->color_type = rt->is_transparent ? GL_UNSIGNED_BYTE : GL_UNSIGNED_INT_2_10_10_10_REV;
  1316. rt->image_format = Image::FORMAT_RGBA8;
  1317. glDisable(GL_SCISSOR_TEST);
  1318. glColorMask(1, 1, 1, 1);
  1319. glDepthMask(GL_FALSE);
  1320. {
  1321. Texture *texture;
  1322. bool use_multiview = rt->view_count > 1 && config->multiview_supported;
  1323. GLenum texture_target = use_multiview ? GL_TEXTURE_2D_ARRAY : GL_TEXTURE_2D;
  1324. /* Front FBO */
  1325. glGenFramebuffers(1, &rt->fbo);
  1326. glBindFramebuffer(GL_FRAMEBUFFER, rt->fbo);
  1327. // color
  1328. if (rt->overridden.color.is_valid()) {
  1329. texture = get_texture(rt->overridden.color);
  1330. ERR_FAIL_COND(!texture);
  1331. rt->color = texture->tex_id;
  1332. rt->size = Size2i(texture->width, texture->height);
  1333. } else {
  1334. texture = get_texture(rt->texture);
  1335. ERR_FAIL_COND(!texture);
  1336. glGenTextures(1, &rt->color);
  1337. glBindTexture(texture_target, rt->color);
  1338. if (use_multiview) {
  1339. 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);
  1340. } else {
  1341. glTexImage2D(texture_target, 0, rt->color_internal_format, rt->size.x, rt->size.y, 0, rt->color_format, rt->color_type, nullptr);
  1342. }
  1343. glTexParameteri(texture_target, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  1344. glTexParameteri(texture_target, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  1345. glTexParameteri(texture_target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  1346. glTexParameteri(texture_target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  1347. }
  1348. #ifndef IOS_ENABLED
  1349. if (use_multiview) {
  1350. glFramebufferTextureMultiviewOVR(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, rt->color, 0, 0, rt->view_count);
  1351. } else {
  1352. #else
  1353. {
  1354. #endif
  1355. glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, rt->color, 0);
  1356. }
  1357. // depth
  1358. if (rt->overridden.depth.is_valid()) {
  1359. texture = get_texture(rt->overridden.depth);
  1360. ERR_FAIL_COND(!texture);
  1361. rt->depth = texture->tex_id;
  1362. } else {
  1363. glGenTextures(1, &rt->depth);
  1364. glBindTexture(texture_target, rt->depth);
  1365. if (use_multiview) {
  1366. glTexImage3D(texture_target, 0, GL_DEPTH_COMPONENT24, rt->size.x, rt->size.y, rt->view_count, 0, GL_DEPTH_COMPONENT, GL_UNSIGNED_INT, nullptr);
  1367. } else {
  1368. glTexImage2D(texture_target, 0, GL_DEPTH_COMPONENT24, rt->size.x, rt->size.y, 0, GL_DEPTH_COMPONENT, GL_UNSIGNED_INT, nullptr);
  1369. }
  1370. glTexParameteri(texture_target, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  1371. glTexParameteri(texture_target, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  1372. glTexParameteri(texture_target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  1373. glTexParameteri(texture_target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  1374. }
  1375. #ifndef IOS_ENABLED
  1376. if (use_multiview) {
  1377. glFramebufferTextureMultiviewOVR(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, rt->depth, 0, 0, rt->view_count);
  1378. } else {
  1379. #else
  1380. {
  1381. #endif
  1382. glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, rt->depth, 0);
  1383. }
  1384. GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
  1385. if (status != GL_FRAMEBUFFER_COMPLETE) {
  1386. glDeleteFramebuffers(1, &rt->fbo);
  1387. glDeleteTextures(1, &rt->color);
  1388. rt->fbo = 0;
  1389. rt->size.x = 0;
  1390. rt->size.y = 0;
  1391. rt->color = 0;
  1392. rt->depth = 0;
  1393. if (rt->overridden.color.is_null()) {
  1394. texture->tex_id = 0;
  1395. texture->active = false;
  1396. }
  1397. WARN_PRINT("Could not create render target, status: " + get_framebuffer_error(status));
  1398. return;
  1399. }
  1400. texture->is_render_target = true;
  1401. texture->render_target = rt;
  1402. if (rt->overridden.color.is_null()) {
  1403. texture->format = rt->image_format;
  1404. texture->real_format = rt->image_format;
  1405. texture->target = texture_target;
  1406. if (rt->view_count > 1 && config->multiview_supported) {
  1407. texture->type = Texture::TYPE_LAYERED;
  1408. texture->layers = rt->view_count;
  1409. } else {
  1410. texture->type = Texture::TYPE_2D;
  1411. texture->layers = 1;
  1412. }
  1413. texture->gl_format_cache = rt->color_format;
  1414. texture->gl_type_cache = GL_UNSIGNED_BYTE;
  1415. texture->gl_internal_format_cache = rt->color_internal_format;
  1416. texture->tex_id = rt->color;
  1417. texture->width = rt->size.x;
  1418. texture->alloc_width = rt->size.x;
  1419. texture->height = rt->size.y;
  1420. texture->alloc_height = rt->size.y;
  1421. texture->active = true;
  1422. }
  1423. }
  1424. glClearColor(0, 0, 0, 0);
  1425. glClear(GL_COLOR_BUFFER_BIT);
  1426. glBindFramebuffer(GL_FRAMEBUFFER, system_fbo);
  1427. }
  1428. void TextureStorage::_create_render_target_backbuffer(RenderTarget *rt) {
  1429. ERR_FAIL_COND_MSG(rt->backbuffer_fbo != 0, "Cannot allocate RenderTarget backbuffer: already initialized.");
  1430. ERR_FAIL_COND(rt->direct_to_screen);
  1431. // Allocate mipmap chains for full screen blur
  1432. // Limit mipmaps so smallest is 32x32 to avoid unnecessary framebuffer switches
  1433. int count = MAX(1, Image::get_image_required_mipmaps(rt->size.x, rt->size.y, Image::FORMAT_RGBA8) - 4);
  1434. if (rt->size.x > 40 && rt->size.y > 40) {
  1435. GLsizei width = rt->size.x;
  1436. GLsizei height = rt->size.y;
  1437. rt->mipmap_count = count;
  1438. glGenTextures(1, &rt->backbuffer);
  1439. glBindTexture(GL_TEXTURE_2D, rt->backbuffer);
  1440. for (int l = 0; l < count; l++) {
  1441. glTexImage2D(GL_TEXTURE_2D, l, rt->color_internal_format, width, height, 0, rt->color_format, rt->color_type, nullptr);
  1442. width = MAX(1, (width / 2));
  1443. height = MAX(1, (height / 2));
  1444. }
  1445. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_BASE_LEVEL, 0);
  1446. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, count - 1);
  1447. glGenFramebuffers(1, &rt->backbuffer_fbo);
  1448. glBindFramebuffer(GL_FRAMEBUFFER, rt->backbuffer_fbo);
  1449. glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, rt->backbuffer, 0);
  1450. GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
  1451. if (status != GL_FRAMEBUFFER_COMPLETE) {
  1452. WARN_PRINT_ONCE("Cannot allocate mipmaps for canvas screen blur. Status: " + get_framebuffer_error(status));
  1453. glBindFramebuffer(GL_FRAMEBUFFER, system_fbo);
  1454. return;
  1455. }
  1456. // Initialize all levels to opaque Magenta.
  1457. for (int j = 0; j < count; j++) {
  1458. glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, rt->backbuffer, j);
  1459. glClearColor(1.0, 0.0, 1.0, 1.0);
  1460. glClear(GL_COLOR_BUFFER_BIT);
  1461. }
  1462. glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, rt->backbuffer, 0);
  1463. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  1464. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
  1465. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  1466. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  1467. }
  1468. }
  1469. void GLES3::TextureStorage::copy_scene_to_backbuffer(RenderTarget *rt, const bool uses_screen_texture, const bool uses_depth_texture) {
  1470. if (rt->backbuffer != 0 && rt->backbuffer_depth != 0) {
  1471. return;
  1472. }
  1473. Config *config = Config::get_singleton();
  1474. bool use_multiview = rt->view_count > 1 && config->multiview_supported;
  1475. GLenum texture_target = use_multiview ? GL_TEXTURE_2D_ARRAY : GL_TEXTURE_2D;
  1476. if (rt->backbuffer_fbo == 0) {
  1477. glGenFramebuffers(1, &rt->backbuffer_fbo);
  1478. }
  1479. glBindFramebuffer(GL_FRAMEBUFFER, rt->backbuffer_fbo);
  1480. if (rt->backbuffer == 0 && uses_screen_texture) {
  1481. glGenTextures(1, &rt->backbuffer);
  1482. glBindTexture(texture_target, rt->backbuffer);
  1483. if (use_multiview) {
  1484. 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);
  1485. } else {
  1486. glTexImage2D(texture_target, 0, rt->color_internal_format, rt->size.x, rt->size.y, 0, rt->color_format, rt->color_type, nullptr);
  1487. }
  1488. glTexParameteri(texture_target, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  1489. glTexParameteri(texture_target, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  1490. glTexParameteri(texture_target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  1491. glTexParameteri(texture_target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  1492. #ifndef IOS_ENABLED
  1493. if (use_multiview) {
  1494. glFramebufferTextureMultiviewOVR(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, rt->backbuffer, 0, 0, rt->view_count);
  1495. } else {
  1496. #else
  1497. {
  1498. #endif
  1499. glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, rt->backbuffer, 0);
  1500. }
  1501. }
  1502. if (rt->backbuffer_depth == 0 && uses_depth_texture) {
  1503. glGenTextures(1, &rt->backbuffer_depth);
  1504. glBindTexture(texture_target, rt->backbuffer_depth);
  1505. if (use_multiview) {
  1506. glTexImage3D(texture_target, 0, GL_DEPTH_COMPONENT24, rt->size.x, rt->size.y, rt->view_count, 0, GL_DEPTH_COMPONENT, GL_UNSIGNED_INT, nullptr);
  1507. } else {
  1508. glTexImage2D(texture_target, 0, GL_DEPTH_COMPONENT24, rt->size.x, rt->size.y, 0, GL_DEPTH_COMPONENT, GL_UNSIGNED_INT, nullptr);
  1509. }
  1510. glTexParameteri(texture_target, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  1511. glTexParameteri(texture_target, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  1512. glTexParameteri(texture_target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  1513. glTexParameteri(texture_target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  1514. #ifndef IOS_ENABLED
  1515. if (use_multiview) {
  1516. glFramebufferTextureMultiviewOVR(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, rt->backbuffer_depth, 0, 0, rt->view_count);
  1517. } else {
  1518. #else
  1519. {
  1520. #endif
  1521. glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, rt->backbuffer_depth, 0);
  1522. }
  1523. }
  1524. }
  1525. void TextureStorage::_clear_render_target(RenderTarget *rt) {
  1526. // there is nothing to clear when DIRECT_TO_SCREEN is used
  1527. if (rt->direct_to_screen) {
  1528. return;
  1529. }
  1530. // Dispose of the cached fbo's and the allocated textures
  1531. for (KeyValue<uint32_t, RenderTarget::RTOverridden::FBOCacheEntry> &E : rt->overridden.fbo_cache) {
  1532. glDeleteTextures(E.value.allocated_textures.size(), E.value.allocated_textures.ptr());
  1533. // Don't delete the current FBO, we'll do that a couple lines down.
  1534. if (E.value.fbo != rt->fbo) {
  1535. glDeleteFramebuffers(1, &E.value.fbo);
  1536. }
  1537. }
  1538. rt->overridden.fbo_cache.clear();
  1539. if (rt->fbo) {
  1540. glDeleteFramebuffers(1, &rt->fbo);
  1541. rt->fbo = 0;
  1542. }
  1543. if (rt->overridden.color.is_null()) {
  1544. if (rt->texture.is_valid()) {
  1545. Texture *tex = get_texture(rt->texture);
  1546. tex->alloc_height = 0;
  1547. tex->alloc_width = 0;
  1548. tex->width = 0;
  1549. tex->height = 0;
  1550. tex->active = false;
  1551. tex->render_target = nullptr;
  1552. tex->is_render_target = false;
  1553. }
  1554. } else {
  1555. Texture *tex = get_texture(rt->overridden.color);
  1556. tex->render_target = nullptr;
  1557. tex->is_render_target = false;
  1558. }
  1559. if (rt->overridden.color.is_valid()) {
  1560. rt->overridden.color = RID();
  1561. } else if (rt->color) {
  1562. glDeleteTextures(1, &rt->color);
  1563. if (rt->texture.is_valid()) {
  1564. Texture *tex = get_texture(rt->texture);
  1565. tex->tex_id = 0;
  1566. }
  1567. }
  1568. rt->color = 0;
  1569. if (rt->overridden.depth.is_valid()) {
  1570. rt->overridden.depth = RID();
  1571. } else if (rt->depth) {
  1572. glDeleteTextures(1, &rt->depth);
  1573. }
  1574. rt->depth = 0;
  1575. rt->overridden.velocity = RID();
  1576. rt->overridden.is_overridden = false;
  1577. if (rt->backbuffer_fbo != 0) {
  1578. glDeleteFramebuffers(1, &rt->backbuffer_fbo);
  1579. glDeleteTextures(1, &rt->backbuffer);
  1580. rt->backbuffer = 0;
  1581. rt->backbuffer_fbo = 0;
  1582. }
  1583. if (rt->backbuffer_depth != 0) {
  1584. glDeleteTextures(1, &rt->backbuffer_depth);
  1585. rt->backbuffer_depth = 0;
  1586. }
  1587. _render_target_clear_sdf(rt);
  1588. }
  1589. RID TextureStorage::render_target_create() {
  1590. RenderTarget render_target;
  1591. render_target.used_in_frame = false;
  1592. render_target.clear_requested = false;
  1593. Texture t;
  1594. t.active = true;
  1595. t.render_target = &render_target;
  1596. t.is_render_target = true;
  1597. render_target.texture = texture_owner.make_rid(t);
  1598. _update_render_target(&render_target);
  1599. return render_target_owner.make_rid(render_target);
  1600. }
  1601. void TextureStorage::render_target_free(RID p_rid) {
  1602. RenderTarget *rt = render_target_owner.get_or_null(p_rid);
  1603. _clear_render_target(rt);
  1604. Texture *t = get_texture(rt->texture);
  1605. if (t) {
  1606. t->is_render_target = false;
  1607. if (rt->overridden.color.is_null()) {
  1608. texture_free(rt->texture);
  1609. }
  1610. //memdelete(t);
  1611. }
  1612. render_target_owner.free(p_rid);
  1613. }
  1614. void TextureStorage::render_target_set_position(RID p_render_target, int p_x, int p_y) {
  1615. RenderTarget *rt = render_target_owner.get_or_null(p_render_target);
  1616. ERR_FAIL_COND(!rt);
  1617. rt->position = Point2i(p_x, p_y);
  1618. }
  1619. Point2i TextureStorage::render_target_get_position(RID p_render_target) const {
  1620. RenderTarget *rt = render_target_owner.get_or_null(p_render_target);
  1621. ERR_FAIL_COND_V(!rt, Point2i());
  1622. return rt->position;
  1623. };
  1624. void TextureStorage::render_target_set_size(RID p_render_target, int p_width, int p_height, uint32_t p_view_count) {
  1625. RenderTarget *rt = render_target_owner.get_or_null(p_render_target);
  1626. ERR_FAIL_COND(!rt);
  1627. if (p_width == rt->size.x && p_height == rt->size.y && p_view_count == rt->view_count) {
  1628. return;
  1629. }
  1630. if (rt->overridden.color.is_valid()) {
  1631. return;
  1632. }
  1633. _clear_render_target(rt);
  1634. rt->size = Size2i(p_width, p_height);
  1635. rt->view_count = p_view_count;
  1636. _update_render_target(rt);
  1637. }
  1638. // TODO: convert to Size2i internally
  1639. Size2i TextureStorage::render_target_get_size(RID p_render_target) const {
  1640. RenderTarget *rt = render_target_owner.get_or_null(p_render_target);
  1641. ERR_FAIL_COND_V(!rt, Size2i());
  1642. return rt->size;
  1643. }
  1644. void TextureStorage::render_target_set_override(RID p_render_target, RID p_color_texture, RID p_depth_texture, RID p_velocity_texture) {
  1645. RenderTarget *rt = render_target_owner.get_or_null(p_render_target);
  1646. ERR_FAIL_COND(!rt);
  1647. ERR_FAIL_COND(rt->direct_to_screen);
  1648. rt->overridden.velocity = p_velocity_texture;
  1649. if (rt->overridden.color == p_color_texture && rt->overridden.depth == p_depth_texture) {
  1650. return;
  1651. }
  1652. if (p_color_texture.is_null() && p_depth_texture.is_null()) {
  1653. _clear_render_target(rt);
  1654. _update_render_target(rt);
  1655. return;
  1656. }
  1657. if (!rt->overridden.is_overridden) {
  1658. _clear_render_target(rt);
  1659. }
  1660. rt->overridden.color = p_color_texture;
  1661. rt->overridden.depth = p_depth_texture;
  1662. rt->overridden.is_overridden = true;
  1663. uint32_t hash_key = hash_murmur3_one_64(p_color_texture.get_id());
  1664. hash_key = hash_murmur3_one_64(p_depth_texture.get_id(), hash_key);
  1665. hash_key = hash_fmix32(hash_key);
  1666. RBMap<uint32_t, RenderTarget::RTOverridden::FBOCacheEntry>::Element *cache;
  1667. if ((cache = rt->overridden.fbo_cache.find(hash_key)) != nullptr) {
  1668. rt->fbo = cache->get().fbo;
  1669. rt->color = cache->get().color;
  1670. rt->depth = cache->get().depth;
  1671. rt->size = cache->get().size;
  1672. rt->texture = p_color_texture;
  1673. return;
  1674. }
  1675. _update_render_target(rt);
  1676. RenderTarget::RTOverridden::FBOCacheEntry new_entry;
  1677. new_entry.fbo = rt->fbo;
  1678. new_entry.color = rt->color;
  1679. new_entry.depth = rt->depth;
  1680. new_entry.size = rt->size;
  1681. // Keep track of any textures we had to allocate because they weren't overridden.
  1682. if (p_color_texture.is_null()) {
  1683. new_entry.allocated_textures.push_back(rt->color);
  1684. }
  1685. if (p_depth_texture.is_null()) {
  1686. new_entry.allocated_textures.push_back(rt->depth);
  1687. }
  1688. rt->overridden.fbo_cache.insert(hash_key, new_entry);
  1689. }
  1690. RID TextureStorage::render_target_get_override_color(RID p_render_target) const {
  1691. RenderTarget *rt = render_target_owner.get_or_null(p_render_target);
  1692. ERR_FAIL_COND_V(!rt, RID());
  1693. return rt->overridden.color;
  1694. }
  1695. RID TextureStorage::render_target_get_override_depth(RID p_render_target) const {
  1696. RenderTarget *rt = render_target_owner.get_or_null(p_render_target);
  1697. ERR_FAIL_COND_V(!rt, RID());
  1698. return rt->overridden.depth;
  1699. }
  1700. RID TextureStorage::render_target_get_override_velocity(RID p_render_target) const {
  1701. RenderTarget *rt = render_target_owner.get_or_null(p_render_target);
  1702. ERR_FAIL_COND_V(!rt, RID());
  1703. return rt->overridden.velocity;
  1704. }
  1705. RID TextureStorage::render_target_get_texture(RID p_render_target) {
  1706. RenderTarget *rt = render_target_owner.get_or_null(p_render_target);
  1707. ERR_FAIL_COND_V(!rt, RID());
  1708. if (rt->overridden.color.is_valid()) {
  1709. return rt->overridden.color;
  1710. }
  1711. return rt->texture;
  1712. }
  1713. void TextureStorage::render_target_set_transparent(RID p_render_target, bool p_transparent) {
  1714. RenderTarget *rt = render_target_owner.get_or_null(p_render_target);
  1715. ERR_FAIL_COND(!rt);
  1716. rt->is_transparent = p_transparent;
  1717. if (rt->overridden.color.is_null()) {
  1718. _clear_render_target(rt);
  1719. _update_render_target(rt);
  1720. }
  1721. }
  1722. bool TextureStorage::render_target_get_transparent(RID p_render_target) const {
  1723. RenderTarget *rt = render_target_owner.get_or_null(p_render_target);
  1724. ERR_FAIL_COND_V(!rt, false);
  1725. return rt->is_transparent;
  1726. }
  1727. void TextureStorage::render_target_set_direct_to_screen(RID p_render_target, bool p_direct_to_screen) {
  1728. RenderTarget *rt = render_target_owner.get_or_null(p_render_target);
  1729. ERR_FAIL_COND(!rt);
  1730. if (p_direct_to_screen == rt->direct_to_screen) {
  1731. return;
  1732. }
  1733. // When setting DIRECT_TO_SCREEN, you need to clear before the value is set, but allocate after as
  1734. // those functions change how they operate depending on the value of DIRECT_TO_SCREEN
  1735. _clear_render_target(rt);
  1736. rt->direct_to_screen = p_direct_to_screen;
  1737. if (rt->direct_to_screen) {
  1738. rt->overridden.color = RID();
  1739. rt->overridden.depth = RID();
  1740. rt->overridden.velocity = RID();
  1741. }
  1742. _update_render_target(rt);
  1743. }
  1744. bool TextureStorage::render_target_get_direct_to_screen(RID p_render_target) const {
  1745. RenderTarget *rt = render_target_owner.get_or_null(p_render_target);
  1746. ERR_FAIL_COND_V(!rt, false);
  1747. return rt->direct_to_screen;
  1748. }
  1749. bool TextureStorage::render_target_was_used(RID p_render_target) const {
  1750. RenderTarget *rt = render_target_owner.get_or_null(p_render_target);
  1751. ERR_FAIL_COND_V(!rt, false);
  1752. return rt->used_in_frame;
  1753. }
  1754. void TextureStorage::render_target_clear_used(RID p_render_target) {
  1755. RenderTarget *rt = render_target_owner.get_or_null(p_render_target);
  1756. ERR_FAIL_COND(!rt);
  1757. rt->used_in_frame = false;
  1758. }
  1759. void TextureStorage::render_target_set_msaa(RID p_render_target, RS::ViewportMSAA p_msaa) {
  1760. RenderTarget *rt = render_target_owner.get_or_null(p_render_target);
  1761. ERR_FAIL_COND(!rt);
  1762. if (p_msaa == rt->msaa) {
  1763. return;
  1764. }
  1765. WARN_PRINT("2D MSAA is not yet supported for GLES3.");
  1766. _clear_render_target(rt);
  1767. rt->msaa = p_msaa;
  1768. _update_render_target(rt);
  1769. }
  1770. RS::ViewportMSAA TextureStorage::render_target_get_msaa(RID p_render_target) const {
  1771. RenderTarget *rt = render_target_owner.get_or_null(p_render_target);
  1772. ERR_FAIL_COND_V(!rt, RS::VIEWPORT_MSAA_DISABLED);
  1773. return rt->msaa;
  1774. }
  1775. void TextureStorage::render_target_request_clear(RID p_render_target, const Color &p_clear_color) {
  1776. RenderTarget *rt = render_target_owner.get_or_null(p_render_target);
  1777. ERR_FAIL_COND(!rt);
  1778. rt->clear_requested = true;
  1779. rt->clear_color = p_clear_color;
  1780. }
  1781. bool TextureStorage::render_target_is_clear_requested(RID p_render_target) {
  1782. RenderTarget *rt = render_target_owner.get_or_null(p_render_target);
  1783. ERR_FAIL_COND_V(!rt, false);
  1784. return rt->clear_requested;
  1785. }
  1786. Color TextureStorage::render_target_get_clear_request_color(RID p_render_target) {
  1787. RenderTarget *rt = render_target_owner.get_or_null(p_render_target);
  1788. ERR_FAIL_COND_V(!rt, Color());
  1789. return rt->clear_color;
  1790. }
  1791. void TextureStorage::render_target_disable_clear_request(RID p_render_target) {
  1792. RenderTarget *rt = render_target_owner.get_or_null(p_render_target);
  1793. ERR_FAIL_COND(!rt);
  1794. rt->clear_requested = false;
  1795. }
  1796. void TextureStorage::render_target_do_clear_request(RID p_render_target) {
  1797. RenderTarget *rt = render_target_owner.get_or_null(p_render_target);
  1798. ERR_FAIL_COND(!rt);
  1799. if (!rt->clear_requested) {
  1800. return;
  1801. }
  1802. glBindFramebuffer(GL_FRAMEBUFFER, rt->fbo);
  1803. glClearBufferfv(GL_COLOR, 0, rt->clear_color.components);
  1804. rt->clear_requested = false;
  1805. glBindFramebuffer(GL_FRAMEBUFFER, system_fbo);
  1806. }
  1807. void TextureStorage::render_target_set_sdf_size_and_scale(RID p_render_target, RS::ViewportSDFOversize p_size, RS::ViewportSDFScale p_scale) {
  1808. RenderTarget *rt = render_target_owner.get_or_null(p_render_target);
  1809. ERR_FAIL_COND(!rt);
  1810. if (rt->sdf_oversize == p_size && rt->sdf_scale == p_scale) {
  1811. return;
  1812. }
  1813. rt->sdf_oversize = p_size;
  1814. rt->sdf_scale = p_scale;
  1815. _render_target_clear_sdf(rt);
  1816. }
  1817. Rect2i TextureStorage::_render_target_get_sdf_rect(const RenderTarget *rt) const {
  1818. Size2i margin;
  1819. int scale;
  1820. switch (rt->sdf_oversize) {
  1821. case RS::VIEWPORT_SDF_OVERSIZE_100_PERCENT: {
  1822. scale = 100;
  1823. } break;
  1824. case RS::VIEWPORT_SDF_OVERSIZE_120_PERCENT: {
  1825. scale = 120;
  1826. } break;
  1827. case RS::VIEWPORT_SDF_OVERSIZE_150_PERCENT: {
  1828. scale = 150;
  1829. } break;
  1830. case RS::VIEWPORT_SDF_OVERSIZE_200_PERCENT: {
  1831. scale = 200;
  1832. } break;
  1833. default: {
  1834. }
  1835. }
  1836. margin = (rt->size * scale / 100) - rt->size;
  1837. Rect2i r(Vector2i(), rt->size);
  1838. r.position -= margin;
  1839. r.size += margin * 2;
  1840. return r;
  1841. }
  1842. Rect2i TextureStorage::render_target_get_sdf_rect(RID p_render_target) const {
  1843. const RenderTarget *rt = render_target_owner.get_or_null(p_render_target);
  1844. ERR_FAIL_COND_V(!rt, Rect2i());
  1845. return _render_target_get_sdf_rect(rt);
  1846. }
  1847. void TextureStorage::render_target_mark_sdf_enabled(RID p_render_target, bool p_enabled) {
  1848. RenderTarget *rt = render_target_owner.get_or_null(p_render_target);
  1849. ERR_FAIL_COND(!rt);
  1850. rt->sdf_enabled = p_enabled;
  1851. }
  1852. bool TextureStorage::render_target_is_sdf_enabled(RID p_render_target) const {
  1853. const RenderTarget *rt = render_target_owner.get_or_null(p_render_target);
  1854. ERR_FAIL_COND_V(!rt, false);
  1855. return rt->sdf_enabled;
  1856. }
  1857. GLuint TextureStorage::render_target_get_sdf_texture(RID p_render_target) {
  1858. RenderTarget *rt = render_target_owner.get_or_null(p_render_target);
  1859. ERR_FAIL_COND_V(!rt, 0);
  1860. if (rt->sdf_texture_read == 0) {
  1861. Texture *texture = texture_owner.get_or_null(default_gl_textures[DEFAULT_GL_TEXTURE_BLACK]);
  1862. return texture->tex_id;
  1863. }
  1864. return rt->sdf_texture_read;
  1865. }
  1866. void TextureStorage::_render_target_allocate_sdf(RenderTarget *rt) {
  1867. ERR_FAIL_COND(rt->sdf_texture_write_fb != 0);
  1868. Size2i size = _render_target_get_sdf_rect(rt).size;
  1869. glGenTextures(1, &rt->sdf_texture_write);
  1870. glActiveTexture(GL_TEXTURE0);
  1871. glBindTexture(GL_TEXTURE_2D, rt->sdf_texture_write);
  1872. glTexImage2D(GL_TEXTURE_2D, 0, GL_R8, size.width, size.height, 0, GL_RED, GL_UNSIGNED_BYTE, nullptr);
  1873. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  1874. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  1875. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_BASE_LEVEL, 0);
  1876. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, 1);
  1877. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  1878. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  1879. glGenFramebuffers(1, &rt->sdf_texture_write_fb);
  1880. glBindFramebuffer(GL_FRAMEBUFFER, rt->sdf_texture_write_fb);
  1881. glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, rt->sdf_texture_write, 0);
  1882. int scale;
  1883. switch (rt->sdf_scale) {
  1884. case RS::VIEWPORT_SDF_SCALE_100_PERCENT: {
  1885. scale = 100;
  1886. } break;
  1887. case RS::VIEWPORT_SDF_SCALE_50_PERCENT: {
  1888. scale = 50;
  1889. } break;
  1890. case RS::VIEWPORT_SDF_SCALE_25_PERCENT: {
  1891. scale = 25;
  1892. } break;
  1893. default: {
  1894. scale = 100;
  1895. } break;
  1896. }
  1897. rt->process_size = size * scale / 100;
  1898. rt->process_size.x = MAX(rt->process_size.x, 1);
  1899. rt->process_size.y = MAX(rt->process_size.y, 1);
  1900. glGenTextures(2, rt->sdf_texture_process);
  1901. glBindTexture(GL_TEXTURE_2D, rt->sdf_texture_process[0]);
  1902. glTexImage2D(GL_TEXTURE_2D, 0, GL_RG16I, rt->process_size.width, rt->process_size.height, 0, GL_RG_INTEGER, GL_SHORT, nullptr);
  1903. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  1904. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  1905. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_BASE_LEVEL, 0);
  1906. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, 1);
  1907. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  1908. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  1909. glBindTexture(GL_TEXTURE_2D, rt->sdf_texture_process[1]);
  1910. glTexImage2D(GL_TEXTURE_2D, 0, GL_RG16I, rt->process_size.width, rt->process_size.height, 0, GL_RG_INTEGER, GL_SHORT, nullptr);
  1911. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  1912. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  1913. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_BASE_LEVEL, 0);
  1914. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, 1);
  1915. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  1916. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  1917. glGenTextures(1, &rt->sdf_texture_read);
  1918. glBindTexture(GL_TEXTURE_2D, rt->sdf_texture_read);
  1919. glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, rt->process_size.width, rt->process_size.height, 0, GL_RGBA, GL_UNSIGNED_BYTE, nullptr);
  1920. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  1921. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  1922. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_BASE_LEVEL, 0);
  1923. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, 1);
  1924. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  1925. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  1926. }
  1927. void TextureStorage::_render_target_clear_sdf(RenderTarget *rt) {
  1928. if (rt->sdf_texture_write_fb != 0) {
  1929. glDeleteTextures(1, &rt->sdf_texture_read);
  1930. glDeleteTextures(1, &rt->sdf_texture_write);
  1931. glDeleteTextures(2, rt->sdf_texture_process);
  1932. glDeleteFramebuffers(1, &rt->sdf_texture_write_fb);
  1933. rt->sdf_texture_read = 0;
  1934. rt->sdf_texture_write = 0;
  1935. rt->sdf_texture_process[0] = 0;
  1936. rt->sdf_texture_process[1] = 0;
  1937. rt->sdf_texture_write_fb = 0;
  1938. }
  1939. }
  1940. GLuint TextureStorage::render_target_get_sdf_framebuffer(RID p_render_target) {
  1941. RenderTarget *rt = render_target_owner.get_or_null(p_render_target);
  1942. ERR_FAIL_COND_V(!rt, 0);
  1943. if (rt->sdf_texture_write_fb == 0) {
  1944. _render_target_allocate_sdf(rt);
  1945. }
  1946. return rt->sdf_texture_write_fb;
  1947. }
  1948. void TextureStorage::render_target_sdf_process(RID p_render_target) {
  1949. CopyEffects *copy_effects = CopyEffects::get_singleton();
  1950. RenderTarget *rt = render_target_owner.get_or_null(p_render_target);
  1951. ERR_FAIL_COND(!rt);
  1952. ERR_FAIL_COND(rt->sdf_texture_write_fb == 0);
  1953. Rect2i r = _render_target_get_sdf_rect(rt);
  1954. Size2i size = r.size;
  1955. int32_t shift = 0;
  1956. bool shrink = false;
  1957. switch (rt->sdf_scale) {
  1958. case RS::VIEWPORT_SDF_SCALE_50_PERCENT: {
  1959. size[0] >>= 1;
  1960. size[1] >>= 1;
  1961. shift = 1;
  1962. shrink = true;
  1963. } break;
  1964. case RS::VIEWPORT_SDF_SCALE_25_PERCENT: {
  1965. size[0] >>= 2;
  1966. size[1] >>= 2;
  1967. shift = 2;
  1968. shrink = true;
  1969. } break;
  1970. default: {
  1971. };
  1972. }
  1973. GLuint temp_fb;
  1974. glGenFramebuffers(1, &temp_fb);
  1975. glBindFramebuffer(GL_FRAMEBUFFER, temp_fb);
  1976. // Load
  1977. CanvasSdfShaderGLES3::ShaderVariant variant = shrink ? CanvasSdfShaderGLES3::MODE_LOAD_SHRINK : CanvasSdfShaderGLES3::MODE_LOAD;
  1978. bool success = sdf_shader.shader.version_bind_shader(sdf_shader.shader_version, variant);
  1979. if (!success) {
  1980. return;
  1981. }
  1982. sdf_shader.shader.version_set_uniform(CanvasSdfShaderGLES3::BASE_SIZE, r.size, sdf_shader.shader_version, variant);
  1983. sdf_shader.shader.version_set_uniform(CanvasSdfShaderGLES3::SIZE, size, sdf_shader.shader_version, variant);
  1984. sdf_shader.shader.version_set_uniform(CanvasSdfShaderGLES3::STRIDE, 0, sdf_shader.shader_version, variant);
  1985. sdf_shader.shader.version_set_uniform(CanvasSdfShaderGLES3::SHIFT, shift, sdf_shader.shader_version, variant);
  1986. glActiveTexture(GL_TEXTURE0);
  1987. glBindTexture(GL_TEXTURE_2D, rt->sdf_texture_write);
  1988. glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, rt->sdf_texture_process[0], 0);
  1989. glViewport(0, 0, size.width, size.height);
  1990. glEnable(GL_SCISSOR_TEST);
  1991. glScissor(0, 0, size.width, size.height);
  1992. copy_effects->draw_screen_triangle();
  1993. // Process
  1994. int stride = nearest_power_of_2_templated(MAX(size.width, size.height) / 2);
  1995. variant = CanvasSdfShaderGLES3::MODE_PROCESS;
  1996. success = sdf_shader.shader.version_bind_shader(sdf_shader.shader_version, variant);
  1997. if (!success) {
  1998. return;
  1999. }
  2000. sdf_shader.shader.version_set_uniform(CanvasSdfShaderGLES3::BASE_SIZE, r.size, sdf_shader.shader_version, variant);
  2001. sdf_shader.shader.version_set_uniform(CanvasSdfShaderGLES3::SIZE, size, sdf_shader.shader_version, variant);
  2002. sdf_shader.shader.version_set_uniform(CanvasSdfShaderGLES3::STRIDE, stride, sdf_shader.shader_version, variant);
  2003. sdf_shader.shader.version_set_uniform(CanvasSdfShaderGLES3::SHIFT, shift, sdf_shader.shader_version, variant);
  2004. bool swap = false;
  2005. //jumpflood
  2006. while (stride > 0) {
  2007. glBindTexture(GL_TEXTURE_2D, 0);
  2008. glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, rt->sdf_texture_process[swap ? 0 : 1], 0);
  2009. glBindTexture(GL_TEXTURE_2D, rt->sdf_texture_process[swap ? 1 : 0]);
  2010. sdf_shader.shader.version_set_uniform(CanvasSdfShaderGLES3::STRIDE, stride, sdf_shader.shader_version, variant);
  2011. copy_effects->draw_screen_triangle();
  2012. stride /= 2;
  2013. swap = !swap;
  2014. }
  2015. // Store
  2016. variant = shrink ? CanvasSdfShaderGLES3::MODE_STORE_SHRINK : CanvasSdfShaderGLES3::MODE_STORE;
  2017. success = sdf_shader.shader.version_bind_shader(sdf_shader.shader_version, variant);
  2018. if (!success) {
  2019. return;
  2020. }
  2021. sdf_shader.shader.version_set_uniform(CanvasSdfShaderGLES3::BASE_SIZE, r.size, sdf_shader.shader_version, variant);
  2022. sdf_shader.shader.version_set_uniform(CanvasSdfShaderGLES3::SIZE, size, sdf_shader.shader_version, variant);
  2023. sdf_shader.shader.version_set_uniform(CanvasSdfShaderGLES3::STRIDE, stride, sdf_shader.shader_version, variant);
  2024. sdf_shader.shader.version_set_uniform(CanvasSdfShaderGLES3::SHIFT, shift, sdf_shader.shader_version, variant);
  2025. glBindTexture(GL_TEXTURE_2D, 0);
  2026. glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, rt->sdf_texture_read, 0);
  2027. glBindTexture(GL_TEXTURE_2D, rt->sdf_texture_process[swap ? 1 : 0]);
  2028. copy_effects->draw_screen_triangle();
  2029. glBindTexture(GL_TEXTURE_2D, 0);
  2030. glBindFramebuffer(GL_FRAMEBUFFER, system_fbo);
  2031. glDeleteFramebuffers(1, &temp_fb);
  2032. glDisable(GL_SCISSOR_TEST);
  2033. }
  2034. void TextureStorage::render_target_copy_to_back_buffer(RID p_render_target, const Rect2i &p_region, bool p_gen_mipmaps) {
  2035. RenderTarget *rt = render_target_owner.get_or_null(p_render_target);
  2036. ERR_FAIL_COND(!rt);
  2037. ERR_FAIL_COND(rt->direct_to_screen);
  2038. if (rt->backbuffer_fbo == 0) {
  2039. _create_render_target_backbuffer(rt);
  2040. }
  2041. Rect2i region;
  2042. if (p_region == Rect2i()) {
  2043. region.size = rt->size;
  2044. } else {
  2045. region = Rect2i(Size2i(), rt->size).intersection(p_region);
  2046. if (region.size == Size2i()) {
  2047. return; //nothing to do
  2048. }
  2049. }
  2050. glDisable(GL_BLEND);
  2051. //single texture copy for backbuffer
  2052. glBindFramebuffer(GL_FRAMEBUFFER, rt->backbuffer_fbo);
  2053. glActiveTexture(GL_TEXTURE0);
  2054. glBindTexture(GL_TEXTURE_2D, rt->color);
  2055. GLES3::CopyEffects::get_singleton()->copy_screen();
  2056. if (p_gen_mipmaps) {
  2057. GLES3::CopyEffects::get_singleton()->bilinear_blur(rt->backbuffer, rt->mipmap_count, region);
  2058. glBindFramebuffer(GL_FRAMEBUFFER, rt->backbuffer_fbo);
  2059. }
  2060. glEnable(GL_BLEND); // 2D almost always uses blend.
  2061. }
  2062. void TextureStorage::render_target_clear_back_buffer(RID p_render_target, const Rect2i &p_region, const Color &p_color) {
  2063. RenderTarget *rt = render_target_owner.get_or_null(p_render_target);
  2064. ERR_FAIL_COND(!rt);
  2065. ERR_FAIL_COND(rt->direct_to_screen);
  2066. if (rt->backbuffer_fbo == 0) {
  2067. _create_render_target_backbuffer(rt);
  2068. }
  2069. Rect2i region;
  2070. if (p_region == Rect2i()) {
  2071. // Just do a full screen clear;
  2072. glBindFramebuffer(GL_FRAMEBUFFER, rt->backbuffer_fbo);
  2073. glClearColor(p_color.r, p_color.g, p_color.b, p_color.a);
  2074. glClear(GL_COLOR_BUFFER_BIT);
  2075. } else {
  2076. region = Rect2i(Size2i(), rt->size).intersection(p_region);
  2077. if (region.size == Size2i()) {
  2078. return; //nothing to do
  2079. }
  2080. glBindFramebuffer(GL_FRAMEBUFFER, rt->backbuffer_fbo);
  2081. GLES3::CopyEffects::get_singleton()->set_color(p_color, region);
  2082. }
  2083. }
  2084. void TextureStorage::render_target_gen_back_buffer_mipmaps(RID p_render_target, const Rect2i &p_region) {
  2085. RenderTarget *rt = render_target_owner.get_or_null(p_render_target);
  2086. ERR_FAIL_COND(!rt);
  2087. if (rt->backbuffer_fbo == 0) {
  2088. _create_render_target_backbuffer(rt);
  2089. }
  2090. Rect2i region;
  2091. if (p_region == Rect2i()) {
  2092. region.size = rt->size;
  2093. } else {
  2094. region = Rect2i(Size2i(), rt->size).intersection(p_region);
  2095. if (region.size == Size2i()) {
  2096. return; //nothing to do
  2097. }
  2098. }
  2099. GLES3::CopyEffects::get_singleton()->bilinear_blur(rt->backbuffer, rt->mipmap_count, region);
  2100. glBindFramebuffer(GL_FRAMEBUFFER, rt->backbuffer_fbo);
  2101. }
  2102. #endif // GLES3_ENABLED