Skip to content

Commit 0a6c03b

Browse files
committed
Add slang shaders for additional samples
1 parent 4df49db commit 0a6c03b

5 files changed

Lines changed: 235 additions & 6 deletions

File tree

shaders/slang/_rename.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@
77
def checkRenameFiles(samplename):
88
mappings = {}
99
match samplename:
10+
case "geometryshader":
11+
mappings = {
12+
"normaldebug.vert.spv": "base.vert.spv",
13+
"normaldebug.frag.spv": "base.frag.spv",
14+
}
1015
case "raytracingbasic":
1116
mappings = {
1217
"raytracingbasic.rchit.spv": "closesthit.rchit.spv",
@@ -18,6 +23,10 @@ def checkRenameFiles(samplename):
1823
"raytracingreflections.rchit.spv": "closesthit.rchit.spv",
1924
"raytracingreflections.rmiss.spv": "miss.rmiss.spv",
2025
"raytracingreflections.rgen.spv": "raygen.rgen.spv",
21-
}
26+
}
27+
case "viewportarray":
28+
mappings = {
29+
"scene.geom.spv": "multiview.geom.spv",
30+
}
2231
for x, y in mappings.items():
2332
move(samplename + "\\" + x, samplename + "\\" + y)

shaders/slang/compileshaders.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,13 +46,15 @@ def getShaderStages(filename):
4646
if '[shader("closesthit")]' in filecontent:
4747
stages.append("closesthit")
4848
if '[shader("callable")]' in filecontent:
49-
stages.append("callable")
49+
stages.append("callable")
5050
if '[shader("compute")]' in filecontent:
51-
stages.append("compute")
51+
stages.append("compute")
5252
if '[shader("amplification")]' in filecontent:
53-
stages.append("amplification")
53+
stages.append("amplification")
5454
if '[shader("mesh")]' in filecontent:
55-
stages.append("mesh")
55+
stages.append("mesh")
56+
if '[shader("geometry")]' in filecontent:
57+
stages.append("geometry")
5658
f.close()
5759
return stages
5860

@@ -64,7 +66,7 @@ def getShaderStages(filename):
6466
if args.sample != None:
6567
compile_single_sample = args.sample
6668
if (not os.path.isdir(compile_single_sample)):
67-
print("ERROR: No directory found with name %s" % compile_single_sample)
69+
print("ERROR: No directory found with name %s" % compile_single_sample)
6870
exit(-1)
6971

7072
dir_path = os.path.dirname(os.path.realpath(__file__))
@@ -105,6 +107,8 @@ def getShaderStages(filename):
105107
output_ext = ".mesh"
106108
case "amplification":
107109
output_ext = ".task"
110+
case "geometry":
111+
output_ext = ".geom"
108112
output_file = output_base_file_name + output_ext + ".spv"
109113
output_file = output_file.replace(".slang", "")
110114
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: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/* Copyright (c) 2025, Sascha Willems
2+
*
3+
* SPDX-License-Identifier: MIT
4+
*
5+
*/
6+
7+
struct VSInput
8+
{
9+
float4 Pos;
10+
float3 Normal;
11+
float3 Color;
12+
};
13+
14+
struct VSOutput
15+
{
16+
float4 Pos : SV_POSITION;
17+
float3 Normal;
18+
float3 Color;
19+
float3 ViewVec;
20+
float3 LightVec;
21+
};
22+
23+
struct UBO
24+
{
25+
float4x4 projection;
26+
float4x4 model;
27+
};
28+
ConstantBuffer<UBO> ubo;
29+
30+
[shader("vertex")]
31+
VSOutput vertexMain(VSInput input)
32+
{
33+
VSOutput output;
34+
output.Normal = input.Normal;
35+
output.Color = input.Color;
36+
output.Pos = mul(ubo.projection, mul(ubo.model, input.Pos));
37+
38+
float4 pos = mul(ubo.model, float4(input.Pos.xyz, 1.0));
39+
output.Normal = mul((float4x3)ubo.model, input.Normal).xyz;
40+
41+
float3 lightPos = float3(1.0f, -1.0f, 1.0f);
42+
output.LightVec = lightPos.xyz - pos.xyz;
43+
output.ViewVec = -pos.xyz;
44+
return output;
45+
}
46+
47+
[shader("fragment")]
48+
float4 fragmentMain(VSOutput input)
49+
{
50+
float3 N = normalize(input.Normal);
51+
float3 L = normalize(input.LightVec);
52+
float3 V = normalize(input.ViewVec);
53+
float3 R = reflect(-L, N);
54+
float3 ambient = float3(0.1, 0.1, 0.1);
55+
float3 diffuse = max(dot(N, L), 0.0) * float3(1.0, 1.0, 1.0);
56+
float3 specular = pow(max(dot(R, V), 0.0), 16.0) * float3(0.75, 0.75, 0.75);
57+
return float4((ambient + diffuse) * input.Color.rgb + specular, 1.0);
58+
}
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+
struct VSInput
8+
{
9+
float3 Pos;
10+
float3 Normal;
11+
};
12+
13+
struct VSOutput
14+
{
15+
float4 Pos : POSITION0;
16+
float3 Normal;
17+
};
18+
19+
struct GSOutput
20+
{
21+
float4 Pos : SV_POSITION;
22+
float3 Color;
23+
};
24+
25+
struct UBO
26+
{
27+
float4x4 projection;
28+
float4x4 model;
29+
};
30+
ConstantBuffer<UBO> ubo;
31+
32+
[shader("vertex")]
33+
VSOutput vertexMain(VSInput input)
34+
{
35+
VSOutput output;
36+
output.Normal = input.Normal;
37+
output.Pos = float4(input.Pos.xyz, 1.0);
38+
return output;
39+
}
40+
41+
[shader("geometry")]
42+
[maxvertexcount(6)]
43+
void geometryMain(triangle VSOutput input[3], inout LineStream<GSOutput> outStream)
44+
{
45+
float normalLength = 0.02;
46+
for(int i=0; i<3; i++)
47+
{
48+
float3 pos = input[i].Pos.xyz;
49+
float3 normal = input[i].Normal.xyz;
50+
51+
GSOutput output = (GSOutput)0;
52+
output.Pos = mul(ubo.projection, mul(ubo.model, float4(pos, 1.0)));
53+
output.Color = float3(1.0, 0.0, 0.0);
54+
outStream.Append( output );
55+
56+
output.Pos = mul(ubo.projection, mul(ubo.model, float4(pos + normal * normalLength, 1.0)));
57+
output.Color = float3(0.0, 0.0, 1.0);
58+
outStream.Append( output );
59+
60+
outStream.RestartStrip();
61+
}
62+
}
63+
64+
[shader("fragment")]
65+
float4 fragmentMain(GSOutput input)
66+
{
67+
return float4(input.Color, 1.0);
68+
}
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
/* Copyright (c) 2025, Sascha Willems
2+
*
3+
* SPDX-License-Identifier: MIT
4+
*
5+
*/
6+
7+
struct VSInput
8+
{
9+
float3 Pos;
10+
float3 Normal;
11+
float3 Color;
12+
};
13+
14+
struct VSOutput
15+
{
16+
float4 Pos : SV_POSITION;
17+
float3 Normal;
18+
float3 Color;
19+
};
20+
21+
struct GSOutput
22+
{
23+
float4 Pos : SV_POSITION;
24+
uint ViewportIndex : SV_ViewportArrayIndex;
25+
uint PrimitiveID : SV_PrimitiveID;
26+
float3 Normal;
27+
float3 Color;
28+
float3 ViewVec;
29+
float3 LightVec;
30+
}
31+
32+
struct UBO
33+
{
34+
float4x4 projection[2];
35+
float4x4 modelview[2];
36+
float4 lightPos;
37+
};
38+
ConstantBuffer<UBO> ubo;
39+
40+
[shader("vertex")]
41+
VSOutput vertexMain(VSInput input)
42+
{
43+
VSOutput output;
44+
output.Color = input.Color;
45+
output.Normal = input.Normal;
46+
output.Pos = float4(input.Pos.xyz, 1.0);
47+
return output;
48+
}
49+
50+
[shader("geometry")]
51+
[maxvertexcount(3)]
52+
[instance(2)]
53+
void geometryMain(triangle VSOutput input[3], inout TriangleStream<GSOutput> outStream, uint InvocationID: SV_GSInstanceID, uint PrimitiveID: SV_PrimitiveID)
54+
{
55+
for (int i = 0; i < 3; i++)
56+
{
57+
GSOutput output;
58+
output.Normal = mul((float3x3)ubo.modelview[InvocationID], input[i].Normal);
59+
output.Color = input[i].Color;
60+
61+
float4 pos = input[i].Pos;
62+
float4 worldPos = mul(ubo.modelview[InvocationID], pos);
63+
64+
float3 lPos = mul(ubo.modelview[InvocationID], ubo.lightPos).xyz;
65+
output.LightVec = lPos - worldPos.xyz;
66+
output.ViewVec = -worldPos.xyz;
67+
68+
output.Pos = mul(ubo.projection[InvocationID], worldPos);
69+
70+
// Set the viewport index that the vertex will be emitted to
71+
output.ViewportIndex = InvocationID;
72+
output.PrimitiveID = PrimitiveID;
73+
outStream.Append(output);
74+
}
75+
76+
outStream.RestartStrip();
77+
}
78+
79+
[shader("fragment")]
80+
float4 fragmentMain(GSOutput input)
81+
{
82+
float3 N = normalize(input.Normal);
83+
float3 L = normalize(input.LightVec);
84+
float3 V = normalize(input.ViewVec);
85+
float3 R = reflect(-L, N);
86+
float3 ambient = float3(0.1, 0.1, 0.1);
87+
float3 diffuse = max(dot(N, L), 0.0) * float3(1.0, 1.0, 1.0);
88+
float3 specular = pow(max(dot(R, V), 0.0), 16.0) * float3(0.75, 0.75, 0.75);
89+
return float4((ambient + diffuse) * input.Color.rgb + specular, 1.0);
90+
}

0 commit comments

Comments
 (0)