Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions .ai/memory/architecture/core-library.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,17 @@ Renderers must treat a null scene as a failed layer. Scene building skips empty
geometry, renders polygonal members of geometry collections, and counts other
skipped parts in `VectorRenderScene::skippedGeometryCount`.

`MapView` automatically builds viewport-filtered layer scenes with a calculated
margin only when the OGR driver reports a native fast spatial filter. The
private layer cache records the processing extent and retains immutable
geometry and backend mesh identity while the visible extent remains inside it.
Drivers without a fast filter retain complete scenes rather than repeatedly
reading the whole source. Prepared scenes supplied through `AddLayer` are also
complete snapshots and have no processing-extent restriction. Filtered scene
construction must not replace a layer's cached full extent or feature count
with the selected subset. See `VectorRenderSceneOptions`,
`VectorLayerAccess::ForEachGeometry`, and `MapView.cpp`.

## References

- [Architecture documentation](../../../docs/ARCHITECTURE.md#module-design)
Expand Down
21 changes: 21 additions & 0 deletions .github/plans/Vector-Rendering-Performance-Improvement.md
Original file line number Diff line number Diff line change
Expand Up @@ -387,6 +387,27 @@ When the user pans or zooms:
- Small pans do not cause complete layer reconstruction.
- No visible features are accidentally omitted at viewport boundaries.

### Phase 5 implementation and limitations

- `MapView` requests features through an OGR bounding-box spatial filter when
the driver reports `OLCFastSpatialFilter`. The margin is at least 64 pixels
and grows with the viewport size to cover longer navigation steps.
- Filtered scenes retain source geometry and backend cache identity while the
visible extent remains inside the processed extent.
- Pans or zoom-outs beyond the margin rebuild the affected layer selection.
This phase does not yet provide incremental feature-level merging or tiling.
- Caller-prepared scenes remain complete snapshots and are not viewport
filtered. The ActiveX control currently supplies these prepared complete
scenes, so its large-layer initial load still processes every feature before
the first visible rendering. The phase remains open until that path uses
viewport-aware preparation or the acceptance criteria are revised.
- Drivers without a native fast spatial filter retain complete scenes instead
of repeatedly scanning the complete layer for marginal viewport changes.
An automatic filtered build that fails falls back to a complete scene.
- The skipped-feature counter is available only when OGR can return a
reliable non-forced layer feature count and the source uses a native fast
spatial filter. It never forces a complete scan for the diagnostic value.

---

# Phase 6 – Optimize OpenGL rendering and data transfer
Expand Down
63 changes: 42 additions & 21 deletions docs/ARCHITECTURE.md
Original file line number Diff line number Diff line change
Expand Up @@ -233,10 +233,17 @@ has no extent or transform of its own.
in `skippedLayers` with a reason and do not stop the other layers.
This includes `std::exception` errors except `std::bad_alloc`, which propagates.
Exceptions outside that hierarchy also propagate. The scene
holds no view state and no references to the map or its datasets.
holds no view state and no references to the map or its datasets. The
lower-level scene options can apply an OGR spatial filter so callers load
only feature envelopes that intersect a requested extent.
- **Shared viewport.** Renderers draw the scene through the `Viewport` passed
with it, so every layer uses the same transformation. A view change, such as
a resize, does not rebuild the scene.
with it, so every layer uses the same transformation. `MapView` builds
automatically prepared layer scenes from the visible extent plus a
64-pixel margin. Small pans and zoom-in operations therefore retain the
immutable scene and backend mesh caches. A resize or navigation operation
that moves outside the cached processing extent rebuilds only the affected
layer scenes; prepared scenes supplied by a caller are complete snapshots
and remain reusable for every view.
- **Redraws.** Map changes go through `MapView` (`AddLayer`, `RemoveLayer`,
`ClearLayers`, `SetLayerVisible`), which marks the scene stale; `Scene()`
assembles it from cached layer scenes on the next call.
Expand Down Expand Up @@ -316,8 +323,13 @@ the frame trigger a full render.
Wheel input calls `MapView::ZoomAt` at the cursor. The control acquires and
releases mouse capture through either its window or its windowless in-place
site, and ends a drag on capture loss, cancellation, close, or destruction.
These view changes do not rebuild the render scene, and both backends draw
through the updated viewport.
Automatically built `MapView` scenes use a viewport-relative processing margin:
small view changes reuse the scene, and a larger change rebuilds only affected
layer selections. The ActiveX control currently commits caller-prepared complete
scenes, so its view changes continue to reuse the complete snapshot and do not
request filtered scenes. Alternate-size draws use the live scene; filtered
direct-host scenes are therefore valid only through the live viewport. Hosts
that draw a different viewport must supply complete prepared scenes.

Rendering consumes backend-neutral input prepared from core data; neither the
coordinator nor a backend loads datasets or implements GIS rules. For each
Expand Down Expand Up @@ -463,17 +475,25 @@ layers and other geometry types are not rendered. There is no reprojection, so
coordinates remain in the source coordinate system and layers are assumed to
share one.

Core prepares and retains the complete supported polygon scene in memory,
reading feature geometries sequentially without WKT serialization and
reparsing. MapView caches one scene per layer: visibility changes reassemble
the map snapshot without rereading visible layers, hidden layers are prepared
when first shown, and removing or clearing layers drops only the corresponding
cache entries. InvalidateLayerScene rebuilds one layer after source edits;
InvalidateScene clears all layer caches. Adding a prepared layer seeds its
cache directly. Layer scenes are immutable and shared through
For direct `CreateVectorRenderScene` and `CreateMapRenderScene` calls, Core
prepares and retains the complete supported polygon scene in memory. `MapView`
instead applies a padded visible-extent spatial filter to
automatically prepared layers when the driver reports
`OLCFastSpatialFilter`, while a caller-supplied prepared scene remains
complete. Drivers without a native fast filter retain complete scenes so a pan
does not repeatedly force a full-layer scan.
Core reads feature geometries sequentially without WKT serialization and
reparsing. MapView caches one filtered or complete scene per layer: visibility
changes reassemble the map snapshot without rereading visible layers, hidden
layers are prepared when first shown, and removing or clearing layers drops
only the corresponding cache entries. InvalidateLayerScene rebuilds one
layer after source edits; InvalidateScene clears all layer caches. Adding a
prepared layer seeds its cache directly. Layer scenes are immutable and shared through
`std::shared_ptr<const VectorRenderScene>`, so assembling the map snapshot
shares cached coordinates and meshes instead of copying them; peak memory
stays at one copy of each layer's geometry.
shares cached coordinates and meshes instead of copying them. An automatically
built layer holds its selected geometry once, and a prepared layer holds its
complete geometry once. Rebuilding a selection temporarily retains both the old
scene and the new selection until the assembled snapshot is replaced.
OpenGL's lazily generated fill meshes have a second, backend-owned cache keyed
by stable `LayerId` and the immutable scene identity. Reordering, hiding,
redrawing, viewport changes, and backend selection retain valid entries.
Expand All @@ -493,12 +513,13 @@ each polygon. Both backends read the flat buffers directly. Triangulation
accepts a view without reconstructing nested vectors. Standalone `Polygon`
and `Ring` types remain available for caller-created triangulation input.

There is no tiling, paging, or clipping to the visible extent. GDI skips a
layer whose device coordinates exceed its range. It clips drawing to the
requested target rectangle and the host's existing clip region, then restores
the device context. Fill triangulation runs when scene preparation explicitly
requests it or when OpenGL first needs a missing mesh. View changes (resize,
zoom, pan) and redraws reuse the retained meshes. The opt-in
There is no tiling or paging. GDI skips a layer whose device coordinates
exceed its range. It clips drawing to the requested target rectangle and the
host's existing clip region, then restores the device context. Fill
triangulation runs when scene preparation explicitly requests it or when
OpenGL first needs a missing mesh. View changes inside the scene's processing
margin and redraws reuse retained meshes; changes outside the margin replace
the affected scene and its backend mesh entry. The opt-in
rendering baseline (see [TESTING.md](TESTING.md#rendering-performance-baseline))
tracks scene preparation, mesh storage, and backend frame costs on both a
convex grid and a complex mix of concave, holed multipart, and many-vertex
Expand Down
23 changes: 16 additions & 7 deletions src/MapWinGIS.Core/include/mapwingis/map/MapView.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,16 @@ namespace mwg
///
/// Redraws: every mutator returns whether the displayed frame may have
/// changed. Hosts request a redraw when it returns true and draw Scene()
/// through GetViewport(). Changes to the layer collection or visibility
/// mark the scene stale; Scene() rebuilds it on the next call. Adding a
/// prepared scene retains the existing geometry instead. Edits to source
/// geometry are not observed. Call InvalidateLayerScene for each affected
/// map layer, or InvalidateScene for all layers.
/// through GetViewport(). Automatically filtered scenes cover only the
/// live viewport plus a processing margin; drawing a different viewport
/// requires a complete prepared scene. Changes to the layer collection or
/// visibility mark the scene stale; Scene() rebuilds it on the next call.
/// For a valid viewport, automatically built layer scenes use a padded
/// spatial filter. Small viewport changes reuse the cached scene; a change
/// beyond that margin refreshes only the affected layer scenes. Adding a
/// prepared scene retains the existing complete geometry instead. Edits to
/// source geometry are not observed. Call InvalidateLayerScene for each
/// affected map layer, or InvalidateScene for all layers.
///
/// Initial view: while the viewport has no view, adding a layer fits the
/// viewport to the map. A fit that cannot happen because the viewport has
Expand Down Expand Up @@ -160,8 +165,12 @@ namespace mwg
/// per-layer scenes when the map or a layer scene changed. The
/// reference stays valid for the lifetime of the MapView; a later
/// call that reassembles the scene replaces its contents. Geometry
/// for a visible uncached layer is read once; visibility changes reuse
/// its cache. Reassembly shares cached geometry with the map snapshot
/// for a visible uncached layer is read once. A valid viewport limits
/// automatically built scenes to features intersecting a padded
/// visible extent when the driver supports a fast spatial filter.
/// Drivers without that capability retain a complete scene. Visibility
/// changes and viewport moves within the processing extent reuse
/// the cache. Reassembly shares cached geometry with the map snapshot
/// instead of copying it, so a held LayerRenderScene::scene stays
/// valid after the layer is invalidated or removed.
/// Failed layers retain their diagnostics until explicitly invalidated.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ namespace mwg::rendering
/// The layer cannot be rendered, for example because it is not a
/// polygon layer or its geometry cannot be read. See the message.
Failed,
/// No polygon geometry intersects an applied spatial filter.
OutsideViewport,
};

/// A map layer that is not part of a MapRenderScene.
Expand Down Expand Up @@ -74,8 +76,9 @@ namespace mwg::rendering
/// layers' source coordinate system; the scene does not reproject.
///
/// The scene is a snapshot: later changes to the map or its sources are
/// not reflected, so create a new scene after the map changes. Reads
/// every visible layer completely. Not thread-safe; see Map.
/// not reflected, so create a new scene after the map changes. With no
/// spatial filter, every visible layer is read completely. Not
/// thread-safe; see Map.
/// \throws std::bad_alloc when memory runs out. Other std::exception
/// errors from individual layers are reported in skippedLayers.
/// Exceptions not derived from std::exception propagate.
Expand All @@ -86,6 +89,7 @@ namespace mwg::rendering
/// Hidden, empty, and failed layers retain the same handling as the
/// default overload. Fill preparation is disabled by default, so drawable
/// layer scenes contain source rings without triangulating polygons.
/// \throws InvalidArgument when the spatial-filter extent is invalid.
[[nodiscard]] MWG_CORE_API MapRenderScene CreateMapRenderScene(
const Map& map,
const VectorRenderSceneOptions& options);
Expand Down
18 changes: 15 additions & 3 deletions src/MapWinGIS.Core/include/mapwingis/rendering/VectorRenderScene.h
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,12 @@ namespace mwg
/// Enable this only for a rendering operation that needs triangle
/// meshes. Source geometry and bounds are prepared either way.
bool prepareFillMeshes = false;
/// When present, only features whose bounding boxes intersect this
/// extent are converted. Drivers with OLCFastSpatialFilter avoid
/// reading the other features; other drivers may still read and
/// parse them. Scene construction restores the source filter.
/// \throws InvalidArgument when the extent is not valid.
std::optional<Extent> spatialFilter;
};

/// Owned polygon geometry and its source-coordinate bounds.
Expand Down Expand Up @@ -164,8 +170,10 @@ namespace mwg
/// has fewer than three points, are skipped and counted in
/// skippedGeometryCount. Interior rings with fewer than three points
/// are dropped. The returned coordinates remain in the layer's coordinate
/// system; this function does not transform projections. Not
/// thread-safe, consistent with the input layer's contract.
/// system; this function does not transform projections. When options
/// specify a spatial filter, sourceFeatureCount is the number of
/// matching source features and extent covers only their geometry.
/// Not thread-safe, consistent with the input layer's contract.
/// \throws CoreError when the layer is not polygonal or contains no
/// polygon geometry to render.
[[nodiscard]] MWG_CORE_API VectorRenderScene CreateVectorRenderScene(
Expand All @@ -175,7 +183,11 @@ namespace mwg
///
/// The default is source geometry only. Set prepareFillMeshes to true
/// when the requested fill rendering operation needs triangle meshes.
/// The default overload never invokes polygon triangulation.
/// Set spatialFilter to avoid converting geometry outside a viewport.
/// Drivers with OLCFastSpatialFilter can also avoid reading it.
/// Features returned by an OGR spatial filter include bounding-box
/// boundary intersections. The default overload never invokes polygon
/// triangulation.
[[nodiscard]] MWG_CORE_API VectorRenderScene CreateVectorRenderScene(
const VectorLayer& layer,
const VectorRenderSceneOptions& options);
Expand Down
7 changes: 6 additions & 1 deletion src/MapWinGIS.Core/src/internal/VectorLayerAccess.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,15 @@ namespace mwg::detail
static std::int64_t ForEachGeometry(
const VectorLayer& layer,
const std::function<void(const OGRGeometry&)>& callback,
Extent* extent = nullptr);
Extent* extent = nullptr,
const Extent* spatialFilter = nullptr);

/// Whether the source driver can return an extent without scanning
/// every feature.
[[nodiscard]] static bool HasFastExtent(const VectorLayer& layer);

/// Whether MapView can safely reduce feature reads with the layer's
/// single active geometry field.
[[nodiscard]] static bool HasFastSpatialFilter(const VectorLayer& layer);
};
}
4 changes: 4 additions & 0 deletions src/MapWinGIS.Core/src/internal/VectorRenderSceneBuilder.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,8 @@ namespace mwg::rendering::detail
[[nodiscard]] VectorRenderScene BuildVectorRenderScene(
const VectorLayer& layer,
const VectorRenderSceneOptions& options = {});

/// Rejects programmer errors in scene options before per-layer failures
/// can convert them into skipped-layer diagnostics.
void ValidateVectorRenderSceneOptions(const VectorRenderSceneOptions& options);
}
Loading