2 * Copyright © 2015 Intel Corporation
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:
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
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
28 #include <sys/sysinfo.h>
32 #include <drm_fourcc.h>
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"
41 #include "genxml/gen7_pack.h"
44 compiler_debug_log(void *data
, const char *fmt
, ...)
48 compiler_perf_log(void *data
, const char *fmt
, ...)
53 if (unlikely(INTEL_DEBUG
& DEBUG_PERF
))
54 intel_logd_v(fmt
, args
);
60 anv_compute_heap_size(int fd
, uint64_t *heap_size
)
63 if (anv_gem_get_context_param(fd
, 0, I915_CONTEXT_PARAM_GTT_SIZE
,
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.
68 anv_perf_warn(NULL
, NULL
,
69 "Failed to get I915_CONTEXT_PARAM_GTT_SIZE: %m");
71 if (anv_gem_get_aperture(fd
, >t_size
) == -1) {
72 return vk_errorf(NULL
, NULL
, VK_ERROR_INITIALIZATION_FAILED
,
73 "failed to get aperture size: %m");
77 /* Query the total ram from the system */
81 uint64_t total_ram
= (uint64_t)info
.totalram
* (uint64_t)info
.mem_unit
;
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.
86 uint64_t available_ram
;
87 if (total_ram
<= 4ull * 1024ull * 1024ull * 1024ull)
88 available_ram
= total_ram
/ 2;
90 available_ram
= total_ram
* 3 / 4;
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.
95 uint64_t available_gtt
= gtt_size
* 3 / 4;
97 *heap_size
= MIN2(available_ram
, available_gtt
);
103 anv_physical_device_init_heaps(struct anv_physical_device
*device
, int fd
)
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.
109 device
->supports_48bit_addresses
=
110 (device
->info
.gen
>= 8) && anv_gem_supports_48b_addresses(fd
);
113 VkResult result
= anv_compute_heap_size(fd
, &heap_size
);
114 if (result
!= VK_SUCCESS
)
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.
123 intel_logw("%s:%d: The kernel reported a GTT size larger than 2 GiB but "
124 "not support for 48-bit addresses",
126 heap_size
= 2ull << 30;
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
134 device
->memory
.heap_count
= 1;
135 device
->memory
.heaps
[0] = (struct anv_memory_heap
) {
137 .flags
= VK_MEMORY_HEAP_DEVICE_LOCAL_BIT
,
138 .supports_48bit_addresses
= false,
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
148 const uint64_t heap_size_32bit
= 1ull << 30;
149 const uint64_t heap_size_48bit
= heap_size
- heap_size_32bit
;
151 assert(device
->supports_48bit_addresses
);
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,
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,
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;
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.
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
);
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.
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
,
192 .valid_buffer_usage
= valid_buffer_usage
,
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).
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
,
205 .valid_buffer_usage
= valid_buffer_usage
,
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
,
212 .valid_buffer_usage
= valid_buffer_usage
,
216 device
->memory
.type_count
= type_count
;
222 anv_physical_device_init_uuids(struct anv_physical_device
*device
)
224 const struct build_id_note
*note
=
225 build_id_find_nhdr_for_addr(anv_physical_device_init_uuids
);
227 return vk_errorf(device
->instance
, device
,
228 VK_ERROR_INITIALIZATION_FAILED
,
229 "Failed to find build-id");
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");
239 struct mesa_sha1 sha1_ctx
;
241 STATIC_ASSERT(VK_UUID_SIZE
<= sizeof(sha1
));
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.
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
);
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.
258 memcpy(device
->driver_uuid
, build_id_data(note
), VK_UUID_SIZE
);
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.
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
);
278 anv_physical_device_init(struct anv_physical_device
*device
,
279 struct anv_instance
*instance
,
285 brw_process_intel_debug_variable();
287 fd
= open(path
, O_RDWR
| O_CLOEXEC
);
289 return vk_error(VK_ERROR_INCOMPATIBLE_DRIVER
);
291 device
->_loader_data
.loaderMagic
= ICD_LOADER_MAGIC
;
292 device
->instance
= instance
;
294 assert(strlen(path
) < ARRAY_SIZE(device
->path
));
295 strncpy(device
->path
, path
, ARRAY_SIZE(device
->path
));
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
);
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
);
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");
321 result
= vk_errorf(device
->instance
, device
,
322 VK_ERROR_INCOMPATIBLE_DRIVER
,
323 "Vulkan not yet supported on %s", device
->name
);
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");
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");
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");
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");
361 result
= anv_physical_device_init_heaps(device
, fd
);
362 if (result
!= VK_SUCCESS
)
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
);
372 bool swizzled
= anv_gem_get_bit6_swizzle(fd
, I915_TILING_X
);
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
);
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.
383 if (device
->subslice_total
< 1 || device
->eu_total
< 1) {
384 intel_logw("Kernel 4.1 required to properly query GPU properties");
386 } else if (device
->info
.gen
== 7) {
387 device
->subslice_total
= 1 << (device
->info
.gt
- 1);
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
;
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
;
401 device
->compiler
= brw_compiler_create(NULL
, &device
->info
);
402 if (device
->compiler
== NULL
) {
403 result
= vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
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;
410 isl_device_init(&device
->isl_dev
, &device
->info
, swizzled
);
412 result
= anv_physical_device_init_uuids(device
);
413 if (result
!= VK_SUCCESS
)
416 result
= anv_init_wsi(device
);
417 if (result
!= VK_SUCCESS
) {
418 ralloc_free(device
->compiler
);
422 device
->local_fd
= fd
;
431 anv_physical_device_finish(struct anv_physical_device
*device
)
433 anv_finish_wsi(device
);
434 ralloc_free(device
->compiler
);
435 close(device
->local_fd
);
439 default_alloc_func(void *pUserData
, size_t size
, size_t align
,
440 VkSystemAllocationScope allocationScope
)
446 default_realloc_func(void *pUserData
, void *pOriginal
, size_t size
,
447 size_t align
, VkSystemAllocationScope allocationScope
)
449 return realloc(pOriginal
, size
);
453 default_free_func(void *pUserData
, void *pMemory
)
458 static const VkAllocationCallbacks default_alloc
= {
460 .pfnAllocation
= default_alloc_func
,
461 .pfnReallocation
= default_realloc_func
,
462 .pfnFree
= default_free_func
,
465 VkResult
anv_CreateInstance(
466 const VkInstanceCreateInfo
* pCreateInfo
,
467 const VkAllocationCallbacks
* pAllocator
,
468 VkInstance
* pInstance
)
470 struct anv_instance
*instance
;
472 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO
);
474 /* Check if user passed a debug report callback to be used during
475 * Create/Destroy of instance.
477 const VkDebugReportCallbackCreateInfoEXT
*ctor_cb
=
478 vk_find_struct_const(pCreateInfo
->pNext
,
479 DEBUG_REPORT_CALLBACK_CREATE_INFO_EXT
);
481 uint32_t client_version
;
482 if (pCreateInfo
->pApplicationInfo
&&
483 pCreateInfo
->pApplicationInfo
->apiVersion
!= 0) {
484 client_version
= pCreateInfo
->pApplicationInfo
->apiVersion
;
486 client_version
= VK_MAKE_VERSION(1, 0, 0);
489 if (VK_MAKE_VERSION(1, 0, 0) > client_version
||
490 client_version
> VK_MAKE_VERSION(1, 0, 0xfff)) {
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. */
499 "incompatible driver version",
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
));
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
);
515 instance
= vk_alloc2(&default_alloc
, pAllocator
, sizeof(*instance
), 8,
516 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE
);
518 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
520 instance
->_loader_data
.loaderMagic
= ICD_LOADER_MAGIC
;
523 instance
->alloc
= *pAllocator
;
525 instance
->alloc
= default_alloc
;
527 instance
->apiVersion
= client_version
;
528 instance
->physicalDeviceCount
= -1;
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
);
535 list_inithead(&instance
->callbacks
);
537 /* Store report debug callback to be used during DestroyInstance. */
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
;
546 VG(VALGRIND_CREATE_MEMPOOL(instance
, 0, false));
548 *pInstance
= anv_instance_to_handle(instance
);
553 void anv_DestroyInstance(
554 VkInstance _instance
,
555 const VkAllocationCallbacks
* pAllocator
)
557 ANV_FROM_HANDLE(anv_instance
, instance
, _instance
);
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
);
568 VG(VALGRIND_DESTROY_MEMPOOL(instance
));
570 pthread_mutex_destroy(&instance
->callbacks_mutex
);
574 vk_free(&instance
->alloc
, instance
);
578 anv_enumerate_devices(struct anv_instance
*instance
)
580 /* TODO: Check for more devices ? */
581 drmDevicePtr devices
[8];
582 VkResult result
= VK_ERROR_INCOMPATIBLE_DRIVER
;
585 instance
->physicalDeviceCount
= 0;
587 max_devices
= drmGetDevices2(0, devices
, ARRAY_SIZE(devices
));
589 return VK_ERROR_INCOMPATIBLE_DRIVER
;
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) {
596 result
= anv_physical_device_init(&instance
->physicalDevice
,
598 devices
[i
]->nodes
[DRM_NODE_RENDER
]);
599 if (result
!= VK_ERROR_INCOMPATIBLE_DRIVER
)
603 drmFreeDevices(devices
, max_devices
);
605 if (result
== VK_SUCCESS
)
606 instance
->physicalDeviceCount
= 1;
612 VkResult
anv_EnumeratePhysicalDevices(
613 VkInstance _instance
,
614 uint32_t* pPhysicalDeviceCount
,
615 VkPhysicalDevice
* pPhysicalDevices
)
617 ANV_FROM_HANDLE(anv_instance
, instance
, _instance
);
618 VK_OUTARRAY_MAKE(out
, pPhysicalDevices
, pPhysicalDeviceCount
);
621 if (instance
->physicalDeviceCount
< 0) {
622 result
= anv_enumerate_devices(instance
);
623 if (result
!= VK_SUCCESS
&&
624 result
!= VK_ERROR_INCOMPATIBLE_DRIVER
)
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
);
635 return vk_outarray_status(&out
);
638 void anv_GetPhysicalDeviceFeatures(
639 VkPhysicalDevice physicalDevice
,
640 VkPhysicalDeviceFeatures
* pFeatures
)
642 ANV_FROM_HANDLE(anv_physical_device
, pdevice
, physicalDevice
);
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,
654 .multiDrawIndirect
= true,
655 .drawIndirectFirstInstance
= true,
657 .depthBiasClamp
= true,
658 .fillModeNonSolid
= true,
659 .depthBounds
= false,
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,
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
];
698 void anv_GetPhysicalDeviceFeatures2KHR(
699 VkPhysicalDevice physicalDevice
,
700 VkPhysicalDeviceFeatures2KHR
* pFeatures
)
702 anv_GetPhysicalDeviceFeatures(physicalDevice
, &pFeatures
->features
);
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;
715 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VARIABLE_POINTER_FEATURES_KHR
: {
716 VkPhysicalDeviceVariablePointerFeaturesKHR
*features
= (void *)ext
;
717 features
->variablePointersStorageBuffer
= true;
718 features
->variablePointers
= false;
722 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SAMPLER_YCBCR_CONVERSION_FEATURES_KHR
: {
723 VkPhysicalDeviceSamplerYcbcrConversionFeaturesKHR
*features
=
724 (VkPhysicalDeviceSamplerYcbcrConversionFeaturesKHR
*) ext
;
725 features
->samplerYcbcrConversion
= true;
730 anv_debug_ignored_stype(ext
->sType
);
736 void anv_GetPhysicalDeviceProperties(
737 VkPhysicalDevice physicalDevice
,
738 VkPhysicalDeviceProperties
* pProperties
)
740 ANV_FROM_HANDLE(anv_physical_device
, pdevice
, physicalDevice
);
741 const struct gen_device_info
*devinfo
= &pdevice
->info
;
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);
747 const uint32_t max_samplers
= (devinfo
->gen
>= 8 || devinfo
->is_haswell
) ?
750 VkSampleCountFlags sample_counts
=
751 isl_device_get_sample_counts(&pdevice
->isl_dev
);
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
,
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,
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,
866 *pProperties
= (VkPhysicalDeviceProperties
) {
867 .apiVersion
= anv_physical_device_api_version(pdevice
),
868 .driverVersion
= vk_get_driver_version(),
870 .deviceID
= pdevice
->chipset_id
,
871 .deviceType
= VK_PHYSICAL_DEVICE_TYPE_INTEGRATED_GPU
,
873 .sparseProperties
= {0}, /* Broadwell doesn't do sparse. */
876 snprintf(pProperties
->deviceName
, sizeof(pProperties
->deviceName
),
877 "%s", pdevice
->name
);
878 memcpy(pProperties
->pipelineCacheUUID
,
879 pdevice
->pipeline_cache_uuid
, VK_UUID_SIZE
);
882 void anv_GetPhysicalDeviceProperties2KHR(
883 VkPhysicalDevice physicalDevice
,
884 VkPhysicalDeviceProperties2KHR
* pProperties
)
886 ANV_FROM_HANDLE(anv_physical_device
, pdevice
, physicalDevice
);
888 anv_GetPhysicalDeviceProperties(physicalDevice
, &pProperties
->properties
);
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
;
896 properties
->maxPushDescriptors
= MAX_PUSH_DESCRIPTORS
;
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;
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;
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");
927 anv_debug_ignored_stype(ext
->sType
);
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
,
940 .timestampValidBits
= 36, /* XXX: Real value here */
941 .minImageTransferGranularity
= { 1, 1, 1 },
944 void anv_GetPhysicalDeviceQueueFamilyProperties(
945 VkPhysicalDevice physicalDevice
,
947 VkQueueFamilyProperties
* pQueueFamilyProperties
)
949 VK_OUTARRAY_MAKE(out
, pQueueFamilyProperties
, pCount
);
951 vk_outarray_append(&out
, p
) {
952 *p
= anv_queue_family_properties
;
956 void anv_GetPhysicalDeviceQueueFamilyProperties2KHR(
957 VkPhysicalDevice physicalDevice
,
958 uint32_t* pQueueFamilyPropertyCount
,
959 VkQueueFamilyProperties2KHR
* pQueueFamilyProperties
)
962 VK_OUTARRAY_MAKE(out
, pQueueFamilyProperties
, pQueueFamilyPropertyCount
);
964 vk_outarray_append(&out
, p
) {
965 p
->queueFamilyProperties
= anv_queue_family_properties
;
967 vk_foreach_struct(s
, p
->pNext
) {
968 anv_debug_ignored_stype(s
->sType
);
973 void anv_GetPhysicalDeviceMemoryProperties(
974 VkPhysicalDevice physicalDevice
,
975 VkPhysicalDeviceMemoryProperties
* pMemoryProperties
)
977 ANV_FROM_HANDLE(anv_physical_device
, physical_device
, physicalDevice
);
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
,
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
,
996 void anv_GetPhysicalDeviceMemoryProperties2KHR(
997 VkPhysicalDevice physicalDevice
,
998 VkPhysicalDeviceMemoryProperties2KHR
* pMemoryProperties
)
1000 anv_GetPhysicalDeviceMemoryProperties(physicalDevice
,
1001 &pMemoryProperties
->memoryProperties
);
1003 vk_foreach_struct(ext
, pMemoryProperties
->pNext
) {
1004 switch (ext
->sType
) {
1006 anv_debug_ignored_stype(ext
->sType
);
1012 PFN_vkVoidFunction
anv_GetInstanceProcAddr(
1013 VkInstance instance
,
1016 return anv_lookup_entrypoint(NULL
, pName
);
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.
1023 VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL
vk_icdGetInstanceProcAddr(
1024 VkInstance instance
,
1028 VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL
vk_icdGetInstanceProcAddr(
1029 VkInstance instance
,
1032 return anv_GetInstanceProcAddr(instance
, pName
);
1035 PFN_vkVoidFunction
anv_GetDeviceProcAddr(
1039 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1040 return anv_lookup_entrypoint(&device
->info
, pName
);
1044 anv_queue_init(struct anv_device
*device
, struct anv_queue
*queue
)
1046 queue
->_loader_data
.loaderMagic
= ICD_LOADER_MAGIC
;
1047 queue
->device
= device
;
1048 queue
->pool
= &device
->surface_state_pool
;
1052 anv_queue_finish(struct anv_queue
*queue
)
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
)
1059 struct anv_state state
;
1061 state
= anv_state_pool_alloc(pool
, size
, align
);
1062 memcpy(state
.map
, p
, size
);
1064 anv_state_flush(pool
->block_pool
.device
, state
);
1069 struct gen8_border_color
{
1074 /* Pad out to 64 bytes */
1079 anv_device_init_border_colors(struct anv_device
*device
)
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 } },
1090 device
->border_colors
= anv_state_pool_emit_data(&device
->dynamic_state_pool
,
1091 sizeof(border_colors
), 64,
1096 anv_device_init_trivial_batch(struct anv_device
*device
)
1098 anv_bo_init_new(&device
->trivial_batch_bo
, device
, 4096);
1100 if (device
->instance
->physicalDevice
.has_exec_async
)
1101 device
->trivial_batch_bo
.flags
|= EXEC_OBJECT_ASYNC
;
1103 void *map
= anv_gem_mmap(device
, device
->trivial_batch_bo
.gem_handle
,
1106 struct anv_batch batch
= {
1112 anv_batch_emit(&batch
, GEN7_MI_BATCH_BUFFER_END
, bbe
);
1113 anv_batch_emit(&batch
, GEN7_MI_NOOP
, noop
);
1115 if (!device
->info
.has_llc
)
1116 gen_clflush_range(map
, batch
.next
- map
);
1118 anv_gem_munmap(map
, device
->trivial_batch_bo
.size
);
1121 VkResult
anv_CreateDevice(
1122 VkPhysicalDevice physicalDevice
,
1123 const VkDeviceCreateInfo
* pCreateInfo
,
1124 const VkAllocationCallbacks
* pAllocator
,
1127 ANV_FROM_HANDLE(anv_physical_device
, physical_device
, physicalDevice
);
1129 struct anv_device
*device
;
1131 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO
);
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
);
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
);
1152 device
= vk_alloc2(&physical_device
->instance
->alloc
, pAllocator
,
1154 VK_SYSTEM_ALLOCATION_SCOPE_DEVICE
);
1156 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
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;
1164 device
->alloc
= *pAllocator
;
1166 device
->alloc
= physical_device
->instance
->alloc
;
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
);
1175 device
->context_id
= anv_gem_create_context(device
);
1176 if (device
->context_id
== -1) {
1177 result
= vk_error(VK_ERROR_INITIALIZATION_FAILED
);
1181 device
->info
= physical_device
->info
;
1182 device
->isl_dev
= physical_device
->isl_dev
;
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
1189 device
->can_chain_batches
= device
->info
.gen
>= 8;
1191 device
->robust_buffer_access
= pCreateInfo
->pEnabledFeatures
&&
1192 pCreateInfo
->pEnabledFeatures
->robustBufferAccess
;
1194 if (pthread_mutex_init(&device
->mutex
, NULL
) != 0) {
1195 result
= vk_error(VK_ERROR_INITIALIZATION_FAILED
);
1196 goto fail_context_id
;
1199 pthread_condattr_t condattr
;
1200 if (pthread_condattr_init(&condattr
) != 0) {
1201 result
= vk_error(VK_ERROR_INITIALIZATION_FAILED
);
1204 if (pthread_condattr_setclock(&condattr
, CLOCK_MONOTONIC
) != 0) {
1205 pthread_condattr_destroy(&condattr
);
1206 result
= vk_error(VK_ERROR_INITIALIZATION_FAILED
);
1209 if (pthread_cond_init(&device
->queue_submit
, NULL
) != 0) {
1210 pthread_condattr_destroy(&condattr
);
1211 result
= vk_error(VK_ERROR_INITIALIZATION_FAILED
);
1214 pthread_condattr_destroy(&condattr
);
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);
1221 anv_bo_pool_init(&device
->batch_bo_pool
, device
, bo_flags
);
1223 result
= anv_bo_cache_init(&device
->bo_cache
);
1224 if (result
!= VK_SUCCESS
)
1225 goto fail_batch_bo_pool
;
1227 /* For the state pools we explicitly disable 48bit. */
1228 bo_flags
= physical_device
->has_exec_async
? EXEC_OBJECT_ASYNC
: 0;
1230 result
= anv_state_pool_init(&device
->dynamic_state_pool
, device
, 16384,
1232 if (result
!= VK_SUCCESS
)
1235 result
= anv_state_pool_init(&device
->instruction_state_pool
, device
, 16384,
1237 (physical_device
->has_exec_capture
? EXEC_OBJECT_CAPTURE
: 0));
1238 if (result
!= VK_SUCCESS
)
1239 goto fail_dynamic_state_pool
;
1241 result
= anv_state_pool_init(&device
->surface_state_pool
, device
, 4096,
1243 if (result
!= VK_SUCCESS
)
1244 goto fail_instruction_state_pool
;
1246 result
= anv_bo_init_new(&device
->workaround_bo
, device
, 1024);
1247 if (result
!= VK_SUCCESS
)
1248 goto fail_surface_state_pool
;
1250 anv_device_init_trivial_batch(device
);
1252 anv_scratch_pool_init(device
, &device
->scratch_pool
);
1254 anv_queue_init(device
, &device
->queue
);
1256 switch (device
->info
.gen
) {
1258 if (!device
->info
.is_haswell
)
1259 result
= gen7_init_device_state(device
);
1261 result
= gen75_init_device_state(device
);
1264 result
= gen8_init_device_state(device
);
1267 result
= gen9_init_device_state(device
);
1270 result
= gen10_init_device_state(device
);
1273 /* Shouldn't get here as we don't create physical devices for any other
1275 unreachable("unhandled gen");
1277 if (result
!= VK_SUCCESS
)
1278 goto fail_workaround_bo
;
1280 anv_device_init_blorp(device
);
1282 anv_device_init_border_colors(device
);
1284 *pDevice
= anv_device_to_handle(device
);
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
);
1300 anv_bo_cache_finish(&device
->bo_cache
);
1302 anv_bo_pool_finish(&device
->batch_bo_pool
);
1303 pthread_cond_destroy(&device
->queue_submit
);
1305 pthread_mutex_destroy(&device
->mutex
);
1307 anv_gem_destroy_context(device
, device
->context_id
);
1311 vk_free(&device
->alloc
, device
);
1316 void anv_DestroyDevice(
1318 const VkAllocationCallbacks
* pAllocator
)
1320 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1325 anv_device_finish_blorp(device
);
1327 anv_queue_finish(&device
->queue
);
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.
1333 anv_state_pool_free(&device
->dynamic_state_pool
, device
->border_colors
);
1336 anv_scratch_pool_finish(device
, &device
->scratch_pool
);
1338 anv_gem_munmap(device
->workaround_bo
.map
, device
->workaround_bo
.size
);
1339 anv_gem_close(device
, device
->workaround_bo
.gem_handle
);
1341 anv_gem_close(device
, device
->trivial_batch_bo
.gem_handle
);
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
);
1347 anv_bo_cache_finish(&device
->bo_cache
);
1349 anv_bo_pool_finish(&device
->batch_bo_pool
);
1351 pthread_cond_destroy(&device
->queue_submit
);
1352 pthread_mutex_destroy(&device
->mutex
);
1354 anv_gem_destroy_context(device
, device
->context_id
);
1358 vk_free(&device
->alloc
, device
);
1361 VkResult
anv_EnumerateInstanceLayerProperties(
1362 uint32_t* pPropertyCount
,
1363 VkLayerProperties
* pProperties
)
1365 if (pProperties
== NULL
) {
1366 *pPropertyCount
= 0;
1370 /* None supported at this time */
1371 return vk_error(VK_ERROR_LAYER_NOT_PRESENT
);
1374 VkResult
anv_EnumerateDeviceLayerProperties(
1375 VkPhysicalDevice physicalDevice
,
1376 uint32_t* pPropertyCount
,
1377 VkLayerProperties
* pProperties
)
1379 if (pProperties
== NULL
) {
1380 *pPropertyCount
= 0;
1384 /* None supported at this time */
1385 return vk_error(VK_ERROR_LAYER_NOT_PRESENT
);
1388 void anv_GetDeviceQueue(
1390 uint32_t queueNodeIndex
,
1391 uint32_t queueIndex
,
1394 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1396 assert(queueIndex
== 0);
1398 *pQueue
= anv_queue_to_handle(&device
->queue
);
1402 anv_device_query_status(struct anv_device
*device
)
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
1408 if (unlikely(device
->lost
))
1409 return VK_ERROR_DEVICE_LOST
;
1411 uint32_t active
, pending
;
1412 int ret
= anv_gem_gpu_get_reset_stats(device
, &active
, &pending
);
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");
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");
1434 anv_device_bo_busy(struct anv_device
*device
, struct anv_bo
*bo
)
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.
1440 int ret
= anv_gem_busy(device
, bo
->gem_handle
);
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");
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.
1456 return anv_device_query_status(device
);
1460 anv_device_wait(struct anv_device
*device
, struct anv_bo
*bo
,
1463 int ret
= anv_gem_wait(device
, bo
->gem_handle
, &timeout
);
1464 if (ret
== -1 && errno
== ETIME
) {
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");
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.
1478 return anv_device_query_status(device
);
1481 VkResult
anv_DeviceWaitIdle(
1484 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1485 if (unlikely(device
->lost
))
1486 return VK_ERROR_DEVICE_LOST
;
1488 struct anv_batch batch
;
1491 batch
.start
= batch
.next
= cmds
;
1492 batch
.end
= (void *) cmds
+ sizeof(cmds
);
1494 anv_batch_emit(&batch
, GEN7_MI_BATCH_BUFFER_END
, bbe
);
1495 anv_batch_emit(&batch
, GEN7_MI_NOOP
, noop
);
1497 return anv_device_submit_simple_batch(device
, &batch
);
1501 anv_bo_init_new(struct anv_bo
*bo
, struct anv_device
*device
, uint64_t size
)
1503 uint32_t gem_handle
= anv_gem_create(device
, size
);
1505 return vk_error(VK_ERROR_OUT_OF_DEVICE_MEMORY
);
1507 anv_bo_init(bo
, gem_handle
, size
);
1512 VkResult
anv_AllocateMemory(
1514 const VkMemoryAllocateInfo
* pAllocateInfo
,
1515 const VkAllocationCallbacks
* pAllocator
,
1516 VkDeviceMemory
* pMem
)
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
;
1523 assert(pAllocateInfo
->sType
== VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO
);
1525 /* The Vulkan 1.0.33 spec says "allocationSize must be greater than 0". */
1526 assert(pAllocateInfo
->allocationSize
> 0);
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:
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."
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.
1545 if (pAllocateInfo
->allocationSize
> (1ull << 31))
1546 return VK_ERROR_OUT_OF_DEVICE_MEMORY
;
1548 /* FINISHME: Fail if allocation request exceeds heap size. */
1550 mem
= vk_alloc2(&device
->alloc
, pAllocator
, sizeof(*mem
), 8,
1551 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
1553 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
1555 assert(pAllocateInfo
->memoryTypeIndex
< pdevice
->memory
.type_count
);
1556 mem
->type
= &pdevice
->memory
.types
[pAllocateInfo
->memoryTypeIndex
];
1560 const VkImportMemoryFdInfoKHR
*fd_info
=
1561 vk_find_struct_const(pAllocateInfo
->pNext
, IMPORT_MEMORY_FD_INFO_KHR
);
1563 /* The Vulkan spec permits handleType to be 0, in which case the struct is
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
);
1573 result
= anv_bo_cache_import(device
, &device
->bo_cache
,
1574 fd_info
->fd
, &mem
->bo
);
1575 if (result
!= VK_SUCCESS
)
1578 VkDeviceSize aligned_alloc_size
=
1579 align_u64(pAllocateInfo
->allocationSize
, 4096);
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.
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
);
1600 /* From the Vulkan spec:
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."
1607 * If the import fails, we leave the file descriptor open.
1611 result
= anv_bo_cache_alloc(device
, &device
->bo_cache
,
1612 pAllocateInfo
->allocationSize
,
1614 if (result
!= VK_SUCCESS
)
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
);
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
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.
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
,
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");
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
;
1656 if (pdevice
->has_exec_async
)
1657 mem
->bo
->flags
|= EXEC_OBJECT_ASYNC
;
1659 *pMem
= anv_device_memory_to_handle(mem
);
1664 vk_free2(&device
->alloc
, pAllocator
, mem
);
1669 VkResult
anv_GetMemoryFdKHR(
1671 const VkMemoryGetFdInfoKHR
* pGetFdInfo
,
1674 ANV_FROM_HANDLE(anv_device
, dev
, device_h
);
1675 ANV_FROM_HANDLE(anv_device_memory
, mem
, pGetFdInfo
->memory
);
1677 assert(pGetFdInfo
->sType
== VK_STRUCTURE_TYPE_MEMORY_GET_FD_INFO_KHR
);
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
);
1682 return anv_bo_cache_export(dev
, &dev
->bo_cache
, mem
->bo
, pFd
);
1685 VkResult
anv_GetMemoryFdPropertiesKHR(
1687 VkExternalMemoryHandleTypeFlagBitsKHR handleType
,
1689 VkMemoryFdPropertiesKHR
* pMemoryFdProperties
)
1691 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1692 struct anv_physical_device
*pdevice
= &device
->instance
->physicalDevice
;
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;
1702 /* The valid usage section for this function says:
1704 * "handleType must not be one of the handle types defined as
1707 * So opaque handle types fall into the default "unsupported" case.
1709 return vk_error(VK_ERROR_INVALID_EXTERNAL_HANDLE_KHR
);
1713 void anv_FreeMemory(
1715 VkDeviceMemory _mem
,
1716 const VkAllocationCallbacks
* pAllocator
)
1718 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1719 ANV_FROM_HANDLE(anv_device_memory
, mem
, _mem
);
1725 anv_UnmapMemory(_device
, _mem
);
1727 anv_bo_cache_release(device
, &device
->bo_cache
, mem
->bo
);
1729 vk_free2(&device
->alloc
, pAllocator
, mem
);
1732 VkResult
anv_MapMemory(
1734 VkDeviceMemory _memory
,
1735 VkDeviceSize offset
,
1737 VkMemoryMapFlags flags
,
1740 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1741 ANV_FROM_HANDLE(anv_device_memory
, mem
, _memory
);
1748 if (size
== VK_WHOLE_SIZE
)
1749 size
= mem
->bo
->size
- offset
;
1751 /* From the Vulkan spec version 1.0.32 docs for MapMemory:
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
1759 assert(offset
+ size
<= mem
->bo
->size
);
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
1767 uint32_t gem_flags
= 0;
1769 if (!device
->info
.has_llc
&&
1770 (mem
->type
->propertyFlags
& VK_MEMORY_PROPERTY_HOST_COHERENT_BIT
))
1771 gem_flags
|= I915_MMAP_WC
;
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
;
1778 /* Let's map whole pages */
1779 map_size
= align_u64(map_size
, 4096);
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
);
1787 mem
->map_size
= map_size
;
1789 *ppData
= mem
->map
+ (offset
- map_offset
);
1794 void anv_UnmapMemory(
1796 VkDeviceMemory _memory
)
1798 ANV_FROM_HANDLE(anv_device_memory
, mem
, _memory
);
1803 anv_gem_munmap(mem
->map
, mem
->map_size
);
1810 clflush_mapped_ranges(struct anv_device
*device
,
1812 const VkMappedMemoryRange
*ranges
)
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
)
1819 gen_clflush_range(mem
->map
+ ranges
[i
].offset
,
1820 MIN2(ranges
[i
].size
, mem
->map_size
- ranges
[i
].offset
));
1824 VkResult
anv_FlushMappedMemoryRanges(
1826 uint32_t memoryRangeCount
,
1827 const VkMappedMemoryRange
* pMemoryRanges
)
1829 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1831 if (device
->info
.has_llc
)
1834 /* Make sure the writes we're flushing have landed. */
1835 __builtin_ia32_mfence();
1837 clflush_mapped_ranges(device
, memoryRangeCount
, pMemoryRanges
);
1842 VkResult
anv_InvalidateMappedMemoryRanges(
1844 uint32_t memoryRangeCount
,
1845 const VkMappedMemoryRange
* pMemoryRanges
)
1847 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1849 if (device
->info
.has_llc
)
1852 clflush_mapped_ranges(device
, memoryRangeCount
, pMemoryRanges
);
1854 /* Make sure no reads get moved up above the invalidate. */
1855 __builtin_ia32_mfence();
1860 void anv_GetBufferMemoryRequirements(
1863 VkMemoryRequirements
* pMemoryRequirements
)
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
;
1869 /* The Vulkan spec (git aaed022) says:
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.
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
);
1883 pMemoryRequirements
->size
= buffer
->size
;
1884 pMemoryRequirements
->alignment
= 16;
1885 pMemoryRequirements
->memoryTypeBits
= memory_types
;
1888 void anv_GetBufferMemoryRequirements2KHR(
1890 const VkBufferMemoryRequirementsInfo2KHR
* pInfo
,
1891 VkMemoryRequirements2KHR
* pMemoryRequirements
)
1893 anv_GetBufferMemoryRequirements(_device
, pInfo
->buffer
,
1894 &pMemoryRequirements
->memoryRequirements
);
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
;
1906 anv_debug_ignored_stype(ext
->sType
);
1912 void anv_GetImageMemoryRequirements(
1915 VkMemoryRequirements
* pMemoryRequirements
)
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
;
1921 /* The Vulkan spec (git aaed022) says:
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.
1928 * All types are currently supported for images.
1930 uint32_t memory_types
= (1ull << pdevice
->memory
.type_count
) - 1;
1932 pMemoryRequirements
->size
= image
->size
;
1933 pMemoryRequirements
->alignment
= image
->alignment
;
1934 pMemoryRequirements
->memoryTypeBits
= memory_types
;
1937 void anv_GetImageMemoryRequirements2KHR(
1939 const VkImageMemoryRequirementsInfo2KHR
* pInfo
,
1940 VkMemoryRequirements2KHR
* pMemoryRequirements
)
1942 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1943 ANV_FROM_HANDLE(anv_image
, image
, pInfo
->image
);
1945 anv_GetImageMemoryRequirements(_device
, pInfo
->image
,
1946 &pMemoryRequirements
->memoryRequirements
);
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
);
1957 assert(image
->planes
[plane
].offset
== 0);
1959 /* The Vulkan spec (git aaed022) says:
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.
1967 * All types are currently supported for images.
1969 pMemoryRequirements
->memoryRequirements
.memoryTypeBits
=
1970 (1ull << pdevice
->memory
.type_count
) - 1;
1972 pMemoryRequirements
->memoryRequirements
.size
= image
->planes
[plane
].size
;
1973 pMemoryRequirements
->memoryRequirements
.alignment
=
1974 image
->planes
[plane
].alignment
;
1979 anv_debug_ignored_stype(ext
->sType
);
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.
1991 * See also anv_AllocateMemory.
1993 requirements
->prefersDedicatedAllocation
= VK_TRUE
;
1994 requirements
->requiresDedicatedAllocation
= VK_TRUE
;
1996 requirements
->prefersDedicatedAllocation
= VK_FALSE
;
1997 requirements
->requiresDedicatedAllocation
= VK_FALSE
;
2003 anv_debug_ignored_stype(ext
->sType
);
2009 void anv_GetImageSparseMemoryRequirements(
2012 uint32_t* pSparseMemoryRequirementCount
,
2013 VkSparseImageMemoryRequirements
* pSparseMemoryRequirements
)
2015 *pSparseMemoryRequirementCount
= 0;
2018 void anv_GetImageSparseMemoryRequirements2KHR(
2020 const VkImageSparseMemoryRequirementsInfo2KHR
* pInfo
,
2021 uint32_t* pSparseMemoryRequirementCount
,
2022 VkSparseImageMemoryRequirements2KHR
* pSparseMemoryRequirements
)
2024 *pSparseMemoryRequirementCount
= 0;
2027 void anv_GetDeviceMemoryCommitment(
2029 VkDeviceMemory memory
,
2030 VkDeviceSize
* pCommittedMemoryInBytes
)
2032 *pCommittedMemoryInBytes
= 0;
2036 anv_bind_buffer_memory(const VkBindBufferMemoryInfoKHR
*pBindInfo
)
2038 ANV_FROM_HANDLE(anv_device_memory
, mem
, pBindInfo
->memory
);
2039 ANV_FROM_HANDLE(anv_buffer
, buffer
, pBindInfo
->buffer
);
2041 assert(pBindInfo
->sType
== VK_STRUCTURE_TYPE_BIND_BUFFER_MEMORY_INFO_KHR
);
2044 assert((buffer
->usage
& mem
->type
->valid_buffer_usage
) == buffer
->usage
);
2045 buffer
->bo
= mem
->bo
;
2046 buffer
->offset
= pBindInfo
->memoryOffset
;
2053 VkResult
anv_BindBufferMemory(
2056 VkDeviceMemory memory
,
2057 VkDeviceSize memoryOffset
)
2059 anv_bind_buffer_memory(
2060 &(VkBindBufferMemoryInfoKHR
) {
2061 .sType
= VK_STRUCTURE_TYPE_BIND_BUFFER_MEMORY_INFO_KHR
,
2064 .memoryOffset
= memoryOffset
,
2070 VkResult
anv_BindBufferMemory2KHR(
2072 uint32_t bindInfoCount
,
2073 const VkBindBufferMemoryInfoKHR
* pBindInfos
)
2075 for (uint32_t i
= 0; i
< bindInfoCount
; i
++)
2076 anv_bind_buffer_memory(&pBindInfos
[i
]);
2081 VkResult
anv_QueueBindSparse(
2083 uint32_t bindInfoCount
,
2084 const VkBindSparseInfo
* pBindInfo
,
2087 ANV_FROM_HANDLE(anv_queue
, queue
, _queue
);
2088 if (unlikely(queue
->device
->lost
))
2089 return VK_ERROR_DEVICE_LOST
;
2091 return vk_error(VK_ERROR_FEATURE_NOT_PRESENT
);
2096 VkResult
anv_CreateEvent(
2098 const VkEventCreateInfo
* pCreateInfo
,
2099 const VkAllocationCallbacks
* pAllocator
,
2102 ANV_FROM_HANDLE(anv_device
, device
, _device
);
2103 struct anv_state state
;
2104 struct anv_event
*event
;
2106 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_EVENT_CREATE_INFO
);
2108 state
= anv_state_pool_alloc(&device
->dynamic_state_pool
,
2111 event
->state
= state
;
2112 event
->semaphore
= VK_EVENT_RESET
;
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
);
2120 *pEvent
= anv_event_to_handle(event
);
2125 void anv_DestroyEvent(
2128 const VkAllocationCallbacks
* pAllocator
)
2130 ANV_FROM_HANDLE(anv_device
, device
, _device
);
2131 ANV_FROM_HANDLE(anv_event
, event
, _event
);
2136 anv_state_pool_free(&device
->dynamic_state_pool
, event
->state
);
2139 VkResult
anv_GetEventStatus(
2143 ANV_FROM_HANDLE(anv_device
, device
, _device
);
2144 ANV_FROM_HANDLE(anv_event
, event
, _event
);
2146 if (unlikely(device
->lost
))
2147 return VK_ERROR_DEVICE_LOST
;
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();
2156 return event
->semaphore
;
2159 VkResult
anv_SetEvent(
2163 ANV_FROM_HANDLE(anv_device
, device
, _device
);
2164 ANV_FROM_HANDLE(anv_event
, event
, _event
);
2166 event
->semaphore
= VK_EVENT_SET
;
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
);
2177 VkResult
anv_ResetEvent(
2181 ANV_FROM_HANDLE(anv_device
, device
, _device
);
2182 ANV_FROM_HANDLE(anv_event
, event
, _event
);
2184 event
->semaphore
= VK_EVENT_RESET
;
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
);
2197 VkResult
anv_CreateBuffer(
2199 const VkBufferCreateInfo
* pCreateInfo
,
2200 const VkAllocationCallbacks
* pAllocator
,
2203 ANV_FROM_HANDLE(anv_device
, device
, _device
);
2204 struct anv_buffer
*buffer
;
2206 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO
);
2208 buffer
= vk_alloc2(&device
->alloc
, pAllocator
, sizeof(*buffer
), 8,
2209 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
2211 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
2213 buffer
->size
= pCreateInfo
->size
;
2214 buffer
->usage
= pCreateInfo
->usage
;
2218 *pBuffer
= anv_buffer_to_handle(buffer
);
2223 void anv_DestroyBuffer(
2226 const VkAllocationCallbacks
* pAllocator
)
2228 ANV_FROM_HANDLE(anv_device
, device
, _device
);
2229 ANV_FROM_HANDLE(anv_buffer
, buffer
, _buffer
);
2234 vk_free2(&device
->alloc
, pAllocator
, buffer
);
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
)
2242 isl_buffer_fill_state(&device
->isl_dev
, state
.map
,
2244 .mocs
= device
->default_mocs
,
2249 anv_state_flush(device
, state
);
2252 void anv_DestroySampler(
2255 const VkAllocationCallbacks
* pAllocator
)
2257 ANV_FROM_HANDLE(anv_device
, device
, _device
);
2258 ANV_FROM_HANDLE(anv_sampler
, sampler
, _sampler
);
2263 vk_free2(&device
->alloc
, pAllocator
, sampler
);
2266 VkResult
anv_CreateFramebuffer(
2268 const VkFramebufferCreateInfo
* pCreateInfo
,
2269 const VkAllocationCallbacks
* pAllocator
,
2270 VkFramebuffer
* pFramebuffer
)
2272 ANV_FROM_HANDLE(anv_device
, device
, _device
);
2273 struct anv_framebuffer
*framebuffer
;
2275 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO
);
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
);
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
);
2290 framebuffer
->width
= pCreateInfo
->width
;
2291 framebuffer
->height
= pCreateInfo
->height
;
2292 framebuffer
->layers
= pCreateInfo
->layers
;
2294 *pFramebuffer
= anv_framebuffer_to_handle(framebuffer
);
2299 void anv_DestroyFramebuffer(
2302 const VkAllocationCallbacks
* pAllocator
)
2304 ANV_FROM_HANDLE(anv_device
, device
, _device
);
2305 ANV_FROM_HANDLE(anv_framebuffer
, fb
, _fb
);
2310 vk_free2(&device
->alloc
, pAllocator
, fb
);
2313 /* vk_icd.h does not declare this function, so we declare it here to
2314 * suppress Wmissing-prototypes.
2316 PUBLIC VKAPI_ATTR VkResult VKAPI_CALL
2317 vk_icdNegotiateLoaderICDInterfaceVersion(uint32_t* pSupportedVersion
);
2319 PUBLIC VKAPI_ATTR VkResult VKAPI_CALL
2320 vk_icdNegotiateLoaderICDInterfaceVersion(uint32_t* pSupportedVersion
)
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.
2327 * - Loader interface v0 is incompatible with later versions. We don't
2330 * - In loader interface v1:
2331 * - The first ICD entrypoint called by the loader is
2332 * vk_icdGetInstanceProcAddr(). The ICD must statically expose this
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.
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.
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.
2353 *pSupportedVersion
= MIN2(*pSupportedVersion
, 3u);