From c763dd56d46f89886c823dbbd80a32b27bd2cc7e Mon Sep 17 00:00:00 2001 From: Kbz-8 Date: Tue, 17 Mar 2026 15:14:15 +0100 Subject: [PATCH] making opt-in device debug allocator --- build.zig | 7 ++++++- src/soft/SoftDevice.zig | 9 ++++----- src/vulkan/logger/Manager.zig | 7 +++++++ src/vulkan/logger/logger.zig | 6 ++++++ 4 files changed, 23 insertions(+), 6 deletions(-) diff --git a/build.zig b/build.zig index 069c140..73f56fd 100644 --- a/build.zig +++ b/build.zig @@ -32,7 +32,6 @@ pub fn build(b: *std.Build) !void { const zdt = b.dependency("zdt", .{}).module("zdt"); const zigrc = b.dependency("zigrc", .{}).module("zigrc"); - //const spv_tools = b.dependency("SPIRV_Tools", .{}).module("zigrc"); const vulkan_headers = b.dependency("vulkan_headers", .{}); const vulkan_utility_libraries = b.dependency("vulkan_utility_libraries", .{}); @@ -143,6 +142,12 @@ fn customSoft(b: *std.Build, lib: *std.Build.Step.Compile) !void { .@"use-llvm" = true, }).module("spv"); lib.root_module.addImport("spv", spv); + + const debug_allocator_option = b.option(bool, "debug-allocator", "debug device allocator") orelse false; + + const options = b.addOptions(); + options.addOption(bool, "debug_allocator", debug_allocator_option); + lib.root_module.addOptions("config", options); } fn addCTest(b: *std.Build, target: std.Build.ResolvedTarget, optimize: std.builtin.OptimizeMode, vulkan_headers: *std.Build.Dependency, impl: *const ImplementationDesc, impl_lib: *std.Build.Step.Compile) !*std.Build.Step.Compile { diff --git a/src/soft/SoftDevice.zig b/src/soft/SoftDevice.zig index 752ca7a..f06a924 100644 --- a/src/soft/SoftDevice.zig +++ b/src/soft/SoftDevice.zig @@ -2,8 +2,7 @@ const std = @import("std"); const vk = @import("vulkan"); const base = @import("base"); const builtin = @import("builtin"); - -const Debug = std.builtin.OptimizeMode.Debug; +const config = @import("config"); const SoftQueue = @import("SoftQueue.zig"); const Blitter = @import("device/Blitter.zig"); @@ -37,7 +36,7 @@ pub const Interface = base.Device; const SpawnError = std.Thread.SpawnError; interface: Interface, -device_allocator: if (builtin.mode == Debug) std.heap.DebugAllocator(.{}) else std.heap.ThreadSafeAllocator, +device_allocator: if (config.debug_allocator) std.heap.DebugAllocator(.{}) else std.heap.ThreadSafeAllocator, workers: std.Thread.Pool, blitter: Blitter, @@ -78,7 +77,7 @@ pub fn create(physical_device: *base.PhysicalDevice, allocator: std.mem.Allocato self.* = .{ .interface = interface, - .device_allocator = if (builtin.mode == Debug) .init else .{ .child_allocator = std.heap.c_allocator }, // TODO: better device allocator + .device_allocator = if (config.debug_allocator) .init else .{ .child_allocator = std.heap.c_allocator }, // TODO: better device allocator .workers = undefined, .blitter = .init, }; @@ -96,7 +95,7 @@ pub fn destroy(interface: *Interface, allocator: std.mem.Allocator) VkError!void const self: *Self = @alignCast(@fieldParentPtr("interface", interface)); self.workers.deinit(); - if (builtin.mode == Debug) { + if (config.debug_allocator) { // All device memory allocations should've been freed by now if (!self.device_allocator.detectLeaks()) { std.log.scoped(.vkDestroyDevice).debug("No device memory leaks detected", .{}); diff --git a/src/vulkan/logger/Manager.zig b/src/vulkan/logger/Manager.zig index 9b0938d..2fd3041 100644 --- a/src/vulkan/logger/Manager.zig +++ b/src/vulkan/logger/Manager.zig @@ -1,5 +1,6 @@ const std = @import("std"); const DebugStack = @import("DebugStack.zig"); +const lib = @import("../lib.zig"); const Self = @This(); @@ -14,6 +15,9 @@ pub const init: Self = .{ }; pub fn indent(self: *Self) void { + if (lib.getLogVerboseLevel() == .None) { + return; + } const new_indent_level, const has_overflown = @addWithOverflow(self.indent_level, 1); if (has_overflown == 0) { self.indent_level = new_indent_level; @@ -21,6 +25,9 @@ pub fn indent(self: *Self) void { } pub fn unindent(self: *Self) void { + if (lib.getLogVerboseLevel() == .None) { + return; + } const new_indent_level, const has_overflown = @subWithOverflow(self.indent_level, 1); if (has_overflown == 0) { self.indent_level = new_indent_level; diff --git a/src/vulkan/logger/logger.zig b/src/vulkan/logger/logger.zig index 90aecd9..d0e33df 100644 --- a/src/vulkan/logger/logger.zig +++ b/src/vulkan/logger/logger.zig @@ -24,12 +24,18 @@ pub inline fn getManager() *ThreadSafeManager { } pub inline fn fixme(comptime format: []const u8, args: anytype) void { + if (lib.getLogVerboseLevel() == .None) { + return; + } getManager().get().disableIndent(); defer getManager().get().enableIndent(); nestedFixme(format, args); } pub inline fn nestedFixme(comptime format: []const u8, args: anytype) void { + if (lib.getLogVerboseLevel() == .None) { + return; + } std.log.scoped(.FIXME).warn("FIXME: " ++ format, args); }