Files
voxel-game/src/engine/Engine.zig
2026-05-22 23:34:25 +02:00

1363 lines
54 KiB
Zig

const Engine = @This();
const std = @import("std");
const c = @import("../const.zig");
const ctx = @import("../AppContext.zig");
const glfw = @import("zglfw");
const vk = @import("vulkan");
const Queue = @import("Queue.zig");
const QueueType = @import("QueueType.zig").QueueType;
const VkAllocator = @import("VkAllocator.zig");
const WaitSemaphore = @import("WaitSemaphore.zig");
const QueueAllocator = struct {
queue_families_properties_buffer: [max_queue_families]vk.QueueFamilyProperties,
queue_families_in_use: [max_queue_families]u32,
queue_families_count: u32,
const max_queue_families = 16;
pub fn init(instance: vk.InstanceProxy, physical_device: vk.PhysicalDevice) QueueAllocator {
var queue_families_properties_buffer: [max_queue_families]vk.QueueFamilyProperties = undefined;
var queue_families_count: u32 = max_queue_families;
instance.getPhysicalDeviceQueueFamilyProperties(physical_device, &queue_families_count, &queue_families_properties_buffer);
var queue_families_in_use: [max_queue_families]u32 = undefined;
@memset(queue_families_in_use[0..queue_families_count], 0);
return .{
.queue_families_properties_buffer = queue_families_properties_buffer,
.queue_families_in_use = queue_families_in_use,
.queue_families_count = queue_families_count,
};
}
pub fn allocateQueueIndex(self: *QueueAllocator, queue_family: u32) u32 {
std.debug.assert(queue_family < self.queue_families_count);
if (self.queue_families_in_use[queue_family] < self.queue_families_properties_buffer[queue_family].queue_count) {
const queue_index = self.queue_families_in_use[queue_family];
self.queue_families_in_use[queue_family] += 1;
return queue_index;
} else {
return 0;
}
}
pub fn queueFamiliesProperties(self: *const QueueAllocator) []const vk.QueueFamilyProperties {
return self.queue_families_properties_buffer[0..self.queue_families_count];
}
pub fn queueInUse(self: *const QueueAllocator) []const u32 {
return self.queue_families_in_use[0..self.queue_families_count];
}
};
const QueueAllocations = struct {
graphics_queue: Queue.Allocation,
compute_queue: Queue.Allocation,
transfer_queue: Queue.Allocation,
presentation_queue: Queue.Allocation,
};
const DebugUtilsMessenger = if (debug) vk.DebugUtilsMessengerEXT else void;
base: vk.BaseWrapper,
instance: vk.InstanceProxy,
device: vk.DeviceProxy,
surface: vk.SurfaceKHR,
debug_utils_messenger: DebugUtilsMessenger,
physical_device: vk.PhysicalDevice,
memory_types: std.ArrayList(vk.MemoryType),
memory_heaps: std.ArrayList(vk.MemoryHeap),
graphics_queue: Queue,
compute_queue: Queue,
transfer_queue: Queue,
presentation_queue: Queue,
graphics_command_pool: vk.CommandPool,
compute_command_pool: vk.CommandPool,
transfer_command_pool: vk.CommandPool,
prng_ptr: *std.Random.Pcg,
random: std.Random,
const debug = @import("builtin").mode == .Debug;
const vk_version: vk.Version = vk.makeApiVersion(
0,
c.app_version.major,
c.app_version.minor,
c.app_version.patch,
);
const vk_debug_utils_messenger_create_info: vk.DebugUtilsMessengerCreateInfoEXT = .{
.message_severity = .{
.verbose_bit_ext = false,
.info_bit_ext = false,
.warning_bit_ext = true,
.error_bit_ext = true,
},
.message_type = .{
.general_bit_ext = true,
.validation_bit_ext = true,
.performance_bit_ext = true,
},
.pfn_user_callback = &debugUtilsMessengerCallback,
.p_user_data = null,
};
pub fn init() !*Engine {
const allocator_persistent = ctx.allocator_persistent;
const io = ctx.io;
const window = ctx.window;
const engine = try allocator_persistent.create(Engine);
errdefer allocator_persistent.destroy(engine);
// --- LOAD BASE DISPATCH --------------------------------------------------
const base = vk.BaseWrapper.load(glfw.getInstanceProcAddress);
// --- CREATE INSTANCE, ITS WRAPPER AND PROXY ------------------------------
const instance_handle = blk: {
var enabled_extensions_buffer: [16][*:0]const u8 = undefined;
var enabled_extensions: std.ArrayList([*:0]const u8) = .initBuffer(&enabled_extensions_buffer);
if (debug) {
try enabled_extensions.appendBounded(vk.extensions.ext_debug_utils.name);
}
const glfw_instance_extensions = try glfw.getRequiredInstanceExtensions();
try enabled_extensions.appendSliceBounded(glfw_instance_extensions);
const enabled_layers: []const [*:0]const u8 = if (debug) &.{"VK_LAYER_KHRONOS_validation"} else &.{};
break :blk try base.createInstance(&.{
.p_application_info = &.{
.p_application_name = c.app_name,
.application_version = vk_version.toU32(),
.p_engine_name = c.app_name,
.engine_version = vk_version.toU32(),
.api_version = @bitCast(vk.API_VERSION_1_4),
},
.enabled_layer_count = enabled_layers.len,
.pp_enabled_layer_names = enabled_layers.ptr,
.enabled_extension_count = @intCast(enabled_extensions.items.len),
.pp_enabled_extension_names = enabled_extensions.items.ptr,
.p_next = if (debug) &vk_debug_utils_messenger_create_info else null,
}, ctx.vk_allocator.capture());
};
const instance_wrapper_ptr = try allocator_persistent.create(vk.InstanceWrapper);
errdefer allocator_persistent.destroy(instance_wrapper_ptr);
instance_wrapper_ptr.* = .load(instance_handle, base.dispatch.vkGetInstanceProcAddr.?);
const instance = vk.InstanceProxy.init(instance_handle, instance_wrapper_ptr);
errdefer instance.destroyInstance(ctx.vk_allocator.capture());
// --- CREATE DEBUG UTILS MESSENGER ----------------------------------------
const debug_utils_messenger: DebugUtilsMessenger = if (debug) try instance.createDebugUtilsMessengerEXT(&vk_debug_utils_messenger_create_info, ctx.vk_allocator.capture()) else {};
errdefer {
if (debug) instance.destroyDebugUtilsMessengerEXT(debug_utils_messenger, ctx.vk_allocator.capture());
}
// --- CHOOSE PHYSICAL DEVICE, GET ITS MEMORY PROPERTIES -------------------
const physical_device = blk: {
var physical_devices_buffer: [16]vk.PhysicalDevice = undefined;
var physical_devices_count: u32 = physical_devices_buffer.len;
_ = try instance.enumeratePhysicalDevices(&physical_devices_count, &physical_devices_buffer);
// Look for the first GPU with its `device_type` equal to
// `.discrete_gpu`.
for (physical_devices_buffer[0..physical_devices_count]) |physical_device_candidate| {
const physical_device_properties = instance.getPhysicalDeviceProperties(physical_device_candidate);
if (physical_device_properties.device_type == .discrete_gpu) {
break :blk physical_device_candidate;
}
}
// Look for the first GPU.
if (physical_devices_count > 0) {
break :blk physical_devices_buffer[0];
}
return error.NoDevice;
};
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 ------------------------------------------------------
var surface: vk.SurfaceKHR = undefined;
try glfw.createWindowSurface(instance_handle, window, ctx.vk_allocator.capture(), &surface);
errdefer {
instance.destroySurfaceKHR(surface, ctx.vk_allocator.capture());
}
// --- ALLOCATE QUEUES -----------------------------------------------------
const max_queues = 4;
var queue_create_info_buffer: [max_queues]vk.DeviceQueueCreateInfo = undefined;
var queue_create_info: std.ArrayList(vk.DeviceQueueCreateInfo) = .initBuffer(&queue_create_info_buffer);
const queue_allocations: QueueAllocations = blk: {
var queue_allocator = QueueAllocator.init(instance, physical_device);
const queue_families_properties = queue_allocator.queueFamiliesProperties();
const graphics_queue_family = findGraphicsQueueFamily(queue_families_properties) orelse return error.NoGraphicsQueue;
const compute_queue_family = findComputeQueueFamily(queue_families_properties) orelse return error.NoComputeQueue;
const transfer_queue_family = findTransferQueueFamily(queue_families_properties) orelse return error.NoTransferQueue;
const presentation_queue_family = try findPresentationQueueFamily(queue_families_properties, instance, physical_device, surface) orelse return error.NoPresentationQueue;
const graphics_queue_index = queue_allocator.allocateQueueIndex(graphics_queue_family);
const compute_queue_index = queue_allocator.allocateQueueIndex(compute_queue_family);
const transfer_queue_index = queue_allocator.allocateQueueIndex(transfer_queue_family);
const presentation_queue_index = queue_allocator.allocateQueueIndex(presentation_queue_family);
const queue_priorities = [_]f32{1.0} ** max_queues;
for (queue_allocator.queueInUse(), 0..) |count, queue_family| {
if (count == 0) continue;
queue_create_info.appendBounded(.{
.queue_family_index = @intCast(queue_family),
.queue_count = count,
.p_queue_priorities = &queue_priorities,
}) catch unreachable;
}
break :blk .{
.graphics_queue = .{ .family = graphics_queue_family, .index = graphics_queue_index },
.compute_queue = .{ .family = compute_queue_family, .index = compute_queue_index },
.transfer_queue = .{ .family = transfer_queue_family, .index = transfer_queue_index },
.presentation_queue = .{ .family = presentation_queue_family, .index = presentation_queue_index },
};
};
// --- CREATE (LOGICAL) DEVICE, ITS WRAPPER AND PROXY ----------------------
const device_handle = blk: {
var enabled_extensions_buffer: [16][*:0]const u8 = undefined;
var enabled_extensions: std.ArrayList([*:0]const u8) = .initBuffer(&enabled_extensions_buffer);
try enabled_extensions.appendBounded(vk.extensions.khr_swapchain.name);
var enabled_features_vulkan10: vk.PhysicalDeviceFeatures2 = .{
.features = .{
.shader_int_16 = .true,
},
};
var enabled_features_vulkan11: vk.PhysicalDeviceVulkan11Features = .{
.p_next = &enabled_features_vulkan10,
.storage_buffer_16_bit_access = .true,
.uniform_and_storage_buffer_16_bit_access = .true,
};
var enabled_features_vulkan12: vk.PhysicalDeviceVulkan12Features = .{
.p_next = &enabled_features_vulkan11,
.descriptor_indexing = .true,
.descriptor_binding_partially_bound = .true,
.descriptor_binding_variable_descriptor_count = .true,
.runtime_descriptor_array = .true,
.scalar_block_layout = .true,
.imageless_framebuffer = .true,
.timeline_semaphore = .true,
};
var enabled_features_vulkan13: vk.PhysicalDeviceVulkan13Features = .{
.p_next = &enabled_features_vulkan12,
.robust_image_access = .true,
.inline_uniform_block = .true,
.synchronization_2 = .true,
.dynamic_rendering = .true,
.maintenance_4 = .true,
};
var enabled_features_vulkan14: vk.PhysicalDeviceVulkan14Features = .{
.p_next = &enabled_features_vulkan13,
.maintenance_5 = .true,
.maintenance_6 = .true,
};
break :blk try instance.createDevice(physical_device, &.{
.p_next = &enabled_features_vulkan14,
.queue_create_info_count = @intCast(queue_create_info.items.len),
.p_queue_create_infos = queue_create_info.items.ptr,
.enabled_extension_count = @intCast(enabled_extensions.items.len),
.pp_enabled_extension_names = enabled_extensions.items.ptr,
}, ctx.vk_allocator.capture());
};
const device_wrapper_ptr = try allocator_persistent.create(vk.DeviceWrapper);
errdefer allocator_persistent.destroy(device_wrapper_ptr);
device_wrapper_ptr.* = .load(device_handle, instance.wrapper.dispatch.vkGetDeviceProcAddr.?);
const device = vk.DeviceProxy.init(device_handle, device_wrapper_ptr);
errdefer device.destroyDevice(ctx.vk_allocator.capture());
// --- CREATE COMMAND POOLS, GET QUEUES ------------------------------------
const graphics_command_pool = try device.createCommandPool(&.{
.flags = .{ .transient_bit = true },
.queue_family_index = queue_allocations.graphics_queue.family,
}, ctx.vk_allocator.capture());
errdefer device.destroyCommandPool(graphics_command_pool, ctx.vk_allocator.capture());
const compute_command_pool = try device.createCommandPool(&.{
.flags = .{ .transient_bit = true },
.queue_family_index = queue_allocations.compute_queue.family,
}, ctx.vk_allocator.capture());
errdefer device.destroyCommandPool(compute_command_pool, ctx.vk_allocator.capture());
const transfer_command_pool = try device.createCommandPool(&.{
.flags = .{ .transient_bit = true },
.queue_family_index = queue_allocations.transfer_queue.family,
}, ctx.vk_allocator.capture());
errdefer device.destroyCommandPool(transfer_command_pool, ctx.vk_allocator.capture());
const graphics_queue = Queue.init(
device.getDeviceQueue(
queue_allocations.graphics_queue.family,
queue_allocations.graphics_queue.index,
),
queue_allocations.graphics_queue,
);
const compute_queue = Queue.init(
device.getDeviceQueue(
queue_allocations.compute_queue.family,
queue_allocations.compute_queue.index,
),
queue_allocations.compute_queue,
);
const transfer_queue = Queue.init(
device.getDeviceQueue(
queue_allocations.transfer_queue.family,
queue_allocations.transfer_queue.index,
),
queue_allocations.transfer_queue,
);
const presentation_queue = Queue.init(
device.getDeviceQueue(
queue_allocations.presentation_queue.family,
queue_allocations.presentation_queue.index,
),
queue_allocations.presentation_queue,
);
// --- CREATE AND SEED RNG -------------------------------------------------
const prng_ptr = try allocator_persistent.create(std.Random.Pcg);
errdefer allocator_persistent.destroy(prng_ptr);
const timestamp: u64 = @bitCast(@as(i64, @truncate(std.Io.Timestamp.now(io, .awake).nanoseconds)));
prng_ptr.* = .init(timestamp);
const random = prng_ptr.random();
// -------------------------------------------------------------------------
engine.* = .{
.base = base,
.instance = instance,
.device = device,
.surface = surface,
.debug_utils_messenger = debug_utils_messenger,
.physical_device = physical_device,
.memory_types = memory_types,
.memory_heaps = memory_heaps,
.graphics_queue = graphics_queue,
.compute_queue = compute_queue,
.transfer_queue = transfer_queue,
.presentation_queue = presentation_queue,
.graphics_command_pool = graphics_command_pool,
.compute_command_pool = compute_command_pool,
.transfer_command_pool = transfer_command_pool,
.prng_ptr = prng_ptr,
.random = random,
};
return engine;
}
pub fn deinit(self: *Engine) void {
std.log.scoped(.deinit).debug("Deinitializing {*}", .{self});
self.device.destroyCommandPool(self.graphics_command_pool, ctx.vk_allocator.capture());
self.device.destroyCommandPool(self.compute_command_pool, ctx.vk_allocator.capture());
self.device.destroyCommandPool(self.transfer_command_pool, ctx.vk_allocator.capture());
self.device.destroyDevice(ctx.vk_allocator.capture());
self.instance.destroySurfaceKHR(self.surface, ctx.vk_allocator.capture());
if (debug) self.instance.destroyDebugUtilsMessengerEXT(self.debug_utils_messenger, ctx.vk_allocator.capture());
self.instance.destroyInstance(ctx.vk_allocator.capture());
self.* = undefined;
}
pub fn allocate(self: *const Engine, memory_requirements: vk.MemoryRequirements, memory_property_flags: vk.MemoryPropertyFlags) !vk.DeviceMemory {
for (self.memory_types.items, 0..) |memory_type, i| {
const is_type = memory_requirements.memory_type_bits & (@as(u32, 1) << @truncate(i)) != 0;
const has_flags = memory_type.property_flags.contains(memory_property_flags);
if (is_type and has_flags) {
return try self.device.allocateMemory(&.{
.allocation_size = memory_requirements.size,
.memory_type_index = @truncate(i),
}, ctx.vk_allocator.capture());
}
}
return error.NoSuitableMemoryType;
}
pub fn setObjectName(self: *const Engine, handle: anytype, comptime fmt: []const u8, args: anytype) void {
if (debug) {
const HandleType = @TypeOf(handle);
const type_name = @typeName(HandleType);
if (@as(std.builtin.TypeId, @typeInfo(HandleType)) != .@"enum") {
@compileError("Cannot set object name for a handle of type " ++ type_name ++ ", which is not an enum.");
}
const object_type: vk.ObjectType = switch (HandleType) {
vk.Instance => .instance,
vk.PhysicalDevice => .physical_device,
vk.Device => .device,
vk.Queue => .queue,
vk.Semaphore => .semaphore,
vk.CommandBuffer => .command_buffer,
vk.Fence => .fence,
vk.DeviceMemory => .device_memory,
vk.Buffer => .buffer,
vk.Image => .image,
vk.Event => .event,
vk.QueryPool => .query_pool,
vk.BufferView => .buffer_view,
vk.ImageView => .image_view,
vk.ShaderModule => .shader_module,
vk.PipelineCache => .pipeline_cache,
vk.PipelineLayout => .pipeline_layout,
vk.RenderPass => .render_pass,
vk.Pipeline => .pipeline,
vk.DescriptorSetLayout => .descriptor_set_layout,
vk.Sampler => .sampler,
vk.DescriptorPool => .descriptor_pool,
vk.DescriptorSet => .descriptor_set,
vk.Framebuffer => .framebuffer,
vk.CommandPool => .command_pool,
vk.SurfaceKHR => .surface_khr,
vk.SwapchainKHR => .swapchain_khr,
else => @compileError("Unsupported type " ++ type_name ++ " for setting and object name"),
};
const handle_value: u64 = @intFromEnum(handle);
var buf: [256]u8 = undefined;
const name = std.fmt.bufPrintZ(&buf, fmt, args) catch |err| {
std.log.debug("Failed to set object name for {s}#{X}: {s}", .{ type_name, handle_value, @errorName(err) });
return;
};
self.device.setDebugUtilsObjectNameEXT(&.{
.object_type = object_type,
.object_handle = handle_value,
.p_object_name = name.ptr,
}) catch |err| {
std.log.debug("Failed to set object name for {s}#{X}: {s}", .{ type_name, handle_value, @errorName(err) });
return;
};
}
}
pub const SubmitInfo = struct {
wait_semaphores: []const WaitSemaphore = &.{},
signal_semaphores: []const vk.Semaphore = &.{},
fence: vk.Fence = .null_handle,
};
pub fn queueSubmit(
self: *const Engine,
queue_type: QueueType,
command_buffer: vk.CommandBuffer,
submit_info: SubmitInfo,
) !void {
const allocator_frame = ctx.allocator_frame;
const queue = switch (queue_type) {
.graphics => self.graphics_queue.handle,
.compute => self.compute_queue.handle,
.transfer => self.transfer_queue.handle,
};
const command_buffers = [_]vk.CommandBuffer{command_buffer};
var wait_semaphores = std.MultiArrayList(WaitSemaphore){};
defer wait_semaphores.deinit(allocator_frame);
try wait_semaphores.ensureTotalCapacity(allocator_frame, submit_info.wait_semaphores.len);
for (submit_info.wait_semaphores) |wait_semaphore| {
wait_semaphores.appendAssumeCapacity(wait_semaphore);
}
const submits = [_]vk.SubmitInfo{
.{
.wait_semaphore_count = @intCast(wait_semaphores.len),
.p_wait_semaphores = wait_semaphores.items(.semaphore).ptr,
.p_wait_dst_stage_mask = wait_semaphores.items(.stage_flags).ptr,
.command_buffer_count = command_buffers.len,
.p_command_buffers = &command_buffers,
.signal_semaphore_count = @intCast(submit_info.signal_semaphores.len),
.p_signal_semaphores = submit_info.signal_semaphores.ptr,
},
};
try self.device.queueSubmit(queue, &submits, submit_info.fence);
}
pub const PresentInfo = struct {
wait_semaphores: []const vk.Semaphore = &.{},
image_index: u32,
swapchain: vk.SwapchainKHR,
};
pub fn queuePresent(self: *Engine, present_info: PresentInfo) !vk.Result {
const swapchains = [_]vk.SwapchainKHR{present_info.swapchain};
const image_indices = [_]u32{present_info.image_index};
const res = try self.device.queuePresentKHR(self.presentation_queue.handle, &.{
.wait_semaphore_count = @intCast(present_info.wait_semaphores.len),
.p_wait_semaphores = present_info.wait_semaphores.ptr,
.swapchain_count = swapchains.len,
.p_swapchains = &swapchains,
.p_image_indices = &image_indices,
});
return res;
}
fn debugUtilsMessengerCallback(
severity: vk.DebugUtilsMessageSeverityFlagsEXT,
message_type: vk.DebugUtilsMessageTypeFlagsEXT,
maybe_callback_data: ?*const vk.DebugUtilsMessengerCallbackDataEXT,
_: ?*anyopaque,
) callconv(vk.vulkan_call_conv) vk.Bool32 {
const log = std.log.scoped(.vulkan);
const message = if (maybe_callback_data) |callback_data| callback_data.p_message orelse return .false else return .false;
const type_name = if (message_type.general_bit_ext) "general" else if (message_type.validation_bit_ext) "validation" else if (message_type.performance_bit_ext) "performance" else if (message_type.device_address_binding_bit_ext) "device_address_binding" else "unknown";
const format = "[{s}] {s}";
const args = .{ type_name, message };
if (severity.error_bit_ext) {
log.err(format, args);
} else if (severity.warning_bit_ext) {
log.warn(format, args);
} else if (severity.info_bit_ext) {
log.info(format, args);
} else if (severity.verbose_bit_ext) {
log.debug(format, args);
}
return .false;
}
fn findGraphicsQueueFamily(queue_families_properties: []const vk.QueueFamilyProperties) ?u32 {
// Look for the first family that has the `graphics_bit` set.
for (queue_families_properties, 0..) |queue_family_properties, i| {
const graphics_bit = queue_family_properties.queue_flags.graphics_bit;
if (graphics_bit) {
return @intCast(i);
}
}
return null;
}
fn findComputeQueueFamily(queue_families_properties: []const vk.QueueFamilyProperties) ?u32 {
// Look for the first family that has the `compute_bit` set, but not
// `graphics_bit`.
for (queue_families_properties, 0..) |queue_family_properties, i| {
const compute_bit = queue_family_properties.queue_flags.compute_bit;
const graphics_bit = queue_family_properties.queue_flags.graphics_bit;
if (compute_bit and !graphics_bit) {
return @intCast(i);
}
}
// Look for the first family that has the `compute_bit` set.
for (queue_families_properties, 0..) |queue_family_properties, i| {
const compute_bit = queue_family_properties.queue_flags.compute_bit;
if (compute_bit) {
return @intCast(i);
}
}
return null;
}
fn findTransferQueueFamily(queue_families_properties: []const vk.QueueFamilyProperties) ?u32 {
// Look for the first family that has the `transfer_bit` set, but neither
// `graphics_bit` or `compute_bit`.
for (queue_families_properties, 0..) |queue_family_properties, i| {
const transfer_bit = queue_family_properties.queue_flags.transfer_bit;
const graphics_bit = queue_family_properties.queue_flags.graphics_bit;
const compute_bit = queue_family_properties.queue_flags.compute_bit;
if (transfer_bit and !graphics_bit and !compute_bit) {
return @intCast(i);
}
}
// Look for the first family that has either `transfer_bit`, `graphics_bit`
// or `compute_bit` set.
// NOTE `graphics_bit` or `compute_bit` imply `transfer_bit`, even if not
// set explicitly.
for (queue_families_properties, 0..) |queue_family_properties, i| {
const transfer_bit = queue_family_properties.queue_flags.transfer_bit;
const graphics_bit = queue_family_properties.queue_flags.graphics_bit;
const compute_bit = queue_family_properties.queue_flags.compute_bit;
if (transfer_bit or graphics_bit or compute_bit) {
return @intCast(i);
}
}
return null;
}
fn findPresentationQueueFamily(queue_families_properties: []const vk.QueueFamilyProperties, instance: vk.InstanceProxy, physical_device: vk.PhysicalDevice, surface: vk.SurfaceKHR) !?u32 {
// Look for the first family that supports presentation to a given surface.
for (queue_families_properties, 0..) |_, i| {
if (try instance.getPhysicalDeviceSurfaceSupportKHR(physical_device, @intCast(i), surface) == .true) {
return @intCast(i);
}
}
return null;
}
fn makeArena(self: *const Engine) std.heap.ArenaAllocator {
return .init(self.vk_allocator.allocator);
}
fn resolveCommandPool(self: *const Engine, queue_type: QueueType) vk.CommandPool {
return switch (queue_type) {
.graphics => self.graphics_command_pool,
.compute => self.compute_command_pool,
.transfer => self.transfer_command_pool,
};
}
// --- VULKAN WRAPPERS ---------------------------------------------------------
pub const AcquireInfo = struct {
timeout: u64 = std.math.maxInt(u64),
semaphore: vk.Semaphore = .null_handle,
fence: vk.Fence = .null_handle,
};
pub const BufferCreateInfo = struct {
flags: vk.BufferCreateFlags = .{},
size: vk.DeviceSize,
usage: vk.BufferUsageFlags,
queue_family_indices: []const u32 = &.{},
};
pub const CommandBufferAllocateInfo = struct {
queue_type: QueueType,
level: vk.CommandBufferLevel = .primary,
};
pub const CommandBufferFreeInfo = struct {
queue_type: QueueType,
command_buffer: vk.CommandBuffer,
};
pub const DescriptorPoolCreateInfo = struct {
flags: vk.DescriptorPoolCreateFlags = .{},
max_sets: u32,
pool_sizes: []const vk.DescriptorPoolSize = &.{},
};
pub const DescriptorSetAllocateInfo = struct {
descriptor_pool: vk.DescriptorPool,
set_layout: vk.DescriptorSetLayout,
variable_descriptor_count: ?u32 = null,
};
pub const DescriptorSetsAllocateInfo = struct {
descriptor_pool: vk.DescriptorPool,
set_layouts: []const vk.DescriptorSetLayout,
variable_descriptor_counts: []const u32 = &.{},
};
pub const DescriptorSetLayoutBinding = struct {
binding: u32,
descriptor_type: vk.DescriptorType,
descriptor_count: u32,
stage_flags: vk.ShaderStageFlags,
immutable_samplers: []const vk.Sampler = &.{},
flags: vk.DescriptorBindingFlags = .{},
};
pub const DescriptorSetLayoutCreateInfo = struct {
flags: vk.DescriptorSetLayoutCreateFlags = .{},
bindings: []const DescriptorSetLayoutBinding = &.{},
};
pub const DescriptorSetsUpdateInfo = struct {
writes: []const WriteDescriptorSet = &.{},
copies: []const vk.CopyDescriptorSet = &.{},
};
pub const GraphicsPipelineCreateInfo = struct {
flags: vk.PipelineCreateFlags = .{},
stages: []const PipelineShaderStageCreateInfo = &.{},
vertex_input_state: ?PipelineVertexInputStateCreateInfo = null,
input_assembly_state: ?vk.PipelineInputAssemblyStateCreateInfo = null,
tessellation_state: ?vk.PipelineTessellationStateCreateInfo = null,
viewport_state: ?PipelineViewportStateCreateInfo = null,
rasterization_state: ?vk.PipelineRasterizationStateCreateInfo = null,
multisample_state: ?vk.PipelineMultisampleStateCreateInfo = null,
depth_stencil_state: ?vk.PipelineDepthStencilStateCreateInfo = null,
color_blend_state: ?PipelineColorBlendStateCreateInfo = null,
dynamic_state: ?PipelineDynamicStateCreateInfo = null,
layout: vk.PipelineLayout = .null_handle,
view_mask: u32,
color_attachment_formats: []const vk.Format = &.{},
depth_attachment_format: vk.Format = .undefined,
stencil_attachment_format: vk.Format = .undefined,
base_pipeline_handle: vk.Pipeline = .null_handle,
};
pub const ImageCreateInfo = struct {
flags: vk.ImageCreateFlags = .{},
image_type: vk.ImageType,
format: vk.Format,
extent: vk.Extent3D,
mip_levels: u32,
array_layers: u32,
samples: vk.SampleCountFlags,
tiling: vk.ImageTiling,
usage: vk.ImageUsageFlags,
queue_family_indices: []const u32 = &.{},
initial_layout: vk.ImageLayout,
};
pub const ImageViewCreateInfo = struct {
flags: vk.ImageViewCreateFlags = .{},
image: vk.Image,
view_type: vk.ImageViewType,
format: vk.Format,
components: vk.ComponentMapping = .{
.r = .identity,
.g = .identity,
.b = .identity,
.a = .identity,
},
subresource_range: vk.ImageSubresourceRange,
};
pub const PipelineColorBlendStateCreateInfo = struct {
flags: vk.PipelineColorBlendStateCreateFlags = .{},
logic_op_enable: vk.Bool32,
logic_op: vk.LogicOp,
attachments: []const vk.PipelineColorBlendAttachmentState = &.{},
blend_constants: [4]f32,
};
pub const PipelineDynamicStateCreateInfo = struct {
flags: vk.PipelineDynamicStateCreateFlags = .{},
dynamic_states: []const vk.DynamicState = &.{},
};
pub const PipelineLayoutCreateInfo = struct {
flags: vk.PipelineLayoutCreateFlags = .{},
set_layouts: []const vk.DescriptorSetLayout = &.{},
push_constant_ranges: []const vk.PushConstantRange = &.{},
};
pub const PipelineShaderStageCreateInfo = struct {
flags: vk.PipelineShaderStageCreateFlags = .{},
stage: vk.ShaderStageFlags,
module: vk.ShaderModule = .null_handle,
name: [*:0]const u8,
specialization_info: ?SpecializationInfo = null,
};
pub const PipelineVertexInputStateCreateInfo = struct {
flags: vk.PipelineVertexInputStateCreateFlags = .{},
vertex_binding_descriptions: []const vk.VertexInputBindingDescription = &.{},
vertex_attribute_descriptions: []const vk.VertexInputAttributeDescription = &.{},
};
pub const PipelineViewportStateCreateInfo = struct {
flags: vk.PipelineViewportStateCreateFlags = .{},
viewports: []const vk.Viewport = &.{},
scissors: []const vk.Rect2D = &.{},
};
pub const RenderPassCreateInfo = struct {
flags: vk.RenderPassCreateFlags = .{},
attachments: []const vk.AttachmentDescription = &.{},
subpasses: []const SubpassDescription,
dependencies: []const vk.SubpassDependency = &.{},
};
pub const ShaderModuleCreateInfo = struct {
flags: vk.ShaderModuleCreateFlags = .{},
code: []align(@alignOf(u32)) const u8,
};
pub const SpecializationInfo = struct {
map_entries: []vk.SpecializationMapEntry = &.{},
data: []const u8 = &.{},
};
pub const SubpassDescription = struct {
flags: vk.SubpassDescriptionFlags = .{},
pipeline_bind_point: vk.PipelineBindPoint,
input_attachments: []const vk.AttachmentReference = &.{},
color_attachments: []const vk.AttachmentReference = &.{},
resolve_attachments: ?[]const vk.AttachmentReference = null,
depth_stencil_attachment: ?vk.AttachmentReference = null,
preserve_attachments: []const u32 = &.{},
};
pub const SwapchainCreateInfo = struct {
flags: vk.SwapchainCreateFlagsKHR = .{},
surface: vk.SurfaceKHR,
min_image_count: u32,
image_format: vk.Format,
image_color_space: vk.ColorSpaceKHR,
image_extent: vk.Extent2D,
image_array_layers: u32,
image_usage: vk.ImageUsageFlags,
queue_family_indices: []const u32 = &.{},
pre_transform: vk.SurfaceTransformFlagsKHR,
composite_alpha: vk.CompositeAlphaFlagsKHR,
present_mode: vk.PresentModeKHR,
clipped: vk.Bool32,
old_swapchain: vk.SwapchainKHR = .null_handle,
};
pub const WriteDescriptorSet = struct {
dst_set: vk.DescriptorSet,
dst_binding: u32,
dst_array_element: u32,
descriptor_type: vk.DescriptorType,
descriptor_infos: union(enum) {
image: []const vk.DescriptorImageInfo,
buffer: []const vk.DescriptorBufferInfo,
texel_buffer_view: []const vk.BufferView,
},
};
pub fn acquireNextImage(self: *Engine, swapchain: vk.SwapchainKHR, acquire_info: AcquireInfo) !vk.DeviceProxy.AcquireNextImageKHRResult {
const res = try self.device.acquireNextImageKHR(
swapchain,
acquire_info.timeout,
acquire_info.semaphore,
acquire_info.fence,
);
return res;
}
pub fn allocateCommandBuffer(self: *Engine, allocate_info: CommandBufferAllocateInfo) !vk.CommandBuffer {
const command_pool = self.resolveCommandPool(allocate_info.queue_type);
var command_buffers: [1]vk.CommandBuffer = undefined;
try self.device.allocateCommandBuffers(&.{
.command_pool = command_pool,
.level = allocate_info.level,
.command_buffer_count = command_buffers.len,
}, &command_buffers);
return command_buffers[0];
}
pub fn allocateDescriptorSet(self: *Engine, allocate_info: DescriptorSetAllocateInfo) !vk.DescriptorSet {
var descriptor_sets: [1]vk.DescriptorSet = undefined;
try self.allocateDescriptorSets(.{
.descriptor_pool = allocate_info.descriptor_pool,
.set_layouts = &.{allocate_info.set_layout},
.variable_descriptor_counts = if (allocate_info.variable_descriptor_count) |x| &.{x} else &.{},
}, &descriptor_sets);
return descriptor_sets[0];
}
pub fn allocateDescriptorSets(self: *Engine, allocate_info: DescriptorSetsAllocateInfo, descriptor_sets: []vk.DescriptorSet) !void {
std.debug.assert(descriptor_sets.len >= allocate_info.set_layouts.len);
const has_variable_descriptor_counts = allocate_info.variable_descriptor_counts.len > 0;
var p_next: ?*const anyopaque = null;
if (has_variable_descriptor_counts) {
p_next = &vk.DescriptorSetVariableDescriptorCountAllocateInfo{
.p_next = p_next,
.descriptor_set_count = @intCast(allocate_info.variable_descriptor_counts.len),
.p_descriptor_counts = allocate_info.variable_descriptor_counts.ptr,
};
}
try self.device.allocateDescriptorSets(&.{
.p_next = p_next,
.descriptor_pool = allocate_info.descriptor_pool,
.descriptor_set_count = @intCast(allocate_info.set_layouts.len),
.p_set_layouts = allocate_info.set_layouts.ptr,
}, descriptor_sets.ptr);
}
pub fn bindBufferMemory(self: *Engine, buffer: vk.Buffer, memory: vk.DeviceMemory, memory_offset: vk.DeviceSize) !void {
try self.device.bindBufferMemory(buffer, memory, memory_offset);
}
pub fn bindImageMemory(self: *Engine, image: vk.Image, memory: vk.DeviceMemory, memory_offset: vk.DeviceSize) !void {
try self.device.bindImageMemory(image, memory, memory_offset);
}
pub fn createBuffer(self: *Engine, create_info: BufferCreateInfo) !vk.Buffer {
const allocator_frame = ctx.allocator_frame;
var queue_family_indices_set: std.AutoArrayHashMapUnmanaged(u32, void) = .{};
for (create_info.queue_family_indices) |queue_family_index| {
try queue_family_indices_set.put(allocator_frame, queue_family_index, {});
}
const queue_family_indices = queue_family_indices_set.keys();
const buffer = self.device.createBuffer(&.{
.flags = create_info.flags,
.size = create_info.size,
.usage = create_info.usage,
.sharing_mode = if (queue_family_indices.len > 1) .concurrent else .exclusive,
.queue_family_index_count = if (queue_family_indices.len > 1) @intCast(queue_family_indices.len) else 0,
.p_queue_family_indices = if (queue_family_indices.len > 1) queue_family_indices.ptr else null,
}, ctx.vk_allocator.capture());
return buffer;
}
pub fn createDescriptorSetLayout(self: *Engine, create_info: DescriptorSetLayoutCreateInfo) !vk.DescriptorSetLayout {
const allocator_frame = ctx.allocator_frame;
const has_binding_flags = blk: {
for (create_info.bindings) |binding| {
if (@as(vk.Flags, @bitCast(binding.flags)) != 0) {
break :blk true;
}
}
break :blk false;
};
var p_next: ?*const anyopaque = null;
if (has_binding_flags) {
const descriptor_set_layout_bindings_flags = try allocator_frame.alloc(vk.DescriptorBindingFlags, create_info.bindings.len);
for (descriptor_set_layout_bindings_flags, create_info.bindings) |*x, binding| {
x.* = binding.flags;
}
p_next = &vk.DescriptorSetLayoutBindingFlagsCreateInfo{
.p_next = p_next,
.binding_count = @intCast(descriptor_set_layout_bindings_flags.len),
.p_binding_flags = descriptor_set_layout_bindings_flags.ptr,
};
}
const bindings = try allocator_frame.alloc(vk.DescriptorSetLayoutBinding, create_info.bindings.len);
for (bindings, create_info.bindings) |*x, binding| {
x.* = .{
.binding = binding.binding,
.descriptor_type = binding.descriptor_type,
.descriptor_count = binding.descriptor_count,
.stage_flags = binding.stage_flags,
.p_immutable_samplers = binding.immutable_samplers.ptr,
};
}
const descriptor_set_layout = try self.device.createDescriptorSetLayout(&.{
.p_next = p_next,
.flags = create_info.flags,
.binding_count = @intCast(bindings.len),
.p_bindings = bindings.ptr,
}, ctx.vk_allocator.capture());
return descriptor_set_layout;
}
pub fn createDescriptorPool(self: *Engine, create_info: DescriptorPoolCreateInfo) !vk.DescriptorPool {
const descriptor_pool = try self.device.createDescriptorPool(&.{
.flags = create_info.flags,
.max_sets = create_info.max_sets,
.pool_size_count = @intCast(create_info.pool_sizes.len),
.p_pool_sizes = create_info.pool_sizes.ptr,
}, ctx.vk_allocator.capture());
return descriptor_pool;
}
pub fn createFence(self: *Engine, create_info: vk.FenceCreateInfo) !vk.Fence {
const fence = try self.device.createFence(&create_info, ctx.vk_allocator.capture());
return fence;
}
pub fn createGraphicsPipeline(self: *Engine, create_info: GraphicsPipelineCreateInfo) !vk.Pipeline {
const allocator_frame = ctx.allocator_frame;
const stages = try allocator_frame.alloc(vk.PipelineShaderStageCreateInfo, create_info.stages.len);
for (stages, create_info.stages) |*x, stage| {
x.* = .{
.flags = stage.flags,
.stage = stage.stage,
.p_name = stage.name,
.module = stage.module,
.p_specialization_info = blk: {
if (stage.specialization_info) |y| {
const specialization_info = try allocator_frame.create(vk.SpecializationInfo);
specialization_info.* = .{
.map_entry_count = @intCast(y.map_entries.len),
.p_map_entries = y.map_entries.ptr,
.data_size = y.data.len,
.p_data = y.data.ptr,
};
break :blk specialization_info;
} else {
break :blk null;
}
},
};
}
const pipeline_rendering_create_info: vk.PipelineRenderingCreateInfo = .{
.view_mask = create_info.view_mask,
.color_attachment_count = @intCast(create_info.color_attachment_formats.len),
.p_color_attachment_formats = create_info.color_attachment_formats.ptr,
.depth_attachment_format = create_info.depth_attachment_format,
.stencil_attachment_format = create_info.stencil_attachment_format,
};
const graphics_pipeline_create_infos = [_]vk.GraphicsPipelineCreateInfo{
.{
.p_next = &pipeline_rendering_create_info,
.flags = create_info.flags,
.stage_count = @intCast(stages.len),
.p_stages = stages.ptr,
.p_vertex_input_state = if (create_info.vertex_input_state) |vertex_input_state| &.{
.flags = vertex_input_state.flags,
.vertex_binding_description_count = @intCast(vertex_input_state.vertex_binding_descriptions.len),
.p_vertex_binding_descriptions = vertex_input_state.vertex_binding_descriptions.ptr,
.vertex_attribute_description_count = @intCast(vertex_input_state.vertex_attribute_descriptions.len),
.p_vertex_attribute_descriptions = vertex_input_state.vertex_attribute_descriptions.ptr,
} else null,
.p_input_assembly_state = if (create_info.input_assembly_state) |*x| x else null,
.p_tessellation_state = if (create_info.tessellation_state) |*x| x else null,
.p_viewport_state = if (create_info.viewport_state) |viewport_state| &.{
.flags = viewport_state.flags,
.viewport_count = @intCast(viewport_state.viewports.len),
.p_viewports = viewport_state.viewports.ptr,
.scissor_count = @intCast(viewport_state.scissors.len),
.p_scissors = viewport_state.scissors.ptr,
} else null,
.p_rasterization_state = if (create_info.rasterization_state) |*x| x else null,
.p_multisample_state = if (create_info.multisample_state) |*x| x else null,
.p_depth_stencil_state = if (create_info.depth_stencil_state) |*x| x else null,
.p_color_blend_state = if (create_info.color_blend_state) |color_blend_state| &.{
.flags = color_blend_state.flags,
.logic_op_enable = color_blend_state.logic_op_enable,
.logic_op = color_blend_state.logic_op,
.attachment_count = @intCast(color_blend_state.attachments.len),
.p_attachments = color_blend_state.attachments.ptr,
.blend_constants = color_blend_state.blend_constants,
} else null,
.p_dynamic_state = if (create_info.dynamic_state) |dynamic_state| &.{
.flags = dynamic_state.flags,
.dynamic_state_count = @intCast(dynamic_state.dynamic_states.len),
.p_dynamic_states = dynamic_state.dynamic_states.ptr,
} else null,
.layout = create_info.layout,
.subpass = 0,
.base_pipeline_handle = create_info.base_pipeline_handle,
.base_pipeline_index = -1,
},
};
var pipelines: [1]vk.Pipeline = undefined;
_ = try self.device.createGraphicsPipelines(.null_handle, &graphics_pipeline_create_infos, ctx.vk_allocator.capture(), &pipelines);
return pipelines[0];
}
pub fn createImage(self: *Engine, create_info: ImageCreateInfo) !vk.Image {
const allocator_frame = ctx.allocator_frame;
var queue_family_indices_set: std.AutoArrayHashMapUnmanaged(u32, void) = .{};
for (create_info.queue_family_indices) |queue_family_index| {
try queue_family_indices_set.put(allocator_frame, queue_family_index, {});
}
const queue_family_indices = queue_family_indices_set.keys();
const image = self.device.createImage(&.{
.flags = create_info.flags,
.image_type = create_info.image_type,
.format = create_info.format,
.extent = create_info.extent,
.mip_levels = create_info.mip_levels,
.array_layers = create_info.array_layers,
.samples = create_info.samples,
.tiling = create_info.tiling,
.usage = create_info.usage,
.sharing_mode = if (queue_family_indices.len > 1) .concurrent else .exclusive,
.queue_family_index_count = if (queue_family_indices.len > 1) @intCast(queue_family_indices.len) else 0,
.p_queue_family_indices = if (queue_family_indices.len > 1) queue_family_indices.ptr else null,
.initial_layout = create_info.initial_layout,
}, ctx.vk_allocator.capture());
return image;
}
pub fn createImageView(self: *Engine, create_info: ImageViewCreateInfo) !vk.ImageView {
const image_view = try self.device.createImageView(&.{
.flags = create_info.flags,
.image = create_info.image,
.view_type = create_info.view_type,
.format = create_info.format,
.components = create_info.components,
.subresource_range = create_info.subresource_range,
}, ctx.vk_allocator.capture());
return image_view;
}
pub fn createPipelineLayout(self: *Engine, create_info: PipelineLayoutCreateInfo) !vk.PipelineLayout {
const pipeline_layout = try self.device.createPipelineLayout(&.{
.flags = create_info.flags,
.set_layout_count = @intCast(create_info.set_layouts.len),
.p_set_layouts = create_info.set_layouts.ptr,
.push_constant_range_count = @intCast(create_info.push_constant_ranges.len),
.p_push_constant_ranges = create_info.push_constant_ranges.ptr,
}, ctx.vk_allocator.capture());
return pipeline_layout;
}
pub fn createSampler(self: *Engine, create_info: vk.SamplerCreateInfo) !vk.Sampler {
const sampler = try self.device.createSampler(&create_info, ctx.vk_allocator.capture());
return sampler;
}
pub fn createSemaphore(self: *Engine) !vk.Semaphore {
const semaphore = try self.device.createSemaphore(&.{}, ctx.vk_allocator.capture());
return semaphore;
}
pub fn createShaderModule(self: *Engine, create_info: ShaderModuleCreateInfo) !vk.ShaderModule {
const shader_module = try self.device.createShaderModule(&.{
.flags = create_info.flags,
.code_size = create_info.code.len,
.p_code = @ptrCast(create_info.code.ptr),
}, ctx.vk_allocator.capture());
return shader_module;
}
pub fn createSwapchain(self: *Engine, create_info: SwapchainCreateInfo) !vk.SwapchainKHR {
const allocator_frame = ctx.allocator_frame;
var queue_family_indices_set: std.AutoArrayHashMapUnmanaged(u32, void) = .{};
for (create_info.queue_family_indices) |queue_family_index| {
try queue_family_indices_set.put(allocator_frame, queue_family_index, {});
}
const queue_family_indices = queue_family_indices_set.keys();
const swapchain = try self.device.createSwapchainKHR(&.{
.flags = create_info.flags,
.surface = create_info.surface,
.min_image_count = create_info.min_image_count,
.image_format = create_info.image_format,
.image_color_space = create_info.image_color_space,
.image_extent = create_info.image_extent,
.image_array_layers = create_info.image_array_layers,
.image_usage = create_info.image_usage,
.image_sharing_mode = if (queue_family_indices.len > 1) .concurrent else .exclusive,
.queue_family_index_count = if (queue_family_indices.len > 1) @intCast(queue_family_indices.len) else 0,
.p_queue_family_indices = if (queue_family_indices.len > 1) queue_family_indices.ptr else null,
.pre_transform = create_info.pre_transform,
.composite_alpha = create_info.composite_alpha,
.present_mode = create_info.present_mode,
.clipped = create_info.clipped,
.old_swapchain = create_info.old_swapchain,
}, ctx.vk_allocator.capture());
return swapchain;
}
pub fn destroyBuffer(self: *Engine, buffer: vk.Buffer) void {
self.device.destroyBuffer(buffer, ctx.vk_allocator.capture());
}
pub fn destroyDescriptorPool(self: *Engine, descriptor_pool: vk.DescriptorPool) void {
self.device.destroyDescriptorPool(descriptor_pool, ctx.vk_allocator.capture());
}
pub fn destroyDescriptorSetLayout(self: *Engine, descriptor_set_layout: vk.DescriptorSetLayout) void {
self.device.destroyDescriptorSetLayout(descriptor_set_layout, ctx.vk_allocator.capture());
}
pub fn destroyFence(self: *Engine, fence: vk.Fence) void {
self.device.destroyFence(fence, ctx.vk_allocator.capture());
}
pub fn destroyImage(self: *Engine, image: vk.Image) void {
self.device.destroyImage(image, ctx.vk_allocator.capture());
}
pub fn destroyImageView(self: *Engine, image_view: vk.ImageView) void {
self.device.destroyImageView(image_view, ctx.vk_allocator.capture());
}
pub fn destroyPipeline(self: *Engine, pipeline: vk.Pipeline) void {
self.device.destroyPipeline(pipeline, ctx.vk_allocator.capture());
}
pub fn destroyPipelineLayout(self: *Engine, pipeline_layout: vk.PipelineLayout) void {
self.device.destroyPipelineLayout(pipeline_layout, ctx.vk_allocator.capture());
}
pub fn destroySampler(self: *Engine, sampler: vk.Sampler) void {
self.device.destroySampler(sampler, ctx.vk_allocator.capture());
}
pub fn destroySemaphore(self: *Engine, semaphore: vk.Semaphore) void {
self.device.destroySemaphore(semaphore, ctx.vk_allocator.capture());
}
pub fn destroyShaderModule(self: *Engine, shader_module: vk.ShaderModule) void {
self.device.destroyShaderModule(shader_module, ctx.vk_allocator.capture());
}
pub fn destroySwapchain(self: *Engine, swapchain: vk.SwapchainKHR) void {
self.device.destroySwapchainKHR(swapchain, ctx.vk_allocator.capture());
}
pub fn deviceWaitIdle(self: *Engine) !void {
try self.device.deviceWaitIdle();
}
pub fn freeCommandBuffer(self: *Engine, free_info: CommandBufferFreeInfo) void {
const command_pool = self.resolveCommandPool(free_info.queue_type);
const command_buffers = [_]vk.CommandBuffer{free_info.command_buffer};
self.device.freeCommandBuffers(command_pool, &command_buffers);
}
pub fn freeDescriptorSet(self: *Engine, descriptor_pool: vk.DescriptorPool, descriptor_set: vk.DescriptorSet) void {
const descriptor_sets = [_]vk.DescriptorSet{descriptor_set};
self.device.freeDescriptorSets(descriptor_pool, &descriptor_sets) catch unreachable;
}
pub fn freeMemory(self: *Engine, device_memory: vk.DeviceMemory) void {
self.device.freeMemory(device_memory, ctx.vk_allocator.capture());
}
pub fn getBufferMemoryRequirements(self: *Engine, buffer: vk.Buffer) vk.MemoryRequirements {
return self.device.getBufferMemoryRequirements(buffer);
}
pub fn getImageMemoryRequirements(self: *Engine, image: vk.Image) vk.MemoryRequirements {
return self.device.getImageMemoryRequirements(image);
}
pub fn getPhysicalDeviceSurfaceCapabilities(self: *Engine) !vk.SurfaceCapabilitiesKHR {
const surface_capabilities = try self.instance.getPhysicalDeviceSurfaceCapabilitiesKHR(self.physical_device, self.surface);
return surface_capabilities;
}
pub fn getPhysicalDeviceSurfaceFormatsAlloc(self: *Engine, allocator: std.mem.Allocator) ![]vk.SurfaceFormatKHR {
const surface_formats = try self.instance.getPhysicalDeviceSurfaceFormatsAllocKHR(self.physical_device, self.surface, allocator);
return surface_formats;
}
pub fn getSwapchainImagesAlloc(self: *Engine, swapchain: vk.SwapchainKHR, allocator: std.mem.Allocator) ![]vk.Image {
const images = try self.device.getSwapchainImagesAllocKHR(swapchain, allocator);
return images;
}
pub fn mapMemory(self: *Engine, memory: vk.DeviceMemory, offset: vk.DeviceSize, size: vk.DeviceSize, flags: vk.MemoryMapFlags) ![]u8 {
const mapped_memory = try self.device.mapMemory(memory, offset, size, flags);
return @as([*]u8, @ptrCast(mapped_memory))[0..size];
}
pub fn resetFence(self: *Engine, fence: vk.Fence) !void {
const fences = [_]vk.Fence{fence};
try self.device.resetFences(&fences);
}
pub fn unmapMemory(self: *Engine, memory: vk.DeviceMemory) void {
self.device.unmapMemory(memory);
}
pub fn updateDescriptorSets(self: *Engine, update_info: DescriptorSetsUpdateInfo) !void {
const allocator_frame = ctx.allocator_frame;
const descriptor_writes = try allocator_frame.alloc(vk.WriteDescriptorSet, update_info.writes.len);
for (descriptor_writes, update_info.writes) |*x, write| {
x.* = switch (write.descriptor_infos) {
.image => |y| .{
.dst_set = write.dst_set,
.dst_binding = write.dst_binding,
.dst_array_element = write.dst_array_element,
.descriptor_count = @intCast(y.len),
.descriptor_type = write.descriptor_type,
.p_image_info = y.ptr,
.p_buffer_info = &.{},
.p_texel_buffer_view = &.{},
},
.buffer => |y| .{
.dst_set = write.dst_set,
.dst_binding = write.dst_binding,
.dst_array_element = write.dst_array_element,
.descriptor_count = @intCast(y.len),
.descriptor_type = write.descriptor_type,
.p_image_info = &.{},
.p_buffer_info = y.ptr,
.p_texel_buffer_view = &.{},
},
.texel_buffer_view => |y| .{
.dst_set = write.dst_set,
.dst_binding = write.dst_binding,
.dst_array_element = write.dst_array_element,
.descriptor_count = @intCast(y.len),
.descriptor_type = write.descriptor_type,
.p_image_info = &.{},
.p_buffer_info = &.{},
.p_texel_buffer_view = y.ptr,
},
};
}
self.device.updateDescriptorSets(descriptor_writes, update_info.copies);
}
pub fn waitForFence(self: *Engine, fence: vk.Fence) !void {
const fences = [_]vk.Fence{fence};
_ = try self.device.waitForFences(&fences, .true, 1 * std.time.ns_per_s);
}