From e5cc4660c634a0dd35a9813e03192d380f253332 Mon Sep 17 00:00:00 2001 From: Tim Foley Date: Thu, 29 Nov 2018 07:48:38 -0800 Subject: Fix uses of dynamic_cast on types in reflection API (#731) The `Type` infrastructure uses a class hierarchy, but blindly `dynamic_cast`ing to a desired case doesn't always give the expected result, because a `Type` could represent a `typedef` (a `NamedExpressionType`) that itself resolves to, e.g, a vector type (a `VectorExpressionType`). In that case a `dynamic_cast(someType)` would fail, even though the type logically represents a vector. The `Type::As()` method is designed to handle this case, by "looking through" simple `typedef`s to get at the real definition of a type. The fix in this case is to use `Type::As()` at various points in the reflection code (`reflection.cpp`) instead of `dynamic_cast`. This problem surfaced with a `StructuredBuffer` not reflecting correctly, because the element type (`float2`) is actually a `typedef` (for `vector`), so I've included a test case that stresses that case. Getting the right output in the test required tweaking the `slang-reflection-test` tool to produce additional output for resource types (currently narrowed down to only affect structured buffers to avoid large diffs in expected test outputs). --- tests/reflection/structured-buffer.slang | 19 ++++++ tests/reflection/structured-buffer.slang.expected | 75 +++++++++++++++++++++++ tests/reflection/thread-group-size.hlsl.expected | 6 +- tests/reflection/unbounded-arrays.hlsl.1.expected | 10 ++- 4 files changed, 108 insertions(+), 2 deletions(-) create mode 100644 tests/reflection/structured-buffer.slang create mode 100644 tests/reflection/structured-buffer.slang.expected (limited to 'tests/reflection') diff --git a/tests/reflection/structured-buffer.slang b/tests/reflection/structured-buffer.slang new file mode 100644 index 000000000..491d61486 --- /dev/null +++ b/tests/reflection/structured-buffer.slang @@ -0,0 +1,19 @@ +//TEST:REFLECTION:-profile ps_4_0 -target hlsl + +// Confirm that we reflect the contents of structure-buffer types correctly. + +struct S +{ + float2 a; + float b; + uint c; +}; + +StructuredBuffer x; +StructuredBuffer y; +StructuredBuffer z; + +float4 main() : SV_Target +{ + return x[0] + y[0].xyxy + z[0].a.xyxy; +} \ No newline at end of file diff --git a/tests/reflection/structured-buffer.slang.expected b/tests/reflection/structured-buffer.slang.expected new file mode 100644 index 000000000..70ceb64f2 --- /dev/null +++ b/tests/reflection/structured-buffer.slang.expected @@ -0,0 +1,75 @@ +result code = 0 +standard error = { +} +standard output = { +{ + "parameters": [ + { + "name": "x", + "binding": {"kind": "shaderResource", "index": 0}, + "type": { + "kind": "resource", + "baseShape": "structuredBuffer", + "resultType": { + "kind": "scalar", + "scalarType": "uint32" + } + } + }, + { + "name": "y", + "binding": {"kind": "shaderResource", "index": 1}, + "type": { + "kind": "resource", + "baseShape": "structuredBuffer", + "resultType": { + "kind": "vector", + "elementCount": 2, + "elementType": { + "kind": "scalar", + "scalarType": "float32" + } + } + } + }, + { + "name": "z", + "binding": {"kind": "shaderResource", "index": 2}, + "type": { + "kind": "resource", + "baseShape": "structuredBuffer", + "resultType": { + "kind": "struct", + "fields": [ + "name": "a", + "type": { + "kind": "vector", + "elementCount": 2, + "elementType": { + "kind": "scalar", + "scalarType": "float32" + } + }, + "name": "b", + "type": { + "kind": "scalar", + "scalarType": "float32" + }, + "name": "c", + "type": { + "kind": "scalar", + "scalarType": "uint32" + } + ] + } + } + } + ], + "entryPoints": [ + { + "name": "main", + "stage:": "fragment" + } + ] +} +} diff --git a/tests/reflection/thread-group-size.hlsl.expected b/tests/reflection/thread-group-size.hlsl.expected index cd5d09e35..46b0eb87e 100644 --- a/tests/reflection/thread-group-size.hlsl.expected +++ b/tests/reflection/thread-group-size.hlsl.expected @@ -10,7 +10,11 @@ standard output = { "type": { "kind": "resource", "baseShape": "structuredBuffer", - "access": "readWrite" + "access": "readWrite", + "resultType": { + "kind": "scalar", + "scalarType": "float32" + } } } ], diff --git a/tests/reflection/unbounded-arrays.hlsl.1.expected b/tests/reflection/unbounded-arrays.hlsl.1.expected index 382fc25bc..60d894ba7 100644 --- a/tests/reflection/unbounded-arrays.hlsl.1.expected +++ b/tests/reflection/unbounded-arrays.hlsl.1.expected @@ -98,7 +98,15 @@ standard output = { "type": { "kind": "resource", "baseShape": "structuredBuffer", - "access": "readWrite" + "access": "readWrite", + "resultType": { + "kind": "vector", + "elementCount": 4, + "elementType": { + "kind": "scalar", + "scalarType": "float32" + } + } } } ], -- cgit v1.2.3