- change ssao pass to use an uniform block

This commit is contained in:
Magnus Norddahl 2018-06-11 20:48:43 +02:00
parent 29d2e77840
commit 763c5c9769
9 changed files with 283 additions and 113 deletions

View file

@ -118,13 +118,14 @@ void FGLRenderer::AmbientOccludeScene()
mLinearDepthShader->Bind();
mLinearDepthShader->DepthTexture.Set(0);
mLinearDepthShader->ColorTexture.Set(1);
if (gl_multisample > 1) mLinearDepthShader->SampleIndex.Set(0);
mLinearDepthShader->LinearizeDepthA.Set(1.0f / GetZFar() - 1.0f / GetZNear());
mLinearDepthShader->LinearizeDepthB.Set(MAX(1.0f / GetZNear(), 1.e-8f));
mLinearDepthShader->InverseDepthRangeA.Set(1.0f);
mLinearDepthShader->InverseDepthRangeB.Set(0.0f);
mLinearDepthShader->Scale.Set(sceneScaleX, sceneScaleY);
mLinearDepthShader->Offset.Set(sceneOffsetX, sceneOffsetY);
if (gl_multisample > 1) mLinearDepthShader->Uniforms->SampleIndex = 0;
mLinearDepthShader->Uniforms->LinearizeDepthA = 1.0f / GetZFar() - 1.0f / GetZNear();
mLinearDepthShader->Uniforms->LinearizeDepthB = MAX(1.0f / GetZNear(), 1.e-8f);
mLinearDepthShader->Uniforms->InverseDepthRangeA = 1.0f;
mLinearDepthShader->Uniforms->InverseDepthRangeB = 0.0f;
mLinearDepthShader->Uniforms->Scale = { sceneScaleX, sceneScaleY };
mLinearDepthShader->Uniforms->Offset = { sceneOffsetX, sceneOffsetY };
mLinearDepthShader->Uniforms.Set();
RenderScreenQuad();
// Apply ambient occlusion
@ -136,17 +137,18 @@ void FGLRenderer::AmbientOccludeScene()
mSSAOShader->DepthTexture.Set(0);
mSSAOShader->RandomTexture.Set(1);
mSSAOShader->NormalTexture.Set(2);
if (gl_multisample > 1) mSSAOShader->SampleIndex.Set(0);
mSSAOShader->UVToViewA.Set(2.0f * invFocalLenX, 2.0f * invFocalLenY);
mSSAOShader->UVToViewB.Set(-invFocalLenX, -invFocalLenY);
mSSAOShader->InvFullResolution.Set(1.0f / mBuffers->AmbientWidth, 1.0f / mBuffers->AmbientHeight);
mSSAOShader->NDotVBias.Set(nDotVBias);
mSSAOShader->NegInvR2.Set(-1.0f / r2);
mSSAOShader->RadiusToScreen.Set(aoRadius * 0.5 / tanHalfFovy * mBuffers->AmbientHeight);
mSSAOShader->AOMultiplier.Set(1.0f / (1.0f - nDotVBias));
mSSAOShader->AOStrength.Set(aoStrength);
mSSAOShader->Scale.Set(sceneScaleX, sceneScaleY);
mSSAOShader->Offset.Set(sceneOffsetX, sceneOffsetY);
if (gl_multisample > 1) mSSAOShader->Uniforms->SampleIndex = 0;
mSSAOShader->Uniforms->UVToViewA = { 2.0f * invFocalLenX, 2.0f * invFocalLenY };
mSSAOShader->Uniforms->UVToViewB = { -invFocalLenX, -invFocalLenY };
mSSAOShader->Uniforms->InvFullResolution = { 1.0f / mBuffers->AmbientWidth, 1.0f / mBuffers->AmbientHeight };
mSSAOShader->Uniforms->NDotVBias = nDotVBias;
mSSAOShader->Uniforms->NegInvR2 = -1.0f / r2;
mSSAOShader->Uniforms->RadiusToScreen = aoRadius * 0.5 / tanHalfFovy * mBuffers->AmbientHeight;
mSSAOShader->Uniforms->AOMultiplier = 1.0f / (1.0f - nDotVBias);
mSSAOShader->Uniforms->AOStrength = aoStrength;
mSSAOShader->Uniforms->Scale = { sceneScaleX, sceneScaleY };
mSSAOShader->Uniforms->Offset = { sceneOffsetX, sceneOffsetY };
mSSAOShader->Uniforms.Set();
RenderScreenQuad();
// Blur SSAO texture
@ -155,16 +157,18 @@ void FGLRenderer::AmbientOccludeScene()
mBuffers->AmbientFB0.Bind();
mBuffers->AmbientTexture1.Bind(0);
mDepthBlurShader->Bind(false);
mDepthBlurShader->BlurSharpness[false].Set(blurSharpness);
mDepthBlurShader->InvFullResolution[false].Set(1.0f / mBuffers->AmbientWidth, 1.0f / mBuffers->AmbientHeight);
mDepthBlurShader->Uniforms[false]->BlurSharpness = blurSharpness;
mDepthBlurShader->Uniforms[false]->InvFullResolution = { 1.0f / mBuffers->AmbientWidth, 1.0f / mBuffers->AmbientHeight };
mDepthBlurShader->Uniforms[false].Set();
RenderScreenQuad();
mBuffers->AmbientFB1.Bind();
mBuffers->AmbientTexture0.Bind(0);
mDepthBlurShader->Bind(true);
mDepthBlurShader->BlurSharpness[true].Set(blurSharpness);
mDepthBlurShader->InvFullResolution[true].Set(1.0f / mBuffers->AmbientWidth, 1.0f / mBuffers->AmbientHeight);
mDepthBlurShader->PowExponent[true].Set(gl_ssao_exponent);
mDepthBlurShader->Uniforms[true]->BlurSharpness = blurSharpness;
mDepthBlurShader->Uniforms[true]->InvFullResolution = { 1.0f / mBuffers->AmbientWidth, 1.0f / mBuffers->AmbientHeight };
mDepthBlurShader->Uniforms[true]->PowExponent = gl_ssao_exponent;
mDepthBlurShader->Uniforms[true].Set();
RenderScreenQuad();
}
@ -184,9 +188,10 @@ void FGLRenderer::AmbientOccludeScene()
mSSAOCombineShader->Bind();
mSSAOCombineShader->AODepthTexture.Set(0);
mSSAOCombineShader->SceneFogTexture.Set(1);
if (gl_multisample > 1) mSSAOCombineShader->SampleCount.Set(gl_multisample);
mSSAOCombineShader->Scale.Set(sceneScaleX, sceneScaleY);
mSSAOCombineShader->Offset.Set(sceneOffsetX, sceneOffsetY);
if (gl_multisample > 1) mSSAOCombineShader->Uniforms->SampleCount = gl_multisample;
mSSAOCombineShader->Uniforms->Scale = { sceneScaleX, sceneScaleY };
mSSAOCombineShader->Uniforms->Offset = { sceneOffsetX, sceneOffsetY };
mSSAOCombineShader->Uniforms.Set();
RenderScreenQuad();
FGLDebug::PopGroup();

View file

@ -33,21 +33,18 @@ void FLinearDepthShader::Bind()
if (!mShader)
{
FString prolog = Uniforms.CreateDeclaration("Uniforms", UniformBlock::Desc());
if (multisample) prolog += "#define MULTISAMPLE\n";
mShader.reset(new FShaderProgram());
mShader->Compile(FShaderProgram::Vertex, "shaders/glsl/screenquad.vp", "", 330);
mShader->Compile(FShaderProgram::Fragment, "shaders/glsl/lineardepth.fp", multisample ? "#define MULTISAMPLE\n" : "", 330);
mShader->Compile(FShaderProgram::Fragment, "shaders/glsl/lineardepth.fp", prolog, 330);
mShader->SetFragDataLocation(0, "FragColor");
mShader->Link("shaders/glsl/lineardepth");
mShader->SetAttribLocation(0, "PositionInProjection");
DepthTexture.Init(*mShader, "DepthTexture");
ColorTexture.Init(*mShader, "ColorTexture");
SampleIndex.Init(*mShader, "SampleIndex");
LinearizeDepthA.Init(*mShader, "LinearizeDepthA");
LinearizeDepthB.Init(*mShader, "LinearizeDepthB");
InverseDepthRangeA.Init(*mShader, "InverseDepthRangeA");
InverseDepthRangeB.Init(*mShader, "InverseDepthRangeB");
Scale.Init(*mShader, "Scale");
Offset.Init(*mShader, "Offset");
Uniforms.Init(*mShader);
mMultisample = multisample;
}
mShader->Bind();
@ -61,26 +58,19 @@ void FSSAOShader::Bind()
if (!mShader)
{
FString prolog = Uniforms.CreateDeclaration("Uniforms", UniformBlock::Desc());
prolog += GetDefines(gl_ssao, multisample);
mShader.reset(new FShaderProgram());
mShader->Compile(FShaderProgram::Vertex, "shaders/glsl/screenquad.vp", "", 330);
mShader->Compile(FShaderProgram::Fragment, "shaders/glsl/ssao.fp", GetDefines(gl_ssao, multisample), 330);
mShader->Compile(FShaderProgram::Fragment, "shaders/glsl/ssao.fp", prolog, 330);
mShader->SetFragDataLocation(0, "FragColor");
mShader->Link("shaders/glsl/ssao");
mShader->SetAttribLocation(0, "PositionInProjection");
DepthTexture.Init(*mShader, "DepthTexture");
NormalTexture.Init(*mShader, "NormalTexture");
RandomTexture.Init(*mShader, "RandomTexture");
UVToViewA.Init(*mShader, "UVToViewA");
UVToViewB.Init(*mShader, "UVToViewB");
InvFullResolution.Init(*mShader, "InvFullResolution");
NDotVBias.Init(*mShader, "NDotVBias");
NegInvR2.Init(*mShader, "NegInvR2");
RadiusToScreen.Init(*mShader, "RadiusToScreen");
AOMultiplier.Init(*mShader, "AOMultiplier");
AOStrength.Init(*mShader, "AOStrength");
Scale.Init(*mShader, "Scale");
Offset.Init(*mShader, "Offset");
SampleIndex.Init(*mShader, "SampleIndex");
Uniforms.Init(*mShader);
mMultisample = multisample;
}
mShader->Bind();
@ -116,15 +106,19 @@ void FDepthBlurShader::Bind(bool vertical)
auto &shader = mShader[vertical];
if (!shader)
{
FString prolog = Uniforms[vertical].CreateDeclaration("Uniforms", UniformBlock::Desc());
if (vertical)
prolog += "#define BLUR_VERTICAL\n";
else
prolog += "#define BLUR_HORIZONTAL\n";
shader.Compile(FShaderProgram::Vertex, "shaders/glsl/screenquad.vp", "", 330);
shader.Compile(FShaderProgram::Fragment, "shaders/glsl/depthblur.fp", vertical ? "#define BLUR_VERTICAL\n" : "#define BLUR_HORIZONTAL\n", 330);
shader.Compile(FShaderProgram::Fragment, "shaders/glsl/depthblur.fp", prolog, 330);
shader.SetFragDataLocation(0, "FragColor");
shader.Link("shaders/glsl/depthblur");
shader.SetAttribLocation(0, "PositionInProjection");
AODepthTexture[vertical].Init(shader, "AODepthTexture");
BlurSharpness[vertical].Init(shader, "BlurSharpness");
InvFullResolution[vertical].Init(shader, "InvFullResolution");
PowExponent[vertical].Init(shader, "PowExponent");
Uniforms[vertical].Init(shader);
}
shader.Bind();
}
@ -137,17 +131,19 @@ void FSSAOCombineShader::Bind()
if (!mShader)
{
FString prolog = Uniforms.CreateDeclaration("Uniforms", UniformBlock::Desc());
if (multisample)
prolog += "#define MULTISAMPLE\n";
mShader.reset(new FShaderProgram());
mShader->Compile(FShaderProgram::Vertex, "shaders/glsl/screenquad.vp", "", 330);
mShader->Compile(FShaderProgram::Fragment, "shaders/glsl/ssaocombine.fp", multisample ? "#define MULTISAMPLE\n" : "", 330);
mShader->Compile(FShaderProgram::Fragment, "shaders/glsl/ssaocombine.fp", prolog, 330);
mShader->SetFragDataLocation(0, "FragColor");
mShader->Link("shaders/glsl/ssaocombine");
mShader->SetAttribLocation(0, "PositionInProjection");
AODepthTexture.Init(*mShader, "AODepthTexture");
SceneFogTexture.Init(*mShader, "SceneFogTexture");
SampleCount.Init(*mShader, "SampleCount");
Scale.Init(*mShader, "Scale");
Offset.Init(*mShader, "Offset");
Uniforms.Init(*mShader);
mMultisample = multisample;
}
mShader->Bind();

View file

@ -12,13 +12,37 @@ public:
FBufferedUniformSampler DepthTexture;
FBufferedUniformSampler ColorTexture;
FBufferedUniform1i SampleIndex;
FBufferedUniform1f LinearizeDepthA;
FBufferedUniform1f LinearizeDepthB;
FBufferedUniform1f InverseDepthRangeA;
FBufferedUniform1f InverseDepthRangeB;
FBufferedUniform2f Scale;
FBufferedUniform2f Offset;
struct UniformBlock
{
int SampleIndex;
float LinearizeDepthA;
float LinearizeDepthB;
float InverseDepthRangeA;
float InverseDepthRangeB;
float Padding0, Padding1, Padding2;
FVector2 Scale;
FVector2 Offset;
static std::vector<UniformFieldDesc> Desc()
{
return
{
{ "SampleIndex", UniformType::Int, offsetof(UniformBlock, SampleIndex) },
{ "LinearizeDepthA", UniformType::Float, offsetof(UniformBlock, LinearizeDepthA) },
{ "LinearizeDepthB", UniformType::Float, offsetof(UniformBlock, LinearizeDepthB) },
{ "InverseDepthRangeA", UniformType::Float, offsetof(UniformBlock, InverseDepthRangeA) },
{ "InverseDepthRangeB", UniformType::Float, offsetof(UniformBlock, InverseDepthRangeB) },
{ "Padding0", UniformType::Float, offsetof(UniformBlock, Padding0) },
{ "Padding1", UniformType::Float, offsetof(UniformBlock, Padding1) },
{ "Padding2", UniformType::Float, offsetof(UniformBlock, Padding2) },
{ "Scale", UniformType::Vec2, offsetof(UniformBlock, Scale) },
{ "Offset", UniformType::Vec2, offsetof(UniformBlock, Offset) }
};
}
};
ShaderUniforms<UniformBlock> Uniforms;
private:
std::unique_ptr<FShaderProgram> mShader;
@ -33,17 +57,44 @@ public:
FBufferedUniformSampler DepthTexture;
FBufferedUniformSampler NormalTexture;
FBufferedUniformSampler RandomTexture;
FBufferedUniform2f UVToViewA;
FBufferedUniform2f UVToViewB;
FBufferedUniform2f InvFullResolution;
FBufferedUniform1f NDotVBias;
FBufferedUniform1f NegInvR2;
FBufferedUniform1f RadiusToScreen;
FBufferedUniform1f AOMultiplier;
FBufferedUniform1f AOStrength;
FBufferedUniform2f Scale;
FBufferedUniform2f Offset;
FBufferedUniform1i SampleIndex;
struct UniformBlock
{
FVector2 UVToViewA;
FVector2 UVToViewB;
FVector2 InvFullResolution;
float NDotVBias;
float NegInvR2;
float RadiusToScreen;
float AOMultiplier;
float AOStrength;
int SampleIndex;
float Padding0, Padding1;
FVector2 Scale;
FVector2 Offset;
static std::vector<UniformFieldDesc> Desc()
{
return
{
{ "UVToViewA", UniformType::Vec2, offsetof(UniformBlock, UVToViewA) },
{ "UVToViewB", UniformType::Vec2, offsetof(UniformBlock, UVToViewB) },
{ "InvFullResolution", UniformType::Vec2, offsetof(UniformBlock, InvFullResolution) },
{ "NDotVBias", UniformType::Float, offsetof(UniformBlock, NDotVBias) },
{ "NegInvR2", UniformType::Float, offsetof(UniformBlock, NegInvR2) },
{ "RadiusToScreen", UniformType::Float, offsetof(UniformBlock, RadiusToScreen) },
{ "AOMultiplier", UniformType::Float, offsetof(UniformBlock, AOMultiplier) },
{ "AOStrength", UniformType::Float, offsetof(UniformBlock, AOStrength) },
{ "SampleIndex", UniformType::Int, offsetof(UniformBlock, SampleIndex) },
{ "Padding0", UniformType::Float, offsetof(UniformBlock, Padding0) },
{ "Padding1", UniformType::Float, offsetof(UniformBlock, Padding1) },
{ "Scale", UniformType::Vec2, offsetof(UniformBlock, Scale) },
{ "Offset", UniformType::Vec2, offsetof(UniformBlock, Offset) },
};
}
};
ShaderUniforms<UniformBlock> Uniforms;
private:
enum Quality
@ -68,9 +119,25 @@ public:
void Bind(bool vertical);
FBufferedUniformSampler AODepthTexture[2];
FBufferedUniform1f BlurSharpness[2];
FBufferedUniform2f InvFullResolution[2];
FBufferedUniform1f PowExponent[2];
struct UniformBlock
{
float BlurSharpness;
float PowExponent;
FVector2 InvFullResolution;
static std::vector<UniformFieldDesc> Desc()
{
return
{
{ "BlurSharpness", UniformType::Float, offsetof(UniformBlock, BlurSharpness) },
{ "PowExponent", UniformType::Float, offsetof(UniformBlock, PowExponent) },
{ "InvFullResolution", UniformType::Vec2, offsetof(UniformBlock, InvFullResolution) }
};
}
};
ShaderUniforms<UniformBlock> Uniforms[2];
private:
FShaderProgram mShader[2];
@ -83,9 +150,29 @@ public:
FBufferedUniformSampler AODepthTexture;
FBufferedUniformSampler SceneFogTexture;
FBufferedUniform1i SampleCount;
FBufferedUniform2f Scale;
FBufferedUniform2f Offset;
struct UniformBlock
{
int SampleCount;
int Padding0, Padding1, Padding2;
FVector2 Scale;
FVector2 Offset;
static std::vector<UniformFieldDesc> Desc()
{
return
{
{ "SampleCount", UniformType::Int, offsetof(UniformBlock, SampleCount) },
{ "Padding0", UniformType::Int, offsetof(UniformBlock, Padding0) },
{ "Padding1", UniformType::Int, offsetof(UniformBlock, Padding1) },
{ "Padding2", UniformType::Int, offsetof(UniformBlock, Padding2) },
{ "Scale", UniformType::Vec2, offsetof(UniformBlock, Scale) },
{ "Offset", UniformType::Vec2, offsetof(UniformBlock, Offset) }
};
}
};
ShaderUniforms<UniformBlock> Uniforms;
private:
std::unique_ptr<FShaderProgram> mShader;

View file

@ -407,7 +407,8 @@ public:
enum
{
LIGHTBUF_BINDINGPOINT = 1
LIGHTBUF_BINDINGPOINT = 1,
POSTPROCESS_BINDINGPOINT = 2
};
#endif

View file

@ -1,8 +1,121 @@
#ifndef __GL_SHADERPROGRAM_H
#define __GL_SHADERPROGRAM_H
#pragma once
#include "gl_shader.h"
enum class UniformType
{
Int,
UInt,
Float,
Vec2,
Vec3,
Vec4,
IVec2,
IVec3,
IVec4,
UVec2,
UVec3,
UVec4,
};
class UniformFieldDesc
{
public:
UniformFieldDesc() { }
UniformFieldDesc(const char *name, UniformType type, std::size_t offset) : Name(name), Type(type), Offset(offset) { }
const char *Name;
UniformType Type;
std::size_t Offset;
};
template<typename T>
class ShaderUniforms
{
public:
ShaderUniforms()
{
memset(&Values, 0, sizeof(Values));
}
~ShaderUniforms()
{
if (mBufferHandle)
glDeleteBuffers(1, &mBufferHandle);
}
FString CreateDeclaration(const char *name, const std::vector<UniformFieldDesc> &fields)
{
mName = name;
mFields = fields;
FString decl;
decl.Format("layout(std140) uniform %s\n{\n", name);
for (const auto &field : fields)
{
decl.AppendFormat("\t%s %s;\n", GetTypeStr(field.Type), field.Name);
}
decl += "};\n";
return decl;
}
void Init(GLuint hShader, int index = POSTPROCESS_BINDINGPOINT)
{
GLuint uniformBlockIndex = glGetUniformBlockIndex(hShader, mName);
if (uniformBlockIndex != GL_INVALID_INDEX)
{
glUniformBlockBinding(hShader, uniformBlockIndex, index);
glGenBuffers(1, (GLuint*)&mBufferHandle);
mIndex = index;
}
}
void Set()
{
glBindBuffer(GL_UNIFORM_BUFFER, mBufferHandle);
glBufferData(GL_UNIFORM_BUFFER, sizeof(T), &Values, GL_STREAM_DRAW);
glBindBufferBase(GL_UNIFORM_BUFFER, mIndex, mBufferHandle);
}
T *operator->() { return &Values; }
const T *operator->() const { return &Values; }
T Values;
private:
ShaderUniforms(const ShaderUniforms &) = delete;
ShaderUniforms &operator=(const ShaderUniforms &) = delete;
const char *GetTypeStr(UniformType type)
{
switch (type)
{
default:
case UniformType::Int: return "int";
case UniformType::UInt: return "uint";
case UniformType::Float: return "float";
case UniformType::Vec2: return "vec2";
case UniformType::Vec3: return "vec3";
case UniformType::Vec4: return "vec4";
case UniformType::IVec2: return "ivec2";
case UniformType::IVec3: return "ivec3";
case UniformType::IVec4: return "ivec4";
case UniformType::UVec2: return "uvec2";
case UniformType::UVec3: return "uvec3";
case UniformType::UVec4: return "uvec4";
}
}
const char *mName = nullptr;
int mIndex = -1;
GLuint mBufferHandle = 0;
std::vector<UniformFieldDesc> mFields;
std::vector<int> mUniformLocations;
};
class FShaderProgram
{
public:
@ -39,5 +152,3 @@ private:
GLuint mProgram = 0;
GLuint mShaders[NumShaderTypes];
};
#endif

View file

@ -3,9 +3,6 @@ in vec2 TexCoord;
out vec4 FragColor;
uniform sampler2D AODepthTexture;
uniform float BlurSharpness;
uniform vec2 InvFullResolution;
uniform float PowExponent;
#define KERNEL_RADIUS 3.0

View file

@ -5,19 +5,11 @@ out vec4 FragColor;
#if defined(MULTISAMPLE)
uniform sampler2DMS DepthTexture;
uniform sampler2DMS ColorTexture;
uniform int SampleIndex;
#else
uniform sampler2D DepthTexture;
uniform sampler2D ColorTexture;
#endif
uniform float LinearizeDepthA;
uniform float LinearizeDepthB;
uniform float InverseDepthRangeA;
uniform float InverseDepthRangeB;
uniform vec2 Scale;
uniform vec2 Offset;
float normalizeDepth(float depth)
{
float normalizedDepth = clamp(InverseDepthRangeA * depth + InverseDepthRangeB, 0.0, 1.0);

View file

@ -2,25 +2,10 @@
in vec2 TexCoord;
out vec4 FragColor;
uniform vec2 UVToViewA;
uniform vec2 UVToViewB;
uniform vec2 InvFullResolution;
uniform float NDotVBias;
uniform float NegInvR2;
uniform float RadiusToScreen;
uniform float AOMultiplier;
uniform float AOStrength;
uniform vec2 Scale;
uniform vec2 Offset;
uniform sampler2D DepthTexture;
#if defined(MULTISAMPLE)
uniform sampler2DMS NormalTexture;
uniform int SampleIndex;
#else
uniform sampler2D NormalTexture;
#endif

View file

@ -6,14 +6,10 @@ uniform sampler2D AODepthTexture;
#if defined(MULTISAMPLE)
uniform sampler2DMS SceneFogTexture;
uniform int SampleCount;
#else
uniform sampler2D SceneFogTexture;
#endif
uniform vec2 Scale;
uniform vec2 Offset;
void main()
{
vec2 uv = Offset + TexCoord * Scale;