Vulkan Schnee 0.0.1
High-performance rendering engine
Loading...
Searching...
No Matches
DescriptorSetBuilder.cpp
Go to the documentation of this file.
2#include <cassert>
3
4namespace EngineCore {
5
9
10 DescriptorSetUpdater& DescriptorSetUpdater::addStorageBuffer(uint32_t binding, const VkDescriptorBufferInfo *pBufferInfo)
11 {
12 // Store a copy of the buffer info to ensure it outlives this call
13 bufferInfoStorage.push_back(*pBufferInfo);
14 writeDescriptorSets.push_back({
15 .sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET,
16 .dstSet = dstSet,
17 .dstBinding = binding,
18 .dstArrayElement = 0,
19 .descriptorCount = 1u,
20 .descriptorType = VK_DESCRIPTOR_TYPE_STORAGE_BUFFER,
21 .pBufferInfo = &bufferInfoStorage.back(),
22 });
23 return *this;
24 }
25
26 DescriptorSetUpdater & DescriptorSetUpdater::addUniformBuffer(uint32_t binding, const VkDescriptorBufferInfo *pBufferInfo)
27 {
28 // Store a copy of the buffer info to ensure it outlives this call
29 bufferInfoStorage.push_back(*pBufferInfo);
30 writeDescriptorSets.push_back({
31 .sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET,
32 .dstSet = dstSet,
33 .dstBinding = binding,
34 .dstArrayElement = 0,
35 .descriptorCount = 1u,
36 .descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
37 .pBufferInfo = &bufferInfoStorage.back(),
38 });
39 return *this;
40 }
41
42 DescriptorSetUpdater& DescriptorSetUpdater::addStorageBuffer(uint32_t binding, const VkDescriptorBufferInfo& bufferInfo)
43 {
44 // Store a copy of the buffer info to ensure it outlives this call
45 bufferInfoStorage.push_back(bufferInfo);
46 writeDescriptorSets.push_back({
47 .sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET,
48 .dstSet = dstSet,
49 .dstBinding = binding,
50 .dstArrayElement = 0,
51 .descriptorCount = 1u,
52 .descriptorType = VK_DESCRIPTOR_TYPE_STORAGE_BUFFER,
53 .pBufferInfo = &bufferInfoStorage.back(),
54 });
55 return *this;
56 }
57
58 DescriptorSetUpdater & DescriptorSetUpdater::addUniformBuffer(uint32_t binding, const VkDescriptorBufferInfo& bufferInfo)
59 {
60 // Store a copy of the buffer info to ensure it outlives this call
61 bufferInfoStorage.push_back(bufferInfo);
62 writeDescriptorSets.push_back({
63 .sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET,
64 .dstSet = dstSet,
65 .dstBinding = binding,
66 .dstArrayElement = 0,
67 .descriptorCount = 1u,
68 .descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
69 .pBufferInfo = &bufferInfoStorage.back(),
70 });
71 return *this;
72 }
73
74 DescriptorSetUpdater & DescriptorSetUpdater::addTextureArrayIf(const bool condition, uint32_t binding, std::vector<VkDescriptorImageInfo>* imageInfos) {
75 if (condition) {
76 writeDescriptorSets.push_back(
77 {
78 .sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET,
79 .dstSet = dstSet,
80 .dstBinding = binding,
81 .dstArrayElement = 0,
82 .descriptorCount = static_cast<uint32_t>(imageInfos->size()),
83 .descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
84 .pImageInfo = imageInfos->data(),
85 .pBufferInfo = nullptr,
86 });
87 }
88 return *this;
89 }
90
91 DescriptorSetUpdater & DescriptorSetUpdater::addCombinedImageSampler(uint32_t binding, const VkDescriptorImageInfo* pImageInfo) {
92 // Store a copy of the image info to ensure it outlives this call
93 imageInfoStorage.push_back(*pImageInfo);
94 writeDescriptorSets.push_back({
95 .sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET,
96 .dstSet = dstSet,
97 .dstBinding = binding,
98 .dstArrayElement = 0,
99 .descriptorCount = 1u,
100 .descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
101 .pImageInfo = &imageInfoStorage.back(),
102 });
103 return *this;
104 }
105
106 DescriptorSetUpdater & DescriptorSetUpdater::addStorageImage(uint32_t binding, const VkDescriptorImageInfo* pImageInfo) {
107 // Store a copy of the image info to ensure it outlives this call
108 imageInfoStorage.push_back(*pImageInfo);
109 writeDescriptorSets.push_back({
110 .sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET,
111 .dstSet = dstSet,
112 .dstBinding = binding,
113 .dstArrayElement = 0,
114 .descriptorCount = 1u,
115 .descriptorType = VK_DESCRIPTOR_TYPE_STORAGE_IMAGE,
116 .pImageInfo = &imageInfoStorage.back(),
117 });
118 return *this;
119 }
120
121 void DescriptorSetUpdater::update(VkDevice device) const {
122 assert( device );
123 vkUpdateDescriptorSets(device, static_cast<uint32_t>(writeDescriptorSets.size()), writeDescriptorSets.data(), 0, nullptr);
124 }
125
126 DescriptorSetLayoutBuilder & DescriptorSetLayoutBuilder::addStorageBuffer(uint32_t binding, VkShaderStageFlags stageFlags) {
127 layoutBindings.push_back({
128 .binding = binding,
129 .descriptorType = VK_DESCRIPTOR_TYPE_STORAGE_BUFFER,
130 .descriptorCount = 1u,
131 .stageFlags = stageFlags,
132 });
133 return *this;
134 }
135
137 VkShaderStageFlags stageFlags) {
138 layoutBindings.push_back({
139 .binding = binding,
140 .descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
141 .descriptorCount = 1u,
142 .stageFlags = stageFlags,
143 });
144 return *this;
145 }
146
148 VkShaderStageFlags stageFlags) {
149 layoutBindings.push_back({
150 .binding = binding,
151 .descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
152 .descriptorCount = 1u,
153 .stageFlags = stageFlags,
154 });
155 return *this;
156 }
157
159 VkShaderStageFlags stageFlags) {
160 layoutBindings.push_back({
161 .binding = binding,
162 .descriptorType = VK_DESCRIPTOR_TYPE_STORAGE_IMAGE,
163 .descriptorCount = 1u,
164 .stageFlags = stageFlags,
165 });
166 return *this;
167 }
168
170 uint32_t count, VkShaderStageFlags stageFlags) {
171 layoutBindings.push_back({
172 .binding = binding,
173 .descriptorType = VK_DESCRIPTOR_TYPE_STORAGE_IMAGE,
174 .descriptorCount = count,
175 .stageFlags = stageFlags,
176 });
177 return *this;
178 }
179
180 std::vector<VkDescriptorSetLayoutBinding> DescriptorSetLayoutBuilder::build() {
181 return layoutBindings;
182 }
183}
184
185
186
187
188
std::vector< VkDescriptorSetLayoutBinding > build()
DescriptorSetLayoutBuilder & addStorageBuffer(uint32_t binding, VkShaderStageFlags stageFlags)
DescriptorSetLayoutBuilder & addCombinedImageSampler(uint32_t binding, VkShaderStageFlags stageFlags)
DescriptorSetLayoutBuilder & addUniformBuffer(uint32_t binding, VkShaderStageFlags stageFlags)
DescriptorSetLayoutBuilder & addStorageImageArray(uint32_t binding, uint32_t count, VkShaderStageFlags stageFlags)
DescriptorSetLayoutBuilder & addStorageImage(uint32_t binding, VkShaderStageFlags stageFlags)
std::vector< VkDescriptorSetLayoutBinding > layoutBindings
void update(VkDevice device) const
Updates the bindings on a descriptor sets.
std::deque< VkDescriptorBufferInfo > bufferInfoStorage
DescriptorSetUpdater(VkDescriptorSet dstSet)
std::vector< VkWriteDescriptorSet > writeDescriptorSets
DescriptorSetUpdater & addCombinedImageSampler(uint32_t binding, const VkDescriptorImageInfo *pImageInfo)
Adds a combined image sampler to the chain of updates.
DescriptorSetUpdater & addUniformBuffer(uint32_t binding, const VkDescriptorBufferInfo *pBufferInfo)
Adds a uniform buffer (glsl std140 buffer) to the chain of updates to execute.
std::deque< VkDescriptorImageInfo > imageInfoStorage
DescriptorSetUpdater & addStorageBuffer(uint32_t binding, const VkDescriptorBufferInfo *pBufferInfo)
Adds a storage buffer (glsl std430 buffer) to the chain of updates to execute.
DescriptorSetUpdater & addTextureArrayIf(const bool condition, uint32_t binding, std::vector< VkDescriptorImageInfo > *imageInfos)
Adds updates to texture array.
DescriptorSetUpdater & addStorageImage(uint32_t binding, const VkDescriptorImageInfo *pImageInfo)
Adds a storage image to the chain of updates.
Log category system implementation.