refactor stub

This commit is contained in:
David Allemang
2024-07-09 13:19:56 -04:00
parent eaf97a306f
commit 1269018e61
8 changed files with 468 additions and 273 deletions

View File

@@ -1,287 +1,29 @@
const std = @import("std");
const vk = @import("vk");
const c = @import("c.zig");
const shaders = @import("shaders");
const Allocator = std.mem.Allocator;
const au = @import("au.zig");
const Uber = @import("Uber.zig");
const nu = @import("nu.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 } },
.{ .pos = .{ -0.5, 0.5, -0.5, 1.0 }, .color = .{ 0, 1, 0 } },
.{ .pos = .{ 0.5, -0.5, -0.5, 1.0 }, .color = .{ 0, 0, 1 } },
.{ .pos = .{ 0.5, 0.5, -0.5, 1.0 }, .color = .{ 1, 1, 0 } },
.{ .pos = .{ -0.5, -0.5, 0.5, 1.0 }, .color = .{ 1, 0, 0 } },
.{ .pos = .{ -0.5, 0.5, 0.5, 1.0 }, .color = .{ 0, 1, 0 } },
.{ .pos = .{ 0.5, -0.5, 0.5, 1.0 }, .color = .{ 0, 0, 1 } },
.{ .pos = .{ 0.5, 0.5, 0.5, 1.0 }, .color = .{ 1, 1, 0 } },
};
const indices = [_]Uber.Index{ 4, 5, 6, 6, 5, 7 };
const uniform = Uber.Uniform{
.mat = .{
0.5, 0.0, 0.0, 0.0,
0.0, 1.0, 0.0, 0.0,
0.0, 0.0, 1.0, 0.0,
0.0, 0.0, 0.0, 1.0,
},
};
fn record_render(
cmd: au.CommandBufferProxy,
uber: Uber,
area: vk.Rect2D, // render area, scissor, and viewport.
vertex_buffer: vk.Buffer,
index_buffer: vk.Buffer,
descriptor_set: vk.DescriptorSet,
) void {
cmd.setViewport(0, 1, &.{.{
.x = @floatFromInt(area.offset.x),
.y = @floatFromInt(area.offset.y),
.width = @floatFromInt(area.extent.width),
.height = @floatFromInt(area.extent.height),
.min_depth = 0,
.max_depth = 1,
}});
cmd.setScissor(0, 1, &.{area});
cmd.bindPipeline(.graphics, uber.pipeline);
cmd.bindDescriptorSets(.graphics, uber.layout, 0, 1, &.{descriptor_set}, 0, null);
cmd.bindVertexBuffers(0, 1, &.{vertex_buffer}, &.{0});
cmd.bindIndexBuffer(index_buffer, 0, .uint16);
cmd.drawIndexed(indices.len, 1, 0, 0, 0);
}
const App = @import("App.zig");
pub fn main() !void {
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
defer _ = gpa.detectLeaks();
const alloc = gpa.allocator();
try au.init(alloc);
defer au.deinit();
var window = try nu.Window.init(alloc, .{ .title = "Hello World" });
defer window.deinit();
{
const pdev_prop = au.I.getPhysicalDeviceProperties(au.device_config.pdev);
std.debug.print("Selected '{s}' in mode '{any}'\n", .{ pdev_prop.device_name, au.device_config.mode });
}
var render = try nu.Render.init(alloc, &window);
defer render.deinit();
var sc = try au.SwapChain.init(alloc);
defer sc.deinit();
var imgui = try nu.ImGui.init(alloc, &window, &render);
defer imgui.deinit();
var flights = try au.Flights.init(alloc, 3); // FRAMES IN FLIGHT
defer flights.deinit();
var app = try App.init(alloc, &render, &imgui);
defer app.deinit();
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 },
.pool_size_count = 1,
.p_pool_sizes = &.{
vk.DescriptorPoolSize{ .descriptor_count = 32, .type = .combined_image_sampler },
},
.max_sets = 32,
}, null);
defer au.D.destroyDescriptorPool(descriptorPool, null);
_ = try sc.rebuild();
const cache = try au.D.createPipelineCache(&vk.PipelineCacheCreateInfo{}, null);
defer au.D.destroyPipelineCache(cache, null);
const uber = try Uber.init(cache);
defer uber.deinit();
const vkalloc = au.VkAllocator.init();
const vertex_buffer = try au.D.createBuffer(&vk.BufferCreateInfo{
.size = @sizeOf(@TypeOf(vertices)),
.usage = .{ .vertex_buffer_bit = true },
.sharing_mode = .exclusive,
}, null);
defer au.D.destroyBuffer(vertex_buffer, null);
const vertex_memory = try vkalloc.alloc(
au.D.getBufferMemoryRequirements(vertex_buffer),
.{ .host_visible_bit = true, .host_coherent_bit = true },
);
defer vkalloc.free(vertex_memory);
try au.D.bindBufferMemory(vertex_buffer, vertex_memory, 0);
const vertex_data: *align(1) @TypeOf(vertices) =
@ptrCast(try au.D.mapMemory(vertex_memory, 0, vk.WHOLE_SIZE, .{}));
defer au.D.unmapMemory(vertex_memory);
vertex_data.* = vertices;
const index_buffer = try au.D.createBuffer(&vk.BufferCreateInfo{
.size = @sizeOf(@TypeOf(indices)),
.usage = .{ .index_buffer_bit = true },
.sharing_mode = .exclusive,
}, null);
defer au.D.destroyBuffer(index_buffer, null);
const index_memory = try vkalloc.alloc(
au.D.getBufferMemoryRequirements(index_buffer),
.{ .host_visible_bit = true, .host_coherent_bit = true },
);
defer vkalloc.free(index_memory);
try au.D.bindBufferMemory(index_buffer, index_memory, 0);
const index_data: *align(1) @TypeOf(indices) =
@ptrCast(try au.D.mapMemory(index_memory, 0, vk.WHOLE_SIZE, .{}));
defer au.D.unmapMemory(index_memory);
index_data.* = indices;
// todo ring buffer for frames in flight. need to use an offset when binding
// use dynamic offset - descriptor type VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC
const uniform_buffer = try au.D.createBuffer(&vk.BufferCreateInfo{
.size = @sizeOf(@TypeOf(uniform)),
.usage = .{ .uniform_buffer_bit = true },
.sharing_mode = .exclusive,
}, null);
defer au.D.destroyBuffer(uniform_buffer, null);
const uniform_memory = try vkalloc.alloc(
au.D.getBufferMemoryRequirements(uniform_buffer),
.{ .host_visible_bit = true, .host_coherent_bit = true },
);
defer vkalloc.free(uniform_memory);
try au.D.bindBufferMemory(uniform_buffer, uniform_memory, 0);
const uniform_data: *align(1) @TypeOf(uniform) =
@ptrCast(try au.D.mapMemory(uniform_memory, 0, vk.WHOLE_SIZE, .{}));
defer au.D.unmapMemory(uniform_memory);
uniform_data.* = uniform;
var descriptorSet: vk.DescriptorSet = undefined;
try au.D.allocateDescriptorSets(&vk.DescriptorSetAllocateInfo{
.descriptor_pool = descriptorPool,
.descriptor_set_count = 1,
.p_set_layouts = &.{uber.set_layout},
}, @ptrCast(&descriptorSet));
defer au.D.freeDescriptorSets(descriptorPool, 1, &.{descriptorSet}) catch unreachable; // todo handle this?
au.D.updateDescriptorSets(
1,
&.{
vk.WriteDescriptorSet{
.dst_set = descriptorSet,
.dst_binding = 0,
.dst_array_element = 0,
.descriptor_type = .uniform_buffer,
.descriptor_count = 1,
.p_image_info = undefined,
.p_texel_buffer_view = undefined,
.p_buffer_info = &.{
vk.DescriptorBufferInfo{
.buffer = uniform_buffer,
.offset = 0,
.range = vk.WHOLE_SIZE,
},
},
},
},
0,
null,
);
var prng = std.Random.Sfc64.init(std.crypto.random.int(u64));
const rand = prng.random();
while (!au.W.should_close()) {
ui.NewFrame();
ui.igShowMetricsWindow(null);
ui.EndFrame();
const flight = flights.next();
const events = if (au.W.focused())
au.poll_events()
else
au.wait_events_timeout(0.5);
for (events) |u| {
switch (u) {
.framebufferSize => sc.mark(),
.cursorPos, .windowPos, .windowSize, .windowRefresh => {},
else => |e| std.debug.print("{any}\n", .{e}),
}
}
_ = try au.D.waitForFences(1, &.{flight.fence}, vk.TRUE, std.math.maxInt(u64));
try au.D.resetFences(1, &.{flight.fence});
// TODO need to check the standard to see what happens to a fence or semaphore on OutOfDateKHR error.
// acquire, submit, and present.
while (true) {
_ = try sc.rebuild();
const target = sc.acquire(flight.acquire, .null_handle) catch |err| switch (err) {
error.OutOfDateKHR => {
sc.mark();
continue;
},
else => return err,
};
try au.D.resetCommandPool(flight.pool, .{});
var cmd = au.CommandBufferProxy.init(flight.cmd, au.D.wrapper);
try cmd.beginCommandBuffer(&.{ .flags = .{ .one_time_submit_bit = true } });
const render_area: vk.Rect2D = .{
.offset = .{ .x = 0, .y = 0 },
.extent = sc.cinfo.image_extent,
};
target.begin_rendering(cmd, render_area);
record_render(
cmd,
uber,
render_area,
vertex_buffer,
index_buffer,
descriptorSet,
);
ui.Draw(cmd);
target.end_rendering(cmd);
for (vertex_data) |*v| {
for (v.pos[0..2]) |*f| {
f.* += (rand.float(f32) - 0.5) * 0.001;
}
}
try cmd.endCommandBuffer();
try au.Q.submit(
1,
&.{
vk.SubmitInfo{
.wait_semaphore_count = 1,
.p_wait_semaphores = @ptrCast(&flight.acquire),
.p_wait_dst_stage_mask = @ptrCast(&vk.PipelineStageFlags{ .color_attachment_output_bit = true }),
.command_buffer_count = 1,
.p_command_buffers = @ptrCast(&cmd.handle),
.signal_semaphore_count = 1,
.p_signal_semaphores = @ptrCast(&flight.complete),
},
},
flight.fence,
);
if (sc.present(&.{flight.complete}, target)) |_| {
break;
} else |err| switch (err) {
error.OutOfDateKHR => {
_ = try au.D.waitForFences(1, &.{flight.fence}, vk.TRUE, std.math.maxInt(u64));
try au.D.resetFences(1, &.{flight.fence});
sc.mark();
continue;
},
else => return err,
}
}
}
try au.D.deviceWaitIdle();
try nu.run(&window, .{
&app,
&imgui,
&render,
});
}