commit fb35bee28d5c4798cede5817c569558d364ab959
parent 48879993ed11ac676a6c2b719627475e4a920484
Author: Christian Ermann <christianermann@gmail.com>
Date: Wed, 8 May 2024 20:05:08 -0400
Add Buffer.init_indirect
Diffstat:
1 file changed, 25 insertions(+), 14 deletions(-)
diff --git a/src/main.zig b/src/main.zig
@@ -390,6 +390,24 @@ pub const Buffer = struct {
};
}
+ pub fn init_indirect(n_meshes: u32, device: *gpu.Device) Buffer {
+ std.log.info("buffer init: {} indirect", .{n_meshes});
+ const attrib_size = @sizeOf(u32) * 5;
+ const size = n_meshes * attrib_size;
+ const descriptor = gpu.Buffer.Descriptor{
+ .size = size,
+ .usage = .{ .indirect = true, .copy_dst = true },
+ .mapped_at_creation = .false,
+ };
+ const data = device.createBuffer(&descriptor);
+ return .{
+ .data = data,
+ .size = size,
+ .offset = 0,
+ .attrib_size = attrib_size,
+ };
+ }
+
pub fn init_instance(n_instances: u32, device: *gpu.Device) Buffer {
std.log.info("buffer init: {} instances", .{n_instances});
const attrib_size = @sizeOf(InstanceData);
@@ -458,9 +476,10 @@ pub const MeshBuffer = struct {
normals: Buffer,
tex_coords: Buffer,
indices: Buffer,
+
instances: Buffer,
+ indirect: Buffer,
- buf_indirect: *gpu.Buffer,
meshes: std.ArrayList(Mesh),
pub fn init(options: MeshBufferOptions) MeshBuffer {
@@ -468,13 +487,6 @@ pub const MeshBuffer = struct {
const n_vertices = options.n_vertices;
const n_indices = options.n_indices;
- const descriptor = gpu.Buffer.Descriptor{
- .size = options.n_meshes * 5 * @sizeOf(u32),
- .usage = .{ .indirect = true, .copy_dst = true },
- .mapped_at_creation = .false,
- };
- const buf_indirect = device.createBuffer(&descriptor);
-
return .{
.device = options.device,
.queue = options.queue,
@@ -483,7 +495,7 @@ pub const MeshBuffer = struct {
.tex_coords = Buffer.init_vtx(n_vertices, device),
.indices = Buffer.init_idx(n_indices, device),
.instances = Buffer.init_instance(options.n_meshes, device),
- .buf_indirect = buf_indirect,
+ .indirect = Buffer.init_indirect(options.n_meshes, device),
.meshes = std.ArrayList(Mesh).init(options.allocator),
};
}
@@ -516,16 +528,15 @@ pub const MeshBuffer = struct {
std.log.info("uniform offset: {} bytes", .{offset_u});
const mesh_id: u32 = @intCast(self.meshes.items.len);
- const indirect_args = [_]u32{
+ const indirect_args = [1][5]u32{.{
@intCast(num_indices),
1,
offset_i / self.indices.attrib_size,
offset_p / self.positions.attrib_size,
mesh_id,
- };
+ }};
std.log.info("indirect: {any}", .{indirect_args});
- const offset = mesh_id * 5 * @sizeOf(u32);
- self.queue.writeBuffer(self.buf_indirect, offset, &indirect_args);
+ _ = try self.indirect.allocWrite(&indirect_args, self.queue);
try self.meshes.append(mesh);
return mesh;
@@ -685,7 +696,7 @@ const UnlitRenderPipeline = struct {
for (0..self.mesh_buffer.meshes.items.len) |mesh_idx| {
const offset = mesh_idx * 5 * @sizeOf(u32);
- pass.drawIndexedIndirect(self.mesh_buffer.buf_indirect, offset);
+ pass.drawIndexedIndirect(self.mesh_buffer.indirect.data, offset);
}
}