yum-mirror/slang

Making it easier to work with shaders

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

Jay KwakUpdate spirv-header and spirv-tools to Jun/2024 (#4679)ad379b7c5

master
3.0 KiB130 linesraw
1//TEST:SIMPLE(filecheck=CHECK_SPV): -emit-spirv-directly -stage raygeneration -entry main -target spirv-assembly
2//TEST:SIMPLE(filecheck=CHECK_HLSL): -stage raygeneration -entry main -target hlsl
3//TEST:SIMPLE(filecheck=CHECK_GLSL): -stage raygeneration -entry main -target glsl
4
5#define TRACING_EPSILON 1e-6
6
7Texture2D   samplerPosition;
8Texture2D   samplerNormal;
9SamplerState sampler;
10
11struct Light {
12    float4 position;
13    float4 color;
14};
15
16struct Uniforms
17{
18    Light       light;
19    float4      viewPos;
20    float4x4    view;
21    float4x4    model;    
22};
23ConstantBuffer<Uniforms> ubo;
24
25layout(rgba32f)
26RWTexture2D<float4> outputImage;
27
28RaytracingAccelerationStructure as;
29
30struct ShadowRay
31{
32    float hitDistance;
33};
34
35struct ReflectionRay
36{
37    float color;
38};
39
40void main() 
41{
42    int2 launchID = int2(DispatchRaysIndex().xy);
43    int2 launchSize = int2(DispatchRaysDimensions().xy);
44    
45    float2 inUV = float2(
46        (float(launchID.x) + 0.5f) / float(launchSize.x),
47        (float(launchID.y) + 0.5f) / float(launchSize.y)
48    );
49
50    float3 P = samplerPosition.SampleLevel(sampler, inUV, 0).rgb;
51    float3 N = samplerNormal.SampleLevel(sampler, inUV, 0).rgb * 2.0 - 1.0;
52
53    float3 lightPos = ubo.light.position.xyz;
54    float3 lightDelta = lightPos - P;
55    float lightDist = length(lightDelta);
56    float3 L = normalize(lightDelta);
57    float atten = 1.0f / (lightDist*lightDist);
58
59    RayDesc ray;
60    ray.Origin = P;
61    ray.TMin = TRACING_EPSILON;
62    ray.Direction = lightDelta;
63    ray.TMax = lightDist;
64
65    {
66        ShadowRay shadowRay;
67        shadowRay.hitDistance = 0;
68
69        float currentTime = 1;
70
71        TraceMotionRay(as,
72                // ray flags
73                1,
74                // cull mask
75                0xff,
76                // sbt record offset
77                0,
78                // sbt record stride
79                0,
80                // missIndex
81                2,
82                // ray
83                ray,
84                // currentTime
85                currentTime,
86                // payload
87                shadowRay);
88        
89        if (shadowRay.hitDistance < lightDist)
90        {
91            atten = 0.f;
92        }
93    }
94
95    float3 color = ubo.light.color.xyz * saturate(dot(N,L)) * atten;
96
97    {
98        ReflectionRay reflectionRay;
99        TraceRay(as,
100                // ray flags
101                1,
102                // cull mask
103                0xff,
104                // sbt record offset
105                0,
106                // sbt record stride
107                0,
108                // missIndex
109                2,
110                // ray
111                ray,
112                // payload
113                reflectionRay);
114
115
116        color = color + reflectionRay.color;
117    }
118
119    outputImage[launchID] = float4(color, 1.0);
120}
121
122// CHECK_SPV: %{{.*}} = OpVariable %_ptr_RayPayload{{NV|KHR}}_ReflectionRay{{.*}} RayPayload
123// CHECK_SPV: OpTraceRayMotionNV
124// CHECK_SPV: OpTraceRayKHR
125
126// CHECK_HLSL: TraceMotionRay
127// CHECK_HLSL: TraceRay
128
129// CHECK_GLSL: traceRayMotionNV(
130// CHECK_GLSL: traceRayEXT(