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