engine icon indicating copy to clipboard operation
engine copied to clipboard

Support CameraComponent#reverseZ on WebGL2 through EXT_clip_control

Open mvaligursky opened this issue 2 weeks ago • 2 comments

#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): true on WebGPU, and on WebGL2 when EXT_clip_control is available.
  • Without the extension, keep today's behaviour: the setting is ignored (with a debug warning), and Camera#reverseZ reads back false so 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_EXT only for the render passes of a reverse-z camera and restore NEGATIVE_ONE_TO_ONE_EXT afterwards. 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.

mvaligursky avatar Nov 16 '26 12:11 mvaligursky

Hi @mvaligursky, I'd like to work on this! Following your proposed approach and building on #10013:

  1. Add GraphicsDevice#supportsReverseZ (true on WebGPU, and on WebGL2 when EXT_clip_control is available; false on WebGL2 without the extension / NullGraphicsDevice).
  2. Gate CameraComponent#reverseZ / Camera#reverseZ on device.supportsReverseZ (ignoring with a debug warning and reading back false when unsupported).
  3. Query EXT_clip_control in WebglGraphicsDevice, toggle clipControlEXT(LOWER_LEFT_EXT, ZERO_TO_ONE_EXT) only during reverse-Z camera render passes, and restore NEGATIVE_ONE_TO_ONE_EXT afterwards (skipping any EXT_clip_control queries/calls on context lost).
  4. Update Renderer#setCameraUniforms (and any WebGL2 reverse-Z depth/clear paths) to apply the 0..1 depth range transform when device.isWebGPU || reverseZ, and add unit tests.

Sauvi72 avatar Nov 26 '26 10:11 Sauvi72

@SashaRX is working on reverseZ features currently, please hold off working on this as well @Sauvi72

mvaligursky avatar Nov 26 '26 11:11 mvaligursky