From 6215a20ed6ca867f55a81c7ad083ea98016c6f58 Mon Sep 17 00:00:00 2001 From: Kbz-8 Date: Mon, 10 Nov 2025 21:19:37 +0100 Subject: [PATCH] working on queues --- src/soft/SoftDevice.zig | 2 +- src/soft/SoftInstance.zig | 2 +- src/soft/SoftPhysicalDevice.zig | 2 +- src/soft/SoftQueue.zig | 38 +++++++++++++++++++++++++++++++++ src/vulkan/Device.zig | 5 +++++ src/vulkan/Instance.zig | 2 +- src/vulkan/Queue.zig | 6 +++--- 7 files changed, 50 insertions(+), 7 deletions(-) diff --git a/src/soft/SoftDevice.zig b/src/soft/SoftDevice.zig index 7d64664..e8193a1 100644 --- a/src/soft/SoftDevice.zig +++ b/src/soft/SoftDevice.zig @@ -39,7 +39,7 @@ pub fn create(physical_device: *base.PhysicalDevice, allocator: std.mem.Allocato .workers = undefined, }; - self.workers.init(.{ .allocator = std.heap.c_allocator }) catch |err| return switch (err) { + self.workers.init(.{ .allocator = self.interface.host_allocator.allocator() }) catch |err| return switch (err) { SpawnError.OutOfMemory, SpawnError.LockedMemoryLimitExceeded => VkError.OutOfDeviceMemory, else => VkError.Unknown, }; diff --git a/src/soft/SoftInstance.zig b/src/soft/SoftInstance.zig index 1b93705..351947f 100644 --- a/src/soft/SoftInstance.zig +++ b/src/soft/SoftInstance.zig @@ -29,7 +29,7 @@ fn requestPhysicalDevices(interface: *Interface, allocator: std.mem.Allocator) V // Software driver only has one physical device (the CPU) const physical_device = try SoftPhysicalDevice.create(allocator, interface); errdefer physical_device.interface.releasePhysicalDevice(allocator) catch {}; - interface.physical_devices.append(allocator, try Dispatchable(SoftPhysicalDevice.Interface).wrap(allocator, &physical_device.interface)) catch return VkError.OutOfHostMemory; + interface.physical_devices.append(allocator, try Dispatchable(base.PhysicalDevice).wrap(allocator, &physical_device.interface)) catch return VkError.OutOfHostMemory; } fn releasePhysicalDevices(interface: *Interface, allocator: std.mem.Allocator) VkError!void { diff --git a/src/soft/SoftPhysicalDevice.zig b/src/soft/SoftPhysicalDevice.zig index fd6a09a..ced8b6c 100644 --- a/src/soft/SoftPhysicalDevice.zig +++ b/src/soft/SoftPhysicalDevice.zig @@ -90,7 +90,7 @@ pub fn create(allocator: std.mem.Allocator, instance: *const base.Instance) VkEr return self; } -pub fn createDevice(interface: *Interface, allocator: std.mem.Allocator, infos: *const vk.DeviceCreateInfo) VkError!*SoftDevice.Interface { +pub fn createDevice(interface: *Interface, allocator: std.mem.Allocator, infos: *const vk.DeviceCreateInfo) VkError!*base.Device { const device = try SoftDevice.create(interface, allocator, infos); return &device.interface; } diff --git a/src/soft/SoftQueue.zig b/src/soft/SoftQueue.zig index 324a1e8..f4a72c0 100644 --- a/src/soft/SoftQueue.zig +++ b/src/soft/SoftQueue.zig @@ -12,3 +12,41 @@ pub const Interface = base.Queue; interface: Interface, mutex: std.Thread.Mutex, + +pub fn create(allocator: std.mem.Allocator, device: *const base.Device, index: u32, family_index: u32, flags: vk.DeviceQueueCreateFlags) VkError!*Self { + const self = allocator.create(Self) catch return VkError.OutOfHostMemory; + errdefer allocator.destroy(self); + + var interface = try Interface.init(allocator, device, index, family_index, flags); + + interface.dispatch_table = &.{ + .bindSparse = bindSparse, + .submit = submit, + .waitIdle = waitIdle, + }; + + self.* = .{ + .interface = interface, + .mutex = .{}, + }; + return self; +} + +pub fn bindSparse(interface: *Interface, info: []*const vk.BindSparseInfo, fence: ?*base.Fence) VkError!void { + const self: *Self = @alignCast(@fieldParentPtr("interface", interface)); + _ = self; + _ = info; + _ = fence; +} + +pub fn submit(interface: *Interface, info: []*const vk.SubmitInfo, fence: ?*base.Fence) VkError!void { + const self: *Self = @alignCast(@fieldParentPtr("interface", interface)); + _ = self; + _ = info; + _ = fence; +} + +pub fn waitIdle(interface: *Interface) VkError!void { + const self: *Self = @alignCast(@fieldParentPtr("interface", interface)); + _ = self; +} diff --git a/src/vulkan/Device.zig b/src/vulkan/Device.zig index 169b73c..1a5bf4c 100644 --- a/src/vulkan/Device.zig +++ b/src/vulkan/Device.zig @@ -1,11 +1,13 @@ const std = @import("std"); 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 DeviceMemory = @import("DeviceMemory.zig"); const Fence = @import("Fence.zig"); +const Queue = @import("Queue.zig"); const Self = @This(); pub const ObjectType: vk.ObjectType = .device; @@ -13,6 +15,7 @@ pub const ObjectType: vk.ObjectType = .device; physical_device: *const PhysicalDevice, dispatch_table: *const DispatchTable, host_allocator: VulkanAllocator, +queues: std.AutoArrayHashMapUnmanaged(u32, *Dispatchable(Queue)), pub const DispatchTable = struct { allocateMemory: *const fn (*Self, std.mem.Allocator, *const vk.MemoryAllocateInfo) VkError!*DeviceMemory, @@ -32,10 +35,12 @@ pub fn init(allocator: std.mem.Allocator, physical_device: *const PhysicalDevice .physical_device = physical_device, .dispatch_table = undefined, .host_allocator = vulkan_allocator.*, + .queues = .empty, }; } pub fn destroy(self: *Self, allocator: std.mem.Allocator) VkError!void { + self.queues.deinit(allocator); try self.dispatch_table.destroy(self, allocator); } diff --git a/src/vulkan/Instance.zig b/src/vulkan/Instance.zig index 9a900cc..6ac494a 100644 --- a/src/vulkan/Instance.zig +++ b/src/vulkan/Instance.zig @@ -18,7 +18,7 @@ comptime { const Self = @This(); pub const ObjectType: vk.ObjectType = .instance; -physical_devices: std.ArrayList(*Dispatchable(PhysicalDevice)), +physical_devices: std.ArrayListUnmanaged(*Dispatchable(PhysicalDevice)), dispatch_table: *const DispatchTable, pub const DispatchTable = struct { diff --git a/src/vulkan/Queue.zig b/src/vulkan/Queue.zig index e1a6a2e..4aeb1ac 100644 --- a/src/vulkan/Queue.zig +++ b/src/vulkan/Queue.zig @@ -21,13 +21,13 @@ pub const DispatchTable = struct { waitIdle: *const fn (*Self) VkError!void, }; -pub fn init(allocator: std.mem.Allocator, device: *const Device, index: u32, info: vk.DeviceQueueCreateInfo) VkError!Self { +pub fn init(allocator: std.mem.Allocator, device: *const Device, index: u32, family_index: u32, flags: vk.DeviceQueueCreateFlags) VkError!Self { _ = allocator; return .{ .owner = device, - .family_index = info.queueFamilyIndex, + .family_index = family_index, .index = index, - .flags = info.flags, + .flags = flags, .dispatch_table = undefined, }; }