diff --git a/crates/metal2vulkan-harness/src/rt.rs b/crates/metal2vulkan-harness/src/rt.rs index 636193813..c464d78bd 100644 --- a/crates/metal2vulkan-harness/src/rt.rs +++ b/crates/metal2vulkan-harness/src/rt.rs @@ -222,7 +222,7 @@ struct Placed { impl Heap { /// A place for a buffer of `length` bytes in `class` memory. fn place(&mut self, class: rt::MemoryClass, length: u64) -> Result { - if length > rt::BLOCK_LEN { + if length > class.block_len() { let block = self.device.take_block(class, length).map_err(error)?.id; return Ok(Placed { block, range: None }); } diff --git a/crates/metal2vulkan-rt/README.md b/crates/metal2vulkan-rt/README.md index a1d6ad92a..aed87e3c7 100644 --- a/crates/metal2vulkan-rt/README.md +++ b/crates/metal2vulkan-rt/README.md @@ -85,13 +85,19 @@ in its memory. block's life) memory, as a `BlockInfo`: its number, class, length, device address and, for Shared, its host address. One `VkBuffer` spans each block, with every buffer usage and a device address. Nothing in a block is exportable. -- Up to `BLOCK_LEN` (256 MiB) a block is pooled. `Device::new` makes one block of each class - before it returns, and the block-maker thread keeps one of each class ready after that, made off - every caller's path, so a take waits only when none is ready. No block is so made beside the - consumer's first calls: on an RTX 3090 (NVIDIA's Linux driver), a pipeline state's creation right - after `Device::new` waited 47 to 61 ms behind the allocation of the first Shared block's memory. Past - `BLOCK_LEN` a block is dedicated: the length rounded up to 64 KiB, made in the call, up to the - device's `maxMemoryAllocationSize` and `maxBufferSize` (`Limit::BlockLen` past them). +- Up to its class's `MemoryClass::block_len`, 16 MiB of Shared memory and 64 MiB of Private on + every device, a block is pooled. `Device::new` makes one block of each class before it returns, + and the block-maker thread keeps one of each class ready after that, made off every caller's + path, so a take waits only when none is ready. No block is so made beside the consumer's first + calls: on an RTX 3090 (NVIDIA's Linux driver), a pipeline state's creation right after + `Device::new` waited 47 to 61 ms behind the allocation of a first 256 MiB Shared block's memory. + A driver may hold the device's other calls for the whole of a block's allocation, longer for + more bytes, so the lengths are short: there a creation beside a Shared block's making took 6 to + 8 ms at 16 MiB (40 to 45 ms at 256 MiB), and 3.4 to 4.6 ms beside a 64 MiB Private block's. 16 + MiB is the largest piece a guest's allocator takes for its buffers; at 64 MiB a texture or a + Managed buffer's device copy of 32 MiB takes a pooled Private block. Past its class's length a + block is dedicated: the length rounded up to 64 KiB, made in the call, up to the device's + `maxMemoryAllocationSize` and `maxBufferSize` (`Limit::BlockLen` past them). - A Private allocation takes the device-local Private type. Where that allocation itself fails out of device memory (never a budget poll), it takes the next memory type the resource allows, in the device's order: `BlockInfo::device_local` is false for such a block, and @@ -133,7 +139,7 @@ in its memory. which `contents` answers. The device's memory-type query picks its form. Where the Shared type is device-local, the buffer is its bytes, as a Shared buffer is, and `did_modify_range` does nothing. Elsewhere shaders reach a device copy at `gpu_address`: a place in a copy block, a - Private block of the pool (a dedicated one past `BLOCK_LEN`) that holds device copies alone. Its + Private block of the pool (a dedicated one past 64 MiB) that holds device copies alone. Its first `copied` bytes are copied from the consumer's and the rest zeroed, in the upload. `Buffer::did_modify_range(start, length)` marks a range changed (`Refusal::ModifiedRange` past the buffer): the upload copies it into the device copy as it goes, ahead of every command buffer @@ -193,13 +199,13 @@ made, with no lock, and never rewritten for layouts. are. The image is placed in a texture block: a Private block the device takes from its pool, at the first place with room at the image's alignment, so a texture makes no allocation. It has an allocation of its own only where the device requires one for it (`requiresDedicatedAllocation`; - preferring one is no reason), where it is larger than a block, or where the blocks' memory type - does not serve it. A place is free again once the image is freed; one empty texture block is kept, - and another that empties goes back to the pool. `index` is given exactly where shaders reach the - texture, shader-read or shader-write usage (`Refusal::TextureIndex` otherwise), from a live grant - of the class the usage picks: `IndexClass::StorageTexture` with shader-write usage, else - `IndexClass::Texture`. `TextureUsage` with no bits is Metal's unknown usage: shader read and - write, render target and pixel-format view. + preferring one is no reason), where it is larger than a Private block (64 MiB), or where the + blocks' memory type does not serve it. A place is free again once the image is freed; one empty + texture block is kept, and another that empties goes back to the pool. `index` is given exactly + where shaders reach the texture, shader-read or shader-write usage (`Refusal::TextureIndex` + otherwise), from a live grant of the class the usage picks: `IndexClass::StorageTexture` with + shader-write usage, else `IndexClass::Texture`. `TextureUsage` with no bits is Metal's unknown + usage: shader read and write, render target and pixel-format view. - The image's usage is the descriptor's plus transfer source and destination. Its home layout is general where it has a storage element, shader-read-only where shaders only read it, and the attachment layout where only render passes use it. Its elements of set 1 are written before the diff --git a/crates/metal2vulkan-rt/src/device.rs b/crates/metal2vulkan-rt/src/device.rs index 92af3b1e7..bd994053b 100644 --- a/crates/metal2vulkan-rt/src/device.rs +++ b/crates/metal2vulkan-rt/src/device.rs @@ -12,9 +12,7 @@ use crate::floor; use crate::hazard::Ids; use crate::instance::{Instance, PhysicalDevice}; use crate::kernel_map::KernelMap; -use crate::memory::{ - self, Block, BlockId, BlockInfo, BlockMappings, Memory, MemoryClass, Pool, BLOCK_LEN, -}; +use crate::memory::{self, Block, BlockId, BlockInfo, BlockMappings, Memory, MemoryClass, Pool}; use crate::pipeline::PUSH_BYTES; use crate::places::Places; use crate::reaper::{self, Job, Reaper}; @@ -552,9 +550,11 @@ impl Device { /// No Metal counterpart: takes a block of `class` memory at least `len` bytes long, for the /// consumer to place buffers in. /// - /// For `len` up to [`BLOCK_LEN`] it is a pooled block. The device's making makes one of each - /// class, and the block-maker thread makes the next ones ahead of need, off every caller's - /// path, so this call waits only when no block of the class is ready. Past [`BLOCK_LEN`] it is a dedicated block, made in this call. + /// For `len` up to the class's [`MemoryClass::block_len`] (16 MiB Shared, 64 MiB Private) it + /// is a pooled block of that length. The device's making makes one of each class, and the + /// block-maker thread makes the next ones ahead of need, off every caller's path, so this + /// call waits only when no block of the class is ready. Past it, it is a dedicated block, + /// made in this call. /// /// Every block reads zero, as Metal's new buffers do, whether new or taken again: a Shared /// block is zeroed through its mapping, which also makes each page resident before the block @@ -1016,13 +1016,13 @@ impl Core { self.device_local & (1 << self.memory_types.shared) != 0 } - /// A block of `class` for `len` bytes: a pooled one up to [`BLOCK_LEN`], else a dedicated one + /// A block of `class` for `len` bytes: a pooled one up to the class's block length, else a dedicated one /// of `len` rounded up to 64 KiB, made in the call. /// /// Err: what [`Pool::take`] and [`memory::make`] return; /// [`Limit::BlockLen`](crate::Limit::BlockLen) past the device's longest block. pub(crate) fn take_memory(&self, class: MemoryClass, len: u64) -> Result { - if len <= BLOCK_LEN { + if len <= class.block_len() { self.pool.take(class) } else { let len = memory::dedicated_len(len, self.max_block_len)?; diff --git a/crates/metal2vulkan-rt/src/lib.rs b/crates/metal2vulkan-rt/src/lib.rs index 43427e4ef..2766a5efc 100644 --- a/crates/metal2vulkan-rt/src/lib.rs +++ b/crates/metal2vulkan-rt/src/lib.rs @@ -55,7 +55,7 @@ pub use event::{Event, SharedEvent}; pub use extension::{ConsumerFeature, DeviceExtension}; pub use fence::Fence; pub use instance::{Instance, PhysicalDevice}; -pub use memory::{BlockId, BlockInfo, BlockMappings, MemoryClass, BLOCK_LEN}; +pub use memory::{BlockId, BlockInfo, BlockMappings, MemoryClass}; /// `MTLLibrary`: the translator's, read from AIR or a metallib. /// `MTLSamplerDescriptor`: the translator's. pub use metal2vulkan::bind::SamplerDescriptor; diff --git a/crates/metal2vulkan-rt/src/memory.rs b/crates/metal2vulkan-rt/src/memory.rs index e70be9ecd..ffb7ef209 100644 --- a/crates/metal2vulkan-rt/src/memory.rs +++ b/crates/metal2vulkan-rt/src/memory.rs @@ -10,12 +10,6 @@ use std::collections::HashSet; use std::ptr::NonNull; use std::sync::{Arc, Condvar, Mutex, MutexGuard, PoisonError}; -/// No Metal counterpart: a pooled block's length, 256 MiB. -/// -/// [`Device::take_block`](crate::Device::take_block) gives a pooled block for any length up to -/// this, and a dedicated one past it. -pub const BLOCK_LEN: u64 = 256 << 20; - /// Blocks of each class the device's making makes ([`stock`]) and the block-maker thread keeps /// ready after it. const READY: usize = 1; @@ -40,6 +34,27 @@ pub enum MemoryClass { impl MemoryClass { const ALL: [MemoryClass; 2] = [MemoryClass::Private, MemoryClass::Shared]; + /// No Metal counterpart: the length of the class's pooled block, on every device: 16 MiB of + /// Shared memory, 64 MiB of Private. + /// + /// [`Device::take_block`](crate::Device::take_block) gives a pooled block for any length up + /// to it, and a dedicated one past it. + /// + /// A driver may hold the device's other calls (pipeline creation, submissions, other + /// allocations) for the whole of a block's `vkAllocateMemory`, and the hold grows with the + /// bytes, so the length sets how long one hold lasts. On an RTX 3090 (NVIDIA's Linux + /// driver) a pipeline state's creation beside a Shared block's making took 40 to 45 ms at + /// 256 MiB and 6 to 8 ms at 16 MiB, against 3 to 4 ms settled. Shared blocks are 16 MiB, the + /// largest piece a guest's allocator takes for its buffers. Private memory holds less: 3.4 to + /// 4.6 ms beside a 64 MiB block, at most 1 ms more than beside a 32 MiB one, and at 64 MiB a + /// texture or a Managed buffer's device copy of 32 MiB takes a pooled block. + pub const fn block_len(self) -> u64 { + match self { + MemoryClass::Private => 64 << 20, + MemoryClass::Shared => 16 << 20, + } + } + fn index(self) -> usize { match self { MemoryClass::Private => 0, @@ -73,8 +88,8 @@ pub struct BlockInfo { pub id: BlockId, /// Its storage mode. pub class: MemoryClass, - /// [`BLOCK_LEN`] for a pooled block; for a dedicated one, the length asked for rounded up to - /// 64 KiB. + /// [`MemoryClass::block_len`] for a pooled block; for a dedicated one, the length asked for + /// rounded up to 64 KiB. pub len: u64, /// `gpuAddress` of the block's first byte: its `VkBuffer`'s device address. pub gpu_address: u64, @@ -364,13 +379,14 @@ fn number(state: &mut State) -> Result { /// class, so the consumer's first block of each class is ready as the device is, and no block is /// made beside the consumer's first calls. A driver may hold the device's other calls while it /// allocates a block's memory: on an RTX 3090 (NVIDIA's Linux driver), a pipeline state's creation -/// right after the device's making waited 47 to 61 ms behind the block-maker thread's first Shared -/// block, against 2.4 to 3.1 ms once the block was made here. A class whose block cannot be made +/// right after the device's making waited 47 to 61 ms behind the block-maker thread's first 256 MiB +/// Shared block, against 2.4 to 3.1 ms once the block was made here. A class whose block cannot be made /// keeps why for its next taker, as the block-maker thread does. pub(crate) fn stock(core: &Core) { for class in MemoryClass::ALL { for _ in 0..READY { - let made = (core.pool.number()).and_then(|id| make(core, class, BLOCK_LEN, id, true)); + let made = + (core.pool.number()).and_then(|id| make(core, class, class.block_len(), id, true)); let mut state = core.pool.lock(); match made { Ok(memory) => state.idle[class.index()].push(memory), @@ -419,7 +435,7 @@ pub(crate) fn run(core: Arc) { if let Some(class) = wanted { let id = number(&mut state); drop(state); - let made = id.and_then(|id| make(&core, class, BLOCK_LEN, id, true)); + let made = id.and_then(|id| make(&core, class, class.block_len(), id, true)); state = pool.lock(); match made { Ok(memory) => state.idle[class.index()].push(memory), diff --git a/crates/metal2vulkan-rt/src/tests/command.rs b/crates/metal2vulkan-rt/src/tests/command.rs index 92ef9a177..f343ffbe6 100644 --- a/crates/metal2vulkan-rt/src/tests/command.rs +++ b/crates/metal2vulkan-rt/src/tests/command.rs @@ -11,7 +11,7 @@ pub(crate) mod on_device { use crate::{ host_time, BlockId, BlockMappings, Buffer, CommandBuffer, CommandBufferError, CommandBufferStatus, CommandQueue, Device, DeviceOptions, DispatchType, Error, Gauges, - Instance, MemoryClass, Refusal, SharedEvent, BLOCK_LEN, + Instance, MemoryClass, Refusal, SharedEvent, }; use ash::vk; use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering}; @@ -674,7 +674,7 @@ pub(crate) mod on_device { device.core().pool.idle(MemoryClass::Private) > 0 }); device - .new_buffer(block.id, 0, BLOCK_LEN, false) + .new_buffer(block.id, 0, MemoryClass::Private.block_len(), false) .expect("a buffer") } @@ -863,7 +863,15 @@ pub(crate) mod on_device { command_buffer .record_for_test(&[&work], |device, command| { // SAFETY: as `busy`'s. - unsafe { device.cmd_fill_buffer(command, target, 0, BLOCK_LEN, word) }; + unsafe { + device.cmd_fill_buffer( + command, + target, + 0, + MemoryClass::Private.block_len(), + word, + ) + }; }) .expect("a fill"); log.handlers(label, &command_buffer); @@ -1402,7 +1410,7 @@ pub(crate) mod on_device { // Blocks stay taken; a reader thread reads one; the block-maker zeroes released ones; a // command buffer is held. let block = device - .take_block(MemoryClass::Shared, BLOCK_LEN) + .take_block(MemoryClass::Shared, MemoryClass::Shared.block_len()) .expect("a Shared block"); let released: Vec<_> = (0..3) .map(|_| device.take_block(MemoryClass::Shared, 1).expect("a block")) @@ -1410,7 +1418,7 @@ pub(crate) mod on_device { let address = block.contents.expect("contents").as_ptr() as usize; *lock(&READER) = Some(thread::spawn(move || { while READING.load(Ordering::Acquire) { - for offset in (0..BLOCK_LEN as usize).step_by(4096) { + for offset in (0..MemoryClass::Shared.block_len() as usize).step_by(4096) { // SAFETY: the block stays taken and mapped until the process ends. unsafe { ((address + offset) as *const u8).read_volatile() }; } diff --git a/crates/metal2vulkan-rt/src/tests/memory.rs b/crates/metal2vulkan-rt/src/tests/memory.rs index 443d7def0..5999633ed 100644 --- a/crates/metal2vulkan-rt/src/tests/memory.rs +++ b/crates/metal2vulkan-rt/src/tests/memory.rs @@ -70,7 +70,7 @@ mod on_device { use crate::tests::devices_of_the_cfgs_kind; use crate::{ BlockId, BlockInfo, BlockMappings, Buffer, CommandBufferStatus, Device, DeviceOptions, - Error, Gauges, Instance, Limit, MemoryClass, Refusal, BLOCK_LEN, MAX_BUFFER_LENGTH, + Error, Gauges, Instance, Limit, MemoryClass, Refusal, MAX_BUFFER_LENGTH, }; use ash::vk; use std::collections::HashMap; @@ -239,10 +239,11 @@ mod on_device { }); } + /// Copies `size` bytes of `from` at `offset` to the start of `to`. fn copy(&self, from: vk::Buffer, to: vk::Buffer, offset: u64, size: u64) { let region = [vk::BufferCopy { src_offset: offset, - dst_offset: offset, + dst_offset: 0, size, }]; // SAFETY: recorded in `run`; the range is in both buffers. @@ -252,6 +253,22 @@ mod on_device { } } + impl Gpu { + /// Whether every byte of the Private block `private` reads zero, copied a Shared block's + /// length at a time to the start of the pooled Shared block `shared`, whose bytes are + /// `host`. + fn reads_zero(&self, private: vk::Buffer, shared: vk::Buffer, host: &mut [u8]) -> bool { + let step = MemoryClass::Shared.block_len(); + (0..MemoryClass::Private.block_len()) + .step_by(step as usize) + .all(|offset| { + host.fill(0xFF); + self.copy(private, shared, offset, step); + host.iter().all(|&byte| byte == 0) + }) + } + } + impl Drop for Gpu { fn drop(&mut self) { let raw = self.device.raw(); @@ -372,7 +389,10 @@ mod on_device { let shared = device .take_block(MemoryClass::Shared, 1) .expect("a Shared block"); - assert_eq!((shared.class, shared.len), (MemoryClass::Shared, BLOCK_LEN)); + assert_eq!( + (shared.class, shared.len), + (MemoryClass::Shared, MemoryClass::Shared.block_len()) + ); assert_ne!(shared.gpu_address, 0, "{name}: a Shared block's address"); let host = bytes(shared); assert!( @@ -382,23 +402,20 @@ mod on_device { // A new Private block reads zero, copied into the Shared block. let private = device - .take_block(MemoryClass::Private, BLOCK_LEN) + .take_block(MemoryClass::Private, MemoryClass::Private.block_len()) .expect("a Private block"); assert_eq!( (private.class, private.len, private.contents), - (MemoryClass::Private, BLOCK_LEN, None) + (MemoryClass::Private, MemoryClass::Private.block_len(), None) ); assert_ne!(private.gpu_address, 0, "{name}: a Private block's address"); assert_ne!(private.id, shared.id); - host.fill(0xFF); - gpu.copy( - vk_buffer(&device, private.id), - vk_buffer(&device, shared.id), - 0, - BLOCK_LEN, - ); assert!( - host.iter().all(|&byte| byte == 0), + gpu.reads_zero( + vk_buffer(&device, private.id), + vk_buffer(&device, shared.id), + host + ), "{name}: a new Private block" ); @@ -417,7 +434,7 @@ mod on_device { (shared.id, 4096, 256) ); let whole = device - .new_buffer(private.id, 0, BLOCK_LEN, false) + .new_buffer(private.id, 0, MemoryClass::Private.block_len(), false) .expect("a Private buffer"); assert_eq!( (whole.gpu_address(), whole.contents()), @@ -498,13 +515,14 @@ mod on_device { other => panic!("{name}: a buffer of {length}: {:?}", other.err()), } } - for (offset, length) in [(BLOCK_LEN - 4, 8), (u64::MAX, 2), (BLOCK_LEN, 1)] { + let shared_len = MemoryClass::Shared.block_len(); + for (offset, length) in [(shared_len - 4, 8), (u64::MAX, 2), (shared_len, 1)] { match device.new_buffer(shared.id, offset, length, false) { Err(Error::Refused(Refusal::BufferPastBlock { offset: o, length: l, block_len, - })) => assert_eq!((o, l, block_len), (offset, length, BLOCK_LEN)), + })) => assert_eq!((o, l, block_len), (offset, length, shared_len)), other => panic!("{name}: {offset}+{length}: {:?}", other.err()), } } @@ -513,11 +531,11 @@ mod on_device { Err(Error::Limit(Limit::BlockLen { len: u64::MAX, .. })) )); - // A dedicated block: past BLOCK_LEN, rounded up to 64 KiB, zeroed. + // A dedicated block: past the class's block length, rounded up to 64 KiB, zeroed. let dedicated = device - .take_block(MemoryClass::Shared, BLOCK_LEN + 1) + .take_block(MemoryClass::Shared, shared_len + 1) .expect("a dedicated block"); - assert_eq!(dedicated.len, BLOCK_LEN + (64 << 10)); + assert_eq!(dedicated.len, shared_len + (64 << 10)); let dedicated_host = bytes(dedicated); assert!( dedicated_host.iter().all(|&byte| byte == 0), @@ -634,7 +652,7 @@ mod on_device { let taken: Vec = (0..3) .map(|_| { device - .take_block(MemoryClass::Shared, BLOCK_LEN) + .take_block(MemoryClass::Shared, MemoryClass::Shared.block_len()) .expect("a block") }) .collect(); @@ -692,7 +710,12 @@ mod on_device { .take_block(MemoryClass::Private, 1) .expect("a Private block"); handed.push(private.id); - gpu.fill(vk_buffer(&device, private.id), 0, BLOCK_LEN, u32::MAX); + gpu.fill( + vk_buffer(&device, private.id), + 0, + MemoryClass::Private.block_len(), + u32::MAX, + ); device.release_block(private.id).expect("a release"); // The pool zeroes and keeps it before it makes anything, so it is one of the next two. let next: Vec = (0..2) @@ -704,15 +727,12 @@ mod on_device { .find(|block| block.id == private.id) .expect("the released Private block among the next two"); assert_eq!(back.gpu_address, private.gpu_address); - host.fill(0xFF); - gpu.copy( - vk_buffer(&device, private.id), - vk_buffer(&device, again.id), - 0, - BLOCK_LEN, - ); assert!( - host.iter().all(|&byte| byte == 0), + gpu.reads_zero( + vk_buffer(&device, private.id), + vk_buffer(&device, again.id), + host + ), "{name}: a Private block taken again" ); diff --git a/crates/metal2vulkan-rt/src/tests/places.rs b/crates/metal2vulkan-rt/src/tests/places.rs index 32854e153..63b8b87cb 100644 --- a/crates/metal2vulkan-rt/src/tests/places.rs +++ b/crates/metal2vulkan-rt/src/tests/places.rs @@ -3,7 +3,8 @@ //! blocks of the pool with no allocation of their own: a block's worth of 1 MiB textures takes //! one block and then a second, releasing them gives one back to the pool and keeps the other, //! textures made and released in that block allocate nothing, and every gauge reads zero after -//! teardown. +//! teardown; pooled blocks are 16 MiB Shared and 64 MiB Private, and a texture of 32 MiB takes a +//! place in one. use crate::places::Ranges; @@ -90,7 +91,7 @@ mod on_device { use crate::tests::devices_of_the_cfgs_kind; use crate::{ Device, DeviceOptions, Gauges, IndexClass, Instance, MemoryClass, PixelFormat, - TextureDescriptor, TextureType, BLOCK_LEN, + TextureDescriptor, TextureType, }; use std::thread; use std::time::{Duration, Instant}; @@ -120,7 +121,7 @@ mod on_device { }); let base = counters.gauges().blocks; let made = counters.metrics().blocks_made; - let most = 2 * BLOCK_LEN / MIB; + let most = 2 * MemoryClass::Private.block_len() / MIB; let grant = device .grant_indices(IndexClass::Texture, 4096, most as u32) .unwrap_or_else(|error| panic!("{name}: {error}")); @@ -143,7 +144,7 @@ mod on_device { } let in_first = textures.len() as u64 - 1; assert!( - in_first > BLOCK_LEN / MIB / 2, + in_first > MemoryClass::Private.block_len() / MIB / 2, "{name}: the first block held {in_first} textures of 1 MiB" ); // Each block the pool's ready one, which the pool made again. @@ -196,4 +197,58 @@ mod on_device { ); } } + + /// Pooled blocks are 16 MiB of Shared memory and 64 MiB of Private, on every device: a take + /// up to its class's length is a pooled block of that length, one past it a dedicated block, + /// and a texture of 32 MiB is placed in a texture block, with no allocation of its own. + #[test] + fn every_device_pools_16_mib_shared_and_64_mib_private_blocks() { + const MIB: u64 = 1 << 20; + let instance = Instance::new(&[]).expect("an instance at the floor"); + let devices = devices_of_the_cfgs_kind(&instance); + assert!(!devices.is_empty(), "no device of this cfg's kind"); + for (name, physical) in &devices { + let device = Device::new(physical, DeviceOptions::default()) + .unwrap_or_else(|error| panic!("{name}: {error}")); + let core = device.core(); + let counters = device.counters(); + for (class, len) in [ + (MemoryClass::Shared, 16 * MIB), + (MemoryClass::Private, 64 * MIB), + ] { + assert_eq!(class.block_len(), len, "{name}: {class:?}"); + let pooled = (device.take_block(class, len)) + .unwrap_or_else(|error| panic!("{name}: a pooled {class:?} block: {error}")); + assert_eq!(pooled.len, len, "{name}: a pooled {class:?} block"); + let dedicated = (device.take_block(class, len + 1)) + .unwrap_or_else(|error| panic!("{name}: a dedicated {class:?} block: {error}")); + assert_eq!( + dedicated.len, + len + (64 << 10), + "{name}: a dedicated {class:?} block" + ); + for block in [pooled, dedicated] { + (device.release_block(block.id)) + .unwrap_or_else(|error| panic!("{name}: a release: {error}")); + } + } + let grant = device + .grant_indices(IndexClass::Texture, 4096, 1) + .unwrap_or_else(|error| panic!("{name}: {error}")); + let d = + TextureDescriptor::new(TextureType::Type2D, PixelFormat::RGBA8Unorm, 4096, 2048); + let texture = (device.new_texture(&d, Some(4096))) + .unwrap_or_else(|error| panic!("{name}: a texture of 32 MiB: {error}")); + assert_eq!( + core.places.blocks(), + 1, + "{name}: a texture of 32 MiB in a texture block" + ); + drop(texture); + wait_until("the texture freed", || counters.gauges().textures == 0); + device + .release_indices(grant) + .unwrap_or_else(|error| panic!("{name}: {error}")); + } + } } diff --git a/crates/metal2vulkan-rt/src/tests/spill.rs b/crates/metal2vulkan-rt/src/tests/spill.rs index c401f76fa..aa594cd99 100644 --- a/crates/metal2vulkan-rt/src/tests/spill.rs +++ b/crates/metal2vulkan-rt/src/tests/spill.rs @@ -10,9 +10,7 @@ #[cfg(any(m2v_gpu, m2v_cpu_icd))] mod on_device { use crate::tests::command::on_device::each_device; - use crate::{ - MemoryClass, PixelFormat, TextureDescriptor, TextureType, TextureUsage, BLOCK_LEN, - }; + use crate::{MemoryClass, PixelFormat, TextureDescriptor, TextureType, TextureUsage}; use std::sync::atomic::Ordering::Relaxed; use std::thread; use std::time::{Duration, Instant}; @@ -37,7 +35,8 @@ mod on_device { // A dedicated Private block, made in the call, whose device-local allocation fails. core.fail_private.store(1, Relaxed); - let spilled = device.take_block(MemoryClass::Private, BLOCK_LEN + 1); + let spilled = + device.take_block(MemoryClass::Private, MemoryClass::Private.block_len() + 1); assert_eq!( core.fail_private.load(Relaxed), 0, @@ -70,7 +69,7 @@ mod on_device { assert!(plain.device_local, "{name}: the ready block"); wait_until("the pool's spilled block", || { core.pool.idle(MemoryClass::Private) == 1 - && counters.gauges().spilled_bytes >= BLOCK_LEN + && counters.gauges().spilled_bytes >= MemoryClass::Private.block_len() }); // A texture takes no texture block in it. diff --git a/crates/metal2vulkan-rt/src/tests/upload.rs b/crates/metal2vulkan-rt/src/tests/upload.rs index 901205fc1..f283c6865 100644 --- a/crates/metal2vulkan-rt/src/tests/upload.rs +++ b/crates/metal2vulkan-rt/src/tests/upload.rs @@ -21,7 +21,7 @@ mod on_device { use crate::tests::devices_of_the_cfgs_kind; use crate::{ Device, DeviceOptions, Gauges, IndexClass, Instance, MemoryClass, PixelFormat, Region, - StorageMode, TextureDescriptor, TextureType, BLOCK_LEN, + StorageMode, TextureDescriptor, TextureType, }; use std::thread; use std::time::{Duration, Instant}; @@ -209,7 +209,7 @@ mod on_device { // A Private block released after a zero fill: the next fill sends the upload, and the // block comes back to the pool. let released = device - .take_block(MemoryClass::Private, BLOCK_LEN) + .take_block(MemoryClass::Private, MemoryClass::Private.block_len()) .unwrap_or_else(|error| panic!("{name}: {error}")); let buffer = device .new_buffer(released.id, 0, 256, true) @@ -219,9 +219,12 @@ mod on_device { .release_block(released.id) .unwrap_or_else(|error| panic!("{name}: {error}")); let (_, bytes, _) = core.work.pending(); - assert!(bytes >= BLOCK_LEN, "{name}: the released block is counted"); + assert!( + bytes >= MemoryClass::Private.block_len(), + "{name}: the released block is counted" + ); let fills = device - .take_block(MemoryClass::Private, BLOCK_LEN) + .take_block(MemoryClass::Private, MemoryClass::Private.block_len()) .unwrap_or_else(|error| panic!("{name}: {error}")); let buffer = device .new_buffer(fills.id, 0, 256, true) diff --git a/crates/metal2vulkan-rt/src/texture.rs b/crates/metal2vulkan-rt/src/texture.rs index ca644b99a..679f3116a 100644 --- a/crates/metal2vulkan-rt/src/texture.rs +++ b/crates/metal2vulkan-rt/src/texture.rs @@ -26,7 +26,7 @@ use crate::buffer::Buffer; use crate::counters::Gauge; use crate::device::{lock, Core, Device}; use crate::error::{Error, Gap, Limit, Refusal, TextureViewKind}; -use crate::memory::{self, Block, BlockId, MemoryClass, BLOCK_LEN}; +use crate::memory::{self, Block, BlockId, MemoryClass}; use crate::places::Place; use crate::reaper::{Free, Job}; use crate::staging::Stage; @@ -1434,7 +1434,7 @@ impl Made { // A place in a texture block where the image takes the blocks' memory type and the device // does not require an allocation of its own for it (preferring one is no reason). let placed = dedicated.requires_dedicated_allocation == vk::FALSE - && requirements.size <= BLOCK_LEN + && requirements.size <= MemoryClass::Private.block_len() && bits & (1 << core.memory_type(MemoryClass::Private)) != 0; // Where the device-local memory has no texture block left, memory of its own. let pooled = match placed && matches!(bind, Bind::Pooled) { diff --git a/crates/metal2vulkan-validation/src/replay/new_runtime/places.rs b/crates/metal2vulkan-validation/src/replay/new_runtime/places.rs index 6544599e5..3bae6ddec 100644 --- a/crates/metal2vulkan-validation/src/replay/new_runtime/places.rs +++ b/crates/metal2vulkan-validation/src/replay/new_runtime/places.rs @@ -1,6 +1,6 @@ //! The places of the stream's own buffers in the blocks the backend takes: each memory class's -//! blocks of `BLOCK_LEN`, first fit at Metal's 256-byte buffer alignment, and a block of its own -//! for a buffer past `BLOCK_LEN`. A buffer's place is free again only once every command buffer +//! pooled blocks (`MemoryClass::block_len`), first fit at Metal's 256-byte buffer alignment, and +//! a block of its own for a buffer past the class's block length. A buffer's place is free again only once every command buffer //! committed before the buffer was released has completed, as Metal keeps a released buffer for //! the work that names it. @@ -49,7 +49,7 @@ impl Places { completed: u64, ) -> Result { self.free_completed(completed)?; - if len > rt::BLOCK_LEN { + if len > class.block_len() { let info = self.device.take_block(class, len)?; return Ok(Place { block: info.id, @@ -71,7 +71,7 @@ impl Places { }); } } - let info = self.device.take_block(class, rt::BLOCK_LEN)?; + let info = self.device.take_block(class, class.block_len())?; let mut free = Vec::new(); free.push(0..info.len); let range = take_from(&mut free, span).expect("a new block holds any pooled place"); diff --git a/crates/metal2vulkan/tests/api_signatures.tsv b/crates/metal2vulkan/tests/api_signatures.tsv index 7609e3c82..73e6c9d17 100644 --- a/crates/metal2vulkan/tests/api_signatures.tsv +++ b/crates/metal2vulkan/tests/api_signatures.tsv @@ -1090,7 +1090,6 @@ const metal2vulkan-air::tensor_ops::DESCRIPTOR_BYTES pub const DESCRIPTOR_BYTES: const metal2vulkan-air::tensor_ops::PREFIX pub const PREFIX: &[u8] const metal2vulkan-air::tensor_ops::RESERVED_INDEX pub const RESERVED_INDEX: u16 const metal2vulkan-air::tensor_ops::SECTION pub const SECTION: &[u8] -const metal2vulkan-rt::BLOCK_LEN pub const BLOCK_LEN: u64 const metal2vulkan-rt::HOST_DOMAIN #[cfg(all(unix, not(target_os = "macos")))] pub const HOST_DOMAIN: vk::TimeDomainKHR const metal2vulkan-rt::HOST_DOMAIN #[cfg(target_os = "macos")] pub const HOST_DOMAIN: vk::TimeDomainKHR const metal2vulkan-rt::HOST_DOMAIN #[cfg(windows)] pub const HOST_DOMAIN: vk::TimeDomainKHR @@ -1194,7 +1193,6 @@ const-value metal2vulkan-air::tensor_ops::DESCRIPTOR_BYTES 20 const-value metal2vulkan-air::tensor_ops::PREFIX b"__tensorops_impl_matmul2d_op_" const-value metal2vulkan-air::tensor_ops::RESERVED_INDEX u16::MAX const-value metal2vulkan-air::tensor_ops::SECTION b"air.externally_defined" -const-value metal2vulkan-rt::BLOCK_LEN 256 <<20 const-value metal2vulkan-rt::HOST_DOMAIN vk::TimeDomainKHR::CLOCK_MONOTONIC const-value metal2vulkan-rt::HOST_DOMAIN vk::TimeDomainKHR::CLOCK_MONOTONIC_RAW const-value metal2vulkan-rt::HOST_DOMAIN vk::TimeDomainKHR::QUERY_PERFORMANCE_COUNTER @@ -9362,6 +9360,7 @@ method metal2vulkan-rt::Library::is_empty pub fn is_empty(&self) -> bool method metal2vulkan-rt::Library::len pub fn len(&self) -> u32 method metal2vulkan-rt::Library::library_type pub fn library_type(&self) -> LibraryType method metal2vulkan-rt::Library::parse pub fn parse(bytes: &[u8]) -> Result +method metal2vulkan-rt::MemoryClass::block_len pub const fn block_len(self) -> u64 method metal2vulkan-rt::PhysicalDevice::check pub fn check(&self) -> Result method metal2vulkan-rt::PhysicalDevice::raw pub fn raw(&self) -> vk::PhysicalDevice method metal2vulkan-rt::PrecisionNote::of_float_operator pub fn of_float_operator(op: AirBin, width: FloatWidth, fmf: FastMath, p: &DeviceProfile, math: EntryMath) -> Option> diff --git a/docs/REPLAY-STREAM.md b/docs/REPLAY-STREAM.md index 910171e7c..bde32fb69 100644 --- a/docs/REPLAY-STREAM.md +++ b/docs/REPLAY-STREAM.md @@ -423,12 +423,13 @@ old runtime (the default) or the new one (`rt`), and writes `DIR/report.txt`. `c copies to and from the guest's RAM (`skipped_blits`). The counts are among its executed counters. A play with any of them above zero is no gate - result: `report.txt` says so on a `skipped` line. It places buffers of their own in pooled - 256 MiB blocks of the runtime (a larger one in a block of its own), a range freed for reuse - once every command buffer committed before its release completed, and gives textures and - sampler states indices from the whole of each index class. Its readback serves Shared and Managed buffers only; a check of anything else - is a refusal. The runtime gives a compute pipeline state no `gpuResourceID`, so each one the - program saw is reported on an `id` line as `none` and counted (`gap_pipeline_ids`). + result: `report.txt` says so on a `skipped` line. It places buffers of their own in the + runtime's pooled blocks (16 MiB Shared, 64 MiB Private; a longer one in a block of its own), a + range freed for reuse once every command buffer committed before its release completed, and + gives textures and sampler states indices from the whole of each index class. Its readback + serves Shared and Managed buffers only; a check of anything else is a refusal. The runtime gives + a compute pipeline state no `gpuResourceID`, so each one the program saw is reported on an `id` + line as `none` and counted (`gap_pipeline_ids`). `report.txt` holds one line per item: `device NAME`, `completed IDS COUNTS`, `frame N COUNTS`, `check ID SHA256 match|differs|unrecorded`, `id ID RECORDED PLAYED` for each object whose runtime