yum-mirror/slang

Making it easier to work with shaders

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

Dario MylonopoulosSupport spirv ops added by multiple extensions (#6615)969d101af

master
3.1 KiB132 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
25
26layout(rgba32f)
27RWTexture2D<float4> outputImage;
28
29RaytracingAccelerationStructure as;
30
31struct ShadowRay
32{
33    float hitDistance;
34};
35
36struct ReflectionRay
37{
38    float color;
39};
40
41#define gl_LaunchIDNV DispatchRaysIndex()
42#define gl_LaunchSizeNV DispatchRaysDimensions()
43
44void main() 
45{
46    float2 inUV = float2(
47        (float(gl_LaunchIDNV.x) + 0.5f) / float(gl_LaunchSizeNV.x),
48        (float(gl_LaunchIDNV.y) + 0.5f) / float(gl_LaunchSizeNV.y)
49    );
50
51    float3 P = samplerPosition.SampleLevel(sampler, inUV, 0).rgb;
52    float3 N = samplerNormal.SampleLevel(sampler, inUV, 0).rgb * 2.0 - 1.0;
53
54    float3 lightPos = ubo.light.position.xyz;
55    float3 lightDelta = lightPos - P;
56    float lightDist = length(lightDelta);
57    float3 L = normalize(lightDelta);
58    float atten = 1.0f / (lightDist*lightDist);
59
60    RayDesc ray;
61    ray.Origin = P;
62    ray.TMin = TRACING_EPSILON;
63    ray.Direction = lightDelta;
64    ray.TMax = lightDist;
65
66    {
67        ShadowRay shadowRay;
68        shadowRay.hitDistance = 0;
69
70        TraceRay(as,
71                // ray flags
72                1,
73                // cull mask
74                0xff,
75                // sbt record offset
76                0,
77                // sbt record stride
78                0,
79                // missIndex
80                2,
81                // ray
82                ray,
83                // payload
84                shadowRay);
85        
86        if (shadowRay.hitDistance < lightDist)
87        {
88            atten = 0.f;
89        }
90    }
91
92    float3 color = ubo.light.color.xyz * saturate(dot(N,L)) * atten;
93
94
95    {
96        ReflectionRay reflectionRay;
97        TraceRay(as,
98                // ray flags
99                1,
100                // cull mask
101                0xff,
102                // sbt record offset
103                0,
104                // sbt record stride
105                0,
106                // missIndex
107                2,
108                // ray
109                ray,
110                // payload
111                reflectionRay);
112
113
114        color = color + reflectionRay.color;
115    }
116
117    outputImage[int2(gl_LaunchIDNV.xy)] = float4(color, 1.0);
118}
119
120// CHECK_SPV: OpCapability RayTracingKHR
121// CHECK_SPV-NOT: OpCapability RayQueryKHR
122// CHECK_SPV: OpExtension "SPV_KHR_ray_tracing"
123// CHECK_SPV-NOT: OpExtension "SPV_KHR_ray_query"
124// CHECK_SPV: %{{.*}} = OpVariable %_ptr_RayPayload{{NV|KHR}}_ReflectionRay{{.*}} RayPayload
125// CHECK_SPV: OpTraceRayKHR
126// CHECK_SPV: OpTraceRayKHR
127
128// CHECK_HLSL: TraceRay
129// CHECK_HLSL: TraceRay
130
131// CHECK_GLSL: traceRayEXT(
132// CHECK_GLSL: traceRayEXT(