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,98 +89,114 @@ pub fn rebuild(self: *Self) !bool {
} }, null); } }, null);
} }
// todo repopulate images and synchronization
return true; return true;
} }
pub fn getImage(self: Self, idx: u32) vk.Image { pub fn acquire(self: Self, semaphore: vk.Semaphore, fence: vk.Fence) !Target {
return self.images.items[idx]; 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 { const Target = struct {
return self.views.items[idx]; 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( cmd.pipelineBarrier(
.{ .top_of_pipe_bit = true }, .{ .top_of_pipe_bit = true },
.{ .color_attachment_output_bit = true }, .{ .color_attachment_output_bit = true },
.{}, .{},
0, 0,
null, null,
0, 0,
null, null,
1, 1,
&.{ &.{
vk.ImageMemoryBarrier{ vk.ImageMemoryBarrier{
.src_access_mask = .{}, .src_access_mask = .{},
.dst_access_mask = .{ .color_attachment_write_bit = true }, .dst_access_mask = .{ .color_attachment_write_bit = true },
.old_layout = .undefined, .old_layout = .undefined,
.new_layout = .color_attachment_optimal, .new_layout = .color_attachment_optimal,
.src_queue_family_index = 0, .src_queue_family_index = 0,
.dst_queue_family_index = 0, .dst_queue_family_index = 0,
.image = self.getImage(idx), .image = self.image,
.subresource_range = .{ .subresource_range = .{
.aspect_mask = .{ .color_bit = true }, .aspect_mask = .{ .color_bit = true },
.base_mip_level = 0, .base_mip_level = 0,
.level_count = 1, .level_count = 1,
.base_array_layer = 0, .base_array_layer = 0,
.layer_count = 1, .layer_count = 1,
},
}, },
}, },
}, );
);
cmd.beginRendering(&vk.RenderingInfo{ cmd.beginRendering(&vk.RenderingInfo{
.render_area = area, .render_area = area,
.layer_count = 1, .layer_count = 1,
.view_mask = 0, .view_mask = 0,
.color_attachment_count = 1, .color_attachment_count = 1,
.p_color_attachments = &.{ .p_color_attachments = &.{
vk.RenderingAttachmentInfo{ vk.RenderingAttachmentInfo{
.image_view = self.getView(idx), .image_view = self.view,
.image_layout = .color_attachment_optimal, .image_layout = .color_attachment_optimal,
.resolve_mode = .{}, .resolve_mode = .{},
.resolve_image_view = .null_handle, .resolve_image_view = .null_handle,
.resolve_image_layout = .undefined, .resolve_image_layout = .undefined,
.load_op = .clear, .load_op = .clear,
.store_op = .store, .store_op = .store,
.clear_value = .{ .color = .{ .float_32 = .{ 0, 0, 0, 1 } } }, .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,
},
},
},
);
}

View File

@@ -248,12 +248,7 @@ pub fn main() !void {
try au.D.resetFences(1, &.{flight.fence}); try au.D.resetFences(1, &.{flight.fence});
try au.D.resetCommandPool(flight.pool, .{}); try au.D.resetCommandPool(flight.pool, .{});
const acq = try au.D.acquireNextImageKHR( const tgt = try sc.acquire(flight.acquire, .null_handle);
sc.handle,
std.math.maxInt(u64),
flight.acquire,
.null_handle,
);
var cmd = au.CommandBufferProxy.init(flight.cmd, au.D.wrapper); var cmd = au.CommandBufferProxy.init(flight.cmd, au.D.wrapper);
try cmd.beginCommandBuffer(&.{ .flags = .{ .one_time_submit_bit = true } }); try cmd.beginCommandBuffer(&.{ .flags = .{ .one_time_submit_bit = true } });
@@ -263,7 +258,7 @@ pub fn main() !void {
.extent = sc.cinfo.image_extent, .extent = sc.cinfo.image_extent,
}; };
sc.beginRendering(cmd, render_area, acq.image_index); tgt.begin_rendering(cmd, render_area);
record_render( record_render(
cmd, cmd,
uber, uber,
@@ -273,7 +268,7 @@ pub fn main() !void {
descriptorSet, descriptorSet,
); );
im.c.ImGui_ImplVulkan_RenderDrawData(im.c.igGetDrawData(), @ptrFromInt(@intFromEnum(cmd.handle)), null); 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 (vertex_data) |*v| {
for (v.pos[0..2]) |*f| { for (v.pos[0..2]) |*f| {
@@ -304,14 +299,7 @@ pub fn main() !void {
@panic("Submission failed"); @panic("Submission failed");
}; };
_ = try au.Q.presentKHR(&vk.PresentInfoKHR{ _ = try sc.present(&.{flight.complete}, tgt); // todo suboptimal?
.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 au.D.deviceWaitIdle(); try au.D.deviceWaitIdle();