2023-10-26 18:37:29 +02:00
|
|
|
#include "DescriptorSets.h"
|
|
|
|
|
#include "Graphics.h"
|
|
|
|
|
#include "Initializer.h"
|
|
|
|
|
#include "CommandBuffer.h"
|
2023-11-05 10:36:01 +01:00
|
|
|
#include "Texture.h"
|
2020-03-09 12:30:07 +01:00
|
|
|
|
2020-03-20 01:27:40 +01:00
|
|
|
using namespace Seele;
|
|
|
|
|
using namespace Seele::Vulkan;
|
|
|
|
|
|
2021-05-06 17:02:10 +02:00
|
|
|
DescriptorLayout::DescriptorLayout(PGraphics graphics, const std::string& name)
|
2021-12-27 15:04:53 +01:00
|
|
|
: Gfx::DescriptorLayout(name)
|
|
|
|
|
, graphics(graphics)
|
|
|
|
|
, layoutHandle(VK_NULL_HANDLE)
|
2020-10-03 11:00:10 +02:00
|
|
|
{
|
|
|
|
|
}
|
2020-03-20 01:27:40 +01:00
|
|
|
DescriptorLayout::~DescriptorLayout()
|
2020-03-09 12:30:07 +01:00
|
|
|
{
|
2021-12-27 15:04:53 +01:00
|
|
|
if (layoutHandle != VK_NULL_HANDLE)
|
|
|
|
|
{
|
|
|
|
|
vkDestroyDescriptorSetLayout(graphics->getDevice(), layoutHandle, nullptr);
|
|
|
|
|
}
|
2020-03-09 12:30:07 +01:00
|
|
|
}
|
|
|
|
|
|
2020-03-20 01:27:40 +01:00
|
|
|
void DescriptorLayout::create()
|
2020-03-09 12:30:07 +01:00
|
|
|
{
|
2021-12-27 15:04:53 +01:00
|
|
|
if (layoutHandle != VK_NULL_HANDLE)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
bindings.resize(descriptorBindings.size());
|
2022-04-15 11:19:30 +02:00
|
|
|
Array<VkDescriptorBindingFlags> bindingFlags(descriptorBindings.size());
|
2021-12-27 15:04:53 +01:00
|
|
|
for (size_t i = 0; i < descriptorBindings.size(); ++i)
|
|
|
|
|
{
|
|
|
|
|
VkDescriptorSetLayoutBinding &binding = bindings[i];
|
|
|
|
|
const Gfx::DescriptorBinding &rhiBinding = descriptorBindings[i];
|
|
|
|
|
binding.binding = rhiBinding.binding;
|
|
|
|
|
binding.descriptorCount = rhiBinding.descriptorCount;
|
|
|
|
|
binding.descriptorType = cast(rhiBinding.descriptorType);
|
|
|
|
|
binding.stageFlags = rhiBinding.shaderStages;
|
|
|
|
|
binding.pImmutableSamplers = nullptr;
|
2022-04-15 11:19:30 +02:00
|
|
|
bindingFlags[i] = rhiBinding.bindingFlags;
|
2021-12-27 15:04:53 +01:00
|
|
|
}
|
|
|
|
|
VkDescriptorSetLayoutCreateInfo createInfo =
|
|
|
|
|
init::DescriptorSetLayoutCreateInfo(bindings.data(), (uint32)bindings.size());
|
2022-04-15 11:19:30 +02:00
|
|
|
VkDescriptorSetLayoutBindingFlagsCreateInfo bindingFlagsInfo = {};
|
|
|
|
|
bindingFlagsInfo.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_BINDING_FLAGS_CREATE_INFO;
|
|
|
|
|
bindingFlagsInfo.pNext = nullptr;
|
|
|
|
|
bindingFlagsInfo.bindingCount = static_cast<uint32>(bindingFlags.size());
|
|
|
|
|
bindingFlagsInfo.pBindingFlags = bindingFlags.data();
|
|
|
|
|
createInfo.pNext = &bindingFlagsInfo;
|
2021-12-27 15:04:53 +01:00
|
|
|
VK_CHECK(vkCreateDescriptorSetLayout(graphics->getDevice(), &createInfo, nullptr, &layoutHandle));
|
2020-03-09 12:30:07 +01:00
|
|
|
|
2021-12-27 15:04:53 +01:00
|
|
|
allocator = new DescriptorAllocator(graphics, *this);
|
2020-10-03 11:00:10 +02:00
|
|
|
|
2022-11-22 22:11:37 +01:00
|
|
|
hash = CRC::Calculate(bindings.data(), sizeof(VkDescriptorSetLayoutBinding) * bindings.size(), CRC::CRC_32());
|
2020-03-09 12:30:07 +01:00
|
|
|
}
|
|
|
|
|
|
2020-03-20 01:27:40 +01:00
|
|
|
PipelineLayout::~PipelineLayout()
|
2020-03-13 12:44:33 +01:00
|
|
|
{
|
2021-12-27 15:04:53 +01:00
|
|
|
if (layoutHandle != VK_NULL_HANDLE)
|
|
|
|
|
{
|
2022-01-12 14:40:26 +01:00
|
|
|
//vkDestroyPipelineLayout(graphics->getDevice(), layoutHandle, nullptr);
|
2021-12-27 15:04:53 +01:00
|
|
|
}
|
2020-03-13 12:44:33 +01:00
|
|
|
}
|
|
|
|
|
|
2020-10-03 11:00:10 +02:00
|
|
|
static Map<uint32, VkPipelineLayout> layoutCache;
|
|
|
|
|
|
2020-03-20 01:27:40 +01:00
|
|
|
void PipelineLayout::create()
|
2020-03-09 12:30:07 +01:00
|
|
|
{
|
2021-12-27 15:04:53 +01:00
|
|
|
vulkanDescriptorLayouts.resize(descriptorSetLayouts.size());
|
|
|
|
|
for (size_t i = 0; i < descriptorSetLayouts.size(); ++i)
|
|
|
|
|
{
|
|
|
|
|
// There could be unused descriptor set indices
|
|
|
|
|
if(descriptorSetLayouts[i] == nullptr)
|
|
|
|
|
{
|
|
|
|
|
vulkanDescriptorLayouts[i] = VK_NULL_HANDLE;
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
PDescriptorLayout layout = descriptorSetLayouts[i].cast<DescriptorLayout>();
|
|
|
|
|
layout->create();
|
|
|
|
|
vulkanDescriptorLayouts[i] = layout->getHandle();
|
|
|
|
|
}
|
2020-10-03 11:00:10 +02:00
|
|
|
|
2021-12-27 15:04:53 +01:00
|
|
|
VkPipelineLayoutCreateInfo createInfo =
|
|
|
|
|
init::PipelineLayoutCreateInfo(vulkanDescriptorLayouts.data(), (uint32)vulkanDescriptorLayouts.size());
|
|
|
|
|
Array<VkPushConstantRange> vkPushConstants(pushConstants.size());
|
|
|
|
|
for (size_t i = 0; i < pushConstants.size(); i++)
|
|
|
|
|
{
|
|
|
|
|
vkPushConstants[i].offset = pushConstants[i].offset;
|
|
|
|
|
vkPushConstants[i].size = pushConstants[i].size;
|
|
|
|
|
vkPushConstants[i].stageFlags = (VkShaderStageFlagBits)pushConstants[i].stageFlags;
|
|
|
|
|
}
|
|
|
|
|
createInfo.pushConstantRangeCount = (uint32)vkPushConstants.size();
|
|
|
|
|
createInfo.pPushConstantRanges = vkPushConstants.data();
|
2020-03-13 12:44:33 +01:00
|
|
|
|
2022-11-22 22:11:37 +01:00
|
|
|
layoutHash = CRC::Calculate(createInfo.pPushConstantRanges, sizeof(VkPushConstantRange) * createInfo.pushConstantRangeCount, CRC::CRC_32());
|
|
|
|
|
layoutHash = CRC::Calculate(createInfo.pSetLayouts, sizeof(VkDescriptorSetLayout) * createInfo.setLayoutCount, CRC::CRC_32(), layoutHash);
|
2020-10-03 11:00:10 +02:00
|
|
|
|
2021-12-27 15:04:53 +01:00
|
|
|
if(layoutCache[layoutHash] != VK_NULL_HANDLE)
|
|
|
|
|
{
|
|
|
|
|
layoutHandle = layoutCache[layoutHash];
|
|
|
|
|
return;
|
|
|
|
|
}
|
2020-10-03 11:00:10 +02:00
|
|
|
|
2021-12-27 15:04:53 +01:00
|
|
|
VK_CHECK(vkCreatePipelineLayout(graphics->getDevice(), &createInfo, nullptr, &layoutHandle));
|
|
|
|
|
layoutCache[layoutHash] = layoutHandle;
|
2020-03-13 12:44:33 +01:00
|
|
|
}
|
|
|
|
|
|
2020-06-02 11:46:18 +02:00
|
|
|
void PipelineLayout::reset()
|
|
|
|
|
{
|
2021-12-27 15:04:53 +01:00
|
|
|
vkDestroyPipelineLayout(graphics->getDevice(), layoutHandle, nullptr);
|
|
|
|
|
descriptorSetLayouts.clear();
|
|
|
|
|
pushConstants.clear();
|
2020-06-02 11:46:18 +02:00
|
|
|
}
|
|
|
|
|
|
2020-03-20 01:27:40 +01:00
|
|
|
DescriptorSet::~DescriptorSet()
|
2020-03-13 12:44:33 +01:00
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-20 01:27:40 +01:00
|
|
|
void DescriptorSet::updateBuffer(uint32_t binding, Gfx::PUniformBuffer uniformBuffer)
|
2020-03-13 12:44:33 +01:00
|
|
|
{
|
2021-12-27 15:04:53 +01:00
|
|
|
PUniformBuffer vulkanBuffer = uniformBuffer.cast<UniformBuffer>();
|
|
|
|
|
UniformBuffer* cachedBuffer = reinterpret_cast<UniformBuffer*>(cachedData[binding]);
|
|
|
|
|
if(vulkanBuffer->isDataEquals(cachedBuffer))
|
|
|
|
|
{
|
2022-04-18 18:08:38 +02:00
|
|
|
//std::cout << "uniform data equal, skip" << std::endl;
|
2021-12-27 15:04:53 +01:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
bufferInfos.add(init::DescriptorBufferInfo(vulkanBuffer->getHandle(), 0, vulkanBuffer->getSize()));
|
2020-03-13 12:44:33 +01:00
|
|
|
|
2021-12-27 15:04:53 +01:00
|
|
|
VkWriteDescriptorSet writeDescriptor = init::WriteDescriptorSet(setHandle, VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, binding, &bufferInfos.back());
|
|
|
|
|
writeDescriptors.add(writeDescriptor);
|
2020-10-03 11:00:10 +02:00
|
|
|
|
2023-11-05 10:36:01 +01:00
|
|
|
cachedData[binding] = vulkanBuffer.getHandle();
|
2020-03-13 12:44:33 +01:00
|
|
|
}
|
|
|
|
|
|
2023-11-05 10:36:01 +01:00
|
|
|
void DescriptorSet::updateBuffer(uint32_t binding, Gfx::PShaderBuffer shaderBuffer)
|
2020-03-13 12:44:33 +01:00
|
|
|
{
|
2023-11-05 10:36:01 +01:00
|
|
|
PShaderBuffer vulkanBuffer = shaderBuffer.cast<ShaderBuffer>();
|
2023-08-28 21:23:13 +02:00
|
|
|
ShaderBuffer* cachedBuffer = reinterpret_cast<ShaderBuffer*>(cachedData[binding]);
|
2021-12-27 15:04:53 +01:00
|
|
|
if(vulkanBuffer.getHandle() == cachedBuffer)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
bufferInfos.add(init::DescriptorBufferInfo(vulkanBuffer->getHandle(), 0, vulkanBuffer->getSize()));
|
2020-03-13 12:44:33 +01:00
|
|
|
|
2021-12-27 15:04:53 +01:00
|
|
|
VkWriteDescriptorSet writeDescriptor = init::WriteDescriptorSet(setHandle, VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, binding, &bufferInfos.back());
|
|
|
|
|
writeDescriptors.add(writeDescriptor);
|
2020-10-03 11:00:10 +02:00
|
|
|
|
2021-12-27 15:04:53 +01:00
|
|
|
cachedData[binding] = vulkanBuffer.getHandle();
|
2020-03-13 12:44:33 +01:00
|
|
|
}
|
|
|
|
|
|
2020-03-20 01:27:40 +01:00
|
|
|
void DescriptorSet::updateSampler(uint32_t binding, Gfx::PSamplerState samplerState)
|
2020-03-13 12:44:33 +01:00
|
|
|
{
|
2021-12-27 15:04:53 +01:00
|
|
|
PSamplerState vulkanSampler = samplerState.cast<SamplerState>();
|
|
|
|
|
SamplerState* cachedSampler = reinterpret_cast<SamplerState*>(cachedData[binding]);
|
|
|
|
|
if(vulkanSampler.getHandle() == cachedSampler)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
VkDescriptorImageInfo imageInfo =
|
|
|
|
|
init::DescriptorImageInfo(
|
|
|
|
|
vulkanSampler->sampler,
|
|
|
|
|
VK_NULL_HANDLE,
|
|
|
|
|
VK_IMAGE_LAYOUT_UNDEFINED);
|
|
|
|
|
imageInfos.add(imageInfo);
|
2020-03-13 12:44:33 +01:00
|
|
|
|
2021-12-27 15:04:53 +01:00
|
|
|
VkWriteDescriptorSet writeDescriptor = init::WriteDescriptorSet(setHandle, VK_DESCRIPTOR_TYPE_SAMPLER, binding, &imageInfos.back());
|
|
|
|
|
writeDescriptors.add(writeDescriptor);
|
2020-10-03 11:00:10 +02:00
|
|
|
|
2021-12-27 15:04:53 +01:00
|
|
|
cachedData[binding] = vulkanSampler.getHandle();
|
2020-03-13 12:44:33 +01:00
|
|
|
}
|
|
|
|
|
|
2020-03-20 01:27:40 +01:00
|
|
|
void DescriptorSet::updateTexture(uint32_t binding, Gfx::PTexture texture, Gfx::PSamplerState samplerState)
|
2020-03-13 12:44:33 +01:00
|
|
|
{
|
2021-12-27 15:04:53 +01:00
|
|
|
TextureHandle* vulkanTexture = TextureBase::cast(texture);
|
|
|
|
|
TextureHandle* cachedTexture = reinterpret_cast<TextureHandle*>(cachedData[binding]);
|
|
|
|
|
if(vulkanTexture == cachedTexture)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
//It is assumed that the image is in the correct layout
|
|
|
|
|
VkDescriptorImageInfo imageInfo =
|
|
|
|
|
init::DescriptorImageInfo(
|
|
|
|
|
VK_NULL_HANDLE,
|
|
|
|
|
vulkanTexture->getView(),
|
|
|
|
|
cast(vulkanTexture->getLayout()));
|
|
|
|
|
if (samplerState != nullptr)
|
|
|
|
|
{
|
|
|
|
|
PSamplerState vulkanSampler = samplerState.cast<SamplerState>();
|
|
|
|
|
imageInfo.sampler = vulkanSampler->sampler;
|
|
|
|
|
}
|
|
|
|
|
imageInfos.add(imageInfo);
|
2022-04-15 11:19:30 +02:00
|
|
|
VkDescriptorType descriptorType = VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE;
|
|
|
|
|
if(samplerState != nullptr)
|
|
|
|
|
{
|
|
|
|
|
descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
|
|
|
|
|
}
|
|
|
|
|
else if (vulkanTexture->getUsage() & VK_IMAGE_USAGE_STORAGE_BIT)
|
|
|
|
|
{
|
|
|
|
|
descriptorType = VK_DESCRIPTOR_TYPE_STORAGE_IMAGE;
|
|
|
|
|
}
|
2021-12-27 15:04:53 +01:00
|
|
|
VkWriteDescriptorSet writeDescriptor =
|
|
|
|
|
init::WriteDescriptorSet(
|
|
|
|
|
setHandle,
|
2022-04-15 11:19:30 +02:00
|
|
|
descriptorType,
|
2021-12-27 15:04:53 +01:00
|
|
|
binding,
|
|
|
|
|
&imageInfos.back());
|
2022-04-15 11:19:30 +02:00
|
|
|
|
2021-12-27 15:04:53 +01:00
|
|
|
writeDescriptors.add(writeDescriptor);
|
2020-10-03 11:00:10 +02:00
|
|
|
|
2021-12-27 15:04:53 +01:00
|
|
|
cachedData[binding] = vulkanTexture;
|
2020-03-13 12:44:33 +01:00
|
|
|
}
|
2022-04-15 11:19:30 +02:00
|
|
|
void DescriptorSet::updateTextureArray(uint32_t binding, Array<Gfx::PTexture> textures)
|
|
|
|
|
{
|
|
|
|
|
// maybe make this a parameter?
|
|
|
|
|
uint32 arrayElement = 0;
|
|
|
|
|
for(const auto& gfxTexture : textures)
|
|
|
|
|
{
|
|
|
|
|
TextureHandle* vulkanTexture = TextureBase::cast(gfxTexture);
|
|
|
|
|
imageInfos.add(
|
|
|
|
|
init::DescriptorImageInfo(
|
|
|
|
|
VK_NULL_HANDLE,
|
|
|
|
|
vulkanTexture->getView(),
|
|
|
|
|
cast(vulkanTexture->getLayout())));
|
|
|
|
|
|
|
|
|
|
VkDescriptorType descriptorType = VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE;
|
|
|
|
|
if (vulkanTexture->getUsage() & VK_IMAGE_USAGE_STORAGE_BIT)
|
|
|
|
|
{
|
|
|
|
|
descriptorType = VK_DESCRIPTOR_TYPE_STORAGE_IMAGE;
|
|
|
|
|
}
|
|
|
|
|
VkWriteDescriptorSet& write = writeDescriptors.add(
|
|
|
|
|
init::WriteDescriptorSet(
|
|
|
|
|
setHandle,
|
|
|
|
|
descriptorType,
|
|
|
|
|
binding,
|
|
|
|
|
&imageInfos.back()
|
|
|
|
|
));
|
|
|
|
|
write.dstArrayElement = arrayElement++;
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-03-13 12:44:33 +01:00
|
|
|
|
2020-03-20 01:27:40 +01:00
|
|
|
bool DescriptorSet::operator<(Gfx::PDescriptorSet other)
|
2020-03-13 12:44:33 +01:00
|
|
|
{
|
2021-12-27 15:04:53 +01:00
|
|
|
PDescriptorSet otherSet = other.cast<DescriptorSet>();
|
|
|
|
|
return this < otherSet.getHandle();
|
2020-03-13 12:44:33 +01:00
|
|
|
}
|
|
|
|
|
|
2021-04-13 23:09:16 +02:00
|
|
|
uint32 DescriptorSet::getSetIndex() const
|
|
|
|
|
{
|
2021-12-27 15:04:53 +01:00
|
|
|
return owner->getLayout().getSetIndex();
|
2021-04-13 23:09:16 +02:00
|
|
|
}
|
|
|
|
|
|
2020-03-20 01:27:40 +01:00
|
|
|
void DescriptorSet::writeChanges()
|
2020-03-13 12:44:33 +01:00
|
|
|
{
|
2021-12-27 15:04:53 +01:00
|
|
|
if (writeDescriptors.size() > 0)
|
|
|
|
|
{
|
|
|
|
|
if(isCurrentlyBound())
|
|
|
|
|
{
|
|
|
|
|
std::cout << "Descriptor currently bound, allocate a new one instead" << std::endl;
|
|
|
|
|
assert(!isCurrentlyBound());
|
|
|
|
|
}
|
|
|
|
|
vkUpdateDescriptorSets(graphics->getDevice(), (uint32)writeDescriptors.size(), writeDescriptors.data(), 0, nullptr);
|
|
|
|
|
writeDescriptors.clear();
|
|
|
|
|
imageInfos.clear();
|
|
|
|
|
bufferInfos.clear();
|
|
|
|
|
}
|
2020-03-13 12:44:33 +01:00
|
|
|
}
|
|
|
|
|
|
2020-04-12 15:47:19 +02:00
|
|
|
DescriptorAllocator::DescriptorAllocator(PGraphics graphics, DescriptorLayout &layout)
|
2021-12-27 15:04:53 +01:00
|
|
|
: graphics(graphics)
|
|
|
|
|
, layout(layout)
|
2020-03-13 12:44:33 +01:00
|
|
|
{
|
2021-12-27 15:04:53 +01:00
|
|
|
for(uint32 i = 0; i < cachedHandles.size(); ++i)
|
|
|
|
|
{
|
|
|
|
|
cachedHandles[i] = nullptr;
|
|
|
|
|
}
|
2020-10-03 11:00:10 +02:00
|
|
|
|
2021-12-27 15:04:53 +01:00
|
|
|
uint32 perTypeSizes[VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT]; // TODO: FIX ENUM
|
|
|
|
|
std::memset(perTypeSizes, 0, sizeof(perTypeSizes));
|
|
|
|
|
for (uint32 i = 0; i < layout.getBindings().size(); ++i)
|
|
|
|
|
{
|
|
|
|
|
auto &binding = layout.getBindings()[i];
|
|
|
|
|
int typeIndex = binding.descriptorType;
|
|
|
|
|
perTypeSizes[typeIndex] += 256;
|
|
|
|
|
}
|
|
|
|
|
Array<VkDescriptorPoolSize> poolSizes;
|
|
|
|
|
for (uint32 i = 0; i < VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT; ++i)
|
|
|
|
|
{
|
|
|
|
|
if (perTypeSizes[i] > 0)
|
|
|
|
|
{
|
|
|
|
|
VkDescriptorPoolSize size;
|
|
|
|
|
size.descriptorCount = perTypeSizes[i];
|
|
|
|
|
size.type = (VkDescriptorType)i;
|
|
|
|
|
poolSizes.add(size);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
VkDescriptorPoolCreateInfo createInfo
|
|
|
|
|
= init::DescriptorPoolCreateInfo(
|
|
|
|
|
(uint32)poolSizes.size(),
|
|
|
|
|
poolSizes.data(),
|
|
|
|
|
maxSets * Gfx::numFramesBuffered);
|
|
|
|
|
VK_CHECK(vkCreateDescriptorPool(graphics->getDevice(), &createInfo, nullptr, &poolHandle));
|
2020-03-13 12:44:33 +01:00
|
|
|
}
|
|
|
|
|
|
2020-03-20 01:27:40 +01:00
|
|
|
DescriptorAllocator::~DescriptorAllocator()
|
2020-03-13 12:44:33 +01:00
|
|
|
{
|
2021-12-27 15:04:53 +01:00
|
|
|
vkDestroyDescriptorPool(graphics->getDevice(), poolHandle, nullptr);
|
|
|
|
|
graphics = nullptr;
|
|
|
|
|
if(nextAlloc)
|
|
|
|
|
{
|
|
|
|
|
delete nextAlloc;
|
|
|
|
|
}
|
2020-03-13 12:44:33 +01:00
|
|
|
}
|
|
|
|
|
|
2023-11-01 23:12:30 +01:00
|
|
|
Gfx::PDescriptorSet DescriptorAllocator::allocateDescriptorSet()
|
2020-03-13 12:44:33 +01:00
|
|
|
{
|
2021-12-27 15:04:53 +01:00
|
|
|
VkDescriptorSetLayout layoutHandle = layout.getHandle();
|
|
|
|
|
VkDescriptorSetAllocateInfo allocInfo =
|
|
|
|
|
init::DescriptorSetAllocateInfo(poolHandle, &layoutHandle, 1);
|
2022-04-15 11:19:30 +02:00
|
|
|
VkDescriptorSetVariableDescriptorCountAllocateInfo setCounts = {};
|
|
|
|
|
setCounts.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_VARIABLE_DESCRIPTOR_COUNT_ALLOCATE_INFO;
|
|
|
|
|
setCounts.pNext = nullptr;
|
|
|
|
|
setCounts.descriptorSetCount = 1;
|
|
|
|
|
uint32 counts = 0;
|
|
|
|
|
for(const auto& binding : layout.bindings)
|
|
|
|
|
{
|
2022-04-15 23:45:44 +02:00
|
|
|
if(binding.descriptorCount > 0)
|
2022-04-15 11:19:30 +02:00
|
|
|
{
|
|
|
|
|
counts = binding.descriptorCount;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
setCounts.pDescriptorCounts = &counts;
|
|
|
|
|
allocInfo.pNext = &setCounts;
|
2021-04-13 23:09:16 +02:00
|
|
|
|
2021-12-27 15:04:53 +01:00
|
|
|
for(uint32 setIndex = 0; setIndex < cachedHandles.size(); ++setIndex)
|
|
|
|
|
{
|
|
|
|
|
if(cachedHandles[setIndex] == nullptr)
|
|
|
|
|
{
|
|
|
|
|
cachedHandles[setIndex] = new DescriptorSet(graphics, this);
|
|
|
|
|
}
|
|
|
|
|
if(cachedHandles[setIndex]->isCurrentlyBound() || cachedHandles[setIndex]->isCurrentlyInUse())
|
|
|
|
|
{
|
|
|
|
|
// Currently in use, skip
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
if(cachedHandles[setIndex]->getHandle() == VK_NULL_HANDLE)
|
|
|
|
|
{
|
|
|
|
|
//If it hasnt been initialized, allocate it
|
|
|
|
|
VK_CHECK(vkAllocateDescriptorSets(graphics->getDevice(), &allocInfo, &cachedHandles[setIndex]->setHandle));
|
|
|
|
|
}
|
|
|
|
|
cachedHandles[setIndex]->allocate();
|
|
|
|
|
|
2023-11-05 10:36:01 +01:00
|
|
|
PDescriptorSet vulkanSet = cachedHandles[setIndex];
|
2021-12-27 15:04:53 +01:00
|
|
|
vulkanSet->cachedData.resize(layout.bindings.size());
|
|
|
|
|
// Not really pretty, but this way the set knows which ones are valid
|
|
|
|
|
std::memset(vulkanSet->cachedData.data(), 0, sizeof(void*) * vulkanSet->cachedData.size());
|
|
|
|
|
|
|
|
|
|
//Found set, stop searching
|
2023-11-05 10:36:01 +01:00
|
|
|
return vulkanSet;
|
2021-12-27 15:04:53 +01:00
|
|
|
}
|
|
|
|
|
if(nextAlloc == nullptr)
|
|
|
|
|
{
|
|
|
|
|
nextAlloc = new DescriptorAllocator(graphics, layout);
|
|
|
|
|
}
|
2023-11-05 10:36:01 +01:00
|
|
|
return nextAlloc->allocateDescriptorSet();
|
2021-12-27 15:04:53 +01:00
|
|
|
//throw std::logic_error("Out of descriptor sets");
|
2020-03-13 12:44:33 +01:00
|
|
|
}
|
2020-10-03 11:00:10 +02:00
|
|
|
|
|
|
|
|
void DescriptorAllocator::reset()
|
|
|
|
|
{
|
2021-12-27 15:04:53 +01:00
|
|
|
for(uint32 i = 0; i < cachedHandles.size(); ++i)
|
|
|
|
|
{
|
|
|
|
|
if(cachedHandles[i] == nullptr)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
cachedHandles[i]->free();
|
|
|
|
|
}
|
|
|
|
|
if(nextAlloc != nullptr)
|
|
|
|
|
{
|
|
|
|
|
nextAlloc->reset();
|
|
|
|
|
}
|
2020-10-03 11:00:10 +02:00
|
|
|
}
|