Skip to content

Commit 6416cf1

Browse files
authored
remove sampleDepth(), inline the code instead. (#9806)
This hides less what actually happens, and allows us to save a few calls to saturate().
1 parent ddb2de1 commit 6416cf1

File tree

1 file changed

+34
-25
lines changed

1 file changed

+34
-25
lines changed

shaders/src/surface_shadowing.fs

Lines changed: 34 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -15,38 +15,30 @@
1515
// PCF Shadow Sampling
1616
//------------------------------------------------------------------------------
1717

18-
float sampleDepth(const mediump sampler2DArrayShadow map,
19-
const highp vec4 scissorNormalized,
20-
const uint layer, highp vec2 uv, highp float depth) {
21-
22-
// clamp needed for directional lights and/or large kernels
23-
uv = clamp(uv, scissorNormalized.xy, scissorNormalized.zw);
24-
25-
// depth must be clamped to support floating-point depth formats which are always in
26-
// the range [0, 1].
27-
return texture(map, vec4(uv, layer, saturate(depth)));
28-
}
29-
3018
// use hardware assisted PCF
3119
float ShadowSample_PCF_Hard(const mediump sampler2DArrayShadow map,
3220
const highp vec4 scissorNormalized,
3321
const uint layer, const highp vec4 shadowPosition) {
34-
highp vec3 position = shadowPosition.xyz * (1.0 / shadowPosition.w);
3522
// note: shadowPosition.z is in the [1, 0] range (reversed Z)
36-
return sampleDepth(map, scissorNormalized, layer, position.xy, position.z);
23+
highp vec3 position = shadowPosition.xyz * (1.0 / shadowPosition.w);
24+
position.xy = clamp(position.xy, scissorNormalized.xy, scissorNormalized.zw);
25+
position.z = saturate(position.z);
26+
return texture(map, vec4(position.xy, layer, position.z));
3727
}
3828

3929
// use hardware assisted PCF + 3x3 gaussian filter
4030
float ShadowSample_PCF_Low(const mediump sampler2DArrayShadow map,
4131
const highp vec4 scissorNormalized,
4232
const uint layer, const highp vec4 shadowPosition) {
43-
highp vec3 position = shadowPosition.xyz * (1.0 / shadowPosition.w);
33+
34+
highp vec2 size = vec2(frameUniforms.shadowAtlasResolution.x);
35+
highp vec2 texelSize = vec2(frameUniforms.shadowAtlasResolution.y);
36+
4437
// note: shadowPosition.z is in the [1, 0] range (reversed Z)
45-
highp vec2 size = vec2(textureSize(map, 0));
46-
highp vec2 texelSize = vec2(1.0) / size;
38+
highp vec3 position = shadowPosition.xyz * (1.0 / shadowPosition.w);
39+
position.z = saturate(position.z);
4740

4841
// Castaño, 2013, "Shadow Mapping Summary Part 1"
49-
highp float depth = position.z;
5042

5143
// clamp position to avoid overflows below, which cause some GPUs to abort
5244
position.xy = clamp(position.xy, vec2(-1.0), vec2(2.0));
@@ -65,12 +57,27 @@ float ShadowSample_PCF_Low(const mediump sampler2DArrayShadow map,
6557
u *= texelSize.x;
6658
v *= texelSize.y;
6759

60+
float w0 = uw.x * vw.x;
61+
float w1 = uw.y * vw.x;
62+
float w2 = uw.x * vw.y;
63+
float w3 = uw.y * vw.y;
64+
65+
highp vec2 uv0 = base + vec2(u.x, v.x);
66+
highp vec2 uv1 = base + vec2(u.y, v.x);
67+
highp vec2 uv2 = base + vec2(u.x, v.y);
68+
highp vec2 uv3 = base + vec2(u.y, v.y);
69+
70+
uv0 = clamp(uv0, scissorNormalized.xy, scissorNormalized.zw);
71+
uv1 = clamp(uv1, scissorNormalized.xy, scissorNormalized.zw);
72+
uv2 = clamp(uv2, scissorNormalized.xy, scissorNormalized.zw);
73+
uv3 = clamp(uv3, scissorNormalized.xy, scissorNormalized.zw);
74+
6875
float sum = 0.0;
69-
sum += uw.x * vw.x * sampleDepth(map, scissorNormalized, layer, base + vec2(u.x, v.x), depth);
70-
sum += uw.y * vw.x * sampleDepth(map, scissorNormalized, layer, base + vec2(u.y, v.x), depth);
71-
sum += uw.x * vw.y * sampleDepth(map, scissorNormalized, layer, base + vec2(u.x, v.y), depth);
72-
sum += uw.y * vw.y * sampleDepth(map, scissorNormalized, layer, base + vec2(u.y, v.y), depth);
73-
return sum * (1.0 / 16.0);
76+
sum += w0 * texture(map, vec4(uv0, layer, position.z));
77+
sum += w1 * texture(map, vec4(uv1, layer, position.z));
78+
sum += w2 * texture(map, vec4(uv2, layer, position.z));
79+
sum += w3 * texture(map, vec4(uv3, layer, position.z));
80+
return sum * 0.0625;
7481
}
7582

7683
// use manual PCF
@@ -79,8 +86,10 @@ float ShadowSample_PCF(const mediump sampler2DArray map,
7986
const uint layer, const highp vec4 shadowPosition) {
8087
highp vec3 position = shadowPosition.xyz * (1.0 / shadowPosition.w);
8188
// note: shadowPosition.z is in the [1, 0] range (reversed Z)
82-
highp vec2 tc = clamp(position.xy, scissorNormalized.xy, scissorNormalized.zw);
83-
return step(0.0, position.z - textureLod(map, vec3(tc, layer), 0.0).r);
89+
position.xy = clamp(position.xy, scissorNormalized.xy, scissorNormalized.zw);
90+
position.z = saturate(position.z);
91+
highp float depth = textureLod(map, vec3(position.xy, layer), 0.0).r;
92+
return step(0.0, position.z - depth);
8493
}
8594

8695
//------------------------------------------------------------------------------

0 commit comments

Comments
 (0)