From d22fb24e28319cbfbba6a7654bd5eb689d849d58 Mon Sep 17 00:00:00 2001 From: Magnus Norddahl Date: Mon, 11 Jun 2018 20:58:20 +0200 Subject: [PATCH] - Uniform buffers for lens shader --- src/gl/renderer/gl_postprocess.cpp | 9 ++++--- src/gl/shaders/gl_lensshader.cpp | 9 +++---- src/gl/shaders/gl_lensshader.h | 28 +++++++++++++++++--- wadsrc/static/shaders/glsl/lensdistortion.fp | 4 --- 4 files changed, 33 insertions(+), 17 deletions(-) diff --git a/src/gl/renderer/gl_postprocess.cpp b/src/gl/renderer/gl_postprocess.cpp index ce7e1ac61..a57b80b59 100644 --- a/src/gl/renderer/gl_postprocess.cpp +++ b/src/gl/renderer/gl_postprocess.cpp @@ -582,10 +582,11 @@ void FGLRenderer::LensDistortScene() mBuffers->BindCurrentTexture(0, GL_LINEAR); mLensShader->Bind(); mLensShader->InputTexture.Set(0); - mLensShader->AspectRatio.Set(aspect); - mLensShader->Scale.Set(scale); - mLensShader->LensDistortionCoefficient.Set(k); - mLensShader->CubicDistortionValue.Set(kcube); + mLensShader->Uniforms->AspectRatio = aspect; + mLensShader->Uniforms->Scale = scale; + mLensShader->Uniforms->LensDistortionCoefficient = k; + mLensShader->Uniforms->CubicDistortionValue = kcube; + mLensShader->Uniforms.Set(); RenderScreenQuad(); mBuffers->NextTexture(); diff --git a/src/gl/shaders/gl_lensshader.cpp b/src/gl/shaders/gl_lensshader.cpp index 96ec217a3..13945e8eb 100644 --- a/src/gl/shaders/gl_lensshader.cpp +++ b/src/gl/shaders/gl_lensshader.cpp @@ -33,16 +33,15 @@ void FLensShader::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/lensdistortion.fp", "", 330); + mShader.Compile(FShaderProgram::Fragment, "shaders/glsl/lensdistortion.fp", prolog, 330); mShader.SetFragDataLocation(0, "FragColor"); mShader.Link("shaders/glsl/lensdistortion"); mShader.SetAttribLocation(0, "PositionInProjection"); InputTexture.Init(mShader, "InputTexture"); - AspectRatio.Init(mShader, "Aspect"); - Scale.Init(mShader, "Scale"); - LensDistortionCoefficient.Init(mShader, "k"); - CubicDistortionValue.Init(mShader, "kcube"); + Uniforms.Init(mShader); } mShader.Bind(); } diff --git a/src/gl/shaders/gl_lensshader.h b/src/gl/shaders/gl_lensshader.h index 7e9ef6392..ace6054cb 100644 --- a/src/gl/shaders/gl_lensshader.h +++ b/src/gl/shaders/gl_lensshader.h @@ -9,10 +9,30 @@ public: void Bind(); FBufferedUniformSampler InputTexture; - FBufferedUniform1f AspectRatio; - FBufferedUniform1f Scale; - FBufferedUniform4f LensDistortionCoefficient; - FBufferedUniform4f CubicDistortionValue; + + struct UniformBlock + { + float AspectRatio; + float Scale; + float Padding0, Padding1; + FVector4 LensDistortionCoefficient; + FVector4 CubicDistortionValue; + + static std::vector Desc() + { + return + { + { "Aspect", UniformType::Float, offsetof(UniformBlock, AspectRatio) }, + { "Scale", UniformType::Float, offsetof(UniformBlock, Scale) }, + { "Padding0", UniformType::Float, offsetof(UniformBlock, Padding0) }, + { "Padding1", UniformType::Float, offsetof(UniformBlock, Padding1) }, + { "k", UniformType::Vec4, offsetof(UniformBlock, LensDistortionCoefficient) }, + { "kcube", UniformType::Vec4, offsetof(UniformBlock, CubicDistortionValue) } + }; + } + }; + + ShaderUniforms Uniforms; private: FShaderProgram mShader; diff --git a/wadsrc/static/shaders/glsl/lensdistortion.fp b/wadsrc/static/shaders/glsl/lensdistortion.fp index 7facf5a80..d3cc5c7ca 100644 --- a/wadsrc/static/shaders/glsl/lensdistortion.fp +++ b/wadsrc/static/shaders/glsl/lensdistortion.fp @@ -33,10 +33,6 @@ in vec2 TexCoord; out vec4 FragColor; uniform sampler2D InputTexture; -uniform float Aspect; // image width/height -uniform float Scale; // 1/max(f) -uniform vec4 k; // lens distortion coefficient -uniform vec4 kcube; // cubic distortion value void main() {