From bc4421b7541b895e273634beeb3bd0c8196101f3 Mon Sep 17 00:00:00 2001 From: David Allemang Date: Fri, 28 Jun 2024 21:43:30 -0400 Subject: [PATCH] extract generic au.Flights --- src/au.zig | 1 + src/au/flights.zig | 57 ++++++++++++++++++++++++++++++++++++++++++++++ src/main.zig | 33 ++++----------------------- 3 files changed, 63 insertions(+), 28 deletions(-) create mode 100644 src/au/flights.zig diff --git a/src/au.zig b/src/au.zig index 4cf299f..11a1815 100644 --- a/src/au.zig +++ b/src/au.zig @@ -6,6 +6,7 @@ const c = @import("c.zig"); pub const Bus = @import("au/Bus.zig"); pub const SwapChain = @import("au/SwapChain.zig"); +pub const Flights = @import("au/flights.zig").Flights; pub const use_debug_messenger = switch (builtin.mode) { .Debug, .ReleaseSafe => true, diff --git a/src/au/flights.zig b/src/au/flights.zig new file mode 100644 index 0000000..65350a9 --- /dev/null +++ b/src/au/flights.zig @@ -0,0 +1,57 @@ +const std = @import("std"); +const vk = @import("vk"); +const au = @import("../au.zig"); + +pub fn Flights(T: type) type { + return struct { + const Self = @This(); + + const Flight = struct { + acquire: vk.Semaphore = .null_handle, + complete: vk.Semaphore = .null_handle, + fence: vk.Fence = .null_handle, + pool: vk.CommandPool = .null_handle, + ctx: T, + }; + + alloc: std.mem.Allocator, + flights: []Flight, + idx: usize, + + pub fn init(alloc: std.mem.Allocator, n: usize) !Self { + var self: Self = .{ + .alloc = alloc, + .flights = try alloc.alloc(Flight, n), + .idx = 0, + }; + errdefer self.deinit(); + + for (self.flights) |*flight| { + flight.acquire = try au.D.createSemaphore(&.{}, null); + 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); + flight.ctx = try T.init(); + } + + return self; + } + + pub fn deinit(self: Self) void { + for (self.flights) |flight| { + au.D.destroySemaphore(flight.acquire, null); + au.D.destroySemaphore(flight.complete, null); + au.D.destroyFence(flight.fence, null); + au.D.destroyCommandPool(flight.pool, null); + flight.ctx.deinit(); + } + self.alloc.free(self.flights); + } + + pub fn next(self: *Self) Flight { + const idx = self.idx; + self.idx = (self.idx + 1) % self.flights.len; + return self.flights[idx]; + } + }; +} diff --git a/src/main.zig b/src/main.zig index a45bc89..a1169d5 100644 --- a/src/main.zig +++ b/src/main.zig @@ -50,13 +50,6 @@ const vertices = [_]Vertex{ const indices = [_]Index{ 4, 5, 6, 6, 5, 7 }; -const Flight = struct { - acquire: vk.Semaphore = .null_handle, - complete: vk.Semaphore = .null_handle, - fence: vk.Fence = .null_handle, - pool: vk.CommandPool = .null_handle, - frame: Frame, -}; const Frame = struct { pub fn init() !Frame { @@ -181,27 +174,11 @@ pub fn main() !void { var sc = try au.SwapChain.init(alloc); defer sc.deinit(); - const flights = try alloc.alloc(Flight, 3); // FRAMES IN FLIGHT - defer alloc.free(flights); + var flights = try au.Flights(Frame).init(alloc, 3); // FRAMES IN FLIGHT + defer flights.deinit(); - for (flights) |*flight| { - flight.acquire = try au.D.createSemaphore(&.{}, null); - 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); - flight.frame = try Frame.init(); - } - defer for (flights) |flight| { - au.D.destroySemaphore(flight.acquire, null); - au.D.destroySemaphore(flight.complete, null); - au.D.destroyFence(flight.fence, null); - au.D.destroyCommandPool(flight.pool, null); - flight.frame.deinit(); - }; - - var flight_idx: usize = 0; - while (!au.W.should_close()) : (flight_idx = (flight_idx + 1) % flights.len) { - const flight = flights[flight_idx]; + while (!au.W.should_close()) { + const flight = flights.next(); const events = if (au.W.focused()) au.wait_events_timeout(0.1) @@ -243,7 +220,7 @@ pub fn main() !void { try render_cmd.beginCommandBuffer(&.{ .flags = .{ .one_time_submit_bit = true } }); - try flight.frame.record_render( + try flight.ctx.record_render( render_cmd, image, view,