all: refactor: cleanup module structure
Signed-off-by: Stephen Gutekanst <stephen@hexops.com>
This commit is contained in:
parent
52c4eb5d74
commit
c16cddd250
7 changed files with 88 additions and 67 deletions
|
|
@ -15,7 +15,7 @@ const Mat4x4 = math.Mat4x4;
|
||||||
/// Internal state
|
/// Internal state
|
||||||
pipelines: std.AutoArrayHashMapUnmanaged(u32, Pipeline),
|
pipelines: std.AutoArrayHashMapUnmanaged(u32, Pipeline),
|
||||||
|
|
||||||
pub const name = .engine_sprite2d;
|
pub const name = .mach_gfx_sprite;
|
||||||
|
|
||||||
pub const components = struct {
|
pub const components = struct {
|
||||||
/// The ID of the pipeline this sprite belongs to. By default, zero.
|
/// The ID of the pipeline this sprite belongs to. By default, zero.
|
||||||
|
|
@ -120,23 +120,23 @@ pub const PipelineOptions = struct {
|
||||||
pipeline_layout: ?*gpu.PipelineLayout = null,
|
pipeline_layout: ?*gpu.PipelineLayout = null,
|
||||||
};
|
};
|
||||||
|
|
||||||
pub fn engineSprite2dInit(
|
pub fn machGfxSpriteInit(
|
||||||
sprite2d: *mach.Mod(.engine_sprite2d),
|
sprite_mod: *mach.Mod(.mach_gfx_sprite),
|
||||||
) !void {
|
) !void {
|
||||||
sprite2d.state = .{
|
sprite_mod.state = .{
|
||||||
// TODO: struct default value initializers don't work
|
// TODO: struct default value initializers don't work
|
||||||
.pipelines = .{},
|
.pipelines = .{},
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn engineSprite2dInitPipeline(
|
pub fn machGfxSpriteInitPipeline(
|
||||||
engine: *mach.Mod(.engine),
|
engine: *mach.Mod(.engine),
|
||||||
sprite2d: *mach.Mod(.engine_sprite2d),
|
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 sprite2d.state.pipelines.getOrPut(engine.allocator, opt.pipeline);
|
const pipeline = try sprite_mod.state.pipelines.getOrPut(engine.allocator, opt.pipeline);
|
||||||
if (pipeline.found_existing) {
|
if (pipeline.found_existing) {
|
||||||
pipeline.value_ptr.*.deinit();
|
pipeline.value_ptr.*.deinit();
|
||||||
}
|
}
|
||||||
|
|
@ -224,7 +224,7 @@ pub fn engineSprite2dInitPipeline(
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
const shader_module = opt.shader orelse device.createShaderModuleWGSL("sprite2d.wgsl", @embedFile("sprite2d.wgsl"));
|
const shader_module = opt.shader orelse device.createShaderModuleWGSL("sprite.wgsl", @embedFile("sprite.wgsl"));
|
||||||
defer shader_module.release();
|
defer shader_module.release();
|
||||||
|
|
||||||
const color_target = opt.color_target_state orelse gpu.ColorTargetState{
|
const color_target = opt.color_target_state orelse gpu.ColorTargetState{
|
||||||
|
|
@ -269,23 +269,23 @@ pub fn engineSprite2dInitPipeline(
|
||||||
pipeline.value_ptr.reference();
|
pipeline.value_ptr.reference();
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn deinit(sprite2d: *mach.Mod(.engine_sprite2d)) !void {
|
pub fn deinit(sprite_mod: *mach.Mod(.mach_gfx_sprite)) !void {
|
||||||
for (sprite2d.state.pipelines.entries.items(.value)) |*pipeline| pipeline.deinit();
|
for (sprite_mod.state.pipelines.entries.items(.value)) |*pipeline| pipeline.deinit();
|
||||||
sprite2d.state.pipelines.deinit(sprite2d.allocator);
|
sprite_mod.state.pipelines.deinit(sprite_mod.allocator);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn engineSprite2dUpdated(
|
pub fn machGfxSpriteUpdated(
|
||||||
engine: *mach.Mod(.engine),
|
engine: *mach.Mod(.engine),
|
||||||
sprite2d: *mach.Mod(.engine_sprite2d),
|
sprite_mod: *mach.Mod(.mach_gfx_sprite),
|
||||||
pipeline_id: u32,
|
pipeline_id: u32,
|
||||||
) !void {
|
) !void {
|
||||||
const pipeline = sprite2d.state.pipelines.getPtr(pipeline_id).?;
|
const pipeline = sprite_mod.state.pipelines.getPtr(pipeline_id).?;
|
||||||
const device = engine.state.device;
|
const device = engine.state.device;
|
||||||
|
|
||||||
// TODO: make sure these entities only belong to the given pipeline
|
// TODO: make sure these entities only belong to the given pipeline
|
||||||
// we need a better tagging mechanism
|
// we need a better tagging mechanism
|
||||||
var archetypes_iter = engine.entities.query(.{ .all = &.{
|
var archetypes_iter = engine.entities.query(.{ .all = &.{
|
||||||
.{ .engine_sprite2d = &.{
|
.{ .mach_gfx_sprite = &.{
|
||||||
.uv_transform,
|
.uv_transform,
|
||||||
.transform,
|
.transform,
|
||||||
.size,
|
.size,
|
||||||
|
|
@ -301,9 +301,9 @@ pub fn engineSprite2dUpdated(
|
||||||
var uv_transforms_offset: usize = 0;
|
var uv_transforms_offset: usize = 0;
|
||||||
var sizes_offset: usize = 0;
|
var sizes_offset: usize = 0;
|
||||||
while (archetypes_iter.next()) |archetype| {
|
while (archetypes_iter.next()) |archetype| {
|
||||||
var transforms = archetype.slice(.engine_sprite2d, .transform);
|
var transforms = archetype.slice(.mach_gfx_sprite, .transform);
|
||||||
var uv_transforms = archetype.slice(.engine_sprite2d, .uv_transform);
|
var uv_transforms = archetype.slice(.mach_gfx_sprite, .uv_transform);
|
||||||
var sizes = archetype.slice(.engine_sprite2d, .size);
|
var sizes = archetype.slice(.mach_gfx_sprite, .size);
|
||||||
|
|
||||||
// TODO: confirm the lifetime of these slices is OK for writeBuffer, how long do they need
|
// TODO: confirm the lifetime of these slices is OK for writeBuffer, how long do they need
|
||||||
// to live?
|
// to live?
|
||||||
|
|
@ -323,12 +323,12 @@ pub fn engineSprite2dUpdated(
|
||||||
engine.state.queue.submit(&[_]*gpu.CommandBuffer{command});
|
engine.state.queue.submit(&[_]*gpu.CommandBuffer{command});
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn engineSprite2dPreRender(
|
pub fn machGfxSpritePreRender(
|
||||||
engine: *mach.Mod(.engine),
|
engine: *mach.Mod(.engine),
|
||||||
sprite2d: *mach.Mod(.engine_sprite2d),
|
sprite_mod: *mach.Mod(.mach_gfx_sprite),
|
||||||
pipeline_id: u32,
|
pipeline_id: u32,
|
||||||
) !void {
|
) !void {
|
||||||
const pipeline = sprite2d.state.pipelines.get(pipeline_id).?;
|
const pipeline = sprite_mod.state.pipelines.get(pipeline_id).?;
|
||||||
|
|
||||||
// Update uniform buffer
|
// Update uniform buffer
|
||||||
const ortho = Mat4x4.ortho(
|
const ortho = Mat4x4.ortho(
|
||||||
|
|
@ -351,12 +351,12 @@ pub fn engineSprite2dPreRender(
|
||||||
engine.state.encoder.writeBuffer(pipeline.uniforms, 0, &[_]Uniforms{uniforms});
|
engine.state.encoder.writeBuffer(pipeline.uniforms, 0, &[_]Uniforms{uniforms});
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn engineSprite2dRender(
|
pub fn machGfxSpriteRender(
|
||||||
engine: *mach.Mod(.engine),
|
engine: *mach.Mod(.engine),
|
||||||
sprite2d: *mach.Mod(.engine_sprite2d),
|
sprite_mod: *mach.Mod(.mach_gfx_sprite),
|
||||||
pipeline_id: u32,
|
pipeline_id: u32,
|
||||||
) !void {
|
) !void {
|
||||||
const pipeline = sprite2d.state.pipelines.get(pipeline_id).?;
|
const pipeline = sprite_mod.state.pipelines.get(pipeline_id).?;
|
||||||
|
|
||||||
// Draw the sprite batch
|
// Draw the sprite batch
|
||||||
const pass = engine.state.pass;
|
const pass = engine.state.pass;
|
||||||
|
|
@ -17,7 +17,7 @@ const Mat4x4 = math.Mat4x4;
|
||||||
/// Internal state
|
/// Internal state
|
||||||
pipelines: std.AutoArrayHashMapUnmanaged(u32, Pipeline),
|
pipelines: std.AutoArrayHashMapUnmanaged(u32, Pipeline),
|
||||||
|
|
||||||
pub const name = .engine_text2d;
|
pub const name = .mach_gfx_text;
|
||||||
|
|
||||||
/// Converts points to pixels. e.g. a 12pt font size `12.0 * points_to_pixels == 16.0`
|
/// Converts points to pixels. e.g. a 12pt font size `12.0 * points_to_pixels == 16.0`
|
||||||
pub const points_to_pixels = 4.0 / 3.0;
|
pub const points_to_pixels = 4.0 / 3.0;
|
||||||
|
|
@ -160,23 +160,23 @@ pub const PipelineOptions = struct {
|
||||||
pipeline_layout: ?*gpu.PipelineLayout = null,
|
pipeline_layout: ?*gpu.PipelineLayout = null,
|
||||||
};
|
};
|
||||||
|
|
||||||
pub fn engineText2dInit(
|
pub fn machGfxTextInit(
|
||||||
text2d: *mach.Mod(.engine_text2d),
|
text_mod: *mach.Mod(.mach_gfx_text),
|
||||||
) !void {
|
) !void {
|
||||||
text2d.state = .{
|
text_mod.state = .{
|
||||||
// TODO: struct default value initializers don't work
|
// TODO: struct default value initializers don't work
|
||||||
.pipelines = .{},
|
.pipelines = .{},
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn engineText2dInitPipeline(
|
pub fn machGfxTextInitPipeline(
|
||||||
engine: *mach.Mod(.engine),
|
engine: *mach.Mod(.engine),
|
||||||
text2d: *mach.Mod(.engine_text2d),
|
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 text2d.state.pipelines.getOrPut(engine.allocator, opt.pipeline);
|
const pipeline = try text_mod.state.pipelines.getOrPut(engine.allocator, opt.pipeline);
|
||||||
if (pipeline.found_existing) {
|
if (pipeline.found_existing) {
|
||||||
pipeline.value_ptr.*.deinit(engine.allocator);
|
pipeline.value_ptr.*.deinit(engine.allocator);
|
||||||
}
|
}
|
||||||
|
|
@ -282,7 +282,7 @@ pub fn engineText2dInitPipeline(
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
const shader_module = opt.shader orelse device.createShaderModuleWGSL("text2d.wgsl", @embedFile("text2d.wgsl"));
|
const shader_module = opt.shader orelse device.createShaderModuleWGSL("text.wgsl", @embedFile("text.wgsl"));
|
||||||
defer shader_module.release();
|
defer shader_module.release();
|
||||||
|
|
||||||
const color_target = opt.color_target_state orelse gpu.ColorTargetState{
|
const color_target = opt.color_target_state orelse gpu.ColorTargetState{
|
||||||
|
|
@ -329,23 +329,23 @@ pub fn engineText2dInitPipeline(
|
||||||
pipeline.value_ptr.reference();
|
pipeline.value_ptr.reference();
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn deinit(text2d: *mach.Mod(.engine_text2d)) !void {
|
pub fn deinit(text_mod: *mach.Mod(.mach_gfx_text)) !void {
|
||||||
for (text2d.state.pipelines.entries.items(.value)) |*pipeline| pipeline.deinit(text2d.allocator);
|
for (text_mod.state.pipelines.entries.items(.value)) |*pipeline| pipeline.deinit(text_mod.allocator);
|
||||||
text2d.state.pipelines.deinit(text2d.allocator);
|
text_mod.state.pipelines.deinit(text_mod.allocator);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn engineText2dUpdated(
|
pub fn machGfxTextUpdated(
|
||||||
engine: *mach.Mod(.engine),
|
engine: *mach.Mod(.engine),
|
||||||
text2d: *mach.Mod(.engine_text2d),
|
text_mod: *mach.Mod(.mach_gfx_text),
|
||||||
pipeline_id: u32,
|
pipeline_id: u32,
|
||||||
) !void {
|
) !void {
|
||||||
const pipeline = text2d.state.pipelines.getPtr(pipeline_id).?;
|
const pipeline = text_mod.state.pipelines.getPtr(pipeline_id).?;
|
||||||
const device = engine.state.device;
|
const device = engine.state.device;
|
||||||
|
|
||||||
// TODO: make sure these entities only belong to the given pipeline
|
// TODO: make sure these entities only belong to the given pipeline
|
||||||
// we need a better tagging mechanism
|
// we need a better tagging mechanism
|
||||||
var archetypes_iter = engine.entities.query(.{ .all = &.{
|
var archetypes_iter = engine.entities.query(.{ .all = &.{
|
||||||
.{ .engine_text2d = &.{
|
.{ .mach_gfx_text = &.{
|
||||||
.pipeline,
|
.pipeline,
|
||||||
.transform,
|
.transform,
|
||||||
.text,
|
.text,
|
||||||
|
|
@ -365,8 +365,8 @@ pub fn engineText2dUpdated(
|
||||||
var colors_offset: usize = 0;
|
var colors_offset: usize = 0;
|
||||||
var texture_update = false;
|
var texture_update = false;
|
||||||
while (archetypes_iter.next()) |archetype| {
|
while (archetypes_iter.next()) |archetype| {
|
||||||
var transforms = archetype.slice(.engine_text2d, .transform);
|
var transforms = archetype.slice(.mach_gfx_text, .transform);
|
||||||
var colors = archetype.slice(.engine_text2d, .color);
|
var colors = archetype.slice(.mach_gfx_text, .color);
|
||||||
|
|
||||||
// TODO: confirm the lifetime of these slices is OK for writeBuffer, how long do they need
|
// TODO: confirm the lifetime of these slices is OK for writeBuffer, how long do they need
|
||||||
// to live?
|
// to live?
|
||||||
|
|
@ -381,9 +381,9 @@ pub fn engineText2dUpdated(
|
||||||
// TODO: this is very expensive and shouldn't be done here, should be done only on detected
|
// TODO: this is very expensive and shouldn't be done here, should be done only on detected
|
||||||
// text change.
|
// text change.
|
||||||
const px_density = 2.0;
|
const px_density = 2.0;
|
||||||
var fonts = archetype.slice(.engine_text2d, .font);
|
var fonts = archetype.slice(.mach_gfx_text, .font);
|
||||||
var font_sizes = archetype.slice(.engine_text2d, .font_size);
|
var font_sizes = archetype.slice(.mach_gfx_text, .font_size);
|
||||||
var texts = archetype.slice(.engine_text2d, .text);
|
var texts = archetype.slice(.mach_gfx_text, .text);
|
||||||
for (fonts, font_sizes, texts) |font, font_size, text| {
|
for (fonts, font_sizes, texts) |font, font_size, text| {
|
||||||
var offset_x: f32 = 0.0;
|
var offset_x: f32 = 0.0;
|
||||||
var offset_y: f32 = 0.0;
|
var offset_y: f32 = 0.0;
|
||||||
|
|
@ -476,12 +476,12 @@ pub fn engineText2dUpdated(
|
||||||
engine.state.queue.submit(&[_]*gpu.CommandBuffer{command});
|
engine.state.queue.submit(&[_]*gpu.CommandBuffer{command});
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn engineText2dPreRender(
|
pub fn machGfxTextPreRender(
|
||||||
engine: *mach.Mod(.engine),
|
engine: *mach.Mod(.engine),
|
||||||
text2d: *mach.Mod(.engine_text2d),
|
text_mod: *mach.Mod(.mach_gfx_text),
|
||||||
pipeline_id: u32,
|
pipeline_id: u32,
|
||||||
) !void {
|
) !void {
|
||||||
const pipeline = text2d.state.pipelines.get(pipeline_id).?;
|
const pipeline = text_mod.state.pipelines.get(pipeline_id).?;
|
||||||
|
|
||||||
// Update uniform buffer
|
// Update uniform buffer
|
||||||
const ortho = Mat4x4.ortho(
|
const ortho = Mat4x4.ortho(
|
||||||
|
|
@ -504,12 +504,12 @@ pub fn engineText2dPreRender(
|
||||||
engine.state.encoder.writeBuffer(pipeline.uniforms, 0, &[_]Uniforms{uniforms});
|
engine.state.encoder.writeBuffer(pipeline.uniforms, 0, &[_]Uniforms{uniforms});
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn engineText2dRender(
|
pub fn machGfxTextRender(
|
||||||
engine: *mach.Mod(.engine),
|
engine: *mach.Mod(.engine),
|
||||||
text2d: *mach.Mod(.engine_text2d),
|
text_mod: *mach.Mod(.mach_gfx_text),
|
||||||
pipeline_id: u32,
|
pipeline_id: u32,
|
||||||
) !void {
|
) !void {
|
||||||
const pipeline = text2d.state.pipelines.get(pipeline_id).?;
|
const pipeline = text_mod.state.pipelines.get(pipeline_id).?;
|
||||||
|
|
||||||
// Draw the text batch
|
// Draw the text batch
|
||||||
const pass = engine.state.pass;
|
const pass = engine.state.pass;
|
||||||
20
src/gfx/main.zig
Normal file
20
src/gfx/main.zig
Normal file
|
|
@ -0,0 +1,20 @@
|
||||||
|
pub const util = @import("util.zig");
|
||||||
|
pub const Sprite = @import("Sprite.zig");
|
||||||
|
pub const Text = @import("Text.zig");
|
||||||
|
pub const FontRenderer = @import("font.zig").FontRenderer;
|
||||||
|
pub const RGBA32 = @import("font.zig").RGBA32;
|
||||||
|
pub const Glyph = @import("font.zig").Glyph;
|
||||||
|
pub const GlyphMetrics = @import("font.zig").GlyphMetrics;
|
||||||
|
|
||||||
|
test {
|
||||||
|
const std = @import("std");
|
||||||
|
// TODO: refactor code so we can use this here:
|
||||||
|
// std.testing.refAllDeclsRecursive(@This());
|
||||||
|
std.testing.refAllDeclsRecursive(util);
|
||||||
|
// std.testing.refAllDeclsRecursive(Sprite);
|
||||||
|
// std.testing.refAllDeclsRecursive(Text);
|
||||||
|
std.testing.refAllDeclsRecursive(FontRenderer);
|
||||||
|
std.testing.refAllDeclsRecursive(RGBA32);
|
||||||
|
std.testing.refAllDeclsRecursive(Glyph);
|
||||||
|
std.testing.refAllDeclsRecursive(GlyphMetrics);
|
||||||
|
}
|
||||||
35
src/main.zig
35
src/main.zig
|
|
@ -1,38 +1,39 @@
|
||||||
|
// Core re-exports
|
||||||
pub const core = @import("mach-core");
|
pub const core = @import("mach-core");
|
||||||
pub const GPUInterface = core.GPUInterface;
|
pub const GPUInterface = core.GPUInterface;
|
||||||
|
pub const Timer = core.Timer;
|
||||||
pub const scope_levels = core.scope_levels;
|
pub const scope_levels = core.scope_levels;
|
||||||
pub const log_level = core.log_level;
|
pub const log_level = core.log_level;
|
||||||
pub const Timer = core.Timer;
|
|
||||||
pub const gpu = core.gpu;
|
|
||||||
|
|
||||||
|
// Mach packages
|
||||||
|
pub const gpu = core.gpu;
|
||||||
pub const sysjs = @import("sysjs");
|
pub const sysjs = @import("sysjs");
|
||||||
pub const ecs = @import("mach-ecs");
|
pub const ecs = @import("mach-ecs");
|
||||||
pub const sysaudio = @import("mach-sysaudio");
|
pub const sysaudio = @import("mach-sysaudio");
|
||||||
pub const gfx = @import("gfx/util.zig");
|
|
||||||
pub const gfx2d = struct {
|
// Mach standard library
|
||||||
pub const Sprite2D = @import("gfx2d/Sprite2D.zig");
|
pub const Atlas = @import("atlas/Atlas.zig");
|
||||||
pub const Text2D = @import("gfx2d/Text2D.zig");
|
pub const gfx = @import("gfx/main.zig");
|
||||||
pub const FontRenderer = @import("gfx2d/font.zig").FontRenderer;
|
|
||||||
pub const RGBA32 = @import("gfx2d/font.zig").RGBA32;
|
|
||||||
pub const Glyph = @import("gfx2d/font.zig").Glyph;
|
|
||||||
pub const GlyphMetrics = @import("gfx2d/font.zig").GlyphMetrics;
|
|
||||||
};
|
|
||||||
pub const math = @import("math/main.zig");
|
pub const math = @import("math/main.zig");
|
||||||
pub const testing = @import("testing.zig");
|
pub const testing = @import("testing.zig");
|
||||||
|
|
||||||
pub const Atlas = @import("atlas/Atlas.zig");
|
|
||||||
|
|
||||||
// Engine exports
|
// Engine exports
|
||||||
pub const App = @import("engine.zig").App;
|
pub const App = @import("engine.zig").App;
|
||||||
pub const Engine = @import("engine.zig").Engine;
|
pub const Engine = @import("engine.zig").Engine;
|
||||||
pub const World = @import("engine.zig").World;
|
pub const World = @import("engine.zig").World;
|
||||||
pub const Mod = World.Mod;
|
pub const Mod = World.Mod;
|
||||||
|
|
||||||
const std = @import("std");
|
|
||||||
|
|
||||||
test {
|
test {
|
||||||
std.testing.refAllDeclsRecursive(gfx);
|
const std = @import("std");
|
||||||
|
// TODO: refactor code so we can use this here:
|
||||||
|
// std.testing.refAllDeclsRecursive(@This());
|
||||||
|
_ = core;
|
||||||
|
_ = gpu;
|
||||||
|
_ = ecs;
|
||||||
|
_ = sysaudio;
|
||||||
|
_ = gfx;
|
||||||
|
_ = math;
|
||||||
|
_ = testing;
|
||||||
std.testing.refAllDeclsRecursive(Atlas);
|
std.testing.refAllDeclsRecursive(Atlas);
|
||||||
std.testing.refAllDeclsRecursive(math);
|
std.testing.refAllDeclsRecursive(math);
|
||||||
_ = ecs;
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue