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, 2)) {
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,
391 void anv_GetPhysicalDeviceProperties(
392 VkPhysicalDevice physicalDevice
,
393 VkPhysicalDeviceProperties
* pProperties
)
395 ANV_FROM_HANDLE(anv_physical_device
, pdevice
, physicalDevice
);
396 const struct brw_device_info
*devinfo
= pdevice
->info
;
398 anv_finishme("Get correct values for VkPhysicalDeviceLimits");
400 const float time_stamp_base
= devinfo
->gen
>= 9 ? 83.333 : 80.0;
402 VkSampleCountFlags sample_counts
=
403 isl_device_get_sample_counts(&pdevice
->isl_dev
);
405 VkPhysicalDeviceLimits limits
= {
406 .maxImageDimension1D
= (1 << 14),
407 .maxImageDimension2D
= (1 << 14),
408 .maxImageDimension3D
= (1 << 10),
409 .maxImageDimensionCube
= (1 << 14),
410 .maxImageArrayLayers
= (1 << 10),
411 .maxTexelBufferElements
= 128 * 1024 * 1024,
412 .maxUniformBufferRange
= UINT32_MAX
,
413 .maxStorageBufferRange
= UINT32_MAX
,
414 .maxPushConstantsSize
= MAX_PUSH_CONSTANTS_SIZE
,
415 .maxMemoryAllocationCount
= UINT32_MAX
,
416 .maxSamplerAllocationCount
= 64 * 1024,
417 .bufferImageGranularity
= 64, /* A cache line */
418 .sparseAddressSpaceSize
= 0,
419 .maxBoundDescriptorSets
= MAX_SETS
,
420 .maxPerStageDescriptorSamplers
= 64,
421 .maxPerStageDescriptorUniformBuffers
= 64,
422 .maxPerStageDescriptorStorageBuffers
= 64,
423 .maxPerStageDescriptorSampledImages
= 64,
424 .maxPerStageDescriptorStorageImages
= 64,
425 .maxPerStageDescriptorInputAttachments
= 64,
426 .maxPerStageResources
= 128,
427 .maxDescriptorSetSamplers
= 256,
428 .maxDescriptorSetUniformBuffers
= 256,
429 .maxDescriptorSetUniformBuffersDynamic
= 256,
430 .maxDescriptorSetStorageBuffers
= 256,
431 .maxDescriptorSetStorageBuffersDynamic
= 256,
432 .maxDescriptorSetSampledImages
= 256,
433 .maxDescriptorSetStorageImages
= 256,
434 .maxDescriptorSetInputAttachments
= 256,
435 .maxVertexInputAttributes
= 32,
436 .maxVertexInputBindings
= 32,
437 .maxVertexInputAttributeOffset
= 2047,
438 .maxVertexInputBindingStride
= 2048,
439 .maxVertexOutputComponents
= 128,
440 .maxTessellationGenerationLevel
= 0,
441 .maxTessellationPatchSize
= 0,
442 .maxTessellationControlPerVertexInputComponents
= 0,
443 .maxTessellationControlPerVertexOutputComponents
= 0,
444 .maxTessellationControlPerPatchOutputComponents
= 0,
445 .maxTessellationControlTotalOutputComponents
= 0,
446 .maxTessellationEvaluationInputComponents
= 0,
447 .maxTessellationEvaluationOutputComponents
= 0,
448 .maxGeometryShaderInvocations
= 32,
449 .maxGeometryInputComponents
= 64,
450 .maxGeometryOutputComponents
= 128,
451 .maxGeometryOutputVertices
= 256,
452 .maxGeometryTotalOutputComponents
= 1024,
453 .maxFragmentInputComponents
= 128,
454 .maxFragmentOutputAttachments
= 8,
455 .maxFragmentDualSrcAttachments
= 2,
456 .maxFragmentCombinedOutputResources
= 8,
457 .maxComputeSharedMemorySize
= 32768,
458 .maxComputeWorkGroupCount
= { 65535, 65535, 65535 },
459 .maxComputeWorkGroupInvocations
= 16 * devinfo
->max_cs_threads
,
460 .maxComputeWorkGroupSize
= {
461 16 * devinfo
->max_cs_threads
,
462 16 * devinfo
->max_cs_threads
,
463 16 * devinfo
->max_cs_threads
,
465 .subPixelPrecisionBits
= 4 /* FIXME */,
466 .subTexelPrecisionBits
= 4 /* FIXME */,
467 .mipmapPrecisionBits
= 4 /* FIXME */,
468 .maxDrawIndexedIndexValue
= UINT32_MAX
,
469 .maxDrawIndirectCount
= UINT32_MAX
,
470 .maxSamplerLodBias
= 16,
471 .maxSamplerAnisotropy
= 16,
472 .maxViewports
= MAX_VIEWPORTS
,
473 .maxViewportDimensions
= { (1 << 14), (1 << 14) },
474 .viewportBoundsRange
= { -16384.0, 16384.0 },
475 .viewportSubPixelBits
= 13, /* We take a float? */
476 .minMemoryMapAlignment
= 4096, /* A page */
477 .minTexelBufferOffsetAlignment
= 1,
478 .minUniformBufferOffsetAlignment
= 1,
479 .minStorageBufferOffsetAlignment
= 1,
480 .minTexelOffset
= -8,
482 .minTexelGatherOffset
= -8,
483 .maxTexelGatherOffset
= 7,
484 .minInterpolationOffset
= 0, /* FIXME */
485 .maxInterpolationOffset
= 0, /* FIXME */
486 .subPixelInterpolationOffsetBits
= 0, /* FIXME */
487 .maxFramebufferWidth
= (1 << 14),
488 .maxFramebufferHeight
= (1 << 14),
489 .maxFramebufferLayers
= (1 << 10),
490 .framebufferColorSampleCounts
= sample_counts
,
491 .framebufferDepthSampleCounts
= sample_counts
,
492 .framebufferStencilSampleCounts
= sample_counts
,
493 .framebufferNoAttachmentsSampleCounts
= sample_counts
,
494 .maxColorAttachments
= MAX_RTS
,
495 .sampledImageColorSampleCounts
= sample_counts
,
496 .sampledImageIntegerSampleCounts
= VK_SAMPLE_COUNT_1_BIT
,
497 .sampledImageDepthSampleCounts
= sample_counts
,
498 .sampledImageStencilSampleCounts
= sample_counts
,
499 .storageImageSampleCounts
= VK_SAMPLE_COUNT_1_BIT
,
500 .maxSampleMaskWords
= 1,
501 .timestampComputeAndGraphics
= false,
502 .timestampPeriod
= time_stamp_base
/ (1000 * 1000 * 1000),
503 .maxClipDistances
= 0 /* FIXME */,
504 .maxCullDistances
= 0 /* FIXME */,
505 .maxCombinedClipAndCullDistances
= 0 /* FIXME */,
506 .discreteQueuePriorities
= 1,
507 .pointSizeRange
= { 0.125, 255.875 },
508 .lineWidthRange
= { 0.0, 7.9921875 },
509 .pointSizeGranularity
= (1.0 / 8.0),
510 .lineWidthGranularity
= (1.0 / 128.0),
511 .strictLines
= false, /* FINISHME */
512 .standardSampleLocations
= true,
513 .optimalBufferCopyOffsetAlignment
= 128,
514 .optimalBufferCopyRowPitchAlignment
= 128,
515 .nonCoherentAtomSize
= 64,
518 *pProperties
= (VkPhysicalDeviceProperties
) {
519 .apiVersion
= VK_MAKE_VERSION(1, 0, 2),
522 .deviceID
= pdevice
->chipset_id
,
523 .deviceType
= VK_PHYSICAL_DEVICE_TYPE_INTEGRATED_GPU
,
525 .sparseProperties
= {0}, /* Broadwell doesn't do sparse. */
528 strcpy(pProperties
->deviceName
, pdevice
->name
);
529 snprintf((char *)pProperties
->pipelineCacheUUID
, VK_UUID_SIZE
,
530 "anv-%s", MESA_GIT_SHA1
+ 4);
533 void anv_GetPhysicalDeviceQueueFamilyProperties(
534 VkPhysicalDevice physicalDevice
,
536 VkQueueFamilyProperties
* pQueueFamilyProperties
)
538 if (pQueueFamilyProperties
== NULL
) {
543 assert(*pCount
>= 1);
545 *pQueueFamilyProperties
= (VkQueueFamilyProperties
) {
546 .queueFlags
= VK_QUEUE_GRAPHICS_BIT
|
547 VK_QUEUE_COMPUTE_BIT
|
548 VK_QUEUE_TRANSFER_BIT
,
550 .timestampValidBits
= 36, /* XXX: Real value here */
551 .minImageTransferGranularity
= (VkExtent3D
) { 1, 1, 1 },
555 void anv_GetPhysicalDeviceMemoryProperties(
556 VkPhysicalDevice physicalDevice
,
557 VkPhysicalDeviceMemoryProperties
* pMemoryProperties
)
559 ANV_FROM_HANDLE(anv_physical_device
, physical_device
, physicalDevice
);
560 VkDeviceSize heap_size
;
562 /* Reserve some wiggle room for the driver by exposing only 75% of the
563 * aperture to the heap.
565 heap_size
= 3 * physical_device
->aperture_size
/ 4;
567 if (physical_device
->info
->has_llc
) {
568 /* Big core GPUs share LLC with the CPU and thus one memory type can be
569 * both cached and coherent at the same time.
571 pMemoryProperties
->memoryTypeCount
= 1;
572 pMemoryProperties
->memoryTypes
[0] = (VkMemoryType
) {
573 .propertyFlags
= VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT
|
574 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT
|
575 VK_MEMORY_PROPERTY_HOST_COHERENT_BIT
|
576 VK_MEMORY_PROPERTY_HOST_CACHED_BIT
,
580 /* The spec requires that we expose a host-visible, coherent memory
581 * type, but Atom GPUs don't share LLC. Thus we offer two memory types
582 * to give the application a choice between cached, but not coherent and
583 * coherent but uncached (WC though).
585 pMemoryProperties
->memoryTypeCount
= 2;
586 pMemoryProperties
->memoryTypes
[0] = (VkMemoryType
) {
587 .propertyFlags
= VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT
|
588 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT
|
589 VK_MEMORY_PROPERTY_HOST_COHERENT_BIT
,
592 pMemoryProperties
->memoryTypes
[1] = (VkMemoryType
) {
593 .propertyFlags
= VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT
|
594 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT
|
595 VK_MEMORY_PROPERTY_HOST_CACHED_BIT
,
600 pMemoryProperties
->memoryHeapCount
= 1;
601 pMemoryProperties
->memoryHeaps
[0] = (VkMemoryHeap
) {
603 .flags
= VK_MEMORY_HEAP_DEVICE_LOCAL_BIT
,
607 PFN_vkVoidFunction
anv_GetInstanceProcAddr(
611 return anv_lookup_entrypoint(pName
);
614 PFN_vkVoidFunction
anv_GetDeviceProcAddr(
618 return anv_lookup_entrypoint(pName
);
622 anv_queue_init(struct anv_device
*device
, struct anv_queue
*queue
)
624 queue
->_loader_data
.loaderMagic
= ICD_LOADER_MAGIC
;
625 queue
->device
= device
;
626 queue
->pool
= &device
->surface_state_pool
;
632 anv_queue_finish(struct anv_queue
*queue
)
636 static struct anv_state
637 anv_state_pool_emit_data(struct anv_state_pool
*pool
, size_t size
, size_t align
, const void *p
)
639 struct anv_state state
;
641 state
= anv_state_pool_alloc(pool
, size
, align
);
642 memcpy(state
.map
, p
, size
);
644 if (!pool
->block_pool
->device
->info
.has_llc
)
645 anv_state_clflush(state
);
650 struct gen8_border_color
{
655 /* Pad out to 64 bytes */
660 anv_device_init_border_colors(struct anv_device
*device
)
662 static const struct gen8_border_color border_colors
[] = {
663 [VK_BORDER_COLOR_FLOAT_TRANSPARENT_BLACK
] = { .float32
= { 0.0, 0.0, 0.0, 0.0 } },
664 [VK_BORDER_COLOR_FLOAT_OPAQUE_BLACK
] = { .float32
= { 0.0, 0.0, 0.0, 1.0 } },
665 [VK_BORDER_COLOR_FLOAT_OPAQUE_WHITE
] = { .float32
= { 1.0, 1.0, 1.0, 1.0 } },
666 [VK_BORDER_COLOR_INT_TRANSPARENT_BLACK
] = { .uint32
= { 0, 0, 0, 0 } },
667 [VK_BORDER_COLOR_INT_OPAQUE_BLACK
] = { .uint32
= { 0, 0, 0, 1 } },
668 [VK_BORDER_COLOR_INT_OPAQUE_WHITE
] = { .uint32
= { 1, 1, 1, 1 } },
671 device
->border_colors
= anv_state_pool_emit_data(&device
->dynamic_state_pool
,
672 sizeof(border_colors
), 64,
676 VkResult
anv_CreateDevice(
677 VkPhysicalDevice physicalDevice
,
678 const VkDeviceCreateInfo
* pCreateInfo
,
679 const VkAllocationCallbacks
* pAllocator
,
682 ANV_FROM_HANDLE(anv_physical_device
, physical_device
, physicalDevice
);
684 struct anv_device
*device
;
686 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO
);
688 for (uint32_t i
= 0; i
< pCreateInfo
->enabledExtensionCount
; i
++) {
690 for (uint32_t j
= 0; j
< ARRAY_SIZE(device_extensions
); j
++) {
691 if (strcmp(pCreateInfo
->ppEnabledExtensionNames
[i
],
692 device_extensions
[j
].extensionName
) == 0) {
698 return vk_error(VK_ERROR_EXTENSION_NOT_PRESENT
);
701 anv_set_dispatch_devinfo(physical_device
->info
);
703 device
= anv_alloc2(&physical_device
->instance
->alloc
, pAllocator
,
705 VK_SYSTEM_ALLOCATION_SCOPE_DEVICE
);
707 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
709 device
->_loader_data
.loaderMagic
= ICD_LOADER_MAGIC
;
710 device
->instance
= physical_device
->instance
;
713 device
->alloc
= *pAllocator
;
715 device
->alloc
= physical_device
->instance
->alloc
;
717 /* XXX(chadv): Can we dup() physicalDevice->fd here? */
718 device
->fd
= open(physical_device
->path
, O_RDWR
| O_CLOEXEC
);
719 if (device
->fd
== -1) {
720 result
= vk_error(VK_ERROR_INITIALIZATION_FAILED
);
724 device
->context_id
= anv_gem_create_context(device
);
725 if (device
->context_id
== -1) {
726 result
= vk_error(VK_ERROR_INITIALIZATION_FAILED
);
730 device
->info
= *physical_device
->info
;
731 device
->isl_dev
= physical_device
->isl_dev
;
733 pthread_mutex_init(&device
->mutex
, NULL
);
735 anv_bo_pool_init(&device
->batch_bo_pool
, device
, ANV_CMD_BUFFER_BATCH_SIZE
);
737 anv_block_pool_init(&device
->dynamic_state_block_pool
, device
, 16384);
739 anv_state_pool_init(&device
->dynamic_state_pool
,
740 &device
->dynamic_state_block_pool
);
742 anv_block_pool_init(&device
->instruction_block_pool
, device
, 128 * 1024);
743 anv_pipeline_cache_init(&device
->default_pipeline_cache
, device
);
745 anv_block_pool_init(&device
->surface_state_block_pool
, device
, 4096);
747 anv_state_pool_init(&device
->surface_state_pool
,
748 &device
->surface_state_block_pool
);
750 anv_bo_init_new(&device
->workaround_bo
, device
, 1024);
752 anv_block_pool_init(&device
->scratch_block_pool
, device
, 0x10000);
754 anv_queue_init(device
, &device
->queue
);
756 result
= anv_device_init_meta(device
);
757 if (result
!= VK_SUCCESS
)
760 anv_device_init_border_colors(device
);
762 *pDevice
= anv_device_to_handle(device
);
769 anv_free(&device
->alloc
, device
);
774 void anv_DestroyDevice(
776 const VkAllocationCallbacks
* pAllocator
)
778 ANV_FROM_HANDLE(anv_device
, device
, _device
);
780 anv_queue_finish(&device
->queue
);
782 anv_device_finish_meta(device
);
785 /* We only need to free these to prevent valgrind errors. The backing
786 * BO will go away in a couple of lines so we don't actually leak.
788 anv_state_pool_free(&device
->dynamic_state_pool
, device
->border_colors
);
791 anv_gem_munmap(device
->workaround_bo
.map
, device
->workaround_bo
.size
);
792 anv_gem_close(device
, device
->workaround_bo
.gem_handle
);
794 anv_bo_pool_finish(&device
->batch_bo_pool
);
795 anv_state_pool_finish(&device
->dynamic_state_pool
);
796 anv_block_pool_finish(&device
->dynamic_state_block_pool
);
797 anv_block_pool_finish(&device
->instruction_block_pool
);
798 anv_state_pool_finish(&device
->surface_state_pool
);
799 anv_block_pool_finish(&device
->surface_state_block_pool
);
800 anv_block_pool_finish(&device
->scratch_block_pool
);
804 pthread_mutex_destroy(&device
->mutex
);
806 anv_free(&device
->alloc
, device
);
809 VkResult
anv_EnumerateInstanceExtensionProperties(
810 const char* pLayerName
,
811 uint32_t* pPropertyCount
,
812 VkExtensionProperties
* pProperties
)
814 if (pProperties
== NULL
) {
815 *pPropertyCount
= ARRAY_SIZE(global_extensions
);
819 assert(*pPropertyCount
>= ARRAY_SIZE(global_extensions
));
821 *pPropertyCount
= ARRAY_SIZE(global_extensions
);
822 memcpy(pProperties
, global_extensions
, sizeof(global_extensions
));
827 VkResult
anv_EnumerateDeviceExtensionProperties(
828 VkPhysicalDevice physicalDevice
,
829 const char* pLayerName
,
830 uint32_t* pPropertyCount
,
831 VkExtensionProperties
* pProperties
)
833 if (pProperties
== NULL
) {
834 *pPropertyCount
= ARRAY_SIZE(device_extensions
);
838 assert(*pPropertyCount
>= ARRAY_SIZE(device_extensions
));
840 *pPropertyCount
= ARRAY_SIZE(device_extensions
);
841 memcpy(pProperties
, device_extensions
, sizeof(device_extensions
));
846 VkResult
anv_EnumerateInstanceLayerProperties(
847 uint32_t* pPropertyCount
,
848 VkLayerProperties
* pProperties
)
850 if (pProperties
== NULL
) {
855 /* None supported at this time */
856 return vk_error(VK_ERROR_LAYER_NOT_PRESENT
);
859 VkResult
anv_EnumerateDeviceLayerProperties(
860 VkPhysicalDevice physicalDevice
,
861 uint32_t* pPropertyCount
,
862 VkLayerProperties
* pProperties
)
864 if (pProperties
== NULL
) {
869 /* None supported at this time */
870 return vk_error(VK_ERROR_LAYER_NOT_PRESENT
);
873 void anv_GetDeviceQueue(
875 uint32_t queueNodeIndex
,
879 ANV_FROM_HANDLE(anv_device
, device
, _device
);
881 assert(queueIndex
== 0);
883 *pQueue
= anv_queue_to_handle(&device
->queue
);
886 VkResult
anv_QueueSubmit(
888 uint32_t submitCount
,
889 const VkSubmitInfo
* pSubmits
,
892 ANV_FROM_HANDLE(anv_queue
, queue
, _queue
);
893 ANV_FROM_HANDLE(anv_fence
, fence
, _fence
);
894 struct anv_device
*device
= queue
->device
;
897 for (uint32_t i
= 0; i
< submitCount
; i
++) {
898 for (uint32_t j
= 0; j
< pSubmits
[i
].commandBufferCount
; j
++) {
899 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
,
900 pSubmits
[i
].pCommandBuffers
[j
]);
901 assert(cmd_buffer
->level
== VK_COMMAND_BUFFER_LEVEL_PRIMARY
);
903 ret
= anv_gem_execbuffer(device
, &cmd_buffer
->execbuf2
.execbuf
);
905 /* We don't know the real error. */
906 return vk_errorf(VK_ERROR_OUT_OF_DEVICE_MEMORY
,
907 "execbuf2 failed: %m");
911 ret
= anv_gem_execbuffer(device
, &fence
->execbuf
);
913 /* We don't know the real error. */
914 return vk_errorf(VK_ERROR_OUT_OF_DEVICE_MEMORY
,
915 "execbuf2 failed: %m");
919 for (uint32_t k
= 0; k
< cmd_buffer
->execbuf2
.bo_count
; k
++)
920 cmd_buffer
->execbuf2
.bos
[k
]->offset
= cmd_buffer
->execbuf2
.objects
[k
].offset
;
927 VkResult
anv_QueueWaitIdle(
930 ANV_FROM_HANDLE(anv_queue
, queue
, _queue
);
932 return ANV_CALL(DeviceWaitIdle
)(anv_device_to_handle(queue
->device
));
935 VkResult
anv_DeviceWaitIdle(
938 ANV_FROM_HANDLE(anv_device
, device
, _device
);
939 struct anv_state state
;
940 struct anv_batch batch
;
941 struct drm_i915_gem_execbuffer2 execbuf
;
942 struct drm_i915_gem_exec_object2 exec2_objects
[1];
943 struct anv_bo
*bo
= NULL
;
948 state
= anv_state_pool_alloc(&device
->dynamic_state_pool
, 32, 32);
949 bo
= &device
->dynamic_state_pool
.block_pool
->bo
;
950 batch
.start
= batch
.next
= state
.map
;
951 batch
.end
= state
.map
+ 32;
952 anv_batch_emit(&batch
, GEN7_MI_BATCH_BUFFER_END
);
953 anv_batch_emit(&batch
, GEN7_MI_NOOP
);
955 if (!device
->info
.has_llc
)
956 anv_state_clflush(state
);
958 exec2_objects
[0].handle
= bo
->gem_handle
;
959 exec2_objects
[0].relocation_count
= 0;
960 exec2_objects
[0].relocs_ptr
= 0;
961 exec2_objects
[0].alignment
= 0;
962 exec2_objects
[0].offset
= bo
->offset
;
963 exec2_objects
[0].flags
= 0;
964 exec2_objects
[0].rsvd1
= 0;
965 exec2_objects
[0].rsvd2
= 0;
967 execbuf
.buffers_ptr
= (uintptr_t) exec2_objects
;
968 execbuf
.buffer_count
= 1;
969 execbuf
.batch_start_offset
= state
.offset
;
970 execbuf
.batch_len
= batch
.next
- state
.map
;
971 execbuf
.cliprects_ptr
= 0;
972 execbuf
.num_cliprects
= 0;
977 I915_EXEC_HANDLE_LUT
| I915_EXEC_NO_RELOC
| I915_EXEC_RENDER
;
978 execbuf
.rsvd1
= device
->context_id
;
981 ret
= anv_gem_execbuffer(device
, &execbuf
);
983 /* We don't know the real error. */
984 result
= vk_errorf(VK_ERROR_OUT_OF_DEVICE_MEMORY
, "execbuf2 failed: %m");
989 ret
= anv_gem_wait(device
, bo
->gem_handle
, &timeout
);
991 /* We don't know the real error. */
992 result
= vk_errorf(VK_ERROR_OUT_OF_DEVICE_MEMORY
, "execbuf2 failed: %m");
996 anv_state_pool_free(&device
->dynamic_state_pool
, state
);
1001 anv_state_pool_free(&device
->dynamic_state_pool
, state
);
1007 anv_bo_init_new(struct anv_bo
*bo
, struct anv_device
*device
, uint64_t size
)
1009 bo
->gem_handle
= anv_gem_create(device
, size
);
1010 if (!bo
->gem_handle
)
1011 return vk_error(VK_ERROR_OUT_OF_DEVICE_MEMORY
);
1021 VkResult
anv_AllocateMemory(
1023 const VkMemoryAllocateInfo
* pAllocateInfo
,
1024 const VkAllocationCallbacks
* pAllocator
,
1025 VkDeviceMemory
* pMem
)
1027 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1028 struct anv_device_memory
*mem
;
1031 assert(pAllocateInfo
->sType
== VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO
);
1033 if (pAllocateInfo
->allocationSize
== 0) {
1034 /* Apparently, this is allowed */
1035 *pMem
= VK_NULL_HANDLE
;
1039 /* We support exactly one memory heap. */
1040 assert(pAllocateInfo
->memoryTypeIndex
== 0 ||
1041 (!device
->info
.has_llc
&& pAllocateInfo
->memoryTypeIndex
< 2));
1043 /* FINISHME: Fail if allocation request exceeds heap size. */
1045 mem
= anv_alloc2(&device
->alloc
, pAllocator
, sizeof(*mem
), 8,
1046 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
1048 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
1050 /* The kernel is going to give us whole pages anyway */
1051 uint64_t alloc_size
= align_u64(pAllocateInfo
->allocationSize
, 4096);
1053 result
= anv_bo_init_new(&mem
->bo
, device
, alloc_size
);
1054 if (result
!= VK_SUCCESS
)
1057 mem
->type_index
= pAllocateInfo
->memoryTypeIndex
;
1059 *pMem
= anv_device_memory_to_handle(mem
);
1064 anv_free2(&device
->alloc
, pAllocator
, mem
);
1069 void anv_FreeMemory(
1071 VkDeviceMemory _mem
,
1072 const VkAllocationCallbacks
* pAllocator
)
1074 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1075 ANV_FROM_HANDLE(anv_device_memory
, mem
, _mem
);
1081 anv_gem_munmap(mem
->bo
.map
, mem
->bo
.size
);
1083 if (mem
->bo
.gem_handle
!= 0)
1084 anv_gem_close(device
, mem
->bo
.gem_handle
);
1086 anv_free2(&device
->alloc
, pAllocator
, mem
);
1089 VkResult
anv_MapMemory(
1091 VkDeviceMemory _memory
,
1092 VkDeviceSize offset
,
1094 VkMemoryMapFlags flags
,
1097 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1098 ANV_FROM_HANDLE(anv_device_memory
, mem
, _memory
);
1105 if (size
== VK_WHOLE_SIZE
)
1106 size
= mem
->bo
.size
- offset
;
1108 /* FIXME: Is this supposed to be thread safe? Since vkUnmapMemory() only
1109 * takes a VkDeviceMemory pointer, it seems like only one map of the memory
1110 * at a time is valid. We could just mmap up front and return an offset
1111 * pointer here, but that may exhaust virtual memory on 32 bit
1114 uint32_t gem_flags
= 0;
1115 if (!device
->info
.has_llc
&& mem
->type_index
== 0)
1116 gem_flags
|= I915_MMAP_WC
;
1118 /* GEM will fail to map if the offset isn't 4k-aligned. Round down. */
1119 uint64_t map_offset
= offset
& ~4095ull;
1120 assert(offset
>= map_offset
);
1121 uint64_t map_size
= (offset
+ size
) - map_offset
;
1123 /* Let's map whole pages */
1124 map_size
= align_u64(map_size
, 4096);
1126 mem
->map
= anv_gem_mmap(device
, mem
->bo
.gem_handle
,
1127 map_offset
, map_size
, gem_flags
);
1128 mem
->map_size
= map_size
;
1130 *ppData
= mem
->map
+ (offset
- map_offset
);
1135 void anv_UnmapMemory(
1137 VkDeviceMemory _memory
)
1139 ANV_FROM_HANDLE(anv_device_memory
, mem
, _memory
);
1144 anv_gem_munmap(mem
->map
, mem
->map_size
);
1148 clflush_mapped_ranges(struct anv_device
*device
,
1150 const VkMappedMemoryRange
*ranges
)
1152 for (uint32_t i
= 0; i
< count
; i
++) {
1153 ANV_FROM_HANDLE(anv_device_memory
, mem
, ranges
[i
].memory
);
1154 void *p
= mem
->map
+ (ranges
[i
].offset
& ~CACHELINE_MASK
);
1157 if (ranges
[i
].offset
+ ranges
[i
].size
> mem
->map_size
)
1158 end
= mem
->map
+ mem
->map_size
;
1160 end
= mem
->map
+ ranges
[i
].offset
+ ranges
[i
].size
;
1163 __builtin_ia32_clflush(p
);
1164 p
+= CACHELINE_SIZE
;
1169 VkResult
anv_FlushMappedMemoryRanges(
1171 uint32_t memoryRangeCount
,
1172 const VkMappedMemoryRange
* pMemoryRanges
)
1174 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1176 if (device
->info
.has_llc
)
1179 /* Make sure the writes we're flushing have landed. */
1180 __builtin_ia32_mfence();
1182 clflush_mapped_ranges(device
, memoryRangeCount
, pMemoryRanges
);
1187 VkResult
anv_InvalidateMappedMemoryRanges(
1189 uint32_t memoryRangeCount
,
1190 const VkMappedMemoryRange
* pMemoryRanges
)
1192 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1194 if (device
->info
.has_llc
)
1197 clflush_mapped_ranges(device
, memoryRangeCount
, pMemoryRanges
);
1199 /* Make sure no reads get moved up above the invalidate. */
1200 __builtin_ia32_mfence();
1205 void anv_GetBufferMemoryRequirements(
1208 VkMemoryRequirements
* pMemoryRequirements
)
1210 ANV_FROM_HANDLE(anv_buffer
, buffer
, _buffer
);
1212 /* The Vulkan spec (git aaed022) says:
1214 * memoryTypeBits is a bitfield and contains one bit set for every
1215 * supported memory type for the resource. The bit `1<<i` is set if and
1216 * only if the memory type `i` in the VkPhysicalDeviceMemoryProperties
1217 * structure for the physical device is supported.
1219 * We support exactly one memory type.
1221 pMemoryRequirements
->memoryTypeBits
= 1;
1223 pMemoryRequirements
->size
= buffer
->size
;
1224 pMemoryRequirements
->alignment
= 16;
1227 void anv_GetImageMemoryRequirements(
1230 VkMemoryRequirements
* pMemoryRequirements
)
1232 ANV_FROM_HANDLE(anv_image
, image
, _image
);
1234 /* The Vulkan spec (git aaed022) says:
1236 * memoryTypeBits is a bitfield and contains one bit set for every
1237 * supported memory type for the resource. The bit `1<<i` is set if and
1238 * only if the memory type `i` in the VkPhysicalDeviceMemoryProperties
1239 * structure for the physical device is supported.
1241 * We support exactly one memory type.
1243 pMemoryRequirements
->memoryTypeBits
= 1;
1245 pMemoryRequirements
->size
= image
->size
;
1246 pMemoryRequirements
->alignment
= image
->alignment
;
1249 void anv_GetImageSparseMemoryRequirements(
1252 uint32_t* pSparseMemoryRequirementCount
,
1253 VkSparseImageMemoryRequirements
* pSparseMemoryRequirements
)
1258 void anv_GetDeviceMemoryCommitment(
1260 VkDeviceMemory memory
,
1261 VkDeviceSize
* pCommittedMemoryInBytes
)
1263 *pCommittedMemoryInBytes
= 0;
1266 VkResult
anv_BindBufferMemory(
1269 VkDeviceMemory _memory
,
1270 VkDeviceSize memoryOffset
)
1272 ANV_FROM_HANDLE(anv_device_memory
, mem
, _memory
);
1273 ANV_FROM_HANDLE(anv_buffer
, buffer
, _buffer
);
1276 buffer
->bo
= &mem
->bo
;
1277 buffer
->offset
= memoryOffset
;
1286 VkResult
anv_BindImageMemory(
1289 VkDeviceMemory _memory
,
1290 VkDeviceSize memoryOffset
)
1292 ANV_FROM_HANDLE(anv_device_memory
, mem
, _memory
);
1293 ANV_FROM_HANDLE(anv_image
, image
, _image
);
1296 image
->bo
= &mem
->bo
;
1297 image
->offset
= memoryOffset
;
1306 VkResult
anv_QueueBindSparse(
1308 uint32_t bindInfoCount
,
1309 const VkBindSparseInfo
* pBindInfo
,
1312 stub_return(VK_ERROR_INCOMPATIBLE_DRIVER
);
1315 VkResult
anv_CreateFence(
1317 const VkFenceCreateInfo
* pCreateInfo
,
1318 const VkAllocationCallbacks
* pAllocator
,
1321 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1322 struct anv_fence
*fence
;
1323 struct anv_batch batch
;
1326 const uint32_t fence_size
= 128;
1328 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_FENCE_CREATE_INFO
);
1330 fence
= anv_alloc2(&device
->alloc
, pAllocator
, sizeof(*fence
), 8,
1331 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
1333 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
1335 result
= anv_bo_init_new(&fence
->bo
, device
, fence_size
);
1336 if (result
!= VK_SUCCESS
)
1340 anv_gem_mmap(device
, fence
->bo
.gem_handle
, 0, fence
->bo
.size
, 0);
1341 batch
.next
= batch
.start
= fence
->bo
.map
;
1342 batch
.end
= fence
->bo
.map
+ fence
->bo
.size
;
1343 anv_batch_emit(&batch
, GEN7_MI_BATCH_BUFFER_END
);
1344 anv_batch_emit(&batch
, GEN7_MI_NOOP
);
1346 if (!device
->info
.has_llc
) {
1347 assert(((uintptr_t) fence
->bo
.map
& CACHELINE_MASK
) == 0);
1348 assert(batch
.next
- fence
->bo
.map
<= CACHELINE_SIZE
);
1349 __builtin_ia32_mfence();
1350 __builtin_ia32_clflush(fence
->bo
.map
);
1353 fence
->exec2_objects
[0].handle
= fence
->bo
.gem_handle
;
1354 fence
->exec2_objects
[0].relocation_count
= 0;
1355 fence
->exec2_objects
[0].relocs_ptr
= 0;
1356 fence
->exec2_objects
[0].alignment
= 0;
1357 fence
->exec2_objects
[0].offset
= fence
->bo
.offset
;
1358 fence
->exec2_objects
[0].flags
= 0;
1359 fence
->exec2_objects
[0].rsvd1
= 0;
1360 fence
->exec2_objects
[0].rsvd2
= 0;
1362 fence
->execbuf
.buffers_ptr
= (uintptr_t) fence
->exec2_objects
;
1363 fence
->execbuf
.buffer_count
= 1;
1364 fence
->execbuf
.batch_start_offset
= 0;
1365 fence
->execbuf
.batch_len
= batch
.next
- fence
->bo
.map
;
1366 fence
->execbuf
.cliprects_ptr
= 0;
1367 fence
->execbuf
.num_cliprects
= 0;
1368 fence
->execbuf
.DR1
= 0;
1369 fence
->execbuf
.DR4
= 0;
1371 fence
->execbuf
.flags
=
1372 I915_EXEC_HANDLE_LUT
| I915_EXEC_NO_RELOC
| I915_EXEC_RENDER
;
1373 fence
->execbuf
.rsvd1
= device
->context_id
;
1374 fence
->execbuf
.rsvd2
= 0;
1376 fence
->ready
= false;
1378 *pFence
= anv_fence_to_handle(fence
);
1383 anv_free2(&device
->alloc
, pAllocator
, fence
);
1388 void anv_DestroyFence(
1391 const VkAllocationCallbacks
* pAllocator
)
1393 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1394 ANV_FROM_HANDLE(anv_fence
, fence
, _fence
);
1396 anv_gem_munmap(fence
->bo
.map
, fence
->bo
.size
);
1397 anv_gem_close(device
, fence
->bo
.gem_handle
);
1398 anv_free2(&device
->alloc
, pAllocator
, fence
);
1401 VkResult
anv_ResetFences(
1403 uint32_t fenceCount
,
1404 const VkFence
* pFences
)
1406 for (uint32_t i
= 0; i
< fenceCount
; i
++) {
1407 ANV_FROM_HANDLE(anv_fence
, fence
, pFences
[i
]);
1408 fence
->ready
= false;
1414 VkResult
anv_GetFenceStatus(
1418 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1419 ANV_FROM_HANDLE(anv_fence
, fence
, _fence
);
1426 ret
= anv_gem_wait(device
, fence
->bo
.gem_handle
, &t
);
1428 fence
->ready
= true;
1432 return VK_NOT_READY
;
1435 VkResult
anv_WaitForFences(
1437 uint32_t fenceCount
,
1438 const VkFence
* pFences
,
1442 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1444 /* DRM_IOCTL_I915_GEM_WAIT uses a signed 64 bit timeout and is supposed
1445 * to block indefinitely timeouts <= 0. Unfortunately, this was broken
1446 * for a couple of kernel releases. Since there's no way to know
1447 * whether or not the kernel we're using is one of the broken ones, the
1448 * best we can do is to clamp the timeout to INT64_MAX. This limits the
1449 * maximum timeout from 584 years to 292 years - likely not a big deal.
1451 if (timeout
> INT64_MAX
)
1452 timeout
= INT64_MAX
;
1454 int64_t t
= timeout
;
1456 /* FIXME: handle !waitAll */
1458 for (uint32_t i
= 0; i
< fenceCount
; i
++) {
1459 ANV_FROM_HANDLE(anv_fence
, fence
, pFences
[i
]);
1460 int ret
= anv_gem_wait(device
, fence
->bo
.gem_handle
, &t
);
1461 if (ret
== -1 && errno
== ETIME
) {
1463 } else if (ret
== -1) {
1464 /* We don't know the real error. */
1465 return vk_errorf(VK_ERROR_OUT_OF_DEVICE_MEMORY
,
1466 "gem wait failed: %m");
1473 // Queue semaphore functions
1475 VkResult
anv_CreateSemaphore(
1477 const VkSemaphoreCreateInfo
* pCreateInfo
,
1478 const VkAllocationCallbacks
* pAllocator
,
1479 VkSemaphore
* pSemaphore
)
1481 /* The DRM execbuffer ioctl always execute in-oder, even between different
1482 * rings. As such, there's nothing to do for the user space semaphore.
1485 *pSemaphore
= (VkSemaphore
)1;
1490 void anv_DestroySemaphore(
1492 VkSemaphore semaphore
,
1493 const VkAllocationCallbacks
* pAllocator
)
1499 VkResult
anv_CreateEvent(
1501 const VkEventCreateInfo
* pCreateInfo
,
1502 const VkAllocationCallbacks
* pAllocator
,
1505 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1506 struct anv_state state
;
1507 struct anv_event
*event
;
1509 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_EVENT_CREATE_INFO
);
1511 state
= anv_state_pool_alloc(&device
->dynamic_state_pool
,
1514 event
->state
= state
;
1515 event
->semaphore
= VK_EVENT_RESET
;
1517 if (!device
->info
.has_llc
) {
1518 /* Make sure the writes we're flushing have landed. */
1519 __builtin_ia32_mfence();
1520 __builtin_ia32_clflush(event
);
1523 *pEvent
= anv_event_to_handle(event
);
1528 void anv_DestroyEvent(
1531 const VkAllocationCallbacks
* pAllocator
)
1533 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1534 ANV_FROM_HANDLE(anv_event
, event
, _event
);
1536 anv_state_pool_free(&device
->dynamic_state_pool
, event
->state
);
1539 VkResult
anv_GetEventStatus(
1543 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1544 ANV_FROM_HANDLE(anv_event
, event
, _event
);
1546 if (!device
->info
.has_llc
) {
1547 /* Invalidate read cache before reading event written by GPU. */
1548 __builtin_ia32_clflush(event
);
1549 __builtin_ia32_mfence();
1553 return event
->semaphore
;
1556 VkResult
anv_SetEvent(
1560 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1561 ANV_FROM_HANDLE(anv_event
, event
, _event
);
1563 event
->semaphore
= VK_EVENT_SET
;
1565 if (!device
->info
.has_llc
) {
1566 /* Make sure the writes we're flushing have landed. */
1567 __builtin_ia32_mfence();
1568 __builtin_ia32_clflush(event
);
1574 VkResult
anv_ResetEvent(
1578 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1579 ANV_FROM_HANDLE(anv_event
, event
, _event
);
1581 event
->semaphore
= VK_EVENT_RESET
;
1583 if (!device
->info
.has_llc
) {
1584 /* Make sure the writes we're flushing have landed. */
1585 __builtin_ia32_mfence();
1586 __builtin_ia32_clflush(event
);
1594 VkResult
anv_CreateBuffer(
1596 const VkBufferCreateInfo
* pCreateInfo
,
1597 const VkAllocationCallbacks
* pAllocator
,
1600 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1601 struct anv_buffer
*buffer
;
1603 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO
);
1605 buffer
= anv_alloc2(&device
->alloc
, pAllocator
, sizeof(*buffer
), 8,
1606 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
1608 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
1610 buffer
->size
= pCreateInfo
->size
;
1611 buffer
->usage
= pCreateInfo
->usage
;
1615 *pBuffer
= anv_buffer_to_handle(buffer
);
1620 void anv_DestroyBuffer(
1623 const VkAllocationCallbacks
* pAllocator
)
1625 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1626 ANV_FROM_HANDLE(anv_buffer
, buffer
, _buffer
);
1628 anv_free2(&device
->alloc
, pAllocator
, buffer
);
1632 anv_fill_buffer_surface_state(struct anv_device
*device
, struct anv_state state
,
1633 enum isl_format format
,
1634 uint32_t offset
, uint32_t range
, uint32_t stride
)
1636 switch (device
->info
.gen
) {
1638 if (device
->info
.is_haswell
)
1639 gen75_fill_buffer_surface_state(state
.map
, format
, offset
, range
,
1642 gen7_fill_buffer_surface_state(state
.map
, format
, offset
, range
,
1646 gen8_fill_buffer_surface_state(state
.map
, format
, offset
, range
, stride
);
1649 gen9_fill_buffer_surface_state(state
.map
, format
, offset
, range
, stride
);
1652 unreachable("unsupported gen\n");
1655 if (!device
->info
.has_llc
)
1656 anv_state_clflush(state
);
1659 void anv_DestroySampler(
1662 const VkAllocationCallbacks
* pAllocator
)
1664 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1665 ANV_FROM_HANDLE(anv_sampler
, sampler
, _sampler
);
1667 anv_free2(&device
->alloc
, pAllocator
, sampler
);
1670 VkResult
anv_CreateFramebuffer(
1672 const VkFramebufferCreateInfo
* pCreateInfo
,
1673 const VkAllocationCallbacks
* pAllocator
,
1674 VkFramebuffer
* pFramebuffer
)
1676 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1677 struct anv_framebuffer
*framebuffer
;
1679 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO
);
1681 size_t size
= sizeof(*framebuffer
) +
1682 sizeof(struct anv_image_view
*) * pCreateInfo
->attachmentCount
;
1683 framebuffer
= anv_alloc2(&device
->alloc
, pAllocator
, size
, 8,
1684 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
1685 if (framebuffer
== NULL
)
1686 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
1688 framebuffer
->attachment_count
= pCreateInfo
->attachmentCount
;
1689 for (uint32_t i
= 0; i
< pCreateInfo
->attachmentCount
; i
++) {
1690 VkImageView _iview
= pCreateInfo
->pAttachments
[i
];
1691 framebuffer
->attachments
[i
] = anv_image_view_from_handle(_iview
);
1694 framebuffer
->width
= pCreateInfo
->width
;
1695 framebuffer
->height
= pCreateInfo
->height
;
1696 framebuffer
->layers
= pCreateInfo
->layers
;
1698 *pFramebuffer
= anv_framebuffer_to_handle(framebuffer
);
1703 void anv_DestroyFramebuffer(
1706 const VkAllocationCallbacks
* pAllocator
)
1708 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1709 ANV_FROM_HANDLE(anv_framebuffer
, fb
, _fb
);
1711 anv_free2(&device
->alloc
, pAllocator
, fb
);
1714 void vkCmdDbgMarkerBegin(
1715 VkCommandBuffer commandBuffer
,
1716 const char* pMarker
)
1717 __attribute__ ((visibility ("default")));
1719 void vkCmdDbgMarkerEnd(
1720 VkCommandBuffer commandBuffer
)
1721 __attribute__ ((visibility ("default")));
1723 void vkCmdDbgMarkerBegin(
1724 VkCommandBuffer commandBuffer
,
1725 const char* pMarker
)
1729 void vkCmdDbgMarkerEnd(
1730 VkCommandBuffer commandBuffer
)