easy comptime hooks

This commit is contained in:
2024-07-09 23:35:40 -04:00
parent cb8684cbf3
commit cc1a9fdabc
5 changed files with 69 additions and 68 deletions

View File

@@ -30,33 +30,11 @@ pub const Options = struct {
var sc: au.SwapChain = undefined;
var flights: au.Flights = undefined;
const PresentCallback = *const fn (au.CommandBufferProxy) void;
var present_callbacks: std.ArrayList(PresentCallback) = undefined;
pub fn add_present_callback(cb: PresentCallback) !void {
if (std.mem.indexOfScalar(PresentCallback, present_callbacks.items, cb)) |_| {
return;
} else {
try present_callbacks.append(cb);
}
}
pub fn remove_present_callback(cb: PresentCallback) void {
if (std.mem.indexOfScalar(PresentCallback, present_callbacks.items, cb)) |idx| {
_ = present_callbacks.orderedRemove(idx);
} else {
return;
}
}
pub fn init(alloc: std.mem.Allocator) !void {
// todo pick apart au into helpers; not a sub-module filled with its own globals.
try au.init(alloc);
errdefer au.deinit();
present_callbacks = std.ArrayList(PresentCallback).init(alloc);
errdefer present_callbacks.deinit();
sc = try au.SwapChain.init(alloc);
errdefer sc.deinit();
@@ -64,7 +42,7 @@ pub fn init(alloc: std.mem.Allocator) !void {
errdefer flights.deinit();
}
pub fn frame() !void {
pub fn nu_frame() !void {
const flight: au.Flights.Flight = flights.next();
try flight.wait();
@@ -89,14 +67,11 @@ pub fn frame() !void {
try cmd.beginCommandBuffer(&.{ .flags = .{ .one_time_submit_bit = true } });
target.begin_rendering(cmd, render_area);
for (present_callbacks.items) |cb| {
cb(cmd);
}
// todo really don't like this.
// there should be some comptime means for a module to invoke hooks on other modules. eg there should be some
// "record" hook that for each module that gets called here; but if the render module is never added then that
// hook never gets called
// todo manage frame in flight state for each hook; pass the current flight in as context.
// will need some comptime -> anytype mapping.
try nu.invoke_hook("nu_render_present", .{cmd});
target.end_rendering(cmd);
try cmd.endCommandBuffer();
@@ -127,9 +102,11 @@ pub fn frame() !void {
}
}
pub fn nu_close() !void {
try au.D.deviceWaitIdle();
}
pub fn deinit() void {
present_callbacks.deinit();
au.D.deviceWaitIdle() catch {};
flights.deinit();
sc.deinit();
au.deinit();