Skip to content

Commit

Permalink
remove device arg from all CommandEncoder methods
Browse files Browse the repository at this point in the history
  • Loading branch information
teoxoy committed Dec 20, 2024
1 parent 973def4 commit a5c3be5
Show file tree
Hide file tree
Showing 9 changed files with 49 additions and 63 deletions.
4 changes: 2 additions & 2 deletions wgpu-core/src/command/clear.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ impl Global {

// actual hal barrier & operation
let dst_barrier = dst_pending.map(|pending| pending.into_hal(&dst_buffer, &snatch_guard));
let cmd_buf_raw = cmd_buf_data.encoder.open(&cmd_buf.device)?;
let cmd_buf_raw = cmd_buf_data.encoder.open()?;
unsafe {
cmd_buf_raw.transition_buffers(dst_barrier.as_slice());
cmd_buf_raw.clear_buffer(dst_raw, offset..end_offset);
Expand Down Expand Up @@ -235,7 +235,7 @@ impl Global {

let device = &cmd_buf.device;
device.check_is_valid()?;
let (encoder, tracker) = cmd_buf_data.open_encoder_and_tracker(&cmd_buf.device)?;
let (encoder, tracker) = cmd_buf_data.open_encoder_and_tracker()?;

let snatch_guard = device.snatchable_lock.read();
clear_texture(
Expand Down
14 changes: 5 additions & 9 deletions wgpu-core/src/command/compute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -415,11 +415,9 @@ impl Global {
// We automatically keep extending command buffers over time, and because
// we want to insert a command buffer _before_ what we're about to record,
// we need to make sure to close the previous one.
encoder
.close_if_open(&cmd_buf.device)
.map_pass_err(pass_scope)?;
encoder.close_if_open().map_pass_err(pass_scope)?;
let raw_encoder = encoder
.open_pass(base.label.as_deref(), &cmd_buf.device)
.open_pass(base.label.as_deref())
.map_pass_err(pass_scope)?;

let mut state = State {
Expand Down Expand Up @@ -598,13 +596,13 @@ impl Global {
} = state;

// Stop the current command buffer.
encoder.close(&cmd_buf.device).map_pass_err(pass_scope)?;
encoder.close().map_pass_err(pass_scope)?;

// Create a new command buffer, which we will insert _before_ the body of the compute pass.
//
// Use that buffer to insert barriers and clear discarded images.
let transit = encoder
.open_pass(Some("(wgpu internal) Pre Pass"), &cmd_buf.device)
.open_pass(Some("(wgpu internal) Pre Pass"))
.map_pass_err(pass_scope)?;
fixup_discarded_surfaces(
pending_discard_init_fixups.into_iter(),
Expand All @@ -620,9 +618,7 @@ impl Global {
&snatch_guard,
);
// Close the command buffer, and swap it with the previous.
encoder
.close_and_swap(&cmd_buf.device)
.map_pass_err(pass_scope)?;
encoder.close_and_swap().map_pass_err(pass_scope)?;
cmd_buf_data_guard.mark_successful();

Ok(())
Expand Down
47 changes: 23 additions & 24 deletions wgpu-core/src/command/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,10 +150,10 @@ impl CommandEncoderStatus {
}
}

fn finish(&mut self, device: &Device) -> Result<(), CommandEncoderError> {
fn finish(&mut self) -> Result<(), CommandEncoderError> {
match mem::replace(self, Self::Error) {
Self::Recording(mut inner) => {
if let Err(e) = inner.encoder.close_if_open(device) {
if let Err(e) = inner.encoder.close_if_open() {
Err(e.into())
} else {
*self = Self::Finished(inner);
Expand Down Expand Up @@ -302,11 +302,12 @@ impl CommandEncoder {
/// [l]: CommandEncoder::list
/// [`transition_buffers`]: hal::CommandEncoder::transition_buffers
/// [`transition_textures`]: hal::CommandEncoder::transition_textures
fn close_and_swap(&mut self, device: &Device) -> Result<(), DeviceError> {
fn close_and_swap(&mut self) -> Result<(), DeviceError> {
assert!(self.is_open);
self.is_open = false;

let new = unsafe { self.raw.end_encoding() }.map_err(|e| device.handle_hal_error(e))?;
let new =
unsafe { self.raw.end_encoding() }.map_err(|e| self.device.handle_hal_error(e))?;
self.list.insert(self.list.len() - 1, new);

Ok(())
Expand All @@ -322,11 +323,12 @@ impl CommandEncoder {
/// - If the encoder is not open.
///
/// [l]: CommandEncoder::list
pub(crate) fn close_and_push_front(&mut self, device: &Device) -> Result<(), DeviceError> {
pub(crate) fn close_and_push_front(&mut self) -> Result<(), DeviceError> {
assert!(self.is_open);
self.is_open = false;

let new = unsafe { self.raw.end_encoding() }.map_err(|e| device.handle_hal_error(e))?;
let new =
unsafe { self.raw.end_encoding() }.map_err(|e| self.device.handle_hal_error(e))?;
self.list.insert(0, new);

Ok(())
Expand All @@ -342,11 +344,12 @@ impl CommandEncoder {
/// - If the encoder is not open.
///
/// [l]: CommandEncoder::list
pub(crate) fn close(&mut self, device: &Device) -> Result<(), DeviceError> {
pub(crate) fn close(&mut self) -> Result<(), DeviceError> {
assert!(self.is_open);
self.is_open = false;

let cmd_buf = unsafe { self.raw.end_encoding() }.map_err(|e| device.handle_hal_error(e))?;
let cmd_buf =
unsafe { self.raw.end_encoding() }.map_err(|e| self.device.handle_hal_error(e))?;
self.list.push(cmd_buf);

Ok(())
Expand All @@ -362,11 +365,11 @@ impl CommandEncoder {
/// On return, the underlying hal encoder is closed.
///
/// [l]: CommandEncoder::list
fn close_if_open(&mut self, device: &Device) -> Result<(), DeviceError> {
fn close_if_open(&mut self) -> Result<(), DeviceError> {
if self.is_open {
self.is_open = false;
let cmd_buf =
unsafe { self.raw.end_encoding() }.map_err(|e| device.handle_hal_error(e))?;
unsafe { self.raw.end_encoding() }.map_err(|e| self.device.handle_hal_error(e))?;
self.list.push(cmd_buf);
}

Expand All @@ -376,15 +379,12 @@ impl CommandEncoder {
/// Begin recording a new command buffer, if we haven't already.
///
/// The underlying hal encoder is put in the "recording" state.
pub(crate) fn open(
&mut self,
device: &Device,
) -> Result<&mut dyn hal::DynCommandEncoder, DeviceError> {
pub(crate) fn open(&mut self) -> Result<&mut dyn hal::DynCommandEncoder, DeviceError> {
if !self.is_open {
self.is_open = true;
let hal_label = self.hal_label.as_deref();
unsafe { self.raw.begin_encoding(hal_label) }
.map_err(|e| device.handle_hal_error(e))?;
.map_err(|e| self.device.handle_hal_error(e))?;
}

Ok(self.raw.as_mut())
Expand All @@ -401,13 +401,13 @@ impl CommandEncoder {
pub(crate) fn open_pass(
&mut self,
label: Option<&str>,
device: &Device,
) -> Result<&mut dyn hal::DynCommandEncoder, DeviceError> {
assert!(!self.is_open);
self.is_open = true;

let hal_label = hal_label(label, device.instance_flags);
unsafe { self.raw.begin_encoding(hal_label) }.map_err(|e| device.handle_hal_error(e))?;
let hal_label = hal_label(label, self.device.instance_flags);
unsafe { self.raw.begin_encoding(hal_label) }
.map_err(|e| self.device.handle_hal_error(e))?;

Ok(self.raw.as_mut())
}
Expand Down Expand Up @@ -468,9 +468,8 @@ pub struct CommandBufferMutable {
impl CommandBufferMutable {
pub(crate) fn open_encoder_and_tracker(
&mut self,
device: &Device,
) -> Result<(&mut dyn hal::DynCommandEncoder, &mut Tracker), DeviceError> {
let encoder = self.encoder.open(device)?;
let encoder = self.encoder.open()?;
let tracker = &mut self.trackers;

Ok((encoder, tracker))
Expand Down Expand Up @@ -753,7 +752,7 @@ impl Global {

let cmd_buf = hub.command_buffers.get(encoder_id.into_command_buffer_id());

let error = match cmd_buf.data.lock().finish(&cmd_buf.device) {
let error = match cmd_buf.data.lock().finish() {
Ok(_) => None,
Err(e) => Some(e),
};
Expand Down Expand Up @@ -781,7 +780,7 @@ impl Global {
list.push(TraceCommand::PushDebugGroup(label.to_string()));
}

let cmd_buf_raw = cmd_buf_data.encoder.open(&cmd_buf.device)?;
let cmd_buf_raw = cmd_buf_data.encoder.open()?;
if !cmd_buf
.device
.instance_flags
Expand Down Expand Up @@ -821,7 +820,7 @@ impl Global {
.instance_flags
.contains(wgt::InstanceFlags::DISCARD_HAL_LABELS)
{
let cmd_buf_raw = cmd_buf_data.encoder.open(&cmd_buf.device)?;
let cmd_buf_raw = cmd_buf_data.encoder.open()?;
unsafe {
cmd_buf_raw.insert_debug_marker(label);
}
Expand Down Expand Up @@ -850,7 +849,7 @@ impl Global {
list.push(TraceCommand::PopDebugGroup);
}

let cmd_buf_raw = cmd_buf_data.encoder.open(&cmd_buf.device)?;
let cmd_buf_raw = cmd_buf_data.encoder.open()?;
if !cmd_buf
.device
.instance_flags
Expand Down
4 changes: 2 additions & 2 deletions wgpu-core/src/command/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,7 @@ impl Global {
});
}

let raw_encoder = cmd_buf_data.encoder.open(&cmd_buf.device)?;
let raw_encoder = cmd_buf_data.encoder.open()?;

let query_set = hub.query_sets.get(query_set_id).get()?;

Expand Down Expand Up @@ -447,7 +447,7 @@ impl Global {
);

let raw_dst_buffer = dst_buffer.try_raw(&snatch_guard)?;
let raw_encoder = cmd_buf_data.encoder.open(&cmd_buf.device)?;
let raw_encoder = cmd_buf_data.encoder.open()?;
unsafe {
raw_encoder.transition_buffers(dst_barrier.as_slice());
raw_encoder.copy_query_results(
Expand Down
4 changes: 2 additions & 2 deletions wgpu-core/src/command/ray_tracing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@ impl Global {
let blas_present = !blas_storage.is_empty();
let tlas_present = !tlas_storage.is_empty();

let cmd_buf_raw = cmd_buf_data.encoder.open(device)?;
let cmd_buf_raw = cmd_buf_data.encoder.open()?;

let mut descriptors = Vec::new();

Expand Down Expand Up @@ -674,7 +674,7 @@ impl Global {
let blas_present = !blas_storage.is_empty();
let tlas_present = !tlas_storage.is_empty();

let cmd_buf_raw = cmd_buf_data.encoder.open(device)?;
let cmd_buf_raw = cmd_buf_data.encoder.open()?;

let mut descriptors = Vec::new();

Expand Down
14 changes: 5 additions & 9 deletions wgpu-core/src/command/render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1663,11 +1663,9 @@ impl Global {
// We automatically keep extending command buffers over time, and because
// we want to insert a command buffer _before_ what we're about to record,
// we need to make sure to close the previous one.
encoder.close_if_open().map_pass_err(pass_scope)?;
encoder
.close_if_open(&cmd_buf.device)
.map_pass_err(pass_scope)?;
encoder
.open_pass(base.label.as_deref(), &cmd_buf.device)
.open_pass(base.label.as_deref())
.map_pass_err(pass_scope)?;

let info = RenderPassInfo::start(
Expand Down Expand Up @@ -1971,7 +1969,7 @@ impl Global {
.finish(state.raw_encoder, state.snatch_guard)
.map_pass_err(pass_scope)?;

encoder.close(&cmd_buf.device).map_pass_err(pass_scope)?;
encoder.close().map_pass_err(pass_scope)?;
(trackers, pending_discard_init_fixups)
};

Expand All @@ -1980,7 +1978,7 @@ impl Global {

{
let transit = encoder
.open_pass(Some("(wgpu internal) Pre Pass"), &cmd_buf.device)
.open_pass(Some("(wgpu internal) Pre Pass"))
.map_pass_err(pass_scope)?;

fixup_discarded_surfaces(
Expand All @@ -1996,9 +1994,7 @@ impl Global {
CommandBuffer::insert_barriers_from_scope(transit, tracker, &scope, snatch_guard);
}

encoder
.close_and_swap(&cmd_buf.device)
.map_pass_err(pass_scope)?;
encoder.close_and_swap().map_pass_err(pass_scope)?;
cmd_buf_data_guard.mark_successful();

Ok(())
Expand Down
10 changes: 5 additions & 5 deletions wgpu-core/src/command/transfer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -444,7 +444,7 @@ fn handle_texture_init(

// In rare cases we may need to insert an init operation immediately onto the command buffer.
if !immediate_inits.is_empty() {
let cmd_buf_raw = cmd_buf_data.encoder.open(device)?;
let cmd_buf_raw = cmd_buf_data.encoder.open()?;
for init in immediate_inits {
clear_texture(
&init.texture,
Expand Down Expand Up @@ -678,7 +678,7 @@ impl Global {
dst_offset: destination_offset,
size: wgt::BufferSize::new(size).unwrap(),
};
let cmd_buf_raw = cmd_buf_data.encoder.open(&cmd_buf.device)?;
let cmd_buf_raw = cmd_buf_data.encoder.open()?;
let barriers = src_barrier
.into_iter()
.chain(dst_barrier)
Expand Down Expand Up @@ -838,7 +838,7 @@ impl Global {
})
.collect::<Vec<_>>();

let cmd_buf_raw = cmd_buf_data.encoder.open(&cmd_buf.device)?;
let cmd_buf_raw = cmd_buf_data.encoder.open()?;
unsafe {
cmd_buf_raw.transition_textures(&dst_barrier);
cmd_buf_raw.transition_buffers(src_barrier.as_slice());
Expand Down Expand Up @@ -1004,7 +1004,7 @@ impl Global {
}
})
.collect::<Vec<_>>();
let cmd_buf_raw = cmd_buf_data.encoder.open(&cmd_buf.device)?;
let cmd_buf_raw = cmd_buf_data.encoder.open()?;
unsafe {
cmd_buf_raw.transition_buffers(dst_barrier.as_slice());
cmd_buf_raw.transition_textures(&src_barrier);
Expand Down Expand Up @@ -1168,7 +1168,7 @@ impl Global {
}
})
.collect::<Vec<_>>();
let cmd_buf_raw = cmd_buf_data.encoder.open(&cmd_buf.device)?;
let cmd_buf_raw = cmd_buf_data.encoder.open()?;
unsafe {
cmd_buf_raw.transition_textures(&barriers);
cmd_buf_raw.copy_texture_to_texture(
Expand Down
13 changes: 4 additions & 9 deletions wgpu-core/src/device/queue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1164,10 +1164,7 @@ impl Queue {
};

// execute resource transitions
if let Err(e) = baked
.encoder
.open_pass(Some("(wgpu internal) Transit"), &self.device)
{
if let Err(e) = baked.encoder.open_pass(Some("(wgpu internal) Transit")) {
break 'error Err(e.into());
}

Expand All @@ -1194,17 +1191,15 @@ impl Queue {
&snatch_guard,
);

if let Err(e) = baked.encoder.close_and_push_front(&self.device) {
if let Err(e) = baked.encoder.close_and_push_front() {
break 'error Err(e.into());
}

// Transition surface textures into `Present` state.
// Note: we could technically do it after all of the command buffers,
// but here we have a command encoder by hand, so it's easier to use it.
if !used_surface_textures.is_empty() {
if let Err(e) = baked
.encoder
.open_pass(Some("(wgpu internal) Present"), &self.device)
if let Err(e) = baked.encoder.open_pass(Some("(wgpu internal) Present"))
{
break 'error Err(e.into());
}
Expand All @@ -1218,7 +1213,7 @@ impl Queue {
unsafe {
baked.encoder.raw.transition_textures(&texture_barriers);
};
if let Err(e) = baked.encoder.close(&self.device) {
if let Err(e) = baked.encoder.close() {
break 'error Err(e.into());
}
used_surface_textures = track::TextureUsageScope::default();
Expand Down
2 changes: 1 addition & 1 deletion wgpu-core/src/resource.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1381,7 +1381,7 @@ impl Global {
if let Ok(mut cmd_buf_data_guard) = cmd_buf_data_guard {
let cmd_buf_raw = cmd_buf_data_guard
.encoder
.open(&cmd_buf.device)
.open()
.ok()
.and_then(|encoder| encoder.as_any_mut().downcast_mut());
let ret = hal_command_encoder_callback(cmd_buf_raw);
Expand Down

0 comments on commit a5c3be5

Please sign in to comment.