Skip to content

Commit

Permalink
Borrow the command encoder mutably for destruction instead of moving
Browse files Browse the repository at this point in the history
  • Loading branch information
kvark committed Jan 28, 2024
1 parent 815b7bf commit 421f303
Show file tree
Hide file tree
Showing 10 changed files with 35 additions and 38 deletions.
2 changes: 1 addition & 1 deletion blade-graphics/src/gles/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
{
Expand Down
2 changes: 1 addition & 1 deletion blade-graphics/src/metal/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
2 changes: 1 addition & 1 deletion blade-graphics/src/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
4 changes: 2 additions & 2 deletions blade-graphics/src/vulkan/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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);
};
}
Expand Down
13 changes: 6 additions & 7 deletions blade-render/src/util/frame_pacer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ pub struct FramePacer {
frame_index: usize,
prev_resources: FrameResources,
prev_sync_point: Option<blade_graphics::SyncPoint>,
command_encoder: Option<blade_graphics::CommandEncoder>,
command_encoder: blade_graphics::CommandEncoder,
next_resources: FrameResources,
}

Expand All @@ -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(),
}
}
Expand All @@ -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.
Expand Down
17 changes: 8 additions & 9 deletions examples/bunnymark/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ struct Sprite {

struct Example {
pipeline: gpu::RenderPipeline,
command_encoder: Option<gpu::CommandEncoder>,
command_encoder: gpu::CommandEncoder,
prev_sync_point: Option<gpu::SyncPoint>,
texture: gpu::Texture,
view: gpu::TextureView,
Expand Down Expand Up @@ -169,7 +169,7 @@ impl Example {

Self {
pipeline,
command_encoder: Some(command_encoder),
command_encoder,
prev_sync_point: None,
texture,
view,
Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -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);
}
Expand All @@ -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);
}
}

Expand Down
2 changes: 1 addition & 1 deletion examples/init/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
2 changes: 1 addition & 1 deletion examples/mini/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
24 changes: 11 additions & 13 deletions examples/particle/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use blade_graphics as gpu;
mod particle;

struct Example {
command_encoder: Option<gpu::CommandEncoder>,
command_encoder: gpu::CommandEncoder,
prev_sync_point: Option<gpu::SyncPoint>,
context: gpu::Context,
gui_painter: blade_egui::GuiPainter,
Expand Down Expand Up @@ -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,
Expand All @@ -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);
}
Expand All @@ -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),
Expand All @@ -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() {
Expand Down
5 changes: 3 additions & 2 deletions examples/ray-query/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 421f303

Please sign in to comment.