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
) - 2);
287 _mesa_sha1_format(timestamp
, device
->driver_build_sha1
);
289 const uint64_t driver_flags
=
290 brw_get_compiler_config_value(device
->compiler
);
291 device
->disk_cache
= disk_cache_create(renderer
, timestamp
, driver_flags
);
293 device
->disk_cache
= NULL
;
298 anv_physical_device_free_disk_cache(struct anv_physical_device
*device
)
300 #ifdef ENABLE_SHADER_CACHE
301 if (device
->disk_cache
)
302 disk_cache_destroy(device
->disk_cache
);
304 assert(device
->disk_cache
== NULL
);
309 anv_physical_device_init(struct anv_physical_device
*device
,
310 struct anv_instance
*instance
,
311 const char *primary_path
,
318 brw_process_intel_debug_variable();
320 fd
= open(path
, O_RDWR
| O_CLOEXEC
);
322 return vk_error(VK_ERROR_INCOMPATIBLE_DRIVER
);
324 device
->_loader_data
.loaderMagic
= ICD_LOADER_MAGIC
;
325 device
->instance
= instance
;
327 assert(strlen(path
) < ARRAY_SIZE(device
->path
));
328 snprintf(device
->path
, ARRAY_SIZE(device
->path
), "%s", path
);
330 device
->no_hw
= getenv("INTEL_NO_HW") != NULL
;
332 const int pci_id_override
= gen_get_pci_device_id_override();
333 if (pci_id_override
< 0) {
334 device
->chipset_id
= anv_gem_get_param(fd
, I915_PARAM_CHIPSET_ID
);
335 if (!device
->chipset_id
) {
336 result
= vk_error(VK_ERROR_INCOMPATIBLE_DRIVER
);
340 device
->chipset_id
= pci_id_override
;
341 device
->no_hw
= true;
344 device
->name
= gen_get_device_name(device
->chipset_id
);
345 if (!gen_get_device_info(device
->chipset_id
, &device
->info
)) {
346 result
= vk_error(VK_ERROR_INCOMPATIBLE_DRIVER
);
350 if (device
->info
.is_haswell
) {
351 intel_logw("Haswell Vulkan support is incomplete");
352 } else if (device
->info
.gen
== 7 && !device
->info
.is_baytrail
) {
353 intel_logw("Ivy Bridge Vulkan support is incomplete");
354 } else if (device
->info
.gen
== 7 && device
->info
.is_baytrail
) {
355 intel_logw("Bay Trail Vulkan support is incomplete");
356 } else if (device
->info
.gen
>= 8 && device
->info
.gen
<= 10) {
357 /* Gen8-10 fully supported */
358 } else if (device
->info
.gen
== 11) {
359 intel_logw("Vulkan is not yet fully supported on gen11.");
361 result
= vk_errorf(device
->instance
, device
,
362 VK_ERROR_INCOMPATIBLE_DRIVER
,
363 "Vulkan not yet supported on %s", device
->name
);
367 device
->cmd_parser_version
= -1;
368 if (device
->info
.gen
== 7) {
369 device
->cmd_parser_version
=
370 anv_gem_get_param(fd
, I915_PARAM_CMD_PARSER_VERSION
);
371 if (device
->cmd_parser_version
== -1) {
372 result
= vk_errorf(device
->instance
, device
,
373 VK_ERROR_INITIALIZATION_FAILED
,
374 "failed to get command parser version");
379 if (!anv_gem_get_param(fd
, I915_PARAM_HAS_WAIT_TIMEOUT
)) {
380 result
= vk_errorf(device
->instance
, device
,
381 VK_ERROR_INITIALIZATION_FAILED
,
382 "kernel missing gem wait");
386 if (!anv_gem_get_param(fd
, I915_PARAM_HAS_EXECBUF2
)) {
387 result
= vk_errorf(device
->instance
, device
,
388 VK_ERROR_INITIALIZATION_FAILED
,
389 "kernel missing execbuf2");
393 if (!device
->info
.has_llc
&&
394 anv_gem_get_param(fd
, I915_PARAM_MMAP_VERSION
) < 1) {
395 result
= vk_errorf(device
->instance
, device
,
396 VK_ERROR_INITIALIZATION_FAILED
,
397 "kernel missing wc mmap");
401 result
= anv_physical_device_init_heaps(device
, fd
);
402 if (result
!= VK_SUCCESS
)
405 device
->has_exec_async
= anv_gem_get_param(fd
, I915_PARAM_HAS_EXEC_ASYNC
);
406 device
->has_exec_capture
= anv_gem_get_param(fd
, I915_PARAM_HAS_EXEC_CAPTURE
);
407 device
->has_exec_fence
= anv_gem_get_param(fd
, I915_PARAM_HAS_EXEC_FENCE
);
408 device
->has_syncobj
= anv_gem_get_param(fd
, I915_PARAM_HAS_EXEC_FENCE_ARRAY
);
409 device
->has_syncobj_wait
= device
->has_syncobj
&&
410 anv_gem_supports_syncobj_wait(fd
);
411 device
->has_context_priority
= anv_gem_has_context_priority(fd
);
413 device
->use_softpin
= anv_gem_get_param(fd
, I915_PARAM_HAS_EXEC_SOFTPIN
)
414 && device
->supports_48bit_addresses
;
416 device
->has_context_isolation
=
417 anv_gem_get_param(fd
, I915_PARAM_HAS_CONTEXT_ISOLATION
);
419 bool swizzled
= anv_gem_get_bit6_swizzle(fd
, I915_TILING_X
);
421 /* Starting with Gen10, the timestamp frequency of the command streamer may
422 * vary from one part to another. We can query the value from the kernel.
424 if (device
->info
.gen
>= 10) {
425 int timestamp_frequency
=
426 anv_gem_get_param(fd
, I915_PARAM_CS_TIMESTAMP_FREQUENCY
);
428 if (timestamp_frequency
< 0)
429 intel_logw("Kernel 4.16-rc1+ required to properly query CS timestamp frequency");
431 device
->info
.timestamp_frequency
= timestamp_frequency
;
434 /* GENs prior to 8 do not support EU/Subslice info */
435 if (device
->info
.gen
>= 8) {
436 device
->subslice_total
= anv_gem_get_param(fd
, I915_PARAM_SUBSLICE_TOTAL
);
437 device
->eu_total
= anv_gem_get_param(fd
, I915_PARAM_EU_TOTAL
);
439 /* Without this information, we cannot get the right Braswell
440 * brandstrings, and we have to use conservative numbers for GPGPU on
441 * many platforms, but otherwise, things will just work.
443 if (device
->subslice_total
< 1 || device
->eu_total
< 1) {
444 intel_logw("Kernel 4.1 required to properly query GPU properties");
446 } else if (device
->info
.gen
== 7) {
447 device
->subslice_total
= 1 << (device
->info
.gt
- 1);
450 if (device
->info
.is_cherryview
&&
451 device
->subslice_total
> 0 && device
->eu_total
> 0) {
452 /* Logical CS threads = EUs per subslice * num threads per EU */
453 uint32_t max_cs_threads
=
454 device
->eu_total
/ device
->subslice_total
* device
->info
.num_thread_per_eu
;
456 /* Fuse configurations may give more threads than expected, never less. */
457 if (max_cs_threads
> device
->info
.max_cs_threads
)
458 device
->info
.max_cs_threads
= max_cs_threads
;
461 device
->compiler
= brw_compiler_create(NULL
, &device
->info
);
462 if (device
->compiler
== NULL
) {
463 result
= vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
466 device
->compiler
->shader_debug_log
= compiler_debug_log
;
467 device
->compiler
->shader_perf_log
= compiler_perf_log
;
468 device
->compiler
->supports_pull_constants
= false;
469 device
->compiler
->constant_buffer_0_is_relative
=
470 device
->info
.gen
< 8 || !device
->has_context_isolation
;
471 device
->compiler
->supports_shader_constants
= true;
473 isl_device_init(&device
->isl_dev
, &device
->info
, swizzled
);
475 result
= anv_physical_device_init_uuids(device
);
476 if (result
!= VK_SUCCESS
)
479 anv_physical_device_init_disk_cache(device
);
481 if (instance
->enabled_extensions
.KHR_display
) {
482 master_fd
= open(primary_path
, O_RDWR
| O_CLOEXEC
);
483 if (master_fd
>= 0) {
484 /* prod the device with a GETPARAM call which will fail if
485 * we don't have permission to even render on this device
487 if (anv_gem_get_param(master_fd
, I915_PARAM_CHIPSET_ID
) == 0) {
493 device
->master_fd
= master_fd
;
495 result
= anv_init_wsi(device
);
496 if (result
!= VK_SUCCESS
) {
497 ralloc_free(device
->compiler
);
498 anv_physical_device_free_disk_cache(device
);
502 anv_physical_device_get_supported_extensions(device
,
503 &device
->supported_extensions
);
506 device
->local_fd
= fd
;
518 anv_physical_device_finish(struct anv_physical_device
*device
)
520 anv_finish_wsi(device
);
521 anv_physical_device_free_disk_cache(device
);
522 ralloc_free(device
->compiler
);
523 close(device
->local_fd
);
524 if (device
->master_fd
>= 0)
525 close(device
->master_fd
);
529 default_alloc_func(void *pUserData
, size_t size
, size_t align
,
530 VkSystemAllocationScope allocationScope
)
536 default_realloc_func(void *pUserData
, void *pOriginal
, size_t size
,
537 size_t align
, VkSystemAllocationScope allocationScope
)
539 return realloc(pOriginal
, size
);
543 default_free_func(void *pUserData
, void *pMemory
)
548 static const VkAllocationCallbacks default_alloc
= {
550 .pfnAllocation
= default_alloc_func
,
551 .pfnReallocation
= default_realloc_func
,
552 .pfnFree
= default_free_func
,
555 VkResult
anv_EnumerateInstanceExtensionProperties(
556 const char* pLayerName
,
557 uint32_t* pPropertyCount
,
558 VkExtensionProperties
* pProperties
)
560 VK_OUTARRAY_MAKE(out
, pProperties
, pPropertyCount
);
562 for (int i
= 0; i
< ANV_INSTANCE_EXTENSION_COUNT
; i
++) {
563 if (anv_instance_extensions_supported
.extensions
[i
]) {
564 vk_outarray_append(&out
, prop
) {
565 *prop
= anv_instance_extensions
[i
];
570 return vk_outarray_status(&out
);
573 VkResult
anv_CreateInstance(
574 const VkInstanceCreateInfo
* pCreateInfo
,
575 const VkAllocationCallbacks
* pAllocator
,
576 VkInstance
* pInstance
)
578 struct anv_instance
*instance
;
581 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO
);
583 struct anv_instance_extension_table enabled_extensions
= {};
584 for (uint32_t i
= 0; i
< pCreateInfo
->enabledExtensionCount
; i
++) {
586 for (idx
= 0; idx
< ANV_INSTANCE_EXTENSION_COUNT
; idx
++) {
587 if (strcmp(pCreateInfo
->ppEnabledExtensionNames
[i
],
588 anv_instance_extensions
[idx
].extensionName
) == 0)
592 if (idx
>= ANV_INSTANCE_EXTENSION_COUNT
)
593 return vk_error(VK_ERROR_EXTENSION_NOT_PRESENT
);
595 if (!anv_instance_extensions_supported
.extensions
[idx
])
596 return vk_error(VK_ERROR_EXTENSION_NOT_PRESENT
);
598 enabled_extensions
.extensions
[idx
] = true;
601 instance
= vk_alloc2(&default_alloc
, pAllocator
, sizeof(*instance
), 8,
602 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE
);
604 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
606 instance
->_loader_data
.loaderMagic
= ICD_LOADER_MAGIC
;
609 instance
->alloc
= *pAllocator
;
611 instance
->alloc
= default_alloc
;
613 if (pCreateInfo
->pApplicationInfo
&&
614 pCreateInfo
->pApplicationInfo
->apiVersion
!= 0) {
615 instance
->apiVersion
= pCreateInfo
->pApplicationInfo
->apiVersion
;
617 anv_EnumerateInstanceVersion(&instance
->apiVersion
);
620 instance
->enabled_extensions
= enabled_extensions
;
622 for (unsigned i
= 0; i
< ARRAY_SIZE(instance
->dispatch
.entrypoints
); i
++) {
623 /* Vulkan requires that entrypoints for extensions which have not been
624 * enabled must not be advertised.
626 if (!anv_entrypoint_is_enabled(i
, instance
->apiVersion
,
627 &instance
->enabled_extensions
, NULL
)) {
628 instance
->dispatch
.entrypoints
[i
] = NULL
;
629 } else if (anv_dispatch_table
.entrypoints
[i
] != NULL
) {
630 instance
->dispatch
.entrypoints
[i
] = anv_dispatch_table
.entrypoints
[i
];
632 instance
->dispatch
.entrypoints
[i
] =
633 anv_tramp_dispatch_table
.entrypoints
[i
];
637 instance
->physicalDeviceCount
= -1;
639 result
= vk_debug_report_instance_init(&instance
->debug_report_callbacks
);
640 if (result
!= VK_SUCCESS
) {
641 vk_free2(&default_alloc
, pAllocator
, instance
);
642 return vk_error(result
);
645 instance
->pipeline_cache_enabled
=
646 env_var_as_boolean("ANV_ENABLE_PIPELINE_CACHE", true);
650 VG(VALGRIND_CREATE_MEMPOOL(instance
, 0, false));
652 *pInstance
= anv_instance_to_handle(instance
);
657 void anv_DestroyInstance(
658 VkInstance _instance
,
659 const VkAllocationCallbacks
* pAllocator
)
661 ANV_FROM_HANDLE(anv_instance
, instance
, _instance
);
666 if (instance
->physicalDeviceCount
> 0) {
667 /* We support at most one physical device. */
668 assert(instance
->physicalDeviceCount
== 1);
669 anv_physical_device_finish(&instance
->physicalDevice
);
672 VG(VALGRIND_DESTROY_MEMPOOL(instance
));
674 vk_debug_report_instance_destroy(&instance
->debug_report_callbacks
);
678 vk_free(&instance
->alloc
, instance
);
682 anv_enumerate_devices(struct anv_instance
*instance
)
684 /* TODO: Check for more devices ? */
685 drmDevicePtr devices
[8];
686 VkResult result
= VK_ERROR_INCOMPATIBLE_DRIVER
;
689 instance
->physicalDeviceCount
= 0;
691 max_devices
= drmGetDevices2(0, devices
, ARRAY_SIZE(devices
));
693 return VK_ERROR_INCOMPATIBLE_DRIVER
;
695 for (unsigned i
= 0; i
< (unsigned)max_devices
; i
++) {
696 if (devices
[i
]->available_nodes
& 1 << DRM_NODE_RENDER
&&
697 devices
[i
]->bustype
== DRM_BUS_PCI
&&
698 devices
[i
]->deviceinfo
.pci
->vendor_id
== 0x8086) {
700 result
= anv_physical_device_init(&instance
->physicalDevice
,
702 devices
[i
]->nodes
[DRM_NODE_PRIMARY
],
703 devices
[i
]->nodes
[DRM_NODE_RENDER
]);
704 if (result
!= VK_ERROR_INCOMPATIBLE_DRIVER
)
708 drmFreeDevices(devices
, max_devices
);
710 if (result
== VK_SUCCESS
)
711 instance
->physicalDeviceCount
= 1;
717 anv_instance_ensure_physical_device(struct anv_instance
*instance
)
719 if (instance
->physicalDeviceCount
< 0) {
720 VkResult result
= anv_enumerate_devices(instance
);
721 if (result
!= VK_SUCCESS
&&
722 result
!= VK_ERROR_INCOMPATIBLE_DRIVER
)
729 VkResult
anv_EnumeratePhysicalDevices(
730 VkInstance _instance
,
731 uint32_t* pPhysicalDeviceCount
,
732 VkPhysicalDevice
* pPhysicalDevices
)
734 ANV_FROM_HANDLE(anv_instance
, instance
, _instance
);
735 VK_OUTARRAY_MAKE(out
, pPhysicalDevices
, pPhysicalDeviceCount
);
737 VkResult result
= anv_instance_ensure_physical_device(instance
);
738 if (result
!= VK_SUCCESS
)
741 if (instance
->physicalDeviceCount
== 0)
744 assert(instance
->physicalDeviceCount
== 1);
745 vk_outarray_append(&out
, i
) {
746 *i
= anv_physical_device_to_handle(&instance
->physicalDevice
);
749 return vk_outarray_status(&out
);
752 VkResult
anv_EnumeratePhysicalDeviceGroups(
753 VkInstance _instance
,
754 uint32_t* pPhysicalDeviceGroupCount
,
755 VkPhysicalDeviceGroupProperties
* pPhysicalDeviceGroupProperties
)
757 ANV_FROM_HANDLE(anv_instance
, instance
, _instance
);
758 VK_OUTARRAY_MAKE(out
, pPhysicalDeviceGroupProperties
,
759 pPhysicalDeviceGroupCount
);
761 VkResult result
= anv_instance_ensure_physical_device(instance
);
762 if (result
!= VK_SUCCESS
)
765 if (instance
->physicalDeviceCount
== 0)
768 assert(instance
->physicalDeviceCount
== 1);
770 vk_outarray_append(&out
, p
) {
771 p
->physicalDeviceCount
= 1;
772 memset(p
->physicalDevices
, 0, sizeof(p
->physicalDevices
));
773 p
->physicalDevices
[0] =
774 anv_physical_device_to_handle(&instance
->physicalDevice
);
775 p
->subsetAllocation
= VK_FALSE
;
777 vk_foreach_struct(ext
, p
->pNext
)
778 anv_debug_ignored_stype(ext
->sType
);
781 return vk_outarray_status(&out
);
784 void anv_GetPhysicalDeviceFeatures(
785 VkPhysicalDevice physicalDevice
,
786 VkPhysicalDeviceFeatures
* pFeatures
)
788 ANV_FROM_HANDLE(anv_physical_device
, pdevice
, physicalDevice
);
790 *pFeatures
= (VkPhysicalDeviceFeatures
) {
791 .robustBufferAccess
= true,
792 .fullDrawIndexUint32
= true,
793 .imageCubeArray
= true,
794 .independentBlend
= true,
795 .geometryShader
= true,
796 .tessellationShader
= true,
797 .sampleRateShading
= true,
798 .dualSrcBlend
= true,
800 .multiDrawIndirect
= true,
801 .drawIndirectFirstInstance
= true,
803 .depthBiasClamp
= true,
804 .fillModeNonSolid
= true,
805 .depthBounds
= false,
809 .multiViewport
= true,
810 .samplerAnisotropy
= true,
811 .textureCompressionETC2
= pdevice
->info
.gen
>= 8 ||
812 pdevice
->info
.is_baytrail
,
813 .textureCompressionASTC_LDR
= pdevice
->info
.gen
>= 9, /* FINISHME CHV */
814 .textureCompressionBC
= true,
815 .occlusionQueryPrecise
= true,
816 .pipelineStatisticsQuery
= true,
817 .fragmentStoresAndAtomics
= true,
818 .shaderTessellationAndGeometryPointSize
= true,
819 .shaderImageGatherExtended
= true,
820 .shaderStorageImageExtendedFormats
= true,
821 .shaderStorageImageMultisample
= false,
822 .shaderStorageImageReadWithoutFormat
= false,
823 .shaderStorageImageWriteWithoutFormat
= true,
824 .shaderUniformBufferArrayDynamicIndexing
= true,
825 .shaderSampledImageArrayDynamicIndexing
= true,
826 .shaderStorageBufferArrayDynamicIndexing
= true,
827 .shaderStorageImageArrayDynamicIndexing
= true,
828 .shaderClipDistance
= true,
829 .shaderCullDistance
= true,
830 .shaderFloat64
= pdevice
->info
.gen
>= 8 &&
831 pdevice
->info
.has_64bit_types
,
832 .shaderInt64
= pdevice
->info
.gen
>= 8 &&
833 pdevice
->info
.has_64bit_types
,
834 .shaderInt16
= pdevice
->info
.gen
>= 8,
835 .shaderResourceMinLod
= false,
836 .variableMultisampleRate
= true,
837 .inheritedQueries
= true,
840 /* We can't do image stores in vec4 shaders */
841 pFeatures
->vertexPipelineStoresAndAtomics
=
842 pdevice
->compiler
->scalar_stage
[MESA_SHADER_VERTEX
] &&
843 pdevice
->compiler
->scalar_stage
[MESA_SHADER_GEOMETRY
];
846 void anv_GetPhysicalDeviceFeatures2(
847 VkPhysicalDevice physicalDevice
,
848 VkPhysicalDeviceFeatures2
* pFeatures
)
850 anv_GetPhysicalDeviceFeatures(physicalDevice
, &pFeatures
->features
);
852 vk_foreach_struct(ext
, pFeatures
->pNext
) {
853 switch (ext
->sType
) {
854 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PROTECTED_MEMORY_FEATURES
: {
855 VkPhysicalDeviceProtectedMemoryFeatures
*features
= (void *)ext
;
856 features
->protectedMemory
= VK_FALSE
;
860 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_MULTIVIEW_FEATURES
: {
861 VkPhysicalDeviceMultiviewFeatures
*features
=
862 (VkPhysicalDeviceMultiviewFeatures
*)ext
;
863 features
->multiview
= true;
864 features
->multiviewGeometryShader
= true;
865 features
->multiviewTessellationShader
= true;
869 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VARIABLE_POINTER_FEATURES
: {
870 VkPhysicalDeviceVariablePointerFeatures
*features
= (void *)ext
;
871 features
->variablePointersStorageBuffer
= true;
872 features
->variablePointers
= true;
876 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SAMPLER_YCBCR_CONVERSION_FEATURES
: {
877 VkPhysicalDeviceSamplerYcbcrConversionFeatures
*features
=
878 (VkPhysicalDeviceSamplerYcbcrConversionFeatures
*) ext
;
879 features
->samplerYcbcrConversion
= true;
883 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SHADER_DRAW_PARAMETER_FEATURES
: {
884 VkPhysicalDeviceShaderDrawParameterFeatures
*features
= (void *)ext
;
885 features
->shaderDrawParameters
= true;
889 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_16BIT_STORAGE_FEATURES_KHR
: {
890 VkPhysicalDevice16BitStorageFeaturesKHR
*features
=
891 (VkPhysicalDevice16BitStorageFeaturesKHR
*)ext
;
892 ANV_FROM_HANDLE(anv_physical_device
, pdevice
, physicalDevice
);
894 features
->storageBuffer16BitAccess
= pdevice
->info
.gen
>= 8;
895 features
->uniformAndStorageBuffer16BitAccess
= pdevice
->info
.gen
>= 8;
896 features
->storagePushConstant16
= pdevice
->info
.gen
>= 8;
897 features
->storageInputOutput16
= false;
901 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_8BIT_STORAGE_FEATURES_KHR
: {
902 VkPhysicalDevice8BitStorageFeaturesKHR
*features
=
903 (VkPhysicalDevice8BitStorageFeaturesKHR
*)ext
;
904 ANV_FROM_HANDLE(anv_physical_device
, pdevice
, physicalDevice
);
906 features
->storageBuffer8BitAccess
= pdevice
->info
.gen
>= 8;
907 features
->uniformAndStorageBuffer8BitAccess
= pdevice
->info
.gen
>= 8;
908 features
->storagePushConstant8
= pdevice
->info
.gen
>= 8;
913 anv_debug_ignored_stype(ext
->sType
);
919 void anv_GetPhysicalDeviceProperties(
920 VkPhysicalDevice physicalDevice
,
921 VkPhysicalDeviceProperties
* pProperties
)
923 ANV_FROM_HANDLE(anv_physical_device
, pdevice
, physicalDevice
);
924 const struct gen_device_info
*devinfo
= &pdevice
->info
;
926 /* See assertions made when programming the buffer surface state. */
927 const uint32_t max_raw_buffer_sz
= devinfo
->gen
>= 7 ?
928 (1ul << 30) : (1ul << 27);
930 const uint32_t max_samplers
= (devinfo
->gen
>= 8 || devinfo
->is_haswell
) ?
933 VkSampleCountFlags sample_counts
=
934 isl_device_get_sample_counts(&pdevice
->isl_dev
);
936 VkPhysicalDeviceLimits limits
= {
937 .maxImageDimension1D
= (1 << 14),
938 .maxImageDimension2D
= (1 << 14),
939 .maxImageDimension3D
= (1 << 11),
940 .maxImageDimensionCube
= (1 << 14),
941 .maxImageArrayLayers
= (1 << 11),
942 .maxTexelBufferElements
= 128 * 1024 * 1024,
943 .maxUniformBufferRange
= (1ul << 27),
944 .maxStorageBufferRange
= max_raw_buffer_sz
,
945 .maxPushConstantsSize
= MAX_PUSH_CONSTANTS_SIZE
,
946 .maxMemoryAllocationCount
= UINT32_MAX
,
947 .maxSamplerAllocationCount
= 64 * 1024,
948 .bufferImageGranularity
= 64, /* A cache line */
949 .sparseAddressSpaceSize
= 0,
950 .maxBoundDescriptorSets
= MAX_SETS
,
951 .maxPerStageDescriptorSamplers
= max_samplers
,
952 .maxPerStageDescriptorUniformBuffers
= 64,
953 .maxPerStageDescriptorStorageBuffers
= 64,
954 .maxPerStageDescriptorSampledImages
= max_samplers
,
955 .maxPerStageDescriptorStorageImages
= 64,
956 .maxPerStageDescriptorInputAttachments
= 64,
957 .maxPerStageResources
= 250,
958 .maxDescriptorSetSamplers
= 6 * max_samplers
, /* number of stages * maxPerStageDescriptorSamplers */
959 .maxDescriptorSetUniformBuffers
= 6 * 64, /* number of stages * maxPerStageDescriptorUniformBuffers */
960 .maxDescriptorSetUniformBuffersDynamic
= MAX_DYNAMIC_BUFFERS
/ 2,
961 .maxDescriptorSetStorageBuffers
= 6 * 64, /* number of stages * maxPerStageDescriptorStorageBuffers */
962 .maxDescriptorSetStorageBuffersDynamic
= MAX_DYNAMIC_BUFFERS
/ 2,
963 .maxDescriptorSetSampledImages
= 6 * max_samplers
, /* number of stages * maxPerStageDescriptorSampledImages */
964 .maxDescriptorSetStorageImages
= 6 * 64, /* number of stages * maxPerStageDescriptorStorageImages */
965 .maxDescriptorSetInputAttachments
= 256,
966 .maxVertexInputAttributes
= MAX_VBS
,
967 .maxVertexInputBindings
= MAX_VBS
,
968 .maxVertexInputAttributeOffset
= 2047,
969 .maxVertexInputBindingStride
= 2048,
970 .maxVertexOutputComponents
= 128,
971 .maxTessellationGenerationLevel
= 64,
972 .maxTessellationPatchSize
= 32,
973 .maxTessellationControlPerVertexInputComponents
= 128,
974 .maxTessellationControlPerVertexOutputComponents
= 128,
975 .maxTessellationControlPerPatchOutputComponents
= 128,
976 .maxTessellationControlTotalOutputComponents
= 2048,
977 .maxTessellationEvaluationInputComponents
= 128,
978 .maxTessellationEvaluationOutputComponents
= 128,
979 .maxGeometryShaderInvocations
= 32,
980 .maxGeometryInputComponents
= 64,
981 .maxGeometryOutputComponents
= 128,
982 .maxGeometryOutputVertices
= 256,
983 .maxGeometryTotalOutputComponents
= 1024,
984 .maxFragmentInputComponents
= 112, /* 128 components - (POS, PSIZ, CLIP_DIST0, CLIP_DIST1) */
985 .maxFragmentOutputAttachments
= 8,
986 .maxFragmentDualSrcAttachments
= 1,
987 .maxFragmentCombinedOutputResources
= 8,
988 .maxComputeSharedMemorySize
= 32768,
989 .maxComputeWorkGroupCount
= { 65535, 65535, 65535 },
990 .maxComputeWorkGroupInvocations
= 16 * devinfo
->max_cs_threads
,
991 .maxComputeWorkGroupSize
= {
992 16 * devinfo
->max_cs_threads
,
993 16 * devinfo
->max_cs_threads
,
994 16 * devinfo
->max_cs_threads
,
996 .subPixelPrecisionBits
= 4 /* FIXME */,
997 .subTexelPrecisionBits
= 4 /* FIXME */,
998 .mipmapPrecisionBits
= 4 /* FIXME */,
999 .maxDrawIndexedIndexValue
= UINT32_MAX
,
1000 .maxDrawIndirectCount
= UINT32_MAX
,
1001 .maxSamplerLodBias
= 16,
1002 .maxSamplerAnisotropy
= 16,
1003 .maxViewports
= MAX_VIEWPORTS
,
1004 .maxViewportDimensions
= { (1 << 14), (1 << 14) },
1005 .viewportBoundsRange
= { INT16_MIN
, INT16_MAX
},
1006 .viewportSubPixelBits
= 13, /* We take a float? */
1007 .minMemoryMapAlignment
= 4096, /* A page */
1008 .minTexelBufferOffsetAlignment
= 1,
1009 /* We need 16 for UBO block reads to work and 32 for push UBOs */
1010 .minUniformBufferOffsetAlignment
= 32,
1011 .minStorageBufferOffsetAlignment
= 4,
1012 .minTexelOffset
= -8,
1013 .maxTexelOffset
= 7,
1014 .minTexelGatherOffset
= -32,
1015 .maxTexelGatherOffset
= 31,
1016 .minInterpolationOffset
= -0.5,
1017 .maxInterpolationOffset
= 0.4375,
1018 .subPixelInterpolationOffsetBits
= 4,
1019 .maxFramebufferWidth
= (1 << 14),
1020 .maxFramebufferHeight
= (1 << 14),
1021 .maxFramebufferLayers
= (1 << 11),
1022 .framebufferColorSampleCounts
= sample_counts
,
1023 .framebufferDepthSampleCounts
= sample_counts
,
1024 .framebufferStencilSampleCounts
= sample_counts
,
1025 .framebufferNoAttachmentsSampleCounts
= sample_counts
,
1026 .maxColorAttachments
= MAX_RTS
,
1027 .sampledImageColorSampleCounts
= sample_counts
,
1028 .sampledImageIntegerSampleCounts
= VK_SAMPLE_COUNT_1_BIT
,
1029 .sampledImageDepthSampleCounts
= sample_counts
,
1030 .sampledImageStencilSampleCounts
= sample_counts
,
1031 .storageImageSampleCounts
= VK_SAMPLE_COUNT_1_BIT
,
1032 .maxSampleMaskWords
= 1,
1033 .timestampComputeAndGraphics
= false,
1034 .timestampPeriod
= 1000000000.0 / devinfo
->timestamp_frequency
,
1035 .maxClipDistances
= 8,
1036 .maxCullDistances
= 8,
1037 .maxCombinedClipAndCullDistances
= 8,
1038 .discreteQueuePriorities
= 1,
1039 .pointSizeRange
= { 0.125, 255.875 },
1040 .lineWidthRange
= { 0.0, 7.9921875 },
1041 .pointSizeGranularity
= (1.0 / 8.0),
1042 .lineWidthGranularity
= (1.0 / 128.0),
1043 .strictLines
= false, /* FINISHME */
1044 .standardSampleLocations
= true,
1045 .optimalBufferCopyOffsetAlignment
= 128,
1046 .optimalBufferCopyRowPitchAlignment
= 128,
1047 .nonCoherentAtomSize
= 64,
1050 *pProperties
= (VkPhysicalDeviceProperties
) {
1051 .apiVersion
= anv_physical_device_api_version(pdevice
),
1052 .driverVersion
= vk_get_driver_version(),
1054 .deviceID
= pdevice
->chipset_id
,
1055 .deviceType
= VK_PHYSICAL_DEVICE_TYPE_INTEGRATED_GPU
,
1057 .sparseProperties
= {0}, /* Broadwell doesn't do sparse. */
1060 snprintf(pProperties
->deviceName
, sizeof(pProperties
->deviceName
),
1061 "%s", pdevice
->name
);
1062 memcpy(pProperties
->pipelineCacheUUID
,
1063 pdevice
->pipeline_cache_uuid
, VK_UUID_SIZE
);
1066 void anv_GetPhysicalDeviceProperties2(
1067 VkPhysicalDevice physicalDevice
,
1068 VkPhysicalDeviceProperties2
* pProperties
)
1070 ANV_FROM_HANDLE(anv_physical_device
, pdevice
, physicalDevice
);
1072 anv_GetPhysicalDeviceProperties(physicalDevice
, &pProperties
->properties
);
1074 vk_foreach_struct(ext
, pProperties
->pNext
) {
1075 switch (ext
->sType
) {
1076 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PUSH_DESCRIPTOR_PROPERTIES_KHR
: {
1077 VkPhysicalDevicePushDescriptorPropertiesKHR
*properties
=
1078 (VkPhysicalDevicePushDescriptorPropertiesKHR
*) ext
;
1080 properties
->maxPushDescriptors
= MAX_PUSH_DESCRIPTORS
;
1084 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_ID_PROPERTIES
: {
1085 VkPhysicalDeviceIDProperties
*id_props
=
1086 (VkPhysicalDeviceIDProperties
*)ext
;
1087 memcpy(id_props
->deviceUUID
, pdevice
->device_uuid
, VK_UUID_SIZE
);
1088 memcpy(id_props
->driverUUID
, pdevice
->driver_uuid
, VK_UUID_SIZE
);
1089 /* The LUID is for Windows. */
1090 id_props
->deviceLUIDValid
= false;
1094 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_MAINTENANCE_3_PROPERTIES
: {
1095 VkPhysicalDeviceMaintenance3Properties
*props
=
1096 (VkPhysicalDeviceMaintenance3Properties
*)ext
;
1097 /* This value doesn't matter for us today as our per-stage
1098 * descriptors are the real limit.
1100 props
->maxPerSetDescriptors
= 1024;
1101 props
->maxMemoryAllocationSize
= MAX_MEMORY_ALLOCATION_SIZE
;
1105 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_MULTIVIEW_PROPERTIES
: {
1106 VkPhysicalDeviceMultiviewProperties
*properties
=
1107 (VkPhysicalDeviceMultiviewProperties
*)ext
;
1108 properties
->maxMultiviewViewCount
= 16;
1109 properties
->maxMultiviewInstanceIndex
= UINT32_MAX
/ 16;
1113 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_POINT_CLIPPING_PROPERTIES
: {
1114 VkPhysicalDevicePointClippingProperties
*properties
=
1115 (VkPhysicalDevicePointClippingProperties
*) ext
;
1116 properties
->pointClippingBehavior
= VK_POINT_CLIPPING_BEHAVIOR_ALL_CLIP_PLANES
;
1117 anv_finishme("Implement pop-free point clipping");
1121 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SUBGROUP_PROPERTIES
: {
1122 VkPhysicalDeviceSubgroupProperties
*properties
= (void *)ext
;
1124 properties
->subgroupSize
= BRW_SUBGROUP_SIZE
;
1126 VkShaderStageFlags scalar_stages
= 0;
1127 for (unsigned stage
= 0; stage
< MESA_SHADER_STAGES
; stage
++) {
1128 if (pdevice
->compiler
->scalar_stage
[stage
])
1129 scalar_stages
|= mesa_to_vk_shader_stage(stage
);
1131 properties
->supportedStages
= scalar_stages
;
1133 properties
->supportedOperations
= VK_SUBGROUP_FEATURE_BASIC_BIT
|
1134 VK_SUBGROUP_FEATURE_VOTE_BIT
|
1135 VK_SUBGROUP_FEATURE_ARITHMETIC_BIT
|
1136 VK_SUBGROUP_FEATURE_BALLOT_BIT
|
1137 VK_SUBGROUP_FEATURE_SHUFFLE_BIT
|
1138 VK_SUBGROUP_FEATURE_SHUFFLE_RELATIVE_BIT
|
1139 VK_SUBGROUP_FEATURE_CLUSTERED_BIT
|
1140 VK_SUBGROUP_FEATURE_QUAD_BIT
;
1141 properties
->quadOperationsInAllStages
= VK_TRUE
;
1145 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VERTEX_ATTRIBUTE_DIVISOR_PROPERTIES_EXT
: {
1146 VkPhysicalDeviceVertexAttributeDivisorPropertiesEXT
*props
=
1147 (VkPhysicalDeviceVertexAttributeDivisorPropertiesEXT
*)ext
;
1148 /* We have to restrict this a bit for multiview */
1149 props
->maxVertexAttribDivisor
= UINT32_MAX
/ 16;
1154 anv_debug_ignored_stype(ext
->sType
);
1160 /* We support exactly one queue family. */
1161 static const VkQueueFamilyProperties
1162 anv_queue_family_properties
= {
1163 .queueFlags
= VK_QUEUE_GRAPHICS_BIT
|
1164 VK_QUEUE_COMPUTE_BIT
|
1165 VK_QUEUE_TRANSFER_BIT
,
1167 .timestampValidBits
= 36, /* XXX: Real value here */
1168 .minImageTransferGranularity
= { 1, 1, 1 },
1171 void anv_GetPhysicalDeviceQueueFamilyProperties(
1172 VkPhysicalDevice physicalDevice
,
1174 VkQueueFamilyProperties
* pQueueFamilyProperties
)
1176 VK_OUTARRAY_MAKE(out
, pQueueFamilyProperties
, pCount
);
1178 vk_outarray_append(&out
, p
) {
1179 *p
= anv_queue_family_properties
;
1183 void anv_GetPhysicalDeviceQueueFamilyProperties2(
1184 VkPhysicalDevice physicalDevice
,
1185 uint32_t* pQueueFamilyPropertyCount
,
1186 VkQueueFamilyProperties2
* pQueueFamilyProperties
)
1189 VK_OUTARRAY_MAKE(out
, pQueueFamilyProperties
, pQueueFamilyPropertyCount
);
1191 vk_outarray_append(&out
, p
) {
1192 p
->queueFamilyProperties
= anv_queue_family_properties
;
1194 vk_foreach_struct(s
, p
->pNext
) {
1195 anv_debug_ignored_stype(s
->sType
);
1200 void anv_GetPhysicalDeviceMemoryProperties(
1201 VkPhysicalDevice physicalDevice
,
1202 VkPhysicalDeviceMemoryProperties
* pMemoryProperties
)
1204 ANV_FROM_HANDLE(anv_physical_device
, physical_device
, physicalDevice
);
1206 pMemoryProperties
->memoryTypeCount
= physical_device
->memory
.type_count
;
1207 for (uint32_t i
= 0; i
< physical_device
->memory
.type_count
; i
++) {
1208 pMemoryProperties
->memoryTypes
[i
] = (VkMemoryType
) {
1209 .propertyFlags
= physical_device
->memory
.types
[i
].propertyFlags
,
1210 .heapIndex
= physical_device
->memory
.types
[i
].heapIndex
,
1214 pMemoryProperties
->memoryHeapCount
= physical_device
->memory
.heap_count
;
1215 for (uint32_t i
= 0; i
< physical_device
->memory
.heap_count
; i
++) {
1216 pMemoryProperties
->memoryHeaps
[i
] = (VkMemoryHeap
) {
1217 .size
= physical_device
->memory
.heaps
[i
].size
,
1218 .flags
= physical_device
->memory
.heaps
[i
].flags
,
1223 void anv_GetPhysicalDeviceMemoryProperties2(
1224 VkPhysicalDevice physicalDevice
,
1225 VkPhysicalDeviceMemoryProperties2
* pMemoryProperties
)
1227 anv_GetPhysicalDeviceMemoryProperties(physicalDevice
,
1228 &pMemoryProperties
->memoryProperties
);
1230 vk_foreach_struct(ext
, pMemoryProperties
->pNext
) {
1231 switch (ext
->sType
) {
1233 anv_debug_ignored_stype(ext
->sType
);
1240 anv_GetDeviceGroupPeerMemoryFeatures(
1243 uint32_t localDeviceIndex
,
1244 uint32_t remoteDeviceIndex
,
1245 VkPeerMemoryFeatureFlags
* pPeerMemoryFeatures
)
1247 assert(localDeviceIndex
== 0 && remoteDeviceIndex
== 0);
1248 *pPeerMemoryFeatures
= VK_PEER_MEMORY_FEATURE_COPY_SRC_BIT
|
1249 VK_PEER_MEMORY_FEATURE_COPY_DST_BIT
|
1250 VK_PEER_MEMORY_FEATURE_GENERIC_SRC_BIT
|
1251 VK_PEER_MEMORY_FEATURE_GENERIC_DST_BIT
;
1254 PFN_vkVoidFunction
anv_GetInstanceProcAddr(
1255 VkInstance _instance
,
1258 ANV_FROM_HANDLE(anv_instance
, instance
, _instance
);
1260 /* The Vulkan 1.0 spec for vkGetInstanceProcAddr has a table of exactly
1261 * when we have to return valid function pointers, NULL, or it's left
1262 * undefined. See the table for exact details.
1267 #define LOOKUP_ANV_ENTRYPOINT(entrypoint) \
1268 if (strcmp(pName, "vk" #entrypoint) == 0) \
1269 return (PFN_vkVoidFunction)anv_##entrypoint
1271 LOOKUP_ANV_ENTRYPOINT(EnumerateInstanceExtensionProperties
);
1272 LOOKUP_ANV_ENTRYPOINT(EnumerateInstanceLayerProperties
);
1273 LOOKUP_ANV_ENTRYPOINT(EnumerateInstanceVersion
);
1274 LOOKUP_ANV_ENTRYPOINT(CreateInstance
);
1276 #undef LOOKUP_ANV_ENTRYPOINT
1278 if (instance
== NULL
)
1281 int idx
= anv_get_entrypoint_index(pName
);
1285 return instance
->dispatch
.entrypoints
[idx
];
1288 /* With version 1+ of the loader interface the ICD should expose
1289 * vk_icdGetInstanceProcAddr to work around certain LD_PRELOAD issues seen in apps.
1292 VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL
vk_icdGetInstanceProcAddr(
1293 VkInstance instance
,
1297 VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL
vk_icdGetInstanceProcAddr(
1298 VkInstance instance
,
1301 return anv_GetInstanceProcAddr(instance
, pName
);
1304 PFN_vkVoidFunction
anv_GetDeviceProcAddr(
1308 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1310 if (!device
|| !pName
)
1313 int idx
= anv_get_entrypoint_index(pName
);
1317 return device
->dispatch
.entrypoints
[idx
];
1321 anv_CreateDebugReportCallbackEXT(VkInstance _instance
,
1322 const VkDebugReportCallbackCreateInfoEXT
* pCreateInfo
,
1323 const VkAllocationCallbacks
* pAllocator
,
1324 VkDebugReportCallbackEXT
* pCallback
)
1326 ANV_FROM_HANDLE(anv_instance
, instance
, _instance
);
1327 return vk_create_debug_report_callback(&instance
->debug_report_callbacks
,
1328 pCreateInfo
, pAllocator
, &instance
->alloc
,
1333 anv_DestroyDebugReportCallbackEXT(VkInstance _instance
,
1334 VkDebugReportCallbackEXT _callback
,
1335 const VkAllocationCallbacks
* pAllocator
)
1337 ANV_FROM_HANDLE(anv_instance
, instance
, _instance
);
1338 vk_destroy_debug_report_callback(&instance
->debug_report_callbacks
,
1339 _callback
, pAllocator
, &instance
->alloc
);
1343 anv_DebugReportMessageEXT(VkInstance _instance
,
1344 VkDebugReportFlagsEXT flags
,
1345 VkDebugReportObjectTypeEXT objectType
,
1348 int32_t messageCode
,
1349 const char* pLayerPrefix
,
1350 const char* pMessage
)
1352 ANV_FROM_HANDLE(anv_instance
, instance
, _instance
);
1353 vk_debug_report(&instance
->debug_report_callbacks
, flags
, objectType
,
1354 object
, location
, messageCode
, pLayerPrefix
, pMessage
);
1358 anv_queue_init(struct anv_device
*device
, struct anv_queue
*queue
)
1360 queue
->_loader_data
.loaderMagic
= ICD_LOADER_MAGIC
;
1361 queue
->device
= device
;
1366 anv_queue_finish(struct anv_queue
*queue
)
1370 static struct anv_state
1371 anv_state_pool_emit_data(struct anv_state_pool
*pool
, size_t size
, size_t align
, const void *p
)
1373 struct anv_state state
;
1375 state
= anv_state_pool_alloc(pool
, size
, align
);
1376 memcpy(state
.map
, p
, size
);
1378 anv_state_flush(pool
->block_pool
.device
, state
);
1383 struct gen8_border_color
{
1388 /* Pad out to 64 bytes */
1393 anv_device_init_border_colors(struct anv_device
*device
)
1395 static const struct gen8_border_color border_colors
[] = {
1396 [VK_BORDER_COLOR_FLOAT_TRANSPARENT_BLACK
] = { .float32
= { 0.0, 0.0, 0.0, 0.0 } },
1397 [VK_BORDER_COLOR_FLOAT_OPAQUE_BLACK
] = { .float32
= { 0.0, 0.0, 0.0, 1.0 } },
1398 [VK_BORDER_COLOR_FLOAT_OPAQUE_WHITE
] = { .float32
= { 1.0, 1.0, 1.0, 1.0 } },
1399 [VK_BORDER_COLOR_INT_TRANSPARENT_BLACK
] = { .uint32
= { 0, 0, 0, 0 } },
1400 [VK_BORDER_COLOR_INT_OPAQUE_BLACK
] = { .uint32
= { 0, 0, 0, 1 } },
1401 [VK_BORDER_COLOR_INT_OPAQUE_WHITE
] = { .uint32
= { 1, 1, 1, 1 } },
1404 device
->border_colors
= anv_state_pool_emit_data(&device
->dynamic_state_pool
,
1405 sizeof(border_colors
), 64,
1410 anv_device_init_trivial_batch(struct anv_device
*device
)
1412 anv_bo_init_new(&device
->trivial_batch_bo
, device
, 4096);
1414 if (device
->instance
->physicalDevice
.has_exec_async
)
1415 device
->trivial_batch_bo
.flags
|= EXEC_OBJECT_ASYNC
;
1417 if (device
->instance
->physicalDevice
.use_softpin
)
1418 device
->trivial_batch_bo
.flags
|= EXEC_OBJECT_PINNED
;
1420 anv_vma_alloc(device
, &device
->trivial_batch_bo
);
1422 void *map
= anv_gem_mmap(device
, device
->trivial_batch_bo
.gem_handle
,
1425 struct anv_batch batch
= {
1431 anv_batch_emit(&batch
, GEN7_MI_BATCH_BUFFER_END
, bbe
);
1432 anv_batch_emit(&batch
, GEN7_MI_NOOP
, noop
);
1434 if (!device
->info
.has_llc
)
1435 gen_clflush_range(map
, batch
.next
- map
);
1437 anv_gem_munmap(map
, device
->trivial_batch_bo
.size
);
1440 VkResult
anv_EnumerateDeviceExtensionProperties(
1441 VkPhysicalDevice physicalDevice
,
1442 const char* pLayerName
,
1443 uint32_t* pPropertyCount
,
1444 VkExtensionProperties
* pProperties
)
1446 ANV_FROM_HANDLE(anv_physical_device
, device
, physicalDevice
);
1447 VK_OUTARRAY_MAKE(out
, pProperties
, pPropertyCount
);
1450 for (int i
= 0; i
< ANV_DEVICE_EXTENSION_COUNT
; i
++) {
1451 if (device
->supported_extensions
.extensions
[i
]) {
1452 vk_outarray_append(&out
, prop
) {
1453 *prop
= anv_device_extensions
[i
];
1458 return vk_outarray_status(&out
);
1462 anv_device_init_dispatch(struct anv_device
*device
)
1464 const struct anv_dispatch_table
*genX_table
;
1465 switch (device
->info
.gen
) {
1467 genX_table
= &gen11_dispatch_table
;
1470 genX_table
= &gen10_dispatch_table
;
1473 genX_table
= &gen9_dispatch_table
;
1476 genX_table
= &gen8_dispatch_table
;
1479 if (device
->info
.is_haswell
)
1480 genX_table
= &gen75_dispatch_table
;
1482 genX_table
= &gen7_dispatch_table
;
1485 unreachable("unsupported gen\n");
1488 for (unsigned i
= 0; i
< ARRAY_SIZE(device
->dispatch
.entrypoints
); i
++) {
1489 /* Vulkan requires that entrypoints for extensions which have not been
1490 * enabled must not be advertised.
1492 if (!anv_entrypoint_is_enabled(i
, device
->instance
->apiVersion
,
1493 &device
->instance
->enabled_extensions
,
1494 &device
->enabled_extensions
)) {
1495 device
->dispatch
.entrypoints
[i
] = NULL
;
1496 } else if (genX_table
->entrypoints
[i
]) {
1497 device
->dispatch
.entrypoints
[i
] = genX_table
->entrypoints
[i
];
1499 device
->dispatch
.entrypoints
[i
] = anv_dispatch_table
.entrypoints
[i
];
1505 vk_priority_to_gen(int priority
)
1508 case VK_QUEUE_GLOBAL_PRIORITY_LOW_EXT
:
1509 return GEN_CONTEXT_LOW_PRIORITY
;
1510 case VK_QUEUE_GLOBAL_PRIORITY_MEDIUM_EXT
:
1511 return GEN_CONTEXT_MEDIUM_PRIORITY
;
1512 case VK_QUEUE_GLOBAL_PRIORITY_HIGH_EXT
:
1513 return GEN_CONTEXT_HIGH_PRIORITY
;
1514 case VK_QUEUE_GLOBAL_PRIORITY_REALTIME_EXT
:
1515 return GEN_CONTEXT_REALTIME_PRIORITY
;
1517 unreachable("Invalid priority");
1522 anv_device_init_hiz_clear_batch(struct anv_device
*device
)
1524 anv_bo_init_new(&device
->hiz_clear_bo
, device
, 4096);
1525 uint32_t *map
= anv_gem_mmap(device
, device
->hiz_clear_bo
.gem_handle
,
1528 union isl_color_value hiz_clear
= { .u32
= { 0, } };
1529 hiz_clear
.f32
[0] = ANV_HZ_FC_VAL
;
1531 memcpy(map
, hiz_clear
.u32
, sizeof(hiz_clear
.u32
));
1532 anv_gem_munmap(map
, device
->hiz_clear_bo
.size
);
1535 VkResult
anv_CreateDevice(
1536 VkPhysicalDevice physicalDevice
,
1537 const VkDeviceCreateInfo
* pCreateInfo
,
1538 const VkAllocationCallbacks
* pAllocator
,
1541 ANV_FROM_HANDLE(anv_physical_device
, physical_device
, physicalDevice
);
1543 struct anv_device
*device
;
1545 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO
);
1547 struct anv_device_extension_table enabled_extensions
= { };
1548 for (uint32_t i
= 0; i
< pCreateInfo
->enabledExtensionCount
; i
++) {
1550 for (idx
= 0; idx
< ANV_DEVICE_EXTENSION_COUNT
; idx
++) {
1551 if (strcmp(pCreateInfo
->ppEnabledExtensionNames
[i
],
1552 anv_device_extensions
[idx
].extensionName
) == 0)
1556 if (idx
>= ANV_DEVICE_EXTENSION_COUNT
)
1557 return vk_error(VK_ERROR_EXTENSION_NOT_PRESENT
);
1559 if (!physical_device
->supported_extensions
.extensions
[idx
])
1560 return vk_error(VK_ERROR_EXTENSION_NOT_PRESENT
);
1562 enabled_extensions
.extensions
[idx
] = true;
1565 /* Check enabled features */
1566 if (pCreateInfo
->pEnabledFeatures
) {
1567 VkPhysicalDeviceFeatures supported_features
;
1568 anv_GetPhysicalDeviceFeatures(physicalDevice
, &supported_features
);
1569 VkBool32
*supported_feature
= (VkBool32
*)&supported_features
;
1570 VkBool32
*enabled_feature
= (VkBool32
*)pCreateInfo
->pEnabledFeatures
;
1571 unsigned num_features
= sizeof(VkPhysicalDeviceFeatures
) / sizeof(VkBool32
);
1572 for (uint32_t i
= 0; i
< num_features
; i
++) {
1573 if (enabled_feature
[i
] && !supported_feature
[i
])
1574 return vk_error(VK_ERROR_FEATURE_NOT_PRESENT
);
1578 /* Check requested queues and fail if we are requested to create any
1579 * queues with flags we don't support.
1581 assert(pCreateInfo
->queueCreateInfoCount
> 0);
1582 for (uint32_t i
= 0; i
< pCreateInfo
->queueCreateInfoCount
; i
++) {
1583 if (pCreateInfo
->pQueueCreateInfos
[i
].flags
!= 0)
1584 return vk_error(VK_ERROR_INITIALIZATION_FAILED
);
1587 /* Check if client specified queue priority. */
1588 const VkDeviceQueueGlobalPriorityCreateInfoEXT
*queue_priority
=
1589 vk_find_struct_const(pCreateInfo
->pQueueCreateInfos
[0].pNext
,
1590 DEVICE_QUEUE_GLOBAL_PRIORITY_CREATE_INFO_EXT
);
1592 VkQueueGlobalPriorityEXT priority
=
1593 queue_priority
? queue_priority
->globalPriority
:
1594 VK_QUEUE_GLOBAL_PRIORITY_MEDIUM_EXT
;
1596 device
= vk_alloc2(&physical_device
->instance
->alloc
, pAllocator
,
1598 VK_SYSTEM_ALLOCATION_SCOPE_DEVICE
);
1600 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
1602 device
->_loader_data
.loaderMagic
= ICD_LOADER_MAGIC
;
1603 device
->instance
= physical_device
->instance
;
1604 device
->chipset_id
= physical_device
->chipset_id
;
1605 device
->no_hw
= physical_device
->no_hw
;
1606 device
->lost
= false;
1609 device
->alloc
= *pAllocator
;
1611 device
->alloc
= physical_device
->instance
->alloc
;
1613 /* XXX(chadv): Can we dup() physicalDevice->fd here? */
1614 device
->fd
= open(physical_device
->path
, O_RDWR
| O_CLOEXEC
);
1615 if (device
->fd
== -1) {
1616 result
= vk_error(VK_ERROR_INITIALIZATION_FAILED
);
1620 device
->context_id
= anv_gem_create_context(device
);
1621 if (device
->context_id
== -1) {
1622 result
= vk_error(VK_ERROR_INITIALIZATION_FAILED
);
1626 if (physical_device
->use_softpin
) {
1627 if (pthread_mutex_init(&device
->vma_mutex
, NULL
) != 0) {
1628 result
= vk_error(VK_ERROR_INITIALIZATION_FAILED
);
1632 /* keep the page with address zero out of the allocator */
1633 util_vma_heap_init(&device
->vma_lo
, LOW_HEAP_MIN_ADDRESS
, LOW_HEAP_SIZE
);
1634 device
->vma_lo_available
=
1635 physical_device
->memory
.heaps
[physical_device
->memory
.heap_count
- 1].size
;
1637 /* Leave the last 4GiB out of the high vma range, so that no state base
1638 * address + size can overflow 48 bits. For more information see the
1639 * comment about Wa32bitGeneralStateOffset in anv_allocator.c
1641 util_vma_heap_init(&device
->vma_hi
, HIGH_HEAP_MIN_ADDRESS
,
1643 device
->vma_hi_available
= physical_device
->memory
.heap_count
== 1 ? 0 :
1644 physical_device
->memory
.heaps
[0].size
;
1647 /* As per spec, the driver implementation may deny requests to acquire
1648 * a priority above the default priority (MEDIUM) if the caller does not
1649 * have sufficient privileges. In this scenario VK_ERROR_NOT_PERMITTED_EXT
1652 if (physical_device
->has_context_priority
) {
1653 int err
= anv_gem_set_context_param(device
->fd
, device
->context_id
,
1654 I915_CONTEXT_PARAM_PRIORITY
,
1655 vk_priority_to_gen(priority
));
1656 if (err
!= 0 && priority
> VK_QUEUE_GLOBAL_PRIORITY_MEDIUM_EXT
) {
1657 result
= vk_error(VK_ERROR_NOT_PERMITTED_EXT
);
1662 device
->info
= physical_device
->info
;
1663 device
->isl_dev
= physical_device
->isl_dev
;
1665 /* On Broadwell and later, we can use batch chaining to more efficiently
1666 * implement growing command buffers. Prior to Haswell, the kernel
1667 * command parser gets in the way and we have to fall back to growing
1670 device
->can_chain_batches
= device
->info
.gen
>= 8;
1672 device
->robust_buffer_access
= pCreateInfo
->pEnabledFeatures
&&
1673 pCreateInfo
->pEnabledFeatures
->robustBufferAccess
;
1674 device
->enabled_extensions
= enabled_extensions
;
1676 anv_device_init_dispatch(device
);
1678 if (pthread_mutex_init(&device
->mutex
, NULL
) != 0) {
1679 result
= vk_error(VK_ERROR_INITIALIZATION_FAILED
);
1680 goto fail_context_id
;
1683 pthread_condattr_t condattr
;
1684 if (pthread_condattr_init(&condattr
) != 0) {
1685 result
= vk_error(VK_ERROR_INITIALIZATION_FAILED
);
1688 if (pthread_condattr_setclock(&condattr
, CLOCK_MONOTONIC
) != 0) {
1689 pthread_condattr_destroy(&condattr
);
1690 result
= vk_error(VK_ERROR_INITIALIZATION_FAILED
);
1693 if (pthread_cond_init(&device
->queue_submit
, NULL
) != 0) {
1694 pthread_condattr_destroy(&condattr
);
1695 result
= vk_error(VK_ERROR_INITIALIZATION_FAILED
);
1698 pthread_condattr_destroy(&condattr
);
1701 (physical_device
->supports_48bit_addresses
? EXEC_OBJECT_SUPPORTS_48B_ADDRESS
: 0) |
1702 (physical_device
->has_exec_async
? EXEC_OBJECT_ASYNC
: 0) |
1703 (physical_device
->has_exec_capture
? EXEC_OBJECT_CAPTURE
: 0) |
1704 (physical_device
->use_softpin
? EXEC_OBJECT_PINNED
: 0);
1706 anv_bo_pool_init(&device
->batch_bo_pool
, device
, bo_flags
);
1708 result
= anv_bo_cache_init(&device
->bo_cache
);
1709 if (result
!= VK_SUCCESS
)
1710 goto fail_batch_bo_pool
;
1712 if (!physical_device
->use_softpin
)
1713 bo_flags
&= ~EXEC_OBJECT_SUPPORTS_48B_ADDRESS
;
1715 result
= anv_state_pool_init(&device
->dynamic_state_pool
, device
,
1716 DYNAMIC_STATE_POOL_MIN_ADDRESS
,
1719 if (result
!= VK_SUCCESS
)
1722 result
= anv_state_pool_init(&device
->instruction_state_pool
, device
,
1723 INSTRUCTION_STATE_POOL_MIN_ADDRESS
,
1726 if (result
!= VK_SUCCESS
)
1727 goto fail_dynamic_state_pool
;
1729 result
= anv_state_pool_init(&device
->surface_state_pool
, device
,
1730 SURFACE_STATE_POOL_MIN_ADDRESS
,
1733 if (result
!= VK_SUCCESS
)
1734 goto fail_instruction_state_pool
;
1736 if (physical_device
->use_softpin
) {
1737 result
= anv_state_pool_init(&device
->binding_table_pool
, device
,
1738 BINDING_TABLE_POOL_MIN_ADDRESS
,
1741 if (result
!= VK_SUCCESS
)
1742 goto fail_surface_state_pool
;
1745 result
= anv_bo_init_new(&device
->workaround_bo
, device
, 1024);
1746 if (result
!= VK_SUCCESS
)
1747 goto fail_binding_table_pool
;
1749 if (physical_device
->use_softpin
)
1750 device
->workaround_bo
.flags
|= EXEC_OBJECT_PINNED
;
1752 if (!anv_vma_alloc(device
, &device
->workaround_bo
))
1753 goto fail_workaround_bo
;
1755 anv_device_init_trivial_batch(device
);
1757 if (device
->info
.gen
>= 10)
1758 anv_device_init_hiz_clear_batch(device
);
1760 anv_scratch_pool_init(device
, &device
->scratch_pool
);
1762 anv_queue_init(device
, &device
->queue
);
1764 switch (device
->info
.gen
) {
1766 if (!device
->info
.is_haswell
)
1767 result
= gen7_init_device_state(device
);
1769 result
= gen75_init_device_state(device
);
1772 result
= gen8_init_device_state(device
);
1775 result
= gen9_init_device_state(device
);
1778 result
= gen10_init_device_state(device
);
1781 result
= gen11_init_device_state(device
);
1784 /* Shouldn't get here as we don't create physical devices for any other
1786 unreachable("unhandled gen");
1788 if (result
!= VK_SUCCESS
)
1789 goto fail_workaround_bo
;
1791 anv_pipeline_cache_init(&device
->default_pipeline_cache
, device
, true);
1793 anv_device_init_blorp(device
);
1795 anv_device_init_border_colors(device
);
1797 *pDevice
= anv_device_to_handle(device
);
1802 anv_queue_finish(&device
->queue
);
1803 anv_scratch_pool_finish(device
, &device
->scratch_pool
);
1804 anv_gem_munmap(device
->workaround_bo
.map
, device
->workaround_bo
.size
);
1805 anv_gem_close(device
, device
->workaround_bo
.gem_handle
);
1806 fail_binding_table_pool
:
1807 if (physical_device
->use_softpin
)
1808 anv_state_pool_finish(&device
->binding_table_pool
);
1809 fail_surface_state_pool
:
1810 anv_state_pool_finish(&device
->surface_state_pool
);
1811 fail_instruction_state_pool
:
1812 anv_state_pool_finish(&device
->instruction_state_pool
);
1813 fail_dynamic_state_pool
:
1814 anv_state_pool_finish(&device
->dynamic_state_pool
);
1816 anv_bo_cache_finish(&device
->bo_cache
);
1818 anv_bo_pool_finish(&device
->batch_bo_pool
);
1819 pthread_cond_destroy(&device
->queue_submit
);
1821 pthread_mutex_destroy(&device
->mutex
);
1823 anv_gem_destroy_context(device
, device
->context_id
);
1827 vk_free(&device
->alloc
, device
);
1832 void anv_DestroyDevice(
1834 const VkAllocationCallbacks
* pAllocator
)
1836 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1837 struct anv_physical_device
*physical_device
;
1842 physical_device
= &device
->instance
->physicalDevice
;
1844 anv_device_finish_blorp(device
);
1846 anv_pipeline_cache_finish(&device
->default_pipeline_cache
);
1848 anv_queue_finish(&device
->queue
);
1850 #ifdef HAVE_VALGRIND
1851 /* We only need to free these to prevent valgrind errors. The backing
1852 * BO will go away in a couple of lines so we don't actually leak.
1854 anv_state_pool_free(&device
->dynamic_state_pool
, device
->border_colors
);
1857 anv_scratch_pool_finish(device
, &device
->scratch_pool
);
1859 anv_gem_munmap(device
->workaround_bo
.map
, device
->workaround_bo
.size
);
1860 anv_vma_free(device
, &device
->workaround_bo
);
1861 anv_gem_close(device
, device
->workaround_bo
.gem_handle
);
1863 anv_vma_free(device
, &device
->trivial_batch_bo
);
1864 anv_gem_close(device
, device
->trivial_batch_bo
.gem_handle
);
1865 if (device
->info
.gen
>= 10)
1866 anv_gem_close(device
, device
->hiz_clear_bo
.gem_handle
);
1868 if (physical_device
->use_softpin
)
1869 anv_state_pool_finish(&device
->binding_table_pool
);
1870 anv_state_pool_finish(&device
->surface_state_pool
);
1871 anv_state_pool_finish(&device
->instruction_state_pool
);
1872 anv_state_pool_finish(&device
->dynamic_state_pool
);
1874 anv_bo_cache_finish(&device
->bo_cache
);
1876 anv_bo_pool_finish(&device
->batch_bo_pool
);
1878 pthread_cond_destroy(&device
->queue_submit
);
1879 pthread_mutex_destroy(&device
->mutex
);
1881 anv_gem_destroy_context(device
, device
->context_id
);
1885 vk_free(&device
->alloc
, device
);
1888 VkResult
anv_EnumerateInstanceLayerProperties(
1889 uint32_t* pPropertyCount
,
1890 VkLayerProperties
* pProperties
)
1892 if (pProperties
== NULL
) {
1893 *pPropertyCount
= 0;
1897 /* None supported at this time */
1898 return vk_error(VK_ERROR_LAYER_NOT_PRESENT
);
1901 VkResult
anv_EnumerateDeviceLayerProperties(
1902 VkPhysicalDevice physicalDevice
,
1903 uint32_t* pPropertyCount
,
1904 VkLayerProperties
* pProperties
)
1906 if (pProperties
== NULL
) {
1907 *pPropertyCount
= 0;
1911 /* None supported at this time */
1912 return vk_error(VK_ERROR_LAYER_NOT_PRESENT
);
1915 void anv_GetDeviceQueue(
1917 uint32_t queueNodeIndex
,
1918 uint32_t queueIndex
,
1921 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1923 assert(queueIndex
== 0);
1925 *pQueue
= anv_queue_to_handle(&device
->queue
);
1928 void anv_GetDeviceQueue2(
1930 const VkDeviceQueueInfo2
* pQueueInfo
,
1933 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1935 assert(pQueueInfo
->queueIndex
== 0);
1937 if (pQueueInfo
->flags
== device
->queue
.flags
)
1938 *pQueue
= anv_queue_to_handle(&device
->queue
);
1944 anv_device_query_status(struct anv_device
*device
)
1946 /* This isn't likely as most of the callers of this function already check
1947 * for it. However, it doesn't hurt to check and it potentially lets us
1950 if (unlikely(device
->lost
))
1951 return VK_ERROR_DEVICE_LOST
;
1953 uint32_t active
, pending
;
1954 int ret
= anv_gem_gpu_get_reset_stats(device
, &active
, &pending
);
1956 /* We don't know the real error. */
1957 device
->lost
= true;
1958 return vk_errorf(device
->instance
, device
, VK_ERROR_DEVICE_LOST
,
1959 "get_reset_stats failed: %m");
1963 device
->lost
= true;
1964 return vk_errorf(device
->instance
, device
, VK_ERROR_DEVICE_LOST
,
1965 "GPU hung on one of our command buffers");
1966 } else if (pending
) {
1967 device
->lost
= true;
1968 return vk_errorf(device
->instance
, device
, VK_ERROR_DEVICE_LOST
,
1969 "GPU hung with commands in-flight");
1976 anv_device_bo_busy(struct anv_device
*device
, struct anv_bo
*bo
)
1978 /* Note: This only returns whether or not the BO is in use by an i915 GPU.
1979 * Other usages of the BO (such as on different hardware) will not be
1980 * flagged as "busy" by this ioctl. Use with care.
1982 int ret
= anv_gem_busy(device
, bo
->gem_handle
);
1984 return VK_NOT_READY
;
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 busy call. If the BO we're checking
1993 * got caught in a GPU hang we don't want to return VK_SUCCESS to the
1994 * client because it clearly doesn't have valid data. Yes, this most
1995 * likely means an ioctl, but we just did an ioctl to query the busy status
1996 * so it's no great loss.
1998 return anv_device_query_status(device
);
2002 anv_device_wait(struct anv_device
*device
, struct anv_bo
*bo
,
2005 int ret
= anv_gem_wait(device
, bo
->gem_handle
, &timeout
);
2006 if (ret
== -1 && errno
== ETIME
) {
2008 } else if (ret
== -1) {
2009 /* We don't know the real error. */
2010 device
->lost
= true;
2011 return vk_errorf(device
->instance
, device
, VK_ERROR_DEVICE_LOST
,
2012 "gem wait failed: %m");
2015 /* Query for device status after the wait. If the BO we're waiting on got
2016 * caught in a GPU hang we don't want to return VK_SUCCESS to the client
2017 * because it clearly doesn't have valid data. Yes, this most likely means
2018 * an ioctl, but we just did an ioctl to wait so it's no great loss.
2020 return anv_device_query_status(device
);
2023 VkResult
anv_DeviceWaitIdle(
2026 ANV_FROM_HANDLE(anv_device
, device
, _device
);
2027 if (unlikely(device
->lost
))
2028 return VK_ERROR_DEVICE_LOST
;
2030 struct anv_batch batch
;
2033 batch
.start
= batch
.next
= cmds
;
2034 batch
.end
= (void *) cmds
+ sizeof(cmds
);
2036 anv_batch_emit(&batch
, GEN7_MI_BATCH_BUFFER_END
, bbe
);
2037 anv_batch_emit(&batch
, GEN7_MI_NOOP
, noop
);
2039 return anv_device_submit_simple_batch(device
, &batch
);
2043 anv_vma_alloc(struct anv_device
*device
, struct anv_bo
*bo
)
2045 if (!(bo
->flags
& EXEC_OBJECT_PINNED
))
2048 pthread_mutex_lock(&device
->vma_mutex
);
2052 if (bo
->flags
& EXEC_OBJECT_SUPPORTS_48B_ADDRESS
&&
2053 device
->vma_hi_available
>= bo
->size
) {
2054 uint64_t addr
= util_vma_heap_alloc(&device
->vma_hi
, bo
->size
, 4096);
2056 bo
->offset
= gen_canonical_address(addr
);
2057 assert(addr
== gen_48b_address(bo
->offset
));
2058 device
->vma_hi_available
-= bo
->size
;
2062 if (bo
->offset
== 0 && device
->vma_lo_available
>= bo
->size
) {
2063 uint64_t addr
= util_vma_heap_alloc(&device
->vma_lo
, bo
->size
, 4096);
2065 bo
->offset
= gen_canonical_address(addr
);
2066 assert(addr
== gen_48b_address(bo
->offset
));
2067 device
->vma_lo_available
-= bo
->size
;
2071 pthread_mutex_unlock(&device
->vma_mutex
);
2073 return bo
->offset
!= 0;
2077 anv_vma_free(struct anv_device
*device
, struct anv_bo
*bo
)
2079 if (!(bo
->flags
& EXEC_OBJECT_PINNED
))
2082 const uint64_t addr_48b
= gen_48b_address(bo
->offset
);
2084 pthread_mutex_lock(&device
->vma_mutex
);
2086 if (addr_48b
>= LOW_HEAP_MIN_ADDRESS
&&
2087 addr_48b
<= LOW_HEAP_MAX_ADDRESS
) {
2088 util_vma_heap_free(&device
->vma_lo
, addr_48b
, bo
->size
);
2089 device
->vma_lo_available
+= bo
->size
;
2091 assert(addr_48b
>= HIGH_HEAP_MIN_ADDRESS
&&
2092 addr_48b
<= HIGH_HEAP_MAX_ADDRESS
);
2093 util_vma_heap_free(&device
->vma_hi
, addr_48b
, bo
->size
);
2094 device
->vma_hi_available
+= bo
->size
;
2097 pthread_mutex_unlock(&device
->vma_mutex
);
2103 anv_bo_init_new(struct anv_bo
*bo
, struct anv_device
*device
, uint64_t size
)
2105 uint32_t gem_handle
= anv_gem_create(device
, size
);
2107 return vk_error(VK_ERROR_OUT_OF_DEVICE_MEMORY
);
2109 anv_bo_init(bo
, gem_handle
, size
);
2114 VkResult
anv_AllocateMemory(
2116 const VkMemoryAllocateInfo
* pAllocateInfo
,
2117 const VkAllocationCallbacks
* pAllocator
,
2118 VkDeviceMemory
* pMem
)
2120 ANV_FROM_HANDLE(anv_device
, device
, _device
);
2121 struct anv_physical_device
*pdevice
= &device
->instance
->physicalDevice
;
2122 struct anv_device_memory
*mem
;
2123 VkResult result
= VK_SUCCESS
;
2125 assert(pAllocateInfo
->sType
== VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO
);
2127 /* The Vulkan 1.0.33 spec says "allocationSize must be greater than 0". */
2128 assert(pAllocateInfo
->allocationSize
> 0);
2130 if (pAllocateInfo
->allocationSize
> MAX_MEMORY_ALLOCATION_SIZE
)
2131 return VK_ERROR_OUT_OF_DEVICE_MEMORY
;
2133 /* FINISHME: Fail if allocation request exceeds heap size. */
2135 mem
= vk_alloc2(&device
->alloc
, pAllocator
, sizeof(*mem
), 8,
2136 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
2138 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
2140 assert(pAllocateInfo
->memoryTypeIndex
< pdevice
->memory
.type_count
);
2141 mem
->type
= &pdevice
->memory
.types
[pAllocateInfo
->memoryTypeIndex
];
2145 uint64_t bo_flags
= 0;
2147 assert(mem
->type
->heapIndex
< pdevice
->memory
.heap_count
);
2148 if (pdevice
->memory
.heaps
[mem
->type
->heapIndex
].supports_48bit_addresses
)
2149 bo_flags
|= EXEC_OBJECT_SUPPORTS_48B_ADDRESS
;
2151 const struct wsi_memory_allocate_info
*wsi_info
=
2152 vk_find_struct_const(pAllocateInfo
->pNext
, WSI_MEMORY_ALLOCATE_INFO_MESA
);
2153 if (wsi_info
&& wsi_info
->implicit_sync
) {
2154 /* We need to set the WRITE flag on window system buffers so that GEM
2155 * will know we're writing to them and synchronize uses on other rings
2156 * (eg if the display server uses the blitter ring).
2158 bo_flags
|= EXEC_OBJECT_WRITE
;
2159 } else if (pdevice
->has_exec_async
) {
2160 bo_flags
|= EXEC_OBJECT_ASYNC
;
2163 if (pdevice
->use_softpin
)
2164 bo_flags
|= EXEC_OBJECT_PINNED
;
2166 const VkImportMemoryFdInfoKHR
*fd_info
=
2167 vk_find_struct_const(pAllocateInfo
->pNext
, IMPORT_MEMORY_FD_INFO_KHR
);
2169 /* The Vulkan spec permits handleType to be 0, in which case the struct is
2172 if (fd_info
&& fd_info
->handleType
) {
2173 /* At the moment, we support only the below handle types. */
2174 assert(fd_info
->handleType
==
2175 VK_EXTERNAL_MEMORY_HANDLE_TYPE_OPAQUE_FD_BIT
||
2176 fd_info
->handleType
==
2177 VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_EXT
);
2179 result
= anv_bo_cache_import(device
, &device
->bo_cache
,
2180 fd_info
->fd
, bo_flags
, &mem
->bo
);
2181 if (result
!= VK_SUCCESS
)
2184 VkDeviceSize aligned_alloc_size
=
2185 align_u64(pAllocateInfo
->allocationSize
, 4096);
2187 /* For security purposes, we reject importing the bo if it's smaller
2188 * than the requested allocation size. This prevents a malicious client
2189 * from passing a buffer to a trusted client, lying about the size, and
2190 * telling the trusted client to try and texture from an image that goes
2191 * out-of-bounds. This sort of thing could lead to GPU hangs or worse
2192 * in the trusted client. The trusted client can protect itself against
2193 * this sort of attack but only if it can trust the buffer size.
2195 if (mem
->bo
->size
< aligned_alloc_size
) {
2196 result
= vk_errorf(device
->instance
, device
,
2197 VK_ERROR_INVALID_EXTERNAL_HANDLE_KHR
,
2198 "aligned allocationSize too large for "
2199 "VK_EXTERNAL_MEMORY_HANDLE_TYPE_OPAQUE_FD_BIT_KHR: "
2200 "%"PRIu64
"B > %"PRIu64
"B",
2201 aligned_alloc_size
, mem
->bo
->size
);
2202 anv_bo_cache_release(device
, &device
->bo_cache
, mem
->bo
);
2206 /* From the Vulkan spec:
2208 * "Importing memory from a file descriptor transfers ownership of
2209 * the file descriptor from the application to the Vulkan
2210 * implementation. The application must not perform any operations on
2211 * the file descriptor after a successful import."
2213 * If the import fails, we leave the file descriptor open.
2217 result
= anv_bo_cache_alloc(device
, &device
->bo_cache
,
2218 pAllocateInfo
->allocationSize
, bo_flags
,
2220 if (result
!= VK_SUCCESS
)
2223 const VkMemoryDedicatedAllocateInfoKHR
*dedicated_info
=
2224 vk_find_struct_const(pAllocateInfo
->pNext
, MEMORY_DEDICATED_ALLOCATE_INFO_KHR
);
2225 if (dedicated_info
&& dedicated_info
->image
!= VK_NULL_HANDLE
) {
2226 ANV_FROM_HANDLE(anv_image
, image
, dedicated_info
->image
);
2228 /* Some legacy (non-modifiers) consumers need the tiling to be set on
2229 * the BO. In this case, we have a dedicated allocation.
2231 if (image
->needs_set_tiling
) {
2232 const uint32_t i915_tiling
=
2233 isl_tiling_to_i915_tiling(image
->planes
[0].surface
.isl
.tiling
);
2234 int ret
= anv_gem_set_tiling(device
, mem
->bo
->gem_handle
,
2235 image
->planes
[0].surface
.isl
.row_pitch
,
2238 anv_bo_cache_release(device
, &device
->bo_cache
, mem
->bo
);
2239 return vk_errorf(device
->instance
, NULL
,
2240 VK_ERROR_OUT_OF_DEVICE_MEMORY
,
2241 "failed to set BO tiling: %m");
2247 *pMem
= anv_device_memory_to_handle(mem
);
2252 vk_free2(&device
->alloc
, pAllocator
, mem
);
2257 VkResult
anv_GetMemoryFdKHR(
2259 const VkMemoryGetFdInfoKHR
* pGetFdInfo
,
2262 ANV_FROM_HANDLE(anv_device
, dev
, device_h
);
2263 ANV_FROM_HANDLE(anv_device_memory
, mem
, pGetFdInfo
->memory
);
2265 assert(pGetFdInfo
->sType
== VK_STRUCTURE_TYPE_MEMORY_GET_FD_INFO_KHR
);
2267 assert(pGetFdInfo
->handleType
== VK_EXTERNAL_MEMORY_HANDLE_TYPE_OPAQUE_FD_BIT
||
2268 pGetFdInfo
->handleType
== VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_EXT
);
2270 return anv_bo_cache_export(dev
, &dev
->bo_cache
, mem
->bo
, pFd
);
2273 VkResult
anv_GetMemoryFdPropertiesKHR(
2275 VkExternalMemoryHandleTypeFlagBitsKHR handleType
,
2277 VkMemoryFdPropertiesKHR
* pMemoryFdProperties
)
2279 ANV_FROM_HANDLE(anv_device
, device
, _device
);
2280 struct anv_physical_device
*pdevice
= &device
->instance
->physicalDevice
;
2282 switch (handleType
) {
2283 case VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_EXT
:
2284 /* dma-buf can be imported as any memory type */
2285 pMemoryFdProperties
->memoryTypeBits
=
2286 (1 << pdevice
->memory
.type_count
) - 1;
2290 /* The valid usage section for this function says:
2292 * "handleType must not be one of the handle types defined as
2295 * So opaque handle types fall into the default "unsupported" case.
2297 return vk_error(VK_ERROR_INVALID_EXTERNAL_HANDLE
);
2301 void anv_FreeMemory(
2303 VkDeviceMemory _mem
,
2304 const VkAllocationCallbacks
* pAllocator
)
2306 ANV_FROM_HANDLE(anv_device
, device
, _device
);
2307 ANV_FROM_HANDLE(anv_device_memory
, mem
, _mem
);
2313 anv_UnmapMemory(_device
, _mem
);
2315 anv_bo_cache_release(device
, &device
->bo_cache
, mem
->bo
);
2317 vk_free2(&device
->alloc
, pAllocator
, mem
);
2320 VkResult
anv_MapMemory(
2322 VkDeviceMemory _memory
,
2323 VkDeviceSize offset
,
2325 VkMemoryMapFlags flags
,
2328 ANV_FROM_HANDLE(anv_device
, device
, _device
);
2329 ANV_FROM_HANDLE(anv_device_memory
, mem
, _memory
);
2336 if (size
== VK_WHOLE_SIZE
)
2337 size
= mem
->bo
->size
- offset
;
2339 /* From the Vulkan spec version 1.0.32 docs for MapMemory:
2341 * * If size is not equal to VK_WHOLE_SIZE, size must be greater than 0
2342 * assert(size != 0);
2343 * * If size is not equal to VK_WHOLE_SIZE, size must be less than or
2344 * equal to the size of the memory minus offset
2347 assert(offset
+ size
<= mem
->bo
->size
);
2349 /* FIXME: Is this supposed to be thread safe? Since vkUnmapMemory() only
2350 * takes a VkDeviceMemory pointer, it seems like only one map of the memory
2351 * at a time is valid. We could just mmap up front and return an offset
2352 * pointer here, but that may exhaust virtual memory on 32 bit
2355 uint32_t gem_flags
= 0;
2357 if (!device
->info
.has_llc
&&
2358 (mem
->type
->propertyFlags
& VK_MEMORY_PROPERTY_HOST_COHERENT_BIT
))
2359 gem_flags
|= I915_MMAP_WC
;
2361 /* GEM will fail to map if the offset isn't 4k-aligned. Round down. */
2362 uint64_t map_offset
= offset
& ~4095ull;
2363 assert(offset
>= map_offset
);
2364 uint64_t map_size
= (offset
+ size
) - map_offset
;
2366 /* Let's map whole pages */
2367 map_size
= align_u64(map_size
, 4096);
2369 void *map
= anv_gem_mmap(device
, mem
->bo
->gem_handle
,
2370 map_offset
, map_size
, gem_flags
);
2371 if (map
== MAP_FAILED
)
2372 return vk_error(VK_ERROR_MEMORY_MAP_FAILED
);
2375 mem
->map_size
= map_size
;
2377 *ppData
= mem
->map
+ (offset
- map_offset
);
2382 void anv_UnmapMemory(
2384 VkDeviceMemory _memory
)
2386 ANV_FROM_HANDLE(anv_device_memory
, mem
, _memory
);
2391 anv_gem_munmap(mem
->map
, mem
->map_size
);
2398 clflush_mapped_ranges(struct anv_device
*device
,
2400 const VkMappedMemoryRange
*ranges
)
2402 for (uint32_t i
= 0; i
< count
; i
++) {
2403 ANV_FROM_HANDLE(anv_device_memory
, mem
, ranges
[i
].memory
);
2404 if (ranges
[i
].offset
>= mem
->map_size
)
2407 gen_clflush_range(mem
->map
+ ranges
[i
].offset
,
2408 MIN2(ranges
[i
].size
, mem
->map_size
- ranges
[i
].offset
));
2412 VkResult
anv_FlushMappedMemoryRanges(
2414 uint32_t memoryRangeCount
,
2415 const VkMappedMemoryRange
* pMemoryRanges
)
2417 ANV_FROM_HANDLE(anv_device
, device
, _device
);
2419 if (device
->info
.has_llc
)
2422 /* Make sure the writes we're flushing have landed. */
2423 __builtin_ia32_mfence();
2425 clflush_mapped_ranges(device
, memoryRangeCount
, pMemoryRanges
);
2430 VkResult
anv_InvalidateMappedMemoryRanges(
2432 uint32_t memoryRangeCount
,
2433 const VkMappedMemoryRange
* pMemoryRanges
)
2435 ANV_FROM_HANDLE(anv_device
, device
, _device
);
2437 if (device
->info
.has_llc
)
2440 clflush_mapped_ranges(device
, memoryRangeCount
, pMemoryRanges
);
2442 /* Make sure no reads get moved up above the invalidate. */
2443 __builtin_ia32_mfence();
2448 void anv_GetBufferMemoryRequirements(
2451 VkMemoryRequirements
* pMemoryRequirements
)
2453 ANV_FROM_HANDLE(anv_buffer
, buffer
, _buffer
);
2454 ANV_FROM_HANDLE(anv_device
, device
, _device
);
2455 struct anv_physical_device
*pdevice
= &device
->instance
->physicalDevice
;
2457 /* The Vulkan spec (git aaed022) says:
2459 * memoryTypeBits is a bitfield and contains one bit set for every
2460 * supported memory type for the resource. The bit `1<<i` is set if and
2461 * only if the memory type `i` in the VkPhysicalDeviceMemoryProperties
2462 * structure for the physical device is supported.
2464 uint32_t memory_types
= 0;
2465 for (uint32_t i
= 0; i
< pdevice
->memory
.type_count
; i
++) {
2466 uint32_t valid_usage
= pdevice
->memory
.types
[i
].valid_buffer_usage
;
2467 if ((valid_usage
& buffer
->usage
) == buffer
->usage
)
2468 memory_types
|= (1u << i
);
2471 /* Base alignment requirement of a cache line */
2472 uint32_t alignment
= 16;
2474 /* We need an alignment of 32 for pushing UBOs */
2475 if (buffer
->usage
& VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT
)
2476 alignment
= MAX2(alignment
, 32);
2478 pMemoryRequirements
->size
= buffer
->size
;
2479 pMemoryRequirements
->alignment
= alignment
;
2481 /* Storage and Uniform buffers should have their size aligned to
2482 * 32-bits to avoid boundary checks when last DWord is not complete.
2483 * This would ensure that not internal padding would be needed for
2486 if (device
->robust_buffer_access
&&
2487 (buffer
->usage
& VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT
||
2488 buffer
->usage
& VK_BUFFER_USAGE_STORAGE_BUFFER_BIT
))
2489 pMemoryRequirements
->size
= align_u64(buffer
->size
, 4);
2491 pMemoryRequirements
->memoryTypeBits
= memory_types
;
2494 void anv_GetBufferMemoryRequirements2(
2496 const VkBufferMemoryRequirementsInfo2
* pInfo
,
2497 VkMemoryRequirements2
* pMemoryRequirements
)
2499 anv_GetBufferMemoryRequirements(_device
, pInfo
->buffer
,
2500 &pMemoryRequirements
->memoryRequirements
);
2502 vk_foreach_struct(ext
, pMemoryRequirements
->pNext
) {
2503 switch (ext
->sType
) {
2504 case VK_STRUCTURE_TYPE_MEMORY_DEDICATED_REQUIREMENTS
: {
2505 VkMemoryDedicatedRequirements
*requirements
= (void *)ext
;
2506 requirements
->prefersDedicatedAllocation
= VK_FALSE
;
2507 requirements
->requiresDedicatedAllocation
= VK_FALSE
;
2512 anv_debug_ignored_stype(ext
->sType
);
2518 void anv_GetImageMemoryRequirements(
2521 VkMemoryRequirements
* pMemoryRequirements
)
2523 ANV_FROM_HANDLE(anv_image
, image
, _image
);
2524 ANV_FROM_HANDLE(anv_device
, device
, _device
);
2525 struct anv_physical_device
*pdevice
= &device
->instance
->physicalDevice
;
2527 /* The Vulkan spec (git aaed022) says:
2529 * memoryTypeBits is a bitfield and contains one bit set for every
2530 * supported memory type for the resource. The bit `1<<i` is set if and
2531 * only if the memory type `i` in the VkPhysicalDeviceMemoryProperties
2532 * structure for the physical device is supported.
2534 * All types are currently supported for images.
2536 uint32_t memory_types
= (1ull << pdevice
->memory
.type_count
) - 1;
2538 pMemoryRequirements
->size
= image
->size
;
2539 pMemoryRequirements
->alignment
= image
->alignment
;
2540 pMemoryRequirements
->memoryTypeBits
= memory_types
;
2543 void anv_GetImageMemoryRequirements2(
2545 const VkImageMemoryRequirementsInfo2
* pInfo
,
2546 VkMemoryRequirements2
* pMemoryRequirements
)
2548 ANV_FROM_HANDLE(anv_device
, device
, _device
);
2549 ANV_FROM_HANDLE(anv_image
, image
, pInfo
->image
);
2551 anv_GetImageMemoryRequirements(_device
, pInfo
->image
,
2552 &pMemoryRequirements
->memoryRequirements
);
2554 vk_foreach_struct_const(ext
, pInfo
->pNext
) {
2555 switch (ext
->sType
) {
2556 case VK_STRUCTURE_TYPE_IMAGE_PLANE_MEMORY_REQUIREMENTS_INFO
: {
2557 struct anv_physical_device
*pdevice
= &device
->instance
->physicalDevice
;
2558 const VkImagePlaneMemoryRequirementsInfoKHR
*plane_reqs
=
2559 (const VkImagePlaneMemoryRequirementsInfoKHR
*) ext
;
2560 uint32_t plane
= anv_image_aspect_to_plane(image
->aspects
,
2561 plane_reqs
->planeAspect
);
2563 assert(image
->planes
[plane
].offset
== 0);
2565 /* The Vulkan spec (git aaed022) says:
2567 * memoryTypeBits is a bitfield and contains one bit set for every
2568 * supported memory type for the resource. The bit `1<<i` is set
2569 * if and only if the memory type `i` in the
2570 * VkPhysicalDeviceMemoryProperties structure for the physical
2571 * device is supported.
2573 * All types are currently supported for images.
2575 pMemoryRequirements
->memoryRequirements
.memoryTypeBits
=
2576 (1ull << pdevice
->memory
.type_count
) - 1;
2578 pMemoryRequirements
->memoryRequirements
.size
= image
->planes
[plane
].size
;
2579 pMemoryRequirements
->memoryRequirements
.alignment
=
2580 image
->planes
[plane
].alignment
;
2585 anv_debug_ignored_stype(ext
->sType
);
2590 vk_foreach_struct(ext
, pMemoryRequirements
->pNext
) {
2591 switch (ext
->sType
) {
2592 case VK_STRUCTURE_TYPE_MEMORY_DEDICATED_REQUIREMENTS
: {
2593 VkMemoryDedicatedRequirements
*requirements
= (void *)ext
;
2594 if (image
->needs_set_tiling
) {
2595 /* If we need to set the tiling for external consumers, we need a
2596 * dedicated allocation.
2598 * See also anv_AllocateMemory.
2600 requirements
->prefersDedicatedAllocation
= VK_TRUE
;
2601 requirements
->requiresDedicatedAllocation
= VK_TRUE
;
2603 requirements
->prefersDedicatedAllocation
= VK_FALSE
;
2604 requirements
->requiresDedicatedAllocation
= VK_FALSE
;
2610 anv_debug_ignored_stype(ext
->sType
);
2616 void anv_GetImageSparseMemoryRequirements(
2619 uint32_t* pSparseMemoryRequirementCount
,
2620 VkSparseImageMemoryRequirements
* pSparseMemoryRequirements
)
2622 *pSparseMemoryRequirementCount
= 0;
2625 void anv_GetImageSparseMemoryRequirements2(
2627 const VkImageSparseMemoryRequirementsInfo2
* pInfo
,
2628 uint32_t* pSparseMemoryRequirementCount
,
2629 VkSparseImageMemoryRequirements2
* pSparseMemoryRequirements
)
2631 *pSparseMemoryRequirementCount
= 0;
2634 void anv_GetDeviceMemoryCommitment(
2636 VkDeviceMemory memory
,
2637 VkDeviceSize
* pCommittedMemoryInBytes
)
2639 *pCommittedMemoryInBytes
= 0;
2643 anv_bind_buffer_memory(const VkBindBufferMemoryInfo
*pBindInfo
)
2645 ANV_FROM_HANDLE(anv_device_memory
, mem
, pBindInfo
->memory
);
2646 ANV_FROM_HANDLE(anv_buffer
, buffer
, pBindInfo
->buffer
);
2648 assert(pBindInfo
->sType
== VK_STRUCTURE_TYPE_BIND_BUFFER_MEMORY_INFO
);
2651 assert((buffer
->usage
& mem
->type
->valid_buffer_usage
) == buffer
->usage
);
2652 buffer
->address
= (struct anv_address
) {
2654 .offset
= pBindInfo
->memoryOffset
,
2657 buffer
->address
= ANV_NULL_ADDRESS
;
2661 VkResult
anv_BindBufferMemory(
2664 VkDeviceMemory memory
,
2665 VkDeviceSize memoryOffset
)
2667 anv_bind_buffer_memory(
2668 &(VkBindBufferMemoryInfo
) {
2669 .sType
= VK_STRUCTURE_TYPE_BIND_BUFFER_MEMORY_INFO
,
2672 .memoryOffset
= memoryOffset
,
2678 VkResult
anv_BindBufferMemory2(
2680 uint32_t bindInfoCount
,
2681 const VkBindBufferMemoryInfo
* pBindInfos
)
2683 for (uint32_t i
= 0; i
< bindInfoCount
; i
++)
2684 anv_bind_buffer_memory(&pBindInfos
[i
]);
2689 VkResult
anv_QueueBindSparse(
2691 uint32_t bindInfoCount
,
2692 const VkBindSparseInfo
* pBindInfo
,
2695 ANV_FROM_HANDLE(anv_queue
, queue
, _queue
);
2696 if (unlikely(queue
->device
->lost
))
2697 return VK_ERROR_DEVICE_LOST
;
2699 return vk_error(VK_ERROR_FEATURE_NOT_PRESENT
);
2704 VkResult
anv_CreateEvent(
2706 const VkEventCreateInfo
* pCreateInfo
,
2707 const VkAllocationCallbacks
* pAllocator
,
2710 ANV_FROM_HANDLE(anv_device
, device
, _device
);
2711 struct anv_state state
;
2712 struct anv_event
*event
;
2714 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_EVENT_CREATE_INFO
);
2716 state
= anv_state_pool_alloc(&device
->dynamic_state_pool
,
2719 event
->state
= state
;
2720 event
->semaphore
= VK_EVENT_RESET
;
2722 if (!device
->info
.has_llc
) {
2723 /* Make sure the writes we're flushing have landed. */
2724 __builtin_ia32_mfence();
2725 __builtin_ia32_clflush(event
);
2728 *pEvent
= anv_event_to_handle(event
);
2733 void anv_DestroyEvent(
2736 const VkAllocationCallbacks
* pAllocator
)
2738 ANV_FROM_HANDLE(anv_device
, device
, _device
);
2739 ANV_FROM_HANDLE(anv_event
, event
, _event
);
2744 anv_state_pool_free(&device
->dynamic_state_pool
, event
->state
);
2747 VkResult
anv_GetEventStatus(
2751 ANV_FROM_HANDLE(anv_device
, device
, _device
);
2752 ANV_FROM_HANDLE(anv_event
, event
, _event
);
2754 if (unlikely(device
->lost
))
2755 return VK_ERROR_DEVICE_LOST
;
2757 if (!device
->info
.has_llc
) {
2758 /* Invalidate read cache before reading event written by GPU. */
2759 __builtin_ia32_clflush(event
);
2760 __builtin_ia32_mfence();
2764 return event
->semaphore
;
2767 VkResult
anv_SetEvent(
2771 ANV_FROM_HANDLE(anv_device
, device
, _device
);
2772 ANV_FROM_HANDLE(anv_event
, event
, _event
);
2774 event
->semaphore
= VK_EVENT_SET
;
2776 if (!device
->info
.has_llc
) {
2777 /* Make sure the writes we're flushing have landed. */
2778 __builtin_ia32_mfence();
2779 __builtin_ia32_clflush(event
);
2785 VkResult
anv_ResetEvent(
2789 ANV_FROM_HANDLE(anv_device
, device
, _device
);
2790 ANV_FROM_HANDLE(anv_event
, event
, _event
);
2792 event
->semaphore
= VK_EVENT_RESET
;
2794 if (!device
->info
.has_llc
) {
2795 /* Make sure the writes we're flushing have landed. */
2796 __builtin_ia32_mfence();
2797 __builtin_ia32_clflush(event
);
2805 VkResult
anv_CreateBuffer(
2807 const VkBufferCreateInfo
* pCreateInfo
,
2808 const VkAllocationCallbacks
* pAllocator
,
2811 ANV_FROM_HANDLE(anv_device
, device
, _device
);
2812 struct anv_buffer
*buffer
;
2814 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO
);
2816 buffer
= vk_alloc2(&device
->alloc
, pAllocator
, sizeof(*buffer
), 8,
2817 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
2819 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
2821 buffer
->size
= pCreateInfo
->size
;
2822 buffer
->usage
= pCreateInfo
->usage
;
2823 buffer
->address
= ANV_NULL_ADDRESS
;
2825 *pBuffer
= anv_buffer_to_handle(buffer
);
2830 void anv_DestroyBuffer(
2833 const VkAllocationCallbacks
* pAllocator
)
2835 ANV_FROM_HANDLE(anv_device
, device
, _device
);
2836 ANV_FROM_HANDLE(anv_buffer
, buffer
, _buffer
);
2841 vk_free2(&device
->alloc
, pAllocator
, buffer
);
2845 anv_fill_buffer_surface_state(struct anv_device
*device
, struct anv_state state
,
2846 enum isl_format format
,
2847 struct anv_address address
,
2848 uint32_t range
, uint32_t stride
)
2850 isl_buffer_fill_state(&device
->isl_dev
, state
.map
,
2851 .address
= anv_address_physical(address
),
2852 .mocs
= device
->default_mocs
,
2857 anv_state_flush(device
, state
);
2860 void anv_DestroySampler(
2863 const VkAllocationCallbacks
* pAllocator
)
2865 ANV_FROM_HANDLE(anv_device
, device
, _device
);
2866 ANV_FROM_HANDLE(anv_sampler
, sampler
, _sampler
);
2871 vk_free2(&device
->alloc
, pAllocator
, sampler
);
2874 VkResult
anv_CreateFramebuffer(
2876 const VkFramebufferCreateInfo
* pCreateInfo
,
2877 const VkAllocationCallbacks
* pAllocator
,
2878 VkFramebuffer
* pFramebuffer
)
2880 ANV_FROM_HANDLE(anv_device
, device
, _device
);
2881 struct anv_framebuffer
*framebuffer
;
2883 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO
);
2885 size_t size
= sizeof(*framebuffer
) +
2886 sizeof(struct anv_image_view
*) * pCreateInfo
->attachmentCount
;
2887 framebuffer
= vk_alloc2(&device
->alloc
, pAllocator
, size
, 8,
2888 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
2889 if (framebuffer
== NULL
)
2890 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
2892 framebuffer
->attachment_count
= pCreateInfo
->attachmentCount
;
2893 for (uint32_t i
= 0; i
< pCreateInfo
->attachmentCount
; i
++) {
2894 VkImageView _iview
= pCreateInfo
->pAttachments
[i
];
2895 framebuffer
->attachments
[i
] = anv_image_view_from_handle(_iview
);
2898 framebuffer
->width
= pCreateInfo
->width
;
2899 framebuffer
->height
= pCreateInfo
->height
;
2900 framebuffer
->layers
= pCreateInfo
->layers
;
2902 *pFramebuffer
= anv_framebuffer_to_handle(framebuffer
);
2907 void anv_DestroyFramebuffer(
2910 const VkAllocationCallbacks
* pAllocator
)
2912 ANV_FROM_HANDLE(anv_device
, device
, _device
);
2913 ANV_FROM_HANDLE(anv_framebuffer
, fb
, _fb
);
2918 vk_free2(&device
->alloc
, pAllocator
, fb
);
2921 /* vk_icd.h does not declare this function, so we declare it here to
2922 * suppress Wmissing-prototypes.
2924 PUBLIC VKAPI_ATTR VkResult VKAPI_CALL
2925 vk_icdNegotiateLoaderICDInterfaceVersion(uint32_t* pSupportedVersion
);
2927 PUBLIC VKAPI_ATTR VkResult VKAPI_CALL
2928 vk_icdNegotiateLoaderICDInterfaceVersion(uint32_t* pSupportedVersion
)
2930 /* For the full details on loader interface versioning, see
2931 * <https://github.com/KhronosGroup/Vulkan-LoaderAndValidationLayers/blob/master/loader/LoaderAndLayerInterface.md>.
2932 * What follows is a condensed summary, to help you navigate the large and
2933 * confusing official doc.
2935 * - Loader interface v0 is incompatible with later versions. We don't
2938 * - In loader interface v1:
2939 * - The first ICD entrypoint called by the loader is
2940 * vk_icdGetInstanceProcAddr(). The ICD must statically expose this
2942 * - The ICD must statically expose no other Vulkan symbol unless it is
2943 * linked with -Bsymbolic.
2944 * - Each dispatchable Vulkan handle created by the ICD must be
2945 * a pointer to a struct whose first member is VK_LOADER_DATA. The
2946 * ICD must initialize VK_LOADER_DATA.loadMagic to ICD_LOADER_MAGIC.
2947 * - The loader implements vkCreate{PLATFORM}SurfaceKHR() and
2948 * vkDestroySurfaceKHR(). The ICD must be capable of working with
2949 * such loader-managed surfaces.
2951 * - Loader interface v2 differs from v1 in:
2952 * - The first ICD entrypoint called by the loader is
2953 * vk_icdNegotiateLoaderICDInterfaceVersion(). The ICD must
2954 * statically expose this entrypoint.
2956 * - Loader interface v3 differs from v2 in:
2957 * - The ICD must implement vkCreate{PLATFORM}SurfaceKHR(),
2958 * vkDestroySurfaceKHR(), and other API which uses VKSurfaceKHR,
2959 * because the loader no longer does so.
2961 *pSupportedVersion
= MIN2(*pSupportedVersion
, 3u);