Skip to content

Commit 75f3508

Browse files
committed
Add slang shaders for additional ray tracing samples
1 parent b2272c5 commit 75f3508

3 files changed

Lines changed: 179 additions & 1 deletion

File tree

shaders/slang/_rename.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,25 @@ def checkRenameFiles(samplename):
2424
"raytracinggltf.rmiss.spv": "miss.rmiss.spv",
2525
"raytracinggltf.rgen.spv": "raygen.rgen.spv",
2626
"raytracinggltf.rahit.spv": "anyhit.rahit.spv",
27-
}
27+
}
28+
case "raytracingpositionfetch":
29+
mappings = {
30+
"raytracingpositionfetch.rchit.spv": "closesthit.rchit.spv",
31+
"raytracingpositionfetch.rmiss.spv": "miss.rmiss.spv",
32+
"raytracingpositionfetch.rgen.spv": "raygen.rgen.spv",
33+
}
2834
case "raytracingreflections":
2935
mappings = {
3036
"raytracingreflections.rchit.spv": "closesthit.rchit.spv",
3137
"raytracingreflections.rmiss.spv": "miss.rmiss.spv",
3238
"raytracingreflections.rgen.spv": "raygen.rgen.spv",
3339
}
40+
case "raytracingsbtdata":
41+
mappings = {
42+
"raytracingsbtdata.rchit.spv": "closesthit.rchit.spv",
43+
"raytracingsbtdata.rmiss.spv": "miss.rmiss.spv",
44+
"raytracingsbtdata.rgen.spv": "raygen.rgen.spv",
45+
}
3446
case "raytracingshadows":
3547
mappings = {
3648
"raytracingshadows.rchit.spv": "closesthit.rchit.spv",
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
/* Copyright (c) 2025, Sascha Willems
2+
*
3+
* SPDX-License-Identifier: MIT
4+
*
5+
*/
6+
7+
struct Attributes
8+
{
9+
float2 bary;
10+
};
11+
12+
struct Payload
13+
{
14+
[[vk::location(0)]] float3 hitValue;
15+
};
16+
17+
RaytracingAccelerationStructure accelStruct;
18+
RWTexture2D<float4> image;
19+
struct UBO
20+
{
21+
float4x4 viewInverse;
22+
float4x4 projInverse;
23+
float4 lightPos;
24+
};
25+
ConstantBuffer<UBO> ubo;
26+
27+
[shader("raygeneration")]
28+
void raygenerationMain()
29+
{
30+
uint3 LaunchID = DispatchRaysIndex();
31+
uint3 LaunchSize = DispatchRaysDimensions();
32+
33+
const float2 pixelCenter = float2(LaunchID.xy) + float2(0.5, 0.5);
34+
const float2 inUV = pixelCenter / float2(LaunchSize.xy);
35+
float2 d = inUV * 2.0 - 1.0;
36+
float4 target = mul(ubo.projInverse, float4(d.x, d.y, 1, 1));
37+
38+
RayDesc rayDesc;
39+
rayDesc.Origin = mul(ubo.viewInverse, float4(0, 0, 0, 1)).xyz;
40+
rayDesc.Direction = mul(ubo.viewInverse, float4(normalize(target.xyz), 0)).xyz;
41+
rayDesc.TMin = 0.001;
42+
rayDesc.TMax = 10000.0;
43+
44+
Payload payload;
45+
TraceRay(accelStruct, RAY_FLAG_FORCE_OPAQUE, 0xff, 0, 0, 0, rayDesc, payload);
46+
47+
image[int2(LaunchID.xy)] = float4(payload.hitValue, 0.0);
48+
}
49+
50+
[shader("closesthit")]
51+
void closesthitMain(inout Payload payload, in Attributes attribs)
52+
{
53+
// We need the barycentric coordinates to calculate data for the current position
54+
const float3 barycentricCoords = float3(1.0f - attribs.bary.x - attribs.bary.y, attribs.bary.x, attribs.bary.y);
55+
56+
// With VK_KHR_ray_tracing_position_fetch we can access the vertices for the hit triangle in the shader
57+
58+
float3 vertexPos0 = HitTriangleVertexPosition(0);
59+
float3 vertexPos1 = HitTriangleVertexPosition(1);
60+
float3 vertexPos2 = HitTriangleVertexPosition(2);
61+
float3 currentPos = vertexPos0 * barycentricCoords.x + vertexPos1 * barycentricCoords.y + vertexPos2 * barycentricCoords.z;
62+
63+
// Calcualte the normal from above values
64+
float3 normal = normalize(cross(vertexPos1 - vertexPos0, vertexPos2 - vertexPos0));
65+
normal = normalize(mul(float4(normal, 1.0), WorldToObject4x3()));
66+
67+
// Basic lighting
68+
float3 lightDir = normalize(ubo.lightPos.xyz - currentPos);
69+
float diffuse = max(dot(normal, lightDir), 0.0);
70+
71+
payload.hitValue.rgb = 0.1 + diffuse;
72+
}
73+
74+
[shader("miss")]
75+
void missMain(inout Payload payload)
76+
{
77+
payload.hitValue = float3(0.0, 0.0, 0.2);
78+
}
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
/* Copyright (c) 2025, Sascha Willems
2+
*
3+
* SPDX-License-Identifier: MIT
4+
*
5+
*/
6+
7+
struct Attributes
8+
{
9+
float2 bary;
10+
};
11+
12+
struct Payload
13+
{
14+
float3 hitValue;
15+
};
16+
17+
RaytracingAccelerationStructure accelStruct;
18+
RWTexture2D<float4> image;
19+
struct CameraProperties
20+
{
21+
float4x4 viewInverse;
22+
float4x4 projInverse;
23+
};
24+
ConstantBuffer<CameraProperties> cam;
25+
26+
struct SBT {
27+
float r;
28+
float g;
29+
float b;
30+
};
31+
[[vk::shader_record]] ConstantBuffer<SBT> sbt;
32+
33+
[shader("raygeneration")]
34+
void raygenerationMain()
35+
{
36+
uint3 LaunchID = DispatchRaysIndex();
37+
uint3 LaunchSize = DispatchRaysDimensions();
38+
39+
const float2 pixelCenter = float2(LaunchID.xy) + float2(0.5, 0.5);
40+
const float2 inUV = pixelCenter / float2(LaunchSize.xy);
41+
float2 d = inUV * 2.0 - 1.0;
42+
float4 target = mul(cam.projInverse, float4(d.x, d.y, 1, 1));
43+
44+
RayDesc rayDesc;
45+
rayDesc.Origin = mul(cam.viewInverse, float4(0, 0, 0, 1)).xyz;
46+
rayDesc.Direction = mul(cam.viewInverse, float4(normalize(target.xyz), 0)).xyz;
47+
rayDesc.TMin = 0.001;
48+
rayDesc.TMax = 10000.0;
49+
50+
Payload payload;
51+
52+
// use border to demonstrate raygen record data
53+
if (all(LaunchID.xy > int2(16, 16)) && all(LaunchID.xy < LaunchSize.xy - int2(16, 16)))
54+
{
55+
// Generate a checker board pattern to trace out rays or use hit record data
56+
int2 pos = int2(LaunchID.xy / 16);
57+
if (((pos.x + pos.y % 2) % 2) == 0) {
58+
// This will set hit value to either hit or miss SBT record color
59+
TraceRay(accelStruct, RAY_FLAG_FORCE_OPAQUE, 0xff, 0, 0, 0, rayDesc, payload);
60+
}
61+
else {
62+
// Set the hit value to the raygen SBT data
63+
payload.hitValue = float3(sbt.r, sbt.g, sbt.b);
64+
}
65+
}
66+
else {
67+
// Set hit value to black
68+
payload.hitValue = float3(0.0, 0.0, 0.0);
69+
}
70+
71+
image[int2(LaunchID.xy)] = float4(payload.hitValue, 0.0);
72+
}
73+
74+
[shader("closesthit")]
75+
void closesthitMain(inout Payload payload, in Attributes attribs)
76+
{
77+
// Update the hit value to the hit record SBT data associated with this
78+
// geometry ID and ray ID
79+
payload.hitValue = float3(sbt.r, sbt.g, sbt.g);
80+
}
81+
82+
[shader("miss")]
83+
void missMain(inout Payload payload)
84+
{
85+
// Update the hit value to the hit record SBT data associated with this
86+
// miss record
87+
payload.hitValue = float3(sbt.r, sbt.g, sbt.g);
88+
}

0 commit comments

Comments
 (0)