fc5554ea2aa825f40e16283a56654156b2fa506f
[mesa.git] / src / intel / vulkan / anv_device.c
1 /*
2 * Copyright © 2015 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 */
23
24 #include <assert.h>
25 #include <stdbool.h>
26 #include <string.h>
27 #include <sys/mman.h>
28 #include <sys/sysinfo.h>
29 #include <unistd.h>
30 #include <fcntl.h>
31 #include <xf86drm.h>
32 #include <drm_fourcc.h>
33
34 #include "anv_private.h"
35 #include "util/strtod.h"
36 #include "util/debug.h"
37 #include "util/build_id.h"
38 #include "util/mesa-sha1.h"
39 #include "vk_util.h"
40
41 #include "genxml/gen7_pack.h"
42
43 static void
44 compiler_debug_log(void *data, const char *fmt, ...)
45 { }
46
47 static void
48 compiler_perf_log(void *data, const char *fmt, ...)
49 {
50 va_list args;
51 va_start(args, fmt);
52
53 if (unlikely(INTEL_DEBUG & DEBUG_PERF))
54 intel_logd_v(fmt, args);
55
56 va_end(args);
57 }
58
59 static VkResult
60 anv_compute_heap_size(int fd, uint64_t *heap_size)
61 {
62 uint64_t gtt_size;
63 if (anv_gem_get_context_param(fd, 0, I915_CONTEXT_PARAM_GTT_SIZE,
64 &gtt_size) == -1) {
65 /* If, for whatever reason, we can't actually get the GTT size from the
66 * kernel (too old?) fall back to the aperture size.
67 */
68 anv_perf_warn(NULL, NULL,
69 "Failed to get I915_CONTEXT_PARAM_GTT_SIZE: %m");
70
71 if (anv_gem_get_aperture(fd, &gtt_size) == -1) {
72 return vk_errorf(NULL, NULL, VK_ERROR_INITIALIZATION_FAILED,
73 "failed to get aperture size: %m");
74 }
75 }
76
77 /* Query the total ram from the system */
78 struct sysinfo info;
79 sysinfo(&info);
80
81 uint64_t total_ram = (uint64_t)info.totalram * (uint64_t)info.mem_unit;
82
83 /* We don't want to burn too much ram with the GPU. If the user has 4GiB
84 * or less, we use at most half. If they have more than 4GiB, we use 3/4.
85 */
86 uint64_t available_ram;
87 if (total_ram <= 4ull * 1024ull * 1024ull * 1024ull)
88 available_ram = total_ram / 2;
89 else
90 available_ram = total_ram * 3 / 4;
91
92 /* We also want to leave some padding for things we allocate in the driver,
93 * so don't go over 3/4 of the GTT either.
94 */
95 uint64_t available_gtt = gtt_size * 3 / 4;
96
97 *heap_size = MIN2(available_ram, available_gtt);
98
99 return VK_SUCCESS;
100 }
101
102 static VkResult
103 anv_physical_device_init_heaps(struct anv_physical_device *device, int fd)
104 {
105 /* The kernel query only tells us whether or not the kernel supports the
106 * EXEC_OBJECT_SUPPORTS_48B_ADDRESS flag and not whether or not the
107 * hardware has actual 48bit address support.
108 */
109 device->supports_48bit_addresses =
110 (device->info.gen >= 8) && anv_gem_supports_48b_addresses(fd);
111
112 uint64_t heap_size;
113 VkResult result = anv_compute_heap_size(fd, &heap_size);
114 if (result != VK_SUCCESS)
115 return result;
116
117 if (heap_size > (2ull << 30) && !device->supports_48bit_addresses) {
118 /* When running with an overridden PCI ID, we may get a GTT size from
119 * the kernel that is greater than 2 GiB but the execbuf check for 48bit
120 * address support can still fail. Just clamp the address space size to
121 * 2 GiB if we don't have 48-bit support.
122 */
123 intel_logw("%s:%d: The kernel reported a GTT size larger than 2 GiB but "
124 "not support for 48-bit addresses",
125 __FILE__, __LINE__);
126 heap_size = 2ull << 30;
127 }
128
129 if (heap_size <= 3ull * (1ull << 30)) {
130 /* In this case, everything fits nicely into the 32-bit address space,
131 * so there's no need for supporting 48bit addresses on client-allocated
132 * memory objects.
133 */
134 device->memory.heap_count = 1;
135 device->memory.heaps[0] = (struct anv_memory_heap) {
136 .size = heap_size,
137 .flags = VK_MEMORY_HEAP_DEVICE_LOCAL_BIT,
138 .supports_48bit_addresses = false,
139 };
140 } else {
141 /* Not everything will fit nicely into a 32-bit address space. In this
142 * case we need a 64-bit heap. Advertise a small 32-bit heap and a
143 * larger 48-bit heap. If we're in this case, then we have a total heap
144 * size larger than 3GiB which most likely means they have 8 GiB of
145 * video memory and so carving off 1 GiB for the 32-bit heap should be
146 * reasonable.
147 */
148 const uint64_t heap_size_32bit = 1ull << 30;
149 const uint64_t heap_size_48bit = heap_size - heap_size_32bit;
150
151 assert(device->supports_48bit_addresses);
152
153 device->memory.heap_count = 2;
154 device->memory.heaps[0] = (struct anv_memory_heap) {
155 .size = heap_size_48bit,
156 .flags = VK_MEMORY_HEAP_DEVICE_LOCAL_BIT,
157 .supports_48bit_addresses = true,
158 };
159 device->memory.heaps[1] = (struct anv_memory_heap) {
160 .size = heap_size_32bit,
161 .flags = VK_MEMORY_HEAP_DEVICE_LOCAL_BIT,
162 .supports_48bit_addresses = false,
163 };
164 }
165
166 uint32_t type_count = 0;
167 for (uint32_t heap = 0; heap < device->memory.heap_count; heap++) {
168 uint32_t valid_buffer_usage = ~0;
169
170 /* There appears to be a hardware issue in the VF cache where it only
171 * considers the bottom 32 bits of memory addresses. If you happen to
172 * have two vertex buffers which get placed exactly 4 GiB apart and use
173 * them in back-to-back draw calls, you can get collisions. In order to
174 * solve this problem, we require vertex and index buffers be bound to
175 * memory allocated out of the 32-bit heap.
176 */
177 if (device->memory.heaps[heap].supports_48bit_addresses) {
178 valid_buffer_usage &= ~(VK_BUFFER_USAGE_INDEX_BUFFER_BIT |
179 VK_BUFFER_USAGE_VERTEX_BUFFER_BIT);
180 }
181
182 if (device->info.has_llc) {
183 /* Big core GPUs share LLC with the CPU and thus one memory type can be
184 * both cached and coherent at the same time.
185 */
186 device->memory.types[type_count++] = (struct anv_memory_type) {
187 .propertyFlags = VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT |
188 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT |
189 VK_MEMORY_PROPERTY_HOST_COHERENT_BIT |
190 VK_MEMORY_PROPERTY_HOST_CACHED_BIT,
191 .heapIndex = heap,
192 .valid_buffer_usage = valid_buffer_usage,
193 };
194 } else {
195 /* The spec requires that we expose a host-visible, coherent memory
196 * type, but Atom GPUs don't share LLC. Thus we offer two memory types
197 * to give the application a choice between cached, but not coherent and
198 * coherent but uncached (WC though).
199 */
200 device->memory.types[type_count++] = (struct anv_memory_type) {
201 .propertyFlags = VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT |
202 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT |
203 VK_MEMORY_PROPERTY_HOST_COHERENT_BIT,
204 .heapIndex = heap,
205 .valid_buffer_usage = valid_buffer_usage,
206 };
207 device->memory.types[type_count++] = (struct anv_memory_type) {
208 .propertyFlags = VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT |
209 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT |
210 VK_MEMORY_PROPERTY_HOST_CACHED_BIT,
211 .heapIndex = heap,
212 .valid_buffer_usage = valid_buffer_usage,
213 };
214 }
215 }
216 device->memory.type_count = type_count;
217
218 return VK_SUCCESS;
219 }
220
221 static VkResult
222 anv_physical_device_init_uuids(struct anv_physical_device *device)
223 {
224 const struct build_id_note *note =
225 build_id_find_nhdr_for_addr(anv_physical_device_init_uuids);
226 if (!note) {
227 return vk_errorf(device->instance, device,
228 VK_ERROR_INITIALIZATION_FAILED,
229 "Failed to find build-id");
230 }
231
232 unsigned build_id_len = build_id_length(note);
233 if (build_id_len < 20) {
234 return vk_errorf(device->instance, device,
235 VK_ERROR_INITIALIZATION_FAILED,
236 "build-id too short. It needs to be a SHA");
237 }
238
239 struct mesa_sha1 sha1_ctx;
240 uint8_t sha1[20];
241 STATIC_ASSERT(VK_UUID_SIZE <= sizeof(sha1));
242
243 /* The pipeline cache UUID is used for determining when a pipeline cache is
244 * invalid. It needs both a driver build and the PCI ID of the device.
245 */
246 _mesa_sha1_init(&sha1_ctx);
247 _mesa_sha1_update(&sha1_ctx, build_id_data(note), build_id_len);
248 _mesa_sha1_update(&sha1_ctx, &device->chipset_id,
249 sizeof(device->chipset_id));
250 _mesa_sha1_final(&sha1_ctx, sha1);
251 memcpy(device->pipeline_cache_uuid, sha1, VK_UUID_SIZE);
252
253 /* The driver UUID is used for determining sharability of images and memory
254 * between two Vulkan instances in separate processes. People who want to
255 * share memory need to also check the device UUID (below) so all this
256 * needs to be is the build-id.
257 */
258 memcpy(device->driver_uuid, build_id_data(note), VK_UUID_SIZE);
259
260 /* The device UUID uniquely identifies the given device within the machine.
261 * Since we never have more than one device, this doesn't need to be a real
262 * UUID. However, on the off-chance that someone tries to use this to
263 * cache pre-tiled images or something of the like, we use the PCI ID and
264 * some bits of ISL info to ensure that this is safe.
265 */
266 _mesa_sha1_init(&sha1_ctx);
267 _mesa_sha1_update(&sha1_ctx, &device->chipset_id,
268 sizeof(device->chipset_id));
269 _mesa_sha1_update(&sha1_ctx, &device->isl_dev.has_bit6_swizzling,
270 sizeof(device->isl_dev.has_bit6_swizzling));
271 _mesa_sha1_final(&sha1_ctx, sha1);
272 memcpy(device->device_uuid, sha1, VK_UUID_SIZE);
273
274 return VK_SUCCESS;
275 }
276
277 static VkResult
278 anv_physical_device_init(struct anv_physical_device *device,
279 struct anv_instance *instance,
280 const char *path)
281 {
282 VkResult result;
283 int fd;
284
285 brw_process_intel_debug_variable();
286
287 fd = open(path, O_RDWR | O_CLOEXEC);
288 if (fd < 0)
289 return vk_error(VK_ERROR_INCOMPATIBLE_DRIVER);
290
291 device->_loader_data.loaderMagic = ICD_LOADER_MAGIC;
292 device->instance = instance;
293
294 assert(strlen(path) < ARRAY_SIZE(device->path));
295 strncpy(device->path, path, ARRAY_SIZE(device->path));
296
297 device->chipset_id = anv_gem_get_param(fd, I915_PARAM_CHIPSET_ID);
298 if (!device->chipset_id) {
299 result = vk_error(VK_ERROR_INCOMPATIBLE_DRIVER);
300 goto fail;
301 }
302
303 device->name = gen_get_device_name(device->chipset_id);
304 if (!gen_get_device_info(device->chipset_id, &device->info)) {
305 result = vk_error(VK_ERROR_INCOMPATIBLE_DRIVER);
306 goto fail;
307 }
308
309 if (device->info.is_haswell) {
310 intel_logw("Haswell Vulkan support is incomplete");
311 } else if (device->info.gen == 7 && !device->info.is_baytrail) {
312 intel_logw("Ivy Bridge Vulkan support is incomplete");
313 } else if (device->info.gen == 7 && device->info.is_baytrail) {
314 intel_logw("Bay Trail Vulkan support is incomplete");
315 } else if (device->info.gen >= 8 && device->info.gen <= 9) {
316 /* Broadwell, Cherryview, Skylake, Broxton, Kabylake, Coffelake is as
317 * fully supported as anything */
318 } else if (device->info.gen == 10) {
319 intel_logw("Cannonlake Vulkan support is alpha");
320 } else {
321 result = vk_errorf(device->instance, device,
322 VK_ERROR_INCOMPATIBLE_DRIVER,
323 "Vulkan not yet supported on %s", device->name);
324 goto fail;
325 }
326
327 device->cmd_parser_version = -1;
328 if (device->info.gen == 7) {
329 device->cmd_parser_version =
330 anv_gem_get_param(fd, I915_PARAM_CMD_PARSER_VERSION);
331 if (device->cmd_parser_version == -1) {
332 result = vk_errorf(device->instance, device,
333 VK_ERROR_INITIALIZATION_FAILED,
334 "failed to get command parser version");
335 goto fail;
336 }
337 }
338
339 if (!anv_gem_get_param(fd, I915_PARAM_HAS_WAIT_TIMEOUT)) {
340 result = vk_errorf(device->instance, device,
341 VK_ERROR_INITIALIZATION_FAILED,
342 "kernel missing gem wait");
343 goto fail;
344 }
345
346 if (!anv_gem_get_param(fd, I915_PARAM_HAS_EXECBUF2)) {
347 result = vk_errorf(device->instance, device,
348 VK_ERROR_INITIALIZATION_FAILED,
349 "kernel missing execbuf2");
350 goto fail;
351 }
352
353 if (!device->info.has_llc &&
354 anv_gem_get_param(fd, I915_PARAM_MMAP_VERSION) < 1) {
355 result = vk_errorf(device->instance, device,
356 VK_ERROR_INITIALIZATION_FAILED,
357 "kernel missing wc mmap");
358 goto fail;
359 }
360
361 result = anv_physical_device_init_heaps(device, fd);
362 if (result != VK_SUCCESS)
363 goto fail;
364
365 device->has_exec_async = anv_gem_get_param(fd, I915_PARAM_HAS_EXEC_ASYNC);
366 device->has_exec_capture = anv_gem_get_param(fd, I915_PARAM_HAS_EXEC_CAPTURE);
367 device->has_exec_fence = anv_gem_get_param(fd, I915_PARAM_HAS_EXEC_FENCE);
368 device->has_syncobj = anv_gem_get_param(fd, I915_PARAM_HAS_EXEC_FENCE_ARRAY);
369 device->has_syncobj_wait = device->has_syncobj &&
370 anv_gem_supports_syncobj_wait(fd);
371
372 bool swizzled = anv_gem_get_bit6_swizzle(fd, I915_TILING_X);
373
374 /* GENs prior to 8 do not support EU/Subslice info */
375 if (device->info.gen >= 8) {
376 device->subslice_total = anv_gem_get_param(fd, I915_PARAM_SUBSLICE_TOTAL);
377 device->eu_total = anv_gem_get_param(fd, I915_PARAM_EU_TOTAL);
378
379 /* Without this information, we cannot get the right Braswell
380 * brandstrings, and we have to use conservative numbers for GPGPU on
381 * many platforms, but otherwise, things will just work.
382 */
383 if (device->subslice_total < 1 || device->eu_total < 1) {
384 intel_logw("Kernel 4.1 required to properly query GPU properties");
385 }
386 } else if (device->info.gen == 7) {
387 device->subslice_total = 1 << (device->info.gt - 1);
388 }
389
390 if (device->info.is_cherryview &&
391 device->subslice_total > 0 && device->eu_total > 0) {
392 /* Logical CS threads = EUs per subslice * num threads per EU */
393 uint32_t max_cs_threads =
394 device->eu_total / device->subslice_total * device->info.num_thread_per_eu;
395
396 /* Fuse configurations may give more threads than expected, never less. */
397 if (max_cs_threads > device->info.max_cs_threads)
398 device->info.max_cs_threads = max_cs_threads;
399 }
400
401 device->compiler = brw_compiler_create(NULL, &device->info);
402 if (device->compiler == NULL) {
403 result = vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
404 goto fail;
405 }
406 device->compiler->shader_debug_log = compiler_debug_log;
407 device->compiler->shader_perf_log = compiler_perf_log;
408 device->compiler->supports_pull_constants = false;
409
410 isl_device_init(&device->isl_dev, &device->info, swizzled);
411
412 result = anv_physical_device_init_uuids(device);
413 if (result != VK_SUCCESS)
414 goto fail;
415
416 result = anv_init_wsi(device);
417 if (result != VK_SUCCESS) {
418 ralloc_free(device->compiler);
419 goto fail;
420 }
421
422 device->local_fd = fd;
423 return VK_SUCCESS;
424
425 fail:
426 close(fd);
427 return result;
428 }
429
430 static void
431 anv_physical_device_finish(struct anv_physical_device *device)
432 {
433 anv_finish_wsi(device);
434 ralloc_free(device->compiler);
435 close(device->local_fd);
436 }
437
438 static void *
439 default_alloc_func(void *pUserData, size_t size, size_t align,
440 VkSystemAllocationScope allocationScope)
441 {
442 return malloc(size);
443 }
444
445 static void *
446 default_realloc_func(void *pUserData, void *pOriginal, size_t size,
447 size_t align, VkSystemAllocationScope allocationScope)
448 {
449 return realloc(pOriginal, size);
450 }
451
452 static void
453 default_free_func(void *pUserData, void *pMemory)
454 {
455 free(pMemory);
456 }
457
458 static const VkAllocationCallbacks default_alloc = {
459 .pUserData = NULL,
460 .pfnAllocation = default_alloc_func,
461 .pfnReallocation = default_realloc_func,
462 .pfnFree = default_free_func,
463 };
464
465 VkResult anv_CreateInstance(
466 const VkInstanceCreateInfo* pCreateInfo,
467 const VkAllocationCallbacks* pAllocator,
468 VkInstance* pInstance)
469 {
470 struct anv_instance *instance;
471
472 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO);
473
474 /* Check if user passed a debug report callback to be used during
475 * Create/Destroy of instance.
476 */
477 const VkDebugReportCallbackCreateInfoEXT *ctor_cb =
478 vk_find_struct_const(pCreateInfo->pNext,
479 DEBUG_REPORT_CALLBACK_CREATE_INFO_EXT);
480
481 uint32_t client_version;
482 if (pCreateInfo->pApplicationInfo &&
483 pCreateInfo->pApplicationInfo->apiVersion != 0) {
484 client_version = pCreateInfo->pApplicationInfo->apiVersion;
485 } else {
486 client_version = VK_MAKE_VERSION(1, 0, 0);
487 }
488
489 if (VK_MAKE_VERSION(1, 0, 0) > client_version ||
490 client_version > VK_MAKE_VERSION(1, 0, 0xfff)) {
491
492 if (ctor_cb && ctor_cb->flags & VK_DEBUG_REPORT_ERROR_BIT_EXT)
493 ctor_cb->pfnCallback(VK_DEBUG_REPORT_ERROR_BIT_EXT,
494 VK_DEBUG_REPORT_OBJECT_TYPE_INSTANCE_EXT,
495 VK_NULL_HANDLE, /* No handle available yet. */
496 __LINE__,
497 0,
498 "anv",
499 "incompatible driver version",
500 ctor_cb->pUserData);
501
502 return vk_errorf(NULL, NULL, VK_ERROR_INCOMPATIBLE_DRIVER,
503 "Client requested version %d.%d.%d",
504 VK_VERSION_MAJOR(client_version),
505 VK_VERSION_MINOR(client_version),
506 VK_VERSION_PATCH(client_version));
507 }
508
509 for (uint32_t i = 0; i < pCreateInfo->enabledExtensionCount; i++) {
510 const char *ext_name = pCreateInfo->ppEnabledExtensionNames[i];
511 if (!anv_instance_extension_supported(ext_name))
512 return vk_error(VK_ERROR_EXTENSION_NOT_PRESENT);
513 }
514
515 instance = vk_alloc2(&default_alloc, pAllocator, sizeof(*instance), 8,
516 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
517 if (!instance)
518 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
519
520 instance->_loader_data.loaderMagic = ICD_LOADER_MAGIC;
521
522 if (pAllocator)
523 instance->alloc = *pAllocator;
524 else
525 instance->alloc = default_alloc;
526
527 instance->apiVersion = client_version;
528 instance->physicalDeviceCount = -1;
529
530 if (pthread_mutex_init(&instance->callbacks_mutex, NULL) != 0) {
531 vk_free2(&default_alloc, pAllocator, instance);
532 return vk_error(VK_ERROR_INITIALIZATION_FAILED);
533 }
534
535 list_inithead(&instance->callbacks);
536
537 /* Store report debug callback to be used during DestroyInstance. */
538 if (ctor_cb) {
539 instance->destroy_debug_cb.flags = ctor_cb->flags;
540 instance->destroy_debug_cb.callback = ctor_cb->pfnCallback;
541 instance->destroy_debug_cb.data = ctor_cb->pUserData;
542 }
543
544 _mesa_locale_init();
545
546 VG(VALGRIND_CREATE_MEMPOOL(instance, 0, false));
547
548 *pInstance = anv_instance_to_handle(instance);
549
550 return VK_SUCCESS;
551 }
552
553 void anv_DestroyInstance(
554 VkInstance _instance,
555 const VkAllocationCallbacks* pAllocator)
556 {
557 ANV_FROM_HANDLE(anv_instance, instance, _instance);
558
559 if (!instance)
560 return;
561
562 if (instance->physicalDeviceCount > 0) {
563 /* We support at most one physical device. */
564 assert(instance->physicalDeviceCount == 1);
565 anv_physical_device_finish(&instance->physicalDevice);
566 }
567
568 VG(VALGRIND_DESTROY_MEMPOOL(instance));
569
570 pthread_mutex_destroy(&instance->callbacks_mutex);
571
572 _mesa_locale_fini();
573
574 vk_free(&instance->alloc, instance);
575 }
576
577 static VkResult
578 anv_enumerate_devices(struct anv_instance *instance)
579 {
580 /* TODO: Check for more devices ? */
581 drmDevicePtr devices[8];
582 VkResult result = VK_ERROR_INCOMPATIBLE_DRIVER;
583 int max_devices;
584
585 instance->physicalDeviceCount = 0;
586
587 max_devices = drmGetDevices2(0, devices, ARRAY_SIZE(devices));
588 if (max_devices < 1)
589 return VK_ERROR_INCOMPATIBLE_DRIVER;
590
591 for (unsigned i = 0; i < (unsigned)max_devices; i++) {
592 if (devices[i]->available_nodes & 1 << DRM_NODE_RENDER &&
593 devices[i]->bustype == DRM_BUS_PCI &&
594 devices[i]->deviceinfo.pci->vendor_id == 0x8086) {
595
596 result = anv_physical_device_init(&instance->physicalDevice,
597 instance,
598 devices[i]->nodes[DRM_NODE_RENDER]);
599 if (result != VK_ERROR_INCOMPATIBLE_DRIVER)
600 break;
601 }
602 }
603 drmFreeDevices(devices, max_devices);
604
605 if (result == VK_SUCCESS)
606 instance->physicalDeviceCount = 1;
607
608 return result;
609 }
610
611
612 VkResult anv_EnumeratePhysicalDevices(
613 VkInstance _instance,
614 uint32_t* pPhysicalDeviceCount,
615 VkPhysicalDevice* pPhysicalDevices)
616 {
617 ANV_FROM_HANDLE(anv_instance, instance, _instance);
618 VK_OUTARRAY_MAKE(out, pPhysicalDevices, pPhysicalDeviceCount);
619 VkResult result;
620
621 if (instance->physicalDeviceCount < 0) {
622 result = anv_enumerate_devices(instance);
623 if (result != VK_SUCCESS &&
624 result != VK_ERROR_INCOMPATIBLE_DRIVER)
625 return result;
626 }
627
628 if (instance->physicalDeviceCount > 0) {
629 assert(instance->physicalDeviceCount == 1);
630 vk_outarray_append(&out, i) {
631 *i = anv_physical_device_to_handle(&instance->physicalDevice);
632 }
633 }
634
635 return vk_outarray_status(&out);
636 }
637
638 void anv_GetPhysicalDeviceFeatures(
639 VkPhysicalDevice physicalDevice,
640 VkPhysicalDeviceFeatures* pFeatures)
641 {
642 ANV_FROM_HANDLE(anv_physical_device, pdevice, physicalDevice);
643
644 *pFeatures = (VkPhysicalDeviceFeatures) {
645 .robustBufferAccess = true,
646 .fullDrawIndexUint32 = true,
647 .imageCubeArray = true,
648 .independentBlend = true,
649 .geometryShader = true,
650 .tessellationShader = true,
651 .sampleRateShading = true,
652 .dualSrcBlend = true,
653 .logicOp = true,
654 .multiDrawIndirect = true,
655 .drawIndirectFirstInstance = true,
656 .depthClamp = true,
657 .depthBiasClamp = true,
658 .fillModeNonSolid = true,
659 .depthBounds = false,
660 .wideLines = true,
661 .largePoints = true,
662 .alphaToOne = true,
663 .multiViewport = true,
664 .samplerAnisotropy = true,
665 .textureCompressionETC2 = pdevice->info.gen >= 8 ||
666 pdevice->info.is_baytrail,
667 .textureCompressionASTC_LDR = pdevice->info.gen >= 9, /* FINISHME CHV */
668 .textureCompressionBC = true,
669 .occlusionQueryPrecise = true,
670 .pipelineStatisticsQuery = true,
671 .fragmentStoresAndAtomics = true,
672 .shaderTessellationAndGeometryPointSize = true,
673 .shaderImageGatherExtended = true,
674 .shaderStorageImageExtendedFormats = true,
675 .shaderStorageImageMultisample = false,
676 .shaderStorageImageReadWithoutFormat = false,
677 .shaderStorageImageWriteWithoutFormat = true,
678 .shaderUniformBufferArrayDynamicIndexing = true,
679 .shaderSampledImageArrayDynamicIndexing = true,
680 .shaderStorageBufferArrayDynamicIndexing = true,
681 .shaderStorageImageArrayDynamicIndexing = true,
682 .shaderClipDistance = true,
683 .shaderCullDistance = true,
684 .shaderFloat64 = pdevice->info.gen >= 8,
685 .shaderInt64 = pdevice->info.gen >= 8,
686 .shaderInt16 = false,
687 .shaderResourceMinLod = false,
688 .variableMultisampleRate = false,
689 .inheritedQueries = true,
690 };
691
692 /* We can't do image stores in vec4 shaders */
693 pFeatures->vertexPipelineStoresAndAtomics =
694 pdevice->compiler->scalar_stage[MESA_SHADER_VERTEX] &&
695 pdevice->compiler->scalar_stage[MESA_SHADER_GEOMETRY];
696 }
697
698 void anv_GetPhysicalDeviceFeatures2KHR(
699 VkPhysicalDevice physicalDevice,
700 VkPhysicalDeviceFeatures2KHR* pFeatures)
701 {
702 anv_GetPhysicalDeviceFeatures(physicalDevice, &pFeatures->features);
703
704 vk_foreach_struct(ext, pFeatures->pNext) {
705 switch (ext->sType) {
706 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_MULTIVIEW_FEATURES_KHX: {
707 VkPhysicalDeviceMultiviewFeaturesKHX *features =
708 (VkPhysicalDeviceMultiviewFeaturesKHX *)ext;
709 features->multiview = true;
710 features->multiviewGeometryShader = true;
711 features->multiviewTessellationShader = true;
712 break;
713 }
714
715 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VARIABLE_POINTER_FEATURES_KHR: {
716 VkPhysicalDeviceVariablePointerFeaturesKHR *features = (void *)ext;
717 features->variablePointersStorageBuffer = true;
718 features->variablePointers = false;
719 break;
720 }
721
722 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SAMPLER_YCBCR_CONVERSION_FEATURES_KHR: {
723 VkPhysicalDeviceSamplerYcbcrConversionFeaturesKHR *features =
724 (VkPhysicalDeviceSamplerYcbcrConversionFeaturesKHR *) ext;
725 features->samplerYcbcrConversion = true;
726 break;
727 }
728
729 default:
730 anv_debug_ignored_stype(ext->sType);
731 break;
732 }
733 }
734 }
735
736 void anv_GetPhysicalDeviceProperties(
737 VkPhysicalDevice physicalDevice,
738 VkPhysicalDeviceProperties* pProperties)
739 {
740 ANV_FROM_HANDLE(anv_physical_device, pdevice, physicalDevice);
741 const struct gen_device_info *devinfo = &pdevice->info;
742
743 /* See assertions made when programming the buffer surface state. */
744 const uint32_t max_raw_buffer_sz = devinfo->gen >= 7 ?
745 (1ul << 30) : (1ul << 27);
746
747 const uint32_t max_samplers = (devinfo->gen >= 8 || devinfo->is_haswell) ?
748 128 : 16;
749
750 VkSampleCountFlags sample_counts =
751 isl_device_get_sample_counts(&pdevice->isl_dev);
752
753 VkPhysicalDeviceLimits limits = {
754 .maxImageDimension1D = (1 << 14),
755 .maxImageDimension2D = (1 << 14),
756 .maxImageDimension3D = (1 << 11),
757 .maxImageDimensionCube = (1 << 14),
758 .maxImageArrayLayers = (1 << 11),
759 .maxTexelBufferElements = 128 * 1024 * 1024,
760 .maxUniformBufferRange = (1ul << 27),
761 .maxStorageBufferRange = max_raw_buffer_sz,
762 .maxPushConstantsSize = MAX_PUSH_CONSTANTS_SIZE,
763 .maxMemoryAllocationCount = UINT32_MAX,
764 .maxSamplerAllocationCount = 64 * 1024,
765 .bufferImageGranularity = 64, /* A cache line */
766 .sparseAddressSpaceSize = 0,
767 .maxBoundDescriptorSets = MAX_SETS,
768 .maxPerStageDescriptorSamplers = max_samplers,
769 .maxPerStageDescriptorUniformBuffers = 64,
770 .maxPerStageDescriptorStorageBuffers = 64,
771 .maxPerStageDescriptorSampledImages = max_samplers,
772 .maxPerStageDescriptorStorageImages = 64,
773 .maxPerStageDescriptorInputAttachments = 64,
774 .maxPerStageResources = 250,
775 .maxDescriptorSetSamplers = 256,
776 .maxDescriptorSetUniformBuffers = 256,
777 .maxDescriptorSetUniformBuffersDynamic = MAX_DYNAMIC_BUFFERS / 2,
778 .maxDescriptorSetStorageBuffers = 256,
779 .maxDescriptorSetStorageBuffersDynamic = MAX_DYNAMIC_BUFFERS / 2,
780 .maxDescriptorSetSampledImages = 256,
781 .maxDescriptorSetStorageImages = 256,
782 .maxDescriptorSetInputAttachments = 256,
783 .maxVertexInputAttributes = MAX_VBS,
784 .maxVertexInputBindings = MAX_VBS,
785 .maxVertexInputAttributeOffset = 2047,
786 .maxVertexInputBindingStride = 2048,
787 .maxVertexOutputComponents = 128,
788 .maxTessellationGenerationLevel = 64,
789 .maxTessellationPatchSize = 32,
790 .maxTessellationControlPerVertexInputComponents = 128,
791 .maxTessellationControlPerVertexOutputComponents = 128,
792 .maxTessellationControlPerPatchOutputComponents = 128,
793 .maxTessellationControlTotalOutputComponents = 2048,
794 .maxTessellationEvaluationInputComponents = 128,
795 .maxTessellationEvaluationOutputComponents = 128,
796 .maxGeometryShaderInvocations = 32,
797 .maxGeometryInputComponents = 64,
798 .maxGeometryOutputComponents = 128,
799 .maxGeometryOutputVertices = 256,
800 .maxGeometryTotalOutputComponents = 1024,
801 .maxFragmentInputComponents = 128,
802 .maxFragmentOutputAttachments = 8,
803 .maxFragmentDualSrcAttachments = 1,
804 .maxFragmentCombinedOutputResources = 8,
805 .maxComputeSharedMemorySize = 32768,
806 .maxComputeWorkGroupCount = { 65535, 65535, 65535 },
807 .maxComputeWorkGroupInvocations = 16 * devinfo->max_cs_threads,
808 .maxComputeWorkGroupSize = {
809 16 * devinfo->max_cs_threads,
810 16 * devinfo->max_cs_threads,
811 16 * devinfo->max_cs_threads,
812 },
813 .subPixelPrecisionBits = 4 /* FIXME */,
814 .subTexelPrecisionBits = 4 /* FIXME */,
815 .mipmapPrecisionBits = 4 /* FIXME */,
816 .maxDrawIndexedIndexValue = UINT32_MAX,
817 .maxDrawIndirectCount = UINT32_MAX,
818 .maxSamplerLodBias = 16,
819 .maxSamplerAnisotropy = 16,
820 .maxViewports = MAX_VIEWPORTS,
821 .maxViewportDimensions = { (1 << 14), (1 << 14) },
822 .viewportBoundsRange = { INT16_MIN, INT16_MAX },
823 .viewportSubPixelBits = 13, /* We take a float? */
824 .minMemoryMapAlignment = 4096, /* A page */
825 .minTexelBufferOffsetAlignment = 1,
826 .minUniformBufferOffsetAlignment = 16,
827 .minStorageBufferOffsetAlignment = 4,
828 .minTexelOffset = -8,
829 .maxTexelOffset = 7,
830 .minTexelGatherOffset = -32,
831 .maxTexelGatherOffset = 31,
832 .minInterpolationOffset = -0.5,
833 .maxInterpolationOffset = 0.4375,
834 .subPixelInterpolationOffsetBits = 4,
835 .maxFramebufferWidth = (1 << 14),
836 .maxFramebufferHeight = (1 << 14),
837 .maxFramebufferLayers = (1 << 11),
838 .framebufferColorSampleCounts = sample_counts,
839 .framebufferDepthSampleCounts = sample_counts,
840 .framebufferStencilSampleCounts = sample_counts,
841 .framebufferNoAttachmentsSampleCounts = sample_counts,
842 .maxColorAttachments = MAX_RTS,
843 .sampledImageColorSampleCounts = sample_counts,
844 .sampledImageIntegerSampleCounts = VK_SAMPLE_COUNT_1_BIT,
845 .sampledImageDepthSampleCounts = sample_counts,
846 .sampledImageStencilSampleCounts = sample_counts,
847 .storageImageSampleCounts = VK_SAMPLE_COUNT_1_BIT,
848 .maxSampleMaskWords = 1,
849 .timestampComputeAndGraphics = false,
850 .timestampPeriod = 1000000000.0 / devinfo->timestamp_frequency,
851 .maxClipDistances = 8,
852 .maxCullDistances = 8,
853 .maxCombinedClipAndCullDistances = 8,
854 .discreteQueuePriorities = 1,
855 .pointSizeRange = { 0.125, 255.875 },
856 .lineWidthRange = { 0.0, 7.9921875 },
857 .pointSizeGranularity = (1.0 / 8.0),
858 .lineWidthGranularity = (1.0 / 128.0),
859 .strictLines = false, /* FINISHME */
860 .standardSampleLocations = true,
861 .optimalBufferCopyOffsetAlignment = 128,
862 .optimalBufferCopyRowPitchAlignment = 128,
863 .nonCoherentAtomSize = 64,
864 };
865
866 *pProperties = (VkPhysicalDeviceProperties) {
867 .apiVersion = anv_physical_device_api_version(pdevice),
868 .driverVersion = vk_get_driver_version(),
869 .vendorID = 0x8086,
870 .deviceID = pdevice->chipset_id,
871 .deviceType = VK_PHYSICAL_DEVICE_TYPE_INTEGRATED_GPU,
872 .limits = limits,
873 .sparseProperties = {0}, /* Broadwell doesn't do sparse. */
874 };
875
876 snprintf(pProperties->deviceName, sizeof(pProperties->deviceName),
877 "%s", pdevice->name);
878 memcpy(pProperties->pipelineCacheUUID,
879 pdevice->pipeline_cache_uuid, VK_UUID_SIZE);
880 }
881
882 void anv_GetPhysicalDeviceProperties2KHR(
883 VkPhysicalDevice physicalDevice,
884 VkPhysicalDeviceProperties2KHR* pProperties)
885 {
886 ANV_FROM_HANDLE(anv_physical_device, pdevice, physicalDevice);
887
888 anv_GetPhysicalDeviceProperties(physicalDevice, &pProperties->properties);
889
890 vk_foreach_struct(ext, pProperties->pNext) {
891 switch (ext->sType) {
892 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PUSH_DESCRIPTOR_PROPERTIES_KHR: {
893 VkPhysicalDevicePushDescriptorPropertiesKHR *properties =
894 (VkPhysicalDevicePushDescriptorPropertiesKHR *) ext;
895
896 properties->maxPushDescriptors = MAX_PUSH_DESCRIPTORS;
897 break;
898 }
899
900 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_ID_PROPERTIES_KHR: {
901 VkPhysicalDeviceIDPropertiesKHR *id_props =
902 (VkPhysicalDeviceIDPropertiesKHR *)ext;
903 memcpy(id_props->deviceUUID, pdevice->device_uuid, VK_UUID_SIZE);
904 memcpy(id_props->driverUUID, pdevice->driver_uuid, VK_UUID_SIZE);
905 /* The LUID is for Windows. */
906 id_props->deviceLUIDValid = false;
907 break;
908 }
909
910 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_MULTIVIEW_PROPERTIES_KHX: {
911 VkPhysicalDeviceMultiviewPropertiesKHX *properties =
912 (VkPhysicalDeviceMultiviewPropertiesKHX *)ext;
913 properties->maxMultiviewViewCount = 16;
914 properties->maxMultiviewInstanceIndex = UINT32_MAX / 16;
915 break;
916 }
917
918 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_POINT_CLIPPING_PROPERTIES_KHR: {
919 VkPhysicalDevicePointClippingPropertiesKHR *properties =
920 (VkPhysicalDevicePointClippingPropertiesKHR *) ext;
921 properties->pointClippingBehavior = VK_POINT_CLIPPING_BEHAVIOR_ALL_CLIP_PLANES_KHR;
922 anv_finishme("Implement pop-free point clipping");
923 break;
924 }
925
926 default:
927 anv_debug_ignored_stype(ext->sType);
928 break;
929 }
930 }
931 }
932
933 /* We support exactly one queue family. */
934 static const VkQueueFamilyProperties
935 anv_queue_family_properties = {
936 .queueFlags = VK_QUEUE_GRAPHICS_BIT |
937 VK_QUEUE_COMPUTE_BIT |
938 VK_QUEUE_TRANSFER_BIT,
939 .queueCount = 1,
940 .timestampValidBits = 36, /* XXX: Real value here */
941 .minImageTransferGranularity = { 1, 1, 1 },
942 };
943
944 void anv_GetPhysicalDeviceQueueFamilyProperties(
945 VkPhysicalDevice physicalDevice,
946 uint32_t* pCount,
947 VkQueueFamilyProperties* pQueueFamilyProperties)
948 {
949 VK_OUTARRAY_MAKE(out, pQueueFamilyProperties, pCount);
950
951 vk_outarray_append(&out, p) {
952 *p = anv_queue_family_properties;
953 }
954 }
955
956 void anv_GetPhysicalDeviceQueueFamilyProperties2KHR(
957 VkPhysicalDevice physicalDevice,
958 uint32_t* pQueueFamilyPropertyCount,
959 VkQueueFamilyProperties2KHR* pQueueFamilyProperties)
960 {
961
962 VK_OUTARRAY_MAKE(out, pQueueFamilyProperties, pQueueFamilyPropertyCount);
963
964 vk_outarray_append(&out, p) {
965 p->queueFamilyProperties = anv_queue_family_properties;
966
967 vk_foreach_struct(s, p->pNext) {
968 anv_debug_ignored_stype(s->sType);
969 }
970 }
971 }
972
973 void anv_GetPhysicalDeviceMemoryProperties(
974 VkPhysicalDevice physicalDevice,
975 VkPhysicalDeviceMemoryProperties* pMemoryProperties)
976 {
977 ANV_FROM_HANDLE(anv_physical_device, physical_device, physicalDevice);
978
979 pMemoryProperties->memoryTypeCount = physical_device->memory.type_count;
980 for (uint32_t i = 0; i < physical_device->memory.type_count; i++) {
981 pMemoryProperties->memoryTypes[i] = (VkMemoryType) {
982 .propertyFlags = physical_device->memory.types[i].propertyFlags,
983 .heapIndex = physical_device->memory.types[i].heapIndex,
984 };
985 }
986
987 pMemoryProperties->memoryHeapCount = physical_device->memory.heap_count;
988 for (uint32_t i = 0; i < physical_device->memory.heap_count; i++) {
989 pMemoryProperties->memoryHeaps[i] = (VkMemoryHeap) {
990 .size = physical_device->memory.heaps[i].size,
991 .flags = physical_device->memory.heaps[i].flags,
992 };
993 }
994 }
995
996 void anv_GetPhysicalDeviceMemoryProperties2KHR(
997 VkPhysicalDevice physicalDevice,
998 VkPhysicalDeviceMemoryProperties2KHR* pMemoryProperties)
999 {
1000 anv_GetPhysicalDeviceMemoryProperties(physicalDevice,
1001 &pMemoryProperties->memoryProperties);
1002
1003 vk_foreach_struct(ext, pMemoryProperties->pNext) {
1004 switch (ext->sType) {
1005 default:
1006 anv_debug_ignored_stype(ext->sType);
1007 break;
1008 }
1009 }
1010 }
1011
1012 PFN_vkVoidFunction anv_GetInstanceProcAddr(
1013 VkInstance instance,
1014 const char* pName)
1015 {
1016 return anv_lookup_entrypoint(NULL, pName);
1017 }
1018
1019 /* With version 1+ of the loader interface the ICD should expose
1020 * vk_icdGetInstanceProcAddr to work around certain LD_PRELOAD issues seen in apps.
1021 */
1022 PUBLIC
1023 VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL vk_icdGetInstanceProcAddr(
1024 VkInstance instance,
1025 const char* pName);
1026
1027 PUBLIC
1028 VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL vk_icdGetInstanceProcAddr(
1029 VkInstance instance,
1030 const char* pName)
1031 {
1032 return anv_GetInstanceProcAddr(instance, pName);
1033 }
1034
1035 PFN_vkVoidFunction anv_GetDeviceProcAddr(
1036 VkDevice _device,
1037 const char* pName)
1038 {
1039 ANV_FROM_HANDLE(anv_device, device, _device);
1040 return anv_lookup_entrypoint(&device->info, pName);
1041 }
1042
1043 static void
1044 anv_queue_init(struct anv_device *device, struct anv_queue *queue)
1045 {
1046 queue->_loader_data.loaderMagic = ICD_LOADER_MAGIC;
1047 queue->device = device;
1048 queue->pool = &device->surface_state_pool;
1049 }
1050
1051 static void
1052 anv_queue_finish(struct anv_queue *queue)
1053 {
1054 }
1055
1056 static struct anv_state
1057 anv_state_pool_emit_data(struct anv_state_pool *pool, size_t size, size_t align, const void *p)
1058 {
1059 struct anv_state state;
1060
1061 state = anv_state_pool_alloc(pool, size, align);
1062 memcpy(state.map, p, size);
1063
1064 anv_state_flush(pool->block_pool.device, state);
1065
1066 return state;
1067 }
1068
1069 struct gen8_border_color {
1070 union {
1071 float float32[4];
1072 uint32_t uint32[4];
1073 };
1074 /* Pad out to 64 bytes */
1075 uint32_t _pad[12];
1076 };
1077
1078 static void
1079 anv_device_init_border_colors(struct anv_device *device)
1080 {
1081 static const struct gen8_border_color border_colors[] = {
1082 [VK_BORDER_COLOR_FLOAT_TRANSPARENT_BLACK] = { .float32 = { 0.0, 0.0, 0.0, 0.0 } },
1083 [VK_BORDER_COLOR_FLOAT_OPAQUE_BLACK] = { .float32 = { 0.0, 0.0, 0.0, 1.0 } },
1084 [VK_BORDER_COLOR_FLOAT_OPAQUE_WHITE] = { .float32 = { 1.0, 1.0, 1.0, 1.0 } },
1085 [VK_BORDER_COLOR_INT_TRANSPARENT_BLACK] = { .uint32 = { 0, 0, 0, 0 } },
1086 [VK_BORDER_COLOR_INT_OPAQUE_BLACK] = { .uint32 = { 0, 0, 0, 1 } },
1087 [VK_BORDER_COLOR_INT_OPAQUE_WHITE] = { .uint32 = { 1, 1, 1, 1 } },
1088 };
1089
1090 device->border_colors = anv_state_pool_emit_data(&device->dynamic_state_pool,
1091 sizeof(border_colors), 64,
1092 border_colors);
1093 }
1094
1095 static void
1096 anv_device_init_trivial_batch(struct anv_device *device)
1097 {
1098 anv_bo_init_new(&device->trivial_batch_bo, device, 4096);
1099
1100 if (device->instance->physicalDevice.has_exec_async)
1101 device->trivial_batch_bo.flags |= EXEC_OBJECT_ASYNC;
1102
1103 void *map = anv_gem_mmap(device, device->trivial_batch_bo.gem_handle,
1104 0, 4096, 0);
1105
1106 struct anv_batch batch = {
1107 .start = map,
1108 .next = map,
1109 .end = map + 4096,
1110 };
1111
1112 anv_batch_emit(&batch, GEN7_MI_BATCH_BUFFER_END, bbe);
1113 anv_batch_emit(&batch, GEN7_MI_NOOP, noop);
1114
1115 if (!device->info.has_llc)
1116 gen_clflush_range(map, batch.next - map);
1117
1118 anv_gem_munmap(map, device->trivial_batch_bo.size);
1119 }
1120
1121 VkResult anv_CreateDevice(
1122 VkPhysicalDevice physicalDevice,
1123 const VkDeviceCreateInfo* pCreateInfo,
1124 const VkAllocationCallbacks* pAllocator,
1125 VkDevice* pDevice)
1126 {
1127 ANV_FROM_HANDLE(anv_physical_device, physical_device, physicalDevice);
1128 VkResult result;
1129 struct anv_device *device;
1130
1131 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO);
1132
1133 for (uint32_t i = 0; i < pCreateInfo->enabledExtensionCount; i++) {
1134 const char *ext_name = pCreateInfo->ppEnabledExtensionNames[i];
1135 if (!anv_physical_device_extension_supported(physical_device, ext_name))
1136 return vk_error(VK_ERROR_EXTENSION_NOT_PRESENT);
1137 }
1138
1139 /* Check enabled features */
1140 if (pCreateInfo->pEnabledFeatures) {
1141 VkPhysicalDeviceFeatures supported_features;
1142 anv_GetPhysicalDeviceFeatures(physicalDevice, &supported_features);
1143 VkBool32 *supported_feature = (VkBool32 *)&supported_features;
1144 VkBool32 *enabled_feature = (VkBool32 *)pCreateInfo->pEnabledFeatures;
1145 unsigned num_features = sizeof(VkPhysicalDeviceFeatures) / sizeof(VkBool32);
1146 for (uint32_t i = 0; i < num_features; i++) {
1147 if (enabled_feature[i] && !supported_feature[i])
1148 return vk_error(VK_ERROR_FEATURE_NOT_PRESENT);
1149 }
1150 }
1151
1152 device = vk_alloc2(&physical_device->instance->alloc, pAllocator,
1153 sizeof(*device), 8,
1154 VK_SYSTEM_ALLOCATION_SCOPE_DEVICE);
1155 if (!device)
1156 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
1157
1158 device->_loader_data.loaderMagic = ICD_LOADER_MAGIC;
1159 device->instance = physical_device->instance;
1160 device->chipset_id = physical_device->chipset_id;
1161 device->lost = false;
1162
1163 if (pAllocator)
1164 device->alloc = *pAllocator;
1165 else
1166 device->alloc = physical_device->instance->alloc;
1167
1168 /* XXX(chadv): Can we dup() physicalDevice->fd here? */
1169 device->fd = open(physical_device->path, O_RDWR | O_CLOEXEC);
1170 if (device->fd == -1) {
1171 result = vk_error(VK_ERROR_INITIALIZATION_FAILED);
1172 goto fail_device;
1173 }
1174
1175 device->context_id = anv_gem_create_context(device);
1176 if (device->context_id == -1) {
1177 result = vk_error(VK_ERROR_INITIALIZATION_FAILED);
1178 goto fail_fd;
1179 }
1180
1181 device->info = physical_device->info;
1182 device->isl_dev = physical_device->isl_dev;
1183
1184 /* On Broadwell and later, we can use batch chaining to more efficiently
1185 * implement growing command buffers. Prior to Haswell, the kernel
1186 * command parser gets in the way and we have to fall back to growing
1187 * the batch.
1188 */
1189 device->can_chain_batches = device->info.gen >= 8;
1190
1191 device->robust_buffer_access = pCreateInfo->pEnabledFeatures &&
1192 pCreateInfo->pEnabledFeatures->robustBufferAccess;
1193
1194 if (pthread_mutex_init(&device->mutex, NULL) != 0) {
1195 result = vk_error(VK_ERROR_INITIALIZATION_FAILED);
1196 goto fail_context_id;
1197 }
1198
1199 pthread_condattr_t condattr;
1200 if (pthread_condattr_init(&condattr) != 0) {
1201 result = vk_error(VK_ERROR_INITIALIZATION_FAILED);
1202 goto fail_mutex;
1203 }
1204 if (pthread_condattr_setclock(&condattr, CLOCK_MONOTONIC) != 0) {
1205 pthread_condattr_destroy(&condattr);
1206 result = vk_error(VK_ERROR_INITIALIZATION_FAILED);
1207 goto fail_mutex;
1208 }
1209 if (pthread_cond_init(&device->queue_submit, NULL) != 0) {
1210 pthread_condattr_destroy(&condattr);
1211 result = vk_error(VK_ERROR_INITIALIZATION_FAILED);
1212 goto fail_mutex;
1213 }
1214 pthread_condattr_destroy(&condattr);
1215
1216 uint64_t bo_flags =
1217 (physical_device->supports_48bit_addresses ? EXEC_OBJECT_SUPPORTS_48B_ADDRESS : 0) |
1218 (physical_device->has_exec_async ? EXEC_OBJECT_ASYNC : 0) |
1219 (physical_device->has_exec_capture ? EXEC_OBJECT_CAPTURE : 0);
1220
1221 anv_bo_pool_init(&device->batch_bo_pool, device, bo_flags);
1222
1223 result = anv_bo_cache_init(&device->bo_cache);
1224 if (result != VK_SUCCESS)
1225 goto fail_batch_bo_pool;
1226
1227 /* For the state pools we explicitly disable 48bit. */
1228 bo_flags = physical_device->has_exec_async ? EXEC_OBJECT_ASYNC : 0;
1229
1230 result = anv_state_pool_init(&device->dynamic_state_pool, device, 16384,
1231 bo_flags);
1232 if (result != VK_SUCCESS)
1233 goto fail_bo_cache;
1234
1235 result = anv_state_pool_init(&device->instruction_state_pool, device, 16384,
1236 bo_flags |
1237 (physical_device->has_exec_capture ? EXEC_OBJECT_CAPTURE : 0));
1238 if (result != VK_SUCCESS)
1239 goto fail_dynamic_state_pool;
1240
1241 result = anv_state_pool_init(&device->surface_state_pool, device, 4096,
1242 bo_flags);
1243 if (result != VK_SUCCESS)
1244 goto fail_instruction_state_pool;
1245
1246 result = anv_bo_init_new(&device->workaround_bo, device, 1024);
1247 if (result != VK_SUCCESS)
1248 goto fail_surface_state_pool;
1249
1250 anv_device_init_trivial_batch(device);
1251
1252 anv_scratch_pool_init(device, &device->scratch_pool);
1253
1254 anv_queue_init(device, &device->queue);
1255
1256 switch (device->info.gen) {
1257 case 7:
1258 if (!device->info.is_haswell)
1259 result = gen7_init_device_state(device);
1260 else
1261 result = gen75_init_device_state(device);
1262 break;
1263 case 8:
1264 result = gen8_init_device_state(device);
1265 break;
1266 case 9:
1267 result = gen9_init_device_state(device);
1268 break;
1269 case 10:
1270 result = gen10_init_device_state(device);
1271 break;
1272 default:
1273 /* Shouldn't get here as we don't create physical devices for any other
1274 * gens. */
1275 unreachable("unhandled gen");
1276 }
1277 if (result != VK_SUCCESS)
1278 goto fail_workaround_bo;
1279
1280 anv_device_init_blorp(device);
1281
1282 anv_device_init_border_colors(device);
1283
1284 *pDevice = anv_device_to_handle(device);
1285
1286 return VK_SUCCESS;
1287
1288 fail_workaround_bo:
1289 anv_queue_finish(&device->queue);
1290 anv_scratch_pool_finish(device, &device->scratch_pool);
1291 anv_gem_munmap(device->workaround_bo.map, device->workaround_bo.size);
1292 anv_gem_close(device, device->workaround_bo.gem_handle);
1293 fail_surface_state_pool:
1294 anv_state_pool_finish(&device->surface_state_pool);
1295 fail_instruction_state_pool:
1296 anv_state_pool_finish(&device->instruction_state_pool);
1297 fail_dynamic_state_pool:
1298 anv_state_pool_finish(&device->dynamic_state_pool);
1299 fail_bo_cache:
1300 anv_bo_cache_finish(&device->bo_cache);
1301 fail_batch_bo_pool:
1302 anv_bo_pool_finish(&device->batch_bo_pool);
1303 pthread_cond_destroy(&device->queue_submit);
1304 fail_mutex:
1305 pthread_mutex_destroy(&device->mutex);
1306 fail_context_id:
1307 anv_gem_destroy_context(device, device->context_id);
1308 fail_fd:
1309 close(device->fd);
1310 fail_device:
1311 vk_free(&device->alloc, device);
1312
1313 return result;
1314 }
1315
1316 void anv_DestroyDevice(
1317 VkDevice _device,
1318 const VkAllocationCallbacks* pAllocator)
1319 {
1320 ANV_FROM_HANDLE(anv_device, device, _device);
1321
1322 if (!device)
1323 return;
1324
1325 anv_device_finish_blorp(device);
1326
1327 anv_queue_finish(&device->queue);
1328
1329 #ifdef HAVE_VALGRIND
1330 /* We only need to free these to prevent valgrind errors. The backing
1331 * BO will go away in a couple of lines so we don't actually leak.
1332 */
1333 anv_state_pool_free(&device->dynamic_state_pool, device->border_colors);
1334 #endif
1335
1336 anv_scratch_pool_finish(device, &device->scratch_pool);
1337
1338 anv_gem_munmap(device->workaround_bo.map, device->workaround_bo.size);
1339 anv_gem_close(device, device->workaround_bo.gem_handle);
1340
1341 anv_gem_close(device, device->trivial_batch_bo.gem_handle);
1342
1343 anv_state_pool_finish(&device->surface_state_pool);
1344 anv_state_pool_finish(&device->instruction_state_pool);
1345 anv_state_pool_finish(&device->dynamic_state_pool);
1346
1347 anv_bo_cache_finish(&device->bo_cache);
1348
1349 anv_bo_pool_finish(&device->batch_bo_pool);
1350
1351 pthread_cond_destroy(&device->queue_submit);
1352 pthread_mutex_destroy(&device->mutex);
1353
1354 anv_gem_destroy_context(device, device->context_id);
1355
1356 close(device->fd);
1357
1358 vk_free(&device->alloc, device);
1359 }
1360
1361 VkResult anv_EnumerateInstanceLayerProperties(
1362 uint32_t* pPropertyCount,
1363 VkLayerProperties* pProperties)
1364 {
1365 if (pProperties == NULL) {
1366 *pPropertyCount = 0;
1367 return VK_SUCCESS;
1368 }
1369
1370 /* None supported at this time */
1371 return vk_error(VK_ERROR_LAYER_NOT_PRESENT);
1372 }
1373
1374 VkResult anv_EnumerateDeviceLayerProperties(
1375 VkPhysicalDevice physicalDevice,
1376 uint32_t* pPropertyCount,
1377 VkLayerProperties* pProperties)
1378 {
1379 if (pProperties == NULL) {
1380 *pPropertyCount = 0;
1381 return VK_SUCCESS;
1382 }
1383
1384 /* None supported at this time */
1385 return vk_error(VK_ERROR_LAYER_NOT_PRESENT);
1386 }
1387
1388 void anv_GetDeviceQueue(
1389 VkDevice _device,
1390 uint32_t queueNodeIndex,
1391 uint32_t queueIndex,
1392 VkQueue* pQueue)
1393 {
1394 ANV_FROM_HANDLE(anv_device, device, _device);
1395
1396 assert(queueIndex == 0);
1397
1398 *pQueue = anv_queue_to_handle(&device->queue);
1399 }
1400
1401 VkResult
1402 anv_device_query_status(struct anv_device *device)
1403 {
1404 /* This isn't likely as most of the callers of this function already check
1405 * for it. However, it doesn't hurt to check and it potentially lets us
1406 * avoid an ioctl.
1407 */
1408 if (unlikely(device->lost))
1409 return VK_ERROR_DEVICE_LOST;
1410
1411 uint32_t active, pending;
1412 int ret = anv_gem_gpu_get_reset_stats(device, &active, &pending);
1413 if (ret == -1) {
1414 /* We don't know the real error. */
1415 device->lost = true;
1416 return vk_errorf(device->instance, device, VK_ERROR_DEVICE_LOST,
1417 "get_reset_stats failed: %m");
1418 }
1419
1420 if (active) {
1421 device->lost = true;
1422 return vk_errorf(device->instance, device, VK_ERROR_DEVICE_LOST,
1423 "GPU hung on one of our command buffers");
1424 } else if (pending) {
1425 device->lost = true;
1426 return vk_errorf(device->instance, device, VK_ERROR_DEVICE_LOST,
1427 "GPU hung with commands in-flight");
1428 }
1429
1430 return VK_SUCCESS;
1431 }
1432
1433 VkResult
1434 anv_device_bo_busy(struct anv_device *device, struct anv_bo *bo)
1435 {
1436 /* Note: This only returns whether or not the BO is in use by an i915 GPU.
1437 * Other usages of the BO (such as on different hardware) will not be
1438 * flagged as "busy" by this ioctl. Use with care.
1439 */
1440 int ret = anv_gem_busy(device, bo->gem_handle);
1441 if (ret == 1) {
1442 return VK_NOT_READY;
1443 } else if (ret == -1) {
1444 /* We don't know the real error. */
1445 device->lost = true;
1446 return vk_errorf(device->instance, device, VK_ERROR_DEVICE_LOST,
1447 "gem wait failed: %m");
1448 }
1449
1450 /* Query for device status after the busy call. If the BO we're checking
1451 * got caught in a GPU hang we don't want to return VK_SUCCESS to the
1452 * client because it clearly doesn't have valid data. Yes, this most
1453 * likely means an ioctl, but we just did an ioctl to query the busy status
1454 * so it's no great loss.
1455 */
1456 return anv_device_query_status(device);
1457 }
1458
1459 VkResult
1460 anv_device_wait(struct anv_device *device, struct anv_bo *bo,
1461 int64_t timeout)
1462 {
1463 int ret = anv_gem_wait(device, bo->gem_handle, &timeout);
1464 if (ret == -1 && errno == ETIME) {
1465 return VK_TIMEOUT;
1466 } else if (ret == -1) {
1467 /* We don't know the real error. */
1468 device->lost = true;
1469 return vk_errorf(device->instance, device, VK_ERROR_DEVICE_LOST,
1470 "gem wait failed: %m");
1471 }
1472
1473 /* Query for device status after the wait. If the BO we're waiting on got
1474 * caught in a GPU hang we don't want to return VK_SUCCESS to the client
1475 * because it clearly doesn't have valid data. Yes, this most likely means
1476 * an ioctl, but we just did an ioctl to wait so it's no great loss.
1477 */
1478 return anv_device_query_status(device);
1479 }
1480
1481 VkResult anv_DeviceWaitIdle(
1482 VkDevice _device)
1483 {
1484 ANV_FROM_HANDLE(anv_device, device, _device);
1485 if (unlikely(device->lost))
1486 return VK_ERROR_DEVICE_LOST;
1487
1488 struct anv_batch batch;
1489
1490 uint32_t cmds[8];
1491 batch.start = batch.next = cmds;
1492 batch.end = (void *) cmds + sizeof(cmds);
1493
1494 anv_batch_emit(&batch, GEN7_MI_BATCH_BUFFER_END, bbe);
1495 anv_batch_emit(&batch, GEN7_MI_NOOP, noop);
1496
1497 return anv_device_submit_simple_batch(device, &batch);
1498 }
1499
1500 VkResult
1501 anv_bo_init_new(struct anv_bo *bo, struct anv_device *device, uint64_t size)
1502 {
1503 uint32_t gem_handle = anv_gem_create(device, size);
1504 if (!gem_handle)
1505 return vk_error(VK_ERROR_OUT_OF_DEVICE_MEMORY);
1506
1507 anv_bo_init(bo, gem_handle, size);
1508
1509 return VK_SUCCESS;
1510 }
1511
1512 VkResult anv_AllocateMemory(
1513 VkDevice _device,
1514 const VkMemoryAllocateInfo* pAllocateInfo,
1515 const VkAllocationCallbacks* pAllocator,
1516 VkDeviceMemory* pMem)
1517 {
1518 ANV_FROM_HANDLE(anv_device, device, _device);
1519 struct anv_physical_device *pdevice = &device->instance->physicalDevice;
1520 struct anv_device_memory *mem;
1521 VkResult result = VK_SUCCESS;
1522
1523 assert(pAllocateInfo->sType == VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO);
1524
1525 /* The Vulkan 1.0.33 spec says "allocationSize must be greater than 0". */
1526 assert(pAllocateInfo->allocationSize > 0);
1527
1528 /* The kernel relocation API has a limitation of a 32-bit delta value
1529 * applied to the address before it is written which, in spite of it being
1530 * unsigned, is treated as signed . Because of the way that this maps to
1531 * the Vulkan API, we cannot handle an offset into a buffer that does not
1532 * fit into a signed 32 bits. The only mechanism we have for dealing with
1533 * this at the moment is to limit all VkDeviceMemory objects to a maximum
1534 * of 2GB each. The Vulkan spec allows us to do this:
1535 *
1536 * "Some platforms may have a limit on the maximum size of a single
1537 * allocation. For example, certain systems may fail to create
1538 * allocations with a size greater than or equal to 4GB. Such a limit is
1539 * implementation-dependent, and if such a failure occurs then the error
1540 * VK_ERROR_OUT_OF_DEVICE_MEMORY should be returned."
1541 *
1542 * We don't use vk_error here because it's not an error so much as an
1543 * indication to the application that the allocation is too large.
1544 */
1545 if (pAllocateInfo->allocationSize > (1ull << 31))
1546 return VK_ERROR_OUT_OF_DEVICE_MEMORY;
1547
1548 /* FINISHME: Fail if allocation request exceeds heap size. */
1549
1550 mem = vk_alloc2(&device->alloc, pAllocator, sizeof(*mem), 8,
1551 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
1552 if (mem == NULL)
1553 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
1554
1555 assert(pAllocateInfo->memoryTypeIndex < pdevice->memory.type_count);
1556 mem->type = &pdevice->memory.types[pAllocateInfo->memoryTypeIndex];
1557 mem->map = NULL;
1558 mem->map_size = 0;
1559
1560 const VkImportMemoryFdInfoKHR *fd_info =
1561 vk_find_struct_const(pAllocateInfo->pNext, IMPORT_MEMORY_FD_INFO_KHR);
1562
1563 /* The Vulkan spec permits handleType to be 0, in which case the struct is
1564 * ignored.
1565 */
1566 if (fd_info && fd_info->handleType) {
1567 /* At the moment, we support only the below handle types. */
1568 assert(fd_info->handleType ==
1569 VK_EXTERNAL_MEMORY_HANDLE_TYPE_OPAQUE_FD_BIT_KHR ||
1570 fd_info->handleType ==
1571 VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_EXT);
1572
1573 result = anv_bo_cache_import(device, &device->bo_cache,
1574 fd_info->fd, &mem->bo);
1575 if (result != VK_SUCCESS)
1576 goto fail;
1577
1578 VkDeviceSize aligned_alloc_size =
1579 align_u64(pAllocateInfo->allocationSize, 4096);
1580
1581 /* For security purposes, we reject importing the bo if it's smaller
1582 * than the requested allocation size. This prevents a malicious client
1583 * from passing a buffer to a trusted client, lying about the size, and
1584 * telling the trusted client to try and texture from an image that goes
1585 * out-of-bounds. This sort of thing could lead to GPU hangs or worse
1586 * in the trusted client. The trusted client can protect itself against
1587 * this sort of attack but only if it can trust the buffer size.
1588 */
1589 if (mem->bo->size < aligned_alloc_size) {
1590 result = vk_errorf(device->instance, device,
1591 VK_ERROR_INVALID_EXTERNAL_HANDLE_KHR,
1592 "aligned allocationSize too large for "
1593 "VK_EXTERNAL_MEMORY_HANDLE_TYPE_OPAQUE_FD_BIT_KHR: "
1594 "%"PRIu64"B > %"PRIu64"B",
1595 aligned_alloc_size, mem->bo->size);
1596 anv_bo_cache_release(device, &device->bo_cache, mem->bo);
1597 goto fail;
1598 }
1599
1600 /* From the Vulkan spec:
1601 *
1602 * "Importing memory from a file descriptor transfers ownership of
1603 * the file descriptor from the application to the Vulkan
1604 * implementation. The application must not perform any operations on
1605 * the file descriptor after a successful import."
1606 *
1607 * If the import fails, we leave the file descriptor open.
1608 */
1609 close(fd_info->fd);
1610 } else {
1611 result = anv_bo_cache_alloc(device, &device->bo_cache,
1612 pAllocateInfo->allocationSize,
1613 &mem->bo);
1614 if (result != VK_SUCCESS)
1615 goto fail;
1616
1617 const VkMemoryDedicatedAllocateInfoKHR *dedicated_info =
1618 vk_find_struct_const(pAllocateInfo->pNext, MEMORY_DEDICATED_ALLOCATE_INFO_KHR);
1619 if (dedicated_info && dedicated_info->image != VK_NULL_HANDLE) {
1620 ANV_FROM_HANDLE(anv_image, image, dedicated_info->image);
1621
1622 /* For images using modifiers, we require a dedicated allocation
1623 * and we set the BO tiling to match the tiling of the underlying
1624 * modifier. This is a bit unfortunate as this is completely
1625 * pointless for Vulkan. However, GL needs to be able to map things
1626 * so it needs the tiling to be set. The only way to do this in a
1627 * non-racy way is to set the tiling in the creator of the BO so that
1628 * makes it our job.
1629 *
1630 * One of these days, once the GL driver learns to not map things
1631 * through the GTT in random places, we can drop this and start
1632 * allowing multiple modified images in the same BO.
1633 */
1634 if (image->drm_format_mod != DRM_FORMAT_MOD_INVALID) {
1635 assert(isl_drm_modifier_get_info(image->drm_format_mod)->tiling ==
1636 image->planes[0].surface.isl.tiling);
1637 const uint32_t i915_tiling =
1638 isl_tiling_to_i915_tiling(image->planes[0].surface.isl.tiling);
1639 int ret = anv_gem_set_tiling(device, mem->bo->gem_handle,
1640 image->planes[0].surface.isl.row_pitch,
1641 i915_tiling);
1642 if (ret) {
1643 anv_bo_cache_release(device, &device->bo_cache, mem->bo);
1644 return vk_errorf(device->instance, NULL,
1645 VK_ERROR_OUT_OF_DEVICE_MEMORY,
1646 "failed to set BO tiling: %m");
1647 }
1648 }
1649 }
1650 }
1651
1652 assert(mem->type->heapIndex < pdevice->memory.heap_count);
1653 if (pdevice->memory.heaps[mem->type->heapIndex].supports_48bit_addresses)
1654 mem->bo->flags |= EXEC_OBJECT_SUPPORTS_48B_ADDRESS;
1655
1656 if (pdevice->has_exec_async)
1657 mem->bo->flags |= EXEC_OBJECT_ASYNC;
1658
1659 *pMem = anv_device_memory_to_handle(mem);
1660
1661 return VK_SUCCESS;
1662
1663 fail:
1664 vk_free2(&device->alloc, pAllocator, mem);
1665
1666 return result;
1667 }
1668
1669 VkResult anv_GetMemoryFdKHR(
1670 VkDevice device_h,
1671 const VkMemoryGetFdInfoKHR* pGetFdInfo,
1672 int* pFd)
1673 {
1674 ANV_FROM_HANDLE(anv_device, dev, device_h);
1675 ANV_FROM_HANDLE(anv_device_memory, mem, pGetFdInfo->memory);
1676
1677 assert(pGetFdInfo->sType == VK_STRUCTURE_TYPE_MEMORY_GET_FD_INFO_KHR);
1678
1679 assert(pGetFdInfo->handleType == VK_EXTERNAL_MEMORY_HANDLE_TYPE_OPAQUE_FD_BIT_KHR ||
1680 pGetFdInfo->handleType == VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_EXT);
1681
1682 return anv_bo_cache_export(dev, &dev->bo_cache, mem->bo, pFd);
1683 }
1684
1685 VkResult anv_GetMemoryFdPropertiesKHR(
1686 VkDevice _device,
1687 VkExternalMemoryHandleTypeFlagBitsKHR handleType,
1688 int fd,
1689 VkMemoryFdPropertiesKHR* pMemoryFdProperties)
1690 {
1691 ANV_FROM_HANDLE(anv_device, device, _device);
1692 struct anv_physical_device *pdevice = &device->instance->physicalDevice;
1693
1694 switch (handleType) {
1695 case VK_EXTERNAL_MEMORY_HANDLE_TYPE_OPAQUE_FD_BIT_KHR:
1696 /* dma-buf can be imported as any memory type */
1697 pMemoryFdProperties->memoryTypeBits =
1698 (1 << pdevice->memory.type_count) - 1;
1699 return VK_SUCCESS;
1700
1701 default:
1702 /* The valid usage section for this function says:
1703 *
1704 * "handleType must not be one of the handle types defined as
1705 * opaque."
1706 *
1707 * So opaque handle types fall into the default "unsupported" case.
1708 */
1709 return vk_error(VK_ERROR_INVALID_EXTERNAL_HANDLE_KHR);
1710 }
1711 }
1712
1713 void anv_FreeMemory(
1714 VkDevice _device,
1715 VkDeviceMemory _mem,
1716 const VkAllocationCallbacks* pAllocator)
1717 {
1718 ANV_FROM_HANDLE(anv_device, device, _device);
1719 ANV_FROM_HANDLE(anv_device_memory, mem, _mem);
1720
1721 if (mem == NULL)
1722 return;
1723
1724 if (mem->map)
1725 anv_UnmapMemory(_device, _mem);
1726
1727 anv_bo_cache_release(device, &device->bo_cache, mem->bo);
1728
1729 vk_free2(&device->alloc, pAllocator, mem);
1730 }
1731
1732 VkResult anv_MapMemory(
1733 VkDevice _device,
1734 VkDeviceMemory _memory,
1735 VkDeviceSize offset,
1736 VkDeviceSize size,
1737 VkMemoryMapFlags flags,
1738 void** ppData)
1739 {
1740 ANV_FROM_HANDLE(anv_device, device, _device);
1741 ANV_FROM_HANDLE(anv_device_memory, mem, _memory);
1742
1743 if (mem == NULL) {
1744 *ppData = NULL;
1745 return VK_SUCCESS;
1746 }
1747
1748 if (size == VK_WHOLE_SIZE)
1749 size = mem->bo->size - offset;
1750
1751 /* From the Vulkan spec version 1.0.32 docs for MapMemory:
1752 *
1753 * * If size is not equal to VK_WHOLE_SIZE, size must be greater than 0
1754 * assert(size != 0);
1755 * * If size is not equal to VK_WHOLE_SIZE, size must be less than or
1756 * equal to the size of the memory minus offset
1757 */
1758 assert(size > 0);
1759 assert(offset + size <= mem->bo->size);
1760
1761 /* FIXME: Is this supposed to be thread safe? Since vkUnmapMemory() only
1762 * takes a VkDeviceMemory pointer, it seems like only one map of the memory
1763 * at a time is valid. We could just mmap up front and return an offset
1764 * pointer here, but that may exhaust virtual memory on 32 bit
1765 * userspace. */
1766
1767 uint32_t gem_flags = 0;
1768
1769 if (!device->info.has_llc &&
1770 (mem->type->propertyFlags & VK_MEMORY_PROPERTY_HOST_COHERENT_BIT))
1771 gem_flags |= I915_MMAP_WC;
1772
1773 /* GEM will fail to map if the offset isn't 4k-aligned. Round down. */
1774 uint64_t map_offset = offset & ~4095ull;
1775 assert(offset >= map_offset);
1776 uint64_t map_size = (offset + size) - map_offset;
1777
1778 /* Let's map whole pages */
1779 map_size = align_u64(map_size, 4096);
1780
1781 void *map = anv_gem_mmap(device, mem->bo->gem_handle,
1782 map_offset, map_size, gem_flags);
1783 if (map == MAP_FAILED)
1784 return vk_error(VK_ERROR_MEMORY_MAP_FAILED);
1785
1786 mem->map = map;
1787 mem->map_size = map_size;
1788
1789 *ppData = mem->map + (offset - map_offset);
1790
1791 return VK_SUCCESS;
1792 }
1793
1794 void anv_UnmapMemory(
1795 VkDevice _device,
1796 VkDeviceMemory _memory)
1797 {
1798 ANV_FROM_HANDLE(anv_device_memory, mem, _memory);
1799
1800 if (mem == NULL)
1801 return;
1802
1803 anv_gem_munmap(mem->map, mem->map_size);
1804
1805 mem->map = NULL;
1806 mem->map_size = 0;
1807 }
1808
1809 static void
1810 clflush_mapped_ranges(struct anv_device *device,
1811 uint32_t count,
1812 const VkMappedMemoryRange *ranges)
1813 {
1814 for (uint32_t i = 0; i < count; i++) {
1815 ANV_FROM_HANDLE(anv_device_memory, mem, ranges[i].memory);
1816 if (ranges[i].offset >= mem->map_size)
1817 continue;
1818
1819 gen_clflush_range(mem->map + ranges[i].offset,
1820 MIN2(ranges[i].size, mem->map_size - ranges[i].offset));
1821 }
1822 }
1823
1824 VkResult anv_FlushMappedMemoryRanges(
1825 VkDevice _device,
1826 uint32_t memoryRangeCount,
1827 const VkMappedMemoryRange* pMemoryRanges)
1828 {
1829 ANV_FROM_HANDLE(anv_device, device, _device);
1830
1831 if (device->info.has_llc)
1832 return VK_SUCCESS;
1833
1834 /* Make sure the writes we're flushing have landed. */
1835 __builtin_ia32_mfence();
1836
1837 clflush_mapped_ranges(device, memoryRangeCount, pMemoryRanges);
1838
1839 return VK_SUCCESS;
1840 }
1841
1842 VkResult anv_InvalidateMappedMemoryRanges(
1843 VkDevice _device,
1844 uint32_t memoryRangeCount,
1845 const VkMappedMemoryRange* pMemoryRanges)
1846 {
1847 ANV_FROM_HANDLE(anv_device, device, _device);
1848
1849 if (device->info.has_llc)
1850 return VK_SUCCESS;
1851
1852 clflush_mapped_ranges(device, memoryRangeCount, pMemoryRanges);
1853
1854 /* Make sure no reads get moved up above the invalidate. */
1855 __builtin_ia32_mfence();
1856
1857 return VK_SUCCESS;
1858 }
1859
1860 void anv_GetBufferMemoryRequirements(
1861 VkDevice _device,
1862 VkBuffer _buffer,
1863 VkMemoryRequirements* pMemoryRequirements)
1864 {
1865 ANV_FROM_HANDLE(anv_buffer, buffer, _buffer);
1866 ANV_FROM_HANDLE(anv_device, device, _device);
1867 struct anv_physical_device *pdevice = &device->instance->physicalDevice;
1868
1869 /* The Vulkan spec (git aaed022) says:
1870 *
1871 * memoryTypeBits is a bitfield and contains one bit set for every
1872 * supported memory type for the resource. The bit `1<<i` is set if and
1873 * only if the memory type `i` in the VkPhysicalDeviceMemoryProperties
1874 * structure for the physical device is supported.
1875 */
1876 uint32_t memory_types = 0;
1877 for (uint32_t i = 0; i < pdevice->memory.type_count; i++) {
1878 uint32_t valid_usage = pdevice->memory.types[i].valid_buffer_usage;
1879 if ((valid_usage & buffer->usage) == buffer->usage)
1880 memory_types |= (1u << i);
1881 }
1882
1883 pMemoryRequirements->size = buffer->size;
1884 pMemoryRequirements->alignment = 16;
1885 pMemoryRequirements->memoryTypeBits = memory_types;
1886 }
1887
1888 void anv_GetBufferMemoryRequirements2KHR(
1889 VkDevice _device,
1890 const VkBufferMemoryRequirementsInfo2KHR* pInfo,
1891 VkMemoryRequirements2KHR* pMemoryRequirements)
1892 {
1893 anv_GetBufferMemoryRequirements(_device, pInfo->buffer,
1894 &pMemoryRequirements->memoryRequirements);
1895
1896 vk_foreach_struct(ext, pMemoryRequirements->pNext) {
1897 switch (ext->sType) {
1898 case VK_STRUCTURE_TYPE_MEMORY_DEDICATED_REQUIREMENTS_KHR: {
1899 VkMemoryDedicatedRequirementsKHR *requirements = (void *)ext;
1900 requirements->prefersDedicatedAllocation = VK_FALSE;
1901 requirements->requiresDedicatedAllocation = VK_FALSE;
1902 break;
1903 }
1904
1905 default:
1906 anv_debug_ignored_stype(ext->sType);
1907 break;
1908 }
1909 }
1910 }
1911
1912 void anv_GetImageMemoryRequirements(
1913 VkDevice _device,
1914 VkImage _image,
1915 VkMemoryRequirements* pMemoryRequirements)
1916 {
1917 ANV_FROM_HANDLE(anv_image, image, _image);
1918 ANV_FROM_HANDLE(anv_device, device, _device);
1919 struct anv_physical_device *pdevice = &device->instance->physicalDevice;
1920
1921 /* The Vulkan spec (git aaed022) says:
1922 *
1923 * memoryTypeBits is a bitfield and contains one bit set for every
1924 * supported memory type for the resource. The bit `1<<i` is set if and
1925 * only if the memory type `i` in the VkPhysicalDeviceMemoryProperties
1926 * structure for the physical device is supported.
1927 *
1928 * All types are currently supported for images.
1929 */
1930 uint32_t memory_types = (1ull << pdevice->memory.type_count) - 1;
1931
1932 pMemoryRequirements->size = image->size;
1933 pMemoryRequirements->alignment = image->alignment;
1934 pMemoryRequirements->memoryTypeBits = memory_types;
1935 }
1936
1937 void anv_GetImageMemoryRequirements2KHR(
1938 VkDevice _device,
1939 const VkImageMemoryRequirementsInfo2KHR* pInfo,
1940 VkMemoryRequirements2KHR* pMemoryRequirements)
1941 {
1942 ANV_FROM_HANDLE(anv_device, device, _device);
1943 ANV_FROM_HANDLE(anv_image, image, pInfo->image);
1944
1945 anv_GetImageMemoryRequirements(_device, pInfo->image,
1946 &pMemoryRequirements->memoryRequirements);
1947
1948 vk_foreach_struct_const(ext, pInfo->pNext) {
1949 switch (ext->sType) {
1950 case VK_STRUCTURE_TYPE_IMAGE_PLANE_MEMORY_REQUIREMENTS_INFO_KHR: {
1951 struct anv_physical_device *pdevice = &device->instance->physicalDevice;
1952 const VkImagePlaneMemoryRequirementsInfoKHR *plane_reqs =
1953 (const VkImagePlaneMemoryRequirementsInfoKHR *) ext;
1954 uint32_t plane = anv_image_aspect_to_plane(image->aspects,
1955 plane_reqs->planeAspect);
1956
1957 assert(image->planes[plane].offset == 0);
1958
1959 /* The Vulkan spec (git aaed022) says:
1960 *
1961 * memoryTypeBits is a bitfield and contains one bit set for every
1962 * supported memory type for the resource. The bit `1<<i` is set
1963 * if and only if the memory type `i` in the
1964 * VkPhysicalDeviceMemoryProperties structure for the physical
1965 * device is supported.
1966 *
1967 * All types are currently supported for images.
1968 */
1969 pMemoryRequirements->memoryRequirements.memoryTypeBits =
1970 (1ull << pdevice->memory.type_count) - 1;
1971
1972 pMemoryRequirements->memoryRequirements.size = image->planes[plane].size;
1973 pMemoryRequirements->memoryRequirements.alignment =
1974 image->planes[plane].alignment;
1975 break;
1976 }
1977
1978 default:
1979 anv_debug_ignored_stype(ext->sType);
1980 break;
1981 }
1982 }
1983
1984 vk_foreach_struct(ext, pMemoryRequirements->pNext) {
1985 switch (ext->sType) {
1986 case VK_STRUCTURE_TYPE_MEMORY_DEDICATED_REQUIREMENTS_KHR: {
1987 VkMemoryDedicatedRequirementsKHR *requirements = (void *)ext;
1988 if (image->drm_format_mod != DRM_FORMAT_MOD_INVALID) {
1989 /* Require a dedicated allocation for images with modifiers.
1990 *
1991 * See also anv_AllocateMemory.
1992 */
1993 requirements->prefersDedicatedAllocation = VK_TRUE;
1994 requirements->requiresDedicatedAllocation = VK_TRUE;
1995 } else {
1996 requirements->prefersDedicatedAllocation = VK_FALSE;
1997 requirements->requiresDedicatedAllocation = VK_FALSE;
1998 }
1999 break;
2000 }
2001
2002 default:
2003 anv_debug_ignored_stype(ext->sType);
2004 break;
2005 }
2006 }
2007 }
2008
2009 void anv_GetImageSparseMemoryRequirements(
2010 VkDevice device,
2011 VkImage image,
2012 uint32_t* pSparseMemoryRequirementCount,
2013 VkSparseImageMemoryRequirements* pSparseMemoryRequirements)
2014 {
2015 *pSparseMemoryRequirementCount = 0;
2016 }
2017
2018 void anv_GetImageSparseMemoryRequirements2KHR(
2019 VkDevice device,
2020 const VkImageSparseMemoryRequirementsInfo2KHR* pInfo,
2021 uint32_t* pSparseMemoryRequirementCount,
2022 VkSparseImageMemoryRequirements2KHR* pSparseMemoryRequirements)
2023 {
2024 *pSparseMemoryRequirementCount = 0;
2025 }
2026
2027 void anv_GetDeviceMemoryCommitment(
2028 VkDevice device,
2029 VkDeviceMemory memory,
2030 VkDeviceSize* pCommittedMemoryInBytes)
2031 {
2032 *pCommittedMemoryInBytes = 0;
2033 }
2034
2035 static void
2036 anv_bind_buffer_memory(const VkBindBufferMemoryInfoKHR *pBindInfo)
2037 {
2038 ANV_FROM_HANDLE(anv_device_memory, mem, pBindInfo->memory);
2039 ANV_FROM_HANDLE(anv_buffer, buffer, pBindInfo->buffer);
2040
2041 assert(pBindInfo->sType == VK_STRUCTURE_TYPE_BIND_BUFFER_MEMORY_INFO_KHR);
2042
2043 if (mem) {
2044 assert((buffer->usage & mem->type->valid_buffer_usage) == buffer->usage);
2045 buffer->bo = mem->bo;
2046 buffer->offset = pBindInfo->memoryOffset;
2047 } else {
2048 buffer->bo = NULL;
2049 buffer->offset = 0;
2050 }
2051 }
2052
2053 VkResult anv_BindBufferMemory(
2054 VkDevice device,
2055 VkBuffer buffer,
2056 VkDeviceMemory memory,
2057 VkDeviceSize memoryOffset)
2058 {
2059 anv_bind_buffer_memory(
2060 &(VkBindBufferMemoryInfoKHR) {
2061 .sType = VK_STRUCTURE_TYPE_BIND_BUFFER_MEMORY_INFO_KHR,
2062 .buffer = buffer,
2063 .memory = memory,
2064 .memoryOffset = memoryOffset,
2065 });
2066
2067 return VK_SUCCESS;
2068 }
2069
2070 VkResult anv_BindBufferMemory2KHR(
2071 VkDevice device,
2072 uint32_t bindInfoCount,
2073 const VkBindBufferMemoryInfoKHR* pBindInfos)
2074 {
2075 for (uint32_t i = 0; i < bindInfoCount; i++)
2076 anv_bind_buffer_memory(&pBindInfos[i]);
2077
2078 return VK_SUCCESS;
2079 }
2080
2081 VkResult anv_QueueBindSparse(
2082 VkQueue _queue,
2083 uint32_t bindInfoCount,
2084 const VkBindSparseInfo* pBindInfo,
2085 VkFence fence)
2086 {
2087 ANV_FROM_HANDLE(anv_queue, queue, _queue);
2088 if (unlikely(queue->device->lost))
2089 return VK_ERROR_DEVICE_LOST;
2090
2091 return vk_error(VK_ERROR_FEATURE_NOT_PRESENT);
2092 }
2093
2094 // Event functions
2095
2096 VkResult anv_CreateEvent(
2097 VkDevice _device,
2098 const VkEventCreateInfo* pCreateInfo,
2099 const VkAllocationCallbacks* pAllocator,
2100 VkEvent* pEvent)
2101 {
2102 ANV_FROM_HANDLE(anv_device, device, _device);
2103 struct anv_state state;
2104 struct anv_event *event;
2105
2106 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_EVENT_CREATE_INFO);
2107
2108 state = anv_state_pool_alloc(&device->dynamic_state_pool,
2109 sizeof(*event), 8);
2110 event = state.map;
2111 event->state = state;
2112 event->semaphore = VK_EVENT_RESET;
2113
2114 if (!device->info.has_llc) {
2115 /* Make sure the writes we're flushing have landed. */
2116 __builtin_ia32_mfence();
2117 __builtin_ia32_clflush(event);
2118 }
2119
2120 *pEvent = anv_event_to_handle(event);
2121
2122 return VK_SUCCESS;
2123 }
2124
2125 void anv_DestroyEvent(
2126 VkDevice _device,
2127 VkEvent _event,
2128 const VkAllocationCallbacks* pAllocator)
2129 {
2130 ANV_FROM_HANDLE(anv_device, device, _device);
2131 ANV_FROM_HANDLE(anv_event, event, _event);
2132
2133 if (!event)
2134 return;
2135
2136 anv_state_pool_free(&device->dynamic_state_pool, event->state);
2137 }
2138
2139 VkResult anv_GetEventStatus(
2140 VkDevice _device,
2141 VkEvent _event)
2142 {
2143 ANV_FROM_HANDLE(anv_device, device, _device);
2144 ANV_FROM_HANDLE(anv_event, event, _event);
2145
2146 if (unlikely(device->lost))
2147 return VK_ERROR_DEVICE_LOST;
2148
2149 if (!device->info.has_llc) {
2150 /* Invalidate read cache before reading event written by GPU. */
2151 __builtin_ia32_clflush(event);
2152 __builtin_ia32_mfence();
2153
2154 }
2155
2156 return event->semaphore;
2157 }
2158
2159 VkResult anv_SetEvent(
2160 VkDevice _device,
2161 VkEvent _event)
2162 {
2163 ANV_FROM_HANDLE(anv_device, device, _device);
2164 ANV_FROM_HANDLE(anv_event, event, _event);
2165
2166 event->semaphore = VK_EVENT_SET;
2167
2168 if (!device->info.has_llc) {
2169 /* Make sure the writes we're flushing have landed. */
2170 __builtin_ia32_mfence();
2171 __builtin_ia32_clflush(event);
2172 }
2173
2174 return VK_SUCCESS;
2175 }
2176
2177 VkResult anv_ResetEvent(
2178 VkDevice _device,
2179 VkEvent _event)
2180 {
2181 ANV_FROM_HANDLE(anv_device, device, _device);
2182 ANV_FROM_HANDLE(anv_event, event, _event);
2183
2184 event->semaphore = VK_EVENT_RESET;
2185
2186 if (!device->info.has_llc) {
2187 /* Make sure the writes we're flushing have landed. */
2188 __builtin_ia32_mfence();
2189 __builtin_ia32_clflush(event);
2190 }
2191
2192 return VK_SUCCESS;
2193 }
2194
2195 // Buffer functions
2196
2197 VkResult anv_CreateBuffer(
2198 VkDevice _device,
2199 const VkBufferCreateInfo* pCreateInfo,
2200 const VkAllocationCallbacks* pAllocator,
2201 VkBuffer* pBuffer)
2202 {
2203 ANV_FROM_HANDLE(anv_device, device, _device);
2204 struct anv_buffer *buffer;
2205
2206 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO);
2207
2208 buffer = vk_alloc2(&device->alloc, pAllocator, sizeof(*buffer), 8,
2209 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
2210 if (buffer == NULL)
2211 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
2212
2213 buffer->size = pCreateInfo->size;
2214 buffer->usage = pCreateInfo->usage;
2215 buffer->bo = NULL;
2216 buffer->offset = 0;
2217
2218 *pBuffer = anv_buffer_to_handle(buffer);
2219
2220 return VK_SUCCESS;
2221 }
2222
2223 void anv_DestroyBuffer(
2224 VkDevice _device,
2225 VkBuffer _buffer,
2226 const VkAllocationCallbacks* pAllocator)
2227 {
2228 ANV_FROM_HANDLE(anv_device, device, _device);
2229 ANV_FROM_HANDLE(anv_buffer, buffer, _buffer);
2230
2231 if (!buffer)
2232 return;
2233
2234 vk_free2(&device->alloc, pAllocator, buffer);
2235 }
2236
2237 void
2238 anv_fill_buffer_surface_state(struct anv_device *device, struct anv_state state,
2239 enum isl_format format,
2240 uint32_t offset, uint32_t range, uint32_t stride)
2241 {
2242 isl_buffer_fill_state(&device->isl_dev, state.map,
2243 .address = offset,
2244 .mocs = device->default_mocs,
2245 .size = range,
2246 .format = format,
2247 .stride = stride);
2248
2249 anv_state_flush(device, state);
2250 }
2251
2252 void anv_DestroySampler(
2253 VkDevice _device,
2254 VkSampler _sampler,
2255 const VkAllocationCallbacks* pAllocator)
2256 {
2257 ANV_FROM_HANDLE(anv_device, device, _device);
2258 ANV_FROM_HANDLE(anv_sampler, sampler, _sampler);
2259
2260 if (!sampler)
2261 return;
2262
2263 vk_free2(&device->alloc, pAllocator, sampler);
2264 }
2265
2266 VkResult anv_CreateFramebuffer(
2267 VkDevice _device,
2268 const VkFramebufferCreateInfo* pCreateInfo,
2269 const VkAllocationCallbacks* pAllocator,
2270 VkFramebuffer* pFramebuffer)
2271 {
2272 ANV_FROM_HANDLE(anv_device, device, _device);
2273 struct anv_framebuffer *framebuffer;
2274
2275 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO);
2276
2277 size_t size = sizeof(*framebuffer) +
2278 sizeof(struct anv_image_view *) * pCreateInfo->attachmentCount;
2279 framebuffer = vk_alloc2(&device->alloc, pAllocator, size, 8,
2280 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
2281 if (framebuffer == NULL)
2282 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
2283
2284 framebuffer->attachment_count = pCreateInfo->attachmentCount;
2285 for (uint32_t i = 0; i < pCreateInfo->attachmentCount; i++) {
2286 VkImageView _iview = pCreateInfo->pAttachments[i];
2287 framebuffer->attachments[i] = anv_image_view_from_handle(_iview);
2288 }
2289
2290 framebuffer->width = pCreateInfo->width;
2291 framebuffer->height = pCreateInfo->height;
2292 framebuffer->layers = pCreateInfo->layers;
2293
2294 *pFramebuffer = anv_framebuffer_to_handle(framebuffer);
2295
2296 return VK_SUCCESS;
2297 }
2298
2299 void anv_DestroyFramebuffer(
2300 VkDevice _device,
2301 VkFramebuffer _fb,
2302 const VkAllocationCallbacks* pAllocator)
2303 {
2304 ANV_FROM_HANDLE(anv_device, device, _device);
2305 ANV_FROM_HANDLE(anv_framebuffer, fb, _fb);
2306
2307 if (!fb)
2308 return;
2309
2310 vk_free2(&device->alloc, pAllocator, fb);
2311 }
2312
2313 /* vk_icd.h does not declare this function, so we declare it here to
2314 * suppress Wmissing-prototypes.
2315 */
2316 PUBLIC VKAPI_ATTR VkResult VKAPI_CALL
2317 vk_icdNegotiateLoaderICDInterfaceVersion(uint32_t* pSupportedVersion);
2318
2319 PUBLIC VKAPI_ATTR VkResult VKAPI_CALL
2320 vk_icdNegotiateLoaderICDInterfaceVersion(uint32_t* pSupportedVersion)
2321 {
2322 /* For the full details on loader interface versioning, see
2323 * <https://github.com/KhronosGroup/Vulkan-LoaderAndValidationLayers/blob/master/loader/LoaderAndLayerInterface.md>.
2324 * What follows is a condensed summary, to help you navigate the large and
2325 * confusing official doc.
2326 *
2327 * - Loader interface v0 is incompatible with later versions. We don't
2328 * support it.
2329 *
2330 * - In loader interface v1:
2331 * - The first ICD entrypoint called by the loader is
2332 * vk_icdGetInstanceProcAddr(). The ICD must statically expose this
2333 * entrypoint.
2334 * - The ICD must statically expose no other Vulkan symbol unless it is
2335 * linked with -Bsymbolic.
2336 * - Each dispatchable Vulkan handle created by the ICD must be
2337 * a pointer to a struct whose first member is VK_LOADER_DATA. The
2338 * ICD must initialize VK_LOADER_DATA.loadMagic to ICD_LOADER_MAGIC.
2339 * - The loader implements vkCreate{PLATFORM}SurfaceKHR() and
2340 * vkDestroySurfaceKHR(). The ICD must be capable of working with
2341 * such loader-managed surfaces.
2342 *
2343 * - Loader interface v2 differs from v1 in:
2344 * - The first ICD entrypoint called by the loader is
2345 * vk_icdNegotiateLoaderICDInterfaceVersion(). The ICD must
2346 * statically expose this entrypoint.
2347 *
2348 * - Loader interface v3 differs from v2 in:
2349 * - The ICD must implement vkCreate{PLATFORM}SurfaceKHR(),
2350 * vkDestroySurfaceKHR(), and other API which uses VKSurfaceKHR,
2351 * because the loader no longer does so.
2352 */
2353 *pSupportedVersion = MIN2(*pSupportedVersion, 3u);
2354 return VK_SUCCESS;
2355 }