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
30 #include "anv_private.h"
31 #include "anv_timestamp.h"
32 #include "util/strtod.h"
33 #include "util/debug.h"
35 #include "genxml/gen7_pack.h"
37 struct anv_dispatch_table dtable
;
40 compiler_debug_log(void *data
, const char *fmt
, ...)
44 compiler_perf_log(void *data
, const char *fmt
, ...)
49 if (unlikely(INTEL_DEBUG
& DEBUG_PERF
))
50 vfprintf(stderr
, fmt
, args
);
56 anv_physical_device_init(struct anv_physical_device
*device
,
57 struct anv_instance
*instance
,
63 fd
= open(path
, O_RDWR
| O_CLOEXEC
);
65 return vk_error(VK_ERROR_INCOMPATIBLE_DRIVER
);
67 device
->_loader_data
.loaderMagic
= ICD_LOADER_MAGIC
;
68 device
->instance
= instance
;
70 assert(strlen(path
) < ARRAY_SIZE(device
->path
));
71 strncpy(device
->path
, path
, ARRAY_SIZE(device
->path
));
73 device
->chipset_id
= anv_gem_get_param(fd
, I915_PARAM_CHIPSET_ID
);
74 if (!device
->chipset_id
) {
75 result
= vk_error(VK_ERROR_INCOMPATIBLE_DRIVER
);
79 device
->name
= gen_get_device_name(device
->chipset_id
);
80 device
->info
= gen_get_device_info(device
->chipset_id
);
82 result
= vk_error(VK_ERROR_INCOMPATIBLE_DRIVER
);
86 if (device
->info
->is_haswell
) {
87 fprintf(stderr
, "WARNING: Haswell Vulkan support is incomplete\n");
88 } else if (device
->info
->gen
== 7 && !device
->info
->is_baytrail
) {
89 fprintf(stderr
, "WARNING: Ivy Bridge Vulkan support is incomplete\n");
90 } else if (device
->info
->gen
== 7 && device
->info
->is_baytrail
) {
91 fprintf(stderr
, "WARNING: Bay Trail Vulkan support is incomplete\n");
92 } else if (device
->info
->gen
>= 8) {
93 /* Broadwell, Cherryview, Skylake, Broxton, Kabylake is as fully
94 * supported as anything */
96 result
= vk_errorf(VK_ERROR_INCOMPATIBLE_DRIVER
,
97 "Vulkan not yet supported on %s", device
->name
);
101 device
->cmd_parser_version
= -1;
102 if (device
->info
->gen
== 7) {
103 device
->cmd_parser_version
=
104 anv_gem_get_param(fd
, I915_PARAM_CMD_PARSER_VERSION
);
105 if (device
->cmd_parser_version
== -1) {
106 result
= vk_errorf(VK_ERROR_INITIALIZATION_FAILED
,
107 "failed to get command parser version");
112 if (anv_gem_get_aperture(fd
, &device
->aperture_size
) == -1) {
113 result
= vk_errorf(VK_ERROR_INITIALIZATION_FAILED
,
114 "failed to get aperture size: %m");
118 if (!anv_gem_get_param(fd
, I915_PARAM_HAS_WAIT_TIMEOUT
)) {
119 result
= vk_errorf(VK_ERROR_INITIALIZATION_FAILED
,
120 "kernel missing gem wait");
124 if (!anv_gem_get_param(fd
, I915_PARAM_HAS_EXECBUF2
)) {
125 result
= vk_errorf(VK_ERROR_INITIALIZATION_FAILED
,
126 "kernel missing execbuf2");
130 if (!device
->info
->has_llc
&&
131 anv_gem_get_param(fd
, I915_PARAM_MMAP_VERSION
) < 1) {
132 result
= vk_errorf(VK_ERROR_INITIALIZATION_FAILED
,
133 "kernel missing wc mmap");
137 bool swizzled
= anv_gem_get_bit6_swizzle(fd
, I915_TILING_X
);
139 device
->max_vs_threads
= device
->info
->max_vs_threads
;
140 device
->max_hs_threads
= device
->info
->max_hs_threads
;
141 device
->max_ds_threads
= device
->info
->max_ds_threads
;
142 device
->max_gs_threads
= device
->info
->max_gs_threads
;
143 device
->max_wm_threads
= device
->info
->max_wm_threads
;
145 /* GENs prior to 8 do not support EU/Subslice info */
146 if (device
->info
->gen
>= 8) {
147 device
->subslice_total
= anv_gem_get_param(fd
, I915_PARAM_SUBSLICE_TOTAL
);
148 device
->eu_total
= anv_gem_get_param(fd
, I915_PARAM_EU_TOTAL
);
150 /* Without this information, we cannot get the right Braswell
151 * brandstrings, and we have to use conservative numbers for GPGPU on
152 * many platforms, but otherwise, things will just work.
154 if (device
->subslice_total
< 1 || device
->eu_total
< 1) {
155 fprintf(stderr
, "WARNING: Kernel 4.1 required to properly"
156 " query GPU properties.\n");
158 } else if (device
->info
->gen
== 7) {
159 device
->subslice_total
= 1 << (device
->info
->gt
- 1);
162 if (device
->info
->is_cherryview
&&
163 device
->subslice_total
> 0 && device
->eu_total
> 0) {
164 /* Logical CS threads = EUs per subslice * 7 threads per EU */
165 device
->max_cs_threads
= device
->eu_total
/ device
->subslice_total
* 7;
167 /* Fuse configurations may give more threads than expected, never less. */
168 if (device
->max_cs_threads
< device
->info
->max_cs_threads
)
169 device
->max_cs_threads
= device
->info
->max_cs_threads
;
171 device
->max_cs_threads
= device
->info
->max_cs_threads
;
176 brw_process_intel_debug_variable();
178 device
->compiler
= brw_compiler_create(NULL
, device
->info
);
179 if (device
->compiler
== NULL
) {
180 result
= vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
183 device
->compiler
->shader_debug_log
= compiler_debug_log
;
184 device
->compiler
->shader_perf_log
= compiler_perf_log
;
186 result
= anv_init_wsi(device
);
187 if (result
!= VK_SUCCESS
)
190 /* XXX: Actually detect bit6 swizzling */
191 isl_device_init(&device
->isl_dev
, device
->info
, swizzled
);
201 anv_physical_device_finish(struct anv_physical_device
*device
)
203 anv_finish_wsi(device
);
204 ralloc_free(device
->compiler
);
207 static const VkExtensionProperties global_extensions
[] = {
209 .extensionName
= VK_KHR_SURFACE_EXTENSION_NAME
,
212 #ifdef VK_USE_PLATFORM_XCB_KHR
214 .extensionName
= VK_KHR_XCB_SURFACE_EXTENSION_NAME
,
218 #ifdef VK_USE_PLATFORM_XLIB_KHR
220 .extensionName
= VK_KHR_XLIB_SURFACE_EXTENSION_NAME
,
224 #ifdef VK_USE_PLATFORM_WAYLAND_KHR
226 .extensionName
= VK_KHR_WAYLAND_SURFACE_EXTENSION_NAME
,
232 static const VkExtensionProperties device_extensions
[] = {
234 .extensionName
= VK_KHR_SWAPCHAIN_EXTENSION_NAME
,
240 default_alloc_func(void *pUserData
, size_t size
, size_t align
,
241 VkSystemAllocationScope allocationScope
)
247 default_realloc_func(void *pUserData
, void *pOriginal
, size_t size
,
248 size_t align
, VkSystemAllocationScope allocationScope
)
250 return realloc(pOriginal
, size
);
254 default_free_func(void *pUserData
, void *pMemory
)
259 static const VkAllocationCallbacks default_alloc
= {
261 .pfnAllocation
= default_alloc_func
,
262 .pfnReallocation
= default_realloc_func
,
263 .pfnFree
= default_free_func
,
266 VkResult
anv_CreateInstance(
267 const VkInstanceCreateInfo
* pCreateInfo
,
268 const VkAllocationCallbacks
* pAllocator
,
269 VkInstance
* pInstance
)
271 struct anv_instance
*instance
;
273 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO
);
275 uint32_t client_version
;
276 if (pCreateInfo
->pApplicationInfo
&&
277 pCreateInfo
->pApplicationInfo
->apiVersion
!= 0) {
278 client_version
= pCreateInfo
->pApplicationInfo
->apiVersion
;
280 client_version
= VK_MAKE_VERSION(1, 0, 0);
283 if (VK_MAKE_VERSION(1, 0, 0) > client_version
||
284 client_version
> VK_MAKE_VERSION(1, 0, 0xfff)) {
285 return vk_errorf(VK_ERROR_INCOMPATIBLE_DRIVER
,
286 "Client requested version %d.%d.%d",
287 VK_VERSION_MAJOR(client_version
),
288 VK_VERSION_MINOR(client_version
),
289 VK_VERSION_PATCH(client_version
));
292 for (uint32_t i
= 0; i
< pCreateInfo
->enabledExtensionCount
; i
++) {
294 for (uint32_t j
= 0; j
< ARRAY_SIZE(global_extensions
); j
++) {
295 if (strcmp(pCreateInfo
->ppEnabledExtensionNames
[i
],
296 global_extensions
[j
].extensionName
) == 0) {
302 return vk_error(VK_ERROR_EXTENSION_NOT_PRESENT
);
305 instance
= anv_alloc2(&default_alloc
, pAllocator
, sizeof(*instance
), 8,
306 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE
);
308 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
310 instance
->_loader_data
.loaderMagic
= ICD_LOADER_MAGIC
;
313 instance
->alloc
= *pAllocator
;
315 instance
->alloc
= default_alloc
;
317 instance
->apiVersion
= client_version
;
318 instance
->physicalDeviceCount
= -1;
322 VG(VALGRIND_CREATE_MEMPOOL(instance
, 0, false));
324 *pInstance
= anv_instance_to_handle(instance
);
329 void anv_DestroyInstance(
330 VkInstance _instance
,
331 const VkAllocationCallbacks
* pAllocator
)
333 ANV_FROM_HANDLE(anv_instance
, instance
, _instance
);
335 if (instance
->physicalDeviceCount
> 0) {
336 /* We support at most one physical device. */
337 assert(instance
->physicalDeviceCount
== 1);
338 anv_physical_device_finish(&instance
->physicalDevice
);
341 VG(VALGRIND_DESTROY_MEMPOOL(instance
));
345 anv_free(&instance
->alloc
, instance
);
348 VkResult
anv_EnumeratePhysicalDevices(
349 VkInstance _instance
,
350 uint32_t* pPhysicalDeviceCount
,
351 VkPhysicalDevice
* pPhysicalDevices
)
353 ANV_FROM_HANDLE(anv_instance
, instance
, _instance
);
356 if (instance
->physicalDeviceCount
< 0) {
358 for (unsigned i
= 0; i
< 8; i
++) {
359 snprintf(path
, sizeof(path
), "/dev/dri/renderD%d", 128 + i
);
360 result
= anv_physical_device_init(&instance
->physicalDevice
,
362 if (result
== VK_SUCCESS
)
366 if (result
== VK_ERROR_INCOMPATIBLE_DRIVER
) {
367 instance
->physicalDeviceCount
= 0;
368 } else if (result
== VK_SUCCESS
) {
369 instance
->physicalDeviceCount
= 1;
375 /* pPhysicalDeviceCount is an out parameter if pPhysicalDevices is NULL;
376 * otherwise it's an inout parameter.
378 * The Vulkan spec (git aaed022) says:
380 * pPhysicalDeviceCount is a pointer to an unsigned integer variable
381 * that is initialized with the number of devices the application is
382 * prepared to receive handles to. pname:pPhysicalDevices is pointer to
383 * an array of at least this many VkPhysicalDevice handles [...].
385 * Upon success, if pPhysicalDevices is NULL, vkEnumeratePhysicalDevices
386 * overwrites the contents of the variable pointed to by
387 * pPhysicalDeviceCount with the number of physical devices in in the
388 * instance; otherwise, vkEnumeratePhysicalDevices overwrites
389 * pPhysicalDeviceCount with the number of physical handles written to
392 if (!pPhysicalDevices
) {
393 *pPhysicalDeviceCount
= instance
->physicalDeviceCount
;
394 } else if (*pPhysicalDeviceCount
>= 1) {
395 pPhysicalDevices
[0] = anv_physical_device_to_handle(&instance
->physicalDevice
);
396 *pPhysicalDeviceCount
= 1;
398 *pPhysicalDeviceCount
= 0;
404 void anv_GetPhysicalDeviceFeatures(
405 VkPhysicalDevice physicalDevice
,
406 VkPhysicalDeviceFeatures
* pFeatures
)
408 ANV_FROM_HANDLE(anv_physical_device
, pdevice
, physicalDevice
);
410 *pFeatures
= (VkPhysicalDeviceFeatures
) {
411 .robustBufferAccess
= true,
412 .fullDrawIndexUint32
= true,
413 .imageCubeArray
= false,
414 .independentBlend
= true,
415 .geometryShader
= true,
416 .tessellationShader
= false,
417 .sampleRateShading
= true,
418 .dualSrcBlend
= true,
420 .multiDrawIndirect
= false,
421 .drawIndirectFirstInstance
= false,
423 .depthBiasClamp
= false,
424 .fillModeNonSolid
= true,
425 .depthBounds
= false,
429 .multiViewport
= true,
430 .samplerAnisotropy
= false, /* FINISHME */
431 .textureCompressionETC2
= pdevice
->info
->gen
>= 8 ||
432 pdevice
->info
->is_baytrail
,
433 .textureCompressionASTC_LDR
= pdevice
->info
->gen
>= 9, /* FINISHME CHV */
434 .textureCompressionBC
= true,
435 .occlusionQueryPrecise
= true,
436 .pipelineStatisticsQuery
= false,
437 .fragmentStoresAndAtomics
= true,
438 .shaderTessellationAndGeometryPointSize
= true,
439 .shaderImageGatherExtended
= false,
440 .shaderStorageImageExtendedFormats
= false,
441 .shaderStorageImageMultisample
= false,
442 .shaderUniformBufferArrayDynamicIndexing
= true,
443 .shaderSampledImageArrayDynamicIndexing
= true,
444 .shaderStorageBufferArrayDynamicIndexing
= true,
445 .shaderStorageImageArrayDynamicIndexing
= true,
446 .shaderStorageImageReadWithoutFormat
= false,
447 .shaderStorageImageWriteWithoutFormat
= true,
448 .shaderClipDistance
= false,
449 .shaderCullDistance
= false,
450 .shaderFloat64
= false,
451 .shaderInt64
= false,
452 .shaderInt16
= false,
454 .variableMultisampleRate
= false,
455 .inheritedQueries
= false,
458 /* We can't do image stores in vec4 shaders */
459 pFeatures
->vertexPipelineStoresAndAtomics
=
460 pdevice
->compiler
->scalar_stage
[MESA_SHADER_VERTEX
] &&
461 pdevice
->compiler
->scalar_stage
[MESA_SHADER_GEOMETRY
];
465 anv_device_get_cache_uuid(void *uuid
)
467 memset(uuid
, 0, VK_UUID_SIZE
);
468 snprintf(uuid
, VK_UUID_SIZE
, "anv-%s", ANV_TIMESTAMP
);
471 void anv_GetPhysicalDeviceProperties(
472 VkPhysicalDevice physicalDevice
,
473 VkPhysicalDeviceProperties
* pProperties
)
475 ANV_FROM_HANDLE(anv_physical_device
, pdevice
, physicalDevice
);
476 const struct gen_device_info
*devinfo
= pdevice
->info
;
478 const float time_stamp_base
= devinfo
->gen
>= 9 ? 83.333 : 80.0;
480 /* See assertions made when programming the buffer surface state. */
481 const uint32_t max_raw_buffer_sz
= devinfo
->gen
>= 7 ?
482 (1ul << 30) : (1ul << 27);
484 VkSampleCountFlags sample_counts
=
485 isl_device_get_sample_counts(&pdevice
->isl_dev
);
487 VkPhysicalDeviceLimits limits
= {
488 .maxImageDimension1D
= (1 << 14),
489 .maxImageDimension2D
= (1 << 14),
490 .maxImageDimension3D
= (1 << 11),
491 .maxImageDimensionCube
= (1 << 14),
492 .maxImageArrayLayers
= (1 << 11),
493 .maxTexelBufferElements
= 128 * 1024 * 1024,
494 .maxUniformBufferRange
= (1ul << 27),
495 .maxStorageBufferRange
= max_raw_buffer_sz
,
496 .maxPushConstantsSize
= MAX_PUSH_CONSTANTS_SIZE
,
497 .maxMemoryAllocationCount
= UINT32_MAX
,
498 .maxSamplerAllocationCount
= 64 * 1024,
499 .bufferImageGranularity
= 64, /* A cache line */
500 .sparseAddressSpaceSize
= 0,
501 .maxBoundDescriptorSets
= MAX_SETS
,
502 .maxPerStageDescriptorSamplers
= 64,
503 .maxPerStageDescriptorUniformBuffers
= 64,
504 .maxPerStageDescriptorStorageBuffers
= 64,
505 .maxPerStageDescriptorSampledImages
= 64,
506 .maxPerStageDescriptorStorageImages
= 64,
507 .maxPerStageDescriptorInputAttachments
= 64,
508 .maxPerStageResources
= 128,
509 .maxDescriptorSetSamplers
= 256,
510 .maxDescriptorSetUniformBuffers
= 256,
511 .maxDescriptorSetUniformBuffersDynamic
= 256,
512 .maxDescriptorSetStorageBuffers
= 256,
513 .maxDescriptorSetStorageBuffersDynamic
= 256,
514 .maxDescriptorSetSampledImages
= 256,
515 .maxDescriptorSetStorageImages
= 256,
516 .maxDescriptorSetInputAttachments
= 256,
517 .maxVertexInputAttributes
= 32,
518 .maxVertexInputBindings
= 32,
519 .maxVertexInputAttributeOffset
= 2047,
520 .maxVertexInputBindingStride
= 2048,
521 .maxVertexOutputComponents
= 128,
522 .maxTessellationGenerationLevel
= 0,
523 .maxTessellationPatchSize
= 0,
524 .maxTessellationControlPerVertexInputComponents
= 0,
525 .maxTessellationControlPerVertexOutputComponents
= 0,
526 .maxTessellationControlPerPatchOutputComponents
= 0,
527 .maxTessellationControlTotalOutputComponents
= 0,
528 .maxTessellationEvaluationInputComponents
= 0,
529 .maxTessellationEvaluationOutputComponents
= 0,
530 .maxGeometryShaderInvocations
= 32,
531 .maxGeometryInputComponents
= 64,
532 .maxGeometryOutputComponents
= 128,
533 .maxGeometryOutputVertices
= 256,
534 .maxGeometryTotalOutputComponents
= 1024,
535 .maxFragmentInputComponents
= 128,
536 .maxFragmentOutputAttachments
= 8,
537 .maxFragmentDualSrcAttachments
= 2,
538 .maxFragmentCombinedOutputResources
= 8,
539 .maxComputeSharedMemorySize
= 32768,
540 .maxComputeWorkGroupCount
= { 65535, 65535, 65535 },
541 .maxComputeWorkGroupInvocations
= 16 * pdevice
->max_cs_threads
,
542 .maxComputeWorkGroupSize
= {
543 16 * pdevice
->max_cs_threads
,
544 16 * pdevice
->max_cs_threads
,
545 16 * pdevice
->max_cs_threads
,
547 .subPixelPrecisionBits
= 4 /* FIXME */,
548 .subTexelPrecisionBits
= 4 /* FIXME */,
549 .mipmapPrecisionBits
= 4 /* FIXME */,
550 .maxDrawIndexedIndexValue
= UINT32_MAX
,
551 .maxDrawIndirectCount
= UINT32_MAX
,
552 .maxSamplerLodBias
= 16,
553 .maxSamplerAnisotropy
= 16,
554 .maxViewports
= MAX_VIEWPORTS
,
555 .maxViewportDimensions
= { (1 << 14), (1 << 14) },
556 .viewportBoundsRange
= { INT16_MIN
, INT16_MAX
},
557 .viewportSubPixelBits
= 13, /* We take a float? */
558 .minMemoryMapAlignment
= 4096, /* A page */
559 .minTexelBufferOffsetAlignment
= 1,
560 .minUniformBufferOffsetAlignment
= 1,
561 .minStorageBufferOffsetAlignment
= 1,
562 .minTexelOffset
= -8,
564 .minTexelGatherOffset
= -8,
565 .maxTexelGatherOffset
= 7,
566 .minInterpolationOffset
= -0.5,
567 .maxInterpolationOffset
= 0.4375,
568 .subPixelInterpolationOffsetBits
= 4,
569 .maxFramebufferWidth
= (1 << 14),
570 .maxFramebufferHeight
= (1 << 14),
571 .maxFramebufferLayers
= (1 << 10),
572 .framebufferColorSampleCounts
= sample_counts
,
573 .framebufferDepthSampleCounts
= sample_counts
,
574 .framebufferStencilSampleCounts
= sample_counts
,
575 .framebufferNoAttachmentsSampleCounts
= sample_counts
,
576 .maxColorAttachments
= MAX_RTS
,
577 .sampledImageColorSampleCounts
= sample_counts
,
578 .sampledImageIntegerSampleCounts
= VK_SAMPLE_COUNT_1_BIT
,
579 .sampledImageDepthSampleCounts
= sample_counts
,
580 .sampledImageStencilSampleCounts
= sample_counts
,
581 .storageImageSampleCounts
= VK_SAMPLE_COUNT_1_BIT
,
582 .maxSampleMaskWords
= 1,
583 .timestampComputeAndGraphics
= false,
584 .timestampPeriod
= time_stamp_base
/ (1000 * 1000 * 1000),
585 .maxClipDistances
= 0 /* FIXME */,
586 .maxCullDistances
= 0 /* FIXME */,
587 .maxCombinedClipAndCullDistances
= 0 /* FIXME */,
588 .discreteQueuePriorities
= 1,
589 .pointSizeRange
= { 0.125, 255.875 },
590 .lineWidthRange
= { 0.0, 7.9921875 },
591 .pointSizeGranularity
= (1.0 / 8.0),
592 .lineWidthGranularity
= (1.0 / 128.0),
593 .strictLines
= false, /* FINISHME */
594 .standardSampleLocations
= true,
595 .optimalBufferCopyOffsetAlignment
= 128,
596 .optimalBufferCopyRowPitchAlignment
= 128,
597 .nonCoherentAtomSize
= 64,
600 *pProperties
= (VkPhysicalDeviceProperties
) {
601 .apiVersion
= VK_MAKE_VERSION(1, 0, 5),
604 .deviceID
= pdevice
->chipset_id
,
605 .deviceType
= VK_PHYSICAL_DEVICE_TYPE_INTEGRATED_GPU
,
607 .sparseProperties
= {0}, /* Broadwell doesn't do sparse. */
610 strcpy(pProperties
->deviceName
, pdevice
->name
);
611 anv_device_get_cache_uuid(pProperties
->pipelineCacheUUID
);
614 void anv_GetPhysicalDeviceQueueFamilyProperties(
615 VkPhysicalDevice physicalDevice
,
617 VkQueueFamilyProperties
* pQueueFamilyProperties
)
619 if (pQueueFamilyProperties
== NULL
) {
624 assert(*pCount
>= 1);
626 *pQueueFamilyProperties
= (VkQueueFamilyProperties
) {
627 .queueFlags
= VK_QUEUE_GRAPHICS_BIT
|
628 VK_QUEUE_COMPUTE_BIT
|
629 VK_QUEUE_TRANSFER_BIT
,
631 .timestampValidBits
= 36, /* XXX: Real value here */
632 .minImageTransferGranularity
= (VkExtent3D
) { 1, 1, 1 },
636 void anv_GetPhysicalDeviceMemoryProperties(
637 VkPhysicalDevice physicalDevice
,
638 VkPhysicalDeviceMemoryProperties
* pMemoryProperties
)
640 ANV_FROM_HANDLE(anv_physical_device
, physical_device
, physicalDevice
);
641 VkDeviceSize heap_size
;
643 /* Reserve some wiggle room for the driver by exposing only 75% of the
644 * aperture to the heap.
646 heap_size
= 3 * physical_device
->aperture_size
/ 4;
648 if (physical_device
->info
->has_llc
) {
649 /* Big core GPUs share LLC with the CPU and thus one memory type can be
650 * both cached and coherent at the same time.
652 pMemoryProperties
->memoryTypeCount
= 1;
653 pMemoryProperties
->memoryTypes
[0] = (VkMemoryType
) {
654 .propertyFlags
= VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT
|
655 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT
|
656 VK_MEMORY_PROPERTY_HOST_COHERENT_BIT
|
657 VK_MEMORY_PROPERTY_HOST_CACHED_BIT
,
661 /* The spec requires that we expose a host-visible, coherent memory
662 * type, but Atom GPUs don't share LLC. Thus we offer two memory types
663 * to give the application a choice between cached, but not coherent and
664 * coherent but uncached (WC though).
666 pMemoryProperties
->memoryTypeCount
= 2;
667 pMemoryProperties
->memoryTypes
[0] = (VkMemoryType
) {
668 .propertyFlags
= VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT
|
669 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT
|
670 VK_MEMORY_PROPERTY_HOST_COHERENT_BIT
,
673 pMemoryProperties
->memoryTypes
[1] = (VkMemoryType
) {
674 .propertyFlags
= VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT
|
675 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT
|
676 VK_MEMORY_PROPERTY_HOST_CACHED_BIT
,
681 pMemoryProperties
->memoryHeapCount
= 1;
682 pMemoryProperties
->memoryHeaps
[0] = (VkMemoryHeap
) {
684 .flags
= VK_MEMORY_HEAP_DEVICE_LOCAL_BIT
,
688 PFN_vkVoidFunction
anv_GetInstanceProcAddr(
692 return anv_lookup_entrypoint(pName
);
695 /* With version 1+ of the loader interface the ICD should expose
696 * vk_icdGetInstanceProcAddr to work around certain LD_PRELOAD issues seen in apps.
699 VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL
vk_icdGetInstanceProcAddr(
704 VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL
vk_icdGetInstanceProcAddr(
708 return anv_GetInstanceProcAddr(instance
, pName
);
711 PFN_vkVoidFunction
anv_GetDeviceProcAddr(
715 return anv_lookup_entrypoint(pName
);
719 anv_queue_init(struct anv_device
*device
, struct anv_queue
*queue
)
721 queue
->_loader_data
.loaderMagic
= ICD_LOADER_MAGIC
;
722 queue
->device
= device
;
723 queue
->pool
= &device
->surface_state_pool
;
729 anv_queue_finish(struct anv_queue
*queue
)
733 static struct anv_state
734 anv_state_pool_emit_data(struct anv_state_pool
*pool
, size_t size
, size_t align
, const void *p
)
736 struct anv_state state
;
738 state
= anv_state_pool_alloc(pool
, size
, align
);
739 memcpy(state
.map
, p
, size
);
741 if (!pool
->block_pool
->device
->info
.has_llc
)
742 anv_state_clflush(state
);
747 struct gen8_border_color
{
752 /* Pad out to 64 bytes */
757 anv_device_init_border_colors(struct anv_device
*device
)
759 static const struct gen8_border_color border_colors
[] = {
760 [VK_BORDER_COLOR_FLOAT_TRANSPARENT_BLACK
] = { .float32
= { 0.0, 0.0, 0.0, 0.0 } },
761 [VK_BORDER_COLOR_FLOAT_OPAQUE_BLACK
] = { .float32
= { 0.0, 0.0, 0.0, 1.0 } },
762 [VK_BORDER_COLOR_FLOAT_OPAQUE_WHITE
] = { .float32
= { 1.0, 1.0, 1.0, 1.0 } },
763 [VK_BORDER_COLOR_INT_TRANSPARENT_BLACK
] = { .uint32
= { 0, 0, 0, 0 } },
764 [VK_BORDER_COLOR_INT_OPAQUE_BLACK
] = { .uint32
= { 0, 0, 0, 1 } },
765 [VK_BORDER_COLOR_INT_OPAQUE_WHITE
] = { .uint32
= { 1, 1, 1, 1 } },
768 device
->border_colors
= anv_state_pool_emit_data(&device
->dynamic_state_pool
,
769 sizeof(border_colors
), 64,
774 anv_device_submit_simple_batch(struct anv_device
*device
,
775 struct anv_batch
*batch
)
777 struct drm_i915_gem_execbuffer2 execbuf
;
778 struct drm_i915_gem_exec_object2 exec2_objects
[1];
780 VkResult result
= VK_SUCCESS
;
785 /* Kernel driver requires 8 byte aligned batch length */
786 size
= align_u32(batch
->next
- batch
->start
, 8);
787 result
= anv_bo_pool_alloc(&device
->batch_bo_pool
, &bo
, size
);
788 if (result
!= VK_SUCCESS
)
791 memcpy(bo
.map
, batch
->start
, size
);
792 if (!device
->info
.has_llc
)
793 anv_clflush_range(bo
.map
, size
);
795 exec2_objects
[0].handle
= bo
.gem_handle
;
796 exec2_objects
[0].relocation_count
= 0;
797 exec2_objects
[0].relocs_ptr
= 0;
798 exec2_objects
[0].alignment
= 0;
799 exec2_objects
[0].offset
= bo
.offset
;
800 exec2_objects
[0].flags
= 0;
801 exec2_objects
[0].rsvd1
= 0;
802 exec2_objects
[0].rsvd2
= 0;
804 execbuf
.buffers_ptr
= (uintptr_t) exec2_objects
;
805 execbuf
.buffer_count
= 1;
806 execbuf
.batch_start_offset
= 0;
807 execbuf
.batch_len
= size
;
808 execbuf
.cliprects_ptr
= 0;
809 execbuf
.num_cliprects
= 0;
814 I915_EXEC_HANDLE_LUT
| I915_EXEC_NO_RELOC
| I915_EXEC_RENDER
;
815 execbuf
.rsvd1
= device
->context_id
;
818 ret
= anv_gem_execbuffer(device
, &execbuf
);
820 /* We don't know the real error. */
821 result
= vk_errorf(VK_ERROR_OUT_OF_DEVICE_MEMORY
, "execbuf2 failed: %m");
826 ret
= anv_gem_wait(device
, bo
.gem_handle
, &timeout
);
828 /* We don't know the real error. */
829 result
= vk_errorf(VK_ERROR_OUT_OF_DEVICE_MEMORY
, "execbuf2 failed: %m");
834 anv_bo_pool_free(&device
->batch_bo_pool
, &bo
);
839 VkResult
anv_CreateDevice(
840 VkPhysicalDevice physicalDevice
,
841 const VkDeviceCreateInfo
* pCreateInfo
,
842 const VkAllocationCallbacks
* pAllocator
,
845 ANV_FROM_HANDLE(anv_physical_device
, physical_device
, physicalDevice
);
847 struct anv_device
*device
;
849 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO
);
851 for (uint32_t i
= 0; i
< pCreateInfo
->enabledExtensionCount
; i
++) {
853 for (uint32_t j
= 0; j
< ARRAY_SIZE(device_extensions
); j
++) {
854 if (strcmp(pCreateInfo
->ppEnabledExtensionNames
[i
],
855 device_extensions
[j
].extensionName
) == 0) {
861 return vk_error(VK_ERROR_EXTENSION_NOT_PRESENT
);
864 anv_set_dispatch_devinfo(physical_device
->info
);
866 device
= anv_alloc2(&physical_device
->instance
->alloc
, pAllocator
,
868 VK_SYSTEM_ALLOCATION_SCOPE_DEVICE
);
870 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
872 device
->_loader_data
.loaderMagic
= ICD_LOADER_MAGIC
;
873 device
->instance
= physical_device
->instance
;
874 device
->chipset_id
= physical_device
->chipset_id
;
877 device
->alloc
= *pAllocator
;
879 device
->alloc
= physical_device
->instance
->alloc
;
881 /* XXX(chadv): Can we dup() physicalDevice->fd here? */
882 device
->fd
= open(physical_device
->path
, O_RDWR
| O_CLOEXEC
);
883 if (device
->fd
== -1) {
884 result
= vk_error(VK_ERROR_INITIALIZATION_FAILED
);
888 device
->context_id
= anv_gem_create_context(device
);
889 if (device
->context_id
== -1) {
890 result
= vk_error(VK_ERROR_INITIALIZATION_FAILED
);
894 device
->info
= *physical_device
->info
;
895 device
->isl_dev
= physical_device
->isl_dev
;
897 /* On Broadwell and later, we can use batch chaining to more efficiently
898 * implement growing command buffers. Prior to Haswell, the kernel
899 * command parser gets in the way and we have to fall back to growing
902 device
->can_chain_batches
= device
->info
.gen
>= 8;
904 device
->robust_buffer_access
= pCreateInfo
->pEnabledFeatures
&&
905 pCreateInfo
->pEnabledFeatures
->robustBufferAccess
;
907 pthread_mutex_init(&device
->mutex
, NULL
);
909 anv_bo_pool_init(&device
->batch_bo_pool
, device
);
911 anv_block_pool_init(&device
->dynamic_state_block_pool
, device
, 16384);
913 anv_state_pool_init(&device
->dynamic_state_pool
,
914 &device
->dynamic_state_block_pool
);
916 anv_block_pool_init(&device
->instruction_block_pool
, device
, 128 * 1024);
917 anv_state_pool_init(&device
->instruction_state_pool
,
918 &device
->instruction_block_pool
);
920 anv_block_pool_init(&device
->surface_state_block_pool
, device
, 4096);
922 anv_state_pool_init(&device
->surface_state_pool
,
923 &device
->surface_state_block_pool
);
925 anv_bo_init_new(&device
->workaround_bo
, device
, 1024);
927 anv_scratch_pool_init(device
, &device
->scratch_pool
);
929 anv_queue_init(device
, &device
->queue
);
931 switch (device
->info
.gen
) {
933 if (!device
->info
.is_haswell
)
934 result
= gen7_init_device_state(device
);
936 result
= gen75_init_device_state(device
);
939 result
= gen8_init_device_state(device
);
942 result
= gen9_init_device_state(device
);
945 /* Shouldn't get here as we don't create physical devices for any other
947 unreachable("unhandled gen");
949 if (result
!= VK_SUCCESS
)
952 result
= anv_device_init_meta(device
);
953 if (result
!= VK_SUCCESS
)
956 anv_device_init_blorp(device
);
958 anv_device_init_border_colors(device
);
960 *pDevice
= anv_device_to_handle(device
);
967 anv_free(&device
->alloc
, device
);
972 void anv_DestroyDevice(
974 const VkAllocationCallbacks
* pAllocator
)
976 ANV_FROM_HANDLE(anv_device
, device
, _device
);
978 anv_queue_finish(&device
->queue
);
980 anv_device_finish_blorp(device
);
982 anv_device_finish_meta(device
);
985 /* We only need to free these to prevent valgrind errors. The backing
986 * BO will go away in a couple of lines so we don't actually leak.
988 anv_state_pool_free(&device
->dynamic_state_pool
, device
->border_colors
);
991 anv_gem_munmap(device
->workaround_bo
.map
, device
->workaround_bo
.size
);
992 anv_gem_close(device
, device
->workaround_bo
.gem_handle
);
994 anv_bo_pool_finish(&device
->batch_bo_pool
);
995 anv_state_pool_finish(&device
->dynamic_state_pool
);
996 anv_block_pool_finish(&device
->dynamic_state_block_pool
);
997 anv_state_pool_finish(&device
->instruction_state_pool
);
998 anv_block_pool_finish(&device
->instruction_block_pool
);
999 anv_state_pool_finish(&device
->surface_state_pool
);
1000 anv_block_pool_finish(&device
->surface_state_block_pool
);
1001 anv_scratch_pool_finish(device
, &device
->scratch_pool
);
1005 pthread_mutex_destroy(&device
->mutex
);
1007 anv_free(&device
->alloc
, device
);
1010 VkResult
anv_EnumerateInstanceExtensionProperties(
1011 const char* pLayerName
,
1012 uint32_t* pPropertyCount
,
1013 VkExtensionProperties
* pProperties
)
1015 if (pProperties
== NULL
) {
1016 *pPropertyCount
= ARRAY_SIZE(global_extensions
);
1020 assert(*pPropertyCount
>= ARRAY_SIZE(global_extensions
));
1022 *pPropertyCount
= ARRAY_SIZE(global_extensions
);
1023 memcpy(pProperties
, global_extensions
, sizeof(global_extensions
));
1028 VkResult
anv_EnumerateDeviceExtensionProperties(
1029 VkPhysicalDevice physicalDevice
,
1030 const char* pLayerName
,
1031 uint32_t* pPropertyCount
,
1032 VkExtensionProperties
* pProperties
)
1034 if (pProperties
== NULL
) {
1035 *pPropertyCount
= ARRAY_SIZE(device_extensions
);
1039 assert(*pPropertyCount
>= ARRAY_SIZE(device_extensions
));
1041 *pPropertyCount
= ARRAY_SIZE(device_extensions
);
1042 memcpy(pProperties
, device_extensions
, sizeof(device_extensions
));
1047 VkResult
anv_EnumerateInstanceLayerProperties(
1048 uint32_t* pPropertyCount
,
1049 VkLayerProperties
* pProperties
)
1051 if (pProperties
== NULL
) {
1052 *pPropertyCount
= 0;
1056 /* None supported at this time */
1057 return vk_error(VK_ERROR_LAYER_NOT_PRESENT
);
1060 VkResult
anv_EnumerateDeviceLayerProperties(
1061 VkPhysicalDevice physicalDevice
,
1062 uint32_t* pPropertyCount
,
1063 VkLayerProperties
* pProperties
)
1065 if (pProperties
== NULL
) {
1066 *pPropertyCount
= 0;
1070 /* None supported at this time */
1071 return vk_error(VK_ERROR_LAYER_NOT_PRESENT
);
1074 void anv_GetDeviceQueue(
1076 uint32_t queueNodeIndex
,
1077 uint32_t queueIndex
,
1080 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1082 assert(queueIndex
== 0);
1084 *pQueue
= anv_queue_to_handle(&device
->queue
);
1087 VkResult
anv_QueueSubmit(
1089 uint32_t submitCount
,
1090 const VkSubmitInfo
* pSubmits
,
1093 ANV_FROM_HANDLE(anv_queue
, queue
, _queue
);
1094 ANV_FROM_HANDLE(anv_fence
, fence
, _fence
);
1095 struct anv_device
*device
= queue
->device
;
1098 for (uint32_t i
= 0; i
< submitCount
; i
++) {
1099 for (uint32_t j
= 0; j
< pSubmits
[i
].commandBufferCount
; j
++) {
1100 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
,
1101 pSubmits
[i
].pCommandBuffers
[j
]);
1102 assert(cmd_buffer
->level
== VK_COMMAND_BUFFER_LEVEL_PRIMARY
);
1104 ret
= anv_gem_execbuffer(device
, &cmd_buffer
->execbuf2
.execbuf
);
1106 /* We don't know the real error. */
1107 return vk_errorf(VK_ERROR_OUT_OF_DEVICE_MEMORY
,
1108 "execbuf2 failed: %m");
1111 for (uint32_t k
= 0; k
< cmd_buffer
->execbuf2
.bo_count
; k
++)
1112 cmd_buffer
->execbuf2
.bos
[k
]->offset
= cmd_buffer
->execbuf2
.objects
[k
].offset
;
1117 ret
= anv_gem_execbuffer(device
, &fence
->execbuf
);
1119 /* We don't know the real error. */
1120 return vk_errorf(VK_ERROR_OUT_OF_DEVICE_MEMORY
,
1121 "execbuf2 failed: %m");
1128 VkResult
anv_QueueWaitIdle(
1131 ANV_FROM_HANDLE(anv_queue
, queue
, _queue
);
1133 return ANV_CALL(DeviceWaitIdle
)(anv_device_to_handle(queue
->device
));
1136 VkResult
anv_DeviceWaitIdle(
1139 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1140 struct anv_batch batch
;
1143 batch
.start
= batch
.next
= cmds
;
1144 batch
.end
= (void *) cmds
+ sizeof(cmds
);
1146 anv_batch_emit(&batch
, GEN7_MI_BATCH_BUFFER_END
, bbe
);
1147 anv_batch_emit(&batch
, GEN7_MI_NOOP
, noop
);
1149 return anv_device_submit_simple_batch(device
, &batch
);
1153 anv_bo_init_new(struct anv_bo
*bo
, struct anv_device
*device
, uint64_t size
)
1155 bo
->gem_handle
= anv_gem_create(device
, size
);
1156 if (!bo
->gem_handle
)
1157 return vk_error(VK_ERROR_OUT_OF_DEVICE_MEMORY
);
1163 bo
->is_winsys_bo
= false;
1168 VkResult
anv_AllocateMemory(
1170 const VkMemoryAllocateInfo
* pAllocateInfo
,
1171 const VkAllocationCallbacks
* pAllocator
,
1172 VkDeviceMemory
* pMem
)
1174 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1175 struct anv_device_memory
*mem
;
1178 assert(pAllocateInfo
->sType
== VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO
);
1180 if (pAllocateInfo
->allocationSize
== 0) {
1181 /* Apparently, this is allowed */
1182 *pMem
= VK_NULL_HANDLE
;
1186 /* We support exactly one memory heap. */
1187 assert(pAllocateInfo
->memoryTypeIndex
== 0 ||
1188 (!device
->info
.has_llc
&& pAllocateInfo
->memoryTypeIndex
< 2));
1190 /* FINISHME: Fail if allocation request exceeds heap size. */
1192 mem
= anv_alloc2(&device
->alloc
, pAllocator
, sizeof(*mem
), 8,
1193 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
1195 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
1197 /* The kernel is going to give us whole pages anyway */
1198 uint64_t alloc_size
= align_u64(pAllocateInfo
->allocationSize
, 4096);
1200 result
= anv_bo_init_new(&mem
->bo
, device
, alloc_size
);
1201 if (result
!= VK_SUCCESS
)
1204 mem
->type_index
= pAllocateInfo
->memoryTypeIndex
;
1206 *pMem
= anv_device_memory_to_handle(mem
);
1211 anv_free2(&device
->alloc
, pAllocator
, mem
);
1216 void anv_FreeMemory(
1218 VkDeviceMemory _mem
,
1219 const VkAllocationCallbacks
* pAllocator
)
1221 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1222 ANV_FROM_HANDLE(anv_device_memory
, mem
, _mem
);
1228 anv_gem_munmap(mem
->bo
.map
, mem
->bo
.size
);
1230 if (mem
->bo
.gem_handle
!= 0)
1231 anv_gem_close(device
, mem
->bo
.gem_handle
);
1233 anv_free2(&device
->alloc
, pAllocator
, mem
);
1236 VkResult
anv_MapMemory(
1238 VkDeviceMemory _memory
,
1239 VkDeviceSize offset
,
1241 VkMemoryMapFlags flags
,
1244 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1245 ANV_FROM_HANDLE(anv_device_memory
, mem
, _memory
);
1252 if (size
== VK_WHOLE_SIZE
)
1253 size
= mem
->bo
.size
- offset
;
1255 /* FIXME: Is this supposed to be thread safe? Since vkUnmapMemory() only
1256 * takes a VkDeviceMemory pointer, it seems like only one map of the memory
1257 * at a time is valid. We could just mmap up front and return an offset
1258 * pointer here, but that may exhaust virtual memory on 32 bit
1261 uint32_t gem_flags
= 0;
1262 if (!device
->info
.has_llc
&& mem
->type_index
== 0)
1263 gem_flags
|= I915_MMAP_WC
;
1265 /* GEM will fail to map if the offset isn't 4k-aligned. Round down. */
1266 uint64_t map_offset
= offset
& ~4095ull;
1267 assert(offset
>= map_offset
);
1268 uint64_t map_size
= (offset
+ size
) - map_offset
;
1270 /* Let's map whole pages */
1271 map_size
= align_u64(map_size
, 4096);
1273 mem
->map
= anv_gem_mmap(device
, mem
->bo
.gem_handle
,
1274 map_offset
, map_size
, gem_flags
);
1275 mem
->map_size
= map_size
;
1277 *ppData
= mem
->map
+ (offset
- map_offset
);
1282 void anv_UnmapMemory(
1284 VkDeviceMemory _memory
)
1286 ANV_FROM_HANDLE(anv_device_memory
, mem
, _memory
);
1291 anv_gem_munmap(mem
->map
, mem
->map_size
);
1295 clflush_mapped_ranges(struct anv_device
*device
,
1297 const VkMappedMemoryRange
*ranges
)
1299 for (uint32_t i
= 0; i
< count
; i
++) {
1300 ANV_FROM_HANDLE(anv_device_memory
, mem
, ranges
[i
].memory
);
1301 void *p
= mem
->map
+ (ranges
[i
].offset
& ~CACHELINE_MASK
);
1304 if (ranges
[i
].offset
+ ranges
[i
].size
> mem
->map_size
)
1305 end
= mem
->map
+ mem
->map_size
;
1307 end
= mem
->map
+ ranges
[i
].offset
+ ranges
[i
].size
;
1310 __builtin_ia32_clflush(p
);
1311 p
+= CACHELINE_SIZE
;
1316 VkResult
anv_FlushMappedMemoryRanges(
1318 uint32_t memoryRangeCount
,
1319 const VkMappedMemoryRange
* pMemoryRanges
)
1321 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1323 if (device
->info
.has_llc
)
1326 /* Make sure the writes we're flushing have landed. */
1327 __builtin_ia32_mfence();
1329 clflush_mapped_ranges(device
, memoryRangeCount
, pMemoryRanges
);
1334 VkResult
anv_InvalidateMappedMemoryRanges(
1336 uint32_t memoryRangeCount
,
1337 const VkMappedMemoryRange
* pMemoryRanges
)
1339 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1341 if (device
->info
.has_llc
)
1344 clflush_mapped_ranges(device
, memoryRangeCount
, pMemoryRanges
);
1346 /* Make sure no reads get moved up above the invalidate. */
1347 __builtin_ia32_mfence();
1352 void anv_GetBufferMemoryRequirements(
1355 VkMemoryRequirements
* pMemoryRequirements
)
1357 ANV_FROM_HANDLE(anv_buffer
, buffer
, _buffer
);
1359 /* The Vulkan spec (git aaed022) says:
1361 * memoryTypeBits is a bitfield and contains one bit set for every
1362 * supported memory type for the resource. The bit `1<<i` is set if and
1363 * only if the memory type `i` in the VkPhysicalDeviceMemoryProperties
1364 * structure for the physical device is supported.
1366 * We support exactly one memory type.
1368 pMemoryRequirements
->memoryTypeBits
= 1;
1370 pMemoryRequirements
->size
= buffer
->size
;
1371 pMemoryRequirements
->alignment
= 16;
1374 void anv_GetImageMemoryRequirements(
1377 VkMemoryRequirements
* pMemoryRequirements
)
1379 ANV_FROM_HANDLE(anv_image
, image
, _image
);
1381 /* The Vulkan spec (git aaed022) says:
1383 * memoryTypeBits is a bitfield and contains one bit set for every
1384 * supported memory type for the resource. The bit `1<<i` is set if and
1385 * only if the memory type `i` in the VkPhysicalDeviceMemoryProperties
1386 * structure for the physical device is supported.
1388 * We support exactly one memory type.
1390 pMemoryRequirements
->memoryTypeBits
= 1;
1392 pMemoryRequirements
->size
= image
->size
;
1393 pMemoryRequirements
->alignment
= image
->alignment
;
1396 void anv_GetImageSparseMemoryRequirements(
1399 uint32_t* pSparseMemoryRequirementCount
,
1400 VkSparseImageMemoryRequirements
* pSparseMemoryRequirements
)
1405 void anv_GetDeviceMemoryCommitment(
1407 VkDeviceMemory memory
,
1408 VkDeviceSize
* pCommittedMemoryInBytes
)
1410 *pCommittedMemoryInBytes
= 0;
1413 VkResult
anv_BindBufferMemory(
1416 VkDeviceMemory _memory
,
1417 VkDeviceSize memoryOffset
)
1419 ANV_FROM_HANDLE(anv_device_memory
, mem
, _memory
);
1420 ANV_FROM_HANDLE(anv_buffer
, buffer
, _buffer
);
1423 buffer
->bo
= &mem
->bo
;
1424 buffer
->offset
= memoryOffset
;
1433 VkResult
anv_BindImageMemory(
1436 VkDeviceMemory _memory
,
1437 VkDeviceSize memoryOffset
)
1439 ANV_FROM_HANDLE(anv_device_memory
, mem
, _memory
);
1440 ANV_FROM_HANDLE(anv_image
, image
, _image
);
1443 image
->bo
= &mem
->bo
;
1444 image
->offset
= memoryOffset
;
1453 VkResult
anv_QueueBindSparse(
1455 uint32_t bindInfoCount
,
1456 const VkBindSparseInfo
* pBindInfo
,
1459 stub_return(VK_ERROR_INCOMPATIBLE_DRIVER
);
1462 VkResult
anv_CreateFence(
1464 const VkFenceCreateInfo
* pCreateInfo
,
1465 const VkAllocationCallbacks
* pAllocator
,
1468 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1469 struct anv_bo fence_bo
;
1470 struct anv_fence
*fence
;
1471 struct anv_batch batch
;
1474 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_FENCE_CREATE_INFO
);
1476 result
= anv_bo_pool_alloc(&device
->batch_bo_pool
, &fence_bo
, 4096);
1477 if (result
!= VK_SUCCESS
)
1480 /* Fences are small. Just store the CPU data structure in the BO. */
1481 fence
= fence_bo
.map
;
1482 fence
->bo
= fence_bo
;
1484 /* Place the batch after the CPU data but on its own cache line. */
1485 const uint32_t batch_offset
= align_u32(sizeof(*fence
), CACHELINE_SIZE
);
1486 batch
.next
= batch
.start
= fence
->bo
.map
+ batch_offset
;
1487 batch
.end
= fence
->bo
.map
+ fence
->bo
.size
;
1488 anv_batch_emit(&batch
, GEN7_MI_BATCH_BUFFER_END
, bbe
);
1489 anv_batch_emit(&batch
, GEN7_MI_NOOP
, noop
);
1491 if (!device
->info
.has_llc
) {
1492 assert(((uintptr_t) batch
.start
& CACHELINE_MASK
) == 0);
1493 assert(batch
.next
- batch
.start
<= CACHELINE_SIZE
);
1494 __builtin_ia32_mfence();
1495 __builtin_ia32_clflush(batch
.start
);
1498 fence
->exec2_objects
[0].handle
= fence
->bo
.gem_handle
;
1499 fence
->exec2_objects
[0].relocation_count
= 0;
1500 fence
->exec2_objects
[0].relocs_ptr
= 0;
1501 fence
->exec2_objects
[0].alignment
= 0;
1502 fence
->exec2_objects
[0].offset
= fence
->bo
.offset
;
1503 fence
->exec2_objects
[0].flags
= 0;
1504 fence
->exec2_objects
[0].rsvd1
= 0;
1505 fence
->exec2_objects
[0].rsvd2
= 0;
1507 fence
->execbuf
.buffers_ptr
= (uintptr_t) fence
->exec2_objects
;
1508 fence
->execbuf
.buffer_count
= 1;
1509 fence
->execbuf
.batch_start_offset
= batch
.start
- fence
->bo
.map
;
1510 fence
->execbuf
.batch_len
= batch
.next
- batch
.start
;
1511 fence
->execbuf
.cliprects_ptr
= 0;
1512 fence
->execbuf
.num_cliprects
= 0;
1513 fence
->execbuf
.DR1
= 0;
1514 fence
->execbuf
.DR4
= 0;
1516 fence
->execbuf
.flags
=
1517 I915_EXEC_HANDLE_LUT
| I915_EXEC_NO_RELOC
| I915_EXEC_RENDER
;
1518 fence
->execbuf
.rsvd1
= device
->context_id
;
1519 fence
->execbuf
.rsvd2
= 0;
1521 fence
->ready
= false;
1523 *pFence
= anv_fence_to_handle(fence
);
1528 void anv_DestroyFence(
1531 const VkAllocationCallbacks
* pAllocator
)
1533 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1534 ANV_FROM_HANDLE(anv_fence
, fence
, _fence
);
1536 assert(fence
->bo
.map
== fence
);
1537 anv_bo_pool_free(&device
->batch_bo_pool
, &fence
->bo
);
1540 VkResult
anv_ResetFences(
1542 uint32_t fenceCount
,
1543 const VkFence
* pFences
)
1545 for (uint32_t i
= 0; i
< fenceCount
; i
++) {
1546 ANV_FROM_HANDLE(anv_fence
, fence
, pFences
[i
]);
1547 fence
->ready
= false;
1553 VkResult
anv_GetFenceStatus(
1557 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1558 ANV_FROM_HANDLE(anv_fence
, fence
, _fence
);
1565 ret
= anv_gem_wait(device
, fence
->bo
.gem_handle
, &t
);
1567 fence
->ready
= true;
1571 return VK_NOT_READY
;
1574 VkResult
anv_WaitForFences(
1576 uint32_t fenceCount
,
1577 const VkFence
* pFences
,
1581 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1583 /* DRM_IOCTL_I915_GEM_WAIT uses a signed 64 bit timeout and is supposed
1584 * to block indefinitely timeouts <= 0. Unfortunately, this was broken
1585 * for a couple of kernel releases. Since there's no way to know
1586 * whether or not the kernel we're using is one of the broken ones, the
1587 * best we can do is to clamp the timeout to INT64_MAX. This limits the
1588 * maximum timeout from 584 years to 292 years - likely not a big deal.
1590 if (timeout
> INT64_MAX
)
1591 timeout
= INT64_MAX
;
1593 int64_t t
= timeout
;
1595 /* FIXME: handle !waitAll */
1597 for (uint32_t i
= 0; i
< fenceCount
; i
++) {
1598 ANV_FROM_HANDLE(anv_fence
, fence
, pFences
[i
]);
1599 int ret
= anv_gem_wait(device
, fence
->bo
.gem_handle
, &t
);
1600 if (ret
== -1 && errno
== ETIME
) {
1602 } else if (ret
== -1) {
1603 /* We don't know the real error. */
1604 return vk_errorf(VK_ERROR_OUT_OF_DEVICE_MEMORY
,
1605 "gem wait failed: %m");
1612 // Queue semaphore functions
1614 VkResult
anv_CreateSemaphore(
1616 const VkSemaphoreCreateInfo
* pCreateInfo
,
1617 const VkAllocationCallbacks
* pAllocator
,
1618 VkSemaphore
* pSemaphore
)
1620 /* The DRM execbuffer ioctl always execute in-oder, even between different
1621 * rings. As such, there's nothing to do for the user space semaphore.
1624 *pSemaphore
= (VkSemaphore
)1;
1629 void anv_DestroySemaphore(
1631 VkSemaphore semaphore
,
1632 const VkAllocationCallbacks
* pAllocator
)
1638 VkResult
anv_CreateEvent(
1640 const VkEventCreateInfo
* pCreateInfo
,
1641 const VkAllocationCallbacks
* pAllocator
,
1644 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1645 struct anv_state state
;
1646 struct anv_event
*event
;
1648 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_EVENT_CREATE_INFO
);
1650 state
= anv_state_pool_alloc(&device
->dynamic_state_pool
,
1653 event
->state
= state
;
1654 event
->semaphore
= VK_EVENT_RESET
;
1656 if (!device
->info
.has_llc
) {
1657 /* Make sure the writes we're flushing have landed. */
1658 __builtin_ia32_mfence();
1659 __builtin_ia32_clflush(event
);
1662 *pEvent
= anv_event_to_handle(event
);
1667 void anv_DestroyEvent(
1670 const VkAllocationCallbacks
* pAllocator
)
1672 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1673 ANV_FROM_HANDLE(anv_event
, event
, _event
);
1675 anv_state_pool_free(&device
->dynamic_state_pool
, event
->state
);
1678 VkResult
anv_GetEventStatus(
1682 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1683 ANV_FROM_HANDLE(anv_event
, event
, _event
);
1685 if (!device
->info
.has_llc
) {
1686 /* Invalidate read cache before reading event written by GPU. */
1687 __builtin_ia32_clflush(event
);
1688 __builtin_ia32_mfence();
1692 return event
->semaphore
;
1695 VkResult
anv_SetEvent(
1699 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1700 ANV_FROM_HANDLE(anv_event
, event
, _event
);
1702 event
->semaphore
= VK_EVENT_SET
;
1704 if (!device
->info
.has_llc
) {
1705 /* Make sure the writes we're flushing have landed. */
1706 __builtin_ia32_mfence();
1707 __builtin_ia32_clflush(event
);
1713 VkResult
anv_ResetEvent(
1717 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1718 ANV_FROM_HANDLE(anv_event
, event
, _event
);
1720 event
->semaphore
= VK_EVENT_RESET
;
1722 if (!device
->info
.has_llc
) {
1723 /* Make sure the writes we're flushing have landed. */
1724 __builtin_ia32_mfence();
1725 __builtin_ia32_clflush(event
);
1733 VkResult
anv_CreateBuffer(
1735 const VkBufferCreateInfo
* pCreateInfo
,
1736 const VkAllocationCallbacks
* pAllocator
,
1739 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1740 struct anv_buffer
*buffer
;
1742 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO
);
1744 buffer
= anv_alloc2(&device
->alloc
, pAllocator
, sizeof(*buffer
), 8,
1745 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
1747 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
1749 buffer
->size
= pCreateInfo
->size
;
1750 buffer
->usage
= pCreateInfo
->usage
;
1754 *pBuffer
= anv_buffer_to_handle(buffer
);
1759 void anv_DestroyBuffer(
1762 const VkAllocationCallbacks
* pAllocator
)
1764 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1765 ANV_FROM_HANDLE(anv_buffer
, buffer
, _buffer
);
1767 anv_free2(&device
->alloc
, pAllocator
, buffer
);
1771 anv_fill_buffer_surface_state(struct anv_device
*device
, struct anv_state state
,
1772 enum isl_format format
,
1773 uint32_t offset
, uint32_t range
, uint32_t stride
)
1775 isl_buffer_fill_state(&device
->isl_dev
, state
.map
,
1777 .mocs
= device
->default_mocs
,
1782 if (!device
->info
.has_llc
)
1783 anv_state_clflush(state
);
1786 void anv_DestroySampler(
1789 const VkAllocationCallbacks
* pAllocator
)
1791 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1792 ANV_FROM_HANDLE(anv_sampler
, sampler
, _sampler
);
1794 anv_free2(&device
->alloc
, pAllocator
, sampler
);
1797 VkResult
anv_CreateFramebuffer(
1799 const VkFramebufferCreateInfo
* pCreateInfo
,
1800 const VkAllocationCallbacks
* pAllocator
,
1801 VkFramebuffer
* pFramebuffer
)
1803 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1804 struct anv_framebuffer
*framebuffer
;
1806 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO
);
1808 size_t size
= sizeof(*framebuffer
) +
1809 sizeof(struct anv_image_view
*) * pCreateInfo
->attachmentCount
;
1810 framebuffer
= anv_alloc2(&device
->alloc
, pAllocator
, size
, 8,
1811 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
1812 if (framebuffer
== NULL
)
1813 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
1815 framebuffer
->attachment_count
= pCreateInfo
->attachmentCount
;
1816 for (uint32_t i
= 0; i
< pCreateInfo
->attachmentCount
; i
++) {
1817 VkImageView _iview
= pCreateInfo
->pAttachments
[i
];
1818 framebuffer
->attachments
[i
] = anv_image_view_from_handle(_iview
);
1821 framebuffer
->width
= pCreateInfo
->width
;
1822 framebuffer
->height
= pCreateInfo
->height
;
1823 framebuffer
->layers
= pCreateInfo
->layers
;
1825 *pFramebuffer
= anv_framebuffer_to_handle(framebuffer
);
1830 void anv_DestroyFramebuffer(
1833 const VkAllocationCallbacks
* pAllocator
)
1835 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1836 ANV_FROM_HANDLE(anv_framebuffer
, fb
, _fb
);
1838 anv_free2(&device
->alloc
, pAllocator
, fb
);