Files
Anees Iqbal 11055c78d5 ⬆️ Build on metal2vulkan development 82e7c39216
Moves both metal2vulkan revs from d83fb05624 to 82e7c39216, 65 commits on its
development branch, named green by its checks (m3-max-59311). The runtime's
crate takes its published name there (9a1c8fe6dc): metal2vulkan-rt 0.0.0 is
now metal2vulkan-runtime 0.2.0, its library metal2vulkan_runtime. The
dependency key, the crate's paths (each still `as rt`), the lock's entry and
the docs, comments, log lines and scripts that named the crate follow it. The
rename changes no API.

On rt:
- A command buffer waiting for a shared event's value whose signal is
  submitted is Scheduled as it is submitted (4730af16e7): its scheduled
  handlers run on the submit thread then, and its queue's later command
  buffers are no longer held behind it. A wait for a value that neither the
  host set nor a submitted signal promises stays held, Committed, as before
  (counted in Metrics::event_holds, fb4b77048c). GPU order and completion are
  unchanged. The docs that said rt's completion thread runs a held command
  buffer's scheduled handlers now say its submit thread does.
- Copies over buffers are tracked by the memory a buffer lies in, never its
  id (7e38a29b69): a texture over one buffer sees a draw through a texture
  over another buffer over the same bytes, in one command buffer and across
  a held group.
- A kernel that reads buffers through an argument buffer's pointer members,
  or through a gpuAddress the CPU wrote into a constant buffer, now makes
  (3cdca62567, c0403a1c98); the translator refused it (IdentityDomains).
- A refused kernel input names its kind (98895eb6d2, 1c26e1dc54):
  Gap::KernelInput { kind }, so the refusal's text, in the log and in the
  NSError's description, carries the kind. It is still counted as
  KernelInput.
- Memory goes back sooner. The release of a texture or block that an unsent
  upload holds sends the upload at once (0277c68696). A texture longer than
  a 16 MiB block has its block to itself and gives it back at its release
  (61949e8ba4). Every free of device memory on a normal path is made by the
  block-maker thread, and counted against the submits it meets (4c808ffa6d,
  09735ad792). New gauges tell texture blocks apart from the consumer's
  Private takes (b3f6c859dd).
- ComputePipelineState::dispatch_variant, the key of the VkPipeline a dispatch
  runs (82e7c39216), which this tree does not call yet.
- The translator's old path is deleted (0b94d919d9 to 16c831165d): its
  profile comes from Vulkan queries alone, with no policy switches and no
  driver-fact tables. SEMANTICS_VERSION goes from 2201 to 2203 and
  RUNTIME_VERSION from 2540 to 2541, so rt's shader and pipeline caches miss
  once: the first run on each host translates its pipelines again.

The rest is metal2vulkan's own: its conformance declarations and ledger, its
generated documents and the tests of each change. The NSError texts the guest
receives, and the comments of the wire header and the guest's sources that the
prebuilt kext sets and the option ROM record, keep the old name here; commits
of their own change them.
2026-11-20 09:41:45 -08:00

341 lines
23 KiB
Markdown

# Developing reims-vgpu
The repository's layout, its tests, and the Rust library's crates, commands, build profiles and
features. [README.md](../README.md) lists what working on the code needs and the `make` targets,
and [AGENTS.md](../AGENTS.md) has the rules for changing it.
## Repo layout
```text
AGENTS.md the rules for working on the repository
Cargo.toml the Cargo workspace, with Cargo.lock,
rust-toolchain.toml, clippy.toml and
.cargo/config.toml
crates/ its seven crates (below), and reims-ctl, a
workspace of its own that links none of them
xtask/ cargo xtask (below)
firmware/ the reims-graphics device's option ROM: its UEFI
driver, in C, and the prebuilt ROM reims-ctl
carries; in firmware/ambrosia, the device's
driver module for Ambrosia, arm64 guests'
firmware
guest/ the guest's kexts, loader and Metal bundle, and
the prebuilt kext set reims-ctl carries
tests/
shaders/ the tests' metallibs and their sources
suite/fixtures/shaders a link to tests/shaders, which guest/bundle's
build and tools name
scripts/ what a person installing reims-ctl runs
install-ctl.sh builds and installs reims-ctl and its QEMU (and
reims-util, on Linux and Windows); on Windows,
in MSYS2's CLANG64 shell
install.ps1 on Windows: installs MSYS2 and Git for Windows
with winget, turns on the Windows Hypervisor
Platform, then runs install-ctl.sh in CLANG64
scripts/internal/ what developing the project runs
dev-env.sh the environment a developer's builds run in,
through kache where it is installed (below)
new-worktree.sh a new worktree on a new branch
lane-gate.sh, lane-gate/ the graphics lane's landing gate
opencore-add-kexts.sh a copy of a VM's OpenCore image with kexts added
guest-minibuddy-off.sh keeps Setup Assistant's panes from a guest's
next login
walk_window_macos.sh the manual walk of a display's window on macOS
docs/
api.md the C API and the Rust API it wraps, with the
settings and environment variables
architecture.md how the code is built
conventions.md the conventions the code follows
development.md this file: the layout, the tests, the crates
and their commands
testing.md how the code is tested, and the tags a claim
about Metal carries
qemu.md QEMU on the library: its build, VM boots and
what they measured
lessons.md traps that tests, reviews and boots found
accuracy-ledger.md where the Vulkan platform's answers can
differ from Metal's, and what it has not
built or rt refuses
graphics-kext/ the reims-graphics device: its design, the dGPU
oracle and the benchmark
video/ the device's host video: what it answers, its
arms, the video crate and the open bugs
vendor/qemu/ QEMU with the reims-graphics device on
reims-vgpu (submodule)
assets/readme/ the README's screenshots
LICENSE the license, and LICENSES/ its texts (README.md, License)
```
## Testing
- **`cargo xtask check`** runs the lints, rustfmt, clippy and every unit test. Run it once before a
branch that changes code merges.
- **Unit tests** sit beside the code. A test of the Vulkan platform runs on an rt device of the
host's GPU ([testing.md](testing.md)).
- **VM boots** are the only check of what a guest's frameworks choose and of QEMU's side of the C
API ([qemu.md](qemu.md), "Booting a guest to check a change").
- **The guest's kext sets against old releases**: a rebuild of `guest/prebuilt` or
`guest/prebuilt-arm64e` runs `make -C guest kc-check KC=<Boot KC> KC64=<kernelcache>` on a Mac
against the oldest release of each set, x86-64 11.7.11 and arm64 14.6.1, and the newest. It
fails where a kext imports a name that release's kernel collection does not define, which the
newest SDK's headers make easy and a boot of only the newest releases does not see
([lessons.md](lessons.md)). 11.7.11 stands for 12 here: 12.7.6's IOHIDDevice defines every name
11.7.11's does and lacks the same ones (measured 2026-11-16). 12.7.6 is not a superset in
general: it drops other names 11.7.11 defines, none that the sets import today, so a set that
starts importing from another class runs the check on 12.7.6 too. The collections are the
guests' own, read from a VM and kept local,
never committed: an x86-64 guest's `/System/Library/KernelCollections/BootKernelExtensions.kc`
(`scp` from the running guest), and an arm64 guest's kernelcache, from its Preboot volume's
`<volume UUID>/boot/<hash>/System/Library/Caches/com.apple.kernelcaches/kernelcache` (on an APFS
clone of the VM's `disk.img`, attached with `hdiutil attach -nomount -readonly -imagekey
diskimage-class=CRawDiskImage` and the Preboot volume mounted read-only).
A change that means to alter behaviour changes the tests that hold it in the same commit, so the
diff shows exactly what changed for the guest and the host.
## The Rust library
[architecture.md](architecture.md) describes the crates, threads and locks, the raise and death
model, the platform boundary, the APIs, the option ROM and QEMU. Each module's `//!`
doc holds its area's ordering contracts and the measured platform facts it relies on.
### Crates
| crate | path | role |
|---|---|---|
| `reims-vgpu-vulkan` | `crates/reims-vgpu-vulkan` | `core` (`src/core.rs`, `src/core/`), what the platform and the device share, generic over `P: Platform`: the platform traits, the runtime, raises and deaths, the testkit, using nothing else of the crate; and the `Platform` over metal2vulkan-runtime, `RtPlatform` (`src/rt_platform.rs`, `src/rt_platform/`), the library's only one: on Linux and Windows, and on macOS on Mesa's KosmicKrisp; the pick of the host's GPU (`src/device_choice.rs`); its OS services (on macOS Mach VM, threads and the Objective-C message layer the window's AppKit side sends through), and the library's shaders (`src/gpu/shaders.metal`) |
| `reims-vgpu-window` | `crates/reims-vgpu-window` | the device's own window: AppKit's or SDL3's on macOS, SDL3's on Linux, its input and the host's screens; knows nothing of devices or displays. `features.md` lists what the window does, each entry deliberate |
| `reims-vgpu` | `crates/reims-vgpu` | the public Rust API: the reims-graphics device over the platform, its Metal-call decoder and its window; the process's platform |
| `reims-vgpu-c` | `crates/reims-vgpu-c` | the C API (`rvg_*`, `include/reims_vgpu.h`), which QEMU links; `cdylib`, `staticlib` and `rlib` |
| `reims-vgpu-video` | `crates/reims-vgpu-video` | the reims-graphics device's video on the host, independent of the library and QEMU: `core` (what the video arms share), `codec` (the codecs above the hardware, in plain Rust), and on Linux `vaapi` (VA-API through libva) and `nvidia` (NVDEC and NVENC over the CUDA driver API), their libraries loaded at run time |
| `reims-vgpu-video-conformance` | `crates/reims-vgpu-video/conformance` | the test streams' readers and writers the video examples use: MP4 in and out, and frames hashed as ffmpeg's `framemd5` does |
| `reims-vgpu-graphics` | `crates/reims-vgpu-graphics` | the reims-graphics device (1234:5247, `docs/graphics-kext/design.md`): its wire format, whose C header (`include/reims_graphics_wire.h`) the guest's kext and option ROM include, and its host side |
| `xtask` | `xtask` | `cargo xtask`, below |
### Build and test
Run from the repository root. `cargo xtask` is an alias for `cargo run --package xtask --`
(`.cargo/config.toml`), and with no command it lists the commands.
```sh
cargo build --workspace
cargo nextest run --workspace
cargo xtask check # the lints, rustfmt, clippy and every test; run it once before a merge
cargo xtask dist # target/dist-vulkan: the library, its header and reims-vgpu.pc,
# for a host that links it through pkg-config
cargo xtask metallibs # on a Mac: the Vulkan platform's metallibs and their record
```
| command | what it does |
|---|---|
| `testkit` | core's `testkit` module is gated, and only dev-dependencies turn its feature on (D50) |
| `header [--check]` | `include/reims_vgpu.h` from cbindgen; `--check` fails when the committed header drifts from cbindgen's output |
| `dist` | the library as a plain library, below |
| `metallibs` | the Vulkan platform's metallibs and their record, `metallibs.toml`, compiled on a Mac into `crates/reims-vgpu-vulkan/metallibs`, which a build elsewhere takes them from |
| `check` | `testkit`, `header --check`, the records of the prebuilt option ROM and kext set, rustfmt, clippy and the tests ([architecture.md](architecture.md) 13.1); `--no-cargo` skips the cargo steps |
`cargo xtask check` tests with every feature but `reims-vgpu-c/vulkan`, which puts the C API on the
Vulkan platform, and lints with every feature too. On a Mac it also clippy-checks the Linux target,
and, where Rosetta is installed, runs core's tests (the Vulkan crate's unit tests below `core::`)
as `x86_64-apple-darwin`. On the Mac and on Linux it clippy-checks the Windows target,
`x86_64-pc-windows-gnullvm`, too. Its cargo steps run in the environment it was started in,
incremental as cargo's dev and test profiles are, so a check run again after a fix rebuilds what
the fix changed.
#### Building through kache
[kache](https://github.com/kunobi-ninja/kache) is a compiler cache: run in front of rustc, it stores
each unit's outputs on the host, keyed by everything the unit is built from, and restores them as
clones of the stored files (APFS clones on a Mac, reflinks on btrfs), so a worktree builds only the
units no tree on the host built before. It is a tool on the PATH, version 2.0.0 or later (`cargo
install --locked kache --version 2.0.0`, which installs `kache` and `kache-engine` together; run
`kache daemon restart` after an upgrade), never a dependency and never Cargo's configured wrapper:
a host without it builds as before. A
developer's builds take it up through `scripts/internal/dev-env.sh`, sourced from the worktree's
root before each cargo command:
```sh
. scripts/internal/dev-env.sh && cargo nextest run -p reims-vgpu-vulkan -E 'test(/^core::/)'
```
which, where `kache` is on the PATH, sets `RUSTC_WRAPPER=kache`, `KACHE_LOCAL_ONLY=1` and
`KACHE_BUILD_SCRIPT_HERMETIC=1`, and leaves the build incremental (it unsets `CARGO_INCREMENTAL`).
`cargo xtask check` and `make` run their cargo steps in the environment they are started in.
Builds are incremental, and kache keeps their incremental state (`adaptive_incremental` and
`seed_incremental` in the host's configuration, below). A unit whose inputs match an entry in the
store is restored from it. A workspace crate or path dependency whose sources or dependencies
changed since its last build in the same target directory (an edit, a pull, a branch switch, a
rebase) compiles incrementally instead, with incremental state of its own in the target directory,
and kache neither stores nor restores it for up to eight builds, each within 15 minutes of the
last. A compile error keeps the state. A worktree's first incremental compile of a unit imports
compatible finished incremental state from another worktree's build of it on the host, as clones,
so a new worktree's edits start warm (rustc 1.98 or later; read from kache 2.0.0's documentation).
kache seeds only between target directories inside their worktrees, cargo's default `target/` with
no `CARGO_TARGET_DIR`. It maps the compiler's working directory, the worktree's root, to one path
(`-Zremap-cwd-prefix=/src`), and a target directory elsewhere puts a path of its own in the
compiler's arguments, so each target's state is compatible with no other's. Measured on the M3 Max
on 2026-11-03, the first edit of core (then the crate `reims-vgpu-core`) after a build of the
tree: with targets inside the worktrees, a second worktree's compile seeded from the first's state
took 4.3 s, against 7.0 s for the first worktree's, which had no donor; with each target in a
directory of its own under `~/.cache`, the two units' seed identities differed and the second took
5.7 s, against 6.0 s.
A unit is shared between worktrees when nothing it is built from names the worktree's path, and a
build script's run when nothing it writes does. So the tests find the tree at run time: they read
`CARGO_MANIFEST_DIR` and `OUT_DIR` with `std::env::var_os`, which cargo and nextest set for a
package's test processes (`OUT_DIR` where the package has a build script), and
`CARGO_TARGET_TMPDIR`, which neither sets at run time, the same way, with the system's temporary
directory in its place (measured with cargo 1.98.1 and nextest 0.9.140). `env!` of any of them
compiles the worktree's path into the test. `include_bytes!` and `include_str!` compile in a file's
bytes, which kache keys by content, so a test includes a fixture unless the fixture's bytes name the
worktree, and then reads it at run time. These are built again in each worktree:
- `xtask` and its tests: the binary finds the repository from where it was built
(`env!("CARGO_MANIFEST_DIR")`), so a run of another tree's binary acts on the tree that built it;
- on a Mac, the Vulkan crate's build script, whose import metallibs name its `OUT_DIR`: a
workspace package's hermetic run (below) is keyed by its manifest directory.
A build script runs hermetically (`KACHE_BUILD_SCRIPT_HERMETIC=1`): kache runs it with its `OUT_DIR`
in the store, under `out-dirs/v2/`, in a directory named by everything the script sees but its
`OUT_DIR`, and makes the target directory's `OUT_DIR` a link to it. A registry package's run is
then shared between worktrees even where what it writes names its `OUT_DIR`, as SDL3's library
does: its objects name their build directory there (`sdl3-sys`), so otherwise each worktree builds
SDL again (22 s on the Ryzen 7 7840HS, 31 to 67 s on the M3 Max beside other work) and links a
library of other bytes into `sdl3-sys`, the C API's library and the tests of the window,
`reims-vgpu` and the C API. A run kache cannot share (one that fails, writes outside its `OUT_DIR`
or declares other inputs) runs in the target directory, as without it. What ships is the same: a
host without kache builds SDL in its target directory, and through kache only SDL's objects' debug
build directory names the store instead. kache's gc (`kache gc`, and the daemon's) removes a run
no target directory has linked to for a week; removing the store leaves the links dangling, so
remove the target directories built through it with it.
Measured on 2026-10-26: a second worktree's build from an empty target directory, after a first
worktree had built the same tree through kache (`cargo build --workspace`, then `cargo nextest run
--workspace --no-run`, with `cargo xtask check`'s features), and the test targets of the 31 it
built that came from the store:
| | Ryzen 7 7840HS, Linux | M3 Max, macOS, beside other work |
|---|---|---|
| tests compiling the tree's paths in | 170 s (26 s + 144 s), 23 | 216 s (36 s + 180 s), 21 |
| tests reading them at run time | 30 s (26 s + 5 s), 24 | 87 s (37 s + 50 s), 24 |
| and build scripts run hermetically | 1.5 s (0.7 s + 0.8 s), 30 | 9.5 s (6.1 s + 3.4 s), 30 |
Each dependency and workspace library came from the store but `sdl3-sys` (all of them with hermetic
runs), and in the last row only `xtask` and its tests compiled. The M3 Max ran them beside
another project's tests (load averages 11 to 34), so its times are upper bounds.
kache does not restore what a build script writes outside its `OUT_DIR`. The one such write here,
`cargo xtask metallibs`' export, always runs: the export rewrites the record it declares as an
input, and kache records no run whose declared input changed while it ran (measured with kache
1.2.2).
Each host keeps one configuration, `~/.config/kache/config.toml`, read by every build through
kache on it:
```toml
[cache]
local_only = true # no remote storage, no planner: nothing leaves the host
local_max_size = "100GiB" # the store's ceiling, kept by kache (40GiB on Linux)
daemon_idle_timeout_secs = 1800 # the daemon exits when idle
auto_clean_orphaned_targets = false # kache never removes or rewrites a target directory
auto_share_target_files = false
auto_clean_unused_units_days = 0
auto_clean_idle_targets_days = 0
auto_recover_targets = false
seed_new_targets = false # nor copies one tree's build units into another's
adaptive_incremental = true # a unit being edited compiles incrementally
seed_incremental = true # its incremental state seeded from another worktree's
```
Never run `kache init`, which writes the wrapper into `~/.cargo/config.toml` for every project on
the host, links compiler shims into the shell's PATH and installs a login service, nor `kache
login`. kache's store is under `~/Library/Caches/kache` on a Mac and `~/.cache/kache` on Linux:
`kache stats` shows its size and limit, `kache explain CRATE` why a unit missed, and removing the
directory (with no build running) empties it. kache 2 compiles through its daemon (`kache-engine`),
which a build through kache starts where none runs, as `kache stats` does, and which exits after
`daemon_idle_timeout_secs` with nothing to do (`kache daemon stop` ends it at once). The
ceiling counts what the store alone holds: evicting a file still cloned into a target directory
frees nothing, so kache keeps it until that target directory is gone.
#### Dist
```sh
cargo xtask dist # release, into target/dist-vulkan
cargo xtask dist --profile checked --out /tmp/reims-vgpu
```
`cargo xtask dist` builds `reims-vgpu-c` and writes the library for a host that links it the way it
links its other dependencies, through pkg-config:
```text
target/dist-vulkan/
include/reims_vgpu.h the committed header
lib/libreims_vgpu.a the staticlib
lib/libreims_vgpu.so the cdylib (on a Mac libreims_vgpu.dylib,
@rpath/libreims_vgpu.dylib, signed ad hoc; on
Windows libreims_vgpu.dll, linked from the archive)
lib/pkgconfig/reims-vgpu.pc the package
share/doc/reims-vgpu/ the license: LICENSE and LICENSES/, as in the repository
```
`reims-vgpu.pc` takes its prefix from its own path, so the tree can move. Its `Libs.private` lists
the system frameworks and libraries a static link needs. QEMU links `libreims_vgpu.a`, so the
library is part of its binary; the C API's tests link the shared library.
The library is built on the Vulkan platform, its only one (architecture 11.3); `--platform vulkan`
names it for the builds that pass it, and any other platform is refused. `CARGO_TARGET_DIR` moves
every output, the dist tree included, out of `target`.
#### The link order
On Linux the archive lays out the library's functions that `crates/reims-vgpu-c/link-order.txt`
lists ahead of the rest of its code, in the list's order: `dist` renames each one's section to
`.text.sorted.NNNNNN`, which GNU ld's default script sorts by name ahead of `.text`, and prints how
many of the list's functions it placed (`xtask/src/dist/order.rs`). The list names functions as GNU
`nm -C` prints them, without crate disambiguators, so a new metal2vulkan pin keeps it; a function
renamed or removed since is skipped, and a stale list costs only the order's speed, never the
build. To refresh it, on a Linux host: build the tree's dist and QEMU linked by lld (`make qemu
QEMU_CONFIGURE=--extra-ldflags=-fuse-ld=lld`); record QEMU's threads with the LBR's taken branches
while the guest's desktop works (`perf record -e cycles:u -j any,u,save_type -p <QEMU>` over window
drags and Apps opening and closing); count each call or jump between two of the binary's functions
into a call graph, one `caller callee count` a line, which lld reads; link QEMU again with
`-Wl,--call-graph-ordering-file=<graph>`, so lld clusters callers with their callees; then
`cargo xtask link-order <that qemu-system-x86_64> <graph>` rewrites the list from that binary's
order.
### Profiles
Set in `Cargo.toml` (architecture 2.3):
| profile | settings | why |
|---|---|---|
| `dev` | `panic = "unwind"`, overflow checks on, `debug = "line-tables-only"`, dependencies without debuginfo | an unannotated arithmetic slip dies in tests. Line tables keep a backtrace's files and lines, at a fraction of full debuginfo's build time and disk |
| `debugging` | `dev` with full debuginfo, dependencies' too | a debugger session's build (`--profile debugging`), whose variables and types lldb shows |
| `release` | `panic = "unwind"`, overflow checks off, `debug = "line-tables-only"`, thin LTO | the product. Wrapping is explicit where the guest's values wrap (D19), so the checks add nothing |
| `checked` | `release` plus overflow checks and debug assertions | a second pass of the tests: a death here is an arithmetic site review missed |
- **`panic = "unwind"` everywhere** (D9). objc2's `exception::catch` needs it. It is not a way to
survive a panic: `reims-vgpu`'s singleton installs a hook that aborts before anything unwinds, so
an unintended panic dies at its op with no side effects.
- **objc2's debug assertions are on in `dev`** and off in `checked`
(`[profile.checked.package.objc2]`, D36). They panic on a message to nil, a missing method and an
encoding the runtime disagrees with, which the library never sends, so the tests that send to
real Metal check each send.
- objc2's `exception` feature is always on, and `catch-all` never.
### Features
- `reims-vgpu/vulkan` and `reims-vgpu-c/vulkan`: they change nothing, as the Vulkan platform is
the library's only one, and stay for the builds that name them.
- `macos-sdl` (`reims-vgpu-window`, forwarded by `reims-vgpu` and `reims-vgpu-c`), on by default:
SDL3's window on macOS, which `REIMS_VGPU_WINDOW_IMPL=sdl` opens in place of AppKit's. Without it
the macOS library has no SDL.
- `reims-vgpu-window/sdl-no-link`: SDL left unbuilt, for the Linux and Windows targets' clippy on
another OS.
- `reims-vgpu-vulkan/testkit`: `core::testkit` for other crates' tests.
- `reims-vgpu/testkit`: the same, through the public crate, and each handle's core object.
[api.md](api.md) section 2 lists the device's settings and the environment variables the library
reads.