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 struct anv_dispatch_table dtable
;
37 compiler_debug_log(void *data
, const char *fmt
, ...)
41 compiler_perf_log(void *data
, const char *fmt
, ...)
46 if (unlikely(INTEL_DEBUG
& DEBUG_PERF
))
47 vfprintf(stderr
, fmt
, args
);
53 anv_physical_device_init(struct anv_physical_device
*device
,
54 struct anv_instance
*instance
,
60 fd
= open(path
, O_RDWR
| O_CLOEXEC
);
62 return vk_errorf(VK_ERROR_INITIALIZATION_FAILED
,
63 "failed to open %s: %m", path
);
65 device
->_loader_data
.loaderMagic
= ICD_LOADER_MAGIC
;
66 device
->instance
= instance
;
69 device
->chipset_id
= anv_gem_get_param(fd
, I915_PARAM_CHIPSET_ID
);
70 if (!device
->chipset_id
) {
71 result
= vk_errorf(VK_ERROR_INITIALIZATION_FAILED
,
72 "failed to get chipset id: %m");
76 device
->name
= brw_get_device_name(device
->chipset_id
);
77 device
->info
= brw_get_device_info(device
->chipset_id
);
79 result
= vk_errorf(VK_ERROR_INITIALIZATION_FAILED
,
80 "failed to get device info");
84 if (device
->info
->gen
== 7 &&
85 !device
->info
->is_haswell
&& !device
->info
->is_baytrail
) {
86 fprintf(stderr
, "WARNING: Ivy Bridge Vulkan support is incomplete");
87 } else if (device
->info
->gen
== 8 && !device
->info
->is_cherryview
) {
88 /* Briadwell is as fully supported as anything */
90 result
= vk_errorf(VK_UNSUPPORTED
,
91 "Vulkan not yet supported on %s", device
->name
);
95 if (anv_gem_get_aperture(fd
, &device
->aperture_size
) == -1) {
96 result
= vk_errorf(VK_ERROR_INITIALIZATION_FAILED
,
97 "failed to get aperture size: %m");
101 if (!anv_gem_get_param(fd
, I915_PARAM_HAS_WAIT_TIMEOUT
)) {
102 result
= vk_errorf(VK_ERROR_INITIALIZATION_FAILED
,
103 "kernel missing gem wait");
107 if (!anv_gem_get_param(fd
, I915_PARAM_HAS_EXECBUF2
)) {
108 result
= vk_errorf(VK_ERROR_INITIALIZATION_FAILED
,
109 "kernel missing execbuf2");
113 if (!anv_gem_get_param(fd
, I915_PARAM_HAS_LLC
)) {
114 result
= vk_errorf(VK_ERROR_INITIALIZATION_FAILED
,
121 brw_process_intel_debug_variable();
123 device
->compiler
= brw_compiler_create(NULL
, device
->info
);
124 if (device
->compiler
== NULL
) {
125 result
= vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
128 device
->compiler
->shader_debug_log
= compiler_debug_log
;
129 device
->compiler
->shader_perf_log
= compiler_perf_log
;
139 anv_physical_device_finish(struct anv_physical_device
*device
)
141 ralloc_free(device
->compiler
);
144 static void *default_alloc(
148 VkSystemAllocType allocType
)
153 static void default_free(
160 static const VkAllocCallbacks default_alloc_callbacks
= {
162 .pfnAlloc
= default_alloc
,
163 .pfnFree
= default_free
166 static const VkExtensionProperties global_extensions
[] = {
168 .extName
= VK_EXT_KHR_SWAPCHAIN_EXTENSION_NAME
,
173 static const VkExtensionProperties device_extensions
[] = {
175 .extName
= VK_EXT_KHR_DEVICE_SWAPCHAIN_EXTENSION_NAME
,
180 VkResult
anv_CreateInstance(
181 const VkInstanceCreateInfo
* pCreateInfo
,
182 VkInstance
* pInstance
)
184 struct anv_instance
*instance
;
185 const VkAllocCallbacks
*alloc_callbacks
= &default_alloc_callbacks
;
186 void *user_data
= NULL
;
188 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO
);
190 if (pCreateInfo
->pAppInfo
->apiVersion
!= VK_MAKE_VERSION(0, 170, 2))
191 return vk_error(VK_ERROR_INCOMPATIBLE_DRIVER
);
193 for (uint32_t i
= 0; i
< pCreateInfo
->extensionCount
; i
++) {
195 for (uint32_t j
= 0; j
< ARRAY_SIZE(global_extensions
); j
++) {
196 if (strcmp(pCreateInfo
->ppEnabledExtensionNames
[i
],
197 global_extensions
[j
].extName
) == 0) {
203 return vk_error(VK_ERROR_EXTENSION_NOT_PRESENT
);
206 if (pCreateInfo
->pAllocCb
) {
207 alloc_callbacks
= pCreateInfo
->pAllocCb
;
208 user_data
= pCreateInfo
->pAllocCb
->pUserData
;
210 instance
= alloc_callbacks
->pfnAlloc(user_data
, sizeof(*instance
), 8,
211 VK_SYSTEM_ALLOC_TYPE_API_OBJECT
);
213 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
215 instance
->_loader_data
.loaderMagic
= ICD_LOADER_MAGIC
;
216 instance
->pAllocUserData
= alloc_callbacks
->pUserData
;
217 instance
->pfnAlloc
= alloc_callbacks
->pfnAlloc
;
218 instance
->pfnFree
= alloc_callbacks
->pfnFree
;
219 instance
->apiVersion
= pCreateInfo
->pAppInfo
->apiVersion
;
220 instance
->physicalDeviceCount
= -1;
224 VG(VALGRIND_CREATE_MEMPOOL(instance
, 0, false));
226 anv_init_wsi(instance
);
228 *pInstance
= anv_instance_to_handle(instance
);
233 void anv_DestroyInstance(
234 VkInstance _instance
)
236 ANV_FROM_HANDLE(anv_instance
, instance
, _instance
);
238 if (instance
->physicalDeviceCount
> 0) {
239 /* We support at most one physical device. */
240 assert(instance
->physicalDeviceCount
== 1);
241 anv_physical_device_finish(&instance
->physicalDevice
);
244 anv_finish_wsi(instance
);
246 VG(VALGRIND_DESTROY_MEMPOOL(instance
));
250 instance
->pfnFree(instance
->pAllocUserData
, instance
);
254 anv_instance_alloc(struct anv_instance
*instance
, size_t size
,
255 size_t alignment
, VkSystemAllocType allocType
)
257 void *mem
= instance
->pfnAlloc(instance
->pAllocUserData
,
258 size
, alignment
, allocType
);
260 VG(VALGRIND_MEMPOOL_ALLOC(instance
, mem
, size
));
261 VG(VALGRIND_MAKE_MEM_UNDEFINED(mem
, size
));
267 anv_instance_free(struct anv_instance
*instance
, void *mem
)
272 VG(VALGRIND_MEMPOOL_FREE(instance
, mem
));
274 instance
->pfnFree(instance
->pAllocUserData
, mem
);
277 VkResult
anv_EnumeratePhysicalDevices(
278 VkInstance _instance
,
279 uint32_t* pPhysicalDeviceCount
,
280 VkPhysicalDevice
* pPhysicalDevices
)
282 ANV_FROM_HANDLE(anv_instance
, instance
, _instance
);
285 if (instance
->physicalDeviceCount
< 0) {
286 result
= anv_physical_device_init(&instance
->physicalDevice
,
287 instance
, "/dev/dri/renderD128");
288 if (result
== VK_UNSUPPORTED
) {
289 instance
->physicalDeviceCount
= 0;
290 } else if (result
== VK_SUCCESS
) {
291 instance
->physicalDeviceCount
= 1;
297 /* pPhysicalDeviceCount is an out parameter if pPhysicalDevices is NULL;
298 * otherwise it's an inout parameter.
300 * The Vulkan spec (git aaed022) says:
302 * pPhysicalDeviceCount is a pointer to an unsigned integer variable
303 * that is initialized with the number of devices the application is
304 * prepared to receive handles to. pname:pPhysicalDevices is pointer to
305 * an array of at least this many VkPhysicalDevice handles [...].
307 * Upon success, if pPhysicalDevices is NULL, vkEnumeratePhysicalDevices
308 * overwrites the contents of the variable pointed to by
309 * pPhysicalDeviceCount with the number of physical devices in in the
310 * instance; otherwise, vkEnumeratePhysicalDevices overwrites
311 * pPhysicalDeviceCount with the number of physical handles written to
314 if (!pPhysicalDevices
) {
315 *pPhysicalDeviceCount
= instance
->physicalDeviceCount
;
316 } else if (*pPhysicalDeviceCount
>= 1) {
317 pPhysicalDevices
[0] = anv_physical_device_to_handle(&instance
->physicalDevice
);
318 *pPhysicalDeviceCount
= 1;
320 *pPhysicalDeviceCount
= 0;
326 VkResult
anv_GetPhysicalDeviceFeatures(
327 VkPhysicalDevice physicalDevice
,
328 VkPhysicalDeviceFeatures
* pFeatures
)
330 anv_finishme("Get correct values for PhysicalDeviceFeatures");
332 *pFeatures
= (VkPhysicalDeviceFeatures
) {
333 .robustBufferAccess
= false,
334 .fullDrawIndexUint32
= false,
335 .imageCubeArray
= false,
336 .independentBlend
= false,
337 .geometryShader
= true,
338 .tessellationShader
= false,
339 .sampleRateShading
= false,
340 .dualSourceBlend
= true,
342 .multiDrawIndirect
= true,
344 .depthBiasClamp
= false,
345 .fillModeNonSolid
= true,
346 .depthBounds
= false,
349 .textureCompressionETC2
= true,
350 .textureCompressionASTC_LDR
= true,
351 .textureCompressionBC
= true,
352 .occlusionQueryNonConservative
= false, /* FINISHME */
353 .pipelineStatisticsQuery
= true,
354 .vertexSideEffects
= false,
355 .tessellationSideEffects
= false,
356 .geometrySideEffects
= false,
357 .fragmentSideEffects
= false,
358 .shaderTessellationPointSize
= false,
359 .shaderGeometryPointSize
= true,
360 .shaderImageGatherExtended
= true,
361 .shaderStorageImageExtendedFormats
= false,
362 .shaderStorageImageMultisample
= false,
363 .shaderUniformBufferArrayDynamicIndexing
= true,
364 .shaderSampledImageArrayDynamicIndexing
= false,
365 .shaderStorageBufferArrayDynamicIndexing
= false,
366 .shaderStorageImageArrayDynamicIndexing
= false,
367 .shaderClipDistance
= false,
368 .shaderCullDistance
= false,
369 .shaderFloat64
= false,
370 .shaderInt64
= false,
371 .shaderInt16
= false,
378 VkResult
anv_GetPhysicalDeviceProperties(
379 VkPhysicalDevice physicalDevice
,
380 VkPhysicalDeviceProperties
* pProperties
)
382 ANV_FROM_HANDLE(anv_physical_device
, pdevice
, physicalDevice
);
383 const struct brw_device_info
*devinfo
= pdevice
->info
;
385 anv_finishme("Get correct values for VkPhysicalDeviceLimits");
387 VkPhysicalDeviceLimits limits
= {
388 .maxImageDimension1D
= (1 << 14),
389 .maxImageDimension2D
= (1 << 14),
390 .maxImageDimension3D
= (1 << 10),
391 .maxImageDimensionCube
= (1 << 14),
392 .maxImageArrayLayers
= (1 << 10),
394 /* Broadwell supports 1, 2, 4, and 8 samples. */
397 .maxTexelBufferSize
= (1 << 14),
398 .maxUniformBufferSize
= UINT32_MAX
,
399 .maxStorageBufferSize
= UINT32_MAX
,
400 .maxPushConstantsSize
= MAX_PUSH_CONSTANTS_SIZE
,
401 .maxMemoryAllocationCount
= UINT32_MAX
,
402 .bufferImageGranularity
= 64, /* A cache line */
403 .sparseAddressSpaceSize
= 0,
404 .maxBoundDescriptorSets
= MAX_SETS
,
405 .maxDescriptorSets
= UINT32_MAX
,
406 .maxPerStageDescriptorSamplers
= 64,
407 .maxPerStageDescriptorUniformBuffers
= 64,
408 .maxPerStageDescriptorStorageBuffers
= 64,
409 .maxPerStageDescriptorSampledImages
= 64,
410 .maxPerStageDescriptorStorageImages
= 64,
411 .maxDescriptorSetSamplers
= 256,
412 .maxDescriptorSetUniformBuffers
= 256,
413 .maxDescriptorSetUniformBuffersDynamic
= 256,
414 .maxDescriptorSetStorageBuffers
= 256,
415 .maxDescriptorSetStorageBuffersDynamic
= 256,
416 .maxDescriptorSetSampledImages
= 256,
417 .maxDescriptorSetStorageImages
= 256,
418 .maxVertexInputAttributes
= 32,
419 .maxVertexInputBindings
= 32,
420 .maxVertexInputAttributeOffset
= 256,
421 .maxVertexInputBindingStride
= 256,
422 .maxVertexOutputComponents
= 32,
423 .maxTessGenLevel
= 0,
424 .maxTessPatchSize
= 0,
425 .maxTessControlPerVertexInputComponents
= 0,
426 .maxTessControlPerVertexOutputComponents
= 0,
427 .maxTessControlPerPatchOutputComponents
= 0,
428 .maxTessControlTotalOutputComponents
= 0,
429 .maxTessEvaluationInputComponents
= 0,
430 .maxTessEvaluationOutputComponents
= 0,
431 .maxGeometryShaderInvocations
= 6,
432 .maxGeometryInputComponents
= 16,
433 .maxGeometryOutputComponents
= 16,
434 .maxGeometryOutputVertices
= 16,
435 .maxGeometryTotalOutputComponents
= 16,
436 .maxFragmentInputComponents
= 16,
437 .maxFragmentOutputBuffers
= 8,
438 .maxFragmentDualSourceBuffers
= 2,
439 .maxFragmentCombinedOutputResources
= 8,
440 .maxComputeSharedMemorySize
= 1024,
441 .maxComputeWorkGroupCount
= {
442 16 * devinfo
->max_cs_threads
,
443 16 * devinfo
->max_cs_threads
,
444 16 * devinfo
->max_cs_threads
,
446 .maxComputeWorkGroupInvocations
= 16 * devinfo
->max_cs_threads
,
447 .maxComputeWorkGroupSize
= {
448 16 * devinfo
->max_cs_threads
,
449 16 * devinfo
->max_cs_threads
,
450 16 * devinfo
->max_cs_threads
,
452 .subPixelPrecisionBits
= 4 /* FIXME */,
453 .subTexelPrecisionBits
= 4 /* FIXME */,
454 .mipmapPrecisionBits
= 4 /* FIXME */,
455 .maxDrawIndexedIndexValue
= UINT32_MAX
,
456 .maxDrawIndirectInstanceCount
= UINT32_MAX
,
457 .primitiveRestartForPatches
= UINT32_MAX
,
458 .maxSamplerLodBias
= 16,
459 .maxSamplerAnisotropy
= 16,
460 .maxViewports
= MAX_VIEWPORTS
,
461 .maxViewportDimensions
= { (1 << 14), (1 << 14) },
462 .viewportBoundsRange
= { -1.0, 1.0 }, /* FIXME */
463 .viewportSubPixelBits
= 13, /* We take a float? */
464 .minMemoryMapAlignment
= 64, /* A cache line */
465 .minTexelBufferOffsetAlignment
= 1,
466 .minUniformBufferOffsetAlignment
= 1,
467 .minStorageBufferOffsetAlignment
= 1,
468 .minTexelOffset
= 0, /* FIXME */
469 .maxTexelOffset
= 0, /* FIXME */
470 .minTexelGatherOffset
= 0, /* FIXME */
471 .maxTexelGatherOffset
= 0, /* FIXME */
472 .minInterpolationOffset
= 0, /* FIXME */
473 .maxInterpolationOffset
= 0, /* FIXME */
474 .subPixelInterpolationOffsetBits
= 0, /* FIXME */
475 .maxFramebufferWidth
= (1 << 14),
476 .maxFramebufferHeight
= (1 << 14),
477 .maxFramebufferLayers
= (1 << 10),
478 .maxFramebufferColorSamples
= 8,
479 .maxFramebufferDepthSamples
= 8,
480 .maxFramebufferStencilSamples
= 8,
481 .maxColorAttachments
= MAX_RTS
,
482 .maxSampledImageColorSamples
= 8,
483 .maxSampledImageDepthSamples
= 8,
484 .maxSampledImageIntegerSamples
= 1,
485 .maxStorageImageSamples
= 1,
486 .maxSampleMaskWords
= 1,
487 .timestampFrequency
= 1000 * 1000 * 1000 / 80,
488 .maxClipDistances
= 0 /* FIXME */,
489 .maxCullDistances
= 0 /* FIXME */,
490 .maxCombinedClipAndCullDistances
= 0 /* FIXME */,
491 .pointSizeRange
= { 0.125, 255.875 },
492 .lineWidthRange
= { 0.0, 7.9921875 },
493 .pointSizeGranularity
= (1.0 / 8.0),
494 .lineWidthGranularity
= (1.0 / 128.0),
497 *pProperties
= (VkPhysicalDeviceProperties
) {
498 .apiVersion
= VK_MAKE_VERSION(0, 170, 2),
501 .deviceId
= pdevice
->chipset_id
,
502 .deviceType
= VK_PHYSICAL_DEVICE_TYPE_INTEGRATED_GPU
,
504 .sparseProperties
= {0}, /* Broadwell doesn't do sparse. */
507 strcpy(pProperties
->deviceName
, pdevice
->name
);
508 snprintf((char *)pProperties
->pipelineCacheUUID
, VK_UUID_LENGTH
,
509 "anv-%s", MESA_GIT_SHA1
+ 4);
514 VkResult
anv_GetPhysicalDeviceQueueFamilyProperties(
515 VkPhysicalDevice physicalDevice
,
517 VkQueueFamilyProperties
* pQueueFamilyProperties
)
519 if (pQueueFamilyProperties
== NULL
) {
524 assert(*pCount
>= 1);
526 *pQueueFamilyProperties
= (VkQueueFamilyProperties
) {
527 .queueFlags
= VK_QUEUE_GRAPHICS_BIT
|
528 VK_QUEUE_COMPUTE_BIT
|
531 .supportsTimestamps
= true,
537 VkResult
anv_GetPhysicalDeviceMemoryProperties(
538 VkPhysicalDevice physicalDevice
,
539 VkPhysicalDeviceMemoryProperties
* pMemoryProperties
)
541 ANV_FROM_HANDLE(anv_physical_device
, physical_device
, physicalDevice
);
542 VkDeviceSize heap_size
;
544 /* Reserve some wiggle room for the driver by exposing only 75% of the
545 * aperture to the heap.
547 heap_size
= 3 * physical_device
->aperture_size
/ 4;
549 /* The property flags below are valid only for llc platforms. */
550 pMemoryProperties
->memoryTypeCount
= 1;
551 pMemoryProperties
->memoryTypes
[0] = (VkMemoryType
) {
552 .propertyFlags
= VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT
,
556 pMemoryProperties
->memoryHeapCount
= 1;
557 pMemoryProperties
->memoryHeaps
[0] = (VkMemoryHeap
) {
559 .flags
= VK_MEMORY_HEAP_HOST_LOCAL_BIT
,
565 PFN_vkVoidFunction
anv_GetInstanceProcAddr(
569 return anv_lookup_entrypoint(pName
);
572 PFN_vkVoidFunction
anv_GetDeviceProcAddr(
576 return anv_lookup_entrypoint(pName
);
580 anv_queue_init(struct anv_device
*device
, struct anv_queue
*queue
)
582 queue
->_loader_data
.loaderMagic
= ICD_LOADER_MAGIC
;
583 queue
->device
= device
;
584 queue
->pool
= &device
->surface_state_pool
;
590 anv_queue_finish(struct anv_queue
*queue
)
595 anv_device_init_border_colors(struct anv_device
*device
)
597 static const VkClearColorValue border_colors
[] = {
598 [VK_BORDER_COLOR_FLOAT_TRANSPARENT_BLACK
] = { .float32
= { 0.0, 0.0, 0.0, 0.0 } },
599 [VK_BORDER_COLOR_FLOAT_OPAQUE_BLACK
] = { .float32
= { 0.0, 0.0, 0.0, 1.0 } },
600 [VK_BORDER_COLOR_FLOAT_OPAQUE_WHITE
] = { .float32
= { 1.0, 1.0, 1.0, 1.0 } },
601 [VK_BORDER_COLOR_INT_TRANSPARENT_BLACK
] = { .uint32
= { 0, 0, 0, 0 } },
602 [VK_BORDER_COLOR_INT_OPAQUE_BLACK
] = { .uint32
= { 0, 0, 0, 1 } },
603 [VK_BORDER_COLOR_INT_OPAQUE_WHITE
] = { .uint32
= { 1, 1, 1, 1 } },
606 device
->border_colors
=
607 anv_state_pool_alloc(&device
->dynamic_state_pool
,
608 sizeof(border_colors
), 32);
609 memcpy(device
->border_colors
.map
, border_colors
, sizeof(border_colors
));
612 VkResult
anv_CreateDevice(
613 VkPhysicalDevice physicalDevice
,
614 const VkDeviceCreateInfo
* pCreateInfo
,
617 ANV_FROM_HANDLE(anv_physical_device
, physical_device
, physicalDevice
);
618 struct anv_instance
*instance
= physical_device
->instance
;
619 struct anv_device
*device
;
621 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO
);
623 for (uint32_t i
= 0; i
< pCreateInfo
->extensionCount
; i
++) {
625 for (uint32_t j
= 0; j
< ARRAY_SIZE(device_extensions
); j
++) {
626 if (strcmp(pCreateInfo
->ppEnabledExtensionNames
[i
],
627 device_extensions
[j
].extName
) == 0) {
633 return vk_error(VK_ERROR_EXTENSION_NOT_PRESENT
);
636 anv_set_dispatch_gen(physical_device
->info
->gen
);
638 device
= anv_instance_alloc(instance
, sizeof(*device
), 8,
639 VK_SYSTEM_ALLOC_TYPE_API_OBJECT
);
641 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
643 device
->_loader_data
.loaderMagic
= ICD_LOADER_MAGIC
;
644 device
->instance
= physical_device
->instance
;
646 /* XXX(chadv): Can we dup() physicalDevice->fd here? */
647 device
->fd
= open(physical_device
->path
, O_RDWR
| O_CLOEXEC
);
648 if (device
->fd
== -1)
651 device
->context_id
= anv_gem_create_context(device
);
652 if (device
->context_id
== -1)
655 pthread_mutex_init(&device
->mutex
, NULL
);
657 anv_bo_pool_init(&device
->batch_bo_pool
, device
, ANV_CMD_BUFFER_BATCH_SIZE
);
659 anv_block_pool_init(&device
->dynamic_state_block_pool
, device
, 2048);
661 anv_state_pool_init(&device
->dynamic_state_pool
,
662 &device
->dynamic_state_block_pool
);
664 anv_block_pool_init(&device
->instruction_block_pool
, device
, 2048);
665 anv_block_pool_init(&device
->surface_state_block_pool
, device
, 4096);
667 anv_state_pool_init(&device
->surface_state_pool
,
668 &device
->surface_state_block_pool
);
670 anv_block_pool_init(&device
->scratch_block_pool
, device
, 0x10000);
672 device
->info
= *physical_device
->info
;
674 anv_queue_init(device
, &device
->queue
);
676 anv_device_init_meta(device
);
678 anv_device_init_border_colors(device
);
680 *pDevice
= anv_device_to_handle(device
);
687 anv_device_free(device
, device
);
689 return vk_error(VK_ERROR_INITIALIZATION_FAILED
);
692 void anv_DestroyDevice(
695 ANV_FROM_HANDLE(anv_device
, device
, _device
);
697 anv_queue_finish(&device
->queue
);
699 anv_device_finish_meta(device
);
702 /* We only need to free these to prevent valgrind errors. The backing
703 * BO will go away in a couple of lines so we don't actually leak.
705 anv_state_pool_free(&device
->dynamic_state_pool
, device
->border_colors
);
708 anv_bo_pool_finish(&device
->batch_bo_pool
);
709 anv_state_pool_finish(&device
->dynamic_state_pool
);
710 anv_block_pool_finish(&device
->dynamic_state_block_pool
);
711 anv_block_pool_finish(&device
->instruction_block_pool
);
712 anv_state_pool_finish(&device
->surface_state_pool
);
713 anv_block_pool_finish(&device
->surface_state_block_pool
);
714 anv_block_pool_finish(&device
->scratch_block_pool
);
718 anv_instance_free(device
->instance
, device
);
721 VkResult
anv_EnumerateInstanceExtensionProperties(
722 const char* pLayerName
,
724 VkExtensionProperties
* pProperties
)
726 if (pProperties
== NULL
) {
727 *pCount
= ARRAY_SIZE(global_extensions
);
731 assert(*pCount
>= ARRAY_SIZE(global_extensions
));
733 *pCount
= ARRAY_SIZE(global_extensions
);
734 memcpy(pProperties
, global_extensions
, sizeof(global_extensions
));
739 VkResult
anv_EnumerateDeviceExtensionProperties(
740 VkPhysicalDevice physicalDevice
,
741 const char* pLayerName
,
743 VkExtensionProperties
* pProperties
)
745 if (pProperties
== NULL
) {
746 *pCount
= ARRAY_SIZE(device_extensions
);
750 assert(*pCount
>= ARRAY_SIZE(device_extensions
));
752 *pCount
= ARRAY_SIZE(device_extensions
);
753 memcpy(pProperties
, device_extensions
, sizeof(device_extensions
));
758 VkResult
anv_EnumerateInstanceLayerProperties(
760 VkLayerProperties
* pProperties
)
762 if (pProperties
== NULL
) {
767 /* None supported at this time */
768 return vk_error(VK_ERROR_LAYER_NOT_PRESENT
);
771 VkResult
anv_EnumerateDeviceLayerProperties(
772 VkPhysicalDevice physicalDevice
,
774 VkLayerProperties
* pProperties
)
776 if (pProperties
== NULL
) {
781 /* None supported at this time */
782 return vk_error(VK_ERROR_LAYER_NOT_PRESENT
);
785 VkResult
anv_GetDeviceQueue(
787 uint32_t queueNodeIndex
,
791 ANV_FROM_HANDLE(anv_device
, device
, _device
);
793 assert(queueIndex
== 0);
795 *pQueue
= anv_queue_to_handle(&device
->queue
);
800 VkResult
anv_QueueSubmit(
802 uint32_t cmdBufferCount
,
803 const VkCmdBuffer
* pCmdBuffers
,
806 ANV_FROM_HANDLE(anv_queue
, queue
, _queue
);
807 ANV_FROM_HANDLE(anv_fence
, fence
, _fence
);
808 struct anv_device
*device
= queue
->device
;
811 for (uint32_t i
= 0; i
< cmdBufferCount
; i
++) {
812 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, pCmdBuffers
[i
]);
814 assert(cmd_buffer
->level
== VK_CMD_BUFFER_LEVEL_PRIMARY
);
816 ret
= anv_gem_execbuffer(device
, &cmd_buffer
->execbuf2
.execbuf
);
818 /* We don't know the real error. */
819 return vk_errorf(VK_ERROR_OUT_OF_DEVICE_MEMORY
,
820 "execbuf2 failed: %m");
824 ret
= anv_gem_execbuffer(device
, &fence
->execbuf
);
826 /* We don't know the real error. */
827 return vk_errorf(VK_ERROR_OUT_OF_DEVICE_MEMORY
,
828 "execbuf2 failed: %m");
832 for (uint32_t i
= 0; i
< cmd_buffer
->execbuf2
.bo_count
; i
++)
833 cmd_buffer
->execbuf2
.bos
[i
]->offset
= cmd_buffer
->execbuf2
.objects
[i
].offset
;
839 VkResult
anv_QueueWaitIdle(
842 ANV_FROM_HANDLE(anv_queue
, queue
, _queue
);
844 return ANV_CALL(DeviceWaitIdle
)(anv_device_to_handle(queue
->device
));
847 VkResult
anv_DeviceWaitIdle(
850 ANV_FROM_HANDLE(anv_device
, device
, _device
);
851 struct anv_state state
;
852 struct anv_batch batch
;
853 struct drm_i915_gem_execbuffer2 execbuf
;
854 struct drm_i915_gem_exec_object2 exec2_objects
[1];
855 struct anv_bo
*bo
= NULL
;
860 state
= anv_state_pool_alloc(&device
->dynamic_state_pool
, 32, 32);
861 bo
= &device
->dynamic_state_pool
.block_pool
->bo
;
862 batch
.start
= batch
.next
= state
.map
;
863 batch
.end
= state
.map
+ 32;
864 anv_batch_emit(&batch
, GEN7_MI_BATCH_BUFFER_END
);
865 anv_batch_emit(&batch
, GEN7_MI_NOOP
);
867 exec2_objects
[0].handle
= bo
->gem_handle
;
868 exec2_objects
[0].relocation_count
= 0;
869 exec2_objects
[0].relocs_ptr
= 0;
870 exec2_objects
[0].alignment
= 0;
871 exec2_objects
[0].offset
= bo
->offset
;
872 exec2_objects
[0].flags
= 0;
873 exec2_objects
[0].rsvd1
= 0;
874 exec2_objects
[0].rsvd2
= 0;
876 execbuf
.buffers_ptr
= (uintptr_t) exec2_objects
;
877 execbuf
.buffer_count
= 1;
878 execbuf
.batch_start_offset
= state
.offset
;
879 execbuf
.batch_len
= batch
.next
- state
.map
;
880 execbuf
.cliprects_ptr
= 0;
881 execbuf
.num_cliprects
= 0;
886 I915_EXEC_HANDLE_LUT
| I915_EXEC_NO_RELOC
| I915_EXEC_RENDER
;
887 execbuf
.rsvd1
= device
->context_id
;
890 ret
= anv_gem_execbuffer(device
, &execbuf
);
892 /* We don't know the real error. */
893 result
= vk_errorf(VK_ERROR_OUT_OF_DEVICE_MEMORY
, "execbuf2 failed: %m");
898 ret
= anv_gem_wait(device
, bo
->gem_handle
, &timeout
);
900 /* We don't know the real error. */
901 result
= vk_errorf(VK_ERROR_OUT_OF_DEVICE_MEMORY
, "execbuf2 failed: %m");
905 anv_state_pool_free(&device
->dynamic_state_pool
, state
);
910 anv_state_pool_free(&device
->dynamic_state_pool
, state
);
916 anv_device_alloc(struct anv_device
* device
,
919 VkSystemAllocType allocType
)
921 return anv_instance_alloc(device
->instance
, size
, alignment
, allocType
);
925 anv_device_free(struct anv_device
* device
,
928 anv_instance_free(device
->instance
, mem
);
932 anv_bo_init_new(struct anv_bo
*bo
, struct anv_device
*device
, uint64_t size
)
934 bo
->gem_handle
= anv_gem_create(device
, size
);
936 return vk_error(VK_ERROR_OUT_OF_DEVICE_MEMORY
);
946 VkResult
anv_AllocMemory(
948 const VkMemoryAllocInfo
* pAllocInfo
,
949 VkDeviceMemory
* pMem
)
951 ANV_FROM_HANDLE(anv_device
, device
, _device
);
952 struct anv_device_memory
*mem
;
955 assert(pAllocInfo
->sType
== VK_STRUCTURE_TYPE_MEMORY_ALLOC_INFO
);
957 /* We support exactly one memory heap. */
958 assert(pAllocInfo
->memoryTypeIndex
== 0);
960 /* FINISHME: Fail if allocation request exceeds heap size. */
962 mem
= anv_device_alloc(device
, sizeof(*mem
), 8,
963 VK_SYSTEM_ALLOC_TYPE_API_OBJECT
);
965 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
967 result
= anv_bo_init_new(&mem
->bo
, device
, pAllocInfo
->allocationSize
);
968 if (result
!= VK_SUCCESS
)
971 *pMem
= anv_device_memory_to_handle(mem
);
976 anv_device_free(device
, mem
);
985 ANV_FROM_HANDLE(anv_device
, device
, _device
);
986 ANV_FROM_HANDLE(anv_device_memory
, mem
, _mem
);
989 anv_gem_munmap(mem
->bo
.map
, mem
->bo
.size
);
991 if (mem
->bo
.gem_handle
!= 0)
992 anv_gem_close(device
, mem
->bo
.gem_handle
);
994 anv_device_free(device
, mem
);
997 VkResult
anv_MapMemory(
1000 VkDeviceSize offset
,
1002 VkMemoryMapFlags flags
,
1005 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1006 ANV_FROM_HANDLE(anv_device_memory
, mem
, _mem
);
1008 /* FIXME: Is this supposed to be thread safe? Since vkUnmapMemory() only
1009 * takes a VkDeviceMemory pointer, it seems like only one map of the memory
1010 * at a time is valid. We could just mmap up front and return an offset
1011 * pointer here, but that may exhaust virtual memory on 32 bit
1014 mem
->map
= anv_gem_mmap(device
, mem
->bo
.gem_handle
, offset
, size
);
1015 mem
->map_size
= size
;
1022 void anv_UnmapMemory(
1024 VkDeviceMemory _mem
)
1026 ANV_FROM_HANDLE(anv_device_memory
, mem
, _mem
);
1028 anv_gem_munmap(mem
->map
, mem
->map_size
);
1031 VkResult
anv_FlushMappedMemoryRanges(
1033 uint32_t memRangeCount
,
1034 const VkMappedMemoryRange
* pMemRanges
)
1036 /* clflush here for !llc platforms */
1041 VkResult
anv_InvalidateMappedMemoryRanges(
1043 uint32_t memRangeCount
,
1044 const VkMappedMemoryRange
* pMemRanges
)
1046 return anv_FlushMappedMemoryRanges(device
, memRangeCount
, pMemRanges
);
1049 VkResult
anv_GetBufferMemoryRequirements(
1052 VkMemoryRequirements
* pMemoryRequirements
)
1054 ANV_FROM_HANDLE(anv_buffer
, buffer
, _buffer
);
1056 /* The Vulkan spec (git aaed022) says:
1058 * memoryTypeBits is a bitfield and contains one bit set for every
1059 * supported memory type for the resource. The bit `1<<i` is set if and
1060 * only if the memory type `i` in the VkPhysicalDeviceMemoryProperties
1061 * structure for the physical device is supported.
1063 * We support exactly one memory type.
1065 pMemoryRequirements
->memoryTypeBits
= 1;
1067 pMemoryRequirements
->size
= buffer
->size
;
1068 pMemoryRequirements
->alignment
= 16;
1073 VkResult
anv_GetImageMemoryRequirements(
1076 VkMemoryRequirements
* pMemoryRequirements
)
1078 ANV_FROM_HANDLE(anv_image
, image
, _image
);
1080 /* The Vulkan spec (git aaed022) says:
1082 * memoryTypeBits is a bitfield and contains one bit set for every
1083 * supported memory type for the resource. The bit `1<<i` is set if and
1084 * only if the memory type `i` in the VkPhysicalDeviceMemoryProperties
1085 * structure for the physical device is supported.
1087 * We support exactly one memory type.
1089 pMemoryRequirements
->memoryTypeBits
= 1;
1091 pMemoryRequirements
->size
= image
->size
;
1092 pMemoryRequirements
->alignment
= image
->alignment
;
1097 VkResult
anv_GetImageSparseMemoryRequirements(
1100 uint32_t* pNumRequirements
,
1101 VkSparseImageMemoryRequirements
* pSparseMemoryRequirements
)
1103 return vk_error(VK_UNSUPPORTED
);
1106 VkResult
anv_GetDeviceMemoryCommitment(
1108 VkDeviceMemory memory
,
1109 VkDeviceSize
* pCommittedMemoryInBytes
)
1111 *pCommittedMemoryInBytes
= 0;
1112 stub_return(VK_SUCCESS
);
1115 VkResult
anv_BindBufferMemory(
1118 VkDeviceMemory _mem
,
1119 VkDeviceSize memOffset
)
1121 ANV_FROM_HANDLE(anv_device_memory
, mem
, _mem
);
1122 ANV_FROM_HANDLE(anv_buffer
, buffer
, _buffer
);
1124 buffer
->bo
= &mem
->bo
;
1125 buffer
->offset
= memOffset
;
1130 VkResult
anv_BindImageMemory(
1133 VkDeviceMemory _mem
,
1134 VkDeviceSize memOffset
)
1136 ANV_FROM_HANDLE(anv_device_memory
, mem
, _mem
);
1137 ANV_FROM_HANDLE(anv_image
, image
, _image
);
1139 image
->bo
= &mem
->bo
;
1140 image
->offset
= memOffset
;
1145 VkResult
anv_QueueBindSparseBufferMemory(
1148 uint32_t numBindings
,
1149 const VkSparseMemoryBindInfo
* pBindInfo
)
1151 stub_return(VK_UNSUPPORTED
);
1154 VkResult
anv_QueueBindSparseImageOpaqueMemory(
1157 uint32_t numBindings
,
1158 const VkSparseMemoryBindInfo
* pBindInfo
)
1160 stub_return(VK_UNSUPPORTED
);
1163 VkResult
anv_QueueBindSparseImageMemory(
1166 uint32_t numBindings
,
1167 const VkSparseImageMemoryBindInfo
* pBindInfo
)
1169 stub_return(VK_UNSUPPORTED
);
1172 VkResult
anv_CreateFence(
1174 const VkFenceCreateInfo
* pCreateInfo
,
1177 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1178 struct anv_fence
*fence
;
1179 struct anv_batch batch
;
1182 const uint32_t fence_size
= 128;
1184 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_FENCE_CREATE_INFO
);
1186 fence
= anv_device_alloc(device
, sizeof(*fence
), 8,
1187 VK_SYSTEM_ALLOC_TYPE_API_OBJECT
);
1189 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
1191 result
= anv_bo_init_new(&fence
->bo
, device
, fence_size
);
1192 if (result
!= VK_SUCCESS
)
1196 anv_gem_mmap(device
, fence
->bo
.gem_handle
, 0, fence
->bo
.size
);
1197 batch
.next
= batch
.start
= fence
->bo
.map
;
1198 batch
.end
= fence
->bo
.map
+ fence
->bo
.size
;
1199 anv_batch_emit(&batch
, GEN7_MI_BATCH_BUFFER_END
);
1200 anv_batch_emit(&batch
, GEN7_MI_NOOP
);
1202 fence
->exec2_objects
[0].handle
= fence
->bo
.gem_handle
;
1203 fence
->exec2_objects
[0].relocation_count
= 0;
1204 fence
->exec2_objects
[0].relocs_ptr
= 0;
1205 fence
->exec2_objects
[0].alignment
= 0;
1206 fence
->exec2_objects
[0].offset
= fence
->bo
.offset
;
1207 fence
->exec2_objects
[0].flags
= 0;
1208 fence
->exec2_objects
[0].rsvd1
= 0;
1209 fence
->exec2_objects
[0].rsvd2
= 0;
1211 fence
->execbuf
.buffers_ptr
= (uintptr_t) fence
->exec2_objects
;
1212 fence
->execbuf
.buffer_count
= 1;
1213 fence
->execbuf
.batch_start_offset
= 0;
1214 fence
->execbuf
.batch_len
= batch
.next
- fence
->bo
.map
;
1215 fence
->execbuf
.cliprects_ptr
= 0;
1216 fence
->execbuf
.num_cliprects
= 0;
1217 fence
->execbuf
.DR1
= 0;
1218 fence
->execbuf
.DR4
= 0;
1220 fence
->execbuf
.flags
=
1221 I915_EXEC_HANDLE_LUT
| I915_EXEC_NO_RELOC
| I915_EXEC_RENDER
;
1222 fence
->execbuf
.rsvd1
= device
->context_id
;
1223 fence
->execbuf
.rsvd2
= 0;
1225 *pFence
= anv_fence_to_handle(fence
);
1230 anv_device_free(device
, fence
);
1235 void anv_DestroyFence(
1239 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1240 ANV_FROM_HANDLE(anv_fence
, fence
, _fence
);
1242 anv_gem_munmap(fence
->bo
.map
, fence
->bo
.size
);
1243 anv_gem_close(device
, fence
->bo
.gem_handle
);
1244 anv_device_free(device
, fence
);
1247 VkResult
anv_ResetFences(
1249 uint32_t fenceCount
,
1250 const VkFence
* pFences
)
1252 for (uint32_t i
= 0; i
< fenceCount
; i
++) {
1253 ANV_FROM_HANDLE(anv_fence
, fence
, pFences
[i
]);
1254 fence
->ready
= false;
1260 VkResult
anv_GetFenceStatus(
1264 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1265 ANV_FROM_HANDLE(anv_fence
, fence
, _fence
);
1272 ret
= anv_gem_wait(device
, fence
->bo
.gem_handle
, &t
);
1274 fence
->ready
= true;
1278 return VK_NOT_READY
;
1281 VkResult
anv_WaitForFences(
1283 uint32_t fenceCount
,
1284 const VkFence
* pFences
,
1288 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1289 int64_t t
= timeout
;
1292 /* FIXME: handle !waitAll */
1294 for (uint32_t i
= 0; i
< fenceCount
; i
++) {
1295 ANV_FROM_HANDLE(anv_fence
, fence
, pFences
[i
]);
1296 ret
= anv_gem_wait(device
, fence
->bo
.gem_handle
, &t
);
1297 if (ret
== -1 && errno
== ETIME
) {
1299 } else if (ret
== -1) {
1300 /* We don't know the real error. */
1301 return vk_errorf(VK_ERROR_OUT_OF_DEVICE_MEMORY
,
1302 "gem wait failed: %m");
1309 // Queue semaphore functions
1311 VkResult
anv_CreateSemaphore(
1313 const VkSemaphoreCreateInfo
* pCreateInfo
,
1314 VkSemaphore
* pSemaphore
)
1316 pSemaphore
->handle
= 1;
1317 stub_return(VK_SUCCESS
);
1320 void anv_DestroySemaphore(
1322 VkSemaphore semaphore
)
1327 VkResult
anv_QueueSignalSemaphore(
1329 VkSemaphore semaphore
)
1331 stub_return(VK_UNSUPPORTED
);
1334 VkResult
anv_QueueWaitSemaphore(
1336 VkSemaphore semaphore
)
1338 stub_return(VK_UNSUPPORTED
);
1343 VkResult
anv_CreateEvent(
1345 const VkEventCreateInfo
* pCreateInfo
,
1348 stub_return(VK_UNSUPPORTED
);
1351 void anv_DestroyEvent(
1358 VkResult
anv_GetEventStatus(
1362 stub_return(VK_UNSUPPORTED
);
1365 VkResult
anv_SetEvent(
1369 stub_return(VK_UNSUPPORTED
);
1372 VkResult
anv_ResetEvent(
1376 stub_return(VK_UNSUPPORTED
);
1381 VkResult
anv_CreateBuffer(
1383 const VkBufferCreateInfo
* pCreateInfo
,
1386 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1387 struct anv_buffer
*buffer
;
1389 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO
);
1391 buffer
= anv_device_alloc(device
, sizeof(*buffer
), 8,
1392 VK_SYSTEM_ALLOC_TYPE_API_OBJECT
);
1394 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
1396 buffer
->size
= pCreateInfo
->size
;
1400 *pBuffer
= anv_buffer_to_handle(buffer
);
1405 void anv_DestroyBuffer(
1409 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1410 ANV_FROM_HANDLE(anv_buffer
, buffer
, _buffer
);
1412 anv_device_free(device
, buffer
);
1416 anv_fill_buffer_surface_state(struct anv_device
*device
, void *state
,
1417 const struct anv_format
*format
,
1418 uint32_t offset
, uint32_t range
)
1420 switch (device
->info
.gen
) {
1422 gen7_fill_buffer_surface_state(state
, format
, offset
, range
);
1425 gen8_fill_buffer_surface_state(state
, format
, offset
, range
);
1428 unreachable("unsupported gen\n");
1433 anv_buffer_view_create(
1434 struct anv_device
* device
,
1435 const VkBufferViewCreateInfo
* pCreateInfo
,
1436 struct anv_buffer_view
** bview_out
)
1438 ANV_FROM_HANDLE(anv_buffer
, buffer
, pCreateInfo
->buffer
);
1439 struct anv_buffer_view
*bview
;
1441 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_BUFFER_VIEW_CREATE_INFO
);
1443 bview
= anv_device_alloc(device
, sizeof(*bview
), 8,
1444 VK_SYSTEM_ALLOC_TYPE_API_OBJECT
);
1446 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
1448 *bview
= (struct anv_buffer_view
) {
1450 .offset
= buffer
->offset
+ pCreateInfo
->offset
,
1451 .surface_state
= anv_state_pool_alloc(&device
->surface_state_pool
, 64, 64),
1452 .format
= anv_format_for_vk_format(pCreateInfo
->format
),
1453 .range
= pCreateInfo
->range
,
1461 void anv_DestroyBufferView(
1463 VkBufferView _bview
)
1465 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1466 ANV_FROM_HANDLE(anv_buffer_view
, bview
, _bview
);
1468 anv_state_pool_free(&device
->surface_state_pool
, bview
->surface_state
);
1469 anv_device_free(device
, bview
);
1472 void anv_DestroySampler(
1476 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1477 ANV_FROM_HANDLE(anv_sampler
, sampler
, _sampler
);
1479 anv_device_free(device
, sampler
);
1482 // Descriptor set functions
1484 VkResult
anv_CreateDescriptorSetLayout(
1486 const VkDescriptorSetLayoutCreateInfo
* pCreateInfo
,
1487 VkDescriptorSetLayout
* pSetLayout
)
1489 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1490 struct anv_descriptor_set_layout
*set_layout
;
1493 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO
);
1495 uint32_t immutable_sampler_count
= 0;
1496 for (uint32_t b
= 0; b
< pCreateInfo
->count
; b
++) {
1497 if (pCreateInfo
->pBinding
[b
].pImmutableSamplers
)
1498 immutable_sampler_count
+= pCreateInfo
->pBinding
[b
].arraySize
;
1501 size_t size
= sizeof(struct anv_descriptor_set_layout
) +
1502 pCreateInfo
->count
* sizeof(set_layout
->binding
[0]) +
1503 immutable_sampler_count
* sizeof(struct anv_sampler
*);
1505 set_layout
= anv_device_alloc(device
, size
, 8,
1506 VK_SYSTEM_ALLOC_TYPE_API_OBJECT
);
1508 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
1510 /* We just allocate all the samplers at the end of the struct */
1511 struct anv_sampler
**samplers
=
1512 (struct anv_sampler
**)&set_layout
->binding
[pCreateInfo
->count
];
1514 set_layout
->binding_count
= pCreateInfo
->count
;
1515 set_layout
->shader_stages
= 0;
1516 set_layout
->size
= 0;
1518 /* Initialize all binding_layout entries to -1 */
1519 memset(set_layout
->binding
, -1,
1520 pCreateInfo
->count
* sizeof(set_layout
->binding
[0]));
1522 /* Initialize all samplers to 0 */
1523 memset(samplers
, 0, immutable_sampler_count
* sizeof(*samplers
));
1525 uint32_t sampler_count
[VK_SHADER_STAGE_NUM
] = { 0, };
1526 uint32_t surface_count
[VK_SHADER_STAGE_NUM
] = { 0, };
1527 uint32_t dynamic_offset_count
= 0;
1529 for (uint32_t b
= 0; b
< pCreateInfo
->count
; b
++) {
1530 uint32_t array_size
= MAX2(1, pCreateInfo
->pBinding
[b
].arraySize
);
1531 set_layout
->binding
[b
].array_size
= array_size
;
1532 set_layout
->binding
[b
].descriptor_index
= set_layout
->size
;
1533 set_layout
->size
+= array_size
;
1535 switch (pCreateInfo
->pBinding
[b
].descriptorType
) {
1536 case VK_DESCRIPTOR_TYPE_SAMPLER
:
1537 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER
:
1538 for_each_bit(s
, pCreateInfo
->pBinding
[b
].stageFlags
) {
1539 set_layout
->binding
[b
].stage
[s
].sampler_index
= sampler_count
[s
];
1540 sampler_count
[s
] += array_size
;
1547 switch (pCreateInfo
->pBinding
[b
].descriptorType
) {
1548 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER
:
1549 case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE
:
1550 case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE
:
1551 case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER
:
1552 case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER
:
1553 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER
:
1554 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER
:
1555 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC
:
1556 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC
:
1557 case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT
:
1558 for_each_bit(s
, pCreateInfo
->pBinding
[b
].stageFlags
) {
1559 set_layout
->binding
[b
].stage
[s
].surface_index
= surface_count
[s
];
1560 surface_count
[s
] += array_size
;
1567 switch (pCreateInfo
->pBinding
[b
].descriptorType
) {
1568 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC
:
1569 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC
:
1570 set_layout
->binding
[b
].dynamic_offset_index
= dynamic_offset_count
;
1571 dynamic_offset_count
+= array_size
;
1577 if (pCreateInfo
->pBinding
[b
].pImmutableSamplers
) {
1578 set_layout
->binding
[b
].immutable_samplers
= samplers
;
1579 samplers
+= array_size
;
1581 for (uint32_t i
= 0; i
< array_size
; i
++)
1582 set_layout
->binding
[b
].immutable_samplers
[i
] =
1583 anv_sampler_from_handle(pCreateInfo
->pBinding
[b
].pImmutableSamplers
[i
]);
1585 set_layout
->binding
[b
].immutable_samplers
= NULL
;
1588 set_layout
->shader_stages
|= pCreateInfo
->pBinding
[b
].stageFlags
;
1591 set_layout
->dynamic_offset_count
= dynamic_offset_count
;
1593 *pSetLayout
= anv_descriptor_set_layout_to_handle(set_layout
);
1598 void anv_DestroyDescriptorSetLayout(
1600 VkDescriptorSetLayout _set_layout
)
1602 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1603 ANV_FROM_HANDLE(anv_descriptor_set_layout
, set_layout
, _set_layout
);
1605 anv_device_free(device
, set_layout
);
1608 VkResult
anv_CreateDescriptorPool(
1610 const VkDescriptorPoolCreateInfo
* pCreateInfo
,
1611 VkDescriptorPool
* pDescriptorPool
)
1613 anv_finishme("VkDescriptorPool is a stub");
1614 pDescriptorPool
->handle
= 1;
1618 void anv_DestroyDescriptorPool(
1620 VkDescriptorPool _pool
)
1622 anv_finishme("VkDescriptorPool is a stub: free the pool's descriptor sets");
1625 VkResult
anv_ResetDescriptorPool(
1627 VkDescriptorPool descriptorPool
)
1629 anv_finishme("VkDescriptorPool is a stub: free the pool's descriptor sets");
1634 anv_descriptor_set_create(struct anv_device
*device
,
1635 const struct anv_descriptor_set_layout
*layout
,
1636 struct anv_descriptor_set
**out_set
)
1638 struct anv_descriptor_set
*set
;
1639 size_t size
= sizeof(*set
) + layout
->size
* sizeof(set
->descriptors
[0]);
1641 set
= anv_device_alloc(device
, size
, 8, VK_SYSTEM_ALLOC_TYPE_API_OBJECT
);
1643 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
1645 /* A descriptor set may not be 100% filled. Clear the set so we can can
1646 * later detect holes in it.
1648 memset(set
, 0, size
);
1650 set
->layout
= layout
;
1652 /* Go through and fill out immutable samplers if we have any */
1653 struct anv_descriptor
*desc
= set
->descriptors
;
1654 for (uint32_t b
= 0; b
< layout
->binding_count
; b
++) {
1655 if (layout
->binding
[b
].immutable_samplers
) {
1656 for (uint32_t i
= 0; i
< layout
->binding
[b
].array_size
; i
++)
1657 desc
[i
].sampler
= layout
->binding
[b
].immutable_samplers
[i
];
1659 desc
+= layout
->binding
[b
].array_size
;
1668 anv_descriptor_set_destroy(struct anv_device
*device
,
1669 struct anv_descriptor_set
*set
)
1671 anv_device_free(device
, set
);
1674 VkResult
anv_AllocDescriptorSets(
1676 VkDescriptorPool descriptorPool
,
1677 VkDescriptorSetUsage setUsage
,
1679 const VkDescriptorSetLayout
* pSetLayouts
,
1680 VkDescriptorSet
* pDescriptorSets
)
1682 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1684 VkResult result
= VK_SUCCESS
;
1685 struct anv_descriptor_set
*set
;
1688 for (i
= 0; i
< count
; i
++) {
1689 ANV_FROM_HANDLE(anv_descriptor_set_layout
, layout
, pSetLayouts
[i
]);
1691 result
= anv_descriptor_set_create(device
, layout
, &set
);
1692 if (result
!= VK_SUCCESS
)
1695 pDescriptorSets
[i
] = anv_descriptor_set_to_handle(set
);
1698 if (result
!= VK_SUCCESS
)
1699 anv_FreeDescriptorSets(_device
, descriptorPool
, i
, pDescriptorSets
);
1704 VkResult
anv_FreeDescriptorSets(
1706 VkDescriptorPool descriptorPool
,
1708 const VkDescriptorSet
* pDescriptorSets
)
1710 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1712 for (uint32_t i
= 0; i
< count
; i
++) {
1713 ANV_FROM_HANDLE(anv_descriptor_set
, set
, pDescriptorSets
[i
]);
1715 anv_descriptor_set_destroy(device
, set
);
1721 void anv_UpdateDescriptorSets(
1723 uint32_t writeCount
,
1724 const VkWriteDescriptorSet
* pDescriptorWrites
,
1726 const VkCopyDescriptorSet
* pDescriptorCopies
)
1728 for (uint32_t i
= 0; i
< writeCount
; i
++) {
1729 const VkWriteDescriptorSet
*write
= &pDescriptorWrites
[i
];
1730 ANV_FROM_HANDLE(anv_descriptor_set
, set
, write
->destSet
);
1731 const struct anv_descriptor_set_binding_layout
*bind_layout
=
1732 &set
->layout
->binding
[write
->destBinding
];
1733 struct anv_descriptor
*desc
=
1734 &set
->descriptors
[bind_layout
->descriptor_index
];
1736 switch (write
->descriptorType
) {
1737 case VK_DESCRIPTOR_TYPE_SAMPLER
:
1738 for (uint32_t j
= 0; j
< write
->count
; j
++) {
1739 ANV_FROM_HANDLE(anv_sampler
, sampler
,
1740 write
->pDescriptors
[j
].sampler
);
1742 desc
[j
] = (struct anv_descriptor
) {
1743 .type
= ANV_DESCRIPTOR_TYPE_SAMPLER
,
1749 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER
:
1750 for (uint32_t j
= 0; j
< write
->count
; j
++) {
1751 ANV_FROM_HANDLE(anv_image_view
, iview
,
1752 write
->pDescriptors
[j
].imageView
);
1753 ANV_FROM_HANDLE(anv_sampler
, sampler
,
1754 write
->pDescriptors
[j
].sampler
);
1756 desc
[j
].type
= ANV_DESCRIPTOR_TYPE_IMAGE_VIEW_AND_SAMPLER
;
1757 desc
[j
].image_view
= iview
;
1759 /* If this descriptor has an immutable sampler, we don't want
1763 desc
->sampler
= sampler
;
1767 case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE
:
1768 case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE
:
1769 for (uint32_t j
= 0; j
< write
->count
; j
++) {
1770 ANV_FROM_HANDLE(anv_image_view
, iview
,
1771 write
->pDescriptors
[j
].imageView
);
1773 desc
[j
] = (struct anv_descriptor
) {
1774 .type
= ANV_DESCRIPTOR_TYPE_IMAGE_VIEW
,
1775 .image_view
= iview
,
1780 case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER
:
1781 case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER
:
1782 anv_finishme("texel buffers not implemented");
1785 case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT
:
1786 anv_finishme("input attachments not implemented");
1789 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER
:
1790 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER
:
1791 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC
:
1792 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC
:
1793 for (uint32_t j
= 0; j
< write
->count
; j
++) {
1794 assert(write
->pDescriptors
[j
].bufferInfo
.buffer
.handle
);
1795 ANV_FROM_HANDLE(anv_buffer
, buffer
,
1796 write
->pDescriptors
[j
].bufferInfo
.buffer
);
1799 desc
[j
] = (struct anv_descriptor
) {
1800 .type
= ANV_DESCRIPTOR_TYPE_BUFFER_AND_OFFSET
,
1802 .offset
= write
->pDescriptors
[j
].bufferInfo
.offset
,
1803 .range
= write
->pDescriptors
[j
].bufferInfo
.range
,
1812 for (uint32_t i
= 0; i
< copyCount
; i
++) {
1813 const VkCopyDescriptorSet
*copy
= &pDescriptorCopies
[i
];
1814 ANV_FROM_HANDLE(anv_descriptor_set
, src
, copy
->destSet
);
1815 ANV_FROM_HANDLE(anv_descriptor_set
, dest
, copy
->destSet
);
1816 for (uint32_t j
= 0; j
< copy
->count
; j
++) {
1817 dest
->descriptors
[copy
->destBinding
+ j
] =
1818 src
->descriptors
[copy
->srcBinding
+ j
];
1823 VkResult
anv_CreateFramebuffer(
1825 const VkFramebufferCreateInfo
* pCreateInfo
,
1826 VkFramebuffer
* pFramebuffer
)
1828 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1829 struct anv_framebuffer
*framebuffer
;
1831 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO
);
1833 size_t size
= sizeof(*framebuffer
) +
1834 sizeof(struct anv_image_view
*) * pCreateInfo
->attachmentCount
;
1835 framebuffer
= anv_device_alloc(device
, size
, 8,
1836 VK_SYSTEM_ALLOC_TYPE_API_OBJECT
);
1837 if (framebuffer
== NULL
)
1838 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
1840 framebuffer
->attachment_count
= pCreateInfo
->attachmentCount
;
1841 for (uint32_t i
= 0; i
< pCreateInfo
->attachmentCount
; i
++) {
1842 VkImageView _iview
= pCreateInfo
->pAttachments
[i
];
1843 framebuffer
->attachments
[i
] = anv_image_view_from_handle(_iview
);
1846 framebuffer
->width
= pCreateInfo
->width
;
1847 framebuffer
->height
= pCreateInfo
->height
;
1848 framebuffer
->layers
= pCreateInfo
->layers
;
1850 *pFramebuffer
= anv_framebuffer_to_handle(framebuffer
);
1855 void anv_DestroyFramebuffer(
1859 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1860 ANV_FROM_HANDLE(anv_framebuffer
, fb
, _fb
);
1862 anv_device_free(device
, fb
);
1865 void vkCmdDbgMarkerBegin(
1866 VkCmdBuffer cmdBuffer
,
1867 const char* pMarker
)
1868 __attribute__ ((visibility ("default")));
1870 void vkCmdDbgMarkerEnd(
1871 VkCmdBuffer cmdBuffer
)
1872 __attribute__ ((visibility ("default")));
1874 void vkCmdDbgMarkerBegin(
1875 VkCmdBuffer cmdBuffer
,
1876 const char* pMarker
)
1880 void vkCmdDbgMarkerEnd(
1881 VkCmdBuffer cmdBuffer
)