Skip to content

Add GRVkMemoryAllocator managed binding (#4567) - #4585

Open
ramezgerges wants to merge 6 commits into
mono:mainfrom
ramezgerges:dev/issue-4567-vulkan-memory-allocator
Open

ramezgerges wants to merge 6 commits into
mono:mainfrom
ramezgerges:dev/issue-4567-vulkan-memory-allocator

Conversation

@ramezgerges

@ramezgerges ramezgerges commented Jul 30, 2026 •

Copy link
Copy Markdown
Contributor

Description

Wraps the new gr_vk_memory_allocator_make_default C API so managed callers can supply a Vulkan memory allocator to both Ganesh and Graphite.

Motivation: Skia m152 removes Ganesh's internal SK_USE_VMA fallback (upstream commit 5e183e5aea), after which any null allocator slot causes gr_direct_context_make_vulkan to fail. Graphite hits the same failure today via its own null check. Without a managed way to construct an allocator, SkiaSharp callers cannot fill the slot even though the C struct already exposes it.

Related issues

Fixes #4567

Required skia PR

Requires mono/skia#307

Areas affected

  • Managed API (binding/)
  • Native / C API (externals/skia/src/c, include/c)
  • Generated P/Invoke bindings
  • Native dependency or Skia update (libpng, HarfBuzz, FreeType, zlib, milestone bump, …)
  • Views & integrations (MAUI, Uno, WPF, WinUI, Blazor, …)
  • Rendering output / visual behavior
  • Performance
  • Tests
  • Build, packaging, or CI
  • Documentation or samples

Changes

Public API

ABI-stable additive changes only.

// new type
public unsafe class GRVkMemoryAllocator : SKObject
{
    // Overloads rather than default parameters (default parameters are not ABI-safe).
    public static GRVkMemoryAllocator CreateDefault(
        IntPtr vkInstance, IntPtr vkPhysicalDevice, IntPtr vkDevice, IntPtr vkQueue,
        uint maxApiVersion, GRVkGetProcedureAddressDelegate getProcedureAddress);
    public static GRVkMemoryAllocator CreateDefault(
        IntPtr vkInstance, IntPtr vkPhysicalDevice, IntPtr vkDevice, IntPtr vkQueue,
        uint maxApiVersion, GRVkGetProcedureAddressDelegate getProcedureAddress,
        bool threadSafe);
    public static GRVkMemoryAllocator CreateDefault(
        IntPtr vkInstance, IntPtr vkPhysicalDevice, IntPtr vkDevice, IntPtr vkQueue,
        uint maxApiVersion, GRVkGetProcedureAddressDelegate getProcedureAddress,
        bool threadSafe, bool protectedContent);
}

// new properties
public GRVkMemoryAllocator GRVkBackendContext.MemoryAllocator { get; set; }
public GRVkMemoryAllocator SKGraphiteVkBackendContext.MemoryAllocator { get; set; }

Behavior

  • Contexts take an internal sk_ref_sp on the supplied allocator, so callers can Dispose() the managed wrapper immediately after GRContext.CreateVulkan / SKGraphiteContext.CreateVulkan returns.
  • When MemoryAllocator is left null, the native shim continues to auto-create Skia's default VMA-backed allocator (same behavior as before this PR for Graphite; new safety net for Ganesh once m152 lands).
  • vkQueue is required. skgpu::VulkanMemoryAllocators::Make asserts the queue is set even though it does not currently read it; an earlier revision left it null, which aborted every Vulkan-enabled debug build (release CI compiles the assert out, so it never showed). The native side now also rejects a null queue, so invalid input returns null in release builds rather than only aborting in debug ones.
  • GRContext.CreateVulkan keeps the backend context (and so its allocator wrapper) alive with GC.KeepAlive until the native factory has taken its own ref, matching CreateGl.
  • Get-proc delegate lifetime scoped to CreateDefault only — VMA caches resolved Vulkan function pointers, so the GCHandle is released before the method returns.

Testing

New tests/VulkanTests/GRVkMemoryAllocatorTest.cs:

  • CreateDefaultRejectsNullHandlesQueueAndGetProc: argument validation, including the required queue
  • CreateDefaultReturnsAllocator: happy path on a real Vulkan device
  • GaneshContextRetainsAllocatorAfterWrapperDispose: asserts the context raises this allocator's native ref count (Skia would otherwise fall back to its own default allocator unnoticed), then disposes the wrapper and draws + flushes
  • GraphiteContextAcceptsCustomAllocator: the same ref-count check and pass-through on Graphite, plus a Sync submit
  • AllocatorGetProcIsOnlyUsedDuringCreation: the get-proc delegate is not called after CreateDefault returns
  • AllocatorSurvivesGetProcCollection: the allocator stays usable after the delegate is collected
  • ThreadSafeAllocatorCanBeSharedByContexts: one thread-safe allocator shared by several contexts

CI covers the current head on all Vulkan-enabled platforms.

Checklist

🤖 Generated with Claude Code

@github-actions

github-actions Bot commented Jul 30, 2026 •

Copy link
Copy Markdown
Contributor

📦 Try the packages from this PR

Warning

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 -- 4585

PowerShell / Windows:

iex "& { $(irm https://raw.githubusercontent.com/mono/SkiaSharp/main/scripts/get-skiasharp-pr.ps1) } 4585"

Step 2 — Add the local NuGet source

dotnet nuget add source ~/.skiasharp/hives/pr-4585/packages --name skiasharp-pr-4585
More options
Option Description
--successful-only / -SuccessfulOnly Only use successful builds
--force / -Force Overwrite previously downloaded packages
--list / -List List available artifacts without downloading
--build-id ID / -BuildId ID Download from a specific build

Or download manually from Azure Pipelines — look for the nuget artifact on the build for this PR.

Remove the source when you're done:

dotnet nuget remove source skiasharp-pr-4585

@mattleibow

mattleibow commented Jul 31, 2026 •

Copy link
Copy Markdown
Contributor

/azp run

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines will not run the associated pipelines, because the pull request was updated after the run command was issued. Review the pull request again and issue a new run command.

1 similar comment
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines will not run the associated pipelines, because the pull request was updated after the run command was issued. Review the pull request again and issue a new run command.

@ramezgerges
ramezgerges force-pushed the dev/issue-4567-vulkan-memory-allocator branch from 3c0bebf to 0936f91 Compare August 3, 2026 17:18
@ramezgerges

Copy link
Copy Markdown
Contributor Author

Rebased onto the current main — includes #4612 (deterministic bindings regen). The two failed /azp run attempts happened while I was force-pushing; the branch is stable now.

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 1 pipeline(s).

8 similar comments
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 1 pipeline(s).

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 1 pipeline(s).

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 1 pipeline(s).

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 1 pipeline(s).

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 1 pipeline(s).

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 1 pipeline(s).

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 1 pipeline(s).

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 1 pipeline(s).

@github-actions

github-actions Bot commented Aug 4, 2026 •

Copy link
Copy Markdown
Contributor

📦 Artifact size report

Packages from this PR (build 1539635) vs the latest nightly baseline 4.151.0-nightly.124 (observed 2026-08-04).

Total .nupkg size: 569.4 MB → 568.8 MB (−620.2 KB, -0.1%)

Packages

⚠️ marks growth over 500.0 KB or 2%. Changes under 50.0 KB are treated as noise.

Package baseline this PR Δ Δ%
SkiaSharp.NativeAssets.WinUI 109.1 MB 108.3 MB 🟢 −774.9 KB -0.7%
SkiaSharp.NativeAssets.WebAssembly 69.6 MB 69.6 MB 🔴 +64.1 KB +0.1%

+38 package(s) unchanged (< 50.0 KB).

Per-file changes

SkiaSharp.NativeAssets.WinUI

File Size
runtimes/win-x64/native/libGLESv2.pdb 66.6 MB → 66.5 MB (🟢 −144.0 KB)
runtimes/win-arm64/native/libGLESv2.pdb 64.5 MB → 64.4 MB (🟢 −128.0 KB)
runtimes/win-x86/native/libGLESv2.pdb 67.3 MB → 67.2 MB (🟢 −96.0 KB)

Informational only — this never blocks the PR. Native binaries are labelled by os/arch.

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 1 pipeline(s).

6 similar comments
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 1 pipeline(s).

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 1 pipeline(s).

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 1 pipeline(s).

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 1 pipeline(s).

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 1 pipeline(s).

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 1 pipeline(s).

@ramezgerges
ramezgerges force-pushed the dev/issue-4567-vulkan-memory-allocator branch from 0936f91 to 6d759c5 Compare August 4, 2026 17:03
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines will not run the associated pipelines, because the pull request was updated after the run command was issued. Review the pull request again and issue a new run command.

1 similar comment
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines will not run the associated pipelines, because the pull request was updated after the run command was issued. Review the pull request again and issue a new run command.

@ramezgerges
ramezgerges force-pushed the dev/issue-4567-vulkan-memory-allocator branch from 6d759c5 to 9c9ae4c Compare September 14, 2026 15:34
@ramezgerges
ramezgerges force-pushed the dev/issue-4567-vulkan-memory-allocator branch 2 times, most recently from 15915ad to 763cede Compare September 21, 2026 18:07
@ramezgerges
ramezgerges force-pushed the dev/issue-4567-vulkan-memory-allocator branch 2 times, most recently from c610325 to 9fb0ae6 Compare October 13, 2026 16:15
@mattleibow
mattleibow force-pushed the dev/issue-4567-vulkan-memory-allocator branch from 9fb0ae6 to ea3cb18 Compare November 15, 2026 08:00

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

Small PR-description cleanup: the Changes > Public API snippet still shows threadSafe = false and protectedContent = false as default parameters, but the binding exposes overloads instead. Please update the snippet to match the actual API. The docs-issue checkbox is also unchecked; if the API-docs issue was filed, please mark it, otherwise file it before merging.

@github-project-automation github-project-automation Bot moved this to Changes Requested in SkiaSharp Backlog Nov 15, 2026
ramezgerges added a commit to ramezgerges/skia that referenced this pull request Nov 16, 2026
Addresses the review on mono/SkiaSharp#4585.

ToAllocatorBackendContext left fQueue as VK_NULL_HANDLE on the reasoning that
the queue is irrelevant to allocator construction. That is true of what Make
reads: neither VulkanAMDMemoryAllocator::Make nor skgpu::MakeInterface touches
fQueue. But Make opens with SkASSERT(backendContext.fQueue != VK_NULL_HANDLE),
so gr_vk_memory_allocator_make_default aborted on every Vulkan-enabled debug
build even with valid handles. Release CI compiles the assert out and never
saw it.

gr_vk_allocator_default_options_t gains a required fQueue, which is passed
through, and make_default now rejects a null queue alongside the other
handles Make asserts on, so invalid input returns null in release builds too
rather than only aborting in debug ones.

The options struct is new in this PR and unreleased, so adding the field has
no ABI cost.

Not verified locally: no Vulkan-enabled debug build is available here.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
ramezgerges added a commit to ramezgerges/SkiaSharp that referenced this pull request Nov 16, 2026
Addresses the review on mono#4585. Pins the companion mono/skia#307.

skgpu::VulkanMemoryAllocators::Make asserts that the backend context's queue is
set. The C shim left it null, so CreateDefault aborted on every Vulkan-enabled
debug build even with valid handles; release CI compiles the assert out.

All three CreateDefault overloads now take a required vkQueue after vkDevice,
validated like the other handles and passed through to the native options.
This API is new in this PR and unreleased, so the signature change has no
compatibility cost.

GRVkMemoryAllocatorTest covers the null-queue rejection and passes the test
device's graphics queue in every allocator-creating test.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@ramezgerges
ramezgerges force-pushed the dev/issue-4567-vulkan-memory-allocator branch from fd2f9a6 to 97313f7 Compare November 16, 2026 19:24
@mattleibow
mattleibow requested a balanced review from Copilot November 17, 2026 05:07

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.

🟡 Changes recommended

Unresolved critical native integration, ABI compatibility, and allocator-lifetime defects risk missing-entry-point exceptions and native crashes.

4 open issues
What changed in this PR

Adds a managed Vulkan memory allocator wrapper so callers can supply allocators to both Ganesh and Graphite, addressing #4567 alongside the companion native API.

Changes:

  • Adds GRVkMemoryAllocator.CreateDefault overloads and allocator properties on both Vulkan backend contexts.
  • Adds generated native bindings and allocator option mappings.
  • Adds validation, lifetime, delegate-collection, and allocator-sharing tests.
File Description
tests/​VulkanTests/​GRVkMemoryAllocatorTest.cs Adds allocator creation, lifetime, and rendering tests.
tests/​Tests/​ApiTests.cs Accounts for the delegate-bearing allocator options struct.
binding/​SkiaSharp/​GRVkMemoryAllocator.cs Introduces the reference-counted allocator wrapper and factories.
binding/​SkiaSharp/​GRVkBackendContext.cs Passes caller-provided allocators to Ganesh.
binding/​SkiaSharp/​Gpu/​Graphite/​SKGraphiteVkBackendContext.cs Passes caller-provided allocators to Graphite.
binding/​SkiaSharp/​Generated/​SkiaApi_include_c_gr_vk_allocator_h.generated.cs Adds native allocator creation and reference-management bindings.
binding/​SkiaSharp/​Generated/​SKGraphiteVkBackendContextNative.generated.cs Adds the Graphite allocator field.
binding/​SkiaSharp/​Generated/​GRVkAllocatorDefaultOptionsNative.generated.cs Defines the native allocator options layout.
binding/​libSkiaSharp.json Maps native allocator options to the managed interop type.

Review effort: Balanced

Comment thread binding/SkiaSharp/GRVkBackendContext.cs Resolved
ramezgerges and others added 4 commits November 17, 2026 22:58
Wraps the new gr_vk_memory_allocator_make_default C API so managed
callers can supply a Vulkan memory allocator to both Ganesh and
Graphite. Motivation: Skia m152 removes Ganesh's internal VMA fallback,
after which any null allocator slot causes gr_direct_context_make_vulkan
to fail. Graphite hits the same failure today via its own null check.

* new GRVkMemoryAllocator : SKObject, ISKReferenceCounted with
  CreateDefault(vkInstance, vkPhysicalDevice, vkDevice, maxApiVersion,
  getProcedureAddress, threadSafe, protectedContent) — the get-proc
  delegate is only used synchronously during Make, so its GCHandle is
  released before the method returns
* GRVkBackendContext.MemoryAllocator + SKGraphiteVkBackendContext.
  MemoryAllocator setters plumbed through ToNative()
* Context takes its own internal ref via sk_ref_sp; managed wrapper can
  be disposed immediately after CreateVulkan returns

Tests (tests/VulkanTests/GRVkMemoryAllocatorTest.cs) cover:
* argument validation (null instance/phys-device/device/getProc)
* CreateDefault succeeds on a real Vulkan context
* Ganesh: dispose wrapper after CreateVulkan → context keeps working
* Graphite: same pass-through smoke test, plus a Sync submit

Co-Authored-By: Claude Opus 4.7 <[email protected]>
Prove the allocator only uses getProc during creation, survives
collection of the getProc delegate, and that a thread-safe allocator
can be shared by multiple Ganesh contexts.

Co-authored-by: Copilot App <[email protected]>
Addresses the review on mono#4585. Pins the companion mono/skia#307.

skgpu::VulkanMemoryAllocators::Make asserts that the backend context's queue is
set. The C shim left it null, so CreateDefault aborted on every Vulkan-enabled
debug build even with valid handles; release CI compiles the assert out.

All three CreateDefault overloads now take a required vkQueue after vkDevice,
validated like the other handles and passed through to the native options.
This API is new in this PR and unreleased, so the signature change has no
compatibility cost.

GRVkMemoryAllocatorTest covers the null-queue rejection and passes the test
device's graphics queue in every allocator-creating test.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
mattleibow pushed a commit to ramezgerges/skia that referenced this pull request Nov 17, 2026
Addresses the review on mono/SkiaSharp#4585.

ToAllocatorBackendContext left fQueue as VK_NULL_HANDLE on the reasoning that
the queue is irrelevant to allocator construction. That is true of what Make
reads: neither VulkanAMDMemoryAllocator::Make nor skgpu::MakeInterface touches
fQueue. But Make opens with SkASSERT(backendContext.fQueue != VK_NULL_HANDLE),
so gr_vk_memory_allocator_make_default aborted on every Vulkan-enabled debug
build even with valid handles. Release CI compiles the assert out and never
saw it.

gr_vk_allocator_default_options_t gains a required fQueue, which is passed
through, and make_default now rejects a null queue alongside the other
handles Make asserts on, so invalid input returns null in release builds too
rather than only aborting in debug ones.

The options struct is new in this PR and unreleased, so adding the field has
no ABI cost.

Not verified locally: no Vulkan-enabled debug build is available here.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@mattleibow
mattleibow force-pushed the dev/issue-4567-vulkan-memory-allocator branch from 1672de4 to 9c0091b Compare November 17, 2026 20:59
ramezgerges and others added 2 commits November 18, 2026 15:43
…ono#4567)

ToNative() copies raw handles (the memory allocator, the extensions) out of
wrappers that only backendContext references, and neither branch used
backendContext after that. A temporary allocator's finalizer could then
release the native allocator before the factory took its own reference.
Call GC.KeepAlive(backendContext) after the native call in both branches,
as CreateGl does. SKGraphiteContext.CreateVulkan already uses the backend
context after its native call, so it needs no change.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Skia falls back to its own default allocator when none is forwarded, so
the rendering-only assertions passed even if the caller's allocator never
reached the native context. Check that creating the Ganesh and Graphite
contexts raises this allocator's native ref count.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@ramezgerges

Copy link
Copy Markdown
Contributor Author

The Public API snippet in the description now shows the three CreateDefault overloads instead of default parameters, matching the binding. I also refreshed the Behavior and Testing sections for the latest commits.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

Status: Changes Requested

Development

Successfully merging this pull request may close these issues.

[FEATURE] Expose Vulkan memory allocators for Ganesh and Graphite

4 participants