mach: VertexWriter fixes (#633)

* mach: VertexWriter fixes
* mach: Reference decls in gfx
* remove comptime block from refAllDecls as is redundant
This commit is contained in:
Keith Chambers 2022-11-30 11:27:00 -05:00 committed by GitHub
parent f331597bc2
commit c151222011
Failed to generate hash of commit
2 changed files with 12 additions and 13 deletions

View file

@ -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 /// 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. /// with `put`, only it's index will be written to the index buffer.
/// `IndexType` should match the integer type used for 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 { return struct {
const MapEntry = struct { const MapEntry = struct {
packed_index: IndexType = null_index, packed_index: IndexType = null_index,
@ -36,7 +36,7 @@ fn VertexWriter(comptime VertexType: type, comptime IndexType: type) type {
max_vertex_count: IndexType, max_vertex_count: IndexType,
) !@This() { ) !@This() {
var result: @This() = undefined; 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.indices = try allocator.alloc(IndexType, indices_count);
result.sparse_to_packed_map = try allocator.alloc(MapEntry, max_vertex_count); result.sparse_to_packed_map = try allocator.alloc(MapEntry, max_vertex_count);
result.next_collision_index = sparse_vertices_count; result.next_collision_index = sparse_vertices_count;
@ -46,7 +46,7 @@ fn VertexWriter(comptime VertexType: type, comptime IndexType: type) type {
return result; 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) { 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` // New start of chain, reserve a new packed index and add entry to `index_map`
const packed_index = self.next_packed_index; const packed_index = self.next_packed_index;
@ -92,22 +92,19 @@ fn VertexWriter(comptime VertexType: type, comptime IndexType: type) type {
return self.indices; return self.indices;
} }
pub fn vertexBuffer(self: @This()) []Vertex { pub fn vertexBuffer(self: @This()) []VertexType {
return self.vertices[0..self.next_packed_index]; 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" { test "VertexWriter" {
const Vec3 = [3]f32;
const Vertex = extern struct {
position: Vec3,
normal: Vec3,
};
const expect = std.testing.expect; const expect = std.testing.expect;
const allocator = std.testing.allocator; const allocator = std.testing.allocator;

View file

@ -9,6 +9,7 @@ pub const ecs = @import("ecs");
pub const sysaudio = @import("sysaudio"); pub const sysaudio = @import("sysaudio");
pub const sysjs = @import("sysjs"); pub const sysjs = @import("sysjs");
pub const earcut = @import("earcut"); pub const earcut = @import("earcut");
pub const gfx = @import("gfx/util.zig");
// Engine exports // Engine exports
pub const App = @import("engine.zig").App; pub const App = @import("engine.zig").App;
@ -22,4 +23,5 @@ test {
// testing.refAllDeclsRecursive(Core); // testing.refAllDeclsRecursive(Core);
// testing.refAllDeclsRecursive(Timer); // testing.refAllDeclsRecursive(Timer);
testing.refAllDeclsRecursive(ResourceManager); testing.refAllDeclsRecursive(ResourceManager);
testing.refAllDeclsRecursive(gfx);
} }