clean up acquire/present

This commit is contained in:
David Allemang
2024-07-08 14:54:44 -04:00
parent 8fd94e631d
commit c6ed235e51
2 changed files with 103 additions and 99 deletions

View File

@@ -89,20 +89,24 @@ 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 {
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 },
@@ -120,7 +124,7 @@ pub fn beginRendering(self: Self, cmd: au.CommandBufferProxy, area: vk.Rect2D, i
.new_layout = .color_attachment_optimal,
.src_queue_family_index = 0,
.dst_queue_family_index = 0,
.image = self.getImage(idx),
.image = self.image,
.subresource_range = .{
.aspect_mask = .{ .color_bit = true },
.base_mip_level = 0,
@@ -139,7 +143,7 @@ pub fn beginRendering(self: Self, cmd: au.CommandBufferProxy, area: vk.Rect2D, i
.color_attachment_count = 1,
.p_color_attachments = &.{
vk.RenderingAttachmentInfo{
.image_view = self.getView(idx),
.image_view = self.view,
.image_layout = .color_attachment_optimal,
.resolve_mode = .{},
.resolve_image_view = .null_handle,
@@ -150,9 +154,9 @@ pub fn beginRendering(self: Self, cmd: au.CommandBufferProxy, area: vk.Rect2D, i
},
},
});
}
}
pub fn endRendering(self: Self, cmd: au.CommandBufferProxy, idx: u32) void {
pub fn end_rendering(self: Target, cmd: au.CommandBufferProxy) void {
cmd.endRendering();
cmd.pipelineBarrier(
@@ -172,7 +176,7 @@ pub fn endRendering(self: Self, cmd: au.CommandBufferProxy, idx: u32) void {
.new_layout = .present_src_khr,
.src_queue_family_index = 0,
.dst_queue_family_index = 0,
.image = self.getImage(idx),
.image = self.image,
.subresource_range = .{
.aspect_mask = .{ .color_bit = true },
.base_mip_level = 0,
@@ -183,4 +187,16 @@ pub fn endRendering(self: Self, cmd: au.CommandBufferProxy, idx: u32) void {
},
},
);
}
};
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,
});
}

View File

@@ -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();