diff --git a/.cache/clangd/index/main.c.30565AEBA22FFC9B.idx b/.cache/clangd/index/main.c.30565AEBA22FFC9B.idx new file mode 100644 index 0000000..db25210 Binary files /dev/null and b/.cache/clangd/index/main.c.30565AEBA22FFC9B.idx differ diff --git a/README.md b/README.md index 058ef42..639fca1 100644 --- a/README.md +++ b/README.md @@ -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 web’s endless mausoleum of documentation. 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 diff --git a/build.zig b/build.zig index 841fe77..b8b2ec4 100644 --- a/build.zig +++ b/build.zig @@ -1,6 +1,8 @@ const std = @import("std"); const Step = std.Build.Step; +const zcc = @import("compile_commands"); + const ImplementationDesc = struct { name: []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 optimize = b.standardOptimizeOption(.{}); @@ -38,6 +40,8 @@ pub fn build(b: *std.Build) void { base_mod.addSystemIncludePath(vulkan_headers.path("include")); for (implementations) |impl| { + var targets = std.ArrayListUnmanaged(*std.Build.Step.Compile){}; + const lib_mod = b.createModule(.{ .root_source_file = b.path(impl.root_source_file), .target = target, @@ -90,6 +94,11 @@ pub fn build(b: *std.Build) void { 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); run_c_test.step.dependOn(&lib_install.step); run_c_test.step.dependOn(&c_test_exe_install.step); diff --git a/build.zig.zon b/build.zig.zon index c114eb3..71036a8 100644 --- a/build.zig.zon +++ b/build.zig.zon @@ -27,6 +27,10 @@ .hash = "cpuinfo-0.1.0-V7dMLcghAADJuG7dkd3MnwDPZ232pBK_8uGjxY43eP5u", .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 = .{ diff --git a/src/soft/SoftQueue.zig b/src/soft/SoftQueue.zig index e45b11c..5ddbaee 100644 --- a/src/soft/SoftQueue.zig +++ b/src/soft/SoftQueue.zig @@ -2,6 +2,7 @@ const std = @import("std"); const vk = @import("vulkan"); const base = @import("base"); +const SoftDevice = @import("SoftDevice.zig"); const SoftDeviceMemory = @import("SoftDeviceMemory.zig"); const SoftFence = @import("SoftFence.zig"); @@ -11,9 +12,11 @@ const Self = @This(); pub const Interface = base.Queue; interface: Interface, +wait_group: std.Thread.WaitGroup, 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; errdefer allocator.destroy(self); @@ -27,7 +30,9 @@ pub fn create(allocator: std.mem.Allocator, device: *const base.Device, index: u self.* = .{ .interface = interface, + .wait_group = .{}, .mutex = .{}, + .worker_mutex = .{}, }; return &self.interface; } @@ -37,21 +42,46 @@ pub fn destroy(interface: *Interface, allocator: std.mem.Allocator) VkError!void 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)); _ = self; _ = info; _ = fence; + return VkError.FeatureNotPresent; } -pub fn submit(interface: *Interface, info: []*const vk.SubmitInfo, fence: ?*base.Fence) VkError!void { - const self: *Self = @alignCast(@fieldParentPtr("interface", interface)); - _ = self; +pub fn submit(interface: *Interface, info: []const vk.SubmitInfo, fence: ?*base.Fence) VkError!void { + var self: *Self = @alignCast(@fieldParentPtr("interface", interface)); _ = 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 { const self: *Self = @alignCast(@fieldParentPtr("interface", interface)); - _ = self; + + self.mutex.lock(); + defer self.mutex.unlock(); + + self.wait_group.wait(); } diff --git a/src/vulkan/Device.zig b/src/vulkan/Device.zig index a49ccf8..a840927 100644 --- a/src/vulkan/Device.zig +++ b/src/vulkan/Device.zig @@ -19,7 +19,7 @@ dispatch_table: *const DispatchTable, vtable: *const VTable, 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, }; diff --git a/src/vulkan/Queue.zig b/src/vulkan/Queue.zig index 06c886d..5bcfa50 100644 --- a/src/vulkan/Queue.zig +++ b/src/vulkan/Queue.zig @@ -8,7 +8,7 @@ const Fence = @import("Fence.zig"); const Self = @This(); pub const ObjectType: vk.ObjectType = .queue; -owner: *const Device, +owner: *Device, family_index: u32, index: u32, flags: vk.DeviceQueueCreateFlags, @@ -16,12 +16,12 @@ flags: vk.DeviceQueueCreateFlags, dispatch_table: *const DispatchTable, pub const DispatchTable = struct { - bindSparse: *const fn (*Self, []*const vk.BindSparseInfo, ?*Fence) VkError!void, - submit: *const fn (*Self, []*const vk.SubmitInfo, ?*Fence) VkError!void, + bindSparse: *const fn (*Self, []const vk.BindSparseInfo, ?*Fence) VkError!void, + submit: *const fn (*Self, []const vk.SubmitInfo, ?*Fence) 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 }); _ = allocator; 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); } -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); } diff --git a/src/vulkan/lib_vulkan.zig b/src/vulkan/lib_vulkan.zig index 3a60bba..4d5d208 100644 --- a/src/vulkan/lib_vulkan.zig +++ b/src/vulkan/lib_vulkan.zig @@ -16,9 +16,11 @@ const VulkanAllocator = @import("VulkanAllocator.zig"); const Instance = @import("Instance.zig"); const Device = @import("Device.zig"); +const PhysicalDevice = @import("PhysicalDevice.zig"); +const Queue = @import("Queue.zig"); + const DeviceMemory = @import("DeviceMemory.zig"); const Fence = @import("Fence.zig"); -const PhysicalDevice = @import("PhysicalDevice.zig"); // This file contains all exported Vulkan entrypoints. @@ -80,6 +82,9 @@ const device_pfn_map = std.StaticStringMap(vk.PfnVoidFunction).initComptime(.{ functionMapEntryPoint("vkMapMemory"), functionMapEntryPoint("vkUnmapMemory"), functionMapEntryPoint("vkResetFences"), + functionMapEntryPoint("vkQueueBindSparse"), + functionMapEntryPoint("vkQueueSubmit"), + functionMapEntryPoint("vkQueueWaitIdle"), functionMapEntryPoint("vkWaitForFences"), }); @@ -389,6 +394,26 @@ pub export fn strollResetFences(p_device: vk.Device, count: u32, p_fences: [*]co 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 { const device = Dispatchable(Device).fromHandleObject(p_device) catch |err| return toVkResult(err); const allocator = std.heap.c_allocator; diff --git a/test/c/main.c b/test/c/main.c index 40c8e6b..04a6972 100644 --- a/test/c/main.c +++ b/test/c/main.c @@ -85,7 +85,6 @@ int main(void) VkQueue queue = VK_NULL_HANDLE; vkGetDeviceQueue(device, 1, 0, &queue); - printf("VkQueue %p\n", queue); VkFenceCreateInfo fence_info = {}; fence_info.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO; @@ -93,6 +92,11 @@ int main(void) VkFence fence = VK_NULL_HANDLE; 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); vkDestroyDevice(device, NULL);