1/**************************************************************************/
2/* texture_storage.h */
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#ifndef TEXTURE_STORAGE_GLES3_H
32#define TEXTURE_STORAGE_GLES3_H
33
34#ifdef GLES3_ENABLED
35
36#include "config.h"
37#include "core/os/os.h"
38#include "core/templates/rid_owner.h"
39#include "servers/rendering/renderer_compositor.h"
40#include "servers/rendering/storage/texture_storage.h"
41
42#include "drivers/gles3/shaders/canvas_sdf.glsl.gen.h"
43
44// This must come first to avoid windows.h mess
45#include "platform_config.h"
46#ifndef OPENGL_INCLUDE_H
47#include <GLES3/gl3.h>
48#else
49#include OPENGL_INCLUDE_H
50#endif
51
52namespace GLES3 {
53
54#define _GL_TEXTURE_MAX_ANISOTROPY_EXT 0x84FE
55#define _GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT 0x84FF
56
57#define _EXT_COMPRESSED_RGBA_S3TC_DXT1_EXT 0x83F1
58#define _EXT_COMPRESSED_RGBA_S3TC_DXT3_EXT 0x83F2
59#define _EXT_COMPRESSED_RGBA_S3TC_DXT5_EXT 0x83F3
60
61#define _EXT_COMPRESSED_RED_RGTC1_EXT 0x8DBB
62#define _EXT_COMPRESSED_RED_RGTC1 0x8DBB
63#define _EXT_COMPRESSED_SIGNED_RED_RGTC1 0x8DBC
64#define _EXT_COMPRESSED_RG_RGTC2 0x8DBD
65#define _EXT_COMPRESSED_SIGNED_RG_RGTC2 0x8DBE
66#define _EXT_COMPRESSED_SIGNED_RED_RGTC1_EXT 0x8DBC
67#define _EXT_COMPRESSED_RED_GREEN_RGTC2_EXT 0x8DBD
68#define _EXT_COMPRESSED_SIGNED_RED_GREEN_RGTC2_EXT 0x8DBE
69#define _EXT_ETC1_RGB8_OES 0x8D64
70
71#define _EXT_COMPRESSED_RGBA_BPTC_UNORM 0x8E8C
72#define _EXT_COMPRESSED_SRGB_ALPHA_BPTC_UNORM 0x8E8D
73#define _EXT_COMPRESSED_RGB_BPTC_SIGNED_FLOAT 0x8E8E
74#define _EXT_COMPRESSED_RGB_BPTC_UNSIGNED_FLOAT 0x8E8F
75
76#define _EXT_COMPRESSED_R11_EAC 0x9270
77#define _EXT_COMPRESSED_SIGNED_R11_EAC 0x9271
78#define _EXT_COMPRESSED_RG11_EAC 0x9272
79#define _EXT_COMPRESSED_SIGNED_RG11_EAC 0x9273
80#define _EXT_COMPRESSED_RGB8_ETC2 0x9274
81#define _EXT_COMPRESSED_SRGB8_ETC2 0x9275
82#define _EXT_COMPRESSED_RGB8_PUNCHTHROUGH_ALPHA1_ETC2 0x9276
83#define _EXT_COMPRESSED_SRGB8_PUNCHTHROUGH_ALPHA1_ETC2 0x9277
84#define _EXT_COMPRESSED_RGBA8_ETC2_EAC 0x9278
85#define _EXT_COMPRESSED_SRGB8_ALPHA8_ETC2_EAC 0x9279
86
87#define _EXT_COMPRESSED_RGBA_ASTC_4x4_KHR 0x93B0
88#define _EXT_COMPRESSED_RGBA_ASTC_5x4_KHR 0x93B1
89#define _EXT_COMPRESSED_RGBA_ASTC_5x5_KHR 0x93B2
90#define _EXT_COMPRESSED_RGBA_ASTC_6x5_KHR 0x93B3
91#define _EXT_COMPRESSED_RGBA_ASTC_6x6_KHR 0x93B4
92#define _EXT_COMPRESSED_RGBA_ASTC_8x5_KHR 0x93B5
93#define _EXT_COMPRESSED_RGBA_ASTC_8x6_KHR 0x93B6
94#define _EXT_COMPRESSED_RGBA_ASTC_8x8_KHR 0x93B7
95#define _EXT_COMPRESSED_RGBA_ASTC_10x5_KHR 0x93B8
96#define _EXT_COMPRESSED_RGBA_ASTC_10x6_KHR 0x93B9
97#define _EXT_COMPRESSED_RGBA_ASTC_10x8_KHR 0x93BA
98#define _EXT_COMPRESSED_RGBA_ASTC_10x10_KHR 0x93BB
99#define _EXT_COMPRESSED_RGBA_ASTC_12x10_KHR 0x93BC
100#define _EXT_COMPRESSED_RGBA_ASTC_12x12_KHR 0x93BD
101
102#define _EXT_COMPRESSED_SRGB8_ALPHA8_ASTC_4x4_KHR 0x93D0
103#define _EXT_COMPRESSED_SRGB8_ALPHA8_ASTC_5x4_KHR 0x93D1
104#define _EXT_COMPRESSED_SRGB8_ALPHA8_ASTC_5x5_KHR 0x93D2
105#define _EXT_COMPRESSED_SRGB8_ALPHA8_ASTC_6x5_KHR 0x93D3
106#define _EXT_COMPRESSED_SRGB8_ALPHA8_ASTC_6x6_KHR 0x93D4
107#define _EXT_COMPRESSED_SRGB8_ALPHA8_ASTC_8x5_KHR 0x93D5
108#define _EXT_COMPRESSED_SRGB8_ALPHA8_ASTC_8x6_KHR 0x93D6
109#define _EXT_COMPRESSED_SRGB8_ALPHA8_ASTC_8x8_KHR 0x93D7
110#define _EXT_COMPRESSED_SRGB8_ALPHA8_ASTC_10x5_KHR 0x93D8
111#define _EXT_COMPRESSED_SRGB8_ALPHA8_ASTC_10x6_KHR 0x93D9
112#define _EXT_COMPRESSED_SRGB8_ALPHA8_ASTC_10x8_KHR 0x93DA
113#define _EXT_COMPRESSED_SRGB8_ALPHA8_ASTC_10x10_KHR 0x93DB
114#define _EXT_COMPRESSED_SRGB8_ALPHA8_ASTC_12x10_KHR 0x93DC
115#define _EXT_COMPRESSED_SRGB8_ALPHA8_ASTC_12x12_KHR 0x93DD
116
117#define _GL_TEXTURE_EXTERNAL_OES 0x8D65
118
119#define _EXT_TEXTURE_CUBE_MAP_SEAMLESS 0x884F
120
121#ifndef GLES_OVER_GL
122#define glClearDepth glClearDepthf
123#endif //!GLES_OVER_GL
124
125enum DefaultGLTexture {
126 DEFAULT_GL_TEXTURE_WHITE,
127 DEFAULT_GL_TEXTURE_BLACK,
128 DEFAULT_GL_TEXTURE_TRANSPARENT,
129 DEFAULT_GL_TEXTURE_NORMAL,
130 DEFAULT_GL_TEXTURE_ANISO,
131 DEFAULT_GL_TEXTURE_DEPTH,
132 DEFAULT_GL_TEXTURE_CUBEMAP_BLACK,
133 //DEFAULT_GL_TEXTURE_CUBEMAP_ARRAY_BLACK, // Cubemap Arrays not supported in GL 3.3 or GL ES 3.0
134 DEFAULT_GL_TEXTURE_CUBEMAP_WHITE,
135 DEFAULT_GL_TEXTURE_3D_WHITE,
136 DEFAULT_GL_TEXTURE_3D_BLACK,
137 DEFAULT_GL_TEXTURE_2D_ARRAY_WHITE,
138 DEFAULT_GL_TEXTURE_2D_UINT,
139 DEFAULT_GL_TEXTURE_MAX
140};
141
142struct CanvasTexture {
143 RID diffuse;
144 RID normal_map;
145 RID specular;
146 Color specular_color = Color(1, 1, 1, 1);
147 float shininess = 1.0;
148
149 RS::CanvasItemTextureFilter texture_filter = RS::CANVAS_ITEM_TEXTURE_FILTER_DEFAULT;
150 RS::CanvasItemTextureRepeat texture_repeat = RS::CANVAS_ITEM_TEXTURE_REPEAT_DEFAULT;
151};
152
153struct RenderTarget;
154
155struct Texture {
156 RID self;
157
158 bool is_proxy = false;
159 bool is_external = false;
160 bool is_render_target = false;
161
162 RID proxy_to;
163 Vector<RID> proxies;
164
165 String path;
166 int width = 0;
167 int height = 0;
168 int depth = 0;
169 int mipmaps = 1;
170 int layers = 1;
171 int alloc_width = 0;
172 int alloc_height = 0;
173 Image::Format format = Image::FORMAT_R8;
174 Image::Format real_format = Image::FORMAT_R8;
175
176 enum Type {
177 TYPE_2D,
178 TYPE_LAYERED,
179 TYPE_3D
180 };
181
182 Type type;
183 RS::TextureLayeredType layered_type = RS::TEXTURE_LAYERED_2D_ARRAY;
184
185 GLenum target = GL_TEXTURE_2D;
186 GLenum gl_format_cache = 0;
187 GLenum gl_internal_format_cache = 0;
188 GLenum gl_type_cache = 0;
189
190 int total_data_size = 0;
191
192 bool compressed = false;
193
194 bool resize_to_po2 = false;
195
196 bool active = false;
197 GLuint tex_id = 0;
198
199 uint16_t stored_cube_sides = 0;
200
201 RenderTarget *render_target = nullptr;
202
203 Ref<Image> image_cache_2d;
204
205 bool redraw_if_visible = false;
206
207 RS::TextureDetectCallback detect_3d_callback = nullptr;
208 void *detect_3d_callback_ud = nullptr;
209
210 RS::TextureDetectCallback detect_normal_callback = nullptr;
211 void *detect_normal_callback_ud = nullptr;
212
213 RS::TextureDetectRoughnessCallback detect_roughness_callback = nullptr;
214 void *detect_roughness_callback_ud = nullptr;
215
216 CanvasTexture *canvas_texture = nullptr;
217
218 void copy_from(const Texture &o) {
219 proxy_to = o.proxy_to;
220 is_proxy = o.is_proxy;
221 is_external = o.is_external;
222 width = o.width;
223 height = o.height;
224 alloc_width = o.alloc_width;
225 alloc_height = o.alloc_height;
226 format = o.format;
227 type = o.type;
228 layered_type = o.layered_type;
229 target = o.target;
230 total_data_size = o.total_data_size;
231 compressed = o.compressed;
232 mipmaps = o.mipmaps;
233 resize_to_po2 = o.resize_to_po2;
234 active = o.active;
235 tex_id = o.tex_id;
236 stored_cube_sides = o.stored_cube_sides;
237 render_target = o.render_target;
238 is_render_target = o.is_render_target;
239 redraw_if_visible = o.redraw_if_visible;
240 detect_3d_callback = o.detect_3d_callback;
241 detect_3d_callback_ud = o.detect_3d_callback_ud;
242 detect_normal_callback = o.detect_normal_callback;
243 detect_normal_callback_ud = o.detect_normal_callback_ud;
244 detect_roughness_callback = o.detect_roughness_callback;
245 detect_roughness_callback_ud = o.detect_roughness_callback_ud;
246 }
247
248 // texture state
249 void gl_set_filter(RS::CanvasItemTextureFilter p_filter) {
250 if (p_filter == state_filter) {
251 return;
252 }
253 Config *config = Config::get_singleton();
254 state_filter = p_filter;
255 GLenum pmin = GL_NEAREST;
256 GLenum pmag = GL_NEAREST;
257 GLint max_lod = 0;
258 GLfloat anisotropy = 1.0f;
259 switch (state_filter) {
260 case RS::CANVAS_ITEM_TEXTURE_FILTER_NEAREST: {
261 pmin = GL_NEAREST;
262 pmag = GL_NEAREST;
263 max_lod = 0;
264 } break;
265 case RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR: {
266 pmin = GL_LINEAR;
267 pmag = GL_LINEAR;
268 max_lod = 0;
269 } break;
270 case RS::CANVAS_ITEM_TEXTURE_FILTER_NEAREST_WITH_MIPMAPS_ANISOTROPIC: {
271 anisotropy = config->anisotropic_level;
272 };
273 [[fallthrough]];
274 case RS::CANVAS_ITEM_TEXTURE_FILTER_NEAREST_WITH_MIPMAPS: {
275 pmag = GL_NEAREST;
276 if (mipmaps <= 1) {
277 pmin = GL_NEAREST;
278 max_lod = 0;
279 } else if (config->use_nearest_mip_filter) {
280 pmin = GL_NEAREST_MIPMAP_NEAREST;
281 max_lod = 1000;
282 } else {
283 pmin = GL_NEAREST_MIPMAP_LINEAR;
284 max_lod = 1000;
285 }
286 } break;
287 case RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR_WITH_MIPMAPS_ANISOTROPIC: {
288 anisotropy = config->anisotropic_level;
289 };
290 [[fallthrough]];
291 case RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR_WITH_MIPMAPS: {
292 pmag = GL_LINEAR;
293 if (mipmaps <= 1) {
294 pmin = GL_LINEAR;
295 max_lod = 0;
296 } else if (config->use_nearest_mip_filter) {
297 pmin = GL_LINEAR_MIPMAP_NEAREST;
298 max_lod = 1000;
299 } else {
300 pmin = GL_LINEAR_MIPMAP_LINEAR;
301 max_lod = 1000;
302 }
303 } break;
304 default: {
305 return;
306 } break;
307 }
308 glTexParameteri(target, GL_TEXTURE_MIN_FILTER, pmin);
309 glTexParameteri(target, GL_TEXTURE_MAG_FILTER, pmag);
310 glTexParameteri(target, GL_TEXTURE_BASE_LEVEL, 0);
311 glTexParameteri(target, GL_TEXTURE_MAX_LEVEL, max_lod);
312 if (config->support_anisotropic_filter) {
313 glTexParameterf(target, _GL_TEXTURE_MAX_ANISOTROPY_EXT, anisotropy);
314 }
315 }
316 void gl_set_repeat(RS::CanvasItemTextureRepeat p_repeat) {
317 if (p_repeat == state_repeat) {
318 return;
319 }
320 state_repeat = p_repeat;
321 GLenum prep = GL_CLAMP_TO_EDGE;
322 switch (state_repeat) {
323 case RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED: {
324 prep = GL_CLAMP_TO_EDGE;
325 } break;
326 case RS::CANVAS_ITEM_TEXTURE_REPEAT_ENABLED: {
327 prep = GL_REPEAT;
328 } break;
329 case RS::CANVAS_ITEM_TEXTURE_REPEAT_MIRROR: {
330 prep = GL_MIRRORED_REPEAT;
331 } break;
332 default: {
333 return;
334 } break;
335 }
336 glTexParameteri(target, GL_TEXTURE_WRAP_T, prep);
337 glTexParameteri(target, GL_TEXTURE_WRAP_R, prep);
338 glTexParameteri(target, GL_TEXTURE_WRAP_S, prep);
339 }
340
341private:
342 RS::CanvasItemTextureFilter state_filter = RS::CANVAS_ITEM_TEXTURE_FILTER_MAX;
343 RS::CanvasItemTextureRepeat state_repeat = RS::CANVAS_ITEM_TEXTURE_REPEAT_MAX;
344};
345
346struct RenderTarget {
347 Point2i position = Point2i(0, 0);
348 Size2i size = Size2i(0, 0);
349 uint32_t view_count = 1;
350 int mipmap_count = 1;
351 RID self;
352 GLuint fbo = 0;
353 GLuint color = 0;
354 GLuint depth = 0;
355 GLuint backbuffer_fbo = 0;
356 GLuint backbuffer = 0;
357 GLuint backbuffer_depth = 0;
358
359 GLuint color_internal_format = GL_RGBA8;
360 GLuint color_format = GL_RGBA;
361 GLuint color_type = GL_UNSIGNED_BYTE;
362 Image::Format image_format = Image::FORMAT_RGBA8;
363
364 GLuint sdf_texture_write = 0;
365 GLuint sdf_texture_write_fb = 0;
366 GLuint sdf_texture_process[2] = { 0, 0 };
367 GLuint sdf_texture_read = 0;
368 RS::ViewportSDFOversize sdf_oversize = RS::VIEWPORT_SDF_OVERSIZE_120_PERCENT;
369 RS::ViewportSDFScale sdf_scale = RS::VIEWPORT_SDF_SCALE_50_PERCENT;
370 Size2i process_size;
371 bool sdf_enabled = false;
372
373 bool is_transparent = false;
374 bool direct_to_screen = false;
375
376 bool used_in_frame = false;
377 RS::ViewportMSAA msaa = RS::VIEWPORT_MSAA_DISABLED;
378
379 struct RTOverridden {
380 bool is_overridden = false;
381 RID color;
382 RID depth;
383 RID velocity;
384
385 struct FBOCacheEntry {
386 GLuint fbo;
387 GLuint color;
388 GLuint depth;
389 Size2i size;
390 Vector<GLuint> allocated_textures;
391 };
392 RBMap<uint32_t, FBOCacheEntry> fbo_cache;
393 } overridden;
394
395 RID texture;
396
397 Color clear_color = Color(1, 1, 1, 1);
398 bool clear_requested = false;
399
400 RenderTarget() {
401 }
402};
403
404class TextureStorage : public RendererTextureStorage {
405private:
406 static TextureStorage *singleton;
407
408 RID default_gl_textures[DEFAULT_GL_TEXTURE_MAX];
409
410 /* Canvas Texture API */
411
412 RID_Owner<CanvasTexture, true> canvas_texture_owner;
413
414 /* Texture API */
415
416 mutable RID_Owner<Texture> texture_owner;
417
418 Ref<Image> _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;
419
420 /* TEXTURE ATLAS API */
421
422 struct TextureAtlas {
423 struct Texture {
424 int users;
425 Rect2 uv_rect;
426 };
427
428 struct SortItem {
429 RID texture;
430 Size2i pixel_size;
431 Size2i size;
432 Point2i pos;
433
434 bool operator<(const SortItem &p_item) const {
435 //sort larger to smaller
436 if (size.height == p_item.size.height) {
437 return size.width > p_item.size.width;
438 } else {
439 return size.height > p_item.size.height;
440 }
441 }
442 };
443
444 HashMap<RID, Texture> textures;
445 bool dirty = true;
446
447 GLuint texture = 0;
448 GLuint framebuffer = 0;
449 Size2i size;
450 } texture_atlas;
451
452 /* Render Target API */
453
454 mutable RID_Owner<RenderTarget> render_target_owner;
455
456 void _clear_render_target(RenderTarget *rt);
457 void _update_render_target(RenderTarget *rt);
458 void _create_render_target_backbuffer(RenderTarget *rt);
459 void _render_target_allocate_sdf(RenderTarget *rt);
460 void _render_target_clear_sdf(RenderTarget *rt);
461 Rect2i _render_target_get_sdf_rect(const RenderTarget *rt) const;
462
463 void _texture_set_data(RID p_texture, const Ref<Image> &p_image, int p_layer, bool initialize);
464
465 struct RenderTargetSDF {
466 CanvasSdfShaderGLES3 shader;
467 RID shader_version;
468 } sdf_shader;
469
470public:
471 static TextureStorage *get_singleton();
472
473 TextureStorage();
474 virtual ~TextureStorage();
475
476 _FORCE_INLINE_ RID texture_gl_get_default(DefaultGLTexture p_texture) {
477 return default_gl_textures[p_texture];
478 }
479
480 /* Canvas Texture API */
481
482 CanvasTexture *get_canvas_texture(RID p_rid) { return canvas_texture_owner.get_or_null(p_rid); };
483 bool owns_canvas_texture(RID p_rid) { return canvas_texture_owner.owns(p_rid); };
484
485 virtual RID canvas_texture_allocate() override;
486 virtual void canvas_texture_initialize(RID p_rid) override;
487 virtual void canvas_texture_free(RID p_rid) override;
488
489 virtual void canvas_texture_set_channel(RID p_canvas_texture, RS::CanvasTextureChannel p_channel, RID p_texture) override;
490 virtual void canvas_texture_set_shading_parameters(RID p_canvas_texture, const Color &p_base_color, float p_shininess) override;
491
492 virtual void canvas_texture_set_texture_filter(RID p_item, RS::CanvasItemTextureFilter p_filter) override;
493 virtual void canvas_texture_set_texture_repeat(RID p_item, RS::CanvasItemTextureRepeat p_repeat) override;
494
495 /* Texture API */
496
497 Texture *get_texture(RID p_rid) {
498 Texture *texture = texture_owner.get_or_null(p_rid);
499 if (texture && texture->is_proxy) {
500 return texture_owner.get_or_null(texture->proxy_to);
501 }
502 return texture;
503 };
504 bool owns_texture(RID p_rid) { return texture_owner.owns(p_rid); };
505
506 virtual bool can_create_resources_async() const override;
507
508 virtual RID texture_allocate() override;
509 virtual void texture_free(RID p_rid) override;
510
511 virtual void texture_2d_initialize(RID p_texture, const Ref<Image> &p_image) override;
512 virtual void texture_2d_layered_initialize(RID p_texture, const Vector<Ref<Image>> &p_layers, RS::TextureLayeredType p_layered_type) override;
513 virtual void texture_3d_initialize(RID p_texture, Image::Format, int p_width, int p_height, int p_depth, bool p_mipmaps, const Vector<Ref<Image>> &p_data) override;
514 virtual void texture_proxy_initialize(RID p_texture, RID p_base) override; //all slices, then all the mipmaps, must be coherent
515
516 RID texture_create_external(Texture::Type p_type, Image::Format p_format, unsigned int p_image, int p_width, int p_height, int p_depth, int p_layers, RS::TextureLayeredType p_layered_type = RS::TEXTURE_LAYERED_2D_ARRAY);
517
518 virtual void texture_2d_update(RID p_texture, const Ref<Image> &p_image, int p_layer = 0) override;
519 virtual void texture_3d_update(RID p_texture, const Vector<Ref<Image>> &p_data) override{};
520 virtual void texture_proxy_update(RID p_proxy, RID p_base) override;
521
522 //these two APIs can be used together or in combination with the others.
523 virtual void texture_2d_placeholder_initialize(RID p_texture) override;
524 virtual void texture_2d_layered_placeholder_initialize(RID p_texture, RenderingServer::TextureLayeredType p_layered_type) override;
525 virtual void texture_3d_placeholder_initialize(RID p_texture) override;
526
527 virtual Ref<Image> texture_2d_get(RID p_texture) const override;
528 virtual Ref<Image> texture_2d_layer_get(RID p_texture, int p_layer) const override { return Ref<Image>(); };
529 virtual Vector<Ref<Image>> texture_3d_get(RID p_texture) const override { return Vector<Ref<Image>>(); };
530
531 virtual void texture_replace(RID p_texture, RID p_by_texture) override;
532 virtual void texture_set_size_override(RID p_texture, int p_width, int p_height) override;
533
534 virtual void texture_set_path(RID p_texture, const String &p_path) override;
535 virtual String texture_get_path(RID p_texture) const override;
536
537 virtual void texture_set_detect_3d_callback(RID p_texture, RS::TextureDetectCallback p_callback, void *p_userdata) override;
538 void texture_set_detect_srgb_callback(RID p_texture, RS::TextureDetectCallback p_callback, void *p_userdata);
539 virtual void texture_set_detect_normal_callback(RID p_texture, RS::TextureDetectCallback p_callback, void *p_userdata) override;
540 virtual void texture_set_detect_roughness_callback(RID p_texture, RS::TextureDetectRoughnessCallback p_callback, void *p_userdata) override;
541
542 virtual void texture_debug_usage(List<RS::TextureInfo> *r_info) override;
543
544 virtual void texture_set_force_redraw_if_visible(RID p_texture, bool p_enable) override;
545
546 virtual Size2 texture_size_with_proxy(RID p_proxy) override;
547
548 virtual void texture_rd_initialize(RID p_texture, const RID &p_rd_texture, const RS::TextureLayeredType p_layer_type = RS::TEXTURE_LAYERED_2D_ARRAY) override;
549 virtual RID texture_get_rd_texture(RID p_texture, bool p_srgb = false) const override;
550 virtual uint64_t texture_get_native_handle(RID p_texture, bool p_srgb = false) const override;
551
552 void texture_set_data(RID p_texture, const Ref<Image> &p_image, int p_layer = 0);
553 virtual Image::Format texture_get_format(RID p_texture) const override;
554 uint32_t texture_get_texid(RID p_texture) const;
555 uint32_t texture_get_width(RID p_texture) const;
556 uint32_t texture_get_height(RID p_texture) const;
557 uint32_t texture_get_depth(RID p_texture) const;
558 void texture_bind(RID p_texture, uint32_t p_texture_no);
559
560 /* TEXTURE ATLAS API */
561
562 void update_texture_atlas();
563
564 GLuint texture_atlas_get_texture() const;
565 _FORCE_INLINE_ Rect2 texture_atlas_get_texture_rect(RID p_texture) {
566 TextureAtlas::Texture *t = texture_atlas.textures.getptr(p_texture);
567 if (!t) {
568 return Rect2();
569 }
570
571 return t->uv_rect;
572 }
573
574 void texture_add_to_texture_atlas(RID p_texture);
575 void texture_remove_from_texture_atlas(RID p_texture);
576 void texture_atlas_mark_dirty_on_texture(RID p_texture);
577 void texture_atlas_remove_texture(RID p_texture);
578
579 /* DECAL API */
580
581 virtual RID decal_allocate() override;
582 virtual void decal_initialize(RID p_rid) override;
583 virtual void decal_free(RID p_rid) override{};
584
585 virtual void decal_set_size(RID p_decal, const Vector3 &p_size) override;
586 virtual void decal_set_texture(RID p_decal, RS::DecalTexture p_type, RID p_texture) override;
587 virtual void decal_set_emission_energy(RID p_decal, float p_energy) override;
588 virtual void decal_set_albedo_mix(RID p_decal, float p_mix) override;
589 virtual void decal_set_modulate(RID p_decal, const Color &p_modulate) override;
590 virtual void decal_set_cull_mask(RID p_decal, uint32_t p_layers) override;
591 virtual void decal_set_distance_fade(RID p_decal, bool p_enabled, float p_begin, float p_length) override;
592 virtual void decal_set_fade(RID p_decal, float p_above, float p_below) override;
593 virtual void decal_set_normal_fade(RID p_decal, float p_fade) override;
594
595 virtual AABB decal_get_aabb(RID p_decal) const override;
596 virtual uint32_t decal_get_cull_mask(RID p_decal) const override { return 0; }
597
598 virtual void texture_add_to_decal_atlas(RID p_texture, bool p_panorama_to_dp = false) override {}
599 virtual void texture_remove_from_decal_atlas(RID p_texture, bool p_panorama_to_dp = false) override {}
600
601 /* DECAL INSTANCE */
602
603 virtual RID decal_instance_create(RID p_decal) override { return RID(); }
604 virtual void decal_instance_free(RID p_decal_instance) override {}
605 virtual void decal_instance_set_transform(RID p_decal, const Transform3D &p_transform) override {}
606 virtual void decal_instance_set_sorting_offset(RID p_decal_instance, float p_sorting_offset) override {}
607
608 /* RENDER TARGET API */
609
610 static GLuint system_fbo;
611
612 RenderTarget *get_render_target(RID p_rid) { return render_target_owner.get_or_null(p_rid); };
613 bool owns_render_target(RID p_rid) { return render_target_owner.owns(p_rid); };
614
615 void copy_scene_to_backbuffer(RenderTarget *rt, const bool uses_screen_texture, const bool uses_depth_texture);
616
617 virtual RID render_target_create() override;
618 virtual void render_target_free(RID p_rid) override;
619
620 virtual void render_target_set_position(RID p_render_target, int p_x, int p_y) override;
621 virtual Point2i render_target_get_position(RID p_render_target) const override;
622 virtual void render_target_set_size(RID p_render_target, int p_width, int p_height, uint32_t p_view_count) override;
623 virtual Size2i render_target_get_size(RID p_render_target) const override;
624 virtual void render_target_set_transparent(RID p_render_target, bool p_is_transparent) override;
625 virtual bool render_target_get_transparent(RID p_render_target) const override;
626 virtual void render_target_set_direct_to_screen(RID p_render_target, bool p_direct_to_screen) override;
627 virtual bool render_target_get_direct_to_screen(RID p_render_target) const override;
628 virtual bool render_target_was_used(RID p_render_target) const override;
629 void render_target_clear_used(RID p_render_target);
630 virtual void render_target_set_msaa(RID p_render_target, RS::ViewportMSAA p_msaa) override;
631 virtual RS::ViewportMSAA render_target_get_msaa(RID p_render_target) const override;
632 virtual void render_target_set_use_hdr(RID p_render_target, bool p_use_hdr_2d) override {}
633 virtual bool render_target_is_using_hdr(RID p_render_target) const override { return false; }
634
635 // new
636 void render_target_set_as_unused(RID p_render_target) override {
637 render_target_clear_used(p_render_target);
638 }
639
640 void render_target_request_clear(RID p_render_target, const Color &p_clear_color) override;
641 bool render_target_is_clear_requested(RID p_render_target) override;
642 Color render_target_get_clear_request_color(RID p_render_target) override;
643 void render_target_disable_clear_request(RID p_render_target) override;
644 void render_target_do_clear_request(RID p_render_target) override;
645
646 virtual void render_target_set_sdf_size_and_scale(RID p_render_target, RS::ViewportSDFOversize p_size, RS::ViewportSDFScale p_scale) override;
647 virtual Rect2i render_target_get_sdf_rect(RID p_render_target) const override;
648 GLuint render_target_get_sdf_texture(RID p_render_target);
649 GLuint render_target_get_sdf_framebuffer(RID p_render_target);
650 void render_target_sdf_process(RID p_render_target);
651 virtual void render_target_mark_sdf_enabled(RID p_render_target, bool p_enabled) override;
652 bool render_target_is_sdf_enabled(RID p_render_target) const;
653
654 void render_target_copy_to_back_buffer(RID p_render_target, const Rect2i &p_region, bool p_gen_mipmaps);
655 void render_target_clear_back_buffer(RID p_render_target, const Rect2i &p_region, const Color &p_color);
656 void render_target_gen_back_buffer_mipmaps(RID p_render_target, const Rect2i &p_region);
657
658 virtual void render_target_set_vrs_mode(RID p_render_target, RS::ViewportVRSMode p_mode) override {}
659 virtual RS::ViewportVRSMode render_target_get_vrs_mode(RID p_render_target) const override { return RS::VIEWPORT_VRS_DISABLED; }
660 virtual void render_target_set_vrs_texture(RID p_render_target, RID p_texture) override {}
661 virtual RID render_target_get_vrs_texture(RID p_render_target) const override { return RID(); }
662
663 virtual void render_target_set_override(RID p_render_target, RID p_color_texture, RID p_depth_texture, RID p_velocity_texture) override;
664 virtual RID render_target_get_override_color(RID p_render_target) const override;
665 virtual RID render_target_get_override_depth(RID p_render_target) const override;
666 virtual RID render_target_get_override_velocity(RID p_render_target) const override;
667
668 virtual RID render_target_get_texture(RID p_render_target) override;
669
670 void bind_framebuffer(GLuint framebuffer) {
671 glBindFramebuffer(GL_FRAMEBUFFER, framebuffer);
672 }
673
674 void bind_framebuffer_system() {
675 glBindFramebuffer(GL_FRAMEBUFFER, GLES3::TextureStorage::system_fbo);
676 }
677
678 String get_framebuffer_error(GLenum p_status);
679};
680
681inline String TextureStorage::get_framebuffer_error(GLenum p_status) {
682#if defined(DEBUG_ENABLED) && defined(GLES_OVER_GL)
683 if (p_status == GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT) {
684 return "GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT";
685 } else if (p_status == GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT) {
686 return "GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT";
687 } else if (p_status == GL_FRAMEBUFFER_INCOMPLETE_DRAW_BUFFER) {
688 return "GL_FRAMEBUFFER_INCOMPLETE_DRAW_BUFFER";
689 } else if (p_status == GL_FRAMEBUFFER_INCOMPLETE_READ_BUFFER) {
690 return "GL_FRAMEBUFFER_INCOMPLETE_READ_BUFFER";
691 }
692#endif
693 return itos(p_status);
694}
695
696} // namespace GLES3
697
698#endif // GLES3_ENABLED
699
700#endif // TEXTURE_STORAGE_GLES3_H
701