]> git.nega.tv - josh/narcissus/commitdiff
Improve lifetimes command buffer tokens
authorJoshua Simmons <josh@nega.tv>
Sat, 5 Nov 2022 10:00:23 +0000 (11:00 +0100)
committerJoshua Simmons <josh@nega.tv>
Sat, 5 Nov 2022 10:29:40 +0000 (11:29 +0100)
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.

narcissus-gpu/src/lib.rs
narcissus-gpu/src/vulkan.rs
narcissus/src/main.rs

index b256b0f055b4a9262d77cd830698c7b3189cdd12..b235c2e5e6832c73eacec8e53ea02c9fccf4e51d 100644 (file)
@@ -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,
index d0d29bbfb329a8451690eb222109073f69534464..8517b6243095cffe7df509a39437fbd6f0f96f15 100644 (file)
@@ -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
index db27801e1d18f3a83ed7655fdcba11c832623e79..52de1269a2b5c6ccf2d40d8622e89f52b6ba909d 100644 (file)
@@ -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);