xref: /aosp_15_r20/external/deqp/external/vulkancts/framework/vulkan/vkDeviceFeatures.hpp (revision 35238bce31c2a825756842865a792f8cf7f89930)
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     const uint32_t 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     {
63     }
64     virtual void initializeFeatureFromBlob(const AllFeaturesBlobs &allFeaturesBlobs) = 0;
65     virtual uint32_t getFeatureTypeId(void) const                                    = 0;
66     virtual FeatureDesc getFeatureDesc(void) const                                   = 0;
67     virtual void **getFeatureTypeNext(void)                                          = 0;
68     virtual void *getFeatureTypeRaw(void)                                            = 0;
69 };
70 
71 using FeatureStructWrapperCreator = FeatureStructWrapperBase *(*)(void);
72 struct FeatureStructCreationData
73 {
74     FeatureStructWrapperCreator creatorFunction;
75     const char *name;
76     uint32_t specVersion;
77 };
78 
79 template <class FeatureType>
80 class FeatureStructWrapper;
81 template <class FeatureType>
82 FeatureDesc makeFeatureDesc(void);
83 
84 template <class FeatureType>
createFeatureStructWrapper(void)85 FeatureStructWrapperBase *createFeatureStructWrapper(void)
86 {
87     return new FeatureStructWrapper<FeatureType>(makeFeatureDesc<FeatureType>());
88 }
89 
90 template <class FeatureType>
91 void initFeatureFromBlob(FeatureType &featureType, const AllFeaturesBlobs &allFeaturesBlobs);
92 
93 template <class FeatureType>
initFeatureFromBlobWrapper(FeatureType & featureType,const AllFeaturesBlobs & allFeaturesBlobs)94 void initFeatureFromBlobWrapper(FeatureType &featureType, const AllFeaturesBlobs &allFeaturesBlobs)
95 {
96     initFeatureFromBlob<FeatureType>(featureType, allFeaturesBlobs);
97 }
98 
99 class DeviceFeatures
100 {
101 public:
102     DeviceFeatures(const InstanceInterface &vki, const uint32_t apiVersion, const VkPhysicalDevice physicalDevice,
103                    const std::vector<std::string> &instanceExtensions, const std::vector<std::string> &deviceExtensions,
104                    const bool enableAllFeatures = false);
105 
106     ~DeviceFeatures(void);
107 
108     template <class FeatureType>
109     const FeatureType &getFeatureType(void) const;
110 
getCoreFeatures2(void) const111     const VkPhysicalDeviceFeatures2 &getCoreFeatures2(void) const
112     {
113         return m_coreFeatures2;
114     }
getVulkan11Features(void) const115     const VkPhysicalDeviceVulkan11Features &getVulkan11Features(void) const
116     {
117         return m_vulkan11Features;
118     }
getVulkan12Features(void) const119     const VkPhysicalDeviceVulkan12Features &getVulkan12Features(void) const
120     {
121         return m_vulkan12Features;
122     }
123 #ifndef CTS_USES_VULKANSC
getVulkan13Features(void) const124     const VkPhysicalDeviceVulkan13Features &getVulkan13Features(void) const
125     {
126         return m_vulkan13Features;
127     }
128 #endif // CTS_USES_VULKANSC
129 #ifdef CTS_USES_VULKANSC
getVulkanSC10Features(void) const130     const VkPhysicalDeviceVulkanSC10Features &getVulkanSC10Features(void) const
131     {
132         return m_vulkanSC10Features;
133     }
134 #endif // CTS_USES_VULKANSC
135 
136     bool contains(const std::string &feature, bool throwIfNotExists = false) const;
137 
138     bool isDeviceFeatureInitialized(VkStructureType sType) const;
139 
140 private:
141     static bool verifyFeatureAddCriteria(const FeatureStructCreationData &item,
142                                          const std::vector<VkExtensionProperties> &properties);
143 
144 private:
145     VkPhysicalDeviceFeatures2 m_coreFeatures2;
146     mutable std::vector<FeatureStructWrapperBase *> m_features;
147     VkPhysicalDeviceVulkan11Features m_vulkan11Features;
148     VkPhysicalDeviceVulkan12Features m_vulkan12Features;
149 #ifndef CTS_USES_VULKANSC
150     VkPhysicalDeviceVulkan13Features m_vulkan13Features;
151 #endif // CTS_USES_VULKANSC
152 #ifdef CTS_USES_VULKANSC
153     VkPhysicalDeviceVulkanSC10Features m_vulkanSC10Features;
154 #endif // CTS_USES_VULKANSC
155 };
156 
157 template <class FeatureType>
getFeatureType(void) const158 const FeatureType &DeviceFeatures::getFeatureType(void) const
159 {
160     typedef FeatureStructWrapper<FeatureType> *FeatureWrapperPtr;
161 
162     const FeatureDesc featDesc  = makeFeatureDesc<FeatureType>();
163     const VkStructureType sType = featDesc.sType;
164 
165     // try to find feature by sType
166     for (auto feature : m_features)
167     {
168         if (sType == feature->getFeatureDesc().sType)
169             return static_cast<FeatureWrapperPtr>(feature)->getFeatureTypeRef();
170     }
171 
172     // try to find feature by id that was assigned by gen_framework script
173     const uint32_t featureId = featDesc.typeId;
174     for (auto feature : m_features)
175     {
176         if (featureId == feature->getFeatureTypeId())
177             return static_cast<FeatureWrapperPtr>(feature)->getFeatureTypeRef();
178     }
179 
180     // if initialized feature structure was not found create empty one and return it
181     m_features.push_back(vk::createFeatureStructWrapper<FeatureType>());
182     return static_cast<FeatureWrapperPtr>(m_features.back())->getFeatureTypeRef();
183 }
184 
185 template <class FeatureType>
186 class FeatureStructWrapper : public FeatureStructWrapperBase
187 {
188 public:
FeatureStructWrapper(const FeatureDesc & featureDesc)189     FeatureStructWrapper(const FeatureDesc &featureDesc) : m_featureDesc(featureDesc)
190     {
191         deMemset(&m_featureType, 0, sizeof(m_featureType));
192         m_featureType.sType = featureDesc.sType;
193     }
194 
initializeFeatureFromBlob(const AllFeaturesBlobs & allFeaturesBlobs)195     void initializeFeatureFromBlob(const AllFeaturesBlobs &allFeaturesBlobs)
196     {
197         initFeatureFromBlobWrapper(m_featureType, allFeaturesBlobs);
198     }
199 
getFeatureTypeId(void) const200     uint32_t getFeatureTypeId(void) const
201     {
202         return m_featureDesc.typeId;
203     }
getFeatureDesc(void) const204     FeatureDesc getFeatureDesc(void) const
205     {
206         return m_featureDesc;
207     }
getFeatureTypeNext(void)208     void **getFeatureTypeNext(void)
209     {
210         return &m_featureType.pNext;
211     }
getFeatureTypeRaw(void)212     void *getFeatureTypeRaw(void)
213     {
214         return &m_featureType;
215     }
getFeatureTypeRef(void)216     FeatureType &getFeatureTypeRef(void)
217     {
218         return m_featureType;
219     }
220 
221 public:
222     // metadata about feature structure
223     const FeatureDesc m_featureDesc;
224 
225     // actual vulkan feature structure
226     FeatureType m_featureType;
227 };
228 
229 } // namespace vk
230 
231 #endif // _VKDEVICEFEATURES_HPP
232