From afbd45e1b113ac45f88c2fbf39e454ff11476fe2 Mon Sep 17 00:00:00 2001 From: Magnus Norddahl Date: Mon, 8 Apr 2019 05:27:35 +0200 Subject: [PATCH 1/2] - fix uniform aligment bug for vec3 --- .../hwrenderer/postprocessing/hw_postprocess.cpp | 16 ++++++++++++---- .../hwrenderer/postprocessing/hw_postprocess.h | 2 +- 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/src/rendering/hwrenderer/postprocessing/hw_postprocess.cpp b/src/rendering/hwrenderer/postprocessing/hw_postprocess.cpp index 0e7880806a..838c078e3a 100644 --- a/src/rendering/hwrenderer/postprocessing/hw_postprocess.cpp +++ b/src/rendering/hwrenderer/postprocessing/hw_postprocess.cpp @@ -858,7 +858,7 @@ PPCustomShaderInstance::PPCustomShaderInstance(PostProcessShader *desc) : Desc(d case PostProcessUniformType::Float: AddUniformField(offset, name, UniformType::Float, sizeof(float)); break; case PostProcessUniformType::Int: AddUniformField(offset, name, UniformType::Int, sizeof(int)); break; case PostProcessUniformType::Vec2: AddUniformField(offset, name, UniformType::Vec2, sizeof(float) * 2); break; - case PostProcessUniformType::Vec3: AddUniformField(offset, name, UniformType::Vec3, sizeof(float) * 3); break; + case PostProcessUniformType::Vec3: AddUniformField(offset, name, UniformType::Vec3, sizeof(float) * 3, sizeof(float) * 4); break; default: break; } } @@ -993,9 +993,9 @@ void PPCustomShaderInstance::SetUniforms(PPRenderState *renderstate) renderstate->Uniforms.Data = uniforms; } -void PPCustomShaderInstance::AddUniformField(size_t &offset, const FString &name, UniformType type, size_t fieldsize) +void PPCustomShaderInstance::AddUniformField(size_t &offset, const FString &name, UniformType type, size_t fieldsize, size_t alignment) { - size_t alignment = fieldsize; + if (alignment == 0) alignment = fieldsize; offset = (offset + alignment - 1) / alignment * alignment; FieldOffset[name] = offset; @@ -1004,6 +1004,14 @@ void PPCustomShaderInstance::AddUniformField(size_t &offset, const FString &name auto chars = name2->GetChars(); FieldNames.push_back(std::move(name2)); Fields.push_back({ chars, type, offset }); - offset += fieldsize; + + if (fieldsize != alignment) // Workaround for buggy OpenGL drivers that does not do std140 layout correctly for vec3 + { + name2 = std::make_unique(name + "__padding"); + chars = name2->GetChars(); + FieldNames.push_back(std::move(name2)); + Fields.push_back({ chars, UniformType::Float, offset }); + offset += alignment - fieldsize; + } } diff --git a/src/rendering/hwrenderer/postprocessing/hw_postprocess.h b/src/rendering/hwrenderer/postprocessing/hw_postprocess.h index aba625a7c0..222dbd249b 100644 --- a/src/rendering/hwrenderer/postprocessing/hw_postprocess.h +++ b/src/rendering/hwrenderer/postprocessing/hw_postprocess.h @@ -791,7 +791,7 @@ public: private: void CreateShaders(); - void AddUniformField(size_t &offset, const FString &name, UniformType type, size_t fieldsize); + void AddUniformField(size_t &offset, const FString &name, UniformType type, size_t fieldsize, size_t alignment = 0); void SetTextures(PPRenderState *renderstate); void SetUniforms(PPRenderState *renderstate); From b9900450c794ea55bb3f41725d398fa5e28b7270 Mon Sep 17 00:00:00 2001 From: Magnus Norddahl Date: Mon, 8 Apr 2019 05:31:20 +0200 Subject: [PATCH 2/2] - remove unused function declaration --- src/rendering/hwrenderer/postprocessing/hw_postprocess.h | 1 - 1 file changed, 1 deletion(-) diff --git a/src/rendering/hwrenderer/postprocessing/hw_postprocess.h b/src/rendering/hwrenderer/postprocessing/hw_postprocess.h index 222dbd249b..c867d4ba32 100644 --- a/src/rendering/hwrenderer/postprocessing/hw_postprocess.h +++ b/src/rendering/hwrenderer/postprocessing/hw_postprocess.h @@ -790,7 +790,6 @@ public: PostProcessShader *Desc = nullptr; private: - void CreateShaders(); void AddUniformField(size_t &offset, const FString &name, UniformType type, size_t fieldsize, size_t alignment = 0); void SetTextures(PPRenderState *renderstate); void SetUniforms(PPRenderState *renderstate);