OpenGL: Implement RenderPipelineLayout
This commit is contained in:
@@ -5,6 +5,7 @@
|
||||
#include <Nazara/OpenGLRenderer/OpenGLDevice.hpp>
|
||||
#include <Nazara/Renderer/CommandPool.hpp>
|
||||
#include <Nazara/OpenGLRenderer/OpenGLBuffer.hpp>
|
||||
#include <Nazara/OpenGLRenderer/OpenGLRenderPipelineLayout.hpp>
|
||||
#include <Nazara/OpenGLRenderer/OpenGLShaderStage.hpp>
|
||||
#include <Nazara/OpenGLRenderer/OpenGLTexture.hpp>
|
||||
#include <Nazara/OpenGLRenderer/Wrapper/Loader.hpp>
|
||||
@@ -58,7 +59,7 @@ namespace Nz
|
||||
|
||||
std::shared_ptr<RenderPipelineLayout> OpenGLDevice::InstantiateRenderPipelineLayout(RenderPipelineLayoutInfo pipelineLayoutInfo)
|
||||
{
|
||||
return {};
|
||||
return std::make_shared<OpenGLRenderPipelineLayout>(std::move(pipelineLayoutInfo));
|
||||
}
|
||||
|
||||
std::shared_ptr<ShaderStageImpl> OpenGLDevice::InstantiateShaderStage(ShaderStageType type, ShaderLanguage lang, const void* source, std::size_t sourceSize)
|
||||
|
||||
@@ -2,8 +2,6 @@
|
||||
// This file is part of the "Nazara Engine - OpenGL Renderer"
|
||||
// For conditions of distribution and use, see copyright notice in Config.hpp
|
||||
|
||||
#if 0
|
||||
|
||||
#include <Nazara/OpenGLRenderer/OpenGLRenderPipelineLayout.hpp>
|
||||
#include <Nazara/Core/ErrorFlags.hpp>
|
||||
#include <Nazara/Core/MemoryHelper.hpp>
|
||||
@@ -15,6 +13,29 @@
|
||||
|
||||
namespace Nz
|
||||
{
|
||||
OpenGLRenderPipelineLayout::OpenGLRenderPipelineLayout(RenderPipelineLayoutInfo layoutInfo) :
|
||||
m_textureDescriptorCount(0),
|
||||
m_uniformBufferDescriptorCount(0),
|
||||
m_layoutInfo(std::move(layoutInfo))
|
||||
{
|
||||
for (const auto& bindingInfo : m_layoutInfo.bindings)
|
||||
{
|
||||
switch (bindingInfo.type)
|
||||
{
|
||||
case ShaderBindingType::Texture:
|
||||
m_textureDescriptorCount++;
|
||||
break;
|
||||
|
||||
case ShaderBindingType::UniformBuffer:
|
||||
m_uniformBufferDescriptorCount++;
|
||||
break;
|
||||
|
||||
default:
|
||||
throw std::runtime_error(("unknown binding type 0x" + String::Number(UnderlyingCast(bindingInfo.type), 16)).ToStdString());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
OpenGLRenderPipelineLayout::~OpenGLRenderPipelineLayout()
|
||||
{
|
||||
for (auto& pool : m_descriptorPools)
|
||||
@@ -46,50 +67,15 @@ namespace Nz
|
||||
return bindingPtr;
|
||||
}
|
||||
|
||||
bool OpenGLRenderPipelineLayout::Create(Vk::Device& device, RenderPipelineLayoutInfo layoutInfo)
|
||||
{
|
||||
m_device = &device;
|
||||
m_layoutInfo = std::move(layoutInfo);
|
||||
|
||||
StackVector<VkDescriptorSetLayoutBinding> layoutBindings = NazaraStackVector(VkDescriptorSetLayoutBinding, m_layoutInfo.bindings.size());
|
||||
|
||||
for (const auto& bindingInfo : m_layoutInfo.bindings)
|
||||
{
|
||||
VkDescriptorSetLayoutBinding& layoutBinding = layoutBindings.emplace_back();
|
||||
layoutBinding.binding = bindingInfo.index;
|
||||
layoutBinding.descriptorCount = 1U;
|
||||
layoutBinding.descriptorType = ToOpenGL(bindingInfo.type);
|
||||
layoutBinding.stageFlags = ToOpenGL(bindingInfo.shaderStageFlags);
|
||||
}
|
||||
|
||||
if (!m_descriptorSetLayout.Create(*m_device, UInt32(layoutBindings.size()), layoutBindings.data()))
|
||||
return false;
|
||||
|
||||
if (!m_pipelineLayout.Create(*m_device, m_descriptorSetLayout))
|
||||
return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
auto OpenGLRenderPipelineLayout::AllocatePool() -> DescriptorPool&
|
||||
{
|
||||
StackVector<VkDescriptorPoolSize> poolSizes = NazaraStackVector(VkDescriptorPoolSize, m_layoutInfo.bindings.size());
|
||||
|
||||
constexpr UInt32 MaxSet = 128;
|
||||
|
||||
for (const auto& bindingInfo : m_layoutInfo.bindings)
|
||||
{
|
||||
VkDescriptorPoolSize& poolSize = poolSizes.emplace_back();
|
||||
poolSize.descriptorCount = MaxSet;
|
||||
poolSize.type = ToOpenGL(bindingInfo.type);
|
||||
}
|
||||
|
||||
DescriptorPool pool;
|
||||
if (!pool.descriptorPool.Create(*m_device, MaxSet, UInt32(poolSizes.size()), poolSizes.data(), VK_DESCRIPTOR_POOL_CREATE_FREE_DESCRIPTOR_SET_BIT))
|
||||
throw std::runtime_error("Failed to allocate new descriptor pool: " + TranslateOpenGLError(pool.descriptorPool.GetLastErrorCode()));
|
||||
|
||||
pool.freeBindings.Resize(MaxSet, true);
|
||||
pool.storage = std::make_unique<DescriptorPool::BindingStorage[]>(MaxSet);
|
||||
pool.textureDescriptor.resize(m_textureDescriptorCount * MaxSet);
|
||||
pool.uniformBufferDescriptor.resize(m_uniformBufferDescriptorCount * MaxSet);
|
||||
|
||||
return m_descriptorPools.emplace_back(std::move(pool));
|
||||
}
|
||||
@@ -102,17 +88,30 @@ namespace Nz
|
||||
if (freeBindingId == pool.freeBindings.npos)
|
||||
return {}; //< No free binding in this pool
|
||||
|
||||
Vk::DescriptorSet descriptorSet = pool.descriptorPool.AllocateDescriptorSet(m_descriptorSetLayout);
|
||||
if (!descriptorSet)
|
||||
{
|
||||
NazaraWarning("Failed to allocate descriptor set: " + TranslateOpenGLError(pool.descriptorPool.GetLastErrorCode()));
|
||||
return {};
|
||||
}
|
||||
|
||||
pool.freeBindings.Reset(freeBindingId);
|
||||
|
||||
OpenGLShaderBinding* freeBindingMemory = reinterpret_cast<OpenGLShaderBinding*>(&pool.storage[freeBindingId]);
|
||||
return ShaderBindingPtr(PlacementNew(freeBindingMemory, *this, poolIndex, freeBindingId, std::move(descriptorSet)));
|
||||
return ShaderBindingPtr(PlacementNew(freeBindingMemory, *this, poolIndex, freeBindingId));
|
||||
}
|
||||
|
||||
auto OpenGLRenderPipelineLayout::GetTextureDescriptor(std::size_t poolIndex, std::size_t bindingIndex, std::size_t textureIndex) -> TextureDescriptor&
|
||||
{
|
||||
assert(poolIndex < m_descriptorPools.size());
|
||||
auto& pool = m_descriptorPools[poolIndex];
|
||||
assert(!pool.freeBindings.Test(bindingIndex));
|
||||
assert(textureIndex < m_textureDescriptorCount);
|
||||
|
||||
return pool.textureDescriptor[bindingIndex * m_textureDescriptorCount + textureIndex];
|
||||
}
|
||||
|
||||
auto OpenGLRenderPipelineLayout::GetUniformBufferDescriptor(std::size_t poolIndex, std::size_t bindingIndex, std::size_t uniformBufferIndex) -> UniformBufferDescriptor&
|
||||
{
|
||||
assert(poolIndex < m_descriptorPools.size());
|
||||
auto& pool = m_descriptorPools[poolIndex];
|
||||
assert(!pool.freeBindings.Test(bindingIndex));
|
||||
assert(uniformBufferIndex < m_uniformBufferDescriptorCount);
|
||||
|
||||
return pool.uniformBufferDescriptor[bindingIndex * m_uniformBufferDescriptorCount + uniformBufferIndex];
|
||||
}
|
||||
|
||||
void OpenGLRenderPipelineLayout::Release(ShaderBinding& binding)
|
||||
@@ -136,5 +135,3 @@ namespace Nz
|
||||
TryToShrink();
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
@@ -2,8 +2,6 @@
|
||||
// This file is part of the "Nazara Engine - OpenGL Renderer"
|
||||
// For conditions of distribution and use, see copyright notice in Config.hpp
|
||||
|
||||
#if 0
|
||||
|
||||
#include <Nazara/OpenGLRenderer/OpenGLShaderBinding.hpp>
|
||||
#include <Nazara/Core/Algorithm.hpp>
|
||||
#include <Nazara/Core/StackVector.hpp>
|
||||
@@ -17,57 +15,58 @@ namespace Nz
|
||||
{
|
||||
void OpenGLShaderBinding::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());
|
||||
const auto& layoutInfo = m_owner.GetLayoutInfo();
|
||||
|
||||
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);
|
||||
assert(binding.bindingIndex < layoutInfo.bindings.size());
|
||||
const auto& bindingDesc = layoutInfo.bindings[binding.bindingIndex];
|
||||
|
||||
std::visit([&](auto&& arg)
|
||||
std::size_t resourceIndex = 0;
|
||||
for (std::size_t i = binding.bindingIndex; i > 0; --i)
|
||||
{
|
||||
using T = std::decay_t<decltype(arg)>;
|
||||
// Use i-1 to prevent underflow in for loop
|
||||
if (layoutInfo.bindings[i - 1].type == bindingDesc.type)
|
||||
resourceIndex++;
|
||||
}
|
||||
|
||||
if constexpr (std::is_same_v<T, TextureBinding>)
|
||||
switch (bindingDesc.type)
|
||||
{
|
||||
case ShaderBindingType::Texture:
|
||||
{
|
||||
OpenGLTexture& vkTexture = *static_cast<OpenGLTexture*>(arg.texture);
|
||||
OpenGLTextureSampler& vkSampler = *static_cast<OpenGLTextureSampler*>(arg.sampler);
|
||||
if (!std::holds_alternative<TextureBinding>(binding.content))
|
||||
throw std::runtime_error("binding #" + std::to_string(binding.bindingIndex) + " is a texture but another binding type has been supplied");
|
||||
|
||||
VkDescriptorImageInfo& imageInfo = imageBinding.emplace_back();
|
||||
imageInfo.imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
|
||||
imageInfo.imageView = vkTexture.GetImageView();
|
||||
imageInfo.sampler = vkSampler.GetSampler();
|
||||
const TextureBinding& texBinding = std::get<TextureBinding>(binding.content);
|
||||
|
||||
writeOp.descriptorCount = 1;
|
||||
writeOp.descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
|
||||
OpenGLTexture& glTexture = *static_cast<OpenGLTexture*>(texBinding.texture);
|
||||
OpenGLTextureSampler& glSampler = *static_cast<OpenGLTextureSampler*>(texBinding.sampler);
|
||||
|
||||
writeOp.pImageInfo = &imageInfo;
|
||||
auto& textureDescriptor = m_owner.GetTextureDescriptor(m_poolIndex, m_bindingIndex, resourceIndex);
|
||||
textureDescriptor.sampler = glSampler.GetSampler().GetObjectId();
|
||||
textureDescriptor.texture = glTexture.GetTexture().GetObjectId();
|
||||
break;
|
||||
}
|
||||
else if constexpr (std::is_same_v<T, UniformBufferBinding>)
|
||||
|
||||
case ShaderBindingType::UniformBuffer:
|
||||
{
|
||||
OpenGLBuffer& vkBuffer = *static_cast<OpenGLBuffer*>(arg.buffer);
|
||||
if (!std::holds_alternative<UniformBufferBinding>(binding.content))
|
||||
throw std::runtime_error("binding #" + std::to_string(binding.bindingIndex) + " is an uniform buffer but another binding type has been supplied");
|
||||
|
||||
VkDescriptorBufferInfo& bufferInfo = bufferBinding.emplace_back();
|
||||
bufferInfo.buffer = vkBuffer.GetBuffer();
|
||||
bufferInfo.offset = arg.offset;
|
||||
bufferInfo.range = arg.range;
|
||||
const UniformBufferBinding& uboBinding = std::get<UniformBufferBinding>(binding.content);
|
||||
|
||||
writeOp.descriptorCount = 1;
|
||||
writeOp.descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
|
||||
OpenGLBuffer& glBuffer = *static_cast<OpenGLBuffer*>(uboBinding.buffer);
|
||||
if (glBuffer.GetType() != BufferType_Uniform)
|
||||
throw std::runtime_error("expected uniform buffer");
|
||||
|
||||
writeOp.pBufferInfo = &bufferInfo;
|
||||
auto& uboDescriptor = m_owner.GetUniformBufferDescriptor(m_poolIndex, m_bindingIndex, resourceIndex);
|
||||
uboDescriptor.buffer = glBuffer.GetBuffer().GetObjectId();
|
||||
uboDescriptor.offset = uboBinding.offset;
|
||||
uboDescriptor.size = uboBinding.range;
|
||||
break;
|
||||
}
|
||||
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);
|
||||
}
|
||||
|
||||
void OpenGLShaderBinding::Release()
|
||||
@@ -75,5 +74,3 @@ namespace Nz
|
||||
m_owner.Release(*this);
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
Reference in New Issue
Block a user