Skip to content

Commit

Permalink
Redo clear buffer to reduce file sizes
Browse files Browse the repository at this point in the history
  • Loading branch information
ckrowland committed Dec 5, 2024
1 parent 3c6ca5b commit c942714
Show file tree
Hide file tree
Showing 8 changed files with 39 additions and 167 deletions.
11 changes: 7 additions & 4 deletions src/editor/main.zig
Original file line number Diff line number Diff line change
Expand Up @@ -398,7 +398,7 @@ pub fn draw(demo: *DemoState) void {
.depth_load_op = .clear,
.depth_store_op = .store,
.depth_clear_value = 1.0,
.stencil_read_only = 1,
.stencil_read_only = .true,
};
const render_pass_info = wgpu.RenderPassDescriptor{
.color_attachment_count = color_attachments.len,
Expand Down Expand Up @@ -483,9 +483,12 @@ pub fn clearSimulation(demo: *DemoState) void {
}

const gctx = demo.gctx;
Wgpu.clearObjBuffer(gctx, Consumer, &demo.buffers.data.consumers);
Wgpu.clearObjBuffer(gctx, Producer, &demo.buffers.data.producers);
Wgpu.clearObjBuffer(gctx, ConsumerHover, &demo.buffers.data.consumer_hovers);
const encoder = demo.gctx.device.createCommandEncoder(null);
defer encoder.release();

Wgpu.clearObjBuffer(encoder, gctx, Consumer, &demo.buffers.data.consumers);
Wgpu.clearObjBuffer(encoder, gctx, Producer, &demo.buffers.data.producers);
Wgpu.clearObjBuffer(encoder, gctx, ConsumerHover, &demo.buffers.data.consumer_hovers);

Statistics.setNum(gctx, .{
.stat_obj = demo.buffers.data.stats,
Expand Down
54 changes: 6 additions & 48 deletions src/editor/wgpu.zig
Original file line number Diff line number Diff line change
Expand Up @@ -288,25 +288,6 @@ pub fn writeToMappedBuffer(gctx: *Gctx, buf: zgpu.BufferHandle, mapped: zgpu.Buf
gctx.submit(&.{commands});
}

pub const shrinkArgs = struct {
new_size: u32,
buf: zgpu.BufferHandle,
};
pub fn shrinkBuffer(gctx: *Gctx, comptime T: type, args: shrinkArgs) void {
const all_zero = [_]u8{0} ** 10000000;
const buf = gctx.lookupResource(args.buf).?;
const buf_info = gctx.lookupResourceInfo(args.buf).?;
const size_to_keep = @sizeOf(T) * args.new_size;
const size_to_clear = buf_info.size - size_to_keep;
const usize_to_clear = @as(usize, @intCast(size_to_clear));
gctx.queue.writeBuffer(
buf,
size_to_keep,
u8,
all_zero[0..usize_to_clear],
);
}

pub fn appendArgs(comptime T: type) type {
return struct {
num_old_structs: u32,
Expand All @@ -323,37 +304,14 @@ pub fn appendBuffer(gctx: *Gctx, comptime T: type, args: appendArgs(T)) void {
);
}

pub fn clearBuffer(gctx: *Gctx, buf: zgpu.BufferHandle) void {
const all_zero = [_]u8{0} ** 10000000;
const buf_info = gctx.lookupResourceInfo(buf).?;
const b_size = @as(usize, @intCast(buf_info.size));
gctx.queue.writeBuffer(
gctx.lookupResource(buf).?,
0,
u8,
all_zero[0..b_size],
);
}

pub fn clearObjBuffer(gctx: *Gctx, comptime T: type, obj_buf: *ObjectBuffer(T)) void {
const all_zero = [_]u8{0} ** 10000000;
pub fn clearObjBuffer(encoder: wgpu.CommandEncoder, gctx: *Gctx, comptime T: type, obj_buf: *ObjectBuffer(T)) void {
const buf = gctx.lookupResource(obj_buf.buf).?;
const map_buf = gctx.lookupResource(obj_buf.mapping.buf).?;
const buf_info = gctx.lookupResourceInfo(obj_buf.buf).?;
const b_size = @as(usize, @intCast(buf_info.size));
gctx.queue.writeBuffer(
gctx.lookupResource(obj_buf.buf).?,
0,
u8,
all_zero[0..b_size],
);

const map_buf_info = gctx.lookupResourceInfo(obj_buf.mapping.buf).?;
const m_size = @as(usize, @intCast(map_buf_info.size));
gctx.queue.writeBuffer(
gctx.lookupResource(obj_buf.mapping.buf).?,
0,
u8,
all_zero[0..m_size],
);

encoder.clearBuffer(buf, 0, @intCast(buf_info.size));
encoder.clearBuffer(map_buf, 0, @intCast(map_buf_info.size));

obj_buf.list.clearAndFree();
obj_buf.mapping.insert_idx = 0;
Expand Down
10 changes: 0 additions & 10 deletions src/random/gui.zig
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,6 @@ fn parameters(demo: *DemoState, gctx: *zgpu.GraphicsContext) void {

zgui.text("Number Of Producers", .{});

const p_bufs = demo.buffers.data.producers;
if (zgui.sliderScalar(
"##np",
u32,
Expand All @@ -120,10 +119,6 @@ fn parameters(demo: *DemoState, gctx: *zgpu.GraphicsContext) void {
});

if (num_producers.old >= num_producers.new) {
Wgpu.shrinkBuffer(gctx, Producer, .{
.new_size = num_producers.new,
.buf = p_bufs.buf,
});
demo.buffers.data.producers.list.resize(num_producers.new) catch unreachable;
demo.buffers.data.producers.mapping.num_structs = num_producers.new;
} else {
Expand Down Expand Up @@ -197,7 +192,6 @@ fn parameters(demo: *DemoState, gctx: *zgpu.GraphicsContext) void {

zgui.dummy(.{ .w = 1.0, .h = 40.0 });

const c_bufs = demo.buffers.data.consumers;
zgui.text("Number of Consumers", .{});
if (zgui.sliderScalar("##nc", u32, .{
.v = &demo.params.num_consumers.new,
Expand All @@ -212,10 +206,6 @@ fn parameters(demo: *DemoState, gctx: *zgpu.GraphicsContext) void {
});

if (num_consumers.old >= num_consumers.new) {
Wgpu.shrinkBuffer(gctx, Consumer, .{
.new_size = num_consumers.new,
.buf = c_bufs.buf,
});
demo.buffers.data.consumers.list.resize(num_consumers.new) catch unreachable;
demo.buffers.data.consumers.mapping.num_structs = num_consumers.new;
} else {
Expand Down
13 changes: 8 additions & 5 deletions src/random/main.zig
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,7 @@ pub fn draw(demo: *DemoState) void {
.depth_load_op = .clear,
.depth_store_op = .store,
.depth_clear_value = 1.0,
.stencil_read_only = 1,
.stencil_read_only = .true,
};
const render_pass_info = wgpu.RenderPassDescriptor{
.color_attachment_count = color_attachments.len,
Expand Down Expand Up @@ -437,9 +437,12 @@ pub fn restartSimulation(demo: *DemoState) void {
return;
}

Wgpu.clearObjBuffer(demo.gctx, Consumer, &demo.buffers.data.consumers);
Wgpu.clearObjBuffer(demo.gctx, Producer, &demo.buffers.data.producers);
Wgpu.clearObjBuffer(demo.gctx, u32, &demo.buffers.data.stats);
const encoder = demo.gctx.device.createCommandEncoder(null);
defer encoder.release();

Wgpu.clearObjBuffer(encoder, demo.gctx, Consumer, &demo.buffers.data.consumers);
Wgpu.clearObjBuffer(encoder, demo.gctx, Producer, &demo.buffers.data.producers);
Wgpu.clearObjBuffer(encoder, demo.gctx, u32, &demo.buffers.data.stats);
demo.buffers.data.stats.mapping.num_structs = Statistics.NUM_STATS;

Consumer.generateBulk(
Expand All @@ -464,7 +467,7 @@ pub fn restartSimulation(demo: *DemoState) void {
.num = demo.params.num_producers.new,
.param = .producers,
});
//demo.stats.clear();
demo.stats.clear();
demo.push_restart = false;
}

Expand Down
50 changes: 6 additions & 44 deletions src/random/wgpu.zig
Original file line number Diff line number Diff line change
Expand Up @@ -292,21 +292,6 @@ pub const shrinkArgs = struct {
new_size: u32,
buf: zgpu.BufferHandle,
};
pub fn shrinkBuffer(gctx: *Gctx, comptime T: type, args: shrinkArgs) void {
const all_zero = [_]u8{0} ** 10000000;
const buf = gctx.lookupResource(args.buf).?;
const buf_info = gctx.lookupResourceInfo(args.buf).?;
const size_to_keep = @sizeOf(T) * args.new_size;
const size_to_clear = buf_info.size - size_to_keep;
const usize_to_clear = @as(usize, @intCast(size_to_clear));
gctx.queue.writeBuffer(
buf,
size_to_keep,
u8,
all_zero[0..usize_to_clear],
);
}

pub fn appendArgs(comptime T: type) type {
return struct {
num_old_structs: u32,
Expand All @@ -323,37 +308,14 @@ pub fn appendBuffer(gctx: *Gctx, comptime T: type, args: appendArgs(T)) void {
);
}

pub fn clearBuffer(gctx: *Gctx, buf: zgpu.BufferHandle) void {
const all_zero = [_]u8{0} ** 10000000;
const buf_info = gctx.lookupResourceInfo(buf).?;
const b_size = @as(usize, @intCast(buf_info.size));
gctx.queue.writeBuffer(
gctx.lookupResource(buf).?,
0,
u8,
all_zero[0..b_size],
);
}

pub fn clearObjBuffer(gctx: *Gctx, comptime T: type, obj_buf: *ObjectBuffer(T)) void {
const all_zero = [_]u8{0} ** 10000000;
pub fn clearObjBuffer(encoder: wgpu.CommandEncoder, gctx: *Gctx, comptime T: type, obj_buf: *ObjectBuffer(T)) void {
const buf = gctx.lookupResource(obj_buf.buf).?;
const map_buf = gctx.lookupResource(obj_buf.mapping.buf).?;
const buf_info = gctx.lookupResourceInfo(obj_buf.buf).?;
const b_size = @as(usize, @intCast(buf_info.size));
gctx.queue.writeBuffer(
gctx.lookupResource(obj_buf.buf).?,
0,
u8,
all_zero[0..b_size],
);

const map_buf_info = gctx.lookupResourceInfo(obj_buf.mapping.buf).?;
const m_size = @as(usize, @intCast(map_buf_info.size));
gctx.queue.writeBuffer(
gctx.lookupResource(obj_buf.mapping.buf).?,
0,
u8,
all_zero[0..m_size],
);

encoder.clearBuffer(buf, 0, @intCast(buf_info.size));
encoder.clearBuffer(map_buf, 0, @intCast(map_buf_info.size));

obj_buf.list.clearAndFree();
obj_buf.mapping.insert_idx = 0;
Expand Down
4 changes: 0 additions & 4 deletions src/variable/gui.zig
Original file line number Diff line number Diff line change
Expand Up @@ -510,8 +510,6 @@ pub fn numConsumerUpdate(demo: *DemoState) void {

const old: u32 = demo.buffers.data.consumers.mapping.num_structs;
if (old >= new) {
const buf = demo.buffers.data.consumers.buf;
Wgpu.shrinkBuffer(demo.gctx, buf, Consumer, new);
demo.buffers.data.consumers.mapping.num_structs = new;
} else {
Consumer.generateBulk(demo, new - old);
Expand All @@ -524,8 +522,6 @@ pub fn numProducersUpdate(demo: *DemoState) void {

const old: u32 = demo.buffers.data.producers.mapping.num_structs;
if (old >= new) {
const buf = demo.buffers.data.producers.buf;
Wgpu.shrinkBuffer(demo.gctx, buf, Producer, new);
demo.buffers.data.producers.mapping.num_structs = new;
} else {
Producer.generateBulk(demo, new - old);
Expand Down
9 changes: 6 additions & 3 deletions src/variable/main.zig
Original file line number Diff line number Diff line change
Expand Up @@ -580,9 +580,12 @@ pub fn restartSimulation(demo: *DemoState) void {
return;
}

Wgpu.clearObjBuffer(demo.gctx, Consumer, &demo.buffers.data.consumers);
Wgpu.clearObjBuffer(demo.gctx, Producer, &demo.buffers.data.producers);
Wgpu.clearObjBuffer(demo.gctx, u32, &demo.buffers.data.stats);
const encoder = demo.gctx.device.createCommandEncoder(null);
defer encoder.release();

Wgpu.clearObjBuffer(encoder, demo.gctx, Consumer, &demo.buffers.data.consumers);
Wgpu.clearObjBuffer(encoder, demo.gctx, Producer, &demo.buffers.data.producers);
Wgpu.clearObjBuffer(encoder, demo.gctx, u32, &demo.buffers.data.stats);
demo.buffers.data.stats.mapping.num_structs = Statistics.NUM_STATS;

const num_consumers = demo.sliders.get("num_consumers").?.slider.val;
Expand Down
55 changes: 6 additions & 49 deletions src/variable/wgpu.zig
Original file line number Diff line number Diff line change
Expand Up @@ -199,26 +199,6 @@ pub fn writeBuffer(
gctx.queue.writeBuffer(gctx.lookupResource(buf).?, 0, T, structs);
}

pub fn shrinkBuffer(
gctx: *Gctx,
buf: zgpu.BufferHandle,
comptime T: type,
new_size: u32,
) void {
const all_zero = [_]u8{0} ** 10000000;
const buff = gctx.lookupResource(buf).?;
const buf_info = gctx.lookupResourceInfo(buf).?;
const size_to_keep = @sizeOf(T) * new_size;
const size_to_clear = buf_info.size - size_to_keep;
const usize_to_clear = @as(usize, @intCast(size_to_clear));
gctx.queue.writeBuffer(
buff,
size_to_keep,
u8,
all_zero[0..usize_to_clear],
);
}

pub fn appendArgs(comptime T: type) type {
return struct {
num_old_structs: u32,
Expand All @@ -235,37 +215,14 @@ pub fn appendBuffer(gctx: *Gctx, comptime T: type, args: appendArgs(T)) void {
);
}

pub fn clearBuffer(gctx: *Gctx, buf: zgpu.BufferHandle) void {
const all_zero = [_]u8{0} ** 10000000;
const buf_info = gctx.lookupResourceInfo(buf).?;
const b_size = @as(usize, @intCast(buf_info.size));
gctx.queue.writeBuffer(
gctx.lookupResource(buf).?,
0,
u8,
all_zero[0..b_size],
);
}

pub fn clearObjBuffer(gctx: *Gctx, comptime T: type, obj_buf: *ObjectBuffer(T)) void {
const all_zero = [_]u8{0} ** 10000000;
pub fn clearObjBuffer(encoder: wgpu.CommandEncoder, gctx: *Gctx, comptime T: type, obj_buf: *ObjectBuffer(T)) void {
const buf = gctx.lookupResource(obj_buf.buf).?;
const map_buf = gctx.lookupResource(obj_buf.mapping.buf).?;
const buf_info = gctx.lookupResourceInfo(obj_buf.buf).?;
const b_size = @as(usize, @intCast(buf_info.size));
gctx.queue.writeBuffer(
gctx.lookupResource(obj_buf.buf).?,
0,
u8,
all_zero[0..b_size],
);

const map_buf_info = gctx.lookupResourceInfo(obj_buf.mapping.buf).?;
const m_size = @as(usize, @intCast(map_buf_info.size));
gctx.queue.writeBuffer(
gctx.lookupResource(obj_buf.mapping.buf).?,
0,
u8,
all_zero[0..m_size],
);

encoder.clearBuffer(buf, 0, @intCast(buf_info.size));
encoder.clearBuffer(map_buf, 0, @intCast(map_buf_info.size));

obj_buf.mapping.insert_idx = 0;
obj_buf.mapping.remove_idx = 0;
Expand Down

0 comments on commit c942714

Please sign in to comment.