From c01c0d5166d4df0d7c9c4334c8cac58331a1b67c Mon Sep 17 00:00:00 2001 From: Logan McNaughton Date: Sat, 7 Apr 2018 10:22:09 -0600 Subject: [PATCH] use gl_LastFragDepthARM and glTextureBarrier --- src/DepthBuffer.cpp | 1 + src/FrameBuffer.cpp | 2 ++ src/Graphics/Context.cpp | 5 +++++ src/Graphics/Context.h | 2 ++ src/Graphics/ContextImpl.h | 1 + src/Graphics/OpenGLContext/GLFunctions.cpp | 4 ++++ src/Graphics/OpenGLContext/GLFunctions.h | 4 ++++ .../GLSL/glsl_SpecialShadersFactory.cpp | 17 +++++++++++++++-- .../OpenGLContext/opengl_ContextImpl.cpp | 8 ++++++++ src/Graphics/OpenGLContext/opengl_ContextImpl.h | 2 ++ src/Graphics/OpenGLContext/opengl_GLInfo.cpp | 4 ++++ src/Graphics/OpenGLContext/opengl_GLInfo.h | 3 +++ 12 files changed, 51 insertions(+), 2 deletions(-) diff --git a/src/DepthBuffer.cpp b/src/DepthBuffer.cpp index a09c6cb8..1d08b40a 100644 --- a/src/DepthBuffer.cpp +++ b/src/DepthBuffer.cpp @@ -314,6 +314,7 @@ CachedTexture * DepthBuffer::copyDepthBufferTexture(FrameBuffer * _pBuffer) void DepthBuffer::activateDepthBufferTexture(FrameBuffer * _pBuffer) { textureCache().activateTexture(0, resolveDepthBufferTexture(_pBuffer)); + gfxContext.textureBarrier(); } void DepthBuffer::bindDepthImageTexture() diff --git a/src/FrameBuffer.cpp b/src/FrameBuffer.cpp index 6a581e17..b987d9ff 100644 --- a/src/FrameBuffer.cpp +++ b/src/FrameBuffer.cpp @@ -1278,6 +1278,7 @@ void FrameBuffer_ActivateBufferTexture(u32 t, u32 _frameBufferAddress) // frameBufferList().renderBuffer(pBuffer->m_startAddress); textureCache().activateTexture(t, pTexture); + gfxContext.textureBarrier(); gDP.changed |= CHANGED_FB_TEXTURE; } @@ -1293,6 +1294,7 @@ void FrameBuffer_ActivateBufferTextureBG(u32 t, u32 _frameBufferAddress) // frameBufferList().renderBuffer(pBuffer->m_startAddress); textureCache().activateTexture(t, pTexture); + gfxContext.textureBarrier(); gDP.changed |= CHANGED_FB_TEXTURE; } diff --git a/src/Graphics/Context.cpp b/src/Graphics/Context.cpp index 98f5a881..759d079c 100644 --- a/src/Graphics/Context.cpp +++ b/src/Graphics/Context.cpp @@ -167,6 +167,11 @@ u32 Context::convertInternalTextureFormat(u32 _format) const return m_impl->convertInternalTextureFormat(_format); } +void Context::textureBarrier() +{ + m_impl->textureBarrier(); +} + /*---------------Framebuffer-------------*/ const FramebufferTextureFormats & Context::getFramebufferTextureFormats() diff --git a/src/Graphics/Context.h b/src/Graphics/Context.h index eb1e29d4..13d124a3 100644 --- a/src/Graphics/Context.h +++ b/src/Graphics/Context.h @@ -152,6 +152,8 @@ namespace graphics { u32 convertInternalTextureFormat(u32 _format) const; + void textureBarrier(); + /*---------------Framebuffer-------------*/ const FramebufferTextureFormats & getFramebufferTextureFormats(); diff --git a/src/Graphics/ContextImpl.h b/src/Graphics/ContextImpl.h index 9f9e2c12..f6ac650e 100644 --- a/src/Graphics/ContextImpl.h +++ b/src/Graphics/ContextImpl.h @@ -37,6 +37,7 @@ namespace graphics { virtual s32 getMaxTextureSize() const = 0; virtual void bindImageTexture(const Context::BindImageTextureParameters & _params) = 0; virtual u32 convertInternalTextureFormat(u32 _format) const = 0; + virtual void textureBarrier() = 0; virtual FramebufferTextureFormats * getFramebufferTextureFormats() = 0; virtual ObjectHandle createFramebuffer() = 0; virtual void deleteFramebuffer(ObjectHandle _name) = 0; diff --git a/src/Graphics/OpenGLContext/GLFunctions.cpp b/src/Graphics/OpenGLContext/GLFunctions.cpp index bc6d72df..4b4c6911 100644 --- a/src/Graphics/OpenGLContext/GLFunctions.cpp +++ b/src/Graphics/OpenGLContext/GLFunctions.cpp @@ -190,6 +190,8 @@ PFNGLCREATEFRAMEBUFFERSPROC g_glCreateFramebuffers; PFNGLNAMEDFRAMEBUFFERTEXTUREPROC g_glNamedFramebufferTexture; PFNGLDRAWRANGEELEMENTSBASEVERTEXPROC g_glDrawRangeElementsBaseVertex; PFNGLFLUSHMAPPEDBUFFERRANGEPROC g_glFlushMappedBufferRange; +PFNGLTEXTUREBARRIERPROC g_glTextureBarrier; +PFNGLTEXTUREBARRIERNVPROC g_glTextureBarrierNV; void initGLFunctions() { @@ -330,4 +332,6 @@ void initGLFunctions() GL_GET_PROC_ADR(PFNGLNAMEDFRAMEBUFFERTEXTUREPROC, glNamedFramebufferTexture); GL_GET_PROC_ADR(PFNGLDRAWRANGEELEMENTSBASEVERTEXPROC, glDrawRangeElementsBaseVertex); GL_GET_PROC_ADR(PFNGLFLUSHMAPPEDBUFFERRANGEPROC, glFlushMappedBufferRange); + GL_GET_PROC_ADR(PFNGLTEXTUREBARRIERPROC, glTextureBarrier); + GL_GET_PROC_ADR(PFNGLTEXTUREBARRIERNVPROC, glTextureBarrierNV); } diff --git a/src/Graphics/OpenGLContext/GLFunctions.h b/src/Graphics/OpenGLContext/GLFunctions.h index b92702e6..3cc9b7d4 100644 --- a/src/Graphics/OpenGLContext/GLFunctions.h +++ b/src/Graphics/OpenGLContext/GLFunctions.h @@ -214,6 +214,8 @@ extern PFNGLBLENDCOLORPROC g_glBlendColor; #define glNamedFramebufferTexture(...) CHECKED_GL_FUNCTION(g_glNamedFramebufferTexture, __VA_ARGS__) #define glDrawRangeElementsBaseVertex(...) CHECKED_GL_FUNCTION(g_glDrawRangeElementsBaseVertex, __VA_ARGS__) #define glFlushMappedBufferRange(...) CHECKED_GL_FUNCTION(g_glFlushMappedBufferRange, __VA_ARGS__) +#define glTextureBarrier(...) CHECKED_GL_FUNCTION(g_glTextureBarrier, __VA_ARGS__) +#define glTextureBarrierNV(...) CHECKED_GL_FUNCTION(g_glTextureBarrierNV, __VA_ARGS__) extern PFNGLCREATESHADERPROC g_glCreateShader; extern PFNGLCOMPILESHADERPROC g_glCompileShader; @@ -308,6 +310,8 @@ extern PFNGLCREATEFRAMEBUFFERSPROC g_glCreateFramebuffers; extern PFNGLNAMEDFRAMEBUFFERTEXTUREPROC g_glNamedFramebufferTexture; extern PFNGLDRAWRANGEELEMENTSBASEVERTEXPROC g_glDrawRangeElementsBaseVertex; extern PFNGLFLUSHMAPPEDBUFFERRANGEPROC g_glFlushMappedBufferRange; +extern PFNGLTEXTUREBARRIERPROC g_glTextureBarrier; +extern PFNGLTEXTUREBARRIERNVPROC g_glTextureBarrierNV; void initGLFunctions(); diff --git a/src/Graphics/OpenGLContext/GLSL/glsl_SpecialShadersFactory.cpp b/src/Graphics/OpenGLContext/GLSL/glsl_SpecialShadersFactory.cpp index 84889831..0e65f794 100644 --- a/src/Graphics/OpenGLContext/GLSL/glsl_SpecialShadersFactory.cpp +++ b/src/Graphics/OpenGLContext/GLSL/glsl_SpecialShadersFactory.cpp @@ -63,8 +63,18 @@ namespace glsl { "OUT lowp vec4 fragColor; \n" "lowp float get_alpha() \n" "{ \n" - " mediump ivec2 coord = ivec2(gl_FragCoord.xy); \n" - " highp float bufZ = texelFetch(uDepthImage,coord, 0).r; \n" + ; + if (_glinfo.fetch_depth) { + m_part += + " highp float bufZ = gl_LastFragDepthARM; \n" + ; + } else { + m_part += + " mediump ivec2 coord = ivec2(gl_FragCoord.xy); \n" + " highp float bufZ = texelFetch(uDepthImage,coord, 0).r; \n" + ; + } + m_part += " highp int iZ = bufZ > 0.999 ? 262143 : int(floor(bufZ * 262143.0));\n" " mediump int y0 = clamp(iZ/512, 0, 511); \n" " mediump int x0 = iZ - 512*y0; \n" @@ -79,6 +89,9 @@ namespace glsl { " fragColor = vec4(uFogColor.rgb, get_alpha()); \n" "} \n" ; + + if (_glinfo.fetch_depth) + m_part = "#extension GL_ARM_shader_framebuffer_fetch_depth_stencil : enable \n" + m_part; } }; diff --git a/src/Graphics/OpenGLContext/opengl_ContextImpl.cpp b/src/Graphics/OpenGLContext/opengl_ContextImpl.cpp index c809297d..5427ce39 100644 --- a/src/Graphics/OpenGLContext/opengl_ContextImpl.cpp +++ b/src/Graphics/OpenGLContext/opengl_ContextImpl.cpp @@ -276,6 +276,14 @@ u32 ContextImpl::convertInternalTextureFormat(u32 _format) const return _format; } +void ContextImpl::textureBarrier() +{ + if (m_glInfo.texture_barrier) + glTextureBarrier(); + else if (m_glInfo.texture_barrierNV) + glTextureBarrierNV(); +} + /*---------------Framebuffer-------------*/ graphics::FramebufferTextureFormats * ContextImpl::getFramebufferTextureFormats() diff --git a/src/Graphics/OpenGLContext/opengl_ContextImpl.h b/src/Graphics/OpenGLContext/opengl_ContextImpl.h index 960b77e2..91a03a13 100644 --- a/src/Graphics/OpenGLContext/opengl_ContextImpl.h +++ b/src/Graphics/OpenGLContext/opengl_ContextImpl.h @@ -76,6 +76,8 @@ namespace opengl { u32 convertInternalTextureFormat(u32 _format) const override; + void textureBarrier() override; + /*---------------Framebuffer-------------*/ graphics::FramebufferTextureFormats * getFramebufferTextureFormats() override; diff --git a/src/Graphics/OpenGLContext/opengl_GLInfo.cpp b/src/Graphics/OpenGLContext/opengl_GLInfo.cpp index 607cf0a0..89baffb2 100644 --- a/src/Graphics/OpenGLContext/opengl_GLInfo.cpp +++ b/src/Graphics/OpenGLContext/opengl_GLInfo.cpp @@ -102,4 +102,8 @@ void GLInfo::init() { depthTexture = !isGLES2 || Utils::isExtensionSupported(*this, "GL_OES_depth_texture"); noPerspective = Utils::isExtensionSupported(*this, "GL_NV_shader_noperspective_interpolation"); + + fetch_depth = Utils::isExtensionSupported(*this, "GL_ARM_shader_framebuffer_fetch_depth_stencil"); + texture_barrier = (!isGLESX && numericVersion >= 45) || Utils::isExtensionSupported(*this, "GL_ARB_texture_barrier"); + texture_barrierNV = Utils::isExtensionSupported(*this, "GL_NV_texture_barrier"); } diff --git a/src/Graphics/OpenGLContext/opengl_GLInfo.h b/src/Graphics/OpenGLContext/opengl_GLInfo.h index c6a94163..df9ddb40 100644 --- a/src/Graphics/OpenGLContext/opengl_GLInfo.h +++ b/src/Graphics/OpenGLContext/opengl_GLInfo.h @@ -25,6 +25,9 @@ struct GLInfo { bool msaa = false; bool depthTexture = false; bool noPerspective = false; + bool fetch_depth = false; + bool texture_barrier = false; + bool texture_barrierNV = false; Renderer renderer = Renderer::Other; void init();