adding public queue functions and worker base

This commit is contained in:
2025-11-12 17:40:54 +01:00
parent d03515c335
commit 9a0cc0d03d
9 changed files with 96 additions and 17 deletions

Binary file not shown.

View File

@@ -25,6 +25,13 @@ Then ensure thy Vulkan loader is pointed toward the ICD manifest.
The precise ritual varies by system — consult the tomes of your operating system, or wander the webs endless mausoleum of documentation. The precise ritual varies by system — consult the tomes of your operating system, or wander the webs endless mausoleum of documentation.
Use at your own risk. If thy machine shudders, weeps, or attempts to flee — know that it was warned. Use at your own risk. If thy machine shudders, weeps, or attempts to flee — know that it was warned.
\
\
\
Thou may also conjure forth a tome of compile commands by doing thus:
```
zig build cdb
```
## License ## License

View File

@@ -1,6 +1,8 @@
const std = @import("std"); const std = @import("std");
const Step = std.Build.Step; const Step = std.Build.Step;
const zcc = @import("compile_commands");
const ImplementationDesc = struct { const ImplementationDesc = struct {
name: []const u8, name: []const u8,
root_source_file: []const u8, root_source_file: []const u8,
@@ -15,7 +17,7 @@ const implementations = [_]ImplementationDesc{
}, },
}; };
pub fn build(b: *std.Build) void { pub fn build(b: *std.Build) !void {
const target = b.standardTargetOptions(.{}); const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{}); const optimize = b.standardOptimizeOption(.{});
@@ -38,6 +40,8 @@ pub fn build(b: *std.Build) void {
base_mod.addSystemIncludePath(vulkan_headers.path("include")); base_mod.addSystemIncludePath(vulkan_headers.path("include"));
for (implementations) |impl| { for (implementations) |impl| {
var targets = std.ArrayListUnmanaged(*std.Build.Step.Compile){};
const lib_mod = b.createModule(.{ const lib_mod = b.createModule(.{
.root_source_file = b.path(impl.root_source_file), .root_source_file = b.path(impl.root_source_file),
.target = target, .target = target,
@@ -90,6 +94,11 @@ pub fn build(b: *std.Build) void {
const c_test_exe_install = b.addInstallArtifact(c_test_exe, .{}); const c_test_exe_install = b.addInstallArtifact(c_test_exe, .{});
try targets.append(b.allocator, lib);
try targets.append(b.allocator, c_test_exe);
_ = zcc.createStep(b, "cdb", try targets.toOwnedSlice(b.allocator));
const run_c_test = b.addRunArtifact(c_test_exe); const run_c_test = b.addRunArtifact(c_test_exe);
run_c_test.step.dependOn(&lib_install.step); run_c_test.step.dependOn(&lib_install.step);
run_c_test.step.dependOn(&c_test_exe_install.step); run_c_test.step.dependOn(&c_test_exe_install.step);

View File

@@ -27,6 +27,10 @@
.hash = "cpuinfo-0.1.0-V7dMLcghAADJuG7dkd3MnwDPZ232pBK_8uGjxY43eP5u", .hash = "cpuinfo-0.1.0-V7dMLcghAADJuG7dkd3MnwDPZ232pBK_8uGjxY43eP5u",
.lazy = true, .lazy = true,
}, },
.compile_commands = .{
.url = "git+https://github.com/the-argus/zig-compile-commands",
.hash = "zig_compile_commands-0.0.1-OZg5-a3CAACM-h32Kjb1obTMqrKGs9YoDhorVZ8-LGle",
},
}, },
.paths = .{ .paths = .{

View File

@@ -2,6 +2,7 @@ const std = @import("std");
const vk = @import("vulkan"); const vk = @import("vulkan");
const base = @import("base"); const base = @import("base");
const SoftDevice = @import("SoftDevice.zig");
const SoftDeviceMemory = @import("SoftDeviceMemory.zig"); const SoftDeviceMemory = @import("SoftDeviceMemory.zig");
const SoftFence = @import("SoftFence.zig"); const SoftFence = @import("SoftFence.zig");
@@ -11,9 +12,11 @@ const Self = @This();
pub const Interface = base.Queue; pub const Interface = base.Queue;
interface: Interface, interface: Interface,
wait_group: std.Thread.WaitGroup,
mutex: std.Thread.Mutex, mutex: std.Thread.Mutex,
worker_mutex: std.Thread.Mutex,
pub fn create(allocator: std.mem.Allocator, device: *const base.Device, index: u32, family_index: u32, flags: vk.DeviceQueueCreateFlags) VkError!*Interface { pub fn create(allocator: std.mem.Allocator, device: *base.Device, index: u32, family_index: u32, flags: vk.DeviceQueueCreateFlags) VkError!*Interface {
const self = allocator.create(Self) catch return VkError.OutOfHostMemory; const self = allocator.create(Self) catch return VkError.OutOfHostMemory;
errdefer allocator.destroy(self); errdefer allocator.destroy(self);
@@ -27,7 +30,9 @@ pub fn create(allocator: std.mem.Allocator, device: *const base.Device, index: u
self.* = .{ self.* = .{
.interface = interface, .interface = interface,
.wait_group = .{},
.mutex = .{}, .mutex = .{},
.worker_mutex = .{},
}; };
return &self.interface; return &self.interface;
} }
@@ -37,21 +42,46 @@ pub fn destroy(interface: *Interface, allocator: std.mem.Allocator) VkError!void
allocator.destroy(self); allocator.destroy(self);
} }
pub fn bindSparse(interface: *Interface, info: []*const vk.BindSparseInfo, fence: ?*base.Fence) VkError!void { pub fn bindSparse(interface: *Interface, info: []const vk.BindSparseInfo, fence: ?*base.Fence) VkError!void {
const self: *Self = @alignCast(@fieldParentPtr("interface", interface)); const self: *Self = @alignCast(@fieldParentPtr("interface", interface));
_ = self; _ = self;
_ = info; _ = info;
_ = fence; _ = fence;
return VkError.FeatureNotPresent;
} }
pub fn submit(interface: *Interface, info: []*const vk.SubmitInfo, fence: ?*base.Fence) VkError!void { pub fn submit(interface: *Interface, info: []const vk.SubmitInfo, fence: ?*base.Fence) VkError!void {
const self: *Self = @alignCast(@fieldParentPtr("interface", interface)); var self: *Self = @alignCast(@fieldParentPtr("interface", interface));
_ = self;
_ = info; _ = info;
_ = fence;
const Runner = struct {
fn run(queue: *Self, p_fence: ?*base.Fence) void {
// Waiting for older submits to finish execution
queue.worker_mutex.lock();
defer queue.worker_mutex.unlock();
// TODO: commands executions
std.log.debug("Queue execution", .{});
std.Thread.sleep(1_000_000_000);
if (p_fence) |fence_obj| {
fence_obj.signal() catch {};
}
}
};
self.mutex.lock();
defer self.mutex.unlock();
var soft_device: *SoftDevice = @alignCast(@fieldParentPtr("interface", interface.owner));
soft_device.workers.spawnWg(&self.wait_group, Runner.run, .{ self, fence });
} }
pub fn waitIdle(interface: *Interface) VkError!void { pub fn waitIdle(interface: *Interface) VkError!void {
const self: *Self = @alignCast(@fieldParentPtr("interface", interface)); const self: *Self = @alignCast(@fieldParentPtr("interface", interface));
_ = self;
self.mutex.lock();
defer self.mutex.unlock();
self.wait_group.wait();
} }

View File

@@ -19,7 +19,7 @@ dispatch_table: *const DispatchTable,
vtable: *const VTable, vtable: *const VTable,
pub const VTable = struct { pub const VTable = struct {
createQueue: *const fn (std.mem.Allocator, *const Self, u32, u32, vk.DeviceQueueCreateFlags) VkError!*Queue, createQueue: *const fn (std.mem.Allocator, *Self, u32, u32, vk.DeviceQueueCreateFlags) VkError!*Queue,
destroyQueue: *const fn (*Queue, std.mem.Allocator) VkError!void, destroyQueue: *const fn (*Queue, std.mem.Allocator) VkError!void,
}; };

View File

@@ -8,7 +8,7 @@ const Fence = @import("Fence.zig");
const Self = @This(); const Self = @This();
pub const ObjectType: vk.ObjectType = .queue; pub const ObjectType: vk.ObjectType = .queue;
owner: *const Device, owner: *Device,
family_index: u32, family_index: u32,
index: u32, index: u32,
flags: vk.DeviceQueueCreateFlags, flags: vk.DeviceQueueCreateFlags,
@@ -16,12 +16,12 @@ flags: vk.DeviceQueueCreateFlags,
dispatch_table: *const DispatchTable, dispatch_table: *const DispatchTable,
pub const DispatchTable = struct { pub const DispatchTable = struct {
bindSparse: *const fn (*Self, []*const vk.BindSparseInfo, ?*Fence) VkError!void, bindSparse: *const fn (*Self, []const vk.BindSparseInfo, ?*Fence) VkError!void,
submit: *const fn (*Self, []*const vk.SubmitInfo, ?*Fence) VkError!void, submit: *const fn (*Self, []const vk.SubmitInfo, ?*Fence) VkError!void,
waitIdle: *const fn (*Self) VkError!void, waitIdle: *const fn (*Self) VkError!void,
}; };
pub fn init(allocator: std.mem.Allocator, device: *const Device, index: u32, family_index: u32, flags: vk.DeviceQueueCreateFlags) VkError!Self { pub fn init(allocator: std.mem.Allocator, device: *Device, index: u32, family_index: u32, flags: vk.DeviceQueueCreateFlags) VkError!Self {
std.log.scoped(.vkCreateDevice).info("Creating device queue with family index {d} and index {d}", .{ family_index, index }); std.log.scoped(.vkCreateDevice).info("Creating device queue with family index {d} and index {d}", .{ family_index, index });
_ = allocator; _ = allocator;
return .{ return .{
@@ -33,11 +33,11 @@ pub fn init(allocator: std.mem.Allocator, device: *const Device, index: u32, fam
}; };
} }
pub inline fn bindSparse(self: *Self, info: []*const vk.BindSparseInfo, fence: ?*Fence) VkError!void { pub inline fn bindSparse(self: *Self, info: []const vk.BindSparseInfo, fence: ?*Fence) VkError!void {
try self.dispatch_table.bindSparse(self, info, fence); try self.dispatch_table.bindSparse(self, info, fence);
} }
pub inline fn submit(self: *Self, info: []*const vk.SubmitInfo, fence: ?*Fence) VkError!void { pub inline fn submit(self: *Self, info: []const vk.SubmitInfo, fence: ?*Fence) VkError!void {
try self.dispatch_table.submit(self, info, fence); try self.dispatch_table.submit(self, info, fence);
} }

View File

@@ -16,9 +16,11 @@ const VulkanAllocator = @import("VulkanAllocator.zig");
const Instance = @import("Instance.zig"); const Instance = @import("Instance.zig");
const Device = @import("Device.zig"); const Device = @import("Device.zig");
const PhysicalDevice = @import("PhysicalDevice.zig");
const Queue = @import("Queue.zig");
const DeviceMemory = @import("DeviceMemory.zig"); const DeviceMemory = @import("DeviceMemory.zig");
const Fence = @import("Fence.zig"); const Fence = @import("Fence.zig");
const PhysicalDevice = @import("PhysicalDevice.zig");
// This file contains all exported Vulkan entrypoints. // This file contains all exported Vulkan entrypoints.
@@ -80,6 +82,9 @@ const device_pfn_map = std.StaticStringMap(vk.PfnVoidFunction).initComptime(.{
functionMapEntryPoint("vkMapMemory"), functionMapEntryPoint("vkMapMemory"),
functionMapEntryPoint("vkUnmapMemory"), functionMapEntryPoint("vkUnmapMemory"),
functionMapEntryPoint("vkResetFences"), functionMapEntryPoint("vkResetFences"),
functionMapEntryPoint("vkQueueBindSparse"),
functionMapEntryPoint("vkQueueSubmit"),
functionMapEntryPoint("vkQueueWaitIdle"),
functionMapEntryPoint("vkWaitForFences"), functionMapEntryPoint("vkWaitForFences"),
}); });
@@ -389,6 +394,26 @@ pub export fn strollResetFences(p_device: vk.Device, count: u32, p_fences: [*]co
return .success; return .success;
} }
pub export fn strollQueueBindSparse(p_queue: vk.Queue, count: u32, info: [*]vk.BindSparseInfo, p_fence: vk.Fence) callconv(vk.vulkan_call_conv) vk.Result {
const queue = Dispatchable(Queue).fromHandleObject(p_queue) catch |err| return toVkResult(err);
const fence = if (p_fence != .null_handle) NonDispatchable(Fence).fromHandleObject(p_fence) catch |err| return toVkResult(err) else null;
queue.bindSparse(info[0..count], fence) catch |err| return toVkResult(err);
return .success;
}
pub export fn strollQueueSubmit(p_queue: vk.Queue, count: u32, info: [*]const vk.SubmitInfo, p_fence: vk.Fence) callconv(vk.vulkan_call_conv) vk.Result {
const queue = Dispatchable(Queue).fromHandleObject(p_queue) catch |err| return toVkResult(err);
const fence = if (p_fence != .null_handle) NonDispatchable(Fence).fromHandleObject(p_fence) catch |err| return toVkResult(err) else null;
queue.submit(info[0..count], fence) catch |err| return toVkResult(err);
return .success;
}
pub export fn strollQueueWaitIdle(p_queue: vk.Queue) callconv(vk.vulkan_call_conv) vk.Result {
const queue = Dispatchable(Queue).fromHandleObject(p_queue) catch |err| return toVkResult(err);
queue.waitIdle() catch |err| return toVkResult(err);
return .success;
}
pub export fn strollWaitForFences(p_device: vk.Device, count: u32, p_fences: [*]const vk.Fence, waitForAll: vk.Bool32, timeout: u64) callconv(vk.vulkan_call_conv) vk.Result { pub export fn strollWaitForFences(p_device: vk.Device, count: u32, p_fences: [*]const vk.Fence, waitForAll: vk.Bool32, timeout: u64) callconv(vk.vulkan_call_conv) vk.Result {
const device = Dispatchable(Device).fromHandleObject(p_device) catch |err| return toVkResult(err); const device = Dispatchable(Device).fromHandleObject(p_device) catch |err| return toVkResult(err);
const allocator = std.heap.c_allocator; const allocator = std.heap.c_allocator;

View File

@@ -85,7 +85,6 @@ int main(void)
VkQueue queue = VK_NULL_HANDLE; VkQueue queue = VK_NULL_HANDLE;
vkGetDeviceQueue(device, 1, 0, &queue); vkGetDeviceQueue(device, 1, 0, &queue);
printf("VkQueue %p\n", queue);
VkFenceCreateInfo fence_info = {}; VkFenceCreateInfo fence_info = {};
fence_info.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO; fence_info.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO;
@@ -93,6 +92,11 @@ int main(void)
VkFence fence = VK_NULL_HANDLE; VkFence fence = VK_NULL_HANDLE;
CheckVk(vkCreateFence(device, &fence_info, NULL, &fence)); CheckVk(vkCreateFence(device, &fence_info, NULL, &fence));
CheckVk(vkQueueSubmit(queue, 0, NULL, fence));
CheckVk(vkQueueSubmit(queue, 0, NULL, fence));
CheckVk(vkQueueSubmit(queue, 0, NULL, fence));
CheckVk(vkQueueWaitIdle(queue));
vkDestroyFence(device, fence, NULL); vkDestroyFence(device, fence, NULL);
vkDestroyDevice(device, NULL); vkDestroyDevice(device, NULL);