1 // Copyright 2018 The SwiftShader Authors. All Rights Reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //    http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 // Vulkan unit tests that provide coverage for functionality not tested by
16 // the dEQP test suite. Also used as a smoke test.
17 
18 #include "gtest/gtest.h"
19 #include "gmock/gmock.h"
20 
21 #include <vulkan/vulkan.h>
22 #include <vulkan/vk_icd.h>
23 
24 #include <cstring>
25 
26 typedef PFN_vkVoidFunction(__stdcall *vk_icdGetInstanceProcAddrPtr)(VkInstance, const char*);
27 
28 #if defined(_WIN32)
29 #include <Windows.h>
30 #endif
31 
32 class SwiftShaderVulkanTest : public testing::Test
33 {
34 protected:
SetUp()35 	void SetUp() override
36 	{
37 		HMODULE libVulkan = nullptr;
38 		const char* libVulkanName = nullptr;
39 
40 		#if defined(_WIN64)
41 			#if defined(NDEBUG)
42 				libVulkanName = "../../out/Release_x64/vk_swiftshader.dll";
43 			#else
44 				libVulkanName = "../../out/Debug_x64/vk_swiftshader.dll";
45 			#endif
46 		#else
47 			#error Unimplemented platform
48 		#endif
49 
50 		#if defined(_WIN32)
51 			libVulkan = LoadLibraryA(libVulkanName);
52 			EXPECT_NE((HMODULE)NULL, libVulkan);
53 			vk_icdGetInstanceProcAddr = (vk_icdGetInstanceProcAddrPtr)GetProcAddress(libVulkan, "vk_icdGetInstanceProcAddr");
54 			EXPECT_NE((vk_icdGetInstanceProcAddrPtr)nullptr, vk_icdGetInstanceProcAddr);
55 		#endif
56 	}
57 
58 	vk_icdGetInstanceProcAddrPtr vk_icdGetInstanceProcAddr = nullptr;
59 };
60 
TEST_F(SwiftShaderVulkanTest,ICD_Check)61 TEST_F(SwiftShaderVulkanTest, ICD_Check)
62 {
63 	if(vk_icdGetInstanceProcAddr)
64 	{
65 		auto createInstance = vk_icdGetInstanceProcAddr(VK_NULL_HANDLE, "vkCreateInstance");
66 		EXPECT_NE(createInstance, nullptr);
67 
68 		auto enumerateInstanceExtensionProperties = vk_icdGetInstanceProcAddr(VK_NULL_HANDLE, "vkEnumerateInstanceExtensionProperties");
69 		EXPECT_NE(enumerateInstanceExtensionProperties, nullptr);
70 
71 		auto enumerateInstanceLayerProperties = vk_icdGetInstanceProcAddr(VK_NULL_HANDLE, "vkEnumerateInstanceLayerProperties");
72 		EXPECT_NE(enumerateInstanceLayerProperties, nullptr);
73 
74 		auto enumerateInstanceVersion = vk_icdGetInstanceProcAddr(VK_NULL_HANDLE, "vkEnumerateInstanceVersion");
75 		EXPECT_NE(enumerateInstanceVersion, nullptr);
76 
77 		auto bad_function = vk_icdGetInstanceProcAddr(VK_NULL_HANDLE, "bad_function");
78 		EXPECT_EQ(bad_function, nullptr);
79 	}
80 }
81 
TEST_F(SwiftShaderVulkanTest,Version)82 TEST_F(SwiftShaderVulkanTest, Version)
83 {
84 	uint32_t apiVersion = 0;
85 	VkResult result = vkEnumerateInstanceVersion(&apiVersion);
86 	EXPECT_EQ(apiVersion, VK_API_VERSION_1_1);
87 
88 	const VkInstanceCreateInfo createInfo =
89 	{
90 		VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO, // sType
91 		nullptr, // pNext
92 		0,       // flags
93 		nullptr, // pApplicationInfo
94 		0,       // enabledLayerCount
95 		nullptr, // ppEnabledLayerNames
96 		0,       // enabledExtensionCount
97 		nullptr, // ppEnabledExtensionNames
98 	};
99 	VkInstance instance = VK_NULL_HANDLE;
100 	result = vkCreateInstance(&createInfo, nullptr, &instance);
101 	EXPECT_EQ(result, VK_SUCCESS);
102 
103 	uint32_t pPhysicalDeviceCount = 0;
104 	result = vkEnumeratePhysicalDevices(instance, &pPhysicalDeviceCount, nullptr);
105 	EXPECT_EQ(result, VK_SUCCESS);
106 	EXPECT_EQ(pPhysicalDeviceCount, 1);
107 
108 	VkPhysicalDevice pPhysicalDevice = VK_NULL_HANDLE;
109 	result = vkEnumeratePhysicalDevices(instance, &pPhysicalDeviceCount, &pPhysicalDevice);
110 	EXPECT_EQ(result, VK_SUCCESS);
111 	EXPECT_NE(pPhysicalDevice, (VkPhysicalDevice)VK_NULL_HANDLE);
112 
113 	VkPhysicalDeviceProperties physicalDeviceProperties;
114 	vkGetPhysicalDeviceProperties(pPhysicalDevice, &physicalDeviceProperties);
115 	EXPECT_EQ(physicalDeviceProperties.apiVersion, VK_API_VERSION_1_1);
116 	EXPECT_EQ(physicalDeviceProperties.deviceID, 0xC0DE);
117 	EXPECT_EQ(physicalDeviceProperties.deviceType, VK_PHYSICAL_DEVICE_TYPE_CPU);
118 
119 	EXPECT_EQ(strncmp(physicalDeviceProperties.deviceName, "SwiftShader Device", VK_MAX_PHYSICAL_DEVICE_NAME_SIZE), 0);
120 }
121