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 "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
= pCreateInfo
->pApplicationInfo
->apiVersion
;
218 if (VK_MAKE_VERSION(1, 0, 0) > client_version
||
219 client_version
> VK_MAKE_VERSION(1, 0, 3)) {
220 return vk_errorf(VK_ERROR_INCOMPATIBLE_DRIVER
,
221 "Client requested version %d.%d.%d",
222 VK_VERSION_MAJOR(client_version
),
223 VK_VERSION_MINOR(client_version
),
224 VK_VERSION_PATCH(client_version
));
227 for (uint32_t i
= 0; i
< pCreateInfo
->enabledExtensionCount
; i
++) {
229 for (uint32_t j
= 0; j
< ARRAY_SIZE(global_extensions
); j
++) {
230 if (strcmp(pCreateInfo
->ppEnabledExtensionNames
[i
],
231 global_extensions
[j
].extensionName
) == 0) {
237 return vk_error(VK_ERROR_EXTENSION_NOT_PRESENT
);
240 instance
= anv_alloc2(&default_alloc
, pAllocator
, sizeof(*instance
), 8,
241 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE
);
243 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
245 instance
->_loader_data
.loaderMagic
= ICD_LOADER_MAGIC
;
248 instance
->alloc
= *pAllocator
;
250 instance
->alloc
= default_alloc
;
252 instance
->apiVersion
= pCreateInfo
->pApplicationInfo
->apiVersion
;
253 instance
->physicalDeviceCount
= -1;
257 VG(VALGRIND_CREATE_MEMPOOL(instance
, 0, false));
259 anv_init_wsi(instance
);
261 *pInstance
= anv_instance_to_handle(instance
);
266 void anv_DestroyInstance(
267 VkInstance _instance
,
268 const VkAllocationCallbacks
* pAllocator
)
270 ANV_FROM_HANDLE(anv_instance
, instance
, _instance
);
272 if (instance
->physicalDeviceCount
> 0) {
273 /* We support at most one physical device. */
274 assert(instance
->physicalDeviceCount
== 1);
275 anv_physical_device_finish(&instance
->physicalDevice
);
278 anv_finish_wsi(instance
);
280 VG(VALGRIND_DESTROY_MEMPOOL(instance
));
284 anv_free(&instance
->alloc
, instance
);
287 VkResult
anv_EnumeratePhysicalDevices(
288 VkInstance _instance
,
289 uint32_t* pPhysicalDeviceCount
,
290 VkPhysicalDevice
* pPhysicalDevices
)
292 ANV_FROM_HANDLE(anv_instance
, instance
, _instance
);
295 if (instance
->physicalDeviceCount
< 0) {
296 result
= anv_physical_device_init(&instance
->physicalDevice
,
297 instance
, "/dev/dri/renderD128");
298 if (result
== VK_ERROR_INCOMPATIBLE_DRIVER
) {
299 instance
->physicalDeviceCount
= 0;
300 } else if (result
== VK_SUCCESS
) {
301 instance
->physicalDeviceCount
= 1;
307 /* pPhysicalDeviceCount is an out parameter if pPhysicalDevices is NULL;
308 * otherwise it's an inout parameter.
310 * The Vulkan spec (git aaed022) says:
312 * pPhysicalDeviceCount is a pointer to an unsigned integer variable
313 * that is initialized with the number of devices the application is
314 * prepared to receive handles to. pname:pPhysicalDevices is pointer to
315 * an array of at least this many VkPhysicalDevice handles [...].
317 * Upon success, if pPhysicalDevices is NULL, vkEnumeratePhysicalDevices
318 * overwrites the contents of the variable pointed to by
319 * pPhysicalDeviceCount with the number of physical devices in in the
320 * instance; otherwise, vkEnumeratePhysicalDevices overwrites
321 * pPhysicalDeviceCount with the number of physical handles written to
324 if (!pPhysicalDevices
) {
325 *pPhysicalDeviceCount
= instance
->physicalDeviceCount
;
326 } else if (*pPhysicalDeviceCount
>= 1) {
327 pPhysicalDevices
[0] = anv_physical_device_to_handle(&instance
->physicalDevice
);
328 *pPhysicalDeviceCount
= 1;
330 *pPhysicalDeviceCount
= 0;
336 void anv_GetPhysicalDeviceFeatures(
337 VkPhysicalDevice physicalDevice
,
338 VkPhysicalDeviceFeatures
* pFeatures
)
340 anv_finishme("Get correct values for PhysicalDeviceFeatures");
342 *pFeatures
= (VkPhysicalDeviceFeatures
) {
343 .robustBufferAccess
= true,
344 .fullDrawIndexUint32
= false,
345 .imageCubeArray
= false,
346 .independentBlend
= false,
347 .geometryShader
= true,
348 .tessellationShader
= false,
349 .sampleRateShading
= false,
350 .dualSrcBlend
= true,
352 .multiDrawIndirect
= false,
353 .drawIndirectFirstInstance
= false,
355 .depthBiasClamp
= false,
356 .fillModeNonSolid
= true,
357 .depthBounds
= false,
361 .multiViewport
= true,
362 .samplerAnisotropy
= false, /* FINISHME */
363 .textureCompressionETC2
= true,
364 .textureCompressionASTC_LDR
= true,
365 .textureCompressionBC
= true,
366 .occlusionQueryPrecise
= false, /* FINISHME */
367 .pipelineStatisticsQuery
= true,
368 .vertexPipelineStoresAndAtomics
= false,
369 .fragmentStoresAndAtomics
= true,
370 .shaderTessellationAndGeometryPointSize
= true,
371 .shaderImageGatherExtended
= true,
372 .shaderStorageImageExtendedFormats
= false,
373 .shaderStorageImageMultisample
= false,
374 .shaderUniformBufferArrayDynamicIndexing
= true,
375 .shaderSampledImageArrayDynamicIndexing
= false,
376 .shaderStorageBufferArrayDynamicIndexing
= false,
377 .shaderStorageImageArrayDynamicIndexing
= false,
378 .shaderStorageImageReadWithoutFormat
= false,
379 .shaderStorageImageWriteWithoutFormat
= true,
380 .shaderClipDistance
= false,
381 .shaderCullDistance
= false,
382 .shaderFloat64
= false,
383 .shaderInt64
= false,
384 .shaderInt16
= false,
386 .variableMultisampleRate
= false,
387 .inheritedQueries
= false,
392 anv_device_get_cache_uuid(void *uuid
)
394 memset(uuid
, 0, VK_UUID_SIZE
);
395 snprintf(uuid
, VK_UUID_SIZE
, "anv-%s", MESA_GIT_SHA1
+ 4);
398 void anv_GetPhysicalDeviceProperties(
399 VkPhysicalDevice physicalDevice
,
400 VkPhysicalDeviceProperties
* pProperties
)
402 ANV_FROM_HANDLE(anv_physical_device
, pdevice
, physicalDevice
);
403 const struct brw_device_info
*devinfo
= pdevice
->info
;
405 anv_finishme("Get correct values for VkPhysicalDeviceLimits");
407 const float time_stamp_base
= devinfo
->gen
>= 9 ? 83.333 : 80.0;
409 VkSampleCountFlags sample_counts
=
410 isl_device_get_sample_counts(&pdevice
->isl_dev
);
412 VkPhysicalDeviceLimits limits
= {
413 .maxImageDimension1D
= (1 << 14),
414 .maxImageDimension2D
= (1 << 14),
415 .maxImageDimension3D
= (1 << 10),
416 .maxImageDimensionCube
= (1 << 14),
417 .maxImageArrayLayers
= (1 << 10),
418 .maxTexelBufferElements
= 128 * 1024 * 1024,
419 .maxUniformBufferRange
= UINT32_MAX
,
420 .maxStorageBufferRange
= UINT32_MAX
,
421 .maxPushConstantsSize
= MAX_PUSH_CONSTANTS_SIZE
,
422 .maxMemoryAllocationCount
= UINT32_MAX
,
423 .maxSamplerAllocationCount
= 64 * 1024,
424 .bufferImageGranularity
= 64, /* A cache line */
425 .sparseAddressSpaceSize
= 0,
426 .maxBoundDescriptorSets
= MAX_SETS
,
427 .maxPerStageDescriptorSamplers
= 64,
428 .maxPerStageDescriptorUniformBuffers
= 64,
429 .maxPerStageDescriptorStorageBuffers
= 64,
430 .maxPerStageDescriptorSampledImages
= 64,
431 .maxPerStageDescriptorStorageImages
= 64,
432 .maxPerStageDescriptorInputAttachments
= 64,
433 .maxPerStageResources
= 128,
434 .maxDescriptorSetSamplers
= 256,
435 .maxDescriptorSetUniformBuffers
= 256,
436 .maxDescriptorSetUniformBuffersDynamic
= 256,
437 .maxDescriptorSetStorageBuffers
= 256,
438 .maxDescriptorSetStorageBuffersDynamic
= 256,
439 .maxDescriptorSetSampledImages
= 256,
440 .maxDescriptorSetStorageImages
= 256,
441 .maxDescriptorSetInputAttachments
= 256,
442 .maxVertexInputAttributes
= 32,
443 .maxVertexInputBindings
= 32,
444 .maxVertexInputAttributeOffset
= 2047,
445 .maxVertexInputBindingStride
= 2048,
446 .maxVertexOutputComponents
= 128,
447 .maxTessellationGenerationLevel
= 0,
448 .maxTessellationPatchSize
= 0,
449 .maxTessellationControlPerVertexInputComponents
= 0,
450 .maxTessellationControlPerVertexOutputComponents
= 0,
451 .maxTessellationControlPerPatchOutputComponents
= 0,
452 .maxTessellationControlTotalOutputComponents
= 0,
453 .maxTessellationEvaluationInputComponents
= 0,
454 .maxTessellationEvaluationOutputComponents
= 0,
455 .maxGeometryShaderInvocations
= 32,
456 .maxGeometryInputComponents
= 64,
457 .maxGeometryOutputComponents
= 128,
458 .maxGeometryOutputVertices
= 256,
459 .maxGeometryTotalOutputComponents
= 1024,
460 .maxFragmentInputComponents
= 128,
461 .maxFragmentOutputAttachments
= 8,
462 .maxFragmentDualSrcAttachments
= 2,
463 .maxFragmentCombinedOutputResources
= 8,
464 .maxComputeSharedMemorySize
= 32768,
465 .maxComputeWorkGroupCount
= { 65535, 65535, 65535 },
466 .maxComputeWorkGroupInvocations
= 16 * devinfo
->max_cs_threads
,
467 .maxComputeWorkGroupSize
= {
468 16 * devinfo
->max_cs_threads
,
469 16 * devinfo
->max_cs_threads
,
470 16 * devinfo
->max_cs_threads
,
472 .subPixelPrecisionBits
= 4 /* FIXME */,
473 .subTexelPrecisionBits
= 4 /* FIXME */,
474 .mipmapPrecisionBits
= 4 /* FIXME */,
475 .maxDrawIndexedIndexValue
= UINT32_MAX
,
476 .maxDrawIndirectCount
= UINT32_MAX
,
477 .maxSamplerLodBias
= 16,
478 .maxSamplerAnisotropy
= 16,
479 .maxViewports
= MAX_VIEWPORTS
,
480 .maxViewportDimensions
= { (1 << 14), (1 << 14) },
481 .viewportBoundsRange
= { -16384.0, 16384.0 },
482 .viewportSubPixelBits
= 13, /* We take a float? */
483 .minMemoryMapAlignment
= 4096, /* A page */
484 .minTexelBufferOffsetAlignment
= 1,
485 .minUniformBufferOffsetAlignment
= 1,
486 .minStorageBufferOffsetAlignment
= 1,
487 .minTexelOffset
= -8,
489 .minTexelGatherOffset
= -8,
490 .maxTexelGatherOffset
= 7,
491 .minInterpolationOffset
= 0, /* FIXME */
492 .maxInterpolationOffset
= 0, /* FIXME */
493 .subPixelInterpolationOffsetBits
= 0, /* FIXME */
494 .maxFramebufferWidth
= (1 << 14),
495 .maxFramebufferHeight
= (1 << 14),
496 .maxFramebufferLayers
= (1 << 10),
497 .framebufferColorSampleCounts
= sample_counts
,
498 .framebufferDepthSampleCounts
= sample_counts
,
499 .framebufferStencilSampleCounts
= sample_counts
,
500 .framebufferNoAttachmentsSampleCounts
= sample_counts
,
501 .maxColorAttachments
= MAX_RTS
,
502 .sampledImageColorSampleCounts
= sample_counts
,
503 .sampledImageIntegerSampleCounts
= VK_SAMPLE_COUNT_1_BIT
,
504 .sampledImageDepthSampleCounts
= sample_counts
,
505 .sampledImageStencilSampleCounts
= sample_counts
,
506 .storageImageSampleCounts
= VK_SAMPLE_COUNT_1_BIT
,
507 .maxSampleMaskWords
= 1,
508 .timestampComputeAndGraphics
= false,
509 .timestampPeriod
= time_stamp_base
/ (1000 * 1000 * 1000),
510 .maxClipDistances
= 0 /* FIXME */,
511 .maxCullDistances
= 0 /* FIXME */,
512 .maxCombinedClipAndCullDistances
= 0 /* FIXME */,
513 .discreteQueuePriorities
= 1,
514 .pointSizeRange
= { 0.125, 255.875 },
515 .lineWidthRange
= { 0.0, 7.9921875 },
516 .pointSizeGranularity
= (1.0 / 8.0),
517 .lineWidthGranularity
= (1.0 / 128.0),
518 .strictLines
= false, /* FINISHME */
519 .standardSampleLocations
= true,
520 .optimalBufferCopyOffsetAlignment
= 128,
521 .optimalBufferCopyRowPitchAlignment
= 128,
522 .nonCoherentAtomSize
= 64,
525 *pProperties
= (VkPhysicalDeviceProperties
) {
526 .apiVersion
= VK_MAKE_VERSION(1, 0, 2),
529 .deviceID
= pdevice
->chipset_id
,
530 .deviceType
= VK_PHYSICAL_DEVICE_TYPE_INTEGRATED_GPU
,
532 .sparseProperties
= {0}, /* Broadwell doesn't do sparse. */
535 strcpy(pProperties
->deviceName
, pdevice
->name
);
536 anv_device_get_cache_uuid(pProperties
->pipelineCacheUUID
);
539 void anv_GetPhysicalDeviceQueueFamilyProperties(
540 VkPhysicalDevice physicalDevice
,
542 VkQueueFamilyProperties
* pQueueFamilyProperties
)
544 if (pQueueFamilyProperties
== NULL
) {
549 assert(*pCount
>= 1);
551 *pQueueFamilyProperties
= (VkQueueFamilyProperties
) {
552 .queueFlags
= VK_QUEUE_GRAPHICS_BIT
|
553 VK_QUEUE_COMPUTE_BIT
|
554 VK_QUEUE_TRANSFER_BIT
,
556 .timestampValidBits
= 36, /* XXX: Real value here */
557 .minImageTransferGranularity
= (VkExtent3D
) { 1, 1, 1 },
561 void anv_GetPhysicalDeviceMemoryProperties(
562 VkPhysicalDevice physicalDevice
,
563 VkPhysicalDeviceMemoryProperties
* pMemoryProperties
)
565 ANV_FROM_HANDLE(anv_physical_device
, physical_device
, physicalDevice
);
566 VkDeviceSize heap_size
;
568 /* Reserve some wiggle room for the driver by exposing only 75% of the
569 * aperture to the heap.
571 heap_size
= 3 * physical_device
->aperture_size
/ 4;
573 if (physical_device
->info
->has_llc
) {
574 /* Big core GPUs share LLC with the CPU and thus one memory type can be
575 * both cached and coherent at the same time.
577 pMemoryProperties
->memoryTypeCount
= 1;
578 pMemoryProperties
->memoryTypes
[0] = (VkMemoryType
) {
579 .propertyFlags
= VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT
|
580 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT
|
581 VK_MEMORY_PROPERTY_HOST_COHERENT_BIT
|
582 VK_MEMORY_PROPERTY_HOST_CACHED_BIT
,
586 /* The spec requires that we expose a host-visible, coherent memory
587 * type, but Atom GPUs don't share LLC. Thus we offer two memory types
588 * to give the application a choice between cached, but not coherent and
589 * coherent but uncached (WC though).
591 pMemoryProperties
->memoryTypeCount
= 2;
592 pMemoryProperties
->memoryTypes
[0] = (VkMemoryType
) {
593 .propertyFlags
= VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT
|
594 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT
|
595 VK_MEMORY_PROPERTY_HOST_COHERENT_BIT
,
598 pMemoryProperties
->memoryTypes
[1] = (VkMemoryType
) {
599 .propertyFlags
= VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT
|
600 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT
|
601 VK_MEMORY_PROPERTY_HOST_CACHED_BIT
,
606 pMemoryProperties
->memoryHeapCount
= 1;
607 pMemoryProperties
->memoryHeaps
[0] = (VkMemoryHeap
) {
609 .flags
= VK_MEMORY_HEAP_DEVICE_LOCAL_BIT
,
613 PFN_vkVoidFunction
anv_GetInstanceProcAddr(
617 return anv_lookup_entrypoint(pName
);
620 /* The loader wants us to expose a second GetInstanceProcAddr function
621 * to work around certain LD_PRELOAD issues seen in apps.
623 VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL
vk_icdGetInstanceProcAddr(
627 VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL
vk_icdGetInstanceProcAddr(
631 return anv_GetInstanceProcAddr(instance
, pName
);
634 PFN_vkVoidFunction
anv_GetDeviceProcAddr(
638 return anv_lookup_entrypoint(pName
);
642 anv_queue_init(struct anv_device
*device
, struct anv_queue
*queue
)
644 queue
->_loader_data
.loaderMagic
= ICD_LOADER_MAGIC
;
645 queue
->device
= device
;
646 queue
->pool
= &device
->surface_state_pool
;
652 anv_queue_finish(struct anv_queue
*queue
)
656 static struct anv_state
657 anv_state_pool_emit_data(struct anv_state_pool
*pool
, size_t size
, size_t align
, const void *p
)
659 struct anv_state state
;
661 state
= anv_state_pool_alloc(pool
, size
, align
);
662 memcpy(state
.map
, p
, size
);
664 if (!pool
->block_pool
->device
->info
.has_llc
)
665 anv_state_clflush(state
);
670 struct gen8_border_color
{
675 /* Pad out to 64 bytes */
680 anv_device_init_border_colors(struct anv_device
*device
)
682 static const struct gen8_border_color border_colors
[] = {
683 [VK_BORDER_COLOR_FLOAT_TRANSPARENT_BLACK
] = { .float32
= { 0.0, 0.0, 0.0, 0.0 } },
684 [VK_BORDER_COLOR_FLOAT_OPAQUE_BLACK
] = { .float32
= { 0.0, 0.0, 0.0, 1.0 } },
685 [VK_BORDER_COLOR_FLOAT_OPAQUE_WHITE
] = { .float32
= { 1.0, 1.0, 1.0, 1.0 } },
686 [VK_BORDER_COLOR_INT_TRANSPARENT_BLACK
] = { .uint32
= { 0, 0, 0, 0 } },
687 [VK_BORDER_COLOR_INT_OPAQUE_BLACK
] = { .uint32
= { 0, 0, 0, 1 } },
688 [VK_BORDER_COLOR_INT_OPAQUE_WHITE
] = { .uint32
= { 1, 1, 1, 1 } },
691 device
->border_colors
= anv_state_pool_emit_data(&device
->dynamic_state_pool
,
692 sizeof(border_colors
), 64,
697 anv_device_submit_simple_batch(struct anv_device
*device
,
698 struct anv_batch
*batch
)
700 struct drm_i915_gem_execbuffer2 execbuf
;
701 struct drm_i915_gem_exec_object2 exec2_objects
[1];
703 VkResult result
= VK_SUCCESS
;
708 /* Kernel driver requires 8 byte aligned batch length */
709 size
= align_u32(batch
->next
- batch
->start
, 8);
710 assert(size
< device
->batch_bo_pool
.bo_size
);
711 result
= anv_bo_pool_alloc(&device
->batch_bo_pool
, &bo
);
712 if (result
!= VK_SUCCESS
)
715 memcpy(bo
.map
, batch
->start
, size
);
716 if (!device
->info
.has_llc
)
717 anv_clflush_range(bo
.map
, size
);
719 exec2_objects
[0].handle
= bo
.gem_handle
;
720 exec2_objects
[0].relocation_count
= 0;
721 exec2_objects
[0].relocs_ptr
= 0;
722 exec2_objects
[0].alignment
= 0;
723 exec2_objects
[0].offset
= bo
.offset
;
724 exec2_objects
[0].flags
= 0;
725 exec2_objects
[0].rsvd1
= 0;
726 exec2_objects
[0].rsvd2
= 0;
728 execbuf
.buffers_ptr
= (uintptr_t) exec2_objects
;
729 execbuf
.buffer_count
= 1;
730 execbuf
.batch_start_offset
= 0;
731 execbuf
.batch_len
= size
;
732 execbuf
.cliprects_ptr
= 0;
733 execbuf
.num_cliprects
= 0;
738 I915_EXEC_HANDLE_LUT
| I915_EXEC_NO_RELOC
| I915_EXEC_RENDER
;
739 execbuf
.rsvd1
= device
->context_id
;
742 ret
= anv_gem_execbuffer(device
, &execbuf
);
744 /* We don't know the real error. */
745 result
= vk_errorf(VK_ERROR_OUT_OF_DEVICE_MEMORY
, "execbuf2 failed: %m");
750 ret
= anv_gem_wait(device
, bo
.gem_handle
, &timeout
);
752 /* We don't know the real error. */
753 result
= vk_errorf(VK_ERROR_OUT_OF_DEVICE_MEMORY
, "execbuf2 failed: %m");
758 anv_bo_pool_free(&device
->batch_bo_pool
, &bo
);
763 VkResult
anv_CreateDevice(
764 VkPhysicalDevice physicalDevice
,
765 const VkDeviceCreateInfo
* pCreateInfo
,
766 const VkAllocationCallbacks
* pAllocator
,
769 ANV_FROM_HANDLE(anv_physical_device
, physical_device
, physicalDevice
);
771 struct anv_device
*device
;
773 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO
);
775 for (uint32_t i
= 0; i
< pCreateInfo
->enabledExtensionCount
; i
++) {
777 for (uint32_t j
= 0; j
< ARRAY_SIZE(device_extensions
); j
++) {
778 if (strcmp(pCreateInfo
->ppEnabledExtensionNames
[i
],
779 device_extensions
[j
].extensionName
) == 0) {
785 return vk_error(VK_ERROR_EXTENSION_NOT_PRESENT
);
788 anv_set_dispatch_devinfo(physical_device
->info
);
790 device
= anv_alloc2(&physical_device
->instance
->alloc
, pAllocator
,
792 VK_SYSTEM_ALLOCATION_SCOPE_DEVICE
);
794 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
796 device
->_loader_data
.loaderMagic
= ICD_LOADER_MAGIC
;
797 device
->instance
= physical_device
->instance
;
798 device
->chipset_id
= physical_device
->chipset_id
;
801 device
->alloc
= *pAllocator
;
803 device
->alloc
= physical_device
->instance
->alloc
;
805 /* XXX(chadv): Can we dup() physicalDevice->fd here? */
806 device
->fd
= open(physical_device
->path
, O_RDWR
| O_CLOEXEC
);
807 if (device
->fd
== -1) {
808 result
= vk_error(VK_ERROR_INITIALIZATION_FAILED
);
812 device
->context_id
= anv_gem_create_context(device
);
813 if (device
->context_id
== -1) {
814 result
= vk_error(VK_ERROR_INITIALIZATION_FAILED
);
818 device
->info
= *physical_device
->info
;
819 device
->isl_dev
= physical_device
->isl_dev
;
821 pthread_mutex_init(&device
->mutex
, NULL
);
823 anv_bo_pool_init(&device
->batch_bo_pool
, device
, ANV_CMD_BUFFER_BATCH_SIZE
);
825 anv_block_pool_init(&device
->dynamic_state_block_pool
, device
, 16384);
827 anv_state_pool_init(&device
->dynamic_state_pool
,
828 &device
->dynamic_state_block_pool
);
830 anv_block_pool_init(&device
->instruction_block_pool
, device
, 128 * 1024);
831 anv_pipeline_cache_init(&device
->default_pipeline_cache
, device
);
833 anv_block_pool_init(&device
->surface_state_block_pool
, device
, 4096);
835 anv_state_pool_init(&device
->surface_state_pool
,
836 &device
->surface_state_block_pool
);
838 anv_bo_init_new(&device
->workaround_bo
, device
, 1024);
840 anv_block_pool_init(&device
->scratch_block_pool
, device
, 0x10000);
842 anv_queue_init(device
, &device
->queue
);
844 switch (device
->info
.gen
) {
846 if (!device
->info
.is_haswell
)
847 result
= gen7_init_device_state(device
);
849 result
= gen75_init_device_state(device
);
852 result
= gen8_init_device_state(device
);
855 result
= gen9_init_device_state(device
);
858 /* Shouldn't get here as we don't create physical devices for any other
860 unreachable("unhandled gen");
862 if (result
!= VK_SUCCESS
)
865 result
= anv_device_init_meta(device
);
866 if (result
!= VK_SUCCESS
)
869 anv_device_init_border_colors(device
);
871 *pDevice
= anv_device_to_handle(device
);
878 anv_free(&device
->alloc
, device
);
883 void anv_DestroyDevice(
885 const VkAllocationCallbacks
* pAllocator
)
887 ANV_FROM_HANDLE(anv_device
, device
, _device
);
889 anv_queue_finish(&device
->queue
);
891 anv_device_finish_meta(device
);
894 /* We only need to free these to prevent valgrind errors. The backing
895 * BO will go away in a couple of lines so we don't actually leak.
897 anv_state_pool_free(&device
->dynamic_state_pool
, device
->border_colors
);
900 anv_gem_munmap(device
->workaround_bo
.map
, device
->workaround_bo
.size
);
901 anv_gem_close(device
, device
->workaround_bo
.gem_handle
);
903 anv_bo_pool_finish(&device
->batch_bo_pool
);
904 anv_state_pool_finish(&device
->dynamic_state_pool
);
905 anv_block_pool_finish(&device
->dynamic_state_block_pool
);
906 anv_block_pool_finish(&device
->instruction_block_pool
);
907 anv_state_pool_finish(&device
->surface_state_pool
);
908 anv_block_pool_finish(&device
->surface_state_block_pool
);
909 anv_block_pool_finish(&device
->scratch_block_pool
);
913 pthread_mutex_destroy(&device
->mutex
);
915 anv_free(&device
->alloc
, device
);
918 VkResult
anv_EnumerateInstanceExtensionProperties(
919 const char* pLayerName
,
920 uint32_t* pPropertyCount
,
921 VkExtensionProperties
* pProperties
)
923 if (pProperties
== NULL
) {
924 *pPropertyCount
= ARRAY_SIZE(global_extensions
);
928 assert(*pPropertyCount
>= ARRAY_SIZE(global_extensions
));
930 *pPropertyCount
= ARRAY_SIZE(global_extensions
);
931 memcpy(pProperties
, global_extensions
, sizeof(global_extensions
));
936 VkResult
anv_EnumerateDeviceExtensionProperties(
937 VkPhysicalDevice physicalDevice
,
938 const char* pLayerName
,
939 uint32_t* pPropertyCount
,
940 VkExtensionProperties
* pProperties
)
942 if (pProperties
== NULL
) {
943 *pPropertyCount
= ARRAY_SIZE(device_extensions
);
947 assert(*pPropertyCount
>= ARRAY_SIZE(device_extensions
));
949 *pPropertyCount
= ARRAY_SIZE(device_extensions
);
950 memcpy(pProperties
, device_extensions
, sizeof(device_extensions
));
955 VkResult
anv_EnumerateInstanceLayerProperties(
956 uint32_t* pPropertyCount
,
957 VkLayerProperties
* pProperties
)
959 if (pProperties
== NULL
) {
964 /* None supported at this time */
965 return vk_error(VK_ERROR_LAYER_NOT_PRESENT
);
968 VkResult
anv_EnumerateDeviceLayerProperties(
969 VkPhysicalDevice physicalDevice
,
970 uint32_t* pPropertyCount
,
971 VkLayerProperties
* pProperties
)
973 if (pProperties
== NULL
) {
978 /* None supported at this time */
979 return vk_error(VK_ERROR_LAYER_NOT_PRESENT
);
982 void anv_GetDeviceQueue(
984 uint32_t queueNodeIndex
,
988 ANV_FROM_HANDLE(anv_device
, device
, _device
);
990 assert(queueIndex
== 0);
992 *pQueue
= anv_queue_to_handle(&device
->queue
);
995 VkResult
anv_QueueSubmit(
997 uint32_t submitCount
,
998 const VkSubmitInfo
* pSubmits
,
1001 ANV_FROM_HANDLE(anv_queue
, queue
, _queue
);
1002 ANV_FROM_HANDLE(anv_fence
, fence
, _fence
);
1003 struct anv_device
*device
= queue
->device
;
1006 for (uint32_t i
= 0; i
< submitCount
; i
++) {
1007 for (uint32_t j
= 0; j
< pSubmits
[i
].commandBufferCount
; j
++) {
1008 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
,
1009 pSubmits
[i
].pCommandBuffers
[j
]);
1010 assert(cmd_buffer
->level
== VK_COMMAND_BUFFER_LEVEL_PRIMARY
);
1012 ret
= anv_gem_execbuffer(device
, &cmd_buffer
->execbuf2
.execbuf
);
1014 /* We don't know the real error. */
1015 return vk_errorf(VK_ERROR_OUT_OF_DEVICE_MEMORY
,
1016 "execbuf2 failed: %m");
1019 for (uint32_t k
= 0; k
< cmd_buffer
->execbuf2
.bo_count
; k
++)
1020 cmd_buffer
->execbuf2
.bos
[k
]->offset
= cmd_buffer
->execbuf2
.objects
[k
].offset
;
1025 ret
= anv_gem_execbuffer(device
, &fence
->execbuf
);
1027 /* We don't know the real error. */
1028 return vk_errorf(VK_ERROR_OUT_OF_DEVICE_MEMORY
,
1029 "execbuf2 failed: %m");
1036 VkResult
anv_QueueWaitIdle(
1039 ANV_FROM_HANDLE(anv_queue
, queue
, _queue
);
1041 return ANV_CALL(DeviceWaitIdle
)(anv_device_to_handle(queue
->device
));
1044 VkResult
anv_DeviceWaitIdle(
1047 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1048 struct anv_batch batch
;
1051 batch
.start
= batch
.next
= cmds
;
1052 batch
.end
= (void *) cmds
+ sizeof(cmds
);
1054 anv_batch_emit(&batch
, GEN7_MI_BATCH_BUFFER_END
);
1055 anv_batch_emit(&batch
, GEN7_MI_NOOP
);
1057 return anv_device_submit_simple_batch(device
, &batch
);
1061 anv_bo_init_new(struct anv_bo
*bo
, struct anv_device
*device
, uint64_t size
)
1063 bo
->gem_handle
= anv_gem_create(device
, size
);
1064 if (!bo
->gem_handle
)
1065 return vk_error(VK_ERROR_OUT_OF_DEVICE_MEMORY
);
1075 VkResult
anv_AllocateMemory(
1077 const VkMemoryAllocateInfo
* pAllocateInfo
,
1078 const VkAllocationCallbacks
* pAllocator
,
1079 VkDeviceMemory
* pMem
)
1081 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1082 struct anv_device_memory
*mem
;
1085 assert(pAllocateInfo
->sType
== VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO
);
1087 if (pAllocateInfo
->allocationSize
== 0) {
1088 /* Apparently, this is allowed */
1089 *pMem
= VK_NULL_HANDLE
;
1093 /* We support exactly one memory heap. */
1094 assert(pAllocateInfo
->memoryTypeIndex
== 0 ||
1095 (!device
->info
.has_llc
&& pAllocateInfo
->memoryTypeIndex
< 2));
1097 /* FINISHME: Fail if allocation request exceeds heap size. */
1099 mem
= anv_alloc2(&device
->alloc
, pAllocator
, sizeof(*mem
), 8,
1100 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
1102 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
1104 /* The kernel is going to give us whole pages anyway */
1105 uint64_t alloc_size
= align_u64(pAllocateInfo
->allocationSize
, 4096);
1107 result
= anv_bo_init_new(&mem
->bo
, device
, alloc_size
);
1108 if (result
!= VK_SUCCESS
)
1111 mem
->type_index
= pAllocateInfo
->memoryTypeIndex
;
1113 *pMem
= anv_device_memory_to_handle(mem
);
1118 anv_free2(&device
->alloc
, pAllocator
, mem
);
1123 void anv_FreeMemory(
1125 VkDeviceMemory _mem
,
1126 const VkAllocationCallbacks
* pAllocator
)
1128 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1129 ANV_FROM_HANDLE(anv_device_memory
, mem
, _mem
);
1135 anv_gem_munmap(mem
->bo
.map
, mem
->bo
.size
);
1137 if (mem
->bo
.gem_handle
!= 0)
1138 anv_gem_close(device
, mem
->bo
.gem_handle
);
1140 anv_free2(&device
->alloc
, pAllocator
, mem
);
1143 VkResult
anv_MapMemory(
1145 VkDeviceMemory _memory
,
1146 VkDeviceSize offset
,
1148 VkMemoryMapFlags flags
,
1151 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1152 ANV_FROM_HANDLE(anv_device_memory
, mem
, _memory
);
1159 if (size
== VK_WHOLE_SIZE
)
1160 size
= mem
->bo
.size
- offset
;
1162 /* FIXME: Is this supposed to be thread safe? Since vkUnmapMemory() only
1163 * takes a VkDeviceMemory pointer, it seems like only one map of the memory
1164 * at a time is valid. We could just mmap up front and return an offset
1165 * pointer here, but that may exhaust virtual memory on 32 bit
1168 uint32_t gem_flags
= 0;
1169 if (!device
->info
.has_llc
&& mem
->type_index
== 0)
1170 gem_flags
|= I915_MMAP_WC
;
1172 /* GEM will fail to map if the offset isn't 4k-aligned. Round down. */
1173 uint64_t map_offset
= offset
& ~4095ull;
1174 assert(offset
>= map_offset
);
1175 uint64_t map_size
= (offset
+ size
) - map_offset
;
1177 /* Let's map whole pages */
1178 map_size
= align_u64(map_size
, 4096);
1180 mem
->map
= anv_gem_mmap(device
, mem
->bo
.gem_handle
,
1181 map_offset
, map_size
, gem_flags
);
1182 mem
->map_size
= map_size
;
1184 *ppData
= mem
->map
+ (offset
- map_offset
);
1189 void anv_UnmapMemory(
1191 VkDeviceMemory _memory
)
1193 ANV_FROM_HANDLE(anv_device_memory
, mem
, _memory
);
1198 anv_gem_munmap(mem
->map
, mem
->map_size
);
1202 clflush_mapped_ranges(struct anv_device
*device
,
1204 const VkMappedMemoryRange
*ranges
)
1206 for (uint32_t i
= 0; i
< count
; i
++) {
1207 ANV_FROM_HANDLE(anv_device_memory
, mem
, ranges
[i
].memory
);
1208 void *p
= mem
->map
+ (ranges
[i
].offset
& ~CACHELINE_MASK
);
1211 if (ranges
[i
].offset
+ ranges
[i
].size
> mem
->map_size
)
1212 end
= mem
->map
+ mem
->map_size
;
1214 end
= mem
->map
+ ranges
[i
].offset
+ ranges
[i
].size
;
1217 __builtin_ia32_clflush(p
);
1218 p
+= CACHELINE_SIZE
;
1223 VkResult
anv_FlushMappedMemoryRanges(
1225 uint32_t memoryRangeCount
,
1226 const VkMappedMemoryRange
* pMemoryRanges
)
1228 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1230 if (device
->info
.has_llc
)
1233 /* Make sure the writes we're flushing have landed. */
1234 __builtin_ia32_mfence();
1236 clflush_mapped_ranges(device
, memoryRangeCount
, pMemoryRanges
);
1241 VkResult
anv_InvalidateMappedMemoryRanges(
1243 uint32_t memoryRangeCount
,
1244 const VkMappedMemoryRange
* pMemoryRanges
)
1246 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1248 if (device
->info
.has_llc
)
1251 clflush_mapped_ranges(device
, memoryRangeCount
, pMemoryRanges
);
1253 /* Make sure no reads get moved up above the invalidate. */
1254 __builtin_ia32_mfence();
1259 void anv_GetBufferMemoryRequirements(
1262 VkMemoryRequirements
* pMemoryRequirements
)
1264 ANV_FROM_HANDLE(anv_buffer
, buffer
, _buffer
);
1266 /* The Vulkan spec (git aaed022) says:
1268 * memoryTypeBits is a bitfield and contains one bit set for every
1269 * supported memory type for the resource. The bit `1<<i` is set if and
1270 * only if the memory type `i` in the VkPhysicalDeviceMemoryProperties
1271 * structure for the physical device is supported.
1273 * We support exactly one memory type.
1275 pMemoryRequirements
->memoryTypeBits
= 1;
1277 pMemoryRequirements
->size
= buffer
->size
;
1278 pMemoryRequirements
->alignment
= 16;
1281 void anv_GetImageMemoryRequirements(
1284 VkMemoryRequirements
* pMemoryRequirements
)
1286 ANV_FROM_HANDLE(anv_image
, image
, _image
);
1288 /* The Vulkan spec (git aaed022) says:
1290 * memoryTypeBits is a bitfield and contains one bit set for every
1291 * supported memory type for the resource. The bit `1<<i` is set if and
1292 * only if the memory type `i` in the VkPhysicalDeviceMemoryProperties
1293 * structure for the physical device is supported.
1295 * We support exactly one memory type.
1297 pMemoryRequirements
->memoryTypeBits
= 1;
1299 pMemoryRequirements
->size
= image
->size
;
1300 pMemoryRequirements
->alignment
= image
->alignment
;
1303 void anv_GetImageSparseMemoryRequirements(
1306 uint32_t* pSparseMemoryRequirementCount
,
1307 VkSparseImageMemoryRequirements
* pSparseMemoryRequirements
)
1312 void anv_GetDeviceMemoryCommitment(
1314 VkDeviceMemory memory
,
1315 VkDeviceSize
* pCommittedMemoryInBytes
)
1317 *pCommittedMemoryInBytes
= 0;
1320 VkResult
anv_BindBufferMemory(
1323 VkDeviceMemory _memory
,
1324 VkDeviceSize memoryOffset
)
1326 ANV_FROM_HANDLE(anv_device_memory
, mem
, _memory
);
1327 ANV_FROM_HANDLE(anv_buffer
, buffer
, _buffer
);
1330 buffer
->bo
= &mem
->bo
;
1331 buffer
->offset
= memoryOffset
;
1340 VkResult
anv_BindImageMemory(
1343 VkDeviceMemory _memory
,
1344 VkDeviceSize memoryOffset
)
1346 ANV_FROM_HANDLE(anv_device_memory
, mem
, _memory
);
1347 ANV_FROM_HANDLE(anv_image
, image
, _image
);
1350 image
->bo
= &mem
->bo
;
1351 image
->offset
= memoryOffset
;
1360 VkResult
anv_QueueBindSparse(
1362 uint32_t bindInfoCount
,
1363 const VkBindSparseInfo
* pBindInfo
,
1366 stub_return(VK_ERROR_INCOMPATIBLE_DRIVER
);
1369 VkResult
anv_CreateFence(
1371 const VkFenceCreateInfo
* pCreateInfo
,
1372 const VkAllocationCallbacks
* pAllocator
,
1375 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1376 struct anv_fence
*fence
;
1377 struct anv_batch batch
;
1380 const uint32_t fence_size
= 128;
1382 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_FENCE_CREATE_INFO
);
1384 fence
= anv_alloc2(&device
->alloc
, pAllocator
, sizeof(*fence
), 8,
1385 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
1387 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
1389 result
= anv_bo_init_new(&fence
->bo
, device
, fence_size
);
1390 if (result
!= VK_SUCCESS
)
1394 anv_gem_mmap(device
, fence
->bo
.gem_handle
, 0, fence
->bo
.size
, 0);
1395 batch
.next
= batch
.start
= fence
->bo
.map
;
1396 batch
.end
= fence
->bo
.map
+ fence
->bo
.size
;
1397 anv_batch_emit(&batch
, GEN7_MI_BATCH_BUFFER_END
);
1398 anv_batch_emit(&batch
, GEN7_MI_NOOP
);
1400 if (!device
->info
.has_llc
) {
1401 assert(((uintptr_t) fence
->bo
.map
& CACHELINE_MASK
) == 0);
1402 assert(batch
.next
- fence
->bo
.map
<= CACHELINE_SIZE
);
1403 __builtin_ia32_mfence();
1404 __builtin_ia32_clflush(fence
->bo
.map
);
1407 fence
->exec2_objects
[0].handle
= fence
->bo
.gem_handle
;
1408 fence
->exec2_objects
[0].relocation_count
= 0;
1409 fence
->exec2_objects
[0].relocs_ptr
= 0;
1410 fence
->exec2_objects
[0].alignment
= 0;
1411 fence
->exec2_objects
[0].offset
= fence
->bo
.offset
;
1412 fence
->exec2_objects
[0].flags
= 0;
1413 fence
->exec2_objects
[0].rsvd1
= 0;
1414 fence
->exec2_objects
[0].rsvd2
= 0;
1416 fence
->execbuf
.buffers_ptr
= (uintptr_t) fence
->exec2_objects
;
1417 fence
->execbuf
.buffer_count
= 1;
1418 fence
->execbuf
.batch_start_offset
= 0;
1419 fence
->execbuf
.batch_len
= batch
.next
- fence
->bo
.map
;
1420 fence
->execbuf
.cliprects_ptr
= 0;
1421 fence
->execbuf
.num_cliprects
= 0;
1422 fence
->execbuf
.DR1
= 0;
1423 fence
->execbuf
.DR4
= 0;
1425 fence
->execbuf
.flags
=
1426 I915_EXEC_HANDLE_LUT
| I915_EXEC_NO_RELOC
| I915_EXEC_RENDER
;
1427 fence
->execbuf
.rsvd1
= device
->context_id
;
1428 fence
->execbuf
.rsvd2
= 0;
1430 fence
->ready
= false;
1432 *pFence
= anv_fence_to_handle(fence
);
1437 anv_free2(&device
->alloc
, pAllocator
, fence
);
1442 void anv_DestroyFence(
1445 const VkAllocationCallbacks
* pAllocator
)
1447 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1448 ANV_FROM_HANDLE(anv_fence
, fence
, _fence
);
1450 anv_gem_munmap(fence
->bo
.map
, fence
->bo
.size
);
1451 anv_gem_close(device
, fence
->bo
.gem_handle
);
1452 anv_free2(&device
->alloc
, pAllocator
, fence
);
1455 VkResult
anv_ResetFences(
1457 uint32_t fenceCount
,
1458 const VkFence
* pFences
)
1460 for (uint32_t i
= 0; i
< fenceCount
; i
++) {
1461 ANV_FROM_HANDLE(anv_fence
, fence
, pFences
[i
]);
1462 fence
->ready
= false;
1468 VkResult
anv_GetFenceStatus(
1472 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1473 ANV_FROM_HANDLE(anv_fence
, fence
, _fence
);
1480 ret
= anv_gem_wait(device
, fence
->bo
.gem_handle
, &t
);
1482 fence
->ready
= true;
1486 return VK_NOT_READY
;
1489 VkResult
anv_WaitForFences(
1491 uint32_t fenceCount
,
1492 const VkFence
* pFences
,
1496 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1498 /* DRM_IOCTL_I915_GEM_WAIT uses a signed 64 bit timeout and is supposed
1499 * to block indefinitely timeouts <= 0. Unfortunately, this was broken
1500 * for a couple of kernel releases. Since there's no way to know
1501 * whether or not the kernel we're using is one of the broken ones, the
1502 * best we can do is to clamp the timeout to INT64_MAX. This limits the
1503 * maximum timeout from 584 years to 292 years - likely not a big deal.
1505 if (timeout
> INT64_MAX
)
1506 timeout
= INT64_MAX
;
1508 int64_t t
= timeout
;
1510 /* FIXME: handle !waitAll */
1512 for (uint32_t i
= 0; i
< fenceCount
; i
++) {
1513 ANV_FROM_HANDLE(anv_fence
, fence
, pFences
[i
]);
1514 int ret
= anv_gem_wait(device
, fence
->bo
.gem_handle
, &t
);
1515 if (ret
== -1 && errno
== ETIME
) {
1517 } else if (ret
== -1) {
1518 /* We don't know the real error. */
1519 return vk_errorf(VK_ERROR_OUT_OF_DEVICE_MEMORY
,
1520 "gem wait failed: %m");
1527 // Queue semaphore functions
1529 VkResult
anv_CreateSemaphore(
1531 const VkSemaphoreCreateInfo
* pCreateInfo
,
1532 const VkAllocationCallbacks
* pAllocator
,
1533 VkSemaphore
* pSemaphore
)
1535 /* The DRM execbuffer ioctl always execute in-oder, even between different
1536 * rings. As such, there's nothing to do for the user space semaphore.
1539 *pSemaphore
= (VkSemaphore
)1;
1544 void anv_DestroySemaphore(
1546 VkSemaphore semaphore
,
1547 const VkAllocationCallbacks
* pAllocator
)
1553 VkResult
anv_CreateEvent(
1555 const VkEventCreateInfo
* pCreateInfo
,
1556 const VkAllocationCallbacks
* pAllocator
,
1559 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1560 struct anv_state state
;
1561 struct anv_event
*event
;
1563 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_EVENT_CREATE_INFO
);
1565 state
= anv_state_pool_alloc(&device
->dynamic_state_pool
,
1568 event
->state
= state
;
1569 event
->semaphore
= VK_EVENT_RESET
;
1571 if (!device
->info
.has_llc
) {
1572 /* Make sure the writes we're flushing have landed. */
1573 __builtin_ia32_mfence();
1574 __builtin_ia32_clflush(event
);
1577 *pEvent
= anv_event_to_handle(event
);
1582 void anv_DestroyEvent(
1585 const VkAllocationCallbacks
* pAllocator
)
1587 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1588 ANV_FROM_HANDLE(anv_event
, event
, _event
);
1590 anv_state_pool_free(&device
->dynamic_state_pool
, event
->state
);
1593 VkResult
anv_GetEventStatus(
1597 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1598 ANV_FROM_HANDLE(anv_event
, event
, _event
);
1600 if (!device
->info
.has_llc
) {
1601 /* Invalidate read cache before reading event written by GPU. */
1602 __builtin_ia32_clflush(event
);
1603 __builtin_ia32_mfence();
1607 return event
->semaphore
;
1610 VkResult
anv_SetEvent(
1614 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1615 ANV_FROM_HANDLE(anv_event
, event
, _event
);
1617 event
->semaphore
= VK_EVENT_SET
;
1619 if (!device
->info
.has_llc
) {
1620 /* Make sure the writes we're flushing have landed. */
1621 __builtin_ia32_mfence();
1622 __builtin_ia32_clflush(event
);
1628 VkResult
anv_ResetEvent(
1632 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1633 ANV_FROM_HANDLE(anv_event
, event
, _event
);
1635 event
->semaphore
= VK_EVENT_RESET
;
1637 if (!device
->info
.has_llc
) {
1638 /* Make sure the writes we're flushing have landed. */
1639 __builtin_ia32_mfence();
1640 __builtin_ia32_clflush(event
);
1648 VkResult
anv_CreateBuffer(
1650 const VkBufferCreateInfo
* pCreateInfo
,
1651 const VkAllocationCallbacks
* pAllocator
,
1654 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1655 struct anv_buffer
*buffer
;
1657 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO
);
1659 buffer
= anv_alloc2(&device
->alloc
, pAllocator
, sizeof(*buffer
), 8,
1660 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
1662 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
1664 buffer
->size
= pCreateInfo
->size
;
1665 buffer
->usage
= pCreateInfo
->usage
;
1669 *pBuffer
= anv_buffer_to_handle(buffer
);
1674 void anv_DestroyBuffer(
1677 const VkAllocationCallbacks
* pAllocator
)
1679 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1680 ANV_FROM_HANDLE(anv_buffer
, buffer
, _buffer
);
1682 anv_free2(&device
->alloc
, pAllocator
, buffer
);
1686 anv_fill_buffer_surface_state(struct anv_device
*device
, struct anv_state state
,
1687 enum isl_format format
,
1688 uint32_t offset
, uint32_t range
, uint32_t stride
)
1690 switch (device
->info
.gen
) {
1692 if (device
->info
.is_haswell
)
1693 gen75_fill_buffer_surface_state(state
.map
, format
, offset
, range
,
1696 gen7_fill_buffer_surface_state(state
.map
, format
, offset
, range
,
1700 gen8_fill_buffer_surface_state(state
.map
, format
, offset
, range
, stride
);
1703 gen9_fill_buffer_surface_state(state
.map
, format
, offset
, range
, stride
);
1706 unreachable("unsupported gen\n");
1709 if (!device
->info
.has_llc
)
1710 anv_state_clflush(state
);
1713 void anv_DestroySampler(
1716 const VkAllocationCallbacks
* pAllocator
)
1718 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1719 ANV_FROM_HANDLE(anv_sampler
, sampler
, _sampler
);
1721 anv_free2(&device
->alloc
, pAllocator
, sampler
);
1724 VkResult
anv_CreateFramebuffer(
1726 const VkFramebufferCreateInfo
* pCreateInfo
,
1727 const VkAllocationCallbacks
* pAllocator
,
1728 VkFramebuffer
* pFramebuffer
)
1730 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1731 struct anv_framebuffer
*framebuffer
;
1733 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO
);
1735 size_t size
= sizeof(*framebuffer
) +
1736 sizeof(struct anv_image_view
*) * pCreateInfo
->attachmentCount
;
1737 framebuffer
= anv_alloc2(&device
->alloc
, pAllocator
, size
, 8,
1738 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
1739 if (framebuffer
== NULL
)
1740 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
1742 framebuffer
->attachment_count
= pCreateInfo
->attachmentCount
;
1743 for (uint32_t i
= 0; i
< pCreateInfo
->attachmentCount
; i
++) {
1744 VkImageView _iview
= pCreateInfo
->pAttachments
[i
];
1745 framebuffer
->attachments
[i
] = anv_image_view_from_handle(_iview
);
1748 framebuffer
->width
= pCreateInfo
->width
;
1749 framebuffer
->height
= pCreateInfo
->height
;
1750 framebuffer
->layers
= pCreateInfo
->layers
;
1752 *pFramebuffer
= anv_framebuffer_to_handle(framebuffer
);
1757 void anv_DestroyFramebuffer(
1760 const VkAllocationCallbacks
* pAllocator
)
1762 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1763 ANV_FROM_HANDLE(anv_framebuffer
, fb
, _fb
);
1765 anv_free2(&device
->alloc
, pAllocator
, fb
);
1768 void vkCmdDbgMarkerBegin(
1769 VkCommandBuffer commandBuffer
,
1770 const char* pMarker
)
1771 __attribute__ ((visibility ("default")));
1773 void vkCmdDbgMarkerEnd(
1774 VkCommandBuffer commandBuffer
)
1775 __attribute__ ((visibility ("default")));
1777 void vkCmdDbgMarkerBegin(
1778 VkCommandBuffer commandBuffer
,
1779 const char* pMarker
)
1783 void vkCmdDbgMarkerEnd(
1784 VkCommandBuffer commandBuffer
)