{gfx,examples}: fix refcounting bugs, improve sysgpu compatability
Signed-off-by: Stephen Gutekanst <stephen@hexops.com>
This commit is contained in:
parent
cbb728c37a
commit
ca08255ec2
11 changed files with 39 additions and 24 deletions
|
|
@ -96,6 +96,7 @@ fn tick(core: *mach.Core.Mod, game: *Mod) !void {
|
||||||
.label = label,
|
.label = label,
|
||||||
.color_attachments = &color_attachments,
|
.color_attachments = &color_attachments,
|
||||||
}));
|
}));
|
||||||
|
defer render_pass.release();
|
||||||
|
|
||||||
// Draw
|
// Draw
|
||||||
render_pass.setPipeline(game.state().pipeline);
|
render_pass.setPipeline(game.state().pipeline);
|
||||||
|
|
|
||||||
|
|
@ -38,7 +38,8 @@ pub const name = .app;
|
||||||
// Note that Mod.state() returns an instance of our module struct.
|
// Note that Mod.state() returns an instance of our module struct.
|
||||||
pub const Mod = mach.Mod(@This());
|
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, .{});
|
core.send(.deinit, .{});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -14,7 +14,6 @@ const num_bind_groups = 1024 * 32;
|
||||||
const uniform_offset = 256;
|
const uniform_offset = 256;
|
||||||
|
|
||||||
pipeline: *gpu.RenderPipeline,
|
pipeline: *gpu.RenderPipeline,
|
||||||
queue: *gpu.Queue,
|
|
||||||
bind_groups: [num_bind_groups]*gpu.BindGroup,
|
bind_groups: [num_bind_groups]*gpu.BindGroup,
|
||||||
uniform_buffer: *gpu.Buffer,
|
uniform_buffer: *gpu.Buffer,
|
||||||
|
|
||||||
|
|
@ -44,6 +43,7 @@ fn init(
|
||||||
) !void {
|
) !void {
|
||||||
const device = core.state().device;
|
const device = core.state().device;
|
||||||
const shader_module = device.createShaderModuleWGSL("shader.wgsl", @embedFile("shader.wgsl"));
|
const shader_module = device.createShaderModuleWGSL("shader.wgsl", @embedFile("shader.wgsl"));
|
||||||
|
defer shader_module.release();
|
||||||
|
|
||||||
// Fragment state
|
// Fragment state
|
||||||
const blend = gpu.BlendState{};
|
const blend = gpu.BlendState{};
|
||||||
|
|
@ -65,6 +65,7 @@ fn init(
|
||||||
.size = @sizeOf(UniformBufferObject) * uniform_offset * num_bind_groups,
|
.size = @sizeOf(UniformBufferObject) * uniform_offset * num_bind_groups,
|
||||||
.mapped_at_creation = .false,
|
.mapped_at_creation = .false,
|
||||||
});
|
});
|
||||||
|
|
||||||
const bind_group_layout_entry = gpu.BindGroupLayout.Entry.buffer(0, .{ .vertex = true }, .uniform, true, 0);
|
const bind_group_layout_entry = gpu.BindGroupLayout.Entry.buffer(0, .{ .vertex = true }, .uniform, true, 0);
|
||||||
const bind_group_layout = device.createBindGroupLayout(
|
const bind_group_layout = device.createBindGroupLayout(
|
||||||
&gpu.BindGroupLayout.Descriptor.init(.{
|
&gpu.BindGroupLayout.Descriptor.init(.{
|
||||||
|
|
@ -72,6 +73,8 @@ fn init(
|
||||||
.entries = &.{bind_group_layout_entry},
|
.entries = &.{bind_group_layout_entry},
|
||||||
}),
|
}),
|
||||||
);
|
);
|
||||||
|
defer bind_group_layout.release();
|
||||||
|
|
||||||
var bind_groups: [num_bind_groups]*gpu.BindGroup = undefined;
|
var bind_groups: [num_bind_groups]*gpu.BindGroup = undefined;
|
||||||
for (bind_groups, 0..) |_, i| {
|
for (bind_groups, 0..) |_, i| {
|
||||||
bind_groups[i] = device.createBindGroup(
|
bind_groups[i] = device.createBindGroup(
|
||||||
|
|
@ -93,7 +96,9 @@ fn init(
|
||||||
.label = label,
|
.label = label,
|
||||||
.bind_group_layouts = &bind_group_layouts,
|
.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,
|
.label = label,
|
||||||
.fragment = &fragment,
|
.fragment = &fragment,
|
||||||
.layout = pipeline_layout,
|
.layout = pipeline_layout,
|
||||||
|
|
@ -101,22 +106,19 @@ fn init(
|
||||||
.module = shader_module,
|
.module = shader_module,
|
||||||
.entry_point = "vertex_main",
|
.entry_point = "vertex_main",
|
||||||
},
|
},
|
||||||
};
|
});
|
||||||
|
|
||||||
renderer.init(.{
|
renderer.init(.{
|
||||||
.pipeline = device.createRenderPipeline(&pipeline_descriptor),
|
.pipeline = pipeline,
|
||||||
.queue = device.getQueue(),
|
|
||||||
.bind_groups = bind_groups,
|
.bind_groups = bind_groups,
|
||||||
.uniform_buffer = uniform_buffer,
|
.uniform_buffer = uniform_buffer,
|
||||||
});
|
});
|
||||||
shader_module.release();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn deinit(
|
fn deinit(
|
||||||
renderer: *Mod,
|
renderer: *Mod,
|
||||||
) !void {
|
) !void {
|
||||||
renderer.state().pipeline.release();
|
renderer.state().pipeline.release();
|
||||||
renderer.state().queue.release();
|
|
||||||
for (renderer.state().bind_groups) |bind_group| bind_group.release();
|
for (renderer.state().bind_groups) |bind_group| bind_group.release();
|
||||||
renderer.state().uniform_buffer.release();
|
renderer.state().uniform_buffer.release();
|
||||||
}
|
}
|
||||||
|
|
@ -168,6 +170,7 @@ fn renderFrame(
|
||||||
.label = label,
|
.label = label,
|
||||||
.color_attachments = &color_attachments,
|
.color_attachments = &color_attachments,
|
||||||
}));
|
}));
|
||||||
|
defer render_pass.release();
|
||||||
|
|
||||||
// Draw
|
// Draw
|
||||||
for (renderer.state().bind_groups[0..num_entities]) |bind_group| {
|
for (renderer.state().bind_groups[0..num_entities]) |bind_group| {
|
||||||
|
|
|
||||||
|
|
@ -222,8 +222,6 @@ fn tick(
|
||||||
.load_op = .clear,
|
.load_op = .clear,
|
||||||
.store_op = .store,
|
.store_op = .store,
|
||||||
}};
|
}};
|
||||||
// TODO: can we eliminate this assignment
|
|
||||||
game.state().frame_encoder = core.state().device.createCommandEncoder(&.{ .label = label });
|
|
||||||
game.state().frame_render_pass = game.state().frame_encoder.beginRenderPass(&gpu.RenderPassDescriptor.init(.{
|
game.state().frame_render_pass = game.state().frame_encoder.beginRenderPass(&gpu.RenderPassDescriptor.init(.{
|
||||||
.label = label,
|
.label = label,
|
||||||
.color_attachments = &color_attachments,
|
.color_attachments = &color_attachments,
|
||||||
|
|
@ -244,9 +242,10 @@ fn endFrame(game: *Mod, core: *mach.Core.Mod) !void {
|
||||||
game.state().frame_render_pass.end();
|
game.state().frame_render_pass.end();
|
||||||
const label = @tagName(name) ++ ".endFrame";
|
const label = @tagName(name) ++ ".endFrame";
|
||||||
var command = game.state().frame_encoder.finish(&.{ .label = label });
|
var command = game.state().frame_encoder.finish(&.{ .label = label });
|
||||||
game.state().frame_encoder.release();
|
|
||||||
defer command.release();
|
defer command.release();
|
||||||
core.state().queue.submit(&[_]*gpu.CommandBuffer{command});
|
core.state().queue.submit(&[_]*gpu.CommandBuffer{command});
|
||||||
|
game.state().frame_encoder.release();
|
||||||
|
game.state().frame_render_pass.release();
|
||||||
|
|
||||||
// Present the frame
|
// Present the frame
|
||||||
core.send(.present_frame, .{});
|
core.send(.present_frame, .{});
|
||||||
|
|
|
||||||
|
|
@ -76,8 +76,6 @@ fn init(
|
||||||
}
|
}
|
||||||
|
|
||||||
fn prepare(core: *mach.Core.Mod, glyphs: *Mod) !void {
|
fn prepare(core: *mach.Core.Mod, glyphs: *Mod) !void {
|
||||||
const device = core.state().device;
|
|
||||||
const queue = device.getQueue();
|
|
||||||
var s = glyphs.state();
|
var s = glyphs.state();
|
||||||
|
|
||||||
// Prepare which glyphs we will render
|
// Prepare which glyphs we will render
|
||||||
|
|
@ -126,5 +124,5 @@ fn prepare(core: *mach.Core.Mod, glyphs: *Mod) !void {
|
||||||
.bytes_per_row = @as(u32, @intCast(img_size.width * 4)),
|
.bytes_per_row = @as(u32, @intCast(img_size.width * 4)),
|
||||||
.rows_per_image = @as(u32, @intCast(img_size.height)),
|
.rows_per_image = @as(u32, @intCast(img_size.height)),
|
||||||
};
|
};
|
||||||
queue.writeTexture(&.{ .texture = s.texture }, &data_layout, &img_size, s.texture_atlas.data);
|
core.state().queue.writeTexture(&.{ .texture = s.texture }, &data_layout, &img_size, s.texture_atlas.data);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -162,6 +162,7 @@ fn tick(
|
||||||
.label = label,
|
.label = label,
|
||||||
.color_attachments = &color_attachments,
|
.color_attachments = &color_attachments,
|
||||||
}));
|
}));
|
||||||
|
defer render_pass.release();
|
||||||
|
|
||||||
// Draw nothing
|
// Draw nothing
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -207,8 +207,6 @@ fn tick(
|
||||||
.load_op = .clear,
|
.load_op = .clear,
|
||||||
.store_op = .store,
|
.store_op = .store,
|
||||||
}};
|
}};
|
||||||
// TODO: can we eliminate this assignment
|
|
||||||
game.state().frame_encoder = core.state().device.createCommandEncoder(&.{ .label = label });
|
|
||||||
game.state().frame_render_pass = game.state().frame_encoder.beginRenderPass(&gpu.RenderPassDescriptor.init(.{
|
game.state().frame_render_pass = game.state().frame_encoder.beginRenderPass(&gpu.RenderPassDescriptor.init(.{
|
||||||
.label = label,
|
.label = label,
|
||||||
.color_attachments = &color_attachments,
|
.color_attachments = &color_attachments,
|
||||||
|
|
|
||||||
|
|
@ -251,7 +251,6 @@ fn tick(
|
||||||
.load_op = .clear,
|
.load_op = .clear,
|
||||||
.store_op = .store,
|
.store_op = .store,
|
||||||
}};
|
}};
|
||||||
game.state().frame_encoder = core.state().device.createCommandEncoder(&.{ .label = label });
|
|
||||||
game.state().frame_render_pass = game.state().frame_encoder.beginRenderPass(&gpu.RenderPassDescriptor.init(.{
|
game.state().frame_render_pass = game.state().frame_encoder.beginRenderPass(&gpu.RenderPassDescriptor.init(.{
|
||||||
.label = label,
|
.label = label,
|
||||||
.color_attachments = &color_attachments,
|
.color_attachments = &color_attachments,
|
||||||
|
|
|
||||||
|
|
@ -107,6 +107,7 @@ fn updatePipeline(
|
||||||
encoder.writeBuffer(built.transforms, 0, gfx.SpritePipeline.cp_transforms[0..i]);
|
encoder.writeBuffer(built.transforms, 0, gfx.SpritePipeline.cp_transforms[0..i]);
|
||||||
encoder.writeBuffer(built.uv_transforms, 0, gfx.SpritePipeline.cp_uv_transforms[0..i]);
|
encoder.writeBuffer(built.uv_transforms, 0, gfx.SpritePipeline.cp_uv_transforms[0..i]);
|
||||||
encoder.writeBuffer(built.sizes, 0, gfx.SpritePipeline.cp_sizes[0..i]);
|
encoder.writeBuffer(built.sizes, 0, gfx.SpritePipeline.cp_sizes[0..i]);
|
||||||
|
|
||||||
var command = encoder.finish(&.{ .label = label });
|
var command = encoder.finish(&.{ .label = label });
|
||||||
defer command.release();
|
defer command.release();
|
||||||
core.state().queue.submit(&[_]*gpu.CommandBuffer{command});
|
core.state().queue.submit(&[_]*gpu.CommandBuffer{command});
|
||||||
|
|
|
||||||
|
|
@ -60,7 +60,7 @@ pub const components = .{
|
||||||
};
|
};
|
||||||
|
|
||||||
pub const events = .{
|
pub const events = .{
|
||||||
.init = .{ .handler = fn () void },
|
.init = .{ .handler = init },
|
||||||
.deinit = .{ .handler = deinit },
|
.deinit = .{ .handler = deinit },
|
||||||
.update = .{ .handler = update },
|
.update = .{ .handler = update },
|
||||||
.pre_render = .{ .handler = preRender },
|
.pre_render = .{ .handler = preRender },
|
||||||
|
|
@ -119,6 +119,10 @@ pub const BuiltPipeline = struct {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
fn init(sprite_pipeline: *Mod) void {
|
||||||
|
sprite_pipeline.init(.{});
|
||||||
|
}
|
||||||
|
|
||||||
fn deinit(sprite_pipeline: *Mod) void {
|
fn deinit(sprite_pipeline: *Mod) void {
|
||||||
var archetypes_iter = sprite_pipeline.entities.query(.{ .all = &.{
|
var archetypes_iter = sprite_pipeline.entities.query(.{ .all = &.{
|
||||||
.{ .mach_gfx_sprite_pipeline = &.{
|
.{ .mach_gfx_sprite_pipeline = &.{
|
||||||
|
|
@ -131,8 +135,6 @@ fn deinit(sprite_pipeline: *Mod) void {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn update(core: *mach.Core.Mod, sprite_pipeline: *Mod) !void {
|
fn update(core: *mach.Core.Mod, sprite_pipeline: *Mod) !void {
|
||||||
sprite_pipeline.init(.{});
|
|
||||||
|
|
||||||
// Destroy all sprite render pipelines. We will rebuild them all.
|
// Destroy all sprite render pipelines. We will rebuild them all.
|
||||||
deinit(sprite_pipeline);
|
deinit(sprite_pipeline);
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -267,9 +267,21 @@ fn buildPipeline(
|
||||||
.label = label,
|
.label = label,
|
||||||
.layout = bind_group_layout,
|
.layout = bind_group_layout,
|
||||||
.entries = &.{
|
.entries = &.{
|
||||||
|
if (mach.use_sysgpu)
|
||||||
|
gpu.BindGroup.Entry.buffer(0, uniforms, 0, @sizeOf(Uniforms), @sizeOf(Uniforms))
|
||||||
|
else
|
||||||
gpu.BindGroup.Entry.buffer(0, uniforms, 0, @sizeOf(Uniforms)),
|
gpu.BindGroup.Entry.buffer(0, uniforms, 0, @sizeOf(Uniforms)),
|
||||||
|
if (mach.use_sysgpu)
|
||||||
|
gpu.BindGroup.Entry.buffer(1, transforms, 0, @sizeOf(math.Mat4x4) * texts_buffer_cap, @sizeOf(math.Mat4x4))
|
||||||
|
else
|
||||||
gpu.BindGroup.Entry.buffer(1, transforms, 0, @sizeOf(math.Mat4x4) * texts_buffer_cap),
|
gpu.BindGroup.Entry.buffer(1, transforms, 0, @sizeOf(math.Mat4x4) * texts_buffer_cap),
|
||||||
|
if (mach.use_sysgpu)
|
||||||
|
gpu.BindGroup.Entry.buffer(2, colors, 0, @sizeOf(math.Vec4) * texts_buffer_cap, @sizeOf(math.Vec4))
|
||||||
|
else
|
||||||
gpu.BindGroup.Entry.buffer(2, colors, 0, @sizeOf(math.Vec4) * texts_buffer_cap),
|
gpu.BindGroup.Entry.buffer(2, colors, 0, @sizeOf(math.Vec4) * texts_buffer_cap),
|
||||||
|
if (mach.use_sysgpu)
|
||||||
|
gpu.BindGroup.Entry.buffer(3, glyphs, 0, @sizeOf(Glyph) * texts_buffer_cap, @sizeOf(Glyph))
|
||||||
|
else
|
||||||
gpu.BindGroup.Entry.buffer(3, glyphs, 0, @sizeOf(Glyph) * texts_buffer_cap),
|
gpu.BindGroup.Entry.buffer(3, glyphs, 0, @sizeOf(Glyph) * texts_buffer_cap),
|
||||||
gpu.BindGroup.Entry.sampler(4, texture_sampler),
|
gpu.BindGroup.Entry.sampler(4, texture_sampler),
|
||||||
gpu.BindGroup.Entry.textureView(5, texture_view),
|
gpu.BindGroup.Entry.textureView(5, texture_view),
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue