yum-mirror/slang

Making it easier to work with shaders

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

jsmall-nvidia'Explicit specialization' experiments with extensions (#2099)3aaa586c9

master
1.4 KiB56 linesraw
1//DISABLE_TEST:SIMPLE:-target hlsl -entry computeMain -profile cs_6_2
2
3/* 
4In C++ we are able to explicitly specialize over more than one type/value. Here we try to use a 'dummy' generic 
5type such that an extension can be applied to it.
6
7I can't just specialize a function also complicating things. 
8
9If I try explicit function specialization in C++. In g++11.1 it will complain if there isn't a specialition visible. 
10Visual studio it seems to assume it is available for import and doesn't complain.
11*/
12
13RWStructuredBuffer<float> outputBuffer;
14
15interface IDoThing
16{
17    static float doThing(float v);
18};
19
20struct Combination<T, let V : int> {};
21
22extension Combination<int, 10> : IDoThing
23{
24    static float doThing(float v)
25    {
26        return int(v) + 10;
27    }
28};
29
30extension Combination<float, 20> : IDoThing
31{
32    static float doThing(float v)
33    {
34        return float(v) + 20;
35    }
36};
37
38
39[numthreads(4, 1, 1)]
40void computeMain(uint3 dispatchThreadID : SV_DispatchThreadID)
41{
42    uint tid = dispatchThreadID.x;
43
44    let v = Combination<float, 20>::doThing(tid) + 
45        Combination<int, 10>::doThing(tid);
46    
47    // Produces an error - although the error message of typeof(Combination)
48    // is probably not great.
49    // 
50    // slang(35): error 30027: 'doThing' is not a member of 'typeof(Combination)'.
51    //
52    //let y = Combination<int, 20>::doThing(tid);
53    
54    outputBuffer[tid] = v;
55}
56