From: Joshua Simmons Date: Sat, 5 Nov 2022 10:00:23 +0000 (+0100) Subject: Improve lifetimes command buffer tokens X-Git-Url: https://git.nega.tv//gitweb.cgi?a=commitdiff_plain;h=0479d0855951e36f5d1897892437607b421d8759;p=josh%2Fnarcissus Improve lifetimes command buffer tokens Not binding to the lifetime of the thread token meant you could create multiple mut references to the same command buffer with `command_buffer_mut`. Relax requirements for mut references on the command buffer tokens in the API as they're not required. --- diff --git a/narcissus-gpu/src/lib.rs b/narcissus-gpu/src/lib.rs index b256b0f..b235c2e 100644 --- a/narcissus-gpu/src/lib.rs +++ b/narcissus-gpu/src/lib.rs @@ -295,36 +295,31 @@ pub trait Device { &self, frame_token: &FrameToken, thread_token: &mut ThreadToken, - command_buffer_token: &mut CommandBufferToken, + command_buffer_token: &CommandBufferToken, pipeline: Pipeline, layout: BindGroupLayout, bind_group_index: u32, bindings: &[Bind], ); - fn cmd_set_pipeline(&self, command_buffer_token: &mut CommandBufferToken, pipeline: Pipeline); + fn cmd_set_pipeline(&self, command_buffer_token: &CommandBufferToken, pipeline: Pipeline); fn cmd_begin_rendering( &self, frame_token: &FrameToken, thread_token: &mut ThreadToken, - command_buffer_token: &mut CommandBufferToken, + command_buffer_token: &CommandBufferToken, desc: &RenderingDesc, ); - fn cmd_end_rendering(&self, command_buffer_token: &mut CommandBufferToken); + fn cmd_end_rendering(&self, command_buffer_token: &CommandBufferToken); - fn cmd_set_viewports( - &self, - command_buffer_token: &mut CommandBufferToken, - viewports: &[Viewport], - ); - - fn cmd_set_scissors(&self, command_buffer_token: &mut CommandBufferToken, scissors: &[Scissor]); + fn cmd_set_viewports(&self, command_buffer_token: &CommandBufferToken, viewports: &[Viewport]); + fn cmd_set_scissors(&self, command_buffer_token: &CommandBufferToken, scissors: &[Scissor]); fn cmd_draw( &self, - command_buffer_token: &mut CommandBufferToken, + command_buffer_token: &CommandBufferToken, vertex_count: u32, instance_count: u32, first_vertex: u32, diff --git a/narcissus-gpu/src/vulkan.rs b/narcissus-gpu/src/vulkan.rs index d0d29bb..8517b62 100644 --- a/narcissus-gpu/src/vulkan.rs +++ b/narcissus-gpu/src/vulkan.rs @@ -355,11 +355,11 @@ struct VulkanFrame { } impl VulkanFrame { - fn command_buffer_mut<'token>( + fn command_buffer_mut<'a>( &self, - thread_token: &'token mut ThreadToken, - command_buffer_token: &CommandBufferToken, - ) -> &'token mut VulkanCommandBuffer { + thread_token: &'a mut ThreadToken, + command_buffer_token: &'a CommandBufferToken, + ) -> &'a mut VulkanCommandBuffer { let command_buffer_pool = self.command_buffer_pools.get_mut(thread_token); &mut command_buffer_pool.command_buffers[command_buffer_token.index] } @@ -1927,7 +1927,7 @@ impl<'driver> Device for VulkanDevice<'driver> { &self, frame_token: &FrameToken, thread_token: &mut ThreadToken, - command_buffer_token: &mut CommandBufferToken, + command_buffer_token: &CommandBufferToken, pipeline: Pipeline, layout: BindGroupLayout, bind_group_index: u32, @@ -2057,7 +2057,7 @@ impl<'driver> Device for VulkanDevice<'driver> { } } - fn cmd_set_pipeline(&self, command_buffer_token: &mut CommandBufferToken, pipeline: Pipeline) { + fn cmd_set_pipeline(&self, command_buffer_token: &CommandBufferToken, pipeline: Pipeline) { let command_buffer = vk::CommandBuffer::from_raw(command_buffer_token.raw); let VulkanPipeline { pipeline, @@ -2074,7 +2074,7 @@ impl<'driver> Device for VulkanDevice<'driver> { &self, frame_token: &FrameToken, thread_token: &mut ThreadToken, - command_buffer_token: &mut CommandBufferToken, + command_buffer_token: &CommandBufferToken, desc: &crate::RenderingDesc, ) { let frame = self.frame(frame_token); @@ -2189,14 +2189,14 @@ impl<'driver> Device for VulkanDevice<'driver> { } } - fn cmd_end_rendering(&self, command_buffer_token: &mut CommandBufferToken) { + fn cmd_end_rendering(&self, command_buffer_token: &CommandBufferToken) { let command_buffer = vk::CommandBuffer::from_raw(command_buffer_token.raw); unsafe { self.device_fn.cmd_end_rendering(command_buffer) } } fn cmd_set_viewports( &self, - command_buffer_token: &mut CommandBufferToken, + command_buffer_token: &CommandBufferToken, viewports: &[crate::Viewport], ) { let command_buffer = vk::CommandBuffer::from_raw(command_buffer_token.raw); @@ -2210,7 +2210,7 @@ impl<'driver> Device for VulkanDevice<'driver> { fn cmd_set_scissors( &self, - command_buffer_token: &mut CommandBufferToken, + command_buffer_token: &CommandBufferToken, scissors: &[crate::Scissor], ) { let command_buffer = vk::CommandBuffer::from_raw(command_buffer_token.raw); @@ -2224,7 +2224,7 @@ impl<'driver> Device for VulkanDevice<'driver> { fn cmd_draw( &self, - command_buffer_token: &mut CommandBufferToken, + command_buffer_token: &CommandBufferToken, vertex_count: u32, instance_count: u32, first_vertex: u32, @@ -2246,14 +2246,14 @@ impl<'driver> Device for VulkanDevice<'driver> { &self, frame_token: &FrameToken, thread_token: &mut ThreadToken, - mut command_buffer_token: CommandBufferToken, + command_buffer_token: CommandBufferToken, ) { let fence = self.universal_queue_fence.fetch_add(1, Ordering::SeqCst) + 1; let frame = self.frame(frame_token); frame.universal_queue_fence.store(fence, Ordering::Relaxed); - let command_buffer = frame.command_buffer_mut(thread_token, &mut command_buffer_token); + let command_buffer = frame.command_buffer_mut(thread_token, &command_buffer_token); for &(image, _) in command_buffer.swapchains_touched.values() { // transition swapchain image from attachment optimal to present src diff --git a/narcissus/src/main.rs b/narcissus/src/main.rs index db27801..52de126 100644 --- a/narcissus/src/main.rs +++ b/narcissus/src/main.rs @@ -216,13 +216,12 @@ pub fn main() { let (width, height, swapchain_image) = device.acquire_swapchain(&frame_token, window, TextureFormat::BGRA8_SRGB); - let mut command_buffer_token = - device.create_command_buffer(&frame_token, &mut thread_token); + let command_buffer_token = device.create_command_buffer(&frame_token, &mut thread_token); device.cmd_begin_rendering( &frame_token, &mut thread_token, - &mut command_buffer_token, + &command_buffer_token, &RenderingDesc { x: 0, y: 0, @@ -240,11 +239,11 @@ pub fn main() { }, ); - device.cmd_set_pipeline(&mut command_buffer_token, pipeline); + device.cmd_set_pipeline(&command_buffer_token, pipeline); device.cmd_set_bind_group( &frame_token, &mut thread_token, - &mut command_buffer_token, + &command_buffer_token, pipeline, bind_group_layout, 0, @@ -256,7 +255,7 @@ pub fn main() { ); device.cmd_set_scissors( - &mut command_buffer_token, + &command_buffer_token, &[Scissor { x: 0, y: 0, @@ -265,7 +264,7 @@ pub fn main() { }], ); device.cmd_set_viewports( - &mut command_buffer_token, + &command_buffer_token, &[Viewport { x: 0.0, y: 0.0, @@ -275,8 +274,8 @@ pub fn main() { max_depth: 1.0, }], ); - device.cmd_draw(&mut command_buffer_token, 3, 1, 0, 0); - device.cmd_end_rendering(&mut command_buffer_token); + device.cmd_draw(&command_buffer_token, 3, 1, 0, 0); + device.cmd_end_rendering(&command_buffer_token); device.submit(&frame_token, &mut thread_token, command_buffer_token);