Debian Codegraph @

shaders/src/shadowing.fs

raw
1 //------------------------------------------------------------------------------
2 // Shadowing configuration
3 //------------------------------------------------------------------------------
4
5 #define SHADOW_SAMPLING_PCF_HARD 0
6 #define SHADOW_SAMPLING_PCF_LOW 1
7 #define SHADOW_SAMPLING_PCF_MEDIUM 2
8 #define SHADOW_SAMPLING_PCF_HIGH 3
9
10 #define SHADOW_SAMPLING_ERROR_DISABLED 0
11 #define SHADOW_SAMPLING_ERROR_ENABLED 1
12
13 #define SHADOW_RECEIVER_PLANE_DEPTH_BIAS_DISABLED 0
14 #define SHADOW_RECEIVER_PLANE_DEPTH_BIAS_ENABLED 1
15
16 #define SHADOW_RECEIVER_PLANE_DEPTH_BIAS_MIN_SAMPLING_METHOD SHADOW_SAMPLING_PCF_MEDIUM
17
18 #define SHADOW_SAMPLING_METHOD SHADOW_SAMPLING_PCF_LOW
19 #define SHADOW_SAMPLING_ERROR SHADOW_SAMPLING_ERROR_DISABLED
20 #define SHADOW_RECEIVER_PLANE_DEPTH_BIAS SHADOW_RECEIVER_PLANE_DEPTH_BIAS_DISABLED
21
22 #if SHADOW_SAMPLING_ERROR == SHADOW_SAMPLING_ERROR_ENABLED
23 #undef SHADOW_RECEIVER_PLANE_DEPTH_BIAS
24 #define SHADOW_RECEIVER_PLANE_DEPTH_BIAS SHADOW_RECEIVER_PLANE_DEPTH_BIAS_ENABLED
25 #elif SHADOW_SAMPLING_METHOD < SHADOW_RECEIVER_PLANE_DEPTH_BIAS_MIN_SAMPLING_METHOD
26 #undef SHADOW_RECEIVER_PLANE_DEPTH_BIAS
27 #define SHADOW_RECEIVER_PLANE_DEPTH_BIAS SHADOW_RECEIVER_PLANE_DEPTH_BIAS_DISABLED
28 #endif
29
30 //------------------------------------------------------------------------------
31 // Shadow sampling methods
32 //------------------------------------------------------------------------------
33
34 vec2 computeReceiverPlaneDepthBias(const vec3 position) {
35 // see: GDC '06: Shadow Mapping: GPU-based Tips and Techniques
36 vec2 bias;
37 #if SHADOW_RECEIVER_PLANE_DEPTH_BIAS == SHADOW_RECEIVER_PLANE_DEPTH_BIAS_ENABLED
38 vec3 du = dFdx(position);
39 vec3 dv = dFdy(position);
40
41 // Chain rule we use:
42 // | du.x du.y |^-T | dv.y -du.y |T | dv.y -dv.x |
43 // D * | dv.x dv.y | = | -dv.x du.x | = | -du.y du.x |
44
45 bias = inverse(mat2(du.xy, dv.xy)) * vec2(du.z, dv.z);
46 #else
47 bias = vec2(0.0);
48 #endif
49 return bias;
50 }
51
52 float samplingBias(float depth, const vec2 rpdb, const vec2 texelSize) {
53 #if SHADOW_SAMPLING_ERROR == SHADOW_SAMPLING_ERROR_ENABLED
54 // note: if filtering is set to NEAREST, the 2.0 factor below can be changed to 1.0
55 float samplingError = min(2.0 * dot(texelSize, abs(rpdb)), 0.01);
56 depth += samplingError;
57 #endif
58 return depth;
59 }
60
61 float sampleDepth(const mediump sampler2DArrayShadow map, const uint layer, vec2 base, vec2 dudv, float depth, vec2 rpdb) {
62 #if SHADOW_RECEIVER_PLANE_DEPTH_BIAS == SHADOW_RECEIVER_PLANE_DEPTH_BIAS_ENABLED
63 #if SHADOW_SAMPLING_METHOD >= SHADOW_RECEIVER_PLANE_DEPTH_BIAS_MIN_SAMPLING_METHOD
64 depth += dot(dudv, rpdb);
65 #endif
66 #endif
67 // depth must be clamped to support floating-point depth formats. This is to avoid comparing a
68 // value from the depth texture (which is never greater than 1.0) with a greater-than-one
69 // comparison value (which is possible with floating-point formats).
70 return texture(map, vec4(base + dudv, layer, clamp(depth, 0.0, 1.0)));
71 }
72
73 #if SHADOW_SAMPLING_METHOD == SHADOW_SAMPLING_PCF_HARD
74 float ShadowSample_Hard(const mediump sampler2DArrayShadow map, const uint layer, const vec2 size, const vec3 position) {
75 vec2 rpdb = computeReceiverPlaneDepthBias(position);
76 float depth = samplingBias(position.z, rpdb, vec2(1.0) / size);
77 return texture(map, vec4(position.xy, layer, clamp(depth, 0.0, 1.0)));
78 }
79 #endif
80
81 #if SHADOW_SAMPLING_METHOD == SHADOW_SAMPLING_PCF_LOW
82 float ShadowSample_PCF_Low(const mediump sampler2DArrayShadow map, const uint layer, const vec2 size, vec3 position) {
83 // Castaño, 2013, "Shadow Mapping Summary Part 1"
84 vec2 texelSize = vec2(1.0) / size;
85
86 // clamp position to avoid overflows below, which cause some GPUs to abort
87 position.xy = clamp(position.xy, vec2(-1.0), vec2(2.0));
88
89 vec2 offset = vec2(0.5);
90 vec2 uv = (position.xy * size) + offset;
91 vec2 base = (floor(uv) - offset) * texelSize;
92 vec2 st = fract(uv);
93
94 vec2 uw = vec2(3.0 - 2.0 * st.x, 1.0 + 2.0 * st.x);
95 vec2 vw = vec2(3.0 - 2.0 * st.y, 1.0 + 2.0 * st.y);
96
97 vec2 u = vec2((2.0 - st.x) / uw.x - 1.0, st.x / uw.y + 1.0);
98 vec2 v = vec2((2.0 - st.y) / vw.x - 1.0, st.y / vw.y + 1.0);
99
100 u *= texelSize.x;
101 v *= texelSize.y;
102
103 vec2 rpdb = computeReceiverPlaneDepthBias(position);
104
105 float depth = samplingBias(position.z, rpdb, texelSize);
106 float sum = 0.0;
107
108 sum += uw.x * vw.x * sampleDepth(map, layer, base, vec2(u.x, v.x), depth, rpdb);
109 sum += uw.y * vw.x * sampleDepth(map, layer, base, vec2(u.y, v.x), depth, rpdb);
110
111 sum += uw.x * vw.y * sampleDepth(map, layer, base, vec2(u.x, v.y), depth, rpdb);
112 sum += uw.y * vw.y * sampleDepth(map, layer, base, vec2(u.y, v.y), depth, rpdb);
113
114 return sum * (1.0 / 16.0);
115 }
116 #endif
117
118 #if SHADOW_SAMPLING_METHOD == SHADOW_SAMPLING_PCF_MEDIUM
119 float ShadowSample_PCF_Medium(const mediump sampler2DArrayShadow map, const uint layer, const vec2 size, vec3 position) {
120 // Castaño, 2013, "Shadow Mapping Summary Part 1"
121 vec2 texelSize = vec2(1.0) / size;
122
123 // clamp position to avoid overflows below, which cause some GPUs to abort
124 position.xy = clamp(position.xy, vec2(-1.0), vec2(2.0));
125
126 vec2 offset = vec2(0.5);
127 vec2 uv = (position.xy * size) + offset;
128 vec2 base = (floor(uv) - offset) * texelSize;
129 vec2 st = fract(uv);
130
131 vec3 uw = vec3(4.0 - 3.0 * st.x, 7.0, 1.0 + 3.0 * st.x);
132 vec3 vw = vec3(4.0 - 3.0 * st.y, 7.0, 1.0 + 3.0 * st.y);
133
134 vec3 u = vec3((3.0 - 2.0 * st.x) / uw.x - 2.0, (3.0 + st.x) / uw.y, st.x / uw.z + 2.0);
135 vec3 v = vec3((3.0 - 2.0 * st.y) / vw.x - 2.0, (3.0 + st.y) / vw.y, st.y / vw.z + 2.0);
136
137 u *= texelSize.x;
138 v *= texelSize.y;
139
140 vec2 rpdb = computeReceiverPlaneDepthBias(position);
141
142 float depth = samplingBias(position.z, rpdb, texelSize);
143 float sum = 0.0;
144
145 sum += uw.x * vw.x * sampleDepth(map, layer, base, vec2(u.x, v.x), depth, rpdb);
146 sum += uw.y * vw.x * sampleDepth(map, layer, base, vec2(u.y, v.x), depth, rpdb);
147 sum += uw.z * vw.x * sampleDepth(map, layer, base, vec2(u.z, v.x), depth, rpdb);
148
149 sum += uw.x * vw.y * sampleDepth(map, layer, base, vec2(u.x, v.y), depth, rpdb);
150 sum += uw.y * vw.y * sampleDepth(map, layer, base, vec2(u.y, v.y), depth, rpdb);
151 sum += uw.z * vw.y * sampleDepth(map, layer, base, vec2(u.z, v.y), depth, rpdb);
152
153 sum += uw.x * vw.z * sampleDepth(map, layer, base, vec2(u.x, v.z), depth, rpdb);
154 sum += uw.y * vw.z * sampleDepth(map, layer, base, vec2(u.y, v.z), depth, rpdb);
155 sum += uw.z * vw.z * sampleDepth(map, layer, base, vec2(u.z, v.z), depth, rpdb);
156
157 return sum * (1.0 / 144.0);
158 }
159 #endif
160
161 #if SHADOW_SAMPLING_METHOD == SHADOW_SAMPLING_PCF_HIGH
162 float ShadowSample_PCF_High(const mediump sampler2DArrayShadow map, const uint layer, const vec2 size, vec3 position) {
163 // Castaño, 2013, "Shadow Mapping Summary Part 1"
164 vec2 texelSize = vec2(1.0) / size;
165
166 // clamp position to avoid overflows below, which cause some GPUs to abort
167 position.xy = clamp(position.xy, vec2(-1.0), vec2(2.0));
168
169 vec2 offset = vec2(0.5);
170 vec2 uv = (position.xy * size) + offset;
171 vec2 base = (floor(uv) - offset) * texelSize;
172 vec2 st = fract(uv);
173
174 vec4 uw = vec4(
175 5.0 * st.x - 6.0,
176 11.0 * st.x - 28.0,
177 -(11.0 * st.x + 17.0),
178 -(5.0 * st.x + 1.0));
179 vec4 vw = vec4(
180 5.0 * st.y - 6.0,
181 11.0 * st.y - 28.0,
182 -(11.0 * st.y + 17.0),
183 -(5.0 * st.y + 1.0));
184
185 vec4 u = vec4(
186 (4.0 * st.x - 5.0) / uw.x - 3.0,
187 (4.0 * st.x - 16.0) / uw.y - 1.0,
188 -(7.0 * st.x + 5.0) / uw.z + 1.0,
189 -st.x / uw.w + 3.0);
190 vec4 v = vec4(
191 (4.0 * st.y - 5.0) / vw.x - 3.0,
192 (4.0 * st.y - 16.0) / vw.y - 1.0,
193 -(7.0 * st.y + 5.0) / vw.z + 1.0,
194 -st.y / vw.w + 3.0);
195
196 u *= texelSize.x;
197 v *= texelSize.y;
198
199 vec2 rpdb = computeReceiverPlaneDepthBias(position);
200
201 float depth = samplingBias(position.z, rpdb, texelSize);
202 float sum = 0.0;
203
204 sum += uw.x * vw.x * sampleDepth(map, layer, base, vec2(u.x, v.x), depth, rpdb);
205 sum += uw.y * vw.x * sampleDepth(map, layer, base, vec2(u.y, v.x), depth, rpdb);
206 sum += uw.z * vw.x * sampleDepth(map, layer, base, vec2(u.z, v.x), depth, rpdb);
207 sum += uw.w * vw.x * sampleDepth(map, layer, base, vec2(u.w, v.x), depth, rpdb);
208
209 sum += uw.x * vw.y * sampleDepth(map, layer, base, vec2(u.x, v.y), depth, rpdb);
210 sum += uw.y * vw.y * sampleDepth(map, layer, base, vec2(u.y, v.y), depth, rpdb);
211 sum += uw.z * vw.y * sampleDepth(map, layer, base, vec2(u.z, v.y), depth, rpdb);
212 sum += uw.w * vw.y * sampleDepth(map, layer, base, vec2(u.w, v.y), depth, rpdb);
213
214 sum += uw.x * vw.z * sampleDepth(map, layer, base, vec2(u.x, v.z), depth, rpdb);
215 sum += uw.y * vw.z * sampleDepth(map, layer, base, vec2(u.y, v.z), depth, rpdb);
216 sum += uw.z * vw.z * sampleDepth(map, layer, base, vec2(u.z, v.z), depth, rpdb);
217 sum += uw.w * vw.z * sampleDepth(map, layer, base, vec2(u.w, v.z), depth, rpdb);
218
219 sum += uw.x * vw.w * sampleDepth(map, layer, base, vec2(u.x, v.w), depth, rpdb);
220 sum += uw.y * vw.w * sampleDepth(map, layer, base, vec2(u.y, v.w), depth, rpdb);
221 sum += uw.z * vw.w * sampleDepth(map, layer, base, vec2(u.z, v.w), depth, rpdb);
222 sum += uw.w * vw.w * sampleDepth(map, layer, base, vec2(u.w, v.w), depth, rpdb);
223
224 return sum * (1.0 / 2704.0);
225 }
226 #endif
227
228 //------------------------------------------------------------------------------
229 // Screen-space Contact Shadows
230 //------------------------------------------------------------------------------
231
232 struct ScreenSpaceRay {
233 highp vec3 ssRayStart;
234 highp vec3 ssRayEnd;
235 highp vec3 ssViewRayEnd;
236 highp vec3 uvRayStart;
237 highp vec3 uvRay;
238 };
239
240 void initScreenSpaceRay(out ScreenSpaceRay ray, highp vec3 wsRayStart, vec3 wsRayDirection, float wsRayLength) {
241 highp mat4 worldToClip = getClipFromWorldMatrix();
242 highp mat4 viewToClip = getClipFromViewMatrix();
243
244 // ray end in world space
245 highp vec3 wsRayEnd = wsRayStart + wsRayDirection * wsRayLength;
246
247 // ray start/end in clip space
248 highp vec4 csRayStart = worldToClip * vec4(wsRayStart, 1.0);
249 highp vec4 csRayEnd = worldToClip * vec4(wsRayEnd, 1.0);
250 highp vec4 csViewRayEnd = csRayStart + viewToClip * vec4(0.0, 0.0, wsRayLength, 0.0);
251
252 // ray start/end in screen space
253 ray.ssRayStart = csRayStart.xyz * (1.0 / csRayStart.w);
254 ray.ssRayEnd = csRayEnd.xyz * (1.0 / csRayEnd.w);
255 ray.ssViewRayEnd = csViewRayEnd.xyz * (1.0 / csViewRayEnd.w);
256
257 // convert all to uv (texture) space
258 highp vec3 uvRayEnd = ray.ssRayEnd.xyz * 0.5 + 0.5;
259 ray.uvRayStart = ray.ssRayStart.xyz * 0.5 + 0.5;
260 ray.uvRay = uvRayEnd - ray.uvRayStart;
261 }
262
263 float screenSpaceContactShadow(vec3 lightDirection) {
264 // cast a ray in the direction of the light
265 float occlusion = 0.0;
266 uint kStepCount = (frameUniforms.directionalShadows >> 8u) & 0xFFu;
267 float kDistanceMax = frameUniforms.ssContactShadowDistance;
268
269 ScreenSpaceRay rayData;
270 initScreenSpaceRay(rayData, shading_position, lightDirection, kDistanceMax);
271
272 // step
273 highp float dt = 1.0 / float(kStepCount);
274
275 // tolerance
276 float tolerance = abs(rayData.ssViewRayEnd.z - rayData.ssRayStart.z) * dt * 0.5;
277
278 // dither the ray with interleaved gradient noise
279 const vec3 m = vec3(0.06711056, 0.00583715, 52.9829189);
280 float dither = fract(m.z * fract(dot(gl_FragCoord.xy, m.xy))) - 0.5;
281
282 // normalized position on the ray (0 to 1)
283 float t = dt * dither + dt;
284
285 highp vec3 ray;
286 for (uint i = 0u ; i < kStepCount ; i++, t += dt) {
287 ray = rayData.uvRayStart + rayData.uvRay * t;
288 float z = 1.0 - textureLod(light_structure, uvToRenderTargetUV(ray.xy), 0.0).r;
289 float dz = ray.z - z;
290 if (abs(tolerance - dz) < tolerance) {
291 occlusion = 1.0;
292 break;
293 }
294 }
295
296 // we fade out the contribution of contact shadows towards the edge of the screen
297 // because we don't have depth data there
298 vec2 fade = max(12.0 * abs(ray.xy - 0.5) - 5.0, 0.0);
299 occlusion *= saturate(1.0 - dot(fade, fade));
300 return occlusion;
301 }
302
303 //------------------------------------------------------------------------------
304 // VSM
305 //------------------------------------------------------------------------------
306
307 highp float linstep(const highp float a, const highp float b, const highp float v) {
308 return clamp((v - a) / (b - a), 0.0, 1.0);
309 }
310
311 highp float reduceLightBleed(const highp float pMax, const highp float amount) {
312 return linstep(amount, 1.0, pMax);
313 }
314
315 highp float chebyshevUpperBound(const highp vec2 moments, const highp float mean,
316 const highp float minVariance, const highp float lightBleedReduction) {
317 // Donnelly and Lauritzen 2006, "Variance Shadow Maps"
318
319 highp float variance = moments.y - (moments.x * moments.x);
320 variance = max(variance, minVariance);
321
322 highp float d = mean - moments.x;
323 highp float pMax = variance / (variance + d * d);
324 pMax = reduceLightBleed(pMax, lightBleedReduction);
325
326 return mean <= moments.x ? 1.0 : pMax;
327 }
328
329 //------------------------------------------------------------------------------
330 // Shadow sampling dispatch
331 //------------------------------------------------------------------------------
332
333 /**
334 * Samples the light visibility at the specified position in light (shadow)
335 * space. The output is a filtered visibility factor that can be used to multiply
336 * the light intensity.
337 */
338 float shadow(const mediump sampler2DArrayShadow shadowMap, const uint layer, vec3 shadowPosition) {
339 vec2 size = vec2(textureSize(shadowMap, 0));
340 // note: shadowPosition.z is in the [1, 0] range (reversed Z)
341 #if SHADOW_SAMPLING_METHOD == SHADOW_SAMPLING_PCF_HARD
342 return ShadowSample_Hard(shadowMap, layer, size, shadowPosition);
343 #elif SHADOW_SAMPLING_METHOD == SHADOW_SAMPLING_PCF_LOW
344 return ShadowSample_PCF_Low(shadowMap, layer, size, shadowPosition);
345 #elif SHADOW_SAMPLING_METHOD == SHADOW_SAMPLING_PCF_MEDIUM
346 return ShadowSample_PCF_Medium(shadowMap, layer, size, shadowPosition);
347 #elif SHADOW_SAMPLING_METHOD == SHADOW_SAMPLING_PCF_HIGH
348 return ShadowSample_PCF_High(shadowMap, layer, size, shadowPosition);
349 #endif
350 }
351
352 highp float shadowVsm(const highp sampler2DArray shadowMap, const uint layer,
353 const highp vec3 shadowPosition) {
354 highp vec2 moments = texture(shadowMap, vec3(shadowPosition.xy, layer)).xy;
355 highp float depth = shadowPosition.z;
356
357 // depth must be clamped to support floating-point depth formats. This is to avoid comparing a
358 // value from the depth texture (which is never greater than 1.0) with a greater-than-one
359 // comparison value (which is possible with floating-point formats).
360 depth = min(depth, 1.0f);
361
362 // TODO: bias and lightBleedReduction should be uniforms
363 const float bias = 0.01;
364 const float lightBleedReduction = 0.2;
365
366 const float minVariance = bias * 0.001;
367 return chebyshevUpperBound(moments, depth, minVariance, lightBleedReduction);
368 }