yum-mirror/slang

Making it easier to work with shaders

git clone https://git.yummers.dev/yum-mirror/slang

Yong He`is` and `as` operator and `Optional<T>`. (#2355)88f04c292

master
796 B46 linesraw
1// is-as.slang
2
3// Test that `is` and `as` operators works as intended.
4
5//TEST(compute):COMPARE_COMPUTE: -shaderobj
6
7//TEST_INPUT:ubuffer(data=[0 0 0 0], stride=4):out,name=outputBuffer
8RWStructuredBuffer<int> outputBuffer;
9
10interface IFoo
11{
12    int method();
13}
14
15struct Impl1 : IFoo
16{
17    int data;
18    int method() { return data; }
19}
20
21struct Impl2 : IFoo
22{
23    int data1;
24    int data2;
25    int method() { return data1 + data2; }
26}
27
28int getData(IFoo foo)
29{
30    let castResult = foo as Impl2;
31    if (castResult.hasValue)
32    {
33        return castResult.value.method();
34    }
35    return 0;
36}
37
38[numthreads(1, 1, 1)]
39void computeMain(uint3 dispatchThreadID: SV_DispatchThreadID)
40{
41    Impl2 obj;
42    obj.data1 = 1;
43    obj.data2 = 2;
44    int outVal = getData(obj);
45    outputBuffer[0] = outVal;
46}