- uniform buffers for the tonemap shaders

This commit is contained in:
Magnus Norddahl 2018-06-11 21:18:20 +02:00
parent 2bde741a7e
commit 6fcc79d72a
5 changed files with 54 additions and 26 deletions

View file

@ -223,8 +223,9 @@ void FGLRenderer::UpdateCameraExposure()
mBuffers->BindCurrentTexture(0, GL_LINEAR); mBuffers->BindCurrentTexture(0, GL_LINEAR);
mExposureExtractShader->Bind(); mExposureExtractShader->Bind();
mExposureExtractShader->SceneTexture.Set(0); mExposureExtractShader->SceneTexture.Set(0);
mExposureExtractShader->Scale.Set(mSceneViewport.width / (float)mScreenViewport.width, mSceneViewport.height / (float)mScreenViewport.height); mExposureExtractShader->Uniforms->Scale = { mSceneViewport.width / (float)mScreenViewport.width, mSceneViewport.height / (float)mScreenViewport.height };
mExposureExtractShader->Offset.Set(mSceneViewport.left / (float)mScreenViewport.width, mSceneViewport.top / (float)mScreenViewport.height); mExposureExtractShader->Uniforms->Offset = { mSceneViewport.left / (float)mScreenViewport.width, mSceneViewport.top / (float)mScreenViewport.height };
mExposureExtractShader->Uniforms.Set();
RenderScreenQuad(); RenderScreenQuad();
// Find the average value: // Find the average value:
@ -257,10 +258,11 @@ void FGLRenderer::UpdateCameraExposure()
mBuffers->ExposureLevels.Last().Texture.Bind(0); mBuffers->ExposureLevels.Last().Texture.Bind(0);
mExposureCombineShader->Bind(); mExposureCombineShader->Bind();
mExposureCombineShader->ExposureTexture.Set(0); mExposureCombineShader->ExposureTexture.Set(0);
mExposureCombineShader->ExposureBase.Set(gl_exposure_base); mExposureCombineShader->Uniforms->ExposureBase = gl_exposure_base;
mExposureCombineShader->ExposureMin.Set(gl_exposure_min); mExposureCombineShader->Uniforms->ExposureMin = gl_exposure_min;
mExposureCombineShader->ExposureScale.Set(gl_exposure_scale); mExposureCombineShader->Uniforms->ExposureScale = gl_exposure_scale;
mExposureCombineShader->ExposureSpeed.Set(gl_exposure_speed); mExposureCombineShader->Uniforms->ExposureSpeed = gl_exposure_speed;
mExposureCombineShader->Uniforms.Set();
RenderScreenQuad(); RenderScreenQuad();
glViewport(mScreenViewport.left, mScreenViewport.top, mScreenViewport.width, mScreenViewport.height); glViewport(mScreenViewport.left, mScreenViewport.top, mScreenViewport.width, mScreenViewport.height);

View file

@ -69,14 +69,15 @@ void FExposureExtractShader::Bind()
{ {
if (!mShader) if (!mShader)
{ {
FString prolog = Uniforms.CreateDeclaration("Uniforms", UniformBlock::Desc());
mShader.Compile(FShaderProgram::Vertex, "shaders/glsl/screenquad.vp", "", 330); mShader.Compile(FShaderProgram::Vertex, "shaders/glsl/screenquad.vp", "", 330);
mShader.Compile(FShaderProgram::Fragment, "shaders/glsl/exposureextract.fp", "", 330); mShader.Compile(FShaderProgram::Fragment, "shaders/glsl/exposureextract.fp", prolog, 330);
mShader.SetFragDataLocation(0, "FragColor"); mShader.SetFragDataLocation(0, "FragColor");
mShader.Link("shaders/glsl/exposureextract"); mShader.Link("shaders/glsl/exposureextract");
mShader.SetAttribLocation(0, "PositionInProjection"); mShader.SetAttribLocation(0, "PositionInProjection");
SceneTexture.Init(mShader, "SceneTexture"); SceneTexture.Init(mShader, "SceneTexture");
Scale.Init(mShader, "Scale"); Uniforms.Init(mShader);
Offset.Init(mShader, "Offset");
} }
mShader.Bind(); mShader.Bind();
} }
@ -99,16 +100,15 @@ void FExposureCombineShader::Bind()
{ {
if (!mShader) if (!mShader)
{ {
FString prolog = Uniforms.CreateDeclaration("Uniforms", UniformBlock::Desc());
mShader.Compile(FShaderProgram::Vertex, "shaders/glsl/screenquad.vp", "", 330); mShader.Compile(FShaderProgram::Vertex, "shaders/glsl/screenquad.vp", "", 330);
mShader.Compile(FShaderProgram::Fragment, "shaders/glsl/exposurecombine.fp", "", 330); mShader.Compile(FShaderProgram::Fragment, "shaders/glsl/exposurecombine.fp", prolog, 330);
mShader.SetFragDataLocation(0, "FragColor"); mShader.SetFragDataLocation(0, "FragColor");
mShader.Link("shaders/glsl/exposurecombine"); mShader.Link("shaders/glsl/exposurecombine");
mShader.SetAttribLocation(0, "PositionInProjection"); mShader.SetAttribLocation(0, "PositionInProjection");
ExposureTexture.Init(mShader, "ExposureTexture"); ExposureTexture.Init(mShader, "ExposureTexture");
ExposureBase.Init(mShader, "ExposureBase"); Uniforms.Init(mShader);
ExposureMin.Init(mShader, "ExposureMin");
ExposureScale.Init(mShader, "ExposureScale");
ExposureSpeed.Init(mShader, "ExposureSpeed");
} }
mShader.Bind(); mShader.Bind();
} }

View file

@ -37,8 +37,23 @@ public:
void Bind(); void Bind();
FBufferedUniformSampler SceneTexture; FBufferedUniformSampler SceneTexture;
FBufferedUniform2f Scale;
FBufferedUniform2f Offset; struct UniformBlock
{
FVector2 Scale;
FVector2 Offset;
static std::vector<UniformFieldDesc> Desc()
{
return
{
{ "Scale", UniformType::Vec2, offsetof(UniformBlock, Scale) },
{ "Offset", UniformType::Vec2, offsetof(UniformBlock, Offset) }
};
}
};
ShaderUniforms<UniformBlock> Uniforms;
private: private:
FShaderProgram mShader; FShaderProgram mShader;
@ -61,10 +76,27 @@ public:
void Bind(); void Bind();
FBufferedUniformSampler ExposureTexture; FBufferedUniformSampler ExposureTexture;
FBufferedUniform1f ExposureBase;
FBufferedUniform1f ExposureMin; struct UniformBlock
FBufferedUniform1f ExposureScale; {
FBufferedUniform1f ExposureSpeed; float ExposureBase;
float ExposureMin;
float ExposureScale;
float ExposureSpeed;
static std::vector<UniformFieldDesc> Desc()
{
return
{
{ "ExposureBase", UniformType::Float, offsetof(UniformBlock, ExposureBase) },
{ "ExposureMin", UniformType::Float, offsetof(UniformBlock, ExposureMin) },
{ "ExposureScale", UniformType::Float, offsetof(UniformBlock, ExposureScale) },
{ "ExposureSpeed", UniformType::Float, offsetof(UniformBlock, ExposureSpeed) }
};
}
};
ShaderUniforms<UniformBlock> Uniforms;
private: private:
FShaderProgram mShader; FShaderProgram mShader;

View file

@ -3,10 +3,6 @@ in vec2 TexCoord;
out vec4 FragColor; out vec4 FragColor;
uniform sampler2D ExposureTexture; uniform sampler2D ExposureTexture;
uniform float ExposureBase;
uniform float ExposureMin;
uniform float ExposureScale;
uniform float ExposureSpeed;
void main() void main()
{ {

View file

@ -3,8 +3,6 @@ in vec2 TexCoord;
out vec4 FragColor; out vec4 FragColor;
uniform sampler2D SceneTexture; uniform sampler2D SceneTexture;
uniform vec2 Scale;
uniform vec2 Offset;
void main() void main()
{ {