Orange/src/Orange/Render/PhysicalDevice.cpp

66 lines
2.2 KiB
C++

#include "Orange/Core/Log.h"
#include "Orange/Core/Result.h"
#include <Orange/Render/Renderer.h>
namespace orange
{
static uint32_t PickQueueFamilyIndex(VkPhysicalDevice physDev)
{
uint32_t queueFamilyPropertyCount = {};
vkGetPhysicalDeviceQueueFamilyProperties(physDev, &queueFamilyPropertyCount, nullptr);
SmallVector<VkQueueFamilyProperties, 32> queueFamilyProperties{ queueFamilyPropertyCount };
vkGetPhysicalDeviceQueueFamilyProperties(physDev, &queueFamilyPropertyCount, queueFamilyProperties.Data());
uint32_t index = ~0u;
for (uint32_t i = 0; i < queueFamilyPropertyCount; i++)
{
const auto& family = queueFamilyProperties[i];
if (family.queueFlags & VK_QUEUE_GRAPHICS_BIT)
{
index = i;
break;
}
}
return index;
}
Result<PhysicalDevice> PhysicalDevice::Create(VkPhysicalDevice physicalDevice)
{
uint32_t graphicsQueueFamilyIndex = ~0u;
if ((graphicsQueueFamilyIndex = PickQueueFamilyIndex(physicalDevice)) == ~0u)
{
log::info("Ignoring physical device as it has no graphics queue family");
return Result<PhysicalDevice>::Error(BasicErrorCode::Failed);
}
// TODO: Check if it can present to surface.
return Result<PhysicalDevice>::Success(physicalDevice, graphicsQueueFamilyIndex);
}
int PhysicalDevice::Score()
{
VkPhysicalDeviceProperties2 props = { VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PROPERTIES_2 };
vkGetPhysicalDeviceProperties2(m_physicalDevice, &props);
int score = 0;
if (props.properties.deviceType == VK_PHYSICAL_DEVICE_TYPE_DISCRETE_GPU ||
props.properties.deviceType == VK_PHYSICAL_DEVICE_TYPE_VIRTUAL_GPU)
score += 10;
if (props.properties.deviceType == VK_PHYSICAL_DEVICE_TYPE_INTEGRATED_GPU)
score += 5;
return score;
}
PhysicalDevice::PhysicalDevice(VkPhysicalDevice physicalDevice, uint32_t graphicsQueueFamilyIndex)
: m_physicalDevice { physicalDevice }
, m_graphicsQueueFamilyIndex{ graphicsQueueFamilyIndex }
{
}
}