From 7e1bc387ca33b7395e34da75c59e076c0d382d38 Mon Sep 17 00:00:00 2001 From: AdrienBouvais Date: Tue, 19 May 2026 09:15:32 +0200 Subject: [PATCH] Synthax --- src/GpuAllocator.zig | 8 ++------ src/GpuArena.zig | 13 +++++-------- src/GpuBuffer.zig | 4 ++-- 3 files changed, 9 insertions(+), 16 deletions(-) diff --git a/src/GpuAllocator.zig b/src/GpuAllocator.zig index e1678d1..aa585b8 100644 --- a/src/GpuAllocator.zig +++ b/src/GpuAllocator.zig @@ -1,11 +1,7 @@ -// GpuAllocator.zig const std = @import("std"); const GpuDevice = @import("GpuDevice.zig"); const c = @import("utils.zig").c; -const GpuAllocator = @This(); - -/// The function definitions our underlying implementations must satisfy pub const VTable = struct { 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, @@ -15,10 +11,10 @@ device: GpuDevice, ptr: *anyopaque, 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); } -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); } diff --git a/src/GpuArena.zig b/src/GpuArena.zig index 2043206..890d71c 100644 --- a/src/GpuArena.zig +++ b/src/GpuArena.zig @@ -1,23 +1,20 @@ -// GpuArena.zig const std = @import("std"); const GpuDevice = @import("GpuDevice.zig"); const GpuAllocator = @import("GpuAllocator.zig"); const c = @import("utils.zig").c; -const GpuArena = @This(); - device: GpuDevice, tracked_buffers: std.AutoHashMap(c.WGPUBuffer, void), 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 .{ .device = device, .tracked_buffers = .init(cpu_allocator), }; } -pub fn deinit(self: *GpuArena) void { +pub fn deinit(self: *@This()) void { var it = self.tracked_buffers.keyIterator(); while (it.next()) |buf_ptr| { c.wgpuBufferDestroy(buf_ptr.*); @@ -27,7 +24,7 @@ pub fn deinit(self: *GpuArena) void { } /// Returns the type-erased immutable interface wrapper -pub fn gpuAllocator(self: *GpuArena) GpuAllocator { +pub fn gpuAllocator(self: *@This()) GpuAllocator { return .{ .device = self.device, .ptr = self, @@ -39,7 +36,7 @@ pub fn gpuAllocator(self: *GpuArena) GpuAllocator { } 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) 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 { - const self: *GpuArena = @ptrCast(@alignCast(ctx)); + const self: *@This() = @ptrCast(@alignCast(ctx)); if (self.tracked_buffers.remove(buf_raw)) { c.wgpuBufferDestroy(buf_raw); diff --git a/src/GpuBuffer.zig b/src/GpuBuffer.zig index 72b4f46..155e233 100644 --- a/src/GpuBuffer.zig +++ b/src/GpuBuffer.zig @@ -3,7 +3,7 @@ const c = @import("utils.zig").c; const GpuAllocator = @import("GpuAllocator.zig"); raw: c.WGPUBuffer, -size: u64, // Now tracks the 4-byte aligned size directly +size: u64, usage: c.WGPUBufferUsage, 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); return .{ .raw = raw_handle, - .size = aligned_size, // Expose the aligned size to the rest of the application + .size = aligned_size, .usage = use, .gloc = gloc, };