Skip to content

Commit 7e5a387

Browse files
committed
Add slang shaders for additional samples
1 parent d0a20a6 commit 7e5a387

5 files changed

Lines changed: 320 additions & 0 deletions

File tree

shaders/slang/_rename.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,19 @@ def checkRenameFiles(samplename):
2424
"raytracingreflections.rmiss.spv": "miss.rmiss.spv",
2525
"raytracingreflections.rgen.spv": "raygen.rgen.spv",
2626
}
27+
case "raytracingshadows":
28+
mappings = {
29+
"raytracingshadows.rchit.spv": "closesthit.rchit.spv",
30+
"raytracingshadows.rmiss.spv": "miss.rmiss.spv",
31+
"raytracingshadows.rgen.spv": "raygen.rgen.spv",
32+
}
33+
case "raytracingintersection":
34+
mappings = {
35+
"raytracingintersection.rchit.spv": "closesthit.rchit.spv",
36+
"raytracingintersection.rmiss.spv": "miss.rmiss.spv",
37+
"raytracingintersection.rgen.spv": "raygen.rgen.spv",
38+
"raytracingintersection.rint.spv": "intersection.rint.spv",
39+
}
2740
case "viewportarray":
2841
mappings = {
2942
"scene.geom.spv": "multiview.geom.spv",
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
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+
float2 UV;
12+
float3 Color;
13+
float4 Tangent;
14+
};
15+
16+
struct VSOutput
17+
{
18+
float4 Pos : SV_POSITION;
19+
float3 Normal;
20+
float3 Color;
21+
float2 UV;
22+
float3 ViewVec;
23+
float3 LightVec;
24+
float4 Tangent;
25+
};
26+
27+
struct UBO
28+
{
29+
float4x4 projection;
30+
float4x4 view;
31+
float4x4 model;
32+
float4 lightPos;
33+
float4 viewPos;
34+
int colorShadingRates;
35+
};
36+
ConstantBuffer<UBO> ubo;
37+
38+
[[vk::binding(0, 1)]] Sampler2D samplerColorMap;
39+
[[vk::binding(1, 1)]] Sampler2D samplerNormalMap;
40+
41+
[[SpecializationConstant]] const bool ALPHA_MASK = false;
42+
[[SpecializationConstant]] const float ALPHA_MASK_CUTOFF = 0.0;
43+
44+
[shader("vertex")]
45+
VSOutput vertexMain(VSInput input)
46+
{
47+
VSOutput output;
48+
output.Normal = input.Normal;
49+
output.Color = input.Color;
50+
output.UV = input.UV;
51+
output.Tangent = input.Tangent;
52+
53+
float4x4 modelView = mul(ubo.view, ubo.model);
54+
55+
output.Pos = mul(ubo.projection, mul(modelView, float4(input.Pos.xyz, 1.0)));
56+
57+
output.Normal = mul((float3x3)ubo.model, input.Normal);
58+
float4 pos = mul(ubo.model, float4(input.Pos, 1.0));
59+
output.LightVec = ubo.lightPos.xyz - pos.xyz;
60+
output.ViewVec = ubo.viewPos.xyz - pos.xyz;
61+
return output;
62+
}
63+
64+
static const uint SHADING_RATE_PER_PIXEL = 0;
65+
static const uint SHADING_RATE_PER_2X1_PIXELS = 6;
66+
static const uint SHADING_RATE_PER_1X2_PIXELS = 7;
67+
static const uint SHADING_RATE_PER_2X2_PIXELS = 8;
68+
static const uint SHADING_RATE_PER_4X2_PIXELS = 9;
69+
static const uint SHADING_RATE_PER_2X4_PIXELS = 10;
70+
71+
[shader("fragment")]
72+
float4 fragmentMain(VSOutput input, uint shadingRate : SV_ShadingRate)
73+
{
74+
float4 color = samplerColorMap.Sample(input.UV) * float4(input.Color, 1.0);
75+
76+
if (ALPHA_MASK) {
77+
if (color.a < ALPHA_MASK_CUTOFF) {
78+
discard;
79+
}
80+
}
81+
82+
float3 N = normalize(input.Normal);
83+
float3 T = normalize(input.Tangent.xyz);
84+
float3 B = cross(input.Normal, input.Tangent.xyz) * input.Tangent.w;
85+
float3x3 TBN = float3x3(T, B, N);
86+
N = mul(normalize(samplerNormalMap.Sample(input.UV).xyz * 2.0 - float3(1.0, 1.0, 1.0)), TBN);
87+
88+
const float ambient = 0.1;
89+
float3 L = normalize(input.LightVec);
90+
float3 V = normalize(input.ViewVec);
91+
float3 R = reflect(-L, N);
92+
float3 diffuse = max(dot(N, L), ambient).rrr;
93+
float3 specular = pow(max(dot(R, V), 0.0), 32.0);
94+
color = float4(diffuse * color.rgb + specular, color.a);
95+
96+
if (ubo.colorShadingRates == 1) {
97+
switch (shadingRate) {
98+
case SHADING_RATE_PER_PIXEL:
99+
return color * float4(0.0, 0.8, 0.4, 1.0);
100+
case SHADING_RATE_PER_2X1_PIXELS:
101+
return color * float4(0.2, 0.6, 1.0, 1.0);
102+
case SHADING_RATE_PER_1X2_PIXELS:
103+
return color * float4(0.0, 0.4, 0.8, 1.0);
104+
case SHADING_RATE_PER_2X2_PIXELS:
105+
return color * float4(1.0, 1.0, 0.2, 1.0);
106+
case SHADING_RATE_PER_4X2_PIXELS:
107+
return color * float4(0.8, 0.8, 0.0, 1.0);
108+
case SHADING_RATE_PER_2X4_PIXELS:
109+
return color * float4(1.0, 0.4, 0.2, 1.0);
110+
default:
111+
return color * float4(0.8, 0.0, 0.0, 1.0);
112+
}
113+
}
114+
115+
return color;
116+
}
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+
struct VSInput
8+
{
9+
float4 Pos;
10+
float3 Normal;
11+
float2 TexCoord;
12+
float3 Color;
13+
};
14+
15+
struct VSOutput
16+
{
17+
float4 Pos : SV_POSITION;
18+
float2 UV;
19+
float3 Normal;
20+
float3 Color;
21+
float3 EyePos;
22+
float3 LightVec;
23+
};
24+
25+
struct UBO
26+
{
27+
float4x4 projection;
28+
float4x4 model;
29+
float4x4 normal;
30+
float4x4 view;
31+
float3 lightpos;
32+
};
33+
ConstantBuffer<UBO> ubo;
34+
35+
[shader("vertex")]
36+
VSOutput vertexMain(VSInput input)
37+
{
38+
VSOutput output;
39+
float4x4 modelView = mul(ubo.view, ubo.model);
40+
float4 pos = mul(modelView, input.Pos);
41+
output.UV = input.TexCoord.xy;
42+
output.Normal = normalize(mul((float3x3)ubo.normal, input.Normal));
43+
output.Color = input.Color;
44+
output.Pos = mul(ubo.projection, pos);
45+
output.EyePos = mul(modelView, pos).xyz;
46+
float4 lightPos = mul(modelView, float4(1.0, 2.0, 0.0, 1.0));
47+
output.LightVec = normalize(lightPos.xyz - output.EyePos);
48+
return output;
49+
}
50+
51+
[shader("fragment")]
52+
float4 fragmentMain(VSOutput input)
53+
{
54+
float3 Eye = normalize(-input.EyePos);
55+
float3 Reflected = normalize(reflect(-input.LightVec, input.Normal));
56+
float4 diff = float4(input.Color, 1.0) * max(dot(input.Normal, input.LightVec), 0.0);
57+
float shininess = 0.0;
58+
float4 spec = float4(1.0, 1.0, 1.0, 1.0) * pow(max(dot(Reflected, Eye), 0.0), 2.5) * shininess;
59+
return float4((diff + spec).rgb, 1);
60+
}
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
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+
float2 TexCoord;
12+
float3 Color;
13+
};
14+
15+
struct VSOutput
16+
{
17+
float4 Pos : SV_POSITION;
18+
float2 UV;
19+
float3 Normal;
20+
float3 Color;
21+
float3 EyePos;
22+
float3 LightVec;
23+
};
24+
25+
struct UBO
26+
{
27+
float4x4 projection;
28+
float4x4 model;
29+
float4x4 normal;
30+
float4x4 view;
31+
float3 lightpos;
32+
};
33+
ConstantBuffer<UBO> ubo;
34+
35+
[shader("vertex")]
36+
VSOutput vertexMain(VSInput input)
37+
{
38+
VSOutput output;
39+
output.UV = input.TexCoord.xy;
40+
output.Normal = normalize(mul((float3x3)ubo.normal, input.Normal));
41+
output.Color = input.Color;
42+
float4x4 modelView = mul(ubo.view, ubo.model);
43+
float4 pos = mul(modelView, input.Pos);
44+
output.Pos = mul(ubo.projection, pos);
45+
output.EyePos = mul(modelView, pos).xyz;
46+
float4 lightPos = mul(modelView, float4(ubo.lightpos, 1.0));
47+
output.LightVec = normalize(lightPos.xyz - output.EyePos);
48+
return output;
49+
}
50+
51+
float specpart(float3 L, float3 N, float3 H)
52+
{
53+
if (dot(N, L) > 0.0)
54+
{
55+
return pow(clamp(dot(H, N), 0.0, 1.0), 64.0);
56+
}
57+
return 0.0;
58+
}
59+
60+
[shader("fragment")]
61+
float4 fragmentMain(VSOutput input)
62+
{
63+
float3 Eye = normalize(-input.EyePos);
64+
float3 Reflected = normalize(reflect(-input.LightVec, input.Normal));
65+
66+
float3 halfVec = normalize(input.LightVec + input.EyePos);
67+
float diff = clamp(dot(input.LightVec, input.Normal), 0.0, 1.0);
68+
float spec = specpart(input.LightVec, input.Normal, halfVec);
69+
float intensity = 0.1 + diff + spec;
70+
71+
float4 IAmbient = float4(0.2, 0.2, 0.2, 1.0);
72+
float4 IDiffuse = float4(0.5, 0.5, 0.5, 0.5) * max(dot(input.Normal, input.LightVec), 0.0);
73+
float shininess = 0.75;
74+
float4 ISpecular = float4(0.5, 0.5, 0.5, 1.0) * pow(max(dot(Reflected, Eye), 0.0), 2.0) * shininess;
75+
76+
float4 outFragColor = float4((IAmbient + IDiffuse) * float4(input.Color, 1.0) + ISpecular);
77+
78+
// Some manual saturation
79+
if (intensity > 0.95)
80+
outFragColor *= 2.25;
81+
if (intensity < 0.15)
82+
outFragColor = float4(0.1, 0.1, 0.1, 0.1);
83+
84+
return outFragColor;
85+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/* Copyright (c) 2025, Sascha Willems
2+
*
3+
* SPDX-License-Identifier: MIT
4+
*
5+
*/
6+
7+
struct VSInput
8+
{
9+
float3 Pos;
10+
};
11+
12+
struct VSOutput
13+
{
14+
float4 Pos : SV_POSITION;
15+
float3 UVW;
16+
};
17+
18+
struct UBO
19+
{
20+
float4x4 projection;
21+
float4x4 model;
22+
float4x4 normal;
23+
float4x4 view;
24+
};
25+
ConstantBuffer<UBO> ubo;
26+
SamplerCube samplerCubeMap;
27+
28+
[shader("vertex")]
29+
VSOutput vertexMain(VSInput input)
30+
{
31+
VSOutput output;
32+
// Remove translation from view matrix
33+
float4x4 viewMat = ubo.view;
34+
viewMat[0][3] = 0.0;
35+
viewMat[1][3] = 0.0;
36+
viewMat[2][3] = 0.0;
37+
output.UVW = input.Pos;
38+
output.Pos = mul(ubo.projection, mul(viewMat, mul(ubo.model, float4(input.Pos.xyz, 1.0))));
39+
return output;
40+
}
41+
42+
[shader("fragment")]
43+
float4 fragmentMain(VSOutput input)
44+
{
45+
return samplerCubeMap.Sample(input.UVW);
46+
}

0 commit comments

Comments
 (0)