[Bug] PostProcessEffect disables depth testing for 3D layers: render buffers have no depth attachment

Open Beginner friendly
#10,981 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
77/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
typescript

Research direction

Start in modules/core/src/lib/deck-renderer.ts at DeckRenderer._resizeRenderBuffers and inspect how the framebuffer for renderBuffers[0] is created and resized. Then find the existing render tests for post-process effects and add coverage for occlusion between overlapping 3D layers with an effect active. Done means the nearer layer still occludes the farther one, including after a canvas resize.

Written by the indexing model from the issue text.

Description

Description

When a PostProcessEffect is active, 3D layers lose depth testing. A far object draws over a near object when the far layer draws second.

The cause is in DeckRenderer._resizeRenderBuffers (modules/core/src/lib/deck-renderer.ts). When there is a post-process effect, LayersPass draws into renderBuffers[0]. That framebuffer has a color attachment only:

this.device.createFramebuffer({
  id: `deck-renderbuffer-${i}`,
  colorAttachments: [texture]
})

With no depth attachment, the depth test has nothing to test against. The effect does not need to change any pixel. A pass that returns its input color unchanged is sufficient to cause the problem.

Motivation

We render a 3D vehicle model with ScenegraphLayer in an OrbitView, and we added a tone-mapping pass as a PostProcessEffect. When the pass was on, rings and rails behind the hull showed through the hull. Any 3D scene that uses a post-process effect (for example, a color grade, tone mapping or a screen-space effect from @luma.gl/effects) has the same problem.

Flavors
  • Script tag (dist.min.js)
  • React (@deck.gl/react)
Expected Behavior

A post-process effect changes only the colors of the final frame. Occlusion between 3D layers stays the same as without the effect.

Steps to Reproduce

Two extruded boxes in an OrbitView. The red box is nearer to the camera. The blue box is behind it and draws second. Open the page as is, then with ?postfx.

  • No effects: the red box hides the lower part of the blue box (correct).
  • ?postfx: the blue box draws over the red box (incorrect).
repro.html (deck.gl 9.4.1 from unpkg)
<!doctype html>
<html><head><meta charset="utf-8">
<script src="https://unpkg.com/[email protected]/dist.min.js"></script>
<style>html,body{margin:0;height:100%;background:#fff}</style></head>
<body><script>
const {Deck, OrbitView, SolidPolygonLayer, PostProcessEffect, COORDINATE_SYSTEM} = deck;
const identity = {
  name: 'identity',
  fs: `vec4 identity_filterColor_ext(vec4 color, vec2 texSize, vec2 texCoord) { return color; }`,
  passes: [{filter: true}]
};
const square = (x, y, s) => [[x - s, y - s], [x + s, y - s], [x + s, y + s], [x - s, y + s]];
// Red box is nearer the camera; blue box is behind it, drawn second.
const layers = [
  new SolidPolygonLayer({id: 'near-red', data: [square(0, -30, 20)], getPolygon: d => d,
    extruded: true, getElevation: 40, getFillColor: [220, 40, 40], coordinateSystem: COORDINATE_SYSTEM.CARTESIAN}),
  new SolidPolygonLayer({id: 'far-blue', data: [square(0, 30, 20)], getPolygon: d => d,
    extruded: true, getElevation: 40, getFillColor: [40, 80, 220], coordinateSystem: COORDINATE_SYSTEM.CARTESIAN})
];
const params = new URLSearchParams(location.search);
new Deck({
  views: new OrbitView(),
  initialViewState: {target: [0, 0, 20], rotationX: 20, rotationOrbit: 0, zoom: 1.5},
  controller: true,
  layers,
  effects: params.has('postfx') ? [new PostProcessEffect(identity, {})] : [],
  // ?fix: the proposed change, applied from outside.
  onDeviceInitialized: device => {
    if (!params.has('fix')) return;
    const create = device.createFramebuffer.bind(device);
    device.createFramebuffer = props =>
      create(props.id?.startsWith('deck-renderbuffer') ? {...props, depthStencilAttachment: 'depth16unorm'} : props);
  }
});
</script></body></html>
Proposed fix

Give the render buffers a depth attachment:

this.device.createFramebuffer({
  id: `deck-renderbuffer-${i}`,
  colorAttachments: [texture],
  depthStencilAttachment: 'depth16unorm'
})

We tested this with the ?postfx&fix flags in the repro above. Occlusion is correct again, and it stays correct after a canvas resize. Framebuffer.resize() clones the depth attachment with the color attachment, and LayersPass already clears depth when it clears the canvas.

Open points for a PR:

  • Only renderBuffers[0] receives layer draws. renderBuffers[1] holds only the output of the passes, so it may not need a depth attachment.
  • depth16unorm can be too coarse for large scenes. depth24plus is a possible alternative, and it is the usual choice on WebGPU.

I can open a PR with this change and a render test.

Environment
  • Framework version: deck.gl 9.4.1 (also 9.3.7). The same code is on master today.
  • Browser: Chrome (Chromium, ANGLE Metal)
  • OS: macOS
Logs

No console errors or warnings.

Dominant language
TypeScript
Stars
14.8k
Forks
2.3k
Avg merge
1d 9h
Merged PRs (30d)
230

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from visgl/deck.gl

All issues in visgl/deck.gl

Similar issues

More TypeScript issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.