Another go at getting post-processing to work properly with all graphics drivers.

This commit is contained in:
Leon Styhre 2022-03-12 17:57:59 +01:00
parent 32251e0264
commit adb162e0d1
5 changed files with 28 additions and 30 deletions

View file

@ -96,8 +96,7 @@ namespace Renderer
namespace Texture namespace Texture
{ {
enum Type { enum Type {
RGB, // Replace with AllowShortEnumsOnASingleLine: false (clang-format >=11.0). RGBA, // Replace with AllowShortEnumsOnASingleLine: false (clang-format >=11.0).
RGBA,
BGRA, BGRA,
ALPHA ALPHA
}; };
@ -125,6 +124,7 @@ namespace Renderer
float saturation; float saturation;
float dimming; float dimming;
bool convertBGRAToRGBA; bool convertBGRAToRGBA;
bool postProcessing;
unsigned int shaders; unsigned int shaders;
Vertex() Vertex()
@ -132,6 +132,7 @@ namespace Renderer
, saturation {1.0f} , saturation {1.0f}
, dimming {1.0f} , dimming {1.0f}
, convertBGRAToRGBA {false} , convertBGRAToRGBA {false}
, postProcessing {false}
, shaders {0} , shaders {0}
{ {
} }
@ -143,6 +144,7 @@ namespace Renderer
, saturation {1.0f} , saturation {1.0f}
, dimming {1.0f} , dimming {1.0f}
, convertBGRAToRGBA {false} , convertBGRAToRGBA {false}
, postProcessing {false}
, shaders {0} , shaders {0}
{ {
} }
@ -202,10 +204,6 @@ namespace Renderer
const unsigned int height, const unsigned int height,
void* data); void* data);
void bindTexture(const unsigned int texture); 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, void drawTriangleStrips(const Vertex* vertices,
const unsigned int numVertices, const unsigned int numVertices,
const Blend::Factor srcBlendFactor = Blend::SRC_ALPHA, const Blend::Factor srcBlendFactor = Blend::SRC_ALPHA,

View file

@ -23,10 +23,10 @@ namespace Renderer
static GLuint postProcTexture1 {0}; static GLuint postProcTexture1 {0};
static GLuint postProcTexture2 {0}; static GLuint postProcTexture2 {0};
inline GLenum convertBlendFactor(const Blend::Factor _blendFactor) inline GLenum convertBlendFactor(const Blend::Factor blendFactor)
{ {
// clang-format off // clang-format off
switch (_blendFactor) { switch (blendFactor) {
case Blend::ZERO: { return GL_ZERO; } break; case Blend::ZERO: { return GL_ZERO; } break;
case Blend::ONE: { return GL_ONE; } break; case Blend::ONE: { return GL_ONE; } break;
case Blend::SRC_COLOR: { return GL_SRC_COLOR; } break; case Blend::SRC_COLOR: { return GL_SRC_COLOR; } break;
@ -42,11 +42,10 @@ namespace Renderer
// clang-format on // clang-format on
} }
inline GLenum convertTextureType(const Texture::Type _type) inline GLenum convertTextureType(const Texture::Type type)
{ {
// clang-format off // clang-format off
switch (_type) { switch (type) {
case Texture::RGB: { return GL_RGB; } break;
case Texture::RGBA: { return GL_RGBA; } break; case Texture::RGBA: { return GL_RGBA; } break;
case Texture::BGRA: { return GL_BGRA; } break; case Texture::BGRA: { return GL_BGRA; } break;
case Texture::ALPHA: { return GL_LUMINANCE_ALPHA; } break; case Texture::ALPHA: { return GL_LUMINANCE_ALPHA; } break;
@ -145,13 +144,11 @@ namespace Renderer
return false; return false;
} }
// For the post-processing textures we want to discard the alpha channel to avoid postProcTexture1 = createTexture(Texture::RGBA, false, false, false,
// weird problems with some graphics drivers.
postProcTexture1 = createTexture(Texture::RGB, false, false, false,
static_cast<unsigned int>(getScreenWidth()), static_cast<unsigned int>(getScreenWidth()),
static_cast<unsigned int>(getScreenHeight()), nullptr); static_cast<unsigned int>(getScreenHeight()), nullptr);
postProcTexture2 = createTexture(Texture::RGB, false, false, false, postProcTexture2 = createTexture(Texture::RGBA, false, false, false,
static_cast<unsigned int>(getScreenWidth()), static_cast<unsigned int>(getScreenWidth()),
static_cast<unsigned int>(getScreenHeight()), nullptr); static_cast<unsigned int>(getScreenHeight()), nullptr);
@ -281,21 +278,6 @@ namespace Renderer
GL_CHECK_ERROR(glBindTexture(GL_TEXTURE_2D, texture)); 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, void drawTriangleStrips(const Vertex* vertices,
const unsigned int numVertices, const unsigned int numVertices,
const Blend::Factor srcBlendFactor, const Blend::Factor srcBlendFactor,
@ -320,6 +302,7 @@ namespace Renderer
runShader->setSaturation(vertices->saturation); runShader->setSaturation(vertices->saturation);
runShader->setDimming(vertices->dimming); runShader->setDimming(vertices->dimming);
runShader->setBGRAToRGBA(vertices->convertBGRAToRGBA); runShader->setBGRAToRGBA(vertices->convertBGRAToRGBA);
runShader->setPostProcessing(vertices->postProcessing);
GL_CHECK_ERROR(glDrawArrays(GL_TRIANGLE_STRIP, 0, numVertices)); GL_CHECK_ERROR(glDrawArrays(GL_TRIANGLE_STRIP, 0, numVertices));
runShader->deactivateShaders(); runShader->deactivateShaders();
} }
@ -461,6 +444,7 @@ namespace Renderer
vertices->opacity = parameters.opacity; vertices->opacity = parameters.opacity;
vertices->saturation = parameters.saturation; vertices->saturation = parameters.saturation;
vertices->dimming = parameters.dimming; vertices->dimming = parameters.dimming;
vertices->postProcessing = true;
shaderList.emplace_back(Renderer::SHADER_CORE); shaderList.emplace_back(Renderer::SHADER_CORE);

View file

@ -111,6 +111,7 @@ namespace Renderer
shaderSaturation = glGetUniformLocation(mProgramID, "saturation"); shaderSaturation = glGetUniformLocation(mProgramID, "saturation");
shaderDimming = glGetUniformLocation(mProgramID, "dimming"); shaderDimming = glGetUniformLocation(mProgramID, "dimming");
shaderBGRAToRGBA = glGetUniformLocation(mProgramID, "BGRAToRGBA"); shaderBGRAToRGBA = glGetUniformLocation(mProgramID, "BGRAToRGBA");
shaderPostProcessing = glGetUniformLocation(mProgramID, "postProcessing");
} }
void Renderer::Shader::setModelViewProjectionMatrix(glm::mat4 mvpMatrix) void Renderer::Shader::setModelViewProjectionMatrix(glm::mat4 mvpMatrix)
@ -158,6 +159,13 @@ namespace Renderer
GL_CHECK_ERROR(glUniform1i(shaderBGRAToRGBA, BGRAToRGBA ? 1 : 0)); 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() void Renderer::Shader::activateShaders()
{ {
// Install the shader program. // Install the shader program.

View file

@ -53,6 +53,7 @@ namespace Renderer
void setSaturation(GLfloat saturation); void setSaturation(GLfloat saturation);
void setDimming(GLfloat dimming); void setDimming(GLfloat dimming);
void setBGRAToRGBA(GLboolean BGRAToRGBA); void setBGRAToRGBA(GLboolean BGRAToRGBA);
void setPostProcessing(GLboolean postProcessing);
// Sets the shader program to use the loaded shaders. // Sets the shader program to use the loaded shaders.
void activateShaders(); void activateShaders();
// Sets the shader program to 0 which reverts to the fixed function pipeline. // Sets the shader program to 0 which reverts to the fixed function pipeline.
@ -75,6 +76,7 @@ namespace Renderer
GLint shaderSaturation; GLint shaderSaturation;
GLint shaderDimming; GLint shaderDimming;
GLint shaderBGRAToRGBA; GLint shaderBGRAToRGBA;
GLint shaderPostProcessing;
}; };
} // namespace Renderer } // namespace Renderer

View file

@ -44,12 +44,18 @@ uniform float opacity = 1.0f;
uniform float saturation = 1.0f; uniform float saturation = 1.0f;
uniform float dimming = 1.0f; uniform float dimming = 1.0f;
uniform int BGRAToRGBA = 0; uniform int BGRAToRGBA = 0;
uniform int postProcessing = 0;
uniform sampler2D myTexture; uniform sampler2D myTexture;
void main() void main()
{ {
vec4 color = COMPAT_TEXTURE(myTexture, texCoord) * color; 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. // Opacity.
if (opacity != 1.0f) if (opacity != 1.0f)
color.a = color.a * opacity; color.a = color.a * opacity;