From c16cddd2509092f9201de08c8eed97c110526bd0 Mon Sep 17 00:00:00 2001 From: Stephen Gutekanst Date: Fri, 22 Sep 2023 08:24:16 -0700 Subject: [PATCH] all: refactor: cleanup module structure Signed-off-by: Stephen Gutekanst --- src/{gfx2d/Sprite2D.zig => gfx/Sprite.zig} | 48 +++++++++--------- src/{gfx2d/Text2D.zig => gfx/Text.zig} | 52 ++++++++++---------- src/{gfx2d => gfx}/font.zig | 0 src/gfx/main.zig | 20 ++++++++ src/{gfx2d/sprite2d.wgsl => gfx/sprite.wgsl} | 0 src/{gfx2d/text2d.wgsl => gfx/text.wgsl} | 0 src/main.zig | 35 ++++++------- 7 files changed, 88 insertions(+), 67 deletions(-) rename src/{gfx2d/Sprite2D.zig => gfx/Sprite.zig} (90%) rename src/{gfx2d/Text2D.zig => gfx/Text.zig} (93%) rename src/{gfx2d => gfx}/font.zig (100%) create mode 100644 src/gfx/main.zig rename src/{gfx2d/sprite2d.wgsl => gfx/sprite.wgsl} (100%) rename src/{gfx2d/text2d.wgsl => gfx/text.wgsl} (100%) diff --git a/src/gfx2d/Sprite2D.zig b/src/gfx/Sprite.zig similarity index 90% rename from src/gfx2d/Sprite2D.zig rename to src/gfx/Sprite.zig index 7119dca1..87959a69 100644 --- a/src/gfx2d/Sprite2D.zig +++ b/src/gfx/Sprite.zig @@ -15,7 +15,7 @@ const Mat4x4 = math.Mat4x4; /// Internal state pipelines: std.AutoArrayHashMapUnmanaged(u32, Pipeline), -pub const name = .engine_sprite2d; +pub const name = .mach_gfx_sprite; pub const components = struct { /// 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, }; -pub fn engineSprite2dInit( - sprite2d: *mach.Mod(.engine_sprite2d), +pub fn machGfxSpriteInit( + sprite_mod: *mach.Mod(.mach_gfx_sprite), ) !void { - sprite2d.state = .{ + sprite_mod.state = .{ // TODO: struct default value initializers don't work .pipelines = .{}, }; } -pub fn engineSprite2dInitPipeline( +pub fn machGfxSpriteInitPipeline( engine: *mach.Mod(.engine), - sprite2d: *mach.Mod(.engine_sprite2d), + sprite_mod: *mach.Mod(.mach_gfx_sprite), opt: PipelineOptions, ) !void { 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) { 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(); const color_target = opt.color_target_state orelse gpu.ColorTargetState{ @@ -269,23 +269,23 @@ pub fn engineSprite2dInitPipeline( pipeline.value_ptr.reference(); } -pub fn deinit(sprite2d: *mach.Mod(.engine_sprite2d)) !void { - for (sprite2d.state.pipelines.entries.items(.value)) |*pipeline| pipeline.deinit(); - sprite2d.state.pipelines.deinit(sprite2d.allocator); +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 fn engineSprite2dUpdated( +pub fn machGfxSpriteUpdated( engine: *mach.Mod(.engine), - sprite2d: *mach.Mod(.engine_sprite2d), + sprite_mod: *mach.Mod(.mach_gfx_sprite), pipeline_id: u32, ) !void { - const pipeline = sprite2d.state.pipelines.getPtr(pipeline_id).?; + const pipeline = sprite_mod.state.pipelines.getPtr(pipeline_id).?; const device = engine.state.device; // TODO: make sure these entities only belong to the given pipeline // we need a better tagging mechanism var archetypes_iter = engine.entities.query(.{ .all = &.{ - .{ .engine_sprite2d = &.{ + .{ .mach_gfx_sprite = &.{ .uv_transform, .transform, .size, @@ -301,9 +301,9 @@ pub fn engineSprite2dUpdated( var uv_transforms_offset: usize = 0; var sizes_offset: usize = 0; while (archetypes_iter.next()) |archetype| { - var transforms = archetype.slice(.engine_sprite2d, .transform); - var uv_transforms = archetype.slice(.engine_sprite2d, .uv_transform); - var sizes = archetype.slice(.engine_sprite2d, .size); + var transforms = archetype.slice(.mach_gfx_sprite, .transform); + var uv_transforms = archetype.slice(.mach_gfx_sprite, .uv_transform); + var sizes = archetype.slice(.mach_gfx_sprite, .size); // TODO: confirm the lifetime of these slices is OK for writeBuffer, how long do they need // to live? @@ -323,12 +323,12 @@ pub fn engineSprite2dUpdated( engine.state.queue.submit(&[_]*gpu.CommandBuffer{command}); } -pub fn engineSprite2dPreRender( +pub fn machGfxSpritePreRender( engine: *mach.Mod(.engine), - sprite2d: *mach.Mod(.engine_sprite2d), + sprite_mod: *mach.Mod(.mach_gfx_sprite), pipeline_id: u32, ) !void { - const pipeline = sprite2d.state.pipelines.get(pipeline_id).?; + const pipeline = sprite_mod.state.pipelines.get(pipeline_id).?; // Update uniform buffer const ortho = Mat4x4.ortho( @@ -351,12 +351,12 @@ pub fn engineSprite2dPreRender( engine.state.encoder.writeBuffer(pipeline.uniforms, 0, &[_]Uniforms{uniforms}); } -pub fn engineSprite2dRender( +pub fn machGfxSpriteRender( engine: *mach.Mod(.engine), - sprite2d: *mach.Mod(.engine_sprite2d), + sprite_mod: *mach.Mod(.mach_gfx_sprite), pipeline_id: u32, ) !void { - const pipeline = sprite2d.state.pipelines.get(pipeline_id).?; + const pipeline = sprite_mod.state.pipelines.get(pipeline_id).?; // Draw the sprite batch const pass = engine.state.pass; diff --git a/src/gfx2d/Text2D.zig b/src/gfx/Text.zig similarity index 93% rename from src/gfx2d/Text2D.zig rename to src/gfx/Text.zig index 8fcf4f69..79208757 100644 --- a/src/gfx2d/Text2D.zig +++ b/src/gfx/Text.zig @@ -17,7 +17,7 @@ const Mat4x4 = math.Mat4x4; /// Internal state 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` pub const points_to_pixels = 4.0 / 3.0; @@ -160,23 +160,23 @@ pub const PipelineOptions = struct { pipeline_layout: ?*gpu.PipelineLayout = null, }; -pub fn engineText2dInit( - text2d: *mach.Mod(.engine_text2d), +pub fn machGfxTextInit( + text_mod: *mach.Mod(.mach_gfx_text), ) !void { - text2d.state = .{ + text_mod.state = .{ // TODO: struct default value initializers don't work .pipelines = .{}, }; } -pub fn engineText2dInitPipeline( +pub fn machGfxTextInitPipeline( engine: *mach.Mod(.engine), - text2d: *mach.Mod(.engine_text2d), + text_mod: *mach.Mod(.mach_gfx_text), opt: PipelineOptions, ) !void { 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) { 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(); const color_target = opt.color_target_state orelse gpu.ColorTargetState{ @@ -329,23 +329,23 @@ pub fn engineText2dInitPipeline( pipeline.value_ptr.reference(); } -pub fn deinit(text2d: *mach.Mod(.engine_text2d)) !void { - for (text2d.state.pipelines.entries.items(.value)) |*pipeline| pipeline.deinit(text2d.allocator); - text2d.state.pipelines.deinit(text2d.allocator); +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 fn engineText2dUpdated( +pub fn machGfxTextUpdated( engine: *mach.Mod(.engine), - text2d: *mach.Mod(.engine_text2d), + text_mod: *mach.Mod(.mach_gfx_text), pipeline_id: u32, ) !void { - const pipeline = text2d.state.pipelines.getPtr(pipeline_id).?; + const pipeline = text_mod.state.pipelines.getPtr(pipeline_id).?; const device = engine.state.device; // TODO: make sure these entities only belong to the given pipeline // we need a better tagging mechanism var archetypes_iter = engine.entities.query(.{ .all = &.{ - .{ .engine_text2d = &.{ + .{ .mach_gfx_text = &.{ .pipeline, .transform, .text, @@ -365,8 +365,8 @@ pub fn engineText2dUpdated( var colors_offset: usize = 0; var texture_update = false; while (archetypes_iter.next()) |archetype| { - var transforms = archetype.slice(.engine_text2d, .transform); - var colors = archetype.slice(.engine_text2d, .color); + var transforms = archetype.slice(.mach_gfx_text, .transform); + var colors = archetype.slice(.mach_gfx_text, .color); // TODO: confirm the lifetime of these slices is OK for writeBuffer, how long do they need // 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 // text change. const px_density = 2.0; - var fonts = archetype.slice(.engine_text2d, .font); - var font_sizes = archetype.slice(.engine_text2d, .font_size); - var texts = archetype.slice(.engine_text2d, .text); + var fonts = archetype.slice(.mach_gfx_text, .font); + var font_sizes = archetype.slice(.mach_gfx_text, .font_size); + var texts = archetype.slice(.mach_gfx_text, .text); for (fonts, font_sizes, texts) |font, font_size, text| { var offset_x: f32 = 0.0; var offset_y: f32 = 0.0; @@ -476,12 +476,12 @@ pub fn engineText2dUpdated( engine.state.queue.submit(&[_]*gpu.CommandBuffer{command}); } -pub fn engineText2dPreRender( +pub fn machGfxTextPreRender( engine: *mach.Mod(.engine), - text2d: *mach.Mod(.engine_text2d), + text_mod: *mach.Mod(.mach_gfx_text), pipeline_id: u32, ) !void { - const pipeline = text2d.state.pipelines.get(pipeline_id).?; + const pipeline = text_mod.state.pipelines.get(pipeline_id).?; // Update uniform buffer const ortho = Mat4x4.ortho( @@ -504,12 +504,12 @@ pub fn engineText2dPreRender( engine.state.encoder.writeBuffer(pipeline.uniforms, 0, &[_]Uniforms{uniforms}); } -pub fn engineText2dRender( +pub fn machGfxTextRender( engine: *mach.Mod(.engine), - text2d: *mach.Mod(.engine_text2d), + text_mod: *mach.Mod(.mach_gfx_text), pipeline_id: u32, ) !void { - const pipeline = text2d.state.pipelines.get(pipeline_id).?; + const pipeline = text_mod.state.pipelines.get(pipeline_id).?; // Draw the text batch const pass = engine.state.pass; diff --git a/src/gfx2d/font.zig b/src/gfx/font.zig similarity index 100% rename from src/gfx2d/font.zig rename to src/gfx/font.zig diff --git a/src/gfx/main.zig b/src/gfx/main.zig new file mode 100644 index 00000000..fa5cdfc8 --- /dev/null +++ b/src/gfx/main.zig @@ -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); +} diff --git a/src/gfx2d/sprite2d.wgsl b/src/gfx/sprite.wgsl similarity index 100% rename from src/gfx2d/sprite2d.wgsl rename to src/gfx/sprite.wgsl diff --git a/src/gfx2d/text2d.wgsl b/src/gfx/text.wgsl similarity index 100% rename from src/gfx2d/text2d.wgsl rename to src/gfx/text.wgsl diff --git a/src/main.zig b/src/main.zig index 47951ddb..e6fc97d3 100644 --- a/src/main.zig +++ b/src/main.zig @@ -1,38 +1,39 @@ +// Core re-exports pub const core = @import("mach-core"); pub const GPUInterface = core.GPUInterface; +pub const Timer = core.Timer; pub const scope_levels = core.scope_levels; 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 ecs = @import("mach-ecs"); pub const sysaudio = @import("mach-sysaudio"); -pub const gfx = @import("gfx/util.zig"); -pub const gfx2d = struct { - pub const Sprite2D = @import("gfx2d/Sprite2D.zig"); - pub const Text2D = @import("gfx2d/Text2D.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; -}; + +// Mach standard library +pub const Atlas = @import("atlas/Atlas.zig"); +pub const gfx = @import("gfx/main.zig"); pub const math = @import("math/main.zig"); pub const testing = @import("testing.zig"); -pub const Atlas = @import("atlas/Atlas.zig"); - // Engine exports pub const App = @import("engine.zig").App; pub const Engine = @import("engine.zig").Engine; pub const World = @import("engine.zig").World; pub const Mod = World.Mod; -const std = @import("std"); - 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(math); - _ = ecs; }