From 0fcb9ee351c81160d0f48eb78c97eeedb261a954 Mon Sep 17 00:00:00 2001 From: adrien Date: Mon, 18 May 2026 00:10:09 +0200 Subject: [PATCH] Synthax improv + GpuDeviceConfig --- src/GpuAllocator.zig | 7 +++++-- src/GpuBuffer.zig | 2 +- src/GpuDevice.zig | 19 ++++++++++--------- src/GpuPipeline.zig | 7 ++----- src/Vec.zig | 2 +- src/bench.zig | 23 +++++++++++------------ src/c.zig | 1 - src/utils.zig | 5 +++++ 8 files changed, 35 insertions(+), 31 deletions(-) delete mode 100644 src/c.zig create mode 100644 src/utils.zig diff --git a/src/GpuAllocator.zig b/src/GpuAllocator.zig index f877f10..017288f 100644 --- a/src/GpuAllocator.zig +++ b/src/GpuAllocator.zig @@ -1,7 +1,7 @@ const std = @import("std"); const GpuDevice = @import("GpuDevice.zig"); const GpuBuffer = @import("GpuBuffer.zig"); -const c = @import("c.zig").c; +const c = @import("utils.zig").c; device: GpuDevice, tracked_buffers: std.AutoHashMap(c.WGPUBuffer, void), @@ -39,6 +39,10 @@ pub fn registerBuffer( .usage = usage, .size = bytes, }) orelse return error.BufferAlloc; + errdefer { + c.wgpuBufferDestroy(buf); + c.wgpuBufferRelease(buf); + } try self.tracked_buffers.put(buf, {}); self.allocated_vram_bytes += bytes; @@ -50,6 +54,5 @@ pub fn unregisterAndDestroyBuffer(self: *@This(), buf: GpuBuffer) void { c.wgpuBufferDestroy(buf.raw); c.wgpuBufferRelease(buf.raw); self.allocated_vram_bytes -= buf.size; - self.device.poll(); } } diff --git a/src/GpuBuffer.zig b/src/GpuBuffer.zig index 156520c..eaeab2a 100644 --- a/src/GpuBuffer.zig +++ b/src/GpuBuffer.zig @@ -1,5 +1,5 @@ const std = @import("std"); -const c = @import("c.zig").c; +const c = @import("utils.zig").c; const GpuAllocator = @import("GpuAllocator.zig"); raw: c.WGPUBuffer, diff --git a/src/GpuDevice.zig b/src/GpuDevice.zig index 7622523..1820b16 100644 --- a/src/GpuDevice.zig +++ b/src/GpuDevice.zig @@ -1,22 +1,26 @@ const std = @import("std"); -const c = @import("c.zig").c; +const c = @import("utils.zig").c; +const sv = @import("utils.zig").sv; const Ctx = struct { adapter: c.WGPUAdapter = null, device: c.WGPUDevice = null, }; +const GpuDeviceConfig = struct { + /// VRAM limit. Default 2 GB + vram_bytes_limit: u64 = 2 * 1024 * 1024 * 1024, +}; + instance: c.WGPUInstance, adapter: c.WGPUAdapter, device: c.WGPUDevice, queue: c.WGPUQueue, limits: c.WGPULimits, -config: struct { - vram_bytes_limit: u64 = 10 * 1024 * 1024 * 1024, // 10 GB -} = .{}, +config: GpuDeviceConfig, -pub fn init() !@This() { +pub fn init(config: GpuDeviceConfig) !@This() { const instance = c.wgpuCreateInstance( &std.mem.zeroes(c.WGPUInstanceDescriptor), ) orelse return error.NoInstance; @@ -61,6 +65,7 @@ pub fn init() !@This() { .device = device, .queue = c.wgpuDeviceGetQueue(device), .limits = supported_limits, + .config = config, }; } @@ -104,7 +109,3 @@ fn onDevice( const ctx: *Ctx = @ptrCast(@alignCast(userdata1.?)); ctx.device = device; } - -fn sv(s: []const u8) c.WGPUStringView { - return .{ .data = s.ptr, .length = s.len }; -} diff --git a/src/GpuPipeline.zig b/src/GpuPipeline.zig index 68b3540..83d986d 100644 --- a/src/GpuPipeline.zig +++ b/src/GpuPipeline.zig @@ -1,6 +1,7 @@ const std = @import("std"); const GpuDevice = @import("GpuDevice.zig"); -const c = @import("c.zig").c; +const c = @import("utils.zig").c; +const sv = @import("utils.zig").sv; raw: c.WGPUComputePipeline, @@ -22,7 +23,3 @@ pub fn init(device: GpuDevice, wgsl: []const u8) !@This() { pub fn deinit(self: @This()) void { c.wgpuComputePipelineRelease(self.raw); } - -fn sv(s: []const u8) c.WGPUStringView { - return .{ .data = s.ptr, .length = s.len }; -} diff --git a/src/Vec.zig b/src/Vec.zig index 8c4370b..1283cc5 100644 --- a/src/Vec.zig +++ b/src/Vec.zig @@ -1,6 +1,6 @@ /// Dummy const std = @import("std"); -const c = @import("c.zig").c; +const c = @import("utils.zig").c; const GpuAllocator = @import("GpuAllocator.zig"); const GpuBuffer = @import("GpuBuffer.zig"); const GpuDevice = @import("GpuDevice.zig"); diff --git a/src/bench.zig b/src/bench.zig index 3c5ee01..b7b424f 100644 --- a/src/bench.zig +++ b/src/bench.zig @@ -4,10 +4,10 @@ const GpuAllocator = @import("GpuAllocator.zig"); const GpuPipeline = @import("GpuPipeline.zig"); const Vec = @import("Vec.zig"); -const c = @import("c.zig").c; +const c = @import("utils.zig").c; pub fn main(init: std.process.Init) !void { - const device = try GpuDevice.init(); + const device = try GpuDevice.init(.{ .vram_bytes_limit = 4 * 1024 * 1024 * 1024 }); defer device.deinit(); var gloc = try GpuAllocator.init(init.gpa, device); @@ -41,11 +41,11 @@ pub fn main(init: std.process.Init) !void { 4 * 4 * 4 * 1024, 4 * 4 * 4 * 4 * 1024, 1024 * 1024, - 4 * 1024 * 1024, - 4 * 4 * 1024 * 1024, - 4 * 4 * 4 * 1024 * 1024, - 4 * 4 * 4 * 4 * 1024 * 1024, - 4 * 4 * 4 * 4 * 4 * 1024 * 1024, + // 4 * 1024 * 1024, + // 4 * 4 * 1024 * 1024, + // 4 * 4 * 4 * 1024 * 1024, + // 4 * 4 * 4 * 4 * 1024 * 1024, + // 4 * 4 * 4 * 4 * 4 * 1024 * 1024, }; const iterations = 10; @@ -56,9 +56,9 @@ pub fn main(init: std.process.Init) !void { for (sizes) |size| { // --- Phase 1: Host Init/Alloc (Outside the iteration loop for pure host prep) --- - var data_a = try allocator.alloc(f32, size); + const data_a = try allocator.alloc(f32, size); defer allocator.free(data_a); - var data_b = try allocator.alloc(f32, size); + const data_b = try allocator.alloc(f32, size); defer allocator.free(data_b); for (0..size) |i| { @@ -72,7 +72,7 @@ pub fn main(init: std.process.Init) !void { var min_compute_ns: u64 = std.math.maxInt(u64); // Track peak VRAM usage observed during the iterations - var peak_vram_bytes: usize = 0; + var peak_vram_bytes: u64 = 0; for (0..iterations) |_| { // --- 1. GPU ALLOCATION PHASE --- @@ -95,9 +95,8 @@ pub fn main(init: std.process.Init) !void { // All 3 buffers (a, b, sum) are currently resident in VRAM here. // Querying now catches the true peak allocation step. - if (gloc.allocated_vram_bytes > peak_vram_bytes) { + if (gloc.allocated_vram_bytes > peak_vram_bytes) peak_vram_bytes = gloc.allocated_vram_bytes; - } _ = c.wgpuDevicePoll(device.device, 1, null); diff --git a/src/c.zig b/src/c.zig deleted file mode 100644 index 2df094e..0000000 --- a/src/c.zig +++ /dev/null @@ -1 +0,0 @@ -pub const c = @cImport(@cInclude("wgpu.h")); diff --git a/src/utils.zig b/src/utils.zig new file mode 100644 index 0000000..f4d62b4 --- /dev/null +++ b/src/utils.zig @@ -0,0 +1,5 @@ +pub const c = @cImport(@cInclude("wgpu.h")); + +pub fn sv(s: []const u8) c.WGPUStringView { + return .{ .data = s.ptr, .length = s.len }; +}