From d3ab77f844b978f263dd50b6cb6a071e494c8e23 Mon Sep 17 00:00:00 2001 From: Anees Iqbal Date: Wed, 11 Nov 2026 10:06:02 -0800 Subject: [PATCH] Runtime: take resource option bit 20 as Metal takes it Bit 20 (0x100000) is an option no Metal header names, of unstated meaning, which programs pass with their buffers' options (0x100010 for a Managed no-copy buffer). ResourceOptions::from_raw refused it as undefined, so such a buffer could not be made at all. Measured with probes/resource_option_bit20.m on an M3 Max (macOS 27.0.1) and a Radeon Pro 5300M (macOS 26.7.1), plain and under API validation: buffers of a length, of bytes and over the caller's pages are made with it, of every storage mode, and report their options without it; so do a heap and a heap's buffer (API validation stops the heap's buffer); a texture descriptor with it ends Metal's process. So from_raw takes it (ResourceOptions::BIT_20), every buffer and heap maker drops it (own_memory_options, no_copy_options, take_options, a heap's new_buffer), and texture_options refuses it as it refuses bit 24 there. Nothing reads it. docs/API.md states both halves; api_signatures blessed. Tests: values::bit_20_is_taken_and_dropped_apart, and the device test option_bit_20 (buffers, no-copy, heaps and their buffers report without it; a texture descriptor with it is Invalid). Not yet run. --- .../probes/resource_option_bit20.m | 17 ++++ .../src/buffer_descriptor.rs | 4 +- .../metal2vulkan-runtime/src/degradation.rs | 27 ++++-- crates/metal2vulkan-runtime/src/heap.rs | 9 +- crates/metal2vulkan-runtime/src/tests/mod.rs | 2 + .../src/tests/option_bit_20.rs | 92 +++++++++++++++++++ .../metal2vulkan-runtime/src/tests/values.rs | 15 +++ crates/metal2vulkan-runtime/src/values.rs | 30 +++++- crates/metal2vulkan/tests/api_signatures.tsv | 2 + docs/API.md | 14 +++ 10 files changed, 192 insertions(+), 20 deletions(-) create mode 100644 crates/metal2vulkan-runtime/src/tests/option_bit_20.rs diff --git a/crates/metal2vulkan-conformance/probes/resource_option_bit20.m b/crates/metal2vulkan-conformance/probes/resource_option_bit20.m index 899d514fc..d82d4b94c 100644 --- a/crates/metal2vulkan-conformance/probes/resource_option_bit20.m +++ b/crates/metal2vulkan-conformance/probes/resource_option_bit20.m @@ -7,6 +7,23 @@ // BIT20 made=<0|1> options= storage= // cache= hazard= // +// WHAT IT MEASURED, plain and under API validation, an Apple M3 Max under macOS 27.0.1 and an AMD +// Radeon Pro 5300M under macOS 26.7.1 alike where both ran: +// +// - newBufferWithLength: (0x100000, 0x100010, 0x100020), newBufferWithBytes: and +// newBufferWithBytesNoCopy: (0x100000, 0x100010) are made, and report their options WITHOUT +// bit 20: 0x200, 0x210 and 0x220 (the hazard tracking mode resolved to tracked); +// - a heap with it (the M3 Max: `Shared` and `Private`; the 5300M refuses a `Shared` heap +// whatever the bit) is made and reports its options without it; a heap's newBufferWithLength: +// with it is made plainly, reporting without it, and stopped by API validation ("unknown bits +// 0x100000"); +// - a texture descriptor keeps it (`resourceOptions` reads 0x100000), and newTextureWithDescriptor: +// with it ends the process plainly (IOGPUMetalSuballocator: "Different MTLResourceOptions used +// for the same heap index"). +// +// So `ResourceOptions::from_raw` takes it, a buffer and a heap are made as without it, and a +// texture descriptor with it is refused (`ResourceOptions::BIT_20`). +// // Run it twice, plain and under API validation (a refusal there ends the process, so the line // before it says how far it got): // diff --git a/crates/metal2vulkan-runtime/src/buffer_descriptor.rs b/crates/metal2vulkan-runtime/src/buffer_descriptor.rs index 7577c994d..1dd90a950 100644 --- a/crates/metal2vulkan-runtime/src/buffer_descriptor.rs +++ b/crates/metal2vulkan-runtime/src/buffer_descriptor.rs @@ -2424,12 +2424,12 @@ fn unwritable_pages(host: &HostWindow, vouched: bool, counts: &Counters) -> Resu } /// `options` of a no-copy buffer or heap over the caller's bytes without -/// [`ResourceOptions::PRIVATE_BIT`], and whether its memory drops the GPU's writes, as Metal's does +/// [`ResourceOptions::PRIVATE_BIT`] and [`ResourceOptions::BIT_20`], and whether its memory drops the GPU's writes, as Metal's does /// where the options set the bit: on macOS the runtime imports the pages through a read-only view /// of them (`read_only_view.rs`); elsewhere it makes the object over a copy of them whose GPU /// writes it never writes back (`copied.rs`, announced as `copied-host-pages`). pub(super) fn no_copy_options(options: ResourceOptions) -> (ResourceOptions, bool) { - options.without_private_bit() + options.without_bit_20().without_private_bit() } /// Why a no-copy buffer or heap is made over a copy of the caller's pages (`Device::copied_buffer`). diff --git a/crates/metal2vulkan-runtime/src/degradation.rs b/crates/metal2vulkan-runtime/src/degradation.rs index 4ea12587c..4da3ec5c3 100644 --- a/crates/metal2vulkan-runtime/src/degradation.rs +++ b/crates/metal2vulkan-runtime/src/degradation.rs @@ -3405,9 +3405,15 @@ pub(crate) fn announcement_breach(each: bool) -> Option { } } -/// `Invalid` (`Problem::UndefinedValue`) where `options` set [`ResourceOptions::PRIVATE_BIT`]. -fn refuse_private_bit(call: &'static str, options: ResourceOptions) -> Result<(), Error> { - match options.without_private_bit().1 { +/// `Invalid` (`Problem::UndefinedValue`) where `options` set [`ResourceOptions::PRIVATE_BIT`], +/// or, where `bit_20`, [`ResourceOptions::BIT_20`]. +fn refuse_private_bit( + call: &'static str, + options: ResourceOptions, + bit_20: bool, +) -> Result<(), Error> { + let with_bit_20 = bit_20 && options.without_bit_20() != options; + match options.without_private_bit().1 || with_bit_20 { true => Err(crate::error::invalid( call, crate::error::Problem::UndefinedValue { @@ -3898,7 +3904,8 @@ impl Device { call: &'static str, options: ResourceOptions, ) -> ResourceOptions { - let (taken, private) = options.without_private_bit(); + // Bit 20 is dropped unannounced, as Metal makes a heap with it and reports it without. + let (taken, private) = options.without_bit_20().without_private_bit(); if private { self.degrade(Degradation::PrivateResourceOption, || { ( @@ -3917,27 +3924,29 @@ impl Device { /// [`ResourceOptions::PRIVATE_BIT`] (`Invalid`, `Problem::UndefinedValue`), as Metal ends the /// process there (the M3 Max under macOS 27.0.1: `newBufferWithLength:options:` and /// `newBufferWithBytes:length:options:`, `Shared` or `Private`, assert in the buffer's - /// suballocator, "Different MTLResourceOptions used for the same heap index"). + /// suballocator, "Different MTLResourceOptions used for the same heap index"); without + /// [`ResourceOptions::BIT_20`], as Metal makes such a buffer with it and reports it without. pub(super) fn own_memory_options( &self, call: &'static str, options: ResourceOptions, ) -> Result { - refuse_private_bit(call, options)?; - Ok(options) + refuse_private_bit(call, options, false)?; + Ok(options.without_bit_20()) } /// `descriptor`, refused where its options set [`ResourceOptions::PRIVATE_BIT`] (`Invalid`, /// `Problem::UndefinedValue`), as Metal's texture descriptor validation ends the process there /// for every texture (the M3 Max under macOS 27.0.1: "MTLResourceOptions (0x1000000) contains /// unknown bits", a device's texture, `Shared` or `Private`, a heap's, a buffer's and a - /// shareable one alike). + /// shareable one alike), or [`ResourceOptions::BIT_20`], with which Metal's suballocator ends + /// the process for a device's texture (the M3 Max under macOS 27.0.1, `Shared`). pub(super) fn texture_options( &self, call: &'static str, descriptor: &TextureDescriptor, ) -> Result { - refuse_private_bit(call, descriptor.resource_options)?; + refuse_private_bit(call, descriptor.resource_options, true)?; Ok(*descriptor) } diff --git a/crates/metal2vulkan-runtime/src/heap.rs b/crates/metal2vulkan-runtime/src/heap.rs index 54260c76c..f79e855f3 100644 --- a/crates/metal2vulkan-runtime/src/heap.rs +++ b/crates/metal2vulkan-runtime/src/heap.rs @@ -1060,8 +1060,9 @@ impl Heap { pub fn new_buffer(&self, length: u64, options: ResourceOptions) -> Result { const CALL: &str = "new_buffer"; // Metal makes a placed buffer with the private bit as without it (the heap's own options - // decide where its writes go). - let options = options.without_private_bit().0; + // decide where its writes go), and with bit 20 as without it + // (`ResourceOptions::BIT_20`). + let options = options.without_bit_20().without_private_bit().0; let options = self.placed_options(CALL, options)?; let bytes = self.check_type(CALL, HeapType::Automatic)?; if length == 0 { @@ -1097,8 +1098,8 @@ impl Heap { offset: u64, ) -> Result { const CALL: &str = "new_buffer_at_offset"; - // As `new_buffer`: the private bit is the heap's to take. - let options = options.without_private_bit().0; + // As `new_buffer`: the private bit is the heap's to take, and bit 20 is dropped. + let options = options.without_bit_20().without_private_bit().0; let options = self.placed_options(CALL, options)?; let bytes = self.check_type(CALL, HeapType::Placement)?; self.check_length(CALL, length)?; diff --git a/crates/metal2vulkan-runtime/src/tests/mod.rs b/crates/metal2vulkan-runtime/src/tests/mod.rs index db58dc6cc..a9be84411 100644 --- a/crates/metal2vulkan-runtime/src/tests/mod.rs +++ b/crates/metal2vulkan-runtime/src/tests/mod.rs @@ -368,6 +368,8 @@ mod opencl_swizzles; #[cfg(any(m2v_gpu, m2v_cpu_icd))] mod opengl_mode; #[cfg(any(m2v_gpu, m2v_cpu_icd))] +mod option_bit_20; +#[cfg(any(m2v_gpu, m2v_cpu_icd))] mod optional_extensions; mod options; mod own; diff --git a/crates/metal2vulkan-runtime/src/tests/option_bit_20.rs b/crates/metal2vulkan-runtime/src/tests/option_bit_20.rs new file mode 100644 index 000000000..83485baf3 --- /dev/null +++ b/crates/metal2vulkan-runtime/src/tests/option_bit_20.rs @@ -0,0 +1,92 @@ +//! `MTLResourceOptions` bit 20 ([`ResourceOptions::BIT_20`]), as Metal answers it on an M3 Max and +//! a Radeon Pro 5300M (`crates/metal2vulkan-conformance/probes/resource_option_bit20.m`): a buffer +//! of a length, of bytes or over host pages, of each storage mode, a heap and a heap's buffer are +//! made with it and report their options without it; a texture descriptor with it is refused +//! (`Invalid`, `Problem::UndefinedValue`), where Metal ends the process. +//! +//! A device test: compiled only under the validation crate's GPU safety cfgs (`m2v_gpu`, +//! `m2v_cpu_icd`), on the kind of device the cfg selects. + +use super::blit_holds::{device, Host, Scratch, BYTES}; +use crate::{ + Error, HeapDescriptor, HeapType, Invalid, Problem, ResourceOptions, StorageMode, + TextureDescriptor, +}; +use metal2vulkan::metal::PixelFormat; + +/// `options` with bit 20. +fn with_bit_20(options: ResourceOptions) -> ResourceOptions { + ResourceOptions::from_raw(options.raw() | ResourceOptions::BIT_20).unwrap() +} + +/// The options of a storage mode, the other modes at their defaults. +fn of(storage: StorageMode) -> ResourceOptions { + ResourceOptions::shared().with_storage_mode(storage) +} + +#[test] +fn bit_20_is_taken_by_buffers_and_heaps_and_refused_by_textures() { + let scratch = Scratch::new("option-bit-20"); + let device = device(&scratch.0); + // Reported as Metal reports them: the storage mode, the hazard tracking mode resolved to + // tracked (0x200), and no bit 20. + for storage in [ + StorageMode::Shared, + StorageMode::Managed, + StorageMode::Private, + ] { + let asked = with_bit_20(of(storage)); + let reported = 0x200 | storage.raw() << 4; + let buffer = device.new_buffer(4096, asked).unwrap(); + assert_eq!(buffer.resource_options().raw(), reported, "{storage:?}"); + if storage == StorageMode::Private { + continue; + } + let buffer = device.new_buffer_with_bytes(&[7; 64], asked).unwrap(); + assert_eq!(buffer.resource_options().raw(), reported, "{storage:?}"); + let host = Host::new(); + // SAFETY: the pages outlive the buffer, which alone uses them. + let made = unsafe { + device.new_buffer_with_bytes_no_copy(host.memory(), BYTES as u64, asked, None) + }; + match made { + Ok(buffer) => assert_eq!(buffer.resource_options().raw(), reported, "{storage:?}"), + Err(Error::Unsupported(gap)) => { + eprintln!("OPTION-BIT-20: the device imports no host memory: {gap:?}"); + } + Err(error) => panic!("no-copy {storage:?}: {error:?}"), + } + } + for storage in [StorageMode::Shared, StorageMode::Private] { + let descriptor = HeapDescriptor { + heap_type: HeapType::Automatic, + size: 1 << 20, + resource_options: with_bit_20(of(storage)), + ..HeapDescriptor::default() + }; + let heap = device.new_heap(&descriptor).unwrap(); + assert_eq!( + heap.resource_options().without_bit_20(), + heap.resource_options() + ); + let buffer = heap.new_buffer(4096, with_bit_20(of(storage))).unwrap(); + assert_eq!( + buffer.resource_options().raw() & ResourceOptions::BIT_20, + 0, + "{storage:?}" + ); + } + let mut descriptor = TextureDescriptor::texture_2d(PixelFormat::RGBA8Unorm, 16, 16, false); + descriptor.resource_options = with_bit_20(ResourceOptions::shared()); + let raw = descriptor.resource_options.raw(); + assert_eq!( + device.new_texture(&descriptor).err(), + Some(Error::Invalid(Invalid::new( + "new_texture", + Problem::UndefinedValue { + ty: "MTLResourceOptions", + raw, + }, + ))) + ); +} diff --git a/crates/metal2vulkan-runtime/src/tests/values.rs b/crates/metal2vulkan-runtime/src/tests/values.rs index d4788ae38..7f493116a 100644 --- a/crates/metal2vulkan-runtime/src/tests/values.rs +++ b/crates/metal2vulkan-runtime/src/tests/values.rs @@ -85,6 +85,21 @@ fn the_private_bit_is_taken_and_dropped_apart() { assert!(ResourceOptions::from_raw(0x200_0000).is_err()); } +/// Bit 20 is taken beside the modes, as Metal takes it, and making a buffer or a heap drops it; +/// bits 21 to 23 stay undefined. +#[test] +fn bit_20_is_taken_and_dropped_apart() { + let raw = 0x10_0000 | 0x10; + let options = ResourceOptions::from_raw(raw).unwrap(); + assert_eq!(options.raw(), raw); + assert_eq!(options.storage_mode(), Ok(StorageMode::Managed)); + assert_eq!(options.without_bit_20().raw(), 0x10); + assert_eq!(options.without_private_bit(), (options, false)); + for bit in 21..24 { + assert!(ResourceOptions::from_raw(1 << bit).is_err(), "bit {bit}"); + } +} + #[test] fn undefined_raw_values_are_refused() { let undefined = |ty, raw| { diff --git a/crates/metal2vulkan-runtime/src/values.rs b/crates/metal2vulkan-runtime/src/values.rs index b012cbfa7..ee65908d5 100644 --- a/crates/metal2vulkan-runtime/src/values.rs +++ b/crates/metal2vulkan-runtime/src/values.rs @@ -112,8 +112,8 @@ impl HazardTrackingMode { } /// `MTLResourceOptions`: the CPU cache mode in bits 0-3, the storage mode in bits 4-7, and the -/// hazard tracking mode in bits 8-9, as Metal packs them, and Metal's private bit 24 -/// ([`ResourceOptions::PRIVATE_BIT`]). +/// hazard tracking mode in bits 8-9, as Metal packs them, Metal's private bit 24 +/// ([`ResourceOptions::PRIVATE_BIT`]) and bit 20 ([`ResourceOptions::BIT_20`]). #[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)] pub struct ResourceOptions(u64); @@ -149,6 +149,20 @@ impl ResourceOptions { /// (`Device::new_buffer_with_vk_buffer`). pub const PRIVATE_BIT: u64 = 1 << 24; + /// Bit 20 (0x10_0000), an option no Metal header names, whose meaning Metal does not state + /// (programs pass it with a buffer's options: 0x100010 for a `Managed` no-copy buffer). + /// [`from_raw`](Self::from_raw) takes it. What Metal does with it (an M3 Max under macOS + /// 27.0.1 and an AMD Radeon Pro 5300M under macOS 26.7.1, plain and under API validation; + /// `crates/metal2vulkan-conformance/probes/resource_option_bit20.m`), and the runtime alike: + /// - a buffer of a length, of bytes or over the caller's pages (`newBufferWithBytesNoCopy:`), + /// of every storage mode, is made with it and reports its options without it; so are a + /// heap and a heap's buffer (the M3 Max; Metal's API validation stops the heap's buffer, "unknown + /// bits 0x100000"). The runtime makes each as without it, and nothing reads it; + /// - a texture descriptor with it ends Metal's process (its suballocator asserts, "Different + /// MTLResourceOptions used for the same heap index"): refused `Invalid` + /// (`Problem::UndefinedValue`), as with [`PRIVATE_BIT`](Self::PRIVATE_BIT). + pub const BIT_20: u64 = 1 << 20; + /// The options of these three modes. pub fn new( storage: StorageMode, @@ -177,10 +191,10 @@ impl ResourceOptions { } /// The options of Metal's raw value `raw`: refused when it sets a bit outside the three - /// fields and [`PRIVATE_BIT`](Self::PRIVATE_BIT), or a field to a value its type does not - /// define. + /// fields, [`PRIVATE_BIT`](Self::PRIVATE_BIT) and [`BIT_20`](Self::BIT_20), or a field to a + /// value its type does not define. pub fn from_raw(raw: u64) -> Result { - if raw & !(Self::DEFINED | Self::PRIVATE_BIT) != 0 { + if raw & !(Self::DEFINED | Self::PRIVATE_BIT | Self::BIT_20) != 0 { return Err(undefined("MTLResourceOptions", raw)); } let options = ResourceOptions(raw); @@ -236,6 +250,12 @@ impl ResourceOptions { self.0 & Self::PRIVATE_BIT != 0, ) } + + /// The options without [`BIT_20`](Self::BIT_20), as Metal makes and reports a buffer or a + /// heap of them. + pub(crate) fn without_bit_20(self) -> ResourceOptions { + ResourceOptions(self.0 & !Self::BIT_20) + } } /// `MTLCommandBufferStatus`. diff --git a/crates/metal2vulkan/tests/api_signatures.tsv b/crates/metal2vulkan/tests/api_signatures.tsv index 306deec17..3f76c4ccb 100644 --- a/crates/metal2vulkan/tests/api_signatures.tsv +++ b/crates/metal2vulkan/tests/api_signatures.tsv @@ -55,6 +55,7 @@ assoc-const metal2vulkan-runtime::PixelFormatInfo::INVALID pub const INVALID: Pi assoc-const metal2vulkan-runtime::PrivateCount::ALL pub const ALL: [PrivateCount; 28] assoc-const metal2vulkan-runtime::PrivateFlag::ALL pub const ALL: [PrivateFlag; 23] assoc-const metal2vulkan-runtime::Query::ALL pub const ALL: [Query; 9] +assoc-const metal2vulkan-runtime::ResourceOptions::BIT_20 pub const BIT_20: u64 assoc-const metal2vulkan-runtime::ResourceOptions::PRIVATE_BIT pub const PRIVATE_BIT: u64 assoc-const metal2vulkan-runtime::StoreActionOptions::CUSTOM_SAMPLE_POSITIONS pub const CUSTOM_SAMPLE_POSITIONS: StoreActionOptions assoc-const metal2vulkan-runtime::StoreActionOptions::NONE pub const NONE: StoreActionOptions @@ -536,6 +537,7 @@ assoc-const-value metal2vulkan-runtime::PixelFormatInfo::INVALID PixelFormatInfo assoc-const-value metal2vulkan-runtime::PrivateCount::ALL assoc-const-value metal2vulkan-runtime::PrivateFlag::ALL assoc-const-value metal2vulkan-runtime::Query::ALL [Query::RasterOrderGroups, Query::Depth24Stencil8, Query::ProgrammableSamplePositions, Query::TextureSampleCount, Query::VertexAmplificationCount, Query::RasterizationRateMapLayerCount, Query::MinimumLinearTextureAlignment, Query::SamplerReductionMode, Query::PullModelInterpolation] +assoc-const-value metal2vulkan-runtime::ResourceOptions::BIT_20 1 <<20 assoc-const-value metal2vulkan-runtime::ResourceOptions::PRIVATE_BIT 1 <<24 assoc-const-value metal2vulkan-runtime::StoreActionOptions::CUSTOM_SAMPLE_POSITIONS StoreActionOptions(1) assoc-const-value metal2vulkan-runtime::StoreActionOptions::NONE StoreActionOptions(0) diff --git a/docs/API.md b/docs/API.md index 4f49cea98..037be99ff 100644 --- a/docs/API.md +++ b/docs/API.md @@ -497,6 +497,20 @@ first submission acquires from `VK_QUEUE_FAMILY_FOREIGN_EXT`; a linear image ove runtime's own memory starts `VK_IMAGE_LAYOUT_PREINITIALIZED`, and its move from there keeps its bytes. FAST-PATHS.md's costs and known gaps state what this costs and differs from Metal in. +`MTLResourceOptions` bit 20 (0x100000, `ResourceOptions::BIT_20`) is an option no Metal header +names, whose meaning Metal does not state; programs pass it with a buffer's options (0x100010 for +a `Managed` no-copy buffer). `ResourceOptions::from_raw` takes it, and nothing in the runtime +reads it. Measured on an M3 Max under macOS 27.0.1 and an AMD Radeon Pro 5300M under macOS 26.7.1, +plain and under API validation (`crates/metal2vulkan-conformance/probes/resource_option_bit20.m`), +the runtime following both halves: +- a buffer of a length, of bytes or over the caller's pages (`newBufferWithBytesNoCopy:`), of + every storage mode, is made with it and reports its options without it; so are a heap and a + heap's buffer (the M3 Max; Metal's API validation stops the heap's buffer, "unknown bits + 0x100000"). The runtime makes each as without it; +- a texture descriptor with it ends Metal's process (its suballocator asserts, "Different + MTLResourceOptions used for the same heap index"): the runtime refuses it (`Invalid`, + `Problem::UndefinedValue`), as it refuses bit 24 there. + ## 2. A library `Library::parse` takes a metallib, a bitcode wrapper or a raw AIR module; `Library::from_air`