fix(3d): blank render + lost position after 3D viewer close/reopen (gl1 context guard)

Closing the viewer destroys its wxGLCanvas's WebGL context; reopening mints a
new one. The gl1 shim cached GL names (FFP program, stream/scratch VBOs) in
never-reset statics behind `if (!handle)` guards — in the new context every
draw died with INVALID_OPERATION and the viewer showed only the clear color
("Reload time 0.031 s" is benign: warm model caches make the rebuild fast).

contextSync() (gl1_state.cpp) now detects the context change in programSync()
— the one choke point every shim draw crosses, and a path the 2D GAL never
reaches (a first attempt checking in the glBindTexture wrap saw the GAL's
context and thrash-rebuilt the program 23x per run) — and drops the cached
names for lazy rebuild in the new context. Context identity is a monotonic id
stamped on Emscripten's per-context record: the numeric
EMSCRIPTEN_WEBGL_CONTEXT_HANDLE is recycled, so a destroy-then-create can
return the same number and a handle comparison detects nothing.

The lost-position half is a wxwidgets wasm fix (pointer bump: GetFromWindow
reports display 0; saved geometry used to carry display=(unsigned)-1, which
LoadWindowState treats as "display not found" and re-centres the frame).

TDD (red observed before each fix, green after):
- tests/kicad/3d-viewer-reopen.spec.ts (new, own worker like the deadlock
  spec): load board, open viewer, render-gate, drag by the titlebar, close
  via the x, reopen; asserts the board re-renders (was: 1 distinct colour for
  90 s) and the window position is restored (was: re-centred to 0,0 after
  closing at 40,90). Green run logs exactly one [gl1] context-change line.
- 3d-regression harness: recreateContext() destroys the context AND swaps in
  a fresh canvas element (a browser canvas keeps its context for life, so
  same-element recreation hands back the live old context and hides the bug);
  the new 3d-webgl spec test renders redraw-mini-board-navigator before and
  after recreation and requires pixel-identical output. Parity: 47/47, zero
  drift.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
Istvan Matejcsok 2026-08-24 15:55:31 +02:00
commit 3722891d48
13 changed files with 504 additions and 20 deletions

View file

@ -217,6 +217,23 @@ void stateBlendFunc( GLenum sfactor, GLenum dfactor );
void stateLineWidth( GLfloat width );
void stateAlphaFunc( GLenum func, GLclampf ref );
// --- context-generation guard (gl1_state.cpp) ---
// GL object names live and die with the WebGL context, and the 3D viewer's
// close/reopen destroys and recreates it (~wxGLCanvas destroys the context
// with the canvas; the next open mints new ones). Called from programSync()
// ONLY: that is the single choke point every shim draw crosses, it always
// runs under the 3D context, and — critically — it is a path the 2D GAL never
// reaches, so alternating 2D/3D paints cannot ping-pong the owner (a check in
// the glBindTexture wrap did exactly that: every caller crosses a wrap).
// On a context change it drops every cached name so the shim rebuilds lazily
// in the new context. Deliberately never touches display-list or
// immediate-mode state: the change can be detected mid-scene-rebuild, and
// those modules are context-agnostic CPU state.
void contextSync();
// Per-TU cache drops invoked by contextSync() on a context change.
void shadersDropContextObjects(); // FFP program + uniform locations + fail latch
void drawDropContextObjects(); // stream/scratch VBOs
// GL1 normalized-attribute rule: integer colors and normals are normalized,
// floats are not (positions/texcoords are float-only in this codebase).
bool attribNormalized( int arrayIndex, GLenum type );