Add initial support for shader binding sets (WIP)

This commit is contained in:
Jérôme Leclercq
2021-06-14 22:35:05 +02:00
parent 815a7b0c62
commit f22b501e25
53 changed files with 885 additions and 511 deletions

View File

@@ -29,7 +29,8 @@ namespace Nz
void BindIndexBuffer(AbstractBuffer* indexBuffer, UInt64 offset = 0) override;
void BindPipeline(const RenderPipeline& pipeline) override;
void BindShaderBinding(const ShaderBinding& binding) override;
void BindShaderBinding(UInt32 set, const ShaderBinding& binding) override;
void BindShaderBinding(const RenderPipelineLayout& pipelineLayout, UInt32 set, const ShaderBinding& binding) override;
void BindVertexBuffer(UInt32 binding, Nz::AbstractBuffer* vertexBuffer, UInt64 offset = 0) override;
void CopyBuffer(const RenderBufferView& source, const RenderBufferView& target, UInt64 size, UInt64 sourceOffset = 0, UInt64 targetOffset = 0) override;

View File

@@ -0,0 +1,58 @@
// 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_VULKANDESCRIPTORSETLAYOUTCACHE_HPP
#define NAZARA_VULKANRENDERER_VULKANDESCRIPTORSETLAYOUTCACHE_HPP
#include <Nazara/Prerequisites.hpp>
#include <Nazara/Core/Algorithm.hpp>
#include <Nazara/VulkanRenderer/Wrapper/DescriptorSetLayout.hpp>
#include <unordered_map>
namespace Nz
{
namespace Vk
{
class Device;
}
struct VulkanDescriptorSetLayoutInfo
{
VkDescriptorSetLayoutCreateFlags createFlags = 0;
std::vector<VkDescriptorSetLayoutBinding> bindings;
};
struct VulkanDescriptorSetLayoutBindingHasher
{
inline std::size_t operator()(const VulkanDescriptorSetLayoutInfo& layoutInfo) const;
};
struct VulkanDescriptorSetLayoutBindingEqual
{
inline bool operator()(const VulkanDescriptorSetLayoutInfo& lhs, const VulkanDescriptorSetLayoutInfo& rhs) const;
};
class VulkanDescriptorSetLayoutCache
{
public:
inline VulkanDescriptorSetLayoutCache(Vk::Device& device);
~VulkanDescriptorSetLayoutCache() = default;
inline void Clear();
inline const Vk::DescriptorSetLayout& Get(const VulkanDescriptorSetLayoutInfo& layoutInfo) const;
private:
using Cache = std::unordered_map<VulkanDescriptorSetLayoutInfo, Vk::DescriptorSetLayout, VulkanDescriptorSetLayoutBindingHasher, VulkanDescriptorSetLayoutBindingEqual>;
mutable Cache m_cache;
Vk::Device& m_device;
};
}
#include <Nazara/VulkanRenderer/VulkanDescriptorSetLayoutCache.inl>
#endif

View File

@@ -0,0 +1,84 @@
// 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/VulkanDescriptorSetLayoutCache.hpp>
#include <Nazara/VulkanRenderer/Utils.hpp>
#include <stdexcept>
#include <Nazara/VulkanRenderer/Debug.hpp>
namespace Nz
{
inline std::size_t VulkanDescriptorSetLayoutBindingHasher::operator()(const VulkanDescriptorSetLayoutInfo& layoutInfo) const
{
std::size_t hash = 0;
HashCombine(hash, layoutInfo.createFlags);
for (const auto& binding : layoutInfo.bindings)
{
HashCombine(hash, binding.binding);
HashCombine(hash, binding.descriptorCount);
HashCombine(hash, binding.descriptorType);
HashCombine(hash, binding.pImmutableSamplers);
HashCombine(hash, binding.stageFlags);
}
return hash;
}
inline bool VulkanDescriptorSetLayoutBindingEqual::operator()(const VulkanDescriptorSetLayoutInfo& lhs, const VulkanDescriptorSetLayoutInfo& rhs) const
{
if (lhs.createFlags != rhs.createFlags)
return false;
if (lhs.bindings.size() != rhs.bindings.size())
return false;
for (std::size_t i = 0; i < lhs.bindings.size(); ++i)
{
const auto& lhsBinding = lhs.bindings[i];
const auto& bindingRhs = rhs.bindings[i];
if (lhsBinding.binding != bindingRhs.binding)
return false;
if (lhsBinding.descriptorCount != bindingRhs.descriptorCount)
return false;
if (lhsBinding.descriptorType != bindingRhs.descriptorType)
return false;
if (lhsBinding.pImmutableSamplers != bindingRhs.pImmutableSamplers)
return false;
if (lhsBinding.stageFlags != bindingRhs.stageFlags)
return false;
}
return true;
}
inline VulkanDescriptorSetLayoutCache::VulkanDescriptorSetLayoutCache(Vk::Device& device) :
m_device(device)
{
}
inline void VulkanDescriptorSetLayoutCache::Clear()
{
m_cache.clear();
}
inline const Vk::DescriptorSetLayout& VulkanDescriptorSetLayoutCache::Get(const VulkanDescriptorSetLayoutInfo& layoutInfo) const
{
auto it = m_cache.find(layoutInfo);
if (it != m_cache.end())
return it->second;
Vk::DescriptorSetLayout setLayout;
if (!setLayout.Create(m_device, UInt32(layoutInfo.bindings.size()), layoutInfo.bindings.data(), layoutInfo.createFlags))
throw std::runtime_error("failed to create descriptor set layout: " + TranslateVulkanError(setLayout.GetLastErrorCode()));
return m_cache.emplace(layoutInfo, std::move(setLayout)).first->second;
}
}
#include <Nazara/VulkanRenderer/DebugOff.hpp>

View File

@@ -31,20 +31,19 @@ namespace Nz
VulkanRenderPipelineLayout() = default;
~VulkanRenderPipelineLayout();
ShaderBindingPtr AllocateShaderBinding() override;
ShaderBindingPtr AllocateShaderBinding(UInt32 setIndex) override;
bool Create(Vk::Device& device, RenderPipelineLayoutInfo layoutInfo);
inline Vk::Device* GetDevice() const;
inline const Vk::DescriptorSetLayout& GetDescriptorSetLayout() const;
inline const Vk::PipelineLayout& GetPipelineLayout() const;
private:
struct DescriptorPool;
DescriptorPool& AllocatePool();
ShaderBindingPtr AllocateFromPool(std::size_t poolIndex);
ShaderBindingPtr AllocateFromPool(std::size_t poolIndex, UInt32 setIndex);
void Release(ShaderBinding& binding);
inline void TryToShrink();
@@ -59,7 +58,7 @@ namespace Nz
MovablePtr<Vk::Device> m_device;
std::vector<DescriptorPool> m_descriptorPools;
Vk::DescriptorSetLayout m_descriptorSetLayout;
std::vector<const Vk::DescriptorSetLayout*> m_descriptorSetLayouts;
Vk::PipelineLayout m_pipelineLayout;
RenderPipelineLayoutInfo m_layoutInfo;
};

View File

@@ -12,11 +12,6 @@ namespace Nz
return m_device.Get();
}
inline const Vk::DescriptorSetLayout& VulkanRenderPipelineLayout::GetDescriptorSetLayout() const
{
return m_descriptorSetLayout;
}
inline const Vk::PipelineLayout& VulkanRenderPipelineLayout::GetPipelineLayout() const
{
return m_pipelineLayout;

View File

@@ -23,6 +23,8 @@ VK_DEFINE_HANDLE(VmaAllocation)
namespace Nz
{
class VulkanDescriptorSetLayoutCache;
namespace Vk
{
class AutoCommandBuffer;
@@ -46,18 +48,17 @@ namespace Nz
bool Create(const Vk::PhysicalDevice& deviceInfo, const VkDeviceCreateInfo& createInfo, const VkAllocationCallbacks* allocator = nullptr);
inline void Destroy();
inline UInt32 GetDefaultFamilyIndex(QueueType queueType) const;
const VulkanDescriptorSetLayoutCache& GetDescriptorSetLayoutCache() const;
inline const std::vector<QueueFamilyInfo>& GetEnabledQueues() const;
inline const QueueList& GetEnabledQueues(UInt32 familyQueue) const;
QueueHandle GetQueue(UInt32 queueFamilyIndex, UInt32 queueIndex);
inline Instance& GetInstance();
inline const Instance& GetInstance() const;
inline VkResult GetLastErrorCode() const;
inline VmaAllocator GetMemoryAllocator() const;
inline VkPhysicalDevice GetPhysicalDevice() const;
inline const Vk::PhysicalDevice& GetPhysicalDeviceInfo() const;
inline UInt32 GetDefaultFamilyIndex(QueueType queueType) const;
QueueHandle GetQueue(UInt32 queueFamilyIndex, UInt32 queueIndex);
inline bool IsExtensionLoaded(const std::string& extensionName);
inline bool IsLayerLoaded(const std::string& layerName);
@@ -75,13 +76,6 @@ namespace Nz
#include <Nazara/VulkanRenderer/Wrapper/DeviceFunctions.hpp>
struct QueueInfo
{
QueueFamilyInfo* familyInfo;
VkQueue queue;
float priority;
};
struct QueueFamilyInfo
{
QueueList queues;
@@ -91,6 +85,13 @@ namespace Nz
UInt32 timestampValidBits;
};
struct QueueInfo
{
QueueFamilyInfo* familyInfo;
VkQueue queue;
float priority;
};
static constexpr UInt32 InvalidQueue = std::numeric_limits<UInt32>::max();
private:

View File

@@ -8,92 +8,89 @@
#include <Nazara/VulkanRenderer/Wrapper/Instance.hpp>
#include <Nazara/VulkanRenderer/Debug.hpp>
namespace Nz
namespace Nz::Vk
{
namespace Vk
inline UInt32 Device::GetDefaultFamilyIndex(QueueType queueType) const
{
inline const std::vector<Device::QueueFamilyInfo>& Device::GetEnabledQueues() const
return m_defaultQueues[UnderlyingCast(queueType)];
}
inline const std::vector<Device::QueueFamilyInfo>& Device::GetEnabledQueues() const
{
return m_enabledQueuesInfos;
}
inline const Device::QueueList& Device::GetEnabledQueues(UInt32 familyQueue) const
{
NazaraAssert(familyQueue < m_enabledQueuesInfos.size(), "Invalid family queue");
return *m_queuesByFamily[familyQueue];
}
inline Instance& Device::GetInstance()
{
return m_instance;
}
inline const Instance& Device::GetInstance() const
{
return m_instance;
}
inline VkResult Device::GetLastErrorCode() const
{
return m_lastErrorCode;
}
inline VmaAllocator Device::GetMemoryAllocator() const
{
return m_memAllocator;
}
inline VkPhysicalDevice Device::GetPhysicalDevice() const
{
return m_physicalDevice->physDevice;
}
inline const Vk::PhysicalDevice& Device::GetPhysicalDeviceInfo() const
{
return *m_physicalDevice;
}
inline bool Device::IsExtensionLoaded(const std::string& extensionName)
{
return m_loadedExtensions.count(extensionName) > 0;
}
inline bool Device::IsLayerLoaded(const std::string& layerName)
{
return m_loadedLayers.count(layerName) > 0;
}
inline bool Device::WaitForIdle()
{
m_lastErrorCode = vkDeviceWaitIdle(m_device);
if (m_lastErrorCode != VkResult::VK_SUCCESS)
{
return m_enabledQueuesInfos;
NazaraError("Failed to wait for device idle: " + TranslateVulkanError(m_lastErrorCode));
return false;
}
inline const Device::QueueList& Device::GetEnabledQueues(UInt32 familyQueue) const
{
NazaraAssert(familyQueue < m_enabledQueuesInfos.size(), "Invalid family queue");
return true;
}
return *m_queuesByFamily[familyQueue];
}
inline Device::operator VkDevice()
{
return m_device;
}
inline Instance& Device::GetInstance()
{
return m_instance;
}
inline const Instance& Device::GetInstance() const
{
return m_instance;
}
inline VkResult Device::GetLastErrorCode() const
{
return m_lastErrorCode;
}
inline VmaAllocator Device::GetMemoryAllocator() const
{
return m_memAllocator;
}
inline VkPhysicalDevice Device::GetPhysicalDevice() const
{
return m_physicalDevice->physDevice;
}
inline const Vk::PhysicalDevice& Device::GetPhysicalDeviceInfo() const
{
return *m_physicalDevice;
}
inline UInt32 Device::GetDefaultFamilyIndex(QueueType queueType) const
{
return m_defaultQueues[UnderlyingCast(queueType)];
}
inline bool Device::IsExtensionLoaded(const std::string& extensionName)
{
return m_loadedExtensions.count(extensionName) > 0;
}
inline bool Device::IsLayerLoaded(const std::string& layerName)
{
return m_loadedLayers.count(layerName) > 0;
}
inline bool Device::WaitForIdle()
{
m_lastErrorCode = vkDeviceWaitIdle(m_device);
if (m_lastErrorCode != VkResult::VK_SUCCESS)
{
NazaraError("Failed to wait for device idle: " + TranslateVulkanError(m_lastErrorCode));
return false;
}
return true;
}
inline Device::operator VkDevice()
{
return m_device;
}
inline PFN_vkVoidFunction Device::GetProcAddr(const char* name)
{
PFN_vkVoidFunction func = m_instance.GetDeviceProcAddr(m_device, name);
if (!func)
NazaraError("Failed to get " + std::string(name) + " address");
inline PFN_vkVoidFunction Device::GetProcAddr(const char* name)
{
PFN_vkVoidFunction func = m_instance.GetDeviceProcAddr(m_device, name);
if (!func)
NazaraError("Failed to get " + std::string(name) + " address");
return func;
}
return func;
}
}

View File

@@ -13,42 +13,39 @@
#include <vulkan/vulkan_core.h>
#include <string>
namespace Nz
namespace Nz::Vk
{
namespace Vk
template<typename C, typename VkType, typename CreateInfo, VkObjectType ObjectType>
class DeviceObject
{
template<typename C, typename VkType, typename CreateInfo, VkObjectType ObjectType>
class DeviceObject
{
public:
DeviceObject();
DeviceObject(const DeviceObject&) = delete;
DeviceObject(DeviceObject&& object) noexcept;
~DeviceObject();
public:
DeviceObject();
DeviceObject(const DeviceObject&) = delete;
DeviceObject(DeviceObject&& object) noexcept;
~DeviceObject();
bool Create(Device& device, const CreateInfo& createInfo, const VkAllocationCallbacks* allocator = nullptr);
void Destroy();
bool Create(Device& device, const CreateInfo& createInfo, const VkAllocationCallbacks* allocator = nullptr);
void Destroy();
bool IsValid() const;
bool IsValid() const;
Device* GetDevice() const;
VkResult GetLastErrorCode() const;
Device* GetDevice() const;
VkResult GetLastErrorCode() const;
void SetDebugName(const char* name);
void SetDebugName(const std::string& name);
void SetDebugName(const char* name);
void SetDebugName(const std::string& name);
DeviceObject& operator=(const DeviceObject&) = delete;
DeviceObject& operator=(DeviceObject&& object) noexcept;
DeviceObject& operator=(const DeviceObject&) = delete;
DeviceObject& operator=(DeviceObject&& object) noexcept;
operator VkType() const;
operator VkType() const;
protected:
MovablePtr<Device> m_device;
VkAllocationCallbacks m_allocator;
VkType m_handle;
mutable VkResult m_lastErrorCode;
};
}
protected:
MovablePtr<Device> m_device;
VkAllocationCallbacks m_allocator;
VkType m_handle;
mutable VkResult m_lastErrorCode;
};
}
#include <Nazara/VulkanRenderer/Wrapper/DeviceObject.inl>

View File

@@ -8,126 +8,123 @@
#include <type_traits>
#include <Nazara/VulkanRenderer/Debug.hpp>
namespace Nz
namespace Nz::Vk
{
namespace Vk
template<typename C, typename VkType, typename CreateInfo, VkObjectType ObjectType>
DeviceObject<C, VkType, CreateInfo, ObjectType>::DeviceObject() :
m_handle(VK_NULL_HANDLE)
{
template<typename C, typename VkType, typename CreateInfo, VkObjectType ObjectType>
DeviceObject<C, VkType, CreateInfo, ObjectType>::DeviceObject() :
m_handle(VK_NULL_HANDLE)
}
template<typename C, typename VkType, typename CreateInfo, VkObjectType ObjectType>
DeviceObject<C, VkType, CreateInfo, ObjectType>::DeviceObject(DeviceObject&& object) noexcept :
m_device(std::move(object.m_device)),
m_allocator(object.m_allocator),
m_handle(object.m_handle),
m_lastErrorCode(object.m_lastErrorCode)
{
object.m_handle = VK_NULL_HANDLE;
}
template<typename C, typename VkType, typename CreateInfo, VkObjectType ObjectType>
DeviceObject<C, VkType, CreateInfo, ObjectType>::~DeviceObject()
{
Destroy();
}
template<typename C, typename VkType, typename CreateInfo, VkObjectType ObjectType>
bool DeviceObject<C, VkType, CreateInfo, ObjectType>::Create(Device& device, const CreateInfo& createInfo, const VkAllocationCallbacks* allocator)
{
Destroy();
m_device = &device;
m_lastErrorCode = C::CreateHelper(*m_device, &createInfo, allocator, &m_handle);
if (m_lastErrorCode != VkResult::VK_SUCCESS)
{
NazaraError("Failed to create Vulkan object: " + TranslateVulkanError(m_lastErrorCode));
return false;
}
template<typename C, typename VkType, typename CreateInfo, VkObjectType ObjectType>
DeviceObject<C, VkType, CreateInfo, ObjectType>::DeviceObject(DeviceObject&& object) noexcept :
m_device(std::move(object.m_device)),
m_allocator(object.m_allocator),
m_handle(object.m_handle),
m_lastErrorCode(object.m_lastErrorCode)
// Store the allocator to access them when needed
if (allocator)
m_allocator = *allocator;
else
m_allocator.pfnAllocation = nullptr;
return true;
}
template<typename C, typename VkType, typename CreateInfo, VkObjectType ObjectType>
void DeviceObject<C, VkType, CreateInfo, ObjectType>::Destroy()
{
if (IsValid())
{
object.m_handle = VK_NULL_HANDLE;
C::DestroyHelper(*m_device, m_handle, (m_allocator.pfnAllocation) ? &m_allocator : nullptr);
m_handle = VK_NULL_HANDLE;
}
}
template<typename C, typename VkType, typename CreateInfo, VkObjectType ObjectType>
DeviceObject<C, VkType, CreateInfo, ObjectType>::~DeviceObject()
template<typename C, typename VkType, typename CreateInfo, VkObjectType ObjectType>
bool DeviceObject<C, VkType, CreateInfo, ObjectType>::IsValid() const
{
return m_handle != VK_NULL_HANDLE;
}
template<typename C, typename VkType, typename CreateInfo, VkObjectType ObjectType>
Device* DeviceObject<C, VkType, CreateInfo, ObjectType>::GetDevice() const
{
return m_device;
}
template<typename C, typename VkType, typename CreateInfo, VkObjectType ObjectType>
VkResult DeviceObject<C, VkType, CreateInfo, ObjectType>::GetLastErrorCode() const
{
return m_lastErrorCode;
}
template<typename C, typename VkType, typename CreateInfo, VkObjectType ObjectType>
void DeviceObject<C, VkType, CreateInfo, ObjectType>::SetDebugName(const char* name)
{
if (m_device->vkSetDebugUtilsObjectNameEXT)
{
Destroy();
}
VkDebugUtilsObjectNameInfoEXT debugName = {
VK_STRUCTURE_TYPE_DEBUG_UTILS_OBJECT_NAME_INFO_EXT,
nullptr,
ObjectType,
0,
name
};
template<typename C, typename VkType, typename CreateInfo, VkObjectType ObjectType>
bool DeviceObject<C, VkType, CreateInfo, ObjectType>::Create(Device& device, const CreateInfo& createInfo, const VkAllocationCallbacks* allocator)
{
Destroy();
m_device = &device;
m_lastErrorCode = C::CreateHelper(*m_device, &createInfo, allocator, &m_handle);
if (m_lastErrorCode != VkResult::VK_SUCCESS)
{
NazaraError("Failed to create Vulkan object: " + TranslateVulkanError(m_lastErrorCode));
return false;
}
// Store the allocator to access them when needed
if (allocator)
m_allocator = *allocator;
if constexpr (std::is_pointer_v<VkType>)
debugName.objectHandle = static_cast<UInt64>(reinterpret_cast<std::uintptr_t>(m_handle));
else
m_allocator.pfnAllocation = nullptr;
debugName.objectHandle = static_cast<UInt64>(m_handle);
return true;
m_device->vkSetDebugUtilsObjectNameEXT(*m_device, &debugName);
}
}
template<typename C, typename VkType, typename CreateInfo, VkObjectType ObjectType>
void DeviceObject<C, VkType, CreateInfo, ObjectType>::Destroy()
{
if (IsValid())
{
C::DestroyHelper(*m_device, m_handle, (m_allocator.pfnAllocation) ? &m_allocator : nullptr);
m_handle = VK_NULL_HANDLE;
}
}
template<typename C, typename VkType, typename CreateInfo, VkObjectType ObjectType>
void DeviceObject<C, VkType, CreateInfo, ObjectType>::SetDebugName(const std::string& name)
{
return SetDebugName(name.data());
}
template<typename C, typename VkType, typename CreateInfo, VkObjectType ObjectType>
bool DeviceObject<C, VkType, CreateInfo, ObjectType>::IsValid() const
{
return m_handle != VK_NULL_HANDLE;
}
template<typename C, typename VkType, typename CreateInfo, VkObjectType ObjectType>
auto DeviceObject<C, VkType, CreateInfo, ObjectType>::operator=(DeviceObject&& object) noexcept -> DeviceObject&
{
std::swap(m_allocator, object.m_allocator);
std::swap(m_device, object.m_device);
std::swap(m_handle, object.m_handle);
std::swap(m_lastErrorCode, object.m_lastErrorCode);
template<typename C, typename VkType, typename CreateInfo, VkObjectType ObjectType>
Device* DeviceObject<C, VkType, CreateInfo, ObjectType>::GetDevice() const
{
return m_device;
}
return *this;
}
template<typename C, typename VkType, typename CreateInfo, VkObjectType ObjectType>
VkResult DeviceObject<C, VkType, CreateInfo, ObjectType>::GetLastErrorCode() const
{
return m_lastErrorCode;
}
template<typename C, typename VkType, typename CreateInfo, VkObjectType ObjectType>
void DeviceObject<C, VkType, CreateInfo, ObjectType>::SetDebugName(const char* name)
{
if (m_device->vkSetDebugUtilsObjectNameEXT)
{
VkDebugUtilsObjectNameInfoEXT debugName = {
VK_STRUCTURE_TYPE_DEBUG_UTILS_OBJECT_NAME_INFO_EXT,
nullptr,
ObjectType,
0,
name
};
if constexpr (std::is_pointer_v<VkType>)
debugName.objectHandle = static_cast<UInt64>(reinterpret_cast<std::uintptr_t>(m_handle));
else
debugName.objectHandle = static_cast<UInt64>(m_handle);
m_device->vkSetDebugUtilsObjectNameEXT(*m_device, &debugName);
}
}
template<typename C, typename VkType, typename CreateInfo, VkObjectType ObjectType>
void DeviceObject<C, VkType, CreateInfo, ObjectType>::SetDebugName(const std::string& name)
{
return SetDebugName(name.data());
}
template<typename C, typename VkType, typename CreateInfo, VkObjectType ObjectType>
auto DeviceObject<C, VkType, CreateInfo, ObjectType>::operator=(DeviceObject&& object) noexcept -> DeviceObject&
{
std::swap(m_allocator, object.m_allocator);
std::swap(m_device, object.m_device);
std::swap(m_handle, object.m_handle);
std::swap(m_lastErrorCode, object.m_lastErrorCode);
return *this;
}
template<typename C, typename VkType, typename CreateInfo, VkObjectType ObjectType>
DeviceObject<C, VkType, CreateInfo, ObjectType>::operator VkType() const
{
return m_handle;
}
template<typename C, typename VkType, typename CreateInfo, VkObjectType ObjectType>
DeviceObject<C, VkType, CreateInfo, ObjectType>::operator VkType() const
{
return m_handle;
}
}