Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reintroduce buffer snatching Part 1 #4867

Merged
merged 4 commits into from
Dec 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions wgpu-core/src/command/bundle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -785,6 +785,8 @@ impl<A: HalApi> RenderBundle<A> {
}
}

let snatch_guard = self.device.snatchable_lock.read();

for command in self.base.commands.iter() {
match *command {
RenderCommand::SetBindGroup {
Expand Down Expand Up @@ -818,7 +820,7 @@ impl<A: HalApi> RenderBundle<A> {
size,
} => {
let buffers = trackers.buffers.read();
let buffer = buffers.get(buffer_id).unwrap().raw();
let buffer = buffers.get(buffer_id).unwrap().raw(&snatch_guard);
let bb = hal::BufferBinding {
buffer,
offset,
Expand All @@ -833,7 +835,7 @@ impl<A: HalApi> RenderBundle<A> {
size,
} => {
let buffers = trackers.buffers.read();
let buffer = buffers.get(buffer_id).unwrap().raw();
let buffer = buffers.get(buffer_id).unwrap().raw(&snatch_guard);
let bb = hal::BufferBinding {
buffer,
offset,
Expand Down Expand Up @@ -912,7 +914,7 @@ impl<A: HalApi> RenderBundle<A> {
indexed: false,
} => {
let buffers = trackers.buffers.read();
let buffer = buffers.get(buffer_id).unwrap().raw();
let buffer = buffers.get(buffer_id).unwrap().raw(&snatch_guard);
unsafe { raw.draw_indirect(buffer, offset, 1) };
}
RenderCommand::MultiDrawIndirect {
Expand All @@ -922,7 +924,7 @@ impl<A: HalApi> RenderBundle<A> {
indexed: true,
} => {
let buffers = trackers.buffers.read();
let buffer = buffers.get(buffer_id).unwrap().raw();
let buffer = buffers.get(buffer_id).unwrap().raw(&snatch_guard);
unsafe { raw.draw_indexed_indirect(buffer, offset, 1) };
}
RenderCommand::MultiDrawIndirect { .. }
Expand Down
6 changes: 4 additions & 2 deletions wgpu-core/src/command/clear.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,10 @@ impl<G: GlobalIdentityHandlerFactory> Global<G> {
.set_single(dst_buffer, hal::BufferUses::COPY_DST)
.ok_or(ClearError::InvalidBuffer(dst))?
};
let snatch_guard = dst_buffer.device.snatchable_lock.read();
let dst_raw = dst_buffer
.raw
.as_ref()
.get(&snatch_guard)
.ok_or(ClearError::InvalidBuffer(dst))?;
if !dst_buffer.usage.contains(BufferUsages::COPY_DST) {
return Err(ClearError::MissingCopyDstUsageFlag(Some(dst), None));
Expand Down Expand Up @@ -145,8 +146,9 @@ impl<G: GlobalIdentityHandlerFactory> Global<G> {
MemoryInitKind::ImplicitlyInitialized,
),
);

// actual hal barrier & operation
let dst_barrier = dst_pending.map(|pending| pending.into_hal(&dst_buffer));
let dst_barrier = dst_pending.map(|pending| pending.into_hal(&dst_buffer, &snatch_guard));
let cmd_buf_raw = cmd_buf_data.encoder.open();
unsafe {
cmd_buf_raw.transition_buffers(dst_barrier.into_iter());
Expand Down
24 changes: 20 additions & 4 deletions wgpu-core/src/command/compute.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::resource::Resource;
use crate::snatch::SnatchGuard;
use crate::{
binding_model::{
BindError, BindGroup, LateMinBufferBindingSizeMismatch, PushConstantUploadError,
Expand Down Expand Up @@ -310,6 +311,7 @@ impl<A: HalApi> State<A> {
base_trackers: &mut Tracker<A>,
bind_group_guard: &Storage<BindGroup<A>, id::BindGroupId>,
indirect_buffer: Option<id::BufferId>,
snatch_guard: &SnatchGuard,
) -> Result<(), UsageConflict> {
for id in self.binder.list_active() {
unsafe { self.scope.merge_bind_group(&bind_group_guard[id].used)? };
Expand All @@ -335,7 +337,7 @@ impl<A: HalApi> State<A> {

log::trace!("Encoding dispatch barriers");

CommandBuffer::drain_barriers(raw_encoder, base_trackers);
CommandBuffer::drain_barriers(raw_encoder, base_trackers, snatch_guard);
Ok(())
}
}
Expand Down Expand Up @@ -453,6 +455,8 @@ impl<G: GlobalIdentityHandlerFactory> Global<G> {
None
};

let snatch_guard = device.snatchable_lock.read();

tracker.set_size(
Some(&*buffer_guard),
Some(&*texture_guard),
Expand Down Expand Up @@ -673,7 +677,13 @@ impl<G: GlobalIdentityHandlerFactory> Global<G> {
state.is_ready().map_pass_err(scope)?;

state
.flush_states(raw, &mut intermediate_trackers, &*bind_group_guard, None)
.flush_states(
raw,
&mut intermediate_trackers,
&*bind_group_guard,
None,
&snatch_guard,
)
.map_pass_err(scope)?;

let groups_size_limit = cmd_buf.limits.max_compute_workgroups_per_dimension;
Expand Down Expand Up @@ -727,7 +737,7 @@ impl<G: GlobalIdentityHandlerFactory> Global<G> {

let buf_raw = indirect_buffer
.raw
.as_ref()
.get(&snatch_guard)
.ok_or(ComputePassErrorInner::InvalidIndirectBuffer(buffer_id))
.map_pass_err(scope)?;

Expand All @@ -747,6 +757,7 @@ impl<G: GlobalIdentityHandlerFactory> Global<G> {
&mut intermediate_trackers,
&*bind_group_guard,
Some(buffer_id),
&snatch_guard,
)
.map_pass_err(scope)?;
unsafe {
Expand Down Expand Up @@ -860,7 +871,12 @@ impl<G: GlobalIdentityHandlerFactory> Global<G> {
&mut tracker.textures,
device,
);
CommandBuffer::insert_barriers_from_tracker(transit, tracker, &intermediate_trackers);
CommandBuffer::insert_barriers_from_tracker(
transit,
tracker,
&intermediate_trackers,
&snatch_guard,
);
// Close the command buffer, and swap it with the previous.
encoder.close_and_swap();

Expand Down
8 changes: 6 additions & 2 deletions wgpu-core/src/command/memory_init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -223,12 +223,16 @@ impl<A: HalApi> BakedCommands<A> {
.unwrap()
.1;

let raw_buf = buffer.raw.as_ref().ok_or(DestroyedBufferError(buffer_id))?;
let snatch_guard = buffer.device.snatchable_lock.read();
let raw_buf = buffer
.raw
.get(&snatch_guard)
.ok_or(DestroyedBufferError(buffer_id))?;

unsafe {
self.encoder.transition_buffers(
transition
.map(|pending| pending.into_hal(&buffer))
.map(|pending| pending.into_hal(&buffer, &snatch_guard))
.into_iter(),
);
}
Expand Down
15 changes: 11 additions & 4 deletions wgpu-core/src/command/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ use crate::device::Device;
use crate::error::{ErrorFormatter, PrettyError};
use crate::hub::Hub;
use crate::id::CommandBufferId;
use crate::snatch::SnatchGuard;

use crate::init_tracker::BufferInitTrackerAction;
use crate::resource::{Resource, ResourceInfo, ResourceType};
Expand Down Expand Up @@ -193,32 +194,38 @@ impl<A: HalApi> CommandBuffer<A> {
raw: &mut A::CommandEncoder,
base: &mut Tracker<A>,
head: &Tracker<A>,
snatch_guard: &SnatchGuard,
) {
profiling::scope!("insert_barriers");

base.buffers.set_from_tracker(&head.buffers);
base.textures.set_from_tracker(&head.textures);

Self::drain_barriers(raw, base);
Self::drain_barriers(raw, base, snatch_guard);
}

pub(crate) fn insert_barriers_from_scope(
raw: &mut A::CommandEncoder,
base: &mut Tracker<A>,
head: &UsageScope<A>,
snatch_guard: &SnatchGuard,
) {
profiling::scope!("insert_barriers");

base.buffers.set_from_usage_scope(&head.buffers);
base.textures.set_from_usage_scope(&head.textures);

Self::drain_barriers(raw, base);
Self::drain_barriers(raw, base, snatch_guard);
}

pub(crate) fn drain_barriers(raw: &mut A::CommandEncoder, base: &mut Tracker<A>) {
pub(crate) fn drain_barriers(
raw: &mut A::CommandEncoder,
base: &mut Tracker<A>,
snatch_guard: &SnatchGuard,
) {
profiling::scope!("drain_barriers");

let buffer_barriers = base.buffers.drain_transitions();
let buffer_barriers = base.buffers.drain_transitions(snatch_guard);
let (transitions, textures) = base.textures.drain_transitions();
let texture_barriers = transitions
.into_iter()
Expand Down
7 changes: 5 additions & 2 deletions wgpu-core/src/command/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -430,7 +430,10 @@ impl<G: GlobalIdentityHandlerFactory> Global<G> {
.set_single(dst_buffer, hal::BufferUses::COPY_DST)
.ok_or(QueryError::InvalidBuffer(destination))?
};
let dst_barrier = dst_pending.map(|pending| pending.into_hal(&dst_buffer));

let snatch_guard = dst_buffer.device.snatchable_lock.read();

let dst_barrier = dst_pending.map(|pending| pending.into_hal(&dst_buffer, &snatch_guard));

if !dst_buffer.usage.contains(wgt::BufferUsages::QUERY_RESOLVE) {
return Err(ResolveError::MissingBufferUsage.into());
Expand Down Expand Up @@ -481,7 +484,7 @@ impl<G: GlobalIdentityHandlerFactory> Global<G> {
raw_encoder.copy_query_results(
query_set.raw(),
start_query..end_query,
dst_buffer.raw(),
dst_buffer.raw(&snatch_guard),
destination_offset,
wgt::BufferSize::new_unchecked(stride as u64),
);
Expand Down
23 changes: 15 additions & 8 deletions wgpu-core/src/command/render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1308,8 +1308,11 @@ impl<G: GlobalIdentityHandlerFactory> Global<G> {

let hub = A::hub(self);

let cmd_buf = CommandBuffer::get_encoder(hub, encoder_id).map_pass_err(init_scope)?;
let device = &cmd_buf.device;
let snatch_guard = device.snatchable_lock.read();

let (scope, pending_discard_init_fixups) = {
let cmd_buf = CommandBuffer::get_encoder(hub, encoder_id).map_pass_err(init_scope)?;
let mut cmd_buf_data = cmd_buf.data.lock();
let cmd_buf_data = cmd_buf_data.as_mut().unwrap();

Expand All @@ -1324,7 +1327,6 @@ impl<G: GlobalIdentityHandlerFactory> Global<G> {
});
}

let device = &cmd_buf.device;
if !device.is_valid() {
return Err(DeviceError::Lost).map_pass_err(init_scope);
}
Expand Down Expand Up @@ -1639,7 +1641,7 @@ impl<G: GlobalIdentityHandlerFactory> Global<G> {
.map_pass_err(scope)?;
let buf_raw = buffer
.raw
.as_ref()
.get(&snatch_guard)
.ok_or(RenderCommandError::DestroyedBuffer(buffer_id))
.map_pass_err(scope)?;

Expand Down Expand Up @@ -1701,7 +1703,7 @@ impl<G: GlobalIdentityHandlerFactory> Global<G> {
.map_pass_err(scope)?;
let buf_raw = buffer
.raw
.as_ref()
.get(&snatch_guard)
.ok_or(RenderCommandError::DestroyedBuffer(buffer_id))
.map_pass_err(scope)?;

Expand Down Expand Up @@ -1991,7 +1993,7 @@ impl<G: GlobalIdentityHandlerFactory> Global<G> {
.map_pass_err(scope)?;
let indirect_raw = indirect_buffer
.raw
.as_ref()
.get(&snatch_guard)
.ok_or(RenderCommandError::DestroyedBuffer(buffer_id))
.map_pass_err(scope)?;

Expand Down Expand Up @@ -2063,7 +2065,7 @@ impl<G: GlobalIdentityHandlerFactory> Global<G> {
.map_pass_err(scope)?;
let indirect_raw = indirect_buffer
.raw
.as_ref()
.get(&snatch_guard)
.ok_or(RenderCommandError::DestroyedBuffer(buffer_id))
.map_pass_err(scope)?;

Expand All @@ -2080,7 +2082,7 @@ impl<G: GlobalIdentityHandlerFactory> Global<G> {
.map_pass_err(scope)?;
let count_raw = count_buffer
.raw
.as_ref()
.get(&snatch_guard)
.ok_or(RenderCommandError::DestroyedBuffer(count_buffer_id))
.map_pass_err(scope)?;

Expand Down Expand Up @@ -2385,7 +2387,12 @@ impl<G: GlobalIdentityHandlerFactory> Global<G> {
.map_err(RenderCommandError::InvalidQuerySet)
.map_pass_err(PassErrorScope::QueryReset)?;

super::CommandBuffer::insert_barriers_from_scope(transit, tracker, &scope);
super::CommandBuffer::insert_barriers_from_scope(
transit,
tracker,
&scope,
&snatch_guard,
);
}

*status = CommandEncoderStatus::Recording;
Expand Down
Loading
Loading