Renamed gloc to glloc
This commit is contained in:
+16
-16
@@ -13,10 +13,10 @@ const Vec = struct {
|
||||
buf: GpuBuffer,
|
||||
len: usize,
|
||||
|
||||
// Changed: gloc is passed by value (const)
|
||||
pub fn initZero(gloc: GpuAllocator, len: usize) !Vec {
|
||||
// Changed: glloc is passed by value (const)
|
||||
pub fn initZero(glloc: GpuAllocator, len: usize) !Vec {
|
||||
return .{
|
||||
.buf = try GpuBuffer.init(gloc, .{
|
||||
.buf = try GpuBuffer.init(glloc, .{
|
||||
.size = len * @sizeOf(f16),
|
||||
.usage = .initMany(&.{ .Storage, .CopyDst, .CopySrc }),
|
||||
}),
|
||||
@@ -24,9 +24,9 @@ const Vec = struct {
|
||||
};
|
||||
}
|
||||
|
||||
// Changed: gloc is passed by value
|
||||
pub fn initLoad(gloc: GpuAllocator, data: []const f16) !Vec {
|
||||
var self = try initZero(gloc, data.len);
|
||||
// Changed: glloc is passed by value
|
||||
pub fn initLoad(glloc: GpuAllocator, data: []const f16) !Vec {
|
||||
var self = try initZero(glloc, data.len);
|
||||
try self.load(data); // Direct access via the interface copy
|
||||
return self;
|
||||
}
|
||||
@@ -40,18 +40,18 @@ const Vec = struct {
|
||||
try self.buf.load(f16, data);
|
||||
}
|
||||
|
||||
// Changed: gloc is passed by value instead of *GpuAllocator
|
||||
pub fn run(self: Vec, gloc: GpuAllocator, other: Vec, process: GpuCompute) !Vec {
|
||||
// Changed: glloc is passed by value instead of *GpuAllocator
|
||||
pub fn run(self: Vec, glloc: GpuAllocator, other: Vec, process: GpuCompute) !Vec {
|
||||
std.debug.assert(self.len == other.len);
|
||||
|
||||
const result = try Vec.initZero(gloc, self.len);
|
||||
const result = try Vec.initZero(glloc, self.len);
|
||||
errdefer result.deinit();
|
||||
|
||||
try process.run(gloc, .{ self.buf, other.buf, result.buf });
|
||||
try process.run(glloc, .{ self.buf, other.buf, result.buf });
|
||||
return result;
|
||||
}
|
||||
|
||||
// Changed: gloc is passed by value instead of *GpuAllocator
|
||||
// Changed: glloc is passed by value instead of *GpuAllocator
|
||||
pub fn read(self: Vec, alloc: std.mem.Allocator) ![]f16 {
|
||||
return self.buf.read(alloc, f16);
|
||||
}
|
||||
@@ -63,9 +63,9 @@ pub fn main(init: std.process.Init) !void {
|
||||
|
||||
var grena = GpuArenaAllocator.init(init.gpa, device.gpuAllocator());
|
||||
defer grena.deinit();
|
||||
const gloc = grena.gpuAllocator();
|
||||
const glloc = grena.gpuAllocator();
|
||||
|
||||
const add_pip = try GpuCompute.init(gloc, @embedFile("shaders/add.wgsl"), .{ .bindings = &.{
|
||||
const add_pip = try GpuCompute.init(glloc, @embedFile("shaders/add.wgsl"), .{ .bindings = &.{
|
||||
.{ .element_size = @sizeOf(f16) },
|
||||
.{ .element_size = @sizeOf(f16) },
|
||||
.{ .element_size = @sizeOf(f16) },
|
||||
@@ -120,9 +120,9 @@ pub fn main(init: std.process.Init) !void {
|
||||
// --- 1. GPU ALLOCATION PHASE ---
|
||||
const alloc_start = std.Io.Clock.awake.now(init.io);
|
||||
|
||||
const a = try Vec.initLoad(gloc, data_a);
|
||||
const a = try Vec.initLoad(glloc, data_a);
|
||||
defer a.deinit();
|
||||
const b = try Vec.initLoad(gloc, data_b);
|
||||
const b = try Vec.initLoad(glloc, data_b);
|
||||
defer b.deinit();
|
||||
|
||||
const alloc_duration = alloc_start.durationTo(std.Io.Clock.awake.now(init.io));
|
||||
@@ -132,7 +132,7 @@ pub fn main(init: std.process.Init) !void {
|
||||
// --- 2. COMPUTE PHASE ---
|
||||
const compute_start = std.Io.Clock.awake.now(init.io);
|
||||
|
||||
const sum = try a.run(gloc, b, add_pip);
|
||||
const sum = try a.run(glloc, b, add_pip);
|
||||
defer sum.deinit();
|
||||
|
||||
// All 3 buffers (a, b, sum) are currently resident in VRAM here.
|
||||
|
||||
+6
-6
@@ -20,18 +20,18 @@ pub fn main(init: std.process.Init) !void {
|
||||
// 2. Init VRAM Arena
|
||||
var grena = GpuArenaAllocator.init(allocator, device.gpuAllocator());
|
||||
defer grena.deinit();
|
||||
const gloc = grena.gpuAllocator();
|
||||
const glloc = grena.gpuAllocator();
|
||||
|
||||
// 3. Load Render Pipeline
|
||||
const circle_rp = try GpuRender.init(
|
||||
gloc,
|
||||
glloc,
|
||||
@embedFile("shaders/circle.wgsl"),
|
||||
.{ .bindings = &.{}, .texture_format = .RGBA8Unorm, .topology = .TriangleStrip },
|
||||
);
|
||||
defer circle_rp.deinit();
|
||||
|
||||
// 4. Create VRAM texture to render into
|
||||
const texture = try GpuTexture.init(gloc, .{
|
||||
const texture = try GpuTexture.init(glloc, .{
|
||||
.format = .RGBA8Unorm,
|
||||
.size = .{ .width = width, .height = height, .depthOrArrayLayers = 1 },
|
||||
.usage = .initMany(&.{ .RenderAttachment, .CopySrc }),
|
||||
@@ -39,14 +39,14 @@ pub fn main(init: std.process.Init) !void {
|
||||
defer texture.deinit();
|
||||
|
||||
// 5. Create a view from texture
|
||||
const view = try GpuTextureView.init(gloc, texture, .{});
|
||||
const view = try GpuTextureView.init(glloc, texture, .{});
|
||||
defer view.deinit();
|
||||
|
||||
// 6. Run the rendering pipeline
|
||||
try circle_rp.draw(gloc, view, 4, .{});
|
||||
try circle_rp.draw(glloc, view, 4, .{});
|
||||
|
||||
// 7. Load Texture into GpuBuffer
|
||||
const cpu_staging_cpu = try texture.buffCopy(gloc);
|
||||
const cpu_staging_cpu = try texture.buffCopy(glloc);
|
||||
defer cpu_staging_cpu.deinit();
|
||||
|
||||
// 8. Read GpuBuffer to CPU
|
||||
|
||||
@@ -15,11 +15,11 @@ pub fn main(init: std.process.Init) !void {
|
||||
// 2. Create a GPU Arena to manage VRAM
|
||||
var grena = GpuArenaAllocator.init(allocator, device.gpuAllocator());
|
||||
defer grena.deinit();
|
||||
const gloc = grena.gpuAllocator();
|
||||
const glloc = grena.gpuAllocator();
|
||||
|
||||
// 3. Load the WGSL compute pipeline
|
||||
const add_cp = try GpuCompute.init(
|
||||
gloc,
|
||||
glloc,
|
||||
@embedFile("shaders/add.wgsl"),
|
||||
.{
|
||||
.label = "add",
|
||||
@@ -45,9 +45,9 @@ pub fn main(init: std.process.Init) !void {
|
||||
|
||||
// 5. Initialize raw GPU Buffers
|
||||
const byte_size = len * @sizeOf(f16);
|
||||
const buf_a = try GpuBuffer.init(gloc, .{ .label = "a", .size = byte_size, .usage = .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, .{ .label = "out", .size = byte_size, .usage = .initMany(&.{ .Storage, .CopyDst, .CopySrc }) });
|
||||
const buf_a = try GpuBuffer.init(glloc, .{ .label = "a", .size = byte_size, .usage = .initMany(&.{ .Storage, .CopyDst, .CopySrc }) });
|
||||
const buf_b = try GpuBuffer.init(glloc, .{ .label = "b", .size = byte_size, .usage = .initMany(&.{ .Storage, .CopyDst, .CopySrc }) });
|
||||
const buf_out = try GpuBuffer.init(glloc, .{ .label = "out", .size = byte_size, .usage = .initMany(&.{ .Storage, .CopyDst, .CopySrc }) });
|
||||
|
||||
// Note: Buffers are safely tied to the GpuArenaAllocator which will automatically
|
||||
// release them at the end. You can also manually call buf_x.deinit() if desired.
|
||||
@@ -58,10 +58,10 @@ pub fn main(init: std.process.Init) !void {
|
||||
try buf_b.load(f16, data_b);
|
||||
|
||||
// 7. Dispatch the Compute
|
||||
try add_cp.run(gloc, .{ buf_a, buf_b, buf_out });
|
||||
try add_cp.run(glloc, .{ buf_a, buf_b, buf_out });
|
||||
|
||||
// 8. Map and copy the resulting buffer back to the CPU
|
||||
const staging = try GpuBuffer.init(gloc, .{
|
||||
const staging = try GpuBuffer.init(glloc, .{
|
||||
.size = byte_size,
|
||||
.usage = .initMany(&.{ .MapRead, .CopyDst }),
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user