|
| 1 | +module; |
| 2 | + |
| 3 | +#include <memory> |
| 4 | +#include <array> |
| 5 | +#include <vector> |
| 6 | + |
| 7 | +export module Descriptor; |
| 8 | + |
| 9 | +import vulkan_hpp; |
| 10 | + |
| 11 | +import Config; |
| 12 | +import Device; |
| 13 | +import GBuffer; |
| 14 | +import GraphicsPipeline; |
| 15 | +import UniformBuffer; |
| 16 | +import TextureSampler; |
| 17 | +import LightUniformBuffer; |
| 18 | + |
| 19 | +export namespace vht { |
| 20 | + |
| 21 | + /** |
| 22 | + * @brief 描述符集管理类 |
| 23 | + * @details |
| 24 | + * - 依赖: |
| 25 | + * - m_device: 逻辑设备 |
| 26 | + * - m_graphics_pipeline: 图形管线 |
| 27 | + * - m_uniform_buffer: Uniform Buffer对象 |
| 28 | + * - m_texture_sampler: 纹理采样器对象 |
| 29 | + * - 工作: |
| 30 | + * - 创建描述符池和描述符集 |
| 31 | + * - 可访问成员: |
| 32 | + * - pool(): 获取描述符池 |
| 33 | + * - sets(): 获取描述符集列表 |
| 34 | + */ |
| 35 | + class Descriptor { |
| 36 | + std::shared_ptr<vht::Device> m_device; |
| 37 | + std::shared_ptr<vht::GBuffer> m_g_buffer; |
| 38 | + std::shared_ptr<vht::GraphicsPipeline> m_graphics_pipeline; |
| 39 | + std::shared_ptr<vht::UniformBuffer> m_uniform_buffer; |
| 40 | + std::shared_ptr<vht::TextureSampler> m_texture_sampler; |
| 41 | + std::shared_ptr<vht::LightUniformBuffer> m_light_uniform_buffer; |
| 42 | + vk::raii::DescriptorPool m_pool{ nullptr }; |
| 43 | + std::vector<vk::raii::DescriptorSet> m_sets; |
| 44 | + std::vector<vk::raii::DescriptorSet> m_second_sets; |
| 45 | + public: |
| 46 | + explicit Descriptor( |
| 47 | + std::shared_ptr<vht::Device> device, |
| 48 | + std::shared_ptr<vht::GBuffer> g_buffer, |
| 49 | + std::shared_ptr<vht::GraphicsPipeline> m_graphics_pipeline, |
| 50 | + std::shared_ptr<vht::UniformBuffer> m_uniform_buffer, |
| 51 | + std::shared_ptr<vht::TextureSampler> m_texture_sampler, |
| 52 | + std::shared_ptr<vht::LightUniformBuffer> m_light_uniform_buffer |
| 53 | + ): m_device(std::move(device)), |
| 54 | + m_g_buffer(std::move(g_buffer)), |
| 55 | + m_graphics_pipeline(std::move(m_graphics_pipeline)), |
| 56 | + m_uniform_buffer(std::move(m_uniform_buffer)), |
| 57 | + m_texture_sampler(std::move(m_texture_sampler)), |
| 58 | + m_light_uniform_buffer(std::move(m_light_uniform_buffer)) { |
| 59 | + init(); |
| 60 | + } |
| 61 | + |
| 62 | + [[nodiscard]] |
| 63 | + const vk::raii::DescriptorPool& pool() const { return m_pool; } |
| 64 | + [[nodiscard]] |
| 65 | + const std::vector<vk::raii::DescriptorSet>& sets() const { return m_sets; } |
| 66 | + [[nodiscard]] |
| 67 | + const std::vector<vk::raii::DescriptorSet>& second_sets() const { return m_second_sets; } |
| 68 | + |
| 69 | + void recreate() { |
| 70 | + m_sets.clear(); |
| 71 | + m_second_sets.clear(); |
| 72 | + create_descriptor_sets(); |
| 73 | + } |
| 74 | + |
| 75 | + private: |
| 76 | + void init() { |
| 77 | + create_descriptor_pool(); |
| 78 | + create_descriptor_sets(); |
| 79 | + } |
| 80 | + // 创建描述符池 |
| 81 | + void create_descriptor_pool() { |
| 82 | + std::array<vk::DescriptorPoolSize, 3> pool_sizes; |
| 83 | + pool_sizes[0].type = vk::DescriptorType::eUniformBuffer; |
| 84 | + pool_sizes[0].descriptorCount = static_cast<uint32_t>(MAX_FRAMES_IN_FLIGHT * 2); // 包括 Light UBO |
| 85 | + pool_sizes[1].type = vk::DescriptorType::eCombinedImageSampler; |
| 86 | + pool_sizes[1].descriptorCount = static_cast<uint32_t>(MAX_FRAMES_IN_FLIGHT); |
| 87 | + pool_sizes[2].type = vk::DescriptorType::eInputAttachment; |
| 88 | + pool_sizes[2].descriptorCount = static_cast<uint32_t>(MAX_FRAMES_IN_FLIGHT * 3); |
| 89 | + |
| 90 | + vk::DescriptorPoolCreateInfo poolInfo; |
| 91 | + poolInfo.flags = vk::DescriptorPoolCreateFlagBits::eFreeDescriptorSet; |
| 92 | + poolInfo.setPoolSizes( pool_sizes ); |
| 93 | + poolInfo.maxSets = static_cast<uint32_t>(MAX_FRAMES_IN_FLIGHT * 2); |
| 94 | + |
| 95 | + m_pool = m_device->device().createDescriptorPool(poolInfo); |
| 96 | + } |
| 97 | + // 创建描述符集 |
| 98 | + void create_descriptor_sets() { |
| 99 | + std::vector<vk::DescriptorSetLayout> layouts(MAX_FRAMES_IN_FLIGHT, *m_graphics_pipeline->descriptor_set_layout()); |
| 100 | + vk::DescriptorSetAllocateInfo alloc_info; |
| 101 | + alloc_info.descriptorPool = m_pool; |
| 102 | + alloc_info.setSetLayouts( layouts ); |
| 103 | + |
| 104 | + m_sets = m_device->device().allocateDescriptorSets(alloc_info); |
| 105 | + |
| 106 | + for (size_t i = 0; i < MAX_FRAMES_IN_FLIGHT; ++i) { |
| 107 | + vk::DescriptorBufferInfo buffer_info; |
| 108 | + buffer_info.buffer = m_uniform_buffer->buffers()[i]; |
| 109 | + buffer_info.offset = 0; |
| 110 | + buffer_info.range = sizeof(UBO); |
| 111 | + |
| 112 | + vk::DescriptorImageInfo image_info; |
| 113 | + image_info.imageLayout = vk::ImageLayout::eShaderReadOnlyOptimal; |
| 114 | + image_info.imageView = m_texture_sampler->image_view(); |
| 115 | + image_info.sampler = m_texture_sampler->sampler(); |
| 116 | + |
| 117 | + std::array<vk::WriteDescriptorSet, 2> writes; |
| 118 | + writes[0].dstSet = m_sets[i]; |
| 119 | + writes[0].dstBinding = 0; |
| 120 | + writes[0].dstArrayElement = 0; |
| 121 | + writes[0].descriptorType = vk::DescriptorType::eUniformBuffer; |
| 122 | + writes[0].setBufferInfo(buffer_info); |
| 123 | + writes[1].dstSet = m_sets[i]; |
| 124 | + writes[1].dstBinding = 1; |
| 125 | + writes[1].dstArrayElement = 0; |
| 126 | + writes[1].descriptorType = vk::DescriptorType::eCombinedImageSampler; |
| 127 | + writes[1].setImageInfo(image_info); |
| 128 | + |
| 129 | + m_device->device().updateDescriptorSets(writes, nullptr); |
| 130 | + } |
| 131 | + |
| 132 | + std::vector<vk::DescriptorSetLayout> second_layouts(MAX_FRAMES_IN_FLIGHT, *m_graphics_pipeline->second_descriptor_set_layout()); |
| 133 | + vk::DescriptorSetAllocateInfo second_alloc_info; |
| 134 | + second_alloc_info.descriptorPool = m_pool; |
| 135 | + second_alloc_info.setSetLayouts( second_layouts ); |
| 136 | + |
| 137 | + m_second_sets = m_device->device().allocateDescriptorSets(second_alloc_info); |
| 138 | + |
| 139 | + for (size_t i = 0; i < MAX_FRAMES_IN_FLIGHT; ++i) { |
| 140 | + vk::DescriptorBufferInfo light_buffer_info; |
| 141 | + light_buffer_info.buffer = m_light_uniform_buffer->buffers()[i]; |
| 142 | + light_buffer_info.offset = 0; |
| 143 | + light_buffer_info.range = sizeof(LightUBO); |
| 144 | + |
| 145 | + std::array<vk::DescriptorImageInfo, 3> input_attachments; |
| 146 | + input_attachments[0].imageLayout = vk::ImageLayout::eShaderReadOnlyOptimal; |
| 147 | + input_attachments[0].imageView = m_g_buffer->pos_views(); |
| 148 | + input_attachments[1].imageLayout = vk::ImageLayout::eShaderReadOnlyOptimal; |
| 149 | + input_attachments[1].imageView = m_g_buffer->color_views(); |
| 150 | + input_attachments[2].imageLayout = vk::ImageLayout::eShaderReadOnlyOptimal; |
| 151 | + input_attachments[2].imageView = m_g_buffer->normal_depth_views(); |
| 152 | + |
| 153 | + std::array<vk::WriteDescriptorSet, 2> writes; |
| 154 | + writes[0].dstSet = m_second_sets[i]; |
| 155 | + writes[0].dstBinding = 0; |
| 156 | + writes[0].dstArrayElement = 0; |
| 157 | + writes[0].descriptorType = vk::DescriptorType::eUniformBuffer; |
| 158 | + writes[0].setBufferInfo(light_buffer_info); |
| 159 | + writes[1].dstSet = m_second_sets[i]; |
| 160 | + writes[1].dstBinding = 1; |
| 161 | + writes[1].dstArrayElement = 0; |
| 162 | + writes[1].descriptorType = vk::DescriptorType::eInputAttachment; |
| 163 | + writes[1].setImageInfo(input_attachments); |
| 164 | + |
| 165 | + m_device->device().updateDescriptorSets(writes, nullptr); |
| 166 | + } |
| 167 | + } |
| 168 | + }; |
| 169 | + |
| 170 | +} // namespace vht |
| 171 | + |
0 commit comments