From c151222011b206653238eb816ae83ecd3279fb1c Mon Sep 17 00:00:00 2001 From: Keith Chambers Date: Wed, 30 Nov 2022 11:27:00 -0500 Subject: [PATCH] mach: VertexWriter fixes (#633) * mach: VertexWriter fixes * mach: Reference decls in gfx * remove comptime block from refAllDecls as is redundant --- src/gfx/util.zig | 23 ++++++++++------------- src/main.zig | 2 ++ 2 files changed, 12 insertions(+), 13 deletions(-) diff --git a/src/gfx/util.zig b/src/gfx/util.zig index 1fa51c1c..0a017b5d 100644 --- a/src/gfx/util.zig +++ b/src/gfx/util.zig @@ -3,7 +3,7 @@ const std = @import("std"); /// Vertex writer manages the placement of vertices by tracking which are unique. If a duplicate vertex is added /// with `put`, only it's index will be written to the index buffer. /// `IndexType` should match the integer type used for the index buffer -fn VertexWriter(comptime VertexType: type, comptime IndexType: type) type { +pub fn VertexWriter(comptime VertexType: type, comptime IndexType: type) type { return struct { const MapEntry = struct { packed_index: IndexType = null_index, @@ -36,7 +36,7 @@ fn VertexWriter(comptime VertexType: type, comptime IndexType: type) type { max_vertex_count: IndexType, ) !@This() { var result: @This() = undefined; - result.vertices = try allocator.alloc(Vertex, max_vertex_count); + result.vertices = try allocator.alloc(VertexType, max_vertex_count); result.indices = try allocator.alloc(IndexType, indices_count); result.sparse_to_packed_map = try allocator.alloc(MapEntry, max_vertex_count); result.next_collision_index = sparse_vertices_count; @@ -46,7 +46,7 @@ fn VertexWriter(comptime VertexType: type, comptime IndexType: type) type { return result; } - pub fn put(self: *@This(), vertex: Vertex, sparse_index: IndexType) void { + pub fn put(self: *@This(), vertex: VertexType, sparse_index: IndexType) void { if (self.sparse_to_packed_map[sparse_index].packed_index == null_index) { // New start of chain, reserve a new packed index and add entry to `index_map` const packed_index = self.next_packed_index; @@ -92,22 +92,19 @@ fn VertexWriter(comptime VertexType: type, comptime IndexType: type) type { return self.indices; } - pub fn vertexBuffer(self: @This()) []Vertex { + pub fn vertexBuffer(self: @This()) []VertexType { return self.vertices[0..self.next_packed_index]; } }; } -const Vec4 = [4]f32; -const Vec3 = [3]f32; -const Vec2 = [2]f32; - -const Vertex = extern struct { - position: Vec3, - normal: Vec3, -}; - test "VertexWriter" { + const Vec3 = [3]f32; + const Vertex = extern struct { + position: Vec3, + normal: Vec3, + }; + const expect = std.testing.expect; const allocator = std.testing.allocator; diff --git a/src/main.zig b/src/main.zig index aaa24593..aa92b073 100644 --- a/src/main.zig +++ b/src/main.zig @@ -9,6 +9,7 @@ pub const ecs = @import("ecs"); pub const sysaudio = @import("sysaudio"); pub const sysjs = @import("sysjs"); pub const earcut = @import("earcut"); +pub const gfx = @import("gfx/util.zig"); // Engine exports pub const App = @import("engine.zig").App; @@ -22,4 +23,5 @@ test { // testing.refAllDeclsRecursive(Core); // testing.refAllDeclsRecursive(Timer); testing.refAllDeclsRecursive(ResourceManager); + testing.refAllDeclsRecursive(gfx); }