• 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 uint32_t specVersion;
43 };
44 
45 // Structure containg all feature blobs - this simplifies generated code
46 struct AllFeaturesBlobs
47 {
48     VkPhysicalDeviceVulkan11Features &vk11;
49     VkPhysicalDeviceVulkan12Features &vk12;
50 #ifndef CTS_USES_VULKANSC
51     VkPhysicalDeviceVulkan13Features &vk13;
52     VkPhysicalDeviceVulkan14Features &vk14;
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     {
63     }
64     virtual void initializeFeatureFromBlob(const AllFeaturesBlobs &allFeaturesBlobs) = 0;
65     virtual FeatureDesc getFeatureDesc(void) const                                   = 0;
66     virtual void **getFeatureTypeNext(void)                                          = 0;
67     virtual void *getFeatureTypeRaw(void)                                            = 0;
68 };
69 
70 using FeatureStructWrapperCreator = FeatureStructWrapperBase *(*)(void);
71 struct FeatureStructCreationData
72 {
73     FeatureStructWrapperCreator creatorFunction;
74     const char *name;
75     uint32_t specVersion;
76 };
77 
78 template <class FeatureType>
79 class FeatureStructWrapper;
80 template <class FeatureType>
81 FeatureDesc makeFeatureDesc(void);
82 
83 template <class FeatureType>
createFeatureStructWrapper(void)84 FeatureStructWrapperBase *createFeatureStructWrapper(void)
85 {
86     return new FeatureStructWrapper<FeatureType>(makeFeatureDesc<FeatureType>());
87 }
88 
89 template <class FeatureType>
90 void initFeatureFromBlob(FeatureType &featureType, const AllFeaturesBlobs &allFeaturesBlobs);
91 
92 template <class FeatureType>
initFeatureFromBlobWrapper(FeatureType & featureType,const AllFeaturesBlobs & allFeaturesBlobs)93 void initFeatureFromBlobWrapper(FeatureType &featureType, const AllFeaturesBlobs &allFeaturesBlobs)
94 {
95     initFeatureFromBlob<FeatureType>(featureType, allFeaturesBlobs);
96 }
97 
98 class DeviceFeatures
99 {
100 public:
101     DeviceFeatures(const InstanceInterface &vki, const uint32_t apiVersion, const VkPhysicalDevice physicalDevice,
102                    const std::vector<std::string> &instanceExtensions, const std::vector<std::string> &deviceExtensions,
103                    const bool enableAllFeatures = 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
111     {
112         return m_coreFeatures2;
113     }
getVulkan11Features(void) const114     const VkPhysicalDeviceVulkan11Features &getVulkan11Features(void) const
115     {
116         return m_vulkan11Features;
117     }
getVulkan12Features(void) const118     const VkPhysicalDeviceVulkan12Features &getVulkan12Features(void) const
119     {
120         return m_vulkan12Features;
121     }
122 #ifndef CTS_USES_VULKANSC
getVulkan13Features(void) const123     const VkPhysicalDeviceVulkan13Features &getVulkan13Features(void) const
124     {
125         return m_vulkan13Features;
126     }
getVulkan14Features(void) const127     const VkPhysicalDeviceVulkan14Features &getVulkan14Features(void) const
128     {
129         return m_vulkan14Features;
130     }
131 #endif // CTS_USES_VULKANSC
132 #ifdef CTS_USES_VULKANSC
getVulkanSC10Features(void) const133     const VkPhysicalDeviceVulkanSC10Features &getVulkanSC10Features(void) const
134     {
135         return m_vulkanSC10Features;
136     }
137 #endif // CTS_USES_VULKANSC
138 
139     bool contains(const std::string &feature, bool throwIfNotExists = false) const;
140 
141     bool isDeviceFeatureInitialized(VkStructureType sType) const;
142 
143 private:
144     static bool verifyFeatureAddCriteria(const FeatureStructCreationData &item,
145                                          const std::vector<VkExtensionProperties> &properties);
146 
147 private:
148     VkPhysicalDeviceFeatures2 m_coreFeatures2;
149     mutable std::vector<FeatureStructWrapperBase *> m_features;
150     VkPhysicalDeviceVulkan11Features m_vulkan11Features;
151     VkPhysicalDeviceVulkan12Features m_vulkan12Features;
152 #ifndef CTS_USES_VULKANSC
153     VkPhysicalDeviceVulkan13Features m_vulkan13Features;
154     VkPhysicalDeviceVulkan14Features m_vulkan14Features;
155 #endif // CTS_USES_VULKANSC
156 #ifdef CTS_USES_VULKANSC
157     VkPhysicalDeviceVulkanSC10Features m_vulkanSC10Features;
158 #endif // CTS_USES_VULKANSC
159 };
160 
161 template <class FeatureType>
getFeatureType(void) const162 const FeatureType &DeviceFeatures::getFeatureType(void) const
163 {
164     typedef FeatureStructWrapper<FeatureType> *FeatureWrapperPtr;
165 
166     const FeatureDesc featDesc  = makeFeatureDesc<FeatureType>();
167     const VkStructureType sType = featDesc.sType;
168 
169     // try to find feature by sType
170     for (auto feature : m_features)
171     {
172         if (sType == feature->getFeatureDesc().sType)
173             return static_cast<FeatureWrapperPtr>(feature)->getFeatureTypeRef();
174     }
175 
176     // if initialized feature structure was not found create empty one and return it
177     m_features.push_back(vk::createFeatureStructWrapper<FeatureType>());
178     return static_cast<FeatureWrapperPtr>(m_features.back())->getFeatureTypeRef();
179 }
180 
181 template <class FeatureType>
182 class FeatureStructWrapper : public FeatureStructWrapperBase
183 {
184 public:
FeatureStructWrapper(const FeatureDesc & featureDesc)185     FeatureStructWrapper(const FeatureDesc &featureDesc) : m_featureDesc(featureDesc)
186     {
187         deMemset(&m_featureType, 0, sizeof(m_featureType));
188         m_featureType.sType = featureDesc.sType;
189     }
190 
initializeFeatureFromBlob(const AllFeaturesBlobs & allFeaturesBlobs)191     void initializeFeatureFromBlob(const AllFeaturesBlobs &allFeaturesBlobs)
192     {
193         initFeatureFromBlobWrapper(m_featureType, allFeaturesBlobs);
194     }
195 
getFeatureDesc(void) const196     FeatureDesc getFeatureDesc(void) const
197     {
198         return m_featureDesc;
199     }
getFeatureTypeNext(void)200     void **getFeatureTypeNext(void)
201     {
202         return &m_featureType.pNext;
203     }
getFeatureTypeRaw(void)204     void *getFeatureTypeRaw(void)
205     {
206         return &m_featureType;
207     }
getFeatureTypeRef(void)208     FeatureType &getFeatureTypeRef(void)
209     {
210         return m_featureType;
211     }
212 
213 public:
214     // metadata about feature structure
215     const FeatureDesc m_featureDesc;
216 
217     // actual vulkan feature structure
218     FeatureType m_featureType;
219 };
220 
221 } // namespace vk
222 
223 #endif // _VKDEVICEFEATURES_HPP
224