diff --git a/src/GpuAllocator.zig b/src/GpuAllocator.zig index 5f9274d..f877f10 100644 --- a/src/GpuAllocator.zig +++ b/src/GpuAllocator.zig @@ -3,20 +3,18 @@ const GpuDevice = @import("GpuDevice.zig"); const GpuBuffer = @import("GpuBuffer.zig"); const c = @import("c.zig").c; -const GpuAllocator = @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) !GpuAllocator { +pub fn init(cpu_allocator: std.mem.Allocator, device: GpuDevice) !@This() { return .{ .device = device, .tracked_buffers = .init(cpu_allocator), }; } -pub fn deinit(self: *GpuAllocator) void { +pub fn deinit(self: *@This()) void { var it = self.tracked_buffers.keyIterator(); while (it.next()) |buf_ptr| { const buf = buf_ptr.*; @@ -27,7 +25,7 @@ pub fn deinit(self: *GpuAllocator) void { } pub fn registerBuffer( - self: *GpuAllocator, + self: *@This(), bytes: u64, usage: c.WGPUBufferUsage, ) !c.WGPUBuffer { @@ -47,7 +45,7 @@ pub fn registerBuffer( return buf; } -pub fn unregisterAndDestroyBuffer(self: *GpuAllocator, buf: GpuBuffer) void { +pub fn unregisterAndDestroyBuffer(self: *@This(), buf: GpuBuffer) void { if (self.tracked_buffers.remove(buf.raw)) { c.wgpuBufferDestroy(buf.raw); c.wgpuBufferRelease(buf.raw); diff --git a/src/GpuBuffer.zig b/src/GpuBuffer.zig index a000c10..156520c 100644 --- a/src/GpuBuffer.zig +++ b/src/GpuBuffer.zig @@ -2,15 +2,13 @@ const std = @import("std"); const c = @import("c.zig").c; const GpuAllocator = @import("GpuAllocator.zig"); -const GpuBuffer = @This(); - raw: c.WGPUBuffer, size: u64, usage: c.WGPUBufferUsage, gloc: *GpuAllocator, /// Allocates the underlying WebGPU handle and registers it to the parent GpuAllocator -pub fn init(gloc: *GpuAllocator, bytes: u64, usage: c.WGPUBufferUsage) !GpuBuffer { +pub fn init(gloc: *GpuAllocator, bytes: u64, usage: c.WGPUBufferUsage) !@This() { const raw_handle = try gloc.registerBuffer(bytes, usage); return .{ .raw = raw_handle, @@ -21,13 +19,13 @@ pub fn init(gloc: *GpuAllocator, bytes: u64, usage: c.WGPUBufferUsage) !GpuBuffe } /// Unregisters from the parent GpuAllocator and cleanly destroys GPU resources -pub fn deinit(self: GpuBuffer) void { +pub fn deinit(self: @This()) void { self.gloc.unregisterAndDestroyBuffer(self); } /// Native mapAsync wrapper pub fn mapAsync( - self: GpuBuffer, + self: @This(), mode: c.WGPUMapMode, offset: u64, size: u64, @@ -37,11 +35,11 @@ pub fn mapAsync( } /// Native getConstMappedRange wrapper -pub fn getConstMappedRange(self: GpuBuffer, offset: u64, size: u64) ?*const anyopaque { +pub fn getConstMappedRange(self: @This(), offset: u64, size: u64) ?*const anyopaque { return c.wgpuBufferGetConstMappedRange(self.raw, offset, size); } /// Native unmap wrapper -pub fn unmap(self: GpuBuffer) void { +pub fn unmap(self: @This()) void { c.wgpuBufferUnmap(self.raw); } diff --git a/src/GpuDevice.zig b/src/GpuDevice.zig index b8aa03b..7622523 100644 --- a/src/GpuDevice.zig +++ b/src/GpuDevice.zig @@ -6,8 +6,6 @@ const Ctx = struct { device: c.WGPUDevice = null, }; -const GpuAllocator = @This(); - instance: c.WGPUInstance, adapter: c.WGPUAdapter, device: c.WGPUDevice, @@ -18,7 +16,7 @@ config: struct { vram_bytes_limit: u64 = 10 * 1024 * 1024 * 1024, // 10 GB } = .{}, -pub fn init() !GpuAllocator { +pub fn init() !@This() { const instance = c.wgpuCreateInstance( &std.mem.zeroes(c.WGPUInstanceDescriptor), ) orelse return error.NoInstance; @@ -66,14 +64,14 @@ pub fn init() !GpuAllocator { }; } -pub fn deinit(self: GpuAllocator) void { +pub fn deinit(self: @This()) void { c.wgpuQueueRelease(self.queue); c.wgpuDeviceRelease(self.device); c.wgpuAdapterRelease(self.adapter); c.wgpuInstanceRelease(self.instance); } -pub fn poll(self: *GpuAllocator) void { +pub fn poll(self: *@This()) void { _ = c.wgpuDevicePoll(self.device, 1, null); }