Skip to content

Commit

Permalink
OpenGL backend: disable scissor test when copying/blitting textures
Browse files Browse the repository at this point in the history
  • Loading branch information
TheMostDiligent committed Nov 5, 2023
1 parent 464571d commit f9ea53a
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 10 deletions.
12 changes: 12 additions & 0 deletions Graphics/GraphicsEngineOpenGL/include/GLContextState.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,18 @@ class GLContextState
};
const ContextCaps& GetContextCaps() { return m_Caps; }

// glBlitFramebuffer respects scissor test, which is never what we want
void BlitFramebufferNoScissor(GLint srcX0,
GLint srcY0,
GLint srcX1,
GLint srcY1,
GLint dstX0,
GLint dstY0,
GLint dstX1,
GLint dstY1,
GLbitfield mask,
GLenum filter);

private:
// It is unsafe to use GL handle to keep track of bound objects
// When an object is released, GL is free to reuse its handle for
Expand Down
20 changes: 10 additions & 10 deletions Graphics/GraphicsEngineOpenGL/src/DeviceContextGLImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -563,12 +563,12 @@ void DeviceContextGLImpl::EndSubpass()
DEV_CHECK_GL_ERROR("Failed to bind resolve destination FBO as draw framebuffer");

const auto& FBODesc = m_pBoundFramebuffer->GetDesc();
glBlitFramebuffer(0, 0, static_cast<GLint>(FBODesc.Width), static_cast<GLint>(FBODesc.Height),
0, 0, static_cast<GLint>(FBODesc.Width), static_cast<GLint>(FBODesc.Height),
GL_COLOR_BUFFER_BIT,
GL_NEAREST // Filter is ignored
m_ContextState.BlitFramebufferNoScissor(
0, 0, static_cast<GLint>(FBODesc.Width), static_cast<GLint>(FBODesc.Height),
0, 0, static_cast<GLint>(FBODesc.Width), static_cast<GLint>(FBODesc.Height),
GL_COLOR_BUFFER_BIT,
GL_NEAREST // Filter is ignored
);
DEV_CHECK_GL_ERROR("glBlitFramebuffer() failed when resolving multi-sampled attachments");
}

if (glInvalidateFramebuffer != nullptr)
Expand Down Expand Up @@ -1741,12 +1741,12 @@ void DeviceContextGLImpl::ResolveTextureSubresource(ITexture*
DEV_CHECK_GL_ERROR("Failed to bind FBO as read framebuffer");

const auto& MipAttribs = GetMipLevelProperties(SrcTexDesc, ResolveAttribs.SrcMipLevel);
glBlitFramebuffer(0, 0, static_cast<GLint>(MipAttribs.LogicalWidth), static_cast<GLint>(MipAttribs.LogicalHeight),
0, 0, static_cast<GLint>(MipAttribs.LogicalWidth), static_cast<GLint>(MipAttribs.LogicalHeight),
GL_COLOR_BUFFER_BIT,
GL_NEAREST // Filter is ignored
m_ContextState.BlitFramebufferNoScissor(
0, 0, static_cast<GLint>(MipAttribs.LogicalWidth), static_cast<GLint>(MipAttribs.LogicalHeight),
0, 0, static_cast<GLint>(MipAttribs.LogicalWidth), static_cast<GLint>(MipAttribs.LogicalHeight),
GL_COLOR_BUFFER_BIT,
GL_NEAREST // Filter is ignored
);
DEV_CHECK_GL_ERROR("glBlitFramebuffer() failed when resolving multi-sampled texture");

// Restore original FBO
m_ContextState.InvalidateFBO();
Expand Down
22 changes: 22 additions & 0 deletions Graphics/GraphicsEngineOpenGL/src/GLContextState.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -880,4 +880,26 @@ void GLContextState::SetNumPatchVertices(Int32 NumVertices)
#endif
}

void GLContextState::BlitFramebufferNoScissor(GLint srcX0,
GLint srcY0,
GLint srcX1,
GLint srcY1,
GLint dstX0,
GLint dstY0,
GLint dstX1,
GLint dstY1,
GLbitfield mask,
GLenum filter)
{
bool ScissorEnabled = m_RSState.ScissorTestEnable;
if (ScissorEnabled)
EnableScissorTest(false);

glBlitFramebuffer(srcX0, srcY0, srcX1, srcY1, dstX0, dstY0, dstX1, dstY1, mask, filter);
DEV_CHECK_GL_ERROR("glBlitFramebuffer() failed");

if (ScissorEnabled)
EnableScissorTest(true);
}

} // namespace Diligent
8 changes: 8 additions & 0 deletions Graphics/GraphicsEngineOpenGL/src/TextureBaseGL.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -627,6 +627,11 @@ void TextureBaseGL::CopyData(DeviceContextGLImpl* pDeviceCtxGL,
{
auto& GLState = pDeviceCtxGL->GetContextState();

// Copy operations (glCopyTexSubImage2D and family, glBindFramebuffer) are affected by scissor test!
GLboolean ScissorEnabled = GLState.GetScissorTestEnabled();
if (ScissorEnabled)
GLState.EnableScissorTest(false);

for (Uint32 DepthSlice = 0; DepthSlice < pSrcBox->Depth(); ++DepthSlice)
{
GLuint SrcFboHandle = 0;
Expand Down Expand Up @@ -688,6 +693,9 @@ void TextureBaseGL::CopyData(DeviceContextGLImpl* pDeviceCtxGL,
}
}

if (ScissorEnabled)
GLState.EnableScissorTest(true);

// Invalidate FBO as we used glBindFramebuffer directly
GLState.InvalidateFBO();

Expand Down

0 comments on commit f9ea53a

Please sign in to comment.