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.)
