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"
35 #include <sys/sysinfo.h>
39 #include "compiler/glsl_types.h"
40 #include "util/debug.h"
41 #include "util/disk_cache.h"
42 #include "vk_format.h"
45 #include "drm-uapi/msm_drm.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_get_driver_uuid(void *uuid
)
66 memset(uuid
, 0, VK_UUID_SIZE
);
67 snprintf(uuid
, VK_UUID_SIZE
, "freedreno");
71 tu_get_device_uuid(void *uuid
)
73 memset(uuid
, 0, VK_UUID_SIZE
);
77 tu_bo_init(struct tu_device
*dev
,
82 uint64_t iova
= tu_gem_info_iova(dev
, gem_handle
);
84 return VK_ERROR_OUT_OF_DEVICE_MEMORY
;
86 *bo
= (struct tu_bo
) {
87 .gem_handle
= gem_handle
,
96 tu_bo_init_new(struct tu_device
*dev
, struct tu_bo
*bo
, uint64_t size
)
98 /* TODO: Choose better flags. As of 2018-11-12, freedreno/drm/msm_bo.c
99 * always sets `flags = MSM_BO_WC`, and we copy that behavior here.
101 uint32_t gem_handle
= tu_gem_new(dev
, size
, MSM_BO_WC
);
103 return vk_error(dev
->instance
, VK_ERROR_OUT_OF_DEVICE_MEMORY
);
105 VkResult result
= tu_bo_init(dev
, bo
, gem_handle
, size
);
106 if (result
!= VK_SUCCESS
) {
107 tu_gem_close(dev
, gem_handle
);
108 return vk_error(dev
->instance
, result
);
115 tu_bo_init_dmabuf(struct tu_device
*dev
,
120 uint32_t gem_handle
= tu_gem_import_dmabuf(dev
, fd
, size
);
122 return vk_error(dev
->instance
, VK_ERROR_INVALID_EXTERNAL_HANDLE
);
124 VkResult result
= tu_bo_init(dev
, bo
, gem_handle
, size
);
125 if (result
!= VK_SUCCESS
) {
126 tu_gem_close(dev
, gem_handle
);
127 return vk_error(dev
->instance
, result
);
134 tu_bo_export_dmabuf(struct tu_device
*dev
, struct tu_bo
*bo
)
136 return tu_gem_export_dmabuf(dev
, bo
->gem_handle
);
140 tu_bo_map(struct tu_device
*dev
, struct tu_bo
*bo
)
145 uint64_t offset
= tu_gem_info_offset(dev
, bo
->gem_handle
);
147 return vk_error(dev
->instance
, VK_ERROR_OUT_OF_DEVICE_MEMORY
);
149 /* TODO: Should we use the wrapper os_mmap() like Freedreno does? */
150 void *map
= mmap(0, bo
->size
, PROT_READ
| PROT_WRITE
, MAP_SHARED
,
151 dev
->physical_device
->local_fd
, offset
);
152 if (map
== MAP_FAILED
)
153 return vk_error(dev
->instance
, VK_ERROR_MEMORY_MAP_FAILED
);
160 tu_bo_finish(struct tu_device
*dev
, struct tu_bo
*bo
)
162 assert(bo
->gem_handle
);
165 munmap(bo
->map
, bo
->size
);
167 tu_gem_close(dev
, bo
->gem_handle
);
171 tu_physical_device_init(struct tu_physical_device
*device
,
172 struct tu_instance
*instance
,
173 drmDevicePtr drm_device
)
175 const char *path
= drm_device
->nodes
[DRM_NODE_RENDER
];
176 VkResult result
= VK_SUCCESS
;
177 drmVersionPtr version
;
181 fd
= open(path
, O_RDWR
| O_CLOEXEC
);
183 return vk_errorf(instance
, VK_ERROR_INCOMPATIBLE_DRIVER
,
184 "failed to open device %s", path
);
187 /* Version 1.3 added MSM_INFO_IOVA. */
188 const int min_version_major
= 1;
189 const int min_version_minor
= 3;
191 version
= drmGetVersion(fd
);
194 return vk_errorf(instance
, VK_ERROR_INCOMPATIBLE_DRIVER
,
195 "failed to query kernel driver version for device %s",
199 if (strcmp(version
->name
, "msm")) {
200 drmFreeVersion(version
);
202 return vk_errorf(instance
, VK_ERROR_INCOMPATIBLE_DRIVER
,
203 "device %s does not use the msm kernel driver", path
);
206 if (version
->version_major
!= min_version_major
||
207 version
->version_minor
< min_version_minor
) {
208 result
= vk_errorf(instance
, VK_ERROR_INCOMPATIBLE_DRIVER
,
209 "kernel driver for device %s has version %d.%d, "
210 "but Vulkan requires version >= %d.%d",
211 path
, version
->version_major
, version
->version_minor
,
212 min_version_major
, min_version_minor
);
213 drmFreeVersion(version
);
218 drmFreeVersion(version
);
220 if (instance
->debug_flags
& TU_DEBUG_STARTUP
)
221 tu_logi("Found compatible device '%s'.", path
);
223 device
->_loader_data
.loaderMagic
= ICD_LOADER_MAGIC
;
224 device
->instance
= instance
;
225 assert(strlen(path
) < ARRAY_SIZE(device
->path
));
226 strncpy(device
->path
, path
, ARRAY_SIZE(device
->path
));
228 if (instance
->enabled_extensions
.KHR_display
) {
230 open(drm_device
->nodes
[DRM_NODE_PRIMARY
], O_RDWR
| O_CLOEXEC
);
231 if (master_fd
>= 0) {
232 /* TODO: free master_fd is accel is not working? */
236 device
->master_fd
= master_fd
;
237 device
->local_fd
= fd
;
239 if (tu_drm_get_gpu_id(device
, &device
->gpu_id
)) {
240 if (instance
->debug_flags
& TU_DEBUG_STARTUP
)
241 tu_logi("Could not query the GPU ID");
242 result
= vk_errorf(instance
, VK_ERROR_INITIALIZATION_FAILED
,
243 "could not get GPU ID");
247 if (tu_drm_get_gmem_size(device
, &device
->gmem_size
)) {
248 if (instance
->debug_flags
& TU_DEBUG_STARTUP
)
249 tu_logi("Could not query the GMEM size");
250 result
= vk_errorf(instance
, VK_ERROR_INITIALIZATION_FAILED
,
251 "could not get GMEM size");
255 if (tu_drm_get_gmem_base(device
, &device
->gmem_base
)) {
256 if (instance
->debug_flags
& TU_DEBUG_STARTUP
)
257 tu_logi("Could not query the GMEM size");
258 result
= vk_errorf(instance
, VK_ERROR_INITIALIZATION_FAILED
,
259 "could not get GMEM size");
263 memset(device
->name
, 0, sizeof(device
->name
));
264 sprintf(device
->name
, "FD%d", device
->gpu_id
);
266 switch (device
->gpu_id
) {
268 device
->magic
.RB_UNKNOWN_8E04_blit
= 0x00100000;
269 device
->ccu_offset_gmem
= 0x7c000; /* 0x7e000 in some cases? */
270 device
->ccu_offset_bypass
= 0x10000;
271 device
->magic
.PC_UNKNOWN_9805
= 0x0;
272 device
->magic
.SP_UNKNOWN_A0F8
= 0x0;
276 device
->magic
.RB_UNKNOWN_8E04_blit
= 0x01000000;
277 device
->ccu_offset_gmem
= 0xf8000;
278 device
->ccu_offset_bypass
= 0x20000;
279 device
->magic
.PC_UNKNOWN_9805
= 0x1;
280 device
->magic
.SP_UNKNOWN_A0F8
= 0x1;
283 result
= vk_errorf(instance
, VK_ERROR_INITIALIZATION_FAILED
,
284 "device %s is unsupported", device
->name
);
287 if (tu_device_get_cache_uuid(device
->gpu_id
, device
->cache_uuid
)) {
288 result
= vk_errorf(instance
, VK_ERROR_INITIALIZATION_FAILED
,
289 "cannot generate UUID");
293 /* The gpu id is already embedded in the uuid so we just pass "tu"
294 * when creating the cache.
296 char buf
[VK_UUID_SIZE
* 2 + 1];
297 disk_cache_format_hex_id(buf
, device
->cache_uuid
, VK_UUID_SIZE
* 2);
298 device
->disk_cache
= disk_cache_create(device
->name
, buf
, 0);
300 fprintf(stderr
, "WARNING: tu is not a conformant vulkan implementation, "
301 "testing use only.\n");
303 tu_get_driver_uuid(&device
->device_uuid
);
304 tu_get_device_uuid(&device
->device_uuid
);
306 tu_fill_device_extension_table(device
, &device
->supported_extensions
);
308 if (result
!= VK_SUCCESS
) {
309 vk_error(instance
, result
);
313 result
= tu_wsi_init(device
);
314 if (result
!= VK_SUCCESS
) {
315 vk_error(instance
, result
);
329 tu_physical_device_finish(struct tu_physical_device
*device
)
331 tu_wsi_finish(device
);
333 disk_cache_destroy(device
->disk_cache
);
334 close(device
->local_fd
);
335 if (device
->master_fd
!= -1)
336 close(device
->master_fd
);
339 static VKAPI_ATTR
void *
340 default_alloc_func(void *pUserData
,
343 VkSystemAllocationScope allocationScope
)
348 static VKAPI_ATTR
void *
349 default_realloc_func(void *pUserData
,
353 VkSystemAllocationScope allocationScope
)
355 return realloc(pOriginal
, size
);
358 static VKAPI_ATTR
void
359 default_free_func(void *pUserData
, void *pMemory
)
364 static const VkAllocationCallbacks default_alloc
= {
366 .pfnAllocation
= default_alloc_func
,
367 .pfnReallocation
= default_realloc_func
,
368 .pfnFree
= default_free_func
,
371 static const struct debug_control tu_debug_options
[] = {
372 { "startup", TU_DEBUG_STARTUP
},
373 { "nir", TU_DEBUG_NIR
},
374 { "ir3", TU_DEBUG_IR3
},
375 { "nobin", TU_DEBUG_NOBIN
},
376 { "sysmem", TU_DEBUG_SYSMEM
},
377 { "forcebin", TU_DEBUG_FORCEBIN
},
382 tu_get_debug_option_name(int id
)
384 assert(id
< ARRAY_SIZE(tu_debug_options
) - 1);
385 return tu_debug_options
[id
].string
;
389 tu_get_instance_extension_index(const char *name
)
391 for (unsigned i
= 0; i
< TU_INSTANCE_EXTENSION_COUNT
; ++i
) {
392 if (strcmp(name
, tu_instance_extensions
[i
].extensionName
) == 0)
399 tu_CreateInstance(const VkInstanceCreateInfo
*pCreateInfo
,
400 const VkAllocationCallbacks
*pAllocator
,
401 VkInstance
*pInstance
)
403 struct tu_instance
*instance
;
406 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO
);
408 uint32_t client_version
;
409 if (pCreateInfo
->pApplicationInfo
&&
410 pCreateInfo
->pApplicationInfo
->apiVersion
!= 0) {
411 client_version
= pCreateInfo
->pApplicationInfo
->apiVersion
;
413 tu_EnumerateInstanceVersion(&client_version
);
416 instance
= vk_zalloc2(&default_alloc
, pAllocator
, sizeof(*instance
), 8,
417 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE
);
419 return vk_error(NULL
, VK_ERROR_OUT_OF_HOST_MEMORY
);
421 instance
->_loader_data
.loaderMagic
= ICD_LOADER_MAGIC
;
424 instance
->alloc
= *pAllocator
;
426 instance
->alloc
= default_alloc
;
428 instance
->api_version
= client_version
;
429 instance
->physical_device_count
= -1;
431 instance
->debug_flags
=
432 parse_debug_string(getenv("TU_DEBUG"), tu_debug_options
);
434 if (instance
->debug_flags
& TU_DEBUG_STARTUP
)
435 tu_logi("Created an instance");
437 for (uint32_t i
= 0; i
< pCreateInfo
->enabledExtensionCount
; i
++) {
438 const char *ext_name
= pCreateInfo
->ppEnabledExtensionNames
[i
];
439 int index
= tu_get_instance_extension_index(ext_name
);
441 if (index
< 0 || !tu_supported_instance_extensions
.extensions
[index
]) {
442 vk_free2(&default_alloc
, pAllocator
, instance
);
443 return vk_error(instance
, VK_ERROR_EXTENSION_NOT_PRESENT
);
446 instance
->enabled_extensions
.extensions
[index
] = true;
449 result
= vk_debug_report_instance_init(&instance
->debug_report_callbacks
);
450 if (result
!= VK_SUCCESS
) {
451 vk_free2(&default_alloc
, pAllocator
, instance
);
452 return vk_error(instance
, result
);
455 glsl_type_singleton_init_or_ref();
457 VG(VALGRIND_CREATE_MEMPOOL(instance
, 0, false));
459 *pInstance
= tu_instance_to_handle(instance
);
465 tu_DestroyInstance(VkInstance _instance
,
466 const VkAllocationCallbacks
*pAllocator
)
468 TU_FROM_HANDLE(tu_instance
, instance
, _instance
);
473 for (int i
= 0; i
< instance
->physical_device_count
; ++i
) {
474 tu_physical_device_finish(instance
->physical_devices
+ i
);
477 VG(VALGRIND_DESTROY_MEMPOOL(instance
));
479 glsl_type_singleton_decref();
481 vk_debug_report_instance_destroy(&instance
->debug_report_callbacks
);
483 vk_free(&instance
->alloc
, instance
);
487 tu_enumerate_devices(struct tu_instance
*instance
)
489 /* TODO: Check for more devices ? */
490 drmDevicePtr devices
[8];
491 VkResult result
= VK_ERROR_INCOMPATIBLE_DRIVER
;
494 instance
->physical_device_count
= 0;
496 max_devices
= drmGetDevices2(0, devices
, ARRAY_SIZE(devices
));
498 if (instance
->debug_flags
& TU_DEBUG_STARTUP
)
499 tu_logi("Found %d drm nodes", max_devices
);
502 return vk_error(instance
, VK_ERROR_INCOMPATIBLE_DRIVER
);
504 for (unsigned i
= 0; i
< (unsigned) max_devices
; i
++) {
505 if (devices
[i
]->available_nodes
& 1 << DRM_NODE_RENDER
&&
506 devices
[i
]->bustype
== DRM_BUS_PLATFORM
) {
508 result
= tu_physical_device_init(
509 instance
->physical_devices
+ instance
->physical_device_count
,
510 instance
, devices
[i
]);
511 if (result
== VK_SUCCESS
)
512 ++instance
->physical_device_count
;
513 else if (result
!= VK_ERROR_INCOMPATIBLE_DRIVER
)
517 drmFreeDevices(devices
, max_devices
);
523 tu_EnumeratePhysicalDevices(VkInstance _instance
,
524 uint32_t *pPhysicalDeviceCount
,
525 VkPhysicalDevice
*pPhysicalDevices
)
527 TU_FROM_HANDLE(tu_instance
, instance
, _instance
);
528 VK_OUTARRAY_MAKE(out
, pPhysicalDevices
, pPhysicalDeviceCount
);
532 if (instance
->physical_device_count
< 0) {
533 result
= tu_enumerate_devices(instance
);
534 if (result
!= VK_SUCCESS
&& result
!= VK_ERROR_INCOMPATIBLE_DRIVER
)
538 for (uint32_t i
= 0; i
< instance
->physical_device_count
; ++i
) {
539 vk_outarray_append(&out
, p
)
541 *p
= tu_physical_device_to_handle(instance
->physical_devices
+ i
);
545 return vk_outarray_status(&out
);
549 tu_EnumeratePhysicalDeviceGroups(
550 VkInstance _instance
,
551 uint32_t *pPhysicalDeviceGroupCount
,
552 VkPhysicalDeviceGroupProperties
*pPhysicalDeviceGroupProperties
)
554 TU_FROM_HANDLE(tu_instance
, instance
, _instance
);
555 VK_OUTARRAY_MAKE(out
, pPhysicalDeviceGroupProperties
,
556 pPhysicalDeviceGroupCount
);
559 if (instance
->physical_device_count
< 0) {
560 result
= tu_enumerate_devices(instance
);
561 if (result
!= VK_SUCCESS
&& result
!= VK_ERROR_INCOMPATIBLE_DRIVER
)
565 for (uint32_t i
= 0; i
< instance
->physical_device_count
; ++i
) {
566 vk_outarray_append(&out
, p
)
568 p
->physicalDeviceCount
= 1;
569 p
->physicalDevices
[0] =
570 tu_physical_device_to_handle(instance
->physical_devices
+ i
);
571 p
->subsetAllocation
= false;
575 return vk_outarray_status(&out
);
579 tu_GetPhysicalDeviceFeatures(VkPhysicalDevice physicalDevice
,
580 VkPhysicalDeviceFeatures
*pFeatures
)
582 memset(pFeatures
, 0, sizeof(*pFeatures
));
584 *pFeatures
= (VkPhysicalDeviceFeatures
) {
585 .robustBufferAccess
= false,
586 .fullDrawIndexUint32
= true,
587 .imageCubeArray
= false,
588 .independentBlend
= true,
589 .geometryShader
= true,
590 .tessellationShader
= false,
591 .sampleRateShading
= true,
592 .dualSrcBlend
= true,
594 .multiDrawIndirect
= false,
595 .drawIndirectFirstInstance
= false,
597 .depthBiasClamp
= false,
598 .fillModeNonSolid
= false,
599 .depthBounds
= false,
601 .largePoints
= false,
603 .multiViewport
= false,
604 .samplerAnisotropy
= true,
605 .textureCompressionETC2
= true,
606 .textureCompressionASTC_LDR
= true,
607 .textureCompressionBC
= true,
608 .occlusionQueryPrecise
= true,
609 .pipelineStatisticsQuery
= false,
610 .vertexPipelineStoresAndAtomics
= false,
611 .fragmentStoresAndAtomics
= false,
612 .shaderTessellationAndGeometryPointSize
= false,
613 .shaderImageGatherExtended
= false,
614 .shaderStorageImageExtendedFormats
= false,
615 .shaderStorageImageMultisample
= false,
616 .shaderUniformBufferArrayDynamicIndexing
= false,
617 .shaderSampledImageArrayDynamicIndexing
= false,
618 .shaderStorageBufferArrayDynamicIndexing
= false,
619 .shaderStorageImageArrayDynamicIndexing
= false,
620 .shaderStorageImageReadWithoutFormat
= false,
621 .shaderStorageImageWriteWithoutFormat
= false,
622 .shaderClipDistance
= false,
623 .shaderCullDistance
= false,
624 .shaderFloat64
= false,
625 .shaderInt64
= false,
626 .shaderInt16
= false,
627 .sparseBinding
= false,
628 .variableMultisampleRate
= false,
629 .inheritedQueries
= false,
634 tu_GetPhysicalDeviceFeatures2(VkPhysicalDevice physicalDevice
,
635 VkPhysicalDeviceFeatures2
*pFeatures
)
637 vk_foreach_struct(ext
, pFeatures
->pNext
)
639 switch (ext
->sType
) {
640 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VARIABLE_POINTERS_FEATURES
: {
641 VkPhysicalDeviceVariablePointersFeatures
*features
= (void *) ext
;
642 features
->variablePointersStorageBuffer
= false;
643 features
->variablePointers
= false;
646 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_MULTIVIEW_FEATURES
: {
647 VkPhysicalDeviceMultiviewFeatures
*features
=
648 (VkPhysicalDeviceMultiviewFeatures
*) ext
;
649 features
->multiview
= false;
650 features
->multiviewGeometryShader
= false;
651 features
->multiviewTessellationShader
= false;
654 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SHADER_DRAW_PARAMETERS_FEATURES
: {
655 VkPhysicalDeviceShaderDrawParametersFeatures
*features
=
656 (VkPhysicalDeviceShaderDrawParametersFeatures
*) ext
;
657 features
->shaderDrawParameters
= false;
660 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PROTECTED_MEMORY_FEATURES
: {
661 VkPhysicalDeviceProtectedMemoryFeatures
*features
=
662 (VkPhysicalDeviceProtectedMemoryFeatures
*) ext
;
663 features
->protectedMemory
= false;
666 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_16BIT_STORAGE_FEATURES
: {
667 VkPhysicalDevice16BitStorageFeatures
*features
=
668 (VkPhysicalDevice16BitStorageFeatures
*) ext
;
669 features
->storageBuffer16BitAccess
= false;
670 features
->uniformAndStorageBuffer16BitAccess
= false;
671 features
->storagePushConstant16
= false;
672 features
->storageInputOutput16
= false;
675 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SAMPLER_YCBCR_CONVERSION_FEATURES
: {
676 VkPhysicalDeviceSamplerYcbcrConversionFeatures
*features
=
677 (VkPhysicalDeviceSamplerYcbcrConversionFeatures
*) ext
;
678 features
->samplerYcbcrConversion
= false;
681 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_DESCRIPTOR_INDEXING_FEATURES_EXT
: {
682 VkPhysicalDeviceDescriptorIndexingFeaturesEXT
*features
=
683 (VkPhysicalDeviceDescriptorIndexingFeaturesEXT
*) ext
;
684 features
->shaderInputAttachmentArrayDynamicIndexing
= false;
685 features
->shaderUniformTexelBufferArrayDynamicIndexing
= false;
686 features
->shaderStorageTexelBufferArrayDynamicIndexing
= false;
687 features
->shaderUniformBufferArrayNonUniformIndexing
= false;
688 features
->shaderSampledImageArrayNonUniformIndexing
= false;
689 features
->shaderStorageBufferArrayNonUniformIndexing
= false;
690 features
->shaderStorageImageArrayNonUniformIndexing
= false;
691 features
->shaderInputAttachmentArrayNonUniformIndexing
= false;
692 features
->shaderUniformTexelBufferArrayNonUniformIndexing
= false;
693 features
->shaderStorageTexelBufferArrayNonUniformIndexing
= false;
694 features
->descriptorBindingUniformBufferUpdateAfterBind
= false;
695 features
->descriptorBindingSampledImageUpdateAfterBind
= false;
696 features
->descriptorBindingStorageImageUpdateAfterBind
= false;
697 features
->descriptorBindingStorageBufferUpdateAfterBind
= false;
698 features
->descriptorBindingUniformTexelBufferUpdateAfterBind
= false;
699 features
->descriptorBindingStorageTexelBufferUpdateAfterBind
= false;
700 features
->descriptorBindingUpdateUnusedWhilePending
= false;
701 features
->descriptorBindingPartiallyBound
= false;
702 features
->descriptorBindingVariableDescriptorCount
= false;
703 features
->runtimeDescriptorArray
= false;
706 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_CONDITIONAL_RENDERING_FEATURES_EXT
: {
707 VkPhysicalDeviceConditionalRenderingFeaturesEXT
*features
=
708 (VkPhysicalDeviceConditionalRenderingFeaturesEXT
*) ext
;
709 features
->conditionalRendering
= false;
710 features
->inheritedConditionalRendering
= false;
713 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_TRANSFORM_FEEDBACK_FEATURES_EXT
: {
714 VkPhysicalDeviceTransformFeedbackFeaturesEXT
*features
=
715 (VkPhysicalDeviceTransformFeedbackFeaturesEXT
*) ext
;
716 features
->transformFeedback
= true;
717 features
->geometryStreams
= false;
724 return tu_GetPhysicalDeviceFeatures(physicalDevice
, &pFeatures
->features
);
728 tu_GetPhysicalDeviceProperties(VkPhysicalDevice physicalDevice
,
729 VkPhysicalDeviceProperties
*pProperties
)
731 TU_FROM_HANDLE(tu_physical_device
, pdevice
, physicalDevice
);
732 VkSampleCountFlags sample_counts
=
733 VK_SAMPLE_COUNT_1_BIT
| VK_SAMPLE_COUNT_2_BIT
| VK_SAMPLE_COUNT_4_BIT
;
735 /* I have no idea what the maximum size is, but the hardware supports very
736 * large numbers of descriptors (at least 2^16). This limit is based on
737 * CP_LOAD_STATE6, which has a 28-bit field for the DWORD offset, so that
738 * we don't have to think about what to do if that overflows, but really
739 * nothing is likely to get close to this.
741 const size_t max_descriptor_set_size
= (1 << 28) / A6XX_TEX_CONST_DWORDS
;
743 VkPhysicalDeviceLimits limits
= {
744 .maxImageDimension1D
= (1 << 14),
745 .maxImageDimension2D
= (1 << 14),
746 .maxImageDimension3D
= (1 << 11),
747 .maxImageDimensionCube
= (1 << 14),
748 .maxImageArrayLayers
= (1 << 11),
749 .maxTexelBufferElements
= 128 * 1024 * 1024,
750 .maxUniformBufferRange
= MAX_UNIFORM_BUFFER_RANGE
,
751 .maxStorageBufferRange
= MAX_STORAGE_BUFFER_RANGE
,
752 .maxPushConstantsSize
= MAX_PUSH_CONSTANTS_SIZE
,
753 .maxMemoryAllocationCount
= UINT32_MAX
,
754 .maxSamplerAllocationCount
= 64 * 1024,
755 .bufferImageGranularity
= 64, /* A cache line */
756 .sparseAddressSpaceSize
= 0xffffffffu
, /* buffer max size */
757 .maxBoundDescriptorSets
= MAX_SETS
,
758 .maxPerStageDescriptorSamplers
= max_descriptor_set_size
,
759 .maxPerStageDescriptorUniformBuffers
= max_descriptor_set_size
,
760 .maxPerStageDescriptorStorageBuffers
= max_descriptor_set_size
,
761 .maxPerStageDescriptorSampledImages
= max_descriptor_set_size
,
762 .maxPerStageDescriptorStorageImages
= max_descriptor_set_size
,
763 .maxPerStageDescriptorInputAttachments
= MAX_RTS
,
764 .maxPerStageResources
= max_descriptor_set_size
,
765 .maxDescriptorSetSamplers
= max_descriptor_set_size
,
766 .maxDescriptorSetUniformBuffers
= max_descriptor_set_size
,
767 .maxDescriptorSetUniformBuffersDynamic
= MAX_DYNAMIC_UNIFORM_BUFFERS
,
768 .maxDescriptorSetStorageBuffers
= max_descriptor_set_size
,
769 .maxDescriptorSetStorageBuffersDynamic
= MAX_DYNAMIC_STORAGE_BUFFERS
,
770 .maxDescriptorSetSampledImages
= max_descriptor_set_size
,
771 .maxDescriptorSetStorageImages
= max_descriptor_set_size
,
772 .maxDescriptorSetInputAttachments
= MAX_RTS
,
773 .maxVertexInputAttributes
= 32,
774 .maxVertexInputBindings
= 32,
775 .maxVertexInputAttributeOffset
= 4095,
776 .maxVertexInputBindingStride
= 2048,
777 .maxVertexOutputComponents
= 128,
778 .maxTessellationGenerationLevel
= 64,
779 .maxTessellationPatchSize
= 32,
780 .maxTessellationControlPerVertexInputComponents
= 128,
781 .maxTessellationControlPerVertexOutputComponents
= 128,
782 .maxTessellationControlPerPatchOutputComponents
= 120,
783 .maxTessellationControlTotalOutputComponents
= 4096,
784 .maxTessellationEvaluationInputComponents
= 128,
785 .maxTessellationEvaluationOutputComponents
= 128,
786 .maxGeometryShaderInvocations
= 32,
787 .maxGeometryInputComponents
= 64,
788 .maxGeometryOutputComponents
= 128,
789 .maxGeometryOutputVertices
= 256,
790 .maxGeometryTotalOutputComponents
= 1024,
791 .maxFragmentInputComponents
= 124,
792 .maxFragmentOutputAttachments
= 8,
793 .maxFragmentDualSrcAttachments
= 1,
794 .maxFragmentCombinedOutputResources
= 8,
795 .maxComputeSharedMemorySize
= 32768,
796 .maxComputeWorkGroupCount
= { 65535, 65535, 65535 },
797 .maxComputeWorkGroupInvocations
= 2048,
798 .maxComputeWorkGroupSize
= { 2048, 2048, 2048 },
799 .subPixelPrecisionBits
= 8,
800 .subTexelPrecisionBits
= 4 /* FIXME */,
801 .mipmapPrecisionBits
= 4 /* FIXME */,
802 .maxDrawIndexedIndexValue
= UINT32_MAX
,
803 .maxDrawIndirectCount
= UINT32_MAX
,
804 .maxSamplerLodBias
= 16,
805 .maxSamplerAnisotropy
= 16,
806 .maxViewports
= MAX_VIEWPORTS
,
807 .maxViewportDimensions
= { (1 << 14), (1 << 14) },
808 .viewportBoundsRange
= { INT16_MIN
, INT16_MAX
},
809 .viewportSubPixelBits
= 8,
810 .minMemoryMapAlignment
= 4096, /* A page */
811 .minTexelBufferOffsetAlignment
= 64,
812 .minUniformBufferOffsetAlignment
= 64,
813 .minStorageBufferOffsetAlignment
= 64,
814 .minTexelOffset
= -32,
815 .maxTexelOffset
= 31,
816 .minTexelGatherOffset
= -32,
817 .maxTexelGatherOffset
= 31,
818 .minInterpolationOffset
= -2,
819 .maxInterpolationOffset
= 2,
820 .subPixelInterpolationOffsetBits
= 8,
821 .maxFramebufferWidth
= (1 << 14),
822 .maxFramebufferHeight
= (1 << 14),
823 .maxFramebufferLayers
= (1 << 10),
824 .framebufferColorSampleCounts
= sample_counts
,
825 .framebufferDepthSampleCounts
= sample_counts
,
826 .framebufferStencilSampleCounts
= sample_counts
,
827 .framebufferNoAttachmentsSampleCounts
= sample_counts
,
828 .maxColorAttachments
= MAX_RTS
,
829 .sampledImageColorSampleCounts
= sample_counts
,
830 .sampledImageIntegerSampleCounts
= VK_SAMPLE_COUNT_1_BIT
,
831 .sampledImageDepthSampleCounts
= sample_counts
,
832 .sampledImageStencilSampleCounts
= sample_counts
,
833 .storageImageSampleCounts
= VK_SAMPLE_COUNT_1_BIT
,
834 .maxSampleMaskWords
= 1,
835 .timestampComputeAndGraphics
= true,
836 .timestampPeriod
= 1000000000.0 / 19200000.0, /* CP_ALWAYS_ON_COUNTER is fixed 19.2MHz */
837 .maxClipDistances
= 8,
838 .maxCullDistances
= 8,
839 .maxCombinedClipAndCullDistances
= 8,
840 .discreteQueuePriorities
= 1,
841 .pointSizeRange
= { 0.125, 255.875 },
842 .lineWidthRange
= { 0.0, 7.9921875 },
843 .pointSizeGranularity
= (1.0 / 8.0),
844 .lineWidthGranularity
= (1.0 / 128.0),
845 .strictLines
= false, /* FINISHME */
846 .standardSampleLocations
= true,
847 .optimalBufferCopyOffsetAlignment
= 128,
848 .optimalBufferCopyRowPitchAlignment
= 128,
849 .nonCoherentAtomSize
= 64,
852 *pProperties
= (VkPhysicalDeviceProperties
) {
853 .apiVersion
= tu_physical_device_api_version(pdevice
),
854 .driverVersion
= vk_get_driver_version(),
855 .vendorID
= 0, /* TODO */
857 .deviceType
= VK_PHYSICAL_DEVICE_TYPE_INTEGRATED_GPU
,
859 .sparseProperties
= { 0 },
862 strcpy(pProperties
->deviceName
, pdevice
->name
);
863 memcpy(pProperties
->pipelineCacheUUID
, pdevice
->cache_uuid
, VK_UUID_SIZE
);
867 tu_GetPhysicalDeviceProperties2(VkPhysicalDevice physicalDevice
,
868 VkPhysicalDeviceProperties2
*pProperties
)
870 TU_FROM_HANDLE(tu_physical_device
, pdevice
, physicalDevice
);
871 tu_GetPhysicalDeviceProperties(physicalDevice
, &pProperties
->properties
);
873 vk_foreach_struct(ext
, pProperties
->pNext
)
875 switch (ext
->sType
) {
876 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PUSH_DESCRIPTOR_PROPERTIES_KHR
: {
877 VkPhysicalDevicePushDescriptorPropertiesKHR
*properties
=
878 (VkPhysicalDevicePushDescriptorPropertiesKHR
*) ext
;
879 properties
->maxPushDescriptors
= MAX_PUSH_DESCRIPTORS
;
882 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_ID_PROPERTIES
: {
883 VkPhysicalDeviceIDProperties
*properties
=
884 (VkPhysicalDeviceIDProperties
*) ext
;
885 memcpy(properties
->driverUUID
, pdevice
->driver_uuid
, VK_UUID_SIZE
);
886 memcpy(properties
->deviceUUID
, pdevice
->device_uuid
, VK_UUID_SIZE
);
887 properties
->deviceLUIDValid
= false;
890 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_MULTIVIEW_PROPERTIES
: {
891 VkPhysicalDeviceMultiviewProperties
*properties
=
892 (VkPhysicalDeviceMultiviewProperties
*) ext
;
893 properties
->maxMultiviewViewCount
= MAX_VIEWS
;
894 properties
->maxMultiviewInstanceIndex
= INT_MAX
;
897 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_POINT_CLIPPING_PROPERTIES
: {
898 VkPhysicalDevicePointClippingProperties
*properties
=
899 (VkPhysicalDevicePointClippingProperties
*) ext
;
900 properties
->pointClippingBehavior
=
901 VK_POINT_CLIPPING_BEHAVIOR_ALL_CLIP_PLANES
;
904 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_MAINTENANCE_3_PROPERTIES
: {
905 VkPhysicalDeviceMaintenance3Properties
*properties
=
906 (VkPhysicalDeviceMaintenance3Properties
*) ext
;
907 /* Make sure everything is addressable by a signed 32-bit int, and
908 * our largest descriptors are 96 bytes. */
909 properties
->maxPerSetDescriptors
= (1ull << 31) / 96;
910 /* Our buffer size fields allow only this much */
911 properties
->maxMemoryAllocationSize
= 0xFFFFFFFFull
;
914 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_TRANSFORM_FEEDBACK_PROPERTIES_EXT
: {
915 VkPhysicalDeviceTransformFeedbackPropertiesEXT
*properties
=
916 (VkPhysicalDeviceTransformFeedbackPropertiesEXT
*)ext
;
918 properties
->maxTransformFeedbackStreams
= IR3_MAX_SO_STREAMS
;
919 properties
->maxTransformFeedbackBuffers
= IR3_MAX_SO_BUFFERS
;
920 properties
->maxTransformFeedbackBufferSize
= UINT32_MAX
;
921 properties
->maxTransformFeedbackStreamDataSize
= 512;
922 properties
->maxTransformFeedbackBufferDataSize
= 512;
923 properties
->maxTransformFeedbackBufferDataStride
= 512;
924 /* TODO: enable xfb query */
925 properties
->transformFeedbackQueries
= false;
926 properties
->transformFeedbackStreamsLinesTriangles
= false;
927 properties
->transformFeedbackRasterizationStreamSelect
= false;
928 properties
->transformFeedbackDraw
= true;
931 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SAMPLE_LOCATIONS_PROPERTIES_EXT
: {
932 VkPhysicalDeviceSampleLocationsPropertiesEXT
*properties
=
933 (VkPhysicalDeviceSampleLocationsPropertiesEXT
*)ext
;
934 properties
->sampleLocationSampleCounts
= 0;
935 if (pdevice
->supported_extensions
.EXT_sample_locations
) {
936 properties
->sampleLocationSampleCounts
=
937 VK_SAMPLE_COUNT_1_BIT
| VK_SAMPLE_COUNT_2_BIT
| VK_SAMPLE_COUNT_4_BIT
;
939 properties
->maxSampleLocationGridSize
= (VkExtent2D
) { 1 , 1 };
940 properties
->sampleLocationCoordinateRange
[0] = 0.0f
;
941 properties
->sampleLocationCoordinateRange
[1] = 0.9375f
;
942 properties
->sampleLocationSubPixelBits
= 4;
943 properties
->variableSampleLocations
= true;
953 static const VkQueueFamilyProperties tu_queue_family_properties
= {
955 VK_QUEUE_GRAPHICS_BIT
| VK_QUEUE_COMPUTE_BIT
| VK_QUEUE_TRANSFER_BIT
,
957 .timestampValidBits
= 48,
958 .minImageTransferGranularity
= { 1, 1, 1 },
962 tu_GetPhysicalDeviceQueueFamilyProperties(
963 VkPhysicalDevice physicalDevice
,
964 uint32_t *pQueueFamilyPropertyCount
,
965 VkQueueFamilyProperties
*pQueueFamilyProperties
)
967 VK_OUTARRAY_MAKE(out
, pQueueFamilyProperties
, pQueueFamilyPropertyCount
);
969 vk_outarray_append(&out
, p
) { *p
= tu_queue_family_properties
; }
973 tu_GetPhysicalDeviceQueueFamilyProperties2(
974 VkPhysicalDevice physicalDevice
,
975 uint32_t *pQueueFamilyPropertyCount
,
976 VkQueueFamilyProperties2
*pQueueFamilyProperties
)
978 VK_OUTARRAY_MAKE(out
, pQueueFamilyProperties
, pQueueFamilyPropertyCount
);
980 vk_outarray_append(&out
, p
)
982 p
->queueFamilyProperties
= tu_queue_family_properties
;
987 tu_get_system_heap_size()
992 uint64_t total_ram
= (uint64_t) info
.totalram
* (uint64_t) info
.mem_unit
;
994 /* We don't want to burn too much ram with the GPU. If the user has 4GiB
995 * or less, we use at most half. If they have more than 4GiB, we use 3/4.
997 uint64_t available_ram
;
998 if (total_ram
<= 4ull * 1024ull * 1024ull * 1024ull)
999 available_ram
= total_ram
/ 2;
1001 available_ram
= total_ram
* 3 / 4;
1003 return available_ram
;
1007 tu_GetPhysicalDeviceMemoryProperties(
1008 VkPhysicalDevice physicalDevice
,
1009 VkPhysicalDeviceMemoryProperties
*pMemoryProperties
)
1011 pMemoryProperties
->memoryHeapCount
= 1;
1012 pMemoryProperties
->memoryHeaps
[0].size
= tu_get_system_heap_size();
1013 pMemoryProperties
->memoryHeaps
[0].flags
= VK_MEMORY_HEAP_DEVICE_LOCAL_BIT
;
1015 pMemoryProperties
->memoryTypeCount
= 1;
1016 pMemoryProperties
->memoryTypes
[0].propertyFlags
=
1017 VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT
|
1018 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT
|
1019 VK_MEMORY_PROPERTY_HOST_COHERENT_BIT
;
1020 pMemoryProperties
->memoryTypes
[0].heapIndex
= 0;
1024 tu_GetPhysicalDeviceMemoryProperties2(
1025 VkPhysicalDevice physicalDevice
,
1026 VkPhysicalDeviceMemoryProperties2
*pMemoryProperties
)
1028 return tu_GetPhysicalDeviceMemoryProperties(
1029 physicalDevice
, &pMemoryProperties
->memoryProperties
);
1033 tu_queue_init(struct tu_device
*device
,
1034 struct tu_queue
*queue
,
1035 uint32_t queue_family_index
,
1037 VkDeviceQueueCreateFlags flags
)
1039 queue
->_loader_data
.loaderMagic
= ICD_LOADER_MAGIC
;
1040 queue
->device
= device
;
1041 queue
->queue_family_index
= queue_family_index
;
1042 queue
->queue_idx
= idx
;
1043 queue
->flags
= flags
;
1045 int ret
= tu_drm_submitqueue_new(device
, 0, &queue
->msm_queue_id
);
1047 return VK_ERROR_INITIALIZATION_FAILED
;
1049 tu_fence_init(&queue
->submit_fence
, false);
1055 tu_queue_finish(struct tu_queue
*queue
)
1057 tu_fence_finish(&queue
->submit_fence
);
1058 tu_drm_submitqueue_close(queue
->device
, queue
->msm_queue_id
);
1062 tu_get_device_extension_index(const char *name
)
1064 for (unsigned i
= 0; i
< TU_DEVICE_EXTENSION_COUNT
; ++i
) {
1065 if (strcmp(name
, tu_device_extensions
[i
].extensionName
) == 0)
1071 struct PACKED bcolor_entry
{
1083 uint32_t z24
; /* also s8? */
1084 uint16_t srgb
[4]; /* appears to duplicate fp16[], but clamped, used for srgb */
1086 } border_color
[] = {
1087 [VK_BORDER_COLOR_FLOAT_TRANSPARENT_BLACK
] = {},
1088 [VK_BORDER_COLOR_INT_TRANSPARENT_BLACK
] = {},
1089 [VK_BORDER_COLOR_FLOAT_OPAQUE_BLACK
] = {
1090 .fp32
[3] = 0x3f800000,
1098 .rgb10a2
= 0xc0000000,
1101 [VK_BORDER_COLOR_INT_OPAQUE_BLACK
] = {
1105 [VK_BORDER_COLOR_FLOAT_OPAQUE_WHITE
] = {
1106 .fp32
[0 ... 3] = 0x3f800000,
1107 .ui16
[0 ... 3] = 0xffff,
1108 .si16
[0 ... 3] = 0x7fff,
1109 .fp16
[0 ... 3] = 0x3c00,
1113 .ui8
[0 ... 3] = 0xff,
1114 .si8
[0 ... 3] = 0x7f,
1115 .rgb10a2
= 0xffffffff,
1117 .srgb
[0 ... 3] = 0x3c00,
1119 [VK_BORDER_COLOR_INT_OPAQUE_WHITE
] = {
1127 tu_CreateDevice(VkPhysicalDevice physicalDevice
,
1128 const VkDeviceCreateInfo
*pCreateInfo
,
1129 const VkAllocationCallbacks
*pAllocator
,
1132 TU_FROM_HANDLE(tu_physical_device
, physical_device
, physicalDevice
);
1134 struct tu_device
*device
;
1136 /* Check enabled features */
1137 if (pCreateInfo
->pEnabledFeatures
) {
1138 VkPhysicalDeviceFeatures supported_features
;
1139 tu_GetPhysicalDeviceFeatures(physicalDevice
, &supported_features
);
1140 VkBool32
*supported_feature
= (VkBool32
*) &supported_features
;
1141 VkBool32
*enabled_feature
= (VkBool32
*) pCreateInfo
->pEnabledFeatures
;
1142 unsigned num_features
=
1143 sizeof(VkPhysicalDeviceFeatures
) / sizeof(VkBool32
);
1144 for (uint32_t i
= 0; i
< num_features
; i
++) {
1145 if (enabled_feature
[i
] && !supported_feature
[i
])
1146 return vk_error(physical_device
->instance
,
1147 VK_ERROR_FEATURE_NOT_PRESENT
);
1151 device
= vk_zalloc2(&physical_device
->instance
->alloc
, pAllocator
,
1152 sizeof(*device
), 8, VK_SYSTEM_ALLOCATION_SCOPE_DEVICE
);
1154 return vk_error(physical_device
->instance
, VK_ERROR_OUT_OF_HOST_MEMORY
);
1156 device
->_loader_data
.loaderMagic
= ICD_LOADER_MAGIC
;
1157 device
->instance
= physical_device
->instance
;
1158 device
->physical_device
= physical_device
;
1161 device
->alloc
= *pAllocator
;
1163 device
->alloc
= physical_device
->instance
->alloc
;
1165 for (uint32_t i
= 0; i
< pCreateInfo
->enabledExtensionCount
; i
++) {
1166 const char *ext_name
= pCreateInfo
->ppEnabledExtensionNames
[i
];
1167 int index
= tu_get_device_extension_index(ext_name
);
1169 !physical_device
->supported_extensions
.extensions
[index
]) {
1170 vk_free(&device
->alloc
, device
);
1171 return vk_error(physical_device
->instance
,
1172 VK_ERROR_EXTENSION_NOT_PRESENT
);
1175 device
->enabled_extensions
.extensions
[index
] = true;
1178 for (unsigned i
= 0; i
< pCreateInfo
->queueCreateInfoCount
; i
++) {
1179 const VkDeviceQueueCreateInfo
*queue_create
=
1180 &pCreateInfo
->pQueueCreateInfos
[i
];
1181 uint32_t qfi
= queue_create
->queueFamilyIndex
;
1182 device
->queues
[qfi
] = vk_alloc(
1183 &device
->alloc
, queue_create
->queueCount
* sizeof(struct tu_queue
),
1184 8, VK_SYSTEM_ALLOCATION_SCOPE_DEVICE
);
1185 if (!device
->queues
[qfi
]) {
1186 result
= VK_ERROR_OUT_OF_HOST_MEMORY
;
1190 memset(device
->queues
[qfi
], 0,
1191 queue_create
->queueCount
* sizeof(struct tu_queue
));
1193 device
->queue_count
[qfi
] = queue_create
->queueCount
;
1195 for (unsigned q
= 0; q
< queue_create
->queueCount
; q
++) {
1196 result
= tu_queue_init(device
, &device
->queues
[qfi
][q
], qfi
, q
,
1197 queue_create
->flags
);
1198 if (result
!= VK_SUCCESS
)
1203 device
->compiler
= ir3_compiler_create(NULL
, physical_device
->gpu_id
);
1204 if (!device
->compiler
)
1207 #define VSC_DATA_SIZE(pitch) ((pitch) * 32 + 0x100) /* extra size to store VSC_SIZE */
1208 #define VSC_DATA2_SIZE(pitch) ((pitch) * 32)
1210 device
->vsc_data_pitch
= 0x440 * 4;
1211 device
->vsc_data2_pitch
= 0x1040 * 4;
1213 result
= tu_bo_init_new(device
, &device
->vsc_data
, VSC_DATA_SIZE(device
->vsc_data_pitch
));
1214 if (result
!= VK_SUCCESS
)
1217 result
= tu_bo_init_new(device
, &device
->vsc_data2
, VSC_DATA2_SIZE(device
->vsc_data2_pitch
));
1218 if (result
!= VK_SUCCESS
)
1219 goto fail_vsc_data2
;
1221 STATIC_ASSERT(sizeof(struct bcolor_entry
) == 128);
1222 result
= tu_bo_init_new(device
, &device
->border_color
, sizeof(border_color
));
1223 if (result
!= VK_SUCCESS
)
1224 goto fail_border_color
;
1226 result
= tu_bo_map(device
, &device
->border_color
);
1227 if (result
!= VK_SUCCESS
)
1228 goto fail_border_color_map
;
1230 memcpy(device
->border_color
.map
, border_color
, sizeof(border_color
));
1232 VkPipelineCacheCreateInfo ci
;
1233 ci
.sType
= VK_STRUCTURE_TYPE_PIPELINE_CACHE_CREATE_INFO
;
1236 ci
.pInitialData
= NULL
;
1237 ci
.initialDataSize
= 0;
1240 tu_CreatePipelineCache(tu_device_to_handle(device
), &ci
, NULL
, &pc
);
1241 if (result
!= VK_SUCCESS
)
1242 goto fail_pipeline_cache
;
1244 device
->mem_cache
= tu_pipeline_cache_from_handle(pc
);
1246 *pDevice
= tu_device_to_handle(device
);
1249 fail_pipeline_cache
:
1250 fail_border_color_map
:
1251 tu_bo_finish(device
, &device
->border_color
);
1254 tu_bo_finish(device
, &device
->vsc_data2
);
1257 tu_bo_finish(device
, &device
->vsc_data
);
1260 ralloc_free(device
->compiler
);
1263 for (unsigned i
= 0; i
< TU_MAX_QUEUE_FAMILIES
; i
++) {
1264 for (unsigned q
= 0; q
< device
->queue_count
[i
]; q
++)
1265 tu_queue_finish(&device
->queues
[i
][q
]);
1266 if (device
->queue_count
[i
])
1267 vk_free(&device
->alloc
, device
->queues
[i
]);
1270 vk_free(&device
->alloc
, device
);
1275 tu_DestroyDevice(VkDevice _device
, const VkAllocationCallbacks
*pAllocator
)
1277 TU_FROM_HANDLE(tu_device
, device
, _device
);
1282 tu_bo_finish(device
, &device
->vsc_data
);
1283 tu_bo_finish(device
, &device
->vsc_data2
);
1285 for (unsigned i
= 0; i
< TU_MAX_QUEUE_FAMILIES
; i
++) {
1286 for (unsigned q
= 0; q
< device
->queue_count
[i
]; q
++)
1287 tu_queue_finish(&device
->queues
[i
][q
]);
1288 if (device
->queue_count
[i
])
1289 vk_free(&device
->alloc
, device
->queues
[i
]);
1292 /* the compiler does not use pAllocator */
1293 ralloc_free(device
->compiler
);
1295 VkPipelineCache pc
= tu_pipeline_cache_to_handle(device
->mem_cache
);
1296 tu_DestroyPipelineCache(tu_device_to_handle(device
), pc
, NULL
);
1298 vk_free(&device
->alloc
, device
);
1302 tu_EnumerateInstanceLayerProperties(uint32_t *pPropertyCount
,
1303 VkLayerProperties
*pProperties
)
1305 *pPropertyCount
= 0;
1310 tu_EnumerateDeviceLayerProperties(VkPhysicalDevice physicalDevice
,
1311 uint32_t *pPropertyCount
,
1312 VkLayerProperties
*pProperties
)
1314 *pPropertyCount
= 0;
1319 tu_GetDeviceQueue2(VkDevice _device
,
1320 const VkDeviceQueueInfo2
*pQueueInfo
,
1323 TU_FROM_HANDLE(tu_device
, device
, _device
);
1324 struct tu_queue
*queue
;
1327 &device
->queues
[pQueueInfo
->queueFamilyIndex
][pQueueInfo
->queueIndex
];
1328 if (pQueueInfo
->flags
!= queue
->flags
) {
1329 /* From the Vulkan 1.1.70 spec:
1331 * "The queue returned by vkGetDeviceQueue2 must have the same
1332 * flags value from this structure as that used at device
1333 * creation time in a VkDeviceQueueCreateInfo instance. If no
1334 * matching flags were specified at device creation time then
1335 * pQueue will return VK_NULL_HANDLE."
1337 *pQueue
= VK_NULL_HANDLE
;
1341 *pQueue
= tu_queue_to_handle(queue
);
1345 tu_GetDeviceQueue(VkDevice _device
,
1346 uint32_t queueFamilyIndex
,
1347 uint32_t queueIndex
,
1350 const VkDeviceQueueInfo2 info
=
1351 (VkDeviceQueueInfo2
) { .sType
= VK_STRUCTURE_TYPE_DEVICE_QUEUE_INFO_2
,
1352 .queueFamilyIndex
= queueFamilyIndex
,
1353 .queueIndex
= queueIndex
};
1355 tu_GetDeviceQueue2(_device
, &info
, pQueue
);
1359 tu_QueueSubmit(VkQueue _queue
,
1360 uint32_t submitCount
,
1361 const VkSubmitInfo
*pSubmits
,
1364 TU_FROM_HANDLE(tu_queue
, queue
, _queue
);
1366 for (uint32_t i
= 0; i
< submitCount
; ++i
) {
1367 const VkSubmitInfo
*submit
= pSubmits
+ i
;
1368 const bool last_submit
= (i
== submitCount
- 1);
1369 struct tu_bo_list bo_list
;
1370 tu_bo_list_init(&bo_list
);
1372 uint32_t entry_count
= 0;
1373 for (uint32_t j
= 0; j
< submit
->commandBufferCount
; ++j
) {
1374 TU_FROM_HANDLE(tu_cmd_buffer
, cmdbuf
, submit
->pCommandBuffers
[j
]);
1375 entry_count
+= cmdbuf
->cs
.entry_count
;
1378 struct drm_msm_gem_submit_cmd cmds
[entry_count
];
1379 uint32_t entry_idx
= 0;
1380 for (uint32_t j
= 0; j
< submit
->commandBufferCount
; ++j
) {
1381 TU_FROM_HANDLE(tu_cmd_buffer
, cmdbuf
, submit
->pCommandBuffers
[j
]);
1382 struct tu_cs
*cs
= &cmdbuf
->cs
;
1383 for (unsigned i
= 0; i
< cs
->entry_count
; ++i
, ++entry_idx
) {
1384 cmds
[entry_idx
].type
= MSM_SUBMIT_CMD_BUF
;
1385 cmds
[entry_idx
].submit_idx
=
1386 tu_bo_list_add(&bo_list
, cs
->entries
[i
].bo
,
1387 MSM_SUBMIT_BO_READ
| MSM_SUBMIT_BO_DUMP
);
1388 cmds
[entry_idx
].submit_offset
= cs
->entries
[i
].offset
;
1389 cmds
[entry_idx
].size
= cs
->entries
[i
].size
;
1390 cmds
[entry_idx
].pad
= 0;
1391 cmds
[entry_idx
].nr_relocs
= 0;
1392 cmds
[entry_idx
].relocs
= 0;
1395 tu_bo_list_merge(&bo_list
, &cmdbuf
->bo_list
);
1398 uint32_t flags
= MSM_PIPE_3D0
;
1400 flags
|= MSM_SUBMIT_FENCE_FD_OUT
;
1403 struct drm_msm_gem_submit req
= {
1405 .queueid
= queue
->msm_queue_id
,
1406 .bos
= (uint64_t)(uintptr_t) bo_list
.bo_infos
,
1407 .nr_bos
= bo_list
.count
,
1408 .cmds
= (uint64_t)(uintptr_t)cmds
,
1409 .nr_cmds
= entry_count
,
1412 int ret
= drmCommandWriteRead(queue
->device
->physical_device
->local_fd
,
1416 fprintf(stderr
, "submit failed: %s\n", strerror(errno
));
1420 tu_bo_list_destroy(&bo_list
);
1423 /* no need to merge fences as queue execution is serialized */
1424 tu_fence_update_fd(&queue
->submit_fence
, req
.fence_fd
);
1428 if (_fence
!= VK_NULL_HANDLE
) {
1429 TU_FROM_HANDLE(tu_fence
, fence
, _fence
);
1430 tu_fence_copy(fence
, &queue
->submit_fence
);
1437 tu_QueueWaitIdle(VkQueue _queue
)
1439 TU_FROM_HANDLE(tu_queue
, queue
, _queue
);
1441 tu_fence_wait_idle(&queue
->submit_fence
);
1447 tu_DeviceWaitIdle(VkDevice _device
)
1449 TU_FROM_HANDLE(tu_device
, device
, _device
);
1451 for (unsigned i
= 0; i
< TU_MAX_QUEUE_FAMILIES
; i
++) {
1452 for (unsigned q
= 0; q
< device
->queue_count
[i
]; q
++) {
1453 tu_QueueWaitIdle(tu_queue_to_handle(&device
->queues
[i
][q
]));
1460 tu_ImportSemaphoreFdKHR(VkDevice _device
,
1461 const VkImportSemaphoreFdInfoKHR
*pImportSemaphoreFdInfo
)
1469 tu_GetSemaphoreFdKHR(VkDevice _device
,
1470 const VkSemaphoreGetFdInfoKHR
*pGetFdInfo
,
1479 tu_ImportFenceFdKHR(VkDevice _device
,
1480 const VkImportFenceFdInfoKHR
*pImportFenceFdInfo
)
1488 tu_GetFenceFdKHR(VkDevice _device
,
1489 const VkFenceGetFdInfoKHR
*pGetFdInfo
,
1498 tu_EnumerateInstanceExtensionProperties(const char *pLayerName
,
1499 uint32_t *pPropertyCount
,
1500 VkExtensionProperties
*pProperties
)
1502 VK_OUTARRAY_MAKE(out
, pProperties
, pPropertyCount
);
1504 /* We spport no lyaers */
1506 return vk_error(NULL
, VK_ERROR_LAYER_NOT_PRESENT
);
1508 for (int i
= 0; i
< TU_INSTANCE_EXTENSION_COUNT
; i
++) {
1509 if (tu_supported_instance_extensions
.extensions
[i
]) {
1510 vk_outarray_append(&out
, prop
) { *prop
= tu_instance_extensions
[i
]; }
1514 return vk_outarray_status(&out
);
1518 tu_EnumerateDeviceExtensionProperties(VkPhysicalDevice physicalDevice
,
1519 const char *pLayerName
,
1520 uint32_t *pPropertyCount
,
1521 VkExtensionProperties
*pProperties
)
1523 /* We spport no lyaers */
1524 TU_FROM_HANDLE(tu_physical_device
, device
, physicalDevice
);
1525 VK_OUTARRAY_MAKE(out
, pProperties
, pPropertyCount
);
1527 /* We spport no lyaers */
1529 return vk_error(NULL
, VK_ERROR_LAYER_NOT_PRESENT
);
1531 for (int i
= 0; i
< TU_DEVICE_EXTENSION_COUNT
; i
++) {
1532 if (device
->supported_extensions
.extensions
[i
]) {
1533 vk_outarray_append(&out
, prop
) { *prop
= tu_device_extensions
[i
]; }
1537 return vk_outarray_status(&out
);
1541 tu_GetInstanceProcAddr(VkInstance _instance
, const char *pName
)
1543 TU_FROM_HANDLE(tu_instance
, instance
, _instance
);
1545 return tu_lookup_entrypoint_checked(
1546 pName
, instance
? instance
->api_version
: 0,
1547 instance
? &instance
->enabled_extensions
: NULL
, NULL
);
1550 /* The loader wants us to expose a second GetInstanceProcAddr function
1551 * to work around certain LD_PRELOAD issues seen in apps.
1554 VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL
1555 vk_icdGetInstanceProcAddr(VkInstance instance
, const char *pName
);
1558 VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL
1559 vk_icdGetInstanceProcAddr(VkInstance instance
, const char *pName
)
1561 return tu_GetInstanceProcAddr(instance
, pName
);
1565 tu_GetDeviceProcAddr(VkDevice _device
, const char *pName
)
1567 TU_FROM_HANDLE(tu_device
, device
, _device
);
1569 return tu_lookup_entrypoint_checked(pName
, device
->instance
->api_version
,
1570 &device
->instance
->enabled_extensions
,
1571 &device
->enabled_extensions
);
1575 tu_alloc_memory(struct tu_device
*device
,
1576 const VkMemoryAllocateInfo
*pAllocateInfo
,
1577 const VkAllocationCallbacks
*pAllocator
,
1578 VkDeviceMemory
*pMem
)
1580 struct tu_device_memory
*mem
;
1583 assert(pAllocateInfo
->sType
== VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO
);
1585 if (pAllocateInfo
->allocationSize
== 0) {
1586 /* Apparently, this is allowed */
1587 *pMem
= VK_NULL_HANDLE
;
1591 mem
= vk_alloc2(&device
->alloc
, pAllocator
, sizeof(*mem
), 8,
1592 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
1594 return vk_error(device
->instance
, VK_ERROR_OUT_OF_HOST_MEMORY
);
1596 const VkImportMemoryFdInfoKHR
*fd_info
=
1597 vk_find_struct_const(pAllocateInfo
->pNext
, IMPORT_MEMORY_FD_INFO_KHR
);
1598 if (fd_info
&& !fd_info
->handleType
)
1602 assert(fd_info
->handleType
==
1603 VK_EXTERNAL_MEMORY_HANDLE_TYPE_OPAQUE_FD_BIT
||
1604 fd_info
->handleType
==
1605 VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_EXT
);
1608 * TODO Importing the same fd twice gives us the same handle without
1609 * reference counting. We need to maintain a per-instance handle-to-bo
1610 * table and add reference count to tu_bo.
1612 result
= tu_bo_init_dmabuf(device
, &mem
->bo
,
1613 pAllocateInfo
->allocationSize
, fd_info
->fd
);
1614 if (result
== VK_SUCCESS
) {
1615 /* take ownership and close the fd */
1620 tu_bo_init_new(device
, &mem
->bo
, pAllocateInfo
->allocationSize
);
1623 if (result
!= VK_SUCCESS
) {
1624 vk_free2(&device
->alloc
, pAllocator
, mem
);
1628 mem
->size
= pAllocateInfo
->allocationSize
;
1629 mem
->type_index
= pAllocateInfo
->memoryTypeIndex
;
1632 mem
->user_ptr
= NULL
;
1634 *pMem
= tu_device_memory_to_handle(mem
);
1640 tu_AllocateMemory(VkDevice _device
,
1641 const VkMemoryAllocateInfo
*pAllocateInfo
,
1642 const VkAllocationCallbacks
*pAllocator
,
1643 VkDeviceMemory
*pMem
)
1645 TU_FROM_HANDLE(tu_device
, device
, _device
);
1646 return tu_alloc_memory(device
, pAllocateInfo
, pAllocator
, pMem
);
1650 tu_FreeMemory(VkDevice _device
,
1651 VkDeviceMemory _mem
,
1652 const VkAllocationCallbacks
*pAllocator
)
1654 TU_FROM_HANDLE(tu_device
, device
, _device
);
1655 TU_FROM_HANDLE(tu_device_memory
, mem
, _mem
);
1660 tu_bo_finish(device
, &mem
->bo
);
1661 vk_free2(&device
->alloc
, pAllocator
, mem
);
1665 tu_MapMemory(VkDevice _device
,
1666 VkDeviceMemory _memory
,
1667 VkDeviceSize offset
,
1669 VkMemoryMapFlags flags
,
1672 TU_FROM_HANDLE(tu_device
, device
, _device
);
1673 TU_FROM_HANDLE(tu_device_memory
, mem
, _memory
);
1681 if (mem
->user_ptr
) {
1682 *ppData
= mem
->user_ptr
;
1683 } else if (!mem
->map
) {
1684 result
= tu_bo_map(device
, &mem
->bo
);
1685 if (result
!= VK_SUCCESS
)
1687 *ppData
= mem
->map
= mem
->bo
.map
;
1696 return vk_error(device
->instance
, VK_ERROR_MEMORY_MAP_FAILED
);
1700 tu_UnmapMemory(VkDevice _device
, VkDeviceMemory _memory
)
1702 /* I do not see any unmapping done by the freedreno Gallium driver. */
1706 tu_FlushMappedMemoryRanges(VkDevice _device
,
1707 uint32_t memoryRangeCount
,
1708 const VkMappedMemoryRange
*pMemoryRanges
)
1714 tu_InvalidateMappedMemoryRanges(VkDevice _device
,
1715 uint32_t memoryRangeCount
,
1716 const VkMappedMemoryRange
*pMemoryRanges
)
1722 tu_GetBufferMemoryRequirements(VkDevice _device
,
1724 VkMemoryRequirements
*pMemoryRequirements
)
1726 TU_FROM_HANDLE(tu_buffer
, buffer
, _buffer
);
1728 pMemoryRequirements
->memoryTypeBits
= 1;
1729 pMemoryRequirements
->alignment
= 64;
1730 pMemoryRequirements
->size
=
1731 align64(buffer
->size
, pMemoryRequirements
->alignment
);
1735 tu_GetBufferMemoryRequirements2(
1737 const VkBufferMemoryRequirementsInfo2
*pInfo
,
1738 VkMemoryRequirements2
*pMemoryRequirements
)
1740 tu_GetBufferMemoryRequirements(device
, pInfo
->buffer
,
1741 &pMemoryRequirements
->memoryRequirements
);
1745 tu_GetImageMemoryRequirements(VkDevice _device
,
1747 VkMemoryRequirements
*pMemoryRequirements
)
1749 TU_FROM_HANDLE(tu_image
, image
, _image
);
1751 pMemoryRequirements
->memoryTypeBits
= 1;
1752 pMemoryRequirements
->size
= image
->layout
.size
;
1753 pMemoryRequirements
->alignment
= image
->layout
.base_align
;
1757 tu_GetImageMemoryRequirements2(VkDevice device
,
1758 const VkImageMemoryRequirementsInfo2
*pInfo
,
1759 VkMemoryRequirements2
*pMemoryRequirements
)
1761 tu_GetImageMemoryRequirements(device
, pInfo
->image
,
1762 &pMemoryRequirements
->memoryRequirements
);
1766 tu_GetImageSparseMemoryRequirements(
1769 uint32_t *pSparseMemoryRequirementCount
,
1770 VkSparseImageMemoryRequirements
*pSparseMemoryRequirements
)
1776 tu_GetImageSparseMemoryRequirements2(
1778 const VkImageSparseMemoryRequirementsInfo2
*pInfo
,
1779 uint32_t *pSparseMemoryRequirementCount
,
1780 VkSparseImageMemoryRequirements2
*pSparseMemoryRequirements
)
1786 tu_GetDeviceMemoryCommitment(VkDevice device
,
1787 VkDeviceMemory memory
,
1788 VkDeviceSize
*pCommittedMemoryInBytes
)
1790 *pCommittedMemoryInBytes
= 0;
1794 tu_BindBufferMemory2(VkDevice device
,
1795 uint32_t bindInfoCount
,
1796 const VkBindBufferMemoryInfo
*pBindInfos
)
1798 for (uint32_t i
= 0; i
< bindInfoCount
; ++i
) {
1799 TU_FROM_HANDLE(tu_device_memory
, mem
, pBindInfos
[i
].memory
);
1800 TU_FROM_HANDLE(tu_buffer
, buffer
, pBindInfos
[i
].buffer
);
1803 buffer
->bo
= &mem
->bo
;
1804 buffer
->bo_offset
= pBindInfos
[i
].memoryOffset
;
1813 tu_BindBufferMemory(VkDevice device
,
1815 VkDeviceMemory memory
,
1816 VkDeviceSize memoryOffset
)
1818 const VkBindBufferMemoryInfo info
= {
1819 .sType
= VK_STRUCTURE_TYPE_BIND_BUFFER_MEMORY_INFO
,
1822 .memoryOffset
= memoryOffset
1825 return tu_BindBufferMemory2(device
, 1, &info
);
1829 tu_BindImageMemory2(VkDevice device
,
1830 uint32_t bindInfoCount
,
1831 const VkBindImageMemoryInfo
*pBindInfos
)
1833 for (uint32_t i
= 0; i
< bindInfoCount
; ++i
) {
1834 TU_FROM_HANDLE(tu_image
, image
, pBindInfos
[i
].image
);
1835 TU_FROM_HANDLE(tu_device_memory
, mem
, pBindInfos
[i
].memory
);
1838 image
->bo
= &mem
->bo
;
1839 image
->bo_offset
= pBindInfos
[i
].memoryOffset
;
1842 image
->bo_offset
= 0;
1850 tu_BindImageMemory(VkDevice device
,
1852 VkDeviceMemory memory
,
1853 VkDeviceSize memoryOffset
)
1855 const VkBindImageMemoryInfo info
= {
1856 .sType
= VK_STRUCTURE_TYPE_BIND_BUFFER_MEMORY_INFO
,
1859 .memoryOffset
= memoryOffset
1862 return tu_BindImageMemory2(device
, 1, &info
);
1866 tu_QueueBindSparse(VkQueue _queue
,
1867 uint32_t bindInfoCount
,
1868 const VkBindSparseInfo
*pBindInfo
,
1874 // Queue semaphore functions
1877 tu_CreateSemaphore(VkDevice _device
,
1878 const VkSemaphoreCreateInfo
*pCreateInfo
,
1879 const VkAllocationCallbacks
*pAllocator
,
1880 VkSemaphore
*pSemaphore
)
1882 TU_FROM_HANDLE(tu_device
, device
, _device
);
1884 struct tu_semaphore
*sem
=
1885 vk_alloc2(&device
->alloc
, pAllocator
, sizeof(*sem
), 8,
1886 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
1888 return vk_error(device
->instance
, VK_ERROR_OUT_OF_HOST_MEMORY
);
1890 *pSemaphore
= tu_semaphore_to_handle(sem
);
1895 tu_DestroySemaphore(VkDevice _device
,
1896 VkSemaphore _semaphore
,
1897 const VkAllocationCallbacks
*pAllocator
)
1899 TU_FROM_HANDLE(tu_device
, device
, _device
);
1900 TU_FROM_HANDLE(tu_semaphore
, sem
, _semaphore
);
1904 vk_free2(&device
->alloc
, pAllocator
, sem
);
1908 tu_CreateEvent(VkDevice _device
,
1909 const VkEventCreateInfo
*pCreateInfo
,
1910 const VkAllocationCallbacks
*pAllocator
,
1913 TU_FROM_HANDLE(tu_device
, device
, _device
);
1914 struct tu_event
*event
=
1915 vk_alloc2(&device
->alloc
, pAllocator
, sizeof(*event
), 8,
1916 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
1919 return vk_error(device
->instance
, VK_ERROR_OUT_OF_HOST_MEMORY
);
1921 VkResult result
= tu_bo_init_new(device
, &event
->bo
, 0x1000);
1922 if (result
!= VK_SUCCESS
)
1925 result
= tu_bo_map(device
, &event
->bo
);
1926 if (result
!= VK_SUCCESS
)
1929 *pEvent
= tu_event_to_handle(event
);
1934 tu_bo_finish(device
, &event
->bo
);
1936 vk_free2(&device
->alloc
, pAllocator
, event
);
1937 return vk_error(device
->instance
, VK_ERROR_OUT_OF_HOST_MEMORY
);
1941 tu_DestroyEvent(VkDevice _device
,
1943 const VkAllocationCallbacks
*pAllocator
)
1945 TU_FROM_HANDLE(tu_device
, device
, _device
);
1946 TU_FROM_HANDLE(tu_event
, event
, _event
);
1951 tu_bo_finish(device
, &event
->bo
);
1952 vk_free2(&device
->alloc
, pAllocator
, event
);
1956 tu_GetEventStatus(VkDevice _device
, VkEvent _event
)
1958 TU_FROM_HANDLE(tu_event
, event
, _event
);
1960 if (*(uint64_t*) event
->bo
.map
== 1)
1961 return VK_EVENT_SET
;
1962 return VK_EVENT_RESET
;
1966 tu_SetEvent(VkDevice _device
, VkEvent _event
)
1968 TU_FROM_HANDLE(tu_event
, event
, _event
);
1969 *(uint64_t*) event
->bo
.map
= 1;
1975 tu_ResetEvent(VkDevice _device
, VkEvent _event
)
1977 TU_FROM_HANDLE(tu_event
, event
, _event
);
1978 *(uint64_t*) event
->bo
.map
= 0;
1984 tu_CreateBuffer(VkDevice _device
,
1985 const VkBufferCreateInfo
*pCreateInfo
,
1986 const VkAllocationCallbacks
*pAllocator
,
1989 TU_FROM_HANDLE(tu_device
, device
, _device
);
1990 struct tu_buffer
*buffer
;
1992 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO
);
1994 buffer
= vk_alloc2(&device
->alloc
, pAllocator
, sizeof(*buffer
), 8,
1995 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
1997 return vk_error(device
->instance
, VK_ERROR_OUT_OF_HOST_MEMORY
);
1999 buffer
->size
= pCreateInfo
->size
;
2000 buffer
->usage
= pCreateInfo
->usage
;
2001 buffer
->flags
= pCreateInfo
->flags
;
2003 *pBuffer
= tu_buffer_to_handle(buffer
);
2009 tu_DestroyBuffer(VkDevice _device
,
2011 const VkAllocationCallbacks
*pAllocator
)
2013 TU_FROM_HANDLE(tu_device
, device
, _device
);
2014 TU_FROM_HANDLE(tu_buffer
, buffer
, _buffer
);
2019 vk_free2(&device
->alloc
, pAllocator
, buffer
);
2023 tu_CreateFramebuffer(VkDevice _device
,
2024 const VkFramebufferCreateInfo
*pCreateInfo
,
2025 const VkAllocationCallbacks
*pAllocator
,
2026 VkFramebuffer
*pFramebuffer
)
2028 TU_FROM_HANDLE(tu_device
, device
, _device
);
2029 struct tu_framebuffer
*framebuffer
;
2031 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO
);
2033 size_t size
= sizeof(*framebuffer
) + sizeof(struct tu_attachment_info
) *
2034 pCreateInfo
->attachmentCount
;
2035 framebuffer
= vk_alloc2(&device
->alloc
, pAllocator
, size
, 8,
2036 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
2037 if (framebuffer
== NULL
)
2038 return vk_error(device
->instance
, VK_ERROR_OUT_OF_HOST_MEMORY
);
2040 framebuffer
->attachment_count
= pCreateInfo
->attachmentCount
;
2041 framebuffer
->width
= pCreateInfo
->width
;
2042 framebuffer
->height
= pCreateInfo
->height
;
2043 framebuffer
->layers
= pCreateInfo
->layers
;
2044 for (uint32_t i
= 0; i
< pCreateInfo
->attachmentCount
; i
++) {
2045 VkImageView _iview
= pCreateInfo
->pAttachments
[i
];
2046 struct tu_image_view
*iview
= tu_image_view_from_handle(_iview
);
2047 framebuffer
->attachments
[i
].attachment
= iview
;
2050 *pFramebuffer
= tu_framebuffer_to_handle(framebuffer
);
2055 tu_DestroyFramebuffer(VkDevice _device
,
2057 const VkAllocationCallbacks
*pAllocator
)
2059 TU_FROM_HANDLE(tu_device
, device
, _device
);
2060 TU_FROM_HANDLE(tu_framebuffer
, fb
, _fb
);
2064 vk_free2(&device
->alloc
, pAllocator
, fb
);
2067 static enum a6xx_tex_clamp
2068 tu6_tex_wrap(VkSamplerAddressMode address_mode
)
2070 switch (address_mode
) {
2071 case VK_SAMPLER_ADDRESS_MODE_REPEAT
:
2072 return A6XX_TEX_REPEAT
;
2073 case VK_SAMPLER_ADDRESS_MODE_MIRRORED_REPEAT
:
2074 return A6XX_TEX_MIRROR_REPEAT
;
2075 case VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE
:
2076 return A6XX_TEX_CLAMP_TO_EDGE
;
2077 case VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_BORDER
:
2078 return A6XX_TEX_CLAMP_TO_BORDER
;
2079 case VK_SAMPLER_ADDRESS_MODE_MIRROR_CLAMP_TO_EDGE
:
2080 /* only works for PoT.. need to emulate otherwise! */
2081 return A6XX_TEX_MIRROR_CLAMP
;
2083 unreachable("illegal tex wrap mode");
2088 static enum a6xx_tex_filter
2089 tu6_tex_filter(VkFilter filter
, unsigned aniso
)
2092 case VK_FILTER_NEAREST
:
2093 return A6XX_TEX_NEAREST
;
2094 case VK_FILTER_LINEAR
:
2095 return aniso
? A6XX_TEX_ANISO
: A6XX_TEX_LINEAR
;
2096 case VK_FILTER_CUBIC_IMG
:
2098 unreachable("illegal texture filter");
2103 static inline enum adreno_compare_func
2104 tu6_compare_func(VkCompareOp op
)
2106 return (enum adreno_compare_func
) op
;
2110 tu_init_sampler(struct tu_device
*device
,
2111 struct tu_sampler
*sampler
,
2112 const VkSamplerCreateInfo
*pCreateInfo
)
2114 unsigned aniso
= pCreateInfo
->anisotropyEnable
?
2115 util_last_bit(MIN2((uint32_t)pCreateInfo
->maxAnisotropy
>> 1, 8)) : 0;
2116 bool miplinear
= (pCreateInfo
->mipmapMode
== VK_SAMPLER_MIPMAP_MODE_LINEAR
);
2118 sampler
->descriptor
[0] =
2119 COND(miplinear
, A6XX_TEX_SAMP_0_MIPFILTER_LINEAR_NEAR
) |
2120 A6XX_TEX_SAMP_0_XY_MAG(tu6_tex_filter(pCreateInfo
->magFilter
, aniso
)) |
2121 A6XX_TEX_SAMP_0_XY_MIN(tu6_tex_filter(pCreateInfo
->minFilter
, aniso
)) |
2122 A6XX_TEX_SAMP_0_ANISO(aniso
) |
2123 A6XX_TEX_SAMP_0_WRAP_S(tu6_tex_wrap(pCreateInfo
->addressModeU
)) |
2124 A6XX_TEX_SAMP_0_WRAP_T(tu6_tex_wrap(pCreateInfo
->addressModeV
)) |
2125 A6XX_TEX_SAMP_0_WRAP_R(tu6_tex_wrap(pCreateInfo
->addressModeW
)) |
2126 A6XX_TEX_SAMP_0_LOD_BIAS(pCreateInfo
->mipLodBias
);
2127 sampler
->descriptor
[1] =
2128 /* COND(!cso->seamless_cube_map, A6XX_TEX_SAMP_1_CUBEMAPSEAMLESSFILTOFF) | */
2129 COND(pCreateInfo
->unnormalizedCoordinates
, A6XX_TEX_SAMP_1_UNNORM_COORDS
) |
2130 A6XX_TEX_SAMP_1_MIN_LOD(pCreateInfo
->minLod
) |
2131 A6XX_TEX_SAMP_1_MAX_LOD(pCreateInfo
->maxLod
) |
2132 COND(pCreateInfo
->compareEnable
,
2133 A6XX_TEX_SAMP_1_COMPARE_FUNC(tu6_compare_func(pCreateInfo
->compareOp
)));
2134 /* This is an offset into the border_color BO, which we fill with all the
2135 * possible Vulkan border colors in the correct order, so we can just use
2136 * the Vulkan enum with no translation necessary.
2138 sampler
->descriptor
[2] =
2139 A6XX_TEX_SAMP_2_BCOLOR_OFFSET((unsigned) pCreateInfo
->borderColor
*
2140 sizeof(struct bcolor_entry
));
2141 sampler
->descriptor
[3] = 0;
2144 * A6XX_TEX_SAMP_1_MIPFILTER_LINEAR_FAR disables mipmapping, but vk has no NONE mipfilter?
2149 tu_CreateSampler(VkDevice _device
,
2150 const VkSamplerCreateInfo
*pCreateInfo
,
2151 const VkAllocationCallbacks
*pAllocator
,
2152 VkSampler
*pSampler
)
2154 TU_FROM_HANDLE(tu_device
, device
, _device
);
2155 struct tu_sampler
*sampler
;
2157 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO
);
2159 sampler
= vk_alloc2(&device
->alloc
, pAllocator
, sizeof(*sampler
), 8,
2160 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
2162 return vk_error(device
->instance
, VK_ERROR_OUT_OF_HOST_MEMORY
);
2164 tu_init_sampler(device
, sampler
, pCreateInfo
);
2165 *pSampler
= tu_sampler_to_handle(sampler
);
2171 tu_DestroySampler(VkDevice _device
,
2173 const VkAllocationCallbacks
*pAllocator
)
2175 TU_FROM_HANDLE(tu_device
, device
, _device
);
2176 TU_FROM_HANDLE(tu_sampler
, sampler
, _sampler
);
2180 vk_free2(&device
->alloc
, pAllocator
, sampler
);
2183 /* vk_icd.h does not declare this function, so we declare it here to
2184 * suppress Wmissing-prototypes.
2186 PUBLIC VKAPI_ATTR VkResult VKAPI_CALL
2187 vk_icdNegotiateLoaderICDInterfaceVersion(uint32_t *pSupportedVersion
);
2189 PUBLIC VKAPI_ATTR VkResult VKAPI_CALL
2190 vk_icdNegotiateLoaderICDInterfaceVersion(uint32_t *pSupportedVersion
)
2192 /* For the full details on loader interface versioning, see
2193 * <https://github.com/KhronosGroup/Vulkan-LoaderAndValidationLayers/blob/master/loader/LoaderAndLayerInterface.md>.
2194 * What follows is a condensed summary, to help you navigate the large and
2195 * confusing official doc.
2197 * - Loader interface v0 is incompatible with later versions. We don't
2200 * - In loader interface v1:
2201 * - The first ICD entrypoint called by the loader is
2202 * vk_icdGetInstanceProcAddr(). The ICD must statically expose this
2204 * - The ICD must statically expose no other Vulkan symbol unless it
2205 * is linked with -Bsymbolic.
2206 * - Each dispatchable Vulkan handle created by the ICD must be
2207 * a pointer to a struct whose first member is VK_LOADER_DATA. The
2208 * ICD must initialize VK_LOADER_DATA.loadMagic to
2210 * - The loader implements vkCreate{PLATFORM}SurfaceKHR() and
2211 * vkDestroySurfaceKHR(). The ICD must be capable of working with
2212 * such loader-managed surfaces.
2214 * - Loader interface v2 differs from v1 in:
2215 * - The first ICD entrypoint called by the loader is
2216 * vk_icdNegotiateLoaderICDInterfaceVersion(). The ICD must
2217 * statically expose this entrypoint.
2219 * - Loader interface v3 differs from v2 in:
2220 * - The ICD must implement vkCreate{PLATFORM}SurfaceKHR(),
2221 * vkDestroySurfaceKHR(), and other API which uses VKSurfaceKHR,
2222 * because the loader no longer does so.
2224 *pSupportedVersion
= MIN2(*pSupportedVersion
, 3u);
2229 tu_GetMemoryFdKHR(VkDevice _device
,
2230 const VkMemoryGetFdInfoKHR
*pGetFdInfo
,
2233 TU_FROM_HANDLE(tu_device
, device
, _device
);
2234 TU_FROM_HANDLE(tu_device_memory
, memory
, pGetFdInfo
->memory
);
2236 assert(pGetFdInfo
->sType
== VK_STRUCTURE_TYPE_MEMORY_GET_FD_INFO_KHR
);
2238 /* At the moment, we support only the below handle types. */
2239 assert(pGetFdInfo
->handleType
==
2240 VK_EXTERNAL_MEMORY_HANDLE_TYPE_OPAQUE_FD_BIT
||
2241 pGetFdInfo
->handleType
==
2242 VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_EXT
);
2244 int prime_fd
= tu_bo_export_dmabuf(device
, &memory
->bo
);
2246 return vk_error(device
->instance
, VK_ERROR_OUT_OF_DEVICE_MEMORY
);
2253 tu_GetMemoryFdPropertiesKHR(VkDevice _device
,
2254 VkExternalMemoryHandleTypeFlagBits handleType
,
2256 VkMemoryFdPropertiesKHR
*pMemoryFdProperties
)
2258 assert(handleType
== VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_EXT
);
2259 pMemoryFdProperties
->memoryTypeBits
= 1;
2264 tu_GetPhysicalDeviceExternalSemaphoreProperties(
2265 VkPhysicalDevice physicalDevice
,
2266 const VkPhysicalDeviceExternalSemaphoreInfo
*pExternalSemaphoreInfo
,
2267 VkExternalSemaphoreProperties
*pExternalSemaphoreProperties
)
2269 pExternalSemaphoreProperties
->exportFromImportedHandleTypes
= 0;
2270 pExternalSemaphoreProperties
->compatibleHandleTypes
= 0;
2271 pExternalSemaphoreProperties
->externalSemaphoreFeatures
= 0;
2275 tu_GetPhysicalDeviceExternalFenceProperties(
2276 VkPhysicalDevice physicalDevice
,
2277 const VkPhysicalDeviceExternalFenceInfo
*pExternalFenceInfo
,
2278 VkExternalFenceProperties
*pExternalFenceProperties
)
2280 pExternalFenceProperties
->exportFromImportedHandleTypes
= 0;
2281 pExternalFenceProperties
->compatibleHandleTypes
= 0;
2282 pExternalFenceProperties
->externalFenceFeatures
= 0;
2286 tu_CreateDebugReportCallbackEXT(
2287 VkInstance _instance
,
2288 const VkDebugReportCallbackCreateInfoEXT
*pCreateInfo
,
2289 const VkAllocationCallbacks
*pAllocator
,
2290 VkDebugReportCallbackEXT
*pCallback
)
2292 TU_FROM_HANDLE(tu_instance
, instance
, _instance
);
2293 return vk_create_debug_report_callback(&instance
->debug_report_callbacks
,
2294 pCreateInfo
, pAllocator
,
2295 &instance
->alloc
, pCallback
);
2299 tu_DestroyDebugReportCallbackEXT(VkInstance _instance
,
2300 VkDebugReportCallbackEXT _callback
,
2301 const VkAllocationCallbacks
*pAllocator
)
2303 TU_FROM_HANDLE(tu_instance
, instance
, _instance
);
2304 vk_destroy_debug_report_callback(&instance
->debug_report_callbacks
,
2305 _callback
, pAllocator
, &instance
->alloc
);
2309 tu_DebugReportMessageEXT(VkInstance _instance
,
2310 VkDebugReportFlagsEXT flags
,
2311 VkDebugReportObjectTypeEXT objectType
,
2314 int32_t messageCode
,
2315 const char *pLayerPrefix
,
2316 const char *pMessage
)
2318 TU_FROM_HANDLE(tu_instance
, instance
, _instance
);
2319 vk_debug_report(&instance
->debug_report_callbacks
, flags
, objectType
,
2320 object
, location
, messageCode
, pLayerPrefix
, pMessage
);
2324 tu_GetDeviceGroupPeerMemoryFeatures(
2327 uint32_t localDeviceIndex
,
2328 uint32_t remoteDeviceIndex
,
2329 VkPeerMemoryFeatureFlags
*pPeerMemoryFeatures
)
2331 assert(localDeviceIndex
== remoteDeviceIndex
);
2333 *pPeerMemoryFeatures
= VK_PEER_MEMORY_FEATURE_COPY_SRC_BIT
|
2334 VK_PEER_MEMORY_FEATURE_COPY_DST_BIT
|
2335 VK_PEER_MEMORY_FEATURE_GENERIC_SRC_BIT
|
2336 VK_PEER_MEMORY_FEATURE_GENERIC_DST_BIT
;
2339 void tu_GetPhysicalDeviceMultisamplePropertiesEXT(
2340 VkPhysicalDevice physicalDevice
,
2341 VkSampleCountFlagBits samples
,
2342 VkMultisamplePropertiesEXT
* pMultisampleProperties
)
2344 TU_FROM_HANDLE(tu_physical_device
, pdevice
, physicalDevice
);
2346 if (samples
<= VK_SAMPLE_COUNT_4_BIT
&& pdevice
->supported_extensions
.EXT_sample_locations
)
2347 pMultisampleProperties
->maxSampleLocationGridSize
= (VkExtent2D
){ 1, 1 };
2349 pMultisampleProperties
->maxSampleLocationGridSize
= (VkExtent2D
){ 0, 0 };