Files
metal2vulkan/docs/RESOURCE-ID.md
T
Anees Iqbal fa52012831 identity: texture slots are the set-1 elements their pushed resource IDs name
Under the identity domains a kernel's texture slots now lower. A slot's
gpuResourceID is pushed as its PushField::SlotGuestBase (the index in the
low word, the word in the high one). Its texture is the element at that
index of one of set 1's two fixed arrays: binding 0, SAMPLED_IMAGE x
65,536, and binding 1, STORAGE_IMAGE x 4,096. Each is partially bound,
updated after binding and while unused elements are pending, and seen by
every stage, with the same layout for every pipeline
(BindingSource::IdentityArray). Each image class the kernel declares
(shape, numeric type and depth comparison, sampled, or storage without a
format) is a variable of its descriptor type's binding. The slot binds no
descriptor and declares no specialization constant:
- is_null_texture answers whether the index is 0. Nil needs nothing
  else in the shader, since element 0 of each array is a null descriptor
  (the device test of ba4454b05b).
- A read the shader checks itself takes Metal's out-of-bounds alpha of 1
  from bit 12 of the word (TextureWord::OUT_OF_BOUNDS_ALPHA_ONE).
- The ResourcePresence and OutOfBoundsAlpha facts are not stated there.

No binder chooses a variant under identity, so a slot reads whatever
texture its ID names through the plain form:
- the variant facts (cross-class reads, planes read in place, texel rows,
  texture-buffer parts) are not stated;
- a float or half read takes the device's sRGB decode with
  PrecisionNote::SrgbTexelDecode (VkProperty::MetalSampler on MoltenVK),
  as a heap element's does, with no SrgbTexture twin;
- no texture is sparse (the consumer refuses one): no write-dropping
  SparseTexture constant and no ConstantLanes;
- a storage slot is formatless with no StorageTextureFormat fact. Where
  the device cannot access it without a format, the slot is refused as
  Unsupported{IdentityDomains} instead of asking for an input no consumer
  gives there.

Admission stays typed. Under identity, a function is refused as
Unsupported{IdentityDomains}, not implemented, if it:
- samples (any sampler plan; Metal's own sampler as a requirement is
  allowed);
- reaches textures through argument buffers (any heap group);
- declares a slot with no identity class: texture buffers, level views,
  cube-as-array, planes, extended range, RGBX, ulong, or a storage
  image with a format;
- uses a slot whose state comes from anything but its ID: border texels,
  sparse residency arms, a half conversion by the bound format, swizzles,
  offset bases;
- carries a fact naming such state (identity_unsourced).
That keeps the arm64-only forms (two-plane, extended range, sRGB twins)
out of the identity domains, as the x86 scope wants. The old binder
refuses an identity array as an internal error, since the consumer
writes those arrays.

SEMANTICS_VERSION 2150 (identity output changes), ARTIFACT_FORMAT_VERSION
529 (BindingSource gains IdentityArray). docs/RESOURCE-ID.md says what the
arrays are and what the shader reads of an ID. docs/API.md says what the
identity domains lower. The refusal surface and the API signatures are
regenerated. The blessed table also gains
metal2vulkan-rt::PrecisionNote::CappedLodBias, a variant development's
table lacked.

Verified, on development cfa389aa9b (check commit 564d131163, same tree):
- msi, job m3-max-41519: clippy -p metal2vulkan --all-targets
  --all-features -D warnings, rustdoc -D warnings, and nextest of
  metal2vulkan's whole suite: 3672 tests run, 3672 passed, 20 skipped.
- msi, job m3-max-41520: clippy of metal2vulkan under --cfg m2v_gpu and
  --cfg m2v_cpu_icd, both clean.
- The new test, texture_slots_are_the_set_1_elements_their_pushed_resource_ids_name,
  runs on lavapipe, moltenvk-m3, and moltenvk-m3 without
  robustImageAccess2. Its words come from tests/resource_words.tsv rows
  70 and 10, checked against TextureWord::of. It checks the fixed set-1
  layout and that the pushed fields are exactly the buffer, the three
  texture IDs and the region. On the interpreter, with the source at
  sampled element 3 and the destination at storage element 2, it checks
  the reads, the writes, the width and is_null_texture both ways (index
  0 and index 1). On moltenvk-m3 without robustImageAccess2, where the
  shader checks the read, the source image traps any access outside it
  and the out-of-bounds alpha comes from bit 12 of the word: 1.0 for
  R8Unorm, 0.0 for RGBA8Unorm. On the other two profiles the device's
  robust texel answers.
- The same change on the earlier base 9a3b6587b2 (jobs m3-max-41478 and
  41479): 3670 of 3670, clippy clean in all three configurations.
2026-11-17 09:51:53 -08:00

102 lines
5.2 KiB
Markdown

# Resource IDs
A Metal texture's or sampler's `gpuResourceID` is a 64-bit value that a shader reads from an
argument buffer or a buffer of its own and then samples, reads or writes through. Under the
translator's identity binding domains the ID is computed on the guest's side, with no round trip
to the runtime, from the index the consumer grants and the object's Metal descriptor:
```text
gpuResourceID = index | word << 32
```
The low 32 bits are the index into the pipeline's heap arrays (set 1, [API.md](API.md) section 5);
the high 32 bits are the word, which carries what a shader reads of the object beyond its
descriptor. Both are pure functions of what the guest already holds, so the same program produces
the same IDs on every run, and a replay reproduces them.
## Index classes
| Index | Object | Arrays it is valid in |
|---|---|---|
| 0 | nil (the ID 0) | none |
| 1 to 4,095 | a storage-capable texture: usage with `MTLTextureUsageShaderWrite`, or `MTLTextureUsageUnknown` | the sampled-image array and the storage-image array |
| 4,096 to 65,535 | any other texture | the sampled-image array |
| 1 to 3,999 | a sampler | the sampler array |
A texture view takes its own index, in the class of its own usage. A texture buffer has no index:
it is a typed refusal under these domains.
## The arrays
Under the identity domains set 1 has one layout for every pipeline: binding 0 is the sampled-image
array of 65,536 elements and binding 1 the storage-image array of 4,096, each partially bound,
updated after binding and while unused elements are pending, and seen by every stage. Element `i`
of each is the texture whose ID holds index `i`, so the consumer writes an element once, as it
makes the texture, for every pipeline. Element 0 is a null descriptor (`nullDescriptor`): a nil
texture sizes 0, reads and samples zero, and drops a store, as Metal answers for a nil texture, with
no code in the shader. Each texture class a shader declares (its shape, numeric type and depth
comparison, sampled, or storage without a format) is a variable of its descriptor type's binding.
A shader takes a texture slot's ID from the slot's push field (`PushField::SlotGuestBase`). It
indexes the arrays with the low word, answers `is_null_texture` by whether the index is 0, and,
where it checks a `read` itself (a device without `robustImageAccess2`), takes an out-of-bounds
alpha of 1 from bit 12 of the word. Samplers are not lowered under these domains yet.
## The texture word
`metal2vulkan::metal::resource_word::TextureWord::of(pixelFormat, swizzle)`:
| Bits | Field |
|---|---|
| 0-11 | the border texels: bit `3c + b` is set where channel `c` (0 red, 1 green, 2 blue, 3 alpha) reads 1 rather than 0 outside the level for border color `b` (`MTLSamplerBorderColor`'s raw value) |
| 12 | the format has no alpha, so an out-of-bounds `read` reads alpha 1 |
| 13-15 | 0 |
| 16-31 | the `MTLPixelFormat` raw value |
The border texels are taken after the swizzle. A color format's channels are its own, a missing
red, green or blue reads 0 and a missing alpha reads 1; `A8Unorm`'s alpha is the color's red; a
format held rotated (`ARGB8Unorm`) reads red from the color's green, green from its blue, blue from
its alpha and alpha from its red; a depth or stencil format reads as red, green and blue, alpha 1.
Every pixel format's raw value is above 0, so a texture's word is never 0. The placeholders
(`Invalid`, `Unspecialized`) and the formats the translator holds no texture of or only copies have
no word.
## The sampler word
`metal2vulkan::metal::sampler::SamplerWord::of(descriptor)`. Each field holds its
`MTLSamplerDescriptor` value's raw Metal value, so the default descriptor's word is 0:
| Bits | Field |
|---|---|
| 0 | `normalizedCoordinates` is false |
| 1 | `minFilter` is `Linear` |
| 2 | `magFilter` is `Linear` |
| 3-4 | `mipFilter` |
| 5-7, 8-10, 11-13 | `sAddressMode`, `tAddressMode`, `rAddressMode` |
| 14-15 | `borderColor` |
| 16-18 | `compareFunction` |
| 19-20 | `reductionMode` |
| 21 | `maxAnisotropy` is above 1 |
| 22-25 | the level a nearest mip filter reads at `lodMinClamp` |
| 26-29 | 15 less the level a nearest mip filter reads at `lodMaxClamp` |
| 30 | `minFilter` or `magFilter` is `Cubic` |
| 31 | 0 |
The level a nearest mip filter reads at a clamp `c`: take `c` below 0 as 0, and below 64 truncate
it onto sixty-fourths of a level; if the result is 15 or more the level is 15; otherwise, with `L`
its whole levels and `q` its fraction, the level is `L + 1` where `q` is above one half, else `L`.
The LOD clamps themselves, `lodAverage` and the anisotropy's degree are not in the word: the
Vulkan sampler applies them.
## Test vectors
[`crates/metal2vulkan/tests/resource_words.tsv`](../crates/metal2vulkan/tests/resource_words.tsv)
holds one descriptor per line with its word, in tab-separated raw Metal values; its header names
the columns. A texture line is a pixel format and its four swizzle sources; a sampler line is every
`MTLSamplerDescriptor` field above, with `lodMinClamp` and `lodMaxClamp` as the float's IEEE-754
bits. Words are hexadecimal.
The translator's tests (`tests/resource_words.rs`) recompute every line's word and hold the file
equal to what the word functions generate, so a changed word changes the file. A guest's own
computation of the words checks itself against the same file.