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 "Device.hpp"
19 #include "Driver.hpp"
20
21 #include "gmock/gmock.h"
22 #include "gtest/gtest.h"
23
24 class BasicTest : public testing::Test
25 {
26 protected:
27 static Driver driver;
28
SetUp()29 void SetUp() override
30 {
31 ASSERT_TRUE(driver.loadSwiftShader());
32 }
33
TearDown()34 void TearDown() override
35 {
36 driver.unload();
37 }
38 };
39
40 Driver BasicTest::driver;
41
TEST_F(BasicTest,ICD_Check)42 TEST_F(BasicTest, ICD_Check)
43 {
44 auto createInstance = driver.vk_icdGetInstanceProcAddr(VK_NULL_HANDLE, "vkCreateInstance");
45 EXPECT_NE(createInstance, nullptr);
46
47 auto enumerateInstanceExtensionProperties =
48 driver.vk_icdGetInstanceProcAddr(VK_NULL_HANDLE, "vkEnumerateInstanceExtensionProperties");
49 EXPECT_NE(enumerateInstanceExtensionProperties, nullptr);
50
51 auto enumerateInstanceLayerProperties =
52 driver.vk_icdGetInstanceProcAddr(VK_NULL_HANDLE, "vkEnumerateInstanceLayerProperties");
53 EXPECT_NE(enumerateInstanceLayerProperties, nullptr);
54
55 auto enumerateInstanceVersion = driver.vk_icdGetInstanceProcAddr(VK_NULL_HANDLE, "vkEnumerateInstanceVersion");
56 EXPECT_NE(enumerateInstanceVersion, nullptr);
57
58 auto bad_function = driver.vk_icdGetInstanceProcAddr(VK_NULL_HANDLE, "bad_function");
59 EXPECT_EQ(bad_function, nullptr);
60 }
61
TEST_F(BasicTest,Version)62 TEST_F(BasicTest, Version)
63 {
64 uint32_t apiVersion = 0;
65 uint32_t expectedVersion = static_cast<uint32_t>(VK_API_VERSION_1_2);
66 VkResult result = driver.vkEnumerateInstanceVersion(&apiVersion);
67 EXPECT_EQ(apiVersion, expectedVersion);
68
69 const VkInstanceCreateInfo createInfo = {
70 VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO, // sType
71 nullptr, // pNext
72 0, // flags
73 nullptr, // pApplicationInfo
74 0, // enabledLayerCount
75 nullptr, // ppEnabledLayerNames
76 0, // enabledExtensionCount
77 nullptr, // ppEnabledExtensionNames
78 };
79 VkInstance instance = VK_NULL_HANDLE;
80 result = driver.vkCreateInstance(&createInfo, nullptr, &instance);
81 EXPECT_EQ(result, VK_SUCCESS);
82
83 ASSERT_TRUE(driver.resolve(instance));
84
85 uint32_t pPhysicalDeviceCount = 0;
86 result = driver.vkEnumeratePhysicalDevices(instance, &pPhysicalDeviceCount, nullptr);
87 EXPECT_EQ(result, VK_SUCCESS);
88 EXPECT_EQ(pPhysicalDeviceCount, 1U);
89
90 VkPhysicalDevice pPhysicalDevice = VK_NULL_HANDLE;
91 result = driver.vkEnumeratePhysicalDevices(instance, &pPhysicalDeviceCount, &pPhysicalDevice);
92 EXPECT_EQ(result, VK_SUCCESS);
93 EXPECT_NE(pPhysicalDevice, (VkPhysicalDevice)VK_NULL_HANDLE);
94
95 VkPhysicalDeviceProperties physicalDeviceProperties;
96 driver.vkGetPhysicalDeviceProperties(pPhysicalDevice, &physicalDeviceProperties);
97 EXPECT_EQ(physicalDeviceProperties.apiVersion, expectedVersion);
98 EXPECT_EQ(physicalDeviceProperties.deviceID, 0xC0DEU);
99 EXPECT_EQ(physicalDeviceProperties.deviceType, VK_PHYSICAL_DEVICE_TYPE_CPU);
100
101 EXPECT_NE(strstr(physicalDeviceProperties.deviceName, "SwiftShader Device"), nullptr);
102
103 VkPhysicalDeviceProperties2 physicalDeviceProperties2;
104 VkPhysicalDeviceDriverPropertiesKHR physicalDeviceDriverProperties;
105 physicalDeviceProperties2.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PROPERTIES_2;
106 physicalDeviceProperties2.pNext = &physicalDeviceDriverProperties;
107 physicalDeviceDriverProperties.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_DRIVER_PROPERTIES_KHR;
108 physicalDeviceDriverProperties.pNext = nullptr;
109 physicalDeviceDriverProperties.driverID = (VkDriverIdKHR)0;
110 driver.vkGetPhysicalDeviceProperties2(pPhysicalDevice, &physicalDeviceProperties2);
111 EXPECT_EQ(physicalDeviceDriverProperties.driverID, VK_DRIVER_ID_GOOGLE_SWIFTSHADER_KHR);
112
113 driver.vkDestroyInstance(instance, nullptr);
114 }
115 /*
116 TEST_F(BasicTest, UnsupportedDeviceExtension_DISABLED)
117 {
118 uint32_t apiVersion = 0;
119 VkResult result = driver.vkEnumerateInstanceVersion(&apiVersion);
120
121 const VkInstanceCreateInfo createInfo = {
122 VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO, // sType
123 nullptr, // pNext
124 0, // flags
125 nullptr, // pApplicationInfo
126 0, // enabledLayerCount
127 nullptr, // ppEnabledLayerNames
128 0, // enabledExtensionCount
129 nullptr, // ppEnabledExtensionNames
130 };
131 VkInstance instance = VK_NULL_HANDLE;
132 result = driver.vkCreateInstance(&createInfo, nullptr, &instance);
133 EXPECT_EQ(result, VK_SUCCESS);
134
135 ASSERT_TRUE(driver.resolve(instance));
136
137 VkBaseInStructure unsupportedExt = { VK_STRUCTURE_TYPE_SHADER_MODULE_VALIDATION_CACHE_CREATE_INFO_EXT, nullptr };
138
139 // Gather all physical devices
140 std::vector<VkPhysicalDevice> physicalDevices;
141 result = Device::GetPhysicalDevices(&driver, instance, physicalDevices);
142 EXPECT_EQ(result, VK_SUCCESS);
143
144 // Inspect each physical device's queue families for compute support.
145 for(auto physicalDevice : physicalDevices)
146 {
147 int queueFamilyIndex = Device::GetComputeQueueFamilyIndex(&driver, physicalDevice);
148 if(queueFamilyIndex < 0)
149 {
150 continue;
151 }
152
153 const float queuePrioritory = 1.0f;
154 const VkDeviceQueueCreateInfo deviceQueueCreateInfo = {
155 VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO, // sType
156 nullptr, // pNext
157 0, // flags
158 (uint32_t)queueFamilyIndex, // queueFamilyIndex
159 1, // queueCount
160 &queuePrioritory, // pQueuePriorities
161 };
162
163 const VkDeviceCreateInfo deviceCreateInfo = {
164 VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO, // sType
165 &unsupportedExt, // pNext
166 0, // flags
167 1, // queueCreateInfoCount
168 &deviceQueueCreateInfo, // pQueueCreateInfos
169 0, // enabledLayerCount
170 nullptr, // ppEnabledLayerNames
171 0, // enabledExtensionCount
172 nullptr, // ppEnabledExtensionNames
173 nullptr, // pEnabledFeatures
174 };
175
176 VkDevice device;
177 result = driver.vkCreateDevice(physicalDevice, &deviceCreateInfo, nullptr, &device);
178 EXPECT_EQ(result, VK_SUCCESS);
179 driver.vkDestroyDevice(device, nullptr);
180 }
181
182 driver.vkDestroyInstance(instance, nullptr);
183 }
184 */
185