summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--source/slang/reflection.cpp28
-rw-r--r--source/slang/type-layout.cpp22
-rw-r--r--tests/bindings/gh-84.frag35
3 files changed, 76 insertions, 9 deletions
diff --git a/source/slang/reflection.cpp b/source/slang/reflection.cpp
index 1beacc21b..3c89194c8 100644
--- a/source/slang/reflection.cpp
+++ b/source/slang/reflection.cpp
@@ -476,16 +476,28 @@ SLANG_API size_t spReflectionTypeLayout_GetElementStride(SlangReflectionTypeLayo
if( auto arrayTypeLayout = dynamic_cast<ArrayTypeLayout*>(typeLayout))
{
- if(category == SLANG_PARAMETER_CATEGORY_UNIFORM)
+ switch (category)
{
+ // We store the stride explictly for the uniform case
+ case SLANG_PARAMETER_CATEGORY_UNIFORM:
return arrayTypeLayout->uniformStride;
- }
- else
- {
- auto elementTypeLayout = arrayTypeLayout->elementTypeLayout;
- auto info = elementTypeLayout->FindResourceInfo(LayoutResourceKind(category));
- if(!info) return 0;
- return info->count;
+
+ // For most other cases (resource registers), the "stride"
+ // of an array is simply the number of resources (if any)
+ // consumed by its element type.
+ default:
+ {
+ auto elementTypeLayout = arrayTypeLayout->elementTypeLayout;
+ auto info = elementTypeLayout->FindResourceInfo(LayoutResourceKind(category));
+ if(!info) return 0;
+ return info->count;
+ }
+
+ // An import special case, though, is Vulkan descriptor-table slots,
+ // where an entire array will use a single `binding`, so that the
+ // effective stride is zero:
+ case SLANG_PARAMETER_CATEGORY_DESCRIPTOR_TABLE_SLOT:
+ return 0;
}
}
diff --git a/source/slang/type-layout.cpp b/source/slang/type-layout.cpp
index 9bc001e88..2977ee9b8 100644
--- a/source/slang/type-layout.cpp
+++ b/source/slang/type-layout.cpp
@@ -1053,10 +1053,30 @@ SimpleLayoutInfo GetLayoutImpl(
// The uniform case was already handled above
if( elementResourceInfo.kind == LayoutResourceKind::Uniform )
continue;
+
+ // In almost all cases, the resources consumed by an array
+ // will be its element count times the resources consumed
+ // by its element type. The one exception to this is
+ // arrays of resources in Vulkan GLSL, where an entire array
+ // only consumes a single descriptor-table slot.
+ //
+ // Note: We extend this logic to arbitrary arrays-of-structs,
+ // under the assumption that downstream legalization will
+ // turn those into scalarized structs-of-arrays and this
+ // logic will work out.
+ UInt arrayResourceCount = 0;
+ if (elementResourceInfo.kind == LayoutResourceKind::DescriptorTableSlot)
+ {
+ arrayResourceCount = elementResourceInfo.count;
+ }
+ else
+ {
+ arrayResourceCount = elementResourceInfo.count * elementCount;
+ }
typeLayout->addResourceUsage(
elementResourceInfo.kind,
- elementResourceInfo.count * elementCount);
+ arrayResourceCount);
}
}
return arrayUniformInfo;
diff --git a/tests/bindings/gh-84.frag b/tests/bindings/gh-84.frag
new file mode 100644
index 000000000..b5cc1875e
--- /dev/null
+++ b/tests/bindings/gh-84.frag
@@ -0,0 +1,35 @@
+#version 450
+//TEST:COMPARE_GLSL:
+
+// Confirm implementation of GitHub issue #84
+
+#if defined(__SLANG__)
+#define LAYOUT(X) /* empty */
+#else
+#define LAYOUT(X) layout(X)
+#endif
+
+// Array of resources: should only use up one binding
+LAYOUT(binding = 0)
+uniform texture2D t[8];
+
+// This should automatically get binding 1
+LAYOUT(binding = 1)
+uniform sampler s;
+
+LAYOUT(binding = 2)
+uniform U
+{
+ int i;
+};
+
+LAYOUT(location = 0)
+in vec2 uv;
+
+LAYOUT(location = 0)
+out vec4 color;
+
+void main()
+{
+ color = texture(sampler2D(t[i], s), uv);
+}