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"
48 compiler_debug_log(void *data
, const char *fmt
, ...)
52 compiler_perf_log(void *data
, const char *fmt
, ...)
57 if (unlikely(INTEL_DEBUG
& DEBUG_PERF
))
58 intel_logd_v(fmt
, args
);
64 anv_compute_heap_size(int fd
, uint64_t gtt_size
)
66 /* Query the total ram from the system */
70 uint64_t total_ram
= (uint64_t)info
.totalram
* (uint64_t)info
.mem_unit
;
72 /* We don't want to burn too much ram with the GPU. If the user has 4GiB
73 * or less, we use at most half. If they have more than 4GiB, we use 3/4.
75 uint64_t available_ram
;
76 if (total_ram
<= 4ull * 1024ull * 1024ull * 1024ull)
77 available_ram
= total_ram
/ 2;
79 available_ram
= total_ram
* 3 / 4;
81 /* We also want to leave some padding for things we allocate in the driver,
82 * so don't go over 3/4 of the GTT either.
84 uint64_t available_gtt
= gtt_size
* 3 / 4;
86 return MIN2(available_ram
, available_gtt
);
90 anv_physical_device_init_heaps(struct anv_physical_device
*device
, int fd
)
93 if (anv_gem_get_context_param(fd
, 0, I915_CONTEXT_PARAM_GTT_SIZE
,
95 /* If, for whatever reason, we can't actually get the GTT size from the
96 * kernel (too old?) fall back to the aperture size.
98 anv_perf_warn(NULL
, NULL
,
99 "Failed to get I915_CONTEXT_PARAM_GTT_SIZE: %m");
101 if (anv_gem_get_aperture(fd
, >t_size
) == -1) {
102 return vk_errorf(NULL
, NULL
, VK_ERROR_INITIALIZATION_FAILED
,
103 "failed to get aperture size: %m");
107 device
->supports_48bit_addresses
= (device
->info
.gen
>= 8) &&
108 gtt_size
> (4ULL << 30 /* GiB */);
110 uint64_t heap_size
= anv_compute_heap_size(fd
, gtt_size
);
112 if (heap_size
> (2ull << 30) && !device
->supports_48bit_addresses
) {
113 /* When running with an overridden PCI ID, we may get a GTT size from
114 * the kernel that is greater than 2 GiB but the execbuf check for 48bit
115 * address support can still fail. Just clamp the address space size to
116 * 2 GiB if we don't have 48-bit support.
118 intel_logw("%s:%d: The kernel reported a GTT size larger than 2 GiB but "
119 "not support for 48-bit addresses",
121 heap_size
= 2ull << 30;
124 if (heap_size
<= 3ull * (1ull << 30)) {
125 /* In this case, everything fits nicely into the 32-bit address space,
126 * so there's no need for supporting 48bit addresses on client-allocated
129 device
->memory
.heap_count
= 1;
130 device
->memory
.heaps
[0] = (struct anv_memory_heap
) {
132 .flags
= VK_MEMORY_HEAP_DEVICE_LOCAL_BIT
,
133 .supports_48bit_addresses
= false,
136 /* Not everything will fit nicely into a 32-bit address space. In this
137 * case we need a 64-bit heap. Advertise a small 32-bit heap and a
138 * larger 48-bit heap. If we're in this case, then we have a total heap
139 * size larger than 3GiB which most likely means they have 8 GiB of
140 * video memory and so carving off 1 GiB for the 32-bit heap should be
143 const uint64_t heap_size_32bit
= 1ull << 30;
144 const uint64_t heap_size_48bit
= heap_size
- heap_size_32bit
;
146 assert(device
->supports_48bit_addresses
);
148 device
->memory
.heap_count
= 2;
149 device
->memory
.heaps
[0] = (struct anv_memory_heap
) {
150 .size
= heap_size_48bit
,
151 .flags
= VK_MEMORY_HEAP_DEVICE_LOCAL_BIT
,
152 .supports_48bit_addresses
= true,
154 device
->memory
.heaps
[1] = (struct anv_memory_heap
) {
155 .size
= heap_size_32bit
,
156 .flags
= VK_MEMORY_HEAP_DEVICE_LOCAL_BIT
,
157 .supports_48bit_addresses
= false,
161 uint32_t type_count
= 0;
162 for (uint32_t heap
= 0; heap
< device
->memory
.heap_count
; heap
++) {
163 uint32_t valid_buffer_usage
= ~0;
165 /* There appears to be a hardware issue in the VF cache where it only
166 * considers the bottom 32 bits of memory addresses. If you happen to
167 * have two vertex buffers which get placed exactly 4 GiB apart and use
168 * them in back-to-back draw calls, you can get collisions. In order to
169 * solve this problem, we require vertex and index buffers be bound to
170 * memory allocated out of the 32-bit heap.
172 if (device
->memory
.heaps
[heap
].supports_48bit_addresses
) {
173 valid_buffer_usage
&= ~(VK_BUFFER_USAGE_INDEX_BUFFER_BIT
|
174 VK_BUFFER_USAGE_VERTEX_BUFFER_BIT
);
177 if (device
->info
.has_llc
) {
178 /* Big core GPUs share LLC with the CPU and thus one memory type can be
179 * both cached and coherent at the same time.
181 device
->memory
.types
[type_count
++] = (struct anv_memory_type
) {
182 .propertyFlags
= VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT
|
183 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT
|
184 VK_MEMORY_PROPERTY_HOST_COHERENT_BIT
|
185 VK_MEMORY_PROPERTY_HOST_CACHED_BIT
,
187 .valid_buffer_usage
= valid_buffer_usage
,
190 /* The spec requires that we expose a host-visible, coherent memory
191 * type, but Atom GPUs don't share LLC. Thus we offer two memory types
192 * to give the application a choice between cached, but not coherent and
193 * coherent but uncached (WC though).
195 device
->memory
.types
[type_count
++] = (struct anv_memory_type
) {
196 .propertyFlags
= VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT
|
197 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT
|
198 VK_MEMORY_PROPERTY_HOST_COHERENT_BIT
,
200 .valid_buffer_usage
= valid_buffer_usage
,
202 device
->memory
.types
[type_count
++] = (struct anv_memory_type
) {
203 .propertyFlags
= VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT
|
204 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT
|
205 VK_MEMORY_PROPERTY_HOST_CACHED_BIT
,
207 .valid_buffer_usage
= valid_buffer_usage
,
211 device
->memory
.type_count
= type_count
;
217 anv_physical_device_init_uuids(struct anv_physical_device
*device
)
219 const struct build_id_note
*note
=
220 build_id_find_nhdr_for_addr(anv_physical_device_init_uuids
);
222 return vk_errorf(device
->instance
, device
,
223 VK_ERROR_INITIALIZATION_FAILED
,
224 "Failed to find build-id");
227 unsigned build_id_len
= build_id_length(note
);
228 if (build_id_len
< 20) {
229 return vk_errorf(device
->instance
, device
,
230 VK_ERROR_INITIALIZATION_FAILED
,
231 "build-id too short. It needs to be a SHA");
234 memcpy(device
->driver_build_sha1
, build_id_data(note
), 20);
236 struct mesa_sha1 sha1_ctx
;
238 STATIC_ASSERT(VK_UUID_SIZE
<= sizeof(sha1
));
240 /* The pipeline cache UUID is used for determining when a pipeline cache is
241 * invalid. It needs both a driver build and the PCI ID of the device.
243 _mesa_sha1_init(&sha1_ctx
);
244 _mesa_sha1_update(&sha1_ctx
, build_id_data(note
), build_id_len
);
245 _mesa_sha1_update(&sha1_ctx
, &device
->chipset_id
,
246 sizeof(device
->chipset_id
));
247 _mesa_sha1_final(&sha1_ctx
, sha1
);
248 memcpy(device
->pipeline_cache_uuid
, sha1
, VK_UUID_SIZE
);
250 /* The driver UUID is used for determining sharability of images and memory
251 * between two Vulkan instances in separate processes. People who want to
252 * share memory need to also check the device UUID (below) so all this
253 * needs to be is the build-id.
255 memcpy(device
->driver_uuid
, build_id_data(note
), VK_UUID_SIZE
);
257 /* The device UUID uniquely identifies the given device within the machine.
258 * Since we never have more than one device, this doesn't need to be a real
259 * UUID. However, on the off-chance that someone tries to use this to
260 * cache pre-tiled images or something of the like, we use the PCI ID and
261 * some bits of ISL info to ensure that this is safe.
263 _mesa_sha1_init(&sha1_ctx
);
264 _mesa_sha1_update(&sha1_ctx
, &device
->chipset_id
,
265 sizeof(device
->chipset_id
));
266 _mesa_sha1_update(&sha1_ctx
, &device
->isl_dev
.has_bit6_swizzling
,
267 sizeof(device
->isl_dev
.has_bit6_swizzling
));
268 _mesa_sha1_final(&sha1_ctx
, sha1
);
269 memcpy(device
->device_uuid
, sha1
, VK_UUID_SIZE
);
275 anv_physical_device_init_disk_cache(struct anv_physical_device
*device
)
277 #ifdef ENABLE_SHADER_CACHE
279 MAYBE_UNUSED
int len
= snprintf(renderer
, sizeof(renderer
), "anv_%04x",
281 assert(len
== sizeof(renderer
) - 2);
284 _mesa_sha1_format(timestamp
, device
->driver_build_sha1
);
286 const uint64_t driver_flags
=
287 brw_get_compiler_config_value(device
->compiler
);
288 device
->disk_cache
= disk_cache_create(renderer
, timestamp
, driver_flags
);
290 device
->disk_cache
= NULL
;
295 anv_physical_device_free_disk_cache(struct anv_physical_device
*device
)
297 #ifdef ENABLE_SHADER_CACHE
298 if (device
->disk_cache
)
299 disk_cache_destroy(device
->disk_cache
);
301 assert(device
->disk_cache
== NULL
);
306 anv_physical_device_init(struct anv_physical_device
*device
,
307 struct anv_instance
*instance
,
308 drmDevicePtr drm_device
)
310 const char *primary_path
= drm_device
->nodes
[DRM_NODE_PRIMARY
];
311 const char *path
= drm_device
->nodes
[DRM_NODE_RENDER
];
316 brw_process_intel_debug_variable();
318 fd
= open(path
, O_RDWR
| O_CLOEXEC
);
320 return vk_error(VK_ERROR_INCOMPATIBLE_DRIVER
);
322 device
->_loader_data
.loaderMagic
= ICD_LOADER_MAGIC
;
323 device
->instance
= instance
;
325 assert(strlen(path
) < ARRAY_SIZE(device
->path
));
326 snprintf(device
->path
, ARRAY_SIZE(device
->path
), "%s", path
);
328 device
->no_hw
= getenv("INTEL_NO_HW") != NULL
;
330 const int pci_id_override
= gen_get_pci_device_id_override();
331 if (pci_id_override
< 0) {
332 device
->chipset_id
= anv_gem_get_param(fd
, I915_PARAM_CHIPSET_ID
);
333 if (!device
->chipset_id
) {
334 result
= vk_error(VK_ERROR_INCOMPATIBLE_DRIVER
);
338 device
->chipset_id
= pci_id_override
;
339 device
->no_hw
= true;
342 device
->pci_info
.domain
= drm_device
->businfo
.pci
->domain
;
343 device
->pci_info
.bus
= drm_device
->businfo
.pci
->bus
;
344 device
->pci_info
.device
= drm_device
->businfo
.pci
->dev
;
345 device
->pci_info
.function
= drm_device
->businfo
.pci
->func
;
347 device
->name
= gen_get_device_name(device
->chipset_id
);
348 if (!gen_get_device_info(device
->chipset_id
, &device
->info
)) {
349 result
= vk_error(VK_ERROR_INCOMPATIBLE_DRIVER
);
353 if (device
->info
.is_haswell
) {
354 intel_logw("Haswell Vulkan support is incomplete");
355 } else if (device
->info
.gen
== 7 && !device
->info
.is_baytrail
) {
356 intel_logw("Ivy Bridge Vulkan support is incomplete");
357 } else if (device
->info
.gen
== 7 && device
->info
.is_baytrail
) {
358 intel_logw("Bay Trail Vulkan support is incomplete");
359 } else if (device
->info
.gen
>= 8 && device
->info
.gen
<= 10) {
360 /* Gen8-10 fully supported */
361 } else if (device
->info
.gen
== 11) {
362 intel_logw("Vulkan is not yet fully supported on gen11.");
364 result
= vk_errorf(device
->instance
, device
,
365 VK_ERROR_INCOMPATIBLE_DRIVER
,
366 "Vulkan not yet supported on %s", device
->name
);
370 device
->cmd_parser_version
= -1;
371 if (device
->info
.gen
== 7) {
372 device
->cmd_parser_version
=
373 anv_gem_get_param(fd
, I915_PARAM_CMD_PARSER_VERSION
);
374 if (device
->cmd_parser_version
== -1) {
375 result
= vk_errorf(device
->instance
, device
,
376 VK_ERROR_INITIALIZATION_FAILED
,
377 "failed to get command parser version");
382 if (!anv_gem_get_param(fd
, I915_PARAM_HAS_WAIT_TIMEOUT
)) {
383 result
= vk_errorf(device
->instance
, device
,
384 VK_ERROR_INITIALIZATION_FAILED
,
385 "kernel missing gem wait");
389 if (!anv_gem_get_param(fd
, I915_PARAM_HAS_EXECBUF2
)) {
390 result
= vk_errorf(device
->instance
, device
,
391 VK_ERROR_INITIALIZATION_FAILED
,
392 "kernel missing execbuf2");
396 if (!device
->info
.has_llc
&&
397 anv_gem_get_param(fd
, I915_PARAM_MMAP_VERSION
) < 1) {
398 result
= vk_errorf(device
->instance
, device
,
399 VK_ERROR_INITIALIZATION_FAILED
,
400 "kernel missing wc mmap");
404 result
= anv_physical_device_init_heaps(device
, fd
);
405 if (result
!= VK_SUCCESS
)
408 device
->has_exec_async
= anv_gem_get_param(fd
, I915_PARAM_HAS_EXEC_ASYNC
);
409 device
->has_exec_capture
= anv_gem_get_param(fd
, I915_PARAM_HAS_EXEC_CAPTURE
);
410 device
->has_exec_fence
= anv_gem_get_param(fd
, I915_PARAM_HAS_EXEC_FENCE
);
411 device
->has_syncobj
= anv_gem_get_param(fd
, I915_PARAM_HAS_EXEC_FENCE_ARRAY
);
412 device
->has_syncobj_wait
= device
->has_syncobj
&&
413 anv_gem_supports_syncobj_wait(fd
);
414 device
->has_context_priority
= anv_gem_has_context_priority(fd
);
416 device
->use_softpin
= anv_gem_get_param(fd
, I915_PARAM_HAS_EXEC_SOFTPIN
)
417 && device
->supports_48bit_addresses
;
419 device
->has_context_isolation
=
420 anv_gem_get_param(fd
, I915_PARAM_HAS_CONTEXT_ISOLATION
);
422 /* Starting with Gen10, the timestamp frequency of the command streamer may
423 * vary from one part to another. We can query the value from the kernel.
425 if (device
->info
.gen
>= 10) {
426 int timestamp_frequency
=
427 anv_gem_get_param(fd
, I915_PARAM_CS_TIMESTAMP_FREQUENCY
);
429 if (timestamp_frequency
< 0)
430 intel_logw("Kernel 4.16-rc1+ required to properly query CS timestamp frequency");
432 device
->info
.timestamp_frequency
= timestamp_frequency
;
435 /* GENs prior to 8 do not support EU/Subslice info */
436 if (device
->info
.gen
>= 8) {
437 device
->subslice_total
= anv_gem_get_param(fd
, I915_PARAM_SUBSLICE_TOTAL
);
438 device
->eu_total
= anv_gem_get_param(fd
, I915_PARAM_EU_TOTAL
);
440 /* Without this information, we cannot get the right Braswell
441 * brandstrings, and we have to use conservative numbers for GPGPU on
442 * many platforms, but otherwise, things will just work.
444 if (device
->subslice_total
< 1 || device
->eu_total
< 1) {
445 intel_logw("Kernel 4.1 required to properly query GPU properties");
447 } else if (device
->info
.gen
== 7) {
448 device
->subslice_total
= 1 << (device
->info
.gt
- 1);
451 if (device
->info
.is_cherryview
&&
452 device
->subslice_total
> 0 && device
->eu_total
> 0) {
453 /* Logical CS threads = EUs per subslice * num threads per EU */
454 uint32_t max_cs_threads
=
455 device
->eu_total
/ device
->subslice_total
* device
->info
.num_thread_per_eu
;
457 /* Fuse configurations may give more threads than expected, never less. */
458 if (max_cs_threads
> device
->info
.max_cs_threads
)
459 device
->info
.max_cs_threads
= max_cs_threads
;
462 device
->compiler
= brw_compiler_create(NULL
, &device
->info
);
463 if (device
->compiler
== NULL
) {
464 result
= vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
467 device
->compiler
->shader_debug_log
= compiler_debug_log
;
468 device
->compiler
->shader_perf_log
= compiler_perf_log
;
469 device
->compiler
->supports_pull_constants
= false;
470 device
->compiler
->constant_buffer_0_is_relative
=
471 device
->info
.gen
< 8 || !device
->has_context_isolation
;
472 device
->compiler
->supports_shader_constants
= true;
474 /* Broadwell PRM says:
476 * "Before Gen8, there was a historical configuration control field to
477 * swizzle address bit[6] for in X/Y tiling modes. This was set in three
478 * different places: TILECTL[1:0], ARB_MODE[5:4], and
479 * DISP_ARB_CTL[14:13].
481 * For Gen8 and subsequent generations, the swizzle fields are all
482 * reserved, and the CPU's memory controller performs all address
483 * swizzling modifications."
486 device
->info
.gen
< 8 && anv_gem_get_bit6_swizzle(fd
, I915_TILING_X
);
488 isl_device_init(&device
->isl_dev
, &device
->info
, swizzled
);
490 result
= anv_physical_device_init_uuids(device
);
491 if (result
!= VK_SUCCESS
)
494 anv_physical_device_init_disk_cache(device
);
496 if (instance
->enabled_extensions
.KHR_display
) {
497 master_fd
= open(primary_path
, O_RDWR
| O_CLOEXEC
);
498 if (master_fd
>= 0) {
499 /* prod the device with a GETPARAM call which will fail if
500 * we don't have permission to even render on this device
502 if (anv_gem_get_param(master_fd
, I915_PARAM_CHIPSET_ID
) == 0) {
508 device
->master_fd
= master_fd
;
510 result
= anv_init_wsi(device
);
511 if (result
!= VK_SUCCESS
) {
512 ralloc_free(device
->compiler
);
513 anv_physical_device_free_disk_cache(device
);
517 anv_physical_device_get_supported_extensions(device
,
518 &device
->supported_extensions
);
521 device
->local_fd
= fd
;
533 anv_physical_device_finish(struct anv_physical_device
*device
)
535 anv_finish_wsi(device
);
536 anv_physical_device_free_disk_cache(device
);
537 ralloc_free(device
->compiler
);
538 close(device
->local_fd
);
539 if (device
->master_fd
>= 0)
540 close(device
->master_fd
);
544 default_alloc_func(void *pUserData
, size_t size
, size_t align
,
545 VkSystemAllocationScope allocationScope
)
551 default_realloc_func(void *pUserData
, void *pOriginal
, size_t size
,
552 size_t align
, VkSystemAllocationScope allocationScope
)
554 return realloc(pOriginal
, size
);
558 default_free_func(void *pUserData
, void *pMemory
)
563 static const VkAllocationCallbacks default_alloc
= {
565 .pfnAllocation
= default_alloc_func
,
566 .pfnReallocation
= default_realloc_func
,
567 .pfnFree
= default_free_func
,
570 VkResult
anv_EnumerateInstanceExtensionProperties(
571 const char* pLayerName
,
572 uint32_t* pPropertyCount
,
573 VkExtensionProperties
* pProperties
)
575 VK_OUTARRAY_MAKE(out
, pProperties
, pPropertyCount
);
577 for (int i
= 0; i
< ANV_INSTANCE_EXTENSION_COUNT
; i
++) {
578 if (anv_instance_extensions_supported
.extensions
[i
]) {
579 vk_outarray_append(&out
, prop
) {
580 *prop
= anv_instance_extensions
[i
];
585 return vk_outarray_status(&out
);
588 VkResult
anv_CreateInstance(
589 const VkInstanceCreateInfo
* pCreateInfo
,
590 const VkAllocationCallbacks
* pAllocator
,
591 VkInstance
* pInstance
)
593 struct anv_instance
*instance
;
596 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO
);
598 struct anv_instance_extension_table enabled_extensions
= {};
599 for (uint32_t i
= 0; i
< pCreateInfo
->enabledExtensionCount
; i
++) {
601 for (idx
= 0; idx
< ANV_INSTANCE_EXTENSION_COUNT
; idx
++) {
602 if (strcmp(pCreateInfo
->ppEnabledExtensionNames
[i
],
603 anv_instance_extensions
[idx
].extensionName
) == 0)
607 if (idx
>= ANV_INSTANCE_EXTENSION_COUNT
)
608 return vk_error(VK_ERROR_EXTENSION_NOT_PRESENT
);
610 if (!anv_instance_extensions_supported
.extensions
[idx
])
611 return vk_error(VK_ERROR_EXTENSION_NOT_PRESENT
);
613 enabled_extensions
.extensions
[idx
] = true;
616 instance
= vk_alloc2(&default_alloc
, pAllocator
, sizeof(*instance
), 8,
617 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE
);
619 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
621 instance
->_loader_data
.loaderMagic
= ICD_LOADER_MAGIC
;
624 instance
->alloc
= *pAllocator
;
626 instance
->alloc
= default_alloc
;
628 instance
->app_info
= (struct anv_app_info
) { .api_version
= 0 };
629 if (pCreateInfo
->pApplicationInfo
) {
630 const VkApplicationInfo
*app
= pCreateInfo
->pApplicationInfo
;
632 instance
->app_info
.app_name
=
633 vk_strdup(&instance
->alloc
, app
->pApplicationName
,
634 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE
);
635 instance
->app_info
.app_version
= app
->applicationVersion
;
637 instance
->app_info
.engine_name
=
638 vk_strdup(&instance
->alloc
, app
->pEngineName
,
639 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE
);
640 instance
->app_info
.engine_version
= app
->engineVersion
;
642 instance
->app_info
.api_version
= app
->apiVersion
;
645 if (instance
->app_info
.api_version
== 0)
646 instance
->app_info
.api_version
= VK_API_VERSION_1_0
;
648 instance
->enabled_extensions
= enabled_extensions
;
650 for (unsigned i
= 0; i
< ARRAY_SIZE(instance
->dispatch
.entrypoints
); i
++) {
651 /* Vulkan requires that entrypoints for extensions which have not been
652 * enabled must not be advertised.
654 if (!anv_instance_entrypoint_is_enabled(i
, instance
->app_info
.api_version
,
655 &instance
->enabled_extensions
)) {
656 instance
->dispatch
.entrypoints
[i
] = NULL
;
658 instance
->dispatch
.entrypoints
[i
] =
659 anv_instance_dispatch_table
.entrypoints
[i
];
663 for (unsigned i
= 0; i
< ARRAY_SIZE(instance
->device_dispatch
.entrypoints
); i
++) {
664 /* Vulkan requires that entrypoints for extensions which have not been
665 * enabled must not be advertised.
667 if (!anv_device_entrypoint_is_enabled(i
, instance
->app_info
.api_version
,
668 &instance
->enabled_extensions
, NULL
)) {
669 instance
->device_dispatch
.entrypoints
[i
] = NULL
;
671 instance
->device_dispatch
.entrypoints
[i
] =
672 anv_device_dispatch_table
.entrypoints
[i
];
676 instance
->physicalDeviceCount
= -1;
678 result
= vk_debug_report_instance_init(&instance
->debug_report_callbacks
);
679 if (result
!= VK_SUCCESS
) {
680 vk_free2(&default_alloc
, pAllocator
, instance
);
681 return vk_error(result
);
684 instance
->pipeline_cache_enabled
=
685 env_var_as_boolean("ANV_ENABLE_PIPELINE_CACHE", true);
689 VG(VALGRIND_CREATE_MEMPOOL(instance
, 0, false));
691 *pInstance
= anv_instance_to_handle(instance
);
696 void anv_DestroyInstance(
697 VkInstance _instance
,
698 const VkAllocationCallbacks
* pAllocator
)
700 ANV_FROM_HANDLE(anv_instance
, instance
, _instance
);
705 if (instance
->physicalDeviceCount
> 0) {
706 /* We support at most one physical device. */
707 assert(instance
->physicalDeviceCount
== 1);
708 anv_physical_device_finish(&instance
->physicalDevice
);
711 vk_free(&instance
->alloc
, (char *)instance
->app_info
.app_name
);
712 vk_free(&instance
->alloc
, (char *)instance
->app_info
.engine_name
);
714 VG(VALGRIND_DESTROY_MEMPOOL(instance
));
716 vk_debug_report_instance_destroy(&instance
->debug_report_callbacks
);
720 vk_free(&instance
->alloc
, instance
);
724 anv_enumerate_devices(struct anv_instance
*instance
)
726 /* TODO: Check for more devices ? */
727 drmDevicePtr devices
[8];
728 VkResult result
= VK_ERROR_INCOMPATIBLE_DRIVER
;
731 instance
->physicalDeviceCount
= 0;
733 max_devices
= drmGetDevices2(0, devices
, ARRAY_SIZE(devices
));
735 return VK_ERROR_INCOMPATIBLE_DRIVER
;
737 for (unsigned i
= 0; i
< (unsigned)max_devices
; i
++) {
738 if (devices
[i
]->available_nodes
& 1 << DRM_NODE_RENDER
&&
739 devices
[i
]->bustype
== DRM_BUS_PCI
&&
740 devices
[i
]->deviceinfo
.pci
->vendor_id
== 0x8086) {
742 result
= anv_physical_device_init(&instance
->physicalDevice
,
743 instance
, devices
[i
]);
744 if (result
!= VK_ERROR_INCOMPATIBLE_DRIVER
)
748 drmFreeDevices(devices
, max_devices
);
750 if (result
== VK_SUCCESS
)
751 instance
->physicalDeviceCount
= 1;
757 anv_instance_ensure_physical_device(struct anv_instance
*instance
)
759 if (instance
->physicalDeviceCount
< 0) {
760 VkResult result
= anv_enumerate_devices(instance
);
761 if (result
!= VK_SUCCESS
&&
762 result
!= VK_ERROR_INCOMPATIBLE_DRIVER
)
769 VkResult
anv_EnumeratePhysicalDevices(
770 VkInstance _instance
,
771 uint32_t* pPhysicalDeviceCount
,
772 VkPhysicalDevice
* pPhysicalDevices
)
774 ANV_FROM_HANDLE(anv_instance
, instance
, _instance
);
775 VK_OUTARRAY_MAKE(out
, pPhysicalDevices
, pPhysicalDeviceCount
);
777 VkResult result
= anv_instance_ensure_physical_device(instance
);
778 if (result
!= VK_SUCCESS
)
781 if (instance
->physicalDeviceCount
== 0)
784 assert(instance
->physicalDeviceCount
== 1);
785 vk_outarray_append(&out
, i
) {
786 *i
= anv_physical_device_to_handle(&instance
->physicalDevice
);
789 return vk_outarray_status(&out
);
792 VkResult
anv_EnumeratePhysicalDeviceGroups(
793 VkInstance _instance
,
794 uint32_t* pPhysicalDeviceGroupCount
,
795 VkPhysicalDeviceGroupProperties
* pPhysicalDeviceGroupProperties
)
797 ANV_FROM_HANDLE(anv_instance
, instance
, _instance
);
798 VK_OUTARRAY_MAKE(out
, pPhysicalDeviceGroupProperties
,
799 pPhysicalDeviceGroupCount
);
801 VkResult result
= anv_instance_ensure_physical_device(instance
);
802 if (result
!= VK_SUCCESS
)
805 if (instance
->physicalDeviceCount
== 0)
808 assert(instance
->physicalDeviceCount
== 1);
810 vk_outarray_append(&out
, p
) {
811 p
->physicalDeviceCount
= 1;
812 memset(p
->physicalDevices
, 0, sizeof(p
->physicalDevices
));
813 p
->physicalDevices
[0] =
814 anv_physical_device_to_handle(&instance
->physicalDevice
);
815 p
->subsetAllocation
= VK_FALSE
;
817 vk_foreach_struct(ext
, p
->pNext
)
818 anv_debug_ignored_stype(ext
->sType
);
821 return vk_outarray_status(&out
);
824 void anv_GetPhysicalDeviceFeatures(
825 VkPhysicalDevice physicalDevice
,
826 VkPhysicalDeviceFeatures
* pFeatures
)
828 ANV_FROM_HANDLE(anv_physical_device
, pdevice
, physicalDevice
);
830 *pFeatures
= (VkPhysicalDeviceFeatures
) {
831 .robustBufferAccess
= true,
832 .fullDrawIndexUint32
= true,
833 .imageCubeArray
= true,
834 .independentBlend
= true,
835 .geometryShader
= true,
836 .tessellationShader
= true,
837 .sampleRateShading
= true,
838 .dualSrcBlend
= true,
840 .multiDrawIndirect
= true,
841 .drawIndirectFirstInstance
= true,
843 .depthBiasClamp
= true,
844 .fillModeNonSolid
= true,
845 .depthBounds
= false,
849 .multiViewport
= true,
850 .samplerAnisotropy
= true,
851 .textureCompressionETC2
= pdevice
->info
.gen
>= 8 ||
852 pdevice
->info
.is_baytrail
,
853 .textureCompressionASTC_LDR
= pdevice
->info
.gen
>= 9, /* FINISHME CHV */
854 .textureCompressionBC
= true,
855 .occlusionQueryPrecise
= true,
856 .pipelineStatisticsQuery
= true,
857 .fragmentStoresAndAtomics
= true,
858 .shaderTessellationAndGeometryPointSize
= true,
859 .shaderImageGatherExtended
= true,
860 .shaderStorageImageExtendedFormats
= true,
861 .shaderStorageImageMultisample
= false,
862 .shaderStorageImageReadWithoutFormat
= false,
863 .shaderStorageImageWriteWithoutFormat
= true,
864 .shaderUniformBufferArrayDynamicIndexing
= true,
865 .shaderSampledImageArrayDynamicIndexing
= true,
866 .shaderStorageBufferArrayDynamicIndexing
= true,
867 .shaderStorageImageArrayDynamicIndexing
= true,
868 .shaderClipDistance
= true,
869 .shaderCullDistance
= true,
870 .shaderFloat64
= pdevice
->info
.gen
>= 8 &&
871 pdevice
->info
.has_64bit_types
,
872 .shaderInt64
= pdevice
->info
.gen
>= 8 &&
873 pdevice
->info
.has_64bit_types
,
874 .shaderInt16
= pdevice
->info
.gen
>= 8,
875 .shaderResourceMinLod
= pdevice
->info
.gen
>= 9,
876 .variableMultisampleRate
= true,
877 .inheritedQueries
= true,
880 /* We can't do image stores in vec4 shaders */
881 pFeatures
->vertexPipelineStoresAndAtomics
=
882 pdevice
->compiler
->scalar_stage
[MESA_SHADER_VERTEX
] &&
883 pdevice
->compiler
->scalar_stage
[MESA_SHADER_GEOMETRY
];
885 struct anv_app_info
*app_info
= &pdevice
->instance
->app_info
;
887 /* The new DOOM and Wolfenstein games require depthBounds without
888 * checking for it. They seem to run fine without it so just claim it's
889 * there and accept the consequences.
891 if (app_info
->engine_name
&& strcmp(app_info
->engine_name
, "idTech") == 0)
892 pFeatures
->depthBounds
= true;
895 void anv_GetPhysicalDeviceFeatures2(
896 VkPhysicalDevice physicalDevice
,
897 VkPhysicalDeviceFeatures2
* pFeatures
)
899 ANV_FROM_HANDLE(anv_physical_device
, pdevice
, physicalDevice
);
900 anv_GetPhysicalDeviceFeatures(physicalDevice
, &pFeatures
->features
);
902 vk_foreach_struct(ext
, pFeatures
->pNext
) {
903 switch (ext
->sType
) {
904 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_8BIT_STORAGE_FEATURES_KHR
: {
905 VkPhysicalDevice8BitStorageFeaturesKHR
*features
=
906 (VkPhysicalDevice8BitStorageFeaturesKHR
*)ext
;
907 ANV_FROM_HANDLE(anv_physical_device
, pdevice
, physicalDevice
);
909 features
->storageBuffer8BitAccess
= pdevice
->info
.gen
>= 8;
910 features
->uniformAndStorageBuffer8BitAccess
= pdevice
->info
.gen
>= 8;
911 features
->storagePushConstant8
= pdevice
->info
.gen
>= 8;
915 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_16BIT_STORAGE_FEATURES
: {
916 VkPhysicalDevice16BitStorageFeatures
*features
=
917 (VkPhysicalDevice16BitStorageFeatures
*)ext
;
918 features
->storageBuffer16BitAccess
= pdevice
->info
.gen
>= 8;
919 features
->uniformAndStorageBuffer16BitAccess
= pdevice
->info
.gen
>= 8;
920 features
->storagePushConstant16
= pdevice
->info
.gen
>= 8;
921 features
->storageInputOutput16
= false;
925 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_BUFFER_ADDRESS_FEATURES_EXT
: {
926 VkPhysicalDeviceBufferAddressFeaturesEXT
*features
= (void *)ext
;
927 features
->bufferDeviceAddress
= pdevice
->use_softpin
&&
928 pdevice
->info
.gen
>= 8;
929 features
->bufferDeviceAddressCaptureReplay
= false;
930 features
->bufferDeviceAddressMultiDevice
= false;
934 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_CONDITIONAL_RENDERING_FEATURES_EXT
: {
935 VkPhysicalDeviceConditionalRenderingFeaturesEXT
*features
=
936 (VkPhysicalDeviceConditionalRenderingFeaturesEXT
*)ext
;
937 ANV_FROM_HANDLE(anv_physical_device
, pdevice
, physicalDevice
);
939 features
->conditionalRendering
= pdevice
->info
.gen
>= 8 ||
940 pdevice
->info
.is_haswell
;
941 features
->inheritedConditionalRendering
= pdevice
->info
.gen
>= 8 ||
942 pdevice
->info
.is_haswell
;
946 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_DEPTH_CLIP_ENABLE_FEATURES_EXT
: {
947 VkPhysicalDeviceDepthClipEnableFeaturesEXT
*features
=
948 (VkPhysicalDeviceDepthClipEnableFeaturesEXT
*)ext
;
949 features
->depthClipEnable
= VK_TRUE
;
953 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_INLINE_UNIFORM_BLOCK_FEATURES_EXT
: {
954 VkPhysicalDeviceInlineUniformBlockFeaturesEXT
*features
=
955 (VkPhysicalDeviceInlineUniformBlockFeaturesEXT
*)ext
;
956 features
->inlineUniformBlock
= true;
957 features
->descriptorBindingInlineUniformBlockUpdateAfterBind
= false;
961 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_MULTIVIEW_FEATURES
: {
962 VkPhysicalDeviceMultiviewFeatures
*features
=
963 (VkPhysicalDeviceMultiviewFeatures
*)ext
;
964 features
->multiview
= true;
965 features
->multiviewGeometryShader
= true;
966 features
->multiviewTessellationShader
= true;
970 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PROTECTED_MEMORY_FEATURES
: {
971 VkPhysicalDeviceProtectedMemoryFeatures
*features
= (void *)ext
;
972 features
->protectedMemory
= VK_FALSE
;
976 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SAMPLER_YCBCR_CONVERSION_FEATURES
: {
977 VkPhysicalDeviceSamplerYcbcrConversionFeatures
*features
=
978 (VkPhysicalDeviceSamplerYcbcrConversionFeatures
*) ext
;
979 features
->samplerYcbcrConversion
= true;
983 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SCALAR_BLOCK_LAYOUT_FEATURES_EXT
: {
984 VkPhysicalDeviceScalarBlockLayoutFeaturesEXT
*features
=
985 (VkPhysicalDeviceScalarBlockLayoutFeaturesEXT
*)ext
;
986 features
->scalarBlockLayout
= true;
990 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SHADER_DRAW_PARAMETER_FEATURES
: {
991 VkPhysicalDeviceShaderDrawParameterFeatures
*features
= (void *)ext
;
992 features
->shaderDrawParameters
= true;
996 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VARIABLE_POINTER_FEATURES
: {
997 VkPhysicalDeviceVariablePointerFeatures
*features
= (void *)ext
;
998 features
->variablePointersStorageBuffer
= true;
999 features
->variablePointers
= true;
1003 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_TRANSFORM_FEEDBACK_FEATURES_EXT
: {
1004 VkPhysicalDeviceTransformFeedbackFeaturesEXT
*features
=
1005 (VkPhysicalDeviceTransformFeedbackFeaturesEXT
*)ext
;
1006 features
->transformFeedback
= VK_TRUE
;
1007 features
->geometryStreams
= VK_TRUE
;
1011 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VERTEX_ATTRIBUTE_DIVISOR_FEATURES_EXT
: {
1012 VkPhysicalDeviceVertexAttributeDivisorFeaturesEXT
*features
=
1013 (VkPhysicalDeviceVertexAttributeDivisorFeaturesEXT
*)ext
;
1014 features
->vertexAttributeInstanceRateDivisor
= VK_TRUE
;
1015 features
->vertexAttributeInstanceRateZeroDivisor
= VK_TRUE
;
1019 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_YCBCR_IMAGE_ARRAYS_FEATURES_EXT
: {
1020 VkPhysicalDeviceYcbcrImageArraysFeaturesEXT
*features
=
1021 (VkPhysicalDeviceYcbcrImageArraysFeaturesEXT
*)ext
;
1022 features
->ycbcrImageArrays
= VK_TRUE
;
1027 anv_debug_ignored_stype(ext
->sType
);
1033 void anv_GetPhysicalDeviceProperties(
1034 VkPhysicalDevice physicalDevice
,
1035 VkPhysicalDeviceProperties
* pProperties
)
1037 ANV_FROM_HANDLE(anv_physical_device
, pdevice
, physicalDevice
);
1038 const struct gen_device_info
*devinfo
= &pdevice
->info
;
1040 /* See assertions made when programming the buffer surface state. */
1041 const uint32_t max_raw_buffer_sz
= devinfo
->gen
>= 7 ?
1042 (1ul << 30) : (1ul << 27);
1044 const uint32_t max_samplers
= (devinfo
->gen
>= 8 || devinfo
->is_haswell
) ?
1047 const uint32_t max_images
= devinfo
->gen
< 9 ? MAX_GEN8_IMAGES
: MAX_IMAGES
;
1049 VkSampleCountFlags sample_counts
=
1050 isl_device_get_sample_counts(&pdevice
->isl_dev
);
1053 VkPhysicalDeviceLimits limits
= {
1054 .maxImageDimension1D
= (1 << 14),
1055 .maxImageDimension2D
= (1 << 14),
1056 .maxImageDimension3D
= (1 << 11),
1057 .maxImageDimensionCube
= (1 << 14),
1058 .maxImageArrayLayers
= (1 << 11),
1059 .maxTexelBufferElements
= 128 * 1024 * 1024,
1060 .maxUniformBufferRange
= (1ul << 27),
1061 .maxStorageBufferRange
= max_raw_buffer_sz
,
1062 .maxPushConstantsSize
= MAX_PUSH_CONSTANTS_SIZE
,
1063 .maxMemoryAllocationCount
= UINT32_MAX
,
1064 .maxSamplerAllocationCount
= 64 * 1024,
1065 .bufferImageGranularity
= 64, /* A cache line */
1066 .sparseAddressSpaceSize
= 0,
1067 .maxBoundDescriptorSets
= MAX_SETS
,
1068 .maxPerStageDescriptorSamplers
= max_samplers
,
1069 .maxPerStageDescriptorUniformBuffers
= 64,
1070 .maxPerStageDescriptorStorageBuffers
= 64,
1071 .maxPerStageDescriptorSampledImages
= max_samplers
,
1072 .maxPerStageDescriptorStorageImages
= max_images
,
1073 .maxPerStageDescriptorInputAttachments
= 64,
1074 .maxPerStageResources
= 250,
1075 .maxDescriptorSetSamplers
= 6 * max_samplers
, /* number of stages * maxPerStageDescriptorSamplers */
1076 .maxDescriptorSetUniformBuffers
= 6 * 64, /* number of stages * maxPerStageDescriptorUniformBuffers */
1077 .maxDescriptorSetUniformBuffersDynamic
= MAX_DYNAMIC_BUFFERS
/ 2,
1078 .maxDescriptorSetStorageBuffers
= 6 * 64, /* number of stages * maxPerStageDescriptorStorageBuffers */
1079 .maxDescriptorSetStorageBuffersDynamic
= MAX_DYNAMIC_BUFFERS
/ 2,
1080 .maxDescriptorSetSampledImages
= 6 * max_samplers
, /* number of stages * maxPerStageDescriptorSampledImages */
1081 .maxDescriptorSetStorageImages
= 6 * max_images
, /* number of stages * maxPerStageDescriptorStorageImages */
1082 .maxDescriptorSetInputAttachments
= 256,
1083 .maxVertexInputAttributes
= MAX_VBS
,
1084 .maxVertexInputBindings
= MAX_VBS
,
1085 .maxVertexInputAttributeOffset
= 2047,
1086 .maxVertexInputBindingStride
= 2048,
1087 .maxVertexOutputComponents
= 128,
1088 .maxTessellationGenerationLevel
= 64,
1089 .maxTessellationPatchSize
= 32,
1090 .maxTessellationControlPerVertexInputComponents
= 128,
1091 .maxTessellationControlPerVertexOutputComponents
= 128,
1092 .maxTessellationControlPerPatchOutputComponents
= 128,
1093 .maxTessellationControlTotalOutputComponents
= 2048,
1094 .maxTessellationEvaluationInputComponents
= 128,
1095 .maxTessellationEvaluationOutputComponents
= 128,
1096 .maxGeometryShaderInvocations
= 32,
1097 .maxGeometryInputComponents
= 64,
1098 .maxGeometryOutputComponents
= 128,
1099 .maxGeometryOutputVertices
= 256,
1100 .maxGeometryTotalOutputComponents
= 1024,
1101 .maxFragmentInputComponents
= 112, /* 128 components - (POS, PSIZ, CLIP_DIST0, CLIP_DIST1) */
1102 .maxFragmentOutputAttachments
= 8,
1103 .maxFragmentDualSrcAttachments
= 1,
1104 .maxFragmentCombinedOutputResources
= 8,
1105 .maxComputeSharedMemorySize
= 32768,
1106 .maxComputeWorkGroupCount
= { 65535, 65535, 65535 },
1107 .maxComputeWorkGroupInvocations
= 16 * devinfo
->max_cs_threads
,
1108 .maxComputeWorkGroupSize
= {
1109 16 * devinfo
->max_cs_threads
,
1110 16 * devinfo
->max_cs_threads
,
1111 16 * devinfo
->max_cs_threads
,
1113 .subPixelPrecisionBits
= 8,
1114 .subTexelPrecisionBits
= 4 /* FIXME */,
1115 .mipmapPrecisionBits
= 4 /* FIXME */,
1116 .maxDrawIndexedIndexValue
= UINT32_MAX
,
1117 .maxDrawIndirectCount
= UINT32_MAX
,
1118 .maxSamplerLodBias
= 16,
1119 .maxSamplerAnisotropy
= 16,
1120 .maxViewports
= MAX_VIEWPORTS
,
1121 .maxViewportDimensions
= { (1 << 14), (1 << 14) },
1122 .viewportBoundsRange
= { INT16_MIN
, INT16_MAX
},
1123 .viewportSubPixelBits
= 13, /* We take a float? */
1124 .minMemoryMapAlignment
= 4096, /* A page */
1125 .minTexelBufferOffsetAlignment
= 1,
1126 /* We need 16 for UBO block reads to work and 32 for push UBOs */
1127 .minUniformBufferOffsetAlignment
= 32,
1128 .minStorageBufferOffsetAlignment
= 4,
1129 .minTexelOffset
= -8,
1130 .maxTexelOffset
= 7,
1131 .minTexelGatherOffset
= -32,
1132 .maxTexelGatherOffset
= 31,
1133 .minInterpolationOffset
= -0.5,
1134 .maxInterpolationOffset
= 0.4375,
1135 .subPixelInterpolationOffsetBits
= 4,
1136 .maxFramebufferWidth
= (1 << 14),
1137 .maxFramebufferHeight
= (1 << 14),
1138 .maxFramebufferLayers
= (1 << 11),
1139 .framebufferColorSampleCounts
= sample_counts
,
1140 .framebufferDepthSampleCounts
= sample_counts
,
1141 .framebufferStencilSampleCounts
= sample_counts
,
1142 .framebufferNoAttachmentsSampleCounts
= sample_counts
,
1143 .maxColorAttachments
= MAX_RTS
,
1144 .sampledImageColorSampleCounts
= sample_counts
,
1145 .sampledImageIntegerSampleCounts
= VK_SAMPLE_COUNT_1_BIT
,
1146 .sampledImageDepthSampleCounts
= sample_counts
,
1147 .sampledImageStencilSampleCounts
= sample_counts
,
1148 .storageImageSampleCounts
= VK_SAMPLE_COUNT_1_BIT
,
1149 .maxSampleMaskWords
= 1,
1150 .timestampComputeAndGraphics
= false,
1151 .timestampPeriod
= 1000000000.0 / devinfo
->timestamp_frequency
,
1152 .maxClipDistances
= 8,
1153 .maxCullDistances
= 8,
1154 .maxCombinedClipAndCullDistances
= 8,
1155 .discreteQueuePriorities
= 2,
1156 .pointSizeRange
= { 0.125, 255.875 },
1157 .lineWidthRange
= { 0.0, 7.9921875 },
1158 .pointSizeGranularity
= (1.0 / 8.0),
1159 .lineWidthGranularity
= (1.0 / 128.0),
1160 .strictLines
= false, /* FINISHME */
1161 .standardSampleLocations
= true,
1162 .optimalBufferCopyOffsetAlignment
= 128,
1163 .optimalBufferCopyRowPitchAlignment
= 128,
1164 .nonCoherentAtomSize
= 64,
1167 *pProperties
= (VkPhysicalDeviceProperties
) {
1168 .apiVersion
= anv_physical_device_api_version(pdevice
),
1169 .driverVersion
= vk_get_driver_version(),
1171 .deviceID
= pdevice
->chipset_id
,
1172 .deviceType
= VK_PHYSICAL_DEVICE_TYPE_INTEGRATED_GPU
,
1174 .sparseProperties
= {0}, /* Broadwell doesn't do sparse. */
1177 snprintf(pProperties
->deviceName
, sizeof(pProperties
->deviceName
),
1178 "%s", pdevice
->name
);
1179 memcpy(pProperties
->pipelineCacheUUID
,
1180 pdevice
->pipeline_cache_uuid
, VK_UUID_SIZE
);
1183 void anv_GetPhysicalDeviceProperties2(
1184 VkPhysicalDevice physicalDevice
,
1185 VkPhysicalDeviceProperties2
* pProperties
)
1187 ANV_FROM_HANDLE(anv_physical_device
, pdevice
, physicalDevice
);
1189 anv_GetPhysicalDeviceProperties(physicalDevice
, &pProperties
->properties
);
1191 vk_foreach_struct(ext
, pProperties
->pNext
) {
1192 switch (ext
->sType
) {
1193 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_DEPTH_STENCIL_RESOLVE_PROPERTIES_KHR
: {
1194 VkPhysicalDeviceDepthStencilResolvePropertiesKHR
*props
=
1195 (VkPhysicalDeviceDepthStencilResolvePropertiesKHR
*)ext
;
1197 /* We support all of the depth resolve modes */
1198 props
->supportedDepthResolveModes
=
1199 VK_RESOLVE_MODE_SAMPLE_ZERO_BIT_KHR
|
1200 VK_RESOLVE_MODE_AVERAGE_BIT_KHR
|
1201 VK_RESOLVE_MODE_MIN_BIT_KHR
|
1202 VK_RESOLVE_MODE_MAX_BIT_KHR
;
1204 /* Average doesn't make sense for stencil so we don't support that */
1205 props
->supportedStencilResolveModes
=
1206 VK_RESOLVE_MODE_SAMPLE_ZERO_BIT_KHR
;
1207 if (pdevice
->info
.gen
>= 8) {
1208 /* The advanced stencil resolve modes currently require stencil
1209 * sampling be supported by the hardware.
1211 props
->supportedStencilResolveModes
|=
1212 VK_RESOLVE_MODE_MIN_BIT_KHR
|
1213 VK_RESOLVE_MODE_MAX_BIT_KHR
;
1216 props
->independentResolveNone
= VK_TRUE
;
1217 props
->independentResolve
= VK_TRUE
;
1221 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_DRIVER_PROPERTIES_KHR
: {
1222 VkPhysicalDeviceDriverPropertiesKHR
*driver_props
=
1223 (VkPhysicalDeviceDriverPropertiesKHR
*) ext
;
1225 driver_props
->driverID
= VK_DRIVER_ID_INTEL_OPEN_SOURCE_MESA_KHR
;
1226 util_snprintf(driver_props
->driverName
, VK_MAX_DRIVER_NAME_SIZE_KHR
,
1227 "Intel open-source Mesa driver");
1229 util_snprintf(driver_props
->driverInfo
, VK_MAX_DRIVER_INFO_SIZE_KHR
,
1230 "Mesa " PACKAGE_VERSION MESA_GIT_SHA1
);
1232 driver_props
->conformanceVersion
= (VkConformanceVersionKHR
) {
1241 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_EXTERNAL_MEMORY_HOST_PROPERTIES_EXT
: {
1242 VkPhysicalDeviceExternalMemoryHostPropertiesEXT
*props
=
1243 (VkPhysicalDeviceExternalMemoryHostPropertiesEXT
*) ext
;
1244 /* Userptr needs page aligned memory. */
1245 props
->minImportedHostPointerAlignment
= 4096;
1249 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_ID_PROPERTIES
: {
1250 VkPhysicalDeviceIDProperties
*id_props
=
1251 (VkPhysicalDeviceIDProperties
*)ext
;
1252 memcpy(id_props
->deviceUUID
, pdevice
->device_uuid
, VK_UUID_SIZE
);
1253 memcpy(id_props
->driverUUID
, pdevice
->driver_uuid
, VK_UUID_SIZE
);
1254 /* The LUID is for Windows. */
1255 id_props
->deviceLUIDValid
= false;
1259 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_INLINE_UNIFORM_BLOCK_PROPERTIES_EXT
: {
1260 VkPhysicalDeviceInlineUniformBlockPropertiesEXT
*props
=
1261 (VkPhysicalDeviceInlineUniformBlockPropertiesEXT
*)ext
;
1262 props
->maxInlineUniformBlockSize
= MAX_INLINE_UNIFORM_BLOCK_SIZE
;
1263 props
->maxPerStageDescriptorInlineUniformBlocks
=
1264 MAX_INLINE_UNIFORM_BLOCK_DESCRIPTORS
;
1265 props
->maxPerStageDescriptorUpdateAfterBindInlineUniformBlocks
=
1266 MAX_INLINE_UNIFORM_BLOCK_DESCRIPTORS
;
1267 props
->maxDescriptorSetInlineUniformBlocks
=
1268 MAX_INLINE_UNIFORM_BLOCK_DESCRIPTORS
;
1269 props
->maxDescriptorSetUpdateAfterBindInlineUniformBlocks
=
1270 MAX_INLINE_UNIFORM_BLOCK_DESCRIPTORS
;
1274 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_MAINTENANCE_3_PROPERTIES
: {
1275 VkPhysicalDeviceMaintenance3Properties
*props
=
1276 (VkPhysicalDeviceMaintenance3Properties
*)ext
;
1277 /* This value doesn't matter for us today as our per-stage
1278 * descriptors are the real limit.
1280 props
->maxPerSetDescriptors
= 1024;
1281 props
->maxMemoryAllocationSize
= MAX_MEMORY_ALLOCATION_SIZE
;
1285 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_MULTIVIEW_PROPERTIES
: {
1286 VkPhysicalDeviceMultiviewProperties
*properties
=
1287 (VkPhysicalDeviceMultiviewProperties
*)ext
;
1288 properties
->maxMultiviewViewCount
= 16;
1289 properties
->maxMultiviewInstanceIndex
= UINT32_MAX
/ 16;
1293 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PCI_BUS_INFO_PROPERTIES_EXT
: {
1294 VkPhysicalDevicePCIBusInfoPropertiesEXT
*properties
=
1295 (VkPhysicalDevicePCIBusInfoPropertiesEXT
*)ext
;
1296 properties
->pciDomain
= pdevice
->pci_info
.domain
;
1297 properties
->pciBus
= pdevice
->pci_info
.bus
;
1298 properties
->pciDevice
= pdevice
->pci_info
.device
;
1299 properties
->pciFunction
= pdevice
->pci_info
.function
;
1303 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_POINT_CLIPPING_PROPERTIES
: {
1304 VkPhysicalDevicePointClippingProperties
*properties
=
1305 (VkPhysicalDevicePointClippingProperties
*) ext
;
1306 properties
->pointClippingBehavior
= VK_POINT_CLIPPING_BEHAVIOR_ALL_CLIP_PLANES
;
1307 anv_finishme("Implement pop-free point clipping");
1311 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PROTECTED_MEMORY_PROPERTIES
: {
1312 VkPhysicalDeviceProtectedMemoryProperties
*props
=
1313 (VkPhysicalDeviceProtectedMemoryProperties
*)ext
;
1314 props
->protectedNoFault
= false;
1318 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PUSH_DESCRIPTOR_PROPERTIES_KHR
: {
1319 VkPhysicalDevicePushDescriptorPropertiesKHR
*properties
=
1320 (VkPhysicalDevicePushDescriptorPropertiesKHR
*) ext
;
1322 properties
->maxPushDescriptors
= MAX_PUSH_DESCRIPTORS
;
1326 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SAMPLER_FILTER_MINMAX_PROPERTIES_EXT
: {
1327 VkPhysicalDeviceSamplerFilterMinmaxPropertiesEXT
*properties
=
1328 (VkPhysicalDeviceSamplerFilterMinmaxPropertiesEXT
*)ext
;
1329 properties
->filterMinmaxImageComponentMapping
= pdevice
->info
.gen
>= 9;
1330 properties
->filterMinmaxSingleComponentFormats
= true;
1334 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SUBGROUP_PROPERTIES
: {
1335 VkPhysicalDeviceSubgroupProperties
*properties
= (void *)ext
;
1337 properties
->subgroupSize
= BRW_SUBGROUP_SIZE
;
1339 VkShaderStageFlags scalar_stages
= 0;
1340 for (unsigned stage
= 0; stage
< MESA_SHADER_STAGES
; stage
++) {
1341 if (pdevice
->compiler
->scalar_stage
[stage
])
1342 scalar_stages
|= mesa_to_vk_shader_stage(stage
);
1344 properties
->supportedStages
= scalar_stages
;
1346 properties
->supportedOperations
= VK_SUBGROUP_FEATURE_BASIC_BIT
|
1347 VK_SUBGROUP_FEATURE_VOTE_BIT
|
1348 VK_SUBGROUP_FEATURE_ARITHMETIC_BIT
|
1349 VK_SUBGROUP_FEATURE_BALLOT_BIT
|
1350 VK_SUBGROUP_FEATURE_SHUFFLE_BIT
|
1351 VK_SUBGROUP_FEATURE_SHUFFLE_RELATIVE_BIT
|
1352 VK_SUBGROUP_FEATURE_CLUSTERED_BIT
|
1353 VK_SUBGROUP_FEATURE_QUAD_BIT
;
1354 properties
->quadOperationsInAllStages
= VK_TRUE
;
1358 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_TRANSFORM_FEEDBACK_PROPERTIES_EXT
: {
1359 VkPhysicalDeviceTransformFeedbackPropertiesEXT
*props
=
1360 (VkPhysicalDeviceTransformFeedbackPropertiesEXT
*)ext
;
1362 props
->maxTransformFeedbackStreams
= MAX_XFB_STREAMS
;
1363 props
->maxTransformFeedbackBuffers
= MAX_XFB_BUFFERS
;
1364 props
->maxTransformFeedbackBufferSize
= (1ull << 32);
1365 props
->maxTransformFeedbackStreamDataSize
= 128 * 4;
1366 props
->maxTransformFeedbackBufferDataSize
= 128 * 4;
1367 props
->maxTransformFeedbackBufferDataStride
= 2048;
1368 props
->transformFeedbackQueries
= VK_TRUE
;
1369 props
->transformFeedbackStreamsLinesTriangles
= VK_FALSE
;
1370 props
->transformFeedbackRasterizationStreamSelect
= VK_FALSE
;
1371 props
->transformFeedbackDraw
= VK_TRUE
;
1375 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VERTEX_ATTRIBUTE_DIVISOR_PROPERTIES_EXT
: {
1376 VkPhysicalDeviceVertexAttributeDivisorPropertiesEXT
*props
=
1377 (VkPhysicalDeviceVertexAttributeDivisorPropertiesEXT
*)ext
;
1378 /* We have to restrict this a bit for multiview */
1379 props
->maxVertexAttribDivisor
= UINT32_MAX
/ 16;
1384 anv_debug_ignored_stype(ext
->sType
);
1390 /* We support exactly one queue family. */
1391 static const VkQueueFamilyProperties
1392 anv_queue_family_properties
= {
1393 .queueFlags
= VK_QUEUE_GRAPHICS_BIT
|
1394 VK_QUEUE_COMPUTE_BIT
|
1395 VK_QUEUE_TRANSFER_BIT
,
1397 .timestampValidBits
= 36, /* XXX: Real value here */
1398 .minImageTransferGranularity
= { 1, 1, 1 },
1401 void anv_GetPhysicalDeviceQueueFamilyProperties(
1402 VkPhysicalDevice physicalDevice
,
1404 VkQueueFamilyProperties
* pQueueFamilyProperties
)
1406 VK_OUTARRAY_MAKE(out
, pQueueFamilyProperties
, pCount
);
1408 vk_outarray_append(&out
, p
) {
1409 *p
= anv_queue_family_properties
;
1413 void anv_GetPhysicalDeviceQueueFamilyProperties2(
1414 VkPhysicalDevice physicalDevice
,
1415 uint32_t* pQueueFamilyPropertyCount
,
1416 VkQueueFamilyProperties2
* pQueueFamilyProperties
)
1419 VK_OUTARRAY_MAKE(out
, pQueueFamilyProperties
, pQueueFamilyPropertyCount
);
1421 vk_outarray_append(&out
, p
) {
1422 p
->queueFamilyProperties
= anv_queue_family_properties
;
1424 vk_foreach_struct(s
, p
->pNext
) {
1425 anv_debug_ignored_stype(s
->sType
);
1430 void anv_GetPhysicalDeviceMemoryProperties(
1431 VkPhysicalDevice physicalDevice
,
1432 VkPhysicalDeviceMemoryProperties
* pMemoryProperties
)
1434 ANV_FROM_HANDLE(anv_physical_device
, physical_device
, physicalDevice
);
1436 pMemoryProperties
->memoryTypeCount
= physical_device
->memory
.type_count
;
1437 for (uint32_t i
= 0; i
< physical_device
->memory
.type_count
; i
++) {
1438 pMemoryProperties
->memoryTypes
[i
] = (VkMemoryType
) {
1439 .propertyFlags
= physical_device
->memory
.types
[i
].propertyFlags
,
1440 .heapIndex
= physical_device
->memory
.types
[i
].heapIndex
,
1444 pMemoryProperties
->memoryHeapCount
= physical_device
->memory
.heap_count
;
1445 for (uint32_t i
= 0; i
< physical_device
->memory
.heap_count
; i
++) {
1446 pMemoryProperties
->memoryHeaps
[i
] = (VkMemoryHeap
) {
1447 .size
= physical_device
->memory
.heaps
[i
].size
,
1448 .flags
= physical_device
->memory
.heaps
[i
].flags
,
1453 void anv_GetPhysicalDeviceMemoryProperties2(
1454 VkPhysicalDevice physicalDevice
,
1455 VkPhysicalDeviceMemoryProperties2
* pMemoryProperties
)
1457 anv_GetPhysicalDeviceMemoryProperties(physicalDevice
,
1458 &pMemoryProperties
->memoryProperties
);
1460 vk_foreach_struct(ext
, pMemoryProperties
->pNext
) {
1461 switch (ext
->sType
) {
1463 anv_debug_ignored_stype(ext
->sType
);
1470 anv_GetDeviceGroupPeerMemoryFeatures(
1473 uint32_t localDeviceIndex
,
1474 uint32_t remoteDeviceIndex
,
1475 VkPeerMemoryFeatureFlags
* pPeerMemoryFeatures
)
1477 assert(localDeviceIndex
== 0 && remoteDeviceIndex
== 0);
1478 *pPeerMemoryFeatures
= VK_PEER_MEMORY_FEATURE_COPY_SRC_BIT
|
1479 VK_PEER_MEMORY_FEATURE_COPY_DST_BIT
|
1480 VK_PEER_MEMORY_FEATURE_GENERIC_SRC_BIT
|
1481 VK_PEER_MEMORY_FEATURE_GENERIC_DST_BIT
;
1484 PFN_vkVoidFunction
anv_GetInstanceProcAddr(
1485 VkInstance _instance
,
1488 ANV_FROM_HANDLE(anv_instance
, instance
, _instance
);
1490 /* The Vulkan 1.0 spec for vkGetInstanceProcAddr has a table of exactly
1491 * when we have to return valid function pointers, NULL, or it's left
1492 * undefined. See the table for exact details.
1497 #define LOOKUP_ANV_ENTRYPOINT(entrypoint) \
1498 if (strcmp(pName, "vk" #entrypoint) == 0) \
1499 return (PFN_vkVoidFunction)anv_##entrypoint
1501 LOOKUP_ANV_ENTRYPOINT(EnumerateInstanceExtensionProperties
);
1502 LOOKUP_ANV_ENTRYPOINT(EnumerateInstanceLayerProperties
);
1503 LOOKUP_ANV_ENTRYPOINT(EnumerateInstanceVersion
);
1504 LOOKUP_ANV_ENTRYPOINT(CreateInstance
);
1506 #undef LOOKUP_ANV_ENTRYPOINT
1508 if (instance
== NULL
)
1511 int idx
= anv_get_instance_entrypoint_index(pName
);
1513 return instance
->dispatch
.entrypoints
[idx
];
1515 idx
= anv_get_device_entrypoint_index(pName
);
1517 return instance
->device_dispatch
.entrypoints
[idx
];
1522 /* With version 1+ of the loader interface the ICD should expose
1523 * vk_icdGetInstanceProcAddr to work around certain LD_PRELOAD issues seen in apps.
1526 VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL
vk_icdGetInstanceProcAddr(
1527 VkInstance instance
,
1531 VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL
vk_icdGetInstanceProcAddr(
1532 VkInstance instance
,
1535 return anv_GetInstanceProcAddr(instance
, pName
);
1538 PFN_vkVoidFunction
anv_GetDeviceProcAddr(
1542 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1544 if (!device
|| !pName
)
1547 int idx
= anv_get_device_entrypoint_index(pName
);
1551 return device
->dispatch
.entrypoints
[idx
];
1555 anv_CreateDebugReportCallbackEXT(VkInstance _instance
,
1556 const VkDebugReportCallbackCreateInfoEXT
* pCreateInfo
,
1557 const VkAllocationCallbacks
* pAllocator
,
1558 VkDebugReportCallbackEXT
* pCallback
)
1560 ANV_FROM_HANDLE(anv_instance
, instance
, _instance
);
1561 return vk_create_debug_report_callback(&instance
->debug_report_callbacks
,
1562 pCreateInfo
, pAllocator
, &instance
->alloc
,
1567 anv_DestroyDebugReportCallbackEXT(VkInstance _instance
,
1568 VkDebugReportCallbackEXT _callback
,
1569 const VkAllocationCallbacks
* pAllocator
)
1571 ANV_FROM_HANDLE(anv_instance
, instance
, _instance
);
1572 vk_destroy_debug_report_callback(&instance
->debug_report_callbacks
,
1573 _callback
, pAllocator
, &instance
->alloc
);
1577 anv_DebugReportMessageEXT(VkInstance _instance
,
1578 VkDebugReportFlagsEXT flags
,
1579 VkDebugReportObjectTypeEXT objectType
,
1582 int32_t messageCode
,
1583 const char* pLayerPrefix
,
1584 const char* pMessage
)
1586 ANV_FROM_HANDLE(anv_instance
, instance
, _instance
);
1587 vk_debug_report(&instance
->debug_report_callbacks
, flags
, objectType
,
1588 object
, location
, messageCode
, pLayerPrefix
, pMessage
);
1592 anv_queue_init(struct anv_device
*device
, struct anv_queue
*queue
)
1594 queue
->_loader_data
.loaderMagic
= ICD_LOADER_MAGIC
;
1595 queue
->device
= device
;
1600 anv_queue_finish(struct anv_queue
*queue
)
1604 static struct anv_state
1605 anv_state_pool_emit_data(struct anv_state_pool
*pool
, size_t size
, size_t align
, const void *p
)
1607 struct anv_state state
;
1609 state
= anv_state_pool_alloc(pool
, size
, align
);
1610 memcpy(state
.map
, p
, size
);
1615 struct gen8_border_color
{
1620 /* Pad out to 64 bytes */
1625 anv_device_init_border_colors(struct anv_device
*device
)
1627 static const struct gen8_border_color border_colors
[] = {
1628 [VK_BORDER_COLOR_FLOAT_TRANSPARENT_BLACK
] = { .float32
= { 0.0, 0.0, 0.0, 0.0 } },
1629 [VK_BORDER_COLOR_FLOAT_OPAQUE_BLACK
] = { .float32
= { 0.0, 0.0, 0.0, 1.0 } },
1630 [VK_BORDER_COLOR_FLOAT_OPAQUE_WHITE
] = { .float32
= { 1.0, 1.0, 1.0, 1.0 } },
1631 [VK_BORDER_COLOR_INT_TRANSPARENT_BLACK
] = { .uint32
= { 0, 0, 0, 0 } },
1632 [VK_BORDER_COLOR_INT_OPAQUE_BLACK
] = { .uint32
= { 0, 0, 0, 1 } },
1633 [VK_BORDER_COLOR_INT_OPAQUE_WHITE
] = { .uint32
= { 1, 1, 1, 1 } },
1636 device
->border_colors
= anv_state_pool_emit_data(&device
->dynamic_state_pool
,
1637 sizeof(border_colors
), 64,
1642 anv_device_init_trivial_batch(struct anv_device
*device
)
1644 anv_bo_init_new(&device
->trivial_batch_bo
, device
, 4096);
1646 if (device
->instance
->physicalDevice
.has_exec_async
)
1647 device
->trivial_batch_bo
.flags
|= EXEC_OBJECT_ASYNC
;
1649 if (device
->instance
->physicalDevice
.use_softpin
)
1650 device
->trivial_batch_bo
.flags
|= EXEC_OBJECT_PINNED
;
1652 anv_vma_alloc(device
, &device
->trivial_batch_bo
);
1654 void *map
= anv_gem_mmap(device
, device
->trivial_batch_bo
.gem_handle
,
1657 struct anv_batch batch
= {
1663 anv_batch_emit(&batch
, GEN7_MI_BATCH_BUFFER_END
, bbe
);
1664 anv_batch_emit(&batch
, GEN7_MI_NOOP
, noop
);
1666 if (!device
->info
.has_llc
)
1667 gen_clflush_range(map
, batch
.next
- map
);
1669 anv_gem_munmap(map
, device
->trivial_batch_bo
.size
);
1672 VkResult
anv_EnumerateDeviceExtensionProperties(
1673 VkPhysicalDevice physicalDevice
,
1674 const char* pLayerName
,
1675 uint32_t* pPropertyCount
,
1676 VkExtensionProperties
* pProperties
)
1678 ANV_FROM_HANDLE(anv_physical_device
, device
, physicalDevice
);
1679 VK_OUTARRAY_MAKE(out
, pProperties
, pPropertyCount
);
1681 for (int i
= 0; i
< ANV_DEVICE_EXTENSION_COUNT
; i
++) {
1682 if (device
->supported_extensions
.extensions
[i
]) {
1683 vk_outarray_append(&out
, prop
) {
1684 *prop
= anv_device_extensions
[i
];
1689 return vk_outarray_status(&out
);
1693 anv_device_init_dispatch(struct anv_device
*device
)
1695 const struct anv_device_dispatch_table
*genX_table
;
1696 switch (device
->info
.gen
) {
1698 genX_table
= &gen11_device_dispatch_table
;
1701 genX_table
= &gen10_device_dispatch_table
;
1704 genX_table
= &gen9_device_dispatch_table
;
1707 genX_table
= &gen8_device_dispatch_table
;
1710 if (device
->info
.is_haswell
)
1711 genX_table
= &gen75_device_dispatch_table
;
1713 genX_table
= &gen7_device_dispatch_table
;
1716 unreachable("unsupported gen\n");
1719 for (unsigned i
= 0; i
< ARRAY_SIZE(device
->dispatch
.entrypoints
); i
++) {
1720 /* Vulkan requires that entrypoints for extensions which have not been
1721 * enabled must not be advertised.
1723 if (!anv_device_entrypoint_is_enabled(i
, device
->instance
->app_info
.api_version
,
1724 &device
->instance
->enabled_extensions
,
1725 &device
->enabled_extensions
)) {
1726 device
->dispatch
.entrypoints
[i
] = NULL
;
1727 } else if (genX_table
->entrypoints
[i
]) {
1728 device
->dispatch
.entrypoints
[i
] = genX_table
->entrypoints
[i
];
1730 device
->dispatch
.entrypoints
[i
] =
1731 anv_device_dispatch_table
.entrypoints
[i
];
1737 vk_priority_to_gen(int priority
)
1740 case VK_QUEUE_GLOBAL_PRIORITY_LOW_EXT
:
1741 return GEN_CONTEXT_LOW_PRIORITY
;
1742 case VK_QUEUE_GLOBAL_PRIORITY_MEDIUM_EXT
:
1743 return GEN_CONTEXT_MEDIUM_PRIORITY
;
1744 case VK_QUEUE_GLOBAL_PRIORITY_HIGH_EXT
:
1745 return GEN_CONTEXT_HIGH_PRIORITY
;
1746 case VK_QUEUE_GLOBAL_PRIORITY_REALTIME_EXT
:
1747 return GEN_CONTEXT_REALTIME_PRIORITY
;
1749 unreachable("Invalid priority");
1754 anv_device_init_hiz_clear_value_bo(struct anv_device
*device
)
1756 anv_bo_init_new(&device
->hiz_clear_bo
, device
, 4096);
1758 if (device
->instance
->physicalDevice
.has_exec_async
)
1759 device
->hiz_clear_bo
.flags
|= EXEC_OBJECT_ASYNC
;
1761 if (device
->instance
->physicalDevice
.use_softpin
)
1762 device
->hiz_clear_bo
.flags
|= EXEC_OBJECT_PINNED
;
1764 anv_vma_alloc(device
, &device
->hiz_clear_bo
);
1766 uint32_t *map
= anv_gem_mmap(device
, device
->hiz_clear_bo
.gem_handle
,
1769 union isl_color_value hiz_clear
= { .u32
= { 0, } };
1770 hiz_clear
.f32
[0] = ANV_HZ_FC_VAL
;
1772 memcpy(map
, hiz_clear
.u32
, sizeof(hiz_clear
.u32
));
1773 anv_gem_munmap(map
, device
->hiz_clear_bo
.size
);
1777 get_bo_from_pool(struct gen_batch_decode_bo
*ret
,
1778 struct anv_block_pool
*pool
,
1781 for (uint32_t i
= 0; i
< pool
->nbos
; i
++) {
1782 uint64_t bo_address
= pool
->bos
[i
].offset
& (~0ull >> 16);
1783 uint32_t bo_size
= pool
->bos
[i
].size
;
1784 if (address
>= bo_address
&& address
< (bo_address
+ bo_size
)) {
1785 *ret
= (struct gen_batch_decode_bo
) {
1788 .map
= pool
->bos
[i
].map
,
1796 /* Finding a buffer for batch decoding */
1797 static struct gen_batch_decode_bo
1798 decode_get_bo(void *v_batch
, bool ppgtt
, uint64_t address
)
1800 struct anv_device
*device
= v_batch
;
1801 struct gen_batch_decode_bo ret_bo
= {};
1805 if (get_bo_from_pool(&ret_bo
, &device
->dynamic_state_pool
.block_pool
, address
))
1807 if (get_bo_from_pool(&ret_bo
, &device
->instruction_state_pool
.block_pool
, address
))
1809 if (get_bo_from_pool(&ret_bo
, &device
->binding_table_pool
.block_pool
, address
))
1811 if (get_bo_from_pool(&ret_bo
, &device
->surface_state_pool
.block_pool
, address
))
1814 if (!device
->cmd_buffer_being_decoded
)
1815 return (struct gen_batch_decode_bo
) { };
1817 struct anv_batch_bo
**bo
;
1819 u_vector_foreach(bo
, &device
->cmd_buffer_being_decoded
->seen_bbos
) {
1820 /* The decoder zeroes out the top 16 bits, so we need to as well */
1821 uint64_t bo_address
= (*bo
)->bo
.offset
& (~0ull >> 16);
1823 if (address
>= bo_address
&& address
< bo_address
+ (*bo
)->bo
.size
) {
1824 return (struct gen_batch_decode_bo
) {
1826 .size
= (*bo
)->bo
.size
,
1827 .map
= (*bo
)->bo
.map
,
1832 return (struct gen_batch_decode_bo
) { };
1835 VkResult
anv_CreateDevice(
1836 VkPhysicalDevice physicalDevice
,
1837 const VkDeviceCreateInfo
* pCreateInfo
,
1838 const VkAllocationCallbacks
* pAllocator
,
1841 ANV_FROM_HANDLE(anv_physical_device
, physical_device
, physicalDevice
);
1843 struct anv_device
*device
;
1845 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO
);
1847 struct anv_device_extension_table enabled_extensions
= { };
1848 for (uint32_t i
= 0; i
< pCreateInfo
->enabledExtensionCount
; i
++) {
1850 for (idx
= 0; idx
< ANV_DEVICE_EXTENSION_COUNT
; idx
++) {
1851 if (strcmp(pCreateInfo
->ppEnabledExtensionNames
[i
],
1852 anv_device_extensions
[idx
].extensionName
) == 0)
1856 if (idx
>= ANV_DEVICE_EXTENSION_COUNT
)
1857 return vk_error(VK_ERROR_EXTENSION_NOT_PRESENT
);
1859 if (!physical_device
->supported_extensions
.extensions
[idx
])
1860 return vk_error(VK_ERROR_EXTENSION_NOT_PRESENT
);
1862 enabled_extensions
.extensions
[idx
] = true;
1865 /* Check enabled features */
1866 if (pCreateInfo
->pEnabledFeatures
) {
1867 VkPhysicalDeviceFeatures supported_features
;
1868 anv_GetPhysicalDeviceFeatures(physicalDevice
, &supported_features
);
1869 VkBool32
*supported_feature
= (VkBool32
*)&supported_features
;
1870 VkBool32
*enabled_feature
= (VkBool32
*)pCreateInfo
->pEnabledFeatures
;
1871 unsigned num_features
= sizeof(VkPhysicalDeviceFeatures
) / sizeof(VkBool32
);
1872 for (uint32_t i
= 0; i
< num_features
; i
++) {
1873 if (enabled_feature
[i
] && !supported_feature
[i
])
1874 return vk_error(VK_ERROR_FEATURE_NOT_PRESENT
);
1878 /* Check requested queues and fail if we are requested to create any
1879 * queues with flags we don't support.
1881 assert(pCreateInfo
->queueCreateInfoCount
> 0);
1882 for (uint32_t i
= 0; i
< pCreateInfo
->queueCreateInfoCount
; i
++) {
1883 if (pCreateInfo
->pQueueCreateInfos
[i
].flags
!= 0)
1884 return vk_error(VK_ERROR_INITIALIZATION_FAILED
);
1887 /* Check if client specified queue priority. */
1888 const VkDeviceQueueGlobalPriorityCreateInfoEXT
*queue_priority
=
1889 vk_find_struct_const(pCreateInfo
->pQueueCreateInfos
[0].pNext
,
1890 DEVICE_QUEUE_GLOBAL_PRIORITY_CREATE_INFO_EXT
);
1892 VkQueueGlobalPriorityEXT priority
=
1893 queue_priority
? queue_priority
->globalPriority
:
1894 VK_QUEUE_GLOBAL_PRIORITY_MEDIUM_EXT
;
1896 device
= vk_alloc2(&physical_device
->instance
->alloc
, pAllocator
,
1898 VK_SYSTEM_ALLOCATION_SCOPE_DEVICE
);
1900 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
1902 const unsigned decode_flags
=
1903 GEN_BATCH_DECODE_FULL
|
1904 ((INTEL_DEBUG
& DEBUG_COLOR
) ? GEN_BATCH_DECODE_IN_COLOR
: 0) |
1905 GEN_BATCH_DECODE_OFFSETS
|
1906 GEN_BATCH_DECODE_FLOATS
;
1908 gen_batch_decode_ctx_init(&device
->decoder_ctx
,
1909 &physical_device
->info
,
1910 stderr
, decode_flags
, NULL
,
1911 decode_get_bo
, NULL
, device
);
1913 device
->_loader_data
.loaderMagic
= ICD_LOADER_MAGIC
;
1914 device
->instance
= physical_device
->instance
;
1915 device
->chipset_id
= physical_device
->chipset_id
;
1916 device
->no_hw
= physical_device
->no_hw
;
1917 device
->_lost
= false;
1920 device
->alloc
= *pAllocator
;
1922 device
->alloc
= physical_device
->instance
->alloc
;
1924 /* XXX(chadv): Can we dup() physicalDevice->fd here? */
1925 device
->fd
= open(physical_device
->path
, O_RDWR
| O_CLOEXEC
);
1926 if (device
->fd
== -1) {
1927 result
= vk_error(VK_ERROR_INITIALIZATION_FAILED
);
1931 device
->context_id
= anv_gem_create_context(device
);
1932 if (device
->context_id
== -1) {
1933 result
= vk_error(VK_ERROR_INITIALIZATION_FAILED
);
1937 if (physical_device
->use_softpin
) {
1938 if (pthread_mutex_init(&device
->vma_mutex
, NULL
) != 0) {
1939 result
= vk_error(VK_ERROR_INITIALIZATION_FAILED
);
1943 /* keep the page with address zero out of the allocator */
1944 util_vma_heap_init(&device
->vma_lo
, LOW_HEAP_MIN_ADDRESS
, LOW_HEAP_SIZE
);
1945 device
->vma_lo_available
=
1946 physical_device
->memory
.heaps
[physical_device
->memory
.heap_count
- 1].size
;
1948 /* Leave the last 4GiB out of the high vma range, so that no state base
1949 * address + size can overflow 48 bits. For more information see the
1950 * comment about Wa32bitGeneralStateOffset in anv_allocator.c
1952 util_vma_heap_init(&device
->vma_hi
, HIGH_HEAP_MIN_ADDRESS
,
1954 device
->vma_hi_available
= physical_device
->memory
.heap_count
== 1 ? 0 :
1955 physical_device
->memory
.heaps
[0].size
;
1958 /* As per spec, the driver implementation may deny requests to acquire
1959 * a priority above the default priority (MEDIUM) if the caller does not
1960 * have sufficient privileges. In this scenario VK_ERROR_NOT_PERMITTED_EXT
1963 if (physical_device
->has_context_priority
) {
1964 int err
= anv_gem_set_context_param(device
->fd
, device
->context_id
,
1965 I915_CONTEXT_PARAM_PRIORITY
,
1966 vk_priority_to_gen(priority
));
1967 if (err
!= 0 && priority
> VK_QUEUE_GLOBAL_PRIORITY_MEDIUM_EXT
) {
1968 result
= vk_error(VK_ERROR_NOT_PERMITTED_EXT
);
1973 device
->info
= physical_device
->info
;
1974 device
->isl_dev
= physical_device
->isl_dev
;
1976 /* On Broadwell and later, we can use batch chaining to more efficiently
1977 * implement growing command buffers. Prior to Haswell, the kernel
1978 * command parser gets in the way and we have to fall back to growing
1981 device
->can_chain_batches
= device
->info
.gen
>= 8;
1983 device
->robust_buffer_access
= pCreateInfo
->pEnabledFeatures
&&
1984 pCreateInfo
->pEnabledFeatures
->robustBufferAccess
;
1985 device
->enabled_extensions
= enabled_extensions
;
1987 anv_device_init_dispatch(device
);
1989 if (pthread_mutex_init(&device
->mutex
, NULL
) != 0) {
1990 result
= vk_error(VK_ERROR_INITIALIZATION_FAILED
);
1991 goto fail_context_id
;
1994 pthread_condattr_t condattr
;
1995 if (pthread_condattr_init(&condattr
) != 0) {
1996 result
= vk_error(VK_ERROR_INITIALIZATION_FAILED
);
1999 if (pthread_condattr_setclock(&condattr
, CLOCK_MONOTONIC
) != 0) {
2000 pthread_condattr_destroy(&condattr
);
2001 result
= vk_error(VK_ERROR_INITIALIZATION_FAILED
);
2004 if (pthread_cond_init(&device
->queue_submit
, NULL
) != 0) {
2005 pthread_condattr_destroy(&condattr
);
2006 result
= vk_error(VK_ERROR_INITIALIZATION_FAILED
);
2009 pthread_condattr_destroy(&condattr
);
2012 (physical_device
->supports_48bit_addresses
? EXEC_OBJECT_SUPPORTS_48B_ADDRESS
: 0) |
2013 (physical_device
->has_exec_async
? EXEC_OBJECT_ASYNC
: 0) |
2014 (physical_device
->has_exec_capture
? EXEC_OBJECT_CAPTURE
: 0) |
2015 (physical_device
->use_softpin
? EXEC_OBJECT_PINNED
: 0);
2017 anv_bo_pool_init(&device
->batch_bo_pool
, device
, bo_flags
);
2019 result
= anv_bo_cache_init(&device
->bo_cache
);
2020 if (result
!= VK_SUCCESS
)
2021 goto fail_batch_bo_pool
;
2023 if (!physical_device
->use_softpin
)
2024 bo_flags
&= ~EXEC_OBJECT_SUPPORTS_48B_ADDRESS
;
2026 result
= anv_state_pool_init(&device
->dynamic_state_pool
, device
,
2027 DYNAMIC_STATE_POOL_MIN_ADDRESS
,
2030 if (result
!= VK_SUCCESS
)
2033 result
= anv_state_pool_init(&device
->instruction_state_pool
, device
,
2034 INSTRUCTION_STATE_POOL_MIN_ADDRESS
,
2037 if (result
!= VK_SUCCESS
)
2038 goto fail_dynamic_state_pool
;
2040 result
= anv_state_pool_init(&device
->surface_state_pool
, device
,
2041 SURFACE_STATE_POOL_MIN_ADDRESS
,
2044 if (result
!= VK_SUCCESS
)
2045 goto fail_instruction_state_pool
;
2047 if (physical_device
->use_softpin
) {
2048 result
= anv_state_pool_init(&device
->binding_table_pool
, device
,
2049 BINDING_TABLE_POOL_MIN_ADDRESS
,
2052 if (result
!= VK_SUCCESS
)
2053 goto fail_surface_state_pool
;
2056 result
= anv_bo_init_new(&device
->workaround_bo
, device
, 1024);
2057 if (result
!= VK_SUCCESS
)
2058 goto fail_binding_table_pool
;
2060 if (physical_device
->use_softpin
)
2061 device
->workaround_bo
.flags
|= EXEC_OBJECT_PINNED
;
2063 if (!anv_vma_alloc(device
, &device
->workaround_bo
))
2064 goto fail_workaround_bo
;
2066 anv_device_init_trivial_batch(device
);
2068 if (device
->info
.gen
>= 10)
2069 anv_device_init_hiz_clear_value_bo(device
);
2071 if (physical_device
->use_softpin
)
2072 device
->pinned_buffers
= _mesa_pointer_set_create(NULL
);
2074 anv_scratch_pool_init(device
, &device
->scratch_pool
);
2076 anv_queue_init(device
, &device
->queue
);
2078 switch (device
->info
.gen
) {
2080 if (!device
->info
.is_haswell
)
2081 result
= gen7_init_device_state(device
);
2083 result
= gen75_init_device_state(device
);
2086 result
= gen8_init_device_state(device
);
2089 result
= gen9_init_device_state(device
);
2092 result
= gen10_init_device_state(device
);
2095 result
= gen11_init_device_state(device
);
2098 /* Shouldn't get here as we don't create physical devices for any other
2100 unreachable("unhandled gen");
2102 if (result
!= VK_SUCCESS
)
2103 goto fail_workaround_bo
;
2105 anv_pipeline_cache_init(&device
->default_pipeline_cache
, device
, true);
2107 anv_device_init_blorp(device
);
2109 anv_device_init_border_colors(device
);
2111 *pDevice
= anv_device_to_handle(device
);
2116 anv_queue_finish(&device
->queue
);
2117 anv_scratch_pool_finish(device
, &device
->scratch_pool
);
2118 anv_gem_munmap(device
->workaround_bo
.map
, device
->workaround_bo
.size
);
2119 anv_gem_close(device
, device
->workaround_bo
.gem_handle
);
2120 fail_binding_table_pool
:
2121 if (physical_device
->use_softpin
)
2122 anv_state_pool_finish(&device
->binding_table_pool
);
2123 fail_surface_state_pool
:
2124 anv_state_pool_finish(&device
->surface_state_pool
);
2125 fail_instruction_state_pool
:
2126 anv_state_pool_finish(&device
->instruction_state_pool
);
2127 fail_dynamic_state_pool
:
2128 anv_state_pool_finish(&device
->dynamic_state_pool
);
2130 anv_bo_cache_finish(&device
->bo_cache
);
2132 anv_bo_pool_finish(&device
->batch_bo_pool
);
2133 pthread_cond_destroy(&device
->queue_submit
);
2135 pthread_mutex_destroy(&device
->mutex
);
2137 anv_gem_destroy_context(device
, device
->context_id
);
2141 vk_free(&device
->alloc
, device
);
2146 void anv_DestroyDevice(
2148 const VkAllocationCallbacks
* pAllocator
)
2150 ANV_FROM_HANDLE(anv_device
, device
, _device
);
2151 struct anv_physical_device
*physical_device
;
2156 physical_device
= &device
->instance
->physicalDevice
;
2158 anv_device_finish_blorp(device
);
2160 anv_pipeline_cache_finish(&device
->default_pipeline_cache
);
2162 anv_queue_finish(&device
->queue
);
2164 if (physical_device
->use_softpin
)
2165 _mesa_set_destroy(device
->pinned_buffers
, NULL
);
2167 #ifdef HAVE_VALGRIND
2168 /* We only need to free these to prevent valgrind errors. The backing
2169 * BO will go away in a couple of lines so we don't actually leak.
2171 anv_state_pool_free(&device
->dynamic_state_pool
, device
->border_colors
);
2174 anv_scratch_pool_finish(device
, &device
->scratch_pool
);
2176 anv_gem_munmap(device
->workaround_bo
.map
, device
->workaround_bo
.size
);
2177 anv_vma_free(device
, &device
->workaround_bo
);
2178 anv_gem_close(device
, device
->workaround_bo
.gem_handle
);
2180 anv_vma_free(device
, &device
->trivial_batch_bo
);
2181 anv_gem_close(device
, device
->trivial_batch_bo
.gem_handle
);
2182 if (device
->info
.gen
>= 10)
2183 anv_gem_close(device
, device
->hiz_clear_bo
.gem_handle
);
2185 if (physical_device
->use_softpin
)
2186 anv_state_pool_finish(&device
->binding_table_pool
);
2187 anv_state_pool_finish(&device
->surface_state_pool
);
2188 anv_state_pool_finish(&device
->instruction_state_pool
);
2189 anv_state_pool_finish(&device
->dynamic_state_pool
);
2191 anv_bo_cache_finish(&device
->bo_cache
);
2193 anv_bo_pool_finish(&device
->batch_bo_pool
);
2195 pthread_cond_destroy(&device
->queue_submit
);
2196 pthread_mutex_destroy(&device
->mutex
);
2198 anv_gem_destroy_context(device
, device
->context_id
);
2200 gen_batch_decode_ctx_finish(&device
->decoder_ctx
);
2204 vk_free(&device
->alloc
, device
);
2207 VkResult
anv_EnumerateInstanceLayerProperties(
2208 uint32_t* pPropertyCount
,
2209 VkLayerProperties
* pProperties
)
2211 if (pProperties
== NULL
) {
2212 *pPropertyCount
= 0;
2216 /* None supported at this time */
2217 return vk_error(VK_ERROR_LAYER_NOT_PRESENT
);
2220 VkResult
anv_EnumerateDeviceLayerProperties(
2221 VkPhysicalDevice physicalDevice
,
2222 uint32_t* pPropertyCount
,
2223 VkLayerProperties
* pProperties
)
2225 if (pProperties
== NULL
) {
2226 *pPropertyCount
= 0;
2230 /* None supported at this time */
2231 return vk_error(VK_ERROR_LAYER_NOT_PRESENT
);
2234 void anv_GetDeviceQueue(
2236 uint32_t queueNodeIndex
,
2237 uint32_t queueIndex
,
2240 ANV_FROM_HANDLE(anv_device
, device
, _device
);
2242 assert(queueIndex
== 0);
2244 *pQueue
= anv_queue_to_handle(&device
->queue
);
2247 void anv_GetDeviceQueue2(
2249 const VkDeviceQueueInfo2
* pQueueInfo
,
2252 ANV_FROM_HANDLE(anv_device
, device
, _device
);
2254 assert(pQueueInfo
->queueIndex
== 0);
2256 if (pQueueInfo
->flags
== device
->queue
.flags
)
2257 *pQueue
= anv_queue_to_handle(&device
->queue
);
2263 _anv_device_set_lost(struct anv_device
*device
,
2264 const char *file
, int line
,
2265 const char *msg
, ...)
2270 device
->_lost
= true;
2273 err
= __vk_errorv(device
->instance
, device
,
2274 VK_DEBUG_REPORT_OBJECT_TYPE_DEVICE_EXT
,
2275 VK_ERROR_DEVICE_LOST
, file
, line
, msg
, ap
);
2278 if (env_var_as_boolean("ANV_ABORT_ON_DEVICE_LOSS", false))
2285 anv_device_query_status(struct anv_device
*device
)
2287 /* This isn't likely as most of the callers of this function already check
2288 * for it. However, it doesn't hurt to check and it potentially lets us
2291 if (anv_device_is_lost(device
))
2292 return VK_ERROR_DEVICE_LOST
;
2294 uint32_t active
, pending
;
2295 int ret
= anv_gem_gpu_get_reset_stats(device
, &active
, &pending
);
2297 /* We don't know the real error. */
2298 return anv_device_set_lost(device
, "get_reset_stats failed: %m");
2302 return anv_device_set_lost(device
, "GPU hung on one of our command buffers");
2303 } else if (pending
) {
2304 return anv_device_set_lost(device
, "GPU hung with commands in-flight");
2311 anv_device_bo_busy(struct anv_device
*device
, struct anv_bo
*bo
)
2313 /* Note: This only returns whether or not the BO is in use by an i915 GPU.
2314 * Other usages of the BO (such as on different hardware) will not be
2315 * flagged as "busy" by this ioctl. Use with care.
2317 int ret
= anv_gem_busy(device
, bo
->gem_handle
);
2319 return VK_NOT_READY
;
2320 } else if (ret
== -1) {
2321 /* We don't know the real error. */
2322 return anv_device_set_lost(device
, "gem wait failed: %m");
2325 /* Query for device status after the busy call. If the BO we're checking
2326 * got caught in a GPU hang we don't want to return VK_SUCCESS to the
2327 * client because it clearly doesn't have valid data. Yes, this most
2328 * likely means an ioctl, but we just did an ioctl to query the busy status
2329 * so it's no great loss.
2331 return anv_device_query_status(device
);
2335 anv_device_wait(struct anv_device
*device
, struct anv_bo
*bo
,
2338 int ret
= anv_gem_wait(device
, bo
->gem_handle
, &timeout
);
2339 if (ret
== -1 && errno
== ETIME
) {
2341 } else if (ret
== -1) {
2342 /* We don't know the real error. */
2343 return anv_device_set_lost(device
, "gem wait failed: %m");
2346 /* Query for device status after the wait. If the BO we're waiting on got
2347 * caught in a GPU hang we don't want to return VK_SUCCESS to the client
2348 * because it clearly doesn't have valid data. Yes, this most likely means
2349 * an ioctl, but we just did an ioctl to wait so it's no great loss.
2351 return anv_device_query_status(device
);
2354 VkResult
anv_DeviceWaitIdle(
2357 ANV_FROM_HANDLE(anv_device
, device
, _device
);
2358 if (anv_device_is_lost(device
))
2359 return VK_ERROR_DEVICE_LOST
;
2361 struct anv_batch batch
;
2364 batch
.start
= batch
.next
= cmds
;
2365 batch
.end
= (void *) cmds
+ sizeof(cmds
);
2367 anv_batch_emit(&batch
, GEN7_MI_BATCH_BUFFER_END
, bbe
);
2368 anv_batch_emit(&batch
, GEN7_MI_NOOP
, noop
);
2370 return anv_device_submit_simple_batch(device
, &batch
);
2374 anv_vma_alloc(struct anv_device
*device
, struct anv_bo
*bo
)
2376 if (!(bo
->flags
& EXEC_OBJECT_PINNED
))
2379 pthread_mutex_lock(&device
->vma_mutex
);
2383 if (bo
->flags
& EXEC_OBJECT_SUPPORTS_48B_ADDRESS
&&
2384 device
->vma_hi_available
>= bo
->size
) {
2385 uint64_t addr
= util_vma_heap_alloc(&device
->vma_hi
, bo
->size
, 4096);
2387 bo
->offset
= gen_canonical_address(addr
);
2388 assert(addr
== gen_48b_address(bo
->offset
));
2389 device
->vma_hi_available
-= bo
->size
;
2393 if (bo
->offset
== 0 && device
->vma_lo_available
>= bo
->size
) {
2394 uint64_t addr
= util_vma_heap_alloc(&device
->vma_lo
, bo
->size
, 4096);
2396 bo
->offset
= gen_canonical_address(addr
);
2397 assert(addr
== gen_48b_address(bo
->offset
));
2398 device
->vma_lo_available
-= bo
->size
;
2402 pthread_mutex_unlock(&device
->vma_mutex
);
2404 return bo
->offset
!= 0;
2408 anv_vma_free(struct anv_device
*device
, struct anv_bo
*bo
)
2410 if (!(bo
->flags
& EXEC_OBJECT_PINNED
))
2413 const uint64_t addr_48b
= gen_48b_address(bo
->offset
);
2415 pthread_mutex_lock(&device
->vma_mutex
);
2417 if (addr_48b
>= LOW_HEAP_MIN_ADDRESS
&&
2418 addr_48b
<= LOW_HEAP_MAX_ADDRESS
) {
2419 util_vma_heap_free(&device
->vma_lo
, addr_48b
, bo
->size
);
2420 device
->vma_lo_available
+= bo
->size
;
2422 assert(addr_48b
>= HIGH_HEAP_MIN_ADDRESS
&&
2423 addr_48b
<= HIGH_HEAP_MAX_ADDRESS
);
2424 util_vma_heap_free(&device
->vma_hi
, addr_48b
, bo
->size
);
2425 device
->vma_hi_available
+= bo
->size
;
2428 pthread_mutex_unlock(&device
->vma_mutex
);
2434 anv_bo_init_new(struct anv_bo
*bo
, struct anv_device
*device
, uint64_t size
)
2436 uint32_t gem_handle
= anv_gem_create(device
, size
);
2438 return vk_error(VK_ERROR_OUT_OF_DEVICE_MEMORY
);
2440 anv_bo_init(bo
, gem_handle
, size
);
2445 VkResult
anv_AllocateMemory(
2447 const VkMemoryAllocateInfo
* pAllocateInfo
,
2448 const VkAllocationCallbacks
* pAllocator
,
2449 VkDeviceMemory
* pMem
)
2451 ANV_FROM_HANDLE(anv_device
, device
, _device
);
2452 struct anv_physical_device
*pdevice
= &device
->instance
->physicalDevice
;
2453 struct anv_device_memory
*mem
;
2454 VkResult result
= VK_SUCCESS
;
2456 assert(pAllocateInfo
->sType
== VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO
);
2458 /* The Vulkan 1.0.33 spec says "allocationSize must be greater than 0". */
2459 assert(pAllocateInfo
->allocationSize
> 0);
2461 if (pAllocateInfo
->allocationSize
> MAX_MEMORY_ALLOCATION_SIZE
)
2462 return VK_ERROR_OUT_OF_DEVICE_MEMORY
;
2464 /* FINISHME: Fail if allocation request exceeds heap size. */
2466 mem
= vk_alloc2(&device
->alloc
, pAllocator
, sizeof(*mem
), 8,
2467 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
2469 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
2471 assert(pAllocateInfo
->memoryTypeIndex
< pdevice
->memory
.type_count
);
2472 mem
->type
= &pdevice
->memory
.types
[pAllocateInfo
->memoryTypeIndex
];
2476 mem
->host_ptr
= NULL
;
2478 uint64_t bo_flags
= 0;
2480 assert(mem
->type
->heapIndex
< pdevice
->memory
.heap_count
);
2481 if (pdevice
->memory
.heaps
[mem
->type
->heapIndex
].supports_48bit_addresses
)
2482 bo_flags
|= EXEC_OBJECT_SUPPORTS_48B_ADDRESS
;
2484 const struct wsi_memory_allocate_info
*wsi_info
=
2485 vk_find_struct_const(pAllocateInfo
->pNext
, WSI_MEMORY_ALLOCATE_INFO_MESA
);
2486 if (wsi_info
&& wsi_info
->implicit_sync
) {
2487 /* We need to set the WRITE flag on window system buffers so that GEM
2488 * will know we're writing to them and synchronize uses on other rings
2489 * (eg if the display server uses the blitter ring).
2491 bo_flags
|= EXEC_OBJECT_WRITE
;
2492 } else if (pdevice
->has_exec_async
) {
2493 bo_flags
|= EXEC_OBJECT_ASYNC
;
2496 if (pdevice
->use_softpin
)
2497 bo_flags
|= EXEC_OBJECT_PINNED
;
2499 const VkExportMemoryAllocateInfo
*export_info
=
2500 vk_find_struct_const(pAllocateInfo
->pNext
, EXPORT_MEMORY_ALLOCATE_INFO
);
2502 /* Check if we need to support Android HW buffer export. If so,
2503 * create AHardwareBuffer and import memory from it.
2505 bool android_export
= false;
2506 if (export_info
&& export_info
->handleTypes
&
2507 VK_EXTERNAL_MEMORY_HANDLE_TYPE_ANDROID_HARDWARE_BUFFER_BIT_ANDROID
)
2508 android_export
= true;
2510 /* Android memory import. */
2511 const struct VkImportAndroidHardwareBufferInfoANDROID
*ahw_import_info
=
2512 vk_find_struct_const(pAllocateInfo
->pNext
,
2513 IMPORT_ANDROID_HARDWARE_BUFFER_INFO_ANDROID
);
2515 if (ahw_import_info
) {
2516 result
= anv_import_ahw_memory(_device
, mem
, ahw_import_info
);
2517 if (result
!= VK_SUCCESS
)
2521 } else if (android_export
) {
2522 result
= anv_create_ahw_memory(_device
, mem
, pAllocateInfo
);
2523 if (result
!= VK_SUCCESS
)
2526 const struct VkImportAndroidHardwareBufferInfoANDROID import_info
= {
2529 result
= anv_import_ahw_memory(_device
, mem
, &import_info
);
2530 if (result
!= VK_SUCCESS
)
2536 const VkImportMemoryFdInfoKHR
*fd_info
=
2537 vk_find_struct_const(pAllocateInfo
->pNext
, IMPORT_MEMORY_FD_INFO_KHR
);
2539 /* The Vulkan spec permits handleType to be 0, in which case the struct is
2542 if (fd_info
&& fd_info
->handleType
) {
2543 /* At the moment, we support only the below handle types. */
2544 assert(fd_info
->handleType
==
2545 VK_EXTERNAL_MEMORY_HANDLE_TYPE_OPAQUE_FD_BIT
||
2546 fd_info
->handleType
==
2547 VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_EXT
);
2549 result
= anv_bo_cache_import(device
, &device
->bo_cache
, fd_info
->fd
,
2550 bo_flags
| ANV_BO_EXTERNAL
, &mem
->bo
);
2551 if (result
!= VK_SUCCESS
)
2554 VkDeviceSize aligned_alloc_size
=
2555 align_u64(pAllocateInfo
->allocationSize
, 4096);
2557 /* For security purposes, we reject importing the bo if it's smaller
2558 * than the requested allocation size. This prevents a malicious client
2559 * from passing a buffer to a trusted client, lying about the size, and
2560 * telling the trusted client to try and texture from an image that goes
2561 * out-of-bounds. This sort of thing could lead to GPU hangs or worse
2562 * in the trusted client. The trusted client can protect itself against
2563 * this sort of attack but only if it can trust the buffer size.
2565 if (mem
->bo
->size
< aligned_alloc_size
) {
2566 result
= vk_errorf(device
->instance
, device
,
2567 VK_ERROR_INVALID_EXTERNAL_HANDLE
,
2568 "aligned allocationSize too large for "
2569 "VK_EXTERNAL_MEMORY_HANDLE_TYPE_OPAQUE_FD_BIT: "
2570 "%"PRIu64
"B > %"PRIu64
"B",
2571 aligned_alloc_size
, mem
->bo
->size
);
2572 anv_bo_cache_release(device
, &device
->bo_cache
, mem
->bo
);
2576 /* From the Vulkan spec:
2578 * "Importing memory from a file descriptor transfers ownership of
2579 * the file descriptor from the application to the Vulkan
2580 * implementation. The application must not perform any operations on
2581 * the file descriptor after a successful import."
2583 * If the import fails, we leave the file descriptor open.
2589 const VkImportMemoryHostPointerInfoEXT
*host_ptr_info
=
2590 vk_find_struct_const(pAllocateInfo
->pNext
,
2591 IMPORT_MEMORY_HOST_POINTER_INFO_EXT
);
2592 if (host_ptr_info
&& host_ptr_info
->handleType
) {
2593 if (host_ptr_info
->handleType
==
2594 VK_EXTERNAL_MEMORY_HANDLE_TYPE_HOST_MAPPED_FOREIGN_MEMORY_BIT_EXT
) {
2595 result
= vk_error(VK_ERROR_INVALID_EXTERNAL_HANDLE
);
2599 assert(host_ptr_info
->handleType
==
2600 VK_EXTERNAL_MEMORY_HANDLE_TYPE_HOST_ALLOCATION_BIT_EXT
);
2602 result
= anv_bo_cache_import_host_ptr(
2603 device
, &device
->bo_cache
, host_ptr_info
->pHostPointer
,
2604 pAllocateInfo
->allocationSize
, bo_flags
, &mem
->bo
);
2606 if (result
!= VK_SUCCESS
)
2609 mem
->host_ptr
= host_ptr_info
->pHostPointer
;
2613 /* Regular allocate (not importing memory). */
2615 if (export_info
&& export_info
->handleTypes
)
2616 bo_flags
|= ANV_BO_EXTERNAL
;
2618 result
= anv_bo_cache_alloc(device
, &device
->bo_cache
,
2619 pAllocateInfo
->allocationSize
, bo_flags
,
2621 if (result
!= VK_SUCCESS
)
2624 const VkMemoryDedicatedAllocateInfo
*dedicated_info
=
2625 vk_find_struct_const(pAllocateInfo
->pNext
, MEMORY_DEDICATED_ALLOCATE_INFO
);
2626 if (dedicated_info
&& dedicated_info
->image
!= VK_NULL_HANDLE
) {
2627 ANV_FROM_HANDLE(anv_image
, image
, dedicated_info
->image
);
2629 /* Some legacy (non-modifiers) consumers need the tiling to be set on
2630 * the BO. In this case, we have a dedicated allocation.
2632 if (image
->needs_set_tiling
) {
2633 const uint32_t i915_tiling
=
2634 isl_tiling_to_i915_tiling(image
->planes
[0].surface
.isl
.tiling
);
2635 int ret
= anv_gem_set_tiling(device
, mem
->bo
->gem_handle
,
2636 image
->planes
[0].surface
.isl
.row_pitch_B
,
2639 anv_bo_cache_release(device
, &device
->bo_cache
, mem
->bo
);
2640 return vk_errorf(device
->instance
, NULL
,
2641 VK_ERROR_OUT_OF_DEVICE_MEMORY
,
2642 "failed to set BO tiling: %m");
2648 *pMem
= anv_device_memory_to_handle(mem
);
2653 vk_free2(&device
->alloc
, pAllocator
, mem
);
2658 VkResult
anv_GetMemoryFdKHR(
2660 const VkMemoryGetFdInfoKHR
* pGetFdInfo
,
2663 ANV_FROM_HANDLE(anv_device
, dev
, device_h
);
2664 ANV_FROM_HANDLE(anv_device_memory
, mem
, pGetFdInfo
->memory
);
2666 assert(pGetFdInfo
->sType
== VK_STRUCTURE_TYPE_MEMORY_GET_FD_INFO_KHR
);
2668 assert(pGetFdInfo
->handleType
== VK_EXTERNAL_MEMORY_HANDLE_TYPE_OPAQUE_FD_BIT
||
2669 pGetFdInfo
->handleType
== VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_EXT
);
2671 return anv_bo_cache_export(dev
, &dev
->bo_cache
, mem
->bo
, pFd
);
2674 VkResult
anv_GetMemoryFdPropertiesKHR(
2676 VkExternalMemoryHandleTypeFlagBits handleType
,
2678 VkMemoryFdPropertiesKHR
* pMemoryFdProperties
)
2680 ANV_FROM_HANDLE(anv_device
, device
, _device
);
2681 struct anv_physical_device
*pdevice
= &device
->instance
->physicalDevice
;
2683 switch (handleType
) {
2684 case VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_EXT
:
2685 /* dma-buf can be imported as any memory type */
2686 pMemoryFdProperties
->memoryTypeBits
=
2687 (1 << pdevice
->memory
.type_count
) - 1;
2691 /* The valid usage section for this function says:
2693 * "handleType must not be one of the handle types defined as
2696 * So opaque handle types fall into the default "unsupported" case.
2698 return vk_error(VK_ERROR_INVALID_EXTERNAL_HANDLE
);
2702 VkResult
anv_GetMemoryHostPointerPropertiesEXT(
2704 VkExternalMemoryHandleTypeFlagBits handleType
,
2705 const void* pHostPointer
,
2706 VkMemoryHostPointerPropertiesEXT
* pMemoryHostPointerProperties
)
2708 ANV_FROM_HANDLE(anv_device
, device
, _device
);
2710 assert(pMemoryHostPointerProperties
->sType
==
2711 VK_STRUCTURE_TYPE_MEMORY_HOST_POINTER_PROPERTIES_EXT
);
2713 switch (handleType
) {
2714 case VK_EXTERNAL_MEMORY_HANDLE_TYPE_HOST_ALLOCATION_BIT_EXT
: {
2715 struct anv_physical_device
*pdevice
= &device
->instance
->physicalDevice
;
2717 /* Host memory can be imported as any memory type. */
2718 pMemoryHostPointerProperties
->memoryTypeBits
=
2719 (1ull << pdevice
->memory
.type_count
) - 1;
2724 return VK_ERROR_INVALID_EXTERNAL_HANDLE
;
2728 void anv_FreeMemory(
2730 VkDeviceMemory _mem
,
2731 const VkAllocationCallbacks
* pAllocator
)
2733 ANV_FROM_HANDLE(anv_device
, device
, _device
);
2734 ANV_FROM_HANDLE(anv_device_memory
, mem
, _mem
);
2740 anv_UnmapMemory(_device
, _mem
);
2742 anv_bo_cache_release(device
, &device
->bo_cache
, mem
->bo
);
2746 AHardwareBuffer_release(mem
->ahw
);
2749 vk_free2(&device
->alloc
, pAllocator
, mem
);
2752 VkResult
anv_MapMemory(
2754 VkDeviceMemory _memory
,
2755 VkDeviceSize offset
,
2757 VkMemoryMapFlags flags
,
2760 ANV_FROM_HANDLE(anv_device
, device
, _device
);
2761 ANV_FROM_HANDLE(anv_device_memory
, mem
, _memory
);
2768 if (mem
->host_ptr
) {
2769 *ppData
= mem
->host_ptr
+ offset
;
2773 if (size
== VK_WHOLE_SIZE
)
2774 size
= mem
->bo
->size
- offset
;
2776 /* From the Vulkan spec version 1.0.32 docs for MapMemory:
2778 * * If size is not equal to VK_WHOLE_SIZE, size must be greater than 0
2779 * assert(size != 0);
2780 * * If size is not equal to VK_WHOLE_SIZE, size must be less than or
2781 * equal to the size of the memory minus offset
2784 assert(offset
+ size
<= mem
->bo
->size
);
2786 /* FIXME: Is this supposed to be thread safe? Since vkUnmapMemory() only
2787 * takes a VkDeviceMemory pointer, it seems like only one map of the memory
2788 * at a time is valid. We could just mmap up front and return an offset
2789 * pointer here, but that may exhaust virtual memory on 32 bit
2792 uint32_t gem_flags
= 0;
2794 if (!device
->info
.has_llc
&&
2795 (mem
->type
->propertyFlags
& VK_MEMORY_PROPERTY_HOST_COHERENT_BIT
))
2796 gem_flags
|= I915_MMAP_WC
;
2798 /* GEM will fail to map if the offset isn't 4k-aligned. Round down. */
2799 uint64_t map_offset
= offset
& ~4095ull;
2800 assert(offset
>= map_offset
);
2801 uint64_t map_size
= (offset
+ size
) - map_offset
;
2803 /* Let's map whole pages */
2804 map_size
= align_u64(map_size
, 4096);
2806 void *map
= anv_gem_mmap(device
, mem
->bo
->gem_handle
,
2807 map_offset
, map_size
, gem_flags
);
2808 if (map
== MAP_FAILED
)
2809 return vk_error(VK_ERROR_MEMORY_MAP_FAILED
);
2812 mem
->map_size
= map_size
;
2814 *ppData
= mem
->map
+ (offset
- map_offset
);
2819 void anv_UnmapMemory(
2821 VkDeviceMemory _memory
)
2823 ANV_FROM_HANDLE(anv_device_memory
, mem
, _memory
);
2825 if (mem
== NULL
|| mem
->host_ptr
)
2828 anv_gem_munmap(mem
->map
, mem
->map_size
);
2835 clflush_mapped_ranges(struct anv_device
*device
,
2837 const VkMappedMemoryRange
*ranges
)
2839 for (uint32_t i
= 0; i
< count
; i
++) {
2840 ANV_FROM_HANDLE(anv_device_memory
, mem
, ranges
[i
].memory
);
2841 if (ranges
[i
].offset
>= mem
->map_size
)
2844 gen_clflush_range(mem
->map
+ ranges
[i
].offset
,
2845 MIN2(ranges
[i
].size
, mem
->map_size
- ranges
[i
].offset
));
2849 VkResult
anv_FlushMappedMemoryRanges(
2851 uint32_t memoryRangeCount
,
2852 const VkMappedMemoryRange
* pMemoryRanges
)
2854 ANV_FROM_HANDLE(anv_device
, device
, _device
);
2856 if (device
->info
.has_llc
)
2859 /* Make sure the writes we're flushing have landed. */
2860 __builtin_ia32_mfence();
2862 clflush_mapped_ranges(device
, memoryRangeCount
, pMemoryRanges
);
2867 VkResult
anv_InvalidateMappedMemoryRanges(
2869 uint32_t memoryRangeCount
,
2870 const VkMappedMemoryRange
* pMemoryRanges
)
2872 ANV_FROM_HANDLE(anv_device
, device
, _device
);
2874 if (device
->info
.has_llc
)
2877 clflush_mapped_ranges(device
, memoryRangeCount
, pMemoryRanges
);
2879 /* Make sure no reads get moved up above the invalidate. */
2880 __builtin_ia32_mfence();
2885 void anv_GetBufferMemoryRequirements(
2888 VkMemoryRequirements
* pMemoryRequirements
)
2890 ANV_FROM_HANDLE(anv_buffer
, buffer
, _buffer
);
2891 ANV_FROM_HANDLE(anv_device
, device
, _device
);
2892 struct anv_physical_device
*pdevice
= &device
->instance
->physicalDevice
;
2894 /* The Vulkan spec (git aaed022) says:
2896 * memoryTypeBits is a bitfield and contains one bit set for every
2897 * supported memory type for the resource. The bit `1<<i` is set if and
2898 * only if the memory type `i` in the VkPhysicalDeviceMemoryProperties
2899 * structure for the physical device is supported.
2901 uint32_t memory_types
= 0;
2902 for (uint32_t i
= 0; i
< pdevice
->memory
.type_count
; i
++) {
2903 uint32_t valid_usage
= pdevice
->memory
.types
[i
].valid_buffer_usage
;
2904 if ((valid_usage
& buffer
->usage
) == buffer
->usage
)
2905 memory_types
|= (1u << i
);
2908 /* Base alignment requirement of a cache line */
2909 uint32_t alignment
= 16;
2911 /* We need an alignment of 32 for pushing UBOs */
2912 if (buffer
->usage
& VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT
)
2913 alignment
= MAX2(alignment
, 32);
2915 pMemoryRequirements
->size
= buffer
->size
;
2916 pMemoryRequirements
->alignment
= alignment
;
2918 /* Storage and Uniform buffers should have their size aligned to
2919 * 32-bits to avoid boundary checks when last DWord is not complete.
2920 * This would ensure that not internal padding would be needed for
2923 if (device
->robust_buffer_access
&&
2924 (buffer
->usage
& VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT
||
2925 buffer
->usage
& VK_BUFFER_USAGE_STORAGE_BUFFER_BIT
))
2926 pMemoryRequirements
->size
= align_u64(buffer
->size
, 4);
2928 pMemoryRequirements
->memoryTypeBits
= memory_types
;
2931 void anv_GetBufferMemoryRequirements2(
2933 const VkBufferMemoryRequirementsInfo2
* pInfo
,
2934 VkMemoryRequirements2
* pMemoryRequirements
)
2936 anv_GetBufferMemoryRequirements(_device
, pInfo
->buffer
,
2937 &pMemoryRequirements
->memoryRequirements
);
2939 vk_foreach_struct(ext
, pMemoryRequirements
->pNext
) {
2940 switch (ext
->sType
) {
2941 case VK_STRUCTURE_TYPE_MEMORY_DEDICATED_REQUIREMENTS
: {
2942 VkMemoryDedicatedRequirements
*requirements
= (void *)ext
;
2943 requirements
->prefersDedicatedAllocation
= VK_FALSE
;
2944 requirements
->requiresDedicatedAllocation
= VK_FALSE
;
2949 anv_debug_ignored_stype(ext
->sType
);
2955 void anv_GetImageMemoryRequirements(
2958 VkMemoryRequirements
* pMemoryRequirements
)
2960 ANV_FROM_HANDLE(anv_image
, image
, _image
);
2961 ANV_FROM_HANDLE(anv_device
, device
, _device
);
2962 struct anv_physical_device
*pdevice
= &device
->instance
->physicalDevice
;
2964 /* The Vulkan spec (git aaed022) says:
2966 * memoryTypeBits is a bitfield and contains one bit set for every
2967 * supported memory type for the resource. The bit `1<<i` is set if and
2968 * only if the memory type `i` in the VkPhysicalDeviceMemoryProperties
2969 * structure for the physical device is supported.
2971 * All types are currently supported for images.
2973 uint32_t memory_types
= (1ull << pdevice
->memory
.type_count
) - 1;
2975 /* We must have image allocated or imported at this point. According to the
2976 * specification, external images must have been bound to memory before
2977 * calling GetImageMemoryRequirements.
2979 assert(image
->size
> 0);
2981 pMemoryRequirements
->size
= image
->size
;
2982 pMemoryRequirements
->alignment
= image
->alignment
;
2983 pMemoryRequirements
->memoryTypeBits
= memory_types
;
2986 void anv_GetImageMemoryRequirements2(
2988 const VkImageMemoryRequirementsInfo2
* pInfo
,
2989 VkMemoryRequirements2
* pMemoryRequirements
)
2991 ANV_FROM_HANDLE(anv_device
, device
, _device
);
2992 ANV_FROM_HANDLE(anv_image
, image
, pInfo
->image
);
2994 anv_GetImageMemoryRequirements(_device
, pInfo
->image
,
2995 &pMemoryRequirements
->memoryRequirements
);
2997 vk_foreach_struct_const(ext
, pInfo
->pNext
) {
2998 switch (ext
->sType
) {
2999 case VK_STRUCTURE_TYPE_IMAGE_PLANE_MEMORY_REQUIREMENTS_INFO
: {
3000 struct anv_physical_device
*pdevice
= &device
->instance
->physicalDevice
;
3001 const VkImagePlaneMemoryRequirementsInfo
*plane_reqs
=
3002 (const VkImagePlaneMemoryRequirementsInfo
*) ext
;
3003 uint32_t plane
= anv_image_aspect_to_plane(image
->aspects
,
3004 plane_reqs
->planeAspect
);
3006 assert(image
->planes
[plane
].offset
== 0);
3008 /* The Vulkan spec (git aaed022) says:
3010 * memoryTypeBits is a bitfield and contains one bit set for every
3011 * supported memory type for the resource. The bit `1<<i` is set
3012 * if and only if the memory type `i` in the
3013 * VkPhysicalDeviceMemoryProperties structure for the physical
3014 * device is supported.
3016 * All types are currently supported for images.
3018 pMemoryRequirements
->memoryRequirements
.memoryTypeBits
=
3019 (1ull << pdevice
->memory
.type_count
) - 1;
3021 /* We must have image allocated or imported at this point. According to the
3022 * specification, external images must have been bound to memory before
3023 * calling GetImageMemoryRequirements.
3025 assert(image
->planes
[plane
].size
> 0);
3027 pMemoryRequirements
->memoryRequirements
.size
= image
->planes
[plane
].size
;
3028 pMemoryRequirements
->memoryRequirements
.alignment
=
3029 image
->planes
[plane
].alignment
;
3034 anv_debug_ignored_stype(ext
->sType
);
3039 vk_foreach_struct(ext
, pMemoryRequirements
->pNext
) {
3040 switch (ext
->sType
) {
3041 case VK_STRUCTURE_TYPE_MEMORY_DEDICATED_REQUIREMENTS
: {
3042 VkMemoryDedicatedRequirements
*requirements
= (void *)ext
;
3043 if (image
->needs_set_tiling
|| image
->external_format
) {
3044 /* If we need to set the tiling for external consumers, we need a
3045 * dedicated allocation.
3047 * See also anv_AllocateMemory.
3049 requirements
->prefersDedicatedAllocation
= VK_TRUE
;
3050 requirements
->requiresDedicatedAllocation
= VK_TRUE
;
3052 requirements
->prefersDedicatedAllocation
= VK_FALSE
;
3053 requirements
->requiresDedicatedAllocation
= VK_FALSE
;
3059 anv_debug_ignored_stype(ext
->sType
);
3065 void anv_GetImageSparseMemoryRequirements(
3068 uint32_t* pSparseMemoryRequirementCount
,
3069 VkSparseImageMemoryRequirements
* pSparseMemoryRequirements
)
3071 *pSparseMemoryRequirementCount
= 0;
3074 void anv_GetImageSparseMemoryRequirements2(
3076 const VkImageSparseMemoryRequirementsInfo2
* pInfo
,
3077 uint32_t* pSparseMemoryRequirementCount
,
3078 VkSparseImageMemoryRequirements2
* pSparseMemoryRequirements
)
3080 *pSparseMemoryRequirementCount
= 0;
3083 void anv_GetDeviceMemoryCommitment(
3085 VkDeviceMemory memory
,
3086 VkDeviceSize
* pCommittedMemoryInBytes
)
3088 *pCommittedMemoryInBytes
= 0;
3092 anv_bind_buffer_memory(const VkBindBufferMemoryInfo
*pBindInfo
)
3094 ANV_FROM_HANDLE(anv_device_memory
, mem
, pBindInfo
->memory
);
3095 ANV_FROM_HANDLE(anv_buffer
, buffer
, pBindInfo
->buffer
);
3097 assert(pBindInfo
->sType
== VK_STRUCTURE_TYPE_BIND_BUFFER_MEMORY_INFO
);
3100 assert((buffer
->usage
& mem
->type
->valid_buffer_usage
) == buffer
->usage
);
3101 buffer
->address
= (struct anv_address
) {
3103 .offset
= pBindInfo
->memoryOffset
,
3106 buffer
->address
= ANV_NULL_ADDRESS
;
3110 VkResult
anv_BindBufferMemory(
3113 VkDeviceMemory memory
,
3114 VkDeviceSize memoryOffset
)
3116 anv_bind_buffer_memory(
3117 &(VkBindBufferMemoryInfo
) {
3118 .sType
= VK_STRUCTURE_TYPE_BIND_BUFFER_MEMORY_INFO
,
3121 .memoryOffset
= memoryOffset
,
3127 VkResult
anv_BindBufferMemory2(
3129 uint32_t bindInfoCount
,
3130 const VkBindBufferMemoryInfo
* pBindInfos
)
3132 for (uint32_t i
= 0; i
< bindInfoCount
; i
++)
3133 anv_bind_buffer_memory(&pBindInfos
[i
]);
3138 VkResult
anv_QueueBindSparse(
3140 uint32_t bindInfoCount
,
3141 const VkBindSparseInfo
* pBindInfo
,
3144 ANV_FROM_HANDLE(anv_queue
, queue
, _queue
);
3145 if (anv_device_is_lost(queue
->device
))
3146 return VK_ERROR_DEVICE_LOST
;
3148 return vk_error(VK_ERROR_FEATURE_NOT_PRESENT
);
3153 VkResult
anv_CreateEvent(
3155 const VkEventCreateInfo
* pCreateInfo
,
3156 const VkAllocationCallbacks
* pAllocator
,
3159 ANV_FROM_HANDLE(anv_device
, device
, _device
);
3160 struct anv_state state
;
3161 struct anv_event
*event
;
3163 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_EVENT_CREATE_INFO
);
3165 state
= anv_state_pool_alloc(&device
->dynamic_state_pool
,
3168 event
->state
= state
;
3169 event
->semaphore
= VK_EVENT_RESET
;
3171 if (!device
->info
.has_llc
) {
3172 /* Make sure the writes we're flushing have landed. */
3173 __builtin_ia32_mfence();
3174 __builtin_ia32_clflush(event
);
3177 *pEvent
= anv_event_to_handle(event
);
3182 void anv_DestroyEvent(
3185 const VkAllocationCallbacks
* pAllocator
)
3187 ANV_FROM_HANDLE(anv_device
, device
, _device
);
3188 ANV_FROM_HANDLE(anv_event
, event
, _event
);
3193 anv_state_pool_free(&device
->dynamic_state_pool
, event
->state
);
3196 VkResult
anv_GetEventStatus(
3200 ANV_FROM_HANDLE(anv_device
, device
, _device
);
3201 ANV_FROM_HANDLE(anv_event
, event
, _event
);
3203 if (anv_device_is_lost(device
))
3204 return VK_ERROR_DEVICE_LOST
;
3206 if (!device
->info
.has_llc
) {
3207 /* Invalidate read cache before reading event written by GPU. */
3208 __builtin_ia32_clflush(event
);
3209 __builtin_ia32_mfence();
3213 return event
->semaphore
;
3216 VkResult
anv_SetEvent(
3220 ANV_FROM_HANDLE(anv_device
, device
, _device
);
3221 ANV_FROM_HANDLE(anv_event
, event
, _event
);
3223 event
->semaphore
= VK_EVENT_SET
;
3225 if (!device
->info
.has_llc
) {
3226 /* Make sure the writes we're flushing have landed. */
3227 __builtin_ia32_mfence();
3228 __builtin_ia32_clflush(event
);
3234 VkResult
anv_ResetEvent(
3238 ANV_FROM_HANDLE(anv_device
, device
, _device
);
3239 ANV_FROM_HANDLE(anv_event
, event
, _event
);
3241 event
->semaphore
= VK_EVENT_RESET
;
3243 if (!device
->info
.has_llc
) {
3244 /* Make sure the writes we're flushing have landed. */
3245 __builtin_ia32_mfence();
3246 __builtin_ia32_clflush(event
);
3254 VkResult
anv_CreateBuffer(
3256 const VkBufferCreateInfo
* pCreateInfo
,
3257 const VkAllocationCallbacks
* pAllocator
,
3260 ANV_FROM_HANDLE(anv_device
, device
, _device
);
3261 struct anv_buffer
*buffer
;
3263 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO
);
3265 buffer
= vk_alloc2(&device
->alloc
, pAllocator
, sizeof(*buffer
), 8,
3266 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
3268 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
3270 buffer
->size
= pCreateInfo
->size
;
3271 buffer
->usage
= pCreateInfo
->usage
;
3272 buffer
->address
= ANV_NULL_ADDRESS
;
3274 if (buffer
->usage
& VK_BUFFER_USAGE_SHADER_DEVICE_ADDRESS_BIT_EXT
) {
3275 pthread_mutex_lock(&device
->mutex
);
3276 _mesa_set_add(device
->pinned_buffers
, buffer
);
3277 pthread_mutex_unlock(&device
->mutex
);
3280 *pBuffer
= anv_buffer_to_handle(buffer
);
3285 void anv_DestroyBuffer(
3288 const VkAllocationCallbacks
* pAllocator
)
3290 ANV_FROM_HANDLE(anv_device
, device
, _device
);
3291 ANV_FROM_HANDLE(anv_buffer
, buffer
, _buffer
);
3296 if (buffer
->usage
& VK_BUFFER_USAGE_SHADER_DEVICE_ADDRESS_BIT_EXT
) {
3297 pthread_mutex_lock(&device
->mutex
);
3298 _mesa_set_remove_key(device
->pinned_buffers
, buffer
);
3299 pthread_mutex_unlock(&device
->mutex
);
3302 vk_free2(&device
->alloc
, pAllocator
, buffer
);
3305 VkDeviceAddress
anv_GetBufferDeviceAddressEXT(
3307 const VkBufferDeviceAddressInfoEXT
* pInfo
)
3309 ANV_FROM_HANDLE(anv_buffer
, buffer
, pInfo
->buffer
);
3311 assert(buffer
->address
.bo
->flags
& EXEC_OBJECT_PINNED
);
3313 return anv_address_physical(buffer
->address
);
3317 anv_fill_buffer_surface_state(struct anv_device
*device
, struct anv_state state
,
3318 enum isl_format format
,
3319 struct anv_address address
,
3320 uint32_t range
, uint32_t stride
)
3322 isl_buffer_fill_state(&device
->isl_dev
, state
.map
,
3323 .address
= anv_address_physical(address
),
3324 .mocs
= device
->default_mocs
,
3327 .stride_B
= stride
);
3330 void anv_DestroySampler(
3333 const VkAllocationCallbacks
* pAllocator
)
3335 ANV_FROM_HANDLE(anv_device
, device
, _device
);
3336 ANV_FROM_HANDLE(anv_sampler
, sampler
, _sampler
);
3341 vk_free2(&device
->alloc
, pAllocator
, sampler
);
3344 VkResult
anv_CreateFramebuffer(
3346 const VkFramebufferCreateInfo
* pCreateInfo
,
3347 const VkAllocationCallbacks
* pAllocator
,
3348 VkFramebuffer
* pFramebuffer
)
3350 ANV_FROM_HANDLE(anv_device
, device
, _device
);
3351 struct anv_framebuffer
*framebuffer
;
3353 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO
);
3355 size_t size
= sizeof(*framebuffer
) +
3356 sizeof(struct anv_image_view
*) * pCreateInfo
->attachmentCount
;
3357 framebuffer
= vk_alloc2(&device
->alloc
, pAllocator
, size
, 8,
3358 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
3359 if (framebuffer
== NULL
)
3360 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
3362 framebuffer
->attachment_count
= pCreateInfo
->attachmentCount
;
3363 for (uint32_t i
= 0; i
< pCreateInfo
->attachmentCount
; i
++) {
3364 VkImageView _iview
= pCreateInfo
->pAttachments
[i
];
3365 framebuffer
->attachments
[i
] = anv_image_view_from_handle(_iview
);
3368 framebuffer
->width
= pCreateInfo
->width
;
3369 framebuffer
->height
= pCreateInfo
->height
;
3370 framebuffer
->layers
= pCreateInfo
->layers
;
3372 *pFramebuffer
= anv_framebuffer_to_handle(framebuffer
);
3377 void anv_DestroyFramebuffer(
3380 const VkAllocationCallbacks
* pAllocator
)
3382 ANV_FROM_HANDLE(anv_device
, device
, _device
);
3383 ANV_FROM_HANDLE(anv_framebuffer
, fb
, _fb
);
3388 vk_free2(&device
->alloc
, pAllocator
, fb
);
3391 static const VkTimeDomainEXT anv_time_domains
[] = {
3392 VK_TIME_DOMAIN_DEVICE_EXT
,
3393 VK_TIME_DOMAIN_CLOCK_MONOTONIC_EXT
,
3394 VK_TIME_DOMAIN_CLOCK_MONOTONIC_RAW_EXT
,
3397 VkResult
anv_GetPhysicalDeviceCalibrateableTimeDomainsEXT(
3398 VkPhysicalDevice physicalDevice
,
3399 uint32_t *pTimeDomainCount
,
3400 VkTimeDomainEXT
*pTimeDomains
)
3403 VK_OUTARRAY_MAKE(out
, pTimeDomains
, pTimeDomainCount
);
3405 for (d
= 0; d
< ARRAY_SIZE(anv_time_domains
); d
++) {
3406 vk_outarray_append(&out
, i
) {
3407 *i
= anv_time_domains
[d
];
3411 return vk_outarray_status(&out
);
3415 anv_clock_gettime(clockid_t clock_id
)
3417 struct timespec current
;
3420 ret
= clock_gettime(clock_id
, ¤t
);
3421 if (ret
< 0 && clock_id
== CLOCK_MONOTONIC_RAW
)
3422 ret
= clock_gettime(CLOCK_MONOTONIC
, ¤t
);
3426 return (uint64_t) current
.tv_sec
* 1000000000ULL + current
.tv_nsec
;
3429 #define TIMESTAMP 0x2358
3431 VkResult
anv_GetCalibratedTimestampsEXT(
3433 uint32_t timestampCount
,
3434 const VkCalibratedTimestampInfoEXT
*pTimestampInfos
,
3435 uint64_t *pTimestamps
,
3436 uint64_t *pMaxDeviation
)
3438 ANV_FROM_HANDLE(anv_device
, device
, _device
);
3439 uint64_t timestamp_frequency
= device
->info
.timestamp_frequency
;
3442 uint64_t begin
, end
;
3443 uint64_t max_clock_period
= 0;
3445 begin
= anv_clock_gettime(CLOCK_MONOTONIC_RAW
);
3447 for (d
= 0; d
< timestampCount
; d
++) {
3448 switch (pTimestampInfos
[d
].timeDomain
) {
3449 case VK_TIME_DOMAIN_DEVICE_EXT
:
3450 ret
= anv_gem_reg_read(device
, TIMESTAMP
| 1,
3454 return anv_device_set_lost(device
, "Failed to read the TIMESTAMP "
3457 uint64_t device_period
= DIV_ROUND_UP(1000000000, timestamp_frequency
);
3458 max_clock_period
= MAX2(max_clock_period
, device_period
);
3460 case VK_TIME_DOMAIN_CLOCK_MONOTONIC_EXT
:
3461 pTimestamps
[d
] = anv_clock_gettime(CLOCK_MONOTONIC
);
3462 max_clock_period
= MAX2(max_clock_period
, 1);
3465 case VK_TIME_DOMAIN_CLOCK_MONOTONIC_RAW_EXT
:
3466 pTimestamps
[d
] = begin
;
3474 end
= anv_clock_gettime(CLOCK_MONOTONIC_RAW
);
3477 * The maximum deviation is the sum of the interval over which we
3478 * perform the sampling and the maximum period of any sampled
3479 * clock. That's because the maximum skew between any two sampled
3480 * clock edges is when the sampled clock with the largest period is
3481 * sampled at the end of that period but right at the beginning of the
3482 * sampling interval and some other clock is sampled right at the
3483 * begining of its sampling period and right at the end of the
3484 * sampling interval. Let's assume the GPU has the longest clock
3485 * period and that the application is sampling GPU and monotonic:
3488 * w x y z 0 1 2 3 4 5 6 7 8 9 a b c d e f
3489 * Raw -_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-
3493 * GPU -----_____-----_____-----_____-----_____
3496 * x y z 0 1 2 3 4 5 6 7 8 9 a b c
3497 * Monotonic -_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-
3499 * Interval <----------------->
3500 * Deviation <-------------------------->
3504 * m = read(monotonic) 2
3507 * We round the sample interval up by one tick to cover sampling error
3508 * in the interval clock
3511 uint64_t sample_interval
= end
- begin
+ 1;
3513 *pMaxDeviation
= sample_interval
+ max_clock_period
;
3518 /* vk_icd.h does not declare this function, so we declare it here to
3519 * suppress Wmissing-prototypes.
3521 PUBLIC VKAPI_ATTR VkResult VKAPI_CALL
3522 vk_icdNegotiateLoaderICDInterfaceVersion(uint32_t* pSupportedVersion
);
3524 PUBLIC VKAPI_ATTR VkResult VKAPI_CALL
3525 vk_icdNegotiateLoaderICDInterfaceVersion(uint32_t* pSupportedVersion
)
3527 /* For the full details on loader interface versioning, see
3528 * <https://github.com/KhronosGroup/Vulkan-LoaderAndValidationLayers/blob/master/loader/LoaderAndLayerInterface.md>.
3529 * What follows is a condensed summary, to help you navigate the large and
3530 * confusing official doc.
3532 * - Loader interface v0 is incompatible with later versions. We don't
3535 * - In loader interface v1:
3536 * - The first ICD entrypoint called by the loader is
3537 * vk_icdGetInstanceProcAddr(). The ICD must statically expose this
3539 * - The ICD must statically expose no other Vulkan symbol unless it is
3540 * linked with -Bsymbolic.
3541 * - Each dispatchable Vulkan handle created by the ICD must be
3542 * a pointer to a struct whose first member is VK_LOADER_DATA. The
3543 * ICD must initialize VK_LOADER_DATA.loadMagic to ICD_LOADER_MAGIC.
3544 * - The loader implements vkCreate{PLATFORM}SurfaceKHR() and
3545 * vkDestroySurfaceKHR(). The ICD must be capable of working with
3546 * such loader-managed surfaces.
3548 * - Loader interface v2 differs from v1 in:
3549 * - The first ICD entrypoint called by the loader is
3550 * vk_icdNegotiateLoaderICDInterfaceVersion(). The ICD must
3551 * statically expose this entrypoint.
3553 * - Loader interface v3 differs from v2 in:
3554 * - The ICD must implement vkCreate{PLATFORM}SurfaceKHR(),
3555 * vkDestroySurfaceKHR(), and other API which uses VKSurfaceKHR,
3556 * because the loader no longer does so.
3558 *pSupportedVersion
= MIN2(*pSupportedVersion
, 3u);