Files
VulkanDriver/src/vulkan/Instance.zig
T
kbz_8 e793cb6c3f
Build / build (push) Successful in 4m14s
Test / build_and_test (push) Has been cancelled
fixing debug allocator
2026-05-08 14:23:49 +02:00

121 lines
3.7 KiB
Zig

const std = @import("std");
const builtin = @import("builtin");
const vk = @import("vulkan");
const config = @import("lib.zig").config;
const VkError = @import("error_set.zig").VkError;
const Dispatchable = @import("Dispatchable.zig").Dispatchable;
const PhysicalDevice = @import("PhysicalDevice.zig");
const root = @import("root");
comptime {
if (!builtin.is_test) {
if (!@hasDecl(root, "VULKAN_VERSION")) {
@compileError("Missing VULKAN_VERSION in module root");
}
}
}
const Self = @This();
pub const ObjectType: vk.ObjectType = .instance;
const DeviceAllocator = struct {
pub inline fn allocator(_: @This()) std.mem.Allocator {
return std.heap.smp_allocator;
}
};
physical_devices: std.ArrayList(*Dispatchable(PhysicalDevice)),
dispatch_table: *const DispatchTable,
vtable: *const VTable,
pub const VTable = struct {
releasePhysicalDevices: *const fn (*Self, std.mem.Allocator) VkError!void,
requestPhysicalDevices: *const fn (*Self, std.mem.Allocator) VkError!void,
io: *const fn (*Self) std.Io,
};
pub const DispatchTable = struct {
destroy: *const fn (*Self, std.mem.Allocator) VkError!void,
};
pub fn init(allocator: std.mem.Allocator, infos: *const vk.InstanceCreateInfo) VkError!Self {
_ = allocator;
_ = infos;
return .{
.physical_devices = .empty,
.dispatch_table = undefined,
.vtable = undefined,
};
}
/// Dummy to avoid compile error in tests and doc generation
pub fn create(allocator: std.mem.Allocator, infos: *const vk.InstanceCreateInfo) VkError!*Self {
_ = allocator;
_ = infos;
return VkError.IncompatibleDriver;
}
pub fn deinit(self: *Self, allocator: std.mem.Allocator) VkError!void {
try self.releasePhysicalDevices(allocator);
try self.dispatch_table.destroy(self, allocator);
}
pub fn enumerateLayerProperties(count: *u32, p_properties: ?[*]vk.LayerProperties) VkError!void {
if (comptime !builtin.is_test and @hasDecl(root.Instance, "LAYERS")) {
count.* = root.Instance.LAYERS.len;
if (p_properties) |properties| {
for (root.Instance.LAYERS, properties[0..]) |layer, *prop| {
prop.* = layer;
}
}
} else {
count.* = 0;
}
}
pub fn enumerateExtensionProperties(layer_name: ?[]const u8, count: *u32, p_properties: ?[*]vk.ExtensionProperties) VkError!void {
if (layer_name) |_| {
return VkError.LayerNotPresent;
}
if (comptime !builtin.is_test and @hasDecl(root.Instance, "EXTENSIONS")) {
count.* = root.Instance.EXTENSIONS.len;
if (p_properties) |properties| {
for (root.Instance.EXTENSIONS, properties[0..]) |ext, *prop| {
prop.* = ext;
}
}
} else {
count.* = 0;
}
}
pub fn enumerateVersion(version: *u32) VkError!void {
if (!builtin.is_test) {
version.* = @bitCast(root.VULKAN_VERSION);
} else {
version.* = @bitCast(vk.makeApiVersion(0, 1, 0, 0));
}
}
pub fn releasePhysicalDevices(self: *Self, allocator: std.mem.Allocator) VkError!void {
try self.vtable.releasePhysicalDevices(self, allocator);
}
pub fn requestPhysicalDevices(self: *Self, allocator: std.mem.Allocator) VkError!void {
try self.vtable.requestPhysicalDevices(self, allocator);
if (self.physical_devices.items.len == 0) {
std.log.scoped(.vkCreateInstance).err("No VkPhysicalDevice found", .{});
return;
}
for (self.physical_devices.items) |physical_device| {
std.log.scoped(.vkCreateInstance).debug("Found VkPhysicalDevice named {s}", .{physical_device.object.props.device_name});
}
}
pub fn io(self: *Self) std.Io {
return self.vtable.io(self);
}