summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--slang.h6
-rw-r--r--source/slang/reflection.cpp43
-rw-r--r--source/slang/type-layout.cpp41
-rw-r--r--tests/reflection/std430-layout.glsl49
-rw-r--r--tests/reflection/std430-layout.glsl.expected107
5 files changed, 234 insertions, 12 deletions
diff --git a/slang.h b/slang.h
index 926ef531d..b6f35f255 100644
--- a/slang.h
+++ b/slang.h
@@ -358,7 +358,9 @@ extern "C"
SLANG_TYPE_KIND_CONSTANT_BUFFER,
SLANG_TYPE_KIND_RESOURCE,
SLANG_TYPE_KIND_SAMPLER_STATE,
-
+ SLANG_TYPE_KIND_TEXTURE_BUFFER,
+ SLANG_TYPE_KIND_SHADER_STORAGE_BUFFER,
+
SLANG_TYPE_KIND_COUNT,
};
@@ -547,6 +549,8 @@ namespace slang
ConstantBuffer = SLANG_TYPE_KIND_CONSTANT_BUFFER,
Resource = SLANG_TYPE_KIND_RESOURCE,
SamplerState = SLANG_TYPE_KIND_SAMPLER_STATE,
+ TextureBuffer = SLANG_TYPE_KIND_TEXTURE_BUFFER,
+ ShaderStorageBuffer = SLANG_TYPE_KIND_SHADER_STORAGE_BUFFER,
};
enum ScalarType : SlangScalarType
diff --git a/source/slang/reflection.cpp b/source/slang/reflection.cpp
index b27cc0696..d62da70c1 100644
--- a/source/slang/reflection.cpp
+++ b/source/slang/reflection.cpp
@@ -100,6 +100,14 @@ SLANG_API SlangTypeKind spReflectionType_GetKind(SlangReflectionType* inType)
{
return SLANG_TYPE_KIND_CONSTANT_BUFFER;
}
+ else if (type->As<TextureBufferType>())
+ {
+ return SLANG_TYPE_KIND_TEXTURE_BUFFER;
+ }
+ else if (type->As<GLSLShaderStorageBufferType>())
+ {
+ return SLANG_TYPE_KIND_SHADER_STORAGE_BUFFER;
+ }
else if (auto samplerStateType = type->As<SamplerStateType>())
{
return SLANG_TYPE_KIND_SAMPLER_STATE;
@@ -1049,6 +1057,24 @@ static void emitReflectionTypeInfoJSON(
type->getElementType());
break;
+ case SLANG_TYPE_KIND_TEXTURE_BUFFER:
+ write(writer, "\"kind\": \"textureBuffer\"");
+ write(writer, ",\n");
+ write(writer, "\"elementType\": ");
+ emitReflectionTypeJSON(
+ writer,
+ type->getElementType());
+ break;
+
+ case SLANG_TYPE_KIND_SHADER_STORAGE_BUFFER:
+ write(writer, "\"kind\": \"shaderStorageBuffer\"");
+ write(writer, ",\n");
+ write(writer, "\"elementType\": ");
+ emitReflectionTypeJSON(
+ writer,
+ type->getElementType());
+ break;
+
case SLANG_TYPE_KIND_SCALAR:
write(writer, "\"kind\": \"scalar\"");
write(writer, ",\n");
@@ -1184,6 +1210,23 @@ static void emitReflectionTypeLayoutInfoJSON(
typeLayout->getElementTypeLayout());
break;
+ case SLANG_TYPE_KIND_TEXTURE_BUFFER:
+ write(writer, "\"kind\": \"textureBuffer\"");
+ write(writer, ",\n");
+ write(writer, "\"elementType\": ");
+ emitReflectionTypeLayoutJSON(
+ writer,
+ typeLayout->getElementTypeLayout());
+ break;
+
+ case SLANG_TYPE_KIND_SHADER_STORAGE_BUFFER:
+ write(writer, "\"kind\": \"shaderStorageBuffer\"");
+ write(writer, ",\n");
+ write(writer, "\"elementType\": ");
+ emitReflectionTypeLayoutJSON(
+ writer,
+ typeLayout->getElementTypeLayout());
+ break;
}
// TODO: emit size info for types
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
diff --git a/tests/reflection/std430-layout.glsl b/tests/reflection/std430-layout.glsl
new file mode 100644
index 000000000..5d4dee88f
--- /dev/null
+++ b/tests/reflection/std430-layout.glsl
@@ -0,0 +1,49 @@
+//TEST(smoke):SIMPLE:-profile ps_4_0 -target reflection-json
+
+// Confirm fix for GitHub issue #55
+
+struct Foo
+{
+ vec2 f;
+};
+
+layout(set = 0, binding = 0)
+buffer SomeBuffer
+{
+ // offset: 0, size: 4, alignment: 4
+ float a;
+
+ // offset: 16, size: 12, alignment: 16
+ //
+ // Note that it can't immediately follow `a`
+ // bcause of its alignment
+ vec3 b;
+
+ // offset: 28, size: 16, alignment: 4, stride: 4
+ //
+ // This array can be densely packed and 4-byte aligned under `std430` rules
+ float c[4];
+
+ // offset: 48, size: 8, alignment: 8
+ //
+ // This nees to be bumped up to a 8-byte aligned boundary
+ vec2 d;
+
+ // offset: 56, size: 8, alignment: 8
+ //
+ // This can come right after `d`, because `struct` types no longer
+ // get an artificial padding out to 16-byte alignment
+ Foo e;
+
+ // offset: 64, size: 12: alignment: 16
+ vec3 g;
+
+ // offset: 76, size: 4, alignment: 4
+ //
+ // This can fit in the empty space after `g` because we allow
+ // the size of a `vec3` to be smaller than its alignment
+ float h;
+};
+
+void main()
+{}
diff --git a/tests/reflection/std430-layout.glsl.expected b/tests/reflection/std430-layout.glsl.expected
new file mode 100644
index 000000000..701a36909
--- /dev/null
+++ b/tests/reflection/std430-layout.glsl.expected
@@ -0,0 +1,107 @@
+result code = 0
+standard error = {
+}
+standard output = {
+{
+ "parameters": [
+ {
+ "name": "SomeBuffer",
+ "binding": {"kind": "descriptorTableSlot", "index": 0},
+ "type": {
+ "kind": "shaderStorageBuffer",
+ "elementType": {
+ "kind": "struct",
+ "fields": [
+ {
+ "name": "a",
+ "type": {
+ "kind": "scalar",
+ "scalarType": "float32"
+ },
+ "binding": {"kind": "uniform", "offset": 0, "size": 4}
+ },
+ {
+ "name": "b",
+ "type": {
+ "kind": "vector",
+ "elementCount": 3,
+ "elementType": {
+ "kind": "scalar",
+ "scalarType": "float32"
+ }
+ },
+ "binding": {"kind": "uniform", "offset": 16, "size": 12}
+ },
+ {
+ "name": "c",
+ "type": {
+ "kind": "array",
+ "elementCount": 4,
+ "elementType": {
+ "kind": "scalar",
+ "scalarType": "float32"
+ },
+ "uniformStride": 4
+ },
+ "binding": {"kind": "uniform", "offset": 28, "size": 16}
+ },
+ {
+ "name": "d",
+ "type": {
+ "kind": "vector",
+ "elementCount": 2,
+ "elementType": {
+ "kind": "scalar",
+ "scalarType": "float32"
+ }
+ },
+ "binding": {"kind": "uniform", "offset": 48, "size": 8}
+ },
+ {
+ "name": "e",
+ "type": {
+ "kind": "struct",
+ "fields": [
+ {
+ "name": "f",
+ "type": {
+ "kind": "vector",
+ "elementCount": 2,
+ "elementType": {
+ "kind": "scalar",
+ "scalarType": "float32"
+ }
+ },
+ "binding": {"kind": "uniform", "offset": 0, "size": 8}
+ }
+ ]
+ },
+ "binding": {"kind": "uniform", "offset": 56, "size": 8}
+ },
+ {
+ "name": "g",
+ "type": {
+ "kind": "vector",
+ "elementCount": 3,
+ "elementType": {
+ "kind": "scalar",
+ "scalarType": "float32"
+ }
+ },
+ "binding": {"kind": "uniform", "offset": 64, "size": 12}
+ },
+ {
+ "name": "h",
+ "type": {
+ "kind": "scalar",
+ "scalarType": "float32"
+ },
+ "binding": {"kind": "uniform", "offset": 76, "size": 4}
+ }
+ ]
+ }
+ }
+ }
+ ]
+}
+}