moving command buffer creation to base command pool

This commit is contained in:
2025-11-15 23:32:38 +01:00
parent 027bd2ce1c
commit b8c7a6e41e
2 changed files with 22 additions and 19 deletions

View File

@@ -20,7 +20,7 @@ pub fn create(device: *base.Device, allocator: std.mem.Allocator, info: *const v
var interface = try Interface.init(device, allocator, info); var interface = try Interface.init(device, allocator, info);
interface.vtable = &.{ interface.vtable = &.{
.allocateCommandBuffers = allocateCommandBuffers, .createCommandBuffer = createCommandBuffer,
.destroy = destroy, .destroy = destroy,
.reset = reset, .reset = reset,
}; };
@@ -31,18 +31,9 @@ pub fn create(device: *base.Device, allocator: std.mem.Allocator, info: *const v
return self; return self;
} }
pub fn allocateCommandBuffers(interface: *Interface, info: *const vk.CommandBufferAllocateInfo) VkError!void { pub fn createCommandBuffer(interface: *Interface, allocator: std.mem.Allocator, info: *const vk.CommandBufferAllocateInfo) VkError!*base.CommandBuffer {
const allocator = interface.host_allocator.allocator();
while (interface.buffers.capacity < interface.buffers.items.len + info.command_buffer_count) {
interface.buffers.ensureUnusedCapacity(allocator, base.CommandPool.BUFFER_POOL_BASE_CAPACITY) catch return VkError.OutOfHostMemory;
}
for (0..info.command_buffer_count) |_| {
const cmd = try SoftCommandBuffer.create(interface.owner, allocator, info); const cmd = try SoftCommandBuffer.create(interface.owner, allocator, info);
const non_dis_cmd = try NonDispatchable(base.CommandBuffer).wrap(allocator, &cmd.interface); return &cmd.interface;
interface.buffers.appendAssumeCapacity(non_dis_cmd);
}
} }
pub fn destroy(interface: *Interface, allocator: std.mem.Allocator) void { pub fn destroy(interface: *Interface, allocator: std.mem.Allocator) void {

View File

@@ -11,6 +11,8 @@ const Device = @import("Device.zig");
const Self = @This(); const Self = @This();
pub const ObjectType: vk.ObjectType = .command_pool; pub const ObjectType: vk.ObjectType = .command_pool;
/// Base capacity of the command buffer pool.
/// Every increase of the capacity will be by this amount.
pub const BUFFER_POOL_BASE_CAPACITY = 64; pub const BUFFER_POOL_BASE_CAPACITY = 64;
owner: *Device, owner: *Device,
@@ -19,16 +21,17 @@ queue_family_index: u32,
host_allocator: VulkanAllocator, host_allocator: VulkanAllocator,
/// Contiguous dynamic array of command buffers with free ones /// Contiguous dynamic array of command buffers with free ones
/// grouped at the end and the first free index being storesd in /// grouped at the end.
/// `first_free_buffer_index` /// When freed swaps happen to keep the free buffers at the end.
/// When freed swaps happen to keep the free buffers at the end
buffers: std.ArrayList(*NonDispatchable(CommandBuffer)), buffers: std.ArrayList(*NonDispatchable(CommandBuffer)),
/// Index of the first free command buffer.
first_free_buffer_index: usize, first_free_buffer_index: usize,
vtable: *const VTable, vtable: *const VTable,
pub const VTable = struct { pub const VTable = struct {
allocateCommandBuffers: *const fn (*Self, *const vk.CommandBufferAllocateInfo) VkError!void, createCommandBuffer: *const fn (*Self, std.mem.Allocator, *const vk.CommandBufferAllocateInfo) VkError!*CommandBuffer,
destroy: *const fn (*Self, std.mem.Allocator) void, destroy: *const fn (*Self, std.mem.Allocator) void,
reset: *const fn (*Self, vk.CommandPoolResetFlags) VkError!void, reset: *const fn (*Self, vk.CommandPoolResetFlags) VkError!void,
}; };
@@ -46,8 +49,17 @@ pub fn init(device: *Device, allocator: std.mem.Allocator, info: *const vk.Comma
} }
pub fn allocateCommandBuffers(self: *Self, info: *const vk.CommandBufferAllocateInfo) VkError![]*NonDispatchable(CommandBuffer) { pub fn allocateCommandBuffers(self: *Self, info: *const vk.CommandBufferAllocateInfo) VkError![]*NonDispatchable(CommandBuffer) {
const allocator = self.host_allocator.allocator();
if (self.buffers.items.len < info.command_buffer_count or self.first_free_buffer_index + info.command_buffer_count > self.buffers.items.len) { if (self.buffers.items.len < info.command_buffer_count or self.first_free_buffer_index + info.command_buffer_count > self.buffers.items.len) {
try self.vtable.allocateCommandBuffers(self, info); while (self.buffers.capacity < self.buffers.items.len + info.command_buffer_count) {
self.buffers.ensureUnusedCapacity(allocator, BUFFER_POOL_BASE_CAPACITY) catch return VkError.OutOfHostMemory;
}
for (0..info.command_buffer_count) |_| {
const cmd = try self.vtable.createCommandBuffer(self, allocator, info);
const non_dis_cmd = try NonDispatchable(CommandBuffer).wrap(allocator, cmd);
self.buffers.appendAssumeCapacity(non_dis_cmd);
}
} }
const bound_up = self.first_free_buffer_index + info.command_buffer_count; const bound_up = self.first_free_buffer_index + info.command_buffer_count;
@@ -56,7 +68,7 @@ pub fn allocateCommandBuffers(self: *Self, info: *const vk.CommandBufferAllocate
return slice; return slice;
} }
pub inline fn destroy(self: *Self, allocator: std.mem.Allocator) void { pub fn destroy(self: *Self, allocator: std.mem.Allocator) void {
for (self.buffers.items) |non_dis_cmd| { for (self.buffers.items) |non_dis_cmd| {
non_dis_cmd.object.destroy(allocator); non_dis_cmd.object.destroy(allocator);
non_dis_cmd.destroy(allocator); non_dis_cmd.destroy(allocator);