From adb162e0d165112bcc4ede3160315dc05e197441 Mon Sep 17 00:00:00 2001 From: Leon Styhre Date: Sat, 12 Mar 2022 17:57:59 +0100 Subject: [PATCH] Another go at getting post-processing to work properly with all graphics drivers. --- es-core/src/renderers/Renderer.h | 10 ++++---- es-core/src/renderers/Renderer_GL21.cpp | 32 +++++++------------------ es-core/src/renderers/Shader_GL21.cpp | 8 +++++++ es-core/src/renderers/Shader_GL21.h | 2 ++ resources/shaders/glsl/core.glsl | 6 +++++ 5 files changed, 28 insertions(+), 30 deletions(-) diff --git a/es-core/src/renderers/Renderer.h b/es-core/src/renderers/Renderer.h index 713b82273..a08e216f9 100644 --- a/es-core/src/renderers/Renderer.h +++ b/es-core/src/renderers/Renderer.h @@ -96,8 +96,7 @@ namespace Renderer namespace Texture { enum Type { - RGB, // Replace with AllowShortEnumsOnASingleLine: false (clang-format >=11.0). - RGBA, + RGBA, // Replace with AllowShortEnumsOnASingleLine: false (clang-format >=11.0). BGRA, ALPHA }; @@ -125,6 +124,7 @@ namespace Renderer float saturation; float dimming; bool convertBGRAToRGBA; + bool postProcessing; unsigned int shaders; Vertex() @@ -132,6 +132,7 @@ namespace Renderer , saturation {1.0f} , dimming {1.0f} , convertBGRAToRGBA {false} + , postProcessing {false} , shaders {0} { } @@ -143,6 +144,7 @@ namespace Renderer , saturation {1.0f} , dimming {1.0f} , convertBGRAToRGBA {false} + , postProcessing {false} , shaders {0} { } @@ -202,10 +204,6 @@ namespace Renderer const unsigned int height, void* data); void bindTexture(const unsigned int texture); - void drawLines(const Vertex* vertices, - const unsigned int numVertices, - const Blend::Factor srcBlendFactor = Blend::SRC_ALPHA, - const Blend::Factor dstBlendFactor = Blend::ONE_MINUS_SRC_ALPHA); void drawTriangleStrips(const Vertex* vertices, const unsigned int numVertices, const Blend::Factor srcBlendFactor = Blend::SRC_ALPHA, diff --git a/es-core/src/renderers/Renderer_GL21.cpp b/es-core/src/renderers/Renderer_GL21.cpp index 758878d47..5900f263c 100644 --- a/es-core/src/renderers/Renderer_GL21.cpp +++ b/es-core/src/renderers/Renderer_GL21.cpp @@ -23,10 +23,10 @@ namespace Renderer static GLuint postProcTexture1 {0}; static GLuint postProcTexture2 {0}; - inline GLenum convertBlendFactor(const Blend::Factor _blendFactor) + inline GLenum convertBlendFactor(const Blend::Factor blendFactor) { // clang-format off - switch (_blendFactor) { + switch (blendFactor) { case Blend::ZERO: { return GL_ZERO; } break; case Blend::ONE: { return GL_ONE; } break; case Blend::SRC_COLOR: { return GL_SRC_COLOR; } break; @@ -42,11 +42,10 @@ namespace Renderer // clang-format on } - inline GLenum convertTextureType(const Texture::Type _type) + inline GLenum convertTextureType(const Texture::Type type) { // clang-format off - switch (_type) { - case Texture::RGB: { return GL_RGB; } break; + switch (type) { case Texture::RGBA: { return GL_RGBA; } break; case Texture::BGRA: { return GL_BGRA; } break; case Texture::ALPHA: { return GL_LUMINANCE_ALPHA; } break; @@ -145,13 +144,11 @@ namespace Renderer return false; } - // For the post-processing textures we want to discard the alpha channel to avoid - // weird problems with some graphics drivers. - postProcTexture1 = createTexture(Texture::RGB, false, false, false, + postProcTexture1 = createTexture(Texture::RGBA, false, false, false, static_cast(getScreenWidth()), static_cast(getScreenHeight()), nullptr); - postProcTexture2 = createTexture(Texture::RGB, false, false, false, + postProcTexture2 = createTexture(Texture::RGBA, false, false, false, static_cast(getScreenWidth()), static_cast(getScreenHeight()), nullptr); @@ -281,21 +278,6 @@ namespace Renderer GL_CHECK_ERROR(glBindTexture(GL_TEXTURE_2D, texture)); } - void drawLines(const Vertex* vertices, - const unsigned int numVertices, - const Blend::Factor srcBlendFactor, - const Blend::Factor dstBlendFactor) - { - GL_CHECK_ERROR(glVertexPointer(2, GL_FLOAT, sizeof(Vertex), &vertices[0].position)); - GL_CHECK_ERROR(glTexCoordPointer(2, GL_FLOAT, sizeof(Vertex), &vertices[0].texture)); - GL_CHECK_ERROR(glColorPointer(4, GL_UNSIGNED_BYTE, sizeof(Vertex), &vertices[0].color)); - - GL_CHECK_ERROR( - glBlendFunc(convertBlendFactor(srcBlendFactor), convertBlendFactor(dstBlendFactor))); - - GL_CHECK_ERROR(glDrawArrays(GL_LINES, 0, numVertices)); - } - void drawTriangleStrips(const Vertex* vertices, const unsigned int numVertices, const Blend::Factor srcBlendFactor, @@ -320,6 +302,7 @@ namespace Renderer runShader->setSaturation(vertices->saturation); runShader->setDimming(vertices->dimming); runShader->setBGRAToRGBA(vertices->convertBGRAToRGBA); + runShader->setPostProcessing(vertices->postProcessing); GL_CHECK_ERROR(glDrawArrays(GL_TRIANGLE_STRIP, 0, numVertices)); runShader->deactivateShaders(); } @@ -461,6 +444,7 @@ namespace Renderer vertices->opacity = parameters.opacity; vertices->saturation = parameters.saturation; vertices->dimming = parameters.dimming; + vertices->postProcessing = true; shaderList.emplace_back(Renderer::SHADER_CORE); diff --git a/es-core/src/renderers/Shader_GL21.cpp b/es-core/src/renderers/Shader_GL21.cpp index 1051f14ec..e3b8d1517 100644 --- a/es-core/src/renderers/Shader_GL21.cpp +++ b/es-core/src/renderers/Shader_GL21.cpp @@ -111,6 +111,7 @@ namespace Renderer shaderSaturation = glGetUniformLocation(mProgramID, "saturation"); shaderDimming = glGetUniformLocation(mProgramID, "dimming"); shaderBGRAToRGBA = glGetUniformLocation(mProgramID, "BGRAToRGBA"); + shaderPostProcessing = glGetUniformLocation(mProgramID, "postProcessing"); } void Renderer::Shader::setModelViewProjectionMatrix(glm::mat4 mvpMatrix) @@ -158,6 +159,13 @@ namespace Renderer GL_CHECK_ERROR(glUniform1i(shaderBGRAToRGBA, BGRAToRGBA ? 1 : 0)); } + void Renderer::Shader::setPostProcessing(GLboolean postProcessing) + { + if (shaderPostProcessing != GL_INVALID_VALUE && + shaderPostProcessing != GL_INVALID_OPERATION) + GL_CHECK_ERROR(glUniform1i(shaderPostProcessing, postProcessing ? 1 : 0)); + } + void Renderer::Shader::activateShaders() { // Install the shader program. diff --git a/es-core/src/renderers/Shader_GL21.h b/es-core/src/renderers/Shader_GL21.h index faa273ebd..8131715bf 100644 --- a/es-core/src/renderers/Shader_GL21.h +++ b/es-core/src/renderers/Shader_GL21.h @@ -53,6 +53,7 @@ namespace Renderer void setSaturation(GLfloat saturation); void setDimming(GLfloat dimming); void setBGRAToRGBA(GLboolean BGRAToRGBA); + void setPostProcessing(GLboolean postProcessing); // Sets the shader program to use the loaded shaders. void activateShaders(); // Sets the shader program to 0 which reverts to the fixed function pipeline. @@ -75,6 +76,7 @@ namespace Renderer GLint shaderSaturation; GLint shaderDimming; GLint shaderBGRAToRGBA; + GLint shaderPostProcessing; }; } // namespace Renderer diff --git a/resources/shaders/glsl/core.glsl b/resources/shaders/glsl/core.glsl index 5f673cd9d..e8377bbcf 100644 --- a/resources/shaders/glsl/core.glsl +++ b/resources/shaders/glsl/core.glsl @@ -44,12 +44,18 @@ uniform float opacity = 1.0f; uniform float saturation = 1.0f; uniform float dimming = 1.0f; uniform int BGRAToRGBA = 0; +uniform int postProcessing = 0; uniform sampler2D myTexture; void main() { vec4 color = COMPAT_TEXTURE(myTexture, texCoord) * color; + // When post-processing we drop the alpha channel to avoid strange issues + // with some graphics drivers. + if (postProcessing == 1) + color.a = 1.0f; + // Opacity. if (opacity != 1.0f) color.a = color.a * opacity;