From 3b2f23fc64ad23d26d6014acbad1db4a8ec8c046 Mon Sep 17 00:00:00 2001 From: Paul Meems Date: Thu, 19 Nov 2026 17:44:20 +0100 Subject: [PATCH 1/6] =?UTF-8?q?=E2=9A=A1=EF=B8=8F=20perf(rendering):=20ins?= =?UTF-8?q?trument=20vector=20performance=20pipeline?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Add process-wide opt-in timings and counters across Core and ActiveX - Benchmark fill-enabled and outline-only GDI/OpenGL configurations - Document Release results and the remaining OpenGL outline limitation Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- ...ector-Rendering-Performance-Improvement.md | 511 ++++++++++++++++++ docs/TESTING.md | 63 ++- .../GdiVectorLayerRenderer.cpp | 3 + .../OpenGlVectorLayerRenderer.cpp | 25 +- src/MapWinGIS.Core/CMakeLists.txt | 2 + src/MapWinGIS.Core/MapWinGIS.Core.vcxproj | 2 + .../diagnostics/PerformanceDiagnostics.h | 113 ++++ .../mapwingis/rendering/MapRenderScene.h | 9 + .../mapwingis/rendering/VectorRenderScene.h | 34 +- .../diagnostics/PerformanceDiagnostics.cpp | 151 ++++++ .../src/internal/VectorRenderSceneBuilder.h | 8 +- src/MapWinGIS.Core/src/map/MapView.cpp | 14 + .../src/rendering/MapRenderScene.cpp | 16 +- .../src/rendering/PolygonTriangulation.cpp | 40 +- .../src/rendering/VectorRenderScene.cpp | 36 +- .../src/vector/VectorDataset.cpp | 22 +- .../RenderingBaselineTests.cpp | 216 +++++++- .../VectorLayerRendererTests.cpp | 40 ++ tests/MapWinGIS.Core.Tests/CMakeLists.txt | 1 + .../MapWinGIS.Core.Tests.vcxproj | 1 + .../PerformanceDiagnosticsTests.cpp | 181 +++++++ 21 files changed, 1447 insertions(+), 41 deletions(-) create mode 100644 .github/plans/Vector-Rendering-Performance-Improvement.md create mode 100644 src/MapWinGIS.Core/include/mapwingis/diagnostics/PerformanceDiagnostics.h create mode 100644 src/MapWinGIS.Core/src/diagnostics/PerformanceDiagnostics.cpp create mode 100644 tests/MapWinGIS.Core.Tests/PerformanceDiagnosticsTests.cpp diff --git a/.github/plans/Vector-Rendering-Performance-Improvement.md b/.github/plans/Vector-Rendering-Performance-Improvement.md new file mode 100644 index 0000000..1620951 --- /dev/null +++ b/.github/plans/Vector-Rendering-Performance-Improvement.md @@ -0,0 +1,511 @@ +# MapWinGIS6 – Vector Rendering Performance Improvement Plan + +## Implementation checklist + +Copilot: Update this checklist as each phase is implemented. Only mark a phase complete when its acceptance criteria have been met and its PR is ready. + +- [x] **Phase 1 – Establish a performance baseline** (PR 1) + - Added opt-in, thread-safe timings and counters across Core and ActiveX, plus A-D benchmark configurations and Release results in `docs/TESTING.md`. + - Limitation: the referenced 10.3 MB shapefile is not stored in the repository; `MWG_RENDERING_BASELINE_DATASET` supports measuring that file when available. + - Limitation: configuration C records the current OpenGL rejection because outline-only OpenGL rendering is a Phase 2 change. +- [ ] **Phase 2 – Separate geometry loading from rendering preparation** (PR 2) +- [ ] **Phase 3 – Implement lazy polygon triangulation** (PR 3) +- [ ] **Phase 4 – Add intelligent caching and invalidation** (PR 4) +- [ ] **Phase 5 – Introduce viewport-aware geometry processing** (PR 5) +- [ ] **Phase 6 – Optimize OpenGL rendering and data transfer** (PR 6) +- [ ] **Phase 7 – Testing and regression prevention** (PR 7) + +**Target branch:** `development` + +## Objective + +Improve the performance of opening and initially rendering large polygon shapefiles in MapWinGIS6. + +A performance comparison with MapWinGIS5 showed that MapWinGIS6 currently takes considerably longer to load and display a polygon shapefile. + +Test dataset: + +- File size: approximately 10.3 MB +- Features: 2,459 polygons +- MapWinGIS5: nearly instantaneous initial display +- MapWinGIS6: more than 7 seconds before the initial display + +Initial experiments established: + +- GDI with GEOS triangulation disabled is the fastest configuration. +- Zooming and panning are already reasonably fast once the layer has been rendered. + +This suggests that the initial scene-building and triangulation pipeline deserves particular attention. However, do not assume triangulation is the only bottleneck. Instrument the complete pipeline before deciding which optimizations are necessary. + +## Architectural principles + +The implementation must follow these principles: + +1. Opening a dataset must not require polygon triangulation. +2. Geometry loading and rendering preparation must be separate operations. +3. Polygon outlines must be renderable without triangulation. +4. Polygon filling should use triangulation only when the selected rendering backend requires it. +5. Expensive geometry processing should be performed lazily. +6. Generated geometry and rendering resources should be cached and reused. +7. Only geometry needed for the current viewport should be processed where practical. +8. Existing rendering functionality and public ActiveX behavior must remain intact. +9. GEOS must remain an optional implementation detail of the rendering pipeline, not a prerequisite for opening a dataset. +10. Performance improvements must be measured rather than assumed. + +Prioritize a clean architecture and maintainability over preserving the existing implementation structure. + +--- + +# Phase 1 – Establish a performance baseline + +Before changing the rendering architecture, add instrumentation to identify exactly where time is spent. + +### 1.1 Instrument the complete pipeline + +Measure the following operations independently: + +- Opening the shapefile and its associated files. +- Reading feature metadata. +- Reading feature geometries from GDAL/OGR. +- Converting geometries into MapWinGIS internal representations. +- Building the vector rendering scene. +- GEOS geometry validation. +- GEOS triangulation. +- Uploading geometry buffers to OpenGL. +- Initial rendering. +- Any framebuffer readback operations, including `glReadPixels`. + +Use `std::chrono::steady_clock` for C++ timing. + +Instrumentation should be easy to enable or disable. Prefer a consistent diagnostic mechanism instead of scattered console output. + +### 1.2 Add useful counters + +In addition to elapsed time, record: + +- Number of features processed. +- Number of geometries loaded. +- Number of polygons triangulated. +- Number of triangles generated. +- Number of invalid geometries. +- Number of triangulation failures. +- Number of features skipped because they are outside the viewport. +- Number of cache hits and misses. + +### 1.3 Benchmark configurations + +Use the same dataset and machine for all tests. + +Compare: + +| Test | Backend | Triangulation | +|---|---|---| +| A | GDI | Disabled | +| B | GDI | Enabled | +| C | OpenGL | Disabled | +| D | OpenGL | Enabled | + +Record both initial loading time and first-render time separately. + +Also measure subsequent zoom and pan operations. + +### Deliverable + +A performance report identifying the dominant costs and providing a baseline for future comparisons. + +Do not proceed by simply assuming that GEOS triangulation accounts for the entire delay. + +--- + +# Phase 2 – Separate geometry loading from rendering preparation + +Inspect the current implementation, particularly: + +- `VectorRenderScene.cpp` +- `PolygonTriangulation.cpp` +- `VectorDataset.cpp` +- `MapView.cpp` +- `OpenGlVectorLayerRenderer.cpp` +- `VectorLayerRenderer.cpp` + +### 2.1 Review the current scene-building process + +Currently, the scene-building pipeline iterates through geometries and builds rendering data. Polygon triangulation is performed as part of this process. + +Change this architecture so that building a vector scene does not automatically triangulate every polygon. + +### 2.2 Introduce separate geometry representations + +Keep the original geometry data available independently of any triangulated representation. + +Conceptually, distinguish between: + +- Source geometry: original points, rings, and geometry structure. +- Rendering metadata: bounding boxes, feature identifiers, and other information needed for rendering. +- Polygon fill meshes: triangulated geometry used by fill rendering. + +Do not duplicate large amounts of geometry unnecessarily. + +### 2.3 Preserve outline rendering + +Polygon outlines must be drawable directly from the original geometry representation. + +GDI rendering must not depend on triangulation. + +OpenGL should also be able to render polygon outlines without requiring fill meshes. + +### 2.4 Remove mandatory triangulation + +Remove the unconditional triangulation step from the normal scene-building path. + +Building a scene should prepare only the data required for the initial rendering operation. + +### Acceptance criteria + +- Opening a polygon shapefile does not invoke GEOS triangulation. +- Polygon outlines can be rendered without triangulation. +- GDI rendering works with triangulation disabled. +- Existing polygon rendering behavior is preserved when fill rendering is requested. + +--- + +# Phase 3 – Implement lazy polygon triangulation + +Introduce triangulation as an on-demand operation. + +### 3.1 Define when triangulation is necessary + +Triangulation should only be requested when: + +- Polygon fill rendering is enabled. +- The selected rendering backend needs triangle meshes. +- A particular polygon does not already have a valid cached mesh. + +Do not triangulate polygons merely because they are part of a newly opened dataset. + +### 3.2 Implement lazy generation + +When an OpenGL fill operation needs a polygon mesh: + +1. Check whether a valid mesh already exists. +2. If it exists, reuse it. +3. Otherwise, generate the mesh using the existing GEOS triangulation implementation. +4. Store the generated mesh in the appropriate cache. +5. Continue rendering. + +Avoid repeated triangulation during zooming, panning, resizing, or repainting. + +### 3.3 Handle failures gracefully + +Triangulation must not prevent the dataset from being opened or its outlines from being displayed. + +Handle: + +- Invalid geometries. +- Empty geometries. +- GEOS failures. +- Polygons containing holes. +- MultiPolygon geometries. +- Very large polygons. +- Degenerate rings. + +A failed triangulation should not crash the application or invalidate unrelated features. + +Do not silently change the source geometry to make triangulation succeed. + +### 3.4 Consider background processing + +After the basic lazy implementation works, investigate whether triangulation can be performed asynchronously. + +Requirements: + +- Never block the UI thread for expensive triangulation. +- Avoid concurrent access to unsafe GDAL/OGR or GEOS objects. +- Ensure cancellation when a layer is removed or its data changes. +- Ensure generated results are associated with the correct dataset and feature. +- Ensure rendering can safely handle meshes that are not yet available. + +Do not introduce asynchronous processing until the synchronous lazy implementation is correct and measured. + +### Acceptance criteria + +- Opening a polygon shapefile does not trigger triangulation. +- GDI can render polygons without fill meshes. +- OpenGL can render outlines without fill meshes. +- Polygon fills trigger triangulation only when needed. +- Repeated rendering reuses existing meshes. +- Triangulation failures do not prevent outline rendering. + +--- + +# Phase 4 – Add intelligent caching and invalidation + +The existing scene cache in `MapView` is a useful starting point. Extend the architecture without introducing unnecessary duplicate caches. + +### 4.1 Cache triangulation results + +Cache generated fill meshes per feature or another suitable stable geometry identifier. + +The cache should support: + +- Reuse across repaint operations. +- Reuse across zoom and pan operations. +- Efficient lookup. +- Memory-aware storage. +- Explicit invalidation. + +### 4.2 Define cache invalidation rules + +Invalidate affected cached data when: + +- The underlying dataset changes. +- A layer is removed. +- A layer is reloaded. +- Geometry is edited. +- The coordinate system or transformation changes in a way that invalidates cached coordinates. +- Rendering configuration changes in a way that makes cached data unusable. + +Changing only the viewport should not invalidate triangulation meshes. + +Changing between GDI and OpenGL should not discard valid geometry or triangulation data unnecessarily. + +### 4.3 Manage memory + +Large polygon datasets can generate substantial mesh data. + +Consider: + +- Tracking the memory used by cached meshes. +- Avoiding duplicate copies of vertices. +- Releasing resources when layers are removed. +- Releasing GPU resources when appropriate. +- An optional cache-size limit or eviction policy if measurements justify it. + +Do not implement a complicated cache eviction system unless actual memory measurements show that it is needed. + +### Acceptance criteria + +- Repeated zooming and panning do not trigger repeated triangulation. +- Switching rendering backends does not cause unnecessary geometry reconstruction. +- Removing a layer releases its associated resources. +- Cache invalidation is correct and testable. + +--- + +# Phase 5 – Introduce viewport-aware geometry processing + +Currently, the scene-building process may process every feature, even when most features are outside the visible map area. + +Investigate how to reduce this work. + +### 5.1 Use feature bounding boxes + +Use feature extents to determine whether a feature intersects the current viewport. + +Avoid loading, converting, triangulating, or uploading geometry that cannot contribute to the current rendering operation. + +Use a small configurable or calculated margin around the viewport to avoid excessive work during small pans. + +### 5.2 Investigate spatial indexing + +Check whether GDAL/OGR spatial filtering can be used effectively. + +Also investigate whether MapWinGIS6 already has a suitable spatial index or whether an existing one can be reused. + +Do not introduce a new spatial indexing implementation without first checking the existing architecture. + +### 5.3 Avoid unnecessary full-dataset geometry copying + +MapWinGIS5 uses a more demand-driven approach to reading shapefile geometries. Investigate whether a similar strategy is appropriate for MapWinGIS6. + +Potential improvements: + +- Load feature metadata and extents first. +- Retrieve full geometry only when required. +- Retain already loaded geometry in a cache. +- Avoid copying every feature into multiple internal representations. + +Do not compromise the existing GDAL abstraction or create unnecessary coupling to the shapefile file format. + +### 5.4 Handle viewport changes efficiently + +When the user pans or zooms: + +- Determine which features have become visible. +- Reuse already loaded geometries. +- Load missing geometries only when needed. +- Reuse cached triangulation meshes. +- Avoid rebuilding the entire layer scene if only a small part of the viewport changes. + +### Acceptance criteria + +- Large datasets do not require processing every feature before the first visible rendering. +- Features outside the viewport do not undergo unnecessary triangulation. +- Small pans do not cause complete layer reconstruction. +- No visible features are accidentally omitted at viewport boundaries. + +--- + +# Phase 6 – Optimize OpenGL rendering and data transfer + +After the preceding phases, investigate remaining OpenGL overhead. + +### 6.1 Inspect buffer management + +Ensure that vertex and index buffers are reused rather than recreated on every frame. + +Separate geometry preparation from GPU buffer creation. + +### 6.2 Investigate framebuffer readback + +Inspect `OpenGlVectorLayerRenderer.cpp`, especially `glReadPixels`. + +Framebuffer readback can force synchronization between the GPU and CPU. + +Determine: + +- Why the framebuffer is read back. +- Whether the readback is required for the current ActiveX architecture. +- Whether it can be avoided or reduced. +- Whether the current implementation copies more data than necessary. + +Do not remove functionality that depends on the readback without providing an alternative. + +### 6.3 Avoid unnecessary rendering work + +Investigate: + +- Drawing only visible features. +- Avoiding unnecessary state changes. +- Reducing redundant buffer uploads. +- Reusing GPU resources. +- Avoiding repeated coordinate transformations. + +### Acceptance criteria + +- OpenGL initial rendering does not perform unnecessary CPU/GPU synchronization. +- GPU buffers are reused appropriately. +- Rendering remains correct across repeated zoom and pan operations. + +--- + +# Phase 7 – Testing and regression prevention + +Performance improvements must not introduce rendering regressions. + +### 7.1 Test datasets + +Use a representative set of datasets: + +- Small polygon shapefile. +- Medium polygon shapefile. +- Large polygon shapefile. +- Polygon shapefile with holes. +- Multipolygon shapefile. +- Invalid or degenerate polygon geometries. +- Large polygon features. +- Point and line datasets. + +Include the original 10.3 MB dataset with 2,459 features as a repeatable benchmark. + +### 7.2 Test rendering backends + +Test: + +- GDI with polygon outlines. +- GDI with polygon fills. +- OpenGL with polygon outlines. +- OpenGL with polygon fills. +- Backend switching. +- Repeated zooming and panning. +- Layer removal and reload. + +### 7.3 Test correctness + +Compare rendered output before and after the changes. + +Pay particular attention to: + +- Polygon holes. +- Multipolygons. +- Polygon boundaries. +- Feature visibility. +- Clipping at viewport boundaries. +- Invalid geometries. +- Coordinate transformations. + +### 7.4 Add automated tests + +Add unit tests for: + +- Lazy triangulation. +- Cache hits and misses. +- Cache invalidation. +- Failed triangulation. +- Empty geometries. +- Layer removal and resource cleanup. + +Add integration tests where practical. + +### 7.5 Performance targets + +Use the original dataset as the primary benchmark. + +Targets: + +- Opening a dataset must not trigger GEOS triangulation. +- Initial GDI outline rendering should be close to the cost of loading and drawing the visible geometry. +- Initial OpenGL outline rendering should not require polygon fill meshes. +- Repeated rendering must not repeat expensive geometry preparation. +- Zoom and pan performance must remain at least as good as the current implementation. + +Do not introduce arbitrary timing thresholds into CI unless the test environment is sufficiently stable. Record benchmark results and compare them against the established baseline. + +--- + +# Implementation strategy + +Implement this work in separate, reviewable pull requests. + +Suggested sequence: + +| PR | Scope | +|---|---| +| PR 1 | Performance instrumentation and reproducible benchmark | +| PR 2 | Separate geometry preparation from triangulation | +| PR 3 | Lazy triangulation for OpenGL polygon fills | +| PR 4 | Triangulation cache and invalidation | +| PR 5 | Viewport-aware geometry processing | +| PR 6 | OpenGL buffer and framebuffer readback optimization | +| PR 7 | Additional automated tests and performance regression coverage | + +Each PR must: + +1. Build successfully using the existing CMake build configuration. +2. Include appropriate tests. +3. Document any architectural decisions. +4. Include before/after performance measurements where applicable. +5. Avoid unrelated refactoring. +6. Be independently reviewable. +7. Include a clear description of limitations and remaining work. + +Do not wait for all phases to be completed before creating PRs. Each phase should result in a separate PR against the `development` branch, following the repository's existing contribution workflow. + +## Instructions to Copilot + +Before implementing anything: + +1. Inspect the current `development` branch. +2. Read `AGENTS.md` and all applicable repository instructions. +3. Review the existing rendering architecture and recent changes. +4. Verify that the files and classes mentioned in this plan still match the current implementation. +5. Identify existing abstractions that can be reused. +6. Present a short implementation proposal for the first PR. + +Do not assume the architecture described in this plan is still exactly accurate. Verify it against the current source code. + +Do not implement all phases in one large change. + +Preserve the existing ActiveX public API unless a change is demonstrably necessary. Prefer internal architectural improvements. + +The primary goal is to make MapWinGIS6 fast when opening and initially displaying large vector datasets, without sacrificing the advantages of the new GDAL, GEOS, and OpenGL architecture. diff --git a/docs/TESTING.md b/docs/TESTING.md index ca6d97c..742d1e3 100644 --- a/docs/TESTING.md +++ b/docs/TESTING.md @@ -200,11 +200,24 @@ For build and manual-use instructions, see the ## Rendering performance baseline -`RenderingBaseline.MeasuresCoreSceneAndBackendCosts` is opt-in. It generates a -GeoPackage grid of convex 32-vertex polygons and separately reports Core -dataset open, render-scene preparation, process private-memory growth, the -median GDI frame, the first OpenGL frame (context creation and drawing), and -the median steady OpenGL frame, for a 1280x720 off-screen target. +The `RenderingBaseline` tests are opt-in. They use either a generated +GeoPackage grid of convex 32-vertex polygons or the dataset named by +`MWG_RENDERING_BASELINE_DATASET`. `MeasuresTriangulationDisabledCosts` builds an +outline-only scene (configuration A for GDI and configuration C for OpenGL). +`MeasuresCoreSceneAndBackendCosts` uses the default fill-mesh preparation +(configurations B and D). Both report first and steady frame times, plus pan +and zoom frames, for a 1280x720 off-screen target. + +Performance diagnostics are disabled by default and aggregate process-wide +timings with `std::chrono::steady_clock`. The collector records dataset +opening, layer metadata, OGR feature reads, internal geometry conversion, +scene construction, GEOS validation, triangulation, OpenGL geometry +submission, complete backend rendering, and `glReadPixels`. It also records +feature/geometry counts, triangles, invalid and failed triangulations, +viewport skips, and scene-cache hits and misses. OpenGL currently submits +client-side transformed arrays with `glDrawElements`; there is no persistent +GPU buffer-upload stage to measure yet. + `RenderingBaseline.MeasuresComplexPolygonCosts` generates a second dataset — concave L-shaped polygons, holed rectangles where every feature is a two-component multipolygon, and concave 32-vertex star polygons across three @@ -217,11 +230,47 @@ stage fails. Use a Release build for meaningful numbers: ```powershell $env:MWG_RUN_RENDERING_BASELINE = '1' $env:MWG_RENDERING_BASELINE_FEATURES = '100000' # optional; default 50000 +$env:MWG_RENDERING_BASELINE_DATASET = 'C:\data\large-polygons.shp' # optional bin\Release\x64\MapWinGIS.ActiveX.Tests.exe --gtest_filter=RenderingBaseline.* ``` -Measurements on one developer workstation (Release, hardware OpenGL), with -triangulation included in scene preparation: +When `MWG_RENDERING_BASELINE_DATASET` is set, the first two tests run both the +fill-enabled and outline-only configurations against that same source. The +complex-geometry test continues to generate its own controlled dataset. The +original 10.3 MB, 2,459-feature shapefile referenced by the performance plan +is not committed to the repository; set the variable to rerun that exact +baseline. + +Phase 1 measurements on one developer workstation (Release, hardware OpenGL) +used a generated 2,459-feature grid with 32 vertices per polygon: + +| Configuration | Scene preparation | First frame | Steady frame | Pan / zoom | +|---|---:|---:|---:|---:| +| A. GDI, no fill meshes | 7.7 ms | 7.2 ms | 6.3 ms | 6.1 / 5.4 ms | +| B. GDI, fill meshes | 230.6 ms | 7.4 ms | 6.2 ms | 6.1 / 5.6 ms | +| C. OpenGL, no fill meshes | 7.7 ms | 0.0 ms rejection | — | — | +| D. OpenGL, fill meshes | 230.6 ms | 220.8 ms | 2.8 ms | 2.8 / 2.5 ms | + +In configuration D, GEOS validation took 13.4 ms and triangulation 205.8 ms, +so triangulation accounted for about 89% of scene construction. The scene +contained 73,770 triangles. With triangulation disabled, OGR feature reads +took 5.0 ms and internal geometry conversion took 1.8 ms of the 7.7 ms scene +build. The first OpenGL frame included 220.8 ms of context and target setup; +subsequent frames took about 2.8 ms. Across eight OpenGL draws, +`glReadPixels` took 20.0 ms in aggregate, while transformed client-array +submission took 5.3 ms. These measurements show scene triangulation is the +dominant opening cost on the generated grid, without assuming it is the only +cost for other datasets. + +Configuration C is intentionally reported as a rejection rather than a +successful outline render: the current OpenGL backend requires a triangle +mesh for every polygon. Measuring successful triangulation-free OpenGL +outlines is a Phase 2 deliverable. Viewport culling and its skipped-feature +counter remain zero because scene construction currently processes all +features. + +Previous measurements on one developer workstation, with triangulation +included in scene preparation, remain useful as history: | Measurement | Convex grid, 50k polygons | Complex mix, 162.5k meshes | |---|---:|---:| diff --git a/src/MapWinGIS.ActiveX/GdiVectorLayerRenderer.cpp b/src/MapWinGIS.ActiveX/GdiVectorLayerRenderer.cpp index e9d0eb7..53b9887 100644 --- a/src/MapWinGIS.ActiveX/GdiVectorLayerRenderer.cpp +++ b/src/MapWinGIS.ActiveX/GdiVectorLayerRenderer.cpp @@ -17,6 +17,7 @@ #include #include "ViewTransform.h" +#include "mapwingis/diagnostics/PerformanceDiagnostics.h" namespace mwg::activex { @@ -214,6 +215,8 @@ namespace mwg::activex const Viewport& viewport, const bool) noexcept { + const diagnostics::PerformanceScope scope( + diagnostics::PerformanceOperation::FrameRender); _failedLayers.clear(); if (deviceContext == nullptr || !MatchesViewport(bounds, viewport)) { diff --git a/src/MapWinGIS.ActiveX/OpenGlVectorLayerRenderer.cpp b/src/MapWinGIS.ActiveX/OpenGlVectorLayerRenderer.cpp index 8069407..df8a4db 100644 --- a/src/MapWinGIS.ActiveX/OpenGlVectorLayerRenderer.cpp +++ b/src/MapWinGIS.ActiveX/OpenGlVectorLayerRenderer.cpp @@ -30,6 +30,7 @@ #pragma comment(lib, "opengl32.lib") #include "ViewTransform.h" +#include "mapwingis/diagnostics/PerformanceDiagnostics.h" namespace mwg::activex { @@ -292,6 +293,8 @@ namespace mwg::activex const rendering::VectorRenderScene& scene, const ViewTransform& transform) noexcept { + const diagnostics::PerformanceScope submissionScope( + diagnostics::PerformanceOperation::OpenGlGeometrySubmission); glEnableClientState(GL_VERTEX_ARRAY); std::vector deviceVertices; for (std::size_t index = 0; index < scene.polygons.size(); ++index) @@ -729,14 +732,18 @@ namespace mwg::activex glFlush(); glReadBuffer(kColorAttachment0); - glReadPixels( - 0, - 0, - target.width, - target.height, - kBgra, - GL_UNSIGNED_BYTE, - _pixelBuffer.data()); + { + const diagnostics::PerformanceScope readbackScope( + diagnostics::PerformanceOperation::FramebufferReadback); + glReadPixels( + 0, + 0, + target.width, + target.height, + kBgra, + GL_UNSIGNED_BYTE, + _pixelBuffer.data()); + } if (glGetError() != GL_NO_ERROR) { _lastFailureReason = @@ -847,6 +854,8 @@ namespace mwg::activex temporaryRenderer._initializationRetryDrawsRemaining; return result; } + const diagnostics::PerformanceScope frameScope( + diagnostics::PerformanceOperation::FrameRender); _lastFailureReason = L"OpenGL did not render this frame."; const TargetValidation validation = ValidateTarget(deviceContext, bounds, scene, viewport); diff --git a/src/MapWinGIS.Core/CMakeLists.txt b/src/MapWinGIS.Core/CMakeLists.txt index b43c9ee..2435480 100644 --- a/src/MapWinGIS.Core/CMakeLists.txt +++ b/src/MapWinGIS.Core/CMakeLists.txt @@ -10,6 +10,7 @@ set(MWG_CORE_PUBLIC_HEADERS include/mapwingis/core/Enums.h include/mapwingis/core/Error.h include/mapwingis/core/Extent.h + include/mapwingis/diagnostics/PerformanceDiagnostics.h include/mapwingis/gdal/GdalAlgorithmRunner.h include/mapwingis/gdal/GdalConfig.h include/mapwingis/gdal/GdalDiagnostics.h @@ -39,6 +40,7 @@ set(MWG_CORE_PRIVATE_HEADERS ) set(MWG_CORE_SOURCES + src/diagnostics/PerformanceDiagnostics.cpp src/gdal/GdalAlgorithmRunner.cpp src/gdal/GdalEnvironment.cpp src/gdal/GdalModern.cpp diff --git a/src/MapWinGIS.Core/MapWinGIS.Core.vcxproj b/src/MapWinGIS.Core/MapWinGIS.Core.vcxproj index 7de79cc..1554ed9 100644 --- a/src/MapWinGIS.Core/MapWinGIS.Core.vcxproj +++ b/src/MapWinGIS.Core/MapWinGIS.Core.vcxproj @@ -52,6 +52,7 @@ + @@ -78,6 +79,7 @@ + diff --git a/src/MapWinGIS.Core/include/mapwingis/diagnostics/PerformanceDiagnostics.h b/src/MapWinGIS.Core/include/mapwingis/diagnostics/PerformanceDiagnostics.h new file mode 100644 index 0000000..d098e67 --- /dev/null +++ b/src/MapWinGIS.Core/include/mapwingis/diagnostics/PerformanceDiagnostics.h @@ -0,0 +1,113 @@ +// SPDX-License-Identifier: MPL-2.0 + +/** + * @file PerformanceDiagnostics.h + * @brief Declares opt-in, process-wide performance measurements. + * + * Provides thread-safe aggregate timings and counters for the vector loading + * and rendering pipeline. Measurements are disabled by default and are kept + * in Core so both Core and ActiveX can contribute to the same diagnostic run. + */ +#pragma once + +#include +#include +#include +#include + +#include "mapwingis/core/Api.h" + +namespace mwg::diagnostics +{ + enum class PerformanceOperation : int + { + DatasetOpen, + LayerMetadataRead, + FeatureGeometryRead, + GeometryConversion, + SceneBuild, + GeometryValidation, + PolygonTriangulation, + OpenGlGeometrySubmission, + FrameRender, + FramebufferReadback, + Count, + }; + + enum class PerformanceCounter : int + { + FeaturesProcessed, + GeometriesLoaded, + PolygonsTriangulated, + TrianglesGenerated, + InvalidGeometries, + TriangulationFailures, + FeaturesSkippedOutsideViewport, + SceneCacheHits, + SceneCacheMisses, + Count, + }; + + constexpr std::size_t PerformanceOperationCount = + static_cast(PerformanceOperation::Count); + constexpr std::size_t PerformanceCounterCount = + static_cast(PerformanceCounter::Count); + + struct MWG_CORE_API PerformanceTiming + { + std::int64_t callCount = 0; + std::int64_t durationNanoseconds = 0; + + [[nodiscard]] double DurationMilliseconds() const noexcept; + }; + + struct MWG_CORE_API PerformanceSnapshot + { + std::array timings{}; + std::array counters{}; + + [[nodiscard]] const PerformanceTiming& Timing(PerformanceOperation operation) const noexcept; + [[nodiscard]] std::int64_t Counter(PerformanceCounter counter) const noexcept; + }; + + /// Process-wide aggregate collector for opt-in performance diagnostics. + /// + /// Timings and counters are safe to record from concurrent threads. + /// Disabled recording has only an atomic enablement check at each + /// instrumentation point. Reset before starting a diagnostic run and + /// do not reset while measured work is active; otherwise a long-running + /// scope may add its completion to the new run. + class MWG_CORE_API PerformanceDiagnostics + { + public: + [[nodiscard]] static bool IsEnabled() noexcept; + static void SetEnabled(bool enabled) noexcept; + static void Reset() noexcept; + [[nodiscard]] static PerformanceSnapshot Snapshot() noexcept; + + static void IncrementCounter( + PerformanceCounter counter, + std::int64_t amount = 1) noexcept; + static void RecordDuration( + PerformanceOperation operation, + std::chrono::nanoseconds duration) noexcept; + }; + + /// Measures one operation with std::chrono::steady_clock when diagnostics + /// are enabled. A scope that started enabled records on destruction even + /// if diagnostics were disabled meanwhile. + class MWG_CORE_API PerformanceScope final + { + public: + explicit PerformanceScope(PerformanceOperation operation) noexcept; + ~PerformanceScope(); + + PerformanceScope(const PerformanceScope&) = delete; + PerformanceScope& operator=(const PerformanceScope&) = delete; + + private: + PerformanceOperation _operation; + std::chrono::steady_clock::time_point _start; + bool _enabled = false; + }; +} diff --git a/src/MapWinGIS.Core/include/mapwingis/rendering/MapRenderScene.h b/src/MapWinGIS.Core/include/mapwingis/rendering/MapRenderScene.h index 21cb9a2..97d5846 100644 --- a/src/MapWinGIS.Core/include/mapwingis/rendering/MapRenderScene.h +++ b/src/MapWinGIS.Core/include/mapwingis/rendering/MapRenderScene.h @@ -80,4 +80,13 @@ namespace mwg::rendering /// errors from individual layers are reported in skippedLayers. /// Exceptions not derived from std::exception propagate. [[nodiscard]] MWG_CORE_API MapRenderScene CreateMapRenderScene(const Map& map); + + /// Creates a map scene with explicit fill-preparation behavior. + /// + /// Hidden, empty, and failed layers retain the same handling as the + /// default overload. When prepareFillMeshes is false, every drawable + /// scene contains rings but no fill meshes. + [[nodiscard]] MWG_CORE_API MapRenderScene CreateMapRenderScene( + const Map& map, + const VectorRenderSceneOptions& options); } diff --git a/src/MapWinGIS.Core/include/mapwingis/rendering/VectorRenderScene.h b/src/MapWinGIS.Core/include/mapwingis/rendering/VectorRenderScene.h index cb8653b..0040577 100644 --- a/src/MapWinGIS.Core/include/mapwingis/rendering/VectorRenderScene.h +++ b/src/MapWinGIS.Core/include/mapwingis/rendering/VectorRenderScene.h @@ -127,6 +127,15 @@ namespace mwg std::vector indices; }; + /// Controls optional fill preparation while constructing a scene. + struct VectorRenderSceneOptions + { + /// When false, preserves polygon rings and leaves fillMeshes empty. + /// This is useful for baseline measurements and outline-only + /// backends; OpenGL currently requires fill meshes. + bool prepareFillMeshes = true; + }; + /// Owned polygon geometry and its source-coordinate bounds. struct MWG_CORE_API VectorRenderScene { @@ -138,12 +147,11 @@ namespace mwg /// exterior ring of at least three points. std::int64_t skippedGeometryCount = 0; PolygonBuffer polygons; - /// Triangulated fill meshes prepared once when the scene is - /// created, aligned with polygons: fillMeshes[i] belongs to - /// polygons[i]. An entry is std::nullopt when that polygon could - /// not be triangulated; its rings in polygons remain available - /// for outline drawing and for backends that draw rings directly, - /// such as GDI. + /// Fill meshes prepared once when the scene is created. This is + /// empty when scene options disable fill preparation; otherwise + /// fillMeshes[i] belongs to polygons[i]. An entry is std::nullopt + /// when that polygon could not be triangulated; its rings remain + /// available for outline drawing and for backends such as GDI. std::vector> fillMeshes; }; @@ -162,6 +170,14 @@ namespace mwg [[nodiscard]] MWG_CORE_API VectorRenderScene CreateVectorRenderScene( const VectorLayer& layer); + /// Creates a scene with explicit fill-preparation behavior. + /// + /// Disabling fill meshes measures and renders only original polygon + /// geometry; the default overload remains unchanged. + [[nodiscard]] MWG_CORE_API VectorRenderScene CreateVectorRenderScene( + const VectorLayer& layer, + const VectorRenderSceneOptions& options); + /// Copies polygon geometry from the first layer into owned scene data. /// /// A dataset with no layers is rejected. Only the first layer is @@ -170,5 +186,11 @@ namespace mwg /// is not polygonal or contains no polygon geometry to render. [[nodiscard]] MWG_CORE_API VectorRenderScene CreateVectorRenderScene( const VectorDataset& dataset); + + /// Creates the first layer's scene with explicit fill preparation. + /// Layer selection and errors match the default overload. + [[nodiscard]] MWG_CORE_API VectorRenderScene CreateVectorRenderScene( + const VectorDataset& dataset, + const VectorRenderSceneOptions& options); } } diff --git a/src/MapWinGIS.Core/src/diagnostics/PerformanceDiagnostics.cpp b/src/MapWinGIS.Core/src/diagnostics/PerformanceDiagnostics.cpp new file mode 100644 index 0000000..b8044dc --- /dev/null +++ b/src/MapWinGIS.Core/src/diagnostics/PerformanceDiagnostics.cpp @@ -0,0 +1,151 @@ +// SPDX-License-Identifier: MPL-2.0 + +/** + * @file PerformanceDiagnostics.cpp + * @brief Implements aggregate vector-pipeline performance diagnostics. + * + * Stores timings and counters in atomics so Core and ActiveX workers can + * record measurements without serializing the pipeline or allocating while + * rendering. + */ +#include "mapwingis/diagnostics/PerformanceDiagnostics.h" + +#include + +namespace mwg::diagnostics +{ + namespace + { + struct AtomicPerformanceState + { + std::atomic_bool enabled = false; + std::array callCounts{}; + std::array durations{}; + std::array counters{}; + }; + + AtomicPerformanceState& State() + { + static AtomicPerformanceState state; + return state; + } + + [[nodiscard]] std::size_t OperationIndex( + const PerformanceOperation operation) noexcept + { + return static_cast(operation); + } + + [[nodiscard]] std::size_t CounterIndex( + const PerformanceCounter counter) noexcept + { + return static_cast(counter); + } + } + + double PerformanceTiming::DurationMilliseconds() const noexcept + { + return static_cast(durationNanoseconds) / 1000000.0; + } + + const PerformanceTiming& PerformanceSnapshot::Timing( + const PerformanceOperation operation) const noexcept + { + return timings[OperationIndex(operation)]; + } + + std::int64_t PerformanceSnapshot::Counter( + const PerformanceCounter counter) const noexcept + { + return counters[CounterIndex(counter)]; + } + + bool PerformanceDiagnostics::IsEnabled() noexcept + { + return State().enabled.load(std::memory_order_relaxed); + } + + void PerformanceDiagnostics::SetEnabled(const bool enabled) noexcept + { + State().enabled.store(enabled, std::memory_order_relaxed); + } + + void PerformanceDiagnostics::Reset() noexcept + { + AtomicPerformanceState& state = State(); + for (std::atomic_int64_t& value : state.callCounts) + { + value.store(0, std::memory_order_relaxed); + } + for (std::atomic_int64_t& value : state.durations) + { + value.store(0, std::memory_order_relaxed); + } + for (std::atomic_int64_t& value : state.counters) + { + value.store(0, std::memory_order_relaxed); + } + } + + PerformanceSnapshot PerformanceDiagnostics::Snapshot() noexcept + { + const AtomicPerformanceState& state = State(); + PerformanceSnapshot result; + for (std::size_t index = 0; index < PerformanceOperationCount; ++index) + { + result.timings[index] = { + state.callCounts[index].load(std::memory_order_relaxed), + state.durations[index].load(std::memory_order_relaxed)}; + } + for (std::size_t index = 0; index < PerformanceCounterCount; ++index) + { + result.counters[index] = + state.counters[index].load(std::memory_order_relaxed); + } + return result; + } + + void PerformanceDiagnostics::IncrementCounter( + const PerformanceCounter counter, + const std::int64_t amount) noexcept + { + if (IsEnabled()) + { + State().counters[CounterIndex(counter)].fetch_add( + amount, std::memory_order_relaxed); + } + } + + void PerformanceDiagnostics::RecordDuration( + const PerformanceOperation operation, + const std::chrono::nanoseconds duration) noexcept + { + AtomicPerformanceState& state = State(); + state.callCounts[OperationIndex(operation)].fetch_add( + 1, std::memory_order_relaxed); + state.durations[OperationIndex(operation)].fetch_add( + duration.count(), std::memory_order_relaxed); + } + + PerformanceScope::PerformanceScope( + const PerformanceOperation operation) noexcept + : _operation(operation), + _enabled(PerformanceDiagnostics::IsEnabled()) + { + if (_enabled) + { + _start = std::chrono::steady_clock::now(); + } + } + + PerformanceScope::~PerformanceScope() + { + if (_enabled) + { + PerformanceDiagnostics::RecordDuration( + _operation, + std::chrono::duration_cast( + std::chrono::steady_clock::now() - _start)); + } + } +} diff --git a/src/MapWinGIS.Core/src/internal/VectorRenderSceneBuilder.h b/src/MapWinGIS.Core/src/internal/VectorRenderSceneBuilder.h index 9531221..f51e21d 100644 --- a/src/MapWinGIS.Core/src/internal/VectorRenderSceneBuilder.h +++ b/src/MapWinGIS.Core/src/internal/VectorRenderSceneBuilder.h @@ -19,12 +19,16 @@ namespace mwg::rendering::detail /// Builds drawable geometry or a skip diagnostic for one map layer. /// Allocation failures and non-standard exceptions propagate. - [[nodiscard]] LayerScene BuildLayerRenderScene(const MapLayer& layer); + [[nodiscard]] LayerScene BuildLayerRenderScene( + const MapLayer& layer, + const VectorRenderSceneOptions& options = {}); /// Copies polygon geometry from a layer into scene data. Unlike /// CreateVectorRenderScene, a layer without polygon geometry gives a /// scene with no polygons instead of an error. /// \throws CoreError when the layer is not polygonal or its geometry /// cannot be converted. - [[nodiscard]] VectorRenderScene BuildVectorRenderScene(const VectorLayer& layer); + [[nodiscard]] VectorRenderScene BuildVectorRenderScene( + const VectorLayer& layer, + const VectorRenderSceneOptions& options = {}); } diff --git a/src/MapWinGIS.Core/src/map/MapView.cpp b/src/MapWinGIS.Core/src/map/MapView.cpp index f2a8484..a167d97 100644 --- a/src/MapWinGIS.Core/src/map/MapView.cpp +++ b/src/MapWinGIS.Core/src/map/MapView.cpp @@ -14,6 +14,7 @@ #include #include "mapwingis/core/Error.h" +#include "mapwingis/diagnostics/PerformanceDiagnostics.h" #include "mapwingis/vector/VectorDataset.h" #include "internal/VectorRenderSceneBuilder.h" @@ -305,9 +306,16 @@ namespace mwg auto cached = _impl->_layerScenes.find(layer.Id()); if (cached == _impl->_layerScenes.end()) { + diagnostics::PerformanceDiagnostics::IncrementCounter( + diagnostics::PerformanceCounter::SceneCacheMisses); cached = _impl->_layerScenes.emplace( layer.Id(), rendering::detail::BuildLayerRenderScene(layer)).first; } + else + { + diagnostics::PerformanceDiagnostics::IncrementCounter( + diagnostics::PerformanceCounter::SceneCacheHits); + } if (const auto* drawable = std::get_if(&cached->second)) @@ -324,6 +332,12 @@ namespace mwg _impl->_scene = std::move(scene); _impl->_sceneStale = false; } + else + { + diagnostics::PerformanceDiagnostics::IncrementCounter( + diagnostics::PerformanceCounter::SceneCacheHits, + static_cast(_impl->_scene.layers.size())); + } return _impl->_scene; } diff --git a/src/MapWinGIS.Core/src/rendering/MapRenderScene.cpp b/src/MapWinGIS.Core/src/rendering/MapRenderScene.cpp index 799ef70..86acfac 100644 --- a/src/MapWinGIS.Core/src/rendering/MapRenderScene.cpp +++ b/src/MapWinGIS.Core/src/rendering/MapRenderScene.cpp @@ -15,7 +15,9 @@ namespace mwg::rendering { - detail::LayerScene detail::BuildLayerRenderScene(const MapLayer& layer) + detail::LayerScene detail::BuildLayerRenderScene( + const MapLayer& layer, + const VectorRenderSceneOptions& options) { if (!layer.IsVisible()) { @@ -25,7 +27,7 @@ namespace mwg::rendering VectorRenderScene scene; try { - scene = detail::BuildVectorRenderScene(layer.Source()); + scene = detail::BuildVectorRenderScene(layer.Source(), options); } catch (const std::bad_alloc&) { @@ -44,11 +46,19 @@ namespace mwg::rendering } MapRenderScene CreateMapRenderScene(const Map& map) + { + return CreateMapRenderScene(map, {}); + } + + MapRenderScene CreateMapRenderScene( + const Map& map, + const VectorRenderSceneOptions& options) { MapRenderScene result; for (int position = 0; position < map.LayerCount(); ++position) { - detail::LayerScene layerScene = detail::BuildLayerRenderScene(map.Layer(position)); + detail::LayerScene layerScene = + detail::BuildLayerRenderScene(map.Layer(position), options); if (auto* drawable = std::get_if(&layerScene)) { result.layers.push_back(std::move(*drawable)); diff --git a/src/MapWinGIS.Core/src/rendering/PolygonTriangulation.cpp b/src/MapWinGIS.Core/src/rendering/PolygonTriangulation.cpp index d984b88..de53762 100644 --- a/src/MapWinGIS.Core/src/rendering/PolygonTriangulation.cpp +++ b/src/MapWinGIS.Core/src/rendering/PolygonTriangulation.cpp @@ -24,6 +24,7 @@ #include #include "mapwingis/core/Error.h" +#include "mapwingis/diagnostics/PerformanceDiagnostics.h" #include "mapwingis/gdal/GdalDiagnostics.h" #include "mapwingis/gdal/GdalEnvironment.h" @@ -205,24 +206,55 @@ namespace mwg::rendering throw CoreError("Triangulation returned an unexpected geometry type"); } } + + class TriangulationFailureScope final + { + public: + void MarkSucceeded() noexcept + { + _succeeded = true; + } + + ~TriangulationFailureScope() + { + if (!_succeeded) + { + diagnostics::PerformanceDiagnostics::IncrementCounter( + diagnostics::PerformanceCounter::TriangulationFailures); + } + } + + private: + bool _succeeded = false; + }; } template TriangleMesh Triangulate(const PolygonType& polygon) { + TriangulationFailureScope failureScope; GdalEnvironment::Initialize(); const std::unique_ptr source = MakeOgrPolygon(polygon); GdalErrorScope scope; - const bool valid = source->IsValid(); + bool valid = false; + { + const diagnostics::PerformanceScope validationScope( + diagnostics::PerformanceOperation::GeometryValidation); + valid = source->IsValid(); + } scope.RethrowCaptureException(); if (!valid) { + diagnostics::PerformanceDiagnostics::IncrementCounter( + diagnostics::PerformanceCounter::InvalidGeometries); throw CoreError( "Polygon triangulation requires a valid polygon; the input is " "self-intersecting or otherwise invalid"); } + const diagnostics::PerformanceScope triangulationScope( + diagnostics::PerformanceOperation::PolygonTriangulation); std::unique_ptr triangulation; try { @@ -255,6 +287,12 @@ namespace mwg::rendering { throw CoreError("Polygon triangulation produced no triangles"); } + diagnostics::PerformanceDiagnostics::IncrementCounter( + diagnostics::PerformanceCounter::PolygonsTriangulated); + diagnostics::PerformanceDiagnostics::IncrementCounter( + diagnostics::PerformanceCounter::TrianglesGenerated, + static_cast(mesh.indices.size() / 3U)); + failureScope.MarkSucceeded(); return mesh; } diff --git a/src/MapWinGIS.Core/src/rendering/VectorRenderScene.cpp b/src/MapWinGIS.Core/src/rendering/VectorRenderScene.cpp index 7cd1511..52f1b83 100644 --- a/src/MapWinGIS.Core/src/rendering/VectorRenderScene.cpp +++ b/src/MapWinGIS.Core/src/rendering/VectorRenderScene.cpp @@ -18,6 +18,7 @@ #include #include "mapwingis/core/Error.h" +#include "mapwingis/diagnostics/PerformanceDiagnostics.h" #include "mapwingis/vector/VectorDataset.h" #include "internal/VectorLayerAccess.h" @@ -231,8 +232,12 @@ namespace mwg::rendering }; } - VectorRenderScene detail::BuildVectorRenderScene(const VectorLayer& layer) + VectorRenderScene detail::BuildVectorRenderScene( + const VectorLayer& layer, + const VectorRenderSceneOptions& options) { + const diagnostics::PerformanceScope scope( + diagnostics::PerformanceOperation::SceneBuild); if (BaseShapeType(layer.Type()) != ShapeType::Polygon) { throw CoreError("Vector rendering supports polygon layers only"); @@ -249,14 +254,17 @@ namespace mwg::rendering layer, [&builder](const OGRGeometry& geometry) { + const diagnostics::PerformanceScope conversionScope( + diagnostics::PerformanceOperation::GeometryConversion); builder.AppendGeometry(geometry); }, hasFastExtent ? nullptr : &scene.extent); - // Prepare one fill mesh per polygon while the scene is created, so - // renderers never triangulate per frame. A polygon that cannot be - // triangulated keeps a null mesh; its rings above remain available - // for outlines and ring-based backends such as GDI. + if (!options.prepareFillMeshes) + { + return scene; + } + scene.fillMeshes.reserve(scene.polygons.size()); for (std::size_t index = 0; index < scene.polygons.size(); ++index) { @@ -275,7 +283,14 @@ namespace mwg::rendering VectorRenderScene CreateVectorRenderScene(const VectorLayer& layer) { - VectorRenderScene scene = detail::BuildVectorRenderScene(layer); + return CreateVectorRenderScene(layer, {}); + } + + VectorRenderScene CreateVectorRenderScene( + const VectorLayer& layer, + const VectorRenderSceneOptions& options) + { + VectorRenderScene scene = detail::BuildVectorRenderScene(layer, options); if (scene.polygons.empty()) { throw CoreError("Vector layer contains no polygon geometry to render"); @@ -284,12 +299,19 @@ namespace mwg::rendering } VectorRenderScene CreateVectorRenderScene(const VectorDataset& dataset) + { + return CreateVectorRenderScene(dataset, {}); + } + + VectorRenderScene CreateVectorRenderScene( + const VectorDataset& dataset, + const VectorRenderSceneOptions& options) { if (dataset.LayerCount() == 0) { throw CoreError("Vector dataset contains no layers"); } - return CreateVectorRenderScene(dataset.Layer(0)); + return CreateVectorRenderScene(dataset.Layer(0), options); } } diff --git a/src/MapWinGIS.Core/src/vector/VectorDataset.cpp b/src/MapWinGIS.Core/src/vector/VectorDataset.cpp index 92efccc..1ffabe0 100644 --- a/src/MapWinGIS.Core/src/vector/VectorDataset.cpp +++ b/src/MapWinGIS.Core/src/vector/VectorDataset.cpp @@ -25,6 +25,7 @@ #include "internal/Utf8.h" #include "internal/VectorLayerAccess.h" #include "mapwingis/core/Error.h" +#include "mapwingis/diagnostics/PerformanceDiagnostics.h" #include "mapwingis/gdal/GdalEnvironment.h" namespace mwg @@ -163,6 +164,8 @@ namespace mwg Impl(GDALDataset& dataset, OGRLayer* layer, const bool writable) : _dataset(dataset), _layer(layer), _writable(writable) { + const diagnostics::PerformanceScope scope( + diagnostics::PerformanceOperation::LayerMetadataRead); if (const OGRFeatureDefn* defn = _layer->GetLayerDefn()) { _fields.reserve(static_cast(defn->GetFieldCount())); @@ -473,16 +476,31 @@ namespace mwg std::optional computedExtent; std::int64_t count = 0; - while (const OGRFeatureUniquePtr feature{ReadNextFeature(ogrLayer)}) + while (true) { + OGRFeatureUniquePtr feature; + { + const diagnostics::PerformanceScope scope( + diagnostics::PerformanceOperation::FeatureGeometryRead); + feature = ReadNextFeature(ogrLayer); + } + if (feature == nullptr) + { + break; + } + if (count == std::numeric_limits::max()) { throw CoreError("Vector layer feature count is out of range"); } ++count; + diagnostics::PerformanceDiagnostics::IncrementCounter( + diagnostics::PerformanceCounter::FeaturesProcessed); const OGRGeometry* geometry = feature->GetGeometryRef(); if (geometry != nullptr && !geometry->IsEmpty()) { + diagnostics::PerformanceDiagnostics::IncrementCounter( + diagnostics::PerformanceCounter::GeometriesLoaded); if (extent != nullptr) { OGREnvelope envelope; @@ -744,6 +762,8 @@ namespace mwg std::unique_ptr VectorDataset::TryOpen(const std::filesystem::path& path, const OpenMode mode) { + const diagnostics::PerformanceScope scope( + diagnostics::PerformanceOperation::DatasetOpen); GdalEnvironment::Initialize(); GdalEnvironment::ClearLastError(); diff --git a/tests/MapWinGIS.ActiveX.Tests/RenderingBaselineTests.cpp b/tests/MapWinGIS.ActiveX.Tests/RenderingBaselineTests.cpp index 979217a..7c30d67 100644 --- a/tests/MapWinGIS.ActiveX.Tests/RenderingBaselineTests.cpp +++ b/tests/MapWinGIS.ActiveX.Tests/RenderingBaselineTests.cpp @@ -39,6 +39,7 @@ #include "OpenGlVectorLayerRenderer.h" #include "GdiVectorLayerRenderer.h" #include "TestData.h" +#include "mapwingis/diagnostics/PerformanceDiagnostics.h" #include "mapwingis/gdal/GdalEnvironment.h" #include "mapwingis/map/Map.h" #include "mapwingis/map/Viewport.h" @@ -153,6 +154,26 @@ namespace return path; } + [[nodiscard]] std::filesystem::path BaselineDatasetPath( + const std::filesystem::path& directory, + const int generatedFeatureCount) + { + const char* const configuredPath = + std::getenv("MWG_RENDERING_BASELINE_DATASET"); + if (configuredPath == nullptr || configuredPath[0] == '\0') + { + return MakePolygonGrid(directory, generatedFeatureCount); + } + + const std::filesystem::path path(configuredPath); + if (!std::filesystem::exists(path)) + { + throw std::runtime_error( + "MWG_RENDERING_BASELINE_DATASET does not exist: " + path.string()); + } + return path; + } + /// Adds a closed ring to a polygon from (x, y) coordinate pairs. void AddRing(OGRPolygon& polygon, std::initializer_list> points) { @@ -371,9 +392,150 @@ namespace << ' ' << unit << '\n'; RecordProperty(name, std::to_string(value)); } + + void ReportPerformanceDiagnostics() + { + using diagnostics::PerformanceCounter; + using diagnostics::PerformanceDiagnostics; + using diagnostics::PerformanceOperation; + + constexpr std::pair operations[] = { + {PerformanceOperation::DatasetOpen, "Diagnostic dataset open"}, + {PerformanceOperation::LayerMetadataRead, "Diagnostic layer metadata"}, + {PerformanceOperation::FeatureGeometryRead, "Diagnostic feature geometry read"}, + {PerformanceOperation::GeometryConversion, "Diagnostic geometry conversion"}, + {PerformanceOperation::SceneBuild, "Diagnostic scene build"}, + {PerformanceOperation::GeometryValidation, "Diagnostic geometry validation"}, + {PerformanceOperation::PolygonTriangulation, "Diagnostic polygon triangulation"}, + {PerformanceOperation::OpenGlGeometrySubmission, "Diagnostic OpenGL geometry submission"}, + {PerformanceOperation::FrameRender, "Diagnostic complete frame render"}, + {PerformanceOperation::FramebufferReadback, "Diagnostic framebuffer readback"}}; + constexpr std::pair counters[] = { + {PerformanceCounter::FeaturesProcessed, "Diagnostic features processed"}, + {PerformanceCounter::GeometriesLoaded, "Diagnostic geometries loaded"}, + {PerformanceCounter::PolygonsTriangulated, "Diagnostic polygons triangulated"}, + {PerformanceCounter::TrianglesGenerated, "Diagnostic triangles generated"}, + {PerformanceCounter::InvalidGeometries, "Diagnostic invalid geometries"}, + {PerformanceCounter::TriangulationFailures, "Diagnostic triangulation failures"}, + {PerformanceCounter::FeaturesSkippedOutsideViewport, + "Diagnostic features outside viewport"}, + {PerformanceCounter::SceneCacheHits, "Diagnostic scene cache hits"}, + {PerformanceCounter::SceneCacheMisses, "Diagnostic scene cache misses"}}; + + const diagnostics::PerformanceSnapshot snapshot = + PerformanceDiagnostics::Snapshot(); + for (const auto& [operation, name] : operations) + { + const diagnostics::PerformanceTiming& timing = snapshot.Timing(operation); + Report(name, timing.DurationMilliseconds(), "ms"); + Report((std::string(name) + " calls").c_str(), static_cast(timing.callCount), ""); + } + for (const auto& [counter, name] : counters) + { + Report(name, static_cast(snapshot.Counter(counter)), ""); + } + } + + class DiagnosticCapture final + { + public: + DiagnosticCapture() + { + diagnostics::PerformanceDiagnostics::SetEnabled(false); + diagnostics::PerformanceDiagnostics::Reset(); + diagnostics::PerformanceDiagnostics::SetEnabled(true); + } + + ~DiagnosticCapture() + { + diagnostics::PerformanceDiagnostics::SetEnabled(false); + diagnostics::PerformanceDiagnostics::Reset(); + } + }; }; } +TEST_F(RenderingBaseline, MeasuresTriangulationDisabledCosts) +{ + const char* enabled = std::getenv("MWG_RUN_RENDERING_BASELINE"); + if (enabled == nullptr || std::string(enabled) != "1") + { + GTEST_SKIP() << "Set MWG_RUN_RENDERING_BASELINE=1 to measure the rendering baseline."; + } + + DiagnosticCapture diagnostics; + const int featureCount = FeatureCountFromEnvironment(); + TempDirectory directory; + const std::filesystem::path path = + BaselineDatasetPath(directory.Path(), featureCount); + + Clock::time_point start = Clock::now(); + Map map; + map.AddLayer(VectorDataset::Open(path)); + const double openMs = ElapsedMilliseconds(start); + + start = Clock::now(); + const rendering::MapRenderScene scene = + rendering::CreateMapRenderScene( + map, + rendering::VectorRenderSceneOptions{.prepareFillMeshes = false}); + const double sceneMs = ElapsedMilliseconds(start); + ASSERT_EQ(scene.layers.size(), 1U); + ASSERT_TRUE(scene.layers.front().scene->fillMeshes.empty()); + const std::int64_t measuredFeatureCount = + scene.layers.front().scene->sourceFeatureCount; + ASSERT_GT(measuredFeatureCount, 0); + + DibSurface surface(kTargetWidth, kTargetHeight); + ASSERT_TRUE(surface.IsValid()); + const RECT bounds{0, 0, kTargetWidth, kTargetHeight}; + Viewport viewport(kTargetWidth, kTargetHeight); + ASSERT_TRUE(viewport.FitExtent(map.GetExtent(), 12.0)); + + activex::GdiVectorLayerRenderer gdi; + std::vector gdiFrames; + for (int frame = 0; frame < kSteadyFrames; ++frame) + { + start = Clock::now(); + ASSERT_EQ(gdi.Draw(surface.Get(), bounds, scene, viewport), S_OK); + gdiFrames.push_back(ElapsedMilliseconds(start)); + } + + Viewport panned = viewport; + ASSERT_TRUE(panned.Pan(1.0, 1.0)); + start = Clock::now(); + ASSERT_EQ(gdi.Draw(surface.Get(), bounds, scene, panned), S_OK); + const double gdiPanMs = ElapsedMilliseconds(start); + + Viewport zoomed = viewport; + ASSERT_TRUE(zoomed.Zoom(2.0)); + start = Clock::now(); + ASSERT_EQ(gdi.Draw(surface.Get(), bounds, scene, zoomed), S_OK); + const double gdiZoomMs = ElapsedMilliseconds(start); + + activex::OpenGlVectorLayerRenderer openGl; + start = Clock::now(); + const HRESULT openGlResult = openGl.Draw(surface.Get(), bounds, scene, viewport); + const double openGlMs = ElapsedMilliseconds(start); + EXPECT_EQ(openGlResult, S_FALSE) + << "The current OpenGL backend must reject scenes without fill meshes."; + + std::cout << "Triangulation-disabled baseline: " << measuredFeatureCount + << " source features (" << scene.layers.front().scene->polygons.size() + << " polygons), " << kTargetWidth << 'x' << kTargetHeight << " target\n"; + Report("Core open dataset", openMs, "ms"); + Report("Core prepare outline-only scene", sceneMs, "ms"); + Report("GDI first frame", gdiFrames.front(), "ms"); + Report( + "GDI steady frame median", + Median(std::vector(gdiFrames.begin() + 1, gdiFrames.end())), + "ms"); + Report("GDI pan frame", gdiPanMs, "ms"); + Report("GDI zoom frame", gdiZoomMs, "ms"); + Report("OpenGL no-mesh rejection", openGlMs, "ms"); + ReportPerformanceDiagnostics(); +} + TEST_F(RenderingBaseline, MeasuresCoreSceneAndBackendCosts) { const char* enabled = std::getenv("MWG_RUN_RENDERING_BASELINE"); @@ -382,9 +544,11 @@ TEST_F(RenderingBaseline, MeasuresCoreSceneAndBackendCosts) GTEST_SKIP() << "Set MWG_RUN_RENDERING_BASELINE=1 to measure the rendering baseline."; } + DiagnosticCapture diagnostics; const int featureCount = FeatureCountFromEnvironment(); TempDirectory directory; - const std::filesystem::path path = MakePolygonGrid(directory.Path(), featureCount); + const std::filesystem::path path = + BaselineDatasetPath(directory.Path(), featureCount); DWORD memoryError = ERROR_SUCCESS; const std::optional memoryBefore = PrivateMiB(memoryError); @@ -400,7 +564,10 @@ TEST_F(RenderingBaseline, MeasuresCoreSceneAndBackendCosts) const std::optional memoryAfterScene = PrivateMiB(memoryError); ASSERT_TRUE(memoryAfterScene.has_value()) << "GetProcessMemoryInfo failed: " << memoryError; ASSERT_EQ(scene.layers.size(), 1U); - ASSERT_EQ(scene.layers.front().scene->polygons.size(), static_cast(featureCount)); + ASSERT_FALSE(scene.layers.front().scene->polygons.empty()); + const std::int64_t measuredFeatureCount = + scene.layers.front().scene->sourceFeatureCount; + ASSERT_GT(measuredFeatureCount, 0); DibSurface surface(kTargetWidth, kTargetHeight); ASSERT_TRUE(surface.IsValid()); @@ -417,13 +584,31 @@ TEST_F(RenderingBaseline, MeasuresCoreSceneAndBackendCosts) gdiFrames.push_back(ElapsedMilliseconds(start)); } - std::cout << "Rendering baseline: " << featureCount << " convex polygons x " - << kVerticesPerPolygon << " vertices, " << kTargetWidth << 'x' - << kTargetHeight << " target\n"; + Viewport pannedGdi = viewport; + ASSERT_TRUE(pannedGdi.Pan(1.0, 1.0)); + start = Clock::now(); + ASSERT_EQ(gdi.Draw(surface.Get(), bounds, scene, pannedGdi), S_OK); + const double gdiPanMs = ElapsedMilliseconds(start); + + Viewport zoomedGdi = viewport; + ASSERT_TRUE(zoomedGdi.Zoom(2.0)); + start = Clock::now(); + ASSERT_EQ(gdi.Draw(surface.Get(), bounds, scene, zoomedGdi), S_OK); + const double gdiZoomMs = ElapsedMilliseconds(start); + + std::cout << "Rendering baseline: " << measuredFeatureCount + << " source features (" << scene.layers.front().scene->polygons.size() + << " polygons), " << kTargetWidth << 'x' << kTargetHeight << " target\n"; Report("Core open dataset", openMs, "ms"); Report("Core prepare render scene", sceneMs, "ms"); Report("Private memory increase for scene", *memoryAfterScene - *memoryBefore, "MiB"); - Report("GDI frame (median)", Median(gdiFrames), "ms"); + Report("GDI first frame", gdiFrames.front(), "ms"); + Report( + "GDI frame (median)", + Median(std::vector(gdiFrames.begin() + 1, gdiFrames.end())), + "ms"); + Report("GDI pan frame", gdiPanMs, "ms"); + Report("GDI zoom frame", gdiZoomMs, "ms"); activex::OpenGlVectorLayerRenderer openGl; start = Clock::now(); @@ -432,6 +617,7 @@ TEST_F(RenderingBaseline, MeasuresCoreSceneAndBackendCosts) if (firstFrame == S_FALSE) { std::wcout << L" OpenGL unavailable: " << openGl.GetLastFailureReason() << L'\n'; + ReportPerformanceDiagnostics(); return; } ASSERT_EQ(firstFrame, S_OK); @@ -444,11 +630,26 @@ TEST_F(RenderingBaseline, MeasuresCoreSceneAndBackendCosts) openGlFrames.push_back(ElapsedMilliseconds(start)); } + Viewport pannedOpenGl = viewport; + ASSERT_TRUE(pannedOpenGl.Pan(1.0, 1.0)); + start = Clock::now(); + ASSERT_EQ(openGl.Draw(surface.Get(), bounds, scene, pannedOpenGl), S_OK); + const double openGlPanMs = ElapsedMilliseconds(start); + + Viewport zoomedOpenGl = viewport; + ASSERT_TRUE(zoomedOpenGl.Zoom(2.0)); + start = Clock::now(); + ASSERT_EQ(openGl.Draw(surface.Get(), bounds, scene, zoomedOpenGl), S_OK); + const double openGlZoomMs = ElapsedMilliseconds(start); + Report("OpenGL first frame (context+draw)", firstOpenGlMs, "ms"); Report("OpenGL frame (median)", Median(openGlFrames), "ms"); + Report("OpenGL pan frame", openGlPanMs, "ms"); + Report("OpenGL zoom frame", openGlZoomMs, "ms"); const std::optional memoryAfterOpenGl = PrivateMiB(memoryError); ASSERT_TRUE(memoryAfterOpenGl.has_value()) << "GetProcessMemoryInfo failed: " << memoryError; Report("Private memory increase total", *memoryAfterOpenGl - *memoryBefore, "MiB"); + ReportPerformanceDiagnostics(); } TEST_F(RenderingBaseline, MeasuresComplexPolygonCosts) @@ -459,6 +660,7 @@ TEST_F(RenderingBaseline, MeasuresComplexPolygonCosts) GTEST_SKIP() << "Set MWG_RUN_RENDERING_BASELINE=1 to measure the rendering baseline."; } + DiagnosticCapture diagnostics; const int featureCount = FeatureCountFromEnvironment(); TempDirectory directory; const std::filesystem::path path = @@ -539,6 +741,7 @@ TEST_F(RenderingBaseline, MeasuresComplexPolygonCosts) if (firstFrame == S_FALSE) { std::wcout << L" OpenGL unavailable: " << openGl.GetLastFailureReason() << L'\n'; + ReportPerformanceDiagnostics(); return; } ASSERT_EQ(firstFrame, S_OK); @@ -556,4 +759,5 @@ TEST_F(RenderingBaseline, MeasuresComplexPolygonCosts) const std::optional memoryAfterOpenGl = PrivateMiB(memoryError); ASSERT_TRUE(memoryAfterOpenGl.has_value()) << "GetProcessMemoryInfo failed: " << memoryError; Report("Private memory increase total", *memoryAfterOpenGl - *memoryBefore, "MiB"); + ReportPerformanceDiagnostics(); } diff --git a/tests/MapWinGIS.ActiveX.Tests/VectorLayerRendererTests.cpp b/tests/MapWinGIS.ActiveX.Tests/VectorLayerRendererTests.cpp index 90ef80a..3ac94a9 100644 --- a/tests/MapWinGIS.ActiveX.Tests/VectorLayerRendererTests.cpp +++ b/tests/MapWinGIS.ActiveX.Tests/VectorLayerRendererTests.cpp @@ -26,6 +26,7 @@ #include "ViewTransform.h" #include "mapwingis/core/Error.h" +#include "mapwingis/diagnostics/PerformanceDiagnostics.h" #include "mapwingis/rendering/PolygonTriangulation.h" namespace @@ -222,6 +223,23 @@ namespace constexpr mwg::Extent kTwoLayerExtent{0.0, 0.0, 10.0, 5.0}; + class PerformanceCapture final + { + public: + PerformanceCapture() + { + mwg::diagnostics::PerformanceDiagnostics::SetEnabled(false); + mwg::diagnostics::PerformanceDiagnostics::Reset(); + mwg::diagnostics::PerformanceDiagnostics::SetEnabled(true); + } + + ~PerformanceCapture() + { + mwg::diagnostics::PerformanceDiagnostics::SetEnabled(false); + mwg::diagnostics::PerformanceDiagnostics::Reset(); + } + }; + /// Checks the pixels of MakeTwoLayerScene drawn through viewport. void ExpectTwoLayerPixels(const HDC surface, const mwg::Viewport& viewport) { @@ -234,6 +252,28 @@ namespace } } +TEST(VectorLayerRenderer, RecordsGdiFrameDurationWhenDiagnosticsAreEnabled) +{ + PerformanceCapture diagnostics; + BitmapSurface surface(120, 100); + ASSERT_TRUE(surface.IsValid()); + + const RECT bounds{0, 0, 120, 100}; + const mwg::rendering::MapRenderScene scene = MakeConvexPolygonScene(); + const mwg::Viewport viewport = FitViewport(bounds); + mwg::activex::VectorLayerRenderer renderer; + renderer.SetPreference(mwg::activex::VectorRenderBackendPreference::Gdi); + + ASSERT_EQ(renderer.Draw(surface.Get(), bounds, scene, viewport), S_OK); + + const mwg::diagnostics::PerformanceSnapshot snapshot = + mwg::diagnostics::PerformanceDiagnostics::Snapshot(); + const mwg::diagnostics::PerformanceTiming& frame = + snapshot.Timing(mwg::diagnostics::PerformanceOperation::FrameRender); + EXPECT_EQ(frame.callCount, 1); + EXPECT_GE(frame.durationNanoseconds, 0); +} + TEST(VectorLayerRenderer, UsesOpenGlForConvexPolygonWhenAvailable) { BitmapSurface surface(120, 100); diff --git a/tests/MapWinGIS.Core.Tests/CMakeLists.txt b/tests/MapWinGIS.Core.Tests/CMakeLists.txt index 4167739..bb01302 100644 --- a/tests/MapWinGIS.Core.Tests/CMakeLists.txt +++ b/tests/MapWinGIS.Core.Tests/CMakeLists.txt @@ -24,6 +24,7 @@ set(MWG_CORE_TEST_SOURCES MapRenderSceneTests.cpp MapTests.cpp MapViewTests.cpp + PerformanceDiagnosticsTests.cpp PolygonTriangulationTests.cpp TestData.cpp VectorDatasetTests.cpp diff --git a/tests/MapWinGIS.Core.Tests/MapWinGIS.Core.Tests.vcxproj b/tests/MapWinGIS.Core.Tests/MapWinGIS.Core.Tests.vcxproj index 2c0f845..10f7d81 100644 --- a/tests/MapWinGIS.Core.Tests/MapWinGIS.Core.Tests.vcxproj +++ b/tests/MapWinGIS.Core.Tests/MapWinGIS.Core.Tests.vcxproj @@ -69,6 +69,7 @@ + diff --git a/tests/MapWinGIS.Core.Tests/PerformanceDiagnosticsTests.cpp b/tests/MapWinGIS.Core.Tests/PerformanceDiagnosticsTests.cpp new file mode 100644 index 0000000..4334dc2 --- /dev/null +++ b/tests/MapWinGIS.Core.Tests/PerformanceDiagnosticsTests.cpp @@ -0,0 +1,181 @@ +// SPDX-License-Identifier: MPL-2.0 + +/** + * @file PerformanceDiagnosticsTests.cpp + * @brief Tests opt-in timing and counter aggregation for vector pipelines. + */ +#include + +#include +#include +#include + +#include "TestData.h" +#include "mapwingis/core/Error.h" +#include "mapwingis/diagnostics/PerformanceDiagnostics.h" +#include "mapwingis/gdal/GdalEnvironment.h" +#include "mapwingis/map/MapView.h" +#include "mapwingis/rendering/PolygonTriangulation.h" +#include "mapwingis/rendering/VectorRenderScene.h" +#include "mapwingis/vector/VectorDataset.h" + +using namespace mwg; +using namespace mwg::diagnostics; +using namespace mwg::test; + +namespace +{ + class PerformanceDiagnosticsTest : public testing::Test + { + protected: + void SetUp() override + { + PerformanceDiagnostics::SetEnabled(false); + PerformanceDiagnostics::Reset(); + PerformanceDiagnostics::SetEnabled(true); + } + + void TearDown() override + { + PerformanceDiagnostics::SetEnabled(false); + PerformanceDiagnostics::Reset(); + } + }; +} + +TEST_F(PerformanceDiagnosticsTest, AggregatesScopesAndCountersFromConcurrentThreads) +{ + constexpr int kThreadCount = 4; + constexpr int kIncrementsPerThread = 100; + std::vector threads; + threads.reserve(kThreadCount); + for (int index = 0; index < kThreadCount; ++index) + { + threads.emplace_back([]() + { + const PerformanceScope scope( + PerformanceOperation::OpenGlGeometrySubmission); + for (int increment = 0; increment < kIncrementsPerThread; ++increment) + { + PerformanceDiagnostics::IncrementCounter( + PerformanceCounter::FeaturesProcessed); + } + }); + } + for (std::thread& thread : threads) + { + thread.join(); + } + + const PerformanceSnapshot snapshot = PerformanceDiagnostics::Snapshot(); + EXPECT_EQ( + snapshot.Timing(PerformanceOperation::OpenGlGeometrySubmission).callCount, + kThreadCount); + EXPECT_GE( + snapshot.Timing(PerformanceOperation::OpenGlGeometrySubmission).durationNanoseconds, + 0); + EXPECT_EQ( + snapshot.Counter(PerformanceCounter::FeaturesProcessed), + kThreadCount * kIncrementsPerThread); +} + +TEST_F(PerformanceDiagnosticsTest, RecordsDatasetAndScenePipelineStages) +{ + TempDirectory directory; + constexpr std::array rectangles{{{0, 0, 10, 10}}}; + const std::filesystem::path path = + MakeRectanglesDataset(directory.Path(), "diagnostics.gpkg", rectangles); + + PerformanceDiagnostics::Reset(); + const std::unique_ptr dataset = VectorDataset::Open(path); + const rendering::VectorRenderScene scene = + rendering::CreateVectorRenderScene(*dataset); + + ASSERT_EQ(scene.polygons.size(), 1U); + ASSERT_EQ(scene.fillMeshes.size(), 1U); + const PerformanceSnapshot snapshot = PerformanceDiagnostics::Snapshot(); + EXPECT_EQ(snapshot.Timing(PerformanceOperation::DatasetOpen).callCount, 1); + EXPECT_EQ(snapshot.Timing(PerformanceOperation::LayerMetadataRead).callCount, 1); + EXPECT_EQ(snapshot.Timing(PerformanceOperation::FeatureGeometryRead).callCount, 2); + EXPECT_EQ(snapshot.Timing(PerformanceOperation::GeometryConversion).callCount, 1); + EXPECT_EQ(snapshot.Timing(PerformanceOperation::SceneBuild).callCount, 1); + EXPECT_EQ(snapshot.Timing(PerformanceOperation::GeometryValidation).callCount, 1); + EXPECT_EQ(snapshot.Timing(PerformanceOperation::PolygonTriangulation).callCount, 1); + EXPECT_EQ(snapshot.Counter(PerformanceCounter::FeaturesProcessed), 1); + EXPECT_EQ(snapshot.Counter(PerformanceCounter::GeometriesLoaded), 1); + EXPECT_EQ(snapshot.Counter(PerformanceCounter::PolygonsTriangulated), 1); + EXPECT_GT(snapshot.Counter(PerformanceCounter::TrianglesGenerated), 0); +} + +TEST_F(PerformanceDiagnosticsTest, CanPrepareScenesWithoutFillMeshes) +{ + TempDirectory directory; + constexpr std::array rectangles{{{0, 0, 10, 10}}}; + const std::filesystem::path path = + MakeRectanglesDataset(directory.Path(), "outlines.gpkg", rectangles); + const std::unique_ptr dataset = VectorDataset::Open(path); + + PerformanceDiagnostics::Reset(); + const rendering::VectorRenderScene scene = + rendering::CreateVectorRenderScene( + *dataset, + rendering::VectorRenderSceneOptions{.prepareFillMeshes = false}); + + EXPECT_EQ(scene.polygons.size(), 1U); + EXPECT_TRUE(scene.fillMeshes.empty()); + const PerformanceSnapshot snapshot = PerformanceDiagnostics::Snapshot(); + EXPECT_EQ(snapshot.Timing(PerformanceOperation::GeometryValidation).callCount, 0); + EXPECT_EQ(snapshot.Timing(PerformanceOperation::PolygonTriangulation).callCount, 0); + EXPECT_EQ(snapshot.Counter(PerformanceCounter::PolygonsTriangulated), 0); +} + +TEST_F(PerformanceDiagnosticsTest, RecordsSceneCacheMissesAndHits) +{ + TempDirectory directory; + constexpr std::array rectangles{{{0, 0, 10, 10}}}; + const std::filesystem::path path = + MakeRectanglesDataset(directory.Path(), "cache.gpkg", rectangles); + MapView view; + (void)view.AddLayer(VectorDataset::Open(path)); + + PerformanceDiagnostics::Reset(); + ASSERT_FALSE(view.Scene().layers.empty()); + ASSERT_FALSE(view.Scene().layers.empty()); + + const PerformanceSnapshot snapshot = PerformanceDiagnostics::Snapshot(); + EXPECT_EQ(snapshot.Counter(PerformanceCounter::SceneCacheMisses), 1); + EXPECT_EQ(snapshot.Counter(PerformanceCounter::SceneCacheHits), 1); +} + +TEST_F(PerformanceDiagnosticsTest, RecordsInvalidAndFailedTriangulation) +{ + const rendering::Polygon bowTie{{ + {0, 0}, {2, 2}, {0, 2}, {2, 0}, {0, 0}}}; + + { + const GdalEnvironment::QuietScope quiet; + EXPECT_THROW( + (void)rendering::TriangulatePolygon(bowTie), + CoreError); + } + + const PerformanceSnapshot snapshot = PerformanceDiagnostics::Snapshot(); + EXPECT_EQ(snapshot.Counter(PerformanceCounter::InvalidGeometries), 1); + EXPECT_EQ(snapshot.Counter(PerformanceCounter::TriangulationFailures), 1); + EXPECT_EQ(snapshot.Counter(PerformanceCounter::PolygonsTriangulated), 0); +} + +TEST(PerformanceDiagnosticsDisabled, DoesNotRecordNewMeasurements) +{ + PerformanceDiagnostics::SetEnabled(false); + PerformanceDiagnostics::Reset(); + { + const PerformanceScope scope(PerformanceOperation::SceneBuild); + PerformanceDiagnostics::IncrementCounter( + PerformanceCounter::FeaturesProcessed); + } + + const PerformanceSnapshot snapshot = PerformanceDiagnostics::Snapshot(); + EXPECT_EQ(snapshot.Timing(PerformanceOperation::SceneBuild).callCount, 0); + EXPECT_EQ(snapshot.Counter(PerformanceCounter::FeaturesProcessed), 0); +} From 5dabe75c2677356819d6c70425c56f4db938c60c Mon Sep 17 00:00:00 2001 From: Paul Meems Date: Thu, 19 Nov 2026 18:00:15 +0100 Subject: [PATCH 2/6] =?UTF-8?q?=F0=9F=93=9D=20docs(performance):=20record?= =?UTF-8?q?=20baseline=20shapefile=20timings?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Add all A-D timings for the supplied 10.3 MB shapefile - Record the dominant pipeline costs and estimated first-display times Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- ...ector-Rendering-Performance-Improvement.md | 35 ++++++++++++++++++- 1 file changed, 34 insertions(+), 1 deletion(-) diff --git a/.github/plans/Vector-Rendering-Performance-Improvement.md b/.github/plans/Vector-Rendering-Performance-Improvement.md index 1620951..c034507 100644 --- a/.github/plans/Vector-Rendering-Performance-Improvement.md +++ b/.github/plans/Vector-Rendering-Performance-Improvement.md @@ -6,7 +6,8 @@ Copilot: Update this checklist as each phase is implemented. Only mark a phase c - [x] **Phase 1 – Establish a performance baseline** (PR 1) - Added opt-in, thread-safe timings and counters across Core and ActiveX, plus A-D benchmark configurations and Release results in `docs/TESTING.md`. - - Limitation: the referenced 10.3 MB shapefile is not stored in the repository; `MWG_RENDERING_BASELINE_DATASET` supports measuring that file when available. + - Measured the supplied 10.3 MB, 2,459-feature shapefile with all A-D configurations; results are recorded in the Phase 1 benchmark section below. + - Limitation: the measured shapefile is not stored in the repository; `MWG_RENDERING_BASELINE_DATASET` can rerun it when available. - Limitation: configuration C records the current OpenGL rejection because outline-only OpenGL rendering is a Phase 2 change. - [ ] **Phase 2 – Separate geometry loading from rendering preparation** (PR 2) - [ ] **Phase 3 – Implement lazy polygon triangulation** (PR 3) @@ -113,6 +114,38 @@ Also measure subsequent zoom and pan operations. A performance report identifying the dominant costs and providing a baseline for future comparisons. +### Baseline results for the supplied 10.3 MB shapefile + +Release x64, 1,280 x 720 target, 2,459 source features, and 2,470 polygons: + +| Configuration | Open | Scene preparation | First frame | Steady frame | Pan | Zoom | +|---|---:|---:|---:|---:|---:|---:| +| A. GDI, triangulation disabled | 2.9 ms | 101.4 ms | 19.2 ms | 18.3 ms | 18.5 ms | 16.6 ms | +| B. GDI, triangulation enabled | 0.4 ms | 1,948.8 ms | 19.4 ms | 17.7 ms | 18.0 ms | 15.5 ms | +| C. OpenGL, triangulation disabled | 2.9 ms | 101.4 ms | Rejected | — | — | — | +| D. OpenGL, triangulation enabled | 0.4 ms | 1,948.8 ms | 162.5 ms | 15.2 ms | 14.7 ms | 15.3 ms | + +Configuration C is rejected because the current OpenGL backend requires a +triangle mesh for every polygon. This is expected to be addressed in Phase 2. + +Pipeline detail for triangulation-enabled scene construction: + +| Operation | Time | +|---|---:| +| OGR feature and geometry reads | 84.3 ms | +| Internal geometry conversion | 7.3 ms | +| GEOS geometry validation | 56.3 ms | +| Polygon triangulation | 1,789.6 ms | + +The run generated 586,414 triangles. Geometry submission took 102.1 ms +across eight OpenGL frames, and framebuffer readback took 3.8 ms across the +same frames. Triangulation accounted for about 92% of scene preparation. +The estimated open-to-first-display times were: + +- GDI without triangulation: 123.5 ms. +- GDI with triangulation: 1,968.6 ms. +- OpenGL with triangulation: 2,111.7 ms. + Do not proceed by simply assuming that GEOS triangulation accounts for the entire delay. --- From 100f372060c2eaaa519955a56781ecca6af6a729 Mon Sep 17 00:00:00 2001 From: Paul Meems Date: Thu, 19 Nov 2026 18:28:17 +0100 Subject: [PATCH 3/6] =?UTF-8?q?=F0=9F=90=9B=20fix(diagnostics):=20honor=20?= =?UTF-8?q?disabled=20recording?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Prevent public duration recording while diagnostics are disabled`n- Preserve completion timing for scopes that started enabled Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- .../diagnostics/PerformanceDiagnostics.cpp | 22 ++++++++++++++----- .../PerformanceDiagnosticsTests.cpp | 15 +++++++++++++ 2 files changed, 31 insertions(+), 6 deletions(-) diff --git a/src/MapWinGIS.Core/src/diagnostics/PerformanceDiagnostics.cpp b/src/MapWinGIS.Core/src/diagnostics/PerformanceDiagnostics.cpp index b8044dc..72579f7 100644 --- a/src/MapWinGIS.Core/src/diagnostics/PerformanceDiagnostics.cpp +++ b/src/MapWinGIS.Core/src/diagnostics/PerformanceDiagnostics.cpp @@ -41,6 +41,17 @@ namespace mwg::diagnostics { return static_cast(counter); } + + void RecordDurationUnconditionally( + const PerformanceOperation operation, + const std::chrono::nanoseconds duration) noexcept + { + AtomicPerformanceState& state = State(); + state.callCounts[OperationIndex(operation)].fetch_add( + 1, std::memory_order_relaxed); + state.durations[OperationIndex(operation)].fetch_add( + duration.count(), std::memory_order_relaxed); + } } double PerformanceTiming::DurationMilliseconds() const noexcept @@ -120,11 +131,10 @@ namespace mwg::diagnostics const PerformanceOperation operation, const std::chrono::nanoseconds duration) noexcept { - AtomicPerformanceState& state = State(); - state.callCounts[OperationIndex(operation)].fetch_add( - 1, std::memory_order_relaxed); - state.durations[OperationIndex(operation)].fetch_add( - duration.count(), std::memory_order_relaxed); + if (IsEnabled()) + { + RecordDurationUnconditionally(operation, duration); + } } PerformanceScope::PerformanceScope( @@ -142,7 +152,7 @@ namespace mwg::diagnostics { if (_enabled) { - PerformanceDiagnostics::RecordDuration( + RecordDurationUnconditionally( _operation, std::chrono::duration_cast( std::chrono::steady_clock::now() - _start)); diff --git a/tests/MapWinGIS.Core.Tests/PerformanceDiagnosticsTests.cpp b/tests/MapWinGIS.Core.Tests/PerformanceDiagnosticsTests.cpp index 4334dc2..26ce242 100644 --- a/tests/MapWinGIS.Core.Tests/PerformanceDiagnosticsTests.cpp +++ b/tests/MapWinGIS.Core.Tests/PerformanceDiagnosticsTests.cpp @@ -174,8 +174,23 @@ TEST(PerformanceDiagnosticsDisabled, DoesNotRecordNewMeasurements) PerformanceDiagnostics::IncrementCounter( PerformanceCounter::FeaturesProcessed); } + PerformanceDiagnostics::RecordDuration( + PerformanceOperation::FrameRender, + std::chrono::milliseconds(1)); const PerformanceSnapshot snapshot = PerformanceDiagnostics::Snapshot(); EXPECT_EQ(snapshot.Timing(PerformanceOperation::SceneBuild).callCount, 0); + EXPECT_EQ(snapshot.Timing(PerformanceOperation::FrameRender).callCount, 0); EXPECT_EQ(snapshot.Counter(PerformanceCounter::FeaturesProcessed), 0); } + +TEST_F(PerformanceDiagnosticsTest, RecordsScopeThatStartsWhileEnabled) +{ + { + const PerformanceScope scope(PerformanceOperation::SceneBuild); + PerformanceDiagnostics::SetEnabled(false); + } + + const PerformanceSnapshot snapshot = PerformanceDiagnostics::Snapshot(); + EXPECT_EQ(snapshot.Timing(PerformanceOperation::SceneBuild).callCount, 1); +} From ff6169ddff044b182462687f345838d64972d20a Mon Sep 17 00:00:00 2001 From: Paul Meems Date: Thu, 19 Nov 2026 22:20:09 +0100 Subject: [PATCH 4/6] Enhance performance diagnostics and update TESTING.md Updated performance diagnostics reporting in MapView and clarified testing documentation. Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com> --- docs/TESTING.md | 9 +++++---- src/MapWinGIS.Core/src/map/MapView.cpp | 13 ++++++++++--- 2 files changed, 15 insertions(+), 7 deletions(-) diff --git a/docs/TESTING.md b/docs/TESTING.md index 742d1e3..79869c2 100644 --- a/docs/TESTING.md +++ b/docs/TESTING.md @@ -202,11 +202,12 @@ For build and manual-use instructions, see the The `RenderingBaseline` tests are opt-in. They use either a generated GeoPackage grid of convex 32-vertex polygons or the dataset named by -`MWG_RENDERING_BASELINE_DATASET`. `MeasuresTriangulationDisabledCosts` builds an -outline-only scene (configuration A for GDI and configuration C for OpenGL). +`MWG_RENDERING_BASELINE_DATASET`. `MeasuresTriangulationDisabledCosts` builds a +mesh-free scene (configuration A for GDI, which still fills the original rings, +and configuration C for OpenGL, which rejects it). `MeasuresCoreSceneAndBackendCosts` uses the default fill-mesh preparation -(configurations B and D). Both report first and steady frame times, plus pan -and zoom frames, for a 1280x720 off-screen target. +(configurations B and D). Successful backends report first and steady frame +times, plus pan and zoom frames, for a 1280x720 off-screen target. Performance diagnostics are disabled by default and aggregate process-wide timings with `std::chrono::steady_clock`. The collector records dataset diff --git a/src/MapWinGIS.Core/src/map/MapView.cpp b/src/MapWinGIS.Core/src/map/MapView.cpp index a167d97..041f0ee 100644 --- a/src/MapWinGIS.Core/src/map/MapView.cpp +++ b/src/MapWinGIS.Core/src/map/MapView.cpp @@ -332,11 +332,18 @@ namespace mwg _impl->_scene = std::move(scene); _impl->_sceneStale = false; } - else + else if (diagnostics::PerformanceDiagnostics::IsEnabled()) { + std::int64_t hits = static_cast(_impl->_scene.layers.size()); + for (const rendering::SkippedLayer& layer : _impl->_scene.skippedLayers) + { + if (layer.reason != rendering::LayerSkipReason::Hidden) + { + ++hits; + } + } diagnostics::PerformanceDiagnostics::IncrementCounter( - diagnostics::PerformanceCounter::SceneCacheHits, - static_cast(_impl->_scene.layers.size())); + diagnostics::PerformanceCounter::SceneCacheHits, hits); } return _impl->_scene; } From a400e7d91f81548f7c8c91871c76e45749c7b345 Mon Sep 17 00:00:00 2001 From: Paul Meems Date: Thu, 19 Nov 2026 23:03:14 +0100 Subject: [PATCH 5/6] Fix CMake CI vcpkg toolset ABI mismatch Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- .ai/memory/build/windows.md | 5 +++- .github/workflows/cmake-vs.yml | 51 +++++++++++++++++++++++++--------- docs/BUILDING.md | 4 +++ 3 files changed, 46 insertions(+), 14 deletions(-) diff --git a/.ai/memory/build/windows.md b/.ai/memory/build/windows.md index e3ea206..041cdbf 100644 --- a/.ai/memory/build/windows.md +++ b/.ai/memory/build/windows.md @@ -25,7 +25,10 @@ flags and warnings against the MSBuild baseline. CTest excludes tests labelled `interactive`; the C# host checks require a desktop and are not part of CI. The runner overrides the default SDK with `10.0` because `windows-2025` does not include the repository's pinned SDK. Each build job has a 180-minute -timeout. +timeout. The CMake Visual Studio jobs pin the exact `VCToolsVersion` selected +by the MSBuild project and install vcpkg dependencies with the standalone CLI +before configuring CMake; otherwise CMake's automatic manifest install can +select a different installed v145 patch version and miss the prewarmed cache. ## References diff --git a/.github/workflows/cmake-vs.yml b/.github/workflows/cmake-vs.yml index c22be17..9dd959f 100644 --- a/.github/workflows/cmake-vs.yml +++ b/.github/workflows/cmake-vs.yml @@ -52,20 +52,45 @@ jobs: $architecture = if ($platform -eq 'x64') { 'x64' } else { 'x86' } $configurePreset = "vs2026-$architecture" $buildPreset = "$configurePreset-$($configuration.ToLowerInvariant())" - $arguments = @("-DMWG_WINDOWS_SDK=$env:MWG_WINDOWS_SDK") - if ($configuration -eq 'Release') { - # Release-only triplets skip the debug build of every dependency. - $arguments += @( - '-DVCPKG_MANIFEST_FEATURES=tests;full-drivers', - "-DVCPKG_TARGET_TRIPLET=$architecture-windows-v145-release", - '-DVCPKG_HOST_TRIPLET=x64-windows-v145-release', - "-DVCPKG_INSTALLED_DIR=$env:GITHUB_WORKSPACE/vcpkg_installed/cmake-$platform-release" - ) + $toolsetVersion = (& msbuild ` + src/MapWinGIS.Core/MapWinGIS.Core.vcxproj ` + -getProperty:VCToolsVersion ` + -p:Configuration=$configuration ` + -p:Platform=$platform ` + -p:MwgWindowsSdk=$env:MWG_WINDOWS_SDK ` + -nologo | Out-String).Trim() + if ($LASTEXITCODE -ne 0 -or $toolsetVersion -notmatch '^\d+\.\d+\.\d+$') { + throw "Could not determine the MSVC v145 toolset version (MSBuild exit code: $LASTEXITCODE, value: '$toolsetVersion')." } - else { - $arguments += '-DVCPKG_MANIFEST_FEATURES=tests' - } - cmake --preset $configurePreset @arguments + $release = $configuration -eq 'Release' + $suffix = if ($release) { '-release' } else { '' } + $triplet = "$architecture-windows-v145$suffix" + $hostTriplet = "x64-windows-v145$suffix" + $installedDir = "$env:GITHUB_WORKSPACE/vcpkg_installed/cmake-$platform$suffix" + $features = if ($release) { @('tests', 'full-drivers') } else { @('tests') } + $vcpkg = Join-Path $env:MWG_VCPKG_ROOT 'vcpkg.exe' + + & $vcpkg install ` + "--x-manifest-root=$env:GITHUB_WORKSPACE" ` + "--x-install-root=$installedDir" ` + "--overlay-triplets=$env:GITHUB_WORKSPACE\build\triplets" ` + --triplet $triplet ` + --host-triplet $hostTriplet ` + @($features | ForEach-Object { "--x-feature=$_" }) ` + --clean-after-build + if ($LASTEXITCODE -ne 0) { exit $LASTEXITCODE } + + $arguments = @( + "-DMWG_WINDOWS_SDK=$env:MWG_WINDOWS_SDK", + "-DVCPKG_MANIFEST_FEATURES=$($features -join ';')", + "-DVCPKG_TARGET_TRIPLET=$triplet", + "-DVCPKG_HOST_TRIPLET=$hostTriplet", + "-DVCPKG_INSTALLED_DIR=$installedDir", + '-DVCPKG_MANIFEST_INSTALL=OFF' + ) + cmake --preset $configurePreset ` + -T "v145,version=$toolsetVersion" ` + @arguments if ($LASTEXITCODE -ne 0) { exit $LASTEXITCODE } Add-Content -Path $env:GITHUB_ENV -Value "MWG_CMAKE_BUILD_PRESET=$buildPreset" diff --git a/docs/BUILDING.md b/docs/BUILDING.md index 7dbe5e9..6ae093f 100644 --- a/docs/BUILDING.md +++ b/docs/BUILDING.md @@ -269,6 +269,10 @@ the manifest baseline, the prefix also identifies the vcpkg version. The job saves a new cache, `-`, when vcpkg built a package or the cache was restored from an older prefix. Pull requests save a cache only when vcpkg added packages; pushes also refresh a stale-prefix cache. +The CMake Visual Studio jobs query the runner's `VCToolsVersion` from the +MSBuild project and pass that exact v145 patch version to CMake. They install +dependencies through the vcpkg CLI before configuring, matching the dependency +prewarm job's compiler and ABI, then disable the automatic manifest install. `build\scripts\Sync-VcpkgBinaryCache.ps1` first removes archives that no installed package uses after a successful install. On a failed install it keeps all archives and can save the packages that were built. A separate From cfe0c8f6222b74ade66bd1b548b7dc49f28b9413 Mon Sep 17 00:00:00 2001 From: Paul Meems Date: Thu, 19 Nov 2026 23:08:47 +0100 Subject: [PATCH 6/6] Ensure CMake CI can query MSBuild toolset Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- .github/workflows/cmake-vs.yml | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.github/workflows/cmake-vs.yml b/.github/workflows/cmake-vs.yml index 9dd959f..4f80e52 100644 --- a/.github/workflows/cmake-vs.yml +++ b/.github/workflows/cmake-vs.yml @@ -44,6 +44,9 @@ jobs: shell: pwsh run: ./build/scripts/bootstrap-vcpkg.ps1 -Baseline + - name: Set up MSBuild + uses: microsoft/setup-msbuild@v3 + - name: Configure CMake shell: pwsh run: |