213 : m_physical_device(other.m_physical_device)
214 , m_logical_device(other.m_logical_device)
215 , m_graphics_queue(other.m_graphics_queue)
216 , m_compute_queue(other.m_compute_queue)
217 , m_transfer_queue(other.m_transfer_queue)
218 , m_queue_families(other.m_queue_families)
219 , m_graphics_presents(other.m_graphics_presents)
220 , m_present_queues(std::move(other.m_present_queues))
221 , m_device_name(std::move(other.m_device_name))
222 , m_device_uuid(other.m_device_uuid)
223 , m_supports_mesh_shaders(other.m_supports_mesh_shaders)
225 other.m_physical_device = VK_NULL_HANDLE;
226 other.m_logical_device = VK_NULL_HANDLE;
227 other.m_graphics_queue = VK_NULL_HANDLE;
228 other.m_compute_queue = VK_NULL_HANDLE;
229 other.m_transfer_queue = VK_NULL_HANDLE;
287 auto devices =
instance.enumeratePhysicalDevices();
289 if (devices.empty()) {
291 std::source_location::current(),
292 "Failed to find GPUs with Vulkan support!");
295 const PresentationProbe probe(
instance);
299 "Presentation support cannot be verified on this platform; "
300 "device selection will not filter on it");
303 std::vector<DeviceCandidate> candidates;
304 candidates.reserve(devices.size());
306 for (uint32_t i = 0; i < devices.size(); ++i) {
307 const auto&
device = devices[i];
309 DeviceCandidate cand;
313 auto available_extensions =
device.enumerateDeviceExtensionProperties();
314 bool has_pci_ext =
false;
316 for (
const auto& ext : available_extensions) {
317 if (strcmp(ext.extensionName, VK_KHR_SWAPCHAIN_EXTENSION_NAME) == 0)
318 cand.has_swapchain =
true;
319 if (strcmp(ext.extensionName, VK_EXT_MESH_SHADER_EXTENSION_NAME) == 0)
320 cand.has_mesh_shader =
true;
321 if (strcmp(ext.extensionName, VK_EXT_PCI_BUS_INFO_EXTENSION_NAME) == 0)
325 auto prop_chain = vk::StructureChain {
326 vk::PhysicalDeviceProperties2 {},
327 vk::PhysicalDeviceVulkan11Properties {},
328 vk::PhysicalDeviceVulkan12Properties {},
329 vk::PhysicalDevicePCIBusInfoPropertiesEXT {}
333 prop_chain.unlink<vk::PhysicalDevicePCIBusInfoPropertiesEXT>();
336 device.getProperties2(&prop_chain.get<vk::PhysicalDeviceProperties2>());
338 const auto& props = prop_chain.get<vk::PhysicalDeviceProperties2>().properties;
339 const auto& props11 = prop_chain.get<vk::PhysicalDeviceVulkan11Properties>();
340 const auto& props12 = prop_chain.get<vk::PhysicalDeviceVulkan12Properties>();
342 cand.name = props.deviceName.data();
343 cand.type = props.deviceType;
344 cand.api_version = props.apiVersion;
345 cand.driver_id = props12.driverID;
346 cand.driver_name = props12.driverName.data();
347 std::copy_n(props11.deviceUUID.data(), VK_UUID_SIZE, cand.uuid.begin());
348 cand.uuid_hex = uuid_to_hex(cand.uuid.data());
351 cand.has_pci_info =
true;
352 cand.pci_bus = prop_chain.get<vk::PhysicalDevicePCIBusInfoPropertiesEXT>().pciBus;
355 auto memory_props =
device.getMemoryProperties();
356 for (uint32_t
h = 0;
h < memory_props.memoryHeapCount; ++
h) {
357 if (memory_props.memoryHeaps[
h].flags & vk::MemoryHeapFlagBits::eDeviceLocal) {
358 cand.device_local_bytes = std::max(cand.device_local_bytes,
359 static_cast<uint64_t
>(memory_props.memoryHeaps[
h].size));
365 if (cand.has_mesh_shader) {
366 vk::PhysicalDeviceMeshShaderFeaturesEXT mesh_features;
367 vk::PhysicalDeviceFeatures2 features;
368 features.pNext = &mesh_features;
369 device.getFeatures2(&features);
371 cand.has_mesh_shader = mesh_features.meshShader == VK_TRUE
372 && mesh_features.taskShader == VK_TRUE;
376 auto family_props =
device.getQueueFamilyProperties();
379 for (uint32_t f = 0; f < probe_count; ++f) {
380 if (probe.supports(
device, f))
381 cand.present_family_mask |= (1U << f);
384 cand.families.present_family_mask = cand.present_family_mask;
386 if (cand.families.graphics_family.has_value())
387 cand.graphics_presents = cand.families.can_present(cand.families.graphics_family.value());
390 if (!cand.families.graphics_family.has_value()) {
391 cand.reject_reason =
"no graphics queue family";
392 }
else if (cand.api_version < VK_API_VERSION_1_3) {
393 cand.reject_reason =
"device API version below 1.3";
394 }
else if (!cand.has_swapchain) {
395 cand.reject_reason =
"no VK_KHR_swapchain";
397 cand.reject_reason =
"graphics family cannot present";
400 cand.score = type_base_score(cand.type);
404 if (cand.type == vk::PhysicalDeviceType::eDiscreteGpu)
408 if (cand.type == vk::PhysicalDeviceType::eIntegratedGpu)
412 if (cand.type == vk::PhysicalDeviceType::eVirtualGpu)
416 if (cand.type == vk::PhysicalDeviceType::eDiscreteGpu) {
418 if (cand.has_pci_info)
419 cand.score +=
static_cast<int64_t
>(cand.pci_bus) * 4;
427 if (cand.graphics_presents)
429 if (cand.has_mesh_shader)
432 cand.score +=
static_cast<int64_t
>(cand.device_local_bytes >> 30U);
434 candidates.push_back(std::move(cand));
437 for (
const auto& cand : candidates) {
439 "GPU [{}] {} | type={} driver={} ({}) api={}.{}.{} | uuid={} | "
440 "gfx={} compute={} transfer={} | present=gfx:{} mask:{:#x} mesh={} vram={}MB pci_bus={} | score={}{}{}",
441 cand.index, cand.name, vk::to_string(cand.type),
442 vk::to_string(cand.driver_id), cand.driver_name,
443 VK_API_VERSION_MAJOR(cand.api_version),
444 VK_API_VERSION_MINOR(cand.api_version),
445 VK_API_VERSION_PATCH(cand.api_version),
447 cand.families.graphics_family.has_value() ? std::to_string(cand.families.graphics_family.value()) :
"none",
448 cand.families.compute_family.has_value() ? std::to_string(cand.families.compute_family.value()) :
"none",
449 cand.families.transfer_family.has_value() ? std::to_string(cand.families.transfer_family.value()) :
"none",
450 cand.graphics_presents ?
"yes" :
"no",
451 cand.present_family_mask,
452 cand.has_mesh_shader ?
"yes" :
"no",
453 cand.device_local_bytes >> 20U,
454 cand.has_pci_info ? std::to_string(cand.pci_bus) :
"n/a",
456 cand.viable() ?
"" :
" | REJECTED: ",
457 cand.reject_reason ? cand.reject_reason :
"");
461 "Presentation probe mechanism: {}, require_presentation={}, device_preference={}",
469 if (int32_t env_index = -1; read_gpu_env(env_index, want_name)) {
470 want_index = env_index;
473 "MAYAFLUX_GPU override active (index={}, name='{}')", want_index, want_name);
476 const DeviceCandidate* selected =
nullptr;
477 const char* selection_basis =
"score";
479 if (want_index >= 0) {
480 for (
const auto& cand : candidates) {
481 if (
static_cast<int32_t
>(cand.index) == want_index && cand.viable()) {
483 selection_basis =
"device_index";
489 if (!selected && !want_uuid.empty()) {
490 for (
const auto& cand : candidates) {
491 if (cand.uuid_hex == want_uuid && cand.viable()) {
493 selection_basis =
"device_uuid";
499 if (!selected && !want_name.empty()) {
500 for (
const auto& cand : candidates) {
501 if (!cand.viable() || !contains_nocase(cand.name, want_name))
503 if (!selected || cand.score > selected->score) {
505 selection_basis =
"device_name";
510 const bool selector_requested = want_index >= 0 || !want_uuid.empty() || !want_name.empty();
512 if (selector_requested && !selected) {
515 std::source_location::current(),
516 "No viable physical device matched the requested selector "
517 "(index={}, uuid='{}', name='{}') and strict_device_selection is set",
518 want_index, want_uuid, want_name);
522 "No viable physical device matched the requested selector "
523 "(index={}, uuid='{}', name='{}'); falling back to score",
524 want_index, want_uuid, want_name);
528 for (
const auto& cand : candidates) {
531 if (!selected || cand.score > selected->score)
538 std::source_location::current(),
539 "No suitable GPU found among {} enumerated device(s); "
540 "see the candidate list above for rejection reasons",
552 "Selected GPU [{}] {} by {} (uuid={}, score={})",
553 selected->index, selected->name, selection_basis, selected->uuid_hex, selected->score);
629 std::source_location::current(),
630 "No graphics queue family found!");
633 std::set<uint32_t> unique_queue_families;
646 unique_queue_families.insert(f);
649 std::vector<vk::DeviceQueueCreateInfo> queue_create_infos;
650 float queue_priority = 1.0F;
652 for (uint32_t queue_family : unique_queue_families) {
653 vk::DeviceQueueCreateInfo queue_create_info {};
654 queue_create_info.queueFamilyIndex = queue_family;
655 queue_create_info.queueCount = 1;
656 queue_create_info.pQueuePriorities = &queue_priority;
657 queue_create_infos.push_back(queue_create_info);
660 vk::PhysicalDeviceFeatures device_features {};
667 std::vector<const char*> device_extensions = { VK_KHR_SWAPCHAIN_EXTENSION_NAME };
669 auto supported_extensions =
m_physical_device.enumerateDeviceExtensionProperties();
671 auto is_supported = [&supported_extensions](std::string_view
name) {
672 return std::ranges::any_of(supported_extensions, [
name](
const auto& ext) {
673 return name == ext.extensionName.data();
677#ifdef MAYAFLUX_PLATFORM_MACOS
678 if (is_supported(
"VK_KHR_portability_subset")) {
679 device_extensions.push_back(
"VK_KHR_portability_subset");
682 auto feature_chain = vk::StructureChain {
683 vk::PhysicalDeviceFeatures2 {},
684 vk::PhysicalDeviceVulkan13Features {},
685 vk::PhysicalDeviceVulkan12Features {}
690 device_extensions.push_back(VK_EXT_MESH_SHADER_EXTENSION_NAME);
693 auto feature_chain = vk::StructureChain {
694 vk::PhysicalDeviceFeatures2 {},
695 vk::PhysicalDeviceVulkan13Features {},
696 vk::PhysicalDeviceVulkan12Features {},
697 vk::PhysicalDeviceMeshShaderFeaturesEXT {}
701 feature_chain.unlink<vk::PhysicalDeviceMeshShaderFeaturesEXT>();
703 feature_chain.get<vk::PhysicalDeviceMeshShaderFeaturesEXT>().taskShader = VK_TRUE;
704 feature_chain.get<vk::PhysicalDeviceMeshShaderFeaturesEXT>().meshShader = VK_TRUE;
708 feature_chain.get<vk::PhysicalDeviceFeatures2>().features = device_features;
709 feature_chain.get<vk::PhysicalDeviceVulkan13Features>().dynamicRendering = VK_TRUE;
710 feature_chain.get<vk::PhysicalDeviceVulkan13Features>().synchronization2 = VK_TRUE;
711 feature_chain.get<vk::PhysicalDeviceVulkan12Features>().bufferDeviceAddress = VK_TRUE;
713 std::vector<std::string> missing_required;
716 if (is_supported(ext)) {
717 device_extensions.push_back(ext.c_str());
719 missing_required.push_back(ext);
723 if (!missing_required.empty()) {
725 for (
const auto& ext : missing_required) {
732 std::source_location::current(),
733 "Device '{}' does not support required extension(s): {}",
738 if (is_supported(ext)) {
739 device_extensions.push_back(ext.c_str());
742 "Device '{}' does not support optional extension '{}'; skipping",
747 vk::DeviceCreateInfo create_info {};
748 create_info.queueCreateInfoCount =
static_cast<uint32_t
>(queue_create_infos.size());
749 create_info.pQueueCreateInfos = queue_create_infos.data();
750 create_info.pNext = &feature_chain.get<vk::PhysicalDeviceFeatures2>();
751 create_info.enabledExtensionCount =
static_cast<uint32_t
>(device_extensions.size());
752 create_info.ppEnabledExtensionNames = device_extensions.data();
758 }
catch (
const std::exception& e) {
760 std::source_location::current(),
761 "Failed to create logical device: {}", e.what());