yum-mirror/slang

Making it easier to work with shaders

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

Yong HeEnhance buffer load specialization pass to specialize past field extracts. (#8547)e4611e2e3

master
1.6 KiB83 linesraw
1//TEST:SIMPLE(filecheck=WGSL): -target wgsl -conformance "Circle:IShape=0" -conformance "Rectangle:IShape=1"
2
3[anyValueSize(16)]
4interface IShape
5{
6    float getArea();
7}
8
9struct Circle : IShape
10{
11    float radius;
12    
13    float getArea() { return 3.14159 * radius * radius; }
14}
15
16struct Rectangle : IShape
17{
18    float width;
19    float height;
20    
21    float getArea() { return width * height; }
22}
23
24struct ShapeDataBlob { uint type; uint payload[16]; }
25
26struct ShapeBuffer
27{
28    StructuredBuffer<ShapeDataBlob> shapes;
29    IShape getShape(uint index)
30    {
31      uint type = shapes[index].type;
32      return createDynamicObject<IShape, ShapeDataBlob>(type, shapes[index]);
33    }
34}
35
36struct VertexInput
37{
38    float2 position;
39    float3 color;
40}
41
42struct VertexOutput
43{
44    float4 position : SV_POSITION;
45    float3 color : TEXCOORD0;
46    float area : TEXCOORD1;
47}
48
49
50[shader("vertex")]
51func vs_main( input: VertexInput, shapes: ShapeBuffer)->VertexOutput
52{
53    VertexOutput output;
54    output.position = float4(input.position, 0.0, 1.0);
55    output.color = input.color;
56    output.area = shapes.getShape(0).getArea(); 
57    return output;
58}
59
60struct FragmentOutput
61{
62    float4 color : SV_TARGET;
63}
64
65[shader("fragment")]
66func fs_main(VertexOutput input)->FragmentOutput
67{
68    FragmentOutput output;
69    output.color = float4(input.color * input.area, 1.0);
70    return output;
71}
72
73//WGSL:     switch({{.*}})
74//WGSL-NEXT:     {
75//WGSL-NEXT:     case u32(0):
76//WGSL-NEXT:         {
77//WGSL-NEXT:             return Circle_getArea_0
78//WGSL-NEXT:         }
79//WGSL-NEXT:     default :
80//WGSL-NEXT:         {
81//WGSL-NEXT:             return Rectangle_getArea_0
82//WGSL-NEXT:         }
83//WGSL-NEXT:     }