Skip to content

Commit d8c8630

Browse files
committed
Added slang shaders for pipeline samples
1 parent b81e9d9 commit d8c8630

4 files changed

Lines changed: 299 additions & 0 deletions

File tree

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
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+
float3 ViewVec;
20+
float3 LightVec;
21+
};
22+
23+
struct UBO
24+
{
25+
float4x4 projection;
26+
float4x4 model;
27+
float4 lightPos;
28+
};
29+
ConstantBuffer<UBO> ubo;
30+
31+
Sampler2D samplerColorMap;
32+
33+
[shader("vertex")]
34+
VSOutput vertexMain(VSInput input)
35+
{
36+
VSOutput output;
37+
output.Color = input.Color;
38+
output.Pos = mul(ubo.projection, mul(ubo.model, float4(input.Pos.xyz, 1.0)));
39+
float4 pos = mul(ubo.model, float4(input.Pos, 1.0));
40+
output.Normal = mul((float3x3)ubo.model, input.Normal);
41+
float3 lPos = mul((float3x3)ubo.model, ubo.lightPos.xyz);
42+
output.LightVec = lPos - pos.xyz;
43+
output.ViewVec = -pos.xyz;
44+
return output;
45+
}
46+
47+
[shader("fragment")]
48+
float4 fragmentMain(VSOutput input)
49+
{
50+
// Desaturate color
51+
float3 color = lerp(input.Color, dot(float3(0.2126,0.7152,0.0722), input.Color).xxx, 0.65);
52+
53+
// High ambient colors because mesh materials are pretty dark
54+
float3 ambient = color * float3(1.0, 1.0, 1.0);
55+
float3 N = normalize(input.Normal);
56+
float3 L = normalize(input.LightVec);
57+
float3 V = normalize(input.ViewVec);
58+
float3 R = reflect(-L, N);
59+
float3 diffuse = max(dot(N, L), 0.0) * color;
60+
float3 specular = pow(max(dot(R, V), 0.0), 32.0) * float3(0.35, 0.35, 0.35);
61+
return float4(ambient + diffuse * 1.75 + specular, 1.0);
62+
}

shaders/slang/pipelines/toon.slang

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
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+
float3 ViewVec;
20+
float3 LightVec;
21+
};
22+
23+
struct UBO
24+
{
25+
float4x4 projection;
26+
float4x4 model;
27+
float4 lightPos;
28+
};
29+
ConstantBuffer<UBO> ubo;
30+
31+
Sampler2D samplerColorMap;
32+
33+
[shader("vertex")]
34+
VSOutput vertexMain(VSInput input)
35+
{
36+
VSOutput output;
37+
output.Color = input.Color;
38+
output.Pos = mul(ubo.projection, mul(ubo.model, float4(input.Pos.xyz, 1.0)));
39+
float4 pos = mul(ubo.model, float4(input.Pos, 1.0));
40+
output.Normal = mul((float3x3)ubo.model, input.Normal);
41+
float3 lPos = mul((float3x3)ubo.model, ubo.lightPos.xyz);
42+
output.LightVec = lPos - pos.xyz;
43+
output.ViewVec = -pos.xyz;
44+
return output;
45+
}
46+
47+
[shader("fragment")]
48+
float4 fragmentMain(VSOutput input)
49+
{
50+
// Desaturate color
51+
float3 color = lerp(input.Color, dot(float3(0.2126,0.7152,0.0722), input.Color).xxx, 0.65);
52+
53+
// High ambient colors because mesh materials are pretty dark
54+
float3 ambient = color * float3(1.0, 1.0, 1.0);
55+
float3 N = normalize(input.Normal);
56+
float3 L = normalize(input.LightVec);
57+
float3 V = normalize(input.ViewVec);
58+
float3 R = reflect(-L, N);
59+
float3 diffuse = max(dot(N, L), 0.0) * color;
60+
float3 specular = pow(max(dot(R, V), 0.0), 16.0) * float3(0.75, 0.75, 0.75);
61+
62+
float intensity = dot(N,L);
63+
float shade = 1.0;
64+
shade = intensity < 0.5 ? 0.75 : shade;
65+
shade = intensity < 0.35 ? 0.6 : shade;
66+
shade = intensity < 0.25 ? 0.5 : shade;
67+
shade = intensity < 0.1 ? 0.25 : shade;
68+
69+
return float4(input.Color * 3.0 * shade, 1);
70+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/* Copyright (c) 2025, Sascha Willems
2+
*
3+
* SPDX-License-Identifier: MIT
4+
*
5+
*/
6+
7+
struct VSInput
8+
{
9+
float4 Pos;
10+
float2 UV;
11+
float3 Color;
12+
};
13+
14+
struct VSOutput
15+
{
16+
float4 Pos : SV_POSITION;
17+
float3 Color;
18+
};
19+
20+
struct UBO
21+
{
22+
float4x4 projection;
23+
float4x4 model;
24+
};
25+
ConstantBuffer<UBO> ubo;
26+
27+
[shader("vertex")]
28+
VSOutput vertexMain(VSInput input)
29+
{
30+
VSOutput output;
31+
output.Color = input.Color;
32+
output.Pos = mul(ubo.projection, mul(ubo.model, input.Pos));
33+
return output;
34+
}
35+
36+
[shader("fragment")]
37+
float4 fragmentMain(VSOutput input)
38+
{
39+
return float4(input.Color * 1.5, 1);
40+
}
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
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+
float3 ViewVec;
20+
float3 LightVec;
21+
};
22+
23+
struct HSOutput
24+
{
25+
float4 Pos : SV_POSITION;
26+
float3 Normal;
27+
float3 Color;
28+
float3 ViewVec;
29+
float3 LightVec;
30+
};
31+
32+
struct DSOutput
33+
{
34+
float4 Pos : SV_POSITION;
35+
float3 Normal;
36+
float3 Color;
37+
float3 ViewVec;
38+
float3 LightVec;
39+
};
40+
41+
struct ConstantsHSOutput
42+
{
43+
float TessLevelOuter[3] : SV_TessFactor;
44+
float TessLevelInner[2] : SV_InsideTessFactor;
45+
};
46+
47+
struct UBO
48+
{
49+
float4x4 projection;
50+
float4x4 modelview;
51+
float4 lightPos;
52+
};
53+
ConstantBuffer<UBO> ubo;
54+
55+
struct PushConsts {
56+
float3 objPos;
57+
};
58+
[[vk::push_constant]] PushConsts pushConsts;
59+
60+
ConstantsHSOutput ConstantsHS(InputPatch<VSOutput, 3> patch)
61+
{
62+
ConstantsHSOutput output;
63+
output.TessLevelInner[0] = 2.0;
64+
output.TessLevelInner[1] = 2.0;
65+
output.TessLevelOuter[0] = 1.0;
66+
output.TessLevelOuter[1] = 1.0;
67+
output.TessLevelOuter[2] = 1.0;
68+
return output;
69+
}
70+
71+
[shader("hull")]
72+
[domain("tri")]
73+
[partitioning("integer")]
74+
[outputtopology("triangle_ccw")]
75+
[outputcontrolpoints(3)]
76+
[patchconstantfunc("ConstantsHS")]
77+
[maxtessfactor(20.0f)]
78+
HSOutput hullMain(InputPatch<VSOutput, 3> patch, uint InvocationID: SV_OutputControlPointID)
79+
{
80+
HSOutput output;
81+
output.Pos = patch[InvocationID].Pos;
82+
output.Normal = patch[InvocationID].Normal;
83+
output.Color = patch[InvocationID].Color;
84+
output.ViewVec = patch[InvocationID].ViewVec;
85+
output.LightVec = patch[InvocationID].LightVec;
86+
return output;
87+
}
88+
89+
[shader("domain")]
90+
[domain("tri")]
91+
DSOutput domainMain(ConstantsHSOutput input, float3 TessCoord: SV_DomainLocation, const OutputPatch<HSOutput, 3> patch)
92+
{
93+
DSOutput output;
94+
output.Pos = (TessCoord.x * patch[2].Pos) + (TessCoord.y * patch[1].Pos) + (TessCoord.z * patch[0].Pos);
95+
output.Normal = TessCoord.x * patch[2].Normal + TessCoord.y * patch[1].Normal + TessCoord.z * patch[0].Normal;
96+
output.ViewVec = TessCoord.x * patch[2].ViewVec + TessCoord.y * patch[1].ViewVec + TessCoord.z * patch[0].ViewVec;
97+
output.LightVec = TessCoord.x * patch[2].LightVec + TessCoord.y * patch[1].LightVec + TessCoord.z * patch[0].LightVec;
98+
output.Color = patch[0].Color;
99+
return output;
100+
}
101+
102+
[shader("vertex")]
103+
VSOutput vertexMain(VSInput input)
104+
{
105+
VSOutput output;
106+
output.Color = input.Color;
107+
float3 locPos = mul(ubo.modelview, float4(input.Pos, 1.0)).xyz;
108+
float3 worldPos = mul(ubo.modelview, float4(input.Pos + pushConsts.objPos, 1.0)).xyz;
109+
output.Pos = mul(ubo.projection, float4(worldPos, 1.0));
110+
float4 pos = mul(ubo.modelview, float4(worldPos, 1.0));
111+
output.Normal = mul((float3x3)ubo.modelview, input.Normal);
112+
output.LightVec = ubo.lightPos.xyz - pos.xyz;
113+
output.ViewVec = -pos.xyz;
114+
return output;
115+
}
116+
117+
[shader("fragment")]
118+
float4 fragmentMain(VSOutput input)
119+
{
120+
float3 N = normalize(input.Normal);
121+
float3 L = normalize(input.LightVec);
122+
float3 V = normalize(input.ViewVec);
123+
float3 R = reflect(-L, N);
124+
float3 diffuse = max(dot(N, L), 0.0) * input.Color;
125+
float3 specular = pow(max(dot(R, V), 0.0), 8.0) * float3(0.75, 0.75, 0.75);
126+
return float4(diffuse + specular, 0.5);
127+
}

0 commit comments

Comments
 (0)