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