Implement UploadPool to efficiently update UBOs
This commit is contained in:
@@ -21,6 +21,8 @@ namespace Nz
|
||||
class AbstractHash;
|
||||
class ByteArray;
|
||||
|
||||
template<typename T> constexpr T Align(T offset, T alignment);
|
||||
template<typename T> constexpr T AlignPow2(T offset, T alignment);
|
||||
template<typename F, typename Tuple> decltype(auto) Apply(F&& fn, Tuple&& t);
|
||||
template<typename O, typename F, typename Tuple> decltype(auto) Apply(O& object, F&& fn, Tuple&& t);
|
||||
template<typename T> constexpr std::size_t BitCount();
|
||||
@@ -29,6 +31,7 @@ namespace Nz
|
||||
template<typename T, std::size_t N> constexpr std::size_t CountOf(T(&name)[N]) noexcept;
|
||||
template<typename T> std::size_t CountOf(const T& c);
|
||||
template<typename T> void HashCombine(std::size_t& seed, const T& v);
|
||||
template<typename T> bool IsPowerOfTwo(T value);
|
||||
template<typename T> T ReverseBits(T integer);
|
||||
template<typename T> constexpr auto UnderlyingCast(T value) -> std::underlying_type_t<T>;
|
||||
|
||||
|
||||
@@ -11,6 +11,7 @@
|
||||
#include <Nazara/Core/ByteArray.hpp>
|
||||
#include <Nazara/Core/Error.hpp>
|
||||
#include <Nazara/Core/Stream.hpp>
|
||||
#include <cassert>
|
||||
#include <climits>
|
||||
#include <utility>
|
||||
#include <Nazara/Core/Debug.hpp>
|
||||
@@ -35,6 +36,43 @@ namespace Nz
|
||||
NAZARA_CORE_API extern const UInt8 BitReverseTable256[256];
|
||||
}
|
||||
|
||||
/*!
|
||||
* \ingroup core
|
||||
* \brief Align an offset
|
||||
* \return Aligned offset according to alignment
|
||||
*
|
||||
* \param offset Base offset
|
||||
* \param alignment Non-zero alignment
|
||||
*
|
||||
* \see AlignPow2
|
||||
*/
|
||||
template<typename T>
|
||||
constexpr T Align(T offset, T alignment)
|
||||
{
|
||||
assert(alignment > 0);
|
||||
return ((offset + alignment - 1) / alignment) * alignment;
|
||||
}
|
||||
|
||||
/*!
|
||||
* \ingroup core
|
||||
* \brief Align an offset
|
||||
* \return Aligned offset according to a power of two alignment
|
||||
*
|
||||
* \param offset Base offset
|
||||
* \param alignment Non-zero power of two alignment
|
||||
*
|
||||
* \see Align
|
||||
* \remark This function is quicker than Align but only works with power of two alignment values
|
||||
*/
|
||||
template<typename T>
|
||||
constexpr T AlignPow2(T offset, T alignment)
|
||||
{
|
||||
assert(alignment > 0);
|
||||
assert(IsPowerOfTwo(alignment));
|
||||
|
||||
return (offset + alignment - 1) & ~(alignment - 1);
|
||||
}
|
||||
|
||||
/*!
|
||||
* \ingroup core
|
||||
* \brief Applies the tuple to the function (e.g. calls the function using the tuple content as arguments)
|
||||
@@ -178,6 +216,20 @@ namespace Nz
|
||||
seed = static_cast<std::size_t>(b * kMul);
|
||||
}
|
||||
|
||||
/*!
|
||||
* \ingroup core
|
||||
* \brief Check if a value is a power of two
|
||||
* \return true if value is a power of two
|
||||
*
|
||||
* \param value Non-zero value
|
||||
*/
|
||||
template<typename T>
|
||||
bool IsPowerOfTwo(T value)
|
||||
{
|
||||
assert(value != 0);
|
||||
return (value & (value - 1)) == 0;
|
||||
}
|
||||
|
||||
/*!
|
||||
* \ingroup core
|
||||
* \brief Reverse the bit order of the integer
|
||||
|
||||
@@ -41,6 +41,7 @@
|
||||
#include <Nazara/VulkanRenderer/VulkanRenderPipelineLayout.hpp>
|
||||
#include <Nazara/VulkanRenderer/VulkanShaderStage.hpp>
|
||||
#include <Nazara/VulkanRenderer/VulkanSurface.hpp>
|
||||
#include <Nazara/VulkanRenderer/VulkanUploadPool.hpp>
|
||||
#include <Nazara/VulkanRenderer/Wrapper.hpp>
|
||||
|
||||
#endif // NAZARA_GLOBAL_VULKANRENDERER_HPP
|
||||
|
||||
61
include/Nazara/VulkanRenderer/VulkanUploadPool.hpp
Normal file
61
include/Nazara/VulkanRenderer/VulkanUploadPool.hpp
Normal file
@@ -0,0 +1,61 @@
|
||||
// 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_VULKANRENDERER_VULKANUPLOADPOOL_HPP
|
||||
#define NAZARA_VULKANRENDERER_VULKANUPLOADPOOL_HPP
|
||||
|
||||
#include <Nazara/Prerequisites.hpp>
|
||||
#include <Nazara/Core/MovablePtr.hpp>
|
||||
#include <Nazara/VulkanRenderer/Wrapper/Buffer.hpp>
|
||||
#include <Nazara/VulkanRenderer/Wrapper/DeviceMemory.hpp>
|
||||
#include <optional>
|
||||
#include <vector>
|
||||
|
||||
namespace Nz
|
||||
{
|
||||
class NAZARA_VULKANRENDERER_API VulkanUploadPool
|
||||
{
|
||||
public:
|
||||
struct AllocationData;
|
||||
|
||||
inline VulkanUploadPool(Vk::Device& device, UInt64 blockSize);
|
||||
VulkanUploadPool(const VulkanUploadPool&) = delete;
|
||||
VulkanUploadPool(VulkanUploadPool&&) noexcept = default;
|
||||
~VulkanUploadPool() = default;
|
||||
|
||||
std::optional<AllocationData> Allocate(UInt64 size);
|
||||
std::optional<AllocationData> Allocate(UInt64 size, UInt64 alignment);
|
||||
|
||||
void Reset();
|
||||
|
||||
struct AllocationData
|
||||
{
|
||||
VkBuffer buffer;
|
||||
void* mappedPtr;
|
||||
UInt64 offset;
|
||||
UInt64 size;
|
||||
};
|
||||
|
||||
VulkanUploadPool& operator=(const VulkanUploadPool&) = delete;
|
||||
VulkanUploadPool& operator=(VulkanUploadPool&&) = delete;
|
||||
|
||||
private:
|
||||
struct Block
|
||||
{
|
||||
Vk::DeviceMemory blockMemory;
|
||||
Vk::Buffer buffer;
|
||||
UInt64 freeOffset;
|
||||
};
|
||||
|
||||
UInt64 m_blockSize;
|
||||
Vk::Device& m_device;
|
||||
std::vector<Block> m_blocks;
|
||||
};
|
||||
}
|
||||
|
||||
#include <Nazara/VulkanRenderer/VulkanUploadPool.inl>
|
||||
|
||||
#endif // NAZARA_VULKANRENDERER_VULKANUPLOADPOOL_HPP
|
||||
17
include/Nazara/VulkanRenderer/VulkanUploadPool.inl
Normal file
17
include/Nazara/VulkanRenderer/VulkanUploadPool.inl
Normal file
@@ -0,0 +1,17 @@
|
||||
// Copyright (C) 2020 Jérôme Leclercq
|
||||
// This file is part of the "Nazara Engine - Vulkan Renderer"
|
||||
// For conditions of distribution and use, see copyright notice in Config.hpp
|
||||
|
||||
#include <Nazara/VulkanRenderer/VulkanUploadPool.hpp>
|
||||
#include <Nazara/VulkanRenderer/Debug.hpp>
|
||||
|
||||
namespace Nz
|
||||
{
|
||||
inline VulkanUploadPool::VulkanUploadPool(Vk::Device& device, UInt64 blockSize) :
|
||||
m_blockSize(blockSize),
|
||||
m_device(device)
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
#include <Nazara/VulkanRenderer/DebugOff.hpp>
|
||||
@@ -21,7 +21,7 @@ namespace Nz
|
||||
public:
|
||||
Buffer() = default;
|
||||
Buffer(const Buffer&) = delete;
|
||||
Buffer(Buffer&&) = default;
|
||||
Buffer(Buffer&&) noexcept = default;
|
||||
~Buffer() = default;
|
||||
|
||||
bool BindBufferMemory(VkDeviceMemory memory, VkDeviceSize offset = 0);
|
||||
|
||||
@@ -8,6 +8,7 @@
|
||||
#define NAZARA_VULKANRENDERER_VKDEVICEMEMORY_HPP
|
||||
|
||||
#include <Nazara/Prerequisites.hpp>
|
||||
#include <Nazara/Core/MovablePtr.hpp>
|
||||
#include <Nazara/VulkanRenderer/Wrapper/DeviceObject.hpp>
|
||||
|
||||
namespace Nz
|
||||
@@ -19,9 +20,9 @@ namespace Nz
|
||||
friend DeviceObject;
|
||||
|
||||
public:
|
||||
DeviceMemory();
|
||||
DeviceMemory() = default;
|
||||
DeviceMemory(const DeviceMemory&) = delete;
|
||||
inline DeviceMemory(DeviceMemory&& memory);
|
||||
DeviceMemory(DeviceMemory&& memory) noexcept = default;
|
||||
~DeviceMemory() = default;
|
||||
|
||||
using DeviceObject::Create;
|
||||
@@ -33,6 +34,7 @@ namespace Nz
|
||||
|
||||
inline void* GetMappedPointer();
|
||||
|
||||
inline bool Map(VkMemoryMapFlags flags = 0);
|
||||
inline bool Map(VkDeviceSize offset, VkDeviceSize size, VkMemoryMapFlags flags = 0);
|
||||
|
||||
inline void Unmap();
|
||||
@@ -44,7 +46,7 @@ namespace Nz
|
||||
static inline VkResult CreateHelper(Device& device, const VkMemoryAllocateInfo* allocInfo, const VkAllocationCallbacks* allocator, VkDeviceMemory* handle);
|
||||
static inline void DestroyHelper(Device& device, VkDeviceMemory handle, const VkAllocationCallbacks* allocator);
|
||||
|
||||
void* m_mappedPtr;
|
||||
MovablePtr<void> m_mappedPtr;
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
@@ -12,18 +12,6 @@ namespace Nz
|
||||
{
|
||||
namespace Vk
|
||||
{
|
||||
inline DeviceMemory::DeviceMemory() :
|
||||
m_mappedPtr(nullptr)
|
||||
{
|
||||
}
|
||||
|
||||
inline DeviceMemory::DeviceMemory(DeviceMemory&& memory) :
|
||||
DeviceObject(std::move(memory))
|
||||
{
|
||||
m_mappedPtr = memory.m_mappedPtr;
|
||||
memory.m_mappedPtr = nullptr;
|
||||
}
|
||||
|
||||
inline bool DeviceMemory::Create(Device& device, VkDeviceSize size, UInt32 memoryType, const VkAllocationCallbacks* allocator)
|
||||
{
|
||||
VkMemoryAllocateInfo allocInfo =
|
||||
@@ -87,15 +75,23 @@ namespace Nz
|
||||
return m_mappedPtr;
|
||||
}
|
||||
|
||||
inline bool DeviceMemory::Map(VkMemoryMapFlags flags)
|
||||
{
|
||||
return Map(0, VK_WHOLE_SIZE, flags);
|
||||
}
|
||||
|
||||
inline bool DeviceMemory::Map(VkDeviceSize offset, VkDeviceSize size, VkMemoryMapFlags flags)
|
||||
{
|
||||
m_lastErrorCode = m_device->vkMapMemory(*m_device, m_handle, offset, size, flags, &m_mappedPtr);
|
||||
void* mappedPtr;
|
||||
m_lastErrorCode = m_device->vkMapMemory(*m_device, m_handle, offset, size, flags, &mappedPtr);
|
||||
if (m_lastErrorCode != VK_SUCCESS)
|
||||
{
|
||||
NazaraError("Failed to map device memory: " + TranslateVulkanError(m_lastErrorCode));
|
||||
return false;
|
||||
}
|
||||
|
||||
m_mappedPtr = mappedPtr;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user