Globalize textures, materials and gui

This commit is contained in:
2026-05-23 01:37:28 +02:00
parent c971443b02
commit 40fcba9323
13 changed files with 195 additions and 242 deletions

View File

@@ -1,11 +1,20 @@
//! This module contains some common global variables, which mostly refer to
//! singleton modules that are active throughout entire program's runtime.
//!
//! These globals can be safely referenced during and after game initialization.
const AppContext = @This(); const AppContext = @This();
const std = @import("std"); const std = @import("std");
const media = @import("media");
const glfw = @import("zglfw"); const glfw = @import("zglfw");
const Atoms = @import("engine/Atoms.zig"); const Atoms = @import("engine/Atoms.zig");
const Engine = @import("engine/Engine.zig"); const Engine = @import("engine/Engine.zig");
const Gui = @import("engine/Gui.zig");
const Materials = @import("engine/Materials.zig");
const Swapchain = @import("engine/Swapchain.zig"); const Swapchain = @import("engine/Swapchain.zig");
const Textures = @import("engine/Textures.zig");
const VkAllocator = @import("engine/VkAllocator.zig"); const VkAllocator = @import("engine/VkAllocator.zig");
pub var allocator_general: std.mem.Allocator = undefined; pub var allocator_general: std.mem.Allocator = undefined;
@@ -13,8 +22,13 @@ pub var allocator_frame: std.mem.Allocator = undefined;
pub var allocator_persistent: std.mem.Allocator = undefined; pub var allocator_persistent: std.mem.Allocator = undefined;
pub var io: std.Io = undefined; pub var io: std.Io = undefined;
pub var vk_allocator: *VkAllocator = undefined; pub var vk_allocator: *VkAllocator = undefined;
pub var stbi: *media.stbi = undefined;
pub var window: *glfw.Window = undefined; pub var window: *glfw.Window = undefined;
pub var atoms: *Atoms = undefined; pub var atoms: *Atoms = undefined;
pub var engine: *Engine = undefined; pub var engine: *Engine = undefined;
pub var swapchain: *Swapchain = undefined; pub var swapchain: *Swapchain = undefined;
pub var textures: *Textures = undefined;
pub var materials: *Materials = undefined;
pub var gui: *Gui = undefined;

View File

@@ -16,7 +16,6 @@ const Chunk = @import("Chunk.zig");
const Chunks = @import("Chunks.zig"); const Chunks = @import("Chunks.zig");
const CommandBuffer = @import("engine/CommandBuffer.zig"); const CommandBuffer = @import("engine/CommandBuffer.zig");
const Engine = @import("engine/Engine.zig"); const Engine = @import("engine/Engine.zig");
const Gui = @import("Gui.zig");
const Iterator2 = math.Iterator2; const Iterator2 = math.Iterator2;
const Materials = @import("engine/Materials.zig"); const Materials = @import("engine/Materials.zig");
const Player = @import("Player.zig"); const Player = @import("Player.zig");
@@ -44,14 +43,9 @@ directional_lights: shaders.DirectionalLightBuffer,
sampler: vk.Sampler, sampler: vk.Sampler,
deferred_command_buffers: std.ArrayList(CommandBuffer), deferred_command_buffers: std.ArrayList(CommandBuffer),
stbi: media.stbi,
blocks: Blocks, blocks: Blocks,
materials: Materials,
textures: Textures,
chunks: Chunks, chunks: Chunks,
skybox: Skybox, skybox: Skybox,
gui: Gui,
player: Player, player: Player,
const max_textures = 1024; const max_textures = 1024;
@@ -64,18 +58,11 @@ const camera_near_plane = 0.1;
pub fn init() !Game { pub fn init() !Game {
const allocator_general = ctx.allocator_general; const allocator_general = ctx.allocator_general;
const allocator_frame = ctx.allocator_frame; const allocator_frame = ctx.allocator_frame;
const io = ctx.io;
const engine = ctx.engine; const engine = ctx.engine;
const swapchain = ctx.swapchain; const swapchain = ctx.swapchain;
const textures = ctx.textures;
var stbi = media.stbi.init(allocator_general, io); const materials = ctx.materials;
errdefer stbi.deinit(); const gui = ctx.gui;
var materials = try Materials.init();
errdefer materials.deinit();
var textures = try Textures.init();
errdefer textures.deinit();
var blocks = try Blocks.init(); var blocks = try Blocks.init();
errdefer blocks.deinit(); errdefer blocks.deinit();
@@ -83,8 +70,7 @@ pub fn init() !Game {
// JANK HACK When this line is removed, capturing a frame with RenderDoc // JANK HACK When this line is removed, capturing a frame with RenderDoc
// will crash the game with segfault reading address 0x140 (presumably // will crash the game with segfault reading address 0x140 (presumably
// within librenderdoc.so). // within librenderdoc.so).
blocks.loadAll();
blocks.loadAll(&materials, &textures, &stbi);
const sampler = try engine.createSampler(.{ const sampler = try engine.createSampler(.{
.mag_filter = .linear, .mag_filter = .linear,
@@ -447,10 +433,10 @@ pub fn init() !Game {
}); });
engine.setObjectName(global_descriptor_set, "DS Global", .{}); engine.setObjectName(global_descriptor_set, "DS Global", .{});
const block_grass = try blocks.getOrLoad(&materials, &textures, &stbi, "Grass.json"); const block_grass = try blocks.getOrLoad("Grass.json");
const block_dirt = try blocks.getOrLoad(&materials, &textures, &stbi, "Dirt.json"); const block_dirt = try blocks.getOrLoad("Dirt.json");
const block_stone = try blocks.getOrLoad(&materials, &textures, &stbi, "Stone.json"); const block_stone = try blocks.getOrLoad("Stone.json");
const block_bedrock = try blocks.getOrLoad(&materials, &textures, &stbi, "Bedrock.json"); const block_bedrock = try blocks.getOrLoad("Bedrock.json");
// VOLATILE Load all assets before this point // VOLATILE Load all assets before this point
@@ -640,18 +626,33 @@ pub fn init() !Game {
.elements = directional_lights_data, .elements = directional_lights_data,
}); });
var skybox = try Skybox.load( var skybox = try Skybox.load("skybox.hdr", 512, global_uniforms.buffer);
"skybox.hdr",
&stbi,
512,
global_uniforms.buffer,
);
errdefer skybox.deinit(); errdefer skybox.deinit();
var gui = try Gui.init( // TODO This coupling is not elegant - the game must provide the GUI with
global_uniforms.buffer, // uniform buffer for globals. Maybe GUI should just use their own global
); // uniform buffer not for any other reason than decoupling. After all, GUI
errdefer gui.deinit(); // only needs a single 3×2 matrix and does not need to depend on game's
// rendering pipeline.
try engine.updateDescriptorSets(.{
.writes = &.{
.{
.dst_set = gui.box_descriptor_set,
.dst_binding = 0,
.dst_array_element = 0,
.descriptor_type = .uniform_buffer,
.descriptor_infos = .{
.buffer = &.{
.{
.buffer = global_uniforms.buffer,
.offset = 0,
.range = vk.WHOLE_SIZE,
},
},
},
},
},
});
return .{ return .{
.global_descriptor_set_layout = global_descriptor_set_layout, .global_descriptor_set_layout = global_descriptor_set_layout,
@@ -672,14 +673,9 @@ pub fn init() !Game {
.sampler = sampler, .sampler = sampler,
.deferred_command_buffers = try .initCapacity(allocator_general, 4), .deferred_command_buffers = try .initCapacity(allocator_general, 4),
.stbi = stbi,
.blocks = blocks, .blocks = blocks,
.materials = materials,
.textures = textures,
.chunks = .{ .chunks = chunks }, .chunks = .{ .chunks = chunks },
.skybox = skybox, .skybox = skybox,
.gui = gui,
.player = .init(player_position_sv, 0, 0), .player = .init(player_position_sv, 0, 0),
}; };
} }
@@ -700,7 +696,6 @@ pub fn deinit(self: *Game) void {
self.chunks.deinit(self.descriptor_pool); self.chunks.deinit(self.descriptor_pool);
self.skybox.deinit(); self.skybox.deinit();
self.gui.deinit();
self.global_uniforms.deinit(); self.global_uniforms.deinit();
self.global_uniforms_staging_buffer.deinit(); self.global_uniforms_staging_buffer.deinit();
@@ -719,17 +714,14 @@ pub fn deinit(self: *Game) void {
engine.destroyDescriptorSetLayout(self.per_batch_descriptor_set_layout); engine.destroyDescriptorSetLayout(self.per_batch_descriptor_set_layout);
engine.destroyDescriptorSetLayout(self.global_descriptor_set_layout); engine.destroyDescriptorSetLayout(self.global_descriptor_set_layout);
self.textures.deinit();
self.materials.deinit();
self.blocks.deinit(); self.blocks.deinit();
self.stbi.deinit();
self.* = undefined; self.* = undefined;
} }
pub fn update(self: *Game, dt: f32) void { pub fn update(self: *Game, dt: f32) void {
const swapchain = ctx.swapchain; const swapchain = ctx.swapchain;
const gui = ctx.gui;
self.gui.beginFrame();
self.player.update(dt, &self.chunks); self.player.update(dt, &self.chunks);
const extent = swapchain.extent; const extent = swapchain.extent;
@@ -741,7 +733,7 @@ pub fn update(self: *Game, dt: f32) void {
const crosshair_half_extent_px = 8; const crosshair_half_extent_px = 8;
const crosshair_half_width_px = 1; const crosshair_half_width_px = 1;
self.gui.pushBox(.{ gui.pushBox(.{
.background_color = .init(1, 1, 1, 0.5), .background_color = .init(1, 1, 1, 0.5),
.border_color = .init(0, 0, 0, 1), .border_color = .init(0, 0, 0, 1),
.position_sspx = framebuffer_size.mulScalar(0.5).add(.init(-crosshair_half_extent_px, -crosshair_half_width_px)), .position_sspx = framebuffer_size.mulScalar(0.5).add(.init(-crosshair_half_extent_px, -crosshair_half_width_px)),
@@ -750,7 +742,7 @@ pub fn update(self: *Game, dt: f32) void {
.border_radius_px = 0, .border_radius_px = 0,
}); });
self.gui.pushBox(.{ gui.pushBox(.{
.background_color = .init(1, 1, 1, 0.5), .background_color = .init(1, 1, 1, 0.5),
.border_color = .init(0, 0, 0, 1), .border_color = .init(0, 0, 0, 1),
.position_sspx = framebuffer_size.mulScalar(0.5).add(.init(-crosshair_half_width_px, -crosshair_half_extent_px)), .position_sspx = framebuffer_size.mulScalar(0.5).add(.init(-crosshair_half_width_px, -crosshair_half_extent_px)),
@@ -790,6 +782,7 @@ fn render(self: *Game) !void {
const allocator_general = ctx.allocator_general; const allocator_general = ctx.allocator_general;
const window = ctx.window; const window = ctx.window;
const swapchain = ctx.swapchain; const swapchain = ctx.swapchain;
const gui = ctx.gui;
const framebuffer_width, const framebuffer_height = blk: { const framebuffer_width, const framebuffer_height = blk: {
const w, const h = window.getFramebufferSize(); const w, const h = window.getFramebufferSize();
@@ -1020,7 +1013,7 @@ fn render(self: *Game) !void {
// --- RENDER GUI --- // --- RENDER GUI ---
try self.gui.draw(command_buffer); try gui.draw(command_buffer);
// --- FINALIZE --- // --- FINALIZE ---

View File

@@ -262,12 +262,7 @@ pub fn onMouseDown(self: *Player, button: glfw.MouseButton, game: *Game) void {
}, },
.right => blk: { .right => blk: {
const target_vx = raycast_hit.voxel.add(raycast_hit.side.getSignVector()); const target_vx = raycast_hit.voxel.add(raycast_hit.side.getSignVector());
const id = game.blocks.getOrLoad( const id = game.blocks.getOrLoad(blocks[self.block_index]) catch |err| {
&game.materials,
&game.textures,
&game.stbi,
blocks[self.block_index],
) catch |err| {
std.log.err("Error while placing voxel at {f}: {}", .{ target_vx, err }); std.log.err("Error while placing voxel at {f}: {}", .{ target_vx, err });
break :blk; break :blk;
}; };

View File

@@ -136,13 +136,7 @@ pub fn getAtom(self: *const Blocks, filename: Atom) ?Id {
}); });
} }
pub fn getOrLoad( pub fn getOrLoad(self: *Blocks, filename: []const u8) !Id {
self: *Blocks,
materials: *Materials,
textures: *Textures,
stbi: *media.stbi,
filename: []const u8,
) !Id {
const key: Key = .{ const key: Key = .{
// If the material already exists, then the atom must exist and the // If the material already exists, then the atom must exist and the
// following line will not return any error. // following line will not return any error.
@@ -158,7 +152,7 @@ pub fn getOrLoad(
const id = Id.fromIndexSafe(self.array.items.len) catch |err| switch (err) { const id = Id.fromIndexSafe(self.array.items.len) catch |err| switch (err) {
error.Overflow => return error.OutOfBlocks, error.Overflow => return error.OutOfBlocks,
}; };
const def = try loadBlock(materials, textures, stbi, filename); const def = try loadBlock(filename);
self.map.putAssumeCapacityNoClobber(key, id); self.map.putAssumeCapacityNoClobber(key, id);
self.array.appendAssumeCapacity(def); self.array.appendAssumeCapacity(def);
@@ -167,13 +161,7 @@ pub fn getOrLoad(
} }
} }
pub fn getOrLoadAtom( pub fn getOrLoadAtom(self: *Blocks, filename: Atom) !Id {
self: *Blocks,
materials: *Materials,
textures: *Textures,
stbi: *media.stbi,
filename: Atom,
) !Id {
const key: Key = .{ const key: Key = .{
.filename = filename, .filename = filename,
}; };
@@ -187,7 +175,7 @@ pub fn getOrLoadAtom(
const id = Id.fromIndexSafe(self.array.items.len) catch |err| switch (err) { const id = Id.fromIndexSafe(self.array.items.len) catch |err| switch (err) {
error.Overflow => return error.OutOfBlocks, error.Overflow => return error.OutOfBlocks,
}; };
const def = try loadBlock(materials, textures, stbi, filename.toString()); const def = try loadBlock(filename.toString());
self.map.putAssumeCapacityNoClobber(key, id); self.map.putAssumeCapacityNoClobber(key, id);
self.array.appendAssumeCapacity(def); self.array.appendAssumeCapacity(def);
@@ -196,12 +184,7 @@ pub fn getOrLoadAtom(
} }
} }
pub fn loadAll( pub fn loadAll(self: *Blocks) void {
self: *Blocks,
materials: *Materials,
textures: *Textures,
stbi: *media.stbi,
) void {
const io = ctx.io; const io = ctx.io;
const cwd = std.Io.Dir.cwd(); const cwd = std.Io.Dir.cwd();
@@ -222,20 +205,16 @@ pub fn loadAll(
continue; continue;
} }
_ = self.getOrLoad(materials, textures, stbi, entry.name) catch |err| { _ = self.getOrLoad(entry.name) catch |err| {
std.log.err("Error while loading block definition entry {s}: {s}", .{ entry.name, @errorName(err) }); std.log.err("Error while loading block definition entry {s}: {s}", .{ entry.name, @errorName(err) });
}; };
} }
} }
fn loadBlock( fn loadBlock(filename: []const u8) !Definition {
materials: *Materials,
textures: *Textures,
stbi: *media.stbi,
filename: []const u8,
) !Definition {
const allocator_frame = ctx.allocator_frame; const allocator_frame = ctx.allocator_frame;
const io = ctx.io; const io = ctx.io;
const materials = ctx.materials;
const DefinitionJson = struct { const DefinitionJson = struct {
pub const Wall = struct { pub const Wall = struct {
@@ -278,7 +257,7 @@ fn loadBlock(
return error.ParseError; return error.ParseError;
} }
const material = try materials.getOrLoad(textures, stbi, name); const material = try materials.getOrLoad(name);
break :blk .initUniform(material); break :blk .initUniform(material);
} }
@@ -288,7 +267,7 @@ fn loadBlock(
var ret: Definition.Walls = undefined; var ret: Definition.Walls = undefined;
inline for (@typeInfo(voxels.Orientation).@"enum".fields) |field| { inline for (@typeInfo(voxels.Orientation).@"enum".fields) |field| {
@field(ret, field.name) = .{ @field(ret, field.name) = .{
.material = try materials.getOrLoad(textures, stbi, @field(walls, field.name).material), .material = try materials.getOrLoad(@field(walls, field.name).material),
.transform = @field(walls, field.name).transform, .transform = @field(walls, field.name).transform,
}; };
} }

View File

@@ -35,7 +35,6 @@ pub const Atom = enum(u16) {
/// new one, if necessary. This will always produce a valid atom. Will not /// new one, if necessary. This will always produce a valid atom. Will not
/// return any error if the atom already exists. /// return any error if the atom already exists.
pub fn fromString(string: []const u8) error{ OutOfMemory, OutOfAtoms }!Atom { pub fn fromString(string: []const u8) error{ OutOfMemory, OutOfAtoms }!Atom {
const allocator_general = ctx.allocator_general;
const allocator_persistent = ctx.allocator_persistent; const allocator_persistent = ctx.allocator_persistent;
const io = ctx.io; const io = ctx.io;
const atoms = ctx.atoms; const atoms = ctx.atoms;
@@ -43,23 +42,21 @@ pub const Atom = enum(u16) {
atoms.mutex.lockUncancelable(io); atoms.mutex.lockUncancelable(io);
defer atoms.mutex.unlock(io); defer atoms.mutex.unlock(io);
const entry = try atoms.map.getOrPut(allocator_general, string); // We don't use `getOrPutAssumeCapacity` method, because we might
// already be at full capacity, in which case we should return
// `error.OutOfAtoms`.
if (entry.found_existing) { if (atoms.map.get(string)) |atom| {
return entry.value_ptr.*; return atom;
} else { } else {
errdefer _ = atoms.map.remove(string);
const atom = Atom.fromIndexSafe(atoms.array.items.len) catch |err| switch (err) { const atom = Atom.fromIndexSafe(atoms.array.items.len) catch |err| switch (err) {
error.Overflow => return error.OutOfAtoms, error.Overflow => return error.OutOfAtoms,
}; };
try atoms.array.ensureUnusedCapacity(allocator_general, 1);
const owned_string = try allocator_persistent.dupeZ(u8, string); const owned_string = try allocator_persistent.dupeZ(u8, string);
entry.key_ptr.* = owned_string; atoms.map.putAssumeCapacityNoClobber(owned_string, atom);
entry.value_ptr.* = atom;
atoms.array.appendAssumeCapacity(owned_string); atoms.array.appendAssumeCapacity(owned_string);
return atom; return atom;
} }
} }

View File

@@ -18,21 +18,20 @@ const ctx = @import("../AppContext.zig");
const Atom = @import("Atom.zig").Atom; const Atom = @import("Atom.zig").Atom;
/// Maps a string value to an atom value. Uses `allocator_general`. /// Maps a string value to an atom value. Preallocated with
/// `allocator_persistent`.
map: std.StringHashMapUnmanaged(Atom), map: std.StringHashMapUnmanaged(Atom),
/// Maps an atom value to a string. Preallocated with `allocator_persistent`.
/// Maps an atom value to a string. Uses `allocator_general`.
array: std.ArrayList([:0]const u8), array: std.ArrayList([:0]const u8),
/// Protects all reads and writes to `map` and `array`. /// Protects all reads and writes to `map` and `array`.
mutex: std.Io.Mutex, mutex: std.Io.Mutex,
pub const max_atoms = 1 << @typeInfo(std.meta.Tag(Atom)).int.bits;
pub fn init() !*Atoms { pub fn init() !*Atoms {
const allocator_general = ctx.allocator_general;
const allocator_persistent = ctx.allocator_persistent; const allocator_persistent = ctx.allocator_persistent;
const atoms = try allocator_persistent.create(Atoms); const atoms = try allocator_persistent.create(Atoms);
errdefer allocator_persistent.destroy(atoms);
atoms.* = .{ atoms.* = .{
.map = .empty, .map = .empty,
@@ -40,26 +39,24 @@ pub fn init() !*Atoms {
.mutex = .init, .mutex = .init,
}; };
try atoms.map.ensureTotalCapacity(allocator_persistent, max_atoms);
try atoms.array.ensureTotalCapacityPrecise(allocator_persistent, max_atoms);
// VOLATILE The initial contents of `atoms.map` and `atoms.array` must // VOLATILE The initial contents of `atoms.map` and `atoms.array` must
// correspond to explicitly defined values at the top of the `Atom` type. // correspond to explicitly defined values at the top of the `Atom` type.
try atoms.map.put(allocator_general, "", .empty); atoms.map.putAssumeCapacityNoClobber("", .empty);
try atoms.array.append(allocator_general, ""); atoms.array.appendAssumeCapacity("");
return atoms; return atoms;
} }
pub fn deinit(self: *Atoms) void { pub fn deinit(self: *Atoms) void {
const allocator_general = ctx.allocator_general;
std.log.scoped(.deinit).debug("Deinitializing {*}", .{self}); std.log.scoped(.deinit).debug("Deinitializing {*}", .{self});
// No waiting; if atoms are in use while deinitializing, something is wrong. // No waiting; if atoms are in use while deinitializing, something is wrong.
std.debug.assert(self.mutex.tryLock()); std.debug.assert(self.mutex.tryLock());
self.map.deinit(allocator_general);
self.array.deinit(allocator_general);
self.* = undefined; self.* = undefined;
} }

View File

@@ -9,6 +9,15 @@ memory_type_index: u32,
allocated: usize, allocated: usize,
capacity: usize, capacity: usize,
/// Dummy `DeviceAllocation`, which isn't safe to use, but it is safe to call
/// `deinit` on it (once).
pub const empty: DeviceAllocation = .{
.device_memory = .null_handle,
.memory_type_index = vk.MAX_MEMORY_TYPES,
.allocated = 0,
.capacity = 0,
};
pub fn deinit(self: *DeviceAllocation) void { pub fn deinit(self: *DeviceAllocation) void {
const engine = ctx.engine; const engine = ctx.engine;

View File

@@ -196,11 +196,9 @@ pub fn init() !*Engine {
const physical_device_memory_properties = instance.getPhysicalDeviceMemoryProperties(physical_device); const physical_device_memory_properties = instance.getPhysicalDeviceMemoryProperties(physical_device);
var memory_types = try std.ArrayList(vk.MemoryType).initCapacity(allocator_persistent, vk.MAX_MEMORY_TYPES); var memory_types = try std.ArrayList(vk.MemoryType).initCapacity(allocator_persistent, vk.MAX_MEMORY_TYPES);
errdefer memory_types.deinit(allocator_persistent);
memory_types.appendSliceAssumeCapacity(physical_device_memory_properties.memory_types[0..physical_device_memory_properties.memory_type_count]); memory_types.appendSliceAssumeCapacity(physical_device_memory_properties.memory_types[0..physical_device_memory_properties.memory_type_count]);
var memory_heaps = try std.ArrayList(vk.MemoryHeap).initCapacity(allocator_persistent, vk.MAX_MEMORY_HEAPS); var memory_heaps = try std.ArrayList(vk.MemoryHeap).initCapacity(allocator_persistent, vk.MAX_MEMORY_HEAPS);
errdefer memory_heaps.deinit(allocator_persistent);
memory_heaps.appendSliceAssumeCapacity(physical_device_memory_properties.memory_heaps[0..physical_device_memory_properties.memory_heap_count]); memory_heaps.appendSliceAssumeCapacity(physical_device_memory_properties.memory_heaps[0..physical_device_memory_properties.memory_heap_count]);
// --- CREATE SURFACE ------------------------------------------------------ // --- CREATE SURFACE ------------------------------------------------------

View File

@@ -1,17 +1,17 @@
const Gui = @This(); const Gui = @This();
const std = @import("std"); const std = @import("std");
const ctx = @import("AppContext.zig"); const ctx = @import("../AppContext.zig");
const shaders = @import("shaders.zig"); const shaders = @import("../shaders.zig");
const vk = @import("vulkan"); const vk = @import("vulkan");
const vm = @import("vecmath"); const vm = @import("vecmath");
const CommandBuffer = @import("engine/CommandBuffer.zig"); const CommandBuffer = @import("CommandBuffer.zig");
const Engine = @import("engine/Engine.zig"); const Engine = @import("Engine.zig");
const GenericBuffer = @import("engine/GenericBuffer.zig").GenericBuffer; const GenericBuffer = @import("GenericBuffer.zig").GenericBuffer;
const Swapchain = @import("engine/Swapchain.zig"); const Swapchain = @import("Swapchain.zig");
const Texture = @import("engine/Texture.zig"); const Texture = @import("Texture.zig");
const Textures = @import("engine/Textures.zig"); const Textures = @import("Textures.zig");
pub const Draw = struct { pub const Draw = struct {
pub const Box = extern struct { pub const Box = extern struct {
@@ -74,13 +74,13 @@ pub const max_text_draws = 4096;
pub const max_batches = 1024; pub const max_batches = 1024;
pub fn init( pub fn init() !*Gui {
global_uniforms_buffer: vk.Buffer, const allocator_persistent = ctx.allocator_persistent;
) !Gui {
const allocator_general = ctx.allocator_general;
const engine = ctx.engine; const engine = ctx.engine;
const swapchain = ctx.swapchain; const swapchain = ctx.swapchain;
const gui = try allocator_persistent.create(Gui);
var box_gpu_buffer: GenericBuffer(void, Draw.Box) = try .init(.{ var box_gpu_buffer: GenericBuffer(void, Draw.Box) = try .init(.{
.usage = .storage, .usage = .storage,
.target_queue = .graphics, .target_queue = .graphics,
@@ -105,17 +105,10 @@ pub fn init(
}); });
errdefer image_gpu_buffer.deinit(); errdefer image_gpu_buffer.deinit();
var box_cpu_buffer: std.ArrayList(Draw.Box) = try .initCapacity(allocator_general, max_box_draws); const box_cpu_buffer: std.ArrayList(Draw.Box) = try .initCapacity(allocator_persistent, max_box_draws);
errdefer box_cpu_buffer.deinit(allocator_general); const text_cpu_buffer: std.ArrayList(Draw.Text) = try .initCapacity(allocator_persistent, max_text_draws);
const image_cpu_buffer: std.ArrayList(Draw.Image) = try .initCapacity(allocator_persistent, max_image_draws);
var text_cpu_buffer: std.ArrayList(Draw.Text) = try .initCapacity(allocator_general, max_text_draws); const batches: std.ArrayList(Batch) = try .initCapacity(allocator_persistent, max_batches);
errdefer text_cpu_buffer.deinit(allocator_general);
var image_cpu_buffer: std.ArrayList(Draw.Image) = try .initCapacity(allocator_general, max_image_draws);
errdefer image_cpu_buffer.deinit(allocator_general);
var batches: std.ArrayList(Batch) = try .initCapacity(allocator_general, max_batches);
errdefer batches.deinit(allocator_general);
const sampler = try engine.createSampler(.{ const sampler = try engine.createSampler(.{
.mag_filter = .linear, .mag_filter = .linear,
@@ -308,21 +301,6 @@ pub fn init(
try engine.updateDescriptorSets(.{ try engine.updateDescriptorSets(.{
.writes = &.{ .writes = &.{
.{
.dst_set = box_descriptor_set,
.dst_binding = 0,
.dst_array_element = 0,
.descriptor_type = .uniform_buffer,
.descriptor_infos = .{
.buffer = &.{
.{
.buffer = global_uniforms_buffer,
.offset = 0,
.range = vk.WHOLE_SIZE,
},
},
},
},
.{ .{
.dst_set = box_descriptor_set, .dst_set = box_descriptor_set,
.dst_binding = 1, .dst_binding = 1,
@@ -341,7 +319,7 @@ pub fn init(
}, },
}); });
return .{ gui.* = .{
.box_gpu_buffer = box_gpu_buffer, .box_gpu_buffer = box_gpu_buffer,
.text_gpu_buffer = text_gpu_buffer, .text_gpu_buffer = text_gpu_buffer,
.image_gpu_buffer = image_gpu_buffer, .image_gpu_buffer = image_gpu_buffer,
@@ -362,10 +340,11 @@ pub fn init(
.descriptor_pool = descriptor_pool, .descriptor_pool = descriptor_pool,
.box_descriptor_set = box_descriptor_set, .box_descriptor_set = box_descriptor_set,
}; };
return gui;
} }
pub fn deinit(self: *Gui) void { pub fn deinit(self: *Gui) void {
const allocator_general = ctx.allocator_general;
const engine = ctx.engine; const engine = ctx.engine;
std.log.scoped(.deinit).debug("Deinitializing {*}", .{self}); std.log.scoped(.deinit).debug("Deinitializing {*}", .{self});
@@ -377,12 +356,6 @@ pub fn deinit(self: *Gui) void {
engine.destroyDescriptorSetLayout(self.box_descriptor_set_layout); engine.destroyDescriptorSetLayout(self.box_descriptor_set_layout);
engine.destroySampler(self.sampler); engine.destroySampler(self.sampler);
self.batches.deinit(allocator_general);
self.image_cpu_buffer.deinit(allocator_general);
self.text_cpu_buffer.deinit(allocator_general);
self.box_cpu_buffer.deinit(allocator_general);
self.image_gpu_buffer.deinit(); self.image_gpu_buffer.deinit();
self.text_gpu_buffer.deinit(); self.text_gpu_buffer.deinit();
self.box_gpu_buffer.deinit(); self.box_gpu_buffer.deinit();

View File

@@ -58,7 +58,7 @@ pub const Key = struct {
filename: Atom, filename: Atom,
}; };
/// Maps a key value to a material ID. Preallocated with `allocator_general`. /// Maps a key value to a material ID. Preallocated with `allocator_persistent`.
map: std.AutoHashMapUnmanaged(Key, Id), map: std.AutoHashMapUnmanaged(Key, Id),
/// Stores all material data in a single contiguous storage buffer. Use the /// Stores all material data in a single contiguous storage buffer. Use the
/// material ID as an index into this buffer. /// material ID as an index into this buffer.
@@ -70,24 +70,30 @@ material_count: usize,
/// storage buffer should take 208 kiB in VRAM. /// storage buffer should take 208 kiB in VRAM.
pub const max_materials = 4096; pub const max_materials = 4096;
pub fn init() !Materials { pub fn init() !*Materials {
const allocator_general = ctx.allocator_general; const allocator_persistent = ctx.allocator_persistent;
var map: std.AutoHashMapUnmanaged(Key, Id) = .empty; const materials = try allocator_persistent.create(Materials);
errdefer map.deinit(allocator_general);
try map.ensureTotalCapacity(allocator_general, max_materials);
var material_buffer = try shaders.MaterialBuffer.init(.{ materials.* = .{
.map = .empty,
.material_buffer = undefined,
.material_count = 0,
};
try materials.map.ensureTotalCapacity(allocator_persistent, max_materials);
materials.material_buffer = try .init(.{
.usage = .storage, .usage = .storage,
.target_queue = .graphics, .target_queue = .graphics,
.array_capacity = max_materials, .array_capacity = max_materials,
.name = "Materials", .name = "Materials",
}); });
errdefer material_buffer.deinit(); errdefer materials.material_buffer.deinit();
// VOLATILE Synchronize with explicit values on top of `Id` type. // VOLATILE Synchronize with explicit values on top of `Id` type.
try material_buffer.write(.{ try materials.material_buffer.write(.{
.element_offset = Id.empty.toInt(), .element_offset = Id.empty.toInt(),
.elements = &.{ .elements = &.{
.{ .{
@@ -105,21 +111,16 @@ pub fn init() !Materials {
}, },
}, },
}); });
materials.material_count += 1;
return .{ std.debug.assert(materials.material_count == @typeInfo(Id).@"enum".fields.len);
.map = map, return materials;
.material_buffer = material_buffer,
.material_count = @typeInfo(Id).@"enum".fields.len,
};
} }
pub fn deinit(self: *Materials) void { pub fn deinit(self: *Materials) void {
const allocator_general = ctx.allocator_general;
std.log.scoped(.deinit).debug("Deinitializing {*}", .{self}); std.log.scoped(.deinit).debug("Deinitializing {*}", .{self});
self.material_buffer.deinit(); self.material_buffer.deinit();
self.map.deinit(allocator_general);
self.* = undefined; self.* = undefined;
} }
@@ -161,12 +162,7 @@ pub fn getAtom(self: *const Materials, filename: Atom) ?Id {
/// deinitialized or reset after this function returns. Note that during loading /// deinitialized or reset after this function returns. Note that during loading
/// the engine will make its own persistent allocations, so an out of memory /// the engine will make its own persistent allocations, so an out of memory
/// error is not necessarily related to `temp_allocator`. /// error is not necessarily related to `temp_allocator`.
pub fn getOrLoad( pub fn getOrLoad(self: *Materials, maybe_filename: ?[]const u8) !Id {
self: *Materials,
textures: *Textures,
stbi: *media.stbi,
maybe_filename: ?[]const u8,
) !Id {
if (maybe_filename) |filename| { if (maybe_filename) |filename| {
const key: Key = .{ const key: Key = .{
// If the material already exists, then the atom must exist and the // If the material already exists, then the atom must exist and the
@@ -183,7 +179,7 @@ pub fn getOrLoad(
const id = Id.fromIndexSafe(self.material_count) catch |err| switch (err) { const id = Id.fromIndexSafe(self.material_count) catch |err| switch (err) {
error.Overflow => return error.OutOfMaterials, error.Overflow => return error.OutOfMaterials,
}; };
try self.loadMaterial(textures, stbi, filename, id.toInt()); try self.loadMaterial(filename, id.toInt());
self.map.putAssumeCapacityNoClobber(key, id); self.map.putAssumeCapacityNoClobber(key, id);
self.material_count += 1; self.material_count += 1;
@@ -206,12 +202,7 @@ pub fn getOrLoad(
/// deinitialized or reset after this function returns. Note that during loading /// deinitialized or reset after this function returns. Note that during loading
/// the engine will make its own persistent allocations, so an out of memory /// the engine will make its own persistent allocations, so an out of memory
/// error is not necessarily related to `temp_allocator`. /// error is not necessarily related to `temp_allocator`.
pub fn getOrLoadAtom( pub fn getOrLoadAtom(self: *Materials, filename: Atom) !Id {
self: *Materials,
textures: *Textures,
stbi: *media.stbi,
filename: Atom,
) !Id {
if (filename != .empty) { if (filename != .empty) {
const key: Key = .{ const key: Key = .{
.filename = filename, .filename = filename,
@@ -226,7 +217,7 @@ pub fn getOrLoadAtom(
const id = Id.fromIndexSafe(self.material_count) catch |err| switch (err) { const id = Id.fromIndexSafe(self.material_count) catch |err| switch (err) {
error.Overflow => return error.OutOfMaterials, error.Overflow => return error.OutOfMaterials,
}; };
try self.loadMaterial(textures, stbi, filename.toString(), id.toInt()); try self.loadMaterial(filename.toString(), id.toInt());
self.map.putAssumeCapacityNoClobber(key, id); self.map.putAssumeCapacityNoClobber(key, id);
self.material_count += 1; self.material_count += 1;
@@ -248,11 +239,7 @@ pub fn getOrLoadAtom(
/// deinitialized or reset after this function returns. Note that during loading /// deinitialized or reset after this function returns. Note that during loading
/// the engine will make its own persistent allocations, so an out of memory /// the engine will make its own persistent allocations, so an out of memory
/// error is not necessarily related to `temp_allocator`. /// error is not necessarily related to `temp_allocator`.
pub fn loadAll( pub fn loadAll(self: *Materials) void {
self: *Materials,
textures: *Textures,
stbi: *media.stbi,
) void {
const io = ctx.io; const io = ctx.io;
const cwd = std.Io.Dir.cwd(); const cwd = std.Io.Dir.cwd();
@@ -273,15 +260,16 @@ pub fn loadAll(
continue; continue;
} }
_ = self.getOrLoad(textures, stbi, entry.name) catch |err| { _ = self.getOrLoad(entry.name) catch |err| {
std.log.err("Error while loading material entry {s}: {s}", .{ entry.name, @errorName(err) }); std.log.err("Error while loading material entry {s}: {s}", .{ entry.name, @errorName(err) });
}; };
} }
} }
fn loadMaterial(self: *Materials, textures: *Textures, stbi: *media.stbi, filename: []const u8, index: u32) !void { fn loadMaterial(self: *Materials, filename: []const u8, index: u32) !void {
const allocator_frame = ctx.allocator_frame; const allocator_frame = ctx.allocator_frame;
const io = ctx.io; const io = ctx.io;
const textures = ctx.textures;
const MaterialJson = struct { const MaterialJson = struct {
baseColor: [3]f32 = .{ 1, 1, 1 }, baseColor: [3]f32 = .{ 1, 1, 1 },
@@ -330,10 +318,10 @@ fn loadMaterial(self: *Materials, textures: *Textures, stbi: *media.stbi, filena
.normal_scale = material_json.normalScale, .normal_scale = material_json.normalScale,
.occlusion_texture_strength = material_json.occlusionTextureStrength, .occlusion_texture_strength = material_json.occlusionTextureStrength,
.roughness = material_json.roughness, .roughness = material_json.roughness,
.base_color_texture = try textures.getOrLoad(stbi, material_json.baseColorTexture, .base_color), .base_color_texture = try textures.getOrLoad(material_json.baseColorTexture, .base_color),
.emissive_texture = try textures.getOrLoad(stbi, material_json.emissiveTexture, .emissive), .emissive_texture = try textures.getOrLoad(material_json.emissiveTexture, .emissive),
.normal_texture = try textures.getOrLoad(stbi, material_json.normalTexture, .normal), .normal_texture = try textures.getOrLoad(material_json.normalTexture, .normal),
.occlusion_roughness_metallic_texture = try textures.getOrLoad(stbi, material_json.occlusionRoughnessMetallicTexture, .occlusion_roughness_metallic), .occlusion_roughness_metallic_texture = try textures.getOrLoad(material_json.occlusionRoughnessMetallicTexture, .occlusion_roughness_metallic),
}, },
}, },
}); });

View File

@@ -30,12 +30,12 @@ pipeline: vk.Pipeline,
pub fn load( pub fn load(
filename: []const u8, filename: []const u8,
stbi: *media.stbi,
cube_size: u32, cube_size: u32,
global_uniforms_buffer: vk.Buffer, global_uniforms_buffer: vk.Buffer,
) !Skybox { ) !Skybox {
const allocator_frame = ctx.allocator_frame; const allocator_frame = ctx.allocator_frame;
const io = ctx.io; const io = ctx.io;
const stbi = ctx.stbi;
const engine = ctx.engine; const engine = ctx.engine;
const swapchain = ctx.swapchain; const swapchain = ctx.swapchain;

View File

@@ -59,10 +59,10 @@ pub const Key = struct {
usage: Texture.Usage, usage: Texture.Usage,
}; };
/// Maps a key value to a texture ID. Preallocated with `allocator_general`. /// Maps a key value to a texture ID. Preallocated with `allocator_persistent`.
map: std.AutoHashMapUnmanaged(Key, Id), map: std.AutoHashMapUnmanaged(Key, Id),
/// Stores all `Texture` structs and maps a texture ID to a `Texture` struct. /// Stores all `Texture` structs and maps a texture ID to a `Texture` struct.
/// Preallocated with `allocator_general`. /// Preallocated with `allocator_persistent`.
array: std.ArrayList(Texture), array: std.ArrayList(Texture),
device_allocation: DeviceAllocation, device_allocation: DeviceAllocation,
@@ -70,23 +70,28 @@ pub const max_textures = 4096;
/// Enough for 4096 textures of usage `.base_color` and 64×64 dimensions. /// Enough for 4096 textures of usage `.base_color` and 64×64 dimensions.
pub const max_memory = 64 * 1024 * 1024; pub const max_memory = 64 * 1024 * 1024;
pub fn init() !Textures { pub fn init() !*Textures {
const allocator_general = ctx.allocator_general; const allocator_persistent = ctx.allocator_persistent;
const engine = ctx.engine; const engine = ctx.engine;
var map: std.AutoHashMapUnmanaged(Key, Id) = .empty; const textures = try allocator_persistent.create(Textures);
errdefer map.deinit(allocator_general);
try map.ensureTotalCapacity(allocator_general, max_textures); textures.* = .{
.map = .empty,
.array = .empty,
.device_allocation = .empty,
};
var array: std.ArrayList(Texture) = try .initCapacity(allocator_general, max_textures);
errdefer { errdefer {
for (array.items) |*texture| { for (textures.array.items) |*texture| {
texture.deinit(); texture.deinit();
} }
array.deinit(allocator_general); textures.device_allocation.deinit();
} }
var device_allocation = try engine.allocateForImage( try textures.map.ensureTotalCapacity(allocator_persistent, max_textures);
try textures.array.ensureTotalCapacityPrecise(allocator_persistent, max_textures);
textures.device_allocation = try engine.allocateForImage(
.{ .{
.image_type = .@"2d", .image_type = .@"2d",
.format = .r8g8b8a8_unorm, .format = .r8g8b8a8_unorm,
@@ -113,7 +118,6 @@ pub fn init() !Textures {
max_memory, max_memory,
.{ .device_local_bit = true }, .{ .device_local_bit = true },
); );
errdefer engine.freeMemory(device_allocation.device_memory);
// VOLATILE Synchronize with explicit values on top of `Id` type. // VOLATILE Synchronize with explicit values on top of `Id` type.
@@ -122,64 +126,56 @@ pub fn init() !Textures {
.height = 1, .height = 1,
.usage = .base_color, .usage = .base_color,
.target_queue = .graphics, .target_queue = .graphics,
.device_allocation = &device_allocation, .device_allocation = &textures.device_allocation,
.name = "@Empty", .name = "@Empty",
}); });
array.appendAssumeCapacity(empty_base_color_texture); textures.array.appendAssumeCapacity(empty_base_color_texture);
const empty_emissive_texture = try Texture.init(.{ const empty_emissive_texture = try Texture.init(.{
.width = 1, .width = 1,
.height = 1, .height = 1,
.usage = .emissive, .usage = .emissive,
.target_queue = .graphics, .target_queue = .graphics,
.device_allocation = &device_allocation, .device_allocation = &textures.device_allocation,
.name = "@Empty", .name = "@Empty",
}); });
array.appendAssumeCapacity(empty_emissive_texture); textures.array.appendAssumeCapacity(empty_emissive_texture);
const empty_normal_texture = try Texture.init(.{ const empty_normal_texture = try Texture.init(.{
.width = 1, .width = 1,
.height = 1, .height = 1,
.usage = .normal, .usage = .normal,
.target_queue = .graphics, .target_queue = .graphics,
.device_allocation = &device_allocation, .device_allocation = &textures.device_allocation,
.name = "@Empty", .name = "@Empty",
}); });
array.appendAssumeCapacity(empty_normal_texture); textures.array.appendAssumeCapacity(empty_normal_texture);
const empty_occlusuion_roughness_metallic_texture = try Texture.init(.{ const empty_occlusuion_roughness_metallic_texture = try Texture.init(.{
.width = 1, .width = 1,
.height = 1, .height = 1,
.usage = .occlusion_roughness_metallic, .usage = .occlusion_roughness_metallic,
.target_queue = .graphics, .target_queue = .graphics,
.device_allocation = &device_allocation, .device_allocation = &textures.device_allocation,
.name = "@Empty", .name = "@Empty",
}); });
array.appendAssumeCapacity(empty_occlusuion_roughness_metallic_texture); textures.array.appendAssumeCapacity(empty_occlusuion_roughness_metallic_texture);
try empty_base_color_texture.writeSamples(u8, &.{ 255, 255, 255, 255 }); try empty_base_color_texture.writeSamples(u8, &.{ 255, 255, 255, 255 });
try empty_emissive_texture.writeSamples(f16, &.{ 1.0, 1.0, 1.0, 1.0 }); try empty_emissive_texture.writeSamples(f16, &.{ 1.0, 1.0, 1.0, 1.0 });
try empty_normal_texture.writeSamples(i8, &.{ 0, 0, 127, 127 }); try empty_normal_texture.writeSamples(i8, &.{ 0, 0, 127, 127 });
try empty_occlusuion_roughness_metallic_texture.writeSamples(u8, &.{ 255, 255, 255, 255 }); try empty_occlusuion_roughness_metallic_texture.writeSamples(u8, &.{ 255, 255, 255, 255 });
return .{ return textures;
.map = map,
.array = array,
.device_allocation = device_allocation,
};
} }
pub fn deinit(self: *Textures) void { pub fn deinit(self: *Textures) void {
const allocator_general = ctx.allocator_general;
std.log.scoped(.deinit).debug("Deinitializing {*}", .{self}); std.log.scoped(.deinit).debug("Deinitializing {*}", .{self});
for (self.array.items) |*texture| { for (self.array.items) |*texture| {
texture.deinit(); texture.deinit();
} }
self.device_allocation.deinit(); self.device_allocation.deinit();
self.array.deinit(allocator_general);
self.map.deinit(allocator_general);
self.* = undefined; self.* = undefined;
} }
@@ -217,12 +213,7 @@ pub fn getAtom(self: *const Textures, filename: Atom, usage: Texture.Usage) ?Id
/// if necessary. Will not return any error if the texture already exists. When /// if necessary. Will not return any error if the texture already exists. When
/// the filename is `null`, returns an empty texture ID appropriate for given /// the filename is `null`, returns an empty texture ID appropriate for given
/// usage. /// usage.
pub fn getOrLoad( pub fn getOrLoad(self: *Textures, maybe_filename: ?[]const u8, usage: Texture.Usage) !Id {
self: *Textures,
stbi: *media.stbi,
maybe_filename: ?[]const u8,
usage: Texture.Usage,
) !Id {
if (maybe_filename) |filename| { if (maybe_filename) |filename| {
const key: Key = .{ const key: Key = .{
// If the texture already exists, then the atom must exist and the // If the texture already exists, then the atom must exist and the
@@ -240,7 +231,7 @@ pub fn getOrLoad(
const id = Id.fromIndexSafe(self.array.items.len) catch |err| switch (err) { const id = Id.fromIndexSafe(self.array.items.len) catch |err| switch (err) {
error.Overflow => return error.OutOfTextures, error.Overflow => return error.OutOfTextures,
}; };
const texture = try loadTexture(stbi, filename, usage, &self.device_allocation); const texture = try loadTexture(filename, usage, &self.device_allocation);
self.map.putAssumeCapacityNoClobber(key, id); self.map.putAssumeCapacityNoClobber(key, id);
self.array.appendAssumeCapacity(texture); self.array.appendAssumeCapacity(texture);
@@ -257,20 +248,16 @@ pub fn getOrLoad(
/// if necessary. Will not return any error if the texture already exists. When /// if necessary. Will not return any error if the texture already exists. When
/// the filename is `.empty`, returns an empty texture ID appropriate for given /// the filename is `.empty`, returns an empty texture ID appropriate for given
/// usage. /// usage.
pub fn getOrLoadAtom( pub fn getOrLoadAtom(self: *Textures, filename: Atom, usage: Texture.Usage) !Id {
self: *Textures,
stbi: *media.stbi,
filename: Atom,
usage: Texture.Usage,
) !Id {
if (filename != .empty) { if (filename != .empty) {
const key: Key = .{ const key: Key = .{
.filename = filename, .filename = filename,
.usage = usage, .usage = usage,
}; };
// We don't use `getOrPutAssumeCapacity` method, because we might already be // We don't use `getOrPutAssumeCapacity` method, because we might
// at full capacity, in which case we should return `error.OutOfTextures`. // already be at full capacity, in which case we should return
// `error.OutOfTextures`.
if (self.map.get(key)) |id| { if (self.map.get(key)) |id| {
return id; return id;
@@ -278,7 +265,7 @@ pub fn getOrLoadAtom(
const id = Id.fromIndexSafe(self.array.items.len) catch |err| switch (err) { const id = Id.fromIndexSafe(self.array.items.len) catch |err| switch (err) {
error.Overflow => return error.OutOfTextures, error.Overflow => return error.OutOfTextures,
}; };
const texture = try loadTexture(stbi, filename.toString(), usage, &self.device_allocation); const texture = try loadTexture(filename.toString(), usage, &self.device_allocation);
self.map.putAssumeCapacityNoClobber(key, id); self.map.putAssumeCapacityNoClobber(key, id);
self.array.appendAssumeCapacity(texture); self.array.appendAssumeCapacity(texture);
@@ -291,12 +278,12 @@ pub fn getOrLoadAtom(
} }
fn loadTexture( fn loadTexture(
stbi: *media.stbi,
filename: []const u8, filename: []const u8,
usage: Texture.Usage, usage: Texture.Usage,
device_allocation: *DeviceAllocation, device_allocation: *DeviceAllocation,
) !Texture { ) !Texture {
const io = ctx.io; const io = ctx.io;
const stbi = ctx.stbi;
std.log.debug("Loading texture \"{s}\" as {s}...", .{ filename, @tagName(usage) }); std.log.debug("Loading texture \"{s}\" as {s}...", .{ filename, @tagName(usage) });

View File

@@ -3,6 +3,7 @@ const std = @import("std");
const c = @import("const.zig"); const c = @import("const.zig");
const ctx = @import("AppContext.zig"); const ctx = @import("AppContext.zig");
const glfw = @import("zglfw"); const glfw = @import("zglfw");
const media = @import("media");
const vk = @import("vulkan"); const vk = @import("vulkan");
const Atoms = @import("engine/Atoms.zig"); const Atoms = @import("engine/Atoms.zig");
@@ -37,6 +38,9 @@ pub fn main(init: std.process.Init) !void {
ctx.vk_allocator.deinit(); ctx.vk_allocator.deinit();
ctx.vk_allocator = undefined; ctx.vk_allocator = undefined;
} }
ctx.stbi = try ctx.allocator_persistent.create(media.stbi);
ctx.stbi.* = .init(ctx.allocator_general, ctx.io);
defer ctx.stbi.deinit();
// --- INITIALIZE APP CONTEXT --- // --- INITIALIZE APP CONTEXT ---
@@ -89,6 +93,24 @@ pub fn main(init: std.process.Init) !void {
ctx.swapchain = undefined; ctx.swapchain = undefined;
} }
ctx.textures = try .init();
defer {
ctx.textures.deinit();
ctx.textures = undefined;
}
ctx.materials = try .init();
defer {
ctx.materials.deinit();
ctx.materials = undefined;
}
ctx.gui = try .init();
defer {
ctx.gui.deinit();
ctx.gui = undefined;
}
// --- INITIALIZE THE GAME --- // --- INITIALIZE THE GAME ---
var game = try Game.init(); var game = try Game.init();
@@ -117,6 +139,7 @@ pub fn main(init: std.process.Init) !void {
const dt: f32 = @floatCast(t2 - t1); const dt: f32 = @floatCast(t2 - t1);
t1 = t2; t1 = t2;
ctx.gui.beginFrame();
game.update(dt); game.update(dt);
_ = arena_frame.reset(.retain_capacity); _ = arena_frame.reset(.retain_capacity);
} }