Rework buffers synchronization

This commit is contained in:
Lynix
2020-03-13 18:44:49 +01:00
parent 63547fcd4e
commit b774a879b6
19 changed files with 223 additions and 68 deletions

View File

@@ -18,7 +18,6 @@
namespace Nz
{
class Buffer;
class ShaderStageImpl;
class NAZARA_RENDERER_API RenderDevice
@@ -27,7 +26,7 @@ namespace Nz
RenderDevice() = default;
virtual ~RenderDevice();
virtual std::unique_ptr<AbstractBuffer> InstantiateBuffer(Buffer* parent, BufferType type) = 0;
virtual std::unique_ptr<AbstractBuffer> InstantiateBuffer(BufferType type) = 0;
virtual std::unique_ptr<RenderPipeline> InstantiateRenderPipeline(RenderPipelineInfo pipelineInfo) = 0;
virtual std::shared_ptr<RenderPipelineLayout> InstantiateRenderPipelineLayout(RenderPipelineLayoutInfo pipelineLayoutInfo) = 0;
virtual std::shared_ptr<ShaderStageImpl> InstantiateShaderStage(ShaderStageType type, ShaderLanguage lang, const void* source, std::size_t sourceSize) = 0;

View File

@@ -56,10 +56,10 @@ namespace Nz
enum BufferUsage
{
BufferUsage_Dynamic,
BufferUsage_FastRead,
BufferUsage_DeviceLocal,
BufferUsage_DirectMapping,
BufferUsage_Max = BufferUsage_FastRead
BufferUsage_Max = BufferUsage_DirectMapping
};
template<>

View File

@@ -12,9 +12,11 @@
#include <Nazara/Renderer/Enums.hpp>
#include <Nazara/Utility/Enums.hpp>
#include <Nazara/VulkanRenderer/Wrapper/Loader.hpp>
#include <string>
namespace Nz
{
inline VkBufferUsageFlags ToVulkan(BufferType bufferType);
inline VkFormat ToVulkan(ComponentType componentType);
inline VkCullModeFlagBits ToVulkan(FaceSide faceSide);
inline VkPolygonMode ToVulkan(FaceFilling faceFilling);
@@ -26,7 +28,7 @@ namespace Nz
inline VkStencilOp ToVulkan(StencilOperation stencilOp);
inline VkVertexInputRate ToVulkan(VertexInputRate inputRate);
NAZARA_VULKANRENDERER_API String TranslateVulkanError(VkResult code);
NAZARA_VULKANRENDERER_API std::string TranslateVulkanError(VkResult code);
}
#include <Nazara/VulkanRenderer/Utils.inl>

View File

@@ -10,6 +10,19 @@
namespace Nz
{
VkBufferUsageFlags ToVulkan(BufferType bufferType)
{
switch (bufferType)
{
case BufferType_Index: return VK_BUFFER_USAGE_INDEX_BUFFER_BIT;
case BufferType_Vertex: return VK_BUFFER_USAGE_VERTEX_BUFFER_BIT;
case BufferType_Uniform: return VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT;
}
NazaraError("Unhandled BufferType 0x" + String::Number(bufferType, 16));
return 0;
}
VkFormat ToVulkan(ComponentType componentType)
{
switch (componentType)

View File

@@ -18,12 +18,10 @@
namespace Nz
{
class Buffer;
class NAZARA_VULKANRENDERER_API VulkanBuffer : public AbstractBuffer
{
public:
inline VulkanBuffer(Vk::Device& device, Buffer* parent, BufferType type);
inline VulkanBuffer(Vk::Device& device, BufferType type);
VulkanBuffer(const VulkanBuffer&) = delete;
VulkanBuffer(VulkanBuffer&&) = delete; ///TODO
virtual ~VulkanBuffer();
@@ -35,18 +33,22 @@ namespace Nz
DataStorage GetStorage() const override;
void* Map(BufferAccess access, UInt32 offset = 0, UInt32 size = 0) override;
void* Map(BufferAccess access, UInt32 offset, UInt32 size) override;
bool Unmap() override;
VulkanBuffer& operator=(const VulkanBuffer&) = delete;
VulkanBuffer& operator=(VulkanBuffer&&) = delete; ///TODO
private:
Buffer* m_parent;
Vk::Buffer m_stagingBuffer;
Vk::DeviceMemory m_stagingMemory;
BufferType m_type;
Nz::Vk::Buffer m_buffer;
Nz::Vk::DeviceMemory m_memory;
BufferUsageFlags m_usage;
UInt32 m_size;
Vk::Buffer m_buffer;
Vk::Fence m_stagingFence;
Vk::Device& m_device;
Vk::DeviceMemory m_memory;
};
}

View File

@@ -7,14 +7,13 @@
namespace Nz
{
inline VulkanBuffer::VulkanBuffer(Vk::Device& device, Buffer* /*parent*/, BufferType type) :
inline VulkanBuffer::VulkanBuffer(Vk::Device& device, BufferType type) :
m_device(device),
m_parent(parent),
m_type(type)
{
}
inline Nz::Vk::Buffer& Nz::VulkanBuffer::GetBufferHandle()
inline Vk::Buffer& VulkanBuffer::GetBufferHandle()
{
return m_buffer;
}

View File

@@ -23,7 +23,7 @@ namespace Nz
VulkanDevice(VulkanDevice&&) = delete; ///TODO?
~VulkanDevice();
std::unique_ptr<AbstractBuffer> InstantiateBuffer(Buffer* parent, BufferType type) override;
std::unique_ptr<AbstractBuffer> InstantiateBuffer(BufferType type) override;
std::unique_ptr<RenderPipeline> InstantiateRenderPipeline(RenderPipelineInfo pipelineInfo) override;
std::shared_ptr<RenderPipelineLayout> InstantiateRenderPipelineLayout(RenderPipelineLayoutInfo pipelineLayoutInfo) override;
std::shared_ptr<ShaderStageImpl> InstantiateShaderStage(ShaderStageType type, ShaderLanguage lang, const void* source, std::size_t sourceSize) override;

View File

@@ -51,6 +51,8 @@ namespace Nz
inline void ClearDepthStencilImage(VkImage image, VkImageLayout imageLayout, const VkClearDepthStencilValue& depthStencil, const VkImageSubresourceRange& range);
inline void ClearDepthStencilImage(VkImage image, VkImageLayout imageLayout, const VkClearDepthStencilValue& depthStencil, UInt32 rangeCount, const VkImageSubresourceRange* ranges);
inline void CopyBuffer(VkBuffer source, VkBuffer target, UInt32 size, UInt32 sourceOffset = 0, UInt32 targetOffset = 0);
inline void Draw(UInt32 vertexCount, UInt32 instanceCount = 1, UInt32 firstVertex = 0, UInt32 firstInstance = 0);
inline void DrawIndexed(UInt32 indexCount, UInt32 instanceCount = 1, UInt32 firstVertex = 0, Int32 vertexOffset = 0, UInt32 firstInstance = 0);

View File

@@ -194,6 +194,16 @@ namespace Nz
return m_pool->GetDevice()->vkCmdClearDepthStencilImage(m_handle, image, imageLayout, &depthStencil, rangeCount, ranges);
}
inline void CommandBuffer::CopyBuffer(VkBuffer source, VkBuffer target, UInt32 size, UInt32 sourceOffset, UInt32 targetOffset)
{
VkBufferCopy region;
region.dstOffset = targetOffset;
region.size = size;
region.srcOffset = sourceOffset;
return m_pool->GetDevice()->vkCmdCopyBuffer(m_handle, source, target, 1, &region);
}
inline void CommandBuffer::Draw(UInt32 vertexCount, UInt32 instanceCount, UInt32 firstVertex, UInt32 firstInstance)
{
return m_pool->GetDevice()->vkCmdDraw(m_handle, vertexCount, instanceCount, firstVertex, firstInstance);

View File

@@ -19,6 +19,7 @@ namespace Nz
{
namespace Vk
{
class CommandBuffer;
class Instance;
class Queue;
@@ -34,6 +35,8 @@ namespace Nz
Device(Device&&) = delete;
~Device();
CommandBuffer AllocateTransferCommandBuffer();
bool Create(const Vk::PhysicalDevice& deviceInfo, const VkDeviceCreateInfo& createInfo, const VkAllocationCallbacks* allocator = nullptr);
inline void Destroy();
@@ -47,6 +50,8 @@ namespace Nz
inline VkPhysicalDevice GetPhysicalDevice() const;
inline const Vk::PhysicalDevice& GetPhysicalDeviceInfo() const;
inline UInt32 GetTransferQueueFamilyIndex() const;
inline bool IsExtensionLoaded(const std::string& extensionName);
inline bool IsLayerLoaded(const std::string& layerName);
@@ -90,11 +95,15 @@ namespace Nz
inline PFN_vkVoidFunction GetProcAddr(const char* name);
struct InternalData;
std::unique_ptr<InternalData> m_internalData;
Instance& m_instance;
const Vk::PhysicalDevice* m_physicalDevice;
VkAllocationCallbacks m_allocator;
VkDevice m_device;
VkResult m_lastErrorCode;
UInt32 m_transferQueueFamilyIndex;
std::unordered_set<std::string> m_loadedExtensions;
std::unordered_set<std::string> m_loadedLayers;
std::vector<QueueFamilyInfo> m_enabledQueuesInfos;

View File

@@ -49,6 +49,11 @@ namespace Nz
return *m_physicalDevice;
}
inline UInt32 Device::GetTransferQueueFamilyIndex() const
{
return m_transferQueueFamilyIndex;
}
inline bool Device::IsExtensionLoaded(const std::string& extensionName)
{
return m_loadedExtensions.count(extensionName) > 0;

View File

@@ -28,6 +28,9 @@ namespace Nz
inline bool Create(Device& device, VkDeviceSize size, UInt32 memoryType, const VkAllocationCallbacks* allocator = nullptr);
inline bool Create(Device& device, VkDeviceSize size, UInt32 typeBits, VkFlags properties, const VkAllocationCallbacks* allocator = nullptr);
inline bool FlushMemory();
inline bool FlushMemory(UInt64 offset, UInt64 size);
inline void* GetMappedPointer();
inline bool Map(VkDeviceSize offset, VkDeviceSize size, VkMemoryMapFlags flags = 0);

View File

@@ -57,6 +57,31 @@ namespace Nz
return false;
}
inline bool DeviceMemory::FlushMemory()
{
return FlushMemory(0, VK_WHOLE_SIZE);
}
inline bool DeviceMemory::FlushMemory(UInt64 offset, UInt64 size)
{
VkMappedMemoryRange memoryRange = {
VK_STRUCTURE_TYPE_MAPPED_MEMORY_RANGE,
nullptr,
m_handle,
offset,
size
};
m_lastErrorCode = m_device->vkFlushMappedMemoryRanges(*m_device, 1, &memoryRange);
if (m_lastErrorCode != VK_SUCCESS)
{
NazaraError("Failed to flush memory: " + TranslateVulkanError(m_lastErrorCode));
return false;
}
return true;
}
inline void* DeviceMemory::GetMappedPointer()
{
return m_mappedPtr;