diff --git a/src/au/SwapChain.zig b/src/au/SwapChain.zig index bca6d60..2d4e62e 100644 --- a/src/au/SwapChain.zig +++ b/src/au/SwapChain.zig @@ -89,98 +89,114 @@ pub fn rebuild(self: *Self) !bool { } }, null); } - // todo repopulate images and synchronization - return true; } -pub fn getImage(self: Self, idx: u32) vk.Image { - return self.images.items[idx]; +pub fn acquire(self: Self, semaphore: vk.Semaphore, fence: vk.Fence) !Target { + const acq = try au.D.acquireNextImageKHR(self.handle, std.math.maxInt(u64), semaphore, fence); + return .{ + .idx = acq.image_index, + .image = self.images.items[acq.image_index], + .view = self.views.items[acq.image_index], + }; } -pub fn getView(self: Self, idx: u32) vk.ImageView { - return self.views.items[idx]; -} +const Target = struct { + idx: u32, + image: vk.Image, + view: vk.ImageView, -pub fn beginRendering(self: Self, cmd: au.CommandBufferProxy, area: vk.Rect2D, idx: u32) void { - cmd.pipelineBarrier( - .{ .top_of_pipe_bit = true }, - .{ .color_attachment_output_bit = true }, - .{}, - 0, - null, - 0, - null, - 1, - &.{ - vk.ImageMemoryBarrier{ - .src_access_mask = .{}, - .dst_access_mask = .{ .color_attachment_write_bit = true }, - .old_layout = .undefined, - .new_layout = .color_attachment_optimal, - .src_queue_family_index = 0, - .dst_queue_family_index = 0, - .image = self.getImage(idx), - .subresource_range = .{ - .aspect_mask = .{ .color_bit = true }, - .base_mip_level = 0, - .level_count = 1, - .base_array_layer = 0, - .layer_count = 1, + pub fn begin_rendering(self: Target, cmd: au.CommandBufferProxy, area: vk.Rect2D) void { + cmd.pipelineBarrier( + .{ .top_of_pipe_bit = true }, + .{ .color_attachment_output_bit = true }, + .{}, + 0, + null, + 0, + null, + 1, + &.{ + vk.ImageMemoryBarrier{ + .src_access_mask = .{}, + .dst_access_mask = .{ .color_attachment_write_bit = true }, + .old_layout = .undefined, + .new_layout = .color_attachment_optimal, + .src_queue_family_index = 0, + .dst_queue_family_index = 0, + .image = self.image, + .subresource_range = .{ + .aspect_mask = .{ .color_bit = true }, + .base_mip_level = 0, + .level_count = 1, + .base_array_layer = 0, + .layer_count = 1, + }, }, }, - }, - ); + ); - cmd.beginRendering(&vk.RenderingInfo{ - .render_area = area, - .layer_count = 1, - .view_mask = 0, - .color_attachment_count = 1, - .p_color_attachments = &.{ - vk.RenderingAttachmentInfo{ - .image_view = self.getView(idx), - .image_layout = .color_attachment_optimal, - .resolve_mode = .{}, - .resolve_image_view = .null_handle, - .resolve_image_layout = .undefined, - .load_op = .clear, - .store_op = .store, - .clear_value = .{ .color = .{ .float_32 = .{ 0, 0, 0, 1 } } }, + cmd.beginRendering(&vk.RenderingInfo{ + .render_area = area, + .layer_count = 1, + .view_mask = 0, + .color_attachment_count = 1, + .p_color_attachments = &.{ + vk.RenderingAttachmentInfo{ + .image_view = self.view, + .image_layout = .color_attachment_optimal, + .resolve_mode = .{}, + .resolve_image_view = .null_handle, + .resolve_image_layout = .undefined, + .load_op = .clear, + .store_op = .store, + .clear_value = .{ .color = .{ .float_32 = .{ 0, 0, 0, 1 } } }, + }, }, - }, + }); + } + + pub fn end_rendering(self: Target, cmd: au.CommandBufferProxy) void { + cmd.endRendering(); + + cmd.pipelineBarrier( + .{ .color_attachment_output_bit = true }, + .{ .bottom_of_pipe_bit = true }, + .{}, + 0, + null, + 0, + null, + 1, + &.{ + vk.ImageMemoryBarrier{ + .src_access_mask = .{ .color_attachment_write_bit = true }, + .dst_access_mask = .{}, + .old_layout = .color_attachment_optimal, + .new_layout = .present_src_khr, + .src_queue_family_index = 0, + .dst_queue_family_index = 0, + .image = self.image, + .subresource_range = .{ + .aspect_mask = .{ .color_bit = true }, + .base_mip_level = 0, + .level_count = 1, + .base_array_layer = 0, + .layer_count = 1, + }, + }, + }, + ); + } +}; + +pub fn present(self: Self, wait_semaphores: []const vk.Semaphore, target: Target) !vk.Result { + return try au.Q.presentKHR(&vk.PresentInfoKHR{ + .wait_semaphore_count = @intCast(wait_semaphores.len), + .p_wait_semaphores = wait_semaphores.ptr, + .swapchain_count = 1, + .p_swapchains = &.{self.handle}, + .p_image_indices = &.{target.idx}, + .p_results = null, }); } - -pub fn endRendering(self: Self, cmd: au.CommandBufferProxy, idx: u32) void { - cmd.endRendering(); - - cmd.pipelineBarrier( - .{ .color_attachment_output_bit = true }, - .{ .bottom_of_pipe_bit = true }, - .{}, - 0, - null, - 0, - null, - 1, - &.{ - vk.ImageMemoryBarrier{ - .src_access_mask = .{ .color_attachment_write_bit = true }, - .dst_access_mask = .{}, - .old_layout = .color_attachment_optimal, - .new_layout = .present_src_khr, - .src_queue_family_index = 0, - .dst_queue_family_index = 0, - .image = self.getImage(idx), - .subresource_range = .{ - .aspect_mask = .{ .color_bit = true }, - .base_mip_level = 0, - .level_count = 1, - .base_array_layer = 0, - .layer_count = 1, - }, - }, - }, - ); -} diff --git a/src/main.zig b/src/main.zig index 6fd5d86..7808765 100644 --- a/src/main.zig +++ b/src/main.zig @@ -248,12 +248,7 @@ pub fn main() !void { try au.D.resetFences(1, &.{flight.fence}); try au.D.resetCommandPool(flight.pool, .{}); - const acq = try au.D.acquireNextImageKHR( - sc.handle, - std.math.maxInt(u64), - flight.acquire, - .null_handle, - ); + const tgt = try sc.acquire(flight.acquire, .null_handle); var cmd = au.CommandBufferProxy.init(flight.cmd, au.D.wrapper); try cmd.beginCommandBuffer(&.{ .flags = .{ .one_time_submit_bit = true } }); @@ -263,7 +258,7 @@ pub fn main() !void { .extent = sc.cinfo.image_extent, }; - sc.beginRendering(cmd, render_area, acq.image_index); + tgt.begin_rendering(cmd, render_area); record_render( cmd, uber, @@ -273,7 +268,7 @@ pub fn main() !void { descriptorSet, ); im.c.ImGui_ImplVulkan_RenderDrawData(im.c.igGetDrawData(), @ptrFromInt(@intFromEnum(cmd.handle)), null); - sc.endRendering(cmd, acq.image_index); + tgt.end_rendering(cmd); for (vertex_data) |*v| { for (v.pos[0..2]) |*f| { @@ -304,14 +299,7 @@ pub fn main() !void { @panic("Submission failed"); }; - _ = try au.Q.presentKHR(&vk.PresentInfoKHR{ - .wait_semaphore_count = 1, - .p_wait_semaphores = &.{flight.complete}, - .swapchain_count = 1, - .p_swapchains = &.{sc.handle}, - .p_image_indices = &.{acq.image_index}, - .p_results = null, - }); // todo suboptimal? + _ = try sc.present(&.{flight.complete}, tgt); // todo suboptimal? } try au.D.deviceWaitIdle();