No-copy buffers and host imports now cost the kernel and the encoding thread less, and their deallocators run in the call Metal runs them in, including when a detach races a commit. What changes: - Idle pointer imports. A no-copy buffer's own pointer import is releasable, as its dma-buf import was: while only blits read it and no pending command buffer holds it, the residency thread releases it after its idle window (2 s, doubling at each import again up to 64 s), and the next blit imports it again at the same pointer. Only where the new measured fact DriverFact::PointerImportsFollowTheirAddress holds (ANV 26.2.4 on the Iris Xe; lavapipe 26.0.8 to 26.2.4): there a pointer import reads the pages mapped at its address at each submission, so an import again reads what the kept one would. A refused re-import fails the blit's call, typed. - Deallocator timing. A released import hands its keep-alive and deallocators to the objects a command buffer still holds, so they run as that memory is freed (or parked and announced, as a kept import's). A committed but unsubmitted command buffer holds a blit's import objects weakly, so a rebind or detach before its first submission runs the deallocator in its call, as Metal does. A detach racing a commit on another thread runs the deallocator in one of the two calls: a let-go mark on the pages and the commit's hold count, both sequentially consistent, make each side see the other, and a commit that sees the mark replays its own command buffer at once. The live list re-reads a buffer's pages where a detach moved them meanwhile. HostDeallocatorDelayed is no longer announced for these races. - Fewer kernel queries. A no-copy buffer asks the kernel about its mappings (PROCMAP_QUERY) only where it makes its own import; a rebind onto a live buffer's bytes lies in that buffer's import and asks nothing; /proc/self/maps stays open for the queries (reopened in a forked process). Three blit paths that read the caller's buffer as a read apart now read it as blits, so their imports stay releasable. - Fewer rewrites and lookups. A rebind or detach rewrites only the heap slots of textures over the bytes it moved; host-page alias lookups visit only the length classes that hold ranges and keep each range's overlapping ranges; a binding and a descriptor each read where a no-copy buffer lies once. - Counters (WorkCounts): host_imports_used and host_imports_read_apart (with its six reasons), host_imports_kept_for_fds and kept imports by cause (of_heap, rebound, pointer, no_stand_in), heap_slots_rewritten, host_mappings_queried (Linux; 0 elsewhere), host_range_lookups, host_remaps_reported, host_remaps_over_imports, host_imports_remapped, host_pointer_imports_made and _bytes_made, no_copy_lies_read, host_imports_rebound_unlisted, host_rebind_imports_made and _covered. - Docs: docs/HOWTO.md on both kinds of release, the RLIMIT_NOFILE soft limit a program raises itself and the new gauges; a FAST-PATHS known gap that a buffer made before a remap reads the new pages on drivers whose pointer imports follow their address, where Metal keeps the old ones (HostMemory::new's contract forbids such a remap); the rustdoc of host_range_remapped. Why: ANV over i915 lists every VkDeviceMemory in every execbuffer, and a pointer import costs about 0.19 us an execbuffer; the consumer's guest keeps hundreds of no-copy buffers live and uses few in a frame. Its swap thread spent 1.54 ms of kernel time a frame querying mappings of a buffer that joined a live import. And a released import's deallocator unmapped pages under a live userptr, losing the device. Verified: - Iris Xe, a guest-shaped lane of 520 pointer imports and 43 command buffers a frame, the idle ones released: objects per execbuffer 536 -> 51, ioctl 4.24-4.31 -> 0.85-1.09 ms a frame, scheduler CPU 4.96-5.05 -> 1.33-1.74 ms a frame, frame p50 5.27-5.43 -> 2.70-3.11 ms. - The fact, probed outside the runtime on the Iris Xe and through tests::pointer_residency on it and lavapipe: a kept import read the pages a remap put at its address. - The detach race on the Iris Xe and RTX 5080 under the validation layer: without the fix 3 of 3 runs failed (deallocators late or on another thread); with it 12 of 12 runs, 15,360 races, each deallocator in the detach or the commit, never late. - A second no-copy buffer over 2040 scattered mappings: 415-418 us -> 0.3-0.6 us. A rebind with 1024 other live textures: 4766 -> 49 us. Alias lookups: a draw -5.3% instructions on host-page frames (RX 7900 XTX). - Device and unit tests: freed_memory, pointer_residency, host_residency, blit_holds, encoder_holds, host_import_counts, host_aliases (up to 45 passed per run); lanes rebinds, rebind_reach, detach, detach_textures, host_memory, no_copy_heaps, dma_buf_imports, draw_work (74 passed on lavapipe, 30 on the Iris Xe for the deallocator fixes). clippy -D warnings (plain, m2v_gpu, m2v_cpu_icd) and rustdoc.
metal2vulkan
metal2vulkan runs Metal shaders and Metal programs on Vulkan. It reads Metal AIR (a metallib, or AIR bitcode) and compiles it into Vulkan pipelines, and it offers the Metal API itself on top of a Vulkan device.
Beta. The API, the command line, the pipeline JSON and the emitted SPIR-V may change during the
0.xseries. Version your cached outputs.
The crates
| Crate | Directory | What it is |
|---|---|---|
metal2vulkan |
crates/metal2vulkan |
The translator. A Metal-shaped API (device, library, pipeline descriptors, binding plans) that compiles compute, render, tessellation, tile and mesh pipelines for a described Vulkan device: SPIR-V per stage, one pipeline layout, fixed-function state, and per-draw descriptor writes and push constants. The cli feature builds the metal2vulkan command. |
metal2vulkan-runtime |
crates/metal2vulkan-runtime |
The Metal API on Vulkan: devices, command queues and buffers, encoders, buffers, textures, heaps, pipeline states and events, run on a Vulkan device through ash. It uses only the translator's public API. |
metal2vulkan-air |
crates/metal2vulkan-air |
The AIR front end: the metallib container, the bitcode reader, a typed IR, AIR's metadata ABI, intrinsics and canonicalization. For tools that read AIR without translating it. |
metal2vulkan-validation |
crates/metal2vulkan-validation |
Not published. Authored cases run on Metal and on Vulkan and compared, and bounded audits over a private corpus. |
metal2vulkan-devtools |
crates/metal2vulkan-devtools |
Not published. The external-tool runner and fixture assemblers the tests share. |
The three published crates share one version and are released together; metal2vulkan pins
metal2vulkan-air exactly.
What the translator promises:
- Typed refusals. Input the device cannot run exactly fails with an error that names the Metal feature and what would make it work. It never returns a module that computes something else.
- Recorded differences. Where a pipeline gives a near-equivalent of Metal's answer, its artifact records a precision note, and the runtime announces it once with a stable code.
- Fast by default. Off Apple's GPUs, choices that trade exactness for speed take the device's native form; each has an opt-in exact switch (docs/FAST-PATHS.md).
- Bounded work. Every compile runs under a deadline, a cancellation token and deterministic work and memory caps.
Install
0.2.0 is not on crates.io yet (crates.io has 0.1.0, an unrelated earlier design). Depend on a git revision:
[dependencies]
metal2vulkan = { git = "https://github.com/steelbrain/metal2vulkan", rev = "<sha>" }
# For the Metal API on Vulkan:
metal2vulkan-runtime = { git = "https://github.com/steelbrain/metal2vulkan", rev = "<sha>" }
Features of metal2vulkan, none needed by the library:
serde: serialization of the API's plain-data values.ash: conversions to and fromash::vk, andDeviceDescription::query, which reads a physical device.cli: themetal2vulkancommand (enablesserde).
Requirements: Rust 1.98 or newer. The libraries run no external tool. The runtime opens the Vulkan loader at run time and needs a Vulkan 1.2 device.
Command line
cargo install --git https://github.com/steelbrain/metal2vulkan metal2vulkan --features cli
The command compiles one pipeline of a metallib, an AIR bitcode module or an LLVM text module
(.ll) for the device a Khronos profiles JSON describes, and writes its SPIR-V:
# A kernel: one module.
metal2vulkan library.metallib kernel.spv --profile device.json --function my_kernel
# A render pipeline: one module per stage (pipeline.vert.spv, pipeline.frag.spv).
metal2vulkan shaders.air pipeline.spv --profile device.json \
--function vertex_main --fragment fragment_main --vertex-descriptor vertex.json \
--color-format 0:BGRA8Unorm --depth-format Depth32Float
# The pipeline's layout, interface, dependencies and stages as JSON.
metal2vulkan kernel.ll kernel.spv --profile device.json --emit-meta kernel.json
--profileis required: the crate ships no device description. Any Khronos profiles JSON works;crates/metal2vulkan-validation/profiles/has examples, from a bare Vulkan 1.2 device to desktop GPUs and MoltenVK.- Without
--functionit compiles the library's only kernel or vertex function. A kernel with--color-formatcompiles as a tile pipeline. A post-tessellation vertex function adds.tesc.spvand.tese.spvmodules on a device withtessellationShader; on one without, it is the.vert.spvmodule itself, which draws the tessellator's triangles. - A compile that needs a pipeline input fails naming it and the option that supplies it:
--constant ID:TYPE:VALUEfor a function constant,--stage-input FILEor--vertex-descriptor FILEfor a stage-input or vertex descriptor (as JSON). - A
.llinput is assembled withllvm-as(LLVM 21 or newer). Each module written is checked withspirv-valwhen it is found, under the environments the library states for the device;--no-spirv-valskips it. Both tools come fromPATH, or fromMETAL2VULKAN_LLVM_ASandMETAL2VULKAN_SPIRV_VAL. - Success prints a line with
PASSand exits 0. Failure printsmetal2vulkan: FALLBACK (<why>), writes nothing, and exits 2; a failure after the inputs are read also writes a repro bundle under$TMPDIR/metal2vulkan-repros(orMETAL2VULKAN_REPRO_DIR). metal2vulkan --helplists every option.
Library
The flow follows Metal's object lifecycle: describe the device, parse a library, compile a pipeline, then plan and instantiate each dispatch or draw.
use metal2vulkan::device::{Device, DeviceDescription, DevicePolicy};
use metal2vulkan::library::Library;
use metal2vulkan::pipeline::{ComputePipelineDescriptor, FunctionDescriptor, StageSource};
use metal2vulkan::Budget;
let description = DeviceDescription::from_khronos_json(&profile_json, None)?;
let device = Device::new(description, DevicePolicy::default())?;
let library = Library::parse(&metallib_bytes)?;
let kernel = FunctionDescriptor::new(library.function("my_kernel")?);
let descriptor = ComputePipelineDescriptor::new(StageSource::Function(kernel));
let pipeline = device.compile_compute_pipeline(&descriptor, &Budget::standard())?;
// pipeline.stage.spirv, pipeline.layout, pipeline.interface: create the Vulkan objects from these,
// then pipeline.plan(...) and plan.instantiate_dispatch(...) per dispatch.
docs/API.md walks the whole flow on a runnable example,
examples/api_preview.rs:
cargo run -p metal2vulkan --example api_preview # needs llvm-as on PATH
For the runtime, start at its README.
Documentation
| Document | Read it when |
|---|---|
| docs/API.md | You use the translator's API: devices, libraries, pipelines, binding, caching, errors; and the runtime's degrade notices |
| Runtime README | You use the Metal API on Vulkan |
| docs/HOWTO.md | You integrate: tools, caching, failures, isolation, the runtime's shader cache |
| docs/RUNTIME-PORTING.md | You port Metal calls (Objective-C or metal-rs) to the runtime |
| docs/FAST-PATHS.md | You need every speed-for-accuracy default and its exact switch |
| docs/METAL-COVERAGE.md | You ask whether a Metal API item is covered |
| docs/REFUSALS.md | You ask what Metal accepts that the code refuses |
| docs/ARCHITECTURE.md | You change the translator |
| docs/VALIDATION.md | You check behavior against Metal |
| CONTRIBUTING.md | You build and test the repository |
The rustdoc of each crate (cargo doc --workspace --all-features --open) documents every public
item; each runtime item names its Metal counterpart.
Shaders in this repository
The repository ships no third-party shaders. Tests use synthetic fixtures written for it. Passing
spirv-val shows a module is valid, not that it computes what Metal computes; that claim needs the
authored validation cases (docs/VALIDATION.md).
License
metal2vulkan is dual-licensed: you can redistribute it and/or modify it under the terms of either of these licenses, at your choice (LICENSE):
- the GNU General Public License, version 2, or (at your option) any later version;
- the GNU Lesser General Public License, version 3, or (at your option) any later version. It incorporates the terms of the GNU General Public License, version 3.
As an SPDX expression: GPL-2.0-or-later OR LGPL-3.0-or-later. Each published crate carries the
notice and the three texts.
Metal is a trademark of Apple Inc. metal2vulkan is an independent project and is not affiliated with, sponsored by, or endorsed by Apple Inc.