parent
705d6fa119
commit
d1c32ce089
10 changed files with 194 additions and 167 deletions
@ -0,0 +1,26 @@ |
||||
#pragma once |
||||
|
||||
#include <vulkan/vulkan_core.h> |
||||
#include <vector> |
||||
#include <map> |
||||
|
||||
class Buffer; |
||||
|
||||
enum class DescriptorSet { |
||||
WORLD = 0, |
||||
MESH = 1 |
||||
}; |
||||
|
||||
class DescriptorPool { |
||||
public: |
||||
DescriptorPool(); |
||||
~DescriptorPool(); |
||||
|
||||
void bindBuffer(Buffer* buffer, VkDescriptorType type, DescriptorSet set, uint32_t binding); |
||||
std::map<DescriptorSet, VkDescriptorSet> sets; |
||||
std::map<DescriptorSet, VkDescriptorSetLayout> setLayouts; |
||||
private: |
||||
VkDescriptorPool handle = VK_NULL_HANDLE; |
||||
|
||||
void createLayout(DescriptorSet set, const std::vector<VkDescriptorSetLayoutBinding> &bindings); |
||||
}; |
@ -0,0 +1,81 @@ |
||||
#include "vulkan/descriptor_pool.hpp" |
||||
#include "vulkan/instance.hpp" |
||||
#include "vulkan/buffer.hpp" |
||||
|
||||
|
||||
DescriptorPool::DescriptorPool() { |
||||
VkDescriptorPoolSize poolSizes[] = { |
||||
{VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, 20}, |
||||
{VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, 20} |
||||
}; |
||||
|
||||
VkDescriptorPoolCreateInfo poolInfo {}; |
||||
poolInfo.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO; |
||||
poolInfo.pPoolSizes = poolSizes; |
||||
poolInfo.poolSizeCount = sizeof(poolSizes) / sizeof(VkDescriptorPoolSize); |
||||
poolInfo.maxSets = 10; |
||||
|
||||
vkCreateDescriptorPool(Instance::instance->device, &poolInfo, nullptr, &handle); |
||||
|
||||
std::map<DescriptorSet, std::vector<VkDescriptorSetLayoutBinding>> setBindings; |
||||
setBindings[DescriptorSet::WORLD].push_back({ |
||||
.binding = 0, |
||||
.descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, |
||||
.descriptorCount = 1, |
||||
.stageFlags = VK_SHADER_STAGE_VERTEX_BIT |
||||
}); |
||||
for (uint32_t i = 0; i < 5; i++){ |
||||
setBindings[DescriptorSet::MESH].push_back({ |
||||
.binding = i, |
||||
.descriptorType = VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, |
||||
.descriptorCount = 1, |
||||
.stageFlags = VK_SHADER_STAGE_COMPUTE_BIT |
||||
}); |
||||
} |
||||
|
||||
for (const auto &[set, bindings] : setBindings) |
||||
createLayout(set, bindings); |
||||
|
||||
for (const auto &[set, layout] : setLayouts){ |
||||
VkDescriptorSetAllocateInfo allocateInfo {}; |
||||
allocateInfo.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO; |
||||
allocateInfo.descriptorPool = handle; |
||||
allocateInfo.descriptorSetCount = 1; |
||||
allocateInfo.pSetLayouts = &layout; |
||||
|
||||
vkAllocateDescriptorSets(Instance::instance->device, &allocateInfo, &sets[set]); |
||||
} |
||||
} |
||||
|
||||
DescriptorPool::~DescriptorPool() { |
||||
vkDestroyDescriptorPool(Instance::instance->device, handle, nullptr); |
||||
for (const auto &[type, layout] : setLayouts) |
||||
vkDestroyDescriptorSetLayout(Instance::instance->device, layout, nullptr); |
||||
} |
||||
|
||||
void DescriptorPool::bindBuffer(Buffer *buffer, VkDescriptorType type, DescriptorSet set, uint32_t binding) { |
||||
VkDescriptorBufferInfo bufferInfo {}; |
||||
bufferInfo.buffer = buffer->handle; |
||||
bufferInfo.offset = 0; |
||||
bufferInfo.range = buffer->size; |
||||
|
||||
VkWriteDescriptorSet descriptorWrite {}; |
||||
descriptorWrite.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET; |
||||
descriptorWrite.dstSet = sets[set]; |
||||
descriptorWrite.dstBinding = binding; |
||||
descriptorWrite.dstArrayElement = 0; |
||||
descriptorWrite.descriptorType = type; |
||||
descriptorWrite.descriptorCount = 1; |
||||
descriptorWrite.pBufferInfo = &bufferInfo; |
||||
|
||||
vkUpdateDescriptorSets(Instance::instance->device, 1, &descriptorWrite, 0, nullptr); |
||||
} |
||||
|
||||
void DescriptorPool::createLayout(DescriptorSet set, const std::vector<VkDescriptorSetLayoutBinding> &bindings) { |
||||
VkDescriptorSetLayoutCreateInfo layoutCreateInfo {}; |
||||
layoutCreateInfo.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO; |
||||
layoutCreateInfo.bindingCount = bindings.size(); |
||||
layoutCreateInfo.pBindings = bindings.data(); |
||||
|
||||
vkCreateDescriptorSetLayout(Instance::instance->device, &layoutCreateInfo, nullptr, &setLayouts[set]); |
||||
} |
Loading…
Reference in new issue