Add ShaderBinding

This commit is contained in:
Lynix 2020-03-26 21:19:46 +01:00
parent 874130efd4
commit 1dc0ed8e94
14 changed files with 265 additions and 150 deletions

View File

@ -179,9 +179,7 @@ int main()
std::shared_ptr<Nz::RenderPipelineLayout> renderPipelineLayout = device->InstantiateRenderPipelineLayout(pipelineLayoutInfo);
Nz::VulkanRenderPipelineLayout* vkPipelineLayout = static_cast<Nz::VulkanRenderPipelineLayout*>(renderPipelineLayout.get());
Nz::Vk::DescriptorSet descriptorSet = vkPipelineLayout->AllocateDescriptorSet();
Nz::ShaderBinding& shaderBinding = renderPipelineLayout->AllocateShaderBinding();
std::unique_ptr<Nz::AbstractBuffer> 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<Nz::VulkanBuffer*>(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<Nz::VulkanRenderPipeline*>(pipeline.get());
Nz::VulkanRenderPipelineLayout* vkPipelineLayout = static_cast<Nz::VulkanRenderPipelineLayout*>(renderPipelineLayout.get());
Nz::VulkanShaderBinding& vkShaderBinding = static_cast<Nz::VulkanShaderBinding&>(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);

View File

@ -45,8 +45,8 @@
#include <Nazara/Renderer/RenderWindow.hpp>
#include <Nazara/Renderer/RenderWindowImpl.hpp>
#include <Nazara/Renderer/RenderWindowParameters.hpp>
#include <Nazara/Renderer/Shader.hpp>
#include <Nazara/Renderer/ShaderAst.hpp>
#include <Nazara/Renderer/ShaderBinding.hpp>
#include <Nazara/Renderer/ShaderBuilder.hpp>
#include <Nazara/Renderer/ShaderStageImpl.hpp>
#include <Nazara/Renderer/ShaderWriter.hpp>

View File

@ -27,11 +27,15 @@ namespace Nz
std::vector<Binding> bindings;
};
class ShaderBinding;
class NAZARA_RENDERER_API RenderPipelineLayout
{
public:
RenderPipelineLayout() = default;
virtual ~RenderPipelineLayout();
virtual ShaderBinding& AllocateShaderBinding() = 0;
};
}

View File

@ -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 <Nazara/Prerequisites.hpp>
#include <Nazara/Core/ByteArray.hpp>
#include <Nazara/Core/ObjectLibrary.hpp>
#include <Nazara/Core/ObjectRef.hpp>
#include <Nazara/Core/RefCounted.hpp>
#include <Nazara/Core/Signal.hpp>
#include <Nazara/Core/String.hpp>
#include <Nazara/Math/Matrix4.hpp>
#include <Nazara/Math/Vector2.hpp>
#include <Nazara/Math/Vector3.hpp>
#include <Nazara/Math/Vector4.hpp>
#include <Nazara/Renderer/Config.hpp>
#include <Nazara/Renderer/Enums.hpp>
#include <filesystem>
namespace Nz
{
class Color;
class Shader;
class ShaderStage;
using ShaderConstRef = ObjectRef<const Shader>;
using ShaderLibrary = ObjectLibrary<Shader>;
using ShaderRef = ObjectRef<Shader>;
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<typename... Args> 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<unsigned int> m_attachedShaders[ShaderStageType_Max+1];
bool m_linked;
int m_uniformLocations[ShaderUniform_Max+1];
unsigned int m_program;
static ShaderLibrary::LibraryMap s_library;
};
}
#include <Nazara/Renderer/Shader.inl>
#endif // NAZARA_SHADER_HPP

View File

@ -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 <Nazara/Prerequisites.hpp>
#include <Nazara/Renderer/Config.hpp>
#include <variant>
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<Binding> bindings) = 0;
struct TextureBinding
{
Texture* texture;
TextureSampler* sampler;
};
struct UniformBufferBinding
{
AbstractBuffer* buffer;
UInt64 offset;
UInt64 range;
};
struct Binding
{
std::size_t bindingIndex;
std::variant<TextureBinding, UniformBufferBinding> content;
};
protected:
ShaderBinding(const ShaderBinding&) = delete;
ShaderBinding(ShaderBinding&&) = default;
};
}
#include <Nazara/Renderer/ShaderBinding.inl>
#endif // NAZARA_SHADERBINDING_HPP

View File

@ -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 <Nazara/Renderer/ShaderBinding.hpp>
#include <Nazara/Renderer/Debug.hpp>
namespace Nz
{
}
#include <Nazara/Renderer/DebugOff.hpp>

View File

@ -39,6 +39,7 @@
#include <Nazara/VulkanRenderer/VulkanRenderer.hpp>
#include <Nazara/VulkanRenderer/VulkanRenderPipeline.hpp>
#include <Nazara/VulkanRenderer/VulkanRenderPipelineLayout.hpp>
#include <Nazara/VulkanRenderer/VulkanShaderBinding.hpp>
#include <Nazara/VulkanRenderer/VulkanShaderStage.hpp>
#include <Nazara/VulkanRenderer/VulkanSurface.hpp>
#include <Nazara/VulkanRenderer/VulkanTexture.hpp>

View File

@ -10,6 +10,7 @@
#include <Nazara/Prerequisites.hpp>
#include <Nazara/Renderer/RenderPipelineLayout.hpp>
#include <Nazara/VulkanRenderer/Config.hpp>
#include <Nazara/VulkanRenderer/VulkanShaderBinding.hpp>
#include <Nazara/VulkanRenderer/Wrapper/Device.hpp>
#include <Nazara/VulkanRenderer/Wrapper/DescriptorPool.hpp>
#include <Nazara/VulkanRenderer/Wrapper/DescriptorSet.hpp>
@ -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<VulkanShaderBinding> allocatedSets;
};
MovablePtr<Vk::Device> m_device;

View File

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

View File

@ -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 <Nazara/Prerequisites.hpp>
#include <Nazara/Renderer/ShaderBinding.hpp>
#include <Nazara/VulkanRenderer/Wrapper/DescriptorSet.hpp>
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<Binding> bindings) override;
VulkanShaderBinding& operator=(const VulkanShaderBinding&) = delete;
VulkanShaderBinding& operator=(VulkanShaderBinding&&) = delete;
private:
Vk::AutoDescriptorSet m_descriptorSet;
VulkanRenderPipelineLayout& m_owner;
};
}
#include <Nazara/VulkanRenderer/VulkanShaderBinding.inl>
#endif // NAZARA_VULKANRENDERER_VULKANSHADERBINDING_HPP

View File

@ -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 <Nazara/VulkanRenderer/VulkanShaderBinding.hpp>
#include <Nazara/VulkanRenderer/Debug.hpp>
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 <Nazara/VulkanRenderer/DebugOff.hpp>

View File

@ -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 <Nazara/Renderer/ShaderBinding.hpp>
#include <Nazara/Renderer/Debug.hpp>
namespace Nz
{
ShaderBinding::~ShaderBinding() = default;
}

View File

@ -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)

View File

@ -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 <Nazara/VulkanRenderer/VulkanShaderBinding.hpp>
#include <Nazara/Core/Algorithm.hpp>
#include <Nazara/Core/StackVector.hpp>
#include <Nazara/VulkanRenderer/VulkanBuffer.hpp>
#include <Nazara/VulkanRenderer/VulkanRenderPipelineLayout.hpp>
#include <Nazara/VulkanRenderer/VulkanTexture.hpp>
#include <Nazara/VulkanRenderer/VulkanTextureSampler.hpp>
#include <Nazara/VulkanRenderer/Debug.hpp>
namespace Nz
{
void VulkanShaderBinding::Update(std::initializer_list<Binding> bindings)
{
StackVector<VkDescriptorBufferInfo> bufferBinding = NazaraStackVector(VkDescriptorBufferInfo, bindings.size());
StackVector<VkDescriptorImageInfo> imageBinding = NazaraStackVector(VkDescriptorImageInfo, bindings.size());
StackVector<VkWriteDescriptorSet> 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<decltype(arg)>;
if constexpr (std::is_same_v<T, TextureBinding>)
{
VulkanTexture& vkTexture = *static_cast<VulkanTexture*>(arg.texture);
VulkanTextureSampler& vkSampler = *static_cast<VulkanTextureSampler*>(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<T, UniformBufferBinding>)
{
VulkanBuffer& vkBuffer = *static_cast<VulkanBuffer*>(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<T>::value, "non-exhaustive visitor");
}, binding.content);
}
m_owner.GetDevice()->vkUpdateDescriptorSets(*m_owner.GetDevice(), UInt32(writeOps.size()), writeOps.data(), 0U, nullptr);
}
}