Skip to content

Commit 834ee9e

Browse files
committed
Added slang shaders for additional samples
1 parent 8291187 commit 834ee9e

5 files changed

Lines changed: 271 additions & 0 deletions

File tree

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
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+
[shader("vertex")]
24+
VSOutput vertexMain(VSInput input, uniform float4x4 mvp, uniform float3 color)
25+
{
26+
VSOutput output;
27+
if ((input.Color.r == 1.0) && (input.Color.g == 0.0) && (input.Color.b == 0.0))
28+
{
29+
output.Color = color;
30+
}
31+
else
32+
{
33+
output.Color = input.Color;
34+
}
35+
output.Pos = mul(mvp, float4(input.Pos.xyz, 1.0));
36+
float4 pos = mul(mvp, float4(input.Pos, 1.0));
37+
output.Normal = mul((float3x3)mvp, input.Normal);
38+
float3 lPos = float3(0.0, 0.0, 0.0);
39+
output.LightVec = lPos - pos.xyz;
40+
output.ViewVec = -pos.xyz;
41+
return output;
42+
}
43+
44+
[shader("fragment")]
45+
float4 fragmentMain(VSOutput input)
46+
{
47+
float3 N = normalize(input.Normal);
48+
float3 L = normalize(input.LightVec);
49+
float3 V = normalize(input.ViewVec);
50+
float3 R = reflect(-L, N);
51+
float3 diffuse = max(dot(N, L), 0.0) * input.Color;
52+
float3 specular = pow(max(dot(R, V), 0.0), 8.0) * float3(0.75, 0.75, 0.75);
53+
return float4(diffuse + specular, 1.0);
54+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/* Copyright (c) 2025, Sascha Willems
2+
*
3+
* SPDX-License-Identifier: MIT
4+
*
5+
*/
6+
7+
#define HASHSCALE3 float3(443.897, 441.423, 437.195)
8+
#define STARFREQUENCY 0.01
9+
10+
// Hash function by Dave Hoskins (https://www.shadertoy.com/view/4djSRW)
11+
float hash33(float3 p3)
12+
{
13+
p3 = frac(p3 * HASHSCALE3);
14+
p3 += dot(p3, p3.yxz+float3(19.19, 19.19, 19.19));
15+
return frac((p3.x + p3.y)*p3.z + (p3.x+p3.z)*p3.y + (p3.y+p3.z)*p3.x);
16+
}
17+
18+
float3 starField(float3 pos)
19+
{
20+
float3 color = float3(0.0, 0.0, 0.0);
21+
float threshhold = (1.0 - STARFREQUENCY);
22+
float rnd = hash33(pos);
23+
if (rnd >= threshhold)
24+
{
25+
float starCol = pow((rnd - threshhold) / (1.0 - threshhold), 16.0);
26+
color += starCol.xxx;
27+
}
28+
return color;
29+
}
30+
31+
struct VSInput
32+
{
33+
float3 Pos;
34+
}
35+
36+
struct VSOutput
37+
{
38+
float4 Pos : SV_POSITION;
39+
float3 UVW;
40+
};
41+
42+
[shader("vertex")]
43+
VSOutput vertexMain(VSInput input, uniform float4x4 mvp)
44+
{
45+
VSOutput output;
46+
output.UVW = input.Pos;
47+
output.Pos = mul(mvp, float4(input.Pos.xyz, 1.0));
48+
return output;
49+
}
50+
51+
[shader("fragment")]
52+
float4 fragmentMain(VSOutput input)
53+
{
54+
// Fake atmosphere at the bottom
55+
float3 atmosphere = clamp(float3(0.1, 0.15, 0.4) * (input.UVW.y + 0.25), 0.0, 1.0);
56+
float3 color = starField(input.UVW) + atmosphere;
57+
return float4(color, 1.0);
58+
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
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+
float4 ProjCoord;
16+
};
17+
18+
struct UBO
19+
{
20+
float4x4 projection;
21+
float4x4 view;
22+
float4x4 model;
23+
};
24+
ConstantBuffer<UBO> ubo;
25+
26+
Sampler2D samplerColor;
27+
28+
[shader("vertex")]
29+
VSOutput vertexMain(VSInput input)
30+
{
31+
VSOutput output;
32+
output.ProjCoord = mul(ubo.projection, mul(ubo.view, mul(ubo.model, float4(input.Pos.xyz, 1.0))));
33+
output.Pos = output.ProjCoord;
34+
return output;
35+
}
36+
37+
[shader("fragment")]
38+
float4 fragmentMain(VSOutput input, bool FrontFacing : SV_IsFrontFace)
39+
{
40+
float4 tmp = (1.0 / input.ProjCoord.w).xxxx;
41+
float4 projCoord = input.ProjCoord * tmp;
42+
43+
// Scale and bias
44+
projCoord += float4(1.0, 1.0, 1.0, 1.0);
45+
projCoord *= float4(0.5, 0.5, 0.5, 0.5);
46+
47+
// Slow single pass blur
48+
// For demonstration purposes only
49+
const float blurSize = 1.0 / 512.0;
50+
51+
float4 color = float4(float3(0.0, 0.0, 0.0), 1.);
52+
53+
if (FrontFacing)
54+
{
55+
// Only render mirrored scene on front facing (upper) side of mirror surface
56+
float4 reflection = float4(0.0, 0.0, 0.0, 0.0);
57+
for (int x = -3; x <= 3; x++)
58+
{
59+
for (int y = -3; y <= 3; y++)
60+
{
61+
reflection += samplerColor.Sample(float2(projCoord.x + x * blurSize, projCoord.y + y * blurSize)) / 49.0;
62+
}
63+
}
64+
color += reflection;
65+
}
66+
67+
return color;
68+
}
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 Color;
11+
float3 Normal;
12+
};
13+
14+
struct VSOutput
15+
{
16+
float4 Pos : SV_POSITION;
17+
float ClipDistance : SV_ClipDistance0;
18+
float3 Normal;
19+
float3 Color;
20+
float3 EyePos;
21+
float3 LightVec;
22+
};
23+
24+
struct UBO
25+
{
26+
float4x4 projection;
27+
float4x4 view;
28+
float4x4 model;
29+
float4 lightPos;
30+
};
31+
ConstantBuffer<UBO> ubo;
32+
33+
[shader("vertex")]
34+
VSOutput vertexMain(VSInput input)
35+
{
36+
VSOutput output;
37+
output.Normal = input.Normal;
38+
output.Color = input.Color;
39+
output.Pos = mul(ubo.projection, mul(ubo.view, mul(ubo.model, float4(input.Pos, 1.0))));
40+
output.EyePos = mul(ubo.view, mul(ubo.model, float4(input.Pos, 1.0))).xyz;
41+
output.LightVec = normalize(ubo.lightPos.xyz - output.EyePos);
42+
// Clip against reflection plane
43+
float4 clipPlane = float4(0.0, 0.0, 0.0, 0.0);
44+
output.ClipDistance = dot(float4(input.Pos, 1.0), clipPlane);
45+
return output;
46+
}
47+
48+
49+
[shader("fragment")]
50+
float4 fragmentMain(VSOutput input)
51+
{
52+
float3 Eye = normalize(-input.EyePos);
53+
float3 Reflected = normalize(reflect(-input.LightVec, input.Normal));
54+
float4 IAmbient = float4(0.1, 0.1, 0.1, 1.0);
55+
float4 IDiffuse = max(dot(input.Normal, input.LightVec), 0.0).xxxx;
56+
float specular = 0.75;
57+
float4 ISpecular = float4(0.0, 0.0, 0.0, 0.0);
58+
if (dot(input.EyePos, input.Normal) < 0.0)
59+
{
60+
ISpecular = float4(0.5, 0.5, 0.5, 1.0) * pow(max(dot(Reflected, Eye), 0.0), 16.0) * specular;
61+
}
62+
return float4((IAmbient + IDiffuse) * float4(input.Color, 1.0) + ISpecular);
63+
}

shaders/slang/offscreen/quad.slang

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+
[[vk::binding(1, 0)]] 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+
}

0 commit comments

Comments
 (0)