The translator built geometry stages in two forms that no Metal function asks for: - the barycentric weights: a fragment function's [[barycentric_coord]] read as varyings, which a generated geometry stage of each topology class wrote, on a device without fragmentShaderBarycentric; - the rate map's fan-out: a draw of a rasterization-rate-mapped pass's triangles once, a geometry stage emitting a copy of each per region, with its probe and its region-copy vertex variant. Neither form reached a guest. The runtime enables no geometryShader, so it refused the weights form already. It never sets BindShape::region_fan_out, and draws a rate-mapped pass region by region (the regions route, RateMapRegions, and region_viewports, which stay). Validation never set region_fan_out either. After this change, one form is left for each: - A fragment function that reads [[barycentric_coord]] reads the device's own BaryCoordKHR/BaryCoordNoPerspKHR. Without fragmentShaderBarycentric it is refused as Unsupported(Barycentrics) needing that feature alone; geometryShader is no longer offered. - A rate-mapped pass draws through the regions route. Rule 1: no precision note stops being raised. The fan-out's copies were a near-equivalent of the regions route, which carries its own note (RateMapRegions::precision), and the weights form raised none. The catalogue is unchanged. Removed: - pipeline::link::geometry (GeometryStages, the three classes' stages) and pipeline::link::fan_out, with their tests (link/tests/geometry.rs, link/tests/fan_out.rs) and tests/engine_region_fan_out; - bind::fan_out (the region table, the probe's word layout and its constants); - RegionFanOut and FanOutState; - the region_fan_out fields of GraphicsState, BindState, BindShape, VariantKey and RenderLinkInputs, and LoweringInputs::region_copies; - the region copy's lowering: StageIoPlan::region_copy, RegionCopy and the region table's declaration; - PushField::RegionBox and RegionProbe; - SyntheticBinding::RateMapRegions and RateMapFlags, and WriteSource::RateMapRegions and RateMapFlags; - RuntimeFact::SynthesizedGeometry, RenderPipeline::runs_stage and Linked::geometry; - IoRole::Barycentric with VaryingKey::barycentric and its key tag; - VkProperty::SubgroupGeometryStage; - the SPIR-V IR's geometry execution model, which only these stages produced: ExecModel::Geometry, OpEmitVertex and OpEndPrimitive, the InputPoints, InputLines, OutputLineStrip, OutputTriangleStrip and Invocations modes, ModelNeed::Geometry, EntryRule::GeometryModes and the builder's geometry ops; - from scripts/regen-spirv-grammar/items.txt, the opcodes EmitVertex and EndPrimitive, the execution model Geometry and those five execution modes. spv.rs is regenerated: the opcodes and the model stay as test-only values, which the test parser reads, and the modes go; - in validation, the census's geometry stage filter, the draw's runs_stage filter and the candidate's GEOMETRY stage arm. The census test that read three geometry stages from engine/bary_weights goes with them. The SPIR-V builder now marks only Position Invariant; ClipDistance was the fan-out's. band::plan loses its geometry flag, and takes the four SPIR-V helpers it shared with the geometry module. The binder's stage map takes a geometry stage as no render stage. Validation cases that executed either form: none. - Fan-out: no validation code sets region_fan_out. - Weights: two authored cases read [[barycentric_coord]], fa8b920d3cf3 and ae0a7e39b12f. Neither has a stored Vulkan observation on a profile without fragmentShaderBarycentric. fa8b920d3cf3 is observed on Metal and MoltenVK, and moltenvk-m3 has the feature. ae0a7e39b12f is observed on Metal only. Fourth user of GeometryStages: none. It was used by link, the compile API's splice, the binder's selection (through RuntimeFact::SynthesizedGeometry and GraphicsState::region_fan_out) and its own tests. Tests: - engine_stage_io_tail's weights test is now without_the_feature_the_weights_are_refused: on lavapipe, ANV's Iris Xe, NVIDIA's Pascal (each with geometryShader) and runtime-floor (without it), both barycentric entries are refused needing fragmentShaderBarycentric. - engine_target_indices checks each pipeline's stages valid where it counted geometry stages, and loses the_weights_stage_beside_a_vertex_layer_is_not_implemented: that combination is now the plain refusal above. - engine_amplification no longer counts geometry stages. - The binder's render stage test refuses a geometry stage. - The builder's invariant test refuses ClipDistance. - engine_rotated_weights' rotated draw runs on moltenvk-m3 and RDNA3's RADV, each with fragmentShaderBarycentric, where it ran on lavapipe through the weights form. - The fact mechanism test numbers its 100 RuntimeFact variants densely again (PrivateArena, which development added, takes 99). Bless due until the next named pin, where the M3 bless job takes them; nothing is blessed here: - tests::public_fixtures_engine (the inventory): every [[barycentric_coord]] cell on a profile without fragmentShaderBarycentric becomes Unsupported(Barycentrics) needing it alone, where it compiled through the geometry stages (with geometryShader) or offered GeometryShader as the alternative; - tests::golden_hashes, and pipeline::compile::tests::the_old_render_descriptor_key_keeps_its_value (the render key), as under the commit below: the profiles' fingerprints change; - the authored compile census. SEMANTICS_VERSION 2247 -> 2248 and ARTIFACT_FORMAT_VERSION 583 -> 584 are placeholders that main renumbers. SEMANTICS: a fragment function's weights and every rate-mapped variant change form. ARTIFACT: the codec loses the RegionFanOut, FanOutState, GraphicsState::region_fan_out, IoRole::Barycentric, RuntimeFact::SynthesizedGeometry, PushField, SyntheticBinding and VkProperty tags. RUNTIME_VERSION stays: the runtime's render-pipeline kind no longer reads region_fan_out, which it never set, and its SynthesizedStages doc names the bands alone. runtime-sources.sha256 keeps development's (land-pkt re-pins it), api_signatures and api_surface drop the removed items, and docs/REFUSALS.md is regenerated. API.md drops the geometry stages from RenderPipeline's stages, its barycentric bullet and its band bullet. ARCHITECTURE.md's link line drops pipeline::link::geometry, and the runtime README's Barycentrics gap names the feature alone. Consumer check (consumer_check.sh, consumer development b76d6ac4a9): region_fan_out, RegionFanOut, FanOutState, region_copies, region_copy, RateMapFlags, RegionBox, RegionProbe, SynthesizedGeometry, FEW_COPIES, PROBE_DRAW_WORDS, RegionCopies and runs_stage have 0 hits. GeometryStages appears in crates/reims-vgpu-c/link-order.txt only. fan_out's hits are the consumer's own event fan-out in crates/reims-vgpu/src/graphics/decode/cmdbuf.rs, not this module. Verified (jobs read): - m3-max-61801: the suites at this change's work in progress (4f1f934423); its real reds were the mechanism numbering and the rotated weights on amd-rdna3-radv, both fixed here; the render key reads bless-due. - At the stack's tip over this change (dvl-5 1fa4aed963 and dvl-6 92fe40c38f): 61978 and 61987, fmt, clippy in every configuration, Rustdoc, each feature alone, the API docs and the Windows clippy, green; 61891, the suites at dvl-4: 13 failures, each a bless-due golden above or development's own (the raster state test, the inventory's sample_fill rows); 61968, the lavapipe device suite of metal2vulkan-validation, 1483 of 1483, and public case qualification Match; 62042, the metal2vulkan-validation suite at dvl-5, 1378 of 1378 passed. - States-only play of this change's player (p-gd.zst) over 35 distinct streams on gem10 (61919), um870 (61924), ms01 (61967) and um790 (62004): 456,384 render and 91,972 compute states made, 0 refused, every stream exit 0. The msi stream (windrag-780m-b2) is not played: msi had no memory free. Rebased onto development4cf936ed4d(selidx landed as3ecf606aa8). The conflicts: the version lines; the mechanism test, where development added RuntimeFact::PrivateArena; runtime-sources.sha256, where development's is kept; and api_signatures.tsv and docs/REFUSALS.md, both regenerated (signatures.py --bless, refusal_surface.py) and differing from development by this change's items alone. The jobs above ran at the old base; the rebased tip's are in the report to main.
3005 lines
234 KiB
Markdown
3005 lines
234 KiB
Markdown
# The API
|
||
|
||
`metal2vulkan` compiles Metal AIR into Vulkan pipelines through an API shaped like Metal's object
|
||
lifecycle. You hand it Metal-shaped inputs (a device description, a library, pipeline
|
||
descriptors, what a draw or dispatch binds) and get back plain data to create and record Vulkan
|
||
objects with: SPIR-V per stage, one pipeline layout, fixed-function state, and per-draw descriptor
|
||
writes and push-constant bytes. You never parse AIR, never patch SPIR-V, never derive binding
|
||
numbers from Metal indices, and never classify errors by message text.
|
||
|
||
This guide walks the flow on the repository's own fixtures. Every `rust` block is quoted from
|
||
[`examples/api_preview.rs`](../crates/metal2vulkan/examples/api_preview.rs), which the test suite
|
||
runs (`tests/api_preview_example.rs` fails when a block here stops matching it). Sections 9 and 10
|
||
also cover the runtime crate (`metal2vulkan-runtime`): the precision notes it carries and its deliberate
|
||
differences from Metal. The runtime's own guide is [its README](../crates/metal2vulkan-runtime/README.md).
|
||
|
||
## Stability
|
||
|
||
- **No feature needed.** Feature `ash` adds conversions to `ash::vk` and
|
||
`DeviceDescription::query`; `serde` serializes the plain-data values; `cli` builds the command.
|
||
- **Frozen names, additive changes.** The surface is the items at the roots of `error`, `device`,
|
||
`library`, `reflection`, `pipeline`, `bind`, `cache`, `metal` (with `metal::{format, sampler,
|
||
state, vertex}`), `vk` and `ash_interop`, plus `Budget` and `CancelToken` at the crate root.
|
||
`tests/api_surface.rs` pins every name. Structs and enums are `#[non_exhaustive]` or opaque, so
|
||
build values with `new`, `Default` or constants, assign fields, and match with a wildcard arm.
|
||
The exceptions are exhaustive on purpose: keys and Vulkan structs (`SlotRef`, `ResourceSlot`,
|
||
`ApiVersion`, ...), and the enums a consumer must interpret completely to bind and draw
|
||
correctly (`StageSource`, `PushHole`, `HeapSource`, `ImageSource`, `SamplerSource`,
|
||
`UseTarget`, `SlotClass`, `StorageAttachmentView`, `VertexStride`, `TileMapping`, `CopySource`,
|
||
`DrawCommand`, `PrecisionNote`, and Metal's `PrimitiveType`, `IndexType`, `CullMode`, `Winding`
|
||
and `TextureType`), where a new variant breaks your build on purpose.
|
||
- **Semver** covers this surface from 0.2.0 (still `0.x`: a breaking change bumps the minor
|
||
version).
|
||
- **Not API**: anything the reference does not show. Hidden items, every submodule of `library`,
|
||
`pipeline`, `bind` and `device` (write `pipeline::SlotRef`, never `pipeline::sym::SlotRef`), and
|
||
the translator core are private.
|
||
- **The AIR front end** (container, bitcode reader, typed IR, metadata ABI, intrinsics,
|
||
canonicalization) is the `metal2vulkan-air` crate, pinned exactly. The input and budget errors
|
||
this crate re-exports are that crate's types.
|
||
|
||
`scripts/api-docs` renders the reference of exactly this surface (add `--open` to open it); it
|
||
fails when a page links to a hidden or internal item.
|
||
|
||
## Run the tour
|
||
|
||
```sh
|
||
PATH=/opt/homebrew/opt/llvm/bin:$PATH cargo run -p metal2vulkan --example api_preview
|
||
```
|
||
|
||
No GPU is involved. The fixtures are committed as LLVM text, so the example assembles them with
|
||
`llvm-as` first; the API itself reads bitcode only.
|
||
|
||
## 1. A device
|
||
|
||
A `DeviceDescription` states the Vulkan physical device as you create it: API version, enabled
|
||
features and extensions, limits, subgroup and float-controls properties, format support and the
|
||
driver's identity. Three ways to get one:
|
||
|
||
- `DeviceDescription::from_khronos_json(json, profile_name)` reads a Khronos profiles JSON. The
|
||
tour uses the committed profiles under `crates/metal2vulkan-validation/profiles/`, from a bare
|
||
Vulkan 1.2 device to desktop GPUs and MoltenVK.
|
||
- `unsafe DeviceDescription::query(&instance, physical_device, instance_api_version)` (feature
|
||
`ash`) reads a physical device. That is what it supports: enable at least
|
||
`description.recommended_enables()` at `vkCreateDevice`, then pass
|
||
`with_enabled(&enabled)` so the description states what is enabled. `with_enabled` refuses a set
|
||
that breaks one of Vulkan's co-enable rules, naming its VUID. `VkFeature::exposure()` and
|
||
`VkExtension::requires(version)` say where each feature lives at the device's version, to chain
|
||
the create info.
|
||
- Build one field by field.
|
||
|
||
```rust
|
||
let description = DeviceDescription::from_khronos_json(&json, None)?;
|
||
let device = Device::from_queries(description, DevicePolicy::default())?;
|
||
```
|
||
|
||
`Device::from_queries` needs no Vulkan device: it derives the profile from the description's
|
||
queries alone, reading no vendor, device or driver identity. It refuses a device below Vulkan 1.2,
|
||
one whose `maxPushConstantsSize` is below the 256-byte push window every stage is lowered for
|
||
(`Unsupported{PushWindow}`, a `Limit` of `MaxPushConstantsSize`), and an inconsistent description
|
||
or policy. A
|
||
`Device` is cheap to clone; every compile and plan takes it.
|
||
|
||
### The policy
|
||
|
||
`DevicePolicy` holds your product choices; together with the description they decide everything a
|
||
compile does. `DevicePolicy::default()` is `DevicePolicy::UNTRUSTED`:
|
||
|
||
- **Bounds on a buffer-slot access at a run-time offset.** Such an access stays inside the range
|
||
the binder supplied: where the device enables `robustBufferAccess`, buffer accesses and vertex
|
||
input are left to it, and elsewhere the shader checks them. (MoltenVK advertises the feature
|
||
without implementing it; there such accesses are not bounded, as on native Metal.)
|
||
- **The device's arithmetic.** The device's own maths library, divisions, fused multiply-adds,
|
||
`float` denormals, rounding order of `dot`/`mix`/`fast::sqrt`, the grouping and fusion of
|
||
fast-math sums and products (each operation in the AIR's order, its own fast-math flags its
|
||
decoration), NaN bits, and the operand of `fmin`, `fmax`, `min3`, `max3`, `clamp` and
|
||
`saturate`, on every device and with no exact form.
|
||
Each records a precision note with its bound, on every device.
|
||
- **The device's native forms.** The device's own sampler (its filter, footprint, level,
|
||
anisotropy, texel conversion and comparison, a two-plane YCbCr texture's filter among them),
|
||
with no switch: the device's answer is the contract, recorded as `PrecisionNote::DeviceSampling`
|
||
with its measured bound. The device's own `half` rounding and its blend of a `half` color have
|
||
no switch either: each records its note.
|
||
- **The resident-table and handle-table domains** (`PointerDomain::ResidentTable`,
|
||
`HandleDomain::Table`, the default). The identity domains (`PointerDomain::Identity` with
|
||
`HandleDomain::Identity`, taken together) are the runtime's form: pointers are device
|
||
addresses used directly and a texture's or sampler's handle is its `gpuResourceID`
|
||
([RESOURCE-ID.md](RESOURCE-ID.md)). They need `bufferDeviceAddress` and `shaderInt64`. Under
|
||
them a buffer slot is reached at its own device address (`gpuAddress` plus the bind offset),
|
||
pushed less 2^31 as `PushField::SlotGuestBase` (section 5), with no root table and no
|
||
descriptor. The constant blob (the program-scope constants whose address merges, escapes or is
|
||
compared, or that are read through another type than their own) is the module's own there: a
|
||
`Private` array of its words, zero-padded to 16 bytes, which every load is checked against, and
|
||
no stage carries a `constant_blob`. A blob past 64 KiB is refused
|
||
(`Unsupported{IdentityDomains}`, naming the constant data's size). An access of the slot whose byte offset is not a
|
||
translation-time constant is checked against the slot's buffer (`PushField::SlotBounds`), from
|
||
the buffer's start to its end, as Metal's domain is the buffer: an offset before the bind
|
||
offset reads and writes the buffer's bytes there. Outside the buffer, a load reads zero and a
|
||
store or atomic does nothing (section 5). An access at a constant offset is not checked. A
|
||
pointer loaded from memory or made from an integer, null included, is used as given: no binding
|
||
bounds its accesses, so the bounds the default domains place on an access do not hold for
|
||
it. A slot
|
||
whose argument states an alignment of at least 4 (`air.arg_type_align_size`) is reached from
|
||
that address as it is: each access is at the pushed word plus its 32-bit byte offset plus 2^31,
|
||
modulo 2^32, zero-extended, which is the address plus the offset read as signed, exact as the
|
||
runtime makes no buffer longer than 2 GiB (`MAX_BUFFER_LENGTH`); a constant part of the
|
||
offset joins the 2^31 in one constant. An access of 8 or 16 bytes at an offset proven a multiple
|
||
of its size is one `uvec2` or `uvec4`. The function states `RuntimeFact::AddressAlignment` with
|
||
that alignment (at most 16), and the runtime refuses a dispatch whose bound address is not a
|
||
multiple of it, as Metal's API validation refuses the bind offset (an accepted difference,
|
||
section 10). A slot whose argument states an alignment below 4, or none, is reached the same way where each of
|
||
its accesses is one 1- or 2-byte unit no wider than that alignment (an `i8` access of a `uchar`
|
||
buffer, an `i16` one of a `ushort` buffer) on a device with that width's storage access. Any
|
||
other slot is reached from the word below each access, its offset sign-extended, so an access
|
||
before the bind offset reaches below the bind address as Metal's does. Fields past a
|
||
function's push window go to a parameter block in memory, read at the device address
|
||
`PushField::ArgumentTableAddress`, which the window always holds. The function reads the block
|
||
16 bytes at a time at multiples of 16 (its size, `argument_table_size`, is a multiple of 16) and
|
||
never writes it, so the consumer places it at a multiple of 16 and leaves it unchanged while
|
||
the command reads it. A kernel's window is 256 bytes, as the runtime's floor requires that much
|
||
`maxPushConstantsSize`: a dispatch region and 24 buffer addresses below its tail. A render
|
||
pipeline's two functions share one 256-byte range: the vertex function's window is `[0, 248)`,
|
||
and the fragment function's runs from the vertex function's end to 256. A post-tessellation
|
||
vertex function's window is `[24, 248)`, and its fragment function's starts at 24 or past: the
|
||
pipeline's control stage pushes its fields (8 to 24 bytes) at 0, so the pair's fields, which
|
||
spill where they pass their windows, never take its bytes. A
|
||
vertex function is lowered without knowing its fragment function, so it leaves the last 8
|
||
bytes, where a fragment function whose fields spill always pushes its block's address; that
|
||
costs only a vertex function whose own fields pass 248 bytes, which spills where it would have
|
||
filled them. The fields only a checked access reads
|
||
(`PushField::ZeroAddress` and each `PushField::SlotBounds`) come last in the order, so they
|
||
are the first to move to the block. Each function's push ends with its tail, 24 bytes or more above its other pushed fields: its highest
|
||
`PushField::SlotBounds` or `PushField::ZeroAddress`, moved there, or where it pushes neither, the
|
||
zero word `PushField::TailWord` at `PushPlacement::tail_word`, with which its `push_range` ends;
|
||
a function that pushes one field of 8 bytes or fewer has none. A driver that uploads a stage's
|
||
push bytes up to the highest one the stage reads then holds every load it widens from the other
|
||
fields. The function returns at its start where its tail holds what the consumer never pushes
|
||
there (a tail word other than zero, bounds whose two words are both all ones, a zero-range
|
||
address of all ones), so the consumer pushes the tail word zero with the other fields; the
|
||
bytes of the hole are not read. A kernel that reads a grid builtin reads `PushField::DispatchRegion` on
|
||
every dispatch: a dispatch the host knows states Metal's grid and its piece's first threadgroup
|
||
there, and an indirect dispatch a zero grid, whose kernel takes `NumWorkgroups` times its
|
||
workgroup size. It reads its first threadgroup from every region (0 where the
|
||
dispatch is not split, and for an indirect one) and declares `MultiRegion`, default 0: at 0
|
||
Metal's threadgroup size is the pipeline's workgroup size; at 1 it is the region's, for a region
|
||
whose workgroup size is not Metal's (an edge region). A kernel whose workgroup is its
|
||
threadgroup (one outside the flattened SIMD-group tiers, without compute derivatives) also
|
||
declares `SpecKey::DeepThreadgroup`, default false: at true its workgroup is one row of the
|
||
threadgroup's threads, `(sx·sy·sz, 1, 1)`, each thread's position its `LocalInvocationIndex`
|
||
unflattened in Metal's order, so a threadgroup deeper than a device's
|
||
`maxComputeWorkGroupSize[2]` runs; the binder sets it for a region deeper than
|
||
`pipeline::DEEP_THREADGROUP` (64) alone, and so does a runtime, so every other pipeline
|
||
specializes it away. `MetalDeviceProperties::max_threads_per_threadgroup` is
|
||
the capped `maxComputeWorkGroupInvocations` on every axis. A texture slot's `gpuResourceID` is pushed as
|
||
its `PushField::SlotGuestBase`, and its texture is the element at the ID's index of set 1's two
|
||
fixed arrays, the same layout for every pipeline ([RESOURCE-ID.md](RESOURCE-ID.md)); it binds no
|
||
descriptor of its own and declares no specialization constant. An argument buffer's texture is
|
||
the element at the index of the `gpuResourceID` its member holds, with no handle table, so a
|
||
slot and a member of one class index one array. No binder chooses a variant there: a
|
||
texture reads through the plain form whatever texture its ID names, its `float` or `half` reads
|
||
take the device's sRGB decode with `PrecisionNote::SrgbTexelDecode`, and no texture is sparse
|
||
(the consumer refuses a sparse texture). A texture slot sampled through a runtime sampler slot
|
||
samples set 1's element of the sampler arrays at the index of the sampler's pushed
|
||
`gpuResourceID`, joined to the texture's element by `OpSampledImage`, and reads the sampler's
|
||
word from that ID. One sampled through a constexpr sampler samples the element at the index the
|
||
consumer pushes for it (`PushField::ConstexprSampler`), where the consumer places the device
|
||
sampler `CombinedSampler::constexpr_sampler` describes, normalized: the shader scales a pixel
|
||
sampler's coordinate. An integer write to a storage slot without a format saturates to the channel
|
||
widths the consumer pushes (`PushField::TextureIntegerChannels`). A sample of a 2D texture
|
||
slot through a bicubic constexpr sampler at normalized coordinates reads how Metal's bicubic
|
||
filter treats the bound texture's texels (its inner taps, its clamp and its border texels) from
|
||
the word the consumer pushes for that slot (`PushField::BicubicTexels`:
|
||
`metal::format::bicubic_texels_word` of the bound texture's format and swizzle,
|
||
`metal::format::BICUBIC_TEXELS_UNBOUND` for a slot bound to nothing), the bits the default
|
||
domains specialize `SpecKey::BicubicTexels` to. `bicubic_texels_word` has no word for a
|
||
compressed, YUV, stencil, integer, extended-range or 8-bit sRGB format, so such a texture has
|
||
no bicubic sample there (the runtime refuses the dispatch, `Gap::BicubicTexels`). Its
|
||
arithmetic is noted `PrecisionNote::BicubicFilterArithmetic`, as on the default domains. A
|
||
fragment function under the pipeline's private alpha test
|
||
(`PrivateRenderState::alpha_test_enabled`, a compare function other
|
||
than `Never` or `Always`) compares its color 0's alpha with the code the consumer pushes
|
||
(`PushField::AlphaTestReference`): `bind::alpha_test_code` of the encoder's
|
||
`setAlphaTestReferenceValue:`, 1 until the encoder sets it. It is Metal's 8-bit unorm code of the
|
||
reference (a NaN taken as 0, clamped to `[0, 1]`, the exact product with 255 rounded to
|
||
nearest), the form the function converts its alpha to, so a consumer that pushes it compares as
|
||
Metal does. A fragment function's read of a color attachment (framebuffer fetch, section 4) on a
|
||
device with neither ordered form takes no input attachment: it reads, at the integer part of
|
||
its position (its own sample where the pass is multisampled), level 0, the sampled-texture
|
||
array's element at the index the consumer pushes for that attachment
|
||
(`PushField::FetchedColor { color, texels }`, `texels` the read's numeric class), and a
|
||
read-only signed-integer attachment joins `signed_color_attachments`. A vertex function that
|
||
writes `[[render_target_array_index]]` compiles in the variant that writes `Layer`, which stores
|
||
0 for an index at or past the render pass's length the consumer pushes
|
||
(`PushField::RenderTargetArrayLength`, `renderTargetArrayLength` at least 1). A kernel without a
|
||
call stack whose private bytes pass the 16 KiB floor every device holds keeps its private memory
|
||
in the consumer's device memory: every alloca it indexes at run time is in its private arena,
|
||
whose words for an invocation are the slot of `RuntimeFact::PrivateArena { slot_bytes }` (the
|
||
arena's bytes rounded up to 16) at the address the consumer pushes
|
||
(`PushField::PrivateArenaAddress`, a `u64`, a multiple of 16) plus `i * slot_bytes`, `i` the
|
||
invocation's index in its Vulkan dispatch, `((wz * ny + wy) * nx + wx) * n` plus its
|
||
`LocalInvocationIndex` (`n` the workgroup's invocations), counted in 32 bits. The consumer
|
||
supplies `count * slot_bytes` bytes for each dispatch of `count` invocations, and keeps a
|
||
dispatch's invocations under `2^32`. The memory needs no zeros: Metal's private memory is
|
||
undefined until the thread writes it. Every precision note is
|
||
static there: no constant or binder decides one. So far they lower a kernel that reaches memory
|
||
through its buffer slots and the device addresses it loads (an argument buffer's pointer
|
||
member, a stored `gpuAddress`) or makes from integers, and reads, writes, sizes or tests for nil
|
||
its texture slots and its argument buffers' textures, those of an argument buffer a pointer
|
||
member reaches included, reads and writes its `ulong` texture slots (an `RG32` unsigned image,
|
||
declared `Rg32ui` where it is a storage image, the element at its ID's index), updates its
|
||
texture slots atomically (a slot declared `R32ui` or `R32i`, the storage-image array's element
|
||
at its ID's index), and samples its texture slots
|
||
through its runtime sampler slots and its constexpr samplers and reads them through those (MSL
|
||
4.1's `read(sampler, coord)`, its border texel from bits 0-11 of the ID's word). Any other
|
||
function is refused as `Unsupported{IdentityDomains}`, whose symbol names the first such form
|
||
the function holds: one that samples through an argument buffer's sampler or samples an
|
||
argument buffer's texture, emulates a sampler's anisotropy, samples a 1D
|
||
texture or at pixel coordinates through a bicubic filter, uses a texture buffer, a slot's levels
|
||
or swizzle, a two-plane, extended-range or sRGB-twin form, reads an argument buffer's texture
|
||
through another numeric type or as raw texels, or uses any other state of a texture or sampler
|
||
beyond its ID.
|
||
An argument buffer's texture reads the element at its ID's index masked to its low 16 bits,
|
||
so a bad ID reads an element of the arrays ([RESOURCE-ID.md](RESOURCE-ID.md)); the change that
|
||
first lowers an argument buffer's sampler masks its index the same way.
|
||
|
||
Loops run as written: no execution bound is translated in.
|
||
|
||
### What the device answers
|
||
|
||
```rust
|
||
let device = committed_device("moltenvk-m3")?;
|
||
let enables = device.required_enables();
|
||
// ...
|
||
hex(&device.profile().fingerprint()),
|
||
// ...
|
||
println!(" Metal properties: {:?}", device.metal_properties());
|
||
```
|
||
|
||
- `required_enables()`: everything an artifact compiled for this device can need enabled.
|
||
- `metal_properties()`: the answers to the guest's device queries (thread execution width,
|
||
threadgroup limits, argument-buffer and read-write texture tiers, ...).
|
||
- `profile().fingerprint()`: a hash of every profile field, the policy included. Every cache key
|
||
includes it, so an artifact never serves another device. Facts that follow the machine's memory
|
||
(`DeviceDescription::allocation`: `maxMemoryAllocationSize`, `maxBufferSize`) are not profile
|
||
fields, so two machines that differ only there share artifacts.
|
||
- `profile().spirv_validation()`: the `spirv-val` environments and relaxations every module for
|
||
the device is valid under. The command line runs exactly these.
|
||
- The profile's facts: `enabled()`, `limits()`, `images()`, `fragment()`, `descriptors()`,
|
||
its `subgroups()` model and `simd_tier(...)`, the buffer robustness that bounds a buffer-slot
|
||
access (`robustness()`, `checks_buffers_in_shader()`), and the Function
|
||
storage a thread holds (`function_storage()`: `FunctionStorage::Measured` bytes for a measured
|
||
device, or `FunctionStorage::Assumed` with the 16 KiB floor, past which a vertex or fragment
|
||
function runs with `FunctionStorageAssumed` up to 192 KiB and any other stage is refused).
|
||
|
||
### Driver behavior
|
||
|
||
Where Vulkan leaves an answer to the driver, the profile takes the device's own answer, recorded
|
||
as a precision note, or, where the answer decides a capability, its emulation or a refusal.
|
||
`Device::from_queries` reads no driver identity, so no answer is taken as Metal's because a driver
|
||
was measured to give it.
|
||
|
||
Metal semantics the translation keeps on every device, beyond Vulkan's: Metal's 32-lane
|
||
simdgroups (and, under the resident-table domains alone, the order Apple's lockstep gives a
|
||
simdgroup's threadgroup and device accesses within a barrier interval). `fneg` and
|
||
`fabs` are the device's `OpFNegate` and `FAbs`, so a NaN's payload and a denormal are the
|
||
device's (`native-nan-encoding`, `device-denormals`). A NaN's bits, the
|
||
operand `fmin`, `fmax`, `min3`, `max3`, `clamp` and `saturate` pick beside a NaN or between two
|
||
zeros, a `half` read from its bits, which is the device's widening on every device (a NaN's
|
||
bits, and a denormal on a device that may flush 16-bit ones), and a `half` rounded from a value
|
||
below the smallest normal `half` on a device without `shaderFloat16` (its `OpQuantizeToF16`'s
|
||
zero) are the device's, each recorded as a precision note.
|
||
|
||
## 2. A library
|
||
|
||
`Library::parse` takes a metallib, a bitcode wrapper or a raw AIR module; `Library::from_air`
|
||
takes the last two. LLVM text is not an input: assemble `.ll` with `llvm-as` first. Listing
|
||
functions decodes nothing.
|
||
|
||
```rust
|
||
let sampling = Library::from_air(&fixture_bitcode("sampling")?)?;
|
||
for info in sampling.functions() {
|
||
println!("library: {} ({:?})", info.name, info.stage);
|
||
}
|
||
```
|
||
|
||
- `library.function(name)` finds a function like `newFunctionWithName:`; a missing name is
|
||
`Error::InvalidDescriptor(EntryNotFound)`.
|
||
- A metallib function's `info.air_hash` is the SHA-256 of its bitcode, its content key. In every
|
||
library but a Core Image kernel library it is the function's `HASH` tag, checked against the
|
||
bitcode: a mismatch is `InvalidInput(Container(HashMismatch))`, though Metal checks no `HASH`
|
||
and loads such a library. A Core Image kernel library (file type 1, `metallib -cikernel`)
|
||
lists its kernels as extern functions (`FunctionStage::Extern`, no stage) over one module, and
|
||
Apple's toolchain writes them a `HASH` that, for some modules, is not that module's SHA-256
|
||
(what it digests is not known); Core Image's own libraries are such. Its `HASH` is read as 32
|
||
bytes and kept as read, and `air_hash` is the module's SHA-256, computed. The conformance
|
||
crate's `probes/metallib_function_hashes.m` measures what Metal answers for both kinds of
|
||
library.
|
||
- `function.reflection(&budget)` answers as `reflectionForFunctionWithName:` does: the function's
|
||
arguments in declaration order with their types, access, indices and use, in Metal's shape
|
||
(module `reflection`). It is `None` for a metallib function whose library carries no reflection,
|
||
where Metal answers nil. An argument whose Metal answer was not measured is
|
||
`Unsupported(Feature::Reflection)`.
|
||
- A pipeline descriptor's `reflection(&budget)` answers what the `reflection:` out-parameter of
|
||
Metal's creation call does.
|
||
- A pipeline descriptor's `slot_writes(&budget)` answers, per stage, what the function may store
|
||
through the object bound at each slot its arguments bind (`SlotWrites`, one `SlotWrite` per
|
||
binding). `SlotWrite::may_write(slots, binding_type, index)` answers for one slot:
|
||
`SlotWritability::Never` where the declaration is read-only (a `constant` or `const device`
|
||
buffer, a texture declared `access::read` or `access::sample`, a sampler, a function table, an
|
||
acceleration structure, an imageblock, a mesh function's payload) and for a slot no argument
|
||
binds, `Maybe` where it is write-only or read-write, and `Unknown(SlotWriteGap)` for a slot that
|
||
one argument alone leaves undetermined.
|
||
|
||
The values are the ones `reflection` gives as `Binding::access`, worked out without the type
|
||
description a reflection must also give. That is why a state that compiles always answers here,
|
||
while its `reflection` is refused where Metal's answer for one argument's *type* was not
|
||
measured, and why uncertainty stays on the slot it belongs to instead of refusing the state:
|
||
a caller asking whether a read-only buffer may sit at a slot gets an answer per slot, and an
|
||
undetermined slot reads as neither writable nor read-only.
|
||
- `function.argument_buffer_layout(index, &constants, &budget)` gives the layout of the argument
|
||
buffer the function takes at a buffer index, as `newArgumentEncoderWithBufferIndex:` encodes it:
|
||
its length, alignment, and each argument's offset and type by `[[id(n)]]`.
|
||
`ArgumentBufferLayout::from_descriptors` gives the layout `newArgumentEncoderWithArguments:`
|
||
makes of a list of `ArgumentDescriptor`s. Each argument is 8 bytes: a buffer's address plus
|
||
offset, or a resource ID.
|
||
- `function.properties(&budget)` answers what `MTLFunction` states about the function.
|
||
|
||
### One entry of a bare module
|
||
|
||
A tool that holds bitcode and an entry name, not a library, compiles through the profile:
|
||
|
||
```rust
|
||
let bitcode = fixture_bitcode("sampling")?;
|
||
let entry = EntryDescriptor::new(&bitcode, EntryRef::Name("sample_level"));
|
||
let profile = device.profile();
|
||
let (compiled, report) = profile.compile_entry(&entry, &LoweringInputs::new(), &budget);
|
||
let alone = profile.link_standalone(&compiled?)?;
|
||
```
|
||
|
||
`link_standalone` links the function with no partner stage. The report says what the compile cost
|
||
against its budget, whether it succeeded or not.
|
||
|
||
## 3. A compute pipeline
|
||
|
||
Descriptors mirror Metal's. Each stage is a `StageSource`: a `FunctionDescriptor` to compile (a
|
||
function, its constant values, the functions it links) or a `CompiledFunction` to link as it is.
|
||
Every compile takes a `Budget`:
|
||
|
||
```rust
|
||
let cancel = CancelToken::new();
|
||
let mut budget = Budget::standard();
|
||
budget.cancel = Some(cancel.clone());
|
||
let kernel = FunctionDescriptor::new(sampling.function("sample_level")?);
|
||
let descriptor = ComputePipelineDescriptor::new(StageSource::Function(kernel));
|
||
let pipeline = device.compile_compute_pipeline(&descriptor, &budget)?;
|
||
```
|
||
|
||
`Budget::standard()` is a 20-second deadline. `cancel.cancel()` from any thread ends the attempt
|
||
at its next poll point with `Error::Cancelled`; a passed deadline is `Error::Budget`. Both are
|
||
transient. Deterministic caps (work units, a memory ledger, structural limits) apply whatever the
|
||
budget, and exceeding one is `Error::LimitExceeded`, permanent for that input.
|
||
|
||
A `ComputePipeline` is immutable plain data:
|
||
|
||
- `stage`: a `StageModule` (SPIR-V 1.5 words valid for the device, the entry point, specialization
|
||
constants, a required subgroup size and stage flags, precision notes, `private_bytes`, the
|
||
private memory a thread keeps, as `StageInterface::private_bytes` states it too, and `stats`,
|
||
what the module contains);
|
||
- `layout`: the pipeline layout, dense and complete; create it as it is;
|
||
- `interface`, `dependencies`: what it binds and what bind-time state it depends on (section 5);
|
||
- `metal_info`: answers to Metal's per-pipeline queries (`maxTotalThreadsPerThreadgroup`, ...);
|
||
- `requirements`: what the device must have enabled.
|
||
|
||
Do not create the compute `VkPipeline` from `stage` alone: its specialization defaults are
|
||
placeholders (a kernel's local size is 1). Create each pipeline from a planned dispatch's
|
||
`DispatchRegion` (section 6), with the region's `specialization`, `stage_flags` and
|
||
`required_subgroup_size`. A required subgroup size needs `subgroupSizeControl` and the stage in
|
||
`requiredSubgroupSizeStages`; full subgroups need `computeFullSubgroups`. A pipeline compiled for
|
||
one device pins only what that device can take; `DeviceProfile::check` tells whether another
|
||
device can.
|
||
|
||
A kernel whose simdgroups run on 32-lane subgroups pins a required size of 32 with full
|
||
subgroups. On a device whose compiler chooses a kernel's SIMD width between a `minSubgroupSize`
|
||
below 32 and a `maxSubgroupSize` of 32 (ANV), that pin compiles the kernel in the device's widest
|
||
mode, where each lane's value takes four times the registers of the narrowest one. A kernel whose
|
||
entry, inlined, holds more than 256 AIR instructions and whose values are estimated to hold more
|
||
than 36 32-bit words at once (live intervals over the body in layout order, a value read in a loop
|
||
held to the loop's end) takes the emulated simdgroup tier there instead: each simdgroup is 32
|
||
consecutive threads, its operations and lockstep barriers go through workgroup memory and
|
||
workgroup barriers, and its module pins no subgroup size, so the driver chooses the width. Both
|
||
forms are exact. The runtime counts such kernels (`WorkCounts::widest_pin_kernels_emulated`). A
|
||
kernel the emulated tier refuses (a 64-bit exchange, or a scratch that does not fit beside its
|
||
threadgroup memory) keeps the pin.
|
||
|
||
The bounds come from an authored kernel measured on an Iris Xe (RPL-P, ANV): `S` steps, each a
|
||
branch on one of `K` live `float` accumulators whose arm stores a device word and loads the next
|
||
(a lockstep pair in divergent flow, so a subgroup barrier there), with `P` 1 the arm's accesses
|
||
going through a pointer it loads from memory (a resident-set search a step), then every
|
||
accumulator updated. Pressure, not size, separates the cases. The driver's compile time
|
||
(`vkCreateComputePipelines`) and the GPU time of a dispatch of 12,288 threads, pinned and
|
||
emulated, one run each (compile times varied up to twice between runs):
|
||
|
||
| `P K S` | AIR instructions | estimated words | compile ms, pinned | emulated | GPU ms, pinned | emulated |
|
||
|------------|-----------------:|----------------:|-------------------:|---------:|---------------:|---------:|
|
||
| `0 16 128` | 7,850 | 24 | 589 | 559 | 0.129 | 0.159 |
|
||
| `1 4 32` | 874 | 34 | 783 | 766 | 0.010 | 0.018 |
|
||
| `0 32 64` | 7,050 | 40 | 6,652 | 282 | 1.061 | 0.107 |
|
||
| `1 16 16` | 1,042 | 46 | 971 | 300 | 0.070 | 0.015 |
|
||
| `1 16 64` | 4,066 | 46 | 11,987 | 2,585 | 0.319 | 0.074 |
|
||
| `0 48 8` | 1,362 | 56 | 458 | 38 | 0.168 | 0.021 |
|
||
| `1 48 32` | 5,186 | 78 | 25,799 | 1,277 | 1.985 | 0.081 |
|
||
|
||
At 34 estimated words or fewer the two compile alike and the pinned kernel runs as fast or
|
||
faster; from 40 the pinned compile took 1.6 to 24 times the emulated one and its GPU time 1.5 to
|
||
25 times. At or below 256 instructions every pinned compile measured took at most 102 ms. Answers
|
||
were the same bits on both tiers. The estimate costs time linear in the body: 5 ms of a 3.4 s
|
||
release translation of a 162,914-instruction kernel.
|
||
|
||
On the emulated tier, a simdgroup operation or barrier in a block whose control flow differs
|
||
between threads is walked predicated: each thread carries a live flag, the threads that do not run
|
||
the block take part in its exchanges as inactive lanes, and a loop that holds one votes on its
|
||
exit. A kernel whose threads' positions alone make such a block differ, along some axes
|
||
(`thread_position_in_threadgroup` or `thread_position_in_grid`, read by lane), states
|
||
`RuntimeFact::UnitAxes { axes: 0, relief }`: `relief` holds, at bit `m - 1`, each mask `m` of axes
|
||
(bit 0 `x`, bit 1 `y`, bit 2 `z`) under which fewer of those blocks differ once the positions along
|
||
them are taken as uniform. In a threadgroup of one thread along an axis, Metal's
|
||
`threads_per_threadgroup` is 1 there, so every thread's `thread_position_in_threadgroup` on that
|
||
axis is 0 and its `thread_position_in_grid` is its threadgroup's position, partial threadgroups
|
||
included. `BindingPlan::unit_axes_variant(&dispatch)` offers a dispatch of such threadgroups,
|
||
where its axes are among the masks of `relief`, the kernel's unit-axes variant
|
||
(`VariantKey::unit_axes`, which `LoweringInputs::unit_axes` compiles): the same kernel with those
|
||
positions uniform, walked without the predication they caused, giving the same answers. Its plan
|
||
states `UnitAxes { axes, relief: 0 }` and sends a dispatch with more than one thread along one of
|
||
its axes back to the variant without them (`BindError::NeedsVariant`). A kernel has at most seven
|
||
such variants per variant it runs on, one per mask. On the Iris Xe, an authored kernel of four shuffles each way and a
|
||
simdgroup sum in a loop of 8 to 11 trips, in threadgroups of 8 x 1 x 16, ran 1.14 to 2.16 times
|
||
the GPU time predicated (its trip count read from `thread_position_in_grid.y`) as with the same
|
||
trip counts read from `threadgroup_position_in_grid.y`, over four sizes from 46 to 126 estimated
|
||
words (one run each), with the same bits.
|
||
|
||
### What the module contains
|
||
|
||
Every stage states the size and shape of its module, counted by the verifier that runs on every
|
||
translation, in every build: `stage.stats`, a `ModuleStats`. A function compiled on its own states
|
||
the same counts as `module_stats()`, before it is linked. Reading them is how a caller watches the
|
||
size of what it hands a driver, and what in it grew.
|
||
|
||
```rust
|
||
let stats = pipeline.stage.stats;
|
||
println!(
|
||
"{} blocks, {} instructions",
|
||
stats.blocks, stats.instructions
|
||
);
|
||
assert!(stats.reachable_blocks <= stats.blocks);
|
||
```
|
||
|
||
- `blocks` and `instructions` are the module's size: a larger module costs more to compile on the
|
||
drivers measured. Canonicalization inlines every direct call before the module is emitted, so
|
||
they count the whole program, not one function.
|
||
- `functions` -- the entry point and the functions left beside it -- says how far `blocks` stands
|
||
for that program. A driver inlines before it charges, so a module of few functions is one whose
|
||
blocks are the program's, while a module that calls a many-block function from many sites holds
|
||
a program far larger than its own blocks; the two read alike in `blocks` alone. Lowering emits
|
||
such functions itself: the handle-table search (unrolled branch-free steps) and the root-table
|
||
search (a loop), each emitted once and called at every access.
|
||
- Compare one module against the next of the same shader, not one shader against another. Measured
|
||
cost per block differed several-fold between a synthetic module and a real workload, so a
|
||
per-block figure taken from one shader predicts nothing for the next; `instructions` is the
|
||
second number for that reason.
|
||
- `constructs` (selection, switch and loop headers) and `loops` say whose blocks those are.
|
||
Translation adds blocks of its own -- inlining, and the structurizer's layout of control flow as
|
||
SPIR-V requires -- and the input has blocks of its own. The structurizer makes no loop the input
|
||
does not have, so a `blocks` that has risen while `loops` holds still is translation's work or a
|
||
changed shape of the same control flow, not new control flow in the shader.
|
||
- `reachable_blocks` is a bound, not a lever. It is never above `blocks`, and below it by the merge
|
||
blocks SPIR-V requires a construct to declare that no path reaches -- both arms of a conditional
|
||
inside a loop leaving the construct, say -- which the module terminates with `OpUnreachable`. The
|
||
difference is blocks the driver still reads and no invocation runs.
|
||
- The AIR side of the comparison is yours, through `metal2vulkan-air`. For the counts the input
|
||
itself holds, before inlining, decode it and count the blocks of each function's body
|
||
(`metal2vulkan_air::decode`, then each `ir::Function`'s `body`); for the ones canonicalization
|
||
produced, walk the entry `DeviceProfile::specialized_entry` gives, which is that AIR specialized
|
||
and canonicalized. A decoded function's `ir::InstKind::Call` sites give each callee's call
|
||
count, which is what turns its own blocks into the program inlining will make of it.
|
||
- `inert` says the module holds no loop and that no instruction of it writes memory other than an
|
||
invocation's own and its workgroup's (`Function`, `Private` and `Workgroup` storage), writes an
|
||
image, or emits or drops an output of its stage. Every invocation of such a module ends and
|
||
leaves nothing behind, so a dispatch of an inert kernel computes nothing anyone can read. An
|
||
instruction the
|
||
check does not classify as reading or computing alone (an interlock, a ray query) makes a
|
||
module not inert.
|
||
|
||
The counts are of the module `spirv` holds: the verifier counts the module it checked, and
|
||
serialization and relocation assign link numbers without adding or removing an instruction. They
|
||
are part of the encoded artifact, so a cache hit states them too, and they are left out of
|
||
`content_digest`, which already covers the words they describe.
|
||
|
||
### A kernel with `[[stage_in]]`
|
||
|
||
A kernel's `[[stage_in]]` attributes are fetched in the shader, as Metal does. The compile asks for
|
||
the stage-input descriptor it needs, and you supply it:
|
||
|
||
```rust
|
||
match device.compile_compute_pipeline(&descriptor, budget) {
|
||
Err(Error::NeedsInput(RuntimeInput::StageInputDescriptor { attribute })) => {
|
||
let mut stage_input = StageInputDescriptor::new();
|
||
let fetch = StageInputAttributeDescriptor::new(AttributeFormat::Float, 0, 1);
|
||
stage_input.attributes.insert(attribute, fetch);
|
||
let mut layout = StageInputLayoutDescriptor::new(Some(4));
|
||
layout.step_function = StepFunction::ThreadPositionInGridX;
|
||
stage_input.layouts.insert(1, layout);
|
||
descriptor.stage_input = Some(stage_input);
|
||
}
|
||
// ...
|
||
let mut inputs = LoweringInputs::new();
|
||
inputs.stage_input = descriptor.stage_input.clone();
|
||
let compiled = device.compile_function(&kernel, &inputs, budget)?;
|
||
```
|
||
|
||
A fresh `StageInputLayoutDescriptor` steps per vertex at rate 1, as a fresh Metal layout does. A
|
||
layout stride of `None` is `MTLBufferLayoutStrideDynamic`: the stride comes from the encoder.
|
||
|
||
A kernel that takes a patch control-point input (`air.patch_control_point_input`, the input of a
|
||
post-tessellation vertex function) is refused, `InvalidInput(Abi(UnknownKey))` at that argument and
|
||
key. Metal's compiler writes that input for no kernel, and Metal makes no compute pipeline of a
|
||
kernel that takes one: on an M3 Max under macOS 27.0.1 it makes the library and the function, then
|
||
fails the pipeline with "Internal compiler error." under every compute step function. A kernel reads
|
||
vertices by index through `[[stage_in]]` under a stage-input descriptor (one element per thread), or
|
||
through buffer arguments it reads at any index.
|
||
|
||
### Other compute features
|
||
|
||
- **Visible functions and recursion.** Linked visible functions, function tables and
|
||
intersection function tables compile; recursion through visible functions runs on a stack of
|
||
frames sized by `max_call_stack_depth`. A call that finds the stack full returns zero.
|
||
- **Linked functions: names and constants.** A visible-function reference resolves to the linked
|
||
function whose `LinkedFunction::name` it names: its specialized name where it was made with one
|
||
(`with_specialized_name`, Metal's `MTLFunctionDescriptor.specializedName`), else its library
|
||
function's own; its body is still that library function's. A linked function whose module
|
||
declares function constants is specialized with its own values before it is linked, its
|
||
module's static initializer run over them. A function made specialized
|
||
(`LinkedFunction::specialized`: with constant values, or by `newFunctionWithDescriptor:` with or
|
||
without them) reads a constant it was given no value for as an entry's undefined constants read
|
||
(section 10, "Function constants"). One made unspecialized (`newFunctionWithName:`) that reaches
|
||
one of its module's constants refuses the pipeline at a call that survives specialization,
|
||
`DescriptorError::LinkedFunction` with `LinkProblem::UnresolvedConstant { index }`, guarded by
|
||
`is_function_constant_defined` or not, as Metal fails the creation ("cannot lower module with
|
||
unresolved function constants"); one that reaches none links. The specialized name and the
|
||
specialization are part of the compile key. A reference resolves against `functions` and
|
||
`private_functions` (`LinkedList::resolves_references`), never against `binary_functions`,
|
||
whose functions only a visible-function table calls: a reference only a binary function names
|
||
is `NeedsInput(LinkedFunction)`, as Metal fails the creation ("unresolved visible function
|
||
reference"). Two different functions of one name, one in `functions` and one in
|
||
`binary_functions`, are two functions a table holds, each with its handle, and the reference
|
||
calls the one of `functions`, as Metal links them.
|
||
- **Linked functions: types.** A reference resolves to the linked function of its name whatever
|
||
the definition's type is, and Metal makes every such pipeline (the probe
|
||
`linked_function_signatures.m`, an M3 Max under macOS 27.0.1). The call passes its arguments by
|
||
position: one past the definition's last parameter is dropped, and a parameter past the
|
||
reference's last argument reads zero. An argument of another plain-value type (an integer or
|
||
floating-point scalar or vector, a `bool` as one bit) passes its bits: all of them at the same
|
||
width, its low bits to a narrower parameter, its bits with zeros above to a wider one. The result
|
||
passes back the same way; a definition returning nothing returns zero, and a reference returning
|
||
nothing drops the result. A struct a `[[visible]]` function takes by value reaches it as its
|
||
fields, so a reference passing the fields one by one is passing it. A pointer for a value or a
|
||
value for a pointer, a pointer of another address space, and an aggregate of another type refuse
|
||
the pipeline at a call that survives specialization, `Unsupported` with `Feature::Linking`
|
||
(section 10, "Linked functions").
|
||
- **Stitched functions.** A function Metal's stitcher made (`newLibraryWithStitchedDescriptor:`)
|
||
links like any other visible function, in `functions` or in `privateFunctions`: hand the
|
||
stitched library's bytes to `Library::from_metallib`, and link its function. Each stitched
|
||
function is a module of its own whose `!air.signature` describes the graph it was made from;
|
||
the module's own code, the graph's nodes inlined, is what is linked. The translator makes no
|
||
stitched library itself. A module whose linkage signature has no reading (a linked-constants
|
||
signature on a module that still declares function constants) refuses the pipeline at a call
|
||
that survives specialization, `Unsupported` with `Feature::Linking`.
|
||
- **Threadgroup memory.** No threadgroup memory access is
|
||
bounded: a load or store, atomic, memory intrinsic, pointer cell or imageblock access of a
|
||
threadgroup arena, and a typed threadgroup object's index, run as the program states them, and
|
||
one past the memory is undefined, as in Metal. A kernel whose every threadgroup access is a
|
||
whole, aligned `float` (a scalar or a vector of two to four), with no atomic, memory intrinsic,
|
||
imageblock or pointer kept in it, and whose SIMD groups are not emulated, holds its threadgroup
|
||
memory as a `Workgroup` array of `float`: each access is one typed load or store a lane, with no
|
||
branch and no bitcast, at the word `offset / 4` and the words after it. The array is the
|
||
memory's own words (the static part and the specialized `[[threadgroup(n)]]` lengths), at least
|
||
four: the memory Metal allocates, so its threadgroup memory is Metal's static length and lengths,
|
||
and a length filling the limit runs. The strategy census names its accesses
|
||
`access.threadgroup.load.floats` and `access.threadgroup.store.floats`. Every other kernel holds
|
||
`u32` words.
|
||
- **Threadgroup memory before the program writes it.** A function's threadgroup memory reads zero until the program writes it: the `Workgroup` variables
|
||
that hold it, its threadgroup arena (declared threadgroup variables, `[[threadgroup(n)]]`
|
||
arguments and an explicit imageblock) and its typed threadgroup objects, have a null
|
||
initializer, in every threadgroup of every dispatch. That needs
|
||
`shaderZeroInitializeWorkgroupMemory`, which Vulkan 1.3 requires; a function that uses
|
||
threadgroup memory on a profile without it is `Unsupported` with
|
||
`Feature::ZeroedThreadgroupMemory`. The translator's own `Workgroup` words have none: an
|
||
emulated SIMD group's exchange scratch and vote word, an object function's mesh grid, a mesh
|
||
function's primitive count and staged indices, and those of the binder's own shadow-merge and
|
||
index-rewrite kernels. Each is written
|
||
before any read of it, and an object function's payload "is assumed uninitialized at the entry
|
||
of the object function" (the Metal Shading Language Specification, the `payload` attribute). Metal's documents give
|
||
threadgroup memory no first contents: `MTLComputeCommandEncoder.h`'s
|
||
`setThreadgroupMemoryLength:atIndex:` reads "Set the threadgroup memory byte length at the
|
||
binding point specified by the index. This applies to all compute kernels.", Apple's page for it
|
||
"Configures the size of a block of threadgroup memory." and points to the specification's
|
||
section 4.4, which says "Threadgroup variables in a kernel, mesh, or object function only exist
|
||
for the lifetime of the threadgroup that executes the kernel." and nothing of what they hold
|
||
first. An Apple M3 Max under macOS 27.0.1 reads zero there, a declared array and a
|
||
`[[threadgroup(n)]]` argument alike, in the later threadgroups of a dispatch of 2048
|
||
threadgroups of 16 KiB each, many more than its cores hold at once, and in a second dispatch
|
||
(the conformance case `compute-threadgroup-memory-reads-zero`); recorded programs read entries of
|
||
it they have not written.
|
||
- **Binary functions, required threadgroup sizes, buffer mutability.** Whether a binary function
|
||
was compiled to a binary (`LinkedFunction::precompiled`), required threadgroup sizes and buffer
|
||
mutability change no translation and are in no compile key; the runtime checks them as Metal
|
||
does.
|
||
- **Dynamic libraries.** `DynamicLibrary::new(library, &budget)` checks a dynamic metallib as
|
||
Metal's `newDynamicLibrary:` does; pipelines link its exported functions.
|
||
- **OpenCL C kernels.** Metal compiles OpenCL C (`newLibraryWithSource` with the compile options'
|
||
`sourceLanguage` 1, which the GL renderer's compute path uses), naming each kernel
|
||
`__entry_<name>`, and its AIR has shapes MSL's does not:
|
||
- A helper left out of line that passes its parameter on as an intrinsic's immediate operand:
|
||
OpenCL C's `barrier(flags)` passes its flags to `air.wg.barrier`, and `mem_fence(flags)`
|
||
computes `air.atomic.fence`'s flags from them with `llvm.bitreverse.i2`. Each inlined copy
|
||
reads its own call site's constants, folded through the integer arithmetic between them. An
|
||
immediate that is still not a constant after that is
|
||
`InvalidInput(Intrinsic(ImmediateNotConstant))` at the call. `llvm.ctpop`, `llvm.bitreverse`,
|
||
`llvm.bswap` and the scalar `llvm.umin`, `llvm.umax`, `llvm.smin` and `llvm.smax` are folded
|
||
over constants, on a width no SPIR-V type has (`i2`) too; an integer `llvm.*` operation on such
|
||
a width that is not folded is refused (`Lowering(Arith)`).
|
||
- An argument passed by value (a kernel's scalar, vector or struct parameter, `air.constant`):
|
||
the entry reads it from a constant-space pointer at its location, so it is a read-only
|
||
constant buffer holding its own bytes, bound with `set_bytes` or a buffer at an offset. The
|
||
reflection lists it as such a buffer of its own data type, size and alignment, a struct as
|
||
`Struct` with its members, as the M3 Max answers: `float k` is `Float`, 4 bytes aligned to 4,
|
||
and `struct { float a; int b; }` is `Struct`, 8 bytes aligned to 4.
|
||
- A required work-group size (`reqd_work_group_size(x, y, z)`, `air.work_group_size`) caps
|
||
the pipeline's `max_total_threads_per_threadgroup` at `x * y * z` threads, as the M3 Max
|
||
answers (64 for `(64, 1, 1)`). A dispatch in smaller threadgroups runs, as it does there.
|
||
- OpenCL mode. Metal makes a pipeline of such a kernel only with
|
||
`ComputePipelineDescriptor::open_cl_mode_enabled` (`openCLModeEnabled`, Apple SPI), and the
|
||
crate compiles the kernel as Metal links it then (`LoweringInputs::open_cl_mode`, part of the
|
||
key). Three hidden buffers come before the kernel's own: `__global_offset_and_num_dims` at
|
||
index 0, four `uint`s the dispatcher fills with the dispatch's global offset in each of its
|
||
dimensions and its dimension count, `__printf_buffer` at 1 and
|
||
`__image_order_data_type_table` at 2. Each argument the kernel binds at a buffer index sits 3
|
||
past its own; threadgroup memory (`__local`), textures and samplers keep theirs. The
|
||
reflection lists the kernel's arguments at those indices, then the three buffers (read-only
|
||
`uint` 4 bytes, read-write `char` 1 byte, read-only `uint` 4 bytes), each used, as the M3 Max
|
||
answers. OpenCL C's work-item functions read the dispatch: `get_global_id(d)` is the thread's
|
||
position in the grid plus the offset in `d`, `get_global_offset(d)` and `get_work_dim()` read
|
||
buffer 0, `get_local_id`, `get_group_id`, `get_local_size`, `get_global_size` and
|
||
`get_num_groups` are the built-ins of those names, and a dimension the kernel reads from its
|
||
data selects among the three. A dimension past 2 answers OpenCL's 0 for an id or an offset
|
||
and 1 for a size or a count. A compiled kernel serves only a pipeline of the mode it was
|
||
compiled in (`RuntimeFact::OpenClMode`, `InvalidDescriptor(OpenClMode)` otherwise). Without
|
||
the mode Metal refuses the pipeline ("Internal compiler error"); the crate compiles the AIR as
|
||
Metal AIR, its buffers at their own indices, and refuses a read of `get_global_offset` or
|
||
`get_work_dim` (`Unsupported` with `Lowering(Dispatch)`).
|
||
- Images in OpenCL mode. `get_image_channel_order(image)` and
|
||
`get_image_channel_data_type(image)` (`air.get_channel_order_texture_<kind>` and
|
||
`air.get_channel_data_type_texture_<kind>`, `Query::ChannelOrder` and
|
||
`Query::ChannelDataType`) read the third hidden buffer as it holds them: word `2t + 1` for the
|
||
order and word `2t` for the data type of the image at texture index `t`, as an M3 Max (macOS
|
||
27.0.1) and a Radeon Pro 5300M (macOS 26.7.1) answer. Metal refuses the queries of a buffer or
|
||
a depth image, and the crate decodes none (`UnknownIntrinsic`); without the mode it refuses
|
||
each query (`Unsupported` with `Lowering(Texture)`). Apple's OpenCL binds a 2D texture of
|
||
height 1 for an `image1d_t`, which the AIR declares `texture1d`, and a 2D array texture of
|
||
height 1 for an `image1d_array_t` (`texture1d_array`). In the mode the kernel's slot is that
|
||
texture's type (`TextureType::Type2D`, `Type2DArray`), and each read, sample and write of the
|
||
image, `get_image_width` and `get_image_array_size` is the 2D texture's at row 0 (a sampled
|
||
coordinate at the row's centre), as both Macs answer. A real 1D texture bound for an
|
||
`image1d_t`, or a 1D array texture for an `image1d_array_t`, which Apple's OpenCL never binds,
|
||
takes the kernel's variant that declares that slot as the 1D texture
|
||
(`RuntimeFact::OpenClLineTexture`, `VariantKey::open_cl_lines`): each read, sample and size
|
||
query answers the 1D texture's values, of the layer an array access names, as a Radeon Pro
|
||
5300M (macOS 26.7.1) answers (an Intel UHD Graphics 630 reads layer 0 of the array, an M3 Max
|
||
texel 0 at every coordinate). Apple's OpenCL binds an image of most
|
||
channel orders as a swizzled view (on an M3 Max, macOS 27.0.1, with `CL_UNORM_INT8`: `CL_R`,
|
||
`CL_RG`, `CL_A`, `CL_RA` and `CL_INTENSITY` as `R8Unorm` or `RG8Unorm` views, `CL_BGRA`,
|
||
`CL_ARGB` and `CL_RGB` as `RGBA8Unorm` views). A read (with a sampler or without) answers the
|
||
swizzled texel, a write stores it with the swizzle undone (each channel from the first lane
|
||
whose source it is), and a query answers the view's size and the hidden buffer's words, which
|
||
no swizzle reaches, as plain Metal answers for the same view; the order is never applied twice.
|
||
A storage slot the kernel only queries binds the texture's view with no swizzle. The reflection
|
||
states each image argument as the texture bound for it, an `image1d_t` `TextureType::Type2D`
|
||
and an `image1d_array_t` `Type2DArray`, of data type `DataType::None` (the AIR names its component
|
||
`unknown`) whatever the kernel reads it as, as an M3 Max (macOS 27.0.1) answers for 1D, 1D
|
||
array, 2D and 3D images read as float, int, uint and half, written, only queried and unused.
|
||
- OpenCL C's `native_<function>` and `half_<function>` of `float` types
|
||
(`air.native_<stem>.<type>` and `air.half_<stem>.<type>`, for `cos`, `exp`, `exp10`, `exp2`,
|
||
`log`, `log10`, `log2`, `powr`, `rsqrt`, `sin`, `sqrt` and `tan`, scalar and of every vector
|
||
width) are the function's fast form, as `fast::<function>` is (`Precision::Fast`). OpenCL C
|
||
leaves their precision to the implementation, and Metal's devices differ:
|
||
`native_powr(100, 0.5)` is `0x1.400002p+3` on the M3 Max and `0x1.4p+3` on the Radeon Pro
|
||
5300M.
|
||
- OpenCL C's vector data functions (`vload<n>`, `vstore<n>`, `vload_half[<n>]`,
|
||
`vloada_half<n>`, `vstore_half[<n>][_<rounding>]` and `vstorea_half<n>[_<rounding>]`, which
|
||
the compiler spells `air.vload[a][_half].<space>.<type>` and
|
||
`air.vstore[a][_half].<space>[.<rounding>].<type>`, `VectorData`) read and write `n` elements
|
||
at `p + offset * n` (`offset * 4` for `vloada_half3` and `vstorea_half3`), each at its own
|
||
element's alignment, in every address space and of every element type the compiler spells.
|
||
`vload3` of a `uint` pointer one element past a buffer's start reads the three words that
|
||
follow, as the M3 Max, a Radeon Pro 5300M and a UHD Graphics 630 (macOS 26.7.1) do.
|
||
`vstorea_half3` writes three halves of each four, as the M3 Max and the UHD Graphics 630 do
|
||
(the Radeon Pro 5300M's writes the fourth too). The `half` forms widen each `half` read to
|
||
`float` and narrow each lane stored to `half`; a store's rounding is an accepted difference
|
||
(section 10, Conversions).
|
||
|
||
## 4. A render pipeline
|
||
|
||
```rust
|
||
let mut vertex_descriptor = VertexDescriptor::new();
|
||
let layout = VertexBufferLayoutDescriptor::new(Some(24));
|
||
vertex_descriptor.layouts.insert(0, layout);
|
||
// ...
|
||
let mut descriptor = RenderPipelineDescriptor::new(StageSource::Function(vertex));
|
||
descriptor.fragment = Some(StageSource::Function(fragment));
|
||
descriptor.vertex_descriptor = Some(vertex_descriptor);
|
||
let color = ColorAttachmentDescriptor::new(PixelFormat::RGBA8Unorm);
|
||
descriptor.color_attachments[0] = Some(color);
|
||
// ...
|
||
let pipeline = device.compile_render_pipeline(&descriptor, &Budget::standard())?;
|
||
```
|
||
|
||
A `RenderPipeline` holds:
|
||
|
||
- `stages`: vertex, then fragment when rasterization is enabled;
|
||
- `layout`: one pipeline layout for every stage;
|
||
- `vertex_input`: Vulkan bindings and attributes. An attribute Vulkan cannot fetch as described is
|
||
pulled in the shader instead, and so is every attribute on a device without
|
||
`robustBufferAccess`. A `FloatRGB9E5` attribute is pulled on every
|
||
device: no device must fetch the shared-exponent format as vertex input, and its decode is
|
||
exact, so the shader's is the answer a conformant fetch gives. Under the identity domains
|
||
nothing is pulled, as the pulled fetch reads through a
|
||
descriptor: every attribute is fetched in fixed function, with or without buffer robustness
|
||
(a fetch outside its buffer reads what the device gives), an unnormalized signed-integer format
|
||
into a variable declared `int`, the constant step at vertex rate on a binding of stride 0 (no
|
||
divisor); an attribute fixed-function input cannot take is refused,
|
||
`Unsupported{VertexStepRate}` (a per-instance rate above 1, naming the missing
|
||
`vertexAttributeInstanceRateDivisor` or the `maxVertexAttribDivisor` it passes; a device that
|
||
lacks only `supportsNonZeroFirstInstance` takes the divisor, and the consumer refuses each draw
|
||
through it at a base instance other than 0),
|
||
`Unsupported{Format}` (a format the device cannot fetch as `VERTEX_BUFFER`,
|
||
and `FloatRGB9E5`) or `Unsupported{VertexInputLayout}` (an offset or stride off the format's
|
||
alignment or past the device's limits, more attributes or layouts than its limits, or the
|
||
constant step's stride of 0 under the portability subset);
|
||
- `graphics`: the fixed-function state (topology, attachment formats and blend states, sample
|
||
count, dynamic states, input attachments, ...). Build
|
||
`VkPipelineColorBlendStateCreateInfo::pAttachments` from `graphics.color_blend_attachments()`.
|
||
|
||
Where Metal's rules differ from Vulkan's, the pipeline gives Metal's answer:
|
||
|
||
- A color attachment no fragment output names keeps its texels (an empty write mask).
|
||
- What Metal refuses, the link refuses as `InvalidDescriptor(ColorAttachment)`: an output with
|
||
fewer components than its attachment's channels, a blend that reads the second output without
|
||
the first, alpha to coverage over an integer attachment 0. Function-constant values that put two
|
||
outputs at one attachment are `InvalidDescriptor(DuplicateColorOutput)`, and values that put two
|
||
arguments of one argument table (buffer, threadgroup memory, texture or sampler) on overlapping
|
||
slots are `InvalidDescriptor(OverlappingArgument)`, naming the later argument, of any stage:
|
||
Metal refuses both when it makes the function.
|
||
- `ColorAttachment`'s `metal` is how plain Metal answers the pipeline (measured on an Apple M3 Max
|
||
under macOS 27.0.1): `MetalAnswer::NilWithError`, nil and an `NSError` (code 3), for an output or
|
||
color input of another numeric class than its attachment, an output with fewer components, a
|
||
blend of the second source without both outputs, and a color input or tile imageblock member of
|
||
an attachment without a format; `MetalAnswer::EndsProcess`, Metal's descriptor validation
|
||
asserting, for a blend into a bfloat attachment and for alpha to coverage over an integer
|
||
attachment 0. A layer that answers as Metal does ends the process only for the second.
|
||
- On a device without `independentBlend`, attachments the function does not write copy the blend
|
||
state of those it does; where those differ the pipeline is refused, naming the feature.
|
||
- A packed 16-bit attachment (`A1BGR5Unorm`, `ABGR4Unorm`, `BGRA4Unorm`) of a format the device
|
||
samples but does not render (the RTX 5080) renders through an `R16_UINT` view of its texture
|
||
(`metal::format::WordAttachment`, `LoweringInputs::word_colors`): the fragment function writes
|
||
the word Metal stores and `graphics` gives the attachment that format; a pass clears it to the
|
||
word of Metal's clear. Exact, for a write unblended (or blended one and zero); under a mask of
|
||
some of its channels the function merges the word into the texel in the interlocked storage
|
||
form (a `GraphicsState::storage_attachments` entry of `R16_UINT`) where the device orders fetch
|
||
through the pixel interlock. Blended otherwise, there too the function computes Metal's blend of
|
||
a `half4` or `float4` output over the word it reads, within one code of Metal's
|
||
(`PrecisionNote::DeviceColorConversion`: the attachment holds codes where Metal's tile holds a
|
||
clear's or an earlier blend's result in the pass as a `half`, and under fast math Metal sums a
|
||
few states otherwise). A blend of the second source, and a blend or
|
||
partial mask on a device without the interlock, is refused as the device lacking the format; a
|
||
blend of an output of fewer lanes is `NotImplemented`; a color input of
|
||
one is read in the interlocked storage form, the word decoded to each channel's code over its
|
||
maximum code rounded to `half`, nearest even, which is Metal's value (measured on an Apple M3
|
||
Max under macOS 27.0.1 for a `float4` and a `half4` input, every code of each 4-bit channel).
|
||
- An 8-bit sRGB attachment of one or two channels (`R8Unorm_sRGB`, `RG8Unorm_sRGB`) the device
|
||
does not render (the RTX 5080: it renders neither `R8_SRGB` nor `R8G8_SRGB`, and holds the
|
||
latter in its UNORM twin) renders through an `R8_UINT` or `R16_UINT` view of its texture the
|
||
same way: the fragment function writes the bytes of the device's sRGB encode
|
||
(`DevicePackConversion`), a pass clears it to the bytes of Metal's clear, and a partial mask
|
||
merges the word in the interlock, or over the attachment's input attachment on a device that
|
||
orders fetch through rasterization-order input attachments (lavapipe;
|
||
`RuntimeFact::WordInputMerge`). A blend of one
|
||
other than one and zero is refused as the device lacking the format; a color input of one is
|
||
`NotImplemented(Format)`.
|
||
- An `RGB9E5Float` attachment the device does not render (the RTX 5080) renders through an
|
||
`R32_UINT` view of its texture: the fragment function writes Metal's word of a `float` output
|
||
(each channel clamped to `[0, 65408]`, a NaN 0, the exponent of the largest, every mantissa
|
||
truncated), and a pass clears it to the word of Metal's clear. Exact. A `half` output to one
|
||
is `NotImplemented(Format)` (Metal rounds the `float` the `half` was converted from, half up);
|
||
a blend, or a mask of some of its channels, is refused as the device lacking the format; a
|
||
color input of one is `NotImplemented(Format)`.
|
||
- A multisampled texture of one of these formats, on a device that samples the format but
|
||
renders it at no count (the RTX 5080), is held in its word's format (`R8_UINT`, `R16_UINT` or
|
||
`R32_UINT`) wherever the device renders that format at the texture's count:
|
||
its samples are the words Metal stores, read through the format's own view as Metal reads them,
|
||
and copied bitwise. Its resolve is the crate-owned one, a pass after the pass that reads the
|
||
stored words: Metal's for `RGB9E5Float` (each channel's mean of the samples'
|
||
values, rounded to the nearest `half`, then encoded with its mantissas rounded half up) and an
|
||
8-bit sRGB format, every pixel; for a packed 16-bit format each field's mean of the samples'
|
||
codes, a tie rounded down (a one-bit field's up), within one code of Metal's, which averages its
|
||
tile's finer values. A device whose word's format renders one sample at that
|
||
count refuses the texture (`TextureSampleCount`).
|
||
- A fragment function that reads a color attachment (framebuffer fetch) reads it in an ordered
|
||
form wherever the device has one. On a device with neither ordered form it reads the attachment
|
||
as an input attachment without rasterization-order access
|
||
(`GraphicsState::rasterization_order_attachment_access` false), and the consumer records a
|
||
barrier of the subpass's dependency on itself, by region, before each draw that reads input
|
||
attachments after an earlier draw of the subpass, or its load operations, wrote a color, and
|
||
before each draw that writes a color after an earlier one read them: a fetch reads what every earlier draw stored, Metal's
|
||
answer, but the overlapping fragments of one draw are not ordered, which the fragment stage
|
||
records (`PrecisionNote::UnorderedFetchWithinDraw`; the runtime does all of this itself). Under
|
||
the identity domains the read is the pushed texture of section 1 instead, at the fragment's own
|
||
pixel, with the same note: the consumer reads the attachment's texture there and orders each
|
||
fetching draw after the earlier draws of its pass. Linking refuses an attachment the read
|
||
cannot take: none attached, or one of another numeric class than the read's
|
||
(`InvalidDescriptor` at the color attachment), or one of a rotated, Y'CbCr or extended-range
|
||
format (`Unsupported{Format}`). A read of one of Apple's extended-range formats (`BGR10_XR`,
|
||
`BGRA10_XR`, `RGB10A8_2P_XR10` and their `_sRGB` twins), and a blend into one, are refused on
|
||
every device in either form (`Unsupported{Format}` at the color attachment): Metal's tile holds
|
||
such an attachment as `half` through the render pass and rounds it to the codes only at the
|
||
store, so a read or a blend after an earlier draw's write sees that draw's `half`, which the
|
||
stored codes do not keep. An unblended write, under any write mask, is built. A
|
||
raster order group on such a color input, or on a color output of an attachment the function
|
||
reads, orders that read alone there and takes no interlock; a group on anything else still
|
||
needs the pixel interlock (`Unsupported{RasterOrderGroups}`).
|
||
The ordered forms are rasterization-order attachment access, or the interlocked
|
||
storage form, which binds the attachment as a storage image viewed in
|
||
`StorageAttachmentUse::view_format()`: its own format, an 8-bit sRGB format's unsigned-integer
|
||
view, or, where the device declares a BGRA-order format neither typed nor without a format, its
|
||
RGBA-order twin (the shader exchanges red and blue). A `[[stencil]]` output uses
|
||
`VK_EXT_shader_stencil_export`.
|
||
- `[[barycentric_coord]]` reads `BaryCoordKHR` or `BaryCoordNoPerspKHR`, whose vertex order is
|
||
Vulkan's first-vertex order and Metal's for every primitive type. A `float3` read takes the
|
||
first two weights and computes the third as `1 - (x + y)` in `float`, as Metal does, not the
|
||
device's own third lane. On a device without `fragmentShaderBarycentric` the function is
|
||
refused, `Unsupported{Barycentrics}` needing the feature.
|
||
- A vertex function stores `Layer` or `ViewportIndex` only where it writes the index the same at
|
||
every vertex of a primitive, decided from its structure (computed from constants, `constant`
|
||
memory, the instance, the amplification and per-instance attributes, as
|
||
`StageIo::uniform_outputs` lists), on a device with `shaderOutputLayer` or
|
||
`shaderOutputViewportIndex`. Metal leaves a primitive whose vertices write different indices
|
||
undefined ("If values differ, the behavior and value passed to the fragment function are
|
||
undefined.", the Metal Shading Language specification), and Vulkan asks every vertex of a
|
||
primitive to write the same `Layer` and `ViewportIndex`
|
||
(VUID-VkPipelineShaderStageCreateInfo-stage-02596 and -02597), so a vertex function whose index
|
||
is not shown the same is refused, `NotImplemented(LayeredRendering)`. One shown the same on a
|
||
device without the output is refused needing it, as `Unsupported(LayeredRendering)`, or as
|
||
`Unsupported(VertexAmplification)` for an index the amplification's offset alone sets. The
|
||
variant for a pass of one layer selects no slice and runs everywhere. A device without
|
||
`multiViewport` draws through one viewport, so there the vertex function stores no viewport
|
||
index at all; a draw that selects among more than one viewport there is refused at the draw,
|
||
`Unsupported(LayeredRendering)` needing `multiViewport`. A fragment function that reads an
|
||
index no stage writes, on a device that can declare neither builtin, reads 0, Metal's value
|
||
with no vertex write.
|
||
- `[[primitive_id]]` counts the draw's primitives in each instance from 0; vertex start, base
|
||
vertex and base instance do not move it. A fragment stage declares `PrimitiveId` through
|
||
`geometryShader` or `tessellationShader`. Without both it reads a flat varying the vertex
|
||
function writes (`IoRole::PrimitiveId`, `PushField::VerticesPerPrimitive`); an indirect draw's
|
||
arguments are then read on the CPU only without `shaderDrawParameters`. An indexed draw runs
|
||
de-indexed (`VariantKey::primitive_id_indices`: needs device addresses, shades every index).
|
||
`NotImplemented(PrimitiveId)`: an indexed strip, an indirect indexed draw, indices past their
|
||
buffer, a vertex function that writes memory in that form, and a mesh pipeline.
|
||
A draw of Metal's private quads or quad strip, which the consumer lists as two triangles a quad
|
||
(`BindState::quad_primitive_ids`), runs the fragment variant that reads `PrimitiveId` shifted
|
||
right by one (`VariantKey::quad_primitive_ids`), as Metal counts one primitive a quad. A
|
||
private type's draw whose vertex function writes the varying, or writes memory (Metal runs it
|
||
once for each vertex of its list, where an indexed draw may run a shared vertex once), runs the
|
||
private list form (`VariantKey::private_list`, `BindShape::private_list`; needs device
|
||
addresses): the consumer draws its list without indices from its first index as first vertex,
|
||
and the vertex function reads each position's index, and Metal's base vertex in the word before
|
||
the list, at the list's address (`PushHole::CaptureIndexAddress`), adding them as Metal's vertex
|
||
ID wraps, so each position runs once and the positions count Metal's primitives.
|
||
- A device draws to at most the lesser of its `maxColorAttachments` and
|
||
`maxFragmentOutputAttachments` color attachments at once (eight at the runtime's floor), where
|
||
Metal takes eight. A pipeline with an attachment at an index at or past the first is refused
|
||
`InterfaceComponents` at `MaxColorAttachments`, and a fragment function with a color output at
|
||
a location at or past the second at `MaxFragmentOutputAttachments`.
|
||
- A pipeline's band variant (`VariantKey::line_band`, of a `BandPrimitive`) draws each line, or
|
||
each edge of a triangle in fill mode `Lines`, as the parallelogram Metal draws for a line under
|
||
Apple's `setLineWidth:` at a width the device's own lines do not draw (the runtime chooses
|
||
where: its README's line widths). Link adds two stages after the vertex stage
|
||
(`pipeline::link::band`): a tessellation control stage that passes the primitive's vertices
|
||
through, and an evaluation stage that places each vertex of a quad patch at an endpoint, moved
|
||
across the segment along its minor axis by half the width in framebuffer pixels. A line's patch
|
||
is two triangles over its parallelogram, front-facing under `LineBandState::FRONT_FACE`; a
|
||
triangle's is the fans of the quad domain's degenerate inner ring over its three edges'
|
||
parallelograms, which face as the triangle does under the draw's front face (a zero-area
|
||
triangle faces back). `graphics.line_band` (`LineBandState`) states the draw's contract: each
|
||
primitive one patch of its vertices (`PATCH_LIST`, `BandPrimitive::patch_control_points`), a
|
||
line's without culling under front face `LineBandState::FRONT_FACE` and without depth bias, a
|
||
triangle's under the draw's cull mode, front face and depth bias, and the band's `[sx, sy, h, f]`
|
||
pushed to the evaluation stage at `push_offset` (`BindState::line_band`: Vulkan viewport 0's half
|
||
extent, the half width, and for triangles 1 under a clockwise front face, -1 under a
|
||
counter-clockwise one). A smooth varying and the clip distances are each endpoint's, a flat or
|
||
integer varying the first vertex's. It needs `tessellationShader` (`Unsupported{WideLines}`
|
||
needing it); a post-tessellation vertex function, a vertex function that writes a layer or
|
||
viewport index, a fragment function that reads barycentric weights and multiview are not
|
||
implemented.
|
||
- `graphics` also states what the runtime needs to record draws correctly: whether the fragment
|
||
function reads flat inputs, barycentrics or its position's depth, whether the vertex function
|
||
writes memory or a layer or viewport index, and which colors the function may write.
|
||
- On a device with `VK_KHR_portability_subset` (MoltenVK), each of the subset's missing features
|
||
is checked against what the pipeline uses.
|
||
|
||
`metal::state` maps the encoder-side Metal state a pipeline does not own (cull mode, winding,
|
||
depth/stencil state, ...) to Vulkan values.
|
||
|
||
### Tessellation
|
||
|
||
A post-tessellation vertex function makes a tessellation pipeline:
|
||
|
||
```rust
|
||
let vertex = FunctionDescriptor::new(library.function("tess_quad_buffer")?);
|
||
let mut descriptor = RenderPipelineDescriptor::new(StageSource::Function(vertex));
|
||
descriptor.tessellation = Some(TessellationDescriptor::default());
|
||
let pipeline = device.compile_render_pipeline(&descriptor, &Budget::standard())?;
|
||
```
|
||
|
||
Its `stages` are a synthesized vertex and tessellation control stage, the function as the
|
||
evaluation stage, and the fragment stage. It draws patches only (`DrawCall::Patches` or
|
||
`PatchesIndirect`), with the factors from a tessellation factor buffer. The device's tessellator
|
||
orders the triangles (`PrecisionNote::TessellationTriangleOrder` where it is not Apple's).
|
||
|
||
A device without `tessellationShader` refuses the function (`Unsupported{Tessellation}`, naming
|
||
the feature), and a fragment `[[primitive_id]]` behind it is `NotImplemented(Tessellation)`.
|
||
|
||
### Mesh pipelines
|
||
|
||
`Device::compile_mesh_pipeline` compiles a `MeshPipelineDescriptor` (an optional object function,
|
||
a mesh function and a fragment function), and `compile_mesh_variant` its variants. Its plan draws
|
||
mesh draws only (`DrawCall::MeshThreadgroups`, `MeshThreads` and their indirect forms). The
|
||
stages are task and mesh shaders, which need `VK_EXT_mesh_shader` with `meshShader` (and
|
||
`taskShader` for an object function). A device without them (MoltenVK, older ANV) refuses the
|
||
pipeline and each of its object and mesh functions (`Unsupported{MeshShading}`, naming what it
|
||
lacks).
|
||
|
||
A grid Metal draws nothing for (an axis past `bind::METAL_MESH_GRID_AXIS`, or more than
|
||
`bind::METAL_MESH_GRID_TOTAL` threadgroups, measured on the M3 Max) draws nothing in every form:
|
||
a direct draw's command is zeroed, an object function's wrapper launches nothing for such a grid,
|
||
and an indirect draw reads its counts on the GPU. An indirect draw's
|
||
`DrawCommands::mesh_arguments` is a pass to record before the render pass
|
||
(`bind::mesh_arguments_module`, one invocation): it writes the draw's counts into the consumer's
|
||
scratch (`MeshArguments::scratch_bytes`), which the draw reads in place of Metal's arguments.
|
||
|
||
An object grid runs at any count Metal runs, past the task stage's limits too, on a device with
|
||
device addresses, `shaderInt64`, `multiDrawIndirect` and `drawIndirectCount` (every committed
|
||
profile with the extension): the object function is compiled folded, and each draw runs its grid
|
||
in pieces within the task limits (`bind::fold_object_grid`), its object stage reading the
|
||
threadgroups Metal runs and its grid from a 32-byte region record at its
|
||
`PushHole::ReplayRegionAddress`. A direct draw's fold is `DrawCommands::object_fold`, whose bytes
|
||
the consumer uploads (the record, and its pieces' commands where `draw` is indirect); an indirect
|
||
draw's arguments pass (`MeshArgumentsForm::FoldedObject`) writes the record, the pieces and their
|
||
commands, which `draw` (`DrawMeshTasksIndirectCount`) reads. Metal's threads per grid wrap to 32
|
||
bits along each axis, as its builtins report them; a direct draw runs only the threadgroups those
|
||
threads fill, rounded up, an indirect one every threadgroup (`bind::metal_object_grid`). Every
|
||
device runs a grid of up to `bind::METAL_OBJECT_GRID_TOTAL` (2^29) threadgroups, the most Metal's GPU
|
||
ran within its watchdog's 5 seconds. A larger one, which Metal's GPU discards, draws nothing and
|
||
fails its command buffer with Metal's error (the runtime's `CommandBufferError::GpuRecoveryVictim`):
|
||
a direct draw says so in `DrawCommands::discards_command_buffer`, and the consumer fails the command
|
||
buffer from the draw's encoder on, as Metal does; an indirect draw's arguments pass sets
|
||
`StatusBits::MESH_GRID_DISCARDED` in the command buffer's `ExecutionStatus`. Where the device cannot
|
||
launch a grid Metal draws, the draw draws nothing and the command buffer fails with the runtime's
|
||
own error (`CommandBufferError::MeshGridPastLimits`, not Metal's): an indirect draw's object grid past
|
||
the task limits of a device that does not fold object grids, or a mesh grid past the mesh limits,
|
||
through `StatusBits::MESH_GRID_PAST_LIMITS`, or a fold of more pieces than
|
||
`MeshArguments::OBJECT_PIECES` (only under task limits below Vulkan's required least), its `past`.
|
||
A direct object grid past the task limits of a device that does not fold is refused naming the
|
||
limit.
|
||
|
||
### Tile pipelines
|
||
|
||
`Device::compile_tile_pipeline` compiles a `TileRenderPipelineDescriptor` (the tile function and the
|
||
render pass's color attachment formats) into a `ComputePipeline` that stands in for
|
||
`dispatchThreadsPerTile:`. Dispatch it as `Dispatch::Threadgroups` over the render area, one
|
||
threadgroup per tile, with `BindState::imageblock_size` and `BindState::render_area` set, and bind
|
||
each attachment it reaches as a storage image where the plan writes `WriteSource::TileAttachment`.
|
||
Its threads are numbered as Metal's tile dispatch numbers them, for every threadgroup of even sides
|
||
up to 32x32 and the single thread; a threadgroup with an odd side, whose quads Metal leaves partly
|
||
empty, is refused. A tile function runs on a single-sample pass: a multisample
|
||
imageblock's operations (its sample count, the colors a pixel holds and their coverage masks, a
|
||
store to the samples a coverage mask selects) are refused as not implemented
|
||
(`Feature::Imageblocks`, naming the intrinsic).
|
||
|
||
## 5. The interface and the plan
|
||
|
||
The pipeline states what it binds and how:
|
||
|
||
```rust
|
||
for (slot, interface) in &pipeline.interface.slots {
|
||
println!(" slot {slot:?}: {interface:?}");
|
||
}
|
||
for set in &pipeline.layout.sets {
|
||
for binding in &set.bindings {
|
||
// ...
|
||
for dependency in &pipeline.dependencies {
|
||
let (fact, mechanism) = (dependency.fact, dependency.mechanism);
|
||
println!(" depends on {fact:?} via {mechanism:?}");
|
||
}
|
||
```
|
||
|
||
- `interface.slots` holds every statically used Metal slot (`SlotRef`: stage, table, index): how
|
||
the shader accesses it, and for a buffer the byte window it can touch. A buffer slot's `kind`
|
||
says whether Metal binds a buffer, a visible-function table or an intersection function table
|
||
there.
|
||
- Under the identity domains (section 1) a buffer slot's `PushField::SlotGuestBase` is its
|
||
address, the bound buffer's `gpuAddress` plus the bind offset (or the address of its
|
||
`setBytes` copy, or of the consumer's nil buffer where nothing is bound), less 2^31, modulo
|
||
2^64. An access through the slot adds its 32-bit byte offset plus 2^31, modulo 2^32,
|
||
zero-extended, to that word; a pointer compare, a pulled vertex fetch and an access from the
|
||
word below it add the 2^31 back to read the address itself, and so does a slot pointer
|
||
converted to an integer or stored, which is the slot's address plus its offset. The binder
|
||
(`BindingPlan::instantiate_*`) of a device that compiles under the identity domains writes the
|
||
word so. A pointer loaded from memory (an argument buffer's pointer member, a `gpuAddress` the
|
||
program stored) and a pointer made from an integer are the device address the word holds,
|
||
unbiased and used directly, unchecked: there is no root table, no `PushField::RootTableAddress`
|
||
and no search. A texture's or sampler's `SlotGuestBase` is its `gpuResourceID`, unbiased.
|
||
- Under the identity domains, a buffer slot that an access may reach at a
|
||
byte offset that is not a translation-time constant (an index the memory plan does not prove
|
||
one value, a looping `memcpy` or `memset`, a pulled fetch) also reads
|
||
`PushField::SlotBounds`: a `uvec2`, the bound buffer's bytes before the bind offset (the bind
|
||
offset), then the buffer's length; 0 and the length of its `setBytes` copy; 0 and 0 where
|
||
nothing is bound. Each such access whose offset the module does not hold as a constant is
|
||
checked against the buffer: for an `n`-byte access at its 32-bit byte offset `m` from the bind
|
||
offset, read as signed, the normative check `(len >= n) && (m + before <= len - n)` in `u32`.
|
||
The domain is the bound buffer, as Metal's is and as a storage view of a whole window's is
|
||
(from the buffer's start): an offset before the bind offset is inside where the buffer holds
|
||
it, and one before the buffer's start wraps to at least 2^31, outside. The module computes it
|
||
as `m + before < limit`, the slot's limit for `n`-byte accesses `len - min(len, n - 1)` (0 for
|
||
a slot shorter than one access), made once per slot and size in the function's first block:
|
||
one add and one compare per access. A load outside reads each of its units at
|
||
`PushField::ZeroAddress`, a `u64` the consumer pushes: the address of 16 bytes of zeros, a
|
||
multiple of 16, that nothing writes. That is one select of each unit's address, with no select
|
||
of the value and no branch, and the load touches nothing of the slot. A store or an atomic
|
||
outside sits in the check's selection and runs nothing; an atomic's answer there is 0. Nil and a
|
||
slot shorter than one access push a length that every such access past it fails. An access at
|
||
a constant offset, and an access through a pointer loaded from memory or made from an integer,
|
||
is not checked. The binder writes `SlotBounds` from the bound buffer and refuses
|
||
`ZeroAddress` (`Unsupported{IdentityDomains}`): the zero range is the runtime's own memory.
|
||
What the check costs, measured on the runtime against the same build with the check
|
||
removed (release, no validation layer): in the compute harness's GPU time, `wide_bindings`
|
||
34.7% slower on a Radeon 780M (job m3-max-56485), 38.5% on an RTX 3090 (m3-max-56486) and 68%
|
||
on an Iris Xe (m3-max-56546); `bind_2000` 17.9%, 32.1% and 11.3%; `constant_managed` and
|
||
`constant_private` 8.0% and 8.3% on the 780M, 22.2% and 25.8% on the 3090, level on the Iris
|
||
Xe; `row_cells` level on the 780M and the 3090, 9.5% on the Iris Xe. A recorded replay
|
||
stream's GPU time a frame, its players built from 653014c278 (the same jobs): 3.3% more on the
|
||
780M over 438 frames (one round), level on the 3090 over 464 frames, and 5.5% more on the Iris
|
||
Xe over 200 frames (m3-max-56641). Making two recorded sessions' render pipelines cold takes
|
||
3.5% and 3.8% more driver time on two RTX 3090s with the check than without it (players built
|
||
from 84b4f52e4b, jobs m3-max-58076 and m3-max-58098).
|
||
- The layout numbers set 0 densely, in `ResourceSlot` order. Set 1 holds the heap arrays (the
|
||
resources reached through argument buffers and resource IDs); `pipeline::heap_sampler_capacity`
|
||
is the most elements a set-1 sampler array has on a profile. A texture buffer reached so is an
|
||
element of a texel-buffer class: a uniform texel buffer array where the function only reads it,
|
||
a storage texel buffer array declared without a format where it writes it, each needing the
|
||
texel buffer kind's non-uniform indexing and update-after-bind features (a storage one also
|
||
`shaderStorageImageWriteWithoutFormat`, and where it is read `shaderStorageImageReadWithoutFormat`
|
||
or, without it, the formats' own read bits, section 10). `bind::heap_element` answers
|
||
`HeapElement::TexelBuffer` for a texture buffer such a class holds, the format of its texel
|
||
buffer view; a planned heap's members are textures with images, so only a heap set the consumer
|
||
keeps for every command (each live texture at its resource ID) holds texture buffers. Push constants carry the values the
|
||
binder writes per command. A stage that writes the command buffer's `ExecutionStatus` (a
|
||
`seq_cst` fence, an argument buffer's cubic sampler) reaches it by device address, a push hole, where the device addresses buffers
|
||
(`bufferDeviceAddress` and `shaderInt64`), and through a set-0 storage buffer of its own
|
||
(`SyntheticBinding::ExecutionStatus`) where it does not. That binding counts against the
|
||
stage's storage-buffer limits: past one, an absent read or a fence is refused
|
||
(`DescriptorCount`), and a stage that would bind it only to test its argument samplers for a
|
||
cubic filter binds nothing, so the plan refuses a cubic sampler listed beside it
|
||
(`SamplerBicubicFilter`).
|
||
- A heap texture sampled through one of several constexpr samplers, the one a sampler handle names
|
||
at run time (a table of constexpr samplers read at a computed index), samples once through a
|
||
set-0 array of those samplers (`ResourceKind::SamplerArray`, `BindingSource::SamplerArray`),
|
||
at the element the handle names. The binding's `immutable_samplers` holds each element's sampler,
|
||
`count` of them, each made as a heap texture sampled through that sampler alone bakes it; it takes
|
||
no descriptor write, and a pushed set 0 pushes them. `StageInterface::sampler_arrays` lists each
|
||
array's constexpr samplers by its slot. The module takes this form where the candidates' handles
|
||
are consecutive, each candidate's sampling is the same but for its sampler, and the device indexes
|
||
a sampler array by a value that may differ between invocations
|
||
(`shaderSampledImageArrayNonUniformIndexing`, which heap textures already need); otherwise, and
|
||
always under the identity domains (whose stages bind no set-0 sampler), it switches over the
|
||
candidates, one sampling per candidate, each through the constexpr sampler the consumer pushes.
|
||
- Each binding's `variables` is the most variables one stage's entry point uses at it (those its
|
||
`OpEntryPoint` lists): a function that reaches a buffer through several types uses a variable of
|
||
each type at the buffer's binding. A set-0 uniform or storage buffer binding may be created as its dynamic type
|
||
(`VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC`, `..._STORAGE_BUFFER_DYNAMIC`), its descriptor
|
||
written at offset 0 and its range's offset bound as the dynamic offset: the module reads the same
|
||
bytes, and commands that differ only in offsets can bind one set. The runtime does so for each
|
||
such binding of one descriptor and one variable that no stage the pipeline adds reads, in binding
|
||
order within `maxDescriptorSetUniformBuffersDynamic` and `maxDescriptorSetStorageBuffersDynamic`,
|
||
where it does not push set 0; the rest stay of their own types. Keep a binding of several
|
||
variables static on MoltenVK 1.4.1: its SPIRV-Cross writes Metal that does not compile for a
|
||
dynamic buffer an entry point uses through more than one variable.
|
||
- A buffer slot's uniform view is a `Uniform` block (`DescriptorView::UniformWords`, or
|
||
`UniformVec4` without `uniformBufferStandardLayout`).
|
||
- On MoltenVK, push set 0 (a push descriptor set layout) for a pipeline whose fragment stage writes
|
||
a storage buffer of set 0 inside its interlock (an interlock execution mode, and a `StorageBuffer`
|
||
variable of set 0 not decorated `NonWritable`): binding through Metal argument buffers, MoltenVK
|
||
packs a bound set 0 into one argument buffer, and Metal leaves such a buffer out of its raster
|
||
order group where the function also reads another buffer of that argument buffer, so overlapping
|
||
fragments of a draw read it unordered (measured on an M3 Max with 1.4.2). A pushed set's buffers
|
||
are discrete arguments, which Metal orders. MoltenVK's pushed sets do not hold their buffers'
|
||
sizes, so bind set 0 there for a pipeline a stage of which reads a buffer's length
|
||
(`OpArrayLength`).
|
||
- `dependencies` lists the bind-time state the pipeline depends on (`RuntimeFact`: threadgroup
|
||
sizes, sampler coordinate modes, buffer ranges, ...) and how this profile honors each
|
||
(`Mechanism`: a specialization constant, a push field, a plan-time check, a variant). The plan
|
||
acts on them; you do not have to.
|
||
- The format class of a bound texture is read by the shader as push data, not specialized, so binds
|
||
that differ only in their textures' formats share one `VkPipeline`. On MoltenVK a slot read with a
|
||
`half` result also specializes one bit of it: whether the class converts its texels to `half` by
|
||
rounding to nearest even alone (every class but those with 32-bit float channels, 16-bit
|
||
normalized ones and YCbCr fields on the grid `8192`), so a slot bound to both kinds of format
|
||
takes two pipelines. Other bound values (a slot's
|
||
presence, a sampler's word, whether a uniform buffer's descriptor range starts at its bind offset)
|
||
are specialization constants. The last lets a driver load the constants ahead of the shader:
|
||
inline bytes take that form, and so does a buffer bound at a multiple of
|
||
`max(minUniformBufferOffsetAlignment, 16)` at a slot whose table tells that alignment apart
|
||
(`state.view_base_alignment`, and the slot's bit in its table's `at_bind_views`). A buffer bound at any other offset, or at a slot left out, takes the other form, a
|
||
pipeline of its own: leave out a slot bound at offsets that move, so that its draws keep one
|
||
shape and one pipeline (the runtime leaves out each slot of a state once it finds one bound at
|
||
an offset the alignment does not divide). A consumer that draws and dispatches every key with
|
||
its uniform views at their residuals (each uniform view's `RangeAtBindOffset` at 0, as the
|
||
runtime does on every device whose driver is not measured to load such reads ahead of the
|
||
shader) says so with
|
||
`state.uniform_views_at_residual = true`: its uniform views then bind ranges their bind offsets
|
||
share (below), as a bounded storage view's do. No storage view declares the key: it reads at its
|
||
residual, its range from the buffer's start, so the bytes before a bind offset stay reachable.
|
||
- A bound buffer's length enters its shape only as a length class (`bind::length_class`), which a
|
||
plan compares with the device's `maxUniformBufferRange` and `maxStorageBufferRange` alone
|
||
(whether every range of the buffer fits each). Set each table's
|
||
`buffer_ranges = BufferRanges::of(limits)` and its shape tells apart only the classes those
|
||
limits tell apart (`BufferRanges::length_class`: a class rounded up to the greatest one that fits
|
||
the same limits), so commands over buffers of many sizes keep one shape and one plan: on a device
|
||
whose two limits are both 4 GiB, every buffer below 4 GiB. The default tells every class apart.
|
||
The runtime sets them on every table it reads.
|
||
- A two-plane YCbCr texture whose planes a function may read in place states them in its facts
|
||
(`TextureFacts::ycbcr_in_place`, a `YCbCrInPlace` of its planes). A slot whose function only samples it (nearest or linear, no comparison,
|
||
anisotropy or `min_lod_clamp`), gathers it or reads its size (`RuntimeFact::YCbCrInPlace`) takes the
|
||
pipeline's in-place variant (`VariantKey::ycbcr_in_place`): the slot binds a uniform texel buffer
|
||
over the planes' bytes (`WriteSource::YCbCrInPlace`, in `YCbCrInPlace::format`), its push field
|
||
(`PushField::YCbCrInPlace`) holds `YCbCrInPlace::words`, and the function converts and filters
|
||
the codes as Metal does, exactly (a gather Metal's taps of them). Any other slot, and any texture without the fact, binds the
|
||
texture's image of converted texels. Where the consumer also holds the planes as a linear image
|
||
of their multi-planar 4:2:0 format over their bytes, which the device's sampler Y′CbCr
|
||
conversion reads with Metal's reconstruction, it states so (`YCbCrInPlace::with_planes_image`,
|
||
its conversion `YCbCrInPlace::sampler`, a `YCbCrPlanesSampler`). A slot whose every sample the
|
||
device's sampler takes (the fact's `device`: no border address mode, no texel offset placed by a
|
||
parent's size, no gather; a runtime sampler's word without a border mode) then takes the planes variant
|
||
(`VariantKey::ycbcr_planes_sampled`): the slot is a combined image sampler of the image
|
||
(`ResourceKind::YCbCrPlanes`, `BindingSource::YCbCrPlanes`, written from
|
||
`WriteSource::YCbCrPlanesImage`) whose immutable sampler is `SamplerDesc::ycbcr` of the
|
||
conversion. The consumer chains its `VkSamplerYcbcrConversionInfo` into that sampler and makes
|
||
the image's view with an identically defined conversion, and sizes a pool for the format's
|
||
`combinedImageSamplerDescriptorCount` descriptors at the binding. Each sample is the device's,
|
||
at level 0, its conversion and filter of the codes rather than Metal's.
|
||
- A 2D texture whose image is a shadow but whose bytes the consumer reads and writes in place
|
||
states its rows in its facts (`TextureFacts::texel_rows`, a `TexelRows`: its width, height, the
|
||
texels from one row's first to the next's, and its first texel in a texel buffer view of its
|
||
bytes; the runtime states one for a shadow over host pages whose image the device refused, not
|
||
one its policy asks for). A slot whose function only reads, writes or asks the size of it (no
|
||
sample, gather, atomic, read through a sampler, level views, switch at use or residency;
|
||
`RuntimeFact::TexelRows`) takes the pipeline's rows variant (`VariantKey::texel_rows`) where
|
||
the device views the texture's format as a texel buffer of the slot's kind (uniform where the
|
||
slot's declaration only reads, storage otherwise, read and written without a declared format):
|
||
the slot binds a texel buffer of the format over the rows' bytes (`WriteSource::TexelRows`,
|
||
from the view's first texel through the last row's last; a use of class `AccessClass::TexelRows`
|
||
of those bytes), its push field (`PushField::TexelRows`) holds `TexelRows::words`, and the
|
||
function addresses texel `(x, y)` at `first + y · row + x`, testing every read and write against
|
||
the texture's size itself (a coordinate past the width names the next row's texel in the view):
|
||
a read outside is Metal's out-of-bounds texel and a write there is dropped, and a size query
|
||
answers the field's size and one level. A texel buffer's texels go through the device's format
|
||
conversion as an image's do, so the reads and writes are the image route's, bit for bit (the
|
||
runtime's `texel_rows_device` tests compare every `RGBA8Unorm` and `RGBA16Float` code), and no
|
||
precision note is recorded. Any other slot, and any texture without the fact, binds the image.
|
||
- A texture buffer of more texels than the device's `maxTexelBufferElements` (Metal makes them up
|
||
to 2^28 texels on every Mac; one texel buffer view holds 2^27 on ANV and lavapipe and 2^26 on
|
||
MoltenVK) is bound through views of its parts. State with its binding the views its texels take
|
||
(`BoundTexture::Buffer::parts`, `bind::texel_buffer_parts(limits, texels)`: 1 within the limit,
|
||
which binds one view as before and keeps the pipeline and its key). Each part but the last holds
|
||
`bind::part_texels(limits)` texels, the limit rounded down to a multiple of
|
||
`minTexelBufferOffsetAlignment`, so every part starts on that alignment where the texture buffer
|
||
does; the last holds the rest. A slot whose function only reads (not through a sampler), writes,
|
||
updates by an atomic or asks the width of the texture buffer, on a device that indexes an array
|
||
of the slot's kind of texel buffer by a `NonUniform` value (`RuntimeFact::TexelBufferParts`),
|
||
takes the pipeline's parts variant (`VariantKey::texel_buffer_parts`) where the shape binds more
|
||
than one part there: the slot is an array of the parts' texel buffers, one `WriteSource::TexelBuffer`
|
||
per array element, each element's view the bytes of the instance's `DescriptorRange` at that
|
||
`element`. The function reads texel `i` as texel `i - k · P` of part `k = min(⌊i / P⌋, parts - 1)`;
|
||
a texel past the last lies past the last part's end, so its read is Metal's out-of-bounds texel
|
||
and its write is dropped, as through one view, and the width is `(parts - 1) · P` plus the last
|
||
part's. Each part is the device's own view in the format one view takes, so a read converts its
|
||
texel as that view does, and no precision note is recorded. Still refused with
|
||
`BindError::TexelBufferTooLarge`: a slot that reads the texture buffer through a sampler, a device
|
||
without `shaderUniformTexelBufferArrayNonUniformIndexing` (or
|
||
`shaderStorageTexelBufferArrayNonUniformIndexing` for a slot that writes), and a format whose
|
||
block holds several texels; parts past a stage's sampled or storage image descriptors are the
|
||
link's descriptor-count refusal. A texture buffer past the limit in an argument buffer is the
|
||
heap class's neutral.
|
||
- `interface.reaches_command_buffers` and `interface.texture_writing_stages` say, provably, which
|
||
pipelines may touch an indirect command buffer and which stages may write a texture.
|
||
`interface.command_writes` (`CommandWrites`) says what its functions may write into the
|
||
records they reach: the indirect-command operations they may call, and the primitive types
|
||
their draws may pass (a draw's constant type, or all five where it is not a constant). A clear
|
||
bit is proven.
|
||
- `interface.writes_memory` says, provably, whether a pipeline's functions write memory another
|
||
invocation or a later command can read: a store to a buffer or through a device address, a
|
||
texture write, or an atomic update. A compute pipeline without it has no effect a program can
|
||
observe, so a runtime may run nothing for its dispatches.
|
||
- `interface.reads_unbound_resources` and `interface.writes_unbound_resources` say, provably,
|
||
whether a pipeline's functions may read or write a resource through anything but their bound
|
||
slots: through a pointer or texture they load from memory or make from an integer (an argument
|
||
buffer's resources, an address stored in a buffer, a resource ID), an indirect command buffer,
|
||
ray tracing, or a call the function cannot see into. Metal requires such resources to be
|
||
declared with `useResource` or `useHeap`. With each slot's `access`, they are what a compute
|
||
encoder needs to place a barrier only on a hazard.
|
||
- `interface.unbound_storage_textures` says, provably, whether a pipeline's functions may reach
|
||
a texture through its storage image through anything but their bound slots: a texture write or
|
||
atomic, or a read the operation does not declare `access::sample` or `access::read`, through a
|
||
texture they load from memory or make from an integer (an argument buffer's texture,
|
||
a resource ID), or an intersection or a call the function cannot see into. A runtime that
|
||
gives some textures no storage image needs it to find such an access to one.
|
||
|
||
### Plans
|
||
|
||
Per draw or dispatch, describe what is bound, keyed by Metal slot, as a `BindState`. The binder
|
||
never sees your resources, only facts about them:
|
||
|
||
```rust
|
||
let mut table = StageTable::default();
|
||
table.buffers.insert(0, buffer(4096, 0));
|
||
let facts = TextureFacts::new(TextureType::Type2D, PixelFormat::RGBA8Unorm);
|
||
let texture = BoundTexture::Texture {
|
||
facts,
|
||
resource_id: None,
|
||
};
|
||
table.textures.insert(0, texture);
|
||
let sampler = BoundSampler::new(SamplerDescriptor::new());
|
||
table.samplers.insert(0, sampler);
|
||
let mut state = BindState::default();
|
||
state.stages.insert(Stage::Kernel, table);
|
||
|
||
let plan = pipeline.plan(&device, &state.shape())?;
|
||
assert!(plan.variant.is_empty(), "this shape needs no other variant");
|
||
```
|
||
|
||
`state.shape()` keeps only what can change a plan (which slots are bound with what kind of
|
||
resource, formats, level counts, sampler modes, alignment classes). A plan is a pure function of the
|
||
pipeline, the device and the shape: memoize it on `(PipelineKey, BindShape)`. A consumer that keeps
|
||
the shapes it planned can find a state's among them without making it: `state.has_shape(&shape)`
|
||
answers `state.shape() == shape`, compared in place.
|
||
|
||
The plan holds one `DescriptorWrite` per layout binding element (the resource at a slot, a buffer
|
||
the binder synthesizes, or a neutral resource where nothing compatible is bound), the vertex
|
||
buffers, and the `ResourceUse`s that drive staging, writeback and barriers. List in the state
|
||
every live sampler state and visible-function table the command can reach through argument buffers
|
||
(`argument_samplers`, function tables), and the resources it uses (`used_resources`). A consumer
|
||
that lists those in token order says so (`used_in_token_order`), and one that points every root
|
||
table at a resident set it uploads apart, holding them and the ranges it lists
|
||
(`resident_ranges`), says that (`resident_apart`, with `ResidentSet`): a command's
|
||
instantiation then costs its own bindings and heap members, not every resource the state lists.
|
||
|
||
A plan whose `variant` is not empty asks for another variant of the pipeline (a storage texture's
|
||
format, a buffer range past the descriptor limit, a vertex buffer that fixed-function input cannot
|
||
fetch, ...). Compile it and plan it for the same shape: `device.compile_render_variant(&descriptor,
|
||
&plan.variant, &budget)` for a render pipeline, or compile the kernel with
|
||
`plan.variant.lowering_inputs(Stage::Kernel)` and link it as `StageSource::Compiled`. A dispatch can
|
||
ask too (`plan.dispatch_variant(&dispatch)`, or `BindError::NeedsVariant` from instantiation).
|
||
|
||
Textures follow Metal's texel rules: sparse textures report residency where the device can,
|
||
`Depth32Float` texels are the device's depth image's (a value it clamps or a denormal it flushes is
|
||
the device's, `PrecisionNote::DepthDenormals`), `ulong` textures and their atomics use
|
||
64-bit images where the device has them, and `half` and sRGB reads give Metal's values or record
|
||
their bound (an sRGB texel is the device's own decode wherever the device samples its format,
|
||
`srgb-texel-decode`). The
|
||
rustdoc of `BindState`, `TextureFacts` and `BindingPlan` states each rule. A read's, sample's or
|
||
gather's `{T, i8}` result (the texel and its residency word) may pass through phis, selects,
|
||
freezes and member insertions and extractions, Apple's GL code generator's swizzled read among
|
||
them. A function whose effects depend on a bit of the word other than bit 0 (`resident()`'s), or
|
||
that stores, passes or returns a value holding the word, is refused (`Lowering(Texture)`): Metal
|
||
establishes no other bit.
|
||
|
||
## 6. At bind time
|
||
|
||
Instantiate the plan per dispatch or draw:
|
||
|
||
```rust
|
||
let dispatch = Dispatch::Threads {
|
||
threads: [100, 1, 1],
|
||
threads_per_group: [32, 1, 1],
|
||
};
|
||
let commands = plan.instantiate_dispatch(&state, &dispatch)?;
|
||
for region in &commands.regions {
|
||
// ...
|
||
println!(" descriptor ranges: {:?}", commands.descriptor_ranges);
|
||
println!(" push holes: {:?}", commands.push_holes);
|
||
```
|
||
|
||
```rust
|
||
let plan = pipeline.plan(device, &state.shape())?;
|
||
let draw = DrawCall::Primitives {
|
||
vertex_start: 0,
|
||
vertex_count: 6,
|
||
instance_count: 1,
|
||
base_instance: 0,
|
||
};
|
||
let commands = plan.instantiate_draw(&state, &draw)?;
|
||
```
|
||
|
||
A buffer slot read through a storage view of its whole window (a `device` pointer the function
|
||
indexes) takes the descriptor range from the start of the bound buffer's space to its end padded
|
||
to 16 bytes, whatever its bind offset, which its `SlotBindOffset` push field holds: draws and
|
||
dispatches that bind one buffer at other offsets write the same descriptors, and only push data
|
||
differs. Where that range would pass the view's `maxStorageBufferRange` or 4 GiB, or the offset
|
||
lies at or past the padded end, the range starts at the bind offset aligned down to the
|
||
descriptor base alignment. An access that reaches back before the bind offset, inside the buffer,
|
||
reads the buffer's bytes there, as Metal's does.
|
||
|
||
A slot of a bounded window read at its residual -- a storage view's always, a uniform view's
|
||
where the state says `uniform_views_at_residual` -- takes a range its bind offsets share in the
|
||
same way: the buffer from 0 to its padded end where that fits the view's maximum range (a uniform
|
||
view's is `maxUniformBufferRange` capped at 64 KiB, its texel form's `maxTexelBufferElements`
|
||
texels) and 4 GiB, else the block of half that
|
||
maximum (rounded down to a power of two) its window starts in, to the maximum past the block's
|
||
start. A window of at most half the maximum starting in a block ends inside its block's range; a
|
||
longer one, or one at or past the padded end, takes the range from its bind offset, as every
|
||
other descriptor-form slot does. So draws that move such a slot's offset along one buffer (a
|
||
consumer's staged `setVertexBytes`, a constant reference bound at the next offset of an upload
|
||
chunk) write the same descriptor, keep set 0 and push only the slot's `SlotBindOffset`. Each read
|
||
inside the window reads the bytes it read before; one outside it, which Metal leaves undefined,
|
||
reads the buffer's bytes inside the range.
|
||
|
||
`instantiate_draw_into` writes the same commands over a `DrawCommands` you keep, in the storage
|
||
its vectors already hold: keep one from draw to draw (`DrawCommands::default()` to start) and the
|
||
draws allocate none of it again once it holds as much. `instantiate_dispatch_into` does the same
|
||
for a compute dispatch over a `DispatchCommands` you keep (one `instantiate_dispatch` made, to
|
||
start): each region's specialization and push bytes, and the argument table's, are written in the
|
||
storage the commands before held.
|
||
|
||
A plan that reads handles (a texture's or sampler state's resource ID, a visible function table's)
|
||
writes heap tables among its descriptor bytes, after the stage tables: its handle tables, sampler
|
||
tables and function-table directories. Their bytes grow with the resources the state lists as
|
||
used and the textures its slots bind that a handle names, and they depend on nothing else of the
|
||
state but its argument samplers, function tables and heap placement. `heap_elements` is a
|
||
function of the same inputs and the heap's capacity.
|
||
|
||
A dispatch is one or more regions (a grid that is not whole threadgroups adds edge regions), each
|
||
with its group count, specialization and push constants: create one `VkPipeline` per distinct
|
||
specialization. The plan of a kernel's partial-threadgroups variant (`VariantKey::partial_threadgroups`,
|
||
which `LoweringInputs::partial_threadgroups` compiles) runs such a grid as the threadgroups its
|
||
threads fill at the dispatch's threadgroup size instead, the last along an axis Metal's partial
|
||
threadgroup, so every remainder takes the same `VkPipeline`; a kernel the variant cannot run
|
||
exactly (a tile function, a stage-input region, compute derivatives, emulated simdgroups) is
|
||
refused it, `Unsupported{GpuEncodedIndirectCommands, NotImplemented}`, and keeps the edges. On
|
||
the identity domains the variant of a kernel that does not
|
||
observe its threadgroup partition (`KernelFacts::partition_oblivious`) runs its grid in tiles
|
||
instead (`RuntimeFact::GridTiles`): its plans run a direct dispatch's threads in
|
||
`pipeline::grid_tile`'s tiles, one workgroup of `pipeline::GRID_TILE_THREADS` each with the tile
|
||
as the region's nominal size (`MultiRegion` 1), the region flagged `NONUNIFORM` where the tile
|
||
does not divide the grid, and a range of threadgroups at its own size (`MultiRegion` 0); the
|
||
kernel runs the same positions with the same answers: where the region is flagged `NONUNIFORM`,
|
||
every invocation compares its position with the region's grid and one past it returns before the
|
||
body, a test inside one branch on the flag; an unflagged region runs no such test, so a runtime
|
||
flags every region of a grid its workgroups do not divide. At `MultiRegion` 0 a runtime may run a
|
||
direct dispatch of the variant in workgroups of a size it picks, flagged where they do not divide
|
||
the grid, whose invocations past it return before the body as a tile's do; an indirect dispatch's
|
||
region states no grid and is unflagged, and all of its invocations run.
|
||
`Device::kernel_observes_partition(&kernel, &inputs, &budget, &entries)` answers, before any
|
||
lowering, what `CompiledFunction::observes_threadgroup_partition` answers for the compile under the
|
||
same inputs (from the specialized entry `entries` holds, ordered for the device where its
|
||
profile takes the lockstep order), so a runtime lowers only the variant a creation needs.
|
||
`CompiledFunction::is_image_only` (`KernelFacts::image_only`) states, from the AIR alone, a kernel
|
||
that samples, gathers or reads a texture and loads, stores and updates atomically nothing through
|
||
a `device` pointer (`constant` loads and texture writes allowed), which a runtime may run in
|
||
square workgroups. On
|
||
that plan a grid of whole threadgroups, of `Threads` or `Threadgroups`, takes the same regions
|
||
unflagged (each `MultiRegion` 1, every thread live), so one `VkPipeline` of a threadgroup size runs
|
||
all of a kernel's direct dispatches. The runtime lowers a kernel that does not observe its
|
||
partition once, as that variant, and runs every direct dispatch of it in its tiles; a kernel that
|
||
observes its partition runs its whole grids, threadgroup dispatches and indirect dispatches on its
|
||
plain module at their own threadgroup size, and a `dispatchThreads` grid that is not a whole
|
||
number of threadgroups on its partial-threadgroups variant. A kernel
|
||
that steps a stage-in attribute by thread position, or reads `[[grid_origin]]` or `[[grid_size]]`,
|
||
needs the encoder's stage-in region:
|
||
|
||
```rust
|
||
match plan.instantiate_dispatch(&state, &dispatch) {
|
||
Err(BindError::MissingStageInRegion) => {}
|
||
// ...
|
||
state.stage_in_region = Some(StageInRegion::new([0, 0, 0], [64, 1, 1]));
|
||
let commands = plan.instantiate_dispatch(&state, &dispatch)?;
|
||
```
|
||
|
||
A tessellation pipeline draws patches, with the factors' buffer in the state:
|
||
|
||
```rust
|
||
let factors = TessellationFactorBuffer::new(4096, 0, 0);
|
||
state.tessellation_factors = TessellationFactors::new(Some(factors), 1.0);
|
||
let plan = pipeline.plan(device, &state.shape())?;
|
||
let draw = DrawCall::Patches {
|
||
control_points: 4,
|
||
patch_start: 0,
|
||
patch_count: 16,
|
||
patch_index: None,
|
||
instance_count: 1,
|
||
base_instance: 0,
|
||
control_point_index: None,
|
||
};
|
||
let commands = plan.instantiate_draw(&state, &draw)?;
|
||
```
|
||
|
||
What you record from `DispatchCommands` or `DrawCommands`:
|
||
|
||
- **Descriptors.** Write each of the plan's `descriptors`, binding each buffer descriptor over
|
||
exactly its `descriptor_ranges` entry. Upload `descriptor_bytes` (argument and handle tables) and
|
||
the resident root tables where present. A handle table maps each resident resource's
|
||
`gpuResourceID` to its heap element; a consumer that keeps one heap for every command buffer can
|
||
bind one table of every texture ID instead (`bind::handle_entry`, `bind::heap_element`,
|
||
`bind::handle_entry_flags`), as the runtime does.
|
||
- **Push constants.** Write `push_constants`, and fill each of the `push_holes` with the device
|
||
address it names: the uploaded root table, the command buffer's `ExecutionStatus` buffer, an
|
||
indirect command buffer's records. A snapshot variant's `PushHole::SnapshotPhase` is a word, not
|
||
an address (below).
|
||
- **The dispatch or draw.** Record `regions` or `draw` as given: the binder has already applied
|
||
Metal's offsets, bases and clamps.
|
||
- **Status.** Per command buffer, keep one `ExecutionStatus` buffer for what translated shaders
|
||
report (a resident-set miss, for one), and read it when the command buffer completes. A plan
|
||
binds it as `WriteSource::ExecutionStatus` on a device that does not address buffers: bind the
|
||
same buffer there, its `ExecutionStatus::BYTES` bytes.
|
||
|
||
### Snapshot reads
|
||
|
||
A draw that reads, at its fragments' own pixels, a texture its own subpass renders can read it
|
||
through a snapshot instead of a copy taken before the render pass instance. The interface lists
|
||
the fragment slots that allow it (`StageInterface::snapshot_textures`, `StageModule::
|
||
snapshot_textures`): own-pixel slots whose texels reach no derivative, sample, quad or subgroup
|
||
operation, branch, integer divisor, dynamic vector index or store outside the invocation, directly
|
||
or through a call, in a function that writes no memory. A selection on a texel is allowed where
|
||
its arms leave through its merge alone and hold no derivative, implicit-level sample or
|
||
level-of-detail query, quad or subgroup operation, loop or undefined operand, nor call a function
|
||
that does; what its arms leave behind (its merge's phis, the variables they store to) is judged
|
||
as the texel is. It says why it leaves out each other
|
||
own-pixel slot (`snapshot_refusals`: each slot with every `SnapshotRefusal` that holds for it).
|
||
Name the slots in `BindState::snapshot_textures`; the plan keeps those its pipeline can read so
|
||
and states them in `variant.snapshot_textures`, each with the form its snapshot holds texels in
|
||
(`SnapshotForm::of` the slot's bound view and declared type: the texel's codes for an 8- or 16-bit
|
||
normalized, half-float or integer format of the slot's class, which a read decodes as Metal reads
|
||
the format, and the fetched lanes' bits for any other), compiled like any other variant. It keeps
|
||
none on a
|
||
device without `fragmentShaderPixelInterlock`, `fragmentStoresAndAtomics`,
|
||
`attachmentFeedbackLoopLayout` or `extendedDynamicState3ColorWriteMask`, for a pipeline of more
|
||
than one sample, a depth or stencil attachment, attachments in the storage form, input
|
||
attachments, an imageblock, tile memory, a shader-side stencil or tests, tessellation or mesh
|
||
stages, or a function that writes memory, and for an amplified or layered draw. Serve every other
|
||
read as before.
|
||
|
||
A pipeline of that variant states `GraphicsState::snapshot_reads`: create it with
|
||
`VK_PIPELINE_CREATE_COLOR_ATTACHMENT_FEEDBACK_LOOP_BIT_EXT`; its `dynamic_states` hold
|
||
`VK_DYNAMIC_STATE_COLOR_WRITE_MASK_EXT`, and its requirements `attachmentFeedbackLoopLayout` and
|
||
`extendedDynamicState3ColorWriteMask`. Bind the slot's own texture in the attachment's feedback-loop
|
||
layout, and at `WriteSource::SnapshotTexels` a storage image of the slot's form's
|
||
`SnapshotForm::format` (`R8G8B8A8_UINT`, `R16G16B16A16_UINT` or `R32G32B32A32_UINT`) covering the
|
||
render area, in `GENERAL` layout. Record each draw twice in its subpass: first with every color
|
||
write mask 0 and `PushHole::SnapshotPhase` set to 1, each fragment copying its texel into the
|
||
snapshot and running nothing else; then with the pipeline's own masks and the phase 0 (the plan's
|
||
bytes), each fragment reading the snapshot. A by-region pipeline barrier before the first draw
|
||
makes the attachment's earlier writes visible to the copy, and one between the two makes the copy
|
||
visible to the reads and orders it before the second draw's color writes. Each step rests on a
|
||
sentence of the Vulkan specification: the feedback loop's per-pixel barrier, the color write mask
|
||
leaving memory unmodified, the invariance of fragment generation under write masks (Rule 2,
|
||
Corollary 1), and the ordered pixel interlock. One draw alone cannot be exact: nothing orders a
|
||
fragment shader's read of a texel before the same draw's color write of it.
|
||
|
||
Where the attachment's image lacks feedback-loop usage (NVIDIA's drivers make no linear image with
|
||
it), the two draws can run in two render pass instances instead, after the instance before them
|
||
ends. Record the first in a render pass instance of its own over the framebuffer of the instance
|
||
that follows, of a render pass compatible with that instance's that loads every attachment and
|
||
stores none (`VK_ATTACHMENT_STORE_OP_NONE`, core in Vulkan 1.3 and brought below it by
|
||
`VK_KHR_load_store_op_none` or `VK_EXT_load_store_op_none`), every color in `GENERAL` and the
|
||
slot's texture bound in `GENERAL`, between pipeline barriers from everything before and to
|
||
everything after; then begin the next instance and record the second draw first in it, with no
|
||
barrier between the draws. Neither instance enables a feedback loop, and neither needs one: the
|
||
first writes no color, by its masks or by its load and store operations
|
||
(`VUID-vkCmdDraw-None-10795`, `VUID-vkCmdDraw-None-12338`), and the second's fetch of the texture
|
||
never runs. The same pipeline serves both, as render pass compatibility ignores load and store
|
||
operations and layouts. The runtime takes this form for such a read after a memory barrier inside
|
||
the instance (`WorkCounts::reading_draws_snapshotted_leading`), where it would otherwise split the
|
||
instance and copy the texture, and where it weighs cheaper than the split by the scissors' area or,
|
||
on a device with precise occlusion queries, by the pixels the draw covered in its last measured
|
||
pass, even over a buffer's bytes: its first draw reads the texture at every pixel the draw covers.
|
||
|
||
A draw whose pipeline writes no channel of the color it reads so needs no snapshot: the runtime
|
||
creates its pipeline (the variant without snapshots) with the feedback-loop flag, binds the slot's
|
||
own texture in the attachment's feedback-loop layout, and records a by-region barrier before the
|
||
draw, from the color writes to the fragment shader's reads, and one after it, from those reads to
|
||
the color output (section 10 says what it then reads).
|
||
|
||
### Levels a slot reads
|
||
|
||
Each texture slot of a stage states the levels of its bound view its reads reach, counted from the
|
||
view's first level (`StageModule::level_reads`, one `LevelReads` a slot): whether some read may
|
||
reach any level (`any`: a level the function computes, a level-count query used beyond the
|
||
containment of a constant level, a level-of-detail query, a sample through no sampler or with a
|
||
minimum level of detail, a storage access, or a use the analysis does not follow), the highest
|
||
constant level a fetch, size query or gather names (`named`) and the lowest whose texels a fetch
|
||
or gather reads (`lowest_read`), and its samples by the sampler each reads through (`sampled`, with the highest
|
||
and the lowest constant level of detail). A sample's level is its sampler's to bound.
|
||
|
||
### Indirect command buffers
|
||
|
||
An indirect command buffer's commands are records in ordinary buffer storage
|
||
(`bind::indirect_commands`): one format whatever the descriptor, written by the CPU or by a kernel.
|
||
The binder replays records into dispatches and draws: on the host, or on the device through a
|
||
preprocessing kernel (`replay_preprocess`), and through `VK_EXT_device_generated_commands` where
|
||
the device has it (`replay_preprocess_generated`). Draws of primitives under the encoder's
|
||
pipeline replay on the device through `draw_replay_preprocess`: one indirect draw per command
|
||
and primitive type the execution names, the pipeline compiled as its replay variant
|
||
(`VariantKey::replay_bindings`), whose vertex and fragment functions read each command's buffers
|
||
from the slot tables the preprocess writes. The variant pulls every layout fixed-function vertex
|
||
input would fetch (`draw_replay_vertex_layouts`, named in the shape's `far_vertex_buffers`: the
|
||
pulled-vertex-buffer variant, no buffer bound there), so each attribute reads its command's buffer
|
||
at the command's own vertex and instance; a layout whose stride is set per bind keeps an
|
||
execution off the device (`DrawReplayRule::VertexAttributes`), since each command binds its own
|
||
stride. Draws of one list type or points whose fragment function
|
||
reads no buffer and no `[[primitive_id]]` may instead merge into one draw (`DrawReplay::merged`):
|
||
two merge passes (`draw_merge_module`) write each command's first vertex in the merged draw, and
|
||
the vertex function, compiled as its merged variant (`VariantKey::merged_draws`), finds its
|
||
command by that prefix and runs the command's vertex, instance and buffers, in the commands'
|
||
order. A kernel that encodes commands writes the records that start at
|
||
`bind::indirect_commands::records_address` of the buffer's ID, outside every Metal address, and
|
||
finds them only at their start: records may run past later IDs' starts, and a resident set
|
||
(`bind::ResidentSet`) keeps each records range whole. Each of the kernel's writes of a command's
|
||
record also stores 1 at the command's written flag, a `u32` a command past the records
|
||
(`bind::indirect_commands::written_flag_offset`), where the storage holds them; storage that ends
|
||
at its records takes none. A consumer whose own writes of the records set the flags too may reset
|
||
only the flagged records of a run of commands, clearing each flag with its record.
|
||
|
||
## 7. Caching
|
||
|
||
The crate keeps no cache; keys and encodings let you keep one across processes. Keys are SHA-256
|
||
hashes of the inputs, `cache::SEMANTICS_VERSION` and the profile fingerprint, known before
|
||
compiling (`device.compute_pipeline_key(&descriptor)`, `render_pipeline_key(&descriptor,
|
||
&budget)`, `function_compile_key`):
|
||
|
||
```rust
|
||
let bytes = pipeline.to_bytes();
|
||
let cached = ComputePipeline::from_bytes_for_key(&bytes, &pipeline.key, &device)?;
|
||
assert_eq!(cached, pipeline);
|
||
```
|
||
|
||
A decode checks the format and semantics versions, the key, the profile fingerprint and a
|
||
checksum. Every `ArtifactError` is a cache miss: compile again and replace the entry.
|
||
|
||
Functions cache on their own, so pipelines that share a function share its compile, and a
|
||
pipeline links a cached function without decoding it again:
|
||
|
||
```rust
|
||
let kernel = FunctionDescriptor::new(sampling.function("sample_level")?);
|
||
let inputs = LoweringInputs::new();
|
||
let key = device.function_compile_key(&kernel, &inputs);
|
||
let compiled = device.compile_function(&kernel, &inputs, &budget)?;
|
||
let bytes = compiled.to_bytes();
|
||
let function = CompiledFunction::from_bytes_for_key(&bytes, &key, &device)?;
|
||
let descriptor = ComputePipelineDescriptor::new(StageSource::Compiled(function));
|
||
let linked = device.compile_compute_pipeline(&descriptor, &budget)?;
|
||
assert_eq!(linked, pipeline);
|
||
```
|
||
|
||
- A compiled function states before linking what its stage will: `stage()`, `requirements()`
|
||
(check it against another profile with `DeviceProfile::check`), `precision_notes()` (the
|
||
pipeline's stage adds those of its own state: a fragment stage's attachment writes, a vertex
|
||
stage's formats its fixed-function vertex input converts),
|
||
`reconvergence()`, `required_subgroup_size()`, `requires_full_subgroups()`, `module_stats()`
|
||
(section 3) and `writes_memory()` (stores or atomics, which a render pipeline drawn in more than
|
||
one color group refuses).
|
||
`compile_function_with_report` also returns a `CompileReport` of what the compile cost.
|
||
- A render pipeline's stages compile under inputs that a decode of its functions learns.
|
||
`render_pipeline_key` returns `RenderPipelineKeys`: each stage's function key, and the inputs to
|
||
compile a missing stage under. So does a compute pipeline's kernel:
|
||
`kernel_inputs_read(&kernel, &inputs, &budget, &entries)` narrows the stage-input descriptor to
|
||
the attributes its entry reads, and the kernel keys and compiles under those, so states that
|
||
differ only in an attribute it never reads share one compiled function. Those are the plain
|
||
module's inputs; `inputs.kernel_variant(variant)` gives each `KernelVariant`'s (`Plain`,
|
||
`PartialThreadgroups`, `EmulatedSimdgroups`), so consumers that lower a kernel's variants on one
|
||
device lower one module per variant. What a creation reads of
|
||
the descriptor past the fetch -- its layout agreement, its reflection, and `compute_pipeline_key`
|
||
-- reads it whole.
|
||
- A key names an artifact by its inputs and versions; a content digest names it by what it says.
|
||
`pipeline.content_digest()` hashes everything but the key and profile, and `plan.content_digest()`
|
||
(with `serde`) everything a plan states. A cache of driver objects keyed on the digest survives a
|
||
release that bumped `SEMANTICS_VERSION` without changing the output. A digest identifies
|
||
content; it authenticates nothing.
|
||
- A compiled function keeps its last relocation beside it, so a sweep of pipeline states over
|
||
one function pair takes the same `Arc<[u32]>` on every creation.
|
||
- A memo that holds pipelines in memory and bounds itself by bytes can weigh one it never encoded.
|
||
`pipeline.footprint()` is the bytes a `RenderPipeline` or a `ComputePipeline` keeps live --
|
||
every word of every stage, each stage's constant blob and notes, and its own storage -- so a
|
||
pipeline kept without
|
||
`to_bytes()` is weighed by what it costs rather than by the zero length of an encoding that was
|
||
not made.
|
||
- Versions move independently: the crate's (the API), `SEMANTICS_VERSION` (the output),
|
||
`ARTIFACT_FORMAT_VERSION` (the encoding) and `RUNTIME_VERSION` (what the runtime does with an
|
||
unchanged artifact).
|
||
|
||
## 8. Refusals, requested inputs, and precision
|
||
|
||
Every failure is typed; match variants, never messages. A refusal says what would change the
|
||
answer:
|
||
|
||
```rust
|
||
let floor = committed_device("runtime-floor")?;
|
||
let min_lod = FunctionDescriptor::new(sampling.function("sample_min_lod")?);
|
||
match floor.compile_function(&min_lod, &LoweringInputs::new(), &Budget::standard()) {
|
||
Err(Error::Unsupported(refusal)) => match (&refusal.feature, &refusal.needs) {
|
||
(feature, Needs::AnyOf(alternatives)) => {
|
||
println!("refused {feature:?}: needs any of {alternatives:?}");
|
||
}
|
||
(feature, needs) => println!("refused {feature:?}: {needs:?}"),
|
||
},
|
||
```
|
||
|
||
`Error` has these variants:
|
||
|
||
| Variant | Means | Retry |
|
||
|---|---|---|
|
||
| `Unsupported` | The device cannot run this exactly. `feature` names the Metal feature, `needs` what would change it | No |
|
||
| `NeedsInput` | The compile needs a pipeline input you did not supply | After supplying it |
|
||
| `InvalidInput` | The AIR or container is malformed | No |
|
||
| `InvalidDescriptor` | The descriptor is invalid as Metal would refuse it, or names a missing entry | No |
|
||
| `LimitExceeded` | A deterministic cap (work units, ledger bytes, output size, structural limits) | No |
|
||
| `Budget` | The deadline passed | Later |
|
||
| `Cancelled` | The `CancelToken` was triggered | Later |
|
||
| `Internal` | A translator bug (a panic, a verifier failure) | No; report it |
|
||
|
||
`Needs` says what a refusal needs:
|
||
|
||
- `AnyOf`: alternative sets of device capabilities; a device that enables every capability of one
|
||
set compiles it.
|
||
- `NotImplemented`: a gap of this version. `NotRepresentable`: no Vulkan mechanism exists.
|
||
- `StageBound`: a stage that the device's plan writes heavier than the translator hands any
|
||
driver (the heaviest measured to compile within host-sized memory); `symbol` names its entry
|
||
weight and the bound. A device whose plan writes it lighter compiles it.
|
||
- `MetalGpuFamily`: a Metal GPU family the device does not claim, where Metal itself refuses the
|
||
input without it.
|
||
|
||
`location` and `symbol` are diagnostics only. A render pipeline's refusal of one attachment's
|
||
state names it (`location.attachment()`). [REFUSALS.md](REFUSALS.md) lists every refusal in the
|
||
code, with its device condition.
|
||
|
||
A compile that needs an input asks for it (`Error::NeedsInput`) instead of guessing: a storage
|
||
texture's format, a linked function, a kernel's stage-input descriptor. Supply it and compile
|
||
again. A function-constant value is no such input: a constant the request leaves undefined
|
||
compiles, as plain Metal's does (section 10, "Function constants").
|
||
|
||
A storage texture's format (`RuntimeInput::StorageTextureFormat { slot }`) comes from what is
|
||
bound: `metal::format::storage_image_choice(pixel_format, device.profile())`, placed in a
|
||
`VariantKey`'s `storage_images` and compiled as a variant. The plan asks for it itself where the
|
||
bound format needs a declared format.
|
||
|
||
**What to cache.** `error.retry()` says whether the same request can succeed later. A
|
||
`BindError` from `plan` depends only on the pipeline, the device and the shape, so it
|
||
memoizes beside the plan; one from instantiation does not.
|
||
|
||
**Precision.** Where an output gives a near-equivalent of Metal's answer, its stage lists it in
|
||
|
||
`precision_notes` (a `PrecisionNote`, each documenting its bound), and `reconvergence` says
|
||
whether subgroup reconvergence after divergence matches Metal's
|
||
(`Reconvergence::ImplementationDefined` where the profile lacks maximal reconvergence). A note
|
||
that holds only under some bound values is in `specialized_notes`;
|
||
`stage.precision_notes_bound(&plan.specialization, &|key| plan.specialized_value(key))` gives the
|
||
notes a plan reaches. Section 9 lists every note.
|
||
|
||
**The runtime's errors** are its own type, `metal2vulkan_runtime::Error`;
|
||
|
||
[REFUSALS.md](REFUSALS.md) lists its refusals with their basis, and its
|
||
[README](../crates/metal2vulkan-runtime/README.md) states each known gap.
|
||
|
||
## 9. Degrade notices
|
||
|
||
Where an answer may differ from Metal's, the operation carries a precision note
|
||
(`metal2vulkan::pipeline::PrecisionNote`), fixed per operation and never decided at run time: by
|
||
the translated module, the pixel format, the descriptor or the state, on every device.
|
||
`metal2vulkan-runtime` gives them through these items:
|
||
|
||
- `ComputePipelineState::precision_notes` and `RenderPipelineState::precision_notes`: the
|
||
translator's notes of the state's modules, with `SwizzledOpaqueBlackBorder` where a function
|
||
samples through a constexpr sampler whose address mode reads an opaque-black border, and
|
||
`DeviceLineRasterization` where a render pipeline's descriptor enables alpha to coverage or
|
||
alpha to one;
|
||
- `Texture::precision_note`: `CompressedTexelDecode` where the texture's own pixel format is
|
||
block-compressed;
|
||
- `SamplerState::precision_note`: `SwizzledOpaqueBlackBorder` where an address mode reads an
|
||
opaque-black border;
|
||
- `DepthStencilState::precision_note`: `DepthDenormals` where the state tests or writes depth;
|
||
- `RenderPassDescriptor::precision_note`: `DeviceRoundedResolve` where a color attachment
|
||
resolves;
|
||
- `BlitCommandEncoder::GENERATE_MIPMAPS_NOTE` (`DeviceFilteredMipmaps`, of `generate_mipmaps`) and
|
||
`RenderCommandEncoder::DEPTH_BIAS_NOTE` (`DeviceDepthBias`, of a draw of triangles under a depth
|
||
bias).
|
||
|
||
A note is never fatal: the work runs on every device. The runtime prints and sends no note; a
|
||
consumer reads them through these items. The translator's own API gives a stage's notes
|
||
(section 8).
|
||
|
||
Each entry below is named by a code, `metal2vulkan-degrade-` and the note's name in kebab case
|
||
(`PrecisionNote::NativeFma` is `metal2vulkan-degrade-native-fma`), with its category, and says what
|
||
makes an operation carry the note and what may differ. Each `PrecisionNote`'s rustdoc states its
|
||
bound.
|
||
|
||
- `metal2vulkan-degrade-device-rounded-resolve` (precision). `DeviceRoundedResolve`: A multisample
|
||
color resolve averages through Vulkan's `AVERAGE` mode, which leaves the rounding of the average
|
||
(and the sRGB encoding) to the device: any
|
||
color attachment but an extended-range one and one resolving into its format's sRGB twin (each
|
||
resolved after the pass), one a fetching pass's sample planes hold as it ends included (the
|
||
planes merged back first). Also, on every device, a resolve into an 8-bit UNORM attachment's
|
||
sRGB twin where the device has
|
||
the sRGB format: the crate-owned resolve decodes the samples and encodes their average by the
|
||
device's own sRGB conversions, where Metal's resolve takes its own 12-bit tables (with correctly
|
||
rounded conversions, 1,758 of the 65,536 pairs of codes at 2 samples land one code from
|
||
Metal's; MoltenVK 1.4.2 on the M3 Max: 696 of 262,144 channels at 2 samples, one code below;
|
||
lavapipe, Mesa 26.0.8: 57,865 of 524,288 at 4, within one code). Measured against Metal's rules
|
||
at 2 and 4 samples on RADV (Mesa 26.0.8, the Radeon 780M and the RX
|
||
7900 XTX), ANV (Mesa 26.2.4, the Iris Xe), NVIDIA (615.71, the RTX 5080 Laptop GPU) and lavapipe
|
||
(Mesa 26.2.4, at 4 samples): every unorm, snorm and packed 16-bit unorm
|
||
channel within one code of Metal's, an sRGB channel within two (NVIDIA's at 4 samples; one
|
||
elsewhere). Each rounds an 8-bit unorm tie down (RADV and ANV: 32512 of `R8Unorm`'s 65536 pairs
|
||
of samples one code below at 2 samples); an sRGB or packed channel lands above Metal's code in
|
||
some texels and below in others. A float channel's average differs in its last bits, counted and
|
||
not bounded (RADV: 8565 of `R32Float`'s 65536 channels at 2 samples). Cost: a resolved channel can
|
||
differ from Metal's by one code (an sRGB channel by two) where the average lies near a rounding
|
||
boundary, a float channel in its last bits, and wholly where a sample is a
|
||
NaN or an infinity: Metal's resolve counts a half or small-float NaN as 0 and a half infinity as
|
||
65536 of its sign, and makes any 32-bit float NaN 0x7fc00000, where the device's average may
|
||
keep them.
|
||
On `metal2vulkan-runtime` it is the static note `PrecisionNote::DeviceRoundedResolve` of a render
|
||
pass that resolves a color attachment (`RenderPassDescriptor::precision_note`), by Vulkan's
|
||
`AVERAGE` in the pass's own rendering, on every device: an extended-range, bfloat or YCbCr
|
||
attachment's resolve is `Gap::RenderPass` of `RenderPassKind::Resolve`, and an integer one is
|
||
`Refusal::ResolveFormat`.
|
||
- `metal2vulkan-degrade-device-filtered-mipmaps` (precision). `DeviceFilteredMipmaps`:
|
||
`generate_mipmaps` of a filtered format (an integer format's texels are picked as plain Metal
|
||
picks them), the static note `BlitCommandEncoder::GENERATE_MIPMAPS_NOTE` on every device: each
|
||
level is the device's linear filter of the one above at the scaled texel centres, its blit chain
|
||
(`vkCmdBlitImage` with `VK_FILTER_LINEAR`); a format the device's blits cannot filter is
|
||
`Gap::MipmapFilter`. The device's filter is not Apple's datapath, whose answers also differ
|
||
between Apple's GPUs (an M3 Max's and an M4's by up to a unit in the last place). No bound
|
||
against Metal's answer is measured. Cost: a channel can differ from Metal's where the filtered
|
||
value lies near a rounding boundary, and wholly where a box averages infinities, NaNs or a float
|
||
format's extremes.
|
||
- `metal2vulkan-degrade-capped-lod-bias` (precision). `CappedLodBias`: A sampler state whose
|
||
`lodBias` the consumer has the device add to every sample's level (`SamplerUse::lod_bias`, for
|
||
a GPU that adds it, as a Mac with an Intel or AMD GPU does; the runtime sets it nowhere and
|
||
samples as an Apple GPU does, with no bias), where the device's samplers cannot add it as it is
|
||
(`metal::sampler::DeviceLodBias`): past the device's `maxSamplerLodBias` (an infinity included),
|
||
a NaN, or a bias other than 0 where the device's portability subset withholds
|
||
`samplerMipLodBias` (MoltenVK). Cost: its samples add the device's
|
||
limit of the bias's sign where the bias is past it, and no bias where the device withholds it or
|
||
the bias is NaN: a sample whose level with what the device adds has not reached the last level
|
||
the sampler reads (a positive bias) or level 0 (a negative one) reads another level than Metal's,
|
||
which adds the whole bias and then clamps the level into the texture's levels; what Metal samples
|
||
for a NaN bias is not measured. The translator states the same difference as the sampler state's
|
||
static note, `PrecisionNote::CappedLodBias` (`DeviceLodBias::note`), which no pipeline stage
|
||
records.
|
||
- `metal2vulkan-degrade-device-reconvergence` (precision). `DeviceReconvergence`: A stage holds a
|
||
tangled instruction (a simdgroup or quad operation, or a barrier whose lanes depend on
|
||
reconvergence) on a device without maximal reconvergence (`shaderMaximalReconvergence`): after
|
||
divergent control flow the device decides which lanes run it together, where Metal's lanes
|
||
reconverge maximally, so such an operation may see other lanes active than Metal's. Under
|
||
subgroup-uniform control flow (`shaderSubgroupUniformControlFlow`) the lanes reconverge where
|
||
control flow is uniform. Static per stage: recorded for any tangled instruction in the entry.
|
||
- `metal2vulkan-degrade-simdgroup-access-order` (precision). `SimdgroupAccessOrder`: A kernel
|
||
holds two accesses of threadgroup or device memory, at least one a write, that different threads
|
||
of one SIMD-group may make to the same bytes between two barriers, and no barrier or atomic
|
||
orders them (a lane reads the slot another lane of its SIMD-group wrote one instruction earlier).
|
||
An Apple GPU runs a SIMD-group's 32 threads in lockstep, so such a pair runs in program order
|
||
there; the module keeps the pair as the AIR states it, and the device runs the threads in its own
|
||
order between barriers, so the values the pair reads and leaves may be other than Metal's. The
|
||
Metal Shading Language calls such a pair a data race, and the difference is an accepted one
|
||
(section 10, "The threads of a SIMD-group run in no order between barriers"). It is the static
|
||
note `PrecisionNote::SimdgroupAccessOrder` of every kernel in which the translator finds such a
|
||
pair, from the AIR's structure alone, on every device under the identity domains
|
||
(`metal2vulkan-runtime`). The resident-table domains order each pair with barriers of their own
|
||
and record none.
|
||
- `metal2vulkan-degrade-snorm-minimum-kept` (precision). `SnormMinimumKept`: A draw in a render pass
|
||
that loads a single-sampled color attachment of a signed normalized format (`R8Snorm`, `RG8Snorm`,
|
||
`RGBA8Snorm`, `R16Snorm`, `RG16Snorm`, `RGBA16Snorm`) and does not discard it at the store. Cost:
|
||
the texels the pass's draws do not write keep their bytes; Metal's pass rewrites a channel's
|
||
minimum code (-128, or -32768 for 16 bits) as the code above it (-127, -32767) in each tile a draw
|
||
reaches: a texel whose bytes no Metal write left (uploaded or copied bytes) keeps a code Metal's
|
||
pass would change. It is the static note `PrecisionNote::SnormMinimumKept` of every
|
||
single-sampled render pipeline that draws into such an attachment, whatever its passes load, on
|
||
its fragment stage (its vertex stage where it has none), on every device.
|
||
- `metal2vulkan-degrade-shared-exponent-kept` (precision). `SharedExponentKept`: A draw in a render
|
||
pass that loads a single-sampled `RGB9E5Float` color attachment and does not discard it at the
|
||
store. Cost: the texels the pass's draws do not write keep their words; Metal's pass normalizes a
|
||
word whose largest mantissa is below 256 (its exponent above 0) in each tile a draw reaches: a
|
||
texel whose bytes no Metal write left (uploaded or copied words) keeps a word Metal would change,
|
||
of the same value. It is the static note `PrecisionNote::SharedExponentKept` of every
|
||
single-sampled render pipeline that draws into such an attachment, whatever its passes load, on
|
||
its fragment stage (its vertex stage where it has none), on every device.
|
||
- `metal2vulkan-degrade-compressed-texel-decode` (precision). `CompressedTexelDecode`: The static
|
||
note of every texture or view whose own pixel format is block-compressed (BC1 to BC7, and the
|
||
ASTC, ETC2 and EAC formats where the device makes them; `Texture::precision_note`), on every
|
||
device. The texture's image is its
|
||
own image of that format, which the device samples through its own decode: no BC texture keeps a
|
||
decoded twin. The bounds are the
|
||
largest differences from Metal's texels measured on random blocks of every format; a driver not
|
||
named is unmeasured. Validation derives no tolerance from them; the conformance suite's BC1 to
|
||
BC5 decode cases hold each texel to its family's largest bound on any driver (an sRGB form's to
|
||
the sRGB decode's largest step over 4/255), and its ASTC cases each linear (`_LDR`) texel to
|
||
1/2048, plus both values' rounding to `float`, justified by the note alone.
|
||
Cost: a read, sample or gather of its texels answers the device's decode of its blocks (an sRGB
|
||
form's code through the device's sRGB decode), where Metal interpolates with 6-bit weights: a BC1
|
||
to BC3 channel up to 4/255 off Metal's on NVIDIA (615.71.09, the RTX 3090 and RTX 5080 Laptop
|
||
GPU; BC3's alpha 6/255), 3/255 on lavapipe, 1/255 on ANV; a BC4 or BC5 channel up to 7.4/255
|
||
(signed 0.065) on NVIDIA, 1.3/255 (signed 0.013) on ANV, 2.4/255 (signed 0.022) on lavapipe, and on RADV
|
||
(Mesa 26.2.4, the Radeon 780M) a signed texel with an endpoint of -128 up to 1.7/255; a BC7
|
||
channel a `float` ulp on lavapipe; its blocks' bytes, copies and sizes are Metal's. An ASTC LDR
|
||
channel is up to 1/2048 off Metal's on ANV (Mesa 26.2.6, the Iris Xe of Raptor Lake-P), over the
|
||
fourteen footprints. Measured
|
||
on more drivers: NVIDIA's 620.70.06 (the RTX 2080) a BC1 to BC3 channel 3/255 (BC3's alpha
|
||
6/255) and a BC4 or BC5 channel 7.33/255 (signed 0.064); ANV (the Iris Xe of Raptor Lake-P) and
|
||
Intel's Windows driver (the UHD 630) BC3's alpha 2/255 and a BC4 or BC5 channel 1.4/255 (signed
|
||
0.0134), a BC4 or BC5 texel within 0.0001 of the specification's exact decode there, so what
|
||
moves is Metal's 6-bit weights.
|
||
- `metal2vulkan-degrade-depth-denormals` (precision). `DepthDenormals`: A write that may store a
|
||
denormal in the 32-bit float depth of a depth image (a pass's clear to one, a blit's copy into a
|
||
`Depth32Float` or `Depth32Float_Stencil8` texture's depth, a `replace_region` whose bytes hold
|
||
one): a driver may store one a write on the GPU gives it as a zero (RADV and ANV do; a host copy
|
||
keeps it), and a depth test may compare one otherwise than Metal's (every driver measured but
|
||
MoltenVK on an Apple M4). Cost: a denormal depth may read, sample and copy out as a
|
||
zero, or the depth test may compare it as zero (a positive one) or below zero (a negative one),
|
||
where Metal keeps it and compares a positive one above zero and a negative one not below it.
|
||
On `metal2vulkan-runtime` it is the static note `PrecisionNote::DepthDenormals` of every
|
||
depth-stencil state that tests or writes depth (`DepthStencilState::precision_note`), on every
|
||
device.
|
||
- `metal2vulkan-degrade-f16-fma-double-rounding` (precision). `PrecisionNote::F16FmaDoubleRounding`:
|
||
Widened half fma rounds twice.
|
||
- `metal2vulkan-degrade-f16-int-conv-double-rounding` (precision).
|
||
`PrecisionNote::F16IntConvDoubleRounding`: Widened half-integer conversions round twice.
|
||
- `metal2vulkan-degrade-grad-from-bias` (precision). `PrecisionNote::GradFromBias`: Bias sampling
|
||
emulated with gradients may pick another level and another anisotropic footprint than Metal's:
|
||
derivatives hoisted out of a per-fragment texture choice, which scale by `2^B` through every
|
||
sampler, and, on every device, a bias whose sum with
|
||
the sampler's own may lie beyond the device's `maxSamplerLodBias`, which Vulkan clamps, or that an
|
||
anisotropic sampler applies to its footprint's minor axis alone. On a device without
|
||
`maintenance8`, a fragment's implicit-level sample with a dynamic texel offset samples with its
|
||
derivatives too, taken before the switch over the offset's values.
|
||
- `metal2vulkan-degrade-device-transcendentals` (precision). `PrecisionNote::DeviceTranscendentals`:
|
||
Transcendentals answer within Apple's stated bound, not Metal's bits: the device's precision for a
|
||
call no other note bounds. Apple states no answer at a NaN operand under fast math, and where the
|
||
device's compiler takes one math mode for the whole entry (MoltenVK) a precise function of a
|
||
fast-math entry answers by its fast form, so a NaN operand may answer a number (`precise::sin` and
|
||
`precise::cos` of a NaN answer `0` on the Apple M3 Max through MoltenVK 1.4.2, where Metal answers
|
||
the NaN).
|
||
- `metal2vulkan-degrade-reduction-order` (precision). `PrecisionNote::ReductionOrder`: Float
|
||
reductions use the device's association order.
|
||
- `metal2vulkan-degrade-device-denormals` (precision). `PrecisionNote::DeviceDenormals`: `float`
|
||
denormals follow the device: Metal reads an `f32` denormal operand of its float arithmetic,
|
||
comparisons, and functions as the zero of its sign and flushes a denormal result to one, where a
|
||
Vulkan device keeps or flushes them by its own float controls (this translation declares no
|
||
`DenormFlushToZero`). Recorded where the module leaves an operation's denormals to the device,
|
||
on every device: `+ - *`, the comparisons, `fma`, `dot`, `sqrt`, `rsqrt`, `trunc`, `rint` and
|
||
`ldexp` on `float` (by the device's products); `fneg`, `fabs`, `floor` and `ceil` on `float`;
|
||
the divisions and the remainder, the device's maths functions (`fast::fmod`'s fused
|
||
multiply-add, `sign` and `fract` among them), `mix` by `FMix`, `fast::sqrt` by `Sqrt`, `dot` of
|
||
vectors of other than two to four lanes, whose chunks' products the device sums where Metal
|
||
flushes each one below `2^-126`, and a conversion to or from `double`, where Metal's reading is
|
||
not measured. The operations that read a denormal on its bits answer Metal on every device:
|
||
`copysign` and `atan2`. Where the minimum and maximum
|
||
functions and `saturate` are the device's `NMin`, `NMax` and `NClamp`, their pick of a denormal
|
||
is `NativeMinMax`'s. Off MoltenVK the device's multiply and fused multiply-add, and `ldexp` on
|
||
`float` by that multiply, round a result whose exact value is below `2^-126` up to `2^-126`, which
|
||
Metal flushes. A nearest pixel-coordinate sample at its texel's
|
||
centre `floor(x) + 0.5`, and a pixel fetch at `floor(x) + k` under an offset, records it for that
|
||
`floor`: Metal's sampler reads a negative subnormal `x` as texel -1, where MoltenVK's `floor`
|
||
reads it as the zero of its sign, texel 0 (an Apple M3 Max). An address mode that reads texel -1
|
||
as texel 0 (clamp to edge, mirror clamp to edge, mirrored repeat) answers alike, so the sample
|
||
records it only where its `s` or `t` mode is repeat, clamp to zero or clamp to the border: through
|
||
a constexpr sampler of such a mode; through a runtime sampler slot only under a pipeline made for
|
||
a nearest pixel state (`SpecKey::NearestPixelSampler`), never under a normalized one, and there
|
||
where the bound state is of such a mode (`SpecKey::BoundSamplerWord`); through an argument
|
||
buffer's sampler
|
||
`argument-pixel-sampler-scaling` records it instead. A pixel fetch under an offset records it
|
||
outright.
|
||
- `metal2vulkan-degrade-pixel-sampler-scaling` (precision). `PrecisionNote::PixelSamplerScaling`:
|
||
A pixel-coordinate sampler's coordinate reaches the device's normalized sampler divided by the
|
||
texture's level-0 size in the shader, and the device reads its texels and weights from that
|
||
coordinate in its own arithmetic: within a few units in the last place of the edge of a linear
|
||
filter's pair of texels, a gather's block or one of the filter's sub-texel steps it may read the
|
||
neighbouring pair, block or step, where Metal's sampler reads the coordinate as given. Recorded
|
||
for a linear sample, a gather and a level query through a constexpr pixel sampler of an array, a
|
||
3D texture or a texture whose residency the function observes, and through a runtime sampler
|
||
slot (where the bound state reads pixel coordinates, and for a sample or comparison where it is
|
||
not a nearest one of one isotropic level). Measured: the device's normalized
|
||
sampler's answer within one `f32` ulp of the quotient on MoltenVK and at the quotient on
|
||
lavapipe; a linear downscale by 8 within 1/255 of Metal's on MoltenVK and 2/255 on lavapipe.
|
||
Under the identity domains it is also recorded for a linear sample and a gather through any
|
||
constexpr pixel sampler, whose device sampler the consumer makes normalized. Nothing is
|
||
recorded where Metal's texel or footprint follows from the coordinate itself: elsewhere a
|
||
constexpr pixel sampler's sample or gather of a 1D or 2D texture that is no array hands the
|
||
coordinate, moved by its offset, to the device's unnormalized sampler; a nearest sample elsewhere
|
||
hands the device its texel's centre, where the device reads exactly Metal's texel; and a nearest
|
||
constexpr sampler clamped to the edge fetches its texel.
|
||
- `metal2vulkan-degrade-device-arithmetic` (precision). `PrecisionNote::DeviceArithmetic`: Float
|
||
division, remainder, square root, fused multiply-add, and dot products use Vulkan's precision
|
||
bounds (e.g. 2.5 ULP division, a possibly unfused `Fma`) rather than correct rounding.
|
||
- `metal2vulkan-degrade-device-signed-zero-inf-nan` (precision).
|
||
`PrecisionNote::DeviceSignedZeroInfNan`: Float operations LLVM requires to keep signed zeros,
|
||
infinities, and NaNs run without `SignedZeroInfNanPreserve`, which the device does not support at
|
||
their width. Without the mode the device may answer such a zero with either sign.
|
||
- `metal2vulkan-degrade-device-ray-intersection` (precision).
|
||
`PrecisionNote::DeviceRayIntersection`: A ray intersection traversed by the device's ray query.
|
||
- `metal2vulkan-degrade-fast-math-rewrites` (precision). `PrecisionNote::FastMathRewrites`: Float
|
||
arithmetic whose AIR lets a compiler rewrite it with a neighbouring operation: an addition or
|
||
subtraction and the multiply feeding it both carry LLVM's `contract`, two chained additions,
|
||
subtractions, or multiplies both carry `reassoc`, or an `llvm.fmuladd` (fused or not, as the
|
||
implementation chooses). The module states each operation in the AIR's order with its own
|
||
flags: one carrying both `contract` and `reassoc` is the device's compiler's to group and fuse,
|
||
any other is kept apart (`NoContraction`); Metal's compiler groups and fuses by its own choice.
|
||
On a device without `shaderFloat16`, a chain of such `half` additions, subtractions and products
|
||
is held at `float` precision between the roundings where its values leave it. On
|
||
MoltenVK a `half` addition, subtraction or product carrying both flags is the 16-bit instruction
|
||
left undecorated, whose conversions Metal's fast compile folds away: the `float` result,
|
||
unrounded. Cost: a sum of mixed signs grouped otherwise, or a multiply-add fused on one side
|
||
alone, differs from Metal's by any amount relative to the result; a held `half` chain, or a
|
||
MoltenVK `half` operation left to the compiler, by the roundings it skips, a value inside it past
|
||
`half`'s range staying finite.
|
||
- `metal2vulkan-degrade-tessellation-winding` (precision). `PrecisionNote::TessellationWinding`: A
|
||
tessellation pipeline's vertex order: Metal's clockwise output winding is declared
|
||
`VertexOrderCcw` and its counter-clockwise one `VertexOrderCw`, as MoltenVK maps them under
|
||
Vulkan's default upper-left domain origin. Measured for both windings against Metal's bytes
|
||
(recorded on an Apple M3 Max) by a consumer's device test,
|
||
`tessellation_states_tessellate_as_metals`, on RADV (Radeon 780M, RX 7900 XTX), NVIDIA Turing
|
||
and MoltenVK; other drivers are not measured.
|
||
- `metal2vulkan-degrade-tessellation-factor-edge-cases` (precision).
|
||
`PrecisionNote::TessellationFactorEdgeCases`: A tessellation pipeline's factors at the edges of
|
||
Metal's stated rules: a subnormal edge factor is positive and does not cull (its level is the
|
||
range's lower end), where a flush to zero would cull the patch; and a NaN inside factor's level is
|
||
the range's lower end.
|
||
- `metal2vulkan-degrade-tessellation-factor-scale` (precision).
|
||
`PrecisionNote::TessellationFactorScale`: A tessellation pipeline with
|
||
`tessellationFactorScaleEnabled` multiplies each factor by the encoder's scale rounded to `half`,
|
||
exactly in `float`, after the cull.
|
||
- `metal2vulkan-degrade-tessellation-factor-instance` (precision).
|
||
`PrecisionNote::TessellationFactorInstance`: A tessellation pipeline whose factor step function is
|
||
per instance reads instance `i`'s factors at `i · instanceStride`, `i` counting the draw's base
|
||
instance as `[[instance_id]]` does.
|
||
- `metal2vulkan-degrade-tessellation-fractional-spacing` (precision).
|
||
`PrecisionNote::TessellationFractionalSpacing`: A tessellation pipeline of a fractional partition
|
||
(`fractionalOdd`, `fractionalEven`) drawn through the device's tessellation stages: Vulkan leaves
|
||
where a fractional spacing places its vertices to the implementation (its two shorter segments'
|
||
length need only fall monotonically with the rounded level less the level, and their place on the
|
||
edge is the implementation's), where Metal's tessellator places them by its own rule. Measured
|
||
equal to Metal's, at factors within the device's range, by a consumer's device tests on RADV
|
||
(Radeon 780M, RX 7900 XTX), NVIDIA Turing, ANV (Intel Iris Xe) and MoltenVK; other drivers,
|
||
lavapipe among them, and factors past the range are not measured. A device without
|
||
`tessellationShader` draws Metal's tessellator through the emulation and records none.
|
||
- `metal2vulkan-degrade-tessellation-triangle-order` (precision).
|
||
`PrecisionNote::TessellationTriangleOrder`: A tessellation pipeline drawn through the device's
|
||
tessellation stages (a device with `tessellationShader`), on every device (no query tells
|
||
Apple's tessellator apart): the device's
|
||
tessellator orders each patch's triangles, picks each one's first corner, and joins the patch's
|
||
rings where Vulkan leaves that to the implementation. The domain points are Metal's; a flat
|
||
varying, a value interpolated across another triangulation, and the order a patch's triangles
|
||
blend in may differ.
|
||
- `metal2vulkan-degrade-unmeasured-group-speculation` (precision).
|
||
`PrecisionNote::UnmeasuredGroupSpeculation`: A function holds a branch whose arms hold a quad or
|
||
simdgroup operation that answers over every lane of the branch's block when Metal's backend
|
||
speculates the arms (a quad shuffle, broadcast or vote, a simdgroup shuffle, `simd_is_first`),
|
||
where no measurement says whether Metal speculates them: an arm of an instruction, size or shape
|
||
no probe covers. The branch keeps the AIR's lanes, so that operation may answer over the lanes
|
||
that take the arm where Metal's answers over the block's. A branch Metal was measured to
|
||
speculate is speculated, and one it was measured to keep is kept, with no note.
|
||
- `metal2vulkan-degrade-device-sample-level` (precision). `PrecisionNote::DeviceSampleLevel`: An
|
||
extended-range texture's (`BGR10_XR`, `BGRA10_XR` and their sRGB twins) fragment sample at an
|
||
implicit level, or with explicit gradients, on every device: its texels are read in the shader, at
|
||
Vulkan's level of detail computed there and Metal's level rule, where a constexpr sampler states a
|
||
mip filter or a runtime sampler's word does. The device takes the level of detail by Vulkan's
|
||
rules, whose scale factor may be approximated; Metal's level of detail from derivatives is not
|
||
measured. The sample may read another level or filter than Metal's near a boundary.
|
||
- `metal2vulkan-degrade-unmeasured-fetch-blend` (precision). `PrecisionNote::UnmeasuredFetchBlend`:
|
||
A fragment function reads a color attachment the pipeline blends, on a device that orders
|
||
framebuffer fetch only through the pixel interlock (MoltenVK, RADV, ANV), of a format whose Metal
|
||
blend arithmetic is not built: a format whose channels read as floats but a 16-bit float, a 32-bit
|
||
float, an sRGB and an extended-range one (a 16-bit normalized, 8-bit signed normalized, 10-bit,
|
||
packed float or `A8Unorm` format, among others), an 8-bit unorm one written by an output of fewer
|
||
than four lanes, a 32-bit float one (`R32Float`, `RG32Float`, `RGBA32Float`) written by an output
|
||
of fewer than four lanes, by a module without fast math, or under a blend state the one sweep that
|
||
measured that family does not fit: a color or alpha arithmetic of more than a lone term, a lone
|
||
fused multiply-add, or a minimum or maximum, outside the states measured word for word. A
|
||
one-channel attachment's `SourceColor` over `DestinationColor` is such a state, and so is a blend
|
||
color's difference, which a 16-bit float attachment's blend fits and a 32-bit float one's does
|
||
not; a lone term that reads the blend color, `OneMinusBlendColor` over `Zero` among them, is
|
||
fitted and takes the exact form. Also a 16-bit float or 8-bit unorm one under a blend of the
|
||
second source or beside a second (index 1) output. No Vulkan blend applies to the storage write,
|
||
so the shader computes the pipeline's blend in `float`: each factor, product and sum rounded to
|
||
nearest even, the source and the blend color clamped first to a normalized format's range (a NaN
|
||
to its low end), an output's missing lane zero, and the result stored through the device's
|
||
conversion. Metal's blend arithmetic for these formats is not measured, so the bound is not known.
|
||
A factor of the second source reads the function's second output, clamped as the source. An 8-bit
|
||
sRGB attachment (`R8Unorm_sRGB`, `RG8Unorm_sRGB`, `RGBA8Unorm_sRGB`, `BGRA8Unorm_sRGB`) under a
|
||
blend of the second source, a one- or two-channel one under a lerp, each in a module compiled with
|
||
fast math, and one written by an output of fewer than four lanes, record it too: the shader
|
||
computes Metal's 8-bit unorm blend's rounded forms over the decoded texel, in the output's type (a
|
||
missing lane zero), and stores the result by the device's sRGB encode (`DevicePackConversion`),
|
||
Metal's blend without fast math at four lanes; under fast math Metal regroups the blend,
|
||
unmeasured.
|
||
- `metal2vulkan-degrade-unordered-fetch-within-draw` (precision).
|
||
`PrecisionNote::UnorderedFetchWithinDraw`: A fragment function reads a color attachment
|
||
(framebuffer fetch) on a device with neither rasterization-order color attachment access nor
|
||
`fragmentShaderPixelInterlock` (Honeykrisp on Apple's M2 Pro; RADV on the RX 550), or under the
|
||
identity domains on any device without rasterization-order access, the pixel interlock's included:
|
||
there the interlock chooses no fetch form, as its storage form is a descriptor the identity
|
||
domains do not hold. It reads the attachment as an input attachment, and the runtime records a
|
||
barrier of the draws' subpass's dependency on itself, by region, before each draw that reads input
|
||
attachments after an earlier draw of its render pass instance, or its load operations, wrote a
|
||
color, and before each draw that writes a color after an earlier one read them: a fetch reads what
|
||
every earlier draw stored at its pixel, Metal's answer. Under the identity domains it reads the
|
||
attachment's texture at its own pixel instead (`PushField::FetchedColor`), and the consumer orders
|
||
each fetching draw after the earlier draws of its pass by a barrier between them (the runtime's
|
||
`Metrics::attachment_loop_reads`, or its copies into the attachment's shadow on a device without
|
||
the attachment feedback loop). Vulkan orders nothing between the overlapping fragments of one draw
|
||
there, where Metal orders them in primitive order: a fragment that covers a pixel an earlier
|
||
fragment of its own draw covered may read the texel from before that fragment's write. Exact for a
|
||
draw whose primitives do not overlap at a pixel; otherwise each such pixel holds the result of
|
||
some order of the draw's fragments there. A raster order group on the color input, or on a color
|
||
output of an attachment the function reads, takes the same form.
|
||
- `metal2vulkan-degrade-fp8-value` (precision). `PrecisionNote::Fp8Value`: An 8-bit float converts
|
||
to an integer from the format's documented value with Metal's saturating float-to-integer rule,
|
||
where Metal's device here answers otherwise: Metal on Apple M3 compiles these reads but extends
|
||
the carrier byte as an integer, the same for every format, so the result is unverified on Metal.
|
||
- `metal2vulkan-degrade-multisample-position` (precision). `PrecisionNote::MultisamplePosition`: A
|
||
fragment function of a render pipeline that rasterizes more than one sample reads a value Metal
|
||
reads at the pixel center in every invocation and Vulkan evaluates at an implementation-defined
|
||
position within the fragment: `[[position]]`'s z or w (the interpolated depth and `1/w`, whose
|
||
position Vulkan does not state), a center-sampled varying on a device without its pixel form
|
||
(interpolation functions whose `InterpolateAtOffset` reads from the pixel center: "an
|
||
implementation-defined position within the fragment area being shaded"), a centroid varying or an
|
||
`interpolant` read through `interpolate_at_centroid` (anywhere in the fragment's covered area,
|
||
where Metal, measured on Apple M3 Max, moves it to the lowest covered sample's location only under
|
||
partial coverage), an `interpolant` read through `interpolate_at_center` as a center varying,
|
||
`[[point_coord]]` ("the fragment center, or the location of a sample"), or `[[barycentric_coord]]`
|
||
(its builtin). A function that runs per sample under sample shading reads all but the center
|
||
varying where Vulkan evaluates them for its sample, where Metal reads every one as the pixel's in
|
||
each sample's run (Apple M3 Max at 4x, measured). Each is evaluated at a point of the pixel, so it
|
||
differs from Metal's by at most its change over half the pixel's diagonal, and a centroid varying
|
||
by at most its change across the pixel's covered area; exact where the device evaluates a read
|
||
where Metal does.
|
||
- `metal2vulkan-degrade-rasterization-rate-map` (precision). `PrecisionNote::RasterizationRateMap`:
|
||
A render pass drawn by regions over a rasterization-rate map that is not the identity: its
|
||
regions' lines are this crate's tiles' exact lines, where Apple's are its fixed-point ones, so a
|
||
fragment's evaluated point lies within a bounded distance of Metal's.
|
||
- `metal2vulkan-degrade-bicubic-filter-arithmetic` (precision).
|
||
`PrecisionNote::BicubicFilterArithmetic`: a filter emulated by fetches (Metal's bicubic filter on
|
||
every format, and an extended-range texture's linear, 3D and linear-mip-filter samples) sums its
|
||
taps in the device's arithmetic: the Catmull-Rom weights computed in `f32` from the fraction, the
|
||
taps summed by the device's multiply and add, a bilinear, trilinear or level blend by its `FMix`
|
||
at the `f32` fractions, and a `half` result the device's narrowing. The footprint, addressing,
|
||
filter choice, level and clamps are Metal's. Metal's fixed-point weights, exact grid sums, float
|
||
accumulators and sixty-fourth level fractions are not reproduced: a lane may differ by a few grid
|
||
steps (`1/4080` at 8 bits), by up to `2.1e-3` on an extended-range sRGB texture's bicubic sample,
|
||
and by up to `1/64` of the two levels' difference under a linear mip filter. Measured for the
|
||
bicubic filter by the conformance case `bicubic-texel-classes` (Metal on an Apple M3 Max, macOS
|
||
27.0.1): Metal's results lie on the format's grid, a sixteenth of a code for `RGBA8Unorm`,
|
||
`R8Unorm` and `RGBA8Snorm`, a whole code for `RGBA16Unorm` and an exact `half` for
|
||
`RGBA16Float`, and at the six `RGBA16Float` samples whose sum lies exactly halfway between two
|
||
halves Metal rounds away from zero. Ours is the unrounded `f32` sum; `R32Float`'s samples
|
||
compare exactly. The largest difference per format, `float` and `half` results together, over
|
||
lavapipe, the M3 on KosmicKrisp and on MoltenVK, a Radeon 780M, an RTX 3090 and an Iris Xe:
|
||
`RGBA8Unorm` `2.44e-4`, `R8Unorm` `1.53e-4`, `RGBA8Snorm` `4.88e-4`, `RGBA16Float` `3.74e-4`,
|
||
`RGBA16Unorm` `2.16e-4` on lavapipe, KosmicKrisp and the 3090 and `7.8e-6` elsewhere (lavapipe's
|
||
`half` results keep their `f32` values). Through a runtime sampler slot it holds only where the
|
||
bound sampler filters (a linear minification or magnification filter, anisotropy, a cubic filter
|
||
or a linear mip filter): a nearest sampler's sample is the decoded texel at Metal's position.
|
||
Through an argument buffer it holds only where the command can name a texture held by its raw
|
||
texels in a form the function filters (`BGR10_XR`, `BGRA10_XR`, the padded forms; not a bfloat
|
||
one).
|
||
- `metal2vulkan-degrade-sparse-residency` (precision). `PrecisionNote::SparseResidency`: A stage
|
||
samples or gathers a texture whose residency it observes (`sparse_color::resident`) through the
|
||
slot's sparse form, which the module has where the profile enables `shaderResourceResidency`:
|
||
the word is the device's `OpImageSparse*` residency, whose footprint may count a texel of zero
|
||
weight Metal does not or call a footprint of the border alone not resident where Metal answers
|
||
resident, and an unmapped texel's lanes the device's non-resident value. Where the
|
||
device has no residency of its own, the slot's tile map answers Metal's footprint rule exactly,
|
||
unrecorded.
|
||
- `metal2vulkan-degrade-device-nan-encoding` (precision). `PrecisionNote::DeviceNanEncoding`: A
|
||
`float` NaN result's bit pattern follows the device: Metal answers the canonical `0x7fc00000` for
|
||
the NaN results of its float arithmetic and functions, and Vulkan states no encoding, so a device
|
||
may answer another sign or payload (llvmpipe: `0xffc00000`). Also a blend
|
||
a fragment function computes into a 16-bit float attachment through the pixel interlock: a NaN
|
||
of the blend is the device's arithmetic's, which the storage image's store converts, where Metal
|
||
stores every NaN of the blend as `0x7e00`. A texture write records it where the stored NaN is the
|
||
device's: a constant texel none of whose lanes is a NaN records nothing, and a `half` or
|
||
`float` write through an argument buffer holds only where the command can name a texture of a
|
||
format family whose stored NaN is the device's (for a `half` write, a texture of any format that
|
||
holds a NaN; for a `float` write, a 16-bit float or `RG11B10Float` one). So does a fragment
|
||
function's color output where the device's conversion stores its NaN, but one every return of
|
||
which holds such a constant records nothing.
|
||
- `metal2vulkan-degrade-restricted-depth-range` (precision). `PrecisionNote::RestrictedDepthRange`:
|
||
The device compares a depth texture's texels on a device without `VK_EXT_depth_range_unrestricted`
|
||
enabled (MoltenVK on the M3 Max, ANV on the Iris Xe, RADV), where Vulkan gives a depth image no
|
||
value outside `[0, 1]`, whatever its driver is measured to keep: an upload into a 32-bit float
|
||
depth is refused (`Gap::FloatDepthUpload`), and a render write is clamped to the viewport's
|
||
depth range. A
|
||
comparison of a texel Metal holds outside `[0, 1]` can differ; one within it is exact.
|
||
- `metal2vulkan-degrade-cube-gradient-footprint` (precision).
|
||
`PrecisionNote::CubeGradientFootprint`: An explicit-gradient sample of a cube texture takes
|
||
Metal's footprint in the form Vulkan's rule reads, in every stage on every device. Cost: the
|
||
sample takes the device's logarithm of Metal's footprint and, if anisotropic, the device's taps,
|
||
so a level or blend near a boundary can differ from Metal's. Where the shader takes Metal's level
|
||
and taps itself (`PrecisionNote::AnisotropicFilter`) this note is not recorded.
|
||
- `metal2vulkan-degrade-half-texel-conversion` (precision). `PrecisionNote::HalfTexelConversion`:
|
||
The module reads, gathers, or samples a texture with a `half` result, the device's `float` texel
|
||
rounded to nearest even on every device: within one `half` ulp of Metal's, but a 32-bit float
|
||
texel's sample past `±65504`, the infinity where Metal saturates. Recorded outright for every
|
||
such access, a heap element's and a depth texture's included: the device's `float` texel is its
|
||
own.
|
||
- `metal2vulkan-degrade-half-sample-filter` (precision). `PrecisionNote::HalfSampleFilter`: The
|
||
module samples a texture with a `half` result through a filter (linear minification or
|
||
magnification, a linear mip filter, anisotropy, or a runtime sampler slot whose bound sampler
|
||
filters so) by Vulkan's Y′CbCr model of a 4:2:0 two-plane YCbCr texture's planes read in place,
|
||
which blends the codes, not Metal's exact filter of the format, on every device.
|
||
- `metal2vulkan-degrade-srgb-texel-decode` (precision). `PrecisionNote::SrgbTexelDecode`: The module
|
||
reads, samples, or gathers a texture with a `half` or `float` result through the device's sRGB
|
||
decode, or decodes the device's filter of its codes, where the texture may be bound in an 8-bit
|
||
sRGB format. The device's decode is the only form wherever it samples the format (an 8-bit sRGB
|
||
format, or the sRGB form of a BC, PVRTC, ETC2, EAC or ASTC format, through the texture's or its
|
||
decoded twin's sRGB view), so it holds where a command binds such a texture, at an argument
|
||
buffer's texture access, and for a render pipeline that reads an sRGB color attachment as an
|
||
input attachment, on every device. Where the
|
||
device holds the format as its UNORM twin, or a storage texture holds it through its UNORM
|
||
alias (at a slot, or held by an argument buffer), the module decodes the codes (or the
|
||
device's filter of them) by the MSL transfer through the device's `Pow`, at every such access on
|
||
every device.
|
||
- `metal2vulkan-degrade-float-sample-filter` (precision). `PrecisionNote::FloatSampleFilter`: The
|
||
module samples a texture with a `float` result through a filter by Vulkan's Y′CbCr model of a
|
||
4:2:0 two-plane YCbCr texture's planes read in place, which blends the codes, not Metal's exact
|
||
filter of the format, on every device.
|
||
- `metal2vulkan-degrade-function-storage-assumed` (precision).
|
||
`PrecisionNote::FunctionStorageAssumed`: A vertex or fragment function keeps more private memory
|
||
(its `alloca`s, a call stack among them, or constants held as `Private` variables) than the floor,
|
||
16 KiB a thread, on a device whose Function storage was not measured, up to the 192 KiB a thread
|
||
Metal was measured to hold (`device::function_storage`). The risk is the driver's: past what it
|
||
holds it can answer wrongly or hang the GPU with no error.
|
||
- `metal2vulkan-degrade-quads-counted-before-discard` (precision).
|
||
`PrecisionNote::QuadsCountedBeforeDiscard`: A fragment function runs a non-quad simdgroup
|
||
operation (a reduction, scan, ballot, broadcast, shuffle, vote, or simdgroup matrix) before a
|
||
discard on a path to it, in a shape where Metal's placement of it is not known.
|
||
- `metal2vulkan-degrade-view-base-texel-offset` (precision). `PrecisionNote::ViewBaseTexelOffset`:
|
||
The module places a texel offset on an axis that may repeat or mirror as Metal does in an access
|
||
of an argument buffer's texture: in an emulated raw sample (`heap_raw`) or a bicubic filter's
|
||
footprint.
|
||
- `metal2vulkan-degrade-device-color-conversion` (precision).
|
||
`PrecisionNote::DeviceColorConversion`: A fragment function writes a float to a color channel
|
||
whose conversion the device makes, on every device: a normalized, sRGB or small float channel,
|
||
an attachment that blends with factors other than one and zero
|
||
(`ChannelWrite::Device`), the alpha of attachment 0 under alpha-to-coverage, a format whose
|
||
conversion Metal was not measured with (the packed 16-bit unorm formats, `A8Unorm`, the packed
|
||
floats). Near a tie between two codes the device may store the other one: the RTX 5080 Laptop
|
||
GPU (NVIDIA 615.71.09) stores 0.5 in an 8-bit unorm channel as `0x7f`, and the lower code of
|
||
every input up to two `f32` steps above a tie, where Metal stores `0x80` and rounds the exact
|
||
product to nearest, a tie up; ANV, RADV and lavapipe round the `f32` product with ties to even.
|
||
Also a `float` texel a function writes to a storage texture that may hold a normalized or
|
||
half-float format, or `ARGB8Unorm`; an integer texel written into a storage texture of
|
||
the float class that may hold a half-float or small-float format, which Metal rounds to nearest
|
||
even and the device's store as it rounds a `float` (toward zero on every measured driver, one
|
||
unit in the last place, a finite value past 65504 stored as 65504); and a fragment function's
|
||
`float` blend into a 16-bit float attachment through the pixel interlock, and a blend into an
|
||
8-bit unorm attachment through
|
||
the pixel interlock, whose result the storage image's store converts to its code, or into an
|
||
`ARGB8Unorm` attachment over its input attachment where no Vulkan blend state on its rotated
|
||
image gives Metal's (a destination alpha in a color factor, say), whose result the attachment's
|
||
write converts; and a render pipeline's private logic operation that the fragment function
|
||
computes on a device without `logicOp` into an 8- or 16-bit unorm or snorm attachment, which
|
||
converts the output to the codes it operates on by the device's `float` product rounded to
|
||
nearest even where Metal rounds the exact product (the two differ only near a half-code tie).
|
||
The recorded bound past one code, measured on
|
||
lavapipe against plain Metal on an Apple M3 Max: a blend into an 8-bit unorm attachment
|
||
(`RGBA8Unorm`, `ARGB8Unorm`) within 2 codes of Metal's; a fragment function that fetches an 8-bit
|
||
sRGB attachment and writes it again, within 2 codes once a later draw reads an earlier one's code
|
||
(with `srgb-texel-decode`); a blend into an 8-bit sRGB attachment, within 4 codes; a blend into a
|
||
4-sample `A1BGR5Unorm` attachment the device renders itself (Mesa 26.2.4), within 2 codes, over a
|
||
sample whose fill it stored a code from Metal's. RADV (Mesa 26.2.6 on the Radeon 780M) blends
|
||
into an `RGB9E5Float` attachment it renders itself a source whose channels and alpha are halves
|
||
rounded toward zero, a value past 65504 as 65504, where Metal blends the `float`s (Vulkan asks a
|
||
blend for no more precision and range than the attachment's): at 2 and 4 samples, 30 of 96
|
||
blended samples are not Metal's, up to 33 units of the larger value's last mantissa bit apart
|
||
where the source passes 65504, and 7 where the half's alpha weighs a destination far larger than
|
||
the source. Another device
|
||
is not bounded past one code (an NVIDIA RTX 5080 Laptop GPU's blend into an 8-bit sRGB attachment
|
||
stored up to 11 codes from Metal's).
|
||
- `metal2vulkan-degrade-device-interpolation` (precision). `PrecisionNote::DeviceInterpolation`: A
|
||
fragment function reads a varying the device interpolates (`center_perspective`, `centroid`,
|
||
`sample` or `no_perspective`, not `flat`), on every device. Vulkan states no precision for
|
||
interpolation, and RADV, ANV, lavapipe and NVIDIA's driver each round
|
||
most interpolated lanes otherwise than Metal, up to about `1.5e-4` apart (`3e-4` on lavapipe) on
|
||
the probes. RADV's read of a perspective input at a sample or an offset is further off, and
|
||
records `extrapolated-interpolation`.
|
||
- `metal2vulkan-degrade-device-interpolation-offset` (precision).
|
||
`PrecisionNote::DeviceInterpolationOffset`: A fragment function reads an `interpolant` through
|
||
`interpolate_at_offset`, which Metal evaluates at the offset as given from the pixel's upper-left
|
||
corner, at any distance and on no grid (an Apple M3 Max, macOS 27.0.1: 0.999 read at 0.999, `1e9`
|
||
extrapolated, a NaN or infinite offset NaN). The device reads at the offset moved to its origin
|
||
(the pixel center; 7/16 on MoltenVK) and clamped to its `[minInterpolationOffset,
|
||
maxInterpolationOffset]`, which Vulkan requires, a NaN to the minimum, and rounds it to its
|
||
`subPixelInterpolationOffsetBits` (at least sixteenths): within the range the read lies within
|
||
half a step of Metal's, past it at the range's nearer end. On MoltenVK Metal reads the offset
|
||
itself, so within the range (`[-1/16, 15/16]` from the corner on the M3 Max) the read is Metal's.
|
||
- `metal2vulkan-degrade-extrapolated-interpolation` (precision).
|
||
`PrecisionNote::ExtrapolatedInterpolation`: A fragment function reads a perspective input at a
|
||
sample or an offset (`interpolate_at_sample`, `interpolate_at_offset`, or a sample-qualified
|
||
varying a function looping over its covered samples reads at each pass's sample), recorded on
|
||
every device: a device may answer such a read with the value at the pixel center extrapolated
|
||
along the quad's coarse derivatives, as RADV does, where Metal
|
||
interpolates in perspective at the location itself. The answer is first-order, off by the
|
||
value's curvature across the distance, which grows with the triangle's spread of clip `w`: on a
|
||
triangle of `w` 1, 2 and 4 over a 6x6 target, up to about 1.0 in a lane whose vertices hand 3,
|
||
-2 and 0.5 (the Radeon 780M, Mesa 26.2.6). A no-perspective input is exact there and records
|
||
nothing. It is the device's own interpolation, which Vulkan states no precision for.
|
||
- `metal2vulkan-degrade-quads-split-by-clipping` (precision). `PrecisionNote::QuadsSplitByClipping`:
|
||
A fragment function runs a quad or simdgroup operation, recorded on every device: a device may
|
||
shade a triangle it clips as several triangles, and then a quad a seam between the pieces crosses
|
||
runs once per piece, each run with the other piece's pixels as helper invocations, so the
|
||
operation counts or combines fewer active lanes than Metal's, whose quad holds all four.
|
||
- `metal2vulkan-degrade-fragment-simdgroup-grouping` (precision).
|
||
`PrecisionNote::FragmentSimdgroupGrouping`: A fragment function runs a simdgroup operation (a
|
||
reduction, scan, vote, ballot, shuffle, broadcast or AGX multiply-add) or reads its
|
||
`[[thread_index_in_simdgroup]]`, or discards, asks `quad_is_helper_thread` or
|
||
`simd_is_helper_thread` and runs a quad operation, on every device. A
|
||
simdgroup operation runs over the pixels the device's rasterizer packs into a subgroup, so a sum,
|
||
a ballot or a shuffle combines other pixels than on an Apple GPU, whose packing is measured, and
|
||
the lane is the pixel's place in the device's subgroup, or in its aligned 32 lanes where the
|
||
subgroup holds 64 or 128 (each such run of 32 lanes is one simdgroup, as
|
||
`[[threads_per_simdgroup]]`'s 32 says). An Apple GPU also shades some quads of a
|
||
function that discards as several partial quads from the function's start, each run holding the
|
||
quad's other covered pixels as helpers, where the device shades them as one quad: a ballot of the
|
||
helper query names covered pixels as helpers on the M3 Max (3 of the 16 quads of an 8x8 target, 2
|
||
to 4 of the 12 of a 7x5 one, under the full-screen triangle with every third pixel discarded) and
|
||
none on the device. A device whose fragment subgroup holds fewer than two quads combines the
|
||
one quad's pixels alone, so a reduction ahead of a `discard_fragment` counts no quad the discard
|
||
drops, where an Apple GPU's simdgroup counts the dropped quads of its 8x4 pixel block; the
|
||
device states one fragment subgroup size, so no required size widens it, and no exact form
|
||
exists there.
|
||
- `metal2vulkan-degrade-blended-write-rounding` (precision). `PrecisionNote::BlendedWriteRounding`:
|
||
A fragment function writes a float to a half-float color channel of a render pipeline under
|
||
`TextureWriteRoundingMode::NearestEven` that blends with factors other than one and zero
|
||
(`ChannelWrite::BlendedNearestEven`): Metal rounds the blend's result to nearest even, and the
|
||
device's blend converts it by its own rounding (MoltenVK's toward zero), so a blended value off
|
||
the half grid may store the neighbouring half; under a constant factor Metal's result is neither
|
||
rule's on 2,376 of 580,508 `RGBA16Float` channels near the half denormals.
|
||
- `metal2vulkan-degrade-coverage-reads-rounded-alpha` (precision).
|
||
`PrecisionNote::CoverageReadsRoundedAlpha`: A render pipeline covers by alpha
|
||
(`alphaToCoverageEnabled`) under `TextureWriteRoundingMode::NearestEven`, and its fragment
|
||
function writes a `float` alpha to a half-float attachment 0 that stores it as written: Metal
|
||
stores the alpha rounded to the nearest-even half and reads the coverage from the alpha before
|
||
rounding, which no device separates, so the function rounds the alpha and the coverage reads the
|
||
rounded one. The stored alpha is Metal's; a fragment whose alpha lies within half a `half` step
|
||
of a coverage threshold may cover one sample more or fewer (2 of 16,384 alphas on an Apple M3
|
||
Max). Which samples an alpha covers is the device's own (`device-alpha-coverage`).
|
||
- `metal2vulkan-degrade-device-alpha-coverage` (precision). `PrecisionNote::DeviceAlphaCoverage`: A
|
||
render pipeline covers by alpha (`alphaToCoverageEnabled`, over a four-component float first
|
||
output at attachment 0), on every device. The device converts the alpha to its coverage mask by
|
||
its own algorithm, which Vulkan does not specify ("It is intended that the number of 1's in this
|
||
value be proportional to the alpha value", and "The algorithm may be different at different
|
||
framebuffer coordinates"). Metal, measured over 639 alphas a sample count on an Apple M3 Max under
|
||
macOS 27.0.1, covers a pixel at one sample above an alpha of 1/2, and at two and four samples
|
||
`ceil(alpha * n - t)` samples, `t` a 2x2 ordered dither of the pixel's window coordinates (1/8,
|
||
5/8; 7/8, 3/8), either count within an `f32` ulp of a threshold. Recorded bound, pixel by pixel
|
||
against Metal's reading: on lavapipe (Mesa 26.0.8, arm64) every pixel covers `floor(alpha * n)` or
|
||
`ceil(alpha * n)` samples, so within one sample of Metal's and Metal's where `alpha * n` is an
|
||
integer; at one sample, where lavapipe dithers too, a whole fragment is stored or dropped where
|
||
its alpha lies between its own threshold and 1/2 (2,560 of the sweep's 10,224 pixels); at four
|
||
samples a pixel is one sample apart only within an `f32` ulp of a threshold (96 of 10,224). An
|
||
NVIDIA RTX 5080 Laptop GPU stores a fragment of alpha 1/2 at one sample, where Metal drops it;
|
||
every other device and count is unmeasured.
|
||
- `metal2vulkan-degrade-rotated-blend-rounding` (precision). `PrecisionNote::RotatedBlendRounding`:
|
||
A fragment function writes an attachment held with its channels rotated (`ARGB8Unorm`) whose blend
|
||
is not exact by construction (`LoweringInputs::rotated_blends`): every blend but a minimum, a
|
||
maximum, and one whose Vulkan factors are all `ZERO` or `ONE`. A blend may compute a factor pair
|
||
apart in its color and alpha arithmetic, as Apple's does, and the rotation moves Metal's blue into
|
||
the image's alpha and Metal's alpha into its red, so a channel may store one code apart from
|
||
Metal's.
|
||
- `metal2vulkan-degrade-linked-sampler-numbering` (precision).
|
||
`PrecisionNote::LinkedSamplerNumbering`: The sampler resource IDs a pipeline state takes for the
|
||
constexpr samplers of the visible functions its stage links may be other than Metal's: two or more
|
||
linked functions bring constexpr samplers, which Metal numbers in an order the pipeline's inputs
|
||
do not decide; or a function a table can hold that linking did not import could not be planned, so
|
||
its samplers are not known; or a dynamic library's symbol, a binary function or a function-pointer
|
||
group's function brings them, whose numbering is not measured.
|
||
- `metal2vulkan-degrade-argument-pixel-sampler-scaling` (precision).
|
||
`PrecisionNote::ArgumentPixelSamplerScaling`: A sample, gather or comparison through an argument
|
||
buffer's sampler whose word the specialized module cannot read may hand the device a pixel
|
||
coordinate scaled by the texture's size, where the sampler states pixel coordinates, with
|
||
`pixel-sampler-scaling`'s bound. A sample through a state of nearest filters, one level and no
|
||
anisotropy hands the device its texel's centre instead, where the device reads exactly Metal's
|
||
texel but at a negative subnormal coordinate, whose `floor` is the device's
|
||
(`device-denormals`' reading, which this note records for such a sampler).
|
||
- `metal2vulkan-degrade-half-output-precision` (precision). `PrecisionNote::HalfOutputPrecision`: A
|
||
fragment function returns a `half` color output (a scalar or vector of AIR's `half`) to an
|
||
attachment the pipeline has, and either its module is compiled without fast math
|
||
(`air.compile.fast_math_disable`) and that attachment blends, factors one and zero included, with
|
||
some channel written (`StageIo::half_blend_colors`, which the pipeline's stage reads), or its
|
||
module names no math mode (under fast math the pipeline's stage records
|
||
`PrecisionNote::HalfOutputFastMathBlend` instead).
|
||
- `metal2vulkan-degrade-extended-range-blend-source` (precision).
|
||
`PrecisionNote::ExtendedRangeBlendSource`: A fragment function's `half` color output (a scalar or
|
||
vector of AIR's `half`) is blended into a padded 10-bit attachment (`R10Unorm_X6`,
|
||
`RG10Unorm_X12`, their `_sRGB` twins, `RGB10A2Unorm_sRGB`), whose raw codes no device's blend
|
||
unit reads, so the function blends in the shader in the device's arithmetic: `S·Fs op D·Fd` in
|
||
`float`, in the blend equation's order, the source, second source and blend color unclamped, as
|
||
Metal takes them there (a blend into one of Apple's extended-range formats is refused, section
|
||
4). The device's compiler may contract a
|
||
product into its sum and may regroup the blend: MoltenVK on the M3 Max and NVIDIA's driver on an
|
||
RTX 5080 compute a second source's blend `S·F + D·(1 - F)` as `D + F·(S - D)`. Metal's compiler
|
||
rounds each step to `half`, so a blended channel may store another code than Metal's: within two
|
||
codes on a linear format and within six on an sRGB twin near zero, where its transfer is
|
||
steepest, in the conformance suite's extended-range and padded blend cases; and where the
|
||
device's compiler regroups a padded format's blend, an infinite second source stores the end code
|
||
its sign picks where Metal's sum is NaN, stored as code 0.
|
||
- `metal2vulkan-degrade-extended-range-float-blend-source` (precision).
|
||
`PrecisionNote::ExtendedRangeFloatBlendSource`: `PrecisionNote::ExtendedRangeBlendSource` for a
|
||
`float` color output (a scalar or vector of AIR's `float`): blended into a padded 10-bit
|
||
attachment in the shader in the device's arithmetic, which the device's compiler
|
||
may contract and regroup, where Metal's compiler rounds the output and each step of the blend to
|
||
`half`; a blended channel within `ExtendedRangeBlendSource`'s bound of Metal's.
|
||
- `metal2vulkan-degrade-anisotropic-filter` (precision). `PrecisionNote::AnisotropicFilter`: The
|
||
module samples a texture through an anisotropic sampler with Metal's anisotropic filter emulated
|
||
by the device's samples at explicit levels (`lower::sampler::anisotropic`), on a device without
|
||
`samplerAnisotropy`, every Vulkan sampler being isotropic there, each tap the device's isotropic
|
||
filter: a sample without comparison of a 2D texture or 2D array at the fragment's implicit
|
||
level or with explicit gradients, or of a cube or cube array with explicit gradients, through a
|
||
constexpr sampler of `maxAnisotropy` above 1, or through a runtime sampler slot whose word states
|
||
anisotropy (under that word alone). A device with the feature samples with its own anisotropic
|
||
filter. The footprint's logarithms and powers are the device's `Log2` and `Exp2`, its level and
|
||
spacing unrounded, and a linear mip filter's two levels the device's own blend at the fractional
|
||
level, where Metal takes a piecewise-linear `log2`, 256ths and 4096ths. Cost: a
|
||
cube's taps step across its face by Metal's face gradients, and a tap past the face's edge reads
|
||
the next face as the device projects it, which Metal's own is not measured against.
|
||
- `metal2vulkan-degrade-device-anisotropic-filter` (precision).
|
||
`PrecisionNote::DeviceAnisotropicFilter`: The module samples a texture through a sampler whose
|
||
anisotropy is known where Metal's anisotropic filter is not emulated
|
||
(`PrecisionNote::AnisotropicFilter`'s reach), on a device without `samplerAnisotropy`, which
|
||
filters it isotropically: a cube, a 3D texture, a comparison, a sample whose residency the
|
||
function observes, or a texture of another class than its declared one, at an implicit level or
|
||
with explicit gradients, through a constexpr sampler of `maxAnisotropy` above 1 or a runtime
|
||
sampler slot whose word states anisotropy (under that word alone). An argument buffer's
|
||
anisotropic sampler, which the function cannot record, is refused there (`SamplerAnisotropy`).
|
||
- `metal2vulkan-degrade-device-point-size` (precision). `PrecisionNote::DevicePointSize`: A vertex
|
||
function writes `[[point_size]]`, or a mesh function that outputs points `set_point_size`, on a
|
||
device whose `pointSizeRange` ends below Metal's largest point, 511 pixels
|
||
(`Limits::max_point_size`: 255 on ANV, 256 on lavapipe). A line or triangle mesh's size draws
|
||
nothing, and its function records no note. The render pipeline state carries the note whatever
|
||
primitive a draw of it takes; a draw of lines or triangles of it is Metal's exactly.
|
||
- `metal2vulkan-degrade-clipped-coverage-split` (precision). `PrecisionNote::ClippedCoverageSplit`:
|
||
A fragment function compiled for a multisampled pipeline reads `[[sample_mask]]`, or reads a
|
||
centroid varying and runs once per pixel, on every device (no query tells Apple's rasterizer
|
||
apart): Apple's GPU draws a triangle
|
||
that extends past the viewport as pieces whose shared edges cross pixels, and such a pixel's
|
||
fragments can run once per piece, each reading that piece's coverage, where the device hands the
|
||
pixel's fragment its coverage by the whole primitive (lavapipe by its own pieces). A centroid
|
||
varying is read at the centroid of the whole coverage there.
|
||
- `metal2vulkan-degrade-looped-sample-outputs` (precision). `PrecisionNote::LoopedSampleOutputs`: A
|
||
fragment function that reads `[[sample_mask]]`, runs once per covered sample on Metal and writes
|
||
an output runs once per pixel in a multisampled pipeline, its body once per covered sample, on
|
||
every device. The samples Metal writes are written, but each with the last
|
||
pass's outputs, where Metal writes each sample's from that sample's run: equal where every pass
|
||
computes the same outputs. Vulkan hands an invocation under sample shading only its own sample's
|
||
bit of the coverage.
|
||
- `metal2vulkan-degrade-half-output-fast-math-blend` (precision).
|
||
`PrecisionNote::HalfOutputFastMathBlend`: A fragment function of a module compiled with fast math
|
||
(`air.compile.fast_math_enable`) returns a `half` color output to an attachment the pipeline has
|
||
that stores some channel (its write mask names one of its format's channels;
|
||
`StageIo::half_fast_colors`, which the pipeline's stage reads, or the interlocked storage form's
|
||
write), where `PrecisionNote::HalfOutputPrecision`'s conditions hold; or
|
||
computes the blend of a 16-bit float attachment it fetches in the interlocked storage
|
||
form (no Vulkan blend applies to a storage write) where Metal's compiler may fold the output's
|
||
own arithmetic into that blend or regroup a sum of it: within 2 `half` units in the last place of
|
||
Metal's at the operands' magnitude, 16 at the stored value's under cancellation. Where the output
|
||
is opaque to the blend and its form fits every measured state, the function's blend is Metal's
|
||
and records nothing; so does the blend of a fetched 8-bit unorm attachment it computes there
|
||
(every factor, operation, write mask and blend color it builds), which Metal reads the output
|
||
into clamped, folding none of it (the storage image's store of its result is the device's
|
||
conversion, `DeviceColorConversion`). Every device receives the output widened to `float`.
|
||
- `metal2vulkan-degrade-device-sampling` (precision). `PrecisionNote::DeviceSampling`: Native device
|
||
sampler, on every device (the module requires `VkProperty::DeviceSampling`), its level clamp the
|
||
descriptor's as given (a `lodMinClamp` past level 15, past every texture's last level, handed as
|
||
15, which reads the same level; Metal holds it on a `1/64` grid, and its nearest mip filter reads
|
||
the lower level at a clamp halfway between two, where the device may read the upper) and a
|
||
subnormal coordinate the device's to read (Metal reads a normalized one as a zero before
|
||
addressing, and a negative pixel one as texel -1). That includes a linear sample of a two-plane
|
||
YCbCr texture, whose texels hold Metal's reads exactly: the device's filter of them lands within
|
||
1.183 codes (1/255) of Metal's sum on the format's grid, so a draw binding such a texture makes no
|
||
pipeline of its own.
|
||
- `metal2vulkan-degrade-native-transcendentals` (precision). `PrecisionNote::NativeTranscendentals`:
|
||
Native device maths: the module computes a `float` or `half` math-library function by the device's
|
||
own `GLSL.std.450` instruction (`exp`, `log`, `pow`, `sinh`, `cosh` and `tanh` among them; a
|
||
composition of the device's `Exp2` and `Log2` only for `exp10` and `log10`, which no instruction
|
||
is); `fast::sqrt` the device's `Sqrt`, and a `half` `sqrt` its 16-bit `Sqrt` where the module
|
||
computes `half` in 16 bits under the 16-bit float controls; the fast `sin`, `cos`, `tan`, `sinh`,
|
||
`cosh`, `tanh` and `atanh` the device's instructions, not MSL's fast-math formulas (`fast::sin` of
|
||
`1e7` is the device's sine, where Metal answers `+0`; `fast::tanh(45)` is `1`, where Metal answers
|
||
NaN); `fabs`, `sign` and `fract` the device's `FAbs`, `FSign` and `Fract`, their special operands
|
||
the instruction's (Metal's `sign` of a NaN, a zero and an `f32` denormal is `+0`, its `fract` of
|
||
an infinity `+0`); a `half` function's signed zeros and NaN signs its `float` instruction's
|
||
(`half` `sin(-0)` is `-0`, where Metal answers `+0`), and on every device a `half` `fmod`'s zero
|
||
remainder its `float` remainder's (`half` `fmod(-2, 0.5)` is `-0`, where Metal answers `+0`);
|
||
`pow`, `powr` and `atan2` C99's special values by the device's comparisons around its instruction
|
||
(`pow(x, 1)` the device's `Pow`, where Metal answers `x`; an `f32` denormal operand as the device
|
||
reads it); and `round` C99's from the device's `Trunc` on every device (`round(-0.3)` is `-0`,
|
||
where Metal answers `+0`).
|
||
- `metal2vulkan-degrade-native-division` (precision). `PrecisionNote::NativeDivision`: Native device
|
||
maths: a precise `float` or `half` division, or Metal's fast division or reciprocal, is the
|
||
device's `OpFDiv`, on every device: within Vulkan's 2.5 ulp, where
|
||
Metal's precise division is correctly rounded. A `half` one is the `float` quotient rounded to
|
||
`half`, or, where the module computes `half` in 16 bits under the 16-bit float controls, the
|
||
device's 16-bit `OpFDiv`: within 2.5 `half` ulp, its divisor scaled by a power of two into
|
||
`[2^-14, 2^14]`.
|
||
- `metal2vulkan-degrade-native-fma` (precision). `PrecisionNote::NativeFma`: Native device maths: a
|
||
`float` fused multiply-add on a device without `OpFmaKHR`, or a `half` one where the device has
|
||
no 16-bit `OpFmaKHR` under the 16-bit float controls, is the device's `GLSL.std.450 Fma` (on the
|
||
`half` operands held in `float`, its result rounded to `half`), where Metal's rounds once. On a
|
||
device that computes `half` in 16 bits under the 16-bit float controls, a `half` one is the
|
||
device's 16-bit `Fma` on the `half` operands. Recorded bound: a `half` result
|
||
held in `float` within one `half` ulp of Metal's; a `float` one, and a 16-bit one, within half an
|
||
ulp of the product plus half an ulp of the result, in its own format.
|
||
- `metal2vulkan-degrade-device-texel-conversion` (precision).
|
||
`PrecisionNote::DeviceTexelConversion`: Native device sampling (a texel conversion): the module
|
||
hands a read, sample or gather with a `float` result the device's own conversion of the texel's
|
||
codes, on every device. Recorded bound, of a texel read from one texel of one level: an 8-, 10- or
|
||
16-bit normalized code within one `f32` ulp of Metal's `c / (2^b - 1)` where measured (lavapipe
|
||
multiplies by the reciprocal: some 8-bit codes one ulp high; some 10-bit `BGR10A2Unorm`, a 16-bit
|
||
and 512 `Depth16Unorm` codes one ulp low), and a 10-bit code within 0.015 of a code where the
|
||
device widens it by replicating its bits to 16, `(c << 6 | c >> 4) / 65535` (NVIDIA's RTX 5080
|
||
Laptop GPU); a 4-, 5- or 6-bit channel of a packed format within one `f32` ulp where the device
|
||
converts it by the reciprocal (lavapipe: some `ABGR4Unorm` codes one ulp high), and within 0.18 of
|
||
a code where it widens a 5- or 6-bit channel by replicating its bits (ANV, NVIDIA; a 4-bit
|
||
channel's replication is exact); a half-float or small-float texel's NaN in the device's encoding,
|
||
where Metal answers `0x7fc00000`; a 32-bit float texel's NaN in a gather in the device's encoding,
|
||
and its denormal in a gather a zero of its sign, where Metal hands back the texel's bits (NVIDIA's
|
||
615.71.09 on the RTX 5080 Laptop GPU gathers every NaN as `0x7fffffff` and every denormal as a
|
||
zero); a 32-bit float texel's NaN in a nearest sample in the device's encoding, and its denormal
|
||
there a zero of its sign on a device that flushes it there (ANV's Mesa 26.2.6 on the Iris Xe
|
||
samples every NaN as `0xffc00000` and every denormal as a zero of its sign, and reads and gathers
|
||
both as stored; RADV's Mesa 26.2.6 on the RX 7900 XTX and the Radeon 780M samples a denormal as a
|
||
zero and gathers it as stored); every other class Metal's value.
|
||
- `metal2vulkan-degrade-device-vertex-conversion` (precision).
|
||
`PrecisionNote::DeviceVertexConversion`: Native vertex input (a conversion): a render pipeline's
|
||
vertex descriptor gives a normalized format (`UCharNormalized` through `Short4Normalized` of
|
||
either sign, `UChar4Normalized_BGRA`, `Int1010102Normalized`, `UInt1010102Normalized`) to an
|
||
attribute that fixed-function vertex input fetches, so the attribute is the device's `UNORM` or
|
||
`SNORM` conversion of its codes, where Metal's fetch answers the code over its maximum correctly
|
||
rounded (an `Int1010102Normalized` 10-bit field times `float(1/511)`): within one `f32` ulp where
|
||
measured (lavapipe reads some 8-bit unsigned codes one ulp high), but lavapipe's per-instance
|
||
fetch, which reads the most negative code of an 8- or 16-bit signed format unclamped (-128/127,
|
||
-32768/32767) where Metal reads -1. Recorded on the pipeline's vertex stage on every device; one
|
||
module serves every format the device fetches. A normalized attribute the shader pulls from its
|
||
buffer instead (an offset, stride or step fixed-function input cannot fetch, a kernel's
|
||
`[[stage_in]]` attribute) is the module's product of the code and its maximum's reciprocal,
|
||
rounded once, the signed one clamped to -1: within one `f32` ulp of Metal's quotient (some 8-bit
|
||
unsigned codes one ulp high, some signed, 10-bit and 16-bit codes one ulp toward zero; 0, the
|
||
maximum and -1 exact). The function records it on every device, read as `float` for every
|
||
normalized format but `Int1010102Normalized` (Metal's own product), and read as `half` for a
|
||
16-bit format (one `half` ulp at four codes).
|
||
- `metal2vulkan-degrade-native-half-conversion` (precision). `PrecisionNote::NativeHalfConversion`:
|
||
Native half rounding (a conversion): the module rounds a `half` result it computes in `float` (a
|
||
`half` operation's, a function's, a conversion from `float`'s, a texel's) by the device's
|
||
conversion of the `float` to 16 bits and its widening, on every device with `shaderFloat16`.
|
||
Without the float controls that make that conversion Metal's `half(float)` (`RoundingModeRTE`,
|
||
`DenormPreserve` and `SignedZeroInfNanPreserve` for 16 bits) the conversion is the device's own.
|
||
Under them it is Metal's by the Vulkan spec, but a driver's compiler may fold the conversion's
|
||
round trip into the value it started from (NVIDIA's does) or fuse the `float` addition,
|
||
subtraction, multiply or fused multiply-add computing the value into the narrowing and round its
|
||
exact result once (RADV's does, one `half` ulp from Metal's where the `float` result lies on a
|
||
`half` midpoint), so a pipeline that rounds through the pair records the note there too, a
|
||
static note of the rounding. Under those controls a `half` `+`, `-`, `*`, `/` and `sqrt` are the
|
||
device's 16-bit instructions, whose results take no such rounding. Also recorded where a `half`
|
||
read from its bits is the device's widening, or a `half` encoded to its bits (where they are not
|
||
the bits it was read from) is the device's narrowing, and that conversion is not under those
|
||
controls: the device's conversion without them, and `GLSL.std.450 UnpackHalf2x16` and
|
||
`PackHalf2x16` on every device without `shaderFloat16`. A half denormal may read or encode as the
|
||
zero of its sign.
|
||
- `metal2vulkan-degrade-native-half-quantize` (precision). `PrecisionNote::NativeHalfQuantize`:
|
||
Native half rounding (a quantize): the module rounds a `half` result it computes in `float` (a
|
||
`half` operation's, a function's, a conversion from `float`'s, a texel's) by the device's
|
||
`OpQuantizeToF16` alone, on every device without `shaderFloat16`, its quantize measured or not: a
|
||
value below the smallest normal `half` may round to a zero where Metal answers the half denormal
|
||
(or `2^-14`, from just below it). An integer's `half` rounds exactly instead.
|
||
- `metal2vulkan-degrade-native-nan-encoding` (precision). `PrecisionNote::NativeNanEncoding`: Native
|
||
device maths: a `float` or `half` operation's NaN result is the device's, its sign and payload
|
||
unselected, where Metal answers a NaN, on every device: a `float`
|
||
`fneg` and `fabs`, which keep a NaN operand's payload on Metal, a `half` `fract`, which returns
|
||
it, and a precise `float` `pow` of a NaN base to the exponent one, which returns the base, among
|
||
them. A `float` `ldexp`, which the module computes on the bits, returns a NaN operand itself as
|
||
Metal does, on every device, and records nothing. Also
|
||
recorded wherever a `half` is read from its bits, on every device, since the read is the
|
||
device's widening (its conversion with `shaderFloat16`, `UnpackHalf2x16` without): a NaN's bits
|
||
the device's, where Metal widens every `half` NaN to `0x7fc00000` and moves one it does not
|
||
widen with its bits. And wherever a `half` is encoded to its bits (a store, a reinterpretation, a
|
||
texel's bits) where they are not the bits it was read from (a `half` moved unchanged from its
|
||
read keeps them, through a struct's member too), on every device, since the encode is
|
||
the device's narrowing (its conversion with `shaderFloat16`, `PackHalf2x16` without): a NaN's
|
||
bits the device's, where Metal keeps a `half` NaN's sign and payload.
|
||
- `metal2vulkan-degrade-native-min-max` (precision). `PrecisionNote::NativeMinMax`: Native device
|
||
maths: Metal's `fmin`, `fmax`, `min3`, `max3`, `clamp` or `saturate` (its `clamp(x, +0, 1)`) on
|
||
`float` or `half` is the device's `GLSL.std.450` `NMin`, `NMax` or `NClamp`, and `median3`
|
||
`min(max(c, min(a, b)), max(a, b))` of the device's `NMin` and `NMax`, on every device, whose pick
|
||
of two NaNs and of an `f32` denormal is the device's. The result is one of the operands, Metal's
|
||
but at three ties (of two NaNs a NaN of the device's bits, where Metal answers the second
|
||
operand's; of `+0` and `-0` either zero; an `f32` denormal beside a zero or a denormal compared by
|
||
its value, where Metal compares it as a zero and answers the second) and one undefined answer:
|
||
`fast::median3` of two NaN first operands and a number answers the number, as precise `median3`
|
||
does on Metal, where Metal's fast form answers the second NaN. MSL leaves a NaN operand under fast
|
||
math undefined (section 8.1).
|
||
- `metal2vulkan-degrade-native-dot` (precision). `PrecisionNote::NativeDot`: Native device maths:
|
||
Metal's `dot` on `float` and `half` is the device's `OpDot` (a `half` one summed in `float` and
|
||
rounded once to `half`, or the device's 16-bit `OpDot` where the module computes `half` in 16
|
||
bits under the 16-bit float controls), not Metal's fused chain of its products, on every device.
|
||
- `metal2vulkan-degrade-native-mix` (precision). `PrecisionNote::NativeMix`: Native device maths:
|
||
Metal's `mix(x, y, t)` on `float` and `half` is the device's `FMix` (a `half` one computed in
|
||
`float` and rounded once to `half`, or the device's 16-bit `FMix` where the module computes
|
||
`half` in 16 bits under the 16-bit float controls), not Metal's `fma(t, y, fma(-t, x, x))`, on
|
||
every device.
|
||
- `metal2vulkan-degrade-native-logic-operation` (precision).
|
||
`PrecisionNote::NativeLogicOperation`: A render pipeline's private logic operation (other than
|
||
copy and no-op) is drawn by the device's `logicOp` into an `RGB10A2Unorm`, `BGR10A2Unorm` or
|
||
signed normalized attachment the fragment function writes, with a channel its write mask keeps.
|
||
Vulkan's operation takes the fragment's value converted to the channel's own code and the stored
|
||
code as it is, and stores its result as it is; Metal widens a 10-bit (or 2-bit) code and the
|
||
fragment's to 16-bit codes, operates, and narrows the result to the channel's code, rounded to
|
||
nearest, and reads a signed channel's lowest code (`0x80`, or `0x8000`) as the code above it and
|
||
stores a result of the lowest code as the code above (probe `logic_operation` on an Apple M3 Max,
|
||
macOS 27.0.1). So a channel may store another code than Metal's (lavapipe's xor into
|
||
`RGB10A2Unorm` stored green 938 where Metal's rule gives 937, and its xor of `0xe0` over an
|
||
`RGBA8Snorm` channel's `0x80` stored `0x60` where Metal's gives `0x61`), by a distance the note
|
||
does not bound: the result follows the codes' bits. The device's `logicOp` is its native form
|
||
there, and no other form is built for it; a device without `logicOp` computes Metal's rule in
|
||
the fragment function and records none. A signed channel the draw does not write (outside the
|
||
write mask, or every channel under no-op) keeps its lowest code where any draw's unwritten
|
||
texel does (`snorm-minimum-kept`).
|
||
- `metal2vulkan-degrade-native-float8` (precision). `PrecisionNote::NativeFloat8`: A tensor's
|
||
8-bit float element (`MTLTensorDataTypeFloat8E4M3` or `Float8E5M2`, which a `matmul2d` product
|
||
reads) widens through the device's own 8-bit float on a device with `shaderFloat8`
|
||
(`SPV_EXT_float8`'s `Float8E4M3EXT` or `Float8E5M2EXT`, OCP's encodings), the only form there:
|
||
an e4m3 code `0x7F` or `0xFF` reads as a NaN, where Metal reads 480 and -480 (measured on the M3
|
||
Max), and an e5m2 NaN's bits are the device's, where Metal reads `0x7FC00000`. Every other code
|
||
reads Metal's value. A device without `shaderFloat8` decodes the fields with integers, as Metal
|
||
reads them, and records none. An `air.convert` from `f8e4m3fn` or `f8e5m2` and a
|
||
simdgroup-matrix cell of those formats widen through the same type there and record none: their
|
||
formats are OCP's own, and Metal on Apple M3 does not compile them.
|
||
- `metal2vulkan-degrade-device-depth-bias` (precision). `PrecisionNote::DeviceDepthBias`: A draw
|
||
of triangles under a depth bias (`setDepthBias:slopeScale:clamp:` with a bias or a slope scale
|
||
other than 0) on `metal2vulkan-runtime`, whose render encoder carries it as a static note
|
||
(`RenderCommandEncoder::DEPTH_BIAS_NOTE`): the biased depth is the device's. Vulkan biases by
|
||
`m × slopeScale + r × depthBias`, where the maximum depth slope `m` is the device's (exact, or
|
||
the larger of the two derivatives' magnitudes) and the unit `r` is `2^(e - n)` of the primitive's
|
||
largest depth exponent for a float depth attachment and the device's for a fixed-point one (at
|
||
most `2 × 2^-n`). Apple's documentation states neither the slope measure nor the unit. A draw of
|
||
lines is not biased, as Metal biases triangles alone.
|
||
- `metal2vulkan-degrade-swizzled-opaque-black-border` (precision).
|
||
`PrecisionNote::SwizzledOpaqueBlackBorder`: A sampler that reads its
|
||
border (`ClampToZero` or `ClampToBorderColor` on an axis, both of which read the sampler's
|
||
colour) with an opaque-black border colour. A sampler state carries it as a static note
|
||
(`SamplerState::precision_note`), decided by its descriptor alone on every device; a compute or
|
||
render pipeline whose function samples through such a constexpr sampler carries it among its
|
||
notes (`precision_notes`), decided by the sampler's own constants. Sampled
|
||
through a view whose Vulkan component mapping is not the identity (the texture's swizzle, or the
|
||
mapping its pixel format takes), its border texel is the device's answer on a device without
|
||
`borderColorSwizzleFromImage` (`VK_EXT_border_color_swizzle`): Vulkan leaves it undefined unless
|
||
the sampler names the view's mapping, which a sampler that meets every view cannot, and the
|
||
NVIDIA driver reads `(0, 0, 0, 1)`, where Metal reads the border colour in the format's channels
|
||
through the swizzle. A device with the feature reads Metal's texel.
|
||
- `metal2vulkan-degrade-device-line-rasterization` (precision).
|
||
`PrecisionNote::DeviceLineRasterization`: A render pipeline state that enables alpha to coverage
|
||
or alpha to one, which carries it among its notes (`precision_notes`), decided by its descriptor
|
||
alone. Its lines are drawn by the device's default rule, not Bresenham's, which Vulkan takes with
|
||
neither (`VUID-VkGraphicsPipelineCreateInfo-lineRasterizationMode-02766`). Metal covers the
|
||
pixels whose diamond a line exits; a device whose `strictLines` is set (lavapipe, NVIDIA's
|
||
driver) covers the rectangle of the line's width instead, so the pixels at a line's two ends,
|
||
and on either side of a pixel boundary the line runs along, may differ from Metal's. Every other
|
||
state draws its lines by Bresenham's rule (`bresenhamLines`, the runtime's floor).
|
||
- `metal2vulkan-degrade-device-pack-conversion` (precision). `PrecisionNote::DevicePackConversion`:
|
||
Native device maths (a conversion): `air.pack` or `air.unpack` of a normalized format is the
|
||
device's: `GLSL.std.450`'s `Pack`/`Unpack` `Unorm4x8`, `Snorm4x8`, `Unorm2x16` and `Snorm2x16`,
|
||
their definition by the device's `NClamp`, multiply, `RoundEven` and reciprocal for `rgb10a2` and
|
||
`rgb565`, and the MSL transfer functions by the device's `Pow` for `unorm4x8_srgb`'s colour. Metal
|
||
rounds the exact product and divides correctly rounded (`unorm565` in `float`: the product
|
||
rounded to `float` first, and the code times the `float` nearest the reciprocal, as the
|
||
definition does), answers code 0 for a NaN channel, and
|
||
takes an sRGB colour through its twelve-bit table; a packed code is within one of Metal's, an
|
||
unpacked value within 2.5 ulp, a decoded sRGB colour within half a twelve-bit step (`1 / 8190`)
|
||
of Metal's step beyond its `Pow`'s bound. Also a write of an 8-bit sRGB storage texture held
|
||
through its UNORM alias (at a slot, or held by an argument buffer), which encodes by
|
||
`unorm4x8_srgb`'s pack. Recorded on every device. The same conversions
|
||
serve an implicit imageblock access of a normalized attachment (a `half` load rounding the
|
||
unpacked `float` once more), a fragment function's read and write of an 8-bit sRGB attachment
|
||
in the interlocked storage form or through its word view, and a snapshot read's decode of a
|
||
normalized code (`c` times the `f32` nearest `1 / D`), noted alike. So does every conversion of
|
||
an extended-range, padded 10-bit or bfloat texel (`BGR10_XR`, `BGRA10_XR`, `RGB10A8_2P_XR10`,
|
||
`R10Unorm_X6`, `RG10Unorm_X12`, their sRGB twins, `RGB10A2Unorm_sRGB`, `R16Bfloat`, `RG16Bfloat`,
|
||
`RGBA16Bfloat`), which no Vulkan format holds: a read is `float(c - 384)` (a padded code `c`)
|
||
times the `f32` nearest `1 / 510` (`1 / 1023`), a write `RoundEven` of the clamped value's `f32`
|
||
product plus 384, an sRGB twin's colour the MSL transfer by the device's `Pow` on the magnitude,
|
||
a `half` result the device's narrowing, a bfloat code's NaN as it is (a write's kept a NaN by
|
||
its quiet bit). Metal divides correctly rounded, rounds the exact product, rounds a `half` with
|
||
ties away from zero, converts the sRGB twins through its own tables, makes a bfloat NaN
|
||
`0x7fc00000` or `0x7fc0`, and rounds a bfloat sample's `half` toward zero: a linear value within
|
||
one `f32` ulp, a code within one (`R10Unorm_X6_sRGB`'s and `RGB10A2Unorm_sRGB`'s colour within
|
||
two below `0.0031308`, where Metal rounds the value to `2^-13` before its table, a step there
|
||
spanning 1.6 codes), an sRGB twin's decoded value within half a `2^-13` step of Metal's table
|
||
(`R10Unorm_X6_sRGB`'s and `RGB10A2Unorm_sRGB`'s colour 1.64 steps) beyond the `Pow`'s bound, a
|
||
`half` result within one `half` ulp. A texture a heap holds by its raw texels alone
|
||
(`BGR10_XR`, `BGRA10_XR`, `R10Unorm_X6`, `RG10Unorm_X12` and the bfloat formats:
|
||
`bind::heap_raw_form`), whose `float` read, sample or gather through an argument buffer the
|
||
function decodes the same way: such a stage records the note as a specialized note, holding
|
||
where a texture its handles can name is one (`SpecKey::HeapRawTextures`, which the binding plan
|
||
gives from the textures the command makes resident or binds), so a reading of the module alone
|
||
holds it, and so does a command that can name one.
|
||
- `metal2vulkan-degrade-device-fragment-runs` (precision). `PrecisionNote::DeviceFragmentRuns`: A
|
||
fragment function that writes memory (`StageInterface::writes_memory`) in a render pipeline of
|
||
more than one sample, which the pipeline's compile records on its fragment stage, decided by the
|
||
sample count and the function's writes alone. It runs as many times per pixel as the device runs
|
||
it, each run reading the pixel's whole coverage as `[[sample_mask]]`, so its writes repeat as
|
||
often as the device's runs, and whatever it derives from its own writes across runs follows the
|
||
device's count too, its stored colours and `[[sample_mask]]` output included, not only memory.
|
||
Apple's GPUs were measured to run it once per covered sample;
|
||
documented Metal names no count (MSL section 5.5 makes a function run per sample only for a
|
||
sample-qualified input, `[[sample_id]]` or `[[color(m)]]`). Vulkan runs a fragment shader
|
||
without sample shading at least once per pixel and more where the device chooses (vkspec 35.5).
|
||
|
||
## 10. Accepted differences
|
||
|
||
Where the runtime answers otherwise than Metal on purpose, the difference is listed here. Most
|
||
carry no degrade notice, since nothing is slower or approximate for them; an entry whose answer is
|
||
the device's own names the static note its stage records. Most are answers Metal leaves undefined
|
||
or ends the process for, and answers the runtime adds beyond Metal's API. The rustdoc of each item
|
||
named states the rule in full.
|
||
|
||
**Draws**
|
||
|
||
- **A point near the viewport's edge or the clip volume is clipped or culled by the device's own
|
||
rule**: its `pointClippingBehavior` and the driver's handling of the viewport. Metal states no
|
||
clipping rule for a point (`MTLPrimitiveType.point`; the MSL specification's `[[point_size]]`).
|
||
Measured, Metal on an Apple M3 Max keeps a point whose centre lies within 1.5 w of the
|
||
viewport's centre and covers its square whole, past the viewport's edge, where lavapipe covers
|
||
no pixel whose centre lies on or past that edge and culls no point at the clip volume (the
|
||
conformance case `point-viewport-coverage`).
|
||
- **A line's tie-break is the device's own.** Lines draw by Bresenham's rule, which is Metal's
|
||
(`bresenhamLines`, the runtime's floor): the pixels whose diamond the line exits. Vulkan's
|
||
diamond is open (`|p - x| + |q - y| < 0.5`), so a line that only touches a diamond's vertex
|
||
exits neither diamond the vertex joins, and no Vulkan control decides which pixel a conforming
|
||
driver covers there. Measured, where a line of the conformance case
|
||
`primitive-type-restart-indices` runs through the boundary point (3.5, 1.0), Metal on an Apple M3
|
||
Max covers the lower pixel and NVIDIA's driver on an RTX 5080 the upper.
|
||
- **A memory-writing fragment function runs as many times as the device runs it.** Metal's GPUs
|
||
were measured to run a memory-writing fragment once per covered sample; documented Metal names
|
||
no count; here the count is the device's, for its memory writes and for anything it derives from
|
||
them across runs, stored colours and sample mask included. The pipeline's fragment stage records
|
||
`PrecisionNote::DeviceFragmentRuns` (section 9). The conformance case `a2c-off-4-alpha-50` records
|
||
four runs of a fully covered pixel at four samples on an Apple M3 Max, and compares the runs word
|
||
as nonzero alone.
|
||
|
||
**Resource IDs and identifiers**
|
||
|
||
- **A nil, stale or forged texture or sampler ID reads a defined element.** A texture ID no live
|
||
texture holds reads a null descriptor: zeros, 0 to its size and level queries, and its writes
|
||
dropped. A sampler ID no live sampler state holds samples through its array's default sampler,
|
||
nearest and clamped to the edge, and a comparison through it answers 0. The Metal Shading
|
||
Language leaves every member function of a null texture undefined (section 6.13.18), and
|
||
Metal's read through a stale or forged ID is undefined.
|
||
- **Two live textures with one ID: a function reads the holder**, the texture placed there last.
|
||
|
||
**Render passes**
|
||
|
||
- **The runtime's `DontCare` store writes what the pass rendered** (`metal2vulkan-runtime`'s
|
||
render encoder stores a `DontCare` attachment as it stores a `Store` one, its README's render
|
||
passes). Apple's page for
|
||
`MTLStoreAction.dontCare`: "The GPU has permission to discard the rendered contents of the
|
||
attachment at the end of the render pass, replacing them with arbitrary data." and "Some GPUs
|
||
may still store the contents back to the texture, but you can't rely on that behavior. You need
|
||
to assume that GPU discarded the texture's contents." `MTLRenderPass.h` states only that it is
|
||
the default store action. An Apple M3 Max on macOS 27.0.1 leaves the texels of a texture over a
|
||
buffer as they were before a clearing pass; the runtime stores the clear there. The bytes
|
||
of the buffer outside the texels keep what they held on both.
|
||
|
||
- **A barrier over the render targets orders what it names, as a Mac2 GPU's does.** The Metal
|
||
Feature Set Tables give `MTLBarrierScopeRenderTargets` to Mac2 GPUs, not to Apple GPUs, and an
|
||
M3 Max (macOS 27.0.1) orders nothing with it: a draw after one that samples its own pass's color
|
||
attachment's texture reads the texels from before the pass, whatever the pass's load action or
|
||
draws wrote before the barrier. The runtime orders the draws before such a barrier ahead of the
|
||
next draw that reads an attachment of the pass, which reads what they rendered (the runtime's
|
||
README, Render encoders). The conformance cases `pass-start-barrier-*` keep the M3 Max's texels
|
||
and take the Mac2 answer as the one other.
|
||
- **A draw that reads its own pass's color attachment with no barrier reads, at the pass's first
|
||
such draw, what the load and the draws before it rendered**: the runtime orders them ahead of
|
||
that draw (the runtime's README, Render encoders). Metal defines no answer here, and the Macs
|
||
disagree: an M3 Max (macOS 27.0.1) read the texels from before the pass at every pixel, in a
|
||
pass that clears too; an AMD Radeon Pro 5300M and an Intel UHD Graphics 630 (macOS 26.7.1) read
|
||
the earlier draw's texels at 98.6% and 99.99% of the pixels, and at all of them after a barrier
|
||
over the render targets; the rest read the load's or the clear's texels.
|
||
|
||
**Heaps and memory**
|
||
|
||
- **The runtime answers as a Mac that keeps no texture compressed**, as a Mac with an Intel or
|
||
AMD GPU keeps none, where an Apple silicon Mac's GPU keeps textures losslessly compressed: a new
|
||
texture reads zero until written (the runtime's README, Textures), where an Apple silicon Mac's
|
||
new `Private` texture reads zero or the compressed texture's fixed pattern, and a GPU blit
|
||
through any texture keeps its bytes, where Apple's blit through a compressed texture writes some
|
||
formats' canonical encoding. Metal leaves a new texture's contents undefined. The conformance
|
||
suite's venue on the runtime presents no compression, so the answers such a Mac gives hold
|
||
there.
|
||
- **The runtime's heap texture size and alignment are the device's own** (`metal2vulkan-runtime`'s
|
||
`Device::heap_texture_size_and_align`, its README's textures): the device's own image
|
||
requirements, not Metal's, the size rounded up to and the alignment raised to the device's
|
||
`bufferImageGranularity`, so a buffer placed by the same answers never shares a granularity page
|
||
with the texture. The answers differ by device, so the consumer places by this device's. The
|
||
conformance suite does not compare the two numbers with Metal's; it holds each answer to three
|
||
steps: (1) the alignment is a power of two; (2) a Private descriptor's texture is made at offset
|
||
`align` in a heap of `align + size` bytes; (3) there, level 0's first slice written from a buffer
|
||
reads back unchanged. A Shared, Managed or Memoryless descriptor is held to step 1 alone, as the
|
||
runtime places heap textures in Private blocks only. A descriptor whose bytes the runtime does not
|
||
move (depth and stencil both, multisample, `Depth32Float`) is held to steps 1 and 2.
|
||
- **Apple's private texture usage bits are kept** in `TextureUsage` (`NO_CONCURRENT_ACCESS`,
|
||
`VIDEO`, `BLOCK_WRITES_ONLY`).
|
||
- **A heap's buffer and texture over the same bytes alias them, in the device's layout**, not
|
||
Apple's tiled or compressed one.
|
||
- **The runtime's Managed copies read the consumer's bytes when the GPU reaches them**
|
||
(`metal2vulkan-runtime`'s `Buffer::did_modify_range` and `Texture::upload_region`). A Managed
|
||
buffer with a device copy (on a device that is neither integrated nor a CPU) takes its named
|
||
ranges into that copy at the next commit of a command buffer with commands, on any queue, by
|
||
device copies ahead of that command buffer's commands; a Managed texture's uploaded region is one
|
||
device copy from its named bytes into its image, ahead of every command buffer committed after the
|
||
call on every queue. The copy reads the bytes as they are when the GPU runs it, so a write the
|
||
consumer makes after the commit and before then may be seen. Metal leaves it undefined: an Apple
|
||
M3 and an Intel UHD 630 take the bytes when the GPU reads them, an AMD Radeon Pro 5300M at the
|
||
commit.
|
||
|
||
**Function constants**
|
||
|
||
- **A function constant the request leaves undefined reads as zero.** A value computed from it
|
||
alone is its type's zero (an `and`, `or` or `mul` with a constant that decides it is that
|
||
constant), and a branch or `select` on one takes its false way; a value it meets with one the
|
||
run computes is computed with that zero. As Metal's, `is_function_constant_defined` is false for
|
||
it and an argument gated on it, negated or not, is absent. Metal makes the same pipeline of a
|
||
specialized function (made with constant values, none among them too, or by
|
||
`newFunctionWithDescriptor:`), and its reads of the constant vary from read to read: on an M3
|
||
Max under macOS 27.0.1 it read `u + 3` as 0, `u * 7 + 1` as 1, `u == 0 ? 111 : 222` as 222 and
|
||
`b ? 5 : 6` as 5. The compile is the same for a function made by `newFunctionWithName:`, which
|
||
Metal makes no pipeline state of where its function declares a constant: its creation ends the
|
||
process at every stage ("function ko cannot be used to build a pipeline state. Use
|
||
newFunctionWithName:constantValues:... to get the specialized function"), whether the constant
|
||
is required, read only where `is_function_constant_defined` says, or only gates an argument.
|
||
`FunctionDescriptor::needs_specialization` answers it (`FunctionDescriptor::specialized` and the
|
||
function's `function_constants_dictionary`); the runtime does not ask it and makes the state,
|
||
each constant without a value reading as above. A linked function keeps its own rule (section
|
||
3, "Linked functions: names and constants").
|
||
- **A fast-math float operation of a NaN function constant and a value the run computes stores
|
||
that NaN.** An `fadd`, `fsub`, `fmul` or `fdiv` that carries `nnan` (Metal's default fast math)
|
||
and whose operand is a function constant bound to a NaN is `poison` in the AIR once the
|
||
constants are bound, so any value is a correct answer for it. The translation answers the NaN
|
||
operand, its bits unchanged, as it does for the same operation without fast math, where that NaN
|
||
is Metal's answer. Metal answers a NaN for a fragment function's output of it on an Apple M3 Max,
|
||
an AMD Radeon Pro 5300M and an Intel UHD Graphics 630 under macOS 27.0.1. On the M3 Max a
|
||
kernel's store of it writes nothing, `float` and `half` alike, and the memory keeps what it
|
||
held, while the same kernel's store of the run value is written (the fixture
|
||
`sfold_fast_poison`, case `2d9a7e3091e1f9d9`, job m3-max-12877; the case stays out of the
|
||
corpus, whose comparisons are exact).
|
||
- **A linked function's constant without a value reads as zero too**, where the function was made
|
||
specialized (section 3, "Linked functions: names and constants"). Metal makes that pipeline,
|
||
and its read is undefined: a kernel storing `x * scale + 1` of such a function stored nothing on
|
||
an M3 Max under macOS 27.0.1.
|
||
- **A program-scope constant that the function constants make a division by zero reads as an
|
||
undefined constant reads.** A static initializer's scalar integer division or remainder by
|
||
zero (`udiv`, `sdiv`, `urem`, `srem`) has a result the AIR leaves undefined, and so does
|
||
everything computed from it: the translation reads it as zero, and a branch or `select` on it
|
||
takes its false way, as for a constant the request leaves undefined. Metal makes that pipeline
|
||
on an M3 Max under macOS 27.0.1 and stores nothing of the quotient, nor of a value chosen by it;
|
||
where a loop's trip count is computed from it, the kernel stores nothing at all (the probe
|
||
`static_init_division.m`). A vector division with a zero lane is still
|
||
`Unsupported{StaticInitializer}`.
|
||
|
||
**Linked functions**
|
||
|
||
- **A reference of another type reads zero or its arguments' bits where Metal's read is
|
||
undefined** (section 3, "Linked functions: types"). A parameter the reference passes nothing
|
||
for reads zero, as Metal's read zero on an M3 Max under macOS 27.0.1 and on a Radeon Pro 5300M
|
||
and a UHD Graphics 630 under macOS 26.7.1. An argument of another width passes its low bits to a
|
||
narrower parameter, as the 5300M did for a `float` passed to a `half`, where the M3 Max and the
|
||
UHD 630 left the parameter undefined (a kernel storing a value computed from it stored nothing);
|
||
it passes its bits with zeros above to a wider parameter, and a result of another width or one a
|
||
definition returning nothing gives passes the same way or reads zero, where the M3 Max stored
|
||
nothing.
|
||
- **A reference passing a pointer for a value or a value for a pointer, a pointer of another
|
||
address space, or an aggregate of another type is refused** (`Unsupported`,
|
||
`Feature::Linking`), where Metal makes the pipeline: on the M3 Max each such read was undefined
|
||
(the kernel stored nothing); on the 5300M a value for a pointer read the pointer's low 32 bits
|
||
and a `constant` pointer for a `device` one read through it.
|
||
|
||
**Conversions**
|
||
|
||
- **`air.convert` with a rounding (`.rte`, `.rtz`, `.rtp`, `.rtn`) and `.sat` after its types is
|
||
honored in a compute pipeline of OpenCL mode and is the conversion its types name elsewhere.**
|
||
Apple's OpenCL compiler spells these on every `convert_<type>[_sat][_<mode>]`.
|
||
|
||
In OpenCL mode (`ComputePipelineDescriptor::open_cl_mode_enabled`) Metal honors them. Measured
|
||
over OpenCL C of our own, one work-item per operand (probe jobs m3-max-15651 on a Radeon Pro
|
||
5300M and m3-max-15652 on a UHD Graphics 630, both macOS 26.7.1):
|
||
|
||
| Form, OpenCL mode | Radeon Pro 5300M | UHD Graphics 630 | Runtime |
|
||
|---|---|---|---|
|
||
| integer of 8 to 64 bits to `float`, `.rtz`, `.rtp`, `.rtn` | rounded as named | rounded as named | rounded as named |
|
||
| `float` to an integer of 8 to 64 bits, `.rte`, `.rtp`, `.rtn` | rounded as named, then clamped where `.sat` | as the 5300M, but a `uint` of 2^31 or more is `0x7fffffff` and a `uchar` with `.sat` above 127 is 127 | rounded as named, then saturated (the 5300M's answer) |
|
||
| integer to integer, `.sat` | clamped to the destination's range | clamped to the destination's range | clamped to the destination's range |
|
||
| a `half` side, any suffix | no pipeline | no pipeline | the conversion its types name |
|
||
|
||
An integer to `float` by `.rte`, a `float` to an integer by `.rtz`, and an integer to an integer
|
||
with a rounding and no `.sat` are the conversion their types name on both, as on the runtime.
|
||
Neither creates a pipeline of OpenCL mode for a conversion with a `half` side (its library
|
||
compiles), so no Metal answer exists for one, and the runtime makes the pipeline. The device has
|
||
no directed conversion (Vulkan's integer-to-float conversion rounds in a direction of its own,
|
||
its float-to-integer one truncates), so the runtime builds each exactly: a `float` is rounded to
|
||
an integral value (`RoundEven`, `Ceil`, `Floor`) before the conversion, and the device's
|
||
conversion of an integer to `float` moves one unit in the last place where it lies on the wrong
|
||
side of the integer.
|
||
|
||
Outside OpenCL mode Metal's compilers do not answer them alike. Measured over the owned fixture
|
||
`kernel_convert_rounding_modes`'s edge values, one kernel per form (probe jobs m3-max-14380 on a
|
||
Radeon Pro 5300M and m3-max-14381 on a UHD Graphics 630, both macOS 26.7.1; m3-max-14382 on the
|
||
M3 Max, macOS 27.0.1), the runtime differs from a Mac in three places:
|
||
|
||
| Form | M3 Max | Radeon Pro 5300M | UHD Graphics 630 | Runtime |
|
||
|---|---|---|---|---|
|
||
| float to integer, `.rte` | rounds to nearest even | truncates | no pipeline | truncates |
|
||
| float to `half`, `.rtz`, `.rtp`, `.rtn` | `.rtz` as named; no pipeline for the others | nearest even | as named | nearest even |
|
||
| float to integer, `.sat` | an unsigned destination keeps a negative's wrapped bits; a `long` of an infinity is 0, past its range wraps | an 8-bit destination is not clamped; a `long` is clamped to `int`'s range | no pipeline | clamps, as its conversion without the suffix |
|
||
|
||
Everywhere else every Mac that creates the pipeline answers the conversion without the suffix,
|
||
which is no difference: an integer to `float` or `half` by any rounding is rounded to nearest
|
||
even (the M3 Max creates no pipeline for a directed one), an integer to an integer with `.sat`
|
||
wraps (the M3 Max creates none), and a float to an integer by `.rtp` or `.rtn` truncates on the
|
||
5300M (the others create none). The M3 Max's compiler service exits on the forms it does not
|
||
compile, where the runtime compiles them all.
|
||
- **`vstore_half` and `vstorea_half` with a rounding (`_rtz`, `_rtp`, `_rtn`) store each lane as
|
||
the device converts a `float` to `half`, to nearest even.** Measured over 32 values among them
|
||
ties, overflows past the largest `half`, subnormals and zeros, with the `vstore_half4` forms alike
|
||
(probe jobs m3-max-15563 on the M3 Max, macOS 27.0.1; m3-max-15564 on a Radeon Pro 5300M and
|
||
m3-max-15584 on a UHD Graphics 630, macOS 26.7.1):
|
||
|
||
| Form | M3 Max | Radeon Pro 5300M | UHD Graphics 630 | Runtime |
|
||
|---|---|---|---|---|
|
||
| `vstore_half_rtz`, `_rtp`, `_rtn` (`70000.0` by `_rtz`) | as named (`0x7bff`) | nearest even (`0x7c00`) | nearest even (`0x7c00`) | nearest even (`0x7c00`) |
|
||
- **A float past an integer's range converts to the integer's nearest bound, a `long` of an
|
||
infinity too, where Metal's is 0.** MSL states two answers of a conversion from a floating-point
|
||
type to an integer (MSL 8.6): it rounds toward zero, and a NaN converts to 0. Metal's compiler
|
||
spells every such conversion of MSL as an `air.convert` call (measured over `int`, `uint`,
|
||
`static_cast` and two-lane vector forms of `float` and `half`, in a kernel and in fragment
|
||
functions, with fast math on and off: probe job m3-max-55309, Metal compiler 32023.922), which
|
||
the runtime converts so, and which takes a value past the destination's range to its nearest
|
||
bound. Metal on the M3 Max (macOS 27.0.1) answers the same at every width from `char` to
|
||
`ulong`, from `float` and from `half`, but converts either infinity to a `long` as 0, where
|
||
`ulong` of +inf is its maximum (conformance case `nan-to-integer-conversions`). A plain LLVM
|
||
`fptosi` or `fptoui`, which Metal's compiler does not emit, is Vulkan's `OpConvertFToS` or
|
||
`OpConvertFToU`, whose result for a NaN or a value its integer cannot hold SPIR-V leaves
|
||
undefined: the device's own.
|
||
|
||
**Inline assembly**
|
||
|
||
- **An empty `asm volatile("")` is no compiler barrier.** It is the one inline assembly MSL
|
||
accepts (a template, an operand or a clobber is an "illegal asm statement"), and it assembles to
|
||
no instruction: the translator drops its call, `asm sideeffect "", ""` of type `void ()`, with
|
||
no notice. In Metal it acts on the compiler alone: no memory access moves across it, and a loop
|
||
that holds it is kept. Only a racy spin loop over plain (non-atomic) memory can tell the
|
||
difference, and MSL's memory model leaves that loop undefined; a loop with a visible effect runs
|
||
every iteration either way. Any other inline assembly, which only hand-written AIR can hold, is
|
||
refused where an entry reaches it (`Feature::InlineAsm`).
|
||
|
||
**Kernels**
|
||
|
||
- **A buffer argument a kernel only reads is read-only memory to the device** where the AIR
|
||
declares the argument unaliased (the `air-buffer-no-alias` attribute, which the AIR of Metal's
|
||
current compiler carries on every buffer argument) and no store, atomic, volatile or coherent
|
||
access of the kernel reaches it, through the argument or through a pointer its address may come
|
||
back as: its views are decorated `NonWritable`, and under the identity domains (the runtime)
|
||
its device-address loads go through `NonWritable` places. Under the identity domains a `constant`
|
||
buffer argument is read-only memory too, whether or not the AIR declares it unaliased: Metal's
|
||
`constant` address space is read-only. A dispatch that binds memory to such an argument and to
|
||
an argument the kernel writes may read those bytes as they were before its own writes, where the
|
||
device reads read-only memory through another path (RADV reads a uniform address of it with a
|
||
scalar load). The attribute states that no other argument reaches that memory.
|
||
- **A dispatch that reaches a buffer bound off its argument's alignment is refused** (the
|
||
alignment the kernel's AIR states for the argument, `air.arg_type_align_size`, at most 16;
|
||
`RuntimeFact::AddressAlignment`): the runtime refuses the dispatch
|
||
(`Refusal::BufferOffsetAlignment`) where the bound address, the buffer's `gpuAddress` plus the
|
||
offset, is not a multiple of it, before anything is recorded, as Metal's API validation refuses
|
||
such a bind offset ("must be a multiple of 4 but was set to 2" for a `uint *` argument).
|
||
Without validation an Apple M3 Max (macOS 27.0.1) runs it, reaching each element at its address
|
||
rounded down to the smaller of the element's alignment and 4, for loads, stores and pointers
|
||
stored in a buffer alike (`crates/metal2vulkan-conformance/probes/misaligned_buffer_binds.m`).
|
||
- **A recursive call past the call stack yields zero**: the stack holds what Metal's byte model
|
||
holds for the pipeline's `max_call_stack_depth`; a call that finds it full returns zero, where
|
||
Metal's answer is corrupt.
|
||
- **The threads of a SIMD-group run in no order between barriers** under the identity domains (the
|
||
runtime), where an Apple GPU runs them in lockstep: the Metal Shading Language orders their
|
||
memory accesses only through barriers and atomics, and calls two of them that conflict between
|
||
barriers a data race. A kernel holding such a pair carries the static note
|
||
`PrecisionNote::SimdgroupAccessOrder` (`metal2vulkan-degrade-simdgroup-access-order`).
|
||
- **A simdgroup barrier in divergent code waits for its tangle**, as SPIR-V defines it. The Metal
|
||
Shading Language requires every thread of the SIMD-group to execute a `simdgroup_barrier` that
|
||
any of them executes in a conditional or a loop iteration, so Metal defines no answer there.
|
||
- **A state whose descriptor requires a threadgroup size answers that size's threads** as its
|
||
`maxTotalThreadsPerThreadgroup`, lowered only to the device's own limit
|
||
(`ComputePipelineState::max_total_threads_per_threadgroup`). A compute state compiled by Metal
|
||
for the first time answers the same (M3 Max, macOS 27.0.1, each state of a source compiled
|
||
once: 1 for 1x1x1, 21 for 7x3x1, 128 for 8x8x2, 1024 for 32x32x1). The runtime differs in
|
||
that each state answers its own size: Metal hands a state of a function it has already
|
||
compiled under another size the earlier state's limit, in the same process or a later one. A
|
||
compute state of the same `MTLFunction` answered 16 after a 4x4x1 state, with an 8x8x1 size
|
||
and with none, where the runtime's answers 64 and the compiled limit
|
||
(`compute_pipeline_limits.m`).
|
||
- **A reset compute pipeline descriptor is a fresh one**, every field at its default:
|
||
`ComputePipelineDescriptor::new` stands for `reset`, as `MTLComputePipeline.h` documents it
|
||
("Restore all compute pipeline descriptor properties to their default values", and of
|
||
`maxTotalThreadsPerThreadgroup`, "If it is not set, returns zero"). Metal's `reset` on the M3
|
||
Max (macOS 27.0.1) restores every other field and keeps three as the program set them:
|
||
`threadGroupSizeIsMultipleOfThreadExecutionWidth`, `maxTotalThreadsPerThreadgroup` and
|
||
`requiredThreadsPerThreadgroup` (true, 96 and 8 by 6 by 2 in `compute-descriptor-reset`, where
|
||
the runtime answers false, 0 and 0 by 0 by 0). The tile and mesh render pipeline descriptors'
|
||
resets restore every field on the same machine.
|
||
- **An OpenCL work-item read of a dimension past 2 answers OpenCL's value** in a pipeline of
|
||
OpenCL mode: 0 for `get_global_id`, `get_local_id`, `get_group_id` and `get_global_offset`, 1
|
||
for `get_local_size`, `get_global_size` and `get_num_groups`, as OpenCL C defines them. The M3
|
||
Max (macOS 27.0.1) leaves unwritten the store of `get_global_id(3) + get_local_size(4) +
|
||
get_global_size(5)`, where the runtime stores 2.
|
||
|
||
**Queues and command buffers**
|
||
|
||
- **The runtime schedules a command buffer once its submit thread is free**
|
||
(`metal2vulkan-runtime`'s `CommandBuffer::status`, `add_scheduled_handler` and
|
||
`wait_until_scheduled`). Apple defines `MTLCommandBufferStatusScheduled` as "All dependencies have
|
||
been resolved and the command buffer has been scheduled for execution", and promises no time
|
||
between the two. Every queue's command buffers go to the device's one `VkQueue`, which takes one
|
||
`vkQueueSubmit2` call at a time, all made by the device's one submit thread, and the runtime
|
||
schedules each command buffer there once that thread's earlier calls have returned, whichever
|
||
queue they were for (the runtime's README, Queues, command buffers and events). So a command
|
||
buffer whose wait an earlier submission's signal meets stays `Committed`, its scheduled handlers
|
||
not run, for as long as that submission's driver call lasts, where an Apple M3 Max had scheduled
|
||
it while the signalling command buffer still ran.
|
||
- **A command buffer that waits for a shared event's value is `Scheduled` as it is submitted**
|
||
(`metal2vulkan-runtime`'s `CommandBufferStatus::Scheduled`, `Device::new_shared_event`), its
|
||
scheduled handlers run, while the GPU still waits for the value, as for an `MTLEvent`'s value.
|
||
Apple's MTLCommandBuffer.h describes `MTLCommandBufferStatusScheduled` as "All dependencies
|
||
have been resolved and the command buffer has been scheduled for execution" (lines 47-48), and
|
||
`encodeWaitForEvent:value:` as a command that "pauses execution of this command buffer until
|
||
the specified event reaches a given value" (lines 405-406): execution pauses after scheduling.
|
||
An Apple M3 Max with macOS 27.0.1 holds a command buffer that waits for an `MTLSharedEvent`'s
|
||
value not yet reached at `Committed`, with no scheduled handler run, and its queue's next
|
||
command buffer with it, until the value lands, and holds none for an `MTLEvent` (the
|
||
conformance case `event-wait-behind-a-running-signal`). No documented Metal states that hold,
|
||
so the runtime keeps none. The GPU's order and when each command buffer completes are the same
|
||
either way; what moves is when the program's threads and handlers see `Scheduled`.
|
||
|
||
**Command queues**
|
||
|
||
- **A write whose queue holds nothing back is seen by another queue's command buffer committed
|
||
after it** (`metal2vulkan-runtime`). A writer whose queue has fewer than three submissions in flight
|
||
is submitted at once, ahead of every command buffer committed after it on any queue, so a later
|
||
reader on another queue reads the write, on every device and every run: all 1024 words in the
|
||
conformance case `fence-none-tracked-behind-one`, whose writer's queue has one busy command
|
||
buffer in flight. That is the order Apple describes for a tracked resource: the framework
|
||
prevents subsequent commands from running until write operations finish. Metal's 0 words there
|
||
is measured, not documented: on an M3 Max with macOS 27.0.1, the copy on the other queue ran
|
||
before the fill and read none of it in 5 runs of 5 (job m3-max-52432), and behind three busy
|
||
command buffers likewise (job m3-max-52415); Metal's tracking ordered the copy only against work
|
||
it had already submitted. The runtime's README states what was measured
|
||
(`crates/metal2vulkan-runtime/README.md`, "Queues, command buffers and events").
|