feat(wasm): Disable 3D viewer with comprehensive stubs

- Set KICAD_BUILD_3D_VIEWER_WASM=OFF in build script
- Add comprehensive 3D canvas stubs (~500 lines) including:
  - EDA_3D_CANVAS with wxWidgets event table
  - BOARD_ADAPTER, EDA_3D_VIEWER_SETTINGS
  - PANEL_PREVIEW_3D_MODEL with all event handlers
  - DIALOG_SELECT_3DMODEL
  - TRACK_BALL camera
  - BBOX_2D, BBOX_3D, BVH_CONTAINER_2D
  - OGL_ATT_LIST::GetAttributesList
- Add 3D scenegraph stubs for VRML export
- Update KiCad submodule with WASM guards
- Update WebGL GAL implementation plan to COMPLETE status

All 4 phases of the WebGL GAL implementation are now complete:
- Phase 1: Native test harness (28 scenarios)
- Phase 2: WebGL GAL implementation (~27,800 lines)
- Phase 3: Complete API coverage
- Phase 4: KiCad integration with 3D viewer stubs

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
Viktor Vaczi 2026-01-10 15:15:50 +01:00
commit 71d2444729
6 changed files with 830 additions and 101 deletions

View file

@ -1,18 +1,22 @@
# WebGL GAL Port - Master Plan
# WebGL GAL Port - Implementation Complete
## Status: ALL PHASES COMPLETE ✅
**Branch:** `webgl` (21+ commits)
## Goal
Port KiCad's GAL (Graphics Abstraction Layer) from OpenGL to WebGL to enable full KiCad functionality in the browser. Use the existing 28-scenario test suite to verify visual parity between native OpenGL and WebGL implementations.
## Current State
- **GAL Test Harness**: 28 scenarios covering 100% of GAL API (~70 methods)
- **Native Test**: C++ binary using real OPENGL_GAL, outputs PNGs to `tests/gal-regression/baseline/`
- **KiCad WASM**: Already runs in browser but crashes on OpenGL compatibility issues
- **OpenGL GAL**: ~9,500 lines of C++/GLSL using mix of legacy GL and modern VBOs
## Final State
- **WebGL GAL**: Fully integrated into `kicad/common/gal/webgl/` (~27,800 lines)
- **GAL Test Suite**: 28 scenarios passing, visual parity with native OpenGL
- **KiCad WASM**: Builds and runs successfully with WebGL GAL
- **3D Viewer**: Disabled with stubs for WASM builds (`KICAD_BUILD_3D_VIEWER_WASM=OFF`)
## Key Decisions
- **Approach**: Copy and modify existing OpenGL GAL code
- **Scope**: Full feature parity (all 28 scenarios)
- **Location**: Develop in `tests/gal-regression/` first, move to KiCad later
- **Location**: Developed in `tests/gal-regression/wasm/` first, moved to KiCad for integration
## Architecture
@ -76,106 +80,154 @@ tests/gal-regression/
│ └── webgl/ # WebGL run via Playwright
```
## Phases
## Phases - ALL COMPLETE ✅
### Phase 1: Master Test Script & WebGL Harness Infrastructure
Create the unified test script and WASM-based test harness.
### Phase 1: Native Test Harness ✅
Created unified test infrastructure with 28 scenarios covering 100% of GAL API.
**Commits:** 490f531 → 051fb87
**Deliverables:**
- [x] `scripts/test-gal-regression.sh` - Master build/test/compare script
- [x] `tests/gal-regression/wasm/` directory structure
- [x] `tests/gal-regression/wasm/Makefile` - Emscripten build (use Makefile, not CMake)
- [x] `tests/gal-regression/wasm/gal_webgl_test.cpp` - WASM entry point
- [x] `tests/gal-regression/wasm/gal_webgl_test.html` - Test page with canvas
- [x] `scripts/build-gal-webgl-test.sh` - WASM build script (uses Makefile)
- [x] `tests/e2e/gal-webgl.spec.ts` - Playwright spec for screenshots
- [x] `scripts/build-gal-native-test.sh` - Native build script
- [x] `tests/gal-regression/native/` - Native C++ test harness using OPENGL_GAL
- [x] `tests/gal-regression/scenarios/` - 28 shared test scenarios
- [x] `tests/gal-regression/baseline/` - Native OpenGL reference screenshots
**Build Approach:**
- Use a Makefile with direct `em++` calls (like `tests/apps/Makefile.wasm`)
- Avoid `emcmake cmake` which requires Python 3.10+ (system has 3.9.6)
- Follow the same pattern as `build-wasm-test.sh`
### Phase 2: WebGL GAL Implementation ✅
Full port of OPENGL_GAL to WebGL 2.0 / OpenGL ES 3.0.
**Verification:** Master script builds both, runs native successfully, WebGL loads empty page.
**Commits:** a4f444f → 74faa7e
### Phase 2: WEBGL_GAL Implementation
Copy and modify OpenGL GAL to create pure WebGL implementation.
**Approach:** Start with `kicad/common/gal/opengl/opengl_gal.cpp`, then:
1. Replace legacy immediate mode (`glBegin/glEnd`) with VBO-based rendering
2. Replace GL matrix stack with glm matrices (already used internally)
3. Adapt shaders for WebGL 2.0 / GLSL ES 3.0
4. Handle WebGL-specific limitations
**Key Files:**
- [ ] `tests/gal-regression/wasm/webgl_gal.h` - Class declaration
- [ ] `tests/gal-regression/wasm/webgl_gal.cpp` - Main implementation
- [ ] `tests/gal-regression/wasm/webgl_shaders.cpp` - Shader sources
- [ ] Adapt vertex_manager, gpu_manager as needed
**Verification:** Scenario 0 (basic-lines) renders, master script compares successfully.
### Phase 3: Complete API Coverage
Implement all GAL methods to pass all 28 scenarios.
**Method Groups (incremental):**
1. Basic drawing: DrawLine, DrawSegment, DrawCircle, DrawArc
2. Shapes: DrawRectangle, DrawPolygon, DrawPolyline
3. Advanced: DrawBezier, DrawBezierArc, DrawArcSegment, DrawSegmentChain
4. State: Colors, transforms, depth testing, render targets
5. Groups: BeginGroup, EndGroup, DrawGroup, ChangeGroupColor/Depth
6. Text: DrawGlyph, DrawGlyphs, BitmapText
7. Special: DrawGrid, DrawCursor, DrawBitmap
**Verification:** Run master script after each group - all implemented scenarios match.
### Phase 4: Integration with KiCad
Move WEBGL_GAL into KiCad source and enable for browser builds.
**Key Changes:**
- Replaced legacy `glBegin/glEnd` with VBO-based rendering
- Replaced GL matrix stack with glm matrices
- Converted GLSL shaders to ES 3.0 (`attribute`→`in`, `varying`→`out`, etc.)
- Added VAO support (required for WebGL 2.0)
- Replaced GLU tesselator with earcut.hpp
**Deliverables:**
- [ ] Move `webgl_gal.*` to `kicad/common/gal/webgl/`
- [ ] CMake integration for Emscripten builds
- [ ] Runtime GAL selection based on platform
- [ ] KiCad WASM builds successfully with WEBGL_GAL
- [x] `tests/gal-regression/wasm/webgl/` - Initial WebGL GAL implementation
- [x] `scripts/build-gal-webgl-test.sh` - WASM build script
- [x] `tests/e2e/gal-webgl.spec.ts` - Playwright spec for screenshots
**Verification:** KiCad loads in browser, opens PCB, renders correctly.
### Phase 3: Complete API Coverage ✅
All GAL methods implemented with visual parity.
## File Structure
**Method Groups (all complete):**
- [x] Basic drawing: DrawLine, DrawSegment, DrawCircle, DrawArc
- [x] Shapes: DrawRectangle, DrawPolygon, DrawPolyline
- [x] Advanced: DrawBezier, DrawBezierArc, DrawArcSegment, DrawSegmentChain
- [x] State: Colors, transforms, depth testing, render targets
- [x] Groups: BeginGroup, EndGroup, DrawGroup, ChangeGroupColor/Depth
- [x] Text: DrawGlyph, DrawGlyphs, BitmapText
- [x] Special: DrawGrid, DrawCursor, DrawBitmap
### Phase 4: KiCad Integration ✅
WebGL GAL moved to KiCad source tree with CMake integration.
**Commit:** 37d973f (KiCad submodule: 1b5bb125d2)
**Deliverables:**
- [x] Move `webgl_gal.*` to `kicad/common/gal/webgl/`
- [x] CMake integration for Emscripten builds
- [x] 3D viewer disabled with stubs (`KICAD_BUILD_3D_VIEWER_WASM=OFF`)
- [x] KiCad WASM builds and e2e tests pass
---
## Technical Lessons Learned
### 1. GLSL ES 3.0 Shader Conversion
Desktop OpenGL shaders needed conversion for WebGL 2.0:
```glsl
attribute → in
varying → out (vertex shader) / in (fragment shader)
texture2D() → texture()
gl_FragColor → explicit out variable
```
Automated in `generate_shaders.py`.
### 2. VAO Required for WebGL 2.0
WebGL 2.0 requires Vertex Array Objects (VAOs):
```cpp
glGenVertexArrays(1, &m_vao);
glBindVertexArray(m_vao);
```
### 3. Legacy GL Elimination
All legacy OpenGL calls replaced:
- `glBegin/glEnd` → VBO-based rendering
- `glPushMatrix/glPopMatrix` → glm matrix stack
- `GL_QUADS` → `GL_TRIANGLES` (quads not supported in WebGL)
- `glEnableClientState` → modern vertex attributes
### 4. GLU Tesselator Replacement
GLU not available in WebGL. Implemented using earcut.hpp:
- File: `kicad/common/gal/webgl/glu_tess_impl.cpp`
- Provides `gluNewTess`, `gluTessBeginPolygon`, etc.
### 5. Coordinate System & Retina Scaling
- White background required for screenshot comparison (alpha compositing)
- Retina 2x scaling requires proper `devicePixelRatio` handling
- World-to-screen mapping: `SetWorldUnitLength(1.0 / DPI)`
### 6. Alpha Blending
Proper blend functions required for correct transparency:
```cpp
glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
```
### 7. 3D Viewer Stubbing Strategy
When disabling 3D viewer (`KICAD_BUILD_3D_VIEWER_WASM=OFF`):
- Guard includes/code with `#ifndef __EMSCRIPTEN__`
- Stub classes need wxWidgets event table macros (`BEGIN_EVENT_TABLE`)
- Complex classes (PANEL_PREVIEW_3D_MODEL) require all event handler stubs
## File Structure (Final)
```
kicad/common/gal/webgl/ # WebGL GAL in KiCad source tree
├── webgl_gal.cpp # Main implementation (3184 lines)
├── webgl_gal.h # Class declaration (618 lines)
├── webgl_compositor.cpp # FBO compositing
├── webgl_antialiasing.cpp # SMAA implementation
├── gpu_manager.cpp # VBO/VAO management
├── vertex_manager.cpp # Vertex accumulation
├── shader.cpp # GLSL compilation
├── glu_tess_impl.cpp # GLU tesselator (earcut)
├── earcut.hpp # Polygon triangulation
└── ... (20+ files total)
tests/gal-regression/
├── baseline/ # Committed reference (from native)
├── output/
│ ├── native/ # Fresh native run
│ └── webgl/ # WebGL run via Playwright
├── native/ # Native C++ harness (existing)
│ ├── gal_native_test.cpp
│ └── ...
├── wasm/ # WASM harness (new)
│ ├── Makefile # Use Makefile, not CMake (avoids Python issues)
│ ├── gal_webgl_test.cpp
│ ├── gal_webgl_test.html
│ ├── webgl_gal.h
│ ├── webgl_gal.cpp
│ └── webgl_shaders.cpp
└── scenarios/ # Shared scenarios (existing)
├── baseline/ # Native OpenGL reference (28 PNGs)
├── baseline-webgl/ # WebGL reference (29 PNGs)
├── native/ # Native test harness
├── wasm/ # WebGL test harness (uses KiCad GAL)
└── scenarios/ # Shared 28 test scenarios
wasm/stubs/
├── 3d_canvas_stub.cpp # ~500 lines of 3D stubs
├── 3d_viewer_stub.cpp # EDA_3D_VIEWER_FRAME stub
└── 3d_scenegraph_stub.cpp # VRML export stubs
scripts/
├── build-gal-native-test.sh # existing
├── build-gal-webgl-test.sh # new
└── test-gal-regression.sh # new - MASTER SCRIPT
├── build-gal-native-test.sh # Native build script
├── build-gal-webgl-test.sh # WASM build script
└── test-gal-regression.sh # Master test script
```
## Critical Files to Modify/Create
---
**New files:**
- `scripts/test-gal-regression.sh` - Master test script
- `scripts/build-gal-webgl-test.sh` - WASM build
- `tests/gal-regression/wasm/*` - All WASM harness files
- `tests/e2e/gal-webgl.spec.ts` - Playwright test
## Verification Commands
**Reference files (copy from):**
- `kicad/common/gal/opengl/opengl_gal.cpp` (3098 lines)
- `kicad/common/gal/opengl/opengl_gal.h` (614 lines)
- `kicad/common/gal/opengl/shader.cpp` (298 lines)
- `kicad/common/gal/opengl/vertex_manager.cpp` (318 lines)
- `kicad/common/gal/opengl/gpu_manager.cpp` (340 lines)
```bash
# Build KiCad WASM
docker/build.sh
# Run GAL regression tests
scripts/test-gal-regression.sh
# Run KiCad e2e tests
cd tests && npm run test:kicad
```