incremental remove GraphicsContext

This commit is contained in:
David Allemang
2024-03-30 00:41:48 -04:00
parent 5a48cdd936
commit 2402f29742
2 changed files with 198 additions and 155 deletions

View File

@@ -1,15 +1,55 @@
const std = @import("std");
const vk = @import("vk");
const GraphicsContext = @import("graphics_context.zig").GraphicsContext;
const gfx = @import("gfx.zig");
const Allocator = std.mem.Allocator;
pub const Context = struct {
vki: gfx.InstanceDispatch,
vkd: gfx.DeviceDispatch,
pdev: vk.PhysicalDevice,
dev: vk.Device,
surface: vk.SurfaceKHR,
queue: vk.Queue,
family: u32,
pub fn findMemoryTypeIndex(
self: @This(),
memory_type_bits: u32,
flags: vk.MemoryPropertyFlags,
) !u32 {
const mem_props = self.vki.getPhysicalDeviceMemoryProperties(self.pdev);
for (mem_props.memory_types[0..mem_props.memory_type_count], 0..) |mem_type, i| {
if (memory_type_bits & (@as(u32, 1) << @truncate(i)) != 0 and mem_type.property_flags.contains(flags)) {
return @truncate(i);
}
}
return error.NoSuitableMemoryType;
}
pub fn allocate(
self: @This(),
requirements: vk.MemoryRequirements,
flags: vk.MemoryPropertyFlags,
) !vk.DeviceMemory {
return try self.vkd.allocateMemory(self.dev, &.{
.allocation_size = requirements.size,
.memory_type_index = try self.findMemoryTypeIndex(requirements.memory_type_bits, flags),
}, null);
}
};
pub const Swapchain = struct {
pub const PresentState = enum {
optimal,
suboptimal,
};
gc: *const GraphicsContext,
gc: *const Context,
allocator: Allocator,
surface_format: vk.SurfaceFormatKHR,
@@ -21,11 +61,11 @@ pub const Swapchain = struct {
image_index: u32,
next_image_acquired: vk.Semaphore,
pub fn init(gc: *const GraphicsContext, allocator: Allocator, extent: vk.Extent2D) !Swapchain {
pub fn init(gc: *const Context, allocator: Allocator, extent: vk.Extent2D) !Swapchain {
return try initRecycle(gc, allocator, extent, .null_handle);
}
pub fn initRecycle(gc: *const GraphicsContext, allocator: Allocator, extent: vk.Extent2D, old_handle: vk.SwapchainKHR) !Swapchain {
pub fn initRecycle(gc: *const Context, allocator: Allocator, extent: vk.Extent2D, old_handle: vk.SwapchainKHR) !Swapchain {
const caps = try gc.vki.getPhysicalDeviceSurfaceCapabilitiesKHR(gc.pdev, gc.surface);
const actual_extent = findActualExtent(caps, extent);
if (actual_extent.width == 0 or actual_extent.height == 0) {
@@ -40,12 +80,6 @@ pub const Swapchain = struct {
image_count = @min(image_count, caps.max_image_count);
}
const qfi = [_]u32{ gc.graphics_queue.family, gc.present_queue.family };
const sharing_mode: vk.SharingMode = if (gc.graphics_queue.family != gc.present_queue.family)
.concurrent
else
.exclusive;
const handle = try gc.vkd.createSwapchainKHR(gc.dev, &.{
.surface = gc.surface,
.min_image_count = image_count,
@@ -54,9 +88,7 @@ pub const Swapchain = struct {
.image_extent = actual_extent,
.image_array_layers = 1,
.image_usage = .{ .color_attachment_bit = true, .transfer_dst_bit = true },
.image_sharing_mode = sharing_mode,
.queue_family_index_count = qfi.len,
.p_queue_family_indices = &qfi,
.image_sharing_mode = .exclusive,
.pre_transform = caps.current_transform,
.composite_alpha = .{ .opaque_bit_khr = true },
.present_mode = present_mode,
@@ -154,7 +186,7 @@ pub const Swapchain = struct {
// Step 2: Submit the command buffer
const wait_stage = [_]vk.PipelineStageFlags{.{ .top_of_pipe_bit = true }};
try self.gc.vkd.queueSubmit(self.gc.graphics_queue.handle, 1, &[_]vk.SubmitInfo{.{
try self.gc.vkd.queueSubmit(self.gc.queue, 1, &[_]vk.SubmitInfo{.{
.wait_semaphore_count = 1,
.p_wait_semaphores = @ptrCast(&current.image_acquired),
.p_wait_dst_stage_mask = &wait_stage,
@@ -165,7 +197,7 @@ pub const Swapchain = struct {
}}, current.frame_fence);
// Step 3: Present the current frame
_ = try self.gc.vkd.queuePresentKHR(self.gc.present_queue.handle, &.{
_ = try self.gc.vkd.queuePresentKHR(self.gc.queue, &.{
.wait_semaphore_count = 1,
.p_wait_semaphores = @as([*]const vk.Semaphore, @ptrCast(&current.render_finished)),
.swapchain_count = 1,
@@ -200,7 +232,7 @@ const SwapImage = struct {
render_finished: vk.Semaphore,
frame_fence: vk.Fence,
fn init(gc: *const GraphicsContext, image: vk.Image, format: vk.Format) !SwapImage {
fn init(gc: *const Context, image: vk.Image, format: vk.Format) !SwapImage {
const view = try gc.vkd.createImageView(gc.dev, &.{
.image = image,
.view_type = .@"2d",
@@ -234,7 +266,7 @@ const SwapImage = struct {
};
}
fn deinit(self: SwapImage, gc: *const GraphicsContext) void {
fn deinit(self: SwapImage, gc: *const Context) void {
self.waitForFence(gc) catch return;
gc.vkd.destroyImageView(gc.dev, self.view, null);
gc.vkd.destroySemaphore(gc.dev, self.image_acquired, null);
@@ -242,12 +274,12 @@ const SwapImage = struct {
gc.vkd.destroyFence(gc.dev, self.frame_fence, null);
}
fn waitForFence(self: SwapImage, gc: *const GraphicsContext) !void {
fn waitForFence(self: SwapImage, gc: *const Context) !void {
_ = try gc.vkd.waitForFences(gc.dev, 1, @ptrCast(&self.frame_fence), vk.TRUE, std.math.maxInt(u64));
}
};
fn initSwapchainImages(gc: *const GraphicsContext, swapchain: vk.SwapchainKHR, format: vk.Format, allocator: Allocator) ![]SwapImage {
fn initSwapchainImages(gc: *const Context, swapchain: vk.SwapchainKHR, format: vk.Format, allocator: Allocator) ![]SwapImage {
var count: u32 = undefined;
_ = try gc.vkd.getSwapchainImagesKHR(gc.dev, swapchain, &count, null);
const images = try allocator.alloc(vk.Image, count);
@@ -268,7 +300,7 @@ fn initSwapchainImages(gc: *const GraphicsContext, swapchain: vk.SwapchainKHR, f
return swap_images;
}
fn findSurfaceFormat(gc: *const GraphicsContext, allocator: Allocator) !vk.SurfaceFormatKHR {
fn findSurfaceFormat(gc: *const Context, allocator: Allocator) !vk.SurfaceFormatKHR {
const preferred = vk.SurfaceFormatKHR{
.format = .b8g8r8a8_srgb,
.color_space = .srgb_nonlinear_khr,
@@ -289,7 +321,7 @@ fn findSurfaceFormat(gc: *const GraphicsContext, allocator: Allocator) !vk.Surfa
return surface_formats[0]; // There must always be at least one supported surface format
}
fn findPresentMode(gc: *const GraphicsContext, allocator: Allocator) !vk.PresentModeKHR {
fn findPresentMode(gc: *const Context, allocator: Allocator) !vk.PresentModeKHR {
var count: u32 = undefined;
_ = try gc.vki.getPhysicalDeviceSurfacePresentModesKHR(gc.pdev, gc.surface, &count, null);
const present_modes = try allocator.alloc(vk.PresentModeKHR, count);