adding command pool base

This commit is contained in:
2025-11-12 22:50:41 +01:00
parent d52d6f3ea7
commit e08fba24a6
17 changed files with 177 additions and 71 deletions

39
src/soft/SoftCommandPool.zig git.filemode.normal_file
View File

@@ -0,0 +1,39 @@
const std = @import("std");
const vk = @import("vulkan");
const base = @import("base");
const VkError = base.VkError;
const Device = base.Device;
const Self = @This();
pub const Interface = base.CommandPool;
interface: Interface,
pub fn create(device: *base.Device, allocator: std.mem.Allocator, info: *const vk.CommandPoolCreateInfo) VkError!*Self {
const self = allocator.create(Self) catch return VkError.OutOfHostMemory;
errdefer allocator.destroy(self);
var interface = try Interface.init(device, allocator, info);
interface.vtable = &.{
.destroy = destroy,
.reset = reset,
};
self.* = .{
.interface = interface,
};
return self;
}
pub fn destroy(interface: *Interface, allocator: std.mem.Allocator) void {
const self: *Self = @alignCast(@fieldParentPtr("interface", interface));
allocator.destroy(self);
}
pub fn reset(interface: *Interface, flags: vk.CommandPoolResetFlags) VkError!void {
const self: *Self = @alignCast(@fieldParentPtr("interface", interface));
_ = self;
_ = flags;
}

View File

@@ -5,9 +5,11 @@ const builtin = @import("builtin");
const Debug = std.builtin.OptimizeMode.Debug;
const SoftQueue = @import("SoftQueue.zig");
const SoftCommandPool = @import("SoftCommandPool.zig");
const SoftDeviceMemory = @import("SoftDeviceMemory.zig");
const SoftFence = @import("SoftFence.zig");
const SoftQueue = @import("SoftQueue.zig");
const VkError = base.VkError;
@@ -33,8 +35,10 @@ pub fn create(physical_device: *base.PhysicalDevice, allocator: std.mem.Allocato
interface.dispatch_table = &.{
.allocateMemory = allocateMemory,
.createCommandPool = createCommandPool,
.createFence = createFence,
.destroy = destroy,
.destroyCommandPool = destroyCommandPool,
.destroyFence = destroyFence,
.freeMemory = freeMemory,
.getFenceStatus = getFenceStatus,
@@ -99,6 +103,17 @@ pub fn waitForFences(_: *Interface, fences: []*base.Fence, waitForAll: bool, tim
}
}
// Command Pool functions ============================================================================================================================
pub fn createCommandPool(interface: *Interface, allocator: std.mem.Allocator, info: *const vk.CommandPoolCreateInfo) VkError!*base.CommandPool {
const pool = try SoftCommandPool.create(interface, allocator, info);
return &pool.interface;
}
pub fn destroyCommandPool(_: *Interface, allocator: std.mem.Allocator, pool: *base.CommandPool) VkError!void {
pool.destroy(allocator);
}
// Memory functions ==================================================================================================================================
pub fn allocateMemory(interface: *Interface, allocator: std.mem.Allocator, info: *const vk.MemoryAllocateInfo) VkError!*base.DeviceMemory {

View File

@@ -13,11 +13,11 @@ mutex: std.Thread.Mutex,
condition: std.Thread.Condition,
is_signaled: bool,
pub fn create(device: *const Device, allocator: std.mem.Allocator, info: *const vk.FenceCreateInfo) VkError!*Self {
pub fn create(device: *Device, allocator: std.mem.Allocator, info: *const vk.FenceCreateInfo) VkError!*Self {
const self = allocator.create(Self) catch return VkError.OutOfHostMemory;
errdefer allocator.destroy(self);
var interface = try Interface.init(device, info);
var interface = try Interface.init(device, allocator, info);
interface.vtable = &.{
.destroy = destroy,

View File

@@ -32,6 +32,8 @@ pub fn create(allocator: std.mem.Allocator, instance: *const base.Instance) VkEr
interface.props.device_id = root.DEVICE_ID;
interface.props.device_type = .cpu;
interface.props.limits.max_bound_descriptor_sets = 1024; // tmp
interface.mem_props.memory_type_count = 1;
interface.mem_props.memory_types[0] = .{
.heap_index = 0,
@@ -76,7 +78,7 @@ pub fn create(allocator: std.mem.Allocator, instance: *const base.Instance) VkEr
},
// TODO: maybe add a compute specialized queue
};
interface.queue_family_props = std.ArrayList(vk.QueueFamilyProperties).fromOwnedSlice(queue_family_props[0..]);
interface.queue_family_props.appendSlice(allocator, queue_family_props[0..]) catch return VkError.OutOfHostMemory;
// TODO: use Pytorch's cpuinfo someday
const info = cpuinfo.get(allocator) catch return VkError.InitializationFailed;
@@ -140,5 +142,6 @@ pub fn getSparseImageFormatProperties(
pub fn destroy(interface: *Interface, allocator: std.mem.Allocator) VkError!void {
const self: *Self = @alignCast(@fieldParentPtr("interface", interface));
interface.queue_family_props.deinit(allocator);
allocator.destroy(self);
}

View File

@@ -62,8 +62,6 @@ pub fn submit(interface: *Interface, info: []const vk.SubmitInfo, fence: ?*base.
// TODO: commands executions
std.log.debug("Queue execution", .{});
std.Thread.sleep(1_000_000_000);
if (p_fence) |fence_obj| {
fence_obj.signal() catch {};
}

View File

@@ -7,6 +7,7 @@ pub const SoftDevice = @import("SoftDevice.zig");
pub const SoftPhysicalDevice = @import("SoftPhysicalDevice.zig");
pub const SoftQueue = @import("SoftQueue.zig");
pub const SoftCommandPool = @import("SoftCommandPool.zig");
pub const SoftDeviceMemory = @import("SoftDeviceMemory.zig");
pub const SoftFence = @import("SoftFence.zig");

37
src/vulkan/CommandPool.zig git.filemode.normal_file
View File

@@ -0,0 +1,37 @@
const std = @import("std");
const vk = @import("vulkan");
const VkError = @import("error_set.zig").VkError;
const Device = @import("Device.zig");
const Self = @This();
pub const ObjectType: vk.ObjectType = .command_pool;
owner: *Device,
flags: vk.CommandPoolCreateFlags,
queue_family_index: u32,
vtable: *const VTable,
pub const VTable = struct {
destroy: *const fn (*Self, std.mem.Allocator) void,
reset: *const fn (*Self, vk.CommandPoolResetFlags) VkError!void,
};
pub fn init(device: *Device, allocator: std.mem.Allocator, info: *const vk.CommandPoolCreateInfo) VkError!Self {
_ = allocator;
return .{
.owner = device,
.flags = info.flags,
.queue_family_index = info.queue_family_index,
.vtable = undefined,
};
}
pub inline fn destroy(self: *Self, allocator: std.mem.Allocator) void {
self.vtable.destroy(self, allocator);
}
pub inline fn reset(self: *Self, flags: vk.CommandPoolResetFlags) VkError!void {
try self.vtable.reset(self, flags);
}

View File

@@ -4,10 +4,13 @@ const vk = @import("vulkan");
const Dispatchable = @import("Dispatchable.zig").Dispatchable;
const VulkanAllocator = @import("VulkanAllocator.zig");
const VkError = @import("error_set.zig").VkError;
const PhysicalDevice = @import("PhysicalDevice.zig");
const Queue = @import("Queue.zig");
const CommandPool = @import("CommandPool.zig");
const DeviceMemory = @import("DeviceMemory.zig");
const Fence = @import("Fence.zig");
const Queue = @import("Queue.zig");
const Self = @This();
pub const ObjectType: vk.ObjectType = .device;
@@ -25,11 +28,13 @@ pub const VTable = struct {
pub const DispatchTable = struct {
allocateMemory: *const fn (*Self, std.mem.Allocator, *const vk.MemoryAllocateInfo) VkError!*DeviceMemory,
createCommandPool: *const fn (*Self, std.mem.Allocator, *const vk.CommandPoolCreateInfo) VkError!*CommandPool,
createFence: *const fn (*Self, std.mem.Allocator, *const vk.FenceCreateInfo) VkError!*Fence,
destroy: *const fn (*Self, std.mem.Allocator) VkError!void,
destroyCommandPool: *const fn (*Self, std.mem.Allocator, *CommandPool) VkError!void,
destroyFence: *const fn (*Self, std.mem.Allocator, *Fence) VkError!void,
freeMemory: *const fn (*Self, std.mem.Allocator, *DeviceMemory) VkError!void,
getFenceStatus: *const fn (*Self, *Fence) VkError!void,
destroy: *const fn (*Self, std.mem.Allocator) VkError!void,
resetFences: *const fn (*Self, []*Fence) VkError!void,
waitForFences: *const fn (*Self, []*Fence, bool, u64) VkError!void,
};
@@ -66,7 +71,7 @@ pub fn createQueues(self: *Self, allocator: std.mem.Allocator, info: *const vk.D
}
}
pub fn destroy(self: *Self, allocator: std.mem.Allocator) VkError!void {
pub inline fn destroy(self: *Self, allocator: std.mem.Allocator) VkError!void {
var it = self.queues.iterator();
while (it.next()) |entry| {
const family = entry.value_ptr;
@@ -102,6 +107,16 @@ pub inline fn waitForFences(self: *Self, fences: []*Fence, waitForAll: bool, tim
try self.dispatch_table.waitForFences(self, fences, waitForAll, timeout);
}
// Command Pool functions ============================================================================================================================
pub inline fn createCommandPool(self: *Self, allocator: std.mem.Allocator, info: *const vk.CommandPoolCreateInfo) VkError!*CommandPool {
return self.dispatch_table.createCommandPool(self, allocator, info);
}
pub inline fn destroyCommandPool(self: *Self, allocator: std.mem.Allocator, pool: *CommandPool) VkError!void {
try self.dispatch_table.destroyCommandPool(self, allocator, pool);
}
// Memory functions ==================================================================================================================================
pub inline fn allocateMemory(self: *Self, allocator: std.mem.Allocator, info: *const vk.MemoryAllocateInfo) VkError!*DeviceMemory {

View File

@@ -7,7 +7,7 @@ const Device = @import("Device.zig");
const Self = @This();
pub const ObjectType: vk.ObjectType = .fence;
owner: *const Device,
owner: *Device,
flags: vk.FenceCreateFlags,
vtable: *const VTable,
@@ -20,7 +20,8 @@ pub const VTable = struct {
wait: *const fn (*Self, u64) VkError!void,
};
pub fn init(device: *const Device, info: *const vk.FenceCreateInfo) VkError!Self {
pub fn init(device: *Device, allocator: std.mem.Allocator, info: *const vk.FenceCreateInfo) VkError!Self {
_ = allocator;
return .{
.owner = device,
.flags = info.flags,

View File

@@ -35,7 +35,7 @@ pub fn init(allocator: std.mem.Allocator, instance: *const Instance) VkError!Sel
.device_type = undefined,
.device_name = [_]u8{0} ** vk.MAX_PHYSICAL_DEVICE_NAME_SIZE,
.pipeline_cache_uuid = undefined,
.limits = undefined,
.limits = std.mem.zeroInit(vk.PhysicalDeviceLimits, .{}),
.sparse_properties = undefined,
},
.mem_props = .{

View File

@@ -14,6 +14,7 @@ pub const Device = @import("Device.zig");
pub const PhysicalDevice = @import("PhysicalDevice.zig");
pub const Queue = @import("Queue.zig");
pub const CommandPool = @import("CommandPool.zig");
pub const DeviceMemory = @import("DeviceMemory.zig");
pub const Fence = @import("Fence.zig");

View File

@@ -19,6 +19,7 @@ const Device = @import("Device.zig");
const PhysicalDevice = @import("PhysicalDevice.zig");
const Queue = @import("Queue.zig");
const CommandPool = @import("CommandPool.zig");
const DeviceMemory = @import("DeviceMemory.zig");
const Fence = @import("Fence.zig");
@@ -73,9 +74,11 @@ const physical_device_pfn_map = std.StaticStringMap(vk.PfnVoidFunction).initComp
const device_pfn_map = std.StaticStringMap(vk.PfnVoidFunction).initComptime(.{
functionMapEntryPoint("vkAllocateMemory"),
functionMapEntryPoint("vkCreateCommandPool"),
functionMapEntryPoint("vkCreateFence"),
functionMapEntryPoint("vkDestroyCommandPool"),
functionMapEntryPoint("vkDestroyFence"),
functionMapEntryPoint("vkDestroyDevice"),
functionMapEntryPoint("vkCreateFence"),
functionMapEntryPoint("vkFreeMemory"),
functionMapEntryPoint("vkGetDeviceQueue"),
functionMapEntryPoint("vkGetFenceStatus"),
@@ -291,6 +294,18 @@ pub export fn strollAllocateMemory(p_device: vk.Device, p_info: ?*const vk.Memor
return .success;
}
pub export fn strollCreateCommandPool(p_device: vk.Device, p_info: ?*const vk.CommandPoolCreateInfo, callbacks: ?*const vk.AllocationCallbacks, p_pool: *vk.CommandPool) callconv(vk.vulkan_call_conv) vk.Result {
const info = p_info orelse return .error_validation_failed;
if (info.s_type != .command_pool_create_info) {
return .error_validation_failed;
}
const allocator = VulkanAllocator.init(callbacks, .object).allocator();
const device = Dispatchable(Device).fromHandleObject(p_device) catch |err| return toVkResult(err);
const pool = device.createCommandPool(allocator, info) catch |err| return toVkResult(err);
p_pool.* = (NonDispatchable(CommandPool).wrap(allocator, pool) catch |err| return toVkResult(err)).toVkHandle(vk.CommandPool);
return .success;
}
pub export fn strollCreateFence(p_device: vk.Device, p_info: ?*const vk.FenceCreateInfo, callbacks: ?*const vk.AllocationCallbacks, p_fence: *vk.Fence) callconv(vk.vulkan_call_conv) vk.Result {
const info = p_info orelse return .error_validation_failed;
if (info.s_type != .fence_create_info) {
@@ -314,6 +329,15 @@ pub export fn strollDestroyDevice(p_device: vk.Device, callbacks: ?*const vk.All
dispatchable.destroy(allocator);
}
pub export fn strollDestroyCommandPool(p_device: vk.Device, p_pool: vk.CommandPool, callbacks: ?*const vk.AllocationCallbacks) callconv(vk.vulkan_call_conv) void {
const allocator = VulkanAllocator.init(callbacks, .object).allocator();
const device = Dispatchable(Device).fromHandleObject(p_device) catch return;
const non_dispatchable_pool = NonDispatchable(CommandPool).fromHandle(p_pool) catch return;
device.destroyCommandPool(allocator, non_dispatchable_pool.object) catch return;
non_dispatchable_pool.destroy(allocator);
}
pub export fn strollDestroyFence(p_device: vk.Device, p_fence: vk.Fence, callbacks: ?*const vk.AllocationCallbacks) callconv(vk.vulkan_call_conv) void {
const allocator = VulkanAllocator.init(callbacks, .object).allocator();
const device = Dispatchable(Device).fromHandleObject(p_device) catch return;