From 6b2715eebe848c5becec32076e57f9f78e15609a Mon Sep 17 00:00:00 2001 From: David Allemang Date: Tue, 2 Jul 2024 00:01:36 -0400 Subject: [PATCH] reuse command buffers --- src/au/flights.zig | 7 +++++++ src/main.zig | 27 ++++++++++++--------------- 2 files changed, 19 insertions(+), 15 deletions(-) diff --git a/src/au/flights.zig b/src/au/flights.zig index 65350a9..86825c3 100644 --- a/src/au/flights.zig +++ b/src/au/flights.zig @@ -11,6 +11,7 @@ pub fn Flights(T: type) type { complete: vk.Semaphore = .null_handle, fence: vk.Fence = .null_handle, pool: vk.CommandPool = .null_handle, + cmd: vk.CommandBuffer = .null_handle, ctx: T, }; @@ -31,6 +32,11 @@ pub fn Flights(T: type) type { flight.complete = try au.D.createSemaphore(&.{}, null); flight.fence = try au.D.createFence(&.{ .flags = .{ .signaled_bit = true } }, null); flight.pool = try au.D.createCommandPool(&.{ .queue_family_index = au.device_config.family }, null); + try au.D.allocateCommandBuffers(&vk.CommandBufferAllocateInfo{ + .command_buffer_count = 1, + .command_pool = flight.pool, + .level = .primary, + }, @ptrCast(&flight.cmd)); flight.ctx = try T.init(); } @@ -42,6 +48,7 @@ pub fn Flights(T: type) type { au.D.destroySemaphore(flight.acquire, null); au.D.destroySemaphore(flight.complete, null); au.D.destroyFence(flight.fence, null); + au.D.freeCommandBuffers(flight.pool, 1, &.{flight.cmd}); au.D.destroyCommandPool(flight.pool, null); flight.ctx.deinit(); } diff --git a/src/main.zig b/src/main.zig index 7582341..1f6ad39 100644 --- a/src/main.zig +++ b/src/main.zig @@ -270,28 +270,20 @@ pub fn main() !void { const image = sc.getImage(acq.image_index); const view = sc.getView(acq.image_index); - var render_cmd = au.CommandBufferProxy.init(.null_handle, au.D.wrapper); - try au.D.allocateCommandBuffers( - &.{ - .command_pool = flight.pool, - .level = .primary, - .command_buffer_count = 1, - }, - @ptrCast(&render_cmd.handle), - ); + var cmd = au.CommandBufferProxy.init(flight.cmd, au.D.wrapper); - try render_cmd.beginCommandBuffer(&.{ .flags = .{ .one_time_submit_bit = true } }); + try cmd.beginCommandBuffer(&.{ .flags = .{ .one_time_submit_bit = true } }); try flight.ctx.record_render( - render_cmd, + cmd, image, view, vk.Rect2D{ .offset = .{ .x = 0, .y = 0 }, .extent = sc.cinfo.image_extent }, ); - try render_cmd.endCommandBuffer(); + try cmd.endCommandBuffer(); - try au.Q.submit( + au.Q.submit( 1, &.{ vk.SubmitInfo{ @@ -299,13 +291,18 @@ pub fn main() !void { .p_wait_semaphores = @ptrCast(&flight.acquire), .p_wait_dst_stage_mask = @ptrCast(&vk.PipelineStageFlags{ .color_attachment_output_bit = true }), .command_buffer_count = 1, - .p_command_buffers = @ptrCast(&render_cmd.handle), + .p_command_buffers = @ptrCast(&cmd.handle), .signal_semaphore_count = 1, .p_signal_semaphores = @ptrCast(&flight.complete), }, }, flight.fence, - ); + ) catch { + std.debug.print("Failed to submit.\nWaiting for idle...", .{}); + au.D.deviceWaitIdle() catch + std.debug.print("deviceWaitIdle failed\n", .{}); + @panic("Submission failed"); + }; _ = try au.Q.presentKHR(&vk.PresentInfoKHR{ .wait_semaphore_count = 1,