extract generic au.Flights

This commit is contained in:
2024-06-28 21:43:30 -04:00
parent cd9d77c24a
commit bc4421b754
3 changed files with 63 additions and 28 deletions

View File

@@ -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,

57
src/au/flights.zig Normal file
View File

@@ -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];
}
};
}

View File

@@ -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,