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/os_file.h"
41 #include "util/u_atomic.h"
42 #include "util/u_string.h"
43 #include "util/xmlpool.h"
46 #include "common/gen_defines.h"
47 #include "compiler/glsl_types.h"
49 #include "genxml/gen7_pack.h"
51 static const char anv_dri_options_xml
[] =
55 /* This is probably far to big but it reflects the max size used for messages
56 * in OpenGLs KHR_debug.
58 #define MAX_DEBUG_MESSAGE_LENGTH 4096
61 compiler_debug_log(void *data
, const char *fmt
, ...)
63 char str
[MAX_DEBUG_MESSAGE_LENGTH
];
64 struct anv_device
*device
= (struct anv_device
*)data
;
66 if (list_empty(&device
->instance
->debug_report_callbacks
.callbacks
))
71 (void) vsnprintf(str
, MAX_DEBUG_MESSAGE_LENGTH
, fmt
, args
);
74 vk_debug_report(&device
->instance
->debug_report_callbacks
,
75 VK_DEBUG_REPORT_DEBUG_BIT_EXT
,
76 VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT
,
81 compiler_perf_log(void *data
, const char *fmt
, ...)
86 if (unlikely(INTEL_DEBUG
& DEBUG_PERF
))
87 intel_logd_v(fmt
, args
);
93 anv_compute_heap_size(int fd
, uint64_t gtt_size
)
95 /* Query the total ram from the system */
99 uint64_t total_ram
= (uint64_t)info
.totalram
* (uint64_t)info
.mem_unit
;
101 /* We don't want to burn too much ram with the GPU. If the user has 4GiB
102 * or less, we use at most half. If they have more than 4GiB, we use 3/4.
104 uint64_t available_ram
;
105 if (total_ram
<= 4ull * 1024ull * 1024ull * 1024ull)
106 available_ram
= total_ram
/ 2;
108 available_ram
= total_ram
* 3 / 4;
110 /* We also want to leave some padding for things we allocate in the driver,
111 * so don't go over 3/4 of the GTT either.
113 uint64_t available_gtt
= gtt_size
* 3 / 4;
115 return MIN2(available_ram
, available_gtt
);
119 anv_physical_device_init_heaps(struct anv_physical_device
*device
, int fd
)
122 if (anv_gem_get_context_param(fd
, 0, I915_CONTEXT_PARAM_GTT_SIZE
,
124 /* If, for whatever reason, we can't actually get the GTT size from the
125 * kernel (too old?) fall back to the aperture size.
127 anv_perf_warn(NULL
, NULL
,
128 "Failed to get I915_CONTEXT_PARAM_GTT_SIZE: %m");
130 if (anv_gem_get_aperture(fd
, >t_size
) == -1) {
131 return vk_errorf(NULL
, NULL
, VK_ERROR_INITIALIZATION_FAILED
,
132 "failed to get aperture size: %m");
136 device
->supports_48bit_addresses
= (device
->info
.gen
>= 8) &&
137 gtt_size
> (4ULL << 30 /* GiB */);
139 uint64_t heap_size
= anv_compute_heap_size(fd
, gtt_size
);
141 if (heap_size
> (2ull << 30) && !device
->supports_48bit_addresses
) {
142 /* When running with an overridden PCI ID, we may get a GTT size from
143 * the kernel that is greater than 2 GiB but the execbuf check for 48bit
144 * address support can still fail. Just clamp the address space size to
145 * 2 GiB if we don't have 48-bit support.
147 intel_logw("%s:%d: The kernel reported a GTT size larger than 2 GiB but "
148 "not support for 48-bit addresses",
150 heap_size
= 2ull << 30;
153 if (heap_size
<= 3ull * (1ull << 30)) {
154 /* In this case, everything fits nicely into the 32-bit address space,
155 * so there's no need for supporting 48bit addresses on client-allocated
158 device
->memory
.heap_count
= 1;
159 device
->memory
.heaps
[0] = (struct anv_memory_heap
) {
160 .vma_start
= LOW_HEAP_MIN_ADDRESS
,
161 .vma_size
= LOW_HEAP_SIZE
,
163 .flags
= VK_MEMORY_HEAP_DEVICE_LOCAL_BIT
,
164 .supports_48bit_addresses
= false,
167 /* Not everything will fit nicely into a 32-bit address space. In this
168 * case we need a 64-bit heap. Advertise a small 32-bit heap and a
169 * larger 48-bit heap. If we're in this case, then we have a total heap
170 * size larger than 3GiB which most likely means they have 8 GiB of
171 * video memory and so carving off 1 GiB for the 32-bit heap should be
174 const uint64_t heap_size_32bit
= 1ull << 30;
175 const uint64_t heap_size_48bit
= heap_size
- heap_size_32bit
;
177 assert(device
->supports_48bit_addresses
);
179 device
->memory
.heap_count
= 2;
180 device
->memory
.heaps
[0] = (struct anv_memory_heap
) {
181 .vma_start
= HIGH_HEAP_MIN_ADDRESS
,
182 /* Leave the last 4GiB out of the high vma range, so that no state
183 * base address + size can overflow 48 bits. For more information see
184 * the comment about Wa32bitGeneralStateOffset in anv_allocator.c
186 .vma_size
= gtt_size
- (1ull << 32) - HIGH_HEAP_MIN_ADDRESS
,
187 .size
= heap_size_48bit
,
188 .flags
= VK_MEMORY_HEAP_DEVICE_LOCAL_BIT
,
189 .supports_48bit_addresses
= true,
191 device
->memory
.heaps
[1] = (struct anv_memory_heap
) {
192 .vma_start
= LOW_HEAP_MIN_ADDRESS
,
193 .vma_size
= LOW_HEAP_SIZE
,
194 .size
= heap_size_32bit
,
195 .flags
= VK_MEMORY_HEAP_DEVICE_LOCAL_BIT
,
196 .supports_48bit_addresses
= false,
200 uint32_t type_count
= 0;
201 for (uint32_t heap
= 0; heap
< device
->memory
.heap_count
; heap
++) {
202 uint32_t valid_buffer_usage
= ~0;
204 /* There appears to be a hardware issue in the VF cache where it only
205 * considers the bottom 32 bits of memory addresses. If you happen to
206 * have two vertex buffers which get placed exactly 4 GiB apart and use
207 * them in back-to-back draw calls, you can get collisions. In order to
208 * solve this problem, we require vertex and index buffers be bound to
209 * memory allocated out of the 32-bit heap.
211 if (device
->memory
.heaps
[heap
].supports_48bit_addresses
) {
212 valid_buffer_usage
&= ~(VK_BUFFER_USAGE_INDEX_BUFFER_BIT
|
213 VK_BUFFER_USAGE_VERTEX_BUFFER_BIT
);
216 if (device
->info
.has_llc
) {
217 /* Big core GPUs share LLC with the CPU and thus one memory type can be
218 * both cached and coherent at the same time.
220 device
->memory
.types
[type_count
++] = (struct anv_memory_type
) {
221 .propertyFlags
= VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT
|
222 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT
|
223 VK_MEMORY_PROPERTY_HOST_COHERENT_BIT
|
224 VK_MEMORY_PROPERTY_HOST_CACHED_BIT
,
226 .valid_buffer_usage
= valid_buffer_usage
,
229 /* The spec requires that we expose a host-visible, coherent memory
230 * type, but Atom GPUs don't share LLC. Thus we offer two memory types
231 * to give the application a choice between cached, but not coherent and
232 * coherent but uncached (WC though).
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_COHERENT_BIT
,
239 .valid_buffer_usage
= valid_buffer_usage
,
241 device
->memory
.types
[type_count
++] = (struct anv_memory_type
) {
242 .propertyFlags
= VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT
|
243 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT
|
244 VK_MEMORY_PROPERTY_HOST_CACHED_BIT
,
246 .valid_buffer_usage
= valid_buffer_usage
,
250 device
->memory
.type_count
= type_count
;
256 anv_physical_device_init_uuids(struct anv_physical_device
*device
)
258 const struct build_id_note
*note
=
259 build_id_find_nhdr_for_addr(anv_physical_device_init_uuids
);
261 return vk_errorf(device
->instance
, device
,
262 VK_ERROR_INITIALIZATION_FAILED
,
263 "Failed to find build-id");
266 unsigned build_id_len
= build_id_length(note
);
267 if (build_id_len
< 20) {
268 return vk_errorf(device
->instance
, device
,
269 VK_ERROR_INITIALIZATION_FAILED
,
270 "build-id too short. It needs to be a SHA");
273 memcpy(device
->driver_build_sha1
, build_id_data(note
), 20);
275 struct mesa_sha1 sha1_ctx
;
277 STATIC_ASSERT(VK_UUID_SIZE
<= sizeof(sha1
));
279 /* The pipeline cache UUID is used for determining when a pipeline cache is
280 * invalid. It needs both a driver build and the PCI ID of the device.
282 _mesa_sha1_init(&sha1_ctx
);
283 _mesa_sha1_update(&sha1_ctx
, build_id_data(note
), build_id_len
);
284 _mesa_sha1_update(&sha1_ctx
, &device
->chipset_id
,
285 sizeof(device
->chipset_id
));
286 _mesa_sha1_update(&sha1_ctx
, &device
->always_use_bindless
,
287 sizeof(device
->always_use_bindless
));
288 _mesa_sha1_update(&sha1_ctx
, &device
->has_a64_buffer_access
,
289 sizeof(device
->has_a64_buffer_access
));
290 _mesa_sha1_update(&sha1_ctx
, &device
->has_bindless_images
,
291 sizeof(device
->has_bindless_images
));
292 _mesa_sha1_update(&sha1_ctx
, &device
->has_bindless_samplers
,
293 sizeof(device
->has_bindless_samplers
));
294 _mesa_sha1_final(&sha1_ctx
, sha1
);
295 memcpy(device
->pipeline_cache_uuid
, sha1
, VK_UUID_SIZE
);
297 /* The driver UUID is used for determining sharability of images and memory
298 * between two Vulkan instances in separate processes. People who want to
299 * share memory need to also check the device UUID (below) so all this
300 * needs to be is the build-id.
302 memcpy(device
->driver_uuid
, build_id_data(note
), VK_UUID_SIZE
);
304 /* The device UUID uniquely identifies the given device within the machine.
305 * Since we never have more than one device, this doesn't need to be a real
306 * UUID. However, on the off-chance that someone tries to use this to
307 * cache pre-tiled images or something of the like, we use the PCI ID and
308 * some bits of ISL info to ensure that this is safe.
310 _mesa_sha1_init(&sha1_ctx
);
311 _mesa_sha1_update(&sha1_ctx
, &device
->chipset_id
,
312 sizeof(device
->chipset_id
));
313 _mesa_sha1_update(&sha1_ctx
, &device
->isl_dev
.has_bit6_swizzling
,
314 sizeof(device
->isl_dev
.has_bit6_swizzling
));
315 _mesa_sha1_final(&sha1_ctx
, sha1
);
316 memcpy(device
->device_uuid
, sha1
, VK_UUID_SIZE
);
322 anv_physical_device_init_disk_cache(struct anv_physical_device
*device
)
324 #ifdef ENABLE_SHADER_CACHE
326 ASSERTED
int len
= snprintf(renderer
, sizeof(renderer
), "anv_%04x",
328 assert(len
== sizeof(renderer
) - 2);
331 _mesa_sha1_format(timestamp
, device
->driver_build_sha1
);
333 const uint64_t driver_flags
=
334 brw_get_compiler_config_value(device
->compiler
);
335 device
->disk_cache
= disk_cache_create(renderer
, timestamp
, driver_flags
);
337 device
->disk_cache
= NULL
;
342 anv_physical_device_free_disk_cache(struct anv_physical_device
*device
)
344 #ifdef ENABLE_SHADER_CACHE
345 if (device
->disk_cache
)
346 disk_cache_destroy(device
->disk_cache
);
348 assert(device
->disk_cache
== NULL
);
353 get_available_system_memory()
355 char *meminfo
= os_read_file("/proc/meminfo");
359 char *str
= strstr(meminfo
, "MemAvailable:");
365 uint64_t kb_mem_available
;
366 if (sscanf(str
, "MemAvailable: %" PRIx64
, &kb_mem_available
) == 1) {
368 return kb_mem_available
<< 10;
376 anv_physical_device_init(struct anv_physical_device
*device
,
377 struct anv_instance
*instance
,
378 drmDevicePtr drm_device
)
380 const char *primary_path
= drm_device
->nodes
[DRM_NODE_PRIMARY
];
381 const char *path
= drm_device
->nodes
[DRM_NODE_RENDER
];
386 brw_process_intel_debug_variable();
388 fd
= open(path
, O_RDWR
| O_CLOEXEC
);
390 return vk_error(VK_ERROR_INCOMPATIBLE_DRIVER
);
392 device
->_loader_data
.loaderMagic
= ICD_LOADER_MAGIC
;
393 device
->instance
= instance
;
395 assert(strlen(path
) < ARRAY_SIZE(device
->path
));
396 snprintf(device
->path
, ARRAY_SIZE(device
->path
), "%s", path
);
398 if (!gen_get_device_info_from_fd(fd
, &device
->info
)) {
399 result
= vk_error(VK_ERROR_INCOMPATIBLE_DRIVER
);
402 device
->chipset_id
= device
->info
.chipset_id
;
403 device
->no_hw
= device
->info
.no_hw
;
405 if (getenv("INTEL_NO_HW") != NULL
)
406 device
->no_hw
= true;
408 device
->pci_info
.domain
= drm_device
->businfo
.pci
->domain
;
409 device
->pci_info
.bus
= drm_device
->businfo
.pci
->bus
;
410 device
->pci_info
.device
= drm_device
->businfo
.pci
->dev
;
411 device
->pci_info
.function
= drm_device
->businfo
.pci
->func
;
413 device
->name
= gen_get_device_name(device
->chipset_id
);
415 if (device
->info
.is_haswell
) {
416 intel_logw("Haswell Vulkan support is incomplete");
417 } else if (device
->info
.gen
== 7 && !device
->info
.is_baytrail
) {
418 intel_logw("Ivy Bridge Vulkan support is incomplete");
419 } else if (device
->info
.gen
== 7 && device
->info
.is_baytrail
) {
420 intel_logw("Bay Trail Vulkan support is incomplete");
421 } else if (device
->info
.gen
>= 8 && device
->info
.gen
<= 11) {
422 /* Gen8-11 fully supported */
423 } else if (device
->info
.gen
== 12) {
424 intel_logw("Vulkan is not yet fully supported on gen12");
426 result
= vk_errorf(device
->instance
, device
,
427 VK_ERROR_INCOMPATIBLE_DRIVER
,
428 "Vulkan not yet supported on %s", device
->name
);
432 device
->cmd_parser_version
= -1;
433 if (device
->info
.gen
== 7) {
434 device
->cmd_parser_version
=
435 anv_gem_get_param(fd
, I915_PARAM_CMD_PARSER_VERSION
);
436 if (device
->cmd_parser_version
== -1) {
437 result
= vk_errorf(device
->instance
, device
,
438 VK_ERROR_INITIALIZATION_FAILED
,
439 "failed to get command parser version");
444 if (!anv_gem_get_param(fd
, I915_PARAM_HAS_WAIT_TIMEOUT
)) {
445 result
= vk_errorf(device
->instance
, device
,
446 VK_ERROR_INITIALIZATION_FAILED
,
447 "kernel missing gem wait");
451 if (!anv_gem_get_param(fd
, I915_PARAM_HAS_EXECBUF2
)) {
452 result
= vk_errorf(device
->instance
, device
,
453 VK_ERROR_INITIALIZATION_FAILED
,
454 "kernel missing execbuf2");
458 if (!device
->info
.has_llc
&&
459 anv_gem_get_param(fd
, I915_PARAM_MMAP_VERSION
) < 1) {
460 result
= vk_errorf(device
->instance
, device
,
461 VK_ERROR_INITIALIZATION_FAILED
,
462 "kernel missing wc mmap");
466 result
= anv_physical_device_init_heaps(device
, fd
);
467 if (result
!= VK_SUCCESS
)
470 device
->has_exec_async
= anv_gem_get_param(fd
, I915_PARAM_HAS_EXEC_ASYNC
);
471 device
->has_exec_capture
= anv_gem_get_param(fd
, I915_PARAM_HAS_EXEC_CAPTURE
);
472 device
->has_exec_fence
= anv_gem_get_param(fd
, I915_PARAM_HAS_EXEC_FENCE
);
473 device
->has_syncobj
= anv_gem_get_param(fd
, I915_PARAM_HAS_EXEC_FENCE_ARRAY
);
474 device
->has_syncobj_wait
= device
->has_syncobj
&&
475 anv_gem_supports_syncobj_wait(fd
);
476 device
->has_context_priority
= anv_gem_has_context_priority(fd
);
478 device
->use_softpin
= anv_gem_get_param(fd
, I915_PARAM_HAS_EXEC_SOFTPIN
)
479 && device
->supports_48bit_addresses
;
481 device
->has_context_isolation
=
482 anv_gem_get_param(fd
, I915_PARAM_HAS_CONTEXT_ISOLATION
);
484 device
->always_use_bindless
=
485 env_var_as_boolean("ANV_ALWAYS_BINDLESS", false);
487 /* We first got the A64 messages on broadwell and we can only use them if
488 * we can pass addresses directly into the shader which requires softpin.
490 device
->has_a64_buffer_access
= device
->info
.gen
>= 8 &&
493 /* We first get bindless image access on Skylake and we can only really do
494 * it if we don't have any relocations so we need softpin.
496 device
->has_bindless_images
= device
->info
.gen
>= 9 &&
499 /* We've had bindless samplers since Ivy Bridge (forever in Vulkan terms)
500 * because it's just a matter of setting the sampler address in the sample
501 * message header. However, we've not bothered to wire it up for vec4 so
502 * we leave it disabled on gen7.
504 device
->has_bindless_samplers
= device
->info
.gen
>= 8;
506 device
->has_mem_available
= get_available_system_memory() != 0;
508 /* Starting with Gen10, the timestamp frequency of the command streamer may
509 * vary from one part to another. We can query the value from the kernel.
511 if (device
->info
.gen
>= 10) {
512 int timestamp_frequency
=
513 anv_gem_get_param(fd
, I915_PARAM_CS_TIMESTAMP_FREQUENCY
);
515 if (timestamp_frequency
< 0)
516 intel_logw("Kernel 4.16-rc1+ required to properly query CS timestamp frequency");
518 device
->info
.timestamp_frequency
= timestamp_frequency
;
521 /* GENs prior to 8 do not support EU/Subslice info */
522 if (device
->info
.gen
>= 8) {
523 device
->subslice_total
= anv_gem_get_param(fd
, I915_PARAM_SUBSLICE_TOTAL
);
524 device
->eu_total
= anv_gem_get_param(fd
, I915_PARAM_EU_TOTAL
);
526 /* Without this information, we cannot get the right Braswell
527 * brandstrings, and we have to use conservative numbers for GPGPU on
528 * many platforms, but otherwise, things will just work.
530 if (device
->subslice_total
< 1 || device
->eu_total
< 1) {
531 intel_logw("Kernel 4.1 required to properly query GPU properties");
533 } else if (device
->info
.gen
== 7) {
534 device
->subslice_total
= 1 << (device
->info
.gt
- 1);
537 if (device
->info
.is_cherryview
&&
538 device
->subslice_total
> 0 && device
->eu_total
> 0) {
539 /* Logical CS threads = EUs per subslice * num threads per EU */
540 uint32_t max_cs_threads
=
541 device
->eu_total
/ device
->subslice_total
* device
->info
.num_thread_per_eu
;
543 /* Fuse configurations may give more threads than expected, never less. */
544 if (max_cs_threads
> device
->info
.max_cs_threads
)
545 device
->info
.max_cs_threads
= max_cs_threads
;
548 device
->compiler
= brw_compiler_create(NULL
, &device
->info
);
549 if (device
->compiler
== NULL
) {
550 result
= vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
553 device
->compiler
->shader_debug_log
= compiler_debug_log
;
554 device
->compiler
->shader_perf_log
= compiler_perf_log
;
555 device
->compiler
->supports_pull_constants
= false;
556 device
->compiler
->constant_buffer_0_is_relative
=
557 device
->info
.gen
< 8 || !device
->has_context_isolation
;
558 device
->compiler
->supports_shader_constants
= true;
560 /* Broadwell PRM says:
562 * "Before Gen8, there was a historical configuration control field to
563 * swizzle address bit[6] for in X/Y tiling modes. This was set in three
564 * different places: TILECTL[1:0], ARB_MODE[5:4], and
565 * DISP_ARB_CTL[14:13].
567 * For Gen8 and subsequent generations, the swizzle fields are all
568 * reserved, and the CPU's memory controller performs all address
569 * swizzling modifications."
572 device
->info
.gen
< 8 && anv_gem_get_bit6_swizzle(fd
, I915_TILING_X
);
574 isl_device_init(&device
->isl_dev
, &device
->info
, swizzled
);
576 result
= anv_physical_device_init_uuids(device
);
577 if (result
!= VK_SUCCESS
)
580 anv_physical_device_init_disk_cache(device
);
582 if (instance
->enabled_extensions
.KHR_display
) {
583 master_fd
= open(primary_path
, O_RDWR
| O_CLOEXEC
);
584 if (master_fd
>= 0) {
585 /* prod the device with a GETPARAM call which will fail if
586 * we don't have permission to even render on this device
588 if (anv_gem_get_param(master_fd
, I915_PARAM_CHIPSET_ID
) == 0) {
594 device
->master_fd
= master_fd
;
596 result
= anv_init_wsi(device
);
597 if (result
!= VK_SUCCESS
) {
598 ralloc_free(device
->compiler
);
599 anv_physical_device_free_disk_cache(device
);
603 anv_physical_device_get_supported_extensions(device
,
604 &device
->supported_extensions
);
607 device
->local_fd
= fd
;
619 anv_physical_device_finish(struct anv_physical_device
*device
)
621 anv_finish_wsi(device
);
622 anv_physical_device_free_disk_cache(device
);
623 ralloc_free(device
->compiler
);
624 close(device
->local_fd
);
625 if (device
->master_fd
>= 0)
626 close(device
->master_fd
);
630 default_alloc_func(void *pUserData
, size_t size
, size_t align
,
631 VkSystemAllocationScope allocationScope
)
637 default_realloc_func(void *pUserData
, void *pOriginal
, size_t size
,
638 size_t align
, VkSystemAllocationScope allocationScope
)
640 return realloc(pOriginal
, size
);
644 default_free_func(void *pUserData
, void *pMemory
)
649 static const VkAllocationCallbacks default_alloc
= {
651 .pfnAllocation
= default_alloc_func
,
652 .pfnReallocation
= default_realloc_func
,
653 .pfnFree
= default_free_func
,
656 VkResult
anv_EnumerateInstanceExtensionProperties(
657 const char* pLayerName
,
658 uint32_t* pPropertyCount
,
659 VkExtensionProperties
* pProperties
)
661 VK_OUTARRAY_MAKE(out
, pProperties
, pPropertyCount
);
663 for (int i
= 0; i
< ANV_INSTANCE_EXTENSION_COUNT
; i
++) {
664 if (anv_instance_extensions_supported
.extensions
[i
]) {
665 vk_outarray_append(&out
, prop
) {
666 *prop
= anv_instance_extensions
[i
];
671 return vk_outarray_status(&out
);
674 VkResult
anv_CreateInstance(
675 const VkInstanceCreateInfo
* pCreateInfo
,
676 const VkAllocationCallbacks
* pAllocator
,
677 VkInstance
* pInstance
)
679 struct anv_instance
*instance
;
682 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO
);
684 struct anv_instance_extension_table enabled_extensions
= {};
685 for (uint32_t i
= 0; i
< pCreateInfo
->enabledExtensionCount
; i
++) {
687 for (idx
= 0; idx
< ANV_INSTANCE_EXTENSION_COUNT
; idx
++) {
688 if (strcmp(pCreateInfo
->ppEnabledExtensionNames
[i
],
689 anv_instance_extensions
[idx
].extensionName
) == 0)
693 if (idx
>= ANV_INSTANCE_EXTENSION_COUNT
)
694 return vk_error(VK_ERROR_EXTENSION_NOT_PRESENT
);
696 if (!anv_instance_extensions_supported
.extensions
[idx
])
697 return vk_error(VK_ERROR_EXTENSION_NOT_PRESENT
);
699 enabled_extensions
.extensions
[idx
] = true;
702 instance
= vk_alloc2(&default_alloc
, pAllocator
, sizeof(*instance
), 8,
703 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE
);
705 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
707 instance
->_loader_data
.loaderMagic
= ICD_LOADER_MAGIC
;
710 instance
->alloc
= *pAllocator
;
712 instance
->alloc
= default_alloc
;
714 instance
->app_info
= (struct anv_app_info
) { .api_version
= 0 };
715 if (pCreateInfo
->pApplicationInfo
) {
716 const VkApplicationInfo
*app
= pCreateInfo
->pApplicationInfo
;
718 instance
->app_info
.app_name
=
719 vk_strdup(&instance
->alloc
, app
->pApplicationName
,
720 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE
);
721 instance
->app_info
.app_version
= app
->applicationVersion
;
723 instance
->app_info
.engine_name
=
724 vk_strdup(&instance
->alloc
, app
->pEngineName
,
725 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE
);
726 instance
->app_info
.engine_version
= app
->engineVersion
;
728 instance
->app_info
.api_version
= app
->apiVersion
;
731 if (instance
->app_info
.api_version
== 0)
732 instance
->app_info
.api_version
= VK_API_VERSION_1_0
;
734 instance
->enabled_extensions
= enabled_extensions
;
736 for (unsigned i
= 0; i
< ARRAY_SIZE(instance
->dispatch
.entrypoints
); i
++) {
737 /* Vulkan requires that entrypoints for extensions which have not been
738 * enabled must not be advertised.
740 if (!anv_instance_entrypoint_is_enabled(i
, instance
->app_info
.api_version
,
741 &instance
->enabled_extensions
)) {
742 instance
->dispatch
.entrypoints
[i
] = NULL
;
744 instance
->dispatch
.entrypoints
[i
] =
745 anv_instance_dispatch_table
.entrypoints
[i
];
749 for (unsigned i
= 0; i
< ARRAY_SIZE(instance
->device_dispatch
.entrypoints
); i
++) {
750 /* Vulkan requires that entrypoints for extensions which have not been
751 * enabled must not be advertised.
753 if (!anv_device_entrypoint_is_enabled(i
, instance
->app_info
.api_version
,
754 &instance
->enabled_extensions
, NULL
)) {
755 instance
->device_dispatch
.entrypoints
[i
] = NULL
;
757 instance
->device_dispatch
.entrypoints
[i
] =
758 anv_device_dispatch_table
.entrypoints
[i
];
762 instance
->physicalDeviceCount
= -1;
764 result
= vk_debug_report_instance_init(&instance
->debug_report_callbacks
);
765 if (result
!= VK_SUCCESS
) {
766 vk_free2(&default_alloc
, pAllocator
, instance
);
767 return vk_error(result
);
770 instance
->pipeline_cache_enabled
=
771 env_var_as_boolean("ANV_ENABLE_PIPELINE_CACHE", true);
774 glsl_type_singleton_init_or_ref();
776 VG(VALGRIND_CREATE_MEMPOOL(instance
, 0, false));
778 driParseOptionInfo(&instance
->available_dri_options
, anv_dri_options_xml
);
779 driParseConfigFiles(&instance
->dri_options
, &instance
->available_dri_options
,
782 *pInstance
= anv_instance_to_handle(instance
);
787 void anv_DestroyInstance(
788 VkInstance _instance
,
789 const VkAllocationCallbacks
* pAllocator
)
791 ANV_FROM_HANDLE(anv_instance
, instance
, _instance
);
796 if (instance
->physicalDeviceCount
> 0) {
797 /* We support at most one physical device. */
798 assert(instance
->physicalDeviceCount
== 1);
799 anv_physical_device_finish(&instance
->physicalDevice
);
802 vk_free(&instance
->alloc
, (char *)instance
->app_info
.app_name
);
803 vk_free(&instance
->alloc
, (char *)instance
->app_info
.engine_name
);
805 VG(VALGRIND_DESTROY_MEMPOOL(instance
));
807 vk_debug_report_instance_destroy(&instance
->debug_report_callbacks
);
809 glsl_type_singleton_decref();
812 driDestroyOptionCache(&instance
->dri_options
);
813 driDestroyOptionInfo(&instance
->available_dri_options
);
815 vk_free(&instance
->alloc
, instance
);
819 anv_enumerate_devices(struct anv_instance
*instance
)
821 /* TODO: Check for more devices ? */
822 drmDevicePtr devices
[8];
823 VkResult result
= VK_ERROR_INCOMPATIBLE_DRIVER
;
826 instance
->physicalDeviceCount
= 0;
828 max_devices
= drmGetDevices2(0, devices
, ARRAY_SIZE(devices
));
830 return VK_ERROR_INCOMPATIBLE_DRIVER
;
832 for (unsigned i
= 0; i
< (unsigned)max_devices
; i
++) {
833 if (devices
[i
]->available_nodes
& 1 << DRM_NODE_RENDER
&&
834 devices
[i
]->bustype
== DRM_BUS_PCI
&&
835 devices
[i
]->deviceinfo
.pci
->vendor_id
== 0x8086) {
837 result
= anv_physical_device_init(&instance
->physicalDevice
,
838 instance
, devices
[i
]);
839 if (result
!= VK_ERROR_INCOMPATIBLE_DRIVER
)
843 drmFreeDevices(devices
, max_devices
);
845 if (result
== VK_SUCCESS
)
846 instance
->physicalDeviceCount
= 1;
852 anv_instance_ensure_physical_device(struct anv_instance
*instance
)
854 if (instance
->physicalDeviceCount
< 0) {
855 VkResult result
= anv_enumerate_devices(instance
);
856 if (result
!= VK_SUCCESS
&&
857 result
!= VK_ERROR_INCOMPATIBLE_DRIVER
)
864 VkResult
anv_EnumeratePhysicalDevices(
865 VkInstance _instance
,
866 uint32_t* pPhysicalDeviceCount
,
867 VkPhysicalDevice
* pPhysicalDevices
)
869 ANV_FROM_HANDLE(anv_instance
, instance
, _instance
);
870 VK_OUTARRAY_MAKE(out
, pPhysicalDevices
, pPhysicalDeviceCount
);
872 VkResult result
= anv_instance_ensure_physical_device(instance
);
873 if (result
!= VK_SUCCESS
)
876 if (instance
->physicalDeviceCount
== 0)
879 assert(instance
->physicalDeviceCount
== 1);
880 vk_outarray_append(&out
, i
) {
881 *i
= anv_physical_device_to_handle(&instance
->physicalDevice
);
884 return vk_outarray_status(&out
);
887 VkResult
anv_EnumeratePhysicalDeviceGroups(
888 VkInstance _instance
,
889 uint32_t* pPhysicalDeviceGroupCount
,
890 VkPhysicalDeviceGroupProperties
* pPhysicalDeviceGroupProperties
)
892 ANV_FROM_HANDLE(anv_instance
, instance
, _instance
);
893 VK_OUTARRAY_MAKE(out
, pPhysicalDeviceGroupProperties
,
894 pPhysicalDeviceGroupCount
);
896 VkResult result
= anv_instance_ensure_physical_device(instance
);
897 if (result
!= VK_SUCCESS
)
900 if (instance
->physicalDeviceCount
== 0)
903 assert(instance
->physicalDeviceCount
== 1);
905 vk_outarray_append(&out
, p
) {
906 p
->physicalDeviceCount
= 1;
907 memset(p
->physicalDevices
, 0, sizeof(p
->physicalDevices
));
908 p
->physicalDevices
[0] =
909 anv_physical_device_to_handle(&instance
->physicalDevice
);
910 p
->subsetAllocation
= false;
912 vk_foreach_struct(ext
, p
->pNext
)
913 anv_debug_ignored_stype(ext
->sType
);
916 return vk_outarray_status(&out
);
919 void anv_GetPhysicalDeviceFeatures(
920 VkPhysicalDevice physicalDevice
,
921 VkPhysicalDeviceFeatures
* pFeatures
)
923 ANV_FROM_HANDLE(anv_physical_device
, pdevice
, physicalDevice
);
925 *pFeatures
= (VkPhysicalDeviceFeatures
) {
926 .robustBufferAccess
= true,
927 .fullDrawIndexUint32
= true,
928 .imageCubeArray
= true,
929 .independentBlend
= true,
930 .geometryShader
= true,
931 .tessellationShader
= true,
932 .sampleRateShading
= true,
933 .dualSrcBlend
= true,
935 .multiDrawIndirect
= true,
936 .drawIndirectFirstInstance
= true,
938 .depthBiasClamp
= true,
939 .fillModeNonSolid
= true,
940 .depthBounds
= false,
944 .multiViewport
= true,
945 .samplerAnisotropy
= true,
946 .textureCompressionETC2
= pdevice
->info
.gen
>= 8 ||
947 pdevice
->info
.is_baytrail
,
948 .textureCompressionASTC_LDR
= pdevice
->info
.gen
>= 9, /* FINISHME CHV */
949 .textureCompressionBC
= true,
950 .occlusionQueryPrecise
= true,
951 .pipelineStatisticsQuery
= true,
952 .fragmentStoresAndAtomics
= true,
953 .shaderTessellationAndGeometryPointSize
= true,
954 .shaderImageGatherExtended
= true,
955 .shaderStorageImageExtendedFormats
= true,
956 .shaderStorageImageMultisample
= false,
957 .shaderStorageImageReadWithoutFormat
= false,
958 .shaderStorageImageWriteWithoutFormat
= true,
959 .shaderUniformBufferArrayDynamicIndexing
= true,
960 .shaderSampledImageArrayDynamicIndexing
= true,
961 .shaderStorageBufferArrayDynamicIndexing
= true,
962 .shaderStorageImageArrayDynamicIndexing
= true,
963 .shaderClipDistance
= true,
964 .shaderCullDistance
= true,
965 .shaderFloat64
= pdevice
->info
.gen
>= 8 &&
966 pdevice
->info
.has_64bit_types
,
967 .shaderInt64
= pdevice
->info
.gen
>= 8 &&
968 pdevice
->info
.has_64bit_types
,
969 .shaderInt16
= pdevice
->info
.gen
>= 8,
970 .shaderResourceMinLod
= pdevice
->info
.gen
>= 9,
971 .variableMultisampleRate
= true,
972 .inheritedQueries
= true,
975 /* We can't do image stores in vec4 shaders */
976 pFeatures
->vertexPipelineStoresAndAtomics
=
977 pdevice
->compiler
->scalar_stage
[MESA_SHADER_VERTEX
] &&
978 pdevice
->compiler
->scalar_stage
[MESA_SHADER_GEOMETRY
];
980 struct anv_app_info
*app_info
= &pdevice
->instance
->app_info
;
982 /* The new DOOM and Wolfenstein games require depthBounds without
983 * checking for it. They seem to run fine without it so just claim it's
984 * there and accept the consequences.
986 if (app_info
->engine_name
&& strcmp(app_info
->engine_name
, "idTech") == 0)
987 pFeatures
->depthBounds
= true;
990 void anv_GetPhysicalDeviceFeatures2(
991 VkPhysicalDevice physicalDevice
,
992 VkPhysicalDeviceFeatures2
* pFeatures
)
994 ANV_FROM_HANDLE(anv_physical_device
, pdevice
, physicalDevice
);
995 anv_GetPhysicalDeviceFeatures(physicalDevice
, &pFeatures
->features
);
997 vk_foreach_struct(ext
, pFeatures
->pNext
) {
998 switch (ext
->sType
) {
999 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_8BIT_STORAGE_FEATURES_KHR
: {
1000 VkPhysicalDevice8BitStorageFeaturesKHR
*features
=
1001 (VkPhysicalDevice8BitStorageFeaturesKHR
*)ext
;
1002 features
->storageBuffer8BitAccess
= pdevice
->info
.gen
>= 8;
1003 features
->uniformAndStorageBuffer8BitAccess
= pdevice
->info
.gen
>= 8;
1004 features
->storagePushConstant8
= pdevice
->info
.gen
>= 8;
1008 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_16BIT_STORAGE_FEATURES
: {
1009 VkPhysicalDevice16BitStorageFeatures
*features
=
1010 (VkPhysicalDevice16BitStorageFeatures
*)ext
;
1011 features
->storageBuffer16BitAccess
= pdevice
->info
.gen
>= 8;
1012 features
->uniformAndStorageBuffer16BitAccess
= pdevice
->info
.gen
>= 8;
1013 features
->storagePushConstant16
= pdevice
->info
.gen
>= 8;
1014 features
->storageInputOutput16
= false;
1018 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_BUFFER_DEVICE_ADDRESS_FEATURES_EXT
: {
1019 VkPhysicalDeviceBufferDeviceAddressFeaturesEXT
*features
= (void *)ext
;
1020 features
->bufferDeviceAddress
= pdevice
->has_a64_buffer_access
;
1021 features
->bufferDeviceAddressCaptureReplay
= false;
1022 features
->bufferDeviceAddressMultiDevice
= false;
1026 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_COMPUTE_SHADER_DERIVATIVES_FEATURES_NV
: {
1027 VkPhysicalDeviceComputeShaderDerivativesFeaturesNV
*features
=
1028 (VkPhysicalDeviceComputeShaderDerivativesFeaturesNV
*)ext
;
1029 features
->computeDerivativeGroupQuads
= true;
1030 features
->computeDerivativeGroupLinear
= true;
1034 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_CONDITIONAL_RENDERING_FEATURES_EXT
: {
1035 VkPhysicalDeviceConditionalRenderingFeaturesEXT
*features
=
1036 (VkPhysicalDeviceConditionalRenderingFeaturesEXT
*)ext
;
1037 features
->conditionalRendering
= pdevice
->info
.gen
>= 8 ||
1038 pdevice
->info
.is_haswell
;
1039 features
->inheritedConditionalRendering
= pdevice
->info
.gen
>= 8 ||
1040 pdevice
->info
.is_haswell
;
1044 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_DEPTH_CLIP_ENABLE_FEATURES_EXT
: {
1045 VkPhysicalDeviceDepthClipEnableFeaturesEXT
*features
=
1046 (VkPhysicalDeviceDepthClipEnableFeaturesEXT
*)ext
;
1047 features
->depthClipEnable
= true;
1051 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FLOAT16_INT8_FEATURES_KHR
: {
1052 VkPhysicalDeviceFloat16Int8FeaturesKHR
*features
= (void *)ext
;
1053 features
->shaderFloat16
= pdevice
->info
.gen
>= 8;
1054 features
->shaderInt8
= pdevice
->info
.gen
>= 8;
1058 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FRAGMENT_SHADER_INTERLOCK_FEATURES_EXT
: {
1059 VkPhysicalDeviceFragmentShaderInterlockFeaturesEXT
*features
=
1060 (VkPhysicalDeviceFragmentShaderInterlockFeaturesEXT
*)ext
;
1061 features
->fragmentShaderSampleInterlock
= pdevice
->info
.gen
>= 9;
1062 features
->fragmentShaderPixelInterlock
= pdevice
->info
.gen
>= 9;
1063 features
->fragmentShaderShadingRateInterlock
= false;
1067 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_HOST_QUERY_RESET_FEATURES_EXT
: {
1068 VkPhysicalDeviceHostQueryResetFeaturesEXT
*features
=
1069 (VkPhysicalDeviceHostQueryResetFeaturesEXT
*)ext
;
1070 features
->hostQueryReset
= true;
1074 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_DESCRIPTOR_INDEXING_FEATURES_EXT
: {
1075 VkPhysicalDeviceDescriptorIndexingFeaturesEXT
*features
=
1076 (VkPhysicalDeviceDescriptorIndexingFeaturesEXT
*)ext
;
1077 features
->shaderInputAttachmentArrayDynamicIndexing
= false;
1078 features
->shaderUniformTexelBufferArrayDynamicIndexing
= true;
1079 features
->shaderStorageTexelBufferArrayDynamicIndexing
= true;
1080 features
->shaderUniformBufferArrayNonUniformIndexing
= false;
1081 features
->shaderSampledImageArrayNonUniformIndexing
= true;
1082 features
->shaderStorageBufferArrayNonUniformIndexing
= true;
1083 features
->shaderStorageImageArrayNonUniformIndexing
= true;
1084 features
->shaderInputAttachmentArrayNonUniformIndexing
= false;
1085 features
->shaderUniformTexelBufferArrayNonUniformIndexing
= true;
1086 features
->shaderStorageTexelBufferArrayNonUniformIndexing
= true;
1087 features
->descriptorBindingUniformBufferUpdateAfterBind
= false;
1088 features
->descriptorBindingSampledImageUpdateAfterBind
= true;
1089 features
->descriptorBindingStorageImageUpdateAfterBind
= true;
1090 features
->descriptorBindingStorageBufferUpdateAfterBind
= true;
1091 features
->descriptorBindingUniformTexelBufferUpdateAfterBind
= true;
1092 features
->descriptorBindingStorageTexelBufferUpdateAfterBind
= true;
1093 features
->descriptorBindingUpdateUnusedWhilePending
= true;
1094 features
->descriptorBindingPartiallyBound
= true;
1095 features
->descriptorBindingVariableDescriptorCount
= false;
1096 features
->runtimeDescriptorArray
= true;
1100 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_INDEX_TYPE_UINT8_FEATURES_EXT
: {
1101 VkPhysicalDeviceIndexTypeUint8FeaturesEXT
*features
=
1102 (VkPhysicalDeviceIndexTypeUint8FeaturesEXT
*)ext
;
1103 features
->indexTypeUint8
= true;
1107 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_INLINE_UNIFORM_BLOCK_FEATURES_EXT
: {
1108 VkPhysicalDeviceInlineUniformBlockFeaturesEXT
*features
=
1109 (VkPhysicalDeviceInlineUniformBlockFeaturesEXT
*)ext
;
1110 features
->inlineUniformBlock
= true;
1111 features
->descriptorBindingInlineUniformBlockUpdateAfterBind
= true;
1115 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_LINE_RASTERIZATION_FEATURES_EXT
: {
1116 VkPhysicalDeviceLineRasterizationFeaturesEXT
*features
=
1117 (VkPhysicalDeviceLineRasterizationFeaturesEXT
*)ext
;
1118 features
->rectangularLines
= true;
1119 features
->bresenhamLines
= true;
1120 features
->smoothLines
= true;
1121 features
->stippledRectangularLines
= false;
1122 features
->stippledBresenhamLines
= true;
1123 features
->stippledSmoothLines
= false;
1127 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_MULTIVIEW_FEATURES
: {
1128 VkPhysicalDeviceMultiviewFeatures
*features
=
1129 (VkPhysicalDeviceMultiviewFeatures
*)ext
;
1130 features
->multiview
= true;
1131 features
->multiviewGeometryShader
= true;
1132 features
->multiviewTessellationShader
= true;
1136 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_IMAGELESS_FRAMEBUFFER_FEATURES_KHR
: {
1137 VkPhysicalDeviceImagelessFramebufferFeaturesKHR
*features
=
1138 (VkPhysicalDeviceImagelessFramebufferFeaturesKHR
*)ext
;
1139 features
->imagelessFramebuffer
= true;
1143 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PIPELINE_EXECUTABLE_PROPERTIES_FEATURES_KHR
: {
1144 VkPhysicalDevicePipelineExecutablePropertiesFeaturesKHR
*features
=
1145 (VkPhysicalDevicePipelineExecutablePropertiesFeaturesKHR
*)ext
;
1146 features
->pipelineExecutableInfo
= true;
1150 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PROTECTED_MEMORY_FEATURES
: {
1151 VkPhysicalDeviceProtectedMemoryFeatures
*features
= (void *)ext
;
1152 features
->protectedMemory
= false;
1156 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SAMPLER_YCBCR_CONVERSION_FEATURES
: {
1157 VkPhysicalDeviceSamplerYcbcrConversionFeatures
*features
=
1158 (VkPhysicalDeviceSamplerYcbcrConversionFeatures
*) ext
;
1159 features
->samplerYcbcrConversion
= true;
1163 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SCALAR_BLOCK_LAYOUT_FEATURES_EXT
: {
1164 VkPhysicalDeviceScalarBlockLayoutFeaturesEXT
*features
=
1165 (VkPhysicalDeviceScalarBlockLayoutFeaturesEXT
*)ext
;
1166 features
->scalarBlockLayout
= true;
1170 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SHADER_ATOMIC_INT64_FEATURES_KHR
: {
1171 VkPhysicalDeviceShaderAtomicInt64FeaturesKHR
*features
= (void *)ext
;
1172 features
->shaderBufferInt64Atomics
=
1173 pdevice
->info
.gen
>= 9 && pdevice
->use_softpin
;
1174 features
->shaderSharedInt64Atomics
= VK_FALSE
;
1178 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SHADER_DEMOTE_TO_HELPER_INVOCATION_FEATURES_EXT
: {
1179 VkPhysicalDeviceShaderDemoteToHelperInvocationFeaturesEXT
*features
= (void *)ext
;
1180 features
->shaderDemoteToHelperInvocation
= true;
1184 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SHADER_DRAW_PARAMETERS_FEATURES
: {
1185 VkPhysicalDeviceShaderDrawParametersFeatures
*features
= (void *)ext
;
1186 features
->shaderDrawParameters
= true;
1190 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SUBGROUP_SIZE_CONTROL_FEATURES_EXT
: {
1191 VkPhysicalDeviceSubgroupSizeControlFeaturesEXT
*features
=
1192 (VkPhysicalDeviceSubgroupSizeControlFeaturesEXT
*)ext
;
1193 features
->subgroupSizeControl
= true;
1194 features
->computeFullSubgroups
= true;
1198 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_TEXEL_BUFFER_ALIGNMENT_FEATURES_EXT
: {
1199 VkPhysicalDeviceTexelBufferAlignmentFeaturesEXT
*features
=
1200 (VkPhysicalDeviceTexelBufferAlignmentFeaturesEXT
*)ext
;
1201 features
->texelBufferAlignment
= true;
1205 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VARIABLE_POINTERS_FEATURES
: {
1206 VkPhysicalDeviceVariablePointersFeatures
*features
= (void *)ext
;
1207 features
->variablePointersStorageBuffer
= true;
1208 features
->variablePointers
= true;
1212 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_TRANSFORM_FEEDBACK_FEATURES_EXT
: {
1213 VkPhysicalDeviceTransformFeedbackFeaturesEXT
*features
=
1214 (VkPhysicalDeviceTransformFeedbackFeaturesEXT
*)ext
;
1215 features
->transformFeedback
= true;
1216 features
->geometryStreams
= true;
1220 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_UNIFORM_BUFFER_STANDARD_LAYOUT_FEATURES_KHR
: {
1221 VkPhysicalDeviceUniformBufferStandardLayoutFeaturesKHR
*features
=
1222 (VkPhysicalDeviceUniformBufferStandardLayoutFeaturesKHR
*)ext
;
1223 features
->uniformBufferStandardLayout
= true;
1227 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VERTEX_ATTRIBUTE_DIVISOR_FEATURES_EXT
: {
1228 VkPhysicalDeviceVertexAttributeDivisorFeaturesEXT
*features
=
1229 (VkPhysicalDeviceVertexAttributeDivisorFeaturesEXT
*)ext
;
1230 features
->vertexAttributeInstanceRateDivisor
= true;
1231 features
->vertexAttributeInstanceRateZeroDivisor
= true;
1235 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_YCBCR_IMAGE_ARRAYS_FEATURES_EXT
: {
1236 VkPhysicalDeviceYcbcrImageArraysFeaturesEXT
*features
=
1237 (VkPhysicalDeviceYcbcrImageArraysFeaturesEXT
*)ext
;
1238 features
->ycbcrImageArrays
= true;
1243 anv_debug_ignored_stype(ext
->sType
);
1249 #define MAX_PER_STAGE_DESCRIPTOR_UNIFORM_BUFFERS 64
1251 #define MAX_PER_STAGE_DESCRIPTOR_INPUT_ATTACHMENTS 64
1252 #define MAX_DESCRIPTOR_SET_INPUT_ATTACHMENTS 256
1254 void anv_GetPhysicalDeviceProperties(
1255 VkPhysicalDevice physicalDevice
,
1256 VkPhysicalDeviceProperties
* pProperties
)
1258 ANV_FROM_HANDLE(anv_physical_device
, pdevice
, physicalDevice
);
1259 const struct gen_device_info
*devinfo
= &pdevice
->info
;
1261 /* See assertions made when programming the buffer surface state. */
1262 const uint32_t max_raw_buffer_sz
= devinfo
->gen
>= 7 ?
1263 (1ul << 30) : (1ul << 27);
1265 const uint32_t max_ssbos
= pdevice
->has_a64_buffer_access
? UINT16_MAX
: 64;
1266 const uint32_t max_textures
=
1267 pdevice
->has_bindless_images
? UINT16_MAX
: 128;
1268 const uint32_t max_samplers
=
1269 pdevice
->has_bindless_samplers
? UINT16_MAX
:
1270 (devinfo
->gen
>= 8 || devinfo
->is_haswell
) ? 128 : 16;
1271 const uint32_t max_images
=
1272 pdevice
->has_bindless_images
? UINT16_MAX
: MAX_IMAGES
;
1274 /* The moment we have anything bindless, claim a high per-stage limit */
1275 const uint32_t max_per_stage
=
1276 pdevice
->has_a64_buffer_access
? UINT32_MAX
:
1277 MAX_BINDING_TABLE_SIZE
- MAX_RTS
;
1279 const uint32_t max_workgroup_size
= 32 * devinfo
->max_cs_threads
;
1281 VkSampleCountFlags sample_counts
=
1282 isl_device_get_sample_counts(&pdevice
->isl_dev
);
1285 VkPhysicalDeviceLimits limits
= {
1286 .maxImageDimension1D
= (1 << 14),
1287 .maxImageDimension2D
= (1 << 14),
1288 .maxImageDimension3D
= (1 << 11),
1289 .maxImageDimensionCube
= (1 << 14),
1290 .maxImageArrayLayers
= (1 << 11),
1291 .maxTexelBufferElements
= 128 * 1024 * 1024,
1292 .maxUniformBufferRange
= (1ul << 27),
1293 .maxStorageBufferRange
= max_raw_buffer_sz
,
1294 .maxPushConstantsSize
= MAX_PUSH_CONSTANTS_SIZE
,
1295 .maxMemoryAllocationCount
= UINT32_MAX
,
1296 .maxSamplerAllocationCount
= 64 * 1024,
1297 .bufferImageGranularity
= 64, /* A cache line */
1298 .sparseAddressSpaceSize
= 0,
1299 .maxBoundDescriptorSets
= MAX_SETS
,
1300 .maxPerStageDescriptorSamplers
= max_samplers
,
1301 .maxPerStageDescriptorUniformBuffers
= MAX_PER_STAGE_DESCRIPTOR_UNIFORM_BUFFERS
,
1302 .maxPerStageDescriptorStorageBuffers
= max_ssbos
,
1303 .maxPerStageDescriptorSampledImages
= max_textures
,
1304 .maxPerStageDescriptorStorageImages
= max_images
,
1305 .maxPerStageDescriptorInputAttachments
= MAX_PER_STAGE_DESCRIPTOR_INPUT_ATTACHMENTS
,
1306 .maxPerStageResources
= max_per_stage
,
1307 .maxDescriptorSetSamplers
= 6 * max_samplers
, /* number of stages * maxPerStageDescriptorSamplers */
1308 .maxDescriptorSetUniformBuffers
= 6 * MAX_PER_STAGE_DESCRIPTOR_UNIFORM_BUFFERS
, /* number of stages * maxPerStageDescriptorUniformBuffers */
1309 .maxDescriptorSetUniformBuffersDynamic
= MAX_DYNAMIC_BUFFERS
/ 2,
1310 .maxDescriptorSetStorageBuffers
= 6 * max_ssbos
, /* number of stages * maxPerStageDescriptorStorageBuffers */
1311 .maxDescriptorSetStorageBuffersDynamic
= MAX_DYNAMIC_BUFFERS
/ 2,
1312 .maxDescriptorSetSampledImages
= 6 * max_textures
, /* number of stages * maxPerStageDescriptorSampledImages */
1313 .maxDescriptorSetStorageImages
= 6 * max_images
, /* number of stages * maxPerStageDescriptorStorageImages */
1314 .maxDescriptorSetInputAttachments
= MAX_DESCRIPTOR_SET_INPUT_ATTACHMENTS
,
1315 .maxVertexInputAttributes
= MAX_VBS
,
1316 .maxVertexInputBindings
= MAX_VBS
,
1317 .maxVertexInputAttributeOffset
= 2047,
1318 .maxVertexInputBindingStride
= 2048,
1319 .maxVertexOutputComponents
= 128,
1320 .maxTessellationGenerationLevel
= 64,
1321 .maxTessellationPatchSize
= 32,
1322 .maxTessellationControlPerVertexInputComponents
= 128,
1323 .maxTessellationControlPerVertexOutputComponents
= 128,
1324 .maxTessellationControlPerPatchOutputComponents
= 128,
1325 .maxTessellationControlTotalOutputComponents
= 2048,
1326 .maxTessellationEvaluationInputComponents
= 128,
1327 .maxTessellationEvaluationOutputComponents
= 128,
1328 .maxGeometryShaderInvocations
= 32,
1329 .maxGeometryInputComponents
= 64,
1330 .maxGeometryOutputComponents
= 128,
1331 .maxGeometryOutputVertices
= 256,
1332 .maxGeometryTotalOutputComponents
= 1024,
1333 .maxFragmentInputComponents
= 116, /* 128 components - (PSIZ, CLIP_DIST0, CLIP_DIST1) */
1334 .maxFragmentOutputAttachments
= 8,
1335 .maxFragmentDualSrcAttachments
= 1,
1336 .maxFragmentCombinedOutputResources
= 8,
1337 .maxComputeSharedMemorySize
= 64 * 1024,
1338 .maxComputeWorkGroupCount
= { 65535, 65535, 65535 },
1339 .maxComputeWorkGroupInvocations
= max_workgroup_size
,
1340 .maxComputeWorkGroupSize
= {
1345 .subPixelPrecisionBits
= 8,
1346 .subTexelPrecisionBits
= 8,
1347 .mipmapPrecisionBits
= 8,
1348 .maxDrawIndexedIndexValue
= UINT32_MAX
,
1349 .maxDrawIndirectCount
= UINT32_MAX
,
1350 .maxSamplerLodBias
= 16,
1351 .maxSamplerAnisotropy
= 16,
1352 .maxViewports
= MAX_VIEWPORTS
,
1353 .maxViewportDimensions
= { (1 << 14), (1 << 14) },
1354 .viewportBoundsRange
= { INT16_MIN
, INT16_MAX
},
1355 .viewportSubPixelBits
= 13, /* We take a float? */
1356 .minMemoryMapAlignment
= 4096, /* A page */
1357 /* The dataport requires texel alignment so we need to assume a worst
1358 * case of R32G32B32A32 which is 16 bytes.
1360 .minTexelBufferOffsetAlignment
= 16,
1361 /* We need 16 for UBO block reads to work and 32 for push UBOs */
1362 .minUniformBufferOffsetAlignment
= 32,
1363 .minStorageBufferOffsetAlignment
= 4,
1364 .minTexelOffset
= -8,
1365 .maxTexelOffset
= 7,
1366 .minTexelGatherOffset
= -32,
1367 .maxTexelGatherOffset
= 31,
1368 .minInterpolationOffset
= -0.5,
1369 .maxInterpolationOffset
= 0.4375,
1370 .subPixelInterpolationOffsetBits
= 4,
1371 .maxFramebufferWidth
= (1 << 14),
1372 .maxFramebufferHeight
= (1 << 14),
1373 .maxFramebufferLayers
= (1 << 11),
1374 .framebufferColorSampleCounts
= sample_counts
,
1375 .framebufferDepthSampleCounts
= sample_counts
,
1376 .framebufferStencilSampleCounts
= sample_counts
,
1377 .framebufferNoAttachmentsSampleCounts
= sample_counts
,
1378 .maxColorAttachments
= MAX_RTS
,
1379 .sampledImageColorSampleCounts
= sample_counts
,
1380 .sampledImageIntegerSampleCounts
= VK_SAMPLE_COUNT_1_BIT
,
1381 .sampledImageDepthSampleCounts
= sample_counts
,
1382 .sampledImageStencilSampleCounts
= sample_counts
,
1383 .storageImageSampleCounts
= VK_SAMPLE_COUNT_1_BIT
,
1384 .maxSampleMaskWords
= 1,
1385 .timestampComputeAndGraphics
= true,
1386 .timestampPeriod
= 1000000000.0 / devinfo
->timestamp_frequency
,
1387 .maxClipDistances
= 8,
1388 .maxCullDistances
= 8,
1389 .maxCombinedClipAndCullDistances
= 8,
1390 .discreteQueuePriorities
= 2,
1391 .pointSizeRange
= { 0.125, 255.875 },
1394 (devinfo
->gen
>= 9 || devinfo
->is_cherryview
) ?
1395 2047.9921875 : 7.9921875,
1397 .pointSizeGranularity
= (1.0 / 8.0),
1398 .lineWidthGranularity
= (1.0 / 128.0),
1399 .strictLines
= false,
1400 .standardSampleLocations
= true,
1401 .optimalBufferCopyOffsetAlignment
= 128,
1402 .optimalBufferCopyRowPitchAlignment
= 128,
1403 .nonCoherentAtomSize
= 64,
1406 *pProperties
= (VkPhysicalDeviceProperties
) {
1407 .apiVersion
= anv_physical_device_api_version(pdevice
),
1408 .driverVersion
= vk_get_driver_version(),
1410 .deviceID
= pdevice
->chipset_id
,
1411 .deviceType
= VK_PHYSICAL_DEVICE_TYPE_INTEGRATED_GPU
,
1413 .sparseProperties
= {0}, /* Broadwell doesn't do sparse. */
1416 snprintf(pProperties
->deviceName
, sizeof(pProperties
->deviceName
),
1417 "%s", pdevice
->name
);
1418 memcpy(pProperties
->pipelineCacheUUID
,
1419 pdevice
->pipeline_cache_uuid
, VK_UUID_SIZE
);
1422 void anv_GetPhysicalDeviceProperties2(
1423 VkPhysicalDevice physicalDevice
,
1424 VkPhysicalDeviceProperties2
* pProperties
)
1426 ANV_FROM_HANDLE(anv_physical_device
, pdevice
, physicalDevice
);
1428 anv_GetPhysicalDeviceProperties(physicalDevice
, &pProperties
->properties
);
1430 vk_foreach_struct(ext
, pProperties
->pNext
) {
1431 switch (ext
->sType
) {
1432 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_DEPTH_STENCIL_RESOLVE_PROPERTIES_KHR
: {
1433 VkPhysicalDeviceDepthStencilResolvePropertiesKHR
*props
=
1434 (VkPhysicalDeviceDepthStencilResolvePropertiesKHR
*)ext
;
1436 /* We support all of the depth resolve modes */
1437 props
->supportedDepthResolveModes
=
1438 VK_RESOLVE_MODE_SAMPLE_ZERO_BIT_KHR
|
1439 VK_RESOLVE_MODE_AVERAGE_BIT_KHR
|
1440 VK_RESOLVE_MODE_MIN_BIT_KHR
|
1441 VK_RESOLVE_MODE_MAX_BIT_KHR
;
1443 /* Average doesn't make sense for stencil so we don't support that */
1444 props
->supportedStencilResolveModes
=
1445 VK_RESOLVE_MODE_SAMPLE_ZERO_BIT_KHR
;
1446 if (pdevice
->info
.gen
>= 8) {
1447 /* The advanced stencil resolve modes currently require stencil
1448 * sampling be supported by the hardware.
1450 props
->supportedStencilResolveModes
|=
1451 VK_RESOLVE_MODE_MIN_BIT_KHR
|
1452 VK_RESOLVE_MODE_MAX_BIT_KHR
;
1455 props
->independentResolveNone
= true;
1456 props
->independentResolve
= true;
1460 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_DESCRIPTOR_INDEXING_PROPERTIES_EXT
: {
1461 VkPhysicalDeviceDescriptorIndexingPropertiesEXT
*props
=
1462 (VkPhysicalDeviceDescriptorIndexingPropertiesEXT
*)ext
;
1464 /* It's a bit hard to exactly map our implementation to the limits
1465 * described here. The bindless surface handle in the extended
1466 * message descriptors is 20 bits and it's an index into the table of
1467 * RENDER_SURFACE_STATE structs that starts at bindless surface base
1468 * address. Given that most things consume two surface states per
1469 * view (general/sampled for textures and write-only/read-write for
1470 * images), we claim 2^19 things.
1472 * For SSBOs, we just use A64 messages so there is no real limit
1473 * there beyond the limit on the total size of a descriptor set.
1475 const unsigned max_bindless_views
= 1 << 19;
1477 props
->maxUpdateAfterBindDescriptorsInAllPools
= max_bindless_views
;
1478 props
->shaderUniformBufferArrayNonUniformIndexingNative
= false;
1479 props
->shaderSampledImageArrayNonUniformIndexingNative
= false;
1480 props
->shaderStorageBufferArrayNonUniformIndexingNative
= true;
1481 props
->shaderStorageImageArrayNonUniformIndexingNative
= false;
1482 props
->shaderInputAttachmentArrayNonUniformIndexingNative
= false;
1483 props
->robustBufferAccessUpdateAfterBind
= true;
1484 props
->quadDivergentImplicitLod
= false;
1485 props
->maxPerStageDescriptorUpdateAfterBindSamplers
= max_bindless_views
;
1486 props
->maxPerStageDescriptorUpdateAfterBindUniformBuffers
= MAX_PER_STAGE_DESCRIPTOR_UNIFORM_BUFFERS
;
1487 props
->maxPerStageDescriptorUpdateAfterBindStorageBuffers
= UINT32_MAX
;
1488 props
->maxPerStageDescriptorUpdateAfterBindSampledImages
= max_bindless_views
;
1489 props
->maxPerStageDescriptorUpdateAfterBindStorageImages
= max_bindless_views
;
1490 props
->maxPerStageDescriptorUpdateAfterBindInputAttachments
= MAX_PER_STAGE_DESCRIPTOR_INPUT_ATTACHMENTS
;
1491 props
->maxPerStageUpdateAfterBindResources
= UINT32_MAX
;
1492 props
->maxDescriptorSetUpdateAfterBindSamplers
= max_bindless_views
;
1493 props
->maxDescriptorSetUpdateAfterBindUniformBuffers
= 6 * MAX_PER_STAGE_DESCRIPTOR_UNIFORM_BUFFERS
;
1494 props
->maxDescriptorSetUpdateAfterBindUniformBuffersDynamic
= MAX_DYNAMIC_BUFFERS
/ 2;
1495 props
->maxDescriptorSetUpdateAfterBindStorageBuffers
= UINT32_MAX
;
1496 props
->maxDescriptorSetUpdateAfterBindStorageBuffersDynamic
= MAX_DYNAMIC_BUFFERS
/ 2;
1497 props
->maxDescriptorSetUpdateAfterBindSampledImages
= max_bindless_views
;
1498 props
->maxDescriptorSetUpdateAfterBindStorageImages
= max_bindless_views
;
1499 props
->maxDescriptorSetUpdateAfterBindInputAttachments
= MAX_DESCRIPTOR_SET_INPUT_ATTACHMENTS
;
1503 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_DRIVER_PROPERTIES_KHR
: {
1504 VkPhysicalDeviceDriverPropertiesKHR
*driver_props
=
1505 (VkPhysicalDeviceDriverPropertiesKHR
*) ext
;
1507 driver_props
->driverID
= VK_DRIVER_ID_INTEL_OPEN_SOURCE_MESA_KHR
;
1508 snprintf(driver_props
->driverName
, VK_MAX_DRIVER_NAME_SIZE_KHR
,
1509 "Intel open-source Mesa driver");
1511 snprintf(driver_props
->driverInfo
, VK_MAX_DRIVER_INFO_SIZE_KHR
,
1512 "Mesa " PACKAGE_VERSION MESA_GIT_SHA1
);
1514 driver_props
->conformanceVersion
= (VkConformanceVersionKHR
) {
1523 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_EXTERNAL_MEMORY_HOST_PROPERTIES_EXT
: {
1524 VkPhysicalDeviceExternalMemoryHostPropertiesEXT
*props
=
1525 (VkPhysicalDeviceExternalMemoryHostPropertiesEXT
*) ext
;
1526 /* Userptr needs page aligned memory. */
1527 props
->minImportedHostPointerAlignment
= 4096;
1531 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_ID_PROPERTIES
: {
1532 VkPhysicalDeviceIDProperties
*id_props
=
1533 (VkPhysicalDeviceIDProperties
*)ext
;
1534 memcpy(id_props
->deviceUUID
, pdevice
->device_uuid
, VK_UUID_SIZE
);
1535 memcpy(id_props
->driverUUID
, pdevice
->driver_uuid
, VK_UUID_SIZE
);
1536 /* The LUID is for Windows. */
1537 id_props
->deviceLUIDValid
= false;
1541 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_INLINE_UNIFORM_BLOCK_PROPERTIES_EXT
: {
1542 VkPhysicalDeviceInlineUniformBlockPropertiesEXT
*props
=
1543 (VkPhysicalDeviceInlineUniformBlockPropertiesEXT
*)ext
;
1544 props
->maxInlineUniformBlockSize
= MAX_INLINE_UNIFORM_BLOCK_SIZE
;
1545 props
->maxPerStageDescriptorInlineUniformBlocks
=
1546 MAX_INLINE_UNIFORM_BLOCK_DESCRIPTORS
;
1547 props
->maxPerStageDescriptorUpdateAfterBindInlineUniformBlocks
=
1548 MAX_INLINE_UNIFORM_BLOCK_DESCRIPTORS
;
1549 props
->maxDescriptorSetInlineUniformBlocks
=
1550 MAX_INLINE_UNIFORM_BLOCK_DESCRIPTORS
;
1551 props
->maxDescriptorSetUpdateAfterBindInlineUniformBlocks
=
1552 MAX_INLINE_UNIFORM_BLOCK_DESCRIPTORS
;
1556 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_LINE_RASTERIZATION_PROPERTIES_EXT
: {
1557 VkPhysicalDeviceLineRasterizationPropertiesEXT
*props
=
1558 (VkPhysicalDeviceLineRasterizationPropertiesEXT
*)ext
;
1559 /* In the Skylake PRM Vol. 7, subsection titled "GIQ (Diamond)
1560 * Sampling Rules - Legacy Mode", it says the following:
1562 * "Note that the device divides a pixel into a 16x16 array of
1563 * subpixels, referenced by their upper left corners."
1565 * This is the only known reference in the PRMs to the subpixel
1566 * precision of line rasterization and a "16x16 array of subpixels"
1567 * implies 4 subpixel precision bits. Empirical testing has shown
1568 * that 4 subpixel precision bits applies to all line rasterization
1571 props
->lineSubPixelPrecisionBits
= 4;
1575 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_MAINTENANCE_3_PROPERTIES
: {
1576 VkPhysicalDeviceMaintenance3Properties
*props
=
1577 (VkPhysicalDeviceMaintenance3Properties
*)ext
;
1578 /* This value doesn't matter for us today as our per-stage
1579 * descriptors are the real limit.
1581 props
->maxPerSetDescriptors
= 1024;
1582 props
->maxMemoryAllocationSize
= MAX_MEMORY_ALLOCATION_SIZE
;
1586 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_MULTIVIEW_PROPERTIES
: {
1587 VkPhysicalDeviceMultiviewProperties
*properties
=
1588 (VkPhysicalDeviceMultiviewProperties
*)ext
;
1589 properties
->maxMultiviewViewCount
= 16;
1590 properties
->maxMultiviewInstanceIndex
= UINT32_MAX
/ 16;
1594 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PCI_BUS_INFO_PROPERTIES_EXT
: {
1595 VkPhysicalDevicePCIBusInfoPropertiesEXT
*properties
=
1596 (VkPhysicalDevicePCIBusInfoPropertiesEXT
*)ext
;
1597 properties
->pciDomain
= pdevice
->pci_info
.domain
;
1598 properties
->pciBus
= pdevice
->pci_info
.bus
;
1599 properties
->pciDevice
= pdevice
->pci_info
.device
;
1600 properties
->pciFunction
= pdevice
->pci_info
.function
;
1604 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_POINT_CLIPPING_PROPERTIES
: {
1605 VkPhysicalDevicePointClippingProperties
*properties
=
1606 (VkPhysicalDevicePointClippingProperties
*) ext
;
1607 properties
->pointClippingBehavior
= VK_POINT_CLIPPING_BEHAVIOR_USER_CLIP_PLANES_ONLY
;
1611 #pragma GCC diagnostic push
1612 #pragma GCC diagnostic ignored "-Wswitch"
1613 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PRESENTATION_PROPERTIES_ANDROID
: {
1614 VkPhysicalDevicePresentationPropertiesANDROID
*props
=
1615 (VkPhysicalDevicePresentationPropertiesANDROID
*)ext
;
1616 props
->sharedImage
= VK_FALSE
;
1619 #pragma GCC diagnostic pop
1621 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PROTECTED_MEMORY_PROPERTIES
: {
1622 VkPhysicalDeviceProtectedMemoryProperties
*props
=
1623 (VkPhysicalDeviceProtectedMemoryProperties
*)ext
;
1624 props
->protectedNoFault
= false;
1628 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PUSH_DESCRIPTOR_PROPERTIES_KHR
: {
1629 VkPhysicalDevicePushDescriptorPropertiesKHR
*properties
=
1630 (VkPhysicalDevicePushDescriptorPropertiesKHR
*) ext
;
1632 properties
->maxPushDescriptors
= MAX_PUSH_DESCRIPTORS
;
1636 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SAMPLER_FILTER_MINMAX_PROPERTIES_EXT
: {
1637 VkPhysicalDeviceSamplerFilterMinmaxPropertiesEXT
*properties
=
1638 (VkPhysicalDeviceSamplerFilterMinmaxPropertiesEXT
*)ext
;
1639 properties
->filterMinmaxImageComponentMapping
= pdevice
->info
.gen
>= 9;
1640 properties
->filterMinmaxSingleComponentFormats
= true;
1644 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SUBGROUP_PROPERTIES
: {
1645 VkPhysicalDeviceSubgroupProperties
*properties
= (void *)ext
;
1647 properties
->subgroupSize
= BRW_SUBGROUP_SIZE
;
1649 VkShaderStageFlags scalar_stages
= 0;
1650 for (unsigned stage
= 0; stage
< MESA_SHADER_STAGES
; stage
++) {
1651 if (pdevice
->compiler
->scalar_stage
[stage
])
1652 scalar_stages
|= mesa_to_vk_shader_stage(stage
);
1654 properties
->supportedStages
= scalar_stages
;
1656 properties
->supportedOperations
= VK_SUBGROUP_FEATURE_BASIC_BIT
|
1657 VK_SUBGROUP_FEATURE_VOTE_BIT
|
1658 VK_SUBGROUP_FEATURE_BALLOT_BIT
|
1659 VK_SUBGROUP_FEATURE_SHUFFLE_BIT
|
1660 VK_SUBGROUP_FEATURE_SHUFFLE_RELATIVE_BIT
|
1661 VK_SUBGROUP_FEATURE_QUAD_BIT
;
1662 if (pdevice
->info
.gen
>= 8) {
1663 /* TODO: There's no technical reason why these can't be made to
1664 * work on gen7 but they don't at the moment so it's best to leave
1665 * the feature disabled than enabled and broken.
1667 properties
->supportedOperations
|=
1668 VK_SUBGROUP_FEATURE_ARITHMETIC_BIT
|
1669 VK_SUBGROUP_FEATURE_CLUSTERED_BIT
;
1671 properties
->quadOperationsInAllStages
= pdevice
->info
.gen
>= 8;
1675 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SUBGROUP_SIZE_CONTROL_PROPERTIES_EXT
: {
1676 VkPhysicalDeviceSubgroupSizeControlPropertiesEXT
*props
=
1677 (VkPhysicalDeviceSubgroupSizeControlPropertiesEXT
*)ext
;
1678 STATIC_ASSERT(8 <= BRW_SUBGROUP_SIZE
&& BRW_SUBGROUP_SIZE
<= 32);
1679 props
->minSubgroupSize
= 8;
1680 props
->maxSubgroupSize
= 32;
1681 props
->maxComputeWorkgroupSubgroups
= pdevice
->info
.max_cs_threads
;
1682 props
->requiredSubgroupSizeStages
= VK_SHADER_STAGE_COMPUTE_BIT
;
1686 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_TEXEL_BUFFER_ALIGNMENT_PROPERTIES_EXT
: {
1687 VkPhysicalDeviceTexelBufferAlignmentPropertiesEXT
*props
=
1688 (VkPhysicalDeviceTexelBufferAlignmentPropertiesEXT
*)ext
;
1690 /* From the SKL PRM Vol. 2d, docs for RENDER_SURFACE_STATE::Surface
1693 * "For SURFTYPE_BUFFER non-rendertarget surfaces, this field
1694 * specifies the base address of the first element of the surface,
1695 * computed in software by adding the surface base address to the
1696 * byte offset of the element in the buffer. The base address must
1697 * be aligned to element size."
1699 * The typed dataport messages require that things be texel aligned.
1700 * Otherwise, we may just load/store the wrong data or, in the worst
1701 * case, there may be hangs.
1703 props
->storageTexelBufferOffsetAlignmentBytes
= 16;
1704 props
->storageTexelBufferOffsetSingleTexelAlignment
= true;
1706 /* The sampler, however, is much more forgiving and it can handle
1707 * arbitrary byte alignment for linear and buffer surfaces. It's
1708 * hard to find a good PRM citation for this but years of empirical
1709 * experience demonstrate that this is true.
1711 props
->uniformTexelBufferOffsetAlignmentBytes
= 1;
1712 props
->uniformTexelBufferOffsetSingleTexelAlignment
= false;
1716 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_TRANSFORM_FEEDBACK_PROPERTIES_EXT
: {
1717 VkPhysicalDeviceTransformFeedbackPropertiesEXT
*props
=
1718 (VkPhysicalDeviceTransformFeedbackPropertiesEXT
*)ext
;
1720 props
->maxTransformFeedbackStreams
= MAX_XFB_STREAMS
;
1721 props
->maxTransformFeedbackBuffers
= MAX_XFB_BUFFERS
;
1722 props
->maxTransformFeedbackBufferSize
= (1ull << 32);
1723 props
->maxTransformFeedbackStreamDataSize
= 128 * 4;
1724 props
->maxTransformFeedbackBufferDataSize
= 128 * 4;
1725 props
->maxTransformFeedbackBufferDataStride
= 2048;
1726 props
->transformFeedbackQueries
= true;
1727 props
->transformFeedbackStreamsLinesTriangles
= false;
1728 props
->transformFeedbackRasterizationStreamSelect
= false;
1729 props
->transformFeedbackDraw
= true;
1733 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VERTEX_ATTRIBUTE_DIVISOR_PROPERTIES_EXT
: {
1734 VkPhysicalDeviceVertexAttributeDivisorPropertiesEXT
*props
=
1735 (VkPhysicalDeviceVertexAttributeDivisorPropertiesEXT
*)ext
;
1736 /* We have to restrict this a bit for multiview */
1737 props
->maxVertexAttribDivisor
= UINT32_MAX
/ 16;
1742 anv_debug_ignored_stype(ext
->sType
);
1748 /* We support exactly one queue family. */
1749 static const VkQueueFamilyProperties
1750 anv_queue_family_properties
= {
1751 .queueFlags
= VK_QUEUE_GRAPHICS_BIT
|
1752 VK_QUEUE_COMPUTE_BIT
|
1753 VK_QUEUE_TRANSFER_BIT
,
1755 .timestampValidBits
= 36, /* XXX: Real value here */
1756 .minImageTransferGranularity
= { 1, 1, 1 },
1759 void anv_GetPhysicalDeviceQueueFamilyProperties(
1760 VkPhysicalDevice physicalDevice
,
1762 VkQueueFamilyProperties
* pQueueFamilyProperties
)
1764 VK_OUTARRAY_MAKE(out
, pQueueFamilyProperties
, pCount
);
1766 vk_outarray_append(&out
, p
) {
1767 *p
= anv_queue_family_properties
;
1771 void anv_GetPhysicalDeviceQueueFamilyProperties2(
1772 VkPhysicalDevice physicalDevice
,
1773 uint32_t* pQueueFamilyPropertyCount
,
1774 VkQueueFamilyProperties2
* pQueueFamilyProperties
)
1777 VK_OUTARRAY_MAKE(out
, pQueueFamilyProperties
, pQueueFamilyPropertyCount
);
1779 vk_outarray_append(&out
, p
) {
1780 p
->queueFamilyProperties
= anv_queue_family_properties
;
1782 vk_foreach_struct(s
, p
->pNext
) {
1783 anv_debug_ignored_stype(s
->sType
);
1788 void anv_GetPhysicalDeviceMemoryProperties(
1789 VkPhysicalDevice physicalDevice
,
1790 VkPhysicalDeviceMemoryProperties
* pMemoryProperties
)
1792 ANV_FROM_HANDLE(anv_physical_device
, physical_device
, physicalDevice
);
1794 pMemoryProperties
->memoryTypeCount
= physical_device
->memory
.type_count
;
1795 for (uint32_t i
= 0; i
< physical_device
->memory
.type_count
; i
++) {
1796 pMemoryProperties
->memoryTypes
[i
] = (VkMemoryType
) {
1797 .propertyFlags
= physical_device
->memory
.types
[i
].propertyFlags
,
1798 .heapIndex
= physical_device
->memory
.types
[i
].heapIndex
,
1802 pMemoryProperties
->memoryHeapCount
= physical_device
->memory
.heap_count
;
1803 for (uint32_t i
= 0; i
< physical_device
->memory
.heap_count
; i
++) {
1804 pMemoryProperties
->memoryHeaps
[i
] = (VkMemoryHeap
) {
1805 .size
= physical_device
->memory
.heaps
[i
].size
,
1806 .flags
= physical_device
->memory
.heaps
[i
].flags
,
1812 anv_get_memory_budget(VkPhysicalDevice physicalDevice
,
1813 VkPhysicalDeviceMemoryBudgetPropertiesEXT
*memoryBudget
)
1815 ANV_FROM_HANDLE(anv_physical_device
, device
, physicalDevice
);
1816 uint64_t sys_available
= get_available_system_memory();
1817 assert(sys_available
> 0);
1819 VkDeviceSize total_heaps_size
= 0;
1820 for (size_t i
= 0; i
< device
->memory
.heap_count
; i
++)
1821 total_heaps_size
+= device
->memory
.heaps
[i
].size
;
1823 for (size_t i
= 0; i
< device
->memory
.heap_count
; i
++) {
1824 VkDeviceSize heap_size
= device
->memory
.heaps
[i
].size
;
1825 VkDeviceSize heap_used
= device
->memory
.heaps
[i
].used
;
1826 VkDeviceSize heap_budget
;
1828 double heap_proportion
= (double) heap_size
/ total_heaps_size
;
1829 VkDeviceSize sys_available_prop
= sys_available
* heap_proportion
;
1832 * Let's not incite the app to starve the system: report at most 90% of
1833 * available system memory.
1835 uint64_t heap_available
= sys_available_prop
* 9 / 10;
1836 heap_budget
= MIN2(heap_size
, heap_used
+ heap_available
);
1839 * Round down to the nearest MB
1841 heap_budget
&= ~((1ull << 20) - 1);
1844 * The heapBudget value must be non-zero for array elements less than
1845 * VkPhysicalDeviceMemoryProperties::memoryHeapCount. The heapBudget
1846 * value must be less than or equal to VkMemoryHeap::size for each heap.
1848 assert(0 < heap_budget
&& heap_budget
<= heap_size
);
1850 memoryBudget
->heapUsage
[i
] = heap_used
;
1851 memoryBudget
->heapBudget
[i
] = heap_budget
;
1854 /* The heapBudget and heapUsage values must be zero for array elements
1855 * greater than or equal to VkPhysicalDeviceMemoryProperties::memoryHeapCount
1857 for (uint32_t i
= device
->memory
.heap_count
; i
< VK_MAX_MEMORY_HEAPS
; i
++) {
1858 memoryBudget
->heapBudget
[i
] = 0;
1859 memoryBudget
->heapUsage
[i
] = 0;
1863 void anv_GetPhysicalDeviceMemoryProperties2(
1864 VkPhysicalDevice physicalDevice
,
1865 VkPhysicalDeviceMemoryProperties2
* pMemoryProperties
)
1867 anv_GetPhysicalDeviceMemoryProperties(physicalDevice
,
1868 &pMemoryProperties
->memoryProperties
);
1870 vk_foreach_struct(ext
, pMemoryProperties
->pNext
) {
1871 switch (ext
->sType
) {
1872 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_MEMORY_BUDGET_PROPERTIES_EXT
:
1873 anv_get_memory_budget(physicalDevice
, (void*)ext
);
1876 anv_debug_ignored_stype(ext
->sType
);
1883 anv_GetDeviceGroupPeerMemoryFeatures(
1886 uint32_t localDeviceIndex
,
1887 uint32_t remoteDeviceIndex
,
1888 VkPeerMemoryFeatureFlags
* pPeerMemoryFeatures
)
1890 assert(localDeviceIndex
== 0 && remoteDeviceIndex
== 0);
1891 *pPeerMemoryFeatures
= VK_PEER_MEMORY_FEATURE_COPY_SRC_BIT
|
1892 VK_PEER_MEMORY_FEATURE_COPY_DST_BIT
|
1893 VK_PEER_MEMORY_FEATURE_GENERIC_SRC_BIT
|
1894 VK_PEER_MEMORY_FEATURE_GENERIC_DST_BIT
;
1897 PFN_vkVoidFunction
anv_GetInstanceProcAddr(
1898 VkInstance _instance
,
1901 ANV_FROM_HANDLE(anv_instance
, instance
, _instance
);
1903 /* The Vulkan 1.0 spec for vkGetInstanceProcAddr has a table of exactly
1904 * when we have to return valid function pointers, NULL, or it's left
1905 * undefined. See the table for exact details.
1910 #define LOOKUP_ANV_ENTRYPOINT(entrypoint) \
1911 if (strcmp(pName, "vk" #entrypoint) == 0) \
1912 return (PFN_vkVoidFunction)anv_##entrypoint
1914 LOOKUP_ANV_ENTRYPOINT(EnumerateInstanceExtensionProperties
);
1915 LOOKUP_ANV_ENTRYPOINT(EnumerateInstanceLayerProperties
);
1916 LOOKUP_ANV_ENTRYPOINT(EnumerateInstanceVersion
);
1917 LOOKUP_ANV_ENTRYPOINT(CreateInstance
);
1919 #undef LOOKUP_ANV_ENTRYPOINT
1921 if (instance
== NULL
)
1924 int idx
= anv_get_instance_entrypoint_index(pName
);
1926 return instance
->dispatch
.entrypoints
[idx
];
1928 idx
= anv_get_device_entrypoint_index(pName
);
1930 return instance
->device_dispatch
.entrypoints
[idx
];
1935 /* With version 1+ of the loader interface the ICD should expose
1936 * vk_icdGetInstanceProcAddr to work around certain LD_PRELOAD issues seen in apps.
1939 VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL
vk_icdGetInstanceProcAddr(
1940 VkInstance instance
,
1944 VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL
vk_icdGetInstanceProcAddr(
1945 VkInstance instance
,
1948 return anv_GetInstanceProcAddr(instance
, pName
);
1951 PFN_vkVoidFunction
anv_GetDeviceProcAddr(
1955 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1957 if (!device
|| !pName
)
1960 int idx
= anv_get_device_entrypoint_index(pName
);
1964 return device
->dispatch
.entrypoints
[idx
];
1968 anv_CreateDebugReportCallbackEXT(VkInstance _instance
,
1969 const VkDebugReportCallbackCreateInfoEXT
* pCreateInfo
,
1970 const VkAllocationCallbacks
* pAllocator
,
1971 VkDebugReportCallbackEXT
* pCallback
)
1973 ANV_FROM_HANDLE(anv_instance
, instance
, _instance
);
1974 return vk_create_debug_report_callback(&instance
->debug_report_callbacks
,
1975 pCreateInfo
, pAllocator
, &instance
->alloc
,
1980 anv_DestroyDebugReportCallbackEXT(VkInstance _instance
,
1981 VkDebugReportCallbackEXT _callback
,
1982 const VkAllocationCallbacks
* pAllocator
)
1984 ANV_FROM_HANDLE(anv_instance
, instance
, _instance
);
1985 vk_destroy_debug_report_callback(&instance
->debug_report_callbacks
,
1986 _callback
, pAllocator
, &instance
->alloc
);
1990 anv_DebugReportMessageEXT(VkInstance _instance
,
1991 VkDebugReportFlagsEXT flags
,
1992 VkDebugReportObjectTypeEXT objectType
,
1995 int32_t messageCode
,
1996 const char* pLayerPrefix
,
1997 const char* pMessage
)
1999 ANV_FROM_HANDLE(anv_instance
, instance
, _instance
);
2000 vk_debug_report(&instance
->debug_report_callbacks
, flags
, objectType
,
2001 object
, location
, messageCode
, pLayerPrefix
, pMessage
);
2005 anv_queue_init(struct anv_device
*device
, struct anv_queue
*queue
)
2007 queue
->_loader_data
.loaderMagic
= ICD_LOADER_MAGIC
;
2008 queue
->device
= device
;
2013 anv_queue_finish(struct anv_queue
*queue
)
2017 static struct anv_state
2018 anv_state_pool_emit_data(struct anv_state_pool
*pool
, size_t size
, size_t align
, const void *p
)
2020 struct anv_state state
;
2022 state
= anv_state_pool_alloc(pool
, size
, align
);
2023 memcpy(state
.map
, p
, size
);
2028 /* Haswell border color is a bit of a disaster. Float and unorm formats use a
2029 * straightforward 32-bit float color in the first 64 bytes. Instead of using
2030 * a nice float/integer union like Gen8+, Haswell specifies the integer border
2031 * color as a separate entry /after/ the float color. The layout of this entry
2032 * also depends on the format's bpp (with extra hacks for RG32), and overlaps.
2034 * Since we don't know the format/bpp, we can't make any of the border colors
2035 * containing '1' work for all formats, as it would be in the wrong place for
2036 * some of them. We opt to make 32-bit integers work as this seems like the
2037 * most common option. Fortunately, transparent black works regardless, as
2038 * all zeroes is the same in every bit-size.
2040 struct hsw_border_color
{
2044 uint32_t _pad1
[108];
2047 struct gen8_border_color
{
2052 /* Pad out to 64 bytes */
2057 anv_device_init_border_colors(struct anv_device
*device
)
2059 if (device
->info
.is_haswell
) {
2060 static const struct hsw_border_color border_colors
[] = {
2061 [VK_BORDER_COLOR_FLOAT_TRANSPARENT_BLACK
] = { .float32
= { 0.0, 0.0, 0.0, 0.0 } },
2062 [VK_BORDER_COLOR_FLOAT_OPAQUE_BLACK
] = { .float32
= { 0.0, 0.0, 0.0, 1.0 } },
2063 [VK_BORDER_COLOR_FLOAT_OPAQUE_WHITE
] = { .float32
= { 1.0, 1.0, 1.0, 1.0 } },
2064 [VK_BORDER_COLOR_INT_TRANSPARENT_BLACK
] = { .uint32
= { 0, 0, 0, 0 } },
2065 [VK_BORDER_COLOR_INT_OPAQUE_BLACK
] = { .uint32
= { 0, 0, 0, 1 } },
2066 [VK_BORDER_COLOR_INT_OPAQUE_WHITE
] = { .uint32
= { 1, 1, 1, 1 } },
2069 device
->border_colors
=
2070 anv_state_pool_emit_data(&device
->dynamic_state_pool
,
2071 sizeof(border_colors
), 512, border_colors
);
2073 static const struct gen8_border_color border_colors
[] = {
2074 [VK_BORDER_COLOR_FLOAT_TRANSPARENT_BLACK
] = { .float32
= { 0.0, 0.0, 0.0, 0.0 } },
2075 [VK_BORDER_COLOR_FLOAT_OPAQUE_BLACK
] = { .float32
= { 0.0, 0.0, 0.0, 1.0 } },
2076 [VK_BORDER_COLOR_FLOAT_OPAQUE_WHITE
] = { .float32
= { 1.0, 1.0, 1.0, 1.0 } },
2077 [VK_BORDER_COLOR_INT_TRANSPARENT_BLACK
] = { .uint32
= { 0, 0, 0, 0 } },
2078 [VK_BORDER_COLOR_INT_OPAQUE_BLACK
] = { .uint32
= { 0, 0, 0, 1 } },
2079 [VK_BORDER_COLOR_INT_OPAQUE_WHITE
] = { .uint32
= { 1, 1, 1, 1 } },
2082 device
->border_colors
=
2083 anv_state_pool_emit_data(&device
->dynamic_state_pool
,
2084 sizeof(border_colors
), 64, border_colors
);
2089 anv_device_init_trivial_batch(struct anv_device
*device
)
2091 anv_bo_init_new(&device
->trivial_batch_bo
, device
, 4096);
2093 if (device
->instance
->physicalDevice
.has_exec_async
)
2094 device
->trivial_batch_bo
.flags
|= EXEC_OBJECT_ASYNC
;
2096 if (device
->instance
->physicalDevice
.use_softpin
)
2097 device
->trivial_batch_bo
.flags
|= EXEC_OBJECT_PINNED
;
2099 anv_vma_alloc(device
, &device
->trivial_batch_bo
);
2101 void *map
= anv_gem_mmap(device
, device
->trivial_batch_bo
.gem_handle
,
2104 struct anv_batch batch
= {
2110 anv_batch_emit(&batch
, GEN7_MI_BATCH_BUFFER_END
, bbe
);
2111 anv_batch_emit(&batch
, GEN7_MI_NOOP
, noop
);
2113 if (!device
->info
.has_llc
)
2114 gen_clflush_range(map
, batch
.next
- map
);
2116 anv_gem_munmap(map
, device
->trivial_batch_bo
.size
);
2119 VkResult
anv_EnumerateDeviceExtensionProperties(
2120 VkPhysicalDevice physicalDevice
,
2121 const char* pLayerName
,
2122 uint32_t* pPropertyCount
,
2123 VkExtensionProperties
* pProperties
)
2125 ANV_FROM_HANDLE(anv_physical_device
, device
, physicalDevice
);
2126 VK_OUTARRAY_MAKE(out
, pProperties
, pPropertyCount
);
2128 for (int i
= 0; i
< ANV_DEVICE_EXTENSION_COUNT
; i
++) {
2129 if (device
->supported_extensions
.extensions
[i
]) {
2130 vk_outarray_append(&out
, prop
) {
2131 *prop
= anv_device_extensions
[i
];
2136 return vk_outarray_status(&out
);
2140 anv_device_init_dispatch(struct anv_device
*device
)
2142 const struct anv_device_dispatch_table
*genX_table
;
2143 switch (device
->info
.gen
) {
2145 genX_table
= &gen12_device_dispatch_table
;
2148 genX_table
= &gen11_device_dispatch_table
;
2151 genX_table
= &gen10_device_dispatch_table
;
2154 genX_table
= &gen9_device_dispatch_table
;
2157 genX_table
= &gen8_device_dispatch_table
;
2160 if (device
->info
.is_haswell
)
2161 genX_table
= &gen75_device_dispatch_table
;
2163 genX_table
= &gen7_device_dispatch_table
;
2166 unreachable("unsupported gen\n");
2169 for (unsigned i
= 0; i
< ARRAY_SIZE(device
->dispatch
.entrypoints
); i
++) {
2170 /* Vulkan requires that entrypoints for extensions which have not been
2171 * enabled must not be advertised.
2173 if (!anv_device_entrypoint_is_enabled(i
, device
->instance
->app_info
.api_version
,
2174 &device
->instance
->enabled_extensions
,
2175 &device
->enabled_extensions
)) {
2176 device
->dispatch
.entrypoints
[i
] = NULL
;
2177 } else if (genX_table
->entrypoints
[i
]) {
2178 device
->dispatch
.entrypoints
[i
] = genX_table
->entrypoints
[i
];
2180 device
->dispatch
.entrypoints
[i
] =
2181 anv_device_dispatch_table
.entrypoints
[i
];
2187 vk_priority_to_gen(int priority
)
2190 case VK_QUEUE_GLOBAL_PRIORITY_LOW_EXT
:
2191 return GEN_CONTEXT_LOW_PRIORITY
;
2192 case VK_QUEUE_GLOBAL_PRIORITY_MEDIUM_EXT
:
2193 return GEN_CONTEXT_MEDIUM_PRIORITY
;
2194 case VK_QUEUE_GLOBAL_PRIORITY_HIGH_EXT
:
2195 return GEN_CONTEXT_HIGH_PRIORITY
;
2196 case VK_QUEUE_GLOBAL_PRIORITY_REALTIME_EXT
:
2197 return GEN_CONTEXT_REALTIME_PRIORITY
;
2199 unreachable("Invalid priority");
2204 anv_device_init_hiz_clear_value_bo(struct anv_device
*device
)
2206 anv_bo_init_new(&device
->hiz_clear_bo
, device
, 4096);
2208 if (device
->instance
->physicalDevice
.has_exec_async
)
2209 device
->hiz_clear_bo
.flags
|= EXEC_OBJECT_ASYNC
;
2211 if (device
->instance
->physicalDevice
.use_softpin
)
2212 device
->hiz_clear_bo
.flags
|= EXEC_OBJECT_PINNED
;
2214 anv_vma_alloc(device
, &device
->hiz_clear_bo
);
2216 uint32_t *map
= anv_gem_mmap(device
, device
->hiz_clear_bo
.gem_handle
,
2219 union isl_color_value hiz_clear
= { .u32
= { 0, } };
2220 hiz_clear
.f32
[0] = ANV_HZ_FC_VAL
;
2222 memcpy(map
, hiz_clear
.u32
, sizeof(hiz_clear
.u32
));
2223 anv_gem_munmap(map
, device
->hiz_clear_bo
.size
);
2227 get_bo_from_pool(struct gen_batch_decode_bo
*ret
,
2228 struct anv_block_pool
*pool
,
2231 for (uint32_t i
= 0; i
< pool
->nbos
; i
++) {
2232 uint64_t bo_address
= pool
->bos
[i
].offset
& (~0ull >> 16);
2233 uint32_t bo_size
= pool
->bos
[i
].size
;
2234 if (address
>= bo_address
&& address
< (bo_address
+ bo_size
)) {
2235 *ret
= (struct gen_batch_decode_bo
) {
2238 .map
= pool
->bos
[i
].map
,
2246 /* Finding a buffer for batch decoding */
2247 static struct gen_batch_decode_bo
2248 decode_get_bo(void *v_batch
, bool ppgtt
, uint64_t address
)
2250 struct anv_device
*device
= v_batch
;
2251 struct gen_batch_decode_bo ret_bo
= {};
2255 if (get_bo_from_pool(&ret_bo
, &device
->dynamic_state_pool
.block_pool
, address
))
2257 if (get_bo_from_pool(&ret_bo
, &device
->instruction_state_pool
.block_pool
, address
))
2259 if (get_bo_from_pool(&ret_bo
, &device
->binding_table_pool
.block_pool
, address
))
2261 if (get_bo_from_pool(&ret_bo
, &device
->surface_state_pool
.block_pool
, address
))
2264 if (!device
->cmd_buffer_being_decoded
)
2265 return (struct gen_batch_decode_bo
) { };
2267 struct anv_batch_bo
**bo
;
2269 u_vector_foreach(bo
, &device
->cmd_buffer_being_decoded
->seen_bbos
) {
2270 /* The decoder zeroes out the top 16 bits, so we need to as well */
2271 uint64_t bo_address
= (*bo
)->bo
.offset
& (~0ull >> 16);
2273 if (address
>= bo_address
&& address
< bo_address
+ (*bo
)->bo
.size
) {
2274 return (struct gen_batch_decode_bo
) {
2276 .size
= (*bo
)->bo
.size
,
2277 .map
= (*bo
)->bo
.map
,
2282 return (struct gen_batch_decode_bo
) { };
2285 VkResult
anv_CreateDevice(
2286 VkPhysicalDevice physicalDevice
,
2287 const VkDeviceCreateInfo
* pCreateInfo
,
2288 const VkAllocationCallbacks
* pAllocator
,
2291 ANV_FROM_HANDLE(anv_physical_device
, physical_device
, physicalDevice
);
2293 struct anv_device
*device
;
2295 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO
);
2297 struct anv_device_extension_table enabled_extensions
= { };
2298 for (uint32_t i
= 0; i
< pCreateInfo
->enabledExtensionCount
; i
++) {
2300 for (idx
= 0; idx
< ANV_DEVICE_EXTENSION_COUNT
; idx
++) {
2301 if (strcmp(pCreateInfo
->ppEnabledExtensionNames
[i
],
2302 anv_device_extensions
[idx
].extensionName
) == 0)
2306 if (idx
>= ANV_DEVICE_EXTENSION_COUNT
)
2307 return vk_error(VK_ERROR_EXTENSION_NOT_PRESENT
);
2309 if (!physical_device
->supported_extensions
.extensions
[idx
])
2310 return vk_error(VK_ERROR_EXTENSION_NOT_PRESENT
);
2312 enabled_extensions
.extensions
[idx
] = true;
2315 /* Check enabled features */
2316 if (pCreateInfo
->pEnabledFeatures
) {
2317 VkPhysicalDeviceFeatures supported_features
;
2318 anv_GetPhysicalDeviceFeatures(physicalDevice
, &supported_features
);
2319 VkBool32
*supported_feature
= (VkBool32
*)&supported_features
;
2320 VkBool32
*enabled_feature
= (VkBool32
*)pCreateInfo
->pEnabledFeatures
;
2321 unsigned num_features
= sizeof(VkPhysicalDeviceFeatures
) / sizeof(VkBool32
);
2322 for (uint32_t i
= 0; i
< num_features
; i
++) {
2323 if (enabled_feature
[i
] && !supported_feature
[i
])
2324 return vk_error(VK_ERROR_FEATURE_NOT_PRESENT
);
2328 /* Check requested queues and fail if we are requested to create any
2329 * queues with flags we don't support.
2331 assert(pCreateInfo
->queueCreateInfoCount
> 0);
2332 for (uint32_t i
= 0; i
< pCreateInfo
->queueCreateInfoCount
; i
++) {
2333 if (pCreateInfo
->pQueueCreateInfos
[i
].flags
!= 0)
2334 return vk_error(VK_ERROR_INITIALIZATION_FAILED
);
2337 /* Check if client specified queue priority. */
2338 const VkDeviceQueueGlobalPriorityCreateInfoEXT
*queue_priority
=
2339 vk_find_struct_const(pCreateInfo
->pQueueCreateInfos
[0].pNext
,
2340 DEVICE_QUEUE_GLOBAL_PRIORITY_CREATE_INFO_EXT
);
2342 VkQueueGlobalPriorityEXT priority
=
2343 queue_priority
? queue_priority
->globalPriority
:
2344 VK_QUEUE_GLOBAL_PRIORITY_MEDIUM_EXT
;
2346 device
= vk_alloc2(&physical_device
->instance
->alloc
, pAllocator
,
2348 VK_SYSTEM_ALLOCATION_SCOPE_DEVICE
);
2350 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
2352 if (INTEL_DEBUG
& DEBUG_BATCH
) {
2353 const unsigned decode_flags
=
2354 GEN_BATCH_DECODE_FULL
|
2355 ((INTEL_DEBUG
& DEBUG_COLOR
) ? GEN_BATCH_DECODE_IN_COLOR
: 0) |
2356 GEN_BATCH_DECODE_OFFSETS
|
2357 GEN_BATCH_DECODE_FLOATS
;
2359 gen_batch_decode_ctx_init(&device
->decoder_ctx
,
2360 &physical_device
->info
,
2361 stderr
, decode_flags
, NULL
,
2362 decode_get_bo
, NULL
, device
);
2365 device
->_loader_data
.loaderMagic
= ICD_LOADER_MAGIC
;
2366 device
->instance
= physical_device
->instance
;
2367 device
->chipset_id
= physical_device
->chipset_id
;
2368 device
->no_hw
= physical_device
->no_hw
;
2369 device
->_lost
= false;
2372 device
->alloc
= *pAllocator
;
2374 device
->alloc
= physical_device
->instance
->alloc
;
2376 /* XXX(chadv): Can we dup() physicalDevice->fd here? */
2377 device
->fd
= open(physical_device
->path
, O_RDWR
| O_CLOEXEC
);
2378 if (device
->fd
== -1) {
2379 result
= vk_error(VK_ERROR_INITIALIZATION_FAILED
);
2383 device
->context_id
= anv_gem_create_context(device
);
2384 if (device
->context_id
== -1) {
2385 result
= vk_error(VK_ERROR_INITIALIZATION_FAILED
);
2389 if (physical_device
->use_softpin
) {
2390 if (pthread_mutex_init(&device
->vma_mutex
, NULL
) != 0) {
2391 result
= vk_error(VK_ERROR_INITIALIZATION_FAILED
);
2395 /* keep the page with address zero out of the allocator */
2396 struct anv_memory_heap
*low_heap
=
2397 &physical_device
->memory
.heaps
[physical_device
->memory
.heap_count
- 1];
2398 util_vma_heap_init(&device
->vma_lo
, low_heap
->vma_start
, low_heap
->vma_size
);
2399 device
->vma_lo_available
= low_heap
->size
;
2401 struct anv_memory_heap
*high_heap
=
2402 &physical_device
->memory
.heaps
[0];
2403 util_vma_heap_init(&device
->vma_hi
, high_heap
->vma_start
, high_heap
->vma_size
);
2404 device
->vma_hi_available
= physical_device
->memory
.heap_count
== 1 ? 0 :
2408 list_inithead(&device
->memory_objects
);
2410 /* As per spec, the driver implementation may deny requests to acquire
2411 * a priority above the default priority (MEDIUM) if the caller does not
2412 * have sufficient privileges. In this scenario VK_ERROR_NOT_PERMITTED_EXT
2415 if (physical_device
->has_context_priority
) {
2416 int err
= anv_gem_set_context_param(device
->fd
, device
->context_id
,
2417 I915_CONTEXT_PARAM_PRIORITY
,
2418 vk_priority_to_gen(priority
));
2419 if (err
!= 0 && priority
> VK_QUEUE_GLOBAL_PRIORITY_MEDIUM_EXT
) {
2420 result
= vk_error(VK_ERROR_NOT_PERMITTED_EXT
);
2425 device
->info
= physical_device
->info
;
2426 device
->isl_dev
= physical_device
->isl_dev
;
2428 /* On Broadwell and later, we can use batch chaining to more efficiently
2429 * implement growing command buffers. Prior to Haswell, the kernel
2430 * command parser gets in the way and we have to fall back to growing
2433 device
->can_chain_batches
= device
->info
.gen
>= 8;
2435 device
->robust_buffer_access
= pCreateInfo
->pEnabledFeatures
&&
2436 pCreateInfo
->pEnabledFeatures
->robustBufferAccess
;
2437 device
->enabled_extensions
= enabled_extensions
;
2439 anv_device_init_dispatch(device
);
2441 if (pthread_mutex_init(&device
->mutex
, NULL
) != 0) {
2442 result
= vk_error(VK_ERROR_INITIALIZATION_FAILED
);
2443 goto fail_context_id
;
2446 pthread_condattr_t condattr
;
2447 if (pthread_condattr_init(&condattr
) != 0) {
2448 result
= vk_error(VK_ERROR_INITIALIZATION_FAILED
);
2451 if (pthread_condattr_setclock(&condattr
, CLOCK_MONOTONIC
) != 0) {
2452 pthread_condattr_destroy(&condattr
);
2453 result
= vk_error(VK_ERROR_INITIALIZATION_FAILED
);
2456 if (pthread_cond_init(&device
->queue_submit
, &condattr
) != 0) {
2457 pthread_condattr_destroy(&condattr
);
2458 result
= vk_error(VK_ERROR_INITIALIZATION_FAILED
);
2461 pthread_condattr_destroy(&condattr
);
2464 (physical_device
->supports_48bit_addresses
? EXEC_OBJECT_SUPPORTS_48B_ADDRESS
: 0) |
2465 (physical_device
->has_exec_async
? EXEC_OBJECT_ASYNC
: 0) |
2466 (physical_device
->has_exec_capture
? EXEC_OBJECT_CAPTURE
: 0) |
2467 (physical_device
->use_softpin
? EXEC_OBJECT_PINNED
: 0);
2469 anv_bo_pool_init(&device
->batch_bo_pool
, device
, bo_flags
);
2471 result
= anv_bo_cache_init(&device
->bo_cache
);
2472 if (result
!= VK_SUCCESS
)
2473 goto fail_batch_bo_pool
;
2475 if (!physical_device
->use_softpin
)
2476 bo_flags
&= ~EXEC_OBJECT_SUPPORTS_48B_ADDRESS
;
2478 result
= anv_state_pool_init(&device
->dynamic_state_pool
, device
,
2479 DYNAMIC_STATE_POOL_MIN_ADDRESS
,
2482 if (result
!= VK_SUCCESS
)
2485 result
= anv_state_pool_init(&device
->instruction_state_pool
, device
,
2486 INSTRUCTION_STATE_POOL_MIN_ADDRESS
,
2489 if (result
!= VK_SUCCESS
)
2490 goto fail_dynamic_state_pool
;
2492 result
= anv_state_pool_init(&device
->surface_state_pool
, device
,
2493 SURFACE_STATE_POOL_MIN_ADDRESS
,
2496 if (result
!= VK_SUCCESS
)
2497 goto fail_instruction_state_pool
;
2499 if (physical_device
->use_softpin
) {
2500 result
= anv_state_pool_init(&device
->binding_table_pool
, device
,
2501 BINDING_TABLE_POOL_MIN_ADDRESS
,
2504 if (result
!= VK_SUCCESS
)
2505 goto fail_surface_state_pool
;
2508 result
= anv_bo_init_new(&device
->workaround_bo
, device
, 4096);
2509 if (result
!= VK_SUCCESS
)
2510 goto fail_binding_table_pool
;
2512 if (physical_device
->use_softpin
)
2513 device
->workaround_bo
.flags
|= EXEC_OBJECT_PINNED
;
2515 if (!anv_vma_alloc(device
, &device
->workaround_bo
))
2516 goto fail_workaround_bo
;
2518 anv_device_init_trivial_batch(device
);
2520 if (device
->info
.gen
>= 10)
2521 anv_device_init_hiz_clear_value_bo(device
);
2523 anv_scratch_pool_init(device
, &device
->scratch_pool
);
2525 anv_queue_init(device
, &device
->queue
);
2527 switch (device
->info
.gen
) {
2529 if (!device
->info
.is_haswell
)
2530 result
= gen7_init_device_state(device
);
2532 result
= gen75_init_device_state(device
);
2535 result
= gen8_init_device_state(device
);
2538 result
= gen9_init_device_state(device
);
2541 result
= gen10_init_device_state(device
);
2544 result
= gen11_init_device_state(device
);
2547 result
= gen12_init_device_state(device
);
2550 /* Shouldn't get here as we don't create physical devices for any other
2552 unreachable("unhandled gen");
2554 if (result
!= VK_SUCCESS
)
2555 goto fail_workaround_bo
;
2557 anv_pipeline_cache_init(&device
->default_pipeline_cache
, device
, true);
2559 anv_device_init_blorp(device
);
2561 anv_device_init_border_colors(device
);
2563 *pDevice
= anv_device_to_handle(device
);
2568 anv_queue_finish(&device
->queue
);
2569 anv_scratch_pool_finish(device
, &device
->scratch_pool
);
2570 anv_gem_munmap(device
->workaround_bo
.map
, device
->workaround_bo
.size
);
2571 anv_gem_close(device
, device
->workaround_bo
.gem_handle
);
2572 fail_binding_table_pool
:
2573 if (physical_device
->use_softpin
)
2574 anv_state_pool_finish(&device
->binding_table_pool
);
2575 fail_surface_state_pool
:
2576 anv_state_pool_finish(&device
->surface_state_pool
);
2577 fail_instruction_state_pool
:
2578 anv_state_pool_finish(&device
->instruction_state_pool
);
2579 fail_dynamic_state_pool
:
2580 anv_state_pool_finish(&device
->dynamic_state_pool
);
2582 anv_bo_cache_finish(&device
->bo_cache
);
2584 anv_bo_pool_finish(&device
->batch_bo_pool
);
2585 pthread_cond_destroy(&device
->queue_submit
);
2587 pthread_mutex_destroy(&device
->mutex
);
2589 anv_gem_destroy_context(device
, device
->context_id
);
2593 vk_free(&device
->alloc
, device
);
2598 void anv_DestroyDevice(
2600 const VkAllocationCallbacks
* pAllocator
)
2602 ANV_FROM_HANDLE(anv_device
, device
, _device
);
2603 struct anv_physical_device
*physical_device
;
2608 physical_device
= &device
->instance
->physicalDevice
;
2610 anv_device_finish_blorp(device
);
2612 anv_pipeline_cache_finish(&device
->default_pipeline_cache
);
2614 anv_queue_finish(&device
->queue
);
2616 #ifdef HAVE_VALGRIND
2617 /* We only need to free these to prevent valgrind errors. The backing
2618 * BO will go away in a couple of lines so we don't actually leak.
2620 anv_state_pool_free(&device
->dynamic_state_pool
, device
->border_colors
);
2621 anv_state_pool_free(&device
->dynamic_state_pool
, device
->slice_hash
);
2624 anv_scratch_pool_finish(device
, &device
->scratch_pool
);
2626 anv_gem_munmap(device
->workaround_bo
.map
, device
->workaround_bo
.size
);
2627 anv_vma_free(device
, &device
->workaround_bo
);
2628 anv_gem_close(device
, device
->workaround_bo
.gem_handle
);
2630 anv_vma_free(device
, &device
->trivial_batch_bo
);
2631 anv_gem_close(device
, device
->trivial_batch_bo
.gem_handle
);
2632 if (device
->info
.gen
>= 10)
2633 anv_gem_close(device
, device
->hiz_clear_bo
.gem_handle
);
2635 if (physical_device
->use_softpin
)
2636 anv_state_pool_finish(&device
->binding_table_pool
);
2637 anv_state_pool_finish(&device
->surface_state_pool
);
2638 anv_state_pool_finish(&device
->instruction_state_pool
);
2639 anv_state_pool_finish(&device
->dynamic_state_pool
);
2641 anv_bo_cache_finish(&device
->bo_cache
);
2643 anv_bo_pool_finish(&device
->batch_bo_pool
);
2645 pthread_cond_destroy(&device
->queue_submit
);
2646 pthread_mutex_destroy(&device
->mutex
);
2648 anv_gem_destroy_context(device
, device
->context_id
);
2650 if (INTEL_DEBUG
& DEBUG_BATCH
)
2651 gen_batch_decode_ctx_finish(&device
->decoder_ctx
);
2655 vk_free(&device
->alloc
, device
);
2658 VkResult
anv_EnumerateInstanceLayerProperties(
2659 uint32_t* pPropertyCount
,
2660 VkLayerProperties
* pProperties
)
2662 if (pProperties
== NULL
) {
2663 *pPropertyCount
= 0;
2667 /* None supported at this time */
2668 return vk_error(VK_ERROR_LAYER_NOT_PRESENT
);
2671 VkResult
anv_EnumerateDeviceLayerProperties(
2672 VkPhysicalDevice physicalDevice
,
2673 uint32_t* pPropertyCount
,
2674 VkLayerProperties
* pProperties
)
2676 if (pProperties
== NULL
) {
2677 *pPropertyCount
= 0;
2681 /* None supported at this time */
2682 return vk_error(VK_ERROR_LAYER_NOT_PRESENT
);
2685 void anv_GetDeviceQueue(
2687 uint32_t queueNodeIndex
,
2688 uint32_t queueIndex
,
2691 ANV_FROM_HANDLE(anv_device
, device
, _device
);
2693 assert(queueIndex
== 0);
2695 *pQueue
= anv_queue_to_handle(&device
->queue
);
2698 void anv_GetDeviceQueue2(
2700 const VkDeviceQueueInfo2
* pQueueInfo
,
2703 ANV_FROM_HANDLE(anv_device
, device
, _device
);
2705 assert(pQueueInfo
->queueIndex
== 0);
2707 if (pQueueInfo
->flags
== device
->queue
.flags
)
2708 *pQueue
= anv_queue_to_handle(&device
->queue
);
2714 _anv_device_set_lost(struct anv_device
*device
,
2715 const char *file
, int line
,
2716 const char *msg
, ...)
2721 device
->_lost
= true;
2724 err
= __vk_errorv(device
->instance
, device
,
2725 VK_DEBUG_REPORT_OBJECT_TYPE_DEVICE_EXT
,
2726 VK_ERROR_DEVICE_LOST
, file
, line
, msg
, ap
);
2729 if (env_var_as_boolean("ANV_ABORT_ON_DEVICE_LOSS", false))
2736 anv_device_query_status(struct anv_device
*device
)
2738 /* This isn't likely as most of the callers of this function already check
2739 * for it. However, it doesn't hurt to check and it potentially lets us
2742 if (anv_device_is_lost(device
))
2743 return VK_ERROR_DEVICE_LOST
;
2745 uint32_t active
, pending
;
2746 int ret
= anv_gem_gpu_get_reset_stats(device
, &active
, &pending
);
2748 /* We don't know the real error. */
2749 return anv_device_set_lost(device
, "get_reset_stats failed: %m");
2753 return anv_device_set_lost(device
, "GPU hung on one of our command buffers");
2754 } else if (pending
) {
2755 return anv_device_set_lost(device
, "GPU hung with commands in-flight");
2762 anv_device_bo_busy(struct anv_device
*device
, struct anv_bo
*bo
)
2764 /* Note: This only returns whether or not the BO is in use by an i915 GPU.
2765 * Other usages of the BO (such as on different hardware) will not be
2766 * flagged as "busy" by this ioctl. Use with care.
2768 int ret
= anv_gem_busy(device
, bo
->gem_handle
);
2770 return VK_NOT_READY
;
2771 } else if (ret
== -1) {
2772 /* We don't know the real error. */
2773 return anv_device_set_lost(device
, "gem wait failed: %m");
2776 /* Query for device status after the busy call. If the BO we're checking
2777 * got caught in a GPU hang we don't want to return VK_SUCCESS to the
2778 * client because it clearly doesn't have valid data. Yes, this most
2779 * likely means an ioctl, but we just did an ioctl to query the busy status
2780 * so it's no great loss.
2782 return anv_device_query_status(device
);
2786 anv_device_wait(struct anv_device
*device
, struct anv_bo
*bo
,
2789 int ret
= anv_gem_wait(device
, bo
->gem_handle
, &timeout
);
2790 if (ret
== -1 && errno
== ETIME
) {
2792 } else if (ret
== -1) {
2793 /* We don't know the real error. */
2794 return anv_device_set_lost(device
, "gem wait failed: %m");
2797 /* Query for device status after the wait. If the BO we're waiting on got
2798 * caught in a GPU hang we don't want to return VK_SUCCESS to the client
2799 * because it clearly doesn't have valid data. Yes, this most likely means
2800 * an ioctl, but we just did an ioctl to wait so it's no great loss.
2802 return anv_device_query_status(device
);
2805 VkResult
anv_DeviceWaitIdle(
2808 ANV_FROM_HANDLE(anv_device
, device
, _device
);
2809 if (anv_device_is_lost(device
))
2810 return VK_ERROR_DEVICE_LOST
;
2812 struct anv_batch batch
;
2815 batch
.start
= batch
.next
= cmds
;
2816 batch
.end
= (void *) cmds
+ sizeof(cmds
);
2818 anv_batch_emit(&batch
, GEN7_MI_BATCH_BUFFER_END
, bbe
);
2819 anv_batch_emit(&batch
, GEN7_MI_NOOP
, noop
);
2821 return anv_device_submit_simple_batch(device
, &batch
);
2825 anv_vma_alloc(struct anv_device
*device
, struct anv_bo
*bo
)
2827 if (!(bo
->flags
& EXEC_OBJECT_PINNED
))
2830 pthread_mutex_lock(&device
->vma_mutex
);
2834 if (bo
->flags
& EXEC_OBJECT_SUPPORTS_48B_ADDRESS
&&
2835 device
->vma_hi_available
>= bo
->size
) {
2836 uint64_t addr
= util_vma_heap_alloc(&device
->vma_hi
, bo
->size
, 4096);
2838 bo
->offset
= gen_canonical_address(addr
);
2839 assert(addr
== gen_48b_address(bo
->offset
));
2840 device
->vma_hi_available
-= bo
->size
;
2844 if (bo
->offset
== 0 && device
->vma_lo_available
>= bo
->size
) {
2845 uint64_t addr
= util_vma_heap_alloc(&device
->vma_lo
, bo
->size
, 4096);
2847 bo
->offset
= gen_canonical_address(addr
);
2848 assert(addr
== gen_48b_address(bo
->offset
));
2849 device
->vma_lo_available
-= bo
->size
;
2853 pthread_mutex_unlock(&device
->vma_mutex
);
2855 return bo
->offset
!= 0;
2859 anv_vma_free(struct anv_device
*device
, struct anv_bo
*bo
)
2861 if (!(bo
->flags
& EXEC_OBJECT_PINNED
))
2864 const uint64_t addr_48b
= gen_48b_address(bo
->offset
);
2866 pthread_mutex_lock(&device
->vma_mutex
);
2868 if (addr_48b
>= LOW_HEAP_MIN_ADDRESS
&&
2869 addr_48b
<= LOW_HEAP_MAX_ADDRESS
) {
2870 util_vma_heap_free(&device
->vma_lo
, addr_48b
, bo
->size
);
2871 device
->vma_lo_available
+= bo
->size
;
2873 ASSERTED
const struct anv_physical_device
*physical_device
=
2874 &device
->instance
->physicalDevice
;
2875 assert(addr_48b
>= physical_device
->memory
.heaps
[0].vma_start
&&
2876 addr_48b
< (physical_device
->memory
.heaps
[0].vma_start
+
2877 physical_device
->memory
.heaps
[0].vma_size
));
2878 util_vma_heap_free(&device
->vma_hi
, addr_48b
, bo
->size
);
2879 device
->vma_hi_available
+= bo
->size
;
2882 pthread_mutex_unlock(&device
->vma_mutex
);
2888 anv_bo_init_new(struct anv_bo
*bo
, struct anv_device
*device
, uint64_t size
)
2890 uint32_t gem_handle
= anv_gem_create(device
, size
);
2892 return vk_error(VK_ERROR_OUT_OF_DEVICE_MEMORY
);
2894 anv_bo_init(bo
, gem_handle
, size
);
2899 VkResult
anv_AllocateMemory(
2901 const VkMemoryAllocateInfo
* pAllocateInfo
,
2902 const VkAllocationCallbacks
* pAllocator
,
2903 VkDeviceMemory
* pMem
)
2905 ANV_FROM_HANDLE(anv_device
, device
, _device
);
2906 struct anv_physical_device
*pdevice
= &device
->instance
->physicalDevice
;
2907 struct anv_device_memory
*mem
;
2908 VkResult result
= VK_SUCCESS
;
2910 assert(pAllocateInfo
->sType
== VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO
);
2912 /* The Vulkan 1.0.33 spec says "allocationSize must be greater than 0". */
2913 assert(pAllocateInfo
->allocationSize
> 0);
2915 if (pAllocateInfo
->allocationSize
> MAX_MEMORY_ALLOCATION_SIZE
)
2916 return VK_ERROR_OUT_OF_DEVICE_MEMORY
;
2918 /* FINISHME: Fail if allocation request exceeds heap size. */
2920 mem
= vk_alloc2(&device
->alloc
, pAllocator
, sizeof(*mem
), 8,
2921 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
2923 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
2925 assert(pAllocateInfo
->memoryTypeIndex
< pdevice
->memory
.type_count
);
2926 mem
->type
= &pdevice
->memory
.types
[pAllocateInfo
->memoryTypeIndex
];
2930 mem
->host_ptr
= NULL
;
2932 uint64_t bo_flags
= 0;
2934 assert(mem
->type
->heapIndex
< pdevice
->memory
.heap_count
);
2935 if (pdevice
->memory
.heaps
[mem
->type
->heapIndex
].supports_48bit_addresses
)
2936 bo_flags
|= EXEC_OBJECT_SUPPORTS_48B_ADDRESS
;
2938 const struct wsi_memory_allocate_info
*wsi_info
=
2939 vk_find_struct_const(pAllocateInfo
->pNext
, WSI_MEMORY_ALLOCATE_INFO_MESA
);
2940 if (wsi_info
&& wsi_info
->implicit_sync
) {
2941 /* We need to set the WRITE flag on window system buffers so that GEM
2942 * will know we're writing to them and synchronize uses on other rings
2943 * (eg if the display server uses the blitter ring).
2945 bo_flags
|= EXEC_OBJECT_WRITE
;
2946 } else if (pdevice
->has_exec_async
) {
2947 bo_flags
|= EXEC_OBJECT_ASYNC
;
2950 if (pdevice
->use_softpin
)
2951 bo_flags
|= EXEC_OBJECT_PINNED
;
2953 const VkExportMemoryAllocateInfo
*export_info
=
2954 vk_find_struct_const(pAllocateInfo
->pNext
, EXPORT_MEMORY_ALLOCATE_INFO
);
2956 /* Check if we need to support Android HW buffer export. If so,
2957 * create AHardwareBuffer and import memory from it.
2959 bool android_export
= false;
2960 if (export_info
&& export_info
->handleTypes
&
2961 VK_EXTERNAL_MEMORY_HANDLE_TYPE_ANDROID_HARDWARE_BUFFER_BIT_ANDROID
)
2962 android_export
= true;
2964 /* Android memory import. */
2965 const struct VkImportAndroidHardwareBufferInfoANDROID
*ahw_import_info
=
2966 vk_find_struct_const(pAllocateInfo
->pNext
,
2967 IMPORT_ANDROID_HARDWARE_BUFFER_INFO_ANDROID
);
2969 if (ahw_import_info
) {
2970 result
= anv_import_ahw_memory(_device
, mem
, ahw_import_info
);
2971 if (result
!= VK_SUCCESS
)
2975 } else if (android_export
) {
2976 result
= anv_create_ahw_memory(_device
, mem
, pAllocateInfo
);
2977 if (result
!= VK_SUCCESS
)
2980 const struct VkImportAndroidHardwareBufferInfoANDROID import_info
= {
2983 result
= anv_import_ahw_memory(_device
, mem
, &import_info
);
2984 if (result
!= VK_SUCCESS
)
2990 const VkImportMemoryFdInfoKHR
*fd_info
=
2991 vk_find_struct_const(pAllocateInfo
->pNext
, IMPORT_MEMORY_FD_INFO_KHR
);
2993 /* The Vulkan spec permits handleType to be 0, in which case the struct is
2996 if (fd_info
&& fd_info
->handleType
) {
2997 /* At the moment, we support only the below handle types. */
2998 assert(fd_info
->handleType
==
2999 VK_EXTERNAL_MEMORY_HANDLE_TYPE_OPAQUE_FD_BIT
||
3000 fd_info
->handleType
==
3001 VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_EXT
);
3003 result
= anv_bo_cache_import(device
, &device
->bo_cache
, fd_info
->fd
,
3004 bo_flags
| ANV_BO_EXTERNAL
, &mem
->bo
);
3005 if (result
!= VK_SUCCESS
)
3008 VkDeviceSize aligned_alloc_size
=
3009 align_u64(pAllocateInfo
->allocationSize
, 4096);
3011 /* For security purposes, we reject importing the bo if it's smaller
3012 * than the requested allocation size. This prevents a malicious client
3013 * from passing a buffer to a trusted client, lying about the size, and
3014 * telling the trusted client to try and texture from an image that goes
3015 * out-of-bounds. This sort of thing could lead to GPU hangs or worse
3016 * in the trusted client. The trusted client can protect itself against
3017 * this sort of attack but only if it can trust the buffer size.
3019 if (mem
->bo
->size
< aligned_alloc_size
) {
3020 result
= vk_errorf(device
->instance
, device
,
3021 VK_ERROR_INVALID_EXTERNAL_HANDLE
,
3022 "aligned allocationSize too large for "
3023 "VK_EXTERNAL_MEMORY_HANDLE_TYPE_OPAQUE_FD_BIT: "
3024 "%"PRIu64
"B > %"PRIu64
"B",
3025 aligned_alloc_size
, mem
->bo
->size
);
3026 anv_bo_cache_release(device
, &device
->bo_cache
, mem
->bo
);
3030 /* From the Vulkan spec:
3032 * "Importing memory from a file descriptor transfers ownership of
3033 * the file descriptor from the application to the Vulkan
3034 * implementation. The application must not perform any operations on
3035 * the file descriptor after a successful import."
3037 * If the import fails, we leave the file descriptor open.
3043 const VkImportMemoryHostPointerInfoEXT
*host_ptr_info
=
3044 vk_find_struct_const(pAllocateInfo
->pNext
,
3045 IMPORT_MEMORY_HOST_POINTER_INFO_EXT
);
3046 if (host_ptr_info
&& host_ptr_info
->handleType
) {
3047 if (host_ptr_info
->handleType
==
3048 VK_EXTERNAL_MEMORY_HANDLE_TYPE_HOST_MAPPED_FOREIGN_MEMORY_BIT_EXT
) {
3049 result
= vk_error(VK_ERROR_INVALID_EXTERNAL_HANDLE
);
3053 assert(host_ptr_info
->handleType
==
3054 VK_EXTERNAL_MEMORY_HANDLE_TYPE_HOST_ALLOCATION_BIT_EXT
);
3056 result
= anv_bo_cache_import_host_ptr(
3057 device
, &device
->bo_cache
, host_ptr_info
->pHostPointer
,
3058 pAllocateInfo
->allocationSize
, bo_flags
, &mem
->bo
);
3060 if (result
!= VK_SUCCESS
)
3063 mem
->host_ptr
= host_ptr_info
->pHostPointer
;
3067 /* Regular allocate (not importing memory). */
3069 if (export_info
&& export_info
->handleTypes
)
3070 bo_flags
|= ANV_BO_EXTERNAL
;
3072 result
= anv_bo_cache_alloc(device
, &device
->bo_cache
,
3073 pAllocateInfo
->allocationSize
, bo_flags
,
3075 if (result
!= VK_SUCCESS
)
3078 const VkMemoryDedicatedAllocateInfo
*dedicated_info
=
3079 vk_find_struct_const(pAllocateInfo
->pNext
, MEMORY_DEDICATED_ALLOCATE_INFO
);
3080 if (dedicated_info
&& dedicated_info
->image
!= VK_NULL_HANDLE
) {
3081 ANV_FROM_HANDLE(anv_image
, image
, dedicated_info
->image
);
3083 /* Some legacy (non-modifiers) consumers need the tiling to be set on
3084 * the BO. In this case, we have a dedicated allocation.
3086 if (image
->needs_set_tiling
) {
3087 const uint32_t i915_tiling
=
3088 isl_tiling_to_i915_tiling(image
->planes
[0].surface
.isl
.tiling
);
3089 int ret
= anv_gem_set_tiling(device
, mem
->bo
->gem_handle
,
3090 image
->planes
[0].surface
.isl
.row_pitch_B
,
3093 anv_bo_cache_release(device
, &device
->bo_cache
, mem
->bo
);
3094 return vk_errorf(device
->instance
, NULL
,
3095 VK_ERROR_OUT_OF_DEVICE_MEMORY
,
3096 "failed to set BO tiling: %m");
3102 pthread_mutex_lock(&device
->mutex
);
3103 list_addtail(&mem
->link
, &device
->memory_objects
);
3104 pthread_mutex_unlock(&device
->mutex
);
3106 *pMem
= anv_device_memory_to_handle(mem
);
3108 p_atomic_add(&pdevice
->memory
.heaps
[mem
->type
->heapIndex
].used
,
3114 vk_free2(&device
->alloc
, pAllocator
, mem
);
3119 VkResult
anv_GetMemoryFdKHR(
3121 const VkMemoryGetFdInfoKHR
* pGetFdInfo
,
3124 ANV_FROM_HANDLE(anv_device
, dev
, device_h
);
3125 ANV_FROM_HANDLE(anv_device_memory
, mem
, pGetFdInfo
->memory
);
3127 assert(pGetFdInfo
->sType
== VK_STRUCTURE_TYPE_MEMORY_GET_FD_INFO_KHR
);
3129 assert(pGetFdInfo
->handleType
== VK_EXTERNAL_MEMORY_HANDLE_TYPE_OPAQUE_FD_BIT
||
3130 pGetFdInfo
->handleType
== VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_EXT
);
3132 return anv_bo_cache_export(dev
, &dev
->bo_cache
, mem
->bo
, pFd
);
3135 VkResult
anv_GetMemoryFdPropertiesKHR(
3137 VkExternalMemoryHandleTypeFlagBits handleType
,
3139 VkMemoryFdPropertiesKHR
* pMemoryFdProperties
)
3141 ANV_FROM_HANDLE(anv_device
, device
, _device
);
3142 struct anv_physical_device
*pdevice
= &device
->instance
->physicalDevice
;
3144 switch (handleType
) {
3145 case VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_EXT
:
3146 /* dma-buf can be imported as any memory type */
3147 pMemoryFdProperties
->memoryTypeBits
=
3148 (1 << pdevice
->memory
.type_count
) - 1;
3152 /* The valid usage section for this function says:
3154 * "handleType must not be one of the handle types defined as
3157 * So opaque handle types fall into the default "unsupported" case.
3159 return vk_error(VK_ERROR_INVALID_EXTERNAL_HANDLE
);
3163 VkResult
anv_GetMemoryHostPointerPropertiesEXT(
3165 VkExternalMemoryHandleTypeFlagBits handleType
,
3166 const void* pHostPointer
,
3167 VkMemoryHostPointerPropertiesEXT
* pMemoryHostPointerProperties
)
3169 ANV_FROM_HANDLE(anv_device
, device
, _device
);
3171 assert(pMemoryHostPointerProperties
->sType
==
3172 VK_STRUCTURE_TYPE_MEMORY_HOST_POINTER_PROPERTIES_EXT
);
3174 switch (handleType
) {
3175 case VK_EXTERNAL_MEMORY_HANDLE_TYPE_HOST_ALLOCATION_BIT_EXT
: {
3176 struct anv_physical_device
*pdevice
= &device
->instance
->physicalDevice
;
3178 /* Host memory can be imported as any memory type. */
3179 pMemoryHostPointerProperties
->memoryTypeBits
=
3180 (1ull << pdevice
->memory
.type_count
) - 1;
3185 return VK_ERROR_INVALID_EXTERNAL_HANDLE
;
3189 void anv_FreeMemory(
3191 VkDeviceMemory _mem
,
3192 const VkAllocationCallbacks
* pAllocator
)
3194 ANV_FROM_HANDLE(anv_device
, device
, _device
);
3195 ANV_FROM_HANDLE(anv_device_memory
, mem
, _mem
);
3196 struct anv_physical_device
*pdevice
= &device
->instance
->physicalDevice
;
3201 pthread_mutex_lock(&device
->mutex
);
3202 list_del(&mem
->link
);
3203 pthread_mutex_unlock(&device
->mutex
);
3206 anv_UnmapMemory(_device
, _mem
);
3208 p_atomic_add(&pdevice
->memory
.heaps
[mem
->type
->heapIndex
].used
,
3211 anv_bo_cache_release(device
, &device
->bo_cache
, mem
->bo
);
3213 #if defined(ANDROID) && ANDROID_API_LEVEL >= 26
3215 AHardwareBuffer_release(mem
->ahw
);
3218 vk_free2(&device
->alloc
, pAllocator
, mem
);
3221 VkResult
anv_MapMemory(
3223 VkDeviceMemory _memory
,
3224 VkDeviceSize offset
,
3226 VkMemoryMapFlags flags
,
3229 ANV_FROM_HANDLE(anv_device
, device
, _device
);
3230 ANV_FROM_HANDLE(anv_device_memory
, mem
, _memory
);
3237 if (mem
->host_ptr
) {
3238 *ppData
= mem
->host_ptr
+ offset
;
3242 if (size
== VK_WHOLE_SIZE
)
3243 size
= mem
->bo
->size
- offset
;
3245 /* From the Vulkan spec version 1.0.32 docs for MapMemory:
3247 * * If size is not equal to VK_WHOLE_SIZE, size must be greater than 0
3248 * assert(size != 0);
3249 * * If size is not equal to VK_WHOLE_SIZE, size must be less than or
3250 * equal to the size of the memory minus offset
3253 assert(offset
+ size
<= mem
->bo
->size
);
3255 /* FIXME: Is this supposed to be thread safe? Since vkUnmapMemory() only
3256 * takes a VkDeviceMemory pointer, it seems like only one map of the memory
3257 * at a time is valid. We could just mmap up front and return an offset
3258 * pointer here, but that may exhaust virtual memory on 32 bit
3261 uint32_t gem_flags
= 0;
3263 if (!device
->info
.has_llc
&&
3264 (mem
->type
->propertyFlags
& VK_MEMORY_PROPERTY_HOST_COHERENT_BIT
))
3265 gem_flags
|= I915_MMAP_WC
;
3267 /* GEM will fail to map if the offset isn't 4k-aligned. Round down. */
3268 uint64_t map_offset
= offset
& ~4095ull;
3269 assert(offset
>= map_offset
);
3270 uint64_t map_size
= (offset
+ size
) - map_offset
;
3272 /* Let's map whole pages */
3273 map_size
= align_u64(map_size
, 4096);
3275 void *map
= anv_gem_mmap(device
, mem
->bo
->gem_handle
,
3276 map_offset
, map_size
, gem_flags
);
3277 if (map
== MAP_FAILED
)
3278 return vk_error(VK_ERROR_MEMORY_MAP_FAILED
);
3281 mem
->map_size
= map_size
;
3283 *ppData
= mem
->map
+ (offset
- map_offset
);
3288 void anv_UnmapMemory(
3290 VkDeviceMemory _memory
)
3292 ANV_FROM_HANDLE(anv_device_memory
, mem
, _memory
);
3294 if (mem
== NULL
|| mem
->host_ptr
)
3297 anv_gem_munmap(mem
->map
, mem
->map_size
);
3304 clflush_mapped_ranges(struct anv_device
*device
,
3306 const VkMappedMemoryRange
*ranges
)
3308 for (uint32_t i
= 0; i
< count
; i
++) {
3309 ANV_FROM_HANDLE(anv_device_memory
, mem
, ranges
[i
].memory
);
3310 if (ranges
[i
].offset
>= mem
->map_size
)
3313 gen_clflush_range(mem
->map
+ ranges
[i
].offset
,
3314 MIN2(ranges
[i
].size
, mem
->map_size
- ranges
[i
].offset
));
3318 VkResult
anv_FlushMappedMemoryRanges(
3320 uint32_t memoryRangeCount
,
3321 const VkMappedMemoryRange
* pMemoryRanges
)
3323 ANV_FROM_HANDLE(anv_device
, device
, _device
);
3325 if (device
->info
.has_llc
)
3328 /* Make sure the writes we're flushing have landed. */
3329 __builtin_ia32_mfence();
3331 clflush_mapped_ranges(device
, memoryRangeCount
, pMemoryRanges
);
3336 VkResult
anv_InvalidateMappedMemoryRanges(
3338 uint32_t memoryRangeCount
,
3339 const VkMappedMemoryRange
* pMemoryRanges
)
3341 ANV_FROM_HANDLE(anv_device
, device
, _device
);
3343 if (device
->info
.has_llc
)
3346 clflush_mapped_ranges(device
, memoryRangeCount
, pMemoryRanges
);
3348 /* Make sure no reads get moved up above the invalidate. */
3349 __builtin_ia32_mfence();
3354 void anv_GetBufferMemoryRequirements(
3357 VkMemoryRequirements
* pMemoryRequirements
)
3359 ANV_FROM_HANDLE(anv_buffer
, buffer
, _buffer
);
3360 ANV_FROM_HANDLE(anv_device
, device
, _device
);
3361 struct anv_physical_device
*pdevice
= &device
->instance
->physicalDevice
;
3363 /* The Vulkan spec (git aaed022) says:
3365 * memoryTypeBits is a bitfield and contains one bit set for every
3366 * supported memory type for the resource. The bit `1<<i` is set if and
3367 * only if the memory type `i` in the VkPhysicalDeviceMemoryProperties
3368 * structure for the physical device is supported.
3370 uint32_t memory_types
= 0;
3371 for (uint32_t i
= 0; i
< pdevice
->memory
.type_count
; i
++) {
3372 uint32_t valid_usage
= pdevice
->memory
.types
[i
].valid_buffer_usage
;
3373 if ((valid_usage
& buffer
->usage
) == buffer
->usage
)
3374 memory_types
|= (1u << i
);
3377 /* Base alignment requirement of a cache line */
3378 uint32_t alignment
= 16;
3380 /* We need an alignment of 32 for pushing UBOs */
3381 if (buffer
->usage
& VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT
)
3382 alignment
= MAX2(alignment
, 32);
3384 pMemoryRequirements
->size
= buffer
->size
;
3385 pMemoryRequirements
->alignment
= alignment
;
3387 /* Storage and Uniform buffers should have their size aligned to
3388 * 32-bits to avoid boundary checks when last DWord is not complete.
3389 * This would ensure that not internal padding would be needed for
3392 if (device
->robust_buffer_access
&&
3393 (buffer
->usage
& VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT
||
3394 buffer
->usage
& VK_BUFFER_USAGE_STORAGE_BUFFER_BIT
))
3395 pMemoryRequirements
->size
= align_u64(buffer
->size
, 4);
3397 pMemoryRequirements
->memoryTypeBits
= memory_types
;
3400 void anv_GetBufferMemoryRequirements2(
3402 const VkBufferMemoryRequirementsInfo2
* pInfo
,
3403 VkMemoryRequirements2
* pMemoryRequirements
)
3405 anv_GetBufferMemoryRequirements(_device
, pInfo
->buffer
,
3406 &pMemoryRequirements
->memoryRequirements
);
3408 vk_foreach_struct(ext
, pMemoryRequirements
->pNext
) {
3409 switch (ext
->sType
) {
3410 case VK_STRUCTURE_TYPE_MEMORY_DEDICATED_REQUIREMENTS
: {
3411 VkMemoryDedicatedRequirements
*requirements
= (void *)ext
;
3412 requirements
->prefersDedicatedAllocation
= false;
3413 requirements
->requiresDedicatedAllocation
= false;
3418 anv_debug_ignored_stype(ext
->sType
);
3424 void anv_GetImageMemoryRequirements(
3427 VkMemoryRequirements
* pMemoryRequirements
)
3429 ANV_FROM_HANDLE(anv_image
, image
, _image
);
3430 ANV_FROM_HANDLE(anv_device
, device
, _device
);
3431 struct anv_physical_device
*pdevice
= &device
->instance
->physicalDevice
;
3433 /* The Vulkan spec (git aaed022) says:
3435 * memoryTypeBits is a bitfield and contains one bit set for every
3436 * supported memory type for the resource. The bit `1<<i` is set if and
3437 * only if the memory type `i` in the VkPhysicalDeviceMemoryProperties
3438 * structure for the physical device is supported.
3440 * All types are currently supported for images.
3442 uint32_t memory_types
= (1ull << pdevice
->memory
.type_count
) - 1;
3444 /* We must have image allocated or imported at this point. According to the
3445 * specification, external images must have been bound to memory before
3446 * calling GetImageMemoryRequirements.
3448 assert(image
->size
> 0);
3450 pMemoryRequirements
->size
= image
->size
;
3451 pMemoryRequirements
->alignment
= image
->alignment
;
3452 pMemoryRequirements
->memoryTypeBits
= memory_types
;
3455 void anv_GetImageMemoryRequirements2(
3457 const VkImageMemoryRequirementsInfo2
* pInfo
,
3458 VkMemoryRequirements2
* pMemoryRequirements
)
3460 ANV_FROM_HANDLE(anv_device
, device
, _device
);
3461 ANV_FROM_HANDLE(anv_image
, image
, pInfo
->image
);
3463 anv_GetImageMemoryRequirements(_device
, pInfo
->image
,
3464 &pMemoryRequirements
->memoryRequirements
);
3466 vk_foreach_struct_const(ext
, pInfo
->pNext
) {
3467 switch (ext
->sType
) {
3468 case VK_STRUCTURE_TYPE_IMAGE_PLANE_MEMORY_REQUIREMENTS_INFO
: {
3469 struct anv_physical_device
*pdevice
= &device
->instance
->physicalDevice
;
3470 const VkImagePlaneMemoryRequirementsInfo
*plane_reqs
=
3471 (const VkImagePlaneMemoryRequirementsInfo
*) ext
;
3472 uint32_t plane
= anv_image_aspect_to_plane(image
->aspects
,
3473 plane_reqs
->planeAspect
);
3475 assert(image
->planes
[plane
].offset
== 0);
3477 /* The Vulkan spec (git aaed022) says:
3479 * memoryTypeBits is a bitfield and contains one bit set for every
3480 * supported memory type for the resource. The bit `1<<i` is set
3481 * if and only if the memory type `i` in the
3482 * VkPhysicalDeviceMemoryProperties structure for the physical
3483 * device is supported.
3485 * All types are currently supported for images.
3487 pMemoryRequirements
->memoryRequirements
.memoryTypeBits
=
3488 (1ull << pdevice
->memory
.type_count
) - 1;
3490 /* We must have image allocated or imported at this point. According to the
3491 * specification, external images must have been bound to memory before
3492 * calling GetImageMemoryRequirements.
3494 assert(image
->planes
[plane
].size
> 0);
3496 pMemoryRequirements
->memoryRequirements
.size
= image
->planes
[plane
].size
;
3497 pMemoryRequirements
->memoryRequirements
.alignment
=
3498 image
->planes
[plane
].alignment
;
3503 anv_debug_ignored_stype(ext
->sType
);
3508 vk_foreach_struct(ext
, pMemoryRequirements
->pNext
) {
3509 switch (ext
->sType
) {
3510 case VK_STRUCTURE_TYPE_MEMORY_DEDICATED_REQUIREMENTS
: {
3511 VkMemoryDedicatedRequirements
*requirements
= (void *)ext
;
3512 if (image
->needs_set_tiling
|| image
->external_format
) {
3513 /* If we need to set the tiling for external consumers, we need a
3514 * dedicated allocation.
3516 * See also anv_AllocateMemory.
3518 requirements
->prefersDedicatedAllocation
= true;
3519 requirements
->requiresDedicatedAllocation
= true;
3521 requirements
->prefersDedicatedAllocation
= false;
3522 requirements
->requiresDedicatedAllocation
= false;
3528 anv_debug_ignored_stype(ext
->sType
);
3534 void anv_GetImageSparseMemoryRequirements(
3537 uint32_t* pSparseMemoryRequirementCount
,
3538 VkSparseImageMemoryRequirements
* pSparseMemoryRequirements
)
3540 *pSparseMemoryRequirementCount
= 0;
3543 void anv_GetImageSparseMemoryRequirements2(
3545 const VkImageSparseMemoryRequirementsInfo2
* pInfo
,
3546 uint32_t* pSparseMemoryRequirementCount
,
3547 VkSparseImageMemoryRequirements2
* pSparseMemoryRequirements
)
3549 *pSparseMemoryRequirementCount
= 0;
3552 void anv_GetDeviceMemoryCommitment(
3554 VkDeviceMemory memory
,
3555 VkDeviceSize
* pCommittedMemoryInBytes
)
3557 *pCommittedMemoryInBytes
= 0;
3561 anv_bind_buffer_memory(const VkBindBufferMemoryInfo
*pBindInfo
)
3563 ANV_FROM_HANDLE(anv_device_memory
, mem
, pBindInfo
->memory
);
3564 ANV_FROM_HANDLE(anv_buffer
, buffer
, pBindInfo
->buffer
);
3566 assert(pBindInfo
->sType
== VK_STRUCTURE_TYPE_BIND_BUFFER_MEMORY_INFO
);
3569 assert((buffer
->usage
& mem
->type
->valid_buffer_usage
) == buffer
->usage
);
3570 buffer
->address
= (struct anv_address
) {
3572 .offset
= pBindInfo
->memoryOffset
,
3575 buffer
->address
= ANV_NULL_ADDRESS
;
3579 VkResult
anv_BindBufferMemory(
3582 VkDeviceMemory memory
,
3583 VkDeviceSize memoryOffset
)
3585 anv_bind_buffer_memory(
3586 &(VkBindBufferMemoryInfo
) {
3587 .sType
= VK_STRUCTURE_TYPE_BIND_BUFFER_MEMORY_INFO
,
3590 .memoryOffset
= memoryOffset
,
3596 VkResult
anv_BindBufferMemory2(
3598 uint32_t bindInfoCount
,
3599 const VkBindBufferMemoryInfo
* pBindInfos
)
3601 for (uint32_t i
= 0; i
< bindInfoCount
; i
++)
3602 anv_bind_buffer_memory(&pBindInfos
[i
]);
3607 VkResult
anv_QueueBindSparse(
3609 uint32_t bindInfoCount
,
3610 const VkBindSparseInfo
* pBindInfo
,
3613 ANV_FROM_HANDLE(anv_queue
, queue
, _queue
);
3614 if (anv_device_is_lost(queue
->device
))
3615 return VK_ERROR_DEVICE_LOST
;
3617 return vk_error(VK_ERROR_FEATURE_NOT_PRESENT
);
3622 VkResult
anv_CreateEvent(
3624 const VkEventCreateInfo
* pCreateInfo
,
3625 const VkAllocationCallbacks
* pAllocator
,
3628 ANV_FROM_HANDLE(anv_device
, device
, _device
);
3629 struct anv_state state
;
3630 struct anv_event
*event
;
3632 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_EVENT_CREATE_INFO
);
3634 state
= anv_state_pool_alloc(&device
->dynamic_state_pool
,
3637 event
->state
= state
;
3638 event
->semaphore
= VK_EVENT_RESET
;
3640 if (!device
->info
.has_llc
) {
3641 /* Make sure the writes we're flushing have landed. */
3642 __builtin_ia32_mfence();
3643 __builtin_ia32_clflush(event
);
3646 *pEvent
= anv_event_to_handle(event
);
3651 void anv_DestroyEvent(
3654 const VkAllocationCallbacks
* pAllocator
)
3656 ANV_FROM_HANDLE(anv_device
, device
, _device
);
3657 ANV_FROM_HANDLE(anv_event
, event
, _event
);
3662 anv_state_pool_free(&device
->dynamic_state_pool
, event
->state
);
3665 VkResult
anv_GetEventStatus(
3669 ANV_FROM_HANDLE(anv_device
, device
, _device
);
3670 ANV_FROM_HANDLE(anv_event
, event
, _event
);
3672 if (anv_device_is_lost(device
))
3673 return VK_ERROR_DEVICE_LOST
;
3675 if (!device
->info
.has_llc
) {
3676 /* Invalidate read cache before reading event written by GPU. */
3677 __builtin_ia32_clflush(event
);
3678 __builtin_ia32_mfence();
3682 return event
->semaphore
;
3685 VkResult
anv_SetEvent(
3689 ANV_FROM_HANDLE(anv_device
, device
, _device
);
3690 ANV_FROM_HANDLE(anv_event
, event
, _event
);
3692 event
->semaphore
= VK_EVENT_SET
;
3694 if (!device
->info
.has_llc
) {
3695 /* Make sure the writes we're flushing have landed. */
3696 __builtin_ia32_mfence();
3697 __builtin_ia32_clflush(event
);
3703 VkResult
anv_ResetEvent(
3707 ANV_FROM_HANDLE(anv_device
, device
, _device
);
3708 ANV_FROM_HANDLE(anv_event
, event
, _event
);
3710 event
->semaphore
= VK_EVENT_RESET
;
3712 if (!device
->info
.has_llc
) {
3713 /* Make sure the writes we're flushing have landed. */
3714 __builtin_ia32_mfence();
3715 __builtin_ia32_clflush(event
);
3723 VkResult
anv_CreateBuffer(
3725 const VkBufferCreateInfo
* pCreateInfo
,
3726 const VkAllocationCallbacks
* pAllocator
,
3729 ANV_FROM_HANDLE(anv_device
, device
, _device
);
3730 struct anv_buffer
*buffer
;
3732 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO
);
3734 buffer
= vk_alloc2(&device
->alloc
, pAllocator
, sizeof(*buffer
), 8,
3735 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
3737 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
3739 buffer
->size
= pCreateInfo
->size
;
3740 buffer
->usage
= pCreateInfo
->usage
;
3741 buffer
->address
= ANV_NULL_ADDRESS
;
3743 *pBuffer
= anv_buffer_to_handle(buffer
);
3748 void anv_DestroyBuffer(
3751 const VkAllocationCallbacks
* pAllocator
)
3753 ANV_FROM_HANDLE(anv_device
, device
, _device
);
3754 ANV_FROM_HANDLE(anv_buffer
, buffer
, _buffer
);
3759 vk_free2(&device
->alloc
, pAllocator
, buffer
);
3762 VkDeviceAddress
anv_GetBufferDeviceAddressEXT(
3764 const VkBufferDeviceAddressInfoEXT
* pInfo
)
3766 ANV_FROM_HANDLE(anv_buffer
, buffer
, pInfo
->buffer
);
3768 assert(buffer
->address
.bo
->flags
& EXEC_OBJECT_PINNED
);
3770 return anv_address_physical(buffer
->address
);
3774 anv_fill_buffer_surface_state(struct anv_device
*device
, struct anv_state state
,
3775 enum isl_format format
,
3776 struct anv_address address
,
3777 uint32_t range
, uint32_t stride
)
3779 isl_buffer_fill_state(&device
->isl_dev
, state
.map
,
3780 .address
= anv_address_physical(address
),
3781 .mocs
= device
->default_mocs
,
3784 .swizzle
= ISL_SWIZZLE_IDENTITY
,
3785 .stride_B
= stride
);
3788 void anv_DestroySampler(
3791 const VkAllocationCallbacks
* pAllocator
)
3793 ANV_FROM_HANDLE(anv_device
, device
, _device
);
3794 ANV_FROM_HANDLE(anv_sampler
, sampler
, _sampler
);
3799 if (sampler
->bindless_state
.map
) {
3800 anv_state_pool_free(&device
->dynamic_state_pool
,
3801 sampler
->bindless_state
);
3804 vk_free2(&device
->alloc
, pAllocator
, sampler
);
3807 VkResult
anv_CreateFramebuffer(
3809 const VkFramebufferCreateInfo
* pCreateInfo
,
3810 const VkAllocationCallbacks
* pAllocator
,
3811 VkFramebuffer
* pFramebuffer
)
3813 ANV_FROM_HANDLE(anv_device
, device
, _device
);
3814 struct anv_framebuffer
*framebuffer
;
3816 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO
);
3818 size_t size
= sizeof(*framebuffer
);
3820 /* VK_KHR_imageless_framebuffer extension says:
3822 * If flags includes VK_FRAMEBUFFER_CREATE_IMAGELESS_BIT_KHR,
3823 * parameter pAttachments is ignored.
3825 if (!(pCreateInfo
->flags
& VK_FRAMEBUFFER_CREATE_IMAGELESS_BIT_KHR
)) {
3826 size
+= sizeof(struct anv_image_view
*) * pCreateInfo
->attachmentCount
;
3827 framebuffer
= vk_alloc2(&device
->alloc
, pAllocator
, size
, 8,
3828 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
3829 if (framebuffer
== NULL
)
3830 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
3832 for (uint32_t i
= 0; i
< pCreateInfo
->attachmentCount
; i
++) {
3833 ANV_FROM_HANDLE(anv_image_view
, iview
, pCreateInfo
->pAttachments
[i
]);
3834 framebuffer
->attachments
[i
] = iview
;
3836 framebuffer
->attachment_count
= pCreateInfo
->attachmentCount
;
3838 assert(device
->enabled_extensions
.KHR_imageless_framebuffer
);
3839 framebuffer
= vk_alloc2(&device
->alloc
, pAllocator
, size
, 8,
3840 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
3841 if (framebuffer
== NULL
)
3842 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
3844 framebuffer
->attachment_count
= 0;
3847 framebuffer
->width
= pCreateInfo
->width
;
3848 framebuffer
->height
= pCreateInfo
->height
;
3849 framebuffer
->layers
= pCreateInfo
->layers
;
3851 *pFramebuffer
= anv_framebuffer_to_handle(framebuffer
);
3856 void anv_DestroyFramebuffer(
3859 const VkAllocationCallbacks
* pAllocator
)
3861 ANV_FROM_HANDLE(anv_device
, device
, _device
);
3862 ANV_FROM_HANDLE(anv_framebuffer
, fb
, _fb
);
3867 vk_free2(&device
->alloc
, pAllocator
, fb
);
3870 static const VkTimeDomainEXT anv_time_domains
[] = {
3871 VK_TIME_DOMAIN_DEVICE_EXT
,
3872 VK_TIME_DOMAIN_CLOCK_MONOTONIC_EXT
,
3873 VK_TIME_DOMAIN_CLOCK_MONOTONIC_RAW_EXT
,
3876 VkResult
anv_GetPhysicalDeviceCalibrateableTimeDomainsEXT(
3877 VkPhysicalDevice physicalDevice
,
3878 uint32_t *pTimeDomainCount
,
3879 VkTimeDomainEXT
*pTimeDomains
)
3882 VK_OUTARRAY_MAKE(out
, pTimeDomains
, pTimeDomainCount
);
3884 for (d
= 0; d
< ARRAY_SIZE(anv_time_domains
); d
++) {
3885 vk_outarray_append(&out
, i
) {
3886 *i
= anv_time_domains
[d
];
3890 return vk_outarray_status(&out
);
3894 anv_clock_gettime(clockid_t clock_id
)
3896 struct timespec current
;
3899 ret
= clock_gettime(clock_id
, ¤t
);
3900 if (ret
< 0 && clock_id
== CLOCK_MONOTONIC_RAW
)
3901 ret
= clock_gettime(CLOCK_MONOTONIC
, ¤t
);
3905 return (uint64_t) current
.tv_sec
* 1000000000ULL + current
.tv_nsec
;
3908 #define TIMESTAMP 0x2358
3910 VkResult
anv_GetCalibratedTimestampsEXT(
3912 uint32_t timestampCount
,
3913 const VkCalibratedTimestampInfoEXT
*pTimestampInfos
,
3914 uint64_t *pTimestamps
,
3915 uint64_t *pMaxDeviation
)
3917 ANV_FROM_HANDLE(anv_device
, device
, _device
);
3918 uint64_t timestamp_frequency
= device
->info
.timestamp_frequency
;
3921 uint64_t begin
, end
;
3922 uint64_t max_clock_period
= 0;
3924 begin
= anv_clock_gettime(CLOCK_MONOTONIC_RAW
);
3926 for (d
= 0; d
< timestampCount
; d
++) {
3927 switch (pTimestampInfos
[d
].timeDomain
) {
3928 case VK_TIME_DOMAIN_DEVICE_EXT
:
3929 ret
= anv_gem_reg_read(device
, TIMESTAMP
| 1,
3933 return anv_device_set_lost(device
, "Failed to read the TIMESTAMP "
3936 uint64_t device_period
= DIV_ROUND_UP(1000000000, timestamp_frequency
);
3937 max_clock_period
= MAX2(max_clock_period
, device_period
);
3939 case VK_TIME_DOMAIN_CLOCK_MONOTONIC_EXT
:
3940 pTimestamps
[d
] = anv_clock_gettime(CLOCK_MONOTONIC
);
3941 max_clock_period
= MAX2(max_clock_period
, 1);
3944 case VK_TIME_DOMAIN_CLOCK_MONOTONIC_RAW_EXT
:
3945 pTimestamps
[d
] = begin
;
3953 end
= anv_clock_gettime(CLOCK_MONOTONIC_RAW
);
3956 * The maximum deviation is the sum of the interval over which we
3957 * perform the sampling and the maximum period of any sampled
3958 * clock. That's because the maximum skew between any two sampled
3959 * clock edges is when the sampled clock with the largest period is
3960 * sampled at the end of that period but right at the beginning of the
3961 * sampling interval and some other clock is sampled right at the
3962 * begining of its sampling period and right at the end of the
3963 * sampling interval. Let's assume the GPU has the longest clock
3964 * period and that the application is sampling GPU and monotonic:
3967 * w x y z 0 1 2 3 4 5 6 7 8 9 a b c d e f
3968 * Raw -_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-
3972 * GPU -----_____-----_____-----_____-----_____
3975 * x y z 0 1 2 3 4 5 6 7 8 9 a b c
3976 * Monotonic -_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-
3978 * Interval <----------------->
3979 * Deviation <-------------------------->
3983 * m = read(monotonic) 2
3986 * We round the sample interval up by one tick to cover sampling error
3987 * in the interval clock
3990 uint64_t sample_interval
= end
- begin
+ 1;
3992 *pMaxDeviation
= sample_interval
+ max_clock_period
;
3997 /* vk_icd.h does not declare this function, so we declare it here to
3998 * suppress Wmissing-prototypes.
4000 PUBLIC VKAPI_ATTR VkResult VKAPI_CALL
4001 vk_icdNegotiateLoaderICDInterfaceVersion(uint32_t* pSupportedVersion
);
4003 PUBLIC VKAPI_ATTR VkResult VKAPI_CALL
4004 vk_icdNegotiateLoaderICDInterfaceVersion(uint32_t* pSupportedVersion
)
4006 /* For the full details on loader interface versioning, see
4007 * <https://github.com/KhronosGroup/Vulkan-LoaderAndValidationLayers/blob/master/loader/LoaderAndLayerInterface.md>.
4008 * What follows is a condensed summary, to help you navigate the large and
4009 * confusing official doc.
4011 * - Loader interface v0 is incompatible with later versions. We don't
4014 * - In loader interface v1:
4015 * - The first ICD entrypoint called by the loader is
4016 * vk_icdGetInstanceProcAddr(). The ICD must statically expose this
4018 * - The ICD must statically expose no other Vulkan symbol unless it is
4019 * linked with -Bsymbolic.
4020 * - Each dispatchable Vulkan handle created by the ICD must be
4021 * a pointer to a struct whose first member is VK_LOADER_DATA. The
4022 * ICD must initialize VK_LOADER_DATA.loadMagic to ICD_LOADER_MAGIC.
4023 * - The loader implements vkCreate{PLATFORM}SurfaceKHR() and
4024 * vkDestroySurfaceKHR(). The ICD must be capable of working with
4025 * such loader-managed surfaces.
4027 * - Loader interface v2 differs from v1 in:
4028 * - The first ICD entrypoint called by the loader is
4029 * vk_icdNegotiateLoaderICDInterfaceVersion(). The ICD must
4030 * statically expose this entrypoint.
4032 * - Loader interface v3 differs from v2 in:
4033 * - The ICD must implement vkCreate{PLATFORM}SurfaceKHR(),
4034 * vkDestroySurfaceKHR(), and other API which uses VKSurfaceKHR,
4035 * because the loader no longer does so.
4037 *pSupportedVersion
= MIN2(*pSupportedVersion
, 3u);