Skip to content

Commit 6183ad5

Browse files
committed
Added slang shaders for deferred shadows sample
1 parent 9ff0f35 commit 6183ad5

3 files changed

Lines changed: 327 additions & 0 deletions

File tree

Lines changed: 187 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,187 @@
1+
// Copyright 2020 Google LLC
2+
3+
#define LIGHT_COUNT 3
4+
#define SHADOW_FACTOR 0.25
5+
#define AMBIENT_LIGHT 0.1
6+
#define USE_PCF
7+
8+
struct VSOutput
9+
{
10+
float4 Pos : SV_POSITION;
11+
float2 UV;
12+
};
13+
14+
[[vk::binding(1, 0)]] Sampler2D samplerPosition;
15+
[[vk::binding(2, 0)]] Sampler2D samplerNormal;
16+
[[vk::binding(3, 0)]] Sampler2D samplerAlbedo;
17+
18+
struct Light
19+
{
20+
float4 position;
21+
float4 target;
22+
float4 color;
23+
float4x4 viewMatrix;
24+
};
25+
26+
struct UBO
27+
{
28+
float4 viewPos;
29+
Light lights[LIGHT_COUNT];
30+
int useShadows;
31+
int displayDebugTarget;
32+
};
33+
[[vk::binding(4, 0)]] ConstantBuffer<UBO> ubo;
34+
35+
// Depth from the light's point of view
36+
// layout (binding = 5) uniform sampler2DShadow samplerShadowMap;
37+
[[vk::binding(5, 0)]] Sampler2DArray samplerShadowMap;
38+
39+
float textureProj(float4 P, float layer, float2 offset)
40+
{
41+
float shadow = 1.0;
42+
float4 shadowCoord = P / P.w;
43+
shadowCoord.xy = shadowCoord.xy * 0.5 + 0.5;
44+
45+
if (shadowCoord.z > -1.0 && shadowCoord.z < 1.0)
46+
{
47+
float dist = samplerShadowMap.Sample(float3(shadowCoord.xy + offset, layer)).r;
48+
if (shadowCoord.w > 0.0 && dist < shadowCoord.z)
49+
{
50+
shadow = SHADOW_FACTOR;
51+
}
52+
}
53+
return shadow;
54+
}
55+
56+
float filterPCF(float4 sc, float layer)
57+
{
58+
int2 texDim; int elements; int levels;
59+
samplerShadowMap.GetDimensions(0, texDim.x, texDim.y, elements, levels);
60+
float scale = 1.5;
61+
float dx = scale * 1.0 / float(texDim.x);
62+
float dy = scale * 1.0 / float(texDim.y);
63+
64+
float shadowFactor = 0.0;
65+
int count = 0;
66+
int range = 1;
67+
68+
for (int x = -range; x <= range; x++)
69+
{
70+
for (int y = -range; y <= range; y++)
71+
{
72+
shadowFactor += textureProj(sc, layer, float2(dx*x, dy*y));
73+
count++;
74+
}
75+
76+
}
77+
return shadowFactor / count;
78+
}
79+
80+
float3 shadow(float3 fragcolor, float3 fragPos)
81+
{
82+
for (int i = 0; i < LIGHT_COUNT; ++i)
83+
{
84+
float4 shadowClip = mul(ubo.lights[i].viewMatrix, float4(fragPos.xyz, 1.0));
85+
86+
float shadowFactor;
87+
#ifdef USE_PCF
88+
shadowFactor= filterPCF(shadowClip, i);
89+
#else
90+
shadowFactor = textureProj(shadowClip, i, float2(0.0, 0.0));
91+
#endif
92+
93+
fragcolor *= shadowFactor;
94+
}
95+
return fragcolor;
96+
}
97+
98+
[shader("vertex")]
99+
VSOutput vertexMain(uint VertexIndex: SV_VertexID)
100+
{
101+
VSOutput output;
102+
output.UV = float2((VertexIndex << 1) & 2, VertexIndex & 2);
103+
output.Pos = float4(output.UV * 2.0f - 1.0f, 0.0f, 1.0f);
104+
return output;
105+
}
106+
107+
[shader("fragment")]
108+
float4 fragmentMain(VSOutput input)
109+
{
110+
// Get G-Buffer values
111+
float3 fragPos = samplerPosition.Sample(input.UV).rgb;
112+
float3 normal = samplerNormal.Sample(input.UV).rgb;
113+
float4 albedo = samplerAlbedo.Sample(input.UV);
114+
115+
float3 fragcolor;
116+
117+
// Debug display
118+
if (ubo.displayDebugTarget > 0) {
119+
switch (ubo.displayDebugTarget) {
120+
case 1:
121+
fragcolor.rgb = shadow(float3(1.0, 1.0, 1.0), fragPos);
122+
break;
123+
case 2:
124+
fragcolor.rgb = fragPos;
125+
break;
126+
case 3:
127+
fragcolor.rgb = normal;
128+
break;
129+
case 4:
130+
fragcolor.rgb = albedo.rgb;
131+
break;
132+
case 5:
133+
fragcolor.rgb = albedo.aaa;
134+
break;
135+
}
136+
return float4(fragcolor, 1.0);
137+
}
138+
139+
// Ambient part
140+
fragcolor = albedo.rgb * AMBIENT_LIGHT;
141+
142+
float3 N = normalize(normal);
143+
144+
for(int i = 0; i < LIGHT_COUNT; ++i)
145+
{
146+
// Vector to light
147+
float3 L = ubo.lights[i].position.xyz - fragPos;
148+
// Distance from light to fragment position
149+
float dist = length(L);
150+
L = normalize(L);
151+
152+
// Viewer to fragment
153+
float3 V = ubo.viewPos.xyz - fragPos;
154+
V = normalize(V);
155+
156+
float lightCosInnerAngle = cos(radians(15.0));
157+
float lightCosOuterAngle = cos(radians(25.0));
158+
float lightRange = 100.0;
159+
160+
// Direction vector from source to target
161+
float3 dir = normalize(ubo.lights[i].position.xyz - ubo.lights[i].target.xyz);
162+
163+
// Dual cone spot light with smooth transition between inner and outer angle
164+
float cosDir = dot(L, dir);
165+
float spotEffect = smoothstep(lightCosOuterAngle, lightCosInnerAngle, cosDir);
166+
float heightAttenuation = smoothstep(lightRange, 0.0f, dist);
167+
168+
// Diffuse lighting
169+
float NdotL = max(0.0, dot(N, L));
170+
float3 diff = NdotL.xxx;
171+
172+
// Specular lighting
173+
float3 R = reflect(-L, N);
174+
float NdotR = max(0.0, dot(R, V));
175+
float3 spec = (pow(NdotR, 16.0) * albedo.a * 2.5).xxx;
176+
177+
fragcolor += float3((diff + spec) * spotEffect * heightAttenuation) * ubo.lights[i].color.rgb * albedo.rgb;
178+
}
179+
180+
// Shadow calculations in a separate pass
181+
if (ubo.useShadows > 0)
182+
{
183+
fragcolor = shadow(fragcolor, fragPos);
184+
}
185+
186+
return float4(fragcolor, 1);
187+
}
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
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+
float3 Normal;
13+
float3 Tangent;
14+
};
15+
16+
struct VSOutput
17+
{
18+
float4 Pos : SV_POSITION;
19+
float3 Normal;
20+
float2 UV;
21+
float3 Color;
22+
float3 WorldPos;
23+
float3 Tangent;
24+
};
25+
26+
struct FSOutput
27+
{
28+
float4 Position : SV_TARGET0;
29+
float4 Normal : SV_TARGET1;
30+
float4 Albedo : SV_TARGET2;
31+
};
32+
33+
struct UBO
34+
{
35+
float4x4 projection;
36+
float4x4 model;
37+
float4x4 view;
38+
float4 instancePos[3];
39+
};
40+
ConstantBuffer<UBO> ubo;
41+
42+
Sampler2D samplerColor;
43+
Sampler2D samplerNormalMap;
44+
45+
[shader("vertex")]
46+
VSOutput vertexMain(VSInput input, uint InstanceIndex: SV_InstanceID)
47+
{
48+
VSOutput output;
49+
float4 tmpPos = input.Pos + ubo.instancePos[InstanceIndex];
50+
51+
output.Pos = mul(ubo.projection, mul(ubo.view, mul(ubo.model, tmpPos)));
52+
53+
output.UV = input.UV;
54+
55+
// Vertex position in world space
56+
output.WorldPos = mul(ubo.model, tmpPos).xyz;
57+
58+
// Normal in world space
59+
output.Normal = normalize(input.Normal);
60+
output.Tangent = normalize(input.Tangent);
61+
62+
// Currently just vertex color
63+
output.Color = input.Color;
64+
return output;
65+
}
66+
67+
[shader("fragment")]
68+
FSOutput fragmentMain(VSOutput input)
69+
{
70+
FSOutput output;
71+
output.Position = float4(input.WorldPos, 1.0);
72+
73+
// Calculate normal in tangent space
74+
float3 N = normalize(input.Normal);
75+
float3 T = normalize(input.Tangent);
76+
float3 B = cross(N, T);
77+
float3x3 TBN = float3x3(T, B, N);
78+
float3 tnorm = mul(normalize(samplerNormalMap.Sample(input.UV).xyz * 2.0 - float3(1.0, 1.0, 1.0)), TBN);
79+
output.Normal = float4(tnorm, 1.0);
80+
81+
output.Albedo = samplerColor.Sample(input.UV);
82+
return output;
83+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/* Copyright (c) 2025, Sascha Willems
2+
*
3+
* SPDX-License-Identifier: MIT
4+
*
5+
*/
6+
7+
#define LIGHT_COUNT 3
8+
9+
struct VSInput
10+
{
11+
float4 Pos;
12+
}
13+
14+
struct VSOutput
15+
{
16+
float4 Pos : SV_POSITION;
17+
int InstanceIndex;
18+
};
19+
20+
struct GSOutput
21+
{
22+
float4 Pos : SV_POSITION;
23+
int Layer : SV_RenderTargetArrayIndex;
24+
};
25+
26+
struct UBO
27+
{
28+
float4x4 mvp[LIGHT_COUNT];
29+
float4 instancePos[3];
30+
};
31+
ConstantBuffer<UBO> ubo;
32+
33+
[shader("vertex")]
34+
VSOutput vertexMain(VSInput input, uint InstanceIndex: SV_InstanceID)
35+
{
36+
VSOutput output;
37+
output.InstanceIndex = InstanceIndex;
38+
output.Pos = input.Pos;
39+
return output;
40+
}
41+
42+
[shader("geometry")]
43+
[maxvertexcount(3)]
44+
[instance(3)]
45+
void geometryMain(triangle VSOutput input[3], uint InvocationID : SV_GSInstanceID, inout TriangleStream<GSOutput> outStream)
46+
{
47+
float4 instancedPos = ubo.instancePos[input[0].InstanceIndex];
48+
for (int i = 0; i < 3; i++)
49+
{
50+
float4 tmpPos = input[i].Pos + instancedPos;
51+
GSOutput output;
52+
output.Pos = mul(ubo.mvp[InvocationID], tmpPos);
53+
output.Layer = InvocationID;
54+
outStream.Append( output );
55+
}
56+
outStream.RestartStrip();
57+
}

0 commit comments

Comments
 (0)