Factor out command pool allocation

This commit is contained in:
2025-11-22 01:00:44 +01:00
parent bf0224ccd8
commit c3efeaf452
2 changed files with 44 additions and 32 deletions

View File

@@ -100,6 +100,10 @@ graphics_queue: Queue,
compute_queue: Queue, compute_queue: Queue,
transfer_queue: Queue, transfer_queue: Queue,
graphics_command_pool: vk.CommandPool,
compute_command_pool: vk.CommandPool,
transfer_command_pool: vk.CommandPool,
const debug = @import("builtin").mode == .Debug; const debug = @import("builtin").mode == .Debug;
const vk_application_info: vk.ApplicationInfo = .{ const vk_application_info: vk.ApplicationInfo = .{
@@ -292,12 +296,35 @@ pub fn init(allocator: std.mem.Allocator, maybe_window: ?*glfw.Window) !Engine {
const device = vk.DeviceProxy.init(device_handle, device_wrapper_ptr); const device = vk.DeviceProxy.init(device_handle, device_wrapper_ptr);
errdefer device.destroyDevice(&vk_allocator.interface); errdefer device.destroyDevice(&vk_allocator.interface);
// --- GET QUEUES, CREATE COMMAND POOLS ------------------------------------ // --- CREATE COMMAND POOLS, GET QUEUES ------------------------------------
const graphics_queue = try Queue.initAllocation(device, queue_allocations.graphics_queue, false, &vk_allocator.interface); const command_pool_create_flags: vk.CommandPoolCreateFlags = .{
const compute_queue = try Queue.initAllocation(device, queue_allocations.compute_queue, true, &vk_allocator.interface); .transient_bit = true,
const transfer_queue = try Queue.initAllocation(device, queue_allocations.transfer_queue, true, &vk_allocator.interface); .reset_command_buffer_bit = true,
const presentation_queue = if (maybe_window != null) try Queue.initAllocation(device, queue_allocations.presentation_queue, false, &vk_allocator.interface) else undefined; };
const graphics_command_pool = try device.createCommandPool(&.{
.flags = command_pool_create_flags,
.queue_family_index = queue_allocations.graphics_queue.family,
}, &vk_allocator.interface);
errdefer device.destroyCommandPool(graphics_command_pool, &vk_allocator.interface);
const compute_command_pool = try device.createCommandPool(&.{
.flags = command_pool_create_flags,
.queue_family_index = queue_allocations.compute_queue.family,
}, &vk_allocator.interface);
errdefer device.destroyCommandPool(compute_command_pool, &vk_allocator.interface);
const transfer_command_pool = try device.createCommandPool(&.{
.flags = command_pool_create_flags,
.queue_family_index = queue_allocations.transfer_queue.family,
}, &vk_allocator.interface);
errdefer device.destroyCommandPool(transfer_command_pool, &vk_allocator.interface);
const graphics_queue: Queue = .initAllocation(device, queue_allocations.graphics_queue);
const compute_queue: Queue = .initAllocation(device, queue_allocations.compute_queue);
const transfer_queue: Queue = .initAllocation(device, queue_allocations.transfer_queue);
const presentation_queue: Queue = if (maybe_window != null) .initAllocation(device, queue_allocations.presentation_queue) else undefined;
// ------------------------------------------------------------------------- // -------------------------------------------------------------------------
@@ -321,18 +348,19 @@ pub fn init(allocator: std.mem.Allocator, maybe_window: ?*glfw.Window) !Engine {
.graphics_queue = graphics_queue, .graphics_queue = graphics_queue,
.compute_queue = compute_queue, .compute_queue = compute_queue,
.transfer_queue = transfer_queue, .transfer_queue = transfer_queue,
.graphics_command_pool = graphics_command_pool,
.compute_command_pool = compute_command_pool,
.transfer_command_pool = transfer_command_pool,
}; };
} }
pub fn deinit(self: *Engine) void { pub fn deinit(self: *Engine) void {
const allocator = self.vk_allocator.allocator; const allocator = self.vk_allocator.allocator;
if (std.meta.activeTag(self.mode) == .surface) { self.device.destroyCommandPool(self.graphics_command_pool, &self.vk_allocator.interface);
self.mode.surface.presentation_queue.deinit(self.device, &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.transfer_queue.deinit(self.device, &self.vk_allocator.interface);
self.compute_queue.deinit(self.device, &self.vk_allocator.interface);
self.graphics_queue.deinit(self.device, &self.vk_allocator.interface);
self.device.destroyDevice(&self.vk_allocator.interface); self.device.destroyDevice(&self.vk_allocator.interface);
allocator.destroy(self.device.wrapper); allocator.destroy(self.device.wrapper);
@@ -372,7 +400,7 @@ pub fn allocate(self: *const Engine, memory_requirements: vk.MemoryRequirements,
pub fn allocateTransferCommandBuffer(self: *const Engine) !vk.CommandBufferProxy { pub fn allocateTransferCommandBuffer(self: *const Engine) !vk.CommandBufferProxy {
var command_buffer: vk.CommandBuffer = undefined; var command_buffer: vk.CommandBuffer = undefined;
try self.device.allocateCommandBuffers(&.{ try self.device.allocateCommandBuffers(&.{
.command_pool = self.transfer_queue.command_pool, .command_pool = self.transfer_command_pool,
.level = .primary, .level = .primary,
.command_buffer_count = 1, .command_buffer_count = 1,
}, @ptrCast(&command_buffer)); }, @ptrCast(&command_buffer));
@@ -382,7 +410,7 @@ pub fn allocateTransferCommandBuffer(self: *const Engine) !vk.CommandBufferProxy
pub fn allocateGraphicsCommandBuffer(self: *const Engine) !vk.CommandBufferProxy { pub fn allocateGraphicsCommandBuffer(self: *const Engine) !vk.CommandBufferProxy {
var command_buffer: vk.CommandBuffer = undefined; var command_buffer: vk.CommandBuffer = undefined;
try self.device.allocateCommandBuffers(&.{ try self.device.allocateCommandBuffers(&.{
.command_pool = self.graphics_queue.command_pool, .command_pool = self.graphics_command_pool,
.level = .primary, .level = .primary,
.command_buffer_count = 1, .command_buffer_count = 1,
}, @ptrCast(&command_buffer)); }, @ptrCast(&command_buffer));
@@ -390,11 +418,11 @@ pub fn allocateGraphicsCommandBuffer(self: *const Engine) !vk.CommandBufferProxy
} }
pub fn freeTransferCommandBuffer(self: *const Engine, command_buffer: vk.CommandBufferProxy) void { pub fn freeTransferCommandBuffer(self: *const Engine, command_buffer: vk.CommandBufferProxy) void {
self.device.freeCommandBuffers(self.transfer_queue.command_pool, 1, @ptrCast(&command_buffer)); self.device.freeCommandBuffers(self.transfer_command_pool, 1, @ptrCast(&command_buffer));
} }
pub fn freeGraphicsCommandBuffer(self: *const Engine, command_buffer: vk.CommandBufferProxy) void { pub fn freeGraphicsCommandBuffer(self: *const Engine, command_buffer: vk.CommandBufferProxy) void {
self.device.freeCommandBuffers(self.graphics_queue.command_pool, 1, @ptrCast(&command_buffer)); 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 { pub fn submitTransferCommandBuffer(self: *const Engine, command_buffer: vk.CommandBufferProxy, fence: vk.Fence) !void {

View File

@@ -9,27 +9,11 @@ pub const Allocation = struct {
}; };
handle: vk.Queue, handle: vk.Queue,
command_pool: vk.CommandPool,
allocation: Allocation, allocation: Allocation,
pub fn initAllocation(device: vk.DeviceProxy, allocation: Allocation, transient: bool, p_allocator: ?*const vk.AllocationCallbacks) !Queue { pub fn initAllocation(device: vk.DeviceProxy, allocation: Allocation) Queue {
const command_pool = try device.createCommandPool(&.{
.flags = .{
.transient_bit = transient,
.reset_command_buffer_bit = true,
},
.queue_family_index = allocation.family,
}, p_allocator);
errdefer device.destroyCommandPool(command_pool, p_allocator);
return .{ return .{
.handle = device.getDeviceQueue(allocation.family, allocation.index), .handle = device.getDeviceQueue(allocation.family, allocation.index),
.allocation = allocation, .allocation = allocation,
.command_pool = command_pool,
}; };
} }
pub fn deinit(self: *Queue, device: vk.DeviceProxy, p_allocator: ?*const vk.AllocationCallbacks) void {
device.destroyCommandPool(self.command_pool, p_allocator);
self.* = undefined;
}