mirror of
https://github.com/seekrs/MacroLibX.git
synced 2026-01-13 15:43:34 +00:00
adding descriptors support to renderer
This commit is contained in:
34
src/renderer/descriptors/vk_descriptor_pool.cpp
git.filemode.normal_file
34
src/renderer/descriptors/vk_descriptor_pool.cpp
git.filemode.normal_file
@@ -0,0 +1,34 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* vk_descriptor_pool.cpp :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/01/23 18:34:23 by maldavid #+# #+# */
|
||||
/* Updated: 2023/01/23 18:44:51 by maldavid ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "vk_descriptor_pool.h"
|
||||
#include <renderer/core/render_core.h>
|
||||
|
||||
namespace mlx
|
||||
{
|
||||
void DescriptorPool::init(std::size_t n, VkDescriptorPoolSize* size)
|
||||
{
|
||||
VkDescriptorPoolCreateInfo poolInfo{};
|
||||
poolInfo.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO;
|
||||
poolInfo.poolSizeCount = n;
|
||||
poolInfo.pPoolSizes = size;
|
||||
poolInfo.maxSets = 8192;
|
||||
|
||||
if(vkCreateDescriptorPool(Render_Core::get().getDevice().get(), &poolInfo, nullptr, &_pool) != VK_SUCCESS)
|
||||
core::error::report(e_kind::fatal_error, "Vulkan : failed to create descriptor pool");
|
||||
}
|
||||
|
||||
void DescriptorPool::destroy() noexcept
|
||||
{
|
||||
vkDestroyDescriptorPool(Render_Core::get().getDevice().get(), _pool, nullptr);
|
||||
}
|
||||
}
|
||||
35
src/renderer/descriptors/vk_descriptor_pool.h
git.filemode.normal_file
35
src/renderer/descriptors/vk_descriptor_pool.h
git.filemode.normal_file
@@ -0,0 +1,35 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* vk_descriptor_pool.h :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/01/23 18:32:43 by maldavid #+# #+# */
|
||||
/* Updated: 2023/01/23 18:44:40 by maldavid ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#ifndef __VK_DESCRIPTOR_POOL__
|
||||
#define __VK_DESCRIPTOR_POOL__
|
||||
|
||||
#include <volk.h>
|
||||
#include <cstddef>
|
||||
|
||||
namespace mlx
|
||||
{
|
||||
class DescriptorPool
|
||||
{
|
||||
public:
|
||||
void init(std::size_t n, VkDescriptorPoolSize* size);
|
||||
void destroy() noexcept;
|
||||
|
||||
inline VkDescriptorPool& operator()() noexcept { return _pool; }
|
||||
inline VkDescriptorPool& get() noexcept { return _pool; }
|
||||
|
||||
private:
|
||||
VkDescriptorPool _pool = VK_NULL_HANDLE;
|
||||
};
|
||||
}
|
||||
|
||||
#endif
|
||||
74
src/renderer/descriptors/vk_descriptor_set.cpp
git.filemode.normal_file
74
src/renderer/descriptors/vk_descriptor_set.cpp
git.filemode.normal_file
@@ -0,0 +1,74 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* vk_descriptor_set.cpp :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/01/23 18:40:44 by maldavid #+# #+# */
|
||||
/* Updated: 2023/01/23 18:54:28 by maldavid ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "vk_descriptor_set.h"
|
||||
#include "vk_descriptor_pool.h"
|
||||
#include "vk_descriptor_set_layout.h"
|
||||
#include <renderer/buffers/vk_ubo.h>
|
||||
#include <renderer/renderer.h>
|
||||
|
||||
namespace mlx
|
||||
{
|
||||
void DescriptorSet::init(Renderer* renderer, UBO* ubo, DescriptorSetLayout& layout, DescriptorPool& pool)
|
||||
{
|
||||
_renderer = renderer;
|
||||
|
||||
auto device = Render_Core::get().getDevice().get();
|
||||
|
||||
_pool = pool.get();
|
||||
|
||||
std::array<VkDescriptorSetLayout, MAX_FRAMES_IN_FLIGHT> layouts;
|
||||
layouts.fill(layout.get());
|
||||
|
||||
VkDescriptorSetAllocateInfo allocInfo{};
|
||||
allocInfo.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO;
|
||||
allocInfo.descriptorPool = _pool;
|
||||
allocInfo.descriptorSetCount = MAX_FRAMES_IN_FLIGHT;
|
||||
allocInfo.pSetLayouts = layouts.data();
|
||||
|
||||
if(vkAllocateDescriptorSets(device, &allocInfo, _desc_set.data()) != VK_SUCCESS)
|
||||
core::error::report(e_kind::fatal_error, "Vulkan : failed to allocate descriptor set");
|
||||
|
||||
for(int i = 0; i < MAX_FRAMES_IN_FLIGHT; i++)
|
||||
{
|
||||
VkDescriptorBufferInfo bufferInfo{};
|
||||
bufferInfo.buffer = ubo->get(i);
|
||||
bufferInfo.offset = ubo->getOffset(i);
|
||||
bufferInfo.range = ubo->getSize(i);
|
||||
|
||||
VkWriteDescriptorSet descriptorWrite{};
|
||||
descriptorWrite.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
|
||||
descriptorWrite.dstSet = _desc_set[i];
|
||||
descriptorWrite.dstBinding = 0;
|
||||
descriptorWrite.dstArrayElement = 0;
|
||||
descriptorWrite.descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
|
||||
descriptorWrite.descriptorCount = 1;
|
||||
descriptorWrite.pBufferInfo = &bufferInfo;
|
||||
|
||||
vkUpdateDescriptorSets(device, 1, &descriptorWrite, 0, nullptr);
|
||||
}
|
||||
}
|
||||
|
||||
VkDescriptorSet& DescriptorSet::operator()() noexcept
|
||||
{
|
||||
return _desc_set[_renderer->getActiveImageIndex()];
|
||||
}
|
||||
VkDescriptorSet& DescriptorSet::get() noexcept
|
||||
{
|
||||
return _desc_set[_renderer->getActiveImageIndex()];
|
||||
}
|
||||
|
||||
void DescriptorSet::destroy() noexcept
|
||||
{
|
||||
vkFreeDescriptorSets(Render_Core::get().getDevice().get(), _pool, MAX_FRAMES_IN_FLIGHT, _desc_set.data());
|
||||
}
|
||||
}
|
||||
38
src/renderer/descriptors/vk_descriptor_set.h
git.filemode.normal_file
38
src/renderer/descriptors/vk_descriptor_set.h
git.filemode.normal_file
@@ -0,0 +1,38 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* vk_descriptor_set.h :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/01/23 18:39:36 by maldavid #+# #+# */
|
||||
/* Updated: 2023/01/23 18:54:22 by maldavid ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#ifndef __VK_DESCRIPTOR_SET__
|
||||
#define __VK_DESCRIPTOR_SET__
|
||||
|
||||
#include <volk.h>
|
||||
#include <array>
|
||||
#include <renderer/core/render_core.h>
|
||||
|
||||
namespace mlx
|
||||
{
|
||||
class DescriptorSet
|
||||
{
|
||||
public:
|
||||
void init(class Renderer* renderer, class UBO* ubo, class DescriptorSetLayout& layout, class DescriptorPool& pool);
|
||||
void destroy() noexcept;
|
||||
|
||||
VkDescriptorSet& operator()() noexcept;
|
||||
VkDescriptorSet& get() noexcept;
|
||||
|
||||
private:
|
||||
std::array<VkDescriptorSet, MAX_FRAMES_IN_FLIGHT> _desc_set;
|
||||
VkDescriptorPool _pool = VK_NULL_HANDLE;
|
||||
class Renderer* _renderer = nullptr;
|
||||
};
|
||||
}
|
||||
|
||||
#endif
|
||||
40
src/renderer/descriptors/vk_descriptor_set_layout.cpp
git.filemode.normal_file
40
src/renderer/descriptors/vk_descriptor_set_layout.cpp
git.filemode.normal_file
@@ -0,0 +1,40 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* vk_descriptor_set_layout.cpp :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/01/23 18:37:28 by maldavid #+# #+# */
|
||||
/* Updated: 2023/01/23 18:45:32 by maldavid ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "vk_descriptor_set_layout.h"
|
||||
#include <renderer/core/render_core.h>
|
||||
|
||||
namespace mlx
|
||||
{
|
||||
void DescriptorSetLayout::init(VkDescriptorType t, std::size_t n, int binding, VkShaderStageFlagBits stage)
|
||||
{
|
||||
VkDescriptorSetLayoutBinding bindings{};
|
||||
bindings.binding = binding;
|
||||
bindings.descriptorCount = 1;
|
||||
bindings.descriptorType = t;
|
||||
bindings.pImmutableSamplers = nullptr;
|
||||
bindings.stageFlags = stage;
|
||||
|
||||
VkDescriptorSetLayoutCreateInfo layoutInfo{};
|
||||
layoutInfo.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO;
|
||||
layoutInfo.bindingCount = n;
|
||||
layoutInfo.pBindings = &bindings;
|
||||
|
||||
if(vkCreateDescriptorSetLayout(Render_Core::get().getDevice().get(), &layoutInfo, nullptr, &_layout) != VK_SUCCESS)
|
||||
core::error::report(e_kind::fatal_error, "Vulkan : failed to create descriptor set layout");
|
||||
}
|
||||
|
||||
void DescriptorSetLayout::destroy() noexcept
|
||||
{
|
||||
vkDestroyDescriptorSetLayout(Render_Core::get().getDevice().get(), _layout, nullptr);
|
||||
}
|
||||
}
|
||||
35
src/renderer/descriptors/vk_descriptor_set_layout.h
git.filemode.normal_file
35
src/renderer/descriptors/vk_descriptor_set_layout.h
git.filemode.normal_file
@@ -0,0 +1,35 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* vk_descriptor_set_layout.h :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/01/23 18:36:22 by maldavid #+# #+# */
|
||||
/* Updated: 2023/01/23 18:45:44 by maldavid ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#ifndef __VK_DESCRIPTOR_SET_LAYOUT__
|
||||
#define __VK_DESCRIPTOR_SET_LAYOUT__
|
||||
|
||||
#include <volk.h>
|
||||
#include <cstddef>
|
||||
|
||||
namespace mlx
|
||||
{
|
||||
class DescriptorSetLayout
|
||||
{
|
||||
public:
|
||||
void init(VkDescriptorType t = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, std::size_t n = 1, int binding = 0, VkShaderStageFlagBits stage = VK_SHADER_STAGE_VERTEX_BIT);
|
||||
void destroy() noexcept;
|
||||
|
||||
inline VkDescriptorSetLayout& operator()() noexcept { return _layout; }
|
||||
inline VkDescriptorSetLayout& get() noexcept { return _layout; }
|
||||
|
||||
private:
|
||||
VkDescriptorSetLayout _layout = VK_NULL_HANDLE;
|
||||
};
|
||||
}
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user