{gfx,examples}: fix refcounting bugs, improve sysgpu compatability

Signed-off-by: Stephen Gutekanst <stephen@hexops.com>
This commit is contained in:
Stephen Gutekanst 2024-05-01 20:17:16 -07:00
parent cbb728c37a
commit ca08255ec2
11 changed files with 39 additions and 24 deletions

View file

@ -38,7 +38,8 @@ pub const name = .app;
// Note that Mod.state() returns an instance of our module struct.
pub const Mod = mach.Mod(@This());
pub fn deinit(core: *mach.Core.Mod) void {
pub fn deinit(core: *mach.Core.Mod, renderer: *Renderer.Mod) void {
renderer.send(.deinit, .{});
core.send(.deinit, .{});
}

View file

@ -14,7 +14,6 @@ const num_bind_groups = 1024 * 32;
const uniform_offset = 256;
pipeline: *gpu.RenderPipeline,
queue: *gpu.Queue,
bind_groups: [num_bind_groups]*gpu.BindGroup,
uniform_buffer: *gpu.Buffer,
@ -44,6 +43,7 @@ fn init(
) !void {
const device = core.state().device;
const shader_module = device.createShaderModuleWGSL("shader.wgsl", @embedFile("shader.wgsl"));
defer shader_module.release();
// Fragment state
const blend = gpu.BlendState{};
@ -65,6 +65,7 @@ fn init(
.size = @sizeOf(UniformBufferObject) * uniform_offset * num_bind_groups,
.mapped_at_creation = .false,
});
const bind_group_layout_entry = gpu.BindGroupLayout.Entry.buffer(0, .{ .vertex = true }, .uniform, true, 0);
const bind_group_layout = device.createBindGroupLayout(
&gpu.BindGroupLayout.Descriptor.init(.{
@ -72,6 +73,8 @@ fn init(
.entries = &.{bind_group_layout_entry},
}),
);
defer bind_group_layout.release();
var bind_groups: [num_bind_groups]*gpu.BindGroup = undefined;
for (bind_groups, 0..) |_, i| {
bind_groups[i] = device.createBindGroup(
@ -93,7 +96,9 @@ fn init(
.label = label,
.bind_group_layouts = &bind_group_layouts,
}));
const pipeline_descriptor = gpu.RenderPipeline.Descriptor{
defer pipeline_layout.release();
const pipeline = device.createRenderPipeline(&gpu.RenderPipeline.Descriptor{
.label = label,
.fragment = &fragment,
.layout = pipeline_layout,
@ -101,22 +106,19 @@ fn init(
.module = shader_module,
.entry_point = "vertex_main",
},
};
});
renderer.init(.{
.pipeline = device.createRenderPipeline(&pipeline_descriptor),
.queue = device.getQueue(),
.pipeline = pipeline,
.bind_groups = bind_groups,
.uniform_buffer = uniform_buffer,
});
shader_module.release();
}
fn deinit(
renderer: *Mod,
) !void {
renderer.state().pipeline.release();
renderer.state().queue.release();
for (renderer.state().bind_groups) |bind_group| bind_group.release();
renderer.state().uniform_buffer.release();
}
@ -168,6 +170,7 @@ fn renderFrame(
.label = label,
.color_attachments = &color_attachments,
}));
defer render_pass.release();
// Draw
for (renderer.state().bind_groups[0..num_entities]) |bind_group| {