yum-mirror/slang

Making it easier to work with shaders

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

Yong HeAdd `none` literal that is convertible to `Optional`. (#2356)a083a37ee

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