texture_storage.cpp 85 KB

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