metal.cr
Metal for Crystal
require "metal" provides Metal object wrappers, SDK enums and LibMetal bindings to Metal and the Objective-C runtime. It has no Garnet or window dependency, built-in shader, pixel-format restriction or automatic submission wait.
Device, buffer, texture, library, pipeline, command queue and encoder operations are methods on their corresponding types. Descriptors preserve native configuration. The standalone compute test compiles caller-owned MSL, dispatches a compute kernel and reads its result from a shared buffer; it also creates a non-RGBA texture.
Named wrappers currently cover a subset of Metal. object.send(selector, ReturnType, *args) exposes typed Objective-C messaging for additional selectors; the caller must supply the exact native ABI signature and ownership semantics. LibMetal remains directly accessible. This escape hatch does not mean every Metal operation already has a named Crystal wrapper.
Owned wrappers release their Objective-C reference when collected. close remains available for prompt, idempotent release; native blocks use their corresponding block-release operation. from_native retains a borrowed object, or adopts an existing owned reference with owned: true. Direct native-handle constructors borrow by default. Use Metal.autoreleasepool around native work.
Encoders retain their command wrapper while recording. Typed encoder bindings also attach native resource references to the command object, so they survive collection of Crystal wrappers, including with command_buffer_with_unretained_references. The native command queue keeps committed classic Metal commands alive through execution. Argument encoders attach referenced resources to their argument buffer. These references are retained conservatively until the owning native object is destroyed; rebinding does not immediately remove previous dependencies. Ordinary Metal retention continues to manage descriptors, devices and directly bound resources.
This ownership does not replace GPU synchronization or residency declarations. Raw Objective-C calls, untyped memory, no-copy CPU allocations, and bare GPU addresses cannot convey resource ownership. Metal 4 address-based argument tables and submission lifetimes require a separate implementation and target validation; the automatic submission guarantee above applies to classic CommandBuffer/CommandQueue. Explicit close releases the wrapper's reference and makes that wrapper unusable.
Requires macOS and the system Metal/Foundation/Objective-C libraries. tools/generate_enums.cr derives native enum values from a Metal SDK Clang AST. From the Garnet workspace, run scripts/check-render-api metal. Validated on arm64 macOS; iOS hosting and Intel struct-return dispatch still require target testing.
Regenerating bindings
The generator and ABI-check tooling is written in Crystal. From this shard directory, using a Clang JSON AST produced by importing <Metal/Metal.h>:
crystal run tools/generate_enums.cr -- /path/to/ast.json
crystal run tools/generate_api.cr -- /path/to/ast.json
crystal run tools/check_abi.cr
The API generator emits named methods, native records and tools/coverage.json. The ABI checker uses the installed Apple SDK as the reference. Clang remains necessary to read Objective-C headers and compile the native layout probe; Python is not required.
metal.cr
- 0
- 0
- 0
- 2
- 0
- 1 day ago
- November 30, 2026
MIT License
Wed, 02 Dec 2026 02:09:35 GMT