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

@@ -35,7 +35,6 @@ pub const Atom = enum(u16) {
/// new one, if necessary. This will always produce a valid atom. Will not
/// return any error if the atom already exists.
pub fn fromString(string: []const u8) error{ OutOfMemory, OutOfAtoms }!Atom {
const allocator_general = ctx.allocator_general;
const allocator_persistent = ctx.allocator_persistent;
const io = ctx.io;
const atoms = ctx.atoms;
@@ -43,23 +42,21 @@ pub const Atom = enum(u16) {
atoms.mutex.lockUncancelable(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) {
return entry.value_ptr.*;
if (atoms.map.get(string)) |atom| {
return atom;
} else {
errdefer _ = atoms.map.remove(string);
const atom = Atom.fromIndexSafe(atoms.array.items.len) catch |err| switch (err) {
error.Overflow => return error.OutOfAtoms,
};
try atoms.array.ensureUnusedCapacity(allocator_general, 1);
const owned_string = try allocator_persistent.dupeZ(u8, string);
entry.key_ptr.* = owned_string;
entry.value_ptr.* = atom;
atoms.map.putAssumeCapacityNoClobber(owned_string, atom);
atoms.array.appendAssumeCapacity(owned_string);
return atom;
}
}

View File

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

View File

@@ -9,6 +9,15 @@ memory_type_index: u32,
allocated: 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 {
const engine = ctx.engine;

View File

@@ -196,11 +196,9 @@ pub fn init() !*Engine {
const physical_device_memory_properties = instance.getPhysicalDeviceMemoryProperties(physical_device);
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]);
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]);
// --- CREATE SURFACE ------------------------------------------------------

435
src/engine/Gui.zig Normal file
View File

@@ -0,0 +1,435 @@
const Gui = @This();
const std = @import("std");
const ctx = @import("../AppContext.zig");
const shaders = @import("../shaders.zig");
const vk = @import("vulkan");
const vm = @import("vecmath");
const CommandBuffer = @import("CommandBuffer.zig");
const Engine = @import("Engine.zig");
const GenericBuffer = @import("GenericBuffer.zig").GenericBuffer;
const Swapchain = @import("Swapchain.zig");
const Texture = @import("Texture.zig");
const Textures = @import("Textures.zig");
pub const Draw = struct {
pub const Box = extern struct {
background_color: vm.Vector4,
border_color: vm.Vector4,
position_sspx: vm.Vector2,
size_px: vm.Vector2,
border_width_px: f32,
border_radius_px: f32,
};
pub const Text = extern struct {
color: vm.Vector4,
pos_min: vm.Vector2,
pos_max: vm.Vector2,
uv_min: vm.Vector2,
uv_max: vm.Vector2,
atlas_id: Textures.Id,
};
pub const Image = extern struct {
tint: vm.Vector4,
position_sspx: vm.Vector2,
size_px: vm.Vector2,
uv_min: vm.Vector2,
uv_max: vm.Vector2,
texture_id: Textures.Id,
};
};
pub const Batch = struct {
draw_type: std.meta.DeclEnum(Draw),
first_instance: u32,
instance_count: u32,
};
box_gpu_buffer: GenericBuffer(void, Draw.Box),
text_gpu_buffer: GenericBuffer(void, Draw.Text),
image_gpu_buffer: GenericBuffer(void, Draw.Image),
box_cpu_buffer: std.ArrayList(Draw.Box),
text_cpu_buffer: std.ArrayList(Draw.Text),
image_cpu_buffer: std.ArrayList(Draw.Image),
batches: std.ArrayList(Batch),
sampler: vk.Sampler,
index_buffer: shaders.IndexBuffer,
box_descriptor_set_layout: vk.DescriptorSetLayout,
box_pipeline_layout: vk.PipelineLayout,
box_pipeline: vk.Pipeline,
descriptor_pool: vk.DescriptorPool,
box_descriptor_set: vk.DescriptorSet,
pub const max_box_draws = 1024;
pub const max_image_draws = 1024;
pub const max_text_draws = 4096;
pub const max_batches = 1024;
pub fn init() !*Gui {
const allocator_persistent = ctx.allocator_persistent;
const engine = ctx.engine;
const swapchain = ctx.swapchain;
const gui = try allocator_persistent.create(Gui);
var box_gpu_buffer: GenericBuffer(void, Draw.Box) = try .init(.{
.usage = .storage,
.target_queue = .graphics,
.array_capacity = max_box_draws,
.name = "GUI box draws",
});
errdefer box_gpu_buffer.deinit();
var text_gpu_buffer: GenericBuffer(void, Draw.Text) = try .init(.{
.usage = .storage,
.target_queue = .graphics,
.array_capacity = max_text_draws,
.name = "GUI text draws",
});
errdefer text_gpu_buffer.deinit();
var image_gpu_buffer: GenericBuffer(void, Draw.Image) = try .init(.{
.usage = .storage,
.target_queue = .graphics,
.array_capacity = max_image_draws,
.name = "GUI image draws",
});
errdefer image_gpu_buffer.deinit();
const box_cpu_buffer: std.ArrayList(Draw.Box) = try .initCapacity(allocator_persistent, max_box_draws);
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);
const batches: std.ArrayList(Batch) = try .initCapacity(allocator_persistent, max_batches);
const sampler = try engine.createSampler(.{
.mag_filter = .linear,
.min_filter = .linear,
.mipmap_mode = .linear,
.address_mode_u = .repeat,
.address_mode_v = .repeat,
.address_mode_w = .repeat,
.mip_lod_bias = 0,
.anisotropy_enable = .false,
.max_anisotropy = 0,
.compare_enable = .false,
.compare_op = .always,
.min_lod = 0,
.max_lod = vk.LOD_CLAMP_NONE,
.border_color = .float_transparent_black,
.unnormalized_coordinates = .false,
});
errdefer engine.destroySampler(sampler);
const box_descriptor_set_layout = try engine.createDescriptorSetLayout(.{
.bindings = &.{
.{
.binding = 0,
.descriptor_type = .uniform_buffer,
.descriptor_count = 1,
.stage_flags = .{ .vertex_bit = true, .fragment_bit = true },
},
.{
.binding = 1,
.descriptor_type = .storage_buffer,
.descriptor_count = 1,
.stage_flags = .{ .vertex_bit = true, .fragment_bit = true },
},
},
});
errdefer engine.destroyDescriptorSetLayout(box_descriptor_set_layout);
engine.setObjectName(box_descriptor_set_layout, "DSL GUI Box", .{});
const box_pipeline_layout = try engine.createPipelineLayout(.{
.set_layouts = &.{
box_descriptor_set_layout,
},
});
errdefer engine.destroyPipelineLayout(box_pipeline_layout);
engine.setObjectName(box_pipeline_layout, "PL GUI Box", .{});
const box_vertex_shader = try engine.createShaderModule(.{ .code = &shaders.gui_box_vert_spv });
defer engine.destroyShaderModule(box_vertex_shader);
engine.setObjectName(box_vertex_shader, "SM gui_box_vert", .{});
const box_fragment_shader = try engine.createShaderModule(.{ .code = &shaders.gui_box_frag_spv });
defer engine.destroyShaderModule(box_fragment_shader);
engine.setObjectName(box_fragment_shader, "SM gui_box_frag", .{});
var index_buffer = try shaders.IndexBuffer.init(.{
.usage = .index,
.target_queue = .graphics,
.array_capacity = 6,
.name = "QuadIB",
});
errdefer index_buffer.deinit();
try index_buffer.write(.{
.elements = &.{ 0, 1, 2, 2, 1, 3 },
});
const box_pipeline = try engine.createGraphicsPipeline(.{
.stages = &.{
.{
.stage = .{ .vertex_bit = true },
.module = box_vertex_shader,
.name = "main",
},
.{
.stage = .{ .fragment_bit = true },
.module = box_fragment_shader,
.name = "main",
},
},
.vertex_input_state = .{},
.input_assembly_state = .{
.topology = .triangle_list,
.primitive_restart_enable = .false,
},
.viewport_state = .{
.viewports = &.{undefined}, // dynamic
.scissors = &.{undefined}, // dynamic
},
.rasterization_state = .{
.depth_clamp_enable = .false,
.rasterizer_discard_enable = .false,
.polygon_mode = .fill,
.cull_mode = .{},
.front_face = .counter_clockwise,
.depth_bias_enable = .false,
.depth_bias_constant_factor = 0,
.depth_bias_clamp = 0,
.depth_bias_slope_factor = 0,
.line_width = 1,
},
.multisample_state = .{
.rasterization_samples = .{ .@"1_bit" = true },
.sample_shading_enable = .false,
.min_sample_shading = 1,
.alpha_to_coverage_enable = .false,
.alpha_to_one_enable = .false,
},
.depth_stencil_state = .{
.depth_test_enable = .false,
.depth_write_enable = .false,
.depth_compare_op = .always,
.depth_bounds_test_enable = .false,
.stencil_test_enable = .false,
.front = .{
.fail_op = .keep,
.pass_op = .keep,
.depth_fail_op = .keep,
.compare_op = .never,
.compare_mask = 0,
.write_mask = 0,
.reference = 0,
},
.back = .{
.fail_op = .keep,
.pass_op = .keep,
.depth_fail_op = .keep,
.compare_op = .never,
.compare_mask = 0,
.write_mask = 0,
.reference = 0,
},
.min_depth_bounds = 0,
.max_depth_bounds = 1,
},
.color_blend_state = .{
.logic_op_enable = .false,
.logic_op = .copy,
.attachments = &.{
.{
.blend_enable = .true,
.src_color_blend_factor = .one,
.dst_color_blend_factor = .one_minus_src_alpha,
.color_blend_op = .add,
.src_alpha_blend_factor = .one,
.dst_alpha_blend_factor = .one_minus_src_alpha,
.alpha_blend_op = .add,
.color_write_mask = .{
.r_bit = true,
.g_bit = true,
.b_bit = true,
.a_bit = true,
},
},
},
.blend_constants = .{ 0, 0, 0, 0 },
},
.dynamic_state = .{
.dynamic_states = &.{ .viewport, .scissor },
},
.layout = box_pipeline_layout,
.view_mask = 0,
.color_attachment_formats = &.{swapchain.params.surface_format.format},
.depth_attachment_format = Texture.Usage.depth.vkFormat(),
.stencil_attachment_format = .undefined,
});
errdefer engine.destroyPipeline(box_pipeline);
engine.setObjectName(box_pipeline, "P GUI Box", .{});
const descriptor_pool = try engine.createDescriptorPool(.{
.max_sets = 1,
.pool_sizes = &.{
.{
.type = .uniform_buffer,
.descriptor_count = 1,
},
.{
.type = .storage_buffer,
.descriptor_count = 1,
},
},
});
errdefer engine.destroyDescriptorPool(descriptor_pool);
engine.setObjectName(descriptor_pool, "DP GUI", .{});
const box_descriptor_set = try engine.allocateDescriptorSet(.{
.descriptor_pool = descriptor_pool,
.set_layout = box_descriptor_set_layout,
});
engine.setObjectName(box_descriptor_set, "DS GUI Box", .{});
try engine.updateDescriptorSets(.{
.writes = &.{
.{
.dst_set = box_descriptor_set,
.dst_binding = 1,
.dst_array_element = 0,
.descriptor_type = .storage_buffer,
.descriptor_infos = .{
.buffer = &.{
.{
.buffer = box_gpu_buffer.buffer,
.offset = 0,
.range = vk.WHOLE_SIZE,
},
},
},
},
},
});
gui.* = .{
.box_gpu_buffer = box_gpu_buffer,
.text_gpu_buffer = text_gpu_buffer,
.image_gpu_buffer = image_gpu_buffer,
.box_cpu_buffer = box_cpu_buffer,
.text_cpu_buffer = text_cpu_buffer,
.image_cpu_buffer = image_cpu_buffer,
.batches = batches,
.sampler = sampler,
.index_buffer = index_buffer,
.box_descriptor_set_layout = box_descriptor_set_layout,
.box_pipeline_layout = box_pipeline_layout,
.box_pipeline = box_pipeline,
.descriptor_pool = descriptor_pool,
.box_descriptor_set = box_descriptor_set,
};
return gui;
}
pub fn deinit(self: *Gui) void {
const engine = ctx.engine;
std.log.scoped(.deinit).debug("Deinitializing {*}", .{self});
engine.destroyDescriptorPool(self.descriptor_pool);
engine.destroyPipeline(self.box_pipeline);
self.index_buffer.deinit();
engine.destroyPipelineLayout(self.box_pipeline_layout);
engine.destroyDescriptorSetLayout(self.box_descriptor_set_layout);
engine.destroySampler(self.sampler);
self.image_gpu_buffer.deinit();
self.text_gpu_buffer.deinit();
self.box_gpu_buffer.deinit();
self.* = undefined;
}
pub fn beginFrame(self: *Gui) void {
self.box_cpu_buffer.clearRetainingCapacity();
self.text_cpu_buffer.clearRetainingCapacity();
self.image_cpu_buffer.clearRetainingCapacity();
self.batches.clearRetainingCapacity();
}
pub fn pushBox(self: *Gui, box: Draw.Box) void {
const instance: u32 = @intCast(self.box_cpu_buffer.items.len);
self.box_cpu_buffer.appendBounded(box) catch return;
self.extendOrAddBatch(.Box, instance);
}
pub fn pushText(self: *Gui, text: Draw.Text) void {
const instance: u32 = @intCast(self.text_cpu_buffer.items.len);
self.text_cpu_buffer.appendBounded(text) catch return;
self.extendOrAddBatch(.Text, instance);
}
pub fn pushImage(self: *Gui, image: Draw.Image) void {
const instance: u32 = @intCast(self.image_cpu_buffer.items.len);
self.image_cpu_buffer.appendBounded(image) catch return;
self.extendOrAddBatch(.Image, instance);
}
fn extendOrAddBatch(self: *Gui, draw_type: std.meta.DeclEnum(Draw), instance: u32) void {
if (self.batches.items.len == 0 or self.batches.items[self.batches.items.len - 1].draw_type != draw_type) {
self.batches.appendBounded(.{
.draw_type = draw_type,
.first_instance = instance,
.instance_count = 1,
}) catch return;
} else {
const batch = &self.batches.items[self.batches.items.len - 1];
std.debug.assert(batch.first_instance + batch.instance_count == instance);
batch.instance_count += 1;
}
}
pub fn draw(self: *const Gui, command_buffer: CommandBuffer) !void {
command_buffer.bindIndexBuffer(self.index_buffer.buffer, 0, .uint16);
try self.box_gpu_buffer.write(.{ .elements = self.box_cpu_buffer.items });
try self.text_gpu_buffer.write(.{ .elements = self.text_cpu_buffer.items });
try self.image_gpu_buffer.write(.{ .elements = self.image_cpu_buffer.items });
for (self.batches.items) |batch| {
switch (batch.draw_type) {
.Box => {
command_buffer.bindPipeline(.graphics, self.box_pipeline);
command_buffer.bindDescriptorSet(.graphics, self.box_pipeline_layout, 0, self.box_descriptor_set, null);
},
.Text => {
std.log.warn("GUI Text not implemented; skipping batch", .{});
continue;
},
.Image => {
std.log.warn("GUI Image not implemented; skipping batch", .{});
continue;
},
}
command_buffer.drawIndexed(.{
.index_count = 6,
.first_instance = batch.first_instance,
.instance_count = batch.instance_count,
});
}
}

View File

@@ -58,7 +58,7 @@ pub const Key = struct {
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),
/// Stores all material data in a single contiguous storage buffer. Use the
/// material ID as an index into this buffer.
@@ -70,24 +70,30 @@ material_count: usize,
/// storage buffer should take 208 kiB in VRAM.
pub const max_materials = 4096;
pub fn init() !Materials {
const allocator_general = ctx.allocator_general;
pub fn init() !*Materials {
const allocator_persistent = ctx.allocator_persistent;
var map: std.AutoHashMapUnmanaged(Key, Id) = .empty;
errdefer map.deinit(allocator_general);
try map.ensureTotalCapacity(allocator_general, max_materials);
const materials = try allocator_persistent.create(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,
.target_queue = .graphics,
.array_capacity = max_materials,
.name = "Materials",
});
errdefer material_buffer.deinit();
errdefer materials.material_buffer.deinit();
// VOLATILE Synchronize with explicit values on top of `Id` type.
try material_buffer.write(.{
try materials.material_buffer.write(.{
.element_offset = Id.empty.toInt(),
.elements = &.{
.{
@@ -105,21 +111,16 @@ pub fn init() !Materials {
},
},
});
materials.material_count += 1;
return .{
.map = map,
.material_buffer = material_buffer,
.material_count = @typeInfo(Id).@"enum".fields.len,
};
std.debug.assert(materials.material_count == @typeInfo(Id).@"enum".fields.len);
return materials;
}
pub fn deinit(self: *Materials) void {
const allocator_general = ctx.allocator_general;
std.log.scoped(.deinit).debug("Deinitializing {*}", .{self});
self.material_buffer.deinit();
self.map.deinit(allocator_general);
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
/// the engine will make its own persistent allocations, so an out of memory
/// error is not necessarily related to `temp_allocator`.
pub fn getOrLoad(
self: *Materials,
textures: *Textures,
stbi: *media.stbi,
maybe_filename: ?[]const u8,
) !Id {
pub fn getOrLoad(self: *Materials, maybe_filename: ?[]const u8) !Id {
if (maybe_filename) |filename| {
const key: Key = .{
// 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) {
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.material_count += 1;
@@ -206,12 +202,7 @@ pub fn getOrLoad(
/// deinitialized or reset after this function returns. Note that during loading
/// the engine will make its own persistent allocations, so an out of memory
/// error is not necessarily related to `temp_allocator`.
pub fn getOrLoadAtom(
self: *Materials,
textures: *Textures,
stbi: *media.stbi,
filename: Atom,
) !Id {
pub fn getOrLoadAtom(self: *Materials, filename: Atom) !Id {
if (filename != .empty) {
const key: Key = .{
.filename = filename,
@@ -226,7 +217,7 @@ pub fn getOrLoadAtom(
const id = Id.fromIndexSafe(self.material_count) catch |err| switch (err) {
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.material_count += 1;
@@ -248,11 +239,7 @@ pub fn getOrLoadAtom(
/// deinitialized or reset after this function returns. Note that during loading
/// the engine will make its own persistent allocations, so an out of memory
/// error is not necessarily related to `temp_allocator`.
pub fn loadAll(
self: *Materials,
textures: *Textures,
stbi: *media.stbi,
) void {
pub fn loadAll(self: *Materials) void {
const io = ctx.io;
const cwd = std.Io.Dir.cwd();
@@ -273,15 +260,16 @@ pub fn loadAll(
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) });
};
}
}
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 io = ctx.io;
const textures = ctx.textures;
const MaterialJson = struct {
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,
.occlusion_texture_strength = material_json.occlusionTextureStrength,
.roughness = material_json.roughness,
.base_color_texture = try textures.getOrLoad(stbi, material_json.baseColorTexture, .base_color),
.emissive_texture = try textures.getOrLoad(stbi, material_json.emissiveTexture, .emissive),
.normal_texture = try textures.getOrLoad(stbi, material_json.normalTexture, .normal),
.occlusion_roughness_metallic_texture = try textures.getOrLoad(stbi, material_json.occlusionRoughnessMetallicTexture, .occlusion_roughness_metallic),
.base_color_texture = try textures.getOrLoad(material_json.baseColorTexture, .base_color),
.emissive_texture = try textures.getOrLoad(material_json.emissiveTexture, .emissive),
.normal_texture = try textures.getOrLoad(material_json.normalTexture, .normal),
.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(
filename: []const u8,
stbi: *media.stbi,
cube_size: u32,
global_uniforms_buffer: vk.Buffer,
) !Skybox {
const allocator_frame = ctx.allocator_frame;
const io = ctx.io;
const stbi = ctx.stbi;
const engine = ctx.engine;
const swapchain = ctx.swapchain;

View File

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