From 2928517451de1dbc529c781078f9e2b9fa381cc1 Mon Sep 17 00:00:00 2001 From: Kbz-8 Date: Fri, 14 Nov 2025 17:32:37 +0100 Subject: [PATCH] improving vulkan allocator; ci skip --- .github/workflows/Build.yml | 2 ++ .github/workflows/Test.yml | 2 ++ src/vulkan/CommandPool.zig | 3 +++ src/vulkan/Device.zig | 8 ++++++-- src/vulkan/VulkanAllocator.zig | 16 ++++++++++++++++ src/vulkan/lib_vulkan.zig | 12 ++++++++++++ 6 files changed, 41 insertions(+), 2 deletions(-) diff --git a/.github/workflows/Build.yml b/.github/workflows/Build.yml index 0b0bc92..5475ae8 100644 --- a/.github/workflows/Build.yml +++ b/.github/workflows/Build.yml @@ -9,6 +9,8 @@ on: jobs: build: runs-on: ubuntu-latest + if: "!contains(github.event.head_commit.message, 'ci skip')" + steps: - uses: actions/checkout@v4 - uses: mlugg/setup-zig@v2 diff --git a/.github/workflows/Test.yml b/.github/workflows/Test.yml index 892abff..8f6e10b 100644 --- a/.github/workflows/Test.yml +++ b/.github/workflows/Test.yml @@ -9,6 +9,8 @@ on: jobs: build: runs-on: ubuntu-latest + if: "!contains(github.event.head_commit.message, 'ci skip')" + steps: - uses: actions/checkout@v4 - uses: mlugg/setup-zig@v2 diff --git a/src/vulkan/CommandPool.zig b/src/vulkan/CommandPool.zig index 0b4490e..4615354 100644 --- a/src/vulkan/CommandPool.zig +++ b/src/vulkan/CommandPool.zig @@ -2,6 +2,7 @@ const std = @import("std"); const vk = @import("vulkan"); const VkError = @import("error_set.zig").VkError; +const VulkanAllocator = @import("VulkanAllocator.zig"); const CommandBuffer = @import("CommandBuffer.zig"); const Device = @import("Device.zig"); @@ -15,6 +16,7 @@ flags: vk.CommandPoolCreateFlags, queue_family_index: u32, buffers: std.ArrayList(*CommandBuffer), first_free_buffer_index: usize, +host_allocator: VulkanAllocator, vtable: *const VTable, @@ -30,6 +32,7 @@ pub fn init(device: *Device, allocator: std.mem.Allocator, info: *const vk.Comma .flags = info.flags, .queue_family_index = info.queue_family_index, .buffers = .initCapacity(allocator, BUFFER_POOL_BASE_CAPACITY) catch return VkError.OutOfHostMemory, + .host_allocator = VulkanAllocator.from(allocator).clone(), .first_free_buffer_index = 0, .vtable = undefined, }; diff --git a/src/vulkan/Device.zig b/src/vulkan/Device.zig index f93be73..2012ede 100644 --- a/src/vulkan/Device.zig +++ b/src/vulkan/Device.zig @@ -17,7 +17,7 @@ pub const ObjectType: vk.ObjectType = .device; physical_device: *const PhysicalDevice, queues: std.AutoArrayHashMapUnmanaged(u32, std.ArrayList(*Dispatchable(Queue))), -host_allocator: *VulkanAllocator, +host_allocator: VulkanAllocator, dispatch_table: *const DispatchTable, vtable: *const VTable, @@ -45,7 +45,7 @@ pub fn init(allocator: std.mem.Allocator, physical_device: *const PhysicalDevice return .{ .physical_device = physical_device, .queues = .empty, - .host_allocator = @ptrCast(@alignCast(allocator.ptr)), + .host_allocator = VulkanAllocator.from(allocator).clone(), .dispatch_table = undefined, .vtable = undefined, }; @@ -110,6 +110,10 @@ pub inline fn waitForFences(self: *Self, fences: []*Fence, waitForAll: bool, tim // Command Pool functions ============================================================================================================================ +pub inline fn allocateCommandBuffers(self: *Self, info: *const vk.CommandPoolCreateInfo) VkError![]*CommandBuffer { + return self.dispatch_table.createCommandPool(self, allocator, info); +} + pub inline fn createCommandPool(self: *Self, allocator: std.mem.Allocator, info: *const vk.CommandPoolCreateInfo) VkError!*CommandPool { return self.dispatch_table.createCommandPool(self, allocator, info); } diff --git a/src/vulkan/VulkanAllocator.zig b/src/vulkan/VulkanAllocator.zig index a3dafa6..ca5c016 100644 --- a/src/vulkan/VulkanAllocator.zig +++ b/src/vulkan/VulkanAllocator.zig @@ -37,6 +37,22 @@ pub fn allocator(self: *const Self) Allocator { }; } +pub fn from(a: Allocator) *Self { + const self: *Self = @ptrCast(@alignCast(a.ptr)); + return self; +} + +pub fn clone(self: *Self) Self { + return self.cloneWithScope(self.scope); +} + +pub fn cloneWithScope(self: *Self, scope: vk.SystemAllocationScope) Self { + return .{ + .callbacks = self.callbacks, + .scope = scope, + }; +} + fn alloc(context: *anyopaque, len: usize, alignment: Alignment, ret_addr: usize) ?[*]u8 { const self: *Self = @ptrCast(@alignCast(context)); if (self.callbacks.?.pfn_allocation) |pfn_allocation| { diff --git a/src/vulkan/lib_vulkan.zig b/src/vulkan/lib_vulkan.zig index 58abc51..8537db8 100644 --- a/src/vulkan/lib_vulkan.zig +++ b/src/vulkan/lib_vulkan.zig @@ -73,6 +73,7 @@ const physical_device_pfn_map = std.StaticStringMap(vk.PfnVoidFunction).initComp }); const device_pfn_map = std.StaticStringMap(vk.PfnVoidFunction).initComptime(.{ + functionMapEntryPoint("vkAllocateCommandBuffers"), functionMapEntryPoint("vkAllocateMemory"), functionMapEntryPoint("vkCreateCommandPool"), functionMapEntryPoint("vkCreateFence"), @@ -282,6 +283,17 @@ pub export fn strollGetPhysicalDeviceSparseImageFormatProperties( // Device functions ========================================================================================================================================== +pub export fn strollAllocateCommandBuffers(p_device: vk.Device, p_info: ?*const vk.CommandBufferAllocateInfo, p_cmds: [*]vk.CommandBuffer) callconv(vk.vulkan_call_conv) vk.Result { + const info = p_info orelse return .error_validation_failed; + if (info.s_type != .command_buffer_allocate_info) { + return .error_validation_failed; + } + const device = Dispatchable(Device).fromHandleObject(p_device) catch |err| return toVkResult(err); + const cmds = device.allocateCommandBuffers(info) catch |err| return toVkResult(err); + @memcpy(p_cmds[0..info.command_buffer_count], cmds[0..info.command_buffer_count]); + return .success; +} + pub export fn strollAllocateMemory(p_device: vk.Device, p_info: ?*const vk.MemoryAllocateInfo, callbacks: ?*const vk.AllocationCallbacks, p_memory: *vk.DeviceMemory) callconv(vk.vulkan_call_conv) vk.Result { const info = p_info orelse return .error_validation_failed; if (info.s_type != .memory_allocate_info) {