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"
34 #include "gen7_pack.h"
36 struct anv_dispatch_table dtable
;
39 compiler_debug_log(void *data
, const char *fmt
, ...)
43 compiler_perf_log(void *data
, const char *fmt
, ...)
48 if (unlikely(INTEL_DEBUG
& DEBUG_PERF
))
49 vfprintf(stderr
, fmt
, args
);
55 anv_physical_device_init(struct anv_physical_device
*device
,
56 struct anv_instance
*instance
,
62 fd
= open(path
, O_RDWR
| O_CLOEXEC
);
64 return vk_errorf(VK_ERROR_INITIALIZATION_FAILED
,
65 "failed to open %s: %m", path
);
67 device
->_loader_data
.loaderMagic
= ICD_LOADER_MAGIC
;
68 device
->instance
= instance
;
71 device
->chipset_id
= anv_gem_get_param(fd
, I915_PARAM_CHIPSET_ID
);
72 if (!device
->chipset_id
) {
73 result
= vk_errorf(VK_ERROR_INITIALIZATION_FAILED
,
74 "failed to get chipset id: %m");
78 device
->name
= brw_get_device_name(device
->chipset_id
);
79 device
->info
= brw_get_device_info(device
->chipset_id
);
81 result
= vk_errorf(VK_ERROR_INITIALIZATION_FAILED
,
82 "failed to get device info");
86 if (device
->info
->is_haswell
) {
87 fprintf(stderr
, "WARNING: Haswell Vulkan support is incomplete\n");
88 } else if (device
->info
->gen
== 7 && !device
->info
->is_baytrail
) {
89 fprintf(stderr
, "WARNING: Ivy Bridge Vulkan support is incomplete\n");
90 } else if (device
->info
->gen
== 7 && device
->info
->is_baytrail
) {
91 fprintf(stderr
, "WARNING: Bay Trail Vulkan support is incomplete\n");
92 } else if (device
->info
->gen
== 9 && !device
->info
->is_broxton
) {
93 fprintf(stderr
, "WARNING: Skylake Vulkan support is incomplete\n");
94 } else if (device
->info
->gen
== 9 && device
->info
->is_broxton
) {
95 fprintf(stderr
, "WARNING: Broxton Vulkan support is incomplete\n");
96 } else if (device
->info
->gen
== 8) {
97 /* Broadwell/Cherryview is as fully supported as anything */
99 result
= vk_errorf(VK_ERROR_INCOMPATIBLE_DRIVER
,
100 "Vulkan not yet supported on %s", device
->name
);
104 if (anv_gem_get_aperture(fd
, &device
->aperture_size
) == -1) {
105 result
= vk_errorf(VK_ERROR_INITIALIZATION_FAILED
,
106 "failed to get aperture size: %m");
110 if (!anv_gem_get_param(fd
, I915_PARAM_HAS_WAIT_TIMEOUT
)) {
111 result
= vk_errorf(VK_ERROR_INITIALIZATION_FAILED
,
112 "kernel missing gem wait");
116 if (!anv_gem_get_param(fd
, I915_PARAM_HAS_EXECBUF2
)) {
117 result
= vk_errorf(VK_ERROR_INITIALIZATION_FAILED
,
118 "kernel missing execbuf2");
122 if (!device
->info
->has_llc
&&
123 anv_gem_get_param(fd
, I915_PARAM_MMAP_VERSION
) < 1) {
124 result
= vk_errorf(VK_ERROR_INITIALIZATION_FAILED
,
125 "kernel missing wc mmap");
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 isl_device_init(&device
->isl_dev
, device
->info
);
151 anv_physical_device_finish(struct anv_physical_device
*device
)
153 ralloc_free(device
->compiler
);
156 static const VkExtensionProperties global_extensions
[] = {
158 .extensionName
= VK_KHR_SURFACE_EXTENSION_NAME
,
162 .extensionName
= VK_KHR_XCB_SURFACE_EXTENSION_NAME
,
165 #ifdef HAVE_WAYLAND_PLATFORM
167 .extensionName
= VK_KHR_WAYLAND_SURFACE_EXTENSION_NAME
,
173 static const VkExtensionProperties device_extensions
[] = {
175 .extensionName
= VK_KHR_SWAPCHAIN_EXTENSION_NAME
,
181 default_alloc_func(void *pUserData
, size_t size
, size_t align
,
182 VkSystemAllocationScope allocationScope
)
188 default_realloc_func(void *pUserData
, void *pOriginal
, size_t size
,
189 size_t align
, VkSystemAllocationScope allocationScope
)
191 return realloc(pOriginal
, size
);
195 default_free_func(void *pUserData
, void *pMemory
)
200 static const VkAllocationCallbacks default_alloc
= {
202 .pfnAllocation
= default_alloc_func
,
203 .pfnReallocation
= default_realloc_func
,
204 .pfnFree
= default_free_func
,
207 VkResult
anv_CreateInstance(
208 const VkInstanceCreateInfo
* pCreateInfo
,
209 const VkAllocationCallbacks
* pAllocator
,
210 VkInstance
* pInstance
)
212 struct anv_instance
*instance
;
214 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO
);
216 if (pCreateInfo
->pApplicationInfo
->apiVersion
!= VK_MAKE_VERSION(0, 210, 1))
217 return vk_error(VK_ERROR_INCOMPATIBLE_DRIVER
);
219 for (uint32_t i
= 0; i
< pCreateInfo
->enabledExtensionNameCount
; i
++) {
221 for (uint32_t j
= 0; j
< ARRAY_SIZE(global_extensions
); j
++) {
222 if (strcmp(pCreateInfo
->ppEnabledExtensionNames
[i
],
223 global_extensions
[j
].extensionName
) == 0) {
229 return vk_error(VK_ERROR_EXTENSION_NOT_PRESENT
);
232 instance
= anv_alloc2(&default_alloc
, pAllocator
, sizeof(*instance
), 8,
233 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
235 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
237 instance
->_loader_data
.loaderMagic
= ICD_LOADER_MAGIC
;
240 instance
->alloc
= *pAllocator
;
242 instance
->alloc
= default_alloc
;
244 instance
->apiVersion
= pCreateInfo
->pApplicationInfo
->apiVersion
;
245 instance
->physicalDeviceCount
= -1;
249 VG(VALGRIND_CREATE_MEMPOOL(instance
, 0, false));
251 anv_init_wsi(instance
);
253 *pInstance
= anv_instance_to_handle(instance
);
258 void anv_DestroyInstance(
259 VkInstance _instance
,
260 const VkAllocationCallbacks
* pAllocator
)
262 ANV_FROM_HANDLE(anv_instance
, instance
, _instance
);
264 if (instance
->physicalDeviceCount
> 0) {
265 /* We support at most one physical device. */
266 assert(instance
->physicalDeviceCount
== 1);
267 anv_physical_device_finish(&instance
->physicalDevice
);
270 anv_finish_wsi(instance
);
272 VG(VALGRIND_DESTROY_MEMPOOL(instance
));
276 anv_free(&instance
->alloc
, instance
);
279 VkResult
anv_EnumeratePhysicalDevices(
280 VkInstance _instance
,
281 uint32_t* pPhysicalDeviceCount
,
282 VkPhysicalDevice
* pPhysicalDevices
)
284 ANV_FROM_HANDLE(anv_instance
, instance
, _instance
);
287 if (instance
->physicalDeviceCount
< 0) {
288 result
= anv_physical_device_init(&instance
->physicalDevice
,
289 instance
, "/dev/dri/renderD128");
290 if (result
== VK_ERROR_INCOMPATIBLE_DRIVER
) {
291 instance
->physicalDeviceCount
= 0;
292 } else if (result
== VK_SUCCESS
) {
293 instance
->physicalDeviceCount
= 1;
299 /* pPhysicalDeviceCount is an out parameter if pPhysicalDevices is NULL;
300 * otherwise it's an inout parameter.
302 * The Vulkan spec (git aaed022) says:
304 * pPhysicalDeviceCount is a pointer to an unsigned integer variable
305 * that is initialized with the number of devices the application is
306 * prepared to receive handles to. pname:pPhysicalDevices is pointer to
307 * an array of at least this many VkPhysicalDevice handles [...].
309 * Upon success, if pPhysicalDevices is NULL, vkEnumeratePhysicalDevices
310 * overwrites the contents of the variable pointed to by
311 * pPhysicalDeviceCount with the number of physical devices in in the
312 * instance; otherwise, vkEnumeratePhysicalDevices overwrites
313 * pPhysicalDeviceCount with the number of physical handles written to
316 if (!pPhysicalDevices
) {
317 *pPhysicalDeviceCount
= instance
->physicalDeviceCount
;
318 } else if (*pPhysicalDeviceCount
>= 1) {
319 pPhysicalDevices
[0] = anv_physical_device_to_handle(&instance
->physicalDevice
);
320 *pPhysicalDeviceCount
= 1;
322 *pPhysicalDeviceCount
= 0;
328 void anv_GetPhysicalDeviceFeatures(
329 VkPhysicalDevice physicalDevice
,
330 VkPhysicalDeviceFeatures
* pFeatures
)
332 anv_finishme("Get correct values for PhysicalDeviceFeatures");
334 *pFeatures
= (VkPhysicalDeviceFeatures
) {
335 .robustBufferAccess
= false,
336 .fullDrawIndexUint32
= false,
337 .imageCubeArray
= false,
338 .independentBlend
= false,
339 .geometryShader
= true,
340 .tessellationShader
= false,
341 .sampleRateShading
= false,
342 .dualSrcBlend
= true,
344 .multiDrawIndirect
= true,
346 .depthBiasClamp
= false,
347 .fillModeNonSolid
= true,
348 .depthBounds
= false,
352 .multiViewport
= true,
353 .samplerAnisotropy
= false, /* FINISHME */
354 .textureCompressionETC2
= true,
355 .textureCompressionASTC_LDR
= true,
356 .textureCompressionBC
= true,
357 .occlusionQueryPrecise
= false, /* FINISHME */
358 .pipelineStatisticsQuery
= true,
359 .vertexPipelineStoresAndAtomics
= false,
360 .fragmentStoresAndAtomics
= true,
361 .shaderTessellationAndGeometryPointSize
= true,
362 .shaderImageGatherExtended
= true,
363 .shaderStorageImageExtendedFormats
= false,
364 .shaderStorageImageMultisample
= false,
365 .shaderUniformBufferArrayDynamicIndexing
= true,
366 .shaderSampledImageArrayDynamicIndexing
= false,
367 .shaderStorageBufferArrayDynamicIndexing
= false,
368 .shaderStorageImageArrayDynamicIndexing
= false,
369 .shaderStorageImageReadWithoutFormat
= false,
370 .shaderStorageImageWriteWithoutFormat
= true,
371 .shaderClipDistance
= false,
372 .shaderCullDistance
= false,
373 .shaderFloat64
= false,
374 .shaderInt64
= false,
375 .shaderInt16
= false,
377 .variableMultisampleRate
= false,
381 void anv_GetPhysicalDeviceProperties(
382 VkPhysicalDevice physicalDevice
,
383 VkPhysicalDeviceProperties
* pProperties
)
385 ANV_FROM_HANDLE(anv_physical_device
, pdevice
, physicalDevice
);
386 const struct brw_device_info
*devinfo
= pdevice
->info
;
388 anv_finishme("Get correct values for VkPhysicalDeviceLimits");
390 VkSampleCountFlags sample_counts
=
391 VK_SAMPLE_COUNT_1_BIT
|
392 VK_SAMPLE_COUNT_2_BIT
|
393 VK_SAMPLE_COUNT_4_BIT
|
394 VK_SAMPLE_COUNT_8_BIT
;
396 VkPhysicalDeviceLimits limits
= {
397 .maxImageDimension1D
= (1 << 14),
398 .maxImageDimension2D
= (1 << 14),
399 .maxImageDimension3D
= (1 << 10),
400 .maxImageDimensionCube
= (1 << 14),
401 .maxImageArrayLayers
= (1 << 10),
402 .maxTexelBufferElements
= (1 << 14),
403 .maxUniformBufferRange
= UINT32_MAX
,
404 .maxStorageBufferRange
= UINT32_MAX
,
405 .maxPushConstantsSize
= MAX_PUSH_CONSTANTS_SIZE
,
406 .maxMemoryAllocationCount
= UINT32_MAX
,
407 .maxSamplerAllocationCount
= UINT32_MAX
,
408 .bufferImageGranularity
= 64, /* A cache line */
409 .sparseAddressSpaceSize
= 0,
410 .maxBoundDescriptorSets
= MAX_SETS
,
411 .maxPerStageDescriptorSamplers
= 64,
412 .maxPerStageDescriptorUniformBuffers
= 64,
413 .maxPerStageDescriptorStorageBuffers
= 64,
414 .maxPerStageDescriptorSampledImages
= 64,
415 .maxPerStageDescriptorStorageImages
= 64,
416 .maxPerStageDescriptorInputAttachments
= 64,
417 .maxPerStageResources
= 128,
418 .maxDescriptorSetSamplers
= 256,
419 .maxDescriptorSetUniformBuffers
= 256,
420 .maxDescriptorSetUniformBuffersDynamic
= 256,
421 .maxDescriptorSetStorageBuffers
= 256,
422 .maxDescriptorSetStorageBuffersDynamic
= 256,
423 .maxDescriptorSetSampledImages
= 256,
424 .maxDescriptorSetStorageImages
= 256,
425 .maxDescriptorSetInputAttachments
= 256,
426 .maxVertexInputAttributes
= 32,
427 .maxVertexInputBindings
= 32,
428 .maxVertexInputAttributeOffset
= 256,
429 .maxVertexInputBindingStride
= 256,
430 .maxVertexOutputComponents
= 32,
431 .maxTessellationGenerationLevel
= 0,
432 .maxTessellationPatchSize
= 0,
433 .maxTessellationControlPerVertexInputComponents
= 0,
434 .maxTessellationControlPerVertexOutputComponents
= 0,
435 .maxTessellationControlPerPatchOutputComponents
= 0,
436 .maxTessellationControlTotalOutputComponents
= 0,
437 .maxTessellationEvaluationInputComponents
= 0,
438 .maxTessellationEvaluationOutputComponents
= 0,
439 .maxGeometryShaderInvocations
= 6,
440 .maxGeometryInputComponents
= 16,
441 .maxGeometryOutputComponents
= 16,
442 .maxGeometryOutputVertices
= 16,
443 .maxGeometryTotalOutputComponents
= 16,
444 .maxFragmentInputComponents
= 16,
445 .maxFragmentOutputAttachments
= 8,
446 .maxFragmentDualSrcAttachments
= 2,
447 .maxFragmentCombinedOutputResources
= 8,
448 .maxComputeSharedMemorySize
= 1024,
449 .maxComputeWorkGroupCount
= {
450 16 * devinfo
->max_cs_threads
,
451 16 * devinfo
->max_cs_threads
,
452 16 * devinfo
->max_cs_threads
,
454 .maxComputeWorkGroupInvocations
= 16 * devinfo
->max_cs_threads
,
455 .maxComputeWorkGroupSize
= {
456 16 * devinfo
->max_cs_threads
,
457 16 * devinfo
->max_cs_threads
,
458 16 * devinfo
->max_cs_threads
,
460 .subPixelPrecisionBits
= 4 /* FIXME */,
461 .subTexelPrecisionBits
= 4 /* FIXME */,
462 .mipmapPrecisionBits
= 4 /* FIXME */,
463 .maxDrawIndexedIndexValue
= UINT32_MAX
,
464 .maxDrawIndirectCount
= UINT32_MAX
,
465 .maxSamplerLodBias
= 16,
466 .maxSamplerAnisotropy
= 16,
467 .maxViewports
= MAX_VIEWPORTS
,
468 .maxViewportDimensions
= { (1 << 14), (1 << 14) },
469 .viewportBoundsRange
= { -1.0, 1.0 }, /* FIXME */
470 .viewportSubPixelBits
= 13, /* We take a float? */
471 .minMemoryMapAlignment
= 4096, /* A page */
472 .minTexelBufferOffsetAlignment
= 1,
473 .minUniformBufferOffsetAlignment
= 1,
474 .minStorageBufferOffsetAlignment
= 1,
475 .minTexelOffset
= 0, /* FIXME */
476 .maxTexelOffset
= 0, /* FIXME */
477 .minTexelGatherOffset
= 0, /* FIXME */
478 .maxTexelGatherOffset
= 0, /* FIXME */
479 .minInterpolationOffset
= 0, /* FIXME */
480 .maxInterpolationOffset
= 0, /* FIXME */
481 .subPixelInterpolationOffsetBits
= 0, /* FIXME */
482 .maxFramebufferWidth
= (1 << 14),
483 .maxFramebufferHeight
= (1 << 14),
484 .maxFramebufferLayers
= (1 << 10),
485 .framebufferColorSampleCounts
= sample_counts
,
486 .framebufferDepthSampleCounts
= sample_counts
,
487 .framebufferStencilSampleCounts
= sample_counts
,
488 .framebufferNoAttachmentsSampleCounts
= sample_counts
,
489 .maxColorAttachments
= MAX_RTS
,
490 .sampledImageColorSampleCounts
= sample_counts
,
491 .sampledImageIntegerSampleCounts
= VK_SAMPLE_COUNT_1_BIT
,
492 .sampledImageDepthSampleCounts
= sample_counts
,
493 .sampledImageStencilSampleCounts
= sample_counts
,
494 .storageImageSampleCounts
= VK_SAMPLE_COUNT_1_BIT
,
495 .maxSampleMaskWords
= 1,
496 .timestampPeriod
= 80.0 / (1000 * 1000 * 1000),
497 .maxClipDistances
= 0 /* FIXME */,
498 .maxCullDistances
= 0 /* FIXME */,
499 .maxCombinedClipAndCullDistances
= 0 /* FIXME */,
500 .discreteQueuePriorities
= 1,
501 .pointSizeRange
= { 0.125, 255.875 },
502 .lineWidthRange
= { 0.0, 7.9921875 },
503 .pointSizeGranularity
= (1.0 / 8.0),
504 .lineWidthGranularity
= (1.0 / 128.0),
505 .strictLines
= false, /* FINISHME */
506 .standardSampleLocations
= true, /* FINISHME */
507 .optimalBufferCopyOffsetAlignment
= 128,
508 .optimalBufferCopyRowPitchAlignment
= 128,
509 .nonCoherentAtomSize
= 64,
512 *pProperties
= (VkPhysicalDeviceProperties
) {
513 .apiVersion
= VK_MAKE_VERSION(0, 210, 1),
516 .deviceID
= pdevice
->chipset_id
,
517 .deviceType
= VK_PHYSICAL_DEVICE_TYPE_INTEGRATED_GPU
,
519 .sparseProperties
= {0}, /* Broadwell doesn't do sparse. */
522 strcpy(pProperties
->deviceName
, pdevice
->name
);
523 snprintf((char *)pProperties
->pipelineCacheUUID
, VK_UUID_SIZE
,
524 "anv-%s", MESA_GIT_SHA1
+ 4);
527 void anv_GetPhysicalDeviceQueueFamilyProperties(
528 VkPhysicalDevice physicalDevice
,
530 VkQueueFamilyProperties
* pQueueFamilyProperties
)
532 if (pQueueFamilyProperties
== NULL
) {
537 assert(*pCount
>= 1);
539 *pQueueFamilyProperties
= (VkQueueFamilyProperties
) {
540 .queueFlags
= VK_QUEUE_GRAPHICS_BIT
|
541 VK_QUEUE_COMPUTE_BIT
|
542 VK_QUEUE_TRANSFER_BIT
,
544 .timestampValidBits
= 0, /* XXX: Real value here */
545 .minImageTransferGranularity
= (VkExtent3D
) { 1, 1, 1 },
549 void anv_GetPhysicalDeviceMemoryProperties(
550 VkPhysicalDevice physicalDevice
,
551 VkPhysicalDeviceMemoryProperties
* pMemoryProperties
)
553 ANV_FROM_HANDLE(anv_physical_device
, physical_device
, physicalDevice
);
554 VkDeviceSize heap_size
;
556 /* Reserve some wiggle room for the driver by exposing only 75% of the
557 * aperture to the heap.
559 heap_size
= 3 * physical_device
->aperture_size
/ 4;
561 if (physical_device
->info
->has_llc
) {
562 /* Big core GPUs share LLC with the CPU and thus one memory type can be
563 * both cached and coherent at the same time.
565 pMemoryProperties
->memoryTypeCount
= 1;
566 pMemoryProperties
->memoryTypes
[0] = (VkMemoryType
) {
567 .propertyFlags
= VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT
|
568 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT
|
569 VK_MEMORY_PROPERTY_HOST_COHERENT_BIT
|
570 VK_MEMORY_PROPERTY_HOST_CACHED_BIT
,
574 /* The spec requires that we expose a host-visible, coherent memory
575 * type, but Atom GPUs don't share LLC. Thus we offer two memory types
576 * to give the application a choice between cached, but not coherent and
577 * coherent but uncached (WC though).
579 pMemoryProperties
->memoryTypeCount
= 2;
580 pMemoryProperties
->memoryTypes
[0] = (VkMemoryType
) {
581 .propertyFlags
= VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT
|
582 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT
|
583 VK_MEMORY_PROPERTY_HOST_COHERENT_BIT
,
586 pMemoryProperties
->memoryTypes
[1] = (VkMemoryType
) {
587 .propertyFlags
= VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT
|
588 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT
|
589 VK_MEMORY_PROPERTY_HOST_CACHED_BIT
,
594 pMemoryProperties
->memoryHeapCount
= 1;
595 pMemoryProperties
->memoryHeaps
[0] = (VkMemoryHeap
) {
597 .flags
= VK_MEMORY_HEAP_DEVICE_LOCAL_BIT
,
601 PFN_vkVoidFunction
anv_GetInstanceProcAddr(
605 return anv_lookup_entrypoint(pName
);
608 PFN_vkVoidFunction
anv_GetDeviceProcAddr(
612 return anv_lookup_entrypoint(pName
);
616 anv_queue_init(struct anv_device
*device
, struct anv_queue
*queue
)
618 queue
->_loader_data
.loaderMagic
= ICD_LOADER_MAGIC
;
619 queue
->device
= device
;
620 queue
->pool
= &device
->surface_state_pool
;
626 anv_queue_finish(struct anv_queue
*queue
)
630 static struct anv_state
631 anv_state_pool_emit_data(struct anv_state_pool
*pool
, size_t size
, size_t align
, const void *p
)
633 struct anv_state state
;
635 state
= anv_state_pool_alloc(pool
, size
, align
);
636 memcpy(state
.map
, p
, size
);
638 if (!pool
->block_pool
->device
->info
.has_llc
)
639 anv_state_clflush(state
);
645 anv_device_init_border_colors(struct anv_device
*device
)
647 static const VkClearColorValue border_colors
[] = {
648 [VK_BORDER_COLOR_FLOAT_TRANSPARENT_BLACK
] = { .float32
= { 0.0, 0.0, 0.0, 0.0 } },
649 [VK_BORDER_COLOR_FLOAT_OPAQUE_BLACK
] = { .float32
= { 0.0, 0.0, 0.0, 1.0 } },
650 [VK_BORDER_COLOR_FLOAT_OPAQUE_WHITE
] = { .float32
= { 1.0, 1.0, 1.0, 1.0 } },
651 [VK_BORDER_COLOR_INT_TRANSPARENT_BLACK
] = { .uint32
= { 0, 0, 0, 0 } },
652 [VK_BORDER_COLOR_INT_OPAQUE_BLACK
] = { .uint32
= { 0, 0, 0, 1 } },
653 [VK_BORDER_COLOR_INT_OPAQUE_WHITE
] = { .uint32
= { 1, 1, 1, 1 } },
656 device
->border_colors
= anv_state_pool_emit_data(&device
->dynamic_state_pool
,
657 sizeof(border_colors
), 32, border_colors
);
660 VkResult
anv_CreateDevice(
661 VkPhysicalDevice physicalDevice
,
662 const VkDeviceCreateInfo
* pCreateInfo
,
663 const VkAllocationCallbacks
* pAllocator
,
666 ANV_FROM_HANDLE(anv_physical_device
, physical_device
, physicalDevice
);
668 struct anv_device
*device
;
670 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO
);
672 for (uint32_t i
= 0; i
< pCreateInfo
->enabledExtensionNameCount
; i
++) {
674 for (uint32_t j
= 0; j
< ARRAY_SIZE(device_extensions
); j
++) {
675 if (strcmp(pCreateInfo
->ppEnabledExtensionNames
[i
],
676 device_extensions
[j
].extensionName
) == 0) {
682 return vk_error(VK_ERROR_EXTENSION_NOT_PRESENT
);
685 anv_set_dispatch_devinfo(physical_device
->info
);
687 device
= anv_alloc2(&physical_device
->instance
->alloc
, pAllocator
,
689 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
691 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
693 device
->_loader_data
.loaderMagic
= ICD_LOADER_MAGIC
;
694 device
->instance
= physical_device
->instance
;
697 device
->alloc
= *pAllocator
;
699 device
->alloc
= physical_device
->instance
->alloc
;
701 /* XXX(chadv): Can we dup() physicalDevice->fd here? */
702 device
->fd
= open(physical_device
->path
, O_RDWR
| O_CLOEXEC
);
703 if (device
->fd
== -1) {
704 result
= vk_error(VK_ERROR_INITIALIZATION_FAILED
);
708 device
->context_id
= anv_gem_create_context(device
);
709 if (device
->context_id
== -1) {
710 result
= vk_error(VK_ERROR_INITIALIZATION_FAILED
);
714 device
->info
= *physical_device
->info
;
715 device
->isl_dev
= physical_device
->isl_dev
;
717 pthread_mutex_init(&device
->mutex
, NULL
);
719 anv_bo_pool_init(&device
->batch_bo_pool
, device
, ANV_CMD_BUFFER_BATCH_SIZE
);
721 anv_block_pool_init(&device
->dynamic_state_block_pool
, device
, 16384);
723 anv_state_pool_init(&device
->dynamic_state_pool
,
724 &device
->dynamic_state_block_pool
);
726 anv_block_pool_init(&device
->instruction_block_pool
, device
, 64 * 1024);
727 anv_pipeline_cache_init(&device
->default_pipeline_cache
, device
);
729 anv_block_pool_init(&device
->surface_state_block_pool
, device
, 4096);
731 anv_state_pool_init(&device
->surface_state_pool
,
732 &device
->surface_state_block_pool
);
734 anv_bo_init_new(&device
->workaround_bo
, device
, 1024);
736 anv_block_pool_init(&device
->scratch_block_pool
, device
, 0x10000);
738 anv_queue_init(device
, &device
->queue
);
740 result
= anv_device_init_meta(device
);
741 if (result
!= VK_SUCCESS
)
744 anv_device_init_border_colors(device
);
746 *pDevice
= anv_device_to_handle(device
);
753 anv_free(&device
->alloc
, device
);
758 void anv_DestroyDevice(
760 const VkAllocationCallbacks
* pAllocator
)
762 ANV_FROM_HANDLE(anv_device
, device
, _device
);
764 anv_queue_finish(&device
->queue
);
766 anv_device_finish_meta(device
);
769 /* We only need to free these to prevent valgrind errors. The backing
770 * BO will go away in a couple of lines so we don't actually leak.
772 anv_state_pool_free(&device
->dynamic_state_pool
, device
->border_colors
);
775 anv_gem_munmap(device
->workaround_bo
.map
, device
->workaround_bo
.size
);
776 anv_gem_close(device
, device
->workaround_bo
.gem_handle
);
778 anv_bo_pool_finish(&device
->batch_bo_pool
);
779 anv_state_pool_finish(&device
->dynamic_state_pool
);
780 anv_block_pool_finish(&device
->dynamic_state_block_pool
);
781 anv_block_pool_finish(&device
->instruction_block_pool
);
782 anv_state_pool_finish(&device
->surface_state_pool
);
783 anv_block_pool_finish(&device
->surface_state_block_pool
);
784 anv_block_pool_finish(&device
->scratch_block_pool
);
788 pthread_mutex_destroy(&device
->mutex
);
790 anv_free(&device
->alloc
, device
);
793 VkResult
anv_EnumerateInstanceExtensionProperties(
794 const char* pLayerName
,
795 uint32_t* pPropertyCount
,
796 VkExtensionProperties
* pProperties
)
798 if (pProperties
== NULL
) {
799 *pPropertyCount
= ARRAY_SIZE(global_extensions
);
803 assert(*pPropertyCount
>= ARRAY_SIZE(global_extensions
));
805 *pPropertyCount
= ARRAY_SIZE(global_extensions
);
806 memcpy(pProperties
, global_extensions
, sizeof(global_extensions
));
811 VkResult
anv_EnumerateDeviceExtensionProperties(
812 VkPhysicalDevice physicalDevice
,
813 const char* pLayerName
,
814 uint32_t* pPropertyCount
,
815 VkExtensionProperties
* pProperties
)
817 if (pProperties
== NULL
) {
818 *pPropertyCount
= ARRAY_SIZE(device_extensions
);
822 assert(*pPropertyCount
>= ARRAY_SIZE(device_extensions
));
824 *pPropertyCount
= ARRAY_SIZE(device_extensions
);
825 memcpy(pProperties
, device_extensions
, sizeof(device_extensions
));
830 VkResult
anv_EnumerateInstanceLayerProperties(
831 uint32_t* pPropertyCount
,
832 VkLayerProperties
* pProperties
)
834 if (pProperties
== NULL
) {
839 /* None supported at this time */
840 return vk_error(VK_ERROR_LAYER_NOT_PRESENT
);
843 VkResult
anv_EnumerateDeviceLayerProperties(
844 VkPhysicalDevice physicalDevice
,
845 uint32_t* pPropertyCount
,
846 VkLayerProperties
* pProperties
)
848 if (pProperties
== NULL
) {
853 /* None supported at this time */
854 return vk_error(VK_ERROR_LAYER_NOT_PRESENT
);
857 void anv_GetDeviceQueue(
859 uint32_t queueNodeIndex
,
863 ANV_FROM_HANDLE(anv_device
, device
, _device
);
865 assert(queueIndex
== 0);
867 *pQueue
= anv_queue_to_handle(&device
->queue
);
870 VkResult
anv_QueueSubmit(
872 uint32_t submitCount
,
873 const VkSubmitInfo
* pSubmits
,
876 ANV_FROM_HANDLE(anv_queue
, queue
, _queue
);
877 ANV_FROM_HANDLE(anv_fence
, fence
, _fence
);
878 struct anv_device
*device
= queue
->device
;
881 for (uint32_t i
= 0; i
< submitCount
; i
++) {
882 for (uint32_t j
= 0; j
< pSubmits
[i
].commandBufferCount
; j
++) {
883 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
,
884 pSubmits
[i
].pCommandBuffers
[j
]);
885 assert(cmd_buffer
->level
== VK_COMMAND_BUFFER_LEVEL_PRIMARY
);
887 ret
= anv_gem_execbuffer(device
, &cmd_buffer
->execbuf2
.execbuf
);
889 /* We don't know the real error. */
890 return vk_errorf(VK_ERROR_OUT_OF_DEVICE_MEMORY
,
891 "execbuf2 failed: %m");
895 ret
= anv_gem_execbuffer(device
, &fence
->execbuf
);
897 /* We don't know the real error. */
898 return vk_errorf(VK_ERROR_OUT_OF_DEVICE_MEMORY
,
899 "execbuf2 failed: %m");
903 for (uint32_t k
= 0; k
< cmd_buffer
->execbuf2
.bo_count
; k
++)
904 cmd_buffer
->execbuf2
.bos
[k
]->offset
= cmd_buffer
->execbuf2
.objects
[k
].offset
;
911 VkResult
anv_QueueWaitIdle(
914 ANV_FROM_HANDLE(anv_queue
, queue
, _queue
);
916 return ANV_CALL(DeviceWaitIdle
)(anv_device_to_handle(queue
->device
));
919 VkResult
anv_DeviceWaitIdle(
922 ANV_FROM_HANDLE(anv_device
, device
, _device
);
923 struct anv_state state
;
924 struct anv_batch batch
;
925 struct drm_i915_gem_execbuffer2 execbuf
;
926 struct drm_i915_gem_exec_object2 exec2_objects
[1];
927 struct anv_bo
*bo
= NULL
;
932 state
= anv_state_pool_alloc(&device
->dynamic_state_pool
, 32, 32);
933 bo
= &device
->dynamic_state_pool
.block_pool
->bo
;
934 batch
.start
= batch
.next
= state
.map
;
935 batch
.end
= state
.map
+ 32;
936 anv_batch_emit(&batch
, GEN7_MI_BATCH_BUFFER_END
);
937 anv_batch_emit(&batch
, GEN7_MI_NOOP
);
939 if (!device
->info
.has_llc
)
940 anv_state_clflush(state
);
942 exec2_objects
[0].handle
= bo
->gem_handle
;
943 exec2_objects
[0].relocation_count
= 0;
944 exec2_objects
[0].relocs_ptr
= 0;
945 exec2_objects
[0].alignment
= 0;
946 exec2_objects
[0].offset
= bo
->offset
;
947 exec2_objects
[0].flags
= 0;
948 exec2_objects
[0].rsvd1
= 0;
949 exec2_objects
[0].rsvd2
= 0;
951 execbuf
.buffers_ptr
= (uintptr_t) exec2_objects
;
952 execbuf
.buffer_count
= 1;
953 execbuf
.batch_start_offset
= state
.offset
;
954 execbuf
.batch_len
= batch
.next
- state
.map
;
955 execbuf
.cliprects_ptr
= 0;
956 execbuf
.num_cliprects
= 0;
961 I915_EXEC_HANDLE_LUT
| I915_EXEC_NO_RELOC
| I915_EXEC_RENDER
;
962 execbuf
.rsvd1
= device
->context_id
;
965 ret
= anv_gem_execbuffer(device
, &execbuf
);
967 /* We don't know the real error. */
968 result
= vk_errorf(VK_ERROR_OUT_OF_DEVICE_MEMORY
, "execbuf2 failed: %m");
973 ret
= anv_gem_wait(device
, bo
->gem_handle
, &timeout
);
975 /* We don't know the real error. */
976 result
= vk_errorf(VK_ERROR_OUT_OF_DEVICE_MEMORY
, "execbuf2 failed: %m");
980 anv_state_pool_free(&device
->dynamic_state_pool
, state
);
985 anv_state_pool_free(&device
->dynamic_state_pool
, state
);
991 anv_bo_init_new(struct anv_bo
*bo
, struct anv_device
*device
, uint64_t size
)
993 bo
->gem_handle
= anv_gem_create(device
, size
);
995 return vk_error(VK_ERROR_OUT_OF_DEVICE_MEMORY
);
1005 VkResult
anv_AllocateMemory(
1007 const VkMemoryAllocateInfo
* pAllocateInfo
,
1008 const VkAllocationCallbacks
* pAllocator
,
1009 VkDeviceMemory
* pMem
)
1011 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1012 struct anv_device_memory
*mem
;
1015 assert(pAllocateInfo
->sType
== VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO
);
1017 if (pAllocateInfo
->allocationSize
== 0) {
1018 /* Apparently, this is allowed */
1019 *pMem
= VK_NULL_HANDLE
;
1023 /* We support exactly one memory heap. */
1024 assert(pAllocateInfo
->memoryTypeIndex
== 0 ||
1025 (!device
->info
.has_llc
&& pAllocateInfo
->memoryTypeIndex
< 2));
1027 /* FINISHME: Fail if allocation request exceeds heap size. */
1029 mem
= anv_alloc2(&device
->alloc
, pAllocator
, sizeof(*mem
), 8,
1030 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
1032 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
1034 /* The kernel is going to give us whole pages anyway */
1035 uint64_t alloc_size
= align_u64(pAllocateInfo
->allocationSize
, 4096);
1037 result
= anv_bo_init_new(&mem
->bo
, device
, alloc_size
);
1038 if (result
!= VK_SUCCESS
)
1041 mem
->type_index
= pAllocateInfo
->memoryTypeIndex
;
1043 *pMem
= anv_device_memory_to_handle(mem
);
1048 anv_free2(&device
->alloc
, pAllocator
, mem
);
1053 void anv_FreeMemory(
1055 VkDeviceMemory _mem
,
1056 const VkAllocationCallbacks
* pAllocator
)
1058 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1059 ANV_FROM_HANDLE(anv_device_memory
, mem
, _mem
);
1065 anv_gem_munmap(mem
->bo
.map
, mem
->bo
.size
);
1067 if (mem
->bo
.gem_handle
!= 0)
1068 anv_gem_close(device
, mem
->bo
.gem_handle
);
1070 anv_free2(&device
->alloc
, pAllocator
, mem
);
1073 VkResult
anv_MapMemory(
1075 VkDeviceMemory _memory
,
1076 VkDeviceSize offset
,
1078 VkMemoryMapFlags flags
,
1081 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1082 ANV_FROM_HANDLE(anv_device_memory
, mem
, _memory
);
1089 /* FIXME: Is this supposed to be thread safe? Since vkUnmapMemory() only
1090 * takes a VkDeviceMemory pointer, it seems like only one map of the memory
1091 * at a time is valid. We could just mmap up front and return an offset
1092 * pointer here, but that may exhaust virtual memory on 32 bit
1095 uint32_t gem_flags
= 0;
1096 if (!device
->info
.has_llc
&& mem
->type_index
== 0)
1097 gem_flags
|= I915_MMAP_WC
;
1099 /* GEM will fail to map if the offset isn't 4k-aligned. Round down. */
1100 uint64_t map_offset
= offset
& ~4095ull;
1101 assert(offset
>= map_offset
);
1102 uint64_t map_size
= (offset
+ size
) - map_offset
;
1104 /* Let's map whole pages */
1105 map_size
= align_u64(map_size
, 4096);
1107 mem
->map
= anv_gem_mmap(device
, mem
->bo
.gem_handle
,
1108 map_offset
, map_size
, gem_flags
);
1109 mem
->map_size
= map_size
;
1111 *ppData
= mem
->map
+ (offset
- map_offset
);
1116 void anv_UnmapMemory(
1118 VkDeviceMemory _memory
)
1120 ANV_FROM_HANDLE(anv_device_memory
, mem
, _memory
);
1125 anv_gem_munmap(mem
->map
, mem
->map_size
);
1129 clflush_mapped_ranges(struct anv_device
*device
,
1131 const VkMappedMemoryRange
*ranges
)
1133 for (uint32_t i
= 0; i
< count
; i
++) {
1134 ANV_FROM_HANDLE(anv_device_memory
, mem
, ranges
[i
].memory
);
1135 void *p
= mem
->map
+ (ranges
[i
].offset
& ~CACHELINE_MASK
);
1136 void *end
= mem
->map
+ ranges
[i
].offset
+ ranges
[i
].size
;
1139 __builtin_ia32_clflush(p
);
1140 p
+= CACHELINE_SIZE
;
1145 VkResult
anv_FlushMappedMemoryRanges(
1147 uint32_t memoryRangeCount
,
1148 const VkMappedMemoryRange
* pMemoryRanges
)
1150 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1152 if (device
->info
.has_llc
)
1155 /* Make sure the writes we're flushing have landed. */
1156 __builtin_ia32_sfence();
1158 clflush_mapped_ranges(device
, memoryRangeCount
, pMemoryRanges
);
1163 VkResult
anv_InvalidateMappedMemoryRanges(
1165 uint32_t memoryRangeCount
,
1166 const VkMappedMemoryRange
* pMemoryRanges
)
1168 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1170 if (device
->info
.has_llc
)
1173 clflush_mapped_ranges(device
, memoryRangeCount
, pMemoryRanges
);
1175 /* Make sure no reads get moved up above the invalidate. */
1176 __builtin_ia32_lfence();
1181 void anv_GetBufferMemoryRequirements(
1184 VkMemoryRequirements
* pMemoryRequirements
)
1186 ANV_FROM_HANDLE(anv_buffer
, buffer
, _buffer
);
1188 /* The Vulkan spec (git aaed022) says:
1190 * memoryTypeBits is a bitfield and contains one bit set for every
1191 * supported memory type for the resource. The bit `1<<i` is set if and
1192 * only if the memory type `i` in the VkPhysicalDeviceMemoryProperties
1193 * structure for the physical device is supported.
1195 * We support exactly one memory type.
1197 pMemoryRequirements
->memoryTypeBits
= 1;
1199 pMemoryRequirements
->size
= buffer
->size
;
1200 pMemoryRequirements
->alignment
= 16;
1203 void anv_GetImageMemoryRequirements(
1206 VkMemoryRequirements
* pMemoryRequirements
)
1208 ANV_FROM_HANDLE(anv_image
, image
, _image
);
1210 /* The Vulkan spec (git aaed022) says:
1212 * memoryTypeBits is a bitfield and contains one bit set for every
1213 * supported memory type for the resource. The bit `1<<i` is set if and
1214 * only if the memory type `i` in the VkPhysicalDeviceMemoryProperties
1215 * structure for the physical device is supported.
1217 * We support exactly one memory type.
1219 pMemoryRequirements
->memoryTypeBits
= 1;
1221 pMemoryRequirements
->size
= image
->size
;
1222 pMemoryRequirements
->alignment
= image
->alignment
;
1225 void anv_GetImageSparseMemoryRequirements(
1228 uint32_t* pSparseMemoryRequirementCount
,
1229 VkSparseImageMemoryRequirements
* pSparseMemoryRequirements
)
1234 void anv_GetDeviceMemoryCommitment(
1236 VkDeviceMemory memory
,
1237 VkDeviceSize
* pCommittedMemoryInBytes
)
1239 *pCommittedMemoryInBytes
= 0;
1242 VkResult
anv_BindBufferMemory(
1245 VkDeviceMemory _memory
,
1246 VkDeviceSize memoryOffset
)
1248 ANV_FROM_HANDLE(anv_device_memory
, mem
, _memory
);
1249 ANV_FROM_HANDLE(anv_buffer
, buffer
, _buffer
);
1252 buffer
->bo
= &mem
->bo
;
1253 buffer
->offset
= memoryOffset
;
1262 VkResult
anv_BindImageMemory(
1265 VkDeviceMemory _memory
,
1266 VkDeviceSize memoryOffset
)
1268 ANV_FROM_HANDLE(anv_device_memory
, mem
, _memory
);
1269 ANV_FROM_HANDLE(anv_image
, image
, _image
);
1272 image
->bo
= &mem
->bo
;
1273 image
->offset
= memoryOffset
;
1282 VkResult
anv_QueueBindSparse(
1284 uint32_t bindInfoCount
,
1285 const VkBindSparseInfo
* pBindInfo
,
1288 stub_return(VK_ERROR_INCOMPATIBLE_DRIVER
);
1291 VkResult
anv_CreateFence(
1293 const VkFenceCreateInfo
* pCreateInfo
,
1294 const VkAllocationCallbacks
* pAllocator
,
1297 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1298 struct anv_fence
*fence
;
1299 struct anv_batch batch
;
1302 const uint32_t fence_size
= 128;
1304 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_FENCE_CREATE_INFO
);
1306 fence
= anv_alloc2(&device
->alloc
, pAllocator
, sizeof(*fence
), 8,
1307 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
1309 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
1311 result
= anv_bo_init_new(&fence
->bo
, device
, fence_size
);
1312 if (result
!= VK_SUCCESS
)
1316 anv_gem_mmap(device
, fence
->bo
.gem_handle
, 0, fence
->bo
.size
, 0);
1317 batch
.next
= batch
.start
= fence
->bo
.map
;
1318 batch
.end
= fence
->bo
.map
+ fence
->bo
.size
;
1319 anv_batch_emit(&batch
, GEN7_MI_BATCH_BUFFER_END
);
1320 anv_batch_emit(&batch
, GEN7_MI_NOOP
);
1322 if (!device
->info
.has_llc
) {
1323 assert(((uintptr_t) fence
->bo
.map
& CACHELINE_MASK
) == 0);
1324 assert(batch
.next
- fence
->bo
.map
<= CACHELINE_SIZE
);
1325 __builtin_ia32_sfence();
1326 __builtin_ia32_clflush(fence
->bo
.map
);
1329 fence
->exec2_objects
[0].handle
= fence
->bo
.gem_handle
;
1330 fence
->exec2_objects
[0].relocation_count
= 0;
1331 fence
->exec2_objects
[0].relocs_ptr
= 0;
1332 fence
->exec2_objects
[0].alignment
= 0;
1333 fence
->exec2_objects
[0].offset
= fence
->bo
.offset
;
1334 fence
->exec2_objects
[0].flags
= 0;
1335 fence
->exec2_objects
[0].rsvd1
= 0;
1336 fence
->exec2_objects
[0].rsvd2
= 0;
1338 fence
->execbuf
.buffers_ptr
= (uintptr_t) fence
->exec2_objects
;
1339 fence
->execbuf
.buffer_count
= 1;
1340 fence
->execbuf
.batch_start_offset
= 0;
1341 fence
->execbuf
.batch_len
= batch
.next
- fence
->bo
.map
;
1342 fence
->execbuf
.cliprects_ptr
= 0;
1343 fence
->execbuf
.num_cliprects
= 0;
1344 fence
->execbuf
.DR1
= 0;
1345 fence
->execbuf
.DR4
= 0;
1347 fence
->execbuf
.flags
=
1348 I915_EXEC_HANDLE_LUT
| I915_EXEC_NO_RELOC
| I915_EXEC_RENDER
;
1349 fence
->execbuf
.rsvd1
= device
->context_id
;
1350 fence
->execbuf
.rsvd2
= 0;
1352 *pFence
= anv_fence_to_handle(fence
);
1357 anv_free2(&device
->alloc
, pAllocator
, fence
);
1362 void anv_DestroyFence(
1365 const VkAllocationCallbacks
* pAllocator
)
1367 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1368 ANV_FROM_HANDLE(anv_fence
, fence
, _fence
);
1370 anv_gem_munmap(fence
->bo
.map
, fence
->bo
.size
);
1371 anv_gem_close(device
, fence
->bo
.gem_handle
);
1372 anv_free2(&device
->alloc
, pAllocator
, fence
);
1375 VkResult
anv_ResetFences(
1377 uint32_t fenceCount
,
1378 const VkFence
* pFences
)
1380 for (uint32_t i
= 0; i
< fenceCount
; i
++) {
1381 ANV_FROM_HANDLE(anv_fence
, fence
, pFences
[i
]);
1382 fence
->ready
= false;
1388 VkResult
anv_GetFenceStatus(
1392 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1393 ANV_FROM_HANDLE(anv_fence
, fence
, _fence
);
1400 ret
= anv_gem_wait(device
, fence
->bo
.gem_handle
, &t
);
1402 fence
->ready
= true;
1406 return VK_NOT_READY
;
1409 VkResult
anv_WaitForFences(
1411 uint32_t fenceCount
,
1412 const VkFence
* pFences
,
1416 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1418 /* DRM_IOCTL_I915_GEM_WAIT uses a signed 64 bit timeout and is supposed
1419 * to block indefinitely timeouts <= 0. Unfortunately, this was broken
1420 * for a couple of kernel releases. Since there's no way to know
1421 * whether or not the kernel we're using is one of the broken ones, the
1422 * best we can do is to clamp the timeout to INT64_MAX. This limits the
1423 * maximum timeout from 584 years to 292 years - likely not a big deal.
1425 if (timeout
> INT64_MAX
)
1426 timeout
= INT64_MAX
;
1428 int64_t t
= timeout
;
1430 /* FIXME: handle !waitAll */
1432 for (uint32_t i
= 0; i
< fenceCount
; i
++) {
1433 ANV_FROM_HANDLE(anv_fence
, fence
, pFences
[i
]);
1434 int ret
= anv_gem_wait(device
, fence
->bo
.gem_handle
, &t
);
1435 if (ret
== -1 && errno
== ETIME
) {
1437 } else if (ret
== -1) {
1438 /* We don't know the real error. */
1439 return vk_errorf(VK_ERROR_OUT_OF_DEVICE_MEMORY
,
1440 "gem wait failed: %m");
1447 // Queue semaphore functions
1449 VkResult
anv_CreateSemaphore(
1451 const VkSemaphoreCreateInfo
* pCreateInfo
,
1452 const VkAllocationCallbacks
* pAllocator
,
1453 VkSemaphore
* pSemaphore
)
1455 /* The DRM execbuffer ioctl always execute in-oder, even between different
1456 * rings. As such, there's nothing to do for the user space semaphore.
1459 *pSemaphore
= (VkSemaphore
)1;
1464 void anv_DestroySemaphore(
1466 VkSemaphore semaphore
,
1467 const VkAllocationCallbacks
* pAllocator
)
1473 VkResult
anv_CreateEvent(
1475 const VkEventCreateInfo
* pCreateInfo
,
1476 const VkAllocationCallbacks
* pAllocator
,
1479 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1480 struct anv_state state
;
1481 struct anv_event
*event
;
1483 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_EVENT_CREATE_INFO
);
1485 state
= anv_state_pool_alloc(&device
->dynamic_state_pool
,
1488 event
->state
= state
;
1489 event
->semaphore
= VK_EVENT_RESET
;
1491 if (!device
->info
.has_llc
) {
1492 /* Make sure the writes we're flushing have landed. */
1493 __builtin_ia32_sfence();
1494 __builtin_ia32_clflush(event
);
1497 *pEvent
= anv_event_to_handle(event
);
1502 void anv_DestroyEvent(
1505 const VkAllocationCallbacks
* pAllocator
)
1507 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1508 ANV_FROM_HANDLE(anv_event
, event
, _event
);
1510 anv_state_pool_free(&device
->dynamic_state_pool
, event
->state
);
1513 VkResult
anv_GetEventStatus(
1517 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1518 ANV_FROM_HANDLE(anv_event
, event
, _event
);
1520 if (!device
->info
.has_llc
) {
1521 /* Make sure the writes we're flushing have landed. */
1522 __builtin_ia32_clflush(event
);
1523 __builtin_ia32_lfence();
1526 return event
->semaphore
;
1529 VkResult
anv_SetEvent(
1533 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1534 ANV_FROM_HANDLE(anv_event
, event
, _event
);
1536 event
->semaphore
= VK_EVENT_SET
;
1538 if (!device
->info
.has_llc
) {
1539 /* Make sure the writes we're flushing have landed. */
1540 __builtin_ia32_sfence();
1541 __builtin_ia32_clflush(event
);
1547 VkResult
anv_ResetEvent(
1551 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1552 ANV_FROM_HANDLE(anv_event
, event
, _event
);
1554 event
->semaphore
= VK_EVENT_RESET
;
1556 if (!device
->info
.has_llc
) {
1557 /* Make sure the writes we're flushing have landed. */
1558 __builtin_ia32_sfence();
1559 __builtin_ia32_clflush(event
);
1567 VkResult
anv_CreateBuffer(
1569 const VkBufferCreateInfo
* pCreateInfo
,
1570 const VkAllocationCallbacks
* pAllocator
,
1573 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1574 struct anv_buffer
*buffer
;
1576 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO
);
1578 buffer
= anv_alloc2(&device
->alloc
, pAllocator
, sizeof(*buffer
), 8,
1579 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
1581 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
1583 buffer
->size
= pCreateInfo
->size
;
1584 buffer
->usage
= pCreateInfo
->usage
;
1588 *pBuffer
= anv_buffer_to_handle(buffer
);
1593 void anv_DestroyBuffer(
1596 const VkAllocationCallbacks
* pAllocator
)
1598 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1599 ANV_FROM_HANDLE(anv_buffer
, buffer
, _buffer
);
1601 anv_free2(&device
->alloc
, pAllocator
, buffer
);
1605 anv_fill_buffer_surface_state(struct anv_device
*device
, void *state
,
1606 enum isl_format format
,
1607 uint32_t offset
, uint32_t range
, uint32_t stride
)
1609 switch (device
->info
.gen
) {
1611 if (device
->info
.is_haswell
)
1612 gen75_fill_buffer_surface_state(state
, format
, offset
, range
, stride
);
1614 gen7_fill_buffer_surface_state(state
, format
, offset
, range
, stride
);
1617 gen8_fill_buffer_surface_state(state
, format
, offset
, range
, stride
);
1620 gen9_fill_buffer_surface_state(state
, format
, offset
, range
, stride
);
1623 unreachable("unsupported gen\n");
1627 void anv_DestroySampler(
1630 const VkAllocationCallbacks
* pAllocator
)
1632 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1633 ANV_FROM_HANDLE(anv_sampler
, sampler
, _sampler
);
1635 anv_free2(&device
->alloc
, pAllocator
, sampler
);
1638 VkResult
anv_CreateFramebuffer(
1640 const VkFramebufferCreateInfo
* pCreateInfo
,
1641 const VkAllocationCallbacks
* pAllocator
,
1642 VkFramebuffer
* pFramebuffer
)
1644 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1645 struct anv_framebuffer
*framebuffer
;
1647 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO
);
1649 size_t size
= sizeof(*framebuffer
) +
1650 sizeof(struct anv_image_view
*) * pCreateInfo
->attachmentCount
;
1651 framebuffer
= anv_alloc2(&device
->alloc
, pAllocator
, size
, 8,
1652 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
1653 if (framebuffer
== NULL
)
1654 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
1656 framebuffer
->attachment_count
= pCreateInfo
->attachmentCount
;
1657 for (uint32_t i
= 0; i
< pCreateInfo
->attachmentCount
; i
++) {
1658 VkImageView _iview
= pCreateInfo
->pAttachments
[i
];
1659 framebuffer
->attachments
[i
] = anv_image_view_from_handle(_iview
);
1662 framebuffer
->width
= pCreateInfo
->width
;
1663 framebuffer
->height
= pCreateInfo
->height
;
1664 framebuffer
->layers
= pCreateInfo
->layers
;
1666 *pFramebuffer
= anv_framebuffer_to_handle(framebuffer
);
1671 void anv_DestroyFramebuffer(
1674 const VkAllocationCallbacks
* pAllocator
)
1676 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1677 ANV_FROM_HANDLE(anv_framebuffer
, fb
, _fb
);
1679 anv_free2(&device
->alloc
, pAllocator
, fb
);
1682 void vkCmdDbgMarkerBegin(
1683 VkCommandBuffer commandBuffer
,
1684 const char* pMarker
)
1685 __attribute__ ((visibility ("default")));
1687 void vkCmdDbgMarkerEnd(
1688 VkCommandBuffer commandBuffer
)
1689 __attribute__ ((visibility ("default")));
1691 void vkCmdDbgMarkerBegin(
1692 VkCommandBuffer commandBuffer
,
1693 const char* pMarker
)
1697 void vkCmdDbgMarkerEnd(
1698 VkCommandBuffer commandBuffer
)