Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
5f8da0940d | ||
|
|
4901dc654d |
+10
-3
@@ -32,7 +32,7 @@ pub fn main(init: std.process.Init) !void {
|
|||||||
);
|
);
|
||||||
|
|
||||||
// 4. Setup CPU data
|
// 4. Setup CPU data
|
||||||
const len: usize = 16;
|
const len: usize = 1024;
|
||||||
const data_a = try allocator.alloc(f16, len);
|
const data_a = try allocator.alloc(f16, len);
|
||||||
defer allocator.free(data_a);
|
defer allocator.free(data_a);
|
||||||
const data_b = try allocator.alloc(f16, len);
|
const data_b = try allocator.alloc(f16, len);
|
||||||
@@ -61,8 +61,15 @@ pub fn main(init: std.process.Init) !void {
|
|||||||
try add_cp.run(gloc, .{ buf_a, buf_b, buf_out });
|
try add_cp.run(gloc, .{ buf_a, buf_b, buf_out });
|
||||||
|
|
||||||
// 8. Map and copy the resulting buffer back to the CPU
|
// 8. Map and copy the resulting buffer back to the CPU
|
||||||
const out = try buf_out.read(allocator, f16);
|
const staging = try GpuBuffer.init(gloc, .{
|
||||||
|
.size = byte_size,
|
||||||
|
.usage = .initMany(&.{ .MapRead, .CopyDst }),
|
||||||
|
});
|
||||||
|
defer staging.deinit();
|
||||||
|
|
||||||
|
try buf_out.copy(staging);
|
||||||
|
const out = try staging.read(allocator, f16);
|
||||||
defer allocator.free(out);
|
defer allocator.free(out);
|
||||||
|
|
||||||
std.debug.print("Result: {any}\n", .{out});
|
std.debug.print("Result: {any}\n", .{out[0..@min(6, len)]});
|
||||||
}
|
}
|
||||||
|
|||||||
+39
-31
@@ -4,11 +4,10 @@ const GpuAllocator = @import("GpuAllocator.zig");
|
|||||||
const svOpt = @import("utils.zig").svOpt;
|
const svOpt = @import("utils.zig").svOpt;
|
||||||
|
|
||||||
raw: c.WGPUBuffer,
|
raw: c.WGPUBuffer,
|
||||||
size: u64,
|
|
||||||
usage: c.WGPUBufferUsage,
|
|
||||||
gloc: GpuAllocator,
|
gloc: GpuAllocator,
|
||||||
|
def: GpuBufferDef,
|
||||||
|
|
||||||
const BufferUsage = enum(u64) {
|
pub const GpuBufferUsage = enum(u64) {
|
||||||
None = 0x0000000000000000,
|
None = 0x0000000000000000,
|
||||||
MapRead = 0x0000000000000001,
|
MapRead = 0x0000000000000001,
|
||||||
MapWrite = 0x0000000000000002,
|
MapWrite = 0x0000000000000002,
|
||||||
@@ -20,31 +19,34 @@ const BufferUsage = enum(u64) {
|
|||||||
Storage = 0x0000000000000080,
|
Storage = 0x0000000000000080,
|
||||||
Indirect = 0x0000000000000100,
|
Indirect = 0x0000000000000100,
|
||||||
QueryResolve = 0x0000000000000200,
|
QueryResolve = 0x0000000000000200,
|
||||||
|
|
||||||
|
fn enumSetToWGPUBufferUsage(set: std.EnumSet(GpuBufferUsage)) c.WGPUBufferUsage {
|
||||||
|
var use: u64 = 0;
|
||||||
|
var iter = set.iterator();
|
||||||
|
while (iter.next()) |flag| use |= @intFromEnum(flag);
|
||||||
|
return use;
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
const GpuBufferDef = struct {
|
pub const GpuBufferDef = struct {
|
||||||
label: ?[]const u8 = null,
|
label: ?[]const u8 = null,
|
||||||
size: u64,
|
size: u64,
|
||||||
usage: std.EnumSet(BufferUsage),
|
usage: std.EnumSet(GpuBufferUsage),
|
||||||
};
|
};
|
||||||
|
|
||||||
pub fn init(gloc: GpuAllocator, def: GpuBufferDef) !@This() {
|
pub fn init(gloc: GpuAllocator, def: GpuBufferDef) !@This() {
|
||||||
var use: u64 = 0;
|
|
||||||
var iter = def.usage.iterator();
|
|
||||||
while (iter.next()) |flag| use |= @intFromEnum(flag);
|
|
||||||
|
|
||||||
// Automatically align the buffer size forward to a multiple of 4 bytes under the hood
|
// Automatically align the buffer size forward to a multiple of 4 bytes under the hood
|
||||||
const aligned_size = std.mem.alignForward(u64, def.size, 4);
|
const aligned_size = std.mem.alignForward(u64, def.size, 4);
|
||||||
|
|
||||||
const raw_handle = try gloc.allocBuffer(.{
|
const raw_handle = try gloc.allocBuffer(.{
|
||||||
.size = aligned_size,
|
.size = aligned_size,
|
||||||
.usage = use,
|
.usage = GpuBufferUsage.enumSetToWGPUBufferUsage(def.usage),
|
||||||
.label = svOpt(def.label),
|
.label = svOpt(def.label),
|
||||||
});
|
});
|
||||||
return .{
|
return .{
|
||||||
.raw = raw_handle,
|
.raw = raw_handle,
|
||||||
.size = aligned_size,
|
.def = def,
|
||||||
.usage = use,
|
|
||||||
.gloc = gloc,
|
.gloc = gloc,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
@@ -79,9 +81,9 @@ pub fn load(
|
|||||||
) !void {
|
) !void {
|
||||||
const bytes = data.len * @sizeOf(T);
|
const bytes = data.len * @sizeOf(T);
|
||||||
|
|
||||||
if (bytes == self.size) {
|
if (bytes == self.def.size) {
|
||||||
// Aligned path: direct download
|
// Aligned path: direct download
|
||||||
c.wgpuQueueWriteBuffer(self.gloc.device.queue, self.raw, 0, data.ptr, self.size);
|
c.wgpuQueueWriteBuffer(self.gloc.device.queue, self.raw, 0, data.ptr, self.def.size);
|
||||||
} else {
|
} else {
|
||||||
// Unaligned path: Split the write into an aligned chunk and a padded remainder
|
// Unaligned path: Split the write into an aligned chunk and a padded remainder
|
||||||
// to support arbitrary lengths without any allocations or large stack arrays.
|
// to support arbitrary lengths without any allocations or large stack arrays.
|
||||||
@@ -99,37 +101,26 @@ pub fn load(
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// GPU to CPU
|
/// GPU to CPU
|
||||||
|
/// Buffer must have MapRead usage or returns error.BufferNotMappable.
|
||||||
pub fn read(self: @This(), alloc: std.mem.Allocator, T: type) ![]T {
|
pub fn read(self: @This(), alloc: std.mem.Allocator, T: type) ![]T {
|
||||||
const out = try alloc.alloc(T, @divExact(self.size, @sizeOf(T)));
|
if (!self.def.usage.contains(.MapRead)) return error.BufferNotMappable;
|
||||||
|
|
||||||
const staging = try init(self.gloc, .{
|
const out = try alloc.alloc(T, @divExact(self.def.size, @sizeOf(T)));
|
||||||
.size = self.size,
|
|
||||||
.usage = .initMany(&.{ .MapRead, .CopyDst }),
|
|
||||||
.label = "staging_read_buffer",
|
|
||||||
});
|
|
||||||
defer staging.deinit();
|
|
||||||
|
|
||||||
const enc = c.wgpuDeviceCreateCommandEncoder(self.gloc.device.device, null) orelse return error.Encoder;
|
|
||||||
c.wgpuCommandEncoderCopyBufferToBuffer(enc, self.raw, 0, staging.raw, 0, self.size);
|
|
||||||
const cmd = c.wgpuCommandEncoderFinish(enc, null);
|
|
||||||
defer c.wgpuCommandEncoderRelease(enc);
|
|
||||||
defer c.wgpuCommandBufferRelease(cmd);
|
|
||||||
c.wgpuQueueSubmit(self.gloc.device.queue, 1, &cmd);
|
|
||||||
|
|
||||||
var mapped = false;
|
var mapped = false;
|
||||||
staging.mapAsync(
|
self.mapAsync(
|
||||||
c.WGPUMapMode_Read,
|
c.WGPUMapMode_Read,
|
||||||
0,
|
0,
|
||||||
self.size,
|
self.def.size,
|
||||||
.{ .callback = onMapped, .userdata1 = &mapped },
|
.{ .callback = onMapped, .userdata1 = &mapped },
|
||||||
);
|
);
|
||||||
while (!mapped) self.gloc.device.poll();
|
while (!mapped) self.gloc.device.poll();
|
||||||
|
|
||||||
const ptr: [*]const T = @ptrCast(@alignCast(
|
const ptr: [*]const T = @ptrCast(@alignCast(
|
||||||
staging.getConstMappedRange(0, self.size),
|
self.getConstMappedRange(0, self.def.size),
|
||||||
));
|
));
|
||||||
@memcpy(out[0..out.len], ptr[0..out.len]);
|
@memcpy(out[0..out.len], ptr[0..out.len]);
|
||||||
staging.unmap();
|
self.unmap();
|
||||||
|
|
||||||
return out;
|
return out;
|
||||||
}
|
}
|
||||||
@@ -143,3 +134,20 @@ fn onMapped(
|
|||||||
const flag: *bool = @ptrCast(@alignCast(userdata1.?));
|
const flag: *bool = @ptrCast(@alignCast(userdata1.?));
|
||||||
flag.* = (status == c.WGPUMapAsyncStatus_Success);
|
flag.* = (status == c.WGPUMapAsyncStatus_Success);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// GPU to GPU. Both buffers must be same size, src needs CopySrc, dst needs CopyDst.
|
||||||
|
pub fn copy(src: @This(), dst: @This()) !void {
|
||||||
|
if (src.def.size != dst.def.size) return error.SizeMismatch;
|
||||||
|
|
||||||
|
const copy_src: u64 = @intFromEnum(GpuBufferUsage.CopySrc);
|
||||||
|
const copy_dst: u64 = @intFromEnum(GpuBufferUsage.CopyDst);
|
||||||
|
if (@as(u64, GpuBufferUsage.enumSetToWGPUBufferUsage(src.def.usage)) & copy_src == 0) return error.SrcNotCopyable;
|
||||||
|
if (@as(u64, GpuBufferUsage.enumSetToWGPUBufferUsage(dst.def.usage)) & copy_dst == 0) return error.DstNotWritable;
|
||||||
|
|
||||||
|
const enc = c.wgpuDeviceCreateCommandEncoder(src.gloc.device.device, null) orelse return error.Encoder;
|
||||||
|
c.wgpuCommandEncoderCopyBufferToBuffer(enc, src.raw, 0, dst.raw, 0, src.def.size);
|
||||||
|
const cmd = c.wgpuCommandEncoderFinish(enc, null);
|
||||||
|
defer c.wgpuCommandEncoderRelease(enc);
|
||||||
|
defer c.wgpuCommandBufferRelease(cmd);
|
||||||
|
c.wgpuQueueSubmit(src.gloc.device.queue, 1, &cmd);
|
||||||
|
}
|
||||||
|
|||||||
+3
-3
@@ -74,7 +74,7 @@ pub fn run(
|
|||||||
const buf = @field(args, field.name);
|
const buf = @field(args, field.name);
|
||||||
const el_size = self.def.bindings[i].element_size;
|
const el_size = self.def.bindings[i].element_size;
|
||||||
if (el_size > 0) {
|
if (el_size > 0) {
|
||||||
elements_count = @intCast(buf.size / el_size);
|
elements_count = @intCast(buf.def.size / el_size);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -85,7 +85,7 @@ pub fn run(
|
|||||||
const el_size = self.def.bindings[i].element_size;
|
const el_size = self.def.bindings[i].element_size;
|
||||||
if (el_size > 0) {
|
if (el_size > 0) {
|
||||||
const expected_min_bytes = @as(u64, elements_count) * el_size;
|
const expected_min_bytes = @as(u64, elements_count) * el_size;
|
||||||
if (buf.size < expected_min_bytes)
|
if (buf.def.size < expected_min_bytes)
|
||||||
return error.BufferTooSmall;
|
return error.BufferTooSmall;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -103,7 +103,7 @@ pub fn run(
|
|||||||
.binding = @intCast(i),
|
.binding = @intCast(i),
|
||||||
.buffer = buf.raw,
|
.buffer = buf.raw,
|
||||||
.offset = 0,
|
.offset = 0,
|
||||||
.size = buf.size, // Size exposes the fully allocated length
|
.size = buf.def.size, // Size exposes the fully allocated length
|
||||||
};
|
};
|
||||||
entry_count += 1;
|
entry_count += 1;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user