CommandBuffer wrapper

This commit is contained in:
2025-11-28 17:09:37 +01:00
parent 94c43a170f
commit 2541dee18d
9 changed files with 461 additions and 289 deletions

View File

@@ -6,6 +6,7 @@ const glfw = @import("zglfw");
const vk = @import("vulkan");
const Queue = @import("Queue.zig");
const QueueType = @import("QueueType.zig").QueueType;
const VkAllocator = @import("VkAllocator.zig");
pub const Mode = union(enum) {
@@ -15,18 +16,6 @@ pub const Mode = union(enum) {
surface: vk.SurfaceKHR,
presentation_queue: Queue,
},
pub fn deinit(self: *Mode, instance: vk.InstanceProxy, vk_allocator: *VkAllocator) void {
std.log.debug("Deinitializing {*} with InstanceProxy@{{{X},{*}}} and {*}", .{ self, instance.handle, instance.wrapper, vk_allocator });
switch (self.*) {
.headless => {},
.surface => |x| {
instance.destroySurfaceKHR(x.surface, &vk_allocator.interface);
},
}
self.* = undefined;
}
};
pub const ModeTag = std.meta.Tag(Mode);
@@ -104,6 +93,9 @@ transfer_queue: Queue,
graphics_command_pool: vk.CommandPool,
compute_command_pool: vk.CommandPool,
transfer_command_pool: vk.CommandPool,
transient_graphics_command_pool: vk.CommandPool,
transient_compute_command_pool: vk.CommandPool,
transient_transfer_command_pool: vk.CommandPool,
prng_ptr: *std.Random.Pcg,
@@ -337,6 +329,18 @@ pub fn init(allocator: std.mem.Allocator, maybe_window: ?*glfw.Window) !Engine {
}, &vk_allocator.interface);
errdefer device.destroyCommandPool(transfer_command_pool, &vk_allocator.interface);
const transient_graphics_command_pool = try device.createCommandPool(&.{
.flags = .{ .transient_bit = true },
.queue_family_index = queue_allocations.graphics_queue.family,
}, &vk_allocator.interface);
errdefer device.destroyCommandPool(transient_graphics_command_pool, &vk_allocator.interface);
const transient_compute_command_pool = try device.createCommandPool(&.{
.flags = .{ .transient_bit = true },
.queue_family_index = queue_allocations.compute_queue.family,
}, &vk_allocator.interface);
errdefer device.destroyCommandPool(transient_compute_command_pool, &vk_allocator.interface);
const transient_transfer_command_pool = try device.createCommandPool(&.{
.flags = .{ .transient_bit = true },
.queue_family_index = queue_allocations.transfer_queue.family,
@@ -383,6 +387,9 @@ pub fn init(allocator: std.mem.Allocator, maybe_window: ?*glfw.Window) !Engine {
.graphics_command_pool = graphics_command_pool,
.compute_command_pool = compute_command_pool,
.transfer_command_pool = transfer_command_pool,
.transient_graphics_command_pool = transient_graphics_command_pool,
.transient_compute_command_pool = transient_compute_command_pool,
.transient_transfer_command_pool = transient_transfer_command_pool,
.prng_ptr = prng_ptr,
@@ -400,6 +407,9 @@ pub fn deinit(self: *Engine) void {
self.device.destroyCommandPool(self.graphics_command_pool, &self.vk_allocator.interface);
self.device.destroyCommandPool(self.compute_command_pool, &self.vk_allocator.interface);
self.device.destroyCommandPool(self.transfer_command_pool, &self.vk_allocator.interface);
self.device.destroyCommandPool(self.transient_graphics_command_pool, &self.vk_allocator.interface);
self.device.destroyCommandPool(self.transient_compute_command_pool, &self.vk_allocator.interface);
self.device.destroyCommandPool(self.transient_transfer_command_pool, &self.vk_allocator.interface);
self.device.destroyDevice(&self.vk_allocator.interface);
@@ -437,70 +447,6 @@ pub fn allocate(self: *const Engine, memory_requirements: vk.MemoryRequirements,
return error.NoSuitableMemoryType;
}
pub fn allocateTransientTransferCommandBuffer(self: *const Engine) !vk.CommandBufferProxy {
var command_buffer: vk.CommandBuffer = undefined;
try self.device.allocateCommandBuffers(&.{
.command_pool = self.transient_transfer_command_pool,
.level = .primary,
.command_buffer_count = 1,
}, @ptrCast(&command_buffer));
return .init(command_buffer, self.device.wrapper);
}
pub fn allocateTransferCommandBuffer(self: *const Engine) !vk.CommandBufferProxy {
var command_buffer: vk.CommandBuffer = undefined;
try self.device.allocateCommandBuffers(&.{
.command_pool = self.transfer_command_pool,
.level = .primary,
.command_buffer_count = 1,
}, @ptrCast(&command_buffer));
return .init(command_buffer, self.device.wrapper);
}
pub fn allocateGraphicsCommandBuffer(self: *const Engine) !vk.CommandBufferProxy {
var command_buffer: vk.CommandBuffer = undefined;
try self.device.allocateCommandBuffers(&.{
.command_pool = self.graphics_command_pool,
.level = .primary,
.command_buffer_count = 1,
}, @ptrCast(&command_buffer));
return .init(command_buffer, self.device.wrapper);
}
pub fn freeTransferCommandBuffer(self: *const Engine, command_buffer: vk.CommandBufferProxy) void {
self.device.freeCommandBuffers(self.transfer_command_pool, 1, @ptrCast(&command_buffer));
}
pub fn freeTransientTransferCommandBuffer(self: *const Engine, command_buffer: vk.CommandBufferProxy) void {
self.device.freeCommandBuffers(self.transient_transfer_command_pool, 1, @ptrCast(&command_buffer));
}
pub fn freeGraphicsCommandBuffer(self: *const Engine, command_buffer: vk.CommandBufferProxy) void {
self.device.freeCommandBuffers(self.graphics_command_pool, 1, @ptrCast(&command_buffer));
}
pub fn submitTransferCommandBuffer(self: *const Engine, command_buffer: vk.CommandBufferProxy, fence: vk.Fence) !void {
const submits = [_]vk.SubmitInfo{
.{
.command_buffer_count = 1,
.p_command_buffers = @ptrCast(&command_buffer.handle),
},
};
try self.device.queueSubmit(self.transfer_queue.handle, submits.len, &submits, fence);
}
pub fn submitGraphicsCommandBuffer(self: *const Engine, command_buffer: vk.CommandBufferProxy, fence: vk.Fence) !void {
const submits = [_]vk.SubmitInfo{
.{
.command_buffer_count = 1,
.p_command_buffers = @ptrCast(&command_buffer.handle),
},
};
try self.device.queueSubmit(self.graphics_queue.handle, submits.len, &submits, fence);
}
fn debugUtilsMessengerCallback(
severity: vk.DebugUtilsMessageSeverityFlagsEXT,
message_type: vk.DebugUtilsMessageTypeFlagsEXT,