Creating VmaAllocator object for each Vulkan device.

This commit is contained in:
2022-01-15 03:34:41 -08:00
parent 2df55cf31c
commit 739111ed2d
3 changed files with 50 additions and 17 deletions

View File

@@ -30,9 +30,11 @@
// //
#include "KRDevice.h" #include "KRDevice.h"
#include "KRDeviceManager.h"
KRDevice::KRDevice(const VkPhysicalDevice& device) KRDevice::KRDevice(KRContext& context, const VkPhysicalDevice& device)
: m_device(device) : KRContextObject(context)
, m_device(device)
, m_logicalDevice(VK_NULL_HANDLE) , m_logicalDevice(VK_NULL_HANDLE)
, m_deviceProperties {} , m_deviceProperties {}
, m_deviceFeatures{} , m_deviceFeatures{}
@@ -42,6 +44,7 @@ KRDevice::KRDevice(const VkPhysicalDevice& device)
, m_computeQueue(VK_NULL_HANDLE) , m_computeQueue(VK_NULL_HANDLE)
, m_graphicsCommandPool(VK_NULL_HANDLE) , m_graphicsCommandPool(VK_NULL_HANDLE)
, m_computeCommandPool(VK_NULL_HANDLE) , m_computeCommandPool(VK_NULL_HANDLE)
, m_allocator(VK_NULL_HANDLE)
{ {
} }
@@ -67,6 +70,11 @@ void KRDevice::destroy()
vkDestroyDevice(m_logicalDevice, nullptr); vkDestroyDevice(m_logicalDevice, nullptr);
m_logicalDevice = VK_NULL_HANDLE; m_logicalDevice = VK_NULL_HANDLE;
} }
if (m_allocator != VK_NULL_HANDLE) {
vmaDestroyAllocator(m_allocator);
m_allocator = VK_NULL_HANDLE;
}
} }
bool KRDevice::initialize(const std::vector<const char*>& deviceExtensions) bool KRDevice::initialize(const std::vector<const char*>& deviceExtensions)
@@ -157,15 +165,14 @@ bool KRDevice::initialize(const std::vector<const char*>& deviceExtensions)
poolInfo.flags = 0; poolInfo.flags = 0;
if (vkCreateCommandPool(m_logicalDevice, &poolInfo, nullptr, &m_graphicsCommandPool) != VK_SUCCESS) { if (vkCreateCommandPool(m_logicalDevice, &poolInfo, nullptr, &m_graphicsCommandPool) != VK_SUCCESS) {
vkDestroyDevice(m_logicalDevice, nullptr); destroy();
// TODO - Log a warning... // TODO - Log a warning...
return false; return false;
} }
poolInfo.queueFamilyIndex = m_computeFamilyQueueIndex; poolInfo.queueFamilyIndex = m_computeFamilyQueueIndex;
if (vkCreateCommandPool(m_logicalDevice, &poolInfo, nullptr, &m_computeCommandPool) != VK_SUCCESS) { if (vkCreateCommandPool(m_logicalDevice, &poolInfo, nullptr, &m_computeCommandPool) != VK_SUCCESS) {
vkDestroyCommandPool(m_logicalDevice, m_graphicsCommandPool, nullptr); destroy();
vkDestroyDevice(m_logicalDevice, nullptr);
// TODO - Log a warning... // TODO - Log a warning...
return false; return false;
} }
@@ -183,9 +190,7 @@ bool KRDevice::initialize(const std::vector<const char*>& deviceExtensions)
allocInfo.commandBufferCount = (uint32_t)m_graphicsCommandBuffers.size(); allocInfo.commandBufferCount = (uint32_t)m_graphicsCommandBuffers.size();
if (vkAllocateCommandBuffers(m_logicalDevice, &allocInfo, m_graphicsCommandBuffers.data()) != VK_SUCCESS) { if (vkAllocateCommandBuffers(m_logicalDevice, &allocInfo, m_graphicsCommandBuffers.data()) != VK_SUCCESS) {
vkDestroyCommandPool(m_logicalDevice, m_computeCommandPool, nullptr); destroy();
vkDestroyCommandPool(m_logicalDevice, m_graphicsCommandPool, nullptr);
vkDestroyDevice(m_logicalDevice, nullptr);
// TODO - Log a warning // TODO - Log a warning
return false; return false;
} }
@@ -193,13 +198,33 @@ bool KRDevice::initialize(const std::vector<const char*>& deviceExtensions)
allocInfo.commandPool = m_computeCommandPool; allocInfo.commandPool = m_computeCommandPool;
allocInfo.commandBufferCount = (uint32_t)m_computeCommandBuffers.size(); allocInfo.commandBufferCount = (uint32_t)m_computeCommandBuffers.size();
if (vkAllocateCommandBuffers(m_logicalDevice, &allocInfo, m_computeCommandBuffers.data()) != VK_SUCCESS) { if (vkAllocateCommandBuffers(m_logicalDevice, &allocInfo, m_computeCommandBuffers.data()) != VK_SUCCESS) {
// Note - this repeated cleanup will likely be eliminated with later refactoring to split vulkan destroy();
// object generation out of KRContext
vkDestroyCommandPool(m_logicalDevice, m_computeCommandPool, nullptr);
vkDestroyCommandPool(m_logicalDevice, m_graphicsCommandPool, nullptr);
vkDestroyDevice(m_logicalDevice, nullptr);
// TODO - Log a warning // TODO - Log a warning
return false; return false;
} }
// Create Vulkan Memory Allocator instance for this device
// We are dynamically linking Vulkan, so we need to give VMA some hints
// on finding the function pointers
VmaVulkanFunctions vmaVulkanFunctions{};
vmaVulkanFunctions.vkGetInstanceProcAddr = vkGetInstanceProcAddr;
vmaVulkanFunctions.vkGetDeviceProcAddr = vkGetDeviceProcAddr;
VmaAllocatorCreateInfo vmaCreateInfo{};
vmaCreateInfo.flags = VMA_ALLOCATOR_CREATE_EXTERNALLY_SYNCHRONIZED_BIT;
// TODO - Hook vmaCreateInfo.pAllocationCallbacks;
vmaCreateInfo.physicalDevice = m_device;
vmaCreateInfo.device = m_logicalDevice;
vmaCreateInfo.instance = m_pContext->getDeviceManager()->getVulkanInstance();
vmaCreateInfo.vulkanApiVersion = VK_API_VERSION_1_2;
vmaCreateInfo.pVulkanFunctions = &vmaVulkanFunctions;
if (vmaCreateAllocator(&vmaCreateInfo, &m_allocator) != VK_SUCCESS) {
destroy();
// TODO - Log a warning
return false;
}
return true; return true;
} }

View File

@@ -30,15 +30,16 @@
// //
#include "KREngine-common.h" #include "KREngine-common.h"
#include "KRContextObject.h"
#ifndef KRDEVICE_H #ifndef KRDEVICE_H
#define KRDEVICE_H #define KRDEVICE_H
class KRDevice class KRDevice : public KRContextObject
{ {
public: public:
KRDevice(const VkPhysicalDevice& device); KRDevice(KRContext& context, const VkPhysicalDevice& device);
~KRDevice(); virtual ~KRDevice();
KRDevice(const KRDevice&) = delete; KRDevice(const KRDevice&) = delete;
KRDevice& operator=(const KRDevice&) = delete; KRDevice& operator=(const KRDevice&) = delete;
@@ -58,6 +59,7 @@ public:
VkCommandPool m_computeCommandPool; VkCommandPool m_computeCommandPool;
std::vector<VkCommandBuffer> m_graphicsCommandBuffers; std::vector<VkCommandBuffer> m_graphicsCommandBuffers;
std::vector<VkCommandBuffer> m_computeCommandBuffers; std::vector<VkCommandBuffer> m_computeCommandBuffers;
VmaAllocator m_allocator;
private: private:
}; };

View File

@@ -31,6 +31,12 @@
#include "KRDeviceManager.h" #include "KRDeviceManager.h"
// VMA_IMPLEMENTATION must only be defined in a single CPP file
#define VMA_IMPLEMENTATION
#define VMA_STATIC_VULKAN_FUNCTIONS 0
#define VMA_DYNAMIC_VULKAN_FUNCTIONS 1
#include "vk_mem_alloc.h"
KRDeviceManager::KRDeviceManager(KRContext& context) KRDeviceManager::KRDeviceManager(KRContext& context)
: KRContextObject(context) : KRContextObject(context)
, m_vulkanInstance(VK_NULL_HANDLE) , m_vulkanInstance(VK_NULL_HANDLE)
@@ -142,7 +148,7 @@ void KRDeviceManager::createDevices()
std::vector<std::unique_ptr<KRDevice>> candidateDevices; std::vector<std::unique_ptr<KRDevice>> candidateDevices;
for (const VkPhysicalDevice& physicalDevice : physicalDevices) { for (const VkPhysicalDevice& physicalDevice : physicalDevices) {
std::unique_ptr<KRDevice> device = std::make_unique<KRDevice>(physicalDevice); std::unique_ptr<KRDevice> device = std::make_unique<KRDevice>(*m_pContext, physicalDevice);
if (!device->initialize(deviceExtensions)) { if (!device->initialize(deviceExtensions)) {
continue; continue;
} }