summaryrefslogtreecommitdiffstats
path: root/tests
diff options
context:
space:
mode:
authorTim Foley <tfoleyNV@users.noreply.github.com>2019-08-06 12:14:52 -0700
committerGitHub <noreply@github.com>2019-08-06 12:14:52 -0700
commit81ce78d08a7e3fbe74f2fd41c5a258ea4b078245 (patch)
tree06e0c3cae361ee85cbee513ee539d9f9bde647f9 /tests
parent89758544c45a15e546a1eb08891e2787bb88de4a (diff)
Add support for the HLSL "cast from zero" idiom (#1008)
If the user writes code like this: MyStruct s = (MyStruct) 0; then we will interpret it as if they had written: MyStruct s = {}; That is, the "cast from zero" idiom will be taken as a legacy syntax for default construction (using an empty initializer list). This will be semantically equivalent to zero-initialization for all existing HLSL code (where `struct` fields can't have default initialization expressions defined), and is the easiest option for us to support in Slang (since we already support default-initialization using empty initializer lists). The implementation of this feature is narrowly scoped: * It only targets explicit cast expressions like `(MyStruct) 0` and not "constructor" syntax like `MyStruct(0)` * It only applies when there is a single argument that is exactly an integer literal with a zero value (not a reference to a `static const int` that happens to be zero). This change adds a test case to make sure that the feature works as expected. Because it relies on our existing initializer-list handling, the "cast from zero" idiom should work for any user-defined type where an initializer list would work.
Diffstat (limited to 'tests')
-rw-r--r--tests/compute/cast-zero-to-struct.slang36
-rw-r--r--tests/compute/cast-zero-to-struct.slang.expected.txt4
2 files changed, 40 insertions, 0 deletions
diff --git a/tests/compute/cast-zero-to-struct.slang b/tests/compute/cast-zero-to-struct.slang
new file mode 100644
index 000000000..c2ed8fb6d
--- /dev/null
+++ b/tests/compute/cast-zero-to-struct.slang
@@ -0,0 +1,36 @@
+// cast-zero-to-struct.slang
+
+// Test that HLSL legacy syntax for casting from literal zero
+// to a `struct` type works.
+
+//TEST(compute):COMPARE_COMPUTE:
+
+struct S
+{
+ int2 a;
+ int2 b;
+ int2 c;
+}
+
+int test(int val)
+{
+ S s = (S) 0;
+
+ s.a.x = val;
+ s.b.y = val;
+ s.c.x = val;
+
+ int2 t = s.a + s.b*256 + s.c*65536;
+ return t.x + t.y*16;
+}
+
+//TEST_INPUT: ubuffer(data=[0 0 0 0], stride=4):dxbinding(0),glbinding(0),out
+RWStructuredBuffer<int> gOutputBuffer;
+
+[numthreads(4, 1, 1)]
+void computeMain(uint3 tid : SV_DispatchThreadID)
+{
+ int inVal = tid.x;
+ int outVal = test(inVal);
+ gOutputBuffer[tid.x] = outVal;
+}
diff --git a/tests/compute/cast-zero-to-struct.slang.expected.txt b/tests/compute/cast-zero-to-struct.slang.expected.txt
new file mode 100644
index 000000000..7e1d6b7b7
--- /dev/null
+++ b/tests/compute/cast-zero-to-struct.slang.expected.txt
@@ -0,0 +1,4 @@
+0
+11001
+22002
+33003 \ No newline at end of file