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"
42 #include "common/gen_defines.h"
44 #include "genxml/gen7_pack.h"
47 compiler_debug_log(void *data
, const char *fmt
, ...)
51 compiler_perf_log(void *data
, const char *fmt
, ...)
56 if (unlikely(INTEL_DEBUG
& DEBUG_PERF
))
57 intel_logd_v(fmt
, args
);
63 anv_compute_heap_size(int fd
, uint64_t gtt_size
, uint64_t *heap_size
)
65 /* Query the total ram from the system */
69 uint64_t total_ram
= (uint64_t)info
.totalram
* (uint64_t)info
.mem_unit
;
71 /* We don't want to burn too much ram with the GPU. If the user has 4GiB
72 * or less, we use at most half. If they have more than 4GiB, we use 3/4.
74 uint64_t available_ram
;
75 if (total_ram
<= 4ull * 1024ull * 1024ull * 1024ull)
76 available_ram
= total_ram
/ 2;
78 available_ram
= total_ram
* 3 / 4;
80 /* We also want to leave some padding for things we allocate in the driver,
81 * so don't go over 3/4 of the GTT either.
83 uint64_t available_gtt
= gtt_size
* 3 / 4;
85 *heap_size
= MIN2(available_ram
, available_gtt
);
91 anv_physical_device_init_heaps(struct anv_physical_device
*device
, int fd
)
94 if (anv_gem_get_context_param(fd
, 0, I915_CONTEXT_PARAM_GTT_SIZE
,
96 /* If, for whatever reason, we can't actually get the GTT size from the
97 * kernel (too old?) fall back to the aperture size.
99 anv_perf_warn(NULL
, NULL
,
100 "Failed to get I915_CONTEXT_PARAM_GTT_SIZE: %m");
102 if (anv_gem_get_aperture(fd
, >t_size
) == -1) {
103 return vk_errorf(NULL
, NULL
, VK_ERROR_INITIALIZATION_FAILED
,
104 "failed to get aperture size: %m");
108 device
->supports_48bit_addresses
= (device
->info
.gen
>= 8) &&
109 gtt_size
> (4ULL << 30 /* GiB */);
111 uint64_t heap_size
= 0;
112 VkResult result
= anv_compute_heap_size(fd
, gtt_size
, &heap_size
);
113 if (result
!= VK_SUCCESS
)
116 if (heap_size
> (2ull << 30) && !device
->supports_48bit_addresses
) {
117 /* When running with an overridden PCI ID, we may get a GTT size from
118 * the kernel that is greater than 2 GiB but the execbuf check for 48bit
119 * address support can still fail. Just clamp the address space size to
120 * 2 GiB if we don't have 48-bit support.
122 intel_logw("%s:%d: The kernel reported a GTT size larger than 2 GiB but "
123 "not support for 48-bit addresses",
125 heap_size
= 2ull << 30;
128 if (heap_size
<= 3ull * (1ull << 30)) {
129 /* In this case, everything fits nicely into the 32-bit address space,
130 * so there's no need for supporting 48bit addresses on client-allocated
133 device
->memory
.heap_count
= 1;
134 device
->memory
.heaps
[0] = (struct anv_memory_heap
) {
136 .flags
= VK_MEMORY_HEAP_DEVICE_LOCAL_BIT
,
137 .supports_48bit_addresses
= false,
140 /* Not everything will fit nicely into a 32-bit address space. In this
141 * case we need a 64-bit heap. Advertise a small 32-bit heap and a
142 * larger 48-bit heap. If we're in this case, then we have a total heap
143 * size larger than 3GiB which most likely means they have 8 GiB of
144 * video memory and so carving off 1 GiB for the 32-bit heap should be
147 const uint64_t heap_size_32bit
= 1ull << 30;
148 const uint64_t heap_size_48bit
= heap_size
- heap_size_32bit
;
150 assert(device
->supports_48bit_addresses
);
152 device
->memory
.heap_count
= 2;
153 device
->memory
.heaps
[0] = (struct anv_memory_heap
) {
154 .size
= heap_size_48bit
,
155 .flags
= VK_MEMORY_HEAP_DEVICE_LOCAL_BIT
,
156 .supports_48bit_addresses
= true,
158 device
->memory
.heaps
[1] = (struct anv_memory_heap
) {
159 .size
= heap_size_32bit
,
160 .flags
= VK_MEMORY_HEAP_DEVICE_LOCAL_BIT
,
161 .supports_48bit_addresses
= false,
165 uint32_t type_count
= 0;
166 for (uint32_t heap
= 0; heap
< device
->memory
.heap_count
; heap
++) {
167 uint32_t valid_buffer_usage
= ~0;
169 /* There appears to be a hardware issue in the VF cache where it only
170 * considers the bottom 32 bits of memory addresses. If you happen to
171 * have two vertex buffers which get placed exactly 4 GiB apart and use
172 * them in back-to-back draw calls, you can get collisions. In order to
173 * solve this problem, we require vertex and index buffers be bound to
174 * memory allocated out of the 32-bit heap.
176 if (device
->memory
.heaps
[heap
].supports_48bit_addresses
) {
177 valid_buffer_usage
&= ~(VK_BUFFER_USAGE_INDEX_BUFFER_BIT
|
178 VK_BUFFER_USAGE_VERTEX_BUFFER_BIT
);
181 if (device
->info
.has_llc
) {
182 /* Big core GPUs share LLC with the CPU and thus one memory type can be
183 * both cached and coherent at the same time.
185 device
->memory
.types
[type_count
++] = (struct anv_memory_type
) {
186 .propertyFlags
= VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT
|
187 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT
|
188 VK_MEMORY_PROPERTY_HOST_COHERENT_BIT
|
189 VK_MEMORY_PROPERTY_HOST_CACHED_BIT
,
191 .valid_buffer_usage
= valid_buffer_usage
,
194 /* The spec requires that we expose a host-visible, coherent memory
195 * type, but Atom GPUs don't share LLC. Thus we offer two memory types
196 * to give the application a choice between cached, but not coherent and
197 * coherent but uncached (WC though).
199 device
->memory
.types
[type_count
++] = (struct anv_memory_type
) {
200 .propertyFlags
= VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT
|
201 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT
|
202 VK_MEMORY_PROPERTY_HOST_COHERENT_BIT
,
204 .valid_buffer_usage
= valid_buffer_usage
,
206 device
->memory
.types
[type_count
++] = (struct anv_memory_type
) {
207 .propertyFlags
= VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT
|
208 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT
|
209 VK_MEMORY_PROPERTY_HOST_CACHED_BIT
,
211 .valid_buffer_usage
= valid_buffer_usage
,
215 device
->memory
.type_count
= type_count
;
221 anv_physical_device_init_uuids(struct anv_physical_device
*device
)
223 const struct build_id_note
*note
=
224 build_id_find_nhdr_for_addr(anv_physical_device_init_uuids
);
226 return vk_errorf(device
->instance
, device
,
227 VK_ERROR_INITIALIZATION_FAILED
,
228 "Failed to find build-id");
231 unsigned build_id_len
= build_id_length(note
);
232 if (build_id_len
< 20) {
233 return vk_errorf(device
->instance
, device
,
234 VK_ERROR_INITIALIZATION_FAILED
,
235 "build-id too short. It needs to be a SHA");
238 memcpy(device
->driver_build_sha1
, build_id_data(note
), 20);
240 struct mesa_sha1 sha1_ctx
;
242 STATIC_ASSERT(VK_UUID_SIZE
<= sizeof(sha1
));
244 /* The pipeline cache UUID is used for determining when a pipeline cache is
245 * invalid. It needs both a driver build and the PCI ID of the device.
247 _mesa_sha1_init(&sha1_ctx
);
248 _mesa_sha1_update(&sha1_ctx
, build_id_data(note
), build_id_len
);
249 _mesa_sha1_update(&sha1_ctx
, &device
->chipset_id
,
250 sizeof(device
->chipset_id
));
251 _mesa_sha1_final(&sha1_ctx
, sha1
);
252 memcpy(device
->pipeline_cache_uuid
, sha1
, VK_UUID_SIZE
);
254 /* The driver UUID is used for determining sharability of images and memory
255 * between two Vulkan instances in separate processes. People who want to
256 * share memory need to also check the device UUID (below) so all this
257 * needs to be is the build-id.
259 memcpy(device
->driver_uuid
, build_id_data(note
), VK_UUID_SIZE
);
261 /* The device UUID uniquely identifies the given device within the machine.
262 * Since we never have more than one device, this doesn't need to be a real
263 * UUID. However, on the off-chance that someone tries to use this to
264 * cache pre-tiled images or something of the like, we use the PCI ID and
265 * some bits of ISL info to ensure that this is safe.
267 _mesa_sha1_init(&sha1_ctx
);
268 _mesa_sha1_update(&sha1_ctx
, &device
->chipset_id
,
269 sizeof(device
->chipset_id
));
270 _mesa_sha1_update(&sha1_ctx
, &device
->isl_dev
.has_bit6_swizzling
,
271 sizeof(device
->isl_dev
.has_bit6_swizzling
));
272 _mesa_sha1_final(&sha1_ctx
, sha1
);
273 memcpy(device
->device_uuid
, sha1
, VK_UUID_SIZE
);
279 anv_physical_device_init_disk_cache(struct anv_physical_device
*device
)
281 #ifdef ENABLE_SHADER_CACHE
283 MAYBE_UNUSED
int len
= snprintf(renderer
, sizeof(renderer
), "anv_%04x",
285 assert(len
== sizeof(renderer
) - 2);
288 _mesa_sha1_format(timestamp
, device
->driver_build_sha1
);
290 const uint64_t driver_flags
=
291 brw_get_compiler_config_value(device
->compiler
);
292 device
->disk_cache
= disk_cache_create(renderer
, timestamp
, driver_flags
);
294 device
->disk_cache
= NULL
;
299 anv_physical_device_free_disk_cache(struct anv_physical_device
*device
)
301 #ifdef ENABLE_SHADER_CACHE
302 if (device
->disk_cache
)
303 disk_cache_destroy(device
->disk_cache
);
305 assert(device
->disk_cache
== NULL
);
310 anv_physical_device_init(struct anv_physical_device
*device
,
311 struct anv_instance
*instance
,
312 const char *primary_path
,
319 brw_process_intel_debug_variable();
321 fd
= open(path
, O_RDWR
| O_CLOEXEC
);
323 return vk_error(VK_ERROR_INCOMPATIBLE_DRIVER
);
325 device
->_loader_data
.loaderMagic
= ICD_LOADER_MAGIC
;
326 device
->instance
= instance
;
328 assert(strlen(path
) < ARRAY_SIZE(device
->path
));
329 snprintf(device
->path
, ARRAY_SIZE(device
->path
), "%s", path
);
331 device
->no_hw
= getenv("INTEL_NO_HW") != NULL
;
333 const int pci_id_override
= gen_get_pci_device_id_override();
334 if (pci_id_override
< 0) {
335 device
->chipset_id
= anv_gem_get_param(fd
, I915_PARAM_CHIPSET_ID
);
336 if (!device
->chipset_id
) {
337 result
= vk_error(VK_ERROR_INCOMPATIBLE_DRIVER
);
341 device
->chipset_id
= pci_id_override
;
342 device
->no_hw
= true;
345 device
->name
= gen_get_device_name(device
->chipset_id
);
346 if (!gen_get_device_info(device
->chipset_id
, &device
->info
)) {
347 result
= vk_error(VK_ERROR_INCOMPATIBLE_DRIVER
);
351 if (device
->info
.is_haswell
) {
352 intel_logw("Haswell Vulkan support is incomplete");
353 } else if (device
->info
.gen
== 7 && !device
->info
.is_baytrail
) {
354 intel_logw("Ivy Bridge Vulkan support is incomplete");
355 } else if (device
->info
.gen
== 7 && device
->info
.is_baytrail
) {
356 intel_logw("Bay Trail Vulkan support is incomplete");
357 } else if (device
->info
.gen
>= 8 && device
->info
.gen
<= 10) {
358 /* Gen8-10 fully supported */
359 } else if (device
->info
.gen
== 11) {
360 intel_logw("Vulkan is not yet fully supported on gen11.");
362 result
= vk_errorf(device
->instance
, device
,
363 VK_ERROR_INCOMPATIBLE_DRIVER
,
364 "Vulkan not yet supported on %s", device
->name
);
368 device
->cmd_parser_version
= -1;
369 if (device
->info
.gen
== 7) {
370 device
->cmd_parser_version
=
371 anv_gem_get_param(fd
, I915_PARAM_CMD_PARSER_VERSION
);
372 if (device
->cmd_parser_version
== -1) {
373 result
= vk_errorf(device
->instance
, device
,
374 VK_ERROR_INITIALIZATION_FAILED
,
375 "failed to get command parser version");
380 if (!anv_gem_get_param(fd
, I915_PARAM_HAS_WAIT_TIMEOUT
)) {
381 result
= vk_errorf(device
->instance
, device
,
382 VK_ERROR_INITIALIZATION_FAILED
,
383 "kernel missing gem wait");
387 if (!anv_gem_get_param(fd
, I915_PARAM_HAS_EXECBUF2
)) {
388 result
= vk_errorf(device
->instance
, device
,
389 VK_ERROR_INITIALIZATION_FAILED
,
390 "kernel missing execbuf2");
394 if (!device
->info
.has_llc
&&
395 anv_gem_get_param(fd
, I915_PARAM_MMAP_VERSION
) < 1) {
396 result
= vk_errorf(device
->instance
, device
,
397 VK_ERROR_INITIALIZATION_FAILED
,
398 "kernel missing wc mmap");
402 result
= anv_physical_device_init_heaps(device
, fd
);
403 if (result
!= VK_SUCCESS
)
406 device
->has_exec_async
= anv_gem_get_param(fd
, I915_PARAM_HAS_EXEC_ASYNC
);
407 device
->has_exec_capture
= anv_gem_get_param(fd
, I915_PARAM_HAS_EXEC_CAPTURE
);
408 device
->has_exec_fence
= anv_gem_get_param(fd
, I915_PARAM_HAS_EXEC_FENCE
);
409 device
->has_syncobj
= anv_gem_get_param(fd
, I915_PARAM_HAS_EXEC_FENCE_ARRAY
);
410 device
->has_syncobj_wait
= device
->has_syncobj
&&
411 anv_gem_supports_syncobj_wait(fd
);
412 device
->has_context_priority
= anv_gem_has_context_priority(fd
);
414 device
->use_softpin
= anv_gem_get_param(fd
, I915_PARAM_HAS_EXEC_SOFTPIN
)
415 && device
->supports_48bit_addresses
;
417 device
->has_context_isolation
=
418 anv_gem_get_param(fd
, I915_PARAM_HAS_CONTEXT_ISOLATION
);
420 bool swizzled
= anv_gem_get_bit6_swizzle(fd
, I915_TILING_X
);
422 /* Starting with Gen10, the timestamp frequency of the command streamer may
423 * vary from one part to another. We can query the value from the kernel.
425 if (device
->info
.gen
>= 10) {
426 int timestamp_frequency
=
427 anv_gem_get_param(fd
, I915_PARAM_CS_TIMESTAMP_FREQUENCY
);
429 if (timestamp_frequency
< 0)
430 intel_logw("Kernel 4.16-rc1+ required to properly query CS timestamp frequency");
432 device
->info
.timestamp_frequency
= timestamp_frequency
;
435 /* GENs prior to 8 do not support EU/Subslice info */
436 if (device
->info
.gen
>= 8) {
437 device
->subslice_total
= anv_gem_get_param(fd
, I915_PARAM_SUBSLICE_TOTAL
);
438 device
->eu_total
= anv_gem_get_param(fd
, I915_PARAM_EU_TOTAL
);
440 /* Without this information, we cannot get the right Braswell
441 * brandstrings, and we have to use conservative numbers for GPGPU on
442 * many platforms, but otherwise, things will just work.
444 if (device
->subslice_total
< 1 || device
->eu_total
< 1) {
445 intel_logw("Kernel 4.1 required to properly query GPU properties");
447 } else if (device
->info
.gen
== 7) {
448 device
->subslice_total
= 1 << (device
->info
.gt
- 1);
451 if (device
->info
.is_cherryview
&&
452 device
->subslice_total
> 0 && device
->eu_total
> 0) {
453 /* Logical CS threads = EUs per subslice * num threads per EU */
454 uint32_t max_cs_threads
=
455 device
->eu_total
/ device
->subslice_total
* device
->info
.num_thread_per_eu
;
457 /* Fuse configurations may give more threads than expected, never less. */
458 if (max_cs_threads
> device
->info
.max_cs_threads
)
459 device
->info
.max_cs_threads
= max_cs_threads
;
462 device
->compiler
= brw_compiler_create(NULL
, &device
->info
);
463 if (device
->compiler
== NULL
) {
464 result
= vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
467 device
->compiler
->shader_debug_log
= compiler_debug_log
;
468 device
->compiler
->shader_perf_log
= compiler_perf_log
;
469 device
->compiler
->supports_pull_constants
= false;
470 device
->compiler
->constant_buffer_0_is_relative
=
471 device
->info
.gen
< 8 || !device
->has_context_isolation
;
472 device
->compiler
->supports_shader_constants
= true;
474 isl_device_init(&device
->isl_dev
, &device
->info
, swizzled
);
476 result
= anv_physical_device_init_uuids(device
);
477 if (result
!= VK_SUCCESS
)
480 anv_physical_device_init_disk_cache(device
);
482 if (instance
->enabled_extensions
.KHR_display
) {
483 master_fd
= open(primary_path
, O_RDWR
| O_CLOEXEC
);
484 if (master_fd
>= 0) {
485 /* prod the device with a GETPARAM call which will fail if
486 * we don't have permission to even render on this device
488 if (anv_gem_get_param(master_fd
, I915_PARAM_CHIPSET_ID
) == 0) {
494 device
->master_fd
= master_fd
;
496 result
= anv_init_wsi(device
);
497 if (result
!= VK_SUCCESS
) {
498 ralloc_free(device
->compiler
);
499 anv_physical_device_free_disk_cache(device
);
503 anv_physical_device_get_supported_extensions(device
,
504 &device
->supported_extensions
);
507 device
->local_fd
= fd
;
519 anv_physical_device_finish(struct anv_physical_device
*device
)
521 anv_finish_wsi(device
);
522 anv_physical_device_free_disk_cache(device
);
523 ralloc_free(device
->compiler
);
524 close(device
->local_fd
);
525 if (device
->master_fd
>= 0)
526 close(device
->master_fd
);
530 default_alloc_func(void *pUserData
, size_t size
, size_t align
,
531 VkSystemAllocationScope allocationScope
)
537 default_realloc_func(void *pUserData
, void *pOriginal
, size_t size
,
538 size_t align
, VkSystemAllocationScope allocationScope
)
540 return realloc(pOriginal
, size
);
544 default_free_func(void *pUserData
, void *pMemory
)
549 static const VkAllocationCallbacks default_alloc
= {
551 .pfnAllocation
= default_alloc_func
,
552 .pfnReallocation
= default_realloc_func
,
553 .pfnFree
= default_free_func
,
556 VkResult
anv_EnumerateInstanceExtensionProperties(
557 const char* pLayerName
,
558 uint32_t* pPropertyCount
,
559 VkExtensionProperties
* pProperties
)
561 VK_OUTARRAY_MAKE(out
, pProperties
, pPropertyCount
);
563 for (int i
= 0; i
< ANV_INSTANCE_EXTENSION_COUNT
; i
++) {
564 if (anv_instance_extensions_supported
.extensions
[i
]) {
565 vk_outarray_append(&out
, prop
) {
566 *prop
= anv_instance_extensions
[i
];
571 return vk_outarray_status(&out
);
574 VkResult
anv_CreateInstance(
575 const VkInstanceCreateInfo
* pCreateInfo
,
576 const VkAllocationCallbacks
* pAllocator
,
577 VkInstance
* pInstance
)
579 struct anv_instance
*instance
;
582 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO
);
584 struct anv_instance_extension_table enabled_extensions
= {};
585 for (uint32_t i
= 0; i
< pCreateInfo
->enabledExtensionCount
; i
++) {
587 for (idx
= 0; idx
< ANV_INSTANCE_EXTENSION_COUNT
; idx
++) {
588 if (strcmp(pCreateInfo
->ppEnabledExtensionNames
[i
],
589 anv_instance_extensions
[idx
].extensionName
) == 0)
593 if (idx
>= ANV_INSTANCE_EXTENSION_COUNT
)
594 return vk_error(VK_ERROR_EXTENSION_NOT_PRESENT
);
596 if (!anv_instance_extensions_supported
.extensions
[idx
])
597 return vk_error(VK_ERROR_EXTENSION_NOT_PRESENT
);
599 enabled_extensions
.extensions
[idx
] = true;
602 instance
= vk_alloc2(&default_alloc
, pAllocator
, sizeof(*instance
), 8,
603 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE
);
605 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
607 instance
->_loader_data
.loaderMagic
= ICD_LOADER_MAGIC
;
610 instance
->alloc
= *pAllocator
;
612 instance
->alloc
= default_alloc
;
614 instance
->app_info
= (struct anv_app_info
) { .api_version
= 0 };
615 if (pCreateInfo
->pApplicationInfo
) {
616 const VkApplicationInfo
*app
= pCreateInfo
->pApplicationInfo
;
618 instance
->app_info
.app_name
=
619 vk_strdup(&instance
->alloc
, app
->pApplicationName
,
620 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE
);
621 instance
->app_info
.app_version
= app
->applicationVersion
;
623 instance
->app_info
.engine_name
=
624 vk_strdup(&instance
->alloc
, app
->pEngineName
,
625 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE
);
626 instance
->app_info
.engine_version
= app
->engineVersion
;
628 instance
->app_info
.api_version
= app
->apiVersion
;
631 if (instance
->app_info
.api_version
== 0)
632 anv_EnumerateInstanceVersion(&instance
->app_info
.api_version
);
634 instance
->enabled_extensions
= enabled_extensions
;
636 for (unsigned i
= 0; i
< ARRAY_SIZE(instance
->dispatch
.entrypoints
); i
++) {
637 /* Vulkan requires that entrypoints for extensions which have not been
638 * enabled must not be advertised.
640 if (!anv_entrypoint_is_enabled(i
, instance
->app_info
.api_version
,
641 &instance
->enabled_extensions
, NULL
)) {
642 instance
->dispatch
.entrypoints
[i
] = NULL
;
643 } else if (anv_dispatch_table
.entrypoints
[i
] != NULL
) {
644 instance
->dispatch
.entrypoints
[i
] = anv_dispatch_table
.entrypoints
[i
];
646 instance
->dispatch
.entrypoints
[i
] =
647 anv_tramp_dispatch_table
.entrypoints
[i
];
651 instance
->physicalDeviceCount
= -1;
653 result
= vk_debug_report_instance_init(&instance
->debug_report_callbacks
);
654 if (result
!= VK_SUCCESS
) {
655 vk_free2(&default_alloc
, pAllocator
, instance
);
656 return vk_error(result
);
659 instance
->pipeline_cache_enabled
=
660 env_var_as_boolean("ANV_ENABLE_PIPELINE_CACHE", true);
664 VG(VALGRIND_CREATE_MEMPOOL(instance
, 0, false));
666 *pInstance
= anv_instance_to_handle(instance
);
671 void anv_DestroyInstance(
672 VkInstance _instance
,
673 const VkAllocationCallbacks
* pAllocator
)
675 ANV_FROM_HANDLE(anv_instance
, instance
, _instance
);
680 if (instance
->physicalDeviceCount
> 0) {
681 /* We support at most one physical device. */
682 assert(instance
->physicalDeviceCount
== 1);
683 anv_physical_device_finish(&instance
->physicalDevice
);
686 vk_free(&instance
->alloc
, (char *)instance
->app_info
.app_name
);
687 vk_free(&instance
->alloc
, (char *)instance
->app_info
.engine_name
);
689 VG(VALGRIND_DESTROY_MEMPOOL(instance
));
691 vk_debug_report_instance_destroy(&instance
->debug_report_callbacks
);
695 vk_free(&instance
->alloc
, instance
);
699 anv_enumerate_devices(struct anv_instance
*instance
)
701 /* TODO: Check for more devices ? */
702 drmDevicePtr devices
[8];
703 VkResult result
= VK_ERROR_INCOMPATIBLE_DRIVER
;
706 instance
->physicalDeviceCount
= 0;
708 max_devices
= drmGetDevices2(0, devices
, ARRAY_SIZE(devices
));
710 return VK_ERROR_INCOMPATIBLE_DRIVER
;
712 for (unsigned i
= 0; i
< (unsigned)max_devices
; i
++) {
713 if (devices
[i
]->available_nodes
& 1 << DRM_NODE_RENDER
&&
714 devices
[i
]->bustype
== DRM_BUS_PCI
&&
715 devices
[i
]->deviceinfo
.pci
->vendor_id
== 0x8086) {
717 result
= anv_physical_device_init(&instance
->physicalDevice
,
719 devices
[i
]->nodes
[DRM_NODE_PRIMARY
],
720 devices
[i
]->nodes
[DRM_NODE_RENDER
]);
721 if (result
!= VK_ERROR_INCOMPATIBLE_DRIVER
)
725 drmFreeDevices(devices
, max_devices
);
727 if (result
== VK_SUCCESS
)
728 instance
->physicalDeviceCount
= 1;
734 anv_instance_ensure_physical_device(struct anv_instance
*instance
)
736 if (instance
->physicalDeviceCount
< 0) {
737 VkResult result
= anv_enumerate_devices(instance
);
738 if (result
!= VK_SUCCESS
&&
739 result
!= VK_ERROR_INCOMPATIBLE_DRIVER
)
746 VkResult
anv_EnumeratePhysicalDevices(
747 VkInstance _instance
,
748 uint32_t* pPhysicalDeviceCount
,
749 VkPhysicalDevice
* pPhysicalDevices
)
751 ANV_FROM_HANDLE(anv_instance
, instance
, _instance
);
752 VK_OUTARRAY_MAKE(out
, pPhysicalDevices
, pPhysicalDeviceCount
);
754 VkResult result
= anv_instance_ensure_physical_device(instance
);
755 if (result
!= VK_SUCCESS
)
758 if (instance
->physicalDeviceCount
== 0)
761 assert(instance
->physicalDeviceCount
== 1);
762 vk_outarray_append(&out
, i
) {
763 *i
= anv_physical_device_to_handle(&instance
->physicalDevice
);
766 return vk_outarray_status(&out
);
769 VkResult
anv_EnumeratePhysicalDeviceGroups(
770 VkInstance _instance
,
771 uint32_t* pPhysicalDeviceGroupCount
,
772 VkPhysicalDeviceGroupProperties
* pPhysicalDeviceGroupProperties
)
774 ANV_FROM_HANDLE(anv_instance
, instance
, _instance
);
775 VK_OUTARRAY_MAKE(out
, pPhysicalDeviceGroupProperties
,
776 pPhysicalDeviceGroupCount
);
778 VkResult result
= anv_instance_ensure_physical_device(instance
);
779 if (result
!= VK_SUCCESS
)
782 if (instance
->physicalDeviceCount
== 0)
785 assert(instance
->physicalDeviceCount
== 1);
787 vk_outarray_append(&out
, p
) {
788 p
->physicalDeviceCount
= 1;
789 memset(p
->physicalDevices
, 0, sizeof(p
->physicalDevices
));
790 p
->physicalDevices
[0] =
791 anv_physical_device_to_handle(&instance
->physicalDevice
);
792 p
->subsetAllocation
= VK_FALSE
;
794 vk_foreach_struct(ext
, p
->pNext
)
795 anv_debug_ignored_stype(ext
->sType
);
798 return vk_outarray_status(&out
);
801 void anv_GetPhysicalDeviceFeatures(
802 VkPhysicalDevice physicalDevice
,
803 VkPhysicalDeviceFeatures
* pFeatures
)
805 ANV_FROM_HANDLE(anv_physical_device
, pdevice
, physicalDevice
);
807 *pFeatures
= (VkPhysicalDeviceFeatures
) {
808 .robustBufferAccess
= true,
809 .fullDrawIndexUint32
= true,
810 .imageCubeArray
= true,
811 .independentBlend
= true,
812 .geometryShader
= true,
813 .tessellationShader
= true,
814 .sampleRateShading
= true,
815 .dualSrcBlend
= true,
817 .multiDrawIndirect
= true,
818 .drawIndirectFirstInstance
= true,
820 .depthBiasClamp
= true,
821 .fillModeNonSolid
= true,
822 .depthBounds
= false,
826 .multiViewport
= true,
827 .samplerAnisotropy
= true,
828 .textureCompressionETC2
= pdevice
->info
.gen
>= 8 ||
829 pdevice
->info
.is_baytrail
,
830 .textureCompressionASTC_LDR
= pdevice
->info
.gen
>= 9, /* FINISHME CHV */
831 .textureCompressionBC
= true,
832 .occlusionQueryPrecise
= true,
833 .pipelineStatisticsQuery
= true,
834 .fragmentStoresAndAtomics
= true,
835 .shaderTessellationAndGeometryPointSize
= true,
836 .shaderImageGatherExtended
= true,
837 .shaderStorageImageExtendedFormats
= true,
838 .shaderStorageImageMultisample
= false,
839 .shaderStorageImageReadWithoutFormat
= false,
840 .shaderStorageImageWriteWithoutFormat
= true,
841 .shaderUniformBufferArrayDynamicIndexing
= true,
842 .shaderSampledImageArrayDynamicIndexing
= true,
843 .shaderStorageBufferArrayDynamicIndexing
= true,
844 .shaderStorageImageArrayDynamicIndexing
= true,
845 .shaderClipDistance
= true,
846 .shaderCullDistance
= true,
847 .shaderFloat64
= pdevice
->info
.gen
>= 8 &&
848 pdevice
->info
.has_64bit_types
,
849 .shaderInt64
= pdevice
->info
.gen
>= 8 &&
850 pdevice
->info
.has_64bit_types
,
851 .shaderInt16
= pdevice
->info
.gen
>= 8,
852 .shaderResourceMinLod
= false,
853 .variableMultisampleRate
= true,
854 .inheritedQueries
= true,
857 /* We can't do image stores in vec4 shaders */
858 pFeatures
->vertexPipelineStoresAndAtomics
=
859 pdevice
->compiler
->scalar_stage
[MESA_SHADER_VERTEX
] &&
860 pdevice
->compiler
->scalar_stage
[MESA_SHADER_GEOMETRY
];
862 struct anv_app_info
*app_info
= &pdevice
->instance
->app_info
;
864 /* The new DOOM and Wolfenstein games require depthBounds without
865 * checking for it. They seem to run fine without it so just claim it's
866 * there and accept the consequences.
868 if (app_info
->engine_name
&& strcmp(app_info
->engine_name
, "idTech") == 0)
869 pFeatures
->depthBounds
= true;
872 void anv_GetPhysicalDeviceFeatures2(
873 VkPhysicalDevice physicalDevice
,
874 VkPhysicalDeviceFeatures2
* pFeatures
)
876 anv_GetPhysicalDeviceFeatures(physicalDevice
, &pFeatures
->features
);
878 vk_foreach_struct(ext
, pFeatures
->pNext
) {
879 switch (ext
->sType
) {
880 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PROTECTED_MEMORY_FEATURES
: {
881 VkPhysicalDeviceProtectedMemoryFeatures
*features
= (void *)ext
;
882 features
->protectedMemory
= VK_FALSE
;
886 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_MULTIVIEW_FEATURES
: {
887 VkPhysicalDeviceMultiviewFeatures
*features
=
888 (VkPhysicalDeviceMultiviewFeatures
*)ext
;
889 features
->multiview
= true;
890 features
->multiviewGeometryShader
= true;
891 features
->multiviewTessellationShader
= true;
895 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VARIABLE_POINTER_FEATURES
: {
896 VkPhysicalDeviceVariablePointerFeatures
*features
= (void *)ext
;
897 features
->variablePointersStorageBuffer
= true;
898 features
->variablePointers
= true;
902 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SAMPLER_YCBCR_CONVERSION_FEATURES
: {
903 VkPhysicalDeviceSamplerYcbcrConversionFeatures
*features
=
904 (VkPhysicalDeviceSamplerYcbcrConversionFeatures
*) ext
;
905 features
->samplerYcbcrConversion
= true;
909 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SHADER_DRAW_PARAMETER_FEATURES
: {
910 VkPhysicalDeviceShaderDrawParameterFeatures
*features
= (void *)ext
;
911 features
->shaderDrawParameters
= true;
915 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_16BIT_STORAGE_FEATURES_KHR
: {
916 VkPhysicalDevice16BitStorageFeaturesKHR
*features
=
917 (VkPhysicalDevice16BitStorageFeaturesKHR
*)ext
;
918 ANV_FROM_HANDLE(anv_physical_device
, pdevice
, physicalDevice
);
920 features
->storageBuffer16BitAccess
= pdevice
->info
.gen
>= 8;
921 features
->uniformAndStorageBuffer16BitAccess
= pdevice
->info
.gen
>= 8;
922 features
->storagePushConstant16
= pdevice
->info
.gen
>= 8;
923 features
->storageInputOutput16
= false;
927 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_8BIT_STORAGE_FEATURES_KHR
: {
928 VkPhysicalDevice8BitStorageFeaturesKHR
*features
=
929 (VkPhysicalDevice8BitStorageFeaturesKHR
*)ext
;
930 ANV_FROM_HANDLE(anv_physical_device
, pdevice
, physicalDevice
);
932 features
->storageBuffer8BitAccess
= pdevice
->info
.gen
>= 8;
933 features
->uniformAndStorageBuffer8BitAccess
= pdevice
->info
.gen
>= 8;
934 features
->storagePushConstant8
= pdevice
->info
.gen
>= 8;
938 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VERTEX_ATTRIBUTE_DIVISOR_FEATURES_EXT
: {
939 VkPhysicalDeviceVertexAttributeDivisorFeaturesEXT
*features
=
940 (VkPhysicalDeviceVertexAttributeDivisorFeaturesEXT
*)ext
;
941 features
->vertexAttributeInstanceRateDivisor
= VK_TRUE
;
942 features
->vertexAttributeInstanceRateZeroDivisor
= VK_TRUE
;
947 anv_debug_ignored_stype(ext
->sType
);
953 void anv_GetPhysicalDeviceProperties(
954 VkPhysicalDevice physicalDevice
,
955 VkPhysicalDeviceProperties
* pProperties
)
957 ANV_FROM_HANDLE(anv_physical_device
, pdevice
, physicalDevice
);
958 const struct gen_device_info
*devinfo
= &pdevice
->info
;
960 /* See assertions made when programming the buffer surface state. */
961 const uint32_t max_raw_buffer_sz
= devinfo
->gen
>= 7 ?
962 (1ul << 30) : (1ul << 27);
964 const uint32_t max_samplers
= (devinfo
->gen
>= 8 || devinfo
->is_haswell
) ?
967 VkSampleCountFlags sample_counts
=
968 isl_device_get_sample_counts(&pdevice
->isl_dev
);
970 VkPhysicalDeviceLimits limits
= {
971 .maxImageDimension1D
= (1 << 14),
972 .maxImageDimension2D
= (1 << 14),
973 .maxImageDimension3D
= (1 << 11),
974 .maxImageDimensionCube
= (1 << 14),
975 .maxImageArrayLayers
= (1 << 11),
976 .maxTexelBufferElements
= 128 * 1024 * 1024,
977 .maxUniformBufferRange
= (1ul << 27),
978 .maxStorageBufferRange
= max_raw_buffer_sz
,
979 .maxPushConstantsSize
= MAX_PUSH_CONSTANTS_SIZE
,
980 .maxMemoryAllocationCount
= UINT32_MAX
,
981 .maxSamplerAllocationCount
= 64 * 1024,
982 .bufferImageGranularity
= 64, /* A cache line */
983 .sparseAddressSpaceSize
= 0,
984 .maxBoundDescriptorSets
= MAX_SETS
,
985 .maxPerStageDescriptorSamplers
= max_samplers
,
986 .maxPerStageDescriptorUniformBuffers
= 64,
987 .maxPerStageDescriptorStorageBuffers
= 64,
988 .maxPerStageDescriptorSampledImages
= max_samplers
,
989 .maxPerStageDescriptorStorageImages
= 64,
990 .maxPerStageDescriptorInputAttachments
= 64,
991 .maxPerStageResources
= 250,
992 .maxDescriptorSetSamplers
= 6 * max_samplers
, /* number of stages * maxPerStageDescriptorSamplers */
993 .maxDescriptorSetUniformBuffers
= 6 * 64, /* number of stages * maxPerStageDescriptorUniformBuffers */
994 .maxDescriptorSetUniformBuffersDynamic
= MAX_DYNAMIC_BUFFERS
/ 2,
995 .maxDescriptorSetStorageBuffers
= 6 * 64, /* number of stages * maxPerStageDescriptorStorageBuffers */
996 .maxDescriptorSetStorageBuffersDynamic
= MAX_DYNAMIC_BUFFERS
/ 2,
997 .maxDescriptorSetSampledImages
= 6 * max_samplers
, /* number of stages * maxPerStageDescriptorSampledImages */
998 .maxDescriptorSetStorageImages
= 6 * 64, /* number of stages * maxPerStageDescriptorStorageImages */
999 .maxDescriptorSetInputAttachments
= 256,
1000 .maxVertexInputAttributes
= MAX_VBS
,
1001 .maxVertexInputBindings
= MAX_VBS
,
1002 .maxVertexInputAttributeOffset
= 2047,
1003 .maxVertexInputBindingStride
= 2048,
1004 .maxVertexOutputComponents
= 128,
1005 .maxTessellationGenerationLevel
= 64,
1006 .maxTessellationPatchSize
= 32,
1007 .maxTessellationControlPerVertexInputComponents
= 128,
1008 .maxTessellationControlPerVertexOutputComponents
= 128,
1009 .maxTessellationControlPerPatchOutputComponents
= 128,
1010 .maxTessellationControlTotalOutputComponents
= 2048,
1011 .maxTessellationEvaluationInputComponents
= 128,
1012 .maxTessellationEvaluationOutputComponents
= 128,
1013 .maxGeometryShaderInvocations
= 32,
1014 .maxGeometryInputComponents
= 64,
1015 .maxGeometryOutputComponents
= 128,
1016 .maxGeometryOutputVertices
= 256,
1017 .maxGeometryTotalOutputComponents
= 1024,
1018 .maxFragmentInputComponents
= 112, /* 128 components - (POS, PSIZ, CLIP_DIST0, CLIP_DIST1) */
1019 .maxFragmentOutputAttachments
= 8,
1020 .maxFragmentDualSrcAttachments
= 1,
1021 .maxFragmentCombinedOutputResources
= 8,
1022 .maxComputeSharedMemorySize
= 32768,
1023 .maxComputeWorkGroupCount
= { 65535, 65535, 65535 },
1024 .maxComputeWorkGroupInvocations
= 16 * devinfo
->max_cs_threads
,
1025 .maxComputeWorkGroupSize
= {
1026 16 * devinfo
->max_cs_threads
,
1027 16 * devinfo
->max_cs_threads
,
1028 16 * devinfo
->max_cs_threads
,
1030 .subPixelPrecisionBits
= 4 /* FIXME */,
1031 .subTexelPrecisionBits
= 4 /* FIXME */,
1032 .mipmapPrecisionBits
= 4 /* FIXME */,
1033 .maxDrawIndexedIndexValue
= UINT32_MAX
,
1034 .maxDrawIndirectCount
= UINT32_MAX
,
1035 .maxSamplerLodBias
= 16,
1036 .maxSamplerAnisotropy
= 16,
1037 .maxViewports
= MAX_VIEWPORTS
,
1038 .maxViewportDimensions
= { (1 << 14), (1 << 14) },
1039 .viewportBoundsRange
= { INT16_MIN
, INT16_MAX
},
1040 .viewportSubPixelBits
= 13, /* We take a float? */
1041 .minMemoryMapAlignment
= 4096, /* A page */
1042 .minTexelBufferOffsetAlignment
= 1,
1043 /* We need 16 for UBO block reads to work and 32 for push UBOs */
1044 .minUniformBufferOffsetAlignment
= 32,
1045 .minStorageBufferOffsetAlignment
= 4,
1046 .minTexelOffset
= -8,
1047 .maxTexelOffset
= 7,
1048 .minTexelGatherOffset
= -32,
1049 .maxTexelGatherOffset
= 31,
1050 .minInterpolationOffset
= -0.5,
1051 .maxInterpolationOffset
= 0.4375,
1052 .subPixelInterpolationOffsetBits
= 4,
1053 .maxFramebufferWidth
= (1 << 14),
1054 .maxFramebufferHeight
= (1 << 14),
1055 .maxFramebufferLayers
= (1 << 11),
1056 .framebufferColorSampleCounts
= sample_counts
,
1057 .framebufferDepthSampleCounts
= sample_counts
,
1058 .framebufferStencilSampleCounts
= sample_counts
,
1059 .framebufferNoAttachmentsSampleCounts
= sample_counts
,
1060 .maxColorAttachments
= MAX_RTS
,
1061 .sampledImageColorSampleCounts
= sample_counts
,
1062 .sampledImageIntegerSampleCounts
= VK_SAMPLE_COUNT_1_BIT
,
1063 .sampledImageDepthSampleCounts
= sample_counts
,
1064 .sampledImageStencilSampleCounts
= sample_counts
,
1065 .storageImageSampleCounts
= VK_SAMPLE_COUNT_1_BIT
,
1066 .maxSampleMaskWords
= 1,
1067 .timestampComputeAndGraphics
= false,
1068 .timestampPeriod
= 1000000000.0 / devinfo
->timestamp_frequency
,
1069 .maxClipDistances
= 8,
1070 .maxCullDistances
= 8,
1071 .maxCombinedClipAndCullDistances
= 8,
1072 .discreteQueuePriorities
= 2,
1073 .pointSizeRange
= { 0.125, 255.875 },
1074 .lineWidthRange
= { 0.0, 7.9921875 },
1075 .pointSizeGranularity
= (1.0 / 8.0),
1076 .lineWidthGranularity
= (1.0 / 128.0),
1077 .strictLines
= false, /* FINISHME */
1078 .standardSampleLocations
= true,
1079 .optimalBufferCopyOffsetAlignment
= 128,
1080 .optimalBufferCopyRowPitchAlignment
= 128,
1081 .nonCoherentAtomSize
= 64,
1084 *pProperties
= (VkPhysicalDeviceProperties
) {
1085 .apiVersion
= anv_physical_device_api_version(pdevice
),
1086 .driverVersion
= vk_get_driver_version(),
1088 .deviceID
= pdevice
->chipset_id
,
1089 .deviceType
= VK_PHYSICAL_DEVICE_TYPE_INTEGRATED_GPU
,
1091 .sparseProperties
= {0}, /* Broadwell doesn't do sparse. */
1094 snprintf(pProperties
->deviceName
, sizeof(pProperties
->deviceName
),
1095 "%s", pdevice
->name
);
1096 memcpy(pProperties
->pipelineCacheUUID
,
1097 pdevice
->pipeline_cache_uuid
, VK_UUID_SIZE
);
1100 void anv_GetPhysicalDeviceProperties2(
1101 VkPhysicalDevice physicalDevice
,
1102 VkPhysicalDeviceProperties2
* pProperties
)
1104 ANV_FROM_HANDLE(anv_physical_device
, pdevice
, physicalDevice
);
1106 anv_GetPhysicalDeviceProperties(physicalDevice
, &pProperties
->properties
);
1108 vk_foreach_struct(ext
, pProperties
->pNext
) {
1109 switch (ext
->sType
) {
1110 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PUSH_DESCRIPTOR_PROPERTIES_KHR
: {
1111 VkPhysicalDevicePushDescriptorPropertiesKHR
*properties
=
1112 (VkPhysicalDevicePushDescriptorPropertiesKHR
*) ext
;
1114 properties
->maxPushDescriptors
= MAX_PUSH_DESCRIPTORS
;
1118 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_DRIVER_PROPERTIES_KHR
: {
1119 VkPhysicalDeviceDriverPropertiesKHR
*driver_props
=
1120 (VkPhysicalDeviceDriverPropertiesKHR
*) ext
;
1122 driver_props
->driverID
= VK_DRIVER_ID_INTEL_OPEN_SOURCE_MESA_KHR
;
1123 memset(driver_props
->driverName
, 0, VK_MAX_DRIVER_NAME_SIZE_KHR
);
1124 strcpy(driver_props
->driverName
,
1125 "Intel open-source Mesa driver");
1127 memset(driver_props
->driverInfo
, 0, VK_MAX_DRIVER_INFO_SIZE_KHR
);
1128 strcpy(driver_props
->driverInfo
,
1129 "Mesa " PACKAGE_VERSION MESA_GIT_SHA1
);
1131 driver_props
->conformanceVersion
= (VkConformanceVersionKHR
) {
1140 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_ID_PROPERTIES
: {
1141 VkPhysicalDeviceIDProperties
*id_props
=
1142 (VkPhysicalDeviceIDProperties
*)ext
;
1143 memcpy(id_props
->deviceUUID
, pdevice
->device_uuid
, VK_UUID_SIZE
);
1144 memcpy(id_props
->driverUUID
, pdevice
->driver_uuid
, VK_UUID_SIZE
);
1145 /* The LUID is for Windows. */
1146 id_props
->deviceLUIDValid
= false;
1150 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_MAINTENANCE_3_PROPERTIES
: {
1151 VkPhysicalDeviceMaintenance3Properties
*props
=
1152 (VkPhysicalDeviceMaintenance3Properties
*)ext
;
1153 /* This value doesn't matter for us today as our per-stage
1154 * descriptors are the real limit.
1156 props
->maxPerSetDescriptors
= 1024;
1157 props
->maxMemoryAllocationSize
= MAX_MEMORY_ALLOCATION_SIZE
;
1161 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_MULTIVIEW_PROPERTIES
: {
1162 VkPhysicalDeviceMultiviewProperties
*properties
=
1163 (VkPhysicalDeviceMultiviewProperties
*)ext
;
1164 properties
->maxMultiviewViewCount
= 16;
1165 properties
->maxMultiviewInstanceIndex
= UINT32_MAX
/ 16;
1169 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_POINT_CLIPPING_PROPERTIES
: {
1170 VkPhysicalDevicePointClippingProperties
*properties
=
1171 (VkPhysicalDevicePointClippingProperties
*) ext
;
1172 properties
->pointClippingBehavior
= VK_POINT_CLIPPING_BEHAVIOR_ALL_CLIP_PLANES
;
1173 anv_finishme("Implement pop-free point clipping");
1177 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SAMPLER_FILTER_MINMAX_PROPERTIES_EXT
: {
1178 VkPhysicalDeviceSamplerFilterMinmaxPropertiesEXT
*properties
=
1179 (VkPhysicalDeviceSamplerFilterMinmaxPropertiesEXT
*)ext
;
1180 properties
->filterMinmaxImageComponentMapping
= pdevice
->info
.gen
>= 9;
1181 properties
->filterMinmaxSingleComponentFormats
= true;
1185 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SUBGROUP_PROPERTIES
: {
1186 VkPhysicalDeviceSubgroupProperties
*properties
= (void *)ext
;
1188 properties
->subgroupSize
= BRW_SUBGROUP_SIZE
;
1190 VkShaderStageFlags scalar_stages
= 0;
1191 for (unsigned stage
= 0; stage
< MESA_SHADER_STAGES
; stage
++) {
1192 if (pdevice
->compiler
->scalar_stage
[stage
])
1193 scalar_stages
|= mesa_to_vk_shader_stage(stage
);
1195 properties
->supportedStages
= scalar_stages
;
1197 properties
->supportedOperations
= VK_SUBGROUP_FEATURE_BASIC_BIT
|
1198 VK_SUBGROUP_FEATURE_VOTE_BIT
|
1199 VK_SUBGROUP_FEATURE_ARITHMETIC_BIT
|
1200 VK_SUBGROUP_FEATURE_BALLOT_BIT
|
1201 VK_SUBGROUP_FEATURE_SHUFFLE_BIT
|
1202 VK_SUBGROUP_FEATURE_SHUFFLE_RELATIVE_BIT
|
1203 VK_SUBGROUP_FEATURE_CLUSTERED_BIT
|
1204 VK_SUBGROUP_FEATURE_QUAD_BIT
;
1205 properties
->quadOperationsInAllStages
= VK_TRUE
;
1209 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VERTEX_ATTRIBUTE_DIVISOR_PROPERTIES_EXT
: {
1210 VkPhysicalDeviceVertexAttributeDivisorPropertiesEXT
*props
=
1211 (VkPhysicalDeviceVertexAttributeDivisorPropertiesEXT
*)ext
;
1212 /* We have to restrict this a bit for multiview */
1213 props
->maxVertexAttribDivisor
= UINT32_MAX
/ 16;
1217 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PROTECTED_MEMORY_PROPERTIES
: {
1218 VkPhysicalDeviceProtectedMemoryProperties
*props
=
1219 (VkPhysicalDeviceProtectedMemoryProperties
*)ext
;
1220 props
->protectedNoFault
= false;
1225 anv_debug_ignored_stype(ext
->sType
);
1231 /* We support exactly one queue family. */
1232 static const VkQueueFamilyProperties
1233 anv_queue_family_properties
= {
1234 .queueFlags
= VK_QUEUE_GRAPHICS_BIT
|
1235 VK_QUEUE_COMPUTE_BIT
|
1236 VK_QUEUE_TRANSFER_BIT
,
1238 .timestampValidBits
= 36, /* XXX: Real value here */
1239 .minImageTransferGranularity
= { 1, 1, 1 },
1242 void anv_GetPhysicalDeviceQueueFamilyProperties(
1243 VkPhysicalDevice physicalDevice
,
1245 VkQueueFamilyProperties
* pQueueFamilyProperties
)
1247 VK_OUTARRAY_MAKE(out
, pQueueFamilyProperties
, pCount
);
1249 vk_outarray_append(&out
, p
) {
1250 *p
= anv_queue_family_properties
;
1254 void anv_GetPhysicalDeviceQueueFamilyProperties2(
1255 VkPhysicalDevice physicalDevice
,
1256 uint32_t* pQueueFamilyPropertyCount
,
1257 VkQueueFamilyProperties2
* pQueueFamilyProperties
)
1260 VK_OUTARRAY_MAKE(out
, pQueueFamilyProperties
, pQueueFamilyPropertyCount
);
1262 vk_outarray_append(&out
, p
) {
1263 p
->queueFamilyProperties
= anv_queue_family_properties
;
1265 vk_foreach_struct(s
, p
->pNext
) {
1266 anv_debug_ignored_stype(s
->sType
);
1271 void anv_GetPhysicalDeviceMemoryProperties(
1272 VkPhysicalDevice physicalDevice
,
1273 VkPhysicalDeviceMemoryProperties
* pMemoryProperties
)
1275 ANV_FROM_HANDLE(anv_physical_device
, physical_device
, physicalDevice
);
1277 pMemoryProperties
->memoryTypeCount
= physical_device
->memory
.type_count
;
1278 for (uint32_t i
= 0; i
< physical_device
->memory
.type_count
; i
++) {
1279 pMemoryProperties
->memoryTypes
[i
] = (VkMemoryType
) {
1280 .propertyFlags
= physical_device
->memory
.types
[i
].propertyFlags
,
1281 .heapIndex
= physical_device
->memory
.types
[i
].heapIndex
,
1285 pMemoryProperties
->memoryHeapCount
= physical_device
->memory
.heap_count
;
1286 for (uint32_t i
= 0; i
< physical_device
->memory
.heap_count
; i
++) {
1287 pMemoryProperties
->memoryHeaps
[i
] = (VkMemoryHeap
) {
1288 .size
= physical_device
->memory
.heaps
[i
].size
,
1289 .flags
= physical_device
->memory
.heaps
[i
].flags
,
1294 void anv_GetPhysicalDeviceMemoryProperties2(
1295 VkPhysicalDevice physicalDevice
,
1296 VkPhysicalDeviceMemoryProperties2
* pMemoryProperties
)
1298 anv_GetPhysicalDeviceMemoryProperties(physicalDevice
,
1299 &pMemoryProperties
->memoryProperties
);
1301 vk_foreach_struct(ext
, pMemoryProperties
->pNext
) {
1302 switch (ext
->sType
) {
1304 anv_debug_ignored_stype(ext
->sType
);
1311 anv_GetDeviceGroupPeerMemoryFeatures(
1314 uint32_t localDeviceIndex
,
1315 uint32_t remoteDeviceIndex
,
1316 VkPeerMemoryFeatureFlags
* pPeerMemoryFeatures
)
1318 assert(localDeviceIndex
== 0 && remoteDeviceIndex
== 0);
1319 *pPeerMemoryFeatures
= VK_PEER_MEMORY_FEATURE_COPY_SRC_BIT
|
1320 VK_PEER_MEMORY_FEATURE_COPY_DST_BIT
|
1321 VK_PEER_MEMORY_FEATURE_GENERIC_SRC_BIT
|
1322 VK_PEER_MEMORY_FEATURE_GENERIC_DST_BIT
;
1325 PFN_vkVoidFunction
anv_GetInstanceProcAddr(
1326 VkInstance _instance
,
1329 ANV_FROM_HANDLE(anv_instance
, instance
, _instance
);
1331 /* The Vulkan 1.0 spec for vkGetInstanceProcAddr has a table of exactly
1332 * when we have to return valid function pointers, NULL, or it's left
1333 * undefined. See the table for exact details.
1338 #define LOOKUP_ANV_ENTRYPOINT(entrypoint) \
1339 if (strcmp(pName, "vk" #entrypoint) == 0) \
1340 return (PFN_vkVoidFunction)anv_##entrypoint
1342 LOOKUP_ANV_ENTRYPOINT(EnumerateInstanceExtensionProperties
);
1343 LOOKUP_ANV_ENTRYPOINT(EnumerateInstanceLayerProperties
);
1344 LOOKUP_ANV_ENTRYPOINT(EnumerateInstanceVersion
);
1345 LOOKUP_ANV_ENTRYPOINT(CreateInstance
);
1347 #undef LOOKUP_ANV_ENTRYPOINT
1349 if (instance
== NULL
)
1352 int idx
= anv_get_entrypoint_index(pName
);
1356 return instance
->dispatch
.entrypoints
[idx
];
1359 /* With version 1+ of the loader interface the ICD should expose
1360 * vk_icdGetInstanceProcAddr to work around certain LD_PRELOAD issues seen in apps.
1363 VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL
vk_icdGetInstanceProcAddr(
1364 VkInstance instance
,
1368 VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL
vk_icdGetInstanceProcAddr(
1369 VkInstance instance
,
1372 return anv_GetInstanceProcAddr(instance
, pName
);
1375 PFN_vkVoidFunction
anv_GetDeviceProcAddr(
1379 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1381 if (!device
|| !pName
)
1384 int idx
= anv_get_entrypoint_index(pName
);
1388 return device
->dispatch
.entrypoints
[idx
];
1392 anv_CreateDebugReportCallbackEXT(VkInstance _instance
,
1393 const VkDebugReportCallbackCreateInfoEXT
* pCreateInfo
,
1394 const VkAllocationCallbacks
* pAllocator
,
1395 VkDebugReportCallbackEXT
* pCallback
)
1397 ANV_FROM_HANDLE(anv_instance
, instance
, _instance
);
1398 return vk_create_debug_report_callback(&instance
->debug_report_callbacks
,
1399 pCreateInfo
, pAllocator
, &instance
->alloc
,
1404 anv_DestroyDebugReportCallbackEXT(VkInstance _instance
,
1405 VkDebugReportCallbackEXT _callback
,
1406 const VkAllocationCallbacks
* pAllocator
)
1408 ANV_FROM_HANDLE(anv_instance
, instance
, _instance
);
1409 vk_destroy_debug_report_callback(&instance
->debug_report_callbacks
,
1410 _callback
, pAllocator
, &instance
->alloc
);
1414 anv_DebugReportMessageEXT(VkInstance _instance
,
1415 VkDebugReportFlagsEXT flags
,
1416 VkDebugReportObjectTypeEXT objectType
,
1419 int32_t messageCode
,
1420 const char* pLayerPrefix
,
1421 const char* pMessage
)
1423 ANV_FROM_HANDLE(anv_instance
, instance
, _instance
);
1424 vk_debug_report(&instance
->debug_report_callbacks
, flags
, objectType
,
1425 object
, location
, messageCode
, pLayerPrefix
, pMessage
);
1429 anv_queue_init(struct anv_device
*device
, struct anv_queue
*queue
)
1431 queue
->_loader_data
.loaderMagic
= ICD_LOADER_MAGIC
;
1432 queue
->device
= device
;
1437 anv_queue_finish(struct anv_queue
*queue
)
1441 static struct anv_state
1442 anv_state_pool_emit_data(struct anv_state_pool
*pool
, size_t size
, size_t align
, const void *p
)
1444 struct anv_state state
;
1446 state
= anv_state_pool_alloc(pool
, size
, align
);
1447 memcpy(state
.map
, p
, size
);
1449 anv_state_flush(pool
->block_pool
.device
, state
);
1454 struct gen8_border_color
{
1459 /* Pad out to 64 bytes */
1464 anv_device_init_border_colors(struct anv_device
*device
)
1466 static const struct gen8_border_color border_colors
[] = {
1467 [VK_BORDER_COLOR_FLOAT_TRANSPARENT_BLACK
] = { .float32
= { 0.0, 0.0, 0.0, 0.0 } },
1468 [VK_BORDER_COLOR_FLOAT_OPAQUE_BLACK
] = { .float32
= { 0.0, 0.0, 0.0, 1.0 } },
1469 [VK_BORDER_COLOR_FLOAT_OPAQUE_WHITE
] = { .float32
= { 1.0, 1.0, 1.0, 1.0 } },
1470 [VK_BORDER_COLOR_INT_TRANSPARENT_BLACK
] = { .uint32
= { 0, 0, 0, 0 } },
1471 [VK_BORDER_COLOR_INT_OPAQUE_BLACK
] = { .uint32
= { 0, 0, 0, 1 } },
1472 [VK_BORDER_COLOR_INT_OPAQUE_WHITE
] = { .uint32
= { 1, 1, 1, 1 } },
1475 device
->border_colors
= anv_state_pool_emit_data(&device
->dynamic_state_pool
,
1476 sizeof(border_colors
), 64,
1481 anv_device_init_trivial_batch(struct anv_device
*device
)
1483 anv_bo_init_new(&device
->trivial_batch_bo
, device
, 4096);
1485 if (device
->instance
->physicalDevice
.has_exec_async
)
1486 device
->trivial_batch_bo
.flags
|= EXEC_OBJECT_ASYNC
;
1488 if (device
->instance
->physicalDevice
.use_softpin
)
1489 device
->trivial_batch_bo
.flags
|= EXEC_OBJECT_PINNED
;
1491 anv_vma_alloc(device
, &device
->trivial_batch_bo
);
1493 void *map
= anv_gem_mmap(device
, device
->trivial_batch_bo
.gem_handle
,
1496 struct anv_batch batch
= {
1502 anv_batch_emit(&batch
, GEN7_MI_BATCH_BUFFER_END
, bbe
);
1503 anv_batch_emit(&batch
, GEN7_MI_NOOP
, noop
);
1505 if (!device
->info
.has_llc
)
1506 gen_clflush_range(map
, batch
.next
- map
);
1508 anv_gem_munmap(map
, device
->trivial_batch_bo
.size
);
1511 VkResult
anv_EnumerateDeviceExtensionProperties(
1512 VkPhysicalDevice physicalDevice
,
1513 const char* pLayerName
,
1514 uint32_t* pPropertyCount
,
1515 VkExtensionProperties
* pProperties
)
1517 ANV_FROM_HANDLE(anv_physical_device
, device
, physicalDevice
);
1518 VK_OUTARRAY_MAKE(out
, pProperties
, pPropertyCount
);
1520 for (int i
= 0; i
< ANV_DEVICE_EXTENSION_COUNT
; i
++) {
1521 if (device
->supported_extensions
.extensions
[i
]) {
1522 vk_outarray_append(&out
, prop
) {
1523 *prop
= anv_device_extensions
[i
];
1528 return vk_outarray_status(&out
);
1532 anv_device_init_dispatch(struct anv_device
*device
)
1534 const struct anv_dispatch_table
*genX_table
;
1535 switch (device
->info
.gen
) {
1537 genX_table
= &gen11_dispatch_table
;
1540 genX_table
= &gen10_dispatch_table
;
1543 genX_table
= &gen9_dispatch_table
;
1546 genX_table
= &gen8_dispatch_table
;
1549 if (device
->info
.is_haswell
)
1550 genX_table
= &gen75_dispatch_table
;
1552 genX_table
= &gen7_dispatch_table
;
1555 unreachable("unsupported gen\n");
1558 for (unsigned i
= 0; i
< ARRAY_SIZE(device
->dispatch
.entrypoints
); i
++) {
1559 /* Vulkan requires that entrypoints for extensions which have not been
1560 * enabled must not be advertised.
1562 if (!anv_entrypoint_is_enabled(i
, device
->instance
->app_info
.api_version
,
1563 &device
->instance
->enabled_extensions
,
1564 &device
->enabled_extensions
)) {
1565 device
->dispatch
.entrypoints
[i
] = NULL
;
1566 } else if (genX_table
->entrypoints
[i
]) {
1567 device
->dispatch
.entrypoints
[i
] = genX_table
->entrypoints
[i
];
1569 device
->dispatch
.entrypoints
[i
] = anv_dispatch_table
.entrypoints
[i
];
1575 vk_priority_to_gen(int priority
)
1578 case VK_QUEUE_GLOBAL_PRIORITY_LOW_EXT
:
1579 return GEN_CONTEXT_LOW_PRIORITY
;
1580 case VK_QUEUE_GLOBAL_PRIORITY_MEDIUM_EXT
:
1581 return GEN_CONTEXT_MEDIUM_PRIORITY
;
1582 case VK_QUEUE_GLOBAL_PRIORITY_HIGH_EXT
:
1583 return GEN_CONTEXT_HIGH_PRIORITY
;
1584 case VK_QUEUE_GLOBAL_PRIORITY_REALTIME_EXT
:
1585 return GEN_CONTEXT_REALTIME_PRIORITY
;
1587 unreachable("Invalid priority");
1592 anv_device_init_hiz_clear_value_bo(struct anv_device
*device
)
1594 anv_bo_init_new(&device
->hiz_clear_bo
, device
, 4096);
1596 if (device
->instance
->physicalDevice
.has_exec_async
)
1597 device
->hiz_clear_bo
.flags
|= EXEC_OBJECT_ASYNC
;
1599 if (device
->instance
->physicalDevice
.use_softpin
)
1600 device
->hiz_clear_bo
.flags
|= EXEC_OBJECT_PINNED
;
1602 anv_vma_alloc(device
, &device
->hiz_clear_bo
);
1604 uint32_t *map
= anv_gem_mmap(device
, device
->hiz_clear_bo
.gem_handle
,
1607 union isl_color_value hiz_clear
= { .u32
= { 0, } };
1608 hiz_clear
.f32
[0] = ANV_HZ_FC_VAL
;
1610 memcpy(map
, hiz_clear
.u32
, sizeof(hiz_clear
.u32
));
1611 anv_gem_munmap(map
, device
->hiz_clear_bo
.size
);
1614 VkResult
anv_CreateDevice(
1615 VkPhysicalDevice physicalDevice
,
1616 const VkDeviceCreateInfo
* pCreateInfo
,
1617 const VkAllocationCallbacks
* pAllocator
,
1620 ANV_FROM_HANDLE(anv_physical_device
, physical_device
, physicalDevice
);
1622 struct anv_device
*device
;
1624 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO
);
1626 struct anv_device_extension_table enabled_extensions
= { };
1627 for (uint32_t i
= 0; i
< pCreateInfo
->enabledExtensionCount
; i
++) {
1629 for (idx
= 0; idx
< ANV_DEVICE_EXTENSION_COUNT
; idx
++) {
1630 if (strcmp(pCreateInfo
->ppEnabledExtensionNames
[i
],
1631 anv_device_extensions
[idx
].extensionName
) == 0)
1635 if (idx
>= ANV_DEVICE_EXTENSION_COUNT
)
1636 return vk_error(VK_ERROR_EXTENSION_NOT_PRESENT
);
1638 if (!physical_device
->supported_extensions
.extensions
[idx
])
1639 return vk_error(VK_ERROR_EXTENSION_NOT_PRESENT
);
1641 enabled_extensions
.extensions
[idx
] = true;
1644 /* Check enabled features */
1645 if (pCreateInfo
->pEnabledFeatures
) {
1646 VkPhysicalDeviceFeatures supported_features
;
1647 anv_GetPhysicalDeviceFeatures(physicalDevice
, &supported_features
);
1648 VkBool32
*supported_feature
= (VkBool32
*)&supported_features
;
1649 VkBool32
*enabled_feature
= (VkBool32
*)pCreateInfo
->pEnabledFeatures
;
1650 unsigned num_features
= sizeof(VkPhysicalDeviceFeatures
) / sizeof(VkBool32
);
1651 for (uint32_t i
= 0; i
< num_features
; i
++) {
1652 if (enabled_feature
[i
] && !supported_feature
[i
])
1653 return vk_error(VK_ERROR_FEATURE_NOT_PRESENT
);
1657 /* Check requested queues and fail if we are requested to create any
1658 * queues with flags we don't support.
1660 assert(pCreateInfo
->queueCreateInfoCount
> 0);
1661 for (uint32_t i
= 0; i
< pCreateInfo
->queueCreateInfoCount
; i
++) {
1662 if (pCreateInfo
->pQueueCreateInfos
[i
].flags
!= 0)
1663 return vk_error(VK_ERROR_INITIALIZATION_FAILED
);
1666 /* Check if client specified queue priority. */
1667 const VkDeviceQueueGlobalPriorityCreateInfoEXT
*queue_priority
=
1668 vk_find_struct_const(pCreateInfo
->pQueueCreateInfos
[0].pNext
,
1669 DEVICE_QUEUE_GLOBAL_PRIORITY_CREATE_INFO_EXT
);
1671 VkQueueGlobalPriorityEXT priority
=
1672 queue_priority
? queue_priority
->globalPriority
:
1673 VK_QUEUE_GLOBAL_PRIORITY_MEDIUM_EXT
;
1675 device
= vk_alloc2(&physical_device
->instance
->alloc
, pAllocator
,
1677 VK_SYSTEM_ALLOCATION_SCOPE_DEVICE
);
1679 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
1681 device
->_loader_data
.loaderMagic
= ICD_LOADER_MAGIC
;
1682 device
->instance
= physical_device
->instance
;
1683 device
->chipset_id
= physical_device
->chipset_id
;
1684 device
->no_hw
= physical_device
->no_hw
;
1685 device
->lost
= false;
1688 device
->alloc
= *pAllocator
;
1690 device
->alloc
= physical_device
->instance
->alloc
;
1692 /* XXX(chadv): Can we dup() physicalDevice->fd here? */
1693 device
->fd
= open(physical_device
->path
, O_RDWR
| O_CLOEXEC
);
1694 if (device
->fd
== -1) {
1695 result
= vk_error(VK_ERROR_INITIALIZATION_FAILED
);
1699 device
->context_id
= anv_gem_create_context(device
);
1700 if (device
->context_id
== -1) {
1701 result
= vk_error(VK_ERROR_INITIALIZATION_FAILED
);
1705 if (physical_device
->use_softpin
) {
1706 if (pthread_mutex_init(&device
->vma_mutex
, NULL
) != 0) {
1707 result
= vk_error(VK_ERROR_INITIALIZATION_FAILED
);
1711 /* keep the page with address zero out of the allocator */
1712 util_vma_heap_init(&device
->vma_lo
, LOW_HEAP_MIN_ADDRESS
, LOW_HEAP_SIZE
);
1713 device
->vma_lo_available
=
1714 physical_device
->memory
.heaps
[physical_device
->memory
.heap_count
- 1].size
;
1716 /* Leave the last 4GiB out of the high vma range, so that no state base
1717 * address + size can overflow 48 bits. For more information see the
1718 * comment about Wa32bitGeneralStateOffset in anv_allocator.c
1720 util_vma_heap_init(&device
->vma_hi
, HIGH_HEAP_MIN_ADDRESS
,
1722 device
->vma_hi_available
= physical_device
->memory
.heap_count
== 1 ? 0 :
1723 physical_device
->memory
.heaps
[0].size
;
1726 /* As per spec, the driver implementation may deny requests to acquire
1727 * a priority above the default priority (MEDIUM) if the caller does not
1728 * have sufficient privileges. In this scenario VK_ERROR_NOT_PERMITTED_EXT
1731 if (physical_device
->has_context_priority
) {
1732 int err
= anv_gem_set_context_param(device
->fd
, device
->context_id
,
1733 I915_CONTEXT_PARAM_PRIORITY
,
1734 vk_priority_to_gen(priority
));
1735 if (err
!= 0 && priority
> VK_QUEUE_GLOBAL_PRIORITY_MEDIUM_EXT
) {
1736 result
= vk_error(VK_ERROR_NOT_PERMITTED_EXT
);
1741 device
->info
= physical_device
->info
;
1742 device
->isl_dev
= physical_device
->isl_dev
;
1744 /* On Broadwell and later, we can use batch chaining to more efficiently
1745 * implement growing command buffers. Prior to Haswell, the kernel
1746 * command parser gets in the way and we have to fall back to growing
1749 device
->can_chain_batches
= device
->info
.gen
>= 8;
1751 device
->robust_buffer_access
= pCreateInfo
->pEnabledFeatures
&&
1752 pCreateInfo
->pEnabledFeatures
->robustBufferAccess
;
1753 device
->enabled_extensions
= enabled_extensions
;
1755 anv_device_init_dispatch(device
);
1757 if (pthread_mutex_init(&device
->mutex
, NULL
) != 0) {
1758 result
= vk_error(VK_ERROR_INITIALIZATION_FAILED
);
1759 goto fail_context_id
;
1762 pthread_condattr_t condattr
;
1763 if (pthread_condattr_init(&condattr
) != 0) {
1764 result
= vk_error(VK_ERROR_INITIALIZATION_FAILED
);
1767 if (pthread_condattr_setclock(&condattr
, CLOCK_MONOTONIC
) != 0) {
1768 pthread_condattr_destroy(&condattr
);
1769 result
= vk_error(VK_ERROR_INITIALIZATION_FAILED
);
1772 if (pthread_cond_init(&device
->queue_submit
, NULL
) != 0) {
1773 pthread_condattr_destroy(&condattr
);
1774 result
= vk_error(VK_ERROR_INITIALIZATION_FAILED
);
1777 pthread_condattr_destroy(&condattr
);
1780 (physical_device
->supports_48bit_addresses
? EXEC_OBJECT_SUPPORTS_48B_ADDRESS
: 0) |
1781 (physical_device
->has_exec_async
? EXEC_OBJECT_ASYNC
: 0) |
1782 (physical_device
->has_exec_capture
? EXEC_OBJECT_CAPTURE
: 0) |
1783 (physical_device
->use_softpin
? EXEC_OBJECT_PINNED
: 0);
1785 anv_bo_pool_init(&device
->batch_bo_pool
, device
, bo_flags
);
1787 result
= anv_bo_cache_init(&device
->bo_cache
);
1788 if (result
!= VK_SUCCESS
)
1789 goto fail_batch_bo_pool
;
1791 if (!physical_device
->use_softpin
)
1792 bo_flags
&= ~EXEC_OBJECT_SUPPORTS_48B_ADDRESS
;
1794 result
= anv_state_pool_init(&device
->dynamic_state_pool
, device
,
1795 DYNAMIC_STATE_POOL_MIN_ADDRESS
,
1798 if (result
!= VK_SUCCESS
)
1801 result
= anv_state_pool_init(&device
->instruction_state_pool
, device
,
1802 INSTRUCTION_STATE_POOL_MIN_ADDRESS
,
1805 if (result
!= VK_SUCCESS
)
1806 goto fail_dynamic_state_pool
;
1808 result
= anv_state_pool_init(&device
->surface_state_pool
, device
,
1809 SURFACE_STATE_POOL_MIN_ADDRESS
,
1812 if (result
!= VK_SUCCESS
)
1813 goto fail_instruction_state_pool
;
1815 if (physical_device
->use_softpin
) {
1816 result
= anv_state_pool_init(&device
->binding_table_pool
, device
,
1817 BINDING_TABLE_POOL_MIN_ADDRESS
,
1820 if (result
!= VK_SUCCESS
)
1821 goto fail_surface_state_pool
;
1824 result
= anv_bo_init_new(&device
->workaround_bo
, device
, 1024);
1825 if (result
!= VK_SUCCESS
)
1826 goto fail_binding_table_pool
;
1828 if (physical_device
->use_softpin
)
1829 device
->workaround_bo
.flags
|= EXEC_OBJECT_PINNED
;
1831 if (!anv_vma_alloc(device
, &device
->workaround_bo
))
1832 goto fail_workaround_bo
;
1834 anv_device_init_trivial_batch(device
);
1836 if (device
->info
.gen
>= 10)
1837 anv_device_init_hiz_clear_value_bo(device
);
1839 anv_scratch_pool_init(device
, &device
->scratch_pool
);
1841 anv_queue_init(device
, &device
->queue
);
1843 switch (device
->info
.gen
) {
1845 if (!device
->info
.is_haswell
)
1846 result
= gen7_init_device_state(device
);
1848 result
= gen75_init_device_state(device
);
1851 result
= gen8_init_device_state(device
);
1854 result
= gen9_init_device_state(device
);
1857 result
= gen10_init_device_state(device
);
1860 result
= gen11_init_device_state(device
);
1863 /* Shouldn't get here as we don't create physical devices for any other
1865 unreachable("unhandled gen");
1867 if (result
!= VK_SUCCESS
)
1868 goto fail_workaround_bo
;
1870 anv_pipeline_cache_init(&device
->default_pipeline_cache
, device
, true);
1872 anv_device_init_blorp(device
);
1874 anv_device_init_border_colors(device
);
1876 *pDevice
= anv_device_to_handle(device
);
1881 anv_queue_finish(&device
->queue
);
1882 anv_scratch_pool_finish(device
, &device
->scratch_pool
);
1883 anv_gem_munmap(device
->workaround_bo
.map
, device
->workaround_bo
.size
);
1884 anv_gem_close(device
, device
->workaround_bo
.gem_handle
);
1885 fail_binding_table_pool
:
1886 if (physical_device
->use_softpin
)
1887 anv_state_pool_finish(&device
->binding_table_pool
);
1888 fail_surface_state_pool
:
1889 anv_state_pool_finish(&device
->surface_state_pool
);
1890 fail_instruction_state_pool
:
1891 anv_state_pool_finish(&device
->instruction_state_pool
);
1892 fail_dynamic_state_pool
:
1893 anv_state_pool_finish(&device
->dynamic_state_pool
);
1895 anv_bo_cache_finish(&device
->bo_cache
);
1897 anv_bo_pool_finish(&device
->batch_bo_pool
);
1898 pthread_cond_destroy(&device
->queue_submit
);
1900 pthread_mutex_destroy(&device
->mutex
);
1902 anv_gem_destroy_context(device
, device
->context_id
);
1906 vk_free(&device
->alloc
, device
);
1911 void anv_DestroyDevice(
1913 const VkAllocationCallbacks
* pAllocator
)
1915 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1916 struct anv_physical_device
*physical_device
;
1921 physical_device
= &device
->instance
->physicalDevice
;
1923 anv_device_finish_blorp(device
);
1925 anv_pipeline_cache_finish(&device
->default_pipeline_cache
);
1927 anv_queue_finish(&device
->queue
);
1929 #ifdef HAVE_VALGRIND
1930 /* We only need to free these to prevent valgrind errors. The backing
1931 * BO will go away in a couple of lines so we don't actually leak.
1933 anv_state_pool_free(&device
->dynamic_state_pool
, device
->border_colors
);
1936 anv_scratch_pool_finish(device
, &device
->scratch_pool
);
1938 anv_gem_munmap(device
->workaround_bo
.map
, device
->workaround_bo
.size
);
1939 anv_vma_free(device
, &device
->workaround_bo
);
1940 anv_gem_close(device
, device
->workaround_bo
.gem_handle
);
1942 anv_vma_free(device
, &device
->trivial_batch_bo
);
1943 anv_gem_close(device
, device
->trivial_batch_bo
.gem_handle
);
1944 if (device
->info
.gen
>= 10)
1945 anv_gem_close(device
, device
->hiz_clear_bo
.gem_handle
);
1947 if (physical_device
->use_softpin
)
1948 anv_state_pool_finish(&device
->binding_table_pool
);
1949 anv_state_pool_finish(&device
->surface_state_pool
);
1950 anv_state_pool_finish(&device
->instruction_state_pool
);
1951 anv_state_pool_finish(&device
->dynamic_state_pool
);
1953 anv_bo_cache_finish(&device
->bo_cache
);
1955 anv_bo_pool_finish(&device
->batch_bo_pool
);
1957 pthread_cond_destroy(&device
->queue_submit
);
1958 pthread_mutex_destroy(&device
->mutex
);
1960 anv_gem_destroy_context(device
, device
->context_id
);
1964 vk_free(&device
->alloc
, device
);
1967 VkResult
anv_EnumerateInstanceLayerProperties(
1968 uint32_t* pPropertyCount
,
1969 VkLayerProperties
* pProperties
)
1971 if (pProperties
== NULL
) {
1972 *pPropertyCount
= 0;
1976 /* None supported at this time */
1977 return vk_error(VK_ERROR_LAYER_NOT_PRESENT
);
1980 VkResult
anv_EnumerateDeviceLayerProperties(
1981 VkPhysicalDevice physicalDevice
,
1982 uint32_t* pPropertyCount
,
1983 VkLayerProperties
* pProperties
)
1985 if (pProperties
== NULL
) {
1986 *pPropertyCount
= 0;
1990 /* None supported at this time */
1991 return vk_error(VK_ERROR_LAYER_NOT_PRESENT
);
1994 void anv_GetDeviceQueue(
1996 uint32_t queueNodeIndex
,
1997 uint32_t queueIndex
,
2000 ANV_FROM_HANDLE(anv_device
, device
, _device
);
2002 assert(queueIndex
== 0);
2004 *pQueue
= anv_queue_to_handle(&device
->queue
);
2007 void anv_GetDeviceQueue2(
2009 const VkDeviceQueueInfo2
* pQueueInfo
,
2012 ANV_FROM_HANDLE(anv_device
, device
, _device
);
2014 assert(pQueueInfo
->queueIndex
== 0);
2016 if (pQueueInfo
->flags
== device
->queue
.flags
)
2017 *pQueue
= anv_queue_to_handle(&device
->queue
);
2023 anv_device_query_status(struct anv_device
*device
)
2025 /* This isn't likely as most of the callers of this function already check
2026 * for it. However, it doesn't hurt to check and it potentially lets us
2029 if (unlikely(device
->lost
))
2030 return VK_ERROR_DEVICE_LOST
;
2032 uint32_t active
, pending
;
2033 int ret
= anv_gem_gpu_get_reset_stats(device
, &active
, &pending
);
2035 /* We don't know the real error. */
2036 device
->lost
= true;
2037 return vk_errorf(device
->instance
, device
, VK_ERROR_DEVICE_LOST
,
2038 "get_reset_stats failed: %m");
2042 device
->lost
= true;
2043 return vk_errorf(device
->instance
, device
, VK_ERROR_DEVICE_LOST
,
2044 "GPU hung on one of our command buffers");
2045 } else if (pending
) {
2046 device
->lost
= true;
2047 return vk_errorf(device
->instance
, device
, VK_ERROR_DEVICE_LOST
,
2048 "GPU hung with commands in-flight");
2055 anv_device_bo_busy(struct anv_device
*device
, struct anv_bo
*bo
)
2057 /* Note: This only returns whether or not the BO is in use by an i915 GPU.
2058 * Other usages of the BO (such as on different hardware) will not be
2059 * flagged as "busy" by this ioctl. Use with care.
2061 int ret
= anv_gem_busy(device
, bo
->gem_handle
);
2063 return VK_NOT_READY
;
2064 } else if (ret
== -1) {
2065 /* We don't know the real error. */
2066 device
->lost
= true;
2067 return vk_errorf(device
->instance
, device
, VK_ERROR_DEVICE_LOST
,
2068 "gem wait failed: %m");
2071 /* Query for device status after the busy call. If the BO we're checking
2072 * got caught in a GPU hang we don't want to return VK_SUCCESS to the
2073 * client because it clearly doesn't have valid data. Yes, this most
2074 * likely means an ioctl, but we just did an ioctl to query the busy status
2075 * so it's no great loss.
2077 return anv_device_query_status(device
);
2081 anv_device_wait(struct anv_device
*device
, struct anv_bo
*bo
,
2084 int ret
= anv_gem_wait(device
, bo
->gem_handle
, &timeout
);
2085 if (ret
== -1 && errno
== ETIME
) {
2087 } else if (ret
== -1) {
2088 /* We don't know the real error. */
2089 device
->lost
= true;
2090 return vk_errorf(device
->instance
, device
, VK_ERROR_DEVICE_LOST
,
2091 "gem wait failed: %m");
2094 /* Query for device status after the wait. If the BO we're waiting on got
2095 * caught in a GPU hang we don't want to return VK_SUCCESS to the client
2096 * because it clearly doesn't have valid data. Yes, this most likely means
2097 * an ioctl, but we just did an ioctl to wait so it's no great loss.
2099 return anv_device_query_status(device
);
2102 VkResult
anv_DeviceWaitIdle(
2105 ANV_FROM_HANDLE(anv_device
, device
, _device
);
2106 if (unlikely(device
->lost
))
2107 return VK_ERROR_DEVICE_LOST
;
2109 struct anv_batch batch
;
2112 batch
.start
= batch
.next
= cmds
;
2113 batch
.end
= (void *) cmds
+ sizeof(cmds
);
2115 anv_batch_emit(&batch
, GEN7_MI_BATCH_BUFFER_END
, bbe
);
2116 anv_batch_emit(&batch
, GEN7_MI_NOOP
, noop
);
2118 return anv_device_submit_simple_batch(device
, &batch
);
2122 anv_vma_alloc(struct anv_device
*device
, struct anv_bo
*bo
)
2124 if (!(bo
->flags
& EXEC_OBJECT_PINNED
))
2127 pthread_mutex_lock(&device
->vma_mutex
);
2131 if (bo
->flags
& EXEC_OBJECT_SUPPORTS_48B_ADDRESS
&&
2132 device
->vma_hi_available
>= bo
->size
) {
2133 uint64_t addr
= util_vma_heap_alloc(&device
->vma_hi
, bo
->size
, 4096);
2135 bo
->offset
= gen_canonical_address(addr
);
2136 assert(addr
== gen_48b_address(bo
->offset
));
2137 device
->vma_hi_available
-= bo
->size
;
2141 if (bo
->offset
== 0 && device
->vma_lo_available
>= bo
->size
) {
2142 uint64_t addr
= util_vma_heap_alloc(&device
->vma_lo
, bo
->size
, 4096);
2144 bo
->offset
= gen_canonical_address(addr
);
2145 assert(addr
== gen_48b_address(bo
->offset
));
2146 device
->vma_lo_available
-= bo
->size
;
2150 pthread_mutex_unlock(&device
->vma_mutex
);
2152 return bo
->offset
!= 0;
2156 anv_vma_free(struct anv_device
*device
, struct anv_bo
*bo
)
2158 if (!(bo
->flags
& EXEC_OBJECT_PINNED
))
2161 const uint64_t addr_48b
= gen_48b_address(bo
->offset
);
2163 pthread_mutex_lock(&device
->vma_mutex
);
2165 if (addr_48b
>= LOW_HEAP_MIN_ADDRESS
&&
2166 addr_48b
<= LOW_HEAP_MAX_ADDRESS
) {
2167 util_vma_heap_free(&device
->vma_lo
, addr_48b
, bo
->size
);
2168 device
->vma_lo_available
+= bo
->size
;
2170 assert(addr_48b
>= HIGH_HEAP_MIN_ADDRESS
&&
2171 addr_48b
<= HIGH_HEAP_MAX_ADDRESS
);
2172 util_vma_heap_free(&device
->vma_hi
, addr_48b
, bo
->size
);
2173 device
->vma_hi_available
+= bo
->size
;
2176 pthread_mutex_unlock(&device
->vma_mutex
);
2182 anv_bo_init_new(struct anv_bo
*bo
, struct anv_device
*device
, uint64_t size
)
2184 uint32_t gem_handle
= anv_gem_create(device
, size
);
2186 return vk_error(VK_ERROR_OUT_OF_DEVICE_MEMORY
);
2188 anv_bo_init(bo
, gem_handle
, size
);
2193 VkResult
anv_AllocateMemory(
2195 const VkMemoryAllocateInfo
* pAllocateInfo
,
2196 const VkAllocationCallbacks
* pAllocator
,
2197 VkDeviceMemory
* pMem
)
2199 ANV_FROM_HANDLE(anv_device
, device
, _device
);
2200 struct anv_physical_device
*pdevice
= &device
->instance
->physicalDevice
;
2201 struct anv_device_memory
*mem
;
2202 VkResult result
= VK_SUCCESS
;
2204 assert(pAllocateInfo
->sType
== VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO
);
2206 /* The Vulkan 1.0.33 spec says "allocationSize must be greater than 0". */
2207 assert(pAllocateInfo
->allocationSize
> 0);
2209 if (pAllocateInfo
->allocationSize
> MAX_MEMORY_ALLOCATION_SIZE
)
2210 return VK_ERROR_OUT_OF_DEVICE_MEMORY
;
2212 /* FINISHME: Fail if allocation request exceeds heap size. */
2214 mem
= vk_alloc2(&device
->alloc
, pAllocator
, sizeof(*mem
), 8,
2215 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
2217 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
2219 assert(pAllocateInfo
->memoryTypeIndex
< pdevice
->memory
.type_count
);
2220 mem
->type
= &pdevice
->memory
.types
[pAllocateInfo
->memoryTypeIndex
];
2224 uint64_t bo_flags
= 0;
2226 assert(mem
->type
->heapIndex
< pdevice
->memory
.heap_count
);
2227 if (pdevice
->memory
.heaps
[mem
->type
->heapIndex
].supports_48bit_addresses
)
2228 bo_flags
|= EXEC_OBJECT_SUPPORTS_48B_ADDRESS
;
2230 const struct wsi_memory_allocate_info
*wsi_info
=
2231 vk_find_struct_const(pAllocateInfo
->pNext
, WSI_MEMORY_ALLOCATE_INFO_MESA
);
2232 if (wsi_info
&& wsi_info
->implicit_sync
) {
2233 /* We need to set the WRITE flag on window system buffers so that GEM
2234 * will know we're writing to them and synchronize uses on other rings
2235 * (eg if the display server uses the blitter ring).
2237 bo_flags
|= EXEC_OBJECT_WRITE
;
2238 } else if (pdevice
->has_exec_async
) {
2239 bo_flags
|= EXEC_OBJECT_ASYNC
;
2242 if (pdevice
->use_softpin
)
2243 bo_flags
|= EXEC_OBJECT_PINNED
;
2245 const VkImportMemoryFdInfoKHR
*fd_info
=
2246 vk_find_struct_const(pAllocateInfo
->pNext
, IMPORT_MEMORY_FD_INFO_KHR
);
2248 /* The Vulkan spec permits handleType to be 0, in which case the struct is
2251 if (fd_info
&& fd_info
->handleType
) {
2252 /* At the moment, we support only the below handle types. */
2253 assert(fd_info
->handleType
==
2254 VK_EXTERNAL_MEMORY_HANDLE_TYPE_OPAQUE_FD_BIT
||
2255 fd_info
->handleType
==
2256 VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_EXT
);
2258 result
= anv_bo_cache_import(device
, &device
->bo_cache
,
2259 fd_info
->fd
, bo_flags
, &mem
->bo
);
2260 if (result
!= VK_SUCCESS
)
2263 VkDeviceSize aligned_alloc_size
=
2264 align_u64(pAllocateInfo
->allocationSize
, 4096);
2266 /* For security purposes, we reject importing the bo if it's smaller
2267 * than the requested allocation size. This prevents a malicious client
2268 * from passing a buffer to a trusted client, lying about the size, and
2269 * telling the trusted client to try and texture from an image that goes
2270 * out-of-bounds. This sort of thing could lead to GPU hangs or worse
2271 * in the trusted client. The trusted client can protect itself against
2272 * this sort of attack but only if it can trust the buffer size.
2274 if (mem
->bo
->size
< aligned_alloc_size
) {
2275 result
= vk_errorf(device
->instance
, device
,
2276 VK_ERROR_INVALID_EXTERNAL_HANDLE_KHR
,
2277 "aligned allocationSize too large for "
2278 "VK_EXTERNAL_MEMORY_HANDLE_TYPE_OPAQUE_FD_BIT_KHR: "
2279 "%"PRIu64
"B > %"PRIu64
"B",
2280 aligned_alloc_size
, mem
->bo
->size
);
2281 anv_bo_cache_release(device
, &device
->bo_cache
, mem
->bo
);
2285 /* From the Vulkan spec:
2287 * "Importing memory from a file descriptor transfers ownership of
2288 * the file descriptor from the application to the Vulkan
2289 * implementation. The application must not perform any operations on
2290 * the file descriptor after a successful import."
2292 * If the import fails, we leave the file descriptor open.
2296 result
= anv_bo_cache_alloc(device
, &device
->bo_cache
,
2297 pAllocateInfo
->allocationSize
, bo_flags
,
2299 if (result
!= VK_SUCCESS
)
2302 const VkMemoryDedicatedAllocateInfoKHR
*dedicated_info
=
2303 vk_find_struct_const(pAllocateInfo
->pNext
, MEMORY_DEDICATED_ALLOCATE_INFO_KHR
);
2304 if (dedicated_info
&& dedicated_info
->image
!= VK_NULL_HANDLE
) {
2305 ANV_FROM_HANDLE(anv_image
, image
, dedicated_info
->image
);
2307 /* Some legacy (non-modifiers) consumers need the tiling to be set on
2308 * the BO. In this case, we have a dedicated allocation.
2310 if (image
->needs_set_tiling
) {
2311 const uint32_t i915_tiling
=
2312 isl_tiling_to_i915_tiling(image
->planes
[0].surface
.isl
.tiling
);
2313 int ret
= anv_gem_set_tiling(device
, mem
->bo
->gem_handle
,
2314 image
->planes
[0].surface
.isl
.row_pitch_B
,
2317 anv_bo_cache_release(device
, &device
->bo_cache
, mem
->bo
);
2318 return vk_errorf(device
->instance
, NULL
,
2319 VK_ERROR_OUT_OF_DEVICE_MEMORY
,
2320 "failed to set BO tiling: %m");
2326 *pMem
= anv_device_memory_to_handle(mem
);
2331 vk_free2(&device
->alloc
, pAllocator
, mem
);
2336 VkResult
anv_GetMemoryFdKHR(
2338 const VkMemoryGetFdInfoKHR
* pGetFdInfo
,
2341 ANV_FROM_HANDLE(anv_device
, dev
, device_h
);
2342 ANV_FROM_HANDLE(anv_device_memory
, mem
, pGetFdInfo
->memory
);
2344 assert(pGetFdInfo
->sType
== VK_STRUCTURE_TYPE_MEMORY_GET_FD_INFO_KHR
);
2346 assert(pGetFdInfo
->handleType
== VK_EXTERNAL_MEMORY_HANDLE_TYPE_OPAQUE_FD_BIT
||
2347 pGetFdInfo
->handleType
== VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_EXT
);
2349 return anv_bo_cache_export(dev
, &dev
->bo_cache
, mem
->bo
, pFd
);
2352 VkResult
anv_GetMemoryFdPropertiesKHR(
2354 VkExternalMemoryHandleTypeFlagBitsKHR handleType
,
2356 VkMemoryFdPropertiesKHR
* pMemoryFdProperties
)
2358 ANV_FROM_HANDLE(anv_device
, device
, _device
);
2359 struct anv_physical_device
*pdevice
= &device
->instance
->physicalDevice
;
2361 switch (handleType
) {
2362 case VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_EXT
:
2363 /* dma-buf can be imported as any memory type */
2364 pMemoryFdProperties
->memoryTypeBits
=
2365 (1 << pdevice
->memory
.type_count
) - 1;
2369 /* The valid usage section for this function says:
2371 * "handleType must not be one of the handle types defined as
2374 * So opaque handle types fall into the default "unsupported" case.
2376 return vk_error(VK_ERROR_INVALID_EXTERNAL_HANDLE
);
2380 void anv_FreeMemory(
2382 VkDeviceMemory _mem
,
2383 const VkAllocationCallbacks
* pAllocator
)
2385 ANV_FROM_HANDLE(anv_device
, device
, _device
);
2386 ANV_FROM_HANDLE(anv_device_memory
, mem
, _mem
);
2392 anv_UnmapMemory(_device
, _mem
);
2394 anv_bo_cache_release(device
, &device
->bo_cache
, mem
->bo
);
2396 vk_free2(&device
->alloc
, pAllocator
, mem
);
2399 VkResult
anv_MapMemory(
2401 VkDeviceMemory _memory
,
2402 VkDeviceSize offset
,
2404 VkMemoryMapFlags flags
,
2407 ANV_FROM_HANDLE(anv_device
, device
, _device
);
2408 ANV_FROM_HANDLE(anv_device_memory
, mem
, _memory
);
2415 if (size
== VK_WHOLE_SIZE
)
2416 size
= mem
->bo
->size
- offset
;
2418 /* From the Vulkan spec version 1.0.32 docs for MapMemory:
2420 * * If size is not equal to VK_WHOLE_SIZE, size must be greater than 0
2421 * assert(size != 0);
2422 * * If size is not equal to VK_WHOLE_SIZE, size must be less than or
2423 * equal to the size of the memory minus offset
2426 assert(offset
+ size
<= mem
->bo
->size
);
2428 /* FIXME: Is this supposed to be thread safe? Since vkUnmapMemory() only
2429 * takes a VkDeviceMemory pointer, it seems like only one map of the memory
2430 * at a time is valid. We could just mmap up front and return an offset
2431 * pointer here, but that may exhaust virtual memory on 32 bit
2434 uint32_t gem_flags
= 0;
2436 if (!device
->info
.has_llc
&&
2437 (mem
->type
->propertyFlags
& VK_MEMORY_PROPERTY_HOST_COHERENT_BIT
))
2438 gem_flags
|= I915_MMAP_WC
;
2440 /* GEM will fail to map if the offset isn't 4k-aligned. Round down. */
2441 uint64_t map_offset
= offset
& ~4095ull;
2442 assert(offset
>= map_offset
);
2443 uint64_t map_size
= (offset
+ size
) - map_offset
;
2445 /* Let's map whole pages */
2446 map_size
= align_u64(map_size
, 4096);
2448 void *map
= anv_gem_mmap(device
, mem
->bo
->gem_handle
,
2449 map_offset
, map_size
, gem_flags
);
2450 if (map
== MAP_FAILED
)
2451 return vk_error(VK_ERROR_MEMORY_MAP_FAILED
);
2454 mem
->map_size
= map_size
;
2456 *ppData
= mem
->map
+ (offset
- map_offset
);
2461 void anv_UnmapMemory(
2463 VkDeviceMemory _memory
)
2465 ANV_FROM_HANDLE(anv_device_memory
, mem
, _memory
);
2470 anv_gem_munmap(mem
->map
, mem
->map_size
);
2477 clflush_mapped_ranges(struct anv_device
*device
,
2479 const VkMappedMemoryRange
*ranges
)
2481 for (uint32_t i
= 0; i
< count
; i
++) {
2482 ANV_FROM_HANDLE(anv_device_memory
, mem
, ranges
[i
].memory
);
2483 if (ranges
[i
].offset
>= mem
->map_size
)
2486 gen_clflush_range(mem
->map
+ ranges
[i
].offset
,
2487 MIN2(ranges
[i
].size
, mem
->map_size
- ranges
[i
].offset
));
2491 VkResult
anv_FlushMappedMemoryRanges(
2493 uint32_t memoryRangeCount
,
2494 const VkMappedMemoryRange
* pMemoryRanges
)
2496 ANV_FROM_HANDLE(anv_device
, device
, _device
);
2498 if (device
->info
.has_llc
)
2501 /* Make sure the writes we're flushing have landed. */
2502 __builtin_ia32_mfence();
2504 clflush_mapped_ranges(device
, memoryRangeCount
, pMemoryRanges
);
2509 VkResult
anv_InvalidateMappedMemoryRanges(
2511 uint32_t memoryRangeCount
,
2512 const VkMappedMemoryRange
* pMemoryRanges
)
2514 ANV_FROM_HANDLE(anv_device
, device
, _device
);
2516 if (device
->info
.has_llc
)
2519 clflush_mapped_ranges(device
, memoryRangeCount
, pMemoryRanges
);
2521 /* Make sure no reads get moved up above the invalidate. */
2522 __builtin_ia32_mfence();
2527 void anv_GetBufferMemoryRequirements(
2530 VkMemoryRequirements
* pMemoryRequirements
)
2532 ANV_FROM_HANDLE(anv_buffer
, buffer
, _buffer
);
2533 ANV_FROM_HANDLE(anv_device
, device
, _device
);
2534 struct anv_physical_device
*pdevice
= &device
->instance
->physicalDevice
;
2536 /* The Vulkan spec (git aaed022) says:
2538 * memoryTypeBits is a bitfield and contains one bit set for every
2539 * supported memory type for the resource. The bit `1<<i` is set if and
2540 * only if the memory type `i` in the VkPhysicalDeviceMemoryProperties
2541 * structure for the physical device is supported.
2543 uint32_t memory_types
= 0;
2544 for (uint32_t i
= 0; i
< pdevice
->memory
.type_count
; i
++) {
2545 uint32_t valid_usage
= pdevice
->memory
.types
[i
].valid_buffer_usage
;
2546 if ((valid_usage
& buffer
->usage
) == buffer
->usage
)
2547 memory_types
|= (1u << i
);
2550 /* Base alignment requirement of a cache line */
2551 uint32_t alignment
= 16;
2553 /* We need an alignment of 32 for pushing UBOs */
2554 if (buffer
->usage
& VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT
)
2555 alignment
= MAX2(alignment
, 32);
2557 pMemoryRequirements
->size
= buffer
->size
;
2558 pMemoryRequirements
->alignment
= alignment
;
2560 /* Storage and Uniform buffers should have their size aligned to
2561 * 32-bits to avoid boundary checks when last DWord is not complete.
2562 * This would ensure that not internal padding would be needed for
2565 if (device
->robust_buffer_access
&&
2566 (buffer
->usage
& VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT
||
2567 buffer
->usage
& VK_BUFFER_USAGE_STORAGE_BUFFER_BIT
))
2568 pMemoryRequirements
->size
= align_u64(buffer
->size
, 4);
2570 pMemoryRequirements
->memoryTypeBits
= memory_types
;
2573 void anv_GetBufferMemoryRequirements2(
2575 const VkBufferMemoryRequirementsInfo2
* pInfo
,
2576 VkMemoryRequirements2
* pMemoryRequirements
)
2578 anv_GetBufferMemoryRequirements(_device
, pInfo
->buffer
,
2579 &pMemoryRequirements
->memoryRequirements
);
2581 vk_foreach_struct(ext
, pMemoryRequirements
->pNext
) {
2582 switch (ext
->sType
) {
2583 case VK_STRUCTURE_TYPE_MEMORY_DEDICATED_REQUIREMENTS
: {
2584 VkMemoryDedicatedRequirements
*requirements
= (void *)ext
;
2585 requirements
->prefersDedicatedAllocation
= VK_FALSE
;
2586 requirements
->requiresDedicatedAllocation
= VK_FALSE
;
2591 anv_debug_ignored_stype(ext
->sType
);
2597 void anv_GetImageMemoryRequirements(
2600 VkMemoryRequirements
* pMemoryRequirements
)
2602 ANV_FROM_HANDLE(anv_image
, image
, _image
);
2603 ANV_FROM_HANDLE(anv_device
, device
, _device
);
2604 struct anv_physical_device
*pdevice
= &device
->instance
->physicalDevice
;
2606 /* The Vulkan spec (git aaed022) says:
2608 * memoryTypeBits is a bitfield and contains one bit set for every
2609 * supported memory type for the resource. The bit `1<<i` is set if and
2610 * only if the memory type `i` in the VkPhysicalDeviceMemoryProperties
2611 * structure for the physical device is supported.
2613 * All types are currently supported for images.
2615 uint32_t memory_types
= (1ull << pdevice
->memory
.type_count
) - 1;
2617 pMemoryRequirements
->size
= image
->size
;
2618 pMemoryRequirements
->alignment
= image
->alignment
;
2619 pMemoryRequirements
->memoryTypeBits
= memory_types
;
2622 void anv_GetImageMemoryRequirements2(
2624 const VkImageMemoryRequirementsInfo2
* pInfo
,
2625 VkMemoryRequirements2
* pMemoryRequirements
)
2627 ANV_FROM_HANDLE(anv_device
, device
, _device
);
2628 ANV_FROM_HANDLE(anv_image
, image
, pInfo
->image
);
2630 anv_GetImageMemoryRequirements(_device
, pInfo
->image
,
2631 &pMemoryRequirements
->memoryRequirements
);
2633 vk_foreach_struct_const(ext
, pInfo
->pNext
) {
2634 switch (ext
->sType
) {
2635 case VK_STRUCTURE_TYPE_IMAGE_PLANE_MEMORY_REQUIREMENTS_INFO
: {
2636 struct anv_physical_device
*pdevice
= &device
->instance
->physicalDevice
;
2637 const VkImagePlaneMemoryRequirementsInfoKHR
*plane_reqs
=
2638 (const VkImagePlaneMemoryRequirementsInfoKHR
*) ext
;
2639 uint32_t plane
= anv_image_aspect_to_plane(image
->aspects
,
2640 plane_reqs
->planeAspect
);
2642 assert(image
->planes
[plane
].offset
== 0);
2644 /* The Vulkan spec (git aaed022) says:
2646 * memoryTypeBits is a bitfield and contains one bit set for every
2647 * supported memory type for the resource. The bit `1<<i` is set
2648 * if and only if the memory type `i` in the
2649 * VkPhysicalDeviceMemoryProperties structure for the physical
2650 * device is supported.
2652 * All types are currently supported for images.
2654 pMemoryRequirements
->memoryRequirements
.memoryTypeBits
=
2655 (1ull << pdevice
->memory
.type_count
) - 1;
2657 pMemoryRequirements
->memoryRequirements
.size
= image
->planes
[plane
].size
;
2658 pMemoryRequirements
->memoryRequirements
.alignment
=
2659 image
->planes
[plane
].alignment
;
2664 anv_debug_ignored_stype(ext
->sType
);
2669 vk_foreach_struct(ext
, pMemoryRequirements
->pNext
) {
2670 switch (ext
->sType
) {
2671 case VK_STRUCTURE_TYPE_MEMORY_DEDICATED_REQUIREMENTS
: {
2672 VkMemoryDedicatedRequirements
*requirements
= (void *)ext
;
2673 if (image
->needs_set_tiling
) {
2674 /* If we need to set the tiling for external consumers, we need a
2675 * dedicated allocation.
2677 * See also anv_AllocateMemory.
2679 requirements
->prefersDedicatedAllocation
= VK_TRUE
;
2680 requirements
->requiresDedicatedAllocation
= VK_TRUE
;
2682 requirements
->prefersDedicatedAllocation
= VK_FALSE
;
2683 requirements
->requiresDedicatedAllocation
= VK_FALSE
;
2689 anv_debug_ignored_stype(ext
->sType
);
2695 void anv_GetImageSparseMemoryRequirements(
2698 uint32_t* pSparseMemoryRequirementCount
,
2699 VkSparseImageMemoryRequirements
* pSparseMemoryRequirements
)
2701 *pSparseMemoryRequirementCount
= 0;
2704 void anv_GetImageSparseMemoryRequirements2(
2706 const VkImageSparseMemoryRequirementsInfo2
* pInfo
,
2707 uint32_t* pSparseMemoryRequirementCount
,
2708 VkSparseImageMemoryRequirements2
* pSparseMemoryRequirements
)
2710 *pSparseMemoryRequirementCount
= 0;
2713 void anv_GetDeviceMemoryCommitment(
2715 VkDeviceMemory memory
,
2716 VkDeviceSize
* pCommittedMemoryInBytes
)
2718 *pCommittedMemoryInBytes
= 0;
2722 anv_bind_buffer_memory(const VkBindBufferMemoryInfo
*pBindInfo
)
2724 ANV_FROM_HANDLE(anv_device_memory
, mem
, pBindInfo
->memory
);
2725 ANV_FROM_HANDLE(anv_buffer
, buffer
, pBindInfo
->buffer
);
2727 assert(pBindInfo
->sType
== VK_STRUCTURE_TYPE_BIND_BUFFER_MEMORY_INFO
);
2730 assert((buffer
->usage
& mem
->type
->valid_buffer_usage
) == buffer
->usage
);
2731 buffer
->address
= (struct anv_address
) {
2733 .offset
= pBindInfo
->memoryOffset
,
2736 buffer
->address
= ANV_NULL_ADDRESS
;
2740 VkResult
anv_BindBufferMemory(
2743 VkDeviceMemory memory
,
2744 VkDeviceSize memoryOffset
)
2746 anv_bind_buffer_memory(
2747 &(VkBindBufferMemoryInfo
) {
2748 .sType
= VK_STRUCTURE_TYPE_BIND_BUFFER_MEMORY_INFO
,
2751 .memoryOffset
= memoryOffset
,
2757 VkResult
anv_BindBufferMemory2(
2759 uint32_t bindInfoCount
,
2760 const VkBindBufferMemoryInfo
* pBindInfos
)
2762 for (uint32_t i
= 0; i
< bindInfoCount
; i
++)
2763 anv_bind_buffer_memory(&pBindInfos
[i
]);
2768 VkResult
anv_QueueBindSparse(
2770 uint32_t bindInfoCount
,
2771 const VkBindSparseInfo
* pBindInfo
,
2774 ANV_FROM_HANDLE(anv_queue
, queue
, _queue
);
2775 if (unlikely(queue
->device
->lost
))
2776 return VK_ERROR_DEVICE_LOST
;
2778 return vk_error(VK_ERROR_FEATURE_NOT_PRESENT
);
2783 VkResult
anv_CreateEvent(
2785 const VkEventCreateInfo
* pCreateInfo
,
2786 const VkAllocationCallbacks
* pAllocator
,
2789 ANV_FROM_HANDLE(anv_device
, device
, _device
);
2790 struct anv_state state
;
2791 struct anv_event
*event
;
2793 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_EVENT_CREATE_INFO
);
2795 state
= anv_state_pool_alloc(&device
->dynamic_state_pool
,
2798 event
->state
= state
;
2799 event
->semaphore
= VK_EVENT_RESET
;
2801 if (!device
->info
.has_llc
) {
2802 /* Make sure the writes we're flushing have landed. */
2803 __builtin_ia32_mfence();
2804 __builtin_ia32_clflush(event
);
2807 *pEvent
= anv_event_to_handle(event
);
2812 void anv_DestroyEvent(
2815 const VkAllocationCallbacks
* pAllocator
)
2817 ANV_FROM_HANDLE(anv_device
, device
, _device
);
2818 ANV_FROM_HANDLE(anv_event
, event
, _event
);
2823 anv_state_pool_free(&device
->dynamic_state_pool
, event
->state
);
2826 VkResult
anv_GetEventStatus(
2830 ANV_FROM_HANDLE(anv_device
, device
, _device
);
2831 ANV_FROM_HANDLE(anv_event
, event
, _event
);
2833 if (unlikely(device
->lost
))
2834 return VK_ERROR_DEVICE_LOST
;
2836 if (!device
->info
.has_llc
) {
2837 /* Invalidate read cache before reading event written by GPU. */
2838 __builtin_ia32_clflush(event
);
2839 __builtin_ia32_mfence();
2843 return event
->semaphore
;
2846 VkResult
anv_SetEvent(
2850 ANV_FROM_HANDLE(anv_device
, device
, _device
);
2851 ANV_FROM_HANDLE(anv_event
, event
, _event
);
2853 event
->semaphore
= VK_EVENT_SET
;
2855 if (!device
->info
.has_llc
) {
2856 /* Make sure the writes we're flushing have landed. */
2857 __builtin_ia32_mfence();
2858 __builtin_ia32_clflush(event
);
2864 VkResult
anv_ResetEvent(
2868 ANV_FROM_HANDLE(anv_device
, device
, _device
);
2869 ANV_FROM_HANDLE(anv_event
, event
, _event
);
2871 event
->semaphore
= VK_EVENT_RESET
;
2873 if (!device
->info
.has_llc
) {
2874 /* Make sure the writes we're flushing have landed. */
2875 __builtin_ia32_mfence();
2876 __builtin_ia32_clflush(event
);
2884 VkResult
anv_CreateBuffer(
2886 const VkBufferCreateInfo
* pCreateInfo
,
2887 const VkAllocationCallbacks
* pAllocator
,
2890 ANV_FROM_HANDLE(anv_device
, device
, _device
);
2891 struct anv_buffer
*buffer
;
2893 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO
);
2895 buffer
= vk_alloc2(&device
->alloc
, pAllocator
, sizeof(*buffer
), 8,
2896 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
2898 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
2900 buffer
->size
= pCreateInfo
->size
;
2901 buffer
->usage
= pCreateInfo
->usage
;
2902 buffer
->address
= ANV_NULL_ADDRESS
;
2904 *pBuffer
= anv_buffer_to_handle(buffer
);
2909 void anv_DestroyBuffer(
2912 const VkAllocationCallbacks
* pAllocator
)
2914 ANV_FROM_HANDLE(anv_device
, device
, _device
);
2915 ANV_FROM_HANDLE(anv_buffer
, buffer
, _buffer
);
2920 vk_free2(&device
->alloc
, pAllocator
, buffer
);
2924 anv_fill_buffer_surface_state(struct anv_device
*device
, struct anv_state state
,
2925 enum isl_format format
,
2926 struct anv_address address
,
2927 uint32_t range
, uint32_t stride
)
2929 isl_buffer_fill_state(&device
->isl_dev
, state
.map
,
2930 .address
= anv_address_physical(address
),
2931 .mocs
= device
->default_mocs
,
2934 .stride_B
= stride
);
2936 anv_state_flush(device
, state
);
2939 void anv_DestroySampler(
2942 const VkAllocationCallbacks
* pAllocator
)
2944 ANV_FROM_HANDLE(anv_device
, device
, _device
);
2945 ANV_FROM_HANDLE(anv_sampler
, sampler
, _sampler
);
2950 vk_free2(&device
->alloc
, pAllocator
, sampler
);
2953 VkResult
anv_CreateFramebuffer(
2955 const VkFramebufferCreateInfo
* pCreateInfo
,
2956 const VkAllocationCallbacks
* pAllocator
,
2957 VkFramebuffer
* pFramebuffer
)
2959 ANV_FROM_HANDLE(anv_device
, device
, _device
);
2960 struct anv_framebuffer
*framebuffer
;
2962 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO
);
2964 size_t size
= sizeof(*framebuffer
) +
2965 sizeof(struct anv_image_view
*) * pCreateInfo
->attachmentCount
;
2966 framebuffer
= vk_alloc2(&device
->alloc
, pAllocator
, size
, 8,
2967 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
2968 if (framebuffer
== NULL
)
2969 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
2971 framebuffer
->attachment_count
= pCreateInfo
->attachmentCount
;
2972 for (uint32_t i
= 0; i
< pCreateInfo
->attachmentCount
; i
++) {
2973 VkImageView _iview
= pCreateInfo
->pAttachments
[i
];
2974 framebuffer
->attachments
[i
] = anv_image_view_from_handle(_iview
);
2977 framebuffer
->width
= pCreateInfo
->width
;
2978 framebuffer
->height
= pCreateInfo
->height
;
2979 framebuffer
->layers
= pCreateInfo
->layers
;
2981 *pFramebuffer
= anv_framebuffer_to_handle(framebuffer
);
2986 void anv_DestroyFramebuffer(
2989 const VkAllocationCallbacks
* pAllocator
)
2991 ANV_FROM_HANDLE(anv_device
, device
, _device
);
2992 ANV_FROM_HANDLE(anv_framebuffer
, fb
, _fb
);
2997 vk_free2(&device
->alloc
, pAllocator
, fb
);
3000 /* vk_icd.h does not declare this function, so we declare it here to
3001 * suppress Wmissing-prototypes.
3003 PUBLIC VKAPI_ATTR VkResult VKAPI_CALL
3004 vk_icdNegotiateLoaderICDInterfaceVersion(uint32_t* pSupportedVersion
);
3006 PUBLIC VKAPI_ATTR VkResult VKAPI_CALL
3007 vk_icdNegotiateLoaderICDInterfaceVersion(uint32_t* pSupportedVersion
)
3009 /* For the full details on loader interface versioning, see
3010 * <https://github.com/KhronosGroup/Vulkan-LoaderAndValidationLayers/blob/master/loader/LoaderAndLayerInterface.md>.
3011 * What follows is a condensed summary, to help you navigate the large and
3012 * confusing official doc.
3014 * - Loader interface v0 is incompatible with later versions. We don't
3017 * - In loader interface v1:
3018 * - The first ICD entrypoint called by the loader is
3019 * vk_icdGetInstanceProcAddr(). The ICD must statically expose this
3021 * - The ICD must statically expose no other Vulkan symbol unless it is
3022 * linked with -Bsymbolic.
3023 * - Each dispatchable Vulkan handle created by the ICD must be
3024 * a pointer to a struct whose first member is VK_LOADER_DATA. The
3025 * ICD must initialize VK_LOADER_DATA.loadMagic to ICD_LOADER_MAGIC.
3026 * - The loader implements vkCreate{PLATFORM}SurfaceKHR() and
3027 * vkDestroySurfaceKHR(). The ICD must be capable of working with
3028 * such loader-managed surfaces.
3030 * - Loader interface v2 differs from v1 in:
3031 * - The first ICD entrypoint called by the loader is
3032 * vk_icdNegotiateLoaderICDInterfaceVersion(). The ICD must
3033 * statically expose this entrypoint.
3035 * - Loader interface v3 differs from v2 in:
3036 * - The ICD must implement vkCreate{PLATFORM}SurfaceKHR(),
3037 * vkDestroySurfaceKHR(), and other API which uses VKSurfaceKHR,
3038 * because the loader no longer does so.
3040 *pSupportedVersion
= MIN2(*pSupportedVersion
, 3u);