Skip to content

MapWinGIS6 – Vector Rendering Performance Improvement Plan – Phase 6 – Optimize OpenGL rendering and data transfer - #93

Open
pmeems wants to merge 2 commits into
developmentfrom
phase6-optimize-opengl-rendering-v2
Open

pmeems wants to merge 2 commits into
developmentfrom
phase6-optimize-opengl-rendering-v2

Conversation

@pmeems

@pmeems pmeems commented Nov 21, 2026

Copy link
Copy Markdown
Contributor

Motivation

Reduce repeated OpenGL geometry submission and the CPU cost of copying framebuffer rows to the ActiveX/GDI presentation surface.

Key design decisions

  • Cache immutable fill and outline geometry in context-local vertex and index buffers per layer scene, with client-array fallback.
  • Apply pan and zoom through the OpenGL matrix instead of rebuilding transformed vertices, and retain the required framebuffer readback while removing the extra CPU row reversal.
  • Keep temporary renderer GPU buffers context-local; their lazy CPU triangulation meshes remain shared.

Validation

  • Debug and Release x64 CMake builds succeed with no new warnings; the Debug warning count matches development at 30 existing D9025 overrides.
  • All 474 non-interactive CTest cases pass.
  • On the generated 2,459-feature Release benchmark, steady frame/pan/zoom measured 2.5/2.3/2.3 ms versus 2.7/2.6/2.6 ms before, and eight readbacks measured 13.5 ms versus 18.3 ms.

No dependency changes. Updated the ActiveX architecture memory with the verified buffer lifetime and fallback rules.

- Cache immutable fill and outline geometry in context-local OpenGL buffers.
- Reuse matrix transforms across viewport changes and remove the CPU row reversal.
- Add cache lifecycle tests, benchmark results, and updated rendering documentation.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

@Julian-MW Julian-MW left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Automated review. Final decision is with the human reviewer.

One Major finding: the stale-entry erase in DrawLayerWithGpuBuffers keeps the iterator returned by std::map::erase, so invalidating any layer that is not the last in LayerId order skips the re-upload and draws another cached layer's buffers for one frame. The rest of the design — origin-localized VBO/IBO caching, matrix-based pan/zoom, bottom-up DIB presentation without the CPU row reversal, context-local buffers for temporary renderers — checks out against the client-array fallback math and the documented cache-lifetime rules. CI is green across all builds.

Comment on lines +808 to +810
QueueDeletedGpuBuffer(cached->second.vertexBuffer);
QueueDeletedGpuBuffer(cached->second.indexBuffer);
cached = _gpuBufferCache->layers.erase(cached);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Severity: Major | Confidence: High

Issue: std::map::erase(const_iterator) returns an iterator to the element following the erased one, not necessarily end(). When a stale cache entry is erased here and any cached layer has a higher LayerId, cached points at that other layer's entry. The rebuild check on line 814 (if (cached == _gpuBufferCache->layers.end())) then evaluates false, the re-upload is skipped, and the code at line 987 draws the wrong layer's vertex/index buffers (with the wrong layer's origin transform) for this layerId.

Impact: With two or more cached layers, editing or invalidating any layer that is not the last in LayerId order renders one frame where that layer displays another layer's geometry (and the correct geometry is missing entirely). The same happens on the outline→fill upgrade path (needFill && !hasFill). The frame self-heals on the next draw, which is also why the new single-layer test cannot catch it — after erase on a one-element map the result is always end().

Fix: Discard the iterator returned by erase so the rebuild branch is always taken:

Suggested change
QueueDeletedGpuBuffer(cached->second.vertexBuffer);
QueueDeletedGpuBuffer(cached->second.indexBuffer);
cached = _gpuBufferCache->layers.erase(cached);
QueueDeletedGpuBuffer(cached->second.vertexBuffer);
QueueDeletedGpuBuffer(cached->second.indexBuffer);
(void)_gpuBufferCache->layers.erase(cached);
cached = _gpuBufferCache->layers.end();
FlushDeletedGpuBuffers();

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in ad04dd1: stale-entry erasure now resets the iterator to end so the affected layer is rebuilt instead of drawing another layer cache entry.

}

FlushDeletedGpuBuffers();
if (!PruneGpuBufferCache(scene))

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Severity: Minor | Confidence: High

Issue: PruneGpuBufferCache returns true on every path (both the _gpuBufferCache == nullptr early exit and the fall-through), so this if (!PruneGpuBufferCache(scene)) check can never fire. Unlike PruneFillMeshCache, which can genuinely fail on bad_alloc, the GPU prune performs no throwing allocations itself — its only allocation risk (the deletedBuffers push in QueueDeletedGpuBuffer) is captured internally via _contextInvalid, which is checked two lines below.

Impact: Dead error handling; readers must verify the callee to learn the branch is unreachable, and the [[nodiscard]] bool signature invites future callers to rely on a result that carries no information.

Fix: Either change PruneGpuBufferCache to return void and drop this check, or actually propagate the failure — e.g. have it return false when a queue operation set _contextInvalid, making this branch live and the _contextInvalid re-check below redundant.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in ad04dd1: GPU cache pruning now propagates invalidation-queue allocation failure through its boolean result and returns E_FAIL from the renderer.

return S_FALSE;
}

const diagnostics::PerformanceScope submissionScope(

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Severity: Minor | Confidence: Medium

Issue: When the GPU-buffer upload fails and this function returns S_FALSE, this OpenGlGeometrySubmission scope has already recorded a measurement for the layer — and the client-array fallback in RenderScene then runs DrawLayer, which opens a second OpenGlGeometrySubmission scope for the same layer in the same frame.

Impact: After any upload failure (driver rejects the buffer size, transient GL error), the geometry-submission diagnostics double-count that layer, inflating the operation's totals exactly in the degraded scenarios where the numbers are most interesting. The docs' caveat about the scope covering "upload plus draws" doesn't cover this double-count case.

Fix: Only construct the scope after the upload is known to have succeeded (e.g. move it past the cache-build branch, or scope just the upload inside the build branch), or suppress the fallback scope when a GPU attempt was already recorded for the layer this frame.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in ad04dd1: geometry-submission timing begins only after a successful GPU upload or cache hit, so fallback rendering no longer records the layer twice.

ExpectTwoLayerPixels(surface.Get(), screenViewport);
}

TEST(VectorLayerRenderer, OpenGlReusesGpuBuffersAcrossViewportAndModeChanges)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Severity: Suggestion | Confidence: Medium

Issue: The new test exercises buffer reuse with a single layer, so it structurally cannot catch the stale-erase defect commented on OpenGlVectorLayerRenderer.cpp (cached = layers.erase(cached) skipping the rebuild when a higher LayerId is cached). Three behaviors introduced by this PR have no coverage:

  1. Multi-layer scene replacement: two cached layers, then Edit the layer with the lower LayerId and redraw — assert the frame still matches the client-array reference pixels and that GpuBufferUploadCount increased by exactly one. This fails today.
  2. Outline→fill upgrade: draw in Outline mode first (cached entry with hasFill == false), then in Fill mode — assert a re-upload happened and fills are actually drawn.
  3. Explicit invalidation: ClearLayerFillMeshCache(layerId) / ClearFillMeshCache() should drop the corresponding GpuBufferCacheSize entries.

Impact: The core lifetime guarantees documented in docs/ARCHITECTURE.md and .ai/memory/architecture/activex.md (release on scene replacement, coordinator invalidation, mode upgrade) are only verified by reading the code.

Fix: Extend this test to a two-layer scene and add the upgrade/invalidation cases; the pixel-comparison helpers used by ExpectTwoLayerPixels would catch wrong-layer rendering directly.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in ad04dd1: renderer tests now cover multi-layer scene replacement, outline-to-fill upgrades, pixel correctness, and per-layer/all-cache invalidation.

(void)_gpuBufferCache->layers.erase(cached);
}

bool OpenGlVectorLayerRenderer::HasGpuBufferCache() const noexcept

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Severity: Suggestion | Confidence: Low

Issue: HasGpuBufferCache() returns _bufferFunctions != nullptr, i.e. it reports capability ("buffer-object entry points were loaded"), not cache presence — it returns true even when GpuBufferCacheSize() is 0.

Impact: Naming only; the one caller (the test-access wrapper) uses it as a capability probe, which works, but the name reads as "the cache exists/has entries" and could mislead future callers into skipping fallback logic.

Fix: Rename to something like SupportsGpuBuffers() / AreGpuBuffersAvailable(), or return _gpuBufferCache != nullptr if cache existence was the intent.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in ad04dd1: HasGpuBufferCache now reports actual GPU cache allocation rather than buffer-object capability.

- Rebuild invalidated GPU buffers instead of drawing the next cached layer
- Propagate geometry-buffer invalidation queue failures
- Avoid double-counting failed GPU upload diagnostics
- Cover multi-layer invalidation and outline-to-fill upgrades

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants