runtime hooks

This commit is contained in:
David Allemang
2024-07-10 13:06:44 -04:00
parent e297865e93
commit fd1bd9dbf5
6 changed files with 138 additions and 51 deletions

View File

@@ -6,6 +6,7 @@ const vk = @import("vk");
const nu = @import("../nu.zig");
const au = @import("Render/au.zig");
const Hook = @import("hooks.zig").Hook;
const Render = @import("Render.zig");
const Window = @import("Window.zig");
@@ -14,6 +15,11 @@ pub usingnamespace im;
pub const Options = struct {};
pub const Hooks = struct {
pub const Frame = Hook(fn () void);
frame: Frame,
};
pub fn loader_wrapper(procname: [*c]const u8, _: ?*anyopaque) callconv(.C) vk.PfnVoidFunction {
return au.glfwGetInstanceProcAddress(au.I.handle, procname);
}
@@ -21,7 +27,9 @@ pub fn loader_wrapper(procname: [*c]const u8, _: ?*anyopaque) callconv(.C) vk.Pf
var ctx: *im.ImGuiContext = undefined;
var descriptor_pool: vk.DescriptorPool = undefined;
pub fn init() !void {
pub var hooks: Hooks = undefined;
pub fn init(alloc: std.mem.Allocator) !void {
ctx = im.igCreateContext(null) orelse {
return error.igCreateContextFailed;
};
@@ -77,6 +85,24 @@ pub fn init() !void {
if (!im.impl.ImGui_ImplVulkan_CreateFontsTexture()) {
return error.igVulkanFontTextureFailed;
}
hooks = .{
.frame = Hooks.Frame.init(alloc),
};
errdefer hooks.frame.deinit();
}
pub fn deinit() void {
hooks.frame.deinit();
im.impl.ImGui_ImplVulkan_Shutdown();
au.D.destroyDescriptorPool(descriptor_pool, null);
im.impl.ImGui_ImplGlfw_Shutdown();
im.igDestroyContext(ctx);
}
pub fn connect() !void {
try nu.hooks.frame.register(nu_frame);
try Render.hooks.present.register(nu_render_present);
}
pub fn nu_frame() !void {
@@ -84,7 +110,7 @@ pub fn nu_frame() !void {
im.impl.ImGui_ImplVulkan_NewFrame();
im.igNewFrame();
try nu.invoke_hook("nu_imgui_frame", .{});
hooks.frame.invoke(.{});
im.igEndFrame();
im.igRender();
@@ -97,10 +123,3 @@ pub fn nu_render_present(cmd: au.CommandBufferProxy) void {
null,
);
}
pub fn deinit() void {
im.impl.ImGui_ImplVulkan_Shutdown();
au.D.destroyDescriptorPool(descriptor_pool, null);
im.impl.ImGui_ImplGlfw_Shutdown();
im.igDestroyContext(ctx);
}

View File

@@ -9,6 +9,8 @@ const vk = @import("vk");
const nu = @import("../nu.zig");
const au = @import("Render/au.zig");
const Hook = @import("hooks.zig").Hook;
pub const Options = struct {
app_name: [*:0]const u8 = "nu-au-app",
app_version: struct {
@@ -27,6 +29,14 @@ pub const Options = struct {
frames_in_flight: u8 = 3,
};
pub const Hooks = struct {
pub const Present = Hook(fn (au.CommandBufferProxy) void);
present: Present,
};
pub var hooks: Hooks = undefined;
var sc: au.SwapChain = undefined;
var flights: au.Flights = undefined;
@@ -40,6 +50,21 @@ pub fn init(alloc: std.mem.Allocator) !void {
flights = try au.Flights.init(alloc, nu.options.render.frames_in_flight);
errdefer flights.deinit();
hooks = .{ .present = Hooks.Present.init(alloc) };
errdefer hooks.present.deinit();
}
pub fn deinit() void {
hooks.present.deinit();
flights.deinit();
sc.deinit();
au.deinit();
}
pub fn connect() !void {
try nu.hooks.frame.register(nu_frame);
try nu.hooks.close.register(nu_close);
}
pub fn nu_frame() !void {
@@ -70,7 +95,7 @@ pub fn nu_frame() !void {
// 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});
hooks.present.invoke(.{cmd});
target.end_rendering(cmd);
try cmd.endCommandBuffer();
@@ -102,12 +127,8 @@ pub fn nu_frame() !void {
}
}
pub fn nu_close() !void {
try au.D.deviceWaitIdle();
}
pub fn deinit() void {
flights.deinit();
sc.deinit();
au.deinit();
pub fn nu_close() void {
au.D.deviceWaitIdle() catch |err| {
std.debug.panic("Device wait failed: {!}", .{err});
};
}

View File

@@ -21,7 +21,7 @@ pub const Options = struct {
x11_instance_name: [*:0]const u8 = "floating_window",
};
var bus: Bus = undefined;
var bus: Bus = undefined; // todo bus should probably move to engine.
pub var handle: *c.GLFWwindow = undefined;
var unfocused_rate: f32 = 1.0 / 20.0;