Begin new Asset Pipeline
This commit is contained in:
70
src/assets/Atoms.zig
Normal file
70
src/assets/Atoms.zig
Normal file
@@ -0,0 +1,70 @@
|
||||
pub const Atoms = @This();
|
||||
const std = @import("std");
|
||||
|
||||
allocator: std.mem.Allocator,
|
||||
string_arena_state: std.heap.ArenaAllocator.State,
|
||||
map: Map,
|
||||
array: Array,
|
||||
|
||||
pub const Atom = extern struct {
|
||||
atom: u32,
|
||||
};
|
||||
|
||||
pub const Map = std.StringHashMapUnmanaged(Atom);
|
||||
pub const Array = std.ArrayList([]const u16);
|
||||
|
||||
pub fn init(allocator: std.mem.Allocator) Atoms {
|
||||
return .{
|
||||
.allocator = allocator,
|
||||
.string_arena_state = .{},
|
||||
.map = .empty,
|
||||
.next_index = 0,
|
||||
};
|
||||
}
|
||||
|
||||
pub fn deinit(self: *Atoms) void {
|
||||
self.string_arena_state.promote(std.heap.page_allocator).deinit();
|
||||
self.map.deinit(self.allocator);
|
||||
self.array.deinit(self.allocator);
|
||||
self.* = undefined;
|
||||
}
|
||||
|
||||
pub fn getString(self: *const Atoms, atom: Atom) ?[]const u8 {
|
||||
const index: usize = atom.atom;
|
||||
return if (index < self.array.items.len) self.array.items[index] else null;
|
||||
}
|
||||
|
||||
pub fn getAtom(self: *const Atoms, string: []const u8) ?Atom {
|
||||
return self.map.get(string);
|
||||
}
|
||||
|
||||
pub fn getOrPutAtom(self: *Atoms, string: []const u8) !Atom {
|
||||
const entry = try self.map.getOrPut(self.allocator, string);
|
||||
|
||||
if (entry.found_existing) {
|
||||
return entry.value_ptr.*;
|
||||
} else {
|
||||
errdefer self.map.remove(string);
|
||||
try self.array.ensureUnusedCapacity(self.allocator, 1);
|
||||
const owned_string = try self.toOwnedString(string);
|
||||
|
||||
const index = self.array.items.len;
|
||||
const atom: Atom = .{ .atom = @intCast(index) };
|
||||
|
||||
entry.key_ptr.* = owned_string;
|
||||
entry.value_ptr.* = atom;
|
||||
|
||||
self.array.appendAssumeCapacity(owned_string);
|
||||
|
||||
return atom;
|
||||
}
|
||||
}
|
||||
|
||||
fn toOwnedString(self: *const Atoms, string: []const u8) ![]const u8 {
|
||||
var string_arena = self.string_arena_state.promote(std.heap.page_allocator);
|
||||
defer self.string_arena_state = string_arena.state;
|
||||
|
||||
const allocator = string_arena.allocator();
|
||||
const owned_string = try allocator.dupe(u8, string);
|
||||
return owned_string;
|
||||
}
|
||||
138
src/assets/Materials.zig
Normal file
138
src/assets/Materials.zig
Normal file
@@ -0,0 +1,138 @@
|
||||
const Materials = @This();
|
||||
const std = @import("std");
|
||||
|
||||
const vk = @import("vulkan");
|
||||
|
||||
const Atoms = @import("Atoms.zig");
|
||||
const Engine = @import("../engine/Engine.zig");
|
||||
const Textures = @import("Textures.zig");
|
||||
|
||||
allocator: std.mem.Allocator,
|
||||
map: Map,
|
||||
|
||||
pub const Id = extern struct {
|
||||
id: u32,
|
||||
};
|
||||
|
||||
pub const Map = std.AutoHashMapUnmanaged(Atoms.Atom, Id);
|
||||
|
||||
pub const Material = extern struct {
|
||||
base_color: [3]f32,
|
||||
base_color_texture: Textures.Id,
|
||||
emissive: [3]f32,
|
||||
emissive_texture: Textures.Id,
|
||||
ior: f32,
|
||||
metallic: f32,
|
||||
normal_scale: f32,
|
||||
normal_texture: Textures.Id,
|
||||
occlusion_roughness_metallic_texture: Textures.Id,
|
||||
occlusion_texture_strength: f32,
|
||||
roughness: f32,
|
||||
};
|
||||
|
||||
pub fn init(allocator: std.mem.Allocator) !Materials {
|
||||
var map: Map = .empty;
|
||||
errdefer map.deinit(allocator);
|
||||
|
||||
return .{
|
||||
.allocator = allocator,
|
||||
.map = map,
|
||||
};
|
||||
}
|
||||
|
||||
pub fn deinit(self: *Materials) void {
|
||||
self.arena.deinit();
|
||||
self.* = undefined;
|
||||
}
|
||||
|
||||
fn loadMaterial(self: *Materials, textures: *Textures, engine: *Engine, atoms: *Atoms, key: Atoms.Atom) !Id {
|
||||
const MaterialJson = struct {
|
||||
baseColor: [3]f32 = .{ 1, 1, 1 },
|
||||
baseColorTexture: ?[]const u8 = null,
|
||||
emissive: [3]f32 = .{ 0, 0, 0 },
|
||||
emissiveTexture: ?[]const u8 = null,
|
||||
ior: f32 = 1.45,
|
||||
metallic: f32 = 1,
|
||||
normalScale: f32 = 1,
|
||||
normalTexture: ?[]const u8 = null,
|
||||
occlusionRoughnessMetallicTexture: ?[]const u8 = null,
|
||||
occlusionTextureStrength: f32 = 1,
|
||||
roughness: f32 = 1,
|
||||
};
|
||||
|
||||
const filename = atoms.getString(key).?;
|
||||
|
||||
const cwd = std.fs.cwd();
|
||||
|
||||
var dir = try cwd.openDir("assets/materials", .{});
|
||||
defer dir.close();
|
||||
|
||||
// NOTE Buffer size approximated based on expected JSON structure.
|
||||
var buffer: [512]u8 = undefined;
|
||||
|
||||
const file = try dir.openFile(filename, .{});
|
||||
defer file.close();
|
||||
|
||||
var file_reader = file.reader(&buffer);
|
||||
var json_reader: std.json.Reader = .init(self.allocator, &file_reader.interface);
|
||||
defer json_reader.deinit();
|
||||
|
||||
const parsed: std.json.Parsed(MaterialJson) = try std.json.parseFromTokenSource(MaterialJson, self.allocator, &json_reader, .{
|
||||
.duplicate_field_behavior = .@"error",
|
||||
.ignore_unknown_fields = false,
|
||||
.allocate = .alloc_if_needed,
|
||||
});
|
||||
defer parsed.deinit();
|
||||
|
||||
const material_json = parsed.value;
|
||||
|
||||
const base_color_texture = blk: {
|
||||
if (material_json.baseColorTexture) |name| {
|
||||
const atom = try atoms.getOrPutAtom(name);
|
||||
break :blk try textures.getOrLoadId(engine, .{ .atom = atom, .usage = .base_color });
|
||||
} else {
|
||||
break :blk textures.empty_base_color;
|
||||
}
|
||||
};
|
||||
|
||||
const emissive_texture = blk: {
|
||||
if (material_json.emissiveTexture) |name| {
|
||||
const atom = try atoms.getOrPutAtom(name);
|
||||
break :blk try textures.getOrLoadId(engine, .{ .atom = atom, .usage = .emissive });
|
||||
} else {
|
||||
break :blk textures.empty_emissive;
|
||||
}
|
||||
};
|
||||
|
||||
const normal_texture = blk: {
|
||||
if (material_json.normalTexture) |name| {
|
||||
const atom = try atoms.getOrPutAtom(name);
|
||||
break :blk try textures.getOrLoadId(engine, .{ .atom = atom, .usage = .normal });
|
||||
} else {
|
||||
break :blk textures.empty_normal;
|
||||
}
|
||||
};
|
||||
|
||||
const occlusion_roughness_metallic_texture = blk: {
|
||||
if (material_json.occlusionRoughnessMetallicTexture) |name| {
|
||||
const atom = try atoms.getOrPutAtom(name);
|
||||
break :blk try textures.getOrLoadId(engine, .{ .atom = atom, .usage = .occlusion_roughness_metallic });
|
||||
} else {
|
||||
break :blk textures.empty_occlusuion_roughness_metallic;
|
||||
}
|
||||
};
|
||||
|
||||
const material: Material = .{
|
||||
.base_color = material_json.baseColor,
|
||||
.base_color_texture = base_color_texture,
|
||||
.emissive = material_json.emissive,
|
||||
.emissive_texture = emissive_texture,
|
||||
.ior = material_json.ior,
|
||||
.metallic = material_json.metallic,
|
||||
.normal_scale = material_json.normalScale,
|
||||
.normal_texture = normal_texture,
|
||||
.occlusion_roughness_metallic_texture = occlusion_roughness_metallic_texture,
|
||||
.occlusion_texture_strength = material_json.occlusionTextureStrength,
|
||||
.roughness = material_json.roughness,
|
||||
};
|
||||
}
|
||||
139
src/assets/Textures.zig
Normal file
139
src/assets/Textures.zig
Normal 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) };
|
||||
}
|
||||
Reference in New Issue
Block a user