Files
metal2vulkan/docs
Anees Iqbal 675a3ee122 Take sampled textures over host pages in place where the device's check passes
One work packet, landed as 2 commits (86ec320bae..8485bc6865 on archive/development-linear-2026-10-28), in order:

* Take sampled textures over host pages in place where the device's check passes

A 2D texture (or array) over host pages is now a linear image aliasing those pages, with no shadow,
when all of these hold:
- its declared usage only samples or reads it (no RenderTarget, ShaderWrite or ShaderAtomic);
- the pages are a no-copy buffer's, imported by their pointer or as dma-bufs, or a buffer the
  runtime placed in fresh pages;
- a startup check shows that, on this device, the image's first move keeps every byte while the
  CPU writes beside it.

Vulkan makes such an image start UNDEFINED (VUID-VkImageCreateInfo-pNext-01443) and lets the move
read and write every byte bound to it. Relying on the driver here, once checked, is the owner's
ruling ("In place, trusting the driver"): a deliberate, scoped exception, recorded in FAST-PATHS.

The switch is DevicePolicy::host_page_textures:
- HostPageTexturePolicy::InPlace is the default.
- HostPageTexturePolicy::Shadow is the exact form, which exact_answers() selects. Under it every
  such texture stays a shadow and no check runs.

The check (in_place_check.rs):
- It runs on a thread of its own, started by the first texture that asks, per class of pages:
  pointer imports, fresh pages and dma-buf imports. On a Unix target the dma-buf class uses an
  exported allocation of the device's own.
- Each class tries 11 shapes, 3 rounds each, through the product's own Buffer::new_texture:
  - packed rows of 1, 2, 4, 8 and 16-byte texels;
  - rows with bytes between them, for the same texel sizes;
  - one 3840x2160 surface.
  A shape the device makes no image of is skipped, as the product makes none either.
- A writer thread writes the pattern's complement through texels, the bytes between rows and the
  bytes after the image while the move runs. Every byte is then compared.
- The pattern is a 4099-byte prime period, so it lines up with no row, page or texel. The window
  is filled by copies and compared written bytes first, then a period at a time. That makes the
  33 MB surface cost about 11 ms a round in release.

The verdicts are kept as one record in the shader cache's content store:
- The record is named by vendor, device, driver ID, version, name and info, the UUID, the check's
  kind and version, and RUNTIME_VERSION.
- It is checksummed. A bad, partial or other-version record counts as none: the check reruns and
  overwrites it.
- With no cache directory, the check runs once per process.
- A forced failure (DeviceOptions::fail_in_place_check, a hidden test hook) keeps nothing.

Every texture that still takes a shadow announces why (shadowed-texture) and is counted by cause:
- the policy;
- usage that renders or writes;
- check pending, failed or unchecked;
- refused: rows, detached, image, pitch, placement, the dma-buf source, other dma-buf reasons.

WorkCounts also counts the in-place textures by class and pitched images, and the check's runs and
verdicts.

How an image in place is made, used and visible:
- Pointer and fresh pages take a linear image made for host allocations at Metal's pitch, or a
  DRM_FORMAT_MOD_LINEAR image at an explicit pitch over the host allocation where the driver makes
  one. Dma-buf pages take the modifier image over a dma-buf of the texture's own pages.
- A rebind remakes these images like the others.
- A write or pass beyond the declared usage goes through the existing usage shadow and attachment
  stand-in.
- A CPU write reaches the image's commands as it reaches the buffer's (Host Write Ordering
  Guarantees).

RUNTIME_VERSION 1643 -> 1644. The runtime-sources pin is left for the landing.

Check duration, Apple M3 Max on MoltenVK 1.4.2:
- 75-78 ms in release: Pointer and Fresh passed; DmaBuf unchecked, as there are no dma-bufs on
  macOS.
- 1.76 s in a debug build.
- The first version took 4.1-4.2 s in debug, almost all of it the 3840x2160 shape's per-byte
  hashed fill (240 ms) and compare (461 ms) per round, times 3 rounds and 2 classes; the GPU move
  itself took 0.15-3 ms.
- Nothing was dropped from the check. The 64x64/256 shape became 64x16/256, and packed shapes of
  each texel size were added, because MoltenVK takes no padded pitch: the first version made only
  4-byte images there.

Verified on the M3 Max (debug lanes, --cfg m2v_gpu):
- in_place_textures, all five tests:
  - the path follows the check, and an event-held CPU write after the commit is read;
  - a kernel write and a pass beyond the usage go through their shadows and keep the image;
  - a forced failure keeps the shadow and no record; the next device runs the check, and the one
    after recalls three verdicts and checks nothing;
  - the costs.
- first_use_writes under both settings: 1920x1080/7680 in place, 0 of 542 writes undone.
- no_copy_textures read/write under both settings: ShaderRead-only textures in place at packed
  rows, the rest shadows.
- pitched_surfaces, including the new _in_place variants of the only-reads and read-only-pages
  tests.
- The runtime and translator suites passed (4971 tests), with clippy plain and under --cfg m2v_gpu.

Cost on the M3 Max, release, median GPU time per command buffer, in place against shadow:
- 3840x2160, one pass reading a 32-row strip: 492 against 553 us.
- 1920x1080 strip: 457 against 452 us.
- 1920x1080 whole: 393 against 466 us.
- 1920x1080, three whole passes: 566 against 545 us.

Untested here: RADV, lavapipe and the dma-buf class, which follow; ANV and NVIDIA have no venue
now.

* Re-pin the runtime sources at the landing tip

The landed packet changed a runtime-sources input, so the committed digest no

longer described the sources and `the_runtime_sources_are_the_committed_ones`

would fail. Recomputed over the same inputs and committed by land-pkt.sh.
2026-10-28 17:24:19 +02:00
..