Under the identity domains a texture slot sampled through a constexpr sampler takes the sampler
from the set-1 sampler array of its use (binding 2 plain, 3 compare, 4 integer texture) at an
index the consumer pushes: PushField::ConstexprSampler(CombinedSampler), one u32. The consumer
makes the device sampler that the new public CombinedSampler::constexpr_sampler(profile)
describes, places it at an index of that array, and pushes the index. The index is a push word,
not a specialization constant: an index allocated at run time would key the driver's and the
pipeline cache's entries. Link's immutable sampler for the default domains now goes through the
same method, with the same result.
Under the identity domains every device sampler is normalized: a constexpr pixel sampler's
coordinate is scaled in the shader, as a runtime sampler's is, never handed to the device's
unnormalized sampler. A linear sample or gather through it records
PrecisionNote::PixelSamplerScaling, which is static. No address-mode-map variant applies there
(no binder picks a variant), so the sampler keeps its own modes and the slot records no
ConstexprSampled fact.
Still refused as Unsupported{IdentityDomains}: an argument buffer's sampler, arrays of samplers,
bicubic and anisotropy emulation. The refusal test now uses engine/a2_bicubic_weights.
Translation output changes only under the identity domains (Device::from_queries). Device::new
output is unchanged: golden_hashes_match_the_table passes at this tip (job m3-max-42192), with no
"changed under an unchanged key" and no "profile changed:" line.
Public surface: PushField::ConstexprSampler and CombinedSampler::constexpr_sampler are added
(api_signatures blessed); nothing is removed or changed.
SEMANTICS_VERSION 2162: identity-domain modules of constexpr-sampling kernels change.
ARTIFACT_FORMAT_VERSION 531: PushField gains a variant.
Verified:
- m3-max-42097 (msi), this change on cd5a76a757: clippy -p metal2vulkan --all-targets
--all-features and rustdoc -D warnings green; nextest all() of metal2vulkan, 3,689 of 3,693
passed. The four failures were development's own (the new pad_simd_shape and
vector_word_phases fixtures had no table rows), re-blessed at 74669c0552.
- m3-max-42098 (msi): clippy --cfg m2v_gpu and --cfg m2v_cpu_icd green.
- m3-max-42192 (msi), this tip on the samplers landing on 58891b9ce5: golden_hashes_match_the_table,
public_fixtures_engine inventory and llvm_as part 8, and air_input's engine golden printouts
pass.
- engine_identity a_constexpr_sampler_is_the_set_1_element_its_pushed_index_names: sample_up and
the same kernel through a linear pixel sampler link and pass L3 on every profile and on
moltenvk-m3 without robustImageAccess2. Their push fields are the two texture IDs and
ConstexprSampler(c) of the normalized use, with no spec constant but LocalSize, no immutable
sampler and no ConstexprSampled fact; set 1 bindings 0, 1 and 2 are used; constexpr_sampler
gives a normalized device sampler. PixelSamplerScaling is recorded for the pixel kernel under
identity, and under the default domains, which hand that kernel the unnormalized sampler, it is
not.
120 lines
6.5 KiB
Markdown
120 lines
6.5 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, binding 1 the storage-image array of 4,096, and bindings 2, 3 and 4 the
|
|
sampler arrays of 4,000 elements each, every one partially bound, updated after binding and while
|
|
unused elements are pending, and seen by every stage. Element `i` of each image array 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`), and an
|
|
argument buffer's texture's ID from the member's 8 bytes, with no table between. 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.
|
|
|
|
Element `i` of each sampler array is a device sampler made of the sampler whose ID holds index `i`,
|
|
one per use, which the shader picks by the instruction and the texture's numeric type: binding 2
|
|
for a plain sample, 3 for a depth comparison, and 4 for a texture of an integer format, every
|
|
filter nearest. The consumer makes each from the descriptor (its reduction mode and anisotropy
|
|
included); it makes none of a descriptor whose min or mag filter is cubic. A shader takes a
|
|
runtime sampler slot's ID from the slot's push field, samples through the element its low word
|
|
indexes, and reads the word's fields where a sample depends on them (pixel coordinates, a nearest
|
|
pixel sampler).
|
|
|
|
A constexpr sampler has no ID: the consumer places a device sampler of it (the function's
|
|
`PushField::ConstexprSampler` names it and its use, and `CombinedSampler::constexpr_sampler`
|
|
describes it) at an index of the use's array and pushes that index as a `u32`. Its coordinates
|
|
are normalized; the shader scales a pixel sampler's coordinate itself. It samples with its own
|
|
address modes. An argument buffer's
|
|
sampler is 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.
|