From d35b4d4bc62beebaa56b949a1ac59470192b5dfc Mon Sep 17 00:00:00 2001 From: Tim Foley Date: Fri, 7 Jul 2017 08:48:45 -0700 Subject: Fix support for `std430` layout Fixes #57 There were a bunch of issues in how `std430` was being implemented, largely due to just stubbing it in without any test cases. This commit adds a reasonably good test case to ensure that we've got things basically working. --- source/slang/type-layout.cpp | 41 ++++++++++++++++++++++++++++++----------- 1 file changed, 30 insertions(+), 11 deletions(-) (limited to 'source') diff --git a/source/slang/type-layout.cpp b/source/slang/type-layout.cpp index fbaec3736..10c7327ce 100644 --- a/source/slang/type-layout.cpp +++ b/source/slang/type-layout.cpp @@ -155,20 +155,32 @@ struct GLSLConstantBufferLayoutRulesImpl : DefaultConstantBufferLayoutRulesImpl { }; +// The `std140` and `std430` rules require vectors to be aligned to the next power of +// two up from their size (so a `float2` is 8-byte aligned, and a `float3` is +// 16-byte aligned). +// +// Note that in this case we have a type layout where the size is *not* a multiple +// of the alignment, so it should be possible to pack a scalar after a `float3`. +static SimpleLayoutInfo getGLSLVectorLayout( + SimpleLayoutInfo elementInfo, size_t elementCount) +{ + assert(elementInfo.kind == LayoutResourceKind::Uniform); + auto size = elementInfo.size * elementCount; + SimpleLayoutInfo vectorInfo( + LayoutResourceKind::Uniform, + size, + RoundUpToPowerOfTwo(size)); + return vectorInfo; +} + +// The `std140` rules combine the GLSL-specific layout for 3-vectors with the +// alignment padding for structures and arrays that is common to both HLSL +// and GLSL constant buffers. struct Std140LayoutRulesImpl : GLSLConstantBufferLayoutRulesImpl { - // The `std140` rules require vectors to be aligned to the next power of two - // up from their size (so a `float2` is 8-byte aligned, and a `float3` is - // 16-byte aligned). SimpleLayoutInfo GetVectorLayout(SimpleLayoutInfo elementInfo, size_t elementCount) override { - assert(elementInfo.kind == LayoutResourceKind::Uniform); - auto size = elementInfo.size * elementCount; - SimpleLayoutInfo vectorInfo( - LayoutResourceKind::Uniform, - size, - RoundUpToPowerOfTwo(size)); - return vectorInfo; + return getGLSLVectorLayout(elementInfo, elementCount); } }; @@ -207,8 +219,15 @@ struct HLSLStructuredBufferLayoutRulesImpl : DefaultLayoutRulesImpl // TODO: customize these to be correct... }; -struct Std430LayoutRulesImpl : GLSLConstantBufferLayoutRulesImpl +// The `std430` rules don't include the array/structure alignment padding that +// gets applied to constant buffers, but they do include the padding of 3-vectors +// to be aligned as 4-vectors. +struct Std430LayoutRulesImpl : DefaultLayoutRulesImpl { + SimpleLayoutInfo GetVectorLayout(SimpleLayoutInfo elementInfo, size_t elementCount) override + { + return getGLSLVectorLayout(elementInfo, elementCount); + } }; struct DefaultVaryingLayoutRulesImpl : DefaultLayoutRulesImpl -- cgit v1.2.3