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 VkPhysicalDeviceVulkan13Features& vk13;
52 // add blobs from future vulkan versions here
53 };
54
55 // Base class for all FeatureStructWrapper specializations
56 class FeatureStructWrapperBase
57 {
58 public:
~FeatureStructWrapperBase(void)59 virtual ~FeatureStructWrapperBase (void) {}
60 virtual void initializeFeatureFromBlob (const AllFeaturesBlobs& allFeaturesBlobs) = 0;
61 virtual deUint32 getFeatureTypeId (void) const = 0;
62 virtual FeatureDesc getFeatureDesc (void) const = 0;
63 virtual void** getFeatureTypeNext (void) = 0;
64 virtual void* getFeatureTypeRaw (void) = 0;
65 };
66
67 using FeatureStructWrapperCreator = FeatureStructWrapperBase* (*) (void);
68 struct FeatureStructCreationData
69 {
70 FeatureStructWrapperCreator creatorFunction;
71 const char* name;
72 deUint32 specVersion;
73 };
74
75 template<class FeatureType> class FeatureStructWrapper;
76 template<class FeatureType> FeatureDesc makeFeatureDesc (void);
77
78 template<class FeatureType>
createFeatureStructWrapper(void)79 FeatureStructWrapperBase* createFeatureStructWrapper (void)
80 {
81 return new FeatureStructWrapper<FeatureType>(makeFeatureDesc<FeatureType>());
82 }
83
84 template<class FeatureType>
85 void initFeatureFromBlob(FeatureType& featureType, const AllFeaturesBlobs& allFeaturesBlobs);
86
87 template<class FeatureType>
initFeatureFromBlobWrapper(FeatureType & featureType,const AllFeaturesBlobs & allFeaturesBlobs)88 void initFeatureFromBlobWrapper(FeatureType& featureType, const AllFeaturesBlobs& allFeaturesBlobs)
89 {
90 initFeatureFromBlob<FeatureType>(featureType, allFeaturesBlobs);
91 }
92
93 class DeviceFeatures
94 {
95 public:
96 DeviceFeatures (const InstanceInterface& vki,
97 const deUint32 apiVersion,
98 const VkPhysicalDevice physicalDevice,
99 const std::vector<std::string>& instanceExtensions,
100 const std::vector<std::string>& deviceExtensions,
101 const deBool enableAllFeatures = DE_FALSE);
102
103 ~DeviceFeatures (void);
104
105 template<class FeatureType>
106 const FeatureType& getFeatureType (void) const;
107
getCoreFeatures2(void) const108 const VkPhysicalDeviceFeatures2& getCoreFeatures2 (void) const { return m_coreFeatures2; }
getVulkan11Features(void) const109 const VkPhysicalDeviceVulkan11Features& getVulkan11Features (void) const { return m_vulkan11Features; }
getVulkan12Features(void) const110 const VkPhysicalDeviceVulkan12Features& getVulkan12Features (void) const { return m_vulkan12Features; }
getVulkan13Features(void) const111 const VkPhysicalDeviceVulkan13Features& getVulkan13Features (void) const { return m_vulkan13Features; }
112
113 bool contains (const std::string& feature, bool throwIfNotExists = false) const;
114
115 bool isDeviceFeatureInitialized (VkStructureType sType) const;
116
117 private:
118
119 static bool verifyFeatureAddCriteria (const FeatureStructCreationData& item, const std::vector<VkExtensionProperties>& properties);
120
121 private:
122
123 VkPhysicalDeviceFeatures2 m_coreFeatures2;
124 mutable std::vector<FeatureStructWrapperBase*> m_features;
125 VkPhysicalDeviceVulkan11Features m_vulkan11Features;
126 VkPhysicalDeviceVulkan12Features m_vulkan12Features;
127 VkPhysicalDeviceVulkan13Features m_vulkan13Features;
128 };
129
130 template<class FeatureType>
getFeatureType(void) const131 const FeatureType& DeviceFeatures::getFeatureType(void) const
132 {
133 typedef FeatureStructWrapper<FeatureType>* FeatureWrapperPtr;
134
135 const FeatureDesc featDesc = makeFeatureDesc<FeatureType>();
136 const VkStructureType sType = featDesc.sType;
137
138 // try to find feature by sType
139 for (auto feature : m_features)
140 {
141 if (sType == feature->getFeatureDesc().sType)
142 return static_cast<FeatureWrapperPtr>(feature)->getFeatureTypeRef();
143 }
144
145 // try to find feature by id that was assigned by gen_framework script
146 const deUint32 featureId = featDesc.typeId;
147 for (auto feature : m_features)
148 {
149 if (featureId == feature->getFeatureTypeId())
150 return static_cast<FeatureWrapperPtr>(feature)->getFeatureTypeRef();
151 }
152
153 // if initialized feature structure was not found create empty one and return it
154 m_features.push_back(vk::createFeatureStructWrapper<FeatureType>());
155 return static_cast<FeatureWrapperPtr>(m_features.back())->getFeatureTypeRef();
156 }
157
158 template<class FeatureType>
159 class FeatureStructWrapper : public FeatureStructWrapperBase
160 {
161 public:
FeatureStructWrapper(const FeatureDesc & featureDesc)162 FeatureStructWrapper (const FeatureDesc& featureDesc)
163 : m_featureDesc(featureDesc)
164 {
165 deMemset(&m_featureType, 0, sizeof(m_featureType));
166 m_featureType.sType = featureDesc.sType;
167 }
168
initializeFeatureFromBlob(const AllFeaturesBlobs & allFeaturesBlobs)169 void initializeFeatureFromBlob (const AllFeaturesBlobs& allFeaturesBlobs)
170 {
171 initFeatureFromBlobWrapper(m_featureType, allFeaturesBlobs);
172 }
173
getFeatureTypeId(void) const174 deUint32 getFeatureTypeId (void) const { return m_featureDesc.typeId; }
getFeatureDesc(void) const175 FeatureDesc getFeatureDesc (void) const { return m_featureDesc; }
getFeatureTypeNext(void)176 void** getFeatureTypeNext (void) { return &m_featureType.pNext; }
getFeatureTypeRaw(void)177 void* getFeatureTypeRaw (void) { return &m_featureType; }
getFeatureTypeRef(void)178 FeatureType& getFeatureTypeRef (void) { return m_featureType; }
179
180 public:
181 // metadata about feature structure
182 const FeatureDesc m_featureDesc;
183
184 // actual vulkan feature structure
185 FeatureType m_featureType;
186 };
187
188 } // vk
189
190 #endif // _VKDEVICEFEATURES_HPP
191