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"
45 #include "genxml/gen7_pack.h"
47 /* This is probably far to big but it reflects the max size used for messages
48 * in OpenGLs KHR_debug.
50 #define MAX_DEBUG_MESSAGE_LENGTH 4096
53 compiler_debug_log(void *data
, const char *fmt
, ...)
55 char str
[MAX_DEBUG_MESSAGE_LENGTH
];
56 struct anv_device
*device
= (struct anv_device
*)data
;
58 if (list_empty(&device
->instance
->debug_report_callbacks
.callbacks
))
63 (void) vsnprintf(str
, MAX_DEBUG_MESSAGE_LENGTH
, fmt
, args
);
66 vk_debug_report(&device
->instance
->debug_report_callbacks
,
67 VK_DEBUG_REPORT_DEBUG_BIT_EXT
,
68 VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT
,
73 compiler_perf_log(void *data
, const char *fmt
, ...)
78 if (unlikely(INTEL_DEBUG
& DEBUG_PERF
))
79 intel_logd_v(fmt
, args
);
85 anv_compute_heap_size(int fd
, uint64_t gtt_size
)
87 /* Query the total ram from the system */
91 uint64_t total_ram
= (uint64_t)info
.totalram
* (uint64_t)info
.mem_unit
;
93 /* We don't want to burn too much ram with the GPU. If the user has 4GiB
94 * or less, we use at most half. If they have more than 4GiB, we use 3/4.
96 uint64_t available_ram
;
97 if (total_ram
<= 4ull * 1024ull * 1024ull * 1024ull)
98 available_ram
= total_ram
/ 2;
100 available_ram
= total_ram
* 3 / 4;
102 /* We also want to leave some padding for things we allocate in the driver,
103 * so don't go over 3/4 of the GTT either.
105 uint64_t available_gtt
= gtt_size
* 3 / 4;
107 return MIN2(available_ram
, available_gtt
);
111 anv_physical_device_init_heaps(struct anv_physical_device
*device
, int fd
)
114 if (anv_gem_get_context_param(fd
, 0, I915_CONTEXT_PARAM_GTT_SIZE
,
116 /* If, for whatever reason, we can't actually get the GTT size from the
117 * kernel (too old?) fall back to the aperture size.
119 anv_perf_warn(NULL
, NULL
,
120 "Failed to get I915_CONTEXT_PARAM_GTT_SIZE: %m");
122 if (anv_gem_get_aperture(fd
, >t_size
) == -1) {
123 return vk_errorf(NULL
, NULL
, VK_ERROR_INITIALIZATION_FAILED
,
124 "failed to get aperture size: %m");
128 device
->supports_48bit_addresses
= (device
->info
.gen
>= 8) &&
129 gtt_size
> (4ULL << 30 /* GiB */);
131 uint64_t heap_size
= anv_compute_heap_size(fd
, gtt_size
);
133 if (heap_size
> (2ull << 30) && !device
->supports_48bit_addresses
) {
134 /* When running with an overridden PCI ID, we may get a GTT size from
135 * the kernel that is greater than 2 GiB but the execbuf check for 48bit
136 * address support can still fail. Just clamp the address space size to
137 * 2 GiB if we don't have 48-bit support.
139 intel_logw("%s:%d: The kernel reported a GTT size larger than 2 GiB but "
140 "not support for 48-bit addresses",
142 heap_size
= 2ull << 30;
145 if (heap_size
<= 3ull * (1ull << 30)) {
146 /* In this case, everything fits nicely into the 32-bit address space,
147 * so there's no need for supporting 48bit addresses on client-allocated
150 device
->memory
.heap_count
= 1;
151 device
->memory
.heaps
[0] = (struct anv_memory_heap
) {
152 .vma_start
= LOW_HEAP_MIN_ADDRESS
,
153 .vma_size
= LOW_HEAP_SIZE
,
155 .flags
= VK_MEMORY_HEAP_DEVICE_LOCAL_BIT
,
156 .supports_48bit_addresses
= false,
159 /* Not everything will fit nicely into a 32-bit address space. In this
160 * case we need a 64-bit heap. Advertise a small 32-bit heap and a
161 * larger 48-bit heap. If we're in this case, then we have a total heap
162 * size larger than 3GiB which most likely means they have 8 GiB of
163 * video memory and so carving off 1 GiB for the 32-bit heap should be
166 const uint64_t heap_size_32bit
= 1ull << 30;
167 const uint64_t heap_size_48bit
= heap_size
- heap_size_32bit
;
169 assert(device
->supports_48bit_addresses
);
171 device
->memory
.heap_count
= 2;
172 device
->memory
.heaps
[0] = (struct anv_memory_heap
) {
173 .vma_start
= HIGH_HEAP_MIN_ADDRESS
,
174 .vma_size
= gtt_size
- HIGH_HEAP_MIN_ADDRESS
,
175 .size
= heap_size_48bit
,
176 .flags
= VK_MEMORY_HEAP_DEVICE_LOCAL_BIT
,
177 .supports_48bit_addresses
= true,
179 device
->memory
.heaps
[1] = (struct anv_memory_heap
) {
180 .vma_start
= LOW_HEAP_MIN_ADDRESS
,
181 .vma_size
= LOW_HEAP_SIZE
,
182 .size
= heap_size_32bit
,
183 .flags
= VK_MEMORY_HEAP_DEVICE_LOCAL_BIT
,
184 .supports_48bit_addresses
= false,
188 uint32_t type_count
= 0;
189 for (uint32_t heap
= 0; heap
< device
->memory
.heap_count
; heap
++) {
190 uint32_t valid_buffer_usage
= ~0;
192 /* There appears to be a hardware issue in the VF cache where it only
193 * considers the bottom 32 bits of memory addresses. If you happen to
194 * have two vertex buffers which get placed exactly 4 GiB apart and use
195 * them in back-to-back draw calls, you can get collisions. In order to
196 * solve this problem, we require vertex and index buffers be bound to
197 * memory allocated out of the 32-bit heap.
199 if (device
->memory
.heaps
[heap
].supports_48bit_addresses
) {
200 valid_buffer_usage
&= ~(VK_BUFFER_USAGE_INDEX_BUFFER_BIT
|
201 VK_BUFFER_USAGE_VERTEX_BUFFER_BIT
);
204 if (device
->info
.has_llc
) {
205 /* Big core GPUs share LLC with the CPU and thus one memory type can be
206 * both cached and coherent at the same time.
208 device
->memory
.types
[type_count
++] = (struct anv_memory_type
) {
209 .propertyFlags
= VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT
|
210 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT
|
211 VK_MEMORY_PROPERTY_HOST_COHERENT_BIT
|
212 VK_MEMORY_PROPERTY_HOST_CACHED_BIT
,
214 .valid_buffer_usage
= valid_buffer_usage
,
217 /* The spec requires that we expose a host-visible, coherent memory
218 * type, but Atom GPUs don't share LLC. Thus we offer two memory types
219 * to give the application a choice between cached, but not coherent and
220 * coherent but uncached (WC though).
222 device
->memory
.types
[type_count
++] = (struct anv_memory_type
) {
223 .propertyFlags
= VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT
|
224 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT
|
225 VK_MEMORY_PROPERTY_HOST_COHERENT_BIT
,
227 .valid_buffer_usage
= valid_buffer_usage
,
229 device
->memory
.types
[type_count
++] = (struct anv_memory_type
) {
230 .propertyFlags
= VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT
|
231 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT
|
232 VK_MEMORY_PROPERTY_HOST_CACHED_BIT
,
234 .valid_buffer_usage
= valid_buffer_usage
,
238 device
->memory
.type_count
= type_count
;
244 anv_physical_device_init_uuids(struct anv_physical_device
*device
)
246 const struct build_id_note
*note
=
247 build_id_find_nhdr_for_addr(anv_physical_device_init_uuids
);
249 return vk_errorf(device
->instance
, device
,
250 VK_ERROR_INITIALIZATION_FAILED
,
251 "Failed to find build-id");
254 unsigned build_id_len
= build_id_length(note
);
255 if (build_id_len
< 20) {
256 return vk_errorf(device
->instance
, device
,
257 VK_ERROR_INITIALIZATION_FAILED
,
258 "build-id too short. It needs to be a SHA");
261 memcpy(device
->driver_build_sha1
, build_id_data(note
), 20);
263 struct mesa_sha1 sha1_ctx
;
265 STATIC_ASSERT(VK_UUID_SIZE
<= sizeof(sha1
));
267 /* The pipeline cache UUID is used for determining when a pipeline cache is
268 * invalid. It needs both a driver build and the PCI ID of the device.
270 _mesa_sha1_init(&sha1_ctx
);
271 _mesa_sha1_update(&sha1_ctx
, build_id_data(note
), build_id_len
);
272 _mesa_sha1_update(&sha1_ctx
, &device
->chipset_id
,
273 sizeof(device
->chipset_id
));
274 _mesa_sha1_final(&sha1_ctx
, sha1
);
275 memcpy(device
->pipeline_cache_uuid
, sha1
, VK_UUID_SIZE
);
277 /* The driver UUID is used for determining sharability of images and memory
278 * between two Vulkan instances in separate processes. People who want to
279 * share memory need to also check the device UUID (below) so all this
280 * needs to be is the build-id.
282 memcpy(device
->driver_uuid
, build_id_data(note
), VK_UUID_SIZE
);
284 /* The device UUID uniquely identifies the given device within the machine.
285 * Since we never have more than one device, this doesn't need to be a real
286 * UUID. However, on the off-chance that someone tries to use this to
287 * cache pre-tiled images or something of the like, we use the PCI ID and
288 * some bits of ISL info to ensure that this is safe.
290 _mesa_sha1_init(&sha1_ctx
);
291 _mesa_sha1_update(&sha1_ctx
, &device
->chipset_id
,
292 sizeof(device
->chipset_id
));
293 _mesa_sha1_update(&sha1_ctx
, &device
->isl_dev
.has_bit6_swizzling
,
294 sizeof(device
->isl_dev
.has_bit6_swizzling
));
295 _mesa_sha1_final(&sha1_ctx
, sha1
);
296 memcpy(device
->device_uuid
, sha1
, VK_UUID_SIZE
);
302 anv_physical_device_init_disk_cache(struct anv_physical_device
*device
)
304 #ifdef ENABLE_SHADER_CACHE
306 MAYBE_UNUSED
int len
= snprintf(renderer
, sizeof(renderer
), "anv_%04x",
308 assert(len
== sizeof(renderer
) - 2);
311 _mesa_sha1_format(timestamp
, device
->driver_build_sha1
);
313 const uint64_t driver_flags
=
314 brw_get_compiler_config_value(device
->compiler
);
315 device
->disk_cache
= disk_cache_create(renderer
, timestamp
, driver_flags
);
317 device
->disk_cache
= NULL
;
322 anv_physical_device_free_disk_cache(struct anv_physical_device
*device
)
324 #ifdef ENABLE_SHADER_CACHE
325 if (device
->disk_cache
)
326 disk_cache_destroy(device
->disk_cache
);
328 assert(device
->disk_cache
== NULL
);
333 anv_physical_device_init(struct anv_physical_device
*device
,
334 struct anv_instance
*instance
,
335 drmDevicePtr drm_device
)
337 const char *primary_path
= drm_device
->nodes
[DRM_NODE_PRIMARY
];
338 const char *path
= drm_device
->nodes
[DRM_NODE_RENDER
];
343 brw_process_intel_debug_variable();
345 fd
= open(path
, O_RDWR
| O_CLOEXEC
);
347 return vk_error(VK_ERROR_INCOMPATIBLE_DRIVER
);
349 device
->_loader_data
.loaderMagic
= ICD_LOADER_MAGIC
;
350 device
->instance
= instance
;
352 assert(strlen(path
) < ARRAY_SIZE(device
->path
));
353 snprintf(device
->path
, ARRAY_SIZE(device
->path
), "%s", path
);
355 device
->no_hw
= getenv("INTEL_NO_HW") != NULL
;
357 const int pci_id_override
= gen_get_pci_device_id_override();
358 if (pci_id_override
< 0) {
359 device
->chipset_id
= anv_gem_get_param(fd
, I915_PARAM_CHIPSET_ID
);
360 if (!device
->chipset_id
) {
361 result
= vk_error(VK_ERROR_INCOMPATIBLE_DRIVER
);
365 device
->chipset_id
= pci_id_override
;
366 device
->no_hw
= true;
369 device
->pci_info
.domain
= drm_device
->businfo
.pci
->domain
;
370 device
->pci_info
.bus
= drm_device
->businfo
.pci
->bus
;
371 device
->pci_info
.device
= drm_device
->businfo
.pci
->dev
;
372 device
->pci_info
.function
= drm_device
->businfo
.pci
->func
;
374 device
->name
= gen_get_device_name(device
->chipset_id
);
375 if (!gen_get_device_info(device
->chipset_id
, &device
->info
)) {
376 result
= vk_error(VK_ERROR_INCOMPATIBLE_DRIVER
);
380 if (device
->info
.is_haswell
) {
381 intel_logw("Haswell Vulkan support is incomplete");
382 } else if (device
->info
.gen
== 7 && !device
->info
.is_baytrail
) {
383 intel_logw("Ivy Bridge Vulkan support is incomplete");
384 } else if (device
->info
.gen
== 7 && device
->info
.is_baytrail
) {
385 intel_logw("Bay Trail Vulkan support is incomplete");
386 } else if (device
->info
.gen
>= 8 && device
->info
.gen
<= 10) {
387 /* Gen8-10 fully supported */
388 } else if (device
->info
.gen
== 11) {
389 intel_logw("Vulkan is not yet fully supported on gen11.");
391 result
= vk_errorf(device
->instance
, device
,
392 VK_ERROR_INCOMPATIBLE_DRIVER
,
393 "Vulkan not yet supported on %s", device
->name
);
397 device
->cmd_parser_version
= -1;
398 if (device
->info
.gen
== 7) {
399 device
->cmd_parser_version
=
400 anv_gem_get_param(fd
, I915_PARAM_CMD_PARSER_VERSION
);
401 if (device
->cmd_parser_version
== -1) {
402 result
= vk_errorf(device
->instance
, device
,
403 VK_ERROR_INITIALIZATION_FAILED
,
404 "failed to get command parser version");
409 if (!anv_gem_get_param(fd
, I915_PARAM_HAS_WAIT_TIMEOUT
)) {
410 result
= vk_errorf(device
->instance
, device
,
411 VK_ERROR_INITIALIZATION_FAILED
,
412 "kernel missing gem wait");
416 if (!anv_gem_get_param(fd
, I915_PARAM_HAS_EXECBUF2
)) {
417 result
= vk_errorf(device
->instance
, device
,
418 VK_ERROR_INITIALIZATION_FAILED
,
419 "kernel missing execbuf2");
423 if (!device
->info
.has_llc
&&
424 anv_gem_get_param(fd
, I915_PARAM_MMAP_VERSION
) < 1) {
425 result
= vk_errorf(device
->instance
, device
,
426 VK_ERROR_INITIALIZATION_FAILED
,
427 "kernel missing wc mmap");
431 result
= anv_physical_device_init_heaps(device
, fd
);
432 if (result
!= VK_SUCCESS
)
435 device
->has_exec_async
= anv_gem_get_param(fd
, I915_PARAM_HAS_EXEC_ASYNC
);
436 device
->has_exec_capture
= anv_gem_get_param(fd
, I915_PARAM_HAS_EXEC_CAPTURE
);
437 device
->has_exec_fence
= anv_gem_get_param(fd
, I915_PARAM_HAS_EXEC_FENCE
);
438 device
->has_syncobj
= anv_gem_get_param(fd
, I915_PARAM_HAS_EXEC_FENCE_ARRAY
);
439 device
->has_syncobj_wait
= device
->has_syncobj
&&
440 anv_gem_supports_syncobj_wait(fd
);
441 device
->has_context_priority
= anv_gem_has_context_priority(fd
);
443 device
->use_softpin
= anv_gem_get_param(fd
, I915_PARAM_HAS_EXEC_SOFTPIN
)
444 && device
->supports_48bit_addresses
;
446 device
->has_context_isolation
=
447 anv_gem_get_param(fd
, I915_PARAM_HAS_CONTEXT_ISOLATION
);
449 /* Starting with Gen10, the timestamp frequency of the command streamer may
450 * vary from one part to another. We can query the value from the kernel.
452 if (device
->info
.gen
>= 10) {
453 int timestamp_frequency
=
454 anv_gem_get_param(fd
, I915_PARAM_CS_TIMESTAMP_FREQUENCY
);
456 if (timestamp_frequency
< 0)
457 intel_logw("Kernel 4.16-rc1+ required to properly query CS timestamp frequency");
459 device
->info
.timestamp_frequency
= timestamp_frequency
;
462 /* GENs prior to 8 do not support EU/Subslice info */
463 if (device
->info
.gen
>= 8) {
464 device
->subslice_total
= anv_gem_get_param(fd
, I915_PARAM_SUBSLICE_TOTAL
);
465 device
->eu_total
= anv_gem_get_param(fd
, I915_PARAM_EU_TOTAL
);
467 /* Without this information, we cannot get the right Braswell
468 * brandstrings, and we have to use conservative numbers for GPGPU on
469 * many platforms, but otherwise, things will just work.
471 if (device
->subslice_total
< 1 || device
->eu_total
< 1) {
472 intel_logw("Kernel 4.1 required to properly query GPU properties");
474 } else if (device
->info
.gen
== 7) {
475 device
->subslice_total
= 1 << (device
->info
.gt
- 1);
478 if (device
->info
.is_cherryview
&&
479 device
->subslice_total
> 0 && device
->eu_total
> 0) {
480 /* Logical CS threads = EUs per subslice * num threads per EU */
481 uint32_t max_cs_threads
=
482 device
->eu_total
/ device
->subslice_total
* device
->info
.num_thread_per_eu
;
484 /* Fuse configurations may give more threads than expected, never less. */
485 if (max_cs_threads
> device
->info
.max_cs_threads
)
486 device
->info
.max_cs_threads
= max_cs_threads
;
489 device
->compiler
= brw_compiler_create(NULL
, &device
->info
);
490 if (device
->compiler
== NULL
) {
491 result
= vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
494 device
->compiler
->shader_debug_log
= compiler_debug_log
;
495 device
->compiler
->shader_perf_log
= compiler_perf_log
;
496 device
->compiler
->supports_pull_constants
= false;
497 device
->compiler
->constant_buffer_0_is_relative
=
498 device
->info
.gen
< 8 || !device
->has_context_isolation
;
499 device
->compiler
->supports_shader_constants
= true;
501 /* Broadwell PRM says:
503 * "Before Gen8, there was a historical configuration control field to
504 * swizzle address bit[6] for in X/Y tiling modes. This was set in three
505 * different places: TILECTL[1:0], ARB_MODE[5:4], and
506 * DISP_ARB_CTL[14:13].
508 * For Gen8 and subsequent generations, the swizzle fields are all
509 * reserved, and the CPU's memory controller performs all address
510 * swizzling modifications."
513 device
->info
.gen
< 8 && anv_gem_get_bit6_swizzle(fd
, I915_TILING_X
);
515 isl_device_init(&device
->isl_dev
, &device
->info
, swizzled
);
517 result
= anv_physical_device_init_uuids(device
);
518 if (result
!= VK_SUCCESS
)
521 anv_physical_device_init_disk_cache(device
);
523 if (instance
->enabled_extensions
.KHR_display
) {
524 master_fd
= open(primary_path
, O_RDWR
| O_CLOEXEC
);
525 if (master_fd
>= 0) {
526 /* prod the device with a GETPARAM call which will fail if
527 * we don't have permission to even render on this device
529 if (anv_gem_get_param(master_fd
, I915_PARAM_CHIPSET_ID
) == 0) {
535 device
->master_fd
= master_fd
;
537 result
= anv_init_wsi(device
);
538 if (result
!= VK_SUCCESS
) {
539 ralloc_free(device
->compiler
);
540 anv_physical_device_free_disk_cache(device
);
544 anv_physical_device_get_supported_extensions(device
,
545 &device
->supported_extensions
);
548 device
->local_fd
= fd
;
560 anv_physical_device_finish(struct anv_physical_device
*device
)
562 anv_finish_wsi(device
);
563 anv_physical_device_free_disk_cache(device
);
564 ralloc_free(device
->compiler
);
565 close(device
->local_fd
);
566 if (device
->master_fd
>= 0)
567 close(device
->master_fd
);
571 default_alloc_func(void *pUserData
, size_t size
, size_t align
,
572 VkSystemAllocationScope allocationScope
)
578 default_realloc_func(void *pUserData
, void *pOriginal
, size_t size
,
579 size_t align
, VkSystemAllocationScope allocationScope
)
581 return realloc(pOriginal
, size
);
585 default_free_func(void *pUserData
, void *pMemory
)
590 static const VkAllocationCallbacks default_alloc
= {
592 .pfnAllocation
= default_alloc_func
,
593 .pfnReallocation
= default_realloc_func
,
594 .pfnFree
= default_free_func
,
597 VkResult
anv_EnumerateInstanceExtensionProperties(
598 const char* pLayerName
,
599 uint32_t* pPropertyCount
,
600 VkExtensionProperties
* pProperties
)
602 VK_OUTARRAY_MAKE(out
, pProperties
, pPropertyCount
);
604 for (int i
= 0; i
< ANV_INSTANCE_EXTENSION_COUNT
; i
++) {
605 if (anv_instance_extensions_supported
.extensions
[i
]) {
606 vk_outarray_append(&out
, prop
) {
607 *prop
= anv_instance_extensions
[i
];
612 return vk_outarray_status(&out
);
615 VkResult
anv_CreateInstance(
616 const VkInstanceCreateInfo
* pCreateInfo
,
617 const VkAllocationCallbacks
* pAllocator
,
618 VkInstance
* pInstance
)
620 struct anv_instance
*instance
;
623 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO
);
625 struct anv_instance_extension_table enabled_extensions
= {};
626 for (uint32_t i
= 0; i
< pCreateInfo
->enabledExtensionCount
; i
++) {
628 for (idx
= 0; idx
< ANV_INSTANCE_EXTENSION_COUNT
; idx
++) {
629 if (strcmp(pCreateInfo
->ppEnabledExtensionNames
[i
],
630 anv_instance_extensions
[idx
].extensionName
) == 0)
634 if (idx
>= ANV_INSTANCE_EXTENSION_COUNT
)
635 return vk_error(VK_ERROR_EXTENSION_NOT_PRESENT
);
637 if (!anv_instance_extensions_supported
.extensions
[idx
])
638 return vk_error(VK_ERROR_EXTENSION_NOT_PRESENT
);
640 enabled_extensions
.extensions
[idx
] = true;
643 instance
= vk_alloc2(&default_alloc
, pAllocator
, sizeof(*instance
), 8,
644 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE
);
646 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
648 instance
->_loader_data
.loaderMagic
= ICD_LOADER_MAGIC
;
651 instance
->alloc
= *pAllocator
;
653 instance
->alloc
= default_alloc
;
655 instance
->app_info
= (struct anv_app_info
) { .api_version
= 0 };
656 if (pCreateInfo
->pApplicationInfo
) {
657 const VkApplicationInfo
*app
= pCreateInfo
->pApplicationInfo
;
659 instance
->app_info
.app_name
=
660 vk_strdup(&instance
->alloc
, app
->pApplicationName
,
661 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE
);
662 instance
->app_info
.app_version
= app
->applicationVersion
;
664 instance
->app_info
.engine_name
=
665 vk_strdup(&instance
->alloc
, app
->pEngineName
,
666 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE
);
667 instance
->app_info
.engine_version
= app
->engineVersion
;
669 instance
->app_info
.api_version
= app
->apiVersion
;
672 if (instance
->app_info
.api_version
== 0)
673 instance
->app_info
.api_version
= VK_API_VERSION_1_0
;
675 instance
->enabled_extensions
= enabled_extensions
;
677 for (unsigned i
= 0; i
< ARRAY_SIZE(instance
->dispatch
.entrypoints
); i
++) {
678 /* Vulkan requires that entrypoints for extensions which have not been
679 * enabled must not be advertised.
681 if (!anv_instance_entrypoint_is_enabled(i
, instance
->app_info
.api_version
,
682 &instance
->enabled_extensions
)) {
683 instance
->dispatch
.entrypoints
[i
] = NULL
;
685 instance
->dispatch
.entrypoints
[i
] =
686 anv_instance_dispatch_table
.entrypoints
[i
];
690 for (unsigned i
= 0; i
< ARRAY_SIZE(instance
->device_dispatch
.entrypoints
); i
++) {
691 /* Vulkan requires that entrypoints for extensions which have not been
692 * enabled must not be advertised.
694 if (!anv_device_entrypoint_is_enabled(i
, instance
->app_info
.api_version
,
695 &instance
->enabled_extensions
, NULL
)) {
696 instance
->device_dispatch
.entrypoints
[i
] = NULL
;
698 instance
->device_dispatch
.entrypoints
[i
] =
699 anv_device_dispatch_table
.entrypoints
[i
];
703 instance
->physicalDeviceCount
= -1;
705 result
= vk_debug_report_instance_init(&instance
->debug_report_callbacks
);
706 if (result
!= VK_SUCCESS
) {
707 vk_free2(&default_alloc
, pAllocator
, instance
);
708 return vk_error(result
);
711 instance
->pipeline_cache_enabled
=
712 env_var_as_boolean("ANV_ENABLE_PIPELINE_CACHE", true);
716 VG(VALGRIND_CREATE_MEMPOOL(instance
, 0, false));
718 *pInstance
= anv_instance_to_handle(instance
);
723 void anv_DestroyInstance(
724 VkInstance _instance
,
725 const VkAllocationCallbacks
* pAllocator
)
727 ANV_FROM_HANDLE(anv_instance
, instance
, _instance
);
732 if (instance
->physicalDeviceCount
> 0) {
733 /* We support at most one physical device. */
734 assert(instance
->physicalDeviceCount
== 1);
735 anv_physical_device_finish(&instance
->physicalDevice
);
738 vk_free(&instance
->alloc
, (char *)instance
->app_info
.app_name
);
739 vk_free(&instance
->alloc
, (char *)instance
->app_info
.engine_name
);
741 VG(VALGRIND_DESTROY_MEMPOOL(instance
));
743 vk_debug_report_instance_destroy(&instance
->debug_report_callbacks
);
747 vk_free(&instance
->alloc
, instance
);
751 anv_enumerate_devices(struct anv_instance
*instance
)
753 /* TODO: Check for more devices ? */
754 drmDevicePtr devices
[8];
755 VkResult result
= VK_ERROR_INCOMPATIBLE_DRIVER
;
758 instance
->physicalDeviceCount
= 0;
760 max_devices
= drmGetDevices2(0, devices
, ARRAY_SIZE(devices
));
762 return VK_ERROR_INCOMPATIBLE_DRIVER
;
764 for (unsigned i
= 0; i
< (unsigned)max_devices
; i
++) {
765 if (devices
[i
]->available_nodes
& 1 << DRM_NODE_RENDER
&&
766 devices
[i
]->bustype
== DRM_BUS_PCI
&&
767 devices
[i
]->deviceinfo
.pci
->vendor_id
== 0x8086) {
769 result
= anv_physical_device_init(&instance
->physicalDevice
,
770 instance
, devices
[i
]);
771 if (result
!= VK_ERROR_INCOMPATIBLE_DRIVER
)
775 drmFreeDevices(devices
, max_devices
);
777 if (result
== VK_SUCCESS
)
778 instance
->physicalDeviceCount
= 1;
784 anv_instance_ensure_physical_device(struct anv_instance
*instance
)
786 if (instance
->physicalDeviceCount
< 0) {
787 VkResult result
= anv_enumerate_devices(instance
);
788 if (result
!= VK_SUCCESS
&&
789 result
!= VK_ERROR_INCOMPATIBLE_DRIVER
)
796 VkResult
anv_EnumeratePhysicalDevices(
797 VkInstance _instance
,
798 uint32_t* pPhysicalDeviceCount
,
799 VkPhysicalDevice
* pPhysicalDevices
)
801 ANV_FROM_HANDLE(anv_instance
, instance
, _instance
);
802 VK_OUTARRAY_MAKE(out
, pPhysicalDevices
, pPhysicalDeviceCount
);
804 VkResult result
= anv_instance_ensure_physical_device(instance
);
805 if (result
!= VK_SUCCESS
)
808 if (instance
->physicalDeviceCount
== 0)
811 assert(instance
->physicalDeviceCount
== 1);
812 vk_outarray_append(&out
, i
) {
813 *i
= anv_physical_device_to_handle(&instance
->physicalDevice
);
816 return vk_outarray_status(&out
);
819 VkResult
anv_EnumeratePhysicalDeviceGroups(
820 VkInstance _instance
,
821 uint32_t* pPhysicalDeviceGroupCount
,
822 VkPhysicalDeviceGroupProperties
* pPhysicalDeviceGroupProperties
)
824 ANV_FROM_HANDLE(anv_instance
, instance
, _instance
);
825 VK_OUTARRAY_MAKE(out
, pPhysicalDeviceGroupProperties
,
826 pPhysicalDeviceGroupCount
);
828 VkResult result
= anv_instance_ensure_physical_device(instance
);
829 if (result
!= VK_SUCCESS
)
832 if (instance
->physicalDeviceCount
== 0)
835 assert(instance
->physicalDeviceCount
== 1);
837 vk_outarray_append(&out
, p
) {
838 p
->physicalDeviceCount
= 1;
839 memset(p
->physicalDevices
, 0, sizeof(p
->physicalDevices
));
840 p
->physicalDevices
[0] =
841 anv_physical_device_to_handle(&instance
->physicalDevice
);
842 p
->subsetAllocation
= false;
844 vk_foreach_struct(ext
, p
->pNext
)
845 anv_debug_ignored_stype(ext
->sType
);
848 return vk_outarray_status(&out
);
851 void anv_GetPhysicalDeviceFeatures(
852 VkPhysicalDevice physicalDevice
,
853 VkPhysicalDeviceFeatures
* pFeatures
)
855 ANV_FROM_HANDLE(anv_physical_device
, pdevice
, physicalDevice
);
857 *pFeatures
= (VkPhysicalDeviceFeatures
) {
858 .robustBufferAccess
= true,
859 .fullDrawIndexUint32
= true,
860 .imageCubeArray
= true,
861 .independentBlend
= true,
862 .geometryShader
= true,
863 .tessellationShader
= true,
864 .sampleRateShading
= true,
865 .dualSrcBlend
= true,
867 .multiDrawIndirect
= true,
868 .drawIndirectFirstInstance
= true,
870 .depthBiasClamp
= true,
871 .fillModeNonSolid
= true,
872 .depthBounds
= false,
876 .multiViewport
= true,
877 .samplerAnisotropy
= true,
878 .textureCompressionETC2
= pdevice
->info
.gen
>= 8 ||
879 pdevice
->info
.is_baytrail
,
880 .textureCompressionASTC_LDR
= pdevice
->info
.gen
>= 9, /* FINISHME CHV */
881 .textureCompressionBC
= true,
882 .occlusionQueryPrecise
= true,
883 .pipelineStatisticsQuery
= true,
884 .fragmentStoresAndAtomics
= true,
885 .shaderTessellationAndGeometryPointSize
= true,
886 .shaderImageGatherExtended
= true,
887 .shaderStorageImageExtendedFormats
= true,
888 .shaderStorageImageMultisample
= false,
889 .shaderStorageImageReadWithoutFormat
= false,
890 .shaderStorageImageWriteWithoutFormat
= true,
891 .shaderUniformBufferArrayDynamicIndexing
= true,
892 .shaderSampledImageArrayDynamicIndexing
= true,
893 .shaderStorageBufferArrayDynamicIndexing
= true,
894 .shaderStorageImageArrayDynamicIndexing
= true,
895 .shaderClipDistance
= true,
896 .shaderCullDistance
= true,
897 .shaderFloat64
= pdevice
->info
.gen
>= 8 &&
898 pdevice
->info
.has_64bit_types
,
899 .shaderInt64
= pdevice
->info
.gen
>= 8 &&
900 pdevice
->info
.has_64bit_types
,
901 .shaderInt16
= pdevice
->info
.gen
>= 8,
902 .shaderResourceMinLod
= pdevice
->info
.gen
>= 9,
903 .variableMultisampleRate
= true,
904 .inheritedQueries
= true,
907 /* We can't do image stores in vec4 shaders */
908 pFeatures
->vertexPipelineStoresAndAtomics
=
909 pdevice
->compiler
->scalar_stage
[MESA_SHADER_VERTEX
] &&
910 pdevice
->compiler
->scalar_stage
[MESA_SHADER_GEOMETRY
];
912 struct anv_app_info
*app_info
= &pdevice
->instance
->app_info
;
914 /* The new DOOM and Wolfenstein games require depthBounds without
915 * checking for it. They seem to run fine without it so just claim it's
916 * there and accept the consequences.
918 if (app_info
->engine_name
&& strcmp(app_info
->engine_name
, "idTech") == 0)
919 pFeatures
->depthBounds
= true;
922 void anv_GetPhysicalDeviceFeatures2(
923 VkPhysicalDevice physicalDevice
,
924 VkPhysicalDeviceFeatures2
* pFeatures
)
926 ANV_FROM_HANDLE(anv_physical_device
, pdevice
, physicalDevice
);
927 anv_GetPhysicalDeviceFeatures(physicalDevice
, &pFeatures
->features
);
929 vk_foreach_struct(ext
, pFeatures
->pNext
) {
930 switch (ext
->sType
) {
931 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_8BIT_STORAGE_FEATURES_KHR
: {
932 VkPhysicalDevice8BitStorageFeaturesKHR
*features
=
933 (VkPhysicalDevice8BitStorageFeaturesKHR
*)ext
;
934 ANV_FROM_HANDLE(anv_physical_device
, pdevice
, physicalDevice
);
936 features
->storageBuffer8BitAccess
= pdevice
->info
.gen
>= 8;
937 features
->uniformAndStorageBuffer8BitAccess
= pdevice
->info
.gen
>= 8;
938 features
->storagePushConstant8
= pdevice
->info
.gen
>= 8;
942 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_16BIT_STORAGE_FEATURES
: {
943 VkPhysicalDevice16BitStorageFeatures
*features
=
944 (VkPhysicalDevice16BitStorageFeatures
*)ext
;
945 features
->storageBuffer16BitAccess
= pdevice
->info
.gen
>= 8;
946 features
->uniformAndStorageBuffer16BitAccess
= pdevice
->info
.gen
>= 8;
947 features
->storagePushConstant16
= pdevice
->info
.gen
>= 8;
948 features
->storageInputOutput16
= false;
952 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_BUFFER_ADDRESS_FEATURES_EXT
: {
953 VkPhysicalDeviceBufferAddressFeaturesEXT
*features
= (void *)ext
;
954 features
->bufferDeviceAddress
= pdevice
->use_softpin
&&
955 pdevice
->info
.gen
>= 8;
956 features
->bufferDeviceAddressCaptureReplay
= false;
957 features
->bufferDeviceAddressMultiDevice
= false;
961 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_CONDITIONAL_RENDERING_FEATURES_EXT
: {
962 VkPhysicalDeviceConditionalRenderingFeaturesEXT
*features
=
963 (VkPhysicalDeviceConditionalRenderingFeaturesEXT
*)ext
;
964 ANV_FROM_HANDLE(anv_physical_device
, pdevice
, physicalDevice
);
966 features
->conditionalRendering
= pdevice
->info
.gen
>= 8 ||
967 pdevice
->info
.is_haswell
;
968 features
->inheritedConditionalRendering
= pdevice
->info
.gen
>= 8 ||
969 pdevice
->info
.is_haswell
;
973 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_DEPTH_CLIP_ENABLE_FEATURES_EXT
: {
974 VkPhysicalDeviceDepthClipEnableFeaturesEXT
*features
=
975 (VkPhysicalDeviceDepthClipEnableFeaturesEXT
*)ext
;
976 features
->depthClipEnable
= true;
980 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_HOST_QUERY_RESET_FEATURES_EXT
: {
981 VkPhysicalDeviceHostQueryResetFeaturesEXT
*features
=
982 (VkPhysicalDeviceHostQueryResetFeaturesEXT
*)ext
;
983 features
->hostQueryReset
= true;
987 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_INLINE_UNIFORM_BLOCK_FEATURES_EXT
: {
988 VkPhysicalDeviceInlineUniformBlockFeaturesEXT
*features
=
989 (VkPhysicalDeviceInlineUniformBlockFeaturesEXT
*)ext
;
990 features
->inlineUniformBlock
= true;
991 features
->descriptorBindingInlineUniformBlockUpdateAfterBind
= false;
995 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_MULTIVIEW_FEATURES
: {
996 VkPhysicalDeviceMultiviewFeatures
*features
=
997 (VkPhysicalDeviceMultiviewFeatures
*)ext
;
998 features
->multiview
= true;
999 features
->multiviewGeometryShader
= true;
1000 features
->multiviewTessellationShader
= true;
1004 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PROTECTED_MEMORY_FEATURES
: {
1005 VkPhysicalDeviceProtectedMemoryFeatures
*features
= (void *)ext
;
1006 features
->protectedMemory
= false;
1010 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SAMPLER_YCBCR_CONVERSION_FEATURES
: {
1011 VkPhysicalDeviceSamplerYcbcrConversionFeatures
*features
=
1012 (VkPhysicalDeviceSamplerYcbcrConversionFeatures
*) ext
;
1013 features
->samplerYcbcrConversion
= true;
1017 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SCALAR_BLOCK_LAYOUT_FEATURES_EXT
: {
1018 VkPhysicalDeviceScalarBlockLayoutFeaturesEXT
*features
=
1019 (VkPhysicalDeviceScalarBlockLayoutFeaturesEXT
*)ext
;
1020 features
->scalarBlockLayout
= true;
1024 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SHADER_DRAW_PARAMETER_FEATURES
: {
1025 VkPhysicalDeviceShaderDrawParameterFeatures
*features
= (void *)ext
;
1026 features
->shaderDrawParameters
= true;
1030 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VARIABLE_POINTER_FEATURES
: {
1031 VkPhysicalDeviceVariablePointerFeatures
*features
= (void *)ext
;
1032 features
->variablePointersStorageBuffer
= true;
1033 features
->variablePointers
= true;
1037 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_TRANSFORM_FEEDBACK_FEATURES_EXT
: {
1038 VkPhysicalDeviceTransformFeedbackFeaturesEXT
*features
=
1039 (VkPhysicalDeviceTransformFeedbackFeaturesEXT
*)ext
;
1040 features
->transformFeedback
= true;
1041 features
->geometryStreams
= true;
1045 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VERTEX_ATTRIBUTE_DIVISOR_FEATURES_EXT
: {
1046 VkPhysicalDeviceVertexAttributeDivisorFeaturesEXT
*features
=
1047 (VkPhysicalDeviceVertexAttributeDivisorFeaturesEXT
*)ext
;
1048 features
->vertexAttributeInstanceRateDivisor
= true;
1049 features
->vertexAttributeInstanceRateZeroDivisor
= true;
1053 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_YCBCR_IMAGE_ARRAYS_FEATURES_EXT
: {
1054 VkPhysicalDeviceYcbcrImageArraysFeaturesEXT
*features
=
1055 (VkPhysicalDeviceYcbcrImageArraysFeaturesEXT
*)ext
;
1056 features
->ycbcrImageArrays
= true;
1060 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_COMPUTE_SHADER_DERIVATIVES_FEATURES_NV
: {
1061 VkPhysicalDeviceComputeShaderDerivativesFeaturesNV
*features
=
1062 (VkPhysicalDeviceComputeShaderDerivativesFeaturesNV
*)ext
;
1063 features
->computeDerivativeGroupQuads
= true;
1064 features
->computeDerivativeGroupLinear
= true;
1069 anv_debug_ignored_stype(ext
->sType
);
1075 void anv_GetPhysicalDeviceProperties(
1076 VkPhysicalDevice physicalDevice
,
1077 VkPhysicalDeviceProperties
* pProperties
)
1079 ANV_FROM_HANDLE(anv_physical_device
, pdevice
, physicalDevice
);
1080 const struct gen_device_info
*devinfo
= &pdevice
->info
;
1082 /* See assertions made when programming the buffer surface state. */
1083 const uint32_t max_raw_buffer_sz
= devinfo
->gen
>= 7 ?
1084 (1ul << 30) : (1ul << 27);
1086 const uint32_t max_samplers
= (devinfo
->gen
>= 8 || devinfo
->is_haswell
) ?
1089 const uint32_t max_images
= devinfo
->gen
< 9 ? MAX_GEN8_IMAGES
: MAX_IMAGES
;
1091 VkSampleCountFlags sample_counts
=
1092 isl_device_get_sample_counts(&pdevice
->isl_dev
);
1095 VkPhysicalDeviceLimits limits
= {
1096 .maxImageDimension1D
= (1 << 14),
1097 .maxImageDimension2D
= (1 << 14),
1098 .maxImageDimension3D
= (1 << 11),
1099 .maxImageDimensionCube
= (1 << 14),
1100 .maxImageArrayLayers
= (1 << 11),
1101 .maxTexelBufferElements
= 128 * 1024 * 1024,
1102 .maxUniformBufferRange
= (1ul << 27),
1103 .maxStorageBufferRange
= max_raw_buffer_sz
,
1104 .maxPushConstantsSize
= MAX_PUSH_CONSTANTS_SIZE
,
1105 .maxMemoryAllocationCount
= UINT32_MAX
,
1106 .maxSamplerAllocationCount
= 64 * 1024,
1107 .bufferImageGranularity
= 64, /* A cache line */
1108 .sparseAddressSpaceSize
= 0,
1109 .maxBoundDescriptorSets
= MAX_SETS
,
1110 .maxPerStageDescriptorSamplers
= max_samplers
,
1111 .maxPerStageDescriptorUniformBuffers
= 64,
1112 .maxPerStageDescriptorStorageBuffers
= 64,
1113 .maxPerStageDescriptorSampledImages
= max_samplers
,
1114 .maxPerStageDescriptorStorageImages
= max_images
,
1115 .maxPerStageDescriptorInputAttachments
= 64,
1116 .maxPerStageResources
= 250,
1117 .maxDescriptorSetSamplers
= 6 * max_samplers
, /* number of stages * maxPerStageDescriptorSamplers */
1118 .maxDescriptorSetUniformBuffers
= 6 * 64, /* number of stages * maxPerStageDescriptorUniformBuffers */
1119 .maxDescriptorSetUniformBuffersDynamic
= MAX_DYNAMIC_BUFFERS
/ 2,
1120 .maxDescriptorSetStorageBuffers
= 6 * 64, /* number of stages * maxPerStageDescriptorStorageBuffers */
1121 .maxDescriptorSetStorageBuffersDynamic
= MAX_DYNAMIC_BUFFERS
/ 2,
1122 .maxDescriptorSetSampledImages
= 6 * max_samplers
, /* number of stages * maxPerStageDescriptorSampledImages */
1123 .maxDescriptorSetStorageImages
= 6 * max_images
, /* number of stages * maxPerStageDescriptorStorageImages */
1124 .maxDescriptorSetInputAttachments
= 256,
1125 .maxVertexInputAttributes
= MAX_VBS
,
1126 .maxVertexInputBindings
= MAX_VBS
,
1127 .maxVertexInputAttributeOffset
= 2047,
1128 .maxVertexInputBindingStride
= 2048,
1129 .maxVertexOutputComponents
= 128,
1130 .maxTessellationGenerationLevel
= 64,
1131 .maxTessellationPatchSize
= 32,
1132 .maxTessellationControlPerVertexInputComponents
= 128,
1133 .maxTessellationControlPerVertexOutputComponents
= 128,
1134 .maxTessellationControlPerPatchOutputComponents
= 128,
1135 .maxTessellationControlTotalOutputComponents
= 2048,
1136 .maxTessellationEvaluationInputComponents
= 128,
1137 .maxTessellationEvaluationOutputComponents
= 128,
1138 .maxGeometryShaderInvocations
= 32,
1139 .maxGeometryInputComponents
= 64,
1140 .maxGeometryOutputComponents
= 128,
1141 .maxGeometryOutputVertices
= 256,
1142 .maxGeometryTotalOutputComponents
= 1024,
1143 .maxFragmentInputComponents
= 112, /* 128 components - (POS, PSIZ, CLIP_DIST0, CLIP_DIST1) */
1144 .maxFragmentOutputAttachments
= 8,
1145 .maxFragmentDualSrcAttachments
= 1,
1146 .maxFragmentCombinedOutputResources
= 8,
1147 .maxComputeSharedMemorySize
= 32768,
1148 .maxComputeWorkGroupCount
= { 65535, 65535, 65535 },
1149 .maxComputeWorkGroupInvocations
= 32 * devinfo
->max_cs_threads
,
1150 .maxComputeWorkGroupSize
= {
1151 16 * devinfo
->max_cs_threads
,
1152 16 * devinfo
->max_cs_threads
,
1153 16 * devinfo
->max_cs_threads
,
1155 .subPixelPrecisionBits
= 8,
1156 .subTexelPrecisionBits
= 8,
1157 .mipmapPrecisionBits
= 8,
1158 .maxDrawIndexedIndexValue
= UINT32_MAX
,
1159 .maxDrawIndirectCount
= UINT32_MAX
,
1160 .maxSamplerLodBias
= 16,
1161 .maxSamplerAnisotropy
= 16,
1162 .maxViewports
= MAX_VIEWPORTS
,
1163 .maxViewportDimensions
= { (1 << 14), (1 << 14) },
1164 .viewportBoundsRange
= { INT16_MIN
, INT16_MAX
},
1165 .viewportSubPixelBits
= 13, /* We take a float? */
1166 .minMemoryMapAlignment
= 4096, /* A page */
1167 .minTexelBufferOffsetAlignment
= 1,
1168 /* We need 16 for UBO block reads to work and 32 for push UBOs */
1169 .minUniformBufferOffsetAlignment
= 32,
1170 .minStorageBufferOffsetAlignment
= 4,
1171 .minTexelOffset
= -8,
1172 .maxTexelOffset
= 7,
1173 .minTexelGatherOffset
= -32,
1174 .maxTexelGatherOffset
= 31,
1175 .minInterpolationOffset
= -0.5,
1176 .maxInterpolationOffset
= 0.4375,
1177 .subPixelInterpolationOffsetBits
= 4,
1178 .maxFramebufferWidth
= (1 << 14),
1179 .maxFramebufferHeight
= (1 << 14),
1180 .maxFramebufferLayers
= (1 << 11),
1181 .framebufferColorSampleCounts
= sample_counts
,
1182 .framebufferDepthSampleCounts
= sample_counts
,
1183 .framebufferStencilSampleCounts
= sample_counts
,
1184 .framebufferNoAttachmentsSampleCounts
= sample_counts
,
1185 .maxColorAttachments
= MAX_RTS
,
1186 .sampledImageColorSampleCounts
= sample_counts
,
1187 .sampledImageIntegerSampleCounts
= VK_SAMPLE_COUNT_1_BIT
,
1188 .sampledImageDepthSampleCounts
= sample_counts
,
1189 .sampledImageStencilSampleCounts
= sample_counts
,
1190 .storageImageSampleCounts
= VK_SAMPLE_COUNT_1_BIT
,
1191 .maxSampleMaskWords
= 1,
1192 .timestampComputeAndGraphics
= false,
1193 .timestampPeriod
= 1000000000.0 / devinfo
->timestamp_frequency
,
1194 .maxClipDistances
= 8,
1195 .maxCullDistances
= 8,
1196 .maxCombinedClipAndCullDistances
= 8,
1197 .discreteQueuePriorities
= 2,
1198 .pointSizeRange
= { 0.125, 255.875 },
1199 .lineWidthRange
= { 0.0, 7.9921875 },
1200 .pointSizeGranularity
= (1.0 / 8.0),
1201 .lineWidthGranularity
= (1.0 / 128.0),
1202 .strictLines
= false, /* FINISHME */
1203 .standardSampleLocations
= true,
1204 .optimalBufferCopyOffsetAlignment
= 128,
1205 .optimalBufferCopyRowPitchAlignment
= 128,
1206 .nonCoherentAtomSize
= 64,
1209 *pProperties
= (VkPhysicalDeviceProperties
) {
1210 .apiVersion
= anv_physical_device_api_version(pdevice
),
1211 .driverVersion
= vk_get_driver_version(),
1213 .deviceID
= pdevice
->chipset_id
,
1214 .deviceType
= VK_PHYSICAL_DEVICE_TYPE_INTEGRATED_GPU
,
1216 .sparseProperties
= {0}, /* Broadwell doesn't do sparse. */
1219 snprintf(pProperties
->deviceName
, sizeof(pProperties
->deviceName
),
1220 "%s", pdevice
->name
);
1221 memcpy(pProperties
->pipelineCacheUUID
,
1222 pdevice
->pipeline_cache_uuid
, VK_UUID_SIZE
);
1225 void anv_GetPhysicalDeviceProperties2(
1226 VkPhysicalDevice physicalDevice
,
1227 VkPhysicalDeviceProperties2
* pProperties
)
1229 ANV_FROM_HANDLE(anv_physical_device
, pdevice
, physicalDevice
);
1231 anv_GetPhysicalDeviceProperties(physicalDevice
, &pProperties
->properties
);
1233 vk_foreach_struct(ext
, pProperties
->pNext
) {
1234 switch (ext
->sType
) {
1235 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_DEPTH_STENCIL_RESOLVE_PROPERTIES_KHR
: {
1236 VkPhysicalDeviceDepthStencilResolvePropertiesKHR
*props
=
1237 (VkPhysicalDeviceDepthStencilResolvePropertiesKHR
*)ext
;
1239 /* We support all of the depth resolve modes */
1240 props
->supportedDepthResolveModes
=
1241 VK_RESOLVE_MODE_SAMPLE_ZERO_BIT_KHR
|
1242 VK_RESOLVE_MODE_AVERAGE_BIT_KHR
|
1243 VK_RESOLVE_MODE_MIN_BIT_KHR
|
1244 VK_RESOLVE_MODE_MAX_BIT_KHR
;
1246 /* Average doesn't make sense for stencil so we don't support that */
1247 props
->supportedStencilResolveModes
=
1248 VK_RESOLVE_MODE_SAMPLE_ZERO_BIT_KHR
;
1249 if (pdevice
->info
.gen
>= 8) {
1250 /* The advanced stencil resolve modes currently require stencil
1251 * sampling be supported by the hardware.
1253 props
->supportedStencilResolveModes
|=
1254 VK_RESOLVE_MODE_MIN_BIT_KHR
|
1255 VK_RESOLVE_MODE_MAX_BIT_KHR
;
1258 props
->independentResolveNone
= true;
1259 props
->independentResolve
= true;
1263 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_DRIVER_PROPERTIES_KHR
: {
1264 VkPhysicalDeviceDriverPropertiesKHR
*driver_props
=
1265 (VkPhysicalDeviceDriverPropertiesKHR
*) ext
;
1267 driver_props
->driverID
= VK_DRIVER_ID_INTEL_OPEN_SOURCE_MESA_KHR
;
1268 util_snprintf(driver_props
->driverName
, VK_MAX_DRIVER_NAME_SIZE_KHR
,
1269 "Intel open-source Mesa driver");
1271 util_snprintf(driver_props
->driverInfo
, VK_MAX_DRIVER_INFO_SIZE_KHR
,
1272 "Mesa " PACKAGE_VERSION MESA_GIT_SHA1
);
1274 driver_props
->conformanceVersion
= (VkConformanceVersionKHR
) {
1283 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_EXTERNAL_MEMORY_HOST_PROPERTIES_EXT
: {
1284 VkPhysicalDeviceExternalMemoryHostPropertiesEXT
*props
=
1285 (VkPhysicalDeviceExternalMemoryHostPropertiesEXT
*) ext
;
1286 /* Userptr needs page aligned memory. */
1287 props
->minImportedHostPointerAlignment
= 4096;
1291 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_ID_PROPERTIES
: {
1292 VkPhysicalDeviceIDProperties
*id_props
=
1293 (VkPhysicalDeviceIDProperties
*)ext
;
1294 memcpy(id_props
->deviceUUID
, pdevice
->device_uuid
, VK_UUID_SIZE
);
1295 memcpy(id_props
->driverUUID
, pdevice
->driver_uuid
, VK_UUID_SIZE
);
1296 /* The LUID is for Windows. */
1297 id_props
->deviceLUIDValid
= false;
1301 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_INLINE_UNIFORM_BLOCK_PROPERTIES_EXT
: {
1302 VkPhysicalDeviceInlineUniformBlockPropertiesEXT
*props
=
1303 (VkPhysicalDeviceInlineUniformBlockPropertiesEXT
*)ext
;
1304 props
->maxInlineUniformBlockSize
= MAX_INLINE_UNIFORM_BLOCK_SIZE
;
1305 props
->maxPerStageDescriptorInlineUniformBlocks
=
1306 MAX_INLINE_UNIFORM_BLOCK_DESCRIPTORS
;
1307 props
->maxPerStageDescriptorUpdateAfterBindInlineUniformBlocks
=
1308 MAX_INLINE_UNIFORM_BLOCK_DESCRIPTORS
;
1309 props
->maxDescriptorSetInlineUniformBlocks
=
1310 MAX_INLINE_UNIFORM_BLOCK_DESCRIPTORS
;
1311 props
->maxDescriptorSetUpdateAfterBindInlineUniformBlocks
=
1312 MAX_INLINE_UNIFORM_BLOCK_DESCRIPTORS
;
1316 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_MAINTENANCE_3_PROPERTIES
: {
1317 VkPhysicalDeviceMaintenance3Properties
*props
=
1318 (VkPhysicalDeviceMaintenance3Properties
*)ext
;
1319 /* This value doesn't matter for us today as our per-stage
1320 * descriptors are the real limit.
1322 props
->maxPerSetDescriptors
= 1024;
1323 props
->maxMemoryAllocationSize
= MAX_MEMORY_ALLOCATION_SIZE
;
1327 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_MULTIVIEW_PROPERTIES
: {
1328 VkPhysicalDeviceMultiviewProperties
*properties
=
1329 (VkPhysicalDeviceMultiviewProperties
*)ext
;
1330 properties
->maxMultiviewViewCount
= 16;
1331 properties
->maxMultiviewInstanceIndex
= UINT32_MAX
/ 16;
1335 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PCI_BUS_INFO_PROPERTIES_EXT
: {
1336 VkPhysicalDevicePCIBusInfoPropertiesEXT
*properties
=
1337 (VkPhysicalDevicePCIBusInfoPropertiesEXT
*)ext
;
1338 properties
->pciDomain
= pdevice
->pci_info
.domain
;
1339 properties
->pciBus
= pdevice
->pci_info
.bus
;
1340 properties
->pciDevice
= pdevice
->pci_info
.device
;
1341 properties
->pciFunction
= pdevice
->pci_info
.function
;
1345 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_POINT_CLIPPING_PROPERTIES
: {
1346 VkPhysicalDevicePointClippingProperties
*properties
=
1347 (VkPhysicalDevicePointClippingProperties
*) ext
;
1348 properties
->pointClippingBehavior
= VK_POINT_CLIPPING_BEHAVIOR_ALL_CLIP_PLANES
;
1349 anv_finishme("Implement pop-free point clipping");
1353 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PROTECTED_MEMORY_PROPERTIES
: {
1354 VkPhysicalDeviceProtectedMemoryProperties
*props
=
1355 (VkPhysicalDeviceProtectedMemoryProperties
*)ext
;
1356 props
->protectedNoFault
= false;
1360 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PUSH_DESCRIPTOR_PROPERTIES_KHR
: {
1361 VkPhysicalDevicePushDescriptorPropertiesKHR
*properties
=
1362 (VkPhysicalDevicePushDescriptorPropertiesKHR
*) ext
;
1364 properties
->maxPushDescriptors
= MAX_PUSH_DESCRIPTORS
;
1368 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SAMPLER_FILTER_MINMAX_PROPERTIES_EXT
: {
1369 VkPhysicalDeviceSamplerFilterMinmaxPropertiesEXT
*properties
=
1370 (VkPhysicalDeviceSamplerFilterMinmaxPropertiesEXT
*)ext
;
1371 properties
->filterMinmaxImageComponentMapping
= pdevice
->info
.gen
>= 9;
1372 properties
->filterMinmaxSingleComponentFormats
= true;
1376 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SUBGROUP_PROPERTIES
: {
1377 VkPhysicalDeviceSubgroupProperties
*properties
= (void *)ext
;
1379 properties
->subgroupSize
= BRW_SUBGROUP_SIZE
;
1381 VkShaderStageFlags scalar_stages
= 0;
1382 for (unsigned stage
= 0; stage
< MESA_SHADER_STAGES
; stage
++) {
1383 if (pdevice
->compiler
->scalar_stage
[stage
])
1384 scalar_stages
|= mesa_to_vk_shader_stage(stage
);
1386 properties
->supportedStages
= scalar_stages
;
1388 properties
->supportedOperations
= VK_SUBGROUP_FEATURE_BASIC_BIT
|
1389 VK_SUBGROUP_FEATURE_VOTE_BIT
|
1390 VK_SUBGROUP_FEATURE_ARITHMETIC_BIT
|
1391 VK_SUBGROUP_FEATURE_BALLOT_BIT
|
1392 VK_SUBGROUP_FEATURE_SHUFFLE_BIT
|
1393 VK_SUBGROUP_FEATURE_SHUFFLE_RELATIVE_BIT
|
1394 VK_SUBGROUP_FEATURE_CLUSTERED_BIT
|
1395 VK_SUBGROUP_FEATURE_QUAD_BIT
;
1396 properties
->quadOperationsInAllStages
= true;
1400 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_TRANSFORM_FEEDBACK_PROPERTIES_EXT
: {
1401 VkPhysicalDeviceTransformFeedbackPropertiesEXT
*props
=
1402 (VkPhysicalDeviceTransformFeedbackPropertiesEXT
*)ext
;
1404 props
->maxTransformFeedbackStreams
= MAX_XFB_STREAMS
;
1405 props
->maxTransformFeedbackBuffers
= MAX_XFB_BUFFERS
;
1406 props
->maxTransformFeedbackBufferSize
= (1ull << 32);
1407 props
->maxTransformFeedbackStreamDataSize
= 128 * 4;
1408 props
->maxTransformFeedbackBufferDataSize
= 128 * 4;
1409 props
->maxTransformFeedbackBufferDataStride
= 2048;
1410 props
->transformFeedbackQueries
= true;
1411 props
->transformFeedbackStreamsLinesTriangles
= false;
1412 props
->transformFeedbackRasterizationStreamSelect
= false;
1413 props
->transformFeedbackDraw
= true;
1417 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VERTEX_ATTRIBUTE_DIVISOR_PROPERTIES_EXT
: {
1418 VkPhysicalDeviceVertexAttributeDivisorPropertiesEXT
*props
=
1419 (VkPhysicalDeviceVertexAttributeDivisorPropertiesEXT
*)ext
;
1420 /* We have to restrict this a bit for multiview */
1421 props
->maxVertexAttribDivisor
= UINT32_MAX
/ 16;
1426 anv_debug_ignored_stype(ext
->sType
);
1432 /* We support exactly one queue family. */
1433 static const VkQueueFamilyProperties
1434 anv_queue_family_properties
= {
1435 .queueFlags
= VK_QUEUE_GRAPHICS_BIT
|
1436 VK_QUEUE_COMPUTE_BIT
|
1437 VK_QUEUE_TRANSFER_BIT
,
1439 .timestampValidBits
= 36, /* XXX: Real value here */
1440 .minImageTransferGranularity
= { 1, 1, 1 },
1443 void anv_GetPhysicalDeviceQueueFamilyProperties(
1444 VkPhysicalDevice physicalDevice
,
1446 VkQueueFamilyProperties
* pQueueFamilyProperties
)
1448 VK_OUTARRAY_MAKE(out
, pQueueFamilyProperties
, pCount
);
1450 vk_outarray_append(&out
, p
) {
1451 *p
= anv_queue_family_properties
;
1455 void anv_GetPhysicalDeviceQueueFamilyProperties2(
1456 VkPhysicalDevice physicalDevice
,
1457 uint32_t* pQueueFamilyPropertyCount
,
1458 VkQueueFamilyProperties2
* pQueueFamilyProperties
)
1461 VK_OUTARRAY_MAKE(out
, pQueueFamilyProperties
, pQueueFamilyPropertyCount
);
1463 vk_outarray_append(&out
, p
) {
1464 p
->queueFamilyProperties
= anv_queue_family_properties
;
1466 vk_foreach_struct(s
, p
->pNext
) {
1467 anv_debug_ignored_stype(s
->sType
);
1472 void anv_GetPhysicalDeviceMemoryProperties(
1473 VkPhysicalDevice physicalDevice
,
1474 VkPhysicalDeviceMemoryProperties
* pMemoryProperties
)
1476 ANV_FROM_HANDLE(anv_physical_device
, physical_device
, physicalDevice
);
1478 pMemoryProperties
->memoryTypeCount
= physical_device
->memory
.type_count
;
1479 for (uint32_t i
= 0; i
< physical_device
->memory
.type_count
; i
++) {
1480 pMemoryProperties
->memoryTypes
[i
] = (VkMemoryType
) {
1481 .propertyFlags
= physical_device
->memory
.types
[i
].propertyFlags
,
1482 .heapIndex
= physical_device
->memory
.types
[i
].heapIndex
,
1486 pMemoryProperties
->memoryHeapCount
= physical_device
->memory
.heap_count
;
1487 for (uint32_t i
= 0; i
< physical_device
->memory
.heap_count
; i
++) {
1488 pMemoryProperties
->memoryHeaps
[i
] = (VkMemoryHeap
) {
1489 .size
= physical_device
->memory
.heaps
[i
].size
,
1490 .flags
= physical_device
->memory
.heaps
[i
].flags
,
1495 void anv_GetPhysicalDeviceMemoryProperties2(
1496 VkPhysicalDevice physicalDevice
,
1497 VkPhysicalDeviceMemoryProperties2
* pMemoryProperties
)
1499 anv_GetPhysicalDeviceMemoryProperties(physicalDevice
,
1500 &pMemoryProperties
->memoryProperties
);
1502 vk_foreach_struct(ext
, pMemoryProperties
->pNext
) {
1503 switch (ext
->sType
) {
1505 anv_debug_ignored_stype(ext
->sType
);
1512 anv_GetDeviceGroupPeerMemoryFeatures(
1515 uint32_t localDeviceIndex
,
1516 uint32_t remoteDeviceIndex
,
1517 VkPeerMemoryFeatureFlags
* pPeerMemoryFeatures
)
1519 assert(localDeviceIndex
== 0 && remoteDeviceIndex
== 0);
1520 *pPeerMemoryFeatures
= VK_PEER_MEMORY_FEATURE_COPY_SRC_BIT
|
1521 VK_PEER_MEMORY_FEATURE_COPY_DST_BIT
|
1522 VK_PEER_MEMORY_FEATURE_GENERIC_SRC_BIT
|
1523 VK_PEER_MEMORY_FEATURE_GENERIC_DST_BIT
;
1526 PFN_vkVoidFunction
anv_GetInstanceProcAddr(
1527 VkInstance _instance
,
1530 ANV_FROM_HANDLE(anv_instance
, instance
, _instance
);
1532 /* The Vulkan 1.0 spec for vkGetInstanceProcAddr has a table of exactly
1533 * when we have to return valid function pointers, NULL, or it's left
1534 * undefined. See the table for exact details.
1539 #define LOOKUP_ANV_ENTRYPOINT(entrypoint) \
1540 if (strcmp(pName, "vk" #entrypoint) == 0) \
1541 return (PFN_vkVoidFunction)anv_##entrypoint
1543 LOOKUP_ANV_ENTRYPOINT(EnumerateInstanceExtensionProperties
);
1544 LOOKUP_ANV_ENTRYPOINT(EnumerateInstanceLayerProperties
);
1545 LOOKUP_ANV_ENTRYPOINT(EnumerateInstanceVersion
);
1546 LOOKUP_ANV_ENTRYPOINT(CreateInstance
);
1548 #undef LOOKUP_ANV_ENTRYPOINT
1550 if (instance
== NULL
)
1553 int idx
= anv_get_instance_entrypoint_index(pName
);
1555 return instance
->dispatch
.entrypoints
[idx
];
1557 idx
= anv_get_device_entrypoint_index(pName
);
1559 return instance
->device_dispatch
.entrypoints
[idx
];
1564 /* With version 1+ of the loader interface the ICD should expose
1565 * vk_icdGetInstanceProcAddr to work around certain LD_PRELOAD issues seen in apps.
1568 VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL
vk_icdGetInstanceProcAddr(
1569 VkInstance instance
,
1573 VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL
vk_icdGetInstanceProcAddr(
1574 VkInstance instance
,
1577 return anv_GetInstanceProcAddr(instance
, pName
);
1580 PFN_vkVoidFunction
anv_GetDeviceProcAddr(
1584 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1586 if (!device
|| !pName
)
1589 int idx
= anv_get_device_entrypoint_index(pName
);
1593 return device
->dispatch
.entrypoints
[idx
];
1597 anv_CreateDebugReportCallbackEXT(VkInstance _instance
,
1598 const VkDebugReportCallbackCreateInfoEXT
* pCreateInfo
,
1599 const VkAllocationCallbacks
* pAllocator
,
1600 VkDebugReportCallbackEXT
* pCallback
)
1602 ANV_FROM_HANDLE(anv_instance
, instance
, _instance
);
1603 return vk_create_debug_report_callback(&instance
->debug_report_callbacks
,
1604 pCreateInfo
, pAllocator
, &instance
->alloc
,
1609 anv_DestroyDebugReportCallbackEXT(VkInstance _instance
,
1610 VkDebugReportCallbackEXT _callback
,
1611 const VkAllocationCallbacks
* pAllocator
)
1613 ANV_FROM_HANDLE(anv_instance
, instance
, _instance
);
1614 vk_destroy_debug_report_callback(&instance
->debug_report_callbacks
,
1615 _callback
, pAllocator
, &instance
->alloc
);
1619 anv_DebugReportMessageEXT(VkInstance _instance
,
1620 VkDebugReportFlagsEXT flags
,
1621 VkDebugReportObjectTypeEXT objectType
,
1624 int32_t messageCode
,
1625 const char* pLayerPrefix
,
1626 const char* pMessage
)
1628 ANV_FROM_HANDLE(anv_instance
, instance
, _instance
);
1629 vk_debug_report(&instance
->debug_report_callbacks
, flags
, objectType
,
1630 object
, location
, messageCode
, pLayerPrefix
, pMessage
);
1634 anv_queue_init(struct anv_device
*device
, struct anv_queue
*queue
)
1636 queue
->_loader_data
.loaderMagic
= ICD_LOADER_MAGIC
;
1637 queue
->device
= device
;
1642 anv_queue_finish(struct anv_queue
*queue
)
1646 static struct anv_state
1647 anv_state_pool_emit_data(struct anv_state_pool
*pool
, size_t size
, size_t align
, const void *p
)
1649 struct anv_state state
;
1651 state
= anv_state_pool_alloc(pool
, size
, align
);
1652 memcpy(state
.map
, p
, size
);
1657 struct gen8_border_color
{
1662 /* Pad out to 64 bytes */
1667 anv_device_init_border_colors(struct anv_device
*device
)
1669 static const struct gen8_border_color border_colors
[] = {
1670 [VK_BORDER_COLOR_FLOAT_TRANSPARENT_BLACK
] = { .float32
= { 0.0, 0.0, 0.0, 0.0 } },
1671 [VK_BORDER_COLOR_FLOAT_OPAQUE_BLACK
] = { .float32
= { 0.0, 0.0, 0.0, 1.0 } },
1672 [VK_BORDER_COLOR_FLOAT_OPAQUE_WHITE
] = { .float32
= { 1.0, 1.0, 1.0, 1.0 } },
1673 [VK_BORDER_COLOR_INT_TRANSPARENT_BLACK
] = { .uint32
= { 0, 0, 0, 0 } },
1674 [VK_BORDER_COLOR_INT_OPAQUE_BLACK
] = { .uint32
= { 0, 0, 0, 1 } },
1675 [VK_BORDER_COLOR_INT_OPAQUE_WHITE
] = { .uint32
= { 1, 1, 1, 1 } },
1678 device
->border_colors
= anv_state_pool_emit_data(&device
->dynamic_state_pool
,
1679 sizeof(border_colors
), 64,
1684 anv_device_init_trivial_batch(struct anv_device
*device
)
1686 anv_bo_init_new(&device
->trivial_batch_bo
, device
, 4096);
1688 if (device
->instance
->physicalDevice
.has_exec_async
)
1689 device
->trivial_batch_bo
.flags
|= EXEC_OBJECT_ASYNC
;
1691 if (device
->instance
->physicalDevice
.use_softpin
)
1692 device
->trivial_batch_bo
.flags
|= EXEC_OBJECT_PINNED
;
1694 anv_vma_alloc(device
, &device
->trivial_batch_bo
);
1696 void *map
= anv_gem_mmap(device
, device
->trivial_batch_bo
.gem_handle
,
1699 struct anv_batch batch
= {
1705 anv_batch_emit(&batch
, GEN7_MI_BATCH_BUFFER_END
, bbe
);
1706 anv_batch_emit(&batch
, GEN7_MI_NOOP
, noop
);
1708 if (!device
->info
.has_llc
)
1709 gen_clflush_range(map
, batch
.next
- map
);
1711 anv_gem_munmap(map
, device
->trivial_batch_bo
.size
);
1714 VkResult
anv_EnumerateDeviceExtensionProperties(
1715 VkPhysicalDevice physicalDevice
,
1716 const char* pLayerName
,
1717 uint32_t* pPropertyCount
,
1718 VkExtensionProperties
* pProperties
)
1720 ANV_FROM_HANDLE(anv_physical_device
, device
, physicalDevice
);
1721 VK_OUTARRAY_MAKE(out
, pProperties
, pPropertyCount
);
1723 for (int i
= 0; i
< ANV_DEVICE_EXTENSION_COUNT
; i
++) {
1724 if (device
->supported_extensions
.extensions
[i
]) {
1725 vk_outarray_append(&out
, prop
) {
1726 *prop
= anv_device_extensions
[i
];
1731 return vk_outarray_status(&out
);
1735 anv_device_init_dispatch(struct anv_device
*device
)
1737 const struct anv_device_dispatch_table
*genX_table
;
1738 switch (device
->info
.gen
) {
1740 genX_table
= &gen11_device_dispatch_table
;
1743 genX_table
= &gen10_device_dispatch_table
;
1746 genX_table
= &gen9_device_dispatch_table
;
1749 genX_table
= &gen8_device_dispatch_table
;
1752 if (device
->info
.is_haswell
)
1753 genX_table
= &gen75_device_dispatch_table
;
1755 genX_table
= &gen7_device_dispatch_table
;
1758 unreachable("unsupported gen\n");
1761 for (unsigned i
= 0; i
< ARRAY_SIZE(device
->dispatch
.entrypoints
); i
++) {
1762 /* Vulkan requires that entrypoints for extensions which have not been
1763 * enabled must not be advertised.
1765 if (!anv_device_entrypoint_is_enabled(i
, device
->instance
->app_info
.api_version
,
1766 &device
->instance
->enabled_extensions
,
1767 &device
->enabled_extensions
)) {
1768 device
->dispatch
.entrypoints
[i
] = NULL
;
1769 } else if (genX_table
->entrypoints
[i
]) {
1770 device
->dispatch
.entrypoints
[i
] = genX_table
->entrypoints
[i
];
1772 device
->dispatch
.entrypoints
[i
] =
1773 anv_device_dispatch_table
.entrypoints
[i
];
1779 vk_priority_to_gen(int priority
)
1782 case VK_QUEUE_GLOBAL_PRIORITY_LOW_EXT
:
1783 return GEN_CONTEXT_LOW_PRIORITY
;
1784 case VK_QUEUE_GLOBAL_PRIORITY_MEDIUM_EXT
:
1785 return GEN_CONTEXT_MEDIUM_PRIORITY
;
1786 case VK_QUEUE_GLOBAL_PRIORITY_HIGH_EXT
:
1787 return GEN_CONTEXT_HIGH_PRIORITY
;
1788 case VK_QUEUE_GLOBAL_PRIORITY_REALTIME_EXT
:
1789 return GEN_CONTEXT_REALTIME_PRIORITY
;
1791 unreachable("Invalid priority");
1796 anv_device_init_hiz_clear_value_bo(struct anv_device
*device
)
1798 anv_bo_init_new(&device
->hiz_clear_bo
, device
, 4096);
1800 if (device
->instance
->physicalDevice
.has_exec_async
)
1801 device
->hiz_clear_bo
.flags
|= EXEC_OBJECT_ASYNC
;
1803 if (device
->instance
->physicalDevice
.use_softpin
)
1804 device
->hiz_clear_bo
.flags
|= EXEC_OBJECT_PINNED
;
1806 anv_vma_alloc(device
, &device
->hiz_clear_bo
);
1808 uint32_t *map
= anv_gem_mmap(device
, device
->hiz_clear_bo
.gem_handle
,
1811 union isl_color_value hiz_clear
= { .u32
= { 0, } };
1812 hiz_clear
.f32
[0] = ANV_HZ_FC_VAL
;
1814 memcpy(map
, hiz_clear
.u32
, sizeof(hiz_clear
.u32
));
1815 anv_gem_munmap(map
, device
->hiz_clear_bo
.size
);
1819 get_bo_from_pool(struct gen_batch_decode_bo
*ret
,
1820 struct anv_block_pool
*pool
,
1823 for (uint32_t i
= 0; i
< pool
->nbos
; i
++) {
1824 uint64_t bo_address
= pool
->bos
[i
].offset
& (~0ull >> 16);
1825 uint32_t bo_size
= pool
->bos
[i
].size
;
1826 if (address
>= bo_address
&& address
< (bo_address
+ bo_size
)) {
1827 *ret
= (struct gen_batch_decode_bo
) {
1830 .map
= pool
->bos
[i
].map
,
1838 /* Finding a buffer for batch decoding */
1839 static struct gen_batch_decode_bo
1840 decode_get_bo(void *v_batch
, bool ppgtt
, uint64_t address
)
1842 struct anv_device
*device
= v_batch
;
1843 struct gen_batch_decode_bo ret_bo
= {};
1847 if (get_bo_from_pool(&ret_bo
, &device
->dynamic_state_pool
.block_pool
, address
))
1849 if (get_bo_from_pool(&ret_bo
, &device
->instruction_state_pool
.block_pool
, address
))
1851 if (get_bo_from_pool(&ret_bo
, &device
->binding_table_pool
.block_pool
, address
))
1853 if (get_bo_from_pool(&ret_bo
, &device
->surface_state_pool
.block_pool
, address
))
1856 if (!device
->cmd_buffer_being_decoded
)
1857 return (struct gen_batch_decode_bo
) { };
1859 struct anv_batch_bo
**bo
;
1861 u_vector_foreach(bo
, &device
->cmd_buffer_being_decoded
->seen_bbos
) {
1862 /* The decoder zeroes out the top 16 bits, so we need to as well */
1863 uint64_t bo_address
= (*bo
)->bo
.offset
& (~0ull >> 16);
1865 if (address
>= bo_address
&& address
< bo_address
+ (*bo
)->bo
.size
) {
1866 return (struct gen_batch_decode_bo
) {
1868 .size
= (*bo
)->bo
.size
,
1869 .map
= (*bo
)->bo
.map
,
1874 return (struct gen_batch_decode_bo
) { };
1877 VkResult
anv_CreateDevice(
1878 VkPhysicalDevice physicalDevice
,
1879 const VkDeviceCreateInfo
* pCreateInfo
,
1880 const VkAllocationCallbacks
* pAllocator
,
1883 ANV_FROM_HANDLE(anv_physical_device
, physical_device
, physicalDevice
);
1885 struct anv_device
*device
;
1887 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO
);
1889 struct anv_device_extension_table enabled_extensions
= { };
1890 for (uint32_t i
= 0; i
< pCreateInfo
->enabledExtensionCount
; i
++) {
1892 for (idx
= 0; idx
< ANV_DEVICE_EXTENSION_COUNT
; idx
++) {
1893 if (strcmp(pCreateInfo
->ppEnabledExtensionNames
[i
],
1894 anv_device_extensions
[idx
].extensionName
) == 0)
1898 if (idx
>= ANV_DEVICE_EXTENSION_COUNT
)
1899 return vk_error(VK_ERROR_EXTENSION_NOT_PRESENT
);
1901 if (!physical_device
->supported_extensions
.extensions
[idx
])
1902 return vk_error(VK_ERROR_EXTENSION_NOT_PRESENT
);
1904 enabled_extensions
.extensions
[idx
] = true;
1907 /* Check enabled features */
1908 if (pCreateInfo
->pEnabledFeatures
) {
1909 VkPhysicalDeviceFeatures supported_features
;
1910 anv_GetPhysicalDeviceFeatures(physicalDevice
, &supported_features
);
1911 VkBool32
*supported_feature
= (VkBool32
*)&supported_features
;
1912 VkBool32
*enabled_feature
= (VkBool32
*)pCreateInfo
->pEnabledFeatures
;
1913 unsigned num_features
= sizeof(VkPhysicalDeviceFeatures
) / sizeof(VkBool32
);
1914 for (uint32_t i
= 0; i
< num_features
; i
++) {
1915 if (enabled_feature
[i
] && !supported_feature
[i
])
1916 return vk_error(VK_ERROR_FEATURE_NOT_PRESENT
);
1920 /* Check requested queues and fail if we are requested to create any
1921 * queues with flags we don't support.
1923 assert(pCreateInfo
->queueCreateInfoCount
> 0);
1924 for (uint32_t i
= 0; i
< pCreateInfo
->queueCreateInfoCount
; i
++) {
1925 if (pCreateInfo
->pQueueCreateInfos
[i
].flags
!= 0)
1926 return vk_error(VK_ERROR_INITIALIZATION_FAILED
);
1929 /* Check if client specified queue priority. */
1930 const VkDeviceQueueGlobalPriorityCreateInfoEXT
*queue_priority
=
1931 vk_find_struct_const(pCreateInfo
->pQueueCreateInfos
[0].pNext
,
1932 DEVICE_QUEUE_GLOBAL_PRIORITY_CREATE_INFO_EXT
);
1934 VkQueueGlobalPriorityEXT priority
=
1935 queue_priority
? queue_priority
->globalPriority
:
1936 VK_QUEUE_GLOBAL_PRIORITY_MEDIUM_EXT
;
1938 device
= vk_alloc2(&physical_device
->instance
->alloc
, pAllocator
,
1940 VK_SYSTEM_ALLOCATION_SCOPE_DEVICE
);
1942 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
1944 const unsigned decode_flags
=
1945 GEN_BATCH_DECODE_FULL
|
1946 ((INTEL_DEBUG
& DEBUG_COLOR
) ? GEN_BATCH_DECODE_IN_COLOR
: 0) |
1947 GEN_BATCH_DECODE_OFFSETS
|
1948 GEN_BATCH_DECODE_FLOATS
;
1950 gen_batch_decode_ctx_init(&device
->decoder_ctx
,
1951 &physical_device
->info
,
1952 stderr
, decode_flags
, NULL
,
1953 decode_get_bo
, NULL
, device
);
1955 device
->_loader_data
.loaderMagic
= ICD_LOADER_MAGIC
;
1956 device
->instance
= physical_device
->instance
;
1957 device
->chipset_id
= physical_device
->chipset_id
;
1958 device
->no_hw
= physical_device
->no_hw
;
1959 device
->_lost
= false;
1962 device
->alloc
= *pAllocator
;
1964 device
->alloc
= physical_device
->instance
->alloc
;
1966 /* XXX(chadv): Can we dup() physicalDevice->fd here? */
1967 device
->fd
= open(physical_device
->path
, O_RDWR
| O_CLOEXEC
);
1968 if (device
->fd
== -1) {
1969 result
= vk_error(VK_ERROR_INITIALIZATION_FAILED
);
1973 device
->context_id
= anv_gem_create_context(device
);
1974 if (device
->context_id
== -1) {
1975 result
= vk_error(VK_ERROR_INITIALIZATION_FAILED
);
1979 if (physical_device
->use_softpin
) {
1980 if (pthread_mutex_init(&device
->vma_mutex
, NULL
) != 0) {
1981 result
= vk_error(VK_ERROR_INITIALIZATION_FAILED
);
1985 /* keep the page with address zero out of the allocator */
1986 struct anv_memory_heap
*low_heap
=
1987 &physical_device
->memory
.heaps
[physical_device
->memory
.heap_count
- 1];
1988 util_vma_heap_init(&device
->vma_lo
, low_heap
->vma_start
, low_heap
->vma_size
);
1989 device
->vma_lo_available
= low_heap
->size
;
1991 /* Leave the last 4GiB out of the high vma range, so that no state base
1992 * address + size can overflow 48 bits. For more information see the
1993 * comment about Wa32bitGeneralStateOffset in anv_allocator.c
1995 struct anv_memory_heap
*high_heap
=
1996 &physical_device
->memory
.heaps
[0];
1997 util_vma_heap_init(&device
->vma_hi
, high_heap
->vma_start
, high_heap
->vma_size
);
1998 device
->vma_hi_available
= physical_device
->memory
.heap_count
== 1 ? 0 :
2002 /* As per spec, the driver implementation may deny requests to acquire
2003 * a priority above the default priority (MEDIUM) if the caller does not
2004 * have sufficient privileges. In this scenario VK_ERROR_NOT_PERMITTED_EXT
2007 if (physical_device
->has_context_priority
) {
2008 int err
= anv_gem_set_context_param(device
->fd
, device
->context_id
,
2009 I915_CONTEXT_PARAM_PRIORITY
,
2010 vk_priority_to_gen(priority
));
2011 if (err
!= 0 && priority
> VK_QUEUE_GLOBAL_PRIORITY_MEDIUM_EXT
) {
2012 result
= vk_error(VK_ERROR_NOT_PERMITTED_EXT
);
2017 device
->info
= physical_device
->info
;
2018 device
->isl_dev
= physical_device
->isl_dev
;
2020 /* On Broadwell and later, we can use batch chaining to more efficiently
2021 * implement growing command buffers. Prior to Haswell, the kernel
2022 * command parser gets in the way and we have to fall back to growing
2025 device
->can_chain_batches
= device
->info
.gen
>= 8;
2027 device
->robust_buffer_access
= pCreateInfo
->pEnabledFeatures
&&
2028 pCreateInfo
->pEnabledFeatures
->robustBufferAccess
;
2029 device
->enabled_extensions
= enabled_extensions
;
2031 anv_device_init_dispatch(device
);
2033 if (pthread_mutex_init(&device
->mutex
, NULL
) != 0) {
2034 result
= vk_error(VK_ERROR_INITIALIZATION_FAILED
);
2035 goto fail_context_id
;
2038 pthread_condattr_t condattr
;
2039 if (pthread_condattr_init(&condattr
) != 0) {
2040 result
= vk_error(VK_ERROR_INITIALIZATION_FAILED
);
2043 if (pthread_condattr_setclock(&condattr
, CLOCK_MONOTONIC
) != 0) {
2044 pthread_condattr_destroy(&condattr
);
2045 result
= vk_error(VK_ERROR_INITIALIZATION_FAILED
);
2048 if (pthread_cond_init(&device
->queue_submit
, NULL
) != 0) {
2049 pthread_condattr_destroy(&condattr
);
2050 result
= vk_error(VK_ERROR_INITIALIZATION_FAILED
);
2053 pthread_condattr_destroy(&condattr
);
2056 (physical_device
->supports_48bit_addresses
? EXEC_OBJECT_SUPPORTS_48B_ADDRESS
: 0) |
2057 (physical_device
->has_exec_async
? EXEC_OBJECT_ASYNC
: 0) |
2058 (physical_device
->has_exec_capture
? EXEC_OBJECT_CAPTURE
: 0) |
2059 (physical_device
->use_softpin
? EXEC_OBJECT_PINNED
: 0);
2061 anv_bo_pool_init(&device
->batch_bo_pool
, device
, bo_flags
);
2063 result
= anv_bo_cache_init(&device
->bo_cache
);
2064 if (result
!= VK_SUCCESS
)
2065 goto fail_batch_bo_pool
;
2067 if (!physical_device
->use_softpin
)
2068 bo_flags
&= ~EXEC_OBJECT_SUPPORTS_48B_ADDRESS
;
2070 result
= anv_state_pool_init(&device
->dynamic_state_pool
, device
,
2071 DYNAMIC_STATE_POOL_MIN_ADDRESS
,
2074 if (result
!= VK_SUCCESS
)
2077 result
= anv_state_pool_init(&device
->instruction_state_pool
, device
,
2078 INSTRUCTION_STATE_POOL_MIN_ADDRESS
,
2081 if (result
!= VK_SUCCESS
)
2082 goto fail_dynamic_state_pool
;
2084 result
= anv_state_pool_init(&device
->surface_state_pool
, device
,
2085 SURFACE_STATE_POOL_MIN_ADDRESS
,
2088 if (result
!= VK_SUCCESS
)
2089 goto fail_instruction_state_pool
;
2091 if (physical_device
->use_softpin
) {
2092 result
= anv_state_pool_init(&device
->binding_table_pool
, device
,
2093 BINDING_TABLE_POOL_MIN_ADDRESS
,
2096 if (result
!= VK_SUCCESS
)
2097 goto fail_surface_state_pool
;
2100 result
= anv_bo_init_new(&device
->workaround_bo
, device
, 1024);
2101 if (result
!= VK_SUCCESS
)
2102 goto fail_binding_table_pool
;
2104 if (physical_device
->use_softpin
)
2105 device
->workaround_bo
.flags
|= EXEC_OBJECT_PINNED
;
2107 if (!anv_vma_alloc(device
, &device
->workaround_bo
))
2108 goto fail_workaround_bo
;
2110 anv_device_init_trivial_batch(device
);
2112 if (device
->info
.gen
>= 10)
2113 anv_device_init_hiz_clear_value_bo(device
);
2115 if (physical_device
->use_softpin
)
2116 device
->pinned_buffers
= _mesa_pointer_set_create(NULL
);
2118 anv_scratch_pool_init(device
, &device
->scratch_pool
);
2120 anv_queue_init(device
, &device
->queue
);
2122 switch (device
->info
.gen
) {
2124 if (!device
->info
.is_haswell
)
2125 result
= gen7_init_device_state(device
);
2127 result
= gen75_init_device_state(device
);
2130 result
= gen8_init_device_state(device
);
2133 result
= gen9_init_device_state(device
);
2136 result
= gen10_init_device_state(device
);
2139 result
= gen11_init_device_state(device
);
2142 /* Shouldn't get here as we don't create physical devices for any other
2144 unreachable("unhandled gen");
2146 if (result
!= VK_SUCCESS
)
2147 goto fail_workaround_bo
;
2149 anv_pipeline_cache_init(&device
->default_pipeline_cache
, device
, true);
2151 anv_device_init_blorp(device
);
2153 anv_device_init_border_colors(device
);
2155 *pDevice
= anv_device_to_handle(device
);
2160 anv_queue_finish(&device
->queue
);
2161 anv_scratch_pool_finish(device
, &device
->scratch_pool
);
2162 anv_gem_munmap(device
->workaround_bo
.map
, device
->workaround_bo
.size
);
2163 anv_gem_close(device
, device
->workaround_bo
.gem_handle
);
2164 fail_binding_table_pool
:
2165 if (physical_device
->use_softpin
)
2166 anv_state_pool_finish(&device
->binding_table_pool
);
2167 fail_surface_state_pool
:
2168 anv_state_pool_finish(&device
->surface_state_pool
);
2169 fail_instruction_state_pool
:
2170 anv_state_pool_finish(&device
->instruction_state_pool
);
2171 fail_dynamic_state_pool
:
2172 anv_state_pool_finish(&device
->dynamic_state_pool
);
2174 anv_bo_cache_finish(&device
->bo_cache
);
2176 anv_bo_pool_finish(&device
->batch_bo_pool
);
2177 pthread_cond_destroy(&device
->queue_submit
);
2179 pthread_mutex_destroy(&device
->mutex
);
2181 anv_gem_destroy_context(device
, device
->context_id
);
2185 vk_free(&device
->alloc
, device
);
2190 void anv_DestroyDevice(
2192 const VkAllocationCallbacks
* pAllocator
)
2194 ANV_FROM_HANDLE(anv_device
, device
, _device
);
2195 struct anv_physical_device
*physical_device
;
2200 physical_device
= &device
->instance
->physicalDevice
;
2202 anv_device_finish_blorp(device
);
2204 anv_pipeline_cache_finish(&device
->default_pipeline_cache
);
2206 anv_queue_finish(&device
->queue
);
2208 if (physical_device
->use_softpin
)
2209 _mesa_set_destroy(device
->pinned_buffers
, NULL
);
2211 #ifdef HAVE_VALGRIND
2212 /* We only need to free these to prevent valgrind errors. The backing
2213 * BO will go away in a couple of lines so we don't actually leak.
2215 anv_state_pool_free(&device
->dynamic_state_pool
, device
->border_colors
);
2218 anv_scratch_pool_finish(device
, &device
->scratch_pool
);
2220 anv_gem_munmap(device
->workaround_bo
.map
, device
->workaround_bo
.size
);
2221 anv_vma_free(device
, &device
->workaround_bo
);
2222 anv_gem_close(device
, device
->workaround_bo
.gem_handle
);
2224 anv_vma_free(device
, &device
->trivial_batch_bo
);
2225 anv_gem_close(device
, device
->trivial_batch_bo
.gem_handle
);
2226 if (device
->info
.gen
>= 10)
2227 anv_gem_close(device
, device
->hiz_clear_bo
.gem_handle
);
2229 if (physical_device
->use_softpin
)
2230 anv_state_pool_finish(&device
->binding_table_pool
);
2231 anv_state_pool_finish(&device
->surface_state_pool
);
2232 anv_state_pool_finish(&device
->instruction_state_pool
);
2233 anv_state_pool_finish(&device
->dynamic_state_pool
);
2235 anv_bo_cache_finish(&device
->bo_cache
);
2237 anv_bo_pool_finish(&device
->batch_bo_pool
);
2239 pthread_cond_destroy(&device
->queue_submit
);
2240 pthread_mutex_destroy(&device
->mutex
);
2242 anv_gem_destroy_context(device
, device
->context_id
);
2244 gen_batch_decode_ctx_finish(&device
->decoder_ctx
);
2248 vk_free(&device
->alloc
, device
);
2251 VkResult
anv_EnumerateInstanceLayerProperties(
2252 uint32_t* pPropertyCount
,
2253 VkLayerProperties
* pProperties
)
2255 if (pProperties
== NULL
) {
2256 *pPropertyCount
= 0;
2260 /* None supported at this time */
2261 return vk_error(VK_ERROR_LAYER_NOT_PRESENT
);
2264 VkResult
anv_EnumerateDeviceLayerProperties(
2265 VkPhysicalDevice physicalDevice
,
2266 uint32_t* pPropertyCount
,
2267 VkLayerProperties
* pProperties
)
2269 if (pProperties
== NULL
) {
2270 *pPropertyCount
= 0;
2274 /* None supported at this time */
2275 return vk_error(VK_ERROR_LAYER_NOT_PRESENT
);
2278 void anv_GetDeviceQueue(
2280 uint32_t queueNodeIndex
,
2281 uint32_t queueIndex
,
2284 ANV_FROM_HANDLE(anv_device
, device
, _device
);
2286 assert(queueIndex
== 0);
2288 *pQueue
= anv_queue_to_handle(&device
->queue
);
2291 void anv_GetDeviceQueue2(
2293 const VkDeviceQueueInfo2
* pQueueInfo
,
2296 ANV_FROM_HANDLE(anv_device
, device
, _device
);
2298 assert(pQueueInfo
->queueIndex
== 0);
2300 if (pQueueInfo
->flags
== device
->queue
.flags
)
2301 *pQueue
= anv_queue_to_handle(&device
->queue
);
2307 _anv_device_set_lost(struct anv_device
*device
,
2308 const char *file
, int line
,
2309 const char *msg
, ...)
2314 device
->_lost
= true;
2317 err
= __vk_errorv(device
->instance
, device
,
2318 VK_DEBUG_REPORT_OBJECT_TYPE_DEVICE_EXT
,
2319 VK_ERROR_DEVICE_LOST
, file
, line
, msg
, ap
);
2322 if (env_var_as_boolean("ANV_ABORT_ON_DEVICE_LOSS", false))
2329 anv_device_query_status(struct anv_device
*device
)
2331 /* This isn't likely as most of the callers of this function already check
2332 * for it. However, it doesn't hurt to check and it potentially lets us
2335 if (anv_device_is_lost(device
))
2336 return VK_ERROR_DEVICE_LOST
;
2338 uint32_t active
, pending
;
2339 int ret
= anv_gem_gpu_get_reset_stats(device
, &active
, &pending
);
2341 /* We don't know the real error. */
2342 return anv_device_set_lost(device
, "get_reset_stats failed: %m");
2346 return anv_device_set_lost(device
, "GPU hung on one of our command buffers");
2347 } else if (pending
) {
2348 return anv_device_set_lost(device
, "GPU hung with commands in-flight");
2355 anv_device_bo_busy(struct anv_device
*device
, struct anv_bo
*bo
)
2357 /* Note: This only returns whether or not the BO is in use by an i915 GPU.
2358 * Other usages of the BO (such as on different hardware) will not be
2359 * flagged as "busy" by this ioctl. Use with care.
2361 int ret
= anv_gem_busy(device
, bo
->gem_handle
);
2363 return VK_NOT_READY
;
2364 } else if (ret
== -1) {
2365 /* We don't know the real error. */
2366 return anv_device_set_lost(device
, "gem wait failed: %m");
2369 /* Query for device status after the busy call. If the BO we're checking
2370 * got caught in a GPU hang we don't want to return VK_SUCCESS to the
2371 * client because it clearly doesn't have valid data. Yes, this most
2372 * likely means an ioctl, but we just did an ioctl to query the busy status
2373 * so it's no great loss.
2375 return anv_device_query_status(device
);
2379 anv_device_wait(struct anv_device
*device
, struct anv_bo
*bo
,
2382 int ret
= anv_gem_wait(device
, bo
->gem_handle
, &timeout
);
2383 if (ret
== -1 && errno
== ETIME
) {
2385 } else if (ret
== -1) {
2386 /* We don't know the real error. */
2387 return anv_device_set_lost(device
, "gem wait failed: %m");
2390 /* Query for device status after the wait. If the BO we're waiting on got
2391 * caught in a GPU hang we don't want to return VK_SUCCESS to the client
2392 * because it clearly doesn't have valid data. Yes, this most likely means
2393 * an ioctl, but we just did an ioctl to wait so it's no great loss.
2395 return anv_device_query_status(device
);
2398 VkResult
anv_DeviceWaitIdle(
2401 ANV_FROM_HANDLE(anv_device
, device
, _device
);
2402 if (anv_device_is_lost(device
))
2403 return VK_ERROR_DEVICE_LOST
;
2405 struct anv_batch batch
;
2408 batch
.start
= batch
.next
= cmds
;
2409 batch
.end
= (void *) cmds
+ sizeof(cmds
);
2411 anv_batch_emit(&batch
, GEN7_MI_BATCH_BUFFER_END
, bbe
);
2412 anv_batch_emit(&batch
, GEN7_MI_NOOP
, noop
);
2414 return anv_device_submit_simple_batch(device
, &batch
);
2418 anv_vma_alloc(struct anv_device
*device
, struct anv_bo
*bo
)
2420 if (!(bo
->flags
& EXEC_OBJECT_PINNED
))
2423 pthread_mutex_lock(&device
->vma_mutex
);
2427 if (bo
->flags
& EXEC_OBJECT_SUPPORTS_48B_ADDRESS
&&
2428 device
->vma_hi_available
>= bo
->size
) {
2429 uint64_t addr
= util_vma_heap_alloc(&device
->vma_hi
, bo
->size
, 4096);
2431 bo
->offset
= gen_canonical_address(addr
);
2432 assert(addr
== gen_48b_address(bo
->offset
));
2433 device
->vma_hi_available
-= bo
->size
;
2437 if (bo
->offset
== 0 && device
->vma_lo_available
>= bo
->size
) {
2438 uint64_t addr
= util_vma_heap_alloc(&device
->vma_lo
, bo
->size
, 4096);
2440 bo
->offset
= gen_canonical_address(addr
);
2441 assert(addr
== gen_48b_address(bo
->offset
));
2442 device
->vma_lo_available
-= bo
->size
;
2446 pthread_mutex_unlock(&device
->vma_mutex
);
2448 return bo
->offset
!= 0;
2452 anv_vma_free(struct anv_device
*device
, struct anv_bo
*bo
)
2454 if (!(bo
->flags
& EXEC_OBJECT_PINNED
))
2457 const uint64_t addr_48b
= gen_48b_address(bo
->offset
);
2459 pthread_mutex_lock(&device
->vma_mutex
);
2461 if (addr_48b
>= LOW_HEAP_MIN_ADDRESS
&&
2462 addr_48b
<= LOW_HEAP_MAX_ADDRESS
) {
2463 util_vma_heap_free(&device
->vma_lo
, addr_48b
, bo
->size
);
2464 device
->vma_lo_available
+= bo
->size
;
2466 MAYBE_UNUSED
const struct anv_physical_device
*physical_device
=
2467 &device
->instance
->physicalDevice
;
2468 assert(addr_48b
>= physical_device
->memory
.heaps
[0].vma_start
&&
2469 addr_48b
< (physical_device
->memory
.heaps
[0].vma_start
+
2470 physical_device
->memory
.heaps
[0].vma_size
));
2471 util_vma_heap_free(&device
->vma_hi
, addr_48b
, bo
->size
);
2472 device
->vma_hi_available
+= bo
->size
;
2475 pthread_mutex_unlock(&device
->vma_mutex
);
2481 anv_bo_init_new(struct anv_bo
*bo
, struct anv_device
*device
, uint64_t size
)
2483 uint32_t gem_handle
= anv_gem_create(device
, size
);
2485 return vk_error(VK_ERROR_OUT_OF_DEVICE_MEMORY
);
2487 anv_bo_init(bo
, gem_handle
, size
);
2492 VkResult
anv_AllocateMemory(
2494 const VkMemoryAllocateInfo
* pAllocateInfo
,
2495 const VkAllocationCallbacks
* pAllocator
,
2496 VkDeviceMemory
* pMem
)
2498 ANV_FROM_HANDLE(anv_device
, device
, _device
);
2499 struct anv_physical_device
*pdevice
= &device
->instance
->physicalDevice
;
2500 struct anv_device_memory
*mem
;
2501 VkResult result
= VK_SUCCESS
;
2503 assert(pAllocateInfo
->sType
== VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO
);
2505 /* The Vulkan 1.0.33 spec says "allocationSize must be greater than 0". */
2506 assert(pAllocateInfo
->allocationSize
> 0);
2508 if (pAllocateInfo
->allocationSize
> MAX_MEMORY_ALLOCATION_SIZE
)
2509 return VK_ERROR_OUT_OF_DEVICE_MEMORY
;
2511 /* FINISHME: Fail if allocation request exceeds heap size. */
2513 mem
= vk_alloc2(&device
->alloc
, pAllocator
, sizeof(*mem
), 8,
2514 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
2516 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
2518 assert(pAllocateInfo
->memoryTypeIndex
< pdevice
->memory
.type_count
);
2519 mem
->type
= &pdevice
->memory
.types
[pAllocateInfo
->memoryTypeIndex
];
2523 mem
->host_ptr
= NULL
;
2525 uint64_t bo_flags
= 0;
2527 assert(mem
->type
->heapIndex
< pdevice
->memory
.heap_count
);
2528 if (pdevice
->memory
.heaps
[mem
->type
->heapIndex
].supports_48bit_addresses
)
2529 bo_flags
|= EXEC_OBJECT_SUPPORTS_48B_ADDRESS
;
2531 const struct wsi_memory_allocate_info
*wsi_info
=
2532 vk_find_struct_const(pAllocateInfo
->pNext
, WSI_MEMORY_ALLOCATE_INFO_MESA
);
2533 if (wsi_info
&& wsi_info
->implicit_sync
) {
2534 /* We need to set the WRITE flag on window system buffers so that GEM
2535 * will know we're writing to them and synchronize uses on other rings
2536 * (eg if the display server uses the blitter ring).
2538 bo_flags
|= EXEC_OBJECT_WRITE
;
2539 } else if (pdevice
->has_exec_async
) {
2540 bo_flags
|= EXEC_OBJECT_ASYNC
;
2543 if (pdevice
->use_softpin
)
2544 bo_flags
|= EXEC_OBJECT_PINNED
;
2546 const VkExportMemoryAllocateInfo
*export_info
=
2547 vk_find_struct_const(pAllocateInfo
->pNext
, EXPORT_MEMORY_ALLOCATE_INFO
);
2549 /* Check if we need to support Android HW buffer export. If so,
2550 * create AHardwareBuffer and import memory from it.
2552 bool android_export
= false;
2553 if (export_info
&& export_info
->handleTypes
&
2554 VK_EXTERNAL_MEMORY_HANDLE_TYPE_ANDROID_HARDWARE_BUFFER_BIT_ANDROID
)
2555 android_export
= true;
2557 /* Android memory import. */
2558 const struct VkImportAndroidHardwareBufferInfoANDROID
*ahw_import_info
=
2559 vk_find_struct_const(pAllocateInfo
->pNext
,
2560 IMPORT_ANDROID_HARDWARE_BUFFER_INFO_ANDROID
);
2562 if (ahw_import_info
) {
2563 result
= anv_import_ahw_memory(_device
, mem
, ahw_import_info
);
2564 if (result
!= VK_SUCCESS
)
2568 } else if (android_export
) {
2569 result
= anv_create_ahw_memory(_device
, mem
, pAllocateInfo
);
2570 if (result
!= VK_SUCCESS
)
2573 const struct VkImportAndroidHardwareBufferInfoANDROID import_info
= {
2576 result
= anv_import_ahw_memory(_device
, mem
, &import_info
);
2577 if (result
!= VK_SUCCESS
)
2583 const VkImportMemoryFdInfoKHR
*fd_info
=
2584 vk_find_struct_const(pAllocateInfo
->pNext
, IMPORT_MEMORY_FD_INFO_KHR
);
2586 /* The Vulkan spec permits handleType to be 0, in which case the struct is
2589 if (fd_info
&& fd_info
->handleType
) {
2590 /* At the moment, we support only the below handle types. */
2591 assert(fd_info
->handleType
==
2592 VK_EXTERNAL_MEMORY_HANDLE_TYPE_OPAQUE_FD_BIT
||
2593 fd_info
->handleType
==
2594 VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_EXT
);
2596 result
= anv_bo_cache_import(device
, &device
->bo_cache
, fd_info
->fd
,
2597 bo_flags
| ANV_BO_EXTERNAL
, &mem
->bo
);
2598 if (result
!= VK_SUCCESS
)
2601 VkDeviceSize aligned_alloc_size
=
2602 align_u64(pAllocateInfo
->allocationSize
, 4096);
2604 /* For security purposes, we reject importing the bo if it's smaller
2605 * than the requested allocation size. This prevents a malicious client
2606 * from passing a buffer to a trusted client, lying about the size, and
2607 * telling the trusted client to try and texture from an image that goes
2608 * out-of-bounds. This sort of thing could lead to GPU hangs or worse
2609 * in the trusted client. The trusted client can protect itself against
2610 * this sort of attack but only if it can trust the buffer size.
2612 if (mem
->bo
->size
< aligned_alloc_size
) {
2613 result
= vk_errorf(device
->instance
, device
,
2614 VK_ERROR_INVALID_EXTERNAL_HANDLE
,
2615 "aligned allocationSize too large for "
2616 "VK_EXTERNAL_MEMORY_HANDLE_TYPE_OPAQUE_FD_BIT: "
2617 "%"PRIu64
"B > %"PRIu64
"B",
2618 aligned_alloc_size
, mem
->bo
->size
);
2619 anv_bo_cache_release(device
, &device
->bo_cache
, mem
->bo
);
2623 /* From the Vulkan spec:
2625 * "Importing memory from a file descriptor transfers ownership of
2626 * the file descriptor from the application to the Vulkan
2627 * implementation. The application must not perform any operations on
2628 * the file descriptor after a successful import."
2630 * If the import fails, we leave the file descriptor open.
2636 const VkImportMemoryHostPointerInfoEXT
*host_ptr_info
=
2637 vk_find_struct_const(pAllocateInfo
->pNext
,
2638 IMPORT_MEMORY_HOST_POINTER_INFO_EXT
);
2639 if (host_ptr_info
&& host_ptr_info
->handleType
) {
2640 if (host_ptr_info
->handleType
==
2641 VK_EXTERNAL_MEMORY_HANDLE_TYPE_HOST_MAPPED_FOREIGN_MEMORY_BIT_EXT
) {
2642 result
= vk_error(VK_ERROR_INVALID_EXTERNAL_HANDLE
);
2646 assert(host_ptr_info
->handleType
==
2647 VK_EXTERNAL_MEMORY_HANDLE_TYPE_HOST_ALLOCATION_BIT_EXT
);
2649 result
= anv_bo_cache_import_host_ptr(
2650 device
, &device
->bo_cache
, host_ptr_info
->pHostPointer
,
2651 pAllocateInfo
->allocationSize
, bo_flags
, &mem
->bo
);
2653 if (result
!= VK_SUCCESS
)
2656 mem
->host_ptr
= host_ptr_info
->pHostPointer
;
2660 /* Regular allocate (not importing memory). */
2662 if (export_info
&& export_info
->handleTypes
)
2663 bo_flags
|= ANV_BO_EXTERNAL
;
2665 result
= anv_bo_cache_alloc(device
, &device
->bo_cache
,
2666 pAllocateInfo
->allocationSize
, bo_flags
,
2668 if (result
!= VK_SUCCESS
)
2671 const VkMemoryDedicatedAllocateInfo
*dedicated_info
=
2672 vk_find_struct_const(pAllocateInfo
->pNext
, MEMORY_DEDICATED_ALLOCATE_INFO
);
2673 if (dedicated_info
&& dedicated_info
->image
!= VK_NULL_HANDLE
) {
2674 ANV_FROM_HANDLE(anv_image
, image
, dedicated_info
->image
);
2676 /* Some legacy (non-modifiers) consumers need the tiling to be set on
2677 * the BO. In this case, we have a dedicated allocation.
2679 if (image
->needs_set_tiling
) {
2680 const uint32_t i915_tiling
=
2681 isl_tiling_to_i915_tiling(image
->planes
[0].surface
.isl
.tiling
);
2682 int ret
= anv_gem_set_tiling(device
, mem
->bo
->gem_handle
,
2683 image
->planes
[0].surface
.isl
.row_pitch_B
,
2686 anv_bo_cache_release(device
, &device
->bo_cache
, mem
->bo
);
2687 return vk_errorf(device
->instance
, NULL
,
2688 VK_ERROR_OUT_OF_DEVICE_MEMORY
,
2689 "failed to set BO tiling: %m");
2695 *pMem
= anv_device_memory_to_handle(mem
);
2700 vk_free2(&device
->alloc
, pAllocator
, mem
);
2705 VkResult
anv_GetMemoryFdKHR(
2707 const VkMemoryGetFdInfoKHR
* pGetFdInfo
,
2710 ANV_FROM_HANDLE(anv_device
, dev
, device_h
);
2711 ANV_FROM_HANDLE(anv_device_memory
, mem
, pGetFdInfo
->memory
);
2713 assert(pGetFdInfo
->sType
== VK_STRUCTURE_TYPE_MEMORY_GET_FD_INFO_KHR
);
2715 assert(pGetFdInfo
->handleType
== VK_EXTERNAL_MEMORY_HANDLE_TYPE_OPAQUE_FD_BIT
||
2716 pGetFdInfo
->handleType
== VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_EXT
);
2718 return anv_bo_cache_export(dev
, &dev
->bo_cache
, mem
->bo
, pFd
);
2721 VkResult
anv_GetMemoryFdPropertiesKHR(
2723 VkExternalMemoryHandleTypeFlagBits handleType
,
2725 VkMemoryFdPropertiesKHR
* pMemoryFdProperties
)
2727 ANV_FROM_HANDLE(anv_device
, device
, _device
);
2728 struct anv_physical_device
*pdevice
= &device
->instance
->physicalDevice
;
2730 switch (handleType
) {
2731 case VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_EXT
:
2732 /* dma-buf can be imported as any memory type */
2733 pMemoryFdProperties
->memoryTypeBits
=
2734 (1 << pdevice
->memory
.type_count
) - 1;
2738 /* The valid usage section for this function says:
2740 * "handleType must not be one of the handle types defined as
2743 * So opaque handle types fall into the default "unsupported" case.
2745 return vk_error(VK_ERROR_INVALID_EXTERNAL_HANDLE
);
2749 VkResult
anv_GetMemoryHostPointerPropertiesEXT(
2751 VkExternalMemoryHandleTypeFlagBits handleType
,
2752 const void* pHostPointer
,
2753 VkMemoryHostPointerPropertiesEXT
* pMemoryHostPointerProperties
)
2755 ANV_FROM_HANDLE(anv_device
, device
, _device
);
2757 assert(pMemoryHostPointerProperties
->sType
==
2758 VK_STRUCTURE_TYPE_MEMORY_HOST_POINTER_PROPERTIES_EXT
);
2760 switch (handleType
) {
2761 case VK_EXTERNAL_MEMORY_HANDLE_TYPE_HOST_ALLOCATION_BIT_EXT
: {
2762 struct anv_physical_device
*pdevice
= &device
->instance
->physicalDevice
;
2764 /* Host memory can be imported as any memory type. */
2765 pMemoryHostPointerProperties
->memoryTypeBits
=
2766 (1ull << pdevice
->memory
.type_count
) - 1;
2771 return VK_ERROR_INVALID_EXTERNAL_HANDLE
;
2775 void anv_FreeMemory(
2777 VkDeviceMemory _mem
,
2778 const VkAllocationCallbacks
* pAllocator
)
2780 ANV_FROM_HANDLE(anv_device
, device
, _device
);
2781 ANV_FROM_HANDLE(anv_device_memory
, mem
, _mem
);
2787 anv_UnmapMemory(_device
, _mem
);
2789 anv_bo_cache_release(device
, &device
->bo_cache
, mem
->bo
);
2791 #if defined(ANDROID) && ANDROID_API_LEVEL >= 26
2793 AHardwareBuffer_release(mem
->ahw
);
2796 vk_free2(&device
->alloc
, pAllocator
, mem
);
2799 VkResult
anv_MapMemory(
2801 VkDeviceMemory _memory
,
2802 VkDeviceSize offset
,
2804 VkMemoryMapFlags flags
,
2807 ANV_FROM_HANDLE(anv_device
, device
, _device
);
2808 ANV_FROM_HANDLE(anv_device_memory
, mem
, _memory
);
2815 if (mem
->host_ptr
) {
2816 *ppData
= mem
->host_ptr
+ offset
;
2820 if (size
== VK_WHOLE_SIZE
)
2821 size
= mem
->bo
->size
- offset
;
2823 /* From the Vulkan spec version 1.0.32 docs for MapMemory:
2825 * * If size is not equal to VK_WHOLE_SIZE, size must be greater than 0
2826 * assert(size != 0);
2827 * * If size is not equal to VK_WHOLE_SIZE, size must be less than or
2828 * equal to the size of the memory minus offset
2831 assert(offset
+ size
<= mem
->bo
->size
);
2833 /* FIXME: Is this supposed to be thread safe? Since vkUnmapMemory() only
2834 * takes a VkDeviceMemory pointer, it seems like only one map of the memory
2835 * at a time is valid. We could just mmap up front and return an offset
2836 * pointer here, but that may exhaust virtual memory on 32 bit
2839 uint32_t gem_flags
= 0;
2841 if (!device
->info
.has_llc
&&
2842 (mem
->type
->propertyFlags
& VK_MEMORY_PROPERTY_HOST_COHERENT_BIT
))
2843 gem_flags
|= I915_MMAP_WC
;
2845 /* GEM will fail to map if the offset isn't 4k-aligned. Round down. */
2846 uint64_t map_offset
= offset
& ~4095ull;
2847 assert(offset
>= map_offset
);
2848 uint64_t map_size
= (offset
+ size
) - map_offset
;
2850 /* Let's map whole pages */
2851 map_size
= align_u64(map_size
, 4096);
2853 void *map
= anv_gem_mmap(device
, mem
->bo
->gem_handle
,
2854 map_offset
, map_size
, gem_flags
);
2855 if (map
== MAP_FAILED
)
2856 return vk_error(VK_ERROR_MEMORY_MAP_FAILED
);
2859 mem
->map_size
= map_size
;
2861 *ppData
= mem
->map
+ (offset
- map_offset
);
2866 void anv_UnmapMemory(
2868 VkDeviceMemory _memory
)
2870 ANV_FROM_HANDLE(anv_device_memory
, mem
, _memory
);
2872 if (mem
== NULL
|| mem
->host_ptr
)
2875 anv_gem_munmap(mem
->map
, mem
->map_size
);
2882 clflush_mapped_ranges(struct anv_device
*device
,
2884 const VkMappedMemoryRange
*ranges
)
2886 for (uint32_t i
= 0; i
< count
; i
++) {
2887 ANV_FROM_HANDLE(anv_device_memory
, mem
, ranges
[i
].memory
);
2888 if (ranges
[i
].offset
>= mem
->map_size
)
2891 gen_clflush_range(mem
->map
+ ranges
[i
].offset
,
2892 MIN2(ranges
[i
].size
, mem
->map_size
- ranges
[i
].offset
));
2896 VkResult
anv_FlushMappedMemoryRanges(
2898 uint32_t memoryRangeCount
,
2899 const VkMappedMemoryRange
* pMemoryRanges
)
2901 ANV_FROM_HANDLE(anv_device
, device
, _device
);
2903 if (device
->info
.has_llc
)
2906 /* Make sure the writes we're flushing have landed. */
2907 __builtin_ia32_mfence();
2909 clflush_mapped_ranges(device
, memoryRangeCount
, pMemoryRanges
);
2914 VkResult
anv_InvalidateMappedMemoryRanges(
2916 uint32_t memoryRangeCount
,
2917 const VkMappedMemoryRange
* pMemoryRanges
)
2919 ANV_FROM_HANDLE(anv_device
, device
, _device
);
2921 if (device
->info
.has_llc
)
2924 clflush_mapped_ranges(device
, memoryRangeCount
, pMemoryRanges
);
2926 /* Make sure no reads get moved up above the invalidate. */
2927 __builtin_ia32_mfence();
2932 void anv_GetBufferMemoryRequirements(
2935 VkMemoryRequirements
* pMemoryRequirements
)
2937 ANV_FROM_HANDLE(anv_buffer
, buffer
, _buffer
);
2938 ANV_FROM_HANDLE(anv_device
, device
, _device
);
2939 struct anv_physical_device
*pdevice
= &device
->instance
->physicalDevice
;
2941 /* The Vulkan spec (git aaed022) says:
2943 * memoryTypeBits is a bitfield and contains one bit set for every
2944 * supported memory type for the resource. The bit `1<<i` is set if and
2945 * only if the memory type `i` in the VkPhysicalDeviceMemoryProperties
2946 * structure for the physical device is supported.
2948 uint32_t memory_types
= 0;
2949 for (uint32_t i
= 0; i
< pdevice
->memory
.type_count
; i
++) {
2950 uint32_t valid_usage
= pdevice
->memory
.types
[i
].valid_buffer_usage
;
2951 if ((valid_usage
& buffer
->usage
) == buffer
->usage
)
2952 memory_types
|= (1u << i
);
2955 /* Base alignment requirement of a cache line */
2956 uint32_t alignment
= 16;
2958 /* We need an alignment of 32 for pushing UBOs */
2959 if (buffer
->usage
& VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT
)
2960 alignment
= MAX2(alignment
, 32);
2962 pMemoryRequirements
->size
= buffer
->size
;
2963 pMemoryRequirements
->alignment
= alignment
;
2965 /* Storage and Uniform buffers should have their size aligned to
2966 * 32-bits to avoid boundary checks when last DWord is not complete.
2967 * This would ensure that not internal padding would be needed for
2970 if (device
->robust_buffer_access
&&
2971 (buffer
->usage
& VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT
||
2972 buffer
->usage
& VK_BUFFER_USAGE_STORAGE_BUFFER_BIT
))
2973 pMemoryRequirements
->size
= align_u64(buffer
->size
, 4);
2975 pMemoryRequirements
->memoryTypeBits
= memory_types
;
2978 void anv_GetBufferMemoryRequirements2(
2980 const VkBufferMemoryRequirementsInfo2
* pInfo
,
2981 VkMemoryRequirements2
* pMemoryRequirements
)
2983 anv_GetBufferMemoryRequirements(_device
, pInfo
->buffer
,
2984 &pMemoryRequirements
->memoryRequirements
);
2986 vk_foreach_struct(ext
, pMemoryRequirements
->pNext
) {
2987 switch (ext
->sType
) {
2988 case VK_STRUCTURE_TYPE_MEMORY_DEDICATED_REQUIREMENTS
: {
2989 VkMemoryDedicatedRequirements
*requirements
= (void *)ext
;
2990 requirements
->prefersDedicatedAllocation
= false;
2991 requirements
->requiresDedicatedAllocation
= false;
2996 anv_debug_ignored_stype(ext
->sType
);
3002 void anv_GetImageMemoryRequirements(
3005 VkMemoryRequirements
* pMemoryRequirements
)
3007 ANV_FROM_HANDLE(anv_image
, image
, _image
);
3008 ANV_FROM_HANDLE(anv_device
, device
, _device
);
3009 struct anv_physical_device
*pdevice
= &device
->instance
->physicalDevice
;
3011 /* The Vulkan spec (git aaed022) says:
3013 * memoryTypeBits is a bitfield and contains one bit set for every
3014 * supported memory type for the resource. The bit `1<<i` is set if and
3015 * only if the memory type `i` in the VkPhysicalDeviceMemoryProperties
3016 * structure for the physical device is supported.
3018 * All types are currently supported for images.
3020 uint32_t memory_types
= (1ull << pdevice
->memory
.type_count
) - 1;
3022 /* We must have image allocated or imported at this point. According to the
3023 * specification, external images must have been bound to memory before
3024 * calling GetImageMemoryRequirements.
3026 assert(image
->size
> 0);
3028 pMemoryRequirements
->size
= image
->size
;
3029 pMemoryRequirements
->alignment
= image
->alignment
;
3030 pMemoryRequirements
->memoryTypeBits
= memory_types
;
3033 void anv_GetImageMemoryRequirements2(
3035 const VkImageMemoryRequirementsInfo2
* pInfo
,
3036 VkMemoryRequirements2
* pMemoryRequirements
)
3038 ANV_FROM_HANDLE(anv_device
, device
, _device
);
3039 ANV_FROM_HANDLE(anv_image
, image
, pInfo
->image
);
3041 anv_GetImageMemoryRequirements(_device
, pInfo
->image
,
3042 &pMemoryRequirements
->memoryRequirements
);
3044 vk_foreach_struct_const(ext
, pInfo
->pNext
) {
3045 switch (ext
->sType
) {
3046 case VK_STRUCTURE_TYPE_IMAGE_PLANE_MEMORY_REQUIREMENTS_INFO
: {
3047 struct anv_physical_device
*pdevice
= &device
->instance
->physicalDevice
;
3048 const VkImagePlaneMemoryRequirementsInfo
*plane_reqs
=
3049 (const VkImagePlaneMemoryRequirementsInfo
*) ext
;
3050 uint32_t plane
= anv_image_aspect_to_plane(image
->aspects
,
3051 plane_reqs
->planeAspect
);
3053 assert(image
->planes
[plane
].offset
== 0);
3055 /* The Vulkan spec (git aaed022) says:
3057 * memoryTypeBits is a bitfield and contains one bit set for every
3058 * supported memory type for the resource. The bit `1<<i` is set
3059 * if and only if the memory type `i` in the
3060 * VkPhysicalDeviceMemoryProperties structure for the physical
3061 * device is supported.
3063 * All types are currently supported for images.
3065 pMemoryRequirements
->memoryRequirements
.memoryTypeBits
=
3066 (1ull << pdevice
->memory
.type_count
) - 1;
3068 /* We must have image allocated or imported at this point. According to the
3069 * specification, external images must have been bound to memory before
3070 * calling GetImageMemoryRequirements.
3072 assert(image
->planes
[plane
].size
> 0);
3074 pMemoryRequirements
->memoryRequirements
.size
= image
->planes
[plane
].size
;
3075 pMemoryRequirements
->memoryRequirements
.alignment
=
3076 image
->planes
[plane
].alignment
;
3081 anv_debug_ignored_stype(ext
->sType
);
3086 vk_foreach_struct(ext
, pMemoryRequirements
->pNext
) {
3087 switch (ext
->sType
) {
3088 case VK_STRUCTURE_TYPE_MEMORY_DEDICATED_REQUIREMENTS
: {
3089 VkMemoryDedicatedRequirements
*requirements
= (void *)ext
;
3090 if (image
->needs_set_tiling
|| image
->external_format
) {
3091 /* If we need to set the tiling for external consumers, we need a
3092 * dedicated allocation.
3094 * See also anv_AllocateMemory.
3096 requirements
->prefersDedicatedAllocation
= true;
3097 requirements
->requiresDedicatedAllocation
= true;
3099 requirements
->prefersDedicatedAllocation
= false;
3100 requirements
->requiresDedicatedAllocation
= false;
3106 anv_debug_ignored_stype(ext
->sType
);
3112 void anv_GetImageSparseMemoryRequirements(
3115 uint32_t* pSparseMemoryRequirementCount
,
3116 VkSparseImageMemoryRequirements
* pSparseMemoryRequirements
)
3118 *pSparseMemoryRequirementCount
= 0;
3121 void anv_GetImageSparseMemoryRequirements2(
3123 const VkImageSparseMemoryRequirementsInfo2
* pInfo
,
3124 uint32_t* pSparseMemoryRequirementCount
,
3125 VkSparseImageMemoryRequirements2
* pSparseMemoryRequirements
)
3127 *pSparseMemoryRequirementCount
= 0;
3130 void anv_GetDeviceMemoryCommitment(
3132 VkDeviceMemory memory
,
3133 VkDeviceSize
* pCommittedMemoryInBytes
)
3135 *pCommittedMemoryInBytes
= 0;
3139 anv_bind_buffer_memory(const VkBindBufferMemoryInfo
*pBindInfo
)
3141 ANV_FROM_HANDLE(anv_device_memory
, mem
, pBindInfo
->memory
);
3142 ANV_FROM_HANDLE(anv_buffer
, buffer
, pBindInfo
->buffer
);
3144 assert(pBindInfo
->sType
== VK_STRUCTURE_TYPE_BIND_BUFFER_MEMORY_INFO
);
3147 assert((buffer
->usage
& mem
->type
->valid_buffer_usage
) == buffer
->usage
);
3148 buffer
->address
= (struct anv_address
) {
3150 .offset
= pBindInfo
->memoryOffset
,
3153 buffer
->address
= ANV_NULL_ADDRESS
;
3157 VkResult
anv_BindBufferMemory(
3160 VkDeviceMemory memory
,
3161 VkDeviceSize memoryOffset
)
3163 anv_bind_buffer_memory(
3164 &(VkBindBufferMemoryInfo
) {
3165 .sType
= VK_STRUCTURE_TYPE_BIND_BUFFER_MEMORY_INFO
,
3168 .memoryOffset
= memoryOffset
,
3174 VkResult
anv_BindBufferMemory2(
3176 uint32_t bindInfoCount
,
3177 const VkBindBufferMemoryInfo
* pBindInfos
)
3179 for (uint32_t i
= 0; i
< bindInfoCount
; i
++)
3180 anv_bind_buffer_memory(&pBindInfos
[i
]);
3185 VkResult
anv_QueueBindSparse(
3187 uint32_t bindInfoCount
,
3188 const VkBindSparseInfo
* pBindInfo
,
3191 ANV_FROM_HANDLE(anv_queue
, queue
, _queue
);
3192 if (anv_device_is_lost(queue
->device
))
3193 return VK_ERROR_DEVICE_LOST
;
3195 return vk_error(VK_ERROR_FEATURE_NOT_PRESENT
);
3200 VkResult
anv_CreateEvent(
3202 const VkEventCreateInfo
* pCreateInfo
,
3203 const VkAllocationCallbacks
* pAllocator
,
3206 ANV_FROM_HANDLE(anv_device
, device
, _device
);
3207 struct anv_state state
;
3208 struct anv_event
*event
;
3210 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_EVENT_CREATE_INFO
);
3212 state
= anv_state_pool_alloc(&device
->dynamic_state_pool
,
3215 event
->state
= state
;
3216 event
->semaphore
= VK_EVENT_RESET
;
3218 if (!device
->info
.has_llc
) {
3219 /* Make sure the writes we're flushing have landed. */
3220 __builtin_ia32_mfence();
3221 __builtin_ia32_clflush(event
);
3224 *pEvent
= anv_event_to_handle(event
);
3229 void anv_DestroyEvent(
3232 const VkAllocationCallbacks
* pAllocator
)
3234 ANV_FROM_HANDLE(anv_device
, device
, _device
);
3235 ANV_FROM_HANDLE(anv_event
, event
, _event
);
3240 anv_state_pool_free(&device
->dynamic_state_pool
, event
->state
);
3243 VkResult
anv_GetEventStatus(
3247 ANV_FROM_HANDLE(anv_device
, device
, _device
);
3248 ANV_FROM_HANDLE(anv_event
, event
, _event
);
3250 if (anv_device_is_lost(device
))
3251 return VK_ERROR_DEVICE_LOST
;
3253 if (!device
->info
.has_llc
) {
3254 /* Invalidate read cache before reading event written by GPU. */
3255 __builtin_ia32_clflush(event
);
3256 __builtin_ia32_mfence();
3260 return event
->semaphore
;
3263 VkResult
anv_SetEvent(
3267 ANV_FROM_HANDLE(anv_device
, device
, _device
);
3268 ANV_FROM_HANDLE(anv_event
, event
, _event
);
3270 event
->semaphore
= VK_EVENT_SET
;
3272 if (!device
->info
.has_llc
) {
3273 /* Make sure the writes we're flushing have landed. */
3274 __builtin_ia32_mfence();
3275 __builtin_ia32_clflush(event
);
3281 VkResult
anv_ResetEvent(
3285 ANV_FROM_HANDLE(anv_device
, device
, _device
);
3286 ANV_FROM_HANDLE(anv_event
, event
, _event
);
3288 event
->semaphore
= VK_EVENT_RESET
;
3290 if (!device
->info
.has_llc
) {
3291 /* Make sure the writes we're flushing have landed. */
3292 __builtin_ia32_mfence();
3293 __builtin_ia32_clflush(event
);
3301 VkResult
anv_CreateBuffer(
3303 const VkBufferCreateInfo
* pCreateInfo
,
3304 const VkAllocationCallbacks
* pAllocator
,
3307 ANV_FROM_HANDLE(anv_device
, device
, _device
);
3308 struct anv_buffer
*buffer
;
3310 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO
);
3312 buffer
= vk_alloc2(&device
->alloc
, pAllocator
, sizeof(*buffer
), 8,
3313 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
3315 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
3317 buffer
->size
= pCreateInfo
->size
;
3318 buffer
->usage
= pCreateInfo
->usage
;
3319 buffer
->address
= ANV_NULL_ADDRESS
;
3321 if (buffer
->usage
& VK_BUFFER_USAGE_SHADER_DEVICE_ADDRESS_BIT_EXT
) {
3322 pthread_mutex_lock(&device
->mutex
);
3323 _mesa_set_add(device
->pinned_buffers
, buffer
);
3324 pthread_mutex_unlock(&device
->mutex
);
3327 *pBuffer
= anv_buffer_to_handle(buffer
);
3332 void anv_DestroyBuffer(
3335 const VkAllocationCallbacks
* pAllocator
)
3337 ANV_FROM_HANDLE(anv_device
, device
, _device
);
3338 ANV_FROM_HANDLE(anv_buffer
, buffer
, _buffer
);
3343 if (buffer
->usage
& VK_BUFFER_USAGE_SHADER_DEVICE_ADDRESS_BIT_EXT
) {
3344 pthread_mutex_lock(&device
->mutex
);
3345 _mesa_set_remove_key(device
->pinned_buffers
, buffer
);
3346 pthread_mutex_unlock(&device
->mutex
);
3349 vk_free2(&device
->alloc
, pAllocator
, buffer
);
3352 VkDeviceAddress
anv_GetBufferDeviceAddressEXT(
3354 const VkBufferDeviceAddressInfoEXT
* pInfo
)
3356 ANV_FROM_HANDLE(anv_buffer
, buffer
, pInfo
->buffer
);
3358 assert(buffer
->address
.bo
->flags
& EXEC_OBJECT_PINNED
);
3360 return anv_address_physical(buffer
->address
);
3364 anv_fill_buffer_surface_state(struct anv_device
*device
, struct anv_state state
,
3365 enum isl_format format
,
3366 struct anv_address address
,
3367 uint32_t range
, uint32_t stride
)
3369 isl_buffer_fill_state(&device
->isl_dev
, state
.map
,
3370 .address
= anv_address_physical(address
),
3371 .mocs
= device
->default_mocs
,
3374 .swizzle
= ISL_SWIZZLE_IDENTITY
,
3375 .stride_B
= stride
);
3378 void anv_DestroySampler(
3381 const VkAllocationCallbacks
* pAllocator
)
3383 ANV_FROM_HANDLE(anv_device
, device
, _device
);
3384 ANV_FROM_HANDLE(anv_sampler
, sampler
, _sampler
);
3389 vk_free2(&device
->alloc
, pAllocator
, sampler
);
3392 VkResult
anv_CreateFramebuffer(
3394 const VkFramebufferCreateInfo
* pCreateInfo
,
3395 const VkAllocationCallbacks
* pAllocator
,
3396 VkFramebuffer
* pFramebuffer
)
3398 ANV_FROM_HANDLE(anv_device
, device
, _device
);
3399 struct anv_framebuffer
*framebuffer
;
3401 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO
);
3403 size_t size
= sizeof(*framebuffer
) +
3404 sizeof(struct anv_image_view
*) * pCreateInfo
->attachmentCount
;
3405 framebuffer
= vk_alloc2(&device
->alloc
, pAllocator
, size
, 8,
3406 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
3407 if (framebuffer
== NULL
)
3408 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
3410 framebuffer
->attachment_count
= pCreateInfo
->attachmentCount
;
3411 for (uint32_t i
= 0; i
< pCreateInfo
->attachmentCount
; i
++) {
3412 VkImageView _iview
= pCreateInfo
->pAttachments
[i
];
3413 framebuffer
->attachments
[i
] = anv_image_view_from_handle(_iview
);
3416 framebuffer
->width
= pCreateInfo
->width
;
3417 framebuffer
->height
= pCreateInfo
->height
;
3418 framebuffer
->layers
= pCreateInfo
->layers
;
3420 *pFramebuffer
= anv_framebuffer_to_handle(framebuffer
);
3425 void anv_DestroyFramebuffer(
3428 const VkAllocationCallbacks
* pAllocator
)
3430 ANV_FROM_HANDLE(anv_device
, device
, _device
);
3431 ANV_FROM_HANDLE(anv_framebuffer
, fb
, _fb
);
3436 vk_free2(&device
->alloc
, pAllocator
, fb
);
3439 static const VkTimeDomainEXT anv_time_domains
[] = {
3440 VK_TIME_DOMAIN_DEVICE_EXT
,
3441 VK_TIME_DOMAIN_CLOCK_MONOTONIC_EXT
,
3442 VK_TIME_DOMAIN_CLOCK_MONOTONIC_RAW_EXT
,
3445 VkResult
anv_GetPhysicalDeviceCalibrateableTimeDomainsEXT(
3446 VkPhysicalDevice physicalDevice
,
3447 uint32_t *pTimeDomainCount
,
3448 VkTimeDomainEXT
*pTimeDomains
)
3451 VK_OUTARRAY_MAKE(out
, pTimeDomains
, pTimeDomainCount
);
3453 for (d
= 0; d
< ARRAY_SIZE(anv_time_domains
); d
++) {
3454 vk_outarray_append(&out
, i
) {
3455 *i
= anv_time_domains
[d
];
3459 return vk_outarray_status(&out
);
3463 anv_clock_gettime(clockid_t clock_id
)
3465 struct timespec current
;
3468 ret
= clock_gettime(clock_id
, ¤t
);
3469 if (ret
< 0 && clock_id
== CLOCK_MONOTONIC_RAW
)
3470 ret
= clock_gettime(CLOCK_MONOTONIC
, ¤t
);
3474 return (uint64_t) current
.tv_sec
* 1000000000ULL + current
.tv_nsec
;
3477 #define TIMESTAMP 0x2358
3479 VkResult
anv_GetCalibratedTimestampsEXT(
3481 uint32_t timestampCount
,
3482 const VkCalibratedTimestampInfoEXT
*pTimestampInfos
,
3483 uint64_t *pTimestamps
,
3484 uint64_t *pMaxDeviation
)
3486 ANV_FROM_HANDLE(anv_device
, device
, _device
);
3487 uint64_t timestamp_frequency
= device
->info
.timestamp_frequency
;
3490 uint64_t begin
, end
;
3491 uint64_t max_clock_period
= 0;
3493 begin
= anv_clock_gettime(CLOCK_MONOTONIC_RAW
);
3495 for (d
= 0; d
< timestampCount
; d
++) {
3496 switch (pTimestampInfos
[d
].timeDomain
) {
3497 case VK_TIME_DOMAIN_DEVICE_EXT
:
3498 ret
= anv_gem_reg_read(device
, TIMESTAMP
| 1,
3502 return anv_device_set_lost(device
, "Failed to read the TIMESTAMP "
3505 uint64_t device_period
= DIV_ROUND_UP(1000000000, timestamp_frequency
);
3506 max_clock_period
= MAX2(max_clock_period
, device_period
);
3508 case VK_TIME_DOMAIN_CLOCK_MONOTONIC_EXT
:
3509 pTimestamps
[d
] = anv_clock_gettime(CLOCK_MONOTONIC
);
3510 max_clock_period
= MAX2(max_clock_period
, 1);
3513 case VK_TIME_DOMAIN_CLOCK_MONOTONIC_RAW_EXT
:
3514 pTimestamps
[d
] = begin
;
3522 end
= anv_clock_gettime(CLOCK_MONOTONIC_RAW
);
3525 * The maximum deviation is the sum of the interval over which we
3526 * perform the sampling and the maximum period of any sampled
3527 * clock. That's because the maximum skew between any two sampled
3528 * clock edges is when the sampled clock with the largest period is
3529 * sampled at the end of that period but right at the beginning of the
3530 * sampling interval and some other clock is sampled right at the
3531 * begining of its sampling period and right at the end of the
3532 * sampling interval. Let's assume the GPU has the longest clock
3533 * period and that the application is sampling GPU and monotonic:
3536 * w x y z 0 1 2 3 4 5 6 7 8 9 a b c d e f
3537 * Raw -_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-
3541 * GPU -----_____-----_____-----_____-----_____
3544 * x y z 0 1 2 3 4 5 6 7 8 9 a b c
3545 * Monotonic -_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-
3547 * Interval <----------------->
3548 * Deviation <-------------------------->
3552 * m = read(monotonic) 2
3555 * We round the sample interval up by one tick to cover sampling error
3556 * in the interval clock
3559 uint64_t sample_interval
= end
- begin
+ 1;
3561 *pMaxDeviation
= sample_interval
+ max_clock_period
;
3566 /* vk_icd.h does not declare this function, so we declare it here to
3567 * suppress Wmissing-prototypes.
3569 PUBLIC VKAPI_ATTR VkResult VKAPI_CALL
3570 vk_icdNegotiateLoaderICDInterfaceVersion(uint32_t* pSupportedVersion
);
3572 PUBLIC VKAPI_ATTR VkResult VKAPI_CALL
3573 vk_icdNegotiateLoaderICDInterfaceVersion(uint32_t* pSupportedVersion
)
3575 /* For the full details on loader interface versioning, see
3576 * <https://github.com/KhronosGroup/Vulkan-LoaderAndValidationLayers/blob/master/loader/LoaderAndLayerInterface.md>.
3577 * What follows is a condensed summary, to help you navigate the large and
3578 * confusing official doc.
3580 * - Loader interface v0 is incompatible with later versions. We don't
3583 * - In loader interface v1:
3584 * - The first ICD entrypoint called by the loader is
3585 * vk_icdGetInstanceProcAddr(). The ICD must statically expose this
3587 * - The ICD must statically expose no other Vulkan symbol unless it is
3588 * linked with -Bsymbolic.
3589 * - Each dispatchable Vulkan handle created by the ICD must be
3590 * a pointer to a struct whose first member is VK_LOADER_DATA. The
3591 * ICD must initialize VK_LOADER_DATA.loadMagic to ICD_LOADER_MAGIC.
3592 * - The loader implements vkCreate{PLATFORM}SurfaceKHR() and
3593 * vkDestroySurfaceKHR(). The ICD must be capable of working with
3594 * such loader-managed surfaces.
3596 * - Loader interface v2 differs from v1 in:
3597 * - The first ICD entrypoint called by the loader is
3598 * vk_icdNegotiateLoaderICDInterfaceVersion(). The ICD must
3599 * statically expose this entrypoint.
3601 * - Loader interface v3 differs from v2 in:
3602 * - The ICD must implement vkCreate{PLATFORM}SurfaceKHR(),
3603 * vkDestroySurfaceKHR(), and other API which uses VKSurfaceKHR,
3604 * because the loader no longer does so.
3606 *pSupportedVersion
= MIN2(*pSupportedVersion
, 3u);