diff --git a/examples/VulkanTest/main.cpp b/examples/VulkanTest/main.cpp index 754911922..1e38c6260 100644 --- a/examples/VulkanTest/main.cpp +++ b/examples/VulkanTest/main.cpp @@ -179,9 +179,7 @@ int main() std::shared_ptr renderPipelineLayout = device->InstantiateRenderPipelineLayout(pipelineLayoutInfo); - Nz::VulkanRenderPipelineLayout* vkPipelineLayout = static_cast(renderPipelineLayout.get()); - - Nz::Vk::DescriptorSet descriptorSet = vkPipelineLayout->AllocateDescriptorSet(); + Nz::ShaderBinding& shaderBinding = renderPipelineLayout->AllocateShaderBinding(); std::unique_ptr uniformBuffer = device->InstantiateBuffer(Nz::BufferType_Uniform); if (!uniformBuffer->Initialize(uniformSize, Nz::BufferUsage_DeviceLocal)) @@ -190,9 +188,20 @@ int main() return __LINE__; } - Nz::VulkanBuffer* uniformBufferImpl = static_cast(uniformBuffer.get()); - descriptorSet.WriteUniformDescriptor(0, uniformBufferImpl->GetBufferHandle(), 0, uniformSize); - descriptorSet.WriteCombinedImageSamplerDescriptor(1, imageSampler, imageView, VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL); + shaderBinding.Update({ + { + 0, + Nz::ShaderBinding::UniformBufferBinding { + uniformBuffer.get(), 0, uniformSize + } + }, + { + 1, + Nz::ShaderBinding::TextureBinding { + texture.get(), textureSampler.get() + } + } + }); Nz::RenderPipelineInfo pipelineInfo; pipelineInfo.pipelineLayout = renderPipelineLayout; @@ -237,6 +246,10 @@ int main() Nz::VulkanRenderPipeline* vkPipeline = static_cast(pipeline.get()); + Nz::VulkanRenderPipelineLayout* vkPipelineLayout = static_cast(renderPipelineLayout.get()); + + Nz::VulkanShaderBinding& vkShaderBinding = static_cast(shaderBinding); + for (Nz::UInt32 i = 0; i < imageCount; ++i) { Nz::Vk::CommandBuffer& renderCmd = renderCmds[i]; @@ -265,9 +278,9 @@ int main() renderCmd.Begin(); renderCmd.BeginRenderPass(render_pass_begin_info); - renderCmd.BindIndexBuffer(indexBufferImpl->GetBufferHandle(), 0, VK_INDEX_TYPE_UINT16); - renderCmd.BindVertexBuffer(0, vertexBufferImpl->GetBufferHandle(), 0); - renderCmd.BindDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, vkPipelineLayout->GetPipelineLayout(), 0, descriptorSet); + renderCmd.BindIndexBuffer(indexBufferImpl->GetBuffer(), 0, VK_INDEX_TYPE_UINT16); + renderCmd.BindVertexBuffer(0, vertexBufferImpl->GetBuffer(), 0); + renderCmd.BindDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, vkPipelineLayout->GetPipelineLayout(), 0, vkShaderBinding.GetDescriptorSet()); renderCmd.BindPipeline(VK_PIPELINE_BIND_POINT_GRAPHICS, vkPipeline->Get(vulkanWindow.GetRenderPass())); renderCmd.SetScissor(Nz::Recti{0, 0, int(windowSize.x), int(windowSize.y)}); renderCmd.SetViewport({0.f, 0.f, float(windowSize.x), float(windowSize.y)}, 0.f, 1.f); diff --git a/include/Nazara/Renderer.hpp b/include/Nazara/Renderer.hpp index 546ea5c55..6ce0da6dc 100644 --- a/include/Nazara/Renderer.hpp +++ b/include/Nazara/Renderer.hpp @@ -45,8 +45,8 @@ #include #include #include -#include #include +#include #include #include #include diff --git a/include/Nazara/Renderer/RenderPipelineLayout.hpp b/include/Nazara/Renderer/RenderPipelineLayout.hpp index 13d6ab668..7cbaa54de 100644 --- a/include/Nazara/Renderer/RenderPipelineLayout.hpp +++ b/include/Nazara/Renderer/RenderPipelineLayout.hpp @@ -27,11 +27,15 @@ namespace Nz std::vector bindings; }; + class ShaderBinding; + class NAZARA_RENDERER_API RenderPipelineLayout { public: RenderPipelineLayout() = default; virtual ~RenderPipelineLayout(); + + virtual ShaderBinding& AllocateShaderBinding() = 0; }; } diff --git a/include/Nazara/Renderer/Shader.hpp b/include/Nazara/Renderer/Shader.hpp deleted file mode 100644 index 06d195758..000000000 --- a/include/Nazara/Renderer/Shader.hpp +++ /dev/null @@ -1,135 +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_SHADER_HPP -#define NAZARA_SHADER_HPP - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -namespace Nz -{ - class Color; - class Shader; - class ShaderStage; - - using ShaderConstRef = ObjectRef; - using ShaderLibrary = ObjectLibrary; - using ShaderRef = ObjectRef; - - class NAZARA_RENDERER_API Shader : public RefCounted - { - friend ShaderLibrary; - friend class Renderer; - - public: - Shader(); - Shader(const Shader&) = delete; - Shader(Shader&&) = delete; - ~Shader(); - - void AttachStage(ShaderStageType stage, const ShaderStage& shaderStage); - bool AttachStageFromFile(ShaderStageType stage, const std::filesystem::path& filePath); - bool AttachStageFromSource(ShaderStageType stage, const char* source, unsigned int length); - bool AttachStageFromSource(ShaderStageType stage, const String& source); - - void Bind() const; - - bool Create(); - void Destroy(); - - ByteArray GetBinary() const; - String GetLog() const; - String GetSourceCode(ShaderStageType stage) const; - int GetUniformLocation(const String& name) const; - int GetUniformLocation(ShaderUniform shaderUniform) const; - - bool HasStage(ShaderStageType stage) const; - - bool IsBinaryRetrievable() const; - bool IsLinked() const; - bool IsValid() const; - - bool Link(); - - bool LoadFromBinary(const void* buffer, unsigned int size); - bool LoadFromBinary(const ByteArray& byteArray); - - void SendBoolean(int location, bool value) const; - void SendColor(int location, const Color& color) const; - void SendDouble(int location, double value) const; - void SendDoubleArray(int location, const double* values, unsigned int count) const; - void SendFloat(int location, float value) const; - void SendFloatArray(int location, const float* values, unsigned int count) const; - void SendInteger(int location, int value) const; - void SendIntegerArray(int location, const int* values, unsigned int count) const; - void SendMatrix(int location, const Matrix4d& matrix) const; - void SendMatrix(int location, const Matrix4f& matrix) const; - void SendVector(int location, const Vector2d& vector) const; - void SendVector(int location, const Vector2f& vector) const; - void SendVector(int location, const Vector2i& vector) const; - void SendVector(int location, const Vector3d& vector) const; - void SendVector(int location, const Vector3f& vector) const; - void SendVector(int location, const Vector3i& vector) const; - void SendVector(int location, const Vector4d& vector) const; - void SendVector(int location, const Vector4f& vector) const; - void SendVector(int location, const Vector4i& vector) const; - void SendVectorArray(int location, const Vector2d* vectors, unsigned int count) const; - void SendVectorArray(int location, const Vector2f* vectors, unsigned int count) const; - void SendVectorArray(int location, const Vector2i* vectors, unsigned int count) const; - void SendVectorArray(int location, const Vector3d* vectors, unsigned int count) const; - void SendVectorArray(int location, const Vector3f* vectors, unsigned int count) const; - void SendVectorArray(int location, const Vector3i* vectors, unsigned int count) const; - void SendVectorArray(int location, const Vector4d* vectors, unsigned int count) const; - void SendVectorArray(int location, const Vector4f* vectors, unsigned int count) const; - void SendVectorArray(int location, const Vector4i* vectors, unsigned int count) const; - - bool Validate() const; - - // Fonctions OpenGL - unsigned int GetOpenGLID() const; - - Shader& operator=(const Shader&) = delete; - Shader& operator=(Shader&&) = delete; - - static bool IsStageSupported(ShaderStageType stage); - template static ShaderRef New(Args&&... args); - - // Signals: - NazaraSignal(OnShaderDestroy, const Shader* /*shader*/); - NazaraSignal(OnShaderRelease, const Shader* /*shader*/); - NazaraSignal(OnShaderUniformInvalidated, const Shader* /*shader*/); - - private: - bool PostLinkage(); - - static bool Initialize(); - static void Uninitialize(); - - std::vector m_attachedShaders[ShaderStageType_Max+1]; - bool m_linked; - int m_uniformLocations[ShaderUniform_Max+1]; - unsigned int m_program; - - static ShaderLibrary::LibraryMap s_library; - }; -} - -#include - -#endif // NAZARA_SHADER_HPP diff --git a/include/Nazara/Renderer/ShaderBinding.hpp b/include/Nazara/Renderer/ShaderBinding.hpp new file mode 100644 index 000000000..1a2daba5e --- /dev/null +++ b/include/Nazara/Renderer/ShaderBinding.hpp @@ -0,0 +1,57 @@ +// 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_SHADERBINDING_HPP +#define NAZARA_SHADERBINDING_HPP + +#include +#include +#include + +namespace Nz +{ + class AbstractBuffer; + class Texture; + class TextureSampler; + + class NAZARA_RENDERER_API ShaderBinding + { + public: + struct Binding; + + ShaderBinding() = default; + virtual ~ShaderBinding(); + + virtual void Update(std::initializer_list bindings) = 0; + + struct TextureBinding + { + Texture* texture; + TextureSampler* sampler; + }; + + struct UniformBufferBinding + { + AbstractBuffer* buffer; + UInt64 offset; + UInt64 range; + }; + + struct Binding + { + std::size_t bindingIndex; + std::variant content; + }; + + protected: + ShaderBinding(const ShaderBinding&) = delete; + ShaderBinding(ShaderBinding&&) = default; + }; +} + +#include + +#endif // NAZARA_SHADERBINDING_HPP diff --git a/include/Nazara/Renderer/ShaderBinding.inl b/include/Nazara/Renderer/ShaderBinding.inl new file mode 100644 index 000000000..dccd89939 --- /dev/null +++ b/include/Nazara/Renderer/ShaderBinding.inl @@ -0,0 +1,12 @@ +// 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 + +#include +#include + +namespace Nz +{ +} + +#include diff --git a/include/Nazara/VulkanRenderer.hpp b/include/Nazara/VulkanRenderer.hpp index 5aa12a1c7..6cc1db9dd 100644 --- a/include/Nazara/VulkanRenderer.hpp +++ b/include/Nazara/VulkanRenderer.hpp @@ -39,6 +39,7 @@ #include #include #include +#include #include #include #include diff --git a/include/Nazara/VulkanRenderer/VulkanRenderPipelineLayout.hpp b/include/Nazara/VulkanRenderer/VulkanRenderPipelineLayout.hpp index 80c530b9e..1dc17f883 100644 --- a/include/Nazara/VulkanRenderer/VulkanRenderPipelineLayout.hpp +++ b/include/Nazara/VulkanRenderer/VulkanRenderPipelineLayout.hpp @@ -10,6 +10,7 @@ #include #include #include +#include #include #include #include @@ -25,10 +26,12 @@ namespace Nz VulkanRenderPipelineLayout() = default; ~VulkanRenderPipelineLayout() = default; - Vk::DescriptorSet AllocateDescriptorSet(); + VulkanShaderBinding& AllocateShaderBinding() 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; @@ -36,6 +39,7 @@ namespace Nz struct DescriptorPool { Vk::DescriptorPool descriptorPool; + std::vector allocatedSets; }; MovablePtr m_device; diff --git a/include/Nazara/VulkanRenderer/VulkanRenderPipelineLayout.inl b/include/Nazara/VulkanRenderer/VulkanRenderPipelineLayout.inl index 36cad5f4a..e4a9f73c4 100644 --- a/include/Nazara/VulkanRenderer/VulkanRenderPipelineLayout.inl +++ b/include/Nazara/VulkanRenderer/VulkanRenderPipelineLayout.inl @@ -7,6 +7,11 @@ namespace Nz { + inline Vk::Device* VulkanRenderPipelineLayout::GetDevice() const + { + return m_device.Get(); + } + inline const Vk::DescriptorSetLayout& VulkanRenderPipelineLayout::GetDescriptorSetLayout() const { return m_descriptorSetLayout; diff --git a/include/Nazara/VulkanRenderer/VulkanShaderBinding.hpp b/include/Nazara/VulkanRenderer/VulkanShaderBinding.hpp new file mode 100644 index 000000000..8d92b3697 --- /dev/null +++ b/include/Nazara/VulkanRenderer/VulkanShaderBinding.hpp @@ -0,0 +1,41 @@ +// 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_VULKANSHADERBINDING_HPP +#define NAZARA_VULKANRENDERER_VULKANSHADERBINDING_HPP + +#include +#include +#include + +namespace Nz +{ + class VulkanRenderPipelineLayout; + + class NAZARA_VULKANRENDERER_API VulkanShaderBinding : public ShaderBinding + { + public: + inline VulkanShaderBinding(VulkanRenderPipelineLayout& owner, Vk::DescriptorSet descriptorSet); + VulkanShaderBinding(const VulkanShaderBinding&) = default; + VulkanShaderBinding(VulkanShaderBinding&&) noexcept = default; + ~VulkanShaderBinding() = default; + + inline Vk::DescriptorSet& GetDescriptorSet(); + + void Update(std::initializer_list bindings) override; + + VulkanShaderBinding& operator=(const VulkanShaderBinding&) = delete; + VulkanShaderBinding& operator=(VulkanShaderBinding&&) = delete; + + private: + Vk::AutoDescriptorSet m_descriptorSet; + VulkanRenderPipelineLayout& m_owner; + }; +} + +#include + +#endif // NAZARA_VULKANRENDERER_VULKANSHADERBINDING_HPP diff --git a/include/Nazara/VulkanRenderer/VulkanShaderBinding.inl b/include/Nazara/VulkanRenderer/VulkanShaderBinding.inl new file mode 100644 index 000000000..4300cf788 --- /dev/null +++ b/include/Nazara/VulkanRenderer/VulkanShaderBinding.inl @@ -0,0 +1,22 @@ +// 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 +#include + +namespace Nz +{ + inline VulkanShaderBinding::VulkanShaderBinding(VulkanRenderPipelineLayout& owner, Vk::DescriptorSet descriptorSet) : + m_descriptorSet(std::move(descriptorSet)), + m_owner(owner) + { + } + + inline Vk::DescriptorSet& VulkanShaderBinding::GetDescriptorSet() + { + return m_descriptorSet; + } +} + +#include diff --git a/src/Nazara/Renderer/ShaderBinding.cpp b/src/Nazara/Renderer/ShaderBinding.cpp new file mode 100644 index 000000000..240d848c6 --- /dev/null +++ b/src/Nazara/Renderer/ShaderBinding.cpp @@ -0,0 +1,11 @@ +// Copyright (C) 2015 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 + +#include +#include + +namespace Nz +{ + ShaderBinding::~ShaderBinding() = default; +} diff --git a/src/Nazara/VulkanRenderer/VulkanRenderPipelineLayout.cpp b/src/Nazara/VulkanRenderer/VulkanRenderPipelineLayout.cpp index 8eaf5d268..3af7735fc 100644 --- a/src/Nazara/VulkanRenderer/VulkanRenderPipelineLayout.cpp +++ b/src/Nazara/VulkanRenderer/VulkanRenderPipelineLayout.cpp @@ -12,14 +12,17 @@ namespace Nz { - Vk::DescriptorSet VulkanRenderPipelineLayout::AllocateDescriptorSet() + VulkanShaderBinding& VulkanRenderPipelineLayout::AllocateShaderBinding() { // TODO: Watch descriptor set count for each pool for (auto& pool : m_descriptorPools) { + if (pool.allocatedSets.capacity() <= pool.allocatedSets.size()) + continue; + Vk::DescriptorSet descriptorSet = pool.descriptorPool.AllocateDescriptorSet(m_descriptorSetLayout); if (descriptorSet) - return descriptorSet; + return pool.allocatedSets.emplace_back(*this, std::move(descriptorSet)); } // Allocate a new descriptor pool @@ -36,9 +39,16 @@ namespace Nz DescriptorPool pool; if (!pool.descriptorPool.Create(*m_device, MaxSet, UInt32(poolSizes.size()), poolSizes.data(), VK_DESCRIPTOR_POOL_CREATE_FREE_DESCRIPTOR_SET_BIT)) - return {}; + { + //return {}; + } - return m_descriptorPools.emplace_back(std::move(pool)).descriptorPool.AllocateDescriptorSet(m_descriptorSetLayout); + pool.allocatedSets.reserve(MaxSet); + + auto& poolData = m_descriptorPools.emplace_back(std::move(pool)); + Vk::DescriptorSet descriptorSet = poolData.descriptorPool.AllocateDescriptorSet(m_descriptorSetLayout); + //if (descriptorSet) + return poolData.allocatedSets.emplace_back(*this, std::move(descriptorSet)); } bool VulkanRenderPipelineLayout::Create(Vk::Device& device, RenderPipelineLayoutInfo layoutInfo) diff --git a/src/Nazara/VulkanRenderer/VulkanShaderBinding.cpp b/src/Nazara/VulkanRenderer/VulkanShaderBinding.cpp new file mode 100644 index 000000000..4f1d4754b --- /dev/null +++ b/src/Nazara/VulkanRenderer/VulkanShaderBinding.cpp @@ -0,0 +1,70 @@ +// 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 +#include +#include +#include +#include +#include +#include +#include + +namespace Nz +{ + void VulkanShaderBinding::Update(std::initializer_list bindings) + { + StackVector bufferBinding = NazaraStackVector(VkDescriptorBufferInfo, bindings.size()); + StackVector imageBinding = NazaraStackVector(VkDescriptorImageInfo, bindings.size()); + StackVector writeOps = NazaraStackVector(VkWriteDescriptorSet, bindings.size()); + + for (const Binding& binding : bindings) + { + VkWriteDescriptorSet& writeOp = writeOps.emplace_back(); + writeOp.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET; + writeOp.dstSet = m_descriptorSet; + writeOp.dstBinding = UInt32(binding.bindingIndex); + + std::visit([&](auto&& arg) + { + using T = std::decay_t; + + if constexpr (std::is_same_v) + { + VulkanTexture& vkTexture = *static_cast(arg.texture); + VulkanTextureSampler& vkSampler = *static_cast(arg.sampler); + + VkDescriptorImageInfo& imageInfo = imageBinding.emplace_back(); + imageInfo.imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL; + imageInfo.imageView = vkTexture.GetImageView(); + imageInfo.sampler = vkSampler.GetSampler(); + + writeOp.descriptorCount = 1; + writeOp.descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER; + + writeOp.pImageInfo = &imageInfo; + } + else if constexpr (std::is_same_v) + { + VulkanBuffer& vkBuffer = *static_cast(arg.buffer); + + VkDescriptorBufferInfo& bufferInfo = bufferBinding.emplace_back(); + bufferInfo.buffer = vkBuffer.GetBuffer(); + bufferInfo.offset = arg.offset; + bufferInfo.range = arg.range; + + writeOp.descriptorCount = 1; + writeOp.descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER; + + writeOp.pBufferInfo = &bufferInfo; + } + else + static_assert(AlwaysFalse::value, "non-exhaustive visitor"); + + }, binding.content); + } + + m_owner.GetDevice()->vkUpdateDescriptorSets(*m_owner.GetDevice(), UInt32(writeOps.size()), writeOps.data(), 0U, nullptr); + } +}