• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include <string>
32 
33 namespace vk
34 {
35 
36 // API version introspection
37 
38 void											getCoreInstanceExtensions						(deUint32 apiVersion, std::vector<const char*>& dst);
39 void											getCoreDeviceExtensions							(deUint32 apiVersion, std::vector<const char*>& dst);
40 bool											isCoreInstanceExtension							(const deUint32 apiVersion, const std::string& extension);
41 bool											isCoreDeviceExtension							(const deUint32 apiVersion, const std::string& extension);
42 
43 // API queries
44 
45 std::vector<VkPhysicalDevice>					enumeratePhysicalDevices						(const InstanceInterface& vk, VkInstance instance);
46 std::vector<VkPhysicalDeviceGroupProperties>	enumeratePhysicalDeviceGroups					(const InstanceInterface& vk, VkInstance instance);
47 std::vector<VkQueueFamilyProperties>			getPhysicalDeviceQueueFamilyProperties			(const InstanceInterface& vk, VkPhysicalDevice physicalDevice);
48 VkPhysicalDeviceFeatures						getPhysicalDeviceFeatures						(const InstanceInterface& vk, VkPhysicalDevice physicalDevice);
49 VkPhysicalDeviceFeatures2						getPhysicalDeviceFeatures2						(const InstanceInterface& vk, VkPhysicalDevice physicalDevice);
50 VkPhysicalDeviceVulkan11Features				getPhysicalDeviceVulkan11Features				(const InstanceInterface& vk, VkPhysicalDevice physicalDevice);
51 VkPhysicalDeviceVulkan12Features				getPhysicalDeviceVulkan12Features				(const InstanceInterface& vk, VkPhysicalDevice physicalDevice);
52 VkPhysicalDeviceVulkan11Properties				getPhysicalDeviceVulkan11Properties				(const InstanceInterface& vk, VkPhysicalDevice physicalDevice);
53 VkPhysicalDeviceVulkan12Properties				getPhysicalDeviceVulkan12Properties				(const InstanceInterface& vk, VkPhysicalDevice physicalDevice);
54 VkPhysicalDeviceProperties						getPhysicalDeviceProperties						(const InstanceInterface& vk, VkPhysicalDevice physicalDevice);
55 VkPhysicalDeviceMemoryProperties				getPhysicalDeviceMemoryProperties				(const InstanceInterface& vk, VkPhysicalDevice physicalDevice);
56 VkFormatProperties								getPhysicalDeviceFormatProperties				(const InstanceInterface& vk, VkPhysicalDevice physicalDevice, VkFormat format);
57 VkImageFormatProperties							getPhysicalDeviceImageFormatProperties			(const InstanceInterface& vk, VkPhysicalDevice physicalDevice, VkFormat format, VkImageType type, VkImageTiling tiling, VkImageUsageFlags usage, VkImageCreateFlags flags);
58 
59 #ifndef CTS_USES_VULKANSC
60 std::vector<VkSparseImageFormatProperties>		getPhysicalDeviceSparseImageFormatProperties	(const InstanceInterface& vk, VkPhysicalDevice physicalDevice, VkFormat format, VkImageType type, VkSampleCountFlagBits samples, VkImageUsageFlags usage, VkImageTiling tiling);
61 #endif // CTS_USES_VULKANSC
62 #ifdef CTS_USES_VULKANSC
63 VkPhysicalDeviceVulkanSC10Features				getPhysicalDeviceVulkanSC10Features				(const InstanceInterface& vk, VkPhysicalDevice physicalDevice);
64 VkPhysicalDeviceVulkanSC10Properties			getPhysicalDeviceVulkanSC10Properties			(const InstanceInterface& vk, VkPhysicalDevice physicalDevice);
65 #endif // CTS_USES_VULKANSC
66 VkMemoryRequirements							getBufferMemoryRequirements						(const DeviceInterface& vk, VkDevice device, VkBuffer buffer);
67 VkMemoryRequirements							getImageMemoryRequirements						(const DeviceInterface& vk, VkDevice device, VkImage image);
68 VkMemoryRequirements							getImagePlaneMemoryRequirements					(const DeviceInterface& vk, VkDevice device, VkImage image, VkImageAspectFlagBits planeAspect);
69 #ifndef CTS_USES_VULKANSC
70 std::vector<VkSparseImageMemoryRequirements>	getImageSparseMemoryRequirements				(const DeviceInterface& vk, VkDevice device, VkImage image);
71 #endif // CTS_USES_VULKANSC
72 
73 std::vector<VkLayerProperties>					enumerateInstanceLayerProperties				(const PlatformInterface& vkp);
74 std::vector<VkExtensionProperties>				enumerateInstanceExtensionProperties			(const PlatformInterface& vkp, const char* layerName);
75 std::vector<VkLayerProperties>					enumerateDeviceLayerProperties					(const InstanceInterface& vki, VkPhysicalDevice physicalDevice);
76 std::vector<VkExtensionProperties>				enumerateDeviceExtensionProperties				(const InstanceInterface& vki, VkPhysicalDevice physicalDevice, const char* layerName);
77 
78 VkQueue											getDeviceQueue									(const DeviceInterface& vkd, VkDevice device, deUint32 queueFamilyIndex, deUint32 queueIndex);
79 VkQueue											getDeviceQueue2									(const DeviceInterface& vkd, VkDevice device, const VkDeviceQueueInfo2 *queueInfo);
80 
81 // Feature / extension support
82 
83 bool											isShaderStageSupported							(const VkPhysicalDeviceFeatures& deviceFeatures, VkShaderStageFlagBits stage);
84 
85 struct RequiredExtension
86 {
87 	std::string				name;
88 	tcu::Maybe<deUint32>	minVersion;
89 	tcu::Maybe<deUint32>	maxVersion;
90 
RequiredExtensionvk::RequiredExtension91 	explicit RequiredExtension (const std::string&		name_,
92 								tcu::Maybe<deUint32>	minVersion_ = tcu::Nothing,
93 								tcu::Maybe<deUint32>	maxVersion_ = tcu::Nothing)
94 		: name			(name_)
95 		, minVersion	(minVersion_)
96 		, maxVersion	(maxVersion_)
97 	{}
98 };
99 
100 struct RequiredLayer
101 {
102 	std::string				name;
103 	tcu::Maybe<deUint32>	minSpecVersion;
104 	tcu::Maybe<deUint32>	maxSpecVersion;
105 	tcu::Maybe<deUint32>	minImplVersion;
106 	tcu::Maybe<deUint32>	maxImplVersion;
107 
RequiredLayervk::RequiredLayer108 	explicit RequiredLayer (const std::string&			name_,
109 							tcu::Maybe<deUint32>		minSpecVersion_		= tcu::Nothing,
110 							tcu::Maybe<deUint32>		maxSpecVersion_		= tcu::Nothing,
111 							tcu::Maybe<deUint32>		minImplVersion_		= tcu::Nothing,
112 							tcu::Maybe<deUint32>		maxImplVersion_		= tcu::Nothing)
113 		: name			(name_)
114 		, minSpecVersion(minSpecVersion_)
115 		, maxSpecVersion(maxSpecVersion_)
116 		, minImplVersion(minImplVersion_)
117 		, maxImplVersion(maxImplVersion_)
118 	{}
119 };
120 
121 bool										isCompatible							(const VkExtensionProperties& extensionProperties, const RequiredExtension& required);
122 bool										isCompatible							(const VkLayerProperties& layerProperties, const RequiredLayer& required);
123 
124 template<typename ExtensionIterator>
125 bool										isExtensionStructSupported					(ExtensionIterator begin, ExtensionIterator end, const RequiredExtension& required);
126 bool										isExtensionStructSupported					(const std::vector<VkExtensionProperties>& extensions, const RequiredExtension& required);
127 
128 bool										isInstanceExtensionSupported			(const deUint32 instanceVersion, const std::vector<std::string>& extensions, const std::string& required);
129 
130 template<typename LayerIterator>
131 bool										isLayerSupported						(LayerIterator begin, LayerIterator end, const RequiredLayer& required);
132 bool										isLayerSupported						(const std::vector<VkLayerProperties>& layers, const RequiredLayer& required);
133 
134 const void*									findStructureInChain					(const void* first, VkStructureType type);
135 void*										findStructureInChain					(void* first, VkStructureType type);
136 
137 template<typename StructType>
138 VkStructureType								getStructureType						(void);
139 
140 template<typename StructType>
findStructure(const void * first)141 const StructType*							findStructure							(const void* first)
142 {
143 	return reinterpret_cast<const StructType*>(findStructureInChain(first, getStructureType<StructType>()));
144 }
145 
146 template<typename StructType>
findStructure(void * first)147 StructType*									findStructure							(void* first)
148 {
149 	return reinterpret_cast<StructType*>(findStructureInChain(first, getStructureType<StructType>()));
150 }
151 
152 struct initVulkanStructure
153 {
initVulkanStructurevk::initVulkanStructure154 	initVulkanStructure	(void*	pNext = DE_NULL)	: m_next(pNext)	{}
155 
156 	template<class StructType>
operator StructTypevk::initVulkanStructure157 	operator StructType()
158 	{
159 		StructType result;
160 
161 		deMemset(&result, 0x00, sizeof(StructType));
162 
163 		result.sType	= getStructureType<StructType>();
164 		result.pNext	= m_next;
165 
166 		return result;
167 	}
168 
169 private:
170 	void*	m_next;
171 };
172 
173 template<class StructType>
addToChainVulkanStructure(void *** chainPNextPtr,StructType & structType)174 void addToChainVulkanStructure (void***	chainPNextPtr, StructType&	structType)
175 {
176 	DE_ASSERT(chainPNextPtr != DE_NULL);
177 
178 	(**chainPNextPtr) = &structType;
179 
180 	(*chainPNextPtr) = &structType.pNext;
181 }
182 
183 template<class StructType>
addToChainVulkanStructure(const void *** chainPNextPtr,StructType & structType)184 void addToChainVulkanStructure (const void***	chainPNextPtr, StructType&	structType)
185 {
186 	DE_ASSERT(chainPNextPtr != DE_NULL);
187 
188 	(**chainPNextPtr) = &structType;
189 
190 	(*chainPNextPtr) = &structType.pNext;
191 }
192 
193 struct initVulkanStructureConst
194 {
initVulkanStructureConstvk::initVulkanStructureConst195 	initVulkanStructureConst	(const void*	pNext = DE_NULL)	: m_next(pNext)	{}
196 
197 	template<class StructType>
operator const StructTypevk::initVulkanStructureConst198 	operator const StructType()
199 	{
200 		StructType result;
201 
202 		deMemset(&result, 0x00, sizeof(StructType));
203 
204 		result.sType	= getStructureType<StructType>();
205 		result.pNext	= const_cast<void*>(m_next);
206 
207 		return result;
208 	}
209 
210 private:
211 	const void*	m_next;
212 };
213 
214 struct getPhysicalDeviceExtensionProperties
215 {
getPhysicalDeviceExtensionPropertiesvk::getPhysicalDeviceExtensionProperties216 	getPhysicalDeviceExtensionProperties (const InstanceInterface&	vki, VkPhysicalDevice physicalDevice) : m_vki(vki), m_physicalDevice(physicalDevice) {}
217 
218 	template<class ExtensionProperties>
operator ExtensionPropertiesvk::getPhysicalDeviceExtensionProperties219 	operator ExtensionProperties ()
220 	{
221 		VkPhysicalDeviceProperties2	properties2;
222 		ExtensionProperties			extensionProperties;
223 
224 		deMemset(&extensionProperties, 0x00, sizeof(ExtensionProperties));
225 		extensionProperties.sType = getStructureType<ExtensionProperties>();
226 
227 		deMemset(&properties2, 0x00, sizeof(properties2));
228 		properties2.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PROPERTIES_2;
229 		properties2.pNext = &extensionProperties;
230 
231 		m_vki.getPhysicalDeviceProperties2(m_physicalDevice, &properties2);
232 
233 		return extensionProperties;
234 	}
235 
operator VkPhysicalDeviceProperties2vk::getPhysicalDeviceExtensionProperties236 	operator VkPhysicalDeviceProperties2 ()
237 	{
238 		VkPhysicalDeviceProperties2	properties2;
239 
240 		deMemset(&properties2, 0x00, sizeof(properties2));
241 		properties2.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PROPERTIES_2;
242 
243 		m_vki.getPhysicalDeviceProperties2(m_physicalDevice, &properties2);
244 
245 		return properties2;
246 	}
247 
248 private:
249 	const InstanceInterface&	m_vki;
250 	const VkPhysicalDevice		m_physicalDevice;
251 };
252 
253 // Walks through chain to find empty pNext and assigns what to found pNext
254 void appendStructurePtrToVulkanChain (const void**	chainHead, const void*	structurePtr);
255 
256 namespace ValidateQueryBits
257 {
258 
259 typedef struct
260 {
261 	size_t		offset;
262 	size_t		size;
263 } QueryMemberTableEntry;
264 
265 template <typename Context, typename Interface, typename Type>
266 //!< Return variable initialization validation
validateInitComplete(Context context,void (Interface::* Function)(Context,Type *)const,const Interface & interface,const QueryMemberTableEntry * queryMemberTableEntry)267 bool validateInitComplete(Context context, void (Interface::*Function)(Context, Type*)const, const Interface& interface, const QueryMemberTableEntry* queryMemberTableEntry)
268 {
269 	const QueryMemberTableEntry	*iterator;
270 	Type vec[2];
271 	deMemset(&vec[0], 0x00, sizeof(Type));
272 	deMemset(&vec[1], 0xFF, sizeof(Type));
273 
274 	(interface.*Function)(context, &vec[0]);
275 	(interface.*Function)(context, &vec[1]);
276 
277 	for (iterator = queryMemberTableEntry; iterator->size != 0; iterator++)
278 	{
279 		if (deMemCmp(((deUint8*)(&vec[0]))+iterator->offset, ((deUint8*)(&vec[1]))+iterator->offset, iterator->size) != 0)
280 			return false;
281 	}
282 
283 	return true;
284 }
285 
286 template <typename Type>
287 //!< Return variable initialization validation
validateStructsWithGuard(const QueryMemberTableEntry * queryMemberTableEntry,Type * vec[2],const deUint8 guardValue,const deUint32 guardSize)288 bool validateStructsWithGuard (const QueryMemberTableEntry* queryMemberTableEntry, Type* vec[2], const deUint8 guardValue, const deUint32 guardSize)
289 {
290 	const QueryMemberTableEntry	*iterator;
291 
292 	for (iterator = queryMemberTableEntry; iterator->size != 0; iterator++)
293 	{
294 		if (deMemCmp(((deUint8*)(vec[0]))+iterator->offset, ((deUint8*)(vec[1]))+iterator->offset, iterator->size) != 0)
295 			return false;
296 	}
297 
298 	for (deUint32 vecNdx = 0; vecNdx < 2; ++vecNdx)
299 	{
300 		for (deUint32 ndx = 0; ndx < guardSize; ndx++)
301 		{
302 			if (((deUint8*)(vec[vecNdx]))[ndx + sizeof(Type)] != guardValue)
303 				return false;
304 		}
305 	}
306 
307 	return true;
308 }
309 
310 template<typename IterT>
311 //! Overwrite a range of objects with an 8-bit pattern.
fillBits(IterT beg,const IterT end,const deUint8 pattern=0xdeu)312 inline void fillBits (IterT beg, const IterT end, const deUint8 pattern = 0xdeu)
313 {
314 	for (; beg < end; ++beg)
315 		deMemset(&(*beg), static_cast<int>(pattern), sizeof(*beg));
316 }
317 
318 template<typename IterT>
319 //! 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)320 bool checkBits (IterT beg, const IterT end, const deUint8 pattern = 0xdeu)
321 {
322 	for (; beg < end; ++beg)
323 	{
324 		const deUint8* elementBytes = reinterpret_cast<const deUint8*>(&(*beg));
325 		for (std::size_t i = 0u; i < sizeof(*beg); ++i)
326 		{
327 			if (elementBytes[i] != pattern)
328 				return false;
329 		}
330 	}
331 	return true;
332 }
333 
334 } // ValidateQueryBits
335 
336 // Template implementations
337 
338 template<typename ExtensionIterator>
isExtensionStructSupported(ExtensionIterator begin,ExtensionIterator end,const RequiredExtension & required)339 bool isExtensionStructSupported (ExtensionIterator begin, ExtensionIterator end, const RequiredExtension& required)
340 {
341 	for (ExtensionIterator cur = begin; cur != end; ++cur)
342 	{
343 		if (isCompatible(*cur, required))
344 			return true;
345 	}
346 	return false;
347 }
348 
349 template<typename LayerIterator>
isLayerSupported(LayerIterator begin,LayerIterator end,const RequiredLayer & required)350 bool isLayerSupported (LayerIterator begin, LayerIterator end, const RequiredLayer& required)
351 {
352 	for (LayerIterator cur = begin; cur != end; ++cur)
353 	{
354 		if (isCompatible(*cur, required))
355 			return true;
356 	}
357 	return false;
358 }
359 
360 } // vk
361 
362 #endif // _VKQUERYUTIL_HPP
363