1 #ifndef _VKQUERYUTIL_HPP
2 #define _VKQUERYUTIL_HPP
3 /*-------------------------------------------------------------------------
4 * Vulkan CTS Framework
5 * --------------------
6 *
7 * Copyright (c) 2015 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 Vulkan query utilities.
24 *//*--------------------------------------------------------------------*/
25
26 #include "vkDefs.hpp"
27 #include "tcuMaybe.hpp"
28 #include "deMemory.h"
29
30 #include <vector>
31
32 namespace vk
33 {
34
35 // API queries
36
37 std::vector<VkPhysicalDevice> enumeratePhysicalDevices (const InstanceInterface& vk, VkInstance instance);
38 std::vector<VkQueueFamilyProperties> getPhysicalDeviceQueueFamilyProperties (const InstanceInterface& vk, VkPhysicalDevice physicalDevice);
39 VkPhysicalDeviceFeatures getPhysicalDeviceFeatures (const InstanceInterface& vk, VkPhysicalDevice physicalDevice);
40 VkPhysicalDeviceProperties getPhysicalDeviceProperties (const InstanceInterface& vk, VkPhysicalDevice physicalDevice);
41 VkPhysicalDeviceMemoryProperties getPhysicalDeviceMemoryProperties (const InstanceInterface& vk, VkPhysicalDevice physicalDevice);
42 VkFormatProperties getPhysicalDeviceFormatProperties (const InstanceInterface& vk, VkPhysicalDevice physicalDevice, VkFormat format);
43 VkImageFormatProperties getPhysicalDeviceImageFormatProperties (const InstanceInterface& vk, VkPhysicalDevice physicalDevice, VkFormat format, VkImageType type, VkImageTiling tiling, VkImageUsageFlags usage, VkImageCreateFlags flags);
44 std::vector<VkSparseImageFormatProperties> getPhysicalDeviceSparseImageFormatProperties (const InstanceInterface& vk, VkPhysicalDevice physicalDevice, VkFormat format, VkImageType type, VkSampleCountFlagBits samples, VkImageUsageFlags usage, VkImageTiling tiling);
45
46 VkMemoryRequirements getBufferMemoryRequirements (const DeviceInterface& vk, VkDevice device, VkBuffer buffer);
47 VkMemoryRequirements getImageMemoryRequirements (const DeviceInterface& vk, VkDevice device, VkImage image);
48 VkMemoryRequirements getImagePlaneMemoryRequirements (const DeviceInterface& vk, VkDevice device, VkImage image, VkImageAspectFlagBits planeAspect);
49 std::vector<VkSparseImageMemoryRequirements> getImageSparseMemoryRequirements (const DeviceInterface& vk, VkDevice device, VkImage image);
50
51 std::vector<VkLayerProperties> enumerateInstanceLayerProperties (const PlatformInterface& vkp);
52 std::vector<VkExtensionProperties> enumerateInstanceExtensionProperties (const PlatformInterface& vkp, const char* layerName);
53 std::vector<VkLayerProperties> enumerateDeviceLayerProperties (const InstanceInterface& vki, VkPhysicalDevice physicalDevice);
54 std::vector<VkExtensionProperties> enumerateDeviceExtensionProperties (const InstanceInterface& vki, VkPhysicalDevice physicalDevice, const char* layerName);
55
56 VkQueue getDeviceQueue (const DeviceInterface& vkd, VkDevice device, deUint32 queueFamilyIndex, deUint32 queueIndex);
57
58 // Feature / extension support
59
60 bool isShaderStageSupported (const VkPhysicalDeviceFeatures& deviceFeatures, VkShaderStageFlagBits stage);
61
62 struct RequiredExtension
63 {
64 std::string name;
65 tcu::Maybe<deUint32> minVersion;
66 tcu::Maybe<deUint32> maxVersion;
67
RequiredExtensionvk::RequiredExtension68 explicit RequiredExtension (const std::string& name_,
69 tcu::Maybe<deUint32> minVersion_ = tcu::nothing<deUint32>(),
70 tcu::Maybe<deUint32> maxVersion_ = tcu::nothing<deUint32>())
71 : name (name_)
72 , minVersion (minVersion_)
73 , maxVersion (maxVersion_)
74 {}
75 };
76
77 struct RequiredLayer
78 {
79 std::string name;
80 tcu::Maybe<deUint32> minSpecVersion;
81 tcu::Maybe<deUint32> maxSpecVersion;
82 tcu::Maybe<deUint32> minImplVersion;
83 tcu::Maybe<deUint32> maxImplVersion;
84
RequiredLayervk::RequiredLayer85 explicit RequiredLayer (const std::string& name_,
86 tcu::Maybe<deUint32> minSpecVersion_ = tcu::nothing<deUint32>(),
87 tcu::Maybe<deUint32> maxSpecVersion_ = tcu::nothing<deUint32>(),
88 tcu::Maybe<deUint32> minImplVersion_ = tcu::nothing<deUint32>(),
89 tcu::Maybe<deUint32> maxImplVersion_ = tcu::nothing<deUint32>())
90 : name (name_)
91 , minSpecVersion(minSpecVersion_)
92 , maxSpecVersion(maxSpecVersion_)
93 , minImplVersion(minImplVersion_)
94 , maxImplVersion(maxImplVersion_)
95 {}
96 };
97
98 bool isCompatible (const VkExtensionProperties& extensionProperties, const RequiredExtension& required);
99 bool isCompatible (const VkLayerProperties& layerProperties, const RequiredLayer& required);
100
101 template<typename ExtensionIterator>
102 bool isExtensionSupported (ExtensionIterator begin, ExtensionIterator end, const RequiredExtension& required);
103 bool isExtensionSupported (const std::vector<VkExtensionProperties>& extensions, const RequiredExtension& required);
104
105 template<typename LayerIterator>
106 bool isLayerSupported (LayerIterator begin, LayerIterator end, const RequiredLayer& required);
107 bool isLayerSupported (const std::vector<VkLayerProperties>& layers, const RequiredLayer& required);
108
109 const void* findStructureInChain (const void* first, VkStructureType type);
110 void* findStructureInChain (void* first, VkStructureType type);
111
112 template<typename StructType>
113 VkStructureType getStructureType (void);
114
115 template<typename StructType>
findStructure(const void * first)116 const StructType* findStructure (const void* first)
117 {
118 return reinterpret_cast<const StructType*>(findStructureInChain(first, getStructureType<StructType>()));
119 }
120
121 template<typename StructType>
findStructure(void * first)122 StructType* findStructure (void* first)
123 {
124 return reinterpret_cast<StructType*>(findStructureInChain(first, getStructureType<StructType>()));
125 }
126
127 namespace ValidateQueryBits
128 {
129
130 typedef struct
131 {
132 size_t offset;
133 size_t size;
134 } QueryMemberTableEntry;
135
136 template <typename Context, typename Interface, typename Type>
137 //!< Return variable initialization validation
validateInitComplete(Context context,void (Interface::* Function)(Context,Type *)const,const Interface & interface,const QueryMemberTableEntry * queryMemberTableEntry)138 bool validateInitComplete(Context context, void (Interface::*Function)(Context, Type*)const, const Interface& interface, const QueryMemberTableEntry* queryMemberTableEntry)
139 {
140 const QueryMemberTableEntry *iterator;
141 Type vec[2];
142 deMemset(&vec[0], 0x00, sizeof(Type));
143 deMemset(&vec[1], 0xFF, sizeof(Type));
144
145 (interface.*Function)(context, &vec[0]);
146 (interface.*Function)(context, &vec[1]);
147
148 for (iterator = queryMemberTableEntry; iterator->size != 0; iterator++)
149 {
150 if (deMemCmp(((deUint8*)(&vec[0]))+iterator->offset, ((deUint8*)(&vec[1]))+iterator->offset, iterator->size) != 0)
151 return false;
152 }
153
154 return true;
155 }
156
157 template<typename IterT>
158 //! Overwrite a range of objects with an 8-bit pattern.
fillBits(IterT beg,const IterT end,const deUint8 pattern=0xdeu)159 inline void fillBits (IterT beg, const IterT end, const deUint8 pattern = 0xdeu)
160 {
161 for (; beg < end; ++beg)
162 deMemset(&(*beg), static_cast<int>(pattern), sizeof(*beg));
163 }
164
165 template<typename IterT>
166 //! Verify that each byte of a range of objects is equal to an 8-bit pattern.
checkBits(IterT beg,const IterT end,const deUint8 pattern=0xdeu)167 bool checkBits (IterT beg, const IterT end, const deUint8 pattern = 0xdeu)
168 {
169 for (; beg < end; ++beg)
170 {
171 const deUint8* elementBytes = reinterpret_cast<const deUint8*>(&(*beg));
172 for (std::size_t i = 0u; i < sizeof(*beg); ++i)
173 {
174 if (elementBytes[i] != pattern)
175 return false;
176 }
177 }
178 return true;
179 }
180
181 } // ValidateQueryBits
182
183 // Template implementations
184
185 template<typename ExtensionIterator>
isExtensionSupported(ExtensionIterator begin,ExtensionIterator end,const RequiredExtension & required)186 bool isExtensionSupported (ExtensionIterator begin, ExtensionIterator end, const RequiredExtension& required)
187 {
188 for (ExtensionIterator cur = begin; cur != end; ++cur)
189 {
190 if (isCompatible(*cur, required))
191 return true;
192 }
193 return false;
194 }
195
196 template<typename LayerIterator>
isLayerSupported(LayerIterator begin,LayerIterator end,const RequiredLayer & required)197 bool isLayerSupported (LayerIterator begin, LayerIterator end, const RequiredLayer& required)
198 {
199 for (LayerIterator cur = begin; cur != end; ++cur)
200 {
201 if (isCompatible(*cur, required))
202 return true;
203 }
204 return false;
205 }
206
207 } // vk
208
209 #endif // _VKQUERYUTIL_HPP
210