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 anv_physical_device_init(struct anv_physical_device
*device
,
38 struct anv_instance
*instance
,
44 fd
= open(path
, O_RDWR
| O_CLOEXEC
);
46 return vk_errorf(VK_ERROR_INITIALIZATION_FAILED
,
47 "failed to open %s: %m", path
);
49 device
->_loader_data
.loaderMagic
= ICD_LOADER_MAGIC
;
50 device
->instance
= instance
;
53 device
->chipset_id
= anv_gem_get_param(fd
, I915_PARAM_CHIPSET_ID
);
54 if (!device
->chipset_id
) {
55 result
= vk_errorf(VK_ERROR_INITIALIZATION_FAILED
,
56 "failed to get chipset id: %m");
60 device
->name
= brw_get_device_name(device
->chipset_id
);
61 device
->info
= brw_get_device_info(device
->chipset_id
, -1);
63 result
= vk_errorf(VK_ERROR_INITIALIZATION_FAILED
,
64 "failed to get device info");
68 if (anv_gem_get_aperture(fd
, &device
->aperture_size
) == -1) {
69 result
= vk_errorf(VK_ERROR_INITIALIZATION_FAILED
,
70 "failed to get aperture size: %m");
74 if (!anv_gem_get_param(fd
, I915_PARAM_HAS_WAIT_TIMEOUT
)) {
75 result
= vk_errorf(VK_ERROR_INITIALIZATION_FAILED
,
76 "kernel missing gem wait");
80 if (!anv_gem_get_param(fd
, I915_PARAM_HAS_EXECBUF2
)) {
81 result
= vk_errorf(VK_ERROR_INITIALIZATION_FAILED
,
82 "kernel missing execbuf2");
86 if (!anv_gem_get_param(fd
, I915_PARAM_HAS_LLC
)) {
87 result
= vk_errorf(VK_ERROR_INITIALIZATION_FAILED
,
101 static void *default_alloc(
105 VkSystemAllocType allocType
)
110 static void default_free(
117 static const VkAllocCallbacks default_alloc_callbacks
= {
119 .pfnAlloc
= default_alloc
,
120 .pfnFree
= default_free
123 static const VkExtensionProperties global_extensions
[] = {
125 .extName
= VK_EXT_KHR_SWAPCHAIN_EXTENSION_NAME
,
130 static const VkExtensionProperties device_extensions
[] = {
132 .extName
= VK_EXT_KHR_DEVICE_SWAPCHAIN_EXTENSION_NAME
,
138 VkResult
anv_CreateInstance(
139 const VkInstanceCreateInfo
* pCreateInfo
,
140 VkInstance
* pInstance
)
142 struct anv_instance
*instance
;
143 const VkAllocCallbacks
*alloc_callbacks
= &default_alloc_callbacks
;
144 void *user_data
= NULL
;
146 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO
);
148 if (pCreateInfo
->pAppInfo
->apiVersion
!= VK_MAKE_VERSION(0, 170, 2))
149 return vk_error(VK_ERROR_INCOMPATIBLE_DRIVER
);
151 for (uint32_t i
= 0; i
< pCreateInfo
->extensionCount
; i
++) {
153 for (uint32_t j
= 0; j
< ARRAY_SIZE(global_extensions
); j
++) {
154 if (strcmp(pCreateInfo
->ppEnabledExtensionNames
[i
],
155 global_extensions
[j
].extName
) == 0) {
161 return vk_error(VK_ERROR_EXTENSION_NOT_PRESENT
);
164 if (pCreateInfo
->pAllocCb
) {
165 alloc_callbacks
= pCreateInfo
->pAllocCb
;
166 user_data
= pCreateInfo
->pAllocCb
->pUserData
;
168 instance
= alloc_callbacks
->pfnAlloc(user_data
, sizeof(*instance
), 8,
169 VK_SYSTEM_ALLOC_TYPE_API_OBJECT
);
171 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
173 instance
->_loader_data
.loaderMagic
= ICD_LOADER_MAGIC
;
174 instance
->pAllocUserData
= alloc_callbacks
->pUserData
;
175 instance
->pfnAlloc
= alloc_callbacks
->pfnAlloc
;
176 instance
->pfnFree
= alloc_callbacks
->pfnFree
;
177 instance
->apiVersion
= pCreateInfo
->pAppInfo
->apiVersion
;
178 instance
->physicalDeviceCount
= 0;
182 VG(VALGRIND_CREATE_MEMPOOL(instance
, 0, false));
184 anv_init_wsi(instance
);
186 *pInstance
= anv_instance_to_handle(instance
);
191 void anv_DestroyInstance(
192 VkInstance _instance
)
194 ANV_FROM_HANDLE(anv_instance
, instance
, _instance
);
196 anv_finish_wsi(instance
);
198 VG(VALGRIND_DESTROY_MEMPOOL(instance
));
202 instance
->pfnFree(instance
->pAllocUserData
, instance
);
206 anv_instance_alloc(struct anv_instance
*instance
, size_t size
,
207 size_t alignment
, VkSystemAllocType allocType
)
209 void *mem
= instance
->pfnAlloc(instance
->pAllocUserData
,
210 size
, alignment
, allocType
);
212 VG(VALGRIND_MEMPOOL_ALLOC(instance
, mem
, size
));
213 VG(VALGRIND_MAKE_MEM_UNDEFINED(mem
, size
));
219 anv_instance_free(struct anv_instance
*instance
, void *mem
)
224 VG(VALGRIND_MEMPOOL_FREE(instance
, mem
));
226 instance
->pfnFree(instance
->pAllocUserData
, mem
);
229 VkResult
anv_EnumeratePhysicalDevices(
230 VkInstance _instance
,
231 uint32_t* pPhysicalDeviceCount
,
232 VkPhysicalDevice
* pPhysicalDevices
)
234 ANV_FROM_HANDLE(anv_instance
, instance
, _instance
);
237 if (instance
->physicalDeviceCount
== 0) {
238 result
= anv_physical_device_init(&instance
->physicalDevice
,
239 instance
, "/dev/dri/renderD128");
240 if (result
!= VK_SUCCESS
)
243 instance
->physicalDeviceCount
= 1;
246 /* pPhysicalDeviceCount is an out parameter if pPhysicalDevices is NULL;
247 * otherwise it's an inout parameter.
249 * The Vulkan spec (git aaed022) says:
251 * pPhysicalDeviceCount is a pointer to an unsigned integer variable
252 * that is initialized with the number of devices the application is
253 * prepared to receive handles to. pname:pPhysicalDevices is pointer to
254 * an array of at least this many VkPhysicalDevice handles [...].
256 * Upon success, if pPhysicalDevices is NULL, vkEnumeratePhysicalDevices
257 * overwrites the contents of the variable pointed to by
258 * pPhysicalDeviceCount with the number of physical devices in in the
259 * instance; otherwise, vkEnumeratePhysicalDevices overwrites
260 * pPhysicalDeviceCount with the number of physical handles written to
263 if (!pPhysicalDevices
) {
264 *pPhysicalDeviceCount
= instance
->physicalDeviceCount
;
265 } else if (*pPhysicalDeviceCount
>= 1) {
266 pPhysicalDevices
[0] = anv_physical_device_to_handle(&instance
->physicalDevice
);
267 *pPhysicalDeviceCount
= 1;
269 *pPhysicalDeviceCount
= 0;
275 VkResult
anv_GetPhysicalDeviceFeatures(
276 VkPhysicalDevice physicalDevice
,
277 VkPhysicalDeviceFeatures
* pFeatures
)
279 anv_finishme("Get correct values for PhysicalDeviceFeatures");
281 *pFeatures
= (VkPhysicalDeviceFeatures
) {
282 .robustBufferAccess
= false,
283 .fullDrawIndexUint32
= false,
284 .imageCubeArray
= false,
285 .independentBlend
= false,
286 .geometryShader
= true,
287 .tessellationShader
= false,
288 .sampleRateShading
= false,
289 .dualSourceBlend
= true,
291 .multiDrawIndirect
= true,
293 .depthBiasClamp
= false,
294 .fillModeNonSolid
= true,
295 .depthBounds
= false,
298 .textureCompressionETC2
= true,
299 .textureCompressionASTC_LDR
= true,
300 .textureCompressionBC
= true,
301 .occlusionQueryNonConservative
= false, /* FINISHME */
302 .pipelineStatisticsQuery
= true,
303 .vertexSideEffects
= false,
304 .tessellationSideEffects
= false,
305 .geometrySideEffects
= false,
306 .fragmentSideEffects
= false,
307 .shaderTessellationPointSize
= false,
308 .shaderGeometryPointSize
= true,
309 .shaderImageGatherExtended
= true,
310 .shaderStorageImageExtendedFormats
= false,
311 .shaderStorageImageMultisample
= false,
312 .shaderUniformBufferArrayDynamicIndexing
= true,
313 .shaderSampledImageArrayDynamicIndexing
= false,
314 .shaderStorageBufferArrayDynamicIndexing
= false,
315 .shaderStorageImageArrayDynamicIndexing
= false,
316 .shaderClipDistance
= false,
317 .shaderCullDistance
= false,
318 .shaderFloat64
= false,
319 .shaderInt64
= false,
320 .shaderInt16
= false,
327 VkResult
anv_GetPhysicalDeviceProperties(
328 VkPhysicalDevice physicalDevice
,
329 VkPhysicalDeviceProperties
* pProperties
)
331 ANV_FROM_HANDLE(anv_physical_device
, pdevice
, physicalDevice
);
332 const struct brw_device_info
*devinfo
= pdevice
->info
;
334 anv_finishme("Get correct values for VkPhysicalDeviceLimits");
336 VkPhysicalDeviceLimits limits
= {
337 .maxImageDimension1D
= (1 << 14),
338 .maxImageDimension2D
= (1 << 14),
339 .maxImageDimension3D
= (1 << 10),
340 .maxImageDimensionCube
= (1 << 14),
341 .maxImageArrayLayers
= (1 << 10),
343 /* Broadwell supports 1, 2, 4, and 8 samples. */
346 .maxTexelBufferSize
= (1 << 14),
347 .maxUniformBufferSize
= UINT32_MAX
,
348 .maxStorageBufferSize
= UINT32_MAX
,
349 .maxPushConstantsSize
= MAX_PUSH_CONSTANTS_SIZE
,
350 .maxMemoryAllocationCount
= UINT32_MAX
,
351 .bufferImageGranularity
= 64, /* A cache line */
352 .sparseAddressSpaceSize
= 0,
353 .maxBoundDescriptorSets
= MAX_SETS
,
354 .maxDescriptorSets
= UINT32_MAX
,
355 .maxPerStageDescriptorSamplers
= 64,
356 .maxPerStageDescriptorUniformBuffers
= 64,
357 .maxPerStageDescriptorStorageBuffers
= 64,
358 .maxPerStageDescriptorSampledImages
= 64,
359 .maxPerStageDescriptorStorageImages
= 64,
360 .maxDescriptorSetSamplers
= 256,
361 .maxDescriptorSetUniformBuffers
= 256,
362 .maxDescriptorSetUniformBuffersDynamic
= 256,
363 .maxDescriptorSetStorageBuffers
= 256,
364 .maxDescriptorSetStorageBuffersDynamic
= 256,
365 .maxDescriptorSetSampledImages
= 256,
366 .maxDescriptorSetStorageImages
= 256,
367 .maxVertexInputAttributes
= 32,
368 .maxVertexInputBindings
= 32,
369 .maxVertexInputAttributeOffset
= 256,
370 .maxVertexInputBindingStride
= 256,
371 .maxVertexOutputComponents
= 32,
372 .maxTessGenLevel
= 0,
373 .maxTessPatchSize
= 0,
374 .maxTessControlPerVertexInputComponents
= 0,
375 .maxTessControlPerVertexOutputComponents
= 0,
376 .maxTessControlPerPatchOutputComponents
= 0,
377 .maxTessControlTotalOutputComponents
= 0,
378 .maxTessEvaluationInputComponents
= 0,
379 .maxTessEvaluationOutputComponents
= 0,
380 .maxGeometryShaderInvocations
= 6,
381 .maxGeometryInputComponents
= 16,
382 .maxGeometryOutputComponents
= 16,
383 .maxGeometryOutputVertices
= 16,
384 .maxGeometryTotalOutputComponents
= 16,
385 .maxFragmentInputComponents
= 16,
386 .maxFragmentOutputBuffers
= 8,
387 .maxFragmentDualSourceBuffers
= 2,
388 .maxFragmentCombinedOutputResources
= 8,
389 .maxComputeSharedMemorySize
= 1024,
390 .maxComputeWorkGroupCount
= {
391 16 * devinfo
->max_cs_threads
,
392 16 * devinfo
->max_cs_threads
,
393 16 * devinfo
->max_cs_threads
,
395 .maxComputeWorkGroupInvocations
= 16 * devinfo
->max_cs_threads
,
396 .maxComputeWorkGroupSize
= {
397 16 * devinfo
->max_cs_threads
,
398 16 * devinfo
->max_cs_threads
,
399 16 * devinfo
->max_cs_threads
,
401 .subPixelPrecisionBits
= 4 /* FIXME */,
402 .subTexelPrecisionBits
= 4 /* FIXME */,
403 .mipmapPrecisionBits
= 4 /* FIXME */,
404 .maxDrawIndexedIndexValue
= UINT32_MAX
,
405 .maxDrawIndirectInstanceCount
= UINT32_MAX
,
406 .primitiveRestartForPatches
= UINT32_MAX
,
407 .maxSamplerLodBias
= 16,
408 .maxSamplerAnisotropy
= 16,
409 .maxViewports
= MAX_VIEWPORTS
,
410 .maxViewportDimensions
= { (1 << 14), (1 << 14) },
411 .viewportBoundsRange
= { -1.0, 1.0 }, /* FIXME */
412 .viewportSubPixelBits
= 13, /* We take a float? */
413 .minMemoryMapAlignment
= 64, /* A cache line */
414 .minTexelBufferOffsetAlignment
= 1,
415 .minUniformBufferOffsetAlignment
= 1,
416 .minStorageBufferOffsetAlignment
= 1,
417 .minTexelOffset
= 0, /* FIXME */
418 .maxTexelOffset
= 0, /* FIXME */
419 .minTexelGatherOffset
= 0, /* FIXME */
420 .maxTexelGatherOffset
= 0, /* FIXME */
421 .minInterpolationOffset
= 0, /* FIXME */
422 .maxInterpolationOffset
= 0, /* FIXME */
423 .subPixelInterpolationOffsetBits
= 0, /* FIXME */
424 .maxFramebufferWidth
= (1 << 14),
425 .maxFramebufferHeight
= (1 << 14),
426 .maxFramebufferLayers
= (1 << 10),
427 .maxFramebufferColorSamples
= 8,
428 .maxFramebufferDepthSamples
= 8,
429 .maxFramebufferStencilSamples
= 8,
430 .maxColorAttachments
= MAX_RTS
,
431 .maxSampledImageColorSamples
= 8,
432 .maxSampledImageDepthSamples
= 8,
433 .maxSampledImageIntegerSamples
= 1,
434 .maxStorageImageSamples
= 1,
435 .maxSampleMaskWords
= 1,
436 .timestampFrequency
= 1000 * 1000 * 1000 / 80,
437 .maxClipDistances
= 0 /* FIXME */,
438 .maxCullDistances
= 0 /* FIXME */,
439 .maxCombinedClipAndCullDistances
= 0 /* FIXME */,
440 .pointSizeRange
= { 0.125, 255.875 },
441 .lineWidthRange
= { 0.0, 7.9921875 },
442 .pointSizeGranularity
= (1.0 / 8.0),
443 .lineWidthGranularity
= (1.0 / 128.0),
446 *pProperties
= (VkPhysicalDeviceProperties
) {
447 .apiVersion
= VK_MAKE_VERSION(0, 170, 2),
450 .deviceId
= pdevice
->chipset_id
,
451 .deviceType
= VK_PHYSICAL_DEVICE_TYPE_INTEGRATED_GPU
,
453 .sparseProperties
= {0}, /* Broadwell doesn't do sparse. */
456 strcpy(pProperties
->deviceName
, pdevice
->name
);
457 snprintf((char *)pProperties
->pipelineCacheUUID
, VK_UUID_LENGTH
,
458 "anv-%s", MESA_GIT_SHA1
+ 4);
463 VkResult
anv_GetPhysicalDeviceQueueFamilyProperties(
464 VkPhysicalDevice physicalDevice
,
466 VkQueueFamilyProperties
* pQueueFamilyProperties
)
468 if (pQueueFamilyProperties
== NULL
) {
473 assert(*pCount
>= 1);
475 *pQueueFamilyProperties
= (VkQueueFamilyProperties
) {
476 .queueFlags
= VK_QUEUE_GRAPHICS_BIT
|
477 VK_QUEUE_COMPUTE_BIT
|
480 .supportsTimestamps
= true,
486 VkResult
anv_GetPhysicalDeviceMemoryProperties(
487 VkPhysicalDevice physicalDevice
,
488 VkPhysicalDeviceMemoryProperties
* pMemoryProperties
)
490 ANV_FROM_HANDLE(anv_physical_device
, physical_device
, physicalDevice
);
491 VkDeviceSize heap_size
;
493 /* Reserve some wiggle room for the driver by exposing only 75% of the
494 * aperture to the heap.
496 heap_size
= 3 * physical_device
->aperture_size
/ 4;
498 /* The property flags below are valid only for llc platforms. */
499 pMemoryProperties
->memoryTypeCount
= 1;
500 pMemoryProperties
->memoryTypes
[0] = (VkMemoryType
) {
501 .propertyFlags
= VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT
,
505 pMemoryProperties
->memoryHeapCount
= 1;
506 pMemoryProperties
->memoryHeaps
[0] = (VkMemoryHeap
) {
508 .flags
= VK_MEMORY_HEAP_HOST_LOCAL_BIT
,
514 PFN_vkVoidFunction
anv_GetInstanceProcAddr(
518 return anv_lookup_entrypoint(pName
);
521 PFN_vkVoidFunction
anv_GetDeviceProcAddr(
525 return anv_lookup_entrypoint(pName
);
529 anv_queue_init(struct anv_device
*device
, struct anv_queue
*queue
)
531 queue
->_loader_data
.loaderMagic
= ICD_LOADER_MAGIC
;
532 queue
->device
= device
;
533 queue
->pool
= &device
->surface_state_pool
;
539 anv_queue_finish(struct anv_queue
*queue
)
544 anv_device_init_border_colors(struct anv_device
*device
)
546 static const VkClearColorValue border_colors
[] = {
547 [VK_BORDER_COLOR_FLOAT_TRANSPARENT_BLACK
] = { .float32
= { 0.0, 0.0, 0.0, 0.0 } },
548 [VK_BORDER_COLOR_FLOAT_OPAQUE_BLACK
] = { .float32
= { 0.0, 0.0, 0.0, 1.0 } },
549 [VK_BORDER_COLOR_FLOAT_OPAQUE_WHITE
] = { .float32
= { 1.0, 1.0, 1.0, 1.0 } },
550 [VK_BORDER_COLOR_INT_TRANSPARENT_BLACK
] = { .uint32
= { 0, 0, 0, 0 } },
551 [VK_BORDER_COLOR_INT_OPAQUE_BLACK
] = { .uint32
= { 0, 0, 0, 1 } },
552 [VK_BORDER_COLOR_INT_OPAQUE_WHITE
] = { .uint32
= { 1, 1, 1, 1 } },
555 device
->border_colors
=
556 anv_state_pool_alloc(&device
->dynamic_state_pool
,
557 sizeof(border_colors
), 32);
558 memcpy(device
->border_colors
.map
, border_colors
, sizeof(border_colors
));
561 VkResult
anv_CreateDevice(
562 VkPhysicalDevice physicalDevice
,
563 const VkDeviceCreateInfo
* pCreateInfo
,
566 ANV_FROM_HANDLE(anv_physical_device
, physical_device
, physicalDevice
);
567 struct anv_instance
*instance
= physical_device
->instance
;
568 struct anv_device
*device
;
570 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO
);
572 for (uint32_t i
= 0; i
< pCreateInfo
->extensionCount
; i
++) {
574 for (uint32_t j
= 0; j
< ARRAY_SIZE(device_extensions
); j
++) {
575 if (strcmp(pCreateInfo
->ppEnabledExtensionNames
[i
],
576 device_extensions
[j
].extName
) == 0) {
582 return vk_error(VK_ERROR_EXTENSION_NOT_PRESENT
);
585 anv_set_dispatch_gen(physical_device
->info
->gen
);
587 device
= anv_instance_alloc(instance
, sizeof(*device
), 8,
588 VK_SYSTEM_ALLOC_TYPE_API_OBJECT
);
590 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
592 device
->_loader_data
.loaderMagic
= ICD_LOADER_MAGIC
;
593 device
->instance
= physical_device
->instance
;
595 /* XXX(chadv): Can we dup() physicalDevice->fd here? */
596 device
->fd
= open(physical_device
->path
, O_RDWR
| O_CLOEXEC
);
597 if (device
->fd
== -1)
600 device
->context_id
= anv_gem_create_context(device
);
601 if (device
->context_id
== -1)
604 pthread_mutex_init(&device
->mutex
, NULL
);
606 anv_bo_pool_init(&device
->batch_bo_pool
, device
, ANV_CMD_BUFFER_BATCH_SIZE
);
608 anv_block_pool_init(&device
->dynamic_state_block_pool
, device
, 2048);
610 anv_state_pool_init(&device
->dynamic_state_pool
,
611 &device
->dynamic_state_block_pool
);
613 anv_block_pool_init(&device
->instruction_block_pool
, device
, 2048);
614 anv_block_pool_init(&device
->surface_state_block_pool
, device
, 4096);
616 anv_state_pool_init(&device
->surface_state_pool
,
617 &device
->surface_state_block_pool
);
619 anv_block_pool_init(&device
->scratch_block_pool
, device
, 0x10000);
621 device
->info
= *physical_device
->info
;
623 device
->compiler
= anv_compiler_create(device
);
625 anv_queue_init(device
, &device
->queue
);
627 anv_device_init_meta(device
);
629 anv_device_init_border_colors(device
);
631 *pDevice
= anv_device_to_handle(device
);
638 anv_device_free(device
, device
);
640 return vk_error(VK_ERROR_INITIALIZATION_FAILED
);
643 void anv_DestroyDevice(
646 ANV_FROM_HANDLE(anv_device
, device
, _device
);
648 anv_compiler_destroy(device
->compiler
);
650 anv_queue_finish(&device
->queue
);
652 anv_device_finish_meta(device
);
655 /* We only need to free these to prevent valgrind errors. The backing
656 * BO will go away in a couple of lines so we don't actually leak.
658 anv_state_pool_free(&device
->dynamic_state_pool
, device
->border_colors
);
661 anv_bo_pool_finish(&device
->batch_bo_pool
);
662 anv_state_pool_finish(&device
->dynamic_state_pool
);
663 anv_block_pool_finish(&device
->dynamic_state_block_pool
);
664 anv_block_pool_finish(&device
->instruction_block_pool
);
665 anv_state_pool_finish(&device
->surface_state_pool
);
666 anv_block_pool_finish(&device
->surface_state_block_pool
);
667 anv_block_pool_finish(&device
->scratch_block_pool
);
671 anv_instance_free(device
->instance
, device
);
674 VkResult
anv_EnumerateInstanceExtensionProperties(
675 const char* pLayerName
,
677 VkExtensionProperties
* pProperties
)
679 if (pProperties
== NULL
) {
680 *pCount
= ARRAY_SIZE(global_extensions
);
684 assert(*pCount
>= ARRAY_SIZE(global_extensions
));
686 *pCount
= ARRAY_SIZE(global_extensions
);
687 memcpy(pProperties
, global_extensions
, sizeof(global_extensions
));
692 VkResult
anv_EnumerateDeviceExtensionProperties(
693 VkPhysicalDevice physicalDevice
,
694 const char* pLayerName
,
696 VkExtensionProperties
* pProperties
)
698 if (pProperties
== NULL
) {
699 *pCount
= ARRAY_SIZE(device_extensions
);
703 assert(*pCount
>= ARRAY_SIZE(device_extensions
));
705 *pCount
= ARRAY_SIZE(device_extensions
);
706 memcpy(pProperties
, device_extensions
, sizeof(device_extensions
));
711 VkResult
anv_EnumerateInstanceLayerProperties(
713 VkLayerProperties
* pProperties
)
715 if (pProperties
== NULL
) {
720 /* None supported at this time */
721 return vk_error(VK_ERROR_LAYER_NOT_PRESENT
);
724 VkResult
anv_EnumerateDeviceLayerProperties(
725 VkPhysicalDevice physicalDevice
,
727 VkLayerProperties
* pProperties
)
729 if (pProperties
== NULL
) {
734 /* None supported at this time */
735 return vk_error(VK_ERROR_LAYER_NOT_PRESENT
);
738 VkResult
anv_GetDeviceQueue(
740 uint32_t queueNodeIndex
,
744 ANV_FROM_HANDLE(anv_device
, device
, _device
);
746 assert(queueIndex
== 0);
748 *pQueue
= anv_queue_to_handle(&device
->queue
);
753 VkResult
anv_QueueSubmit(
755 uint32_t cmdBufferCount
,
756 const VkCmdBuffer
* pCmdBuffers
,
759 ANV_FROM_HANDLE(anv_queue
, queue
, _queue
);
760 ANV_FROM_HANDLE(anv_fence
, fence
, _fence
);
761 struct anv_device
*device
= queue
->device
;
764 for (uint32_t i
= 0; i
< cmdBufferCount
; i
++) {
765 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
, pCmdBuffers
[i
]);
767 assert(cmd_buffer
->level
== VK_CMD_BUFFER_LEVEL_PRIMARY
);
769 ret
= anv_gem_execbuffer(device
, &cmd_buffer
->execbuf2
.execbuf
);
771 /* We don't know the real error. */
772 return vk_errorf(VK_ERROR_OUT_OF_DEVICE_MEMORY
,
773 "execbuf2 failed: %m");
777 ret
= anv_gem_execbuffer(device
, &fence
->execbuf
);
779 /* We don't know the real error. */
780 return vk_errorf(VK_ERROR_OUT_OF_DEVICE_MEMORY
,
781 "execbuf2 failed: %m");
785 for (uint32_t i
= 0; i
< cmd_buffer
->execbuf2
.bo_count
; i
++)
786 cmd_buffer
->execbuf2
.bos
[i
]->offset
= cmd_buffer
->execbuf2
.objects
[i
].offset
;
792 VkResult
anv_QueueWaitIdle(
795 ANV_FROM_HANDLE(anv_queue
, queue
, _queue
);
797 return ANV_CALL(DeviceWaitIdle
)(anv_device_to_handle(queue
->device
));
800 VkResult
anv_DeviceWaitIdle(
803 ANV_FROM_HANDLE(anv_device
, device
, _device
);
804 struct anv_state state
;
805 struct anv_batch batch
;
806 struct drm_i915_gem_execbuffer2 execbuf
;
807 struct drm_i915_gem_exec_object2 exec2_objects
[1];
808 struct anv_bo
*bo
= NULL
;
813 state
= anv_state_pool_alloc(&device
->dynamic_state_pool
, 32, 32);
814 bo
= &device
->dynamic_state_pool
.block_pool
->bo
;
815 batch
.start
= batch
.next
= state
.map
;
816 batch
.end
= state
.map
+ 32;
817 anv_batch_emit(&batch
, GEN7_MI_BATCH_BUFFER_END
);
818 anv_batch_emit(&batch
, GEN7_MI_NOOP
);
820 exec2_objects
[0].handle
= bo
->gem_handle
;
821 exec2_objects
[0].relocation_count
= 0;
822 exec2_objects
[0].relocs_ptr
= 0;
823 exec2_objects
[0].alignment
= 0;
824 exec2_objects
[0].offset
= bo
->offset
;
825 exec2_objects
[0].flags
= 0;
826 exec2_objects
[0].rsvd1
= 0;
827 exec2_objects
[0].rsvd2
= 0;
829 execbuf
.buffers_ptr
= (uintptr_t) exec2_objects
;
830 execbuf
.buffer_count
= 1;
831 execbuf
.batch_start_offset
= state
.offset
;
832 execbuf
.batch_len
= batch
.next
- state
.map
;
833 execbuf
.cliprects_ptr
= 0;
834 execbuf
.num_cliprects
= 0;
839 I915_EXEC_HANDLE_LUT
| I915_EXEC_NO_RELOC
| I915_EXEC_RENDER
;
840 execbuf
.rsvd1
= device
->context_id
;
843 ret
= anv_gem_execbuffer(device
, &execbuf
);
845 /* We don't know the real error. */
846 result
= vk_errorf(VK_ERROR_OUT_OF_DEVICE_MEMORY
, "execbuf2 failed: %m");
851 ret
= anv_gem_wait(device
, bo
->gem_handle
, &timeout
);
853 /* We don't know the real error. */
854 result
= vk_errorf(VK_ERROR_OUT_OF_DEVICE_MEMORY
, "execbuf2 failed: %m");
858 anv_state_pool_free(&device
->dynamic_state_pool
, state
);
863 anv_state_pool_free(&device
->dynamic_state_pool
, state
);
869 anv_device_alloc(struct anv_device
* device
,
872 VkSystemAllocType allocType
)
874 return anv_instance_alloc(device
->instance
, size
, alignment
, allocType
);
878 anv_device_free(struct anv_device
* device
,
881 anv_instance_free(device
->instance
, mem
);
885 anv_bo_init_new(struct anv_bo
*bo
, struct anv_device
*device
, uint64_t size
)
887 bo
->gem_handle
= anv_gem_create(device
, size
);
889 return vk_error(VK_ERROR_OUT_OF_DEVICE_MEMORY
);
899 VkResult
anv_AllocMemory(
901 const VkMemoryAllocInfo
* pAllocInfo
,
902 VkDeviceMemory
* pMem
)
904 ANV_FROM_HANDLE(anv_device
, device
, _device
);
905 struct anv_device_memory
*mem
;
908 assert(pAllocInfo
->sType
== VK_STRUCTURE_TYPE_MEMORY_ALLOC_INFO
);
910 /* We support exactly one memory heap. */
911 assert(pAllocInfo
->memoryTypeIndex
== 0);
913 /* FINISHME: Fail if allocation request exceeds heap size. */
915 mem
= anv_device_alloc(device
, sizeof(*mem
), 8,
916 VK_SYSTEM_ALLOC_TYPE_API_OBJECT
);
918 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
920 result
= anv_bo_init_new(&mem
->bo
, device
, pAllocInfo
->allocationSize
);
921 if (result
!= VK_SUCCESS
)
924 *pMem
= anv_device_memory_to_handle(mem
);
929 anv_device_free(device
, mem
);
938 ANV_FROM_HANDLE(anv_device
, device
, _device
);
939 ANV_FROM_HANDLE(anv_device_memory
, mem
, _mem
);
942 anv_gem_munmap(mem
->bo
.map
, mem
->bo
.size
);
944 if (mem
->bo
.gem_handle
!= 0)
945 anv_gem_close(device
, mem
->bo
.gem_handle
);
947 anv_device_free(device
, mem
);
950 VkResult
anv_MapMemory(
955 VkMemoryMapFlags flags
,
958 ANV_FROM_HANDLE(anv_device
, device
, _device
);
959 ANV_FROM_HANDLE(anv_device_memory
, mem
, _mem
);
961 /* FIXME: Is this supposed to be thread safe? Since vkUnmapMemory() only
962 * takes a VkDeviceMemory pointer, it seems like only one map of the memory
963 * at a time is valid. We could just mmap up front and return an offset
964 * pointer here, but that may exhaust virtual memory on 32 bit
967 mem
->map
= anv_gem_mmap(device
, mem
->bo
.gem_handle
, offset
, size
);
968 mem
->map_size
= size
;
975 void anv_UnmapMemory(
979 ANV_FROM_HANDLE(anv_device_memory
, mem
, _mem
);
981 anv_gem_munmap(mem
->map
, mem
->map_size
);
984 VkResult
anv_FlushMappedMemoryRanges(
986 uint32_t memRangeCount
,
987 const VkMappedMemoryRange
* pMemRanges
)
989 /* clflush here for !llc platforms */
994 VkResult
anv_InvalidateMappedMemoryRanges(
996 uint32_t memRangeCount
,
997 const VkMappedMemoryRange
* pMemRanges
)
999 return anv_FlushMappedMemoryRanges(device
, memRangeCount
, pMemRanges
);
1002 VkResult
anv_GetBufferMemoryRequirements(
1005 VkMemoryRequirements
* pMemoryRequirements
)
1007 ANV_FROM_HANDLE(anv_buffer
, buffer
, _buffer
);
1009 /* The Vulkan spec (git aaed022) says:
1011 * memoryTypeBits is a bitfield and contains one bit set for every
1012 * supported memory type for the resource. The bit `1<<i` is set if and
1013 * only if the memory type `i` in the VkPhysicalDeviceMemoryProperties
1014 * structure for the physical device is supported.
1016 * We support exactly one memory type.
1018 pMemoryRequirements
->memoryTypeBits
= 1;
1020 pMemoryRequirements
->size
= buffer
->size
;
1021 pMemoryRequirements
->alignment
= 16;
1026 VkResult
anv_GetImageMemoryRequirements(
1029 VkMemoryRequirements
* pMemoryRequirements
)
1031 ANV_FROM_HANDLE(anv_image
, image
, _image
);
1033 /* The Vulkan spec (git aaed022) says:
1035 * memoryTypeBits is a bitfield and contains one bit set for every
1036 * supported memory type for the resource. The bit `1<<i` is set if and
1037 * only if the memory type `i` in the VkPhysicalDeviceMemoryProperties
1038 * structure for the physical device is supported.
1040 * We support exactly one memory type.
1042 pMemoryRequirements
->memoryTypeBits
= 1;
1044 pMemoryRequirements
->size
= image
->size
;
1045 pMemoryRequirements
->alignment
= image
->alignment
;
1050 VkResult
anv_GetImageSparseMemoryRequirements(
1053 uint32_t* pNumRequirements
,
1054 VkSparseImageMemoryRequirements
* pSparseMemoryRequirements
)
1056 return vk_error(VK_UNSUPPORTED
);
1059 VkResult
anv_GetDeviceMemoryCommitment(
1061 VkDeviceMemory memory
,
1062 VkDeviceSize
* pCommittedMemoryInBytes
)
1064 *pCommittedMemoryInBytes
= 0;
1065 stub_return(VK_SUCCESS
);
1068 VkResult
anv_BindBufferMemory(
1071 VkDeviceMemory _mem
,
1072 VkDeviceSize memOffset
)
1074 ANV_FROM_HANDLE(anv_device_memory
, mem
, _mem
);
1075 ANV_FROM_HANDLE(anv_buffer
, buffer
, _buffer
);
1077 buffer
->bo
= &mem
->bo
;
1078 buffer
->offset
= memOffset
;
1083 VkResult
anv_BindImageMemory(
1086 VkDeviceMemory _mem
,
1087 VkDeviceSize memOffset
)
1089 ANV_FROM_HANDLE(anv_device_memory
, mem
, _mem
);
1090 ANV_FROM_HANDLE(anv_image
, image
, _image
);
1092 image
->bo
= &mem
->bo
;
1093 image
->offset
= memOffset
;
1098 VkResult
anv_QueueBindSparseBufferMemory(
1101 uint32_t numBindings
,
1102 const VkSparseMemoryBindInfo
* pBindInfo
)
1104 stub_return(VK_UNSUPPORTED
);
1107 VkResult
anv_QueueBindSparseImageOpaqueMemory(
1110 uint32_t numBindings
,
1111 const VkSparseMemoryBindInfo
* pBindInfo
)
1113 stub_return(VK_UNSUPPORTED
);
1116 VkResult
anv_QueueBindSparseImageMemory(
1119 uint32_t numBindings
,
1120 const VkSparseImageMemoryBindInfo
* pBindInfo
)
1122 stub_return(VK_UNSUPPORTED
);
1125 VkResult
anv_CreateFence(
1127 const VkFenceCreateInfo
* pCreateInfo
,
1130 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1131 struct anv_fence
*fence
;
1132 struct anv_batch batch
;
1135 const uint32_t fence_size
= 128;
1137 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_FENCE_CREATE_INFO
);
1139 fence
= anv_device_alloc(device
, sizeof(*fence
), 8,
1140 VK_SYSTEM_ALLOC_TYPE_API_OBJECT
);
1142 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
1144 result
= anv_bo_init_new(&fence
->bo
, device
, fence_size
);
1145 if (result
!= VK_SUCCESS
)
1149 anv_gem_mmap(device
, fence
->bo
.gem_handle
, 0, fence
->bo
.size
);
1150 batch
.next
= batch
.start
= fence
->bo
.map
;
1151 batch
.end
= fence
->bo
.map
+ fence
->bo
.size
;
1152 anv_batch_emit(&batch
, GEN7_MI_BATCH_BUFFER_END
);
1153 anv_batch_emit(&batch
, GEN7_MI_NOOP
);
1155 fence
->exec2_objects
[0].handle
= fence
->bo
.gem_handle
;
1156 fence
->exec2_objects
[0].relocation_count
= 0;
1157 fence
->exec2_objects
[0].relocs_ptr
= 0;
1158 fence
->exec2_objects
[0].alignment
= 0;
1159 fence
->exec2_objects
[0].offset
= fence
->bo
.offset
;
1160 fence
->exec2_objects
[0].flags
= 0;
1161 fence
->exec2_objects
[0].rsvd1
= 0;
1162 fence
->exec2_objects
[0].rsvd2
= 0;
1164 fence
->execbuf
.buffers_ptr
= (uintptr_t) fence
->exec2_objects
;
1165 fence
->execbuf
.buffer_count
= 1;
1166 fence
->execbuf
.batch_start_offset
= 0;
1167 fence
->execbuf
.batch_len
= batch
.next
- fence
->bo
.map
;
1168 fence
->execbuf
.cliprects_ptr
= 0;
1169 fence
->execbuf
.num_cliprects
= 0;
1170 fence
->execbuf
.DR1
= 0;
1171 fence
->execbuf
.DR4
= 0;
1173 fence
->execbuf
.flags
=
1174 I915_EXEC_HANDLE_LUT
| I915_EXEC_NO_RELOC
| I915_EXEC_RENDER
;
1175 fence
->execbuf
.rsvd1
= device
->context_id
;
1176 fence
->execbuf
.rsvd2
= 0;
1178 *pFence
= anv_fence_to_handle(fence
);
1183 anv_device_free(device
, fence
);
1188 void anv_DestroyFence(
1192 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1193 ANV_FROM_HANDLE(anv_fence
, fence
, _fence
);
1195 anv_gem_munmap(fence
->bo
.map
, fence
->bo
.size
);
1196 anv_gem_close(device
, fence
->bo
.gem_handle
);
1197 anv_device_free(device
, fence
);
1200 VkResult
anv_ResetFences(
1202 uint32_t fenceCount
,
1203 const VkFence
* pFences
)
1205 for (uint32_t i
= 0; i
< fenceCount
; i
++) {
1206 ANV_FROM_HANDLE(anv_fence
, fence
, pFences
[i
]);
1207 fence
->ready
= false;
1213 VkResult
anv_GetFenceStatus(
1217 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1218 ANV_FROM_HANDLE(anv_fence
, fence
, _fence
);
1225 ret
= anv_gem_wait(device
, fence
->bo
.gem_handle
, &t
);
1227 fence
->ready
= true;
1231 return VK_NOT_READY
;
1234 VkResult
anv_WaitForFences(
1236 uint32_t fenceCount
,
1237 const VkFence
* pFences
,
1241 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1242 int64_t t
= timeout
;
1245 /* FIXME: handle !waitAll */
1247 for (uint32_t i
= 0; i
< fenceCount
; i
++) {
1248 ANV_FROM_HANDLE(anv_fence
, fence
, pFences
[i
]);
1249 ret
= anv_gem_wait(device
, fence
->bo
.gem_handle
, &t
);
1250 if (ret
== -1 && errno
== ETIME
) {
1252 } else if (ret
== -1) {
1253 /* We don't know the real error. */
1254 return vk_errorf(VK_ERROR_OUT_OF_DEVICE_MEMORY
,
1255 "gem wait failed: %m");
1262 // Queue semaphore functions
1264 VkResult
anv_CreateSemaphore(
1266 const VkSemaphoreCreateInfo
* pCreateInfo
,
1267 VkSemaphore
* pSemaphore
)
1269 pSemaphore
->handle
= 1;
1270 stub_return(VK_SUCCESS
);
1273 void anv_DestroySemaphore(
1275 VkSemaphore semaphore
)
1280 VkResult
anv_QueueSignalSemaphore(
1282 VkSemaphore semaphore
)
1284 stub_return(VK_UNSUPPORTED
);
1287 VkResult
anv_QueueWaitSemaphore(
1289 VkSemaphore semaphore
)
1291 stub_return(VK_UNSUPPORTED
);
1296 VkResult
anv_CreateEvent(
1298 const VkEventCreateInfo
* pCreateInfo
,
1301 stub_return(VK_UNSUPPORTED
);
1304 void anv_DestroyEvent(
1311 VkResult
anv_GetEventStatus(
1315 stub_return(VK_UNSUPPORTED
);
1318 VkResult
anv_SetEvent(
1322 stub_return(VK_UNSUPPORTED
);
1325 VkResult
anv_ResetEvent(
1329 stub_return(VK_UNSUPPORTED
);
1334 VkResult
anv_CreateBuffer(
1336 const VkBufferCreateInfo
* pCreateInfo
,
1339 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1340 struct anv_buffer
*buffer
;
1342 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO
);
1344 buffer
= anv_device_alloc(device
, sizeof(*buffer
), 8,
1345 VK_SYSTEM_ALLOC_TYPE_API_OBJECT
);
1347 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
1349 buffer
->size
= pCreateInfo
->size
;
1353 *pBuffer
= anv_buffer_to_handle(buffer
);
1358 void anv_DestroyBuffer(
1362 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1363 ANV_FROM_HANDLE(anv_buffer
, buffer
, _buffer
);
1365 anv_device_free(device
, buffer
);
1369 anv_fill_buffer_surface_state(struct anv_device
*device
, void *state
,
1370 const struct anv_format
*format
,
1371 uint32_t offset
, uint32_t range
)
1373 switch (device
->info
.gen
) {
1375 gen7_fill_buffer_surface_state(state
, format
, offset
, range
);
1378 gen8_fill_buffer_surface_state(state
, format
, offset
, range
);
1381 unreachable("unsupported gen\n");
1386 anv_buffer_view_create(
1387 struct anv_device
* device
,
1388 const VkBufferViewCreateInfo
* pCreateInfo
,
1389 struct anv_buffer_view
** bview_out
)
1391 ANV_FROM_HANDLE(anv_buffer
, buffer
, pCreateInfo
->buffer
);
1392 struct anv_buffer_view
*bview
;
1394 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_BUFFER_VIEW_CREATE_INFO
);
1396 bview
= anv_device_alloc(device
, sizeof(*bview
), 8,
1397 VK_SYSTEM_ALLOC_TYPE_API_OBJECT
);
1399 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
1401 *bview
= (struct anv_buffer_view
) {
1403 .offset
= buffer
->offset
+ pCreateInfo
->offset
,
1404 .surface_state
= anv_state_pool_alloc(&device
->surface_state_pool
, 64, 64),
1405 .format
= anv_format_for_vk_format(pCreateInfo
->format
),
1406 .range
= pCreateInfo
->range
,
1414 void anv_DestroyBufferView(
1416 VkBufferView _bview
)
1418 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1419 ANV_FROM_HANDLE(anv_buffer_view
, bview
, _bview
);
1421 anv_state_pool_free(&device
->surface_state_pool
, bview
->surface_state
);
1422 anv_device_free(device
, bview
);
1425 void anv_DestroySampler(
1429 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1430 ANV_FROM_HANDLE(anv_sampler
, sampler
, _sampler
);
1432 anv_device_free(device
, sampler
);
1435 // Descriptor set functions
1437 VkResult
anv_CreateDescriptorSetLayout(
1439 const VkDescriptorSetLayoutCreateInfo
* pCreateInfo
,
1440 VkDescriptorSetLayout
* pSetLayout
)
1442 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1443 struct anv_descriptor_set_layout
*set_layout
;
1446 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO
);
1448 uint32_t immutable_sampler_count
= 0;
1449 for (uint32_t b
= 0; b
< pCreateInfo
->count
; b
++) {
1450 if (pCreateInfo
->pBinding
[b
].pImmutableSamplers
)
1451 immutable_sampler_count
+= pCreateInfo
->pBinding
[b
].arraySize
;
1454 size_t size
= sizeof(struct anv_descriptor_set_layout
) +
1455 pCreateInfo
->count
* sizeof(set_layout
->binding
[0]) +
1456 immutable_sampler_count
* sizeof(struct anv_sampler
*);
1458 set_layout
= anv_device_alloc(device
, size
, 8,
1459 VK_SYSTEM_ALLOC_TYPE_API_OBJECT
);
1461 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
1463 /* We just allocate all the samplers at the end of the struct */
1464 struct anv_sampler
**samplers
=
1465 (struct anv_sampler
**)&set_layout
->binding
[pCreateInfo
->count
];
1467 set_layout
->binding_count
= pCreateInfo
->count
;
1468 set_layout
->shader_stages
= 0;
1469 set_layout
->size
= 0;
1471 /* Initialize all binding_layout entries to -1 */
1472 memset(set_layout
->binding
, -1,
1473 pCreateInfo
->count
* sizeof(set_layout
->binding
[0]));
1475 /* Initialize all samplers to 0 */
1476 memset(samplers
, 0, immutable_sampler_count
* sizeof(*samplers
));
1478 uint32_t sampler_count
[VK_SHADER_STAGE_NUM
] = { 0, };
1479 uint32_t surface_count
[VK_SHADER_STAGE_NUM
] = { 0, };
1480 uint32_t dynamic_offset_count
= 0;
1482 for (uint32_t b
= 0; b
< pCreateInfo
->count
; b
++) {
1483 uint32_t array_size
= MAX2(1, pCreateInfo
->pBinding
[b
].arraySize
);
1484 set_layout
->binding
[b
].array_size
= array_size
;
1485 set_layout
->size
+= array_size
;
1487 switch (pCreateInfo
->pBinding
[b
].descriptorType
) {
1488 case VK_DESCRIPTOR_TYPE_SAMPLER
:
1489 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER
:
1490 for_each_bit(s
, pCreateInfo
->pBinding
[b
].stageFlags
) {
1491 set_layout
->binding
[b
].stage
[s
].sampler_index
= sampler_count
[s
];
1492 sampler_count
[s
] += array_size
;
1499 switch (pCreateInfo
->pBinding
[b
].descriptorType
) {
1500 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER
:
1501 case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE
:
1502 case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE
:
1503 case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER
:
1504 case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER
:
1505 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER
:
1506 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER
:
1507 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC
:
1508 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC
:
1509 case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT
:
1510 for_each_bit(s
, pCreateInfo
->pBinding
[b
].stageFlags
) {
1511 set_layout
->binding
[b
].stage
[s
].surface_index
= surface_count
[s
];
1512 surface_count
[s
] += array_size
;
1519 switch (pCreateInfo
->pBinding
[b
].descriptorType
) {
1520 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC
:
1521 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC
:
1522 set_layout
->binding
[b
].dynamic_offset_index
= dynamic_offset_count
;
1523 dynamic_offset_count
+= array_size
;
1529 if (pCreateInfo
->pBinding
[b
].pImmutableSamplers
) {
1530 set_layout
->binding
[b
].immutable_samplers
= samplers
;
1531 samplers
+= array_size
;
1533 for (uint32_t i
= 0; i
< array_size
; i
++)
1534 set_layout
->binding
[b
].immutable_samplers
[i
] =
1535 anv_sampler_from_handle(pCreateInfo
->pBinding
[b
].pImmutableSamplers
[i
]);
1537 set_layout
->binding
[b
].immutable_samplers
= NULL
;
1540 set_layout
->shader_stages
|= pCreateInfo
->pBinding
[b
].stageFlags
;
1543 set_layout
->dynamic_offset_count
= dynamic_offset_count
;
1545 *pSetLayout
= anv_descriptor_set_layout_to_handle(set_layout
);
1550 void anv_DestroyDescriptorSetLayout(
1552 VkDescriptorSetLayout _set_layout
)
1554 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1555 ANV_FROM_HANDLE(anv_descriptor_set_layout
, set_layout
, _set_layout
);
1557 anv_device_free(device
, set_layout
);
1560 VkResult
anv_CreateDescriptorPool(
1562 const VkDescriptorPoolCreateInfo
* pCreateInfo
,
1563 VkDescriptorPool
* pDescriptorPool
)
1565 anv_finishme("VkDescriptorPool is a stub");
1566 pDescriptorPool
->handle
= 1;
1570 void anv_DestroyDescriptorPool(
1572 VkDescriptorPool _pool
)
1574 anv_finishme("VkDescriptorPool is a stub: free the pool's descriptor sets");
1577 VkResult
anv_ResetDescriptorPool(
1579 VkDescriptorPool descriptorPool
)
1581 anv_finishme("VkDescriptorPool is a stub: free the pool's descriptor sets");
1586 anv_descriptor_set_create(struct anv_device
*device
,
1587 const struct anv_descriptor_set_layout
*layout
,
1588 struct anv_descriptor_set
**out_set
)
1590 struct anv_descriptor_set
*set
;
1591 size_t size
= sizeof(*set
) + layout
->size
* sizeof(set
->descriptors
[0]);
1593 set
= anv_device_alloc(device
, size
, 8, VK_SYSTEM_ALLOC_TYPE_API_OBJECT
);
1595 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
1597 /* A descriptor set may not be 100% filled. Clear the set so we can can
1598 * later detect holes in it.
1600 memset(set
, 0, size
);
1602 /* Go through and fill out immutable samplers if we have any */
1603 struct anv_descriptor
*desc
= set
->descriptors
;
1604 for (uint32_t b
= 0; b
< layout
->binding_count
; b
++) {
1605 if (layout
->binding
[b
].immutable_samplers
) {
1606 for (uint32_t i
= 0; i
< layout
->binding
[b
].array_size
; i
++)
1607 desc
[i
].sampler
= layout
->binding
[b
].immutable_samplers
[i
];
1609 desc
+= layout
->binding
[b
].array_size
;
1618 anv_descriptor_set_destroy(struct anv_device
*device
,
1619 struct anv_descriptor_set
*set
)
1621 anv_device_free(device
, set
);
1624 VkResult
anv_AllocDescriptorSets(
1626 VkDescriptorPool descriptorPool
,
1627 VkDescriptorSetUsage setUsage
,
1629 const VkDescriptorSetLayout
* pSetLayouts
,
1630 VkDescriptorSet
* pDescriptorSets
)
1632 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1634 VkResult result
= VK_SUCCESS
;
1635 struct anv_descriptor_set
*set
;
1638 for (i
= 0; i
< count
; i
++) {
1639 ANV_FROM_HANDLE(anv_descriptor_set_layout
, layout
, pSetLayouts
[i
]);
1641 result
= anv_descriptor_set_create(device
, layout
, &set
);
1642 if (result
!= VK_SUCCESS
)
1645 pDescriptorSets
[i
] = anv_descriptor_set_to_handle(set
);
1648 if (result
!= VK_SUCCESS
)
1649 anv_FreeDescriptorSets(_device
, descriptorPool
, i
, pDescriptorSets
);
1654 VkResult
anv_FreeDescriptorSets(
1656 VkDescriptorPool descriptorPool
,
1658 const VkDescriptorSet
* pDescriptorSets
)
1660 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1662 for (uint32_t i
= 0; i
< count
; i
++) {
1663 ANV_FROM_HANDLE(anv_descriptor_set
, set
, pDescriptorSets
[i
]);
1665 anv_descriptor_set_destroy(device
, set
);
1671 void anv_UpdateDescriptorSets(
1673 uint32_t writeCount
,
1674 const VkWriteDescriptorSet
* pDescriptorWrites
,
1676 const VkCopyDescriptorSet
* pDescriptorCopies
)
1678 for (uint32_t i
= 0; i
< writeCount
; i
++) {
1679 const VkWriteDescriptorSet
*write
= &pDescriptorWrites
[i
];
1680 ANV_FROM_HANDLE(anv_descriptor_set
, set
, write
->destSet
);
1682 switch (write
->descriptorType
) {
1683 case VK_DESCRIPTOR_TYPE_SAMPLER
:
1684 for (uint32_t j
= 0; j
< write
->count
; j
++) {
1685 ANV_FROM_HANDLE(anv_sampler
, sampler
,
1686 write
->pDescriptors
[j
].sampler
);
1688 set
->descriptors
[write
->destBinding
+ j
] = (struct anv_descriptor
) {
1689 .type
= ANV_DESCRIPTOR_TYPE_SAMPLER
,
1695 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER
:
1696 for (uint32_t j
= 0; j
< write
->count
; j
++) {
1697 struct anv_descriptor
*desc
=
1698 &set
->descriptors
[write
->destBinding
+ j
];
1699 ANV_FROM_HANDLE(anv_image_view
, iview
,
1700 write
->pDescriptors
[j
].imageView
);
1701 ANV_FROM_HANDLE(anv_sampler
, sampler
,
1702 write
->pDescriptors
[j
].sampler
);
1704 desc
->type
= ANV_DESCRIPTOR_TYPE_IMAGE_VIEW_AND_SAMPLER
;
1705 desc
->image_view
= iview
;
1707 /* If this descriptor has an immutable sampler, we don't want
1711 desc
->sampler
= sampler
;
1715 case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE
:
1716 case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE
:
1717 for (uint32_t j
= 0; j
< write
->count
; j
++) {
1718 ANV_FROM_HANDLE(anv_image_view
, iview
,
1719 write
->pDescriptors
[j
].imageView
);
1721 set
->descriptors
[write
->destBinding
+ j
] = (struct anv_descriptor
) {
1722 .type
= ANV_DESCRIPTOR_TYPE_IMAGE_VIEW
,
1723 .image_view
= iview
,
1728 case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER
:
1729 case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER
:
1730 anv_finishme("texel buffers not implemented");
1733 case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT
:
1734 anv_finishme("input attachments not implemented");
1737 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER
:
1738 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER
:
1739 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC
:
1740 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC
:
1741 for (uint32_t j
= 0; j
< write
->count
; j
++) {
1742 if (write
->pDescriptors
[j
].bufferView
.handle
) {
1743 ANV_FROM_HANDLE(anv_buffer_view
, bview
,
1744 write
->pDescriptors
[j
].bufferView
);
1746 set
->descriptors
[write
->destBinding
+ j
] =
1747 (struct anv_descriptor
) {
1748 .type
= ANV_DESCRIPTOR_TYPE_BUFFER_VIEW
,
1749 .buffer_view
= bview
,
1752 ANV_FROM_HANDLE(anv_buffer
, buffer
,
1753 write
->pDescriptors
[j
].bufferInfo
.buffer
);
1756 set
->descriptors
[write
->destBinding
+ j
] =
1757 (struct anv_descriptor
) {
1758 .type
= ANV_DESCRIPTOR_TYPE_BUFFER_AND_OFFSET
,
1760 .offset
= write
->pDescriptors
[j
].bufferInfo
.offset
,
1761 .range
= write
->pDescriptors
[j
].bufferInfo
.range
,
1771 for (uint32_t i
= 0; i
< copyCount
; i
++) {
1772 const VkCopyDescriptorSet
*copy
= &pDescriptorCopies
[i
];
1773 ANV_FROM_HANDLE(anv_descriptor_set
, src
, copy
->destSet
);
1774 ANV_FROM_HANDLE(anv_descriptor_set
, dest
, copy
->destSet
);
1775 for (uint32_t j
= 0; j
< copy
->count
; j
++) {
1776 dest
->descriptors
[copy
->destBinding
+ j
] =
1777 src
->descriptors
[copy
->srcBinding
+ j
];
1782 VkResult
anv_CreateFramebuffer(
1784 const VkFramebufferCreateInfo
* pCreateInfo
,
1785 VkFramebuffer
* pFramebuffer
)
1787 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1788 struct anv_framebuffer
*framebuffer
;
1790 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO
);
1792 size_t size
= sizeof(*framebuffer
) +
1793 sizeof(struct anv_image_view
*) * pCreateInfo
->attachmentCount
;
1794 framebuffer
= anv_device_alloc(device
, size
, 8,
1795 VK_SYSTEM_ALLOC_TYPE_API_OBJECT
);
1796 if (framebuffer
== NULL
)
1797 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
1799 framebuffer
->attachment_count
= pCreateInfo
->attachmentCount
;
1800 for (uint32_t i
= 0; i
< pCreateInfo
->attachmentCount
; i
++) {
1801 VkImageView _iview
= pCreateInfo
->pAttachments
[i
];
1802 framebuffer
->attachments
[i
] = anv_image_view_from_handle(_iview
);
1805 framebuffer
->width
= pCreateInfo
->width
;
1806 framebuffer
->height
= pCreateInfo
->height
;
1807 framebuffer
->layers
= pCreateInfo
->layers
;
1809 *pFramebuffer
= anv_framebuffer_to_handle(framebuffer
);
1814 void anv_DestroyFramebuffer(
1818 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1819 ANV_FROM_HANDLE(anv_framebuffer
, fb
, _fb
);
1821 anv_device_free(device
, fb
);
1824 VkResult
anv_CreateRenderPass(
1826 const VkRenderPassCreateInfo
* pCreateInfo
,
1827 VkRenderPass
* pRenderPass
)
1829 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1830 struct anv_render_pass
*pass
;
1832 size_t attachments_offset
;
1834 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO
);
1836 size
= sizeof(*pass
);
1837 size
+= pCreateInfo
->subpassCount
* sizeof(pass
->subpasses
[0]);
1838 attachments_offset
= size
;
1839 size
+= pCreateInfo
->attachmentCount
* sizeof(pass
->attachments
[0]);
1841 pass
= anv_device_alloc(device
, size
, 8,
1842 VK_SYSTEM_ALLOC_TYPE_API_OBJECT
);
1844 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
1846 /* Clear the subpasses along with the parent pass. This required because
1847 * each array member of anv_subpass must be a valid pointer if not NULL.
1849 memset(pass
, 0, size
);
1850 pass
->attachment_count
= pCreateInfo
->attachmentCount
;
1851 pass
->subpass_count
= pCreateInfo
->subpassCount
;
1852 pass
->attachments
= (void *) pass
+ attachments_offset
;
1854 for (uint32_t i
= 0; i
< pCreateInfo
->attachmentCount
; i
++) {
1855 struct anv_render_pass_attachment
*att
= &pass
->attachments
[i
];
1857 att
->format
= anv_format_for_vk_format(pCreateInfo
->pAttachments
[i
].format
);
1858 att
->samples
= pCreateInfo
->pAttachments
[i
].samples
;
1859 att
->load_op
= pCreateInfo
->pAttachments
[i
].loadOp
;
1860 att
->stencil_load_op
= pCreateInfo
->pAttachments
[i
].stencilLoadOp
;
1861 // att->store_op = pCreateInfo->pAttachments[i].storeOp;
1862 // att->stencil_store_op = pCreateInfo->pAttachments[i].stencilStoreOp;
1864 if (att
->load_op
== VK_ATTACHMENT_LOAD_OP_CLEAR
) {
1865 if (anv_format_is_color(att
->format
)) {
1866 ++pass
->num_color_clear_attachments
;
1867 } else if (att
->format
->depth_format
) {
1868 pass
->has_depth_clear_attachment
= true;
1870 } else if (att
->stencil_load_op
== VK_ATTACHMENT_LOAD_OP_CLEAR
) {
1871 assert(att
->format
->has_stencil
);
1872 pass
->has_stencil_clear_attachment
= true;
1876 for (uint32_t i
= 0; i
< pCreateInfo
->subpassCount
; i
++) {
1877 const VkSubpassDescription
*desc
= &pCreateInfo
->pSubpasses
[i
];
1878 struct anv_subpass
*subpass
= &pass
->subpasses
[i
];
1880 subpass
->input_count
= desc
->inputCount
;
1881 subpass
->color_count
= desc
->colorCount
;
1883 if (desc
->inputCount
> 0) {
1884 subpass
->input_attachments
=
1885 anv_device_alloc(device
, desc
->inputCount
* sizeof(uint32_t),
1886 8, VK_SYSTEM_ALLOC_TYPE_API_OBJECT
);
1888 for (uint32_t j
= 0; j
< desc
->inputCount
; j
++) {
1889 subpass
->input_attachments
[j
]
1890 = desc
->pInputAttachments
[j
].attachment
;
1894 if (desc
->colorCount
> 0) {
1895 subpass
->color_attachments
=
1896 anv_device_alloc(device
, desc
->colorCount
* sizeof(uint32_t),
1897 8, VK_SYSTEM_ALLOC_TYPE_API_OBJECT
);
1899 for (uint32_t j
= 0; j
< desc
->colorCount
; j
++) {
1900 subpass
->color_attachments
[j
]
1901 = desc
->pColorAttachments
[j
].attachment
;
1905 if (desc
->pResolveAttachments
) {
1906 subpass
->resolve_attachments
=
1907 anv_device_alloc(device
, desc
->colorCount
* sizeof(uint32_t),
1908 8, VK_SYSTEM_ALLOC_TYPE_API_OBJECT
);
1910 for (uint32_t j
= 0; j
< desc
->colorCount
; j
++) {
1911 subpass
->resolve_attachments
[j
]
1912 = desc
->pResolveAttachments
[j
].attachment
;
1916 subpass
->depth_stencil_attachment
= desc
->depthStencilAttachment
.attachment
;
1919 *pRenderPass
= anv_render_pass_to_handle(pass
);
1924 void anv_DestroyRenderPass(
1928 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1929 ANV_FROM_HANDLE(anv_render_pass
, pass
, _pass
);
1931 for (uint32_t i
= 0; i
< pass
->subpass_count
; i
++) {
1932 /* In VkSubpassCreateInfo, each of the attachment arrays may be null.
1933 * Don't free the null arrays.
1935 struct anv_subpass
*subpass
= &pass
->subpasses
[i
];
1937 anv_device_free(device
, subpass
->input_attachments
);
1938 anv_device_free(device
, subpass
->color_attachments
);
1939 anv_device_free(device
, subpass
->resolve_attachments
);
1942 anv_device_free(device
, pass
);
1945 VkResult
anv_GetRenderAreaGranularity(
1947 VkRenderPass renderPass
,
1948 VkExtent2D
* pGranularity
)
1950 *pGranularity
= (VkExtent2D
) { 1, 1 };
1955 void vkCmdDbgMarkerBegin(
1956 VkCmdBuffer cmdBuffer
,
1957 const char* pMarker
)
1958 __attribute__ ((visibility ("default")));
1960 void vkCmdDbgMarkerEnd(
1961 VkCmdBuffer cmdBuffer
)
1962 __attribute__ ((visibility ("default")));
1964 void vkCmdDbgMarkerBegin(
1965 VkCmdBuffer cmdBuffer
,
1966 const char* pMarker
)
1970 void vkCmdDbgMarkerEnd(
1971 VkCmdBuffer cmdBuffer
)