Skip to content

Commit 33cab71

Browse files
committed
Add slang shader for mesh shader sample
1 parent 5484d51 commit 33cab71

2 files changed

Lines changed: 82 additions & 0 deletions

File tree

shaders/slang/compileshaders.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,10 @@ def getShaderStages(filename):
4848
stages.append("callable")
4949
if '[shader("compute")]' in filecontent:
5050
stages.append("compute")
51+
if '[shader("amplification")]' in filecontent:
52+
stages.append("amplification")
53+
if '[shader("mesh")]' in filecontent:
54+
stages.append("mesh")
5155
f.close()
5256
return stages
5357

@@ -58,6 +62,9 @@ def getShaderStages(filename):
5862
compile_single_sample = ""
5963
if args.sample != None:
6064
compile_single_sample = args.sample
65+
if (not os.path.isdir(compile_single_sample)):
66+
print("ERROR: No directory found with name %s" % compile_single_sample)
67+
exit(-1)
6168

6269
dir_path = os.path.dirname(os.path.realpath(__file__))
6370
dir_path = dir_path.replace('\\', '/')
@@ -91,6 +98,10 @@ def getShaderStages(filename):
9198
output_ext = ".rcall"
9299
case "compute":
93100
output_ext = ".comp"
101+
case "mesh":
102+
output_ext = ".mesh"
103+
case "amplification":
104+
output_ext = ".task"
94105
output_file = input_file + output_ext + ".spv"
95106
output_file = output_file.replace(".slang", "")
96107
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)
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/* Copyright (c) 2025, Sascha Willems
2+
*
3+
* SPDX-License-Identifier: MIT
4+
*
5+
*/
6+
7+
struct VertexOutput
8+
{
9+
float4 position : SV_Position;
10+
float4 color;
11+
};
12+
13+
struct UBO
14+
{
15+
float4x4 projection;
16+
float4x4 model;
17+
float4x4 view;
18+
};
19+
ConstantBuffer<UBO> ubo;
20+
21+
static const float4 positions[3] = {
22+
float4( 0.0, -1.0, 0.0, 1.0),
23+
float4(-1.0, 1.0, 0.0, 1.0),
24+
float4( 1.0, 1.0, 0.0, 1.0)
25+
};
26+
27+
static const float4 colors[3] = {
28+
float4(0.0, 1.0, 0.0, 1.0),
29+
float4(0.0, 0.0, 1.0, 1.0),
30+
float4(1.0, 0.0, 0.0, 1.0)
31+
};
32+
33+
struct DummyPayLoad
34+
{
35+
uint dummyData;
36+
};
37+
38+
// We don't use pay loads in this sample, but the fn call requires one
39+
groupshared DummyPayLoad dummyPayLoad;
40+
41+
[shader("amplification")]
42+
[numthreads(1, 1, 1)]
43+
void amplificationMain()
44+
{
45+
DispatchMesh(3, 1, 1, dummyPayLoad);
46+
}
47+
48+
[shader("mesh")]
49+
[outputtopology("triangle")]
50+
[numthreads(1, 1, 1)]
51+
void meshMain(out indices uint3 triangles[1], out vertices VertexOutput vertices[3], uint3 DispatchThreadID : SV_DispatchThreadID)
52+
{
53+
float4x4 mvp = mul(ubo.projection, mul(ubo.view, ubo.model));
54+
55+
float4 offset = float4(0.0, 0.0, float(DispatchThreadID.x), 0.0);
56+
57+
SetMeshOutputCounts(3, 1);
58+
for (uint i = 0; i < 3; i++) {
59+
vertices[i].position = mul(mvp, positions[i] + offset);
60+
vertices[i].color = colors[i];
61+
}
62+
63+
SetMeshOutputCounts(3, 1);
64+
triangles[0] = uint3(0, 1, 2);
65+
}
66+
67+
[shader("fragment")]
68+
float4 fragmentMain(VertexOutput input)
69+
{
70+
return input.color;
71+
}

0 commit comments

Comments
 (0)