Add SKMesh API for custom vertex mesh drawing with SkSL shaders#3779
Add SKMesh API for custom vertex mesh drawing with SkSL shaders#3779mattleibow wants to merge 2 commits into
Conversation
📦 Try the packages from this PRWarning Do not run these scripts without first reviewing the code in this PR. Step 1 — Download the packages bash / macOS / Linux: curl -fsSL https://raw.githubusercontent.com/mono/SkiaSharp/main/scripts/get-skiasharp-pr.sh | bash -s -- 3779PowerShell / Windows: iex "& { $(irm https://raw.githubusercontent.com/mono/SkiaSharp/main/scripts/get-skiasharp-pr.ps1) } 3779"Step 2 — Add the local NuGet source dotnet nuget add source ~/.skiasharp/hives/pr-3779/packages --name skiasharp-pr-3779More options
Or download manually from Azure Pipelines — look for the Remove the source when you're done: dotnet nuget remove source skiasharp-pr-3779 |
5c438f0 to
59d5240
Compare
|
📖 Documentation Preview The documentation for this PR has been deployed and is available at: 🔗 View Staging Site This preview will be updated automatically when you push new commits to this PR. This comment is automatically updated by the documentation staging workflow. |
59d5240 to
bb0a20e
Compare
Implements custom vertex mesh drawing with SkSL shaders, mirroring the SKRuntimeEffect API pattern with Create/Build factories, Uniforms/Children properties, and a builder class. New types: SKMeshSpecification, SKMesh, SKMeshVertexBuffer, SKMeshIndexBuffer, SKMeshBuilder, SKMeshMode, SKMeshSpecificationAttributeType/VaryingType. C API uses a builder pattern (sk_mesh_new + setters + sk_mesh_validate) with all P/Invoke calls ≤4 params for WASM interpreter compatibility. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
- SKMeshSpecification.Build() now returns SKMeshBuilder (was SKMeshSpecification), matching SKRuntimeEffect.BuildShader/BuildColorFilter/BuildBlender pattern - SKMeshBuilder.Dispose() now disposes Specification (builder owns it), matching SKRuntimeEffectBuilder.Dispose() which disposes Effect - Nullable annotations on Create/ToMesh/ToMeshIndexed out parameters and nullable SKData/SKRuntimeEffect params — eliminates all CS8604/CS8625 warnings - Add BuildReturnsMeshBuilder test to cover the new return type - Update builder tests to not double-using spec (builder owns it) - Rewrite MeshSample to use Build() factory, cache builder across frames, and animate with a cycling color uniform over an indexed quad mesh - Rebase onto origin/main (skia submodule rebased onto origin/skiasharp) Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
bb0a20e to
d618da5
Compare
|
We took this PR for a spin on a real workload and it held up really well — sharing results and a few findings. Setup: managed Results: mesh build cost is ~0.2 ms per camera change for all 17 meshes, and per-frame CPU drops to near zero versus ~26 ms/frame for our equivalent A few findings from getting there: 1. Default paint renders meshes black — including
|
Fixes #3777
Summary
Adds the
SKMeshAPI to SkiaSharp, wrapping Skia'sSkMeshfor custom vertex mesh drawing with SkSL shaders. This is a major new rendering capability that enables particle systems, deformable geometry, custom terrain rendering, and advanced visual effects.New API surface
SKMeshSpecificationSKMeshSKMeshVertexBuffer/SKMeshIndexBufferSKCanvas.DrawMesh()SKBlender)SKMeshModeSKMeshSpecificationAttributeType/SKMeshSpecificationVaryingTypeArchitecture
ISKNonVirtualReferenceCounted(wrapsSkNVRefCnt)DisposeNativecallssk_mesh_deleteISKReferenceCounted(wrapsSkRefCnt, uses genericsk_refcnt_safe_unref)Changes
Skia submodule (companion PR: mono/skia#203)
include/c/sk_mesh.h— 15 C API function declarationssrc/c/sk_mesh.cpp— implementationsinclude/c/sk_types.h— opaque types, 3 enums, 2 structssk_canvas.h/cpp—sk_canvas_draw_meshsk_types_priv.h— DEF_CLASS_MAP + As/To helpers for nested buffer typesSkiaKeeper.c— symbol retention for Xcode linkinggn/core.gni— build entriesSkiaSharp
binding/SkiaSharp/SKMeshSpecification.cs— spec wrapper withMake()factoriesbinding/SkiaSharp/SKMesh.cs— mesh wrapper withMake()andMakeIndexed()factoriesbinding/SkiaSharp/SKMeshVertexBuffer.cs/SKMeshIndexBuffer.cs— buffer wrappersbinding/SkiaSharp/SKCanvas.cs—DrawMesh()overloadsbinding/libSkiaSharp.json— type mappingstests/Tests/SkiaSharp/SKMeshTest.cs— 18 testssamples/Gallery/Shared/Samples/MeshSample.cs— gallery sample with 4 visual modesTests
18 tests covering specification creation, buffer creation, mesh creation (indexed and non-indexed), drawing, validation, error handling, and triangle strip mode. All pass on macOS arm64.
Known issues
SKMeshSpecification.Make()exhausts the WASM heap during SkSL mesh shader compilation. The mesh API works correctly on all native platforms. This appears to be the Skia SkSL compiler's memory usage exceeding what the WASM GC can handle. The gallery sample currently crashes on Blazor WASM — needs investigation into whether initial heap size or GC tuning can resolve this.SKMeshSpecificationAttributeNative*) had to be mapped toIntPtrinlibSkiaSharp.jsonfor WASM interpreter compatibility. This is a workaround for a mono WASM interpreter limitation with complex P/Invoke signatures.