texture_storage.cpp 79 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552
  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 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