Update README and comment for new GpuProcess

This commit is contained in:
AdrienBouvais 2026-05-19 08:00:42 +02:00
parent 62b5224e6e
commit 09e62cf667
3 changed files with 17 additions and 15 deletions

View File

@ -17,7 +17,6 @@ The library exports five primary components:
Below is a complete, self-contained example demonstrating how to initialize the GPU, load data, run a compute shader, and read the results back to the CPU: Below is a complete, self-contained example demonstrating how to initialize the GPU, load data, run a compute shader, and read the results back to the CPU:
```zig ```zig
const std = @import("std"); const std = @import("std");
const gpu = @import("gpu"); const gpu = @import("gpu");
const GpuDevice = gpu.GpuDevice; const GpuDevice = gpu.GpuDevice;
@ -38,7 +37,15 @@ pub fn main(init: std.process.Init) !void {
const gloc = grena.gpuAllocator(); const gloc = grena.gpuAllocator();
// 3. Load the WGSL compute pipeline // 3. Load the WGSL compute pipeline
const add_process = try GpuProcess.init(device, @embedFile("shaders/add.wgsl")); const add_process = try GpuProcess.init(
device,
@embedFile("shaders/add.wgsl"),
.{ .bindings = &.{
.{ .element_size = @sizeOf(f16) },
.{ .element_size = @sizeOf(f16) },
.{ .element_size = @sizeOf(f16) },
} },
);
defer add_process.deinit(); defer add_process.deinit();
// 4. Setup CPU data // 4. Setup CPU data
@ -67,8 +74,7 @@ pub fn main(init: std.process.Init) !void {
try buf_b.load(f16, data_b); try buf_b.load(f16, data_b);
// 7. Dispatch the Compute Process // 7. Dispatch the Compute Process
// We pass the data type (f16) to allow GpuProcess to calculate chunks correctly try add_process.run(gloc, .{ buf_a, buf_b, buf_out });
try add_process.run(gloc, f16, 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 out = try buf_out.read(allocator, f16);
@ -76,6 +82,7 @@ pub fn main(init: std.process.Init) !void {
std.debug.print("Result: {any}\n", .{out}); std.debug.print("Result: {any}\n", .{out});
} }
``` ```
## Dependencies ## Dependencies

View File

@ -21,13 +21,11 @@ pub fn main(init: std.process.Init) !void {
const add_process = try GpuProcess.init( const add_process = try GpuProcess.init(
device, device,
@embedFile("shaders/add.wgsl"), @embedFile("shaders/add.wgsl"),
.{ .{ .bindings = &.{
.bindings = &.{ .{ .element_size = @sizeOf(f16) },
.{ .element_size = @sizeOf(f16) }, .{ .element_size = @sizeOf(f16) },
.{ .element_size = @sizeOf(f16) }, .{ .element_size = @sizeOf(f16) },
.{ .element_size = @sizeOf(f16) }, } },
},
},
); );
defer add_process.deinit(); defer add_process.deinit();
@ -44,7 +42,6 @@ pub fn main(init: std.process.Init) !void {
} }
// 5. Initialize raw GPU Buffers // 5. Initialize raw GPU Buffers
// We pass the EnumSet inline using `.initMany` since the Enum itself isn't exported
const byte_size = len * @sizeOf(f16); const byte_size = len * @sizeOf(f16);
const buf_a = try GpuBuffer.init(gloc, byte_size, .initMany(&.{ .Storage, .CopyDst, .CopySrc })); const buf_a = try GpuBuffer.init(gloc, byte_size, .initMany(&.{ .Storage, .CopyDst, .CopySrc }));
const buf_b = try GpuBuffer.init(gloc, byte_size, .initMany(&.{ .Storage, .CopyDst, .CopySrc })); const buf_b = try GpuBuffer.init(gloc, byte_size, .initMany(&.{ .Storage, .CopyDst, .CopySrc }));
@ -58,7 +55,6 @@ pub fn main(init: std.process.Init) !void {
try buf_b.load(f16, data_b); try buf_b.load(f16, data_b);
// 7. Dispatch the Compute Process // 7. Dispatch the Compute Process
// We pass the data type (f16) to allow GpuProcess to calculate chunks correctly
try add_process.run(gloc, .{ buf_a, buf_b, buf_out }); try add_process.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

View File

@ -48,8 +48,7 @@ pub fn deinit(self: @This()) void {
} }
/// Execute the compute pass with arbitrary buffer bindings via a tuple. /// Execute the compute pass with arbitrary buffer bindings via a tuple.
/// `override_elements_count` can be `null` to infer the dispatch count from the first checked buffer. /// Example: `try proc.run(gloc, .{ buf_a, buf_b, buf_out });`
/// Example: `try proc.run(gloc, null, .{ buf_a, buf_b, buf_out });`
pub fn run( pub fn run(
self: @This(), self: @This(),
gloc: GpuAllocator, gloc: GpuAllocator,