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

@@ -39,7 +39,7 @@ namespace Nz
inline void BindIndexBuffer(GLuint indexBuffer, UInt64 offset = 0);
inline void BindPipeline(const OpenGLRenderPipeline* pipeline);
inline void BindShaderBinding(const OpenGLShaderBinding* binding);
inline void BindShaderBinding(const OpenGLRenderPipelineLayout& pipelineLayout, UInt32 set, const OpenGLShaderBinding* binding);
inline void BindVertexBuffer(UInt32 binding, GLuint vertexBuffer, UInt64 offset = 0);
inline void CopyBuffer(GLuint source, GLuint target, UInt64 size, UInt64 sourceOffset = 0, UInt64 targetOffset = 0);
@@ -102,10 +102,10 @@ namespace Nz
GLuint indexBuffer = 0;
const OpenGLRenderPipeline* pipeline = nullptr;
const OpenGLShaderBinding* shaderBindings = nullptr;
UInt64 indexBufferOffset;
std::optional<Recti> scissorRegion;
std::optional<Recti> viewportRegion;
std::vector<std::pair<const OpenGLRenderPipelineLayout*, const OpenGLShaderBinding*>> shaderBindings;
std::vector<VertexBuffer> vertexBuffers;
bool shouldFlipY = false;
};

View File

@@ -44,9 +44,12 @@ namespace Nz
m_currentStates.pipeline = pipeline;
}
inline void OpenGLCommandBuffer::BindShaderBinding(const OpenGLShaderBinding* binding)
inline void OpenGLCommandBuffer::BindShaderBinding(const OpenGLRenderPipelineLayout& pipelineLayout, UInt32 set, const OpenGLShaderBinding* binding)
{
m_currentStates.shaderBindings = binding;
if (set >= m_currentStates.shaderBindings.size())
m_currentStates.shaderBindings.resize(set + 1);
m_currentStates.shaderBindings[set] = std::make_pair(&pipelineLayout, binding);
}
inline void OpenGLCommandBuffer::BindVertexBuffer(UInt32 binding, GLuint vertexBuffer, UInt64 offset)

View File

@@ -28,7 +28,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

@@ -40,7 +40,7 @@ namespace Nz
std::shared_ptr<RenderPass> InstantiateRenderPass(std::vector<RenderPass::Attachment> attachments, std::vector<RenderPass::SubpassDescription> subpassDescriptions, std::vector<RenderPass::SubpassDependency> subpassDependencies) override;
std::shared_ptr<RenderPipeline> InstantiateRenderPipeline(RenderPipelineInfo pipelineInfo) override;
std::shared_ptr<RenderPipelineLayout> InstantiateRenderPipelineLayout(RenderPipelineLayoutInfo pipelineLayoutInfo) override;
std::shared_ptr<ShaderModule> InstantiateShaderModule(ShaderStageTypeFlags shaderStages, ShaderAst::StatementPtr& shaderAst, const ShaderWriter::States& states) override;
std::shared_ptr<ShaderModule> InstantiateShaderModule(ShaderStageTypeFlags shaderStages, ShaderAst::Statement& shaderAst, const ShaderWriter::States& states) override;
std::shared_ptr<ShaderModule> InstantiateShaderModule(ShaderStageTypeFlags shaderStages, ShaderLanguage lang, const void* source, std::size_t sourceSize, const ShaderWriter::States& states) override;
std::shared_ptr<Texture> InstantiateTexture(const TextureInfo& params) override;
std::shared_ptr<TextureSampler> InstantiateTextureSampler(const TextureSamplerInfo& params) override;

View File

@@ -14,6 +14,7 @@
#include <Nazara/OpenGLRenderer/OpenGLShaderBinding.hpp>
#include <Nazara/OpenGLRenderer/Wrapper/Context.hpp>
#include <Nazara/OpenGLRenderer/Wrapper/CoreFunctions.hpp>
#include <Nazara/Shader/GlslWriter.hpp>
#include <memory>
#include <type_traits>
#include <vector>
@@ -30,13 +31,11 @@ namespace Nz
OpenGLRenderPipelineLayout(OpenGLRenderPipelineLayout&&) = delete;
~OpenGLRenderPipelineLayout();
ShaderBindingPtr AllocateShaderBinding() override;
ShaderBindingPtr AllocateShaderBinding(UInt32 setIndex) override;
inline const GlslWriter::BindingMapping& GetBindingMapping() const;
inline const RenderPipelineLayoutInfo& GetLayoutInfo() const;
inline std::size_t GetTextureDescriptorCount() const;
inline std::size_t GetUniformBufferDescriptorCount() const;
OpenGLRenderPipelineLayout& operator=(const OpenGLRenderPipelineLayout&) = delete;
OpenGLRenderPipelineLayout& operator=(OpenGLRenderPipelineLayout&&) = delete;
@@ -46,17 +45,15 @@ namespace Nz
struct UniformBufferDescriptor;
DescriptorPool& AllocatePool();
ShaderBindingPtr AllocateFromPool(std::size_t poolIndex);
TextureDescriptor& GetTextureDescriptor(std::size_t poolIndex, std::size_t bindingIndex, std::size_t textureIndex);
UniformBufferDescriptor& GetUniformBufferDescriptor(std::size_t poolIndex, std::size_t bindingIndex, std::size_t uniformBufferIndex);
ShaderBindingPtr AllocateFromPool(std::size_t poolIndex, UInt32 setIndex);
template<typename F> void ForEachDescriptor(std::size_t poolIndex, std::size_t bindingIndex, F&& functor);
TextureDescriptor& GetTextureDescriptor(std::size_t poolIndex, std::size_t bindingIndex, std::size_t descriptorIndex);
UniformBufferDescriptor& GetUniformBufferDescriptor(std::size_t poolIndex, std::size_t bindingIndex, std::size_t descriptorIndex);
void Release(ShaderBinding& binding);
inline void TryToShrink();
static constexpr UInt32 InvalidIndex = 0xFFFFFFFF;
struct TextureDescriptor
{
UInt32 bindingIndex = InvalidIndex;
GLuint texture;
GLuint sampler;
GL::TextureTarget textureTarget;
@@ -64,25 +61,25 @@ namespace Nz
struct UniformBufferDescriptor
{
UInt32 bindingIndex = InvalidIndex;
GLuint buffer;
GLintptr offset;
GLsizeiptr size;
};
using Descriptor = std::variant<std::monostate, TextureDescriptor, UniformBufferDescriptor>;
struct DescriptorPool
{
using BindingStorage = std::aligned_storage_t<sizeof(OpenGLShaderBinding), alignof(OpenGLShaderBinding)>;
Bitset<UInt64> freeBindings;
std::vector<TextureDescriptor> textureDescriptor;
std::vector<UniformBufferDescriptor> uniformBufferDescriptor;
std::vector<Descriptor> descriptors;
std::unique_ptr<BindingStorage[]> storage;
};
std::size_t m_textureDescriptorCount;
std::size_t m_uniformBufferDescriptorCount;
std::size_t m_maxDescriptorCount;
std::vector<DescriptorPool> m_descriptorPools;
GlslWriter::BindingMapping m_bindingMapping;
RenderPipelineLayoutInfo m_layoutInfo;
};
}

View File

@@ -7,19 +7,32 @@
namespace Nz
{
inline const GlslWriter::BindingMapping& OpenGLRenderPipelineLayout::GetBindingMapping() const
{
return m_bindingMapping;
}
inline const RenderPipelineLayoutInfo& OpenGLRenderPipelineLayout::GetLayoutInfo() const
{
return m_layoutInfo;
}
inline std::size_t OpenGLRenderPipelineLayout::GetTextureDescriptorCount() const
template<typename F>
void OpenGLRenderPipelineLayout::ForEachDescriptor(std::size_t poolIndex, std::size_t bindingIndex, F&& functor)
{
return m_textureDescriptorCount;
}
assert(poolIndex < m_descriptorPools.size());
auto& pool = m_descriptorPools[poolIndex];
assert(!pool.freeBindings.Test(bindingIndex));
inline std::size_t OpenGLRenderPipelineLayout::GetUniformBufferDescriptorCount() const
{
return m_uniformBufferDescriptorCount;
for (std::size_t descriptorIndex = 0; descriptorIndex < m_maxDescriptorCount; ++descriptorIndex)
{
std::visit([&](auto&& arg)
{
if constexpr (!std::is_same_v<std::decay_t<decltype(arg)>, std::monostate>)
functor(UInt32(descriptorIndex), arg);
},
pool.descriptors[bindingIndex * m_maxDescriptorCount + descriptorIndex]);
}
}
inline void OpenGLRenderPipelineLayout::TryToShrink()

View File

@@ -24,7 +24,7 @@ namespace Nz
OpenGLShaderBinding(OpenGLShaderBinding&&) = delete;
~OpenGLShaderBinding() = default;
void Apply(const GL::Context& context) const;
void Apply(const OpenGLRenderPipelineLayout& pipelineLayout, UInt32 setIndex, const GL::Context& context) const;
inline std::size_t GetBindingIndex() const;
inline std::size_t GetPoolIndex() const;

View File

@@ -11,8 +11,10 @@
#include <Nazara/Renderer/Enums.hpp>
#include <Nazara/Renderer/ShaderModule.hpp>
#include <Nazara/OpenGLRenderer/Wrapper/Context.hpp>
#include <Nazara/OpenGLRenderer/Wrapper/Program.hpp>
#include <Nazara/OpenGLRenderer/Wrapper/Shader.hpp>
#include <Nazara/Shader/ShaderWriter.hpp>
#include <Nazara/Shader/GlslWriter.hpp>
#include <Nazara/Shader/Ast/Nodes.hpp>
#include <vector>
namespace Nz
@@ -20,30 +22,40 @@ namespace Nz
class NAZARA_OPENGLRENDERER_API OpenGLShaderModule : public ShaderModule
{
public:
struct Shader;
OpenGLShaderModule(OpenGLDevice& device, ShaderStageTypeFlags shaderStages, ShaderAst::StatementPtr& shaderAst, const ShaderWriter::States& states = {});
OpenGLShaderModule(OpenGLDevice& device, ShaderStageTypeFlags shaderStages, ShaderAst::Statement& shaderAst, const ShaderWriter::States& states = {});
OpenGLShaderModule(OpenGLDevice& device, ShaderStageTypeFlags shaderStages, ShaderLanguage lang, const void* source, std::size_t sourceSize, const ShaderWriter::States& states = {});
OpenGLShaderModule(const OpenGLShaderModule&) = delete;
OpenGLShaderModule(OpenGLShaderModule&&) noexcept = default;
~OpenGLShaderModule() = default;
inline const std::vector<Shader>& GetShaders() const;
ShaderStageTypeFlags Attach(GL::Program& program, const GlslWriter::BindingMapping& bindingMapping) const;
OpenGLShaderModule& operator=(const OpenGLShaderModule&) = delete;
OpenGLShaderModule& operator=(OpenGLShaderModule&&) noexcept = default;
struct Shader
{
ShaderStageType stage;
GL::Shader shader;
};
private:
void Create(OpenGLDevice& device, ShaderStageTypeFlags shaderStages, ShaderAst::StatementPtr& shaderAst, const ShaderWriter::States& states);
void Create(OpenGLDevice& device, ShaderStageTypeFlags shaderStages, ShaderAst::Statement& shaderAst, const ShaderWriter::States& states);
static void CheckCompilationStatus(GL::Shader& shader);
struct GlslShader
{
std::string sourceCode;
};
struct ShaderStatement
{
std::shared_ptr<ShaderAst::Statement> ast;
};
struct Shader
{
ShaderStageType stage;
std::variant<GlslShader, ShaderStatement> shader;
};
OpenGLDevice& m_device;
ShaderWriter::States m_states;
std::vector<Shader> m_shaders;
};
}

View File

@@ -7,10 +7,6 @@
namespace Nz
{
inline auto OpenGLShaderModule::GetShaders() const -> const std::vector<Shader>&
{
return m_shaders;
}
}
#include <Nazara/OpenGLRenderer/DebugOff.hpp>

View File

@@ -21,6 +21,7 @@ namespace Nz
class Framebuffer;
class RenderPass;
class RenderPipeline;
class RenderPipelineLayout;
class ShaderBinding;
class Texture;
@@ -41,7 +42,8 @@ namespace Nz
virtual void BindIndexBuffer(Nz::AbstractBuffer* indexBuffer, UInt64 offset = 0) = 0;
virtual void BindPipeline(const RenderPipeline& pipeline) = 0;
virtual void BindShaderBinding(const ShaderBinding& binding) = 0;
virtual void BindShaderBinding(UInt32 set, const ShaderBinding& binding) = 0;
virtual void BindShaderBinding(const RenderPipelineLayout& pipelineLayout, UInt32 set, const ShaderBinding& binding) = 0;
virtual void BindVertexBuffer(UInt32 binding, Nz::AbstractBuffer* vertexBuffer, UInt64 offset = 0) = 0;
inline void CopyBuffer(const RenderBufferView& source, const RenderBufferView& target);

View File

@@ -21,9 +21,10 @@ namespace Nz
{
struct Binding
{
UInt32 setIndex;
UInt32 bindingIndex;
ShaderBindingType type;
ShaderStageTypeFlags shaderStageFlags;
unsigned int index;
};
std::vector<Binding> bindings;
@@ -35,7 +36,7 @@ namespace Nz
RenderPipelineLayout() = default;
virtual ~RenderPipelineLayout();
virtual ShaderBindingPtr AllocateShaderBinding() = 0;
virtual ShaderBindingPtr AllocateShaderBinding(UInt32 setIndex) = 0;
};
}

View File

@@ -29,6 +29,7 @@ namespace Nz
Layout, //< Struct layout (struct only) - has argument style
Location, //< Location (struct member only) - has argument index
Option, //< Conditional compilation option - has argument expr
Set, //< Binding set (external var only) - has argument index
};
enum class BinaryType

View File

@@ -83,7 +83,7 @@ namespace Nz::ShaderAst
struct StructMember
{
std::optional<BuiltinEntry> builtin;
std::optional<unsigned int> locationIndex;
std::optional<UInt32> locationIndex;
std::string name;
ExpressionType type;
};

View File

@@ -252,7 +252,8 @@ namespace Nz::ShaderAst
struct ExternalVar
{
std::optional<unsigned int> bindingIndex;
std::optional<UInt32> bindingIndex;
std::optional<UInt32> bindingSet;
std::string name;
ExpressionType type;
};

View File

@@ -22,6 +22,7 @@ namespace Nz
class NAZARA_SHADER_API GlslWriter : public ShaderWriter, public ShaderAst::ExpressionVisitorExcept, public ShaderAst::StatementVisitorExcept
{
public:
using BindingMapping = std::unordered_map<UInt64 /* set | binding */, unsigned /*glBinding*/>;
struct Environment;
using ExtSupportCallback = std::function<bool(const std::string_view& name)>;
@@ -30,8 +31,8 @@ namespace Nz
GlslWriter(GlslWriter&&) = delete;
~GlslWriter() = default;
inline std::string Generate(ShaderAst::Statement& shader, const States& states = {});
std::string Generate(std::optional<ShaderStageType> shaderStage, ShaderAst::Statement& shader, const States& states = {});
inline std::string Generate(ShaderAst::Statement& shader, const BindingMapping& bindingMapping, const States& states = {});
std::string Generate(std::optional<ShaderStageType> shaderStage, ShaderAst::Statement& shader, const BindingMapping& bindingMapping, const States& states = {});
void SetEnv(Environment environment);

View File

@@ -12,9 +12,9 @@ namespace Nz
{
}
inline std::string GlslWriter::Generate(ShaderAst::Statement& shader, const States& states)
inline std::string GlslWriter::Generate(ShaderAst::Statement& shader, const BindingMapping& bindingMapping, const States& states)
{
return Generate(std::nullopt, shader, states);
return Generate(std::nullopt, shader, bindingMapping, states);
}
}

View File

@@ -44,6 +44,7 @@ namespace Nz
struct EntryAttribute;
struct LayoutAttribute;
struct LocationAttribute;
struct SetAttribute;
void Append(const ShaderAst::ExpressionType& type);
void Append(const ShaderAst::IdentifierType& identifierType);
@@ -57,6 +58,8 @@ namespace Nz
template<typename T> void Append(const T& param);
template<typename T1, typename T2, typename... Args> void Append(const T1& firstParam, const T2& secondParam, Args&&... params);
template<typename... Args> void AppendAttributes(bool appendLine, Args&&... params);
template<typename T> void AppendAttributesInternal(bool& first, const T& param);
template<typename T1, typename T2, typename... Rest> void AppendAttributesInternal(bool& first, const T1& firstParam, const T2& secondParam, Rest&&... params);
void AppendAttribute(BindingAttribute binding);
void AppendAttribute(BuiltinAttribute builtin);
void AppendAttribute(DepthWriteAttribute depthWrite);
@@ -64,6 +67,7 @@ namespace Nz
void AppendAttribute(EntryAttribute entry);
void AppendAttribute(LayoutAttribute layout);
void AppendAttribute(LocationAttribute location);
void AppendAttribute(SetAttribute location);
void AppendCommentSection(const std::string& section);
void AppendField(std::size_t structIndex, const ShaderAst::ExpressionPtr* memberIndices, std::size_t remainingMembers);
void AppendHeader();

View File

@@ -38,6 +38,7 @@
#include <Nazara/VulkanRenderer/VulkanCommandBuffer.hpp>
#include <Nazara/VulkanRenderer/VulkanCommandBufferBuilder.hpp>
#include <Nazara/VulkanRenderer/VulkanCommandPool.hpp>
#include <Nazara/VulkanRenderer/VulkanDescriptorSetLayoutCache.hpp>
#include <Nazara/VulkanRenderer/VulkanDevice.hpp>
#include <Nazara/VulkanRenderer/VulkanFramebuffer.hpp>
#include <Nazara/VulkanRenderer/VulkanMultipleFramebuffer.hpp>

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;
}
}