Minor refactors and cleanups

This commit is contained in:
2025-12-02 15:58:51 +01:00
parent faddb1f35e
commit be4ae4f1a7
15 changed files with 223 additions and 121 deletions

View File

@@ -3,7 +3,7 @@ const std = @import("std");
const vk = @import("vulkan");
const atoms = @import("../engine/atoms.zig");
const Atom = @import("../engine/Atom.zig").Atom;
const Engine = @import("../engine/Engine.zig");
const GenericBuffer = @import("../engine/GenericBuffer.zig").GenericBuffer;
const Textures = @import("Textures.zig");
@@ -17,7 +17,7 @@ next_id: Id,
// capacity * @sizeOf(Material) = 832 kiB
pub const capacity = std.math.maxInt(std.meta.Tag(Id));
pub const Key = struct { atom: atoms.Atom };
pub const Key = struct { atom: Atom };
pub const Id = enum(u16) {
empty,
_,
@@ -67,25 +67,25 @@ pub fn init(engine: *Engine, allocator: std.mem.Allocator) !Materials {
}
pub fn deinit(self: *Materials, engine: *Engine, allocator: std.mem.Allocator) void {
std.log.debug("Deinitializing {*} with {*} and Allocator{{{*},{*}}}", .{ self, engine, allocator.ptr, allocator.vtable });
std.log.scoped(.deinit).debug("Deinitializing {*} with {*} and Allocator{{{*},{*}}}", .{ self, engine, allocator.ptr, allocator.vtable });
self.material_buffer.deinit(engine);
self.map.deinit(allocator);
self.* = undefined;
}
pub fn getAtom(self: *const Materials, atom: atoms.Atom) ?Id {
pub fn getAtom(self: *const Materials, atom: Atom) ?Id {
const key: Key = .{ .atom = atom };
return self.map.get(key);
}
pub fn getFilename(self: *const Materials, filename: []const u8) ?Id {
const atom = atoms.getAtom(filename) orelse return null;
const atom = Atom.fromStringIfExists(filename) orelse return null;
const key: Key = .{ .atom = atom };
return self.map.get(key);
}
pub fn getOrLoadAtom(self: *Materials, engine: *Engine, textures: *Textures, atom: atoms.Atom, temp_allocator: std.mem.Allocator) !Id {
pub fn getOrLoadAtom(self: *Materials, engine: *Engine, textures: *Textures, atom: Atom, temp_allocator: std.mem.Allocator) !Id {
const key: Key = .{ .atom = atom };
const entry = self.map.getOrPutAssumeCapacity(key);
@@ -93,14 +93,14 @@ pub fn getOrLoadAtom(self: *Materials, engine: *Engine, textures: *Textures, ato
return entry.value_ptr.*;
} else {
errdefer _ = self.map.remove(key);
const id = try self.loadMaterial(engine, textures, atoms.getString(atom), temp_allocator);
const id = try self.loadMaterial(engine, textures, atom.toString(), temp_allocator);
entry.value_ptr.* = id;
return id;
}
}
pub fn getOrLoadFilename(self: *Materials, engine: *Engine, textures: *Textures, filename: []const u8, temp_allocator: std.mem.Allocator) !Id {
const atom = try atoms.getOrPutAtom(filename);
const atom = try Atom.fromString(filename);
const key: Key = .{ .atom = atom };
const entry = self.map.getOrPutAssumeCapacity(key);