reuse command buffers

This commit is contained in:
2024-07-02 00:01:36 -04:00
parent 437a60bd5c
commit 6b2715eebe
2 changed files with 19 additions and 15 deletions

View File

@@ -11,6 +11,7 @@ pub fn Flights(T: type) type {
complete: vk.Semaphore = .null_handle, complete: vk.Semaphore = .null_handle,
fence: vk.Fence = .null_handle, fence: vk.Fence = .null_handle,
pool: vk.CommandPool = .null_handle, pool: vk.CommandPool = .null_handle,
cmd: vk.CommandBuffer = .null_handle,
ctx: T, ctx: T,
}; };
@@ -31,6 +32,11 @@ pub fn Flights(T: type) type {
flight.complete = try au.D.createSemaphore(&.{}, null); flight.complete = try au.D.createSemaphore(&.{}, null);
flight.fence = try au.D.createFence(&.{ .flags = .{ .signaled_bit = true } }, 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); 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(); 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.acquire, null);
au.D.destroySemaphore(flight.complete, null); au.D.destroySemaphore(flight.complete, null);
au.D.destroyFence(flight.fence, null); au.D.destroyFence(flight.fence, null);
au.D.freeCommandBuffers(flight.pool, 1, &.{flight.cmd});
au.D.destroyCommandPool(flight.pool, null); au.D.destroyCommandPool(flight.pool, null);
flight.ctx.deinit(); flight.ctx.deinit();
} }

View File

@@ -270,28 +270,20 @@ pub fn main() !void {
const image = sc.getImage(acq.image_index); const image = sc.getImage(acq.image_index);
const view = sc.getView(acq.image_index); const view = sc.getView(acq.image_index);
var render_cmd = au.CommandBufferProxy.init(.null_handle, au.D.wrapper); var cmd = au.CommandBufferProxy.init(flight.cmd, au.D.wrapper);
try au.D.allocateCommandBuffers(
&.{
.command_pool = flight.pool,
.level = .primary,
.command_buffer_count = 1,
},
@ptrCast(&render_cmd.handle),
);
try render_cmd.beginCommandBuffer(&.{ .flags = .{ .one_time_submit_bit = true } }); try cmd.beginCommandBuffer(&.{ .flags = .{ .one_time_submit_bit = true } });
try flight.ctx.record_render( try flight.ctx.record_render(
render_cmd, cmd,
image, image,
view, view,
vk.Rect2D{ .offset = .{ .x = 0, .y = 0 }, .extent = sc.cinfo.image_extent }, 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, 1,
&.{ &.{
vk.SubmitInfo{ vk.SubmitInfo{
@@ -299,13 +291,18 @@ pub fn main() !void {
.p_wait_semaphores = @ptrCast(&flight.acquire), .p_wait_semaphores = @ptrCast(&flight.acquire),
.p_wait_dst_stage_mask = @ptrCast(&vk.PipelineStageFlags{ .color_attachment_output_bit = true }), .p_wait_dst_stage_mask = @ptrCast(&vk.PipelineStageFlags{ .color_attachment_output_bit = true }),
.command_buffer_count = 1, .command_buffer_count = 1,
.p_command_buffers = @ptrCast(&render_cmd.handle), .p_command_buffers = @ptrCast(&cmd.handle),
.signal_semaphore_count = 1, .signal_semaphore_count = 1,
.p_signal_semaphores = @ptrCast(&flight.complete), .p_signal_semaphores = @ptrCast(&flight.complete),
}, },
}, },
flight.fence, 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{ _ = try au.Q.presentKHR(&vk.PresentInfoKHR{
.wait_semaphore_count = 1, .wait_semaphore_count = 1,