Skip to content

fix(wasm): Size strided WebGL uploads to their row length - #25147

Merged
MartinZikmund merged 6 commits into
masterfrom
dev/mazi/wasm-texsubimage-rowlength
Oct 10, 2026
Merged

MartinZikmund merged 6 commits into
masterfrom
dev/mazi/wasm-texsubimage-rowlength

Conversation

@MartinZikmund

@MartinZikmund MartinZikmund commented Oct 9, 2026 •

Copy link
Copy Markdown
Member

GitHub Issue: closes #25146

PR Type:

🐞 Bugfix

What changed? 🚀

On Skia WebAssembly, text randomly lost glyphs ("Browse Grid" → "rowse r") whenever the app's WASM memory limit is above 2 GB, as in SamplesApp (EmccMaximumHeapSize = 4 GB).

Why: above 2 GB, emscripten 3.1.56 turns off its garbage-free WebGL2 upload path (a workaround for Firefox bug 1838218). The fallback slices the pixel source as height * width rows and ignores UNPACK_ROW_LENGTH / UNPACK_SKIP_*. Skia uploads partial glyph-atlas patches with a row length set, so WebGL rejected them with INVALID_OPERATION: texSubImage2D: ArrayBufferView not big enough. Those atlas cells stayed empty while Skia believed they were filled.

Fix: EmscriptenWebGL.createContext now wraps pixelStorei, texImage2D and texSubImage2D on every emscripten-managed WebGL2 context (the Skia surface and GLCanvasElement). When a typed-array upload is shorter than the strided region the unpack state describes, the view is re-sliced to the required length from the same buffer. A short view keeps clear of the large-offset Firefox bug that the garbage-free path would reintroduce. Uploads that are already large enough, PBO offsets and non-array sources pass through untouched.

Validation

Check Result
Red (Runtime, Chromium, unpatched WebGL2 context) the short view a strided upload gets → 1282 INVALID_OPERATION; a correctly sized view → 0
Given_WasmWebGlUploads (Runtime, Skia WASM, net10 publish) 3/3 passed: a recording fake context gets the strided view sizes (64,456,456,64; red before: 64,64,64,64) — this one needs no GPU, so it runs on CI; plus a strided upload on a real patched WebGL2 context raises no error, and the live Skia context is patched (these two are Inconclusive on CI, whose browser has no WebGL2)
SamplesApp on WASM with the fix (Runtime) 0 texSubImage2D errors in the console, text renders fully. Without it, the revamped shell (#25108) dropped glyphs, and 5 of 10 instrumented uploads failed
A/B, same published build with only the fixStridedUploads install call removed (Runtime, Chromium, 1280x800, ?sample=RichTextBlock_Showcase) without: 7 texSubImage2D errors, visible dropout ("The qu ck ow f ju ps o the l zy", command-bar icons and description strip shredded) · with: 0 errors, all text intact
Skia desktop not affected (native GL); no change

Performance

Measured in Chromium on the fixed build (Runtime):

Per call
texSubImage2D, unpatched (no-op target) ~15 ns
patched, no row length set ~72 ns (+55 ns)
patched, strided (re-sliced view) ~266 ns (+250 ns)

No pixel data is copied; the strided path only creates a view over the same buffer. Real upload counts over a full page load: RichTextBlock_Showcase 7 texSubImage2D (6 strided) + 157 pixelStorei; default landing 9 (6 strided) + 71 pixelStorei, with 0.1-0.3 ms spent in the native calls. That puts the added cost at roughly 10 µs per page load, once, during loading; nothing per frame. Without the fix, each rejected upload also costs a GL error and a console warning.

Why now

Uploads only take this path when MAXIMUM_MEMORY is above 2 GB. SamplesApp went to 4 GB in f49c78d (perf samples); at the 2 GB default, emscripten passes the whole heap plus an offset and WebGL sizes strided uploads correctly. Apps on the default ceiling are unaffected.

PR Checklist ✅

🤖 Generated with Claude Code

https://claude.ai/code/session_01Dpy6FGmTfiTyJWZnteuZZG

MartinZikmund and others added 2 commits October 9, 2026 12:50
Above a 2GB MAXIMUM_MEMORY, emscripten drops its garbage-free WebGL2 upload
path and slices the source as width * height, ignoring UNPACK_ROW_LENGTH and
UNPACK_SKIP_*. Skia uploads glyph atlas patches with a row length, so WebGL
rejected them with INVALID_OPERATION and glyphs rendered blank.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01Dpy6FGmTfiTyJWZnteuZZG
Copilot AI balanced review requested due to automatic review settings October 9, 2026 10:51
@MartinZikmund MartinZikmund added kind/bug Something isn't working platform/wasm 🌐 Categorizes an issue or PR as relevant to the WebAssembly platform labels Oct 9, 2026
@MartinZikmund MartinZikmund self-assigned this Oct 9, 2026

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@github-actions

github-actions Bot commented Oct 9, 2026 •

Copy link
Copy Markdown
Contributor

Claude finished @MartinZikmund's task in 28s —— View job


Review of #25147

Verdict: looks good, with minor comments. I reviewed the diff by inspection only. I did not build or run anything.

  • Medium: 1 (Assert.Inconclusive in the runtime tests, which makes a missing WebGL2 context a non-failure).
  • Low: 1 (a rounding edge case in the re-slice length).

Cross-cutting notes

  • The root cause is correctly identified: emscripten's non-garbage-free fallback ignores UNPACK_ROW_LENGTH and UNPACK_SKIP_*. Wrapping at context creation fixes it at the source, so this is a root-cause fix for the Skia and GLCanvasElement contexts. It is not a guard on the symptom.
  • Only texImage2D and texSubImage2D are covered. texImage3D and texSubImage3D also use UNPACK_IMAGE_HEIGHT and UNPACK_SKIP_IMAGES. That is fine if nothing uploads 3D textures today, but worth a comment in the code.
  • The wrappers use rest-args and apply on every upload, even when no unpack state is set. Skia uploads are infrequent enough that this is unlikely to matter, but an early fast-path check on arguments.length would avoid the allocation.
  • The PR notes red evidence only for an unpatched context. Test 1 does run the fix against a native context, so it would fail if the re-slice were removed. That is adequate coverage.

Rounding the re-sliced length up to whole elements could run past the end of
the buffer. Also skip the argument copy when no unpack stride is set.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01Dpy6FGmTfiTyJWZnteuZZG
Copilot AI balanced review requested due to automatic review settings October 9, 2026 11:39

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@MartinZikmund

MartinZikmund commented Oct 9, 2026 •

Copy link
Copy Markdown
Member Author

🤖 Addressed PR comments up to this point

Re the Claude review summary:

  • Low (re-slice rounding): fixed in 494707a, the view length is clamped to the buffer (thread resolved).
  • 3D uploads not covered: added a code comment. Skia doesn't upload 3D textures, so texImage3D/texSubImage3D stay unwrapped.
  • Per-upload allocation: the wrappers now pass arguments straight through and only copy them when an unpack stride is set.
  • Medium (Assert.Inconclusive without WebGL2): kept on purpose. Without WebGL2 the app renders through the software renderer, so the fix is never installed and there is nothing to verify; failing there would be a false negative. On the CI WASM leg WebGL2 is available, so both tests do run. Correction: that was wrong. CI's headless browser has no WebGL2, so both tests were Inconclusive there; see the follow-up below.

@github-actions

github-actions Bot commented Oct 9, 2026 •

Copy link
Copy Markdown
Contributor

Claude finished @MartinZikmund's task in 22s —— View job


Review of #25147 (updated for the latest push)

Verdict: fix-first (minor). I reviewed by inspection only. I did not build or run anything.

  • High: 0
  • Medium: 2, both Assert.Inconclusive in Given_WasmWebGlUploads.cs (inline comments posted)
  • Low: 0

Cross-cutting notes

  • This is a root-cause fix. Wrapping pixelStorei, texImage2D and texSubImage2D at context creation addresses the emscripten fallback that ignores the unpack state.
  • The latest commits resolved the earlier review notes:
    • The re-slice length is now clamped to the buffer end.
    • The wrappers return early when no unpack state is set.
    • The texImage3D and texSubImage3D gap is documented in a comment.
  • Beyond the two Assert.Inconclusive calls, the tests need no changes. Test 1 runs the re-slice against a native context, so it would fail if the re-slice were removed.

MartinZikmund and others added 2 commits October 9, 2026 14:53
Checking for the row length constant instead of the WebGL2RenderingContext
type keeps WebGL 1 out and lets the upload fix be exercised without a GPU.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01Dpy6FGmTfiTyJWZnteuZZG
CI browsers have no WebGL2, so the real-context tests were inconclusive there.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01Dpy6FGmTfiTyJWZnteuZZG
Copilot AI balanced review requested due to automatic review settings October 9, 2026 12:54

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@github-actions

github-actions Bot commented Oct 9, 2026 •

Copy link
Copy Markdown
Contributor

Claude finished @MartinZikmund's task in 35s —— View job


Review of #25147 (latest push)

Verdict: fix-first (minor). I reviewed by inspection only. I did not build or run anything.

  • High: 0
  • Medium: 2, both Assert.Inconclusive in Given_WasmWebGlUploads.cs (inline comments posted)
  • Low: 0

Notes

  • This is a root-cause fix. Wrapping pixelStorei, texImage2D and texSubImage2D at context creation addresses the emscripten fallback that ignores the unpack state.
  • The new RecordedUploadScript test covers the sizing logic without a GPU, using a mocked context. It covers the unstrided, row-length, texImage2D and past-the-buffer cases, and it is the right way to get CI coverage. The two real-context tests still skip when there is no WebGL2. Gate them with a platform condition rather than skipping at runtime.
  • One inline comment landed on line 81 instead of line 71 (the first Assert.Inconclusive). The text still applies to line 71.
  • The earlier review notes are resolved: the length is clamped to the buffer, the early return skips the copy when no unpack state is set, and the 3D gap is documented in a comment.

@MartinZikmund

Copy link
Copy Markdown
Member Author

Follow-up on the Assert.Inconclusive point. I claimed CI's WASM leg has WebGL2; it doesn't: both tests were Inconclusive in build 238496, so the fix had no CI coverage. Addressed in the latest push:

  • fixStridedUploads now recognizes a WebGL2 context by its UNPACK_ROW_LENGTH constant instead of instanceof WebGL2RenderingContext (WebGL 1 is still skipped).
  • New When_Row_Length_Set_Then_Upload_View_Covers_Strided_Rows applies the fix to a recording fake context and asserts the exact view sizes handed to the original calls. It needs no GPU, so it runs on CI. Red against the previous build: 64,64,64,64; green now: 64,456,456,64.
  • The two real-WebGL2 tests stay as end-to-end checks where WebGL2 exists (Inconclusive otherwise).

Local Skia WASM: Given_WasmWebGlUploads 3/3.

WasmSemanticDomHelper exists only under HAS_UNO, so the WinAppSDK head failed
to build.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01Dpy6FGmTfiTyJWZnteuZZG
Copilot AI balanced review requested due to automatic review settings October 9, 2026 18:31

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@github-actions

github-actions Bot commented Oct 9, 2026 •

Copy link
Copy Markdown
Contributor

Claude finished @MartinZikmund's task in 22s —— View job


Review of #25147 (latest push)

Verdict: fix-first (minor). I reviewed by inspection only. I did not build or run anything.

  • High: 0
  • Medium: 2 (both Assert.Inconclusive in Given_WasmWebGlUploads.cs, posted inline)
  • Low: 0

Notes

  • This is a root-cause fix. fixStridedUploads addresses the emscripten fallback that ignores the unpack state, rather than guarding the symptom.
  • The re-slice length is clamped to the buffer, and the wrappers skip the argument copy when no unpack state is set.
  • The GPU-free RecordedUploadScript test gives CI coverage of the sizing logic. Only the two real-WebGL2 tests rely on Assert.Inconclusive.

@nventive-devops

Copy link
Copy Markdown
Contributor

The build 238622 found UI Test snapshots differences: skia-linux-screenshots: 118, skia-windows-screenshots: 1164

Details
  • skia-linux-screenshots: 118 changed over 2458

    🚨🚨 Comparison Details (first 20) 🚨🚨
    • Buttons.png-dark
    • Buttons.png
    • CalendarView_Theming.png-dark
    • CalendarView_Theming.png
    • ButtonClippingTestsControl.png-dark
    • ButtonClippingTestsControl.png
    • ClipboardTests.png-dark
    • CompositionEffectBrush.png-dark
    • CompositionEffectBrush.png
    • CompositionNineGridBrush_Source_Changes.png-dark
    • CompositionNineGridBrush_Source_Changes.png
    • DisplayInformation.png-dark
    • ElementLevelTheme.png-dark
    • ElementLevelTheme.png
    • ImageIconPage.png-dark
    • ImageIconPage.png
    • Examples.png
    • DynamicFont.png-dark
    • DynamicFont.png
    • DoubleImageBrushInList.png-dark
  • skia-windows-screenshots: 1164 changed over 2474

    🚨🚨 Comparison Details (first 20) 🚨🚨
    • Accessibility_ScreenReader.png
    • AutoBorderStretchwithleftmargin.png-dark
    • AutomationProperties_Relations.png-dark
    • AutoSuggestBox_BitmapIcon.png-dark
    • BackGesture.png-dark
    • BackGesture.png
    • BorderBottomwithmargins.png-dark
    • BorderBottomwithmargins.png
    • BorderLeftwithmargins.png-dark
    • BorderLeftwithmargins.png
    • BorderVersusPanelClipping.png-dark
    • BorderVersusPanelClipping.png
    • Border_LinearGradientBrush.png-dark
    • Border_LinearGradientBrush.png
    • Border_Simple.png-dark
    • Border_Simple.png
    • BreadcrumbBarPage.png-dark
    • BreadcrumbBarPage.png
    • Capture with overlap.png-dark
    • Capture with overlap.png

@MartinZikmund
MartinZikmund merged commit 3127b59 into master Oct 10, 2026
51 checks passed
@MartinZikmund
MartinZikmund deleted the dev/mazi/wasm-texsubimage-rowlength branch October 10, 2026 04:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

kind/bug Something isn't working platform/wasm 🌐 Categorizes an issue or PR as relevant to the WebAssembly platform

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[WASM] Glyphs randomly missing when MAXIMUM_MEMORY > 2GB (strided texSubImage2D rejected)

3 participants