Warn when a second GL context is registered without useMultipleContexts#133
Conversation
ContextManager.addContext registered contexts silently. When an app runs more than one GL context but does not set -Dardor3d.useMultipleContexts, the engine is in a quietly-broken state: ContextValueReference ignores its per-context key and keeps a single value, so non-sharable GL resources (VAOs, FBOs) created in one context are handed back for another. Shared resources (buffers, textures, programs) survive that because the contexts genuinely share them at the GL level, but VAOs are not shareable - so a VAO genned in context A and bound in context B is invalid there, corrupting rendering on the second context. The per-context keying (VAOs by unique ref, buffers by sharable ref) is correct by design, but it is a no-op unless useMultipleContexts is on, and nothing told the user. addContext is the one place that knows how many contexts are live, so warn once there when a second distinct context appears while the flag is off, pointing at the fix. This is a one-line diagnostic, not a behavior change - it turns a silent misconfiguration into an actionable log line. The latch is volatile so a set on one registration thread is visible to another; a narrow check-then-set race could still log twice, which is harmless for a one-time diagnostic (the class is otherwise unsynchronized by existing design). Red->green test (ardor3d-core's first ContextManager coverage): a single context and same-key recreation stay quiet; a second distinct context warns exactly once. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
📝 WalkthroughWalkthrough
ChangesMulti-context detection and one-time warning
Estimated code review effort🎯 2 (Simple) | ⏱️ ~10 minutes Poem
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@ardor3d-core/src/main/java/com/ardor3d/renderer/ContextManager.java`:
- Around line 34-39: Replace the volatile boolean field
`_warnedSingleContextMode` with an `AtomicBoolean` to enforce true one-time
emission of the warning. Change the check-then-set pattern that checks the
variable and sets it to true into an atomic compareAndSet operation (or similar
atomic method) that guarantees only one thread can successfully transition the
flag and log the warning, eliminating the race condition. Apply the same atomic
pattern fix to the related warning code referenced at lines 72-83.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro
Run ID: 569e0c96-0328-4ee8-b623-2c8d933cdbfb
📒 Files selected for processing (2)
ardor3d-core/src/main/java/com/ardor3d/renderer/ContextManager.javaardor3d-core/src/test/java/com/ardor3d/renderer/TestContextManager.java
What
ContextManager.addContextnow logs a one-time WARNING when more than one distinct GL context is registered whileConstants.useMultipleContextsisfalse.Why
The engine's per-context GL-resource keying is correct by design: VAOs are keyed by
RenderContext's unique ref, while buffers/textures/programs are keyed by the sharable ref (matching what GL actually shares between contexts). But all of that keying is gated behindConstants.useMultipleContexts, which is read once at class-load from-Dardor3d.useMultipleContextsand defaults to off.When it's off,
ContextValueReference.get/putignore the per-context key entirely and read/write a single value:So an app that runs multiple GL contexts but forgets the flag is in a quietly-broken state:
GL_INVALID_OPERATIONor missing/garbage geometry on the second context.Nothing surfaced this.
addContextis the one place that knows how many contexts are live, so it's the natural spot to turn a silent misconfiguration into an actionable log line that names the fix (-Dardor3d.useMultipleContexts).Notes
volatileand fires once.addContextis otherwise unsynchronized by existing design, so a narrow check-then-set race could log twice — harmless for a one-time diagnostic; full locking just this one boolean while the backingWeakHashMapstays unsynchronized would be inconsistent.Test
TestContextManager—ardor3d-core's first coverage for this class. Verifies a single context (and same-key recreation, e.g. on resize) stays quiet, and a second distinct context warns exactly once. Red→green; fullardor3d-coresuite green.🤖 Generated with Claude Code
Summary by CodeRabbit
New Features
Tests