diff options
author | Clément Foucault <foucault.clem@gmail.com> | 2020-09-02 01:27:41 +0300 |
---|---|---|
committer | Clément Foucault <foucault.clem@gmail.com> | 2020-09-05 18:49:14 +0300 |
commit | f72c1c4547e5fab769c22652d9872192029ad7fe (patch) | |
tree | 4b89ee42810e0505ed386b76bf5f76f5d4107a1c /source/blender/gpu | |
parent | c598e939ad25dfc3b4eb1c92b16de401bde0d88a (diff) |
GPUTexture: Remove bind to edit calls
This is going to be unecessary after the GPU opengl texture backend refactor.
For now add a save/restore mechanism to leave the state untouched.
Also remove some calls where the caller would bind to particular binding
point and set the shader uniform.
Diffstat (limited to 'source/blender/gpu')
-rw-r--r-- | source/blender/gpu/GPU_batch.h | 2 | ||||
-rw-r--r-- | source/blender/gpu/intern/gpu_texture.cc | 84 |
2 files changed, 71 insertions, 15 deletions
diff --git a/source/blender/gpu/GPU_batch.h b/source/blender/gpu/GPU_batch.h index e00aac85655..f26f706d058 100644 --- a/source/blender/gpu/GPU_batch.h +++ b/source/blender/gpu/GPU_batch.h @@ -146,6 +146,8 @@ void GPU_batch_program_set_builtin_with_config(GPUBatch *batch, GPU_shader_uniform_4fv_array((batch)->shader, name, len, val); #define GPU_batch_uniform_mat4(batch, name, val) \ GPU_shader_uniform_mat4((batch)->shader, name, val); +#define GPU_batch_texture_bind(batch, name, tex) \ + GPU_texture_bind(tex, GPU_shader_get_texture_binding((batch)->shader, name)); void GPU_batch_draw(GPUBatch *batch); void GPU_batch_draw_range(GPUBatch *batch, int v_first, int v_count); diff --git a/source/blender/gpu/intern/gpu_texture.cc b/source/blender/gpu/intern/gpu_texture.cc index 3dedfcf763a..0a22df96382 100644 --- a/source/blender/gpu/intern/gpu_texture.cc +++ b/source/blender/gpu/intern/gpu_texture.cc @@ -46,16 +46,6 @@ #include "gpu_context_private.hh" #include "gpu_framebuffer_private.hh" -#define WARN_NOT_BOUND(_tex) \ - { \ - if (_tex->number == -1) { \ - fprintf(stderr, "Warning : Trying to set parameter on a texture not bound.\n"); \ - BLI_assert(0); \ - return; \ - } \ - } \ - ((void)0) - static struct GPUTextureGlobal { /** Texture used in place of invalid textures (not loaded correctly, missing). */ GPUTexture *invalid_tex_1D; @@ -231,6 +221,33 @@ static const char *gl_enum_to_str(GLenum e) #undef ENUM_TO_STRING } +static GLenum gl_enum_target_to_binding(GLenum target) +{ + 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) { @@ -1493,7 +1510,16 @@ void GPU_texture_update_sub(GPUTexture *tex, GLenum data_format = gpu_get_gl_dataformat(tex->format, &tex->format_flag); GLenum data_type = gpu_get_gl_datatype(gpu_data_format); - WARN_NOT_BOUND(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); + } switch (tex->target) { case GL_TEXTURE_1D: @@ -1522,6 +1548,10 @@ void GPU_texture_update_sub(GPUTexture *tex, default: BLI_assert(!"tex->target mode not supported"); } + + if (tex->number == -1) { + glBindTexture(tex->target, texture_bound); + } } void *GPU_texture_read(GPUTexture *tex, eGPUDataFormat gpu_data_format, int miplvl) @@ -1822,12 +1852,19 @@ void GPU_texture_unbind_all(void) void GPU_texture_generate_mipmap(GPUTexture *tex) { - WARN_NOT_BOUND(tex); - gpu_texture_memory_footprint_remove(tex); int levels = 1 + floor(log2(max_ii(tex->w, tex->h))); - glActiveTexture(GL_TEXTURE0 + tex->number); + /* 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); + } if (GPU_texture_depth(tex)) { /* Some drivers have bugs when using glGenerateMipmap with depth textures (see T56789). @@ -1846,6 +1883,10 @@ void GPU_texture_generate_mipmap(GPUTexture *tex) 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) @@ -2002,7 +2043,16 @@ static int gpu_texture_swizzle_to_enum(const char swizzle) void GPU_texture_swizzle_set(GPUTexture *tex, const char swizzle[4]) { - WARN_NOT_BOUND(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); + } GLint gl_swizzle[4] = {gpu_texture_swizzle_to_enum(swizzle[0]), gpu_texture_swizzle_to_enum(swizzle[1]), @@ -2011,6 +2061,10 @@ void GPU_texture_swizzle_set(GPUTexture *tex, const char swizzle[4]) glActiveTexture(GL_TEXTURE0 + tex->number); glTexParameteriv(tex->target_base, GL_TEXTURE_SWIZZLE_RGBA, gl_swizzle); + + if (tex->number == -1) { + glBindTexture(tex->target, texture_bound); + } } void GPU_texture_free(GPUTexture *tex) |