adding DRM device search and intel pci device map
Build / build (push) Successful in 50s
Test / build_and_test (push) Successful in 1m51s

This commit is contained in:
2026-06-18 04:23:06 +02:00
parent d68360173f
commit 1e1a00252f
15 changed files with 509 additions and 45 deletions
+13 -5
View File
@@ -3,6 +3,8 @@ const builtin = @import("builtin");
const vk = @import("vulkan");
const config = @import("lib.zig").config;
const utils = @import("utils.zig");
const drm = @import("drm.zig");
const lib = @import("lib.zig");
const VkError = @import("error_set.zig").VkError;
const Dispatchable = @import("Dispatchable.zig").Dispatchable;
@@ -31,12 +33,13 @@ const DeviceAllocator = struct {
pub const EXTENSIONS = [_]vk.ExtensionProperties{};
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,
requestPhysicalDevices: *const fn (*Self, std.mem.Allocator, []lib.drm.Card) VkError!void,
io: *const fn (*Self) std.Io,
};
@@ -144,10 +147,10 @@ pub fn enumerateExtensionProperties(layer_name: ?[]const u8, count: *u32, p_prop
}
pub fn enumerateVersion(version: *u32) VkError!void {
if (!builtin.is_test) {
version.* = @bitCast(root.VULKAN_VERSION);
} else {
if (comptime builtin.is_test) {
version.* = @bitCast(vk.makeApiVersion(0, 1, 0, 0));
} else {
version.* = @bitCast(root.VULKAN_VERSION);
}
}
@@ -156,11 +159,16 @@ pub fn releasePhysicalDevices(self: *Self, allocator: std.mem.Allocator) VkError
}
pub fn requestPhysicalDevices(self: *Self, allocator: std.mem.Allocator) VkError!void {
try self.vtable.requestPhysicalDevices(self, allocator);
const devices = try drm.enumerateDrmPhysicalDevices(allocator, self);
defer allocator.free(devices);
try self.vtable.requestPhysicalDevices(self, allocator, devices);
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});
}
+1 -1
View File
@@ -137,7 +137,7 @@ pub inline fn getSparseImageFormatProperties(
return self.dispatch_table.getSparseImageFormatProperties(self, format, image_type, samples, tiling, usage, properties);
}
pub fn releasePhysicalDevice(self: *Self, allocator: std.mem.Allocator) VkError!void {
pub fn release(self: *Self, allocator: std.mem.Allocator) VkError!void {
self.queue_family_props.deinit(allocator);
self.queue_family_props = .empty;
try self.dispatch_table.release(self, allocator);
+34
View File
@@ -0,0 +1,34 @@
const std = @import("std");
const drm = @import("drm");
const Instance = @import("Instance.zig");
const VkError = @import("error_set.zig").VkError;
pub fn enumerateDrmPhysicalDevices(allocator: std.mem.Allocator, instance: *Instance) VkError![]drm.Card {
const io = instance.io();
var devices: std.ArrayList(drm.Card) = .empty;
errdefer {
for (devices.items[0..]) |card| {
card.close(io);
}
devices.deinit(allocator);
}
var dri_dir = std.Io.Dir.openDirAbsolute(io, drm.dir_name, .{ .iterate = true }) catch return VkError.InitializationFailed;
defer dri_dir.close(io);
var iterator = dri_dir.iterate();
while (iterator.next(io) catch return VkError.InitializationFailed) |entry| {
if (entry.kind != .character_device)
continue;
const path = std.fmt.allocPrint(allocator, "{s}/{s}", .{ drm.dir_name, entry.name }) catch return VkError.OutOfHostMemory;
defer allocator.free(path);
const card = drm.Card.open(io, path) catch continue;
devices.append(allocator, card) catch return VkError.OutOfHostMemory;
}
return devices.toOwnedSlice(allocator) catch VkError.OutOfHostMemory;
}
+1
View File
@@ -13,6 +13,7 @@ pub const logger = @import("logger.zig");
pub const format = @import("format.zig");
pub const config = @import("config");
pub const utils = @import("utils.zig");
pub const drm = @import("drm");
pub const Dispatchable = @import("Dispatchable.zig").Dispatchable;
pub const fallback_host_allocator = @import("fallback_host_allocator.zig").fallback_host_allocator;
+21 -20
View File
@@ -335,9 +335,10 @@ pub export fn apeCreateInstance(info: *const vk.InstanceCreateInfo, callbacks: ?
var instance: *lib.Instance = undefined;
if (!builtin.is_test) {
// Will call impl instead of interface as root refs the impl module
// Will call impl instead of interface as `root` refs the impl module
instance = root.Instance.create(allocator, info) catch |err| return toVkResult(err);
}
instance.requestPhysicalDevices(allocator) catch |err| {
if (!builtin.is_test) instance.deinit(allocator) catch {};
return toVkResult(err);
@@ -392,25 +393,6 @@ pub export fn apeDestroyInstance(p_instance: vk.Instance, callbacks: ?*const vk.
dispatchable.destroy(allocator);
}
pub export fn apeEnumeratePhysicalDevices(p_instance: vk.Instance, count: *u32, p_devices: ?[*]vk.PhysicalDevice) callconv(vk.vulkan_call_conv) vk.Result {
entryPointBeginLogTrace(.vkEnumeratePhysicalDevices);
defer entryPointEndLogTrace();
const instance = Dispatchable(Instance).fromHandleObject(p_instance) catch |err| return toVkResult(err);
const available = instance.physical_devices.items.len;
if (p_devices) |devices| {
const write_count = @min(count.*, available);
for (0..write_count) |i| {
devices[i] = instance.physical_devices.items[i].toVkHandle(vk.PhysicalDevice);
}
count.* = @intCast(write_count);
if (write_count < available) return .incomplete;
} else {
count.* = @intCast(available);
}
return .success;
}
pub export fn apeEnumeratePhysicalDeviceGroups(p_instance: vk.Instance, count: *u32, p_groups: ?[*]vk.PhysicalDeviceGroupProperties) callconv(vk.vulkan_call_conv) vk.Result {
entryPointBeginLogTrace(.vkEnumeratePhysicalDeviceGroups);
defer entryPointEndLogTrace();
@@ -445,6 +427,25 @@ pub export fn apeEnumeratePhysicalDeviceGroupsKHR(p_instance: vk.Instance, count
return @call(.always_inline, apeEnumeratePhysicalDeviceGroups, .{ p_instance, count, p_groups });
}
pub export fn apeEnumeratePhysicalDevices(p_instance: vk.Instance, count: *u32, p_devices: ?[*]vk.PhysicalDevice) callconv(vk.vulkan_call_conv) vk.Result {
entryPointBeginLogTrace(.vkEnumeratePhysicalDevices);
defer entryPointEndLogTrace();
const instance = Dispatchable(Instance).fromHandleObject(p_instance) catch |err| return toVkResult(err);
const available = instance.physical_devices.items.len;
if (p_devices) |devices| {
const write_count = @min(count.*, available);
for (0..write_count) |i| {
devices[i] = instance.physical_devices.items[i].toVkHandle(vk.PhysicalDevice);
}
count.* = @intCast(write_count);
if (write_count < available) return .incomplete;
} else {
count.* = @intCast(available);
}
return .success;
}
// Physical Device functions =================================================================================================================================
pub export fn apeCreateDevice(p_physical_device: vk.PhysicalDevice, info: *const vk.DeviceCreateInfo, callbacks: ?*const vk.AllocationCallbacks, p_device: *vk.Device) callconv(vk.vulkan_call_conv) vk.Result {