• 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 VkPhysicalDeviceProperties						getPhysicalDeviceProperties						(const InstanceInterface& vk, VkPhysicalDevice physicalDevice);
51 VkPhysicalDeviceMemoryProperties				getPhysicalDeviceMemoryProperties				(const InstanceInterface& vk, VkPhysicalDevice physicalDevice);
52 VkFormatProperties								getPhysicalDeviceFormatProperties				(const InstanceInterface& vk, VkPhysicalDevice physicalDevice, VkFormat format);
53 VkImageFormatProperties							getPhysicalDeviceImageFormatProperties			(const InstanceInterface& vk, VkPhysicalDevice physicalDevice, VkFormat format, VkImageType type, VkImageTiling tiling, VkImageUsageFlags usage, VkImageCreateFlags flags);
54 std::vector<VkSparseImageFormatProperties>		getPhysicalDeviceSparseImageFormatProperties	(const InstanceInterface& vk, VkPhysicalDevice physicalDevice, VkFormat format, VkImageType type, VkSampleCountFlagBits samples, VkImageUsageFlags usage, VkImageTiling tiling);
55 
56 VkMemoryRequirements							getBufferMemoryRequirements						(const DeviceInterface& vk, VkDevice device, VkBuffer buffer);
57 VkMemoryRequirements							getImageMemoryRequirements						(const DeviceInterface& vk, VkDevice device, VkImage image);
58 VkMemoryRequirements							getImagePlaneMemoryRequirements					(const DeviceInterface& vk, VkDevice device, VkImage image, VkImageAspectFlagBits planeAspect);
59 std::vector<VkSparseImageMemoryRequirements>	getImageSparseMemoryRequirements				(const DeviceInterface& vk, VkDevice device, VkImage image);
60 
61 std::vector<VkLayerProperties>					enumerateInstanceLayerProperties				(const PlatformInterface& vkp);
62 std::vector<VkExtensionProperties>				enumerateInstanceExtensionProperties			(const PlatformInterface& vkp, const char* layerName);
63 std::vector<VkLayerProperties>					enumerateDeviceLayerProperties					(const InstanceInterface& vki, VkPhysicalDevice physicalDevice);
64 std::vector<VkExtensionProperties>				enumerateDeviceExtensionProperties				(const InstanceInterface& vki, VkPhysicalDevice physicalDevice, const char* layerName);
65 
66 VkQueue											getDeviceQueue									(const DeviceInterface& vkd, VkDevice device, deUint32 queueFamilyIndex, deUint32 queueIndex);
67 VkQueue											getDeviceQueue2									(const DeviceInterface& vkd, VkDevice device, const VkDeviceQueueInfo2 *queueInfo);
68 
69 // Feature / extension support
70 
71 bool											isShaderStageSupported							(const VkPhysicalDeviceFeatures& deviceFeatures, VkShaderStageFlagBits stage);
72 
73 struct RequiredExtension
74 {
75 	std::string				name;
76 	tcu::Maybe<deUint32>	minVersion;
77 	tcu::Maybe<deUint32>	maxVersion;
78 
RequiredExtensionvk::RequiredExtension79 	explicit RequiredExtension (const std::string&		name_,
80 								tcu::Maybe<deUint32>	minVersion_ = tcu::nothing<deUint32>(),
81 								tcu::Maybe<deUint32>	maxVersion_ = tcu::nothing<deUint32>())
82 		: name			(name_)
83 		, minVersion	(minVersion_)
84 		, maxVersion	(maxVersion_)
85 	{}
86 };
87 
88 struct RequiredLayer
89 {
90 	std::string				name;
91 	tcu::Maybe<deUint32>	minSpecVersion;
92 	tcu::Maybe<deUint32>	maxSpecVersion;
93 	tcu::Maybe<deUint32>	minImplVersion;
94 	tcu::Maybe<deUint32>	maxImplVersion;
95 
RequiredLayervk::RequiredLayer96 	explicit RequiredLayer (const std::string&			name_,
97 							tcu::Maybe<deUint32>		minSpecVersion_		= tcu::nothing<deUint32>(),
98 							tcu::Maybe<deUint32>		maxSpecVersion_		= tcu::nothing<deUint32>(),
99 							tcu::Maybe<deUint32>		minImplVersion_		= tcu::nothing<deUint32>(),
100 							tcu::Maybe<deUint32>		maxImplVersion_		= tcu::nothing<deUint32>())
101 		: name			(name_)
102 		, minSpecVersion(minSpecVersion_)
103 		, maxSpecVersion(maxSpecVersion_)
104 		, minImplVersion(minImplVersion_)
105 		, maxImplVersion(maxImplVersion_)
106 	{}
107 };
108 
109 bool										isCompatible							(const VkExtensionProperties& extensionProperties, const RequiredExtension& required);
110 bool										isCompatible							(const VkLayerProperties& layerProperties, const RequiredLayer& required);
111 
112 template<typename ExtensionIterator>
113 bool										isExtensionSupported					(ExtensionIterator begin, ExtensionIterator end, const RequiredExtension& required);
114 bool										isExtensionSupported					(const std::vector<VkExtensionProperties>& extensions, const RequiredExtension& required);
115 bool										isDeviceExtensionSupported				(const deUint32 deviceVersion, const std::vector<std::string>& extensions, const std::string& required);
116 bool										isInstanceExtensionSupported			(const deUint32 instanceVersion, const std::vector<std::string>& extensions, const std::string& required);
117 bool										isDeviceExtensionSupported				(const deUint32 deviceVersion, const std::vector<VkExtensionProperties>& extensions, const RequiredExtension& required);
118 bool										isInstanceExtensionSupported			(const deUint32 instanceVersion, const std::vector<VkExtensionProperties>& extensions, const RequiredExtension& required);
119 
120 template<typename LayerIterator>
121 bool										isLayerSupported						(LayerIterator begin, LayerIterator end, const RequiredLayer& required);
122 bool										isLayerSupported						(const std::vector<VkLayerProperties>& layers, const RequiredLayer& required);
123 
124 const void*									findStructureInChain					(const void* first, VkStructureType type);
125 void*										findStructureInChain					(void* first, VkStructureType type);
126 
127 template<typename StructType>
128 VkStructureType								getStructureType						(void);
129 
130 template<typename StructType>
findStructure(const void * first)131 const StructType*							findStructure							(const void* first)
132 {
133 	return reinterpret_cast<const StructType*>(findStructureInChain(first, getStructureType<StructType>()));
134 }
135 
136 template<typename StructType>
findStructure(void * first)137 StructType*									findStructure							(void* first)
138 {
139 	return reinterpret_cast<StructType*>(findStructureInChain(first, getStructureType<StructType>()));
140 }
141 
142 namespace ValidateQueryBits
143 {
144 
145 typedef struct
146 {
147 	size_t		offset;
148 	size_t		size;
149 } QueryMemberTableEntry;
150 
151 template <typename Context, typename Interface, typename Type>
152 //!< Return variable initialization validation
validateInitComplete(Context context,void (Interface::* Function)(Context,Type *)const,const Interface & interface,const QueryMemberTableEntry * queryMemberTableEntry)153 bool validateInitComplete(Context context, void (Interface::*Function)(Context, Type*)const, const Interface& interface, const QueryMemberTableEntry* queryMemberTableEntry)
154 {
155 	const QueryMemberTableEntry	*iterator;
156 	Type vec[2];
157 	deMemset(&vec[0], 0x00, sizeof(Type));
158 	deMemset(&vec[1], 0xFF, sizeof(Type));
159 
160 	(interface.*Function)(context, &vec[0]);
161 	(interface.*Function)(context, &vec[1]);
162 
163 	for (iterator = queryMemberTableEntry; iterator->size != 0; iterator++)
164 	{
165 		if (deMemCmp(((deUint8*)(&vec[0]))+iterator->offset, ((deUint8*)(&vec[1]))+iterator->offset, iterator->size) != 0)
166 			return false;
167 	}
168 
169 	return true;
170 }
171 
172 template<typename IterT>
173 //! Overwrite a range of objects with an 8-bit pattern.
fillBits(IterT beg,const IterT end,const deUint8 pattern=0xdeu)174 inline void fillBits (IterT beg, const IterT end, const deUint8 pattern = 0xdeu)
175 {
176 	for (; beg < end; ++beg)
177 		deMemset(&(*beg), static_cast<int>(pattern), sizeof(*beg));
178 }
179 
180 template<typename IterT>
181 //! 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)182 bool checkBits (IterT beg, const IterT end, const deUint8 pattern = 0xdeu)
183 {
184 	for (; beg < end; ++beg)
185 	{
186 		const deUint8* elementBytes = reinterpret_cast<const deUint8*>(&(*beg));
187 		for (std::size_t i = 0u; i < sizeof(*beg); ++i)
188 		{
189 			if (elementBytes[i] != pattern)
190 				return false;
191 		}
192 	}
193 	return true;
194 }
195 
196 } // ValidateQueryBits
197 
198 // Template implementations
199 
200 template<typename ExtensionIterator>
isExtensionSupported(ExtensionIterator begin,ExtensionIterator end,const RequiredExtension & required)201 bool isExtensionSupported (ExtensionIterator begin, ExtensionIterator end, const RequiredExtension& required)
202 {
203 	for (ExtensionIterator cur = begin; cur != end; ++cur)
204 	{
205 		if (isCompatible(*cur, required))
206 			return true;
207 	}
208 	return false;
209 }
210 
211 template<typename LayerIterator>
isLayerSupported(LayerIterator begin,LayerIterator end,const RequiredLayer & required)212 bool isLayerSupported (LayerIterator begin, LayerIterator end, const RequiredLayer& required)
213 {
214 	for (LayerIterator cur = begin; cur != end; ++cur)
215 	{
216 		if (isCompatible(*cur, required))
217 			return true;
218 	}
219 	return false;
220 }
221 
222 } // vk
223 
224 #endif // _VKQUERYUTIL_HPP
225