fixing command pool, adding base command buffer dispatch table

This commit is contained in:
2025-11-16 20:42:08 +01:00
parent c74bc7fb15
commit 5661505bef
7 changed files with 94 additions and 11 deletions

View File

@@ -3,7 +3,7 @@ const vk = @import("vulkan");
const VkError = @import("error_set.zig").VkError;
const VulkanAllocator = @import("VulkanAllocator.zig");
const NonDispatchable = @import("NonDispatchable.zig").NonDispatchable;
const Dispatchable = @import("Dispatchable.zig").Dispatchable;
const CommandBuffer = @import("CommandBuffer.zig");
const Device = @import("Device.zig");
@@ -23,7 +23,7 @@ host_allocator: VulkanAllocator,
/// Contiguous dynamic array of command buffers with free ones
/// grouped at the end.
/// When freed swaps happen to keep the free buffers at the end.
buffers: std.ArrayList(*NonDispatchable(CommandBuffer)),
buffers: std.ArrayList(*Dispatchable(CommandBuffer)),
/// Index of the first free command buffer.
first_free_buffer_index: usize,
@@ -42,13 +42,13 @@ pub fn init(device: *Device, allocator: std.mem.Allocator, info: *const vk.Comma
.flags = info.flags,
.queue_family_index = info.queue_family_index,
.host_allocator = VulkanAllocator.from(allocator).clone(),
.buffers = std.ArrayList(*NonDispatchable(CommandBuffer)).initCapacity(allocator, BUFFER_POOL_BASE_CAPACITY) catch return VkError.OutOfHostMemory,
.buffers = std.ArrayList(*Dispatchable(CommandBuffer)).initCapacity(allocator, BUFFER_POOL_BASE_CAPACITY) catch return VkError.OutOfHostMemory,
.first_free_buffer_index = 0,
.vtable = undefined,
};
}
pub fn allocateCommandBuffers(self: *Self, info: *const vk.CommandBufferAllocateInfo) VkError![]*NonDispatchable(CommandBuffer) {
pub fn allocateCommandBuffers(self: *Self, info: *const vk.CommandBufferAllocateInfo) VkError![]*Dispatchable(CommandBuffer) {
const allocator = self.host_allocator.allocator();
if (self.buffers.items.len < self.first_free_buffer_index + info.command_buffer_count) {
@@ -57,7 +57,7 @@ pub fn allocateCommandBuffers(self: *Self, info: *const vk.CommandBufferAllocate
}
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);
const non_dis_cmd = try Dispatchable(CommandBuffer).wrap(allocator, cmd);
self.buffers.appendAssumeCapacity(non_dis_cmd);
}
}
@@ -68,6 +68,18 @@ pub fn allocateCommandBuffers(self: *Self, info: *const vk.CommandBufferAllocate
return slice;
}
pub fn freeCommandBuffers(self: *Self, cmds: []*Dispatchable(CommandBuffer)) VkError!void {
// Ugly method but it works well
for (cmds) |cmd| {
if (std.mem.indexOf(*Dispatchable(CommandBuffer), self.buffers.items, &[_]*Dispatchable(CommandBuffer){cmd})) |i| {
const save = self.buffers.orderedRemove(i);
// Append the now free command buffer at the end of the pool
self.buffers.appendAssumeCapacity(save);
}
}
self.first_free_buffer_index -= cmds.len;
}
pub fn destroy(self: *Self, allocator: std.mem.Allocator) void {
for (self.buffers.items) |non_dis_cmd| {
non_dis_cmd.object.destroy(allocator);