More refactors around assets. Trust me, we need them

This commit is contained in:
2025-12-04 23:31:30 +01:00
parent d885fbea43
commit a372bcb981
13 changed files with 868 additions and 592 deletions

View File

@@ -1,143 +1,135 @@
//! Module for loading block definitions.
const Blocks = @This();
const std = @import("std");
const voxels = @import("../voxels.zig");
const Atom = @import("../engine/Atom.zig").Atom;
const Engine = @import("../engine/Engine.zig");
const Materials = @import("Materials.zig");
const Textures = @import("Textures.zig");
pub const Block = struct {
positive_x: Materials.Id,
negative_x: Materials.Id,
positive_y: Materials.Id,
negative_y: Materials.Id,
positive_z: Materials.Id,
negative_z: Materials.Id,
pub const Id = enum(u16) {
// VOLATILE Synchronize explicit values with `init` implementation.
pub fn initUniform(material: Materials.Id) Block {
return .{
.positive_x = material,
.negative_x = material,
.positive_y = material,
.negative_y = material,
.positive_z = material,
.negative_z = material,
};
}
pub fn initSeparate(
positive_x: Materials.Id,
negative_x: Materials.Id,
positive_y: Materials.Id,
negative_y: Materials.Id,
positive_z: Materials.Id,
negative_z: Materials.Id,
) Block {
return .{
.positive_x = positive_x,
.negative_x = negative_x,
.positive_y = positive_y,
.negative_y = negative_y,
.positive_z = positive_z,
.negative_z = negative_z,
};
}
pub fn initArray(materials: [6]Materials.Id) Block {
return .{
.positive_x = materials[0],
.negative_x = materials[1],
.positive_y = materials[2],
.negative_y = materials[3],
.positive_z = materials[4],
.negative_z = materials[5],
};
}
};
map: Map,
blocks: Array,
pub const capacity = std.math.maxInt(std.meta.Tag(Id));
pub const Key = struct { atom: Atom };
pub const Id = enum(u12) {
/// An empty block.
air = 0,
_,
pub fn next(self: Id) Id {
return @enumFromInt(@intFromEnum(self) + 1);
/// Cast an integer into an ID. This can produce an invalid ID.
pub fn fromInt(value: u16) Id {
return @enumFromInt(value);
}
/// Cast an index into an ID. This can produce an invalid ID. The caller
/// asserts that the index is not greater than the max ID value.
pub fn fromIndex(index: usize) Id {
std.debug.assert(index < max_blocks);
return @enumFromInt(@as(u16, @intCast(index)));
}
/// Cast an index into an ID. This can produce an invalid ID. Returns an
/// error if the index is greater than the max ID value.
pub fn fromIndexSafe(index: usize) error{Overflow}!Id {
if (index >= max_blocks) return error.Overflow;
return @enumFromInt(@as(u16, @intCast(index)));
}
/// Cast an ID into an integer.
pub fn toInt(self: Id) u16 {
return @intFromEnum(self);
}
};
pub const Map = std.AutoHashMapUnmanaged(Key, Id);
pub const Array = std.ArrayList(Block);
pub const Key = struct {
/// Atom representing the filename of the block definition.
filename: Atom,
};
pub const Definition = struct {
pub const Wall = struct {
material: Materials.Id = .empty,
transform: voxels.Transform = .identity,
};
pub const Walls = std.enums.EnumFieldStruct(voxels.Orientation, Wall, .{});
walls: Walls = .{},
pub fn initUniform(material: Materials.Id) Definition {
const wall: Wall = .{
.material = material,
};
return .{
.walls = .{
.negative_x = wall,
.positive_x = wall,
.negative_y = wall,
.positive_y = wall,
.negative_z = wall,
.positive_z = wall,
},
};
}
};
/// Maps a key value to a block definition ID.
map: std.AutoHashMapUnmanaged(Key, Id),
/// Stores all `Definition` structs and maps a block definition ID to a
/// `Definition` struct.
array: std.ArrayList(Definition),
/// With `@sizeOf(Definition) == 24` and `max_blocks = 4096`, the block
/// definitions should take ~95.4 kiB in RAM.
pub const max_blocks = 4096;
pub fn init(allocator: std.mem.Allocator) !Blocks {
var map: Map = .empty;
var map: std.AutoHashMapUnmanaged(Key, Id) = .empty;
errdefer map.deinit(allocator);
try map.ensureTotalCapacity(allocator, capacity);
try map.ensureTotalCapacity(allocator, max_blocks);
var blocks: Array = try .initCapacity(allocator, capacity);
errdefer blocks.deinit(allocator);
var array: std.ArrayList(Definition) = try .initCapacity(allocator, max_blocks);
errdefer array.deinit(allocator);
const air = Block.initUniform(.empty);
blocks.appendAssumeCapacity(air);
// VOLATILE Synchronize with explicit values on top of `Id` type.
array.appendAssumeCapacity(.{});
return .{
.map = map,
.blocks = blocks,
.array = array,
};
}
pub fn deinit(self: *Blocks, allocator: std.mem.Allocator) void {
std.log.scoped(.deinit).debug("Deinitializing {*} with Allocator{{{*},{*}}}", .{ self, allocator.ptr, allocator.vtable });
self.blocks.deinit(allocator);
self.array.deinit(allocator);
self.map.deinit(allocator);
self.* = undefined;
}
pub fn getAtom(self: *const Blocks, atom: Atom) ?Id {
const key: Key = .{ .atom = atom };
return self.map.get(key);
/// Get the ID of a block definition given its filename (as a string). Returns
/// `null` if such block hasn't been loaded.
pub fn get(self: *const Blocks, filename: []const u8) ?Id {
return self.map.get(.{
// If the atom doesn't exist, then the block definition cannot possibly
// exist.
.filename = .fromStringIfExists(filename) orelse return null,
});
}
pub fn getFilename(self: *const Blocks, filename: []const u8) ?Id {
const atom = Atom.fromStringIfExists(filename) orelse return null;
const key: Key = .{ .atom = atom };
return self.map.get(key);
/// Get the ID of a block definition given its filename (as an atom). Returns
/// `null` if such block hasn't been loaded.
pub fn getAtom(self: *const Blocks, filename: Atom) ?Id {
return self.map.get(.{
.filename = filename,
});
}
pub fn getBlock(self: *const Blocks, id: Id) ?*Block {
const index: usize = @intFromEnum(id);
return if (index < self.blocks.items.len) &self.blocks.items[index] else null;
}
pub fn getOrLoadAtom(
self: *Blocks,
engine: *Engine,
materials: *Materials,
textures: *Textures,
atom: Atom,
temp_allocator: std.mem.Allocator,
) !Id {
const key: Key = .{ .atom = atom };
const entry = self.map.getOrPutAssumeCapacity(key);
if (entry.found_existing) {
return entry.value_ptr.*;
} else {
errdefer _ = self.map.remove(key);
const block = try loadBlock(engine, materials, textures, Atom.toString(), temp_allocator);
const id = self.nextId();
entry.value_ptr.* = id;
self.blocks.appendAssumeCapacity(block);
return id;
}
}
pub fn getOrLoadFilename(
pub fn getOrLoad(
self: *Blocks,
engine: *Engine,
materials: *Materials,
@@ -145,18 +137,56 @@ pub fn getOrLoadFilename(
filename: []const u8,
temp_allocator: std.mem.Allocator,
) !Id {
const atom = try Atom.fromString(filename);
const key: Key = .{ .atom = atom };
const entry = self.map.getOrPutAssumeCapacity(key);
const key: Key = .{
// If the material already exists, then the atom must exist and the
// following line will not return any error.
.filename = try .fromString(filename),
};
if (entry.found_existing) {
return entry.value_ptr.*;
// We don't use `getOrPutAssumeCapacity` method, because we might already be
// at full capacity, in which case we should return `error.OutOfBlocks`.
if (self.map.get(key)) |id| {
return id;
} else {
errdefer _ = self.map.remove(key);
const texture = try loadBlock(engine, materials, textures, filename, temp_allocator);
const id = self.nextId();
entry.value_ptr.* = id;
self.blocks.appendAssumeCapacity(texture);
const id = Id.fromIndexSafe(self.array.items.len) catch |err| switch (err) {
error.Overflow => return error.OutOfBlocks,
};
const def = try loadBlock(engine, materials, textures, filename, temp_allocator);
self.map.putAssumeCapacityNoClobber(key, id);
self.array.appendAssumeCapacity(def);
return id;
}
}
pub fn getOrLoadAtom(
self: *Blocks,
engine: *Engine,
materials: *Materials,
textures: *Textures,
filename: Atom,
temp_allocator: std.mem.Allocator,
) !Id {
const key: Key = .{
.filename = filename,
};
// We don't use `getOrPutAssumeCapacity` method, because we might already be
// at full capacity, in which case we should return `error.OutOfBlocks`.
if (self.map.get(key)) |id| {
return id;
} else {
const id = Id.fromIndexSafe(self.array.items.len) catch |err| switch (err) {
error.Overflow => return error.OutOfBlocks,
};
const def = try loadBlock(engine, materials, textures, filename.toString(), temp_allocator);
self.map.putAssumeCapacityNoClobber(key, id);
self.array.appendAssumeCapacity(def);
return id;
}
}
@@ -171,23 +201,23 @@ pub fn loadAll(
const cwd = std.fs.cwd();
var dir = cwd.openDir("assets/blocks", .{ .iterate = true }) catch |err| {
std.log.err("Error while opening blocks directory: {s}", .{@errorName(err)});
std.log.err("Error while opening block definitions directory: {s}", .{@errorName(err)});
return;
};
defer dir.close();
var it = dir.iterate();
while (it.next() catch |err| {
std.log.err("Error while iterating over blocks directory: {s}", .{@errorName(err)});
std.log.err("Error while iterating over block definitions directory: {s}", .{@errorName(err)});
return;
}) |entry| {
if (entry.kind != .file) {
std.log.warn("Skipping block entry {s}, which is not a file", .{entry.name});
std.log.warn("Skipping block definition entry {s}, which is not a file", .{entry.name});
continue;
}
_ = self.getOrLoadFilename(engine, materials, textures, entry.name, temp_allocator) catch |err| {
std.log.err("Error while loading block entry {s}: {s}", .{ entry.name, @errorName(err) });
_ = self.getOrLoad(engine, materials, textures, entry.name, temp_allocator) catch |err| {
std.log.err("Error while loading block definition entry {s}: {s}", .{ entry.name, @errorName(err) });
};
}
}
@@ -198,10 +228,17 @@ fn loadBlock(
textures: *Textures,
filename: []const u8,
temp_allocator: std.mem.Allocator,
) !Block {
const BlockJson = struct {
) !Definition {
const DefinitionJson = struct {
pub const Wall = struct {
material: ?[]const u8 = null,
transform: voxels.Transform = .identity,
};
pub const Walls = std.enums.EnumFieldStruct(voxels.Orientation, Wall, .{});
material: ?[]const u8 = null,
materials: ?[6][]const u8 = null,
walls: ?Walls = null,
};
std.log.debug("Loading block \"{s}\"...", .{filename});
@@ -212,7 +249,7 @@ fn loadBlock(
defer dir.close();
// NOTE Buffer size approximated based on expected JSON structure.
var buffer: [256]u8 = undefined;
var buffer: [1024]u8 = undefined;
const file = try dir.openFile(filename, .{});
defer file.close();
@@ -221,42 +258,44 @@ fn loadBlock(
var json_reader: std.json.Reader = .init(temp_allocator, &file_reader.interface);
defer json_reader.deinit();
const parsed: std.json.Parsed(BlockJson) = try std.json.parseFromTokenSource(BlockJson, temp_allocator, &json_reader, .{
const parsed: std.json.Parsed(DefinitionJson) = try std.json.parseFromTokenSource(DefinitionJson, temp_allocator, &json_reader, .{
.duplicate_field_behavior = .@"error",
.ignore_unknown_fields = false,
.allocate = .alloc_if_needed,
});
defer parsed.deinit();
const block_json = parsed.value;
const def_json = parsed.value;
const block: Block = blk: {
if (block_json.material) |name| {
if (block_json.materials != null) {
std.log.err("Block entry {s} has both properties \"material\" and \"materials\" defined, but exactly one of them must be defined", .{filename});
const block: Definition = blk: {
if (def_json.material) |name| {
if (def_json.walls != null) {
std.log.err("Block definition entry {s} has both properties \"material\" and \"walls\" defined, but exactly one of them must be defined", .{filename});
return error.ParseError;
}
const material = try materials.getOrLoadFilename(engine, textures, name, temp_allocator);
const material = try materials.getOrLoad(engine, textures, name, temp_allocator);
break :blk .initUniform(material);
}
if (block_json.materials) |names| {
var ids: [6]Materials.Id = undefined;
for (names, &ids) |name, *id| {
id.* = try materials.getOrLoadFilename(engine, textures, name, temp_allocator);
}
break :blk .initArray(ids);
if (def_json.walls) |walls| {
break :blk .{
.walls = walls: {
var ret: Definition.Walls = undefined;
inline for (@typeInfo(voxels.Orientation).@"enum".fields) |field| {
@field(ret, field.name) = .{
.material = try materials.getOrLoad(engine, textures, @field(walls, field.name).material, temp_allocator),
.transform = @field(walls, field.name).transform,
};
}
break :walls ret;
},
};
}
std.log.err("Block entry {s} has neither \"material\" or \"materials\" properties defined, but exactly one of them must be defined", .{filename});
std.log.err("Block definition entry {s} has neither \"material\" or \"walls\" properties defined, but exactly one of them must be defined", .{filename});
return error.ParseError;
};
return block;
}
fn nextId(self: *const Blocks) Id {
const index = self.blocks.items.len;
return @enumFromInt(index);
}