100 lines
3.3 KiB
Zig
100 lines
3.3 KiB
Zig
const std = @import("std");
|
|
const vk = @import("vulkan");
|
|
|
|
const context = @import("context.zig");
|
|
|
|
pub const LogicalDeviceContext = struct {
|
|
physical_device: vk.PhysicalDevice,
|
|
graphics_queue_family_index: u32,
|
|
device: vk.Device,
|
|
vkd: vk.DeviceWrapper,
|
|
graphics_queue: vk.Queue,
|
|
|
|
pub fn destroy(self: *const LogicalDeviceContext) void {
|
|
self.vkd.destroyDevice(self.device, null);
|
|
}
|
|
};
|
|
|
|
pub fn initLogicalDevice(
|
|
vc: context.VulkanContext,
|
|
physical_device: vk.PhysicalDevice,
|
|
graphics_queue_family_index: u32,
|
|
) !LogicalDeviceContext {
|
|
const queue_priority: f32 = 1.0;
|
|
|
|
const queue_create_info = vk.DeviceQueueCreateInfo{
|
|
.queue_family_index = graphics_queue_family_index,
|
|
.queue_count = 1,
|
|
.p_queue_priorities = @ptrCast(&queue_priority),
|
|
};
|
|
|
|
const device_extensions = [_][*:0]const u8{
|
|
"VK_KHR_swapchain",
|
|
};
|
|
|
|
const device_create_info = vk.DeviceCreateInfo{
|
|
.queue_create_info_count = 1,
|
|
.p_queue_create_infos = @ptrCast(&queue_create_info),
|
|
.enabled_extension_count = device_extensions.len,
|
|
.pp_enabled_extension_names = &device_extensions,
|
|
};
|
|
|
|
const device = try vc.vki.createDevice(physical_device, &device_create_info, null);
|
|
errdefer vc.vki.destroyDevice(device, null);
|
|
std.debug.print("created logical device\n", .{});
|
|
|
|
const vkd = vk.DeviceWrapper.load(device, vc.vki.dispatch.vkGetDeviceProcAddr.?);
|
|
const graphics_queue = vkd.getDeviceQueue(device, graphics_queue_family_index, 0);
|
|
std.debug.print("retrieved graphics queue\n", .{});
|
|
|
|
return .{
|
|
.physical_device = physical_device,
|
|
.graphics_queue_family_index = graphics_queue_family_index,
|
|
.device = device,
|
|
.vkd = vkd,
|
|
.graphics_queue = graphics_queue,
|
|
};
|
|
}
|
|
|
|
pub fn debugPhysicalGPUs(
|
|
vc: context.VulkanContext,
|
|
physical_devices: []vk.PhysicalDevice,
|
|
surface: vk.SurfaceKHR,
|
|
) !void {
|
|
std.debug.print("physical devices: {}\n", .{physical_devices.len});
|
|
|
|
for (physical_devices, 0..) |physical_device, i| {
|
|
const props = vc.vki.getPhysicalDeviceProperties(physical_device);
|
|
std.debug.print("device {}: {s}\n", .{ i, std.mem.sliceTo(&props.device_name, 0) });
|
|
const queue_families = try vc.vki.getPhysicalDeviceQueueFamilyPropertiesAlloc(
|
|
physical_device,
|
|
std.heap.page_allocator,
|
|
);
|
|
defer std.heap.page_allocator.free(queue_families);
|
|
|
|
for (queue_families, 0..) |queue_family, queue_index| {
|
|
const supports_graphics = queue_family.queue_flags.graphics_bit;
|
|
const supports_compute = queue_family.queue_flags.compute_bit;
|
|
const supports_transfer = queue_family.queue_flags.transfer_bit;
|
|
|
|
const supports_present = try vc.vki.getPhysicalDeviceSurfaceSupportKHR(
|
|
physical_device,
|
|
@intCast(queue_index),
|
|
surface,
|
|
);
|
|
|
|
std.debug.print(
|
|
" queue {}: count={}, graphics={}, compute={}, transfer={}, present={}\n",
|
|
.{
|
|
queue_index,
|
|
queue_family.queue_count,
|
|
supports_graphics,
|
|
supports_compute,
|
|
supports_transfer,
|
|
supports_present,
|
|
},
|
|
);
|
|
}
|
|
}
|
|
}
|