Repository navigation
feat(viewer): WebGL2 trace rendering (P2) - #108
Merged
Merged
Conversation
Offload the per-frame hot path (one stroked polyline per channel, rebuilt on every pan/zoom/cursor-move) from Canvas 2D to the GPU. A WebGL2 canvas draws the background + signal as 1px LINE_STRIPs (identical pixel mapping to render.ts) and a transparent 2D canvas on top carries the chrome (labels, dividers, events, axis, scale bar). Graceful fallback to the all-2D renderFrame when WebGL2 is unavailable. - gl-trace.ts: createGlTraceRenderer + pure buildTraceVertices (slot/clip/band math mirrors drawTrace), hexToRgb; per-channel color/alpha uniforms, blending - render.ts: renderChrome (chrome-only, transparent overlay); DRY butterfly legend - viewer.ts: scope wrapper holds glCanvas (behind) + 2D canvas (front); sizeCanvas sizes both + resizes GL; renderImpl picks GL+chrome or 2D fallback; dispose on cleanup - BidsTree.astro: .eegv__scope frame + stacked .eegv__glcanvas/.eegv__canvas CSS - gl-trace.test.ts: 8 no-mock geometry tests (line/band/clip/butterfly/dim/empty) Verified live on nm000132 (33ch): stacked, topo split-panel resize, dark theme.
Addresses 3-agent review of #108: - createGlTraceRenderer: re-callable init() so a lost context is rebuilt on webglcontextrestored; every init failure path frees its GL objects (no shader/program/buffer/VAO leaks); guard createBuffer/createVertexArray null + aPos==-1; draw/resize/clear no-op while the context is lost - buildTraceVertices: clamp the band loop to the shortest of nCols/max/min so a boundary-truncated envelope never writes NaN vertices - resize(): only touch the viewport on an actual buffer-size change - clear(): wipe the GL canvas behind the loading/error overlay (no stale frame) - viewer: clear the GL layer before painting the loading/unavailable message
Contributor
Author
Review findings addressed (commit 175c1ef)Ran 3 review agents (pixel-fidelity, GL resource/correctness, integration/CSS). Pixel-fidelity came back clean (GL output is equivalent to the 2D fallback across stacked/butterfly/band/clip/colToX). Applied all real findings: GL resource safety (gl-trace.ts)
Correctness
0 type errors, biome clean, 568 tests, build OK. Re-verified live on nm000132 (33 ch) — rendering unchanged. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Moves the signal viewer's per-frame hot path from Canvas 2D to the GPU. Previously each visible channel was a stroked polyline (
moveTo/lineTo+stroke) rebuilt on every pan/zoom/cursor-move; with 64 channels over a ~2000 px window that is hundreds of thousands of CPU line segments per frame. Now a WebGL2 canvas rasterizes the background + traces; a transparent 2D canvas on top carries only the chrome.Design
.eegv__scope:.eegv__glcanvas(z0, background + traces) behind.eegv__canvas(z1, transparent: labels, slot dividers, event lines + codes, time axis, scale bar). Both fill the scope viainset:0, sized together.LINE_STRIPper channel (WebGLlineWidth>1is unsupported; 1 px matches the 2D renderer'slineWidth=1exactly, so GL and fallback look the same). One draw call per channel with per-channel color/alpha uniforms; band channels use the same max/min zigzag asdrawTrace.render.ts(slot baseline,pxPerPhys, clip envelope,colToX, butterfly center slot) so the GPU output is pixel-equivalent to the 2D path.createGlTraceRendererreturnsnullwhen WebGL2/shader compile is unavailable; the viewer then uses the existing all-2DrenderFrame(glCanvas hidden). No behavior change where WebGL is off.Files
gl-trace.ts(new):createGlTraceRenderer(WebGL2 program, VAO, blending, resize/draw/dispose) + purebuildTraceVertices/hexToRgb.render.ts:renderChrome(chrome-only transparent overlay); extracted shareddrawButterflyLegend(DRY).viewer.ts: scope wrapper + glCanvas inbuildDom;sizeCanvassizes both canvases and callsglRenderer.resize;renderImplpicks GL+chrome vs 2D; dispose on cleanup.BidsTree.astro:.eegv__scopeframe + stacked-canvas CSS.gl-trace.test.ts(new): 8 no-mock geometry tests (line edge x, baseline mapping, clip clamp, band zigzag, dim alpha, butterfly shared baseline, empty frame).Verification
0 type errors, biome clean, 568 tests (8 new), build OK. Verified live against nm000132 (33 ch EEG+EOG) in headless WebGL2:
--color-bgtoken.