Add and make use of Vulkan Memory Allocator
This commit is contained in:
@@ -1,44 +0,0 @@
|
||||
// Copyright (C) 2020 Jérôme Leclercq
|
||||
// This file is part of the "Nazara Engine - Renderer module"
|
||||
// For conditions of distribution and use, see copyright notice in Config.hpp
|
||||
|
||||
#pragma once
|
||||
|
||||
#ifndef NAZARA_HARDWAREBUFFER_HPP
|
||||
#define NAZARA_HARDWAREBUFFER_HPP
|
||||
|
||||
#include <Nazara/Prerequisites.hpp>
|
||||
#include <Nazara/Renderer/OpenGL.hpp>
|
||||
#include <Nazara/Utility/AbstractBuffer.hpp>
|
||||
|
||||
namespace Nz
|
||||
{
|
||||
class Buffer;
|
||||
|
||||
class HardwareBuffer : public AbstractBuffer
|
||||
{
|
||||
public:
|
||||
HardwareBuffer(Buffer* parent, BufferType type);
|
||||
~HardwareBuffer();
|
||||
|
||||
bool Fill(const void* data, UInt32 offset, UInt32 size) override;
|
||||
|
||||
bool Initialize(unsigned int size, BufferUsageFlags usage) override;
|
||||
|
||||
DataStorage GetStorage() const override;
|
||||
|
||||
void* Map(BufferAccess access, UInt32 offset = 0, UInt32 size = 0) override;
|
||||
bool Unmap() override;
|
||||
|
||||
// Fonctions OpenGL
|
||||
void Bind() const;
|
||||
GLuint GetOpenGLID() const;
|
||||
|
||||
private:
|
||||
GLuint m_buffer;
|
||||
BufferType m_type;
|
||||
Buffer* m_parent;
|
||||
};
|
||||
}
|
||||
|
||||
#endif // NAZARA_HARDWAREBUFFER_HPP
|
||||
@@ -31,11 +31,10 @@ namespace Nz
|
||||
|
||||
AbstractBuffer* RenderBuffer::GetHardwareBuffer(RenderDevice* device)
|
||||
{
|
||||
auto it = m_hardwareBuffers.find(device);
|
||||
if (it == m_hardwareBuffers.end())
|
||||
return nullptr;
|
||||
if (HardwareBuffer* hwBuffer = GetHardwareBufferData(device))
|
||||
return hwBuffer->buffer.get();
|
||||
|
||||
return it->second.buffer.get();
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
DataStorage RenderBuffer::GetStorage() const
|
||||
@@ -65,6 +64,18 @@ namespace Nz
|
||||
}
|
||||
|
||||
bool RenderBuffer::Synchronize(RenderDevice* device)
|
||||
{
|
||||
HardwareBuffer* hwBuffer = GetHardwareBufferData(device);
|
||||
if (!hwBuffer)
|
||||
return false;
|
||||
|
||||
if (hwBuffer->synchronized)
|
||||
return true;
|
||||
|
||||
return hwBuffer->buffer->Fill(m_softwareBuffer.GetData(), 0, m_size);
|
||||
}
|
||||
|
||||
auto RenderBuffer::GetHardwareBufferData(RenderDevice* device) -> HardwareBuffer*
|
||||
{
|
||||
auto it = m_hardwareBuffers.find(device);
|
||||
if (it == m_hardwareBuffers.end())
|
||||
@@ -74,16 +85,13 @@ namespace Nz
|
||||
if (!hwBuffer.buffer->Initialize(m_size, m_usage))
|
||||
{
|
||||
NazaraError("Failed to initialize hardware buffer");
|
||||
return false;
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
it = m_hardwareBuffers.emplace(device, std::move(hwBuffer)).first;
|
||||
}
|
||||
|
||||
HardwareBuffer& hwBuffer = it->second;
|
||||
if (hwBuffer.synchronized)
|
||||
return true;
|
||||
|
||||
return hwBuffer.buffer->Fill(m_softwareBuffer.GetData(), 0, m_size);
|
||||
return &it->second;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -163,6 +163,9 @@ namespace Nz
|
||||
#ifdef VK_USE_PLATFORM_WIN32_KHR
|
||||
enabledExtensions.push_back("VK_KHR_win32_surface");
|
||||
#endif
|
||||
|
||||
if (availableExtensions.count(VK_KHR_GET_PHYSICAL_DEVICE_PROPERTIES_2_EXTENSION_NAME))
|
||||
enabledExtensions.push_back(VK_KHR_GET_PHYSICAL_DEVICE_PROPERTIES_2_EXTENSION_NAME);
|
||||
}
|
||||
|
||||
std::vector<String> additionalExtensions; // Just to keep the String alive
|
||||
@@ -424,6 +427,16 @@ namespace Nz
|
||||
// Swapchain extension is required for rendering
|
||||
enabledExtensions.emplace_back(VK_KHR_SWAPCHAIN_EXTENSION_NAME);
|
||||
|
||||
auto EnableIfSupported = [&](const char* extName)
|
||||
{
|
||||
if (deviceInfo.extensions.count(extName))
|
||||
enabledExtensions.emplace_back(extName);
|
||||
};
|
||||
|
||||
// VMA extensions
|
||||
EnableIfSupported(VK_EXT_MEMORY_BUDGET_EXTENSION_NAME);
|
||||
EnableIfSupported(VK_KHR_BIND_MEMORY_2_EXTENSION_NAME);
|
||||
EnableIfSupported(VK_KHR_DEDICATED_ALLOCATION_EXTENSION_NAME);
|
||||
}
|
||||
|
||||
std::vector<String> additionalExtensions; // Just to keep the String alive
|
||||
|
||||
@@ -7,11 +7,15 @@
|
||||
#include <Nazara/Core/String.hpp>
|
||||
#include <Nazara/VulkanRenderer/Wrapper/CommandBuffer.hpp>
|
||||
#include <Nazara/VulkanRenderer/Wrapper/QueueHandle.hpp>
|
||||
#include <vma/vk_mem_alloc.h>
|
||||
#include <Nazara/VulkanRenderer/Debug.hpp>
|
||||
|
||||
namespace Nz
|
||||
{
|
||||
VulkanBuffer::~VulkanBuffer() = default;
|
||||
VulkanBuffer::~VulkanBuffer()
|
||||
{
|
||||
vmaDestroyBuffer(m_device.GetMemoryAllocator(), m_buffer, m_allocation);
|
||||
}
|
||||
|
||||
bool VulkanBuffer::Fill(const void* data, UInt32 offset, UInt32 size)
|
||||
{
|
||||
@@ -32,32 +36,33 @@ namespace Nz
|
||||
m_usage = usage;
|
||||
|
||||
VkBufferUsageFlags bufferUsage = ToVulkan(m_type);
|
||||
VkMemoryPropertyFlags memoryProperties = 0;
|
||||
if (usage & BufferUsage_DeviceLocal)
|
||||
memoryProperties |= VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT;
|
||||
|
||||
if (usage & BufferUsage_DirectMapping)
|
||||
memoryProperties |= VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT;
|
||||
else
|
||||
if ((usage & BufferUsage_DirectMapping) == 0)
|
||||
bufferUsage |= VK_BUFFER_USAGE_TRANSFER_DST_BIT;
|
||||
|
||||
if (!m_buffer.Create(m_device, 0, size, bufferUsage))
|
||||
VkBufferCreateInfo createInfo = {};
|
||||
createInfo.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
|
||||
createInfo.size = size;
|
||||
createInfo.usage = bufferUsage;
|
||||
|
||||
VmaAllocationCreateInfo allocInfo = {};
|
||||
if (usage & BufferUsage_DeviceLocal)
|
||||
{
|
||||
NazaraError("Failed to create vulkan buffer");
|
||||
return false;
|
||||
if (usage & BufferUsage_DirectMapping)
|
||||
allocInfo.usage = VMA_MEMORY_USAGE_CPU_TO_GPU;
|
||||
else
|
||||
allocInfo.usage = VMA_MEMORY_USAGE_GPU_ONLY;
|
||||
}
|
||||
else
|
||||
allocInfo.usage = VMA_MEMORY_USAGE_CPU_ONLY;
|
||||
|
||||
VkMemoryRequirements memRequirement = m_buffer.GetMemoryRequirements();
|
||||
if (usage & BufferUsage_PersistentMapping)
|
||||
allocInfo.flags |= VMA_ALLOCATION_CREATE_MAPPED_BIT;
|
||||
|
||||
if (!m_memory.Create(m_device, memRequirement.size, memRequirement.memoryTypeBits, memoryProperties))
|
||||
VkResult result = vmaCreateBuffer(m_device.GetMemoryAllocator(), &createInfo, &allocInfo, &m_buffer, &m_allocation, nullptr);
|
||||
if (result != VK_SUCCESS)
|
||||
{
|
||||
NazaraError("Failed to allocate buffer memory");
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!m_buffer.BindBufferMemory(m_memory))
|
||||
{
|
||||
NazaraError("Failed to bind vertex buffer to its memory");
|
||||
NazaraError("Failed to allocate buffer: " + TranslateVulkanError(result));
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -73,38 +78,37 @@ namespace Nz
|
||||
{
|
||||
if (m_usage & BufferUsage_DirectMapping)
|
||||
{
|
||||
if (!m_memory.Map(offset, size))
|
||||
void* mappedPtr;
|
||||
VkResult result = vmaMapMemory(m_device.GetMemoryAllocator(), m_allocation, &mappedPtr);
|
||||
if (result != VK_SUCCESS)
|
||||
{
|
||||
NazaraError("Failed to map buffer: " + TranslateVulkanError(result));
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
return m_memory.GetMappedPointer();
|
||||
return static_cast<UInt8*>(mappedPtr) + offset;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (!m_stagingBuffer.Create(m_device, 0, m_size, VK_BUFFER_USAGE_TRANSFER_SRC_BIT))
|
||||
VkBufferCreateInfo createInfo = {};
|
||||
createInfo.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
|
||||
createInfo.size = size;
|
||||
createInfo.usage = VK_BUFFER_USAGE_TRANSFER_SRC_BIT;
|
||||
|
||||
VmaAllocationCreateInfo allocInfo = {};
|
||||
allocInfo.flags = VMA_ALLOCATION_CREATE_MAPPED_BIT;
|
||||
allocInfo.usage = VMA_MEMORY_USAGE_CPU_TO_GPU;
|
||||
|
||||
VmaAllocationInfo allocationInfo;
|
||||
|
||||
VkResult result = vmaCreateBuffer(m_device.GetMemoryAllocator(), &createInfo, &allocInfo, &m_stagingBuffer, &m_stagingAllocation, &allocationInfo);
|
||||
if (result != VK_SUCCESS)
|
||||
{
|
||||
NazaraError("Failed to create staging buffer");
|
||||
NazaraError("Failed to allocate staging buffer: " + TranslateVulkanError(result));
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
VkMemoryPropertyFlags memoryProperties = VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT;
|
||||
|
||||
VkMemoryRequirements memRequirement = m_stagingBuffer.GetMemoryRequirements();
|
||||
if (!m_stagingMemory.Create(m_device, memRequirement.size, memRequirement.memoryTypeBits, memoryProperties))
|
||||
{
|
||||
NazaraError("Failed to allocate vertex buffer memory");
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
if (!m_stagingBuffer.BindBufferMemory(m_stagingMemory))
|
||||
{
|
||||
NazaraError("Failed to bind vertex buffer to its memory");
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
if (!m_stagingMemory.Map(offset, size))
|
||||
return nullptr;
|
||||
|
||||
return m_stagingMemory.GetMappedPointer();
|
||||
return allocationInfo.pMappedData;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -112,20 +116,17 @@ namespace Nz
|
||||
{
|
||||
if (m_usage & BufferUsage_DirectMapping)
|
||||
{
|
||||
m_memory.Unmap();
|
||||
vmaUnmapMemory(m_device.GetMemoryAllocator(), m_allocation);
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
m_stagingMemory.FlushMemory();
|
||||
m_stagingMemory.Unmap();
|
||||
|
||||
Vk::CommandBuffer copyCommandBuffer = m_device.AllocateTransferCommandBuffer();
|
||||
if (!copyCommandBuffer.Begin(VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT))
|
||||
Vk::AutoCommandBuffer copyCommandBuffer = m_device.AllocateTransferCommandBuffer();
|
||||
if (!copyCommandBuffer->Begin(VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT))
|
||||
return false;
|
||||
|
||||
copyCommandBuffer.CopyBuffer(m_stagingBuffer, m_buffer, m_size);
|
||||
if (!copyCommandBuffer.End())
|
||||
copyCommandBuffer->CopyBuffer(m_stagingBuffer, m_buffer, m_size);
|
||||
if (!copyCommandBuffer->End())
|
||||
return false;
|
||||
|
||||
Vk::QueueHandle transferQueue = m_device.GetQueue(m_device.GetTransferQueueFamilyIndex(), 0);
|
||||
@@ -134,8 +135,7 @@ namespace Nz
|
||||
|
||||
transferQueue.WaitIdle();
|
||||
|
||||
m_stagingBuffer.Destroy();
|
||||
m_stagingMemory.Destroy();
|
||||
vmaDestroyBuffer(m_device.GetMemoryAllocator(), m_stagingBuffer, m_stagingAllocation);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,6 +9,11 @@
|
||||
#include <Nazara/VulkanRenderer/Wrapper/CommandBuffer.hpp>
|
||||
#include <Nazara/VulkanRenderer/Wrapper/CommandPool.hpp>
|
||||
#include <Nazara/VulkanRenderer/Wrapper/QueueHandle.hpp>
|
||||
|
||||
#define VMA_IMPLEMENTATION
|
||||
#define VMA_STATIC_VULKAN_FUNCTIONS 0
|
||||
#include <vma/vk_mem_alloc.h>
|
||||
|
||||
#include <Nazara/VulkanRenderer/Debug.hpp>
|
||||
|
||||
namespace Nz
|
||||
@@ -23,7 +28,9 @@ namespace Nz
|
||||
Device::Device(Instance& instance) :
|
||||
m_instance(instance),
|
||||
m_physicalDevice(nullptr),
|
||||
m_device(VK_NULL_HANDLE)
|
||||
m_device(VK_NULL_HANDLE),
|
||||
m_lastErrorCode(VK_SUCCESS),
|
||||
m_memAllocator(VK_NULL_HANDLE)
|
||||
{
|
||||
}
|
||||
|
||||
@@ -43,7 +50,7 @@ namespace Nz
|
||||
m_lastErrorCode = m_instance.vkCreateDevice(deviceInfo.physDevice, &createInfo, allocator, &m_device);
|
||||
if (m_lastErrorCode != VkResult::VK_SUCCESS)
|
||||
{
|
||||
NazaraError("Failed to create Vulkan device");
|
||||
NazaraError("Failed to create Vulkan device: " + TranslateVulkanError(m_lastErrorCode));
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -145,6 +152,61 @@ namespace Nz
|
||||
return false;
|
||||
}
|
||||
|
||||
// Initialize VMA
|
||||
VmaVulkanFunctions vulkanFunctions = {
|
||||
m_instance.vkGetPhysicalDeviceProperties,
|
||||
m_instance.vkGetPhysicalDeviceMemoryProperties,
|
||||
vkAllocateMemory,
|
||||
vkFreeMemory,
|
||||
vkMapMemory,
|
||||
vkUnmapMemory,
|
||||
vkFlushMappedMemoryRanges,
|
||||
vkInvalidateMappedMemoryRanges,
|
||||
vkBindBufferMemory,
|
||||
vkBindImageMemory,
|
||||
vkGetBufferMemoryRequirements,
|
||||
vkGetImageMemoryRequirements,
|
||||
vkCreateBuffer,
|
||||
vkDestroyBuffer,
|
||||
vkCreateImage,
|
||||
vkDestroyImage,
|
||||
vkCmdCopyBuffer,
|
||||
#if VMA_DEDICATED_ALLOCATION || VMA_VULKAN_VERSION >= 1001000
|
||||
vkGetBufferMemoryRequirements2,
|
||||
vkGetImageMemoryRequirements2,
|
||||
#endif
|
||||
#if VMA_BIND_MEMORY2 || VMA_VULKAN_VERSION >= 1001000
|
||||
vkBindBufferMemory2,
|
||||
vkBindImageMemory2,
|
||||
#endif
|
||||
#if VMA_MEMORY_BUDGET || VMA_VULKAN_VERSION >= 1001000
|
||||
m_instance.vkGetPhysicalDeviceMemoryProperties2,
|
||||
#endif
|
||||
};
|
||||
|
||||
VmaAllocatorCreateInfo allocatorInfo = {};
|
||||
allocatorInfo.physicalDevice = deviceInfo.physDevice;
|
||||
allocatorInfo.device = m_device;
|
||||
allocatorInfo.instance = m_instance;
|
||||
allocatorInfo.vulkanApiVersion = std::min<UInt32>(VK_API_VERSION_1_1, m_instance.GetApiVersion());
|
||||
allocatorInfo.pVulkanFunctions = &vulkanFunctions;
|
||||
|
||||
if (vkGetBufferMemoryRequirements2 && vkGetImageMemoryRequirements2)
|
||||
allocatorInfo.flags |= VMA_ALLOCATOR_CREATE_KHR_DEDICATED_ALLOCATION_BIT;
|
||||
|
||||
if (vkBindBufferMemory2 && vkBindImageMemory2)
|
||||
allocatorInfo.flags |= VMA_ALLOCATOR_CREATE_KHR_BIND_MEMORY2_BIT;
|
||||
|
||||
if (IsExtensionLoaded(VK_EXT_MEMORY_BUDGET_EXTENSION_NAME))
|
||||
allocatorInfo.flags |= VMA_ALLOCATOR_CREATE_EXT_MEMORY_BUDGET_BIT;
|
||||
|
||||
m_lastErrorCode = vmaCreateAllocator(&allocatorInfo, &m_memAllocator);
|
||||
if (m_lastErrorCode != VK_SUCCESS)
|
||||
{
|
||||
NazaraError("Failed to initialize Vulkan Memory Allocator (VMA): " + TranslateVulkanError(m_lastErrorCode));
|
||||
return false;
|
||||
}
|
||||
|
||||
destroyOnFailure.Reset();
|
||||
|
||||
return true;
|
||||
@@ -193,6 +255,9 @@ namespace Nz
|
||||
if (vkDeviceWaitIdle)
|
||||
vkDeviceWaitIdle(m_device);
|
||||
|
||||
if (m_memAllocator != VK_NULL_HANDLE)
|
||||
vmaDestroyAllocator(m_memAllocator);
|
||||
|
||||
m_internalData.reset();
|
||||
|
||||
if (vkDestroyDevice)
|
||||
|
||||
Reference in New Issue
Block a user