diff --git a/crates/metal2vulkan-runtime/src/queue.rs b/crates/metal2vulkan-runtime/src/queue.rs index 67bd68d82..0c921a9a4 100644 --- a/crates/metal2vulkan-runtime/src/queue.rs +++ b/crates/metal2vulkan-runtime/src/queue.rs @@ -20,7 +20,7 @@ pub struct CommandQueue { /// The queue itself: what its command buffers and the submit thread hold. pub(crate) struct QueueInner { - max: u32, + recording: Recording, /// The command buffers made and not yet retired, and the lists retired ones left. live: Mutex, /// Signalled when one retires. @@ -46,13 +46,34 @@ pub(crate) struct QueueInner { /// A queue's live command buffers: those made and not yet retired, and the kept-object lists /// of retired ones, empty, with their capacity, each in the allocation it came in -/// (`crate::kept::Objects`): together at most the queue's maximum, since each list is a live -/// command buffer's or one retired since. +/// (`crate::kept::Objects`). Host batches keep only a submission group's worth of idle lists. struct Live { - count: u32, + count: usize, objects: Vec, } +#[derive(Clone, Copy)] +enum Recording { + Metal(u32), + HostBatch, +} + +impl Recording { + fn has_room(self, live: usize) -> bool { + match self { + Self::Metal(max) => live < max as usize, + Self::HostBatch => true, + } + } + + fn recycle(self, idle: usize) -> bool { + match self { + Self::Metal(_) => true, + Self::HostBatch => idle < crate::submit::GROUP, + } + } +} + impl Device { /// `newCommandQueueWithMaxCommandBufferCount:`; `newCommandQueue` is this call with 64. /// @@ -65,10 +86,22 @@ impl Device { if max_command_buffer_count == 0 { return Err(Error::Refused(Refusal::NoCommandBuffers)); } + Ok(self.queue_with_recording(Recording::Metal(max_command_buffer_count))) + } + + /// A private FIFO for host copies prepared together and published after their dependencies + /// have been recorded. Recording must never wait for another member of that transaction to + /// retire. Committed work follows the ordinary submit thread's ordering and GPU pacing. + /// The host owns the lifetime of the batch and the memory held by its recorded copies. + pub fn new_host_batch_queue(&self) -> CommandQueue { + self.queue_with_recording(Recording::HostBatch) + } + + fn queue_with_recording(&self, recording: Recording) -> CommandQueue { let core = self.core(); - Ok(CommandQueue { + CommandQueue { inner: Arc::new(QueueInner { - max: max_command_buffer_count, + recording, live: Mutex::new(Live { count: 0, objects: Vec::new(), @@ -82,7 +115,7 @@ impl Device { core: Arc::downgrade(core), }), device: self.clone(), - }) + } } } @@ -100,7 +133,7 @@ impl CommandQueue { if core.closed() { return Err(Error::Quiesced); } - if live.count < self.inner.max { + if self.inner.recording.has_room(live.count) { break; } if crate::device::on_handler_thread(core) { @@ -205,7 +238,9 @@ impl QueueInner { self.counters.fall(Gauge::CommandBuffers); let mut live = lock(&self.live); live.count -= 1; - live.objects.push(kept); + if self.recording.recycle(live.objects.len()) { + live.objects.push(kept); + } drop(live); self.room.notify_one(); } @@ -221,3 +256,21 @@ pub(crate) fn next_run(expected: Option, nanos: u64) -> u64 { _ => nanos.min(u64::MAX - 1), } } + +#[cfg(test)] +mod recording_tests { + use super::Recording; + + #[test] + fn host_batches_stage_independently_of_the_metal_recording_window() { + let metal = Recording::Metal(2); + assert!(metal.has_room(1)); + for live in [2, 3, 64, 65, 128] { + assert!(!metal.has_room(live)); + assert!(Recording::HostBatch.has_room(live)); + } + let group = crate::submit::GROUP; + assert!(Recording::HostBatch.recycle(group - 1)); + assert!(!Recording::HostBatch.recycle(group)); + } +}