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_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"
41 #include "common/gen_defines.h"
43 #include "genxml/gen7_pack.h"
46 compiler_debug_log(void *data
, const char *fmt
, ...)
50 compiler_perf_log(void *data
, const char *fmt
, ...)
55 if (unlikely(INTEL_DEBUG
& DEBUG_PERF
))
56 intel_logd_v(fmt
, args
);
62 anv_compute_heap_size(int fd
, uint64_t gtt_size
, uint64_t *heap_size
)
64 /* Query the total ram from the system */
68 uint64_t total_ram
= (uint64_t)info
.totalram
* (uint64_t)info
.mem_unit
;
70 /* We don't want to burn too much ram with the GPU. If the user has 4GiB
71 * or less, we use at most half. If they have more than 4GiB, we use 3/4.
73 uint64_t available_ram
;
74 if (total_ram
<= 4ull * 1024ull * 1024ull * 1024ull)
75 available_ram
= total_ram
/ 2;
77 available_ram
= total_ram
* 3 / 4;
79 /* We also want to leave some padding for things we allocate in the driver,
80 * so don't go over 3/4 of the GTT either.
82 uint64_t available_gtt
= gtt_size
* 3 / 4;
84 *heap_size
= 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
= 0;
111 VkResult result
= anv_compute_heap_size(fd
, gtt_size
, &heap_size
);
112 if (result
!= VK_SUCCESS
)
115 if (heap_size
> (2ull << 30) && !device
->supports_48bit_addresses
) {
116 /* When running with an overridden PCI ID, we may get a GTT size from
117 * the kernel that is greater than 2 GiB but the execbuf check for 48bit
118 * address support can still fail. Just clamp the address space size to
119 * 2 GiB if we don't have 48-bit support.
121 intel_logw("%s:%d: The kernel reported a GTT size larger than 2 GiB but "
122 "not support for 48-bit addresses",
124 heap_size
= 2ull << 30;
127 if (heap_size
<= 3ull * (1ull << 30)) {
128 /* In this case, everything fits nicely into the 32-bit address space,
129 * so there's no need for supporting 48bit addresses on client-allocated
132 device
->memory
.heap_count
= 1;
133 device
->memory
.heaps
[0] = (struct anv_memory_heap
) {
135 .flags
= VK_MEMORY_HEAP_DEVICE_LOCAL_BIT
,
136 .supports_48bit_addresses
= false,
139 /* Not everything will fit nicely into a 32-bit address space. In this
140 * case we need a 64-bit heap. Advertise a small 32-bit heap and a
141 * larger 48-bit heap. If we're in this case, then we have a total heap
142 * size larger than 3GiB which most likely means they have 8 GiB of
143 * video memory and so carving off 1 GiB for the 32-bit heap should be
146 const uint64_t heap_size_32bit
= 1ull << 30;
147 const uint64_t heap_size_48bit
= heap_size
- heap_size_32bit
;
149 assert(device
->supports_48bit_addresses
);
151 device
->memory
.heap_count
= 2;
152 device
->memory
.heaps
[0] = (struct anv_memory_heap
) {
153 .size
= heap_size_48bit
,
154 .flags
= VK_MEMORY_HEAP_DEVICE_LOCAL_BIT
,
155 .supports_48bit_addresses
= true,
157 device
->memory
.heaps
[1] = (struct anv_memory_heap
) {
158 .size
= heap_size_32bit
,
159 .flags
= VK_MEMORY_HEAP_DEVICE_LOCAL_BIT
,
160 .supports_48bit_addresses
= false,
164 uint32_t type_count
= 0;
165 for (uint32_t heap
= 0; heap
< device
->memory
.heap_count
; heap
++) {
166 uint32_t valid_buffer_usage
= ~0;
168 /* There appears to be a hardware issue in the VF cache where it only
169 * considers the bottom 32 bits of memory addresses. If you happen to
170 * have two vertex buffers which get placed exactly 4 GiB apart and use
171 * them in back-to-back draw calls, you can get collisions. In order to
172 * solve this problem, we require vertex and index buffers be bound to
173 * memory allocated out of the 32-bit heap.
175 if (device
->memory
.heaps
[heap
].supports_48bit_addresses
) {
176 valid_buffer_usage
&= ~(VK_BUFFER_USAGE_INDEX_BUFFER_BIT
|
177 VK_BUFFER_USAGE_VERTEX_BUFFER_BIT
);
180 if (device
->info
.has_llc
) {
181 /* Big core GPUs share LLC with the CPU and thus one memory type can be
182 * both cached and coherent at the same time.
184 device
->memory
.types
[type_count
++] = (struct anv_memory_type
) {
185 .propertyFlags
= VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT
|
186 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT
|
187 VK_MEMORY_PROPERTY_HOST_COHERENT_BIT
|
188 VK_MEMORY_PROPERTY_HOST_CACHED_BIT
,
190 .valid_buffer_usage
= valid_buffer_usage
,
193 /* The spec requires that we expose a host-visible, coherent memory
194 * type, but Atom GPUs don't share LLC. Thus we offer two memory types
195 * to give the application a choice between cached, but not coherent and
196 * coherent but uncached (WC though).
198 device
->memory
.types
[type_count
++] = (struct anv_memory_type
) {
199 .propertyFlags
= VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT
|
200 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT
|
201 VK_MEMORY_PROPERTY_HOST_COHERENT_BIT
,
203 .valid_buffer_usage
= valid_buffer_usage
,
205 device
->memory
.types
[type_count
++] = (struct anv_memory_type
) {
206 .propertyFlags
= VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT
|
207 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT
|
208 VK_MEMORY_PROPERTY_HOST_CACHED_BIT
,
210 .valid_buffer_usage
= valid_buffer_usage
,
214 device
->memory
.type_count
= type_count
;
220 anv_physical_device_init_uuids(struct anv_physical_device
*device
)
222 const struct build_id_note
*note
=
223 build_id_find_nhdr_for_addr(anv_physical_device_init_uuids
);
225 return vk_errorf(device
->instance
, device
,
226 VK_ERROR_INITIALIZATION_FAILED
,
227 "Failed to find build-id");
230 unsigned build_id_len
= build_id_length(note
);
231 if (build_id_len
< 20) {
232 return vk_errorf(device
->instance
, device
,
233 VK_ERROR_INITIALIZATION_FAILED
,
234 "build-id too short. It needs to be a SHA");
237 memcpy(device
->driver_build_sha1
, build_id_data(note
), 20);
239 struct mesa_sha1 sha1_ctx
;
241 STATIC_ASSERT(VK_UUID_SIZE
<= sizeof(sha1
));
243 /* The pipeline cache UUID is used for determining when a pipeline cache is
244 * invalid. It needs both a driver build and the PCI ID of the device.
246 _mesa_sha1_init(&sha1_ctx
);
247 _mesa_sha1_update(&sha1_ctx
, build_id_data(note
), build_id_len
);
248 _mesa_sha1_update(&sha1_ctx
, &device
->chipset_id
,
249 sizeof(device
->chipset_id
));
250 _mesa_sha1_final(&sha1_ctx
, sha1
);
251 memcpy(device
->pipeline_cache_uuid
, sha1
, VK_UUID_SIZE
);
253 /* The driver UUID is used for determining sharability of images and memory
254 * between two Vulkan instances in separate processes. People who want to
255 * share memory need to also check the device UUID (below) so all this
256 * needs to be is the build-id.
258 memcpy(device
->driver_uuid
, build_id_data(note
), VK_UUID_SIZE
);
260 /* The device UUID uniquely identifies the given device within the machine.
261 * Since we never have more than one device, this doesn't need to be a real
262 * UUID. However, on the off-chance that someone tries to use this to
263 * cache pre-tiled images or something of the like, we use the PCI ID and
264 * some bits of ISL info to ensure that this is safe.
266 _mesa_sha1_init(&sha1_ctx
);
267 _mesa_sha1_update(&sha1_ctx
, &device
->chipset_id
,
268 sizeof(device
->chipset_id
));
269 _mesa_sha1_update(&sha1_ctx
, &device
->isl_dev
.has_bit6_swizzling
,
270 sizeof(device
->isl_dev
.has_bit6_swizzling
));
271 _mesa_sha1_final(&sha1_ctx
, sha1
);
272 memcpy(device
->device_uuid
, sha1
, VK_UUID_SIZE
);
278 anv_physical_device_init_disk_cache(struct anv_physical_device
*device
)
280 #ifdef ENABLE_SHADER_CACHE
282 MAYBE_UNUSED
int len
= snprintf(renderer
, sizeof(renderer
), "anv_%04x",
284 assert(len
== sizeof(renderer
) - 1);
287 _mesa_sha1_format(timestamp
, device
->driver_build_sha1
);
289 device
->disk_cache
= disk_cache_create(renderer
, timestamp
, 0);
291 device
->disk_cache
= NULL
;
296 anv_physical_device_free_disk_cache(struct anv_physical_device
*device
)
298 #ifdef ENABLE_SHADER_CACHE
299 if (device
->disk_cache
)
300 disk_cache_destroy(device
->disk_cache
);
302 assert(device
->disk_cache
== NULL
);
307 anv_physical_device_init(struct anv_physical_device
*device
,
308 struct anv_instance
*instance
,
309 const char *primary_path
,
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 strncpy(device
->path
, path
, ARRAY_SIZE(device
->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
->name
= gen_get_device_name(device
->chipset_id
);
343 if (!gen_get_device_info(device
->chipset_id
, &device
->info
)) {
344 result
= vk_error(VK_ERROR_INCOMPATIBLE_DRIVER
);
348 if (device
->info
.is_haswell
) {
349 intel_logw("Haswell Vulkan support is incomplete");
350 } else if (device
->info
.gen
== 7 && !device
->info
.is_baytrail
) {
351 intel_logw("Ivy Bridge Vulkan support is incomplete");
352 } else if (device
->info
.gen
== 7 && device
->info
.is_baytrail
) {
353 intel_logw("Bay Trail Vulkan support is incomplete");
354 } else if (device
->info
.gen
>= 8 && device
->info
.gen
<= 10) {
355 /* Gen8-10 fully supported */
356 } else if (device
->info
.gen
== 11) {
357 intel_logw("Vulkan is not yet fully supported on gen11.");
359 result
= vk_errorf(device
->instance
, device
,
360 VK_ERROR_INCOMPATIBLE_DRIVER
,
361 "Vulkan not yet supported on %s", device
->name
);
365 device
->cmd_parser_version
= -1;
366 if (device
->info
.gen
== 7) {
367 device
->cmd_parser_version
=
368 anv_gem_get_param(fd
, I915_PARAM_CMD_PARSER_VERSION
);
369 if (device
->cmd_parser_version
== -1) {
370 result
= vk_errorf(device
->instance
, device
,
371 VK_ERROR_INITIALIZATION_FAILED
,
372 "failed to get command parser version");
377 if (!anv_gem_get_param(fd
, I915_PARAM_HAS_WAIT_TIMEOUT
)) {
378 result
= vk_errorf(device
->instance
, device
,
379 VK_ERROR_INITIALIZATION_FAILED
,
380 "kernel missing gem wait");
384 if (!anv_gem_get_param(fd
, I915_PARAM_HAS_EXECBUF2
)) {
385 result
= vk_errorf(device
->instance
, device
,
386 VK_ERROR_INITIALIZATION_FAILED
,
387 "kernel missing execbuf2");
391 if (!device
->info
.has_llc
&&
392 anv_gem_get_param(fd
, I915_PARAM_MMAP_VERSION
) < 1) {
393 result
= vk_errorf(device
->instance
, device
,
394 VK_ERROR_INITIALIZATION_FAILED
,
395 "kernel missing wc mmap");
399 result
= anv_physical_device_init_heaps(device
, fd
);
400 if (result
!= VK_SUCCESS
)
403 device
->has_exec_async
= anv_gem_get_param(fd
, I915_PARAM_HAS_EXEC_ASYNC
);
404 device
->has_exec_capture
= anv_gem_get_param(fd
, I915_PARAM_HAS_EXEC_CAPTURE
);
405 device
->has_exec_fence
= anv_gem_get_param(fd
, I915_PARAM_HAS_EXEC_FENCE
);
406 device
->has_syncobj
= anv_gem_get_param(fd
, I915_PARAM_HAS_EXEC_FENCE_ARRAY
);
407 device
->has_syncobj_wait
= device
->has_syncobj
&&
408 anv_gem_supports_syncobj_wait(fd
);
409 device
->has_context_priority
= anv_gem_has_context_priority(fd
);
411 device
->use_softpin
= anv_gem_get_param(fd
, I915_PARAM_HAS_EXEC_SOFTPIN
)
412 && device
->supports_48bit_addresses
;
414 device
->has_context_isolation
=
415 anv_gem_get_param(fd
, I915_PARAM_HAS_CONTEXT_ISOLATION
);
417 bool swizzled
= anv_gem_get_bit6_swizzle(fd
, I915_TILING_X
);
419 /* Starting with Gen10, the timestamp frequency of the command streamer may
420 * vary from one part to another. We can query the value from the kernel.
422 if (device
->info
.gen
>= 10) {
423 int timestamp_frequency
=
424 anv_gem_get_param(fd
, I915_PARAM_CS_TIMESTAMP_FREQUENCY
);
426 if (timestamp_frequency
< 0)
427 intel_logw("Kernel 4.16-rc1+ required to properly query CS timestamp frequency");
429 device
->info
.timestamp_frequency
= timestamp_frequency
;
432 /* GENs prior to 8 do not support EU/Subslice info */
433 if (device
->info
.gen
>= 8) {
434 device
->subslice_total
= anv_gem_get_param(fd
, I915_PARAM_SUBSLICE_TOTAL
);
435 device
->eu_total
= anv_gem_get_param(fd
, I915_PARAM_EU_TOTAL
);
437 /* Without this information, we cannot get the right Braswell
438 * brandstrings, and we have to use conservative numbers for GPGPU on
439 * many platforms, but otherwise, things will just work.
441 if (device
->subslice_total
< 1 || device
->eu_total
< 1) {
442 intel_logw("Kernel 4.1 required to properly query GPU properties");
444 } else if (device
->info
.gen
== 7) {
445 device
->subslice_total
= 1 << (device
->info
.gt
- 1);
448 if (device
->info
.is_cherryview
&&
449 device
->subslice_total
> 0 && device
->eu_total
> 0) {
450 /* Logical CS threads = EUs per subslice * num threads per EU */
451 uint32_t max_cs_threads
=
452 device
->eu_total
/ device
->subslice_total
* device
->info
.num_thread_per_eu
;
454 /* Fuse configurations may give more threads than expected, never less. */
455 if (max_cs_threads
> device
->info
.max_cs_threads
)
456 device
->info
.max_cs_threads
= max_cs_threads
;
459 device
->compiler
= brw_compiler_create(NULL
, &device
->info
);
460 if (device
->compiler
== NULL
) {
461 result
= vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
464 device
->compiler
->shader_debug_log
= compiler_debug_log
;
465 device
->compiler
->shader_perf_log
= compiler_perf_log
;
466 device
->compiler
->supports_pull_constants
= false;
467 device
->compiler
->constant_buffer_0_is_relative
=
468 device
->info
.gen
< 8 || !device
->has_context_isolation
;
469 device
->compiler
->supports_shader_constants
= true;
471 isl_device_init(&device
->isl_dev
, &device
->info
, swizzled
);
473 result
= anv_physical_device_init_uuids(device
);
474 if (result
!= VK_SUCCESS
)
477 anv_physical_device_init_disk_cache(device
);
479 if (instance
->enabled_extensions
.KHR_display
) {
480 master_fd
= open(primary_path
, O_RDWR
| O_CLOEXEC
);
481 if (master_fd
>= 0) {
482 /* prod the device with a GETPARAM call which will fail if
483 * we don't have permission to even render on this device
485 if (anv_gem_get_param(master_fd
, I915_PARAM_CHIPSET_ID
) == 0) {
491 device
->master_fd
= master_fd
;
493 result
= anv_init_wsi(device
);
494 if (result
!= VK_SUCCESS
) {
495 ralloc_free(device
->compiler
);
496 anv_physical_device_free_disk_cache(device
);
500 anv_physical_device_get_supported_extensions(device
,
501 &device
->supported_extensions
);
504 device
->local_fd
= fd
;
516 anv_physical_device_finish(struct anv_physical_device
*device
)
518 anv_finish_wsi(device
);
519 anv_physical_device_free_disk_cache(device
);
520 ralloc_free(device
->compiler
);
521 close(device
->local_fd
);
522 if (device
->master_fd
>= 0)
523 close(device
->master_fd
);
527 default_alloc_func(void *pUserData
, size_t size
, size_t align
,
528 VkSystemAllocationScope allocationScope
)
534 default_realloc_func(void *pUserData
, void *pOriginal
, size_t size
,
535 size_t align
, VkSystemAllocationScope allocationScope
)
537 return realloc(pOriginal
, size
);
541 default_free_func(void *pUserData
, void *pMemory
)
546 static const VkAllocationCallbacks default_alloc
= {
548 .pfnAllocation
= default_alloc_func
,
549 .pfnReallocation
= default_realloc_func
,
550 .pfnFree
= default_free_func
,
553 VkResult
anv_EnumerateInstanceExtensionProperties(
554 const char* pLayerName
,
555 uint32_t* pPropertyCount
,
556 VkExtensionProperties
* pProperties
)
558 VK_OUTARRAY_MAKE(out
, pProperties
, pPropertyCount
);
560 for (int i
= 0; i
< ANV_INSTANCE_EXTENSION_COUNT
; i
++) {
561 if (anv_instance_extensions_supported
.extensions
[i
]) {
562 vk_outarray_append(&out
, prop
) {
563 *prop
= anv_instance_extensions
[i
];
568 return vk_outarray_status(&out
);
571 VkResult
anv_CreateInstance(
572 const VkInstanceCreateInfo
* pCreateInfo
,
573 const VkAllocationCallbacks
* pAllocator
,
574 VkInstance
* pInstance
)
576 struct anv_instance
*instance
;
579 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO
);
581 struct anv_instance_extension_table enabled_extensions
= {};
582 for (uint32_t i
= 0; i
< pCreateInfo
->enabledExtensionCount
; i
++) {
584 for (idx
= 0; idx
< ANV_INSTANCE_EXTENSION_COUNT
; idx
++) {
585 if (strcmp(pCreateInfo
->ppEnabledExtensionNames
[i
],
586 anv_instance_extensions
[idx
].extensionName
) == 0)
590 if (idx
>= ANV_INSTANCE_EXTENSION_COUNT
)
591 return vk_error(VK_ERROR_EXTENSION_NOT_PRESENT
);
593 if (!anv_instance_extensions_supported
.extensions
[idx
])
594 return vk_error(VK_ERROR_EXTENSION_NOT_PRESENT
);
596 enabled_extensions
.extensions
[idx
] = true;
599 instance
= vk_alloc2(&default_alloc
, pAllocator
, sizeof(*instance
), 8,
600 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE
);
602 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
604 instance
->_loader_data
.loaderMagic
= ICD_LOADER_MAGIC
;
607 instance
->alloc
= *pAllocator
;
609 instance
->alloc
= default_alloc
;
611 if (pCreateInfo
->pApplicationInfo
&&
612 pCreateInfo
->pApplicationInfo
->apiVersion
!= 0) {
613 instance
->apiVersion
= pCreateInfo
->pApplicationInfo
->apiVersion
;
615 anv_EnumerateInstanceVersion(&instance
->apiVersion
);
618 instance
->enabled_extensions
= enabled_extensions
;
620 for (unsigned i
= 0; i
< ARRAY_SIZE(instance
->dispatch
.entrypoints
); i
++) {
621 /* Vulkan requires that entrypoints for extensions which have not been
622 * enabled must not be advertised.
624 if (!anv_entrypoint_is_enabled(i
, instance
->apiVersion
,
625 &instance
->enabled_extensions
, NULL
)) {
626 instance
->dispatch
.entrypoints
[i
] = NULL
;
627 } else if (anv_dispatch_table
.entrypoints
[i
] != NULL
) {
628 instance
->dispatch
.entrypoints
[i
] = anv_dispatch_table
.entrypoints
[i
];
630 instance
->dispatch
.entrypoints
[i
] =
631 anv_tramp_dispatch_table
.entrypoints
[i
];
635 instance
->physicalDeviceCount
= -1;
637 result
= vk_debug_report_instance_init(&instance
->debug_report_callbacks
);
638 if (result
!= VK_SUCCESS
) {
639 vk_free2(&default_alloc
, pAllocator
, instance
);
640 return vk_error(result
);
643 instance
->pipeline_cache_enabled
=
644 env_var_as_boolean("ANV_ENABLE_PIPELINE_CACHE", true);
648 VG(VALGRIND_CREATE_MEMPOOL(instance
, 0, false));
650 *pInstance
= anv_instance_to_handle(instance
);
655 void anv_DestroyInstance(
656 VkInstance _instance
,
657 const VkAllocationCallbacks
* pAllocator
)
659 ANV_FROM_HANDLE(anv_instance
, instance
, _instance
);
664 if (instance
->physicalDeviceCount
> 0) {
665 /* We support at most one physical device. */
666 assert(instance
->physicalDeviceCount
== 1);
667 anv_physical_device_finish(&instance
->physicalDevice
);
670 VG(VALGRIND_DESTROY_MEMPOOL(instance
));
672 vk_debug_report_instance_destroy(&instance
->debug_report_callbacks
);
676 vk_free(&instance
->alloc
, instance
);
680 anv_enumerate_devices(struct anv_instance
*instance
)
682 /* TODO: Check for more devices ? */
683 drmDevicePtr devices
[8];
684 VkResult result
= VK_ERROR_INCOMPATIBLE_DRIVER
;
687 instance
->physicalDeviceCount
= 0;
689 max_devices
= drmGetDevices2(0, devices
, ARRAY_SIZE(devices
));
691 return VK_ERROR_INCOMPATIBLE_DRIVER
;
693 for (unsigned i
= 0; i
< (unsigned)max_devices
; i
++) {
694 if (devices
[i
]->available_nodes
& 1 << DRM_NODE_RENDER
&&
695 devices
[i
]->bustype
== DRM_BUS_PCI
&&
696 devices
[i
]->deviceinfo
.pci
->vendor_id
== 0x8086) {
698 result
= anv_physical_device_init(&instance
->physicalDevice
,
700 devices
[i
]->nodes
[DRM_NODE_PRIMARY
],
701 devices
[i
]->nodes
[DRM_NODE_RENDER
]);
702 if (result
!= VK_ERROR_INCOMPATIBLE_DRIVER
)
706 drmFreeDevices(devices
, max_devices
);
708 if (result
== VK_SUCCESS
)
709 instance
->physicalDeviceCount
= 1;
715 anv_instance_ensure_physical_device(struct anv_instance
*instance
)
717 if (instance
->physicalDeviceCount
< 0) {
718 VkResult result
= anv_enumerate_devices(instance
);
719 if (result
!= VK_SUCCESS
&&
720 result
!= VK_ERROR_INCOMPATIBLE_DRIVER
)
727 VkResult
anv_EnumeratePhysicalDevices(
728 VkInstance _instance
,
729 uint32_t* pPhysicalDeviceCount
,
730 VkPhysicalDevice
* pPhysicalDevices
)
732 ANV_FROM_HANDLE(anv_instance
, instance
, _instance
);
733 VK_OUTARRAY_MAKE(out
, pPhysicalDevices
, pPhysicalDeviceCount
);
735 VkResult result
= anv_instance_ensure_physical_device(instance
);
736 if (result
!= VK_SUCCESS
)
739 if (instance
->physicalDeviceCount
== 0)
742 assert(instance
->physicalDeviceCount
== 1);
743 vk_outarray_append(&out
, i
) {
744 *i
= anv_physical_device_to_handle(&instance
->physicalDevice
);
747 return vk_outarray_status(&out
);
750 VkResult
anv_EnumeratePhysicalDeviceGroups(
751 VkInstance _instance
,
752 uint32_t* pPhysicalDeviceGroupCount
,
753 VkPhysicalDeviceGroupProperties
* pPhysicalDeviceGroupProperties
)
755 ANV_FROM_HANDLE(anv_instance
, instance
, _instance
);
756 VK_OUTARRAY_MAKE(out
, pPhysicalDeviceGroupProperties
,
757 pPhysicalDeviceGroupCount
);
759 VkResult result
= anv_instance_ensure_physical_device(instance
);
760 if (result
!= VK_SUCCESS
)
763 if (instance
->physicalDeviceCount
== 0)
766 assert(instance
->physicalDeviceCount
== 1);
768 vk_outarray_append(&out
, p
) {
769 p
->physicalDeviceCount
= 1;
770 memset(p
->physicalDevices
, 0, sizeof(p
->physicalDevices
));
771 p
->physicalDevices
[0] =
772 anv_physical_device_to_handle(&instance
->physicalDevice
);
773 p
->subsetAllocation
= VK_FALSE
;
775 vk_foreach_struct(ext
, p
->pNext
)
776 anv_debug_ignored_stype(ext
->sType
);
779 return vk_outarray_status(&out
);
782 void anv_GetPhysicalDeviceFeatures(
783 VkPhysicalDevice physicalDevice
,
784 VkPhysicalDeviceFeatures
* pFeatures
)
786 ANV_FROM_HANDLE(anv_physical_device
, pdevice
, physicalDevice
);
788 *pFeatures
= (VkPhysicalDeviceFeatures
) {
789 .robustBufferAccess
= true,
790 .fullDrawIndexUint32
= true,
791 .imageCubeArray
= true,
792 .independentBlend
= true,
793 .geometryShader
= true,
794 .tessellationShader
= true,
795 .sampleRateShading
= true,
796 .dualSrcBlend
= true,
798 .multiDrawIndirect
= true,
799 .drawIndirectFirstInstance
= true,
801 .depthBiasClamp
= true,
802 .fillModeNonSolid
= true,
803 .depthBounds
= false,
807 .multiViewport
= true,
808 .samplerAnisotropy
= true,
809 .textureCompressionETC2
= pdevice
->info
.gen
>= 8 ||
810 pdevice
->info
.is_baytrail
,
811 .textureCompressionASTC_LDR
= pdevice
->info
.gen
>= 9, /* FINISHME CHV */
812 .textureCompressionBC
= true,
813 .occlusionQueryPrecise
= true,
814 .pipelineStatisticsQuery
= true,
815 .fragmentStoresAndAtomics
= true,
816 .shaderTessellationAndGeometryPointSize
= true,
817 .shaderImageGatherExtended
= true,
818 .shaderStorageImageExtendedFormats
= true,
819 .shaderStorageImageMultisample
= false,
820 .shaderStorageImageReadWithoutFormat
= false,
821 .shaderStorageImageWriteWithoutFormat
= true,
822 .shaderUniformBufferArrayDynamicIndexing
= true,
823 .shaderSampledImageArrayDynamicIndexing
= true,
824 .shaderStorageBufferArrayDynamicIndexing
= true,
825 .shaderStorageImageArrayDynamicIndexing
= true,
826 .shaderClipDistance
= true,
827 .shaderCullDistance
= true,
828 .shaderFloat64
= pdevice
->info
.gen
>= 8 &&
829 pdevice
->info
.has_64bit_types
,
830 .shaderInt64
= pdevice
->info
.gen
>= 8 &&
831 pdevice
->info
.has_64bit_types
,
832 .shaderInt16
= pdevice
->info
.gen
>= 8,
833 .shaderResourceMinLod
= false,
834 .variableMultisampleRate
= true,
835 .inheritedQueries
= true,
838 /* We can't do image stores in vec4 shaders */
839 pFeatures
->vertexPipelineStoresAndAtomics
=
840 pdevice
->compiler
->scalar_stage
[MESA_SHADER_VERTEX
] &&
841 pdevice
->compiler
->scalar_stage
[MESA_SHADER_GEOMETRY
];
844 void anv_GetPhysicalDeviceFeatures2(
845 VkPhysicalDevice physicalDevice
,
846 VkPhysicalDeviceFeatures2
* pFeatures
)
848 anv_GetPhysicalDeviceFeatures(physicalDevice
, &pFeatures
->features
);
850 vk_foreach_struct(ext
, pFeatures
->pNext
) {
851 switch (ext
->sType
) {
852 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PROTECTED_MEMORY_FEATURES
: {
853 VkPhysicalDeviceProtectedMemoryFeatures
*features
= (void *)ext
;
854 features
->protectedMemory
= VK_FALSE
;
858 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_MULTIVIEW_FEATURES
: {
859 VkPhysicalDeviceMultiviewFeatures
*features
=
860 (VkPhysicalDeviceMultiviewFeatures
*)ext
;
861 features
->multiview
= true;
862 features
->multiviewGeometryShader
= true;
863 features
->multiviewTessellationShader
= true;
867 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VARIABLE_POINTER_FEATURES
: {
868 VkPhysicalDeviceVariablePointerFeatures
*features
= (void *)ext
;
869 features
->variablePointersStorageBuffer
= true;
870 features
->variablePointers
= true;
874 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SAMPLER_YCBCR_CONVERSION_FEATURES
: {
875 VkPhysicalDeviceSamplerYcbcrConversionFeatures
*features
=
876 (VkPhysicalDeviceSamplerYcbcrConversionFeatures
*) ext
;
877 features
->samplerYcbcrConversion
= true;
881 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SHADER_DRAW_PARAMETER_FEATURES
: {
882 VkPhysicalDeviceShaderDrawParameterFeatures
*features
= (void *)ext
;
883 features
->shaderDrawParameters
= true;
887 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_16BIT_STORAGE_FEATURES_KHR
: {
888 VkPhysicalDevice16BitStorageFeaturesKHR
*features
=
889 (VkPhysicalDevice16BitStorageFeaturesKHR
*)ext
;
890 ANV_FROM_HANDLE(anv_physical_device
, pdevice
, physicalDevice
);
892 features
->storageBuffer16BitAccess
= pdevice
->info
.gen
>= 8;
893 features
->uniformAndStorageBuffer16BitAccess
= pdevice
->info
.gen
>= 8;
894 features
->storagePushConstant16
= pdevice
->info
.gen
>= 8;
895 features
->storageInputOutput16
= false;
900 anv_debug_ignored_stype(ext
->sType
);
906 void anv_GetPhysicalDeviceProperties(
907 VkPhysicalDevice physicalDevice
,
908 VkPhysicalDeviceProperties
* pProperties
)
910 ANV_FROM_HANDLE(anv_physical_device
, pdevice
, physicalDevice
);
911 const struct gen_device_info
*devinfo
= &pdevice
->info
;
913 /* See assertions made when programming the buffer surface state. */
914 const uint32_t max_raw_buffer_sz
= devinfo
->gen
>= 7 ?
915 (1ul << 30) : (1ul << 27);
917 const uint32_t max_samplers
= (devinfo
->gen
>= 8 || devinfo
->is_haswell
) ?
920 VkSampleCountFlags sample_counts
=
921 isl_device_get_sample_counts(&pdevice
->isl_dev
);
923 VkPhysicalDeviceLimits limits
= {
924 .maxImageDimension1D
= (1 << 14),
925 .maxImageDimension2D
= (1 << 14),
926 .maxImageDimension3D
= (1 << 11),
927 .maxImageDimensionCube
= (1 << 14),
928 .maxImageArrayLayers
= (1 << 11),
929 .maxTexelBufferElements
= 128 * 1024 * 1024,
930 .maxUniformBufferRange
= (1ul << 27),
931 .maxStorageBufferRange
= max_raw_buffer_sz
,
932 .maxPushConstantsSize
= MAX_PUSH_CONSTANTS_SIZE
,
933 .maxMemoryAllocationCount
= UINT32_MAX
,
934 .maxSamplerAllocationCount
= 64 * 1024,
935 .bufferImageGranularity
= 64, /* A cache line */
936 .sparseAddressSpaceSize
= 0,
937 .maxBoundDescriptorSets
= MAX_SETS
,
938 .maxPerStageDescriptorSamplers
= max_samplers
,
939 .maxPerStageDescriptorUniformBuffers
= 64,
940 .maxPerStageDescriptorStorageBuffers
= 64,
941 .maxPerStageDescriptorSampledImages
= max_samplers
,
942 .maxPerStageDescriptorStorageImages
= 64,
943 .maxPerStageDescriptorInputAttachments
= 64,
944 .maxPerStageResources
= 250,
945 .maxDescriptorSetSamplers
= 6 * max_samplers
, /* number of stages * maxPerStageDescriptorSamplers */
946 .maxDescriptorSetUniformBuffers
= 6 * 64, /* number of stages * maxPerStageDescriptorUniformBuffers */
947 .maxDescriptorSetUniformBuffersDynamic
= MAX_DYNAMIC_BUFFERS
/ 2,
948 .maxDescriptorSetStorageBuffers
= 6 * 64, /* number of stages * maxPerStageDescriptorStorageBuffers */
949 .maxDescriptorSetStorageBuffersDynamic
= MAX_DYNAMIC_BUFFERS
/ 2,
950 .maxDescriptorSetSampledImages
= 6 * max_samplers
, /* number of stages * maxPerStageDescriptorSampledImages */
951 .maxDescriptorSetStorageImages
= 6 * 64, /* number of stages * maxPerStageDescriptorStorageImages */
952 .maxDescriptorSetInputAttachments
= 256,
953 .maxVertexInputAttributes
= MAX_VBS
,
954 .maxVertexInputBindings
= MAX_VBS
,
955 .maxVertexInputAttributeOffset
= 2047,
956 .maxVertexInputBindingStride
= 2048,
957 .maxVertexOutputComponents
= 128,
958 .maxTessellationGenerationLevel
= 64,
959 .maxTessellationPatchSize
= 32,
960 .maxTessellationControlPerVertexInputComponents
= 128,
961 .maxTessellationControlPerVertexOutputComponents
= 128,
962 .maxTessellationControlPerPatchOutputComponents
= 128,
963 .maxTessellationControlTotalOutputComponents
= 2048,
964 .maxTessellationEvaluationInputComponents
= 128,
965 .maxTessellationEvaluationOutputComponents
= 128,
966 .maxGeometryShaderInvocations
= 32,
967 .maxGeometryInputComponents
= 64,
968 .maxGeometryOutputComponents
= 128,
969 .maxGeometryOutputVertices
= 256,
970 .maxGeometryTotalOutputComponents
= 1024,
971 .maxFragmentInputComponents
= 112, /* 128 components - (POS, PSIZ, CLIP_DIST0, CLIP_DIST1) */
972 .maxFragmentOutputAttachments
= 8,
973 .maxFragmentDualSrcAttachments
= 1,
974 .maxFragmentCombinedOutputResources
= 8,
975 .maxComputeSharedMemorySize
= 32768,
976 .maxComputeWorkGroupCount
= { 65535, 65535, 65535 },
977 .maxComputeWorkGroupInvocations
= 16 * devinfo
->max_cs_threads
,
978 .maxComputeWorkGroupSize
= {
979 16 * devinfo
->max_cs_threads
,
980 16 * devinfo
->max_cs_threads
,
981 16 * devinfo
->max_cs_threads
,
983 .subPixelPrecisionBits
= 4 /* FIXME */,
984 .subTexelPrecisionBits
= 4 /* FIXME */,
985 .mipmapPrecisionBits
= 4 /* FIXME */,
986 .maxDrawIndexedIndexValue
= UINT32_MAX
,
987 .maxDrawIndirectCount
= UINT32_MAX
,
988 .maxSamplerLodBias
= 16,
989 .maxSamplerAnisotropy
= 16,
990 .maxViewports
= MAX_VIEWPORTS
,
991 .maxViewportDimensions
= { (1 << 14), (1 << 14) },
992 .viewportBoundsRange
= { INT16_MIN
, INT16_MAX
},
993 .viewportSubPixelBits
= 13, /* We take a float? */
994 .minMemoryMapAlignment
= 4096, /* A page */
995 .minTexelBufferOffsetAlignment
= 1,
996 /* We need 16 for UBO block reads to work and 32 for push UBOs */
997 .minUniformBufferOffsetAlignment
= 32,
998 .minStorageBufferOffsetAlignment
= 4,
999 .minTexelOffset
= -8,
1000 .maxTexelOffset
= 7,
1001 .minTexelGatherOffset
= -32,
1002 .maxTexelGatherOffset
= 31,
1003 .minInterpolationOffset
= -0.5,
1004 .maxInterpolationOffset
= 0.4375,
1005 .subPixelInterpolationOffsetBits
= 4,
1006 .maxFramebufferWidth
= (1 << 14),
1007 .maxFramebufferHeight
= (1 << 14),
1008 .maxFramebufferLayers
= (1 << 11),
1009 .framebufferColorSampleCounts
= sample_counts
,
1010 .framebufferDepthSampleCounts
= sample_counts
,
1011 .framebufferStencilSampleCounts
= sample_counts
,
1012 .framebufferNoAttachmentsSampleCounts
= sample_counts
,
1013 .maxColorAttachments
= MAX_RTS
,
1014 .sampledImageColorSampleCounts
= sample_counts
,
1015 .sampledImageIntegerSampleCounts
= VK_SAMPLE_COUNT_1_BIT
,
1016 .sampledImageDepthSampleCounts
= sample_counts
,
1017 .sampledImageStencilSampleCounts
= sample_counts
,
1018 .storageImageSampleCounts
= VK_SAMPLE_COUNT_1_BIT
,
1019 .maxSampleMaskWords
= 1,
1020 .timestampComputeAndGraphics
= false,
1021 .timestampPeriod
= 1000000000.0 / devinfo
->timestamp_frequency
,
1022 .maxClipDistances
= 8,
1023 .maxCullDistances
= 8,
1024 .maxCombinedClipAndCullDistances
= 8,
1025 .discreteQueuePriorities
= 1,
1026 .pointSizeRange
= { 0.125, 255.875 },
1027 .lineWidthRange
= { 0.0, 7.9921875 },
1028 .pointSizeGranularity
= (1.0 / 8.0),
1029 .lineWidthGranularity
= (1.0 / 128.0),
1030 .strictLines
= false, /* FINISHME */
1031 .standardSampleLocations
= true,
1032 .optimalBufferCopyOffsetAlignment
= 128,
1033 .optimalBufferCopyRowPitchAlignment
= 128,
1034 .nonCoherentAtomSize
= 64,
1037 *pProperties
= (VkPhysicalDeviceProperties
) {
1038 .apiVersion
= anv_physical_device_api_version(pdevice
),
1039 .driverVersion
= vk_get_driver_version(),
1041 .deviceID
= pdevice
->chipset_id
,
1042 .deviceType
= VK_PHYSICAL_DEVICE_TYPE_INTEGRATED_GPU
,
1044 .sparseProperties
= {0}, /* Broadwell doesn't do sparse. */
1047 snprintf(pProperties
->deviceName
, sizeof(pProperties
->deviceName
),
1048 "%s", pdevice
->name
);
1049 memcpy(pProperties
->pipelineCacheUUID
,
1050 pdevice
->pipeline_cache_uuid
, VK_UUID_SIZE
);
1053 void anv_GetPhysicalDeviceProperties2(
1054 VkPhysicalDevice physicalDevice
,
1055 VkPhysicalDeviceProperties2
* pProperties
)
1057 ANV_FROM_HANDLE(anv_physical_device
, pdevice
, physicalDevice
);
1059 anv_GetPhysicalDeviceProperties(physicalDevice
, &pProperties
->properties
);
1061 vk_foreach_struct(ext
, pProperties
->pNext
) {
1062 switch (ext
->sType
) {
1063 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PUSH_DESCRIPTOR_PROPERTIES_KHR
: {
1064 VkPhysicalDevicePushDescriptorPropertiesKHR
*properties
=
1065 (VkPhysicalDevicePushDescriptorPropertiesKHR
*) ext
;
1067 properties
->maxPushDescriptors
= MAX_PUSH_DESCRIPTORS
;
1071 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_ID_PROPERTIES
: {
1072 VkPhysicalDeviceIDProperties
*id_props
=
1073 (VkPhysicalDeviceIDProperties
*)ext
;
1074 memcpy(id_props
->deviceUUID
, pdevice
->device_uuid
, VK_UUID_SIZE
);
1075 memcpy(id_props
->driverUUID
, pdevice
->driver_uuid
, VK_UUID_SIZE
);
1076 /* The LUID is for Windows. */
1077 id_props
->deviceLUIDValid
= false;
1081 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_MAINTENANCE_3_PROPERTIES
: {
1082 VkPhysicalDeviceMaintenance3Properties
*props
=
1083 (VkPhysicalDeviceMaintenance3Properties
*)ext
;
1084 /* This value doesn't matter for us today as our per-stage
1085 * descriptors are the real limit.
1087 props
->maxPerSetDescriptors
= 1024;
1088 props
->maxMemoryAllocationSize
= MAX_MEMORY_ALLOCATION_SIZE
;
1092 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_MULTIVIEW_PROPERTIES
: {
1093 VkPhysicalDeviceMultiviewProperties
*properties
=
1094 (VkPhysicalDeviceMultiviewProperties
*)ext
;
1095 properties
->maxMultiviewViewCount
= 16;
1096 properties
->maxMultiviewInstanceIndex
= UINT32_MAX
/ 16;
1100 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_POINT_CLIPPING_PROPERTIES
: {
1101 VkPhysicalDevicePointClippingProperties
*properties
=
1102 (VkPhysicalDevicePointClippingProperties
*) ext
;
1103 properties
->pointClippingBehavior
= VK_POINT_CLIPPING_BEHAVIOR_ALL_CLIP_PLANES
;
1104 anv_finishme("Implement pop-free point clipping");
1108 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SUBGROUP_PROPERTIES
: {
1109 VkPhysicalDeviceSubgroupProperties
*properties
= (void *)ext
;
1111 properties
->subgroupSize
= BRW_SUBGROUP_SIZE
;
1113 VkShaderStageFlags scalar_stages
= 0;
1114 for (unsigned stage
= 0; stage
< MESA_SHADER_STAGES
; stage
++) {
1115 if (pdevice
->compiler
->scalar_stage
[stage
])
1116 scalar_stages
|= mesa_to_vk_shader_stage(stage
);
1118 properties
->supportedStages
= scalar_stages
;
1120 properties
->supportedOperations
= VK_SUBGROUP_FEATURE_BASIC_BIT
|
1121 VK_SUBGROUP_FEATURE_VOTE_BIT
|
1122 VK_SUBGROUP_FEATURE_ARITHMETIC_BIT
|
1123 VK_SUBGROUP_FEATURE_BALLOT_BIT
|
1124 VK_SUBGROUP_FEATURE_SHUFFLE_BIT
|
1125 VK_SUBGROUP_FEATURE_SHUFFLE_RELATIVE_BIT
|
1126 VK_SUBGROUP_FEATURE_CLUSTERED_BIT
|
1127 VK_SUBGROUP_FEATURE_QUAD_BIT
;
1128 properties
->quadOperationsInAllStages
= VK_TRUE
;
1133 anv_debug_ignored_stype(ext
->sType
);
1139 /* We support exactly one queue family. */
1140 static const VkQueueFamilyProperties
1141 anv_queue_family_properties
= {
1142 .queueFlags
= VK_QUEUE_GRAPHICS_BIT
|
1143 VK_QUEUE_COMPUTE_BIT
|
1144 VK_QUEUE_TRANSFER_BIT
,
1146 .timestampValidBits
= 36, /* XXX: Real value here */
1147 .minImageTransferGranularity
= { 1, 1, 1 },
1150 void anv_GetPhysicalDeviceQueueFamilyProperties(
1151 VkPhysicalDevice physicalDevice
,
1153 VkQueueFamilyProperties
* pQueueFamilyProperties
)
1155 VK_OUTARRAY_MAKE(out
, pQueueFamilyProperties
, pCount
);
1157 vk_outarray_append(&out
, p
) {
1158 *p
= anv_queue_family_properties
;
1162 void anv_GetPhysicalDeviceQueueFamilyProperties2(
1163 VkPhysicalDevice physicalDevice
,
1164 uint32_t* pQueueFamilyPropertyCount
,
1165 VkQueueFamilyProperties2
* pQueueFamilyProperties
)
1168 VK_OUTARRAY_MAKE(out
, pQueueFamilyProperties
, pQueueFamilyPropertyCount
);
1170 vk_outarray_append(&out
, p
) {
1171 p
->queueFamilyProperties
= anv_queue_family_properties
;
1173 vk_foreach_struct(s
, p
->pNext
) {
1174 anv_debug_ignored_stype(s
->sType
);
1179 void anv_GetPhysicalDeviceMemoryProperties(
1180 VkPhysicalDevice physicalDevice
,
1181 VkPhysicalDeviceMemoryProperties
* pMemoryProperties
)
1183 ANV_FROM_HANDLE(anv_physical_device
, physical_device
, physicalDevice
);
1185 pMemoryProperties
->memoryTypeCount
= physical_device
->memory
.type_count
;
1186 for (uint32_t i
= 0; i
< physical_device
->memory
.type_count
; i
++) {
1187 pMemoryProperties
->memoryTypes
[i
] = (VkMemoryType
) {
1188 .propertyFlags
= physical_device
->memory
.types
[i
].propertyFlags
,
1189 .heapIndex
= physical_device
->memory
.types
[i
].heapIndex
,
1193 pMemoryProperties
->memoryHeapCount
= physical_device
->memory
.heap_count
;
1194 for (uint32_t i
= 0; i
< physical_device
->memory
.heap_count
; i
++) {
1195 pMemoryProperties
->memoryHeaps
[i
] = (VkMemoryHeap
) {
1196 .size
= physical_device
->memory
.heaps
[i
].size
,
1197 .flags
= physical_device
->memory
.heaps
[i
].flags
,
1202 void anv_GetPhysicalDeviceMemoryProperties2(
1203 VkPhysicalDevice physicalDevice
,
1204 VkPhysicalDeviceMemoryProperties2
* pMemoryProperties
)
1206 anv_GetPhysicalDeviceMemoryProperties(physicalDevice
,
1207 &pMemoryProperties
->memoryProperties
);
1209 vk_foreach_struct(ext
, pMemoryProperties
->pNext
) {
1210 switch (ext
->sType
) {
1212 anv_debug_ignored_stype(ext
->sType
);
1219 anv_GetDeviceGroupPeerMemoryFeatures(
1222 uint32_t localDeviceIndex
,
1223 uint32_t remoteDeviceIndex
,
1224 VkPeerMemoryFeatureFlags
* pPeerMemoryFeatures
)
1226 assert(localDeviceIndex
== 0 && remoteDeviceIndex
== 0);
1227 *pPeerMemoryFeatures
= VK_PEER_MEMORY_FEATURE_COPY_SRC_BIT
|
1228 VK_PEER_MEMORY_FEATURE_COPY_DST_BIT
|
1229 VK_PEER_MEMORY_FEATURE_GENERIC_SRC_BIT
|
1230 VK_PEER_MEMORY_FEATURE_GENERIC_DST_BIT
;
1233 PFN_vkVoidFunction
anv_GetInstanceProcAddr(
1234 VkInstance _instance
,
1237 ANV_FROM_HANDLE(anv_instance
, instance
, _instance
);
1239 /* The Vulkan 1.0 spec for vkGetInstanceProcAddr has a table of exactly
1240 * when we have to return valid function pointers, NULL, or it's left
1241 * undefined. See the table for exact details.
1246 #define LOOKUP_ANV_ENTRYPOINT(entrypoint) \
1247 if (strcmp(pName, "vk" #entrypoint) == 0) \
1248 return (PFN_vkVoidFunction)anv_##entrypoint
1250 LOOKUP_ANV_ENTRYPOINT(EnumerateInstanceExtensionProperties
);
1251 LOOKUP_ANV_ENTRYPOINT(EnumerateInstanceLayerProperties
);
1252 LOOKUP_ANV_ENTRYPOINT(EnumerateInstanceVersion
);
1253 LOOKUP_ANV_ENTRYPOINT(CreateInstance
);
1255 #undef LOOKUP_ANV_ENTRYPOINT
1257 if (instance
== NULL
)
1260 int idx
= anv_get_entrypoint_index(pName
);
1264 return instance
->dispatch
.entrypoints
[idx
];
1267 /* With version 1+ of the loader interface the ICD should expose
1268 * vk_icdGetInstanceProcAddr to work around certain LD_PRELOAD issues seen in apps.
1271 VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL
vk_icdGetInstanceProcAddr(
1272 VkInstance instance
,
1276 VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL
vk_icdGetInstanceProcAddr(
1277 VkInstance instance
,
1280 return anv_GetInstanceProcAddr(instance
, pName
);
1283 PFN_vkVoidFunction
anv_GetDeviceProcAddr(
1287 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1289 if (!device
|| !pName
)
1292 int idx
= anv_get_entrypoint_index(pName
);
1296 return device
->dispatch
.entrypoints
[idx
];
1300 anv_CreateDebugReportCallbackEXT(VkInstance _instance
,
1301 const VkDebugReportCallbackCreateInfoEXT
* pCreateInfo
,
1302 const VkAllocationCallbacks
* pAllocator
,
1303 VkDebugReportCallbackEXT
* pCallback
)
1305 ANV_FROM_HANDLE(anv_instance
, instance
, _instance
);
1306 return vk_create_debug_report_callback(&instance
->debug_report_callbacks
,
1307 pCreateInfo
, pAllocator
, &instance
->alloc
,
1312 anv_DestroyDebugReportCallbackEXT(VkInstance _instance
,
1313 VkDebugReportCallbackEXT _callback
,
1314 const VkAllocationCallbacks
* pAllocator
)
1316 ANV_FROM_HANDLE(anv_instance
, instance
, _instance
);
1317 vk_destroy_debug_report_callback(&instance
->debug_report_callbacks
,
1318 _callback
, pAllocator
, &instance
->alloc
);
1322 anv_DebugReportMessageEXT(VkInstance _instance
,
1323 VkDebugReportFlagsEXT flags
,
1324 VkDebugReportObjectTypeEXT objectType
,
1327 int32_t messageCode
,
1328 const char* pLayerPrefix
,
1329 const char* pMessage
)
1331 ANV_FROM_HANDLE(anv_instance
, instance
, _instance
);
1332 vk_debug_report(&instance
->debug_report_callbacks
, flags
, objectType
,
1333 object
, location
, messageCode
, pLayerPrefix
, pMessage
);
1337 anv_queue_init(struct anv_device
*device
, struct anv_queue
*queue
)
1339 queue
->_loader_data
.loaderMagic
= ICD_LOADER_MAGIC
;
1340 queue
->device
= device
;
1345 anv_queue_finish(struct anv_queue
*queue
)
1349 static struct anv_state
1350 anv_state_pool_emit_data(struct anv_state_pool
*pool
, size_t size
, size_t align
, const void *p
)
1352 struct anv_state state
;
1354 state
= anv_state_pool_alloc(pool
, size
, align
);
1355 memcpy(state
.map
, p
, size
);
1357 anv_state_flush(pool
->block_pool
.device
, state
);
1362 struct gen8_border_color
{
1367 /* Pad out to 64 bytes */
1372 anv_device_init_border_colors(struct anv_device
*device
)
1374 static const struct gen8_border_color border_colors
[] = {
1375 [VK_BORDER_COLOR_FLOAT_TRANSPARENT_BLACK
] = { .float32
= { 0.0, 0.0, 0.0, 0.0 } },
1376 [VK_BORDER_COLOR_FLOAT_OPAQUE_BLACK
] = { .float32
= { 0.0, 0.0, 0.0, 1.0 } },
1377 [VK_BORDER_COLOR_FLOAT_OPAQUE_WHITE
] = { .float32
= { 1.0, 1.0, 1.0, 1.0 } },
1378 [VK_BORDER_COLOR_INT_TRANSPARENT_BLACK
] = { .uint32
= { 0, 0, 0, 0 } },
1379 [VK_BORDER_COLOR_INT_OPAQUE_BLACK
] = { .uint32
= { 0, 0, 0, 1 } },
1380 [VK_BORDER_COLOR_INT_OPAQUE_WHITE
] = { .uint32
= { 1, 1, 1, 1 } },
1383 device
->border_colors
= anv_state_pool_emit_data(&device
->dynamic_state_pool
,
1384 sizeof(border_colors
), 64,
1389 anv_device_init_trivial_batch(struct anv_device
*device
)
1391 anv_bo_init_new(&device
->trivial_batch_bo
, device
, 4096);
1393 if (device
->instance
->physicalDevice
.has_exec_async
)
1394 device
->trivial_batch_bo
.flags
|= EXEC_OBJECT_ASYNC
;
1396 if (device
->instance
->physicalDevice
.use_softpin
)
1397 device
->trivial_batch_bo
.flags
|= EXEC_OBJECT_PINNED
;
1399 anv_vma_alloc(device
, &device
->trivial_batch_bo
);
1401 void *map
= anv_gem_mmap(device
, device
->trivial_batch_bo
.gem_handle
,
1404 struct anv_batch batch
= {
1410 anv_batch_emit(&batch
, GEN7_MI_BATCH_BUFFER_END
, bbe
);
1411 anv_batch_emit(&batch
, GEN7_MI_NOOP
, noop
);
1413 if (!device
->info
.has_llc
)
1414 gen_clflush_range(map
, batch
.next
- map
);
1416 anv_gem_munmap(map
, device
->trivial_batch_bo
.size
);
1419 VkResult
anv_EnumerateDeviceExtensionProperties(
1420 VkPhysicalDevice physicalDevice
,
1421 const char* pLayerName
,
1422 uint32_t* pPropertyCount
,
1423 VkExtensionProperties
* pProperties
)
1425 ANV_FROM_HANDLE(anv_physical_device
, device
, physicalDevice
);
1426 VK_OUTARRAY_MAKE(out
, pProperties
, pPropertyCount
);
1429 for (int i
= 0; i
< ANV_DEVICE_EXTENSION_COUNT
; i
++) {
1430 if (device
->supported_extensions
.extensions
[i
]) {
1431 vk_outarray_append(&out
, prop
) {
1432 *prop
= anv_device_extensions
[i
];
1437 return vk_outarray_status(&out
);
1441 anv_device_init_dispatch(struct anv_device
*device
)
1443 const struct anv_dispatch_table
*genX_table
;
1444 switch (device
->info
.gen
) {
1446 genX_table
= &gen11_dispatch_table
;
1449 genX_table
= &gen10_dispatch_table
;
1452 genX_table
= &gen9_dispatch_table
;
1455 genX_table
= &gen8_dispatch_table
;
1458 if (device
->info
.is_haswell
)
1459 genX_table
= &gen75_dispatch_table
;
1461 genX_table
= &gen7_dispatch_table
;
1464 unreachable("unsupported gen\n");
1467 for (unsigned i
= 0; i
< ARRAY_SIZE(device
->dispatch
.entrypoints
); i
++) {
1468 /* Vulkan requires that entrypoints for extensions which have not been
1469 * enabled must not be advertised.
1471 if (!anv_entrypoint_is_enabled(i
, device
->instance
->apiVersion
,
1472 &device
->instance
->enabled_extensions
,
1473 &device
->enabled_extensions
)) {
1474 device
->dispatch
.entrypoints
[i
] = NULL
;
1475 } else if (genX_table
->entrypoints
[i
]) {
1476 device
->dispatch
.entrypoints
[i
] = genX_table
->entrypoints
[i
];
1478 device
->dispatch
.entrypoints
[i
] = anv_dispatch_table
.entrypoints
[i
];
1484 vk_priority_to_gen(int priority
)
1487 case VK_QUEUE_GLOBAL_PRIORITY_LOW_EXT
:
1488 return GEN_CONTEXT_LOW_PRIORITY
;
1489 case VK_QUEUE_GLOBAL_PRIORITY_MEDIUM_EXT
:
1490 return GEN_CONTEXT_MEDIUM_PRIORITY
;
1491 case VK_QUEUE_GLOBAL_PRIORITY_HIGH_EXT
:
1492 return GEN_CONTEXT_HIGH_PRIORITY
;
1493 case VK_QUEUE_GLOBAL_PRIORITY_REALTIME_EXT
:
1494 return GEN_CONTEXT_REALTIME_PRIORITY
;
1496 unreachable("Invalid priority");
1501 anv_device_init_hiz_clear_batch(struct anv_device
*device
)
1503 anv_bo_init_new(&device
->hiz_clear_bo
, device
, 4096);
1504 uint32_t *map
= anv_gem_mmap(device
, device
->hiz_clear_bo
.gem_handle
,
1507 union isl_color_value hiz_clear
= { .u32
= { 0, } };
1508 hiz_clear
.f32
[0] = ANV_HZ_FC_VAL
;
1510 memcpy(map
, hiz_clear
.u32
, sizeof(hiz_clear
.u32
));
1511 anv_gem_munmap(map
, device
->hiz_clear_bo
.size
);
1514 VkResult
anv_CreateDevice(
1515 VkPhysicalDevice physicalDevice
,
1516 const VkDeviceCreateInfo
* pCreateInfo
,
1517 const VkAllocationCallbacks
* pAllocator
,
1520 ANV_FROM_HANDLE(anv_physical_device
, physical_device
, physicalDevice
);
1522 struct anv_device
*device
;
1524 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO
);
1526 struct anv_device_extension_table enabled_extensions
= { };
1527 for (uint32_t i
= 0; i
< pCreateInfo
->enabledExtensionCount
; i
++) {
1529 for (idx
= 0; idx
< ANV_DEVICE_EXTENSION_COUNT
; idx
++) {
1530 if (strcmp(pCreateInfo
->ppEnabledExtensionNames
[i
],
1531 anv_device_extensions
[idx
].extensionName
) == 0)
1535 if (idx
>= ANV_DEVICE_EXTENSION_COUNT
)
1536 return vk_error(VK_ERROR_EXTENSION_NOT_PRESENT
);
1538 if (!physical_device
->supported_extensions
.extensions
[idx
])
1539 return vk_error(VK_ERROR_EXTENSION_NOT_PRESENT
);
1541 enabled_extensions
.extensions
[idx
] = true;
1544 /* Check enabled features */
1545 if (pCreateInfo
->pEnabledFeatures
) {
1546 VkPhysicalDeviceFeatures supported_features
;
1547 anv_GetPhysicalDeviceFeatures(physicalDevice
, &supported_features
);
1548 VkBool32
*supported_feature
= (VkBool32
*)&supported_features
;
1549 VkBool32
*enabled_feature
= (VkBool32
*)pCreateInfo
->pEnabledFeatures
;
1550 unsigned num_features
= sizeof(VkPhysicalDeviceFeatures
) / sizeof(VkBool32
);
1551 for (uint32_t i
= 0; i
< num_features
; i
++) {
1552 if (enabled_feature
[i
] && !supported_feature
[i
])
1553 return vk_error(VK_ERROR_FEATURE_NOT_PRESENT
);
1557 /* Check requested queues and fail if we are requested to create any
1558 * queues with flags we don't support.
1560 assert(pCreateInfo
->queueCreateInfoCount
> 0);
1561 for (uint32_t i
= 0; i
< pCreateInfo
->queueCreateInfoCount
; i
++) {
1562 if (pCreateInfo
->pQueueCreateInfos
[i
].flags
!= 0)
1563 return vk_error(VK_ERROR_INITIALIZATION_FAILED
);
1566 /* Check if client specified queue priority. */
1567 const VkDeviceQueueGlobalPriorityCreateInfoEXT
*queue_priority
=
1568 vk_find_struct_const(pCreateInfo
->pQueueCreateInfos
[0].pNext
,
1569 DEVICE_QUEUE_GLOBAL_PRIORITY_CREATE_INFO_EXT
);
1571 VkQueueGlobalPriorityEXT priority
=
1572 queue_priority
? queue_priority
->globalPriority
:
1573 VK_QUEUE_GLOBAL_PRIORITY_MEDIUM_EXT
;
1575 device
= vk_alloc2(&physical_device
->instance
->alloc
, pAllocator
,
1577 VK_SYSTEM_ALLOCATION_SCOPE_DEVICE
);
1579 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
1581 device
->_loader_data
.loaderMagic
= ICD_LOADER_MAGIC
;
1582 device
->instance
= physical_device
->instance
;
1583 device
->chipset_id
= physical_device
->chipset_id
;
1584 device
->no_hw
= physical_device
->no_hw
;
1585 device
->lost
= false;
1588 device
->alloc
= *pAllocator
;
1590 device
->alloc
= physical_device
->instance
->alloc
;
1592 /* XXX(chadv): Can we dup() physicalDevice->fd here? */
1593 device
->fd
= open(physical_device
->path
, O_RDWR
| O_CLOEXEC
);
1594 if (device
->fd
== -1) {
1595 result
= vk_error(VK_ERROR_INITIALIZATION_FAILED
);
1599 device
->context_id
= anv_gem_create_context(device
);
1600 if (device
->context_id
== -1) {
1601 result
= vk_error(VK_ERROR_INITIALIZATION_FAILED
);
1605 if (physical_device
->use_softpin
) {
1606 if (pthread_mutex_init(&device
->vma_mutex
, NULL
) != 0) {
1607 result
= vk_error(VK_ERROR_INITIALIZATION_FAILED
);
1611 /* keep the page with address zero out of the allocator */
1612 util_vma_heap_init(&device
->vma_lo
, LOW_HEAP_MIN_ADDRESS
, LOW_HEAP_SIZE
);
1613 device
->vma_lo_available
=
1614 physical_device
->memory
.heaps
[physical_device
->memory
.heap_count
- 1].size
;
1616 /* Leave the last 4GiB out of the high vma range, so that no state base
1617 * address + size can overflow 48 bits. For more information see the
1618 * comment about Wa32bitGeneralStateOffset in anv_allocator.c
1620 util_vma_heap_init(&device
->vma_hi
, HIGH_HEAP_MIN_ADDRESS
,
1622 device
->vma_hi_available
= physical_device
->memory
.heap_count
== 1 ? 0 :
1623 physical_device
->memory
.heaps
[0].size
;
1626 /* As per spec, the driver implementation may deny requests to acquire
1627 * a priority above the default priority (MEDIUM) if the caller does not
1628 * have sufficient privileges. In this scenario VK_ERROR_NOT_PERMITTED_EXT
1631 if (physical_device
->has_context_priority
) {
1632 int err
= anv_gem_set_context_param(device
->fd
, device
->context_id
,
1633 I915_CONTEXT_PARAM_PRIORITY
,
1634 vk_priority_to_gen(priority
));
1635 if (err
!= 0 && priority
> VK_QUEUE_GLOBAL_PRIORITY_MEDIUM_EXT
) {
1636 result
= vk_error(VK_ERROR_NOT_PERMITTED_EXT
);
1641 device
->info
= physical_device
->info
;
1642 device
->isl_dev
= physical_device
->isl_dev
;
1644 /* On Broadwell and later, we can use batch chaining to more efficiently
1645 * implement growing command buffers. Prior to Haswell, the kernel
1646 * command parser gets in the way and we have to fall back to growing
1649 device
->can_chain_batches
= device
->info
.gen
>= 8;
1651 device
->robust_buffer_access
= pCreateInfo
->pEnabledFeatures
&&
1652 pCreateInfo
->pEnabledFeatures
->robustBufferAccess
;
1653 device
->enabled_extensions
= enabled_extensions
;
1655 anv_device_init_dispatch(device
);
1657 if (pthread_mutex_init(&device
->mutex
, NULL
) != 0) {
1658 result
= vk_error(VK_ERROR_INITIALIZATION_FAILED
);
1659 goto fail_context_id
;
1662 pthread_condattr_t condattr
;
1663 if (pthread_condattr_init(&condattr
) != 0) {
1664 result
= vk_error(VK_ERROR_INITIALIZATION_FAILED
);
1667 if (pthread_condattr_setclock(&condattr
, CLOCK_MONOTONIC
) != 0) {
1668 pthread_condattr_destroy(&condattr
);
1669 result
= vk_error(VK_ERROR_INITIALIZATION_FAILED
);
1672 if (pthread_cond_init(&device
->queue_submit
, NULL
) != 0) {
1673 pthread_condattr_destroy(&condattr
);
1674 result
= vk_error(VK_ERROR_INITIALIZATION_FAILED
);
1677 pthread_condattr_destroy(&condattr
);
1680 (physical_device
->supports_48bit_addresses
? EXEC_OBJECT_SUPPORTS_48B_ADDRESS
: 0) |
1681 (physical_device
->has_exec_async
? EXEC_OBJECT_ASYNC
: 0) |
1682 (physical_device
->has_exec_capture
? EXEC_OBJECT_CAPTURE
: 0) |
1683 (physical_device
->use_softpin
? EXEC_OBJECT_PINNED
: 0);
1685 anv_bo_pool_init(&device
->batch_bo_pool
, device
, bo_flags
);
1687 result
= anv_bo_cache_init(&device
->bo_cache
);
1688 if (result
!= VK_SUCCESS
)
1689 goto fail_batch_bo_pool
;
1691 if (!physical_device
->use_softpin
)
1692 bo_flags
&= ~EXEC_OBJECT_SUPPORTS_48B_ADDRESS
;
1694 result
= anv_state_pool_init(&device
->dynamic_state_pool
, device
,
1695 DYNAMIC_STATE_POOL_MIN_ADDRESS
,
1698 if (result
!= VK_SUCCESS
)
1701 result
= anv_state_pool_init(&device
->instruction_state_pool
, device
,
1702 INSTRUCTION_STATE_POOL_MIN_ADDRESS
,
1705 if (result
!= VK_SUCCESS
)
1706 goto fail_dynamic_state_pool
;
1708 result
= anv_state_pool_init(&device
->surface_state_pool
, device
,
1709 SURFACE_STATE_POOL_MIN_ADDRESS
,
1712 if (result
!= VK_SUCCESS
)
1713 goto fail_instruction_state_pool
;
1715 if (physical_device
->use_softpin
) {
1716 result
= anv_state_pool_init(&device
->binding_table_pool
, device
,
1717 BINDING_TABLE_POOL_MIN_ADDRESS
,
1720 if (result
!= VK_SUCCESS
)
1721 goto fail_surface_state_pool
;
1724 result
= anv_bo_init_new(&device
->workaround_bo
, device
, 1024);
1725 if (result
!= VK_SUCCESS
)
1726 goto fail_binding_table_pool
;
1728 if (physical_device
->use_softpin
)
1729 device
->workaround_bo
.flags
|= EXEC_OBJECT_PINNED
;
1731 if (!anv_vma_alloc(device
, &device
->workaround_bo
))
1732 goto fail_workaround_bo
;
1734 anv_device_init_trivial_batch(device
);
1736 if (device
->info
.gen
>= 10)
1737 anv_device_init_hiz_clear_batch(device
);
1739 anv_scratch_pool_init(device
, &device
->scratch_pool
);
1741 anv_queue_init(device
, &device
->queue
);
1743 switch (device
->info
.gen
) {
1745 if (!device
->info
.is_haswell
)
1746 result
= gen7_init_device_state(device
);
1748 result
= gen75_init_device_state(device
);
1751 result
= gen8_init_device_state(device
);
1754 result
= gen9_init_device_state(device
);
1757 result
= gen10_init_device_state(device
);
1760 result
= gen11_init_device_state(device
);
1763 /* Shouldn't get here as we don't create physical devices for any other
1765 unreachable("unhandled gen");
1767 if (result
!= VK_SUCCESS
)
1768 goto fail_workaround_bo
;
1770 anv_pipeline_cache_init(&device
->default_pipeline_cache
, device
, true);
1772 anv_device_init_blorp(device
);
1774 anv_device_init_border_colors(device
);
1776 *pDevice
= anv_device_to_handle(device
);
1781 anv_queue_finish(&device
->queue
);
1782 anv_scratch_pool_finish(device
, &device
->scratch_pool
);
1783 anv_gem_munmap(device
->workaround_bo
.map
, device
->workaround_bo
.size
);
1784 anv_gem_close(device
, device
->workaround_bo
.gem_handle
);
1785 fail_binding_table_pool
:
1786 if (physical_device
->use_softpin
)
1787 anv_state_pool_finish(&device
->binding_table_pool
);
1788 fail_surface_state_pool
:
1789 anv_state_pool_finish(&device
->surface_state_pool
);
1790 fail_instruction_state_pool
:
1791 anv_state_pool_finish(&device
->instruction_state_pool
);
1792 fail_dynamic_state_pool
:
1793 anv_state_pool_finish(&device
->dynamic_state_pool
);
1795 anv_bo_cache_finish(&device
->bo_cache
);
1797 anv_bo_pool_finish(&device
->batch_bo_pool
);
1798 pthread_cond_destroy(&device
->queue_submit
);
1800 pthread_mutex_destroy(&device
->mutex
);
1802 anv_gem_destroy_context(device
, device
->context_id
);
1806 vk_free(&device
->alloc
, device
);
1811 void anv_DestroyDevice(
1813 const VkAllocationCallbacks
* pAllocator
)
1815 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1816 struct anv_physical_device
*physical_device
= &device
->instance
->physicalDevice
;
1821 anv_device_finish_blorp(device
);
1823 anv_pipeline_cache_finish(&device
->default_pipeline_cache
);
1825 anv_queue_finish(&device
->queue
);
1827 #ifdef HAVE_VALGRIND
1828 /* We only need to free these to prevent valgrind errors. The backing
1829 * BO will go away in a couple of lines so we don't actually leak.
1831 anv_state_pool_free(&device
->dynamic_state_pool
, device
->border_colors
);
1834 anv_scratch_pool_finish(device
, &device
->scratch_pool
);
1836 anv_gem_munmap(device
->workaround_bo
.map
, device
->workaround_bo
.size
);
1837 anv_vma_free(device
, &device
->workaround_bo
);
1838 anv_gem_close(device
, device
->workaround_bo
.gem_handle
);
1840 anv_vma_free(device
, &device
->trivial_batch_bo
);
1841 anv_gem_close(device
, device
->trivial_batch_bo
.gem_handle
);
1842 if (device
->info
.gen
>= 10)
1843 anv_gem_close(device
, device
->hiz_clear_bo
.gem_handle
);
1845 if (physical_device
->use_softpin
)
1846 anv_state_pool_finish(&device
->binding_table_pool
);
1847 anv_state_pool_finish(&device
->surface_state_pool
);
1848 anv_state_pool_finish(&device
->instruction_state_pool
);
1849 anv_state_pool_finish(&device
->dynamic_state_pool
);
1851 anv_bo_cache_finish(&device
->bo_cache
);
1853 anv_bo_pool_finish(&device
->batch_bo_pool
);
1855 pthread_cond_destroy(&device
->queue_submit
);
1856 pthread_mutex_destroy(&device
->mutex
);
1858 anv_gem_destroy_context(device
, device
->context_id
);
1862 vk_free(&device
->alloc
, device
);
1865 VkResult
anv_EnumerateInstanceLayerProperties(
1866 uint32_t* pPropertyCount
,
1867 VkLayerProperties
* pProperties
)
1869 if (pProperties
== NULL
) {
1870 *pPropertyCount
= 0;
1874 /* None supported at this time */
1875 return vk_error(VK_ERROR_LAYER_NOT_PRESENT
);
1878 VkResult
anv_EnumerateDeviceLayerProperties(
1879 VkPhysicalDevice physicalDevice
,
1880 uint32_t* pPropertyCount
,
1881 VkLayerProperties
* pProperties
)
1883 if (pProperties
== NULL
) {
1884 *pPropertyCount
= 0;
1888 /* None supported at this time */
1889 return vk_error(VK_ERROR_LAYER_NOT_PRESENT
);
1892 void anv_GetDeviceQueue(
1894 uint32_t queueNodeIndex
,
1895 uint32_t queueIndex
,
1898 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1900 assert(queueIndex
== 0);
1902 *pQueue
= anv_queue_to_handle(&device
->queue
);
1905 void anv_GetDeviceQueue2(
1907 const VkDeviceQueueInfo2
* pQueueInfo
,
1910 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1912 assert(pQueueInfo
->queueIndex
== 0);
1914 if (pQueueInfo
->flags
== device
->queue
.flags
)
1915 *pQueue
= anv_queue_to_handle(&device
->queue
);
1921 anv_device_query_status(struct anv_device
*device
)
1923 /* This isn't likely as most of the callers of this function already check
1924 * for it. However, it doesn't hurt to check and it potentially lets us
1927 if (unlikely(device
->lost
))
1928 return VK_ERROR_DEVICE_LOST
;
1930 uint32_t active
, pending
;
1931 int ret
= anv_gem_gpu_get_reset_stats(device
, &active
, &pending
);
1933 /* We don't know the real error. */
1934 device
->lost
= true;
1935 return vk_errorf(device
->instance
, device
, VK_ERROR_DEVICE_LOST
,
1936 "get_reset_stats failed: %m");
1940 device
->lost
= true;
1941 return vk_errorf(device
->instance
, device
, VK_ERROR_DEVICE_LOST
,
1942 "GPU hung on one of our command buffers");
1943 } else if (pending
) {
1944 device
->lost
= true;
1945 return vk_errorf(device
->instance
, device
, VK_ERROR_DEVICE_LOST
,
1946 "GPU hung with commands in-flight");
1953 anv_device_bo_busy(struct anv_device
*device
, struct anv_bo
*bo
)
1955 /* Note: This only returns whether or not the BO is in use by an i915 GPU.
1956 * Other usages of the BO (such as on different hardware) will not be
1957 * flagged as "busy" by this ioctl. Use with care.
1959 int ret
= anv_gem_busy(device
, bo
->gem_handle
);
1961 return VK_NOT_READY
;
1962 } else if (ret
== -1) {
1963 /* We don't know the real error. */
1964 device
->lost
= true;
1965 return vk_errorf(device
->instance
, device
, VK_ERROR_DEVICE_LOST
,
1966 "gem wait failed: %m");
1969 /* Query for device status after the busy call. If the BO we're checking
1970 * got caught in a GPU hang we don't want to return VK_SUCCESS to the
1971 * client because it clearly doesn't have valid data. Yes, this most
1972 * likely means an ioctl, but we just did an ioctl to query the busy status
1973 * so it's no great loss.
1975 return anv_device_query_status(device
);
1979 anv_device_wait(struct anv_device
*device
, struct anv_bo
*bo
,
1982 int ret
= anv_gem_wait(device
, bo
->gem_handle
, &timeout
);
1983 if (ret
== -1 && errno
== ETIME
) {
1985 } else if (ret
== -1) {
1986 /* We don't know the real error. */
1987 device
->lost
= true;
1988 return vk_errorf(device
->instance
, device
, VK_ERROR_DEVICE_LOST
,
1989 "gem wait failed: %m");
1992 /* Query for device status after the wait. If the BO we're waiting on got
1993 * caught in a GPU hang we don't want to return VK_SUCCESS to the client
1994 * because it clearly doesn't have valid data. Yes, this most likely means
1995 * an ioctl, but we just did an ioctl to wait so it's no great loss.
1997 return anv_device_query_status(device
);
2000 VkResult
anv_DeviceWaitIdle(
2003 ANV_FROM_HANDLE(anv_device
, device
, _device
);
2004 if (unlikely(device
->lost
))
2005 return VK_ERROR_DEVICE_LOST
;
2007 struct anv_batch batch
;
2010 batch
.start
= batch
.next
= cmds
;
2011 batch
.end
= (void *) cmds
+ sizeof(cmds
);
2013 anv_batch_emit(&batch
, GEN7_MI_BATCH_BUFFER_END
, bbe
);
2014 anv_batch_emit(&batch
, GEN7_MI_NOOP
, noop
);
2016 return anv_device_submit_simple_batch(device
, &batch
);
2020 anv_vma_alloc(struct anv_device
*device
, struct anv_bo
*bo
)
2022 if (!(bo
->flags
& EXEC_OBJECT_PINNED
))
2025 pthread_mutex_lock(&device
->vma_mutex
);
2029 if (bo
->flags
& EXEC_OBJECT_SUPPORTS_48B_ADDRESS
&&
2030 device
->vma_hi_available
>= bo
->size
) {
2031 uint64_t addr
= util_vma_heap_alloc(&device
->vma_hi
, bo
->size
, 4096);
2033 bo
->offset
= gen_canonical_address(addr
);
2034 assert(addr
== gen_48b_address(bo
->offset
));
2035 device
->vma_hi_available
-= bo
->size
;
2039 if (bo
->offset
== 0 && device
->vma_lo_available
>= bo
->size
) {
2040 uint64_t addr
= util_vma_heap_alloc(&device
->vma_lo
, bo
->size
, 4096);
2042 bo
->offset
= gen_canonical_address(addr
);
2043 assert(addr
== gen_48b_address(bo
->offset
));
2044 device
->vma_lo_available
-= bo
->size
;
2048 pthread_mutex_unlock(&device
->vma_mutex
);
2050 return bo
->offset
!= 0;
2054 anv_vma_free(struct anv_device
*device
, struct anv_bo
*bo
)
2056 if (!(bo
->flags
& EXEC_OBJECT_PINNED
))
2059 const uint64_t addr_48b
= gen_48b_address(bo
->offset
);
2061 pthread_mutex_lock(&device
->vma_mutex
);
2063 if (addr_48b
>= LOW_HEAP_MIN_ADDRESS
&&
2064 addr_48b
<= LOW_HEAP_MAX_ADDRESS
) {
2065 util_vma_heap_free(&device
->vma_lo
, addr_48b
, bo
->size
);
2066 device
->vma_lo_available
+= bo
->size
;
2068 assert(addr_48b
>= HIGH_HEAP_MIN_ADDRESS
&&
2069 addr_48b
<= HIGH_HEAP_MAX_ADDRESS
);
2070 util_vma_heap_free(&device
->vma_hi
, addr_48b
, bo
->size
);
2071 device
->vma_hi_available
+= bo
->size
;
2074 pthread_mutex_unlock(&device
->vma_mutex
);
2080 anv_bo_init_new(struct anv_bo
*bo
, struct anv_device
*device
, uint64_t size
)
2082 uint32_t gem_handle
= anv_gem_create(device
, size
);
2084 return vk_error(VK_ERROR_OUT_OF_DEVICE_MEMORY
);
2086 anv_bo_init(bo
, gem_handle
, size
);
2091 VkResult
anv_AllocateMemory(
2093 const VkMemoryAllocateInfo
* pAllocateInfo
,
2094 const VkAllocationCallbacks
* pAllocator
,
2095 VkDeviceMemory
* pMem
)
2097 ANV_FROM_HANDLE(anv_device
, device
, _device
);
2098 struct anv_physical_device
*pdevice
= &device
->instance
->physicalDevice
;
2099 struct anv_device_memory
*mem
;
2100 VkResult result
= VK_SUCCESS
;
2102 assert(pAllocateInfo
->sType
== VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO
);
2104 /* The Vulkan 1.0.33 spec says "allocationSize must be greater than 0". */
2105 assert(pAllocateInfo
->allocationSize
> 0);
2107 if (pAllocateInfo
->allocationSize
> MAX_MEMORY_ALLOCATION_SIZE
)
2108 return VK_ERROR_OUT_OF_DEVICE_MEMORY
;
2110 /* FINISHME: Fail if allocation request exceeds heap size. */
2112 mem
= vk_alloc2(&device
->alloc
, pAllocator
, sizeof(*mem
), 8,
2113 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
2115 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
2117 assert(pAllocateInfo
->memoryTypeIndex
< pdevice
->memory
.type_count
);
2118 mem
->type
= &pdevice
->memory
.types
[pAllocateInfo
->memoryTypeIndex
];
2122 uint64_t bo_flags
= 0;
2124 assert(mem
->type
->heapIndex
< pdevice
->memory
.heap_count
);
2125 if (pdevice
->memory
.heaps
[mem
->type
->heapIndex
].supports_48bit_addresses
)
2126 bo_flags
|= EXEC_OBJECT_SUPPORTS_48B_ADDRESS
;
2128 const struct wsi_memory_allocate_info
*wsi_info
=
2129 vk_find_struct_const(pAllocateInfo
->pNext
, WSI_MEMORY_ALLOCATE_INFO_MESA
);
2130 if (wsi_info
&& wsi_info
->implicit_sync
) {
2131 /* We need to set the WRITE flag on window system buffers so that GEM
2132 * will know we're writing to them and synchronize uses on other rings
2133 * (eg if the display server uses the blitter ring).
2135 bo_flags
|= EXEC_OBJECT_WRITE
;
2136 } else if (pdevice
->has_exec_async
) {
2137 bo_flags
|= EXEC_OBJECT_ASYNC
;
2140 if (pdevice
->use_softpin
)
2141 bo_flags
|= EXEC_OBJECT_PINNED
;
2143 const VkImportMemoryFdInfoKHR
*fd_info
=
2144 vk_find_struct_const(pAllocateInfo
->pNext
, IMPORT_MEMORY_FD_INFO_KHR
);
2146 /* The Vulkan spec permits handleType to be 0, in which case the struct is
2149 if (fd_info
&& fd_info
->handleType
) {
2150 /* At the moment, we support only the below handle types. */
2151 assert(fd_info
->handleType
==
2152 VK_EXTERNAL_MEMORY_HANDLE_TYPE_OPAQUE_FD_BIT
||
2153 fd_info
->handleType
==
2154 VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_EXT
);
2156 result
= anv_bo_cache_import(device
, &device
->bo_cache
,
2157 fd_info
->fd
, bo_flags
, &mem
->bo
);
2158 if (result
!= VK_SUCCESS
)
2161 VkDeviceSize aligned_alloc_size
=
2162 align_u64(pAllocateInfo
->allocationSize
, 4096);
2164 /* For security purposes, we reject importing the bo if it's smaller
2165 * than the requested allocation size. This prevents a malicious client
2166 * from passing a buffer to a trusted client, lying about the size, and
2167 * telling the trusted client to try and texture from an image that goes
2168 * out-of-bounds. This sort of thing could lead to GPU hangs or worse
2169 * in the trusted client. The trusted client can protect itself against
2170 * this sort of attack but only if it can trust the buffer size.
2172 if (mem
->bo
->size
< aligned_alloc_size
) {
2173 result
= vk_errorf(device
->instance
, device
,
2174 VK_ERROR_INVALID_EXTERNAL_HANDLE_KHR
,
2175 "aligned allocationSize too large for "
2176 "VK_EXTERNAL_MEMORY_HANDLE_TYPE_OPAQUE_FD_BIT_KHR: "
2177 "%"PRIu64
"B > %"PRIu64
"B",
2178 aligned_alloc_size
, mem
->bo
->size
);
2179 anv_bo_cache_release(device
, &device
->bo_cache
, mem
->bo
);
2183 /* From the Vulkan spec:
2185 * "Importing memory from a file descriptor transfers ownership of
2186 * the file descriptor from the application to the Vulkan
2187 * implementation. The application must not perform any operations on
2188 * the file descriptor after a successful import."
2190 * If the import fails, we leave the file descriptor open.
2194 result
= anv_bo_cache_alloc(device
, &device
->bo_cache
,
2195 pAllocateInfo
->allocationSize
, bo_flags
,
2197 if (result
!= VK_SUCCESS
)
2200 const VkMemoryDedicatedAllocateInfoKHR
*dedicated_info
=
2201 vk_find_struct_const(pAllocateInfo
->pNext
, MEMORY_DEDICATED_ALLOCATE_INFO_KHR
);
2202 if (dedicated_info
&& dedicated_info
->image
!= VK_NULL_HANDLE
) {
2203 ANV_FROM_HANDLE(anv_image
, image
, dedicated_info
->image
);
2205 /* Some legacy (non-modifiers) consumers need the tiling to be set on
2206 * the BO. In this case, we have a dedicated allocation.
2208 if (image
->needs_set_tiling
) {
2209 const uint32_t i915_tiling
=
2210 isl_tiling_to_i915_tiling(image
->planes
[0].surface
.isl
.tiling
);
2211 int ret
= anv_gem_set_tiling(device
, mem
->bo
->gem_handle
,
2212 image
->planes
[0].surface
.isl
.row_pitch
,
2215 anv_bo_cache_release(device
, &device
->bo_cache
, mem
->bo
);
2216 return vk_errorf(device
->instance
, NULL
,
2217 VK_ERROR_OUT_OF_DEVICE_MEMORY
,
2218 "failed to set BO tiling: %m");
2224 *pMem
= anv_device_memory_to_handle(mem
);
2229 vk_free2(&device
->alloc
, pAllocator
, mem
);
2234 VkResult
anv_GetMemoryFdKHR(
2236 const VkMemoryGetFdInfoKHR
* pGetFdInfo
,
2239 ANV_FROM_HANDLE(anv_device
, dev
, device_h
);
2240 ANV_FROM_HANDLE(anv_device_memory
, mem
, pGetFdInfo
->memory
);
2242 assert(pGetFdInfo
->sType
== VK_STRUCTURE_TYPE_MEMORY_GET_FD_INFO_KHR
);
2244 assert(pGetFdInfo
->handleType
== VK_EXTERNAL_MEMORY_HANDLE_TYPE_OPAQUE_FD_BIT
||
2245 pGetFdInfo
->handleType
== VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_EXT
);
2247 return anv_bo_cache_export(dev
, &dev
->bo_cache
, mem
->bo
, pFd
);
2250 VkResult
anv_GetMemoryFdPropertiesKHR(
2252 VkExternalMemoryHandleTypeFlagBitsKHR handleType
,
2254 VkMemoryFdPropertiesKHR
* pMemoryFdProperties
)
2256 ANV_FROM_HANDLE(anv_device
, device
, _device
);
2257 struct anv_physical_device
*pdevice
= &device
->instance
->physicalDevice
;
2259 switch (handleType
) {
2260 case VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_EXT
:
2261 /* dma-buf can be imported as any memory type */
2262 pMemoryFdProperties
->memoryTypeBits
=
2263 (1 << pdevice
->memory
.type_count
) - 1;
2267 /* The valid usage section for this function says:
2269 * "handleType must not be one of the handle types defined as
2272 * So opaque handle types fall into the default "unsupported" case.
2274 return vk_error(VK_ERROR_INVALID_EXTERNAL_HANDLE
);
2278 void anv_FreeMemory(
2280 VkDeviceMemory _mem
,
2281 const VkAllocationCallbacks
* pAllocator
)
2283 ANV_FROM_HANDLE(anv_device
, device
, _device
);
2284 ANV_FROM_HANDLE(anv_device_memory
, mem
, _mem
);
2290 anv_UnmapMemory(_device
, _mem
);
2292 anv_bo_cache_release(device
, &device
->bo_cache
, mem
->bo
);
2294 vk_free2(&device
->alloc
, pAllocator
, mem
);
2297 VkResult
anv_MapMemory(
2299 VkDeviceMemory _memory
,
2300 VkDeviceSize offset
,
2302 VkMemoryMapFlags flags
,
2305 ANV_FROM_HANDLE(anv_device
, device
, _device
);
2306 ANV_FROM_HANDLE(anv_device_memory
, mem
, _memory
);
2313 if (size
== VK_WHOLE_SIZE
)
2314 size
= mem
->bo
->size
- offset
;
2316 /* From the Vulkan spec version 1.0.32 docs for MapMemory:
2318 * * If size is not equal to VK_WHOLE_SIZE, size must be greater than 0
2319 * assert(size != 0);
2320 * * If size is not equal to VK_WHOLE_SIZE, size must be less than or
2321 * equal to the size of the memory minus offset
2324 assert(offset
+ size
<= mem
->bo
->size
);
2326 /* FIXME: Is this supposed to be thread safe? Since vkUnmapMemory() only
2327 * takes a VkDeviceMemory pointer, it seems like only one map of the memory
2328 * at a time is valid. We could just mmap up front and return an offset
2329 * pointer here, but that may exhaust virtual memory on 32 bit
2332 uint32_t gem_flags
= 0;
2334 if (!device
->info
.has_llc
&&
2335 (mem
->type
->propertyFlags
& VK_MEMORY_PROPERTY_HOST_COHERENT_BIT
))
2336 gem_flags
|= I915_MMAP_WC
;
2338 /* GEM will fail to map if the offset isn't 4k-aligned. Round down. */
2339 uint64_t map_offset
= offset
& ~4095ull;
2340 assert(offset
>= map_offset
);
2341 uint64_t map_size
= (offset
+ size
) - map_offset
;
2343 /* Let's map whole pages */
2344 map_size
= align_u64(map_size
, 4096);
2346 void *map
= anv_gem_mmap(device
, mem
->bo
->gem_handle
,
2347 map_offset
, map_size
, gem_flags
);
2348 if (map
== MAP_FAILED
)
2349 return vk_error(VK_ERROR_MEMORY_MAP_FAILED
);
2352 mem
->map_size
= map_size
;
2354 *ppData
= mem
->map
+ (offset
- map_offset
);
2359 void anv_UnmapMemory(
2361 VkDeviceMemory _memory
)
2363 ANV_FROM_HANDLE(anv_device_memory
, mem
, _memory
);
2368 anv_gem_munmap(mem
->map
, mem
->map_size
);
2375 clflush_mapped_ranges(struct anv_device
*device
,
2377 const VkMappedMemoryRange
*ranges
)
2379 for (uint32_t i
= 0; i
< count
; i
++) {
2380 ANV_FROM_HANDLE(anv_device_memory
, mem
, ranges
[i
].memory
);
2381 if (ranges
[i
].offset
>= mem
->map_size
)
2384 gen_clflush_range(mem
->map
+ ranges
[i
].offset
,
2385 MIN2(ranges
[i
].size
, mem
->map_size
- ranges
[i
].offset
));
2389 VkResult
anv_FlushMappedMemoryRanges(
2391 uint32_t memoryRangeCount
,
2392 const VkMappedMemoryRange
* pMemoryRanges
)
2394 ANV_FROM_HANDLE(anv_device
, device
, _device
);
2396 if (device
->info
.has_llc
)
2399 /* Make sure the writes we're flushing have landed. */
2400 __builtin_ia32_mfence();
2402 clflush_mapped_ranges(device
, memoryRangeCount
, pMemoryRanges
);
2407 VkResult
anv_InvalidateMappedMemoryRanges(
2409 uint32_t memoryRangeCount
,
2410 const VkMappedMemoryRange
* pMemoryRanges
)
2412 ANV_FROM_HANDLE(anv_device
, device
, _device
);
2414 if (device
->info
.has_llc
)
2417 clflush_mapped_ranges(device
, memoryRangeCount
, pMemoryRanges
);
2419 /* Make sure no reads get moved up above the invalidate. */
2420 __builtin_ia32_mfence();
2425 void anv_GetBufferMemoryRequirements(
2428 VkMemoryRequirements
* pMemoryRequirements
)
2430 ANV_FROM_HANDLE(anv_buffer
, buffer
, _buffer
);
2431 ANV_FROM_HANDLE(anv_device
, device
, _device
);
2432 struct anv_physical_device
*pdevice
= &device
->instance
->physicalDevice
;
2434 /* The Vulkan spec (git aaed022) says:
2436 * memoryTypeBits is a bitfield and contains one bit set for every
2437 * supported memory type for the resource. The bit `1<<i` is set if and
2438 * only if the memory type `i` in the VkPhysicalDeviceMemoryProperties
2439 * structure for the physical device is supported.
2441 uint32_t memory_types
= 0;
2442 for (uint32_t i
= 0; i
< pdevice
->memory
.type_count
; i
++) {
2443 uint32_t valid_usage
= pdevice
->memory
.types
[i
].valid_buffer_usage
;
2444 if ((valid_usage
& buffer
->usage
) == buffer
->usage
)
2445 memory_types
|= (1u << i
);
2448 /* Base alignment requirement of a cache line */
2449 uint32_t alignment
= 16;
2451 /* We need an alignment of 32 for pushing UBOs */
2452 if (buffer
->usage
& VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT
)
2453 alignment
= MAX2(alignment
, 32);
2455 pMemoryRequirements
->size
= buffer
->size
;
2456 pMemoryRequirements
->alignment
= alignment
;
2458 /* Storage and Uniform buffers should have their size aligned to
2459 * 32-bits to avoid boundary checks when last DWord is not complete.
2460 * This would ensure that not internal padding would be needed for
2463 if (device
->robust_buffer_access
&&
2464 (buffer
->usage
& VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT
||
2465 buffer
->usage
& VK_BUFFER_USAGE_STORAGE_BUFFER_BIT
))
2466 pMemoryRequirements
->size
= align_u64(buffer
->size
, 4);
2468 pMemoryRequirements
->memoryTypeBits
= memory_types
;
2471 void anv_GetBufferMemoryRequirements2(
2473 const VkBufferMemoryRequirementsInfo2
* pInfo
,
2474 VkMemoryRequirements2
* pMemoryRequirements
)
2476 anv_GetBufferMemoryRequirements(_device
, pInfo
->buffer
,
2477 &pMemoryRequirements
->memoryRequirements
);
2479 vk_foreach_struct(ext
, pMemoryRequirements
->pNext
) {
2480 switch (ext
->sType
) {
2481 case VK_STRUCTURE_TYPE_MEMORY_DEDICATED_REQUIREMENTS
: {
2482 VkMemoryDedicatedRequirements
*requirements
= (void *)ext
;
2483 requirements
->prefersDedicatedAllocation
= VK_FALSE
;
2484 requirements
->requiresDedicatedAllocation
= VK_FALSE
;
2489 anv_debug_ignored_stype(ext
->sType
);
2495 void anv_GetImageMemoryRequirements(
2498 VkMemoryRequirements
* pMemoryRequirements
)
2500 ANV_FROM_HANDLE(anv_image
, image
, _image
);
2501 ANV_FROM_HANDLE(anv_device
, device
, _device
);
2502 struct anv_physical_device
*pdevice
= &device
->instance
->physicalDevice
;
2504 /* The Vulkan spec (git aaed022) says:
2506 * memoryTypeBits is a bitfield and contains one bit set for every
2507 * supported memory type for the resource. The bit `1<<i` is set if and
2508 * only if the memory type `i` in the VkPhysicalDeviceMemoryProperties
2509 * structure for the physical device is supported.
2511 * All types are currently supported for images.
2513 uint32_t memory_types
= (1ull << pdevice
->memory
.type_count
) - 1;
2515 pMemoryRequirements
->size
= image
->size
;
2516 pMemoryRequirements
->alignment
= image
->alignment
;
2517 pMemoryRequirements
->memoryTypeBits
= memory_types
;
2520 void anv_GetImageMemoryRequirements2(
2522 const VkImageMemoryRequirementsInfo2
* pInfo
,
2523 VkMemoryRequirements2
* pMemoryRequirements
)
2525 ANV_FROM_HANDLE(anv_device
, device
, _device
);
2526 ANV_FROM_HANDLE(anv_image
, image
, pInfo
->image
);
2528 anv_GetImageMemoryRequirements(_device
, pInfo
->image
,
2529 &pMemoryRequirements
->memoryRequirements
);
2531 vk_foreach_struct_const(ext
, pInfo
->pNext
) {
2532 switch (ext
->sType
) {
2533 case VK_STRUCTURE_TYPE_IMAGE_PLANE_MEMORY_REQUIREMENTS_INFO
: {
2534 struct anv_physical_device
*pdevice
= &device
->instance
->physicalDevice
;
2535 const VkImagePlaneMemoryRequirementsInfoKHR
*plane_reqs
=
2536 (const VkImagePlaneMemoryRequirementsInfoKHR
*) ext
;
2537 uint32_t plane
= anv_image_aspect_to_plane(image
->aspects
,
2538 plane_reqs
->planeAspect
);
2540 assert(image
->planes
[plane
].offset
== 0);
2542 /* The Vulkan spec (git aaed022) says:
2544 * memoryTypeBits is a bitfield and contains one bit set for every
2545 * supported memory type for the resource. The bit `1<<i` is set
2546 * if and only if the memory type `i` in the
2547 * VkPhysicalDeviceMemoryProperties structure for the physical
2548 * device is supported.
2550 * All types are currently supported for images.
2552 pMemoryRequirements
->memoryRequirements
.memoryTypeBits
=
2553 (1ull << pdevice
->memory
.type_count
) - 1;
2555 pMemoryRequirements
->memoryRequirements
.size
= image
->planes
[plane
].size
;
2556 pMemoryRequirements
->memoryRequirements
.alignment
=
2557 image
->planes
[plane
].alignment
;
2562 anv_debug_ignored_stype(ext
->sType
);
2567 vk_foreach_struct(ext
, pMemoryRequirements
->pNext
) {
2568 switch (ext
->sType
) {
2569 case VK_STRUCTURE_TYPE_MEMORY_DEDICATED_REQUIREMENTS
: {
2570 VkMemoryDedicatedRequirements
*requirements
= (void *)ext
;
2571 if (image
->needs_set_tiling
) {
2572 /* If we need to set the tiling for external consumers, we need a
2573 * dedicated allocation.
2575 * See also anv_AllocateMemory.
2577 requirements
->prefersDedicatedAllocation
= VK_TRUE
;
2578 requirements
->requiresDedicatedAllocation
= VK_TRUE
;
2580 requirements
->prefersDedicatedAllocation
= VK_FALSE
;
2581 requirements
->requiresDedicatedAllocation
= VK_FALSE
;
2587 anv_debug_ignored_stype(ext
->sType
);
2593 void anv_GetImageSparseMemoryRequirements(
2596 uint32_t* pSparseMemoryRequirementCount
,
2597 VkSparseImageMemoryRequirements
* pSparseMemoryRequirements
)
2599 *pSparseMemoryRequirementCount
= 0;
2602 void anv_GetImageSparseMemoryRequirements2(
2604 const VkImageSparseMemoryRequirementsInfo2
* pInfo
,
2605 uint32_t* pSparseMemoryRequirementCount
,
2606 VkSparseImageMemoryRequirements2
* pSparseMemoryRequirements
)
2608 *pSparseMemoryRequirementCount
= 0;
2611 void anv_GetDeviceMemoryCommitment(
2613 VkDeviceMemory memory
,
2614 VkDeviceSize
* pCommittedMemoryInBytes
)
2616 *pCommittedMemoryInBytes
= 0;
2620 anv_bind_buffer_memory(const VkBindBufferMemoryInfo
*pBindInfo
)
2622 ANV_FROM_HANDLE(anv_device_memory
, mem
, pBindInfo
->memory
);
2623 ANV_FROM_HANDLE(anv_buffer
, buffer
, pBindInfo
->buffer
);
2625 assert(pBindInfo
->sType
== VK_STRUCTURE_TYPE_BIND_BUFFER_MEMORY_INFO
);
2628 assert((buffer
->usage
& mem
->type
->valid_buffer_usage
) == buffer
->usage
);
2629 buffer
->address
= (struct anv_address
) {
2631 .offset
= pBindInfo
->memoryOffset
,
2634 buffer
->address
= ANV_NULL_ADDRESS
;
2638 VkResult
anv_BindBufferMemory(
2641 VkDeviceMemory memory
,
2642 VkDeviceSize memoryOffset
)
2644 anv_bind_buffer_memory(
2645 &(VkBindBufferMemoryInfo
) {
2646 .sType
= VK_STRUCTURE_TYPE_BIND_BUFFER_MEMORY_INFO
,
2649 .memoryOffset
= memoryOffset
,
2655 VkResult
anv_BindBufferMemory2(
2657 uint32_t bindInfoCount
,
2658 const VkBindBufferMemoryInfo
* pBindInfos
)
2660 for (uint32_t i
= 0; i
< bindInfoCount
; i
++)
2661 anv_bind_buffer_memory(&pBindInfos
[i
]);
2666 VkResult
anv_QueueBindSparse(
2668 uint32_t bindInfoCount
,
2669 const VkBindSparseInfo
* pBindInfo
,
2672 ANV_FROM_HANDLE(anv_queue
, queue
, _queue
);
2673 if (unlikely(queue
->device
->lost
))
2674 return VK_ERROR_DEVICE_LOST
;
2676 return vk_error(VK_ERROR_FEATURE_NOT_PRESENT
);
2681 VkResult
anv_CreateEvent(
2683 const VkEventCreateInfo
* pCreateInfo
,
2684 const VkAllocationCallbacks
* pAllocator
,
2687 ANV_FROM_HANDLE(anv_device
, device
, _device
);
2688 struct anv_state state
;
2689 struct anv_event
*event
;
2691 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_EVENT_CREATE_INFO
);
2693 state
= anv_state_pool_alloc(&device
->dynamic_state_pool
,
2696 event
->state
= state
;
2697 event
->semaphore
= VK_EVENT_RESET
;
2699 if (!device
->info
.has_llc
) {
2700 /* Make sure the writes we're flushing have landed. */
2701 __builtin_ia32_mfence();
2702 __builtin_ia32_clflush(event
);
2705 *pEvent
= anv_event_to_handle(event
);
2710 void anv_DestroyEvent(
2713 const VkAllocationCallbacks
* pAllocator
)
2715 ANV_FROM_HANDLE(anv_device
, device
, _device
);
2716 ANV_FROM_HANDLE(anv_event
, event
, _event
);
2721 anv_state_pool_free(&device
->dynamic_state_pool
, event
->state
);
2724 VkResult
anv_GetEventStatus(
2728 ANV_FROM_HANDLE(anv_device
, device
, _device
);
2729 ANV_FROM_HANDLE(anv_event
, event
, _event
);
2731 if (unlikely(device
->lost
))
2732 return VK_ERROR_DEVICE_LOST
;
2734 if (!device
->info
.has_llc
) {
2735 /* Invalidate read cache before reading event written by GPU. */
2736 __builtin_ia32_clflush(event
);
2737 __builtin_ia32_mfence();
2741 return event
->semaphore
;
2744 VkResult
anv_SetEvent(
2748 ANV_FROM_HANDLE(anv_device
, device
, _device
);
2749 ANV_FROM_HANDLE(anv_event
, event
, _event
);
2751 event
->semaphore
= VK_EVENT_SET
;
2753 if (!device
->info
.has_llc
) {
2754 /* Make sure the writes we're flushing have landed. */
2755 __builtin_ia32_mfence();
2756 __builtin_ia32_clflush(event
);
2762 VkResult
anv_ResetEvent(
2766 ANV_FROM_HANDLE(anv_device
, device
, _device
);
2767 ANV_FROM_HANDLE(anv_event
, event
, _event
);
2769 event
->semaphore
= VK_EVENT_RESET
;
2771 if (!device
->info
.has_llc
) {
2772 /* Make sure the writes we're flushing have landed. */
2773 __builtin_ia32_mfence();
2774 __builtin_ia32_clflush(event
);
2782 VkResult
anv_CreateBuffer(
2784 const VkBufferCreateInfo
* pCreateInfo
,
2785 const VkAllocationCallbacks
* pAllocator
,
2788 ANV_FROM_HANDLE(anv_device
, device
, _device
);
2789 struct anv_buffer
*buffer
;
2791 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO
);
2793 buffer
= vk_alloc2(&device
->alloc
, pAllocator
, sizeof(*buffer
), 8,
2794 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
2796 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
2798 buffer
->size
= pCreateInfo
->size
;
2799 buffer
->usage
= pCreateInfo
->usage
;
2800 buffer
->address
= ANV_NULL_ADDRESS
;
2802 *pBuffer
= anv_buffer_to_handle(buffer
);
2807 void anv_DestroyBuffer(
2810 const VkAllocationCallbacks
* pAllocator
)
2812 ANV_FROM_HANDLE(anv_device
, device
, _device
);
2813 ANV_FROM_HANDLE(anv_buffer
, buffer
, _buffer
);
2818 vk_free2(&device
->alloc
, pAllocator
, buffer
);
2822 anv_fill_buffer_surface_state(struct anv_device
*device
, struct anv_state state
,
2823 enum isl_format format
,
2824 struct anv_address address
,
2825 uint32_t range
, uint32_t stride
)
2827 isl_buffer_fill_state(&device
->isl_dev
, state
.map
,
2828 .address
= anv_address_physical(address
),
2829 .mocs
= device
->default_mocs
,
2834 anv_state_flush(device
, state
);
2837 void anv_DestroySampler(
2840 const VkAllocationCallbacks
* pAllocator
)
2842 ANV_FROM_HANDLE(anv_device
, device
, _device
);
2843 ANV_FROM_HANDLE(anv_sampler
, sampler
, _sampler
);
2848 vk_free2(&device
->alloc
, pAllocator
, sampler
);
2851 VkResult
anv_CreateFramebuffer(
2853 const VkFramebufferCreateInfo
* pCreateInfo
,
2854 const VkAllocationCallbacks
* pAllocator
,
2855 VkFramebuffer
* pFramebuffer
)
2857 ANV_FROM_HANDLE(anv_device
, device
, _device
);
2858 struct anv_framebuffer
*framebuffer
;
2860 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO
);
2862 size_t size
= sizeof(*framebuffer
) +
2863 sizeof(struct anv_image_view
*) * pCreateInfo
->attachmentCount
;
2864 framebuffer
= vk_alloc2(&device
->alloc
, pAllocator
, size
, 8,
2865 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
2866 if (framebuffer
== NULL
)
2867 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
2869 framebuffer
->attachment_count
= pCreateInfo
->attachmentCount
;
2870 for (uint32_t i
= 0; i
< pCreateInfo
->attachmentCount
; i
++) {
2871 VkImageView _iview
= pCreateInfo
->pAttachments
[i
];
2872 framebuffer
->attachments
[i
] = anv_image_view_from_handle(_iview
);
2875 framebuffer
->width
= pCreateInfo
->width
;
2876 framebuffer
->height
= pCreateInfo
->height
;
2877 framebuffer
->layers
= pCreateInfo
->layers
;
2879 *pFramebuffer
= anv_framebuffer_to_handle(framebuffer
);
2884 void anv_DestroyFramebuffer(
2887 const VkAllocationCallbacks
* pAllocator
)
2889 ANV_FROM_HANDLE(anv_device
, device
, _device
);
2890 ANV_FROM_HANDLE(anv_framebuffer
, fb
, _fb
);
2895 vk_free2(&device
->alloc
, pAllocator
, fb
);
2898 /* vk_icd.h does not declare this function, so we declare it here to
2899 * suppress Wmissing-prototypes.
2901 PUBLIC VKAPI_ATTR VkResult VKAPI_CALL
2902 vk_icdNegotiateLoaderICDInterfaceVersion(uint32_t* pSupportedVersion
);
2904 PUBLIC VKAPI_ATTR VkResult VKAPI_CALL
2905 vk_icdNegotiateLoaderICDInterfaceVersion(uint32_t* pSupportedVersion
)
2907 /* For the full details on loader interface versioning, see
2908 * <https://github.com/KhronosGroup/Vulkan-LoaderAndValidationLayers/blob/master/loader/LoaderAndLayerInterface.md>.
2909 * What follows is a condensed summary, to help you navigate the large and
2910 * confusing official doc.
2912 * - Loader interface v0 is incompatible with later versions. We don't
2915 * - In loader interface v1:
2916 * - The first ICD entrypoint called by the loader is
2917 * vk_icdGetInstanceProcAddr(). The ICD must statically expose this
2919 * - The ICD must statically expose no other Vulkan symbol unless it is
2920 * linked with -Bsymbolic.
2921 * - Each dispatchable Vulkan handle created by the ICD must be
2922 * a pointer to a struct whose first member is VK_LOADER_DATA. The
2923 * ICD must initialize VK_LOADER_DATA.loadMagic to ICD_LOADER_MAGIC.
2924 * - The loader implements vkCreate{PLATFORM}SurfaceKHR() and
2925 * vkDestroySurfaceKHR(). The ICD must be capable of working with
2926 * such loader-managed surfaces.
2928 * - Loader interface v2 differs from v1 in:
2929 * - The first ICD entrypoint called by the loader is
2930 * vk_icdNegotiateLoaderICDInterfaceVersion(). The ICD must
2931 * statically expose this entrypoint.
2933 * - Loader interface v3 differs from v2 in:
2934 * - The ICD must implement vkCreate{PLATFORM}SurfaceKHR(),
2935 * vkDestroySurfaceKHR(), and other API which uses VKSurfaceKHR,
2936 * because the loader no longer does so.
2938 *pSupportedVersion
= MIN2(*pSupportedVersion
, 3u);