making opt-in device debug allocator

This commit is contained in:
2026-03-17 15:14:15 +01:00
parent d2695f60a3
commit c763dd56d4
4 changed files with 23 additions and 6 deletions

View File

@@ -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 {

View File

@@ -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", .{});

View File

@@ -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;

View File

@@ -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);
}