This commit is contained in:
AdrienBouvais 2026-05-19 09:15:32 +02:00
parent 09e62cf667
commit 7e1bc387ca
3 changed files with 9 additions and 16 deletions

View File

@ -1,11 +1,7 @@
// GpuAllocator.zig
const std = @import("std"); const std = @import("std");
const GpuDevice = @import("GpuDevice.zig"); const GpuDevice = @import("GpuDevice.zig");
const c = @import("utils.zig").c; const c = @import("utils.zig").c;
const GpuAllocator = @This();
/// The function definitions our underlying implementations must satisfy
pub const VTable = struct { pub const VTable = struct {
alloc: *const fn (ctx: *anyopaque, bytes: u64, usage: c.WGPUBufferUsage) anyerror!c.WGPUBuffer, alloc: *const fn (ctx: *anyopaque, bytes: u64, usage: c.WGPUBufferUsage) anyerror!c.WGPUBuffer,
free: *const fn (ctx: *anyopaque, buf_raw: c.WGPUBuffer, size: u64) void, free: *const fn (ctx: *anyopaque, buf_raw: c.WGPUBuffer, size: u64) void,
@ -15,10 +11,10 @@ device: GpuDevice,
ptr: *anyopaque, ptr: *anyopaque,
vtable: *const VTable, vtable: *const VTable,
pub fn allocBuffer(self: GpuAllocator, bytes: u64, usage: c.WGPUBufferUsage) !c.WGPUBuffer { pub fn allocBuffer(self: @This(), bytes: u64, usage: c.WGPUBufferUsage) !c.WGPUBuffer {
return self.vtable.alloc(self.ptr, bytes, usage); return self.vtable.alloc(self.ptr, bytes, usage);
} }
pub fn freeBuffer(self: GpuAllocator, buf_raw: c.WGPUBuffer, size: u64) void { pub fn freeBuffer(self: @This(), buf_raw: c.WGPUBuffer, size: u64) void {
self.vtable.free(self.ptr, buf_raw, size); self.vtable.free(self.ptr, buf_raw, size);
} }

View File

@ -1,23 +1,20 @@
// GpuArena.zig
const std = @import("std"); const std = @import("std");
const GpuDevice = @import("GpuDevice.zig"); const GpuDevice = @import("GpuDevice.zig");
const GpuAllocator = @import("GpuAllocator.zig"); const GpuAllocator = @import("GpuAllocator.zig");
const c = @import("utils.zig").c; const c = @import("utils.zig").c;
const GpuArena = @This();
device: GpuDevice, device: GpuDevice,
tracked_buffers: std.AutoHashMap(c.WGPUBuffer, void), tracked_buffers: std.AutoHashMap(c.WGPUBuffer, void),
allocated_vram_bytes: u64 = 0, allocated_vram_bytes: u64 = 0,
pub fn init(cpu_allocator: std.mem.Allocator, device: GpuDevice) GpuArena { pub fn init(cpu_allocator: std.mem.Allocator, device: GpuDevice) @This() {
return .{ return .{
.device = device, .device = device,
.tracked_buffers = .init(cpu_allocator), .tracked_buffers = .init(cpu_allocator),
}; };
} }
pub fn deinit(self: *GpuArena) void { pub fn deinit(self: *@This()) void {
var it = self.tracked_buffers.keyIterator(); var it = self.tracked_buffers.keyIterator();
while (it.next()) |buf_ptr| { while (it.next()) |buf_ptr| {
c.wgpuBufferDestroy(buf_ptr.*); c.wgpuBufferDestroy(buf_ptr.*);
@ -27,7 +24,7 @@ pub fn deinit(self: *GpuArena) void {
} }
/// Returns the type-erased immutable interface wrapper /// Returns the type-erased immutable interface wrapper
pub fn gpuAllocator(self: *GpuArena) GpuAllocator { pub fn gpuAllocator(self: *@This()) GpuAllocator {
return .{ return .{
.device = self.device, .device = self.device,
.ptr = self, .ptr = self,
@ -39,7 +36,7 @@ pub fn gpuAllocator(self: *GpuArena) GpuAllocator {
} }
fn alloc(ctx: *anyopaque, bytes: u64, usage: c.WGPUBufferUsage) anyerror!c.WGPUBuffer { fn alloc(ctx: *anyopaque, bytes: u64, usage: c.WGPUBufferUsage) anyerror!c.WGPUBuffer {
const self: *GpuArena = @ptrCast(@alignCast(ctx)); const self: *@This() = @ptrCast(@alignCast(ctx));
if (bytes > self.device.limits.maxBufferSize) if (bytes > self.device.limits.maxBufferSize)
return error.SingleBufferExceedsLimit; return error.SingleBufferExceedsLimit;
@ -62,7 +59,7 @@ fn alloc(ctx: *anyopaque, bytes: u64, usage: c.WGPUBufferUsage) anyerror!c.WGPUB
} }
fn free(ctx: *anyopaque, buf_raw: c.WGPUBuffer, size: u64) void { fn free(ctx: *anyopaque, buf_raw: c.WGPUBuffer, size: u64) void {
const self: *GpuArena = @ptrCast(@alignCast(ctx)); const self: *@This() = @ptrCast(@alignCast(ctx));
if (self.tracked_buffers.remove(buf_raw)) { if (self.tracked_buffers.remove(buf_raw)) {
c.wgpuBufferDestroy(buf_raw); c.wgpuBufferDestroy(buf_raw);

View File

@ -3,7 +3,7 @@ const c = @import("utils.zig").c;
const GpuAllocator = @import("GpuAllocator.zig"); const GpuAllocator = @import("GpuAllocator.zig");
raw: c.WGPUBuffer, raw: c.WGPUBuffer,
size: u64, // Now tracks the 4-byte aligned size directly size: u64,
usage: c.WGPUBufferUsage, usage: c.WGPUBufferUsage,
gloc: GpuAllocator, gloc: GpuAllocator,
@ -33,7 +33,7 @@ pub fn init(gloc: GpuAllocator, size: u64, usage: std.EnumSet(BufferUsage)) !@Th
const raw_handle = try gloc.allocBuffer(aligned_size, use); const raw_handle = try gloc.allocBuffer(aligned_size, use);
return .{ return .{
.raw = raw_handle, .raw = raw_handle,
.size = aligned_size, // Expose the aligned size to the rest of the application .size = aligned_size,
.usage = use, .usage = use,
.gloc = gloc, .gloc = gloc,
}; };