1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
|
/*
* Tuxánci 2 - A first person shooter
* Copyright (C) 2025-2026 Connor Thomson
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
struct light_uniforms {
float3 direction;
float shadow_texel_size;
float shadow_minimum_bias;
float shadow_maximum_bias;
float shadow_strength;
float shadow_penumbra_scale;
};
[[vk::binding(0, 3)]]
ConstantBuffer<light_uniforms> light : register(b0, space3);
[[vk::binding(0, 2)]]
Sampler2DShadow shadow_map : register(t0, space2);
[[vk::binding(1, 2)]]
Sampler2D<float> shadow_depth_map : register(t1, space2);
struct FSInput {
float3 normal : TEXCOORD0;
float3 world_position : TEXCOORD1;
float3 camera_position : TEXCOORD2;
float4 light_space_position : TEXCOORD3;
};
struct FSOutput {
float4 out_color : SV_Target0;
};
static const float ambient_strength = 0.22;
static const float diffuse_strength = 0.78;
static const float specular_strength = 0.6;
static const float shininess = 32.0;
static const int shadow_blocker_sample_count = 32;
static const int shadow_pcf_sample_count = 64;
static const float shadow_maximum_search_uv = 0.02;
static const float shadow_minimum_filter_texels = 1.5;
static const float shadow_maximum_filter_uv = 0.02;
static const float2 blocker_search_disk[shadow_blocker_sample_count] = {
float2( 0.44011213, -0.38478364),
float2(-0.93881893, 0.28433844),
float2( 0.29851448, 0.91473036),
float2(-0.56853028, -0.74873636),
float2( 0.92702072, 0.34599034),
float2(-0.17203432, 0.12048409),
float2(-0.40881620, 0.87902032),
float2( 0.11214125, -0.97011162),
float2( 0.35807629, 0.34593269),
float2(-0.93515467, -0.30090412),
float2( 0.96965589, -0.21669445),
float2(-0.12747712, -0.42799143),
float2(-0.04160416, 0.58750344),
float2(-0.50661284, -0.18463137),
float2(-0.49591186, 0.43573011),
float2( 0.69214410, 0.71546306),
float2( 0.58762817, -0.78906213),
float2( 0.60529434, -0.00157846),
float2( 0.18530521, -0.07537744),
float2(-0.06393139, 0.98782194),
float2(-0.24668936, -0.92075575),
float2( 0.81235073, -0.52916214),
float2( 0.18896697, -0.62996998),
float2(-0.71415530, 0.69260619),
float2(-0.65167829, 0.11466757),
float2(-0.65839165, -0.45386823),
float2( 0.40773627, 0.63990787),
float2(-0.99945967, -0.01288785),
float2( 0.06793334, 0.29985044),
float2( 0.88601041, 0.06665496),
float2( 0.63692110, 0.42919088),
float2(-0.38553272, -0.52927284),
};
static const float2 pcf_filter_disk[shadow_pcf_sample_count] = {
float2( 0.32558563, -0.24988839),
float2(-0.98541266, 0.16930865),
float2( 0.09145512, 0.89445213),
float2(-0.54480597, -0.82331975),
float2( 0.86243144, 0.42877010),
float2( 0.19152898, -0.98054135),
float2(-0.24839078, 0.19012049),
float2(-0.58789047, 0.78475191),
float2( 0.97505840, -0.19006157),
float2( 0.30273086, 0.33427269),
float2(-0.60614320, -0.24764520),
float2( 0.64000315, -0.68394018),
float2(-0.12301224, -0.53311522),
float2( 0.51234899, 0.75192892),
float2( 0.64313826, 0.06972717),
float2(-0.19284146, 0.60355651),
float2(-0.62280056, 0.36318874),
float2(-0.18834340, -0.95691492),
float2(-0.04911758, -0.13578936),
float2(-0.82927681, -0.54621998),
float2( 0.27020206, -0.61902601),
float2(-0.98076941, -0.18488425),
float2(-0.26883836, 0.96108127),
float2( 0.66283184, -0.32922370),
float2( 0.98983969, 0.13429859),
float2(-0.44329948, -0.53094113),
float2(-0.54192541, 0.06588807),
float2( 0.14739192, 0.59331660),
float2( 0.04982322, 0.14943985),
float2(-0.31222249, -0.26794820),
float2( 0.56008364, 0.46637058),
float2(-0.81663912, 0.57351727),
float2( 0.32731617, 0.05812120),
float2( 0.85294944, -0.52194453),
float2( 0.44306862, -0.87673132),
float2( 0.03380403, -0.76031090),
float2( 0.08711519, -0.36824602),
float2(-0.04387798, 0.39043461),
float2(-0.44689900, 0.54474622),
float2(-0.79730201, -0.01361312),
float2( 0.34065561, 0.93312502),
float2(-0.28571328, -0.73045171),
float2( 0.74645565, 0.64790973),
float2( 0.47432929, -0.49756161),
float2(-0.32838125, -0.02568294),
float2( 0.49288123, -0.09977098),
float2( 0.51299973, 0.24791173),
float2( 0.75389572, -0.12623179),
float2(-0.84467634, 0.34163260),
float2(-0.36987966, 0.76150517),
float2(-0.27850542, 0.40472485),
float2(-0.10969806, 0.80499616),
float2(-0.81179020, -0.32586517),
float2( 0.17068056, -0.09060725),
float2( 0.36889045, 0.58095936),
float2( 0.79213670, 0.22321366),
float2(-0.62388309, -0.63161735),
float2(-0.45122724, 0.24914915),
float2(-0.11527424, -0.33262249),
float2(-0.71985451, 0.17533603),
float2( 0.07976847, -0.56354086),
float2(-0.64407590, -0.43844161),
float2( 0.25012793, 0.75807218),
float2(-0.13113449, 0.03615541),
};
float find_blocker(float2 uv, float reference, float search_radius, out float blocker_depth) {
float blockers = 0.0;
blocker_depth = 0.0;
for (int index = 0; index < shadow_blocker_sample_count; index++) {
float depth = shadow_depth_map.SampleLevel(uv + blocker_search_disk[index] * search_radius, 0.0);
if (depth < reference) {
blocker_depth += depth;
blockers += 1.0;
}
}
if (blockers > 0.0) {
blocker_depth /= blockers;
}
return blockers;
}
float penumbra_size(float reference, float blocker_depth) {
return light.shadow_penumbra_scale * (reference - blocker_depth);
}
float pcf_filter(float2 uv, float reference, float filter_radius) {
float visibility = 0.0;
for (int index = 0; index < shadow_pcf_sample_count; index++) {
visibility += shadow_map.SampleCmpLevelZero(uv + pcf_filter_disk[index] * filter_radius, reference);
}
return visibility / float(shadow_pcf_sample_count);
}
float shadow_visibility(float4 light_space_position, float normal_dot_light) {
float3 projected = light_space_position.xyz / light_space_position.w;
if (projected.z < 0.0 || projected.z > 1.0) {
return 1.0;
}
float2 uv = float2(0.5 + 0.5 * projected.x, 0.5 - 0.5 * projected.y);
if (any(uv < 0.0) || any(uv > 1.0)) {
return 1.0;
}
float bias = max(light.shadow_maximum_bias * (1.0 - normal_dot_light), light.shadow_minimum_bias);
float reference = projected.z - bias;
float search_radius = clamp(light.shadow_penumbra_scale * reference, light.shadow_texel_size, shadow_maximum_search_uv);
float blocker_depth;
if (find_blocker(uv, reference, search_radius, blocker_depth) == 0.0) {
return 1.0;
}
float filter_radius = clamp(penumbra_size(reference, blocker_depth), shadow_minimum_filter_texels * light.shadow_texel_size, shadow_maximum_filter_uv);
return pcf_filter(uv, reference, filter_radius);
}
FSOutput main(FSInput input) {
float3 light_direction = normalize(light.direction);
float3 normal = normalize(input.normal);
float3 view_direction = normalize(input.camera_position - input.world_position);
float3 halfway_direction = normalize(light_direction + view_direction);
float normal_dot_light = dot(normal, light_direction);
float diffuse = max(normal_dot_light, 0.0);
float specular = diffuse > 0.0 ? pow(max(dot(normal, halfway_direction), 0.0), shininess) : 0.0;
float visibility = shadow_visibility(input.light_space_position, normal_dot_light);
visibility = lerp(1.0, visibility, light.shadow_strength);
float shade = saturate(ambient_strength + visibility * (diffuse * diffuse_strength + specular * specular_strength));
FSOutput output;
output.out_color = float4(shade, shade, shade, 1.0);
return output;
}
|