Skip to content

Commit

Permalink
all: refactor: cleanup module structure
Browse files Browse the repository at this point in the history
Signed-off-by: Stephen Gutekanst <[email protected]>
  • Loading branch information
slimsag committed Sep 22, 2023
1 parent 52c4eb5 commit c16cddd
Show file tree
Hide file tree
Showing 7 changed files with 88 additions and 67 deletions.
48 changes: 24 additions & 24 deletions src/gfx2d/Sprite2D.zig → src/gfx/Sprite.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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();
}
Expand Down Expand Up @@ -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{
Expand Down Expand Up @@ -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,
Expand All @@ -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?
Expand All @@ -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(
Expand All @@ -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;
Expand Down
52 changes: 26 additions & 26 deletions src/gfx2d/Text2D.zig → src/gfx/Text.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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{
Expand Down Expand Up @@ -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,
Expand All @@ -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?
Expand All @@ -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;
Expand Down Expand Up @@ -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(
Expand All @@ -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;
Expand Down
File renamed without changes.
20 changes: 20 additions & 0 deletions src/gfx/main.zig
Original file line number Diff line number Diff line change
@@ -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);
}
File renamed without changes.
File renamed without changes.
35 changes: 18 additions & 17 deletions src/main.zig
Original file line number Diff line number Diff line change
@@ -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;
};
pub const math = @import("math/main.zig");
pub const testing = @import("testing.zig");

// 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");

// 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;
}

0 comments on commit c16cddd

Please sign in to comment.