Make Vk::Device store a reference to Vk::PhysicalDevice info

This commit is contained in:
Lynix 2020-03-08 18:10:12 +01:00
parent 0e27c2315f
commit 28cf4ed6e3
4 changed files with 17 additions and 17 deletions

View File

@ -10,6 +10,7 @@
#include <Nazara/Prerequisites.hpp> #include <Nazara/Prerequisites.hpp>
#include <Nazara/VulkanRenderer/Config.hpp> #include <Nazara/VulkanRenderer/Config.hpp>
#include <Nazara/VulkanRenderer/Wrapper/Loader.hpp> #include <Nazara/VulkanRenderer/Wrapper/Loader.hpp>
#include <Nazara/VulkanRenderer/Wrapper/PhysicalDevice.hpp>
#include <vulkan/vulkan.h> #include <vulkan/vulkan.h>
#include <memory> #include <memory>
#include <unordered_set> #include <unordered_set>
@ -36,7 +37,7 @@ namespace Nz
Device(Device&&) = delete; Device(Device&&) = delete;
inline ~Device(); inline ~Device();
bool Create(VkPhysicalDevice device, const VkDeviceCreateInfo& createInfo, const VkAllocationCallbacks* allocator = nullptr); bool Create(const Vk::PhysicalDevice& deviceInfo, const VkDeviceCreateInfo& createInfo, const VkAllocationCallbacks* allocator = nullptr);
inline void Destroy(); inline void Destroy();
inline const std::vector<QueueFamilyInfo>& GetEnabledQueues() const; inline const std::vector<QueueFamilyInfo>& GetEnabledQueues() const;
@ -47,6 +48,7 @@ namespace Nz
inline const Instance& GetInstance() const; inline const Instance& GetInstance() const;
inline VkResult GetLastErrorCode() const; inline VkResult GetLastErrorCode() const;
inline VkPhysicalDevice GetPhysicalDevice() const; inline VkPhysicalDevice GetPhysicalDevice() const;
inline const Vk::PhysicalDevice& GetPhysicalDeviceInfo() const;
inline bool IsExtensionLoaded(const std::string& extensionName); inline bool IsExtensionLoaded(const std::string& extensionName);
inline bool IsLayerLoaded(const std::string& layerName); inline bool IsLayerLoaded(const std::string& layerName);

View File

@ -14,8 +14,8 @@ namespace Nz
{ {
inline Device::Device(Instance& instance) : inline Device::Device(Instance& instance) :
m_instance(instance), m_instance(instance),
m_device(VK_NULL_HANDLE), m_physicalDevice(nullptr),
m_physicalDevice(VK_NULL_HANDLE) m_device(VK_NULL_HANDLE)
{ {
} }
@ -32,7 +32,7 @@ namespace Nz
vkDestroyDevice(m_device, (m_allocator.pfnAllocation) ? &m_allocator : nullptr); vkDestroyDevice(m_device, (m_allocator.pfnAllocation) ? &m_allocator : nullptr);
m_device = VK_NULL_HANDLE; m_device = VK_NULL_HANDLE;
m_physicalDevice = VK_NULL_HANDLE; m_physicalDevice = nullptr;
} }
} }
@ -65,7 +65,12 @@ namespace Nz
inline VkPhysicalDevice Device::GetPhysicalDevice() const inline VkPhysicalDevice Device::GetPhysicalDevice() const
{ {
return m_physicalDevice; return m_physicalDevice->device;
}
inline const Vk::PhysicalDevice& Device::GetPhysicalDeviceInfo() const
{
return *m_physicalDevice;
} }
inline bool Device::IsExtensionLoaded(const std::string& extensionName) inline bool Device::IsExtensionLoaded(const std::string& extensionName)

View File

@ -429,7 +429,7 @@ namespace Nz
}; };
std::shared_ptr<VulkanDevice> device = std::make_shared<VulkanDevice>(s_instance); std::shared_ptr<VulkanDevice> device = std::make_shared<VulkanDevice>(s_instance);
if (!device->Create(gpu, createInfo)) if (!device->Create(GetPhysicalDeviceInfo(gpu), createInfo))
{ {
NazaraError("Failed to create Vulkan Device: " + TranslateVulkanError(device->GetLastErrorCode())); NazaraError("Failed to create Vulkan Device: " + TranslateVulkanError(device->GetLastErrorCode()));
return {}; return {};

View File

@ -12,23 +12,16 @@ namespace Nz
{ {
namespace Vk namespace Vk
{ {
bool Device::Create(VkPhysicalDevice device, const VkDeviceCreateInfo& createInfo, const VkAllocationCallbacks* allocator) bool Device::Create(const Vk::PhysicalDevice& deviceInfo, const VkDeviceCreateInfo& createInfo, const VkAllocationCallbacks* allocator)
{ {
std::vector<VkQueueFamilyProperties> queuesProperties; m_lastErrorCode = m_instance.vkCreateDevice(deviceInfo.device, &createInfo, allocator, &m_device);
if (!m_instance.GetPhysicalDeviceQueueFamilyProperties(device, &queuesProperties))
{
NazaraError("Failed to query queue family properties");
return false;
}
m_lastErrorCode = m_instance.vkCreateDevice(device, &createInfo, allocator, &m_device);
if (m_lastErrorCode != VkResult::VK_SUCCESS) if (m_lastErrorCode != VkResult::VK_SUCCESS)
{ {
NazaraError("Failed to create Vulkan device"); NazaraError("Failed to create Vulkan device");
return false; return false;
} }
m_physicalDevice = device; m_physicalDevice = &deviceInfo;
// Store the allocator to access them when needed // Store the allocator to access them when needed
if (allocator) if (allocator)
@ -76,7 +69,7 @@ namespace Nz
if (info.familyIndex > maxFamilyIndex) if (info.familyIndex > maxFamilyIndex)
maxFamilyIndex = info.familyIndex; maxFamilyIndex = info.familyIndex;
const VkQueueFamilyProperties& queueProperties = queuesProperties[info.familyIndex]; const VkQueueFamilyProperties& queueProperties = deviceInfo.queues[info.familyIndex];
info.flags = queueProperties.queueFlags; info.flags = queueProperties.queueFlags;
info.minImageTransferGranularity = queueProperties.minImageTransferGranularity; info.minImageTransferGranularity = queueProperties.minImageTransferGranularity;
info.timestampValidBits = queueProperties.timestampValidBits; info.timestampValidBits = queueProperties.timestampValidBits;