From 1ab353a07647ca5f93f6e72c340c5ef9bf317800 Mon Sep 17 00:00:00 2001 From: Dzmitry Malyshau Date: Sun, 28 Jan 2024 01:17:49 -0800 Subject: [PATCH] Borrow the command encoder mutably for destruction instead of moving --- blade-graphics/src/gles/mod.rs | 2 +- blade-graphics/src/metal/mod.rs | 2 +- blade-graphics/src/traits.rs | 2 +- blade-graphics/src/vulkan/mod.rs | 4 ++-- blade-render/src/util/frame_pacer.rs | 13 ++++++------- examples/bunnymark/main.rs | 17 ++++++++--------- examples/init/main.rs | 2 +- examples/mini/main.rs | 2 +- examples/particle/main.rs | 24 +++++++++++------------- examples/ray-query/main.rs | 5 +++-- 10 files changed, 35 insertions(+), 38 deletions(-) diff --git a/blade-graphics/src/gles/mod.rs b/blade-graphics/src/gles/mod.rs index f9cc1899..36ad63f1 100644 --- a/blade-graphics/src/gles/mod.rs +++ b/blade-graphics/src/gles/mod.rs @@ -381,7 +381,7 @@ impl crate::traits::CommandDevice for Context { } } - fn destroy_command_encoder(&self, _command_encoder: CommandEncoder) {} + fn destroy_command_encoder(&self, _command_encoder: &mut CommandEncoder) {} fn submit(&self, encoder: &mut CommandEncoder) -> SyncPoint { { diff --git a/blade-graphics/src/metal/mod.rs b/blade-graphics/src/metal/mod.rs index 3e18f6ec..54319a6b 100644 --- a/blade-graphics/src/metal/mod.rs +++ b/blade-graphics/src/metal/mod.rs @@ -422,7 +422,7 @@ impl crate::traits::CommandDevice for Context { } } - fn destroy_command_encoder(&self, _command_encoder: CommandEncoder) {} + fn destroy_command_encoder(&self, _command_encoder: &mut CommandEncoder) {} fn submit(&self, encoder: &mut CommandEncoder) -> SyncPoint { let cmd_buf = encoder.raw.take().unwrap(); diff --git a/blade-graphics/src/traits.rs b/blade-graphics/src/traits.rs index 3ecfb0a0..43e14a1d 100644 --- a/blade-graphics/src/traits.rs +++ b/blade-graphics/src/traits.rs @@ -27,7 +27,7 @@ pub trait CommandDevice { type SyncPoint: Clone + Debug; fn create_command_encoder(&self, desc: super::CommandEncoderDesc) -> Self::CommandEncoder; - fn destroy_command_encoder(&self, encoder: Self::CommandEncoder); + fn destroy_command_encoder(&self, encoder: &mut Self::CommandEncoder); fn submit(&self, encoder: &mut Self::CommandEncoder) -> Self::SyncPoint; fn wait_for(&self, sp: &Self::SyncPoint, timeout_ms: u32) -> bool; } diff --git a/blade-graphics/src/vulkan/mod.rs b/blade-graphics/src/vulkan/mod.rs index 37d25c30..0772970c 100644 --- a/blade-graphics/src/vulkan/mod.rs +++ b/blade-graphics/src/vulkan/mod.rs @@ -380,7 +380,7 @@ impl crate::traits::CommandDevice for Context { } } - fn destroy_command_encoder(&self, command_encoder: CommandEncoder) { + fn destroy_command_encoder(&self, command_encoder: &mut CommandEncoder) { for cmd_buf in command_encoder.buffers.iter() { let raw_cmd_buffers = [cmd_buf.raw]; unsafe { @@ -397,7 +397,7 @@ impl crate::traits::CommandDevice for Context { .core .destroy_command_pool(command_encoder.pool, None) }; - if let Some(crash_handler) = command_encoder.crash_handler { + if let Some(crash_handler) = command_encoder.crash_handler.take() { self.destroy_buffer(crash_handler.marker_buf); }; } diff --git a/blade-render/src/util/frame_pacer.rs b/blade-render/src/util/frame_pacer.rs index 41938dee..2228c9c9 100644 --- a/blade-render/src/util/frame_pacer.rs +++ b/blade-render/src/util/frame_pacer.rs @@ -8,7 +8,7 @@ pub struct FramePacer { frame_index: usize, prev_resources: FrameResources, prev_sync_point: Option, - command_encoder: Option, + command_encoder: blade_graphics::CommandEncoder, next_resources: FrameResources, } @@ -22,7 +22,7 @@ impl FramePacer { frame_index: 0, prev_resources: FrameResources::default(), prev_sync_point: None, - command_encoder: Some(encoder), + command_encoder: encoder, next_resources: FrameResources::default(), } } @@ -46,17 +46,16 @@ impl FramePacer { pub fn destroy(&mut self, context: &blade_graphics::Context) { self.wait_for_previous_frame(context); - context.destroy_command_encoder(self.command_encoder.take().unwrap()); + context.destroy_command_encoder(&mut self.command_encoder); } pub fn begin_frame(&mut self) -> (&mut blade_graphics::CommandEncoder, &mut FrameResources) { - let encoder = self.command_encoder.as_mut().unwrap(); - encoder.start(); - (encoder, &mut self.next_resources) + self.command_encoder.start(); + (&mut self.command_encoder, &mut self.next_resources) } pub fn end_frame(&mut self, context: &blade_graphics::Context) -> &blade_graphics::SyncPoint { - let sync_point = context.submit(self.command_encoder.as_mut().unwrap()); + let sync_point = context.submit(&mut self.command_encoder); self.frame_index += 1; // Wait for the previous frame immediately - this ensures that we are // only processing one frame at a time, and yet not stalling. diff --git a/examples/bunnymark/main.rs b/examples/bunnymark/main.rs index 2d762d10..ef3a8ab3 100644 --- a/examples/bunnymark/main.rs +++ b/examples/bunnymark/main.rs @@ -39,7 +39,7 @@ struct Sprite { struct Example { pipeline: gpu::RenderPipeline, - command_encoder: Option, + command_encoder: gpu::CommandEncoder, prev_sync_point: Option, texture: gpu::Texture, view: gpu::TextureView, @@ -169,7 +169,7 @@ impl Example { Self { pipeline, - command_encoder: Some(command_encoder), + command_encoder, prev_sync_point: None, texture, view, @@ -223,11 +223,10 @@ impl Example { fn render(&mut self) { let frame = self.context.acquire_frame(); - let encoder = self.command_encoder.as_mut().unwrap(); - encoder.start(); - encoder.init_texture(frame.texture()); + self.command_encoder.start(); + self.command_encoder.init_texture(frame.texture()); - if let mut pass = encoder.render(gpu::RenderTargetSet { + if let mut pass = self.command_encoder.render(gpu::RenderTargetSet { colors: &[gpu::RenderTarget { view: frame.texture_view(), init_op: gpu::InitOp::Clear(gpu::TextureColor::TransparentBlack), @@ -259,8 +258,8 @@ impl Example { rc.draw(0, 4, 0, 1); } } - encoder.present(frame); - let sync_point = self.context.submit(encoder); + self.command_encoder.present(frame); + let sync_point = self.context.submit(&mut self.command_encoder); if let Some(sp) = self.prev_sync_point.take() { self.context.wait_for(&sp, !0); } @@ -273,7 +272,7 @@ impl Example { } self.context.destroy_texture(self.texture); self.context - .destroy_command_encoder(self.command_encoder.take().unwrap()); + .destroy_command_encoder(&mut self.command_encoder); } } diff --git a/examples/init/main.rs b/examples/init/main.rs index 5d6b713c..200a8d25 100644 --- a/examples/init/main.rs +++ b/examples/init/main.rs @@ -223,7 +223,7 @@ fn main() { let sync_point = context.submit(&mut command_encoder); context.wait_for(&sync_point, !0); - context.destroy_command_encoder(command_encoder); + context.destroy_command_encoder(&mut command_encoder); for buffer in temp_buffers { context.destroy_buffer(buffer); } diff --git a/examples/mini/main.rs b/examples/mini/main.rs index bbb4797e..8a8f3c07 100644 --- a/examples/mini/main.rs +++ b/examples/mini/main.rs @@ -167,7 +167,7 @@ fn main() { let answer = unsafe { *(result_buffer.data() as *mut u32) }; println!("Output: 0x{:x}", answer); - context.destroy_command_encoder(command_encoder); + context.destroy_command_encoder(&mut command_encoder); context.destroy_buffer(result_buffer); context.destroy_buffer(upload_buffer); for view in views { diff --git a/examples/particle/main.rs b/examples/particle/main.rs index 294a3df1..2b45eb3d 100644 --- a/examples/particle/main.rs +++ b/examples/particle/main.rs @@ -5,7 +5,7 @@ use blade_graphics as gpu; mod particle; struct Example { - command_encoder: Option, + command_encoder: gpu::CommandEncoder, prev_sync_point: Option, context: gpu::Context, gui_painter: blade_egui::GuiPainter, @@ -54,7 +54,7 @@ impl Example { let sync_point = context.submit(&mut command_encoder); Self { - command_encoder: Some(command_encoder), + command_encoder, prev_sync_point: Some(sync_point), context, gui_painter, @@ -66,8 +66,8 @@ impl Example { if let Some(sp) = self.prev_sync_point.take() { self.context.wait_for(&sp, !0); } - let encoder = self.command_encoder.take().unwrap(); - self.context.destroy_command_encoder(encoder); + self.context + .destroy_command_encoder(&mut self.command_encoder); self.gui_painter.destroy(&self.context); self.particle_system.destroy(&self.context); } @@ -79,17 +79,15 @@ impl Example { screen_desc: &blade_egui::ScreenDescriptor, ) { let frame = self.context.acquire_frame(); - let encoder = self.command_encoder.as_mut().unwrap(); - - encoder.start(); - encoder.init_texture(frame.texture()); + self.command_encoder.start(); + self.command_encoder.init_texture(frame.texture()); self.gui_painter - .update_textures(encoder, gui_textures, &self.context); + .update_textures(&mut self.command_encoder, gui_textures, &self.context); - self.particle_system.update(encoder); + self.particle_system.update(&mut self.command_encoder); - if let mut pass = encoder.render(gpu::RenderTargetSet { + if let mut pass = self.command_encoder.render(gpu::RenderTargetSet { colors: &[gpu::RenderTarget { view: frame.texture_view(), init_op: gpu::InitOp::Clear(gpu::TextureColor::TransparentBlack), @@ -101,8 +99,8 @@ impl Example { self.gui_painter .paint(&mut pass, gui_primitives, screen_desc, &self.context); } - encoder.present(frame); - let sync_point = self.context.submit(encoder); + self.command_encoder.present(frame); + let sync_point = self.context.submit(&mut self.command_encoder); self.gui_painter.after_submit(&sync_point); if let Some(sp) = self.prev_sync_point.take() { diff --git a/examples/ray-query/main.rs b/examples/ray-query/main.rs index 464a0034..377e015e 100644 --- a/examples/ray-query/main.rs +++ b/examples/ray-query/main.rs @@ -245,11 +245,12 @@ impl Example { } } - fn delete(self) { + fn delete(mut self) { if let Some(sp) = self.prev_sync_point { self.context.wait_for(&sp, !0); } - self.context.destroy_command_encoder(self.command_encoder); + self.context + .destroy_command_encoder(&mut self.command_encoder); self.context.destroy_texture_view(self.target_view); self.context.destroy_texture(self.target); self.context.destroy_acceleration_structure(self.blas);