Shader flags are now sent as bit masks instead of via discreet values.

Also changed the shader flags to enums.
This commit is contained in:
Leon Styhre 2022-03-14 22:30:24 +01:00
parent 6b095bde05
commit 06a037d8cf
12 changed files with 105 additions and 124 deletions

View file

@ -90,9 +90,9 @@ void MediaViewer::render(const glm::mat4& /*parentTrans*/)
Renderer::postProcessingParams videoParameters;
unsigned int shaders {0};
if (Settings::getInstance()->getBool("MediaViewerVideoScanlines"))
shaders = Renderer::SHADER_SCANLINES;
shaders = Renderer::Shader::SCANLINES;
if (Settings::getInstance()->getBool("MediaViewerVideoBlur")) {
shaders |= Renderer::SHADER_BLUR_HORIZONTAL;
shaders |= Renderer::Shader::BLUR_HORIZONTAL;
float heightModifier {Renderer::getScreenHeightModifier()};
// clang-format off
if (heightModifier < 1)
@ -120,10 +120,10 @@ void MediaViewer::render(const glm::mat4& /*parentTrans*/)
if (mCurrentImageIndex == mScreenshotIndex &&
Settings::getInstance()->getBool("MediaViewerScreenshotScanlines"))
mRenderer->shaderPostprocessing(Renderer::SHADER_SCANLINES);
mRenderer->shaderPostprocessing(Renderer::Shader::SCANLINES);
else if (mCurrentImageIndex == mTitleScreenIndex &&
Settings::getInstance()->getBool("MediaViewerScreenshotScanlines"))
mRenderer->shaderPostprocessing(Renderer::SHADER_SCANLINES);
mRenderer->shaderPostprocessing(Renderer::Shader::SCANLINES);
// This is necessary so that the video loops if viewing an image when
// the video ends.

View file

@ -269,7 +269,7 @@ void Screensaver::renderScreensaver()
if (Settings::getInstance()->getString("ScreensaverType") == "slideshow") {
if (mHasMediaFiles) {
if (Settings::getInstance()->getBool("ScreensaverSlideshowScanlines"))
mRenderer->shaderPostprocessing(Renderer::SHADER_SCANLINES);
mRenderer->shaderPostprocessing(Renderer::Shader::SCANLINES);
if (Settings::getInstance()->getBool("ScreensaverSlideshowGameInfo") &&
mGameOverlay) {
if (mGameOverlayRectangleCoords.size() == 4) {
@ -297,9 +297,9 @@ void Screensaver::renderScreensaver()
Renderer::postProcessingParams videoParameters;
unsigned int shaders {0};
if (Settings::getInstance()->getBool("ScreensaverVideoScanlines"))
shaders = Renderer::SHADER_SCANLINES;
shaders = Renderer::Shader::SCANLINES;
if (Settings::getInstance()->getBool("ScreensaverVideoBlur")) {
shaders |= Renderer::SHADER_BLUR_HORIZONTAL;
shaders |= Renderer::Shader::BLUR_HORIZONTAL;
float heightModifier = Renderer::getScreenHeightModifier();
// clang-format off
if (heightModifier < 1)
@ -348,7 +348,7 @@ void Screensaver::renderScreensaver()
Renderer::postProcessingParams dimParameters;
dimParameters.dimming = mDimValue;
dimParameters.saturation = mSaturationAmount;
mRenderer->shaderPostprocessing(Renderer::SHADER_CORE, dimParameters);
mRenderer->shaderPostprocessing(Renderer::Shader::CORE, dimParameters);
if (mDimValue > 0.63)
mDimValue = glm::clamp(mDimValue - 0.015f, 0.68f, 1.0f);
if (mSaturationAmount > 0.0)
@ -357,7 +357,7 @@ void Screensaver::renderScreensaver()
else if (Settings::getInstance()->getString("ScreensaverType") == "black") {
Renderer::postProcessingParams blackParameters;
blackParameters.dimming = mDimValue;
mRenderer->shaderPostprocessing(Renderer::SHADER_CORE, blackParameters);
mRenderer->shaderPostprocessing(Renderer::Shader::CORE, blackParameters);
if (mDimValue > 0.0)
mDimValue = glm::clamp(mDimValue - 0.045f, 0.0f, 1.0f);
}

View file

@ -477,15 +477,15 @@ void Window::render()
// Also dim the background slightly.
backgroundParameters.dimming = 0.60f;
mRenderer->shaderPostprocessing(Renderer::SHADER_CORE |
Renderer::SHADER_BLUR_HORIZONTAL |
Renderer::SHADER_BLUR_VERTICAL,
mRenderer->shaderPostprocessing(Renderer::Shader::CORE |
Renderer::Shader::BLUR_HORIZONTAL |
Renderer::Shader::BLUR_VERTICAL,
backgroundParameters, &processedTexture[0]);
}
else {
// Dim the background slightly.
backgroundParameters.dimming = 0.60f;
mRenderer->shaderPostprocessing(Renderer::SHADER_CORE, backgroundParameters,
mRenderer->shaderPostprocessing(Renderer::Shader::CORE, backgroundParameters,
&processedTexture[0]);
}

View file

@ -491,7 +491,7 @@ void GIFAnimComponent::render(const glm::mat4& parentTrans)
vertices->saturation = mSaturation;
vertices->opacity = mOpacity * mThemeOpacity;
vertices->dimming = mDimming;
vertices->convertBGRAToRGBA = true;
vertices->shaderFlags = Renderer::ShaderFlags::BGRA_TO_RGBA;
// Render it.
mRenderer->drawTriangleStrips(&vertices[0], 4);

View file

@ -480,7 +480,7 @@ void LottieAnimComponent::render(const glm::mat4& parentTrans)
vertices->saturation = mSaturation;
vertices->opacity = mOpacity * mThemeOpacity;
vertices->dimming = mDimming;
vertices->convertBGRAToRGBA = true;
vertices->shaderFlags = Renderer::ShaderFlags::BGRA_TO_RGBA;
// Render it.
mRenderer->drawTriangleStrips(&vertices[0], 4);

View file

@ -217,7 +217,7 @@ void VideoFFmpegComponent::render(const glm::mat4& parentTrans)
vertices[0].opacity = mFadeIn * mThemeOpacity;
if ((mLegacyTheme && Settings::getInstance()->getBool("GamelistVideoScanlines")) ||
(!mLegacyTheme && mRenderScanlines)) {
vertices[0].shaders = Renderer::SHADER_SCANLINES;
vertices[0].shaders = Renderer::Shader::SCANLINES;
}
}

View file

@ -17,47 +17,14 @@
#include <vector>
struct SDL_Window;
class Shader;
class Renderer
{
public:
struct Vertex {
glm::vec2 position;
glm::vec2 texture;
unsigned int color;
float opacity;
float saturation;
float dimming;
bool convertBGRAToRGBA;
bool font;
bool postProcessing;
unsigned int shaders;
Vertex()
: opacity {1.0f}
, saturation {1.0f}
, dimming {1.0f}
, convertBGRAToRGBA {false}
, font {false}
, postProcessing {false}
, shaders {0}
{
}
Vertex(const glm::vec2& position, const glm::vec2& textureCoord, const unsigned int color)
: position(position)
, texture(textureCoord)
, color(color)
, opacity {1.0f}
, saturation {1.0f}
, dimming {1.0f}
, convertBGRAToRGBA {false}
, font {false}
, postProcessing {false}
, shaders {0}
{
}
enum class TextureType {
RGBA, // Replace with AllowShortEnumsOnASingleLine: false (clang-format >=11.0).
BGRA,
RED
};
enum class BlendFactor {
@ -73,17 +40,57 @@ public:
ONE_MINUS_DST_ALPHA
};
enum class TextureType {
RGBA, // Replace with AllowShortEnumsOnASingleLine: false (clang-format >=11.0).
BGRA,
RED
// clang-format off
enum Shader {
CORE = 0x00000001,
BLUR_HORIZONTAL = 0x00000002,
BLUR_VERTICAL = 0x00000004,
SCANLINES = 0x00000008
};
enum ShaderFlags {
BGRA_TO_RGBA = 0x00000001,
FONT_TEXTURE = 0x00000002,
POST_PROCESSING = 0x00000004
};
// clang-format on
struct Vertex {
glm::vec2 position;
glm::vec2 texture;
unsigned int color;
float opacity;
float saturation;
float dimming;
unsigned int shaders;
unsigned int shaderFlags;
Vertex()
: opacity {1.0f}
, saturation {1.0f}
, dimming {1.0f}
, shaders {0}
, shaderFlags {0}
{
}
Vertex(const glm::vec2& position, const glm::vec2& textureCoord, const unsigned int color)
: position(position)
, texture(textureCoord)
, color(color)
, opacity {1.0f}
, saturation {1.0f}
, dimming {1.0f}
, shaders {0}
, shaderFlags {0}
{
}
};
struct postProcessingParams {
float opacity;
float saturation;
float dimming;
bool convertBGRAToRGBA;
unsigned int blurPasses;
unsigned int shaders;
@ -91,7 +98,6 @@ public:
: opacity {1.0f}
, saturation {1.0f}
, dimming {1.0f}
, convertBGRAToRGBA {false}
, blurPasses {1}
, shaders {0}
{
@ -192,13 +198,7 @@ public:
virtual void setSwapInterval() = 0;
virtual void swapBuffers() = 0;
// clang-format off
static constexpr unsigned int SHADER_CORE {0x00000001};
static constexpr unsigned int SHADER_BLUR_HORIZONTAL {0x00000002};
static constexpr unsigned int SHADER_BLUR_VERTICAL {0x00000004};
static constexpr unsigned int SHADER_SCANLINES {0x00000008};
// clang-format on
private:
std::stack<Rect> mClipStack;
SDL_Window* mSDLWindow {nullptr};
glm::mat4 mProjectionMatrix {};

View file

@ -365,9 +365,9 @@ void RendererOpenGL::drawTriangleStrips(const Vertex* vertices,
GL_CHECK_ERROR(glBlendFunc(convertBlendFactor(srcBlendFactorFactor),
convertBlendFactor(dstBlendFactorFactor)));
if (vertices->shaders == 0 || vertices->shaders & SHADER_CORE) {
if (vertices->shaders == 0 || vertices->shaders & Shader::CORE) {
if (mCoreShader == nullptr)
mCoreShader = getShaderProgram(SHADER_CORE);
mCoreShader = getShaderProgram(Shader::CORE);
if (mCoreShader) {
if (mLastShader != mCoreShader)
mCoreShader->activateShaders();
@ -379,16 +379,14 @@ void RendererOpenGL::drawTriangleStrips(const Vertex* vertices,
mCoreShader->setOpacity(vertices->opacity);
mCoreShader->setSaturation(vertices->saturation);
mCoreShader->setDimming(vertices->dimming);
mCoreShader->setBGRAToRGBA(vertices->convertBGRAToRGBA);
mCoreShader->setFont(vertices->font);
mCoreShader->setPostProcessing(vertices->postProcessing);
mCoreShader->setFlags(vertices->shaderFlags);
GL_CHECK_ERROR(glDrawArrays(GL_TRIANGLE_STRIP, 0, numVertices));
mLastShader = mCoreShader;
}
}
else if (vertices->shaders & SHADER_BLUR_HORIZONTAL) {
else if (vertices->shaders & Shader::BLUR_HORIZONTAL) {
if (mBlurHorizontalShader == nullptr)
mBlurHorizontalShader = getShaderProgram(SHADER_BLUR_HORIZONTAL);
mBlurHorizontalShader = getShaderProgram(Shader::BLUR_HORIZONTAL);
if (mBlurHorizontalShader) {
if (mLastShader != mBlurHorizontalShader)
mBlurHorizontalShader->activateShaders();
@ -403,9 +401,9 @@ void RendererOpenGL::drawTriangleStrips(const Vertex* vertices,
}
return;
}
else if (vertices->shaders & SHADER_BLUR_VERTICAL) {
else if (vertices->shaders & Shader::BLUR_VERTICAL) {
if (mBlurVerticalShader == nullptr)
mBlurVerticalShader = getShaderProgram(SHADER_BLUR_VERTICAL);
mBlurVerticalShader = getShaderProgram(Shader::BLUR_VERTICAL);
if (mBlurVerticalShader) {
if (mLastShader != mBlurVerticalShader)
mBlurVerticalShader->activateShaders();
@ -420,9 +418,9 @@ void RendererOpenGL::drawTriangleStrips(const Vertex* vertices,
}
return;
}
else if (vertices->shaders & SHADER_SCANLINES) {
else if (vertices->shaders & Shader::SCANLINES) {
if (mScanlinelShader == nullptr)
mScanlinelShader = getShaderProgram(SHADER_SCANLINES);
mScanlinelShader = getShaderProgram(Shader::SCANLINES);
float shaderWidth {width * 1.2f};
// Scale the scanlines relative to screen resolution.
float screenHeightModifier {getScreenHeightModifier()};
@ -481,16 +479,16 @@ void RendererOpenGL::shaderPostprocessing(unsigned int shaders,
vertices->opacity = parameters.opacity;
vertices->saturation = parameters.saturation;
vertices->dimming = parameters.dimming;
vertices->postProcessing = true;
vertices->shaderFlags = ShaderFlags::POST_PROCESSING;
if (shaders & SHADER_CORE)
shaderList.push_back(SHADER_CORE);
if (shaders & SHADER_BLUR_HORIZONTAL)
shaderList.push_back(SHADER_BLUR_HORIZONTAL);
if (shaders & SHADER_BLUR_VERTICAL)
shaderList.push_back(SHADER_BLUR_VERTICAL);
if (shaders & SHADER_SCANLINES)
shaderList.push_back(SHADER_SCANLINES);
if (shaders & Shader::CORE)
shaderList.push_back(Shader::CORE);
if (shaders & Shader::BLUR_HORIZONTAL)
shaderList.push_back(Shader::BLUR_HORIZONTAL);
if (shaders & Shader::BLUR_VERTICAL)
shaderList.push_back(Shader::BLUR_VERTICAL);
if (shaders & Shader::SCANLINES)
shaderList.push_back(Shader::SCANLINES);
setMatrix(getIdentity());
bindTexture(mPostProcTexture1);
@ -511,8 +509,8 @@ void RendererOpenGL::shaderPostprocessing(unsigned int shaders,
int shaderPasses = 1;
// For the blur shaders there is an optional variable to set the number of passes
// to execute, which proportionally affects the blur amount.
if (shaderList[i] == Renderer::SHADER_BLUR_HORIZONTAL ||
shaderList[i] == Renderer::SHADER_BLUR_VERTICAL) {
if (shaderList[i] == Renderer::Shader::BLUR_HORIZONTAL ||
shaderList[i] == Renderer::Shader::BLUR_VERTICAL) {
shaderPasses = parameters.blurPasses;
}

View file

@ -22,9 +22,7 @@ ShaderOpenGL::ShaderOpenGL()
, mShaderOpacity {0}
, mShaderSaturation {0}
, mShaderDimming {0}
, mShaderBGRAToRGBA {0}
, mShaderFont {0}
, mShaderPostProcessing {0}
, mShaderFlags {0}
{
}
@ -125,9 +123,7 @@ void ShaderOpenGL::getVariableLocations(GLuint programID)
mShaderOpacity = glGetUniformLocation(mProgramID, "opacity");
mShaderSaturation = glGetUniformLocation(mProgramID, "saturation");
mShaderDimming = glGetUniformLocation(mProgramID, "dimming");
mShaderBGRAToRGBA = glGetUniformLocation(mProgramID, "BGRAToRGBA");
mShaderFont = glGetUniformLocation(mProgramID, "font");
mShaderPostProcessing = glGetUniformLocation(mProgramID, "postProcessing");
mShaderFlags = glGetUniformLocation(mProgramID, "shaderFlags");
}
void ShaderOpenGL::setModelViewProjectionMatrix(glm::mat4 mvpMatrix)
@ -178,22 +174,10 @@ void ShaderOpenGL::setDimming(GLfloat dimming)
GL_CHECK_ERROR(glUniform1f(mShaderDimming, dimming));
}
void ShaderOpenGL::setBGRAToRGBA(GLboolean BGRAToRGBA)
void ShaderOpenGL::setFlags(GLuint flags)
{
if (mShaderBGRAToRGBA != -1)
GL_CHECK_ERROR(glUniform1i(mShaderBGRAToRGBA, BGRAToRGBA ? 1 : 0));
}
void ShaderOpenGL::setFont(GLboolean font)
{
if (mShaderFont != -1)
GL_CHECK_ERROR(glUniform1i(mShaderFont, font ? 1 : 0));
}
void ShaderOpenGL::setPostProcessing(GLboolean postProcessing)
{
if (mShaderPostProcessing != -1)
GL_CHECK_ERROR(glUniform1i(mShaderPostProcessing, postProcessing ? 1 : 0));
if (mShaderFlags != -1)
GL_CHECK_ERROR(glUniform1ui(mShaderFlags, flags));
}
void ShaderOpenGL::activateShaders()

View file

@ -71,9 +71,7 @@ public:
void setOpacity(GLfloat opacity);
void setSaturation(GLfloat saturation);
void setDimming(GLfloat dimming);
void setBGRAToRGBA(GLboolean BGRAToRGBA);
void setFont(GLboolean font);
void setPostProcessing(GLboolean postProcessing);
void setFlags(GLuint flags);
// Sets the shader program to use the loaded shaders.
void activateShaders();
// Sets the shader program to 0 which reverts to the fixed function pipeline.
@ -97,9 +95,7 @@ private:
GLint mShaderOpacity;
GLint mShaderSaturation;
GLint mShaderDimming;
GLint mShaderBGRAToRGBA;
GLint mShaderFont;
GLint mShaderPostProcessing;
GLint mShaderFlags;
};
#endif // ES_CORE_RENDERER_SHADER_OPENGL_H

View file

@ -397,7 +397,7 @@ void Font::renderTextCache(TextCache* cache)
assert(*it->textureIdPtr != 0);
auto vertexList = *it;
it->verts[0].font = true;
it->verts[0].shaderFlags = Renderer::ShaderFlags::FONT_TEXTURE;
mRenderer->bindTexture(*it->textureIdPtr);
mRenderer->drawTriangleStrips(&it->verts[0],

View file

@ -63,24 +63,27 @@ COMPAT_VARYING COMPAT_PRECISION vec2 texCoord;
uniform COMPAT_PRECISION float opacity;
uniform COMPAT_PRECISION float saturation;
uniform COMPAT_PRECISION float dimming;
uniform int BGRAToRGBA;
uniform int font;
uniform int postProcessing;
uniform sampler2D myTexture;
uniform unsigned int shaderFlags;
uniform sampler2D textureSampler;
// shaderFlags:
// 0x00000001 - BGRA to RGBA conversion
// 0x00000002 - Font texture
// 0x00000004 - Post processing
void main()
{
COMPAT_PRECISION vec4 sampledColor = COMPAT_TEXTURE(myTexture, texCoord);
COMPAT_PRECISION vec4 sampledColor = COMPAT_TEXTURE(textureSampler, texCoord);
// For fonts the alpha information is stored in the red channel.
if (font == 1)
if (0u != (shaderFlags & 2u))
sampledColor = vec4(1.0f, 1.0f, 1.0f, sampledColor.r);
sampledColor *= color;
// When post-processing we drop the alpha channel to avoid strange issues
// with some graphics drivers.
if (postProcessing == 1)
if (0u != (shaderFlags & 4u))
sampledColor.a = 1.0f;
// Opacity.
@ -101,7 +104,7 @@ void main()
}
// BGRA to RGBA conversion.
if (BGRAToRGBA == 1)
if (0u != (shaderFlags & 1u))
sampledColor = vec4(sampledColor.bgr, sampledColor.a);
FragColor = sampledColor;