Anees Iqbal dd5a19aea1 Alias a padded texture that only reads by default, the shadow behind PaddedReadsPolicy::Shadow
The padded-reads work packet (the owner's choice): a padded texture that is only read aliases the
buffer by default, under the same documented first-move gap as tight aliases, checked per driver;
the exact shadow stays behind PaddedReadsPolicy::Shadow, which exact_answers() selects.

What: a 2D texture (or array) over a buffer whose rows or slices lie with bytes between them, and
whose usage as Metal reports it holds no RenderTarget, ShaderWrite, ShaderAtomic or block writes
(Unknown reads as 0x17, so it stays a shadow; a swizzled texture reports ShaderRead |
PixelFormatView and reads), is now an image over the buffer's bytes at Metal's row pitch, as a
texture of packed rows is: the device's own linear image (PREINITIALIZED over ordinary memory, for
host allocations over a no-copy buffer), a DRM_FORMAT_MOD_LINEAR image at that pitch, or one over
a dma-buf of the pages. Its subresources may hold the bytes between its rows and its last row's
padding (to row_pitch x rows); a driver whose image reaches past that is refused as for packed rows
(lavapipe's height rounded up to 4). A render target or a texture a shader writes stays a shadow.
The rebind remakes the image under the same rule (AliasKind::Linear keeps it).

The switch: DevicePolicy.padded_reads, PaddedReadsPolicy::{Alias (default), Shadow};
DevicePolicy::exact_answers() selects Shadow, the runtime's shadow copied in before each use. It
is the runtime's alone: no ProfileFields carries it, so no profile fingerprint moves
(tests::fast_paths::the_runtime_s_padded_reads_choice_keys_no_profile) and SEMANTICS_VERSION
stays. The owner's ruling (relayed by reims-vgpu-orchestrator, 2026-10-14 15:55): "I'll take 1 &
2", (2) being this, "exact to the same standard as tight aliases", under 30c8b4bff8's first-move
known gap extended to the padding bytes, checked by the same per-driver lane check.

Why it is the same standard: the first move is the only layout transition a runtime image takes.
- Every image memory barrier the runtime records is a first move (texture/first_move.rs
  FirstMove::barrier), a new image's own (storage_form.rs, host_copy.rs; never an alias), or a
  caller-wrapped image's Transition (submit.rs via texture/wrapped.rs).
- The tracker's barriers are VkMemoryBarriers (tracker.rs Barrier::record), which transition no
  layout.
- Blits into a texture copy in GENERAL: blit_texture.rs's vkCmdCopyBufferToImage and
  vkCmdCopyImage. VUID-vkCmdCopyBufferToImage-dstImageLayout-01396 and
  VUID-vkCmdCopyImage-dstImageLayout-01395 allow GENERAL; GENERAL "supports all types of device
  access".
- "Each source region specified by pRegions is copied from the source buffer to the destination
  region of the destination image". The Memory Model: "A memory operation must not affect memory
  at a memory location not within its set of memory locations".
- Every descriptor names GENERAL (binding.rs, heap_sets.rs).
- A texture over a buffer has one level (linear_plan's MipmapLevelCount), so generate_mipmaps
  leaves it, and optimize_contents_for_gpu_access records nothing.
- A texture without RenderTarget usage has no COLOR_ATTACHMENT usage. A pass renders it through a
  stand-in, copied in GENERAL, so no load or store operation reaches its image.

How:
- linear_plan's padding() becomes holding(layout, usage, policy) -> Result<Holds, String>, with
  Holds::{Texels, Padding}, Holds::fits (texels_alone, or the new padded_alone) and only_reads.
- aliases() takes the Holds. So do alias_image, dma_buf_alias_image (DmaBufTexels.alone),
  host_alias_unbound, host_pitched_image, and the pointer and dma-buf rebinds.
- storage_over_buffer decides it once, from texture_usage (the effective usage), so the shadow's
  reason no longer reads the raw descriptor's RenderTarget bit. The reason of a read-only texture
  shadowed under the switch names PaddedReadsPolicy::Shadow.
- RUNTIME_VERSION 1211 (renumbered at compose); the runtime-sources pin is recomputed at this tree.

Docs:
- FAST-PATHS.md: a fast-defaults row (padded_reads = PaddedReadsPolicy::Alias/Shadow, ANV's 0.63
  ms copy in per tile use and 135 vs 41 us making).
- FAST-PATHS.md: the padded-row shadow row now covers textures that render or that a shader
  writes.
- FAST-PATHS.md: the first-move gap row now covers the bytes between rows, with the GENERAL anchors
  in prose and the M3 Max probe's point (Metal never writes padding, even under concurrent CPU
  writes).
- API.md's shadowed-texture entry, ShadowedTexture's rustdoc, HOWTO.md, interop.rs and dma_buf.rs.
- The refusal surface is unchanged (refusal_surface.py --check).

Verified so far:
- clippy -D warnings, all targets, all features, --cfg m2v_cpu_icd: clean (um780, Rust 1.98.1).
- cargo fmt --check: clean.
The unit and lane runs follow with the lane checks' commit.

Check per driver that a padded texture that only reads keeps every byte of its pages:
What: three lane tests in runtime_lane::pitched_surfaces, under PaddedReadsPolicy::Alias (the lane's
own devices take exact_answers, so Shadow), each over no-copy buffers of anonymous pages:

- a_texture_that_only_reads_keeps_every_byte_of_its_pages: a ShaderRead texture over each of the
  11 layouts. Its first use is a fragment function's read through its image, before anything else
  touches the pages: it must read the pages' texels, and every byte of the pages, the bytes between
  the rows among them, must be unchanged. That is the image's first move, the known gap 30c8b4bff8
  records, now over padding.
  Then: a nearest and a linear sample, get_bytes and a blit out. The caller rewrites the texels and
  the bytes between the rows; a sample and a read must give the new texels. Blits Metal makes into
  any texture: a buffer's box at an odd origin, replace_region of a box, a private texture's box.
  After each step every byte of the pages is compared (the blit-into-alias check the peer asked
  for: a driver whose copy writes past its region fails, naming the bytes).
  Prints PITCHED-READS, how many padded layouts aliased. Off MoltenVK at least one must, or no
  first move over padded rows was checked.
- a_padded_texture_that_only_reads_copies_no_shadow_by_default: each padded layout under Alias and
  under Shadow. Three nearest samples and a blit out read the texels. An aliased texture copies no
  shadow (WorkCounts::shadow_copies unchanged), a shadow copies one in (more than 0). Under Shadow
  it is never aliased off MoltenVK. Off MoltenVK at least one layout aliases under Alias.
- a_padded_texture_that_renders_or_writes_is_a_shadow_under_either_policy: the browser's tile with
  RenderTarget | ShaderRead, ShaderRead | ShaderWrite and Unknown usage, under both policies, over
  a no-copy buffer (shadow off MoltenVK) and an ordinary buffer (shadow everywhere).

surface() takes whether the device may alias the texture's padded rows; the existing callers pass
false and keep their assertion.

Why: the owner's standard for the read-only padded alias is the tight aliases', checked per driver
by the same first-use lane check. A driver that fails it gets a structural driver-defect entry
that keeps the shadow there.

Lane filter for the round: runtime_lane::pitched_surfaces.
Verified: clippy -D warnings with --cfg m2v_cpu_icd (um780). The device runs follow.

Check a padded texture that only reads over an ordinary buffer's rows too:
What: runtime_lane::pitched_surfaces::a_padded_texture_that_only_reads_over_an_ordinary_buffer_keeps_its_bytes,
over each padded layout of LAYOUTS in a Shared buffer (not host pages), under both policies.
- Under PaddedReadsPolicy::Alias the texture is the device's own linear image over the buffer's
  memory (made PREINITIALIZED, linear_image), where the device lays one at the rows' pitch.
- Its first use, a fragment function's read through it, must read the buffer's texels and leave
  every byte of the buffer as it was, the bytes between the rows included.
- A nearest sample must read the texels too; an aliased texture copies no shadow over the two.
- Under Shadow it is never aliased.
- It prints each layout's form (PITCHED-READS).

Why: the read-only padded alias also takes the runtime's own PREINITIALIZED linear image, a path the
no-copy lanes do not reach. Its first move keeps the contents by the spec (the old layout is the
current one); the move's reach over the padding is the same known gap, now checked here per driver.

Verified: to follow, with the GPU runs (lavapipe, ANV, RADV x2, NVIDIA x2).

Name the drivers whose first move keeps a padded texture's bytes:
docs/FAST-PATHS.md's first-move known gap now lists the drivers whose run showed the bytes kept.
That covers the bytes between the rows of each padded layout the driver aliases, and the ordinary
buffer's check joins the cited tests.

Verified: runtime_lane::pitched_surfaces passed 19 of 19 at 6d4508a65d's tree on each:
- lavapipe and ANV (Iris Xe RPL-P), Mesa 26.2.4;
- RADV: the Radeon 780M on Mesa 26.2.4, the RX 7900 XTX on 26.0.8;
- NVIDIA 615.71.09: RTX 3090 and RTX 5080 Laptop GPU.
Padded layouts that only read and were aliased by default: 8, 8, 2, 2, 4 and 4 of 8. None changed
a byte of the pages. MoltenVK was not run.

Name the padded-reads alias among the default's choices in the API guide and the runtime's guide:
What: docs/API.md's list of what DevicePolicy::UNTRUSTED holds gains the image over a padded
texture that only reads (PaddedReadsPolicy::Alias), its structural test (no RenderTarget,
ShaderWrite, ShaderAtomic or block writes in the usage), its known gap (the bytes between the
rows are kept as far as the driver's first move keeps them) and the switch exact_answers()
selects. The runtime README's sentence on textures over buffers now says when the runtime keeps
a copy: where linear images cannot hold Metal's layout, and over padded rows when the texture
renders or a shader writes it, or only reads under PaddedReadsPolicy::Shadow.

Why: both guides described the default without the runtime's new choice; the README's sentence
already missed the padded render-target shadow.

Verified: docs only. The degrade-catalogue tests read API.md's catalogue, which this does not
touch; no degrade code is named. The runtime-sources pin covers src/ alone, so it is unchanged.

Time a compositor's frame over padded textures that only read, under each PaddedReadsPolicy:
What: surface_texture_costs gains read_only_padded_costs, an ignored release-mode measurement
(SURFTEX-READS). Each frame makes, for each padded layout of LAYOUTS, a no-copy buffer over its
surface's anonymous pages and a ShaderRead-only texture over it, samples each into a private
target of the browser tile in one pass, commits and waits. It runs under PaddedReadsPolicy::Alias
and ::Shadow, a device each, interleaved over three rounds, and prints per policy and round the
textures' forms, the medians and 90th percentiles of a texture's making, the frame's encoding,
commit to completion and process CPU, and the shadow copies a frame. pitched_surfaces' reading
pipeline and sampler helpers become pub(super) for it.

Why: FAST-PATHS' padded-reads row quotes ANV's copy-in cost per use; this measures what the
default saves per frame against the exact switch on any device, with the forms shown so a device
that shadows anyway is not counted as a saving.

Verified: validation-only and ignored by default; it changes no product source, so the runtime
pin and versions are unchanged. Its build and clippy under --cfg m2v_gpu are checked on um780,
and its run is on ms01's ANV.

Print why a padded texture that only reads over an ordinary buffer is a shadow:
What: a_padded_texture_that_only_reads_over_an_ordinary_buffer_keeps_its_bytes appends the
shadowed-texture notice's reason to its PITCHED-READS line, as the pages' tests already do in
their PITCHED-SURFACE lines, instead of taking the notices unread.

Why: on ANV (Mesa 26.2.4, ms01) none of the 8 padded layouts aliased over an ordinary buffer
under PaddedReadsPolicy::Alias, while all 8 alias over no-copy pages; the run showed the form but
not the reason, so the cause could only be guessed. The print names it per device.

Verified: test-only; the run on ms01's ANV prints the reasons (in the follow-up timing window).
Clippy under --cfg m2v_gpu on um780.

Quote ANV's measured frame cost of the padded-reads shadow in its ledger row:
What: FAST-PATHS.md's padded_reads row now quotes what read_only_padded_costs measured on ms01's
ANV (Iris Xe, Mesa 26.2.4). A frame sampling four BGRA8Unorm tiles over no-copy pages
(1307x512 at 5248 and 5376-byte rows, 300x200 at 1216, 1913x96 at 7680) in one pass completes
about 0.36 ms later as shadows: the median of 15 interleaved Alias/Shadow pairs, 0.08 to 0.60 ms,
all 15 positive. It also takes about 56 us more encoding and 91 us more process CPU. A texture's
making costs the same either way, about 45 us.

Why: the row quoted a 0.63 ms copy in per use and 135 vs 41 us making. Those were relayed
figures, not this case's measurement. Here making costs the same, and sampling the linear image
over host pages gives back part of the copy's saving, so the frame saves less than the copies'
sum.

Verified: 5 runs of read_only_padded_costs (3 interleaved rounds of 100 frames after 10 warm,
each) on ms01, release, --cfg m2v_gpu --cfg m2v_system_alloc, under both ANV locks, taskset
0-11, load under 1, forms "aaaa" under Alias and "ssss" under Shadow, 4.00 shadow copies a frame.
A first run of 3 more pairs, taken right after another workload, swung from -2.2 to +0.57 ms,
and is not counted. tests::fast_paths passes with the edited row (5/5, um780).

Versions: RUNTIME_VERSION 1204 -> 1205. RUNTIME_VERSION moves: read-only padded textures alias by
default. crates/metal2vulkan-validation/runtime-sources.sha256 holds
edc97e709c2605eaa13ac8696ea119fb9b727243a276a679d0b3288ed5b760b3, the hash of this tree's runtime
inputs (runtime-pin.py, which reproduces build.rs's). Version numbers quoted above are the values
the work's branch built with. Each part's Verified lines name the tree it ran at.
2026-10-14 20:17:40 +03:00
2026-07-22 13:15:24 +03:00

metal2vulkan

License: GPL-2.0-or-later OR LGPL-3.0-or-later

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.

Alpha. The API, the command line, the pipeline JSON and the emitted SPIR-V may change during the 0.x series. 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 from ash::vk, and DeviceDescription::query, which reads a physical device.
  • cli: the metal2vulkan command (enables serde).

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
  • --profile is 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 --function it compiles the library's only kernel or vertex function. A kernel with --color-format compiles as a tile pipeline. A post-tessellation vertex function adds .tesc.spv and .tese.spv modules.
  • A compile that needs a pipeline input fails naming it and the option that supplies it: --constant ID:TYPE:VALUE for a function constant, --stage-input FILE or --vertex-descriptor FILE for a stage-input or vertex descriptor (as JSON).
  • A .ll input is assembled with llvm-as (LLVM 21 or newer). Each module written is checked with spirv-val when it is found, under the environments the library states for the device; --no-spirv-val skips it. Both tools come from PATH, or from METAL2VULKAN_LLVM_AS and METAL2VULKAN_SPIRV_VAL.
  • Success prints a line with PASS and exits 0. Failure prints metal2vulkan: FALLBACK (<why>), writes nothing, and exits 2; a failure after the inputs are read also writes a repro bundle under $TMPDIR/metal2vulkan-repros (or METAL2VULKAN_REPRO_DIR).
  • metal2vulkan --help lists 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):

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.

S
Description
No description provided
Readme
1.7 GiB
0 Stars 1 Watchers 0 Forks
Languages
Rust 61.3%
LLVM 31.7%
Metal 3%
Objective-C 2.2%
Python 1.2%
Other 0.5%