2 * Copyright © 2015 Intel Corporation
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
28 #include <sys/sysinfo.h>
32 #include "drm-uapi/drm_fourcc.h"
34 #include "anv_private.h"
35 #include "util/strtod.h"
36 #include "util/debug.h"
37 #include "util/build_id.h"
38 #include "util/disk_cache.h"
39 #include "util/mesa-sha1.h"
40 #include "util/u_string.h"
43 #include "common/gen_defines.h"
44 #include "compiler/glsl_types.h"
46 #include "genxml/gen7_pack.h"
48 /* This is probably far to big but it reflects the max size used for messages
49 * in OpenGLs KHR_debug.
51 #define MAX_DEBUG_MESSAGE_LENGTH 4096
54 compiler_debug_log(void *data
, const char *fmt
, ...)
56 char str
[MAX_DEBUG_MESSAGE_LENGTH
];
57 struct anv_device
*device
= (struct anv_device
*)data
;
59 if (list_empty(&device
->instance
->debug_report_callbacks
.callbacks
))
64 (void) vsnprintf(str
, MAX_DEBUG_MESSAGE_LENGTH
, fmt
, args
);
67 vk_debug_report(&device
->instance
->debug_report_callbacks
,
68 VK_DEBUG_REPORT_DEBUG_BIT_EXT
,
69 VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT
,
74 compiler_perf_log(void *data
, const char *fmt
, ...)
79 if (unlikely(INTEL_DEBUG
& DEBUG_PERF
))
80 intel_logd_v(fmt
, args
);
86 anv_compute_heap_size(int fd
, uint64_t gtt_size
)
88 /* Query the total ram from the system */
92 uint64_t total_ram
= (uint64_t)info
.totalram
* (uint64_t)info
.mem_unit
;
94 /* We don't want to burn too much ram with the GPU. If the user has 4GiB
95 * or less, we use at most half. If they have more than 4GiB, we use 3/4.
97 uint64_t available_ram
;
98 if (total_ram
<= 4ull * 1024ull * 1024ull * 1024ull)
99 available_ram
= total_ram
/ 2;
101 available_ram
= total_ram
* 3 / 4;
103 /* We also want to leave some padding for things we allocate in the driver,
104 * so don't go over 3/4 of the GTT either.
106 uint64_t available_gtt
= gtt_size
* 3 / 4;
108 return MIN2(available_ram
, available_gtt
);
112 anv_physical_device_init_heaps(struct anv_physical_device
*device
, int fd
)
115 if (anv_gem_get_context_param(fd
, 0, I915_CONTEXT_PARAM_GTT_SIZE
,
117 /* If, for whatever reason, we can't actually get the GTT size from the
118 * kernel (too old?) fall back to the aperture size.
120 anv_perf_warn(NULL
, NULL
,
121 "Failed to get I915_CONTEXT_PARAM_GTT_SIZE: %m");
123 if (anv_gem_get_aperture(fd
, >t_size
) == -1) {
124 return vk_errorf(NULL
, NULL
, VK_ERROR_INITIALIZATION_FAILED
,
125 "failed to get aperture size: %m");
129 device
->supports_48bit_addresses
= (device
->info
.gen
>= 8) &&
130 gtt_size
> (4ULL << 30 /* GiB */);
132 uint64_t heap_size
= anv_compute_heap_size(fd
, gtt_size
);
134 if (heap_size
> (2ull << 30) && !device
->supports_48bit_addresses
) {
135 /* When running with an overridden PCI ID, we may get a GTT size from
136 * the kernel that is greater than 2 GiB but the execbuf check for 48bit
137 * address support can still fail. Just clamp the address space size to
138 * 2 GiB if we don't have 48-bit support.
140 intel_logw("%s:%d: The kernel reported a GTT size larger than 2 GiB but "
141 "not support for 48-bit addresses",
143 heap_size
= 2ull << 30;
146 if (heap_size
<= 3ull * (1ull << 30)) {
147 /* In this case, everything fits nicely into the 32-bit address space,
148 * so there's no need for supporting 48bit addresses on client-allocated
151 device
->memory
.heap_count
= 1;
152 device
->memory
.heaps
[0] = (struct anv_memory_heap
) {
153 .vma_start
= LOW_HEAP_MIN_ADDRESS
,
154 .vma_size
= LOW_HEAP_SIZE
,
156 .flags
= VK_MEMORY_HEAP_DEVICE_LOCAL_BIT
,
157 .supports_48bit_addresses
= false,
160 /* Not everything will fit nicely into a 32-bit address space. In this
161 * case we need a 64-bit heap. Advertise a small 32-bit heap and a
162 * larger 48-bit heap. If we're in this case, then we have a total heap
163 * size larger than 3GiB which most likely means they have 8 GiB of
164 * video memory and so carving off 1 GiB for the 32-bit heap should be
167 const uint64_t heap_size_32bit
= 1ull << 30;
168 const uint64_t heap_size_48bit
= heap_size
- heap_size_32bit
;
170 assert(device
->supports_48bit_addresses
);
172 device
->memory
.heap_count
= 2;
173 device
->memory
.heaps
[0] = (struct anv_memory_heap
) {
174 .vma_start
= HIGH_HEAP_MIN_ADDRESS
,
175 /* Leave the last 4GiB out of the high vma range, so that no state
176 * base address + size can overflow 48 bits. For more information see
177 * the comment about Wa32bitGeneralStateOffset in anv_allocator.c
179 .vma_size
= gtt_size
- (1ull << 32) - HIGH_HEAP_MIN_ADDRESS
,
180 .size
= heap_size_48bit
,
181 .flags
= VK_MEMORY_HEAP_DEVICE_LOCAL_BIT
,
182 .supports_48bit_addresses
= true,
184 device
->memory
.heaps
[1] = (struct anv_memory_heap
) {
185 .vma_start
= LOW_HEAP_MIN_ADDRESS
,
186 .vma_size
= LOW_HEAP_SIZE
,
187 .size
= heap_size_32bit
,
188 .flags
= VK_MEMORY_HEAP_DEVICE_LOCAL_BIT
,
189 .supports_48bit_addresses
= false,
193 uint32_t type_count
= 0;
194 for (uint32_t heap
= 0; heap
< device
->memory
.heap_count
; heap
++) {
195 uint32_t valid_buffer_usage
= ~0;
197 /* There appears to be a hardware issue in the VF cache where it only
198 * considers the bottom 32 bits of memory addresses. If you happen to
199 * have two vertex buffers which get placed exactly 4 GiB apart and use
200 * them in back-to-back draw calls, you can get collisions. In order to
201 * solve this problem, we require vertex and index buffers be bound to
202 * memory allocated out of the 32-bit heap.
204 if (device
->memory
.heaps
[heap
].supports_48bit_addresses
) {
205 valid_buffer_usage
&= ~(VK_BUFFER_USAGE_INDEX_BUFFER_BIT
|
206 VK_BUFFER_USAGE_VERTEX_BUFFER_BIT
);
209 if (device
->info
.has_llc
) {
210 /* Big core GPUs share LLC with the CPU and thus one memory type can be
211 * both cached and coherent at the same time.
213 device
->memory
.types
[type_count
++] = (struct anv_memory_type
) {
214 .propertyFlags
= VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT
|
215 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT
|
216 VK_MEMORY_PROPERTY_HOST_COHERENT_BIT
|
217 VK_MEMORY_PROPERTY_HOST_CACHED_BIT
,
219 .valid_buffer_usage
= valid_buffer_usage
,
222 /* The spec requires that we expose a host-visible, coherent memory
223 * type, but Atom GPUs don't share LLC. Thus we offer two memory types
224 * to give the application a choice between cached, but not coherent and
225 * coherent but uncached (WC though).
227 device
->memory
.types
[type_count
++] = (struct anv_memory_type
) {
228 .propertyFlags
= VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT
|
229 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT
|
230 VK_MEMORY_PROPERTY_HOST_COHERENT_BIT
,
232 .valid_buffer_usage
= valid_buffer_usage
,
234 device
->memory
.types
[type_count
++] = (struct anv_memory_type
) {
235 .propertyFlags
= VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT
|
236 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT
|
237 VK_MEMORY_PROPERTY_HOST_CACHED_BIT
,
239 .valid_buffer_usage
= valid_buffer_usage
,
243 device
->memory
.type_count
= type_count
;
249 anv_physical_device_init_uuids(struct anv_physical_device
*device
)
251 const struct build_id_note
*note
=
252 build_id_find_nhdr_for_addr(anv_physical_device_init_uuids
);
254 return vk_errorf(device
->instance
, device
,
255 VK_ERROR_INITIALIZATION_FAILED
,
256 "Failed to find build-id");
259 unsigned build_id_len
= build_id_length(note
);
260 if (build_id_len
< 20) {
261 return vk_errorf(device
->instance
, device
,
262 VK_ERROR_INITIALIZATION_FAILED
,
263 "build-id too short. It needs to be a SHA");
266 memcpy(device
->driver_build_sha1
, build_id_data(note
), 20);
268 struct mesa_sha1 sha1_ctx
;
270 STATIC_ASSERT(VK_UUID_SIZE
<= sizeof(sha1
));
272 /* The pipeline cache UUID is used for determining when a pipeline cache is
273 * invalid. It needs both a driver build and the PCI ID of the device.
275 _mesa_sha1_init(&sha1_ctx
);
276 _mesa_sha1_update(&sha1_ctx
, build_id_data(note
), build_id_len
);
277 _mesa_sha1_update(&sha1_ctx
, &device
->chipset_id
,
278 sizeof(device
->chipset_id
));
279 _mesa_sha1_update(&sha1_ctx
, &device
->always_use_bindless
,
280 sizeof(device
->always_use_bindless
));
281 _mesa_sha1_update(&sha1_ctx
, &device
->has_a64_buffer_access
,
282 sizeof(device
->has_a64_buffer_access
));
283 _mesa_sha1_update(&sha1_ctx
, &device
->has_bindless_images
,
284 sizeof(device
->has_bindless_images
));
285 _mesa_sha1_update(&sha1_ctx
, &device
->has_bindless_samplers
,
286 sizeof(device
->has_bindless_samplers
));
287 _mesa_sha1_final(&sha1_ctx
, sha1
);
288 memcpy(device
->pipeline_cache_uuid
, sha1
, VK_UUID_SIZE
);
290 /* The driver UUID is used for determining sharability of images and memory
291 * between two Vulkan instances in separate processes. People who want to
292 * share memory need to also check the device UUID (below) so all this
293 * needs to be is the build-id.
295 memcpy(device
->driver_uuid
, build_id_data(note
), VK_UUID_SIZE
);
297 /* The device UUID uniquely identifies the given device within the machine.
298 * Since we never have more than one device, this doesn't need to be a real
299 * UUID. However, on the off-chance that someone tries to use this to
300 * cache pre-tiled images or something of the like, we use the PCI ID and
301 * some bits of ISL info to ensure that this is safe.
303 _mesa_sha1_init(&sha1_ctx
);
304 _mesa_sha1_update(&sha1_ctx
, &device
->chipset_id
,
305 sizeof(device
->chipset_id
));
306 _mesa_sha1_update(&sha1_ctx
, &device
->isl_dev
.has_bit6_swizzling
,
307 sizeof(device
->isl_dev
.has_bit6_swizzling
));
308 _mesa_sha1_final(&sha1_ctx
, sha1
);
309 memcpy(device
->device_uuid
, sha1
, VK_UUID_SIZE
);
315 anv_physical_device_init_disk_cache(struct anv_physical_device
*device
)
317 #ifdef ENABLE_SHADER_CACHE
319 MAYBE_UNUSED
int len
= snprintf(renderer
, sizeof(renderer
), "anv_%04x",
321 assert(len
== sizeof(renderer
) - 2);
324 _mesa_sha1_format(timestamp
, device
->driver_build_sha1
);
326 const uint64_t driver_flags
=
327 brw_get_compiler_config_value(device
->compiler
);
328 device
->disk_cache
= disk_cache_create(renderer
, timestamp
, driver_flags
);
330 device
->disk_cache
= NULL
;
335 anv_physical_device_free_disk_cache(struct anv_physical_device
*device
)
337 #ifdef ENABLE_SHADER_CACHE
338 if (device
->disk_cache
)
339 disk_cache_destroy(device
->disk_cache
);
341 assert(device
->disk_cache
== NULL
);
346 anv_physical_device_init(struct anv_physical_device
*device
,
347 struct anv_instance
*instance
,
348 drmDevicePtr drm_device
)
350 const char *primary_path
= drm_device
->nodes
[DRM_NODE_PRIMARY
];
351 const char *path
= drm_device
->nodes
[DRM_NODE_RENDER
];
356 brw_process_intel_debug_variable();
358 fd
= open(path
, O_RDWR
| O_CLOEXEC
);
360 return vk_error(VK_ERROR_INCOMPATIBLE_DRIVER
);
362 device
->_loader_data
.loaderMagic
= ICD_LOADER_MAGIC
;
363 device
->instance
= instance
;
365 assert(strlen(path
) < ARRAY_SIZE(device
->path
));
366 snprintf(device
->path
, ARRAY_SIZE(device
->path
), "%s", path
);
368 device
->no_hw
= getenv("INTEL_NO_HW") != NULL
;
370 const int pci_id_override
= gen_get_pci_device_id_override();
371 if (pci_id_override
< 0) {
372 device
->chipset_id
= anv_gem_get_param(fd
, I915_PARAM_CHIPSET_ID
);
373 if (!device
->chipset_id
) {
374 result
= vk_error(VK_ERROR_INCOMPATIBLE_DRIVER
);
378 device
->chipset_id
= pci_id_override
;
379 device
->no_hw
= true;
382 device
->pci_info
.domain
= drm_device
->businfo
.pci
->domain
;
383 device
->pci_info
.bus
= drm_device
->businfo
.pci
->bus
;
384 device
->pci_info
.device
= drm_device
->businfo
.pci
->dev
;
385 device
->pci_info
.function
= drm_device
->businfo
.pci
->func
;
387 device
->name
= gen_get_device_name(device
->chipset_id
);
388 if (!gen_get_device_info(device
->chipset_id
, &device
->info
)) {
389 result
= vk_error(VK_ERROR_INCOMPATIBLE_DRIVER
);
393 if (device
->info
.is_haswell
) {
394 intel_logw("Haswell Vulkan support is incomplete");
395 } else if (device
->info
.gen
== 7 && !device
->info
.is_baytrail
) {
396 intel_logw("Ivy Bridge Vulkan support is incomplete");
397 } else if (device
->info
.gen
== 7 && device
->info
.is_baytrail
) {
398 intel_logw("Bay Trail Vulkan support is incomplete");
399 } else if (device
->info
.gen
>= 8 && device
->info
.gen
<= 10) {
400 /* Gen8-10 fully supported */
401 } else if (device
->info
.gen
== 11) {
402 intel_logw("Vulkan is not yet fully supported on gen11.");
404 result
= vk_errorf(device
->instance
, device
,
405 VK_ERROR_INCOMPATIBLE_DRIVER
,
406 "Vulkan not yet supported on %s", device
->name
);
410 device
->cmd_parser_version
= -1;
411 if (device
->info
.gen
== 7) {
412 device
->cmd_parser_version
=
413 anv_gem_get_param(fd
, I915_PARAM_CMD_PARSER_VERSION
);
414 if (device
->cmd_parser_version
== -1) {
415 result
= vk_errorf(device
->instance
, device
,
416 VK_ERROR_INITIALIZATION_FAILED
,
417 "failed to get command parser version");
422 if (!anv_gem_get_param(fd
, I915_PARAM_HAS_WAIT_TIMEOUT
)) {
423 result
= vk_errorf(device
->instance
, device
,
424 VK_ERROR_INITIALIZATION_FAILED
,
425 "kernel missing gem wait");
429 if (!anv_gem_get_param(fd
, I915_PARAM_HAS_EXECBUF2
)) {
430 result
= vk_errorf(device
->instance
, device
,
431 VK_ERROR_INITIALIZATION_FAILED
,
432 "kernel missing execbuf2");
436 if (!device
->info
.has_llc
&&
437 anv_gem_get_param(fd
, I915_PARAM_MMAP_VERSION
) < 1) {
438 result
= vk_errorf(device
->instance
, device
,
439 VK_ERROR_INITIALIZATION_FAILED
,
440 "kernel missing wc mmap");
444 result
= anv_physical_device_init_heaps(device
, fd
);
445 if (result
!= VK_SUCCESS
)
448 device
->has_exec_async
= anv_gem_get_param(fd
, I915_PARAM_HAS_EXEC_ASYNC
);
449 device
->has_exec_capture
= anv_gem_get_param(fd
, I915_PARAM_HAS_EXEC_CAPTURE
);
450 device
->has_exec_fence
= anv_gem_get_param(fd
, I915_PARAM_HAS_EXEC_FENCE
);
451 device
->has_syncobj
= anv_gem_get_param(fd
, I915_PARAM_HAS_EXEC_FENCE_ARRAY
);
452 device
->has_syncobj_wait
= device
->has_syncobj
&&
453 anv_gem_supports_syncobj_wait(fd
);
454 device
->has_context_priority
= anv_gem_has_context_priority(fd
);
456 device
->use_softpin
= anv_gem_get_param(fd
, I915_PARAM_HAS_EXEC_SOFTPIN
)
457 && device
->supports_48bit_addresses
;
459 device
->has_context_isolation
=
460 anv_gem_get_param(fd
, I915_PARAM_HAS_CONTEXT_ISOLATION
);
462 device
->always_use_bindless
=
463 env_var_as_boolean("ANV_ALWAYS_BINDLESS", false);
465 /* We first got the A64 messages on broadwell and we can only use them if
466 * we can pass addresses directly into the shader which requires softpin.
468 device
->has_a64_buffer_access
= device
->info
.gen
>= 8 &&
471 /* We first get bindless image access on Skylake and we can only really do
472 * it if we don't have any relocations so we need softpin.
474 device
->has_bindless_images
= device
->info
.gen
>= 9 &&
477 /* We've had bindless samplers since Ivy Bridge (forever in Vulkan terms)
478 * because it's just a matter of setting the sampler address in the sample
479 * message header. However, we've not bothered to wire it up for vec4 so
480 * we leave it disabled on gen7.
482 device
->has_bindless_samplers
= device
->info
.gen
>= 8;
484 /* Starting with Gen10, the timestamp frequency of the command streamer may
485 * vary from one part to another. We can query the value from the kernel.
487 if (device
->info
.gen
>= 10) {
488 int timestamp_frequency
=
489 anv_gem_get_param(fd
, I915_PARAM_CS_TIMESTAMP_FREQUENCY
);
491 if (timestamp_frequency
< 0)
492 intel_logw("Kernel 4.16-rc1+ required to properly query CS timestamp frequency");
494 device
->info
.timestamp_frequency
= timestamp_frequency
;
497 /* GENs prior to 8 do not support EU/Subslice info */
498 if (device
->info
.gen
>= 8) {
499 device
->subslice_total
= anv_gem_get_param(fd
, I915_PARAM_SUBSLICE_TOTAL
);
500 device
->eu_total
= anv_gem_get_param(fd
, I915_PARAM_EU_TOTAL
);
502 /* Without this information, we cannot get the right Braswell
503 * brandstrings, and we have to use conservative numbers for GPGPU on
504 * many platforms, but otherwise, things will just work.
506 if (device
->subslice_total
< 1 || device
->eu_total
< 1) {
507 intel_logw("Kernel 4.1 required to properly query GPU properties");
509 } else if (device
->info
.gen
== 7) {
510 device
->subslice_total
= 1 << (device
->info
.gt
- 1);
513 if (device
->info
.is_cherryview
&&
514 device
->subslice_total
> 0 && device
->eu_total
> 0) {
515 /* Logical CS threads = EUs per subslice * num threads per EU */
516 uint32_t max_cs_threads
=
517 device
->eu_total
/ device
->subslice_total
* device
->info
.num_thread_per_eu
;
519 /* Fuse configurations may give more threads than expected, never less. */
520 if (max_cs_threads
> device
->info
.max_cs_threads
)
521 device
->info
.max_cs_threads
= max_cs_threads
;
524 device
->compiler
= brw_compiler_create(NULL
, &device
->info
);
525 if (device
->compiler
== NULL
) {
526 result
= vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
529 device
->compiler
->shader_debug_log
= compiler_debug_log
;
530 device
->compiler
->shader_perf_log
= compiler_perf_log
;
531 device
->compiler
->supports_pull_constants
= false;
532 device
->compiler
->constant_buffer_0_is_relative
=
533 device
->info
.gen
< 8 || !device
->has_context_isolation
;
534 device
->compiler
->supports_shader_constants
= true;
536 /* Broadwell PRM says:
538 * "Before Gen8, there was a historical configuration control field to
539 * swizzle address bit[6] for in X/Y tiling modes. This was set in three
540 * different places: TILECTL[1:0], ARB_MODE[5:4], and
541 * DISP_ARB_CTL[14:13].
543 * For Gen8 and subsequent generations, the swizzle fields are all
544 * reserved, and the CPU's memory controller performs all address
545 * swizzling modifications."
548 device
->info
.gen
< 8 && anv_gem_get_bit6_swizzle(fd
, I915_TILING_X
);
550 isl_device_init(&device
->isl_dev
, &device
->info
, swizzled
);
552 result
= anv_physical_device_init_uuids(device
);
553 if (result
!= VK_SUCCESS
)
556 anv_physical_device_init_disk_cache(device
);
558 if (instance
->enabled_extensions
.KHR_display
) {
559 master_fd
= open(primary_path
, O_RDWR
| O_CLOEXEC
);
560 if (master_fd
>= 0) {
561 /* prod the device with a GETPARAM call which will fail if
562 * we don't have permission to even render on this device
564 if (anv_gem_get_param(master_fd
, I915_PARAM_CHIPSET_ID
) == 0) {
570 device
->master_fd
= master_fd
;
572 result
= anv_init_wsi(device
);
573 if (result
!= VK_SUCCESS
) {
574 ralloc_free(device
->compiler
);
575 anv_physical_device_free_disk_cache(device
);
579 anv_physical_device_get_supported_extensions(device
,
580 &device
->supported_extensions
);
583 device
->local_fd
= fd
;
595 anv_physical_device_finish(struct anv_physical_device
*device
)
597 anv_finish_wsi(device
);
598 anv_physical_device_free_disk_cache(device
);
599 ralloc_free(device
->compiler
);
600 close(device
->local_fd
);
601 if (device
->master_fd
>= 0)
602 close(device
->master_fd
);
606 default_alloc_func(void *pUserData
, size_t size
, size_t align
,
607 VkSystemAllocationScope allocationScope
)
613 default_realloc_func(void *pUserData
, void *pOriginal
, size_t size
,
614 size_t align
, VkSystemAllocationScope allocationScope
)
616 return realloc(pOriginal
, size
);
620 default_free_func(void *pUserData
, void *pMemory
)
625 static const VkAllocationCallbacks default_alloc
= {
627 .pfnAllocation
= default_alloc_func
,
628 .pfnReallocation
= default_realloc_func
,
629 .pfnFree
= default_free_func
,
632 VkResult
anv_EnumerateInstanceExtensionProperties(
633 const char* pLayerName
,
634 uint32_t* pPropertyCount
,
635 VkExtensionProperties
* pProperties
)
637 VK_OUTARRAY_MAKE(out
, pProperties
, pPropertyCount
);
639 for (int i
= 0; i
< ANV_INSTANCE_EXTENSION_COUNT
; i
++) {
640 if (anv_instance_extensions_supported
.extensions
[i
]) {
641 vk_outarray_append(&out
, prop
) {
642 *prop
= anv_instance_extensions
[i
];
647 return vk_outarray_status(&out
);
650 VkResult
anv_CreateInstance(
651 const VkInstanceCreateInfo
* pCreateInfo
,
652 const VkAllocationCallbacks
* pAllocator
,
653 VkInstance
* pInstance
)
655 struct anv_instance
*instance
;
658 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO
);
660 struct anv_instance_extension_table enabled_extensions
= {};
661 for (uint32_t i
= 0; i
< pCreateInfo
->enabledExtensionCount
; i
++) {
663 for (idx
= 0; idx
< ANV_INSTANCE_EXTENSION_COUNT
; idx
++) {
664 if (strcmp(pCreateInfo
->ppEnabledExtensionNames
[i
],
665 anv_instance_extensions
[idx
].extensionName
) == 0)
669 if (idx
>= ANV_INSTANCE_EXTENSION_COUNT
)
670 return vk_error(VK_ERROR_EXTENSION_NOT_PRESENT
);
672 if (!anv_instance_extensions_supported
.extensions
[idx
])
673 return vk_error(VK_ERROR_EXTENSION_NOT_PRESENT
);
675 enabled_extensions
.extensions
[idx
] = true;
678 instance
= vk_alloc2(&default_alloc
, pAllocator
, sizeof(*instance
), 8,
679 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE
);
681 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
683 instance
->_loader_data
.loaderMagic
= ICD_LOADER_MAGIC
;
686 instance
->alloc
= *pAllocator
;
688 instance
->alloc
= default_alloc
;
690 instance
->app_info
= (struct anv_app_info
) { .api_version
= 0 };
691 if (pCreateInfo
->pApplicationInfo
) {
692 const VkApplicationInfo
*app
= pCreateInfo
->pApplicationInfo
;
694 instance
->app_info
.app_name
=
695 vk_strdup(&instance
->alloc
, app
->pApplicationName
,
696 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE
);
697 instance
->app_info
.app_version
= app
->applicationVersion
;
699 instance
->app_info
.engine_name
=
700 vk_strdup(&instance
->alloc
, app
->pEngineName
,
701 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE
);
702 instance
->app_info
.engine_version
= app
->engineVersion
;
704 instance
->app_info
.api_version
= app
->apiVersion
;
707 if (instance
->app_info
.api_version
== 0)
708 instance
->app_info
.api_version
= VK_API_VERSION_1_0
;
710 instance
->enabled_extensions
= enabled_extensions
;
712 for (unsigned i
= 0; i
< ARRAY_SIZE(instance
->dispatch
.entrypoints
); i
++) {
713 /* Vulkan requires that entrypoints for extensions which have not been
714 * enabled must not be advertised.
716 if (!anv_instance_entrypoint_is_enabled(i
, instance
->app_info
.api_version
,
717 &instance
->enabled_extensions
)) {
718 instance
->dispatch
.entrypoints
[i
] = NULL
;
720 instance
->dispatch
.entrypoints
[i
] =
721 anv_instance_dispatch_table
.entrypoints
[i
];
725 for (unsigned i
= 0; i
< ARRAY_SIZE(instance
->device_dispatch
.entrypoints
); i
++) {
726 /* Vulkan requires that entrypoints for extensions which have not been
727 * enabled must not be advertised.
729 if (!anv_device_entrypoint_is_enabled(i
, instance
->app_info
.api_version
,
730 &instance
->enabled_extensions
, NULL
)) {
731 instance
->device_dispatch
.entrypoints
[i
] = NULL
;
733 instance
->device_dispatch
.entrypoints
[i
] =
734 anv_device_dispatch_table
.entrypoints
[i
];
738 instance
->physicalDeviceCount
= -1;
740 result
= vk_debug_report_instance_init(&instance
->debug_report_callbacks
);
741 if (result
!= VK_SUCCESS
) {
742 vk_free2(&default_alloc
, pAllocator
, instance
);
743 return vk_error(result
);
746 instance
->pipeline_cache_enabled
=
747 env_var_as_boolean("ANV_ENABLE_PIPELINE_CACHE", true);
750 glsl_type_singleton_init_or_ref();
752 VG(VALGRIND_CREATE_MEMPOOL(instance
, 0, false));
754 *pInstance
= anv_instance_to_handle(instance
);
759 void anv_DestroyInstance(
760 VkInstance _instance
,
761 const VkAllocationCallbacks
* pAllocator
)
763 ANV_FROM_HANDLE(anv_instance
, instance
, _instance
);
768 if (instance
->physicalDeviceCount
> 0) {
769 /* We support at most one physical device. */
770 assert(instance
->physicalDeviceCount
== 1);
771 anv_physical_device_finish(&instance
->physicalDevice
);
774 vk_free(&instance
->alloc
, (char *)instance
->app_info
.app_name
);
775 vk_free(&instance
->alloc
, (char *)instance
->app_info
.engine_name
);
777 VG(VALGRIND_DESTROY_MEMPOOL(instance
));
779 vk_debug_report_instance_destroy(&instance
->debug_report_callbacks
);
781 glsl_type_singleton_decref();
784 vk_free(&instance
->alloc
, instance
);
788 anv_enumerate_devices(struct anv_instance
*instance
)
790 /* TODO: Check for more devices ? */
791 drmDevicePtr devices
[8];
792 VkResult result
= VK_ERROR_INCOMPATIBLE_DRIVER
;
795 instance
->physicalDeviceCount
= 0;
797 max_devices
= drmGetDevices2(0, devices
, ARRAY_SIZE(devices
));
799 return VK_ERROR_INCOMPATIBLE_DRIVER
;
801 for (unsigned i
= 0; i
< (unsigned)max_devices
; i
++) {
802 if (devices
[i
]->available_nodes
& 1 << DRM_NODE_RENDER
&&
803 devices
[i
]->bustype
== DRM_BUS_PCI
&&
804 devices
[i
]->deviceinfo
.pci
->vendor_id
== 0x8086) {
806 result
= anv_physical_device_init(&instance
->physicalDevice
,
807 instance
, devices
[i
]);
808 if (result
!= VK_ERROR_INCOMPATIBLE_DRIVER
)
812 drmFreeDevices(devices
, max_devices
);
814 if (result
== VK_SUCCESS
)
815 instance
->physicalDeviceCount
= 1;
821 anv_instance_ensure_physical_device(struct anv_instance
*instance
)
823 if (instance
->physicalDeviceCount
< 0) {
824 VkResult result
= anv_enumerate_devices(instance
);
825 if (result
!= VK_SUCCESS
&&
826 result
!= VK_ERROR_INCOMPATIBLE_DRIVER
)
833 VkResult
anv_EnumeratePhysicalDevices(
834 VkInstance _instance
,
835 uint32_t* pPhysicalDeviceCount
,
836 VkPhysicalDevice
* pPhysicalDevices
)
838 ANV_FROM_HANDLE(anv_instance
, instance
, _instance
);
839 VK_OUTARRAY_MAKE(out
, pPhysicalDevices
, pPhysicalDeviceCount
);
841 VkResult result
= anv_instance_ensure_physical_device(instance
);
842 if (result
!= VK_SUCCESS
)
845 if (instance
->physicalDeviceCount
== 0)
848 assert(instance
->physicalDeviceCount
== 1);
849 vk_outarray_append(&out
, i
) {
850 *i
= anv_physical_device_to_handle(&instance
->physicalDevice
);
853 return vk_outarray_status(&out
);
856 VkResult
anv_EnumeratePhysicalDeviceGroups(
857 VkInstance _instance
,
858 uint32_t* pPhysicalDeviceGroupCount
,
859 VkPhysicalDeviceGroupProperties
* pPhysicalDeviceGroupProperties
)
861 ANV_FROM_HANDLE(anv_instance
, instance
, _instance
);
862 VK_OUTARRAY_MAKE(out
, pPhysicalDeviceGroupProperties
,
863 pPhysicalDeviceGroupCount
);
865 VkResult result
= anv_instance_ensure_physical_device(instance
);
866 if (result
!= VK_SUCCESS
)
869 if (instance
->physicalDeviceCount
== 0)
872 assert(instance
->physicalDeviceCount
== 1);
874 vk_outarray_append(&out
, p
) {
875 p
->physicalDeviceCount
= 1;
876 memset(p
->physicalDevices
, 0, sizeof(p
->physicalDevices
));
877 p
->physicalDevices
[0] =
878 anv_physical_device_to_handle(&instance
->physicalDevice
);
879 p
->subsetAllocation
= false;
881 vk_foreach_struct(ext
, p
->pNext
)
882 anv_debug_ignored_stype(ext
->sType
);
885 return vk_outarray_status(&out
);
888 void anv_GetPhysicalDeviceFeatures(
889 VkPhysicalDevice physicalDevice
,
890 VkPhysicalDeviceFeatures
* pFeatures
)
892 ANV_FROM_HANDLE(anv_physical_device
, pdevice
, physicalDevice
);
894 *pFeatures
= (VkPhysicalDeviceFeatures
) {
895 .robustBufferAccess
= true,
896 .fullDrawIndexUint32
= true,
897 .imageCubeArray
= true,
898 .independentBlend
= true,
899 .geometryShader
= true,
900 .tessellationShader
= true,
901 .sampleRateShading
= true,
902 .dualSrcBlend
= true,
904 .multiDrawIndirect
= true,
905 .drawIndirectFirstInstance
= true,
907 .depthBiasClamp
= true,
908 .fillModeNonSolid
= true,
909 .depthBounds
= false,
913 .multiViewport
= true,
914 .samplerAnisotropy
= true,
915 .textureCompressionETC2
= pdevice
->info
.gen
>= 8 ||
916 pdevice
->info
.is_baytrail
,
917 .textureCompressionASTC_LDR
= pdevice
->info
.gen
>= 9, /* FINISHME CHV */
918 .textureCompressionBC
= true,
919 .occlusionQueryPrecise
= true,
920 .pipelineStatisticsQuery
= true,
921 .fragmentStoresAndAtomics
= true,
922 .shaderTessellationAndGeometryPointSize
= true,
923 .shaderImageGatherExtended
= true,
924 .shaderStorageImageExtendedFormats
= true,
925 .shaderStorageImageMultisample
= false,
926 .shaderStorageImageReadWithoutFormat
= false,
927 .shaderStorageImageWriteWithoutFormat
= true,
928 .shaderUniformBufferArrayDynamicIndexing
= true,
929 .shaderSampledImageArrayDynamicIndexing
= true,
930 .shaderStorageBufferArrayDynamicIndexing
= true,
931 .shaderStorageImageArrayDynamicIndexing
= true,
932 .shaderClipDistance
= true,
933 .shaderCullDistance
= true,
934 .shaderFloat64
= pdevice
->info
.gen
>= 8 &&
935 pdevice
->info
.has_64bit_types
,
936 .shaderInt64
= pdevice
->info
.gen
>= 8 &&
937 pdevice
->info
.has_64bit_types
,
938 .shaderInt16
= pdevice
->info
.gen
>= 8,
939 .shaderResourceMinLod
= pdevice
->info
.gen
>= 9,
940 .variableMultisampleRate
= true,
941 .inheritedQueries
= true,
944 /* We can't do image stores in vec4 shaders */
945 pFeatures
->vertexPipelineStoresAndAtomics
=
946 pdevice
->compiler
->scalar_stage
[MESA_SHADER_VERTEX
] &&
947 pdevice
->compiler
->scalar_stage
[MESA_SHADER_GEOMETRY
];
949 struct anv_app_info
*app_info
= &pdevice
->instance
->app_info
;
951 /* The new DOOM and Wolfenstein games require depthBounds without
952 * checking for it. They seem to run fine without it so just claim it's
953 * there and accept the consequences.
955 if (app_info
->engine_name
&& strcmp(app_info
->engine_name
, "idTech") == 0)
956 pFeatures
->depthBounds
= true;
959 void anv_GetPhysicalDeviceFeatures2(
960 VkPhysicalDevice physicalDevice
,
961 VkPhysicalDeviceFeatures2
* pFeatures
)
963 ANV_FROM_HANDLE(anv_physical_device
, pdevice
, physicalDevice
);
964 anv_GetPhysicalDeviceFeatures(physicalDevice
, &pFeatures
->features
);
966 vk_foreach_struct(ext
, pFeatures
->pNext
) {
967 switch (ext
->sType
) {
968 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_8BIT_STORAGE_FEATURES_KHR
: {
969 VkPhysicalDevice8BitStorageFeaturesKHR
*features
=
970 (VkPhysicalDevice8BitStorageFeaturesKHR
*)ext
;
971 features
->storageBuffer8BitAccess
= pdevice
->info
.gen
>= 8;
972 features
->uniformAndStorageBuffer8BitAccess
= pdevice
->info
.gen
>= 8;
973 features
->storagePushConstant8
= pdevice
->info
.gen
>= 8;
977 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_16BIT_STORAGE_FEATURES
: {
978 VkPhysicalDevice16BitStorageFeatures
*features
=
979 (VkPhysicalDevice16BitStorageFeatures
*)ext
;
980 features
->storageBuffer16BitAccess
= pdevice
->info
.gen
>= 8;
981 features
->uniformAndStorageBuffer16BitAccess
= pdevice
->info
.gen
>= 8;
982 features
->storagePushConstant16
= pdevice
->info
.gen
>= 8;
983 features
->storageInputOutput16
= false;
987 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_BUFFER_DEVICE_ADDRESS_FEATURES_EXT
: {
988 VkPhysicalDeviceBufferDeviceAddressFeaturesEXT
*features
= (void *)ext
;
989 features
->bufferDeviceAddress
= pdevice
->has_a64_buffer_access
;
990 features
->bufferDeviceAddressCaptureReplay
= false;
991 features
->bufferDeviceAddressMultiDevice
= false;
995 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_COMPUTE_SHADER_DERIVATIVES_FEATURES_NV
: {
996 VkPhysicalDeviceComputeShaderDerivativesFeaturesNV
*features
=
997 (VkPhysicalDeviceComputeShaderDerivativesFeaturesNV
*)ext
;
998 features
->computeDerivativeGroupQuads
= true;
999 features
->computeDerivativeGroupLinear
= true;
1003 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_CONDITIONAL_RENDERING_FEATURES_EXT
: {
1004 VkPhysicalDeviceConditionalRenderingFeaturesEXT
*features
=
1005 (VkPhysicalDeviceConditionalRenderingFeaturesEXT
*)ext
;
1006 features
->conditionalRendering
= pdevice
->info
.gen
>= 8 ||
1007 pdevice
->info
.is_haswell
;
1008 features
->inheritedConditionalRendering
= pdevice
->info
.gen
>= 8 ||
1009 pdevice
->info
.is_haswell
;
1013 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_DEPTH_CLIP_ENABLE_FEATURES_EXT
: {
1014 VkPhysicalDeviceDepthClipEnableFeaturesEXT
*features
=
1015 (VkPhysicalDeviceDepthClipEnableFeaturesEXT
*)ext
;
1016 features
->depthClipEnable
= true;
1020 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FLOAT16_INT8_FEATURES_KHR
: {
1021 VkPhysicalDeviceFloat16Int8FeaturesKHR
*features
= (void *)ext
;
1022 features
->shaderFloat16
= pdevice
->info
.gen
>= 8;
1023 features
->shaderInt8
= pdevice
->info
.gen
>= 8;
1027 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_HOST_QUERY_RESET_FEATURES_EXT
: {
1028 VkPhysicalDeviceHostQueryResetFeaturesEXT
*features
=
1029 (VkPhysicalDeviceHostQueryResetFeaturesEXT
*)ext
;
1030 features
->hostQueryReset
= true;
1034 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_INLINE_UNIFORM_BLOCK_FEATURES_EXT
: {
1035 VkPhysicalDeviceInlineUniformBlockFeaturesEXT
*features
=
1036 (VkPhysicalDeviceInlineUniformBlockFeaturesEXT
*)ext
;
1037 features
->inlineUniformBlock
= true;
1038 features
->descriptorBindingInlineUniformBlockUpdateAfterBind
= false;
1042 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_MULTIVIEW_FEATURES
: {
1043 VkPhysicalDeviceMultiviewFeatures
*features
=
1044 (VkPhysicalDeviceMultiviewFeatures
*)ext
;
1045 features
->multiview
= true;
1046 features
->multiviewGeometryShader
= true;
1047 features
->multiviewTessellationShader
= true;
1051 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PROTECTED_MEMORY_FEATURES
: {
1052 VkPhysicalDeviceProtectedMemoryFeatures
*features
= (void *)ext
;
1053 features
->protectedMemory
= false;
1057 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SAMPLER_YCBCR_CONVERSION_FEATURES
: {
1058 VkPhysicalDeviceSamplerYcbcrConversionFeatures
*features
=
1059 (VkPhysicalDeviceSamplerYcbcrConversionFeatures
*) ext
;
1060 features
->samplerYcbcrConversion
= true;
1064 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SCALAR_BLOCK_LAYOUT_FEATURES_EXT
: {
1065 VkPhysicalDeviceScalarBlockLayoutFeaturesEXT
*features
=
1066 (VkPhysicalDeviceScalarBlockLayoutFeaturesEXT
*)ext
;
1067 features
->scalarBlockLayout
= true;
1071 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SHADER_ATOMIC_INT64_FEATURES_KHR
: {
1072 VkPhysicalDeviceShaderAtomicInt64FeaturesKHR
*features
= (void *)ext
;
1073 features
->shaderBufferInt64Atomics
=
1074 pdevice
->info
.gen
>= 9 && pdevice
->use_softpin
;
1075 features
->shaderSharedInt64Atomics
= VK_FALSE
;
1079 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SHADER_DRAW_PARAMETERS_FEATURES
: {
1080 VkPhysicalDeviceShaderDrawParametersFeatures
*features
= (void *)ext
;
1081 features
->shaderDrawParameters
= true;
1085 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VARIABLE_POINTERS_FEATURES
: {
1086 VkPhysicalDeviceVariablePointersFeatures
*features
= (void *)ext
;
1087 features
->variablePointersStorageBuffer
= true;
1088 features
->variablePointers
= true;
1092 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_TRANSFORM_FEEDBACK_FEATURES_EXT
: {
1093 VkPhysicalDeviceTransformFeedbackFeaturesEXT
*features
=
1094 (VkPhysicalDeviceTransformFeedbackFeaturesEXT
*)ext
;
1095 features
->transformFeedback
= true;
1096 features
->geometryStreams
= true;
1100 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VERTEX_ATTRIBUTE_DIVISOR_FEATURES_EXT
: {
1101 VkPhysicalDeviceVertexAttributeDivisorFeaturesEXT
*features
=
1102 (VkPhysicalDeviceVertexAttributeDivisorFeaturesEXT
*)ext
;
1103 features
->vertexAttributeInstanceRateDivisor
= true;
1104 features
->vertexAttributeInstanceRateZeroDivisor
= true;
1108 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_YCBCR_IMAGE_ARRAYS_FEATURES_EXT
: {
1109 VkPhysicalDeviceYcbcrImageArraysFeaturesEXT
*features
=
1110 (VkPhysicalDeviceYcbcrImageArraysFeaturesEXT
*)ext
;
1111 features
->ycbcrImageArrays
= true;
1116 anv_debug_ignored_stype(ext
->sType
);
1122 void anv_GetPhysicalDeviceProperties(
1123 VkPhysicalDevice physicalDevice
,
1124 VkPhysicalDeviceProperties
* pProperties
)
1126 ANV_FROM_HANDLE(anv_physical_device
, pdevice
, physicalDevice
);
1127 const struct gen_device_info
*devinfo
= &pdevice
->info
;
1129 /* See assertions made when programming the buffer surface state. */
1130 const uint32_t max_raw_buffer_sz
= devinfo
->gen
>= 7 ?
1131 (1ul << 30) : (1ul << 27);
1133 const uint32_t max_ssbos
= pdevice
->has_a64_buffer_access
? UINT16_MAX
: 64;
1134 const uint32_t max_textures
=
1135 pdevice
->has_bindless_images
? UINT16_MAX
: 128;
1136 const uint32_t max_samplers
=
1137 pdevice
->has_bindless_samplers
? UINT16_MAX
:
1138 (devinfo
->gen
>= 8 || devinfo
->is_haswell
) ? 128 : 16;
1139 const uint32_t max_images
=
1140 pdevice
->has_bindless_images
? UINT16_MAX
: MAX_IMAGES
;
1142 /* The moment we have anything bindless, claim a high per-stage limit */
1143 const uint32_t max_per_stage
=
1144 pdevice
->has_a64_buffer_access
? UINT32_MAX
:
1145 MAX_BINDING_TABLE_SIZE
- MAX_RTS
;
1147 VkSampleCountFlags sample_counts
=
1148 isl_device_get_sample_counts(&pdevice
->isl_dev
);
1151 VkPhysicalDeviceLimits limits
= {
1152 .maxImageDimension1D
= (1 << 14),
1153 .maxImageDimension2D
= (1 << 14),
1154 .maxImageDimension3D
= (1 << 11),
1155 .maxImageDimensionCube
= (1 << 14),
1156 .maxImageArrayLayers
= (1 << 11),
1157 .maxTexelBufferElements
= 128 * 1024 * 1024,
1158 .maxUniformBufferRange
= (1ul << 27),
1159 .maxStorageBufferRange
= max_raw_buffer_sz
,
1160 .maxPushConstantsSize
= MAX_PUSH_CONSTANTS_SIZE
,
1161 .maxMemoryAllocationCount
= UINT32_MAX
,
1162 .maxSamplerAllocationCount
= 64 * 1024,
1163 .bufferImageGranularity
= 64, /* A cache line */
1164 .sparseAddressSpaceSize
= 0,
1165 .maxBoundDescriptorSets
= MAX_SETS
,
1166 .maxPerStageDescriptorSamplers
= max_samplers
,
1167 .maxPerStageDescriptorUniformBuffers
= 64,
1168 .maxPerStageDescriptorStorageBuffers
= max_ssbos
,
1169 .maxPerStageDescriptorSampledImages
= max_textures
,
1170 .maxPerStageDescriptorStorageImages
= max_images
,
1171 .maxPerStageDescriptorInputAttachments
= 64,
1172 .maxPerStageResources
= max_per_stage
,
1173 .maxDescriptorSetSamplers
= 6 * max_samplers
, /* number of stages * maxPerStageDescriptorSamplers */
1174 .maxDescriptorSetUniformBuffers
= 6 * 64, /* number of stages * maxPerStageDescriptorUniformBuffers */
1175 .maxDescriptorSetUniformBuffersDynamic
= MAX_DYNAMIC_BUFFERS
/ 2,
1176 .maxDescriptorSetStorageBuffers
= 6 * max_ssbos
, /* number of stages * maxPerStageDescriptorStorageBuffers */
1177 .maxDescriptorSetStorageBuffersDynamic
= MAX_DYNAMIC_BUFFERS
/ 2,
1178 .maxDescriptorSetSampledImages
= 6 * max_textures
, /* number of stages * maxPerStageDescriptorSampledImages */
1179 .maxDescriptorSetStorageImages
= 6 * max_images
, /* number of stages * maxPerStageDescriptorStorageImages */
1180 .maxDescriptorSetInputAttachments
= 256,
1181 .maxVertexInputAttributes
= MAX_VBS
,
1182 .maxVertexInputBindings
= MAX_VBS
,
1183 .maxVertexInputAttributeOffset
= 2047,
1184 .maxVertexInputBindingStride
= 2048,
1185 .maxVertexOutputComponents
= 128,
1186 .maxTessellationGenerationLevel
= 64,
1187 .maxTessellationPatchSize
= 32,
1188 .maxTessellationControlPerVertexInputComponents
= 128,
1189 .maxTessellationControlPerVertexOutputComponents
= 128,
1190 .maxTessellationControlPerPatchOutputComponents
= 128,
1191 .maxTessellationControlTotalOutputComponents
= 2048,
1192 .maxTessellationEvaluationInputComponents
= 128,
1193 .maxTessellationEvaluationOutputComponents
= 128,
1194 .maxGeometryShaderInvocations
= 32,
1195 .maxGeometryInputComponents
= 64,
1196 .maxGeometryOutputComponents
= 128,
1197 .maxGeometryOutputVertices
= 256,
1198 .maxGeometryTotalOutputComponents
= 1024,
1199 .maxFragmentInputComponents
= 112, /* 128 components - (POS, PSIZ, CLIP_DIST0, CLIP_DIST1) */
1200 .maxFragmentOutputAttachments
= 8,
1201 .maxFragmentDualSrcAttachments
= 1,
1202 .maxFragmentCombinedOutputResources
= 8,
1203 .maxComputeSharedMemorySize
= 32768,
1204 .maxComputeWorkGroupCount
= { 65535, 65535, 65535 },
1205 .maxComputeWorkGroupInvocations
= 32 * devinfo
->max_cs_threads
,
1206 .maxComputeWorkGroupSize
= {
1207 16 * devinfo
->max_cs_threads
,
1208 16 * devinfo
->max_cs_threads
,
1209 16 * devinfo
->max_cs_threads
,
1211 .subPixelPrecisionBits
= 8,
1212 .subTexelPrecisionBits
= 8,
1213 .mipmapPrecisionBits
= 8,
1214 .maxDrawIndexedIndexValue
= UINT32_MAX
,
1215 .maxDrawIndirectCount
= UINT32_MAX
,
1216 .maxSamplerLodBias
= 16,
1217 .maxSamplerAnisotropy
= 16,
1218 .maxViewports
= MAX_VIEWPORTS
,
1219 .maxViewportDimensions
= { (1 << 14), (1 << 14) },
1220 .viewportBoundsRange
= { INT16_MIN
, INT16_MAX
},
1221 .viewportSubPixelBits
= 13, /* We take a float? */
1222 .minMemoryMapAlignment
= 4096, /* A page */
1223 .minTexelBufferOffsetAlignment
= 1,
1224 /* We need 16 for UBO block reads to work and 32 for push UBOs */
1225 .minUniformBufferOffsetAlignment
= 32,
1226 .minStorageBufferOffsetAlignment
= 4,
1227 .minTexelOffset
= -8,
1228 .maxTexelOffset
= 7,
1229 .minTexelGatherOffset
= -32,
1230 .maxTexelGatherOffset
= 31,
1231 .minInterpolationOffset
= -0.5,
1232 .maxInterpolationOffset
= 0.4375,
1233 .subPixelInterpolationOffsetBits
= 4,
1234 .maxFramebufferWidth
= (1 << 14),
1235 .maxFramebufferHeight
= (1 << 14),
1236 .maxFramebufferLayers
= (1 << 11),
1237 .framebufferColorSampleCounts
= sample_counts
,
1238 .framebufferDepthSampleCounts
= sample_counts
,
1239 .framebufferStencilSampleCounts
= sample_counts
,
1240 .framebufferNoAttachmentsSampleCounts
= sample_counts
,
1241 .maxColorAttachments
= MAX_RTS
,
1242 .sampledImageColorSampleCounts
= sample_counts
,
1243 .sampledImageIntegerSampleCounts
= VK_SAMPLE_COUNT_1_BIT
,
1244 .sampledImageDepthSampleCounts
= sample_counts
,
1245 .sampledImageStencilSampleCounts
= sample_counts
,
1246 .storageImageSampleCounts
= VK_SAMPLE_COUNT_1_BIT
,
1247 .maxSampleMaskWords
= 1,
1248 .timestampComputeAndGraphics
= false,
1249 .timestampPeriod
= 1000000000.0 / devinfo
->timestamp_frequency
,
1250 .maxClipDistances
= 8,
1251 .maxCullDistances
= 8,
1252 .maxCombinedClipAndCullDistances
= 8,
1253 .discreteQueuePriorities
= 2,
1254 .pointSizeRange
= { 0.125, 255.875 },
1255 .lineWidthRange
= { 0.0, 7.9921875 },
1256 .pointSizeGranularity
= (1.0 / 8.0),
1257 .lineWidthGranularity
= (1.0 / 128.0),
1258 .strictLines
= false, /* FINISHME */
1259 .standardSampleLocations
= true,
1260 .optimalBufferCopyOffsetAlignment
= 128,
1261 .optimalBufferCopyRowPitchAlignment
= 128,
1262 .nonCoherentAtomSize
= 64,
1265 *pProperties
= (VkPhysicalDeviceProperties
) {
1266 .apiVersion
= anv_physical_device_api_version(pdevice
),
1267 .driverVersion
= vk_get_driver_version(),
1269 .deviceID
= pdevice
->chipset_id
,
1270 .deviceType
= VK_PHYSICAL_DEVICE_TYPE_INTEGRATED_GPU
,
1272 .sparseProperties
= {0}, /* Broadwell doesn't do sparse. */
1275 snprintf(pProperties
->deviceName
, sizeof(pProperties
->deviceName
),
1276 "%s", pdevice
->name
);
1277 memcpy(pProperties
->pipelineCacheUUID
,
1278 pdevice
->pipeline_cache_uuid
, VK_UUID_SIZE
);
1281 void anv_GetPhysicalDeviceProperties2(
1282 VkPhysicalDevice physicalDevice
,
1283 VkPhysicalDeviceProperties2
* pProperties
)
1285 ANV_FROM_HANDLE(anv_physical_device
, pdevice
, physicalDevice
);
1287 anv_GetPhysicalDeviceProperties(physicalDevice
, &pProperties
->properties
);
1289 vk_foreach_struct(ext
, pProperties
->pNext
) {
1290 switch (ext
->sType
) {
1291 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_DEPTH_STENCIL_RESOLVE_PROPERTIES_KHR
: {
1292 VkPhysicalDeviceDepthStencilResolvePropertiesKHR
*props
=
1293 (VkPhysicalDeviceDepthStencilResolvePropertiesKHR
*)ext
;
1295 /* We support all of the depth resolve modes */
1296 props
->supportedDepthResolveModes
=
1297 VK_RESOLVE_MODE_SAMPLE_ZERO_BIT_KHR
|
1298 VK_RESOLVE_MODE_AVERAGE_BIT_KHR
|
1299 VK_RESOLVE_MODE_MIN_BIT_KHR
|
1300 VK_RESOLVE_MODE_MAX_BIT_KHR
;
1302 /* Average doesn't make sense for stencil so we don't support that */
1303 props
->supportedStencilResolveModes
=
1304 VK_RESOLVE_MODE_SAMPLE_ZERO_BIT_KHR
;
1305 if (pdevice
->info
.gen
>= 8) {
1306 /* The advanced stencil resolve modes currently require stencil
1307 * sampling be supported by the hardware.
1309 props
->supportedStencilResolveModes
|=
1310 VK_RESOLVE_MODE_MIN_BIT_KHR
|
1311 VK_RESOLVE_MODE_MAX_BIT_KHR
;
1314 props
->independentResolveNone
= true;
1315 props
->independentResolve
= true;
1319 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_DRIVER_PROPERTIES_KHR
: {
1320 VkPhysicalDeviceDriverPropertiesKHR
*driver_props
=
1321 (VkPhysicalDeviceDriverPropertiesKHR
*) ext
;
1323 driver_props
->driverID
= VK_DRIVER_ID_INTEL_OPEN_SOURCE_MESA_KHR
;
1324 util_snprintf(driver_props
->driverName
, VK_MAX_DRIVER_NAME_SIZE_KHR
,
1325 "Intel open-source Mesa driver");
1327 util_snprintf(driver_props
->driverInfo
, VK_MAX_DRIVER_INFO_SIZE_KHR
,
1328 "Mesa " PACKAGE_VERSION MESA_GIT_SHA1
);
1330 driver_props
->conformanceVersion
= (VkConformanceVersionKHR
) {
1339 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_EXTERNAL_MEMORY_HOST_PROPERTIES_EXT
: {
1340 VkPhysicalDeviceExternalMemoryHostPropertiesEXT
*props
=
1341 (VkPhysicalDeviceExternalMemoryHostPropertiesEXT
*) ext
;
1342 /* Userptr needs page aligned memory. */
1343 props
->minImportedHostPointerAlignment
= 4096;
1347 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_ID_PROPERTIES
: {
1348 VkPhysicalDeviceIDProperties
*id_props
=
1349 (VkPhysicalDeviceIDProperties
*)ext
;
1350 memcpy(id_props
->deviceUUID
, pdevice
->device_uuid
, VK_UUID_SIZE
);
1351 memcpy(id_props
->driverUUID
, pdevice
->driver_uuid
, VK_UUID_SIZE
);
1352 /* The LUID is for Windows. */
1353 id_props
->deviceLUIDValid
= false;
1357 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_INLINE_UNIFORM_BLOCK_PROPERTIES_EXT
: {
1358 VkPhysicalDeviceInlineUniformBlockPropertiesEXT
*props
=
1359 (VkPhysicalDeviceInlineUniformBlockPropertiesEXT
*)ext
;
1360 props
->maxInlineUniformBlockSize
= MAX_INLINE_UNIFORM_BLOCK_SIZE
;
1361 props
->maxPerStageDescriptorInlineUniformBlocks
=
1362 MAX_INLINE_UNIFORM_BLOCK_DESCRIPTORS
;
1363 props
->maxPerStageDescriptorUpdateAfterBindInlineUniformBlocks
=
1364 MAX_INLINE_UNIFORM_BLOCK_DESCRIPTORS
;
1365 props
->maxDescriptorSetInlineUniformBlocks
=
1366 MAX_INLINE_UNIFORM_BLOCK_DESCRIPTORS
;
1367 props
->maxDescriptorSetUpdateAfterBindInlineUniformBlocks
=
1368 MAX_INLINE_UNIFORM_BLOCK_DESCRIPTORS
;
1372 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_MAINTENANCE_3_PROPERTIES
: {
1373 VkPhysicalDeviceMaintenance3Properties
*props
=
1374 (VkPhysicalDeviceMaintenance3Properties
*)ext
;
1375 /* This value doesn't matter for us today as our per-stage
1376 * descriptors are the real limit.
1378 props
->maxPerSetDescriptors
= 1024;
1379 props
->maxMemoryAllocationSize
= MAX_MEMORY_ALLOCATION_SIZE
;
1383 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_MULTIVIEW_PROPERTIES
: {
1384 VkPhysicalDeviceMultiviewProperties
*properties
=
1385 (VkPhysicalDeviceMultiviewProperties
*)ext
;
1386 properties
->maxMultiviewViewCount
= 16;
1387 properties
->maxMultiviewInstanceIndex
= UINT32_MAX
/ 16;
1391 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PCI_BUS_INFO_PROPERTIES_EXT
: {
1392 VkPhysicalDevicePCIBusInfoPropertiesEXT
*properties
=
1393 (VkPhysicalDevicePCIBusInfoPropertiesEXT
*)ext
;
1394 properties
->pciDomain
= pdevice
->pci_info
.domain
;
1395 properties
->pciBus
= pdevice
->pci_info
.bus
;
1396 properties
->pciDevice
= pdevice
->pci_info
.device
;
1397 properties
->pciFunction
= pdevice
->pci_info
.function
;
1401 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_POINT_CLIPPING_PROPERTIES
: {
1402 VkPhysicalDevicePointClippingProperties
*properties
=
1403 (VkPhysicalDevicePointClippingProperties
*) ext
;
1404 properties
->pointClippingBehavior
= VK_POINT_CLIPPING_BEHAVIOR_ALL_CLIP_PLANES
;
1405 anv_finishme("Implement pop-free point clipping");
1409 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PROTECTED_MEMORY_PROPERTIES
: {
1410 VkPhysicalDeviceProtectedMemoryProperties
*props
=
1411 (VkPhysicalDeviceProtectedMemoryProperties
*)ext
;
1412 props
->protectedNoFault
= false;
1416 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PUSH_DESCRIPTOR_PROPERTIES_KHR
: {
1417 VkPhysicalDevicePushDescriptorPropertiesKHR
*properties
=
1418 (VkPhysicalDevicePushDescriptorPropertiesKHR
*) ext
;
1420 properties
->maxPushDescriptors
= MAX_PUSH_DESCRIPTORS
;
1424 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SAMPLER_FILTER_MINMAX_PROPERTIES_EXT
: {
1425 VkPhysicalDeviceSamplerFilterMinmaxPropertiesEXT
*properties
=
1426 (VkPhysicalDeviceSamplerFilterMinmaxPropertiesEXT
*)ext
;
1427 properties
->filterMinmaxImageComponentMapping
= pdevice
->info
.gen
>= 9;
1428 properties
->filterMinmaxSingleComponentFormats
= true;
1432 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SUBGROUP_PROPERTIES
: {
1433 VkPhysicalDeviceSubgroupProperties
*properties
= (void *)ext
;
1435 properties
->subgroupSize
= BRW_SUBGROUP_SIZE
;
1437 VkShaderStageFlags scalar_stages
= 0;
1438 for (unsigned stage
= 0; stage
< MESA_SHADER_STAGES
; stage
++) {
1439 if (pdevice
->compiler
->scalar_stage
[stage
])
1440 scalar_stages
|= mesa_to_vk_shader_stage(stage
);
1442 properties
->supportedStages
= scalar_stages
;
1444 properties
->supportedOperations
= VK_SUBGROUP_FEATURE_BASIC_BIT
|
1445 VK_SUBGROUP_FEATURE_VOTE_BIT
|
1446 VK_SUBGROUP_FEATURE_ARITHMETIC_BIT
|
1447 VK_SUBGROUP_FEATURE_BALLOT_BIT
|
1448 VK_SUBGROUP_FEATURE_SHUFFLE_BIT
|
1449 VK_SUBGROUP_FEATURE_SHUFFLE_RELATIVE_BIT
|
1450 VK_SUBGROUP_FEATURE_CLUSTERED_BIT
|
1451 VK_SUBGROUP_FEATURE_QUAD_BIT
;
1452 properties
->quadOperationsInAllStages
= true;
1456 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_TRANSFORM_FEEDBACK_PROPERTIES_EXT
: {
1457 VkPhysicalDeviceTransformFeedbackPropertiesEXT
*props
=
1458 (VkPhysicalDeviceTransformFeedbackPropertiesEXT
*)ext
;
1460 props
->maxTransformFeedbackStreams
= MAX_XFB_STREAMS
;
1461 props
->maxTransformFeedbackBuffers
= MAX_XFB_BUFFERS
;
1462 props
->maxTransformFeedbackBufferSize
= (1ull << 32);
1463 props
->maxTransformFeedbackStreamDataSize
= 128 * 4;
1464 props
->maxTransformFeedbackBufferDataSize
= 128 * 4;
1465 props
->maxTransformFeedbackBufferDataStride
= 2048;
1466 props
->transformFeedbackQueries
= true;
1467 props
->transformFeedbackStreamsLinesTriangles
= false;
1468 props
->transformFeedbackRasterizationStreamSelect
= false;
1469 props
->transformFeedbackDraw
= true;
1473 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VERTEX_ATTRIBUTE_DIVISOR_PROPERTIES_EXT
: {
1474 VkPhysicalDeviceVertexAttributeDivisorPropertiesEXT
*props
=
1475 (VkPhysicalDeviceVertexAttributeDivisorPropertiesEXT
*)ext
;
1476 /* We have to restrict this a bit for multiview */
1477 props
->maxVertexAttribDivisor
= UINT32_MAX
/ 16;
1482 anv_debug_ignored_stype(ext
->sType
);
1488 /* We support exactly one queue family. */
1489 static const VkQueueFamilyProperties
1490 anv_queue_family_properties
= {
1491 .queueFlags
= VK_QUEUE_GRAPHICS_BIT
|
1492 VK_QUEUE_COMPUTE_BIT
|
1493 VK_QUEUE_TRANSFER_BIT
,
1495 .timestampValidBits
= 36, /* XXX: Real value here */
1496 .minImageTransferGranularity
= { 1, 1, 1 },
1499 void anv_GetPhysicalDeviceQueueFamilyProperties(
1500 VkPhysicalDevice physicalDevice
,
1502 VkQueueFamilyProperties
* pQueueFamilyProperties
)
1504 VK_OUTARRAY_MAKE(out
, pQueueFamilyProperties
, pCount
);
1506 vk_outarray_append(&out
, p
) {
1507 *p
= anv_queue_family_properties
;
1511 void anv_GetPhysicalDeviceQueueFamilyProperties2(
1512 VkPhysicalDevice physicalDevice
,
1513 uint32_t* pQueueFamilyPropertyCount
,
1514 VkQueueFamilyProperties2
* pQueueFamilyProperties
)
1517 VK_OUTARRAY_MAKE(out
, pQueueFamilyProperties
, pQueueFamilyPropertyCount
);
1519 vk_outarray_append(&out
, p
) {
1520 p
->queueFamilyProperties
= anv_queue_family_properties
;
1522 vk_foreach_struct(s
, p
->pNext
) {
1523 anv_debug_ignored_stype(s
->sType
);
1528 void anv_GetPhysicalDeviceMemoryProperties(
1529 VkPhysicalDevice physicalDevice
,
1530 VkPhysicalDeviceMemoryProperties
* pMemoryProperties
)
1532 ANV_FROM_HANDLE(anv_physical_device
, physical_device
, physicalDevice
);
1534 pMemoryProperties
->memoryTypeCount
= physical_device
->memory
.type_count
;
1535 for (uint32_t i
= 0; i
< physical_device
->memory
.type_count
; i
++) {
1536 pMemoryProperties
->memoryTypes
[i
] = (VkMemoryType
) {
1537 .propertyFlags
= physical_device
->memory
.types
[i
].propertyFlags
,
1538 .heapIndex
= physical_device
->memory
.types
[i
].heapIndex
,
1542 pMemoryProperties
->memoryHeapCount
= physical_device
->memory
.heap_count
;
1543 for (uint32_t i
= 0; i
< physical_device
->memory
.heap_count
; i
++) {
1544 pMemoryProperties
->memoryHeaps
[i
] = (VkMemoryHeap
) {
1545 .size
= physical_device
->memory
.heaps
[i
].size
,
1546 .flags
= physical_device
->memory
.heaps
[i
].flags
,
1551 void anv_GetPhysicalDeviceMemoryProperties2(
1552 VkPhysicalDevice physicalDevice
,
1553 VkPhysicalDeviceMemoryProperties2
* pMemoryProperties
)
1555 anv_GetPhysicalDeviceMemoryProperties(physicalDevice
,
1556 &pMemoryProperties
->memoryProperties
);
1558 vk_foreach_struct(ext
, pMemoryProperties
->pNext
) {
1559 switch (ext
->sType
) {
1561 anv_debug_ignored_stype(ext
->sType
);
1568 anv_GetDeviceGroupPeerMemoryFeatures(
1571 uint32_t localDeviceIndex
,
1572 uint32_t remoteDeviceIndex
,
1573 VkPeerMemoryFeatureFlags
* pPeerMemoryFeatures
)
1575 assert(localDeviceIndex
== 0 && remoteDeviceIndex
== 0);
1576 *pPeerMemoryFeatures
= VK_PEER_MEMORY_FEATURE_COPY_SRC_BIT
|
1577 VK_PEER_MEMORY_FEATURE_COPY_DST_BIT
|
1578 VK_PEER_MEMORY_FEATURE_GENERIC_SRC_BIT
|
1579 VK_PEER_MEMORY_FEATURE_GENERIC_DST_BIT
;
1582 PFN_vkVoidFunction
anv_GetInstanceProcAddr(
1583 VkInstance _instance
,
1586 ANV_FROM_HANDLE(anv_instance
, instance
, _instance
);
1588 /* The Vulkan 1.0 spec for vkGetInstanceProcAddr has a table of exactly
1589 * when we have to return valid function pointers, NULL, or it's left
1590 * undefined. See the table for exact details.
1595 #define LOOKUP_ANV_ENTRYPOINT(entrypoint) \
1596 if (strcmp(pName, "vk" #entrypoint) == 0) \
1597 return (PFN_vkVoidFunction)anv_##entrypoint
1599 LOOKUP_ANV_ENTRYPOINT(EnumerateInstanceExtensionProperties
);
1600 LOOKUP_ANV_ENTRYPOINT(EnumerateInstanceLayerProperties
);
1601 LOOKUP_ANV_ENTRYPOINT(EnumerateInstanceVersion
);
1602 LOOKUP_ANV_ENTRYPOINT(CreateInstance
);
1604 #undef LOOKUP_ANV_ENTRYPOINT
1606 if (instance
== NULL
)
1609 int idx
= anv_get_instance_entrypoint_index(pName
);
1611 return instance
->dispatch
.entrypoints
[idx
];
1613 idx
= anv_get_device_entrypoint_index(pName
);
1615 return instance
->device_dispatch
.entrypoints
[idx
];
1620 /* With version 1+ of the loader interface the ICD should expose
1621 * vk_icdGetInstanceProcAddr to work around certain LD_PRELOAD issues seen in apps.
1624 VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL
vk_icdGetInstanceProcAddr(
1625 VkInstance instance
,
1629 VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL
vk_icdGetInstanceProcAddr(
1630 VkInstance instance
,
1633 return anv_GetInstanceProcAddr(instance
, pName
);
1636 PFN_vkVoidFunction
anv_GetDeviceProcAddr(
1640 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1642 if (!device
|| !pName
)
1645 int idx
= anv_get_device_entrypoint_index(pName
);
1649 return device
->dispatch
.entrypoints
[idx
];
1653 anv_CreateDebugReportCallbackEXT(VkInstance _instance
,
1654 const VkDebugReportCallbackCreateInfoEXT
* pCreateInfo
,
1655 const VkAllocationCallbacks
* pAllocator
,
1656 VkDebugReportCallbackEXT
* pCallback
)
1658 ANV_FROM_HANDLE(anv_instance
, instance
, _instance
);
1659 return vk_create_debug_report_callback(&instance
->debug_report_callbacks
,
1660 pCreateInfo
, pAllocator
, &instance
->alloc
,
1665 anv_DestroyDebugReportCallbackEXT(VkInstance _instance
,
1666 VkDebugReportCallbackEXT _callback
,
1667 const VkAllocationCallbacks
* pAllocator
)
1669 ANV_FROM_HANDLE(anv_instance
, instance
, _instance
);
1670 vk_destroy_debug_report_callback(&instance
->debug_report_callbacks
,
1671 _callback
, pAllocator
, &instance
->alloc
);
1675 anv_DebugReportMessageEXT(VkInstance _instance
,
1676 VkDebugReportFlagsEXT flags
,
1677 VkDebugReportObjectTypeEXT objectType
,
1680 int32_t messageCode
,
1681 const char* pLayerPrefix
,
1682 const char* pMessage
)
1684 ANV_FROM_HANDLE(anv_instance
, instance
, _instance
);
1685 vk_debug_report(&instance
->debug_report_callbacks
, flags
, objectType
,
1686 object
, location
, messageCode
, pLayerPrefix
, pMessage
);
1690 anv_queue_init(struct anv_device
*device
, struct anv_queue
*queue
)
1692 queue
->_loader_data
.loaderMagic
= ICD_LOADER_MAGIC
;
1693 queue
->device
= device
;
1698 anv_queue_finish(struct anv_queue
*queue
)
1702 static struct anv_state
1703 anv_state_pool_emit_data(struct anv_state_pool
*pool
, size_t size
, size_t align
, const void *p
)
1705 struct anv_state state
;
1707 state
= anv_state_pool_alloc(pool
, size
, align
);
1708 memcpy(state
.map
, p
, size
);
1713 struct gen8_border_color
{
1718 /* Pad out to 64 bytes */
1723 anv_device_init_border_colors(struct anv_device
*device
)
1725 static const struct gen8_border_color border_colors
[] = {
1726 [VK_BORDER_COLOR_FLOAT_TRANSPARENT_BLACK
] = { .float32
= { 0.0, 0.0, 0.0, 0.0 } },
1727 [VK_BORDER_COLOR_FLOAT_OPAQUE_BLACK
] = { .float32
= { 0.0, 0.0, 0.0, 1.0 } },
1728 [VK_BORDER_COLOR_FLOAT_OPAQUE_WHITE
] = { .float32
= { 1.0, 1.0, 1.0, 1.0 } },
1729 [VK_BORDER_COLOR_INT_TRANSPARENT_BLACK
] = { .uint32
= { 0, 0, 0, 0 } },
1730 [VK_BORDER_COLOR_INT_OPAQUE_BLACK
] = { .uint32
= { 0, 0, 0, 1 } },
1731 [VK_BORDER_COLOR_INT_OPAQUE_WHITE
] = { .uint32
= { 1, 1, 1, 1 } },
1734 device
->border_colors
= anv_state_pool_emit_data(&device
->dynamic_state_pool
,
1735 sizeof(border_colors
), 64,
1740 anv_device_init_trivial_batch(struct anv_device
*device
)
1742 anv_bo_init_new(&device
->trivial_batch_bo
, device
, 4096);
1744 if (device
->instance
->physicalDevice
.has_exec_async
)
1745 device
->trivial_batch_bo
.flags
|= EXEC_OBJECT_ASYNC
;
1747 if (device
->instance
->physicalDevice
.use_softpin
)
1748 device
->trivial_batch_bo
.flags
|= EXEC_OBJECT_PINNED
;
1750 anv_vma_alloc(device
, &device
->trivial_batch_bo
);
1752 void *map
= anv_gem_mmap(device
, device
->trivial_batch_bo
.gem_handle
,
1755 struct anv_batch batch
= {
1761 anv_batch_emit(&batch
, GEN7_MI_BATCH_BUFFER_END
, bbe
);
1762 anv_batch_emit(&batch
, GEN7_MI_NOOP
, noop
);
1764 if (!device
->info
.has_llc
)
1765 gen_clflush_range(map
, batch
.next
- map
);
1767 anv_gem_munmap(map
, device
->trivial_batch_bo
.size
);
1770 VkResult
anv_EnumerateDeviceExtensionProperties(
1771 VkPhysicalDevice physicalDevice
,
1772 const char* pLayerName
,
1773 uint32_t* pPropertyCount
,
1774 VkExtensionProperties
* pProperties
)
1776 ANV_FROM_HANDLE(anv_physical_device
, device
, physicalDevice
);
1777 VK_OUTARRAY_MAKE(out
, pProperties
, pPropertyCount
);
1779 for (int i
= 0; i
< ANV_DEVICE_EXTENSION_COUNT
; i
++) {
1780 if (device
->supported_extensions
.extensions
[i
]) {
1781 vk_outarray_append(&out
, prop
) {
1782 *prop
= anv_device_extensions
[i
];
1787 return vk_outarray_status(&out
);
1791 anv_device_init_dispatch(struct anv_device
*device
)
1793 const struct anv_device_dispatch_table
*genX_table
;
1794 switch (device
->info
.gen
) {
1796 genX_table
= &gen11_device_dispatch_table
;
1799 genX_table
= &gen10_device_dispatch_table
;
1802 genX_table
= &gen9_device_dispatch_table
;
1805 genX_table
= &gen8_device_dispatch_table
;
1808 if (device
->info
.is_haswell
)
1809 genX_table
= &gen75_device_dispatch_table
;
1811 genX_table
= &gen7_device_dispatch_table
;
1814 unreachable("unsupported gen\n");
1817 for (unsigned i
= 0; i
< ARRAY_SIZE(device
->dispatch
.entrypoints
); i
++) {
1818 /* Vulkan requires that entrypoints for extensions which have not been
1819 * enabled must not be advertised.
1821 if (!anv_device_entrypoint_is_enabled(i
, device
->instance
->app_info
.api_version
,
1822 &device
->instance
->enabled_extensions
,
1823 &device
->enabled_extensions
)) {
1824 device
->dispatch
.entrypoints
[i
] = NULL
;
1825 } else if (genX_table
->entrypoints
[i
]) {
1826 device
->dispatch
.entrypoints
[i
] = genX_table
->entrypoints
[i
];
1828 device
->dispatch
.entrypoints
[i
] =
1829 anv_device_dispatch_table
.entrypoints
[i
];
1835 vk_priority_to_gen(int priority
)
1838 case VK_QUEUE_GLOBAL_PRIORITY_LOW_EXT
:
1839 return GEN_CONTEXT_LOW_PRIORITY
;
1840 case VK_QUEUE_GLOBAL_PRIORITY_MEDIUM_EXT
:
1841 return GEN_CONTEXT_MEDIUM_PRIORITY
;
1842 case VK_QUEUE_GLOBAL_PRIORITY_HIGH_EXT
:
1843 return GEN_CONTEXT_HIGH_PRIORITY
;
1844 case VK_QUEUE_GLOBAL_PRIORITY_REALTIME_EXT
:
1845 return GEN_CONTEXT_REALTIME_PRIORITY
;
1847 unreachable("Invalid priority");
1852 anv_device_init_hiz_clear_value_bo(struct anv_device
*device
)
1854 anv_bo_init_new(&device
->hiz_clear_bo
, device
, 4096);
1856 if (device
->instance
->physicalDevice
.has_exec_async
)
1857 device
->hiz_clear_bo
.flags
|= EXEC_OBJECT_ASYNC
;
1859 if (device
->instance
->physicalDevice
.use_softpin
)
1860 device
->hiz_clear_bo
.flags
|= EXEC_OBJECT_PINNED
;
1862 anv_vma_alloc(device
, &device
->hiz_clear_bo
);
1864 uint32_t *map
= anv_gem_mmap(device
, device
->hiz_clear_bo
.gem_handle
,
1867 union isl_color_value hiz_clear
= { .u32
= { 0, } };
1868 hiz_clear
.f32
[0] = ANV_HZ_FC_VAL
;
1870 memcpy(map
, hiz_clear
.u32
, sizeof(hiz_clear
.u32
));
1871 anv_gem_munmap(map
, device
->hiz_clear_bo
.size
);
1875 get_bo_from_pool(struct gen_batch_decode_bo
*ret
,
1876 struct anv_block_pool
*pool
,
1879 for (uint32_t i
= 0; i
< pool
->nbos
; i
++) {
1880 uint64_t bo_address
= pool
->bos
[i
].offset
& (~0ull >> 16);
1881 uint32_t bo_size
= pool
->bos
[i
].size
;
1882 if (address
>= bo_address
&& address
< (bo_address
+ bo_size
)) {
1883 *ret
= (struct gen_batch_decode_bo
) {
1886 .map
= pool
->bos
[i
].map
,
1894 /* Finding a buffer for batch decoding */
1895 static struct gen_batch_decode_bo
1896 decode_get_bo(void *v_batch
, bool ppgtt
, uint64_t address
)
1898 struct anv_device
*device
= v_batch
;
1899 struct gen_batch_decode_bo ret_bo
= {};
1903 if (get_bo_from_pool(&ret_bo
, &device
->dynamic_state_pool
.block_pool
, address
))
1905 if (get_bo_from_pool(&ret_bo
, &device
->instruction_state_pool
.block_pool
, address
))
1907 if (get_bo_from_pool(&ret_bo
, &device
->binding_table_pool
.block_pool
, address
))
1909 if (get_bo_from_pool(&ret_bo
, &device
->surface_state_pool
.block_pool
, address
))
1912 if (!device
->cmd_buffer_being_decoded
)
1913 return (struct gen_batch_decode_bo
) { };
1915 struct anv_batch_bo
**bo
;
1917 u_vector_foreach(bo
, &device
->cmd_buffer_being_decoded
->seen_bbos
) {
1918 /* The decoder zeroes out the top 16 bits, so we need to as well */
1919 uint64_t bo_address
= (*bo
)->bo
.offset
& (~0ull >> 16);
1921 if (address
>= bo_address
&& address
< bo_address
+ (*bo
)->bo
.size
) {
1922 return (struct gen_batch_decode_bo
) {
1924 .size
= (*bo
)->bo
.size
,
1925 .map
= (*bo
)->bo
.map
,
1930 return (struct gen_batch_decode_bo
) { };
1933 VkResult
anv_CreateDevice(
1934 VkPhysicalDevice physicalDevice
,
1935 const VkDeviceCreateInfo
* pCreateInfo
,
1936 const VkAllocationCallbacks
* pAllocator
,
1939 ANV_FROM_HANDLE(anv_physical_device
, physical_device
, physicalDevice
);
1941 struct anv_device
*device
;
1943 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO
);
1945 struct anv_device_extension_table enabled_extensions
= { };
1946 for (uint32_t i
= 0; i
< pCreateInfo
->enabledExtensionCount
; i
++) {
1948 for (idx
= 0; idx
< ANV_DEVICE_EXTENSION_COUNT
; idx
++) {
1949 if (strcmp(pCreateInfo
->ppEnabledExtensionNames
[i
],
1950 anv_device_extensions
[idx
].extensionName
) == 0)
1954 if (idx
>= ANV_DEVICE_EXTENSION_COUNT
)
1955 return vk_error(VK_ERROR_EXTENSION_NOT_PRESENT
);
1957 if (!physical_device
->supported_extensions
.extensions
[idx
])
1958 return vk_error(VK_ERROR_EXTENSION_NOT_PRESENT
);
1960 enabled_extensions
.extensions
[idx
] = true;
1963 /* Check enabled features */
1964 if (pCreateInfo
->pEnabledFeatures
) {
1965 VkPhysicalDeviceFeatures supported_features
;
1966 anv_GetPhysicalDeviceFeatures(physicalDevice
, &supported_features
);
1967 VkBool32
*supported_feature
= (VkBool32
*)&supported_features
;
1968 VkBool32
*enabled_feature
= (VkBool32
*)pCreateInfo
->pEnabledFeatures
;
1969 unsigned num_features
= sizeof(VkPhysicalDeviceFeatures
) / sizeof(VkBool32
);
1970 for (uint32_t i
= 0; i
< num_features
; i
++) {
1971 if (enabled_feature
[i
] && !supported_feature
[i
])
1972 return vk_error(VK_ERROR_FEATURE_NOT_PRESENT
);
1976 /* Check requested queues and fail if we are requested to create any
1977 * queues with flags we don't support.
1979 assert(pCreateInfo
->queueCreateInfoCount
> 0);
1980 for (uint32_t i
= 0; i
< pCreateInfo
->queueCreateInfoCount
; i
++) {
1981 if (pCreateInfo
->pQueueCreateInfos
[i
].flags
!= 0)
1982 return vk_error(VK_ERROR_INITIALIZATION_FAILED
);
1985 /* Check if client specified queue priority. */
1986 const VkDeviceQueueGlobalPriorityCreateInfoEXT
*queue_priority
=
1987 vk_find_struct_const(pCreateInfo
->pQueueCreateInfos
[0].pNext
,
1988 DEVICE_QUEUE_GLOBAL_PRIORITY_CREATE_INFO_EXT
);
1990 VkQueueGlobalPriorityEXT priority
=
1991 queue_priority
? queue_priority
->globalPriority
:
1992 VK_QUEUE_GLOBAL_PRIORITY_MEDIUM_EXT
;
1994 device
= vk_alloc2(&physical_device
->instance
->alloc
, pAllocator
,
1996 VK_SYSTEM_ALLOCATION_SCOPE_DEVICE
);
1998 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
2000 const unsigned decode_flags
=
2001 GEN_BATCH_DECODE_FULL
|
2002 ((INTEL_DEBUG
& DEBUG_COLOR
) ? GEN_BATCH_DECODE_IN_COLOR
: 0) |
2003 GEN_BATCH_DECODE_OFFSETS
|
2004 GEN_BATCH_DECODE_FLOATS
;
2006 gen_batch_decode_ctx_init(&device
->decoder_ctx
,
2007 &physical_device
->info
,
2008 stderr
, decode_flags
, NULL
,
2009 decode_get_bo
, NULL
, device
);
2011 device
->_loader_data
.loaderMagic
= ICD_LOADER_MAGIC
;
2012 device
->instance
= physical_device
->instance
;
2013 device
->chipset_id
= physical_device
->chipset_id
;
2014 device
->no_hw
= physical_device
->no_hw
;
2015 device
->_lost
= false;
2018 device
->alloc
= *pAllocator
;
2020 device
->alloc
= physical_device
->instance
->alloc
;
2022 /* XXX(chadv): Can we dup() physicalDevice->fd here? */
2023 device
->fd
= open(physical_device
->path
, O_RDWR
| O_CLOEXEC
);
2024 if (device
->fd
== -1) {
2025 result
= vk_error(VK_ERROR_INITIALIZATION_FAILED
);
2029 device
->context_id
= anv_gem_create_context(device
);
2030 if (device
->context_id
== -1) {
2031 result
= vk_error(VK_ERROR_INITIALIZATION_FAILED
);
2035 if (physical_device
->use_softpin
) {
2036 if (pthread_mutex_init(&device
->vma_mutex
, NULL
) != 0) {
2037 result
= vk_error(VK_ERROR_INITIALIZATION_FAILED
);
2041 /* keep the page with address zero out of the allocator */
2042 struct anv_memory_heap
*low_heap
=
2043 &physical_device
->memory
.heaps
[physical_device
->memory
.heap_count
- 1];
2044 util_vma_heap_init(&device
->vma_lo
, low_heap
->vma_start
, low_heap
->vma_size
);
2045 device
->vma_lo_available
= low_heap
->size
;
2047 struct anv_memory_heap
*high_heap
=
2048 &physical_device
->memory
.heaps
[0];
2049 util_vma_heap_init(&device
->vma_hi
, high_heap
->vma_start
, high_heap
->vma_size
);
2050 device
->vma_hi_available
= physical_device
->memory
.heap_count
== 1 ? 0 :
2054 list_inithead(&device
->memory_objects
);
2056 /* As per spec, the driver implementation may deny requests to acquire
2057 * a priority above the default priority (MEDIUM) if the caller does not
2058 * have sufficient privileges. In this scenario VK_ERROR_NOT_PERMITTED_EXT
2061 if (physical_device
->has_context_priority
) {
2062 int err
= anv_gem_set_context_param(device
->fd
, device
->context_id
,
2063 I915_CONTEXT_PARAM_PRIORITY
,
2064 vk_priority_to_gen(priority
));
2065 if (err
!= 0 && priority
> VK_QUEUE_GLOBAL_PRIORITY_MEDIUM_EXT
) {
2066 result
= vk_error(VK_ERROR_NOT_PERMITTED_EXT
);
2071 device
->info
= physical_device
->info
;
2072 device
->isl_dev
= physical_device
->isl_dev
;
2074 /* On Broadwell and later, we can use batch chaining to more efficiently
2075 * implement growing command buffers. Prior to Haswell, the kernel
2076 * command parser gets in the way and we have to fall back to growing
2079 device
->can_chain_batches
= device
->info
.gen
>= 8;
2081 device
->robust_buffer_access
= pCreateInfo
->pEnabledFeatures
&&
2082 pCreateInfo
->pEnabledFeatures
->robustBufferAccess
;
2083 device
->enabled_extensions
= enabled_extensions
;
2085 anv_device_init_dispatch(device
);
2087 if (pthread_mutex_init(&device
->mutex
, NULL
) != 0) {
2088 result
= vk_error(VK_ERROR_INITIALIZATION_FAILED
);
2089 goto fail_context_id
;
2092 pthread_condattr_t condattr
;
2093 if (pthread_condattr_init(&condattr
) != 0) {
2094 result
= vk_error(VK_ERROR_INITIALIZATION_FAILED
);
2097 if (pthread_condattr_setclock(&condattr
, CLOCK_MONOTONIC
) != 0) {
2098 pthread_condattr_destroy(&condattr
);
2099 result
= vk_error(VK_ERROR_INITIALIZATION_FAILED
);
2102 if (pthread_cond_init(&device
->queue_submit
, &condattr
) != 0) {
2103 pthread_condattr_destroy(&condattr
);
2104 result
= vk_error(VK_ERROR_INITIALIZATION_FAILED
);
2107 pthread_condattr_destroy(&condattr
);
2110 (physical_device
->supports_48bit_addresses
? EXEC_OBJECT_SUPPORTS_48B_ADDRESS
: 0) |
2111 (physical_device
->has_exec_async
? EXEC_OBJECT_ASYNC
: 0) |
2112 (physical_device
->has_exec_capture
? EXEC_OBJECT_CAPTURE
: 0) |
2113 (physical_device
->use_softpin
? EXEC_OBJECT_PINNED
: 0);
2115 anv_bo_pool_init(&device
->batch_bo_pool
, device
, bo_flags
);
2117 result
= anv_bo_cache_init(&device
->bo_cache
);
2118 if (result
!= VK_SUCCESS
)
2119 goto fail_batch_bo_pool
;
2121 if (!physical_device
->use_softpin
)
2122 bo_flags
&= ~EXEC_OBJECT_SUPPORTS_48B_ADDRESS
;
2124 result
= anv_state_pool_init(&device
->dynamic_state_pool
, device
,
2125 DYNAMIC_STATE_POOL_MIN_ADDRESS
,
2128 if (result
!= VK_SUCCESS
)
2131 result
= anv_state_pool_init(&device
->instruction_state_pool
, device
,
2132 INSTRUCTION_STATE_POOL_MIN_ADDRESS
,
2135 if (result
!= VK_SUCCESS
)
2136 goto fail_dynamic_state_pool
;
2138 result
= anv_state_pool_init(&device
->surface_state_pool
, device
,
2139 SURFACE_STATE_POOL_MIN_ADDRESS
,
2142 if (result
!= VK_SUCCESS
)
2143 goto fail_instruction_state_pool
;
2145 if (physical_device
->use_softpin
) {
2146 result
= anv_state_pool_init(&device
->binding_table_pool
, device
,
2147 BINDING_TABLE_POOL_MIN_ADDRESS
,
2150 if (result
!= VK_SUCCESS
)
2151 goto fail_surface_state_pool
;
2154 result
= anv_bo_init_new(&device
->workaround_bo
, device
, 1024);
2155 if (result
!= VK_SUCCESS
)
2156 goto fail_binding_table_pool
;
2158 if (physical_device
->use_softpin
)
2159 device
->workaround_bo
.flags
|= EXEC_OBJECT_PINNED
;
2161 if (!anv_vma_alloc(device
, &device
->workaround_bo
))
2162 goto fail_workaround_bo
;
2164 anv_device_init_trivial_batch(device
);
2166 if (device
->info
.gen
>= 10)
2167 anv_device_init_hiz_clear_value_bo(device
);
2169 anv_scratch_pool_init(device
, &device
->scratch_pool
);
2171 anv_queue_init(device
, &device
->queue
);
2173 switch (device
->info
.gen
) {
2175 if (!device
->info
.is_haswell
)
2176 result
= gen7_init_device_state(device
);
2178 result
= gen75_init_device_state(device
);
2181 result
= gen8_init_device_state(device
);
2184 result
= gen9_init_device_state(device
);
2187 result
= gen10_init_device_state(device
);
2190 result
= gen11_init_device_state(device
);
2193 /* Shouldn't get here as we don't create physical devices for any other
2195 unreachable("unhandled gen");
2197 if (result
!= VK_SUCCESS
)
2198 goto fail_workaround_bo
;
2200 anv_pipeline_cache_init(&device
->default_pipeline_cache
, device
, true);
2202 anv_device_init_blorp(device
);
2204 anv_device_init_border_colors(device
);
2206 *pDevice
= anv_device_to_handle(device
);
2211 anv_queue_finish(&device
->queue
);
2212 anv_scratch_pool_finish(device
, &device
->scratch_pool
);
2213 anv_gem_munmap(device
->workaround_bo
.map
, device
->workaround_bo
.size
);
2214 anv_gem_close(device
, device
->workaround_bo
.gem_handle
);
2215 fail_binding_table_pool
:
2216 if (physical_device
->use_softpin
)
2217 anv_state_pool_finish(&device
->binding_table_pool
);
2218 fail_surface_state_pool
:
2219 anv_state_pool_finish(&device
->surface_state_pool
);
2220 fail_instruction_state_pool
:
2221 anv_state_pool_finish(&device
->instruction_state_pool
);
2222 fail_dynamic_state_pool
:
2223 anv_state_pool_finish(&device
->dynamic_state_pool
);
2225 anv_bo_cache_finish(&device
->bo_cache
);
2227 anv_bo_pool_finish(&device
->batch_bo_pool
);
2228 pthread_cond_destroy(&device
->queue_submit
);
2230 pthread_mutex_destroy(&device
->mutex
);
2232 anv_gem_destroy_context(device
, device
->context_id
);
2236 vk_free(&device
->alloc
, device
);
2241 void anv_DestroyDevice(
2243 const VkAllocationCallbacks
* pAllocator
)
2245 ANV_FROM_HANDLE(anv_device
, device
, _device
);
2246 struct anv_physical_device
*physical_device
;
2251 physical_device
= &device
->instance
->physicalDevice
;
2253 anv_device_finish_blorp(device
);
2255 anv_pipeline_cache_finish(&device
->default_pipeline_cache
);
2257 anv_queue_finish(&device
->queue
);
2259 #ifdef HAVE_VALGRIND
2260 /* We only need to free these to prevent valgrind errors. The backing
2261 * BO will go away in a couple of lines so we don't actually leak.
2263 anv_state_pool_free(&device
->dynamic_state_pool
, device
->border_colors
);
2266 anv_scratch_pool_finish(device
, &device
->scratch_pool
);
2268 anv_gem_munmap(device
->workaround_bo
.map
, device
->workaround_bo
.size
);
2269 anv_vma_free(device
, &device
->workaround_bo
);
2270 anv_gem_close(device
, device
->workaround_bo
.gem_handle
);
2272 anv_vma_free(device
, &device
->trivial_batch_bo
);
2273 anv_gem_close(device
, device
->trivial_batch_bo
.gem_handle
);
2274 if (device
->info
.gen
>= 10)
2275 anv_gem_close(device
, device
->hiz_clear_bo
.gem_handle
);
2277 if (physical_device
->use_softpin
)
2278 anv_state_pool_finish(&device
->binding_table_pool
);
2279 anv_state_pool_finish(&device
->surface_state_pool
);
2280 anv_state_pool_finish(&device
->instruction_state_pool
);
2281 anv_state_pool_finish(&device
->dynamic_state_pool
);
2283 anv_bo_cache_finish(&device
->bo_cache
);
2285 anv_bo_pool_finish(&device
->batch_bo_pool
);
2287 pthread_cond_destroy(&device
->queue_submit
);
2288 pthread_mutex_destroy(&device
->mutex
);
2290 anv_gem_destroy_context(device
, device
->context_id
);
2292 gen_batch_decode_ctx_finish(&device
->decoder_ctx
);
2296 vk_free(&device
->alloc
, device
);
2299 VkResult
anv_EnumerateInstanceLayerProperties(
2300 uint32_t* pPropertyCount
,
2301 VkLayerProperties
* pProperties
)
2303 if (pProperties
== NULL
) {
2304 *pPropertyCount
= 0;
2308 /* None supported at this time */
2309 return vk_error(VK_ERROR_LAYER_NOT_PRESENT
);
2312 VkResult
anv_EnumerateDeviceLayerProperties(
2313 VkPhysicalDevice physicalDevice
,
2314 uint32_t* pPropertyCount
,
2315 VkLayerProperties
* pProperties
)
2317 if (pProperties
== NULL
) {
2318 *pPropertyCount
= 0;
2322 /* None supported at this time */
2323 return vk_error(VK_ERROR_LAYER_NOT_PRESENT
);
2326 void anv_GetDeviceQueue(
2328 uint32_t queueNodeIndex
,
2329 uint32_t queueIndex
,
2332 ANV_FROM_HANDLE(anv_device
, device
, _device
);
2334 assert(queueIndex
== 0);
2336 *pQueue
= anv_queue_to_handle(&device
->queue
);
2339 void anv_GetDeviceQueue2(
2341 const VkDeviceQueueInfo2
* pQueueInfo
,
2344 ANV_FROM_HANDLE(anv_device
, device
, _device
);
2346 assert(pQueueInfo
->queueIndex
== 0);
2348 if (pQueueInfo
->flags
== device
->queue
.flags
)
2349 *pQueue
= anv_queue_to_handle(&device
->queue
);
2355 _anv_device_set_lost(struct anv_device
*device
,
2356 const char *file
, int line
,
2357 const char *msg
, ...)
2362 device
->_lost
= true;
2365 err
= __vk_errorv(device
->instance
, device
,
2366 VK_DEBUG_REPORT_OBJECT_TYPE_DEVICE_EXT
,
2367 VK_ERROR_DEVICE_LOST
, file
, line
, msg
, ap
);
2370 if (env_var_as_boolean("ANV_ABORT_ON_DEVICE_LOSS", false))
2377 anv_device_query_status(struct anv_device
*device
)
2379 /* This isn't likely as most of the callers of this function already check
2380 * for it. However, it doesn't hurt to check and it potentially lets us
2383 if (anv_device_is_lost(device
))
2384 return VK_ERROR_DEVICE_LOST
;
2386 uint32_t active
, pending
;
2387 int ret
= anv_gem_gpu_get_reset_stats(device
, &active
, &pending
);
2389 /* We don't know the real error. */
2390 return anv_device_set_lost(device
, "get_reset_stats failed: %m");
2394 return anv_device_set_lost(device
, "GPU hung on one of our command buffers");
2395 } else if (pending
) {
2396 return anv_device_set_lost(device
, "GPU hung with commands in-flight");
2403 anv_device_bo_busy(struct anv_device
*device
, struct anv_bo
*bo
)
2405 /* Note: This only returns whether or not the BO is in use by an i915 GPU.
2406 * Other usages of the BO (such as on different hardware) will not be
2407 * flagged as "busy" by this ioctl. Use with care.
2409 int ret
= anv_gem_busy(device
, bo
->gem_handle
);
2411 return VK_NOT_READY
;
2412 } else if (ret
== -1) {
2413 /* We don't know the real error. */
2414 return anv_device_set_lost(device
, "gem wait failed: %m");
2417 /* Query for device status after the busy call. If the BO we're checking
2418 * got caught in a GPU hang we don't want to return VK_SUCCESS to the
2419 * client because it clearly doesn't have valid data. Yes, this most
2420 * likely means an ioctl, but we just did an ioctl to query the busy status
2421 * so it's no great loss.
2423 return anv_device_query_status(device
);
2427 anv_device_wait(struct anv_device
*device
, struct anv_bo
*bo
,
2430 int ret
= anv_gem_wait(device
, bo
->gem_handle
, &timeout
);
2431 if (ret
== -1 && errno
== ETIME
) {
2433 } else if (ret
== -1) {
2434 /* We don't know the real error. */
2435 return anv_device_set_lost(device
, "gem wait failed: %m");
2438 /* Query for device status after the wait. If the BO we're waiting on got
2439 * caught in a GPU hang we don't want to return VK_SUCCESS to the client
2440 * because it clearly doesn't have valid data. Yes, this most likely means
2441 * an ioctl, but we just did an ioctl to wait so it's no great loss.
2443 return anv_device_query_status(device
);
2446 VkResult
anv_DeviceWaitIdle(
2449 ANV_FROM_HANDLE(anv_device
, device
, _device
);
2450 if (anv_device_is_lost(device
))
2451 return VK_ERROR_DEVICE_LOST
;
2453 struct anv_batch batch
;
2456 batch
.start
= batch
.next
= cmds
;
2457 batch
.end
= (void *) cmds
+ sizeof(cmds
);
2459 anv_batch_emit(&batch
, GEN7_MI_BATCH_BUFFER_END
, bbe
);
2460 anv_batch_emit(&batch
, GEN7_MI_NOOP
, noop
);
2462 return anv_device_submit_simple_batch(device
, &batch
);
2466 anv_vma_alloc(struct anv_device
*device
, struct anv_bo
*bo
)
2468 if (!(bo
->flags
& EXEC_OBJECT_PINNED
))
2471 pthread_mutex_lock(&device
->vma_mutex
);
2475 if (bo
->flags
& EXEC_OBJECT_SUPPORTS_48B_ADDRESS
&&
2476 device
->vma_hi_available
>= bo
->size
) {
2477 uint64_t addr
= util_vma_heap_alloc(&device
->vma_hi
, bo
->size
, 4096);
2479 bo
->offset
= gen_canonical_address(addr
);
2480 assert(addr
== gen_48b_address(bo
->offset
));
2481 device
->vma_hi_available
-= bo
->size
;
2485 if (bo
->offset
== 0 && device
->vma_lo_available
>= bo
->size
) {
2486 uint64_t addr
= util_vma_heap_alloc(&device
->vma_lo
, bo
->size
, 4096);
2488 bo
->offset
= gen_canonical_address(addr
);
2489 assert(addr
== gen_48b_address(bo
->offset
));
2490 device
->vma_lo_available
-= bo
->size
;
2494 pthread_mutex_unlock(&device
->vma_mutex
);
2496 return bo
->offset
!= 0;
2500 anv_vma_free(struct anv_device
*device
, struct anv_bo
*bo
)
2502 if (!(bo
->flags
& EXEC_OBJECT_PINNED
))
2505 const uint64_t addr_48b
= gen_48b_address(bo
->offset
);
2507 pthread_mutex_lock(&device
->vma_mutex
);
2509 if (addr_48b
>= LOW_HEAP_MIN_ADDRESS
&&
2510 addr_48b
<= LOW_HEAP_MAX_ADDRESS
) {
2511 util_vma_heap_free(&device
->vma_lo
, addr_48b
, bo
->size
);
2512 device
->vma_lo_available
+= bo
->size
;
2514 MAYBE_UNUSED
const struct anv_physical_device
*physical_device
=
2515 &device
->instance
->physicalDevice
;
2516 assert(addr_48b
>= physical_device
->memory
.heaps
[0].vma_start
&&
2517 addr_48b
< (physical_device
->memory
.heaps
[0].vma_start
+
2518 physical_device
->memory
.heaps
[0].vma_size
));
2519 util_vma_heap_free(&device
->vma_hi
, addr_48b
, bo
->size
);
2520 device
->vma_hi_available
+= bo
->size
;
2523 pthread_mutex_unlock(&device
->vma_mutex
);
2529 anv_bo_init_new(struct anv_bo
*bo
, struct anv_device
*device
, uint64_t size
)
2531 uint32_t gem_handle
= anv_gem_create(device
, size
);
2533 return vk_error(VK_ERROR_OUT_OF_DEVICE_MEMORY
);
2535 anv_bo_init(bo
, gem_handle
, size
);
2540 VkResult
anv_AllocateMemory(
2542 const VkMemoryAllocateInfo
* pAllocateInfo
,
2543 const VkAllocationCallbacks
* pAllocator
,
2544 VkDeviceMemory
* pMem
)
2546 ANV_FROM_HANDLE(anv_device
, device
, _device
);
2547 struct anv_physical_device
*pdevice
= &device
->instance
->physicalDevice
;
2548 struct anv_device_memory
*mem
;
2549 VkResult result
= VK_SUCCESS
;
2551 assert(pAllocateInfo
->sType
== VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO
);
2553 /* The Vulkan 1.0.33 spec says "allocationSize must be greater than 0". */
2554 assert(pAllocateInfo
->allocationSize
> 0);
2556 if (pAllocateInfo
->allocationSize
> MAX_MEMORY_ALLOCATION_SIZE
)
2557 return VK_ERROR_OUT_OF_DEVICE_MEMORY
;
2559 /* FINISHME: Fail if allocation request exceeds heap size. */
2561 mem
= vk_alloc2(&device
->alloc
, pAllocator
, sizeof(*mem
), 8,
2562 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
2564 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
2566 assert(pAllocateInfo
->memoryTypeIndex
< pdevice
->memory
.type_count
);
2567 mem
->type
= &pdevice
->memory
.types
[pAllocateInfo
->memoryTypeIndex
];
2571 mem
->host_ptr
= NULL
;
2573 uint64_t bo_flags
= 0;
2575 assert(mem
->type
->heapIndex
< pdevice
->memory
.heap_count
);
2576 if (pdevice
->memory
.heaps
[mem
->type
->heapIndex
].supports_48bit_addresses
)
2577 bo_flags
|= EXEC_OBJECT_SUPPORTS_48B_ADDRESS
;
2579 const struct wsi_memory_allocate_info
*wsi_info
=
2580 vk_find_struct_const(pAllocateInfo
->pNext
, WSI_MEMORY_ALLOCATE_INFO_MESA
);
2581 if (wsi_info
&& wsi_info
->implicit_sync
) {
2582 /* We need to set the WRITE flag on window system buffers so that GEM
2583 * will know we're writing to them and synchronize uses on other rings
2584 * (eg if the display server uses the blitter ring).
2586 bo_flags
|= EXEC_OBJECT_WRITE
;
2587 } else if (pdevice
->has_exec_async
) {
2588 bo_flags
|= EXEC_OBJECT_ASYNC
;
2591 if (pdevice
->use_softpin
)
2592 bo_flags
|= EXEC_OBJECT_PINNED
;
2594 const VkExportMemoryAllocateInfo
*export_info
=
2595 vk_find_struct_const(pAllocateInfo
->pNext
, EXPORT_MEMORY_ALLOCATE_INFO
);
2597 /* Check if we need to support Android HW buffer export. If so,
2598 * create AHardwareBuffer and import memory from it.
2600 bool android_export
= false;
2601 if (export_info
&& export_info
->handleTypes
&
2602 VK_EXTERNAL_MEMORY_HANDLE_TYPE_ANDROID_HARDWARE_BUFFER_BIT_ANDROID
)
2603 android_export
= true;
2605 /* Android memory import. */
2606 const struct VkImportAndroidHardwareBufferInfoANDROID
*ahw_import_info
=
2607 vk_find_struct_const(pAllocateInfo
->pNext
,
2608 IMPORT_ANDROID_HARDWARE_BUFFER_INFO_ANDROID
);
2610 if (ahw_import_info
) {
2611 result
= anv_import_ahw_memory(_device
, mem
, ahw_import_info
);
2612 if (result
!= VK_SUCCESS
)
2616 } else if (android_export
) {
2617 result
= anv_create_ahw_memory(_device
, mem
, pAllocateInfo
);
2618 if (result
!= VK_SUCCESS
)
2621 const struct VkImportAndroidHardwareBufferInfoANDROID import_info
= {
2624 result
= anv_import_ahw_memory(_device
, mem
, &import_info
);
2625 if (result
!= VK_SUCCESS
)
2631 const VkImportMemoryFdInfoKHR
*fd_info
=
2632 vk_find_struct_const(pAllocateInfo
->pNext
, IMPORT_MEMORY_FD_INFO_KHR
);
2634 /* The Vulkan spec permits handleType to be 0, in which case the struct is
2637 if (fd_info
&& fd_info
->handleType
) {
2638 /* At the moment, we support only the below handle types. */
2639 assert(fd_info
->handleType
==
2640 VK_EXTERNAL_MEMORY_HANDLE_TYPE_OPAQUE_FD_BIT
||
2641 fd_info
->handleType
==
2642 VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_EXT
);
2644 result
= anv_bo_cache_import(device
, &device
->bo_cache
, fd_info
->fd
,
2645 bo_flags
| ANV_BO_EXTERNAL
, &mem
->bo
);
2646 if (result
!= VK_SUCCESS
)
2649 VkDeviceSize aligned_alloc_size
=
2650 align_u64(pAllocateInfo
->allocationSize
, 4096);
2652 /* For security purposes, we reject importing the bo if it's smaller
2653 * than the requested allocation size. This prevents a malicious client
2654 * from passing a buffer to a trusted client, lying about the size, and
2655 * telling the trusted client to try and texture from an image that goes
2656 * out-of-bounds. This sort of thing could lead to GPU hangs or worse
2657 * in the trusted client. The trusted client can protect itself against
2658 * this sort of attack but only if it can trust the buffer size.
2660 if (mem
->bo
->size
< aligned_alloc_size
) {
2661 result
= vk_errorf(device
->instance
, device
,
2662 VK_ERROR_INVALID_EXTERNAL_HANDLE
,
2663 "aligned allocationSize too large for "
2664 "VK_EXTERNAL_MEMORY_HANDLE_TYPE_OPAQUE_FD_BIT: "
2665 "%"PRIu64
"B > %"PRIu64
"B",
2666 aligned_alloc_size
, mem
->bo
->size
);
2667 anv_bo_cache_release(device
, &device
->bo_cache
, mem
->bo
);
2671 /* From the Vulkan spec:
2673 * "Importing memory from a file descriptor transfers ownership of
2674 * the file descriptor from the application to the Vulkan
2675 * implementation. The application must not perform any operations on
2676 * the file descriptor after a successful import."
2678 * If the import fails, we leave the file descriptor open.
2684 const VkImportMemoryHostPointerInfoEXT
*host_ptr_info
=
2685 vk_find_struct_const(pAllocateInfo
->pNext
,
2686 IMPORT_MEMORY_HOST_POINTER_INFO_EXT
);
2687 if (host_ptr_info
&& host_ptr_info
->handleType
) {
2688 if (host_ptr_info
->handleType
==
2689 VK_EXTERNAL_MEMORY_HANDLE_TYPE_HOST_MAPPED_FOREIGN_MEMORY_BIT_EXT
) {
2690 result
= vk_error(VK_ERROR_INVALID_EXTERNAL_HANDLE
);
2694 assert(host_ptr_info
->handleType
==
2695 VK_EXTERNAL_MEMORY_HANDLE_TYPE_HOST_ALLOCATION_BIT_EXT
);
2697 result
= anv_bo_cache_import_host_ptr(
2698 device
, &device
->bo_cache
, host_ptr_info
->pHostPointer
,
2699 pAllocateInfo
->allocationSize
, bo_flags
, &mem
->bo
);
2701 if (result
!= VK_SUCCESS
)
2704 mem
->host_ptr
= host_ptr_info
->pHostPointer
;
2708 /* Regular allocate (not importing memory). */
2710 if (export_info
&& export_info
->handleTypes
)
2711 bo_flags
|= ANV_BO_EXTERNAL
;
2713 result
= anv_bo_cache_alloc(device
, &device
->bo_cache
,
2714 pAllocateInfo
->allocationSize
, bo_flags
,
2716 if (result
!= VK_SUCCESS
)
2719 const VkMemoryDedicatedAllocateInfo
*dedicated_info
=
2720 vk_find_struct_const(pAllocateInfo
->pNext
, MEMORY_DEDICATED_ALLOCATE_INFO
);
2721 if (dedicated_info
&& dedicated_info
->image
!= VK_NULL_HANDLE
) {
2722 ANV_FROM_HANDLE(anv_image
, image
, dedicated_info
->image
);
2724 /* Some legacy (non-modifiers) consumers need the tiling to be set on
2725 * the BO. In this case, we have a dedicated allocation.
2727 if (image
->needs_set_tiling
) {
2728 const uint32_t i915_tiling
=
2729 isl_tiling_to_i915_tiling(image
->planes
[0].surface
.isl
.tiling
);
2730 int ret
= anv_gem_set_tiling(device
, mem
->bo
->gem_handle
,
2731 image
->planes
[0].surface
.isl
.row_pitch_B
,
2734 anv_bo_cache_release(device
, &device
->bo_cache
, mem
->bo
);
2735 return vk_errorf(device
->instance
, NULL
,
2736 VK_ERROR_OUT_OF_DEVICE_MEMORY
,
2737 "failed to set BO tiling: %m");
2743 pthread_mutex_lock(&device
->mutex
);
2744 list_addtail(&mem
->link
, &device
->memory_objects
);
2745 pthread_mutex_unlock(&device
->mutex
);
2747 *pMem
= anv_device_memory_to_handle(mem
);
2752 vk_free2(&device
->alloc
, pAllocator
, mem
);
2757 VkResult
anv_GetMemoryFdKHR(
2759 const VkMemoryGetFdInfoKHR
* pGetFdInfo
,
2762 ANV_FROM_HANDLE(anv_device
, dev
, device_h
);
2763 ANV_FROM_HANDLE(anv_device_memory
, mem
, pGetFdInfo
->memory
);
2765 assert(pGetFdInfo
->sType
== VK_STRUCTURE_TYPE_MEMORY_GET_FD_INFO_KHR
);
2767 assert(pGetFdInfo
->handleType
== VK_EXTERNAL_MEMORY_HANDLE_TYPE_OPAQUE_FD_BIT
||
2768 pGetFdInfo
->handleType
== VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_EXT
);
2770 return anv_bo_cache_export(dev
, &dev
->bo_cache
, mem
->bo
, pFd
);
2773 VkResult
anv_GetMemoryFdPropertiesKHR(
2775 VkExternalMemoryHandleTypeFlagBits handleType
,
2777 VkMemoryFdPropertiesKHR
* pMemoryFdProperties
)
2779 ANV_FROM_HANDLE(anv_device
, device
, _device
);
2780 struct anv_physical_device
*pdevice
= &device
->instance
->physicalDevice
;
2782 switch (handleType
) {
2783 case VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_EXT
:
2784 /* dma-buf can be imported as any memory type */
2785 pMemoryFdProperties
->memoryTypeBits
=
2786 (1 << pdevice
->memory
.type_count
) - 1;
2790 /* The valid usage section for this function says:
2792 * "handleType must not be one of the handle types defined as
2795 * So opaque handle types fall into the default "unsupported" case.
2797 return vk_error(VK_ERROR_INVALID_EXTERNAL_HANDLE
);
2801 VkResult
anv_GetMemoryHostPointerPropertiesEXT(
2803 VkExternalMemoryHandleTypeFlagBits handleType
,
2804 const void* pHostPointer
,
2805 VkMemoryHostPointerPropertiesEXT
* pMemoryHostPointerProperties
)
2807 ANV_FROM_HANDLE(anv_device
, device
, _device
);
2809 assert(pMemoryHostPointerProperties
->sType
==
2810 VK_STRUCTURE_TYPE_MEMORY_HOST_POINTER_PROPERTIES_EXT
);
2812 switch (handleType
) {
2813 case VK_EXTERNAL_MEMORY_HANDLE_TYPE_HOST_ALLOCATION_BIT_EXT
: {
2814 struct anv_physical_device
*pdevice
= &device
->instance
->physicalDevice
;
2816 /* Host memory can be imported as any memory type. */
2817 pMemoryHostPointerProperties
->memoryTypeBits
=
2818 (1ull << pdevice
->memory
.type_count
) - 1;
2823 return VK_ERROR_INVALID_EXTERNAL_HANDLE
;
2827 void anv_FreeMemory(
2829 VkDeviceMemory _mem
,
2830 const VkAllocationCallbacks
* pAllocator
)
2832 ANV_FROM_HANDLE(anv_device
, device
, _device
);
2833 ANV_FROM_HANDLE(anv_device_memory
, mem
, _mem
);
2838 pthread_mutex_lock(&device
->mutex
);
2839 list_del(&mem
->link
);
2840 pthread_mutex_unlock(&device
->mutex
);
2843 anv_UnmapMemory(_device
, _mem
);
2845 anv_bo_cache_release(device
, &device
->bo_cache
, mem
->bo
);
2847 #if defined(ANDROID) && ANDROID_API_LEVEL >= 26
2849 AHardwareBuffer_release(mem
->ahw
);
2852 vk_free2(&device
->alloc
, pAllocator
, mem
);
2855 VkResult
anv_MapMemory(
2857 VkDeviceMemory _memory
,
2858 VkDeviceSize offset
,
2860 VkMemoryMapFlags flags
,
2863 ANV_FROM_HANDLE(anv_device
, device
, _device
);
2864 ANV_FROM_HANDLE(anv_device_memory
, mem
, _memory
);
2871 if (mem
->host_ptr
) {
2872 *ppData
= mem
->host_ptr
+ offset
;
2876 if (size
== VK_WHOLE_SIZE
)
2877 size
= mem
->bo
->size
- offset
;
2879 /* From the Vulkan spec version 1.0.32 docs for MapMemory:
2881 * * If size is not equal to VK_WHOLE_SIZE, size must be greater than 0
2882 * assert(size != 0);
2883 * * If size is not equal to VK_WHOLE_SIZE, size must be less than or
2884 * equal to the size of the memory minus offset
2887 assert(offset
+ size
<= mem
->bo
->size
);
2889 /* FIXME: Is this supposed to be thread safe? Since vkUnmapMemory() only
2890 * takes a VkDeviceMemory pointer, it seems like only one map of the memory
2891 * at a time is valid. We could just mmap up front and return an offset
2892 * pointer here, but that may exhaust virtual memory on 32 bit
2895 uint32_t gem_flags
= 0;
2897 if (!device
->info
.has_llc
&&
2898 (mem
->type
->propertyFlags
& VK_MEMORY_PROPERTY_HOST_COHERENT_BIT
))
2899 gem_flags
|= I915_MMAP_WC
;
2901 /* GEM will fail to map if the offset isn't 4k-aligned. Round down. */
2902 uint64_t map_offset
= offset
& ~4095ull;
2903 assert(offset
>= map_offset
);
2904 uint64_t map_size
= (offset
+ size
) - map_offset
;
2906 /* Let's map whole pages */
2907 map_size
= align_u64(map_size
, 4096);
2909 void *map
= anv_gem_mmap(device
, mem
->bo
->gem_handle
,
2910 map_offset
, map_size
, gem_flags
);
2911 if (map
== MAP_FAILED
)
2912 return vk_error(VK_ERROR_MEMORY_MAP_FAILED
);
2915 mem
->map_size
= map_size
;
2917 *ppData
= mem
->map
+ (offset
- map_offset
);
2922 void anv_UnmapMemory(
2924 VkDeviceMemory _memory
)
2926 ANV_FROM_HANDLE(anv_device_memory
, mem
, _memory
);
2928 if (mem
== NULL
|| mem
->host_ptr
)
2931 anv_gem_munmap(mem
->map
, mem
->map_size
);
2938 clflush_mapped_ranges(struct anv_device
*device
,
2940 const VkMappedMemoryRange
*ranges
)
2942 for (uint32_t i
= 0; i
< count
; i
++) {
2943 ANV_FROM_HANDLE(anv_device_memory
, mem
, ranges
[i
].memory
);
2944 if (ranges
[i
].offset
>= mem
->map_size
)
2947 gen_clflush_range(mem
->map
+ ranges
[i
].offset
,
2948 MIN2(ranges
[i
].size
, mem
->map_size
- ranges
[i
].offset
));
2952 VkResult
anv_FlushMappedMemoryRanges(
2954 uint32_t memoryRangeCount
,
2955 const VkMappedMemoryRange
* pMemoryRanges
)
2957 ANV_FROM_HANDLE(anv_device
, device
, _device
);
2959 if (device
->info
.has_llc
)
2962 /* Make sure the writes we're flushing have landed. */
2963 __builtin_ia32_mfence();
2965 clflush_mapped_ranges(device
, memoryRangeCount
, pMemoryRanges
);
2970 VkResult
anv_InvalidateMappedMemoryRanges(
2972 uint32_t memoryRangeCount
,
2973 const VkMappedMemoryRange
* pMemoryRanges
)
2975 ANV_FROM_HANDLE(anv_device
, device
, _device
);
2977 if (device
->info
.has_llc
)
2980 clflush_mapped_ranges(device
, memoryRangeCount
, pMemoryRanges
);
2982 /* Make sure no reads get moved up above the invalidate. */
2983 __builtin_ia32_mfence();
2988 void anv_GetBufferMemoryRequirements(
2991 VkMemoryRequirements
* pMemoryRequirements
)
2993 ANV_FROM_HANDLE(anv_buffer
, buffer
, _buffer
);
2994 ANV_FROM_HANDLE(anv_device
, device
, _device
);
2995 struct anv_physical_device
*pdevice
= &device
->instance
->physicalDevice
;
2997 /* The Vulkan spec (git aaed022) says:
2999 * memoryTypeBits is a bitfield and contains one bit set for every
3000 * supported memory type for the resource. The bit `1<<i` is set if and
3001 * only if the memory type `i` in the VkPhysicalDeviceMemoryProperties
3002 * structure for the physical device is supported.
3004 uint32_t memory_types
= 0;
3005 for (uint32_t i
= 0; i
< pdevice
->memory
.type_count
; i
++) {
3006 uint32_t valid_usage
= pdevice
->memory
.types
[i
].valid_buffer_usage
;
3007 if ((valid_usage
& buffer
->usage
) == buffer
->usage
)
3008 memory_types
|= (1u << i
);
3011 /* Base alignment requirement of a cache line */
3012 uint32_t alignment
= 16;
3014 /* We need an alignment of 32 for pushing UBOs */
3015 if (buffer
->usage
& VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT
)
3016 alignment
= MAX2(alignment
, 32);
3018 pMemoryRequirements
->size
= buffer
->size
;
3019 pMemoryRequirements
->alignment
= alignment
;
3021 /* Storage and Uniform buffers should have their size aligned to
3022 * 32-bits to avoid boundary checks when last DWord is not complete.
3023 * This would ensure that not internal padding would be needed for
3026 if (device
->robust_buffer_access
&&
3027 (buffer
->usage
& VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT
||
3028 buffer
->usage
& VK_BUFFER_USAGE_STORAGE_BUFFER_BIT
))
3029 pMemoryRequirements
->size
= align_u64(buffer
->size
, 4);
3031 pMemoryRequirements
->memoryTypeBits
= memory_types
;
3034 void anv_GetBufferMemoryRequirements2(
3036 const VkBufferMemoryRequirementsInfo2
* pInfo
,
3037 VkMemoryRequirements2
* pMemoryRequirements
)
3039 anv_GetBufferMemoryRequirements(_device
, pInfo
->buffer
,
3040 &pMemoryRequirements
->memoryRequirements
);
3042 vk_foreach_struct(ext
, pMemoryRequirements
->pNext
) {
3043 switch (ext
->sType
) {
3044 case VK_STRUCTURE_TYPE_MEMORY_DEDICATED_REQUIREMENTS
: {
3045 VkMemoryDedicatedRequirements
*requirements
= (void *)ext
;
3046 requirements
->prefersDedicatedAllocation
= false;
3047 requirements
->requiresDedicatedAllocation
= false;
3052 anv_debug_ignored_stype(ext
->sType
);
3058 void anv_GetImageMemoryRequirements(
3061 VkMemoryRequirements
* pMemoryRequirements
)
3063 ANV_FROM_HANDLE(anv_image
, image
, _image
);
3064 ANV_FROM_HANDLE(anv_device
, device
, _device
);
3065 struct anv_physical_device
*pdevice
= &device
->instance
->physicalDevice
;
3067 /* The Vulkan spec (git aaed022) says:
3069 * memoryTypeBits is a bitfield and contains one bit set for every
3070 * supported memory type for the resource. The bit `1<<i` is set if and
3071 * only if the memory type `i` in the VkPhysicalDeviceMemoryProperties
3072 * structure for the physical device is supported.
3074 * All types are currently supported for images.
3076 uint32_t memory_types
= (1ull << pdevice
->memory
.type_count
) - 1;
3078 /* We must have image allocated or imported at this point. According to the
3079 * specification, external images must have been bound to memory before
3080 * calling GetImageMemoryRequirements.
3082 assert(image
->size
> 0);
3084 pMemoryRequirements
->size
= image
->size
;
3085 pMemoryRequirements
->alignment
= image
->alignment
;
3086 pMemoryRequirements
->memoryTypeBits
= memory_types
;
3089 void anv_GetImageMemoryRequirements2(
3091 const VkImageMemoryRequirementsInfo2
* pInfo
,
3092 VkMemoryRequirements2
* pMemoryRequirements
)
3094 ANV_FROM_HANDLE(anv_device
, device
, _device
);
3095 ANV_FROM_HANDLE(anv_image
, image
, pInfo
->image
);
3097 anv_GetImageMemoryRequirements(_device
, pInfo
->image
,
3098 &pMemoryRequirements
->memoryRequirements
);
3100 vk_foreach_struct_const(ext
, pInfo
->pNext
) {
3101 switch (ext
->sType
) {
3102 case VK_STRUCTURE_TYPE_IMAGE_PLANE_MEMORY_REQUIREMENTS_INFO
: {
3103 struct anv_physical_device
*pdevice
= &device
->instance
->physicalDevice
;
3104 const VkImagePlaneMemoryRequirementsInfo
*plane_reqs
=
3105 (const VkImagePlaneMemoryRequirementsInfo
*) ext
;
3106 uint32_t plane
= anv_image_aspect_to_plane(image
->aspects
,
3107 plane_reqs
->planeAspect
);
3109 assert(image
->planes
[plane
].offset
== 0);
3111 /* The Vulkan spec (git aaed022) says:
3113 * memoryTypeBits is a bitfield and contains one bit set for every
3114 * supported memory type for the resource. The bit `1<<i` is set
3115 * if and only if the memory type `i` in the
3116 * VkPhysicalDeviceMemoryProperties structure for the physical
3117 * device is supported.
3119 * All types are currently supported for images.
3121 pMemoryRequirements
->memoryRequirements
.memoryTypeBits
=
3122 (1ull << pdevice
->memory
.type_count
) - 1;
3124 /* We must have image allocated or imported at this point. According to the
3125 * specification, external images must have been bound to memory before
3126 * calling GetImageMemoryRequirements.
3128 assert(image
->planes
[plane
].size
> 0);
3130 pMemoryRequirements
->memoryRequirements
.size
= image
->planes
[plane
].size
;
3131 pMemoryRequirements
->memoryRequirements
.alignment
=
3132 image
->planes
[plane
].alignment
;
3137 anv_debug_ignored_stype(ext
->sType
);
3142 vk_foreach_struct(ext
, pMemoryRequirements
->pNext
) {
3143 switch (ext
->sType
) {
3144 case VK_STRUCTURE_TYPE_MEMORY_DEDICATED_REQUIREMENTS
: {
3145 VkMemoryDedicatedRequirements
*requirements
= (void *)ext
;
3146 if (image
->needs_set_tiling
|| image
->external_format
) {
3147 /* If we need to set the tiling for external consumers, we need a
3148 * dedicated allocation.
3150 * See also anv_AllocateMemory.
3152 requirements
->prefersDedicatedAllocation
= true;
3153 requirements
->requiresDedicatedAllocation
= true;
3155 requirements
->prefersDedicatedAllocation
= false;
3156 requirements
->requiresDedicatedAllocation
= false;
3162 anv_debug_ignored_stype(ext
->sType
);
3168 void anv_GetImageSparseMemoryRequirements(
3171 uint32_t* pSparseMemoryRequirementCount
,
3172 VkSparseImageMemoryRequirements
* pSparseMemoryRequirements
)
3174 *pSparseMemoryRequirementCount
= 0;
3177 void anv_GetImageSparseMemoryRequirements2(
3179 const VkImageSparseMemoryRequirementsInfo2
* pInfo
,
3180 uint32_t* pSparseMemoryRequirementCount
,
3181 VkSparseImageMemoryRequirements2
* pSparseMemoryRequirements
)
3183 *pSparseMemoryRequirementCount
= 0;
3186 void anv_GetDeviceMemoryCommitment(
3188 VkDeviceMemory memory
,
3189 VkDeviceSize
* pCommittedMemoryInBytes
)
3191 *pCommittedMemoryInBytes
= 0;
3195 anv_bind_buffer_memory(const VkBindBufferMemoryInfo
*pBindInfo
)
3197 ANV_FROM_HANDLE(anv_device_memory
, mem
, pBindInfo
->memory
);
3198 ANV_FROM_HANDLE(anv_buffer
, buffer
, pBindInfo
->buffer
);
3200 assert(pBindInfo
->sType
== VK_STRUCTURE_TYPE_BIND_BUFFER_MEMORY_INFO
);
3203 assert((buffer
->usage
& mem
->type
->valid_buffer_usage
) == buffer
->usage
);
3204 buffer
->address
= (struct anv_address
) {
3206 .offset
= pBindInfo
->memoryOffset
,
3209 buffer
->address
= ANV_NULL_ADDRESS
;
3213 VkResult
anv_BindBufferMemory(
3216 VkDeviceMemory memory
,
3217 VkDeviceSize memoryOffset
)
3219 anv_bind_buffer_memory(
3220 &(VkBindBufferMemoryInfo
) {
3221 .sType
= VK_STRUCTURE_TYPE_BIND_BUFFER_MEMORY_INFO
,
3224 .memoryOffset
= memoryOffset
,
3230 VkResult
anv_BindBufferMemory2(
3232 uint32_t bindInfoCount
,
3233 const VkBindBufferMemoryInfo
* pBindInfos
)
3235 for (uint32_t i
= 0; i
< bindInfoCount
; i
++)
3236 anv_bind_buffer_memory(&pBindInfos
[i
]);
3241 VkResult
anv_QueueBindSparse(
3243 uint32_t bindInfoCount
,
3244 const VkBindSparseInfo
* pBindInfo
,
3247 ANV_FROM_HANDLE(anv_queue
, queue
, _queue
);
3248 if (anv_device_is_lost(queue
->device
))
3249 return VK_ERROR_DEVICE_LOST
;
3251 return vk_error(VK_ERROR_FEATURE_NOT_PRESENT
);
3256 VkResult
anv_CreateEvent(
3258 const VkEventCreateInfo
* pCreateInfo
,
3259 const VkAllocationCallbacks
* pAllocator
,
3262 ANV_FROM_HANDLE(anv_device
, device
, _device
);
3263 struct anv_state state
;
3264 struct anv_event
*event
;
3266 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_EVENT_CREATE_INFO
);
3268 state
= anv_state_pool_alloc(&device
->dynamic_state_pool
,
3271 event
->state
= state
;
3272 event
->semaphore
= VK_EVENT_RESET
;
3274 if (!device
->info
.has_llc
) {
3275 /* Make sure the writes we're flushing have landed. */
3276 __builtin_ia32_mfence();
3277 __builtin_ia32_clflush(event
);
3280 *pEvent
= anv_event_to_handle(event
);
3285 void anv_DestroyEvent(
3288 const VkAllocationCallbacks
* pAllocator
)
3290 ANV_FROM_HANDLE(anv_device
, device
, _device
);
3291 ANV_FROM_HANDLE(anv_event
, event
, _event
);
3296 anv_state_pool_free(&device
->dynamic_state_pool
, event
->state
);
3299 VkResult
anv_GetEventStatus(
3303 ANV_FROM_HANDLE(anv_device
, device
, _device
);
3304 ANV_FROM_HANDLE(anv_event
, event
, _event
);
3306 if (anv_device_is_lost(device
))
3307 return VK_ERROR_DEVICE_LOST
;
3309 if (!device
->info
.has_llc
) {
3310 /* Invalidate read cache before reading event written by GPU. */
3311 __builtin_ia32_clflush(event
);
3312 __builtin_ia32_mfence();
3316 return event
->semaphore
;
3319 VkResult
anv_SetEvent(
3323 ANV_FROM_HANDLE(anv_device
, device
, _device
);
3324 ANV_FROM_HANDLE(anv_event
, event
, _event
);
3326 event
->semaphore
= VK_EVENT_SET
;
3328 if (!device
->info
.has_llc
) {
3329 /* Make sure the writes we're flushing have landed. */
3330 __builtin_ia32_mfence();
3331 __builtin_ia32_clflush(event
);
3337 VkResult
anv_ResetEvent(
3341 ANV_FROM_HANDLE(anv_device
, device
, _device
);
3342 ANV_FROM_HANDLE(anv_event
, event
, _event
);
3344 event
->semaphore
= VK_EVENT_RESET
;
3346 if (!device
->info
.has_llc
) {
3347 /* Make sure the writes we're flushing have landed. */
3348 __builtin_ia32_mfence();
3349 __builtin_ia32_clflush(event
);
3357 VkResult
anv_CreateBuffer(
3359 const VkBufferCreateInfo
* pCreateInfo
,
3360 const VkAllocationCallbacks
* pAllocator
,
3363 ANV_FROM_HANDLE(anv_device
, device
, _device
);
3364 struct anv_buffer
*buffer
;
3366 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO
);
3368 buffer
= vk_alloc2(&device
->alloc
, pAllocator
, sizeof(*buffer
), 8,
3369 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
3371 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
3373 buffer
->size
= pCreateInfo
->size
;
3374 buffer
->usage
= pCreateInfo
->usage
;
3375 buffer
->address
= ANV_NULL_ADDRESS
;
3377 *pBuffer
= anv_buffer_to_handle(buffer
);
3382 void anv_DestroyBuffer(
3385 const VkAllocationCallbacks
* pAllocator
)
3387 ANV_FROM_HANDLE(anv_device
, device
, _device
);
3388 ANV_FROM_HANDLE(anv_buffer
, buffer
, _buffer
);
3393 vk_free2(&device
->alloc
, pAllocator
, buffer
);
3396 VkDeviceAddress
anv_GetBufferDeviceAddressEXT(
3398 const VkBufferDeviceAddressInfoEXT
* pInfo
)
3400 ANV_FROM_HANDLE(anv_buffer
, buffer
, pInfo
->buffer
);
3402 assert(buffer
->address
.bo
->flags
& EXEC_OBJECT_PINNED
);
3404 return anv_address_physical(buffer
->address
);
3408 anv_fill_buffer_surface_state(struct anv_device
*device
, struct anv_state state
,
3409 enum isl_format format
,
3410 struct anv_address address
,
3411 uint32_t range
, uint32_t stride
)
3413 isl_buffer_fill_state(&device
->isl_dev
, state
.map
,
3414 .address
= anv_address_physical(address
),
3415 .mocs
= device
->default_mocs
,
3418 .swizzle
= ISL_SWIZZLE_IDENTITY
,
3419 .stride_B
= stride
);
3422 void anv_DestroySampler(
3425 const VkAllocationCallbacks
* pAllocator
)
3427 ANV_FROM_HANDLE(anv_device
, device
, _device
);
3428 ANV_FROM_HANDLE(anv_sampler
, sampler
, _sampler
);
3433 if (sampler
->bindless_state
.map
) {
3434 anv_state_pool_free(&device
->dynamic_state_pool
,
3435 sampler
->bindless_state
);
3438 vk_free2(&device
->alloc
, pAllocator
, sampler
);
3441 VkResult
anv_CreateFramebuffer(
3443 const VkFramebufferCreateInfo
* pCreateInfo
,
3444 const VkAllocationCallbacks
* pAllocator
,
3445 VkFramebuffer
* pFramebuffer
)
3447 ANV_FROM_HANDLE(anv_device
, device
, _device
);
3448 struct anv_framebuffer
*framebuffer
;
3450 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO
);
3452 size_t size
= sizeof(*framebuffer
) +
3453 sizeof(struct anv_image_view
*) * pCreateInfo
->attachmentCount
;
3454 framebuffer
= vk_alloc2(&device
->alloc
, pAllocator
, size
, 8,
3455 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
3456 if (framebuffer
== NULL
)
3457 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
3459 framebuffer
->attachment_count
= pCreateInfo
->attachmentCount
;
3460 for (uint32_t i
= 0; i
< pCreateInfo
->attachmentCount
; i
++) {
3461 VkImageView _iview
= pCreateInfo
->pAttachments
[i
];
3462 framebuffer
->attachments
[i
] = anv_image_view_from_handle(_iview
);
3465 framebuffer
->width
= pCreateInfo
->width
;
3466 framebuffer
->height
= pCreateInfo
->height
;
3467 framebuffer
->layers
= pCreateInfo
->layers
;
3469 *pFramebuffer
= anv_framebuffer_to_handle(framebuffer
);
3474 void anv_DestroyFramebuffer(
3477 const VkAllocationCallbacks
* pAllocator
)
3479 ANV_FROM_HANDLE(anv_device
, device
, _device
);
3480 ANV_FROM_HANDLE(anv_framebuffer
, fb
, _fb
);
3485 vk_free2(&device
->alloc
, pAllocator
, fb
);
3488 static const VkTimeDomainEXT anv_time_domains
[] = {
3489 VK_TIME_DOMAIN_DEVICE_EXT
,
3490 VK_TIME_DOMAIN_CLOCK_MONOTONIC_EXT
,
3491 VK_TIME_DOMAIN_CLOCK_MONOTONIC_RAW_EXT
,
3494 VkResult
anv_GetPhysicalDeviceCalibrateableTimeDomainsEXT(
3495 VkPhysicalDevice physicalDevice
,
3496 uint32_t *pTimeDomainCount
,
3497 VkTimeDomainEXT
*pTimeDomains
)
3500 VK_OUTARRAY_MAKE(out
, pTimeDomains
, pTimeDomainCount
);
3502 for (d
= 0; d
< ARRAY_SIZE(anv_time_domains
); d
++) {
3503 vk_outarray_append(&out
, i
) {
3504 *i
= anv_time_domains
[d
];
3508 return vk_outarray_status(&out
);
3512 anv_clock_gettime(clockid_t clock_id
)
3514 struct timespec current
;
3517 ret
= clock_gettime(clock_id
, ¤t
);
3518 if (ret
< 0 && clock_id
== CLOCK_MONOTONIC_RAW
)
3519 ret
= clock_gettime(CLOCK_MONOTONIC
, ¤t
);
3523 return (uint64_t) current
.tv_sec
* 1000000000ULL + current
.tv_nsec
;
3526 #define TIMESTAMP 0x2358
3528 VkResult
anv_GetCalibratedTimestampsEXT(
3530 uint32_t timestampCount
,
3531 const VkCalibratedTimestampInfoEXT
*pTimestampInfos
,
3532 uint64_t *pTimestamps
,
3533 uint64_t *pMaxDeviation
)
3535 ANV_FROM_HANDLE(anv_device
, device
, _device
);
3536 uint64_t timestamp_frequency
= device
->info
.timestamp_frequency
;
3539 uint64_t begin
, end
;
3540 uint64_t max_clock_period
= 0;
3542 begin
= anv_clock_gettime(CLOCK_MONOTONIC_RAW
);
3544 for (d
= 0; d
< timestampCount
; d
++) {
3545 switch (pTimestampInfos
[d
].timeDomain
) {
3546 case VK_TIME_DOMAIN_DEVICE_EXT
:
3547 ret
= anv_gem_reg_read(device
, TIMESTAMP
| 1,
3551 return anv_device_set_lost(device
, "Failed to read the TIMESTAMP "
3554 uint64_t device_period
= DIV_ROUND_UP(1000000000, timestamp_frequency
);
3555 max_clock_period
= MAX2(max_clock_period
, device_period
);
3557 case VK_TIME_DOMAIN_CLOCK_MONOTONIC_EXT
:
3558 pTimestamps
[d
] = anv_clock_gettime(CLOCK_MONOTONIC
);
3559 max_clock_period
= MAX2(max_clock_period
, 1);
3562 case VK_TIME_DOMAIN_CLOCK_MONOTONIC_RAW_EXT
:
3563 pTimestamps
[d
] = begin
;
3571 end
= anv_clock_gettime(CLOCK_MONOTONIC_RAW
);
3574 * The maximum deviation is the sum of the interval over which we
3575 * perform the sampling and the maximum period of any sampled
3576 * clock. That's because the maximum skew between any two sampled
3577 * clock edges is when the sampled clock with the largest period is
3578 * sampled at the end of that period but right at the beginning of the
3579 * sampling interval and some other clock is sampled right at the
3580 * begining of its sampling period and right at the end of the
3581 * sampling interval. Let's assume the GPU has the longest clock
3582 * period and that the application is sampling GPU and monotonic:
3585 * w x y z 0 1 2 3 4 5 6 7 8 9 a b c d e f
3586 * Raw -_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-
3590 * GPU -----_____-----_____-----_____-----_____
3593 * x y z 0 1 2 3 4 5 6 7 8 9 a b c
3594 * Monotonic -_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-
3596 * Interval <----------------->
3597 * Deviation <-------------------------->
3601 * m = read(monotonic) 2
3604 * We round the sample interval up by one tick to cover sampling error
3605 * in the interval clock
3608 uint64_t sample_interval
= end
- begin
+ 1;
3610 *pMaxDeviation
= sample_interval
+ max_clock_period
;
3615 /* vk_icd.h does not declare this function, so we declare it here to
3616 * suppress Wmissing-prototypes.
3618 PUBLIC VKAPI_ATTR VkResult VKAPI_CALL
3619 vk_icdNegotiateLoaderICDInterfaceVersion(uint32_t* pSupportedVersion
);
3621 PUBLIC VKAPI_ATTR VkResult VKAPI_CALL
3622 vk_icdNegotiateLoaderICDInterfaceVersion(uint32_t* pSupportedVersion
)
3624 /* For the full details on loader interface versioning, see
3625 * <https://github.com/KhronosGroup/Vulkan-LoaderAndValidationLayers/blob/master/loader/LoaderAndLayerInterface.md>.
3626 * What follows is a condensed summary, to help you navigate the large and
3627 * confusing official doc.
3629 * - Loader interface v0 is incompatible with later versions. We don't
3632 * - In loader interface v1:
3633 * - The first ICD entrypoint called by the loader is
3634 * vk_icdGetInstanceProcAddr(). The ICD must statically expose this
3636 * - The ICD must statically expose no other Vulkan symbol unless it is
3637 * linked with -Bsymbolic.
3638 * - Each dispatchable Vulkan handle created by the ICD must be
3639 * a pointer to a struct whose first member is VK_LOADER_DATA. The
3640 * ICD must initialize VK_LOADER_DATA.loadMagic to ICD_LOADER_MAGIC.
3641 * - The loader implements vkCreate{PLATFORM}SurfaceKHR() and
3642 * vkDestroySurfaceKHR(). The ICD must be capable of working with
3643 * such loader-managed surfaces.
3645 * - Loader interface v2 differs from v1 in:
3646 * - The first ICD entrypoint called by the loader is
3647 * vk_icdNegotiateLoaderICDInterfaceVersion(). The ICD must
3648 * statically expose this entrypoint.
3650 * - Loader interface v3 differs from v2 in:
3651 * - The ICD must implement vkCreate{PLATFORM}SurfaceKHR(),
3652 * vkDestroySurfaceKHR(), and other API which uses VKSurfaceKHR,
3653 * because the loader no longer does so.
3655 *pSupportedVersion
= MIN2(*pSupportedVersion
, 3u);