- uniform buffers for the present and shadowmap shaders

This commit is contained in:
Magnus Norddahl 2018-06-11 21:42:09 +02:00
parent 6fcc79d72a
commit 48f753061a
15 changed files with 87 additions and 71 deletions

View File

@ -52,7 +52,8 @@ void FShadowMap::Update()
GLRenderer->mBuffers->BindShadowMapFB();
GLRenderer->mShadowMapShader->Bind();
GLRenderer->mShadowMapShader->ShadowmapQuality.Set(gl_shadowmap_quality);
GLRenderer->mShadowMapShader->Uniforms->ShadowmapQuality = gl_shadowmap_quality;
GLRenderer->mShadowMapShader->Uniforms.Set();
glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 4, mLightList);
glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 2, mNodesBuffer);
glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 3, mLinesBuffer);

View File

@ -735,20 +735,21 @@ void FGLRenderer::DrawPresentTexture(const IntRect &box, bool applyGamma)
mPresentShader->InputTexture.Set(0);
if (!applyGamma || framebuffer->IsHWGammaActive())
{
mPresentShader->InvGamma.Set(1.0f);
mPresentShader->Contrast.Set(1.0f);
mPresentShader->Brightness.Set(0.0f);
mPresentShader->Saturation.Set(1.0f);
mPresentShader->Uniforms->InvGamma = 1.0f;
mPresentShader->Uniforms->Contrast = 1.0f;
mPresentShader->Uniforms->Brightness = 0.0f;
mPresentShader->Uniforms->Saturation = 1.0f;
}
else
{
mPresentShader->InvGamma.Set(1.0f / clamp<float>(Gamma, 0.1f, 4.f));
mPresentShader->Contrast.Set(clamp<float>(vid_contrast, 0.1f, 3.f));
mPresentShader->Brightness.Set(clamp<float>(vid_brightness, -0.8f, 0.8f));
mPresentShader->Saturation.Set(clamp<float>(vid_saturation, -15.0f, 15.f));
mPresentShader->GrayFormula.Set(static_cast<int>(gl_satformula));
mPresentShader->Uniforms->InvGamma = 1.0f / clamp<float>(Gamma, 0.1f, 4.f);
mPresentShader->Uniforms->Contrast = clamp<float>(vid_contrast, 0.1f, 3.f);
mPresentShader->Uniforms->Brightness = clamp<float>(vid_brightness, -0.8f, 0.8f);
mPresentShader->Uniforms->Saturation = clamp<float>(vid_saturation, -15.0f, 15.f);
mPresentShader->Uniforms->GrayFormula = static_cast<int>(gl_satformula);
}
mPresentShader->Scale.Set(screen->mScreenViewport.width / (float)mBuffers->GetWidth(), screen->mScreenViewport.height / (float)mBuffers->GetHeight());
mPresentShader->Uniforms->Scale = { screen->mScreenViewport.width / (float)mBuffers->GetWidth(), screen->mScreenViewport.height / (float)mBuffers->GetHeight() };
mPresentShader->Uniforms.Set();
RenderScreenQuad();
}

View File

@ -35,7 +35,6 @@ void FPresentStereoShaderBase::Init(const char * vtx_shader_name, const char * p
FPresentShaderBase::Init(vtx_shader_name, program_name);
LeftEyeTexture.Init(mShader, "LeftEyeTexture");
RightEyeTexture.Init(mShader, "RightEyeTexture");
WindowPositionParity.Init(mShader, "WindowPositionParity");
}
void FPresent3DCheckerShader::Bind()

View File

@ -36,7 +36,6 @@ class FPresentStereoShaderBase : public FPresentShaderBase
public:
FBufferedUniformSampler LeftEyeTexture;
FBufferedUniformSampler RightEyeTexture;
FBufferedUniform1i WindowPositionParity;
protected:
void Init(const char * vtx_shader_name, const char * program_name) override;

View File

@ -31,18 +31,15 @@
void FPresentShaderBase::Init(const char * vtx_shader_name, const char * program_name)
{
mShader.Compile(FShaderProgram::Vertex, "shaders/glsl/screenquadscale.vp", "", 330);
mShader.Compile(FShaderProgram::Fragment, vtx_shader_name, "", 330);
FString prolog = Uniforms.CreateDeclaration("Uniforms", UniformBlock::Desc());
mShader.Compile(FShaderProgram::Vertex, "shaders/glsl/screenquadscale.vp", prolog, 330);
mShader.Compile(FShaderProgram::Fragment, vtx_shader_name, prolog, 330);
mShader.SetFragDataLocation(0, "FragColor");
mShader.Link(program_name);
mShader.SetAttribLocation(0, "PositionInProjection");
mShader.SetAttribLocation(1, "UV");
InvGamma.Init(mShader, "InvGamma");
Contrast.Init(mShader, "Contrast");
Brightness.Init(mShader, "Brightness");
Saturation.Init(mShader, "Saturation");
GrayFormula.Init(mShader, "GrayFormula");
Scale.Init(mShader, "UVScale");
Uniforms.Init(mShader);
}
void FPresentShader::Bind()

View File

@ -9,12 +9,32 @@ public:
virtual ~FPresentShaderBase() {}
virtual void Bind() = 0;
FBufferedUniform1f InvGamma;
FBufferedUniform1f Contrast;
FBufferedUniform1f Brightness;
FBufferedUniform1f Saturation;
FBufferedUniform1i GrayFormula;
FBufferedUniform2f Scale;
struct UniformBlock
{
float InvGamma;
float Contrast;
float Brightness;
float Saturation;
int GrayFormula;
int WindowPositionParity; // top-of-window might not be top-of-screen
FVector2 Scale;
static std::vector<UniformFieldDesc> Desc()
{
return
{
{ "InvGamma", UniformType::Float, offsetof(UniformBlock, InvGamma) },
{ "Contrast", UniformType::Float, offsetof(UniformBlock, Contrast) },
{ "Brightness", UniformType::Float, offsetof(UniformBlock, Brightness) },
{ "Saturation", UniformType::Float, offsetof(UniformBlock, Saturation) },
{ "GrayFormula", UniformType::Int, offsetof(UniformBlock, GrayFormula) },
{ "WindowPositionParity", UniformType::Int, offsetof(UniformBlock, WindowPositionParity) },
{ "UVScale", UniformType::Vec2, offsetof(UniformBlock, Scale) },
};
}
};
ShaderUniforms<UniformBlock> Uniforms;
protected:
virtual void Init(const char * vtx_shader_name, const char * program_name);

View File

@ -28,12 +28,14 @@ void FShadowMapShader::Bind()
{
if (!mShader)
{
FString prolog = Uniforms.CreateDeclaration("Uniforms", UniformBlock::Desc());
mShader.Compile(FShaderProgram::Vertex, "shaders/glsl/screenquad.vp", "", 430);
mShader.Compile(FShaderProgram::Fragment, "shaders/glsl/shadowmap.fp", "", 430);
mShader.Compile(FShaderProgram::Fragment, "shaders/glsl/shadowmap.fp", prolog, 430);
mShader.SetFragDataLocation(0, "FragColor");
mShader.Link("shaders/glsl/shadowmap");
mShader.SetAttribLocation(0, "PositionInProjection");
ShadowmapQuality.Init(mShader, "ShadowmapQuality");
Uniforms.Init(mShader);
}
mShader.Bind();
}

View File

@ -8,7 +8,24 @@ class FShadowMapShader
public:
void Bind();
FBufferedUniform1f ShadowmapQuality;
struct UniformBlock
{
float ShadowmapQuality;
float Padding0, Padding1, Padding2;
static std::vector<UniformFieldDesc> Desc()
{
return
{
{ "ShadowmapQuality", UniformType::Float, offsetof(UniformBlock, ShadowmapQuality) },
{ "Padding0", UniformType::Float, offsetof(UniformBlock, Padding0) },
{ "Padding1", UniformType::Float, offsetof(UniformBlock, Padding1) },
{ "Padding2", UniformType::Float, offsetof(UniformBlock, Padding2) },
};
}
};
ShaderUniforms<UniformBlock> Uniforms;
private:
FShaderProgram mShader;

View File

@ -101,22 +101,24 @@ static void prepareInterleavedPresent(FPresentStereoShaderBase& shader)
if ( GLRenderer->framebuffer->IsHWGammaActive() )
{
shader.InvGamma.Set(1.0f);
shader.Contrast.Set(1.0f);
shader.Brightness.Set(0.0f);
shader.Saturation.Set(1.0f);
shader.Uniforms->InvGamma = 1.0f;
shader.Uniforms->Contrast = 1.0f;
shader.Uniforms->Brightness = 0.0f;
shader.Uniforms->Saturation = 1.0f;
}
else
{
shader.InvGamma.Set(1.0f / clamp<float>(Gamma, 0.1f, 4.f));
shader.Contrast.Set(clamp<float>(vid_contrast, 0.1f, 3.f));
shader.Brightness.Set(clamp<float>(vid_brightness, -0.8f, 0.8f));
shader.Saturation.Set(clamp<float>(vid_saturation, -15.0f, 15.0f));
shader.GrayFormula.Set(static_cast<int>(gl_satformula));
shader.Uniforms->InvGamma = 1.0f / clamp<float>(Gamma, 0.1f, 4.f);
shader.Uniforms->Contrast = clamp<float>(vid_contrast, 0.1f, 3.f);
shader.Uniforms->Brightness = clamp<float>(vid_brightness, -0.8f, 0.8f);
shader.Uniforms->Saturation = clamp<float>(vid_saturation, -15.0f, 15.0f);
shader.Uniforms->GrayFormula = static_cast<int>(gl_satformula);
}
shader.Scale.Set(
shader.Uniforms->Scale = {
screen->mScreenViewport.width / (float)GLRenderer->mBuffers->GetWidth(),
screen->mScreenViewport.height / (float)GLRenderer->mBuffers->GetHeight());
screen->mScreenViewport.height / (float)GLRenderer->mBuffers->GetHeight()
};
shader.Uniforms.Set();
}
// fixme: I don't know how to get absolute window position on Mac and Linux
@ -147,13 +149,13 @@ void CheckerInterleaved3D::Present() const
}
#endif // _WIN32
GLRenderer->mPresent3dCheckerShader->WindowPositionParity.Set(
GLRenderer->mPresent3dCheckerShader->Uniforms->WindowPositionParity =
(windowVOffset
+ windowHOffset
+ screen->mOutputLetterbox.height + 1 // +1 because of origin at bottom
) % 2 // because we want the top pixel offset, but gl_FragCoord.y is the bottom pixel offset
);
) % 2; // because we want the top pixel offset, but gl_FragCoord.y is the bottom pixel offset
GLRenderer->mPresent3dCheckerShader->Uniforms.Set();
GLRenderer->RenderScreenQuad();
}
@ -190,7 +192,8 @@ void ColumnInterleaved3D::Present() const
}
#endif // _WIN32
GLRenderer->mPresent3dColumnShader->WindowPositionParity.Set(windowHOffset);
GLRenderer->mPresent3dColumnShader->Uniforms->WindowPositionParity = windowHOffset;
GLRenderer->mPresent3dColumnShader->Uniforms.Set();
GLRenderer->RenderScreenQuad();
}
@ -212,12 +215,12 @@ void RowInterleaved3D::Present() const
}
#endif // _WIN32
GLRenderer->mPresent3dRowShader->WindowPositionParity.Set(
GLRenderer->mPresent3dRowShader->Uniforms->WindowPositionParity =
(windowVOffset
+ screen->mOutputLetterbox.height + 1 // +1 because of origin at bottom
) % 2
);
) % 2;
GLRenderer->mPresent3dColumnShader->Uniforms.Set();
GLRenderer->RenderScreenQuad();
}

View File

@ -3,11 +3,6 @@ in vec2 TexCoord;
out vec4 FragColor;
uniform sampler2D InputTexture;
uniform float InvGamma;
uniform float Contrast;
uniform float Brightness;
uniform float Saturation;
uniform int GrayFormula;
vec4 ApplyGamma(vec4 c)
{

View File

@ -4,10 +4,6 @@ out vec4 FragColor;
uniform sampler2D LeftEyeTexture;
uniform sampler2D RightEyeTexture;
uniform float InvGamma;
uniform float Contrast;
uniform float Brightness;
uniform int WindowPositionParity; // top-of-window might not be top-of-screen
vec4 ApplyGamma(vec4 c)
{

View File

@ -4,10 +4,6 @@ out vec4 FragColor;
uniform sampler2D LeftEyeTexture;
uniform sampler2D RightEyeTexture;
uniform float InvGamma;
uniform float Contrast;
uniform float Brightness;
uniform int WindowPositionParity; // top-of-window might not be top-of-screen
vec4 ApplyGamma(vec4 c)
{

View File

@ -4,10 +4,6 @@ out vec4 FragColor;
uniform sampler2D LeftEyeTexture;
uniform sampler2D RightEyeTexture;
uniform float InvGamma;
uniform float Contrast;
uniform float Brightness;
uniform int WindowPositionParity; // top-of-window might not be top-of-screen
vec4 ApplyGamma(vec4 c)
{

View File

@ -1,7 +1,6 @@
in vec4 PositionInProjection;
in vec2 UV;
uniform vec2 UVScale;
out vec2 TexCoord;
void main()

View File

@ -2,11 +2,6 @@
in vec2 TexCoord;
out vec4 FragColor;
// This constant must match the same constant in gl_shadowmap.h
// #define ShadowmapQuality 1024
//#define ShadowmapQuality 128
uniform float ShadowmapQuality;
struct GPUNode
{
vec2 aabb_min;