Repository navigation
feat: flat common-space helpers for non-flat projections + view-matrix render tests - #10657
Conversation
… module Texture- and bounds-based extensions (mask, clip, fill patterns, terrain) index a FLAT common space: Web Mercator for geospatial views, cartesian for OrthographicView. Under GlobeView geometry.position is sphere XYZ, so every such extension currently needs its own PROJECTION_MODE_GLOBE branch (terrain has one; mask needs one) or is simply broken (clip, fill-style, brushing). Add two projection-generic functions next to project_globe_(): - project_globe_to_mercator_(vec3): the inverse of project_globe_ followed by project_mercator_, i.e. sphere XYZ -> absolute Mercator common space. - project_common_position_to_flat(vec3|vec4): common space -> flat common space. Identity for flat projection modes (so offset-relative positions under WEB_MERCATOR_AUTO_OFFSET pass through unchanged); inverts the sphere under GLOBE. This is the single GPU switch point: a future non-flat projection mode is inverted here, next to the forward branch in project_position(), and no extension needs to change. Document the new function in docs/api-reference/core/project.md. Co-Authored-By: Claude Fable 5.1 <[email protected]> Claude-Session: https://claude.ai/code/session_01FyzWaF5TmQPmGjh3Zr3XL9
…on_position_to_flat project.glsl.ts and project.wgsl.ts are kept in lockstep everywhere else; mirror the two new functions so WebGPU ports of the extensions have the same entry point. No WGSL consumer yet (mask/clip/fill-style have no WGSL path). WGSL has no overloading, so only the vec3 form is provided. Co-Authored-By: Claude Fable 5.1 <[email protected]> Claude-Session: https://claude.ai/code/session_01FyzWaF5TmQPmGjh3Zr3XL9
CPU twins of project_common_position_to_flat() in projection-utils.ts: - isFlatViewport(viewport): the single place in @deck.gl/extensions that tests for GlobeViewport (instanceof _GlobeViewport; swap for a projection mode comparison if core ever exports one). - projectToFlatCommon(layer, position, opts) / projectBoundsToFlatCommon(layer, bounds, opts): Layer.projectPosition with autoOffset on flat viewports (matches offset-relative geometry.position at zoom >= 12), absolute Mercator through the existing reference viewport on non-flat ones. Routing through Layer.projectPosition keeps coordinate systems, coordinateOrigin and modelMatrix handling identical to the GPU. - getFlatCommonOrigin(uniforms, viewport): project.commonOrigin on flat viewports, [0, 0] under globe where the flat space is absolute (for globe + meter-offsets commonOrigin is a sphere position and must not be re-added). - getRenderBounds() returns the full layer extent for non-flat viewports: getViewportBounds() would yield sphere coordinates there, which cannot be intersected with Mercator layer bounds. No behaviour change for shipped extensions: MaskEffect bails out under globe today and terrain guards the call with its own instanceof check. Existing helpers (getMercatorReferenceViewport, lngLatToMercatorCommon, joinLayerBounds, makeViewport) are unchanged. Unit spec pins the GPU/CPU pairing invariant on WebMercator (zoom 11.5 and 14), Globe and Orthographic viewports. Co-Authored-By: Claude Fable 5.1 <[email protected]> Claude-Session: https://claude.ai/code/session_01FyzWaF5TmQPmGjh3Zr3XL9
…ple views No extension render test runs under GlobeView today, and only fill/path cases run under OrthographicView. test/render/view-presets.ts expands one case into one TestCase per view preset so the same layers and extension are rendered under MapView, GlobeView and OrthographicView: - presets share the San Francisco view used by existing goldens; the globe preset uses the same lng/lat/zoom (GlobeViewport is calibrated to converge with Mercator), the orthographic preset is the pixel space of the map preset (toPosition/toBounds map lng/lat data into it). - the 'map' preset keeps the bare name and golden so existing goldens are reused; other presets append -globe / -orthographic. - a load-time guard rejects globe cases above zoom 12, where GlobeView silently hands off to WebMercatorViewport. - per-preset overrides (skip is merged, other fields replace) and a geospatial viewState override let specs keep goldens that use other views. Covered by a unit spec in the render project; adopted by extension specs in follow-up PRs. Co-Authored-By: Claude Fable 5.1 <[email protected]> Claude-Session: https://claude.ai/code/session_01FyzWaF5TmQPmGjh3Zr3XL9
Consumers pass toPosition() straight into getPosition accessors, which expect a Position tuple; returning number[] forced a cast at every call site. Return [number, number] from every preset instead. Co-Authored-By: Claude Fable 5.1 <[email protected]> Claude-Session: https://claude.ai/code/session_01FyzWaF5TmQPmGjh3Zr3XL9
|
…t twin Add project_common_position_to_flat_wrapped(position, referenceX) in GLSL and WGSL. The flat inverse of the globe projection has a seam at the antimeridian where x jumps by a world width; moving vertices to within half a world of a reference (a bounds centre, or the camera) keeps geometry that spans 180 degrees continuous. projectToFlatCommon now mirrors the GLOBE + METER_OFFSETS shader branch exactly (ENU displacement on the sphere, then the sphere's inverse), instead of going through lng/lat, which diverges quadratically with the offset. projectBoundsToFlatCommon unwraps lng/lat bounds whose left edge is east of their right edge so they can describe a region across the antimeridian. Co-Authored-By: Claude Fable 5.1 <[email protected]> Claude-Session: https://claude.ai/code/session_01FyzWaF5TmQPmGjh3Zr3XL9
a8649cc to
5539059
Compare
project_common_position_to_flat_wrapped now moves x to the world copy nearest the reference in every geospatial projection mode, matching the CPU helper that unwraps antimeridian bounds on flat maps too. The globe-only behaviour needed by periodic patterns, which must not jump at world boundaries on a flat map, becomes project_common_position_to_flat_continuous. Co-Authored-By: Claude Fable 5.1 <[email protected]> Claude-Session: https://claude.ai/code/session_01FyzWaF5TmQPmGjh3Zr3XL9
Treat bounds as crossing when the left edge is more than 180 degrees east of the right edge (the shorter arc is the one meant). Edges closer than that in the wrong order are re-ordered as before, so reversed bounds keep working. Co-Authored-By: Claude Fable 5.1 <[email protected]> Claude-Session: https://claude.ai/code/session_01FyzWaF5TmQPmGjh3Zr3XL9
Co-Authored-By: Claude Fable 5.1 <[email protected]> Claude-Session: https://claude.ai/code/session_01FyzWaF5TmQPmGjh3Zr3XL9
felixpalmer
left a comment
There was a problem hiding this comment.
I'm supportive of the general idea of this PR, but worry that we might hit precision issues. It's nice that these new functions are purely additive, so safe to add - but the followup PRs might break things, especially on some GPUs which we haven't tested on
|
|
||
| // Inverse of project_globe_ followed by a Mercator projection: converts a position in | ||
| // globe common space (sphere XYZ) into absolute Mercator common space. | ||
| vec2 project_globe_to_mercator_(vec3 spherePos) { |
There was a problem hiding this comment.
I think the math can be simplified here, already the fact that this converts using degrees() and project_mercator_ goes back using radians() smells off.
An AI suggestion (which I haven't verified):
// ln(tan(PI/4 + radians(89.9)/2)) — matches project_mercator_'s latitude clamp
const float MAX_MERCATOR_Y_ = 7.0439589847;
// Inverse of project_globe_ followed by a Mercator projection.
// Mercator y is atanh(sin(lat)), which has a cancellation-free closed form directly
from the
// sphere position, so latitude is never formed: asin's conditioning degrades as
1/cos(lat)
// (~100 m of error at 89.9 vs ~1 mm here). abs(z) keeps D + |z| away from cancellation
near
// the south pole; the sign puts it back.
vec2 project_globe_to_mercator_(vec3 spherePos) {
float h = max(length(spherePos.xy), 1e-20);
float D = length(spherePos);
float y = sign(spherePos.z) * min(log((D + abs(spherePos.z)) / h), MAX_MERCATOR_Y_);
// project_mercator_'s wrapLongitude branch is a no-op here: atan already returns
(-PI, PI]
float x = atan(spherePos.x, -spherePos.y);
return (vec2(x, y) + PI) * WORLD_SCALE;
}
| return referenceX + t - TILE_SIZE * floor(t / TILE_SIZE) - TILE_SIZE * 0.5; | ||
| } | ||
|
|
||
| // |
There was a problem hiding this comment.
These comments are much longer than what is normal for this file
There was a problem hiding this comment.
I'll take another pass
|
|
||
| // Moves a flat Mercator x to the world copy nearest to referenceX (within half a world width) | ||
| float project_wrap_flat_x_(float x, float referenceX) { | ||
| float t = x - referenceX + TILE_SIZE * 0.5; |
There was a problem hiding this comment.
Simpler?
return x - TILE_SIZE * floor((x - referenceX) / TILE_SIZE + 0.5);
| onAfterUpdate: ({layer}) => { | ||
| // The layer's own projection is sphere XYZ (radius GLOBE_RADIUS = 256)... | ||
| const sphere = layer.projectPosition(POSITION); | ||
| expect(Math.hypot(sphere[0], sphere[1], sphere[2])).toBeCloseTo(256, 3); |
There was a problem hiding this comment.
3 is an exceptional large tolerance, would expect more like 12. Same below
There was a problem hiding this comment.
Great catch, thanks for taking a close look at this. Your suggested formula is looking much better
… comments Compute Mercator y directly from the sphere position as log((D + |z|) / |xy|), which equals atanh(sin(lat)) and avoids forming the latitude with asin, whose conditioning degrades near the poles. Simplify the world-copy wrap to a single floor and trim the helper comments to the length used elsewhere in the module. Unit test tolerances tightened to 12 digits. Co-Authored-By: Claude Fable 5.1 <[email protected]> Claude-Session: https://claude.ai/code/session_01FyzWaF5TmQPmGjh3Zr3XL9
Co-Authored-By: Claude Fable 5.1 <[email protected]> Claude-Session: https://claude.ai/code/session_01FyzWaF5TmQPmGjh3Zr3XL9
Thanks! Yeah, are there situations where you can remember we've been bitten in the past? I have a Mac, Nvidia Windows, and an iPhone - in addition to the Linux render tests.. I'm curious how I might uncover surprises |
…x render tests (#10657) * feat(core): add project_common_position_to_flat to the project shader module Texture- and bounds-based extensions (mask, clip, fill patterns, terrain) index a FLAT common space: Web Mercator for geospatial views, cartesian for OrthographicView. Under GlobeView geometry.position is sphere XYZ, so every such extension currently needs its own PROJECTION_MODE_GLOBE branch (terrain has one; mask needs one) or is simply broken (clip, fill-style, brushing). Add two projection-generic functions next to project_globe_(): - project_globe_to_mercator_(vec3): the inverse of project_globe_ followed by project_mercator_, i.e. sphere XYZ -> absolute Mercator common space. - project_common_position_to_flat(vec3|vec4): common space -> flat common space. Identity for flat projection modes (so offset-relative positions under WEB_MERCATOR_AUTO_OFFSET pass through unchanged); inverts the sphere under GLOBE. This is the single GPU switch point: a future non-flat projection mode is inverted here, next to the forward branch in project_position(), and no extension needs to change. Document the new function in docs/api-reference/core/project.md. Co-Authored-By: Claude Fable 5.1 <[email protected]> Claude-Session: https://claude.ai/code/session_01FyzWaF5TmQPmGjh3Zr3XL9 * feat(core): WGSL parity for project_globe_to_mercator_ / project_common_position_to_flat project.glsl.ts and project.wgsl.ts are kept in lockstep everywhere else; mirror the two new functions so WebGPU ports of the extensions have the same entry point. No WGSL consumer yet (mask/clip/fill-style have no WGSL path). WGSL has no overloading, so only the vec3 form is provided. Co-Authored-By: Claude Fable 5.1 <[email protected]> Claude-Session: https://claude.ai/code/session_01FyzWaF5TmQPmGjh3Zr3XL9 * feat(extensions): flat common-space CPU helpers for non-flat viewports CPU twins of project_common_position_to_flat() in projection-utils.ts: - isFlatViewport(viewport): the single place in @deck.gl/extensions that tests for GlobeViewport (instanceof _GlobeViewport; swap for a projection mode comparison if core ever exports one). - projectToFlatCommon(layer, position, opts) / projectBoundsToFlatCommon(layer, bounds, opts): Layer.projectPosition with autoOffset on flat viewports (matches offset-relative geometry.position at zoom >= 12), absolute Mercator through the existing reference viewport on non-flat ones. Routing through Layer.projectPosition keeps coordinate systems, coordinateOrigin and modelMatrix handling identical to the GPU. - getFlatCommonOrigin(uniforms, viewport): project.commonOrigin on flat viewports, [0, 0] under globe where the flat space is absolute (for globe + meter-offsets commonOrigin is a sphere position and must not be re-added). - getRenderBounds() returns the full layer extent for non-flat viewports: getViewportBounds() would yield sphere coordinates there, which cannot be intersected with Mercator layer bounds. No behaviour change for shipped extensions: MaskEffect bails out under globe today and terrain guards the call with its own instanceof check. Existing helpers (getMercatorReferenceViewport, lngLatToMercatorCommon, joinLayerBounds, makeViewport) are unchanged. Unit spec pins the GPU/CPU pairing invariant on WebMercator (zoom 11.5 and 14), Globe and Orthographic viewports. Co-Authored-By: Claude Fable 5.1 <[email protected]> Claude-Session: https://claude.ai/code/session_01FyzWaF5TmQPmGjh3Zr3XL9 * test(render): add view-matrix helper for rendering a case under multiple views No extension render test runs under GlobeView today, and only fill/path cases run under OrthographicView. test/render/view-presets.ts expands one case into one TestCase per view preset so the same layers and extension are rendered under MapView, GlobeView and OrthographicView: - presets share the San Francisco view used by existing goldens; the globe preset uses the same lng/lat/zoom (GlobeViewport is calibrated to converge with Mercator), the orthographic preset is the pixel space of the map preset (toPosition/toBounds map lng/lat data into it). - the 'map' preset keeps the bare name and golden so existing goldens are reused; other presets append -globe / -orthographic. - a load-time guard rejects globe cases above zoom 12, where GlobeView silently hands off to WebMercatorViewport. - per-preset overrides (skip is merged, other fields replace) and a geospatial viewState override let specs keep goldens that use other views. Covered by a unit spec in the render project; adopted by extension specs in follow-up PRs. Co-Authored-By: Claude Fable 5.1 <[email protected]> Claude-Session: https://claude.ai/code/session_01FyzWaF5TmQPmGjh3Zr3XL9 * test(render): type view preset toPosition as a 2-tuple Consumers pass toPosition() straight into getPosition accessors, which expect a Position tuple; returning number[] forced a cast at every call site. Return [number, number] from every preset instead. Co-Authored-By: Claude Fable 5.1 <[email protected]> Claude-Session: https://claude.ai/code/session_01FyzWaF5TmQPmGjh3Zr3XL9 * feat(core, extensions): wrapped flat projection and exact meter-offset twin Add project_common_position_to_flat_wrapped(position, referenceX) in GLSL and WGSL. The flat inverse of the globe projection has a seam at the antimeridian where x jumps by a world width; moving vertices to within half a world of a reference (a bounds centre, or the camera) keeps geometry that spans 180 degrees continuous. projectToFlatCommon now mirrors the GLOBE + METER_OFFSETS shader branch exactly (ENU displacement on the sphere, then the sphere's inverse), instead of going through lng/lat, which diverges quadratically with the offset. projectBoundsToFlatCommon unwraps lng/lat bounds whose left edge is east of their right edge so they can describe a region across the antimeridian. Co-Authored-By: Claude Fable 5.1 <[email protected]> Claude-Session: https://claude.ai/code/session_01FyzWaF5TmQPmGjh3Zr3XL9 * feat(core): separate wrapped and continuous flat projection helpers project_common_position_to_flat_wrapped now moves x to the world copy nearest the reference in every geospatial projection mode, matching the CPU helper that unwraps antimeridian bounds on flat maps too. The globe-only behaviour needed by periodic patterns, which must not jump at world boundaries on a flat map, becomes project_common_position_to_flat_continuous. Co-Authored-By: Claude Fable 5.1 <[email protected]> Claude-Session: https://claude.ai/code/session_01FyzWaF5TmQPmGjh3Zr3XL9 * fix(extensions): only unwrap lng/lat bounds that cross the antimeridian Treat bounds as crossing when the left edge is more than 180 degrees east of the right edge (the shorter arc is the one meant). Edges closer than that in the wrong order are re-ordered as before, so reversed bounds keep working. Co-Authored-By: Claude Fable 5.1 <[email protected]> Claude-Session: https://claude.ai/code/session_01FyzWaF5TmQPmGjh3Zr3XL9 * docs: note the flat common-space project shader helpers in what's new Co-Authored-By: Claude Fable 5.1 <[email protected]> Claude-Session: https://claude.ai/code/session_01FyzWaF5TmQPmGjh3Zr3XL9 * refactor(core): closed-form globe to Mercator inverse, shorter shader comments Compute Mercator y directly from the sphere position as log((D + |z|) / |xy|), which equals atanh(sin(lat)) and avoids forming the latitude with asin, whose conditioning degrades near the poles. Simplify the world-copy wrap to a single floor and trim the helper comments to the length used elsewhere in the module. Unit test tolerances tightened to 12 digits. Co-Authored-By: Claude Fable 5.1 <[email protected]> Claude-Session: https://claude.ai/code/session_01FyzWaF5TmQPmGjh3Zr3XL9 * docs(extensions): shorten projection-utils comments to house style Co-Authored-By: Claude Fable 5.1 <[email protected]> Claude-Session: https://claude.ai/code/session_01FyzWaF5TmQPmGjh3Zr3XL9 --------- Co-authored-by: Claude <[email protected]> (cherry picked from commit ba16261)
|
Released in deck.gl 9.4.1. |
Base of the GlobeView extension series: #10658, #10659, #10660, #10661, #10662, #10663, #10664, #10666
Background
MaskExtension,ClipExtension,FillStyleExtensionandTerrainExtensionsample a flat common space (Web Mercator, or cartesian inOrthographicView), but underGlobeViewgeometry.positionis sphere XYZ andGlobeViewport.projectFlat()is an identity. Each extension either carries its ownPROJECTION_MODE_GLOBEbranch or renders incorrectly on the globe. This PR adds one shared GPU/CPU conversion so the fixes in the stacked PRs are one-line calls, and a render test helper to run the same case in several views. Additive only: no behavior change for shipped extensions, no new core exports.Change List
projectshader module (GLSL and WGSL): addproject_globe_to_mercator_(vec3)andproject_common_position_to_flat(vec3|vec4), identity for flat projection modes and sphere to absolute Mercator underGLOBE. The inverse computes Mercator y aslog((D + |z|) / |xy|)directly from the sphere position, which stays accurate near the poles where anasinroute does notproject_common_position_to_flat_wrapped(position, referenceX): x moved to the world copy nearest a reference in every geospatial mode, for bounds and texture lookups (pass the bounds centre)project_common_position_to_flat_continuous(position, referenceX): same, but only underGLOBE, so a polygon spanning the antimeridian stays continuous while flat maps are untouched; for periodic patterns (pass the camera)@deck.gl/extensionsprojection-utils: add CPU twinsisFlatViewport,projectToFlatCommon,projectBoundsToFlatCommon,getFlatCommonOrigin;getRenderBoundsreturns the full layer extent for non-flat viewportsprojectToFlatCommonmirrors theGLOBE+METER_OFFSETSshader branch exactly (ENU displacement on the sphere, then the sphere's inverse) instead of going through lng/lat, which diverges by about 200 m at a 300 km offsetprojectBoundsToFlatCommonunwraps lng/lat bounds whose left edge is more than 180° east of their right edge, so bounds can describe a region across the antimeridian while reversed bounds keep re-orderingtest/render/view-presets.ts:expandViewMatrix(case, ['map', 'globe', 'orthographic'])expands one case into per-view cases with shared San Francisco presets and guards against globe cases above the zoom-12 Mercator hand-offWebMercatorViewport(zoom 11.5 and 14),GlobeViewportandOrthographicViewportto 12 digits, including a 300 km meter offset, antimeridian and reversed bounds;expandViewMatrixnaming and overridesproject.md;docs/whats-new.mdGlobeView compatibility bulletRan the unit specs,
tscfor core and extensions, and lint.Note
Medium Risk
Changes core shader projection and shared extension bounds logic used by mask and terrain effects; additive for current callers but incorrect use of flat vs sphere coordinates could break globe rendering in follow-up PRs.
Overview
Adds shared GPU and CPU paths to treat positions in flat Web Mercator (or cartesian) space while rendering on
GlobeView, so mask/clip/fill-style/terrain extensions can sample flat framebuffers and test bounds without duplicating globe branches.The
projectshader module (GLSL and WGSL) gainsproject_globe_to_mercator_andproject_common_position_to_flat, plus_wrappedand_continuousvariants for antimeridian bounds and seamless periodic patterns. Flat projection modes pass through unchanged.@deck.gl/extensionsprojection-utilsaddsprojectToFlatCommon,projectBoundsToFlatCommon,getFlatCommonOrigin, andisFlatViewport, including a globe meter-offsets path that matches the shader (not lng/lat).getRenderBoundsskips viewport intersection on non-flat viewports so flat layer bounds are not mixed with sphere viewport bounds.Also adds
expandViewMatrixrender-test presets (map / globe / orthographic), unit tests for GPU/CPU pairing and antimeridian bounds, and docs inproject.mdandwhats-new.md.Reviewed by Cursor Bugbot for commit b228729. Bugbot is set up for automated code reviews on this repo. Configure here.