submit command

This commit is contained in:
2024-06-27 21:08:07 -04:00
parent 1f82923f59
commit 7b80ef4dbf
2 changed files with 88 additions and 44 deletions

View File

@@ -6,7 +6,8 @@ const Self = @This();
alloc: std.mem.Allocator, alloc: std.mem.Allocator,
cinfo: vk.SwapchainCreateInfoKHR, cinfo: vk.SwapchainCreateInfoKHR,
handle: vk.SwapchainKHR, handle: vk.SwapchainKHR = .null_handle,
images: std.ArrayListUnmanaged(vk.Image) = .{},
pub fn init(alloc: std.mem.Allocator) !Self { pub fn init(alloc: std.mem.Allocator) !Self {
const caps = try au.I.getPhysicalDeviceSurfaceCapabilitiesKHR(au.device_config.pdev, au.W.surface); const caps = try au.I.getPhysicalDeviceSurfaceCapabilitiesKHR(au.device_config.pdev, au.W.surface);
@@ -36,11 +37,11 @@ pub fn init(alloc: std.mem.Allocator) !Self {
.clipped = vk.TRUE, .clipped = vk.TRUE,
.old_swapchain = .null_handle, .old_swapchain = .null_handle,
}, },
.handle = .null_handle,
}; };
} }
pub fn deinit(self: *Self) void { pub fn deinit(self: *Self) void {
self.images.deinit(self.alloc);
au.D.destroySwapchainKHR(self.handle, null); au.D.destroySwapchainKHR(self.handle, null);
} }
@@ -62,7 +63,16 @@ pub fn rebuild(self: *Self) !bool {
au.D.destroySwapchainKHR(self.cinfo.old_swapchain, null); au.D.destroySwapchainKHR(self.cinfo.old_swapchain, null);
self.cinfo.old_swapchain = self.handle; self.cinfo.old_swapchain = self.handle;
var count: u32 = undefined;
_ = try au.D.getSwapchainImagesKHR(self.handle, &count, null);
try self.images.resize(self.alloc, count);
_ = try au.D.getSwapchainImagesKHR(self.handle, &count, self.images.items.ptr);
// todo repopulate images and synchronization // todo repopulate images and synchronization
return true; return true;
} }
pub fn get(self: Self, idx: u32) vk.Image {
return self.images.items[idx];
}

View File

@@ -105,39 +105,67 @@ pub fn main() !void {
const pool = try au.D.createCommandPool(&.{ .queue_family_index = au.device_config.family }, null); const pool = try au.D.createCommandPool(&.{ .queue_family_index = au.device_config.family }, null);
defer au.D.destroyCommandPool(pool, null); defer au.D.destroyCommandPool(pool, null);
const fence: vk.Fence = try au.D.createFence(&.{ .flags = .{ .signaled_bit = true } }, null);
defer au.D.destroyFence(fence, null);
const sem_ready: vk.Semaphore = try au.D.createSemaphore(&.{}, null); const sem_ready: vk.Semaphore = try au.D.createSemaphore(&.{}, null);
defer au.D.destroySemaphore(sem_ready, null); defer au.D.destroySemaphore(sem_ready, null);
const sem_done: vk.Semaphore = try au.D.createSemaphore(&.{}, null); const sem_done: vk.Semaphore = try au.D.createSemaphore(&.{}, null);
defer au.D.destroySemaphore(sem_done, null); defer au.D.destroySemaphore(sem_done, null);
const cmdbufs: [1]vk.CommandBuffer = undefined; const fence: vk.Fence = try au.D.createFence(&.{ .flags = .{ .signaled_bit = true } }, null);
try au.D.allocateCommandBuffers( defer au.D.destroyFence(fence, null);
&.{ .command_pool = pool, .command_buffer_count = @intCast(cmdbufs.len), .level = .primary },
cmdbufs.ptr,
);
defer au.D.freeCommandBuffers(pool, @intCast(cmdbufs.len), cmdbufs.ptr);
{ while (!au.W.should_close()) {
const cmd = au.CommandBufferProxy.init(cmdbufs[0], au.D); // todo switch mode depending on if window is focused
const clear = vk.ClearValue{ .color = .{ .float_32 = .{ 0, 0, 0, 1 } } }; const events = au.wait_events_timeout(0.10);
const viewport = vk.Viewport{
.x = 0, for (events) |u| {
.y = 0, switch (u) {
.width = sc.cinfo.image_extent.width, .framebufferSize => sc.mark(),
.height = sc.cinfo.image_extent.height, .cursorPos, .windowPos, .windowSize, .windowRefresh => {},
.min_depth = 0, else => |e| std.debug.print("{any}\n", .{e}),
.max_depth = 1, }
}; }
const scissor = vk.Rect2D{ .offset = .{ .x = 0, .y = 0 }, .extent = sc.cinfo.image_extent };
_ = try sc.rebuild();
_ = try au.D.waitForFences(1, &.{fence}, vk.TRUE, std.math.maxInt(u64));
try au.D.resetFences(1, &.{fence});
try au.D.resetCommandPool(pool, .{});
const acq = try au.D.acquireNextImageKHR(
sc.handle,
std.math.maxInt(u64),
sem_ready,
.null_handle,
);
const image = sc.get(acq.image_index);
var cmd = au.CommandBufferProxy.init(.null_handle, au.D.wrapper);
try au.D.allocateCommandBuffers(&.{
.command_pool = pool,
.level = .primary,
.command_buffer_count = 1,
}, @ptrCast(&cmd.handle));
try cmd.beginCommandBuffer(&.{ .flags = .{ .one_time_submit_bit = true } });
// const clear = vk.ClearValue{ .color = .{ .float_32 = .{ 0, 0, 0, 1 } } };
// const viewport = vk.Viewport{
// .x = 0,
// .y = 0,
// .width = sc.cinfo.image_extent,
// .height = sc.cinfo.image_extent.height,
// .min_depth = 0,
// .max_depth = 1,
// };
// const scissor = vk.Rect2D{
// .offset = .{ .x = 0, .y = 0 },
// .extent = sc.cinfo.image_extent,
// };
try cmd.beginCommandBuffer(&.{});
cmd.pipelineBarrier( cmd.pipelineBarrier(
.{ .top_of_pipe_bit = true }, .{ .top_of_pipe_bit = true },
.{ .color_attachment_output_bit = true }, .{ .bottom_of_pipe_bit = true },
.{}, .{},
0, 0,
null, null,
@@ -146,31 +174,38 @@ pub fn main() !void {
1, 1,
@ptrCast(&vk.ImageMemoryBarrier{ @ptrCast(&vk.ImageMemoryBarrier{
.src_access_mask = .{}, .src_access_mask = .{},
.dst_access_mask = .{ .color_attachment_write_bit = true }, .dst_access_mask = .{},
.old_layout = .undefined, .old_layout = .undefined,
.new_layout = .color_attachment_optimal, .new_layout = .present_src_khr,
.src_queue_family_index = 0, .src_queue_family_index = 0,
.dst_queue_family_index = 0, .dst_queue_family_index = 0,
.image = .null_handle, // todo this needs to point to the swapchain image, so I can't get away from recording a command buffer for each one. .image = image,
.subresource_range = .{
.aspect_mask = .{ .color_bit = true },
.base_mip_level = 0,
.level_count = 1,
.base_array_layer = 0,
.layer_count = 1,
},
}), }),
); );
} try cmd.endCommandBuffer();
while (!au.W.should_close()) { try au.Q.submit(
// todo switch mode depending on if window is focused 1,
const events = au.wait_events_timeout(0.10); @ptrCast(&vk.SubmitInfo{
.wait_semaphore_count = 1,
.p_wait_semaphores = @ptrCast(&sem_ready),
.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(&sem_done),
}),
fence,
);
for (events) |u| switch (u) { _ = try au.Q.presentKHR(&vk.PresentInfoKHR{
.framebufferSize => sc.mark(),
.cursorPos, .windowPos, .windowSize, .windowRefresh => {},
else => |e| std.debug.print("{any}\n", .{e}),
};
_ = try sc.rebuild();
const acq = try au.D.acquireNextImageKHR(sc.handle, std.math.maxInt(u64), sem_ready, .null_handle);
const pre = try au.Q.presentKHR(&vk.PresentInfoKHR{
.wait_semaphore_count = 1, .wait_semaphore_count = 1,
.p_wait_semaphores = &.{sem_done}, .p_wait_semaphores = &.{sem_done},
.swapchain_count = 1, .swapchain_count = 1,
@@ -178,7 +213,6 @@ pub fn main() !void {
.p_image_indices = &.{acq.image_index}, .p_image_indices = &.{acq.image_index},
.p_results = null, .p_results = null,
}); });
std.debug.print("present result: {}\n", .{pre});
} }
try au.D.deviceWaitIdle(); try au.D.deviceWaitIdle();