Merge branch 'jekstrand_renderpass_transfer_bit_fix' into 'master'
[platform/upstream/VK-GL-CTS.git] / external / vulkancts / framework / vulkan / vkBuilderUtil.cpp
1 /*-------------------------------------------------------------------------
2  * Vulkan CTS Framework
3  * --------------------
4  *
5  * Copyright (c) 2015 Google Inc.
6  *
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:
14  *
15  * The above copyright notice(s) and this permission notice shall be
16  * included in all copies or substantial portions of the Materials.
17  *
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.
21  *
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.
29  *
30  *//*!
31  * \file
32  * \brief Vulkan object builder utilities.
33  *//*--------------------------------------------------------------------*/
34
35 #include "vkBuilderUtil.hpp"
36
37 #include "vkRefUtil.hpp"
38
39 namespace vk
40 {
41
42 // DescriptorSetLayoutBuilder
43
44 DescriptorSetLayoutBuilder::DescriptorSetLayoutBuilder (void)
45 {
46 }
47
48 DescriptorSetLayoutBuilder& DescriptorSetLayoutBuilder::addBinding (VkDescriptorType    descriptorType,
49                                                                                                                                         deUint32                        descriptorCount,
50                                                                                                                                         VkShaderStageFlags      stageFlags,
51                                                                                                                                         const VkSampler*        pImmutableSamplers)
52 {
53         if (pImmutableSamplers)
54         {
55                 const ImmutableSamplerInfo immutableSamplerInfo =
56                 {
57                         (deUint32)m_bindings.size(),
58                         (deUint32)m_immutableSamplers.size()
59                 };
60
61                 m_immutableSamplerInfos.push_back(immutableSamplerInfo);
62
63                 for (size_t descriptorNdx = 0; descriptorNdx < descriptorCount; descriptorNdx++)
64                         m_immutableSamplers.push_back(pImmutableSamplers[descriptorNdx]);
65         }
66
67         // pImmutableSamplers will be updated at build time
68         const VkDescriptorSetLayoutBinding binding =
69         {
70                 (deUint32)m_bindings.size(),    // binding
71                 descriptorType,                                 // descriptorType
72                 descriptorCount,                                // descriptorCount
73                 stageFlags,                                             // stageFlags
74                 DE_NULL,                                                // pImmutableSamplers
75         };
76         m_bindings.push_back(binding);
77         return *this;
78 }
79
80 Move<VkDescriptorSetLayout> DescriptorSetLayoutBuilder::build (const DeviceInterface& vk, VkDevice device) const
81 {
82         // Create new layout bindings with pImmutableSamplers updated
83         std::vector<VkDescriptorSetLayoutBinding>       bindings        = m_bindings;
84
85         for (size_t samplerInfoNdx = 0; samplerInfoNdx < m_immutableSamplerInfos.size(); samplerInfoNdx++)
86         {
87                 const ImmutableSamplerInfo&     samplerInfo     = m_immutableSamplerInfos[samplerInfoNdx];
88
89                 bindings[samplerInfo.bindingIndex].pImmutableSamplers   = &m_immutableSamplers[samplerInfo.samplerBaseIndex];
90         }
91
92         const VkDescriptorSetLayoutCreateInfo           createInfo      =
93         {
94                 VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO,
95                 DE_NULL,
96                 (VkDescriptorSetLayoutCreateFlags)0,                                    // flags
97                 (deUint32)bindings.size(),                                                              // bindingCount
98                 (bindings.empty()) ? (DE_NULL) : (bindings.data()),             // pBinding
99         };
100
101         return createDescriptorSetLayout(vk, device, &createInfo);
102 }
103
104 // DescriptorPoolBuilder
105
106 DescriptorPoolBuilder::DescriptorPoolBuilder (void)
107 {
108 }
109
110 DescriptorPoolBuilder& DescriptorPoolBuilder::addType (VkDescriptorType type, deUint32 numDescriptors)
111 {
112         if (numDescriptors == 0u)
113         {
114                 // nothing to do
115                 return *this;
116         }
117         else
118         {
119                 for (size_t ndx = 0; ndx < m_counts.size(); ++ndx)
120                 {
121                         if (m_counts[ndx].type == type)
122                         {
123                                 // augment existing requirement
124                                 m_counts[ndx].descriptorCount += numDescriptors;
125                                 return *this;
126                         }
127                 }
128
129                 {
130                         // new requirement
131                         const VkDescriptorPoolSize typeCount =
132                         {
133                                 type,                   // type
134                                 numDescriptors, // numDescriptors
135                         };
136
137                         m_counts.push_back(typeCount);
138                         return *this;
139                 }
140         }
141 }
142
143 Move<VkDescriptorPool> DescriptorPoolBuilder::build (const DeviceInterface& vk, VkDevice device, VkDescriptorPoolCreateFlags flags, deUint32 maxSets) const
144 {
145         const VkDescriptorPoolSize* const       typeCountPtr    = (m_counts.empty()) ? (DE_NULL) : (&m_counts[0]);
146         const VkDescriptorPoolCreateInfo        createInfo              =
147         {
148                 VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO,
149                 DE_NULL,
150                 flags,
151                 maxSets,
152                 (deUint32)m_counts.size(),              // poolSizeCount
153                 typeCountPtr,                                   // pPoolSizes
154         };
155
156         return createDescriptorPool(vk, device, &createInfo);
157 }
158
159 // DescriptorSetUpdateBuilder
160
161 DescriptorSetUpdateBuilder::DescriptorSetUpdateBuilder (void)
162 {
163 }
164
165 DescriptorSetUpdateBuilder& DescriptorSetUpdateBuilder::write (VkDescriptorSet                                  destSet,
166                                                                                                                            deUint32                                                     destBinding,
167                                                                                                                            deUint32                                                     destArrayElement,
168                                                                                                                            deUint32                                                     count,
169                                                                                                                            VkDescriptorType                                     descriptorType,
170                                                                                                                            const VkDescriptorImageInfo*         pImageInfo,
171                                                                                                                            const VkDescriptorBufferInfo*        pBufferInfo,
172                                                                                                                            const VkBufferView*                          pTexelBufferView)
173 {
174         // pImageInfo, pBufferInfo and pTexelBufferView will be updated when calling update()
175         const VkWriteDescriptorSet writeParams =
176         {
177                 VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET,
178                 DE_NULL,
179                 destSet,                        //!< destSet
180                 destBinding,            //!< destBinding
181                 destArrayElement,       //!< destArrayElement
182                 count,                          //!< count
183                 descriptorType,         //!< descriptorType
184                 DE_NULL,
185                 DE_NULL,
186                 DE_NULL
187         };
188
189         m_writes.push_back(writeParams);
190
191         // Store a copy of pImageInfo, pBufferInfo and pTexelBufferView
192         WriteDescriptorInfo     writeInfo;
193
194         if (pImageInfo)
195                 writeInfo.imageInfos.insert(writeInfo.imageInfos.end(), pImageInfo, pImageInfo + count);
196
197         if (pBufferInfo)
198                 writeInfo.bufferInfos.insert(writeInfo.bufferInfos.end(), pBufferInfo, pBufferInfo + count);
199
200         if (pTexelBufferView)
201                 writeInfo.texelBufferViews.insert(writeInfo.texelBufferViews.end(), pTexelBufferView, pTexelBufferView + count);
202
203         m_writeDescriptorInfos.push_back(writeInfo);
204
205         return *this;
206 }
207
208 DescriptorSetUpdateBuilder& DescriptorSetUpdateBuilder::copy (VkDescriptorSet   srcSet,
209                                                                                                                           deUint32                      srcBinding,
210                                                                                                                           deUint32                      srcArrayElement,
211                                                                                                                           VkDescriptorSet       destSet,
212                                                                                                                           deUint32                      destBinding,
213                                                                                                                           deUint32                      destArrayElement,
214                                                                                                                           deUint32                      count)
215 {
216         const VkCopyDescriptorSet copyParams =
217         {
218                 VK_STRUCTURE_TYPE_COPY_DESCRIPTOR_SET,
219                 DE_NULL,
220                 srcSet,                         //!< srcSet
221                 srcBinding,                     //!< srcBinding
222                 srcArrayElement,        //!< srcArrayElement
223                 destSet,                        //!< destSet
224                 destBinding,            //!< destBinding
225                 destArrayElement,       //!< destArrayElement
226                 count,                          //!< count
227         };
228         m_copies.push_back(copyParams);
229         return *this;
230 }
231
232 void DescriptorSetUpdateBuilder::update (const DeviceInterface& vk, VkDevice device) const
233 {
234         // Update VkWriteDescriptorSet structures with stored info
235         std::vector<VkWriteDescriptorSet> writes        = m_writes;
236
237         for (size_t writeNdx = 0; writeNdx < m_writes.size(); writeNdx++)
238         {
239                 const WriteDescriptorInfo& writeInfo = m_writeDescriptorInfos[writeNdx];
240
241                 if (!writeInfo.imageInfos.empty())
242                         writes[writeNdx].pImageInfo                     = &writeInfo.imageInfos[0];
243
244                 if (!writeInfo.bufferInfos.empty())
245                         writes[writeNdx].pBufferInfo            = &writeInfo.bufferInfos[0];
246
247                 if (!writeInfo.texelBufferViews.empty())
248                         writes[writeNdx].pTexelBufferView       = &writeInfo.texelBufferViews[0];
249         }
250
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]);
253
254         vk.updateDescriptorSets(device, (deUint32)writes.size(), writePtr, (deUint32)m_copies.size(), copyPtr);
255 }
256
257 } // vk