Repository navigation
Description
Current behavior 🐛
On WebAssembly, GLCanvasElement requests its framebuffer readback in BGRA, but WebGL 2.0 does not accept GL_BGRA as a readPixels format. The read fails with GL_INVALID_ENUM, the destination buffer is left untouched, and the element stops rendering permanently.
The browser console shows:
WebGL: INVALID_ENUM: readPixels: invalid format
and Uno logs (Error level):
GLCanvasElement rendering failed. The element will no longer render.
System.InvalidOperationException: The framebuffer readback failed with InvalidEnum while copying the framebuffer to the back buffer.
Because the exception is caught internally and IsGLInitialized is set to false, the visible result is a blank canvas inside an otherwise working app.
Worth noting for diagnosability: in a Release build the Uno error above does not reach the browser console at all — only the bare WebGL: INVALID_ENUM warning appears. A Release/AOT user therefore sees a single WebGL warning that looks benign, with nothing linking it to GLCanvasElement.
Why the BGRA path is reached on WASM
NeedsRgbaReadbackSwap() deliberately short-circuits for the browser:
// WASM readback stays BGRA regardless of what the context reports: the JS shim swaps
// internally and doesn't surface the native readback capabilities.
if (OperatingSystem.IsBrowser())
{
return false;
}That premise no longer holds. WasmGLFunctions.GetProcAddress resolves entry points from emscripten's native GL first, using the TypeScript shim only as a fallback for names emscripten doesn't expose:
int uno_gl_resolve(const char* name) {
if (strcmp(name, "glTexImage2D") == 0) return (int)(intptr_t)&uno_glTexImage2D;
if (strcmp(name, "glTexImage3D") == 0) return (int)(intptr_t)&uno_glTexImage3D;
return (int)(intptr_t)emscripten_webgl_get_proc_address(name);
}Only glTexImage2D/glTexImage3D are special-cased, so glReadPixels resolves to emscripten's implementation and the shim's BGRA→RGBA translation in WasmGLFunctions.ts (isBgraByte / effectiveFormat) is never reached for GLCanvasElement.
Expected behavior 🎯
GLCanvasElement renders on WebAssembly, with the readback using a format WebGL 2.0 accepts (RGBA + R/B swap, as already implemented for GLES via _readbackAsRgbaWithSwap).
How to reproduce it (as minimally and precisely as possible) 🔬
A. Attached minimal repro — GLCanvasReadbackRepro.zip (Uno.Sdk 6.7.22, single project, one GLCanvasElement that clears to solid blue)
dotnet run --project GLCanvasReadbackRepro/GLCanvasReadbackRepro.csproj -f net10.0-browserwasmThe blue square never appears. No AOT and no Release build are needed — plain Debug/interpreter reproduces it. The same project on -f net10.0-desktop renders correctly.
It reproduces under dotnet publish -f net10.0-browserwasm -c Release with full AOT as well, so it is not execution-mode specific.
The whole element is:
public class SolidColorGlCanvasElement : GLCanvasElement
{
public SolidColorGlCanvasElement() : base(null) { }
protected override void Init(GL gl) { }
protected override void RenderOverride(GL gl)
{
gl.ClearColor(0.2f, 0.4f, 0.8f, 1.0f);
gl.Clear((uint)ClearBufferMask.ColorBufferBit);
}
protected override void OnDestroy(GL gl) { }
}(What is drawn is irrelevant — the failure is in the readback that follows.)
B. Standalone WebGL 2.0 proof (no Uno needed)
This mirrors FrameBufferDetails (RGBA8 texture colour attachment on an FBO) and isolates the format rejection:
<canvas id=c width=64 height=64></canvas>
<script>
const GL_BGRA = 0x80E1, UB = 0x1401, RGBA = 0x1908;
const gl = document.getElementById('c').getContext('webgl2');
const tex = gl.createTexture();
gl.bindTexture(gl.TEXTURE_2D, tex);
gl.texStorage2D(gl.TEXTURE_2D, 1, gl.RGBA8, 64, 64);
const fbo = gl.createFramebuffer();
gl.bindFramebuffer(gl.FRAMEBUFFER, fbo);
gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.TEXTURE_2D, tex, 0);
gl.clearColor(0.2, 0.4, 0.6, 1); gl.clear(gl.COLOR_BUFFER_BIT);
gl.readBuffer(gl.COLOR_ATTACHMENT0);
while (gl.getError() !== gl.NO_ERROR) {}
const bgra = new Uint8Array(64*64*4);
gl.readPixels(0, 0, 64, 64, GL_BGRA, UB, bgra);
console.log('BGRA error 0x' + gl.getError().toString(16), 'buffer all zero:', bgra.every(v => v === 0));
while (gl.getError() !== gl.NO_ERROR) {}
const rgba = new Uint8Array(64*64*4);
gl.readPixels(0, 0, 64, 64, RGBA, UB, rgba);
console.log('RGBA error 0x' + gl.getError().toString(16), 'first pixel', rgba.slice(0,4));
</script>Observed in Chromium:
| Case | Result |
|---|---|
| BGRA read | 0x500 INVALID_ENUM, destination buffer entirely zero |
| RGBA read (control) | no error, correct pixel [51,102,153,255] |
BGRA read with PIXEL_PACK_BUFFER bound |
0x500 INVALID_ENUM |
IMPLEMENTATION_COLOR_READ_FORMAT |
0x1908 (RGBA) — BGRA never advertised |
Workaround 🛠️
None from application code — the readback format is chosen internally by GLCanvasElement.
Suggested fix
The cleanest option looks like removing the OperatingSystem.IsBrowser() short-circuit in NeedsRgbaReadbackSwap() and letting the existing 1×1 BGRA probe run on the browser too: it would observe INVALID_ENUM and select the already-implemented RGBA + SwapRedBlue path, exactly as it does for GLES drivers that reject BGRA.
Alternatives: force _readbackAsRgbaWithSwap = true on browser, or add a glReadPixels wrapper to uno_gl_resolve that performs the translation natively.
Separately, the PIXEL_PACK_BUFFER branch in WasmGLFunctions.ts cannot translate BGRA at all (per its own comment) and performs no error check — worth rejecting BGRA there explicitly rather than forwarding an invalid enum.
Test coverage
There appear to be no GLCanvasElement runtime tests in Uno.UI.RuntimeTests, which is why this wasn't caught. A test asserting non-blank readback pixels on WASM would cover the regression.
Renderer 🎨
Skia
Affected platforms 📱💻🖥️
WebAssembly
Uno.Sdk version (and other relevant versions) 📦
Confirmed at runtime on Uno.Sdk 6.7.22 (attached repro), net10.0-browserwasm, Debug / interpreter — no AOT involved, so this is not AOT-specific:
WebGL: INVALID_ENUM: readPixels: invalid format
fail: Uno.WinUI.Graphics3DGL.GLCanvasElement[0]
GLCanvasElement rendering failed. The element will no longer render.
System.InvalidOperationException: The framebuffer readback failed with InvalidEnum while copying the framebuffer to the back buffer.
at Uno.WinUI.Graphics3DGL.GLCanvasElement.Render()
Verified by inspection that GLCanvasElement.cs, uno_gl_shim.c and WasmGLFunctions.ts are byte-identical (same blob SHAs) on master, release/stable/6.7 and servicing/6.8 — so all three are affected and no fix has landed on the default branch.
Regression
This is a regression, not a day-one bug. GLCanvasElement rendered correctly on WebAssembly before 748231530f — "fix(wasm): Resolve GL from emscripten native GL for AOT", merged 2026-07-29 via #23834. That commit made GetProcAddress resolve from emscripten's native GL table first, which bypasses the TypeScript shim's BGRA translation for glReadPixels.
Same repro project, identical source, only the SDK differs:
| Uno.Sdk | Uno.WinUI | source commit | contains 748231530f |
result |
|---|---|---|---|---|
6.7.0-dev.125 |
6.7.0-dev.815 |
21bf1ad6 (2026-07-16) |
no | renders correctly, no readPixels error |
6.7.22 |
6.7.103 |
cd452e3e (2026-08-29) |
yes | blank, readPixels: invalid format |
Listing every commit touching Uno.UI.Runtime.Skia.WebAssembly.Browser/Graphics/, WasmGLFunctions.ts, build/native/ and AddIns/Uno.WinUI.Graphics3DGL/ between those two builds gives 12 commits, of which exactly one changes GL entry-point resolution: 748231530f. (c60fd08ba4 only adds a fallback for when the native resolver is missing entirely.)
Compounding it, eb66ce71f6 — "probe GLES BGRA readback support with a real read and fall back to RGBA+swap when it fails" — landed after 748231530f and introduced the OperatingSystem.IsBrowser() short-circuit quoted above, justified by the shim swapping internally. That assumption was already invalid by then, so the probe that would otherwise auto-detect this is explicitly disabled on the one platform that needs it. Removing that short-circuit is why the suggested fix above should work unchanged.
Activity
[-][WASM] GLCanvasElement renders blank: BGRA glReadPixels is rejected by WebGL 2.0[/-][+][WASM] Regression: GLCanvasElement renders blank — BGRA glReadPixels rejected by WebGL 2.0[/+]darenm commentedon Sep 15, 2026
@agneszitte @ramezgerges I hit this yesterday running one of our feature prototypes in WASM.
Trying this workaround in our code:
agneszitte commentedon Sep 16, 2026
Thank you for the feedback, @darenm. I’ll let @ramezgerges take a look at the details you shared.
fix(3dgl): Probe the readback format on WebAssembly too
agneszitte commentedon Oct 13, 2026
Hi @darenm (Cc @jbourque-kahua), the fix for this issue can be tested with Uno.Sdk 6.8.0-dev.62 (#25159).
With it, you should be able to remove the workaround.
Could you please confirm that GLCanvasElement renders correctly on WebAssembly on your side, so we can plan to include it in the next 6.7 service release?
Thanks!
darenm commentedon Oct 14, 2026
@jbourque-kahua - you can test this by pulling down the repro linked above and running it with the new version of Uno.Sdk.
fix(3dgl): Probe the readback format on WebAssembly too
fix(3dgl): Probe the readback format on WebAssembly too
jbourque-kahua commentedon Oct 21, 2026
Confirm fixed 🎉
fix(3dgl): Probe the readback format on WebAssembly too
fix(3dgl): Probe the readback format on WebAssembly too
jbourque-kahua commentedon Nov 5, 2026
@agneszitte Confirm fixed in 6.7.45 🎉