Files
Seele/src/Engine/Graphics/Vulkan/Descriptor.cpp
T

398 lines
14 KiB
C++
Raw Normal View History

2023-11-15 17:42:57 +01:00
#include "Descriptor.h"
#include "Buffer.h"
2023-12-02 10:55:00 +01:00
#include "Command.h"
2024-04-13 23:51:38 +02:00
#include "Graphics.h"
#include "Texture.h"
2020-03-09 12:30:07 +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)
2024-04-13 23:51:38 +02:00
: Gfx::DescriptorLayout(name), graphics(graphics), layoutHandle(VK_NULL_HANDLE) {}
DescriptorLayout::~DescriptorLayout() {
2024-05-15 15:27:13 +02:00
graphics->getDestructionManager()->queueResourceForDestruction(std::move(pool));
2024-04-13 23:51:38 +02:00
if (layoutHandle != VK_NULL_HANDLE) {
vkDestroyDescriptorSetLayout(graphics->getDevice(), layoutHandle, nullptr);
}
2020-03-09 12:30:07 +01:00
}
2024-04-13 23:51:38 +02:00
void DescriptorLayout::create() {
if (layoutHandle != VK_NULL_HANDLE) {
return;
}
bindings.resize(descriptorBindings.size());
Array<VkDescriptorBindingFlags> bindingFlags(descriptorBindings.size());
for (size_t i = 0; i < descriptorBindings.size(); ++i) {
const Gfx::DescriptorBinding& gfxBinding = descriptorBindings[i];
bindings[i] = {
.binding = gfxBinding.binding,
.descriptorType = cast(gfxBinding.descriptorType),
.descriptorCount = gfxBinding.descriptorCount,
.stageFlags = gfxBinding.shaderStages,
.pImmutableSamplers = nullptr,
2023-11-10 22:26:47 +01:00
};
2024-04-13 23:51:38 +02:00
bindingFlags[i] = gfxBinding.bindingFlags;
}
VkDescriptorSetLayoutBindingFlagsCreateInfo bindingFlagsInfo = {
.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_BINDING_FLAGS_CREATE_INFO,
.pNext = nullptr,
.bindingCount = static_cast<uint32>(bindingFlags.size()),
.pBindingFlags = bindingFlags.data(),
};
VkDescriptorSetLayoutCreateInfo createInfo = {
.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO,
.pNext = &bindingFlagsInfo,
.flags = 0,
.bindingCount = (uint32)bindings.size(),
.pBindings = bindings.data(),
};
VK_CHECK(vkCreateDescriptorSetLayout(graphics->getDevice(), &createInfo, nullptr, &layoutHandle));
2020-03-09 12:30:07 +01:00
2024-04-13 23:51:38 +02:00
pool = new DescriptorPool(graphics, this);
2020-10-03 11:00:10 +02:00
2024-04-13 23:51:38 +02:00
hash = CRC::Calculate(bindings.data(), sizeof(VkDescriptorSetLayoutBinding) * bindings.size(), CRC::CRC_32());
2020-03-09 12:30:07 +01:00
}
2024-05-15 15:27:13 +02:00
DescriptorPool::DescriptorPool(PGraphics graphics, PDescriptorLayout layout)
: CommandBoundResource(graphics)
, graphics(graphics)
, layout(layout) {
2024-04-13 23:51:38 +02:00
for (uint32 i = 0; i < cachedHandles.size(); ++i) {
cachedHandles[i] = nullptr;
}
2020-03-13 12:44:33 +01:00
2024-04-13 23:51:38 +02: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);
2021-12-27 15:04:53 +01:00
}
2024-04-13 23:51:38 +02:00
}
VkDescriptorPoolCreateInfo createInfo = {
.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO,
.pNext = nullptr,
.flags = 0,
.maxSets = maxSets * Gfx::numFramesBuffered,
.poolSizeCount = (uint32)poolSizes.size(),
.pPoolSizes = poolSizes.data(),
};
VK_CHECK(vkCreateDescriptorPool(graphics->getDevice(), &createInfo, nullptr, &poolHandle));
2020-03-13 12:44:33 +01:00
}
2024-04-13 23:51:38 +02:00
DescriptorPool::~DescriptorPool() {
2024-05-15 15:27:13 +02:00
for (size_t i = 0; i < maxSets; ++i)
{
if (cachedHandles[i] != nullptr)
{
cachedHandles[i] = nullptr;
}
}
vkDestroyDescriptorPool(graphics->getDevice(), poolHandle, nullptr);
if (nextAlloc) {
delete nextAlloc;
}
2020-06-02 11:46:18 +02:00
}
2024-04-13 23:51:38 +02:00
Gfx::PDescriptorSet DescriptorPool::allocateDescriptorSet() {
VkDescriptorSetLayout layoutHandle = layout->getHandle();
VkDescriptorSetVariableDescriptorCountAllocateInfo setCounts = {
.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_VARIABLE_DESCRIPTOR_COUNT_ALLOCATE_INFO,
.pNext = nullptr,
.descriptorSetCount = 1,
};
VkDescriptorSetAllocateInfo allocInfo = {
.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO,
.pNext = nullptr,
.descriptorPool = poolHandle,
.descriptorSetCount = 1,
.pSetLayouts = &layoutHandle,
};
uint32 counts = 0;
for (const auto& binding : layout->bindings) {
if (binding.descriptorCount > 0) {
counts = binding.descriptorCount;
2021-12-27 15:04:53 +01:00
}
2024-04-13 23:51:38 +02:00
}
if (layout->bindings.size() > 0) {
setCounts.pDescriptorCounts = &counts;
allocInfo.pNext = &setCounts;
}
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();
2020-03-13 12:44:33 +01:00
2024-04-13 23:51:38 +02:00
PDescriptorSet vulkanSet = cachedHandles[setIndex];
2020-10-03 11:00:10 +02:00
2024-04-13 23:51:38 +02:00
// Found set, stop searching
return vulkanSet;
}
if (nextAlloc == nullptr) {
nextAlloc = new DescriptorPool(graphics, layout);
}
// std::cout << "Out of descriptors, forwarding" << std::endl;
return nextAlloc->allocateDescriptorSet();
// throw std::logic_error("Out of descriptor sets");
2020-03-13 12:44:33 +01:00
}
2024-04-13 23:51:38 +02:00
void DescriptorPool::reset() {
for (uint32 i = 0; i < cachedHandles.size(); ++i) {
if (cachedHandles[i] == nullptr) {
return;
2021-12-27 15:04:53 +01:00
}
2024-04-13 23:51:38 +02:00
cachedHandles[i]->free();
}
if (nextAlloc != nullptr) {
nextAlloc->reset();
}
2020-03-13 12:44:33 +01:00
}
2024-04-13 23:51:38 +02:00
DescriptorSet::DescriptorSet(PGraphics graphics, PDescriptorPool owner)
2024-05-15 15:27:13 +02:00
: Gfx::DescriptorSet(owner->getLayout())
, CommandBoundResource(graphics)
, setHandle(VK_NULL_HANDLE)
, graphics(graphics)
, owner(owner)
, bindCount(0)
, currentlyInUse(false)
{
boundResources.resize(owner->getLayout()->getBindings().size());
}
2024-04-23 08:11:44 +02:00
2024-05-15 15:27:13 +02:00
DescriptorSet::~DescriptorSet()
{
vkFreeDescriptorSets(graphics->getDevice(), owner->getHandle(), 1, &setHandle);
}
2024-04-13 23:51:38 +02:00
void DescriptorSet::updateBuffer(uint32_t binding, Gfx::PUniformBuffer uniformBuffer) {
PUniformBuffer vulkanBuffer = uniformBuffer.cast<UniformBuffer>();
bufferInfos.add(VkDescriptorBufferInfo{
.buffer = vulkanBuffer->getHandle(),
.offset = 0,
.range = vulkanBuffer->getSize(),
});
writeDescriptors.add(VkWriteDescriptorSet{
.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET,
.pNext = nullptr,
.dstSet = setHandle,
.dstBinding = binding,
.dstArrayElement = 0,
.descriptorCount = 1,
2024-05-12 19:36:32 +02:00
.descriptorType = cast(layout->getBindings()[binding].descriptorType),
2024-04-13 23:51:38 +02:00
.pBufferInfo = &bufferInfos.back(),
});
boundResources[binding] = vulkanBuffer->getAlloc();
2024-04-13 23:51:38 +02:00
}
void DescriptorSet::updateBuffer(uint32_t binding, Gfx::PShaderBuffer shaderBuffer) {
PShaderBuffer vulkanBuffer = shaderBuffer.cast<ShaderBuffer>();
bufferInfos.add(VkDescriptorBufferInfo{
.buffer = vulkanBuffer->getHandle(),
.offset = 0,
.range = vulkanBuffer->getSize(),
});
writeDescriptors.add(VkWriteDescriptorSet{
.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET,
.pNext = nullptr,
.dstSet = setHandle,
.dstBinding = binding,
.dstArrayElement = 0,
.descriptorCount = 1,
2024-05-12 19:36:32 +02:00
.descriptorType = cast(layout->getBindings()[binding].descriptorType),
2024-04-13 23:51:38 +02:00
.pBufferInfo = &bufferInfos.back(),
});
boundResources[binding] = vulkanBuffer->getAlloc();
2024-04-13 23:51:38 +02:00
}
2024-05-06 18:36:16 +02:00
void DescriptorSet::updateBuffer(uint32_t binding, uint32 index, Gfx::PShaderBuffer shaderBuffer) {
PShaderBuffer vulkanBuffer = shaderBuffer.cast<ShaderBuffer>();
bufferInfos.add(VkDescriptorBufferInfo{
.buffer = vulkanBuffer->getHandle(),
.offset = 0,
.range = vulkanBuffer->getSize(),
});
writeDescriptors.add(VkWriteDescriptorSet{
.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET,
.pNext = nullptr,
.dstSet = setHandle,
.dstBinding = binding,
.dstArrayElement = index,
.descriptorCount = 1,
2024-05-12 19:36:32 +02:00
.descriptorType = cast(layout->getBindings()[binding].descriptorType),
2024-05-06 18:36:16 +02:00
.pBufferInfo = &bufferInfos.back(),
});
boundResources[binding] = vulkanBuffer->getAlloc();
2024-05-06 18:36:16 +02:00
}
2024-04-13 23:51:38 +02:00
void DescriptorSet::updateSampler(uint32_t binding, Gfx::PSampler samplerState) {
PSampler vulkanSampler = samplerState.cast<Sampler>();
2024-04-13 23:51:38 +02:00
imageInfos.add(VkDescriptorImageInfo{
.sampler = vulkanSampler->getSampler(),
2024-04-13 23:51:38 +02:00
.imageView = VK_NULL_HANDLE,
.imageLayout = VK_IMAGE_LAYOUT_UNDEFINED,
});
writeDescriptors.add(VkWriteDescriptorSet{
.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET,
.pNext = nullptr,
.dstSet = setHandle,
.dstBinding = binding,
.dstArrayElement = 0,
.descriptorCount = 1,
.descriptorType = VK_DESCRIPTOR_TYPE_SAMPLER,
.pImageInfo = &imageInfos.back(),
});
boundResources[binding] = vulkanSampler->getHandle();
2024-04-13 23:51:38 +02:00
}
void DescriptorSet::updateTexture(uint32_t binding, Gfx::PTexture texture, Gfx::PSampler samplerState) {
TextureBase* vulkanTexture = texture.cast<TextureBase>().getHandle();
2024-04-13 23:51:38 +02:00
// It is assumed that the image is in the correct layout
imageInfos.add(VkDescriptorImageInfo{
.sampler = samplerState != nullptr ? samplerState.cast<Sampler>()->getSampler() : VK_NULL_HANDLE,
2024-04-13 23:51:38 +02:00
.imageView = vulkanTexture->getView(),
.imageLayout = cast(vulkanTexture->getLayout()),
});
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;
}
writeDescriptors.add(VkWriteDescriptorSet{
.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET,
.pNext = nullptr,
.dstSet = setHandle,
.dstBinding = binding,
.dstArrayElement = 0,
.descriptorCount = 1,
.descriptorType = descriptorType,
.pImageInfo = &imageInfos.back(),
});
boundResources[binding] = vulkanTexture->getHandle();
2024-04-13 23:51:38 +02:00
}
void DescriptorSet::updateTextureArray(uint32_t binding, Array<Gfx::PTexture> textures) {
// maybe make this a parameter?
uint32 arrayElement = 0;
boundResources.resize(binding + textures.size());
2024-04-13 23:51:38 +02:00
for (auto& gfxTexture : textures) {
TextureBase* vulkanTexture = gfxTexture.cast<TextureBase>().getHandle();
2023-11-15 17:42:57 +01:00
imageInfos.add(VkDescriptorImageInfo{
2024-04-13 23:51:38 +02:00
.sampler = VK_NULL_HANDLE,
2023-11-15 17:42:57 +01:00
.imageView = vulkanTexture->getView(),
.imageLayout = cast(vulkanTexture->getLayout()),
});
2024-04-13 23:51:38 +02:00
2022-04-15 11:19:30 +02:00
VkDescriptorType descriptorType = VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE;
2024-04-13 23:51:38 +02:00
if (vulkanTexture->getUsage() & VK_IMAGE_USAGE_STORAGE_BIT) {
descriptorType = VK_DESCRIPTOR_TYPE_STORAGE_IMAGE;
2022-04-15 11:19:30 +02:00
}
boundResources[binding + arrayElement] = vulkanTexture->getHandle();
2023-11-15 17:42:57 +01:00
writeDescriptors.add(VkWriteDescriptorSet{
.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET,
.pNext = nullptr,
.dstSet = setHandle,
.dstBinding = binding,
2024-04-13 23:51:38 +02:00
.dstArrayElement = arrayElement++,
2023-11-15 17:42:57 +01:00
.descriptorCount = 1,
.descriptorType = descriptorType,
.pImageInfo = &imageInfos.back(),
2024-04-13 23:51:38 +02:00
});
2024-05-15 15:27:13 +02:00
vulkanTexture->getHandle()->bind();
2024-04-13 23:51:38 +02:00
}
2022-04-15 11:19:30 +02:00
}
2020-03-13 12:44:33 +01:00
2024-04-13 23:51:38 +02:00
void DescriptorSet::writeChanges() {
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
}
2024-04-23 08:11:44 +02:00
PipelineLayout::PipelineLayout(PGraphics graphics, const std::string& name, Gfx::PPipelineLayout baseLayout)
: Gfx::PipelineLayout(name, baseLayout), graphics(graphics), layoutHandle(VK_NULL_HANDLE) {}
2021-04-13 23:09:16 +02:00
2024-04-13 23:51:38 +02:00
PipelineLayout::~PipelineLayout() {}
2020-03-13 12:44:33 +01:00
std::mutex layoutLock;
2024-04-13 23:51:38 +02:00
Map<uint32, VkPipelineLayout> cachedLayouts;
2020-10-03 11:00:10 +02:00
2024-04-13 23:51:38 +02:00
void PipelineLayout::create() {
2024-04-23 08:11:44 +02:00
for (auto [name, desc] : descriptorSetLayouts) {
PDescriptorLayout layout = desc.cast<DescriptorLayout>();
2024-04-13 23:51:38 +02:00
layout->create();
2024-04-23 23:21:30 +02:00
uint32 parameterIndex = parameterMapping[layout->getName()];
2024-04-24 23:25:34 +02:00
if (parameterIndex >= vulkanDescriptorLayouts.size())
2024-04-23 23:21:30 +02:00
{
vulkanDescriptorLayouts.resize(parameterIndex + 1);
}
vulkanDescriptorLayouts[parameterIndex] = layout->getHandle();
2024-04-13 23:51:38 +02:00
}
2020-03-13 12:44:33 +01:00
2024-04-13 23:51:38 +02:00
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;
2024-05-15 15:27:13 +02:00
vkPushConstants[i].stageFlags = pushConstants[i].stageFlags;
2024-04-13 23:51:38 +02:00
}
2020-03-13 12:44:33 +01:00
2024-04-13 23:51:38 +02:00
VkPipelineLayoutCreateInfo createInfo = {
.sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO,
.pNext = nullptr,
.flags = 0,
.setLayoutCount = (uint32)vulkanDescriptorLayouts.size(),
.pSetLayouts = vulkanDescriptorLayouts.data(),
.pushConstantRangeCount = (uint32)vkPushConstants.size(),
.pPushConstantRanges = vkPushConstants.data(),
};
2020-10-03 11:00:10 +02:00
2024-04-13 23:51:38 +02: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);
std::unique_lock l(layoutLock);
2024-04-13 23:51:38 +02:00
if (cachedLayouts.contains(layoutHash)) {
2024-05-23 14:58:14 +02:00
// std::cout << "New Layout" << std::endl;
2024-04-13 23:51:38 +02:00
layoutHandle = cachedLayouts[layoutHash];
return;
}
VK_CHECK(vkCreatePipelineLayout(graphics->getDevice(), &createInfo, nullptr, &layoutHandle));
cachedLayouts[layoutHash] = layoutHandle;
2020-10-03 11:00:10 +02:00
}