Make use of some globals to stop typing engine everywhere.
This commit is contained in:
@@ -2,6 +2,7 @@ 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");
|
||||
|
||||
@@ -10,17 +11,6 @@ const QueueType = @import("QueueType.zig").QueueType;
|
||||
const VkAllocator = @import("VkAllocator.zig");
|
||||
const WaitSemaphore = @import("WaitSemaphore.zig");
|
||||
|
||||
pub const Mode = union(enum) {
|
||||
headless: void,
|
||||
surface: struct {
|
||||
window: *glfw.Window,
|
||||
surface: vk.SurfaceKHR,
|
||||
presentation_queue: Queue,
|
||||
},
|
||||
};
|
||||
|
||||
pub const ModeTag = std.meta.Tag(Mode);
|
||||
|
||||
const QueueAllocator = struct {
|
||||
queue_families_properties_buffer: [max_queue_families]vk.QueueFamilyProperties,
|
||||
queue_families_in_use: [max_queue_families]u32,
|
||||
@@ -69,18 +59,15 @@ const QueueAllocations = struct {
|
||||
graphics_queue: Queue.Allocation,
|
||||
compute_queue: Queue.Allocation,
|
||||
transfer_queue: Queue.Allocation,
|
||||
/// Defined only in `surface` mode, `undefined` otherwise.
|
||||
presentation_queue: Queue.Allocation,
|
||||
};
|
||||
|
||||
const DebugUtilsMessenger = if (debug) vk.DebugUtilsMessengerEXT else void;
|
||||
|
||||
mode: Mode,
|
||||
vk_allocator: *VkAllocator,
|
||||
|
||||
base: vk.BaseWrapper,
|
||||
instance: vk.InstanceProxy,
|
||||
device: vk.DeviceProxy,
|
||||
surface: vk.SurfaceKHR,
|
||||
|
||||
debug_utils_messenger: DebugUtilsMessenger,
|
||||
physical_device: vk.PhysicalDevice,
|
||||
@@ -90,6 +77,7 @@ 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,
|
||||
@@ -99,15 +87,12 @@ prng_ptr: *std.Random.Pcg,
|
||||
random: std.Random,
|
||||
|
||||
const debug = @import("builtin").mode == .Debug;
|
||||
|
||||
const vk_application_info: vk.ApplicationInfo = .{
|
||||
.p_application_name = c.app_name,
|
||||
.application_version = @bitCast(vk_version),
|
||||
.p_engine_name = c.app_name,
|
||||
.engine_version = @bitCast(vk_version),
|
||||
.api_version = @bitCast(vk.API_VERSION_1_4),
|
||||
};
|
||||
|
||||
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,
|
||||
@@ -124,16 +109,13 @@ const vk_debug_utils_messenger_create_info: vk.DebugUtilsMessengerCreateInfoEXT
|
||||
.p_user_data = null,
|
||||
};
|
||||
|
||||
const vk_version: vk.Version = vk.makeApiVersion(
|
||||
0,
|
||||
c.app_version.major,
|
||||
c.app_version.minor,
|
||||
c.app_version.patch,
|
||||
);
|
||||
pub fn init() !*Engine {
|
||||
const allocator_persistent = ctx.allocator_persistent;
|
||||
const io = ctx.io;
|
||||
const window = ctx.window;
|
||||
|
||||
pub fn init(allocator: std.mem.Allocator, io: std.Io, maybe_window: ?*glfw.Window) !Engine {
|
||||
const vk_allocator = try VkAllocator.init(allocator, io);
|
||||
errdefer vk_allocator.deinit();
|
||||
const engine = try allocator_persistent.create(Engine);
|
||||
errdefer allocator_persistent.destroy(engine);
|
||||
|
||||
// --- LOAD BASE DISPATCH --------------------------------------------------
|
||||
|
||||
@@ -149,35 +131,39 @@ pub fn init(allocator: std.mem.Allocator, io: std.Io, maybe_window: ?*glfw.Windo
|
||||
try enabled_extensions.appendBounded(vk.extensions.ext_debug_utils.name);
|
||||
}
|
||||
|
||||
if (maybe_window != null) {
|
||||
const glfw_instance_extensions = try glfw.getRequiredInstanceExtensions();
|
||||
try enabled_extensions.appendSliceBounded(glfw_instance_extensions);
|
||||
}
|
||||
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 = &vk_application_info,
|
||||
.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,
|
||||
}, &vk_allocator.interface);
|
||||
}, ctx.vk_allocator.capture());
|
||||
};
|
||||
|
||||
const instance_wrapper_ptr = try allocator.create(vk.InstanceWrapper);
|
||||
errdefer allocator.destroy(instance_wrapper_ptr);
|
||||
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(&vk_allocator.interface);
|
||||
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, &vk_allocator.interface) else {};
|
||||
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, &vk_allocator.interface);
|
||||
if (debug) instance.destroyDebugUtilsMessengerEXT(debug_utils_messenger, ctx.vk_allocator.capture());
|
||||
}
|
||||
|
||||
// --- CHOOSE PHYSICAL DEVICE, GET ITS MEMORY PROPERTIES -------------------
|
||||
@@ -208,20 +194,20 @@ pub fn init(allocator: std.mem.Allocator, io: std.Io, maybe_window: ?*glfw.Windo
|
||||
|
||||
const physical_device_memory_properties = instance.getPhysicalDeviceMemoryProperties(physical_device);
|
||||
|
||||
var memory_types = try std.ArrayList(vk.MemoryType).initCapacity(allocator, vk.MAX_MEMORY_TYPES);
|
||||
errdefer memory_types.deinit(allocator);
|
||||
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, vk.MAX_MEMORY_HEAPS);
|
||||
errdefer memory_heaps.deinit(allocator);
|
||||
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;
|
||||
if (maybe_window) |window| try glfw.createWindowSurface(instance_handle, window, &vk_allocator.interface, &surface);
|
||||
try glfw.createWindowSurface(instance_handle, window, ctx.vk_allocator.capture(), &surface);
|
||||
errdefer {
|
||||
if (maybe_window != null) instance.destroySurfaceKHR(surface, &vk_allocator.interface);
|
||||
instance.destroySurfaceKHR(surface, ctx.vk_allocator.capture());
|
||||
}
|
||||
|
||||
// --- ALLOCATE QUEUES -----------------------------------------------------
|
||||
@@ -238,12 +224,12 @@ pub fn init(allocator: std.mem.Allocator, io: std.Io, maybe_window: ?*glfw.Windo
|
||||
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 = if (maybe_window != null) (try findPresentationQueueFamily(queue_families_properties, instance, physical_device, surface) orelse return error.NoPresentationQueue) else undefined;
|
||||
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 = if (maybe_window != null) queue_allocator.allocateQueueIndex(presentation_queue_family) else undefined;
|
||||
const presentation_queue_index = queue_allocator.allocateQueueIndex(presentation_queue_family);
|
||||
|
||||
const queue_priorities = [_]f32{1.0} ** max_queues;
|
||||
|
||||
@@ -261,7 +247,7 @@ pub fn init(allocator: std.mem.Allocator, io: std.Io, maybe_window: ?*glfw.Windo
|
||||
.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 = if (maybe_window != null) .{ .family = presentation_queue_family, .index = presentation_queue_index } else undefined,
|
||||
.presentation_queue = .{ .family = presentation_queue_family, .index = presentation_queue_index },
|
||||
};
|
||||
};
|
||||
|
||||
@@ -271,9 +257,7 @@ pub fn init(allocator: std.mem.Allocator, io: std.Io, maybe_window: ?*glfw.Windo
|
||||
var enabled_extensions_buffer: [16][*:0]const u8 = undefined;
|
||||
var enabled_extensions: std.ArrayList([*:0]const u8) = .initBuffer(&enabled_extensions_buffer);
|
||||
|
||||
if (maybe_window != null) {
|
||||
try enabled_extensions.appendBounded(vk.extensions.khr_swapchain.name);
|
||||
}
|
||||
try enabled_extensions.appendBounded(vk.extensions.khr_swapchain.name);
|
||||
|
||||
var enabled_features_vulkan10: vk.PhysicalDeviceFeatures2 = .{
|
||||
.features = .{
|
||||
@@ -319,35 +303,35 @@ pub fn init(allocator: std.mem.Allocator, io: std.Io, maybe_window: ?*glfw.Windo
|
||||
.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,
|
||||
}, &vk_allocator.interface);
|
||||
}, ctx.vk_allocator.capture());
|
||||
};
|
||||
|
||||
const device_wrapper_ptr = try allocator.create(vk.DeviceWrapper);
|
||||
errdefer allocator.destroy(device_wrapper_ptr);
|
||||
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(&vk_allocator.interface);
|
||||
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,
|
||||
}, &vk_allocator.interface);
|
||||
errdefer device.destroyCommandPool(graphics_command_pool, &vk_allocator.interface);
|
||||
}, 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,
|
||||
}, &vk_allocator.interface);
|
||||
errdefer device.destroyCommandPool(compute_command_pool, &vk_allocator.interface);
|
||||
}, 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,
|
||||
}, &vk_allocator.interface);
|
||||
errdefer device.destroyCommandPool(transfer_command_pool, &vk_allocator.interface);
|
||||
}, ctx.vk_allocator.capture());
|
||||
errdefer device.destroyCommandPool(transfer_command_pool, ctx.vk_allocator.capture());
|
||||
|
||||
const graphics_queue = Queue.init(
|
||||
device.getDeviceQueue(
|
||||
@@ -370,18 +354,18 @@ pub fn init(allocator: std.mem.Allocator, io: std.Io, maybe_window: ?*glfw.Windo
|
||||
),
|
||||
queue_allocations.transfer_queue,
|
||||
);
|
||||
const presentation_queue: Queue = if (maybe_window != null) .init(
|
||||
const presentation_queue = Queue.init(
|
||||
device.getDeviceQueue(
|
||||
queue_allocations.presentation_queue.family,
|
||||
queue_allocations.presentation_queue.index,
|
||||
),
|
||||
queue_allocations.presentation_queue,
|
||||
) else undefined;
|
||||
);
|
||||
|
||||
// --- CREATE AND SEED RNG -------------------------------------------------
|
||||
|
||||
const prng_ptr = try allocator.create(std.Random.Pcg);
|
||||
errdefer allocator.destroy(prng_ptr);
|
||||
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);
|
||||
@@ -389,17 +373,11 @@ pub fn init(allocator: std.mem.Allocator, io: std.Io, maybe_window: ?*glfw.Windo
|
||||
|
||||
// -------------------------------------------------------------------------
|
||||
|
||||
return .{
|
||||
.mode = if (maybe_window) |window| .{ .surface = .{
|
||||
.window = window,
|
||||
.surface = surface,
|
||||
.presentation_queue = presentation_queue,
|
||||
} } else .{ .headless = {} },
|
||||
.vk_allocator = vk_allocator,
|
||||
|
||||
engine.* = .{
|
||||
.base = base,
|
||||
.instance = instance,
|
||||
.device = device,
|
||||
.surface = surface,
|
||||
|
||||
.debug_utils_messenger = debug_utils_messenger,
|
||||
.physical_device = physical_device,
|
||||
@@ -409,6 +387,7 @@ pub fn init(allocator: std.mem.Allocator, io: std.Io, maybe_window: ?*glfw.Windo
|
||||
.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,
|
||||
@@ -417,35 +396,23 @@ pub fn init(allocator: std.mem.Allocator, io: std.Io, maybe_window: ?*glfw.Windo
|
||||
.prng_ptr = prng_ptr,
|
||||
.random = random,
|
||||
};
|
||||
|
||||
return engine;
|
||||
}
|
||||
|
||||
pub fn deinit(self: *Engine) void {
|
||||
std.log.scoped(.deinit).debug("Deinitializing {*}", .{self});
|
||||
|
||||
const allocator = self.vk_allocator.allocator;
|
||||
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());
|
||||
|
||||
allocator.destroy(self.prng_ptr);
|
||||
self.device.destroyDevice(ctx.vk_allocator.capture());
|
||||
self.instance.destroySurfaceKHR(self.surface, ctx.vk_allocator.capture());
|
||||
|
||||
self.device.destroyCommandPool(self.graphics_command_pool, &self.vk_allocator.interface);
|
||||
self.device.destroyCommandPool(self.compute_command_pool, &self.vk_allocator.interface);
|
||||
self.device.destroyCommandPool(self.transfer_command_pool, &self.vk_allocator.interface);
|
||||
if (debug) self.instance.destroyDebugUtilsMessengerEXT(self.debug_utils_messenger, ctx.vk_allocator.capture());
|
||||
|
||||
self.device.destroyDevice(&self.vk_allocator.interface);
|
||||
allocator.destroy(self.device.wrapper);
|
||||
|
||||
if (std.meta.activeTag(self.mode) == .surface) {
|
||||
self.instance.destroySurfaceKHR(self.mode.surface.surface, &self.vk_allocator.interface);
|
||||
}
|
||||
|
||||
self.memory_heaps.deinit(allocator);
|
||||
self.memory_types.deinit(allocator);
|
||||
|
||||
if (debug) self.instance.destroyDebugUtilsMessengerEXT(self.debug_utils_messenger, &self.vk_allocator.interface);
|
||||
|
||||
self.instance.destroyInstance(&self.vk_allocator.interface);
|
||||
allocator.destroy(self.instance.wrapper);
|
||||
|
||||
self.vk_allocator.deinit();
|
||||
self.instance.destroyInstance(ctx.vk_allocator.capture());
|
||||
|
||||
self.* = undefined;
|
||||
}
|
||||
@@ -458,7 +425,7 @@ pub fn allocate(self: *const Engine, memory_requirements: vk.MemoryRequirements,
|
||||
return try self.device.allocateMemory(&.{
|
||||
.allocation_size = memory_requirements.size,
|
||||
.memory_type_index = @truncate(i),
|
||||
}, &self.vk_allocator.interface);
|
||||
}, ctx.vk_allocator.capture());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -536,6 +503,8 @@ pub fn queueSubmit(
|
||||
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,
|
||||
@@ -545,8 +514,8 @@ pub fn queueSubmit(
|
||||
const command_buffers = [_]vk.CommandBuffer{command_buffer};
|
||||
|
||||
var wait_semaphores = std.MultiArrayList(WaitSemaphore){};
|
||||
defer wait_semaphores.deinit(self.vk_allocator.allocator);
|
||||
try wait_semaphores.ensureTotalCapacity(self.vk_allocator.allocator, submit_info.wait_semaphores.len);
|
||||
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);
|
||||
@@ -577,7 +546,7 @@ 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.mode.surface.presentation_queue.handle, &.{
|
||||
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,
|
||||
@@ -974,13 +943,11 @@ pub fn bindImageMemory(self: *Engine, image: vk.Image, memory: vk.DeviceMemory,
|
||||
}
|
||||
|
||||
pub fn createBuffer(self: *Engine, create_info: BufferCreateInfo) !vk.Buffer {
|
||||
var arena = self.makeArena();
|
||||
defer arena.deinit();
|
||||
const allocator = arena.allocator();
|
||||
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, queue_family_index, {});
|
||||
try queue_family_indices_set.put(allocator_frame, queue_family_index, {});
|
||||
}
|
||||
|
||||
const queue_family_indices = queue_family_indices_set.keys();
|
||||
@@ -992,14 +959,12 @@ pub fn createBuffer(self: *Engine, create_info: BufferCreateInfo) !vk.Buffer {
|
||||
.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,
|
||||
}, &self.vk_allocator.interface);
|
||||
}, ctx.vk_allocator.capture());
|
||||
return buffer;
|
||||
}
|
||||
|
||||
pub fn createDescriptorSetLayout(self: *Engine, create_info: DescriptorSetLayoutCreateInfo) !vk.DescriptorSetLayout {
|
||||
var arena = self.makeArena();
|
||||
defer arena.deinit();
|
||||
const allocator = arena.allocator();
|
||||
const allocator_frame = ctx.allocator_frame;
|
||||
|
||||
const has_binding_flags = blk: {
|
||||
for (create_info.bindings) |binding| {
|
||||
@@ -1013,7 +978,7 @@ pub fn createDescriptorSetLayout(self: *Engine, create_info: DescriptorSetLayout
|
||||
var p_next: ?*const anyopaque = null;
|
||||
|
||||
if (has_binding_flags) {
|
||||
const descriptor_set_layout_bindings_flags = try allocator.alloc(vk.DescriptorBindingFlags, create_info.bindings.len);
|
||||
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;
|
||||
}
|
||||
@@ -1024,7 +989,7 @@ pub fn createDescriptorSetLayout(self: *Engine, create_info: DescriptorSetLayout
|
||||
};
|
||||
}
|
||||
|
||||
const bindings = try allocator.alloc(vk.DescriptorSetLayoutBinding, create_info.bindings.len);
|
||||
const bindings = try allocator_frame.alloc(vk.DescriptorSetLayoutBinding, create_info.bindings.len);
|
||||
for (bindings, create_info.bindings) |*x, binding| {
|
||||
x.* = .{
|
||||
.binding = binding.binding,
|
||||
@@ -1040,7 +1005,7 @@ pub fn createDescriptorSetLayout(self: *Engine, create_info: DescriptorSetLayout
|
||||
.flags = create_info.flags,
|
||||
.binding_count = @intCast(bindings.len),
|
||||
.p_bindings = bindings.ptr,
|
||||
}, &self.vk_allocator.interface);
|
||||
}, ctx.vk_allocator.capture());
|
||||
return descriptor_set_layout;
|
||||
}
|
||||
|
||||
@@ -1050,21 +1015,19 @@ pub fn createDescriptorPool(self: *Engine, create_info: DescriptorPoolCreateInfo
|
||||
.max_sets = create_info.max_sets,
|
||||
.pool_size_count = @intCast(create_info.pool_sizes.len),
|
||||
.p_pool_sizes = create_info.pool_sizes.ptr,
|
||||
}, &self.vk_allocator.interface);
|
||||
}, 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, &self.vk_allocator.interface);
|
||||
const fence = try self.device.createFence(&create_info, ctx.vk_allocator.capture());
|
||||
return fence;
|
||||
}
|
||||
|
||||
pub fn createGraphicsPipeline(self: *Engine, create_info: GraphicsPipelineCreateInfo) !vk.Pipeline {
|
||||
var arena = self.makeArena();
|
||||
defer arena.deinit();
|
||||
const allocator = arena.allocator();
|
||||
const allocator_frame = ctx.allocator_frame;
|
||||
|
||||
const stages = try allocator.alloc(vk.PipelineShaderStageCreateInfo, create_info.stages.len);
|
||||
const stages = try allocator_frame.alloc(vk.PipelineShaderStageCreateInfo, create_info.stages.len);
|
||||
for (stages, create_info.stages) |*x, stage| {
|
||||
x.* = .{
|
||||
.flags = stage.flags,
|
||||
@@ -1073,7 +1036,7 @@ pub fn createGraphicsPipeline(self: *Engine, create_info: GraphicsPipelineCreate
|
||||
.module = stage.module,
|
||||
.p_specialization_info = blk: {
|
||||
if (stage.specialization_info) |y| {
|
||||
const specialization_info = try allocator.create(vk.SpecializationInfo);
|
||||
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,
|
||||
@@ -1142,18 +1105,16 @@ pub fn createGraphicsPipeline(self: *Engine, create_info: GraphicsPipelineCreate
|
||||
};
|
||||
|
||||
var pipelines: [1]vk.Pipeline = undefined;
|
||||
_ = try self.device.createGraphicsPipelines(.null_handle, &graphics_pipeline_create_infos, &self.vk_allocator.interface, &pipelines);
|
||||
_ = 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 {
|
||||
var arena = self.makeArena();
|
||||
defer arena.deinit();
|
||||
const allocator = arena.allocator();
|
||||
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, queue_family_index, {});
|
||||
try queue_family_indices_set.put(allocator_frame, queue_family_index, {});
|
||||
}
|
||||
|
||||
const queue_family_indices = queue_family_indices_set.keys();
|
||||
@@ -1172,7 +1133,7 @@ pub fn createImage(self: *Engine, create_info: ImageCreateInfo) !vk.Image {
|
||||
.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,
|
||||
}, &self.vk_allocator.interface);
|
||||
}, ctx.vk_allocator.capture());
|
||||
return image;
|
||||
}
|
||||
|
||||
@@ -1184,7 +1145,7 @@ pub fn createImageView(self: *Engine, create_info: ImageViewCreateInfo) !vk.Imag
|
||||
.format = create_info.format,
|
||||
.components = create_info.components,
|
||||
.subresource_range = create_info.subresource_range,
|
||||
}, &self.vk_allocator.interface);
|
||||
}, ctx.vk_allocator.capture());
|
||||
return image_view;
|
||||
}
|
||||
|
||||
@@ -1195,17 +1156,17 @@ pub fn createPipelineLayout(self: *Engine, create_info: PipelineLayoutCreateInfo
|
||||
.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,
|
||||
}, &self.vk_allocator.interface);
|
||||
}, 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, &self.vk_allocator.interface);
|
||||
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(&.{}, &self.vk_allocator.interface);
|
||||
const semaphore = try self.device.createSemaphore(&.{}, ctx.vk_allocator.capture());
|
||||
return semaphore;
|
||||
}
|
||||
|
||||
@@ -1214,18 +1175,16 @@ pub fn createShaderModule(self: *Engine, create_info: ShaderModuleCreateInfo) !v
|
||||
.flags = create_info.flags,
|
||||
.code_size = create_info.code.len,
|
||||
.p_code = @ptrCast(create_info.code.ptr),
|
||||
}, &self.vk_allocator.interface);
|
||||
}, ctx.vk_allocator.capture());
|
||||
return shader_module;
|
||||
}
|
||||
|
||||
pub fn createSwapchain(self: *Engine, create_info: SwapchainCreateInfo) !vk.SwapchainKHR {
|
||||
var arena = self.makeArena();
|
||||
defer arena.deinit();
|
||||
const allocator = arena.allocator();
|
||||
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, queue_family_index, {});
|
||||
try queue_family_indices_set.put(allocator_frame, queue_family_index, {});
|
||||
}
|
||||
|
||||
const queue_family_indices = queue_family_indices_set.keys();
|
||||
@@ -1247,56 +1206,56 @@ pub fn createSwapchain(self: *Engine, create_info: SwapchainCreateInfo) !vk.Swap
|
||||
.present_mode = create_info.present_mode,
|
||||
.clipped = create_info.clipped,
|
||||
.old_swapchain = create_info.old_swapchain,
|
||||
}, &self.vk_allocator.interface);
|
||||
}, ctx.vk_allocator.capture());
|
||||
return swapchain;
|
||||
}
|
||||
|
||||
pub fn destroyBuffer(self: *Engine, buffer: vk.Buffer) void {
|
||||
self.device.destroyBuffer(buffer, &self.vk_allocator.interface);
|
||||
self.device.destroyBuffer(buffer, ctx.vk_allocator.capture());
|
||||
}
|
||||
|
||||
pub fn destroyDescriptorPool(self: *Engine, descriptor_pool: vk.DescriptorPool) void {
|
||||
self.device.destroyDescriptorPool(descriptor_pool, &self.vk_allocator.interface);
|
||||
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, &self.vk_allocator.interface);
|
||||
self.device.destroyDescriptorSetLayout(descriptor_set_layout, ctx.vk_allocator.capture());
|
||||
}
|
||||
|
||||
pub fn destroyFence(self: *Engine, fence: vk.Fence) void {
|
||||
self.device.destroyFence(fence, &self.vk_allocator.interface);
|
||||
self.device.destroyFence(fence, ctx.vk_allocator.capture());
|
||||
}
|
||||
|
||||
pub fn destroyImage(self: *Engine, image: vk.Image) void {
|
||||
self.device.destroyImage(image, &self.vk_allocator.interface);
|
||||
self.device.destroyImage(image, ctx.vk_allocator.capture());
|
||||
}
|
||||
|
||||
pub fn destroyImageView(self: *Engine, image_view: vk.ImageView) void {
|
||||
self.device.destroyImageView(image_view, &self.vk_allocator.interface);
|
||||
self.device.destroyImageView(image_view, ctx.vk_allocator.capture());
|
||||
}
|
||||
|
||||
pub fn destroyPipeline(self: *Engine, pipeline: vk.Pipeline) void {
|
||||
self.device.destroyPipeline(pipeline, &self.vk_allocator.interface);
|
||||
self.device.destroyPipeline(pipeline, ctx.vk_allocator.capture());
|
||||
}
|
||||
|
||||
pub fn destroyPipelineLayout(self: *Engine, pipeline_layout: vk.PipelineLayout) void {
|
||||
self.device.destroyPipelineLayout(pipeline_layout, &self.vk_allocator.interface);
|
||||
self.device.destroyPipelineLayout(pipeline_layout, ctx.vk_allocator.capture());
|
||||
}
|
||||
|
||||
pub fn destroySampler(self: *Engine, sampler: vk.Sampler) void {
|
||||
self.device.destroySampler(sampler, &self.vk_allocator.interface);
|
||||
self.device.destroySampler(sampler, ctx.vk_allocator.capture());
|
||||
}
|
||||
|
||||
pub fn destroySemaphore(self: *Engine, semaphore: vk.Semaphore) void {
|
||||
self.device.destroySemaphore(semaphore, &self.vk_allocator.interface);
|
||||
self.device.destroySemaphore(semaphore, ctx.vk_allocator.capture());
|
||||
}
|
||||
|
||||
pub fn destroyShaderModule(self: *Engine, shader_module: vk.ShaderModule) void {
|
||||
self.device.destroyShaderModule(shader_module, &self.vk_allocator.interface);
|
||||
self.device.destroyShaderModule(shader_module, ctx.vk_allocator.capture());
|
||||
}
|
||||
|
||||
pub fn destroySwapchain(self: *Engine, swapchain: vk.SwapchainKHR) void {
|
||||
self.device.destroySwapchainKHR(swapchain, &self.vk_allocator.interface);
|
||||
self.device.destroySwapchainKHR(swapchain, ctx.vk_allocator.capture());
|
||||
}
|
||||
|
||||
pub fn deviceWaitIdle(self: *Engine) !void {
|
||||
@@ -1315,7 +1274,7 @@ pub fn freeDescriptorSet(self: *Engine, descriptor_pool: vk.DescriptorPool, desc
|
||||
}
|
||||
|
||||
pub fn freeMemory(self: *Engine, device_memory: vk.DeviceMemory) void {
|
||||
self.device.freeMemory(device_memory, &self.vk_allocator.interface);
|
||||
self.device.freeMemory(device_memory, ctx.vk_allocator.capture());
|
||||
}
|
||||
|
||||
pub fn getBufferMemoryRequirements(self: *Engine, buffer: vk.Buffer) vk.MemoryRequirements {
|
||||
@@ -1327,12 +1286,12 @@ pub fn getImageMemoryRequirements(self: *Engine, image: vk.Image) vk.MemoryRequi
|
||||
}
|
||||
|
||||
pub fn getPhysicalDeviceSurfaceCapabilities(self: *Engine) !vk.SurfaceCapabilitiesKHR {
|
||||
const surface_capabilities = try self.instance.getPhysicalDeviceSurfaceCapabilitiesKHR(self.physical_device, self.mode.surface.surface);
|
||||
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.mode.surface.surface, allocator);
|
||||
const surface_formats = try self.instance.getPhysicalDeviceSurfaceFormatsAllocKHR(self.physical_device, self.surface, allocator);
|
||||
return surface_formats;
|
||||
}
|
||||
|
||||
@@ -1356,11 +1315,9 @@ pub fn unmapMemory(self: *Engine, memory: vk.DeviceMemory) void {
|
||||
}
|
||||
|
||||
pub fn updateDescriptorSets(self: *Engine, update_info: DescriptorSetsUpdateInfo) !void {
|
||||
var arena = self.makeArena();
|
||||
defer arena.deinit();
|
||||
const allocator = arena.allocator();
|
||||
const allocator_frame = ctx.allocator_frame;
|
||||
|
||||
const descriptor_writes = try allocator.alloc(vk.WriteDescriptorSet, update_info.writes.len);
|
||||
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| .{
|
||||
|
||||
Reference in New Issue
Block a user