- uniform buffers for the fxaa, blur and bloom shaders

This commit is contained in:
Magnus Norddahl 2018-06-11 22:06:46 +02:00
parent 48f753061a
commit 49073489e5
8 changed files with 47 additions and 24 deletions

View file

@ -302,8 +302,9 @@ void FGLRenderer::BloomScene(int fixedcm)
mBloomExtractShader->Bind(); mBloomExtractShader->Bind();
mBloomExtractShader->SceneTexture.Set(0); mBloomExtractShader->SceneTexture.Set(0);
mBloomExtractShader->ExposureTexture.Set(1); mBloomExtractShader->ExposureTexture.Set(1);
mBloomExtractShader->Scale.Set(mSceneViewport.width / (float)mScreenViewport.width, mSceneViewport.height / (float)mScreenViewport.height); mBloomExtractShader->Uniforms->Scale = { mSceneViewport.width / (float)mScreenViewport.width, mSceneViewport.height / (float)mScreenViewport.height };
mBloomExtractShader->Offset.Set(mSceneViewport.left / (float)mScreenViewport.width, mSceneViewport.top / (float)mScreenViewport.height); mBloomExtractShader->Uniforms->Offset = { mSceneViewport.left / (float)mScreenViewport.width, mSceneViewport.top / (float)mScreenViewport.height };
mBloomExtractShader->Uniforms.Set();
RenderScreenQuad(); RenderScreenQuad();
// Blur and downscale: // Blur and downscale:
@ -611,12 +612,6 @@ void FGLRenderer::ApplyFXAA()
FGLDebug::PushGroup("ApplyFXAA"); FGLDebug::PushGroup("ApplyFXAA");
const GLfloat rpcRes[2] =
{
1.0f / mBuffers->GetWidth(),
1.0f / mBuffers->GetHeight()
};
FGLPostProcessState savedState; FGLPostProcessState savedState;
mBuffers->BindNextFB(); mBuffers->BindNextFB();
@ -630,7 +625,8 @@ void FGLRenderer::ApplyFXAA()
mBuffers->BindCurrentTexture(0, GL_LINEAR); mBuffers->BindCurrentTexture(0, GL_LINEAR);
mFXAAShader->Bind(); mFXAAShader->Bind();
mFXAAShader->InputTexture.Set(0); mFXAAShader->InputTexture.Set(0);
mFXAAShader->ReciprocalResolution.Set(rpcRes); mFXAAShader->Uniforms->ReciprocalResolution = { 1.0f / mBuffers->GetWidth(), 1.0f / mBuffers->GetHeight() };
mFXAAShader->Uniforms.Set();
RenderScreenQuad(); RenderScreenQuad();
mBuffers->NextTexture(); mBuffers->NextTexture();

View file

@ -33,15 +33,16 @@ void FBloomExtractShader::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/bloomextract.fp", "", 330); mShader.Compile(FShaderProgram::Fragment, "shaders/glsl/bloomextract.fp", prolog, 330);
mShader.SetFragDataLocation(0, "FragColor"); mShader.SetFragDataLocation(0, "FragColor");
mShader.Link("shaders/glsl/bloomextract"); mShader.Link("shaders/glsl/bloomextract");
mShader.SetAttribLocation(0, "PositionInProjection"); mShader.SetAttribLocation(0, "PositionInProjection");
SceneTexture.Init(mShader, "SceneTexture"); SceneTexture.Init(mShader, "SceneTexture");
ExposureTexture.Init(mShader, "ExposureTexture"); ExposureTexture.Init(mShader, "ExposureTexture");
Scale.Init(mShader, "Scale"); Uniforms.Init(mShader);
Offset.Init(mShader, "Offset");
} }
mShader.Bind(); mShader.Bind();
} }

View file

@ -10,8 +10,23 @@ public:
FBufferedUniformSampler SceneTexture; FBufferedUniformSampler SceneTexture;
FBufferedUniformSampler ExposureTexture; FBufferedUniformSampler ExposureTexture;
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;

View file

@ -25,8 +25,6 @@ private:
int sampleCount; int sampleCount;
std::shared_ptr<FShaderProgram> VerticalShader; std::shared_ptr<FShaderProgram> VerticalShader;
std::shared_ptr<FShaderProgram> HorizontalShader; std::shared_ptr<FShaderProgram> HorizontalShader;
FBufferedUniform1f VerticalScaleX, VerticalScaleY;
FBufferedUniform1f HorizontalScaleX, HorizontalScaleY;
}; };
BlurSetup *GetSetup(float blurAmount, int sampleCount); BlurSetup *GetSetup(float blurAmount, int sampleCount);

View file

@ -83,16 +83,16 @@ void FFXAAShader::Bind()
if (!shader) if (!shader)
{ {
const FString defines = GetDefines(); const FString prolog = Uniforms.CreateDeclaration("Uniforms", UniformBlock::Desc()) + GetDefines();
const int maxVersion = GetMaxVersion(); const int maxVersion = GetMaxVersion();
shader.Compile(FShaderProgram::Vertex, "shaders/glsl/screenquad.vp", "", 330); shader.Compile(FShaderProgram::Vertex, "shaders/glsl/screenquad.vp", "", 330);
shader.Compile(FShaderProgram::Fragment, "shaders/glsl/fxaa.fp", defines, maxVersion); shader.Compile(FShaderProgram::Fragment, "shaders/glsl/fxaa.fp", prolog, maxVersion);
shader.SetFragDataLocation(0, "FragColor"); shader.SetFragDataLocation(0, "FragColor");
shader.Link("shaders/glsl/fxaa"); shader.Link("shaders/glsl/fxaa");
shader.SetAttribLocation(0, "PositionInProjection"); shader.SetAttribLocation(0, "PositionInProjection");
InputTexture.Init(shader, "InputTexture"); InputTexture.Init(shader, "InputTexture");
ReciprocalResolution.Init(shader, "ReciprocalResolution"); Uniforms.Init(shader);
} }
shader.Bind(); shader.Bind();

View file

@ -48,7 +48,24 @@ public:
void Bind(); void Bind();
FBufferedUniform1i InputTexture; FBufferedUniform1i InputTexture;
FBufferedUniform2f ReciprocalResolution;
struct UniformBlock
{
FVector2 ReciprocalResolution;
float Padding0, Padding1;
static std::vector<UniformFieldDesc> Desc()
{
return
{
{ "ReciprocalResolution", UniformType::Vec2, offsetof(UniformBlock, ReciprocalResolution) },
{ "Padding0", UniformType::Float, offsetof(UniformBlock, Padding0) },
{ "Padding1", UniformType::Float, offsetof(UniformBlock, Padding1) }
};
}
};
ShaderUniforms<UniformBlock> Uniforms;
private: private:
FShaderProgram mShaders[Count]; FShaderProgram mShaders[Count];

View file

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

View file

@ -605,8 +605,6 @@ FxaaFloat4 FxaaPixelShader(FxaaFloat2 pos, FxaaTex tex, FxaaFloat2 fxaaQualityRc
#endif #endif
} }
uniform vec2 ReciprocalResolution;
void main() void main()
{ {
FragColor = FxaaPixelShader(TexCoord, InputTexture, ReciprocalResolution, 0.75f, 0.166f, 0.0833f); FragColor = FxaaPixelShader(TexCoord, InputTexture, ReciprocalResolution, 0.75f, 0.166f, 0.0833f);