1 /*-------------------------------------------------------------------------
5 * Copyright (c) 2015 Google Inc.
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and/or associated documentation files (the
9 * "Materials"), to deal in the Materials without restriction, including
10 * without limitation the rights to use, copy, modify, merge, publish,
11 * distribute, sublicense, and/or sell copies of the Materials, and to
12 * permit persons to whom the Materials are furnished to do so, subject to
13 * the following conditions:
15 * The above copyright notice(s) and this permission notice shall be
16 * included in all copies or substantial portions of the Materials.
18 * The Materials are Confidential Information as defined by the
19 * Khronos Membership Agreement until designated non-confidential by
20 * Khronos, at which point this condition clause shall be removed.
22 * THE MATERIALS ARE PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
25 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
26 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
27 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
28 * MATERIALS OR THE USE OR OTHER DEALINGS IN THE MATERIALS.
32 * \brief Vulkan object builder utilities.
33 *//*--------------------------------------------------------------------*/
35 #include "vkBuilderUtil.hpp"
37 #include "vkRefUtil.hpp"
42 // DescriptorSetLayoutBuilder
44 DescriptorSetLayoutBuilder::DescriptorSetLayoutBuilder (void)
48 DescriptorSetLayoutBuilder& DescriptorSetLayoutBuilder::addBinding (VkDescriptorType descriptorType,
49 deUint32 descriptorCount,
50 VkShaderStageFlags stageFlags,
51 const VkSampler* pImmutableSamplers)
53 if (pImmutableSamplers)
55 const ImmutableSamplerInfo immutableSamplerInfo =
57 (deUint32)m_bindings.size(),
58 (deUint32)m_immutableSamplers.size()
61 m_immutableSamplerInfos.push_back(immutableSamplerInfo);
63 for (size_t descriptorNdx = 0; descriptorNdx < descriptorCount; descriptorNdx++)
64 m_immutableSamplers.push_back(pImmutableSamplers[descriptorNdx]);
67 // pImmutableSamplers will be updated at build time
68 const VkDescriptorSetLayoutBinding binding =
70 (deUint32)m_bindings.size(), // binding
71 descriptorType, // descriptorType
72 descriptorCount, // descriptorCount
73 stageFlags, // stageFlags
74 DE_NULL, // pImmutableSamplers
76 m_bindings.push_back(binding);
80 Move<VkDescriptorSetLayout> DescriptorSetLayoutBuilder::build (const DeviceInterface& vk, VkDevice device) const
82 // Create new layout bindings with pImmutableSamplers updated
83 std::vector<VkDescriptorSetLayoutBinding> bindings = m_bindings;
85 for (size_t samplerInfoNdx = 0; samplerInfoNdx < m_immutableSamplerInfos.size(); samplerInfoNdx++)
87 const ImmutableSamplerInfo& samplerInfo = m_immutableSamplerInfos[samplerInfoNdx];
89 bindings[samplerInfo.bindingIndex].pImmutableSamplers = &m_immutableSamplers[samplerInfo.samplerBaseIndex];
92 const VkDescriptorSetLayoutCreateInfo createInfo =
94 VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO,
96 (VkDescriptorSetLayoutCreateFlags)0, // flags
97 (deUint32)bindings.size(), // bindingCount
98 (bindings.empty()) ? (DE_NULL) : (bindings.data()), // pBinding
101 return createDescriptorSetLayout(vk, device, &createInfo);
104 // DescriptorPoolBuilder
106 DescriptorPoolBuilder::DescriptorPoolBuilder (void)
110 DescriptorPoolBuilder& DescriptorPoolBuilder::addType (VkDescriptorType type, deUint32 numDescriptors)
112 if (numDescriptors == 0u)
119 for (size_t ndx = 0; ndx < m_counts.size(); ++ndx)
121 if (m_counts[ndx].type == type)
123 // augment existing requirement
124 m_counts[ndx].descriptorCount += numDescriptors;
131 const VkDescriptorPoolSize typeCount =
134 numDescriptors, // numDescriptors
137 m_counts.push_back(typeCount);
143 Move<VkDescriptorPool> DescriptorPoolBuilder::build (const DeviceInterface& vk, VkDevice device, VkDescriptorPoolCreateFlags flags, deUint32 maxSets) const
145 const VkDescriptorPoolSize* const typeCountPtr = (m_counts.empty()) ? (DE_NULL) : (&m_counts[0]);
146 const VkDescriptorPoolCreateInfo createInfo =
148 VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO,
152 (deUint32)m_counts.size(), // poolSizeCount
153 typeCountPtr, // pPoolSizes
156 return createDescriptorPool(vk, device, &createInfo);
159 // DescriptorSetUpdateBuilder
161 DescriptorSetUpdateBuilder::DescriptorSetUpdateBuilder (void)
165 DescriptorSetUpdateBuilder& DescriptorSetUpdateBuilder::write (VkDescriptorSet destSet,
166 deUint32 destBinding,
167 deUint32 destArrayElement,
169 VkDescriptorType descriptorType,
170 const VkDescriptorImageInfo* pImageInfo,
171 const VkDescriptorBufferInfo* pBufferInfo,
172 const VkBufferView* pTexelBufferView)
174 // pImageInfo, pBufferInfo and pTexelBufferView will be updated when calling update()
175 const VkWriteDescriptorSet writeParams =
177 VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET,
179 destSet, //!< destSet
180 destBinding, //!< destBinding
181 destArrayElement, //!< destArrayElement
183 descriptorType, //!< descriptorType
189 m_writes.push_back(writeParams);
191 // Store a copy of pImageInfo, pBufferInfo and pTexelBufferView
192 WriteDescriptorInfo writeInfo;
195 writeInfo.imageInfos.insert(writeInfo.imageInfos.end(), pImageInfo, pImageInfo + count);
198 writeInfo.bufferInfos.insert(writeInfo.bufferInfos.end(), pBufferInfo, pBufferInfo + count);
200 if (pTexelBufferView)
201 writeInfo.texelBufferViews.insert(writeInfo.texelBufferViews.end(), pTexelBufferView, pTexelBufferView + count);
203 m_writeDescriptorInfos.push_back(writeInfo);
208 DescriptorSetUpdateBuilder& DescriptorSetUpdateBuilder::copy (VkDescriptorSet srcSet,
210 deUint32 srcArrayElement,
211 VkDescriptorSet destSet,
212 deUint32 destBinding,
213 deUint32 destArrayElement,
216 const VkCopyDescriptorSet copyParams =
218 VK_STRUCTURE_TYPE_COPY_DESCRIPTOR_SET,
221 srcBinding, //!< srcBinding
222 srcArrayElement, //!< srcArrayElement
223 destSet, //!< destSet
224 destBinding, //!< destBinding
225 destArrayElement, //!< destArrayElement
228 m_copies.push_back(copyParams);
232 void DescriptorSetUpdateBuilder::update (const DeviceInterface& vk, VkDevice device) const
234 // Update VkWriteDescriptorSet structures with stored info
235 std::vector<VkWriteDescriptorSet> writes = m_writes;
237 for (size_t writeNdx = 0; writeNdx < m_writes.size(); writeNdx++)
239 const WriteDescriptorInfo& writeInfo = m_writeDescriptorInfos[writeNdx];
241 if (!writeInfo.imageInfos.empty())
242 writes[writeNdx].pImageInfo = &writeInfo.imageInfos[0];
244 if (!writeInfo.bufferInfos.empty())
245 writes[writeNdx].pBufferInfo = &writeInfo.bufferInfos[0];
247 if (!writeInfo.texelBufferViews.empty())
248 writes[writeNdx].pTexelBufferView = &writeInfo.texelBufferViews[0];
251 const VkWriteDescriptorSet* const writePtr = (m_writes.empty()) ? (DE_NULL) : (&writes[0]);
252 const VkCopyDescriptorSet* const copyPtr = (m_copies.empty()) ? (DE_NULL) : (&m_copies[0]);
254 vk.updateDescriptorSets(device, (deUint32)writes.size(), writePtr, (deUint32)m_copies.size(), copyPtr);