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
= VK_SAMPLE_COUNT_1_BIT
|
733 VK_SAMPLE_COUNT_2_BIT
| VK_SAMPLE_COUNT_4_BIT
| VK_SAMPLE_COUNT_8_BIT
;
735 /* make sure that the entire descriptor set is addressable with a signed
736 * 32-bit int. So the sum of all limits scaled by descriptor size has to
737 * be at most 2 GiB. the combined image & samples object count as one of
738 * both. This limit is for the pipeline layout, not for the set layout, but
739 * there is no set limit, so we just set a pipeline limit. I don't think
740 * any app is going to hit this soon. */
741 size_t max_descriptor_set_size
=
742 ((1ull << 31) - 16 * MAX_DYNAMIC_BUFFERS
) /
743 (32 /* uniform buffer, 32 due to potential space wasted on alignment */ +
744 32 /* storage buffer, 32 due to potential space wasted on alignment */ +
745 32 /* sampler, largest when combined with image */ +
746 64 /* sampled image */ + 64 /* storage image */);
748 VkPhysicalDeviceLimits limits
= {
749 .maxImageDimension1D
= (1 << 14),
750 .maxImageDimension2D
= (1 << 14),
751 .maxImageDimension3D
= (1 << 11),
752 .maxImageDimensionCube
= (1 << 14),
753 .maxImageArrayLayers
= (1 << 11),
754 .maxTexelBufferElements
= 128 * 1024 * 1024,
755 .maxUniformBufferRange
= UINT32_MAX
,
756 .maxStorageBufferRange
= MAX_STORAGE_BUFFER_RANGE
,
757 .maxPushConstantsSize
= MAX_PUSH_CONSTANTS_SIZE
,
758 .maxMemoryAllocationCount
= UINT32_MAX
,
759 .maxSamplerAllocationCount
= 64 * 1024,
760 .bufferImageGranularity
= 64, /* A cache line */
761 .sparseAddressSpaceSize
= 0xffffffffu
, /* buffer max size */
762 .maxBoundDescriptorSets
= MAX_SETS
,
763 .maxPerStageDescriptorSamplers
= max_descriptor_set_size
,
764 .maxPerStageDescriptorUniformBuffers
= max_descriptor_set_size
,
765 .maxPerStageDescriptorStorageBuffers
= max_descriptor_set_size
,
766 .maxPerStageDescriptorSampledImages
= max_descriptor_set_size
,
767 .maxPerStageDescriptorStorageImages
= max_descriptor_set_size
,
768 .maxPerStageDescriptorInputAttachments
= max_descriptor_set_size
,
769 .maxPerStageResources
= max_descriptor_set_size
,
770 .maxDescriptorSetSamplers
= max_descriptor_set_size
,
771 .maxDescriptorSetUniformBuffers
= max_descriptor_set_size
,
772 .maxDescriptorSetUniformBuffersDynamic
= MAX_DYNAMIC_UNIFORM_BUFFERS
,
773 .maxDescriptorSetStorageBuffers
= max_descriptor_set_size
,
774 .maxDescriptorSetStorageBuffersDynamic
= MAX_DYNAMIC_STORAGE_BUFFERS
,
775 .maxDescriptorSetSampledImages
= max_descriptor_set_size
,
776 .maxDescriptorSetStorageImages
= max_descriptor_set_size
,
777 .maxDescriptorSetInputAttachments
= max_descriptor_set_size
,
778 .maxVertexInputAttributes
= 32,
779 .maxVertexInputBindings
= 32,
780 .maxVertexInputAttributeOffset
= 4095,
781 .maxVertexInputBindingStride
= 2048,
782 .maxVertexOutputComponents
= 128,
783 .maxTessellationGenerationLevel
= 64,
784 .maxTessellationPatchSize
= 32,
785 .maxTessellationControlPerVertexInputComponents
= 128,
786 .maxTessellationControlPerVertexOutputComponents
= 128,
787 .maxTessellationControlPerPatchOutputComponents
= 120,
788 .maxTessellationControlTotalOutputComponents
= 4096,
789 .maxTessellationEvaluationInputComponents
= 128,
790 .maxTessellationEvaluationOutputComponents
= 128,
791 .maxGeometryShaderInvocations
= 32,
792 .maxGeometryInputComponents
= 64,
793 .maxGeometryOutputComponents
= 128,
794 .maxGeometryOutputVertices
= 256,
795 .maxGeometryTotalOutputComponents
= 1024,
796 .maxFragmentInputComponents
= 128,
797 .maxFragmentOutputAttachments
= 8,
798 .maxFragmentDualSrcAttachments
= 1,
799 .maxFragmentCombinedOutputResources
= 8,
800 .maxComputeSharedMemorySize
= 32768,
801 .maxComputeWorkGroupCount
= { 65535, 65535, 65535 },
802 .maxComputeWorkGroupInvocations
= 2048,
803 .maxComputeWorkGroupSize
= { 2048, 2048, 2048 },
804 .subPixelPrecisionBits
= 8,
805 .subTexelPrecisionBits
= 4 /* FIXME */,
806 .mipmapPrecisionBits
= 4 /* FIXME */,
807 .maxDrawIndexedIndexValue
= UINT32_MAX
,
808 .maxDrawIndirectCount
= UINT32_MAX
,
809 .maxSamplerLodBias
= 16,
810 .maxSamplerAnisotropy
= 16,
811 .maxViewports
= MAX_VIEWPORTS
,
812 .maxViewportDimensions
= { (1 << 14), (1 << 14) },
813 .viewportBoundsRange
= { INT16_MIN
, INT16_MAX
},
814 .viewportSubPixelBits
= 8,
815 .minMemoryMapAlignment
= 4096, /* A page */
816 .minTexelBufferOffsetAlignment
= 64,
817 .minUniformBufferOffsetAlignment
= 4,
818 .minStorageBufferOffsetAlignment
= 4,
819 .minTexelOffset
= -32,
820 .maxTexelOffset
= 31,
821 .minTexelGatherOffset
= -32,
822 .maxTexelGatherOffset
= 31,
823 .minInterpolationOffset
= -2,
824 .maxInterpolationOffset
= 2,
825 .subPixelInterpolationOffsetBits
= 8,
826 .maxFramebufferWidth
= (1 << 14),
827 .maxFramebufferHeight
= (1 << 14),
828 .maxFramebufferLayers
= (1 << 10),
829 .framebufferColorSampleCounts
= sample_counts
,
830 .framebufferDepthSampleCounts
= sample_counts
,
831 .framebufferStencilSampleCounts
= sample_counts
,
832 .framebufferNoAttachmentsSampleCounts
= sample_counts
,
833 .maxColorAttachments
= MAX_RTS
,
834 .sampledImageColorSampleCounts
= sample_counts
,
835 .sampledImageIntegerSampleCounts
= VK_SAMPLE_COUNT_1_BIT
,
836 .sampledImageDepthSampleCounts
= sample_counts
,
837 .sampledImageStencilSampleCounts
= sample_counts
,
838 .storageImageSampleCounts
= VK_SAMPLE_COUNT_1_BIT
,
839 .maxSampleMaskWords
= 1,
840 .timestampComputeAndGraphics
= true,
841 .timestampPeriod
= 1000000000.0 / 19200000.0, /* CP_ALWAYS_ON_COUNTER is fixed 19.2MHz */
842 .maxClipDistances
= 8,
843 .maxCullDistances
= 8,
844 .maxCombinedClipAndCullDistances
= 8,
845 .discreteQueuePriorities
= 1,
846 .pointSizeRange
= { 0.125, 255.875 },
847 .lineWidthRange
= { 0.0, 7.9921875 },
848 .pointSizeGranularity
= (1.0 / 8.0),
849 .lineWidthGranularity
= (1.0 / 128.0),
850 .strictLines
= false, /* FINISHME */
851 .standardSampleLocations
= true,
852 .optimalBufferCopyOffsetAlignment
= 128,
853 .optimalBufferCopyRowPitchAlignment
= 128,
854 .nonCoherentAtomSize
= 64,
857 *pProperties
= (VkPhysicalDeviceProperties
) {
858 .apiVersion
= tu_physical_device_api_version(pdevice
),
859 .driverVersion
= vk_get_driver_version(),
860 .vendorID
= 0, /* TODO */
862 .deviceType
= VK_PHYSICAL_DEVICE_TYPE_INTEGRATED_GPU
,
864 .sparseProperties
= { 0 },
867 strcpy(pProperties
->deviceName
, pdevice
->name
);
868 memcpy(pProperties
->pipelineCacheUUID
, pdevice
->cache_uuid
, VK_UUID_SIZE
);
872 tu_GetPhysicalDeviceProperties2(VkPhysicalDevice physicalDevice
,
873 VkPhysicalDeviceProperties2
*pProperties
)
875 TU_FROM_HANDLE(tu_physical_device
, pdevice
, physicalDevice
);
876 tu_GetPhysicalDeviceProperties(physicalDevice
, &pProperties
->properties
);
878 vk_foreach_struct(ext
, pProperties
->pNext
)
880 switch (ext
->sType
) {
881 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PUSH_DESCRIPTOR_PROPERTIES_KHR
: {
882 VkPhysicalDevicePushDescriptorPropertiesKHR
*properties
=
883 (VkPhysicalDevicePushDescriptorPropertiesKHR
*) ext
;
884 properties
->maxPushDescriptors
= MAX_PUSH_DESCRIPTORS
;
887 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_ID_PROPERTIES
: {
888 VkPhysicalDeviceIDProperties
*properties
=
889 (VkPhysicalDeviceIDProperties
*) ext
;
890 memcpy(properties
->driverUUID
, pdevice
->driver_uuid
, VK_UUID_SIZE
);
891 memcpy(properties
->deviceUUID
, pdevice
->device_uuid
, VK_UUID_SIZE
);
892 properties
->deviceLUIDValid
= false;
895 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_MULTIVIEW_PROPERTIES
: {
896 VkPhysicalDeviceMultiviewProperties
*properties
=
897 (VkPhysicalDeviceMultiviewProperties
*) ext
;
898 properties
->maxMultiviewViewCount
= MAX_VIEWS
;
899 properties
->maxMultiviewInstanceIndex
= INT_MAX
;
902 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_POINT_CLIPPING_PROPERTIES
: {
903 VkPhysicalDevicePointClippingProperties
*properties
=
904 (VkPhysicalDevicePointClippingProperties
*) ext
;
905 properties
->pointClippingBehavior
=
906 VK_POINT_CLIPPING_BEHAVIOR_ALL_CLIP_PLANES
;
909 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_MAINTENANCE_3_PROPERTIES
: {
910 VkPhysicalDeviceMaintenance3Properties
*properties
=
911 (VkPhysicalDeviceMaintenance3Properties
*) ext
;
912 /* Make sure everything is addressable by a signed 32-bit int, and
913 * our largest descriptors are 96 bytes. */
914 properties
->maxPerSetDescriptors
= (1ull << 31) / 96;
915 /* Our buffer size fields allow only this much */
916 properties
->maxMemoryAllocationSize
= 0xFFFFFFFFull
;
919 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_TRANSFORM_FEEDBACK_PROPERTIES_EXT
: {
920 VkPhysicalDeviceTransformFeedbackPropertiesEXT
*properties
=
921 (VkPhysicalDeviceTransformFeedbackPropertiesEXT
*)ext
;
923 properties
->maxTransformFeedbackStreams
= IR3_MAX_SO_STREAMS
;
924 properties
->maxTransformFeedbackBuffers
= IR3_MAX_SO_BUFFERS
;
925 properties
->maxTransformFeedbackBufferSize
= UINT32_MAX
;
926 properties
->maxTransformFeedbackStreamDataSize
= 512;
927 properties
->maxTransformFeedbackBufferDataSize
= 512;
928 properties
->maxTransformFeedbackBufferDataStride
= 512;
929 /* TODO: enable xfb query */
930 properties
->transformFeedbackQueries
= false;
931 properties
->transformFeedbackStreamsLinesTriangles
= false;
932 properties
->transformFeedbackRasterizationStreamSelect
= false;
933 properties
->transformFeedbackDraw
= true;
942 static const VkQueueFamilyProperties tu_queue_family_properties
= {
944 VK_QUEUE_GRAPHICS_BIT
| VK_QUEUE_COMPUTE_BIT
| VK_QUEUE_TRANSFER_BIT
,
946 .timestampValidBits
= 48,
947 .minImageTransferGranularity
= { 1, 1, 1 },
951 tu_GetPhysicalDeviceQueueFamilyProperties(
952 VkPhysicalDevice physicalDevice
,
953 uint32_t *pQueueFamilyPropertyCount
,
954 VkQueueFamilyProperties
*pQueueFamilyProperties
)
956 VK_OUTARRAY_MAKE(out
, pQueueFamilyProperties
, pQueueFamilyPropertyCount
);
958 vk_outarray_append(&out
, p
) { *p
= tu_queue_family_properties
; }
962 tu_GetPhysicalDeviceQueueFamilyProperties2(
963 VkPhysicalDevice physicalDevice
,
964 uint32_t *pQueueFamilyPropertyCount
,
965 VkQueueFamilyProperties2
*pQueueFamilyProperties
)
967 VK_OUTARRAY_MAKE(out
, pQueueFamilyProperties
, pQueueFamilyPropertyCount
);
969 vk_outarray_append(&out
, p
)
971 p
->queueFamilyProperties
= tu_queue_family_properties
;
976 tu_get_system_heap_size()
981 uint64_t total_ram
= (uint64_t) info
.totalram
* (uint64_t) info
.mem_unit
;
983 /* We don't want to burn too much ram with the GPU. If the user has 4GiB
984 * or less, we use at most half. If they have more than 4GiB, we use 3/4.
986 uint64_t available_ram
;
987 if (total_ram
<= 4ull * 1024ull * 1024ull * 1024ull)
988 available_ram
= total_ram
/ 2;
990 available_ram
= total_ram
* 3 / 4;
992 return available_ram
;
996 tu_GetPhysicalDeviceMemoryProperties(
997 VkPhysicalDevice physicalDevice
,
998 VkPhysicalDeviceMemoryProperties
*pMemoryProperties
)
1000 pMemoryProperties
->memoryHeapCount
= 1;
1001 pMemoryProperties
->memoryHeaps
[0].size
= tu_get_system_heap_size();
1002 pMemoryProperties
->memoryHeaps
[0].flags
= VK_MEMORY_HEAP_DEVICE_LOCAL_BIT
;
1004 pMemoryProperties
->memoryTypeCount
= 1;
1005 pMemoryProperties
->memoryTypes
[0].propertyFlags
=
1006 VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT
|
1007 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT
|
1008 VK_MEMORY_PROPERTY_HOST_COHERENT_BIT
;
1009 pMemoryProperties
->memoryTypes
[0].heapIndex
= 0;
1013 tu_GetPhysicalDeviceMemoryProperties2(
1014 VkPhysicalDevice physicalDevice
,
1015 VkPhysicalDeviceMemoryProperties2
*pMemoryProperties
)
1017 return tu_GetPhysicalDeviceMemoryProperties(
1018 physicalDevice
, &pMemoryProperties
->memoryProperties
);
1022 tu_queue_init(struct tu_device
*device
,
1023 struct tu_queue
*queue
,
1024 uint32_t queue_family_index
,
1026 VkDeviceQueueCreateFlags flags
)
1028 queue
->_loader_data
.loaderMagic
= ICD_LOADER_MAGIC
;
1029 queue
->device
= device
;
1030 queue
->queue_family_index
= queue_family_index
;
1031 queue
->queue_idx
= idx
;
1032 queue
->flags
= flags
;
1034 int ret
= tu_drm_submitqueue_new(device
, 0, &queue
->msm_queue_id
);
1036 return VK_ERROR_INITIALIZATION_FAILED
;
1038 tu_fence_init(&queue
->submit_fence
, false);
1044 tu_queue_finish(struct tu_queue
*queue
)
1046 tu_fence_finish(&queue
->submit_fence
);
1047 tu_drm_submitqueue_close(queue
->device
, queue
->msm_queue_id
);
1051 tu_get_device_extension_index(const char *name
)
1053 for (unsigned i
= 0; i
< TU_DEVICE_EXTENSION_COUNT
; ++i
) {
1054 if (strcmp(name
, tu_device_extensions
[i
].extensionName
) == 0)
1060 struct PACKED bcolor_entry
{
1072 uint32_t z24
; /* also s8? */
1073 uint16_t srgb
[4]; /* appears to duplicate fp16[], but clamped, used for srgb */
1075 } border_color
[] = {
1076 [VK_BORDER_COLOR_FLOAT_TRANSPARENT_BLACK
] = {},
1077 [VK_BORDER_COLOR_INT_TRANSPARENT_BLACK
] = {},
1078 [VK_BORDER_COLOR_FLOAT_OPAQUE_BLACK
] = {
1079 .fp32
[3] = 0x3f800000,
1087 .rgb10a2
= 0xc0000000,
1090 [VK_BORDER_COLOR_INT_OPAQUE_BLACK
] = {
1094 [VK_BORDER_COLOR_FLOAT_OPAQUE_WHITE
] = {
1095 .fp32
[0 ... 3] = 0x3f800000,
1096 .ui16
[0 ... 3] = 0xffff,
1097 .si16
[0 ... 3] = 0x7fff,
1098 .fp16
[0 ... 3] = 0x3c00,
1102 .ui8
[0 ... 3] = 0xff,
1103 .si8
[0 ... 3] = 0x7f,
1104 .rgb10a2
= 0xffffffff,
1106 .srgb
[0 ... 3] = 0x3c00,
1108 [VK_BORDER_COLOR_INT_OPAQUE_WHITE
] = {
1116 tu_CreateDevice(VkPhysicalDevice physicalDevice
,
1117 const VkDeviceCreateInfo
*pCreateInfo
,
1118 const VkAllocationCallbacks
*pAllocator
,
1121 TU_FROM_HANDLE(tu_physical_device
, physical_device
, physicalDevice
);
1123 struct tu_device
*device
;
1125 /* Check enabled features */
1126 if (pCreateInfo
->pEnabledFeatures
) {
1127 VkPhysicalDeviceFeatures supported_features
;
1128 tu_GetPhysicalDeviceFeatures(physicalDevice
, &supported_features
);
1129 VkBool32
*supported_feature
= (VkBool32
*) &supported_features
;
1130 VkBool32
*enabled_feature
= (VkBool32
*) pCreateInfo
->pEnabledFeatures
;
1131 unsigned num_features
=
1132 sizeof(VkPhysicalDeviceFeatures
) / sizeof(VkBool32
);
1133 for (uint32_t i
= 0; i
< num_features
; i
++) {
1134 if (enabled_feature
[i
] && !supported_feature
[i
])
1135 return vk_error(physical_device
->instance
,
1136 VK_ERROR_FEATURE_NOT_PRESENT
);
1140 device
= vk_zalloc2(&physical_device
->instance
->alloc
, pAllocator
,
1141 sizeof(*device
), 8, VK_SYSTEM_ALLOCATION_SCOPE_DEVICE
);
1143 return vk_error(physical_device
->instance
, VK_ERROR_OUT_OF_HOST_MEMORY
);
1145 device
->_loader_data
.loaderMagic
= ICD_LOADER_MAGIC
;
1146 device
->instance
= physical_device
->instance
;
1147 device
->physical_device
= physical_device
;
1150 device
->alloc
= *pAllocator
;
1152 device
->alloc
= physical_device
->instance
->alloc
;
1154 for (uint32_t i
= 0; i
< pCreateInfo
->enabledExtensionCount
; i
++) {
1155 const char *ext_name
= pCreateInfo
->ppEnabledExtensionNames
[i
];
1156 int index
= tu_get_device_extension_index(ext_name
);
1158 !physical_device
->supported_extensions
.extensions
[index
]) {
1159 vk_free(&device
->alloc
, device
);
1160 return vk_error(physical_device
->instance
,
1161 VK_ERROR_EXTENSION_NOT_PRESENT
);
1164 device
->enabled_extensions
.extensions
[index
] = true;
1167 for (unsigned i
= 0; i
< pCreateInfo
->queueCreateInfoCount
; i
++) {
1168 const VkDeviceQueueCreateInfo
*queue_create
=
1169 &pCreateInfo
->pQueueCreateInfos
[i
];
1170 uint32_t qfi
= queue_create
->queueFamilyIndex
;
1171 device
->queues
[qfi
] = vk_alloc(
1172 &device
->alloc
, queue_create
->queueCount
* sizeof(struct tu_queue
),
1173 8, VK_SYSTEM_ALLOCATION_SCOPE_DEVICE
);
1174 if (!device
->queues
[qfi
]) {
1175 result
= VK_ERROR_OUT_OF_HOST_MEMORY
;
1179 memset(device
->queues
[qfi
], 0,
1180 queue_create
->queueCount
* sizeof(struct tu_queue
));
1182 device
->queue_count
[qfi
] = queue_create
->queueCount
;
1184 for (unsigned q
= 0; q
< queue_create
->queueCount
; q
++) {
1185 result
= tu_queue_init(device
, &device
->queues
[qfi
][q
], qfi
, q
,
1186 queue_create
->flags
);
1187 if (result
!= VK_SUCCESS
)
1192 device
->compiler
= ir3_compiler_create(NULL
, physical_device
->gpu_id
);
1193 if (!device
->compiler
)
1196 #define VSC_DATA_SIZE(pitch) ((pitch) * 32 + 0x100) /* extra size to store VSC_SIZE */
1197 #define VSC_DATA2_SIZE(pitch) ((pitch) * 32)
1199 device
->vsc_data_pitch
= 0x440 * 4;
1200 device
->vsc_data2_pitch
= 0x1040 * 4;
1202 result
= tu_bo_init_new(device
, &device
->vsc_data
, VSC_DATA_SIZE(device
->vsc_data_pitch
));
1203 if (result
!= VK_SUCCESS
)
1206 result
= tu_bo_init_new(device
, &device
->vsc_data2
, VSC_DATA2_SIZE(device
->vsc_data2_pitch
));
1207 if (result
!= VK_SUCCESS
)
1208 goto fail_vsc_data2
;
1210 STATIC_ASSERT(sizeof(struct bcolor_entry
) == 128);
1211 result
= tu_bo_init_new(device
, &device
->border_color
, sizeof(border_color
));
1212 if (result
!= VK_SUCCESS
)
1213 goto fail_border_color
;
1215 result
= tu_bo_map(device
, &device
->border_color
);
1216 if (result
!= VK_SUCCESS
)
1217 goto fail_border_color_map
;
1219 memcpy(device
->border_color
.map
, border_color
, sizeof(border_color
));
1221 VkPipelineCacheCreateInfo ci
;
1222 ci
.sType
= VK_STRUCTURE_TYPE_PIPELINE_CACHE_CREATE_INFO
;
1225 ci
.pInitialData
= NULL
;
1226 ci
.initialDataSize
= 0;
1229 tu_CreatePipelineCache(tu_device_to_handle(device
), &ci
, NULL
, &pc
);
1230 if (result
!= VK_SUCCESS
)
1231 goto fail_pipeline_cache
;
1233 device
->mem_cache
= tu_pipeline_cache_from_handle(pc
);
1235 *pDevice
= tu_device_to_handle(device
);
1238 fail_pipeline_cache
:
1239 fail_border_color_map
:
1240 tu_bo_finish(device
, &device
->border_color
);
1243 tu_bo_finish(device
, &device
->vsc_data2
);
1246 tu_bo_finish(device
, &device
->vsc_data
);
1249 ralloc_free(device
->compiler
);
1252 for (unsigned i
= 0; i
< TU_MAX_QUEUE_FAMILIES
; i
++) {
1253 for (unsigned q
= 0; q
< device
->queue_count
[i
]; q
++)
1254 tu_queue_finish(&device
->queues
[i
][q
]);
1255 if (device
->queue_count
[i
])
1256 vk_free(&device
->alloc
, device
->queues
[i
]);
1259 vk_free(&device
->alloc
, device
);
1264 tu_DestroyDevice(VkDevice _device
, const VkAllocationCallbacks
*pAllocator
)
1266 TU_FROM_HANDLE(tu_device
, device
, _device
);
1271 tu_bo_finish(device
, &device
->vsc_data
);
1272 tu_bo_finish(device
, &device
->vsc_data2
);
1274 for (unsigned i
= 0; i
< TU_MAX_QUEUE_FAMILIES
; i
++) {
1275 for (unsigned q
= 0; q
< device
->queue_count
[i
]; q
++)
1276 tu_queue_finish(&device
->queues
[i
][q
]);
1277 if (device
->queue_count
[i
])
1278 vk_free(&device
->alloc
, device
->queues
[i
]);
1281 /* the compiler does not use pAllocator */
1282 ralloc_free(device
->compiler
);
1284 VkPipelineCache pc
= tu_pipeline_cache_to_handle(device
->mem_cache
);
1285 tu_DestroyPipelineCache(tu_device_to_handle(device
), pc
, NULL
);
1287 vk_free(&device
->alloc
, device
);
1291 tu_EnumerateInstanceLayerProperties(uint32_t *pPropertyCount
,
1292 VkLayerProperties
*pProperties
)
1294 *pPropertyCount
= 0;
1299 tu_EnumerateDeviceLayerProperties(VkPhysicalDevice physicalDevice
,
1300 uint32_t *pPropertyCount
,
1301 VkLayerProperties
*pProperties
)
1303 *pPropertyCount
= 0;
1308 tu_GetDeviceQueue2(VkDevice _device
,
1309 const VkDeviceQueueInfo2
*pQueueInfo
,
1312 TU_FROM_HANDLE(tu_device
, device
, _device
);
1313 struct tu_queue
*queue
;
1316 &device
->queues
[pQueueInfo
->queueFamilyIndex
][pQueueInfo
->queueIndex
];
1317 if (pQueueInfo
->flags
!= queue
->flags
) {
1318 /* From the Vulkan 1.1.70 spec:
1320 * "The queue returned by vkGetDeviceQueue2 must have the same
1321 * flags value from this structure as that used at device
1322 * creation time in a VkDeviceQueueCreateInfo instance. If no
1323 * matching flags were specified at device creation time then
1324 * pQueue will return VK_NULL_HANDLE."
1326 *pQueue
= VK_NULL_HANDLE
;
1330 *pQueue
= tu_queue_to_handle(queue
);
1334 tu_GetDeviceQueue(VkDevice _device
,
1335 uint32_t queueFamilyIndex
,
1336 uint32_t queueIndex
,
1339 const VkDeviceQueueInfo2 info
=
1340 (VkDeviceQueueInfo2
) { .sType
= VK_STRUCTURE_TYPE_DEVICE_QUEUE_INFO_2
,
1341 .queueFamilyIndex
= queueFamilyIndex
,
1342 .queueIndex
= queueIndex
};
1344 tu_GetDeviceQueue2(_device
, &info
, pQueue
);
1348 tu_QueueSubmit(VkQueue _queue
,
1349 uint32_t submitCount
,
1350 const VkSubmitInfo
*pSubmits
,
1353 TU_FROM_HANDLE(tu_queue
, queue
, _queue
);
1355 for (uint32_t i
= 0; i
< submitCount
; ++i
) {
1356 const VkSubmitInfo
*submit
= pSubmits
+ i
;
1357 const bool last_submit
= (i
== submitCount
- 1);
1358 struct tu_bo_list bo_list
;
1359 tu_bo_list_init(&bo_list
);
1361 uint32_t entry_count
= 0;
1362 for (uint32_t j
= 0; j
< submit
->commandBufferCount
; ++j
) {
1363 TU_FROM_HANDLE(tu_cmd_buffer
, cmdbuf
, submit
->pCommandBuffers
[j
]);
1364 entry_count
+= cmdbuf
->cs
.entry_count
;
1367 struct drm_msm_gem_submit_cmd cmds
[entry_count
];
1368 uint32_t entry_idx
= 0;
1369 for (uint32_t j
= 0; j
< submit
->commandBufferCount
; ++j
) {
1370 TU_FROM_HANDLE(tu_cmd_buffer
, cmdbuf
, submit
->pCommandBuffers
[j
]);
1371 struct tu_cs
*cs
= &cmdbuf
->cs
;
1372 for (unsigned i
= 0; i
< cs
->entry_count
; ++i
, ++entry_idx
) {
1373 cmds
[entry_idx
].type
= MSM_SUBMIT_CMD_BUF
;
1374 cmds
[entry_idx
].submit_idx
=
1375 tu_bo_list_add(&bo_list
, cs
->entries
[i
].bo
,
1376 MSM_SUBMIT_BO_READ
| MSM_SUBMIT_BO_DUMP
);
1377 cmds
[entry_idx
].submit_offset
= cs
->entries
[i
].offset
;
1378 cmds
[entry_idx
].size
= cs
->entries
[i
].size
;
1379 cmds
[entry_idx
].pad
= 0;
1380 cmds
[entry_idx
].nr_relocs
= 0;
1381 cmds
[entry_idx
].relocs
= 0;
1384 tu_bo_list_merge(&bo_list
, &cmdbuf
->bo_list
);
1387 uint32_t flags
= MSM_PIPE_3D0
;
1389 flags
|= MSM_SUBMIT_FENCE_FD_OUT
;
1392 struct drm_msm_gem_submit req
= {
1394 .queueid
= queue
->msm_queue_id
,
1395 .bos
= (uint64_t)(uintptr_t) bo_list
.bo_infos
,
1396 .nr_bos
= bo_list
.count
,
1397 .cmds
= (uint64_t)(uintptr_t)cmds
,
1398 .nr_cmds
= entry_count
,
1401 int ret
= drmCommandWriteRead(queue
->device
->physical_device
->local_fd
,
1405 fprintf(stderr
, "submit failed: %s\n", strerror(errno
));
1409 tu_bo_list_destroy(&bo_list
);
1412 /* no need to merge fences as queue execution is serialized */
1413 tu_fence_update_fd(&queue
->submit_fence
, req
.fence_fd
);
1417 if (_fence
!= VK_NULL_HANDLE
) {
1418 TU_FROM_HANDLE(tu_fence
, fence
, _fence
);
1419 tu_fence_copy(fence
, &queue
->submit_fence
);
1426 tu_QueueWaitIdle(VkQueue _queue
)
1428 TU_FROM_HANDLE(tu_queue
, queue
, _queue
);
1430 tu_fence_wait_idle(&queue
->submit_fence
);
1436 tu_DeviceWaitIdle(VkDevice _device
)
1438 TU_FROM_HANDLE(tu_device
, device
, _device
);
1440 for (unsigned i
= 0; i
< TU_MAX_QUEUE_FAMILIES
; i
++) {
1441 for (unsigned q
= 0; q
< device
->queue_count
[i
]; q
++) {
1442 tu_QueueWaitIdle(tu_queue_to_handle(&device
->queues
[i
][q
]));
1449 tu_ImportSemaphoreFdKHR(VkDevice _device
,
1450 const VkImportSemaphoreFdInfoKHR
*pImportSemaphoreFdInfo
)
1458 tu_GetSemaphoreFdKHR(VkDevice _device
,
1459 const VkSemaphoreGetFdInfoKHR
*pGetFdInfo
,
1468 tu_ImportFenceFdKHR(VkDevice _device
,
1469 const VkImportFenceFdInfoKHR
*pImportFenceFdInfo
)
1477 tu_GetFenceFdKHR(VkDevice _device
,
1478 const VkFenceGetFdInfoKHR
*pGetFdInfo
,
1487 tu_EnumerateInstanceExtensionProperties(const char *pLayerName
,
1488 uint32_t *pPropertyCount
,
1489 VkExtensionProperties
*pProperties
)
1491 VK_OUTARRAY_MAKE(out
, pProperties
, pPropertyCount
);
1493 /* We spport no lyaers */
1495 return vk_error(NULL
, VK_ERROR_LAYER_NOT_PRESENT
);
1497 for (int i
= 0; i
< TU_INSTANCE_EXTENSION_COUNT
; i
++) {
1498 if (tu_supported_instance_extensions
.extensions
[i
]) {
1499 vk_outarray_append(&out
, prop
) { *prop
= tu_instance_extensions
[i
]; }
1503 return vk_outarray_status(&out
);
1507 tu_EnumerateDeviceExtensionProperties(VkPhysicalDevice physicalDevice
,
1508 const char *pLayerName
,
1509 uint32_t *pPropertyCount
,
1510 VkExtensionProperties
*pProperties
)
1512 /* We spport no lyaers */
1513 TU_FROM_HANDLE(tu_physical_device
, device
, physicalDevice
);
1514 VK_OUTARRAY_MAKE(out
, pProperties
, pPropertyCount
);
1516 /* We spport no lyaers */
1518 return vk_error(NULL
, VK_ERROR_LAYER_NOT_PRESENT
);
1520 for (int i
= 0; i
< TU_DEVICE_EXTENSION_COUNT
; i
++) {
1521 if (device
->supported_extensions
.extensions
[i
]) {
1522 vk_outarray_append(&out
, prop
) { *prop
= tu_device_extensions
[i
]; }
1526 return vk_outarray_status(&out
);
1530 tu_GetInstanceProcAddr(VkInstance _instance
, const char *pName
)
1532 TU_FROM_HANDLE(tu_instance
, instance
, _instance
);
1534 return tu_lookup_entrypoint_checked(
1535 pName
, instance
? instance
->api_version
: 0,
1536 instance
? &instance
->enabled_extensions
: NULL
, NULL
);
1539 /* The loader wants us to expose a second GetInstanceProcAddr function
1540 * to work around certain LD_PRELOAD issues seen in apps.
1543 VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL
1544 vk_icdGetInstanceProcAddr(VkInstance instance
, const char *pName
);
1547 VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL
1548 vk_icdGetInstanceProcAddr(VkInstance instance
, const char *pName
)
1550 return tu_GetInstanceProcAddr(instance
, pName
);
1554 tu_GetDeviceProcAddr(VkDevice _device
, const char *pName
)
1556 TU_FROM_HANDLE(tu_device
, device
, _device
);
1558 return tu_lookup_entrypoint_checked(pName
, device
->instance
->api_version
,
1559 &device
->instance
->enabled_extensions
,
1560 &device
->enabled_extensions
);
1564 tu_alloc_memory(struct tu_device
*device
,
1565 const VkMemoryAllocateInfo
*pAllocateInfo
,
1566 const VkAllocationCallbacks
*pAllocator
,
1567 VkDeviceMemory
*pMem
)
1569 struct tu_device_memory
*mem
;
1572 assert(pAllocateInfo
->sType
== VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO
);
1574 if (pAllocateInfo
->allocationSize
== 0) {
1575 /* Apparently, this is allowed */
1576 *pMem
= VK_NULL_HANDLE
;
1580 mem
= vk_alloc2(&device
->alloc
, pAllocator
, sizeof(*mem
), 8,
1581 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
1583 return vk_error(device
->instance
, VK_ERROR_OUT_OF_HOST_MEMORY
);
1585 const VkImportMemoryFdInfoKHR
*fd_info
=
1586 vk_find_struct_const(pAllocateInfo
->pNext
, IMPORT_MEMORY_FD_INFO_KHR
);
1587 if (fd_info
&& !fd_info
->handleType
)
1591 assert(fd_info
->handleType
==
1592 VK_EXTERNAL_MEMORY_HANDLE_TYPE_OPAQUE_FD_BIT
||
1593 fd_info
->handleType
==
1594 VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_EXT
);
1597 * TODO Importing the same fd twice gives us the same handle without
1598 * reference counting. We need to maintain a per-instance handle-to-bo
1599 * table and add reference count to tu_bo.
1601 result
= tu_bo_init_dmabuf(device
, &mem
->bo
,
1602 pAllocateInfo
->allocationSize
, fd_info
->fd
);
1603 if (result
== VK_SUCCESS
) {
1604 /* take ownership and close the fd */
1609 tu_bo_init_new(device
, &mem
->bo
, pAllocateInfo
->allocationSize
);
1612 if (result
!= VK_SUCCESS
) {
1613 vk_free2(&device
->alloc
, pAllocator
, mem
);
1617 mem
->size
= pAllocateInfo
->allocationSize
;
1618 mem
->type_index
= pAllocateInfo
->memoryTypeIndex
;
1621 mem
->user_ptr
= NULL
;
1623 *pMem
= tu_device_memory_to_handle(mem
);
1629 tu_AllocateMemory(VkDevice _device
,
1630 const VkMemoryAllocateInfo
*pAllocateInfo
,
1631 const VkAllocationCallbacks
*pAllocator
,
1632 VkDeviceMemory
*pMem
)
1634 TU_FROM_HANDLE(tu_device
, device
, _device
);
1635 return tu_alloc_memory(device
, pAllocateInfo
, pAllocator
, pMem
);
1639 tu_FreeMemory(VkDevice _device
,
1640 VkDeviceMemory _mem
,
1641 const VkAllocationCallbacks
*pAllocator
)
1643 TU_FROM_HANDLE(tu_device
, device
, _device
);
1644 TU_FROM_HANDLE(tu_device_memory
, mem
, _mem
);
1649 tu_bo_finish(device
, &mem
->bo
);
1650 vk_free2(&device
->alloc
, pAllocator
, mem
);
1654 tu_MapMemory(VkDevice _device
,
1655 VkDeviceMemory _memory
,
1656 VkDeviceSize offset
,
1658 VkMemoryMapFlags flags
,
1661 TU_FROM_HANDLE(tu_device
, device
, _device
);
1662 TU_FROM_HANDLE(tu_device_memory
, mem
, _memory
);
1670 if (mem
->user_ptr
) {
1671 *ppData
= mem
->user_ptr
;
1672 } else if (!mem
->map
) {
1673 result
= tu_bo_map(device
, &mem
->bo
);
1674 if (result
!= VK_SUCCESS
)
1676 *ppData
= mem
->map
= mem
->bo
.map
;
1685 return vk_error(device
->instance
, VK_ERROR_MEMORY_MAP_FAILED
);
1689 tu_UnmapMemory(VkDevice _device
, VkDeviceMemory _memory
)
1691 /* I do not see any unmapping done by the freedreno Gallium driver. */
1695 tu_FlushMappedMemoryRanges(VkDevice _device
,
1696 uint32_t memoryRangeCount
,
1697 const VkMappedMemoryRange
*pMemoryRanges
)
1703 tu_InvalidateMappedMemoryRanges(VkDevice _device
,
1704 uint32_t memoryRangeCount
,
1705 const VkMappedMemoryRange
*pMemoryRanges
)
1711 tu_GetBufferMemoryRequirements(VkDevice _device
,
1713 VkMemoryRequirements
*pMemoryRequirements
)
1715 TU_FROM_HANDLE(tu_buffer
, buffer
, _buffer
);
1717 pMemoryRequirements
->memoryTypeBits
= 1;
1718 pMemoryRequirements
->alignment
= 16;
1719 pMemoryRequirements
->size
=
1720 align64(buffer
->size
, pMemoryRequirements
->alignment
);
1724 tu_GetBufferMemoryRequirements2(
1726 const VkBufferMemoryRequirementsInfo2
*pInfo
,
1727 VkMemoryRequirements2
*pMemoryRequirements
)
1729 tu_GetBufferMemoryRequirements(device
, pInfo
->buffer
,
1730 &pMemoryRequirements
->memoryRequirements
);
1734 tu_GetImageMemoryRequirements(VkDevice _device
,
1736 VkMemoryRequirements
*pMemoryRequirements
)
1738 TU_FROM_HANDLE(tu_image
, image
, _image
);
1740 pMemoryRequirements
->memoryTypeBits
= 1;
1741 pMemoryRequirements
->size
= image
->layout
.size
;
1742 pMemoryRequirements
->alignment
= image
->layout
.base_align
;
1746 tu_GetImageMemoryRequirements2(VkDevice device
,
1747 const VkImageMemoryRequirementsInfo2
*pInfo
,
1748 VkMemoryRequirements2
*pMemoryRequirements
)
1750 tu_GetImageMemoryRequirements(device
, pInfo
->image
,
1751 &pMemoryRequirements
->memoryRequirements
);
1755 tu_GetImageSparseMemoryRequirements(
1758 uint32_t *pSparseMemoryRequirementCount
,
1759 VkSparseImageMemoryRequirements
*pSparseMemoryRequirements
)
1765 tu_GetImageSparseMemoryRequirements2(
1767 const VkImageSparseMemoryRequirementsInfo2
*pInfo
,
1768 uint32_t *pSparseMemoryRequirementCount
,
1769 VkSparseImageMemoryRequirements2
*pSparseMemoryRequirements
)
1775 tu_GetDeviceMemoryCommitment(VkDevice device
,
1776 VkDeviceMemory memory
,
1777 VkDeviceSize
*pCommittedMemoryInBytes
)
1779 *pCommittedMemoryInBytes
= 0;
1783 tu_BindBufferMemory2(VkDevice device
,
1784 uint32_t bindInfoCount
,
1785 const VkBindBufferMemoryInfo
*pBindInfos
)
1787 for (uint32_t i
= 0; i
< bindInfoCount
; ++i
) {
1788 TU_FROM_HANDLE(tu_device_memory
, mem
, pBindInfos
[i
].memory
);
1789 TU_FROM_HANDLE(tu_buffer
, buffer
, pBindInfos
[i
].buffer
);
1792 buffer
->bo
= &mem
->bo
;
1793 buffer
->bo_offset
= pBindInfos
[i
].memoryOffset
;
1802 tu_BindBufferMemory(VkDevice device
,
1804 VkDeviceMemory memory
,
1805 VkDeviceSize memoryOffset
)
1807 const VkBindBufferMemoryInfo info
= {
1808 .sType
= VK_STRUCTURE_TYPE_BIND_BUFFER_MEMORY_INFO
,
1811 .memoryOffset
= memoryOffset
1814 return tu_BindBufferMemory2(device
, 1, &info
);
1818 tu_BindImageMemory2(VkDevice device
,
1819 uint32_t bindInfoCount
,
1820 const VkBindImageMemoryInfo
*pBindInfos
)
1822 for (uint32_t i
= 0; i
< bindInfoCount
; ++i
) {
1823 TU_FROM_HANDLE(tu_image
, image
, pBindInfos
[i
].image
);
1824 TU_FROM_HANDLE(tu_device_memory
, mem
, pBindInfos
[i
].memory
);
1827 image
->bo
= &mem
->bo
;
1828 image
->bo_offset
= pBindInfos
[i
].memoryOffset
;
1831 image
->bo_offset
= 0;
1839 tu_BindImageMemory(VkDevice device
,
1841 VkDeviceMemory memory
,
1842 VkDeviceSize memoryOffset
)
1844 const VkBindImageMemoryInfo info
= {
1845 .sType
= VK_STRUCTURE_TYPE_BIND_BUFFER_MEMORY_INFO
,
1848 .memoryOffset
= memoryOffset
1851 return tu_BindImageMemory2(device
, 1, &info
);
1855 tu_QueueBindSparse(VkQueue _queue
,
1856 uint32_t bindInfoCount
,
1857 const VkBindSparseInfo
*pBindInfo
,
1863 // Queue semaphore functions
1866 tu_CreateSemaphore(VkDevice _device
,
1867 const VkSemaphoreCreateInfo
*pCreateInfo
,
1868 const VkAllocationCallbacks
*pAllocator
,
1869 VkSemaphore
*pSemaphore
)
1871 TU_FROM_HANDLE(tu_device
, device
, _device
);
1873 struct tu_semaphore
*sem
=
1874 vk_alloc2(&device
->alloc
, pAllocator
, sizeof(*sem
), 8,
1875 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
1877 return vk_error(device
->instance
, VK_ERROR_OUT_OF_HOST_MEMORY
);
1879 *pSemaphore
= tu_semaphore_to_handle(sem
);
1884 tu_DestroySemaphore(VkDevice _device
,
1885 VkSemaphore _semaphore
,
1886 const VkAllocationCallbacks
*pAllocator
)
1888 TU_FROM_HANDLE(tu_device
, device
, _device
);
1889 TU_FROM_HANDLE(tu_semaphore
, sem
, _semaphore
);
1893 vk_free2(&device
->alloc
, pAllocator
, sem
);
1897 tu_CreateEvent(VkDevice _device
,
1898 const VkEventCreateInfo
*pCreateInfo
,
1899 const VkAllocationCallbacks
*pAllocator
,
1902 TU_FROM_HANDLE(tu_device
, device
, _device
);
1903 struct tu_event
*event
=
1904 vk_alloc2(&device
->alloc
, pAllocator
, sizeof(*event
), 8,
1905 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
1908 return vk_error(device
->instance
, VK_ERROR_OUT_OF_HOST_MEMORY
);
1910 VkResult result
= tu_bo_init_new(device
, &event
->bo
, 0x1000);
1911 if (result
!= VK_SUCCESS
)
1914 result
= tu_bo_map(device
, &event
->bo
);
1915 if (result
!= VK_SUCCESS
)
1918 *pEvent
= tu_event_to_handle(event
);
1923 tu_bo_finish(device
, &event
->bo
);
1925 vk_free2(&device
->alloc
, pAllocator
, event
);
1926 return vk_error(device
->instance
, VK_ERROR_OUT_OF_HOST_MEMORY
);
1930 tu_DestroyEvent(VkDevice _device
,
1932 const VkAllocationCallbacks
*pAllocator
)
1934 TU_FROM_HANDLE(tu_device
, device
, _device
);
1935 TU_FROM_HANDLE(tu_event
, event
, _event
);
1940 tu_bo_finish(device
, &event
->bo
);
1941 vk_free2(&device
->alloc
, pAllocator
, event
);
1945 tu_GetEventStatus(VkDevice _device
, VkEvent _event
)
1947 TU_FROM_HANDLE(tu_event
, event
, _event
);
1949 if (*(uint64_t*) event
->bo
.map
== 1)
1950 return VK_EVENT_SET
;
1951 return VK_EVENT_RESET
;
1955 tu_SetEvent(VkDevice _device
, VkEvent _event
)
1957 TU_FROM_HANDLE(tu_event
, event
, _event
);
1958 *(uint64_t*) event
->bo
.map
= 1;
1964 tu_ResetEvent(VkDevice _device
, VkEvent _event
)
1966 TU_FROM_HANDLE(tu_event
, event
, _event
);
1967 *(uint64_t*) event
->bo
.map
= 0;
1973 tu_CreateBuffer(VkDevice _device
,
1974 const VkBufferCreateInfo
*pCreateInfo
,
1975 const VkAllocationCallbacks
*pAllocator
,
1978 TU_FROM_HANDLE(tu_device
, device
, _device
);
1979 struct tu_buffer
*buffer
;
1981 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO
);
1983 buffer
= vk_alloc2(&device
->alloc
, pAllocator
, sizeof(*buffer
), 8,
1984 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
1986 return vk_error(device
->instance
, VK_ERROR_OUT_OF_HOST_MEMORY
);
1988 buffer
->size
= pCreateInfo
->size
;
1989 buffer
->usage
= pCreateInfo
->usage
;
1990 buffer
->flags
= pCreateInfo
->flags
;
1992 *pBuffer
= tu_buffer_to_handle(buffer
);
1998 tu_DestroyBuffer(VkDevice _device
,
2000 const VkAllocationCallbacks
*pAllocator
)
2002 TU_FROM_HANDLE(tu_device
, device
, _device
);
2003 TU_FROM_HANDLE(tu_buffer
, buffer
, _buffer
);
2008 vk_free2(&device
->alloc
, pAllocator
, buffer
);
2012 tu_surface_max_layer_count(struct tu_image_view
*iview
)
2014 return iview
->type
== VK_IMAGE_VIEW_TYPE_3D
2015 ? iview
->extent
.depth
2016 : (iview
->base_layer
+ iview
->layer_count
);
2020 tu_CreateFramebuffer(VkDevice _device
,
2021 const VkFramebufferCreateInfo
*pCreateInfo
,
2022 const VkAllocationCallbacks
*pAllocator
,
2023 VkFramebuffer
*pFramebuffer
)
2025 TU_FROM_HANDLE(tu_device
, device
, _device
);
2026 struct tu_framebuffer
*framebuffer
;
2028 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO
);
2030 size_t size
= sizeof(*framebuffer
) + sizeof(struct tu_attachment_info
) *
2031 pCreateInfo
->attachmentCount
;
2032 framebuffer
= vk_alloc2(&device
->alloc
, pAllocator
, size
, 8,
2033 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
2034 if (framebuffer
== NULL
)
2035 return vk_error(device
->instance
, VK_ERROR_OUT_OF_HOST_MEMORY
);
2037 framebuffer
->attachment_count
= pCreateInfo
->attachmentCount
;
2038 framebuffer
->width
= pCreateInfo
->width
;
2039 framebuffer
->height
= pCreateInfo
->height
;
2040 framebuffer
->layers
= pCreateInfo
->layers
;
2041 for (uint32_t i
= 0; i
< pCreateInfo
->attachmentCount
; i
++) {
2042 VkImageView _iview
= pCreateInfo
->pAttachments
[i
];
2043 struct tu_image_view
*iview
= tu_image_view_from_handle(_iview
);
2044 framebuffer
->attachments
[i
].attachment
= iview
;
2046 framebuffer
->width
= MIN2(framebuffer
->width
, iview
->extent
.width
);
2047 framebuffer
->height
= MIN2(framebuffer
->height
, iview
->extent
.height
);
2048 framebuffer
->layers
=
2049 MIN2(framebuffer
->layers
, tu_surface_max_layer_count(iview
));
2052 *pFramebuffer
= tu_framebuffer_to_handle(framebuffer
);
2057 tu_DestroyFramebuffer(VkDevice _device
,
2059 const VkAllocationCallbacks
*pAllocator
)
2061 TU_FROM_HANDLE(tu_device
, device
, _device
);
2062 TU_FROM_HANDLE(tu_framebuffer
, fb
, _fb
);
2066 vk_free2(&device
->alloc
, pAllocator
, fb
);
2069 static enum a6xx_tex_clamp
2070 tu6_tex_wrap(VkSamplerAddressMode address_mode
)
2072 switch (address_mode
) {
2073 case VK_SAMPLER_ADDRESS_MODE_REPEAT
:
2074 return A6XX_TEX_REPEAT
;
2075 case VK_SAMPLER_ADDRESS_MODE_MIRRORED_REPEAT
:
2076 return A6XX_TEX_MIRROR_REPEAT
;
2077 case VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE
:
2078 return A6XX_TEX_CLAMP_TO_EDGE
;
2079 case VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_BORDER
:
2080 return A6XX_TEX_CLAMP_TO_BORDER
;
2081 case VK_SAMPLER_ADDRESS_MODE_MIRROR_CLAMP_TO_EDGE
:
2082 /* only works for PoT.. need to emulate otherwise! */
2083 return A6XX_TEX_MIRROR_CLAMP
;
2085 unreachable("illegal tex wrap mode");
2090 static enum a6xx_tex_filter
2091 tu6_tex_filter(VkFilter filter
, unsigned aniso
)
2094 case VK_FILTER_NEAREST
:
2095 return A6XX_TEX_NEAREST
;
2096 case VK_FILTER_LINEAR
:
2097 return aniso
? A6XX_TEX_ANISO
: A6XX_TEX_LINEAR
;
2098 case VK_FILTER_CUBIC_IMG
:
2100 unreachable("illegal texture filter");
2105 static inline enum adreno_compare_func
2106 tu6_compare_func(VkCompareOp op
)
2108 return (enum adreno_compare_func
) op
;
2112 tu_init_sampler(struct tu_device
*device
,
2113 struct tu_sampler
*sampler
,
2114 const VkSamplerCreateInfo
*pCreateInfo
)
2116 unsigned aniso
= pCreateInfo
->anisotropyEnable
?
2117 util_last_bit(MIN2((uint32_t)pCreateInfo
->maxAnisotropy
>> 1, 8)) : 0;
2118 bool miplinear
= (pCreateInfo
->mipmapMode
== VK_SAMPLER_MIPMAP_MODE_LINEAR
);
2120 sampler
->descriptor
[0] =
2121 COND(miplinear
, A6XX_TEX_SAMP_0_MIPFILTER_LINEAR_NEAR
) |
2122 A6XX_TEX_SAMP_0_XY_MAG(tu6_tex_filter(pCreateInfo
->magFilter
, aniso
)) |
2123 A6XX_TEX_SAMP_0_XY_MIN(tu6_tex_filter(pCreateInfo
->minFilter
, aniso
)) |
2124 A6XX_TEX_SAMP_0_ANISO(aniso
) |
2125 A6XX_TEX_SAMP_0_WRAP_S(tu6_tex_wrap(pCreateInfo
->addressModeU
)) |
2126 A6XX_TEX_SAMP_0_WRAP_T(tu6_tex_wrap(pCreateInfo
->addressModeV
)) |
2127 A6XX_TEX_SAMP_0_WRAP_R(tu6_tex_wrap(pCreateInfo
->addressModeW
)) |
2128 A6XX_TEX_SAMP_0_LOD_BIAS(pCreateInfo
->mipLodBias
);
2129 sampler
->descriptor
[1] =
2130 /* COND(!cso->seamless_cube_map, A6XX_TEX_SAMP_1_CUBEMAPSEAMLESSFILTOFF) | */
2131 COND(pCreateInfo
->unnormalizedCoordinates
, A6XX_TEX_SAMP_1_UNNORM_COORDS
) |
2132 A6XX_TEX_SAMP_1_MIN_LOD(pCreateInfo
->minLod
) |
2133 A6XX_TEX_SAMP_1_MAX_LOD(pCreateInfo
->maxLod
) |
2134 COND(pCreateInfo
->compareEnable
,
2135 A6XX_TEX_SAMP_1_COMPARE_FUNC(tu6_compare_func(pCreateInfo
->compareOp
)));
2136 /* This is an offset into the border_color BO, which we fill with all the
2137 * possible Vulkan border colors in the correct order, so we can just use
2138 * the Vulkan enum with no translation necessary.
2140 sampler
->descriptor
[2] =
2141 A6XX_TEX_SAMP_2_BCOLOR_OFFSET((unsigned) pCreateInfo
->borderColor
*
2142 sizeof(struct bcolor_entry
));
2143 sampler
->descriptor
[3] = 0;
2146 * A6XX_TEX_SAMP_1_MIPFILTER_LINEAR_FAR disables mipmapping, but vk has no NONE mipfilter?
2151 tu_CreateSampler(VkDevice _device
,
2152 const VkSamplerCreateInfo
*pCreateInfo
,
2153 const VkAllocationCallbacks
*pAllocator
,
2154 VkSampler
*pSampler
)
2156 TU_FROM_HANDLE(tu_device
, device
, _device
);
2157 struct tu_sampler
*sampler
;
2159 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO
);
2161 sampler
= vk_alloc2(&device
->alloc
, pAllocator
, sizeof(*sampler
), 8,
2162 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
2164 return vk_error(device
->instance
, VK_ERROR_OUT_OF_HOST_MEMORY
);
2166 tu_init_sampler(device
, sampler
, pCreateInfo
);
2167 *pSampler
= tu_sampler_to_handle(sampler
);
2173 tu_DestroySampler(VkDevice _device
,
2175 const VkAllocationCallbacks
*pAllocator
)
2177 TU_FROM_HANDLE(tu_device
, device
, _device
);
2178 TU_FROM_HANDLE(tu_sampler
, sampler
, _sampler
);
2182 vk_free2(&device
->alloc
, pAllocator
, sampler
);
2185 /* vk_icd.h does not declare this function, so we declare it here to
2186 * suppress Wmissing-prototypes.
2188 PUBLIC VKAPI_ATTR VkResult VKAPI_CALL
2189 vk_icdNegotiateLoaderICDInterfaceVersion(uint32_t *pSupportedVersion
);
2191 PUBLIC VKAPI_ATTR VkResult VKAPI_CALL
2192 vk_icdNegotiateLoaderICDInterfaceVersion(uint32_t *pSupportedVersion
)
2194 /* For the full details on loader interface versioning, see
2195 * <https://github.com/KhronosGroup/Vulkan-LoaderAndValidationLayers/blob/master/loader/LoaderAndLayerInterface.md>.
2196 * What follows is a condensed summary, to help you navigate the large and
2197 * confusing official doc.
2199 * - Loader interface v0 is incompatible with later versions. We don't
2202 * - In loader interface v1:
2203 * - The first ICD entrypoint called by the loader is
2204 * vk_icdGetInstanceProcAddr(). The ICD must statically expose this
2206 * - The ICD must statically expose no other Vulkan symbol unless it
2207 * is linked with -Bsymbolic.
2208 * - Each dispatchable Vulkan handle created by the ICD must be
2209 * a pointer to a struct whose first member is VK_LOADER_DATA. The
2210 * ICD must initialize VK_LOADER_DATA.loadMagic to
2212 * - The loader implements vkCreate{PLATFORM}SurfaceKHR() and
2213 * vkDestroySurfaceKHR(). The ICD must be capable of working with
2214 * such loader-managed surfaces.
2216 * - Loader interface v2 differs from v1 in:
2217 * - The first ICD entrypoint called by the loader is
2218 * vk_icdNegotiateLoaderICDInterfaceVersion(). The ICD must
2219 * statically expose this entrypoint.
2221 * - Loader interface v3 differs from v2 in:
2222 * - The ICD must implement vkCreate{PLATFORM}SurfaceKHR(),
2223 * vkDestroySurfaceKHR(), and other API which uses VKSurfaceKHR,
2224 * because the loader no longer does so.
2226 *pSupportedVersion
= MIN2(*pSupportedVersion
, 3u);
2231 tu_GetMemoryFdKHR(VkDevice _device
,
2232 const VkMemoryGetFdInfoKHR
*pGetFdInfo
,
2235 TU_FROM_HANDLE(tu_device
, device
, _device
);
2236 TU_FROM_HANDLE(tu_device_memory
, memory
, pGetFdInfo
->memory
);
2238 assert(pGetFdInfo
->sType
== VK_STRUCTURE_TYPE_MEMORY_GET_FD_INFO_KHR
);
2240 /* At the moment, we support only the below handle types. */
2241 assert(pGetFdInfo
->handleType
==
2242 VK_EXTERNAL_MEMORY_HANDLE_TYPE_OPAQUE_FD_BIT
||
2243 pGetFdInfo
->handleType
==
2244 VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_EXT
);
2246 int prime_fd
= tu_bo_export_dmabuf(device
, &memory
->bo
);
2248 return vk_error(device
->instance
, VK_ERROR_OUT_OF_DEVICE_MEMORY
);
2255 tu_GetMemoryFdPropertiesKHR(VkDevice _device
,
2256 VkExternalMemoryHandleTypeFlagBits handleType
,
2258 VkMemoryFdPropertiesKHR
*pMemoryFdProperties
)
2260 assert(handleType
== VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_EXT
);
2261 pMemoryFdProperties
->memoryTypeBits
= 1;
2266 tu_GetPhysicalDeviceExternalSemaphoreProperties(
2267 VkPhysicalDevice physicalDevice
,
2268 const VkPhysicalDeviceExternalSemaphoreInfo
*pExternalSemaphoreInfo
,
2269 VkExternalSemaphoreProperties
*pExternalSemaphoreProperties
)
2271 pExternalSemaphoreProperties
->exportFromImportedHandleTypes
= 0;
2272 pExternalSemaphoreProperties
->compatibleHandleTypes
= 0;
2273 pExternalSemaphoreProperties
->externalSemaphoreFeatures
= 0;
2277 tu_GetPhysicalDeviceExternalFenceProperties(
2278 VkPhysicalDevice physicalDevice
,
2279 const VkPhysicalDeviceExternalFenceInfo
*pExternalFenceInfo
,
2280 VkExternalFenceProperties
*pExternalFenceProperties
)
2282 pExternalFenceProperties
->exportFromImportedHandleTypes
= 0;
2283 pExternalFenceProperties
->compatibleHandleTypes
= 0;
2284 pExternalFenceProperties
->externalFenceFeatures
= 0;
2288 tu_CreateDebugReportCallbackEXT(
2289 VkInstance _instance
,
2290 const VkDebugReportCallbackCreateInfoEXT
*pCreateInfo
,
2291 const VkAllocationCallbacks
*pAllocator
,
2292 VkDebugReportCallbackEXT
*pCallback
)
2294 TU_FROM_HANDLE(tu_instance
, instance
, _instance
);
2295 return vk_create_debug_report_callback(&instance
->debug_report_callbacks
,
2296 pCreateInfo
, pAllocator
,
2297 &instance
->alloc
, pCallback
);
2301 tu_DestroyDebugReportCallbackEXT(VkInstance _instance
,
2302 VkDebugReportCallbackEXT _callback
,
2303 const VkAllocationCallbacks
*pAllocator
)
2305 TU_FROM_HANDLE(tu_instance
, instance
, _instance
);
2306 vk_destroy_debug_report_callback(&instance
->debug_report_callbacks
,
2307 _callback
, pAllocator
, &instance
->alloc
);
2311 tu_DebugReportMessageEXT(VkInstance _instance
,
2312 VkDebugReportFlagsEXT flags
,
2313 VkDebugReportObjectTypeEXT objectType
,
2316 int32_t messageCode
,
2317 const char *pLayerPrefix
,
2318 const char *pMessage
)
2320 TU_FROM_HANDLE(tu_instance
, instance
, _instance
);
2321 vk_debug_report(&instance
->debug_report_callbacks
, flags
, objectType
,
2322 object
, location
, messageCode
, pLayerPrefix
, pMessage
);
2326 tu_GetDeviceGroupPeerMemoryFeatures(
2329 uint32_t localDeviceIndex
,
2330 uint32_t remoteDeviceIndex
,
2331 VkPeerMemoryFeatureFlags
*pPeerMemoryFeatures
)
2333 assert(localDeviceIndex
== remoteDeviceIndex
);
2335 *pPeerMemoryFeatures
= VK_PEER_MEMORY_FEATURE_COPY_SRC_BIT
|
2336 VK_PEER_MEMORY_FEATURE_COPY_DST_BIT
|
2337 VK_PEER_MEMORY_FEATURE_GENERIC_SRC_BIT
|
2338 VK_PEER_MEMORY_FEATURE_GENERIC_DST_BIT
;