Directional-light shadows vanish with useReverseDepthBuffer when shadowFrustumSize is set (fixed-frustum projection never swaps near/far)

Hi all! One more from our reversed-depth WebGPU setup (the useReverseDepthBuffer engine option from #19010 is working great for us, thanks!).

In short: with useReverseDepthBuffer on, a DirectionalLight whose shadow uses a fixed frustum (shadowFrustumSize > 0) casts no shadow at all. The auto-extend frustum works. The cause is one function: DirectionalLight._setDefaultFixedFrustumShadowProjectionMatrix builds its ortho projection with minZ, maxZ and never swaps them for reversed depth, while every other shadow projection does.

Repro (Playground, WebGPU engine): https://playground.babylonjs.com/?webgpu#ERLD5W#1

A box over a ground, one directional light with a PCF shadow generator, and three toggles: reversed depth, fixed frustum, apply fix. Reversed depth + fixed frustum: no shadow under the box. Turn off either one, or press Apply fix: the shadow is back. (Checked in a Playground-like page on 9.32.0: 0 validation errors in all four states.)

Measured (9.32.0, Chrome, Intel Iris Xe, WebGPU; the shadow pixel’s RGB sum vs open ground 660):

Depth Frustum No filter PCF
standard auto-extend shadow (0) shadow (0)
standard fixed shadow (0) shadow (0)
reversed auto-extend shadow (0) shadow (0)
reversed fixed no shadow (660) no shadow (660)
reversed fixed, with the fix below shadow (0) shadow (0)

In our game (a large forest, the shadow box follows the camera, so a fixed frustum is the natural choice) the WebGPU picture with shadows matched the one without shadows (0.002-0.024% of pixels differ, noise); with the fix it matches WebGL2 with shadows to within 0.003% of pixels.

Where (master at c9e77a1189, unchanged since at least 9.12):

protected _setDefaultFixedFrustumShadowProjectionMatrix(matrix: Matrix): void {
    const activeCamera = this.getScene().activeCamera;
    Matrix.OrthoLHToRef(
        this.shadowFrustumSize,
        this.shadowFrustumSize,
        this.shadowMinZ !== undefined ? this.shadowMinZ : activeCamera ? activeCamera.minZ : Constants.ShadowMinZ,
        this.shadowMaxZ !== undefined ? this.shadowMaxZ : activeCamera ? activeCamera.maxZ : Constants.ShadowMaxZ,
        matrix,
        this.getScene().getEngine().isNDCHalfZRange
    );
}

Compare _setDefaultAutoExtendShadowProjectionMatrix a few lines below, which passes useReverseDepthBuffer ? maxZ : minZ, useReverseDepthBuffer ? minZ : maxZ, and SpotLight/PointLight, which do the same in PerspectiveFovLHToRef. The rest of the pipeline already expects a reversed map: DirectionalLight.getDepthMinZ/getDepthMaxZ return the reversed values, ShadowGenerator creates its depth texture with GREATER comparison (516) under reverse, the depth test is GEQUAL and the depth clear is 0. With the projection left in standard Z, the map keeps the farthest surface and the comparison runs the other way, so every receiver reads as lit.

Suggested fix (the same swap as the auto-extend path):

protected _setDefaultFixedFrustumShadowProjectionMatrix(matrix: Matrix): void {
    const activeCamera = this.getScene().activeCamera;
    const engine = this.getScene().getEngine();
    const minZ = this.shadowMinZ !== undefined ? this.shadowMinZ : activeCamera ? activeCamera.minZ : Constants.ShadowMinZ;
    const maxZ = this.shadowMaxZ !== undefined ? this.shadowMaxZ : activeCamera ? activeCamera.maxZ : Constants.ShadowMaxZ;
    const useReverseDepthBuffer = engine.useReverseDepthBuffer;
    Matrix.OrthoLHToRef(
        this.shadowFrustumSize,
        this.shadowFrustumSize,
        useReverseDepthBuffer ? maxZ : minZ,
        useReverseDepthBuffer ? minZ : maxZ,
        matrix,
        engine.isNDCHalfZRange
    );
}

Without reversed depth the matrix is unchanged. Until a release has it, light.customProjectionMatrixBuilder with that body is a complete workaround (that is what the Playground’s “Apply fix” does, and what we ship).

Happy to open a PR with the fix and a unit test (a NullEngine test that projects the near and far planes through setShadowProjectionMatrix with useReverseDepthBuffer on), if that helps.

(Screenshot attached: our forest on WebGPU, as shipped and with the fix.)

Thanks for the clear repro and investigation! Your diagnosis is correct: the fixed-frustum path passes near/far unchanged, while the auto-extend path swaps them when reversed depth is enabled. The shadow pipeline already expects reversed depth, so that missing swap explains the disappearing shadows.

The proposed fix looks right and leaves standard-depth behavior unchanged. This is a pre-existing issue rather than a regression introduced by the new engine constructor option.

A PR with the fix and a regression test would be welcome! Please make the implementation change in directionalLight.pure.ts. Testing through setShadowProjectionMatrix, with reversed depth on/off and both NDC depth ranges, would cover the projection behavior. For the NullEngine test, set engine.useReverseDepthBuffer explicitly rather than passing it as a constructor option.

The customProjectionMatrixBuilder workaround is also appropriate in the meantime.

Thanks for confirming, @Evgeni_Popov! The PR is up: Fix DirectionalLight fixed-frustum shadows under reverse depth by christabone · Pull Request #19056 · BabylonJS/Babylon.js · GitHub

The fix is in directionalLight.pure.ts: the fixed-frustum projection now swaps near/far under reverse depth, the same as the auto-extend path. The regression test goes through setShadowProjectionMatrix with reversed depth on and off and both NDC depth ranges, sets engine.useReverseDepthBuffer explicitly on a NullEngine, and also checks the fixed and auto-extend frustums give the same depths. The reverse-depth cases fail without the fix. We’ll use customProjectionMatrixBuilder in the meantime, thanks for the tip!