From 68d336ef793533f482891d18055bba7f306db732 Mon Sep 17 00:00:00 2001 From: teoxoy <28601907+teoxoy@users.noreply.github.com> Date: Fri, 8 Nov 2024 16:00:53 +0100 Subject: [PATCH] use `CommandEncoder` for `BakedCommands` & `EncoderInFlight` --- wgpu-core/src/command/memory_init.rs | 6 ++--- wgpu-core/src/command/mod.rs | 14 +++++------ wgpu-core/src/device/queue.rs | 36 +++++++++++++++------------- 3 files changed, 28 insertions(+), 28 deletions(-) diff --git a/wgpu-core/src/command/memory_init.rs b/wgpu-core/src/command/memory_init.rs index a4711998b2..50a2772a95 100644 --- a/wgpu-core/src/command/memory_init.rs +++ b/wgpu-core/src/command/memory_init.rs @@ -216,7 +216,7 @@ impl BakedCommands { let raw_buf = buffer.try_raw(snatch_guard)?; unsafe { - self.encoder.transition_buffers( + self.encoder.raw.transition_buffers( transition .map(|pending| pending.into_hal(&buffer, snatch_guard)) .as_slice(), @@ -240,7 +240,7 @@ impl BakedCommands { ); unsafe { - self.encoder.clear_buffer(raw_buf, range.clone()); + self.encoder.raw.clear_buffer(raw_buf, range.clone()); } } } @@ -293,7 +293,7 @@ impl BakedCommands { let clear_result = clear_texture( &texture_use.texture, range, - self.encoder.as_mut(), + self.encoder.raw.as_mut(), &mut device_tracker.textures, &device.alignments, device.zero_buffer.as_ref(), diff --git a/wgpu-core/src/command/mod.rs b/wgpu-core/src/command/mod.rs index 127a4ac659..de0f86bce4 100644 --- a/wgpu-core/src/command/mod.rs +++ b/wgpu-core/src/command/mod.rs @@ -122,7 +122,7 @@ pub(crate) struct CommandEncoder { /// /// [`CommandEncoder`]: hal::Api::CommandEncoder /// [`CommandAllocator`]: crate::command::CommandAllocator - raw: Box, + pub(crate) raw: Box, /// All the raw command buffers for our owning [`CommandBuffer`], in /// submission order. @@ -135,7 +135,7 @@ pub(crate) struct CommandEncoder { /// /// [CE::ra]: hal::CommandEncoder::reset_all /// [`wgpu_hal::CommandEncoder`]: hal::CommandEncoder - list: Vec>, + pub(crate) list: Vec>, /// True if `raw` is in the "recording" state. /// @@ -143,9 +143,9 @@ pub(crate) struct CommandEncoder { /// details on the states `raw` can be in. /// /// [`wgpu_hal::CommandEncoder`]: hal::CommandEncoder - is_open: bool, + pub(crate) is_open: bool, - hal_label: Option, + pub(crate) hal_label: Option, } //TODO: handle errors better @@ -248,8 +248,7 @@ impl CommandEncoder { /// Look at the documentation for [`CommandBufferMutable`] for an explanation of /// the fields in this struct. This is the "built" counterpart to that type. pub(crate) struct BakedCommands { - pub(crate) encoder: Box, - pub(crate) list: Vec>, + pub(crate) encoder: CommandEncoder, pub(crate) trackers: Tracker, buffer_memory_init_actions: Vec, texture_memory_actions: CommandBufferTextureMemoryActions, @@ -380,8 +379,7 @@ impl CommandBufferMutable { pub(crate) fn into_baked_commands(self) -> BakedCommands { BakedCommands { - encoder: self.encoder.raw, - list: self.encoder.list, + encoder: self.encoder, trackers: self.trackers, buffer_memory_init_actions: self.buffer_memory_init_actions, texture_memory_actions: self.texture_memory_actions, diff --git a/wgpu-core/src/device/queue.rs b/wgpu-core/src/device/queue.rs index 91c0a5cdf1..f0b18649df 100644 --- a/wgpu-core/src/device/queue.rs +++ b/wgpu-core/src/device/queue.rs @@ -261,8 +261,7 @@ pub enum TempResource { /// [`CommandBuffer`]: hal::Api::CommandBuffer /// [`wgpu_hal::CommandEncoder`]: hal::CommandEncoder pub(crate) struct EncoderInFlight { - raw: Box, - cmd_buffers: Vec>, + inner: crate::command::CommandEncoder, pub(crate) trackers: Tracker, /// These are the buffers that have been tracked by `PendingWrites`. @@ -281,7 +280,7 @@ impl EncoderInFlight { /// Return the command encoder, fully reset and ready to be /// reused. pub(crate) unsafe fn land(mut self) -> Box { - unsafe { self.raw.reset_all(self.cmd_buffers) }; + unsafe { self.inner.raw.reset_all(self.inner.list) }; { // This involves actually decrementing the ref count of all command buffer // resources, so can be _very_ expensive. @@ -292,7 +291,7 @@ impl EncoderInFlight { drop(self.pending_blas_s); drop(self.pending_tlas_s); } - self.raw + self.inner.raw } } @@ -412,8 +411,12 @@ impl PendingWrites { .map_err(|e| device.handle_hal_error(e))?; let encoder = EncoderInFlight { - raw: mem::replace(&mut self.command_encoder, new_encoder), - cmd_buffers: vec![cmd_buf], + inner: crate::command::CommandEncoder { + raw: mem::replace(&mut self.command_encoder, new_encoder), + list: vec![cmd_buf], + is_open: false, + hal_label: None, + }, trackers: Tracker::new(), pending_buffers, pending_textures, @@ -1175,7 +1178,7 @@ impl Queue { // execute resource transitions if let Err(e) = unsafe { - baked.encoder.begin_encoding(hal_label( + baked.encoder.raw.begin_encoding(hal_label( Some("(wgpu internal) Transit"), self.device.instance_flags, )) @@ -1202,21 +1205,21 @@ impl Queue { //Note: stateless trackers are not merged: // device already knows these resources exist. CommandBuffer::insert_barriers_from_device_tracker( - baked.encoder.as_mut(), + baked.encoder.raw.as_mut(), &mut trackers, &baked.trackers, &snatch_guard, ); - let transit = unsafe { baked.encoder.end_encoding().unwrap() }; - baked.list.insert(0, transit); + let transit = unsafe { baked.encoder.raw.end_encoding().unwrap() }; + baked.encoder.list.insert(0, transit); // 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) = unsafe { - baked.encoder.begin_encoding(hal_label( + baked.encoder.raw.begin_encoding(hal_label( Some("(wgpu internal) Present"), self.device.instance_flags, )) @@ -1233,17 +1236,16 @@ impl Queue { ) .collect::>(); let present = unsafe { - baked.encoder.transition_textures(&texture_barriers); - baked.encoder.end_encoding().unwrap() + baked.encoder.raw.transition_textures(&texture_barriers); + baked.encoder.raw.end_encoding().unwrap() }; - baked.list.push(present); + baked.encoder.list.push(present); used_surface_textures = track::TextureUsageScope::default(); } // done active_executions.push(EncoderInFlight { - raw: baked.encoder, - cmd_buffers: baked.list, + inner: baked.encoder, trackers: baked.trackers, pending_buffers: FastHashMap::default(), pending_textures: FastHashMap::default(), @@ -1307,7 +1309,7 @@ impl Queue { } let hal_command_buffers = active_executions .iter() - .flat_map(|e| e.cmd_buffers.iter().map(|b| b.as_ref())) + .flat_map(|e| e.inner.list.iter().map(|b| b.as_ref())) .collect::>(); {