extract imgui setup
This commit is contained in:
@@ -3,8 +3,9 @@ const vkgen = @import("vulkan-zig");
|
||||
|
||||
pub fn build(b: *std.Build) void {
|
||||
const target = b.standardTargetOptions(.{});
|
||||
|
||||
const optimize = b.standardOptimizeOption(.{});
|
||||
const optimize = b.standardOptimizeOption(.{
|
||||
.preferred_optimize_mode = .ReleaseSafe,
|
||||
});
|
||||
|
||||
const vk = b.dependency("vulkan-zig", .{
|
||||
.registry = @as([]const u8, b.pathFromRoot("reg/vk.xml")),
|
||||
|
12
src/au.zig
12
src/au.zig
@@ -32,7 +32,7 @@ pub const device_extensions: []const [*:0]const u8 = &.{
|
||||
};
|
||||
|
||||
pub const app_info: vk.ApplicationInfo = .{
|
||||
.p_application_name = "hey tildes!",
|
||||
.p_application_name = "zig-glfw-vulkan",
|
||||
.application_version = vk.makeApiVersion(0, 0, 0, 0),
|
||||
.p_engine_name = "zig-glfw-vulkan",
|
||||
.engine_version = vk.makeApiVersion(0, 0, 0, 0),
|
||||
@@ -209,6 +209,13 @@ const CandidateDeviceInfo = struct {
|
||||
|
||||
res.pdev = pdev;
|
||||
|
||||
const props = I.getPhysicalDeviceProperties(pdev);
|
||||
score += switch (props.device_type) {
|
||||
vk.PhysicalDeviceType.discrete_gpu => 1000,
|
||||
vk.PhysicalDeviceType.integrated_gpu => 500,
|
||||
else => 0,
|
||||
};
|
||||
|
||||
var format_count: u32 = undefined;
|
||||
_ = try I.getPhysicalDeviceSurfaceFormatsKHR(pdev, W.surface, &format_count, null);
|
||||
if (format_count == 0) return error.NoSurfaceFormats;
|
||||
@@ -233,8 +240,11 @@ const CandidateDeviceInfo = struct {
|
||||
defer alloc.free(modes);
|
||||
_ = try I.getPhysicalDeviceSurfacePresentModesKHR(pdev, W.surface, &mode_count, modes.ptr);
|
||||
|
||||
std.debug.print("Modes ({s}): {any}\n", .{props.device_name, modes});
|
||||
|
||||
if (std.mem.indexOfAny(vk.PresentModeKHR, modes, &.{
|
||||
vk.PresentModeKHR.mailbox_khr,
|
||||
vk.PresentModeKHR.immediate_khr,
|
||||
})) |idx| {
|
||||
res.mode = modes[idx];
|
||||
} else {
|
||||
|
90
src/au/ui.zig
Normal file
90
src/au/ui.zig
Normal file
@@ -0,0 +1,90 @@
|
||||
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);
|
||||
}
|
61
src/main.zig
61
src/main.zig
@@ -5,10 +5,10 @@ const shaders = @import("shaders");
|
||||
const Allocator = std.mem.Allocator;
|
||||
|
||||
const au = @import("au.zig");
|
||||
const im = @import("cimgui");
|
||||
|
||||
const Uber = @import("Uber.zig");
|
||||
|
||||
const ui = @import("au/ui.zig");
|
||||
|
||||
const vertices = [_]Uber.Vertex{
|
||||
// Vulkan depth range is 0, 1 instead of OpenGL -1, 1
|
||||
.{ .pos = .{ -0.5, -0.5, -0.5, 1.0 }, .color = .{ 1, 0, 0 } },
|
||||
@@ -69,14 +69,22 @@ pub fn main() !void {
|
||||
try au.init(alloc);
|
||||
defer au.deinit();
|
||||
|
||||
{
|
||||
const props = au.I.getPhysicalDeviceProperties(au.device_config.pdev);
|
||||
std.debug.print(
|
||||
"Selected Device:\n {s}\n mode: {}\n",
|
||||
.{ props.device_name, au.device_config.mode },
|
||||
);
|
||||
}
|
||||
|
||||
var sc = try au.SwapChain.init(alloc);
|
||||
defer sc.deinit();
|
||||
|
||||
var flights = try au.Flights.init(alloc, 3); // FRAMES IN FLIGHT
|
||||
defer flights.deinit();
|
||||
|
||||
const ctx = im.c.igCreateContext(null) orelse return error.igCreateContextFailed;
|
||||
defer im.c.igDestroyContext(ctx);
|
||||
const ctx = try ui.init(flights.flights.len);
|
||||
defer ui.deinit(ctx);
|
||||
|
||||
const descriptorPool = try au.D.createDescriptorPool(&vk.DescriptorPoolCreateInfo{
|
||||
.flags = .{ .free_descriptor_set_bit = true },
|
||||
@@ -88,41 +96,8 @@ pub fn main() !void {
|
||||
}, null);
|
||||
defer au.D.destroyDescriptorPool(descriptorPool, null);
|
||||
|
||||
_ = im.c.ImGui_ImplVulkan_LoadFunctions(loader_wrapper, null);
|
||||
_ = im.c.ImGui_ImplGlfw_InitForVulkan(@ptrCast(au.W.handle), true);
|
||||
defer im.c.ImGui_ImplGlfw_Shutdown();
|
||||
|
||||
_ = try sc.rebuild();
|
||||
|
||||
const prci: 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},
|
||||
};
|
||||
|
||||
var info: 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(descriptorPool)),
|
||||
.RenderPass = null,
|
||||
.MinImageCount = 2,
|
||||
.ImageCount = @intCast(flights.flights.len),
|
||||
.PipelineRenderingCreateInfo = @bitCast(prci),
|
||||
.MSAASamples = 0,
|
||||
.PipelineCache = null,
|
||||
.Subpass = 0,
|
||||
.UseDynamicRendering = true,
|
||||
.Allocator = null,
|
||||
};
|
||||
_ = im.c.ImGui_ImplVulkan_Init(&info);
|
||||
_ = im.c.ImGui_ImplVulkan_CreateFontsTexture();
|
||||
defer im.c.ImGui_ImplVulkan_Shutdown();
|
||||
|
||||
const cache = try au.D.createPipelineCache(&vk.PipelineCacheCreateInfo{}, null);
|
||||
defer au.D.destroyPipelineCache(cache, null);
|
||||
|
||||
@@ -130,6 +105,7 @@ pub fn main() !void {
|
||||
defer uber.deinit();
|
||||
|
||||
const vkalloc = au.VkAllocator.init();
|
||||
std.debug.print("heaps: {any}\ntypes: {any}\n", .{ vkalloc.heaps(), vkalloc.types() });
|
||||
|
||||
const vertex_buffer = try au.D.createBuffer(&vk.BufferCreateInfo{
|
||||
.size = @sizeOf(@TypeOf(vertices)),
|
||||
@@ -220,12 +196,9 @@ pub fn main() !void {
|
||||
const rand = prng.random();
|
||||
|
||||
while (!au.W.should_close()) {
|
||||
im.c.ImGui_ImplGlfw_NewFrame();
|
||||
im.c.ImGui_ImplVulkan_NewFrame();
|
||||
im.c.igNewFrame();
|
||||
im.c.igShowDemoWindow(null);
|
||||
im.c.igEndFrame();
|
||||
im.c.igRender();
|
||||
ui.NewFrame();
|
||||
ui.igShowMetricsWindow(null);
|
||||
ui.EndFrame();
|
||||
|
||||
const flight = flights.next();
|
||||
|
||||
@@ -267,7 +240,7 @@ pub fn main() !void {
|
||||
index_buffer,
|
||||
descriptorSet,
|
||||
);
|
||||
im.c.ImGui_ImplVulkan_RenderDrawData(im.c.igGetDrawData(), @ptrFromInt(@intFromEnum(cmd.handle)), null);
|
||||
ui.Draw(cmd);
|
||||
tgt.end_rendering(cmd);
|
||||
|
||||
for (vertex_data) |*v| {
|
||||
|
Reference in New Issue
Block a user