Log inGet started
Package

zfighting.renderFeature

Newest version

This package holds more than this page lists. See all of its files in its world.

README.md

zfighting render feature

Draws the zfighting capture view: where two opaque surfaces sit close enough together that the frame's own depth rounding decides which one shows.

A full-screen fill blacks out the lit scene, a geometry pass over @geometry.opaque re-runs the depth test the scene already resolved (lessEqual, writing no depth of its own) and blends a quarter-unit additively into the scene target, and a full-screen pass turns the resulting count into a verdict colour. The count accumulates into the scene target rather than a private one because only that target carries the scene's depth buffer, and the depth test is the whole measurement.

The counting pass carries a depth bias, which is what makes the measurement match what the frame does. Unbiased, the test is exact to the last step the buffer holds, so it counts a second surface only where the two land on the identical value — a contour through the fight rather than the fight. The bias widens the test to a band as deep as the rounding the frame puts into a surface's depth:

  • 32 depth-buffer steps, which covers how far apart two coplanar surfaces drawn through different transforms land when seen head-on. A step is a share of the depth at the nearest point of the fragment's triangle, so the band keeps one relative width at every range: four to eight millionths of that point's distance, a hundredth to two hundredths of a millimetre at 3 m. On a triangle running away from the camera it reaches further at the far end, by the square of how much further off that is.
  • 1/16 of how far depth runs across the fragment's pixel, which covers what the rasterizer's sub-pixel grid and the temporal resolve's sub-pixel camera offset move a surface seen at a glancing angle by.

Two coplanar surfaces trade places inside that band and are counted from every angle; two surfaces a few millimetres apart hold their order in every frame and read as one, at a glancing angle as much as head-on.

Each surface is counted at the depth bias its own material states (depthBias, depthBiasSlope), added to the band, so a decal or an overlay its material lifts toward the camera is counted where the frame drew it. Lifted past the band — a depthBias of -64 or more for its constant part, and a depthBiasSlope of -1 or more for the part that grows at a glancing angle — it reads as the one surface it is; lifted less, it reads red where the band still reaches the surface under it. A depthBias with no slope clears the band only where the overlay faces the camera. Where two surfaces cross, the pixels on the crossing itself are the ones both reach, and those read red one pixel at a time.

Verdict: near-black no opaque surface, dark grey one surface (healthy), red two surfaces the buffer cannot separate, white three or more.

All three passes carry the view's debug channel, so none draws for a camera that is not a capture of pass = "zfighting". The feature is created on demand by @builtin::systems.renderDiagnostics.renderDiagnostics, builds its materials and map effect as it is created, and the capture that created it waits until they can draw, so the first capture of the view in a session is the view.