Support CameraComponent#reverseZ on WebGL2 through EXT_clip_control
#10013 adds per-camera reverse-z (CameraComponent#reverseZ) on WebGPU only. This issue tracks adding it to WebGL2.
Why it needs EXT_clip_control
A reversed result alone works on any WebGL2 device: depth test GEQUAL, clear depth to 0, flip the depth bias signs, and the REVERSE_Z shader define. But without clip control it gains no precision. WebGL clip space is -1..1, and the hardware maps it to window depth with 0.5 * z + 0.5 after the perspective divide. Distant depths sit near -1 in clip space, where float32 steps are about 1.2e-7, so the small values reverse-z relies on are lost before they reach the depth buffer.
With EXT_clip_control and clipControlEXT(LOWER_LEFT_EXT, ZERO_TO_ONE_EXT), WebGL clip space becomes 0..1, the same as WebGPU. Then the reversed projection and the GLSL chunk changes from #10013 apply unchanged.
Proposed approach
- Gate reverse-z on a device capability,
GraphicsDevice#supportsReverseZ(suggested in the review of #10013):trueon WebGPU, and on WebGL2 whenEXT_clip_controlis available. - Without the extension, keep today's behaviour: the setting is ignored (with a debug warning), and
Camera#reverseZreads backfalseso apps can detect it. Don't fall back to a reversed result without the extension; it costs extra shader variants and a code path to test, and gains nothing. - Clip control is global GL state. Turn on
ZERO_TO_ONE_EXTonly for the render passes of a reverse-z camera and restoreNEGATIVE_ONE_TO_ONE_EXTafterwards. Turning it on globally would break the GLSL paths that assume -1..1 clip space (shadows, depth decoding, SSAO and others). - In
Renderer#setCameraUniforms, apply the depth range transform for reverse-z cameras on WebGL too (webgpu || reverseZ), since those passes render with 0..1 clip space. - On context loss, get the extension again on restore and set the clip control state again.
- Review the GLSL chunks touched in #10013: their comments say "near plane on WebGPU", which also applies to WebGL with clip control.
Notes
- Support: Chrome and Edge have the extension, and Safari has shipped it since late 2023. Firefox doesn't yet (bug 1908745). ANGLE turns it off on some Mali GPUs.
- The default WebGL canvas depth buffer is 24-bit integer, where reverse-z gains little. The benefit needs a render target with a float depth buffer (
PIXELFORMAT_DEPTH,DEPTH_COMPONENT32F). - The readers of the camera's depth (scene depth map decode, CameraFrame prepass, picker, gaussian splats) follow the same order as the WebGPU follow-ups to #10013.
Hi @mvaligursky, I'd like to work on this! Following your proposed approach and building on #10013:
- Add
GraphicsDevice#supportsReverseZ(trueon WebGPU, and on WebGL2 whenEXT_clip_controlis available;falseon WebGL2 without the extension / NullGraphicsDevice). - Gate
CameraComponent#reverseZ/Camera#reverseZondevice.supportsReverseZ(ignoring with a debug warning and reading backfalsewhen unsupported). - Query
EXT_clip_controlinWebglGraphicsDevice, toggleclipControlEXT(LOWER_LEFT_EXT, ZERO_TO_ONE_EXT)only during reverse-Z camera render passes, and restoreNEGATIVE_ONE_TO_ONE_EXTafterwards (skipping anyEXT_clip_controlqueries/calls on context lost). - Update
Renderer#setCameraUniforms(and any WebGL2 reverse-Z depth/clear paths) to apply the0..1depth range transform whendevice.isWebGPU || reverseZ, and add unit tests.
@SashaRX is working on reverseZ features currently, please hold off working on this as well @Sauvi72