Skip to content

fix(wasm): Fix GLCanvasElement (WebGL) under WASM AOT - #23834

Merged
ramezgerges merged 7 commits into
unoplatform:masterfrom
ramezgerges:fix/glcanvaselement-wasm-aot
Jul 29, 2026
Merged

ramezgerges merged 7 commits into
unoplatform:masterfrom
ramezgerges:fix/glcanvaselement-wasm-aot

Conversation

@ramezgerges

Copy link
Copy Markdown
Contributor

GitHub Issue: closes unoplatform/kahua-private#520

PR Type:

🐞 Bugfix

What changed? 🚀

GLCanvasElement (WebGL on the Skia WebAssembly target) crashed under full
WASM AOT (RunAOTCompilation=true / WasmBuildNative). GL functions with
more than 8 arguments (the texture upload family — glTexImage2D/3D,
glTexSubImage2D/3D, glCompressedTexImage/SubImage*, glCopyTexSubImage3D,
glBlitFramebuffer) cannot round-trip through a managed [UnmanagedCallersOnly]
dispatcher on browser-wasm (8-arg cap, dotnet/runtime#109338), and under AOT a
C→managed callback resolved by name has no valid native-to-interp entry.

This PR resolves GL the other way around:

  • Serve GL from emscripten's native C GL library (-lGL, the same one
    SkiaSharp links) via a new uno_gl_resolve entry point, instead of forwarding
    every large-arity function to JS through EM_JS. GetProcAddress now prefers
    the native address for each name.
  • Keep small C wrappers only for glTexImage2D / glTexImage3D, which add
    the WebGL2 unsized→sized internal-format promotion emscripten doesn't perform.
  • JS shim is now a fallback only — used solely for entry points the native
    proc table doesn't expose (logged at Debug so a validation run shows exactly
    what still needs it).
  • Build-time SignaturePrimer retained: Silk.NET still callis into the
    resolved addresses, so under the interpreter those managed→native calls need
    matching invoke trampolines. The always-false-guarded [DllImport]s prime the
    float-bearing and 9/10/11-arg all-i32 signatures at build time.
  • Sample added: GLCanvasElement ModelViewer sample in the SamplesApp for
    manual/visual validation of the texturing + >8-arg path.

Validation

  • Compile: Uno.UI.Runtime.Skia.WebAssembly.Browser builds clean (net10.0).
  • Runtime (AOT): exercised via a standalone WASM app publishing with full
    emscripten AOT; the native link (wasm-ld) resolves with no undefined symbols
    and the >8-arg texture path runs. Broader per-sample AOT sweep across the
    GLCanvasElement samples is in progress.

PR Checklist ✅

ramezgerges and others added 3 commits July 21, 2026 11:00
The nine >8-arg GL functions (glTexImage2D, glTexSubImage2D, glTexImage3D,
glTexSubImage3D, glCopyTexSubImage3D, glCompressed{Tex,TexSub}Image*, and
glBlitFramebuffer) crashed under WebAssembly full AOT with "null function or
function signature mismatch" on the first GLCanvasElement render.

Root cause: the shim routed Silk.NET's >8-arg calli into a C wrapper that
called back into a managed [UnmanagedCallersOnly] dispatcher resolved by
EntryPoint name. That native-to-interp callback has no valid entry under full
AOT (the managed->native calli hop itself works). The [UnmanagedCallersOnly]
8-arg cap (dotnet/runtime#109338) and the SignaturePrimer never addressed this.

Fix: the C shim now forwards straight to the JS WebGL layer via EM_JS, removing
the managed callback hop entirely. Silk.NET still calli's into the C wrappers
(the hop that already works). Removes the now-dead managed dispatchers, their
JSImports, and the ILLink descriptor that preserved them.

Validated against the unoplatform/kahua-private#520 repro: renders correctly on
both WASM full AOT (Release publish) and the interpreter (Debug), no crash.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
A lit sphere encircled by a tilted ring, ported from the kahua-private#520
repro. Exercises the framebuffer color-attachment and texture-upload paths that
regressed under WebAssembly full AOT, so it doubles as the regression artifact
for a future full-AOT WASM validation leg.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
Serve GL entry points (including the >8-arg texture functions) from
emscripten's native C GL library - shared with SkiaSharp via -lGL -
through uno_gl_resolve, instead of forwarding each one to JS via EM_JS.
Small C wrappers remain only for glTexImage2D/glTexImage3D to add the
WebGL2 unsized->sized internal-format promotion emscripten omits; every
other name falls back to the hand-written JS shim only when the native
proc table has no entry.

Silk.NET still calli's into the resolved addresses, so the build-time
SignaturePrimer is kept to emit the matching interp->native trampolines.

See unoplatform/kahua-private#520

Co-Authored-By: Claude Opus 4.8 <[email protected]>
Copilot AI review requested due to automatic review settings July 21, 2026 18:38
@github-actions github-actions Bot added platform/wasm 🌐 Categorizes an issue or PR as relevant to the WebAssembly platform area/automation Categorizes an issue or PR as relevant to project automation labels Jul 21, 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.

Pull request overview

This PR fixes GLCanvasElement WebGL crashes under WebAssembly full AOT by switching GL entry-point resolution to prefer emscripten’s native C GL (-lGL) via a new uno_gl_resolve resolver, with the JS shim kept only as a fallback for missing proc-table entries.

Changes:

  • Updated WasmGLFunctions.GetProcAddress to resolve GL entry points via uno_gl_resolve first, falling back to the existing JS shim when needed (with debug logging).
  • Simplified the large-arity shim path by removing the managed [UnmanagedCallersOnly] dispatchers and retaining only the signature-priming DllImports required for interpreter trampolines.
  • Added a new SamplesApp “ModelViewer” manual sample to exercise the 9-arg texture upload path.

Reviewed changes

Copilot reviewed 8 out of 8 changed files in this pull request and generated 6 comments.

Show a summary per file
File Description
src/Uno.UI.Runtime.Skia.WebAssembly.Browser/Uno.UI.Runtime.Skia.WebAssembly.Browser.csproj Removes the embedded ILLink descriptor previously used to preserve managed GL shim exports.
src/Uno.UI.Runtime.Skia.WebAssembly.Browser/Graphics/WasmGLShim.ILLink.Descriptors.xml Deletes the trimmer descriptor now that the C shim no longer depends on managed exports.
src/Uno.UI.Runtime.Skia.WebAssembly.Browser/Graphics/WasmGLFunctions.LargeArityShims.cs Removes the packed-args managed dispatchers and keeps only signature priming for interpreter trampolines.
src/Uno.UI.Runtime.Skia.WebAssembly.Browser/Graphics/WasmGLFunctions.cs Prefers native proc resolution via uno_gl_resolve, with JS shim fallback and logging.
src/Uno.UI.Runtime.Skia.WebAssembly.Browser/build/native/uno_gl_shim.c Implements uno_gl_resolve and keeps small promotion wrappers + signature-primer dummy exports.
src/SamplesApp/SamplesApp.Samples/Windows_UI_Composition/ModelViewerGlCanvasElement.cs Adds a GLCanvasElement-based “model viewer” element for manual/visual validation.
src/SamplesApp/SamplesApp.Samples/Windows_UI_Composition/GLCanvasElement_ModelViewer.xaml.cs Registers the new manual sample via [Sample].
src/SamplesApp/SamplesApp.Samples/Windows_UI_Composition/GLCanvasElement_ModelViewer.xaml Adds the XAML surface for the sample, following existing GLCanvasElement sample patterns.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@unodevops

Copy link
Copy Markdown
Contributor

⚠️⚠️ The build 223408 has failed on Uno.UI - CI.

- Rewrite the WasmGLFunctions header and fix stale comments that still
  described the old packed-argument managed dispatcher path; document the
  native-first uno_gl_resolve resolution instead.
- Cache the logger in a local in the GetProcAddress fallback path instead
  of resolving it twice.
- Replace the private-tracker references in code/comments/sample metadata
  with generic internal-tracking wording, and fix the "calli's" wording.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
Copilot AI review requested due to automatic review settings July 22, 2026 16:21
@ramezgerges

Copy link
Copy Markdown
Contributor Author

Thanks for the review. Addressed in f978c1d:

  • WasmGLFunctions.cs header + RegisterCoreEntries comment: rewritten to describe the native-first uno_gl_resolve resolution; removed the stale packed-argument managed-dispatcher description.
  • Logger double-lookup: cached typeof(WasmGLFunctions).Log() in a local for the fallback path.
  • Private-tracker references (LargeArityShims.cs, uno_gl_shim.c, ModelViewerGlCanvasElement.cs, GLCanvasElement_ModelViewer.xaml.cs): replaced with generic internal-tracking wording, and fixed the calli's wording.

Note: the failing Uno.UI - CI / Analyze (csharp) checks are unrelated to this PR — they fail on NU1903 (System.Security.Cryptography.Xml 8.0.3 advisory tripping warnings-as-errors in Uno.HotReload / RemoteControl.Server.Processors), which affects the branch independently of these changes.

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.

Pull request overview

Copilot reviewed 8 out of 8 changed files in this pull request and generated 2 comments.

@unodevops

Copy link
Copy Markdown
Contributor

Guard the uno_gl_resolve P/Invoke in ResolveNative against
DllNotFoundException/EntryPointNotFoundException so a missing or unlinked
native shim degrades to the JS-shim fallback instead of crashing
GetProcAddress. A latched flag short-circuits subsequent lookups and the
condition is logged once.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
Copilot AI review requested due to automatic review settings July 22, 2026 16:46

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.

Pull request overview

Copilot reviewed 8 out of 8 changed files in this pull request and generated 1 comment.

Comments suppressed due to low confidence (1)

src/Uno.UI.Runtime.Skia.WebAssembly.Browser/Uno.UI.Runtime.Skia.WebAssembly.Browser.csproj:70

  • PR description currently references a private issue ("closes unoplatform/kahua-private#520"). This repo’s PR requirements call for a public, fully-qualified issue URL in the Fixes/Closes/Related footer, or an explicit "No related issue" note (with approver) if this is internal maintenance.
	<ItemGroup>
		<_UnoInjectedResource Include="WasmScripts\**\*.js" LogicalName="$(RootNamespace).WasmScripts.%(FileName)%(Extension)" />
		<EmbeddedResource Include="WasmCSS\**\*.css" />
		<TypeScriptCompile Include="ts\**\*.ts;ts\**\*.tsx" />

		<TypeScriptCompile Include="..\Uno.UI\ts\types\**\*.ts" />

@unodevops

Copy link
Copy Markdown
Contributor

It no longer registered any proc addresses (uno_gl_resolve serves those);
it only forwarded to KeepSignaturePrimer. Call KeepSignaturePrimer
directly from the static ctor so the initialization flow reflects what
actually happens.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
Copilot AI review requested due to automatic review settings July 22, 2026 22:50

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.

Pull request overview

Copilot reviewed 8 out of 8 changed files in this pull request and generated no new comments.

@unodevops

Copy link
Copy Markdown
Contributor

@unodevops

Copy link
Copy Markdown
Contributor

⚠️⚠️ The build 223720 has failed on Uno.UI - CI.

@ajpinedam ajpinedam 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.

Good

Group the native-resolver flag with _addresses at the top of the class
instead of declaring it mid-file next to ResolveNative.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
Copilot AI review requested due to automatic review settings July 29, 2026 14:01

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.

Pull request overview

Copilot reviewed 8 out of 8 changed files in this pull request and generated no new comments.

Comments suppressed due to low confidence (1)

src/Uno.UI.Runtime.Skia.WebAssembly.Browser/Graphics/WasmGLFunctions.cs:91

  • The error log in the ResolveNative fallback path claims the JS shim will be used for all GL entry points, but key names (e.g. glTexImage2D/glTexImage3D and other >8-arg functions) are not registered in the JS fallback dictionary and will still resolve to IntPtr.Zero. This message is misleading for diagnosing missing native shim linkage/symbols.
			if (typeof(WasmGLFunctions).Log().IsEnabled(LogLevel.Error))
			{
				typeof(WasmGLFunctions).Log().Error("Native GL resolver (uno_gl_shim) is unavailable; falling back to the JS shim for all GL entry points.", e);
			}

@unodevops

Copy link
Copy Markdown
Contributor

@ramezgerges
ramezgerges merged commit 23609ca into unoplatform:master Jul 29, 2026
66 checks passed
@ramezgerges
ramezgerges deleted the fix/glcanvaselement-wasm-aot branch July 29, 2026 22:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/automation Categorizes an issue or PR as relevant to project automation 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.

4 participants