GpuBuffer can be for any float and init now

This commit is contained in:
adrien 2026-05-18 09:31:58 +02:00
parent 545e67d72f
commit 0fc5f5dbb8
2 changed files with 12 additions and 4 deletions

View File

@ -8,7 +8,12 @@ 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) !@This() { pub fn init(gloc: *GpuAllocator, T: type, len: usize, usage: c.WGPUBufferUsage) !@This() {
switch (@typeInfo(T)) {
.int, .float => {},
else => @compileError("GpuBuffer can only use int and float type"),
}
const bytes = @sizeOf(T) * len;
const raw_handle = try gloc.registerBuffer(bytes, usage); const raw_handle = try gloc.registerBuffer(bytes, usage);
return .{ return .{
.raw = raw_handle, .raw = raw_handle,

View File

@ -15,7 +15,8 @@ pub fn initZero(gloc: *GpuAllocator, len: usize) !Vec {
return .{ return .{
.buf = try GpuBuffer.init( .buf = try GpuBuffer.init(
gloc, gloc,
len * @sizeOf(f32), f32,
len,
c.WGPUBufferUsage_Storage | c.WGPUBufferUsage_CopyDst | c.WGPUBufferUsage_CopySrc, c.WGPUBufferUsage_Storage | c.WGPUBufferUsage_CopyDst | c.WGPUBufferUsage_CopySrc,
), ),
.len = len, .len = len,
@ -65,7 +66,8 @@ pub fn read(self: Vec, gloc: *GpuAllocator, alloc: std.mem.Allocator) ![]f32 {
const staging = try GpuBuffer.init( const staging = try GpuBuffer.init(
gloc, gloc,
bytes, f32,
self.len,
c.WGPUBufferUsage_MapRead | c.WGPUBufferUsage_CopyDst, c.WGPUBufferUsage_MapRead | c.WGPUBufferUsage_CopyDst,
); );
defer staging.deinit(); defer staging.deinit();
@ -125,7 +127,8 @@ fn dispatch2in1out(
// Create uniform buffer for this specific chunk's size // Create uniform buffer for this specific chunk's size
const info_buf = try GpuBuffer.init( const info_buf = try GpuBuffer.init(
gloc, gloc,
@sizeOf(u32), u32,
1,
c.WGPUBufferUsage_Uniform | c.WGPUBufferUsage_CopyDst, c.WGPUBufferUsage_Uniform | c.WGPUBufferUsage_CopyDst,
); );
defer info_buf.deinit(); defer info_buf.deinit();