Begin new Asset Pipeline

This commit is contained in:
2025-11-21 13:02:32 +01:00
parent bbafc55f6f
commit 63a8eee18c
34 changed files with 1190 additions and 586 deletions

139
src/assets/Textures.zig Normal file
View File

@@ -0,0 +1,139 @@
const Textures = @This();
const std = @import("std");
const stbi = @import("zstbi");
const Atoms = @import("Atoms.zig");
const Engine = @import("../engine/Engine.zig");
const Texture = @import("../engine/Texture.zig");
allocator: std.mem.Allocator,
map: Map,
array: Array,
empty_base_color: Id,
empty_emissive: Id,
empty_normal: Id,
empty_occlusuion_roughness_metallic: Id,
pub const Id = extern struct {
id: u32,
};
pub const Key = struct {
atom: Atoms.Atom,
usage: Texture.Usage,
};
pub const Map = std.AutoHashMapUnmanaged(Key, Id);
pub const Array = std.ArrayList(Texture);
pub fn init(allocator: std.mem.Allocator, engine: *Engine) !Textures {
var map: Map = .empty;
errdefer map.deinit(allocator);
var array: Array = try .initCapacity(allocator, 4);
errdefer {
for (array.items) |texture| {
texture.deinit(engine);
}
array.deinit(allocator);
}
const empty_base_color: Id = .{ .id = @intCast(array.items.len) };
const empty_base_color_texture: Texture = try .init(engine, 1, 1, .base_color);
array.appendAssumeCapacity(empty_base_color_texture);
const empty_emissive: Id = .{ .id = @intCast(array.items.len) };
const empty_emissive_texture: Texture = try .init(engine, 1, 1, .emissive);
array.appendAssumeCapacity(empty_emissive_texture);
const empty_normal: Id = .{ .id = @intCast(array.items.len) };
const empty_normal_texture: Texture = try .init(engine, 1, 1, .normal);
array.appendAssumeCapacity(empty_normal_texture);
const empty_occlusuion_roughness_metallic: Id = .{ .id = @intCast(array.items.len) };
const empty_occlusuion_roughness_metallic_texture: Texture = try .init(engine, 1, 1, .occlusion_roughness_metallic);
array.appendAssumeCapacity(empty_occlusuion_roughness_metallic_texture);
empty_base_color_texture.write([3]u8, &.{.{ 255, 255, 255 }});
empty_emissive_texture.write([3]f32, &.{.{ 1.0, 1.0, 1.0 }});
empty_normal_texture.write([3]u8, &.{.{ 128, 128, 255 }});
empty_occlusuion_roughness_metallic_texture.write([3]u8, &.{.{ 255, 255, 255 }});
return .{
.map = map,
.array = array,
.empty_base_color = empty_base_color,
.empty_emissive = empty_emissive,
.empty_normal = empty_normal,
.empty_occlusuion_roughness_metallic = empty_occlusuion_roughness_metallic,
};
}
pub fn deinit(self: *Textures, allocator: std.mem.Allocator, engine: *Engine) void {
for (self.array.items) |texture| {
texture.deinit(engine);
}
self.array.deinit(allocator);
self.map.deinit(allocator);
}
pub fn getTexture(self: *const Textures, id: Id) ?*Texture {
const index: usize = id.id;
return if (index < self.array.items.len) &self.array.items[index] else null;
}
pub fn getId(self: *const Textures, key: Key) ?Id {
return self.map.get(key);
}
pub fn getOrLoadId(self: *Textures, engine: *Engine, key: Key) !Id {
const entry = try self.map.getOrPut(self.allocator, key);
if (entry.found_existing) {
return entry.value_ptr.*;
} else {
errdefer self.map.remove(key);
try self.array.ensureUnusedCapacity(self.allocator, 1);
const texture = try self.loadTexture(engine, key);
const id = self.nextId();
entry.value_ptr.* = id;
self.array.appendAssumeCapacity(texture);
return id;
}
}
fn loadTexture(self: *Textures, engine: *Engine, key: Key) !Texture {
const cwd = std.fs.cwd();
var dir = try cwd.openDir("assets/textures", .{});
defer dir.close();
const file = try dir.openFile(key.name, .{});
defer file.close();
const file_buf = try file.readToEndAlloc(self.allocator, std.math.maxInt(usize));
defer self.allocator.free(file_buf);
var img = try stbi.Image.loadFromMemory(file_buf, key.usage.sampleCount());
defer img.deinit();
std.debug.assert(img.num_components == key.usage.sampleCount());
var texture: Texture = try .init(engine, img.width, img.height, key.usage);
errdefer texture.deinit(engine);
texture.write(u8, img.data);
return texture;
}
fn nextId(self: *const Textures) Id {
const index = self.array.items.len;
return .{ .id = @intCast(index) };
}