More wrappers, more cleanup

This commit is contained in:
2025-12-04 00:27:10 +01:00
parent be4ae4f1a7
commit d885fbea43
16 changed files with 419 additions and 368 deletions

View File

@@ -7,6 +7,7 @@ const vk = @import("vulkan");
const Queue = @import("Queue.zig");
const QueueType = @import("QueueType.zig").QueueType;
const Transient = @import("Transient.zig").Transient;
const VkAllocator = @import("VkAllocator.zig");
pub const Mode = union(enum) {
@@ -347,10 +348,34 @@ pub fn init(allocator: std.mem.Allocator, maybe_window: ?*glfw.Window) !Engine {
}, &vk_allocator.interface);
errdefer device.destroyCommandPool(transient_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;
const graphics_queue: Queue = .init(
device.getDeviceQueue(
queue_allocations.graphics_queue.family,
queue_allocations.graphics_queue.index,
),
queue_allocations.graphics_queue,
);
const compute_queue: Queue = .init(
device.getDeviceQueue(
queue_allocations.compute_queue.family,
queue_allocations.compute_queue.index,
),
queue_allocations.compute_queue,
);
const transfer_queue: Queue = .init(
device.getDeviceQueue(
queue_allocations.transfer_queue.family,
queue_allocations.transfer_queue.index,
),
queue_allocations.transfer_queue,
);
const presentation_queue: Queue = if (maybe_window != null) .init(
device.getDeviceQueue(
queue_allocations.presentation_queue.family,
queue_allocations.presentation_queue.index,
),
queue_allocations.presentation_queue,
) else undefined;
// --- CREATE AND SEED RNG -------------------------------------------------
@@ -613,6 +638,23 @@ fn findPresentationQueueFamily(queue_families_properties: []const vk.QueueFamily
return null;
}
fn resolveCommandPool(self: *const Engine, queue_type: QueueType, transient: Transient) vk.CommandPool {
return switch (queue_type) {
.graphics => switch (transient) {
.persistent => self.graphics_command_pool,
.transient => self.transient_graphics_command_pool,
},
.compute => switch (transient) {
.persistent => self.compute_command_pool,
.transient => self.transient_compute_command_pool,
},
.transfer => switch (transient) {
.persistent => self.transfer_command_pool,
.transient => self.transient_transfer_command_pool,
},
};
}
// --- VULKAN WRAPPERS ---------------------------------------------------------
pub const BufferCreateInfo = struct {
@@ -622,6 +664,18 @@ pub const BufferCreateInfo = struct {
queue_family_indices: []const u32 = &.{},
};
pub const CommandBufferAllocateInfo = struct {
queue_type: QueueType,
transient: Transient,
level: vk.CommandBufferLevel,
};
pub const CommandBufferFreeInfo = struct {
queue_type: QueueType,
transient: Transient,
command_buffer: vk.CommandBuffer,
};
pub const DescriptorPoolCreateInfo = struct {
flags: vk.DescriptorPoolCreateFlags = .{},
max_sets: u32,
@@ -798,6 +852,17 @@ pub fn createBuffer(self: *Engine, create_info: BufferCreateInfo) !vk.Buffer {
return buffer;
}
pub fn allocateCommandBuffer(self: *Engine, allocate_info: CommandBufferAllocateInfo) !vk.CommandBuffer {
const command_pool = self.resolveCommandPool(allocate_info.queue_type, allocate_info.transient);
var command_buffers: [1]vk.CommandBuffer = undefined;
try self.device.allocateCommandBuffers(&.{
.command_pool = command_pool,
.level = allocate_info.level,
.command_buffer_count = command_buffers.len,
}, &command_buffers);
return command_buffers[0];
}
pub fn allocateDescriptorSet(self: *Engine, allocate_info: DescriptorSetAllocateInfo) !vk.DescriptorSet {
var descriptor_sets: [1]vk.DescriptorSet = undefined;
try self.allocateDescriptorSets(.{
@@ -831,6 +896,14 @@ pub fn allocateDescriptorSets(self: *Engine, allocate_info: DescriptorSetsAlloca
}, descriptor_sets.ptr);
}
pub fn bindBufferMemory(self: *Engine, buffer: vk.Buffer, memory: vk.DeviceMemory, memory_offset: vk.DeviceSize) !void {
try self.device.bindBufferMemory(buffer, memory, memory_offset);
}
pub fn bindImageMemory(self: *Engine, image: vk.Image, memory: vk.DeviceMemory, memory_offset: vk.DeviceSize) !void {
try self.device.bindImageMemory(image, memory, memory_offset);
}
pub fn createDescriptorSetLayout(self: *Engine, create_info: DescriptorSetLayoutCreateInfo) !vk.DescriptorSetLayout {
var arena: std.heap.ArenaAllocator = .init(self.vk_allocator.allocator);
defer arena.deinit();
@@ -1106,6 +1179,16 @@ pub fn destroyShaderModule(self: *Engine, shader_module: vk.ShaderModule) void {
self.device.destroyShaderModule(shader_module, &self.vk_allocator.interface);
}
pub fn deviceWaitIdle(self: *Engine) !void {
try self.device.deviceWaitIdle();
}
pub fn freeCommandBuffer(self: *Engine, free_info: CommandBufferFreeInfo) void {
const command_pool = self.resolveCommandPool(free_info.queue_type, free_info.transient);
const command_buffers = [_]vk.CommandBuffer{free_info.command_buffer};
self.device.freeCommandBuffers(command_pool, command_buffers.len, &command_buffers);
}
pub fn freeDescriptorSet(self: *Engine, descriptor_pool: vk.DescriptorPool, descriptor_set: vk.DescriptorSet) void {
const descriptor_sets = [_]vk.DescriptorSet{descriptor_set};
self.device.freeDescriptorSets(descriptor_pool, descriptor_sets.len, &descriptor_sets) catch unreachable;
@@ -1115,10 +1198,27 @@ pub fn freeMemory(self: *Engine, device_memory: vk.DeviceMemory) void {
self.device.freeMemory(device_memory, &self.vk_allocator.interface);
}
pub fn getBufferMemoryRequirements(self: *Engine, buffer: vk.Buffer) vk.MemoryRequirements {
return self.device.getBufferMemoryRequirements(buffer);
}
pub fn getImageMemoryRequirements(self: *Engine, image: vk.Image) vk.MemoryRequirements {
return self.device.getImageMemoryRequirements(image);
}
pub fn mapMemory(self: *Engine, memory: vk.DeviceMemory, offset: vk.DeviceSize, size: vk.DeviceSize, flags: vk.MemoryMapFlags) ![]u8 {
const mapped_memory = try self.device.mapMemory(memory, offset, size, flags);
return @as([*]u8, @ptrCast(mapped_memory))[0..size];
}
pub fn resetFence(self: *Engine, fence: vk.Fence) !void {
try self.device.resetFences(1, @ptrCast(&fence));
}
pub fn unmapMemory(self: *Engine, memory: vk.DeviceMemory) void {
self.device.unmapMemory(memory);
}
pub fn updateDescriptorSets(self: *Engine, update_info: DescriptorSetsUpdateInfo) !void {
var arena: std.heap.ArenaAllocator = .init(self.vk_allocator.allocator);
defer arena.deinit();