clean up hook invocation
This commit is contained in:
@@ -1,4 +1,9 @@
|
|||||||
pub const c = @cImport({
|
pub usingnamespace @cImport({
|
||||||
|
@cDefine("CIMGUI_DEFINE_ENUMS_AND_STRUCTS", {});
|
||||||
|
@cInclude("cimgui.h");
|
||||||
|
});
|
||||||
|
|
||||||
|
pub const impl = @cImport({
|
||||||
@cDefine("CIMGUI_DEFINE_ENUMS_AND_STRUCTS", {});
|
@cDefine("CIMGUI_DEFINE_ENUMS_AND_STRUCTS", {});
|
||||||
@cInclude("cimgui.h");
|
@cInclude("cimgui.h");
|
||||||
|
|
||||||
|
14
src/main.zig
14
src/main.zig
@@ -11,6 +11,14 @@ pub const nu_options: nu.Options = .{
|
|||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
|
pub const nu_driver = nu.Window;
|
||||||
|
|
||||||
|
pub const nu_modules = .{
|
||||||
|
App,
|
||||||
|
nu.ImGui,
|
||||||
|
nu.Render,
|
||||||
|
};
|
||||||
|
|
||||||
pub fn main() !void {
|
pub fn main() !void {
|
||||||
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
|
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
|
||||||
defer _ = gpa.detectLeaks();
|
defer _ = gpa.detectLeaks();
|
||||||
@@ -37,9 +45,5 @@ pub fn main() !void {
|
|||||||
try App.init(alloc);
|
try App.init(alloc);
|
||||||
defer App.deinit();
|
defer App.deinit();
|
||||||
|
|
||||||
try nu.run(nu.Window, .{
|
try nu.run();
|
||||||
App,
|
|
||||||
nu.ImGui,
|
|
||||||
nu.Render,
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
45
src/nu.zig
45
src/nu.zig
@@ -12,31 +12,36 @@ pub const Options = struct {
|
|||||||
};
|
};
|
||||||
|
|
||||||
pub const options: Options = if (@hasDecl(root, "nu_options")) root.nu_options else .{};
|
pub const options: Options = if (@hasDecl(root, "nu_options")) root.nu_options else .{};
|
||||||
|
pub const modules = root.nu_modules;
|
||||||
|
pub const driver = root.nu_driver;
|
||||||
|
|
||||||
pub fn run(
|
fn invoke(func: anytype, args: anytype) !void {
|
||||||
driver: anytype,
|
if (@typeInfo(@TypeOf(func)).Fn.return_type) |R| {
|
||||||
modules: anytype,
|
switch (@typeInfo(R)) {
|
||||||
) !void {
|
.ErrorUnion => try @call(.auto, func, args),
|
||||||
|
.Void => @call(.auto, func, args),
|
||||||
|
else => {
|
||||||
|
@compileLog(func, @typeInfo(R));
|
||||||
|
@compileError("Invalid hook return type. Must be void or !void.");
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn invoke_hook(comptime name: []const u8, args: anytype) !void {
|
||||||
|
inline for (modules) |module| {
|
||||||
|
if (@hasDecl(module, name)) {
|
||||||
|
try invoke(@field(module, name), args);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn run() !void {
|
||||||
while (driver.next()) |events| {
|
while (driver.next()) |events| {
|
||||||
// todo event handler
|
// todo event handler
|
||||||
_ = events;
|
_ = events;
|
||||||
|
|
||||||
inline for (modules) |module| {
|
try invoke_hook("frame", .{});
|
||||||
if (@hasDecl(module, "frame")) {
|
|
||||||
if (@typeInfo(@TypeOf(module.frame)).Fn.return_type) |R| {
|
|
||||||
switch (@typeInfo(R)) {
|
|
||||||
.ErrorUnion => try module.frame(),
|
|
||||||
.Void => module.frame(),
|
|
||||||
else => {
|
|
||||||
@compileLog(module.frame, @typeInfo(R));
|
|
||||||
@compileError("frame must be void or !void.");
|
|
||||||
},
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
module.frame();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// todo fixed timestep
|
// todo fixed timestep
|
||||||
}
|
}
|
||||||
|
@@ -5,32 +5,34 @@ const std = @import("std");
|
|||||||
const vk = @import("vk");
|
const vk = @import("vk");
|
||||||
const nu = @import("../nu.zig");
|
const nu = @import("../nu.zig");
|
||||||
const au = @import("Render/au.zig");
|
const au = @import("Render/au.zig");
|
||||||
|
|
||||||
const Render = @import("Render.zig");
|
const Render = @import("Render.zig");
|
||||||
const Window = @import("Window.zig");
|
const Window = @import("Window.zig");
|
||||||
|
|
||||||
const im = @import("cimgui");
|
const im = @import("cimgui");
|
||||||
|
pub usingnamespace im;
|
||||||
|
|
||||||
pub fn loader_wrapper(procname: [*c]const u8, _: ?*anyopaque) callconv(.C) vk.PfnVoidFunction {
|
pub fn loader_wrapper(procname: [*c]const u8, _: ?*anyopaque) callconv(.C) vk.PfnVoidFunction {
|
||||||
return au.glfwGetInstanceProcAddress(au.I.handle, procname);
|
return au.glfwGetInstanceProcAddress(au.I.handle, procname);
|
||||||
}
|
}
|
||||||
|
|
||||||
var ctx: *im.c.ImGuiContext = undefined;
|
var ctx: *im.ImGuiContext = undefined;
|
||||||
var descriptor_pool: vk.DescriptorPool = undefined;
|
var descriptor_pool: vk.DescriptorPool = undefined;
|
||||||
|
|
||||||
pub fn init() !void {
|
pub fn init() !void {
|
||||||
ctx = im.c.igCreateContext(null) orelse {
|
ctx = im.igCreateContext(null) orelse {
|
||||||
return error.igCreateContextFailed;
|
return error.igCreateContextFailed;
|
||||||
};
|
};
|
||||||
errdefer im.c.igDestroyContext(ctx);
|
errdefer im.igDestroyContext(ctx);
|
||||||
|
|
||||||
if (!im.c.ImGui_ImplVulkan_LoadFunctions(loader_wrapper, null)) {
|
if (!im.impl.ImGui_ImplVulkan_LoadFunctions(loader_wrapper, null)) {
|
||||||
return error.igVulkanLoadFunctionsFailed;
|
return error.igVulkanLoadFunctionsFailed;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!im.c.ImGui_ImplGlfw_InitForVulkan(@ptrCast(Window.handle), true)) {
|
if (!im.impl.ImGui_ImplGlfw_InitForVulkan(@ptrCast(Window.handle), true)) {
|
||||||
return error.igGlfwInitFailed;
|
return error.igGlfwInitFailed;
|
||||||
}
|
}
|
||||||
errdefer im.c.ImGui_ImplGlfw_Shutdown();
|
errdefer im.impl.ImGui_ImplGlfw_Shutdown();
|
||||||
|
|
||||||
descriptor_pool = try au.D.createDescriptorPool(&vk.DescriptorPoolCreateInfo{
|
descriptor_pool = try au.D.createDescriptorPool(&vk.DescriptorPoolCreateInfo{
|
||||||
.flags = .{ .free_descriptor_set_bit = true },
|
.flags = .{ .free_descriptor_set_bit = true },
|
||||||
@@ -43,7 +45,7 @@ pub fn init() !void {
|
|||||||
}, null);
|
}, null);
|
||||||
errdefer au.D.destroyDescriptorPool(descriptor_pool, null);
|
errdefer au.D.destroyDescriptorPool(descriptor_pool, null);
|
||||||
|
|
||||||
if (im.c.ImGui_ImplVulkan_Init(@constCast(&im.c.ImGui_ImplVulkan_InitInfo{
|
if (im.impl.ImGui_ImplVulkan_Init(@constCast(&im.impl.ImGui_ImplVulkan_InitInfo{
|
||||||
.Instance = @ptrFromInt(@intFromEnum(au.I.handle)),
|
.Instance = @ptrFromInt(@intFromEnum(au.I.handle)),
|
||||||
.PhysicalDevice = @ptrFromInt(@intFromEnum(au.device_config.pdev)),
|
.PhysicalDevice = @ptrFromInt(@intFromEnum(au.device_config.pdev)),
|
||||||
.Device = @ptrFromInt(@intFromEnum(au.D.handle)),
|
.Device = @ptrFromInt(@intFromEnum(au.D.handle)),
|
||||||
@@ -68,9 +70,9 @@ pub fn init() !void {
|
|||||||
})) != true) {
|
})) != true) {
|
||||||
return error.igVulkanInitFailed;
|
return error.igVulkanInitFailed;
|
||||||
}
|
}
|
||||||
errdefer im.c.ImGui_ImplVulkan_Shutdown();
|
errdefer im.impl.ImGui_ImplVulkan_Shutdown();
|
||||||
|
|
||||||
if (!im.c.ImGui_ImplVulkan_CreateFontsTexture()) {
|
if (!im.impl.ImGui_ImplVulkan_CreateFontsTexture()) {
|
||||||
return error.igVulkanFontTextureFailed;
|
return error.igVulkanFontTextureFailed;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -79,27 +81,27 @@ pub fn init() !void {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn frame() void {
|
pub fn frame() void {
|
||||||
im.c.ImGui_ImplGlfw_NewFrame();
|
im.impl.ImGui_ImplGlfw_NewFrame();
|
||||||
im.c.ImGui_ImplVulkan_NewFrame();
|
im.impl.ImGui_ImplVulkan_NewFrame();
|
||||||
im.c.igNewFrame();
|
im.igNewFrame();
|
||||||
|
|
||||||
im.c.igShowDemoWindow(null);
|
im.igShowDemoWindow(null);
|
||||||
|
|
||||||
im.c.igEndFrame();
|
im.igEndFrame();
|
||||||
im.c.igRender();
|
im.igRender();
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn deinit() void {
|
pub fn deinit() void {
|
||||||
Render.remove_present_callback(present);
|
Render.remove_present_callback(present);
|
||||||
im.c.ImGui_ImplVulkan_Shutdown();
|
im.impl.ImGui_ImplVulkan_Shutdown();
|
||||||
au.D.destroyDescriptorPool(descriptor_pool, null);
|
au.D.destroyDescriptorPool(descriptor_pool, null);
|
||||||
im.c.ImGui_ImplGlfw_Shutdown();
|
im.impl.ImGui_ImplGlfw_Shutdown();
|
||||||
im.c.igDestroyContext(ctx);
|
im.igDestroyContext(ctx);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn present(cmd: au.CommandBufferProxy) void {
|
pub fn present(cmd: au.CommandBufferProxy) void {
|
||||||
im.c.ImGui_ImplVulkan_RenderDrawData(
|
im.impl.ImGui_ImplVulkan_RenderDrawData(
|
||||||
im.c.igGetDrawData(),
|
@ptrCast(im.igGetDrawData()),
|
||||||
@ptrFromInt(@intFromEnum(cmd.handle)),
|
@ptrFromInt(@intFromEnum(cmd.handle)),
|
||||||
null,
|
null,
|
||||||
);
|
);
|
||||||
|
@@ -1,90 +0,0 @@
|
|||||||
const std = @import("std");
|
|
||||||
const vk = @import("vk");
|
|
||||||
const im = @import("cimgui");
|
|
||||||
const au = @import("../au.zig");
|
|
||||||
const c = @import("../c.zig");
|
|
||||||
|
|
||||||
pub usingnamespace im.c;
|
|
||||||
|
|
||||||
pub fn loader_wrapper(procname: [*c]const u8, _: ?*anyopaque) callconv(.C) vk.PfnVoidFunction {
|
|
||||||
return c.glfwGetInstanceProcAddress(au.I.handle, procname);
|
|
||||||
}
|
|
||||||
|
|
||||||
var descriptor_pool: vk.DescriptorPool = undefined;
|
|
||||||
|
|
||||||
pub fn init(frames_in_flight: usize) !*im.c.ImGuiContext {
|
|
||||||
const ctx = im.c.igCreateContext(null) orelse return error.igCreateContextFailed;
|
|
||||||
errdefer im.c.igDestroyContext(ctx);
|
|
||||||
|
|
||||||
if (im.c.ImGui_ImplVulkan_LoadFunctions(loader_wrapper, null) != true) {
|
|
||||||
return error.igVulkanLoadFunctionsFailed;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (im.c.ImGui_ImplGlfw_InitForVulkan(@ptrCast(au.W.handle), true) != true) {
|
|
||||||
return error.igGlfwInitFailed;
|
|
||||||
}
|
|
||||||
errdefer im.c.ImGui_ImplGlfw_Shutdown();
|
|
||||||
|
|
||||||
descriptor_pool = try au.D.createDescriptorPool(&vk.DescriptorPoolCreateInfo{
|
|
||||||
.flags = .{ .free_descriptor_set_bit = true },
|
|
||||||
.pool_size_count = 1,
|
|
||||||
.p_pool_sizes = &.{vk.DescriptorPoolSize{ .descriptor_count = 32, .type = .combined_image_sampler }},
|
|
||||||
.max_sets = 32,
|
|
||||||
}, null);
|
|
||||||
errdefer au.D.destroyDescriptorPool(descriptor_pool, null);
|
|
||||||
|
|
||||||
if (im.c.ImGui_ImplVulkan_Init(@constCast(&im.c.ImGui_ImplVulkan_InitInfo{
|
|
||||||
.Instance = @ptrFromInt(@intFromEnum(au.I.handle)),
|
|
||||||
.PhysicalDevice = @ptrFromInt(@intFromEnum(au.device_config.pdev)),
|
|
||||||
.Device = @ptrFromInt(@intFromEnum(au.D.handle)),
|
|
||||||
.QueueFamily = au.device_config.family,
|
|
||||||
.Queue = @ptrFromInt(@intFromEnum(au.Q.handle)),
|
|
||||||
.DescriptorPool = @ptrFromInt(@intFromEnum(descriptor_pool)),
|
|
||||||
.RenderPass = null,
|
|
||||||
.MinImageCount = 2,
|
|
||||||
.ImageCount = @intCast(frames_in_flight),
|
|
||||||
.PipelineRenderingCreateInfo = @bitCast(vk.PipelineRenderingCreateInfo{
|
|
||||||
.view_mask = 0,
|
|
||||||
.depth_attachment_format = .undefined,
|
|
||||||
.stencil_attachment_format = .undefined,
|
|
||||||
.color_attachment_count = 1,
|
|
||||||
.p_color_attachment_formats = &.{au.device_config.format.format},
|
|
||||||
}),
|
|
||||||
.MSAASamples = 0,
|
|
||||||
.PipelineCache = null,
|
|
||||||
.Subpass = 0,
|
|
||||||
.UseDynamicRendering = true,
|
|
||||||
.Allocator = null,
|
|
||||||
})) != true) {
|
|
||||||
return error.igVulkanInitFailed;
|
|
||||||
}
|
|
||||||
errdefer im.c.ImGui_ImplVulkan_Shutdown();
|
|
||||||
|
|
||||||
if (im.c.ImGui_ImplVulkan_CreateFontsTexture() != true) {
|
|
||||||
return error.igVulkanFontTextureFailed;
|
|
||||||
}
|
|
||||||
|
|
||||||
return ctx;
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn deinit(ctx: *im.c.ImGuiContext) void {
|
|
||||||
im.c.ImGui_ImplVulkan_Shutdown();
|
|
||||||
au.D.destroyDescriptorPool(descriptor_pool, null);
|
|
||||||
im.c.ImGui_ImplGlfw_Shutdown();
|
|
||||||
im.c.igDestroyContext(ctx);
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn NewFrame() void {
|
|
||||||
im.c.ImGui_ImplGlfw_NewFrame();
|
|
||||||
im.c.ImGui_ImplVulkan_NewFrame();
|
|
||||||
im.c.igNewFrame();
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn EndFrame() void {
|
|
||||||
im.c.igEndFrame();
|
|
||||||
im.c.igRender();
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn Draw(cmd: au.CommandBufferProxy) void {
|
|
||||||
im.c.ImGui_ImplVulkan_RenderDrawData(im.c.igGetDrawData(), @ptrFromInt(@intFromEnum(cmd.handle)), null);
|
|
||||||
}
|
|
Reference in New Issue
Block a user