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
24 changes: 23 additions & 1 deletion .github/plans/Vector-Rendering-Performance-Improvement.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,10 @@ Copilot: Update this checklist as each phase is implemented. Only mark a phase c
- Viewport navigation reuses uploaded coordinates through a matrix transform; direct bottom-up DIB presentation removes the CPU row reversal, while the required `glReadPixels` transfer remains.
- On the generated 2,459-feature Release grid, steady, pan, and zoom frames measured 2.5 / 2.3 / 2.3 ms versus 2.7 / 2.6 / 2.6 ms before; eight readbacks measured 13.5 ms versus 18.3 ms. First-frame buffer and context setup remains noise-sensitive and did not improve.
- Limitation: the FBO-to-CPU readback remains required by the GDI presentation path, and temporary renderers own separate GPU buffers because OpenGL buffer names are context-local.
- [ ] **Phase 7 – Testing and regression prevention** (PR 7)
- [x] **Phase 7 – Testing and regression prevention** (PR 7)
- Added an integrated OpenGL regression for lazy concave and holed geometry across fill, outline, pan, zoom, explicit invalidation, and layer removal.
- Added explicit point and line dataset coverage for the current polygon-only renderer, plus a repeatable benchmark of source-only scene creation followed by lazy OpenGL fill.
- Limitation: the original 2,459-feature dataset remains external; performance checks report timings and structural cache/triangulation counters without environment-sensitive CI thresholds.

**Target branch:** `development`

Expand Down Expand Up @@ -529,6 +532,25 @@ Targets:

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.

### Phase 7 implementation and limitations

- The regular test suites verify generated polygon, concave, holed, multipart,
invalid, degenerate, empty, point, and line scenarios. Renderer pixel checks
cover GDI and OpenGL paths where the OpenGL context is available.
- The lazy OpenGL lifecycle test confirms that fill meshes are generated once,
reused through render-mode and viewport changes, rebuilt after explicit
invalidation, and pruned after scene removal.
- The opt-in baseline now measures normal source-only scene preparation followed
by lazy OpenGL fill, including first, steady, pan, and zoom draws. It records
counters to prove that the later draws do not repeat triangulation.
- A Release x64 run with the generated 2,459-feature grid prepared the
source-only scene in 3.0 ms instead of 225.7 ms with explicit meshes. Lazy
steady, pan, and zoom frames were 3.3 / 3.2 / 3.1 ms versus 3.1 / 3.3 / 3.0
ms with explicit meshes, and all 2,459 polygons triangulated exactly once.
- Timing remains opt-in and threshold-free because the test environment does not
provide stable CI performance measurements. The original large shapefile is
supplied only through `MWG_RENDERING_BASELINE_DATASET`, not committed.

---

# Implementation strategy
Expand Down
20 changes: 19 additions & 1 deletion docs/TESTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,16 @@ and configuration C for OpenGL outline-only rendering).
times, plus pan and zoom frames, for a 1280x720 off-screen target. Normal scene
creation does not perform triangulation. OpenGL fill rendering lazily
triangulates source-only scenes on their first fill draw and reuses those
backend-owned meshes on later draws.
backend-owned meshes on later draws. `MeasuresLazyOpenGlFillCosts` measures
that production path separately and verifies that scene preparation creates no
meshes and later steady, pan, and zoom draws do not triangulate again. The
renderer regression suite also checks lazy triangulation of concave and holed
polygons across outline/fill changes, viewport changes, cache invalidation, and
scene removal. Point and line datasets are explicitly covered as unsupported
by the polygon-only renderer rather than being silently accepted. OpenGL pixel
and buffer-lifecycle tests that require a framebuffer-capable context are
conditional on that capability. GitHub-hosted Windows CI can skip those tests,
so run them locally on an FBO-capable OpenGL driver to verify that coverage.

Performance diagnostics are disabled by default and aggregate process-wide
timings with `std::chrono::steady_clock`. The collector records dataset
Expand Down Expand Up @@ -330,6 +339,15 @@ per-frame transformed client-array submission total. The frame still requires
`glReadPixels` because the control presents through GDI; this phase removes the
extra CPU scanline reversal, not the required GPU-to-CPU transfer.

Phase 7 validated the production lazy-fill path on one Release x64 run with the
same generated 2,459-feature grid. Source-only scene preparation took 3.0 ms
instead of 225.7 ms for explicit fill-mesh preparation. The lazy OpenGL first
fill took 367.5 ms, and steady, pan, and zoom frames took 3.3, 3.2, and 3.1 ms.
The explicit-mesh run measured 3.1, 3.3, and 3.0 ms for those repeated
operations, so the retained lazy meshes remained within normal run-to-run
variation while 2,459 polygons triangulated exactly once. This measurement is
diagnostic rather than a CI timing threshold.

Scene preparation reads geometries sequentially through OGR and copies
their coordinates directly, avoiding indexed lookups, WKT serialization, and
reparsing. The 100k-feature measurement dropped from 15,225 ms to 91.5 ms on
Expand Down
121 changes: 121 additions & 0 deletions tests/MapWinGIS.ActiveX.Tests/RenderingBaselineTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
#include "mapwingis/map/Map.h"
#include "mapwingis/map/Viewport.h"
#include "mapwingis/rendering/MapRenderScene.h"
#include "mapwingis/rendering/PolygonTriangulation.h"
#include "mapwingis/vector/VectorDataset.h"

using namespace mwg;
Expand Down Expand Up @@ -663,6 +664,126 @@ TEST_F(RenderingBaseline, MeasuresCoreSceneAndBackendCosts)
ReportPerformanceDiagnostics();
}

TEST_F(RenderingBaseline, MeasuresLazyOpenGlFillCosts)
{
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.";
}

rendering::VectorRenderScene probeLayer;
probeLayer.extent = {0.0, 0.0, 1.0, 1.0};
probeLayer.sourceFeatureCount = 1;
probeLayer.polygons = {
{{{0.0, 0.0}, {1.0, 0.0}, {1.0, 1.0}, {0.0, 1.0}, {0.0, 0.0}}}};
probeLayer.fillMeshes.push_back(
rendering::TriangulatePolygon(probeLayer.polygons.front()));
rendering::MapRenderScene probeScene;
probeScene.layers.push_back({
mwg::LayerId{1},
std::make_shared<const rendering::VectorRenderScene>(std::move(probeLayer))});
DibSurface probeSurface(64, 64);
ASSERT_TRUE(probeSurface.IsValid());
const RECT probeBounds{0, 0, 64, 64};
Viewport probeViewport(64, 64);
ASSERT_TRUE(probeViewport.FitExtent(probeScene.layers.front().scene->extent, 4.0));
activex::OpenGlVectorLayerRenderer availabilityProbe;
const HRESULT probeResult =
availabilityProbe.Draw(
probeSurface.Get(),
probeBounds,
probeScene,
probeViewport);
if (probeResult == S_FALSE)
{
GTEST_SKIP() << "OpenGL is unavailable; no baseline can be measured.";
}
ASSERT_EQ(probeResult, S_OK);

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);
const double sceneMs = ElapsedMilliseconds(start);
ASSERT_EQ(scene.layers.size(), 1U);
ASSERT_TRUE(scene.layers.front().scene->fillMeshes.empty());
const std::size_t polygonCount = scene.layers.front().scene->polygons.size();
ASSERT_GT(polygonCount, 0U);
const diagnostics::PerformanceSnapshot beforeDraw =
diagnostics::PerformanceDiagnostics::Snapshot();
EXPECT_EQ(
beforeDraw.Counter(
diagnostics::PerformanceCounter::PolygonsTriangulated),
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::OpenGlVectorLayerRenderer openGl;
start = Clock::now();
const HRESULT firstFrame =
openGl.Draw(surface.Get(), bounds, scene, viewport);
const double firstOpenGlMs = ElapsedMilliseconds(start);
ASSERT_EQ(firstFrame, S_OK);
const diagnostics::PerformanceSnapshot afterFirstDraw =
diagnostics::PerformanceDiagnostics::Snapshot();
EXPECT_EQ(
afterFirstDraw.Counter(
diagnostics::PerformanceCounter::PolygonsTriangulated),
static_cast<std::int64_t>(polygonCount));

std::vector<double> steadyFrames;
for (int frame = 0; frame < kSteadyFrames; ++frame)
{
start = Clock::now();
ASSERT_EQ(openGl.Draw(surface.Get(), bounds, scene, viewport), S_OK);
steadyFrames.push_back(ElapsedMilliseconds(start));
}

Viewport panned = viewport;
ASSERT_TRUE(panned.Pan(1.0, 1.0));
start = Clock::now();
ASSERT_EQ(openGl.Draw(surface.Get(), bounds, scene, panned), S_OK);
const double panMs = ElapsedMilliseconds(start);

Viewport zoomed = viewport;
ASSERT_TRUE(zoomed.Zoom(2.0));
start = Clock::now();
ASSERT_EQ(openGl.Draw(surface.Get(), bounds, scene, zoomed), S_OK);
const double zoomMs = ElapsedMilliseconds(start);

EXPECT_EQ(
diagnostics::PerformanceDiagnostics::Snapshot().Counter(
diagnostics::PerformanceCounter::PolygonsTriangulated),
static_cast<std::int64_t>(polygonCount));
EXPECT_TRUE(scene.layers.front().scene->fillMeshes.empty());

std::cout << "Lazy OpenGL fill baseline: "
<< scene.layers.front().scene->sourceFeatureCount
<< " source features (" << polygonCount << " lazily triangulated), "
<< kTargetWidth << 'x' << kTargetHeight << " target\n";
Report("Core open dataset", openMs, "ms");
Report("Core prepare source-only scene", sceneMs, "ms");
Report("OpenGL lazy-fill first frame", firstOpenGlMs, "ms");
Report("OpenGL lazy-fill frame median", Median(steadyFrames), "ms");
Report("OpenGL lazy-fill pan frame", panMs, "ms");
Report("OpenGL lazy-fill zoom frame", zoomMs, "ms");
ReportPerformanceDiagnostics();
}

TEST_F(RenderingBaseline, MeasuresComplexPolygonCosts)
{
const char* enabled = std::getenv("MWG_RUN_RENDERING_BASELINE");
Expand Down
106 changes: 98 additions & 8 deletions tests/MapWinGIS.ActiveX.Tests/VectorLayerRendererTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ namespace

constexpr double kPadding = 12.0;
constexpr COLORREF kFill = RGB(61, 122, 169);
constexpr COLORREF kOutline = RGB(24, 58, 82);

/// One polygon layer: a closed axis-aligned rectangle.
[[nodiscard]] mwg::rendering::VectorRenderScene MakeRectangleLayer(const mwg::Extent& rectangle)
Expand Down Expand Up @@ -713,7 +714,7 @@ TEST(VectorLayerRenderer, OpenGlLazyTriangulationFailureFallsBackAndPreservesOut
EXPECT_EQ(
renderer.GetActiveBackend(),
mwg::activex::VectorRenderBackendKind::OpenGL);
EXPECT_GT(CountPixels(surface.Get(), bounds, RGB(24, 58, 82)), 0U);
EXPECT_GT(CountPixels(surface.Get(), bounds, kOutline), 0U);

const mwg::diagnostics::PerformanceSnapshot snapshot =
mwg::diagnostics::PerformanceDiagnostics::Snapshot();
Expand Down Expand Up @@ -821,7 +822,7 @@ TEST(VectorLayerRenderer, OpenGlDrawsOutlinesWithoutFillMeshes)
for (int x = 0; x < bounds.right; ++x)
{
outlinePixels +=
GetPixel(surface.Get(), x, y) == RGB(24, 58, 82);
GetPixel(surface.Get(), x, y) == kOutline;
}
}
EXPECT_GT(outlinePixels, 0U);
Expand Down Expand Up @@ -856,7 +857,7 @@ TEST(VectorLayerRenderer, GdiDrawsOutlinesWithoutFillMeshes)
for (int x = 0; x < bounds.right; ++x)
{
outlinePixels +=
GetPixel(surface.Get(), x, y) == RGB(24, 58, 82);
GetPixel(surface.Get(), x, y) == kOutline;
}
}
EXPECT_GT(outlinePixels, 0U);
Expand Down Expand Up @@ -1129,6 +1130,98 @@ TEST(VectorLayerRenderer, OpenGlSupportsConcaveMultipartAndHoledScenes)
mwg::activex::VectorRenderMode::Outline));
}

TEST(VectorLayerRenderer, OpenGlLazyComplexMeshesSurviveModesNavigationAndRemoval)
{
using Access = mwg::activex::testing::OpenGlVectorLayerRendererTestAccess;
BitmapSurface surface(240, 180);
ASSERT_TRUE(surface.IsValid());
const RECT bounds{0, 0, 240, 180};

mwg::rendering::VectorRenderScene layer;
layer.extent = {0.0, 0.0, 22.0, 10.0};
layer.sourceFeatureCount = 2;
layer.polygons = {
{{{0.0, 0.0}, {10.0, 0.0}, {10.0, 4.0}, {4.0, 4.0},
{4.0, 10.0}, {0.0, 10.0}, {0.0, 0.0}}},
{{{12.0, 0.0}, {22.0, 0.0}, {22.0, 10.0}, {12.0, 10.0},
{12.0, 0.0}},
{{15.0, 3.0}, {15.0, 7.0}, {19.0, 7.0}, {19.0, 3.0},
{15.0, 3.0}}}};
mwg::rendering::MapRenderScene scene;
scene.layers.push_back({mwg::LayerId{1}, Share(std::move(layer))});
mwg::Viewport viewport = FitViewport(bounds, {0.0, 0.0, 22.0, 10.0});

mwg::activex::OpenGlVectorLayerRenderer availabilityProbe;
const HRESULT probeResult = availabilityProbe.Draw(
surface.Get(),
bounds,
MakeConvexPolygonScene(),
FitViewport(bounds));
if (probeResult == S_FALSE)
{
GTEST_SKIP() << "No OpenGL context with framebuffer objects is available.";
}
ASSERT_EQ(probeResult, S_OK);

PerformanceCapture diagnostics;
mwg::activex::OpenGlVectorLayerRenderer renderer;

const HRESULT first =
renderer.Draw(surface.Get(), bounds, scene, viewport);
ASSERT_EQ(first, S_OK);
EXPECT_EQ(MapPixel(surface.Get(), viewport, 2.0, 2.0), kFill);
EXPECT_EQ(MapPixel(surface.Get(), viewport, 7.0, 7.0), GetSysColor(COLOR_WINDOW));
EXPECT_EQ(MapPixel(surface.Get(), viewport, 13.0, 2.0), kFill);
EXPECT_EQ(
MapPixel(surface.Get(), viewport, 17.0, 5.0),
GetSysColor(COLOR_WINDOW));
EXPECT_EQ(Access::FillMeshCacheSize(renderer), 1U);
EXPECT_EQ(
mwg::diagnostics::PerformanceDiagnostics::Snapshot().Counter(
mwg::diagnostics::PerformanceCounter::PolygonsTriangulated),
2);
EXPECT_TRUE(scene.layers.front().scene->fillMeshes.empty());

ASSERT_NE(FillRect(surface.Get(), &bounds, GetSysColorBrush(COLOR_WINDOW)), 0);
ASSERT_TRUE(viewport.Pan(0.5, 0.25));
ASSERT_EQ(
renderer.Draw(
surface.Get(),
bounds,
scene,
viewport,
true,
mwg::activex::VectorRenderMode::Outline),
S_OK);
EXPECT_EQ(
MapPixel(surface.Get(), viewport, 2.0, 2.0),
GetSysColor(COLOR_WINDOW));
EXPECT_GT(CountPixels(surface.Get(), bounds, kOutline), 0U);

ASSERT_TRUE(viewport.Zoom(2.0));
ASSERT_EQ(renderer.Draw(surface.Get(), bounds, scene, viewport), S_OK);
EXPECT_EQ(MapPixel(surface.Get(), viewport, 8.0, 2.0), kFill);
EXPECT_EQ(
MapPixel(surface.Get(), viewport, 17.0, 5.0),
GetSysColor(COLOR_WINDOW));
EXPECT_EQ(
mwg::diagnostics::PerformanceDiagnostics::Snapshot().Counter(
mwg::diagnostics::PerformanceCounter::PolygonsTriangulated),
2);

Access::InvalidateLayerSceneCache(renderer, scene.layers.front().layerId);
ASSERT_EQ(renderer.Draw(surface.Get(), bounds, scene, viewport), S_OK);
EXPECT_EQ(
mwg::diagnostics::PerformanceDiagnostics::Snapshot().Counter(
mwg::diagnostics::PerformanceCounter::PolygonsTriangulated),
4);

scene.layers.clear();
ASSERT_EQ(renderer.Draw(surface.Get(), bounds, scene, viewport), S_OK);
EXPECT_EQ(Access::FillMeshCacheSize(renderer), 0U);
EXPECT_EQ(Access::GpuBufferCacheSize(renderer), 0U);
}

TEST(VectorLayerRenderer, OpenGlRendersLShapedPolygon)
{
BitmapSurface surface(120, 100);
Expand Down Expand Up @@ -1234,7 +1327,6 @@ TEST(VectorLayerRenderer, OpenGlDoesNotDrawInternalTriangleEdges)
// A convex quad triangulates into two triangles whose diagonal crosses
// the polygon interior. Only the original ring may be outlined, so no
// interior pixel may have the outline color.
constexpr COLORREF kOutline = RGB(24, 58, 82);
for (double t = 0.15; t < 0.9; t += 0.1)
{
for (const mwg::rendering::Point diagonal :
Expand Down Expand Up @@ -1665,7 +1757,7 @@ TEST(VectorLayerRenderer, OverlappingLayersDrawBottomFirst)
EXPECT_EQ(GetPixel(surface.Get(), 40, 50), kFill);
std::swap(scene.layers[0], scene.layers[1]);
ASSERT_EQ(gdi.Draw(surface.Get(), bounds, scene, viewport), S_OK);
EXPECT_EQ(GetPixel(surface.Get(), 40, 50), RGB(24, 58, 82));
EXPECT_EQ(GetPixel(surface.Get(), 40, 50), kOutline);

mwg::activex::OpenGlVectorLayerRenderer openGl;
const HRESULT result = openGl.Draw(surface.Get(), bounds, scene, viewport);
Expand All @@ -1674,7 +1766,7 @@ TEST(VectorLayerRenderer, OverlappingLayersDrawBottomFirst)
GTEST_SKIP() << "No OpenGL context with framebuffer objects is available.";
}
ASSERT_EQ(result, S_OK);
EXPECT_EQ(GetPixel(surface.Get(), 40, 50), RGB(24, 58, 82));
EXPECT_EQ(GetPixel(surface.Get(), 40, 50), kOutline);
std::swap(scene.layers[0], scene.layers[1]);
ASSERT_EQ(openGl.Draw(surface.Get(), bounds, scene, viewport), S_OK);
EXPECT_EQ(GetPixel(surface.Get(), 40, 50), kFill);
Expand Down Expand Up @@ -1716,7 +1808,6 @@ TEST(VectorLayerRenderer, GdiAndOpenGlAgreeOnOverlappingPolygonsInOneLayer)
// The first polygon's right edge (pixel column 60) and top edge (pixel
// row 49) stay visible only outside the second polygon; inside the
// overlap the second polygon's fill covers them in both backends.
constexpr COLORREF kOutline = RGB(24, 58, 82);
for (const mwg::PixelPoint pixel : {mwg::PixelPoint{60, 60}, {50, 49}})
{
EXPECT_EQ(GetPixel(gdiSurface.Get(), pixel.x, pixel.y), kFill)
Expand Down Expand Up @@ -1966,7 +2057,6 @@ TEST(VectorLayerRenderer, OpenGlPreservesPolygonPrecisionFarFromLayerOrigin)
{
GTEST_SKIP() << "OpenGL buffer objects are unavailable.";
}
constexpr COLORREF kOutline = RGB(24, 58, 82);
EXPECT_GT(CountPixels(surface.Get(), bounds, kOutline), 20U);

ASSERT_EQ(renderer.Draw(surface.Get(), bounds, scene, viewport), S_OK);
Expand Down
Loading