From 93bf17d9521025fe849bf705773de7bacc1dc01d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Foucault?= Date: Wed, 16 Mar 2022 08:36:42 +0100 Subject: GPU: Add support for stencil buffer texturing mode. This adds the possibility to read the stencil buffer inside shaders. This is only available on GL 4.3 so use it accordingly. --- source/blender/gpu/GPU_texture.h | 5 +++++ source/blender/gpu/intern/gpu_texture.cc | 6 ++++++ source/blender/gpu/intern/gpu_texture_private.hh | 1 + source/blender/gpu/opengl/gl_backend.cc | 2 ++ source/blender/gpu/opengl/gl_context.hh | 1 + source/blender/gpu/opengl/gl_texture.cc | 13 +++++++++++++ source/blender/gpu/opengl/gl_texture.hh | 1 + 7 files changed, 29 insertions(+) (limited to 'source') diff --git a/source/blender/gpu/GPU_texture.h b/source/blender/gpu/GPU_texture.h index 0c7f1c1cbd4..4dbcd84672c 100644 --- a/source/blender/gpu/GPU_texture.h +++ b/source/blender/gpu/GPU_texture.h @@ -299,6 +299,11 @@ void GPU_texture_filter_mode(GPUTexture *tex, bool use_filter); void GPU_texture_mipmap_mode(GPUTexture *tex, bool use_mipmap, bool use_filter); void GPU_texture_wrap_mode(GPUTexture *tex, bool use_repeat, bool use_clamp); void GPU_texture_swizzle_set(GPUTexture *tex, const char swizzle[4]); +/** + * Set depth stencil texture sampling behavior. Can work on texture views. + * If stencil sampling is enabled, an unsigned integer sampler is required. + */ +void GPU_texture_stencil_texture_mode_set(GPUTexture *tex, bool use_stencil); /** * Return the number of dimensions of the texture ignoring dimension of layers (1, 2 or 3). diff --git a/source/blender/gpu/intern/gpu_texture.cc b/source/blender/gpu/intern/gpu_texture.cc index 1aebf2ff112..0d2ec43533f 100644 --- a/source/blender/gpu/intern/gpu_texture.cc +++ b/source/blender/gpu/intern/gpu_texture.cc @@ -550,6 +550,12 @@ void GPU_texture_swizzle_set(GPUTexture *tex, const char swizzle[4]) reinterpret_cast(tex)->swizzle_set(swizzle); } +void GPU_texture_stencil_texture_mode_set(GPUTexture *tex, bool use_stencil) +{ + BLI_assert(GPU_texture_stencil(tex) || !use_stencil); + reinterpret_cast(tex)->stencil_texture_mode_set(use_stencil); +} + void GPU_texture_free(GPUTexture *tex_) { Texture *tex = reinterpret_cast(tex_); diff --git a/source/blender/gpu/intern/gpu_texture_private.hh b/source/blender/gpu/intern/gpu_texture_private.hh index 2a7fc074046..1e81980e95f 100644 --- a/source/blender/gpu/intern/gpu_texture_private.hh +++ b/source/blender/gpu/intern/gpu_texture_private.hh @@ -117,6 +117,7 @@ class Texture { 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 stencil_texture_mode_set(bool use_stencil) = 0; virtual void mip_range_set(int min, int max) = 0; virtual void *read(int mip, eGPUDataFormat format) = 0; diff --git a/source/blender/gpu/opengl/gl_backend.cc b/source/blender/gpu/opengl/gl_backend.cc index fe730ba3c05..883f9403920 100644 --- a/source/blender/gpu/opengl/gl_backend.cc +++ b/source/blender/gpu/opengl/gl_backend.cc @@ -444,6 +444,7 @@ bool GLContext::native_barycentric_support = false; bool GLContext::multi_bind_support = false; bool GLContext::multi_draw_indirect_support = false; bool GLContext::shader_draw_parameters_support = false; +bool GLContext::stencil_texturing_support = false; bool GLContext::texture_cube_map_array_support = false; bool GLContext::texture_filter_anisotropic_support = false; bool GLContext::texture_gather_support = false; @@ -511,6 +512,7 @@ void GLBackend::capabilities_init() GLContext::multi_bind_support = GLEW_ARB_multi_bind; GLContext::multi_draw_indirect_support = GLEW_ARB_multi_draw_indirect; GLContext::shader_draw_parameters_support = GLEW_ARB_shader_draw_parameters; + GLContext::stencil_texturing_support = GLEW_VERSION_4_3; GLContext::texture_cube_map_array_support = GLEW_ARB_texture_cube_map_array; GLContext::texture_filter_anisotropic_support = GLEW_EXT_texture_filter_anisotropic; GLContext::texture_gather_support = GLEW_ARB_texture_gather; diff --git a/source/blender/gpu/opengl/gl_context.hh b/source/blender/gpu/opengl/gl_context.hh index 54ef1b69e22..c333c8a4afd 100644 --- a/source/blender/gpu/opengl/gl_context.hh +++ b/source/blender/gpu/opengl/gl_context.hh @@ -63,6 +63,7 @@ class GLContext : public Context { static bool multi_bind_support; static bool multi_draw_indirect_support; static bool shader_draw_parameters_support; + static bool stencil_texturing_support; static bool texture_cube_map_array_support; static bool texture_filter_anisotropic_support; static bool texture_gather_support; diff --git a/source/blender/gpu/opengl/gl_texture.cc b/source/blender/gpu/opengl/gl_texture.cc index 9d070088802..4ab9360daaa 100644 --- a/source/blender/gpu/opengl/gl_texture.cc +++ b/source/blender/gpu/opengl/gl_texture.cc @@ -453,6 +453,19 @@ void GLTexture::swizzle_set(const char swizzle[4]) } } +void GLTexture::stencil_texture_mode_set(bool use_stencil) +{ + BLI_assert(GLContext::stencil_texturing_support); + GLint value = use_stencil ? GL_STENCIL_INDEX : GL_DEPTH_COMPONENT; + if (GLContext::direct_state_access_support) { + glTextureParameteri(tex_id_, GL_DEPTH_STENCIL_TEXTURE_MODE, value); + } + else { + GLContext::state_manager_active_get()->texture_bind_temp(this); + glTexParameteri(target_, GL_DEPTH_STENCIL_TEXTURE_MODE, value); + } +} + void GLTexture::mip_range_set(int min, int max) { BLI_assert(min <= max && min >= 0 && max <= mipmaps_); diff --git a/source/blender/gpu/opengl/gl_texture.hh b/source/blender/gpu/opengl/gl_texture.hh index d4d024f5e3e..2dde8d6c86b 100644 --- a/source/blender/gpu/opengl/gl_texture.hh +++ b/source/blender/gpu/opengl/gl_texture.hh @@ -57,6 +57,7 @@ class GLTexture : public Texture { void copy_to(Texture *dst) override; void clear(eGPUDataFormat format, const void *data) override; void swizzle_set(const char swizzle_mask[4]) override; + void stencil_texture_mode_set(bool use_stencil) override; void mip_range_set(int min, int max) override; void *read(int mip, eGPUDataFormat type) override; -- cgit v1.2.3