bef2ec1df4 copies a texture over a buffer in an exported block in once per change of its bytes.
One change it counted was a write through an address: a submission that held one command of a
pipeline that writes outside its bound slots (writes_unbound) made every such texture owe its
copy (over::ExportedOvers::wrote read Leans::unbound). On the RX 550, whose dma-buf surfaces take
the copy form, a guest step that decoded nothing still made 2,702 copies (31.31 GiB, job m3-max-66531) of the 4K
P010 planes of a stale picture.
An address can reach an exported block's memory only once a public call has handed that address
out. So the bound: each exported block carries one flag (memory::Block::addressed, an
Arc<AtomicBool> that only exported blocks get). Every call below sets it, and the first counts in
the new Metrics::exported_blocks_addressed. ExportedOvers keeps a clone of the flag beside each
texture's weak handle, and wrote() reads it at the submission, where it read Leans::unbound:
`leans.unbound && addressed`. Metal does not define an address taken after the commit and written
for that command buffer to read, so reading the flag at the submission covers every address the
work can use.
Why one flag per block, not one per span: an address reaches its buffer's whole length, and an
exported block's one buffer spans the block. The consumer's facts: an exported block holds one
buffer, made by the host (video decode and encode buffers, probe buffers, surfaces' exported
memory), and no guest buffer lies in one. A copy-form surface's buffer also spans its block. A
per-span flag would therefore always equal the block's.
Why an Arc: the entry keeps its texture weakly, and holding the block would change when its
memory is freed.
The routes that set the flag (the contract):
- Buffer::gpu_address of any buffer whose bytes shaders reach in the block. This includes a
Managed buffer whose device copy the consumer placed in an exported block, which
new_managed_buffer allows ("a Private block the consumer holds"); the flag goes on the block
whose address is answered (BufferObject::address_out).
- Device::new_exported_block. Its Block derefs to BlockInfo, whose pub gpu_address states the
block's address, so the public make marks and counts its block. The copy form of
Device::new_exported_surface calls the crate's exported_block(), which does not.
- A texture buffer's storage element at an index over the block's bytes. Its index reaches the
bytes outside every bound slot, so its make marks the block.
What is not a route:
- A copy-form surface's block. The surface keeps its handle, so its address goes out only through
Surface::buffer() and then gpu_address.
- The planes' resource IDs: they reach the textures' own images, not the block.
- An indirect command buffer's gpuResourceID: the address of its own records block.
- use_resource: tracked as a declared write.
- BlockMappings: neither hook runs for an exported block.
- The runtime's own bound-slot address pushes. The three internal reads on a &Buffer
(tessellation factors, and the indirect execution's range buffer twice) now go through
BufferObject::gpu_address, which marks nothing.
Kept apart: a command recorded while no texture over a buffer in an image of its own lived is
untracked. Today it sets Overs::unbound through after([], true), and it must still make every
texture owe. Overs and Leans now carry it as `untracked` (Overs::untracked), which wrote() still
reads as every texture. Bound slots, declared resources, copies back, acquires and fills are
unchanged. Inside a segment the Shared rules stand as they are; only the "every texture" clause
of the leading copy narrows.
Test (tests/exported_overs.rs, extended): new_exported_block's make counts 1 in
exported_blocks_addressed. Then an opaque-handle surface at a pitch multiple of 4096, which no
driver's pitch of a 256-byte row meets (the copy form, SurfaceCopy::Pitch). Its buffer is filled,
then four submissions each copy the plane out and then dispatch an owned synthetic kernel
(store_through, asserted writes_unbound) that writes through an address into another Shared
buffer, every word checked. Copies in are [1, 0, 0, 0] and the count reads 0. Once the surface
buffer's gpu_address is read, the count reads 1 and three more such submissions copy [0, 1, 1]:
the first owes nothing, because the submission before it went while no address of the block was
out. Every read finds the filled bytes. Without the bound, every submission after the first
copies (expected from the code: wrote() owed on Leans::unbound alone).
Verified at 67a05d9f57 (this tree, on a probe branch):
- compile job m3-max-66670: signatures check, refusal-surface check, fmt, clippy plain /
--cfg m2v_gpu / --cfg m2v_cpu_icd, workspace Rustdoc -D warnings, api-docs, all exit 0. The rt
suite on lavapipe: 642 passed, 3 skipped. The extended test on lavapipe printed "1 1 1 0" and
"[1, 0, 0, 0], then out: [0, 1, 1]; blocks addressed 0, then 1".
- From the m2v_gpu archive m3-max-66672, the extended test printed "1 1 1 0" and
"Copy(Pitch { plane: 0 }) ... [1, 0, 0, 0], then out: [0, 1, 1]; blocks addressed 0, then 1"
on the RX 550 (RADV POLARIS12, Mesa 26.2.6, job m3-max-66678; that leg's rt suite: 644 passed,
3 skipped), the Iris Xe (RPL-P, Mesa 26.2.6, job m3-max-66679) and the RTX 3090 (NVIDIA
620.70.06, job m3-max-66680).
README: the exported block's rule names the routes and the count; the counters line gains
exported_blocks_addressed; the address contract says an exported block's address is one the
consumer read from rt before the commit.
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, 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,
// 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 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.