Skip to content

Commit 0e97506

Browse files
committed
Add slang shaders for additional samples
1 parent e0bff55 commit 0e97506

7 files changed

Lines changed: 418 additions & 0 deletions

File tree

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
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+
};
14+
15+
struct VSOutput
16+
{
17+
float4 Pos : SV_POSITION;
18+
float3 Normal;
19+
float3 Color;
20+
float2 UV;
21+
float3 ViewVec;
22+
float3 LightVec;
23+
};
24+
25+
struct UBO
26+
{
27+
float4x4 projection;
28+
float4x4 model;
29+
float4 lightPos;
30+
};
31+
ConstantBuffer<UBO> ubo;
32+
33+
[[vk::binding(0,1)]] Sampler2D samplerColorMap;
34+
35+
[shader("vertex")]
36+
VSOutput vertexMain(VSInput input)
37+
{
38+
VSOutput output;
39+
output.Normal = input.Normal;
40+
output.Color = input.Color;
41+
output.UV = input.UV;
42+
output.Pos = mul(ubo.projection, mul(ubo.model, float4(input.Pos.xyz, 1.0)));
43+
44+
float4 pos = mul(ubo.model, float4(input.Pos, 0.0));
45+
output.Normal = mul((float3x3)ubo.model, input.Normal);
46+
float3 lPos = mul((float3x3)ubo.model, ubo.lightPos.xyz);
47+
output.LightVec = lPos - input.Pos;
48+
output.ViewVec = -input.Pos;
49+
return output;
50+
}
51+
52+
[shader("fragment")]
53+
float4 fragmentMain(VSOutput input)
54+
{
55+
float4 color = samplerColorMap.Sample(input.UV) * float4(input.Color, 1.0);
56+
float3 N = normalize(input.Normal);
57+
float3 L = normalize(input.LightVec);
58+
float3 V = normalize(input.ViewVec);
59+
float3 R = reflect(-L, N);
60+
float3 diffuse = max(dot(N, L), 0.15) * input.Color;
61+
float3 specular = pow(max(dot(R, V), 0.0), 16.0) * float3(0.75, 0.75, 0.75);
62+
return float4(diffuse * color.rgb + specular, 1.0);
63+
}
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+
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[2];
26+
float4x4 modelview[2];
27+
float4 lightPos;
28+
};
29+
ConstantBuffer<UBO> ubo;
30+
31+
[shader("vertex")]
32+
VSOutput vertexMain(VSInput input, uint ViewIndex: SV_ViewID)
33+
{
34+
VSOutput output;
35+
output.Color = input.Color;
36+
output.Normal = mul((float3x3)ubo.modelview[ViewIndex], input.Normal);
37+
38+
float4 pos = float4(input.Pos.xyz, 1.0);
39+
float4 worldPos = mul(ubo.modelview[ViewIndex], pos);
40+
41+
float3 lPos = mul(ubo.modelview[ViewIndex], ubo.lightPos).xyz;
42+
output.LightVec = lPos - worldPos.xyz;
43+
output.ViewVec = -worldPos.xyz;
44+
45+
output.Pos = mul(ubo.projection[ViewIndex], worldPos);
46+
return output;
47+
}
48+
49+
[shader("fragment")]
50+
float4 fragmentMain(VSOutput input)
51+
{
52+
float3 N = normalize(input.Normal);
53+
float3 L = normalize(input.LightVec);
54+
float3 V = normalize(input.ViewVec);
55+
float3 R = reflect(-L, N);
56+
float3 ambient = float3(0.1, 0.1, 0.1);
57+
float3 diffuse = max(dot(N, L), 0.0) * float3(1.0, 1.0, 1.0);
58+
float3 specular = pow(max(dot(R, V), 0.0), 16.0) * float3(0.75, 0.75, 0.75);
59+
return float4((ambient + diffuse) * input.Color.rgb + specular, 1.0);
60+
}
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 VSOutput
8+
{
9+
float4 Pos : SV_POSITION;
10+
float2 UV;
11+
};
12+
13+
struct UBO
14+
{
15+
float4x4 projection[2];
16+
float4x4 modelview[2];
17+
float4 lightPos;
18+
float distortionAlpha;
19+
};
20+
ConstantBuffer<UBO> ubo;
21+
22+
Sampler2DArray samplerView;
23+
24+
[[SpecializationConstant]] const float VIEW_LAYER = 0.0f;
25+
26+
[shader("vertex")]
27+
VSOutput vertexMain(uint VertexIndex: SV_VertexID)
28+
{
29+
VSOutput output;
30+
output.UV = float2((VertexIndex << 1) & 2, VertexIndex & 2);
31+
output.Pos = float4(output.UV * 2.0f - 1.0f, 0.0f, 1.0f);
32+
return output;
33+
}
34+
35+
[shader("fragment")]
36+
float4 fragmentMain(VSOutput input)
37+
{
38+
const float alpha = ubo.distortionAlpha;
39+
40+
float2 p1 = float2(2.0 * input.UV - 1.0);
41+
float2 p2 = p1 / (1.0 - alpha * length(p1));
42+
p2 = (p2 + 1.0) * 0.5;
43+
44+
bool inside = ((p2.x >= 0.0) && (p2.x <= 1.0) && (p2.y >= 0.0) && (p2.y <= 1.0));
45+
return inside ? samplerView.Sample(float3(p2, VIEW_LAYER)) : float4(0.0, 0.0, 0.0, 0.0);
46+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// Copyright 2020 Google LLC
2+
3+
struct VSInput
4+
{
5+
float3 Pos;
6+
float2 UV;
7+
};
8+
9+
struct VSOutput
10+
{
11+
float4 Pos : SV_POSITION;
12+
float2 UV;
13+
};
14+
15+
Sampler2D samplerColor;
16+
17+
[shader("vertex")]
18+
VSOutput vertexMain(VSInput input)
19+
{
20+
VSOutput output;
21+
output.UV = input.UV;
22+
output.Pos = float4(input.Pos, 1.0f);
23+
return output;
24+
}
25+
26+
[shader("fragment")]
27+
float4 fragmentMain(VSOutput input)
28+
{
29+
return samplerColor.Sample(input.UV);
30+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
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+
};
14+
15+
struct VSOutput
16+
{
17+
float4 Pos : SV_POSITION;
18+
float3 Normal;
19+
float3 Color;
20+
float2 UV;
21+
};
22+
23+
struct UBOCamera {
24+
float4x4 projection;
25+
float4x4 view;
26+
};
27+
ConstantBuffer<UBOCamera> uboCamera;
28+
29+
struct UBOModel {
30+
float4x4 local;
31+
};
32+
ConstantBuffer<UBOModel> uboModel;
33+
34+
Sampler2D samplerColorMap;
35+
36+
[shader("vertex")]
37+
VSOutput vertexMain(VSInput input)
38+
{
39+
VSOutput output;
40+
output.Normal = input.Normal;
41+
output.Color = input.Color;
42+
output.UV = input.UV;
43+
output.Pos = mul(uboCamera.projection, mul(uboCamera.view, mul(uboModel.local, float4(input.Pos.xyz, 1.0))));
44+
return output;
45+
}
46+
47+
[shader("fragment")]
48+
float4 fragmentMain(VSOutput input)
49+
{
50+
return samplerColorMap.Sample(input.UV) * float4(input.Color, 1.0);
51+
}
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
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+
};
14+
15+
struct VSOutput
16+
{
17+
float4 Pos : SV_POSITION;
18+
float3 Normal;
19+
float3 Color;
20+
float2 UV;
21+
float3 ViewVec;
22+
float3 LightVec;
23+
};
24+
25+
struct UBO
26+
{
27+
float4x4 projection;
28+
float4x4 model;
29+
float4 lightPos;
30+
};
31+
ConstantBuffer<UBO> ubo;
32+
33+
Sampler2D samplerColormap;
34+
Sampler2D samplerDiscard;
35+
36+
// We use this constant to control the flow of the shader depending on the
37+
// lighting model selected at pipeline creation time
38+
[[SpecializationConstant]] const int LIGHTING_MODEL = 0;
39+
// Parameter for the toon shading part of the shader
40+
[[SpecializationConstant]] const int PARAM_TOON_DESATURATION = 0;
41+
42+
[shader("vertex")]
43+
VSOutput vertexMain(VSInput input)
44+
{
45+
VSOutput output;
46+
output.Normal = input.Normal;
47+
output.Color = input.Color;
48+
output.UV = input.UV;
49+
output.Pos = mul(ubo.projection, mul(ubo.model, float4(input.Pos.xyz, 1.0)));
50+
51+
float4 pos = mul(ubo.model, float4(input.Pos, 1.0));
52+
output.Normal = mul((float3x3)ubo.model, input.Normal);
53+
float3 lPos = mul((float3x3)ubo.model, ubo.lightPos.xyz);
54+
output.LightVec = lPos - pos.xyz;
55+
output.ViewVec = -pos.xyz;
56+
return output;
57+
}
58+
59+
[shader("fragment")]
60+
float4 fragmentMain(VSOutput input) : SV_TARGET
61+
{
62+
switch (LIGHTING_MODEL) {
63+
case 0: // Phong
64+
{
65+
float3 ambient = input.Color * float3(0.25, 0.25, 0.25);
66+
float3 N = normalize(input.Normal);
67+
float3 L = normalize(input.LightVec);
68+
float3 V = normalize(input.ViewVec);
69+
float3 R = reflect(-L, N);
70+
float3 diffuse = max(dot(N, L), 0.0) * input.Color;
71+
float3 specular = pow(max(dot(R, V), 0.0), 32.0) * float3(0.75, 0.75, 0.75);
72+
return float4(ambient + diffuse * 1.75 + specular, 1.0);
73+
}
74+
case 1: // Toon
75+
{
76+
77+
float3 N = normalize(input.Normal);
78+
float3 L = normalize(input.LightVec);
79+
float intensity = dot(N,L);
80+
float3 color;
81+
if (intensity > 0.98)
82+
color = input.Color * 1.5;
83+
else if (intensity > 0.9)
84+
color = input.Color * 1.0;
85+
else if (intensity > 0.5)
86+
color = input.Color * 0.6;
87+
else if (intensity > 0.25)
88+
color = input.Color * 0.4;
89+
else
90+
color = input.Color * 0.2;
91+
// Desaturate a bit
92+
color = float3(lerp(color, dot(float3(0.2126,0.7152,0.0722), color).xxx, asfloat(PARAM_TOON_DESATURATION)));
93+
return float4(color, 1);
94+
}
95+
case 2: // Textured
96+
{
97+
float4 color = samplerColormap.Sample(input.UV).rrra;
98+
float3 ambient = color.rgb * float3(0.25, 0.25, 0.25) * input.Color;
99+
float3 N = normalize(input.Normal);
100+
float3 L = normalize(input.LightVec);
101+
float3 V = normalize(input.ViewVec);
102+
float3 R = reflect(-L, N);
103+
float3 diffuse = max(dot(N, L), 0.0) * color.rgb;
104+
float specular = pow(max(dot(R, V), 0.0), 32.0) * color.a;
105+
return float4(ambient + diffuse + specular.xxx, 1.0);
106+
}
107+
}
108+
109+
return float4(0, 0, 0, 0);
110+
}

0 commit comments

Comments
 (0)