Loading materials and textures
This commit is contained in:
@@ -5,10 +5,15 @@ const vk = @import("vulkan");
|
||||
|
||||
const Atoms = @import("Atoms.zig");
|
||||
const Engine = @import("../engine/Engine.zig");
|
||||
const StorageBuffer = @import("../engine/StorageBuffer.zig");
|
||||
const Textures = @import("Textures.zig");
|
||||
|
||||
allocator: std.mem.Allocator,
|
||||
map: Map,
|
||||
storage_buffer: StorageBuffer,
|
||||
next_id: Id,
|
||||
|
||||
pub const initial_capacity = 4;
|
||||
|
||||
pub const Id = extern struct {
|
||||
id: u32,
|
||||
@@ -30,22 +35,44 @@ pub const Material = extern struct {
|
||||
roughness: f32,
|
||||
};
|
||||
|
||||
pub fn init(allocator: std.mem.Allocator) !Materials {
|
||||
pub fn init(allocator: std.mem.Allocator, engine: *Engine) !Materials {
|
||||
var map: Map = .empty;
|
||||
errdefer map.deinit(allocator);
|
||||
|
||||
var storage_buffer: StorageBuffer = try .init(engine, void, Material, initial_capacity);
|
||||
errdefer storage_buffer.deinit(engine);
|
||||
|
||||
return .{
|
||||
.allocator = allocator,
|
||||
.map = map,
|
||||
.storage_buffer = storage_buffer,
|
||||
.next_id = .{ .id = 0 },
|
||||
};
|
||||
}
|
||||
|
||||
pub fn deinit(self: *Materials) void {
|
||||
self.arena.deinit();
|
||||
pub fn deinit(self: *Materials, engine: *Engine) void {
|
||||
self.storage_buffer.deinit(engine);
|
||||
self.map.deinit(self.allocator);
|
||||
self.* = undefined;
|
||||
}
|
||||
|
||||
fn loadMaterial(self: *Materials, textures: *Textures, engine: *Engine, atoms: *Atoms, key: Atoms.Atom) !Id {
|
||||
pub fn getId(self: *const Materials, key: Atoms.Atom) ?Id {
|
||||
return self.map.get(key);
|
||||
}
|
||||
|
||||
pub fn getOrLoadId(self: *Materials, engine: *Engine, textures: *Textures, atoms: *Atoms, key: Atoms.Atom) !Id {
|
||||
const entry = try self.map.getOrPut(self.allocator, key);
|
||||
|
||||
if (entry.found_existing) {
|
||||
return entry.value_ptr.*;
|
||||
} else {
|
||||
const id = try self.loadMaterial(engine, textures, atoms, key);
|
||||
entry.value_ptr.* = id;
|
||||
return id;
|
||||
}
|
||||
}
|
||||
|
||||
fn loadMaterial(self: *Materials, engine: *Engine, textures: *Textures, atoms: *Atoms, key: Atoms.Atom) !Id {
|
||||
const MaterialJson = struct {
|
||||
baseColor: [3]f32 = .{ 1, 1, 1 },
|
||||
baseColorTexture: ?[]const u8 = null,
|
||||
@@ -89,7 +116,7 @@ fn loadMaterial(self: *Materials, textures: *Textures, engine: *Engine, atoms: *
|
||||
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 });
|
||||
break :blk try textures.getOrLoadId(engine, atoms, .{ .atom = atom, .usage = .base_color });
|
||||
} else {
|
||||
break :blk textures.empty_base_color;
|
||||
}
|
||||
@@ -98,7 +125,7 @@ fn loadMaterial(self: *Materials, textures: *Textures, engine: *Engine, atoms: *
|
||||
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 });
|
||||
break :blk try textures.getOrLoadId(engine, atoms, .{ .atom = atom, .usage = .emissive });
|
||||
} else {
|
||||
break :blk textures.empty_emissive;
|
||||
}
|
||||
@@ -107,7 +134,7 @@ fn loadMaterial(self: *Materials, textures: *Textures, engine: *Engine, atoms: *
|
||||
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 });
|
||||
break :blk try textures.getOrLoadId(engine, atoms, .{ .atom = atom, .usage = .normal });
|
||||
} else {
|
||||
break :blk textures.empty_normal;
|
||||
}
|
||||
@@ -116,23 +143,37 @@ fn loadMaterial(self: *Materials, textures: *Textures, engine: *Engine, atoms: *
|
||||
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 });
|
||||
break :blk try textures.getOrLoadId(engine, atoms, .{ .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,
|
||||
const next_id = self.next_id;
|
||||
const offset: usize = next_id.id;
|
||||
|
||||
if (offset >= self.storage_buffer.array_capacity) {
|
||||
try self.storage_buffer.enlarge(void, Material, engine, self.storage_buffer.array_capacity * 2);
|
||||
}
|
||||
|
||||
const materials = [_]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,
|
||||
},
|
||||
};
|
||||
|
||||
try self.storage_buffer.writeOffset(void, Material, engine, {}, offset, &materials);
|
||||
self.next_id = .{ .id = next_id.id + 1 };
|
||||
|
||||
return next_id;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user