|
| 1 | +#pragma once |
| 2 | + |
| 3 | +#include <vulkan/vulkan_core.h> |
| 4 | + |
| 5 | +#include <cassert> |
| 6 | +#include <string> |
| 7 | +#include <vector> |
| 8 | + |
| 9 | +namespace inexor::vulkan_renderer::wrapper { |
| 10 | + |
| 11 | +// forward declarations |
| 12 | +class Device; |
| 13 | +class ResourceDescriptor; |
| 14 | + |
| 15 | +class DescriptorBuilder { |
| 16 | + const Device &m_device; |
| 17 | + const std::uint32_t m_swapchain_image_count; |
| 18 | + |
| 19 | + std::vector<VkDescriptorSetLayoutBinding> m_layout_bindings; |
| 20 | + std::vector<VkWriteDescriptorSet> m_write_sets; |
| 21 | + std::vector<VkDescriptorBufferInfo> m_descriptor_buffer_infos; |
| 22 | + std::vector<VkDescriptorImageInfo> m_descriptor_image_infos; |
| 23 | + |
| 24 | +public: |
| 25 | + /// @brief Constructs the descriptor builder. |
| 26 | + /// @param device The const reference to a device RAII wrapper instance. |
| 27 | + /// @param swapchain_image_count The number of images in swapchain. |
| 28 | + DescriptorBuilder(const Device &device, std::uint32_t swapchain_image_count); |
| 29 | + |
| 30 | + DescriptorBuilder(const DescriptorBuilder &) = delete; |
| 31 | + DescriptorBuilder(DescriptorBuilder &&) = delete; |
| 32 | + ~DescriptorBuilder() = default; |
| 33 | + |
| 34 | + DescriptorBuilder &operator=(const DescriptorBuilder &) = delete; |
| 35 | + DescriptorBuilder &operator=(DescriptorBuilder &&) = delete; |
| 36 | + |
| 37 | + // TODO: Implement more descriptor types than just uniform buffers and combined image samplers. |
| 38 | + // TODO: Support uniform buffer offset in VkDescriptorBufferInfo. |
| 39 | + // TODO: Offer overloaded methods which expose more fields of the structures. |
| 40 | + |
| 41 | + /// @brief Adds a uniform buffer to the descriptor container. |
| 42 | + /// @tparam T The type of the uniform buffer. |
| 43 | + /// @param uniform_buffer The uniform buffer which contains the data which will be accessed by the shader. |
| 44 | + /// @param binding The binding index which will be used in the SPIR-V shader. |
| 45 | + /// @param shader_stage The shader stage the uniform buffer will be used in, most likely the vertex shader. |
| 46 | + /// @return A const reference to this DescriptorBuilder instance. |
| 47 | + template <typename T> |
| 48 | + DescriptorBuilder &add_uniform_buffer(const VkBuffer uniform_buffer, const std::uint32_t binding, |
| 49 | + const VkShaderStageFlagBits shader_stage = VK_SHADER_STAGE_VERTEX_BIT); |
| 50 | + |
| 51 | + /// @brief Adds a combined image sampler to the descriptor container. |
| 52 | + /// @param image_sampler The pointer to the combined image sampler. |
| 53 | + /// @param image_view The pointer to the image view. |
| 54 | + /// @param binding The binding index which will be used in the SPIR-V shader. |
| 55 | + /// @param shader_stage The shader stage the uniform buffer will be used in, most likely the fragment shader. |
| 56 | + /// @return A const reference to this DescriptorBuilder instance. |
| 57 | + DescriptorBuilder & |
| 58 | + add_combined_image_sampler(const VkSampler image_sampler, const VkImageView image_view, const std::uint32_t binding, |
| 59 | + const VkShaderStageFlagBits shader_stage = VK_SHADER_STAGE_FRAGMENT_BIT); |
| 60 | + |
| 61 | + /// @brief Builds the resource descriptor. |
| 62 | + /// @param name The internal name of the resource descriptor. |
| 63 | + /// @return The resource descriptor which was created by the builder. |
| 64 | + [[nodiscard]] ResourceDescriptor build(std::string name); |
| 65 | +}; |
| 66 | + |
| 67 | +template <typename T> |
| 68 | +DescriptorBuilder &DescriptorBuilder::add_uniform_buffer(const VkBuffer uniform_buffer, const std::uint32_t binding, |
| 69 | + const VkShaderStageFlagBits shader_stage) { |
| 70 | + assert(uniform_buffer); |
| 71 | + |
| 72 | + VkDescriptorSetLayoutBinding layout_binding{}; |
| 73 | + layout_binding.binding = binding; |
| 74 | + layout_binding.descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER; |
| 75 | + layout_binding.descriptorCount = 1; |
| 76 | + layout_binding.stageFlags = shader_stage; |
| 77 | + layout_binding.pImmutableSamplers = nullptr; |
| 78 | + |
| 79 | + m_layout_bindings.push_back(layout_binding); |
| 80 | + |
| 81 | + VkDescriptorBufferInfo uniform_buffer_info{}; |
| 82 | + uniform_buffer_info.buffer = uniform_buffer; |
| 83 | + uniform_buffer_info.offset = 0; |
| 84 | + uniform_buffer_info.range = sizeof(T); |
| 85 | + |
| 86 | + m_descriptor_buffer_infos.push_back(uniform_buffer_info); |
| 87 | + |
| 88 | + VkWriteDescriptorSet descriptor_write{}; |
| 89 | + descriptor_write.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET; |
| 90 | + descriptor_write.dstSet = nullptr; |
| 91 | + descriptor_write.dstBinding = binding; |
| 92 | + descriptor_write.dstArrayElement = 0; |
| 93 | + descriptor_write.descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER; |
| 94 | + descriptor_write.descriptorCount = 1; |
| 95 | + descriptor_write.pBufferInfo = &m_descriptor_buffer_infos.back(); |
| 96 | + |
| 97 | + m_write_sets.push_back(descriptor_write); |
| 98 | + |
| 99 | + return *this; |
| 100 | +} |
| 101 | + |
| 102 | +} // namespace inexor::vulkan_renderer::wrapper |
0 commit comments