Updated example in README

This commit is contained in:
adrien 2026-05-21 23:38:22 +02:00
parent e61c5f775d
commit 14def5d555

View File

@ -42,11 +42,14 @@ pub fn main(init: std.process.Init) !void {
const add_cp = try GpuCompute.init( const add_cp = try GpuCompute.init(
gloc, gloc,
@embedFile("shaders/add.wgsl"), @embedFile("shaders/add.wgsl"),
.{ .bindings = &.{ .{
.label = "add",
.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) },
} }, },
},
); );
// 4. Setup CPU data // 4. Setup CPU data
@ -63,12 +66,13 @@ pub fn main(init: std.process.Init) !void {
// 5. Initialize raw GPU Buffers // 5. Initialize raw GPU Buffers
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, .{ .label = "a", .size = byte_size, .usage = .initMany(&.{ .Storage, .CopyDst, .CopySrc }) });
const buf_b = try GpuBuffer.init(gloc, byte_size, .initMany(&.{ .Storage, .CopyDst, .CopySrc })); const buf_b = try GpuBuffer.init(gloc, .{ .label = "b", .size = byte_size, .usage = .initMany(&.{ .Storage, .CopyDst, .CopySrc }) });
const buf_out = try GpuBuffer.init(gloc, byte_size, .initMany(&.{ .Storage, .CopyDst, .CopySrc })); const buf_out = try GpuBuffer.init(gloc, .{ .label = "out", .size = byte_size, .usage = .initMany(&.{ .Storage, .CopyDst, .CopySrc }) });
// Note: Buffers, pipelines, and other objects initialized with 'gloc' // Note: Buffers are safely tied to the GpuArenaAllocator which will automatically
// are safely tied to the GpuArenaAllocator and will automatically release. // release them at the end. You can also manually call buf_x.deinit() if desired.
// This will also release pipelines, textures, ect. Everything using a GpuAllocator to init.
// 6. Transfer data from CPU slices to GPU Buffers // 6. Transfer data from CPU slices to GPU Buffers
try buf_a.load(f16, data_a); try buf_a.load(f16, data_a);
@ -96,6 +100,7 @@ and pull the frame pixels back to the CPU to write a standard image file:
const std = @import("std"); const std = @import("std");
const gpu = @import("gpu"); const gpu = @import("gpu");
const GpuDevice = gpu.GpuDevice; const GpuDevice = gpu.GpuDevice;
const GpuArenaAllocator = gpu.GpuArenaAllocator;
const GpuBuffer = gpu.GpuBuffer; const GpuBuffer = gpu.GpuBuffer;
const GpuRender = gpu.GpuRender; const GpuRender = gpu.GpuRender;
const GpuTexture = gpu.GpuTexture; const GpuTexture = gpu.GpuTexture;
@ -111,8 +116,10 @@ pub fn main(init: std.process.Init) !void {
const device = try GpuDevice.init(.{}); const device = try GpuDevice.init(.{});
defer device.deinit(); defer device.deinit();
// 2. Get base device GPU Allocator // 2. Init VRAM Arena
const gloc = device.gpuAllocator(); var grena = GpuArenaAllocator.init(allocator, device.gpuAllocator());
defer grena.deinit();
const gloc = grena.gpuAllocator();
// 3. Load Render Pipeline // 3. Load Render Pipeline
const circle_rp = try GpuRender.init( const circle_rp = try GpuRender.init(
@ -137,15 +144,16 @@ pub fn main(init: std.process.Init) !void {
// 6. Run the rendering pipeline // 6. Run the rendering pipeline
try circle_rp.draw(gloc, view, 4, .{}); try circle_rp.draw(gloc, view, 4, .{});
// 7. Copy Texture into a readable GPU staging buffer // 7. Load Texture into GpuBuffer
const cpu_staging_buf = try texture.buffCopy(gloc); const cpu_staging_cpu = try texture.buffCopy(gloc);
defer cpu_staging_buf.deinit(); defer cpu_staging_cpu.deinit();
// 8. Read GpuBuffer to CPU memory // 8. Read GpuBuffer to CPU
const pixels = try cpu_staging_buf.read(allocator, u8); // This need to be free manually because CPU memory
const pixels = try cpu_staging_cpu.read(allocator, u8);
defer allocator.free(pixels); defer allocator.free(pixels);
// 9. Write out to a simple PPM image // 9. Write a simple ppm image
try savePpm(init.io, "circle.ppm", width, height, pixels); try savePpm(init.io, "circle.ppm", width, height, pixels);
} }