diff --git a/crates/metal2vulkan-harness/src/authored.rs b/crates/metal2vulkan-harness/src/authored.rs index aac9f3dc4..d7e63dfba 100644 --- a/crates/metal2vulkan-harness/src/authored.rs +++ b/crates/metal2vulkan-harness/src/authored.rs @@ -19,7 +19,7 @@ pub struct Workload { } /// The authored workloads. -pub const WORKLOADS: [Workload; 15] = [ +pub const WORKLOADS: [Workload; 16] = [ Workload { name: "bandwidth", plan: bandwidth, @@ -52,6 +52,10 @@ pub const WORKLOADS: [Workload; 15] = [ name: "burst_160", plan: burst_160, }, + Workload { + name: "burst_160_shared", + plan: burst_160_shared, + }, Workload { name: "burst_2000", plan: burst_2000, @@ -401,15 +405,6 @@ fn private(contents: Vec) -> BufferPlan { } } -/// A `Shared` buffer starting with `contents`, written through `contents`. -fn shared(contents: Vec) -> BufferPlan { - BufferPlan { - length: contents.len() as u64, - storage: Storage::Shared, - contents: Some(contents), - } -} - /// A `Private` buffer of `length` bytes whose first run writes before it reads. fn unset(length: u64) -> BufferPlan { BufferPlan { @@ -423,34 +418,47 @@ fn unset(length: u64) -> BufferPlan { /// a frame, two at the 90th percentile, a few dispatches each), in compute alone: a run is one /// command buffer of four `lcg_step` dispatches ([`lcg_frame`]). fn frame() -> Plan { - lcg_frame("frame", 1, 4, None) + lcg_frame("frame", 1, 4, None, Storage::Managed) } /// A burst of small command buffers: a run is 160 command buffers of two `lcg_step` dispatches /// each ([`lcg_frame`]). fn burst_160() -> Plan { - lcg_frame("burst_160", 160, 2, None) + lcg_frame("burst_160", 160, 2, None, Storage::Managed) +} + +/// `burst_160` with its increment in a `Shared` buffer, kept for the record: the recordings bind +/// few small constant arguments from `Shared` memory ([`lcg_frame`]). +fn burst_160_shared() -> Plan { + lcg_frame("burst_160_shared", 160, 2, None, Storage::Shared) } /// The burst a recording commits most: a run is 2,000 command buffers of one compute encoder of /// one `lcg_step` dispatch each ([`lcg_frame`]). fn burst_2000() -> Plan { - lcg_frame("burst_2000", 2000, 1, None) + lcg_frame("burst_2000", 2000, 1, None, Storage::Managed) } /// That burst at five compute encoders a command buffer: a run is 2,000 command buffers of five /// compute encoders of one `lcg_step` dispatch each ([`lcg_frame`]). fn burst_2000x5() -> Plan { - lcg_frame("burst_2000x5", 2000, 5, Some(1)) + lcg_frame("burst_2000x5", 2000, 5, Some(1), Storage::Managed) } /// A run of `command_buffers` command buffers of `per` tiny dispatches each, in compute encoders /// of `per_encoder` dispatches (or one of them all), committed with no wait between and waited /// for once: `lcg_step` over 256 words of `Private` storage, each dispatch setting its bindings, /// since an encoder starts with none. Both are buffers (`setBuffer:offset:atIndex:`): the words, -/// and the increment in a `Shared` buffer written once; the run makes no `setBytes` call -/// (`dispatch_chain` measures those). -fn lcg_frame(name: &str, command_buffers: usize, per: usize, per_encoder: Option) -> Plan { +/// and the increment in a buffer of `storage` written once, a `Managed` one then flushed with +/// `didModifyRange:`, as the recordings bind their small constant arguments; the run makes no +/// `setBytes` call (`dispatch_chain` measures those). +fn lcg_frame( + name: &str, + command_buffers: usize, + per: usize, + per_encoder: Option, + storage: Storage, +) -> Plan { const WORDS: usize = 256; let steps = per * command_buffers; let increment = words(14, 1)[0]; @@ -473,7 +481,11 @@ fn lcg_frame(name: &str, command_buffers: usize, per: usize, per_encoder: Option library: Source::Authored, buffers: vec![ private(word_bytes(&input)), - shared(word_bytes(&[increment, 0, 0, 0])), + BufferPlan { + length: 16, + storage, + contents: Some(word_bytes(&[increment, 0, 0, 0])), + }, ], dispatches: vec![dispatch; steps], expect: vec![Expect { diff --git a/crates/metal2vulkan-harness/src/lib.rs b/crates/metal2vulkan-harness/src/lib.rs index c8d874946..4c3c0f0bd 100644 --- a/crates/metal2vulkan-harness/src/lib.rs +++ b/crates/metal2vulkan-harness/src/lib.rs @@ -131,6 +131,10 @@ pub enum Storage { Shared, /// `MTLStorageModePrivate`: the GPU's alone. Private, + /// `MTLStorageModeManaged`: the CPU writes it through `contents` and calls + /// `didModifyRange:`, which brings the GPU's copy up to date. The new runtime has no such + /// mode; its harness keeps the GPU's copy in a `Private` buffer (`rt`). + Managed, } /// A library's code. @@ -228,6 +232,10 @@ pub trait ComputeDevice: Device { pub trait Buffer { /// `contents`: the CPU address of the buffer's bytes; `None` for `Private` storage. fn contents(&self) -> Option>; + + /// `didModifyRange:` of the `length` bytes at `offset`: the CPU wrote them. Nothing for + /// storage other than `Managed`. + fn did_modify_range(&self, offset: u64, length: u64) -> Result<()>; } /// `MTLLibrary`. diff --git a/crates/metal2vulkan-harness/src/old.rs b/crates/metal2vulkan-harness/src/old.rs index b76fecd3d..dfd1a9814 100644 --- a/crates/metal2vulkan-harness/src/old.rs +++ b/crates/metal2vulkan-harness/src/old.rs @@ -31,6 +31,11 @@ impl crate::Device for runtime::Device { let options = match storage { Storage::Shared => ResourceOptions::shared(), Storage::Private => ResourceOptions::private(), + Storage::Managed => ResourceOptions::new( + runtime::StorageMode::Managed, + runtime::CpuCacheMode::DefaultCache, + runtime::HazardTrackingMode::Default, + ), }; Ok(runtime::Device::new_buffer(self, length, options)?) } @@ -132,6 +137,13 @@ impl crate::Buffer for runtime::Buffer { fn contents(&self) -> Option> { runtime::Buffer::contents(self) } + + fn did_modify_range(&self, offset: u64, length: u64) -> Result<()> { + Ok(runtime::Buffer::did_modify_range( + self, + offset..offset + length, + )?) + } } impl crate::Library for runtime::Library { diff --git a/crates/metal2vulkan-harness/src/rt.rs b/crates/metal2vulkan-harness/src/rt.rs index 92c180829..ea342243a 100644 --- a/crates/metal2vulkan-harness/src/rt.rs +++ b/crates/metal2vulkan-harness/src/rt.rs @@ -9,6 +9,11 @@ //! buffer's memory only then. So a buffer made after one of the same length was dropped takes //! the same range, which the runtime zeroes as `newBufferWithLength:options:` does. //! +//! The new runtime has no `Managed` storage, so a `Managed` buffer is a `Private` one here: the +//! GPU's copy in device memory, which the harness writes as it writes a `Private` buffer, through +//! a `Shared` buffer and its copy kernel, as `didModifyRange:` brings a `Managed` buffer's GPU copy +//! up to date on a Mac with a discrete GPU. It has no CPU copy: its `contents` is `None`. +//! //! Its known gaps and the device's limits (`Error::Unsupported`, `Error::Limit`) are typed //! refusals ([`Refused`]); its other errors are failures. It makes no texture or sampler state //! yet: a plan with either is refused when it is made, so its texture and sampler types have no @@ -234,7 +239,7 @@ impl crate::Device for Device { fn new_buffer(&self, length: u64, storage: Storage) -> Result { let class = match storage { Storage::Shared => rt::MemoryClass::Shared, - Storage::Private => rt::MemoryClass::Private, + Storage::Private | Storage::Managed => rt::MemoryClass::Private, }; let placed = self.heap.borrow_mut().place(class, length)?; let start = placed.range.as_ref().map_or(0, |range| range.start); @@ -448,4 +453,9 @@ impl crate::Buffer for Buffer { fn contents(&self) -> Option> { self.buffer.contents() } + + /// Nothing: a `Managed` buffer has no CPU copy here (the module's docs). + fn did_modify_range(&self, _: u64, _: u64) -> Result<()> { + Ok(()) + } } diff --git a/crates/metal2vulkan-harness/src/run.rs b/crates/metal2vulkan-harness/src/run.rs index d2e8cdc5d..12e48bd55 100644 --- a/crates/metal2vulkan-harness/src/run.rs +++ b/crates/metal2vulkan-harness/src/run.rs @@ -420,16 +420,22 @@ impl Harness { } /// Every buffer's starting contents: a `Shared` one's written through `contents`, a - /// `Private` one's copied in from a `Shared` buffer, all in one command buffer, waited for. + /// `Managed` one's too, then `didModifyRange:` over them, and a `Private` one's, or a + /// `Managed` one's without a CPU copy, copied in from a `Shared` buffer, all in one command + /// buffer, waited for. fn fill(&self, plans: &[BufferPlan], buffers: &[D::Buffer]) -> Result<()> { let mut staged = Vec::new(); for (plan, buffer) in plans.iter().zip(buffers) { let Some(contents) = &plan.contents else { continue; }; - match plan.storage { - Storage::Shared => write(buffer, contents)?, - Storage::Private => { + match (plan.storage, buffer.contents()) { + (Storage::Shared, _) => write(buffer, contents)?, + (Storage::Managed, Some(_)) => { + write(buffer, contents)?; + buffer.did_modify_range(0, plan.length)?; + } + (Storage::Private | Storage::Managed, _) => { let stage = self.device.new_buffer(plan.length, Storage::Shared)?; write(&stage, contents)?; staged.push((stage, buffer, plan.length)); diff --git a/crates/metal2vulkan-harness/src/tests.rs b/crates/metal2vulkan-harness/src/tests.rs index 1fadb6c68..6e65fdac1 100644 --- a/crates/metal2vulkan-harness/src/tests.rs +++ b/crates/metal2vulkan-harness/src/tests.rs @@ -393,6 +393,10 @@ impl Buffer for HostBuffer { fn contents(&self) -> Option> { NonNull::new(self.0.as_ptr().cast_mut().cast()) } + + fn did_modify_range(&self, _: u64, _: u64) -> Result<()> { + Ok(()) + } } #[test]