Skip to content

Instantly share code, notes, and snippets.

@ant1fact
Created May 4, 2024 20:35
Show Gist options
  • Save ant1fact/fb9c5c72752fed57368eac82cb92c731 to your computer and use it in GitHub Desktop.
Save ant1fact/fb9c5c72752fed57368eac82cb92c731 to your computer and use it in GitHub Desktop.
Enumerate available Vulkan physical devices in Zig
// Zig 0.12.0
// Vulkan 1.3.280.0
// Get device count
var physical_device_count: u32 = 0;
if (vk.vkEnumeratePhysicalDevices(instance, &physical_device_count, null) != vk.VK_SUCCESS) {
std.debug.panic("Failed to get number of physical devices.", .{});
}
// Populate physical devices array
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
const allocator = gpa.allocator();
const physical_devices = try allocator.alloc(vk.VkPhysicalDevice, physical_device_count);
if (vk.vkEnumeratePhysicalDevices(instance, &physical_device_count, physical_devices.ptr) != vk.VK_SUCCESS) {
std.debug.panic("Failed to populate list of physical devices.", .{});
}
// Loop over each device and fill the device_properties struct
var device_properties = try allocator.create(vk.VkPhysicalDeviceProperties);
defer allocator.destroy(device_properties);
for (0.., physical_devices) |i, d| {
vk.vkGetPhysicalDeviceProperties(d, device_properties);
const device_name: [*:0]const u8 = @ptrCast(&device_properties.deviceName);
std.debug.print("\n{d}: {s}\n", .{i, device_name});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment