Debian Codegraph @

shaders/src/depth_main.fs

raw
1 #if defined(HAS_VSM)
2 layout(location = 0) out vec4 fragColor;
3 #endif
4
5 //------------------------------------------------------------------------------
6 // Depth
7 //------------------------------------------------------------------------------
8
9 void main() {
10 #if defined(BLEND_MODE_MASKED)
11 MaterialInputs inputs;
12 initMaterial(inputs);
13 material(inputs);
14
15 float alpha = inputs.baseColor.a;
16 if (alpha < getMaskThreshold()) {
17 discard;
18 }
19 #endif
20
21 #if defined(HAS_VSM)
22 // For VSM, we use the linear light space Z coordinate as the depth metric, which works for both
23 // directional and spot lights.
24 // We negate it, because we're using a right-handed coordinate system (-Z points forward).
25 highp float depth = -mulMat4x4Float3(frameUniforms.viewFromWorldMatrix, vertex_worldPosition).z;
26
27 // Scale by cameraFar to help prevent a floating point overflow below when squaring the depth.
28 depth /= abs(frameUniforms.cameraFar);
29
30 highp float dx = dFdx(depth);
31 highp float dy = dFdy(depth);
32
33 // Output the first and second depth moments.
34 // The first moment is mean depth.
35 // The second moment is mean depth squared.
36 // These values are retrieved when sampling the shadow map to compute variance.
37 highp float bias = 0.25 * (dx * dx + dy * dy);
38 fragColor = vec4(depth, depth * depth + bias, 0.0, 0.0);
39 #endif
40 }