2 * Copyright 2018 Józef Kucia for CodeWeavers
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20 #include "wine/heap.h"
21 #include "wine/vulkan.h"
22 #include "wine/test.h"
24 static VkResult
create_instance(uint32_t extension_count
,
25 const char * const *enabled_extensions
, VkInstance
*vk_instance
)
27 VkInstanceCreateInfo create_info
;
29 create_info
.sType
= VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO
;
30 create_info
.pNext
= NULL
;
31 create_info
.flags
= 0;
32 create_info
.pApplicationInfo
= NULL
;
33 create_info
.enabledLayerCount
= 0;
34 create_info
.ppEnabledLayerNames
= NULL
;
35 create_info
.enabledExtensionCount
= extension_count
;
36 create_info
.ppEnabledExtensionNames
= enabled_extensions
;
38 return vkCreateInstance(&create_info
, NULL
, vk_instance
);
41 #define create_instance_skip(a, b, c) create_instance_skip_(__LINE__, a, b, c)
42 static VkResult
create_instance_skip_(unsigned int line
, uint32_t extension_count
,
43 const char * const *enabled_extensions
, VkInstance
*vk_instance
)
47 if ((vr
= create_instance(extension_count
, enabled_extensions
, vk_instance
)) >= 0)
52 case VK_ERROR_EXTENSION_NOT_PRESENT
:
53 if (extension_count
== 1)
54 skip_(__FILE__
, line
)("Instance extension '%s' not supported.\n", enabled_extensions
[0]);
56 skip_(__FILE__
, line
)("Instance extensions not supported.\n");
60 skip_(__FILE__
, line
)("Failed to create Vulkan instance, vr %d.\n", vr
);
67 static VkBool32
find_queue_family(VkPhysicalDevice vk_physical_device
,
68 VkQueueFlags flags
, uint32_t *family_index
)
70 VkQueueFamilyProperties
*properties
;
71 VkBool32 ret
= VK_FALSE
;
74 vkGetPhysicalDeviceQueueFamilyProperties(vk_physical_device
, &count
, NULL
);
75 properties
= heap_calloc(count
, sizeof(*properties
));
76 ok(!!properties
, "Failed to allocate memory.\n");
77 vkGetPhysicalDeviceQueueFamilyProperties(vk_physical_device
, &count
, properties
);
79 for (i
= 0; i
< count
; ++i
)
81 if ((properties
[i
].queueFlags
& flags
) == flags
)
89 heap_free(properties
);
93 static VkResult
create_device(VkPhysicalDevice vk_physical_device
,
94 uint32_t extension_count
, const char * const *enabled_extensions
,
95 const void *next
, VkDevice
*vk_device
)
97 VkDeviceQueueCreateInfo queue_info
;
98 VkDeviceCreateInfo create_info
;
99 float priority
= 0.0f
;
101 queue_info
.sType
= VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO
;
102 queue_info
.pNext
= NULL
;
103 queue_info
.flags
= 0;
104 if (!find_queue_family(vk_physical_device
, VK_QUEUE_GRAPHICS_BIT
, &queue_info
.queueFamilyIndex
))
106 trace("Failed to find queue family.\n");
107 return VK_ERROR_INITIALIZATION_FAILED
;
109 queue_info
.queueCount
= 1;
110 queue_info
.pQueuePriorities
= &priority
;
112 create_info
.sType
= VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO
;
113 create_info
.pNext
= next
;
114 create_info
.flags
= 0;
115 create_info
.queueCreateInfoCount
= 1;
116 create_info
.pQueueCreateInfos
= &queue_info
;
117 create_info
.enabledLayerCount
= 0;
118 create_info
.ppEnabledLayerNames
= NULL
;
119 create_info
.enabledExtensionCount
= extension_count
;
120 create_info
.ppEnabledExtensionNames
= enabled_extensions
;
121 create_info
.pEnabledFeatures
= NULL
;
123 return vkCreateDevice(vk_physical_device
, &create_info
, NULL
, vk_device
);
126 static void test_instance_version(void)
128 PFN_vkEnumerateInstanceVersion pfn_vkEnumerateInstanceVersion
;
132 pfn_vkEnumerateInstanceVersion
= (PFN_vkEnumerateInstanceVersion
)vkGetInstanceProcAddr(
133 NULL
, "vkEnumerateInstanceVersion");
134 if (!pfn_vkEnumerateInstanceVersion
)
136 skip("vkEnumerateInstanceVersion() is not available.\n");
140 vr
= pfn_vkEnumerateInstanceVersion(&version
);
141 ok(vr
== VK_SUCCESS
, "Got unexpected VkResult %d.\n", vr
);
142 ok(version
>= VK_API_VERSION_1_0
, "Invalid version %#x.\n", version
);
143 trace("Vulkan version %u.%u.%u.\n",
144 VK_VERSION_MAJOR(version
), VK_VERSION_MINOR(version
), VK_VERSION_PATCH(version
));
147 static void enumerate_physical_device(VkPhysicalDevice vk_physical_device
)
149 VkPhysicalDeviceProperties properties
;
151 vkGetPhysicalDeviceProperties(vk_physical_device
, &properties
);
153 trace("Device '%s', %#x:%#x, driver version %u.%u.%u (%#x), api version %u.%u.%u.\n",
154 properties
.deviceName
, properties
.vendorID
, properties
.deviceID
,
155 VK_VERSION_MAJOR(properties
.driverVersion
), VK_VERSION_MINOR(properties
.driverVersion
),
156 VK_VERSION_PATCH(properties
.driverVersion
), properties
.driverVersion
,
157 VK_VERSION_MAJOR(properties
.apiVersion
), VK_VERSION_MINOR(properties
.apiVersion
),
158 VK_VERSION_PATCH(properties
.apiVersion
));
161 static void enumerate_device_queues(VkPhysicalDevice vk_physical_device
)
163 VkPhysicalDeviceProperties device_properties
;
164 VkQueueFamilyProperties
*properties
;
167 vkGetPhysicalDeviceProperties(vk_physical_device
, &device_properties
);
169 vkGetPhysicalDeviceQueueFamilyProperties(vk_physical_device
, &count
, NULL
);
170 properties
= heap_calloc(count
, sizeof(*properties
));
171 ok(!!properties
, "Failed to allocate memory.\n");
172 vkGetPhysicalDeviceQueueFamilyProperties(vk_physical_device
, &count
, properties
);
174 for (i
= 0; i
< count
; ++i
)
176 trace("Device '%s', queue family %u: flags %#x count %u.\n",
177 device_properties
.deviceName
, i
, properties
[i
].queueFlags
, properties
[i
].queueCount
);
180 heap_free(properties
);
183 static void test_physical_device_groups(void)
185 PFN_vkEnumeratePhysicalDeviceGroupsKHR vkEnumeratePhysicalDeviceGroupsKHR
;
186 VkPhysicalDeviceGroupProperties
*properties
;
187 VkDeviceGroupDeviceCreateInfo group_info
;
188 VkInstance vk_instance
;
189 uint32_t i
, j
, count
;
193 static const char *extensions
[] =
195 VK_KHR_DEVICE_GROUP_CREATION_EXTENSION_NAME
,
198 if ((vr
= create_instance_skip(ARRAY_SIZE(extensions
), extensions
, &vk_instance
)) < 0)
200 ok(vr
== VK_SUCCESS
, "Got unexpected VkResult %d.\n", vr
);
202 vkEnumeratePhysicalDeviceGroupsKHR
203 = (void *)vkGetInstanceProcAddr(vk_instance
, "vkEnumeratePhysicalDeviceGroupsKHR");
204 ok(!!vkEnumeratePhysicalDeviceGroupsKHR
, "Failed to get proc addr.\n");
206 vr
= vkEnumeratePhysicalDeviceGroupsKHR(vk_instance
, &count
, NULL
);
207 ok(vr
== VK_SUCCESS
, "Got unexpected VkResult %d.\n", vr
);
208 ok(count
> 0, "Unexpected device group count %u.\n", count
);
210 properties
= heap_calloc(count
, sizeof(*properties
));
211 ok(!!properties
, "Failed to allocate memory.\n");
212 vr
= vkEnumeratePhysicalDeviceGroupsKHR(vk_instance
, &count
, properties
);
213 ok(vr
== VK_SUCCESS
, "Got unexpected VkResult %d.\n", vr
);
215 for (i
= 0; i
< count
; ++i
)
217 trace("Group[%u] count %u, subset allocation %#x\n",
218 i
, properties
[i
].physicalDeviceCount
, properties
[i
].subsetAllocation
);
219 for (j
= 0; j
< properties
[i
].physicalDeviceCount
; ++j
)
220 enumerate_physical_device(properties
[i
].physicalDevices
[j
]);
223 if ((vr
= create_device(properties
->physicalDevices
[0], 0, NULL
, NULL
, &vk_device
)) < 0)
225 skip("Failed to create device, vr %d.\n", vr
);
228 ok(vr
== VK_SUCCESS
, "Got unexpected VkResult %d.\n", vr
);
229 vkDestroyDevice(vk_device
, NULL
);
231 group_info
.sType
= VK_STRUCTURE_TYPE_DEVICE_GROUP_DEVICE_CREATE_INFO
;
232 group_info
.pNext
= NULL
;
233 group_info
.physicalDeviceCount
= properties
->physicalDeviceCount
;
234 group_info
.pPhysicalDevices
= properties
->physicalDevices
;
235 vr
= create_device(group_info
.pPhysicalDevices
[0], 0, NULL
, &group_info
, &vk_device
);
236 ok(vr
== VK_SUCCESS
, "Failed to create device, VkResult %d.\n", vr
);
237 vkDestroyDevice(vk_device
, NULL
);
239 heap_free(properties
);
241 vkDestroyInstance(vk_instance
, NULL
);
244 static void test_destroy_command_pool(VkPhysicalDevice vk_physical_device
)
246 VkCommandBufferAllocateInfo allocate_info
;
247 VkCommandPoolCreateInfo pool_info
;
248 VkCommandBuffer vk_cmd_buffers
[4];
249 uint32_t queue_family_index
;
250 VkCommandPool vk_cmd_pool
;
254 if ((vr
= create_device(vk_physical_device
, 0, NULL
, NULL
, &vk_device
)) < 0)
256 skip("Failed to create device, vr %d.\n", vr
);
260 find_queue_family(vk_physical_device
, VK_QUEUE_GRAPHICS_BIT
, &queue_family_index
);
262 pool_info
.sType
= VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO
;
263 pool_info
.pNext
= NULL
;
265 pool_info
.queueFamilyIndex
= queue_family_index
;
266 vr
= vkCreateCommandPool(vk_device
, &pool_info
, NULL
, &vk_cmd_pool
);
267 ok(vr
== VK_SUCCESS
, "Got unexpected VkResult %d.\n", vr
);
269 allocate_info
.sType
= VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO
;
270 allocate_info
.pNext
= NULL
;
271 allocate_info
.commandPool
= vk_cmd_pool
;
272 allocate_info
.level
= VK_COMMAND_BUFFER_LEVEL_PRIMARY
;
273 allocate_info
.commandBufferCount
= ARRAY_SIZE(vk_cmd_buffers
);
274 vr
= vkAllocateCommandBuffers(vk_device
, &allocate_info
, vk_cmd_buffers
);
275 ok(vr
== VK_SUCCESS
, "Got unexpected VkResult %d.\n", vr
);
277 vkDestroyCommandPool(vk_device
, vk_cmd_pool
, NULL
);
278 vkDestroyCommandPool(vk_device
, VK_NULL_HANDLE
, NULL
);
280 vkDestroyDevice(vk_device
, NULL
);
283 static void test_unsupported_instance_extensions(void)
285 VkInstance vk_instance
;
289 static const char *extensions
[] =
291 "VK_KHR_xcb_surface",
292 "VK_KHR_xlib_surface",
295 for (i
= 0; i
< ARRAY_SIZE(extensions
); ++i
)
297 vr
= create_instance(1, &extensions
[i
], &vk_instance
);
298 ok(vr
== VK_ERROR_EXTENSION_NOT_PRESENT
,
299 "Got VkResult %d for extension %s.\n", vr
, extensions
[i
]);
303 static void for_each_device(void (*test_func
)(VkPhysicalDevice
))
305 VkPhysicalDevice
*vk_physical_devices
;
306 VkInstance vk_instance
;
311 if ((vr
= create_instance_skip(0, NULL
, &vk_instance
)) < 0)
313 ok(vr
== VK_SUCCESS
, "Got unexpected VkResult %d.\n", vr
);
315 vr
= vkEnumeratePhysicalDevices(vk_instance
, &count
, NULL
);
316 ok(vr
== VK_SUCCESS
, "Got unexpected VkResult %d.\n", vr
);
319 skip("No physical devices.\n");
320 vkDestroyInstance(vk_instance
, NULL
);
324 vk_physical_devices
= heap_calloc(count
, sizeof(*vk_physical_devices
));
325 ok(!!vk_physical_devices
, "Failed to allocate memory.\n");
326 vr
= vkEnumeratePhysicalDevices(vk_instance
, &count
, vk_physical_devices
);
327 ok(vr
== VK_SUCCESS
, "Got unexpected VkResult %d.\n", vr
);
329 for (i
= 0; i
< count
; ++i
)
330 test_func(vk_physical_devices
[i
]);
332 heap_free(vk_physical_devices
);
334 vkDestroyInstance(vk_instance
, NULL
);
339 test_instance_version();
340 for_each_device(enumerate_physical_device
);
341 for_each_device(enumerate_device_queues
);
342 test_physical_device_groups();
343 for_each_device(test_destroy_command_pool
);
344 test_unsupported_instance_extensions();