Metal.NET 1.0.8

There is a newer version of this package available.
See the version list below for details.
dotnet add package Metal.NET --version 1.0.8
                    
NuGet\Install-Package Metal.NET -Version 1.0.8
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Metal.NET" Version="1.0.8" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Metal.NET" Version="1.0.8" />
                    
Directory.Packages.props
<PackageReference Include="Metal.NET" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Metal.NET --version 1.0.8
                    
#r "nuget: Metal.NET, 1.0.8"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package Metal.NET@1.0.8
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Metal.NET&version=1.0.8
                    
Install as a Cake Addin
#tool nuget:?package=Metal.NET&version=1.0.8
                    
Install as a Cake Tool

Metal.NET

NuGet Version

C# bindings for Apple's Metal graphics API, auto-generated from metal-cpp headers.

Project Structure

Metal.NET.slnx
├── Metal.NET/                            ← Binding library (targets .NET 10, macOS 15+)
│   ├── Common/
│   │   ├── NativeObject.cs               ← INativeObject<TSelf> interface + abstract base wrapper
│   │   ├── ObjectiveCRuntime.cs          ← P/Invoke to libobjc.dylib (objc_msgSend)
│   │   ├── Selector.cs                   ← ObjC selector with implicit string conversion
│   │   ├── Bool8.cs                      ← ObjC BOOL mapped to a single byte
│   │   └── MTLStructs.cs                ← Blittable structs (MTLOrigin, MTLSize, etc.)
│   ├── Foundation/                       ← Hand-written Foundation types
│   │   ├── NSString.cs                   ← Bidirectional string conversion
│   │   ├── NSError.cs                    ← Error wrapper with LocalizedDescription
│   │   ├── NSArray.cs                    ← NSArray ↔ T[] conversion utilities
│   │   ├── NSURL.cs                      ← File URL creation
│   │   └── NSAutoreleasePool.cs          ← Autorelease pool management
│   ├── Metal/                            ← Auto-generated Metal API (352 files)
│   ├── MetalFX/                          ← Auto-generated MetalFX (18 files)
│   └── QuartzCore/                       ← Auto-generated QuartzCore (2 files)
│
└── Metal.NET.Generator/                  ← Offline code generator
    ├── Program.cs                        ← Entry point
    ├── Generator.cs                      ← Orchestrator (parser → emitter pipeline)
    ├── CppParser.cs                      ← Regex-based metal-cpp header parser
    ├── CSharpEmitter.cs                  ← C# source code emitter
    ├── TypeMapper.cs                     ← C++ → C# type mapping and naming
    ├── GeneratorContext.cs               ← Shared state between parser and emitter
    ├── Models.cs                         ← Data models (EnumDef, ClassDef, MethodInfo, etc.)
    └── metal-cpp/                        ← metal-cpp headers (generation source)

Memory Management

Owned vs Borrowed References

Every NativeObject wrapper tracks whether it owns its native reference via bool ownsReference:

  • Owned (true) — the wrapper sends release on Dispose() or GC finalization. Used for objects returned by alloc, new, copy, or mutableCopy selectors.
  • Borrowed (false) — the wrapper never sends release. Used for property getters, objectAtIndex:, and out NSError parameters.
// Owned — selector begins with "new", caller must release
using MTLLibrary library = device.NewDefaultLibrary();

// Borrowed — property getter, retained by the parent object
MTLDevice device = commandQueue.Device;

GC-Safe Dispose

NativeObject implements the full dispose pattern with a finalizer. Release() uses Interlocked.Exchange to guarantee exactly-once semantics, making double-Dispose() and GC finalization safe:

public abstract class NativeObject(nint nativePtr, bool ownsReference) : IDisposable
{
    ~NativeObject() => Release();

    public void Dispose() { Release(); GC.SuppressFinalize(this); }

    private void Release()
    {
        if (Interlocked.Exchange(ref disposed, 1) is not 0) return;
        if (OwnsReference) ObjectiveCRuntime.Release(NativePtr);
    }
}

Updating Bindings

  1. Download the latest metal-cpp archive
  2. Replace Metal.NET.Generator/metal-cpp/ contents
  3. Run the generator:
dotnet run --project Metal.NET.Generator
  1. Build:
dotnet build Metal.NET

Disclaimer

This library was built with AI assistance. It has undergone preliminary testing to verify basic usability, but has not been exhaustively validated in production scenarios. If you plan to use it in a real project, please ensure it meets your requirements through thorough testing.

Alternatives

If you are looking for more mature or alternative Metal bindings for .NET, consider:

  • SharpMetal — A community-maintained C# Metal binding library.
  • .NET net-macos / net-ios TFM — Apple platform targets shipped with .NET that include official Metal API bindings via dotnet/macios.

Trademarks

"Metal" is a trademark of Apple Inc. This project is not affiliated with or endorsed by Apple Inc.

Third-Party Notices

See THIRD-PARTY-NOTICES.md for details on third-party components used in this project.

Product Compatible and additional computed target framework versions.
.NET net10.0 is compatible.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • net10.0

    • No dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.1.0 42 2/25/2026
1.0.9 40 2/25/2026
1.0.8 41 2/25/2026
1.0.7 43 2/25/2026
1.0.6 52 2/25/2026
1.0.5 56 2/24/2026
1.0.4 55 2/24/2026
1.0.3 57 2/21/2026
1.0.2 68 2/20/2026
1.0.1 62 2/19/2026
1.0.0 59 2/19/2026