Skip to content

Commit 3047f3a

Browse files
ramezgergesmattleibowclaudeCopilot
authored
Add Graphite GPU backend bindings for Vulkan, Metal, and Dawn (#3968)
Add Graphite GPU backend bindings for Vulkan, Metal, and Dawn (#3968) Context: mono/skia#236 Context: #4545 Tracking: #4555 Expose Skia's next-generation Graphite GPU backend through additive SkiaSharp APIs. The new surface follows Graphite's explicit record, snap, insert, and submit model while leaving the existing Ganesh APIs and behavior unchanged. Add managed wrappers for contexts, recorders, recordings, backend textures, texture information, and Vulkan, Metal, and Dawn backend contexts. Support wrapping caller-owned textures with release callbacks and provide SKGraphiteImageCache as the default image-provider strategy for uploading and reusing raster images. Enable Graphite in the native builds for supported desktop and mobile platforms. WebAssembly uses Dawn's emdawnwebgpu port so Graphite can render through browser WebGPU. Readback uses the existing backend-neutral asynchronous API because shipping Graphite surfaces do not support synchronous ReadPixels. Cover the new surface with API tests, backend-context and release callback tests, and a cross-backend visual regression matrix with platform-specific goldens. The iOS simulator's Graphite/Metal gradient limitation remains isolated to that environment and is tracked separately. Co-authored-by: Ramez Ragaa <[email protected]> Co-authored-by: Matthew Leibowitz <[email protected]> Co-authored-by: Claude Opus 4.8 (1M context) <[email protected]> Co-authored-by: Copilot App <[email protected]>
1 parent 2e59531 commit 3047f3a

64 files changed

Lines changed: 11866 additions & 7720 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎binding/IncludeNativeAssets.SkiaSharp.targets‎

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,13 +87,45 @@
8787

8888
<PropertyGroup Condition="('$(UsingMicrosoftNETSdkBlazorWebAssembly)' == 'true' or '$(UsingMicrosoftNETSdkWebAssembly)' == 'true')">
8989
<WasmBuildNative Condition="'$(WasmBuildNative)' == ''">true</WasmBuildNative>
90-
<EmccExtraLDFlags>$(EmccExtraLDFlags) -s USE_WEBGL2=1</EmccExtraLDFlags>
90+
<!--
91+
net8.x (emsdk 3.1.34) uses the built-in `-sUSE_WEBGPU=1`; net9.0+ uses
92+
Dawn's emdawnwebgpu port. Discriminate on $(TargetFramework) directly —
93+
$(TargetFrameworkVersion) is populated by a later SDK import so it's still
94+
empty here (hence the ItemGroup below carries its own != '' guard).
95+
-->
96+
<_SkiaSharpUseEmdawnwebgpu Condition="'$(_SkiaSharpUseEmdawnwebgpu)' == '' and '$(TargetFramework)' != '' and !$(TargetFramework.StartsWith('net8.'))">true</_SkiaSharpUseEmdawnwebgpu>
97+
<!-- Normalize to forward slashes: emcc parses `-isystem <abs-path>` as one
98+
shell arg and swallows Windows backslashes. The port is staged into the
99+
WASM native output next to the libSkiaSharp*.a files. -->
100+
<_SkiaSharpEmdawnwebgpuPkg>$([System.String]::Copy('$(MSBuildThisFileDirectory)..\output\native\wasm\emdawnwebgpu_pkg').Replace('\','/'))</_SkiaSharpEmdawnwebgpuPkg>
101+
<!--
102+
Inline the port's inputs rather than emcc's ports mechanism: that
103+
compiles into emcc's cache (needs FROZEN_CACHE=0), and the .NET WASM SDK's
104+
FROZEN_CACHE=1 + parallel compile races on sysroot_install.stamp. Passing
105+
the include paths, JS libraries, and webgpu.cpp directly replicates the
106+
port without touching the cache; -sDEFAULT_TO_CXX=1 keeps LINK_AS_CXX on.
107+
-->
108+
<_SkiaSharpEmdawnwebgpuFlags Condition="'$(_SkiaSharpUseEmdawnwebgpu)' == 'true'">-s DEFAULT_TO_CXX=1 -isystem $(_SkiaSharpEmdawnwebgpuPkg)/webgpu/include -isystem $(_SkiaSharpEmdawnwebgpuPkg)/webgpu_cpp/include --js-library=$(_SkiaSharpEmdawnwebgpuPkg)/webgpu/src/library_webgpu_enum_tables.js --js-library=$(_SkiaSharpEmdawnwebgpuPkg)/webgpu/src/library_webgpu_generated_struct_info.js --js-library=$(_SkiaSharpEmdawnwebgpuPkg)/webgpu/src/library_webgpu_generated_sig_info.js --js-library=$(_SkiaSharpEmdawnwebgpuPkg)/webgpu/src/library_webgpu.js -std=c++20 $(_SkiaSharpEmdawnwebgpuPkg)/webgpu/src/webgpu.cpp</_SkiaSharpEmdawnwebgpuFlags>
109+
<EmccExtraLDFlags Condition="'$(_SkiaSharpUseEmdawnwebgpu)' == 'true'">$(EmccExtraLDFlags) -s USE_WEBGL2=1 $(_SkiaSharpEmdawnwebgpuFlags)</EmccExtraLDFlags>
110+
<EmccExtraLDFlags Condition="'$(_SkiaSharpUseEmdawnwebgpu)' != 'true'">$(EmccExtraLDFlags) -s USE_WEBGL2=1 -s USE_WEBGPU=1</EmccExtraLDFlags>
91111
<_SkiaSharpNativeBinaryType Condition="'$(WasmEnableThreads)' == 'True'">mt</_SkiaSharpNativeBinaryType>
92112
<_SkiaSharpNativeBinaryType Condition="'$(WasmEnableThreads)' != 'True'">st</_SkiaSharpNativeBinaryType>
93113
<_SkiaSharpNativeBinaryType Condition="'$(WasmEnableSIMD)' == 'True'">$(_SkiaSharpNativeBinaryType),simd</_SkiaSharpNativeBinaryType>
94114
<_SkiaSharpNativeBinaryType Condition="'$(WasmEnableSIMD)' != 'True'">$(_SkiaSharpNativeBinaryType)</_SkiaSharpNativeBinaryType>
95115
</PropertyGroup>
96116
<ItemGroup Condition="('$(UsingMicrosoftNETSdkBlazorWebAssembly)' == 'true' or '$(UsingMicrosoftNETSdkWebAssembly)' == 'true') and '$(TargetFrameworkVersion)' != ''">
117+
<!-- SkiaSharp's interop reads Module.WebGPU.{mgr*,importJs*}, so export the
118+
WebGPU runtime method. -->
119+
<EmccExportedRuntimeMethod Include="WebGPU" />
120+
<!-- emdawnwebgpu allocates refcounted WGPUTexture handles; the managed
121+
interop releases them via the C ABI's wgpuTextureRelease, so export it
122+
as Module._wgpuTextureRelease. -->
123+
<EmccExportedFunction Include="_wgpuTextureRelease" Condition="'$(_SkiaSharpUseEmdawnwebgpu)' == 'true'" />
124+
<!-- Skia's DawnBackendContext needs a real WGPUInstance* — the emdawnwebgpu
125+
port's wgpuCreateInstance returns one. Export it as Module._wgpuCreateInstance
126+
so the managed WebGPU shim can call it instead of falling back to a fake
127+
handle (a bogus pointer would hang inside wgpu::Instance::AddRef). -->
128+
<EmccExportedFunction Include="_wgpuCreateInstance" Condition="'$(_SkiaSharpUseEmdawnwebgpu)' == 'true'" />
97129
<NativeFileReference Include="$(MSBuildThisFileDirectory)..\output\native\wasm\libSkiaSharp.a\3.1.34\$(_SkiaSharpNativeBinaryType)\*.a" Condition="$([MSBuild]::VersionEquals($(TargetFrameworkVersion), '8.0'))" />
98130
<NativeFileReference Include="$(MSBuildThisFileDirectory)..\output\native\wasm\libSkiaSharp.a\3.1.56\$(_SkiaSharpNativeBinaryType)\*.a" Condition="$([MSBuild]::VersionGreaterThanOrEquals($(TargetFrameworkVersion), '9.0')) and $([MSBuild]::VersionLessThan($(TargetFrameworkVersion), '11.0'))" />
99131
<NativeFileReference Include="$(MSBuildThisFileDirectory)..\output\native\wasm\libSkiaSharp.a\5.0.6\$(_SkiaSharpNativeBinaryType)\*.a" Condition="$([MSBuild]::VersionGreaterThanOrEquals($(TargetFrameworkVersion), '11.0'))" />

‎binding/SkiaSharp.NativeAssets.WebAssembly/SkiaSharp.NativeAssets.WebAssembly.csproj‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,14 @@
88
<PackageFile Include="..\..\output\native\wasm\**\libSkiaSharp*.a" PackagePath="buildTransitive\netstandard1.0" />
99
<PackageFile Include="buildTransitive\SkiaSharp.props" PackagePath="buildTransitive\netstandard1.0\$(PackageId).props" />
1010
<PackageFile Include="buildTransitive\SkiaSharp.targets" PackagePath="buildTransitive\netstandard1.0\$(PackageId).targets" />
11+
<!--
12+
Ship Dawn's emdawnwebgpu port next to the static libs; consumers on
13+
net9.0+ forward it to emcc at link time via SkiaSharp.targets. The port is
14+
produced by the WASM native build (staged into
15+
output/native/wasm/emdawnwebgpu_pkg by the externals-emdawnwebgpu task),
16+
so it's packed straight from the native output like the libSkiaSharp*.a
17+
files.
18+
-->
19+
<PackageFile Include="..\..\output\native\wasm\emdawnwebgpu_pkg\**\*" PackagePath="buildTransitive\netstandard1.0\emdawnwebgpu_pkg" />
1120
</ItemGroup>
1221
</Project>

‎binding/SkiaSharp.NativeAssets.WebAssembly/buildTransitive/SkiaSharp.targets‎

Lines changed: 38 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,38 @@
1111
</ItemGroup>
1212
<ItemGroup Condition="'$(IsUnoHead)' == 'True' and '$(UnoRuntimeIdentifier)' == 'WebAssembly' and '$(ShouldIncludeNativeSkiaSharp)' == 'True'">
1313
<WasmShellEmccExportedRuntimeMethod Include="GL" />
14+
<WasmShellEmccExportedRuntimeMethod Include="WebGPU" />
1415
<!-- Include the emscripten_gl* symbols -->
1516
<WasmShellExtraEmccFlags Include="-s USE_WEBGL2=1"/>
1617
<!-- Enable GLCtx when threading is available -->
1718
<WasmShellExtraEmccFlags Condition="'$(WasmShellEnableThreads)'=='True'" Include="-s OFFSCREEN_FRAMEBUFFER=1" />
19+
<WasmShellExtraEmccFlags Include="-s USE_WEBGPU=1"/>
1820
</ItemGroup>
1921

2022
<!-- If this is a .NET web assembly app -->
2123
<PropertyGroup Condition="('$(UsingMicrosoftNETSdkBlazorWebAssembly)' == 'true' or '$(UsingMicrosoftNETSdkWebAssembly)' == 'true') and '$(ShouldIncludeNativeSkiaSharp)' == 'True'">
2224
<WasmBuildNative Condition="'$(WasmBuildNative)' == ''">true</WasmBuildNative>
23-
<EmccExtraLDFlags>$(EmccExtraLDFlags) -s USE_WEBGL2=1</EmccExtraLDFlags>
25+
<!--
26+
net8.x (emsdk 3.1.34) uses the built-in `-sUSE_WEBGPU=1`; net9.0+ uses
27+
Dawn's emdawnwebgpu port, which ships in this NuGet at
28+
buildTransitive/netstandard1.0/emdawnwebgpu_pkg/. Discriminate on
29+
$(TargetFramework) directly — $(TargetFrameworkVersion) is populated by a
30+
later SDK import so it's still empty here.
31+
-->
32+
<_SkiaSharpUseEmdawnwebgpu Condition="'$(_SkiaSharpUseEmdawnwebgpu)' == '' and '$(TargetFramework)' != '' and !$(TargetFramework.StartsWith('net8.'))">true</_SkiaSharpUseEmdawnwebgpu>
33+
<!-- Normalize to forward slashes: emcc parses `-isystem <abs-path>` as one
34+
shell arg and swallows Windows backslashes. -->
35+
<_SkiaSharpEmdawnwebgpuPkg>$([System.String]::Copy('$(MSBuildThisFileDirectory)emdawnwebgpu_pkg').Replace('\','/'))</_SkiaSharpEmdawnwebgpuPkg>
36+
<!--
37+
Inline the port's inputs rather than emcc's ports mechanism: that
38+
compiles into emcc's cache (needs FROZEN_CACHE=0), and the .NET WASM SDK's
39+
FROZEN_CACHE=1 + parallel compile races on sysroot_install.stamp. Passing
40+
the include paths, JS libraries, and webgpu.cpp directly replicates the
41+
port without touching the cache; -sDEFAULT_TO_CXX=1 keeps LINK_AS_CXX on.
42+
-->
43+
<_SkiaSharpEmdawnwebgpuFlags Condition="'$(_SkiaSharpUseEmdawnwebgpu)' == 'true'">-s DEFAULT_TO_CXX=1 -isystem $(_SkiaSharpEmdawnwebgpuPkg)/webgpu/include -isystem $(_SkiaSharpEmdawnwebgpuPkg)/webgpu_cpp/include --js-library=$(_SkiaSharpEmdawnwebgpuPkg)/webgpu/src/library_webgpu_enum_tables.js --js-library=$(_SkiaSharpEmdawnwebgpuPkg)/webgpu/src/library_webgpu_generated_struct_info.js --js-library=$(_SkiaSharpEmdawnwebgpuPkg)/webgpu/src/library_webgpu_generated_sig_info.js --js-library=$(_SkiaSharpEmdawnwebgpuPkg)/webgpu/src/library_webgpu.js -std=c++20 $(_SkiaSharpEmdawnwebgpuPkg)/webgpu/src/webgpu.cpp</_SkiaSharpEmdawnwebgpuFlags>
44+
<EmccExtraLDFlags Condition="'$(_SkiaSharpUseEmdawnwebgpu)' == 'true'">$(EmccExtraLDFlags) -s USE_WEBGL2=1 $(_SkiaSharpEmdawnwebgpuFlags)</EmccExtraLDFlags>
45+
<EmccExtraLDFlags Condition="'$(_SkiaSharpUseEmdawnwebgpu)' != 'true'">$(EmccExtraLDFlags) -s USE_WEBGL2=1 -s USE_WEBGPU=1</EmccExtraLDFlags>
2446
<!-- Pick the threading type: 'mt' for multi-threading or 'st' for single-threading -->
2547
<_SkiaSharpNativeBinaryType Condition="'$(WasmEnableThreads)' == 'True'">mt</_SkiaSharpNativeBinaryType>
2648
<_SkiaSharpNativeBinaryType Condition="'$(WasmEnableThreads)' != 'True'">st</_SkiaSharpNativeBinaryType>
@@ -29,12 +51,22 @@
2951
<_SkiaSharpNativeBinaryType Condition="'$(WasmEnableSIMD)' != 'True'">$(_SkiaSharpNativeBinaryType)</_SkiaSharpNativeBinaryType>
3052
</PropertyGroup>
3153
<ItemGroup Condition="('$(UsingMicrosoftNETSdkBlazorWebAssembly)' == 'true' or '$(UsingMicrosoftNETSdkWebAssembly)' == 'true') and '$(TargetFrameworkVersion)' != '' and '$(ShouldIncludeNativeSkiaSharp)' == 'True'">
54+
<!-- SkiaSharp's interop reads Module.WebGPU.{mgr*,importJs*}, so export the
55+
WebGPU runtime method. -->
56+
<EmccExportedRuntimeMethod Include="WebGPU" />
57+
<!-- emdawnwebgpu allocates refcounted WGPUTexture handles; the managed
58+
interop releases them via the C ABI's wgpuTextureRelease, so export it
59+
as Module._wgpuTextureRelease. -->
60+
<EmccExportedFunction Include="_wgpuTextureRelease" Condition="'$(_SkiaSharpUseEmdawnwebgpu)' == 'true'" />
61+
<!-- Skia's DawnBackendContext needs a real WGPUInstance* — the emdawnwebgpu
62+
port's wgpuCreateInstance returns one. Export it as Module._wgpuCreateInstance
63+
so the managed WebGPU shim can call it instead of falling back to a fake
64+
handle (a bogus pointer would hang inside wgpu::Instance::AddRef). -->
65+
<EmccExportedFunction Include="_wgpuCreateInstance" Condition="'$(_SkiaSharpUseEmdawnwebgpu)' == 'true'" />
3266
<!-- net8.0 -->
33-
<NativeFileReference Include="$(SkiaSharpStaticLibraryPath)\3.1.34\$(_SkiaSharpNativeBinaryType)\*.a" Condition="$([MSBuild]::VersionEquals($(TargetFrameworkVersion), '8.0'))" />
34-
<!-- net9.0 - net10.0 -->
35-
<NativeFileReference Include="$(SkiaSharpStaticLibraryPath)\3.1.56\$(_SkiaSharpNativeBinaryType)\*.a" Condition="$([MSBuild]::VersionGreaterThanOrEquals($(TargetFrameworkVersion), '9.0')) and $([MSBuild]::VersionLessThan($(TargetFrameworkVersion), '11.0'))" />
36-
<!-- net11.0+ -->
37-
<NativeFileReference Include="$(SkiaSharpStaticLibraryPath)\5.0.6\$(_SkiaSharpNativeBinaryType)\*.a" Condition="$([MSBuild]::VersionGreaterThanOrEquals($(TargetFrameworkVersion), '11.0'))" />
67+
<NativeFileReference Include="$(SkiaSharpStaticLibraryPath)/3.1.34/$(_SkiaSharpNativeBinaryType)/*.a" Condition="$([MSBuild]::VersionEquals($(TargetFrameworkVersion), '8.0'))" />
68+
<!-- net9.0+ -->
69+
<NativeFileReference Include="$(SkiaSharpStaticLibraryPath)/3.1.56/$(_SkiaSharpNativeBinaryType)/*.a" Condition="$([MSBuild]::VersionGreaterThanOrEquals($(TargetFrameworkVersion), '9.0'))" />
3870
</ItemGroup>
3971

4072
<!-- Error when targeting unsupported WASM TFMs -->

‎binding/SkiaSharp/DelegateProxies.cs‎

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@ namespace SkiaSharp
2424

2525
public delegate IntPtr GRVkGetProcedureAddressDelegate (string name, IntPtr instance, IntPtr device);
2626

27+
public delegate IntPtr SKGraphiteVkGetProcedureAddressDelegate (string name, IntPtr instance, IntPtr device);
28+
29+
public delegate void SKGraphiteReleaseDelegate ();
30+
2731
public delegate void SKGlyphPathDelegate (SKPath path, SKMatrix matrix);
2832

2933
internal static unsafe partial class DelegateProxies
@@ -98,6 +102,38 @@ private static partial IntPtr GRVkGetProcProxyImplementation (void* ctx, void* n
98102
return del.Invoke (Marshal.PtrToStringAnsi ((IntPtr)name), instance, device);
99103
}
100104

105+
private static partial IntPtr SKGraphiteVkGetProxyImplementation (void* userData, void* name, IntPtr instance, IntPtr device)
106+
{
107+
var del = Get<SKGraphiteVkGetProcedureAddressDelegate> ((IntPtr)userData, out _);
108+
109+
return del.Invoke (Marshal.PtrToStringAnsi ((IntPtr)name), instance, device);
110+
}
111+
112+
private static partial void SKGraphiteReleaseProxyImplementation (void* releaseContext)
113+
{
114+
var del = Get<SKGraphiteReleaseDelegate> ((IntPtr)releaseContext, out var gch);
115+
try {
116+
del.Invoke ();
117+
} finally {
118+
gch.Free ();
119+
}
120+
}
121+
122+
private static partial IntPtr SKGraphiteImageProviderProxyImplementation (void* userData, IntPtr recorder, IntPtr image, bool mipmapped)
123+
{
124+
// userData is a GCHandle pinned by SKGraphiteContext.CreateRecorder; the
125+
// recorder keeps it alive for its own lifetime and frees it in DisposeNative.
126+
// Returning IntPtr.Zero drops the draw, same as if no callback were installed.
127+
var del = Get<SKGraphiteFindOrCreateImageProxy> ((IntPtr)userData, out _);
128+
try {
129+
return del.Invoke (recorder, image, mipmapped);
130+
} catch {
131+
// Never throw across the FFI boundary. Drop the draw on any
132+
// managed exception inside FindOrCreate.
133+
return IntPtr.Zero;
134+
}
135+
}
136+
101137
private static partial void SKGlyphPathProxyImplementation (IntPtr pathOrNull, SKMatrix* matrix, void* context)
102138
{
103139
var del = Get<SKGlyphPathDelegate> ((IntPtr)context, out _);
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
#nullable disable
2+
3+
using System;
4+
5+
namespace SkiaSharp
6+
{
7+
public unsafe class SKGraphiteBackendTexture : SKObject
8+
{
9+
internal SKGraphiteBackendTexture (IntPtr handle, bool owns)
10+
: base (handle, owns)
11+
{
12+
}
13+
14+
public static SKGraphiteBackendTexture CreateVulkan (
15+
int width, int height,
16+
SKGraphiteVkTextureInfo info,
17+
int imageLayout,
18+
uint queueFamilyIndex,
19+
IntPtr vkImage)
20+
{
21+
if (vkImage == IntPtr.Zero)
22+
throw new ArgumentNullException (nameof (vkImage));
23+
if (width <= 0)
24+
throw new ArgumentOutOfRangeException (nameof (width), width, "Must be positive.");
25+
if (height <= 0)
26+
throw new ArgumentOutOfRangeException (nameof (height), height, "Must be positive.");
27+
IntPtr handle = SkiaApi.sk_graphite_vk_backend_texture_new (
28+
width, height, &info, imageLayout, queueFamilyIndex, (void*)vkImage);
29+
return handle == IntPtr.Zero ? null : new SKGraphiteBackendTexture (handle, true);
30+
}
31+
32+
public static SKGraphiteBackendTexture CreateMetal (
33+
int width, int height,
34+
IntPtr mtlTexture)
35+
{
36+
if (mtlTexture == IntPtr.Zero)
37+
throw new ArgumentNullException (nameof (mtlTexture));
38+
if (width <= 0)
39+
throw new ArgumentOutOfRangeException (nameof (width), width, "Must be positive.");
40+
if (height <= 0)
41+
throw new ArgumentOutOfRangeException (nameof (height), height, "Must be positive.");
42+
IntPtr handle = SkiaApi.sk_graphite_mtl_backend_texture_new (
43+
width, height, (void*)mtlTexture);
44+
return handle == IntPtr.Zero ? null : new SKGraphiteBackendTexture (handle, true);
45+
}
46+
47+
public static SKGraphiteBackendTexture CreateDawn (IntPtr wgpuTexture)
48+
{
49+
if (wgpuTexture == IntPtr.Zero)
50+
throw new ArgumentNullException (nameof (wgpuTexture));
51+
IntPtr handle = SkiaApi.sk_graphite_dawn_backend_texture_new ((void*)wgpuTexture);
52+
return handle == IntPtr.Zero ? null : new SKGraphiteBackendTexture (handle, true);
53+
}
54+
55+
protected override void DisposeNative () =>
56+
SkiaApi.sk_graphite_backend_texture_delete (Handle);
57+
58+
public bool IsValid => SkiaApi.sk_graphite_backend_texture_is_valid (Handle);
59+
public SKGraphiteBackend Backend => SkiaApi.sk_graphite_backend_texture_get_backend (Handle);
60+
61+
public SKSizeI Dimensions {
62+
get {
63+
int w, h;
64+
SkiaApi.sk_graphite_backend_texture_get_dimensions (Handle, &w, &h);
65+
return new SKSizeI (w, h);
66+
}
67+
}
68+
}
69+
}

0 commit comments

Comments
 (0)