2 * Copyright © 2016 Red Hat.
3 * Copyright © 2016 Bas Nieuwenhuizen
5 * based in part on anv driver which is:
6 * Copyright © 2015 Intel Corporation
8 * Permission is hereby granted, free of charge, to any person obtaining a
9 * copy of this software and associated documentation files (the "Software"),
10 * to deal in the Software without restriction, including without limitation
11 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
12 * and/or sell copies of the Software, and to permit persons to whom the
13 * Software is furnished to do so, subject to the following conditions:
15 * The above copyright notice and this permission notice (including the next
16 * paragraph) shall be included in all copies or substantial portions of the
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
22 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
24 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
25 * DEALINGS IN THE SOFTWARE.
28 #include "tu_private.h"
34 #include <sys/sysinfo.h>
37 #include "compiler/glsl_types.h"
38 #include "util/debug.h"
39 #include "util/disk_cache.h"
40 #include "util/u_atomic.h"
41 #include "vk_format.h"
44 /* for fd_get_driver/device_uuid() */
45 #include "freedreno/common/freedreno_uuid.h"
48 tu_device_get_cache_uuid(uint16_t family
, void *uuid
)
50 uint32_t mesa_timestamp
;
52 memset(uuid
, 0, VK_UUID_SIZE
);
53 if (!disk_cache_get_function_timestamp(tu_device_get_cache_uuid
,
57 memcpy(uuid
, &mesa_timestamp
, 4);
58 memcpy((char *) uuid
+ 4, &f
, 2);
59 snprintf((char *) uuid
+ 6, VK_UUID_SIZE
- 10, "tu");
64 tu_physical_device_init(struct tu_physical_device
*device
,
65 struct tu_instance
*instance
)
67 VkResult result
= VK_SUCCESS
;
69 memset(device
->name
, 0, sizeof(device
->name
));
70 sprintf(device
->name
, "FD%d", device
->gpu_id
);
72 device
->limited_z24s8
= (device
->gpu_id
== 630);
74 switch (device
->gpu_id
) {
76 device
->ccu_offset_gmem
= 0x7c000; /* 0x7e000 in some cases? */
77 device
->ccu_offset_bypass
= 0x10000;
78 device
->tile_align_w
= 32;
79 device
->magic
.PC_UNKNOWN_9805
= 0x0;
80 device
->magic
.SP_UNKNOWN_A0F8
= 0x0;
81 device
->supports_multiview_mask
= false; /* TODO */
85 device
->ccu_offset_gmem
= 0xf8000;
86 device
->ccu_offset_bypass
= 0x20000;
87 device
->tile_align_w
= 32;
88 device
->magic
.PC_UNKNOWN_9805
= 0x1;
89 device
->magic
.SP_UNKNOWN_A0F8
= 0x1;
90 device
->supports_multiview_mask
= device
->gpu_id
!= 630;
93 device
->ccu_offset_gmem
= 0x114000;
94 device
->ccu_offset_bypass
= 0x30000;
95 device
->tile_align_w
= 96;
96 device
->magic
.PC_UNKNOWN_9805
= 0x2;
97 device
->magic
.SP_UNKNOWN_A0F8
= 0x2;
98 device
->supports_multiview_mask
= true;
101 result
= vk_errorf(instance
, VK_ERROR_INITIALIZATION_FAILED
,
102 "device %s is unsupported", device
->name
);
105 if (tu_device_get_cache_uuid(device
->gpu_id
, device
->cache_uuid
)) {
106 result
= vk_errorf(instance
, VK_ERROR_INITIALIZATION_FAILED
,
107 "cannot generate UUID");
111 /* The gpu id is already embedded in the uuid so we just pass "tu"
112 * when creating the cache.
114 char buf
[VK_UUID_SIZE
* 2 + 1];
115 disk_cache_format_hex_id(buf
, device
->cache_uuid
, VK_UUID_SIZE
* 2);
116 device
->disk_cache
= disk_cache_create(device
->name
, buf
, 0);
118 fprintf(stderr
, "WARNING: tu is not a conformant vulkan implementation, "
119 "testing use only.\n");
121 fd_get_driver_uuid(device
->driver_uuid
);
122 fd_get_device_uuid(device
->device_uuid
, device
->gpu_id
);
124 tu_physical_device_get_supported_extensions(device
, &device
->supported_extensions
);
126 if (result
!= VK_SUCCESS
) {
127 vk_error(instance
, result
);
131 result
= tu_wsi_init(device
);
132 if (result
!= VK_SUCCESS
) {
133 vk_error(instance
, result
);
140 close(device
->local_fd
);
141 if (device
->master_fd
!= -1)
142 close(device
->master_fd
);
147 tu_physical_device_finish(struct tu_physical_device
*device
)
149 tu_wsi_finish(device
);
151 disk_cache_destroy(device
->disk_cache
);
152 close(device
->local_fd
);
153 if (device
->master_fd
!= -1)
154 close(device
->master_fd
);
156 vk_object_base_finish(&device
->base
);
159 static VKAPI_ATTR
void *
160 default_alloc_func(void *pUserData
,
163 VkSystemAllocationScope allocationScope
)
168 static VKAPI_ATTR
void *
169 default_realloc_func(void *pUserData
,
173 VkSystemAllocationScope allocationScope
)
175 return realloc(pOriginal
, size
);
178 static VKAPI_ATTR
void
179 default_free_func(void *pUserData
, void *pMemory
)
184 static const VkAllocationCallbacks default_alloc
= {
186 .pfnAllocation
= default_alloc_func
,
187 .pfnReallocation
= default_realloc_func
,
188 .pfnFree
= default_free_func
,
191 static const struct debug_control tu_debug_options
[] = {
192 { "startup", TU_DEBUG_STARTUP
},
193 { "nir", TU_DEBUG_NIR
},
194 { "ir3", TU_DEBUG_IR3
},
195 { "nobin", TU_DEBUG_NOBIN
},
196 { "sysmem", TU_DEBUG_SYSMEM
},
197 { "forcebin", TU_DEBUG_FORCEBIN
},
198 { "noubwc", TU_DEBUG_NOUBWC
},
203 tu_get_debug_option_name(int id
)
205 assert(id
< ARRAY_SIZE(tu_debug_options
) - 1);
206 return tu_debug_options
[id
].string
;
210 tu_get_instance_extension_index(const char *name
)
212 for (unsigned i
= 0; i
< TU_INSTANCE_EXTENSION_COUNT
; ++i
) {
213 if (strcmp(name
, tu_instance_extensions
[i
].extensionName
) == 0)
220 tu_CreateInstance(const VkInstanceCreateInfo
*pCreateInfo
,
221 const VkAllocationCallbacks
*pAllocator
,
222 VkInstance
*pInstance
)
224 struct tu_instance
*instance
;
227 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO
);
229 uint32_t client_version
;
230 if (pCreateInfo
->pApplicationInfo
&&
231 pCreateInfo
->pApplicationInfo
->apiVersion
!= 0) {
232 client_version
= pCreateInfo
->pApplicationInfo
->apiVersion
;
234 tu_EnumerateInstanceVersion(&client_version
);
237 instance
= vk_zalloc2(&default_alloc
, pAllocator
, sizeof(*instance
), 8,
238 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE
);
241 return vk_error(NULL
, VK_ERROR_OUT_OF_HOST_MEMORY
);
243 vk_object_base_init(NULL
, &instance
->base
, VK_OBJECT_TYPE_INSTANCE
);
246 instance
->alloc
= *pAllocator
;
248 instance
->alloc
= default_alloc
;
250 instance
->api_version
= client_version
;
251 instance
->physical_device_count
= -1;
253 instance
->debug_flags
=
254 parse_debug_string(getenv("TU_DEBUG"), tu_debug_options
);
256 if (instance
->debug_flags
& TU_DEBUG_STARTUP
)
257 tu_logi("Created an instance");
259 for (uint32_t i
= 0; i
< pCreateInfo
->enabledExtensionCount
; i
++) {
260 const char *ext_name
= pCreateInfo
->ppEnabledExtensionNames
[i
];
261 int index
= tu_get_instance_extension_index(ext_name
);
263 if (index
< 0 || !tu_instance_extensions_supported
.extensions
[index
]) {
264 vk_object_base_finish(&instance
->base
);
265 vk_free2(&default_alloc
, pAllocator
, instance
);
266 return vk_error(instance
, VK_ERROR_EXTENSION_NOT_PRESENT
);
269 instance
->enabled_extensions
.extensions
[index
] = true;
272 result
= vk_debug_report_instance_init(&instance
->debug_report_callbacks
);
273 if (result
!= VK_SUCCESS
) {
274 vk_object_base_finish(&instance
->base
);
275 vk_free2(&default_alloc
, pAllocator
, instance
);
276 return vk_error(instance
, result
);
279 glsl_type_singleton_init_or_ref();
281 VG(VALGRIND_CREATE_MEMPOOL(instance
, 0, false));
283 *pInstance
= tu_instance_to_handle(instance
);
289 tu_DestroyInstance(VkInstance _instance
,
290 const VkAllocationCallbacks
*pAllocator
)
292 TU_FROM_HANDLE(tu_instance
, instance
, _instance
);
297 for (int i
= 0; i
< instance
->physical_device_count
; ++i
) {
298 tu_physical_device_finish(instance
->physical_devices
+ i
);
301 VG(VALGRIND_DESTROY_MEMPOOL(instance
));
303 glsl_type_singleton_decref();
305 vk_debug_report_instance_destroy(&instance
->debug_report_callbacks
);
307 vk_object_base_finish(&instance
->base
);
308 vk_free(&instance
->alloc
, instance
);
312 tu_EnumeratePhysicalDevices(VkInstance _instance
,
313 uint32_t *pPhysicalDeviceCount
,
314 VkPhysicalDevice
*pPhysicalDevices
)
316 TU_FROM_HANDLE(tu_instance
, instance
, _instance
);
317 VK_OUTARRAY_MAKE(out
, pPhysicalDevices
, pPhysicalDeviceCount
);
321 if (instance
->physical_device_count
< 0) {
322 result
= tu_enumerate_devices(instance
);
323 if (result
!= VK_SUCCESS
&& result
!= VK_ERROR_INCOMPATIBLE_DRIVER
)
327 for (uint32_t i
= 0; i
< instance
->physical_device_count
; ++i
) {
328 vk_outarray_append(&out
, p
)
330 *p
= tu_physical_device_to_handle(instance
->physical_devices
+ i
);
334 return vk_outarray_status(&out
);
338 tu_EnumeratePhysicalDeviceGroups(
339 VkInstance _instance
,
340 uint32_t *pPhysicalDeviceGroupCount
,
341 VkPhysicalDeviceGroupProperties
*pPhysicalDeviceGroupProperties
)
343 TU_FROM_HANDLE(tu_instance
, instance
, _instance
);
344 VK_OUTARRAY_MAKE(out
, pPhysicalDeviceGroupProperties
,
345 pPhysicalDeviceGroupCount
);
348 if (instance
->physical_device_count
< 0) {
349 result
= tu_enumerate_devices(instance
);
350 if (result
!= VK_SUCCESS
&& result
!= VK_ERROR_INCOMPATIBLE_DRIVER
)
354 for (uint32_t i
= 0; i
< instance
->physical_device_count
; ++i
) {
355 vk_outarray_append(&out
, p
)
357 p
->physicalDeviceCount
= 1;
358 p
->physicalDevices
[0] =
359 tu_physical_device_to_handle(instance
->physical_devices
+ i
);
360 p
->subsetAllocation
= false;
364 return vk_outarray_status(&out
);
368 tu_GetPhysicalDeviceFeatures(VkPhysicalDevice physicalDevice
,
369 VkPhysicalDeviceFeatures
*pFeatures
)
371 memset(pFeatures
, 0, sizeof(*pFeatures
));
373 *pFeatures
= (VkPhysicalDeviceFeatures
) {
374 .robustBufferAccess
= true,
375 .fullDrawIndexUint32
= true,
376 .imageCubeArray
= true,
377 .independentBlend
= true,
378 .geometryShader
= true,
379 .tessellationShader
= true,
380 .sampleRateShading
= true,
381 .dualSrcBlend
= true,
383 .multiDrawIndirect
= true,
384 .drawIndirectFirstInstance
= true,
386 .depthBiasClamp
= true,
387 .fillModeNonSolid
= true,
392 .multiViewport
= false,
393 .samplerAnisotropy
= true,
394 .textureCompressionETC2
= true,
395 .textureCompressionASTC_LDR
= true,
396 .textureCompressionBC
= true,
397 .occlusionQueryPrecise
= true,
398 .pipelineStatisticsQuery
= false,
399 .vertexPipelineStoresAndAtomics
= true,
400 .fragmentStoresAndAtomics
= true,
401 .shaderTessellationAndGeometryPointSize
= false,
402 .shaderImageGatherExtended
= false,
403 .shaderStorageImageExtendedFormats
= false,
404 .shaderStorageImageMultisample
= false,
405 .shaderUniformBufferArrayDynamicIndexing
= true,
406 .shaderSampledImageArrayDynamicIndexing
= true,
407 .shaderStorageBufferArrayDynamicIndexing
= true,
408 .shaderStorageImageArrayDynamicIndexing
= true,
409 .shaderStorageImageReadWithoutFormat
= false,
410 .shaderStorageImageWriteWithoutFormat
= false,
411 .shaderClipDistance
= false,
412 .shaderCullDistance
= false,
413 .shaderFloat64
= false,
414 .shaderInt64
= false,
415 .shaderInt16
= false,
416 .sparseBinding
= false,
417 .variableMultisampleRate
= false,
418 .inheritedQueries
= false,
423 tu_GetPhysicalDeviceFeatures2(VkPhysicalDevice physicalDevice
,
424 VkPhysicalDeviceFeatures2
*pFeatures
)
426 vk_foreach_struct(ext
, pFeatures
->pNext
)
428 switch (ext
->sType
) {
429 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VULKAN_1_1_FEATURES
: {
430 VkPhysicalDeviceVulkan11Features
*features
= (void *) ext
;
431 features
->storageBuffer16BitAccess
= false;
432 features
->uniformAndStorageBuffer16BitAccess
= false;
433 features
->storagePushConstant16
= false;
434 features
->storageInputOutput16
= false;
435 features
->multiview
= true;
436 features
->multiviewGeometryShader
= false;
437 features
->multiviewTessellationShader
= false;
438 features
->variablePointersStorageBuffer
= true;
439 features
->variablePointers
= true;
440 features
->protectedMemory
= false;
441 features
->samplerYcbcrConversion
= true;
442 features
->shaderDrawParameters
= true;
445 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VULKAN_1_2_FEATURES
: {
446 VkPhysicalDeviceVulkan12Features
*features
= (void *) ext
;
447 features
->samplerMirrorClampToEdge
= true;
448 features
->drawIndirectCount
= true;
449 features
->storageBuffer8BitAccess
= false;
450 features
->uniformAndStorageBuffer8BitAccess
= false;
451 features
->storagePushConstant8
= false;
452 features
->shaderBufferInt64Atomics
= false;
453 features
->shaderSharedInt64Atomics
= false;
454 features
->shaderFloat16
= false;
455 features
->shaderInt8
= false;
457 features
->descriptorIndexing
= false;
458 features
->shaderInputAttachmentArrayDynamicIndexing
= false;
459 features
->shaderUniformTexelBufferArrayDynamicIndexing
= false;
460 features
->shaderStorageTexelBufferArrayDynamicIndexing
= false;
461 features
->shaderUniformBufferArrayNonUniformIndexing
= false;
462 features
->shaderSampledImageArrayNonUniformIndexing
= false;
463 features
->shaderStorageBufferArrayNonUniformIndexing
= false;
464 features
->shaderStorageImageArrayNonUniformIndexing
= false;
465 features
->shaderInputAttachmentArrayNonUniformIndexing
= false;
466 features
->shaderUniformTexelBufferArrayNonUniformIndexing
= false;
467 features
->shaderStorageTexelBufferArrayNonUniformIndexing
= false;
468 features
->descriptorBindingUniformBufferUpdateAfterBind
= false;
469 features
->descriptorBindingSampledImageUpdateAfterBind
= false;
470 features
->descriptorBindingStorageImageUpdateAfterBind
= false;
471 features
->descriptorBindingStorageBufferUpdateAfterBind
= false;
472 features
->descriptorBindingUniformTexelBufferUpdateAfterBind
= false;
473 features
->descriptorBindingStorageTexelBufferUpdateAfterBind
= false;
474 features
->descriptorBindingUpdateUnusedWhilePending
= false;
475 features
->descriptorBindingPartiallyBound
= false;
476 features
->descriptorBindingVariableDescriptorCount
= false;
477 features
->runtimeDescriptorArray
= false;
479 features
->samplerFilterMinmax
= true;
480 features
->scalarBlockLayout
= false;
481 features
->imagelessFramebuffer
= false;
482 features
->uniformBufferStandardLayout
= false;
483 features
->shaderSubgroupExtendedTypes
= false;
484 features
->separateDepthStencilLayouts
= false;
485 features
->hostQueryReset
= false;
486 features
->timelineSemaphore
= false;
487 features
->bufferDeviceAddress
= false;
488 features
->bufferDeviceAddressCaptureReplay
= false;
489 features
->bufferDeviceAddressMultiDevice
= false;
490 features
->vulkanMemoryModel
= false;
491 features
->vulkanMemoryModelDeviceScope
= false;
492 features
->vulkanMemoryModelAvailabilityVisibilityChains
= false;
493 features
->shaderOutputViewportIndex
= false;
494 features
->shaderOutputLayer
= false;
495 features
->subgroupBroadcastDynamicId
= false;
498 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VARIABLE_POINTERS_FEATURES
: {
499 VkPhysicalDeviceVariablePointersFeatures
*features
= (void *) ext
;
500 features
->variablePointersStorageBuffer
= true;
501 features
->variablePointers
= true;
504 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_MULTIVIEW_FEATURES
: {
505 VkPhysicalDeviceMultiviewFeatures
*features
=
506 (VkPhysicalDeviceMultiviewFeatures
*) ext
;
507 features
->multiview
= true;
508 features
->multiviewGeometryShader
= false;
509 features
->multiviewTessellationShader
= false;
512 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SHADER_DRAW_PARAMETERS_FEATURES
: {
513 VkPhysicalDeviceShaderDrawParametersFeatures
*features
=
514 (VkPhysicalDeviceShaderDrawParametersFeatures
*) ext
;
515 features
->shaderDrawParameters
= true;
518 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PROTECTED_MEMORY_FEATURES
: {
519 VkPhysicalDeviceProtectedMemoryFeatures
*features
=
520 (VkPhysicalDeviceProtectedMemoryFeatures
*) ext
;
521 features
->protectedMemory
= false;
524 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_16BIT_STORAGE_FEATURES
: {
525 VkPhysicalDevice16BitStorageFeatures
*features
=
526 (VkPhysicalDevice16BitStorageFeatures
*) ext
;
527 features
->storageBuffer16BitAccess
= false;
528 features
->uniformAndStorageBuffer16BitAccess
= false;
529 features
->storagePushConstant16
= false;
530 features
->storageInputOutput16
= false;
533 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SAMPLER_YCBCR_CONVERSION_FEATURES
: {
534 VkPhysicalDeviceSamplerYcbcrConversionFeatures
*features
=
535 (VkPhysicalDeviceSamplerYcbcrConversionFeatures
*) ext
;
536 features
->samplerYcbcrConversion
= true;
539 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_DESCRIPTOR_INDEXING_FEATURES_EXT
: {
540 VkPhysicalDeviceDescriptorIndexingFeaturesEXT
*features
=
541 (VkPhysicalDeviceDescriptorIndexingFeaturesEXT
*) ext
;
542 features
->shaderInputAttachmentArrayDynamicIndexing
= false;
543 features
->shaderUniformTexelBufferArrayDynamicIndexing
= false;
544 features
->shaderStorageTexelBufferArrayDynamicIndexing
= false;
545 features
->shaderUniformBufferArrayNonUniformIndexing
= false;
546 features
->shaderSampledImageArrayNonUniformIndexing
= false;
547 features
->shaderStorageBufferArrayNonUniformIndexing
= false;
548 features
->shaderStorageImageArrayNonUniformIndexing
= false;
549 features
->shaderInputAttachmentArrayNonUniformIndexing
= false;
550 features
->shaderUniformTexelBufferArrayNonUniformIndexing
= false;
551 features
->shaderStorageTexelBufferArrayNonUniformIndexing
= false;
552 features
->descriptorBindingUniformBufferUpdateAfterBind
= false;
553 features
->descriptorBindingSampledImageUpdateAfterBind
= false;
554 features
->descriptorBindingStorageImageUpdateAfterBind
= false;
555 features
->descriptorBindingStorageBufferUpdateAfterBind
= false;
556 features
->descriptorBindingUniformTexelBufferUpdateAfterBind
= false;
557 features
->descriptorBindingStorageTexelBufferUpdateAfterBind
= false;
558 features
->descriptorBindingUpdateUnusedWhilePending
= false;
559 features
->descriptorBindingPartiallyBound
= false;
560 features
->descriptorBindingVariableDescriptorCount
= false;
561 features
->runtimeDescriptorArray
= false;
564 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_CONDITIONAL_RENDERING_FEATURES_EXT
: {
565 VkPhysicalDeviceConditionalRenderingFeaturesEXT
*features
=
566 (VkPhysicalDeviceConditionalRenderingFeaturesEXT
*) ext
;
567 features
->conditionalRendering
= true;
568 features
->inheritedConditionalRendering
= true;
571 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_TRANSFORM_FEEDBACK_FEATURES_EXT
: {
572 VkPhysicalDeviceTransformFeedbackFeaturesEXT
*features
=
573 (VkPhysicalDeviceTransformFeedbackFeaturesEXT
*) ext
;
574 features
->transformFeedback
= true;
575 features
->geometryStreams
= false;
578 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_INDEX_TYPE_UINT8_FEATURES_EXT
: {
579 VkPhysicalDeviceIndexTypeUint8FeaturesEXT
*features
=
580 (VkPhysicalDeviceIndexTypeUint8FeaturesEXT
*)ext
;
581 features
->indexTypeUint8
= true;
584 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VERTEX_ATTRIBUTE_DIVISOR_FEATURES_EXT
: {
585 VkPhysicalDeviceVertexAttributeDivisorFeaturesEXT
*features
=
586 (VkPhysicalDeviceVertexAttributeDivisorFeaturesEXT
*)ext
;
587 features
->vertexAttributeInstanceRateDivisor
= true;
588 features
->vertexAttributeInstanceRateZeroDivisor
= true;
591 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PRIVATE_DATA_FEATURES_EXT
: {
592 VkPhysicalDevicePrivateDataFeaturesEXT
*features
=
593 (VkPhysicalDevicePrivateDataFeaturesEXT
*)ext
;
594 features
->privateData
= true;
597 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_DEPTH_CLIP_ENABLE_FEATURES_EXT
: {
598 VkPhysicalDeviceDepthClipEnableFeaturesEXT
*features
=
599 (VkPhysicalDeviceDepthClipEnableFeaturesEXT
*)ext
;
600 features
->depthClipEnable
= true;
603 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_4444_FORMATS_FEATURES_EXT
: {
604 VkPhysicalDevice4444FormatsFeaturesEXT
*features
= (void *)ext
;
605 features
->formatA4R4G4B4
= true;
606 features
->formatA4B4G4R4
= true;
609 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_CUSTOM_BORDER_COLOR_FEATURES_EXT
: {
610 VkPhysicalDeviceCustomBorderColorFeaturesEXT
*features
= (void *) ext
;
611 features
->customBorderColors
= true;
612 features
->customBorderColorWithoutFormat
= true;
619 return tu_GetPhysicalDeviceFeatures(physicalDevice
, &pFeatures
->features
);
623 tu_GetPhysicalDeviceProperties(VkPhysicalDevice physicalDevice
,
624 VkPhysicalDeviceProperties
*pProperties
)
626 TU_FROM_HANDLE(tu_physical_device
, pdevice
, physicalDevice
);
627 VkSampleCountFlags sample_counts
=
628 VK_SAMPLE_COUNT_1_BIT
| VK_SAMPLE_COUNT_2_BIT
| VK_SAMPLE_COUNT_4_BIT
;
630 /* I have no idea what the maximum size is, but the hardware supports very
631 * large numbers of descriptors (at least 2^16). This limit is based on
632 * CP_LOAD_STATE6, which has a 28-bit field for the DWORD offset, so that
633 * we don't have to think about what to do if that overflows, but really
634 * nothing is likely to get close to this.
636 const size_t max_descriptor_set_size
= (1 << 28) / A6XX_TEX_CONST_DWORDS
;
638 VkPhysicalDeviceLimits limits
= {
639 .maxImageDimension1D
= (1 << 14),
640 .maxImageDimension2D
= (1 << 14),
641 .maxImageDimension3D
= (1 << 11),
642 .maxImageDimensionCube
= (1 << 14),
643 .maxImageArrayLayers
= (1 << 11),
644 .maxTexelBufferElements
= 128 * 1024 * 1024,
645 .maxUniformBufferRange
= MAX_UNIFORM_BUFFER_RANGE
,
646 .maxStorageBufferRange
= MAX_STORAGE_BUFFER_RANGE
,
647 .maxPushConstantsSize
= MAX_PUSH_CONSTANTS_SIZE
,
648 .maxMemoryAllocationCount
= UINT32_MAX
,
649 .maxSamplerAllocationCount
= 64 * 1024,
650 .bufferImageGranularity
= 64, /* A cache line */
651 .sparseAddressSpaceSize
= 0xffffffffu
, /* buffer max size */
652 .maxBoundDescriptorSets
= MAX_SETS
,
653 .maxPerStageDescriptorSamplers
= max_descriptor_set_size
,
654 .maxPerStageDescriptorUniformBuffers
= max_descriptor_set_size
,
655 .maxPerStageDescriptorStorageBuffers
= max_descriptor_set_size
,
656 .maxPerStageDescriptorSampledImages
= max_descriptor_set_size
,
657 .maxPerStageDescriptorStorageImages
= max_descriptor_set_size
,
658 .maxPerStageDescriptorInputAttachments
= MAX_RTS
,
659 .maxPerStageResources
= max_descriptor_set_size
,
660 .maxDescriptorSetSamplers
= max_descriptor_set_size
,
661 .maxDescriptorSetUniformBuffers
= max_descriptor_set_size
,
662 .maxDescriptorSetUniformBuffersDynamic
= MAX_DYNAMIC_UNIFORM_BUFFERS
,
663 .maxDescriptorSetStorageBuffers
= max_descriptor_set_size
,
664 .maxDescriptorSetStorageBuffersDynamic
= MAX_DYNAMIC_STORAGE_BUFFERS
,
665 .maxDescriptorSetSampledImages
= max_descriptor_set_size
,
666 .maxDescriptorSetStorageImages
= max_descriptor_set_size
,
667 .maxDescriptorSetInputAttachments
= MAX_RTS
,
668 .maxVertexInputAttributes
= 32,
669 .maxVertexInputBindings
= 32,
670 .maxVertexInputAttributeOffset
= 4095,
671 .maxVertexInputBindingStride
= 2048,
672 .maxVertexOutputComponents
= 128,
673 .maxTessellationGenerationLevel
= 64,
674 .maxTessellationPatchSize
= 32,
675 .maxTessellationControlPerVertexInputComponents
= 128,
676 .maxTessellationControlPerVertexOutputComponents
= 128,
677 .maxTessellationControlPerPatchOutputComponents
= 120,
678 .maxTessellationControlTotalOutputComponents
= 4096,
679 .maxTessellationEvaluationInputComponents
= 128,
680 .maxTessellationEvaluationOutputComponents
= 128,
681 .maxGeometryShaderInvocations
= 32,
682 .maxGeometryInputComponents
= 64,
683 .maxGeometryOutputComponents
= 128,
684 .maxGeometryOutputVertices
= 256,
685 .maxGeometryTotalOutputComponents
= 1024,
686 .maxFragmentInputComponents
= 124,
687 .maxFragmentOutputAttachments
= 8,
688 .maxFragmentDualSrcAttachments
= 1,
689 .maxFragmentCombinedOutputResources
= 8,
690 .maxComputeSharedMemorySize
= 32768,
691 .maxComputeWorkGroupCount
= { 65535, 65535, 65535 },
692 .maxComputeWorkGroupInvocations
= 2048,
693 .maxComputeWorkGroupSize
= { 2048, 2048, 2048 },
694 .subPixelPrecisionBits
= 8,
695 .subTexelPrecisionBits
= 8,
696 .mipmapPrecisionBits
= 8,
697 .maxDrawIndexedIndexValue
= UINT32_MAX
,
698 .maxDrawIndirectCount
= UINT32_MAX
,
699 .maxSamplerLodBias
= 4095.0 / 256.0, /* [-16, 15.99609375] */
700 .maxSamplerAnisotropy
= 16,
701 .maxViewports
= MAX_VIEWPORTS
,
702 .maxViewportDimensions
= { (1 << 14), (1 << 14) },
703 .viewportBoundsRange
= { INT16_MIN
, INT16_MAX
},
704 .viewportSubPixelBits
= 8,
705 .minMemoryMapAlignment
= 4096, /* A page */
706 .minTexelBufferOffsetAlignment
= 64,
707 .minUniformBufferOffsetAlignment
= 64,
708 .minStorageBufferOffsetAlignment
= 64,
709 .minTexelOffset
= -16,
710 .maxTexelOffset
= 15,
711 .minTexelGatherOffset
= -32,
712 .maxTexelGatherOffset
= 31,
713 .minInterpolationOffset
= -0.5,
714 .maxInterpolationOffset
= 0.4375,
715 .subPixelInterpolationOffsetBits
= 4,
716 .maxFramebufferWidth
= (1 << 14),
717 .maxFramebufferHeight
= (1 << 14),
718 .maxFramebufferLayers
= (1 << 10),
719 .framebufferColorSampleCounts
= sample_counts
,
720 .framebufferDepthSampleCounts
= sample_counts
,
721 .framebufferStencilSampleCounts
= sample_counts
,
722 .framebufferNoAttachmentsSampleCounts
= sample_counts
,
723 .maxColorAttachments
= MAX_RTS
,
724 .sampledImageColorSampleCounts
= sample_counts
,
725 .sampledImageIntegerSampleCounts
= VK_SAMPLE_COUNT_1_BIT
,
726 .sampledImageDepthSampleCounts
= sample_counts
,
727 .sampledImageStencilSampleCounts
= sample_counts
,
728 .storageImageSampleCounts
= VK_SAMPLE_COUNT_1_BIT
,
729 .maxSampleMaskWords
= 1,
730 .timestampComputeAndGraphics
= true,
731 .timestampPeriod
= 1000000000.0 / 19200000.0, /* CP_ALWAYS_ON_COUNTER is fixed 19.2MHz */
732 .maxClipDistances
= 8,
733 .maxCullDistances
= 8,
734 .maxCombinedClipAndCullDistances
= 8,
735 .discreteQueuePriorities
= 1,
736 .pointSizeRange
= { 1, 4092 },
737 .lineWidthRange
= { 0.0, 7.9921875 },
738 .pointSizeGranularity
= 0.0625,
739 .lineWidthGranularity
= (1.0 / 128.0),
740 .strictLines
= false, /* FINISHME */
741 .standardSampleLocations
= true,
742 .optimalBufferCopyOffsetAlignment
= 128,
743 .optimalBufferCopyRowPitchAlignment
= 128,
744 .nonCoherentAtomSize
= 64,
747 *pProperties
= (VkPhysicalDeviceProperties
) {
748 .apiVersion
= tu_physical_device_api_version(pdevice
),
749 .driverVersion
= vk_get_driver_version(),
750 .vendorID
= 0, /* TODO */
752 .deviceType
= VK_PHYSICAL_DEVICE_TYPE_INTEGRATED_GPU
,
754 .sparseProperties
= { 0 },
757 strcpy(pProperties
->deviceName
, pdevice
->name
);
758 memcpy(pProperties
->pipelineCacheUUID
, pdevice
->cache_uuid
, VK_UUID_SIZE
);
762 tu_GetPhysicalDeviceProperties2(VkPhysicalDevice physicalDevice
,
763 VkPhysicalDeviceProperties2
*pProperties
)
765 TU_FROM_HANDLE(tu_physical_device
, pdevice
, physicalDevice
);
766 tu_GetPhysicalDeviceProperties(physicalDevice
, &pProperties
->properties
);
768 vk_foreach_struct(ext
, pProperties
->pNext
)
770 switch (ext
->sType
) {
771 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PUSH_DESCRIPTOR_PROPERTIES_KHR
: {
772 VkPhysicalDevicePushDescriptorPropertiesKHR
*properties
=
773 (VkPhysicalDevicePushDescriptorPropertiesKHR
*) ext
;
774 properties
->maxPushDescriptors
= MAX_PUSH_DESCRIPTORS
;
777 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_ID_PROPERTIES
: {
778 VkPhysicalDeviceIDProperties
*properties
=
779 (VkPhysicalDeviceIDProperties
*) ext
;
780 memcpy(properties
->driverUUID
, pdevice
->driver_uuid
, VK_UUID_SIZE
);
781 memcpy(properties
->deviceUUID
, pdevice
->device_uuid
, VK_UUID_SIZE
);
782 properties
->deviceLUIDValid
= false;
785 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_MULTIVIEW_PROPERTIES
: {
786 VkPhysicalDeviceMultiviewProperties
*properties
=
787 (VkPhysicalDeviceMultiviewProperties
*) ext
;
788 properties
->maxMultiviewViewCount
= MAX_VIEWS
;
789 properties
->maxMultiviewInstanceIndex
= INT_MAX
;
792 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_POINT_CLIPPING_PROPERTIES
: {
793 VkPhysicalDevicePointClippingProperties
*properties
=
794 (VkPhysicalDevicePointClippingProperties
*) ext
;
795 properties
->pointClippingBehavior
=
796 VK_POINT_CLIPPING_BEHAVIOR_ALL_CLIP_PLANES
;
799 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_MAINTENANCE_3_PROPERTIES
: {
800 VkPhysicalDeviceMaintenance3Properties
*properties
=
801 (VkPhysicalDeviceMaintenance3Properties
*) ext
;
802 /* Make sure everything is addressable by a signed 32-bit int, and
803 * our largest descriptors are 96 bytes. */
804 properties
->maxPerSetDescriptors
= (1ull << 31) / 96;
805 /* Our buffer size fields allow only this much */
806 properties
->maxMemoryAllocationSize
= 0xFFFFFFFFull
;
809 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_TRANSFORM_FEEDBACK_PROPERTIES_EXT
: {
810 VkPhysicalDeviceTransformFeedbackPropertiesEXT
*properties
=
811 (VkPhysicalDeviceTransformFeedbackPropertiesEXT
*)ext
;
813 properties
->maxTransformFeedbackStreams
= IR3_MAX_SO_STREAMS
;
814 properties
->maxTransformFeedbackBuffers
= IR3_MAX_SO_BUFFERS
;
815 properties
->maxTransformFeedbackBufferSize
= UINT32_MAX
;
816 properties
->maxTransformFeedbackStreamDataSize
= 512;
817 properties
->maxTransformFeedbackBufferDataSize
= 512;
818 properties
->maxTransformFeedbackBufferDataStride
= 512;
819 properties
->transformFeedbackQueries
= true;
820 properties
->transformFeedbackStreamsLinesTriangles
= false;
821 properties
->transformFeedbackRasterizationStreamSelect
= false;
822 properties
->transformFeedbackDraw
= true;
825 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SAMPLE_LOCATIONS_PROPERTIES_EXT
: {
826 VkPhysicalDeviceSampleLocationsPropertiesEXT
*properties
=
827 (VkPhysicalDeviceSampleLocationsPropertiesEXT
*)ext
;
828 properties
->sampleLocationSampleCounts
= 0;
829 if (pdevice
->supported_extensions
.EXT_sample_locations
) {
830 properties
->sampleLocationSampleCounts
=
831 VK_SAMPLE_COUNT_1_BIT
| VK_SAMPLE_COUNT_2_BIT
| VK_SAMPLE_COUNT_4_BIT
;
833 properties
->maxSampleLocationGridSize
= (VkExtent2D
) { 1 , 1 };
834 properties
->sampleLocationCoordinateRange
[0] = 0.0f
;
835 properties
->sampleLocationCoordinateRange
[1] = 0.9375f
;
836 properties
->sampleLocationSubPixelBits
= 4;
837 properties
->variableSampleLocations
= true;
840 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SAMPLER_FILTER_MINMAX_PROPERTIES
: {
841 VkPhysicalDeviceSamplerFilterMinmaxProperties
*properties
=
842 (VkPhysicalDeviceSamplerFilterMinmaxProperties
*)ext
;
843 properties
->filterMinmaxImageComponentMapping
= true;
844 properties
->filterMinmaxSingleComponentFormats
= true;
847 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SUBGROUP_PROPERTIES
: {
848 VkPhysicalDeviceSubgroupProperties
*properties
=
849 (VkPhysicalDeviceSubgroupProperties
*)ext
;
850 properties
->subgroupSize
= 64;
851 properties
->supportedStages
= VK_SHADER_STAGE_COMPUTE_BIT
;
852 properties
->supportedOperations
= VK_SUBGROUP_FEATURE_BASIC_BIT
|
853 VK_SUBGROUP_FEATURE_VOTE_BIT
;
854 properties
->quadOperationsInAllStages
= false;
857 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VERTEX_ATTRIBUTE_DIVISOR_PROPERTIES_EXT
: {
858 VkPhysicalDeviceVertexAttributeDivisorPropertiesEXT
*props
=
859 (VkPhysicalDeviceVertexAttributeDivisorPropertiesEXT
*)ext
;
860 props
->maxVertexAttribDivisor
= UINT32_MAX
;
863 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_CUSTOM_BORDER_COLOR_PROPERTIES_EXT
: {
864 VkPhysicalDeviceCustomBorderColorPropertiesEXT
*props
= (void *)ext
;
865 props
->maxCustomBorderColorSamplers
= TU_BORDER_COLOR_COUNT
;
874 static const VkQueueFamilyProperties tu_queue_family_properties
= {
876 VK_QUEUE_GRAPHICS_BIT
| VK_QUEUE_COMPUTE_BIT
| VK_QUEUE_TRANSFER_BIT
,
878 .timestampValidBits
= 48,
879 .minImageTransferGranularity
= { 1, 1, 1 },
883 tu_GetPhysicalDeviceQueueFamilyProperties(
884 VkPhysicalDevice physicalDevice
,
885 uint32_t *pQueueFamilyPropertyCount
,
886 VkQueueFamilyProperties
*pQueueFamilyProperties
)
888 VK_OUTARRAY_MAKE(out
, pQueueFamilyProperties
, pQueueFamilyPropertyCount
);
890 vk_outarray_append(&out
, p
) { *p
= tu_queue_family_properties
; }
894 tu_GetPhysicalDeviceQueueFamilyProperties2(
895 VkPhysicalDevice physicalDevice
,
896 uint32_t *pQueueFamilyPropertyCount
,
897 VkQueueFamilyProperties2
*pQueueFamilyProperties
)
899 VK_OUTARRAY_MAKE(out
, pQueueFamilyProperties
, pQueueFamilyPropertyCount
);
901 vk_outarray_append(&out
, p
)
903 p
->queueFamilyProperties
= tu_queue_family_properties
;
908 tu_get_system_heap_size()
913 uint64_t total_ram
= (uint64_t) info
.totalram
* (uint64_t) info
.mem_unit
;
915 /* We don't want to burn too much ram with the GPU. If the user has 4GiB
916 * or less, we use at most half. If they have more than 4GiB, we use 3/4.
918 uint64_t available_ram
;
919 if (total_ram
<= 4ull * 1024ull * 1024ull * 1024ull)
920 available_ram
= total_ram
/ 2;
922 available_ram
= total_ram
* 3 / 4;
924 return available_ram
;
928 tu_GetPhysicalDeviceMemoryProperties(
929 VkPhysicalDevice physicalDevice
,
930 VkPhysicalDeviceMemoryProperties
*pMemoryProperties
)
932 pMemoryProperties
->memoryHeapCount
= 1;
933 pMemoryProperties
->memoryHeaps
[0].size
= tu_get_system_heap_size();
934 pMemoryProperties
->memoryHeaps
[0].flags
= VK_MEMORY_HEAP_DEVICE_LOCAL_BIT
;
936 pMemoryProperties
->memoryTypeCount
= 1;
937 pMemoryProperties
->memoryTypes
[0].propertyFlags
=
938 VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT
|
939 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT
|
940 VK_MEMORY_PROPERTY_HOST_COHERENT_BIT
;
941 pMemoryProperties
->memoryTypes
[0].heapIndex
= 0;
945 tu_GetPhysicalDeviceMemoryProperties2(
946 VkPhysicalDevice physicalDevice
,
947 VkPhysicalDeviceMemoryProperties2
*pMemoryProperties
)
949 return tu_GetPhysicalDeviceMemoryProperties(
950 physicalDevice
, &pMemoryProperties
->memoryProperties
);
954 tu_queue_init(struct tu_device
*device
,
955 struct tu_queue
*queue
,
956 uint32_t queue_family_index
,
958 VkDeviceQueueCreateFlags flags
)
960 vk_object_base_init(&device
->vk
, &queue
->base
, VK_OBJECT_TYPE_QUEUE
);
962 queue
->device
= device
;
963 queue
->queue_family_index
= queue_family_index
;
964 queue
->queue_idx
= idx
;
965 queue
->flags
= flags
;
967 int ret
= tu_drm_submitqueue_new(device
, 0, &queue
->msm_queue_id
);
969 return VK_ERROR_INITIALIZATION_FAILED
;
971 tu_fence_init(&queue
->submit_fence
, false);
977 tu_queue_finish(struct tu_queue
*queue
)
979 tu_fence_finish(&queue
->submit_fence
);
980 tu_drm_submitqueue_close(queue
->device
, queue
->msm_queue_id
);
984 tu_get_device_extension_index(const char *name
)
986 for (unsigned i
= 0; i
< TU_DEVICE_EXTENSION_COUNT
; ++i
) {
987 if (strcmp(name
, tu_device_extensions
[i
].extensionName
) == 0)
994 tu_CreateDevice(VkPhysicalDevice physicalDevice
,
995 const VkDeviceCreateInfo
*pCreateInfo
,
996 const VkAllocationCallbacks
*pAllocator
,
999 TU_FROM_HANDLE(tu_physical_device
, physical_device
, physicalDevice
);
1001 struct tu_device
*device
;
1002 bool custom_border_colors
= false;
1004 /* Check enabled features */
1005 if (pCreateInfo
->pEnabledFeatures
) {
1006 VkPhysicalDeviceFeatures supported_features
;
1007 tu_GetPhysicalDeviceFeatures(physicalDevice
, &supported_features
);
1008 VkBool32
*supported_feature
= (VkBool32
*) &supported_features
;
1009 VkBool32
*enabled_feature
= (VkBool32
*) pCreateInfo
->pEnabledFeatures
;
1010 unsigned num_features
=
1011 sizeof(VkPhysicalDeviceFeatures
) / sizeof(VkBool32
);
1012 for (uint32_t i
= 0; i
< num_features
; i
++) {
1013 if (enabled_feature
[i
] && !supported_feature
[i
])
1014 return vk_error(physical_device
->instance
,
1015 VK_ERROR_FEATURE_NOT_PRESENT
);
1019 vk_foreach_struct_const(ext
, pCreateInfo
->pNext
) {
1020 switch (ext
->sType
) {
1021 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_CUSTOM_BORDER_COLOR_FEATURES_EXT
: {
1022 const VkPhysicalDeviceCustomBorderColorFeaturesEXT
*border_color_features
= (const void *)ext
;
1023 custom_border_colors
= border_color_features
->customBorderColors
;
1031 device
= vk_zalloc2(&physical_device
->instance
->alloc
, pAllocator
,
1032 sizeof(*device
), 8, VK_SYSTEM_ALLOCATION_SCOPE_DEVICE
);
1034 return vk_error(physical_device
->instance
, VK_ERROR_OUT_OF_HOST_MEMORY
);
1036 vk_device_init(&device
->vk
, pCreateInfo
,
1037 &physical_device
->instance
->alloc
, pAllocator
);
1039 device
->instance
= physical_device
->instance
;
1040 device
->physical_device
= physical_device
;
1041 device
->_lost
= false;
1043 for (uint32_t i
= 0; i
< pCreateInfo
->enabledExtensionCount
; i
++) {
1044 const char *ext_name
= pCreateInfo
->ppEnabledExtensionNames
[i
];
1045 int index
= tu_get_device_extension_index(ext_name
);
1047 !physical_device
->supported_extensions
.extensions
[index
]) {
1048 vk_free(&device
->vk
.alloc
, device
);
1049 return vk_error(physical_device
->instance
,
1050 VK_ERROR_EXTENSION_NOT_PRESENT
);
1053 device
->enabled_extensions
.extensions
[index
] = true;
1056 for (unsigned i
= 0; i
< pCreateInfo
->queueCreateInfoCount
; i
++) {
1057 const VkDeviceQueueCreateInfo
*queue_create
=
1058 &pCreateInfo
->pQueueCreateInfos
[i
];
1059 uint32_t qfi
= queue_create
->queueFamilyIndex
;
1060 device
->queues
[qfi
] = vk_alloc(
1061 &device
->vk
.alloc
, queue_create
->queueCount
* sizeof(struct tu_queue
),
1062 8, VK_SYSTEM_ALLOCATION_SCOPE_DEVICE
);
1063 if (!device
->queues
[qfi
]) {
1064 result
= VK_ERROR_OUT_OF_HOST_MEMORY
;
1068 memset(device
->queues
[qfi
], 0,
1069 queue_create
->queueCount
* sizeof(struct tu_queue
));
1071 device
->queue_count
[qfi
] = queue_create
->queueCount
;
1073 for (unsigned q
= 0; q
< queue_create
->queueCount
; q
++) {
1074 result
= tu_queue_init(device
, &device
->queues
[qfi
][q
], qfi
, q
,
1075 queue_create
->flags
);
1076 if (result
!= VK_SUCCESS
)
1081 device
->compiler
= ir3_compiler_create(NULL
, physical_device
->gpu_id
);
1082 if (!device
->compiler
)
1085 /* initial sizes, these will increase if there is overflow */
1086 device
->vsc_draw_strm_pitch
= 0x1000 + VSC_PAD
;
1087 device
->vsc_prim_strm_pitch
= 0x4000 + VSC_PAD
;
1089 uint32_t global_size
= sizeof(struct tu6_global
);
1090 if (custom_border_colors
)
1091 global_size
+= TU_BORDER_COLOR_COUNT
* sizeof(struct bcolor_entry
);
1093 result
= tu_bo_init_new(device
, &device
->global_bo
, global_size
);
1094 if (result
!= VK_SUCCESS
)
1095 goto fail_global_bo
;
1097 result
= tu_bo_map(device
, &device
->global_bo
);
1098 if (result
!= VK_SUCCESS
)
1099 goto fail_global_bo_map
;
1101 struct tu6_global
*global
= device
->global_bo
.map
;
1102 tu_init_clear_blit_shaders(device
->global_bo
.map
);
1103 global
->predicate
= 0;
1104 tu6_pack_border_color(&global
->bcolor_builtin
[VK_BORDER_COLOR_FLOAT_TRANSPARENT_BLACK
],
1105 &(VkClearColorValue
) {}, false);
1106 tu6_pack_border_color(&global
->bcolor_builtin
[VK_BORDER_COLOR_INT_TRANSPARENT_BLACK
],
1107 &(VkClearColorValue
) {}, true);
1108 tu6_pack_border_color(&global
->bcolor_builtin
[VK_BORDER_COLOR_FLOAT_OPAQUE_BLACK
],
1109 &(VkClearColorValue
) { .float32
[3] = 1.0f
}, false);
1110 tu6_pack_border_color(&global
->bcolor_builtin
[VK_BORDER_COLOR_INT_OPAQUE_BLACK
],
1111 &(VkClearColorValue
) { .int32
[3] = 1 }, true);
1112 tu6_pack_border_color(&global
->bcolor_builtin
[VK_BORDER_COLOR_FLOAT_OPAQUE_WHITE
],
1113 &(VkClearColorValue
) { .float32
[0 ... 3] = 1.0f
}, false);
1114 tu6_pack_border_color(&global
->bcolor_builtin
[VK_BORDER_COLOR_INT_OPAQUE_WHITE
],
1115 &(VkClearColorValue
) { .int32
[0 ... 3] = 1 }, true);
1117 /* initialize to ones so ffs can be used to find unused slots */
1118 BITSET_ONES(device
->custom_border_color
);
1120 VkPipelineCacheCreateInfo ci
;
1121 ci
.sType
= VK_STRUCTURE_TYPE_PIPELINE_CACHE_CREATE_INFO
;
1124 ci
.pInitialData
= NULL
;
1125 ci
.initialDataSize
= 0;
1128 tu_CreatePipelineCache(tu_device_to_handle(device
), &ci
, NULL
, &pc
);
1129 if (result
!= VK_SUCCESS
)
1130 goto fail_pipeline_cache
;
1132 device
->mem_cache
= tu_pipeline_cache_from_handle(pc
);
1134 for (unsigned i
= 0; i
< ARRAY_SIZE(device
->scratch_bos
); i
++)
1135 mtx_init(&device
->scratch_bos
[i
].construct_mtx
, mtx_plain
);
1137 mtx_init(&device
->mutex
, mtx_plain
);
1139 *pDevice
= tu_device_to_handle(device
);
1142 fail_pipeline_cache
:
1144 tu_bo_finish(device
, &device
->global_bo
);
1147 ralloc_free(device
->compiler
);
1150 for (unsigned i
= 0; i
< TU_MAX_QUEUE_FAMILIES
; i
++) {
1151 for (unsigned q
= 0; q
< device
->queue_count
[i
]; q
++)
1152 tu_queue_finish(&device
->queues
[i
][q
]);
1153 if (device
->queue_count
[i
])
1154 vk_object_free(&device
->vk
, NULL
, device
->queues
[i
]);
1157 vk_free(&device
->vk
.alloc
, device
);
1162 tu_DestroyDevice(VkDevice _device
, const VkAllocationCallbacks
*pAllocator
)
1164 TU_FROM_HANDLE(tu_device
, device
, _device
);
1169 for (unsigned i
= 0; i
< TU_MAX_QUEUE_FAMILIES
; i
++) {
1170 for (unsigned q
= 0; q
< device
->queue_count
[i
]; q
++)
1171 tu_queue_finish(&device
->queues
[i
][q
]);
1172 if (device
->queue_count
[i
])
1173 vk_object_free(&device
->vk
, NULL
, device
->queues
[i
]);
1176 for (unsigned i
= 0; i
< ARRAY_SIZE(device
->scratch_bos
); i
++) {
1177 if (device
->scratch_bos
[i
].initialized
)
1178 tu_bo_finish(device
, &device
->scratch_bos
[i
].bo
);
1181 ir3_compiler_destroy(device
->compiler
);
1183 VkPipelineCache pc
= tu_pipeline_cache_to_handle(device
->mem_cache
);
1184 tu_DestroyPipelineCache(tu_device_to_handle(device
), pc
, NULL
);
1186 vk_free(&device
->vk
.alloc
, device
);
1190 _tu_device_set_lost(struct tu_device
*device
,
1191 const char *file
, int line
,
1192 const char *msg
, ...)
1194 /* Set the flag indicating that waits should return in finite time even
1195 * after device loss.
1197 p_atomic_inc(&device
->_lost
);
1199 /* TODO: Report the log message through VkDebugReportCallbackEXT instead */
1200 fprintf(stderr
, "%s:%d: ", file
, line
);
1203 vfprintf(stderr
, msg
, ap
);
1206 if (env_var_as_boolean("TU_ABORT_ON_DEVICE_LOSS", false))
1209 return VK_ERROR_DEVICE_LOST
;
1213 tu_get_scratch_bo(struct tu_device
*dev
, uint64_t size
, struct tu_bo
**bo
)
1215 unsigned size_log2
= MAX2(util_logbase2_ceil64(size
), MIN_SCRATCH_BO_SIZE_LOG2
);
1216 unsigned index
= size_log2
- MIN_SCRATCH_BO_SIZE_LOG2
;
1217 assert(index
< ARRAY_SIZE(dev
->scratch_bos
));
1219 for (unsigned i
= index
; i
< ARRAY_SIZE(dev
->scratch_bos
); i
++) {
1220 if (p_atomic_read(&dev
->scratch_bos
[i
].initialized
)) {
1221 /* Fast path: just return the already-allocated BO. */
1222 *bo
= &dev
->scratch_bos
[i
].bo
;
1227 /* Slow path: actually allocate the BO. We take a lock because the process
1228 * of allocating it is slow, and we don't want to block the CPU while it
1231 mtx_lock(&dev
->scratch_bos
[index
].construct_mtx
);
1233 /* Another thread may have allocated it already while we were waiting on
1234 * the lock. We need to check this in order to avoid double-allocating.
1236 if (dev
->scratch_bos
[index
].initialized
) {
1237 mtx_unlock(&dev
->scratch_bos
[index
].construct_mtx
);
1238 *bo
= &dev
->scratch_bos
[index
].bo
;
1242 unsigned bo_size
= 1ull << size_log2
;
1243 VkResult result
= tu_bo_init_new(dev
, &dev
->scratch_bos
[index
].bo
, bo_size
);
1244 if (result
!= VK_SUCCESS
) {
1245 mtx_unlock(&dev
->scratch_bos
[index
].construct_mtx
);
1249 p_atomic_set(&dev
->scratch_bos
[index
].initialized
, true);
1251 mtx_unlock(&dev
->scratch_bos
[index
].construct_mtx
);
1253 *bo
= &dev
->scratch_bos
[index
].bo
;
1258 tu_EnumerateInstanceLayerProperties(uint32_t *pPropertyCount
,
1259 VkLayerProperties
*pProperties
)
1261 *pPropertyCount
= 0;
1266 tu_EnumerateDeviceLayerProperties(VkPhysicalDevice physicalDevice
,
1267 uint32_t *pPropertyCount
,
1268 VkLayerProperties
*pProperties
)
1270 *pPropertyCount
= 0;
1275 tu_GetDeviceQueue2(VkDevice _device
,
1276 const VkDeviceQueueInfo2
*pQueueInfo
,
1279 TU_FROM_HANDLE(tu_device
, device
, _device
);
1280 struct tu_queue
*queue
;
1283 &device
->queues
[pQueueInfo
->queueFamilyIndex
][pQueueInfo
->queueIndex
];
1284 if (pQueueInfo
->flags
!= queue
->flags
) {
1285 /* From the Vulkan 1.1.70 spec:
1287 * "The queue returned by vkGetDeviceQueue2 must have the same
1288 * flags value from this structure as that used at device
1289 * creation time in a VkDeviceQueueCreateInfo instance. If no
1290 * matching flags were specified at device creation time then
1291 * pQueue will return VK_NULL_HANDLE."
1293 *pQueue
= VK_NULL_HANDLE
;
1297 *pQueue
= tu_queue_to_handle(queue
);
1301 tu_GetDeviceQueue(VkDevice _device
,
1302 uint32_t queueFamilyIndex
,
1303 uint32_t queueIndex
,
1306 const VkDeviceQueueInfo2 info
=
1307 (VkDeviceQueueInfo2
) { .sType
= VK_STRUCTURE_TYPE_DEVICE_QUEUE_INFO_2
,
1308 .queueFamilyIndex
= queueFamilyIndex
,
1309 .queueIndex
= queueIndex
};
1311 tu_GetDeviceQueue2(_device
, &info
, pQueue
);
1315 tu_QueueWaitIdle(VkQueue _queue
)
1317 TU_FROM_HANDLE(tu_queue
, queue
, _queue
);
1319 if (tu_device_is_lost(queue
->device
))
1320 return VK_ERROR_DEVICE_LOST
;
1322 tu_fence_wait_idle(&queue
->submit_fence
);
1328 tu_DeviceWaitIdle(VkDevice _device
)
1330 TU_FROM_HANDLE(tu_device
, device
, _device
);
1332 if (tu_device_is_lost(device
))
1333 return VK_ERROR_DEVICE_LOST
;
1335 for (unsigned i
= 0; i
< TU_MAX_QUEUE_FAMILIES
; i
++) {
1336 for (unsigned q
= 0; q
< device
->queue_count
[i
]; q
++) {
1337 tu_QueueWaitIdle(tu_queue_to_handle(&device
->queues
[i
][q
]));
1344 tu_EnumerateInstanceExtensionProperties(const char *pLayerName
,
1345 uint32_t *pPropertyCount
,
1346 VkExtensionProperties
*pProperties
)
1348 VK_OUTARRAY_MAKE(out
, pProperties
, pPropertyCount
);
1350 /* We spport no lyaers */
1352 return vk_error(NULL
, VK_ERROR_LAYER_NOT_PRESENT
);
1354 for (int i
= 0; i
< TU_INSTANCE_EXTENSION_COUNT
; i
++) {
1355 if (tu_instance_extensions_supported
.extensions
[i
]) {
1356 vk_outarray_append(&out
, prop
) { *prop
= tu_instance_extensions
[i
]; }
1360 return vk_outarray_status(&out
);
1364 tu_EnumerateDeviceExtensionProperties(VkPhysicalDevice physicalDevice
,
1365 const char *pLayerName
,
1366 uint32_t *pPropertyCount
,
1367 VkExtensionProperties
*pProperties
)
1369 /* We spport no lyaers */
1370 TU_FROM_HANDLE(tu_physical_device
, device
, physicalDevice
);
1371 VK_OUTARRAY_MAKE(out
, pProperties
, pPropertyCount
);
1373 /* We spport no lyaers */
1375 return vk_error(NULL
, VK_ERROR_LAYER_NOT_PRESENT
);
1377 for (int i
= 0; i
< TU_DEVICE_EXTENSION_COUNT
; i
++) {
1378 if (device
->supported_extensions
.extensions
[i
]) {
1379 vk_outarray_append(&out
, prop
) { *prop
= tu_device_extensions
[i
]; }
1383 return vk_outarray_status(&out
);
1387 tu_GetInstanceProcAddr(VkInstance _instance
, const char *pName
)
1389 TU_FROM_HANDLE(tu_instance
, instance
, _instance
);
1391 return tu_lookup_entrypoint_checked(
1392 pName
, instance
? instance
->api_version
: 0,
1393 instance
? &instance
->enabled_extensions
: NULL
, NULL
);
1396 /* The loader wants us to expose a second GetInstanceProcAddr function
1397 * to work around certain LD_PRELOAD issues seen in apps.
1400 VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL
1401 vk_icdGetInstanceProcAddr(VkInstance instance
, const char *pName
);
1404 VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL
1405 vk_icdGetInstanceProcAddr(VkInstance instance
, const char *pName
)
1407 return tu_GetInstanceProcAddr(instance
, pName
);
1411 tu_GetDeviceProcAddr(VkDevice _device
, const char *pName
)
1413 TU_FROM_HANDLE(tu_device
, device
, _device
);
1415 return tu_lookup_entrypoint_checked(pName
, device
->instance
->api_version
,
1416 &device
->instance
->enabled_extensions
,
1417 &device
->enabled_extensions
);
1421 tu_alloc_memory(struct tu_device
*device
,
1422 const VkMemoryAllocateInfo
*pAllocateInfo
,
1423 const VkAllocationCallbacks
*pAllocator
,
1424 VkDeviceMemory
*pMem
)
1426 struct tu_device_memory
*mem
;
1429 assert(pAllocateInfo
->sType
== VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO
);
1431 if (pAllocateInfo
->allocationSize
== 0) {
1432 /* Apparently, this is allowed */
1433 *pMem
= VK_NULL_HANDLE
;
1437 mem
= vk_object_alloc(&device
->vk
, pAllocator
, sizeof(*mem
),
1438 VK_OBJECT_TYPE_DEVICE_MEMORY
);
1440 return vk_error(device
->instance
, VK_ERROR_OUT_OF_HOST_MEMORY
);
1442 const VkImportMemoryFdInfoKHR
*fd_info
=
1443 vk_find_struct_const(pAllocateInfo
->pNext
, IMPORT_MEMORY_FD_INFO_KHR
);
1444 if (fd_info
&& !fd_info
->handleType
)
1448 assert(fd_info
->handleType
==
1449 VK_EXTERNAL_MEMORY_HANDLE_TYPE_OPAQUE_FD_BIT
||
1450 fd_info
->handleType
==
1451 VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_EXT
);
1454 * TODO Importing the same fd twice gives us the same handle without
1455 * reference counting. We need to maintain a per-instance handle-to-bo
1456 * table and add reference count to tu_bo.
1458 result
= tu_bo_init_dmabuf(device
, &mem
->bo
,
1459 pAllocateInfo
->allocationSize
, fd_info
->fd
);
1460 if (result
== VK_SUCCESS
) {
1461 /* take ownership and close the fd */
1466 tu_bo_init_new(device
, &mem
->bo
, pAllocateInfo
->allocationSize
);
1469 if (result
!= VK_SUCCESS
) {
1470 vk_object_free(&device
->vk
, pAllocator
, mem
);
1474 mem
->size
= pAllocateInfo
->allocationSize
;
1475 mem
->type_index
= pAllocateInfo
->memoryTypeIndex
;
1478 mem
->user_ptr
= NULL
;
1480 *pMem
= tu_device_memory_to_handle(mem
);
1486 tu_AllocateMemory(VkDevice _device
,
1487 const VkMemoryAllocateInfo
*pAllocateInfo
,
1488 const VkAllocationCallbacks
*pAllocator
,
1489 VkDeviceMemory
*pMem
)
1491 TU_FROM_HANDLE(tu_device
, device
, _device
);
1492 return tu_alloc_memory(device
, pAllocateInfo
, pAllocator
, pMem
);
1496 tu_FreeMemory(VkDevice _device
,
1497 VkDeviceMemory _mem
,
1498 const VkAllocationCallbacks
*pAllocator
)
1500 TU_FROM_HANDLE(tu_device
, device
, _device
);
1501 TU_FROM_HANDLE(tu_device_memory
, mem
, _mem
);
1506 tu_bo_finish(device
, &mem
->bo
);
1507 vk_object_free(&device
->vk
, pAllocator
, mem
);
1511 tu_MapMemory(VkDevice _device
,
1512 VkDeviceMemory _memory
,
1513 VkDeviceSize offset
,
1515 VkMemoryMapFlags flags
,
1518 TU_FROM_HANDLE(tu_device
, device
, _device
);
1519 TU_FROM_HANDLE(tu_device_memory
, mem
, _memory
);
1527 if (mem
->user_ptr
) {
1528 *ppData
= mem
->user_ptr
;
1529 } else if (!mem
->map
) {
1530 result
= tu_bo_map(device
, &mem
->bo
);
1531 if (result
!= VK_SUCCESS
)
1533 *ppData
= mem
->map
= mem
->bo
.map
;
1542 return vk_error(device
->instance
, VK_ERROR_MEMORY_MAP_FAILED
);
1546 tu_UnmapMemory(VkDevice _device
, VkDeviceMemory _memory
)
1548 /* I do not see any unmapping done by the freedreno Gallium driver. */
1552 tu_FlushMappedMemoryRanges(VkDevice _device
,
1553 uint32_t memoryRangeCount
,
1554 const VkMappedMemoryRange
*pMemoryRanges
)
1560 tu_InvalidateMappedMemoryRanges(VkDevice _device
,
1561 uint32_t memoryRangeCount
,
1562 const VkMappedMemoryRange
*pMemoryRanges
)
1568 tu_GetBufferMemoryRequirements(VkDevice _device
,
1570 VkMemoryRequirements
*pMemoryRequirements
)
1572 TU_FROM_HANDLE(tu_buffer
, buffer
, _buffer
);
1574 pMemoryRequirements
->memoryTypeBits
= 1;
1575 pMemoryRequirements
->alignment
= 64;
1576 pMemoryRequirements
->size
=
1577 align64(buffer
->size
, pMemoryRequirements
->alignment
);
1581 tu_GetBufferMemoryRequirements2(
1583 const VkBufferMemoryRequirementsInfo2
*pInfo
,
1584 VkMemoryRequirements2
*pMemoryRequirements
)
1586 tu_GetBufferMemoryRequirements(device
, pInfo
->buffer
,
1587 &pMemoryRequirements
->memoryRequirements
);
1591 tu_GetImageMemoryRequirements(VkDevice _device
,
1593 VkMemoryRequirements
*pMemoryRequirements
)
1595 TU_FROM_HANDLE(tu_image
, image
, _image
);
1597 pMemoryRequirements
->memoryTypeBits
= 1;
1598 pMemoryRequirements
->size
= image
->total_size
;
1599 pMemoryRequirements
->alignment
= image
->layout
[0].base_align
;
1603 tu_GetImageMemoryRequirements2(VkDevice device
,
1604 const VkImageMemoryRequirementsInfo2
*pInfo
,
1605 VkMemoryRequirements2
*pMemoryRequirements
)
1607 tu_GetImageMemoryRequirements(device
, pInfo
->image
,
1608 &pMemoryRequirements
->memoryRequirements
);
1612 tu_GetImageSparseMemoryRequirements(
1615 uint32_t *pSparseMemoryRequirementCount
,
1616 VkSparseImageMemoryRequirements
*pSparseMemoryRequirements
)
1622 tu_GetImageSparseMemoryRequirements2(
1624 const VkImageSparseMemoryRequirementsInfo2
*pInfo
,
1625 uint32_t *pSparseMemoryRequirementCount
,
1626 VkSparseImageMemoryRequirements2
*pSparseMemoryRequirements
)
1632 tu_GetDeviceMemoryCommitment(VkDevice device
,
1633 VkDeviceMemory memory
,
1634 VkDeviceSize
*pCommittedMemoryInBytes
)
1636 *pCommittedMemoryInBytes
= 0;
1640 tu_BindBufferMemory2(VkDevice device
,
1641 uint32_t bindInfoCount
,
1642 const VkBindBufferMemoryInfo
*pBindInfos
)
1644 for (uint32_t i
= 0; i
< bindInfoCount
; ++i
) {
1645 TU_FROM_HANDLE(tu_device_memory
, mem
, pBindInfos
[i
].memory
);
1646 TU_FROM_HANDLE(tu_buffer
, buffer
, pBindInfos
[i
].buffer
);
1649 buffer
->bo
= &mem
->bo
;
1650 buffer
->bo_offset
= pBindInfos
[i
].memoryOffset
;
1659 tu_BindBufferMemory(VkDevice device
,
1661 VkDeviceMemory memory
,
1662 VkDeviceSize memoryOffset
)
1664 const VkBindBufferMemoryInfo info
= {
1665 .sType
= VK_STRUCTURE_TYPE_BIND_BUFFER_MEMORY_INFO
,
1668 .memoryOffset
= memoryOffset
1671 return tu_BindBufferMemory2(device
, 1, &info
);
1675 tu_BindImageMemory2(VkDevice device
,
1676 uint32_t bindInfoCount
,
1677 const VkBindImageMemoryInfo
*pBindInfos
)
1679 for (uint32_t i
= 0; i
< bindInfoCount
; ++i
) {
1680 TU_FROM_HANDLE(tu_image
, image
, pBindInfos
[i
].image
);
1681 TU_FROM_HANDLE(tu_device_memory
, mem
, pBindInfos
[i
].memory
);
1684 image
->bo
= &mem
->bo
;
1685 image
->bo_offset
= pBindInfos
[i
].memoryOffset
;
1688 image
->bo_offset
= 0;
1696 tu_BindImageMemory(VkDevice device
,
1698 VkDeviceMemory memory
,
1699 VkDeviceSize memoryOffset
)
1701 const VkBindImageMemoryInfo info
= {
1702 .sType
= VK_STRUCTURE_TYPE_BIND_BUFFER_MEMORY_INFO
,
1705 .memoryOffset
= memoryOffset
1708 return tu_BindImageMemory2(device
, 1, &info
);
1712 tu_QueueBindSparse(VkQueue _queue
,
1713 uint32_t bindInfoCount
,
1714 const VkBindSparseInfo
*pBindInfo
,
1722 tu_CreateEvent(VkDevice _device
,
1723 const VkEventCreateInfo
*pCreateInfo
,
1724 const VkAllocationCallbacks
*pAllocator
,
1727 TU_FROM_HANDLE(tu_device
, device
, _device
);
1729 struct tu_event
*event
=
1730 vk_object_alloc(&device
->vk
, pAllocator
, sizeof(*event
),
1731 VK_OBJECT_TYPE_EVENT
);
1733 return vk_error(device
->instance
, VK_ERROR_OUT_OF_HOST_MEMORY
);
1735 VkResult result
= tu_bo_init_new(device
, &event
->bo
, 0x1000);
1736 if (result
!= VK_SUCCESS
)
1739 result
= tu_bo_map(device
, &event
->bo
);
1740 if (result
!= VK_SUCCESS
)
1743 *pEvent
= tu_event_to_handle(event
);
1748 tu_bo_finish(device
, &event
->bo
);
1750 vk_object_free(&device
->vk
, pAllocator
, event
);
1751 return vk_error(device
->instance
, VK_ERROR_OUT_OF_HOST_MEMORY
);
1755 tu_DestroyEvent(VkDevice _device
,
1757 const VkAllocationCallbacks
*pAllocator
)
1759 TU_FROM_HANDLE(tu_device
, device
, _device
);
1760 TU_FROM_HANDLE(tu_event
, event
, _event
);
1765 tu_bo_finish(device
, &event
->bo
);
1766 vk_object_free(&device
->vk
, pAllocator
, event
);
1770 tu_GetEventStatus(VkDevice _device
, VkEvent _event
)
1772 TU_FROM_HANDLE(tu_event
, event
, _event
);
1774 if (*(uint64_t*) event
->bo
.map
== 1)
1775 return VK_EVENT_SET
;
1776 return VK_EVENT_RESET
;
1780 tu_SetEvent(VkDevice _device
, VkEvent _event
)
1782 TU_FROM_HANDLE(tu_event
, event
, _event
);
1783 *(uint64_t*) event
->bo
.map
= 1;
1789 tu_ResetEvent(VkDevice _device
, VkEvent _event
)
1791 TU_FROM_HANDLE(tu_event
, event
, _event
);
1792 *(uint64_t*) event
->bo
.map
= 0;
1798 tu_CreateBuffer(VkDevice _device
,
1799 const VkBufferCreateInfo
*pCreateInfo
,
1800 const VkAllocationCallbacks
*pAllocator
,
1803 TU_FROM_HANDLE(tu_device
, device
, _device
);
1804 struct tu_buffer
*buffer
;
1806 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO
);
1808 buffer
= vk_object_alloc(&device
->vk
, pAllocator
, sizeof(*buffer
),
1809 VK_OBJECT_TYPE_BUFFER
);
1811 return vk_error(device
->instance
, VK_ERROR_OUT_OF_HOST_MEMORY
);
1813 buffer
->size
= pCreateInfo
->size
;
1814 buffer
->usage
= pCreateInfo
->usage
;
1815 buffer
->flags
= pCreateInfo
->flags
;
1817 *pBuffer
= tu_buffer_to_handle(buffer
);
1823 tu_DestroyBuffer(VkDevice _device
,
1825 const VkAllocationCallbacks
*pAllocator
)
1827 TU_FROM_HANDLE(tu_device
, device
, _device
);
1828 TU_FROM_HANDLE(tu_buffer
, buffer
, _buffer
);
1833 vk_object_free(&device
->vk
, pAllocator
, buffer
);
1837 tu_CreateFramebuffer(VkDevice _device
,
1838 const VkFramebufferCreateInfo
*pCreateInfo
,
1839 const VkAllocationCallbacks
*pAllocator
,
1840 VkFramebuffer
*pFramebuffer
)
1842 TU_FROM_HANDLE(tu_device
, device
, _device
);
1843 TU_FROM_HANDLE(tu_render_pass
, pass
, pCreateInfo
->renderPass
);
1844 struct tu_framebuffer
*framebuffer
;
1846 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO
);
1848 size_t size
= sizeof(*framebuffer
) + sizeof(struct tu_attachment_info
) *
1849 pCreateInfo
->attachmentCount
;
1850 framebuffer
= vk_object_alloc(&device
->vk
, pAllocator
, size
,
1851 VK_OBJECT_TYPE_FRAMEBUFFER
);
1852 if (framebuffer
== NULL
)
1853 return vk_error(device
->instance
, VK_ERROR_OUT_OF_HOST_MEMORY
);
1855 framebuffer
->attachment_count
= pCreateInfo
->attachmentCount
;
1856 framebuffer
->width
= pCreateInfo
->width
;
1857 framebuffer
->height
= pCreateInfo
->height
;
1858 framebuffer
->layers
= pCreateInfo
->layers
;
1859 for (uint32_t i
= 0; i
< pCreateInfo
->attachmentCount
; i
++) {
1860 VkImageView _iview
= pCreateInfo
->pAttachments
[i
];
1861 struct tu_image_view
*iview
= tu_image_view_from_handle(_iview
);
1862 framebuffer
->attachments
[i
].attachment
= iview
;
1865 tu_framebuffer_tiling_config(framebuffer
, device
, pass
);
1867 *pFramebuffer
= tu_framebuffer_to_handle(framebuffer
);
1872 tu_DestroyFramebuffer(VkDevice _device
,
1874 const VkAllocationCallbacks
*pAllocator
)
1876 TU_FROM_HANDLE(tu_device
, device
, _device
);
1877 TU_FROM_HANDLE(tu_framebuffer
, fb
, _fb
);
1882 vk_object_free(&device
->vk
, pAllocator
, fb
);
1886 tu_init_sampler(struct tu_device
*device
,
1887 struct tu_sampler
*sampler
,
1888 const VkSamplerCreateInfo
*pCreateInfo
)
1890 const struct VkSamplerReductionModeCreateInfo
*reduction
=
1891 vk_find_struct_const(pCreateInfo
->pNext
, SAMPLER_REDUCTION_MODE_CREATE_INFO
);
1892 const struct VkSamplerYcbcrConversionInfo
*ycbcr_conversion
=
1893 vk_find_struct_const(pCreateInfo
->pNext
, SAMPLER_YCBCR_CONVERSION_INFO
);
1894 const VkSamplerCustomBorderColorCreateInfoEXT
*custom_border_color
=
1895 vk_find_struct_const(pCreateInfo
->pNext
, SAMPLER_CUSTOM_BORDER_COLOR_CREATE_INFO_EXT
);
1896 /* for non-custom border colors, the VK enum is translated directly to an offset in
1897 * the border color buffer. custom border colors are located immediately after the
1898 * builtin colors, and thus an offset of TU_BORDER_COLOR_BUILTIN is added.
1900 uint32_t border_color
= (unsigned) pCreateInfo
->borderColor
;
1901 if (pCreateInfo
->borderColor
== VK_BORDER_COLOR_FLOAT_CUSTOM_EXT
||
1902 pCreateInfo
->borderColor
== VK_BORDER_COLOR_INT_CUSTOM_EXT
) {
1903 mtx_lock(&device
->mutex
);
1904 border_color
= BITSET_FFS(device
->custom_border_color
);
1905 BITSET_CLEAR(device
->custom_border_color
, border_color
);
1906 mtx_unlock(&device
->mutex
);
1907 tu6_pack_border_color(device
->global_bo
.map
+ gb_offset(bcolor
[border_color
]),
1908 &custom_border_color
->customBorderColor
,
1909 pCreateInfo
->borderColor
== VK_BORDER_COLOR_INT_CUSTOM_EXT
);
1910 border_color
+= TU_BORDER_COLOR_BUILTIN
;
1913 unsigned aniso
= pCreateInfo
->anisotropyEnable
?
1914 util_last_bit(MIN2((uint32_t)pCreateInfo
->maxAnisotropy
>> 1, 8)) : 0;
1915 bool miplinear
= (pCreateInfo
->mipmapMode
== VK_SAMPLER_MIPMAP_MODE_LINEAR
);
1916 float min_lod
= CLAMP(pCreateInfo
->minLod
, 0.0f
, 4095.0f
/ 256.0f
);
1917 float max_lod
= CLAMP(pCreateInfo
->maxLod
, 0.0f
, 4095.0f
/ 256.0f
);
1919 sampler
->descriptor
[0] =
1920 COND(miplinear
, A6XX_TEX_SAMP_0_MIPFILTER_LINEAR_NEAR
) |
1921 A6XX_TEX_SAMP_0_XY_MAG(tu6_tex_filter(pCreateInfo
->magFilter
, aniso
)) |
1922 A6XX_TEX_SAMP_0_XY_MIN(tu6_tex_filter(pCreateInfo
->minFilter
, aniso
)) |
1923 A6XX_TEX_SAMP_0_ANISO(aniso
) |
1924 A6XX_TEX_SAMP_0_WRAP_S(tu6_tex_wrap(pCreateInfo
->addressModeU
)) |
1925 A6XX_TEX_SAMP_0_WRAP_T(tu6_tex_wrap(pCreateInfo
->addressModeV
)) |
1926 A6XX_TEX_SAMP_0_WRAP_R(tu6_tex_wrap(pCreateInfo
->addressModeW
)) |
1927 A6XX_TEX_SAMP_0_LOD_BIAS(pCreateInfo
->mipLodBias
);
1928 sampler
->descriptor
[1] =
1929 /* COND(!cso->seamless_cube_map, A6XX_TEX_SAMP_1_CUBEMAPSEAMLESSFILTOFF) | */
1930 COND(pCreateInfo
->unnormalizedCoordinates
, A6XX_TEX_SAMP_1_UNNORM_COORDS
) |
1931 A6XX_TEX_SAMP_1_MIN_LOD(min_lod
) |
1932 A6XX_TEX_SAMP_1_MAX_LOD(max_lod
) |
1933 COND(pCreateInfo
->compareEnable
,
1934 A6XX_TEX_SAMP_1_COMPARE_FUNC(tu6_compare_func(pCreateInfo
->compareOp
)));
1935 sampler
->descriptor
[2] = A6XX_TEX_SAMP_2_BCOLOR(border_color
);
1936 sampler
->descriptor
[3] = 0;
1939 sampler
->descriptor
[2] |= A6XX_TEX_SAMP_2_REDUCTION_MODE(
1940 tu6_reduction_mode(reduction
->reductionMode
));
1943 sampler
->ycbcr_sampler
= ycbcr_conversion
?
1944 tu_sampler_ycbcr_conversion_from_handle(ycbcr_conversion
->conversion
) : NULL
;
1946 if (sampler
->ycbcr_sampler
&&
1947 sampler
->ycbcr_sampler
->chroma_filter
== VK_FILTER_LINEAR
) {
1948 sampler
->descriptor
[2] |= A6XX_TEX_SAMP_2_CHROMA_LINEAR
;
1952 * A6XX_TEX_SAMP_1_MIPFILTER_LINEAR_FAR disables mipmapping, but vk has no NONE mipfilter?
1957 tu_CreateSampler(VkDevice _device
,
1958 const VkSamplerCreateInfo
*pCreateInfo
,
1959 const VkAllocationCallbacks
*pAllocator
,
1960 VkSampler
*pSampler
)
1962 TU_FROM_HANDLE(tu_device
, device
, _device
);
1963 struct tu_sampler
*sampler
;
1965 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO
);
1967 sampler
= vk_object_alloc(&device
->vk
, pAllocator
, sizeof(*sampler
),
1968 VK_OBJECT_TYPE_SAMPLER
);
1970 return vk_error(device
->instance
, VK_ERROR_OUT_OF_HOST_MEMORY
);
1972 tu_init_sampler(device
, sampler
, pCreateInfo
);
1973 *pSampler
= tu_sampler_to_handle(sampler
);
1979 tu_DestroySampler(VkDevice _device
,
1981 const VkAllocationCallbacks
*pAllocator
)
1983 TU_FROM_HANDLE(tu_device
, device
, _device
);
1984 TU_FROM_HANDLE(tu_sampler
, sampler
, _sampler
);
1985 uint32_t border_color
;
1990 border_color
= (sampler
->descriptor
[2] & A6XX_TEX_SAMP_2_BCOLOR__MASK
) >> A6XX_TEX_SAMP_2_BCOLOR__SHIFT
;
1991 if (border_color
>= TU_BORDER_COLOR_BUILTIN
) {
1992 border_color
-= TU_BORDER_COLOR_BUILTIN
;
1993 /* if the sampler had a custom border color, free it. TODO: no lock */
1994 mtx_lock(&device
->mutex
);
1995 assert(!BITSET_TEST(device
->custom_border_color
, border_color
));
1996 BITSET_SET(device
->custom_border_color
, border_color
);
1997 mtx_unlock(&device
->mutex
);
2000 vk_object_free(&device
->vk
, pAllocator
, sampler
);
2003 /* vk_icd.h does not declare this function, so we declare it here to
2004 * suppress Wmissing-prototypes.
2006 PUBLIC VKAPI_ATTR VkResult VKAPI_CALL
2007 vk_icdNegotiateLoaderICDInterfaceVersion(uint32_t *pSupportedVersion
);
2009 PUBLIC VKAPI_ATTR VkResult VKAPI_CALL
2010 vk_icdNegotiateLoaderICDInterfaceVersion(uint32_t *pSupportedVersion
)
2012 /* For the full details on loader interface versioning, see
2013 * <https://github.com/KhronosGroup/Vulkan-LoaderAndValidationLayers/blob/master/loader/LoaderAndLayerInterface.md>.
2014 * What follows is a condensed summary, to help you navigate the large and
2015 * confusing official doc.
2017 * - Loader interface v0 is incompatible with later versions. We don't
2020 * - In loader interface v1:
2021 * - The first ICD entrypoint called by the loader is
2022 * vk_icdGetInstanceProcAddr(). The ICD must statically expose this
2024 * - The ICD must statically expose no other Vulkan symbol unless it
2025 * is linked with -Bsymbolic.
2026 * - Each dispatchable Vulkan handle created by the ICD must be
2027 * a pointer to a struct whose first member is VK_LOADER_DATA. The
2028 * ICD must initialize VK_LOADER_DATA.loadMagic to
2030 * - The loader implements vkCreate{PLATFORM}SurfaceKHR() and
2031 * vkDestroySurfaceKHR(). The ICD must be capable of working with
2032 * such loader-managed surfaces.
2034 * - Loader interface v2 differs from v1 in:
2035 * - The first ICD entrypoint called by the loader is
2036 * vk_icdNegotiateLoaderICDInterfaceVersion(). The ICD must
2037 * statically expose this entrypoint.
2039 * - Loader interface v3 differs from v2 in:
2040 * - The ICD must implement vkCreate{PLATFORM}SurfaceKHR(),
2041 * vkDestroySurfaceKHR(), and other API which uses VKSurfaceKHR,
2042 * because the loader no longer does so.
2044 *pSupportedVersion
= MIN2(*pSupportedVersion
, 3u);
2049 tu_GetMemoryFdKHR(VkDevice _device
,
2050 const VkMemoryGetFdInfoKHR
*pGetFdInfo
,
2053 TU_FROM_HANDLE(tu_device
, device
, _device
);
2054 TU_FROM_HANDLE(tu_device_memory
, memory
, pGetFdInfo
->memory
);
2056 assert(pGetFdInfo
->sType
== VK_STRUCTURE_TYPE_MEMORY_GET_FD_INFO_KHR
);
2058 /* At the moment, we support only the below handle types. */
2059 assert(pGetFdInfo
->handleType
==
2060 VK_EXTERNAL_MEMORY_HANDLE_TYPE_OPAQUE_FD_BIT
||
2061 pGetFdInfo
->handleType
==
2062 VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_EXT
);
2064 int prime_fd
= tu_bo_export_dmabuf(device
, &memory
->bo
);
2066 return vk_error(device
->instance
, VK_ERROR_OUT_OF_DEVICE_MEMORY
);
2073 tu_GetMemoryFdPropertiesKHR(VkDevice _device
,
2074 VkExternalMemoryHandleTypeFlagBits handleType
,
2076 VkMemoryFdPropertiesKHR
*pMemoryFdProperties
)
2078 assert(handleType
== VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_EXT
);
2079 pMemoryFdProperties
->memoryTypeBits
= 1;
2084 tu_ImportFenceFdKHR(VkDevice _device
,
2085 const VkImportFenceFdInfoKHR
*pImportFenceFdInfo
)
2093 tu_GetFenceFdKHR(VkDevice _device
,
2094 const VkFenceGetFdInfoKHR
*pGetFdInfo
,
2103 tu_GetPhysicalDeviceExternalFenceProperties(
2104 VkPhysicalDevice physicalDevice
,
2105 const VkPhysicalDeviceExternalFenceInfo
*pExternalFenceInfo
,
2106 VkExternalFenceProperties
*pExternalFenceProperties
)
2108 pExternalFenceProperties
->exportFromImportedHandleTypes
= 0;
2109 pExternalFenceProperties
->compatibleHandleTypes
= 0;
2110 pExternalFenceProperties
->externalFenceFeatures
= 0;
2114 tu_CreateDebugReportCallbackEXT(
2115 VkInstance _instance
,
2116 const VkDebugReportCallbackCreateInfoEXT
*pCreateInfo
,
2117 const VkAllocationCallbacks
*pAllocator
,
2118 VkDebugReportCallbackEXT
*pCallback
)
2120 TU_FROM_HANDLE(tu_instance
, instance
, _instance
);
2121 return vk_create_debug_report_callback(&instance
->debug_report_callbacks
,
2122 pCreateInfo
, pAllocator
,
2123 &instance
->alloc
, pCallback
);
2127 tu_DestroyDebugReportCallbackEXT(VkInstance _instance
,
2128 VkDebugReportCallbackEXT _callback
,
2129 const VkAllocationCallbacks
*pAllocator
)
2131 TU_FROM_HANDLE(tu_instance
, instance
, _instance
);
2132 vk_destroy_debug_report_callback(&instance
->debug_report_callbacks
,
2133 _callback
, pAllocator
, &instance
->alloc
);
2137 tu_DebugReportMessageEXT(VkInstance _instance
,
2138 VkDebugReportFlagsEXT flags
,
2139 VkDebugReportObjectTypeEXT objectType
,
2142 int32_t messageCode
,
2143 const char *pLayerPrefix
,
2144 const char *pMessage
)
2146 TU_FROM_HANDLE(tu_instance
, instance
, _instance
);
2147 vk_debug_report(&instance
->debug_report_callbacks
, flags
, objectType
,
2148 object
, location
, messageCode
, pLayerPrefix
, pMessage
);
2152 tu_GetDeviceGroupPeerMemoryFeatures(
2155 uint32_t localDeviceIndex
,
2156 uint32_t remoteDeviceIndex
,
2157 VkPeerMemoryFeatureFlags
*pPeerMemoryFeatures
)
2159 assert(localDeviceIndex
== remoteDeviceIndex
);
2161 *pPeerMemoryFeatures
= VK_PEER_MEMORY_FEATURE_COPY_SRC_BIT
|
2162 VK_PEER_MEMORY_FEATURE_COPY_DST_BIT
|
2163 VK_PEER_MEMORY_FEATURE_GENERIC_SRC_BIT
|
2164 VK_PEER_MEMORY_FEATURE_GENERIC_DST_BIT
;
2167 void tu_GetPhysicalDeviceMultisamplePropertiesEXT(
2168 VkPhysicalDevice physicalDevice
,
2169 VkSampleCountFlagBits samples
,
2170 VkMultisamplePropertiesEXT
* pMultisampleProperties
)
2172 TU_FROM_HANDLE(tu_physical_device
, pdevice
, physicalDevice
);
2174 if (samples
<= VK_SAMPLE_COUNT_4_BIT
&& pdevice
->supported_extensions
.EXT_sample_locations
)
2175 pMultisampleProperties
->maxSampleLocationGridSize
= (VkExtent2D
){ 1, 1 };
2177 pMultisampleProperties
->maxSampleLocationGridSize
= (VkExtent2D
){ 0, 0 };
2182 tu_CreatePrivateDataSlotEXT(VkDevice _device
,
2183 const VkPrivateDataSlotCreateInfoEXT
* pCreateInfo
,
2184 const VkAllocationCallbacks
* pAllocator
,
2185 VkPrivateDataSlotEXT
* pPrivateDataSlot
)
2187 TU_FROM_HANDLE(tu_device
, device
, _device
);
2188 return vk_private_data_slot_create(&device
->vk
,
2195 tu_DestroyPrivateDataSlotEXT(VkDevice _device
,
2196 VkPrivateDataSlotEXT privateDataSlot
,
2197 const VkAllocationCallbacks
* pAllocator
)
2199 TU_FROM_HANDLE(tu_device
, device
, _device
);
2200 vk_private_data_slot_destroy(&device
->vk
, privateDataSlot
, pAllocator
);
2204 tu_SetPrivateDataEXT(VkDevice _device
,
2205 VkObjectType objectType
,
2206 uint64_t objectHandle
,
2207 VkPrivateDataSlotEXT privateDataSlot
,
2210 TU_FROM_HANDLE(tu_device
, device
, _device
);
2211 return vk_object_base_set_private_data(&device
->vk
,
2219 tu_GetPrivateDataEXT(VkDevice _device
,
2220 VkObjectType objectType
,
2221 uint64_t objectHandle
,
2222 VkPrivateDataSlotEXT privateDataSlot
,
2225 TU_FROM_HANDLE(tu_device
, device
, _device
);
2226 vk_object_base_get_private_data(&device
->vk
,