Refactor: Compatible compute and graphics VerifyIO
[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 size_t          getByteSize                     (void) const = 0;
53 };
54
55 typedef de::SharedPtr<BufferInterface>  BufferSp;
56 typedef de::MovePtr<vk::Allocation>             AllocationMp;
57 typedef de::SharedPtr<vk::Allocation>   AllocationSp;
58
59 class Resource
60 {
61 public:
62         Resource(const BufferSp& buffer_, vk::VkDescriptorType descriptorType_ = vk::VK_DESCRIPTOR_TYPE_STORAGE_BUFFER)
63                 : buffer(buffer_)
64                 , descriptorType(descriptorType_)
65         {
66         }
67
68         virtual const BufferSp&                 getBuffer                       () const                                                        { return buffer; }
69         virtual void                                    getBytes                        (std::vector<deUint8>& bytes) const     { buffer->getBytes(bytes); }
70         virtual size_t                                  getByteSize                     (void) const                                            { return buffer->getByteSize(); }
71
72         virtual void                                    setDescriptorType       (vk::VkDescriptorType type)             { descriptorType = type; }
73         virtual vk::VkDescriptorType    getDescriptorType       ()      const                                           { return descriptorType; }
74
75 private:
76         BufferSp                                buffer;
77         vk::VkDescriptorType    descriptorType;
78 };
79
80 typedef bool (*VerifyIOFunc) (const std::vector<Resource>&              inputs,
81                                                           const std::vector<AllocationSp>&      outputAllocations,
82                                                           const std::vector<Resource>&          expectedOutputs,
83                                                           tcu::TestLog&                                         log);
84
85 struct SpecConstants
86 {
87 public:
88                                                         SpecConstants (void)
89                                                         {}
90
91         bool                                    empty (void) const
92                                                         {
93                                                                 return valuesBuffer.empty();
94                                                         }
95
96         size_t                                  getValuesCount (void) const
97                                                         {
98                                                                 return sizesBuffer.size();
99                                                         }
100
101         size_t                                  getValueSize (const size_t valueIndex) const
102                                                         {
103                                                                 return sizesBuffer[valueIndex];
104                                                         }
105
106         const void*                             getValuesBuffer (void) const
107                                                         {
108                                                                 if (valuesBuffer.size() == 0)
109                                                                         return DE_NULL;
110                                                                 else
111                                                                         return static_cast<const void*>(&valuesBuffer[0]);
112                                                         }
113
114         template<typename T>
115         void                                    append (const T value)
116                                                         {
117                                                                 append(&value, sizeof(value));
118                                                         }
119
120         void                                    append (const void* buf, const size_t byteSize)
121                                                         {
122                                                                 DE_ASSERT(byteSize > 0);
123
124                                                                 valuesBuffer.resize(valuesBuffer.size() + byteSize);
125                                                                 deMemcpy(&valuesBuffer[valuesBuffer.size() - byteSize], buf, byteSize);
126
127                                                                 sizesBuffer.push_back(byteSize);
128                                                         }
129
130 private:
131         std::vector<deUint8>    valuesBuffer;
132         std::vector<size_t>             sizesBuffer;
133 };
134
135 enum Extension8BitStorageFeatureBits
136 {
137         EXT8BITSTORAGEFEATURES_STORAGE_BUFFER                   = (1u << 1),
138         EXT8BITSTORAGEFEATURES_UNIFORM_STORAGE_BUFFER   = (1u << 2),
139         EXT8BITSTORAGEFEATURES_PUSH_CONSTANT                    = (1u << 3),
140 };
141 typedef deUint32 Extension8BitStorageFeatures;
142
143 enum Extension16BitStorageFeatureBits
144 {
145         EXT16BITSTORAGEFEATURES_UNIFORM_BUFFER_BLOCK    = (1u << 1),
146         EXT16BITSTORAGEFEATURES_UNIFORM                                 = (1u << 2),
147         EXT16BITSTORAGEFEATURES_PUSH_CONSTANT                   = (1u << 3),
148         EXT16BITSTORAGEFEATURES_INPUT_OUTPUT                    = (1u << 4),
149 };
150 typedef deUint32 Extension16BitStorageFeatures;
151
152 enum ExtensionVariablePointersFeaturesBits
153 {
154         EXTVARIABLEPOINTERSFEATURES_VARIABLE_POINTERS_STORAGEBUFFER     = (1u << 1),
155         EXTVARIABLEPOINTERSFEATURES_VARIABLE_POINTERS                           = (1u << 2),
156 };
157 typedef deUint32 ExtensionVariablePointersFeatures;
158
159 struct VulkanFeatures
160 {
161         vk::VkPhysicalDeviceFeatures            coreFeatures;
162         Extension16BitStorageFeatures           ext16BitStorage;
163         ExtensionVariablePointersFeatures       extVariablePointers;
164         Extension8BitStorageFeatures            ext8BitStorage;
165
166         VulkanFeatures                          (void)
167                 : coreFeatures                  (vk::VkPhysicalDeviceFeatures())
168                 , ext16BitStorage               (0)
169                 , extVariablePointers   (0)
170                 , ext8BitStorage                (0)
171         {
172                 deMemset(&coreFeatures, 0, sizeof(coreFeatures));
173         }
174 };
175
176 // Returns true if the given 8bit storage extension features in `toCheck` are all supported.
177 bool is8BitStorageFeaturesSupported (const Context&                                             context,
178                                                                           Extension8BitStorageFeatures          toCheck);
179
180 // Returns true if the given 16bit storage extension features in `toCheck` are all supported.
181 bool isCoreFeaturesSupported (const Context&                                            context,
182                                                           const vk::VkPhysicalDeviceFeatures&   toCheck,
183                                                           const char**                                                  missingFeature);
184
185 // Returns true if the given 16bit storage extension features in `toCheck` are all supported.
186 bool is16BitStorageFeaturesSupported (const Context&                            context,
187                                                                           Extension16BitStorageFeatures toCheck);
188
189 // Returns true if the given variable pointers extension features in `toCheck` are all supported.
190 bool isVariablePointersFeaturesSupported (const Context&                                        context,
191                                                                                   ExtensionVariablePointersFeatures     toCheck);
192
193 deUint32 getMinRequiredVulkanVersion (const vk::SpirvVersion version);
194
195 std::string     getVulkanName (const deUint32 version);
196
197 } // SpirVAssembly
198 } // vkt
199
200 #endif // _VKTSPVASMUTILS_HPP