Fix array stride in uniform buffer for VK_KHR_16bit_storage
[platform/upstream/VK-GL-CTS.git] / external / vulkancts / modules / vulkan / spirv_assembly / vktSpvAsmUtils.hpp
1 #ifndef _VKTSPVASMUTILS_HPP
2 #define _VKTSPVASMUTILS_HPP
3 /*-------------------------------------------------------------------------
4  * Vulkan Conformance Tests
5  * ------------------------
6  *
7  * Copyright (c) 2017 Google Inc.
8  *
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  *
21  *//*!
22  * \file
23  * \brief Utilities for Vulkan SPIR-V assembly tests
24  *//*--------------------------------------------------------------------*/
25
26 #include "vkDefs.hpp"
27 #include "vkMemUtil.hpp"
28 #include "vkRef.hpp"
29 #include "vkTypeUtil.hpp"
30 #include "vktTestCase.hpp"
31
32 #include "deMemory.h"
33 #include "deUniquePtr.hpp"
34 #include "deSharedPtr.hpp"
35
36 #include <string>
37 #include <vector>
38
39 namespace vkt
40 {
41 namespace SpirVAssembly
42 {
43 /*--------------------------------------------------------------------*//*!
44  * \brief Abstract class for an input/output storage buffer object
45  *//*--------------------------------------------------------------------*/
46 class BufferInterface
47 {
48 public:
49         virtual                         ~BufferInterface        (void)                          {}
50
51         virtual void            getBytes                        (std::vector<deUint8>& bytes) const = 0;
52         virtual void            getPackedBytes          (std::vector<deUint8>& bytes) const = 0;
53         virtual size_t          getByteSize                     (void) const = 0;
54 };
55
56 typedef de::SharedPtr<BufferInterface>  BufferSp;
57 typedef de::MovePtr<vk::Allocation>             AllocationMp;
58 typedef de::SharedPtr<vk::Allocation>   AllocationSp;
59
60 class Resource
61 {
62 public:
63         Resource(const BufferSp& buffer_, vk::VkDescriptorType descriptorType_ = vk::VK_DESCRIPTOR_TYPE_STORAGE_BUFFER)
64                 : buffer(buffer_)
65                 , descriptorType(descriptorType_)
66         {
67         }
68
69         virtual const BufferSp&                 getBuffer                       () const                                                        { return buffer; }
70         virtual void                                    getBytes                        (std::vector<deUint8>& bytes) const     { buffer->getBytes(bytes); }
71         virtual size_t                                  getByteSize                     (void) const                                            { return buffer->getByteSize(); }
72
73         virtual void                                    setDescriptorType       (vk::VkDescriptorType type)             { descriptorType = type; }
74         virtual vk::VkDescriptorType    getDescriptorType       ()      const                                           { return descriptorType; }
75
76 private:
77         BufferSp                                buffer;
78         vk::VkDescriptorType    descriptorType;
79 };
80
81 typedef bool (*VerifyIOFunc) (const std::vector<Resource>&              inputs,
82                                                           const std::vector<AllocationSp>&      outputAllocations,
83                                                           const std::vector<Resource>&          expectedOutputs,
84                                                           tcu::TestLog&                                         log);
85
86 struct SpecConstants
87 {
88 public:
89                                                         SpecConstants (void)
90                                                         {}
91
92         bool                                    empty (void) const
93                                                         {
94                                                                 return valuesBuffer.empty();
95                                                         }
96
97         size_t                                  getValuesCount (void) const
98                                                         {
99                                                                 return sizesBuffer.size();
100                                                         }
101
102         size_t                                  getValueSize (const size_t valueIndex) const
103                                                         {
104                                                                 return sizesBuffer[valueIndex];
105                                                         }
106
107         const void*                             getValuesBuffer (void) const
108                                                         {
109                                                                 if (valuesBuffer.size() == 0)
110                                                                         return DE_NULL;
111                                                                 else
112                                                                         return static_cast<const void*>(&valuesBuffer[0]);
113                                                         }
114
115         template<typename T>
116         void                                    append (const T value)
117                                                         {
118                                                                 append(&value, sizeof(value));
119                                                         }
120
121         void                                    append (const void* buf, const size_t byteSize)
122                                                         {
123                                                                 DE_ASSERT(byteSize > 0);
124
125                                                                 valuesBuffer.resize(valuesBuffer.size() + byteSize);
126                                                                 deMemcpy(&valuesBuffer[valuesBuffer.size() - byteSize], buf, byteSize);
127
128                                                                 sizesBuffer.push_back(byteSize);
129                                                         }
130
131 private:
132         std::vector<deUint8>    valuesBuffer;
133         std::vector<size_t>             sizesBuffer;
134 };
135
136 enum Extension8BitStorageFeatureBits
137 {
138         EXT8BITSTORAGEFEATURES_STORAGE_BUFFER                   = (1u << 1),
139         EXT8BITSTORAGEFEATURES_UNIFORM_STORAGE_BUFFER   = (1u << 2),
140         EXT8BITSTORAGEFEATURES_PUSH_CONSTANT                    = (1u << 3),
141 };
142 typedef deUint32 Extension8BitStorageFeatures;
143
144 enum Extension16BitStorageFeatureBits
145 {
146         EXT16BITSTORAGEFEATURES_UNIFORM_BUFFER_BLOCK    = (1u << 1),
147         EXT16BITSTORAGEFEATURES_UNIFORM                                 = (1u << 2),
148         EXT16BITSTORAGEFEATURES_PUSH_CONSTANT                   = (1u << 3),
149         EXT16BITSTORAGEFEATURES_INPUT_OUTPUT                    = (1u << 4),
150 };
151 typedef deUint32 Extension16BitStorageFeatures;
152
153 enum ExtensionVariablePointersFeaturesBits
154 {
155         EXTVARIABLEPOINTERSFEATURES_VARIABLE_POINTERS_STORAGEBUFFER     = (1u << 1),
156         EXTVARIABLEPOINTERSFEATURES_VARIABLE_POINTERS                           = (1u << 2),
157 };
158 typedef deUint32 ExtensionVariablePointersFeatures;
159
160 struct VulkanFeatures
161 {
162         vk::VkPhysicalDeviceFeatures            coreFeatures;
163         Extension16BitStorageFeatures           ext16BitStorage;
164         ExtensionVariablePointersFeatures       extVariablePointers;
165         Extension8BitStorageFeatures            ext8BitStorage;
166
167         VulkanFeatures                          (void)
168                 : coreFeatures                  (vk::VkPhysicalDeviceFeatures())
169                 , ext16BitStorage               (0)
170                 , extVariablePointers   (0)
171                 , ext8BitStorage                (0)
172         {
173                 deMemset(&coreFeatures, 0, sizeof(coreFeatures));
174         }
175 };
176
177 // Returns true if the given 8bit storage extension features in `toCheck` are all supported.
178 bool is8BitStorageFeaturesSupported (const Context&                                             context,
179                                                                           Extension8BitStorageFeatures          toCheck);
180
181 // Returns true if the given 16bit storage extension features in `toCheck` are all supported.
182 bool isCoreFeaturesSupported (const Context&                                            context,
183                                                           const vk::VkPhysicalDeviceFeatures&   toCheck,
184                                                           const char**                                                  missingFeature);
185
186 // Returns true if the given 16bit storage extension features in `toCheck` are all supported.
187 bool is16BitStorageFeaturesSupported (const Context&                            context,
188                                                                           Extension16BitStorageFeatures toCheck);
189
190 // Returns true if the given variable pointers extension features in `toCheck` are all supported.
191 bool isVariablePointersFeaturesSupported (const Context&                                        context,
192                                                                                   ExtensionVariablePointersFeatures     toCheck);
193
194 deUint32 getMinRequiredVulkanVersion (const vk::SpirvVersion version);
195
196 std::string     getVulkanName (const deUint32 version);
197
198 } // SpirVAssembly
199 } // vkt
200
201 #endif // _VKTSPVASMUTILS_HPP