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-uapi/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/disk_cache.h"
39 #include "util/mesa-sha1.h"
40 #include "util/u_string.h"
43 #include "common/gen_defines.h"
44 #include "compiler/glsl_types.h"
46 #include "genxml/gen7_pack.h"
48 /* This is probably far to big but it reflects the max size used for messages
49 * in OpenGLs KHR_debug.
51 #define MAX_DEBUG_MESSAGE_LENGTH 4096
54 compiler_debug_log(void *data
, const char *fmt
, ...)
56 char str
[MAX_DEBUG_MESSAGE_LENGTH
];
57 struct anv_device
*device
= (struct anv_device
*)data
;
59 if (list_empty(&device
->instance
->debug_report_callbacks
.callbacks
))
64 (void) vsnprintf(str
, MAX_DEBUG_MESSAGE_LENGTH
, fmt
, args
);
67 vk_debug_report(&device
->instance
->debug_report_callbacks
,
68 VK_DEBUG_REPORT_DEBUG_BIT_EXT
,
69 VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT
,
74 compiler_perf_log(void *data
, const char *fmt
, ...)
79 if (unlikely(INTEL_DEBUG
& DEBUG_PERF
))
80 intel_logd_v(fmt
, args
);
86 anv_compute_heap_size(int fd
, uint64_t gtt_size
)
88 /* Query the total ram from the system */
92 uint64_t total_ram
= (uint64_t)info
.totalram
* (uint64_t)info
.mem_unit
;
94 /* We don't want to burn too much ram with the GPU. If the user has 4GiB
95 * or less, we use at most half. If they have more than 4GiB, we use 3/4.
97 uint64_t available_ram
;
98 if (total_ram
<= 4ull * 1024ull * 1024ull * 1024ull)
99 available_ram
= total_ram
/ 2;
101 available_ram
= total_ram
* 3 / 4;
103 /* We also want to leave some padding for things we allocate in the driver,
104 * so don't go over 3/4 of the GTT either.
106 uint64_t available_gtt
= gtt_size
* 3 / 4;
108 return MIN2(available_ram
, available_gtt
);
112 anv_physical_device_init_heaps(struct anv_physical_device
*device
, int fd
)
115 if (anv_gem_get_context_param(fd
, 0, I915_CONTEXT_PARAM_GTT_SIZE
,
117 /* If, for whatever reason, we can't actually get the GTT size from the
118 * kernel (too old?) fall back to the aperture size.
120 anv_perf_warn(NULL
, NULL
,
121 "Failed to get I915_CONTEXT_PARAM_GTT_SIZE: %m");
123 if (anv_gem_get_aperture(fd
, >t_size
) == -1) {
124 return vk_errorf(NULL
, NULL
, VK_ERROR_INITIALIZATION_FAILED
,
125 "failed to get aperture size: %m");
129 device
->supports_48bit_addresses
= (device
->info
.gen
>= 8) &&
130 gtt_size
> (4ULL << 30 /* GiB */);
132 uint64_t heap_size
= anv_compute_heap_size(fd
, gtt_size
);
134 if (heap_size
> (2ull << 30) && !device
->supports_48bit_addresses
) {
135 /* When running with an overridden PCI ID, we may get a GTT size from
136 * the kernel that is greater than 2 GiB but the execbuf check for 48bit
137 * address support can still fail. Just clamp the address space size to
138 * 2 GiB if we don't have 48-bit support.
140 intel_logw("%s:%d: The kernel reported a GTT size larger than 2 GiB but "
141 "not support for 48-bit addresses",
143 heap_size
= 2ull << 30;
146 if (heap_size
<= 3ull * (1ull << 30)) {
147 /* In this case, everything fits nicely into the 32-bit address space,
148 * so there's no need for supporting 48bit addresses on client-allocated
151 device
->memory
.heap_count
= 1;
152 device
->memory
.heaps
[0] = (struct anv_memory_heap
) {
153 .vma_start
= LOW_HEAP_MIN_ADDRESS
,
154 .vma_size
= LOW_HEAP_SIZE
,
156 .flags
= VK_MEMORY_HEAP_DEVICE_LOCAL_BIT
,
157 .supports_48bit_addresses
= false,
160 /* Not everything will fit nicely into a 32-bit address space. In this
161 * case we need a 64-bit heap. Advertise a small 32-bit heap and a
162 * larger 48-bit heap. If we're in this case, then we have a total heap
163 * size larger than 3GiB which most likely means they have 8 GiB of
164 * video memory and so carving off 1 GiB for the 32-bit heap should be
167 const uint64_t heap_size_32bit
= 1ull << 30;
168 const uint64_t heap_size_48bit
= heap_size
- heap_size_32bit
;
170 assert(device
->supports_48bit_addresses
);
172 device
->memory
.heap_count
= 2;
173 device
->memory
.heaps
[0] = (struct anv_memory_heap
) {
174 .vma_start
= HIGH_HEAP_MIN_ADDRESS
,
175 /* Leave the last 4GiB out of the high vma range, so that no state
176 * base address + size can overflow 48 bits. For more information see
177 * the comment about Wa32bitGeneralStateOffset in anv_allocator.c
179 .vma_size
= gtt_size
- (1ull << 32) - HIGH_HEAP_MIN_ADDRESS
,
180 .size
= heap_size_48bit
,
181 .flags
= VK_MEMORY_HEAP_DEVICE_LOCAL_BIT
,
182 .supports_48bit_addresses
= true,
184 device
->memory
.heaps
[1] = (struct anv_memory_heap
) {
185 .vma_start
= LOW_HEAP_MIN_ADDRESS
,
186 .vma_size
= LOW_HEAP_SIZE
,
187 .size
= heap_size_32bit
,
188 .flags
= VK_MEMORY_HEAP_DEVICE_LOCAL_BIT
,
189 .supports_48bit_addresses
= false,
193 uint32_t type_count
= 0;
194 for (uint32_t heap
= 0; heap
< device
->memory
.heap_count
; heap
++) {
195 uint32_t valid_buffer_usage
= ~0;
197 /* There appears to be a hardware issue in the VF cache where it only
198 * considers the bottom 32 bits of memory addresses. If you happen to
199 * have two vertex buffers which get placed exactly 4 GiB apart and use
200 * them in back-to-back draw calls, you can get collisions. In order to
201 * solve this problem, we require vertex and index buffers be bound to
202 * memory allocated out of the 32-bit heap.
204 if (device
->memory
.heaps
[heap
].supports_48bit_addresses
) {
205 valid_buffer_usage
&= ~(VK_BUFFER_USAGE_INDEX_BUFFER_BIT
|
206 VK_BUFFER_USAGE_VERTEX_BUFFER_BIT
);
209 if (device
->info
.has_llc
) {
210 /* Big core GPUs share LLC with the CPU and thus one memory type can be
211 * both cached and coherent at the same time.
213 device
->memory
.types
[type_count
++] = (struct anv_memory_type
) {
214 .propertyFlags
= VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT
|
215 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT
|
216 VK_MEMORY_PROPERTY_HOST_COHERENT_BIT
|
217 VK_MEMORY_PROPERTY_HOST_CACHED_BIT
,
219 .valid_buffer_usage
= valid_buffer_usage
,
222 /* The spec requires that we expose a host-visible, coherent memory
223 * type, but Atom GPUs don't share LLC. Thus we offer two memory types
224 * to give the application a choice between cached, but not coherent and
225 * coherent but uncached (WC though).
227 device
->memory
.types
[type_count
++] = (struct anv_memory_type
) {
228 .propertyFlags
= VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT
|
229 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT
|
230 VK_MEMORY_PROPERTY_HOST_COHERENT_BIT
,
232 .valid_buffer_usage
= valid_buffer_usage
,
234 device
->memory
.types
[type_count
++] = (struct anv_memory_type
) {
235 .propertyFlags
= VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT
|
236 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT
|
237 VK_MEMORY_PROPERTY_HOST_CACHED_BIT
,
239 .valid_buffer_usage
= valid_buffer_usage
,
243 device
->memory
.type_count
= type_count
;
249 anv_physical_device_init_uuids(struct anv_physical_device
*device
)
251 const struct build_id_note
*note
=
252 build_id_find_nhdr_for_addr(anv_physical_device_init_uuids
);
254 return vk_errorf(device
->instance
, device
,
255 VK_ERROR_INITIALIZATION_FAILED
,
256 "Failed to find build-id");
259 unsigned build_id_len
= build_id_length(note
);
260 if (build_id_len
< 20) {
261 return vk_errorf(device
->instance
, device
,
262 VK_ERROR_INITIALIZATION_FAILED
,
263 "build-id too short. It needs to be a SHA");
266 memcpy(device
->driver_build_sha1
, build_id_data(note
), 20);
268 struct mesa_sha1 sha1_ctx
;
270 STATIC_ASSERT(VK_UUID_SIZE
<= sizeof(sha1
));
272 /* The pipeline cache UUID is used for determining when a pipeline cache is
273 * invalid. It needs both a driver build and the PCI ID of the device.
275 _mesa_sha1_init(&sha1_ctx
);
276 _mesa_sha1_update(&sha1_ctx
, build_id_data(note
), build_id_len
);
277 _mesa_sha1_update(&sha1_ctx
, &device
->chipset_id
,
278 sizeof(device
->chipset_id
));
279 _mesa_sha1_update(&sha1_ctx
, &device
->always_use_bindless
,
280 sizeof(device
->always_use_bindless
));
281 _mesa_sha1_final(&sha1_ctx
, sha1
);
282 memcpy(device
->pipeline_cache_uuid
, sha1
, VK_UUID_SIZE
);
284 /* The driver UUID is used for determining sharability of images and memory
285 * between two Vulkan instances in separate processes. People who want to
286 * share memory need to also check the device UUID (below) so all this
287 * needs to be is the build-id.
289 memcpy(device
->driver_uuid
, build_id_data(note
), VK_UUID_SIZE
);
291 /* The device UUID uniquely identifies the given device within the machine.
292 * Since we never have more than one device, this doesn't need to be a real
293 * UUID. However, on the off-chance that someone tries to use this to
294 * cache pre-tiled images or something of the like, we use the PCI ID and
295 * some bits of ISL info to ensure that this is safe.
297 _mesa_sha1_init(&sha1_ctx
);
298 _mesa_sha1_update(&sha1_ctx
, &device
->chipset_id
,
299 sizeof(device
->chipset_id
));
300 _mesa_sha1_update(&sha1_ctx
, &device
->isl_dev
.has_bit6_swizzling
,
301 sizeof(device
->isl_dev
.has_bit6_swizzling
));
302 _mesa_sha1_final(&sha1_ctx
, sha1
);
303 memcpy(device
->device_uuid
, sha1
, VK_UUID_SIZE
);
309 anv_physical_device_init_disk_cache(struct anv_physical_device
*device
)
311 #ifdef ENABLE_SHADER_CACHE
313 MAYBE_UNUSED
int len
= snprintf(renderer
, sizeof(renderer
), "anv_%04x",
315 assert(len
== sizeof(renderer
) - 2);
318 _mesa_sha1_format(timestamp
, device
->driver_build_sha1
);
320 const uint64_t driver_flags
=
321 brw_get_compiler_config_value(device
->compiler
);
322 device
->disk_cache
= disk_cache_create(renderer
, timestamp
, driver_flags
);
324 device
->disk_cache
= NULL
;
329 anv_physical_device_free_disk_cache(struct anv_physical_device
*device
)
331 #ifdef ENABLE_SHADER_CACHE
332 if (device
->disk_cache
)
333 disk_cache_destroy(device
->disk_cache
);
335 assert(device
->disk_cache
== NULL
);
340 anv_physical_device_init(struct anv_physical_device
*device
,
341 struct anv_instance
*instance
,
342 drmDevicePtr drm_device
)
344 const char *primary_path
= drm_device
->nodes
[DRM_NODE_PRIMARY
];
345 const char *path
= drm_device
->nodes
[DRM_NODE_RENDER
];
350 brw_process_intel_debug_variable();
352 fd
= open(path
, O_RDWR
| O_CLOEXEC
);
354 return vk_error(VK_ERROR_INCOMPATIBLE_DRIVER
);
356 device
->_loader_data
.loaderMagic
= ICD_LOADER_MAGIC
;
357 device
->instance
= instance
;
359 assert(strlen(path
) < ARRAY_SIZE(device
->path
));
360 snprintf(device
->path
, ARRAY_SIZE(device
->path
), "%s", path
);
362 device
->no_hw
= getenv("INTEL_NO_HW") != NULL
;
364 const int pci_id_override
= gen_get_pci_device_id_override();
365 if (pci_id_override
< 0) {
366 device
->chipset_id
= anv_gem_get_param(fd
, I915_PARAM_CHIPSET_ID
);
367 if (!device
->chipset_id
) {
368 result
= vk_error(VK_ERROR_INCOMPATIBLE_DRIVER
);
372 device
->chipset_id
= pci_id_override
;
373 device
->no_hw
= true;
376 device
->pci_info
.domain
= drm_device
->businfo
.pci
->domain
;
377 device
->pci_info
.bus
= drm_device
->businfo
.pci
->bus
;
378 device
->pci_info
.device
= drm_device
->businfo
.pci
->dev
;
379 device
->pci_info
.function
= drm_device
->businfo
.pci
->func
;
381 device
->name
= gen_get_device_name(device
->chipset_id
);
382 if (!gen_get_device_info(device
->chipset_id
, &device
->info
)) {
383 result
= vk_error(VK_ERROR_INCOMPATIBLE_DRIVER
);
387 if (device
->info
.is_haswell
) {
388 intel_logw("Haswell Vulkan support is incomplete");
389 } else if (device
->info
.gen
== 7 && !device
->info
.is_baytrail
) {
390 intel_logw("Ivy Bridge Vulkan support is incomplete");
391 } else if (device
->info
.gen
== 7 && device
->info
.is_baytrail
) {
392 intel_logw("Bay Trail Vulkan support is incomplete");
393 } else if (device
->info
.gen
>= 8 && device
->info
.gen
<= 10) {
394 /* Gen8-10 fully supported */
395 } else if (device
->info
.gen
== 11) {
396 intel_logw("Vulkan is not yet fully supported on gen11.");
398 result
= vk_errorf(device
->instance
, device
,
399 VK_ERROR_INCOMPATIBLE_DRIVER
,
400 "Vulkan not yet supported on %s", device
->name
);
404 device
->cmd_parser_version
= -1;
405 if (device
->info
.gen
== 7) {
406 device
->cmd_parser_version
=
407 anv_gem_get_param(fd
, I915_PARAM_CMD_PARSER_VERSION
);
408 if (device
->cmd_parser_version
== -1) {
409 result
= vk_errorf(device
->instance
, device
,
410 VK_ERROR_INITIALIZATION_FAILED
,
411 "failed to get command parser version");
416 if (!anv_gem_get_param(fd
, I915_PARAM_HAS_WAIT_TIMEOUT
)) {
417 result
= vk_errorf(device
->instance
, device
,
418 VK_ERROR_INITIALIZATION_FAILED
,
419 "kernel missing gem wait");
423 if (!anv_gem_get_param(fd
, I915_PARAM_HAS_EXECBUF2
)) {
424 result
= vk_errorf(device
->instance
, device
,
425 VK_ERROR_INITIALIZATION_FAILED
,
426 "kernel missing execbuf2");
430 if (!device
->info
.has_llc
&&
431 anv_gem_get_param(fd
, I915_PARAM_MMAP_VERSION
) < 1) {
432 result
= vk_errorf(device
->instance
, device
,
433 VK_ERROR_INITIALIZATION_FAILED
,
434 "kernel missing wc mmap");
438 result
= anv_physical_device_init_heaps(device
, fd
);
439 if (result
!= VK_SUCCESS
)
442 device
->has_exec_async
= anv_gem_get_param(fd
, I915_PARAM_HAS_EXEC_ASYNC
);
443 device
->has_exec_capture
= anv_gem_get_param(fd
, I915_PARAM_HAS_EXEC_CAPTURE
);
444 device
->has_exec_fence
= anv_gem_get_param(fd
, I915_PARAM_HAS_EXEC_FENCE
);
445 device
->has_syncobj
= anv_gem_get_param(fd
, I915_PARAM_HAS_EXEC_FENCE_ARRAY
);
446 device
->has_syncobj_wait
= device
->has_syncobj
&&
447 anv_gem_supports_syncobj_wait(fd
);
448 device
->has_context_priority
= anv_gem_has_context_priority(fd
);
450 device
->use_softpin
= anv_gem_get_param(fd
, I915_PARAM_HAS_EXEC_SOFTPIN
)
451 && device
->supports_48bit_addresses
;
453 device
->has_context_isolation
=
454 anv_gem_get_param(fd
, I915_PARAM_HAS_CONTEXT_ISOLATION
);
456 device
->always_use_bindless
=
457 env_var_as_boolean("ANV_ALWAYS_BINDLESS", false);
460 /* Starting with Gen10, the timestamp frequency of the command streamer may
461 * vary from one part to another. We can query the value from the kernel.
463 if (device
->info
.gen
>= 10) {
464 int timestamp_frequency
=
465 anv_gem_get_param(fd
, I915_PARAM_CS_TIMESTAMP_FREQUENCY
);
467 if (timestamp_frequency
< 0)
468 intel_logw("Kernel 4.16-rc1+ required to properly query CS timestamp frequency");
470 device
->info
.timestamp_frequency
= timestamp_frequency
;
473 /* GENs prior to 8 do not support EU/Subslice info */
474 if (device
->info
.gen
>= 8) {
475 device
->subslice_total
= anv_gem_get_param(fd
, I915_PARAM_SUBSLICE_TOTAL
);
476 device
->eu_total
= anv_gem_get_param(fd
, I915_PARAM_EU_TOTAL
);
478 /* Without this information, we cannot get the right Braswell
479 * brandstrings, and we have to use conservative numbers for GPGPU on
480 * many platforms, but otherwise, things will just work.
482 if (device
->subslice_total
< 1 || device
->eu_total
< 1) {
483 intel_logw("Kernel 4.1 required to properly query GPU properties");
485 } else if (device
->info
.gen
== 7) {
486 device
->subslice_total
= 1 << (device
->info
.gt
- 1);
489 if (device
->info
.is_cherryview
&&
490 device
->subslice_total
> 0 && device
->eu_total
> 0) {
491 /* Logical CS threads = EUs per subslice * num threads per EU */
492 uint32_t max_cs_threads
=
493 device
->eu_total
/ device
->subslice_total
* device
->info
.num_thread_per_eu
;
495 /* Fuse configurations may give more threads than expected, never less. */
496 if (max_cs_threads
> device
->info
.max_cs_threads
)
497 device
->info
.max_cs_threads
= max_cs_threads
;
500 device
->compiler
= brw_compiler_create(NULL
, &device
->info
);
501 if (device
->compiler
== NULL
) {
502 result
= vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
505 device
->compiler
->shader_debug_log
= compiler_debug_log
;
506 device
->compiler
->shader_perf_log
= compiler_perf_log
;
507 device
->compiler
->supports_pull_constants
= false;
508 device
->compiler
->constant_buffer_0_is_relative
=
509 device
->info
.gen
< 8 || !device
->has_context_isolation
;
510 device
->compiler
->supports_shader_constants
= true;
512 /* Broadwell PRM says:
514 * "Before Gen8, there was a historical configuration control field to
515 * swizzle address bit[6] for in X/Y tiling modes. This was set in three
516 * different places: TILECTL[1:0], ARB_MODE[5:4], and
517 * DISP_ARB_CTL[14:13].
519 * For Gen8 and subsequent generations, the swizzle fields are all
520 * reserved, and the CPU's memory controller performs all address
521 * swizzling modifications."
524 device
->info
.gen
< 8 && anv_gem_get_bit6_swizzle(fd
, I915_TILING_X
);
526 isl_device_init(&device
->isl_dev
, &device
->info
, swizzled
);
528 result
= anv_physical_device_init_uuids(device
);
529 if (result
!= VK_SUCCESS
)
532 anv_physical_device_init_disk_cache(device
);
534 if (instance
->enabled_extensions
.KHR_display
) {
535 master_fd
= open(primary_path
, O_RDWR
| O_CLOEXEC
);
536 if (master_fd
>= 0) {
537 /* prod the device with a GETPARAM call which will fail if
538 * we don't have permission to even render on this device
540 if (anv_gem_get_param(master_fd
, I915_PARAM_CHIPSET_ID
) == 0) {
546 device
->master_fd
= master_fd
;
548 result
= anv_init_wsi(device
);
549 if (result
!= VK_SUCCESS
) {
550 ralloc_free(device
->compiler
);
551 anv_physical_device_free_disk_cache(device
);
555 anv_physical_device_get_supported_extensions(device
,
556 &device
->supported_extensions
);
559 device
->local_fd
= fd
;
571 anv_physical_device_finish(struct anv_physical_device
*device
)
573 anv_finish_wsi(device
);
574 anv_physical_device_free_disk_cache(device
);
575 ralloc_free(device
->compiler
);
576 close(device
->local_fd
);
577 if (device
->master_fd
>= 0)
578 close(device
->master_fd
);
582 default_alloc_func(void *pUserData
, size_t size
, size_t align
,
583 VkSystemAllocationScope allocationScope
)
589 default_realloc_func(void *pUserData
, void *pOriginal
, size_t size
,
590 size_t align
, VkSystemAllocationScope allocationScope
)
592 return realloc(pOriginal
, size
);
596 default_free_func(void *pUserData
, void *pMemory
)
601 static const VkAllocationCallbacks default_alloc
= {
603 .pfnAllocation
= default_alloc_func
,
604 .pfnReallocation
= default_realloc_func
,
605 .pfnFree
= default_free_func
,
608 VkResult
anv_EnumerateInstanceExtensionProperties(
609 const char* pLayerName
,
610 uint32_t* pPropertyCount
,
611 VkExtensionProperties
* pProperties
)
613 VK_OUTARRAY_MAKE(out
, pProperties
, pPropertyCount
);
615 for (int i
= 0; i
< ANV_INSTANCE_EXTENSION_COUNT
; i
++) {
616 if (anv_instance_extensions_supported
.extensions
[i
]) {
617 vk_outarray_append(&out
, prop
) {
618 *prop
= anv_instance_extensions
[i
];
623 return vk_outarray_status(&out
);
626 VkResult
anv_CreateInstance(
627 const VkInstanceCreateInfo
* pCreateInfo
,
628 const VkAllocationCallbacks
* pAllocator
,
629 VkInstance
* pInstance
)
631 struct anv_instance
*instance
;
634 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO
);
636 struct anv_instance_extension_table enabled_extensions
= {};
637 for (uint32_t i
= 0; i
< pCreateInfo
->enabledExtensionCount
; i
++) {
639 for (idx
= 0; idx
< ANV_INSTANCE_EXTENSION_COUNT
; idx
++) {
640 if (strcmp(pCreateInfo
->ppEnabledExtensionNames
[i
],
641 anv_instance_extensions
[idx
].extensionName
) == 0)
645 if (idx
>= ANV_INSTANCE_EXTENSION_COUNT
)
646 return vk_error(VK_ERROR_EXTENSION_NOT_PRESENT
);
648 if (!anv_instance_extensions_supported
.extensions
[idx
])
649 return vk_error(VK_ERROR_EXTENSION_NOT_PRESENT
);
651 enabled_extensions
.extensions
[idx
] = true;
654 instance
= vk_alloc2(&default_alloc
, pAllocator
, sizeof(*instance
), 8,
655 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE
);
657 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
659 instance
->_loader_data
.loaderMagic
= ICD_LOADER_MAGIC
;
662 instance
->alloc
= *pAllocator
;
664 instance
->alloc
= default_alloc
;
666 instance
->app_info
= (struct anv_app_info
) { .api_version
= 0 };
667 if (pCreateInfo
->pApplicationInfo
) {
668 const VkApplicationInfo
*app
= pCreateInfo
->pApplicationInfo
;
670 instance
->app_info
.app_name
=
671 vk_strdup(&instance
->alloc
, app
->pApplicationName
,
672 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE
);
673 instance
->app_info
.app_version
= app
->applicationVersion
;
675 instance
->app_info
.engine_name
=
676 vk_strdup(&instance
->alloc
, app
->pEngineName
,
677 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE
);
678 instance
->app_info
.engine_version
= app
->engineVersion
;
680 instance
->app_info
.api_version
= app
->apiVersion
;
683 if (instance
->app_info
.api_version
== 0)
684 instance
->app_info
.api_version
= VK_API_VERSION_1_0
;
686 instance
->enabled_extensions
= enabled_extensions
;
688 for (unsigned i
= 0; i
< ARRAY_SIZE(instance
->dispatch
.entrypoints
); i
++) {
689 /* Vulkan requires that entrypoints for extensions which have not been
690 * enabled must not be advertised.
692 if (!anv_instance_entrypoint_is_enabled(i
, instance
->app_info
.api_version
,
693 &instance
->enabled_extensions
)) {
694 instance
->dispatch
.entrypoints
[i
] = NULL
;
696 instance
->dispatch
.entrypoints
[i
] =
697 anv_instance_dispatch_table
.entrypoints
[i
];
701 for (unsigned i
= 0; i
< ARRAY_SIZE(instance
->device_dispatch
.entrypoints
); i
++) {
702 /* Vulkan requires that entrypoints for extensions which have not been
703 * enabled must not be advertised.
705 if (!anv_device_entrypoint_is_enabled(i
, instance
->app_info
.api_version
,
706 &instance
->enabled_extensions
, NULL
)) {
707 instance
->device_dispatch
.entrypoints
[i
] = NULL
;
709 instance
->device_dispatch
.entrypoints
[i
] =
710 anv_device_dispatch_table
.entrypoints
[i
];
714 instance
->physicalDeviceCount
= -1;
716 result
= vk_debug_report_instance_init(&instance
->debug_report_callbacks
);
717 if (result
!= VK_SUCCESS
) {
718 vk_free2(&default_alloc
, pAllocator
, instance
);
719 return vk_error(result
);
722 instance
->pipeline_cache_enabled
=
723 env_var_as_boolean("ANV_ENABLE_PIPELINE_CACHE", true);
726 glsl_type_singleton_init_or_ref();
728 VG(VALGRIND_CREATE_MEMPOOL(instance
, 0, false));
730 *pInstance
= anv_instance_to_handle(instance
);
735 void anv_DestroyInstance(
736 VkInstance _instance
,
737 const VkAllocationCallbacks
* pAllocator
)
739 ANV_FROM_HANDLE(anv_instance
, instance
, _instance
);
744 if (instance
->physicalDeviceCount
> 0) {
745 /* We support at most one physical device. */
746 assert(instance
->physicalDeviceCount
== 1);
747 anv_physical_device_finish(&instance
->physicalDevice
);
750 vk_free(&instance
->alloc
, (char *)instance
->app_info
.app_name
);
751 vk_free(&instance
->alloc
, (char *)instance
->app_info
.engine_name
);
753 VG(VALGRIND_DESTROY_MEMPOOL(instance
));
755 vk_debug_report_instance_destroy(&instance
->debug_report_callbacks
);
757 glsl_type_singleton_decref();
760 vk_free(&instance
->alloc
, instance
);
764 anv_enumerate_devices(struct anv_instance
*instance
)
766 /* TODO: Check for more devices ? */
767 drmDevicePtr devices
[8];
768 VkResult result
= VK_ERROR_INCOMPATIBLE_DRIVER
;
771 instance
->physicalDeviceCount
= 0;
773 max_devices
= drmGetDevices2(0, devices
, ARRAY_SIZE(devices
));
775 return VK_ERROR_INCOMPATIBLE_DRIVER
;
777 for (unsigned i
= 0; i
< (unsigned)max_devices
; i
++) {
778 if (devices
[i
]->available_nodes
& 1 << DRM_NODE_RENDER
&&
779 devices
[i
]->bustype
== DRM_BUS_PCI
&&
780 devices
[i
]->deviceinfo
.pci
->vendor_id
== 0x8086) {
782 result
= anv_physical_device_init(&instance
->physicalDevice
,
783 instance
, devices
[i
]);
784 if (result
!= VK_ERROR_INCOMPATIBLE_DRIVER
)
788 drmFreeDevices(devices
, max_devices
);
790 if (result
== VK_SUCCESS
)
791 instance
->physicalDeviceCount
= 1;
797 anv_instance_ensure_physical_device(struct anv_instance
*instance
)
799 if (instance
->physicalDeviceCount
< 0) {
800 VkResult result
= anv_enumerate_devices(instance
);
801 if (result
!= VK_SUCCESS
&&
802 result
!= VK_ERROR_INCOMPATIBLE_DRIVER
)
809 VkResult
anv_EnumeratePhysicalDevices(
810 VkInstance _instance
,
811 uint32_t* pPhysicalDeviceCount
,
812 VkPhysicalDevice
* pPhysicalDevices
)
814 ANV_FROM_HANDLE(anv_instance
, instance
, _instance
);
815 VK_OUTARRAY_MAKE(out
, pPhysicalDevices
, pPhysicalDeviceCount
);
817 VkResult result
= anv_instance_ensure_physical_device(instance
);
818 if (result
!= VK_SUCCESS
)
821 if (instance
->physicalDeviceCount
== 0)
824 assert(instance
->physicalDeviceCount
== 1);
825 vk_outarray_append(&out
, i
) {
826 *i
= anv_physical_device_to_handle(&instance
->physicalDevice
);
829 return vk_outarray_status(&out
);
832 VkResult
anv_EnumeratePhysicalDeviceGroups(
833 VkInstance _instance
,
834 uint32_t* pPhysicalDeviceGroupCount
,
835 VkPhysicalDeviceGroupProperties
* pPhysicalDeviceGroupProperties
)
837 ANV_FROM_HANDLE(anv_instance
, instance
, _instance
);
838 VK_OUTARRAY_MAKE(out
, pPhysicalDeviceGroupProperties
,
839 pPhysicalDeviceGroupCount
);
841 VkResult result
= anv_instance_ensure_physical_device(instance
);
842 if (result
!= VK_SUCCESS
)
845 if (instance
->physicalDeviceCount
== 0)
848 assert(instance
->physicalDeviceCount
== 1);
850 vk_outarray_append(&out
, p
) {
851 p
->physicalDeviceCount
= 1;
852 memset(p
->physicalDevices
, 0, sizeof(p
->physicalDevices
));
853 p
->physicalDevices
[0] =
854 anv_physical_device_to_handle(&instance
->physicalDevice
);
855 p
->subsetAllocation
= false;
857 vk_foreach_struct(ext
, p
->pNext
)
858 anv_debug_ignored_stype(ext
->sType
);
861 return vk_outarray_status(&out
);
864 void anv_GetPhysicalDeviceFeatures(
865 VkPhysicalDevice physicalDevice
,
866 VkPhysicalDeviceFeatures
* pFeatures
)
868 ANV_FROM_HANDLE(anv_physical_device
, pdevice
, physicalDevice
);
870 *pFeatures
= (VkPhysicalDeviceFeatures
) {
871 .robustBufferAccess
= true,
872 .fullDrawIndexUint32
= true,
873 .imageCubeArray
= true,
874 .independentBlend
= true,
875 .geometryShader
= true,
876 .tessellationShader
= true,
877 .sampleRateShading
= true,
878 .dualSrcBlend
= true,
880 .multiDrawIndirect
= true,
881 .drawIndirectFirstInstance
= true,
883 .depthBiasClamp
= true,
884 .fillModeNonSolid
= true,
885 .depthBounds
= false,
889 .multiViewport
= true,
890 .samplerAnisotropy
= true,
891 .textureCompressionETC2
= pdevice
->info
.gen
>= 8 ||
892 pdevice
->info
.is_baytrail
,
893 .textureCompressionASTC_LDR
= pdevice
->info
.gen
>= 9, /* FINISHME CHV */
894 .textureCompressionBC
= true,
895 .occlusionQueryPrecise
= true,
896 .pipelineStatisticsQuery
= true,
897 .fragmentStoresAndAtomics
= true,
898 .shaderTessellationAndGeometryPointSize
= true,
899 .shaderImageGatherExtended
= true,
900 .shaderStorageImageExtendedFormats
= true,
901 .shaderStorageImageMultisample
= false,
902 .shaderStorageImageReadWithoutFormat
= false,
903 .shaderStorageImageWriteWithoutFormat
= true,
904 .shaderUniformBufferArrayDynamicIndexing
= true,
905 .shaderSampledImageArrayDynamicIndexing
= true,
906 .shaderStorageBufferArrayDynamicIndexing
= true,
907 .shaderStorageImageArrayDynamicIndexing
= true,
908 .shaderClipDistance
= true,
909 .shaderCullDistance
= true,
910 .shaderFloat64
= pdevice
->info
.gen
>= 8 &&
911 pdevice
->info
.has_64bit_types
,
912 .shaderInt64
= pdevice
->info
.gen
>= 8 &&
913 pdevice
->info
.has_64bit_types
,
914 .shaderInt16
= pdevice
->info
.gen
>= 8,
915 .shaderResourceMinLod
= pdevice
->info
.gen
>= 9,
916 .variableMultisampleRate
= true,
917 .inheritedQueries
= true,
920 /* We can't do image stores in vec4 shaders */
921 pFeatures
->vertexPipelineStoresAndAtomics
=
922 pdevice
->compiler
->scalar_stage
[MESA_SHADER_VERTEX
] &&
923 pdevice
->compiler
->scalar_stage
[MESA_SHADER_GEOMETRY
];
925 struct anv_app_info
*app_info
= &pdevice
->instance
->app_info
;
927 /* The new DOOM and Wolfenstein games require depthBounds without
928 * checking for it. They seem to run fine without it so just claim it's
929 * there and accept the consequences.
931 if (app_info
->engine_name
&& strcmp(app_info
->engine_name
, "idTech") == 0)
932 pFeatures
->depthBounds
= true;
935 void anv_GetPhysicalDeviceFeatures2(
936 VkPhysicalDevice physicalDevice
,
937 VkPhysicalDeviceFeatures2
* pFeatures
)
939 ANV_FROM_HANDLE(anv_physical_device
, pdevice
, physicalDevice
);
940 anv_GetPhysicalDeviceFeatures(physicalDevice
, &pFeatures
->features
);
942 vk_foreach_struct(ext
, pFeatures
->pNext
) {
943 switch (ext
->sType
) {
944 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_8BIT_STORAGE_FEATURES_KHR
: {
945 VkPhysicalDevice8BitStorageFeaturesKHR
*features
=
946 (VkPhysicalDevice8BitStorageFeaturesKHR
*)ext
;
947 features
->storageBuffer8BitAccess
= pdevice
->info
.gen
>= 8;
948 features
->uniformAndStorageBuffer8BitAccess
= pdevice
->info
.gen
>= 8;
949 features
->storagePushConstant8
= pdevice
->info
.gen
>= 8;
953 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_16BIT_STORAGE_FEATURES
: {
954 VkPhysicalDevice16BitStorageFeatures
*features
=
955 (VkPhysicalDevice16BitStorageFeatures
*)ext
;
956 features
->storageBuffer16BitAccess
= pdevice
->info
.gen
>= 8;
957 features
->uniformAndStorageBuffer16BitAccess
= pdevice
->info
.gen
>= 8;
958 features
->storagePushConstant16
= pdevice
->info
.gen
>= 8;
959 features
->storageInputOutput16
= false;
963 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_BUFFER_DEVICE_ADDRESS_FEATURES_EXT
: {
964 VkPhysicalDeviceBufferDeviceAddressFeaturesEXT
*features
= (void *)ext
;
965 features
->bufferDeviceAddress
= pdevice
->use_softpin
&&
966 pdevice
->info
.gen
>= 8;
967 features
->bufferDeviceAddressCaptureReplay
= false;
968 features
->bufferDeviceAddressMultiDevice
= false;
972 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_COMPUTE_SHADER_DERIVATIVES_FEATURES_NV
: {
973 VkPhysicalDeviceComputeShaderDerivativesFeaturesNV
*features
=
974 (VkPhysicalDeviceComputeShaderDerivativesFeaturesNV
*)ext
;
975 features
->computeDerivativeGroupQuads
= true;
976 features
->computeDerivativeGroupLinear
= true;
980 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_CONDITIONAL_RENDERING_FEATURES_EXT
: {
981 VkPhysicalDeviceConditionalRenderingFeaturesEXT
*features
=
982 (VkPhysicalDeviceConditionalRenderingFeaturesEXT
*)ext
;
983 features
->conditionalRendering
= pdevice
->info
.gen
>= 8 ||
984 pdevice
->info
.is_haswell
;
985 features
->inheritedConditionalRendering
= pdevice
->info
.gen
>= 8 ||
986 pdevice
->info
.is_haswell
;
990 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_DEPTH_CLIP_ENABLE_FEATURES_EXT
: {
991 VkPhysicalDeviceDepthClipEnableFeaturesEXT
*features
=
992 (VkPhysicalDeviceDepthClipEnableFeaturesEXT
*)ext
;
993 features
->depthClipEnable
= true;
997 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FLOAT16_INT8_FEATURES_KHR
: {
998 VkPhysicalDeviceFloat16Int8FeaturesKHR
*features
= (void *)ext
;
999 features
->shaderFloat16
= pdevice
->info
.gen
>= 8;
1000 features
->shaderInt8
= pdevice
->info
.gen
>= 8;
1004 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_HOST_QUERY_RESET_FEATURES_EXT
: {
1005 VkPhysicalDeviceHostQueryResetFeaturesEXT
*features
=
1006 (VkPhysicalDeviceHostQueryResetFeaturesEXT
*)ext
;
1007 features
->hostQueryReset
= true;
1011 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_INLINE_UNIFORM_BLOCK_FEATURES_EXT
: {
1012 VkPhysicalDeviceInlineUniformBlockFeaturesEXT
*features
=
1013 (VkPhysicalDeviceInlineUniformBlockFeaturesEXT
*)ext
;
1014 features
->inlineUniformBlock
= true;
1015 features
->descriptorBindingInlineUniformBlockUpdateAfterBind
= false;
1019 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_MULTIVIEW_FEATURES
: {
1020 VkPhysicalDeviceMultiviewFeatures
*features
=
1021 (VkPhysicalDeviceMultiviewFeatures
*)ext
;
1022 features
->multiview
= true;
1023 features
->multiviewGeometryShader
= true;
1024 features
->multiviewTessellationShader
= true;
1028 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PROTECTED_MEMORY_FEATURES
: {
1029 VkPhysicalDeviceProtectedMemoryFeatures
*features
= (void *)ext
;
1030 features
->protectedMemory
= false;
1034 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SAMPLER_YCBCR_CONVERSION_FEATURES
: {
1035 VkPhysicalDeviceSamplerYcbcrConversionFeatures
*features
=
1036 (VkPhysicalDeviceSamplerYcbcrConversionFeatures
*) ext
;
1037 features
->samplerYcbcrConversion
= true;
1041 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SCALAR_BLOCK_LAYOUT_FEATURES_EXT
: {
1042 VkPhysicalDeviceScalarBlockLayoutFeaturesEXT
*features
=
1043 (VkPhysicalDeviceScalarBlockLayoutFeaturesEXT
*)ext
;
1044 features
->scalarBlockLayout
= true;
1048 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SHADER_DRAW_PARAMETERS_FEATURES
: {
1049 VkPhysicalDeviceShaderDrawParametersFeatures
*features
= (void *)ext
;
1050 features
->shaderDrawParameters
= true;
1054 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VARIABLE_POINTERS_FEATURES
: {
1055 VkPhysicalDeviceVariablePointersFeatures
*features
= (void *)ext
;
1056 features
->variablePointersStorageBuffer
= true;
1057 features
->variablePointers
= true;
1061 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_TRANSFORM_FEEDBACK_FEATURES_EXT
: {
1062 VkPhysicalDeviceTransformFeedbackFeaturesEXT
*features
=
1063 (VkPhysicalDeviceTransformFeedbackFeaturesEXT
*)ext
;
1064 features
->transformFeedback
= true;
1065 features
->geometryStreams
= true;
1069 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VERTEX_ATTRIBUTE_DIVISOR_FEATURES_EXT
: {
1070 VkPhysicalDeviceVertexAttributeDivisorFeaturesEXT
*features
=
1071 (VkPhysicalDeviceVertexAttributeDivisorFeaturesEXT
*)ext
;
1072 features
->vertexAttributeInstanceRateDivisor
= true;
1073 features
->vertexAttributeInstanceRateZeroDivisor
= true;
1077 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_YCBCR_IMAGE_ARRAYS_FEATURES_EXT
: {
1078 VkPhysicalDeviceYcbcrImageArraysFeaturesEXT
*features
=
1079 (VkPhysicalDeviceYcbcrImageArraysFeaturesEXT
*)ext
;
1080 features
->ycbcrImageArrays
= true;
1085 anv_debug_ignored_stype(ext
->sType
);
1091 void anv_GetPhysicalDeviceProperties(
1092 VkPhysicalDevice physicalDevice
,
1093 VkPhysicalDeviceProperties
* pProperties
)
1095 ANV_FROM_HANDLE(anv_physical_device
, pdevice
, physicalDevice
);
1096 const struct gen_device_info
*devinfo
= &pdevice
->info
;
1098 /* See assertions made when programming the buffer surface state. */
1099 const uint32_t max_raw_buffer_sz
= devinfo
->gen
>= 7 ?
1100 (1ul << 30) : (1ul << 27);
1102 const uint32_t max_samplers
= (devinfo
->gen
>= 8 || devinfo
->is_haswell
) ?
1105 VkSampleCountFlags sample_counts
=
1106 isl_device_get_sample_counts(&pdevice
->isl_dev
);
1109 VkPhysicalDeviceLimits limits
= {
1110 .maxImageDimension1D
= (1 << 14),
1111 .maxImageDimension2D
= (1 << 14),
1112 .maxImageDimension3D
= (1 << 11),
1113 .maxImageDimensionCube
= (1 << 14),
1114 .maxImageArrayLayers
= (1 << 11),
1115 .maxTexelBufferElements
= 128 * 1024 * 1024,
1116 .maxUniformBufferRange
= (1ul << 27),
1117 .maxStorageBufferRange
= max_raw_buffer_sz
,
1118 .maxPushConstantsSize
= MAX_PUSH_CONSTANTS_SIZE
,
1119 .maxMemoryAllocationCount
= UINT32_MAX
,
1120 .maxSamplerAllocationCount
= 64 * 1024,
1121 .bufferImageGranularity
= 64, /* A cache line */
1122 .sparseAddressSpaceSize
= 0,
1123 .maxBoundDescriptorSets
= MAX_SETS
,
1124 .maxPerStageDescriptorSamplers
= max_samplers
,
1125 .maxPerStageDescriptorUniformBuffers
= 64,
1126 .maxPerStageDescriptorStorageBuffers
= 64,
1127 .maxPerStageDescriptorSampledImages
= max_samplers
,
1128 .maxPerStageDescriptorStorageImages
= MAX_IMAGES
,
1129 .maxPerStageDescriptorInputAttachments
= 64,
1130 .maxPerStageResources
= MAX_BINDING_TABLE_SIZE
- MAX_RTS
,
1131 .maxDescriptorSetSamplers
= 6 * max_samplers
, /* number of stages * maxPerStageDescriptorSamplers */
1132 .maxDescriptorSetUniformBuffers
= 6 * 64, /* number of stages * maxPerStageDescriptorUniformBuffers */
1133 .maxDescriptorSetUniformBuffersDynamic
= MAX_DYNAMIC_BUFFERS
/ 2,
1134 .maxDescriptorSetStorageBuffers
= 6 * 64, /* number of stages * maxPerStageDescriptorStorageBuffers */
1135 .maxDescriptorSetStorageBuffersDynamic
= MAX_DYNAMIC_BUFFERS
/ 2,
1136 .maxDescriptorSetSampledImages
= 6 * max_samplers
, /* number of stages * maxPerStageDescriptorSampledImages */
1137 .maxDescriptorSetStorageImages
= 6 * MAX_IMAGES
, /* number of stages * maxPerStageDescriptorStorageImages */
1138 .maxDescriptorSetInputAttachments
= 256,
1139 .maxVertexInputAttributes
= MAX_VBS
,
1140 .maxVertexInputBindings
= MAX_VBS
,
1141 .maxVertexInputAttributeOffset
= 2047,
1142 .maxVertexInputBindingStride
= 2048,
1143 .maxVertexOutputComponents
= 128,
1144 .maxTessellationGenerationLevel
= 64,
1145 .maxTessellationPatchSize
= 32,
1146 .maxTessellationControlPerVertexInputComponents
= 128,
1147 .maxTessellationControlPerVertexOutputComponents
= 128,
1148 .maxTessellationControlPerPatchOutputComponents
= 128,
1149 .maxTessellationControlTotalOutputComponents
= 2048,
1150 .maxTessellationEvaluationInputComponents
= 128,
1151 .maxTessellationEvaluationOutputComponents
= 128,
1152 .maxGeometryShaderInvocations
= 32,
1153 .maxGeometryInputComponents
= 64,
1154 .maxGeometryOutputComponents
= 128,
1155 .maxGeometryOutputVertices
= 256,
1156 .maxGeometryTotalOutputComponents
= 1024,
1157 .maxFragmentInputComponents
= 112, /* 128 components - (POS, PSIZ, CLIP_DIST0, CLIP_DIST1) */
1158 .maxFragmentOutputAttachments
= 8,
1159 .maxFragmentDualSrcAttachments
= 1,
1160 .maxFragmentCombinedOutputResources
= 8,
1161 .maxComputeSharedMemorySize
= 32768,
1162 .maxComputeWorkGroupCount
= { 65535, 65535, 65535 },
1163 .maxComputeWorkGroupInvocations
= 32 * devinfo
->max_cs_threads
,
1164 .maxComputeWorkGroupSize
= {
1165 16 * devinfo
->max_cs_threads
,
1166 16 * devinfo
->max_cs_threads
,
1167 16 * devinfo
->max_cs_threads
,
1169 .subPixelPrecisionBits
= 8,
1170 .subTexelPrecisionBits
= 8,
1171 .mipmapPrecisionBits
= 8,
1172 .maxDrawIndexedIndexValue
= UINT32_MAX
,
1173 .maxDrawIndirectCount
= UINT32_MAX
,
1174 .maxSamplerLodBias
= 16,
1175 .maxSamplerAnisotropy
= 16,
1176 .maxViewports
= MAX_VIEWPORTS
,
1177 .maxViewportDimensions
= { (1 << 14), (1 << 14) },
1178 .viewportBoundsRange
= { INT16_MIN
, INT16_MAX
},
1179 .viewportSubPixelBits
= 13, /* We take a float? */
1180 .minMemoryMapAlignment
= 4096, /* A page */
1181 .minTexelBufferOffsetAlignment
= 1,
1182 /* We need 16 for UBO block reads to work and 32 for push UBOs */
1183 .minUniformBufferOffsetAlignment
= 32,
1184 .minStorageBufferOffsetAlignment
= 4,
1185 .minTexelOffset
= -8,
1186 .maxTexelOffset
= 7,
1187 .minTexelGatherOffset
= -32,
1188 .maxTexelGatherOffset
= 31,
1189 .minInterpolationOffset
= -0.5,
1190 .maxInterpolationOffset
= 0.4375,
1191 .subPixelInterpolationOffsetBits
= 4,
1192 .maxFramebufferWidth
= (1 << 14),
1193 .maxFramebufferHeight
= (1 << 14),
1194 .maxFramebufferLayers
= (1 << 11),
1195 .framebufferColorSampleCounts
= sample_counts
,
1196 .framebufferDepthSampleCounts
= sample_counts
,
1197 .framebufferStencilSampleCounts
= sample_counts
,
1198 .framebufferNoAttachmentsSampleCounts
= sample_counts
,
1199 .maxColorAttachments
= MAX_RTS
,
1200 .sampledImageColorSampleCounts
= sample_counts
,
1201 .sampledImageIntegerSampleCounts
= VK_SAMPLE_COUNT_1_BIT
,
1202 .sampledImageDepthSampleCounts
= sample_counts
,
1203 .sampledImageStencilSampleCounts
= sample_counts
,
1204 .storageImageSampleCounts
= VK_SAMPLE_COUNT_1_BIT
,
1205 .maxSampleMaskWords
= 1,
1206 .timestampComputeAndGraphics
= false,
1207 .timestampPeriod
= 1000000000.0 / devinfo
->timestamp_frequency
,
1208 .maxClipDistances
= 8,
1209 .maxCullDistances
= 8,
1210 .maxCombinedClipAndCullDistances
= 8,
1211 .discreteQueuePriorities
= 2,
1212 .pointSizeRange
= { 0.125, 255.875 },
1213 .lineWidthRange
= { 0.0, 7.9921875 },
1214 .pointSizeGranularity
= (1.0 / 8.0),
1215 .lineWidthGranularity
= (1.0 / 128.0),
1216 .strictLines
= false, /* FINISHME */
1217 .standardSampleLocations
= true,
1218 .optimalBufferCopyOffsetAlignment
= 128,
1219 .optimalBufferCopyRowPitchAlignment
= 128,
1220 .nonCoherentAtomSize
= 64,
1223 *pProperties
= (VkPhysicalDeviceProperties
) {
1224 .apiVersion
= anv_physical_device_api_version(pdevice
),
1225 .driverVersion
= vk_get_driver_version(),
1227 .deviceID
= pdevice
->chipset_id
,
1228 .deviceType
= VK_PHYSICAL_DEVICE_TYPE_INTEGRATED_GPU
,
1230 .sparseProperties
= {0}, /* Broadwell doesn't do sparse. */
1233 snprintf(pProperties
->deviceName
, sizeof(pProperties
->deviceName
),
1234 "%s", pdevice
->name
);
1235 memcpy(pProperties
->pipelineCacheUUID
,
1236 pdevice
->pipeline_cache_uuid
, VK_UUID_SIZE
);
1239 void anv_GetPhysicalDeviceProperties2(
1240 VkPhysicalDevice physicalDevice
,
1241 VkPhysicalDeviceProperties2
* pProperties
)
1243 ANV_FROM_HANDLE(anv_physical_device
, pdevice
, physicalDevice
);
1245 anv_GetPhysicalDeviceProperties(physicalDevice
, &pProperties
->properties
);
1247 vk_foreach_struct(ext
, pProperties
->pNext
) {
1248 switch (ext
->sType
) {
1249 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_DEPTH_STENCIL_RESOLVE_PROPERTIES_KHR
: {
1250 VkPhysicalDeviceDepthStencilResolvePropertiesKHR
*props
=
1251 (VkPhysicalDeviceDepthStencilResolvePropertiesKHR
*)ext
;
1253 /* We support all of the depth resolve modes */
1254 props
->supportedDepthResolveModes
=
1255 VK_RESOLVE_MODE_SAMPLE_ZERO_BIT_KHR
|
1256 VK_RESOLVE_MODE_AVERAGE_BIT_KHR
|
1257 VK_RESOLVE_MODE_MIN_BIT_KHR
|
1258 VK_RESOLVE_MODE_MAX_BIT_KHR
;
1260 /* Average doesn't make sense for stencil so we don't support that */
1261 props
->supportedStencilResolveModes
=
1262 VK_RESOLVE_MODE_SAMPLE_ZERO_BIT_KHR
;
1263 if (pdevice
->info
.gen
>= 8) {
1264 /* The advanced stencil resolve modes currently require stencil
1265 * sampling be supported by the hardware.
1267 props
->supportedStencilResolveModes
|=
1268 VK_RESOLVE_MODE_MIN_BIT_KHR
|
1269 VK_RESOLVE_MODE_MAX_BIT_KHR
;
1272 props
->independentResolveNone
= true;
1273 props
->independentResolve
= true;
1277 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_DRIVER_PROPERTIES_KHR
: {
1278 VkPhysicalDeviceDriverPropertiesKHR
*driver_props
=
1279 (VkPhysicalDeviceDriverPropertiesKHR
*) ext
;
1281 driver_props
->driverID
= VK_DRIVER_ID_INTEL_OPEN_SOURCE_MESA_KHR
;
1282 util_snprintf(driver_props
->driverName
, VK_MAX_DRIVER_NAME_SIZE_KHR
,
1283 "Intel open-source Mesa driver");
1285 util_snprintf(driver_props
->driverInfo
, VK_MAX_DRIVER_INFO_SIZE_KHR
,
1286 "Mesa " PACKAGE_VERSION MESA_GIT_SHA1
);
1288 driver_props
->conformanceVersion
= (VkConformanceVersionKHR
) {
1297 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_EXTERNAL_MEMORY_HOST_PROPERTIES_EXT
: {
1298 VkPhysicalDeviceExternalMemoryHostPropertiesEXT
*props
=
1299 (VkPhysicalDeviceExternalMemoryHostPropertiesEXT
*) ext
;
1300 /* Userptr needs page aligned memory. */
1301 props
->minImportedHostPointerAlignment
= 4096;
1305 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_ID_PROPERTIES
: {
1306 VkPhysicalDeviceIDProperties
*id_props
=
1307 (VkPhysicalDeviceIDProperties
*)ext
;
1308 memcpy(id_props
->deviceUUID
, pdevice
->device_uuid
, VK_UUID_SIZE
);
1309 memcpy(id_props
->driverUUID
, pdevice
->driver_uuid
, VK_UUID_SIZE
);
1310 /* The LUID is for Windows. */
1311 id_props
->deviceLUIDValid
= false;
1315 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_INLINE_UNIFORM_BLOCK_PROPERTIES_EXT
: {
1316 VkPhysicalDeviceInlineUniformBlockPropertiesEXT
*props
=
1317 (VkPhysicalDeviceInlineUniformBlockPropertiesEXT
*)ext
;
1318 props
->maxInlineUniformBlockSize
= MAX_INLINE_UNIFORM_BLOCK_SIZE
;
1319 props
->maxPerStageDescriptorInlineUniformBlocks
=
1320 MAX_INLINE_UNIFORM_BLOCK_DESCRIPTORS
;
1321 props
->maxPerStageDescriptorUpdateAfterBindInlineUniformBlocks
=
1322 MAX_INLINE_UNIFORM_BLOCK_DESCRIPTORS
;
1323 props
->maxDescriptorSetInlineUniformBlocks
=
1324 MAX_INLINE_UNIFORM_BLOCK_DESCRIPTORS
;
1325 props
->maxDescriptorSetUpdateAfterBindInlineUniformBlocks
=
1326 MAX_INLINE_UNIFORM_BLOCK_DESCRIPTORS
;
1330 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_MAINTENANCE_3_PROPERTIES
: {
1331 VkPhysicalDeviceMaintenance3Properties
*props
=
1332 (VkPhysicalDeviceMaintenance3Properties
*)ext
;
1333 /* This value doesn't matter for us today as our per-stage
1334 * descriptors are the real limit.
1336 props
->maxPerSetDescriptors
= 1024;
1337 props
->maxMemoryAllocationSize
= MAX_MEMORY_ALLOCATION_SIZE
;
1341 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_MULTIVIEW_PROPERTIES
: {
1342 VkPhysicalDeviceMultiviewProperties
*properties
=
1343 (VkPhysicalDeviceMultiviewProperties
*)ext
;
1344 properties
->maxMultiviewViewCount
= 16;
1345 properties
->maxMultiviewInstanceIndex
= UINT32_MAX
/ 16;
1349 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PCI_BUS_INFO_PROPERTIES_EXT
: {
1350 VkPhysicalDevicePCIBusInfoPropertiesEXT
*properties
=
1351 (VkPhysicalDevicePCIBusInfoPropertiesEXT
*)ext
;
1352 properties
->pciDomain
= pdevice
->pci_info
.domain
;
1353 properties
->pciBus
= pdevice
->pci_info
.bus
;
1354 properties
->pciDevice
= pdevice
->pci_info
.device
;
1355 properties
->pciFunction
= pdevice
->pci_info
.function
;
1359 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_POINT_CLIPPING_PROPERTIES
: {
1360 VkPhysicalDevicePointClippingProperties
*properties
=
1361 (VkPhysicalDevicePointClippingProperties
*) ext
;
1362 properties
->pointClippingBehavior
= VK_POINT_CLIPPING_BEHAVIOR_ALL_CLIP_PLANES
;
1363 anv_finishme("Implement pop-free point clipping");
1367 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PROTECTED_MEMORY_PROPERTIES
: {
1368 VkPhysicalDeviceProtectedMemoryProperties
*props
=
1369 (VkPhysicalDeviceProtectedMemoryProperties
*)ext
;
1370 props
->protectedNoFault
= false;
1374 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PUSH_DESCRIPTOR_PROPERTIES_KHR
: {
1375 VkPhysicalDevicePushDescriptorPropertiesKHR
*properties
=
1376 (VkPhysicalDevicePushDescriptorPropertiesKHR
*) ext
;
1378 properties
->maxPushDescriptors
= MAX_PUSH_DESCRIPTORS
;
1382 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SAMPLER_FILTER_MINMAX_PROPERTIES_EXT
: {
1383 VkPhysicalDeviceSamplerFilterMinmaxPropertiesEXT
*properties
=
1384 (VkPhysicalDeviceSamplerFilterMinmaxPropertiesEXT
*)ext
;
1385 properties
->filterMinmaxImageComponentMapping
= pdevice
->info
.gen
>= 9;
1386 properties
->filterMinmaxSingleComponentFormats
= true;
1390 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SUBGROUP_PROPERTIES
: {
1391 VkPhysicalDeviceSubgroupProperties
*properties
= (void *)ext
;
1393 properties
->subgroupSize
= BRW_SUBGROUP_SIZE
;
1395 VkShaderStageFlags scalar_stages
= 0;
1396 for (unsigned stage
= 0; stage
< MESA_SHADER_STAGES
; stage
++) {
1397 if (pdevice
->compiler
->scalar_stage
[stage
])
1398 scalar_stages
|= mesa_to_vk_shader_stage(stage
);
1400 properties
->supportedStages
= scalar_stages
;
1402 properties
->supportedOperations
= VK_SUBGROUP_FEATURE_BASIC_BIT
|
1403 VK_SUBGROUP_FEATURE_VOTE_BIT
|
1404 VK_SUBGROUP_FEATURE_ARITHMETIC_BIT
|
1405 VK_SUBGROUP_FEATURE_BALLOT_BIT
|
1406 VK_SUBGROUP_FEATURE_SHUFFLE_BIT
|
1407 VK_SUBGROUP_FEATURE_SHUFFLE_RELATIVE_BIT
|
1408 VK_SUBGROUP_FEATURE_CLUSTERED_BIT
|
1409 VK_SUBGROUP_FEATURE_QUAD_BIT
;
1410 properties
->quadOperationsInAllStages
= true;
1414 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_TRANSFORM_FEEDBACK_PROPERTIES_EXT
: {
1415 VkPhysicalDeviceTransformFeedbackPropertiesEXT
*props
=
1416 (VkPhysicalDeviceTransformFeedbackPropertiesEXT
*)ext
;
1418 props
->maxTransformFeedbackStreams
= MAX_XFB_STREAMS
;
1419 props
->maxTransformFeedbackBuffers
= MAX_XFB_BUFFERS
;
1420 props
->maxTransformFeedbackBufferSize
= (1ull << 32);
1421 props
->maxTransformFeedbackStreamDataSize
= 128 * 4;
1422 props
->maxTransformFeedbackBufferDataSize
= 128 * 4;
1423 props
->maxTransformFeedbackBufferDataStride
= 2048;
1424 props
->transformFeedbackQueries
= true;
1425 props
->transformFeedbackStreamsLinesTriangles
= false;
1426 props
->transformFeedbackRasterizationStreamSelect
= false;
1427 props
->transformFeedbackDraw
= true;
1431 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VERTEX_ATTRIBUTE_DIVISOR_PROPERTIES_EXT
: {
1432 VkPhysicalDeviceVertexAttributeDivisorPropertiesEXT
*props
=
1433 (VkPhysicalDeviceVertexAttributeDivisorPropertiesEXT
*)ext
;
1434 /* We have to restrict this a bit for multiview */
1435 props
->maxVertexAttribDivisor
= UINT32_MAX
/ 16;
1440 anv_debug_ignored_stype(ext
->sType
);
1446 /* We support exactly one queue family. */
1447 static const VkQueueFamilyProperties
1448 anv_queue_family_properties
= {
1449 .queueFlags
= VK_QUEUE_GRAPHICS_BIT
|
1450 VK_QUEUE_COMPUTE_BIT
|
1451 VK_QUEUE_TRANSFER_BIT
,
1453 .timestampValidBits
= 36, /* XXX: Real value here */
1454 .minImageTransferGranularity
= { 1, 1, 1 },
1457 void anv_GetPhysicalDeviceQueueFamilyProperties(
1458 VkPhysicalDevice physicalDevice
,
1460 VkQueueFamilyProperties
* pQueueFamilyProperties
)
1462 VK_OUTARRAY_MAKE(out
, pQueueFamilyProperties
, pCount
);
1464 vk_outarray_append(&out
, p
) {
1465 *p
= anv_queue_family_properties
;
1469 void anv_GetPhysicalDeviceQueueFamilyProperties2(
1470 VkPhysicalDevice physicalDevice
,
1471 uint32_t* pQueueFamilyPropertyCount
,
1472 VkQueueFamilyProperties2
* pQueueFamilyProperties
)
1475 VK_OUTARRAY_MAKE(out
, pQueueFamilyProperties
, pQueueFamilyPropertyCount
);
1477 vk_outarray_append(&out
, p
) {
1478 p
->queueFamilyProperties
= anv_queue_family_properties
;
1480 vk_foreach_struct(s
, p
->pNext
) {
1481 anv_debug_ignored_stype(s
->sType
);
1486 void anv_GetPhysicalDeviceMemoryProperties(
1487 VkPhysicalDevice physicalDevice
,
1488 VkPhysicalDeviceMemoryProperties
* pMemoryProperties
)
1490 ANV_FROM_HANDLE(anv_physical_device
, physical_device
, physicalDevice
);
1492 pMemoryProperties
->memoryTypeCount
= physical_device
->memory
.type_count
;
1493 for (uint32_t i
= 0; i
< physical_device
->memory
.type_count
; i
++) {
1494 pMemoryProperties
->memoryTypes
[i
] = (VkMemoryType
) {
1495 .propertyFlags
= physical_device
->memory
.types
[i
].propertyFlags
,
1496 .heapIndex
= physical_device
->memory
.types
[i
].heapIndex
,
1500 pMemoryProperties
->memoryHeapCount
= physical_device
->memory
.heap_count
;
1501 for (uint32_t i
= 0; i
< physical_device
->memory
.heap_count
; i
++) {
1502 pMemoryProperties
->memoryHeaps
[i
] = (VkMemoryHeap
) {
1503 .size
= physical_device
->memory
.heaps
[i
].size
,
1504 .flags
= physical_device
->memory
.heaps
[i
].flags
,
1509 void anv_GetPhysicalDeviceMemoryProperties2(
1510 VkPhysicalDevice physicalDevice
,
1511 VkPhysicalDeviceMemoryProperties2
* pMemoryProperties
)
1513 anv_GetPhysicalDeviceMemoryProperties(physicalDevice
,
1514 &pMemoryProperties
->memoryProperties
);
1516 vk_foreach_struct(ext
, pMemoryProperties
->pNext
) {
1517 switch (ext
->sType
) {
1519 anv_debug_ignored_stype(ext
->sType
);
1526 anv_GetDeviceGroupPeerMemoryFeatures(
1529 uint32_t localDeviceIndex
,
1530 uint32_t remoteDeviceIndex
,
1531 VkPeerMemoryFeatureFlags
* pPeerMemoryFeatures
)
1533 assert(localDeviceIndex
== 0 && remoteDeviceIndex
== 0);
1534 *pPeerMemoryFeatures
= VK_PEER_MEMORY_FEATURE_COPY_SRC_BIT
|
1535 VK_PEER_MEMORY_FEATURE_COPY_DST_BIT
|
1536 VK_PEER_MEMORY_FEATURE_GENERIC_SRC_BIT
|
1537 VK_PEER_MEMORY_FEATURE_GENERIC_DST_BIT
;
1540 PFN_vkVoidFunction
anv_GetInstanceProcAddr(
1541 VkInstance _instance
,
1544 ANV_FROM_HANDLE(anv_instance
, instance
, _instance
);
1546 /* The Vulkan 1.0 spec for vkGetInstanceProcAddr has a table of exactly
1547 * when we have to return valid function pointers, NULL, or it's left
1548 * undefined. See the table for exact details.
1553 #define LOOKUP_ANV_ENTRYPOINT(entrypoint) \
1554 if (strcmp(pName, "vk" #entrypoint) == 0) \
1555 return (PFN_vkVoidFunction)anv_##entrypoint
1557 LOOKUP_ANV_ENTRYPOINT(EnumerateInstanceExtensionProperties
);
1558 LOOKUP_ANV_ENTRYPOINT(EnumerateInstanceLayerProperties
);
1559 LOOKUP_ANV_ENTRYPOINT(EnumerateInstanceVersion
);
1560 LOOKUP_ANV_ENTRYPOINT(CreateInstance
);
1562 #undef LOOKUP_ANV_ENTRYPOINT
1564 if (instance
== NULL
)
1567 int idx
= anv_get_instance_entrypoint_index(pName
);
1569 return instance
->dispatch
.entrypoints
[idx
];
1571 idx
= anv_get_device_entrypoint_index(pName
);
1573 return instance
->device_dispatch
.entrypoints
[idx
];
1578 /* With version 1+ of the loader interface the ICD should expose
1579 * vk_icdGetInstanceProcAddr to work around certain LD_PRELOAD issues seen in apps.
1582 VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL
vk_icdGetInstanceProcAddr(
1583 VkInstance instance
,
1587 VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL
vk_icdGetInstanceProcAddr(
1588 VkInstance instance
,
1591 return anv_GetInstanceProcAddr(instance
, pName
);
1594 PFN_vkVoidFunction
anv_GetDeviceProcAddr(
1598 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1600 if (!device
|| !pName
)
1603 int idx
= anv_get_device_entrypoint_index(pName
);
1607 return device
->dispatch
.entrypoints
[idx
];
1611 anv_CreateDebugReportCallbackEXT(VkInstance _instance
,
1612 const VkDebugReportCallbackCreateInfoEXT
* pCreateInfo
,
1613 const VkAllocationCallbacks
* pAllocator
,
1614 VkDebugReportCallbackEXT
* pCallback
)
1616 ANV_FROM_HANDLE(anv_instance
, instance
, _instance
);
1617 return vk_create_debug_report_callback(&instance
->debug_report_callbacks
,
1618 pCreateInfo
, pAllocator
, &instance
->alloc
,
1623 anv_DestroyDebugReportCallbackEXT(VkInstance _instance
,
1624 VkDebugReportCallbackEXT _callback
,
1625 const VkAllocationCallbacks
* pAllocator
)
1627 ANV_FROM_HANDLE(anv_instance
, instance
, _instance
);
1628 vk_destroy_debug_report_callback(&instance
->debug_report_callbacks
,
1629 _callback
, pAllocator
, &instance
->alloc
);
1633 anv_DebugReportMessageEXT(VkInstance _instance
,
1634 VkDebugReportFlagsEXT flags
,
1635 VkDebugReportObjectTypeEXT objectType
,
1638 int32_t messageCode
,
1639 const char* pLayerPrefix
,
1640 const char* pMessage
)
1642 ANV_FROM_HANDLE(anv_instance
, instance
, _instance
);
1643 vk_debug_report(&instance
->debug_report_callbacks
, flags
, objectType
,
1644 object
, location
, messageCode
, pLayerPrefix
, pMessage
);
1648 anv_queue_init(struct anv_device
*device
, struct anv_queue
*queue
)
1650 queue
->_loader_data
.loaderMagic
= ICD_LOADER_MAGIC
;
1651 queue
->device
= device
;
1656 anv_queue_finish(struct anv_queue
*queue
)
1660 static struct anv_state
1661 anv_state_pool_emit_data(struct anv_state_pool
*pool
, size_t size
, size_t align
, const void *p
)
1663 struct anv_state state
;
1665 state
= anv_state_pool_alloc(pool
, size
, align
);
1666 memcpy(state
.map
, p
, size
);
1671 struct gen8_border_color
{
1676 /* Pad out to 64 bytes */
1681 anv_device_init_border_colors(struct anv_device
*device
)
1683 static const struct gen8_border_color border_colors
[] = {
1684 [VK_BORDER_COLOR_FLOAT_TRANSPARENT_BLACK
] = { .float32
= { 0.0, 0.0, 0.0, 0.0 } },
1685 [VK_BORDER_COLOR_FLOAT_OPAQUE_BLACK
] = { .float32
= { 0.0, 0.0, 0.0, 1.0 } },
1686 [VK_BORDER_COLOR_FLOAT_OPAQUE_WHITE
] = { .float32
= { 1.0, 1.0, 1.0, 1.0 } },
1687 [VK_BORDER_COLOR_INT_TRANSPARENT_BLACK
] = { .uint32
= { 0, 0, 0, 0 } },
1688 [VK_BORDER_COLOR_INT_OPAQUE_BLACK
] = { .uint32
= { 0, 0, 0, 1 } },
1689 [VK_BORDER_COLOR_INT_OPAQUE_WHITE
] = { .uint32
= { 1, 1, 1, 1 } },
1692 device
->border_colors
= anv_state_pool_emit_data(&device
->dynamic_state_pool
,
1693 sizeof(border_colors
), 64,
1698 anv_device_init_trivial_batch(struct anv_device
*device
)
1700 anv_bo_init_new(&device
->trivial_batch_bo
, device
, 4096);
1702 if (device
->instance
->physicalDevice
.has_exec_async
)
1703 device
->trivial_batch_bo
.flags
|= EXEC_OBJECT_ASYNC
;
1705 if (device
->instance
->physicalDevice
.use_softpin
)
1706 device
->trivial_batch_bo
.flags
|= EXEC_OBJECT_PINNED
;
1708 anv_vma_alloc(device
, &device
->trivial_batch_bo
);
1710 void *map
= anv_gem_mmap(device
, device
->trivial_batch_bo
.gem_handle
,
1713 struct anv_batch batch
= {
1719 anv_batch_emit(&batch
, GEN7_MI_BATCH_BUFFER_END
, bbe
);
1720 anv_batch_emit(&batch
, GEN7_MI_NOOP
, noop
);
1722 if (!device
->info
.has_llc
)
1723 gen_clflush_range(map
, batch
.next
- map
);
1725 anv_gem_munmap(map
, device
->trivial_batch_bo
.size
);
1728 VkResult
anv_EnumerateDeviceExtensionProperties(
1729 VkPhysicalDevice physicalDevice
,
1730 const char* pLayerName
,
1731 uint32_t* pPropertyCount
,
1732 VkExtensionProperties
* pProperties
)
1734 ANV_FROM_HANDLE(anv_physical_device
, device
, physicalDevice
);
1735 VK_OUTARRAY_MAKE(out
, pProperties
, pPropertyCount
);
1737 for (int i
= 0; i
< ANV_DEVICE_EXTENSION_COUNT
; i
++) {
1738 if (device
->supported_extensions
.extensions
[i
]) {
1739 vk_outarray_append(&out
, prop
) {
1740 *prop
= anv_device_extensions
[i
];
1745 return vk_outarray_status(&out
);
1749 anv_device_init_dispatch(struct anv_device
*device
)
1751 const struct anv_device_dispatch_table
*genX_table
;
1752 switch (device
->info
.gen
) {
1754 genX_table
= &gen11_device_dispatch_table
;
1757 genX_table
= &gen10_device_dispatch_table
;
1760 genX_table
= &gen9_device_dispatch_table
;
1763 genX_table
= &gen8_device_dispatch_table
;
1766 if (device
->info
.is_haswell
)
1767 genX_table
= &gen75_device_dispatch_table
;
1769 genX_table
= &gen7_device_dispatch_table
;
1772 unreachable("unsupported gen\n");
1775 for (unsigned i
= 0; i
< ARRAY_SIZE(device
->dispatch
.entrypoints
); i
++) {
1776 /* Vulkan requires that entrypoints for extensions which have not been
1777 * enabled must not be advertised.
1779 if (!anv_device_entrypoint_is_enabled(i
, device
->instance
->app_info
.api_version
,
1780 &device
->instance
->enabled_extensions
,
1781 &device
->enabled_extensions
)) {
1782 device
->dispatch
.entrypoints
[i
] = NULL
;
1783 } else if (genX_table
->entrypoints
[i
]) {
1784 device
->dispatch
.entrypoints
[i
] = genX_table
->entrypoints
[i
];
1786 device
->dispatch
.entrypoints
[i
] =
1787 anv_device_dispatch_table
.entrypoints
[i
];
1793 vk_priority_to_gen(int priority
)
1796 case VK_QUEUE_GLOBAL_PRIORITY_LOW_EXT
:
1797 return GEN_CONTEXT_LOW_PRIORITY
;
1798 case VK_QUEUE_GLOBAL_PRIORITY_MEDIUM_EXT
:
1799 return GEN_CONTEXT_MEDIUM_PRIORITY
;
1800 case VK_QUEUE_GLOBAL_PRIORITY_HIGH_EXT
:
1801 return GEN_CONTEXT_HIGH_PRIORITY
;
1802 case VK_QUEUE_GLOBAL_PRIORITY_REALTIME_EXT
:
1803 return GEN_CONTEXT_REALTIME_PRIORITY
;
1805 unreachable("Invalid priority");
1810 anv_device_init_hiz_clear_value_bo(struct anv_device
*device
)
1812 anv_bo_init_new(&device
->hiz_clear_bo
, device
, 4096);
1814 if (device
->instance
->physicalDevice
.has_exec_async
)
1815 device
->hiz_clear_bo
.flags
|= EXEC_OBJECT_ASYNC
;
1817 if (device
->instance
->physicalDevice
.use_softpin
)
1818 device
->hiz_clear_bo
.flags
|= EXEC_OBJECT_PINNED
;
1820 anv_vma_alloc(device
, &device
->hiz_clear_bo
);
1822 uint32_t *map
= anv_gem_mmap(device
, device
->hiz_clear_bo
.gem_handle
,
1825 union isl_color_value hiz_clear
= { .u32
= { 0, } };
1826 hiz_clear
.f32
[0] = ANV_HZ_FC_VAL
;
1828 memcpy(map
, hiz_clear
.u32
, sizeof(hiz_clear
.u32
));
1829 anv_gem_munmap(map
, device
->hiz_clear_bo
.size
);
1833 get_bo_from_pool(struct gen_batch_decode_bo
*ret
,
1834 struct anv_block_pool
*pool
,
1837 for (uint32_t i
= 0; i
< pool
->nbos
; i
++) {
1838 uint64_t bo_address
= pool
->bos
[i
].offset
& (~0ull >> 16);
1839 uint32_t bo_size
= pool
->bos
[i
].size
;
1840 if (address
>= bo_address
&& address
< (bo_address
+ bo_size
)) {
1841 *ret
= (struct gen_batch_decode_bo
) {
1844 .map
= pool
->bos
[i
].map
,
1852 /* Finding a buffer for batch decoding */
1853 static struct gen_batch_decode_bo
1854 decode_get_bo(void *v_batch
, bool ppgtt
, uint64_t address
)
1856 struct anv_device
*device
= v_batch
;
1857 struct gen_batch_decode_bo ret_bo
= {};
1861 if (get_bo_from_pool(&ret_bo
, &device
->dynamic_state_pool
.block_pool
, address
))
1863 if (get_bo_from_pool(&ret_bo
, &device
->instruction_state_pool
.block_pool
, address
))
1865 if (get_bo_from_pool(&ret_bo
, &device
->binding_table_pool
.block_pool
, address
))
1867 if (get_bo_from_pool(&ret_bo
, &device
->surface_state_pool
.block_pool
, address
))
1870 if (!device
->cmd_buffer_being_decoded
)
1871 return (struct gen_batch_decode_bo
) { };
1873 struct anv_batch_bo
**bo
;
1875 u_vector_foreach(bo
, &device
->cmd_buffer_being_decoded
->seen_bbos
) {
1876 /* The decoder zeroes out the top 16 bits, so we need to as well */
1877 uint64_t bo_address
= (*bo
)->bo
.offset
& (~0ull >> 16);
1879 if (address
>= bo_address
&& address
< bo_address
+ (*bo
)->bo
.size
) {
1880 return (struct gen_batch_decode_bo
) {
1882 .size
= (*bo
)->bo
.size
,
1883 .map
= (*bo
)->bo
.map
,
1888 return (struct gen_batch_decode_bo
) { };
1891 VkResult
anv_CreateDevice(
1892 VkPhysicalDevice physicalDevice
,
1893 const VkDeviceCreateInfo
* pCreateInfo
,
1894 const VkAllocationCallbacks
* pAllocator
,
1897 ANV_FROM_HANDLE(anv_physical_device
, physical_device
, physicalDevice
);
1899 struct anv_device
*device
;
1901 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO
);
1903 struct anv_device_extension_table enabled_extensions
= { };
1904 for (uint32_t i
= 0; i
< pCreateInfo
->enabledExtensionCount
; i
++) {
1906 for (idx
= 0; idx
< ANV_DEVICE_EXTENSION_COUNT
; idx
++) {
1907 if (strcmp(pCreateInfo
->ppEnabledExtensionNames
[i
],
1908 anv_device_extensions
[idx
].extensionName
) == 0)
1912 if (idx
>= ANV_DEVICE_EXTENSION_COUNT
)
1913 return vk_error(VK_ERROR_EXTENSION_NOT_PRESENT
);
1915 if (!physical_device
->supported_extensions
.extensions
[idx
])
1916 return vk_error(VK_ERROR_EXTENSION_NOT_PRESENT
);
1918 enabled_extensions
.extensions
[idx
] = true;
1921 /* Check enabled features */
1922 if (pCreateInfo
->pEnabledFeatures
) {
1923 VkPhysicalDeviceFeatures supported_features
;
1924 anv_GetPhysicalDeviceFeatures(physicalDevice
, &supported_features
);
1925 VkBool32
*supported_feature
= (VkBool32
*)&supported_features
;
1926 VkBool32
*enabled_feature
= (VkBool32
*)pCreateInfo
->pEnabledFeatures
;
1927 unsigned num_features
= sizeof(VkPhysicalDeviceFeatures
) / sizeof(VkBool32
);
1928 for (uint32_t i
= 0; i
< num_features
; i
++) {
1929 if (enabled_feature
[i
] && !supported_feature
[i
])
1930 return vk_error(VK_ERROR_FEATURE_NOT_PRESENT
);
1934 /* Check requested queues and fail if we are requested to create any
1935 * queues with flags we don't support.
1937 assert(pCreateInfo
->queueCreateInfoCount
> 0);
1938 for (uint32_t i
= 0; i
< pCreateInfo
->queueCreateInfoCount
; i
++) {
1939 if (pCreateInfo
->pQueueCreateInfos
[i
].flags
!= 0)
1940 return vk_error(VK_ERROR_INITIALIZATION_FAILED
);
1943 /* Check if client specified queue priority. */
1944 const VkDeviceQueueGlobalPriorityCreateInfoEXT
*queue_priority
=
1945 vk_find_struct_const(pCreateInfo
->pQueueCreateInfos
[0].pNext
,
1946 DEVICE_QUEUE_GLOBAL_PRIORITY_CREATE_INFO_EXT
);
1948 VkQueueGlobalPriorityEXT priority
=
1949 queue_priority
? queue_priority
->globalPriority
:
1950 VK_QUEUE_GLOBAL_PRIORITY_MEDIUM_EXT
;
1952 device
= vk_alloc2(&physical_device
->instance
->alloc
, pAllocator
,
1954 VK_SYSTEM_ALLOCATION_SCOPE_DEVICE
);
1956 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
1958 const unsigned decode_flags
=
1959 GEN_BATCH_DECODE_FULL
|
1960 ((INTEL_DEBUG
& DEBUG_COLOR
) ? GEN_BATCH_DECODE_IN_COLOR
: 0) |
1961 GEN_BATCH_DECODE_OFFSETS
|
1962 GEN_BATCH_DECODE_FLOATS
;
1964 gen_batch_decode_ctx_init(&device
->decoder_ctx
,
1965 &physical_device
->info
,
1966 stderr
, decode_flags
, NULL
,
1967 decode_get_bo
, NULL
, device
);
1969 device
->_loader_data
.loaderMagic
= ICD_LOADER_MAGIC
;
1970 device
->instance
= physical_device
->instance
;
1971 device
->chipset_id
= physical_device
->chipset_id
;
1972 device
->no_hw
= physical_device
->no_hw
;
1973 device
->_lost
= false;
1976 device
->alloc
= *pAllocator
;
1978 device
->alloc
= physical_device
->instance
->alloc
;
1980 /* XXX(chadv): Can we dup() physicalDevice->fd here? */
1981 device
->fd
= open(physical_device
->path
, O_RDWR
| O_CLOEXEC
);
1982 if (device
->fd
== -1) {
1983 result
= vk_error(VK_ERROR_INITIALIZATION_FAILED
);
1987 device
->context_id
= anv_gem_create_context(device
);
1988 if (device
->context_id
== -1) {
1989 result
= vk_error(VK_ERROR_INITIALIZATION_FAILED
);
1993 if (physical_device
->use_softpin
) {
1994 if (pthread_mutex_init(&device
->vma_mutex
, NULL
) != 0) {
1995 result
= vk_error(VK_ERROR_INITIALIZATION_FAILED
);
1999 /* keep the page with address zero out of the allocator */
2000 struct anv_memory_heap
*low_heap
=
2001 &physical_device
->memory
.heaps
[physical_device
->memory
.heap_count
- 1];
2002 util_vma_heap_init(&device
->vma_lo
, low_heap
->vma_start
, low_heap
->vma_size
);
2003 device
->vma_lo_available
= low_heap
->size
;
2005 struct anv_memory_heap
*high_heap
=
2006 &physical_device
->memory
.heaps
[0];
2007 util_vma_heap_init(&device
->vma_hi
, high_heap
->vma_start
, high_heap
->vma_size
);
2008 device
->vma_hi_available
= physical_device
->memory
.heap_count
== 1 ? 0 :
2012 list_inithead(&device
->memory_objects
);
2014 /* As per spec, the driver implementation may deny requests to acquire
2015 * a priority above the default priority (MEDIUM) if the caller does not
2016 * have sufficient privileges. In this scenario VK_ERROR_NOT_PERMITTED_EXT
2019 if (physical_device
->has_context_priority
) {
2020 int err
= anv_gem_set_context_param(device
->fd
, device
->context_id
,
2021 I915_CONTEXT_PARAM_PRIORITY
,
2022 vk_priority_to_gen(priority
));
2023 if (err
!= 0 && priority
> VK_QUEUE_GLOBAL_PRIORITY_MEDIUM_EXT
) {
2024 result
= vk_error(VK_ERROR_NOT_PERMITTED_EXT
);
2029 device
->info
= physical_device
->info
;
2030 device
->isl_dev
= physical_device
->isl_dev
;
2032 /* On Broadwell and later, we can use batch chaining to more efficiently
2033 * implement growing command buffers. Prior to Haswell, the kernel
2034 * command parser gets in the way and we have to fall back to growing
2037 device
->can_chain_batches
= device
->info
.gen
>= 8;
2039 device
->robust_buffer_access
= pCreateInfo
->pEnabledFeatures
&&
2040 pCreateInfo
->pEnabledFeatures
->robustBufferAccess
;
2041 device
->enabled_extensions
= enabled_extensions
;
2043 anv_device_init_dispatch(device
);
2045 if (pthread_mutex_init(&device
->mutex
, NULL
) != 0) {
2046 result
= vk_error(VK_ERROR_INITIALIZATION_FAILED
);
2047 goto fail_context_id
;
2050 pthread_condattr_t condattr
;
2051 if (pthread_condattr_init(&condattr
) != 0) {
2052 result
= vk_error(VK_ERROR_INITIALIZATION_FAILED
);
2055 if (pthread_condattr_setclock(&condattr
, CLOCK_MONOTONIC
) != 0) {
2056 pthread_condattr_destroy(&condattr
);
2057 result
= vk_error(VK_ERROR_INITIALIZATION_FAILED
);
2060 if (pthread_cond_init(&device
->queue_submit
, &condattr
) != 0) {
2061 pthread_condattr_destroy(&condattr
);
2062 result
= vk_error(VK_ERROR_INITIALIZATION_FAILED
);
2065 pthread_condattr_destroy(&condattr
);
2068 (physical_device
->supports_48bit_addresses
? EXEC_OBJECT_SUPPORTS_48B_ADDRESS
: 0) |
2069 (physical_device
->has_exec_async
? EXEC_OBJECT_ASYNC
: 0) |
2070 (physical_device
->has_exec_capture
? EXEC_OBJECT_CAPTURE
: 0) |
2071 (physical_device
->use_softpin
? EXEC_OBJECT_PINNED
: 0);
2073 anv_bo_pool_init(&device
->batch_bo_pool
, device
, bo_flags
);
2075 result
= anv_bo_cache_init(&device
->bo_cache
);
2076 if (result
!= VK_SUCCESS
)
2077 goto fail_batch_bo_pool
;
2079 if (!physical_device
->use_softpin
)
2080 bo_flags
&= ~EXEC_OBJECT_SUPPORTS_48B_ADDRESS
;
2082 result
= anv_state_pool_init(&device
->dynamic_state_pool
, device
,
2083 DYNAMIC_STATE_POOL_MIN_ADDRESS
,
2086 if (result
!= VK_SUCCESS
)
2089 result
= anv_state_pool_init(&device
->instruction_state_pool
, device
,
2090 INSTRUCTION_STATE_POOL_MIN_ADDRESS
,
2093 if (result
!= VK_SUCCESS
)
2094 goto fail_dynamic_state_pool
;
2096 result
= anv_state_pool_init(&device
->surface_state_pool
, device
,
2097 SURFACE_STATE_POOL_MIN_ADDRESS
,
2100 if (result
!= VK_SUCCESS
)
2101 goto fail_instruction_state_pool
;
2103 if (physical_device
->use_softpin
) {
2104 result
= anv_state_pool_init(&device
->binding_table_pool
, device
,
2105 BINDING_TABLE_POOL_MIN_ADDRESS
,
2108 if (result
!= VK_SUCCESS
)
2109 goto fail_surface_state_pool
;
2112 result
= anv_bo_init_new(&device
->workaround_bo
, device
, 1024);
2113 if (result
!= VK_SUCCESS
)
2114 goto fail_binding_table_pool
;
2116 if (physical_device
->use_softpin
)
2117 device
->workaround_bo
.flags
|= EXEC_OBJECT_PINNED
;
2119 if (!anv_vma_alloc(device
, &device
->workaround_bo
))
2120 goto fail_workaround_bo
;
2122 anv_device_init_trivial_batch(device
);
2124 if (device
->info
.gen
>= 10)
2125 anv_device_init_hiz_clear_value_bo(device
);
2127 anv_scratch_pool_init(device
, &device
->scratch_pool
);
2129 anv_queue_init(device
, &device
->queue
);
2131 switch (device
->info
.gen
) {
2133 if (!device
->info
.is_haswell
)
2134 result
= gen7_init_device_state(device
);
2136 result
= gen75_init_device_state(device
);
2139 result
= gen8_init_device_state(device
);
2142 result
= gen9_init_device_state(device
);
2145 result
= gen10_init_device_state(device
);
2148 result
= gen11_init_device_state(device
);
2151 /* Shouldn't get here as we don't create physical devices for any other
2153 unreachable("unhandled gen");
2155 if (result
!= VK_SUCCESS
)
2156 goto fail_workaround_bo
;
2158 anv_pipeline_cache_init(&device
->default_pipeline_cache
, device
, true);
2160 anv_device_init_blorp(device
);
2162 anv_device_init_border_colors(device
);
2164 *pDevice
= anv_device_to_handle(device
);
2169 anv_queue_finish(&device
->queue
);
2170 anv_scratch_pool_finish(device
, &device
->scratch_pool
);
2171 anv_gem_munmap(device
->workaround_bo
.map
, device
->workaround_bo
.size
);
2172 anv_gem_close(device
, device
->workaround_bo
.gem_handle
);
2173 fail_binding_table_pool
:
2174 if (physical_device
->use_softpin
)
2175 anv_state_pool_finish(&device
->binding_table_pool
);
2176 fail_surface_state_pool
:
2177 anv_state_pool_finish(&device
->surface_state_pool
);
2178 fail_instruction_state_pool
:
2179 anv_state_pool_finish(&device
->instruction_state_pool
);
2180 fail_dynamic_state_pool
:
2181 anv_state_pool_finish(&device
->dynamic_state_pool
);
2183 anv_bo_cache_finish(&device
->bo_cache
);
2185 anv_bo_pool_finish(&device
->batch_bo_pool
);
2186 pthread_cond_destroy(&device
->queue_submit
);
2188 pthread_mutex_destroy(&device
->mutex
);
2190 anv_gem_destroy_context(device
, device
->context_id
);
2194 vk_free(&device
->alloc
, device
);
2199 void anv_DestroyDevice(
2201 const VkAllocationCallbacks
* pAllocator
)
2203 ANV_FROM_HANDLE(anv_device
, device
, _device
);
2204 struct anv_physical_device
*physical_device
;
2209 physical_device
= &device
->instance
->physicalDevice
;
2211 anv_device_finish_blorp(device
);
2213 anv_pipeline_cache_finish(&device
->default_pipeline_cache
);
2215 anv_queue_finish(&device
->queue
);
2217 #ifdef HAVE_VALGRIND
2218 /* We only need to free these to prevent valgrind errors. The backing
2219 * BO will go away in a couple of lines so we don't actually leak.
2221 anv_state_pool_free(&device
->dynamic_state_pool
, device
->border_colors
);
2224 anv_scratch_pool_finish(device
, &device
->scratch_pool
);
2226 anv_gem_munmap(device
->workaround_bo
.map
, device
->workaround_bo
.size
);
2227 anv_vma_free(device
, &device
->workaround_bo
);
2228 anv_gem_close(device
, device
->workaround_bo
.gem_handle
);
2230 anv_vma_free(device
, &device
->trivial_batch_bo
);
2231 anv_gem_close(device
, device
->trivial_batch_bo
.gem_handle
);
2232 if (device
->info
.gen
>= 10)
2233 anv_gem_close(device
, device
->hiz_clear_bo
.gem_handle
);
2235 if (physical_device
->use_softpin
)
2236 anv_state_pool_finish(&device
->binding_table_pool
);
2237 anv_state_pool_finish(&device
->surface_state_pool
);
2238 anv_state_pool_finish(&device
->instruction_state_pool
);
2239 anv_state_pool_finish(&device
->dynamic_state_pool
);
2241 anv_bo_cache_finish(&device
->bo_cache
);
2243 anv_bo_pool_finish(&device
->batch_bo_pool
);
2245 pthread_cond_destroy(&device
->queue_submit
);
2246 pthread_mutex_destroy(&device
->mutex
);
2248 anv_gem_destroy_context(device
, device
->context_id
);
2250 gen_batch_decode_ctx_finish(&device
->decoder_ctx
);
2254 vk_free(&device
->alloc
, device
);
2257 VkResult
anv_EnumerateInstanceLayerProperties(
2258 uint32_t* pPropertyCount
,
2259 VkLayerProperties
* pProperties
)
2261 if (pProperties
== NULL
) {
2262 *pPropertyCount
= 0;
2266 /* None supported at this time */
2267 return vk_error(VK_ERROR_LAYER_NOT_PRESENT
);
2270 VkResult
anv_EnumerateDeviceLayerProperties(
2271 VkPhysicalDevice physicalDevice
,
2272 uint32_t* pPropertyCount
,
2273 VkLayerProperties
* pProperties
)
2275 if (pProperties
== NULL
) {
2276 *pPropertyCount
= 0;
2280 /* None supported at this time */
2281 return vk_error(VK_ERROR_LAYER_NOT_PRESENT
);
2284 void anv_GetDeviceQueue(
2286 uint32_t queueNodeIndex
,
2287 uint32_t queueIndex
,
2290 ANV_FROM_HANDLE(anv_device
, device
, _device
);
2292 assert(queueIndex
== 0);
2294 *pQueue
= anv_queue_to_handle(&device
->queue
);
2297 void anv_GetDeviceQueue2(
2299 const VkDeviceQueueInfo2
* pQueueInfo
,
2302 ANV_FROM_HANDLE(anv_device
, device
, _device
);
2304 assert(pQueueInfo
->queueIndex
== 0);
2306 if (pQueueInfo
->flags
== device
->queue
.flags
)
2307 *pQueue
= anv_queue_to_handle(&device
->queue
);
2313 _anv_device_set_lost(struct anv_device
*device
,
2314 const char *file
, int line
,
2315 const char *msg
, ...)
2320 device
->_lost
= true;
2323 err
= __vk_errorv(device
->instance
, device
,
2324 VK_DEBUG_REPORT_OBJECT_TYPE_DEVICE_EXT
,
2325 VK_ERROR_DEVICE_LOST
, file
, line
, msg
, ap
);
2328 if (env_var_as_boolean("ANV_ABORT_ON_DEVICE_LOSS", false))
2335 anv_device_query_status(struct anv_device
*device
)
2337 /* This isn't likely as most of the callers of this function already check
2338 * for it. However, it doesn't hurt to check and it potentially lets us
2341 if (anv_device_is_lost(device
))
2342 return VK_ERROR_DEVICE_LOST
;
2344 uint32_t active
, pending
;
2345 int ret
= anv_gem_gpu_get_reset_stats(device
, &active
, &pending
);
2347 /* We don't know the real error. */
2348 return anv_device_set_lost(device
, "get_reset_stats failed: %m");
2352 return anv_device_set_lost(device
, "GPU hung on one of our command buffers");
2353 } else if (pending
) {
2354 return anv_device_set_lost(device
, "GPU hung with commands in-flight");
2361 anv_device_bo_busy(struct anv_device
*device
, struct anv_bo
*bo
)
2363 /* Note: This only returns whether or not the BO is in use by an i915 GPU.
2364 * Other usages of the BO (such as on different hardware) will not be
2365 * flagged as "busy" by this ioctl. Use with care.
2367 int ret
= anv_gem_busy(device
, bo
->gem_handle
);
2369 return VK_NOT_READY
;
2370 } else if (ret
== -1) {
2371 /* We don't know the real error. */
2372 return anv_device_set_lost(device
, "gem wait failed: %m");
2375 /* Query for device status after the busy call. If the BO we're checking
2376 * got caught in a GPU hang we don't want to return VK_SUCCESS to the
2377 * client because it clearly doesn't have valid data. Yes, this most
2378 * likely means an ioctl, but we just did an ioctl to query the busy status
2379 * so it's no great loss.
2381 return anv_device_query_status(device
);
2385 anv_device_wait(struct anv_device
*device
, struct anv_bo
*bo
,
2388 int ret
= anv_gem_wait(device
, bo
->gem_handle
, &timeout
);
2389 if (ret
== -1 && errno
== ETIME
) {
2391 } else if (ret
== -1) {
2392 /* We don't know the real error. */
2393 return anv_device_set_lost(device
, "gem wait failed: %m");
2396 /* Query for device status after the wait. If the BO we're waiting on got
2397 * caught in a GPU hang we don't want to return VK_SUCCESS to the client
2398 * because it clearly doesn't have valid data. Yes, this most likely means
2399 * an ioctl, but we just did an ioctl to wait so it's no great loss.
2401 return anv_device_query_status(device
);
2404 VkResult
anv_DeviceWaitIdle(
2407 ANV_FROM_HANDLE(anv_device
, device
, _device
);
2408 if (anv_device_is_lost(device
))
2409 return VK_ERROR_DEVICE_LOST
;
2411 struct anv_batch batch
;
2414 batch
.start
= batch
.next
= cmds
;
2415 batch
.end
= (void *) cmds
+ sizeof(cmds
);
2417 anv_batch_emit(&batch
, GEN7_MI_BATCH_BUFFER_END
, bbe
);
2418 anv_batch_emit(&batch
, GEN7_MI_NOOP
, noop
);
2420 return anv_device_submit_simple_batch(device
, &batch
);
2424 anv_vma_alloc(struct anv_device
*device
, struct anv_bo
*bo
)
2426 if (!(bo
->flags
& EXEC_OBJECT_PINNED
))
2429 pthread_mutex_lock(&device
->vma_mutex
);
2433 if (bo
->flags
& EXEC_OBJECT_SUPPORTS_48B_ADDRESS
&&
2434 device
->vma_hi_available
>= bo
->size
) {
2435 uint64_t addr
= util_vma_heap_alloc(&device
->vma_hi
, bo
->size
, 4096);
2437 bo
->offset
= gen_canonical_address(addr
);
2438 assert(addr
== gen_48b_address(bo
->offset
));
2439 device
->vma_hi_available
-= bo
->size
;
2443 if (bo
->offset
== 0 && device
->vma_lo_available
>= bo
->size
) {
2444 uint64_t addr
= util_vma_heap_alloc(&device
->vma_lo
, bo
->size
, 4096);
2446 bo
->offset
= gen_canonical_address(addr
);
2447 assert(addr
== gen_48b_address(bo
->offset
));
2448 device
->vma_lo_available
-= bo
->size
;
2452 pthread_mutex_unlock(&device
->vma_mutex
);
2454 return bo
->offset
!= 0;
2458 anv_vma_free(struct anv_device
*device
, struct anv_bo
*bo
)
2460 if (!(bo
->flags
& EXEC_OBJECT_PINNED
))
2463 const uint64_t addr_48b
= gen_48b_address(bo
->offset
);
2465 pthread_mutex_lock(&device
->vma_mutex
);
2467 if (addr_48b
>= LOW_HEAP_MIN_ADDRESS
&&
2468 addr_48b
<= LOW_HEAP_MAX_ADDRESS
) {
2469 util_vma_heap_free(&device
->vma_lo
, addr_48b
, bo
->size
);
2470 device
->vma_lo_available
+= bo
->size
;
2472 MAYBE_UNUSED
const struct anv_physical_device
*physical_device
=
2473 &device
->instance
->physicalDevice
;
2474 assert(addr_48b
>= physical_device
->memory
.heaps
[0].vma_start
&&
2475 addr_48b
< (physical_device
->memory
.heaps
[0].vma_start
+
2476 physical_device
->memory
.heaps
[0].vma_size
));
2477 util_vma_heap_free(&device
->vma_hi
, addr_48b
, bo
->size
);
2478 device
->vma_hi_available
+= bo
->size
;
2481 pthread_mutex_unlock(&device
->vma_mutex
);
2487 anv_bo_init_new(struct anv_bo
*bo
, struct anv_device
*device
, uint64_t size
)
2489 uint32_t gem_handle
= anv_gem_create(device
, size
);
2491 return vk_error(VK_ERROR_OUT_OF_DEVICE_MEMORY
);
2493 anv_bo_init(bo
, gem_handle
, size
);
2498 VkResult
anv_AllocateMemory(
2500 const VkMemoryAllocateInfo
* pAllocateInfo
,
2501 const VkAllocationCallbacks
* pAllocator
,
2502 VkDeviceMemory
* pMem
)
2504 ANV_FROM_HANDLE(anv_device
, device
, _device
);
2505 struct anv_physical_device
*pdevice
= &device
->instance
->physicalDevice
;
2506 struct anv_device_memory
*mem
;
2507 VkResult result
= VK_SUCCESS
;
2509 assert(pAllocateInfo
->sType
== VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO
);
2511 /* The Vulkan 1.0.33 spec says "allocationSize must be greater than 0". */
2512 assert(pAllocateInfo
->allocationSize
> 0);
2514 if (pAllocateInfo
->allocationSize
> MAX_MEMORY_ALLOCATION_SIZE
)
2515 return VK_ERROR_OUT_OF_DEVICE_MEMORY
;
2517 /* FINISHME: Fail if allocation request exceeds heap size. */
2519 mem
= vk_alloc2(&device
->alloc
, pAllocator
, sizeof(*mem
), 8,
2520 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
2522 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
2524 assert(pAllocateInfo
->memoryTypeIndex
< pdevice
->memory
.type_count
);
2525 mem
->type
= &pdevice
->memory
.types
[pAllocateInfo
->memoryTypeIndex
];
2529 mem
->host_ptr
= NULL
;
2531 uint64_t bo_flags
= 0;
2533 assert(mem
->type
->heapIndex
< pdevice
->memory
.heap_count
);
2534 if (pdevice
->memory
.heaps
[mem
->type
->heapIndex
].supports_48bit_addresses
)
2535 bo_flags
|= EXEC_OBJECT_SUPPORTS_48B_ADDRESS
;
2537 const struct wsi_memory_allocate_info
*wsi_info
=
2538 vk_find_struct_const(pAllocateInfo
->pNext
, WSI_MEMORY_ALLOCATE_INFO_MESA
);
2539 if (wsi_info
&& wsi_info
->implicit_sync
) {
2540 /* We need to set the WRITE flag on window system buffers so that GEM
2541 * will know we're writing to them and synchronize uses on other rings
2542 * (eg if the display server uses the blitter ring).
2544 bo_flags
|= EXEC_OBJECT_WRITE
;
2545 } else if (pdevice
->has_exec_async
) {
2546 bo_flags
|= EXEC_OBJECT_ASYNC
;
2549 if (pdevice
->use_softpin
)
2550 bo_flags
|= EXEC_OBJECT_PINNED
;
2552 const VkExportMemoryAllocateInfo
*export_info
=
2553 vk_find_struct_const(pAllocateInfo
->pNext
, EXPORT_MEMORY_ALLOCATE_INFO
);
2555 /* Check if we need to support Android HW buffer export. If so,
2556 * create AHardwareBuffer and import memory from it.
2558 bool android_export
= false;
2559 if (export_info
&& export_info
->handleTypes
&
2560 VK_EXTERNAL_MEMORY_HANDLE_TYPE_ANDROID_HARDWARE_BUFFER_BIT_ANDROID
)
2561 android_export
= true;
2563 /* Android memory import. */
2564 const struct VkImportAndroidHardwareBufferInfoANDROID
*ahw_import_info
=
2565 vk_find_struct_const(pAllocateInfo
->pNext
,
2566 IMPORT_ANDROID_HARDWARE_BUFFER_INFO_ANDROID
);
2568 if (ahw_import_info
) {
2569 result
= anv_import_ahw_memory(_device
, mem
, ahw_import_info
);
2570 if (result
!= VK_SUCCESS
)
2574 } else if (android_export
) {
2575 result
= anv_create_ahw_memory(_device
, mem
, pAllocateInfo
);
2576 if (result
!= VK_SUCCESS
)
2579 const struct VkImportAndroidHardwareBufferInfoANDROID import_info
= {
2582 result
= anv_import_ahw_memory(_device
, mem
, &import_info
);
2583 if (result
!= VK_SUCCESS
)
2589 const VkImportMemoryFdInfoKHR
*fd_info
=
2590 vk_find_struct_const(pAllocateInfo
->pNext
, IMPORT_MEMORY_FD_INFO_KHR
);
2592 /* The Vulkan spec permits handleType to be 0, in which case the struct is
2595 if (fd_info
&& fd_info
->handleType
) {
2596 /* At the moment, we support only the below handle types. */
2597 assert(fd_info
->handleType
==
2598 VK_EXTERNAL_MEMORY_HANDLE_TYPE_OPAQUE_FD_BIT
||
2599 fd_info
->handleType
==
2600 VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_EXT
);
2602 result
= anv_bo_cache_import(device
, &device
->bo_cache
, fd_info
->fd
,
2603 bo_flags
| ANV_BO_EXTERNAL
, &mem
->bo
);
2604 if (result
!= VK_SUCCESS
)
2607 VkDeviceSize aligned_alloc_size
=
2608 align_u64(pAllocateInfo
->allocationSize
, 4096);
2610 /* For security purposes, we reject importing the bo if it's smaller
2611 * than the requested allocation size. This prevents a malicious client
2612 * from passing a buffer to a trusted client, lying about the size, and
2613 * telling the trusted client to try and texture from an image that goes
2614 * out-of-bounds. This sort of thing could lead to GPU hangs or worse
2615 * in the trusted client. The trusted client can protect itself against
2616 * this sort of attack but only if it can trust the buffer size.
2618 if (mem
->bo
->size
< aligned_alloc_size
) {
2619 result
= vk_errorf(device
->instance
, device
,
2620 VK_ERROR_INVALID_EXTERNAL_HANDLE
,
2621 "aligned allocationSize too large for "
2622 "VK_EXTERNAL_MEMORY_HANDLE_TYPE_OPAQUE_FD_BIT: "
2623 "%"PRIu64
"B > %"PRIu64
"B",
2624 aligned_alloc_size
, mem
->bo
->size
);
2625 anv_bo_cache_release(device
, &device
->bo_cache
, mem
->bo
);
2629 /* From the Vulkan spec:
2631 * "Importing memory from a file descriptor transfers ownership of
2632 * the file descriptor from the application to the Vulkan
2633 * implementation. The application must not perform any operations on
2634 * the file descriptor after a successful import."
2636 * If the import fails, we leave the file descriptor open.
2642 const VkImportMemoryHostPointerInfoEXT
*host_ptr_info
=
2643 vk_find_struct_const(pAllocateInfo
->pNext
,
2644 IMPORT_MEMORY_HOST_POINTER_INFO_EXT
);
2645 if (host_ptr_info
&& host_ptr_info
->handleType
) {
2646 if (host_ptr_info
->handleType
==
2647 VK_EXTERNAL_MEMORY_HANDLE_TYPE_HOST_MAPPED_FOREIGN_MEMORY_BIT_EXT
) {
2648 result
= vk_error(VK_ERROR_INVALID_EXTERNAL_HANDLE
);
2652 assert(host_ptr_info
->handleType
==
2653 VK_EXTERNAL_MEMORY_HANDLE_TYPE_HOST_ALLOCATION_BIT_EXT
);
2655 result
= anv_bo_cache_import_host_ptr(
2656 device
, &device
->bo_cache
, host_ptr_info
->pHostPointer
,
2657 pAllocateInfo
->allocationSize
, bo_flags
, &mem
->bo
);
2659 if (result
!= VK_SUCCESS
)
2662 mem
->host_ptr
= host_ptr_info
->pHostPointer
;
2666 /* Regular allocate (not importing memory). */
2668 if (export_info
&& export_info
->handleTypes
)
2669 bo_flags
|= ANV_BO_EXTERNAL
;
2671 result
= anv_bo_cache_alloc(device
, &device
->bo_cache
,
2672 pAllocateInfo
->allocationSize
, bo_flags
,
2674 if (result
!= VK_SUCCESS
)
2677 const VkMemoryDedicatedAllocateInfo
*dedicated_info
=
2678 vk_find_struct_const(pAllocateInfo
->pNext
, MEMORY_DEDICATED_ALLOCATE_INFO
);
2679 if (dedicated_info
&& dedicated_info
->image
!= VK_NULL_HANDLE
) {
2680 ANV_FROM_HANDLE(anv_image
, image
, dedicated_info
->image
);
2682 /* Some legacy (non-modifiers) consumers need the tiling to be set on
2683 * the BO. In this case, we have a dedicated allocation.
2685 if (image
->needs_set_tiling
) {
2686 const uint32_t i915_tiling
=
2687 isl_tiling_to_i915_tiling(image
->planes
[0].surface
.isl
.tiling
);
2688 int ret
= anv_gem_set_tiling(device
, mem
->bo
->gem_handle
,
2689 image
->planes
[0].surface
.isl
.row_pitch_B
,
2692 anv_bo_cache_release(device
, &device
->bo_cache
, mem
->bo
);
2693 return vk_errorf(device
->instance
, NULL
,
2694 VK_ERROR_OUT_OF_DEVICE_MEMORY
,
2695 "failed to set BO tiling: %m");
2701 pthread_mutex_lock(&device
->mutex
);
2702 list_addtail(&mem
->link
, &device
->memory_objects
);
2703 pthread_mutex_unlock(&device
->mutex
);
2705 *pMem
= anv_device_memory_to_handle(mem
);
2710 vk_free2(&device
->alloc
, pAllocator
, mem
);
2715 VkResult
anv_GetMemoryFdKHR(
2717 const VkMemoryGetFdInfoKHR
* pGetFdInfo
,
2720 ANV_FROM_HANDLE(anv_device
, dev
, device_h
);
2721 ANV_FROM_HANDLE(anv_device_memory
, mem
, pGetFdInfo
->memory
);
2723 assert(pGetFdInfo
->sType
== VK_STRUCTURE_TYPE_MEMORY_GET_FD_INFO_KHR
);
2725 assert(pGetFdInfo
->handleType
== VK_EXTERNAL_MEMORY_HANDLE_TYPE_OPAQUE_FD_BIT
||
2726 pGetFdInfo
->handleType
== VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_EXT
);
2728 return anv_bo_cache_export(dev
, &dev
->bo_cache
, mem
->bo
, pFd
);
2731 VkResult
anv_GetMemoryFdPropertiesKHR(
2733 VkExternalMemoryHandleTypeFlagBits handleType
,
2735 VkMemoryFdPropertiesKHR
* pMemoryFdProperties
)
2737 ANV_FROM_HANDLE(anv_device
, device
, _device
);
2738 struct anv_physical_device
*pdevice
= &device
->instance
->physicalDevice
;
2740 switch (handleType
) {
2741 case VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_EXT
:
2742 /* dma-buf can be imported as any memory type */
2743 pMemoryFdProperties
->memoryTypeBits
=
2744 (1 << pdevice
->memory
.type_count
) - 1;
2748 /* The valid usage section for this function says:
2750 * "handleType must not be one of the handle types defined as
2753 * So opaque handle types fall into the default "unsupported" case.
2755 return vk_error(VK_ERROR_INVALID_EXTERNAL_HANDLE
);
2759 VkResult
anv_GetMemoryHostPointerPropertiesEXT(
2761 VkExternalMemoryHandleTypeFlagBits handleType
,
2762 const void* pHostPointer
,
2763 VkMemoryHostPointerPropertiesEXT
* pMemoryHostPointerProperties
)
2765 ANV_FROM_HANDLE(anv_device
, device
, _device
);
2767 assert(pMemoryHostPointerProperties
->sType
==
2768 VK_STRUCTURE_TYPE_MEMORY_HOST_POINTER_PROPERTIES_EXT
);
2770 switch (handleType
) {
2771 case VK_EXTERNAL_MEMORY_HANDLE_TYPE_HOST_ALLOCATION_BIT_EXT
: {
2772 struct anv_physical_device
*pdevice
= &device
->instance
->physicalDevice
;
2774 /* Host memory can be imported as any memory type. */
2775 pMemoryHostPointerProperties
->memoryTypeBits
=
2776 (1ull << pdevice
->memory
.type_count
) - 1;
2781 return VK_ERROR_INVALID_EXTERNAL_HANDLE
;
2785 void anv_FreeMemory(
2787 VkDeviceMemory _mem
,
2788 const VkAllocationCallbacks
* pAllocator
)
2790 ANV_FROM_HANDLE(anv_device
, device
, _device
);
2791 ANV_FROM_HANDLE(anv_device_memory
, mem
, _mem
);
2796 pthread_mutex_lock(&device
->mutex
);
2797 list_del(&mem
->link
);
2798 pthread_mutex_unlock(&device
->mutex
);
2801 anv_UnmapMemory(_device
, _mem
);
2803 anv_bo_cache_release(device
, &device
->bo_cache
, mem
->bo
);
2805 #if defined(ANDROID) && ANDROID_API_LEVEL >= 26
2807 AHardwareBuffer_release(mem
->ahw
);
2810 vk_free2(&device
->alloc
, pAllocator
, mem
);
2813 VkResult
anv_MapMemory(
2815 VkDeviceMemory _memory
,
2816 VkDeviceSize offset
,
2818 VkMemoryMapFlags flags
,
2821 ANV_FROM_HANDLE(anv_device
, device
, _device
);
2822 ANV_FROM_HANDLE(anv_device_memory
, mem
, _memory
);
2829 if (mem
->host_ptr
) {
2830 *ppData
= mem
->host_ptr
+ offset
;
2834 if (size
== VK_WHOLE_SIZE
)
2835 size
= mem
->bo
->size
- offset
;
2837 /* From the Vulkan spec version 1.0.32 docs for MapMemory:
2839 * * If size is not equal to VK_WHOLE_SIZE, size must be greater than 0
2840 * assert(size != 0);
2841 * * If size is not equal to VK_WHOLE_SIZE, size must be less than or
2842 * equal to the size of the memory minus offset
2845 assert(offset
+ size
<= mem
->bo
->size
);
2847 /* FIXME: Is this supposed to be thread safe? Since vkUnmapMemory() only
2848 * takes a VkDeviceMemory pointer, it seems like only one map of the memory
2849 * at a time is valid. We could just mmap up front and return an offset
2850 * pointer here, but that may exhaust virtual memory on 32 bit
2853 uint32_t gem_flags
= 0;
2855 if (!device
->info
.has_llc
&&
2856 (mem
->type
->propertyFlags
& VK_MEMORY_PROPERTY_HOST_COHERENT_BIT
))
2857 gem_flags
|= I915_MMAP_WC
;
2859 /* GEM will fail to map if the offset isn't 4k-aligned. Round down. */
2860 uint64_t map_offset
= offset
& ~4095ull;
2861 assert(offset
>= map_offset
);
2862 uint64_t map_size
= (offset
+ size
) - map_offset
;
2864 /* Let's map whole pages */
2865 map_size
= align_u64(map_size
, 4096);
2867 void *map
= anv_gem_mmap(device
, mem
->bo
->gem_handle
,
2868 map_offset
, map_size
, gem_flags
);
2869 if (map
== MAP_FAILED
)
2870 return vk_error(VK_ERROR_MEMORY_MAP_FAILED
);
2873 mem
->map_size
= map_size
;
2875 *ppData
= mem
->map
+ (offset
- map_offset
);
2880 void anv_UnmapMemory(
2882 VkDeviceMemory _memory
)
2884 ANV_FROM_HANDLE(anv_device_memory
, mem
, _memory
);
2886 if (mem
== NULL
|| mem
->host_ptr
)
2889 anv_gem_munmap(mem
->map
, mem
->map_size
);
2896 clflush_mapped_ranges(struct anv_device
*device
,
2898 const VkMappedMemoryRange
*ranges
)
2900 for (uint32_t i
= 0; i
< count
; i
++) {
2901 ANV_FROM_HANDLE(anv_device_memory
, mem
, ranges
[i
].memory
);
2902 if (ranges
[i
].offset
>= mem
->map_size
)
2905 gen_clflush_range(mem
->map
+ ranges
[i
].offset
,
2906 MIN2(ranges
[i
].size
, mem
->map_size
- ranges
[i
].offset
));
2910 VkResult
anv_FlushMappedMemoryRanges(
2912 uint32_t memoryRangeCount
,
2913 const VkMappedMemoryRange
* pMemoryRanges
)
2915 ANV_FROM_HANDLE(anv_device
, device
, _device
);
2917 if (device
->info
.has_llc
)
2920 /* Make sure the writes we're flushing have landed. */
2921 __builtin_ia32_mfence();
2923 clflush_mapped_ranges(device
, memoryRangeCount
, pMemoryRanges
);
2928 VkResult
anv_InvalidateMappedMemoryRanges(
2930 uint32_t memoryRangeCount
,
2931 const VkMappedMemoryRange
* pMemoryRanges
)
2933 ANV_FROM_HANDLE(anv_device
, device
, _device
);
2935 if (device
->info
.has_llc
)
2938 clflush_mapped_ranges(device
, memoryRangeCount
, pMemoryRanges
);
2940 /* Make sure no reads get moved up above the invalidate. */
2941 __builtin_ia32_mfence();
2946 void anv_GetBufferMemoryRequirements(
2949 VkMemoryRequirements
* pMemoryRequirements
)
2951 ANV_FROM_HANDLE(anv_buffer
, buffer
, _buffer
);
2952 ANV_FROM_HANDLE(anv_device
, device
, _device
);
2953 struct anv_physical_device
*pdevice
= &device
->instance
->physicalDevice
;
2955 /* The Vulkan spec (git aaed022) says:
2957 * memoryTypeBits is a bitfield and contains one bit set for every
2958 * supported memory type for the resource. The bit `1<<i` is set if and
2959 * only if the memory type `i` in the VkPhysicalDeviceMemoryProperties
2960 * structure for the physical device is supported.
2962 uint32_t memory_types
= 0;
2963 for (uint32_t i
= 0; i
< pdevice
->memory
.type_count
; i
++) {
2964 uint32_t valid_usage
= pdevice
->memory
.types
[i
].valid_buffer_usage
;
2965 if ((valid_usage
& buffer
->usage
) == buffer
->usage
)
2966 memory_types
|= (1u << i
);
2969 /* Base alignment requirement of a cache line */
2970 uint32_t alignment
= 16;
2972 /* We need an alignment of 32 for pushing UBOs */
2973 if (buffer
->usage
& VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT
)
2974 alignment
= MAX2(alignment
, 32);
2976 pMemoryRequirements
->size
= buffer
->size
;
2977 pMemoryRequirements
->alignment
= alignment
;
2979 /* Storage and Uniform buffers should have their size aligned to
2980 * 32-bits to avoid boundary checks when last DWord is not complete.
2981 * This would ensure that not internal padding would be needed for
2984 if (device
->robust_buffer_access
&&
2985 (buffer
->usage
& VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT
||
2986 buffer
->usage
& VK_BUFFER_USAGE_STORAGE_BUFFER_BIT
))
2987 pMemoryRequirements
->size
= align_u64(buffer
->size
, 4);
2989 pMemoryRequirements
->memoryTypeBits
= memory_types
;
2992 void anv_GetBufferMemoryRequirements2(
2994 const VkBufferMemoryRequirementsInfo2
* pInfo
,
2995 VkMemoryRequirements2
* pMemoryRequirements
)
2997 anv_GetBufferMemoryRequirements(_device
, pInfo
->buffer
,
2998 &pMemoryRequirements
->memoryRequirements
);
3000 vk_foreach_struct(ext
, pMemoryRequirements
->pNext
) {
3001 switch (ext
->sType
) {
3002 case VK_STRUCTURE_TYPE_MEMORY_DEDICATED_REQUIREMENTS
: {
3003 VkMemoryDedicatedRequirements
*requirements
= (void *)ext
;
3004 requirements
->prefersDedicatedAllocation
= false;
3005 requirements
->requiresDedicatedAllocation
= false;
3010 anv_debug_ignored_stype(ext
->sType
);
3016 void anv_GetImageMemoryRequirements(
3019 VkMemoryRequirements
* pMemoryRequirements
)
3021 ANV_FROM_HANDLE(anv_image
, image
, _image
);
3022 ANV_FROM_HANDLE(anv_device
, device
, _device
);
3023 struct anv_physical_device
*pdevice
= &device
->instance
->physicalDevice
;
3025 /* The Vulkan spec (git aaed022) says:
3027 * memoryTypeBits is a bitfield and contains one bit set for every
3028 * supported memory type for the resource. The bit `1<<i` is set if and
3029 * only if the memory type `i` in the VkPhysicalDeviceMemoryProperties
3030 * structure for the physical device is supported.
3032 * All types are currently supported for images.
3034 uint32_t memory_types
= (1ull << pdevice
->memory
.type_count
) - 1;
3036 /* We must have image allocated or imported at this point. According to the
3037 * specification, external images must have been bound to memory before
3038 * calling GetImageMemoryRequirements.
3040 assert(image
->size
> 0);
3042 pMemoryRequirements
->size
= image
->size
;
3043 pMemoryRequirements
->alignment
= image
->alignment
;
3044 pMemoryRequirements
->memoryTypeBits
= memory_types
;
3047 void anv_GetImageMemoryRequirements2(
3049 const VkImageMemoryRequirementsInfo2
* pInfo
,
3050 VkMemoryRequirements2
* pMemoryRequirements
)
3052 ANV_FROM_HANDLE(anv_device
, device
, _device
);
3053 ANV_FROM_HANDLE(anv_image
, image
, pInfo
->image
);
3055 anv_GetImageMemoryRequirements(_device
, pInfo
->image
,
3056 &pMemoryRequirements
->memoryRequirements
);
3058 vk_foreach_struct_const(ext
, pInfo
->pNext
) {
3059 switch (ext
->sType
) {
3060 case VK_STRUCTURE_TYPE_IMAGE_PLANE_MEMORY_REQUIREMENTS_INFO
: {
3061 struct anv_physical_device
*pdevice
= &device
->instance
->physicalDevice
;
3062 const VkImagePlaneMemoryRequirementsInfo
*plane_reqs
=
3063 (const VkImagePlaneMemoryRequirementsInfo
*) ext
;
3064 uint32_t plane
= anv_image_aspect_to_plane(image
->aspects
,
3065 plane_reqs
->planeAspect
);
3067 assert(image
->planes
[plane
].offset
== 0);
3069 /* The Vulkan spec (git aaed022) says:
3071 * memoryTypeBits is a bitfield and contains one bit set for every
3072 * supported memory type for the resource. The bit `1<<i` is set
3073 * if and only if the memory type `i` in the
3074 * VkPhysicalDeviceMemoryProperties structure for the physical
3075 * device is supported.
3077 * All types are currently supported for images.
3079 pMemoryRequirements
->memoryRequirements
.memoryTypeBits
=
3080 (1ull << pdevice
->memory
.type_count
) - 1;
3082 /* We must have image allocated or imported at this point. According to the
3083 * specification, external images must have been bound to memory before
3084 * calling GetImageMemoryRequirements.
3086 assert(image
->planes
[plane
].size
> 0);
3088 pMemoryRequirements
->memoryRequirements
.size
= image
->planes
[plane
].size
;
3089 pMemoryRequirements
->memoryRequirements
.alignment
=
3090 image
->planes
[plane
].alignment
;
3095 anv_debug_ignored_stype(ext
->sType
);
3100 vk_foreach_struct(ext
, pMemoryRequirements
->pNext
) {
3101 switch (ext
->sType
) {
3102 case VK_STRUCTURE_TYPE_MEMORY_DEDICATED_REQUIREMENTS
: {
3103 VkMemoryDedicatedRequirements
*requirements
= (void *)ext
;
3104 if (image
->needs_set_tiling
|| image
->external_format
) {
3105 /* If we need to set the tiling for external consumers, we need a
3106 * dedicated allocation.
3108 * See also anv_AllocateMemory.
3110 requirements
->prefersDedicatedAllocation
= true;
3111 requirements
->requiresDedicatedAllocation
= true;
3113 requirements
->prefersDedicatedAllocation
= false;
3114 requirements
->requiresDedicatedAllocation
= false;
3120 anv_debug_ignored_stype(ext
->sType
);
3126 void anv_GetImageSparseMemoryRequirements(
3129 uint32_t* pSparseMemoryRequirementCount
,
3130 VkSparseImageMemoryRequirements
* pSparseMemoryRequirements
)
3132 *pSparseMemoryRequirementCount
= 0;
3135 void anv_GetImageSparseMemoryRequirements2(
3137 const VkImageSparseMemoryRequirementsInfo2
* pInfo
,
3138 uint32_t* pSparseMemoryRequirementCount
,
3139 VkSparseImageMemoryRequirements2
* pSparseMemoryRequirements
)
3141 *pSparseMemoryRequirementCount
= 0;
3144 void anv_GetDeviceMemoryCommitment(
3146 VkDeviceMemory memory
,
3147 VkDeviceSize
* pCommittedMemoryInBytes
)
3149 *pCommittedMemoryInBytes
= 0;
3153 anv_bind_buffer_memory(const VkBindBufferMemoryInfo
*pBindInfo
)
3155 ANV_FROM_HANDLE(anv_device_memory
, mem
, pBindInfo
->memory
);
3156 ANV_FROM_HANDLE(anv_buffer
, buffer
, pBindInfo
->buffer
);
3158 assert(pBindInfo
->sType
== VK_STRUCTURE_TYPE_BIND_BUFFER_MEMORY_INFO
);
3161 assert((buffer
->usage
& mem
->type
->valid_buffer_usage
) == buffer
->usage
);
3162 buffer
->address
= (struct anv_address
) {
3164 .offset
= pBindInfo
->memoryOffset
,
3167 buffer
->address
= ANV_NULL_ADDRESS
;
3171 VkResult
anv_BindBufferMemory(
3174 VkDeviceMemory memory
,
3175 VkDeviceSize memoryOffset
)
3177 anv_bind_buffer_memory(
3178 &(VkBindBufferMemoryInfo
) {
3179 .sType
= VK_STRUCTURE_TYPE_BIND_BUFFER_MEMORY_INFO
,
3182 .memoryOffset
= memoryOffset
,
3188 VkResult
anv_BindBufferMemory2(
3190 uint32_t bindInfoCount
,
3191 const VkBindBufferMemoryInfo
* pBindInfos
)
3193 for (uint32_t i
= 0; i
< bindInfoCount
; i
++)
3194 anv_bind_buffer_memory(&pBindInfos
[i
]);
3199 VkResult
anv_QueueBindSparse(
3201 uint32_t bindInfoCount
,
3202 const VkBindSparseInfo
* pBindInfo
,
3205 ANV_FROM_HANDLE(anv_queue
, queue
, _queue
);
3206 if (anv_device_is_lost(queue
->device
))
3207 return VK_ERROR_DEVICE_LOST
;
3209 return vk_error(VK_ERROR_FEATURE_NOT_PRESENT
);
3214 VkResult
anv_CreateEvent(
3216 const VkEventCreateInfo
* pCreateInfo
,
3217 const VkAllocationCallbacks
* pAllocator
,
3220 ANV_FROM_HANDLE(anv_device
, device
, _device
);
3221 struct anv_state state
;
3222 struct anv_event
*event
;
3224 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_EVENT_CREATE_INFO
);
3226 state
= anv_state_pool_alloc(&device
->dynamic_state_pool
,
3229 event
->state
= state
;
3230 event
->semaphore
= VK_EVENT_RESET
;
3232 if (!device
->info
.has_llc
) {
3233 /* Make sure the writes we're flushing have landed. */
3234 __builtin_ia32_mfence();
3235 __builtin_ia32_clflush(event
);
3238 *pEvent
= anv_event_to_handle(event
);
3243 void anv_DestroyEvent(
3246 const VkAllocationCallbacks
* pAllocator
)
3248 ANV_FROM_HANDLE(anv_device
, device
, _device
);
3249 ANV_FROM_HANDLE(anv_event
, event
, _event
);
3254 anv_state_pool_free(&device
->dynamic_state_pool
, event
->state
);
3257 VkResult
anv_GetEventStatus(
3261 ANV_FROM_HANDLE(anv_device
, device
, _device
);
3262 ANV_FROM_HANDLE(anv_event
, event
, _event
);
3264 if (anv_device_is_lost(device
))
3265 return VK_ERROR_DEVICE_LOST
;
3267 if (!device
->info
.has_llc
) {
3268 /* Invalidate read cache before reading event written by GPU. */
3269 __builtin_ia32_clflush(event
);
3270 __builtin_ia32_mfence();
3274 return event
->semaphore
;
3277 VkResult
anv_SetEvent(
3281 ANV_FROM_HANDLE(anv_device
, device
, _device
);
3282 ANV_FROM_HANDLE(anv_event
, event
, _event
);
3284 event
->semaphore
= VK_EVENT_SET
;
3286 if (!device
->info
.has_llc
) {
3287 /* Make sure the writes we're flushing have landed. */
3288 __builtin_ia32_mfence();
3289 __builtin_ia32_clflush(event
);
3295 VkResult
anv_ResetEvent(
3299 ANV_FROM_HANDLE(anv_device
, device
, _device
);
3300 ANV_FROM_HANDLE(anv_event
, event
, _event
);
3302 event
->semaphore
= VK_EVENT_RESET
;
3304 if (!device
->info
.has_llc
) {
3305 /* Make sure the writes we're flushing have landed. */
3306 __builtin_ia32_mfence();
3307 __builtin_ia32_clflush(event
);
3315 VkResult
anv_CreateBuffer(
3317 const VkBufferCreateInfo
* pCreateInfo
,
3318 const VkAllocationCallbacks
* pAllocator
,
3321 ANV_FROM_HANDLE(anv_device
, device
, _device
);
3322 struct anv_buffer
*buffer
;
3324 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO
);
3326 buffer
= vk_alloc2(&device
->alloc
, pAllocator
, sizeof(*buffer
), 8,
3327 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
3329 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
3331 buffer
->size
= pCreateInfo
->size
;
3332 buffer
->usage
= pCreateInfo
->usage
;
3333 buffer
->address
= ANV_NULL_ADDRESS
;
3335 *pBuffer
= anv_buffer_to_handle(buffer
);
3340 void anv_DestroyBuffer(
3343 const VkAllocationCallbacks
* pAllocator
)
3345 ANV_FROM_HANDLE(anv_device
, device
, _device
);
3346 ANV_FROM_HANDLE(anv_buffer
, buffer
, _buffer
);
3351 vk_free2(&device
->alloc
, pAllocator
, buffer
);
3354 VkDeviceAddress
anv_GetBufferDeviceAddressEXT(
3356 const VkBufferDeviceAddressInfoEXT
* pInfo
)
3358 ANV_FROM_HANDLE(anv_buffer
, buffer
, pInfo
->buffer
);
3360 assert(buffer
->address
.bo
->flags
& EXEC_OBJECT_PINNED
);
3362 return anv_address_physical(buffer
->address
);
3366 anv_fill_buffer_surface_state(struct anv_device
*device
, struct anv_state state
,
3367 enum isl_format format
,
3368 struct anv_address address
,
3369 uint32_t range
, uint32_t stride
)
3371 isl_buffer_fill_state(&device
->isl_dev
, state
.map
,
3372 .address
= anv_address_physical(address
),
3373 .mocs
= device
->default_mocs
,
3376 .swizzle
= ISL_SWIZZLE_IDENTITY
,
3377 .stride_B
= stride
);
3380 void anv_DestroySampler(
3383 const VkAllocationCallbacks
* pAllocator
)
3385 ANV_FROM_HANDLE(anv_device
, device
, _device
);
3386 ANV_FROM_HANDLE(anv_sampler
, sampler
, _sampler
);
3391 vk_free2(&device
->alloc
, pAllocator
, sampler
);
3394 VkResult
anv_CreateFramebuffer(
3396 const VkFramebufferCreateInfo
* pCreateInfo
,
3397 const VkAllocationCallbacks
* pAllocator
,
3398 VkFramebuffer
* pFramebuffer
)
3400 ANV_FROM_HANDLE(anv_device
, device
, _device
);
3401 struct anv_framebuffer
*framebuffer
;
3403 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO
);
3405 size_t size
= sizeof(*framebuffer
) +
3406 sizeof(struct anv_image_view
*) * pCreateInfo
->attachmentCount
;
3407 framebuffer
= vk_alloc2(&device
->alloc
, pAllocator
, size
, 8,
3408 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
3409 if (framebuffer
== NULL
)
3410 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
3412 framebuffer
->attachment_count
= pCreateInfo
->attachmentCount
;
3413 for (uint32_t i
= 0; i
< pCreateInfo
->attachmentCount
; i
++) {
3414 VkImageView _iview
= pCreateInfo
->pAttachments
[i
];
3415 framebuffer
->attachments
[i
] = anv_image_view_from_handle(_iview
);
3418 framebuffer
->width
= pCreateInfo
->width
;
3419 framebuffer
->height
= pCreateInfo
->height
;
3420 framebuffer
->layers
= pCreateInfo
->layers
;
3422 *pFramebuffer
= anv_framebuffer_to_handle(framebuffer
);
3427 void anv_DestroyFramebuffer(
3430 const VkAllocationCallbacks
* pAllocator
)
3432 ANV_FROM_HANDLE(anv_device
, device
, _device
);
3433 ANV_FROM_HANDLE(anv_framebuffer
, fb
, _fb
);
3438 vk_free2(&device
->alloc
, pAllocator
, fb
);
3441 static const VkTimeDomainEXT anv_time_domains
[] = {
3442 VK_TIME_DOMAIN_DEVICE_EXT
,
3443 VK_TIME_DOMAIN_CLOCK_MONOTONIC_EXT
,
3444 VK_TIME_DOMAIN_CLOCK_MONOTONIC_RAW_EXT
,
3447 VkResult
anv_GetPhysicalDeviceCalibrateableTimeDomainsEXT(
3448 VkPhysicalDevice physicalDevice
,
3449 uint32_t *pTimeDomainCount
,
3450 VkTimeDomainEXT
*pTimeDomains
)
3453 VK_OUTARRAY_MAKE(out
, pTimeDomains
, pTimeDomainCount
);
3455 for (d
= 0; d
< ARRAY_SIZE(anv_time_domains
); d
++) {
3456 vk_outarray_append(&out
, i
) {
3457 *i
= anv_time_domains
[d
];
3461 return vk_outarray_status(&out
);
3465 anv_clock_gettime(clockid_t clock_id
)
3467 struct timespec current
;
3470 ret
= clock_gettime(clock_id
, ¤t
);
3471 if (ret
< 0 && clock_id
== CLOCK_MONOTONIC_RAW
)
3472 ret
= clock_gettime(CLOCK_MONOTONIC
, ¤t
);
3476 return (uint64_t) current
.tv_sec
* 1000000000ULL + current
.tv_nsec
;
3479 #define TIMESTAMP 0x2358
3481 VkResult
anv_GetCalibratedTimestampsEXT(
3483 uint32_t timestampCount
,
3484 const VkCalibratedTimestampInfoEXT
*pTimestampInfos
,
3485 uint64_t *pTimestamps
,
3486 uint64_t *pMaxDeviation
)
3488 ANV_FROM_HANDLE(anv_device
, device
, _device
);
3489 uint64_t timestamp_frequency
= device
->info
.timestamp_frequency
;
3492 uint64_t begin
, end
;
3493 uint64_t max_clock_period
= 0;
3495 begin
= anv_clock_gettime(CLOCK_MONOTONIC_RAW
);
3497 for (d
= 0; d
< timestampCount
; d
++) {
3498 switch (pTimestampInfos
[d
].timeDomain
) {
3499 case VK_TIME_DOMAIN_DEVICE_EXT
:
3500 ret
= anv_gem_reg_read(device
, TIMESTAMP
| 1,
3504 return anv_device_set_lost(device
, "Failed to read the TIMESTAMP "
3507 uint64_t device_period
= DIV_ROUND_UP(1000000000, timestamp_frequency
);
3508 max_clock_period
= MAX2(max_clock_period
, device_period
);
3510 case VK_TIME_DOMAIN_CLOCK_MONOTONIC_EXT
:
3511 pTimestamps
[d
] = anv_clock_gettime(CLOCK_MONOTONIC
);
3512 max_clock_period
= MAX2(max_clock_period
, 1);
3515 case VK_TIME_DOMAIN_CLOCK_MONOTONIC_RAW_EXT
:
3516 pTimestamps
[d
] = begin
;
3524 end
= anv_clock_gettime(CLOCK_MONOTONIC_RAW
);
3527 * The maximum deviation is the sum of the interval over which we
3528 * perform the sampling and the maximum period of any sampled
3529 * clock. That's because the maximum skew between any two sampled
3530 * clock edges is when the sampled clock with the largest period is
3531 * sampled at the end of that period but right at the beginning of the
3532 * sampling interval and some other clock is sampled right at the
3533 * begining of its sampling period and right at the end of the
3534 * sampling interval. Let's assume the GPU has the longest clock
3535 * period and that the application is sampling GPU and monotonic:
3538 * w x y z 0 1 2 3 4 5 6 7 8 9 a b c d e f
3539 * Raw -_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-
3543 * GPU -----_____-----_____-----_____-----_____
3546 * x y z 0 1 2 3 4 5 6 7 8 9 a b c
3547 * Monotonic -_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-
3549 * Interval <----------------->
3550 * Deviation <-------------------------->
3554 * m = read(monotonic) 2
3557 * We round the sample interval up by one tick to cover sampling error
3558 * in the interval clock
3561 uint64_t sample_interval
= end
- begin
+ 1;
3563 *pMaxDeviation
= sample_interval
+ max_clock_period
;
3568 /* vk_icd.h does not declare this function, so we declare it here to
3569 * suppress Wmissing-prototypes.
3571 PUBLIC VKAPI_ATTR VkResult VKAPI_CALL
3572 vk_icdNegotiateLoaderICDInterfaceVersion(uint32_t* pSupportedVersion
);
3574 PUBLIC VKAPI_ATTR VkResult VKAPI_CALL
3575 vk_icdNegotiateLoaderICDInterfaceVersion(uint32_t* pSupportedVersion
)
3577 /* For the full details on loader interface versioning, see
3578 * <https://github.com/KhronosGroup/Vulkan-LoaderAndValidationLayers/blob/master/loader/LoaderAndLayerInterface.md>.
3579 * What follows is a condensed summary, to help you navigate the large and
3580 * confusing official doc.
3582 * - Loader interface v0 is incompatible with later versions. We don't
3585 * - In loader interface v1:
3586 * - The first ICD entrypoint called by the loader is
3587 * vk_icdGetInstanceProcAddr(). The ICD must statically expose this
3589 * - The ICD must statically expose no other Vulkan symbol unless it is
3590 * linked with -Bsymbolic.
3591 * - Each dispatchable Vulkan handle created by the ICD must be
3592 * a pointer to a struct whose first member is VK_LOADER_DATA. The
3593 * ICD must initialize VK_LOADER_DATA.loadMagic to ICD_LOADER_MAGIC.
3594 * - The loader implements vkCreate{PLATFORM}SurfaceKHR() and
3595 * vkDestroySurfaceKHR(). The ICD must be capable of working with
3596 * such loader-managed surfaces.
3598 * - Loader interface v2 differs from v1 in:
3599 * - The first ICD entrypoint called by the loader is
3600 * vk_icdNegotiateLoaderICDInterfaceVersion(). The ICD must
3601 * statically expose this entrypoint.
3603 * - Loader interface v3 differs from v2 in:
3604 * - The ICD must implement vkCreate{PLATFORM}SurfaceKHR(),
3605 * vkDestroySurfaceKHR(), and other API which uses VKSurfaceKHR,
3606 * because the loader no longer does so.
3608 *pSupportedVersion
= MIN2(*pSupportedVersion
, 3u);