• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #ifndef _VKDEVICEFEATURES_HPP
2 #define _VKDEVICEFEATURES_HPP
3 /*-------------------------------------------------------------------------
4  * Vulkan CTS Framework
5  * --------------------
6  *
7  * Copyright (c) 2019 The Khronos Group 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 DeviceFeatures class utility.
24  *//*--------------------------------------------------------------------*/
25 
26 #include <map>
27 #include <string>
28 #include <utility>
29 #include <vector>
30 
31 #include "deMemory.h"
32 #include "vkDefs.hpp"
33 
34 namespace vk
35 {
36 
37 // Structure describing vulkan feature structure
38 struct FeatureDesc
39 {
40 	VkStructureType		sType;
41 	const char*			name;
42 	const deUint32		specVersion;
43 	const deUint32		typeId;
44 };
45 
46 // Structure containg all feature blobs - this simplifies generated code
47 struct AllFeaturesBlobs
48 {
49 	VkPhysicalDeviceVulkan11Features& vk11;
50 	VkPhysicalDeviceVulkan12Features& vk12;
51 	// add blobs from future vulkan versions here
52 };
53 
54 // Base class for all FeatureStructWrapper specializations
55 class FeatureStructWrapperBase
56 {
57 public:
~FeatureStructWrapperBase(void)58 	virtual					~FeatureStructWrapperBase	(void) {}
59 	virtual void			initializeFeatureFromBlob	(const AllFeaturesBlobs& allFeaturesBlobs) = 0;
60 	virtual deUint32		getFeatureTypeId			(void) const = 0;
61 	virtual FeatureDesc		getFeatureDesc				(void) const = 0;
62 	virtual void**			getFeatureTypeNext			(void) = 0;
63 	virtual void*			getFeatureTypeRaw			(void) = 0;
64 };
65 
66 using FeatureStructWrapperCreator	= FeatureStructWrapperBase* (*) (void);
67 struct FeatureStructCreationData
68 {
69 	FeatureStructWrapperCreator	creatorFunction;
70 	const char*					name;
71 	deUint32					specVersion;
72 };
73 
74 template<class FeatureType> class FeatureStructWrapper;
75 template<class FeatureType> FeatureDesc makeFeatureDesc (void);
76 
77 template<class FeatureType>
createFeatureStructWrapper(void)78 FeatureStructWrapperBase* createFeatureStructWrapper (void)
79 {
80 	return new FeatureStructWrapper<FeatureType>(makeFeatureDesc<FeatureType>());
81 }
82 
83 template<class FeatureType>
84 void initFeatureFromBlob(FeatureType& featureType, const AllFeaturesBlobs& allFeaturesBlobs);
85 
86 template<class FeatureType>
initFeatureFromBlobWrapper(FeatureType & featureType,const AllFeaturesBlobs & allFeaturesBlobs)87 void initFeatureFromBlobWrapper(FeatureType& featureType, const AllFeaturesBlobs& allFeaturesBlobs)
88 {
89 	initFeatureFromBlob<FeatureType>(featureType, allFeaturesBlobs);
90 }
91 
92 class DeviceFeatures
93 {
94 public:
95 												DeviceFeatures				(const InstanceInterface&			vki,
96 																			 const deUint32						apiVersion,
97 																			 const VkPhysicalDevice				physicalDevice,
98 																			 const std::vector<std::string>&	instanceExtensions,
99 																			 const std::vector<std::string>&	deviceExtensions,
100 																			 const deBool						enableAllFeatures = DE_FALSE);
101 
102 												~DeviceFeatures				(void);
103 
104 	template<class FeatureType>
105 	const FeatureType&							getFeatureType				(void) const;
106 
getCoreFeatures2(void) const107 	const VkPhysicalDeviceFeatures2&			getCoreFeatures2			(void) const { return m_coreFeatures2; }
getVulkan11Features(void) const108 	const VkPhysicalDeviceVulkan11Features&		getVulkan11Features			(void) const { return m_vulkan11Features; }
getVulkan12Features(void) const109 	const VkPhysicalDeviceVulkan12Features&		getVulkan12Features			(void) const { return m_vulkan12Features; }
110 
111 	bool										contains					(const std::string& feature, bool throwIfNotExists = false) const;
112 
113 	bool										isDeviceFeatureInitialized	(VkStructureType sType) const;
114 
115 private:
116 
117 	static bool							verifyFeatureAddCriteria	(const FeatureStructCreationData& item, const std::vector<VkExtensionProperties>& properties);
118 
119 private:
120 
121 	VkPhysicalDeviceFeatures2						m_coreFeatures2;
122 	mutable std::vector<FeatureStructWrapperBase*>	m_features;
123 	VkPhysicalDeviceVulkan11Features				m_vulkan11Features;
124 	VkPhysicalDeviceVulkan12Features				m_vulkan12Features;
125 };
126 
127 template<class FeatureType>
getFeatureType(void) const128 const FeatureType& DeviceFeatures::getFeatureType(void) const
129 {
130 	typedef FeatureStructWrapper<FeatureType>* FeatureWrapperPtr;
131 
132 	const FeatureDesc		featDesc	= makeFeatureDesc<FeatureType>();
133 	const VkStructureType	sType		= featDesc.sType;
134 
135 	// try to find feature by sType
136 	for (auto feature : m_features)
137 	{
138 		if (sType == feature->getFeatureDesc().sType)
139 			return static_cast<FeatureWrapperPtr>(feature)->getFeatureTypeRef();
140 	}
141 
142 	// try to find feature by id that was assigned by gen_framework script
143 	const deUint32 featureId = featDesc.typeId;
144 	for (auto feature : m_features)
145 	{
146 		if (featureId == feature->getFeatureTypeId())
147 			return static_cast<FeatureWrapperPtr>(feature)->getFeatureTypeRef();
148 	}
149 
150 	// if initialized feature structure was not found create empty one and return it
151 	m_features.push_back(vk::createFeatureStructWrapper<FeatureType>());
152 	return static_cast<FeatureWrapperPtr>(m_features.back())->getFeatureTypeRef();
153 }
154 
155 template<class FeatureType>
156 class FeatureStructWrapper : public FeatureStructWrapperBase
157 {
158 public:
FeatureStructWrapper(const FeatureDesc & featureDesc)159 	FeatureStructWrapper (const FeatureDesc& featureDesc)
160 		: m_featureDesc(featureDesc)
161 	{
162 		deMemset(&m_featureType, 0, sizeof(m_featureType));
163 		m_featureType.sType = featureDesc.sType;
164 	}
165 
initializeFeatureFromBlob(const AllFeaturesBlobs & allFeaturesBlobs)166 	void initializeFeatureFromBlob (const AllFeaturesBlobs& allFeaturesBlobs)
167 	{
168 		initFeatureFromBlobWrapper(m_featureType, allFeaturesBlobs);
169 	}
170 
getFeatureTypeId(void) const171 	deUint32		getFeatureTypeId	(void) const	{ return m_featureDesc.typeId;	}
getFeatureDesc(void) const172 	FeatureDesc		getFeatureDesc		(void) const	{ return m_featureDesc;			}
getFeatureTypeNext(void)173 	void**			getFeatureTypeNext	(void)			{ return &m_featureType.pNext;	}
getFeatureTypeRaw(void)174 	void*			getFeatureTypeRaw	(void)			{ return &m_featureType;		}
getFeatureTypeRef(void)175 	FeatureType&	getFeatureTypeRef	(void)			{ return m_featureType;			}
176 
177 public:
178 	// metadata about feature structure
179 	const FeatureDesc	m_featureDesc;
180 
181 	// actual vulkan feature structure
182 	FeatureType			m_featureType;
183 };
184 
185 } // vk
186 
187 #endif // _VKDEVICEFEATURES_HPP
188