From d1479c437b7bd51bda04cfc0cd74931d11a69e3e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment?= Date: Sat, 5 Sep 2020 14:48:44 +0200 Subject: GLTexture: Add Feedback loop check The check is better than before as we take into consideration the attached mip level. --- source/blender/gpu/intern/gpu_texture_private.hh | 3 ++- source/blender/gpu/opengl/gl_framebuffer.hh | 3 +++ source/blender/gpu/opengl/gl_state.cc | 3 +++ source/blender/gpu/opengl/gl_texture.cc | 31 ++++++++++++++++++++++++ source/blender/gpu/opengl/gl_texture.hh | 2 ++ 5 files changed, 41 insertions(+), 1 deletion(-) diff --git a/source/blender/gpu/intern/gpu_texture_private.hh b/source/blender/gpu/intern/gpu_texture_private.hh index de639d0c435..8f01d28e65e 100644 --- a/source/blender/gpu/intern/gpu_texture_private.hh +++ b/source/blender/gpu/intern/gpu_texture_private.hh @@ -91,11 +91,12 @@ class Texture { /** 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 error checking */ + int mip_min_ = 0, mip_max_ = 0; /** For debugging */ char name_[DEBUG_NAME_LEN]; - private: /** Framebuffer references to update on deletion. */ GPUAttachmentType fb_attachment_[GPU_TEX_MAX_FBO_ATTACHED]; FrameBuffer *fb_[GPU_TEX_MAX_FBO_ATTACHED]; diff --git a/source/blender/gpu/opengl/gl_framebuffer.hh b/source/blender/gpu/opengl/gl_framebuffer.hh index 8173d3b2416..73423425500 100644 --- a/source/blender/gpu/opengl/gl_framebuffer.hh +++ b/source/blender/gpu/opengl/gl_framebuffer.hh @@ -39,6 +39,9 @@ class GLStateManager; * Implementation of FrameBuffer object using OpenGL. **/ class GLFrameBuffer : public FrameBuffer { + /* For debugging purpose. */ + friend class GLTexture; + private: /** OpenGL handle. */ GLuint fbo_id_ = 0; diff --git a/source/blender/gpu/opengl/gl_state.cc b/source/blender/gpu/opengl/gl_state.cc index 20d6d608f78..a97768e5d44 100644 --- a/source/blender/gpu/opengl/gl_state.cc +++ b/source/blender/gpu/opengl/gl_state.cc @@ -434,6 +434,9 @@ void GLStateManager::texture_bind(Texture *tex_, eGPUSamplerState sampler_type, { BLI_assert(unit < GPU_max_textures()); GLTexture *tex = static_cast(tex_); + if (G.debug & G_DEBUG_GPU) { + tex->check_feedback_loop(); + } /* Eliminate redundant binds. */ if ((textures_[unit] == tex->tex_id_) && (samplers_[unit] == GLTexture::samplers_[sampler_type])) { diff --git a/source/blender/gpu/opengl/gl_texture.cc b/source/blender/gpu/opengl/gl_texture.cc index 1736dafa823..990e838e4e6 100644 --- a/source/blender/gpu/opengl/gl_texture.cc +++ b/source/blender/gpu/opengl/gl_texture.cc @@ -402,6 +402,8 @@ void GLTexture::swizzle_set(const char swizzle[4]) void GLTexture::mip_range_set(int min, int max) { BLI_assert(min <= max && min >= 0 && max <= mipmaps_); + mip_min_ = min; + mip_max_ = max; if (GLEW_ARB_direct_state_access) { glTextureParameteri(tex_id_, GL_TEXTURE_BASE_LEVEL, min); glTextureParameteri(tex_id_, GL_TEXTURE_MAX_LEVEL, max); @@ -605,6 +607,35 @@ bool GLTexture::proxy_check(int mip) /** \} */ +void GLTexture::check_feedback_loop(void) +{ + /* Recursive downsample workaround break this check. + * See recursive_downsample() for more infos. */ + if (GPU_mip_render_workaround()) { + return; + } + GLFrameBuffer *fb = static_cast(GPU_context_active_get()->active_fb); + for (int i = 0; i < ARRAY_SIZE(fb_); i++) { + if (fb_[i] == fb) { + GPUAttachmentType type = fb_attachment_[i]; + GPUAttachment attachment = fb->attachments_[type]; + if (attachment.mip <= mip_max_ && attachment.mip >= mip_min_) { + char msg[256]; + SNPRINTF(msg, + "Feedback loop: Trying to bind a texture (%s) with mip range %d-%d but mip %d is " + "attached to the active framebuffer (%s)", + name_, + mip_min_, + mip_max_, + attachment.mip, + fb->name_); + debug::raise_gl_error(msg); + } + return; + } + } +} + /* TODO(fclem) Legacy. Should be removed at some point. */ uint GLTexture::gl_bindcode_get(void) const { diff --git a/source/blender/gpu/opengl/gl_texture.hh b/source/blender/gpu/opengl/gl_texture.hh index c18949c264e..755b7b68f55 100644 --- a/source/blender/gpu/opengl/gl_texture.hh +++ b/source/blender/gpu/opengl/gl_texture.hh @@ -75,6 +75,8 @@ class GLTexture : public Texture { void mip_range_set(int min, int max) override; void *read(int mip, eGPUDataFormat format) override; + void check_feedback_loop(void); + /* TODO(fclem) Legacy. Should be removed at some point. */ uint gl_bindcode_get(void) const override; -- cgit v1.2.3