From 54519ed70fbe154e196d04a8b62f6ea6aeded52a Mon Sep 17 00:00:00 2001 From: Tim Foley Date: Fri, 7 Jul 2017 08:16:09 -0700 Subject: Fix alignment computation for `std140` uniforms Fixes #55 I was incorrectly computing alignment as `elementSize * elementAlignment`, rounded up to a power of two (which works out to be `elementSize` squared), when I should have been using `elementSize * elementCount`, rounded up to a power of two. --- source/slang/type-layout.cpp | 5 ++-- tests/reflection/gh-55.glsl | 13 +++++++++++ tests/reflection/gh-55.glsl.expected | 45 ++++++++++++++++++++++++++++++++++++ 3 files changed, 61 insertions(+), 2 deletions(-) create mode 100644 tests/reflection/gh-55.glsl create mode 100644 tests/reflection/gh-55.glsl.expected diff --git a/source/slang/type-layout.cpp b/source/slang/type-layout.cpp index 2fcf64226..fbaec3736 100644 --- a/source/slang/type-layout.cpp +++ b/source/slang/type-layout.cpp @@ -163,10 +163,11 @@ struct Std140LayoutRulesImpl : GLSLConstantBufferLayoutRulesImpl SimpleLayoutInfo GetVectorLayout(SimpleLayoutInfo elementInfo, size_t elementCount) override { assert(elementInfo.kind == LayoutResourceKind::Uniform); + auto size = elementInfo.size * elementCount; SimpleLayoutInfo vectorInfo( LayoutResourceKind::Uniform, - elementInfo.size * elementCount, - RoundUpToPowerOfTwo(elementInfo.size * elementInfo.alignment)); + size, + RoundUpToPowerOfTwo(size)); return vectorInfo; } }; diff --git a/tests/reflection/gh-55.glsl b/tests/reflection/gh-55.glsl new file mode 100644 index 000000000..4ced68c9e --- /dev/null +++ b/tests/reflection/gh-55.glsl @@ -0,0 +1,13 @@ +//TEST(smoke):SIMPLE:-profile ps_4_0 -target reflection-json + +// Confirm fix for GitHub issue #55 + +layout(set = 0, binding = 0) +uniform PerFrameCB +{ + vec2 offset; + vec2 scale; +}; + +void main() +{} diff --git a/tests/reflection/gh-55.glsl.expected b/tests/reflection/gh-55.glsl.expected new file mode 100644 index 000000000..8cdebdcd8 --- /dev/null +++ b/tests/reflection/gh-55.glsl.expected @@ -0,0 +1,45 @@ +result code = 0 +standard error = { +} +standard output = { +{ + "parameters": [ + { + "name": "PerFrameCB", + "binding": {"kind": "descriptorTableSlot", "index": 0}, + "type": { + "kind": "constantBuffer", + "elementType": { + "kind": "struct", + "fields": [ + { + "name": "offset", + "type": { + "kind": "vector", + "elementCount": 2, + "elementType": { + "kind": "scalar", + "scalarType": "float32" + } + }, + "binding": {"kind": "uniform", "offset": 0, "size": 8} + }, + { + "name": "scale", + "type": { + "kind": "vector", + "elementCount": 2, + "elementType": { + "kind": "scalar", + "scalarType": "float32" + } + }, + "binding": {"kind": "uniform", "offset": 8, "size": 8} + } + ] + } + } + } + ] +} +} -- cgit v1.2.3