• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "deSTLUtil.hpp"
25 #include "vkDeviceUtil.hpp"
26 #include "vkQueryUtil.hpp"
27 #include "vkRefUtil.hpp"
28 #include "vkApiVersion.hpp"
29 #include "vkDebugReportUtil.hpp"
30 
31 #include "tcuCommandLine.hpp"
32 
33 #include "qpInfo.h"
34 
35 namespace vk
36 {
37 
38 using std::vector;
39 using std::string;
40 
createDefaultInstance(const PlatformInterface & vkPlatform,deUint32 apiVersion,const vector<string> & enabledLayers,const vector<string> & enabledExtensions,const VkAllocationCallbacks * pAllocator)41 Move<VkInstance> createDefaultInstance (const PlatformInterface&		vkPlatform,
42 										deUint32						apiVersion,
43 										const vector<string>&			enabledLayers,
44 										const vector<string>&			enabledExtensions,
45 										const VkAllocationCallbacks*	pAllocator)
46 {
47 	bool			validationEnabled	= (!enabledLayers.empty());
48 	vector<string>	actualExtensions	= enabledExtensions;
49 
50 	if (validationEnabled)
51 	{
52 		// Make sure the debug report extension is enabled when validation is enabled.
53 		if (!isDebugReportSupported(vkPlatform))
54 			TCU_THROW(NotSupportedError, "VK_EXT_debug_report is not supported");
55 
56 		if (!de::contains(begin(actualExtensions), end(actualExtensions), "VK_EXT_debug_report"))
57 			actualExtensions.push_back("VK_EXT_debug_report");
58 	}
59 
60 	vector<const char*>		layerNamePtrs		(enabledLayers.size());
61 	vector<const char*>		extensionNamePtrs	(actualExtensions.size());
62 
63 	for (size_t ndx = 0; ndx < enabledLayers.size(); ++ndx)
64 		layerNamePtrs[ndx] = enabledLayers[ndx].c_str();
65 
66 	for (size_t ndx = 0; ndx < actualExtensions.size(); ++ndx)
67 		extensionNamePtrs[ndx] = actualExtensions[ndx].c_str();
68 
69 	const struct VkApplicationInfo		appInfo			=
70 	{
71 		VK_STRUCTURE_TYPE_APPLICATION_INFO,
72 		DE_NULL,
73 		"deqp",									// pAppName
74 		qpGetReleaseId(),						// appVersion
75 		"deqp",									// pEngineName
76 		qpGetReleaseId(),						// engineVersion
77 		apiVersion								// apiVersion
78 	};
79 	const struct VkInstanceCreateInfo	instanceInfo	=
80 	{
81 		VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO,
82 		DE_NULL,
83 		(VkInstanceCreateFlags)0,
84 		&appInfo,
85 		(deUint32)layerNamePtrs.size(),
86 		(validationEnabled ? layerNamePtrs.data() : DE_NULL),
87 		(deUint32)extensionNamePtrs.size(),
88 		(extensionNamePtrs.empty() ? DE_NULL : extensionNamePtrs.data()),
89 	};
90 
91 	return createInstance(vkPlatform, &instanceInfo, pAllocator);
92 }
93 
createDefaultInstance(const PlatformInterface & vkPlatform,deUint32 apiVersion)94 Move<VkInstance> createDefaultInstance (const PlatformInterface& vkPlatform, deUint32 apiVersion)
95 {
96 	return createDefaultInstance(vkPlatform, apiVersion, vector<string>(), vector<string>(), DE_NULL);
97 }
98 
chooseDeviceIndex(const InstanceInterface & vkInstance,const VkInstance instance,const tcu::CommandLine & cmdLine)99 deUint32 chooseDeviceIndex (const InstanceInterface& vkInstance, const VkInstance instance, const tcu::CommandLine& cmdLine)
100 {
101 	const vector<VkPhysicalDevice>			devices					= enumeratePhysicalDevices(vkInstance, instance);
102 
103 	if (devices.empty())
104 		TCU_THROW(NotSupportedError, "No Vulkan devices available");
105 
106 	const deUint32							deviceIdFromCmdLine		= cmdLine.getVKDeviceId();
107 	if (!de::inBounds(deviceIdFromCmdLine, 0u, static_cast<deUint32>(devices.size() + 1)))
108 		TCU_THROW(InternalError, "Invalid --deqp-vk-device-id");
109 
110 	if (deviceIdFromCmdLine > 0)
111 		return deviceIdFromCmdLine - 1u;
112 
113 	deUint32								maxReportedApiVersion	= 0u;
114 	deUint32								ndxOfMaximumVersion		= 0u;
115 
116 	for (deUint32 deviceNdx = 0u; deviceNdx < devices.size(); ++deviceNdx)
117 	{
118 		const VkPhysicalDeviceProperties	props					= getPhysicalDeviceProperties(vkInstance, devices[deviceNdx]);
119 
120 		if (props.apiVersion > maxReportedApiVersion)
121 		{
122 			maxReportedApiVersion = props.apiVersion;
123 			ndxOfMaximumVersion = deviceNdx;
124 		}
125 	}
126 
127 	return ndxOfMaximumVersion;
128 }
129 
chooseDevice(const InstanceInterface & vkInstance,const VkInstance instance,const tcu::CommandLine & cmdLine)130 VkPhysicalDevice chooseDevice (const InstanceInterface& vkInstance, const VkInstance instance, const tcu::CommandLine& cmdLine)
131 {
132 	const vector<VkPhysicalDevice>	devices		= enumeratePhysicalDevices(vkInstance, instance);
133 
134 	if (devices.empty())
135 		TCU_THROW(NotSupportedError, "No Vulkan devices available");
136 
137 	const size_t					deviceId	= chooseDeviceIndex(vkInstance, instance, cmdLine);
138 	return devices[deviceId];
139 }
140 
141 } // vk
142