112 lines
2.2 KiB
C++
112 lines
2.2 KiB
C++
#include "VulkanDeviceManager.h"
|
|
|
|
#include <vector>
|
|
|
|
#include "Logger.h"
|
|
#include <map>
|
|
|
|
VulkanDeviceManager::VulkanDeviceManager()
|
|
{
|
|
}
|
|
|
|
VulkanDeviceManager::~VulkanDeviceManager()
|
|
{
|
|
Cleanup();
|
|
}
|
|
|
|
VulkanDeviceManager::VulkanDeviceManager(VulkanDeviceManager&& Other) noexcept
|
|
{
|
|
}
|
|
|
|
VulkanDeviceManager& VulkanDeviceManager::operator=(VulkanDeviceManager&& Other) noexcept
|
|
{
|
|
if (this != &Other)
|
|
{
|
|
Cleanup();
|
|
}
|
|
|
|
return *this;
|
|
}
|
|
void VulkanDeviceManager::Initialize(VkInstance Instance)
|
|
{
|
|
if (IsInitialized())
|
|
{
|
|
return;
|
|
}
|
|
}
|
|
|
|
void VulkanDeviceManager::Cleanup()
|
|
{
|
|
if (!IsInitialized())
|
|
{
|
|
return;
|
|
}
|
|
}
|
|
|
|
void VulkanDeviceManager::PickPhysicalDevice()
|
|
{
|
|
uint32_t DeviceCount = 0;
|
|
vkEnumeratePhysicalDevices(Instance, &DeviceCount, nullptr);
|
|
|
|
if (DeviceCount == 0)
|
|
{
|
|
Log::Error("Failed to find GPU with Vulkan Support");
|
|
}
|
|
|
|
std::vector<VkPhysicalDevice> Devices(DeviceCount);
|
|
vkEnumeratePhysicalDevices(Instance, &DeviceCount, Devices.data());
|
|
|
|
std::multimap<int, VkPhysicalDevice> Candidates;
|
|
|
|
for (const auto& Device : Devices)
|
|
{
|
|
int Score = RateDeviceSuitability(Device);
|
|
Candidates.insert(std::make_pair(Score, Device));
|
|
}
|
|
|
|
if (Candidates.rbegin()->first > 0)
|
|
{
|
|
PhysicalDevice = Candidates.rbegin()->second;
|
|
}
|
|
else
|
|
{
|
|
Log::Error("Failed to find a suitable GPU");
|
|
}
|
|
}
|
|
|
|
// bool VulkanDeviceManager::IsDeviceSuitable(VkPhysicalDevice Device)
|
|
//{
|
|
// VkPhysicalDeviceProperties DeviceProperties;
|
|
// vkGetPhysicalDeviceProperties(Device, &DeviceProperties);
|
|
//
|
|
// VkPhysicalDeviceFeatures DeviceFeatures;
|
|
// vkGetPhysicalDeviceFeatures(Device, &DeviceFeatures);
|
|
//
|
|
// return DeviceProperties.deviceType == VK_PHYSICAL_DEVICE_TYPE_DISCRETE_GPU && DeviceFeatures.geometryShader;
|
|
// }
|
|
|
|
int VulkanDeviceManager::RateDeviceSuitability(VkPhysicalDevice Device)
|
|
{
|
|
VkPhysicalDeviceProperties DeviceProperties;
|
|
vkGetPhysicalDeviceProperties(Device, &DeviceProperties);
|
|
|
|
VkPhysicalDeviceFeatures DeviceFeatures;
|
|
vkGetPhysicalDeviceFeatures(Device, &DeviceFeatures);
|
|
|
|
int Score = 0;
|
|
|
|
if (DeviceProperties.deviceType == VK_PHYSICAL_DEVICE_TYPE_DISCRETE_GPU)
|
|
{
|
|
Score += 100;
|
|
}
|
|
|
|
Score += DeviceProperties.limits.maxImageDimension2D;
|
|
|
|
if (!DeviceFeatures.geometryShader)
|
|
{
|
|
return 0;
|
|
}
|
|
|
|
return Score;
|
|
}
|