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

View file

@ -69,14 +69,15 @@ void FExposureExtractShader::Bind()
{
if (!mShader)
{
FString prolog = Uniforms.CreateDeclaration("Uniforms", UniformBlock::Desc());
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.Link("shaders/glsl/exposureextract");
mShader.SetAttribLocation(0, "PositionInProjection");
SceneTexture.Init(mShader, "SceneTexture");
Scale.Init(mShader, "Scale");
Offset.Init(mShader, "Offset");
Uniforms.Init(mShader);
}
mShader.Bind();
}
@ -99,16 +100,15 @@ void FExposureCombineShader::Bind()
{
if (!mShader)
{
FString prolog = Uniforms.CreateDeclaration("Uniforms", UniformBlock::Desc());
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.Link("shaders/glsl/exposurecombine");
mShader.SetAttribLocation(0, "PositionInProjection");
ExposureTexture.Init(mShader, "ExposureTexture");
ExposureBase.Init(mShader, "ExposureBase");
ExposureMin.Init(mShader, "ExposureMin");
ExposureScale.Init(mShader, "ExposureScale");
ExposureSpeed.Init(mShader, "ExposureSpeed");
Uniforms.Init(mShader);
}
mShader.Bind();
}

View file

@ -37,8 +37,23 @@ public:
void Bind();
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:
FShaderProgram mShader;
@ -61,10 +76,27 @@ public:
void Bind();
FBufferedUniformSampler ExposureTexture;
FBufferedUniform1f ExposureBase;
FBufferedUniform1f ExposureMin;
FBufferedUniform1f ExposureScale;
FBufferedUniform1f ExposureSpeed;
struct UniformBlock
{
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:
FShaderProgram mShader;

View file

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

View file

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