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 "mesa/main/git_sha1.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_errorf(VK_ERROR_INITIALIZATION_FAILED
,
66 "failed to open %s: %m", path
);
68 device
->_loader_data
.loaderMagic
= ICD_LOADER_MAGIC
;
69 device
->instance
= instance
;
72 device
->chipset_id
= anv_gem_get_param(fd
, I915_PARAM_CHIPSET_ID
);
73 if (!device
->chipset_id
) {
74 result
= vk_errorf(VK_ERROR_INITIALIZATION_FAILED
,
75 "failed to get chipset id: %m");
79 device
->name
= brw_get_device_name(device
->chipset_id
);
80 device
->info
= brw_get_device_info(device
->chipset_id
);
82 result
= vk_errorf(VK_ERROR_INITIALIZATION_FAILED
,
83 "failed to get device info");
87 if (device
->info
->is_haswell
) {
88 fprintf(stderr
, "WARNING: Haswell Vulkan support is incomplete\n");
89 } else if (device
->info
->gen
== 7 && !device
->info
->is_baytrail
) {
90 fprintf(stderr
, "WARNING: Ivy Bridge Vulkan support is incomplete\n");
91 } else if (device
->info
->gen
== 7 && device
->info
->is_baytrail
) {
92 fprintf(stderr
, "WARNING: Bay Trail Vulkan support is incomplete\n");
93 } else if (device
->info
->gen
>= 8) {
94 /* Broadwell, Cherryview, Skylake, Broxton, Kabylake is as fully
95 * supported as anything */
97 result
= vk_errorf(VK_ERROR_INCOMPATIBLE_DRIVER
,
98 "Vulkan not yet supported on %s", device
->name
);
102 if (anv_gem_get_aperture(fd
, &device
->aperture_size
) == -1) {
103 result
= vk_errorf(VK_ERROR_INITIALIZATION_FAILED
,
104 "failed to get aperture size: %m");
108 if (!anv_gem_get_param(fd
, I915_PARAM_HAS_WAIT_TIMEOUT
)) {
109 result
= vk_errorf(VK_ERROR_INITIALIZATION_FAILED
,
110 "kernel missing gem wait");
114 if (!anv_gem_get_param(fd
, I915_PARAM_HAS_EXECBUF2
)) {
115 result
= vk_errorf(VK_ERROR_INITIALIZATION_FAILED
,
116 "kernel missing execbuf2");
120 if (!device
->info
->has_llc
&&
121 anv_gem_get_param(fd
, I915_PARAM_MMAP_VERSION
) < 1) {
122 result
= vk_errorf(VK_ERROR_INITIALIZATION_FAILED
,
123 "kernel missing wc mmap");
127 bool swizzled
= anv_gem_get_bit6_swizzle(fd
, I915_TILING_X
);
131 brw_process_intel_debug_variable();
133 device
->compiler
= brw_compiler_create(NULL
, device
->info
);
134 if (device
->compiler
== NULL
) {
135 result
= vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
138 device
->compiler
->shader_debug_log
= compiler_debug_log
;
139 device
->compiler
->shader_perf_log
= compiler_perf_log
;
141 /* XXX: Actually detect bit6 swizzling */
142 isl_device_init(&device
->isl_dev
, device
->info
, swizzled
);
152 anv_physical_device_finish(struct anv_physical_device
*device
)
154 ralloc_free(device
->compiler
);
157 static const VkExtensionProperties global_extensions
[] = {
159 .extensionName
= VK_KHR_SURFACE_EXTENSION_NAME
,
163 .extensionName
= VK_KHR_XCB_SURFACE_EXTENSION_NAME
,
166 #ifdef HAVE_WAYLAND_PLATFORM
168 .extensionName
= VK_KHR_WAYLAND_SURFACE_EXTENSION_NAME
,
174 static const VkExtensionProperties device_extensions
[] = {
176 .extensionName
= VK_KHR_SWAPCHAIN_EXTENSION_NAME
,
182 default_alloc_func(void *pUserData
, size_t size
, size_t align
,
183 VkSystemAllocationScope allocationScope
)
189 default_realloc_func(void *pUserData
, void *pOriginal
, size_t size
,
190 size_t align
, VkSystemAllocationScope allocationScope
)
192 return realloc(pOriginal
, size
);
196 default_free_func(void *pUserData
, void *pMemory
)
201 static const VkAllocationCallbacks default_alloc
= {
203 .pfnAllocation
= default_alloc_func
,
204 .pfnReallocation
= default_realloc_func
,
205 .pfnFree
= default_free_func
,
208 VkResult
anv_CreateInstance(
209 const VkInstanceCreateInfo
* pCreateInfo
,
210 const VkAllocationCallbacks
* pAllocator
,
211 VkInstance
* pInstance
)
213 struct anv_instance
*instance
;
215 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO
);
217 uint32_t client_version
;
218 if (pCreateInfo
->pApplicationInfo
&&
219 pCreateInfo
->pApplicationInfo
->apiVersion
!= 0) {
220 client_version
= pCreateInfo
->pApplicationInfo
->apiVersion
;
222 client_version
= VK_MAKE_VERSION(1, 0, 0);
225 if (VK_MAKE_VERSION(1, 0, 0) > client_version
||
226 client_version
> VK_MAKE_VERSION(1, 0, 3)) {
227 return vk_errorf(VK_ERROR_INCOMPATIBLE_DRIVER
,
228 "Client requested version %d.%d.%d",
229 VK_VERSION_MAJOR(client_version
),
230 VK_VERSION_MINOR(client_version
),
231 VK_VERSION_PATCH(client_version
));
234 for (uint32_t i
= 0; i
< pCreateInfo
->enabledExtensionCount
; i
++) {
236 for (uint32_t j
= 0; j
< ARRAY_SIZE(global_extensions
); j
++) {
237 if (strcmp(pCreateInfo
->ppEnabledExtensionNames
[i
],
238 global_extensions
[j
].extensionName
) == 0) {
244 return vk_error(VK_ERROR_EXTENSION_NOT_PRESENT
);
247 instance
= anv_alloc2(&default_alloc
, pAllocator
, sizeof(*instance
), 8,
248 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE
);
250 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
252 instance
->_loader_data
.loaderMagic
= ICD_LOADER_MAGIC
;
255 instance
->alloc
= *pAllocator
;
257 instance
->alloc
= default_alloc
;
259 instance
->apiVersion
= client_version
;
260 instance
->physicalDeviceCount
= -1;
262 memset(instance
->wsi
, 0, sizeof(instance
->wsi
));
266 VG(VALGRIND_CREATE_MEMPOOL(instance
, 0, false));
268 anv_init_wsi(instance
);
270 *pInstance
= anv_instance_to_handle(instance
);
275 void anv_DestroyInstance(
276 VkInstance _instance
,
277 const VkAllocationCallbacks
* pAllocator
)
279 ANV_FROM_HANDLE(anv_instance
, instance
, _instance
);
281 if (instance
->physicalDeviceCount
> 0) {
282 /* We support at most one physical device. */
283 assert(instance
->physicalDeviceCount
== 1);
284 anv_physical_device_finish(&instance
->physicalDevice
);
287 anv_finish_wsi(instance
);
289 VG(VALGRIND_DESTROY_MEMPOOL(instance
));
293 anv_free(&instance
->alloc
, instance
);
296 VkResult
anv_EnumeratePhysicalDevices(
297 VkInstance _instance
,
298 uint32_t* pPhysicalDeviceCount
,
299 VkPhysicalDevice
* pPhysicalDevices
)
301 ANV_FROM_HANDLE(anv_instance
, instance
, _instance
);
304 if (instance
->physicalDeviceCount
< 0) {
305 result
= anv_physical_device_init(&instance
->physicalDevice
,
306 instance
, "/dev/dri/renderD128");
307 if (result
== VK_ERROR_INCOMPATIBLE_DRIVER
) {
308 instance
->physicalDeviceCount
= 0;
309 } else if (result
== VK_SUCCESS
) {
310 instance
->physicalDeviceCount
= 1;
316 /* pPhysicalDeviceCount is an out parameter if pPhysicalDevices is NULL;
317 * otherwise it's an inout parameter.
319 * The Vulkan spec (git aaed022) says:
321 * pPhysicalDeviceCount is a pointer to an unsigned integer variable
322 * that is initialized with the number of devices the application is
323 * prepared to receive handles to. pname:pPhysicalDevices is pointer to
324 * an array of at least this many VkPhysicalDevice handles [...].
326 * Upon success, if pPhysicalDevices is NULL, vkEnumeratePhysicalDevices
327 * overwrites the contents of the variable pointed to by
328 * pPhysicalDeviceCount with the number of physical devices in in the
329 * instance; otherwise, vkEnumeratePhysicalDevices overwrites
330 * pPhysicalDeviceCount with the number of physical handles written to
333 if (!pPhysicalDevices
) {
334 *pPhysicalDeviceCount
= instance
->physicalDeviceCount
;
335 } else if (*pPhysicalDeviceCount
>= 1) {
336 pPhysicalDevices
[0] = anv_physical_device_to_handle(&instance
->physicalDevice
);
337 *pPhysicalDeviceCount
= 1;
339 *pPhysicalDeviceCount
= 0;
345 void anv_GetPhysicalDeviceFeatures(
346 VkPhysicalDevice physicalDevice
,
347 VkPhysicalDeviceFeatures
* pFeatures
)
349 ANV_FROM_HANDLE(anv_physical_device
, pdevice
, physicalDevice
);
351 *pFeatures
= (VkPhysicalDeviceFeatures
) {
352 .robustBufferAccess
= true,
353 .fullDrawIndexUint32
= true,
354 .imageCubeArray
= false,
355 .independentBlend
= pdevice
->info
->gen
>= 8,
356 .geometryShader
= true,
357 .tessellationShader
= false,
358 .sampleRateShading
= false,
359 .dualSrcBlend
= true,
361 .multiDrawIndirect
= false,
362 .drawIndirectFirstInstance
= false,
364 .depthBiasClamp
= false,
365 .fillModeNonSolid
= true,
366 .depthBounds
= false,
370 .multiViewport
= true,
371 .samplerAnisotropy
= false, /* FINISHME */
372 .textureCompressionETC2
= true,
373 .textureCompressionASTC_LDR
= true,
374 .textureCompressionBC
= true,
375 .occlusionQueryPrecise
= true,
376 .pipelineStatisticsQuery
= false,
377 .vertexPipelineStoresAndAtomics
= pdevice
->info
->gen
>= 8,
378 .fragmentStoresAndAtomics
= true,
379 .shaderTessellationAndGeometryPointSize
= true,
380 .shaderImageGatherExtended
= true,
381 .shaderStorageImageExtendedFormats
= false,
382 .shaderStorageImageMultisample
= false,
383 .shaderUniformBufferArrayDynamicIndexing
= true,
384 .shaderSampledImageArrayDynamicIndexing
= true,
385 .shaderStorageBufferArrayDynamicIndexing
= true,
386 .shaderStorageImageArrayDynamicIndexing
= true,
387 .shaderStorageImageReadWithoutFormat
= false,
388 .shaderStorageImageWriteWithoutFormat
= true,
389 .shaderClipDistance
= false,
390 .shaderCullDistance
= false,
391 .shaderFloat64
= false,
392 .shaderInt64
= false,
393 .shaderInt16
= false,
395 .variableMultisampleRate
= false,
396 .inheritedQueries
= false,
401 anv_device_get_cache_uuid(void *uuid
)
403 memset(uuid
, 0, VK_UUID_SIZE
);
404 snprintf(uuid
, VK_UUID_SIZE
, "anv-%s", MESA_GIT_SHA1
+ 4);
407 void anv_GetPhysicalDeviceProperties(
408 VkPhysicalDevice physicalDevice
,
409 VkPhysicalDeviceProperties
* pProperties
)
411 ANV_FROM_HANDLE(anv_physical_device
, pdevice
, physicalDevice
);
412 const struct brw_device_info
*devinfo
= pdevice
->info
;
414 anv_finishme("Get correct values for VkPhysicalDeviceLimits");
416 const float time_stamp_base
= devinfo
->gen
>= 9 ? 83.333 : 80.0;
418 VkSampleCountFlags sample_counts
=
419 isl_device_get_sample_counts(&pdevice
->isl_dev
);
421 VkPhysicalDeviceLimits limits
= {
422 .maxImageDimension1D
= (1 << 14),
423 .maxImageDimension2D
= (1 << 14),
424 .maxImageDimension3D
= (1 << 11),
425 .maxImageDimensionCube
= (1 << 14),
426 .maxImageArrayLayers
= (1 << 11),
427 .maxTexelBufferElements
= 128 * 1024 * 1024,
428 .maxUniformBufferRange
= UINT32_MAX
,
429 .maxStorageBufferRange
= UINT32_MAX
,
430 .maxPushConstantsSize
= MAX_PUSH_CONSTANTS_SIZE
,
431 .maxMemoryAllocationCount
= UINT32_MAX
,
432 .maxSamplerAllocationCount
= 64 * 1024,
433 .bufferImageGranularity
= 64, /* A cache line */
434 .sparseAddressSpaceSize
= 0,
435 .maxBoundDescriptorSets
= MAX_SETS
,
436 .maxPerStageDescriptorSamplers
= 64,
437 .maxPerStageDescriptorUniformBuffers
= 64,
438 .maxPerStageDescriptorStorageBuffers
= 64,
439 .maxPerStageDescriptorSampledImages
= 64,
440 .maxPerStageDescriptorStorageImages
= 64,
441 .maxPerStageDescriptorInputAttachments
= 64,
442 .maxPerStageResources
= 128,
443 .maxDescriptorSetSamplers
= 256,
444 .maxDescriptorSetUniformBuffers
= 256,
445 .maxDescriptorSetUniformBuffersDynamic
= 256,
446 .maxDescriptorSetStorageBuffers
= 256,
447 .maxDescriptorSetStorageBuffersDynamic
= 256,
448 .maxDescriptorSetSampledImages
= 256,
449 .maxDescriptorSetStorageImages
= 256,
450 .maxDescriptorSetInputAttachments
= 256,
451 .maxVertexInputAttributes
= 32,
452 .maxVertexInputBindings
= 32,
453 .maxVertexInputAttributeOffset
= 2047,
454 .maxVertexInputBindingStride
= 2048,
455 .maxVertexOutputComponents
= 128,
456 .maxTessellationGenerationLevel
= 0,
457 .maxTessellationPatchSize
= 0,
458 .maxTessellationControlPerVertexInputComponents
= 0,
459 .maxTessellationControlPerVertexOutputComponents
= 0,
460 .maxTessellationControlPerPatchOutputComponents
= 0,
461 .maxTessellationControlTotalOutputComponents
= 0,
462 .maxTessellationEvaluationInputComponents
= 0,
463 .maxTessellationEvaluationOutputComponents
= 0,
464 .maxGeometryShaderInvocations
= 32,
465 .maxGeometryInputComponents
= 64,
466 .maxGeometryOutputComponents
= 128,
467 .maxGeometryOutputVertices
= 256,
468 .maxGeometryTotalOutputComponents
= 1024,
469 .maxFragmentInputComponents
= 128,
470 .maxFragmentOutputAttachments
= 8,
471 .maxFragmentDualSrcAttachments
= 2,
472 .maxFragmentCombinedOutputResources
= 8,
473 .maxComputeSharedMemorySize
= 32768,
474 .maxComputeWorkGroupCount
= { 65535, 65535, 65535 },
475 .maxComputeWorkGroupInvocations
= 16 * devinfo
->max_cs_threads
,
476 .maxComputeWorkGroupSize
= {
477 16 * devinfo
->max_cs_threads
,
478 16 * devinfo
->max_cs_threads
,
479 16 * devinfo
->max_cs_threads
,
481 .subPixelPrecisionBits
= 4 /* FIXME */,
482 .subTexelPrecisionBits
= 4 /* FIXME */,
483 .mipmapPrecisionBits
= 4 /* FIXME */,
484 .maxDrawIndexedIndexValue
= UINT32_MAX
,
485 .maxDrawIndirectCount
= UINT32_MAX
,
486 .maxSamplerLodBias
= 16,
487 .maxSamplerAnisotropy
= 16,
488 .maxViewports
= MAX_VIEWPORTS
,
489 .maxViewportDimensions
= { (1 << 14), (1 << 14) },
490 .viewportBoundsRange
= { -16384.0, 16384.0 },
491 .viewportSubPixelBits
= 13, /* We take a float? */
492 .minMemoryMapAlignment
= 4096, /* A page */
493 .minTexelBufferOffsetAlignment
= 1,
494 .minUniformBufferOffsetAlignment
= 1,
495 .minStorageBufferOffsetAlignment
= 1,
496 .minTexelOffset
= -8,
498 .minTexelGatherOffset
= -8,
499 .maxTexelGatherOffset
= 7,
500 .minInterpolationOffset
= 0, /* FIXME */
501 .maxInterpolationOffset
= 0, /* FIXME */
502 .subPixelInterpolationOffsetBits
= 0, /* FIXME */
503 .maxFramebufferWidth
= (1 << 14),
504 .maxFramebufferHeight
= (1 << 14),
505 .maxFramebufferLayers
= (1 << 10),
506 .framebufferColorSampleCounts
= sample_counts
,
507 .framebufferDepthSampleCounts
= sample_counts
,
508 .framebufferStencilSampleCounts
= sample_counts
,
509 .framebufferNoAttachmentsSampleCounts
= sample_counts
,
510 .maxColorAttachments
= MAX_RTS
,
511 .sampledImageColorSampleCounts
= sample_counts
,
512 .sampledImageIntegerSampleCounts
= VK_SAMPLE_COUNT_1_BIT
,
513 .sampledImageDepthSampleCounts
= sample_counts
,
514 .sampledImageStencilSampleCounts
= sample_counts
,
515 .storageImageSampleCounts
= VK_SAMPLE_COUNT_1_BIT
,
516 .maxSampleMaskWords
= 1,
517 .timestampComputeAndGraphics
= false,
518 .timestampPeriod
= time_stamp_base
/ (1000 * 1000 * 1000),
519 .maxClipDistances
= 0 /* FIXME */,
520 .maxCullDistances
= 0 /* FIXME */,
521 .maxCombinedClipAndCullDistances
= 0 /* FIXME */,
522 .discreteQueuePriorities
= 1,
523 .pointSizeRange
= { 0.125, 255.875 },
524 .lineWidthRange
= { 0.0, 7.9921875 },
525 .pointSizeGranularity
= (1.0 / 8.0),
526 .lineWidthGranularity
= (1.0 / 128.0),
527 .strictLines
= false, /* FINISHME */
528 .standardSampleLocations
= true,
529 .optimalBufferCopyOffsetAlignment
= 128,
530 .optimalBufferCopyRowPitchAlignment
= 128,
531 .nonCoherentAtomSize
= 64,
534 *pProperties
= (VkPhysicalDeviceProperties
) {
535 .apiVersion
= VK_MAKE_VERSION(1, 0, 2),
538 .deviceID
= pdevice
->chipset_id
,
539 .deviceType
= VK_PHYSICAL_DEVICE_TYPE_INTEGRATED_GPU
,
541 .sparseProperties
= {0}, /* Broadwell doesn't do sparse. */
544 strcpy(pProperties
->deviceName
, pdevice
->name
);
545 anv_device_get_cache_uuid(pProperties
->pipelineCacheUUID
);
548 void anv_GetPhysicalDeviceQueueFamilyProperties(
549 VkPhysicalDevice physicalDevice
,
551 VkQueueFamilyProperties
* pQueueFamilyProperties
)
553 if (pQueueFamilyProperties
== NULL
) {
558 assert(*pCount
>= 1);
560 *pQueueFamilyProperties
= (VkQueueFamilyProperties
) {
561 .queueFlags
= VK_QUEUE_GRAPHICS_BIT
|
562 VK_QUEUE_COMPUTE_BIT
|
563 VK_QUEUE_TRANSFER_BIT
,
565 .timestampValidBits
= 36, /* XXX: Real value here */
566 .minImageTransferGranularity
= (VkExtent3D
) { 1, 1, 1 },
570 void anv_GetPhysicalDeviceMemoryProperties(
571 VkPhysicalDevice physicalDevice
,
572 VkPhysicalDeviceMemoryProperties
* pMemoryProperties
)
574 ANV_FROM_HANDLE(anv_physical_device
, physical_device
, physicalDevice
);
575 VkDeviceSize heap_size
;
577 /* Reserve some wiggle room for the driver by exposing only 75% of the
578 * aperture to the heap.
580 heap_size
= 3 * physical_device
->aperture_size
/ 4;
582 if (physical_device
->info
->has_llc
) {
583 /* Big core GPUs share LLC with the CPU and thus one memory type can be
584 * both cached and coherent at the same time.
586 pMemoryProperties
->memoryTypeCount
= 1;
587 pMemoryProperties
->memoryTypes
[0] = (VkMemoryType
) {
588 .propertyFlags
= VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT
|
589 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT
|
590 VK_MEMORY_PROPERTY_HOST_COHERENT_BIT
|
591 VK_MEMORY_PROPERTY_HOST_CACHED_BIT
,
595 /* The spec requires that we expose a host-visible, coherent memory
596 * type, but Atom GPUs don't share LLC. Thus we offer two memory types
597 * to give the application a choice between cached, but not coherent and
598 * coherent but uncached (WC though).
600 pMemoryProperties
->memoryTypeCount
= 2;
601 pMemoryProperties
->memoryTypes
[0] = (VkMemoryType
) {
602 .propertyFlags
= VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT
|
603 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT
|
604 VK_MEMORY_PROPERTY_HOST_COHERENT_BIT
,
607 pMemoryProperties
->memoryTypes
[1] = (VkMemoryType
) {
608 .propertyFlags
= VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT
|
609 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT
|
610 VK_MEMORY_PROPERTY_HOST_CACHED_BIT
,
615 pMemoryProperties
->memoryHeapCount
= 1;
616 pMemoryProperties
->memoryHeaps
[0] = (VkMemoryHeap
) {
618 .flags
= VK_MEMORY_HEAP_DEVICE_LOCAL_BIT
,
622 PFN_vkVoidFunction
anv_GetInstanceProcAddr(
626 return anv_lookup_entrypoint(pName
);
629 /* The loader wants us to expose a second GetInstanceProcAddr function
630 * to work around certain LD_PRELOAD issues seen in apps.
632 VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL
vk_icdGetInstanceProcAddr(
636 VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL
vk_icdGetInstanceProcAddr(
640 return anv_GetInstanceProcAddr(instance
, pName
);
643 PFN_vkVoidFunction
anv_GetDeviceProcAddr(
647 return anv_lookup_entrypoint(pName
);
651 anv_queue_init(struct anv_device
*device
, struct anv_queue
*queue
)
653 queue
->_loader_data
.loaderMagic
= ICD_LOADER_MAGIC
;
654 queue
->device
= device
;
655 queue
->pool
= &device
->surface_state_pool
;
661 anv_queue_finish(struct anv_queue
*queue
)
665 static struct anv_state
666 anv_state_pool_emit_data(struct anv_state_pool
*pool
, size_t size
, size_t align
, const void *p
)
668 struct anv_state state
;
670 state
= anv_state_pool_alloc(pool
, size
, align
);
671 memcpy(state
.map
, p
, size
);
673 if (!pool
->block_pool
->device
->info
.has_llc
)
674 anv_state_clflush(state
);
679 struct gen8_border_color
{
684 /* Pad out to 64 bytes */
689 anv_device_init_border_colors(struct anv_device
*device
)
691 static const struct gen8_border_color border_colors
[] = {
692 [VK_BORDER_COLOR_FLOAT_TRANSPARENT_BLACK
] = { .float32
= { 0.0, 0.0, 0.0, 0.0 } },
693 [VK_BORDER_COLOR_FLOAT_OPAQUE_BLACK
] = { .float32
= { 0.0, 0.0, 0.0, 1.0 } },
694 [VK_BORDER_COLOR_FLOAT_OPAQUE_WHITE
] = { .float32
= { 1.0, 1.0, 1.0, 1.0 } },
695 [VK_BORDER_COLOR_INT_TRANSPARENT_BLACK
] = { .uint32
= { 0, 0, 0, 0 } },
696 [VK_BORDER_COLOR_INT_OPAQUE_BLACK
] = { .uint32
= { 0, 0, 0, 1 } },
697 [VK_BORDER_COLOR_INT_OPAQUE_WHITE
] = { .uint32
= { 1, 1, 1, 1 } },
700 device
->border_colors
= anv_state_pool_emit_data(&device
->dynamic_state_pool
,
701 sizeof(border_colors
), 64,
706 anv_device_submit_simple_batch(struct anv_device
*device
,
707 struct anv_batch
*batch
)
709 struct drm_i915_gem_execbuffer2 execbuf
;
710 struct drm_i915_gem_exec_object2 exec2_objects
[1];
712 VkResult result
= VK_SUCCESS
;
717 /* Kernel driver requires 8 byte aligned batch length */
718 size
= align_u32(batch
->next
- batch
->start
, 8);
719 assert(size
< device
->batch_bo_pool
.bo_size
);
720 result
= anv_bo_pool_alloc(&device
->batch_bo_pool
, &bo
);
721 if (result
!= VK_SUCCESS
)
724 memcpy(bo
.map
, batch
->start
, size
);
725 if (!device
->info
.has_llc
)
726 anv_clflush_range(bo
.map
, size
);
728 exec2_objects
[0].handle
= bo
.gem_handle
;
729 exec2_objects
[0].relocation_count
= 0;
730 exec2_objects
[0].relocs_ptr
= 0;
731 exec2_objects
[0].alignment
= 0;
732 exec2_objects
[0].offset
= bo
.offset
;
733 exec2_objects
[0].flags
= 0;
734 exec2_objects
[0].rsvd1
= 0;
735 exec2_objects
[0].rsvd2
= 0;
737 execbuf
.buffers_ptr
= (uintptr_t) exec2_objects
;
738 execbuf
.buffer_count
= 1;
739 execbuf
.batch_start_offset
= 0;
740 execbuf
.batch_len
= size
;
741 execbuf
.cliprects_ptr
= 0;
742 execbuf
.num_cliprects
= 0;
747 I915_EXEC_HANDLE_LUT
| I915_EXEC_NO_RELOC
| I915_EXEC_RENDER
;
748 execbuf
.rsvd1
= device
->context_id
;
751 ret
= anv_gem_execbuffer(device
, &execbuf
);
753 /* We don't know the real error. */
754 result
= vk_errorf(VK_ERROR_OUT_OF_DEVICE_MEMORY
, "execbuf2 failed: %m");
759 ret
= anv_gem_wait(device
, bo
.gem_handle
, &timeout
);
761 /* We don't know the real error. */
762 result
= vk_errorf(VK_ERROR_OUT_OF_DEVICE_MEMORY
, "execbuf2 failed: %m");
767 anv_bo_pool_free(&device
->batch_bo_pool
, &bo
);
772 VkResult
anv_CreateDevice(
773 VkPhysicalDevice physicalDevice
,
774 const VkDeviceCreateInfo
* pCreateInfo
,
775 const VkAllocationCallbacks
* pAllocator
,
778 ANV_FROM_HANDLE(anv_physical_device
, physical_device
, physicalDevice
);
780 struct anv_device
*device
;
782 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO
);
784 for (uint32_t i
= 0; i
< pCreateInfo
->enabledExtensionCount
; i
++) {
786 for (uint32_t j
= 0; j
< ARRAY_SIZE(device_extensions
); j
++) {
787 if (strcmp(pCreateInfo
->ppEnabledExtensionNames
[i
],
788 device_extensions
[j
].extensionName
) == 0) {
794 return vk_error(VK_ERROR_EXTENSION_NOT_PRESENT
);
797 anv_set_dispatch_devinfo(physical_device
->info
);
799 device
= anv_alloc2(&physical_device
->instance
->alloc
, pAllocator
,
801 VK_SYSTEM_ALLOCATION_SCOPE_DEVICE
);
803 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
805 device
->_loader_data
.loaderMagic
= ICD_LOADER_MAGIC
;
806 device
->instance
= physical_device
->instance
;
807 device
->chipset_id
= physical_device
->chipset_id
;
810 device
->alloc
= *pAllocator
;
812 device
->alloc
= physical_device
->instance
->alloc
;
814 /* XXX(chadv): Can we dup() physicalDevice->fd here? */
815 device
->fd
= open(physical_device
->path
, O_RDWR
| O_CLOEXEC
);
816 if (device
->fd
== -1) {
817 result
= vk_error(VK_ERROR_INITIALIZATION_FAILED
);
821 device
->context_id
= anv_gem_create_context(device
);
822 if (device
->context_id
== -1) {
823 result
= vk_error(VK_ERROR_INITIALIZATION_FAILED
);
827 device
->info
= *physical_device
->info
;
828 device
->isl_dev
= physical_device
->isl_dev
;
830 pthread_mutex_init(&device
->mutex
, NULL
);
832 anv_bo_pool_init(&device
->batch_bo_pool
, device
, ANV_CMD_BUFFER_BATCH_SIZE
);
834 anv_block_pool_init(&device
->dynamic_state_block_pool
, device
, 16384);
836 anv_state_pool_init(&device
->dynamic_state_pool
,
837 &device
->dynamic_state_block_pool
);
839 anv_block_pool_init(&device
->instruction_block_pool
, device
, 128 * 1024);
840 anv_pipeline_cache_init(&device
->default_pipeline_cache
, device
);
842 anv_block_pool_init(&device
->surface_state_block_pool
, device
, 4096);
844 anv_state_pool_init(&device
->surface_state_pool
,
845 &device
->surface_state_block_pool
);
847 anv_bo_init_new(&device
->workaround_bo
, device
, 1024);
849 anv_block_pool_init(&device
->scratch_block_pool
, device
, 0x10000);
851 anv_queue_init(device
, &device
->queue
);
853 switch (device
->info
.gen
) {
855 if (!device
->info
.is_haswell
)
856 result
= gen7_init_device_state(device
);
858 result
= gen75_init_device_state(device
);
861 result
= gen8_init_device_state(device
);
864 result
= gen9_init_device_state(device
);
867 /* Shouldn't get here as we don't create physical devices for any other
869 unreachable("unhandled gen");
871 if (result
!= VK_SUCCESS
)
874 result
= anv_device_init_meta(device
);
875 if (result
!= VK_SUCCESS
)
878 anv_device_init_border_colors(device
);
880 *pDevice
= anv_device_to_handle(device
);
887 anv_free(&device
->alloc
, device
);
892 void anv_DestroyDevice(
894 const VkAllocationCallbacks
* pAllocator
)
896 ANV_FROM_HANDLE(anv_device
, device
, _device
);
898 anv_queue_finish(&device
->queue
);
900 anv_device_finish_meta(device
);
903 /* We only need to free these to prevent valgrind errors. The backing
904 * BO will go away in a couple of lines so we don't actually leak.
906 anv_state_pool_free(&device
->dynamic_state_pool
, device
->border_colors
);
909 anv_gem_munmap(device
->workaround_bo
.map
, device
->workaround_bo
.size
);
910 anv_gem_close(device
, device
->workaround_bo
.gem_handle
);
912 anv_bo_pool_finish(&device
->batch_bo_pool
);
913 anv_state_pool_finish(&device
->dynamic_state_pool
);
914 anv_block_pool_finish(&device
->dynamic_state_block_pool
);
915 anv_block_pool_finish(&device
->instruction_block_pool
);
916 anv_state_pool_finish(&device
->surface_state_pool
);
917 anv_block_pool_finish(&device
->surface_state_block_pool
);
918 anv_block_pool_finish(&device
->scratch_block_pool
);
922 pthread_mutex_destroy(&device
->mutex
);
924 anv_free(&device
->alloc
, device
);
927 VkResult
anv_EnumerateInstanceExtensionProperties(
928 const char* pLayerName
,
929 uint32_t* pPropertyCount
,
930 VkExtensionProperties
* pProperties
)
932 if (pProperties
== NULL
) {
933 *pPropertyCount
= ARRAY_SIZE(global_extensions
);
937 assert(*pPropertyCount
>= ARRAY_SIZE(global_extensions
));
939 *pPropertyCount
= ARRAY_SIZE(global_extensions
);
940 memcpy(pProperties
, global_extensions
, sizeof(global_extensions
));
945 VkResult
anv_EnumerateDeviceExtensionProperties(
946 VkPhysicalDevice physicalDevice
,
947 const char* pLayerName
,
948 uint32_t* pPropertyCount
,
949 VkExtensionProperties
* pProperties
)
951 if (pProperties
== NULL
) {
952 *pPropertyCount
= ARRAY_SIZE(device_extensions
);
956 assert(*pPropertyCount
>= ARRAY_SIZE(device_extensions
));
958 *pPropertyCount
= ARRAY_SIZE(device_extensions
);
959 memcpy(pProperties
, device_extensions
, sizeof(device_extensions
));
964 VkResult
anv_EnumerateInstanceLayerProperties(
965 uint32_t* pPropertyCount
,
966 VkLayerProperties
* pProperties
)
968 if (pProperties
== NULL
) {
973 /* None supported at this time */
974 return vk_error(VK_ERROR_LAYER_NOT_PRESENT
);
977 VkResult
anv_EnumerateDeviceLayerProperties(
978 VkPhysicalDevice physicalDevice
,
979 uint32_t* pPropertyCount
,
980 VkLayerProperties
* pProperties
)
982 if (pProperties
== NULL
) {
987 /* None supported at this time */
988 return vk_error(VK_ERROR_LAYER_NOT_PRESENT
);
991 void anv_GetDeviceQueue(
993 uint32_t queueNodeIndex
,
997 ANV_FROM_HANDLE(anv_device
, device
, _device
);
999 assert(queueIndex
== 0);
1001 *pQueue
= anv_queue_to_handle(&device
->queue
);
1004 VkResult
anv_QueueSubmit(
1006 uint32_t submitCount
,
1007 const VkSubmitInfo
* pSubmits
,
1010 ANV_FROM_HANDLE(anv_queue
, queue
, _queue
);
1011 ANV_FROM_HANDLE(anv_fence
, fence
, _fence
);
1012 struct anv_device
*device
= queue
->device
;
1015 for (uint32_t i
= 0; i
< submitCount
; i
++) {
1016 for (uint32_t j
= 0; j
< pSubmits
[i
].commandBufferCount
; j
++) {
1017 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
,
1018 pSubmits
[i
].pCommandBuffers
[j
]);
1019 assert(cmd_buffer
->level
== VK_COMMAND_BUFFER_LEVEL_PRIMARY
);
1021 ret
= anv_gem_execbuffer(device
, &cmd_buffer
->execbuf2
.execbuf
);
1023 /* We don't know the real error. */
1024 return vk_errorf(VK_ERROR_OUT_OF_DEVICE_MEMORY
,
1025 "execbuf2 failed: %m");
1028 for (uint32_t k
= 0; k
< cmd_buffer
->execbuf2
.bo_count
; k
++)
1029 cmd_buffer
->execbuf2
.bos
[k
]->offset
= cmd_buffer
->execbuf2
.objects
[k
].offset
;
1034 ret
= anv_gem_execbuffer(device
, &fence
->execbuf
);
1036 /* We don't know the real error. */
1037 return vk_errorf(VK_ERROR_OUT_OF_DEVICE_MEMORY
,
1038 "execbuf2 failed: %m");
1045 VkResult
anv_QueueWaitIdle(
1048 ANV_FROM_HANDLE(anv_queue
, queue
, _queue
);
1050 return ANV_CALL(DeviceWaitIdle
)(anv_device_to_handle(queue
->device
));
1053 VkResult
anv_DeviceWaitIdle(
1056 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1057 struct anv_batch batch
;
1060 batch
.start
= batch
.next
= cmds
;
1061 batch
.end
= (void *) cmds
+ sizeof(cmds
);
1063 anv_batch_emit(&batch
, GEN7_MI_BATCH_BUFFER_END
);
1064 anv_batch_emit(&batch
, GEN7_MI_NOOP
);
1066 return anv_device_submit_simple_batch(device
, &batch
);
1070 anv_bo_init_new(struct anv_bo
*bo
, struct anv_device
*device
, uint64_t size
)
1072 bo
->gem_handle
= anv_gem_create(device
, size
);
1073 if (!bo
->gem_handle
)
1074 return vk_error(VK_ERROR_OUT_OF_DEVICE_MEMORY
);
1080 bo
->is_winsys_bo
= false;
1085 VkResult
anv_AllocateMemory(
1087 const VkMemoryAllocateInfo
* pAllocateInfo
,
1088 const VkAllocationCallbacks
* pAllocator
,
1089 VkDeviceMemory
* pMem
)
1091 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1092 struct anv_device_memory
*mem
;
1095 assert(pAllocateInfo
->sType
== VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO
);
1097 if (pAllocateInfo
->allocationSize
== 0) {
1098 /* Apparently, this is allowed */
1099 *pMem
= VK_NULL_HANDLE
;
1103 /* We support exactly one memory heap. */
1104 assert(pAllocateInfo
->memoryTypeIndex
== 0 ||
1105 (!device
->info
.has_llc
&& pAllocateInfo
->memoryTypeIndex
< 2));
1107 /* FINISHME: Fail if allocation request exceeds heap size. */
1109 mem
= anv_alloc2(&device
->alloc
, pAllocator
, sizeof(*mem
), 8,
1110 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
1112 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
1114 /* The kernel is going to give us whole pages anyway */
1115 uint64_t alloc_size
= align_u64(pAllocateInfo
->allocationSize
, 4096);
1117 result
= anv_bo_init_new(&mem
->bo
, device
, alloc_size
);
1118 if (result
!= VK_SUCCESS
)
1121 mem
->type_index
= pAllocateInfo
->memoryTypeIndex
;
1123 *pMem
= anv_device_memory_to_handle(mem
);
1128 anv_free2(&device
->alloc
, pAllocator
, mem
);
1133 void anv_FreeMemory(
1135 VkDeviceMemory _mem
,
1136 const VkAllocationCallbacks
* pAllocator
)
1138 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1139 ANV_FROM_HANDLE(anv_device_memory
, mem
, _mem
);
1145 anv_gem_munmap(mem
->bo
.map
, mem
->bo
.size
);
1147 if (mem
->bo
.gem_handle
!= 0)
1148 anv_gem_close(device
, mem
->bo
.gem_handle
);
1150 anv_free2(&device
->alloc
, pAllocator
, mem
);
1153 VkResult
anv_MapMemory(
1155 VkDeviceMemory _memory
,
1156 VkDeviceSize offset
,
1158 VkMemoryMapFlags flags
,
1161 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1162 ANV_FROM_HANDLE(anv_device_memory
, mem
, _memory
);
1169 if (size
== VK_WHOLE_SIZE
)
1170 size
= mem
->bo
.size
- offset
;
1172 /* FIXME: Is this supposed to be thread safe? Since vkUnmapMemory() only
1173 * takes a VkDeviceMemory pointer, it seems like only one map of the memory
1174 * at a time is valid. We could just mmap up front and return an offset
1175 * pointer here, but that may exhaust virtual memory on 32 bit
1178 uint32_t gem_flags
= 0;
1179 if (!device
->info
.has_llc
&& mem
->type_index
== 0)
1180 gem_flags
|= I915_MMAP_WC
;
1182 /* GEM will fail to map if the offset isn't 4k-aligned. Round down. */
1183 uint64_t map_offset
= offset
& ~4095ull;
1184 assert(offset
>= map_offset
);
1185 uint64_t map_size
= (offset
+ size
) - map_offset
;
1187 /* Let's map whole pages */
1188 map_size
= align_u64(map_size
, 4096);
1190 mem
->map
= anv_gem_mmap(device
, mem
->bo
.gem_handle
,
1191 map_offset
, map_size
, gem_flags
);
1192 mem
->map_size
= map_size
;
1194 *ppData
= mem
->map
+ (offset
- map_offset
);
1199 void anv_UnmapMemory(
1201 VkDeviceMemory _memory
)
1203 ANV_FROM_HANDLE(anv_device_memory
, mem
, _memory
);
1208 anv_gem_munmap(mem
->map
, mem
->map_size
);
1212 clflush_mapped_ranges(struct anv_device
*device
,
1214 const VkMappedMemoryRange
*ranges
)
1216 for (uint32_t i
= 0; i
< count
; i
++) {
1217 ANV_FROM_HANDLE(anv_device_memory
, mem
, ranges
[i
].memory
);
1218 void *p
= mem
->map
+ (ranges
[i
].offset
& ~CACHELINE_MASK
);
1221 if (ranges
[i
].offset
+ ranges
[i
].size
> mem
->map_size
)
1222 end
= mem
->map
+ mem
->map_size
;
1224 end
= mem
->map
+ ranges
[i
].offset
+ ranges
[i
].size
;
1227 __builtin_ia32_clflush(p
);
1228 p
+= CACHELINE_SIZE
;
1233 VkResult
anv_FlushMappedMemoryRanges(
1235 uint32_t memoryRangeCount
,
1236 const VkMappedMemoryRange
* pMemoryRanges
)
1238 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1240 if (device
->info
.has_llc
)
1243 /* Make sure the writes we're flushing have landed. */
1244 __builtin_ia32_mfence();
1246 clflush_mapped_ranges(device
, memoryRangeCount
, pMemoryRanges
);
1251 VkResult
anv_InvalidateMappedMemoryRanges(
1253 uint32_t memoryRangeCount
,
1254 const VkMappedMemoryRange
* pMemoryRanges
)
1256 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1258 if (device
->info
.has_llc
)
1261 clflush_mapped_ranges(device
, memoryRangeCount
, pMemoryRanges
);
1263 /* Make sure no reads get moved up above the invalidate. */
1264 __builtin_ia32_mfence();
1269 void anv_GetBufferMemoryRequirements(
1272 VkMemoryRequirements
* pMemoryRequirements
)
1274 ANV_FROM_HANDLE(anv_buffer
, buffer
, _buffer
);
1276 /* The Vulkan spec (git aaed022) says:
1278 * memoryTypeBits is a bitfield and contains one bit set for every
1279 * supported memory type for the resource. The bit `1<<i` is set if and
1280 * only if the memory type `i` in the VkPhysicalDeviceMemoryProperties
1281 * structure for the physical device is supported.
1283 * We support exactly one memory type.
1285 pMemoryRequirements
->memoryTypeBits
= 1;
1287 pMemoryRequirements
->size
= buffer
->size
;
1288 pMemoryRequirements
->alignment
= 16;
1291 void anv_GetImageMemoryRequirements(
1294 VkMemoryRequirements
* pMemoryRequirements
)
1296 ANV_FROM_HANDLE(anv_image
, image
, _image
);
1298 /* The Vulkan spec (git aaed022) says:
1300 * memoryTypeBits is a bitfield and contains one bit set for every
1301 * supported memory type for the resource. The bit `1<<i` is set if and
1302 * only if the memory type `i` in the VkPhysicalDeviceMemoryProperties
1303 * structure for the physical device is supported.
1305 * We support exactly one memory type.
1307 pMemoryRequirements
->memoryTypeBits
= 1;
1309 pMemoryRequirements
->size
= image
->size
;
1310 pMemoryRequirements
->alignment
= image
->alignment
;
1313 void anv_GetImageSparseMemoryRequirements(
1316 uint32_t* pSparseMemoryRequirementCount
,
1317 VkSparseImageMemoryRequirements
* pSparseMemoryRequirements
)
1322 void anv_GetDeviceMemoryCommitment(
1324 VkDeviceMemory memory
,
1325 VkDeviceSize
* pCommittedMemoryInBytes
)
1327 *pCommittedMemoryInBytes
= 0;
1330 VkResult
anv_BindBufferMemory(
1333 VkDeviceMemory _memory
,
1334 VkDeviceSize memoryOffset
)
1336 ANV_FROM_HANDLE(anv_device_memory
, mem
, _memory
);
1337 ANV_FROM_HANDLE(anv_buffer
, buffer
, _buffer
);
1340 buffer
->bo
= &mem
->bo
;
1341 buffer
->offset
= memoryOffset
;
1350 VkResult
anv_BindImageMemory(
1353 VkDeviceMemory _memory
,
1354 VkDeviceSize memoryOffset
)
1356 ANV_FROM_HANDLE(anv_device_memory
, mem
, _memory
);
1357 ANV_FROM_HANDLE(anv_image
, image
, _image
);
1360 image
->bo
= &mem
->bo
;
1361 image
->offset
= memoryOffset
;
1370 VkResult
anv_QueueBindSparse(
1372 uint32_t bindInfoCount
,
1373 const VkBindSparseInfo
* pBindInfo
,
1376 stub_return(VK_ERROR_INCOMPATIBLE_DRIVER
);
1379 VkResult
anv_CreateFence(
1381 const VkFenceCreateInfo
* pCreateInfo
,
1382 const VkAllocationCallbacks
* pAllocator
,
1385 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1386 struct anv_fence
*fence
;
1387 struct anv_batch batch
;
1390 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_FENCE_CREATE_INFO
);
1392 fence
= anv_alloc2(&device
->alloc
, pAllocator
, sizeof(*fence
), 8,
1393 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
1395 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
1397 result
= anv_bo_pool_alloc(&device
->batch_bo_pool
, &fence
->bo
);
1398 if (result
!= VK_SUCCESS
)
1401 batch
.next
= batch
.start
= fence
->bo
.map
;
1402 batch
.end
= fence
->bo
.map
+ fence
->bo
.size
;
1403 anv_batch_emit(&batch
, GEN7_MI_BATCH_BUFFER_END
);
1404 anv_batch_emit(&batch
, GEN7_MI_NOOP
);
1406 if (!device
->info
.has_llc
) {
1407 assert(((uintptr_t) fence
->bo
.map
& CACHELINE_MASK
) == 0);
1408 assert(batch
.next
- fence
->bo
.map
<= CACHELINE_SIZE
);
1409 __builtin_ia32_mfence();
1410 __builtin_ia32_clflush(fence
->bo
.map
);
1413 fence
->exec2_objects
[0].handle
= fence
->bo
.gem_handle
;
1414 fence
->exec2_objects
[0].relocation_count
= 0;
1415 fence
->exec2_objects
[0].relocs_ptr
= 0;
1416 fence
->exec2_objects
[0].alignment
= 0;
1417 fence
->exec2_objects
[0].offset
= fence
->bo
.offset
;
1418 fence
->exec2_objects
[0].flags
= 0;
1419 fence
->exec2_objects
[0].rsvd1
= 0;
1420 fence
->exec2_objects
[0].rsvd2
= 0;
1422 fence
->execbuf
.buffers_ptr
= (uintptr_t) fence
->exec2_objects
;
1423 fence
->execbuf
.buffer_count
= 1;
1424 fence
->execbuf
.batch_start_offset
= 0;
1425 fence
->execbuf
.batch_len
= batch
.next
- fence
->bo
.map
;
1426 fence
->execbuf
.cliprects_ptr
= 0;
1427 fence
->execbuf
.num_cliprects
= 0;
1428 fence
->execbuf
.DR1
= 0;
1429 fence
->execbuf
.DR4
= 0;
1431 fence
->execbuf
.flags
=
1432 I915_EXEC_HANDLE_LUT
| I915_EXEC_NO_RELOC
| I915_EXEC_RENDER
;
1433 fence
->execbuf
.rsvd1
= device
->context_id
;
1434 fence
->execbuf
.rsvd2
= 0;
1436 fence
->ready
= false;
1438 *pFence
= anv_fence_to_handle(fence
);
1443 anv_free2(&device
->alloc
, pAllocator
, fence
);
1448 void anv_DestroyFence(
1451 const VkAllocationCallbacks
* pAllocator
)
1453 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1454 ANV_FROM_HANDLE(anv_fence
, fence
, _fence
);
1456 anv_bo_pool_free(&device
->batch_bo_pool
, &fence
->bo
);
1457 anv_free2(&device
->alloc
, pAllocator
, fence
);
1460 VkResult
anv_ResetFences(
1462 uint32_t fenceCount
,
1463 const VkFence
* pFences
)
1465 for (uint32_t i
= 0; i
< fenceCount
; i
++) {
1466 ANV_FROM_HANDLE(anv_fence
, fence
, pFences
[i
]);
1467 fence
->ready
= false;
1473 VkResult
anv_GetFenceStatus(
1477 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1478 ANV_FROM_HANDLE(anv_fence
, fence
, _fence
);
1485 ret
= anv_gem_wait(device
, fence
->bo
.gem_handle
, &t
);
1487 fence
->ready
= true;
1491 return VK_NOT_READY
;
1494 VkResult
anv_WaitForFences(
1496 uint32_t fenceCount
,
1497 const VkFence
* pFences
,
1501 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1503 /* DRM_IOCTL_I915_GEM_WAIT uses a signed 64 bit timeout and is supposed
1504 * to block indefinitely timeouts <= 0. Unfortunately, this was broken
1505 * for a couple of kernel releases. Since there's no way to know
1506 * whether or not the kernel we're using is one of the broken ones, the
1507 * best we can do is to clamp the timeout to INT64_MAX. This limits the
1508 * maximum timeout from 584 years to 292 years - likely not a big deal.
1510 if (timeout
> INT64_MAX
)
1511 timeout
= INT64_MAX
;
1513 int64_t t
= timeout
;
1515 /* FIXME: handle !waitAll */
1517 for (uint32_t i
= 0; i
< fenceCount
; i
++) {
1518 ANV_FROM_HANDLE(anv_fence
, fence
, pFences
[i
]);
1519 int ret
= anv_gem_wait(device
, fence
->bo
.gem_handle
, &t
);
1520 if (ret
== -1 && errno
== ETIME
) {
1522 } else if (ret
== -1) {
1523 /* We don't know the real error. */
1524 return vk_errorf(VK_ERROR_OUT_OF_DEVICE_MEMORY
,
1525 "gem wait failed: %m");
1532 // Queue semaphore functions
1534 VkResult
anv_CreateSemaphore(
1536 const VkSemaphoreCreateInfo
* pCreateInfo
,
1537 const VkAllocationCallbacks
* pAllocator
,
1538 VkSemaphore
* pSemaphore
)
1540 /* The DRM execbuffer ioctl always execute in-oder, even between different
1541 * rings. As such, there's nothing to do for the user space semaphore.
1544 *pSemaphore
= (VkSemaphore
)1;
1549 void anv_DestroySemaphore(
1551 VkSemaphore semaphore
,
1552 const VkAllocationCallbacks
* pAllocator
)
1558 VkResult
anv_CreateEvent(
1560 const VkEventCreateInfo
* pCreateInfo
,
1561 const VkAllocationCallbacks
* pAllocator
,
1564 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1565 struct anv_state state
;
1566 struct anv_event
*event
;
1568 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_EVENT_CREATE_INFO
);
1570 state
= anv_state_pool_alloc(&device
->dynamic_state_pool
,
1573 event
->state
= state
;
1574 event
->semaphore
= VK_EVENT_RESET
;
1576 if (!device
->info
.has_llc
) {
1577 /* Make sure the writes we're flushing have landed. */
1578 __builtin_ia32_mfence();
1579 __builtin_ia32_clflush(event
);
1582 *pEvent
= anv_event_to_handle(event
);
1587 void anv_DestroyEvent(
1590 const VkAllocationCallbacks
* pAllocator
)
1592 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1593 ANV_FROM_HANDLE(anv_event
, event
, _event
);
1595 anv_state_pool_free(&device
->dynamic_state_pool
, event
->state
);
1598 VkResult
anv_GetEventStatus(
1602 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1603 ANV_FROM_HANDLE(anv_event
, event
, _event
);
1605 if (!device
->info
.has_llc
) {
1606 /* Invalidate read cache before reading event written by GPU. */
1607 __builtin_ia32_clflush(event
);
1608 __builtin_ia32_mfence();
1612 return event
->semaphore
;
1615 VkResult
anv_SetEvent(
1619 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1620 ANV_FROM_HANDLE(anv_event
, event
, _event
);
1622 event
->semaphore
= VK_EVENT_SET
;
1624 if (!device
->info
.has_llc
) {
1625 /* Make sure the writes we're flushing have landed. */
1626 __builtin_ia32_mfence();
1627 __builtin_ia32_clflush(event
);
1633 VkResult
anv_ResetEvent(
1637 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1638 ANV_FROM_HANDLE(anv_event
, event
, _event
);
1640 event
->semaphore
= VK_EVENT_RESET
;
1642 if (!device
->info
.has_llc
) {
1643 /* Make sure the writes we're flushing have landed. */
1644 __builtin_ia32_mfence();
1645 __builtin_ia32_clflush(event
);
1653 VkResult
anv_CreateBuffer(
1655 const VkBufferCreateInfo
* pCreateInfo
,
1656 const VkAllocationCallbacks
* pAllocator
,
1659 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1660 struct anv_buffer
*buffer
;
1662 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO
);
1664 buffer
= anv_alloc2(&device
->alloc
, pAllocator
, sizeof(*buffer
), 8,
1665 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
1667 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
1669 buffer
->size
= pCreateInfo
->size
;
1670 buffer
->usage
= pCreateInfo
->usage
;
1674 *pBuffer
= anv_buffer_to_handle(buffer
);
1679 void anv_DestroyBuffer(
1682 const VkAllocationCallbacks
* pAllocator
)
1684 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1685 ANV_FROM_HANDLE(anv_buffer
, buffer
, _buffer
);
1687 anv_free2(&device
->alloc
, pAllocator
, buffer
);
1691 anv_fill_buffer_surface_state(struct anv_device
*device
, struct anv_state state
,
1692 enum isl_format format
,
1693 uint32_t offset
, uint32_t range
, uint32_t stride
)
1695 isl_buffer_fill_state(&device
->isl_dev
, state
.map
,
1697 .mocs
= device
->default_mocs
,
1702 if (!device
->info
.has_llc
)
1703 anv_state_clflush(state
);
1706 void anv_DestroySampler(
1709 const VkAllocationCallbacks
* pAllocator
)
1711 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1712 ANV_FROM_HANDLE(anv_sampler
, sampler
, _sampler
);
1714 anv_free2(&device
->alloc
, pAllocator
, sampler
);
1717 VkResult
anv_CreateFramebuffer(
1719 const VkFramebufferCreateInfo
* pCreateInfo
,
1720 const VkAllocationCallbacks
* pAllocator
,
1721 VkFramebuffer
* pFramebuffer
)
1723 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1724 struct anv_framebuffer
*framebuffer
;
1726 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO
);
1728 size_t size
= sizeof(*framebuffer
) +
1729 sizeof(struct anv_image_view
*) * pCreateInfo
->attachmentCount
;
1730 framebuffer
= anv_alloc2(&device
->alloc
, pAllocator
, size
, 8,
1731 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
1732 if (framebuffer
== NULL
)
1733 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
1735 framebuffer
->attachment_count
= pCreateInfo
->attachmentCount
;
1736 for (uint32_t i
= 0; i
< pCreateInfo
->attachmentCount
; i
++) {
1737 VkImageView _iview
= pCreateInfo
->pAttachments
[i
];
1738 framebuffer
->attachments
[i
] = anv_image_view_from_handle(_iview
);
1741 framebuffer
->width
= pCreateInfo
->width
;
1742 framebuffer
->height
= pCreateInfo
->height
;
1743 framebuffer
->layers
= pCreateInfo
->layers
;
1745 *pFramebuffer
= anv_framebuffer_to_handle(framebuffer
);
1750 void anv_DestroyFramebuffer(
1753 const VkAllocationCallbacks
* pAllocator
)
1755 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1756 ANV_FROM_HANDLE(anv_framebuffer
, fb
, _fb
);
1758 anv_free2(&device
->alloc
, pAllocator
, fb
);
1761 void vkCmdDbgMarkerBegin(
1762 VkCommandBuffer commandBuffer
,
1763 const char* pMarker
)
1764 __attribute__ ((visibility ("default")));
1766 void vkCmdDbgMarkerEnd(
1767 VkCommandBuffer commandBuffer
)
1768 __attribute__ ((visibility ("default")));
1770 void vkCmdDbgMarkerBegin(
1771 VkCommandBuffer commandBuffer
,
1772 const char* pMarker
)
1776 void vkCmdDbgMarkerEnd(
1777 VkCommandBuffer commandBuffer
)