all: update to global vs. local ECS change

See hexops/mach-ecs@ef06fb6473

Signed-off-by: Stephen Gutekanst <stephen@hexops.com>
This commit is contained in:
Stephen Gutekanst 2023-12-16 19:40:26 -07:00
parent 11aed4d16f
commit 8ff30c931f
4 changed files with 649 additions and 642 deletions

View file

@ -12,8 +12,8 @@
}, },
.dependencies = .{ .dependencies = .{
.mach_ecs = .{ .mach_ecs = .{
.url = "https://pkg.machengine.org/mach-ecs/46fbc9175a70a8cc983b88f911aa076b625e0fb2.tar.gz", .url = "https://pkg.machengine.org/mach-ecs/ef06fb647353356eff080ad3ec337e028b492d41.tar.gz",
.hash = "1220acdc7bdb09425ccdd92bb906c4e30e3391c95ab15049c946c98cc6643a7ad250", .hash = "1220014851adb37d191430ac371cc467e0e0eb633b84b856c7a37e41a3149dea7ce8",
}, },
.mach_core = .{ .mach_core = .{
.url = "https://pkg.machengine.org/mach-core/cce02fb96ca787378289c5855b381f0ca9f3e090.tar.gz", .url = "https://pkg.machengine.org/mach-core/cce02fb96ca787378289c5855b381f0ca9f3e090.tar.gz",

View file

@ -16,7 +16,8 @@ pub const Engine = struct {
pub const name = .engine; pub const name = .engine;
pub fn engineInit(world: *World) !void { pub const local = struct {
pub fn init(world: *World) !void {
core.allocator = allocator; core.allocator = allocator;
try core.init(.{}); try core.init(.{});
const state = &world.mod.engine.state; const state = &world.mod.engine.state;
@ -27,28 +28,29 @@ pub const Engine = struct {
.label = "engine.state.encoder", .label = "engine.state.encoder",
}); });
try world.send(.init, .{}); try world.send(null, .init, .{});
} }
pub fn engineDeinit( pub fn deinit(
world: *World, world: *World,
engine: *World.Mod(.engine), engine: *World.Mod(.engine),
) !void { ) !void {
// TODO: this triggers a device loss error, which we should handle correctly // TODO: this triggers a device loss error, which we should handle correctly
// engine.state.device.release(); // engine.state.device.release();
engine.state.queue.release(); engine.state.queue.release();
try world.send(.deinit, .{}); try world.send(null, .deinit, .{});
core.deinit(); core.deinit();
world.deinit(); world.deinit();
_ = gpa.deinit(); _ = gpa.deinit();
} }
pub fn engineExit(world: *World) !void { // Engine module's exit handler
try world.send(.exit, .{}); pub fn exit(world: *World) !void {
try world.send(null, .exit, .{});
world.mod.engine.state.exit = true; world.mod.engine.state.exit = true;
} }
pub fn engineBeginPass( pub fn beginPass(
engine: *World.Mod(.engine), engine: *World.Mod(.engine),
clear_color: gpu.Color, clear_color: gpu.Color,
) !void { ) !void {
@ -69,7 +71,7 @@ pub const Engine = struct {
engine.state.pass = engine.state.encoder.beginRenderPass(&pass_info); engine.state.pass = engine.state.encoder.beginRenderPass(&pass_info);
} }
pub fn engineEndPass( pub fn endPass(
engine: *World.Mod(.engine), engine: *World.Mod(.engine),
) !void { ) !void {
// End this pass // End this pass
@ -87,9 +89,10 @@ pub const Engine = struct {
}); });
} }
pub fn enginePresent() !void { pub fn present() !void {
core.swap_chain.present(); core.swap_chain.present();
} }
};
}; };
pub const App = struct { pub const App = struct {
@ -97,15 +100,15 @@ pub const App = struct {
pub fn init(app: *@This()) !void { pub fn init(app: *@This()) !void {
app.* = .{ .world = try World.init(allocator) }; app.* = .{ .world = try World.init(allocator) };
try app.world.send(.engineInit, .{}); try app.world.send(.engine, .init, .{});
} }
pub fn deinit(app: *@This()) void { pub fn deinit(app: *@This()) void {
try app.world.send(.engineDeinit, .{}); try app.world.send(.engine, .deinit, .{});
} }
pub fn update(app: *@This()) !bool { pub fn update(app: *@This()) !bool {
try app.world.send(.tick, .{}); try app.world.send(null, .tick, .{});
return app.world.mod.engine.state.exit; return app.world.mod.engine.state.exit;
} }
}; };

View file

@ -120,20 +120,26 @@ pub const PipelineOptions = struct {
pipeline_layout: ?*gpu.PipelineLayout = null, pipeline_layout: ?*gpu.PipelineLayout = null,
}; };
pub fn machGfxSpriteInit( pub fn deinit(sprite_mod: *mach.Mod(.mach_gfx_sprite)) !void {
for (sprite_mod.state.pipelines.entries.items(.value)) |*pipeline| pipeline.deinit();
sprite_mod.state.pipelines.deinit(sprite_mod.allocator);
}
pub const local = struct {
pub fn init(
sprite_mod: *mach.Mod(.mach_gfx_sprite), sprite_mod: *mach.Mod(.mach_gfx_sprite),
) !void { ) !void {
sprite_mod.state = .{ sprite_mod.state = .{
// TODO: struct default value initializers don't work // TODO: struct default value initializers don't work
.pipelines = .{}, .pipelines = .{},
}; };
} }
pub fn machGfxSpriteInitPipeline( pub fn initPipeline(
engine: *mach.Mod(.engine), engine: *mach.Mod(.engine),
sprite_mod: *mach.Mod(.mach_gfx_sprite), sprite_mod: *mach.Mod(.mach_gfx_sprite),
opt: PipelineOptions, opt: PipelineOptions,
) !void { ) !void {
const device = engine.state.device; const device = engine.state.device;
const pipeline = try sprite_mod.state.pipelines.getOrPut(engine.allocator, opt.pipeline); const pipeline = try sprite_mod.state.pipelines.getOrPut(engine.allocator, opt.pipeline);
@ -243,7 +249,7 @@ pub fn machGfxSpriteInitPipeline(
.bind_group_layouts = &bind_group_layouts, .bind_group_layouts = &bind_group_layouts,
})); }));
defer pipeline_layout.release(); defer pipeline_layout.release();
const render = device.createRenderPipeline(&gpu.RenderPipeline.Descriptor{ const render_pipeline = device.createRenderPipeline(&gpu.RenderPipeline.Descriptor{
.fragment = &fragment, .fragment = &fragment,
.layout = pipeline_layout, .layout = pipeline_layout,
.vertex = gpu.VertexState{ .vertex = gpu.VertexState{
@ -253,7 +259,7 @@ pub fn machGfxSpriteInitPipeline(
}); });
pipeline.value_ptr.* = Pipeline{ pipeline.value_ptr.* = Pipeline{
.render = render, .render = render_pipeline,
.texture_sampler = texture_sampler, .texture_sampler = texture_sampler,
.texture = opt.texture, .texture = opt.texture,
.texture2 = opt.texture2, .texture2 = opt.texture2,
@ -267,18 +273,13 @@ pub fn machGfxSpriteInitPipeline(
.sizes = sizes, .sizes = sizes,
}; };
pipeline.value_ptr.reference(); pipeline.value_ptr.reference();
} }
pub fn deinit(sprite_mod: *mach.Mod(.mach_gfx_sprite)) !void { pub fn updated(
for (sprite_mod.state.pipelines.entries.items(.value)) |*pipeline| pipeline.deinit();
sprite_mod.state.pipelines.deinit(sprite_mod.allocator);
}
pub fn machGfxSpriteUpdated(
engine: *mach.Mod(.engine), engine: *mach.Mod(.engine),
sprite_mod: *mach.Mod(.mach_gfx_sprite), sprite_mod: *mach.Mod(.mach_gfx_sprite),
pipeline_id: u32, pipeline_id: u32,
) !void { ) !void {
const pipeline = sprite_mod.state.pipelines.getPtr(pipeline_id).?; const pipeline = sprite_mod.state.pipelines.getPtr(pipeline_id).?;
const device = engine.state.device; const device = engine.state.device;
@ -321,13 +322,13 @@ pub fn machGfxSpriteUpdated(
defer command.release(); defer command.release();
engine.state.queue.submit(&[_]*gpu.CommandBuffer{command}); engine.state.queue.submit(&[_]*gpu.CommandBuffer{command});
} }
pub fn machGfxSpritePreRender( pub fn preRender(
engine: *mach.Mod(.engine), engine: *mach.Mod(.engine),
sprite_mod: *mach.Mod(.mach_gfx_sprite), sprite_mod: *mach.Mod(.mach_gfx_sprite),
pipeline_id: u32, pipeline_id: u32,
) !void { ) !void {
const pipeline = sprite_mod.state.pipelines.get(pipeline_id).?; const pipeline = sprite_mod.state.pipelines.get(pipeline_id).?;
// Update uniform buffer // Update uniform buffer
@ -349,13 +350,13 @@ pub fn machGfxSpritePreRender(
}; };
engine.state.encoder.writeBuffer(pipeline.uniforms, 0, &[_]Uniforms{uniforms}); engine.state.encoder.writeBuffer(pipeline.uniforms, 0, &[_]Uniforms{uniforms});
} }
pub fn machGfxSpriteRender( pub fn render(
engine: *mach.Mod(.engine), engine: *mach.Mod(.engine),
sprite_mod: *mach.Mod(.mach_gfx_sprite), sprite_mod: *mach.Mod(.mach_gfx_sprite),
pipeline_id: u32, pipeline_id: u32,
) !void { ) !void {
const pipeline = sprite_mod.state.pipelines.get(pipeline_id).?; const pipeline = sprite_mod.state.pipelines.get(pipeline_id).?;
// Draw the sprite batch // Draw the sprite batch
@ -365,4 +366,5 @@ pub fn machGfxSpriteRender(
// TODO: remove dynamic offsets? // TODO: remove dynamic offsets?
pass.setBindGroup(0, pipeline.bind_group, &.{}); pass.setBindGroup(0, pipeline.bind_group, &.{});
pass.draw(total_vertices, 1, 0, 0); pass.draw(total_vertices, 1, 0, 0);
} }
};

View file

@ -170,20 +170,26 @@ pub const PipelineOptions = struct {
pipeline_layout: ?*gpu.PipelineLayout = null, pipeline_layout: ?*gpu.PipelineLayout = null,
}; };
pub fn machGfxTextInit( pub fn deinit(text_mod: *mach.Mod(.mach_gfx_text)) !void {
for (text_mod.state.pipelines.entries.items(.value)) |*pipeline| pipeline.deinit(text_mod.allocator);
text_mod.state.pipelines.deinit(text_mod.allocator);
}
pub const local = struct {
pub fn init(
text_mod: *mach.Mod(.mach_gfx_text), text_mod: *mach.Mod(.mach_gfx_text),
) !void { ) !void {
text_mod.state = .{ text_mod.state = .{
// TODO: struct default value initializers don't work // TODO: struct default value initializers don't work
.pipelines = .{}, .pipelines = .{},
}; };
} }
pub fn machGfxTextInitPipeline( pub fn initPipeline(
engine: *mach.Mod(.engine), engine: *mach.Mod(.engine),
text_mod: *mach.Mod(.mach_gfx_text), text_mod: *mach.Mod(.mach_gfx_text),
opt: PipelineOptions, opt: PipelineOptions,
) !void { ) !void {
const device = engine.state.device; const device = engine.state.device;
const pipeline = try text_mod.state.pipelines.getOrPut(engine.allocator, opt.pipeline); const pipeline = try text_mod.state.pipelines.getOrPut(engine.allocator, opt.pipeline);
@ -311,7 +317,7 @@ pub fn machGfxTextInitPipeline(
.bind_group_layouts = &bind_group_layouts, .bind_group_layouts = &bind_group_layouts,
})); }));
defer pipeline_layout.release(); defer pipeline_layout.release();
const render = device.createRenderPipeline(&gpu.RenderPipeline.Descriptor{ const render_pipeline = device.createRenderPipeline(&gpu.RenderPipeline.Descriptor{
.fragment = &fragment, .fragment = &fragment,
.layout = pipeline_layout, .layout = pipeline_layout,
.vertex = gpu.VertexState{ .vertex = gpu.VertexState{
@ -321,7 +327,7 @@ pub fn machGfxTextInitPipeline(
}); });
pipeline.value_ptr.* = Pipeline{ pipeline.value_ptr.* = Pipeline{
.render = render, .render = render_pipeline,
.texture_sampler = texture_sampler, .texture_sampler = texture_sampler,
.texture = texture, .texture = texture,
.texture_atlas = texture_atlas, .texture_atlas = texture_atlas,
@ -337,18 +343,13 @@ pub fn machGfxTextInitPipeline(
.glyphs = glyphs, .glyphs = glyphs,
}; };
pipeline.value_ptr.reference(); pipeline.value_ptr.reference();
} }
pub fn deinit(text_mod: *mach.Mod(.mach_gfx_text)) !void { pub fn updated(
for (text_mod.state.pipelines.entries.items(.value)) |*pipeline| pipeline.deinit(text_mod.allocator);
text_mod.state.pipelines.deinit(text_mod.allocator);
}
pub fn machGfxTextUpdated(
engine: *mach.Mod(.engine), engine: *mach.Mod(.engine),
text_mod: *mach.Mod(.mach_gfx_text), text_mod: *mach.Mod(.mach_gfx_text),
pipeline_id: u32, pipeline_id: u32,
) !void { ) !void {
const pipeline = text_mod.state.pipelines.getPtr(pipeline_id).?; const pipeline = text_mod.state.pipelines.getPtr(pipeline_id).?;
const device = engine.state.device; const device = engine.state.device;
@ -496,13 +497,13 @@ pub fn machGfxTextUpdated(
defer command.release(); defer command.release();
engine.state.queue.submit(&[_]*gpu.CommandBuffer{command}); engine.state.queue.submit(&[_]*gpu.CommandBuffer{command});
} }
pub fn machGfxTextPreRender( pub fn preRender(
engine: *mach.Mod(.engine), engine: *mach.Mod(.engine),
text_mod: *mach.Mod(.mach_gfx_text), text_mod: *mach.Mod(.mach_gfx_text),
pipeline_id: u32, pipeline_id: u32,
) !void { ) !void {
const pipeline = text_mod.state.pipelines.get(pipeline_id).?; const pipeline = text_mod.state.pipelines.get(pipeline_id).?;
// Update uniform buffer // Update uniform buffer
@ -524,13 +525,13 @@ pub fn machGfxTextPreRender(
}; };
engine.state.encoder.writeBuffer(pipeline.uniforms, 0, &[_]Uniforms{uniforms}); engine.state.encoder.writeBuffer(pipeline.uniforms, 0, &[_]Uniforms{uniforms});
} }
pub fn machGfxTextRender( pub fn render(
engine: *mach.Mod(.engine), engine: *mach.Mod(.engine),
text_mod: *mach.Mod(.mach_gfx_text), text_mod: *mach.Mod(.mach_gfx_text),
pipeline_id: u32, pipeline_id: u32,
) !void { ) !void {
const pipeline = text_mod.state.pipelines.get(pipeline_id).?; const pipeline = text_mod.state.pipelines.get(pipeline_id).?;
// Draw the text batch // Draw the text batch
@ -540,4 +541,5 @@ pub fn machGfxTextRender(
// TODO: remove dynamic offsets? // TODO: remove dynamic offsets?
pass.setBindGroup(0, pipeline.bind_group, &.{}); pass.setBindGroup(0, pipeline.bind_group, &.{});
pass.draw(total_vertices, 1, 0, 0); pass.draw(total_vertices, 1, 0, 0);
} }
};