From efe893738ceb07ef3f87b7cbf23d709f0ac85d58 Mon Sep 17 00:00:00 2001 From: Anees Iqbal Date: Wed, 2 Dec 2026 22:40:21 -0800 Subject: [PATCH] Translator, runtime: supportsTextureSampleCount: answers 8 where the device renders it into a colour, a depth and a stencil attachment The planner's ruling: raster_sample_counts, which supportsTextureSampleCount:, getDefaultSamplePositions:count: and programmableSamplePositionsSupported read, is the device's framebufferColorSampleCounts, framebufferDepthSampleCounts and framebufferStencilSampleCounts, intersected with 1, 2, 4 and 8. A query answer, not a second form: a device that renders 8 into all three attachments answers yes for 8, as a Mac's AMD GPU does (an AMD Radeon Pro 5300M under macOS 26.7.1, job 82204) and Apple10 does, where the Apple9 table answered 1, 2 and 4 alone. TEXTURE_SAMPLE_COUNTS leaves the apple_gpu table, whose other answers are Apple9's. Nothing else capped at 4: a texture's samples are its format's own (Limit::TextureSamples), the pipeline link maps 8, the sample mask takes a word, Vulkan's standard locations hold 8, and interpolate_at_sample reads index & (n - 1). Metal at 8, measured on the 5300M (job 82239), and where ours can differ: - interpolate_at_sample: indices 0 to 9, 15, 16, 100, 2^31 - 1 and 2^32 - 1 read sample index mod 8, the runtime's answer; InterpolantAt::Sample's doc says so. - Alpha to coverage: the 5300M covers a pixel at one sample at an alpha of 1/2 or above, every pixel alike, and at two, four and eight samples floor(alpha * n + 1 - t), t 1/8 and 3/8 at even and odd x of an even row, 5/8 and 3/8 of an odd one; every pixel floor(alpha * n) or ceil(alpha * n). The device's own mapping may differ: PrecisionNote::DeviceAlphaCoverage (and docs/API.md's catalogue) now states the 5300M's rule beside the M3 Max's and that eight samples are unmeasured on every device. - Default positions at 8: the 5300M's answer is Vulkan's standard 8 locations. Docs that said 1, 2 and 4: Device::supports_texture_sample_count and default_sample_positions, the floor's standardSampleLocations line, RenderPassDescriptor::sample_positions, positions.rs, the runtime README (default positions, the floor table) and metal_properties' field. Verified: the stacked gate (m3-max-82337, this change on the (i)+(b) hand-over): fmt, clippy in three configurations, rustdoc, doctests, the validation suite 1322 passed (3 skipped), the product suite 3725 passed with the five reds development's next bless takes (none of this change's), the runtime's lavapipe suite 974 passed (4 skipped); the red commit's tests green on lavapipe (2 translator, 9 runtime). On an RTX 3090 (m3-max-82314), an RX 7900 XTX and a 780M (m3-max-82315) and an Iris Xe (m3-max-82316), the same patch on 8a2acc84fb: the runtime's 9 sample-count and sample-position tests pass, each device answers yes for 8 with Vulkan's standard 8 default positions and answers programmableSamplePositionsSupported yes. --- crates/metal2vulkan-runtime/README.md | 7 +++--- crates/metal2vulkan-runtime/src/device.rs | 20 +++++++++------- crates/metal2vulkan-runtime/src/floor.rs | 2 +- crates/metal2vulkan-runtime/src/pass.rs | 2 +- crates/metal2vulkan-runtime/src/positions.rs | 4 ++-- .../src/tests/sample_positions.rs | 2 +- crates/metal2vulkan/src/device/metal.rs | 24 ++++++++++++------- crates/metal2vulkan/src/lower/stage_io/mod.rs | 6 +++-- crates/metal2vulkan/src/pipeline/artifact.rs | 11 ++++++--- docs/API.md | 7 ++++-- 10 files changed, 53 insertions(+), 32 deletions(-) diff --git a/crates/metal2vulkan-runtime/README.md b/crates/metal2vulkan-runtime/README.md index 8e0f7534c..d501b27f3 100644 --- a/crates/metal2vulkan-runtime/README.md +++ b/crates/metal2vulkan-runtime/README.md @@ -103,7 +103,8 @@ them over this crate's items: the pixel's corner, as an Apple M4 takes them. - `Device::default_sample_positions` answers `getDefaultSamplePositions:count:`: the positions a pass of that many samples that sets none rasterizes at, Vulkan's standard sample locations, - which are Metal's at 1, 2 and 4 samples (plain Metal's answers on an Apple M3 Max). A count + which are Metal's at 1, 2, 4 and 8 samples (plain Metal's answers on an Apple M3 Max, and at 8 + on an AMD Radeon Pro 5300M). A count `Device::supports_texture_sample_count` does not answer is `Refusal::DefaultSamplePositions`, where plain Metal ends the process. - `Device::max_threadgroup_memory_length` answers `maxThreadgroupMemoryLength`: the device's @@ -5718,7 +5719,7 @@ cache is answered compile-required on both. per covered sample) is `Gap::Draw` of `SamplePositions`: Vulkan forbids `InterpolateAtSample` in a pipeline drawn at custom sample locations. A pass that sets none rasterizes at Vulkan's standard sample locations (`standardSampleLocations`, under Floor), which are Metal's defaults - at 1, 2 and 4 samples and what `Device::default_sample_positions` answers. + at 1, 2, 4 and 8 samples and what `Device::default_sample_positions` answers. - Layered rendering refuses: in a pass of `renderTargetArrayLength` 2 or more, an attachment or a resolve texture with fewer slices from its own `slice` than the pass's length (`Refusal::AttachmentPlace`, naming the last slice the pass would render), a 3D texture's @@ -6492,7 +6493,7 @@ below 256 an indirect command reaches fewer vertex slots (Known gaps of indirect | `maxComputeWorkGroupSize[1]` | `1024` | a threadgroup's height | | `maxComputeWorkGroupSize[2]` | `64` | a threadgroup's depth; a deeper one runs as one row | | `timestampComputeAndGraphics` | `1` | GPU times of command buffers | -| `standardSampleLocations` | `1` | Metal's sample positions: those of 1, 2 and 4 samples are Vulkan's standard locations | +| `standardSampleLocations` | `1` | Metal's sample positions: those of 1, 2, 4 and 8 samples are Vulkan's standard locations | | `timestampValidBits` | `36` | GPU times of command buffers | | `maxColorAttachments` | `8` | every color attachment of a render pass descriptor | | `maxFragmentOutputAttachments` | `8` | a fragment function's output to every color attachment | diff --git a/crates/metal2vulkan-runtime/src/device.rs b/crates/metal2vulkan-runtime/src/device.rs index 1af31c69a..af1a22269 100644 --- a/crates/metal2vulkan-runtime/src/device.rs +++ b/crates/metal2vulkan-runtime/src/device.rs @@ -1042,10 +1042,12 @@ impl Device { } /// `supportsTextureSampleCount:`: whether `count` is a sample count the device renders into a - /// colour and a depth attachment (its `framebufferColorSampleCounts` and - /// `framebufferDepthSampleCounts`, of which Vulkan requires 1 and 4) that an Apple GPU takes: - /// 1, 2 or 4 (Apple9's "Maximum sample count in render passes with MSAA" is 4). A count Vulkan - /// has no bit for (0, 3, above 64) is not. A texture's own format may take fewer + /// colour, a depth and a stencil attachment (its `framebufferColorSampleCounts`, + /// `framebufferDepthSampleCounts` and `framebufferStencilSampleCounts`, of which Vulkan + /// requires 1 and 4) that a Mac's Metal renders: 1, 2, 4 or 8. A Mac's AMD GPU answers yes for + /// 8 (an AMD Radeon Pro 5300M, macOS 26.7.1), as Apple10 does, where an Apple M3 Max answers + /// no; so a device that renders 8 answers yes where the M3 Max does not. A count Vulkan has no + /// bit for (0, 3, above 64) is not, nor is 16. A texture's own format may take fewer /// ([`Limit::TextureSamples`](crate::Limit::TextureSamples)). pub fn supports_texture_sample_count(&self, count: u32) -> bool { let counts = self @@ -1074,11 +1076,13 @@ impl Device { /// `getDefaultSamplePositions:count:`: where the samples of a pass of `count` samples lie when /// it sets no positions, sample `i` at `i`: Vulkan's standard sample locations, which the - /// floor's `standardSampleLocations` makes every pass that sets none rasterize at. At 1, 2 and 4 - /// samples, the counts [`supports_texture_sample_count`](Device::supports_texture_sample_count) + /// floor's `standardSampleLocations` makes every pass that sets none rasterize at. At 1, 2, 4 + /// and 8 samples, the counts [`supports_texture_sample_count`](Device::supports_texture_sample_count) /// can answer, they are Metal's own (an Apple M3 Max, macOS 27.0.1: (0.5, 0.5); (0.75, 0.75), - /// (0.25, 0.25); (0.375, 0.125), (0.875, 0.375), (0.125, 0.625), (0.625, 0.875)). Metal writes - /// the positions into the caller's array; this returns them. + /// (0.25, 0.25); (0.375, 0.125), (0.875, 0.375), (0.125, 0.625), (0.625, 0.875); and an AMD + /// Radeon Pro 5300M, macOS 26.7.1, at 8: (0.5625, 0.3125), (0.4375, 0.6875), (0.8125, 0.5625), + /// (0.3125, 0.1875), (0.1875, 0.8125), (0.0625, 0.4375), (0.6875, 0.9375), (0.9375, 0.0625)). + /// Metal writes the positions into the caller's array; this returns them. /// /// Err: [`Refusal::DefaultSamplePositions`] for a count the device does not render, where plain /// Metal ends the process. diff --git a/crates/metal2vulkan-runtime/src/floor.rs b/crates/metal2vulkan-runtime/src/floor.rs index ded1f3425..4e0b10562 100644 --- a/crates/metal2vulkan-runtime/src/floor.rs +++ b/crates/metal2vulkan-runtime/src/floor.rs @@ -1804,7 +1804,7 @@ pub(crate) fn limits(device: &Queried) -> [(&'static str, u32, u32); 17] { limits.timestamp_compute_and_graphics, vk::TRUE, ), - // Metal's sample positions of 1, 2 and 4 samples are Vulkan's standard locations. + // Metal's sample positions of 1, 2, 4 and 8 samples are Vulkan's standard locations. ( "standardSampleLocations", limits.standard_sample_locations, diff --git a/crates/metal2vulkan-runtime/src/pass.rs b/crates/metal2vulkan-runtime/src/pass.rs index 7da68697c..a979de539 100644 --- a/crates/metal2vulkan-runtime/src/pass.rs +++ b/crates/metal2vulkan-runtime/src/pass.rs @@ -440,7 +440,7 @@ pub struct RenderPassDescriptor { /// a pixel (a tie up, at most 15/16) and each sample past them at the pixel's corner, as an /// Apple M4 took them, through `VK_EXT_sample_locations` /// ([`Device::programmable_sample_positions_supported`](crate::Device::programmable_sample_positions_supported)). - /// A set that comes to Metal's default positions at 1, 2 or 4 samples (Vulkan's standard + /// A set that comes to Metal's default positions at 1, 2, 4 or 8 samples (Vulkan's standard /// locations) rasterizes as an empty one, on every device. /// /// Refused ([`Refusal::SamplePositions`]), as Metal's validation stops the descriptor: more diff --git a/crates/metal2vulkan-runtime/src/positions.rs b/crates/metal2vulkan-runtime/src/positions.rs index 7a9eab207..a378baa68 100644 --- a/crates/metal2vulkan-runtime/src/positions.rs +++ b/crates/metal2vulkan-runtime/src/positions.rs @@ -14,7 +14,7 @@ //! Vulkan is then a sixteenth in `[0, 15/16]`, which every device with the extension represents //! exactly: Vulkan requires `sampleLocationSubPixelBits` of 4 and a `sampleLocationCoordinateRange` //! of `[0, 0.9375]` at least. A set that comes to Vulkan's standard locations for the pass's -//! samples (the floor's `standardSampleLocations`, Metal's defaults at 1, 2 and 4 samples) is the +//! samples (the floor's `standardSampleLocations`, Metal's defaults at 1, 2, 4 and 8 samples) is the //! device's own: the pass rasterizes as one that sets none. //! //! A pass of other positions draws with `VkPipeline`s made with `sampleLocationsEnable` and the @@ -61,7 +61,7 @@ pub(crate) fn on_grid(coordinate: f32) -> f32 { /// Vulkan's standard sample locations of `samples` samples (the Vulkan specification's /// "Standard Sample Locations" table), sample `i` at `i`, which the floor's -/// `standardSampleLocations` makes the device's own: Metal's default positions at 1, 2 and 4 +/// `standardSampleLocations` makes the device's own: Metal's default positions at 1, 2, 4 and 8 /// samples. None for a count the table does not hold. pub(crate) fn standard(samples: u32) -> Option<&'static [[f32; 2]]> { Some(match samples { diff --git a/crates/metal2vulkan-runtime/src/tests/sample_positions.rs b/crates/metal2vulkan-runtime/src/tests/sample_positions.rs index 889854903..af06d9581 100644 --- a/crates/metal2vulkan-runtime/src/tests/sample_positions.rs +++ b/crates/metal2vulkan-runtime/src/tests/sample_positions.rs @@ -343,7 +343,7 @@ mod placed { /// Plain Metal's answers to `getDefaultSamplePositions:count:` at each count an Apple M3 Max /// renders, macOS 27.0.1 (the conformance crate's `probes/default_sample_positions.m`, job - /// m3-max-78953), sample `i` at `i`. + /// m3-max-78953), and at 8 an AMD Radeon Pro 5300M's, sample `i` at `i`. pub(crate) const METAL_DEFAULTS: [(u32, &[[f32; 2]]); 4] = [ (1, &[[0.5, 0.5]]), (2, &[[0.75, 0.75], [0.25, 0.25]]), diff --git a/crates/metal2vulkan/src/device/metal.rs b/crates/metal2vulkan/src/device/metal.rs index 8db1bffae..31325560d 100644 --- a/crates/metal2vulkan/src/device/metal.rs +++ b/crates/metal2vulkan/src/device/metal.rs @@ -41,7 +41,8 @@ pub struct MetalDeviceProperties { /// The largest buffer a shader can address: `maxStorageBufferRange` in Profile F (larger /// windows are refused at plan time); the allocation limit in Profile A. pub max_buffer_length: u64, - /// Sample counts usable for both color and depth attachments, of an Apple GPU's 1, 2 and 4. + /// Sample counts usable for color, depth and stencil attachments alike, of the 1, 2, 4 and 8 + /// a Mac's Metal renders (8 on a Mac's AMD GPU and on Apple10, not on Apple9). pub raster_sample_counts: vk::SampleCountFlags, /// Per-stage simd feasibility. A kernel's simdgroups that no subgroup tier runs are emulated, /// for operations in workgroup-uniform control flow. @@ -64,23 +65,27 @@ pub struct MetalDeviceProperties { /// own limit, so no vendor's driver offers a guest more than an Apple GPU does, as /// `isDepth24Stencil8PixelFormatSupported` answers Apple's no everywhere. pub(crate) mod apple_gpu { - use crate::vk::SampleCountFlags; - /// `maxThreadsPerThreadgroup` on each axis and `maxTotalThreadsPerThreadgroup`: "Maximum /// threads per threadgroup", 1024 from Apple4. pub(crate) const MAX_THREADS_PER_THREADGROUP: u32 = 1024; /// `maxThreadgroupMemoryLength`: "Maximum total threadgroup memory allocation", 32 KiB from /// Apple4. pub(crate) const MAX_THREADGROUP_MEMORY_LENGTH: u32 = 32 * 1024; - /// `supportsTextureSampleCount:` answers yes for 1, 2 and 4: "Maximum sample count in render - /// passes with MSAA" is 4 to Apple9 (8 from Apple10). - pub(crate) const TEXTURE_SAMPLE_COUNTS: SampleCountFlags = SampleCountFlags::COUNT_1 - .union(SampleCountFlags::COUNT_2) - .union(SampleCountFlags::COUNT_4); /// "Maximum 2D texture width and height": 16384 from Apple3 (32768 from Apple10). pub(crate) const MAX_TEXTURE_DIMENSION_2D: u32 = 16_384; } +/// The sample counts `supportsTextureSampleCount:` may answer yes for, where the device renders +/// them into a color, a depth and a stencil attachment: 1, 2, 4 and 8. A Mac's AMD GPU answers yes +/// for each (an AMD Radeon Pro 5300M under macOS 26.7.1: yes for 1, 2, 4 and 8, no for 16), as +/// Apple10 does ("Maximum sample count in render passes with MSAA" is 8 from Apple10), where the +/// Apple9 GPUs the runtime otherwise answers as take 1, 2 and 4 alone (an Apple M3 Max under macOS +/// 27.0.1 answers no for 8). Neither Mac answers yes for 16. +pub(crate) const TEXTURE_SAMPLE_COUNTS: vk::SampleCountFlags = vk::SampleCountFlags::COUNT_1 + .union(vk::SampleCountFlags::COUNT_2) + .union(vk::SampleCountFlags::COUNT_4) + .union(vk::SampleCountFlags::COUNT_8); + /// Metal's read-write tier-1 formats. pub const READ_WRITE_TIER1_FORMATS: &[PixelFormat] = &[ PixelFormat::R32Float, @@ -215,7 +220,8 @@ impl MetalDeviceProperties { max_buffer_length, raster_sample_counts: l.framebuffer_color_sample_counts & l.framebuffer_depth_sample_counts - & apple_gpu::TEXTURE_SAMPLE_COUNTS, + & l.framebuffer_stencil_sample_counts + & TEXTURE_SAMPLE_COUNTS, simd: SimdSupport { vertex: stage(Stage::Vertex), fragment: stage(Stage::Fragment), diff --git a/crates/metal2vulkan/src/lower/stage_io/mod.rs b/crates/metal2vulkan/src/lower/stage_io/mod.rs index 1efa71fb1..de0638b72 100644 --- a/crates/metal2vulkan/src/lower/stage_io/mod.rs +++ b/crates/metal2vulkan/src/lower/stage_io/mod.rs @@ -1476,8 +1476,10 @@ pub enum InterpolantAt { /// `interpolate_at_sample(index)`: `InterpolateAtSample` at `index & (n - 1)`, `n` the /// pipeline's sample count (`SpecKey::SampleCount`, a power of two), the sample Metal reads /// for any index (Metal probes on the Apple M3 Max, macOS 27.0.1: at 1, 2 and 4 samples every - /// index to 16, 100, `2^31 - 1` and `2^32 - 1` read sample `index mod n`), where Vulkan leaves - /// the position of a sample the pipeline lacks undefined. + /// index to 16, 100, `2^31 - 1` and `2^32 - 1` read sample `index mod n`; on an AMD Radeon Pro + /// 5300M, macOS 26.7.1, at 1, 2, 4 and 8 samples, indices 0 to 9, 15, 16, 100, `2^31 - 1` and + /// `2^32 - 1` read sample `index mod n`), where Vulkan leaves the position of a sample the + /// pipeline lacks undefined. Sample(ValueId), /// `interpolate_at_offset(offset)`: `InterpolateAtOffset` at `offset - origin`, Metal's offset /// from the pixel's upper-left corner moved to the device's origin, clamped to `[min, max]`, diff --git a/crates/metal2vulkan/src/pipeline/artifact.rs b/crates/metal2vulkan/src/pipeline/artifact.rs index 9bb73cbe1..a62787198 100644 --- a/crates/metal2vulkan/src/pipeline/artifact.rs +++ b/crates/metal2vulkan/src/pipeline/artifact.rs @@ -1780,7 +1780,12 @@ pub enum PrecisionNote { /// Apple M3 Max under macOS 27.0.1, covers a pixel at one sample where its alpha is above 1/2, /// and at two and four samples `ceil(alpha * n - t)` of its `n` samples, `t` a 2x2 ordered /// dither of the pixel's window coordinates (1/8 and 5/8 at even and odd `x` of an even row, - /// 7/8 and 3/8 of an odd one), either count within an `f32` ulp of a threshold. Vulkan fixes no + /// 7/8 and 3/8 of an odd one), either count within an `f32` ulp of a threshold. Metal's rule is + /// the GPU's own: an AMD Radeon Pro 5300M under macOS 26.7.1, over the same sweep at one, two, + /// four and eight samples (job 82239), covers a pixel at one sample where its alpha is 1/2 or + /// above, every pixel alike, and at two, four and eight samples `floor(alpha * n + 1 - t)` of + /// its `n` samples, `t` 1/8 and 3/8 at even and odd `x` of an even row, 5/8 and 3/8 of an odd + /// one; on both GPUs every pixel covers `floor(alpha * n)` or `ceil(alpha * n)`. Vulkan fixes no /// algorithm ("No specific algorithm is specified for converting the alpha value to a temporary /// coverage mask. It is intended that the number of 1's in this value be proportional to the /// alpha value (clamped to \[0,1\]), with all 1's corresponding to a value of 1.0 and all 0's @@ -1797,8 +1802,8 @@ pub enum PrecisionNote { /// pixel is one sample apart only where `alpha * 4` lies within an `f32` ulp of a threshold /// (96 of 10,224). NVIDIA's 615.71.09 on the RTX 5080 Laptop GPU stores the fragment at one /// sample at an alpha of 1/2, where Metal drops it (the conformance case `a2c-on-1-alpha-50`); - /// beyond that row, and on every other device, the count is unmeasured, and Vulkan bounds it - /// by nothing but its intent above. + /// beyond that row, and on every other device, the count is unmeasured, eight samples on + /// every device included, and Vulkan bounds it by nothing but its intent above. /// /// Validation derives no tolerance and no explanation from it: a difference stays a /// mismatch for review. diff --git a/docs/API.md b/docs/API.md index 39268a650..a42a5e246 100644 --- a/docs/API.md +++ b/docs/API.md @@ -2217,14 +2217,17 @@ bound. 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 + 5/8; 7/8, 3/8), either count within an `f32` ulp of a threshold. Metal's rule is the GPU's own: an + AMD Radeon Pro 5300M under macOS 26.7.1 covers a pixel at one sample at an alpha of 1/2 or above, + and at two, four and eight samples `floor(alpha * n + 1 - t)` samples (1/8, 3/8; 5/8, 3/8); on + both, every pixel covers `floor(alpha * n)` or `ceil(alpha * n)`. 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. + every other device and count is unmeasured, eight samples on every device included. - `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