dEQP-VK.renderpass: Set IMAGE_USAGE_TRANSFER_SRC_BIT when needed
[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         const VkDescriptorSetLayoutBinding binding =
54         {
55                 (deUint32)m_bindings.size(),    // binding
56                 descriptorType,                                 // descriptorType
57                 descriptorCount,                                // descriptorCount
58                 stageFlags,                                             // stageFlags
59                 pImmutableSamplers,                             // pImmutableSamplers
60         };
61         m_bindings.push_back(binding);
62         return *this;
63 }
64
65 Move<VkDescriptorSetLayout> DescriptorSetLayoutBuilder::build (const DeviceInterface& vk, VkDevice device) const
66 {
67         const VkDescriptorSetLayoutBinding* const       bindingPtr      = (m_bindings.empty()) ? (DE_NULL) : (&m_bindings[0]);
68         const VkDescriptorSetLayoutCreateInfo           createInfo      =
69         {
70                 VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO,
71                 DE_NULL,
72                 (VkDescriptorSetLayoutCreateFlags)0,    // flags
73                 (deUint32)m_bindings.size(),                    // bindingCount
74                 bindingPtr,                                                             // pBinding
75         };
76
77         return createDescriptorSetLayout(vk, device, &createInfo);
78 }
79
80 // DescriptorPoolBuilder
81
82 DescriptorPoolBuilder::DescriptorPoolBuilder (void)
83 {
84 }
85
86 DescriptorPoolBuilder& DescriptorPoolBuilder::addType (VkDescriptorType type, deUint32 numDescriptors)
87 {
88         if (numDescriptors == 0u)
89         {
90                 // nothing to do
91                 return *this;
92         }
93         else
94         {
95                 for (size_t ndx = 0; ndx < m_counts.size(); ++ndx)
96                 {
97                         if (m_counts[ndx].type == type)
98                         {
99                                 // augment existing requirement
100                                 m_counts[ndx].descriptorCount += numDescriptors;
101                                 return *this;
102                         }
103                 }
104
105                 {
106                         // new requirement
107                         const VkDescriptorPoolSize typeCount =
108                         {
109                                 type,                   // type
110                                 numDescriptors, // numDescriptors
111                         };
112
113                         m_counts.push_back(typeCount);
114                         return *this;
115                 }
116         }
117 }
118
119 Move<VkDescriptorPool> DescriptorPoolBuilder::build (const DeviceInterface& vk, VkDevice device, VkDescriptorPoolCreateFlags flags, deUint32 maxSets) const
120 {
121         const VkDescriptorPoolSize* const       typeCountPtr    = (m_counts.empty()) ? (DE_NULL) : (&m_counts[0]);
122         const VkDescriptorPoolCreateInfo        createInfo              =
123         {
124                 VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO,
125                 DE_NULL,
126                 flags,
127                 maxSets,
128                 (deUint32)m_counts.size(),              // poolSizeCount
129                 typeCountPtr,                                   // pPoolSizes
130         };
131
132         return createDescriptorPool(vk, device, &createInfo);
133 }
134
135 // DescriptorSetUpdateBuilder
136
137 DescriptorSetUpdateBuilder::DescriptorSetUpdateBuilder (void)
138 {
139 }
140
141 DescriptorSetUpdateBuilder& DescriptorSetUpdateBuilder::write (VkDescriptorSet                                  destSet,
142                                                                                                                            deUint32                                                     destBinding,
143                                                                                                                            deUint32                                                     destArrayElement,
144                                                                                                                            deUint32                                                     count,
145                                                                                                                            VkDescriptorType                                     descriptorType,
146                                                                                                                            const VkDescriptorImageInfo*         pImageInfo,
147                                                                                                                            const VkDescriptorBufferInfo*        pBufferInfo,
148                                                                                                                            const VkBufferView*                          pTexelBufferView)
149 {
150         const VkWriteDescriptorSet writeParams =
151         {
152                 VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET,
153                 DE_NULL,
154                 destSet,                        //!< destSet
155                 destBinding,            //!< destBinding
156                 destArrayElement,       //!< destArrayElement
157                 count,                          //!< count
158                 descriptorType,         //!< descriptorType
159                 pImageInfo,
160                 pBufferInfo,
161                 pTexelBufferView
162         };
163         m_writes.push_back(writeParams);
164         return *this;
165 }
166
167 DescriptorSetUpdateBuilder& DescriptorSetUpdateBuilder::copy (VkDescriptorSet   srcSet,
168                                                                                                                           deUint32                      srcBinding,
169                                                                                                                           deUint32                      srcArrayElement,
170                                                                                                                           VkDescriptorSet       destSet,
171                                                                                                                           deUint32                      destBinding,
172                                                                                                                           deUint32                      destArrayElement,
173                                                                                                                           deUint32                      count)
174 {
175         const VkCopyDescriptorSet copyParams =
176         {
177                 VK_STRUCTURE_TYPE_COPY_DESCRIPTOR_SET,
178                 DE_NULL,
179                 srcSet,                         //!< srcSet
180                 srcBinding,                     //!< srcBinding
181                 srcArrayElement,        //!< srcArrayElement
182                 destSet,                        //!< destSet
183                 destBinding,            //!< destBinding
184                 destArrayElement,       //!< destArrayElement
185                 count,                          //!< count
186         };
187         m_copies.push_back(copyParams);
188         return *this;
189 }
190
191 void DescriptorSetUpdateBuilder::update (const DeviceInterface& vk, VkDevice device) const
192 {
193         const VkWriteDescriptorSet* const       writePtr        = (m_writes.empty()) ? (DE_NULL) : (&m_writes[0]);
194         const VkCopyDescriptorSet* const        copyPtr         = (m_copies.empty()) ? (DE_NULL) : (&m_copies[0]);
195
196         vk.updateDescriptorSets(device, (deUint32)m_writes.size(), writePtr, (deUint32)m_copies.size(), copyPtr);
197 }
198
199 } // vk