Skip to content

Commit b81e9d9

Browse files
committed
Added slang shaders for hdr and graphics pipeline library samples
1 parent 381c6ef commit b81e9d9

5 files changed

Lines changed: 347 additions & 1 deletion

File tree

shaders/slang/_rename.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,11 @@ def checkRenameFiles(samplename):
1616
mappings = {
1717
"normaldebug.vert.spv": "base.vert.spv",
1818
"normaldebug.frag.spv": "base.frag.spv",
19-
}
19+
}
20+
case "graphicspipelinelibrary":
21+
mappings = {
22+
"uber.vert.spv": "shared.vert.spv",
23+
}
2024
case "raytracingbasic":
2125
mappings = {
2226
"raytracingbasic.rchit.spv": "closesthit.rchit.spv",
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
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+
// We use this constant to control the flow of the shader depending on the
32+
// lighting model selected at pipeline creation time
33+
[[SpecializationConstant]] const int LIGHTING_MODEL = 0;
34+
35+
[shader("vertex")]
36+
VSOutput vertexMain(VSInput input)
37+
{
38+
VSOutput output;
39+
output.Color = input.Color;
40+
output.Pos = mul(ubo.projection, mul(ubo.model, float4(input.Pos.xyz, 1.0)));
41+
float4 pos = mul(ubo.model, float4(input.Pos, 1.0));
42+
output.Normal = mul((float3x3)ubo.model, input.Normal);
43+
float3 lPos = mul((float3x3)ubo.model, ubo.lightPos.xyz);
44+
output.LightVec = lPos - pos.xyz;
45+
output.ViewVec = -pos.xyz;
46+
return output;
47+
}
48+
49+
[shader("fragment")]
50+
float4 fragmentMain(VSOutput input)
51+
{
52+
float3 outColor = float3(0.0);
53+
54+
switch (LIGHTING_MODEL) {
55+
case 0: // Phong
56+
{
57+
float3 ambient = input.Color * float3(0.25, 0.25, 0.25);
58+
float3 N = normalize(input.Normal);
59+
float3 L = normalize(input.LightVec);
60+
float3 V = normalize(input.ViewVec);
61+
float3 R = reflect(-L, N);
62+
float3 diffuse = max(dot(N, L), 0.0) * input.Color;
63+
float3 specular = pow(max(dot(R, V), 0.0), 32.0) * float3(0.75);
64+
outColor = ambient + diffuse * 1.75 + specular;
65+
break;
66+
}
67+
case 1: // Toon
68+
{
69+
70+
float3 N = normalize(input.Normal);
71+
float3 L = normalize(input.LightVec);
72+
float intensity = dot(N, L);
73+
float3 color;
74+
if (intensity > 0.98)
75+
color = input.Color * 1.5;
76+
else if (intensity > 0.9)
77+
color = input.Color * 1.0;
78+
else if (intensity > 0.5)
79+
color = input.Color * 0.6;
80+
else if (intensity > 0.25)
81+
color = input.Color * 0.4;
82+
else
83+
color = input.Color * 0.2;
84+
outColor = color;
85+
break;
86+
}
87+
case 2: // No shading
88+
{
89+
outColor = input.Color;
90+
break;
91+
}
92+
case 3: // Greyscale
93+
outColor = dot(input.Color, float3(0.299, 0.587, 0.114));
94+
break;
95+
}
96+
97+
// The scene itself is a bit dark, so brigthen it up a bit
98+
return float4(outColor * 1.25, 1.0);
99+
}

shaders/slang/hdr/bloom.slang

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
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+
Sampler2D samplerColor0;
14+
Sampler2D samplerColor1;
15+
16+
[[SpecializationConstant]] const int dir = 0;
17+
18+
[shader("vertex")]
19+
VSOutput vertexMain(uint VertexIndex: SV_VertexID)
20+
{
21+
VSOutput output;
22+
output.UV = float2((VertexIndex << 1) & 2, VertexIndex & 2);
23+
output.Pos = float4(output.UV * 2.0f - 1.0f, 0.0f, 1.0f);
24+
return output;
25+
}
26+
27+
[shader("fragment")]
28+
float4 fragmentMain(VSOutput input)
29+
{
30+
// From the OpenGL Super bible
31+
const float weights[] = { 0.0024499299678342,
32+
0.0043538453346397,
33+
0.0073599963704157,
34+
0.0118349786570722,
35+
0.0181026699707781,
36+
0.0263392293891488,
37+
0.0364543006660986,
38+
0.0479932050577658,
39+
0.0601029809166942,
40+
0.0715974486241365,
41+
0.0811305381519717,
42+
0.0874493212267511,
43+
0.0896631113333857,
44+
0.0874493212267511,
45+
0.0811305381519717,
46+
0.0715974486241365,
47+
0.0601029809166942,
48+
0.0479932050577658,
49+
0.0364543006660986,
50+
0.0263392293891488,
51+
0.0181026699707781,
52+
0.0118349786570722,
53+
0.0073599963704157,
54+
0.0043538453346397,
55+
0.0024499299678342};
56+
57+
58+
const float blurScale = 0.003;
59+
const float blurStrength = 1.0;
60+
61+
float ar = 1.0;
62+
// Aspect ratio for vertical blur pass
63+
if (dir == 1)
64+
{
65+
float2 ts;
66+
samplerColor1.GetDimensions(ts.x, ts.y);
67+
ar = ts.y / ts.x;
68+
}
69+
70+
float2 P = input.UV.yx - float2(0, (25 >> 1) * ar * blurScale);
71+
72+
float4 color = float4(0.0, 0.0, 0.0, 0.0);
73+
for (int i = 0; i < 25; i++)
74+
{
75+
float2 dv = float2(0.0, i * blurScale) * ar;
76+
color += samplerColor1.Sample(P + dv) * weights[i] * blurStrength;
77+
}
78+
79+
return color;
80+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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+
Sampler2D samplerColor;
14+
15+
[shader("vertex")]
16+
VSOutput vertexMain(uint VertexIndex: SV_VertexID)
17+
{
18+
VSOutput output;
19+
output.UV = float2((VertexIndex << 1) & 2, VertexIndex & 2);
20+
output.Pos = float4(output.UV * 2.0f - 1.0f, 0.0f, 1.0f);
21+
return output;
22+
}
23+
24+
[shader("fragment")]
25+
float4 fragmentMain(VSOutput input)
26+
{
27+
return samplerColor.Sample(input.UV);
28+
}

shaders/slang/hdr/gbuffer.slang

Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
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 : SV_POSITION;
16+
float3 UVW;
17+
float3 WorldPos;
18+
float3 Normal;
19+
float3 ViewVec;
20+
float3 LightVec;
21+
};
22+
23+
struct FSOutput
24+
{
25+
float4 Color0 : SV_TARGET0;
26+
float4 Color1 : SV_TARGET1;
27+
};
28+
29+
struct UBO {
30+
float4x4 projection;
31+
float4x4 modelview;
32+
float4x4 inverseModelview;
33+
float exposure;
34+
};
35+
ConstantBuffer<UBO> ubo;
36+
37+
SamplerCube samplerEnvMap;
38+
39+
[[SpecializationConstant]] const int objectType = 0;
40+
41+
[shader("vertex")]
42+
VSOutput vertexMain(VSInput input)
43+
{
44+
VSOutput output;
45+
output.UVW = input.Pos;
46+
47+
switch (objectType) {
48+
case 0: // Skybox
49+
output.WorldPos = mul((float4x3)ubo.modelview, input.Pos).xyz;
50+
output.Pos = mul(ubo.projection, float4(output.WorldPos, 1.0));
51+
break;
52+
case 1: // Object
53+
output.WorldPos = mul(ubo.modelview, float4(input.Pos, 1.0)).xyz;
54+
output.Pos = mul(ubo.projection, mul(ubo.modelview, float4(input.Pos.xyz, 1.0)));
55+
break;
56+
}
57+
output.WorldPos = mul(ubo.modelview, float4(input.Pos, 1.0)).xyz;
58+
output.Normal = mul((float4x3)ubo.modelview, input.Normal).xyz;
59+
float3 lightPos = float3(0.0f, -5.0f, 5.0f);
60+
output.LightVec = lightPos.xyz - output.WorldPos.xyz;
61+
output.ViewVec = -output.WorldPos.xyz;
62+
return output;
63+
}
64+
65+
[shader("fragment")]
66+
FSOutput fragmentMain(VSOutput input)
67+
{
68+
FSOutput output;
69+
float4 color;
70+
float3 wcNormal;
71+
72+
switch (objectType) {
73+
case 0: // Skybox
74+
{
75+
float3 normal = normalize(input.UVW);
76+
color = samplerEnvMap.Sample(normal);
77+
}
78+
break;
79+
80+
case 1: // Reflect
81+
{
82+
float3 wViewVec = mul((float4x3)ubo.inverseModelview, normalize(input.ViewVec)).xyz;
83+
float3 normal = normalize(input.Normal);
84+
float3 wNormal = mul((float4x3)ubo.inverseModelview, normal).xyz;
85+
86+
float NdotL = max(dot(normal, input.LightVec), 0.0);
87+
88+
float3 eyeDir = normalize(input.ViewVec);
89+
float3 halfVec = normalize(input.LightVec + eyeDir);
90+
float NdotH = max(dot(normal, halfVec), 0.0);
91+
float NdotV = max(dot(normal, eyeDir), 0.0);
92+
float VdotH = max(dot(eyeDir, halfVec), 0.0);
93+
94+
// Geometric attenuation
95+
float NH2 = 2.0 * NdotH;
96+
float g1 = (NH2 * NdotV) / VdotH;
97+
float g2 = (NH2 * NdotL) / VdotH;
98+
float geoAtt = min(1.0, min(g1, g2));
99+
100+
const float F0 = 0.6;
101+
const float k = 0.2;
102+
103+
// Fresnel (schlick approximation)
104+
float fresnel = pow(1.0 - VdotH, 5.0);
105+
fresnel *= (1.0 - F0);
106+
fresnel += F0;
107+
108+
float spec = (fresnel * geoAtt) / (NdotV * NdotL * 3.14);
109+
110+
color = samplerEnvMap.Sample(reflect(-wViewVec, wNormal));
111+
112+
color = float4(color.rgb * NdotL * (k + spec * (1.0 - k)), 1.0);
113+
}
114+
break;
115+
116+
case 2: // Refract
117+
{
118+
float3 wViewVec = mul((float4x3)ubo.inverseModelview, normalize(input.ViewVec)).xyz;
119+
float3 wNormal = mul((float4x3)ubo.inverseModelview, input.Normal).xyz;
120+
color = samplerEnvMap.Sample(refract(-wViewVec, wNormal, 1.0/1.6));
121+
}
122+
break;
123+
}
124+
125+
126+
// Color with manual exposure into attachment 0
127+
output.Color0.rgb = float3(1.0, 1.0, 1.0) - exp(-color.rgb * ubo.exposure);
128+
129+
// Bright parts for bloom into attachment 1
130+
float l = dot(output.Color0.rgb, float3(0.2126, 0.7152, 0.0722));
131+
float threshold = 0.75;
132+
output.Color1.rgb = (l > threshold) ? output.Color0.rgb : float3(0.0, 0.0, 0.0);
133+
output.Color1.a = 1.0;
134+
return output;
135+
}

0 commit comments

Comments
 (0)