swapchain with vulkan-tutorial

This commit is contained in:
David Allemang
2024-04-01 23:44:36 -04:00
parent 8e90619d6a
commit 790c7955c7
2 changed files with 180 additions and 147 deletions

View File

@@ -267,7 +267,7 @@ pub fn find_swap_image_count(
surface: vk.SurfaceKHR, surface: vk.SurfaceKHR,
) !u32 { ) !u32 {
const caps = try vki.getPhysicalDeviceSurfaceCapabilitiesKHR(pdev, surface); const caps = try vki.getPhysicalDeviceSurfaceCapabilitiesKHR(pdev, surface);
var count = caps.min_image_count + 1; var count = @max(3, caps.min_image_count + 1);
if (caps.max_image_count > 0) { if (caps.max_image_count > 0) {
count = @min(count, caps.max_image_count); count = @min(count, caps.max_image_count);
} }

View File

@@ -84,6 +84,11 @@ pub fn main() !void {
const queue = vkd.getDeviceQueue(dev, family, 0); const queue = vkd.getDeviceQueue(dev, family, 0);
const pool = try vkd.createCommandPool(dev, &.{
.queue_family_index = family,
}, null);
defer vkd.destroyCommandPool(dev, pool, null);
const preferred_format: vk.SurfaceFormatKHR = .{ const preferred_format: vk.SurfaceFormatKHR = .{
.format = .b8g8r8a8_srgb, .format = .b8g8r8a8_srgb,
.color_space = .srgb_nonlinear_khr, .color_space = .srgb_nonlinear_khr,
@@ -98,12 +103,29 @@ pub fn main() !void {
var swapchain: vk.SwapchainKHR = .null_handle; var swapchain: vk.SwapchainKHR = .null_handle;
defer vkd.destroySwapchainKHR(dev, swapchain, null); defer vkd.destroySwapchainKHR(dev, swapchain, null);
var image_buf: [8]vk.Image = undefined; const ChainImage = struct {
@memset(&image_buf, .null_handle); image: vk.Image = .null_handle,
var images: []vk.Image = &.{}; view: vk.ImageView = .null_handle,
var image_views_buf: [8]vk.ImageView = undefined; cmdbuf: vk.CommandBuffer = .null_handle,
@memset(&image_views_buf, .null_handle); // fence: vk.Fence = .null_handle,
var image_views: []vk.ImageView = &.{}; // image_available: vk.Semaphore = .null_handle,
// render_finished: vk.Semaphore = .null_handle,
};
var chain = std.MultiArrayList(ChainImage){};
defer chain.deinit(ally);
defer vkd.freeCommandBuffers(dev, pool, @intCast(chain.len), chain.items(.cmdbuf).ptr);
defer for (chain.items(.view)) |view| vkd.destroyImageView(dev, view, null);
// defer for (chain.items(.fence)) |fence| vkd.destroyFence(dev, fence, null);
// defer for (chain.items(.image_available)) |sem| vkd.destroySemaphore(dev, sem, null);
// defer for (chain.items(.render_finished)) |sem| vkd.destroySemaphore(dev, sem, null);
const frame_fence = try vkd.createFence(dev, &.{ .flags = .{ .signaled_bit = true } }, null);
defer vkd.destroyFence(dev, frame_fence, null);
const image_available = try vkd.createSemaphore(dev, &.{}, null);
defer vkd.destroySemaphore(dev, image_available, null);
const render_finished = try vkd.createSemaphore(dev, &.{}, null);
defer vkd.destroySemaphore(dev, render_finished, null);
swapchain = try vkd.createSwapchainKHR(dev, &.{ swapchain = try vkd.createSwapchainKHR(dev, &.{
.surface = surface, .surface = surface,
@@ -121,13 +143,12 @@ pub fn main() !void {
.old_swapchain = swapchain, .old_swapchain = swapchain,
}, null); }, null);
var image_count: u32 = @intCast(image_buf.len); var image_count: u32 = undefined;
_ = try vkd.getSwapchainImagesKHR(dev, swapchain, &image_count, &image_buf); _ = try vkd.getSwapchainImagesKHR(dev, swapchain, &image_count, null);
images = image_buf[0..image_count]; try chain.resize(ally, image_count);
image_views = image_views_buf[0..image_count]; _ = try vkd.getSwapchainImagesKHR(dev, swapchain, &image_count, chain.items(.image).ptr);
defer for (image_views) |view| vkd.destroyImageView(dev, view, null);
for (images, image_views) |image, *view| { for (chain.items(.image), chain.items(.view)) |image, *view| {
view.* = try vkd.createImageView(dev, &.{ view.* = try vkd.createImageView(dev, &.{
.image = image, .image = image,
.view_type = .@"2d", .view_type = .@"2d",
@@ -143,6 +164,24 @@ pub fn main() !void {
}, null); }, null);
} }
// for (chain.items(.fence)) |*fence| {
// fence.* = try vkd.createFence(dev, &.{ .flags = .{ .signaled_bit = true } }, null);
// }
//
// for (chain.items(.image_available)) |*sem| {
// sem.* = try vkd.createSemaphore(dev, &.{}, null);
// }
//
// for (chain.items(.render_finished)) |*sem| {
// sem.* = try vkd.createSemaphore(dev, &.{}, null);
// }
try vkd.allocateCommandBuffers(dev, &.{
.command_buffer_count = @intCast(chain.len),
.command_pool = pool,
.level = .primary,
}, chain.items(.cmdbuf).ptr);
const pipeline_layout = try vkd.createPipelineLayout(dev, &.{ const pipeline_layout = try vkd.createPipelineLayout(dev, &.{
.flags = .{}, .flags = .{},
.set_layout_count = 0, .set_layout_count = 0,
@@ -155,11 +194,6 @@ pub fn main() !void {
const pipeline = try createPipeline(dev, pipeline_layout, format, vkd); const pipeline = try createPipeline(dev, pipeline_layout, format, vkd);
defer vkd.destroyPipeline(dev, pipeline, null); defer vkd.destroyPipeline(dev, pipeline, null);
const pool = try vkd.createCommandPool(dev, &.{
.queue_family_index = family,
}, null);
defer vkd.destroyCommandPool(dev, pool, null);
const vertex_buffer = try vkd.createBuffer(dev, &.{ const vertex_buffer = try vkd.createBuffer(dev, &.{
.size = @sizeOf(@TypeOf(vertices)), .size = @sizeOf(@TypeOf(vertices)),
.usage = .{ .transfer_dst_bit = true, .vertex_buffer_bit = true }, .usage = .{ .transfer_dst_bit = true, .vertex_buffer_bit = true },
@@ -186,16 +220,11 @@ pub fn main() !void {
try gfx.uploadData(Index, pdev, vki, dev, vkd, queue, pool, index_buffer, &indices); try gfx.uploadData(Index, pdev, vki, dev, vkd, queue, pool, index_buffer, &indices);
// var cmdbufs = try createCommandBuffers( for (chain.items(.image), chain.items(.view), chain.items(.cmdbuf)) |image, view, cmdbuf| {
// &gc, try record_cmdbuf(cmdbuf, vkd, image, view, extent, pipeline, vertex_buffer, index_buffer);
// pool, }
// ally,
// vertex_buffer, // var index: u32 = 0;
// index_buffer,
// pipeline,
// swapchain,
// );
// defer destroyCommandBuffers(&gc, pool, ally, cmdbufs);
while (c.glfwWindowShouldClose(window) == c.GLFW_FALSE) { while (c.glfwWindowShouldClose(window) == c.GLFW_FALSE) {
var w: c_int = undefined; var w: c_int = undefined;
@@ -208,6 +237,37 @@ pub fn main() !void {
continue; continue;
} }
_ = try vkd.waitForFences(dev, 1, @ptrCast(&frame_fence), vk.TRUE, std.math.maxInt(u64));
try vkd.resetFences(dev, 1, @ptrCast(&frame_fence));
// const frame: ChainImage = chain.get();
// var index: u32 = undefined;
// try vkd.acquireNextImageKHR(dev, swapchain, std.math.maxInt(u64), frame., fence);
const result = try vkd.acquireNextImageKHR(dev, swapchain, std.math.maxInt(u64), image_available, .null_handle);
// std.log.debug("frame {d}", .{result.image_index});
const frame = chain.get(result.image_index);
try vkd.queueSubmit(queue, 1, @ptrCast(&vk.SubmitInfo{
.wait_semaphore_count = 1,
.p_wait_semaphores = @ptrCast(&image_available),
.p_wait_dst_stage_mask = @ptrCast(&vk.PipelineStageFlags{ .color_attachment_output_bit = true }),
.command_buffer_count = 1,
.p_command_buffers = @ptrCast(&frame.cmdbuf),
.signal_semaphore_count = 1,
.p_signal_semaphores = @ptrCast(&render_finished),
}), frame_fence);
_ = try vkd.queuePresentKHR(queue, &.{
.wait_semaphore_count = 1,
.p_wait_semaphores = @ptrCast(&render_finished),
.swapchain_count = 1,
.p_swapchains = @ptrCast(&swapchain),
.p_image_indices = @ptrCast(&result.image_index),
.p_results = null,
});
// const cmdbuf = cmdbufs[swapchain.image_index]; // const cmdbuf = cmdbufs[swapchain.image_index];
// const state = swapchain.present(cmdbuf) catch |err| switch (err) { // const state = swapchain.present(cmdbuf) catch |err| switch (err) {
@@ -240,28 +300,16 @@ pub fn main() !void {
try vkd.deviceWaitIdle(dev); try vkd.deviceWaitIdle(dev);
} }
fn createCommandBuffers( fn record_cmdbuf(
views: []const vk.Image, cmdbuf: vk.CommandBuffer,
images: []const vk.ImageView,
dev: vk.Device,
vkd: gfx.DeviceDispatch, vkd: gfx.DeviceDispatch,
pool: vk.CommandPool, image: vk.Image,
allocator: Allocator, view: vk.ImageView,
extent: vk.Extent2D,
pipeline: vk.Pipeline,
vertex_buffer: vk.Buffer, vertex_buffer: vk.Buffer,
index_buffer: vk.Buffer, index_buffer: vk.Buffer,
pipeline: vk.Pipeline, ) !void {
extent: vk.Extent2D,
) ![]vk.CommandBuffer {
const cmdbufs = try allocator.alloc(vk.CommandBuffer, images.len);
errdefer allocator.free(cmdbufs);
try vkd.allocateCommandBuffers(dev, &.{
.command_pool = pool,
.level = .primary,
.command_buffer_count = @as(u32, @truncate(cmdbufs.len)),
}, cmdbufs.ptr);
errdefer vkd.freeCommandBuffers(dev, pool, @truncate(cmdbufs.len), cmdbufs.ptr);
const clear = vk.ClearValue{ const clear = vk.ClearValue{
.color = .{ .float_32 = .{ 0, 0, 0, 1 } }, .color = .{ .float_32 = .{ 0, 0, 0, 1 } },
}; };
@@ -269,8 +317,8 @@ fn createCommandBuffers(
const viewport = vk.Viewport{ const viewport = vk.Viewport{
.x = 0, .x = 0,
.y = 0, .y = 0,
.width = @as(f32, @floatFromInt(extent.width)), .width = @floatFromInt(extent.width),
.height = @as(f32, @floatFromInt(extent.height)), .height = @floatFromInt(extent.height),
.min_depth = 0, .min_depth = 0,
.max_depth = 1, .max_depth = 1,
}; };
@@ -280,7 +328,6 @@ fn createCommandBuffers(
.extent = extent, .extent = extent,
}; };
for (cmdbufs, images, views) |cmdbuf, image, view| {
try vkd.beginCommandBuffer(cmdbuf, &.{}); try vkd.beginCommandBuffer(cmdbuf, &.{});
vkd.cmdPipelineBarrier( vkd.cmdPipelineBarrier(
@@ -374,20 +421,6 @@ fn createCommandBuffers(
); );
try vkd.endCommandBuffer(cmdbuf); try vkd.endCommandBuffer(cmdbuf);
}
return cmdbufs;
}
fn destroyCommandBuffers(
dev: vk.Device,
vkd: gfx.DeviceDispatch,
pool: vk.CommandPool,
allocator: Allocator,
cmdbufs: []vk.CommandBuffer,
) void {
vkd.freeCommandBuffers(dev, pool, @truncate(cmdbufs.len), cmdbufs.ptr);
allocator.free(cmdbufs);
} }
fn createPipeline(dev: vk.Device, layout: vk.PipelineLayout, format: vk.SurfaceFormatKHR, vkd: gfx.DeviceDispatch) !vk.Pipeline { fn createPipeline(dev: vk.Device, layout: vk.PipelineLayout, format: vk.SurfaceFormatKHR, vkd: gfx.DeviceDispatch) !vk.Pipeline {