1 /*-------------------------------------------------------------------------
2 * Vulkan CTS Framework
3 * --------------------
4 *
5 * Copyright (c) 2015 Google Inc.
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 *
19 *//*!
20 * \file
21 * \brief Instance and device initialization utilities.
22 *//*--------------------------------------------------------------------*/
23
24 #include "vkDeviceUtil.hpp"
25 #include "vkQueryUtil.hpp"
26 #include "vkRefUtil.hpp"
27 #include "vkApiVersion.hpp"
28
29 #include "tcuCommandLine.hpp"
30
31 #include "qpInfo.h"
32
33 namespace vk
34 {
35
36 using std::vector;
37 using std::string;
38
createDefaultInstance(const PlatformInterface & vkPlatform,deUint32 apiVersion,const vector<string> & enabledLayers,const vector<string> & enabledExtensions,const VkAllocationCallbacks * pAllocator)39 Move<VkInstance> createDefaultInstance (const PlatformInterface& vkPlatform,
40 deUint32 apiVersion,
41 const vector<string>& enabledLayers,
42 const vector<string>& enabledExtensions,
43 const VkAllocationCallbacks* pAllocator)
44 {
45 vector<const char*> layerNamePtrs (enabledLayers.size());
46 vector<const char*> extensionNamePtrs (enabledExtensions.size());
47
48 const struct VkApplicationInfo appInfo =
49 {
50 VK_STRUCTURE_TYPE_APPLICATION_INFO,
51 DE_NULL,
52 "deqp", // pAppName
53 qpGetReleaseId(), // appVersion
54 "deqp", // pEngineName
55 qpGetReleaseId(), // engineVersion
56 apiVersion // apiVersion
57 };
58 const struct VkInstanceCreateInfo instanceInfo =
59 {
60 VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO,
61 DE_NULL,
62 (VkInstanceCreateFlags)0,
63 &appInfo,
64 (deUint32)layerNamePtrs.size(),
65 layerNamePtrs.empty() ? DE_NULL : &layerNamePtrs[0],
66 (deUint32)extensionNamePtrs.size(),
67 extensionNamePtrs.empty() ? DE_NULL : &extensionNamePtrs[0],
68 };
69
70 for (size_t ndx = 0; ndx < enabledLayers.size(); ++ndx)
71 layerNamePtrs[ndx] = enabledLayers[ndx].c_str();
72
73 for (size_t ndx = 0; ndx < enabledExtensions.size(); ++ndx)
74 extensionNamePtrs[ndx] = enabledExtensions[ndx].c_str();
75
76 return createInstance(vkPlatform, &instanceInfo, pAllocator);
77 }
78
createDefaultInstance(const PlatformInterface & vkPlatform,deUint32 apiVersion)79 Move<VkInstance> createDefaultInstance (const PlatformInterface& vkPlatform, deUint32 apiVersion)
80 {
81 return createDefaultInstance(vkPlatform, apiVersion, vector<string>(), vector<string>(), DE_NULL);
82 }
83
createInstanceWithExtensions(const PlatformInterface & vkp,const deUint32 version,const std::vector<std::string> requiredExtensions)84 Move<VkInstance> createInstanceWithExtensions (const PlatformInterface& vkp,
85 const deUint32 version,
86 const std::vector<std::string> requiredExtensions)
87 {
88 std::vector<std::string> extensionPtrs;
89 const std::vector<VkExtensionProperties> availableExtensions = enumerateInstanceExtensionProperties(vkp, DE_NULL);
90 for (size_t extensionID = 0; extensionID < requiredExtensions.size(); extensionID++)
91 {
92 if (!isInstanceExtensionSupported(version, availableExtensions, RequiredExtension(requiredExtensions[extensionID])))
93 TCU_THROW(NotSupportedError, (requiredExtensions[extensionID] + " is not supported").c_str());
94
95 if (!isCoreInstanceExtension(version, requiredExtensions[extensionID]))
96 extensionPtrs.push_back(requiredExtensions[extensionID]);
97 }
98
99 return createDefaultInstance(vkp, version, std::vector<std::string>() /* layers */, extensionPtrs, DE_NULL);
100 }
101
createInstanceWithExtension(const PlatformInterface & vkp,const deUint32 version,const std::string requiredExtension)102 Move<VkInstance> createInstanceWithExtension (const PlatformInterface& vkp,
103 const deUint32 version,
104 const std::string requiredExtension)
105 {
106 return createInstanceWithExtensions(vkp, version, std::vector<std::string>(1, requiredExtension));
107 }
108
chooseDeviceIndex(const InstanceInterface & vkInstance,const VkInstance instance,const tcu::CommandLine & cmdLine)109 deUint32 chooseDeviceIndex (const InstanceInterface& vkInstance, const VkInstance instance, const tcu::CommandLine& cmdLine)
110 {
111 const vector<VkPhysicalDevice> devices = enumeratePhysicalDevices(vkInstance, instance);
112
113 if (devices.empty())
114 TCU_THROW(NotSupportedError, "No Vulkan devices available");
115
116 const deUint32 deviceIdFromCmdLine = cmdLine.getVKDeviceId();
117 if (!de::inBounds(deviceIdFromCmdLine, 0u, static_cast<deUint32>(devices.size() + 1)))
118 TCU_THROW(InternalError, "Invalid --deqp-vk-device-id");
119
120 if (deviceIdFromCmdLine > 0)
121 return deviceIdFromCmdLine - 1u;
122
123 deUint32 maxReportedApiVersion = 0u;
124 deUint32 ndxOfMaximumVersion = 0u;
125
126 for (deUint32 deviceNdx = 0u; deviceNdx < devices.size(); ++deviceNdx)
127 {
128 const VkPhysicalDeviceProperties props = getPhysicalDeviceProperties(vkInstance, devices[deviceNdx]);
129
130 if (props.apiVersion > maxReportedApiVersion)
131 {
132 maxReportedApiVersion = props.apiVersion;
133 ndxOfMaximumVersion = deviceNdx;
134 }
135 }
136
137 return ndxOfMaximumVersion;
138 }
139
chooseDevice(const InstanceInterface & vkInstance,const VkInstance instance,const tcu::CommandLine & cmdLine)140 VkPhysicalDevice chooseDevice (const InstanceInterface& vkInstance, const VkInstance instance, const tcu::CommandLine& cmdLine)
141 {
142 const vector<VkPhysicalDevice> devices = enumeratePhysicalDevices(vkInstance, instance);
143
144 if (devices.empty())
145 TCU_THROW(NotSupportedError, "No Vulkan devices available");
146
147 const size_t deviceId = chooseDeviceIndex(vkInstance, instance, cmdLine);
148 return devices[deviceId];
149 }
150
151 } // vk
152