Crafter.Graphics/interfaces/Crafter.Graphics-DescriptorPoolVulkan.cppm

183 lines
7.9 KiB
Text
Raw Normal View History

2026-01-29 00:45:02 +01:00
/*
Crafter®.Graphics
Copyright (C) 2026 Catcrafts®
catcrafts.net
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License version 3.0 as published by the Free Software Foundation;
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
module;
#ifdef CRAFTER_GRAPHICS_VULKAN
2026-03-02 23:53:13 +01:00
#include "vulkan/vulkan.h"
2026-01-29 00:45:02 +01:00
#endif
export module Crafter.Graphics:DescriptorPoolVulkan;
#ifdef CRAFTER_GRAPHICS_VULKAN
import std;
import :VulkanDevice;
import :Types;
2026-02-22 00:46:38 +01:00
import :DescriptorSetLayoutVulkan;
2026-01-29 00:45:02 +01:00
import Crafter.Event;
export namespace Crafter {
2026-01-29 23:31:56 +01:00
struct DescriptorEntry {
VkDescriptorType type;
bool occured = true;
2026-01-29 01:31:17 +01:00
};
2026-01-29 23:31:56 +01:00
class DescriptorPool {
2026-01-29 00:45:02 +01:00
public:
2026-01-29 23:31:56 +01:00
std::vector<VkDescriptorSet> sets;
VkDescriptorPool descriptorPool = VK_NULL_HANDLE;
2026-01-29 00:45:02 +01:00
2026-01-29 23:31:56 +01:00
template <typename Shader>
consteval static void GetOccuringDescriptors(std::array<DescriptorEntry, 20>& types) {
2026-02-03 21:03:11 +01:00
for (const VkDescriptorSetLayoutBinding& binding : Shader::descriptors) {
2026-01-29 23:31:56 +01:00
for (DescriptorEntry& type : types) {
2026-02-03 21:03:11 +01:00
if (type.type == binding.descriptorType) {
2026-01-29 23:31:56 +01:00
type.occured = true;
}
}
2026-01-29 00:45:02 +01:00
}
2026-01-29 23:31:56 +01:00
}
2026-01-29 00:45:02 +01:00
2026-01-29 23:31:56 +01:00
template <typename... Shaders>
consteval static std::uint32_t GetUniqueDiscriptorCount() {
std::array<DescriptorEntry, 20> types = {{{VK_DESCRIPTOR_TYPE_SAMPLER, 0},{VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, 0},{VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE, 0},{VK_DESCRIPTOR_TYPE_STORAGE_IMAGE, 0},{VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER, 0},{VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER, 0},{VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, 0},{VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, 0},{VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC, 0},{VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC, 0},{VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT, 0},{VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK, 0},{VK_DESCRIPTOR_TYPE_ACCELERATION_STRUCTURE_KHR, 0},{VK_DESCRIPTOR_TYPE_ACCELERATION_STRUCTURE_NV, 0},{VK_DESCRIPTOR_TYPE_SAMPLE_WEIGHT_IMAGE_QCOM, 0},{VK_DESCRIPTOR_TYPE_BLOCK_MATCH_IMAGE_QCOM, 0},{VK_DESCRIPTOR_TYPE_MUTABLE_EXT, 0},{VK_DESCRIPTOR_TYPE_PARTITIONED_ACCELERATION_STRUCTURE_NV, 0},{VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK_EXT, 0},{VK_DESCRIPTOR_TYPE_MUTABLE_VALVE, 0}}};
(GetOccuringDescriptors<Shaders>(types), ... );
std::uint32_t size = 0;
for(DescriptorEntry& type : types) {
if(type.occured) {
size++;
}
2026-01-29 00:45:02 +01:00
}
2026-01-29 23:31:56 +01:00
return size;
}
template <typename... Shaders>
consteval static std::array<VkDescriptorPoolSize, GetUniqueDiscriptorCount<Shaders...>()> GetPoolSizes() {
std::array<VkDescriptorPoolSize, GetUniqueDiscriptorCount<Shaders...>()> types = {};
for(std::uint32_t i = 0; i < GetUniqueDiscriptorCount<Shaders...>(); i++){
types[i].descriptorCount = 12345;
}
([&] {
2026-02-03 21:03:11 +01:00
for (const VkDescriptorSetLayoutBinding& binding : Shaders::descriptors) {
2026-01-29 23:31:56 +01:00
bool found = false;
for(VkDescriptorPoolSize& type : types) {
2026-02-03 21:03:11 +01:00
if(type.type == binding.descriptorType && type.descriptorCount != 12345) {
2026-02-05 05:22:01 +01:00
type.descriptorCount += binding.descriptorCount;
2026-01-29 23:31:56 +01:00
found = true;
}
}
if(!found) {
for(std::uint32_t i = 0; i < GetUniqueDiscriptorCount<Shaders...>(); i++){
if(types[i].descriptorCount == 12345) {
2026-02-03 21:03:11 +01:00
types[i].type = binding.descriptorType;
2026-02-05 05:22:01 +01:00
types[i].descriptorCount = binding.descriptorCount;
2026-01-29 23:31:56 +01:00
break;
}
}
}
}
}(),
...);
return types;
}
2026-02-22 00:46:38 +01:00
constexpr static std::vector<VkDescriptorPoolSize> GetPoolSizes(const std::span<const DescriptorSetLayoutVulkan> shaders) {
std::vector<VkDescriptorPoolSize> types;
for(const DescriptorSetLayoutVulkan& shader : shaders) {
for (const VkDescriptorSetLayoutBinding& binding : shader.descriptors) {
for(VkDescriptorPoolSize& type : types) {
if(type.type == binding.descriptorType) {
type.descriptorCount += binding.descriptorCount;
goto inner;
}
}
types.emplace_back(binding.descriptorType, binding.descriptorCount);
inner:;
}
}
return types;
}
2026-02-24 02:32:37 +01:00
template <typename... Shaders>
constexpr static std::vector<VkDescriptorPoolSize> GetPoolSizesCombined(const std::span<const DescriptorSetLayoutVulkan> shaders) {
std::vector<VkDescriptorPoolSize> types;
for(const DescriptorSetLayoutVulkan& shader : shaders) {
for (const VkDescriptorSetLayoutBinding& binding : shader.descriptors) {
for(VkDescriptorPoolSize& type : types) {
if(type.type == binding.descriptorType) {
type.descriptorCount += binding.descriptorCount;
goto inner;
}
}
types.emplace_back(binding.descriptorType, binding.descriptorCount);
inner:;
}
}
([&] {
for (const VkDescriptorSetLayoutBinding& binding : Shaders::descriptors) {
for(VkDescriptorPoolSize& type : types) {
if(type.type == binding.descriptorType) {
type.descriptorCount += binding.descriptorCount;
goto inner2;
}
}
types.emplace_back(binding.descriptorType, binding.descriptorCount);
inner2:;
}
}(),
...);
return types;
}
2026-01-29 23:31:56 +01:00
public:
void BuildPool(std::span<const VkDescriptorPoolSize> poolSizes, std::span<const VkDescriptorSetLayout> setLayouts) {
if(descriptorPool != VK_NULL_HANDLE) {
vkDestroyDescriptorPool(VulkanDevice::device, descriptorPool, nullptr);
}
2026-01-29 00:45:02 +01:00
VkDescriptorPoolCreateInfo descriptorPoolInfo {
.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO,
2026-01-29 23:31:56 +01:00
.maxSets = static_cast<std::uint32_t>(sets.size()),
.poolSizeCount = static_cast<std::uint32_t>(poolSizes.size()),
2026-01-29 00:45:02 +01:00
.pPoolSizes = poolSizes.data()
};
2026-01-29 23:31:56 +01:00
VulkanDevice::CheckVkResult(vkCreateDescriptorPool(VulkanDevice::device, &descriptorPoolInfo, nullptr, &descriptorPool));
2026-01-29 00:45:02 +01:00
VkDescriptorSetAllocateInfo allocInfo {
.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO,
2026-01-29 23:31:56 +01:00
.descriptorPool = descriptorPool,
.descriptorSetCount = static_cast<std::uint32_t>(sets.size()),
.pSetLayouts = setLayouts.data(),
2026-01-29 00:45:02 +01:00
};
VulkanDevice::CheckVkResult(vkAllocateDescriptorSets(VulkanDevice::device, &allocInfo, sets.data()));
}
};
}
#endif