Skip to content

Commit d0a20a6

Browse files
committed
Add slang shaders for additional ray tracing samples
1 parent 150b510 commit d0a20a6

4 files changed

Lines changed: 240 additions & 0 deletions

File tree

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
/* Copyright (c) 2025, Sascha Willems
2+
*
3+
* SPDX-License-Identifier: MIT
4+
*
5+
*/
6+
7+
struct Payload
8+
{
9+
float3 hitValue;
10+
}
11+
12+
RaytracingAccelerationStructure accelStruct;
13+
RWTexture2D<float4> image;
14+
15+
struct UBO
16+
{
17+
float4x4 viewInverse;
18+
float4x4 projInverse;
19+
float4 lightPos;
20+
};
21+
ConstantBuffer<UBO> ubo;
22+
23+
struct Sphere {
24+
float3 center;
25+
float radius;
26+
float4 color;
27+
};
28+
StructuredBuffer<Sphere> spheres;
29+
30+
// Ray-sphere intersection
31+
// By Inigo Quilez, from https://iquilezles.org/articles/spherefunctions/
32+
float sphIntersect(const Sphere s, float3 ro, float3 rd)
33+
{
34+
float3 oc = ro - s.center;
35+
float b = dot(oc, rd);
36+
float c = dot(oc, oc) - s.radius * s.radius;
37+
float h = b * b - c;
38+
if (h < 0.0) {
39+
return -1.0;
40+
}
41+
h = sqrt(h);
42+
return -b - h;
43+
}
44+
45+
[shader("intersection")]
46+
void intersectionMain() {
47+
Sphere sphere = spheres[PrimitiveIndex()];
48+
float hit = sphIntersect(sphere, WorldRayOrigin(), WorldRayDirection());
49+
50+
if (hit > 0) {
51+
ReportHit(hit, 0, 0);
52+
}
53+
}
54+
55+
[shader("raygeneration")]
56+
void raygenerationMain()
57+
{
58+
uint3 LaunchID = DispatchRaysIndex();
59+
uint3 LaunchSize = DispatchRaysDimensions();
60+
61+
const float2 pixelCenter = float2(LaunchID.xy) + float2(0.5, 0.5);
62+
const float2 inUV = pixelCenter / float2(LaunchSize.xy);
63+
float2 d = inUV * 2.0 - 1.0;
64+
float4 target = mul(ubo.projInverse, float4(d.x, d.y, 1, 1));
65+
66+
RayDesc rayDesc;
67+
rayDesc.Origin = mul(ubo.viewInverse, float4(0, 0, 0, 1)).xyz;
68+
rayDesc.Direction = mul(ubo.viewInverse, float4(normalize(target.xyz), 0)).xyz;
69+
rayDesc.TMin = 0.001;
70+
rayDesc.TMax = 10000.0;
71+
72+
Payload payload;
73+
TraceRay(accelStruct, RAY_FLAG_FORCE_OPAQUE, 0xff, 0, 0, 0, rayDesc, payload);
74+
75+
image[int2(LaunchID.xy)] = float4(payload.hitValue, 0.0);
76+
}
77+
78+
[shader("closesthit")]
79+
void closesthitMain(inout Payload payload)
80+
{
81+
Sphere sphere = spheres[PrimitiveIndex()];
82+
83+
float3 worldPos = WorldRayOrigin() + WorldRayDirection() * RayTCurrent();
84+
float3 worldNormal = normalize(worldPos - sphere.center);
85+
86+
// Basic lighting
87+
float3 lightVector = normalize(ubo.lightPos.xyz);
88+
float dot_product = max(dot(lightVector, worldNormal), 0.2);
89+
payload.hitValue = sphere.color.rgb * dot_product;
90+
}
91+
92+
[shader("miss")]
93+
void missMain(inout Payload payload)
94+
{
95+
payload.hitValue = float3(0.0, 0.0, 0.2);
96+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/* Copyright (c) 2025, Sascha Willems
2+
*
3+
* SPDX-License-Identifier: MIT
4+
*
5+
*/
6+
7+
module payload;
8+
9+
public struct Payload
10+
{
11+
public float3 hitValue;
12+
public bool shadowed;
13+
};
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
/* Copyright (c) 2025, Sascha Willems
2+
*
3+
* SPDX-License-Identifier: MIT
4+
*
5+
*/
6+
7+
import payload;
8+
9+
struct Attributes
10+
{
11+
float2 bary;
12+
};
13+
14+
RaytracingAccelerationStructure accelStruct;
15+
RWTexture2D<float4> image;
16+
17+
struct UBO
18+
{
19+
float4x4 viewInverse;
20+
float4x4 projInverse;
21+
float4 lightPos;
22+
int vertexSize;
23+
};
24+
ConstantBuffer<UBO> ubo;
25+
26+
StructuredBuffer<float4> vertices;
27+
StructuredBuffer<uint> indices;
28+
29+
struct Vertex
30+
{
31+
float3 pos;
32+
float3 normal;
33+
float2 uv;
34+
float4 color;
35+
float4 _pad0;
36+
float4 _pad1;
37+
};
38+
39+
Vertex unpack(uint index)
40+
{
41+
// Unpack the vertices from the SSBO using the glTF vertex structure
42+
// The multiplier is the size of the vertex divided by four float components (=16 bytes)
43+
const int m = ubo.vertexSize / 16;
44+
45+
float4 d0 = vertices[m * index + 0];
46+
float4 d1 = vertices[m * index + 1];
47+
float4 d2 = vertices[m * index + 2];
48+
49+
Vertex v;
50+
v.pos = d0.xyz;
51+
v.normal = float3(d0.w, d1.x, d1.y);
52+
v.color = float4(d2.x, d2.y, d2.z, 1.0);
53+
54+
return v;
55+
}
56+
57+
[shader("raygeneration")]
58+
void raygenerationMain()
59+
{
60+
uint3 LaunchID = DispatchRaysIndex();
61+
uint3 LaunchSize = DispatchRaysDimensions();
62+
63+
const float2 pixelCenter = float2(LaunchID.xy) + float2(0.5, 0.5);
64+
const float2 inUV = pixelCenter / float2(LaunchSize.xy);
65+
float2 d = inUV * 2.0 - 1.0;
66+
float4 target = mul(ubo.projInverse, float4(d.x, d.y, 1, 1));
67+
68+
RayDesc rayDesc;
69+
rayDesc.Origin = mul(ubo.viewInverse, float4(0, 0, 0, 1)).xyz;
70+
rayDesc.Direction = mul(ubo.viewInverse, float4(normalize(target.xyz), 0)).xyz;
71+
rayDesc.TMin = 0.001;
72+
rayDesc.TMax = 10000.0;
73+
74+
Payload payload;
75+
TraceRay(accelStruct, RAY_FLAG_FORCE_OPAQUE, 0xff, 0, 0, 0, rayDesc, payload);
76+
77+
image[int2(LaunchID.xy)] = float4(payload.hitValue, 0.0);
78+
}
79+
80+
[shader("closesthit")]
81+
void closesthitMain(inout Payload payload, in Attributes attribs)
82+
{
83+
uint PrimitiveID = PrimitiveIndex();
84+
int3 index = int3(indices[3 * PrimitiveID], indices[3 * PrimitiveID + 1], indices[3 * PrimitiveID + 2]);
85+
86+
Vertex v0 = unpack(index.x);
87+
Vertex v1 = unpack(index.y);
88+
Vertex v2 = unpack(index.z);
89+
90+
// Interpolate normal
91+
const float3 barycentricCoords = float3(1.0f - attribs.bary.x - attribs.bary.y, attribs.bary.x, attribs.bary.y);
92+
float3 normal = normalize(v0.normal * barycentricCoords.x + v1.normal * barycentricCoords.y + v2.normal * barycentricCoords.z);
93+
94+
// Basic lighting
95+
float3 lightVector = normalize(ubo.lightPos.xyz);
96+
float dot_product = max(dot(lightVector, normal), 0.2);
97+
payload.hitValue = v0.color.rgb * dot_product;
98+
99+
RayDesc rayDesc;
100+
rayDesc.Origin = WorldRayOrigin() + WorldRayDirection() * RayTCurrent();
101+
rayDesc.Direction = lightVector;
102+
rayDesc.TMin = 0.001;
103+
rayDesc.TMax = 100.0;
104+
105+
payload.shadowed = true;
106+
// Offset indices to match shadow hit/miss index
107+
TraceRay(accelStruct, RAY_FLAG_ACCEPT_FIRST_HIT_AND_END_SEARCH | RAY_FLAG_FORCE_OPAQUE | RAY_FLAG_SKIP_CLOSEST_HIT_SHADER, 0xff, 0, 0, 1, rayDesc, payload);
108+
if (payload.shadowed) {
109+
payload.hitValue *= 0.3;
110+
}
111+
}
112+
113+
[shader("miss")]
114+
void missMain(inout Payload payload)
115+
{
116+
payload.hitValue = float3(0.0, 0.0, 0.2);
117+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/* Copyright (c) 2025, Sascha Willems
2+
*
3+
* SPDX-License-Identifier: MIT
4+
*
5+
*/
6+
7+
import payload;
8+
9+
[shader("miss")]
10+
void missMain(inout Payload payload)
11+
{
12+
payload.shadowed = false;
13+
}
14+

0 commit comments

Comments
 (0)