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