1 /*-------------------------------------------------------------------------
5 * Copyright (c) 2015 Google Inc.
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
21 * \brief Vulkan object builder utilities.
22 *//*--------------------------------------------------------------------*/
24 #include "vkBuilderUtil.hpp"
26 #include "vkRefUtil.hpp"
31 // DescriptorSetLayoutBuilder
33 DescriptorSetLayoutBuilder::DescriptorSetLayoutBuilder (void)
37 DescriptorSetLayoutBuilder& DescriptorSetLayoutBuilder::addBinding (VkDescriptorType descriptorType,
38 deUint32 descriptorCount,
39 VkShaderStageFlags stageFlags,
40 const VkSampler* pImmutableSamplers)
42 if (pImmutableSamplers)
44 const ImmutableSamplerInfo immutableSamplerInfo =
46 (deUint32)m_bindings.size(),
47 (deUint32)m_immutableSamplers.size()
50 m_immutableSamplerInfos.push_back(immutableSamplerInfo);
52 for (size_t descriptorNdx = 0; descriptorNdx < descriptorCount; descriptorNdx++)
53 m_immutableSamplers.push_back(pImmutableSamplers[descriptorNdx]);
56 // pImmutableSamplers will be updated at build time
57 const VkDescriptorSetLayoutBinding binding =
59 (deUint32)m_bindings.size(), // binding
60 descriptorType, // descriptorType
61 descriptorCount, // descriptorCount
62 stageFlags, // stageFlags
63 DE_NULL, // pImmutableSamplers
65 m_bindings.push_back(binding);
69 Move<VkDescriptorSetLayout> DescriptorSetLayoutBuilder::build (const DeviceInterface& vk, VkDevice device) const
71 // Create new layout bindings with pImmutableSamplers updated
72 std::vector<VkDescriptorSetLayoutBinding> bindings = m_bindings;
74 for (size_t samplerInfoNdx = 0; samplerInfoNdx < m_immutableSamplerInfos.size(); samplerInfoNdx++)
76 const ImmutableSamplerInfo& samplerInfo = m_immutableSamplerInfos[samplerInfoNdx];
78 bindings[samplerInfo.bindingIndex].pImmutableSamplers = &m_immutableSamplers[samplerInfo.samplerBaseIndex];
81 const VkDescriptorSetLayoutCreateInfo createInfo =
83 VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO,
85 (VkDescriptorSetLayoutCreateFlags)0, // flags
86 (deUint32)bindings.size(), // bindingCount
87 (bindings.empty()) ? (DE_NULL) : (bindings.data()), // pBinding
90 return createDescriptorSetLayout(vk, device, &createInfo);
93 // DescriptorPoolBuilder
95 DescriptorPoolBuilder::DescriptorPoolBuilder (void)
99 DescriptorPoolBuilder& DescriptorPoolBuilder::addType (VkDescriptorType type, deUint32 numDescriptors)
101 if (numDescriptors == 0u)
108 for (size_t ndx = 0; ndx < m_counts.size(); ++ndx)
110 if (m_counts[ndx].type == type)
112 // augment existing requirement
113 m_counts[ndx].descriptorCount += numDescriptors;
120 const VkDescriptorPoolSize typeCount =
123 numDescriptors, // numDescriptors
126 m_counts.push_back(typeCount);
132 Move<VkDescriptorPool> DescriptorPoolBuilder::build (const DeviceInterface& vk, VkDevice device, VkDescriptorPoolCreateFlags flags, deUint32 maxSets) const
134 const VkDescriptorPoolSize* const typeCountPtr = (m_counts.empty()) ? (DE_NULL) : (&m_counts[0]);
135 const VkDescriptorPoolCreateInfo createInfo =
137 VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO,
141 (deUint32)m_counts.size(), // poolSizeCount
142 typeCountPtr, // pPoolSizes
145 return createDescriptorPool(vk, device, &createInfo);
148 // DescriptorSetUpdateBuilder
150 DescriptorSetUpdateBuilder::DescriptorSetUpdateBuilder (void)
154 DescriptorSetUpdateBuilder& DescriptorSetUpdateBuilder::write (VkDescriptorSet destSet,
155 deUint32 destBinding,
156 deUint32 destArrayElement,
158 VkDescriptorType descriptorType,
159 const VkDescriptorImageInfo* pImageInfo,
160 const VkDescriptorBufferInfo* pBufferInfo,
161 const VkBufferView* pTexelBufferView)
163 // pImageInfo, pBufferInfo and pTexelBufferView will be updated when calling update()
164 const VkWriteDescriptorSet writeParams =
166 VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET,
168 destSet, //!< destSet
169 destBinding, //!< destBinding
170 destArrayElement, //!< destArrayElement
172 descriptorType, //!< descriptorType
178 m_writes.push_back(writeParams);
180 // Store a copy of pImageInfo, pBufferInfo and pTexelBufferView
181 WriteDescriptorInfo writeInfo;
184 writeInfo.imageInfos.insert(writeInfo.imageInfos.end(), pImageInfo, pImageInfo + count);
187 writeInfo.bufferInfos.insert(writeInfo.bufferInfos.end(), pBufferInfo, pBufferInfo + count);
189 if (pTexelBufferView)
190 writeInfo.texelBufferViews.insert(writeInfo.texelBufferViews.end(), pTexelBufferView, pTexelBufferView + count);
192 m_writeDescriptorInfos.push_back(writeInfo);
197 DescriptorSetUpdateBuilder& DescriptorSetUpdateBuilder::copy (VkDescriptorSet srcSet,
199 deUint32 srcArrayElement,
200 VkDescriptorSet destSet,
201 deUint32 destBinding,
202 deUint32 destArrayElement,
205 const VkCopyDescriptorSet copyParams =
207 VK_STRUCTURE_TYPE_COPY_DESCRIPTOR_SET,
210 srcBinding, //!< srcBinding
211 srcArrayElement, //!< srcArrayElement
212 destSet, //!< destSet
213 destBinding, //!< destBinding
214 destArrayElement, //!< destArrayElement
217 m_copies.push_back(copyParams);
221 void DescriptorSetUpdateBuilder::update (const DeviceInterface& vk, VkDevice device) const
223 // Update VkWriteDescriptorSet structures with stored info
224 std::vector<VkWriteDescriptorSet> writes = m_writes;
226 for (size_t writeNdx = 0; writeNdx < m_writes.size(); writeNdx++)
228 const WriteDescriptorInfo& writeInfo = m_writeDescriptorInfos[writeNdx];
230 if (!writeInfo.imageInfos.empty())
231 writes[writeNdx].pImageInfo = &writeInfo.imageInfos[0];
233 if (!writeInfo.bufferInfos.empty())
234 writes[writeNdx].pBufferInfo = &writeInfo.bufferInfos[0];
236 if (!writeInfo.texelBufferViews.empty())
237 writes[writeNdx].pTexelBufferView = &writeInfo.texelBufferViews[0];
240 const VkWriteDescriptorSet* const writePtr = (m_writes.empty()) ? (DE_NULL) : (&writes[0]);
241 const VkCopyDescriptorSet* const copyPtr = (m_copies.empty()) ? (DE_NULL) : (&m_copies[0]);
243 vk.updateDescriptorSets(device, (deUint32)writes.size(), writePtr, (deUint32)m_copies.size(), copyPtr);