Removed const x = @This() to just use @This()

This commit is contained in:
adrien 2026-05-17 23:54:58 +02:00
parent 1c8e12b1e6
commit 6a2cbe2734
3 changed files with 12 additions and 18 deletions

View File

@ -3,20 +3,18 @@ const GpuDevice = @import("GpuDevice.zig");
const GpuBuffer = @import("GpuBuffer.zig"); const GpuBuffer = @import("GpuBuffer.zig");
const c = @import("c.zig").c; const c = @import("c.zig").c;
const GpuAllocator = @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) !GpuAllocator { 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: *GpuAllocator) 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| {
const buf = buf_ptr.*; const buf = buf_ptr.*;
@ -27,7 +25,7 @@ pub fn deinit(self: *GpuAllocator) void {
} }
pub fn registerBuffer( pub fn registerBuffer(
self: *GpuAllocator, self: *@This(),
bytes: u64, bytes: u64,
usage: c.WGPUBufferUsage, usage: c.WGPUBufferUsage,
) !c.WGPUBuffer { ) !c.WGPUBuffer {
@ -47,7 +45,7 @@ pub fn registerBuffer(
return buf; 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)) { if (self.tracked_buffers.remove(buf.raw)) {
c.wgpuBufferDestroy(buf.raw); c.wgpuBufferDestroy(buf.raw);
c.wgpuBufferRelease(buf.raw); c.wgpuBufferRelease(buf.raw);

View File

@ -2,15 +2,13 @@ const std = @import("std");
const c = @import("c.zig").c; const c = @import("c.zig").c;
const GpuAllocator = @import("GpuAllocator.zig"); const GpuAllocator = @import("GpuAllocator.zig");
const GpuBuffer = @This();
raw: c.WGPUBuffer, raw: c.WGPUBuffer,
size: u64, size: u64,
usage: c.WGPUBufferUsage, usage: c.WGPUBufferUsage,
gloc: *GpuAllocator, gloc: *GpuAllocator,
/// Allocates the underlying WebGPU handle and registers it to the parent 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); const raw_handle = try gloc.registerBuffer(bytes, usage);
return .{ return .{
.raw = raw_handle, .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 /// 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); self.gloc.unregisterAndDestroyBuffer(self);
} }
/// Native mapAsync wrapper /// Native mapAsync wrapper
pub fn mapAsync( pub fn mapAsync(
self: GpuBuffer, self: @This(),
mode: c.WGPUMapMode, mode: c.WGPUMapMode,
offset: u64, offset: u64,
size: u64, size: u64,
@ -37,11 +35,11 @@ pub fn mapAsync(
} }
/// Native getConstMappedRange wrapper /// 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); return c.wgpuBufferGetConstMappedRange(self.raw, offset, size);
} }
/// Native unmap wrapper /// Native unmap wrapper
pub fn unmap(self: GpuBuffer) void { pub fn unmap(self: @This()) void {
c.wgpuBufferUnmap(self.raw); c.wgpuBufferUnmap(self.raw);
} }

View File

@ -6,8 +6,6 @@ const Ctx = struct {
device: c.WGPUDevice = null, device: c.WGPUDevice = null,
}; };
const GpuAllocator = @This();
instance: c.WGPUInstance, instance: c.WGPUInstance,
adapter: c.WGPUAdapter, adapter: c.WGPUAdapter,
device: c.WGPUDevice, device: c.WGPUDevice,
@ -18,7 +16,7 @@ config: struct {
vram_bytes_limit: u64 = 10 * 1024 * 1024 * 1024, // 10 GB vram_bytes_limit: u64 = 10 * 1024 * 1024 * 1024, // 10 GB
} = .{}, } = .{},
pub fn init() !GpuAllocator { pub fn init() !@This() {
const instance = c.wgpuCreateInstance( const instance = c.wgpuCreateInstance(
&std.mem.zeroes(c.WGPUInstanceDescriptor), &std.mem.zeroes(c.WGPUInstanceDescriptor),
) orelse return error.NoInstance; ) 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.wgpuQueueRelease(self.queue);
c.wgpuDeviceRelease(self.device); c.wgpuDeviceRelease(self.device);
c.wgpuAdapterRelease(self.adapter); c.wgpuAdapterRelease(self.adapter);
c.wgpuInstanceRelease(self.instance); c.wgpuInstanceRelease(self.instance);
} }
pub fn poll(self: *GpuAllocator) void { pub fn poll(self: *@This()) void {
_ = c.wgpuDevicePoll(self.device, 1, null); _ = c.wgpuDevicePoll(self.device, 1, null);
} }