Skip to main content

Depth interval filtering

depthInterval and multisampledDepthInterval are WebGPU shader modules exported by @luma.gl/shaders/modules. Use the first with a single-sample depth texture and the second with an MSAA depth texture.

Select one module when creating the pipeline. Both variants expose the WGSL function depthInterval_filterCoverageMask(pixel, depth, coverage). It compares fragment depth against the bound depth texture and returns the surviving bits of the incoming coverage mask.

import {Model} from '@luma.gl/engine/api';
import {depthInterval, multisampledDepthInterval} from '@luma.gl/shaders/modules';

const model = new Model(device, {
source,
modules: [sampleCount > 1 ? multisampledDepthInterval : depthInterval],
parameters: {sampleCount},
bindings: {depthIntervalBound: depthTexture, depthIntervalCompare: compareBuffer}
});

Both bindings use group 1. Supply a depth texture matching the framebuffer dimensions and a 16-byte uniform buffer containing a vec4<u32>. Set its first component to zero to keep depths less than or equal to the bound, or nonzero to keep greater depths. Update the comparison buffer between draws to select the other side of the interval.

Pass framebuffer pixel coordinates, depth, and the host coverage mask to the helper. With a single sample, discard the fragment when the returned mask is zero. With MSAA, write the returned mask to the fragment output's @builtin(sample_mask) so each covered sample keeps its own comparison result.

For example, suppose the fragment depth is 0.5 and the four sample bounds are [0.25, 0.5, 0.75, 0.5]. The less-or-equal comparison passes samples 1, 2, and 3 (0b1110). With incoming coverage 0b0101, the returned mask is 0b0100: only sample 2 survives.