Skip to content

Commit b3c032e

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

18 files changed

Lines changed: 1425 additions & 0 deletions
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
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 view;
27+
float4x4 model;
28+
};
29+
ConstantBuffer<UBO> ubo;
30+
31+
struct Node
32+
{
33+
float4x4 transform;
34+
};
35+
[[vk::binding(0,1)]] ConstantBuffer<Node> node;
36+
37+
[shader("vertex")]
38+
VSOutput vertexMain(VSInput input, uniform float4 baseColorFactor)
39+
{
40+
VSOutput output;
41+
output.Normal = input.Normal;
42+
output.Color = baseColorFactor.rgb;
43+
float4 pos = float4(input.Pos, 1.0);
44+
output.Pos = mul(ubo.projection, mul(ubo.view, mul(ubo.model, mul(node.transform, pos))));
45+
46+
output.Normal = mul((float4x3)mul(ubo.view, mul(ubo.model, node.transform)), input.Normal).xyz;
47+
48+
float4 localpos = mul(ubo.view, mul(ubo.model, mul(node.transform, pos)));
49+
float3 lightPos = float3(10.0f, -10.0f, 10.0f);
50+
output.LightVec = lightPos.xyz - localpos.xyz;
51+
output.ViewVec = -localpos.xyz;
52+
return output;
53+
}
54+
55+
[shader("fragment")]
56+
float4 fragmentMain(VSOutput input)
57+
{
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 ambient = float3(0.1, 0.1, 0.1);
63+
float3 diffuse = max(dot(N, L), 0.0) * float3(1.0, 1.0, 1.0);
64+
float3 specular = pow(max(dot(R, V), 0.0), 16.0) * float3(0.75, 0.75, 0.75);
65+
return float4((ambient + diffuse) * input.Color.rgb + specular, 1.0);
66+
}
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+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/* Copyright (c) 2025, Sascha Willems
2+
*
3+
* SPDX-License-Identifier: MIT
4+
*
5+
*/
6+
struct VSInput
7+
{
8+
float3 Pos;
9+
float3 Color;
10+
};
11+
12+
struct VSOutput
13+
{
14+
float4 Pos : SV_POSITION;
15+
float3 Color;
16+
};
17+
18+
struct UBO
19+
{
20+
float4x4 projection;
21+
float4x4 model;
22+
};
23+
ConstantBuffer<UBO> ubo;
24+
25+
[shader("vertex")]
26+
VSOutput vertexMain(VSInput input)
27+
{
28+
VSOutput output;
29+
output.Color = input.Color;
30+
output.Pos = mul(ubo.projection, mul(ubo.model, float4(input.Pos, 1.0)));
31+
return output;
32+
}
33+
34+
[shader("fragment")]
35+
float4 fragmentMain(VSOutput input)
36+
{
37+
return float4(input.Color, 1);
38+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/* Copyright (c) 2025, Sascha Willems
2+
*
3+
* SPDX-License-Identifier: MIT
4+
*
5+
*/
6+
7+
[shader("fragment")]
8+
float4 fragmentMain()
9+
{
10+
return float4(1.0, 1.0, 1.0, 1.0);
11+
}
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+
[[vk::binding(0, 1)]] ConstantBuffer<UBOModel> uboModel;
33+
34+
[[vk::binding(0, 2)]] 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+
}

shaders/slang/pbrbasic/pbr.slang

Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
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 WorldPos;
17+
float3 Normal;
18+
};
19+
20+
struct UBO
21+
{
22+
float4x4 projection;
23+
float4x4 model;
24+
float4x4 view;
25+
float3 camPos;
26+
};
27+
ConstantBuffer<UBO> ubo;
28+
29+
struct UBOParams {
30+
float4 lights[4];
31+
};
32+
ConstantBuffer<UBOParams> uboParams;
33+
34+
struct Material {
35+
[[vk::offset(12)]] float roughness;
36+
[[vk::offset(16)]] float metallic;
37+
[[vk::offset(20)]] float r;
38+
[[vk::offset(24)]] float g;
39+
[[vk::offset(28)]] float b;
40+
};
41+
[[vk::push_constant]] Material material;
42+
43+
static const float PI = 3.14159265359;
44+
45+
// Normal Distribution function --------------------------------------
46+
float D_GGX(float dotNH, float roughness)
47+
{
48+
float alpha = roughness * roughness;
49+
float alpha2 = alpha * alpha;
50+
float denom = dotNH * dotNH * (alpha2 - 1.0) + 1.0;
51+
return (alpha2)/(PI * denom*denom);
52+
}
53+
54+
// Geometric Shadowing function --------------------------------------
55+
float G_SchlicksmithGGX(float dotNL, float dotNV, float roughness)
56+
{
57+
float r = (roughness + 1.0);
58+
float k = (r*r) / 8.0;
59+
float GL = dotNL / (dotNL * (1.0 - k) + k);
60+
float GV = dotNV / (dotNV * (1.0 - k) + k);
61+
return GL * GV;
62+
}
63+
64+
// Fresnel function ----------------------------------------------------
65+
float3 F_Schlick(float cosTheta, Material material)
66+
{
67+
float3 F0 = lerp(float3(0.04, 0.04, 0.04), float3(material.r, material.g, material.b), material.metallic); // * material.specular
68+
float3 F = F0 + (1.0 - F0) * pow(1.0 - cosTheta, 5.0);
69+
return F;
70+
}
71+
72+
// Specular BRDF composition --------------------------------------------
73+
74+
float3 BRDF(float3 L, float3 V, float3 N, Material material)
75+
{
76+
// Precalculate vectors and dot products
77+
float3 H = normalize (V + L);
78+
float dotNV = clamp(dot(N, V), 0.0, 1.0);
79+
float dotNL = clamp(dot(N, L), 0.0, 1.0);
80+
float dotLH = clamp(dot(L, H), 0.0, 1.0);
81+
float dotNH = clamp(dot(N, H), 0.0, 1.0);
82+
83+
// Light color fixed
84+
float3 lightColor = float3(1.0, 1.0, 1.0);
85+
86+
float3 color = float3(0.0, 0.0, 0.0);
87+
88+
if (dotNL > 0.0)
89+
{
90+
float rroughness = max(0.05, material.roughness);
91+
// D = Normal distribution (Distribution of the microfacets)
92+
float D = D_GGX(dotNH, material.roughness);
93+
// G = Geometric shadowing term (Microfacets shadowing)
94+
float G = G_SchlicksmithGGX(dotNL, dotNV, rroughness);
95+
// F = Fresnel factor (Reflectance depending on angle of incidence)
96+
float3 F = F_Schlick(dotNV, material);
97+
98+
float3 spec = D * F * G / (4.0 * dotNL * dotNV);
99+
100+
color += spec * dotNL * lightColor;
101+
}
102+
103+
return color;
104+
}
105+
106+
[shader("vertex")]
107+
VSOutput vertexMain(VSInput input, uniform float3 objPos)
108+
{
109+
VSOutput output;
110+
float3 locPos = mul(ubo.model, float4(input.Pos, 1.0)).xyz;
111+
output.WorldPos = locPos + objPos;
112+
output.Normal = mul((float3x3)ubo.model, input.Normal);
113+
output.Pos = mul(ubo.projection, mul(ubo.view, float4(output.WorldPos, 1.0)));
114+
return output;
115+
}
116+
117+
[shader("fragment")]
118+
float4 fragmentMain(VSOutput input)
119+
{
120+
float3 N = normalize(input.Normal);
121+
float3 V = normalize(ubo.camPos - input.WorldPos);
122+
123+
// Specular contribution
124+
float3 Lo = float3(0.0, 0.0, 0.0);
125+
for (int i = 0; i < 4; i++) {
126+
float3 L = normalize(uboParams.lights[i].xyz - input.WorldPos);
127+
Lo += BRDF(L, V, N, material);
128+
};
129+
130+
// Combine with ambient
131+
float3 color = float3(material.r, material.g, material.b) * 0.02;
132+
color += Lo;
133+
134+
// Gamma correct
135+
color = pow(color, float3(0.4545, 0.4545, 0.4545));
136+
137+
return float4(color, 1.0);
138+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
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+
[shader("vertex")]
19+
VSOutput vertexMain(VSInput input, uniform float4x4 mvp)
20+
{
21+
VSOutput output;
22+
output.UVW = input.Pos;
23+
output.Pos = mul(mvp, float4(input.Pos.xyz, 1.0));
24+
return output;
25+
}

0 commit comments

Comments
 (0)