Mandrill 2025.6.0
Loading...
Searching...
No Matches
Descriptor.h
1#pragma once
2
3#include "Common.h"
4
5#include "AccelerationStructure.h"
6#include "Buffer.h"
7#include "Device.h"
8#include "Log.h"
9#include "Texture.h"
10
11namespace Mandrill
12{
13 struct DescriptorDesc {
14 VkDescriptorType type;
15 std::variant<ptr<Buffer>, ptr<Image>, ptr<Texture>, ptr<std::vector<ptr<Texture>>>, ptr<AccelerationStructure>>
16 pResource;
17 VkDeviceSize offset = 0;
18 VkDeviceSize range;
19 VkBufferView bufferView = nullptr;
20 VkImageView imageView = nullptr;
21 VkImageLayout imageLayout = VK_IMAGE_LAYOUT_UNDEFINED;
22 uint32_t arrayCount;
23
24 MANDRILL_API DescriptorDesc(VkDescriptorType type, ptr<void> pResource, VkDeviceSize offset = 0,
25 VkDeviceSize range = VK_WHOLE_SIZE, uint32_t arrayCount = 0)
26 : type(type), offset(offset), range(range), arrayCount(arrayCount)
27 {
28 switch (type) {
29 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER:
30 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER:
31 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC:
32 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC:
33 this->pResource = std::static_pointer_cast<Buffer>(pResource);
34 break;
35 case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE:
36 case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT:
37 this->pResource = std::static_pointer_cast<Image>(pResource);
38 break;
39 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER:
40 if (arrayCount) {
41 this->pResource = std::static_pointer_cast<std::vector<ptr<Texture>>>(pResource);
42 } else {
43 this->pResource = std::static_pointer_cast<Texture>(pResource);
44 }
45 break;
46 case VK_DESCRIPTOR_TYPE_ACCELERATION_STRUCTURE_KHR:
47 this->pResource = std::static_pointer_cast<AccelerationStructure>(pResource);
48 break;
49 default:
50 Log::Error("DescriptorDesc: Resource not supported\n");
51 }
52 }
53
54 MANDRILL_API ~DescriptorDesc()
55 {
56 }
57 };
58
63 {
64 public:
65 MANDRILL_NON_COPYABLE(Descriptor)
66
67
73 MANDRILL_API Descriptor(ptr<Device> pDevice, const std::vector<DescriptorDesc>& desc,
74 VkDescriptorSetLayout layout);
75
79 MANDRILL_API ~Descriptor();
80
90 MANDRILL_API void bind(VkCommandBuffer cmd, VkPipelineBindPoint bindPoint, VkPipelineLayout pipelineLayout,
91 uint32_t firstSet, std::vector<uint32_t> offsets)
92 {
93 vkCmdBindDescriptorSets(cmd, bindPoint, pipelineLayout, firstSet, 1, &mSet, count(offsets), offsets.data());
94 }
95
105 MANDRILL_API void bind(VkCommandBuffer cmd, VkPipelineBindPoint bindPoint, VkPipelineLayout pipelineLayout,
106 uint32_t firstSet, uint32_t offset)
107 {
108 std::vector<uint32_t> offsets = {offset};
109 bind(cmd, bindPoint, pipelineLayout, firstSet, offsets);
110 }
111
120 MANDRILL_API void bind(VkCommandBuffer cmd, VkPipelineBindPoint bindPoint, VkPipelineLayout pipelineLayout,
121 uint32_t firstSet)
122 {
123 vkCmdBindDescriptorSets(cmd, bindPoint, pipelineLayout, firstSet, 1, &mSet, 0, nullptr);
124 }
125
130 MANDRILL_API VkDescriptorSet getSet() const
131 {
132 return mSet;
133 }
134
135 private:
136 void allocate(const std::vector<DescriptorDesc>& desc, VkDescriptorSetLayout layout);
137
138 ptr<Device> mpDevice;
139
140 VkDescriptorPool mPool;
141 VkDescriptorSet mSet;
142 };
143} // namespace Mandrill
Descriptor class for managing Vulkan descriptor sets.
Definition Descriptor.h:63
MANDRILL_API void bind(VkCommandBuffer cmd, VkPipelineBindPoint bindPoint, VkPipelineLayout pipelineLayout, uint32_t firstSet, uint32_t offset)
Bind descriptor with one dynamic offset.
Definition Descriptor.h:105
MANDRILL_API void bind(VkCommandBuffer cmd, VkPipelineBindPoint bindPoint, VkPipelineLayout pipelineLayout, uint32_t firstSet)
Bind descriptor.
Definition Descriptor.h:120
MANDRILL_API ~Descriptor()
Destructor for descriptor.
Definition Descriptor.cpp:88
MANDRILL_API void bind(VkCommandBuffer cmd, VkPipelineBindPoint bindPoint, VkPipelineLayout pipelineLayout, uint32_t firstSet, std::vector< uint32_t > offsets)
Bind descriptor with vector of dynamic offsets.
Definition Descriptor.h:90
MANDRILL_API VkDescriptorSet getSet() const
Get the descriptor set handle.
Definition Descriptor.h:130