Repository navigation
fix(core): do not write View clearColor into the picking buffer - #10431
Conversation
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.
chrisgervang
left a comment
There was a problem hiding this comment.
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.
| 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; |
There was a problem hiding this comment.
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.
| 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); | ||
| }); |
There was a problem hiding this comment.
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.
| }); | |
| }); | |
| 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.
Thanks for the review. Good catch on the |
|
Thanks! LGTM. The non-picking path should be equivalent after this change. Here's what how I expect the
|
* 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]>
Closes #10430
Background
Since #9650,
LayersPass._drawLayersInViewportapplies theViewclear/clearColorprops to every render target, including the picking framebuffer used byPickLayersPass. 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 withinpickingRadiusfails whenever the pointer is not exactly on an object, and every hover/click over the background logsPicked non-existent layer. Is picking buffer corrupt?.Change List
clearColor: falseis still honored while picking.MapViewusingclearColor: [255, 255, 255, 255]and asserts that background pixels decode to "no object" while the object is still picked. Fails on master.clearColor: falseboundary: two overlapping views where the top view usesclear: 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
Viewusesclearwith a non-transparentclearColor. View clears were being applied to the picking framebuffer, so background pixels no longer decoded as “no object” and hover/click withinpickingRadiuscould fail.LayersPass._drawLayersInViewportnow receivesisPickingand, while picking, ignores the view’sclearColorso the buffer stays cleared to transparent black;clearColor: falseis still respected. TwoPickLayersPasstests cover the white-clear regression and overlapping views withclearColor: false.Reviewed by Cursor Bugbot for commit c845eb0. Bugbot is set up for automated code reviews on this repo. Configure here.