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
{
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,

View file

@ -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<unsigned int>(getScreenWidth()),
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>(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);

View file

@ -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.

View file

@ -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

View file

@ -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;