From 2edafa5240fd08430b1345e9daab6b5b81064faa Mon Sep 17 00:00:00 2001 From: JMS55 <47158642+JMS55@users.noreply.github.com> Date: Sun, 25 Oct 2026 07:57:55 -0400 Subject: [PATCH] Record 1 barrier for N Queue::write_buffer/texture calls by deferring to submit time (#10319) --- CHANGELOG.md | 1 + wgpu-core/src/device/queue.rs | 174 ++++++++++++++++++++++++++++------ wgpu-core/src/resource.rs | 12 +-- 3 files changed, 151 insertions(+), 36 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index eccf6abe9..cc54ae240 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -312,6 +312,7 @@ By @sagudev in [#10307](https://github.com/gfx-rs/wgpu/pull/10307). ### Performance - Added explicit `Send` and `Sync` implementations to key `wgpu` types so that the compiler can do less work checking those bounds. If you previously added a `#![recursion_limit = ...]` attribute to your crate due to overflow errors involving `wgpu` types, you may now be able to remove it. By @kpreid in [#10177](https://github.com/gfx-rs/wgpu/pull/10177). +- `Queue::write_buffer`, `Queue::write_texture` and buffer unmaps no longer record a separate barrier before each copy. The transitions for a whole batch of queue writes are now coalesced into a single barrier recorded ahead of the batch's copies, so a frame that updates N buffers costs one barrier instead of N. By @JMS55 in [#10319](https://github.com/gfx-rs/wgpu/pull/10319). ### Documentation diff --git a/wgpu-core/src/device/queue.rs b/wgpu-core/src/device/queue.rs index 8f9f639e4..90ef0dec6 100644 --- a/wgpu-core/src/device/queue.rs +++ b/wgpu-core/src/device/queue.rs @@ -2,7 +2,6 @@ use alloc::string::ToString as _; use alloc::{boxed::Box, string::String, sync::Arc, vec, vec::Vec}; use core::{ - iter, mem::{self, ManuallyDrop}, num::NonZeroU64, ptr::NonNull, @@ -397,6 +396,39 @@ pub(crate) struct PendingWrites { dst_textures: FastHashMap>, copied_blas_s: FastHashMap>, instance_flags: wgt::InstanceFlags, + + /// Staging buffers that are the source of a copy recorded in + /// `command_encoder`, and so need a `MAP_WRITE` -> `COPY_SRC` transition. + /// + /// See [`hoisted_buffer_barriers`] for why these are not recorded + /// immediately. Moved to `temp_resources` in [`pre_submit`]. + /// + /// [`hoisted_buffer_barriers`]: PendingWrites::hoisted_buffer_barriers + /// [`pre_submit`]: PendingWrites::pre_submit + hoisted_staging_buffers: Vec, + + /// Buffer transitions that must happen before the copies recorded in + /// `command_encoder`. + /// + /// Recording a barrier immediately before each copy means that a frame + /// which calls `Queue::write_buffer` a few dozen times ends up with many + /// back-to-back barrier/copy pairs, when a single barrier covering every + /// buffer would have done just as well. + /// + /// Instead we accumulate the transitions here and record them all at once, + /// into a command buffer that [`pre_submit`] places in front of + /// `command_encoder`'s. + /// + /// Only a buffer's *first* transition in a batch may be hoisted like this. + /// A buffer written more than once per batch gets a second, `COPY_DST` -> + /// `COPY_DST` transition from the tracker (unless the adapter reports + /// `COPY_DST` as an ordered usage), and that barrier exists precisely to + /// separate the two copies, so it has to stay where it was recorded. See + /// [`hoist_buffer_barrier`]. + /// + /// [`pre_submit`]: PendingWrites::pre_submit + /// [`hoist_buffer_barrier`]: PendingWrites::hoist_buffer_barrier + hoisted_buffer_barriers: Vec<(Arc, hal::StateTransition)>, } impl PendingWrites { @@ -412,6 +444,8 @@ impl PendingWrites { dst_textures: FastHashMap::default(), copied_blas_s: FastHashMap::default(), instance_flags, + hoisted_staging_buffers: Vec::new(), + hoisted_buffer_barriers: Vec::new(), } } @@ -442,9 +476,45 @@ impl PendingWrites { self.temp_resources.push(resource); } + /// Take ownership of a staging buffer that is the source of a copy + /// recorded into this batch. + /// + /// This also queues up the staging buffer's `MAP_WRITE` -> `COPY_SRC` + /// transition, so callers must not record one themselves. pub fn consume(&mut self, buffer: FlushedStagingBuffer) { - self.temp_resources - .push(TempResource::StagingBuffer(buffer)); + self.hoisted_staging_buffers.push(buffer); + } + + /// Queue up a buffer transition to be recorded ahead of this batch's + /// copies. + /// + /// Returns the transition instead if it cannot be hoisted, in which case + /// the caller must record it immediately, before its own copy. This happens + /// when this batch has already copied into `buffer`: the transition is then + /// a `COPY_DST` -> `COPY_DST` write-after-write barrier separating the + /// earlier copy from the caller's, as hoisting it to the front of the + /// batch would leave the two copies unsynchronized. + /// + /// See [`hoisted_buffer_barriers`]. + /// + /// [`hoisted_buffer_barriers`]: PendingWrites::hoisted_buffer_barriers + #[must_use] + fn hoist_buffer_barrier( + &mut self, + buffer: &Arc, + transition: Option>, + ) -> Option> { + let transition = transition?; + + // `dst_buffers` is only populated once a copy has been recorded, so + // true here means an earlier copy in this batch already wrote `buffer`. + if self.dst_buffers.contains_key(&buffer.tracker_index()) { + return Some(transition); + } + + self.hoisted_buffer_barriers + .push((buffer.clone(), transition.usage)); + None } pub fn clear_buffer( @@ -454,19 +524,21 @@ impl PendingWrites { range: core::ops::Range, snatch_guard: &SnatchGuard, ) -> Result<(), QueueWriteError> { - let barriers = { + let transition = { let mut trackers = device.trackers.lock(); trackers .buffers .set_single(buffer, wgt::BufferUses::COPY_DST) - .map(|pending| pending.into_hal(buffer, snatch_guard)) }; + let inline_transition = self.hoist_buffer_barrier(buffer, transition); let dst_raw = buffer.try_raw(snatch_guard)?; let encoder = self.activate(); unsafe { - encoder.transition_buffers(barriers.as_slice()); + if let Some(transition) = inline_transition { + encoder.transition_buffers(&[transition.into_hal(buffer, snatch_guard)]); + } encoder.clear_buffer(dst_raw, range.clone()); } @@ -479,11 +551,56 @@ impl PendingWrites { Ok(()) } + /// Record every transition this batch of writes needs as a single barrier, + /// into a command buffer placed ahead of the one holding the batch's + /// copies. + /// + /// Returns the staging buffers whose transitions were recorded. The caller + /// is responsible for keeping them alive until the submission completes. + fn record_hoisted_barriers( + &mut self, + encoder: &mut EncoderInFlight, + snatch_guard: &SnatchGuard, + ) -> Result, DeviceError> { + let staging_buffers = mem::take(&mut self.hoisted_staging_buffers); + let buffer_barriers = mem::take(&mut self.hoisted_buffer_barriers); + + let barriers = staging_buffers + .iter() + .map(|staging_buffer| hal::BufferBarrier { + buffer: staging_buffer.raw(), + usage: hal::StateTransition { + from: wgt::BufferUses::MAP_WRITE, + to: wgt::BufferUses::COPY_SRC, + }, + }) + .chain(buffer_barriers.iter().filter_map(|(buffer, usage)| { + // The buffer may have been destroyed since the copy was + // recorded, in which case there is nothing to sync. + Some(hal::BufferBarrier { + buffer: buffer.raw(snatch_guard)?, + usage: usage.clone(), + }) + })) + .collect::>(); + + if !barriers.is_empty() { + encoder + .inner + .open_pass(Some("(wgpu internal) PendingWrites Transit"))?; + unsafe { encoder.inner.raw.transition_buffers(&barriers) }; + encoder.inner.close_and_push_front()?; + } + + Ok(staging_buffers) + } + fn pre_submit( &mut self, command_allocator: &CommandAllocator, device: &Arc, queue: &Queue, + snatch_guard: &SnatchGuard, ) -> Result, DeviceError> { if self.is_recording { let pending_buffers = mem::take(&mut self.dst_buffers); @@ -498,7 +615,7 @@ impl PendingWrites { .acquire_encoder(device.raw(), queue.raw()) .map_err(|e| device.handle_hal_error(e))?; - let encoder = EncoderInFlight { + let mut encoder = EncoderInFlight { inner: crate::command::InnerCommandEncoder { raw: ManuallyDrop::new(mem::replace(&mut self.command_encoder, new_encoder)), list: vec![cmd_buf], @@ -516,6 +633,12 @@ impl PendingWrites { pending_textures, pending_blas_s, }; + + let staging_buffers = self.record_hoisted_barriers(&mut encoder, snatch_guard)?; + encoder + .temp_resources + .extend(staging_buffers.into_iter().map(TempResource::StagingBuffer)); + Ok(Some(encoder)) } else { self.dst_buffers.clear(); @@ -934,18 +1057,14 @@ impl Queue { dst_offset: buffer_offset, size: staging_buffer.size, }; - let barriers = iter::once(hal::BufferBarrier { - buffer: staging_buffer.raw(), - usage: hal::StateTransition { - from: wgt::BufferUses::MAP_WRITE, - to: wgt::BufferUses::COPY_SRC, - }, - }) - .chain(transition.map(|pending| pending.into_hal(&buffer, snatch_guard))) - .collect::>(); + // The staging buffer's own `MAP_WRITE` -> `COPY_SRC` transition is + // hoisted by `PendingWrites::consume`, which our caller invokes. + let inline_transition = pending_writes.hoist_buffer_barrier(&buffer, transition); let encoder = pending_writes.activate(); unsafe { - encoder.transition_buffers(&barriers); + if let Some(transition) = inline_transition { + encoder.transition_buffers(&[transition.into_hal(&buffer, snatch_guard)]); + } encoder.copy_buffer_to_buffer(staging_buffer.raw(), dst_raw, &[region]); } @@ -1186,14 +1305,6 @@ impl Queue { .collect::>(); { - let buffer_barrier = hal::BufferBarrier { - buffer: staging_buffer.raw(), - usage: hal::StateTransition { - from: wgt::BufferUses::MAP_WRITE, - to: wgt::BufferUses::COPY_SRC, - }, - }; - let mut trackers = self.device.trackers.lock(); let transition = trackers @@ -1203,9 +1314,11 @@ impl Queue { .map(|pending| pending.into_hal(dst_raw)) .collect::>(); + // The staging buffer's `MAP_WRITE` -> `COPY_SRC` transition is + // hoisted to the front of the batch by `PendingWrites::consume`, + // below. unsafe { encoder.transition_textures(&texture_barriers); - encoder.transition_buffers(&[buffer_barrier]); encoder.copy_buffer_to_texture(staging_buffer.raw(), dst_raw, ®ions); } } @@ -1437,7 +1550,7 @@ impl Queue { source, dst_raw_webgl, premultiplied_alpha, - iter::once(regions), + core::iter::once(regions), ); } @@ -1906,7 +2019,12 @@ impl Queue { }; } - match pending_writes.pre_submit(&self.device.command_allocator, &self.device, self) { + match pending_writes.pre_submit( + &self.device.command_allocator, + &self.device, + self, + &snatch_guard, + ) { Ok(Some(pending_execution)) => { executions.insert(0, pending_execution); } diff --git a/wgpu-core/src/resource.rs b/wgpu-core/src/resource.rs index 81d048fa4..09aa63a81 100644 --- a/wgpu-core/src/resource.rs +++ b/wgpu-core/src/resource.rs @@ -1178,13 +1178,9 @@ impl Buffer { dst_offset: 0, size: staging_buffer.size, }); - let transition_src = hal::BufferBarrier { - buffer: staging_buffer.raw(), - usage: hal::StateTransition { - from: wgt::BufferUses::MAP_WRITE, - to: wgt::BufferUses::COPY_SRC, - }, - }; + // The staging buffer's `MAP_WRITE` -> `COPY_SRC` transition + // is hoisted to the front of the batch by + // `PendingWrites::consume`, below. let transition_dst = hal::BufferBarrier:: { buffer: raw_buf, usage: hal::StateTransition { @@ -1195,7 +1191,7 @@ impl Buffer { let mut pending_writes = queue.pending_writes.lock(); let encoder = pending_writes.activate(); unsafe { - encoder.transition_buffers(&[transition_src, transition_dst]); + encoder.transition_buffers(&[transition_dst]); // Buffers allocate at least `COPY_BUFFER_ALIGNMENT` bytes, so // there's always something to copy here. encoder.copy_buffer_to_buffer(