Skip to content

Commit a4354cd

Browse files
committed
Add slang shader for ray tracing basic sample
1 parent 33cab71 commit a4354cd

4 files changed

Lines changed: 87 additions & 4 deletions

File tree

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -240,4 +240,6 @@ android/.idea/**
240240

241241
libs/vulkan/*.so
242242

243-
.vscode/*
243+
.vscode/*
244+
245+
__pycache__**

shaders/slang/_rename.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Copyright (C) 2025 by Sascha Willems - www.saschawillems.de
2+
# This code is licensed under the MIT license (MIT) (http://opensource.org/licenses/MIT)
3+
4+
from shutil import move
5+
6+
# To match required file names to fother shading languages that don't support multiple entry points, shader files may need to be renamed for some samples
7+
def checkRenameFiles(samplename):
8+
mappings = {}
9+
match samplename:
10+
case "raytracingbasic":
11+
mappings = {
12+
"raytracingbasic.rchit.spv": "closesthit.rchit.spv",
13+
"raytracingbasic.rmiss.spv": "miss.rmiss.spv",
14+
"raytracingbasic.rgen.spv": "raygen.rgen.spv",
15+
}
16+
for x, y in mappings.items():
17+
move(samplename + "\\" + x, samplename + "\\" + y)

shaders/slang/compileshaders.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import os
77
import subprocess
88
import sys
9+
from _rename import *
910

1011
parser = argparse.ArgumentParser(description='Compile all slang shaders')
1112
parser.add_argument('--slangc', type=str, help='path to slangc executable')
@@ -70,13 +71,15 @@ def getShaderStages(filename):
7071
dir_path = dir_path.replace('\\', '/')
7172
for root, dirs, files in os.walk(dir_path):
7273
for file in files:
73-
if (compile_single_sample != "" and os.path.basename(root) != compile_single_sample):
74+
folder_name = os.path.basename(root)
75+
if (compile_single_sample != "" and folder_name != compile_single_sample):
7476
continue
7577
if file.endswith(".slang"):
7678
input_file = os.path.join(root, file)
7779
# Slang can store multiple shader stages in a single file, we need to split into separate SPIR-V files for the sample framework
7880
stages = getShaderStages(input_file)
7981
print("Compiling %s" % input_file)
82+
output_base_file_name = input_file
8083
for stage in stages:
8184
if (len(stages) > 1):
8285
entry_point = stage + "Main"
@@ -102,8 +105,9 @@ def getShaderStages(filename):
102105
output_ext = ".mesh"
103106
case "amplification":
104107
output_ext = ".task"
105-
output_file = input_file + output_ext + ".spv"
108+
output_file = output_base_file_name + output_ext + ".spv"
106109
output_file = output_file.replace(".slang", "")
107110
res = subprocess.call("%s %s -profile spirv_1_4 -matrix-layout-column-major -target spirv -o %s -entry %s -stage %s -warnings-disable 39001" % (compiler_path, input_file, output_file, entry_point, stage), shell=True)
108111
if res != 0:
109-
sys.exit(res)
112+
sys.exit(res)
113+
checkRenameFiles(folder_name)
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
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 Attributes
17+
{
18+
float2 bary;
19+
};
20+
21+
struct Payload
22+
{
23+
float3 hitValue;
24+
};
25+
26+
[shader("raygeneration")]
27+
void raygenerationMain()
28+
{
29+
uint3 LaunchID = DispatchRaysIndex();
30+
uint3 LaunchSize = DispatchRaysDimensions();
31+
32+
const float2 pixelCenter = float2(LaunchID.xy) + float2(0.5, 0.5);
33+
const float2 inUV = pixelCenter/float2(LaunchSize.xy);
34+
float2 d = inUV * 2.0 - 1.0;
35+
float4 target = mul(cam.projInverse, float4(d.x, d.y, 1, 1));
36+
37+
RayDesc rayDesc;
38+
rayDesc.Origin = mul(cam.viewInverse, float4(0,0,0,1)).xyz;
39+
rayDesc.Direction = mul(cam.viewInverse, float4(normalize(target.xyz), 0)).xyz;
40+
rayDesc.TMin = 0.001;
41+
rayDesc.TMax = 10000.0;
42+
43+
Payload payload;
44+
TraceRay(accelStruct, RAY_FLAG_FORCE_OPAQUE, 0xff, 0, 0, 0, rayDesc, payload);
45+
46+
image[int2(LaunchID.xy)] = float4(payload.hitValue, 0.0);
47+
}
48+
49+
[shader("closesthit")]
50+
void closesthitMain(inout Payload p, in Attributes attribs)
51+
{
52+
const float3 barycentricCoords = float3(1.0f - attribs.bary.x - attribs.bary.y, attribs.bary.x, attribs.bary.y);
53+
p.hitValue = barycentricCoords;
54+
}
55+
56+
[shader("miss")]
57+
void missMain(inout Payload p)
58+
{
59+
p.hitValue = float3(0.0, 0.0, 0.2);
60+
}

0 commit comments

Comments
 (0)