A consumer's video path showed green frames. Its paths call external_writes_done before each picture and a release only in their encode fills, so every acquire of an exported block or surface after the first had no release of the runtime's to match (VUID-vkQueueSubmit2-commandBuffer-03879), and whether memory another API wrote was acquired, or discarded, depended on the order the consumer's calls arrived in. f6fe21431c's test shows it: no submission released the surface. Now one form serves every kind of memory another API shares, the form Direct3D 11 planes already took (crate::handed): each vkQueueSubmit2 part whose command buffers or uploads use an exported block, an exported surface's planes in place or its buffer, or a Direct3D 11 plane acquires all of it from the other API's queue family in one barrier, in a runtime command buffer at the part's head after a global barrier, and releases it in one barrier in another at the part's end, which takes the part's last signals. Per such part: 3 barriers and 2 runtime command buffers; no submission, VkSubmitInfo2 or semaphore wait is added. The uploads' copies into textures over a buffer in an exported block go with the first part, which hands that block over too. Exported memory is the runtime's until its making (Work::create): an exported block's buffer is released, and an exported surface's planes in place move out of the undefined layout and are released in the same barrier, in a runtime command buffer at the head of the submission that takes the make's upload, ahead of any acquire. The upload owes the making by weak reference, so a surface or block dropped first owes nothing and holds nothing. The making's submission is the ready point: Surface::wait_until_ready waits for it in both forms (the copy form's is its block's), and the new Block::wait_until_ready is its twin (Metal counterpart none; refuses a block that is not exported; has the submission go at once, so it returns with no command buffer committed). One barrier at the making, nothing per frame beyond the per-part hand-over. external_writes_done keeps only its host-side mark: on a block (and a copy-form surface) the textures over it take its bytes again; in place and on a Direct3D 11 import it records nothing. Surface::buffer records nothing at its make. A block that is not exported is still refused. Deleted, with no replacement: BlitCommandEncoder::release_to_external and BlitCommandEncoder::release_surface_to_external, and Building::release_command, which only they used. Consumer uses (the consumer's checkout, searched for main): release_to_external at vaapi/fill.rs:599 and nvidia/encode/fill.rs:289, each followed by EndEncoding, commit and the consumer's CPU wait; its wrapper trait method BlitEncoder::release_to_external (core/platform/blit.rs:395, a cfg(unix) no-op default) and the RtPlatform impl (rt_platform/blit.rs:992-999); doc mentions at rt_platform/export.rs:33, vaapi/fill.rs:30 and nvidia/encode/fill.rs:18. release_surface_to_external: zero uses. The consumer drops them all in the pin that takes this commit. The guard the planner asked for: a plane's image in place takes its tiling from InPlaceTiling alone, whose values are LINEAR and the DRM_FORMAT_MOD_LINEAR modifier, never OPTIMAL; the device-free test no_plane_in_place_takes_the_optimal_tiling pins it. The order test every_device_completes_a_surface_s_making_before_its_ready_wait_returns checks that each plane owes its move at the make and none once the wait returns, with the device's completed progress at or past the ready point. The export, surface and Direct3D 11 tests now count one acquire and one release per submission and one release at the making, with no release call. The runtime README states the contract (The hand-over contract): nothing another API writes may be handed memory of ours before that memory's ready point; the other API's next write must not begin before the submissions that used the memory have completed; who waits by kind and what a reader gets where no write landed; no optimal image in memory another API writes in place (section 12.11), the Direct3D 11 import being the documented case (section 12.10.1, Table 17). Its known gaps carry the planner's line on host-pointer memory. docs/REFUSALS.md is regenerated and the signature table blessed (one added, two removed). Checked before this commit: clippy of the runtime, all targets, plain and under --cfg m2v_cpu_icd, on this commit's code less its doc changes (job m3-max-77874). The device runs at this commit (lavapipe, the Radeon 780M, the UHD 630) follow and are reported with the lane's tip. The dma-buf import half is not built.
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) 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 the interface a consumer binds each draw and dispatch by. The cli feature builds the metal2vulkan command. |
metal2vulkan-runtime |
crates/metal2vulkan-runtime |
The Metal API on Vulkan, with one path for every device: devices, memory blocks and buffers, textures, sampler states, command queues, command buffers and events, encoders and pipeline states, 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, and metal2vulkan-runtime pins metal2vulkan 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.
- The GPU's native arithmetic. Where the device has a native form (its maths, rounding, conversions and sampler), that form gives the answer, its difference from Metal's recorded.
- 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.3 device with what its README's Floor lists.
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. A profile compiles under the runtime's policy (DevicePolicy::default(), the identity pointer and handle domains), so a profile withoutbufferDeviceAddressandshaderInt64is refused.--device FILEtakes its place with a device description and policy as JSON ({"description": ..., "policy": ...}), which is how a runtime's compile device is reproduced: metal2vulkan-runtime's is the description restricted to the features it enables, under that policy.- 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 withouttessellationShaderit answersFALLBACK, naming the feature. - A vertex function that returns no value, with no
--fragment, compiles with rasterization disabled, the one pipeline Metal makes of it. - 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), whoserepro.shreplays it. 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::from_queries(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,
// and bind each dispatch by the interface, as metal2vulkan-runtime does.
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 precision notes |
| Runtime README | You use the Metal API on Vulkan |
| docs/HOWTO.md | You integrate: tools, caching, failures, isolation, the runtime's shader cache |
| docs/RESOURCE-ID.md | You compute a texture's or sampler's gpuResourceID on the guest's side |
| 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.