Blocks and chunks

This commit is contained in:
2025-11-28 23:24:22 +01:00
parent 2541dee18d
commit 81a56f393e
32 changed files with 714 additions and 113 deletions

View File

@@ -37,12 +37,12 @@ pub fn init(engine: *Engine, allocator: std.mem.Allocator) !Textures {
errdefer map.deinit(allocator);
try map.ensureTotalCapacity(allocator, capacity);
var array: Array = try .initCapacity(allocator, capacity);
var textures: Array = try .initCapacity(allocator, capacity);
errdefer {
for (array.items) |*texture| {
for (textures.items) |*texture| {
texture.deinit(engine);
}
array.deinit(allocator);
textures.deinit(allocator);
}
const empty_base_color_texture = try Texture.init(engine, .{
@@ -51,6 +51,7 @@ pub fn init(engine: *Engine, allocator: std.mem.Allocator) !Textures {
.usage = .base_color,
.target_queue = .graphics,
});
textures.appendAssumeCapacity(empty_base_color_texture);
const empty_emissive_texture = try Texture.init(engine, .{
.width = 1,
@@ -58,6 +59,7 @@ pub fn init(engine: *Engine, allocator: std.mem.Allocator) !Textures {
.usage = .emissive,
.target_queue = .graphics,
});
textures.appendAssumeCapacity(empty_emissive_texture);
const empty_normal_texture = try Texture.init(engine, .{
.width = 1,
@@ -65,6 +67,7 @@ pub fn init(engine: *Engine, allocator: std.mem.Allocator) !Textures {
.usage = .normal,
.target_queue = .graphics,
});
textures.appendAssumeCapacity(empty_normal_texture);
const empty_occlusuion_roughness_metallic_texture = try Texture.init(engine, .{
.width = 1,
@@ -72,13 +75,7 @@ pub fn init(engine: *Engine, allocator: std.mem.Allocator) !Textures {
.usage = .occlusion_roughness_metallic,
.target_queue = .graphics,
});
array.appendSliceAssumeCapacity(&.{
empty_base_color_texture,
empty_emissive_texture,
empty_normal_texture,
empty_occlusuion_roughness_metallic_texture,
});
textures.appendAssumeCapacity(empty_occlusuion_roughness_metallic_texture);
try empty_base_color_texture.writeSamples(u8, engine, &.{ 255, 255, 255, 255 });
try empty_emissive_texture.writeSamples(f16, engine, &.{ 1.0, 1.0, 1.0, 1.0 });
@@ -87,7 +84,7 @@ pub fn init(engine: *Engine, allocator: std.mem.Allocator) !Textures {
return .{
.map = map,
.textures = array,
.textures = textures,
};
}
@@ -114,14 +111,10 @@ pub fn getFilename(self: *const Textures, filename: []const u8, usage: Texture.U
}
pub fn getTexture(self: *const Textures, id: Id) ?*Texture {
const index: usize = id.id;
const index: usize = @intFromEnum(id);
return if (index < self.textures.items.len) &self.textures.items[index] else null;
}
pub fn getId(self: *const Textures, key: Key) ?Id {
return self.map.get(key);
}
pub fn getOrLoadAtom(self: *Textures, engine: *Engine, atom: atoms.Atom, usage: Texture.Usage, temp_allocator: std.mem.Allocator) !Id {
const key: Key = .{ .atom = atom, .usage = usage };
const entry = self.map.getOrPutAssumeCapacity(key);
@@ -130,7 +123,7 @@ pub fn getOrLoadAtom(self: *Textures, engine: *Engine, atom: atoms.Atom, usage:
return entry.value_ptr.*;
} else {
errdefer _ = self.map.remove(key);
const texture = try self.loadTexture(engine, atoms.getString(atom), usage, temp_allocator);
const texture = try loadTexture(engine, atoms.getString(atom), usage, temp_allocator);
const id = self.nextId();
entry.value_ptr.* = id;
self.textures.appendAssumeCapacity(texture);