From 1682b02c670b7d581554b787d7bc253eca61df7b Mon Sep 17 00:00:00 2001 From: Magnus Norddahl Date: Sat, 30 Jul 2016 19:54:20 +0200 Subject: [PATCH] Fix glBindSampler state messing up post processing shaders --- src/gl/renderer/gl_postprocess.cpp | 50 +++++++++++++++++++++++++++--- 1 file changed, 45 insertions(+), 5 deletions(-) diff --git a/src/gl/renderer/gl_postprocess.cpp b/src/gl/renderer/gl_postprocess.cpp index c68cd6531..54848fa64 100644 --- a/src/gl/renderer/gl_postprocess.cpp +++ b/src/gl/renderer/gl_postprocess.cpp @@ -110,6 +110,15 @@ void FGLRenderer::BloomScene() int sampleCount = gl_bloom_kernel_size; // TBD: Maybe need a better way to share state with other parts of the pipeline + GLint activeTex, textureBinding, samplerBinding; + glGetIntegerv(GL_ACTIVE_TEXTURE, &activeTex); + glActiveTexture(GL_TEXTURE0); + glGetIntegerv(GL_TEXTURE_BINDING_2D, &textureBinding); + if (gl.flags & RFL_SAMPLER_OBJECTS) + { + glGetIntegerv(GL_SAMPLER_BINDING, &samplerBinding); + glBindSampler(0, 0); + } GLboolean blendEnabled, scissorEnabled; GLint currentProgram, blendEquationRgb, blendEquationAlpha, blendSrcRgb, blendSrcAlpha, blendDestRgb, blendDestAlpha; glGetBooleanv(GL_BLEND, &blendEnabled); @@ -215,6 +224,10 @@ void FGLRenderer::BloomScene() glBlendEquationSeparate(blendEquationRgb, blendEquationAlpha); glBlendFuncSeparate(blendSrcRgb, blendDestRgb, blendSrcAlpha, blendDestAlpha); glUseProgram(currentProgram); + glBindTexture(GL_TEXTURE_2D, textureBinding); + if (gl.flags & RFL_SAMPLER_OBJECTS) + glBindSampler(0, samplerBinding); + glActiveTexture(activeTex); } //----------------------------------------------------------------------------- @@ -228,6 +241,16 @@ void FGLRenderer::TonemapScene() if (gl_tonemap == 0) return; + GLint activeTex, textureBinding, samplerBinding; + glGetIntegerv(GL_ACTIVE_TEXTURE, &activeTex); + glActiveTexture(GL_TEXTURE0); + glGetIntegerv(GL_TEXTURE_BINDING_2D, &textureBinding); + if (gl.flags & RFL_SAMPLER_OBJECTS) + { + glGetIntegerv(GL_SAMPLER_BINDING, &samplerBinding); + glBindSampler(0, 0); + } + GLboolean blendEnabled, scissorEnabled; glGetBooleanv(GL_BLEND, &blendEnabled); glGetBooleanv(GL_SCISSOR_TEST, &scissorEnabled); @@ -252,6 +275,10 @@ void FGLRenderer::TonemapScene() glEnable(GL_BLEND); if (scissorEnabled) glEnable(GL_SCISSOR_TEST); + glBindTexture(GL_TEXTURE_2D, textureBinding); + if (gl.flags & RFL_SAMPLER_OBJECTS) + glBindSampler(0, samplerBinding); + glActiveTexture(activeTex); } //----------------------------------------------------------------------------- @@ -268,6 +295,20 @@ void FGLRenderer::Flush() glDisable(GL_DEPTH_TEST); glDisable(GL_STENCIL_TEST); + GLboolean blendEnabled; + GLint currentProgram; + GLint activeTex, textureBinding, samplerBinding; + glGetBooleanv(GL_BLEND, &blendEnabled); + glGetIntegerv(GL_CURRENT_PROGRAM, ¤tProgram); + glGetIntegerv(GL_ACTIVE_TEXTURE, &activeTex); + glActiveTexture(GL_TEXTURE0); + glGetIntegerv(GL_TEXTURE_BINDING_2D, &textureBinding); + if (gl.flags & RFL_SAMPLER_OBJECTS) + { + glGetIntegerv(GL_SAMPLER_BINDING, &samplerBinding); + glBindSampler(0, 0); + } + mBuffers->BindOutputFB(); // Calculate letterbox @@ -309,11 +350,6 @@ void FGLRenderer::Flush() // Present what was rendered: glViewport(x, y, width, height); - - GLboolean blendEnabled; - GLint currentProgram; - glGetBooleanv(GL_BLEND, &blendEnabled); - glGetIntegerv(GL_CURRENT_PROGRAM, ¤tProgram); glDisable(GL_BLEND); mPresentShader->Bind(); @@ -342,5 +378,9 @@ void FGLRenderer::Flush() if (blendEnabled) glEnable(GL_BLEND); glUseProgram(currentProgram); + glBindTexture(GL_TEXTURE_2D, textureBinding); + if (gl.flags & RFL_SAMPLER_OBJECTS) + glBindSampler(0, samplerBinding); + glActiveTexture(activeTex); } }