| Filename | Latest commit message | Latest commit date |
|---|---|---|
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>
|
||
| .. | ||
| bindings | ||
| cli | ||
| cmake | ||
| editor | ||
| gl1 | ||
| kiplatform | ||
| ngspice-service | ||
| occ-service | ||
| shims | ||
| stubs | ||
| tools | ||
| README.md | ||
WASM Compatibility Layer
This directory contains WASM-specific implementations that allow KiCad to run in a web browser while keeping our KiCad fork as close to upstream as possible.
Principle
Instead of patching KiCad source files, we:
- Provide alternative implementations for platform-specific code (kiplatform)
- Stub out libraries/features that can't work in the browser (libgit2, curl, nng, scripting, 3D viewer, ...)
- Expose KiCad to JavaScript via Embind bindings
- Override host package detection during cross-compilation (cmake find-modules)
The KiCad-side hooks for this are small if(EMSCRIPTEN) branches in KiCad's own
CMakeLists that pull sources from this directory — see "How it's wired" below.
Directory Structure
wasm/
├── README.md # This file
├── kiplatform/ # Platform abstraction implementations (compiled into KiCad)
│ ├── app.cpp # App lifecycle (paths, startup)
│ ├── drivers.cpp # GPU detection (returns "WebGL")
│ ├── environment.cpp # Environment variables
│ ├── io.cpp # File I/O (WASM virtual filesystem)
│ ├── policy.cpp # Security policy (always permissive)
│ ├── secrets.cpp # Credential storage
│ ├── sysinfo.cpp # System information
│ ├── printing.cpp # Print support (browser print())
│ └── ui.cpp # UI helpers
├── bindings/ # Embind bindings exposing each app to JavaScript
│ ├── pcbnew_embind.cpp
│ ├── eeschema_embind.cpp
│ ├── pl_editor_embind.cpp
│ └── calculator_embind.cpp
├── stubs/ # Stub implementations + header shims for unavailable deps
│ ├── *.c / *.cpp # libgit2, curl, nng, scripting, 3D viewer, frame stubs, ...
│ ├── char_traits_uint16_workaround.h
│ └── GL/ nng/ ngspice/ # Header stubs found via include paths
└── cmake/ # CMake find-module overrides for cross-compilation
└── Find*.cmake / Use*.cmake
How it's wired
kiplatform — compiled into KiCad
The kiplatform/*.cpp files are added directly to KiCad's kiplatform library by an
if(EMSCRIPTEN) branch in kicad/libs/kiplatform/CMakeLists.txt, which references
them as ${PROJECT_SOURCE_DIR}/../wasm/kiplatform/*.cpp. There is no separate
libkiplatform_wasm.a.
stubs — compiled by the build script and KiCad CMakeLists
scripts/kicad/build-kicad-target.sh compiles the C stubs (libgit2_stub.c,
curl_stub.c, nng_stub.c) and force-includes char_traits_uint16_workaround.h.
App-specific *_frame_stub.cpp / *_scripting_stub.cpp are picked up per app, and
the remaining *_stub.cpp files are pulled in by if(EMSCRIPTEN) branches in the
KiCad fork's own CMakeLists. Header stubs under GL/, nng/, ngspice/ are resolved
via include paths.
bindings — per app
build-kicad-target.sh compiles wasm/bindings/<app>_embind.cpp for the app being
built (apps without an embind file get an empty placeholder object).
cmake — module path
build-kicad-target.sh passes -DCMAKE_MODULE_PATH="${PROJECT_ROOT}/wasm/cmake" so
the WASM find-module stubs override host package detection.
Coroutine/fiber support is not in this directory — it comes from the KiCad fork's
kicad/thirdparty/libcontext/libcontext.cpp(LIBCONTEXT_PLATFORM_wasm32). The GLU tesselator comes fromkicad/libs/kimath/glu_tess/glu_tess_impl.cpp.
Adding New Implementations
- Create the implementation file in the appropriate directory (
kiplatform/,stubs/,bindings/). - Wire it in: a stub C file goes in
build-kicad-target.sh; a kiplatform/app source goes in the relevantif(EMSCRIPTEN)branch of the KiCad-side CMakeLists. - Ensure the header interface matches KiCad's expected interface.
- Test with a minimal build before full integration.