Skip to content

Commit

Permalink
[progress #146] custom bindings
Browse files Browse the repository at this point in the history
  • Loading branch information
tiawl committed Apr 5, 2024
1 parent ef50517 commit b785f39
Show file tree
Hide file tree
Showing 11 changed files with 290 additions and 95 deletions.
1 change: 1 addition & 0 deletions build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ fn import (builder: *std.Build, exe: *std.Build.Step.Compile, profile: *const Pr
});
imgui.addImport ("c", c);
imgui.addImport ("glfw", glfw_module);
imgui.addImport ("vk", vk_module);

const build_options = profile.variables.createModule ();
const logger = builder.createModule (.{
Expand Down
5 changes: 5 additions & 0 deletions src/binding/glfw/glfw.zig
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ pub const Events = struct
{
c.glfwWaitEvents ();
}

pub fn poll () void
{
c.glfwPollEvents ();
}
};

pub const Monitor = struct
Expand Down
47 changes: 26 additions & 21 deletions src/binding/glfw/window.zig
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,6 @@ pub const Window = struct
{
handle: *c.GLFWwindow,

pub fn from (handle: *anyopaque) @This ()
{
return .{ .handle = @as (*c.GLFWwindow, @ptrCast (@alignCast (handle))), };
}

const Tag = enum
{
resizable,
Expand Down Expand Up @@ -86,22 +81,6 @@ pub const Window = struct
}
};

pub fn create (width: u32, height: u32, title: [*:0] const u8,
monitor: ?glfw.Monitor, share: ?@This (), hints: [] const glfw.Window.Hint) !@This ()
{
for (hints) |hint| c.glfwWindowHint (hint.tag (), @intFromEnum (std.meta.activeTag (hint)));
if (c.glfwCreateWindow (@as (c_int, @intCast (width)), @as (c_int, @intCast (height)),
&title [0], if (monitor) |m| m.handle else null, if (share) |w| w.handle else null)) |handle|
return from (handle);

return error.WindowInitFailed;
}

pub fn destroy (self: @This ()) void
{
c.glfwDestroyWindow (self.handle);
}

pub const UserPointer = struct
{
pub fn get (comptime T: type) !?*T
Expand Down Expand Up @@ -156,4 +135,30 @@ pub const Window = struct
};
};
};

pub fn create (width: u32, height: u32, title: [*:0] const u8,
monitor: ?glfw.Monitor, share: ?@This (), hints: [] const glfw.Window.Hint) !@This ()
{
for (hints) |hint| c.glfwWindowHint (hint.tag (), @intFromEnum (std.meta.activeTag (hint)));
if (c.glfwCreateWindow (@as (c_int, @intCast (width)), @as (c_int, @intCast (height)),
&title [0], if (monitor) |m| m.handle else null, if (share) |w| w.handle else null)) |handle|
return from (handle);

return error.WindowInitFailed;
}

pub fn destroy (self: @This ()) void
{
c.glfwDestroyWindow (self.handle);
}

pub fn from (handle: *anyopaque) @This ()
{
return .{ .handle = @as (*c.GLFWwindow, @ptrCast (@alignCast (handle))), };
}

pub fn shouldClose (self: @This ()) bool
{
return c.glfwWindowShouldClose (self.handle) == c.GLFW_TRUE;
}
};
165 changes: 163 additions & 2 deletions src/binding/imgui.zig
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,42 @@ const imgui = @This ();

pub const vk = struct
{
pub const DrawData = struct
{
pub const Ex = struct
{
pub fn render (draw_data: *const c.ImGui_DrawData, command_buffer: binding.vk.Command.Buffer, pipeline: binding.vk.Pipeline) void
{
c.cImGui_ImplVulkan_RenderDrawDataEx (draw_data, @ptrFromInt (@intFromEnum (command_buffer)), @ptrFromInt (@intFromEnum (pipeline)));
}
};
};

pub const FontsTexture = struct
{
pub fn create () !void
{
if (!c.cImGui_ImplVulkan_CreateFontsTexture ()) return error.ImGuiVulkanCreateFontsTexture;
}
};

pub const Frame = struct
{
pub fn new () void
{
c.cImGui_ImplVulkan_NewFrame ();
}
};

pub const InitInfo = struct
{
Instance: binding.vk.Instance,
PhysicalDevice: binding.vk.PhysicalDevice,
Device: binding.vk.Device,
QueueFamily: u32,
Queue: binding.vk.Queue,
PipelineCache: binding.vk.PipelineCache,
DescriptorPool: binding.vk.DescriptorPool,
PipelineCache: binding.vk.Pipeline.Cache,
DescriptorPool: binding.vk.Descriptor.Pool,
Subpass: u32,
MinImageCount: u32,
ImageCount: u32,
Expand All @@ -29,30 +56,129 @@ pub const vk = struct
Allocator: [*c] const binding.vk.AllocationCallbacks,
CheckVkResultFn: ?*const fn (c_int) callconv (binding.vk.call_conv) void,
};

fn loader (function_name: [*c] const u8, instance: ?*anyopaque) callconv (binding.vk.call_conv) ?*const fn () callconv (binding.vk.call_conv) void
{
return c.vkGetInstanceProcAddr (if (instance) |v| @as (c.VkInstance, @ptrCast (v)) else null, function_name);
}

pub fn load () !void
{
if (!c.cImGui_ImplVulkan_LoadFunctions (loader)) return error.ImGuiVulkanLoadFunctionsFailure;
}

pub fn init (init_info: *imgui.vk.InitInfo) !void
{
if (!c.cImGui_ImplVulkan_Init (@ptrCast (init_info))) return error.ImGuiVulkanInitFailure;
}

pub fn shutdown () void
{
imgui.cImGui_ImplVulkan_Shutdown ();
}
};

pub const glfw = struct
{
pub const Frame = struct
{
pub fn new () void
{
c.cImGui_ImplGlfw_NewFrame ();
}
};

pub fn init () !void
{
const window = try binding.glfw.Context.get ();
if (!c.cImGui_ImplGlfw_InitForVulkan (@ptrCast (window), true)) return error.ImGuiGlfwInitForVulkanFailure;
}

pub fn shutdown () void
{
imgui.cImGui_ImplGlfw_Shutdown ();
}
};

pub const Cond = c.ImGuiCond;

pub const Context = struct
{
pub fn create () !void
{
if (c.ImGui_CreateContext (null) == null) return error.ImGuiCreateContextFailure;
}

pub fn destroy () void
{
c.ImGui_DestroyContext (null);
}
};

pub const Col = struct
{
pub const WindowBg = c.ImGuiCol_WindowBg;
};

pub const DrawData = struct
{
pub fn get () *const c.ImDrawData
{
return &(c.ImGui_GetDrawData ().*);
}
};

pub const Ex = struct
{
pub fn button (label: [] const u8, size: imgui.Vec2) bool
{
return c.ImGui_ButtonEx (label, size);
}

pub fn sameline (offset_from_start_x: f32, spacing: f32) void
{
c.ImGui_SameLineEx (offset_from_start_x, spacing);
}
};

pub const Frame = struct
{
pub fn new () void
{
c.ImGui_NewFrame ();
}
};

pub const IO = struct
{
pub fn get () *const c.ImGuiIO
{
return &(c.ImGui_GetIO ().*);
}
};

pub const NextWindow = struct
{
pub const Pos = struct
{
pub const Ex = struct
{
pub fn set (pos: imgui.Vec2, cond: imgui.Cond, pivot: imgui.Vec2) void
{
c.ImGui_SetNextWindowPosEx (pos, cond, pivot);
}
};
};

pub const Size = struct
{
pub fn set (size: imgui.Vec2, cond: imgui.Cond) void
{
c.ImGui_SetNextWindowSize (size, cond);
}
};
};

pub const Style = struct
{
pub fn colorsDark () void
Expand Down Expand Up @@ -95,3 +221,38 @@ pub const Style = struct
}
}
};

pub const Vec2 = c.ImVec2;

pub const Window = struct
{
pub const Flags = c.ImGuiWindowFlags_;

pub const Bit = enum (imgui.Window.Flags)
{
NO_COLLAPSE = c.ImGuiWindowFlags_NoCollapse,
NO_MOVE = c.ImGuiWindowFlags_NoMove,
NO_RESIZE = c.ImGuiWindowFlags_NoResize,
NO_TITLE_BAR = c.ImGuiWindowFlags_NoTitleBar,
};
};

pub fn begin (name: [] const u8, p_open: ?*bool, flags: imgui.Window.Flags) !void
{
if (!imgui.ImGui_Begin (name, p_open, flags)) return error.ImGuiBeginFailure;
}

pub fn end () void
{
imgui.ImGui_End ();
}

pub fn render () void
{
imgui.ImGui_Render ();
}

pub fn text (comptime fmt: [] const u8, args: anytype) void
{
c.ImGui_Text (std.debug.comptimePrint (fmt, args));
}
15 changes: 15 additions & 0 deletions src/binding/vk/command.zig
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,11 @@ pub const Command = extern struct
};
};

pub const Reset = extern struct
{
pub const Flags = u32;
};

pub fn create (device: vk.Device, p_create_info: *const vk.Command.Pool.Create.Info, p_allocator: ?*const vk.AllocationCallbacks) !@This ()
{
var command_pool: @This () = undefined;
Expand All @@ -146,5 +151,15 @@ pub const Command = extern struct
{
raw.prototypes.device.vkDestroyCommandPool (device, command_pool, p_allocator);
}

pub fn reset (command_pool: @This (), device: vk.Device, flags: vk.Command.Pool.Reset.Flags) !void
{
const result = raw.prototypes.device.vkResetCommandPool (device, command_pool, flags);
if (result > 0)
{
std.debug.print ("{s} failed with {} status code\n", .{ @typeName (@This ()) ++ "." ++ @src ().fn_name, result, });
return error.UnexpectedResult;
}
}
};
};
10 changes: 10 additions & 0 deletions src/binding/vk/device.zig
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,16 @@ pub const Device = enum (usize)
return device;
}

pub fn waitIdle (device: @This ()) !void
{
const result = raw.prototypes.device.vkDeviceWaitIdle (device);
if (result > 0)
{
std.debug.print ("{s} failed with {} status code\n", .{ @typeName (@This ()) ++ "." ++ @src ().fn_name, result, });
return error.UnexpectedResult;
}
}

pub fn destroy (self: @This (), p_allocator: ?*const vk.AllocationCallbacks) void
{
raw.prototypes.device.vkDestroyDevice (self, p_allocator);
Expand Down
1 change: 1 addition & 0 deletions src/binding/vk/format.zig
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ pub const Format = enum (u32)
R8G8B8_UNORM = c.VK_FORMAT_R8G8B8_UNORM,
R8G8B8A8_UNORM = c.VK_FORMAT_R8G8B8A8_UNORM,
R32G32_SFLOAT = c.VK_FORMAT_R32G32_SFLOAT,
UNDEFINED = c.VK_FORMAT_UNDEFINED,
_,

pub const Feature = extern struct
Expand Down
6 changes: 3 additions & 3 deletions src/context.zig
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ pub const Context = struct
const framebuffer = try self.glfw.get_framebuffer_size ();
try self.vk.init (self.imgui, .{ .width = framebuffer.width, .height = framebuffer.height, });

try self.logger.app ("init OK", .DEBUG, .{});
try self.logger.app (.DEBUG, "init OK", .{});
return self;
}

Expand All @@ -45,14 +45,14 @@ pub const Context = struct
const framebuffer = self.glfw.get_framebuffer_size ();
try self.vk.loop (&(self.imgui), .{ .resized = framebuffer.resized, .width = framebuffer.width, .height = framebuffer.height, }, &arena, &allocator, options);
}
try self.logger.app ("loop OK", .DEBUG, .{});
try self.logger.app (.DEBUG, "loop OK", .{});
}

pub fn cleanup (self: @This ()) !void
{
self.imgui.cleanup ();
try self.vk.cleanup ();
try self.glfw.cleanup ();
try self.logger.app ("cleanup OK", .DEBUG, .{});
try self.logger.app (.DEBUG, "cleanup OK", .{});
}
};
3 changes: 1 addition & 2 deletions src/glfw/context.zig
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,7 @@ pub const Context = struct

pub fn looping (self: @This ()) bool
{
const close_window = self.window.shouldClose ();
return !close_window;
return !self.window.shouldClose ();
}

pub fn loop (self: @This ()) !void
Expand Down
Loading

0 comments on commit b785f39

Please sign in to comment.