From 4b8aae9eb90fbd763a35d8fcfa0eec5dd06312f5 Mon Sep 17 00:00:00 2001 From: Kbz-8 Date: Thu, 4 Dec 2025 17:43:58 +0100 Subject: [PATCH] fixing some issues --- build.zig | 2 +- src/soft/SoftPhysicalDevice.zig | 29 +++++++++++++++++------------ src/vulkan/Device.zig | 6 ++++++ src/vulkan/Dispatchable.zig | 1 + src/vulkan/Instance.zig | 5 +++++ src/vulkan/NonDispatchable.zig | 1 + src/vulkan/PhysicalDevice.zig | 2 ++ src/vulkan/VulkanAllocator.zig | 12 ++---------- src/vulkan/lib_vulkan.zig | 7 ------- 9 files changed, 35 insertions(+), 30 deletions(-) diff --git a/build.zig b/build.zig index bc20f2d..3d83284 100644 --- a/build.zig +++ b/build.zig @@ -147,7 +147,7 @@ pub fn build(b: *std.Build) !void { const run_cts = b.addSystemCommand(&[_][]const u8{ try cts_exe_path.getPath3(b, null).toString(b.allocator), b.fmt("--deqp-caselist-file={s}", .{try cts.path("mustpass/1.0.0/vk-default.txt").getPath3(b, null).toString(b.allocator)}), - b.fmt("--deqp-vk-library-path={s}/{s}", .{ b.getInstallPath(.lib, ""), lib.out_lib_filename }), + b.fmt("--deqp-vk-library-path={s}", .{b.getInstallPath(.lib, lib.out_lib_filename)}), }); run_cts.step.dependOn(&lib_install.step); diff --git a/src/soft/SoftPhysicalDevice.zig b/src/soft/SoftPhysicalDevice.zig index e83fad4..c0453ab 100644 --- a/src/soft/SoftPhysicalDevice.zig +++ b/src/soft/SoftPhysicalDevice.zig @@ -12,6 +12,9 @@ const VulkanAllocator = base.VulkanAllocator; const Self = @This(); pub const Interface = base.PhysicalDevice; +// Device name should always be the same so avoid reprocessing it multiple times +var device_name = [_]u8{0} ** vk.MAX_PHYSICAL_DEVICE_NAME_SIZE; + interface: Interface, pub fn create(allocator: std.mem.Allocator, instance: *const base.Instance) VkError!*Self { @@ -34,7 +37,6 @@ pub fn create(allocator: std.mem.Allocator, instance: *const base.Instance) VkEr interface.props.driver_version = @bitCast(root.DRIVER_VERSION); interface.props.device_id = root.DEVICE_ID; interface.props.device_type = .cpu; - interface.props.limits = .{ .max_image_dimension_1d = 4096, .max_image_dimension_2d = 4096, @@ -190,12 +192,16 @@ pub fn create(allocator: std.mem.Allocator, instance: *const base.Instance) VkEr }; interface.queue_family_props.appendSlice(allocator, queue_family_props[0..]) catch return VkError.OutOfHostMemory; - // TODO: use Pytorch's cpuinfo someday - const info = cpuinfo.get(command_allocator) catch return VkError.InitializationFailed; - defer info.deinit(command_allocator); + if (device_name[0] == 0) { + // TODO: use Pytorch's cpuinfo someday + const info = cpuinfo.get(command_allocator) catch return VkError.InitializationFailed; + defer info.deinit(command_allocator); - var writer = std.Io.Writer.fixed(interface.props.device_name[0 .. vk.MAX_PHYSICAL_DEVICE_NAME_SIZE - 1]); - writer.print("{s} [" ++ root.DRIVER_NAME ++ " StrollDriver]", .{info.name}) catch return VkError.InitializationFailed; + var writer = std.Io.Writer.fixed(device_name[0 .. vk.MAX_PHYSICAL_DEVICE_NAME_SIZE - 1]); + writer.print("{s} [" ++ root.DRIVER_NAME ++ " StrollDriver]", .{info.name}) catch return VkError.InitializationFailed; + } + + @memcpy(&interface.props.device_name, &device_name); self.* = .{ .interface = interface, @@ -203,6 +209,11 @@ pub fn create(allocator: std.mem.Allocator, instance: *const base.Instance) VkEr return self; } +pub fn destroy(interface: *Interface, allocator: std.mem.Allocator) VkError!void { + const self: *Self = @alignCast(@fieldParentPtr("interface", interface)); + allocator.destroy(self); +} + 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; @@ -255,9 +266,3 @@ pub fn getSparseImageFormatProperties( _ = flags; return undefined; } - -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); -} diff --git a/src/vulkan/Device.zig b/src/vulkan/Device.zig index 8edb661..1302fae 100644 --- a/src/vulkan/Device.zig +++ b/src/vulkan/Device.zig @@ -1,6 +1,8 @@ const std = @import("std"); const vk = @import("vulkan"); +const logger = @import("lib.zig").logger; + const Dispatchable = @import("Dispatchable.zig").Dispatchable; const NonDispatchable = @import("NonDispatchable.zig").NonDispatchable; const VulkanAllocator = @import("VulkanAllocator.zig"); @@ -96,6 +98,10 @@ pub fn createQueues(self: *Self, allocator: std.mem.Allocator, info: *const vk.D } const queue = try self.vtable.createQueue(allocator, self, queue_info.queue_family_index, @intCast(family_ptr.items.len), queue_info.flags); + + logger.indent(); + defer logger.unindent(); + const dispatchable_queue = try Dispatchable(Queue).wrap(allocator, queue); family_ptr.append(allocator, dispatchable_queue) catch return VkError.OutOfHostMemory; } diff --git a/src/vulkan/Dispatchable.zig b/src/vulkan/Dispatchable.zig index 2eb4197..4e7a38c 100644 --- a/src/vulkan/Dispatchable.zig +++ b/src/vulkan/Dispatchable.zig @@ -21,6 +21,7 @@ pub fn Dispatchable(comptime T: type) type { .object_type = T.ObjectType, .object = object, }; + std.log.debug("Created dispatchable handle at 0x{X}", .{@intFromPtr(self)}); return self; } diff --git a/src/vulkan/Instance.zig b/src/vulkan/Instance.zig index b54eb6c..354ceec 100644 --- a/src/vulkan/Instance.zig +++ b/src/vulkan/Instance.zig @@ -2,6 +2,8 @@ const std = @import("std"); const builtin = @import("builtin"); const vk = @import("vulkan"); +const logger = @import("lib.zig").logger; + const VkError = @import("error_set.zig").VkError; const Dispatchable = @import("Dispatchable.zig").Dispatchable; const PhysicalDevice = @import("PhysicalDevice.zig"); @@ -78,6 +80,9 @@ pub fn releasePhysicalDevices(self: *Self, allocator: std.mem.Allocator) VkError } pub fn requestPhysicalDevices(self: *Self, allocator: std.mem.Allocator) VkError!void { + logger.indent(); + defer logger.unindent(); + try self.vtable.requestPhysicalDevices(self, allocator); if (self.physical_devices.items.len == 0) { std.log.scoped(.vkCreateInstance).err("No VkPhysicalDevice found", .{}); diff --git a/src/vulkan/NonDispatchable.zig b/src/vulkan/NonDispatchable.zig index 6b0e93e..a75f16b 100644 --- a/src/vulkan/NonDispatchable.zig +++ b/src/vulkan/NonDispatchable.zig @@ -16,6 +16,7 @@ pub fn NonDispatchable(comptime T: type) type { .object_type = T.ObjectType, .object = object, }; + std.log.debug("Created non dispatchable handle at 0x{X}", .{@intFromPtr(self)}); return self; } diff --git a/src/vulkan/PhysicalDevice.zig b/src/vulkan/PhysicalDevice.zig index 7bc8263..c58d82e 100644 --- a/src/vulkan/PhysicalDevice.zig +++ b/src/vulkan/PhysicalDevice.zig @@ -83,5 +83,7 @@ pub fn getSparseImageFormatProperties( } pub fn releasePhysicalDevice(self: *Self, allocator: std.mem.Allocator) VkError!void { + self.queue_family_props.deinit(allocator); + self.queue_family_props = .empty; try self.dispatch_table.release(self, allocator); } diff --git a/src/vulkan/VulkanAllocator.zig b/src/vulkan/VulkanAllocator.zig index c2cf4a0..8bcac6f 100644 --- a/src/vulkan/VulkanAllocator.zig +++ b/src/vulkan/VulkanAllocator.zig @@ -3,16 +3,13 @@ const std = @import("std"); const vk = @import("vulkan"); -const builtin = @import("builtin"); -const DRIVER_DEBUG_ALLOCATOR_ENV_NAME = @import("lib.zig").DRIVER_DEBUG_ALLOCATOR_ENV_NAME; const Allocator = std.mem.Allocator; const Alignment = std.mem.Alignment; const Self = @This(); -/// Global debug allocator for leaks detection purpose -pub var debug_allocator: std.heap.DebugAllocator(.{}) = .init; +var fallback_allocator: std.heap.ThreadSafeAllocator = .{ .child_allocator = std.heap.c_allocator }; callbacks: ?vk.AllocationCallbacks, scope: vk.SystemAllocationScope, @@ -94,10 +91,5 @@ fn free(context: *anyopaque, ptr: []u8, alignment: Alignment, ret_addr: usize) v } inline fn getFallbackAllocator() std.mem.Allocator { - if (std.process.hasEnvVarConstant(DRIVER_DEBUG_ALLOCATOR_ENV_NAME) or builtin.mode == std.builtin.OptimizeMode.Debug) { - @branchHint(.unlikely); - return debug_allocator.allocator(); - } else { - return std.heap.page_allocator; - } + return fallback_allocator.allocator(); } diff --git a/src/vulkan/lib_vulkan.zig b/src/vulkan/lib_vulkan.zig index 258a188..48fefa2 100644 --- a/src/vulkan/lib_vulkan.zig +++ b/src/vulkan/lib_vulkan.zig @@ -347,13 +347,6 @@ pub export fn strollDestroyInstance(p_instance: vk.Instance, callbacks: ?*const const dispatchable = Dispatchable(Instance).fromHandle(p_instance) catch |err| return errorLogger(err); dispatchable.object.deinit(allocator) catch |err| return errorLogger(err); dispatchable.destroy(allocator); - - if (std.process.hasEnvVarConstant(lib.DRIVER_DEBUG_ALLOCATOR_ENV_NAME) or builtin.mode == std.builtin.OptimizeMode.Debug) { - // All host memory allocations should've been freed by now - if (!VulkanAllocator.debug_allocator.detectLeaks()) { - std.log.scoped(.vkDestroyInstance).debug("No memory leaks detected", .{}); - } - } } pub export fn strollEnumeratePhysicalDevices(p_instance: vk.Instance, count: *u32, p_devices: ?[*]vk.PhysicalDevice) callconv(vk.vulkan_call_conv) vk.Result {