Skip to main content

Module depth

Module depth 

Source
Expand description

The depth attachment the opaque strip pass establishes and the alpha pass tests against.

The engine’s early-z scheme needs one DEPTH_FORMAT attachment matching the frame’s target extent. Two things own such an attachment, and which one does is the caller’s call, not the engine’s:

  1. The caller. A host that already ran a 3D pass into the same target has a depth buffer with meaningful contents, and the 2D pass has to test against it rather than against one of its own. That caller passes it in as EngineTarget::depth, and — because it has usually just cleared the buffer for its own pass — tells the engine so through DepthAttachment::set_pre_cleared, which turns the frame’s depth clear into a load. Clearing a depth buffer a 3D pass just populated would throw that pass’s occlusion away.
  2. The engine. With no caller-supplied attachment the engine allocates one lazily on the first frame that needs it and keeps it across frames, reallocating only when the extent changes (DepthAttachment::resize).

The texture itself is plain RENDER_ATTACHMENT, single mip, single sample: nothing ever samples or copies it, so it needs no other usage, and a downlevel target could not offer one anyway.

§What a caller sharing the buffer has to agree with

Three facts, and together they are the whole contract — a renderer that honours them records its own passes into the same encoder, against this same attachment, and gets correct occlusion in either order.

  • The comparison, and which end is near. DEPTH_COMPARE is LessEqual and DEPTH_CLEAR is the far plane, so a nearer fragment carries a smaller z. The engine’s own draws sit at the back of that range — strip.wgsl maps the backmost draw to z = 1.0 and the rest towards 1.0 - painter_index / 2^24 — so anything a caller writes in front of the far plane occludes the 2D frame wherever it lands.
  • The extent. A depth attachment must match its colour attachment’s extent exactly; wgpu refuses the pass otherwise. depth_texture_descriptor is the descriptor the engine itself would have used, so a caller allocating the shared buffer through it agrees by construction rather than by arithmetic of its own.
  • Who clears. Whichever pass runs first owns the clear and the other loads; the engine’s half of that statement is DepthAttachment::set_pre_cleared. The frame’s colour clear is not negotiable the same way — a frame always clears its colour target to the base colour — so content painted into that target ahead of the frame keeps its depth and loses its pixels, and a host that needs it visible records it after the frame rather than before.

Re-exports§

pub use super::pipelines::DEPTH_FORMAT;

Structs§

DepthAttachment
The engine’s depth attachment across frames: an owned texture when the caller supplies none, plus the caller’s pre-cleared statement.
DepthTexture
A depth texture the engine allocated and keeps across frames.

Constants§

DEPTH_CLEAR
The depth value a frame clears its attachment to.
DEPTH_COMPARE
The comparison every depth-testing engine pipeline runs.
DEPTH_USAGE
The usage every depth attachment is created with.

Functions§

depth_load_op
The load operation a frame’s first depth-using pass applies.
depth_texture_descriptor
The descriptor for a depth attachment covering a width x height target.