Skip to content

cvcGL: SDL3 owns the browser canvas and its WebGL2 context - #997

Merged
transfix merged 16 commits into
masterfrom
feat/cvcgl-sdl-browser
Nov 3, 2026
Merged

transfix merged 16 commits into
masterfrom
feat/cvcgl-sdl-browser

Conversation

@transfix

@transfix transfix commented Nov 3, 2026 •

Copy link
Copy Markdown
Owner

#829 made SDL3 own cvcGL's onscreen window and its GL context on the desktop, so SDL's events drive VTK's interactor there. The browser was left on VTK's canvas window and vtkWebAssemblyRenderWindowInteractor, whose key names are the page's KeyboardEvent.key (#993 had to teach ImGui those). This makes the browser window SdlGLRenderWindow with SdlRenderWindowInteractor too: one input path and one set of key names everywhere.

The WebGL2 context

SDL 3.4's Emscripten_GLES_CreateContext makes the context from the SDL GL attributes. With SDL_GL_ALPHA_SIZE 0 (8 only for a translucent window) it is the context VTK's canvas window makes: WebGL2, no antialiasing (VTK resolves its own multisampled framebuffers into it), opaque, premultiplied alpha, depth, stencil, default power preference.

What changes under Emscripten (src/cvcGL/SdlWindow.cpp)

  • sdl_window_enabled() is true in the browser too. ENV.CVCGL_SDL_WINDOW = '0' in the page's preRun keeps VTK's canvas window.
  • Size. Before SDL makes the window, the canvas is styled to fill its container exactly as VTK's interactor styled it. SDL then finds the canvas sized by CSS and takes the window's size from it, from the first frame (what Ariadne in the browser: windows open inside the canvas and follow it as it resizes #977 does for VTK's window) and at every browser resize. An app's SetSize is ignored once the window exists.
  • Keys. SDL's keyboard element is the canvas: keys typed into it, not into the page's own fields or ImGuiOverlay's system-keyboard input. The window gets no title, which SDL would make the page's.
  • Frames. No SDL_GL_SwapWindow (in the browser it only yields). Frame() yields where VTK 9.5's canvas window does, so FrameYield means the same; arigl's App mode plus requestAnimationFrame wait is unchanged. Blit to the bound framebuffer (BlitToCurrent), color bits from WebGL, as VTK's window does.
  • Current context. MakeCurrent, IsCurrent, ReleaseCurrent and the context stack check WebGL's own current context, since SDL skips one it believes current and a VTK canvas window can change it behind SDL.
  • Pointer lock. CameraController's in-press lock, raw motion and UI-window hit test (arigl: clicks on windows keep the mouse, frames paced to the display, drag to steer #988) stay as they are, now on the SDL window's canvas. While the page holds the lock, the interactor's pointer position stands still, as VTK's did.
  • Touch. SDL reads the canvas's pointer events and makes the first finger the mouse (VTK's single touch as mouse). The canvas cancels its touch events, as VTK's touch handlers did, so a tap makes no mouse events or click of its own: without that the browser's mousedown focused the canvas right after ImGuiOverlay focused its system-keyboard field, which would put a phone's keyboard away. TouchGestures still reads two-finger gestures from the page's touch events; from a second finger until the last lifts, the interactor holds the first finger's mouse still.
  • Text. Text events drop control characters (a browser's keypress types Enter as "\r").
  • SdlInput uses the window's event sink, as natively; its hidden-canvas listener is only for VTK's canvas window now.

Docs: docs/CVCGL_WASM.md gains "The window: SDL's canvas"; docs/ARIADNE.md's input note and header comments are brought up to date.

Testing

Built the conference client for wasm locally against the cvcpkg wasm prefix (SDL 3.4.12, VTK 9.5, emsdk 5.0.7) and drove it in headless Chrome (ANGLE D3D11) over the DevTools protocol, with trusted input events, against a local relay.

  • Desktop, 1600x900. SDL's handlers own the canvas and the page title is untouched. The canvas goes from the browser's default 300x150 straight to its container's size (no 1024x768 frame), and follows window resizes (1584x730 → 1350x579 → 1584x730; the windows move in and back).
    • Real key events into the chat field: typing, Backspace and Enter send the message. Letters typed into the field don't walk; a held W walks.
    • A left drag on the floor steers (the relay's facing changes); a right drag turns the view only.
    • Mouse look: clicks on the UI's windows ask for no lock, a click on the floor does. Headless Chrome refuses a real pointer lock, so it was stubbed through document.pointerLockElement: 400 px of motion turned the avatar 50°, and moving across a window's close button under the lock didn't hover it (it does once released).
    • Pacing: 300 of 301 display refreshes drew exactly one frame; no FrameYield watchdog trips.
  • Phone, 390x844 at 3x (touch emulation, trusted touch events): one finger steers, two fingers turn the view and leave the facing, a pinch zooms. Taps on the panel toggle and a window's arrow work. A tap on the message field focuses the system-keyboard input and it keeps the focus (no mousedown or click after the tap); typing (Input.insertText), Backspace and Enter send.
  • Fallback: ENV.CVCGL_SDL_WINDOW = '0' runs VTK's canvas window as before (keys, walking).
  • Native (Windows, MSVC, shared, RelWithDebInfo, with FTXUI): every test this branch can reach passes. ctest: all 103 cvcGL, arigl, ari and cvcMedia entries, cvcgl_sdl_attach, cvcgl_sdl_input and cvcgl_viewport_input among them. The Ariadne and chat gtests (with CI's exclusions): 727 cases pass in ariadne_bind, doc_fetch, ftxui, host, loader, media, requires, runtime, scene_tf, stream_view, term_graphics and room_chat. 30 cases skip because the local dependency prefixes have no yaml-cpp (the three ari CLI binaries, two loader cases); CI has it. The core suites this branch doesn't touch (nav, physics, state, world) were left to CI. On the desktop SDL_CreateWindowWithProperties gets the same properties SDL_CreateWindow set.

In a browser VTK fits the canvas to its container only when it handles a
resize event, and the first one comes from a ResizeObserver after the
first frames have drawn at the window's starting size. cyprty starts at
arigl's 1024x768, so ImGui placed every window against that, and on a
shorter canvas (577-729 px tall in a laptop's browser) windows ran off
the bottom: the "opens inside the view" rule (ARIADNE.md 5.8) had moved
them in for a view that was not there. ViewportManager now takes the
container's size right after the interactor initializes, as VTK's own
resize handling would.

With the real size from frame 1, the overlay's small-screen rule would
start a 1366x768 laptop's browser with its panels hidden (short side
under 700). It is a phone's rule, so the line is now 500 logical px, the
phone view libcvc#974 uses. cvcgl_imgui_panels_start checks both sides.
Checked in headless Chrome at 1366x768: with the canvas sized before
the first frame, the Avatar window was placed in a 597-px stage, and a
frame later the stage was 578. The footer is empty until the program
first prints, then a line taller. It is one line tall from the start
now, in cyprty and the cvcGL demo page, so the canvas keeps the size
the windows were placed in.

When the overlay hides its panels on a phone, it writes that to its
state mirror, as the show/hide button does, so ui.panels_open says
what shows.

cvcgl_imgui_panels_start renders three frames per window: the
overlay's GL backend starts at the first render, so ImGui's first
frame is the second.
The first is the window resize event the wasm interactor's Initialize()
dispatches, handled at the next processUIEvents(), not a ResizeObserver
as the comment (and the first commit's message) said. Checked in Chrome:
with the container held at 0 px past that event, the canvas stayed 0 px
tall after the container got its height, until a window resize event.
A window opened inside the view (5.8), but if the view got smaller
after (a browser window made smaller, a phone turned on its side,
fullscreen left), nothing brought it back: on a shorter canvas the
windows below its new edge stayed there.

Each window now keeps the work area it was last drawn in. When it is
drawn in a smaller one, a window that showed in full there moves back
in as it opened, no larger than the work area. A window the user left
partly outside stays where it is, a view that grows again moves
nothing, and the flow places its windows itself, as before.

cvcgl_imgui_view_shrink checks it on a bare ImGui context: 1600x900,
then 1350x579 (a 1366x768 laptop's browser canvas), 1350x450 and
500x450, then back; and a window dragged past the bottom edge.
Against the old ImGuiBackend.cpp 5 of its 11 checks fail.
A window moved in to fit the view, where it opened or when the view got
smaller, now keeps its home: the place and size it had without the move.
Each frame it heads there as far as the view allows, so a view that
grows again returns it, and a page opened in a small browser window and
then made larger puts the windows where the document says. It is home
again when it gets there. If the user has moved or resized it since it
was last put somewhere, where they put it is its place from then on.

cvcgl_imgui_view_shrink: back at 1600x900 every window is where its
pos: says at its size; a page opened at 1350x579 goes back part way at
1450x700 and all the way at 1600x900; a window the user drags after it
was moved in stays there. 16 checks.
#974 (windows as a list of titles on a phone) and this branch both
changed where ImGuiBackend::begin_window opens a window. Both stay:
a window that does not fit a phone's view opens collapsed in the list,
and one moved in anywhere else keeps its home. A window in the list
gets no home: one would pull it back out of the list, on the phone and
again when the view grows. reset_layout clears the list count, the last
views and the homes.

cvcgl_imgui_view_shrink gains the case (it fails without the rule), and
section 5.8 says a window in a phone's list stays there.
… drag to steer

Three things from trying cyprty with mouse look on.

A click on a window could take the pointer. The page takes its pointer
lock inside the press, and it judged "not on the UI" by a flag the frame
loop set from ImGui's hover, a frame or two old: a move onto a window and
a click within that time locked the mouse (3 of 5 such clicks in headless
Chrome). The overlay now hands the camera the rectangles of the windows it
drew (CameraController::setUiRegions, replacing setPointerOverUi), the
page tests the press's own position against them, and the overlay keeps a
press there from the camera even before ImGui has seen the pointer arrive.
After: 0 of 5, and a click on the floor still locks.

Looking around was jagged. arigl's browser loop yielded on a 0 ms timer,
out of step with the display, so frames stayed up for 0, 1 or 2 refreshes
unevenly: in headless Chrome at 60 Hz, 44-58 of ~385 refreshes got two
frames (one never shown) and 18-19 none. It now waits for the next
animation frame (a 100 ms timer keeps a hidden tab's loop going): 374-375
of ~375 refreshes get exactly one. ?yield=timeout ($CVC_ARIGL_YIELD) is
the A/B. The pointer lock also asks for raw motion (unadjustedMovement:
no pointer acceleration, none of Chromium's jumps under a lock), and takes
the plain lock where a browser has none.

In drag to look, a drag on the floor now steers: the avatar faces the
point under the pointer as it moves, mouse or finger
(CameraController::setPrimaryDragSteers, on in a room; a drag starts
past the 4 px click slop). The right button, or two fingers, turns the
view instead; the browser's context menu is kept off the canvas. A second
finger cancels the first one's press (CameraController::cancelPointer),
so a two-finger turn neither steers nor ends as a tap on the floor. Turns
from a drag are rate-limited like mouse look's (20 a second to the relay,
every frame on screen). The Controls window has the switch: "Drag the
floor to face it (right drag / two fingers look around)".

Tests: cvcgl_camera gains a drag-to-steer section (slop, right drag,
click, cancel, off from state, mouse look); the Controls window test
checks the new row. cvcgl_camera*, cvcgl_imgui_*, ari_golden,
streaming_overlays 13/13, ariadne_runtime 248/248.
… button for messages

In the browser the conference's chat field took typed letters but not
Enter or Backspace, so nothing could be sent. ImGuiOverlay hands ImGui
the keys the viewer's interactor reports, by name. VTK's own windows
name them as X11 does ("Return", "BackSpace", "Left"); in a browser
VTK's interactor passes the page's KeyboardEvent.key ("Enter",
"Backspace", "ArrowLeft"), which the overlay's table did not know.
Letters arrived anyway, through the CharEvent. Headless Chrome with real
key events: "hellox" then Backspace still read "hellox", and Enter
posted nothing.

The table now has both spellings (arrows, PageUp/PageDown, the
modifiers and " " too). It moves to an internal header,
src/cvcGL/ImGuiKeyNames.h, so cvcgl_imgui_key_names can check both
names for each key without a window or GL.

message_input gains `send:`, the label of a button on the field's right
that sends it as Enter does (ImGui; a terminal has Enter only). The
field narrows to leave the button room. The chat component uses
`send: Send`.

Multi-line already works once Enter does: Shift+Enter (and Ctrl+Enter)
add a line, up to max_lines (4), then it scrolls.

Tests: cvcgl_imgui_key_names (new); cvcgl_imgui_chat_widgets gains a
Send case (inside the window, sends, clears, keeps the focus, ignores an
empty field); the loader test reads `send:`.
In a browser cvcGL's onscreen window is now SdlGLRenderWindow too, so SDL's
events drive VTK's interactor there as they do natively: the camera, ImGui
and SdlInput read one input path with one set of key names.

- The canvas fills its container as VTK's canvas window styled it, and SDL
  takes the window's size from the canvas's CSS box when it is made and at
  every resize. The page sizes it; an app's SetSize is ignored there.
- WebGL2 with VTK's attributes: no antialiasing, opaque unless translucent,
  depth and stencil. Blit to the bound framebuffer; color bits from WebGL.
- No swap: Frame() yields where VTK 9.5's canvas window does, so FrameYield
  keeps its meaning. The current context is checked against WebGL's own.
- SDL takes the keys typed into the canvas only, not the page's fields or
  the system keyboard's input. No window title (it would be the page's).
- CameraController's in-press pointer lock and raw motion stay; under the
  lock the interactor's position stands still. While two fingers are down
  the mouse SDL makes of the first stays still (TouchGestures' gesture).
- Text events drop control characters (a keypress types Enter as "\r").
- $CVCGL_SDL_WINDOW=0 in the page's ENV keeps VTK's canvas window.
A tap's compatibility mousedown focused the canvas right after ImGuiOverlay
focused its system-keyboard field, which would put a phone's keyboard away.
VTK's touch handlers cancelled touch events, so there was no mousedown or
click; SDL reads pointer events and cancels nothing.
@transfix transfix changed the title cvcGL: SDL3 owns the browser canvas and its WebGL2 context (stacked on #977, #988, #993) cvcGL: SDL3 owns the browser canvas and its WebGL2 context (stacked on #988, #993) Nov 3, 2026
…/cvcgl-sdl-browser

# Conflicts:
#	src/cvcGL/CameraController.cpp
#	src/cvcGL/TouchGestures.cpp
@transfix transfix changed the title cvcGL: SDL3 owns the browser canvas and its WebGL2 context (stacked on #988, #993) cvcGL: SDL3 owns the browser canvas and its WebGL2 context (stacked on #993) Nov 3, 2026
@transfix transfix changed the title cvcGL: SDL3 owns the browser canvas and its WebGL2 context (stacked on #993) cvcGL: SDL3 owns the browser canvas and its WebGL2 context Nov 3, 2026
@transfix
transfix merged commit 51334f1 into master Nov 3, 2026
23 of 28 checks passed
@transfix
transfix deleted the feat/cvcgl-sdl-browser branch November 3, 2026 19:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant