Skip to content

Commit 381c6ef

Browse files
committed
Added shaders for ray tracing callable sample
1 parent b102f0b commit 381c6ef

5 files changed

Lines changed: 118 additions & 0 deletions

File tree

shaders/slang/_rename.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@
77
def checkRenameFiles(samplename):
88
mappings = {}
99
match samplename:
10+
case "displacement":
11+
mappings = {
12+
"displacement.vert.spv": "base.vert.spv",
13+
"displacement.frag.spv": "base.frag.spv",
14+
}
1015
case "geometryshader":
1116
mappings = {
1217
"normaldebug.vert.spv": "base.vert.spv",
@@ -18,6 +23,12 @@ def checkRenameFiles(samplename):
1823
"raytracingbasic.rmiss.spv": "miss.rmiss.spv",
1924
"raytracingbasic.rgen.spv": "raygen.rgen.spv",
2025
}
26+
case "raytracingcallable":
27+
mappings = {
28+
"raytracingcallable.rchit.spv": "closesthit.rchit.spv",
29+
"raytracingcallable.rmiss.spv": "miss.rmiss.spv",
30+
"raytracingcallable.rgen.spv": "raygen.rgen.spv",
31+
}
2132
case "raytracinggltf":
2233
mappings = {
2334
"raytracinggltf.rchit.spv": "closesthit.rchit.spv",
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+
[shader("callable")]
8+
void callableMain(inout float3 outColor)
9+
{
10+
// Generate a checker board pattern
11+
float2 pos = float2(DispatchRaysIndex().x / 8, DispatchRaysIndex().y / 8);
12+
float col = (pos.x + (pos.y % 2.0)) % 2.0;
13+
outColor = float3(col, col, col);
14+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/* Copyright (c) 2025, Sascha Willems
2+
*
3+
* SPDX-License-Identifier: MIT
4+
*
5+
*/
6+
7+
[shader("callable")]
8+
void callableMain(inout float3 outColor)
9+
{
10+
outColor = float3(0.0, 1.0, 0.0);
11+
}
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+
[shader("callable")]
8+
void callableMain(inout float3 outColor)
9+
{
10+
// Generate a checker board pattern
11+
float2 pos = float2(DispatchRaysIndex().x / 8, DispatchRaysIndex().y / 8);
12+
float col = pos.y % 2.0;
13+
outColor = float3(col, col, col);
14+
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/* Copyright (c) 2025, Sascha Willems
2+
*
3+
* SPDX-License-Identifier: MIT
4+
*
5+
*/
6+
7+
RaytracingAccelerationStructure accelStruct;
8+
RWTexture2D<float4> image;
9+
struct CameraProperties
10+
{
11+
float4x4 viewInverse;
12+
float4x4 projInverse;
13+
};
14+
ConstantBuffer<CameraProperties> cam;
15+
16+
struct Payload
17+
{
18+
float3 hitValue;
19+
};
20+
21+
struct CallData
22+
{
23+
float3 outColor;
24+
};
25+
26+
struct Attributes
27+
{
28+
float2 bary;
29+
};
30+
31+
[shader("raygeneration")]
32+
void raygenerationMain()
33+
{
34+
uint3 LaunchID = DispatchRaysIndex();
35+
uint3 LaunchSize = DispatchRaysDimensions();
36+
37+
const float2 pixelCenter = float2(LaunchID.xy) + float2(0.5, 0.5);
38+
const float2 inUV = pixelCenter / float2(LaunchSize.xy);
39+
float2 d = inUV * 2.0 - 1.0;
40+
float4 target = mul(cam.projInverse, float4(d.x, d.y, 1, 1));
41+
42+
RayDesc rayDesc;
43+
rayDesc.Origin = mul(cam.viewInverse, float4(0, 0, 0, 1)).xyz;
44+
rayDesc.Direction = mul(cam.viewInverse, float4(normalize(target.xyz), 0)).xyz;
45+
rayDesc.TMin = 0.001;
46+
rayDesc.TMax = 10000.0;
47+
48+
Payload payload;
49+
TraceRay(accelStruct, RAY_FLAG_FORCE_OPAQUE, 0xff, 0, 0, 0, rayDesc, payload);
50+
51+
image[int2(LaunchID.xy)] = float4(payload.hitValue, 0.0);
52+
}
53+
54+
[shader("closesthit")]
55+
void closesthitMain(inout Payload p, in Attributes attribs)
56+
{
57+
// Execute the callable shader indexed by the current geometry being hit
58+
// For our sample this means that the first callable shader in the SBT is invoked for the first triangle, the second callable shader for the second triangle, etc.
59+
CallData callData;
60+
CallShader(GeometryIndex(), callData);
61+
p.hitValue = callData.outColor;
62+
}
63+
64+
[shader("miss")]
65+
void missMain(inout Payload p)
66+
{
67+
p.hitValue = float3(0.0, 0.0, 0.2);
68+
}

0 commit comments

Comments
 (0)