starting to test swapchain

This commit is contained in:
David Allemang
2024-06-27 14:17:06 -04:00
parent 1c748022de
commit 1f82923f59
4 changed files with 150 additions and 11 deletions

68
src/au/SwapChain.zig Normal file
View File

@@ -0,0 +1,68 @@
const std = @import("std");
const au = @import("../au.zig");
const vk = @import("vk");
const Self = @This();
alloc: std.mem.Allocator,
cinfo: vk.SwapchainCreateInfoKHR,
handle: vk.SwapchainKHR,
pub fn init(alloc: std.mem.Allocator) !Self {
const caps = try au.I.getPhysicalDeviceSurfaceCapabilitiesKHR(au.device_config.pdev, au.W.surface);
var min_image_count = @max(3, caps.min_image_count + 1); // todo magic numbers
if (caps.max_image_count > 0) {
min_image_count = @min(min_image_count, caps.max_image_count);
}
// determine format
const format = au.device_config.format;
return .{
.alloc = alloc,
.cinfo = .{
.surface = au.W.surface,
.min_image_count = min_image_count,
.image_format = format.format,
.image_color_space = format.color_space,
.image_extent = undefined, // set in rebuild
.image_array_layers = 1,
.image_usage = .{ .color_attachment_bit = true },
.image_sharing_mode = .exclusive,
.pre_transform = .{ .identity_bit_khr = true },
.composite_alpha = .{ .opaque_bit_khr = true },
.present_mode = au.device_config.mode,
.clipped = vk.TRUE,
.old_swapchain = .null_handle,
},
.handle = .null_handle,
};
}
pub fn deinit(self: *Self) void {
au.D.destroySwapchainKHR(self.handle, null);
}
/// mark that the swapchain _should_ be rebuilt with the given extent
/// this function is reentrant, so the swapchain can be marked multiple times
/// and only one rebuild occur
pub fn mark(self: *Self) void {
self.handle = .null_handle;
}
/// rebuild the swapchain only if it is marked. return true if the swapchain was rebuilt.
pub fn rebuild(self: *Self) !bool {
if (self.handle != .null_handle) return false;
const caps = try au.I.getPhysicalDeviceSurfaceCapabilitiesKHR(au.device_config.pdev, self.cinfo.surface);
self.cinfo.image_extent = caps.current_extent;
self.handle = try au.D.createSwapchainKHR(&self.cinfo, null);
au.D.destroySwapchainKHR(self.cinfo.old_swapchain, null);
self.cinfo.old_swapchain = self.handle;
// todo repopulate images and synchronization
return true;
}