• 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 
101 												~DeviceFeatures				(void);
102 
103 	template<class FeatureType>
104 	const FeatureType&							getFeatureType				(void) const;
105 
getCoreFeatures2(void) const106 	const VkPhysicalDeviceFeatures2&			getCoreFeatures2			(void) const { return m_coreFeatures2; }
getVulkan11Features(void) const107 	const VkPhysicalDeviceVulkan11Features&		getVulkan11Features			(void) const { return m_vulkan11Features; }
getVulkan12Features(void) const108 	const VkPhysicalDeviceVulkan12Features&		getVulkan12Features			(void) const { return m_vulkan12Features; }
109 
110 	bool										contains					(const std::string& feature, bool throwIfNotExists = false) const;
111 
112 	bool										isDeviceFeatureInitialized	(VkStructureType sType) const;
113 
114 private:
115 
116 	static bool							verifyFeatureAddCriteria	(const FeatureStructCreationData& item, const std::vector<VkExtensionProperties>& properties);
117 
118 private:
119 
120 	VkPhysicalDeviceFeatures2						m_coreFeatures2;
121 	mutable std::vector<FeatureStructWrapperBase*>	m_features;
122 	VkPhysicalDeviceVulkan11Features				m_vulkan11Features;
123 	VkPhysicalDeviceVulkan12Features				m_vulkan12Features;
124 };
125 
126 template<class FeatureType>
getFeatureType(void) const127 const FeatureType& DeviceFeatures::getFeatureType(void) const
128 {
129 	typedef FeatureStructWrapper<FeatureType>* FeatureWrapperPtr;
130 
131 	const FeatureDesc		featDesc	= makeFeatureDesc<FeatureType>();
132 	const VkStructureType	sType		= featDesc.sType;
133 
134 	// try to find feature by sType
135 	for (auto feature : m_features)
136 	{
137 		if (sType == feature->getFeatureDesc().sType)
138 			return static_cast<FeatureWrapperPtr>(feature)->getFeatureTypeRef();
139 	}
140 
141 	// try to find feature by id that was assigned by gen_framework script
142 	const deUint32 featureId = featDesc.typeId;
143 	for (auto feature : m_features)
144 	{
145 		if (featureId == feature->getFeatureTypeId())
146 			return static_cast<FeatureWrapperPtr>(feature)->getFeatureTypeRef();
147 	}
148 
149 	// if initialized feature structure was not found create empty one and return it
150 	m_features.push_back(vk::createFeatureStructWrapper<FeatureType>());
151 	return static_cast<FeatureWrapperPtr>(m_features.back())->getFeatureTypeRef();
152 }
153 
154 template<class FeatureType>
155 class FeatureStructWrapper : public FeatureStructWrapperBase
156 {
157 public:
FeatureStructWrapper(const FeatureDesc & featureDesc)158 	FeatureStructWrapper (const FeatureDesc& featureDesc)
159 		: m_featureDesc(featureDesc)
160 	{
161 		deMemset(&m_featureType, 0, sizeof(m_featureType));
162 		m_featureType.sType = featureDesc.sType;
163 	}
164 
initializeFeatureFromBlob(const AllFeaturesBlobs & allFeaturesBlobs)165 	void initializeFeatureFromBlob (const AllFeaturesBlobs& allFeaturesBlobs)
166 	{
167 		initFeatureFromBlobWrapper(m_featureType, allFeaturesBlobs);
168 	}
169 
getFeatureTypeId(void) const170 	deUint32		getFeatureTypeId	(void) const	{ return m_featureDesc.typeId;	}
getFeatureDesc(void) const171 	FeatureDesc		getFeatureDesc		(void) const	{ return m_featureDesc;			}
getFeatureTypeNext(void)172 	void**			getFeatureTypeNext	(void)			{ return &m_featureType.pNext;	}
getFeatureTypeRaw(void)173 	void*			getFeatureTypeRaw	(void)			{ return &m_featureType;		}
getFeatureTypeRef(void)174 	FeatureType&	getFeatureTypeRef	(void)			{ return m_featureType;			}
175 
176 public:
177 	// metadata about feature structure
178 	const FeatureDesc	m_featureDesc;
179 
180 	// actual vulkan feature structure
181 	FeatureType			m_featureType;
182 };
183 
184 } // vk
185 
186 #endif // _VKDEVICEFEATURES_HPP
187