Skip to content

NF Firefox headless WebGL support; FIX Vertex in Firefox WebGL - #765

Open
kroq-gar78 wants to merge 2 commits into
gallantlab:devfrom
kroq-gar78:firefox
Open

kroq-gar78 wants to merge 2 commits into
gallantlab:devfrom
kroq-gar78:firefox

Conversation

@kroq-gar78

@kroq-gar78 kroq-gar78 commented Oct 13, 2026 •

Copy link
Copy Markdown
Contributor

This PR adds support for Firefox in the headless WebGL renderer, and it also fixes Vertex rendering in Firefox (#375).

Summary

The user can now choose which browser to use in the relevant functions (headless_viewer, save_3d_views) with browser="chromium"|"firefox". Unlike with Chromium, Firefox needs a display buffer to render to. This means that either DISPLAY needs to be set, or the Xvfb package needs to be installed for truly headless support. (For simplicitly, Chromium is still the default.)

Headless support allows us to run tests for Firefox, which confirms #375 . The issue was that the colormapped surface_vertex shader declares more than 16 vertex attributes, which is WebGL's cap. Chromium (ANGLE) drops the unused ones before checking the limit, but Firefox (Mesa) does not, so the shader fails to link. (This failure is actually in the same vein as gh-714, but Chromium doesn't catch it.)

Changes

  1. Add headless Firefox support. A new webgl_browser() context manager in cortex/export/headless.py launches either browser and handles teardown. Headless Firefox needs an X display for WebGL, so when DISPLAY is unset it starts a private Xvfb. The browser and data type tests run in both browsers, and a new test checks that both browsers render each data type alike. The 8 Firefox cases hit by the bug are marked xfail(strict=True).
  2. Fix the shader. surface_vertex now declares only the attributes it reads, and 2D vertex data is packed into two vec2 attributes instead of four floats (shaderlib.js, dataset.js). The 8 xfails now pass.

Running the Firefox tests

CI still installs only Chromium, to keep runs short, so the Firefox tests skip there. To run them locally:

uv run playwright install firefox
sudo apt-get install xvfb      # not needed if DISPLAY is set
uv run pytest -k firefox

Testing

I ran the full suite on both commits:

kroq-gar78 and others added 2 commits October 12, 2026 01:00
headless_viewer, save_3d_views, and plot_panels gain a
browser="chromium"|"firefox" parameter; only the chosen browser needs to
be installed. Firefox needs a real or virtual X display to create its
WebGL context (unlike Chromium's bundled SwiftShader), so when DISPLAY
isn't set a private Xvfb is started, which picks its own display via
-displayfd. The webgl_browser() context manager launches either browser
the way the viewer does, rejects unknown browser names, and owns the
browser and Xvfb teardown; _PlaywrightThread and the shader tests both
use it.

Tests:
- test_webgl_shaders links every shader variant in both browsers. They
  count vertex attributes differently: Chromium's ANGLE drops
  declared-but-unused attributes before enforcing GL_MAX_VERTEX_ATTRIBS,
  Firefox's Mesa counts every declaration.
- test_datatype_renders renders every dataview type in both browsers,
  and test_chromium_matches_firefox checks the two browsers agree on each
  type.
- test_save_3d_views_headless runs in both browsers, covering the
  browser argument of save_3d_views.
- test_headless_viewer_opens_and_closes runs in both browsers and checks
  that the browser and any Xvfb it started have exited after the context.

The colormapped surface_vertex shader on a flatmap subject declares more
than 16 attributes, so in Firefox four shader variants fail to link and
Vertex/Vertex2D render blank. Those 8 cases are marked xfail(strict=True)
until the shader is fixed.

CI deliberately installs only Chromium, to keep CI runs short, so the
Firefox tests skip there. The workflow and AGENTS.md say so. Browser
tests parametrize over testing_utils.BROWSERS, read from the BrowserName
literal, whose Firefox case carries the FIREFOX_MISSING skip mark; its
reason says how to run them locally (playwright install firefox, plus
Xvfb).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The surface_vertex shader declared two vertex attributes it never reads
outside 2D vertex data (data2/data3), relying on the driver to eliminate
them before enforcing GL_MAX_VERTEX_ATTRIBS (16 on all software
rasterizers here). Chromium's ANGLE does prune unused declarations;
Mesa's llvmpipe, used by headless Firefox, counts every declared
attribute. When the program exceeds the limit it fails to link and the
surface renders blank.

Declare only what is actually read: 2D vertex data packs both dimensions
into the .x/.y of two vec2 attributes instead of four separate floats.
The dimension-to-component mapping is unchanged (.x is dim 0, data0 is
frame f, data1 is frame f+1). Other dataviews declare just data0/data1,
and the data2/data3 placeholder attributes in mriview_surface.js go.

DataView._setPacked2D interleaves the two dimensions to match. setFrame
runs on every animation frame during playback, so it keeps the packed
attributes of the current frame pair: an unchanged frame costs nothing
and a one-frame step reuses the previous "next" frame. The streaming
next-frame fallback is shared with VertexData.set via
VertexData.framePair.

This complements gallantlab#715, which packed wmarea/pialarea into auxdat.zw and
flatheight/flatBumpNorms into flatbump; with both, Firefox no longer
needs the unused data attributes to be pruned. The 8 Firefox shader-link
and vertex-render tests marked xfail now pass, so the marks are removed.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@kroq-gar78 kroq-gar78 changed the title NF Firefox playwright support; FIX Vertex in Firefox NF Firefox headless webgl support; FIX Vertex in Firefox Oct 13, 2026
@kroq-gar78 kroq-gar78 changed the title NF Firefox headless webgl support; FIX Vertex in Firefox NF Firefox headless webgl support; FIX Vertex in Firefox webgl Oct 13, 2026
@kroq-gar78 kroq-gar78 changed the title NF Firefox headless webgl support; FIX Vertex in Firefox webgl NF Firefox headless WebGL support; FIX Vertex in Firefox WebGL Oct 13, 2026
@kroq-gar78
kroq-gar78 marked this pull request as ready for review October 13, 2026 21:23

This branch has not been deployed

No deployments
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