renderer_vulkan/wrapper: Fix physical device sorting
The old code had a sort function that was invalid and it didn't work as expected when the base vector had a different order (e.g. renderdoc was attached). This sorts devices as expected and fixes a debug assert on MSVC.
This commit is contained in:
parent
cc0dc3280d
commit
cd3e959f23
|
@ -6,6 +6,7 @@
|
||||||
#include <exception>
|
#include <exception>
|
||||||
#include <memory>
|
#include <memory>
|
||||||
#include <optional>
|
#include <optional>
|
||||||
|
#include <string_view>
|
||||||
#include <utility>
|
#include <utility>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
|
@ -17,21 +18,42 @@ namespace Vulkan::vk {
|
||||||
|
|
||||||
namespace {
|
namespace {
|
||||||
|
|
||||||
void SortPhysicalDevices(std::vector<VkPhysicalDevice>& devices, const InstanceDispatch& dld) {
|
template <typename Func>
|
||||||
std::stable_sort(devices.begin(), devices.end(), [&](auto lhs, auto rhs) {
|
void SortPhysicalDevices(std::vector<VkPhysicalDevice>& devices, const InstanceDispatch& dld,
|
||||||
// This will call Vulkan more than needed, but these calls are cheap.
|
Func&& func) {
|
||||||
const auto lhs_properties = vk::PhysicalDevice(lhs, dld).GetProperties();
|
// Calling GetProperties calls Vulkan more than needed. But they are supposed to be cheap
|
||||||
const auto rhs_properties = vk::PhysicalDevice(rhs, dld).GetProperties();
|
// functions.
|
||||||
|
std::stable_sort(devices.begin(), devices.end(),
|
||||||
|
[&dld, &func](VkPhysicalDevice lhs, VkPhysicalDevice rhs) {
|
||||||
|
return func(vk::PhysicalDevice(lhs, dld).GetProperties(),
|
||||||
|
vk::PhysicalDevice(rhs, dld).GetProperties());
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
// Prefer discrete GPUs, Nvidia over AMD, AMD over Intel, Intel over the rest.
|
void SortPhysicalDevicesPerVendor(std::vector<VkPhysicalDevice>& devices,
|
||||||
const bool preferred =
|
const InstanceDispatch& dld,
|
||||||
(lhs_properties.deviceType == VK_PHYSICAL_DEVICE_TYPE_DISCRETE_GPU &&
|
std::initializer_list<u32> vendor_ids) {
|
||||||
rhs_properties.deviceType != VK_PHYSICAL_DEVICE_TYPE_DISCRETE_GPU) ||
|
for (auto it = vendor_ids.end(); it != vendor_ids.begin();) {
|
||||||
(lhs_properties.vendorID == 0x10DE && rhs_properties.vendorID != 0x10DE) ||
|
--it;
|
||||||
(lhs_properties.vendorID == 0x1002 && rhs_properties.vendorID != 0x1002) ||
|
SortPhysicalDevices(devices, dld, [id = *it](const auto& lhs, const auto& rhs) {
|
||||||
(lhs_properties.vendorID == 0x8086 && rhs_properties.vendorID != 0x8086);
|
return lhs.vendorID == id && rhs.vendorID != id;
|
||||||
return !preferred;
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void SortPhysicalDevices(std::vector<VkPhysicalDevice>& devices, const InstanceDispatch& dld) {
|
||||||
|
// Sort by name, this will set a base and make GPUs with higher numbers appear first
|
||||||
|
// (e.g. GTX 1650 will intentionally be listed before a GTX 1080).
|
||||||
|
SortPhysicalDevices(devices, dld, [](const auto& lhs, const auto& rhs) {
|
||||||
|
return std::string_view{lhs.deviceName} > std::string_view{rhs.deviceName};
|
||||||
});
|
});
|
||||||
|
// Prefer discrete over non-discrete
|
||||||
|
SortPhysicalDevices(devices, dld, [](const auto& lhs, const auto& rhs) {
|
||||||
|
return lhs.deviceType == VK_PHYSICAL_DEVICE_TYPE_DISCRETE_GPU &&
|
||||||
|
rhs.deviceType != VK_PHYSICAL_DEVICE_TYPE_DISCRETE_GPU;
|
||||||
|
});
|
||||||
|
// Prefer Nvidia over AMD, AMD over Intel, Intel over the rest.
|
||||||
|
SortPhysicalDevicesPerVendor(devices, dld, {0x10DE, 0x1002, 0x8086});
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename T>
|
template <typename T>
|
||||||
|
|
Loading…
Reference in a new issue