Open source Loom alternative. Beautiful, shareable screen recordings.
Rust appcapcossloommacnextjsnextjs15open-sourceossreactrecordrustscreen-capturescreen-recorderscreenshotsolidjstauritauri-apptypescriptvite
perf(rendering): stop committing empty Metal command buffers
Harsha Vardhand0f7bc032b55
diff --git a/vendor/wgpu-hal/src/metal/command.rs b/vendor/wgpu-hal/src/metal/command.rsindex 6f4f2e289..ad946e55a 100644--- a/vendor/wgpu-hal/src/metal/command.rs+++ b/vendor/wgpu-hal/src/metal/command.rs@@ -29,9 +29,37 @@ impl Default for super::CommandState {}impl super::CommandEncoder {+ // Cap-local patch to the vendored wgpu-hal; reapply it whenever vendor/wgpu-hal is replaced.+ // wgpu-core opens a HAL command buffer for every pass, plus one for the barriers it+ // records ahead of it. Barriers are no-ops on Metal, so the MTLCommandBuffer is only+ // created once something is actually encoded and empty ones are never committed+ // (https://github.com/gfx-rs/wgpu/issues/5738). begin_debug_marker still creates the+ // buffer so a debug group pushed before the first pass is popped from the same buffer.+ fn ensure_raw_cmd_buf(&mut self) {+ if self.raw_cmd_buf.is_some() {+ return;+ }+ let queue = &self.raw_queue.lock();+ let retain_references = self.shared.settings.retain_command_buffer_references;+ let label = self.pending_label.as_deref();+ let raw = objc::rc::autoreleasepool(move || {+ let cmd_buf_ref = if retain_references {+ queue.new_command_buffer()+ } else {+ queue.new_command_buffer_with_unretained_references()+ };+ if let Some(label) = label {+ cmd_buf_ref.set_label(label);+ }+ cmd_buf_ref.to_owned()+ });+ self.raw_cmd_buf = Some(raw);+ }+fn enter_blit(&mut self) -> &metal::BlitCommandEncoderRef {if self.state.blit.is_none() {debug_assert!(self.state.render.is_none() && self.state.compute.is_none());+ self.ensure_raw_cmd_buf();let cmd_buf = self.raw_cmd_buf.as_ref().unwrap();// Take care of pending timer queries.@@ -187,22 +215,8 @@ impl crate::CommandEncoder for super::CommandEncoder {type A = super::Api;unsafe fn begin_encoding(&mut self, label: crate::Label) -> Result<(), crate::DeviceError> {- let queue = &self.raw_queue.lock();- let retain_references = self.shared.settings.retain_command_buffer_references;- let raw = objc::rc::autoreleasepool(move || {- let cmd_buf_ref = if retain_references {- queue.new_command_buffer()- } else {- queue.new_command_buffer_with_unretained_references()- };- if let Some(label) = label {- cmd_buf_ref.set_label(label);- }- cmd_buf_ref.to_owned()- });-- self.raw_cmd_buf = Some(raw);-+ self.raw_cmd_buf = None;+ self.pending_label = label.map(|label| label.to_owned());Ok(())}@@ -217,6 +231,7 @@ impl crate::CommandEncoder for super::CommandEncoder {encoder.end_encoding();}self.raw_cmd_buf = None;+ self.pending_label = None;}unsafe fn end_encoding(&mut self) -> Result<super::CommandBuffer, crate::DeviceError> {@@ -232,7 +247,7 @@ impl crate::CommandEncoder for super::CommandEncoder {debug_assert!(self.state.pending_timer_queries.is_empty());Ok(super::CommandBuffer {- raw: self.raw_cmd_buf.take().unwrap(),+ raw: self.raw_cmd_buf.take(),})}@@ -519,6 +534,7 @@ impl crate::CommandEncoder for super::CommandEncoder {desc: &crate::RenderPassDescriptor<super::QuerySet, super::TextureView>,) {self.begin_pass();+ self.ensure_raw_cmd_buf();self.state.index = None;assert!(self.state.blit.is_none());@@ -883,8 +899,11 @@ impl crate::CommandEncoder for super::CommandEncoder {unsafe fn begin_debug_marker(&mut self, group_label: &str) {if let Some(encoder) = self.active_encoder() {encoder.push_debug_group(group_label);- } else if let Some(ref buf) = self.raw_cmd_buf {- buf.push_debug_group(group_label);+ } else {+ self.ensure_raw_cmd_buf();+ if let Some(buf) = &self.raw_cmd_buf {+ buf.push_debug_group(group_label);+ }}}unsafe fn end_debug_marker(&mut self) {@@ -1193,6 +1212,7 @@ impl crate::CommandEncoder for super::CommandEncoder {debug_assert!(self.state.compute.is_none());debug_assert!(self.state.render.is_none());+ self.ensure_raw_cmd_buf();let raw = self.raw_cmd_buf.as_ref().unwrap();objc::rc::autoreleasepool(|| {diff --git a/vendor/wgpu-hal/src/metal/device.rs b/vendor/wgpu-hal/src/metal/device.rsindex bfe3b8e7b..97debf361 100644--- a/vendor/wgpu-hal/src/metal/device.rs+++ b/vendor/wgpu-hal/src/metal/device.rs@@ -609,6 +609,7 @@ impl crate::Device for super::Device {shared: Arc::clone(&self.shared),raw_queue: Arc::clone(&desc.queue.raw),raw_cmd_buf: None,+ pending_label: None,state: super::CommandState::default(),temp: super::Temp::default(),counters: Arc::clone(&self.counters),diff --git a/vendor/wgpu-hal/src/metal/mod.rs b/vendor/wgpu-hal/src/metal/mod.rsindex 08581736e..fffa089b3 100644--- a/vendor/wgpu-hal/src/metal/mod.rs+++ b/vendor/wgpu-hal/src/metal/mod.rs@@ -428,8 +428,12 @@ impl crate::Queue for Queue {}).copy();- let raw = match command_buffers.last() {- Some(&cmd_buf) => cmd_buf.raw.to_owned(),+ let last_encoded = command_buffers+ .iter()+ .rev()+ .find_map(|cmd_buf| cmd_buf.raw.as_ref());+ let raw = match last_encoded {+ Some(raw) => raw.to_owned(),None => {let queue = self.raw.lock();queue@@ -449,14 +453,17 @@ impl crate::Queue for Queue {raw.encode_signal_event(shared_event, signal_value);}// only return an extra one if it's extra- match command_buffers.last() {+ match last_encoded {Some(_) => None,None => Some(raw),}};- for cmd_buffer in command_buffers {- cmd_buffer.raw.commit();+ for raw in command_buffers+ .iter()+ .filter_map(|cmd_buf| cmd_buf.raw.as_ref())+ {+ raw.commit();}if let Some(raw) = extra_command_buffer {@@ -980,6 +987,7 @@ pub struct CommandEncoder {shared: Arc<AdapterShared>,raw_queue: Arc<Mutex<metal::CommandQueue>>,raw_cmd_buf: Option<metal::CommandBuffer>,+ pending_label: Option<String>,state: CommandState,temp: Temp,counters: Arc<wgt::HalCounters>,@@ -999,7 +1007,7 @@ unsafe impl Sync for CommandEncoder {}#[derive(Debug)]pub struct CommandBuffer {- raw: metal::CommandBuffer,+ raw: Option<metal::CommandBuffer>,}impl crate::DynCommandBuffer for CommandBuffer {}