Removed _pip_add and scale to a pipelines struct

This commit is contained in:
adrien 2026-05-17 20:16:19 +02:00
parent a381c71550
commit 915c5cea65
3 changed files with 10 additions and 19 deletions

View File

@ -20,9 +20,9 @@ config: GpuConfig,
tracked_buffers: std.AutoHashMap(c.WGPUBuffer, void), tracked_buffers: std.AutoHashMap(c.WGPUBuffer, void),
// Lazily created, cached for lifetime of allocator pipelines: struct {
_pip_add: c.WGPUComputePipeline = null, add: c.WGPUComputePipeline,
_pip_scale: c.WGPUComputePipeline = null, },
pub fn init(cpu_allocator: std.mem.Allocator) !GpuAllocator { pub fn init(cpu_allocator: std.mem.Allocator) !GpuAllocator {
const instance = c.wgpuCreateInstance( const instance = c.wgpuCreateInstance(
@ -77,12 +77,15 @@ pub fn init(cpu_allocator: std.mem.Allocator) !GpuAllocator {
.queue = c.wgpuDeviceGetQueue(device), .queue = c.wgpuDeviceGetQueue(device),
.config = config, .config = config,
.tracked_buffers = .init(cpu_allocator), .tracked_buffers = .init(cpu_allocator),
.pipelines = .{
.add = try buildPipeline(device, sh.SHADER_ADD),
},
}; };
} }
pub fn deinit(self: *GpuAllocator) void { pub fn deinit(self: *GpuAllocator) void {
if (self._pip_add) |p| c.wgpuComputePipelineRelease(p); inline for (@typeInfo(@TypeOf(self.pipelines)).@"struct".fields) |field|
if (self._pip_scale) |p| c.wgpuComputePipelineRelease(p); c.wgpuComputePipelineRelease(@field(self.pipelines, field.name));
var it = self.tracked_buffers.keyIterator(); var it = self.tracked_buffers.keyIterator();
while (it.next()) |buf_ptr| { while (it.next()) |buf_ptr| {
@ -132,18 +135,6 @@ pub fn makeBuffer(
}) orelse error.BufferAlloc; }) orelse error.BufferAlloc;
} }
pub fn pipAdd(self: *GpuAllocator) !c.WGPUComputePipeline {
if (self._pip_add == null)
self._pip_add = try buildPipeline(self.device, sh.SHADER_ADD);
return self._pip_add.?;
}
pub fn pipScale(self: *GpuAllocator) !c.WGPUComputePipeline {
if (self._pip_scale == null)
self._pip_scale = try buildPipeline(self.device, sh.SHADER_SCALE);
return self._pip_scale.?;
}
/// Poll until GPU work completes. Use after submit if you need CPU sync. /// Poll until GPU work completes. Use after submit if you need CPU sync.
pub fn poll(self: *GpuAllocator) void { pub fn poll(self: *GpuAllocator) void {
_ = c.wgpuDevicePoll(self.device, 1, null); _ = c.wgpuDevicePoll(self.device, 1, null);

View File

@ -57,8 +57,7 @@ pub fn add(self: Mat, gloc: *GpuAllocator, other: Mat) !Mat {
const result = try Mat.zeros(gloc, self.rows, self.cols); const result = try Mat.zeros(gloc, self.rows, self.cols);
errdefer result.deinit(); errdefer result.deinit();
const pipeline = try gloc.pipAdd(); try dispatch2in1out(gloc, gloc.pipelines.add, self.buf, other.buf, result.buf, self.byteSize());
try dispatch2in1out(gloc, pipeline, self.buf, other.buf, result.buf, self.byteSize());
return result; return result;
} }

View File

@ -1 +1,2 @@
pub const GpuAllocator = @import("GpuAllocator.zig"); pub const GpuAllocator = @import("GpuAllocator.zig");
pub const GpuBuffer = @import("GpuBuffer.zig");