Home | History | Annotate | Download | only in vulkan
      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 
     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 
     79 Move<VkInstance> createDefaultInstance (const PlatformInterface& vkPlatform, deUint32 apiVersion)
     80 {
     81 	return createDefaultInstance(vkPlatform, apiVersion, vector<string>(), vector<string>(), DE_NULL);
     82 }
     83 
     84 deUint32 chooseDeviceIndex (const InstanceInterface& vkInstance, const VkInstance instance, const tcu::CommandLine& cmdLine)
     85 {
     86 	const vector<VkPhysicalDevice>			devices					= enumeratePhysicalDevices(vkInstance, instance);
     87 
     88 	if (devices.empty())
     89 		TCU_THROW(NotSupportedError, "No Vulkan devices available");
     90 
     91 	const deUint32							deviceIdFromCmdLine		= cmdLine.getVKDeviceId();
     92 	if (!de::inBounds(deviceIdFromCmdLine, 0u, static_cast<deUint32>(devices.size() + 1)))
     93 		TCU_THROW(InternalError, "Invalid --deqp-vk-device-id");
     94 
     95 	if (deviceIdFromCmdLine > 0)
     96 		return deviceIdFromCmdLine - 1u;
     97 
     98 	deUint32								maxReportedApiVersion	= 0u;
     99 	deUint32								ndxOfMaximumVersion		= 0u;
    100 
    101 	for (deUint32 deviceNdx = 0u; deviceNdx < devices.size(); ++deviceNdx)
    102 	{
    103 		const VkPhysicalDeviceProperties	props					= getPhysicalDeviceProperties(vkInstance, devices[deviceNdx]);
    104 
    105 		if (props.apiVersion > maxReportedApiVersion)
    106 		{
    107 			maxReportedApiVersion = props.apiVersion;
    108 			ndxOfMaximumVersion = deviceNdx;
    109 		}
    110 	}
    111 
    112 	return ndxOfMaximumVersion;
    113 }
    114 
    115 VkPhysicalDevice chooseDevice (const InstanceInterface& vkInstance, const VkInstance instance, const tcu::CommandLine& cmdLine)
    116 {
    117 	const vector<VkPhysicalDevice>	devices		= enumeratePhysicalDevices(vkInstance, instance);
    118 
    119 	if (devices.empty())
    120 		TCU_THROW(NotSupportedError, "No Vulkan devices available");
    121 
    122 	const size_t					deviceId	= chooseDeviceIndex(vkInstance, instance, cmdLine);
    123 	return devices[deviceId];
    124 }
    125 
    126 } // vk
    127