Skip to content

fix(core): do not write View clearColor into the picking buffer - #10431

Merged
chrisgervang merged 5 commits into
visgl:masterfrom
jarnoharno:fix/picking-view-clear-color
Jul 14, 2026
Merged

chrisgervang merged 5 commits into
visgl:masterfrom
jarnoharno:fix/picking-view-clear-color

Conversation

@jarnoharno

@jarnoharno jarnoharno commented Jul 9, 2026 •

Copy link
Copy Markdown
Contributor

Closes #10430

Background

Since #9650, LayersPass._drawLayersInViewport applies the View clear/clearColor props to every render target, including the picking framebuffer used by PickLayersPass. The picking buffer encodes picked object references as colors (alpha encodes the layer index), so clearing it with any color whose alpha is non-zero makes every background pixel decode as a picked object of a non-existent layer. As a result, picking within pickingRadius fails whenever the pointer is not exactly on an object, and every hover/click over the background logs Picked non-existent layer. Is picking buffer corrupt?.

Change List

  • When picking, ignore the view's clearColor and let the clear fall through to transparent black, so background pixels keep decoding as "no object". clearColor: false is still honored while picking.
  • Add two tests:
    • A regression test that renders the picking buffer with a MapView using clearColor: [255, 255, 255, 255] and asserts that background pixels decode to "no object" while the object is still picked. Fails on master.
    • A guard for the clearColor: false boundary: two overlapping views where the top view uses clear: true + clearColor: false, asserting the bottom view's picked object survives. This is not a regression relative to master; it prevents the picking exception from being over-applied.

Note

Medium Risk
Touches shared layer rendering/clear logic used by picking; behavior change is scoped to picking mode with tests, but incorrect clear handling could still affect multi-view picking.

Overview
Fixes picking corruption when a View uses clear with a non-transparent clearColor. View clears were being applied to the picking framebuffer, so background pixels no longer decoded as “no object” and hover/click within pickingRadius could fail.

LayersPass._drawLayersInViewport now receives isPicking and, while picking, ignores the view’s clearColor so the buffer stays cleared to transparent black; clearColor: false is still respected. Two PickLayersPass tests cover the white-clear regression and overlapping views with clearColor: false.

Reviewed by Cursor Bugbot for commit c845eb0. Bugbot is set up for automated code reviews on this repo. Configure here.

Since visgl#9650, the View clear/clearColor props were applied to every
render target, including the picking framebuffer. Picking colors encode
object references, so clearing with a non-transparent color makes every
background pixel decode as a picked object, breaking picks within the
picking radius and logging "Picked non-existent layer" errors. Clear
with transparent black instead when picking.
@coveralls

coveralls commented Jul 10, 2026 •

Copy link
Copy Markdown

Coverage Status

coverage: 83.166% (+0.001%) from 83.165% — jarnoharno:fix/picking-view-clear-color into visgl:master

@chrisgervang chrisgervang left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey, thanks for taking the time to fix this issue and adding a test to prevent it from recurring.

The fix looks well scoped. While looking for edge case, Claude found one I think is worth fixing to preserve semantics, and I had it add a comment for you to review.

Comment thread modules/core/src/passes/layers-pass.ts Outdated
Comment on lines 282 to 290
if (isPicking) {
// The picking buffer encodes object references as colors. Clearing with the
// view's clearColor would fill the background with a color that decodes to a
// (non-existent) picked object, so clear with transparent black instead.
colorToUse = [0, 0, 0, 0];
} else if (Array.isArray(clearColor)) {
colorToUse = [...clearColor.slice(0, 3), clearColor[3] || 255].map(
c => c / 255
) as NumberArray4;

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Small refinement to preserve clearColor semantics during picking: rather than unconditionally clearing to transparent black, only substitute it when clearColor is an actual color array. That way a view with clear: true + clearColor: false still means "clear depth/stencil but don't touch color" while picking — the picking FBO is already cleared to transparent black at the top level (pick-layers-pass.ts), so the background still decodes as "no object" (alpha 0), and any overlapping-view picking data underneath is preserved, as clearColor: false promises.

Verified locally (headless Chromium): the new regression test still passes, and a clear: true + clearColor: false case also stays uncorrupted.

Suggested change
if (isPicking) {
// The picking buffer encodes object references as colors. Clearing with the
// view's clearColor would fill the background with a color that decodes to a
// (non-existent) picked object, so clear with transparent black instead.
colorToUse = [0, 0, 0, 0];
} else if (Array.isArray(clearColor)) {
colorToUse = [...clearColor.slice(0, 3), clearColor[3] || 255].map(
c => c / 255
) as NumberArray4;
if (Array.isArray(clearColor)) {
// The picking buffer encodes object references in the color's alpha channel.
// Clearing it with the view's clearColor would make the background decode as a
// (non-existent) picked object, so clear with transparent black while picking.
// Still honor `clearColor: false` (below) so "don't clear color" is preserved.
colorToUse = isPicking
? [0, 0, 0, 0]
: ([...clearColor.slice(0, 3), clearColor[3] || 255].map(
c => c / 255
) as NumberArray4);

Generated by Claude Code

const centerColor = pixels.slice(centerIndex, centerIndex + 4);
const pickedObject = decodePickingColor!(centerColor);
expect(pickedObject?.pickedLayer?.id, 'object is still picked at the center').toBe(layer.id);
});

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you take the clearColor: false refinement in the other comment, here's a regression test that actually guards it. It uses two overlapping views where the top view has clear: true + clearColor: false, and asserts the bottom view's picked object survives at a pixel inside the top view's rect. I verified this fails against the current isPicking → [0,0,0,0] version (the object gets wiped, decodes to undefined) and passes with the refinement — so it's a true guard, not one that passes either way.

Suggested change
});
});
test('PickLayersPass#overlapping view clearColor:false preserves underlying picking', () => {
const pickingFBO = device.createFramebuffer({colorAttachments: ['rgba8unorm']});
pickingFBO.resize({width: 100, height: 100});
// Bottom view: full screen, no clear. Its object renders at screen (50, 50).
const viewA = new MapView({id: 'A'});
// Top view: right region x:40..100 with clear enabled but clearColor:false.
// Its rect covers (50, 50), so a forced color clear there would wipe A's pixel.
const viewB = new MapView({id: 'B', x: 40, width: 60, clear: true, clearColor: false});
const viewState = {longitude: 0, latitude: 0, zoom: 1};
const viewportA = viewA.makeViewport({width: 100, height: 100, viewState});
const viewportB = viewB.makeViewport({width: 100, height: 100, viewState});
const layer = new ScatterplotLayer({
data: [{position: [0, 0]}],
getPosition: d => d.position,
radiusMinPixels: 10,
pickable: true
});
const layerManager = new LayerManager(device, {viewport: viewportA});
const pickLayersPass = new PickLayersPass(device);
layerManager.setLayers([layer]);
const {decodePickingColor} = pickLayersPass.render({
viewports: [viewportA, viewportB],
views: {A: viewA, B: viewB},
// Render the object in view A only; view B just performs its clear.
layerFilter: ({viewport}) => viewport.id === 'A',
layers: layerManager.getLayers(),
onViewportActive: layerManager.activateViewport,
pickingFBO,
deviceRect: {x: 0, y: 0, width: 100, height: 100}
});
const pixels = device.readPixelsToArrayWebGL(pickingFBO, {
sourceX: 0,
sourceY: 0,
sourceWidth: 100,
sourceHeight: 100
});
// (50, 50) is inside view B's rect (x >= 40). With clearColor:false the color must be
// preserved, so view A's object is still picked there instead of being cleared away.
const centerIndex = (50 * 100 + 50) * 4;
const centerColor = pixels.slice(centerIndex, centerIndex + 4);
const pickedObject = decodePickingColor!(centerColor);
expect(
pickedObject?.pickedLayer?.id,
"bottom view's object survives the top view's clearColor:false clear"
).toBe(layer.id);
});

Generated by Claude Code

Per review feedback, only substitute transparent black for the view's
clearColor when a color clear was actually requested. A view with
clear: true and clearColor: false still means "clear depth/stencil but
don't touch color" during picking, preserving picking data rendered by
underlying overlapping views. Add a regression test with two
overlapping views that fails if the color is force-cleared.
@jarnoharno

Copy link
Copy Markdown
Contributor Author

Hey, thanks for taking the time to fix this issue and adding a test to prevent it from recurring.

The fix looks well scoped. While looking for edge case, Claude found one I think is worth fixing to preserve semantics, and I had it add a comment for you to review.

Thanks for the review. Good catch on the clearColor: false case. I applied the suggestion with a small simplification, letting the initial transparent colorToUse pass through when picking. I also added the suggested test. It doesn't fail on master, but it guards against an incorrect implementation. Both tests pass headless, and I confirmed the new test still fails against the previous unconditional version.

@chrisgervang

Copy link
Copy Markdown
Collaborator

Thanks! LGTM. The non-picking path should be equivalent after this change. Here's what how I expect the clearColor prop to behave after this lands:

clearColor picking result correct?
array no normalized color ✓ (matches master)
array yes [0,0,0,0] (falls through) ✓ no corruption
false no false (no color clear) ✓
false yes false (no color clear) ✓ semantics preserved
undefined either [0,0,0,0] ✓

@chrisgervang
chrisgervang merged commit 8426762 into visgl:master Jul 14, 2026
6 checks passed
@chrisgervang chrisgervang added this to the v9.3 patch releases milestone Jul 14, 2026
chrisgervang added a commit that referenced this pull request Jul 16, 2026
* fix(core): do not write View clearColor into the picking buffer

Since #9650, the View clear/clearColor props were applied to every
render target, including the picking framebuffer. Picking colors encode
object references, so clearing with a non-transparent color makes every
background pixel decode as a picked object, breaking picks within the
picking radius and logging "Picked non-existent layer" errors. Clear
with transparent black instead when picking.

* fix(core): preserve clearColor:false semantics while picking

Per review feedback, only substitute transparent black for the view's
clearColor when a color clear was actually requested. A view with
clear: true and clearColor: false still means "clear depth/stencil but
don't touch color" during picking, preserving picking data rendered by
underlying overlapping views. Add a regression test with two
overlapping views that fails if the color is force-cleared.

---------

Co-authored-by: Chris Gervang <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug] View clearColor corrupts the picking buffer, breaking object picking

3 participants