texture_storage.cpp 72 KB

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