Repository navigation
Conversation
- 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
left a comment
There was a problem hiding this comment.
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.
| QueueDeletedGpuBuffer(cached->second.vertexBuffer); | ||
| QueueDeletedGpuBuffer(cached->second.indexBuffer); | ||
| cached = _gpuBufferCache->layers.erase(cached); |
There was a problem hiding this comment.
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:
| 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(); |
There was a problem hiding this comment.
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)) |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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( |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
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:
- Multi-layer scene replacement: two cached layers, then
Editthe layer with the lowerLayerIdand redraw — assert the frame still matches the client-array reference pixels and thatGpuBufferUploadCountincreased by exactly one. This fails today. - Outline→fill upgrade: draw in
Outlinemode first (cached entry withhasFill == false), then inFillmode — assert a re-upload happened and fills are actually drawn. - Explicit invalidation:
ClearLayerFillMeshCache(layerId)/ClearFillMeshCache()should drop the correspondingGpuBufferCacheSizeentries.
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.
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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>
Motivation
Reduce repeated OpenGL geometry submission and the CPU cost of copying framebuffer rows to the ActiveX/GDI presentation surface.
Key design decisions
Validation
developmentat 30 existingD9025overrides.No dependency changes. Updated the ActiveX architecture memory with the verified buffer lifetime and fallback rules.