Make use of some globals to stop typing engine everywhere.

This commit is contained in:
2026-05-15 02:20:08 +02:00
parent 36434f8107
commit ad80fb4fd9
21 changed files with 902 additions and 798 deletions

View File

@@ -1,6 +1,7 @@
const Skybox = @This();
const std = @import("std");
const ctx = @import("../AppContext.zig");
const media = @import("media");
const shaders = @import("../shaders.zig");
const vk = @import("vulkan");
@@ -29,14 +30,15 @@ pipeline: vk.Pipeline,
pub fn load(
filename: []const u8,
engine: *Engine,
swapchain: *Swapchain,
stbi: *media.stbi,
cube_size: u32,
global_uniforms_buffer: vk.Buffer,
temp_allocator: std.mem.Allocator,
io: std.Io,
) !Skybox {
const allocator_frame = ctx.allocator_frame;
const io = ctx.io;
const engine = ctx.engine;
const swapchain = ctx.swapchain;
std.log.debug("Loading skybox \"{s}\"...", .{filename});
// --- LOAD IMAGE FROM MEMORY ----------------------------------------------
@@ -46,8 +48,7 @@ pub fn load(
var dir = try cwd.openDir(io, "assets", .{});
defer dir.close(io);
const file_buf = try dir.readFileAlloc(io, filename, temp_allocator, .unlimited);
defer temp_allocator.free(file_buf);
const file_buf = try dir.readFileAlloc(io, filename, allocator_frame, .unlimited);
const img = try stbi.loadHdrBuf(file_buf);
defer stbi.freeHdr(img);
@@ -68,15 +69,15 @@ pub fn load(
// --- LOAD IMAGE INTO STAGING BUFFER --------------------------------------
var staging_buffer = try StagingBuffer.init(engine, .{
var staging_buffer = try StagingBuffer.init(.{
.capacity = @intCast(img.width * img.height * @sizeOf(vm.ColorHdr)),
.target_queue = .compute,
});
defer staging_buffer.deinit(engine);
defer staging_buffer.deinit();
const staging_memory = try staging_buffer.map(engine);
const staging_memory = try staging_buffer.map();
@memcpy(staging_memory, @as([*]const u8, @ptrCast(img.data)));
staging_buffer.unmap(engine);
staging_buffer.unmap();
// --- CREATE EQUIRECTANGULAR IMAGE ----------------------------------------
@@ -126,8 +127,8 @@ pub fn load(
// --- TRANSITION TO TRANSFER_DST_OPTIMAL AND COPY -------------------------
var transfer_command_buffer = try CommandBuffer.init(engine, .transfer);
defer transfer_command_buffer.deinit(engine);
var transfer_command_buffer = try CommandBuffer.init(.transfer);
defer transfer_command_buffer.deinit();
try transfer_command_buffer.beginCommandBuffer();
transfer_command_buffer.pipelineBarrier(.{
@@ -174,14 +175,14 @@ pub fn load(
);
try transfer_command_buffer.endCommandBuffer();
try transfer_command_buffer.submit(engine, .{
try transfer_command_buffer.submit(.{
.signal_semaphores = &.{semaphore_transfer_transition},
});
// --- TRANSITION TO SHADER_READ_ONLY_OPTIMAL ------------------------------
var transition1_command_buffer = try CommandBuffer.init(engine, .compute);
defer transition1_command_buffer.deinit(engine);
var transition1_command_buffer = try CommandBuffer.init(.compute);
defer transition1_command_buffer.deinit();
try transition1_command_buffer.beginCommandBuffer();
transition1_command_buffer.pipelineBarrier(.{
@@ -208,7 +209,7 @@ pub fn load(
});
try transition1_command_buffer.endCommandBuffer();
try transition1_command_buffer.submit(engine, .{
try transition1_command_buffer.submit(.{
.wait_semaphores = &.{.{ .semaphore = semaphore_transfer_transition }},
.signal_semaphores = &.{semaphore_transition_compute},
});
@@ -326,7 +327,7 @@ pub fn load(
.base_pipeline_handle = .null_handle,
.base_pipeline_index = -1,
},
}, &engine.vk_allocator.interface, @ptrCast(&compute_pipeline));
}, ctx.vk_allocator.capture(), @ptrCast(&compute_pipeline));
defer engine.destroyPipeline(compute_pipeline);
const compute_descriptor_pool = try engine.createDescriptorPool(.{
@@ -392,8 +393,8 @@ pub fn load(
// 1. Run compute shader
var compute_command_buffer = try CommandBuffer.init(engine, .compute);
defer compute_command_buffer.deinit(engine);
var compute_command_buffer = try CommandBuffer.init(.compute);
defer compute_command_buffer.deinit();
try compute_command_buffer.beginCommandBuffer();
compute_command_buffer.pipelineBarrier(.{
@@ -423,7 +424,7 @@ pub fn load(
compute_command_buffer.proxy.dispatch(@divExact(cube_size, 8), @divExact(cube_size, 8), 6);
try compute_command_buffer.endCommandBuffer();
try compute_command_buffer.submit(engine, .{
try compute_command_buffer.submit(.{
.wait_semaphores = &.{
.{
.semaphore = semaphore_transition_compute,
@@ -435,8 +436,8 @@ pub fn load(
// 2. Transition cubemap
var transition2_command_buffer = try CommandBuffer.init(engine, .graphics);
defer transition2_command_buffer.deinit(engine);
var transition2_command_buffer = try CommandBuffer.init(.graphics);
defer transition2_command_buffer.deinit();
try transition2_command_buffer.beginCommandBuffer();
transition2_command_buffer.pipelineBarrier(.{
@@ -463,7 +464,7 @@ pub fn load(
});
try transition2_command_buffer.endCommandBuffer();
try transition2_command_buffer.submit(engine, .{
try transition2_command_buffer.submit(.{
.wait_semaphores = &.{.{ .semaphore = semaphore_compute_transition }},
.fence = fence,
});
@@ -474,12 +475,12 @@ pub fn load(
// --- SKYBOX PIPELINE -----------------------------------------------------
var vertex_buffer = try GenericBuffer(void, vm.Vector3).init(engine, .{
var vertex_buffer = try GenericBuffer(void, vm.Vector3).init(.{
.usage = .vertex,
.target_queue = .graphics,
.array_capacity = 8,
});
errdefer vertex_buffer.deinit(engine);
errdefer vertex_buffer.deinit();
// 6━━━━7
// ╱│ ╱┃
@@ -489,7 +490,7 @@ pub fn load(
// ┃╱ ┃╱ │╱
// 0━━━━1 O────X
try vertex_buffer.write(engine, .{
try vertex_buffer.write(.{
.elements = &.{
.init(-1, -1, -1),
.init(1, -1, -1),
@@ -502,14 +503,14 @@ pub fn load(
},
});
var index_buffer = try GenericBuffer(void, u16).init(engine, .{
var index_buffer = try GenericBuffer(void, u16).init(.{
.usage = .index,
.target_queue = .graphics,
.array_capacity = 36,
});
errdefer index_buffer.deinit(engine);
errdefer index_buffer.deinit();
try index_buffer.write(engine, .{
try index_buffer.write(.{
.elements = &.{
// Positive X
3, 1, 7,
@@ -760,15 +761,17 @@ pub fn load(
};
}
pub fn deinit(self: *Skybox, engine: *Engine) void {
pub fn deinit(self: *Skybox) void {
const engine = ctx.engine;
engine.destroyPipeline(self.pipeline);
engine.destroyPipelineLayout(self.pipeline_layout);
engine.destroyDescriptorPool(self.descriptor_pool);
engine.destroyDescriptorSetLayout(self.descriptor_set_layout);
engine.destroySampler(self.sampler);
self.index_buffer.deinit(engine);
self.vertex_buffer.deinit(engine);
self.index_buffer.deinit();
self.vertex_buffer.deinit();
engine.destroyImageView(self.image_view);
engine.freeMemory(self.device_memory);