From c766d9b9dc5661693a58e01a3637f15197c2fe59 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Foucault?= Date: Sat, 5 Sep 2020 17:29:51 +0200 Subject: GPUTexture: GL Backend Isolation This is a massive cleanup needed for vulkan support T68990. It provides: - More meaningful enums with conversion functions. - Less hacky supports of arrays and cubemaps (all considered layered). - More inline with the stateless design of vulkan and modern GL. - Methods Fallbacks are using framebuffer functions that are wrapped instead of implementing inside the texture module. What is not in there: - API change. - Samplers support (breaks a few effects). # Conflicts: # source/blender/gpu/GPU_texture.h --- source/blender/gpu/GPU_texture.h | 14 +- source/blender/gpu/intern/gpu_state_private.hh | 6 + source/blender/gpu/intern/gpu_texture.cc | 2344 ++++------------------ source/blender/gpu/intern/gpu_texture_private.hh | 393 +++- source/blender/gpu/opengl/gl_context.hh | 8 + source/blender/gpu/opengl/gl_debug.cc | 5 + source/blender/gpu/opengl/gl_debug.hh | 1 + source/blender/gpu/opengl/gl_state.cc | 87 +- source/blender/gpu/opengl/gl_state.hh | 22 + source/blender/gpu/opengl/gl_texture.cc | 357 +++- source/blender/gpu/opengl/gl_texture.hh | 218 +- 11 files changed, 1396 insertions(+), 2059 deletions(-) diff --git a/source/blender/gpu/GPU_texture.h b/source/blender/gpu/GPU_texture.h index 8cc5936fac9..e56866c5259 100644 --- a/source/blender/gpu/GPU_texture.h +++ b/source/blender/gpu/GPU_texture.h @@ -44,6 +44,7 @@ typedef struct GPUTexture GPUTexture; * - All states are created at startup to avoid runtime costs. */ typedef enum eGPUSamplerState { + GPU_SAMPLER_DEFAULT = 0, GPU_SAMPLER_FILTER = (1 << 0), GPU_SAMPLER_MIPMAP = (1 << 1), GPU_SAMPLER_REPEAT_S = (1 << 2), @@ -52,8 +53,11 @@ typedef enum eGPUSamplerState { GPU_SAMPLER_CLAMP_BORDER = (1 << 5), /* Clamp to border color instead of border texel. */ GPU_SAMPLER_COMPARE = (1 << 6), GPU_SAMPLER_ANISO = (1 << 7), + GPU_SAMPLER_ICON = (1 << 8), + + GPU_SAMPLER_REPEAT = (GPU_SAMPLER_REPEAT_S | GPU_SAMPLER_REPEAT_T | GPU_SAMPLER_REPEAT_R), /* Don't use that. */ - GPU_SAMPLER_MAX = (1 << 8), + GPU_SAMPLER_MAX = (GPU_SAMPLER_ICON + 1), } eGPUSamplerState; ENUM_OPERATORS(eGPUSamplerState) @@ -209,12 +213,6 @@ GPUTexture *GPU_texture_create_1d_array( int w, int h, eGPUTextureFormat tex_format, const float *pixels, char err_out[256]); GPUTexture *GPU_texture_create_2d( int w, int h, eGPUTextureFormat tex_format, const float *pixels, char err_out[256]); -GPUTexture *GPU_texture_create_2d_multisample(int w, - int h, - eGPUTextureFormat tex_format, - const float *pixels, - int samples, - char err_out[256]); GPUTexture *GPU_texture_create_2d_array( int w, int h, int d, eGPUTextureFormat tex_format, const float *pixels, char err_out[256]); GPUTexture *GPU_texture_create_3d( @@ -227,7 +225,6 @@ GPUTexture *GPU_texture_create_cube_array( int w, int d, eGPUTextureFormat tex_format, const float *pixels, char err_out[256]); GPUTexture *GPU_texture_create_from_vertbuf(struct GPUVertBuf *vert); -GPUTexture *GPU_texture_create_buffer(eGPUTextureFormat tex_format, const uint buffer); GPUTexture *GPU_texture_create_compressed( int w, int h, int miplen, eGPUTextureFormat format, const void *data); @@ -286,7 +283,6 @@ int GPU_texture_height(const GPUTexture *tex); int GPU_texture_orig_width(const GPUTexture *tex); int GPU_texture_orig_height(const GPUTexture *tex); void GPU_texture_orig_size_set(GPUTexture *tex, int w, int h); -int GPU_texture_layers(const GPUTexture *tex); eGPUTextureFormat GPU_texture_format(const GPUTexture *tex); int GPU_texture_samples(const GPUTexture *tex); bool GPU_texture_array(const GPUTexture *tex); diff --git a/source/blender/gpu/intern/gpu_state_private.hh b/source/blender/gpu/intern/gpu_state_private.hh index 61234c4612c..6ce240df108 100644 --- a/source/blender/gpu/intern/gpu_state_private.hh +++ b/source/blender/gpu/intern/gpu_state_private.hh @@ -26,6 +26,8 @@ #include "GPU_state.h" +#include "gpu_texture_private.hh" + #include namespace blender { @@ -160,6 +162,10 @@ class GPUStateManager { virtual ~GPUStateManager(){}; virtual void apply_state(void) = 0; + + virtual void texture_bind(Texture *tex, eGPUSamplerState sampler, int unit) = 0; + virtual void texture_unbind(Texture *tex) = 0; + virtual void texture_unbind_all(void) = 0; }; } // namespace gpu diff --git a/source/blender/gpu/intern/gpu_texture.cc b/source/blender/gpu/intern/gpu_texture.cc index ced9ea5a498..9d431ef4648 100644 --- a/source/blender/gpu/intern/gpu_texture.cc +++ b/source/blender/gpu/intern/gpu_texture.cc @@ -21,28 +21,12 @@ * \ingroup gpu */ -#include +#include "BLI_string.h" -#include "MEM_guardedalloc.h" - -#include "DNA_image_types.h" -#include "DNA_userdef_types.h" - -#include "BLI_blenlib.h" -#include "BLI_math_base.h" -#include "BLI_utildefines.h" - -#include "BKE_global.h" - -#include "GPU_batch.h" -#include "GPU_context.h" -#include "GPU_debug.h" -#include "GPU_extensions.h" #include "GPU_framebuffer.h" -#include "GPU_glew.h" -#include "GPU_platform.h" #include "GPU_texture.h" +#include "gpu_backend.hh" #include "gpu_context_private.hh" #include "gpu_framebuffer_private.hh" @@ -62,1478 +46,380 @@ Texture::Texture(const char *name) else { name_[0] = '\0'; } -} - -Texture::~Texture() -{ -} - -/** \} */ - -/* -------------------------------------------------------------------- */ -/** \name Operation - * \{ */ - -void Texture::attach_to(FrameBuffer *) -{ -} - -/** \} */ - -} // namespace blender::gpu - -/* -------------------------------------------------------------------- */ -/** \name C-API - * \{ */ - -using namespace blender::gpu; - -static struct GPUTextureGlobal { - /** Texture used in place of invalid textures (not loaded correctly, missing). */ - GPUTexture *invalid_tex_1D; - GPUTexture *invalid_tex_2D; - GPUTexture *invalid_tex_3D; - /** Sampler objects used to replace internal texture parameters. */ - GLuint samplers[GPU_SAMPLER_MAX]; - GLuint icon_sampler; -} GG = {NULL}; - -/* Maximum number of FBOs a texture can be attached to. */ -#define GPU_TEX_MAX_FBO_ATTACHED 12 - -typedef enum eGPUTextureFormatFlag { - GPU_FORMAT_DEPTH = (1 << 0), - GPU_FORMAT_STENCIL = (1 << 1), - GPU_FORMAT_INTEGER = (1 << 2), - GPU_FORMAT_FLOAT = (1 << 3), - - GPU_FORMAT_1D = (1 << 10), - GPU_FORMAT_2D = (1 << 11), - GPU_FORMAT_3D = (1 << 12), - GPU_FORMAT_CUBE = (1 << 13), - GPU_FORMAT_ARRAY = (1 << 14), -} eGPUTextureFormatFlag; - -ENUM_OPERATORS(eGPUTextureFormatFlag) - -/* GPUTexture */ -struct GPUTexture { - int w, h, d; /* width/height/depth */ - int orig_w, orig_h; /* width/height (of source data), optional. */ - int number; /* Texture unit to which this texture is bound. */ - int refcount; /* reference count */ - GLenum target; /* GL_TEXTURE_* */ - GLenum target_base; /* same as target, (but no multisample) - * use it for unbinding */ - GLuint bindcode; /* opengl identifier for texture */ - - eGPUTextureFormat format; - eGPUTextureFormatFlag format_flag; - eGPUSamplerState sampler_state; /* Internal Sampler state. */ - - int mipmaps; /* number of mipmaps */ - int components; /* number of color/alpha channels */ - int samples; /* number of samples for multisamples textures. 0 if not multisample target */ - - int fb_attachment[GPU_TEX_MAX_FBO_ATTACHED]; - GPUFrameBuffer *fb[GPU_TEX_MAX_FBO_ATTACHED]; - /* Legacy workaround for texture copy. */ - GLuint copy_fb; - GPUContext *copy_fb_ctx; -}; -using namespace blender; -using namespace blender::gpu; - -static uint gpu_get_bytesize(eGPUTextureFormat data_type); -static void gpu_texture_framebuffer_ensure(GPUTexture *tex); - -/* ------ Memory Management ------- */ -/* Records every texture allocation / free - * to estimate the Texture Pool Memory consumption */ -static uint memory_usage = 0; - -static uint gpu_texture_memory_footprint_compute(GPUTexture *tex) -{ - uint memsize; - const uint bytesize = gpu_get_bytesize(tex->format); - const int samp = max_ii(tex->samples, 1); - switch (tex->target_base) { - case GL_TEXTURE_1D: - case GL_TEXTURE_BUFFER: - memsize = bytesize * tex->w * samp; - break; - case GL_TEXTURE_1D_ARRAY: - case GL_TEXTURE_2D: - memsize = bytesize * tex->w * tex->h * samp; - break; - case GL_TEXTURE_2D_ARRAY: - case GL_TEXTURE_3D: - memsize = bytesize * tex->w * tex->h * tex->d * samp; - break; - case GL_TEXTURE_CUBE_MAP: - memsize = bytesize * 6 * tex->w * tex->h * samp; - break; - case GL_TEXTURE_CUBE_MAP_ARRAY_ARB: - memsize = bytesize * 6 * tex->w * tex->h * tex->d * samp; - break; - default: - BLI_assert(0); - return 0; - } - if (tex->mipmaps != 0) { - /* Just to get an idea of the memory used here is computed - * as if the maximum number of mipmaps was generated. */ - memsize += memsize / 3; + for (int i = 0; i < GPU_TEX_MAX_FBO_ATTACHED; i++) { + fb[i] = NULL; } - - return memsize; } -static void gpu_texture_memory_footprint_add(GPUTexture *tex) -{ - memory_usage += gpu_texture_memory_footprint_compute(tex); -} - -static void gpu_texture_memory_footprint_remove(GPUTexture *tex) -{ - memory_usage -= gpu_texture_memory_footprint_compute(tex); -} - -uint GPU_texture_memory_usage_get(void) -{ - return memory_usage; -} - -/* -------------------------------- */ - -static const char *gl_enum_to_str(GLenum e) +Texture::~Texture() { -#define ENUM_TO_STRING(e) \ - case GL_##e: { \ - return STRINGIFY_ARG(e); \ + for (int i = 0; i < GPU_TEX_MAX_FBO_ATTACHED; i++) { + if (fb[i] != NULL) { + FrameBuffer *framebuffer = reinterpret_cast(fb[i]); + framebuffer->attachment_set((GPUAttachmentType)fb_attachment[i], GPU_ATTACHMENT_NONE); + } } - - switch (e) { - ENUM_TO_STRING(TEXTURE_CUBE_MAP); - ENUM_TO_STRING(TEXTURE_CUBE_MAP_ARRAY); - ENUM_TO_STRING(TEXTURE_2D); - ENUM_TO_STRING(TEXTURE_2D_ARRAY); - ENUM_TO_STRING(TEXTURE_1D); - ENUM_TO_STRING(TEXTURE_1D_ARRAY); - ENUM_TO_STRING(TEXTURE_3D); - ENUM_TO_STRING(TEXTURE_2D_MULTISAMPLE); - ENUM_TO_STRING(RGBA32F); - ENUM_TO_STRING(RGBA16F); - ENUM_TO_STRING(RGBA16UI); - ENUM_TO_STRING(RGBA16I); - ENUM_TO_STRING(RGBA16); - ENUM_TO_STRING(RGBA8UI); - ENUM_TO_STRING(RGBA8I); - ENUM_TO_STRING(RGBA8); - ENUM_TO_STRING(RGB16F); - ENUM_TO_STRING(RG32F); - ENUM_TO_STRING(RG16F); - ENUM_TO_STRING(RG16UI); - ENUM_TO_STRING(RG16I); - ENUM_TO_STRING(RG16); - ENUM_TO_STRING(RG8UI); - ENUM_TO_STRING(RG8I); - ENUM_TO_STRING(RG8); - ENUM_TO_STRING(R8UI); - ENUM_TO_STRING(R8I); - ENUM_TO_STRING(R8); - ENUM_TO_STRING(R32F); - ENUM_TO_STRING(R32UI); - ENUM_TO_STRING(R32I); - ENUM_TO_STRING(R16F); - ENUM_TO_STRING(R16UI); - ENUM_TO_STRING(R16I); - ENUM_TO_STRING(R16); - ENUM_TO_STRING(R11F_G11F_B10F); - ENUM_TO_STRING(SRGB8_ALPHA8); - ENUM_TO_STRING(DEPTH24_STENCIL8); - ENUM_TO_STRING(DEPTH32F_STENCIL8); - ENUM_TO_STRING(DEPTH_COMPONENT32F); - ENUM_TO_STRING(DEPTH_COMPONENT24); - ENUM_TO_STRING(DEPTH_COMPONENT16); - default: - return "Unkown enum"; - }; -#undef ENUM_TO_STRING } -static GLenum gl_enum_target_to_binding(GLenum target) +bool Texture::init_1D(int w, int layers, eGPUTextureFormat format) { - switch (target) { - default: - BLI_assert(0); - ATTR_FALLTHROUGH; - case GL_TEXTURE_1D: - return GL_TEXTURE_BINDING_1D; - case GL_TEXTURE_1D_ARRAY: - return GL_TEXTURE_BINDING_1D_ARRAY; - case GL_TEXTURE_2D: - return GL_TEXTURE_BINDING_2D; - case GL_TEXTURE_2D_ARRAY: - return GL_TEXTURE_BINDING_2D_ARRAY; - case GL_TEXTURE_2D_MULTISAMPLE: - return GL_TEXTURE_BINDING_2D_MULTISAMPLE; - case GL_TEXTURE_3D: - return GL_TEXTURE_BINDING_3D; - case GL_TEXTURE_BUFFER: - return GL_TEXTURE_BINDING_BUFFER; - case GL_TEXTURE_CUBE_MAP: - return GL_TEXTURE_BINDING_CUBE_MAP; - case GL_TEXTURE_CUBE_MAP_ARRAY_ARB: - return GL_TEXTURE_BINDING_CUBE_MAP_ARRAY_ARB; - } -} - -static int gpu_get_component_count(eGPUTextureFormat format) -{ - switch (format) { - case GPU_RGBA8: - case GPU_RGBA8UI: - case GPU_RGBA16F: - case GPU_RGBA16: - case GPU_RGBA32F: - case GPU_SRGB8_A8: - return 4; - case GPU_RGB16F: - case GPU_R11F_G11F_B10F: - return 3; - case GPU_RG8: - case GPU_RG16: - case GPU_RG16F: - case GPU_RG16I: - case GPU_RG16UI: - case GPU_RG32F: - return 2; - default: - return 1; + w_ = w; + h_ = layers; + d_ = 0; + format_ = format; + format_flag_ = to_format_flag(format); + type_ = (layers > 0) ? GPU_TEXTURE_1D_ARRAY : GPU_TEXTURE_1D; + if ((format_flag_ & (GPU_FORMAT_DEPTH_STENCIL | GPU_FORMAT_INTEGER)) == 0) { + sampler_state = GPU_SAMPLER_FILTER; } + return this->init_internal(); } -static uint gpu_get_data_format_bytesize(int comp, eGPUDataFormat data_format) +bool Texture::init_2D(int w, int h, int layers, eGPUTextureFormat format) { - switch (data_format) { - case GPU_DATA_FLOAT: - return sizeof(float) * comp; - case GPU_DATA_INT: - case GPU_DATA_UNSIGNED_INT: - return sizeof(int) * comp; - case GPU_DATA_UNSIGNED_INT_24_8: - case GPU_DATA_10_11_11_REV: - return sizeof(int); - case GPU_DATA_UNSIGNED_BYTE: - return sizeof(char) * comp; - default: - BLI_assert(0); - return 0; + w_ = w; + h_ = h; + d_ = layers; + format_ = format; + format_flag_ = to_format_flag(format); + type_ = (layers > 0) ? GPU_TEXTURE_2D_ARRAY : GPU_TEXTURE_2D; + if ((format_flag_ & (GPU_FORMAT_DEPTH_STENCIL | GPU_FORMAT_INTEGER)) == 0) { + sampler_state = GPU_SAMPLER_FILTER; } + return this->init_internal(); } -/* Definitely not complete, edit according to the gl specification. */ -static void gpu_validate_data_format(eGPUTextureFormat tex_format, eGPUDataFormat data_format) +bool Texture::init_3D(int w, int h, int d, eGPUTextureFormat format) { - (void)data_format; - - if (ELEM(tex_format, GPU_DEPTH_COMPONENT24, GPU_DEPTH_COMPONENT16, GPU_DEPTH_COMPONENT32F)) { - BLI_assert(data_format == GPU_DATA_FLOAT); - } - else if (ELEM(tex_format, GPU_DEPTH24_STENCIL8, GPU_DEPTH32F_STENCIL8)) { - BLI_assert(data_format == GPU_DATA_UNSIGNED_INT_24_8); - } - else { - /* Integer formats */ - if (ELEM(tex_format, GPU_RG16I, GPU_R16I, GPU_RG16UI, GPU_R16UI, GPU_R8UI, GPU_R32UI)) { - if (ELEM(tex_format, GPU_R8UI, GPU_R16UI, GPU_RG16UI, GPU_R32UI)) { - BLI_assert(data_format == GPU_DATA_UNSIGNED_INT); - } - else { - BLI_assert(data_format == GPU_DATA_INT); - } - } - /* Byte formats */ - else if (ELEM(tex_format, GPU_R8, GPU_RG8, GPU_RGBA8, GPU_RGBA8UI, GPU_SRGB8_A8)) { - BLI_assert(ELEM(data_format, GPU_DATA_UNSIGNED_BYTE, GPU_DATA_FLOAT)); - } - /* Special case */ - else if (ELEM(tex_format, GPU_R11F_G11F_B10F)) { - BLI_assert(ELEM(data_format, GPU_DATA_10_11_11_REV, GPU_DATA_FLOAT)); - } - /* Float formats */ - else { - BLI_assert(ELEM(data_format, GPU_DATA_FLOAT)); - } + w_ = w; + h_ = h; + d_ = d; + format_ = format; + format_flag_ = to_format_flag(format); + type_ = GPU_TEXTURE_3D; + if ((format_flag_ & (GPU_FORMAT_DEPTH_STENCIL | GPU_FORMAT_INTEGER)) == 0) { + sampler_state = GPU_SAMPLER_FILTER; } + return this->init_internal(); } -static eGPUDataFormat gpu_get_data_format_from_tex_format(eGPUTextureFormat tex_format) +bool Texture::init_cubemap(int w, int layers, eGPUTextureFormat format) { - if (ELEM(tex_format, GPU_DEPTH_COMPONENT24, GPU_DEPTH_COMPONENT16, GPU_DEPTH_COMPONENT32F)) { - return GPU_DATA_FLOAT; - } - if (ELEM(tex_format, GPU_DEPTH24_STENCIL8, GPU_DEPTH32F_STENCIL8)) { - return GPU_DATA_UNSIGNED_INT_24_8; - } - - /* Integer formats */ - if (ELEM(tex_format, GPU_RG16I, GPU_R16I, GPU_RG16UI, GPU_R8UI, GPU_R16UI, GPU_R32UI)) { - if (ELEM(tex_format, GPU_R8UI, GPU_R16UI, GPU_RG16UI, GPU_R32UI)) { - return GPU_DATA_UNSIGNED_INT; - } - - return GPU_DATA_INT; - } - /* Byte formats */ - if (ELEM(tex_format, GPU_R8)) { - return GPU_DATA_UNSIGNED_BYTE; + w_ = w; + h_ = w; + d_ = max_ii(1, layers) * 6; + format_ = format; + format_flag_ = to_format_flag(format); + type_ = (layers > 0) ? GPU_TEXTURE_CUBE_ARRAY : GPU_TEXTURE_CUBE; + if ((format_flag_ & (GPU_FORMAT_DEPTH_STENCIL | GPU_FORMAT_INTEGER)) == 0) { + sampler_state = GPU_SAMPLER_FILTER; } - /* Special case */ - if (ELEM(tex_format, GPU_R11F_G11F_B10F)) { - return GPU_DATA_10_11_11_REV; - } - - return GPU_DATA_FLOAT; + return this->init_internal(); } -/* Definitely not complete, edit according to the gl specification. */ -static GLenum gpu_get_gl_dataformat(eGPUTextureFormat data_type, - eGPUTextureFormatFlag *format_flag) +bool Texture::init_buffer(GPUVertBuf *vbo, eGPUTextureFormat format) { - if (ELEM(data_type, GPU_DEPTH_COMPONENT24, GPU_DEPTH_COMPONENT16, GPU_DEPTH_COMPONENT32F)) { - *format_flag |= GPU_FORMAT_DEPTH; - return GL_DEPTH_COMPONENT; - } - if (ELEM(data_type, GPU_DEPTH24_STENCIL8, GPU_DEPTH32F_STENCIL8)) { - *format_flag |= GPU_FORMAT_DEPTH | GPU_FORMAT_STENCIL; - return GL_DEPTH_STENCIL; - } - - /* Integer formats */ - if (ELEM(data_type, GPU_R8UI, GPU_RG16I, GPU_R16I, GPU_RG16UI, GPU_R16UI, GPU_R32UI)) { - *format_flag |= GPU_FORMAT_INTEGER; - - switch (gpu_get_component_count(data_type)) { - case 1: - return GL_RED_INTEGER; - break; - case 2: - return GL_RG_INTEGER; - break; - case 3: - return GL_RGB_INTEGER; - break; - case 4: - return GL_RGBA_INTEGER; - break; - } - } - else if (ELEM(data_type, GPU_R8)) { - *format_flag |= GPU_FORMAT_FLOAT; - return GL_RED; - } - else { - *format_flag |= GPU_FORMAT_FLOAT; - - switch (gpu_get_component_count(data_type)) { - case 1: - return GL_RED; - break; - case 2: - return GL_RG; - break; - case 3: - return GL_RGB; - break; - case 4: - return GL_RGBA; - break; - } - } - - BLI_assert(0); - *format_flag |= GPU_FORMAT_FLOAT; - return GL_RGBA; -} - -static uint gpu_get_bytesize(eGPUTextureFormat data_type) -{ - switch (data_type) { - case GPU_RGBA32F: - return 32; - case GPU_RG32F: - case GPU_RGBA16F: - case GPU_RGBA16: - return 16; - case GPU_RGB16F: - return 12; - case GPU_DEPTH32F_STENCIL8: /* 32-bit depth, 8 bits stencil, and 24 unused bits. */ - return 8; - case GPU_RG16F: - case GPU_RG16I: - case GPU_RG16UI: - case GPU_RG16: - case GPU_DEPTH24_STENCIL8: - case GPU_DEPTH_COMPONENT32F: - case GPU_RGBA8UI: - case GPU_RGBA8: - case GPU_SRGB8_A8: - case GPU_R11F_G11F_B10F: - case GPU_R32F: - case GPU_R32UI: - case GPU_R32I: - return 4; - case GPU_DEPTH_COMPONENT24: - return 3; - case GPU_DEPTH_COMPONENT16: - case GPU_R16F: - case GPU_R16UI: - case GPU_R16I: - case GPU_RG8: - case GPU_R16: - return 2; - case GPU_R8: - case GPU_R8UI: - return 1; - case GPU_SRGB8_A8_DXT1: - case GPU_SRGB8_A8_DXT3: - case GPU_SRGB8_A8_DXT5: - case GPU_RGBA8_DXT1: - case GPU_RGBA8_DXT3: - case GPU_RGBA8_DXT5: - return 1; /* Incorrect but actual size is fractional. */ - default: - BLI_assert(!"Texture format incorrect or unsupported\n"); - return 0; - } -} - -static GLenum gpu_format_to_gl_internalformat(eGPUTextureFormat format) -{ - /* You can add any of the available type to this list - * For available types see GPU_texture.h */ - switch (format) { - /* Formats texture & renderbuffer */ - case GPU_RGBA8UI: - return GL_RGBA8UI; - case GPU_RGBA8I: - return GL_RGBA8I; - case GPU_RGBA8: - return GL_RGBA8; - case GPU_RGBA32UI: - return GL_RGBA32UI; - case GPU_RGBA32I: - return GL_RGBA32I; - case GPU_RGBA32F: - return GL_RGBA32F; - case GPU_RGBA16UI: - return GL_RGBA16UI; - case GPU_RGBA16I: - return GL_RGBA16I; - case GPU_RGBA16F: - return GL_RGBA16F; - case GPU_RGBA16: - return GL_RGBA16; - case GPU_RG8UI: - return GL_RG8UI; - case GPU_RG8I: - return GL_RG8I; - case GPU_RG8: - return GL_RG8; - case GPU_RG32UI: - return GL_RG32UI; - case GPU_RG32I: - return GL_RG32I; - case GPU_RG32F: - return GL_RG32F; - case GPU_RG16UI: - return GL_RG16UI; - case GPU_RG16I: - return GL_RG16I; - case GPU_RG16F: - return GL_RG16F; - case GPU_RG16: - return GL_RG16; - case GPU_R8UI: - return GL_R8UI; - case GPU_R8I: - return GL_R8I; - case GPU_R8: - return GL_R8; - case GPU_R32UI: - return GL_R32UI; - case GPU_R32I: - return GL_R32I; - case GPU_R32F: - return GL_R32F; - case GPU_R16UI: - return GL_R16UI; - case GPU_R16I: - return GL_R16I; - case GPU_R16F: - return GL_R16F; - case GPU_R16: - return GL_R16; - /* Special formats texture & renderbuffer */ - case GPU_R11F_G11F_B10F: - return GL_R11F_G11F_B10F; - case GPU_DEPTH32F_STENCIL8: - return GL_DEPTH32F_STENCIL8; - case GPU_DEPTH24_STENCIL8: - return GL_DEPTH24_STENCIL8; - case GPU_SRGB8_A8: - return GL_SRGB8_ALPHA8; - /* Texture only format */ - case GPU_RGB16F: - return GL_RGB16F; - /* Special formats texture only */ - case GPU_SRGB8_A8_DXT1: - return GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT1_EXT; - case GPU_SRGB8_A8_DXT3: - return GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT3_EXT; - case GPU_SRGB8_A8_DXT5: - return GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT5_EXT; - case GPU_RGBA8_DXT1: - return GL_COMPRESSED_RGBA_S3TC_DXT1_EXT; - case GPU_RGBA8_DXT3: - return GL_COMPRESSED_RGBA_S3TC_DXT3_EXT; - case GPU_RGBA8_DXT5: - return GL_COMPRESSED_RGBA_S3TC_DXT5_EXT; - /* Depth Formats */ - case GPU_DEPTH_COMPONENT32F: - return GL_DEPTH_COMPONENT32F; - case GPU_DEPTH_COMPONENT24: - return GL_DEPTH_COMPONENT24; - case GPU_DEPTH_COMPONENT16: - return GL_DEPTH_COMPONENT16; - default: - BLI_assert(!"Texture format incorrect or unsupported\n"); - return 0; + /* See to_texture_format(). */ + if (format == GPU_DEPTH_COMPONENT24) { + return false; } + w_ = vbo->vertex_len; + h_ = 0; + d_ = 0; + format_ = format; + format_flag_ = to_format_flag(format); + type_ = GPU_TEXTURE_BUFFER; + return this->init_internal(vbo); } -static GLenum gpu_get_gl_datatype(eGPUDataFormat format) -{ - switch (format) { - case GPU_DATA_FLOAT: - return GL_FLOAT; - case GPU_DATA_INT: - return GL_INT; - case GPU_DATA_UNSIGNED_INT: - return GL_UNSIGNED_INT; - case GPU_DATA_UNSIGNED_BYTE: - return GL_UNSIGNED_BYTE; - case GPU_DATA_UNSIGNED_INT_24_8: - return GL_UNSIGNED_INT_24_8; - case GPU_DATA_10_11_11_REV: - return GL_UNSIGNED_INT_10F_11F_11F_REV; - default: - BLI_assert(!"Unhandled data format"); - return GL_FLOAT; - } -} +/** \} */ -static float *GPU_texture_rescale_3d( - GPUTexture *tex, int w, int h, int d, int channels, const float *fpixels) -{ - const uint xf = w / tex->w, yf = h / tex->h, zf = d / tex->d; - float *nfpixels = (float *)MEM_mallocN(channels * sizeof(float) * tex->w * tex->h * tex->d, - "GPUTexture Rescaled 3Dtex"); - - if (nfpixels) { - GPU_print_error_debug("You need to scale a 3D texture, feel the pain!"); - - for (uint k = 0; k < tex->d; k++) { - for (uint j = 0; j < tex->h; j++) { - for (uint i = 0; i < tex->w; i++) { - /* obviously doing nearest filtering here, - * it's going to be slow in any case, let's not make it worse */ - float xb = i * xf; - float yb = j * yf; - float zb = k * zf; - uint offset = k * (tex->w * tex->h) + i * tex->h + j; - uint offset_orig = (zb) * (w * h) + (xb)*h + (yb); - - if (channels == 4) { - nfpixels[offset * 4] = fpixels[offset_orig * 4]; - nfpixels[offset * 4 + 1] = fpixels[offset_orig * 4 + 1]; - nfpixels[offset * 4 + 2] = fpixels[offset_orig * 4 + 2]; - nfpixels[offset * 4 + 3] = fpixels[offset_orig * 4 + 3]; - } - else { - nfpixels[offset] = fpixels[offset_orig]; - } - } - } - } - } +/* -------------------------------------------------------------------- */ +/** \name Operation + * \{ */ - return nfpixels; +void Texture::attach_to(FrameBuffer *) +{ } -static bool gpu_texture_check_capacity( - GPUTexture *tex, GLenum proxy, GLenum internalformat, GLenum data_format, GLenum data_type) +void Texture::update(eGPUDataFormat format, const void *data) { - if (proxy == GL_PROXY_TEXTURE_CUBE_MAP_ARRAY_ARB && - GPU_type_matches(GPU_DEVICE_ANY, GPU_OS_MAC, GPU_DRIVER_ANY)) { - /* Special fix for T79703. */ - /* Depth has already been checked. */ - return tex->w <= GPU_max_cube_map_size(); - } + int mip = 0; + int extent[3], offset[3] = {0, 0, 0}; + this->mip_size_get(mip, extent); + this->update_sub(mip, offset, extent, format, data); +} - if (GPU_type_matches(GPU_DEVICE_ATI, GPU_OS_WIN, GPU_DRIVER_ANY) || - GPU_type_matches(GPU_DEVICE_NVIDIA, GPU_OS_MAC, GPU_DRIVER_OFFICIAL) || - GPU_type_matches(GPU_DEVICE_ATI, GPU_OS_UNIX, GPU_DRIVER_OFFICIAL)) { - /* Some AMD drivers have a faulty `GL_PROXY_TEXTURE_..` check. - * (see T55888, T56185, T59351). - * Checking with `GL_PROXY_TEXTURE_..` doesn't prevent `Out Of Memory` issue, - * it just states that the OGL implementation can support the texture. - * So manually check the maximum size and maximum number of layers. - * Same thing happens on Nvidia/macOS 10.15 (T78175). */ - switch (proxy) { - case GL_PROXY_TEXTURE_2D_ARRAY: - if ((tex->d < 0) || (tex->d > GPU_max_texture_layers())) { - return false; - } - break; - - case GL_PROXY_TEXTURE_1D_ARRAY: - if ((tex->h < 0) || (tex->h > GPU_max_texture_layers())) { - return false; - } - break; - } +/** \} */ - switch (proxy) { - case GL_PROXY_TEXTURE_3D: - if ((tex->d < 0) || (tex->d > GPU_max_texture_size())) { - return false; - } - ATTR_FALLTHROUGH; - - case GL_PROXY_TEXTURE_2D: - case GL_PROXY_TEXTURE_2D_ARRAY: - if ((tex->h < 0) || (tex->h > GPU_max_texture_size())) { - return false; - } - ATTR_FALLTHROUGH; - - case GL_PROXY_TEXTURE_1D: - case GL_PROXY_TEXTURE_1D_ARRAY: - if ((tex->w < 0) || (tex->w > GPU_max_texture_size())) { - return false; - } - ATTR_FALLTHROUGH; - default: - break; - } +} // namespace blender::gpu - return true; - } +/* -------------------------------------------------------------------- */ +/** \name C-API + * \{ */ - switch (proxy) { - case GL_PROXY_TEXTURE_1D: - glTexImage1D(proxy, 0, internalformat, tex->w, 0, data_format, data_type, NULL); - break; - case GL_PROXY_TEXTURE_1D_ARRAY: - case GL_PROXY_TEXTURE_2D: - case GL_PROXY_TEXTURE_CUBE_MAP: - glTexImage2D(proxy, 0, internalformat, tex->w, tex->h, 0, data_format, data_type, NULL); - break; - case GL_PROXY_TEXTURE_2D_ARRAY: - case GL_PROXY_TEXTURE_3D: - glTexImage3D( - proxy, 0, internalformat, tex->w, tex->h, tex->d, 0, data_format, data_type, NULL); - break; - case GL_PROXY_TEXTURE_CUBE_MAP_ARRAY_ARB: - glTexImage3D( - proxy, 0, internalformat, tex->w, tex->h, tex->d * 6, 0, data_format, data_type, NULL); - break; - } - int width = 0; - glGetTexLevelParameteriv(proxy, 0, GL_TEXTURE_WIDTH, &width); - - return (width > 0); -} - -/* This tries to allocate video memory for a given texture - * If alloc fails, lower the resolution until it fits. */ -static bool gpu_texture_try_alloc(GPUTexture *tex, - GLenum proxy, - GLenum internalformat, - GLenum data_format, - GLenum data_type, - int channels, - bool try_rescale, - const float *fpixels, - float **rescaled_fpixels) -{ - bool ret; - ret = gpu_texture_check_capacity(tex, proxy, internalformat, data_format, data_type); - - if (!ret && try_rescale) { - BLI_assert(!ELEM(proxy, - GL_PROXY_TEXTURE_1D_ARRAY, - GL_PROXY_TEXTURE_2D_ARRAY, - GL_PROXY_TEXTURE_CUBE_MAP, - GL_PROXY_TEXTURE_CUBE_MAP_ARRAY_ARB)); // not implemented - - const int w = tex->w, h = tex->h, d = tex->d; - - /* Find largest texture possible */ - do { - tex->w /= 2; - tex->h /= 2; - tex->d /= 2; - - /* really unlikely to happen but keep this just in case */ - if (tex->w == 0) { - break; - } - if (tex->h == 0 && proxy != GL_PROXY_TEXTURE_1D) { - break; - } - if (tex->d == 0 && proxy == GL_PROXY_TEXTURE_3D) { - break; - } +using namespace blender; +using namespace blender::gpu; - ret = gpu_texture_check_capacity(tex, proxy, internalformat, data_format, data_type); - } while (ret == false); - - /* Rescale */ - if (ret) { - switch (proxy) { - case GL_PROXY_TEXTURE_1D: - case GL_PROXY_TEXTURE_2D: - /* Do nothing for now */ - return false; - case GL_PROXY_TEXTURE_3D: - BLI_assert(data_type == GL_FLOAT); - *rescaled_fpixels = GPU_texture_rescale_3d(tex, w, h, d, channels, fpixels); - return (bool)*rescaled_fpixels; - } - } - } +/* ------ Memory Management ------- */ - return ret; +uint GPU_texture_memory_usage_get(void) +{ + /* TODO(fclem) Do that inside the new Texture class. */ + return 0; } +/* -------------------------------- */ + GPUTexture *GPU_texture_create_nD(int w, int h, int d, int n, const void *pixels, eGPUTextureFormat tex_format, - eGPUDataFormat gpu_data_format, - int samples, - const bool can_rescale, - char err_out[256]) -{ - if (samples) { - CLAMP_MAX(samples, GPU_max_color_texture_samples()); - } - - if ((tex_format == GPU_DEPTH24_STENCIL8) && GPU_depth_blitting_workaround()) { - /* MacOS + Radeon Pro fails to blit depth on GPU_DEPTH24_STENCIL8 - * but works on GPU_DEPTH32F_STENCIL8. */ - tex_format = GPU_DEPTH32F_STENCIL8; - } - - GPUTexture *tex = (GPUTexture *)MEM_callocN(sizeof(GPUTexture), __func__); - tex->w = w; - tex->h = h; - tex->d = d; - tex->samples = samples; - tex->refcount = 1; - tex->format = tex_format; - tex->components = gpu_get_component_count(tex_format); - tex->mipmaps = 0; - tex->format_flag = static_cast(0); - tex->number = -1; - - if (n == 2) { - if (d == 0) { - tex->target_base = tex->target = GL_TEXTURE_2D; - } - else { - tex->target_base = tex->target = GL_TEXTURE_2D_ARRAY; - tex->format_flag |= GPU_FORMAT_ARRAY; - } - } - else if (n == 1) { - if (h == 0) { - tex->target_base = tex->target = GL_TEXTURE_1D; - } - else { - tex->target_base = tex->target = GL_TEXTURE_1D_ARRAY; - tex->format_flag |= GPU_FORMAT_ARRAY; - } - } - else if (n == 3) { - tex->target_base = tex->target = GL_TEXTURE_3D; - } - else { - /* should never happen */ - MEM_freeN(tex); - return NULL; - } - - gpu_validate_data_format(tex_format, gpu_data_format); - - if (samples && n == 2 && d == 0) { - tex->target = GL_TEXTURE_2D_MULTISAMPLE; - } - - GLenum internalformat = gpu_format_to_gl_internalformat(tex_format); - GLenum data_format = gpu_get_gl_dataformat(tex_format, &tex->format_flag); - GLenum data_type = gpu_get_gl_datatype(gpu_data_format); - - /* Generate Texture object */ - tex->bindcode = GPU_tex_alloc(); - - if (!tex->bindcode) { - if (err_out) { - BLI_strncpy(err_out, "GPUTexture: texture create failed\n", 256); - } - else { - fprintf(stderr, "GPUTexture: texture create failed\n"); - } - GPU_texture_free(tex); - return NULL; - } - - glBindTexture(tex->target, tex->bindcode); - - /* Check if texture fit in VRAM */ - GLenum proxy = GL_PROXY_TEXTURE_2D; - - if (n == 2) { - if (d > 1) { - proxy = GL_PROXY_TEXTURE_2D_ARRAY; - } - } - else if (n == 1) { - if (h == 0) { - proxy = GL_PROXY_TEXTURE_1D; - } - else { - proxy = GL_PROXY_TEXTURE_1D_ARRAY; - } - } - else if (n == 3) { - proxy = GL_PROXY_TEXTURE_3D; + eGPUDataFormat data_format, + int UNUSED(samples), + const bool UNUSED(can_rescale), + char UNUSED(err_out[256])) +{ + Texture *tex = GPUBackend::get()->texture_alloc("nD"); + bool success = false; + switch (n) { + case 1: + success = tex->init_1D(w, h, tex_format); + break; + case 2: + success = tex->init_2D(w, h, d, tex_format); + break; + case 3: + success = tex->init_3D(w, h, d, tex_format); + break; + default: + break; } - float *rescaled_pixels = NULL; - bool valid = gpu_texture_try_alloc(tex, - proxy, - internalformat, - data_format, - data_type, - tex->components, - can_rescale, - (float *)pixels, - &rescaled_pixels); - - if (G.debug & G_DEBUG_GPU || !valid) { - printf( - "GPUTexture: create : %s,\t w : %5d, h : %5d, d : %5d, comp : %4d, size : %.2f " - "MiB,\t %s\n", - gl_enum_to_str(tex->target), - w, - h, - d, - tex->components, - gpu_texture_memory_footprint_compute(tex) / 1048576.0f, - gl_enum_to_str(internalformat)); +#if 0 /* TODO */ + if (can_rescale && n == 3) { + /* Search small enough supported texture on the system. */ + /* Rescale input. */ } +#endif - if (!valid) { - if (err_out) { - BLI_strncpy(err_out, "GPUTexture: texture alloc failed\n", 256); - } - else { - fprintf(stderr, "GPUTexture: texture alloc failed. Likely not enough Video Memory.\n"); - fprintf(stderr, - "Current texture memory usage : %.2f MiB.\n", - gpu_texture_memory_footprint_compute(tex) / 1048576.0f); - } - GPU_texture_free(tex); + if (!success) { + delete tex; return NULL; } - - gpu_texture_memory_footprint_add(tex); - - /* Upload Texture */ - const void *pix = (rescaled_pixels) ? rescaled_pixels : pixels; - - if (tex->target == GL_TEXTURE_2D || tex->target == GL_TEXTURE_2D_MULTISAMPLE || - tex->target == GL_TEXTURE_1D_ARRAY) { - if (samples) { - glTexImage2DMultisample(tex->target, samples, internalformat, tex->w, tex->h, true); - if (pix) { - glTexSubImage2D(tex->target, 0, 0, 0, tex->w, tex->h, data_format, data_type, pix); - } - } - else { - glTexImage2D(tex->target, 0, internalformat, tex->w, tex->h, 0, data_format, data_type, pix); - } + if (pixels) { + tex->update(data_format, pixels); } - else if (tex->target == GL_TEXTURE_1D) { - glTexImage1D(tex->target, 0, internalformat, tex->w, 0, data_format, data_type, pix); - } - else { - glTexImage3D( - tex->target, 0, internalformat, tex->w, tex->h, tex->d, 0, data_format, data_type, pix); - } - - if (rescaled_pixels) { - MEM_freeN(rescaled_pixels); - } - - /* Texture Parameters */ - if (GPU_texture_stencil(tex) || /* Does not support filtering */ - GPU_texture_integer(tex) || /* Does not support filtering */ - GPU_texture_depth(tex)) { - tex->sampler_state = GPU_SAMPLER_DEFAULT & ~GPU_SAMPLER_FILTER; - } - else { - tex->sampler_state = GPU_SAMPLER_DEFAULT; - } - /* Avoid issue with incomplete textures. */ - glTexParameteri(tex->target_base, GL_TEXTURE_MIN_FILTER, GL_NEAREST); - - glBindTexture(tex->target, 0); - - return tex; + return reinterpret_cast(tex); } GPUTexture *GPU_texture_cube_create(int w, int d, const void *pixels, eGPUTextureFormat tex_format, - eGPUDataFormat gpu_data_format, - char err_out[256]) -{ - GPUTexture *tex = (GPUTexture *)MEM_callocN(sizeof(GPUTexture), __func__); - tex->w = w; - tex->h = w; - tex->d = d; - tex->samples = 0; - tex->refcount = 1; - tex->format = tex_format; - tex->components = gpu_get_component_count(tex_format); - tex->mipmaps = 0; - tex->format_flag = GPU_FORMAT_CUBE; - tex->number = -1; - - GLenum proxy; - - if (d == 0) { - proxy = GL_PROXY_TEXTURE_CUBE_MAP; - tex->target_base = tex->target = GL_TEXTURE_CUBE_MAP; - } - else { - proxy = GL_PROXY_TEXTURE_CUBE_MAP_ARRAY_ARB; - tex->target_base = tex->target = GL_TEXTURE_CUBE_MAP_ARRAY_ARB; - tex->format_flag |= GPU_FORMAT_ARRAY; - - if (!GPU_arb_texture_cube_map_array_is_supported()) { - fprintf(stderr, "ERROR: Attempt to create a cubemap array without hardware support!\n"); - BLI_assert(0); - GPU_texture_free(tex); - return NULL; - } - - if (d > GPU_max_texture_layers() / 6) { - BLI_assert(0); - GPU_texture_free(tex); - return NULL; - } - } - - GLenum internalformat = gpu_format_to_gl_internalformat(tex_format); - GLenum data_format = gpu_get_gl_dataformat(tex_format, &tex->format_flag); - GLenum data_type = gpu_get_gl_datatype(gpu_data_format); - - /* Generate Texture object */ - tex->bindcode = GPU_tex_alloc(); + eGPUDataFormat data_format, + char UNUSED(err_out[256])) +{ + Texture *tex = GPUBackend::get()->texture_alloc("Cube"); + bool success = tex->init_cubemap(w, d, tex_format); - if (!tex->bindcode) { - if (err_out) { - BLI_strncpy(err_out, "GPUTexture: texture create failed\n", 256); - } - else { - fprintf(stderr, "GPUTexture: texture create failed\n"); - } - GPU_texture_free(tex); + if (!success) { + delete tex; return NULL; } - - bool valid = gpu_texture_try_alloc( - tex, proxy, internalformat, data_format, data_type, tex->components, false, NULL, NULL); - - if (G.debug & G_DEBUG_GPU || !valid) { - printf( - "GPUTexture: create : %s,\t w : %5d, h : %5d, d : %5d, comp : %4d, size : %.2f " - "MiB,\t %s\n", - gl_enum_to_str(tex->target), - w, - w, - d * 6, - tex->components, - gpu_texture_memory_footprint_compute(tex) / 1048576.0f, - gl_enum_to_str(internalformat)); + if (pixels) { + tex->update(data_format, pixels); } + return reinterpret_cast(tex); +} - if (!valid) { - if (err_out) { - BLI_strncpy(err_out, "GPUTexture: texture alloc failed\n", 256); - } - else { - fprintf(stderr, - "GPUTexture: texture alloc failed. Likely not enough Video Memory or the requested " - "size is not supported by the implementation.\n"); - fprintf(stderr, - "Current texture memory usage : %.2f MiB.\n", - gpu_texture_memory_footprint_compute(tex) / 1048576.0f); - } - GPU_texture_free(tex); - return NULL; - } - - gpu_texture_memory_footprint_add(tex); - - glBindTexture(tex->target, tex->bindcode); - - /* Upload Texture */ - if (d == 0) { - const char *pixels_px, *pixels_py, *pixels_pz, *pixels_nx, *pixels_ny, *pixels_nz; - - if (pixels) { - size_t face_ofs = w * w * gpu_get_data_format_bytesize(tex->components, gpu_data_format); - pixels_px = (char *)pixels + 0 * face_ofs; - pixels_nx = (char *)pixels + 1 * face_ofs; - pixels_py = (char *)pixels + 2 * face_ofs; - pixels_ny = (char *)pixels + 3 * face_ofs; - pixels_pz = (char *)pixels + 4 * face_ofs; - pixels_nz = (char *)pixels + 5 * face_ofs; - } - else { - pixels_px = pixels_py = pixels_pz = pixels_nx = pixels_ny = pixels_nz = NULL; - } - - GLuint face = GL_TEXTURE_CUBE_MAP_POSITIVE_X; - glTexImage2D(face++, 0, internalformat, tex->w, tex->h, 0, data_format, data_type, pixels_px); - glTexImage2D(face++, 0, internalformat, tex->w, tex->h, 0, data_format, data_type, pixels_nx); - glTexImage2D(face++, 0, internalformat, tex->w, tex->h, 0, data_format, data_type, pixels_py); - glTexImage2D(face++, 0, internalformat, tex->w, tex->h, 0, data_format, data_type, pixels_ny); - glTexImage2D(face++, 0, internalformat, tex->w, tex->h, 0, data_format, data_type, pixels_pz); - glTexImage2D(face++, 0, internalformat, tex->w, tex->h, 0, data_format, data_type, pixels_nz); - } - else { - glTexImage3D(tex->target, - 0, - internalformat, - tex->w, - tex->h, - tex->d * 6, - 0, - data_format, - data_type, - pixels); - } +/* DDS texture loading. Return NULL if support is not available. */ +GPUTexture *GPU_texture_create_compressed( + int w, int h, int miplen, eGPUTextureFormat tex_format, const void *data) +{ + Texture *tex = GPUBackend::get()->texture_alloc("Cube"); + bool success = tex->init_2D(w, h, 0, tex_format); - /* Texture Parameters */ - if (GPU_texture_stencil(tex) || /* Does not support filtering */ - GPU_texture_integer(tex) || /* Does not support filtering */ - GPU_texture_depth(tex)) { - tex->sampler_state = GPU_SAMPLER_DEFAULT & ~GPU_SAMPLER_FILTER; - } - else { - tex->sampler_state = GPU_SAMPLER_DEFAULT; - } - /* Avoid issue with incomplete textures. */ - glTexParameteri(tex->target_base, GL_TEXTURE_MIN_FILTER, GL_NEAREST); - - glBindTexture(tex->target, 0); - - return tex; -} - -/* Special buffer textures. tex_format must be compatible with the buffer content. */ -GPUTexture *GPU_texture_create_buffer(eGPUTextureFormat tex_format, const GLuint buffer) -{ - GPUTexture *tex = (GPUTexture *)MEM_callocN(sizeof(GPUTexture), __func__); - tex->refcount = 1; - tex->format = tex_format; - tex->components = gpu_get_component_count(tex_format); - tex->format_flag = static_cast(0); - tex->target_base = tex->target = GL_TEXTURE_BUFFER; - tex->mipmaps = 0; - tex->number = -1; - - GLenum internalformat = gpu_format_to_gl_internalformat(tex_format); - - gpu_get_gl_dataformat(tex_format, &tex->format_flag); - - if (!(ELEM(tex_format, GPU_R8, GPU_R16) || ELEM(tex_format, GPU_R16F, GPU_R32F) || - ELEM(tex_format, GPU_R8I, GPU_R16I, GPU_R32I) || - ELEM(tex_format, GPU_R8UI, GPU_R16UI, GPU_R32UI) || ELEM(tex_format, GPU_RG8, GPU_RG16) || - ELEM(tex_format, GPU_RG16F, GPU_RG32F) || - ELEM(tex_format, GPU_RG8I, GPU_RG16I, GPU_RG32I) || - ELEM(tex_format, GPU_RG8UI, GPU_RG16UI, GPU_RG32UI) || - /* Not available until gl 4.0 */ - // ELEM(tex_format, GPU_RGB32F, GPU_RGB32I, GPU_RGB32UI) || - ELEM(tex_format, GPU_RGBA8, GPU_RGBA16) || ELEM(tex_format, GPU_RGBA16F, GPU_RGBA32F) || - ELEM(tex_format, GPU_RGBA8I, GPU_RGBA16I, GPU_RGBA32I) || - ELEM(tex_format, GPU_RGBA8UI, GPU_RGBA16UI, GPU_RGBA32UI))) { - fprintf(stderr, "GPUTexture: invalid format for texture buffer\n"); - GPU_texture_free(tex); + if (!success) { + delete tex; return NULL; } + if (data) { + size_t ofs = 0; + for (int mip = 0; mip < miplen; mip++) { + int extent[3], offset[3] = {0, 0, 0}; + tex->mip_size_get(mip, extent); - /* Generate Texture object */ - tex->bindcode = GPU_tex_alloc(); + size_t size = ((extent[0] + 3) / 4) * ((extent[1] + 3) / 4) * to_block_size(tex_format); + tex->update_sub(mip, offset, extent, to_data_format(tex_format), (uchar *)data + ofs); - if (!tex->bindcode) { - fprintf(stderr, "GPUTexture: texture create failed\n"); - GPU_texture_free(tex); - BLI_assert( - 0 && "glGenTextures failed: Are you sure a valid OGL context is active on this thread?\n"); - return NULL; + ofs += size; + } } + return reinterpret_cast(tex); +} - glBindTexture(tex->target, tex->bindcode); - glTexBuffer(tex->target, internalformat, buffer); - glGetTexLevelParameteriv(tex->target, 0, GL_TEXTURE_WIDTH, &tex->w); - glBindTexture(tex->target, 0); - - gpu_texture_memory_footprint_add(tex); +/* Create an error texture that will bind an invalid texture (pink) at draw time. */ +GPUTexture *GPU_texture_create_error(int dimension, bool is_array) +{ + float pixel[4] = {1.0f, 0.0f, 1.0f, 1.0f}; + int w = 1; + int h = (dimension < 2 && !is_array) ? 0 : 1; + int d = (dimension < 3 && !is_array) ? 0 : 1; - return tex; + fprintf(stderr, "GPUTexture: Blender Texture Not Loaded!"); + return GPU_texture_create_nD( + w, h, d, dimension, pixel, GPU_RGBA8, GPU_DATA_FLOAT, 0, false, NULL); } -static GLenum convert_target_to_gl(int dimension, bool is_array) +static inline eGPUTextureFormat to_texture_format(const GPUVertFormat *format) { - switch (dimension) { + if (format->attr_len > 1 || format->attr_len == 0) { + BLI_assert(!"Incorrect vertex format for buffer texture"); + return GPU_DEPTH_COMPONENT24; + } + switch (format->attrs[0].comp_len) { case 1: - return is_array ? GL_TEXTURE_1D_ARRAY : GL_TEXTURE_1D; + switch (format->attrs[0].comp_type) { + case GPU_COMP_I8: + return GPU_R8I; + case GPU_COMP_U8: + return GPU_R8UI; + case GPU_COMP_I16: + return GPU_R16I; + case GPU_COMP_U16: + return GPU_R16UI; + case GPU_COMP_I32: + return GPU_R32I; + case GPU_COMP_U32: + return GPU_R32UI; + case GPU_COMP_F32: + return GPU_R32F; + default: + break; + } + break; case 2: - return is_array ? GL_TEXTURE_2D_ARRAY : GL_TEXTURE_2D; + switch (format->attrs[0].comp_type) { + case GPU_COMP_I8: + return GPU_RG8I; + case GPU_COMP_U8: + return GPU_RG8UI; + case GPU_COMP_I16: + return GPU_RG16I; + case GPU_COMP_U16: + return GPU_RG16UI; + case GPU_COMP_I32: + return GPU_RG32I; + case GPU_COMP_U32: + return GPU_RG32UI; + case GPU_COMP_F32: + return GPU_RG32F; + default: + break; + } + break; case 3: - return GL_TEXTURE_3D; + /* Not supported until GL 4.0 */ + break; + case 4: + switch (format->attrs[0].comp_type) { + case GPU_COMP_I8: + return GPU_RGBA8I; + case GPU_COMP_U8: + return GPU_RGBA8UI; + case GPU_COMP_I16: + return GPU_RGBA16I; + case GPU_COMP_U16: + return GPU_RGBA16UI; + case GPU_COMP_I32: + return GPU_RGBA32I; + case GPU_COMP_U32: + return GPU_RGBA32UI; + case GPU_COMP_F32: + return GPU_RGBA32F; + default: + break; + } + break; default: - BLI_assert(0); - return GL_TEXTURE_2D; + break; } + BLI_assert(!"Unsupported vertex format for buffer texture"); + return GPU_DEPTH_COMPONENT24; } -/* Create an error texture that will bind an invalid texture (pink) at draw time. */ -GPUTexture *GPU_texture_create_error(int dimension, bool is_array) +GPUTexture *GPU_texture_create_from_vertbuf(GPUVertBuf *vert) { - GLenum textarget = convert_target_to_gl(dimension, is_array); - - GPUTexture *tex = (GPUTexture *)MEM_callocN(sizeof(GPUTexture), __func__); - tex->bindcode = 0; - tex->refcount = 1; - tex->target = textarget; - tex->target_base = textarget; - tex->samples = 0; - tex->sampler_state = GPU_SAMPLER_DEFAULT; - tex->number = -1; - - GPU_print_error_debug("Blender Texture Not Loaded"); - return tex; -} + eGPUTextureFormat tex_format = to_texture_format(&vert->format); + Texture *tex = GPUBackend::get()->texture_alloc("Cube"); -/* DDS texture loading. Return NULL if support is not available. */ -GPUTexture *GPU_texture_create_compressed( - int w, int h, int miplen, eGPUTextureFormat tex_format, const void *data) -{ - if (!GLEW_EXT_texture_compression_s3tc) { + bool success = tex->init_buffer(vert, tex_format); + if (!success) { + delete tex; return NULL; } - - GPUTexture *tex = (GPUTexture *)MEM_callocN(sizeof(GPUTexture), __func__); - tex->w = w; - tex->h = h; - tex->refcount = 1; - tex->target = tex->target_base = GL_TEXTURE_2D; - tex->format_flag = static_cast(0); - tex->components = gpu_get_component_count(tex_format); - tex->mipmaps = miplen - 1; - tex->sampler_state = GPU_SAMPLER_DEFAULT; - tex->number = -1; - - GLenum internalformat = gpu_format_to_gl_internalformat(tex_format); - - glGenTextures(1, &tex->bindcode); - glBindTexture(tex->target, tex->bindcode); - - /* Reset to opengl Defaults. (Untested, might not be needed) */ - glPixelStorei(GL_UNPACK_ALIGNMENT, 4); - - int blocksize = (ELEM(tex_format, GPU_RGBA8_DXT1, GPU_SRGB8_A8_DXT1)) ? 8 : 16; - - size_t ofs = 0; - for (int mip = 0; mip < miplen && (w || h); mip++, w >>= 1, h >>= 1) { - w = max_ii(1, w); - h = max_ii(1, h); - size_t size = ((w + 3) / 4) * ((h + 3) / 4) * blocksize; - - glCompressedTexImage2D(tex->target, mip, internalformat, w, h, 0, size, (uchar *)data + ofs); - - ofs += size; - } - - /* Restore Blender default. */ - glPixelStorei(GL_UNPACK_ALIGNMENT, 1); - - glTexParameteri(tex->target, GL_TEXTURE_MAX_LEVEL, tex->mipmaps); - glTexParameteri(tex->target, GL_TEXTURE_MIN_FILTER, GL_NEAREST); - - glBindTexture(tex->target, 0); - - return tex; + return reinterpret_cast(tex); } GPUTexture *GPU_texture_create_1d(int w, - eGPUTextureFormat tex_format, + eGPUTextureFormat format, const float *pixels, - char err_out[256]) + char out[256]) { - BLI_assert(w > 0); - eGPUDataFormat data_format = gpu_get_data_format_from_tex_format(tex_format); - return GPU_texture_create_nD(w, 0, 0, 1, pixels, tex_format, data_format, 0, false, err_out); + return GPU_texture_create_nD(w, 0, 0, 1, pixels, format, GPU_DATA_FLOAT, 0, false, out); } GPUTexture *GPU_texture_create_1d_array( - int w, int h, eGPUTextureFormat tex_format, const float *pixels, char err_out[256]) + int w, int h, eGPUTextureFormat format, const float *pixels, char out[256]) { - BLI_assert(w > 0 && h > 0); - eGPUDataFormat data_format = gpu_get_data_format_from_tex_format(tex_format); - return GPU_texture_create_nD(w, h, 0, 1, pixels, tex_format, data_format, 0, false, err_out); + return GPU_texture_create_nD(w, h, 0, 1, pixels, format, GPU_DATA_FLOAT, 0, false, out); } GPUTexture *GPU_texture_create_2d( - int w, int h, eGPUTextureFormat tex_format, const float *pixels, char err_out[256]) + int w, int h, eGPUTextureFormat format, const float *pixels, char out[256]) { - BLI_assert(w > 0 && h > 0); - eGPUDataFormat data_format = gpu_get_data_format_from_tex_format(tex_format); - return GPU_texture_create_nD(w, h, 0, 2, pixels, tex_format, data_format, 0, false, err_out); -} - -GPUTexture *GPU_texture_create_2d_multisample(int w, - int h, - eGPUTextureFormat tex_format, - const float *pixels, - int samples, - char err_out[256]) -{ - BLI_assert(w > 0 && h > 0); - eGPUDataFormat data_format = gpu_get_data_format_from_tex_format(tex_format); - return GPU_texture_create_nD( - w, h, 0, 2, pixels, tex_format, data_format, samples, false, err_out); + return GPU_texture_create_nD(w, h, 0, 2, pixels, format, GPU_DATA_FLOAT, 0, false, out); } GPUTexture *GPU_texture_create_2d_array( - int w, int h, int d, eGPUTextureFormat tex_format, const float *pixels, char err_out[256]) + int w, int h, int d, eGPUTextureFormat format, const float *pixels, char out[256]) { - BLI_assert(w > 0 && h > 0 && d > 0); - eGPUDataFormat data_format = gpu_get_data_format_from_tex_format(tex_format); - return GPU_texture_create_nD(w, h, d, 2, pixels, tex_format, data_format, 0, false, err_out); + return GPU_texture_create_nD(w, h, d, 2, pixels, format, GPU_DATA_FLOAT, 0, false, out); } GPUTexture *GPU_texture_create_3d( - int w, int h, int d, eGPUTextureFormat tex_format, const float *pixels, char err_out[256]) + int w, int h, int d, eGPUTextureFormat format, const float *pixels, char out[256]) { - BLI_assert(w > 0 && h > 0 && d > 0); - eGPUDataFormat data_format = gpu_get_data_format_from_tex_format(tex_format); - return GPU_texture_create_nD(w, h, d, 3, pixels, tex_format, data_format, 0, true, err_out); + return GPU_texture_create_nD(w, h, d, 3, pixels, format, GPU_DATA_FLOAT, 0, true, out); } GPUTexture *GPU_texture_create_cube(int w, - eGPUTextureFormat tex_format, + eGPUTextureFormat format, const float *fpixels, - char err_out[256]) + char out[256]) { - BLI_assert(w > 0); - eGPUDataFormat data_format = gpu_get_data_format_from_tex_format(tex_format); - return GPU_texture_cube_create(w, 0, fpixels, tex_format, data_format, err_out); + return GPU_texture_cube_create(w, 0, fpixels, format, GPU_DATA_FLOAT, out); } GPUTexture *GPU_texture_create_cube_array( - int w, int d, eGPUTextureFormat tex_format, const float *fpixels, char err_out[256]) -{ - BLI_assert(w > 0 && d > 0); - eGPUDataFormat data_format = gpu_get_data_format_from_tex_format(tex_format); - return GPU_texture_cube_create(w, d, fpixels, tex_format, data_format, err_out); -} - -GPUTexture *GPU_texture_create_from_vertbuf(GPUVertBuf *vert) + int w, int d, eGPUTextureFormat format, const float *fpixels, char out[256]) { - GPUVertFormat *format = &vert->format; - GPUVertAttr *attr = &format->attrs[0]; - - /* Detect incompatible cases (not supported by texture buffers) */ - BLI_assert(format->attr_len == 1 && vert->vbo_id != 0); - BLI_assert(attr->comp_len != 3); /* Not until OGL 4.0 */ - BLI_assert(attr->comp_type != GPU_COMP_I10); - BLI_assert(attr->fetch_mode != GPU_FETCH_INT_TO_FLOAT); - - uint byte_per_comp = attr->sz / attr->comp_len; - bool is_uint = ELEM(attr->comp_type, GPU_COMP_U8, GPU_COMP_U16, GPU_COMP_U32); - - /* Cannot fetch signed int or 32bit ints as normalized float. */ - if (attr->fetch_mode == GPU_FETCH_INT_TO_FLOAT_UNIT) { - BLI_assert(is_uint || byte_per_comp <= 2); - } - - eGPUTextureFormat data_type; - switch (attr->fetch_mode) { - case GPU_FETCH_FLOAT: - switch (attr->comp_len) { - case 1: - data_type = GPU_R32F; - break; - case 2: - data_type = GPU_RG32F; - break; - // case 3: data_type = GPU_RGB32F; break; /* Not supported */ - default: - data_type = GPU_RGBA32F; - break; - } - break; - case GPU_FETCH_INT: - switch (attr->comp_len) { - case 1: - switch (byte_per_comp) { - case 1: - data_type = (is_uint) ? GPU_R8UI : GPU_R8I; - break; - case 2: - data_type = (is_uint) ? GPU_R16UI : GPU_R16I; - break; - default: - data_type = (is_uint) ? GPU_R32UI : GPU_R32I; - break; - } - break; - case 2: - switch (byte_per_comp) { - case 1: - data_type = (is_uint) ? GPU_RG8UI : GPU_RG8I; - break; - case 2: - data_type = (is_uint) ? GPU_RG16UI : GPU_RG16I; - break; - default: - data_type = (is_uint) ? GPU_RG32UI : GPU_RG32I; - break; - } - break; - default: - switch (byte_per_comp) { - case 1: - data_type = (is_uint) ? GPU_RGBA8UI : GPU_RGBA8I; - break; - case 2: - data_type = (is_uint) ? GPU_RGBA16UI : GPU_RGBA16I; - break; - default: - data_type = (is_uint) ? GPU_RGBA32UI : GPU_RGBA32I; - break; - } - break; - } - break; - case GPU_FETCH_INT_TO_FLOAT_UNIT: - switch (attr->comp_len) { - case 1: - data_type = (byte_per_comp == 1) ? GPU_R8 : GPU_R16; - break; - case 2: - data_type = (byte_per_comp == 1) ? GPU_RG8 : GPU_RG16; - break; - default: - data_type = (byte_per_comp == 1) ? GPU_RGBA8 : GPU_RGBA16; - break; - } - break; - default: - BLI_assert(0); - return NULL; - } - - return GPU_texture_create_buffer(data_type, vert->vbo_id); + return GPU_texture_cube_create(w, d, fpixels, format, GPU_DATA_FLOAT, out); } -void GPU_texture_add_mipmap(GPUTexture *tex, +void GPU_texture_add_mipmap(GPUTexture *tex_, eGPUDataFormat gpu_data_format, int miplvl, const void *pixels) { - BLI_assert((int)tex->format > -1); - BLI_assert(tex->components > -1); - BLI_assert(miplvl > tex->mipmaps); - - gpu_validate_data_format(tex->format, gpu_data_format); - - GLenum internalformat = gpu_format_to_gl_internalformat(tex->format); - GLenum data_format = gpu_get_gl_dataformat(tex->format, &tex->format_flag); - GLenum data_type = gpu_get_gl_datatype(gpu_data_format); - - glBindTexture(tex->target, tex->bindcode); - - int size[3]; - GPU_texture_get_mipmap_size(tex, miplvl, size); - - switch (tex->target) { - case GL_TEXTURE_1D: - glTexImage1D( - tex->target, miplvl, internalformat, size[0], 0, data_format, data_type, pixels); - break; - case GL_TEXTURE_2D: - case GL_TEXTURE_1D_ARRAY: - glTexImage2D(tex->target, - miplvl, - internalformat, - size[0], - size[1], - 0, - data_format, - data_type, - pixels); - break; - case GL_TEXTURE_3D: - case GL_TEXTURE_2D_ARRAY: - case GL_TEXTURE_CUBE_MAP_ARRAY_ARB: - glTexImage3D(tex->target, - miplvl, - internalformat, - size[0], - size[1], - size[2], - 0, - data_format, - data_type, - pixels); - break; - case GL_TEXTURE_2D_MULTISAMPLE: - /* Multisample textures cannot have mipmaps. */ - default: - BLI_assert(!"tex->target mode not supported"); - } - - tex->mipmaps = miplvl; - glTexParameteri(GPU_texture_target(tex), GL_TEXTURE_MAX_LEVEL, miplvl); - - glBindTexture(tex->target, 0); + Texture *tex = reinterpret_cast(tex_); + int extent[3] = {1, 1, 1}, offset[3] = {0, 0, 0}; + tex->mip_size_get(miplvl, extent); + reinterpret_cast(tex)->update_sub(miplvl, offset, extent, gpu_data_format, pixels); } void GPU_texture_update_sub(GPUTexture *tex, @@ -1546,571 +432,144 @@ void GPU_texture_update_sub(GPUTexture *tex, int height, int depth) { - BLI_assert((int)tex->format > -1); - BLI_assert(tex->components > -1); - - GLenum data_format = gpu_get_gl_dataformat(tex->format, &tex->format_flag); - GLenum data_type = gpu_get_gl_datatype(gpu_data_format); - - /* Save and restore. */ - GLint texture_bound = 0; - if (tex->number == -1) { - glActiveTexture(GL_TEXTURE0); - glGetIntegerv(gl_enum_target_to_binding(tex->target), &texture_bound); - glBindTexture(tex->target, tex->bindcode); - } - else { - glActiveTexture(GL_TEXTURE0 + tex->number); - } - - switch (tex->target) { - case GL_TEXTURE_1D: - glTexSubImage1D(tex->target, 0, offset_x, width, data_format, data_type, pixels); - break; - case GL_TEXTURE_2D: - case GL_TEXTURE_2D_MULTISAMPLE: - case GL_TEXTURE_1D_ARRAY: - glTexSubImage2D( - tex->target, 0, offset_x, offset_y, width, height, data_format, data_type, pixels); - break; - case GL_TEXTURE_3D: - case GL_TEXTURE_2D_ARRAY: - glTexSubImage3D(tex->target, - 0, - offset_x, - offset_y, - offset_z, - width, - height, - depth, - data_format, - data_type, - pixels); - break; - default: - BLI_assert(!"tex->target mode not supported"); - } - - if (tex->number == -1) { - glBindTexture(tex->target, texture_bound); - } + int offset[3] = {offset_x, offset_y, offset_z}; + int extent[3] = {width, height, depth}; + reinterpret_cast(tex)->update_sub(0, offset, extent, gpu_data_format, pixels); } -void *GPU_texture_read(GPUTexture *tex, eGPUDataFormat gpu_data_format, int miplvl) +void *GPU_texture_read(GPUTexture *tex_, eGPUDataFormat data_format, int miplvl) { - BLI_assert(miplvl <= tex->mipmaps); - - int size[3] = {0, 0, 0}; - GPU_texture_get_mipmap_size(tex, miplvl, size); - - gpu_validate_data_format(tex->format, gpu_data_format); - - size_t samples_count = max_ii(1, tex->samples); - samples_count *= size[0]; - samples_count *= max_ii(1, size[1]); - samples_count *= max_ii(1, size[2]); - samples_count *= (GPU_texture_cube(tex) && !GPU_texture_array(tex)) ? 6 : 1; - - size_t buf_size = samples_count * gpu_get_data_format_bytesize(tex->components, gpu_data_format); - - /* AMD Pro driver have a bug that write 8 bytes past buffer size - * if the texture is big. (see T66573) */ - void *buf = MEM_mallocN(buf_size + 8, "GPU_texture_read"); - - GLenum data_format = gpu_get_gl_dataformat(tex->format, &tex->format_flag); - GLenum data_type = gpu_get_gl_datatype(gpu_data_format); - - glBindTexture(tex->target, tex->bindcode); - - if (GPU_texture_cube(tex) && !GPU_texture_array(tex)) { - int cube_face_size = buf_size / 6; - for (int i = 0; i < 6; i++) { - glGetTexImage(GL_TEXTURE_CUBE_MAP_POSITIVE_X + i, - miplvl, - data_format, - data_type, - ((char *)buf) + cube_face_size * i); - } - } - else { - glGetTexImage(tex->target, miplvl, data_format, data_type, buf); - } - - glBindTexture(tex->target, 0); - - return buf; + Texture *tex = reinterpret_cast(tex_); + return tex->read(miplvl, data_format); } -void GPU_texture_clear(GPUTexture *tex, eGPUDataFormat gpu_data_format, const void *color) +/** + * Fills the whole texture with the same data for all pixels. + * \warning Only work for 2D texture for now. + * \warning Only clears the mip 0 of the texture. + * \param data_format data format of the pixel data. + * \param data 1 pixel worth of data to fill the texture with. + */ +void GPU_texture_clear(GPUTexture *tex, eGPUDataFormat data_format, const void *data) { - BLI_assert(color != NULL); /* Do not accept NULL as parameter. */ - gpu_validate_data_format(tex->format, gpu_data_format); - - if (false && GLEW_ARB_clear_texture) { - GLenum data_type = gpu_get_gl_datatype(gpu_data_format); - GLenum data_format = gpu_get_gl_dataformat(tex->format, &tex->format_flag); - glClearTexImage(tex->bindcode, 0, data_format, data_type, color); - - if (GPU_texture_stencil(tex) && GPU_texture_depth(tex)) { - /* TODO(clem) implement in fallback. */ - BLI_assert(0); - } - else if (GPU_texture_depth(tex)) { - switch (gpu_data_format) { - case GPU_DATA_FLOAT: - case GPU_DATA_UNSIGNED_INT: - break; - default: - /* TODO(clem) implement in fallback. */ - BLI_assert(0); - break; - } - } - else { - switch (gpu_data_format) { - case GPU_DATA_FLOAT: - case GPU_DATA_UNSIGNED_INT: - case GPU_DATA_UNSIGNED_BYTE: - break; - default: - /* TODO(clem) implement in fallback. */ - BLI_assert(0); - break; - } - } - } - else { - /* Fallback for older GL. */ - GPUFrameBuffer *prev_fb = GPU_framebuffer_active_get(); - - gpu_texture_framebuffer_ensure(tex); - /* This means that this function can only be used in one context for each texture. */ - BLI_assert(tex->copy_fb_ctx == GPU_context_active_get()); - - int viewport[4]; - GPU_viewport_size_get_i(viewport); - - glBindFramebuffer(GL_FRAMEBUFFER, tex->copy_fb); - glViewport(0, 0, tex->w, tex->h); - - /* Watch: Write mask could prevent the clear. - * glClearTexImage does not change the state so we don't do it here either. */ - if (GPU_texture_stencil(tex) && GPU_texture_depth(tex)) { - /* TODO(clem) implement. */ - BLI_assert(0); - } - else if (GPU_texture_depth(tex)) { - float depth; - switch (gpu_data_format) { - case GPU_DATA_FLOAT: { - depth = *(float *)color; - break; - } - case GPU_DATA_UNSIGNED_INT: { - depth = *(uint *)color / (float)UINT_MAX; - break; - } - default: - BLI_assert(!"Unhandled data format"); - depth = 0.0f; - break; - } - glClearDepth(depth); - glClear(GL_DEPTH_BUFFER_BIT); - } - else { - float r, g, b, a; - switch (gpu_data_format) { - case GPU_DATA_FLOAT: { - float *f_color = (float *)color; - r = f_color[0]; - g = (tex->components > 1) ? f_color[1] : 0.0f; - b = (tex->components > 2) ? f_color[2] : 0.0f; - a = (tex->components > 3) ? f_color[3] : 0.0f; - break; - } - case GPU_DATA_UNSIGNED_INT: { - uint *u_color = (uint *)color; - r = u_color[0] / (float)UINT_MAX; - g = (tex->components > 1) ? u_color[1] / (float)UINT_MAX : 0.0f; - b = (tex->components > 2) ? u_color[2] / (float)UINT_MAX : 0.0f; - a = (tex->components > 3) ? u_color[3] / (float)UINT_MAX : 0.0f; - break; - } - case GPU_DATA_UNSIGNED_BYTE: { - uchar *ub_color = (uchar *)color; - r = ub_color[0] / 255.0f; - g = (tex->components > 1) ? ub_color[1] / 255.0f : 0.0f; - b = (tex->components > 2) ? ub_color[2] / 255.0f : 0.0f; - a = (tex->components > 3) ? ub_color[3] / 255.0f : 0.0f; - break; - } - default: - BLI_assert(!"Unhandled data format"); - r = g = b = a = 0.0f; - break; - } - glClearColor(r, g, b, a); - glClear(GL_COLOR_BUFFER_BIT); - } - - glViewport(UNPACK4(viewport)); - - if (prev_fb) { - GPU_framebuffer_bind(prev_fb); - } - } + BLI_assert(data != NULL); /* Do not accept NULL as parameter. */ + reinterpret_cast(tex)->clear(data_format, data); } -void GPU_texture_update(GPUTexture *tex, eGPUDataFormat data_format, const void *pixels) +void GPU_texture_update(GPUTexture *tex, eGPUDataFormat data_format, const void *data) { - GPU_texture_update_sub(tex, data_format, pixels, 0, 0, 0, tex->w, tex->h, tex->d); + reinterpret_cast(tex)->update(data_format, data); } void GPU_invalid_tex_init(void) { - memory_usage = 0; - const float color[4] = {1.0f, 0.0f, 1.0f, 1.0f}; - GG.invalid_tex_1D = GPU_texture_create_1d(1, GPU_RGBA8, color, NULL); - GG.invalid_tex_2D = GPU_texture_create_2d(1, 1, GPU_RGBA8, color, NULL); - GG.invalid_tex_3D = GPU_texture_create_3d(1, 1, 1, GPU_RGBA8, color, NULL); + /* TODO remove */ } -void GPU_invalid_tex_bind(int mode) +void GPU_invalid_tex_bind(int UNUSED(mode)) { - switch (mode) { - case GL_TEXTURE_1D: - glBindTexture(GL_TEXTURE_1D, GG.invalid_tex_1D->bindcode); - break; - case GL_TEXTURE_2D: - glBindTexture(GL_TEXTURE_2D, GG.invalid_tex_2D->bindcode); - break; - case GL_TEXTURE_3D: - glBindTexture(GL_TEXTURE_3D, GG.invalid_tex_3D->bindcode); - break; - } + /* TODO remove */ } void GPU_invalid_tex_free(void) { - if (GG.invalid_tex_1D) { - GPU_texture_free(GG.invalid_tex_1D); - } - if (GG.invalid_tex_2D) { - GPU_texture_free(GG.invalid_tex_2D); - } - if (GG.invalid_tex_3D) { - GPU_texture_free(GG.invalid_tex_3D); - } + /* TODO remove */ } -/* set_number is to save the the texture unit for setting texture parameters. */ -void GPU_texture_bind_ex(GPUTexture *tex, eGPUSamplerState state, int unit, const bool set_number) +void GPU_texture_bind_ex(GPUTexture *tex_, + eGPUSamplerState state, + int unit, + const bool UNUSED(set_number)) { - BLI_assert(unit >= 0); - - if (unit >= GPU_max_textures()) { - fprintf(stderr, "Not enough texture slots.\n"); - return; - } - - if (G.debug & G_DEBUG) { - for (int i = 0; i < GPU_TEX_MAX_FBO_ATTACHED; i++) { - if (tex->fb[i] && GPU_framebuffer_bound(tex->fb[i])) { - fprintf(stderr, - "Feedback loop warning!: Attempting to bind " - "texture attached to current framebuffer!\n"); - BLI_assert(0); /* Should never happen! */ - break; - } - } - } - - if (set_number) { - tex->number = unit; - } - - glActiveTexture(GL_TEXTURE0 + unit); - - state = (state < GPU_SAMPLER_MAX) ? state : tex->sampler_state; - - if (tex->bindcode != 0) { - glBindTexture(tex->target, tex->bindcode); - glBindSampler(unit, GG.samplers[state]); - } - else { - GPU_invalid_tex_bind(tex->target_base); - glBindSampler(unit, 0); - } + Texture *tex = reinterpret_cast(tex_); + GPU_context_active_get()->state_manager->texture_bind(tex, state, unit); } -void GPU_texture_bind(GPUTexture *tex, int unit) +void GPU_texture_bind(GPUTexture *tex_, int unit) { - GPU_texture_bind_ex(tex, GPU_SAMPLER_MAX, unit, true); + Texture *tex = reinterpret_cast(tex_); + GPU_context_active_get()->state_manager->texture_bind(tex, tex->sampler_state, unit); } -void GPU_texture_unbind(GPUTexture *tex) +void GPU_texture_unbind(GPUTexture *tex_) { - if (tex->number == -1) { - return; - } - - glActiveTexture(GL_TEXTURE0 + tex->number); - glBindTexture(tex->target, 0); - glBindSampler(tex->number, 0); - tex->number = -1; + Texture *tex = reinterpret_cast(tex_); + GPU_context_active_get()->state_manager->texture_unbind(tex); } void GPU_texture_unbind_all(void) { - if (GLEW_ARB_multi_bind) { - /* Some drivers crash because of the NULL array even if that's explicitly - * allowed by the spec... *sigh* (see T77549). */ - GLuint texs[32] = {0}; - int count = min_ii(32, GPU_max_textures()); - - glBindTextures(0, count, texs); - glBindSamplers(0, count, texs); - return; - } - - for (int i = 0; i < GPU_max_textures(); i++) { - glActiveTexture(GL_TEXTURE0 + i); - glBindTexture(GL_TEXTURE_2D, 0); - glBindTexture(GL_TEXTURE_2D_ARRAY, 0); - glBindTexture(GL_TEXTURE_1D, 0); - glBindTexture(GL_TEXTURE_1D_ARRAY, 0); - glBindTexture(GL_TEXTURE_3D, 0); - glBindTexture(GL_TEXTURE_CUBE_MAP, 0); - glBindTexture(GL_TEXTURE_BUFFER, 0); - if (GPU_arb_texture_cube_map_array_is_supported()) { - glBindTexture(GL_TEXTURE_CUBE_MAP_ARRAY_ARB, 0); - } - glBindSampler(i, 0); - } - - glActiveTexture(GL_TEXTURE0); + GPU_context_active_get()->state_manager->texture_unbind_all(); } void GPU_texture_generate_mipmap(GPUTexture *tex) { - gpu_texture_memory_footprint_remove(tex); - int levels = 1 + floor(log2(max_ii(tex->w, tex->h))); + // gpu_texture_memory_footprint_remove(tex); - /* Save and restore. */ - GLint texture_bound = 0; - if (tex->number == -1) { - glActiveTexture(GL_TEXTURE0); - glGetIntegerv(gl_enum_target_to_binding(tex->target), &texture_bound); - glBindTexture(tex->target, tex->bindcode); - } - else { - glActiveTexture(GL_TEXTURE0 + tex->number); - } + reinterpret_cast(tex)->generate_mipmap(); - if (GPU_texture_depth(tex)) { - /* Some drivers have bugs when using glGenerateMipmap with depth textures (see T56789). - * In this case we just create a complete texture with mipmaps manually without - * down-sampling. You must initialize the texture levels using other methods like - * GPU_framebuffer_recursive_downsample(). */ - eGPUDataFormat data_format = gpu_get_data_format_from_tex_format(tex->format); - for (int i = 1; i < levels; i++) { - GPU_texture_add_mipmap(tex, data_format, i, NULL); - } - glBindTexture(tex->target, tex->bindcode); - } - else { - glGenerateMipmap(tex->target_base); - } - - tex->mipmaps = levels; - gpu_texture_memory_footprint_add(tex); - - if (tex->number == -1) { - glBindTexture(tex->target, texture_bound); - } -} - -static GLenum gpu_texture_default_attachment(GPUTexture *tex) -{ - return !GPU_texture_depth(tex) ? - GL_COLOR_ATTACHMENT0 : - (GPU_texture_stencil(tex) ? GL_DEPTH_STENCIL_ATTACHMENT : GL_DEPTH_ATTACHMENT); -} - -static void gpu_texture_framebuffer_ensure(GPUTexture *tex) -{ - if (tex->copy_fb == 0) { - tex->copy_fb = GPU_fbo_alloc(); - tex->copy_fb_ctx = GPU_context_active_get(); - - GLenum attachment = gpu_texture_default_attachment(tex); - - glBindFramebuffer(GL_FRAMEBUFFER, tex->copy_fb); - glFramebufferTexture(GL_FRAMEBUFFER, attachment, tex->bindcode, 0); - if (!GPU_texture_depth(tex)) { - glReadBuffer(GL_COLOR_ATTACHMENT0); - glDrawBuffer(GL_COLOR_ATTACHMENT0); - } - BLI_assert(glCheckFramebufferStatus(GL_FRAMEBUFFER) == GL_FRAMEBUFFER_COMPLETE); - glBindFramebuffer(GL_FRAMEBUFFER, 0); - } + // gpu_texture_memory_footprint_add(tex); } /* Copy a texture content to a similar texture. Only Mip 0 is copied. */ -void GPU_texture_copy(GPUTexture *dst, GPUTexture *src) -{ - BLI_assert(dst->target == src->target); - BLI_assert(dst->w == src->w); - BLI_assert(dst->h == src->h); - BLI_assert(!GPU_texture_cube(src) && !GPU_texture_cube(dst)); - /* TODO support array / 3D textures. */ - BLI_assert(dst->d == 0); - BLI_assert(dst->format == src->format); - - if (GLEW_ARB_copy_image && !GPU_texture_copy_workaround()) { - /* Opengl 4.3 */ - glCopyImageSubData(src->bindcode, - src->target, - 0, - 0, - 0, - 0, - dst->bindcode, - dst->target, - 0, - 0, - 0, - 0, - src->w, - src->h, - 1); - } - else { - /* Fallback for older GL. */ - GPUFrameBuffer *prev_fb = GPU_framebuffer_active_get(); - - gpu_texture_framebuffer_ensure(src); - gpu_texture_framebuffer_ensure(dst); - - /* This means that this function can only be used in one context for each texture. */ - BLI_assert(src->copy_fb_ctx == GPU_context_active_get()); - BLI_assert(dst->copy_fb_ctx == GPU_context_active_get()); - - glBindFramebuffer(GL_READ_FRAMEBUFFER, src->copy_fb); - glBindFramebuffer(GL_DRAW_FRAMEBUFFER, dst->copy_fb); - - GLbitfield mask = 0; - if (GPU_texture_stencil(src)) { - mask |= GL_STENCIL_BUFFER_BIT; - } - if (GPU_texture_depth(src)) { - mask |= GL_DEPTH_BUFFER_BIT; - } - else { - mask |= GL_COLOR_BUFFER_BIT; - } - - glBlitFramebuffer(0, 0, src->w, src->h, 0, 0, src->w, src->h, mask, GL_NEAREST); - - if (prev_fb) { - GPU_framebuffer_bind(prev_fb); - } - } +void GPU_texture_copy(GPUTexture *dst_, GPUTexture *src_) +{ + Texture *src = reinterpret_cast(src_); + Texture *dst = reinterpret_cast(dst_); + src->copy_to(dst); } -void GPU_texture_compare_mode(GPUTexture *tex, bool use_compare) +void GPU_texture_compare_mode(GPUTexture *tex_, bool use_compare) { - /* Could become an assertion ? (fclem) */ - if (!GPU_texture_depth(tex)) { - return; - } + Texture *tex = reinterpret_cast(tex_); + /* Only depth formats does support compare mode. */ + BLI_assert(!(use_compare) || (tex->format_flag_get() & GPU_FORMAT_DEPTH)); SET_FLAG_FROM_TEST(tex->sampler_state, use_compare, GPU_SAMPLER_COMPARE); } -void GPU_texture_filter_mode(GPUTexture *tex, bool use_filter) +void GPU_texture_filter_mode(GPUTexture *tex_, bool use_filter) { + Texture *tex = reinterpret_cast(tex_); /* Stencil and integer format does not support filtering. */ - BLI_assert(!use_filter || !(GPU_texture_stencil(tex) || GPU_texture_integer(tex))); - + BLI_assert(!(use_filter) || + !(tex->format_flag_get() & (GPU_FORMAT_STENCIL | GPU_FORMAT_INTEGER))); SET_FLAG_FROM_TEST(tex->sampler_state, use_filter, GPU_SAMPLER_FILTER); } -void GPU_texture_mipmap_mode(GPUTexture *tex, bool use_mipmap, bool use_filter) +void GPU_texture_mipmap_mode(GPUTexture *tex_, bool use_mipmap, bool use_filter) { + Texture *tex = reinterpret_cast(tex_); /* Stencil and integer format does not support filtering. */ BLI_assert(!(use_filter || use_mipmap) || - !(GPU_texture_stencil(tex) || GPU_texture_integer(tex))); - + !(tex->format_flag_get() & (GPU_FORMAT_STENCIL | GPU_FORMAT_INTEGER))); SET_FLAG_FROM_TEST(tex->sampler_state, use_mipmap, GPU_SAMPLER_MIPMAP); SET_FLAG_FROM_TEST(tex->sampler_state, use_filter, GPU_SAMPLER_FILTER); } -void GPU_texture_anisotropic_filter(GPUTexture *tex, bool use_aniso) +void GPU_texture_anisotropic_filter(GPUTexture *tex_, bool use_aniso) { + Texture *tex = reinterpret_cast(tex_); /* Stencil and integer format does not support filtering. */ - BLI_assert(!(use_aniso) || !(GPU_texture_stencil(tex) || GPU_texture_integer(tex))); - + BLI_assert(!(use_aniso) || + !(tex->format_flag_get() & (GPU_FORMAT_STENCIL | GPU_FORMAT_INTEGER))); SET_FLAG_FROM_TEST(tex->sampler_state, use_aniso, GPU_SAMPLER_ANISO); } -void GPU_texture_wrap_mode(GPUTexture *tex, bool use_repeat, bool use_clamp) +void GPU_texture_wrap_mode(GPUTexture *tex_, bool use_repeat, bool use_clamp) { + Texture *tex = reinterpret_cast(tex_); SET_FLAG_FROM_TEST(tex->sampler_state, use_repeat, GPU_SAMPLER_REPEAT); SET_FLAG_FROM_TEST(tex->sampler_state, !use_clamp, GPU_SAMPLER_CLAMP_BORDER); } -static int gpu_texture_swizzle_to_enum(const char swizzle) -{ - switch (swizzle) { - case 'w': - case 'a': - return GL_ALPHA; - case 'z': - case 'b': - return GL_BLUE; - case 'y': - case 'g': - return GL_GREEN; - case '0': - return GL_ZERO; - case '1': - return GL_ONE; - case 'x': - case 'r': - default: - return GL_RED; - } -} - void GPU_texture_swizzle_set(GPUTexture *tex, const char swizzle[4]) { - /* Save and restore. */ - GLint texture_bound = 0; - if (tex->number == -1) { - glActiveTexture(GL_TEXTURE0); - glGetIntegerv(gl_enum_target_to_binding(tex->target), &texture_bound); - glBindTexture(tex->target, tex->bindcode); - } - else { - glActiveTexture(GL_TEXTURE0 + tex->number); - } - - GLint gl_swizzle[4] = {gpu_texture_swizzle_to_enum(swizzle[0]), - gpu_texture_swizzle_to_enum(swizzle[1]), - gpu_texture_swizzle_to_enum(swizzle[2]), - gpu_texture_swizzle_to_enum(swizzle[3])}; - - glActiveTexture(GL_TEXTURE0 + tex->number); - glTexParameteriv(tex->target_base, GL_TEXTURE_SWIZZLE_RGBA, gl_swizzle); - - if (tex->number == -1) { - glBindTexture(tex->target, texture_bound); - } + reinterpret_cast(tex)->swizzle_set(swizzle); } -void GPU_texture_free(GPUTexture *tex) +void GPU_texture_free(GPUTexture *tex_) { + Texture *tex = reinterpret_cast(tex_); tex->refcount--; if (tex->refcount < 0) { @@ -2118,113 +577,98 @@ void GPU_texture_free(GPUTexture *tex) } if (tex->refcount == 0) { - for (int i = 0; i < GPU_TEX_MAX_FBO_ATTACHED; i++) { - if (tex->fb[i] != NULL) { - FrameBuffer *framebuffer = reinterpret_cast(tex->fb[i]); - framebuffer->attachment_set((GPUAttachmentType)tex->fb_attachment[i], GPU_ATTACHMENT_NONE); - } - } - - if (tex->bindcode) { - GPU_tex_free(tex->bindcode); - } - if (tex->copy_fb) { - GPU_fbo_free(tex->copy_fb, tex->copy_fb_ctx); - } - - gpu_texture_memory_footprint_remove(tex); - - MEM_freeN(tex); + delete tex; } } void GPU_texture_ref(GPUTexture *tex) { - tex->refcount++; + reinterpret_cast(tex)->refcount++; } -int GPU_texture_target(const GPUTexture *tex) +/* TODO(fclem) Remove! This is broken as it is! */ +int GPU_texture_target(const GPUTexture *UNUSED(tex)) { - return tex->target; + return GL_TEXTURE_2D; } int GPU_texture_width(const GPUTexture *tex) { - return tex->w; + return reinterpret_cast(tex)->width_get(); } int GPU_texture_height(const GPUTexture *tex) { - return tex->h; + return reinterpret_cast(tex)->height_get(); } int GPU_texture_orig_width(const GPUTexture *tex) { - return tex->orig_w; + return reinterpret_cast(tex)->src_w; } int GPU_texture_orig_height(const GPUTexture *tex) { - return tex->orig_h; -} - -void GPU_texture_orig_size_set(GPUTexture *tex, int w, int h) -{ - tex->orig_w = w; - tex->orig_h = h; + return reinterpret_cast(tex)->src_h; } -int GPU_texture_layers(const GPUTexture *tex) +void GPU_texture_orig_size_set(GPUTexture *tex_, int w, int h) { - return tex->d; + Texture *tex = reinterpret_cast(tex_); + tex->src_w = w; + tex->src_h = h; } eGPUTextureFormat GPU_texture_format(const GPUTexture *tex) { - return tex->format; -} - -int GPU_texture_samples(const GPUTexture *tex) -{ - return tex->samples; + return reinterpret_cast(tex)->format_get(); } -bool GPU_texture_array(const GPUTexture *tex) +/* TODO remove */ +int GPU_texture_samples(const GPUTexture *UNUSED(tex)) { - return (tex->format_flag & GPU_FORMAT_ARRAY) != 0; + return 0; } bool GPU_texture_depth(const GPUTexture *tex) { - return (tex->format_flag & GPU_FORMAT_DEPTH) != 0; + return (reinterpret_cast(tex)->format_flag_get() & GPU_FORMAT_DEPTH) != 0; } bool GPU_texture_stencil(const GPUTexture *tex) { - return (tex->format_flag & GPU_FORMAT_STENCIL) != 0; + return (reinterpret_cast(tex)->format_flag_get() & GPU_FORMAT_STENCIL) != 0; } bool GPU_texture_integer(const GPUTexture *tex) { - return (tex->format_flag & GPU_FORMAT_INTEGER) != 0; + return (reinterpret_cast(tex)->format_flag_get() & GPU_FORMAT_INTEGER) != 0; } bool GPU_texture_cube(const GPUTexture *tex) { - return (tex->format_flag & GPU_FORMAT_CUBE) != 0; + return (reinterpret_cast(tex)->type_get() & GPU_TEXTURE_CUBE) != 0; } +bool GPU_texture_array(const GPUTexture *tex) +{ + return (reinterpret_cast(tex)->type_get() & GPU_TEXTURE_ARRAY) != 0; +} + +/* TODO remove */ int GPU_texture_opengl_bindcode(const GPUTexture *tex) { - return tex->bindcode; + return reinterpret_cast(tex)->gl_bindcode_get(); } -void GPU_texture_attach_framebuffer(GPUTexture *tex, GPUFrameBuffer *fb, int attachment) +void GPU_texture_attach_framebuffer(GPUTexture *tex_, GPUFrameBuffer *fb, int attachment) { + /* TODO cleanup casts */ + Texture *tex = reinterpret_cast(tex_); for (int i = 0; i < GPU_TEX_MAX_FBO_ATTACHED; i++) { if (tex->fb[i] == NULL) { - tex->fb[i] = fb; - tex->fb_attachment[i] = attachment; + tex->fb[i] = reinterpret_cast(fb); + tex->fb_attachment[i] = (GPUAttachmentType)attachment; return; } } @@ -2233,10 +677,12 @@ void GPU_texture_attach_framebuffer(GPUTexture *tex, GPUFrameBuffer *fb, int att } /* Return previous attachment point */ -void GPU_texture_detach_framebuffer(GPUTexture *tex, GPUFrameBuffer *fb) +void GPU_texture_detach_framebuffer(GPUTexture *tex_, GPUFrameBuffer *fb) { + /* TODO cleanup casts */ + Texture *tex = reinterpret_cast(tex_); for (int i = 0; i < GPU_TEX_MAX_FBO_ATTACHED; i++) { - if (tex->fb[i] == fb) { + if (tex->fb[i] == reinterpret_cast(fb)) { tex->fb[i] = NULL; return; } @@ -2245,39 +691,21 @@ void GPU_texture_detach_framebuffer(GPUTexture *tex, GPUFrameBuffer *fb) } /* Return attachment type for the given framebuffer or -1 if not attached. */ -int GPU_texture_framebuffer_attachment_get(GPUTexture *tex, GPUFrameBuffer *fb) +int GPU_texture_framebuffer_attachment_get(GPUTexture *tex_, GPUFrameBuffer *fb) { + /* TODO cleanup casts */ + Texture *tex = reinterpret_cast(tex_); for (int i = 0; i < GPU_TEX_MAX_FBO_ATTACHED; i++) { - if (tex->fb[i] == fb) { + if (tex->fb[i] == reinterpret_cast(fb)) { return tex->fb_attachment[i]; } } return -1; } -void GPU_texture_get_mipmap_size(GPUTexture *tex, int lvl, int *size) +void GPU_texture_get_mipmap_size(GPUTexture *tex, int lvl, int *r_size) { - /* TODO assert if lvl is below the limit of 1px in each dimension. */ - int div = 1 << lvl; - size[0] = max_ii(1, tex->w / div); - - if (tex->target == GL_TEXTURE_1D_ARRAY) { - size[1] = tex->h; - } - else if (tex->h > 0) { - size[1] = max_ii(1, tex->h / div); - } - - if (GPU_texture_array(tex)) { - size[2] = tex->d; - /* Return the number of face layers. */ - if (GPU_texture_cube(tex)) { - size[2] *= 6; - } - } - else if (tex->d > 0) { - size[2] = max_ii(1, tex->d / div); - } + return reinterpret_cast(tex)->mip_size_get(lvl, r_size); } /** \} */ @@ -2291,63 +719,17 @@ void GPU_texture_get_mipmap_size(GPUTexture *tex, int lvl, int *size) void GPU_samplers_init(void) { - float max_anisotropy = 1.0f; - if (GLEW_EXT_texture_filter_anisotropic) { - glGetFloatv(GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT, &max_anisotropy); - } - - glGenSamplers(GPU_SAMPLER_MAX, GG.samplers); - for (int i = 0; i < GPU_SAMPLER_MAX; i++) { - eGPUSamplerState state = static_cast(i); - GLenum clamp_type = (state & GPU_SAMPLER_CLAMP_BORDER) ? GL_CLAMP_TO_BORDER : GL_CLAMP_TO_EDGE; - GLenum wrap_s = (state & GPU_SAMPLER_REPEAT_S) ? GL_REPEAT : clamp_type; - GLenum wrap_t = (state & GPU_SAMPLER_REPEAT_T) ? GL_REPEAT : clamp_type; - GLenum wrap_r = (state & GPU_SAMPLER_REPEAT_R) ? GL_REPEAT : clamp_type; - GLenum mag_filter = (state & GPU_SAMPLER_FILTER) ? GL_LINEAR : GL_NEAREST; - GLenum min_filter = (state & GPU_SAMPLER_FILTER) ? - ((state & GPU_SAMPLER_MIPMAP) ? GL_LINEAR_MIPMAP_LINEAR : GL_LINEAR) : - ((state & GPU_SAMPLER_MIPMAP) ? GL_NEAREST_MIPMAP_LINEAR : GL_NEAREST); - GLenum compare_mode = (state & GPU_SAMPLER_COMPARE) ? GL_COMPARE_REF_TO_TEXTURE : GL_NONE; - /* TODO(fclem) Anisotropic level should be a render engine parameter. */ - float aniso_filter = ((state & GPU_SAMPLER_MIPMAP) && (state & GPU_SAMPLER_ANISO)) ? - max_ff(max_anisotropy, U.anisotropic_filter) : - 1.0f; - - glSamplerParameteri(GG.samplers[i], GL_TEXTURE_WRAP_S, wrap_s); - glSamplerParameteri(GG.samplers[i], GL_TEXTURE_WRAP_T, wrap_t); - glSamplerParameteri(GG.samplers[i], GL_TEXTURE_WRAP_R, wrap_r); - glSamplerParameteri(GG.samplers[i], GL_TEXTURE_MIN_FILTER, min_filter); - glSamplerParameteri(GG.samplers[i], GL_TEXTURE_MAG_FILTER, mag_filter); - glSamplerParameteri(GG.samplers[i], GL_TEXTURE_COMPARE_MODE, compare_mode); - glSamplerParameteri(GG.samplers[i], GL_TEXTURE_COMPARE_FUNC, GL_LEQUAL); - if (GLEW_EXT_texture_filter_anisotropic) { - glSamplerParameterf(GG.samplers[i], GL_TEXTURE_MAX_ANISOTROPY_EXT, aniso_filter); - } - - /** Other states are left to default: - * - GL_TEXTURE_BORDER_COLOR is {0, 0, 0, 0}. - * - GL_TEXTURE_MIN_LOD is -1000. - * - GL_TEXTURE_MAX_LOD is 1000. - * - GL_TEXTURE_LOD_BIAS is 0.0f. - **/ - } - - /* Custom sampler for icons. */ - glGenSamplers(1, &GG.icon_sampler); - glSamplerParameteri(GG.icon_sampler, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST); - glSamplerParameteri(GG.icon_sampler, GL_TEXTURE_MAG_FILTER, GL_LINEAR); - glSamplerParameterf(GG.icon_sampler, GL_TEXTURE_LOD_BIAS, -0.5f); + /* TODO(fclem) port samplers to GLTextures. */ } -void GPU_sampler_icon_bind(int unit) +void GPU_sampler_icon_bind(int UNUSED(unit)) { - glBindSampler(unit, GG.icon_sampler); + /* TODO(fclem) port samplers to GLTextures. */ } void GPU_samplers_free(void) { - glDeleteSamplers(GPU_SAMPLER_MAX, GG.samplers); - glDeleteSamplers(1, &GG.icon_sampler); + /* TODO(fclem) port samplers to GLTextures. */ } /** \} */ diff --git a/source/blender/gpu/intern/gpu_texture_private.hh b/source/blender/gpu/intern/gpu_texture_private.hh index 5eeb3d9e0f0..7b5aed9562e 100644 --- a/source/blender/gpu/intern/gpu_texture_private.hh +++ b/source/blender/gpu/intern/gpu_texture_private.hh @@ -30,28 +30,32 @@ namespace blender { namespace gpu { -typedef enum eGPUTextureFlag { - GPU_TEXFORMAT_DEPTH = (1 << 0), - GPU_TEXFORMAT_STENCIL = (1 << 1), - GPU_TEXFORMAT_INTEGER = (1 << 2), - GPU_TEXFORMAT_FLOAT = (1 << 3), - - GPU_TEXTURE_1D = (1 << 10), - GPU_TEXTURE_2D = (1 << 11), - GPU_TEXTURE_3D = (1 << 12), - GPU_TEXTURE_CUBE = (1 << 13), - GPU_TEXTURE_ARRAY = (1 << 14), - GPU_TEXTURE_BUFFER = (1 << 15), +typedef enum eGPUTextureFormatFlag { + GPU_FORMAT_DEPTH = (1 << 0), + GPU_FORMAT_STENCIL = (1 << 1), + GPU_FORMAT_INTEGER = (1 << 2), + GPU_FORMAT_FLOAT = (1 << 3), + GPU_FORMAT_COMPRESSED = (1 << 4), + + GPU_FORMAT_DEPTH_STENCIL = (GPU_FORMAT_DEPTH | GPU_FORMAT_STENCIL), +} eGPUTextureFormatFlag; + +ENUM_OPERATORS(eGPUTextureFormatFlag) + +typedef enum eGPUTextureType { + GPU_TEXTURE_1D = (1 << 0), + GPU_TEXTURE_2D = (1 << 1), + GPU_TEXTURE_3D = (1 << 2), + GPU_TEXTURE_CUBE = (1 << 3), + GPU_TEXTURE_ARRAY = (1 << 4), + GPU_TEXTURE_BUFFER = (1 << 5), GPU_TEXTURE_1D_ARRAY = (GPU_TEXTURE_1D | GPU_TEXTURE_ARRAY), GPU_TEXTURE_2D_ARRAY = (GPU_TEXTURE_2D | GPU_TEXTURE_ARRAY), GPU_TEXTURE_CUBE_ARRAY = (GPU_TEXTURE_CUBE | GPU_TEXTURE_ARRAY), +} eGPUTextureType; - GPU_TEXTURE_TARGET = (GPU_TEXTURE_1D | GPU_TEXTURE_2D | GPU_TEXTURE_3D | GPU_TEXTURE_CUBE | - GPU_TEXTURE_ARRAY), -} eGPUTextureFlag; - -ENUM_OPERATORS(eGPUTextureFlag) +ENUM_OPERATORS(eGPUTextureType) #ifdef DEBUG # define DEBUG_NAME_LEN 64 @@ -60,23 +64,14 @@ ENUM_OPERATORS(eGPUTextureFlag) #endif /* Maximum number of FBOs a texture can be attached to. */ -#define GPU_TEX_MAX_FBO_ATTACHED 12 +#define GPU_TEX_MAX_FBO_ATTACHED 13 class Texture { public: - /** Width & Height & Depth. */ - int w = 0, h = 0, d = 0; - /** Number of color/alpha channels. */ - int components = 0; - /** Internal data format and it's characteristics. */ - eGPUTextureFormat format; - eGPUTextureFlag flag; /** Internal Sampler state. */ - eGPUSamplerState sampler_state; - /** Number of mipmaps this texture has. */ - int mipmaps = 0; + eGPUSamplerState sampler_state = GPU_SAMPLER_DEFAULT; /** Reference counter. */ - int refcount = 0; + int refcount = 1; /** Width & Height (of source data), optional. */ int src_w = 0, src_h = 0; /** Framebuffer references to update on deletion. */ @@ -84,6 +79,20 @@ class Texture { FrameBuffer *fb[GPU_TEX_MAX_FBO_ATTACHED]; protected: + /* ---- Texture format (immutable after init). ---- */ + /** Width & Height & Depth. For cubemap arrays, d is number of facelayers. */ + int w_, h_, d_; + /** Internal data format. */ + eGPUTextureFormat format_; + /** Format caracteristics. */ + eGPUTextureFormatFlag format_flag_; + /** Texture type. */ + eGPUTextureType type_; + + /** Number of mipmaps this texture has (Max miplvl). */ + /* TODO(fclem) Should become immutable and the need for mipmaps should be specified upfront. */ + int mipmaps_ = -1; + /** For debugging */ char name_[DEBUG_NAME_LEN]; @@ -91,22 +100,110 @@ class Texture { Texture(const char *name); virtual ~Texture(); - virtual void bind(int slot) = 0; - virtual void update(void *data) = 0; - virtual void update_sub(void *data, int offset[3], int size[3]) = 0; + /* Return true on success. */ + bool init_1D(int w, int layers, eGPUTextureFormat format); + bool init_2D(int w, int h, int layers, eGPUTextureFormat format); + bool init_3D(int w, int h, int d, eGPUTextureFormat format); + bool init_cubemap(int w, int layers, eGPUTextureFormat format); + bool init_buffer(GPUVertBuf *vbo, eGPUTextureFormat format); + virtual void generate_mipmap(void) = 0; virtual void copy_to(Texture *tex) = 0; + virtual void clear(eGPUDataFormat format, const void *data) = 0; + virtual void swizzle_set(const char swizzle_mask[4]) = 0; + virtual void mip_range_set(int min, int max) = 0; + virtual void *read(int mip, eGPUDataFormat format) = 0; + + void attach_to(FrameBuffer *fb); + void update(eGPUDataFormat format, const void *data); + void update_mip(int mip, eGPUDataFormat format, const void *data); - virtual void swizzle_set(char swizzle_mask[4]) = 0; + virtual void update_sub( + int mip, int offset[3], int extent[3], eGPUDataFormat format, const void *data) = 0; /* TODO(fclem) Legacy. Should be removed at some point. */ - virtual uint gl_bindcode_get(void) = 0; + virtual uint gl_bindcode_get(void) const = 0; - void attach_to(FrameBuffer *fb); + int width_get(void) const + { + return w_; + } + int height_get(void) const + { + return h_; + } + int depth_get(void) const + { + return d_; + } + void mip_size_get(int mip, int r_size[3]) const + { + /* TODO assert if lvl is below the limit of 1px in each dimension. */ + int div = 1 << mip; + r_size[0] = max_ii(1, w_ / div); + + if (type_ == GPU_TEXTURE_1D_ARRAY) { + r_size[1] = h_; + } + else if (h_ > 0) { + r_size[1] = max_ii(1, h_ / div); + } + + if (type_ & (GPU_TEXTURE_ARRAY | GPU_TEXTURE_CUBE)) { + r_size[2] = d_; + } + else if (d_ > 0) { + r_size[2] = max_ii(1, d_ / div); + } + } + + int mip_width_get(int mip) const + { + return max_ii(1, w_ / (1 << mip)); + } + int mip_height_get(int mip) const + { + return (type_ == GPU_TEXTURE_1D_ARRAY) ? h_ : max_ii(1, h_ / (1 << mip)); + } + int mip_depth_get(int mip) const + { + return (type_ & (GPU_TEXTURE_ARRAY | GPU_TEXTURE_CUBE)) ? d_ : max_ii(1, d_ / (1 << mip)); + } + + /* Return number of dimension taking the array type into account. */ + int dimensions_count(void) const + { + const int array = (type_ & GPU_TEXTURE_ARRAY) ? 1 : 0; + switch (type_ & ~GPU_TEXTURE_ARRAY) { + case GPU_TEXTURE_BUFFER: + return 1; + case GPU_TEXTURE_1D: + return 1 + array; + case GPU_TEXTURE_2D: + return 2 + array; + case GPU_TEXTURE_CUBE: + case GPU_TEXTURE_3D: + default: + return 3; + } + } + + eGPUTextureFormat format_get(void) const + { + return format_; + } + eGPUTextureFormatFlag format_flag_get(void) const + { + return format_flag_; + } + eGPUTextureType type_get(void) const + { + return type_; + } GPUAttachmentType attachment_type(int slot) const { - switch (format) { + switch (format_) { case GPU_DEPTH_COMPONENT32F: case GPU_DEPTH_COMPONENT24: case GPU_DEPTH_COMPONENT16: @@ -120,9 +217,233 @@ class Texture { return GPU_FB_COLOR_ATTACHMENT0 + slot; } } + + protected: + virtual bool init_internal(void) = 0; + virtual bool init_internal(GPUVertBuf *vbo) = 0; }; #undef DEBUG_NAME_LEN +inline size_t to_bytesize(eGPUTextureFormat format) +{ + switch (format) { + case GPU_RGBA32F: + return 32; + case GPU_RG32F: + case GPU_RGBA16F: + case GPU_RGBA16: + return 16; + case GPU_RGB16F: + return 12; + case GPU_DEPTH32F_STENCIL8: /* 32-bit depth, 8 bits stencil, and 24 unused bits. */ + return 8; + case GPU_RG16F: + case GPU_RG16I: + case GPU_RG16UI: + case GPU_RG16: + case GPU_DEPTH24_STENCIL8: + case GPU_DEPTH_COMPONENT32F: + case GPU_RGBA8UI: + case GPU_RGBA8: + case GPU_SRGB8_A8: + case GPU_R11F_G11F_B10F: + case GPU_R32F: + case GPU_R32UI: + case GPU_R32I: + return 4; + case GPU_DEPTH_COMPONENT24: + return 3; + case GPU_DEPTH_COMPONENT16: + case GPU_R16F: + case GPU_R16UI: + case GPU_R16I: + case GPU_RG8: + case GPU_R16: + return 2; + case GPU_R8: + case GPU_R8UI: + return 1; + case GPU_SRGB8_A8_DXT1: + case GPU_SRGB8_A8_DXT3: + case GPU_SRGB8_A8_DXT5: + case GPU_RGBA8_DXT1: + case GPU_RGBA8_DXT3: + case GPU_RGBA8_DXT5: + return 1; /* Incorrect but actual size is fractional. */ + default: + BLI_assert(!"Texture format incorrect or unsupported\n"); + return 0; + } +} + +inline size_t to_block_size(eGPUTextureFormat data_type) +{ + switch (data_type) { + case GPU_SRGB8_A8_DXT1: + case GPU_RGBA8_DXT1: + return 8; + case GPU_SRGB8_A8_DXT3: + case GPU_SRGB8_A8_DXT5: + case GPU_RGBA8_DXT3: + case GPU_RGBA8_DXT5: + return 16; + default: + BLI_assert(!"Texture format is not a compressed format\n"); + return 0; + } +} + +inline eGPUTextureFormatFlag to_format_flag(eGPUTextureFormat format) +{ + switch (format) { + case GPU_DEPTH_COMPONENT24: + case GPU_DEPTH_COMPONENT16: + case GPU_DEPTH_COMPONENT32F: + return GPU_FORMAT_DEPTH; + case GPU_DEPTH24_STENCIL8: + case GPU_DEPTH32F_STENCIL8: + return GPU_FORMAT_DEPTH_STENCIL; + case GPU_R8UI: + case GPU_RG16I: + case GPU_R16I: + case GPU_RG16UI: + case GPU_R16UI: + case GPU_R32UI: + return GPU_FORMAT_INTEGER; + case GPU_SRGB8_A8_DXT1: + case GPU_SRGB8_A8_DXT3: + case GPU_SRGB8_A8_DXT5: + case GPU_RGBA8_DXT1: + case GPU_RGBA8_DXT3: + case GPU_RGBA8_DXT5: + return GPU_FORMAT_COMPRESSED; + default: + return GPU_FORMAT_FLOAT; + } +} + +inline int to_component_len(eGPUTextureFormat format) +{ + switch (format) { + case GPU_RGBA8: + case GPU_RGBA8UI: + case GPU_RGBA16F: + case GPU_RGBA16: + case GPU_RGBA32F: + case GPU_SRGB8_A8: + return 4; + case GPU_RGB16F: + case GPU_R11F_G11F_B10F: + return 3; + case GPU_RG8: + case GPU_RG16: + case GPU_RG16F: + case GPU_RG16I: + case GPU_RG16UI: + case GPU_RG32F: + return 2; + default: + return 1; + } +} + +inline size_t to_bytesize(eGPUTextureFormat tex_format, eGPUDataFormat data_format) +{ + switch (data_format) { + case GPU_DATA_UNSIGNED_BYTE: + return 1 * to_component_len(tex_format); + case GPU_DATA_FLOAT: + case GPU_DATA_INT: + case GPU_DATA_UNSIGNED_INT: + return 4 * to_component_len(tex_format); + case GPU_DATA_UNSIGNED_INT_24_8: + case GPU_DATA_10_11_11_REV: + return 4; + default: + BLI_assert(!"Data format incorrect or unsupported\n"); + return 0; + } +} + +/* Definitely not complete, edit according to the gl specification. */ +inline bool validate_data_format(eGPUTextureFormat tex_format, eGPUDataFormat data_format) +{ + switch (tex_format) { + case GPU_DEPTH_COMPONENT24: + case GPU_DEPTH_COMPONENT16: + case GPU_DEPTH_COMPONENT32F: + return data_format == GPU_DATA_FLOAT; + case GPU_DEPTH24_STENCIL8: + case GPU_DEPTH32F_STENCIL8: + return data_format == GPU_DATA_UNSIGNED_INT_24_8; + case GPU_R8UI: + case GPU_R16UI: + case GPU_RG16UI: + case GPU_R32UI: + return data_format == GPU_DATA_UNSIGNED_INT; + case GPU_RG16I: + case GPU_R16I: + return data_format == GPU_DATA_INT; + case GPU_R8: + case GPU_RG8: + case GPU_RGBA8: + case GPU_RGBA8UI: + case GPU_SRGB8_A8: + return ELEM(data_format, GPU_DATA_UNSIGNED_BYTE, GPU_DATA_FLOAT); + case GPU_R11F_G11F_B10F: + return ELEM(data_format, GPU_DATA_10_11_11_REV, GPU_DATA_FLOAT); + default: + return data_format == GPU_DATA_FLOAT; + } +} + +/* Definitely not complete, edit according to the gl specification. */ +inline eGPUDataFormat to_data_format(eGPUTextureFormat tex_format) +{ + switch (tex_format) { + case GPU_DEPTH_COMPONENT24: + case GPU_DEPTH_COMPONENT16: + case GPU_DEPTH_COMPONENT32F: + return GPU_DATA_FLOAT; + case GPU_DEPTH24_STENCIL8: + case GPU_DEPTH32F_STENCIL8: + return GPU_DATA_UNSIGNED_INT_24_8; + case GPU_R8UI: + case GPU_R16UI: + case GPU_RG16UI: + case GPU_R32UI: + return GPU_DATA_UNSIGNED_INT; + case GPU_RG16I: + case GPU_R16I: + return GPU_DATA_INT; + case GPU_R8: + case GPU_RG8: + case GPU_RGBA8: + case GPU_RGBA8UI: + case GPU_SRGB8_A8: + return GPU_DATA_UNSIGNED_BYTE; + case GPU_R11F_G11F_B10F: + return GPU_DATA_10_11_11_REV; + default: + return GPU_DATA_FLOAT; + } +} + +inline eGPUFrameBufferBits to_framebuffer_bits(eGPUTextureFormat tex_format) +{ + switch (tex_format) { + case GPU_DEPTH_COMPONENT24: + case GPU_DEPTH_COMPONENT16: + case GPU_DEPTH_COMPONENT32F: + return GPU_DEPTH_BIT; + case GPU_DEPTH24_STENCIL8: + case GPU_DEPTH32F_STENCIL8: + return GPU_DEPTH_BIT | GPU_STENCIL_BIT; + default: + return GPU_COLOR_BIT; + } +} + } // namespace gpu } // namespace blender diff --git a/source/blender/gpu/opengl/gl_context.hh b/source/blender/gpu/opengl/gl_context.hh index bc7e2060804..9e6359fabad 100644 --- a/source/blender/gpu/opengl/gl_context.hh +++ b/source/blender/gpu/opengl/gl_context.hh @@ -30,6 +30,8 @@ #include "BLI_set.hh" #include "BLI_vector.hh" +#include "gl_state.hh" + #include "glew-mx.h" #include @@ -83,6 +85,12 @@ class GLContext : public GPUContext { void activate(void) override; void deactivate(void) override; + static inline GLStateManager *state_manager_active_get() + { + GLContext *ctx = static_cast(GPU_context_active_get()); + return static_cast(ctx->state_manager); + }; + /* TODO(fclem) these needs to become private. */ public: void orphans_add(Vector &orphan_list, std::mutex &list_mutex, GLuint id); diff --git a/source/blender/gpu/opengl/gl_debug.cc b/source/blender/gpu/opengl/gl_debug.cc index fc4dc3db2b6..da1f40e6417 100644 --- a/source/blender/gpu/opengl/gl_debug.cc +++ b/source/blender/gpu/opengl/gl_debug.cc @@ -202,6 +202,11 @@ void check_gl_resources(const char *info) } } +void raise_gl_error(const char *msg) +{ + debug_callback(0, GL_DEBUG_TYPE_ERROR, 0, GL_DEBUG_SEVERITY_HIGH, 0, msg, NULL); +} + /** \} */ } // namespace blender::gpu::debug diff --git a/source/blender/gpu/opengl/gl_debug.hh b/source/blender/gpu/opengl/gl_debug.hh index dd98505ebc1..5537147d0fe 100644 --- a/source/blender/gpu/opengl/gl_debug.hh +++ b/source/blender/gpu/opengl/gl_debug.hh @@ -37,6 +37,7 @@ namespace debug { # define GL_CHECK_RESOURCES(info) #endif +void raise_gl_error(const char *info); void check_gl_error(const char *info); void check_gl_resources(const char *info); void init_gl_callbacks(void); diff --git a/source/blender/gpu/opengl/gl_state.cc b/source/blender/gpu/opengl/gl_state.cc index 8f01ff13486..1fd39cf80eb 100644 --- a/source/blender/gpu/opengl/gl_state.cc +++ b/source/blender/gpu/opengl/gl_state.cc @@ -28,9 +28,11 @@ #include "gl_context.hh" #include "gl_framebuffer.hh" +#include "gl_texture.hh" + #include "gl_state.hh" -using namespace blender::gpu; +namespace blender::gpu { /* -------------------------------------------------------------------- */ /** \name GLStateManager @@ -69,6 +71,7 @@ void GLStateManager::apply_state(void) { this->set_state(this->state); this->set_mutable_state(this->mutable_state); + this->texture_bind_apply(); active_fb->apply_state(); }; @@ -419,3 +422,85 @@ void GLStateManager::set_blend(const eGPUBlend value) } /** \} */ + +/* -------------------------------------------------------------------- */ +/** \name Texture state managment + * \{ */ + +void GLStateManager::texture_bind(Texture *tex_, eGPUSamplerState sampler, int unit) +{ + BLI_assert(unit < GPU_max_textures()); + GLTexture *tex = static_cast(tex_); + targets_[unit] = tex->target_; + textures_[unit] = tex->tex_id_; + samplers_[unit] = sampler; + tex->is_bound_ = true; + dirty_texture_binds_ |= 1 << unit; +} + +/* Bind the texture to slot 0 for editing purpose. Used by legacy pipeline. */ +void GLStateManager::texture_bind_temp(GLTexture *tex) +{ + // BLI_assert(!GLEW_ARB_direct_state_access); + glActiveTexture(GL_TEXTURE0); + glBindTexture(tex->target_, tex->tex_id_); + /* Will reset the first texture that was originaly bound to slot 0 back before drawing. */ + dirty_texture_binds_ |= 1; + /* NOTE: This might leave this texture attached to this target even after update. + * In practice it is not causing problems as we have incorrect binding detection + * at higher level. */ +} + +void GLStateManager::texture_unbind(Texture *tex_) +{ + GLTexture *tex = static_cast(tex_); + if (!tex->is_bound_) { + return; + } + + GLuint tex_id = tex->tex_id_; + for (int i = 0; i < ARRAY_SIZE(textures_); i++) { + if (textures_[i] == tex_id) { + textures_[i] = 0; + dirty_texture_binds_ |= 1 << i; + } + } + tex->is_bound_ = false; +} + +void GLStateManager::texture_unbind_all(void) +{ + for (int i = 0; i < ARRAY_SIZE(textures_); i++) { + if (textures_[i] != 0) { + textures_[i] = 0; + dirty_texture_binds_ |= 1 << i; + } + } + this->texture_bind_apply(); +} + +void GLStateManager::texture_bind_apply(void) +{ + if (dirty_texture_binds_ == 0) { + return; + } + + if (false) { + /* TODO multibind */ + } + else { + uint64_t dirty_bind = dirty_texture_binds_; + for (int unit = 0; dirty_bind != 0; dirty_bind >>= 1, unit++) { + if (dirty_bind & 1) { + glActiveTexture(GL_TEXTURE0 + unit); + glBindTexture(targets_[unit], textures_[unit]); + // glBindSampler(unit, samplers_[unit]); + } + } + dirty_texture_binds_ = 0; + } +} + +/** \} */ + +} // namespace blender::gpu \ No newline at end of file diff --git a/source/blender/gpu/opengl/gl_state.hh b/source/blender/gpu/opengl/gl_state.hh index c25e384fcd7..2befad690f0 100644 --- a/source/blender/gpu/opengl/gl_state.hh +++ b/source/blender/gpu/opengl/gl_state.hh @@ -20,6 +20,8 @@ * \ingroup gpu */ +#pragma once + #include "MEM_guardedalloc.h" #include "BLI_utildefines.h" @@ -32,6 +34,7 @@ namespace blender { namespace gpu { class GLFrameBuffer; +class GLTexture; /** * State manager keeping track of the draw state and applying it before drawing. @@ -49,11 +52,28 @@ class GLStateManager : public GPUStateManager { /** Limits. */ float line_width_range_[2]; + /** Texture state: + * We keep the full stack of textures and sampler bounds to use multi bind, and to be able to + * edit and restore texture binds on the fly without querying the context. + * Also this allows us to keep track of textures bounds to many texture units. + * Keep the targets to know what target to set to 0 for unbinding (legacy). + * Init first target to GL_TEXTURE_2D for texture_bind_temp to work. + */ + GLuint targets_[64] = {GL_TEXTURE_2D}; + GLuint textures_[64] = {0}; + GLuint samplers_[64] = {0}; + uint64_t dirty_texture_binds_ = 0; + public: GLStateManager(); void apply_state(void) override; + void texture_bind(Texture *tex, eGPUSamplerState sampler, int unit) override; + void texture_bind_temp(GLTexture *tex); + void texture_unbind(Texture *tex) override; + void texture_unbind_all(void) override; + private: static void set_write_mask(const eGPUWriteMask value); static void set_depth_test(const eGPUDepthTest value); @@ -70,6 +90,8 @@ class GLStateManager : public GPUStateManager { void set_state(const GPUState &state); void set_mutable_state(const GPUStateMutable &state); + void texture_bind_apply(void); + MEM_CXX_CLASS_ALLOC_FUNCS("GLStateManager") }; diff --git a/source/blender/gpu/opengl/gl_texture.cc b/source/blender/gpu/opengl/gl_texture.cc index aaad281ed4e..7c72dc6a5c3 100644 --- a/source/blender/gpu/opengl/gl_texture.cc +++ b/source/blender/gpu/opengl/gl_texture.cc @@ -23,7 +23,12 @@ #include "BKE_global.h" +#include "GPU_extensions.h" +#include "GPU_framebuffer.h" + #include "gl_backend.hh" +#include "gl_debug.hh" +#include "gl_state.hh" #include "gl_texture.hh" @@ -38,23 +43,153 @@ GLTexture::GLTexture(const char *name) : Texture(name) BLI_assert(GPU_context_active_get() != NULL); glGenTextures(1, &tex_id_); +} + +GLTexture::~GLTexture() +{ + if (framebuffer_) { + GPU_framebuffer_free(framebuffer_); + } + GPUContext *ctx = GPU_context_active_get(); + if (ctx != NULL && is_bound_) { + /* This avoid errors when the texture is still inside the bound texture array. */ + ctx->state_manager->texture_unbind(this); + } + GLBackend::get()->tex_free(tex_id_); +} + +/* Return true on success. */ +bool GLTexture::init_internal(void) +{ + if ((format_ == GPU_DEPTH24_STENCIL8) && GPU_depth_blitting_workaround()) { + /* MacOS + Radeon Pro fails to blit depth on GPU_DEPTH24_STENCIL8 + * but works on GPU_DEPTH32F_STENCIL8. */ + format_ = GPU_DEPTH32F_STENCIL8; + } + + if ((type_ == GPU_TEXTURE_CUBE_ARRAY) && !GPU_arb_texture_cube_map_array_is_supported()) { + debug::raise_gl_error("Attempt to create a cubemap array without hardware support!"); + return false; + } + + target_ = to_gl_target(type_); + + /* TODO(fclem) Proxy check. */ + + this->ensure_mipmaps(0); + + /* Avoid issue with incomplete textures. */ + if (false) { + /* TODO(fclem) Direct State Access. */ + } + else { + GLContext::state_manager_active_get()->texture_bind_temp(this); + glTexParameteri(target_, GL_TEXTURE_MIN_FILTER, GL_NEAREST); + } #ifndef __APPLE__ if ((G.debug & G_DEBUG_GPU) && (GLEW_VERSION_4_3 || GLEW_KHR_debug)) { char sh_name[64]; - SNPRINTF(sh_name, "Texture-%s", name); + SNPRINTF(sh_name, "Texture-%s", name_); + /* Binding before setting the label is needed on some drivers. */ glObjectLabel(GL_TEXTURE, tex_id_, -1, sh_name); } #endif + + GL_CHECK_ERROR("Post-texture creation"); + return true; } -GLTexture::~GLTexture() +/* Return true on success. */ +bool GLTexture::init_internal(GPUVertBuf *vbo) { - GLBackend::get()->tex_free(tex_id_); + target_ = to_gl_target(type_); + + GLenum internal_format = to_gl_internal_format(format_); + + if (false) { + /* TODO(fclem) Direct State Access. */ + } + else { + GLContext::state_manager_active_get()->texture_bind_temp(this); + glTexBuffer(target_, internal_format, vbo->vbo_id); + } + +#ifndef __APPLE__ + if ((G.debug & G_DEBUG_GPU) && (GLEW_VERSION_4_3 || GLEW_KHR_debug)) { + char sh_name[64]; + SNPRINTF(sh_name, "Texture-%s", name_); + /* Binding before setting the label is needed on some drivers. */ + glObjectLabel(GL_TEXTURE, tex_id_, -1, sh_name); + } +#endif + + GL_CHECK_ERROR("Post-texture buffer creation"); + return true; } -void GLTexture::init(void) +/* Will create enough mipmaps up to get to the given level. */ +void GLTexture::ensure_mipmaps(int miplvl) { + int effective_h = (type_ == GPU_TEXTURE_1D_ARRAY) ? 0 : h_; + int effective_d = (type_ != GPU_TEXTURE_3D) ? 0 : d_; + int max_dimension = max_iii(w_, effective_h, effective_d); + int max_miplvl = floor(log2(max_dimension)); + miplvl = min_ii(miplvl, max_miplvl); + + while (mipmaps_ < miplvl) { + int mip = ++mipmaps_; + const int dimensions = this->dimensions_count(); + + int w = mip_width_get(mip); + int h = mip_height_get(mip); + int d = mip_depth_get(mip); + GLenum internal_format = to_gl_internal_format(format_); + GLenum gl_format = to_gl_data_format(format_); + GLenum gl_type = to_gl(to_data_format(format_)); + + GLContext::state_manager_active_get()->texture_bind_temp(this); + + if (type_ == GPU_TEXTURE_CUBE) { + for (int i = 0; i < d; i++) { + GLenum target = GL_TEXTURE_CUBE_MAP_POSITIVE_X + i; + glTexImage2D(target, mip, internal_format, w, h, 0, gl_format, gl_type, NULL); + } + } + else if (format_flag_ & GPU_FORMAT_COMPRESSED) { + size_t size = ((w + 3) / 4) * ((h + 3) / 4) * to_block_size(format_); + switch (dimensions) { + default: + case 1: + glCompressedTexImage1D(target_, mip, internal_format, w, 0, size, NULL); + break; + case 2: + glCompressedTexImage2D(target_, mip, internal_format, w, h, 0, size, NULL); + break; + case 3: + glCompressedTexImage3D(target_, mip, internal_format, w, h, d, 0, size, NULL); + break; + } + } + else { + switch (dimensions) { + default: + case 1: + glTexImage1D(target_, mip, internal_format, w, 0, gl_format, gl_type, NULL); + break; + case 2: + glTexImage2D(target_, mip, internal_format, w, h, 0, gl_format, gl_type, NULL); + break; + case 3: + glTexImage3D(target_, mip, internal_format, w, h, d, 0, gl_format, gl_type, NULL); + break; + } + } + + GL_CHECK_ERROR("Post-mipmap creation"); + } + + this->mip_range_set(0, mipmaps_); } /** \} */ @@ -63,34 +198,234 @@ void GLTexture::init(void) /** \name Operations * \{ */ -void GLTexture::bind(int /*slot*/) +void GLTexture::update_sub( + int mip, int offset[3], int extent[3], eGPUDataFormat type, const void *data) { + BLI_assert(validate_data_format(format_, type)); + BLI_assert(data != NULL); + + this->ensure_mipmaps(mip); + + if (mip > mipmaps_) { + debug::raise_gl_error("Updating a miplvl on a texture too small to have this many levels."); + return; + } + + const int dimensions = this->dimensions_count(); + GLenum gl_format = to_gl_data_format(format_); + GLenum gl_type = to_gl(type); + + GLContext::state_manager_active_get()->texture_bind_temp(this); + + if (true && type_ == GPU_TEXTURE_CUBE) { + /* TODO(fclem) bypass if direct state access is available. */ + /* Workaround when ARB_direct_state_access is not available. */ + for (int i = 0; i < extent[2]; i++) { + GLenum target = GL_TEXTURE_CUBE_MAP_POSITIVE_X + offset[2] + i; + glTexSubImage2D(target, mip, UNPACK2(offset), UNPACK2(extent), gl_format, gl_type, data); + } + } + else if (format_flag_ & GPU_FORMAT_COMPRESSED) { + size_t size = ((extent[0] + 3) / 4) * ((extent[1] + 3) / 4) * to_block_size(format_); + switch (dimensions) { + default: + case 1: + glCompressedTexSubImage1D(target_, mip, offset[0], extent[0], gl_format, size, data); + break; + case 2: + glCompressedTexSubImage2D( + target_, mip, UNPACK2(offset), UNPACK2(extent), gl_format, size, data); + break; + case 3: + glCompressedTexSubImage3D( + target_, mip, UNPACK3(offset), UNPACK3(extent), gl_format, size, data); + break; + } + } + else { + switch (dimensions) { + default: + case 1: + glTexSubImage1D(target_, mip, offset[0], extent[0], gl_format, gl_type, data); + break; + case 2: + glTexSubImage2D(target_, mip, UNPACK2(offset), UNPACK2(extent), gl_format, gl_type, data); + break; + case 3: + glTexSubImage3D(target_, mip, UNPACK3(offset), UNPACK3(extent), gl_format, gl_type, data); + break; + } + } + + GL_CHECK_ERROR("Post-update_sub"); } -void GLTexture::update(void * /*data*/) +/** This will create the mipmap images and populate them with filtered data from base level. + * WARNING: Depth textures are not populated but they have their mips correctly defined. + * WARNING: This resets the mipmap range. + */ +void GLTexture::generate_mipmap(void) { + this->ensure_mipmaps(9999); + /* Some drivers have bugs when using glGenerateMipmap with depth textures (see T56789). + * In this case we just create a complete texture with mipmaps manually without + * down-sampling. You must initialize the texture levels using other methods like + * GPU_framebuffer_recursive_downsample(). */ + if (format_flag_ & GPU_FORMAT_DEPTH) { + return; + } + + if (false) { + /* TODO(fclem) Direct State Access. */ + } + else { + /* Downsample from mip 0 using implementation. */ + GLContext::state_manager_active_get()->texture_bind_temp(this); + glGenerateMipmap(target_); + } } -void GLTexture::update_sub(void * /*data*/, int /*offset*/[3], int /*size*/[3]) +void GLTexture::clear(eGPUDataFormat data_format, const void *data) { + BLI_assert(validate_data_format(format_, data_format)); + + if (GLEW_ARB_clear_texture && !(G.debug & G_DEBUG_GPU_FORCE_WORKAROUNDS)) { + int mip = 0; + GLenum gl_format = to_gl_data_format(format_); + GLenum gl_type = to_gl(data_format); + glClearTexImage(tex_id_, mip, gl_format, gl_type, data); + } + else { + /* Fallback for older GL. */ + GPUFrameBuffer *prev_fb = GPU_framebuffer_active_get(); + + FrameBuffer *fb = reinterpret_cast(this->framebuffer_get()); + fb->bind(true); + fb->clear_attachment(this->attachment_type(0), data_format, data); + + GPU_framebuffer_bind(prev_fb); + } } -void GLTexture::generate_mipmap(void) +void GLTexture::copy_to(Texture *dst_) { + GLTexture *dst = static_cast(dst_); + GLTexture *src = this; + + BLI_assert((dst->w_ == src->w_) && (dst->h_ == src->h_) && (dst->d_ == src->d_)); + BLI_assert(dst->format_ == src->format_); + BLI_assert(dst->type_ == src->type_); + /* TODO support array / 3D textures. */ + BLI_assert(dst->d_ == 0); + + if (GLEW_ARB_copy_image && !GPU_texture_copy_workaround()) { + /* Opengl 4.3 */ + int mip = 0; + /* NOTE: mip_size_get() won't override any dimension that is equal to 0. */ + int extent[3] = {1, 1, 1}; + this->mip_size_get(mip, extent); + glCopyImageSubData( + src->tex_id_, target_, mip, 0, 0, 0, dst->tex_id_, target_, mip, 0, 0, 0, UNPACK3(extent)); + } + else { + /* Fallback for older GL. */ + GPU_framebuffer_blit( + src->framebuffer_get(), 0, dst->framebuffer_get(), 0, to_framebuffer_bits(format_)); + } } -void GLTexture::copy_to(Texture * /*tex*/) +void *GLTexture::read(int mip, eGPUDataFormat type) { + BLI_assert(!(format_flag_ & GPU_FORMAT_COMPRESSED)); + BLI_assert(mip <= mipmaps_); + BLI_assert(validate_data_format(format_, type)); + + /* NOTE: mip_size_get() won't override any dimension that is equal to 0. */ + int extent[3] = {1, 1, 1}; + this->mip_size_get(mip, extent); + + size_t sample_len = extent[0] * extent[1] * extent[2]; + size_t sample_size = to_bytesize(format_, type); + size_t texture_size = sample_len * sample_size; + + /* AMD Pro driver have a bug that write 8 bytes past buffer size + * if the texture is big. (see T66573) */ + void *data = MEM_mallocN(texture_size + 8, "GPU_texture_read"); + + GLenum gl_format = to_gl_data_format(format_); + GLenum gl_type = to_gl(type); + + if (false) { + /* TODO(fclem) Direct State Access. */ + /* NOTE: DSA can read GL_TEXTURE_CUBE_MAP directly. */ + } + else { + GLContext::state_manager_active_get()->texture_bind_temp(this); + if (type_ == GPU_TEXTURE_CUBE) { + size_t cube_face_size = texture_size / 6; + char *face_data = (char *)data; + for (int i = 0; i < 6; i++, face_data += cube_face_size) { + glGetTexImage(GL_TEXTURE_CUBE_MAP_POSITIVE_X + i, mip, gl_format, gl_type, face_data); + } + } + else { + glGetTexImage(target_, mip, gl_format, gl_type, data); + } + } + return data; +} + +/** \} */ + +/* -------------------------------------------------------------------- */ +/** \name Getters & setters + * \{ */ + +void GLTexture::swizzle_set(const char swizzle[4]) +{ + GLint gl_swizzle[4] = {(GLint)swizzle_to_gl(swizzle[0]), + (GLint)swizzle_to_gl(swizzle[1]), + (GLint)swizzle_to_gl(swizzle[2]), + (GLint)swizzle_to_gl(swizzle[3])}; + if (false) { + /* TODO(fclem) Direct State Access. */ + } + else { + GLContext::state_manager_active_get()->texture_bind_temp(this); + glTexParameteriv(target_, GL_TEXTURE_SWIZZLE_RGBA, gl_swizzle); + } } -void GLTexture::swizzle_set(char /*swizzle_mask*/[4]) +void GLTexture::mip_range_set(int min, int max) { + BLI_assert(min <= max && min >= 0 && max <= mipmaps_); + if (false) { + /* TODO(fclem) Direct State Access. */ + } + else { + GLContext::state_manager_active_get()->texture_bind_temp(this); + glTexParameteri(target_, GL_TEXTURE_BASE_LEVEL, min); + glTexParameteri(target_, GL_TEXTURE_MAX_LEVEL, max); + } +} + +struct GPUFrameBuffer *GLTexture::framebuffer_get(void) +{ + if (framebuffer_) { + return framebuffer_; + } + BLI_assert(!(type_ & (GPU_TEXTURE_ARRAY | GPU_TEXTURE_CUBE | GPU_TEXTURE_1D | GPU_TEXTURE_3D))); + /* TODO(fclem) cleanup this. Don't use GPU object but blender::gpu ones. */ + GPUTexture *gputex = reinterpret_cast(static_cast(this)); + framebuffer_ = GPU_framebuffer_create(name_); + GPU_framebuffer_texture_attach(framebuffer_, gputex, 0, 0); + return framebuffer_; } /** \} */ /* TODO(fclem) Legacy. Should be removed at some point. */ -uint GLTexture::gl_bindcode_get(void) +uint GLTexture::gl_bindcode_get(void) const { return tex_id_; } diff --git a/source/blender/gpu/opengl/gl_texture.hh b/source/blender/gpu/opengl/gl_texture.hh index 513cef59e85..0e054d7996a 100644 --- a/source/blender/gpu/opengl/gl_texture.hh +++ b/source/blender/gpu/opengl/gl_texture.hh @@ -39,6 +39,8 @@ #include "glew-mx.h" +struct GPUFrameBuffer; + namespace blender { namespace gpu { @@ -53,62 +55,177 @@ class GLContext { #endif class GLTexture : public Texture { + friend class GLStateManager; + private: - /** Texture unit to which this texture is bound. */ - int slot = -1; /** Target to bind the texture to (GL_TEXTURE_1D, GL_TEXTURE_2D, etc...)*/ GLenum target_ = -1; /** opengl identifier for texture. */ GLuint tex_id_ = 0; - /** Legacy workaround for texture copy. */ - GLuint copy_fb = 0; - GPUContext *copy_fb_ctx = NULL; + /** Legacy workaround for texture copy. Created when using framebuffer_get(). */ + struct GPUFrameBuffer *framebuffer_ = NULL; + /** True if this texture is bound to at least one texture unit. */ + /* TODO(fclem) How do we ensure thread safety here? */ + bool is_bound_; public: GLTexture(const char *name); ~GLTexture(); - void bind(int slot) override; - void update(void *data) override; - void update_sub(void *data, int offset[3], int size[3]) override; + void update_sub( + int mip, int offset[3], int extent[3], eGPUDataFormat format, const void *data) override; + void generate_mipmap(void) override; void copy_to(Texture *tex) override; - - void swizzle_set(char swizzle_mask[4]) override; + void clear(eGPUDataFormat format, const void *data) override; + void swizzle_set(const char swizzle_mask[4]) override; + void mip_range_set(int min, int max) override; + void *read(int mip, eGPUDataFormat format) override; /* TODO(fclem) Legacy. Should be removed at some point. */ - uint gl_bindcode_get(void) override; + uint gl_bindcode_get(void) const override; + + protected: + bool init_internal(void) override; + bool init_internal(GPUVertBuf *vbo) override; private: - void init(void); + void ensure_mipmaps(int miplvl); + GPUFrameBuffer *framebuffer_get(void); + + MEM_CXX_CLASS_ALLOC_FUNCS("GLTexture") }; -static inline GLenum target_to_gl(eGPUTextureFlag target) +inline GLenum to_gl_internal_format(eGPUTextureFormat format) +{ + /* You can add any of the available type to this list + * For available types see GPU_texture.h */ + switch (format) { + /* Formats texture & renderbuffer */ + case GPU_RGBA8UI: + return GL_RGBA8UI; + case GPU_RGBA8I: + return GL_RGBA8I; + case GPU_RGBA8: + return GL_RGBA8; + case GPU_RGBA32UI: + return GL_RGBA32UI; + case GPU_RGBA32I: + return GL_RGBA32I; + case GPU_RGBA32F: + return GL_RGBA32F; + case GPU_RGBA16UI: + return GL_RGBA16UI; + case GPU_RGBA16I: + return GL_RGBA16I; + case GPU_RGBA16F: + return GL_RGBA16F; + case GPU_RGBA16: + return GL_RGBA16; + case GPU_RG8UI: + return GL_RG8UI; + case GPU_RG8I: + return GL_RG8I; + case GPU_RG8: + return GL_RG8; + case GPU_RG32UI: + return GL_RG32UI; + case GPU_RG32I: + return GL_RG32I; + case GPU_RG32F: + return GL_RG32F; + case GPU_RG16UI: + return GL_RG16UI; + case GPU_RG16I: + return GL_RG16I; + case GPU_RG16F: + return GL_RG16F; + case GPU_RG16: + return GL_RG16; + case GPU_R8UI: + return GL_R8UI; + case GPU_R8I: + return GL_R8I; + case GPU_R8: + return GL_R8; + case GPU_R32UI: + return GL_R32UI; + case GPU_R32I: + return GL_R32I; + case GPU_R32F: + return GL_R32F; + case GPU_R16UI: + return GL_R16UI; + case GPU_R16I: + return GL_R16I; + case GPU_R16F: + return GL_R16F; + case GPU_R16: + return GL_R16; + /* Special formats texture & renderbuffer */ + case GPU_R11F_G11F_B10F: + return GL_R11F_G11F_B10F; + case GPU_DEPTH32F_STENCIL8: + return GL_DEPTH32F_STENCIL8; + case GPU_DEPTH24_STENCIL8: + return GL_DEPTH24_STENCIL8; + case GPU_SRGB8_A8: + return GL_SRGB8_ALPHA8; + /* Texture only format */ + case GPU_RGB16F: + return GL_RGB16F; + /* Special formats texture only */ + case GPU_SRGB8_A8_DXT1: + return GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT1_EXT; + case GPU_SRGB8_A8_DXT3: + return GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT3_EXT; + case GPU_SRGB8_A8_DXT5: + return GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT5_EXT; + case GPU_RGBA8_DXT1: + return GL_COMPRESSED_RGBA_S3TC_DXT1_EXT; + case GPU_RGBA8_DXT3: + return GL_COMPRESSED_RGBA_S3TC_DXT3_EXT; + case GPU_RGBA8_DXT5: + return GL_COMPRESSED_RGBA_S3TC_DXT5_EXT; + /* Depth Formats */ + case GPU_DEPTH_COMPONENT32F: + return GL_DEPTH_COMPONENT32F; + case GPU_DEPTH_COMPONENT24: + return GL_DEPTH_COMPONENT24; + case GPU_DEPTH_COMPONENT16: + return GL_DEPTH_COMPONENT16; + default: + BLI_assert(!"Texture format incorrect or unsupported\n"); + return 0; + } +} + +inline GLenum to_gl_target(eGPUTextureType type) { - switch (target & GPU_TEXTURE_TARGET) { + switch (type) { case GPU_TEXTURE_1D: return GL_TEXTURE_1D; - case GPU_TEXTURE_1D | GPU_TEXTURE_ARRAY: + case GPU_TEXTURE_1D_ARRAY: return GL_TEXTURE_1D_ARRAY; case GPU_TEXTURE_2D: return GL_TEXTURE_2D; - case GPU_TEXTURE_2D | GPU_TEXTURE_ARRAY: + case GPU_TEXTURE_2D_ARRAY: return GL_TEXTURE_2D_ARRAY; case GPU_TEXTURE_3D: return GL_TEXTURE_3D; case GPU_TEXTURE_CUBE: return GL_TEXTURE_CUBE_MAP; - case GPU_TEXTURE_CUBE | GPU_TEXTURE_ARRAY: + case GPU_TEXTURE_CUBE_ARRAY: return GL_TEXTURE_CUBE_MAP_ARRAY_ARB; case GPU_TEXTURE_BUFFER: return GL_TEXTURE_BUFFER; default: BLI_assert(0); - return GPU_TEXTURE_1D; + return GL_TEXTURE_1D; } } -static inline GLenum swizzle_to_gl(const char swizzle) +inline GLenum swizzle_to_gl(const char swizzle) { switch (swizzle) { default: @@ -131,7 +248,7 @@ static inline GLenum swizzle_to_gl(const char swizzle) } } -static inline GLenum to_gl(eGPUDataFormat format) +inline GLenum to_gl(eGPUDataFormat format) { switch (format) { case GPU_DATA_FLOAT: @@ -152,8 +269,67 @@ static inline GLenum to_gl(eGPUDataFormat format) } } +/* Definitely not complete, edit according to the gl specification. */ +inline GLenum to_gl_data_format(eGPUTextureFormat format) +{ + /* You can add any of the available type to this list + * For available types see GPU_texture.h */ + switch (format) { + case GPU_R8I: + case GPU_R8UI: + case GPU_R16I: + case GPU_R16UI: + case GPU_R32I: + case GPU_R32UI: + return GL_RED_INTEGER; + case GPU_RG8I: + case GPU_RG8UI: + case GPU_RG16I: + case GPU_RG16UI: + case GPU_RG32I: + case GPU_RG32UI: + return GL_RG_INTEGER; + case GPU_RGBA8I: + case GPU_RGBA8UI: + case GPU_RGBA16I: + case GPU_RGBA16UI: + case GPU_RGBA32I: + case GPU_RGBA32UI: + return GL_RGBA_INTEGER; + case GPU_R8: + case GPU_R16: + case GPU_R16F: + case GPU_R32F: + return GL_RED; + case GPU_RG8: + case GPU_RG16: + case GPU_RG16F: + case GPU_RG32F: + return GL_RG; + case GPU_R11F_G11F_B10F: + case GPU_RGB16F: + return GL_RGB; + case GPU_RGBA8: + case GPU_SRGB8_A8: + case GPU_RGBA16: + case GPU_RGBA16F: + case GPU_RGBA32F: + return GL_RGBA; + case GPU_DEPTH24_STENCIL8: + case GPU_DEPTH32F_STENCIL8: + return GL_DEPTH_STENCIL; + case GPU_DEPTH_COMPONENT16: + case GPU_DEPTH_COMPONENT24: + case GPU_DEPTH_COMPONENT32F: + return GL_DEPTH_COMPONENT; + default: + BLI_assert(!"Texture format incorrect or unsupported\n"); + return 0; + } +} + /* Assume Unorm / Float target. Used with glReadPixels. */ -static inline GLenum channel_len_to_gl(int channel_len) +inline GLenum channel_len_to_gl(int channel_len) { switch (channel_len) { case 1: -- cgit v1.2.3