use CommandEncoder for BakedCommands & EncoderInFlight

This commit is contained in:
teoxoy 2024-11-08 16:00:53 +01:00
parent 8d264843c5
commit 144c47680f
3 changed files with 28 additions and 28 deletions

View File

@ -216,7 +216,7 @@ impl BakedCommands {
let raw_buf = buffer.try_raw(snatch_guard)?; let raw_buf = buffer.try_raw(snatch_guard)?;
unsafe { unsafe {
self.encoder.transition_buffers( self.encoder.raw.transition_buffers(
transition transition
.map(|pending| pending.into_hal(&buffer, snatch_guard)) .map(|pending| pending.into_hal(&buffer, snatch_guard))
.as_slice(), .as_slice(),
@ -240,7 +240,7 @@ impl BakedCommands {
); );
unsafe { 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( let clear_result = clear_texture(
&texture_use.texture, &texture_use.texture,
range, range,
self.encoder.as_mut(), self.encoder.raw.as_mut(),
&mut device_tracker.textures, &mut device_tracker.textures,
&device.alignments, &device.alignments,
device.zero_buffer.as_ref(), device.zero_buffer.as_ref(),

View File

@ -121,7 +121,7 @@ pub(crate) struct CommandEncoder {
/// ///
/// [`CommandEncoder`]: hal::Api::CommandEncoder /// [`CommandEncoder`]: hal::Api::CommandEncoder
/// [`CommandAllocator`]: crate::command::CommandAllocator /// [`CommandAllocator`]: crate::command::CommandAllocator
raw: Box<dyn hal::DynCommandEncoder>, pub(crate) raw: Box<dyn hal::DynCommandEncoder>,
/// All the raw command buffers for our owning [`CommandBuffer`], in /// All the raw command buffers for our owning [`CommandBuffer`], in
/// submission order. /// submission order.
@ -134,7 +134,7 @@ pub(crate) struct CommandEncoder {
/// ///
/// [CE::ra]: hal::CommandEncoder::reset_all /// [CE::ra]: hal::CommandEncoder::reset_all
/// [`wgpu_hal::CommandEncoder`]: hal::CommandEncoder /// [`wgpu_hal::CommandEncoder`]: hal::CommandEncoder
list: Vec<Box<dyn hal::DynCommandBuffer>>, pub(crate) list: Vec<Box<dyn hal::DynCommandBuffer>>,
/// True if `raw` is in the "recording" state. /// True if `raw` is in the "recording" state.
/// ///
@ -142,9 +142,9 @@ pub(crate) struct CommandEncoder {
/// details on the states `raw` can be in. /// details on the states `raw` can be in.
/// ///
/// [`wgpu_hal::CommandEncoder`]: hal::CommandEncoder /// [`wgpu_hal::CommandEncoder`]: hal::CommandEncoder
is_open: bool, pub(crate) is_open: bool,
hal_label: Option<String>, pub(crate) hal_label: Option<String>,
} }
//TODO: handle errors better //TODO: handle errors better
@ -247,8 +247,7 @@ impl CommandEncoder {
/// Look at the documentation for [`CommandBufferMutable`] for an explanation of /// Look at the documentation for [`CommandBufferMutable`] for an explanation of
/// the fields in this struct. This is the "built" counterpart to that type. /// the fields in this struct. This is the "built" counterpart to that type.
pub(crate) struct BakedCommands { pub(crate) struct BakedCommands {
pub(crate) encoder: Box<dyn hal::DynCommandEncoder>, pub(crate) encoder: CommandEncoder,
pub(crate) list: Vec<Box<dyn hal::DynCommandBuffer>>,
pub(crate) trackers: Tracker, pub(crate) trackers: Tracker,
buffer_memory_init_actions: Vec<BufferInitTrackerAction>, buffer_memory_init_actions: Vec<BufferInitTrackerAction>,
texture_memory_actions: CommandBufferTextureMemoryActions, texture_memory_actions: CommandBufferTextureMemoryActions,
@ -379,8 +378,7 @@ impl CommandBufferMutable {
pub(crate) fn into_baked_commands(self) -> BakedCommands { pub(crate) fn into_baked_commands(self) -> BakedCommands {
BakedCommands { BakedCommands {
encoder: self.encoder.raw, encoder: self.encoder,
list: self.encoder.list,
trackers: self.trackers, trackers: self.trackers,
buffer_memory_init_actions: self.buffer_memory_init_actions, buffer_memory_init_actions: self.buffer_memory_init_actions,
texture_memory_actions: self.texture_memory_actions, texture_memory_actions: self.texture_memory_actions,

View File

@ -312,8 +312,7 @@ pub enum TempResource {
/// [`CommandBuffer`]: hal::Api::CommandBuffer /// [`CommandBuffer`]: hal::Api::CommandBuffer
/// [`wgpu_hal::CommandEncoder`]: hal::CommandEncoder /// [`wgpu_hal::CommandEncoder`]: hal::CommandEncoder
pub(crate) struct EncoderInFlight { pub(crate) struct EncoderInFlight {
raw: Box<dyn hal::DynCommandEncoder>, inner: crate::command::CommandEncoder,
cmd_buffers: Vec<Box<dyn hal::DynCommandBuffer>>,
pub(crate) trackers: Tracker, pub(crate) trackers: Tracker,
/// These are the buffers that have been tracked by `PendingWrites`. /// These are the buffers that have been tracked by `PendingWrites`.
@ -332,7 +331,7 @@ impl EncoderInFlight {
/// Return the command encoder, fully reset and ready to be /// Return the command encoder, fully reset and ready to be
/// reused. /// reused.
pub(crate) unsafe fn land(mut self) -> Box<dyn hal::DynCommandEncoder> { pub(crate) unsafe fn land(mut self) -> Box<dyn hal::DynCommandEncoder> {
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 // This involves actually decrementing the ref count of all command buffer
// resources, so can be _very_ expensive. // resources, so can be _very_ expensive.
@ -343,7 +342,7 @@ impl EncoderInFlight {
drop(self.pending_blas_s); drop(self.pending_blas_s);
drop(self.pending_tlas_s); drop(self.pending_tlas_s);
} }
self.raw self.inner.raw
} }
} }
@ -463,8 +462,12 @@ impl PendingWrites {
.map_err(|e| device.handle_hal_error(e))?; .map_err(|e| device.handle_hal_error(e))?;
let encoder = EncoderInFlight { let encoder = EncoderInFlight {
raw: mem::replace(&mut self.command_encoder, new_encoder), inner: crate::command::CommandEncoder {
cmd_buffers: vec![cmd_buf], raw: mem::replace(&mut self.command_encoder, new_encoder),
list: vec![cmd_buf],
is_open: false,
hal_label: None,
},
trackers: Tracker::new(), trackers: Tracker::new(),
pending_buffers, pending_buffers,
pending_textures, pending_textures,
@ -1226,7 +1229,7 @@ impl Queue {
// execute resource transitions // execute resource transitions
if let Err(e) = unsafe { if let Err(e) = unsafe {
baked.encoder.begin_encoding(hal_label( baked.encoder.raw.begin_encoding(hal_label(
Some("(wgpu internal) Transit"), Some("(wgpu internal) Transit"),
self.device.instance_flags, self.device.instance_flags,
)) ))
@ -1253,21 +1256,21 @@ impl Queue {
//Note: stateless trackers are not merged: //Note: stateless trackers are not merged:
// device already knows these resources exist. // device already knows these resources exist.
CommandBuffer::insert_barriers_from_device_tracker( CommandBuffer::insert_barriers_from_device_tracker(
baked.encoder.as_mut(), baked.encoder.raw.as_mut(),
&mut trackers, &mut trackers,
&baked.trackers, &baked.trackers,
&snatch_guard, &snatch_guard,
); );
let transit = unsafe { baked.encoder.end_encoding().unwrap() }; let transit = unsafe { baked.encoder.raw.end_encoding().unwrap() };
baked.list.insert(0, transit); baked.encoder.list.insert(0, transit);
// Transition surface textures into `Present` state. // Transition surface textures into `Present` state.
// Note: we could technically do it after all of the command buffers, // 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. // but here we have a command encoder by hand, so it's easier to use it.
if !used_surface_textures.is_empty() { if !used_surface_textures.is_empty() {
if let Err(e) = unsafe { if let Err(e) = unsafe {
baked.encoder.begin_encoding(hal_label( baked.encoder.raw.begin_encoding(hal_label(
Some("(wgpu internal) Present"), Some("(wgpu internal) Present"),
self.device.instance_flags, self.device.instance_flags,
)) ))
@ -1284,17 +1287,16 @@ impl Queue {
) )
.collect::<Vec<_>>(); .collect::<Vec<_>>();
let present = unsafe { let present = unsafe {
baked.encoder.transition_textures(&texture_barriers); baked.encoder.raw.transition_textures(&texture_barriers);
baked.encoder.end_encoding().unwrap() baked.encoder.raw.end_encoding().unwrap()
}; };
baked.list.push(present); baked.encoder.list.push(present);
used_surface_textures = track::TextureUsageScope::default(); used_surface_textures = track::TextureUsageScope::default();
} }
// done // done
active_executions.push(EncoderInFlight { active_executions.push(EncoderInFlight {
raw: baked.encoder, inner: baked.encoder,
cmd_buffers: baked.list,
trackers: baked.trackers, trackers: baked.trackers,
pending_buffers: FastHashMap::default(), pending_buffers: FastHashMap::default(),
pending_textures: FastHashMap::default(), pending_textures: FastHashMap::default(),
@ -1358,7 +1360,7 @@ impl Queue {
} }
let hal_command_buffers = active_executions let hal_command_buffers = active_executions
.iter() .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::<Vec<_>>(); .collect::<Vec<_>>();
{ {