Path: blob/master/drivers/gles3/storage/texture_storage.h
10000 views
/**************************************************************************/1/* texture_storage.h */2/**************************************************************************/3/* This file is part of: */4/* GODOT ENGINE */5/* https://godotengine.org */6/**************************************************************************/7/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */8/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */9/* */10/* Permission is hereby granted, free of charge, to any person obtaining */11/* a copy of this software and associated documentation files (the */12/* "Software"), to deal in the Software without restriction, including */13/* without limitation the rights to use, copy, modify, merge, publish, */14/* distribute, sublicense, and/or sell copies of the Software, and to */15/* permit persons to whom the Software is furnished to do so, subject to */16/* the following conditions: */17/* */18/* The above copyright notice and this permission notice shall be */19/* included in all copies or substantial portions of the Software. */20/* */21/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */22/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */23/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */24/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */25/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */26/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */27/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */28/**************************************************************************/2930#pragma once3132#ifdef GLES3_ENABLED3334#include "platform_gl.h"3536#include "config.h"37#include "core/io/image.h"38#include "core/os/os.h"39#include "core/templates/rid_owner.h"40#include "servers/rendering/renderer_compositor.h"41#include "servers/rendering/storage/texture_storage.h"4243#include "drivers/gles3/shaders/canvas_sdf.glsl.gen.h"4445namespace GLES3 {4647#define _GL_TEXTURE_MAX_ANISOTROPY_EXT 0x84FE48#define _GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT 0x84FF4950#define _EXT_COMPRESSED_RGBA_S3TC_DXT1_EXT 0x83F151#define _EXT_COMPRESSED_RGBA_S3TC_DXT3_EXT 0x83F252#define _EXT_COMPRESSED_RGBA_S3TC_DXT5_EXT 0x83F35354#define _EXT_COMPRESSED_RED_RGTC1_EXT 0x8DBB55#define _EXT_COMPRESSED_RED_RGTC1 0x8DBB56#define _EXT_COMPRESSED_SIGNED_RED_RGTC1 0x8DBC57#define _EXT_COMPRESSED_RG_RGTC2 0x8DBD58#define _EXT_COMPRESSED_SIGNED_RG_RGTC2 0x8DBE59#define _EXT_COMPRESSED_SIGNED_RED_RGTC1_EXT 0x8DBC60#define _EXT_COMPRESSED_RED_GREEN_RGTC2_EXT 0x8DBD61#define _EXT_COMPRESSED_SIGNED_RED_GREEN_RGTC2_EXT 0x8DBE62#define _EXT_ETC1_RGB8_OES 0x8D646364#define _EXT_COMPRESSED_RGBA_BPTC_UNORM 0x8E8C65#define _EXT_COMPRESSED_SRGB_ALPHA_BPTC_UNORM 0x8E8D66#define _EXT_COMPRESSED_RGB_BPTC_SIGNED_FLOAT 0x8E8E67#define _EXT_COMPRESSED_RGB_BPTC_UNSIGNED_FLOAT 0x8E8F6869#define _EXT_COMPRESSED_R11_EAC 0x927070#define _EXT_COMPRESSED_SIGNED_R11_EAC 0x927171#define _EXT_COMPRESSED_RG11_EAC 0x927272#define _EXT_COMPRESSED_SIGNED_RG11_EAC 0x927373#define _EXT_COMPRESSED_RGB8_ETC2 0x927474#define _EXT_COMPRESSED_SRGB8_ETC2 0x927575#define _EXT_COMPRESSED_RGB8_PUNCHTHROUGH_ALPHA1_ETC2 0x927676#define _EXT_COMPRESSED_SRGB8_PUNCHTHROUGH_ALPHA1_ETC2 0x927777#define _EXT_COMPRESSED_RGBA8_ETC2_EAC 0x927878#define _EXT_COMPRESSED_SRGB8_ALPHA8_ETC2_EAC 0x92797980#define _EXT_COMPRESSED_RGBA_ASTC_4x4_KHR 0x93B081#define _EXT_COMPRESSED_RGBA_ASTC_5x4_KHR 0x93B182#define _EXT_COMPRESSED_RGBA_ASTC_5x5_KHR 0x93B283#define _EXT_COMPRESSED_RGBA_ASTC_6x5_KHR 0x93B384#define _EXT_COMPRESSED_RGBA_ASTC_6x6_KHR 0x93B485#define _EXT_COMPRESSED_RGBA_ASTC_8x5_KHR 0x93B586#define _EXT_COMPRESSED_RGBA_ASTC_8x6_KHR 0x93B687#define _EXT_COMPRESSED_RGBA_ASTC_8x8_KHR 0x93B788#define _EXT_COMPRESSED_RGBA_ASTC_10x5_KHR 0x93B889#define _EXT_COMPRESSED_RGBA_ASTC_10x6_KHR 0x93B990#define _EXT_COMPRESSED_RGBA_ASTC_10x8_KHR 0x93BA91#define _EXT_COMPRESSED_RGBA_ASTC_10x10_KHR 0x93BB92#define _EXT_COMPRESSED_RGBA_ASTC_12x10_KHR 0x93BC93#define _EXT_COMPRESSED_RGBA_ASTC_12x12_KHR 0x93BD9495#define _EXT_COMPRESSED_SRGB8_ALPHA8_ASTC_4x4_KHR 0x93D096#define _EXT_COMPRESSED_SRGB8_ALPHA8_ASTC_5x4_KHR 0x93D197#define _EXT_COMPRESSED_SRGB8_ALPHA8_ASTC_5x5_KHR 0x93D298#define _EXT_COMPRESSED_SRGB8_ALPHA8_ASTC_6x5_KHR 0x93D399#define _EXT_COMPRESSED_SRGB8_ALPHA8_ASTC_6x6_KHR 0x93D4100#define _EXT_COMPRESSED_SRGB8_ALPHA8_ASTC_8x5_KHR 0x93D5101#define _EXT_COMPRESSED_SRGB8_ALPHA8_ASTC_8x6_KHR 0x93D6102#define _EXT_COMPRESSED_SRGB8_ALPHA8_ASTC_8x8_KHR 0x93D7103#define _EXT_COMPRESSED_SRGB8_ALPHA8_ASTC_10x5_KHR 0x93D8104#define _EXT_COMPRESSED_SRGB8_ALPHA8_ASTC_10x6_KHR 0x93D9105#define _EXT_COMPRESSED_SRGB8_ALPHA8_ASTC_10x8_KHR 0x93DA106#define _EXT_COMPRESSED_SRGB8_ALPHA8_ASTC_10x10_KHR 0x93DB107#define _EXT_COMPRESSED_SRGB8_ALPHA8_ASTC_12x10_KHR 0x93DC108#define _EXT_COMPRESSED_SRGB8_ALPHA8_ASTC_12x12_KHR 0x93DD109110#define _GL_TEXTURE_EXTERNAL_OES 0x8D65111112#define _EXT_TEXTURE_CUBE_MAP_SEAMLESS 0x884F113114enum DefaultGLTexture {115DEFAULT_GL_TEXTURE_WHITE,116DEFAULT_GL_TEXTURE_BLACK,117DEFAULT_GL_TEXTURE_TRANSPARENT,118DEFAULT_GL_TEXTURE_NORMAL,119DEFAULT_GL_TEXTURE_ANISO,120DEFAULT_GL_TEXTURE_DEPTH,121DEFAULT_GL_TEXTURE_CUBEMAP_BLACK,122//DEFAULT_GL_TEXTURE_CUBEMAP_ARRAY_BLACK, // Cubemap Arrays not supported in GL 3.3 or GL ES 3.0123DEFAULT_GL_TEXTURE_CUBEMAP_WHITE,124DEFAULT_GL_TEXTURE_3D_WHITE,125DEFAULT_GL_TEXTURE_3D_BLACK,126DEFAULT_GL_TEXTURE_2D_ARRAY_WHITE,127DEFAULT_GL_TEXTURE_2D_UINT,128DEFAULT_GL_TEXTURE_EXT,129DEFAULT_GL_TEXTURE_MAX130};131132struct CanvasTexture {133RID diffuse;134RID normal_map;135RID specular;136Color specular_color = Color(1, 1, 1, 1);137float shininess = 1.0;138139RS::CanvasItemTextureFilter texture_filter = RS::CANVAS_ITEM_TEXTURE_FILTER_DEFAULT;140RS::CanvasItemTextureRepeat texture_repeat = RS::CANVAS_ITEM_TEXTURE_REPEAT_DEFAULT;141};142143struct RenderTarget;144145struct Texture {146RID self;147148bool is_proxy = false;149bool is_from_native_handle = false;150bool is_render_target = false;151152RID proxy_to;153Vector<RID> proxies;154155String path;156int width = 0;157int height = 0;158int depth = 0;159int mipmaps = 1;160int layers = 1;161int alloc_width = 0;162int alloc_height = 0;163Image::Format format = Image::FORMAT_R8;164Image::Format real_format = Image::FORMAT_R8;165166enum Type {167TYPE_2D,168TYPE_LAYERED,169TYPE_3D170};171172Type type = TYPE_2D;173RS::TextureLayeredType layered_type = RS::TEXTURE_LAYERED_2D_ARRAY;174175GLenum target = GL_TEXTURE_2D;176GLenum gl_format_cache = 0;177GLenum gl_internal_format_cache = 0;178GLenum gl_type_cache = 0;179180int total_data_size = 0;181182bool compressed = false;183184bool resize_to_po2 = false;185186bool active = false;187GLuint tex_id = 0;188189uint16_t stored_cube_sides = 0;190191RenderTarget *render_target = nullptr;192193Ref<Image> image_cache_2d;194Vector<Ref<Image>> image_cache_3d;195196bool redraw_if_visible = false;197198RS::TextureDetectCallback detect_3d_callback = nullptr;199void *detect_3d_callback_ud = nullptr;200201RS::TextureDetectCallback detect_normal_callback = nullptr;202void *detect_normal_callback_ud = nullptr;203204RS::TextureDetectRoughnessCallback detect_roughness_callback = nullptr;205void *detect_roughness_callback_ud = nullptr;206207CanvasTexture *canvas_texture = nullptr;208209void copy_from(const Texture &o) {210proxy_to = o.proxy_to;211is_proxy = o.is_proxy;212is_from_native_handle = o.is_from_native_handle;213width = o.width;214height = o.height;215alloc_width = o.alloc_width;216alloc_height = o.alloc_height;217format = o.format;218type = o.type;219layered_type = o.layered_type;220target = o.target;221total_data_size = o.total_data_size;222compressed = o.compressed;223mipmaps = o.mipmaps;224resize_to_po2 = o.resize_to_po2;225active = o.active;226tex_id = o.tex_id;227stored_cube_sides = o.stored_cube_sides;228render_target = o.render_target;229is_render_target = o.is_render_target;230redraw_if_visible = o.redraw_if_visible;231detect_3d_callback = o.detect_3d_callback;232detect_3d_callback_ud = o.detect_3d_callback_ud;233detect_normal_callback = o.detect_normal_callback;234detect_normal_callback_ud = o.detect_normal_callback_ud;235detect_roughness_callback = o.detect_roughness_callback;236detect_roughness_callback_ud = o.detect_roughness_callback_ud;237}238239// texture state240void gl_set_filter(RS::CanvasItemTextureFilter p_filter) {241if (p_filter == state_filter) {242return;243}244Config *config = Config::get_singleton();245state_filter = p_filter;246GLenum pmin = GL_NEAREST;247GLenum pmag = GL_NEAREST;248GLint max_lod = 0;249GLfloat anisotropy = 1.0f;250switch (state_filter) {251case RS::CANVAS_ITEM_TEXTURE_FILTER_NEAREST: {252pmin = GL_NEAREST;253pmag = GL_NEAREST;254max_lod = 0;255} break;256case RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR: {257pmin = GL_LINEAR;258pmag = GL_LINEAR;259max_lod = 0;260} break;261case RS::CANVAS_ITEM_TEXTURE_FILTER_NEAREST_WITH_MIPMAPS_ANISOTROPIC: {262anisotropy = config->anisotropic_level;263};264[[fallthrough]];265case RS::CANVAS_ITEM_TEXTURE_FILTER_NEAREST_WITH_MIPMAPS: {266pmag = GL_NEAREST;267if (mipmaps <= 1) {268pmin = GL_NEAREST;269max_lod = 0;270} else if (config->use_nearest_mip_filter) {271pmin = GL_NEAREST_MIPMAP_NEAREST;272max_lod = 1000;273} else {274pmin = GL_NEAREST_MIPMAP_LINEAR;275max_lod = 1000;276}277} break;278case RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR_WITH_MIPMAPS_ANISOTROPIC: {279anisotropy = config->anisotropic_level;280};281[[fallthrough]];282case RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR_WITH_MIPMAPS: {283pmag = GL_LINEAR;284if (mipmaps <= 1) {285pmin = GL_LINEAR;286max_lod = 0;287} else if (config->use_nearest_mip_filter) {288pmin = GL_LINEAR_MIPMAP_NEAREST;289max_lod = 1000;290} else {291pmin = GL_LINEAR_MIPMAP_LINEAR;292max_lod = 1000;293}294} break;295default: {296return;297} break;298}299glTexParameteri(target, GL_TEXTURE_MIN_FILTER, pmin);300glTexParameteri(target, GL_TEXTURE_MAG_FILTER, pmag);301glTexParameteri(target, GL_TEXTURE_BASE_LEVEL, 0);302glTexParameteri(target, GL_TEXTURE_MAX_LEVEL, max_lod);303if (config->support_anisotropic_filter) {304glTexParameterf(target, _GL_TEXTURE_MAX_ANISOTROPY_EXT, anisotropy);305}306}307void gl_set_repeat(RS::CanvasItemTextureRepeat p_repeat) {308if (p_repeat == state_repeat) {309return;310}311state_repeat = p_repeat;312GLenum prep = GL_CLAMP_TO_EDGE;313switch (state_repeat) {314case RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED: {315prep = GL_CLAMP_TO_EDGE;316} break;317case RS::CANVAS_ITEM_TEXTURE_REPEAT_ENABLED: {318prep = GL_REPEAT;319} break;320case RS::CANVAS_ITEM_TEXTURE_REPEAT_MIRROR: {321prep = GL_MIRRORED_REPEAT;322} break;323default: {324return;325} break;326}327glTexParameteri(target, GL_TEXTURE_WRAP_T, prep);328glTexParameteri(target, GL_TEXTURE_WRAP_R, prep);329glTexParameteri(target, GL_TEXTURE_WRAP_S, prep);330}331332private:333RS::CanvasItemTextureFilter state_filter = RS::CANVAS_ITEM_TEXTURE_FILTER_MAX;334RS::CanvasItemTextureRepeat state_repeat = RS::CANVAS_ITEM_TEXTURE_REPEAT_MAX;335};336337struct RenderTarget {338Point2i position = Point2i(0, 0);339Size2i size = Size2i(0, 0);340uint32_t view_count = 1;341int mipmap_count = 1;342RID self;343GLuint fbo = 0;344GLuint color = 0;345GLuint depth = 0;346GLuint backbuffer_fbo = 0;347GLuint backbuffer = 0;348GLuint backbuffer_depth = 0;349bool depth_has_stencil = true;350351bool hdr = false; // For Compatibility this effects both 2D and 3D rendering!352GLuint color_internal_format = GL_RGBA8;353GLuint color_format = GL_RGBA;354GLuint color_type = GL_UNSIGNED_BYTE;355uint32_t color_format_size = 4;356Image::Format image_format = Image::FORMAT_RGBA8;357358GLuint sdf_texture_write = 0;359GLuint sdf_texture_write_fb = 0;360GLuint sdf_texture_process[2] = { 0, 0 };361GLuint sdf_texture_read = 0;362RS::ViewportSDFOversize sdf_oversize = RS::VIEWPORT_SDF_OVERSIZE_120_PERCENT;363RS::ViewportSDFScale sdf_scale = RS::VIEWPORT_SDF_SCALE_50_PERCENT;364Size2i process_size;365bool sdf_enabled = false;366367bool is_transparent = false;368bool direct_to_screen = false;369370bool used_in_frame = false;371RS::ViewportMSAA msaa = RS::VIEWPORT_MSAA_DISABLED;372bool reattach_textures = false;373374Rect2i render_region;375376struct RTOverridden {377bool is_overridden = false;378bool depth_has_stencil = false;379RID color;380RID depth;381RID velocity;382383struct FBOCacheEntry {384GLuint fbo;385GLuint color;386GLuint depth;387Size2i size;388Vector<GLuint> allocated_textures;389bool depth_has_stencil;390};391RBMap<uint32_t, FBOCacheEntry> fbo_cache;392} overridden;393394RID texture;395396Color clear_color = Color(1, 1, 1, 1);397bool clear_requested = false;398399RenderTarget() {400}401};402403class TextureStorage : public RendererTextureStorage {404private:405static TextureStorage *singleton;406407RID default_gl_textures[DEFAULT_GL_TEXTURE_MAX];408409/* Canvas Texture API */410411RID_Owner<CanvasTexture, true> canvas_texture_owner;412413/* Texture API */414// Textures can be created from threads, so this RID_Owner is thread safe.415mutable RID_Owner<Texture, true> texture_owner;416417Ref<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;418419/* TEXTURE ATLAS API */420421struct TextureAtlas {422struct Texture {423int users;424Rect2 uv_rect;425};426427struct SortItem {428RID texture;429Size2i pixel_size;430Size2i size;431Point2i pos;432433bool operator<(const SortItem &p_item) const {434//sort larger to smaller435if (size.height == p_item.size.height) {436return size.width > p_item.size.width;437} else {438return size.height > p_item.size.height;439}440}441};442443HashMap<RID, Texture> textures;444bool dirty = true;445446GLuint texture = 0;447GLuint framebuffer = 0;448Size2i size;449} texture_atlas;450451/* Render Target API */452453mutable RID_Owner<RenderTarget> render_target_owner;454455void _clear_render_target(RenderTarget *rt);456void _update_render_target(RenderTarget *rt);457void _create_render_target_backbuffer(RenderTarget *rt);458void _render_target_allocate_sdf(RenderTarget *rt);459void _render_target_clear_sdf(RenderTarget *rt);460Rect2i _render_target_get_sdf_rect(const RenderTarget *rt) const;461462void _texture_set_data(RID p_texture, const Ref<Image> &p_image, int p_layer, bool p_initialize);463void _texture_set_3d_data(RID p_texture, const Vector<Ref<Image>> &p_data, bool p_initialize);464void _texture_set_swizzle(Texture *p_texture, Image::Format p_real_format);465Vector<Ref<Image>> _texture_3d_read_framebuffer(Texture *p_texture) const;466467struct RenderTargetSDF {468CanvasSdfShaderGLES3 shader;469RID shader_version;470} sdf_shader;471472public:473static TextureStorage *get_singleton();474475TextureStorage();476virtual ~TextureStorage();477478_FORCE_INLINE_ RID texture_gl_get_default(DefaultGLTexture p_texture) {479return default_gl_textures[p_texture];480}481482/* Canvas Texture API */483484CanvasTexture *get_canvas_texture(RID p_rid) { return canvas_texture_owner.get_or_null(p_rid); }485bool owns_canvas_texture(RID p_rid) { return canvas_texture_owner.owns(p_rid); }486487virtual RID canvas_texture_allocate() override;488virtual void canvas_texture_initialize(RID p_rid) override;489virtual void canvas_texture_free(RID p_rid) override;490491virtual void canvas_texture_set_channel(RID p_canvas_texture, RS::CanvasTextureChannel p_channel, RID p_texture) override;492virtual void canvas_texture_set_shading_parameters(RID p_canvas_texture, const Color &p_base_color, float p_shininess) override;493494virtual void canvas_texture_set_texture_filter(RID p_item, RS::CanvasItemTextureFilter p_filter) override;495virtual void canvas_texture_set_texture_repeat(RID p_item, RS::CanvasItemTextureRepeat p_repeat) override;496497/* Texture API */498499Texture *get_texture(RID p_rid) const {500Texture *texture = texture_owner.get_or_null(p_rid);501if (texture && texture->is_proxy) {502return texture_owner.get_or_null(texture->proxy_to);503}504return texture;505}506bool owns_texture(RID p_rid) { return texture_owner.owns(p_rid); }507508void texture_2d_initialize_from_texture(RID p_texture, Texture &p_tex) {509texture_owner.initialize_rid(p_texture, p_tex);510}511512virtual RID texture_allocate() override;513virtual void texture_free(RID p_rid) override;514515virtual void texture_2d_initialize(RID p_texture, const Ref<Image> &p_image) override;516virtual void texture_2d_layered_initialize(RID p_texture, const Vector<Ref<Image>> &p_layers, RS::TextureLayeredType p_layered_type) override;517virtual 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;518virtual void texture_external_initialize(RID p_texture, int p_width, int p_height, uint64_t p_external_buffer) override;519virtual void texture_proxy_initialize(RID p_texture, RID p_base) override; //all slices, then all the mipmaps, must be coherent520521virtual RID 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 = 1, RS::TextureLayeredType p_layered_type = RS::TEXTURE_LAYERED_2D_ARRAY) override;522523virtual void texture_2d_update(RID p_texture, const Ref<Image> &p_image, int p_layer = 0) override;524virtual void texture_3d_update(RID p_texture, const Vector<Ref<Image>> &p_data) override;525virtual void texture_external_update(RID p_texture, int p_width, int p_height, uint64_t p_external_buffer) override;526virtual void texture_proxy_update(RID p_proxy, RID p_base) override;527528Ref<Image> texture_2d_placeholder;529Vector<Ref<Image>> texture_2d_array_placeholder;530Vector<Ref<Image>> cubemap_placeholder;531Vector<Ref<Image>> texture_3d_placeholder;532533//these two APIs can be used together or in combination with the others.534virtual void texture_2d_placeholder_initialize(RID p_texture) override;535virtual void texture_2d_layered_placeholder_initialize(RID p_texture, RenderingServer::TextureLayeredType p_layered_type) override;536virtual void texture_3d_placeholder_initialize(RID p_texture) override;537538virtual Ref<Image> texture_2d_get(RID p_texture) const override;539virtual Ref<Image> texture_2d_layer_get(RID p_texture, int p_layer) const override;540virtual Vector<Ref<Image>> texture_3d_get(RID p_texture) const override;541542virtual void texture_replace(RID p_texture, RID p_by_texture) override;543virtual void texture_set_size_override(RID p_texture, int p_width, int p_height) override;544545virtual void texture_set_path(RID p_texture, const String &p_path) override;546virtual String texture_get_path(RID p_texture) const override;547548virtual void texture_set_detect_3d_callback(RID p_texture, RS::TextureDetectCallback p_callback, void *p_userdata) override;549void texture_set_detect_srgb_callback(RID p_texture, RS::TextureDetectCallback p_callback, void *p_userdata);550virtual void texture_set_detect_normal_callback(RID p_texture, RS::TextureDetectCallback p_callback, void *p_userdata) override;551virtual void texture_set_detect_roughness_callback(RID p_texture, RS::TextureDetectRoughnessCallback p_callback, void *p_userdata) override;552553virtual void texture_debug_usage(List<RS::TextureInfo> *r_info) override;554555virtual void texture_set_force_redraw_if_visible(RID p_texture, bool p_enable) override;556557virtual Size2 texture_size_with_proxy(RID p_proxy) override;558559virtual void texture_rd_initialize(RID p_texture, const RID &p_rd_texture, const RS::TextureLayeredType p_layer_type = RS::TEXTURE_LAYERED_2D_ARRAY) override;560virtual RID texture_get_rd_texture(RID p_texture, bool p_srgb = false) const override;561virtual uint64_t texture_get_native_handle(RID p_texture, bool p_srgb = false) const override;562563void texture_set_data(RID p_texture, const Ref<Image> &p_image, int p_layer = 0);564virtual Image::Format texture_get_format(RID p_texture) const override;565uint32_t texture_get_texid(RID p_texture) const;566Vector3i texture_get_size(RID p_texture) const;567uint32_t texture_get_width(RID p_texture) const;568uint32_t texture_get_height(RID p_texture) const;569uint32_t texture_get_depth(RID p_texture) const;570void texture_bind(RID p_texture, uint32_t p_texture_no);571572/* TEXTURE ATLAS API */573574void update_texture_atlas();575576GLuint texture_atlas_get_texture() const;577_FORCE_INLINE_ Rect2 texture_atlas_get_texture_rect(RID p_texture) {578TextureAtlas::Texture *t = texture_atlas.textures.getptr(p_texture);579if (!t) {580return Rect2();581}582583return t->uv_rect;584}585586void texture_add_to_texture_atlas(RID p_texture);587void texture_remove_from_texture_atlas(RID p_texture);588void texture_atlas_mark_dirty_on_texture(RID p_texture);589void texture_atlas_remove_texture(RID p_texture);590591/* DECAL API */592593virtual RID decal_allocate() override;594virtual void decal_initialize(RID p_rid) override;595virtual void decal_free(RID p_rid) override {}596597virtual void decal_set_size(RID p_decal, const Vector3 &p_size) override;598virtual void decal_set_texture(RID p_decal, RS::DecalTexture p_type, RID p_texture) override;599virtual void decal_set_emission_energy(RID p_decal, float p_energy) override;600virtual void decal_set_albedo_mix(RID p_decal, float p_mix) override;601virtual void decal_set_modulate(RID p_decal, const Color &p_modulate) override;602virtual void decal_set_cull_mask(RID p_decal, uint32_t p_layers) override;603virtual void decal_set_distance_fade(RID p_decal, bool p_enabled, float p_begin, float p_length) override;604virtual void decal_set_fade(RID p_decal, float p_above, float p_below) override;605virtual void decal_set_normal_fade(RID p_decal, float p_fade) override;606607virtual AABB decal_get_aabb(RID p_decal) const override;608virtual uint32_t decal_get_cull_mask(RID p_decal) const override { return 0; }609610virtual void texture_add_to_decal_atlas(RID p_texture, bool p_panorama_to_dp = false) override {}611virtual void texture_remove_from_decal_atlas(RID p_texture, bool p_panorama_to_dp = false) override {}612613/* DECAL INSTANCE */614615virtual RID decal_instance_create(RID p_decal) override { return RID(); }616virtual void decal_instance_free(RID p_decal_instance) override {}617virtual void decal_instance_set_transform(RID p_decal, const Transform3D &p_transform) override {}618virtual void decal_instance_set_sorting_offset(RID p_decal_instance, float p_sorting_offset) override {}619620/* RENDER TARGET API */621622static GLuint system_fbo;623624RenderTarget *get_render_target(RID p_rid) { return render_target_owner.get_or_null(p_rid); }625bool owns_render_target(RID p_rid) { return render_target_owner.owns(p_rid); }626627void check_backbuffer(RenderTarget *rt, const bool uses_screen_texture, const bool uses_depth_texture);628629virtual RID render_target_create() override;630virtual void render_target_free(RID p_rid) override;631632virtual void render_target_set_position(RID p_render_target, int p_x, int p_y) override;633virtual Point2i render_target_get_position(RID p_render_target) const override;634virtual void render_target_set_size(RID p_render_target, int p_width, int p_height, uint32_t p_view_count) override;635virtual Size2i render_target_get_size(RID p_render_target) const override;636virtual void render_target_set_transparent(RID p_render_target, bool p_is_transparent) override;637virtual bool render_target_get_transparent(RID p_render_target) const override;638virtual void render_target_set_direct_to_screen(RID p_render_target, bool p_direct_to_screen) override;639virtual bool render_target_get_direct_to_screen(RID p_render_target) const override;640virtual bool render_target_was_used(RID p_render_target) const override;641void render_target_clear_used(RID p_render_target);642virtual void render_target_set_msaa(RID p_render_target, RS::ViewportMSAA p_msaa) override;643virtual RS::ViewportMSAA render_target_get_msaa(RID p_render_target) const override;644virtual void render_target_set_msaa_needs_resolve(RID p_render_target, bool p_needs_resolve) override {}645virtual bool render_target_get_msaa_needs_resolve(RID p_render_target) const override { return false; }646virtual void render_target_do_msaa_resolve(RID p_render_target) override {}647virtual void render_target_set_use_hdr(RID p_render_target, bool p_use_hdr_2d) override;648virtual bool render_target_is_using_hdr(RID p_render_target) const override;649virtual void render_target_set_use_debanding(RID p_render_target, bool p_use_debanding) override {}650virtual bool render_target_is_using_debanding(RID p_render_target) const override { return false; }651652// new653void render_target_set_as_unused(RID p_render_target) override {654render_target_clear_used(p_render_target);655}656657GLuint render_target_get_color_internal_format(RID p_render_target) const;658GLuint render_target_get_color_format(RID p_render_target) const;659GLuint render_target_get_color_type(RID p_render_target) const;660uint32_t render_target_get_color_format_size(RID p_render_target) const;661662void render_target_request_clear(RID p_render_target, const Color &p_clear_color) override;663bool render_target_is_clear_requested(RID p_render_target) override;664Color render_target_get_clear_request_color(RID p_render_target) override;665void render_target_disable_clear_request(RID p_render_target) override;666void render_target_do_clear_request(RID p_render_target) override;667668GLuint render_target_get_fbo(RID p_render_target) const;669GLuint render_target_get_color(RID p_render_target) const;670GLuint render_target_get_depth(RID p_render_target) const;671bool render_target_get_depth_has_stencil(RID p_render_target) const;672void render_target_set_reattach_textures(RID p_render_target, bool p_reattach_textures) const;673bool render_target_is_reattach_textures(RID p_render_target) const;674675virtual void render_target_set_sdf_size_and_scale(RID p_render_target, RS::ViewportSDFOversize p_size, RS::ViewportSDFScale p_scale) override;676virtual Rect2i render_target_get_sdf_rect(RID p_render_target) const override;677GLuint render_target_get_sdf_texture(RID p_render_target);678GLuint render_target_get_sdf_framebuffer(RID p_render_target);679void render_target_sdf_process(RID p_render_target);680virtual void render_target_mark_sdf_enabled(RID p_render_target, bool p_enabled) override;681bool render_target_is_sdf_enabled(RID p_render_target) const;682683void render_target_copy_to_back_buffer(RID p_render_target, const Rect2i &p_region, bool p_gen_mipmaps);684void render_target_clear_back_buffer(RID p_render_target, const Rect2i &p_region, const Color &p_color);685void render_target_gen_back_buffer_mipmaps(RID p_render_target, const Rect2i &p_region);686687virtual void render_target_set_vrs_mode(RID p_render_target, RS::ViewportVRSMode p_mode) override {}688virtual RS::ViewportVRSMode render_target_get_vrs_mode(RID p_render_target) const override { return RS::VIEWPORT_VRS_DISABLED; }689virtual void render_target_set_vrs_update_mode(RID p_render_target, RS::ViewportVRSUpdateMode p_mode) override {}690virtual RS::ViewportVRSUpdateMode render_target_get_vrs_update_mode(RID p_render_target) const override { return RS::VIEWPORT_VRS_UPDATE_DISABLED; }691virtual void render_target_set_vrs_texture(RID p_render_target, RID p_texture) override {}692virtual RID render_target_get_vrs_texture(RID p_render_target) const override { return RID(); }693694virtual void 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) override;695virtual RID render_target_get_override_color(RID p_render_target) const override;696virtual RID render_target_get_override_depth(RID p_render_target) const override;697virtual RID render_target_get_override_velocity(RID p_render_target) const override;698virtual RID render_target_get_override_velocity_depth(RID p_render_target) const override { return RID(); }699700virtual void render_target_set_render_region(RID p_render_target, const Rect2i &p_render_region) override;701virtual Rect2i render_target_get_render_region(RID p_render_target) const override;702703virtual RID render_target_get_texture(RID p_render_target) override;704705virtual void render_target_set_velocity_target_size(RID p_render_target, const Size2i &p_target_size) override {}706virtual Size2i render_target_get_velocity_target_size(RID p_render_target) const override { return Size2i(); }707708void bind_framebuffer(GLuint framebuffer) {709glBindFramebuffer(GL_FRAMEBUFFER, framebuffer);710}711712void bind_framebuffer_system() {713glBindFramebuffer(GL_FRAMEBUFFER, GLES3::TextureStorage::system_fbo);714}715716String get_framebuffer_error(GLenum p_status);717};718719inline String TextureStorage::get_framebuffer_error(GLenum p_status) {720#if defined(DEBUG_ENABLED) && defined(GL_API_ENABLED)721if (p_status == GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT) {722return "GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT";723} else if (p_status == GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT) {724return "GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT";725} else if (p_status == GL_FRAMEBUFFER_INCOMPLETE_DRAW_BUFFER) {726return "GL_FRAMEBUFFER_INCOMPLETE_DRAW_BUFFER";727} else if (p_status == GL_FRAMEBUFFER_INCOMPLETE_READ_BUFFER) {728return "GL_FRAMEBUFFER_INCOMPLETE_READ_BUFFER";729}730#endif731return itos(p_status);732}733734} // namespace GLES3735736#endif // GLES3_ENABLED737738739