From 8fcf58a732c136e0971d84d52bff53dd78325254 Mon Sep 17 00:00:00 2001 From: BZLZHH Date: Sun, 15 Jun 2025 09:37:21 +0800 Subject: [PATCH] [Fix] (Diligent/PSO): Correct std140 alignment in uniform offset recording. --- .../Implementations/GL/Program/GL_Program.cpp | 46 ++++++++++++++++--- 1 file changed, 40 insertions(+), 6 deletions(-) diff --git a/MG/MG_GL/Implementations/GL/Program/GL_Program.cpp b/MG/MG_GL/Implementations/GL/Program/GL_Program.cpp index 6aab7dbf..3ed7db7e 100644 --- a/MG/MG_GL/Implementations/GL/Program/GL_Program.cpp +++ b/MG/MG_GL/Implementations/GL/Program/GL_Program.cpp @@ -81,6 +81,41 @@ namespace MG_GL::GL { } } + size_t GetStd140Alignment(GLenum type) { + switch (type) { + case GL_FLOAT: + case GL_INT: + case GL_BOOL: + case GL_UNSIGNED_INT: + return size_t(4); + case GL_FLOAT_VEC2: + case GL_INT_VEC2: + case GL_BOOL_VEC2: + case GL_UNSIGNED_INT_VEC2: + return size_t(8); + case GL_FLOAT_VEC3: + case GL_FLOAT_VEC4: + case GL_INT_VEC3: + case GL_INT_VEC4: + case GL_BOOL_VEC3: + case GL_BOOL_VEC4: + case GL_UNSIGNED_INT_VEC3: + case GL_UNSIGNED_INT_VEC4: + case GL_FLOAT_MAT2: + case GL_FLOAT_MAT3: + case GL_FLOAT_MAT4: + case GL_FLOAT_MAT2x3: + case GL_FLOAT_MAT2x4: + case GL_FLOAT_MAT3x2: + case GL_FLOAT_MAT3x4: + case GL_FLOAT_MAT4x2: + case GL_FLOAT_MAT4x3: + return size_t(16); + default: + return size_t(16); + } + } + void RecordUniformOffsets(MG_Diligent::GLProgramInfo& programInfo) { MG_Util::Debug::LogD("RecordUniformOffsets for program"); size_t offset = 0; @@ -89,14 +124,13 @@ namespace MG_GL::GL { for (auto& name: programInfo.uniformBufferNames) { auto& uniform = programInfo.programObj.uniformValues[name]; if (IsSamplerType(uniform.type)) continue; - size_t size = GetUniformSize(uniform.type); - size_t alignedSize = AlignSize(size, 16); - + size_t alignment = GetStd140Alignment(uniform.type); + offset = AlignSize(offset, alignment); programInfo.uniformOffsets[name] = offset; - MG_Util::Debug::LogD(" Uniform '%s': offset = %zu, size = %zu, alignedSize = %zu", - name.c_str(), offset, size, alignedSize); - offset += alignedSize; + MG_Util::Debug::LogD(" Uniform '%s': offset = %zu, size = %zu, alignment = %zu", name.c_str(), offset, size, alignment); + size_t paddedSize = AlignSize(size, alignment); + offset += paddedSize; } }