turnip: enable VK_EXT_index_type_uint8
[mesa.git] / src / freedreno / vulkan / tu_device.c
1 /*
2 * Copyright © 2016 Red Hat.
3 * Copyright © 2016 Bas Nieuwenhuizen
4 *
5 * based in part on anv driver which is:
6 * Copyright © 2015 Intel Corporation
7 *
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:
14 *
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
17 * Software.
18 *
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.
26 */
27
28 #include "tu_private.h"
29
30 #include <fcntl.h>
31 #include <libsync.h>
32 #include <stdbool.h>
33 #include <string.h>
34 #include <sys/mman.h>
35 #include <sys/sysinfo.h>
36 #include <unistd.h>
37 #include <xf86drm.h>
38
39 #include "compiler/glsl_types.h"
40 #include "util/debug.h"
41 #include "util/disk_cache.h"
42 #include "util/u_atomic.h"
43 #include "vk_format.h"
44 #include "vk_util.h"
45
46 #include "drm-uapi/msm_drm.h"
47
48 /* for fd_get_driver/device_uuid() */
49 #include "freedreno/common/freedreno_uuid.h"
50
51 static int
52 tu_device_get_cache_uuid(uint16_t family, void *uuid)
53 {
54 uint32_t mesa_timestamp;
55 uint16_t f = family;
56 memset(uuid, 0, VK_UUID_SIZE);
57 if (!disk_cache_get_function_timestamp(tu_device_get_cache_uuid,
58 &mesa_timestamp))
59 return -1;
60
61 memcpy(uuid, &mesa_timestamp, 4);
62 memcpy((char *) uuid + 4, &f, 2);
63 snprintf((char *) uuid + 6, VK_UUID_SIZE - 10, "tu");
64 return 0;
65 }
66
67 static VkResult
68 tu_bo_init(struct tu_device *dev,
69 struct tu_bo *bo,
70 uint32_t gem_handle,
71 uint64_t size)
72 {
73 uint64_t iova = tu_gem_info_iova(dev, gem_handle);
74 if (!iova)
75 return VK_ERROR_OUT_OF_DEVICE_MEMORY;
76
77 *bo = (struct tu_bo) {
78 .gem_handle = gem_handle,
79 .size = size,
80 .iova = iova,
81 };
82
83 return VK_SUCCESS;
84 }
85
86 VkResult
87 tu_bo_init_new(struct tu_device *dev, struct tu_bo *bo, uint64_t size)
88 {
89 /* TODO: Choose better flags. As of 2018-11-12, freedreno/drm/msm_bo.c
90 * always sets `flags = MSM_BO_WC`, and we copy that behavior here.
91 */
92 uint32_t gem_handle = tu_gem_new(dev, size, MSM_BO_WC);
93 if (!gem_handle)
94 return vk_error(dev->instance, VK_ERROR_OUT_OF_DEVICE_MEMORY);
95
96 VkResult result = tu_bo_init(dev, bo, gem_handle, size);
97 if (result != VK_SUCCESS) {
98 tu_gem_close(dev, gem_handle);
99 return vk_error(dev->instance, result);
100 }
101
102 return VK_SUCCESS;
103 }
104
105 VkResult
106 tu_bo_init_dmabuf(struct tu_device *dev,
107 struct tu_bo *bo,
108 uint64_t size,
109 int fd)
110 {
111 uint32_t gem_handle = tu_gem_import_dmabuf(dev, fd, size);
112 if (!gem_handle)
113 return vk_error(dev->instance, VK_ERROR_INVALID_EXTERNAL_HANDLE);
114
115 VkResult result = tu_bo_init(dev, bo, gem_handle, size);
116 if (result != VK_SUCCESS) {
117 tu_gem_close(dev, gem_handle);
118 return vk_error(dev->instance, result);
119 }
120
121 return VK_SUCCESS;
122 }
123
124 int
125 tu_bo_export_dmabuf(struct tu_device *dev, struct tu_bo *bo)
126 {
127 return tu_gem_export_dmabuf(dev, bo->gem_handle);
128 }
129
130 VkResult
131 tu_bo_map(struct tu_device *dev, struct tu_bo *bo)
132 {
133 if (bo->map)
134 return VK_SUCCESS;
135
136 uint64_t offset = tu_gem_info_offset(dev, bo->gem_handle);
137 if (!offset)
138 return vk_error(dev->instance, VK_ERROR_OUT_OF_DEVICE_MEMORY);
139
140 /* TODO: Should we use the wrapper os_mmap() like Freedreno does? */
141 void *map = mmap(0, bo->size, PROT_READ | PROT_WRITE, MAP_SHARED,
142 dev->physical_device->local_fd, offset);
143 if (map == MAP_FAILED)
144 return vk_error(dev->instance, VK_ERROR_MEMORY_MAP_FAILED);
145
146 bo->map = map;
147 return VK_SUCCESS;
148 }
149
150 void
151 tu_bo_finish(struct tu_device *dev, struct tu_bo *bo)
152 {
153 assert(bo->gem_handle);
154
155 if (bo->map)
156 munmap(bo->map, bo->size);
157
158 tu_gem_close(dev, bo->gem_handle);
159 }
160
161 static VkResult
162 tu_physical_device_init(struct tu_physical_device *device,
163 struct tu_instance *instance,
164 drmDevicePtr drm_device)
165 {
166 const char *path = drm_device->nodes[DRM_NODE_RENDER];
167 VkResult result = VK_SUCCESS;
168 drmVersionPtr version;
169 int fd;
170 int master_fd = -1;
171
172 fd = open(path, O_RDWR | O_CLOEXEC);
173 if (fd < 0) {
174 return vk_errorf(instance, VK_ERROR_INCOMPATIBLE_DRIVER,
175 "failed to open device %s", path);
176 }
177
178 /* Version 1.3 added MSM_INFO_IOVA. */
179 const int min_version_major = 1;
180 const int min_version_minor = 3;
181
182 version = drmGetVersion(fd);
183 if (!version) {
184 close(fd);
185 return vk_errorf(instance, VK_ERROR_INCOMPATIBLE_DRIVER,
186 "failed to query kernel driver version for device %s",
187 path);
188 }
189
190 if (strcmp(version->name, "msm")) {
191 drmFreeVersion(version);
192 close(fd);
193 return vk_errorf(instance, VK_ERROR_INCOMPATIBLE_DRIVER,
194 "device %s does not use the msm kernel driver", path);
195 }
196
197 if (version->version_major != min_version_major ||
198 version->version_minor < min_version_minor) {
199 result = vk_errorf(instance, VK_ERROR_INCOMPATIBLE_DRIVER,
200 "kernel driver for device %s has version %d.%d, "
201 "but Vulkan requires version >= %d.%d",
202 path, version->version_major, version->version_minor,
203 min_version_major, min_version_minor);
204 drmFreeVersion(version);
205 close(fd);
206 return result;
207 }
208
209 drmFreeVersion(version);
210
211 if (instance->debug_flags & TU_DEBUG_STARTUP)
212 tu_logi("Found compatible device '%s'.", path);
213
214 device->_loader_data.loaderMagic = ICD_LOADER_MAGIC;
215 device->instance = instance;
216 assert(strlen(path) < ARRAY_SIZE(device->path));
217 strncpy(device->path, path, ARRAY_SIZE(device->path));
218
219 if (instance->enabled_extensions.KHR_display) {
220 master_fd =
221 open(drm_device->nodes[DRM_NODE_PRIMARY], O_RDWR | O_CLOEXEC);
222 if (master_fd >= 0) {
223 /* TODO: free master_fd is accel is not working? */
224 }
225 }
226
227 device->master_fd = master_fd;
228 device->local_fd = fd;
229
230 if (tu_drm_get_gpu_id(device, &device->gpu_id)) {
231 if (instance->debug_flags & TU_DEBUG_STARTUP)
232 tu_logi("Could not query the GPU ID");
233 result = vk_errorf(instance, VK_ERROR_INITIALIZATION_FAILED,
234 "could not get GPU ID");
235 goto fail;
236 }
237
238 if (tu_drm_get_gmem_size(device, &device->gmem_size)) {
239 if (instance->debug_flags & TU_DEBUG_STARTUP)
240 tu_logi("Could not query the GMEM size");
241 result = vk_errorf(instance, VK_ERROR_INITIALIZATION_FAILED,
242 "could not get GMEM size");
243 goto fail;
244 }
245
246 if (tu_drm_get_gmem_base(device, &device->gmem_base)) {
247 if (instance->debug_flags & TU_DEBUG_STARTUP)
248 tu_logi("Could not query the GMEM size");
249 result = vk_errorf(instance, VK_ERROR_INITIALIZATION_FAILED,
250 "could not get GMEM size");
251 goto fail;
252 }
253
254 memset(device->name, 0, sizeof(device->name));
255 sprintf(device->name, "FD%d", device->gpu_id);
256
257 switch (device->gpu_id) {
258 case 618:
259 device->ccu_offset_gmem = 0x7c000; /* 0x7e000 in some cases? */
260 device->ccu_offset_bypass = 0x10000;
261 device->tile_align_w = 64;
262 device->magic.PC_UNKNOWN_9805 = 0x0;
263 device->magic.SP_UNKNOWN_A0F8 = 0x0;
264 break;
265 case 630:
266 case 640:
267 device->ccu_offset_gmem = 0xf8000;
268 device->ccu_offset_bypass = 0x20000;
269 device->tile_align_w = 64;
270 device->magic.PC_UNKNOWN_9805 = 0x1;
271 device->magic.SP_UNKNOWN_A0F8 = 0x1;
272 break;
273 case 650:
274 device->ccu_offset_gmem = 0x114000;
275 device->ccu_offset_bypass = 0x30000;
276 device->tile_align_w = 96;
277 device->magic.PC_UNKNOWN_9805 = 0x2;
278 device->magic.SP_UNKNOWN_A0F8 = 0x2;
279 break;
280 default:
281 result = vk_errorf(instance, VK_ERROR_INITIALIZATION_FAILED,
282 "device %s is unsupported", device->name);
283 goto fail;
284 }
285 if (tu_device_get_cache_uuid(device->gpu_id, device->cache_uuid)) {
286 result = vk_errorf(instance, VK_ERROR_INITIALIZATION_FAILED,
287 "cannot generate UUID");
288 goto fail;
289 }
290
291 /* The gpu id is already embedded in the uuid so we just pass "tu"
292 * when creating the cache.
293 */
294 char buf[VK_UUID_SIZE * 2 + 1];
295 disk_cache_format_hex_id(buf, device->cache_uuid, VK_UUID_SIZE * 2);
296 device->disk_cache = disk_cache_create(device->name, buf, 0);
297
298 fprintf(stderr, "WARNING: tu is not a conformant vulkan implementation, "
299 "testing use only.\n");
300
301 fd_get_driver_uuid(device->driver_uuid);
302 fd_get_device_uuid(device->device_uuid, device->gpu_id);
303
304 tu_physical_device_get_supported_extensions(device, &device->supported_extensions);
305
306 if (result != VK_SUCCESS) {
307 vk_error(instance, result);
308 goto fail;
309 }
310
311 result = tu_wsi_init(device);
312 if (result != VK_SUCCESS) {
313 vk_error(instance, result);
314 goto fail;
315 }
316
317 return VK_SUCCESS;
318
319 fail:
320 close(fd);
321 if (master_fd != -1)
322 close(master_fd);
323 return result;
324 }
325
326 static void
327 tu_physical_device_finish(struct tu_physical_device *device)
328 {
329 tu_wsi_finish(device);
330
331 disk_cache_destroy(device->disk_cache);
332 close(device->local_fd);
333 if (device->master_fd != -1)
334 close(device->master_fd);
335 }
336
337 static VKAPI_ATTR void *
338 default_alloc_func(void *pUserData,
339 size_t size,
340 size_t align,
341 VkSystemAllocationScope allocationScope)
342 {
343 return malloc(size);
344 }
345
346 static VKAPI_ATTR void *
347 default_realloc_func(void *pUserData,
348 void *pOriginal,
349 size_t size,
350 size_t align,
351 VkSystemAllocationScope allocationScope)
352 {
353 return realloc(pOriginal, size);
354 }
355
356 static VKAPI_ATTR void
357 default_free_func(void *pUserData, void *pMemory)
358 {
359 free(pMemory);
360 }
361
362 static const VkAllocationCallbacks default_alloc = {
363 .pUserData = NULL,
364 .pfnAllocation = default_alloc_func,
365 .pfnReallocation = default_realloc_func,
366 .pfnFree = default_free_func,
367 };
368
369 static const struct debug_control tu_debug_options[] = {
370 { "startup", TU_DEBUG_STARTUP },
371 { "nir", TU_DEBUG_NIR },
372 { "ir3", TU_DEBUG_IR3 },
373 { "nobin", TU_DEBUG_NOBIN },
374 { "sysmem", TU_DEBUG_SYSMEM },
375 { "forcebin", TU_DEBUG_FORCEBIN },
376 { "noubwc", TU_DEBUG_NOUBWC },
377 { NULL, 0 }
378 };
379
380 const char *
381 tu_get_debug_option_name(int id)
382 {
383 assert(id < ARRAY_SIZE(tu_debug_options) - 1);
384 return tu_debug_options[id].string;
385 }
386
387 static int
388 tu_get_instance_extension_index(const char *name)
389 {
390 for (unsigned i = 0; i < TU_INSTANCE_EXTENSION_COUNT; ++i) {
391 if (strcmp(name, tu_instance_extensions[i].extensionName) == 0)
392 return i;
393 }
394 return -1;
395 }
396
397 VkResult
398 tu_CreateInstance(const VkInstanceCreateInfo *pCreateInfo,
399 const VkAllocationCallbacks *pAllocator,
400 VkInstance *pInstance)
401 {
402 struct tu_instance *instance;
403 VkResult result;
404
405 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO);
406
407 uint32_t client_version;
408 if (pCreateInfo->pApplicationInfo &&
409 pCreateInfo->pApplicationInfo->apiVersion != 0) {
410 client_version = pCreateInfo->pApplicationInfo->apiVersion;
411 } else {
412 tu_EnumerateInstanceVersion(&client_version);
413 }
414
415 instance = vk_zalloc2(&default_alloc, pAllocator, sizeof(*instance), 8,
416 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
417 if (!instance)
418 return vk_error(NULL, VK_ERROR_OUT_OF_HOST_MEMORY);
419
420 instance->_loader_data.loaderMagic = ICD_LOADER_MAGIC;
421
422 if (pAllocator)
423 instance->alloc = *pAllocator;
424 else
425 instance->alloc = default_alloc;
426
427 instance->api_version = client_version;
428 instance->physical_device_count = -1;
429
430 instance->debug_flags =
431 parse_debug_string(getenv("TU_DEBUG"), tu_debug_options);
432
433 if (instance->debug_flags & TU_DEBUG_STARTUP)
434 tu_logi("Created an instance");
435
436 for (uint32_t i = 0; i < pCreateInfo->enabledExtensionCount; i++) {
437 const char *ext_name = pCreateInfo->ppEnabledExtensionNames[i];
438 int index = tu_get_instance_extension_index(ext_name);
439
440 if (index < 0 || !tu_instance_extensions_supported.extensions[index]) {
441 vk_free2(&default_alloc, pAllocator, instance);
442 return vk_error(instance, VK_ERROR_EXTENSION_NOT_PRESENT);
443 }
444
445 instance->enabled_extensions.extensions[index] = true;
446 }
447
448 result = vk_debug_report_instance_init(&instance->debug_report_callbacks);
449 if (result != VK_SUCCESS) {
450 vk_free2(&default_alloc, pAllocator, instance);
451 return vk_error(instance, result);
452 }
453
454 glsl_type_singleton_init_or_ref();
455
456 VG(VALGRIND_CREATE_MEMPOOL(instance, 0, false));
457
458 *pInstance = tu_instance_to_handle(instance);
459
460 return VK_SUCCESS;
461 }
462
463 void
464 tu_DestroyInstance(VkInstance _instance,
465 const VkAllocationCallbacks *pAllocator)
466 {
467 TU_FROM_HANDLE(tu_instance, instance, _instance);
468
469 if (!instance)
470 return;
471
472 for (int i = 0; i < instance->physical_device_count; ++i) {
473 tu_physical_device_finish(instance->physical_devices + i);
474 }
475
476 VG(VALGRIND_DESTROY_MEMPOOL(instance));
477
478 glsl_type_singleton_decref();
479
480 vk_debug_report_instance_destroy(&instance->debug_report_callbacks);
481
482 vk_free(&instance->alloc, instance);
483 }
484
485 static VkResult
486 tu_enumerate_devices(struct tu_instance *instance)
487 {
488 /* TODO: Check for more devices ? */
489 drmDevicePtr devices[8];
490 VkResult result = VK_ERROR_INCOMPATIBLE_DRIVER;
491 int max_devices;
492
493 instance->physical_device_count = 0;
494
495 max_devices = drmGetDevices2(0, devices, ARRAY_SIZE(devices));
496
497 if (instance->debug_flags & TU_DEBUG_STARTUP)
498 tu_logi("Found %d drm nodes", max_devices);
499
500 if (max_devices < 1)
501 return vk_error(instance, VK_ERROR_INCOMPATIBLE_DRIVER);
502
503 for (unsigned i = 0; i < (unsigned) max_devices; i++) {
504 if (devices[i]->available_nodes & 1 << DRM_NODE_RENDER &&
505 devices[i]->bustype == DRM_BUS_PLATFORM) {
506
507 result = tu_physical_device_init(
508 instance->physical_devices + instance->physical_device_count,
509 instance, devices[i]);
510 if (result == VK_SUCCESS)
511 ++instance->physical_device_count;
512 else if (result != VK_ERROR_INCOMPATIBLE_DRIVER)
513 break;
514 }
515 }
516 drmFreeDevices(devices, max_devices);
517
518 return result;
519 }
520
521 VkResult
522 tu_EnumeratePhysicalDevices(VkInstance _instance,
523 uint32_t *pPhysicalDeviceCount,
524 VkPhysicalDevice *pPhysicalDevices)
525 {
526 TU_FROM_HANDLE(tu_instance, instance, _instance);
527 VK_OUTARRAY_MAKE(out, pPhysicalDevices, pPhysicalDeviceCount);
528
529 VkResult result;
530
531 if (instance->physical_device_count < 0) {
532 result = tu_enumerate_devices(instance);
533 if (result != VK_SUCCESS && result != VK_ERROR_INCOMPATIBLE_DRIVER)
534 return result;
535 }
536
537 for (uint32_t i = 0; i < instance->physical_device_count; ++i) {
538 vk_outarray_append(&out, p)
539 {
540 *p = tu_physical_device_to_handle(instance->physical_devices + i);
541 }
542 }
543
544 return vk_outarray_status(&out);
545 }
546
547 VkResult
548 tu_EnumeratePhysicalDeviceGroups(
549 VkInstance _instance,
550 uint32_t *pPhysicalDeviceGroupCount,
551 VkPhysicalDeviceGroupProperties *pPhysicalDeviceGroupProperties)
552 {
553 TU_FROM_HANDLE(tu_instance, instance, _instance);
554 VK_OUTARRAY_MAKE(out, pPhysicalDeviceGroupProperties,
555 pPhysicalDeviceGroupCount);
556 VkResult result;
557
558 if (instance->physical_device_count < 0) {
559 result = tu_enumerate_devices(instance);
560 if (result != VK_SUCCESS && result != VK_ERROR_INCOMPATIBLE_DRIVER)
561 return result;
562 }
563
564 for (uint32_t i = 0; i < instance->physical_device_count; ++i) {
565 vk_outarray_append(&out, p)
566 {
567 p->physicalDeviceCount = 1;
568 p->physicalDevices[0] =
569 tu_physical_device_to_handle(instance->physical_devices + i);
570 p->subsetAllocation = false;
571 }
572 }
573
574 return vk_outarray_status(&out);
575 }
576
577 void
578 tu_GetPhysicalDeviceFeatures(VkPhysicalDevice physicalDevice,
579 VkPhysicalDeviceFeatures *pFeatures)
580 {
581 memset(pFeatures, 0, sizeof(*pFeatures));
582
583 *pFeatures = (VkPhysicalDeviceFeatures) {
584 .robustBufferAccess = true,
585 .fullDrawIndexUint32 = true,
586 .imageCubeArray = true,
587 .independentBlend = true,
588 .geometryShader = true,
589 .tessellationShader = true,
590 .sampleRateShading = true,
591 .dualSrcBlend = true,
592 .logicOp = true,
593 .multiDrawIndirect = false,
594 .drawIndirectFirstInstance = false,
595 .depthClamp = true,
596 .depthBiasClamp = false,
597 .fillModeNonSolid = false,
598 .depthBounds = false,
599 .wideLines = false,
600 .largePoints = false,
601 .alphaToOne = true,
602 .multiViewport = false,
603 .samplerAnisotropy = true,
604 .textureCompressionETC2 = true,
605 .textureCompressionASTC_LDR = true,
606 .textureCompressionBC = true,
607 .occlusionQueryPrecise = true,
608 .pipelineStatisticsQuery = false,
609 .vertexPipelineStoresAndAtomics = false,
610 .fragmentStoresAndAtomics = false,
611 .shaderTessellationAndGeometryPointSize = false,
612 .shaderImageGatherExtended = false,
613 .shaderStorageImageExtendedFormats = false,
614 .shaderStorageImageMultisample = false,
615 .shaderUniformBufferArrayDynamicIndexing = false,
616 .shaderSampledImageArrayDynamicIndexing = false,
617 .shaderStorageBufferArrayDynamicIndexing = false,
618 .shaderStorageImageArrayDynamicIndexing = false,
619 .shaderStorageImageReadWithoutFormat = false,
620 .shaderStorageImageWriteWithoutFormat = false,
621 .shaderClipDistance = false,
622 .shaderCullDistance = false,
623 .shaderFloat64 = false,
624 .shaderInt64 = false,
625 .shaderInt16 = false,
626 .sparseBinding = false,
627 .variableMultisampleRate = false,
628 .inheritedQueries = false,
629 };
630 }
631
632 void
633 tu_GetPhysicalDeviceFeatures2(VkPhysicalDevice physicalDevice,
634 VkPhysicalDeviceFeatures2 *pFeatures)
635 {
636 vk_foreach_struct(ext, pFeatures->pNext)
637 {
638 switch (ext->sType) {
639 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VARIABLE_POINTERS_FEATURES: {
640 VkPhysicalDeviceVariablePointersFeatures *features = (void *) ext;
641 features->variablePointersStorageBuffer = false;
642 features->variablePointers = false;
643 break;
644 }
645 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_MULTIVIEW_FEATURES: {
646 VkPhysicalDeviceMultiviewFeatures *features =
647 (VkPhysicalDeviceMultiviewFeatures *) ext;
648 features->multiview = false;
649 features->multiviewGeometryShader = false;
650 features->multiviewTessellationShader = false;
651 break;
652 }
653 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SHADER_DRAW_PARAMETERS_FEATURES: {
654 VkPhysicalDeviceShaderDrawParametersFeatures *features =
655 (VkPhysicalDeviceShaderDrawParametersFeatures *) ext;
656 features->shaderDrawParameters = false;
657 break;
658 }
659 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PROTECTED_MEMORY_FEATURES: {
660 VkPhysicalDeviceProtectedMemoryFeatures *features =
661 (VkPhysicalDeviceProtectedMemoryFeatures *) ext;
662 features->protectedMemory = false;
663 break;
664 }
665 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_16BIT_STORAGE_FEATURES: {
666 VkPhysicalDevice16BitStorageFeatures *features =
667 (VkPhysicalDevice16BitStorageFeatures *) ext;
668 features->storageBuffer16BitAccess = false;
669 features->uniformAndStorageBuffer16BitAccess = false;
670 features->storagePushConstant16 = false;
671 features->storageInputOutput16 = false;
672 break;
673 }
674 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SAMPLER_YCBCR_CONVERSION_FEATURES: {
675 VkPhysicalDeviceSamplerYcbcrConversionFeatures *features =
676 (VkPhysicalDeviceSamplerYcbcrConversionFeatures *) ext;
677 features->samplerYcbcrConversion = true;
678 break;
679 }
680 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_DESCRIPTOR_INDEXING_FEATURES_EXT: {
681 VkPhysicalDeviceDescriptorIndexingFeaturesEXT *features =
682 (VkPhysicalDeviceDescriptorIndexingFeaturesEXT *) ext;
683 features->shaderInputAttachmentArrayDynamicIndexing = false;
684 features->shaderUniformTexelBufferArrayDynamicIndexing = false;
685 features->shaderStorageTexelBufferArrayDynamicIndexing = false;
686 features->shaderUniformBufferArrayNonUniformIndexing = false;
687 features->shaderSampledImageArrayNonUniformIndexing = false;
688 features->shaderStorageBufferArrayNonUniformIndexing = false;
689 features->shaderStorageImageArrayNonUniformIndexing = false;
690 features->shaderInputAttachmentArrayNonUniformIndexing = false;
691 features->shaderUniformTexelBufferArrayNonUniformIndexing = false;
692 features->shaderStorageTexelBufferArrayNonUniformIndexing = false;
693 features->descriptorBindingUniformBufferUpdateAfterBind = false;
694 features->descriptorBindingSampledImageUpdateAfterBind = false;
695 features->descriptorBindingStorageImageUpdateAfterBind = false;
696 features->descriptorBindingStorageBufferUpdateAfterBind = false;
697 features->descriptorBindingUniformTexelBufferUpdateAfterBind = false;
698 features->descriptorBindingStorageTexelBufferUpdateAfterBind = false;
699 features->descriptorBindingUpdateUnusedWhilePending = false;
700 features->descriptorBindingPartiallyBound = false;
701 features->descriptorBindingVariableDescriptorCount = false;
702 features->runtimeDescriptorArray = false;
703 break;
704 }
705 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_CONDITIONAL_RENDERING_FEATURES_EXT: {
706 VkPhysicalDeviceConditionalRenderingFeaturesEXT *features =
707 (VkPhysicalDeviceConditionalRenderingFeaturesEXT *) ext;
708 features->conditionalRendering = false;
709 features->inheritedConditionalRendering = false;
710 break;
711 }
712 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_TRANSFORM_FEEDBACK_FEATURES_EXT: {
713 VkPhysicalDeviceTransformFeedbackFeaturesEXT *features =
714 (VkPhysicalDeviceTransformFeedbackFeaturesEXT *) ext;
715 features->transformFeedback = true;
716 features->geometryStreams = false;
717 break;
718 }
719 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_INDEX_TYPE_UINT8_FEATURES_EXT: {
720 VkPhysicalDeviceIndexTypeUint8FeaturesEXT *features =
721 (VkPhysicalDeviceIndexTypeUint8FeaturesEXT *)ext;
722 features->indexTypeUint8 = true;
723 break;
724 }
725 default:
726 break;
727 }
728 }
729 return tu_GetPhysicalDeviceFeatures(physicalDevice, &pFeatures->features);
730 }
731
732 void
733 tu_GetPhysicalDeviceProperties(VkPhysicalDevice physicalDevice,
734 VkPhysicalDeviceProperties *pProperties)
735 {
736 TU_FROM_HANDLE(tu_physical_device, pdevice, physicalDevice);
737 VkSampleCountFlags sample_counts =
738 VK_SAMPLE_COUNT_1_BIT | VK_SAMPLE_COUNT_2_BIT | VK_SAMPLE_COUNT_4_BIT;
739
740 /* I have no idea what the maximum size is, but the hardware supports very
741 * large numbers of descriptors (at least 2^16). This limit is based on
742 * CP_LOAD_STATE6, which has a 28-bit field for the DWORD offset, so that
743 * we don't have to think about what to do if that overflows, but really
744 * nothing is likely to get close to this.
745 */
746 const size_t max_descriptor_set_size = (1 << 28) / A6XX_TEX_CONST_DWORDS;
747
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 = MAX_UNIFORM_BUFFER_RANGE,
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_RTS,
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_RTS,
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 = 124,
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 = 8,
806 .mipmapPrecisionBits = 8,
807 .maxDrawIndexedIndexValue = UINT32_MAX,
808 .maxDrawIndirectCount = UINT32_MAX,
809 .maxSamplerLodBias = 4095.0 / 256.0, /* [-16, 15.99609375] */
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 = 64,
818 .minStorageBufferOffsetAlignment = 64,
819 .minTexelOffset = -16,
820 .maxTexelOffset = 15,
821 .minTexelGatherOffset = -32,
822 .maxTexelGatherOffset = 31,
823 .minInterpolationOffset = -0.5,
824 .maxInterpolationOffset = 0.4375,
825 .subPixelInterpolationOffsetBits = 4,
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,
855 };
856
857 *pProperties = (VkPhysicalDeviceProperties) {
858 .apiVersion = tu_physical_device_api_version(pdevice),
859 .driverVersion = vk_get_driver_version(),
860 .vendorID = 0, /* TODO */
861 .deviceID = 0,
862 .deviceType = VK_PHYSICAL_DEVICE_TYPE_INTEGRATED_GPU,
863 .limits = limits,
864 .sparseProperties = { 0 },
865 };
866
867 strcpy(pProperties->deviceName, pdevice->name);
868 memcpy(pProperties->pipelineCacheUUID, pdevice->cache_uuid, VK_UUID_SIZE);
869 }
870
871 void
872 tu_GetPhysicalDeviceProperties2(VkPhysicalDevice physicalDevice,
873 VkPhysicalDeviceProperties2 *pProperties)
874 {
875 TU_FROM_HANDLE(tu_physical_device, pdevice, physicalDevice);
876 tu_GetPhysicalDeviceProperties(physicalDevice, &pProperties->properties);
877
878 vk_foreach_struct(ext, pProperties->pNext)
879 {
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;
885 break;
886 }
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;
893 break;
894 }
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;
900 break;
901 }
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;
907 break;
908 }
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;
917 break;
918 }
919 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_TRANSFORM_FEEDBACK_PROPERTIES_EXT: {
920 VkPhysicalDeviceTransformFeedbackPropertiesEXT *properties =
921 (VkPhysicalDeviceTransformFeedbackPropertiesEXT *)ext;
922
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 properties->transformFeedbackQueries = true;
930 properties->transformFeedbackStreamsLinesTriangles = false;
931 properties->transformFeedbackRasterizationStreamSelect = false;
932 properties->transformFeedbackDraw = true;
933 break;
934 }
935 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SAMPLE_LOCATIONS_PROPERTIES_EXT: {
936 VkPhysicalDeviceSampleLocationsPropertiesEXT *properties =
937 (VkPhysicalDeviceSampleLocationsPropertiesEXT *)ext;
938 properties->sampleLocationSampleCounts = 0;
939 if (pdevice->supported_extensions.EXT_sample_locations) {
940 properties->sampleLocationSampleCounts =
941 VK_SAMPLE_COUNT_1_BIT | VK_SAMPLE_COUNT_2_BIT | VK_SAMPLE_COUNT_4_BIT;
942 }
943 properties->maxSampleLocationGridSize = (VkExtent2D) { 1 , 1 };
944 properties->sampleLocationCoordinateRange[0] = 0.0f;
945 properties->sampleLocationCoordinateRange[1] = 0.9375f;
946 properties->sampleLocationSubPixelBits = 4;
947 properties->variableSampleLocations = true;
948 break;
949 }
950 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SAMPLER_FILTER_MINMAX_PROPERTIES: {
951 VkPhysicalDeviceSamplerFilterMinmaxProperties *properties =
952 (VkPhysicalDeviceSamplerFilterMinmaxProperties *)ext;
953 properties->filterMinmaxImageComponentMapping = true;
954 properties->filterMinmaxSingleComponentFormats = true;
955 break;
956 }
957 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SUBGROUP_PROPERTIES: {
958 VkPhysicalDeviceSubgroupProperties *properties =
959 (VkPhysicalDeviceSubgroupProperties *)ext;
960 properties->subgroupSize = 64;
961 properties->supportedStages = VK_SHADER_STAGE_COMPUTE_BIT;
962 properties->supportedOperations = VK_SUBGROUP_FEATURE_BASIC_BIT |
963 VK_SUBGROUP_FEATURE_VOTE_BIT;
964 properties->quadOperationsInAllStages = false;
965 break;
966 }
967
968 default:
969 break;
970 }
971 }
972 }
973
974 static const VkQueueFamilyProperties tu_queue_family_properties = {
975 .queueFlags =
976 VK_QUEUE_GRAPHICS_BIT | VK_QUEUE_COMPUTE_BIT | VK_QUEUE_TRANSFER_BIT,
977 .queueCount = 1,
978 .timestampValidBits = 48,
979 .minImageTransferGranularity = { 1, 1, 1 },
980 };
981
982 void
983 tu_GetPhysicalDeviceQueueFamilyProperties(
984 VkPhysicalDevice physicalDevice,
985 uint32_t *pQueueFamilyPropertyCount,
986 VkQueueFamilyProperties *pQueueFamilyProperties)
987 {
988 VK_OUTARRAY_MAKE(out, pQueueFamilyProperties, pQueueFamilyPropertyCount);
989
990 vk_outarray_append(&out, p) { *p = tu_queue_family_properties; }
991 }
992
993 void
994 tu_GetPhysicalDeviceQueueFamilyProperties2(
995 VkPhysicalDevice physicalDevice,
996 uint32_t *pQueueFamilyPropertyCount,
997 VkQueueFamilyProperties2 *pQueueFamilyProperties)
998 {
999 VK_OUTARRAY_MAKE(out, pQueueFamilyProperties, pQueueFamilyPropertyCount);
1000
1001 vk_outarray_append(&out, p)
1002 {
1003 p->queueFamilyProperties = tu_queue_family_properties;
1004 }
1005 }
1006
1007 static uint64_t
1008 tu_get_system_heap_size()
1009 {
1010 struct sysinfo info;
1011 sysinfo(&info);
1012
1013 uint64_t total_ram = (uint64_t) info.totalram * (uint64_t) info.mem_unit;
1014
1015 /* We don't want to burn too much ram with the GPU. If the user has 4GiB
1016 * or less, we use at most half. If they have more than 4GiB, we use 3/4.
1017 */
1018 uint64_t available_ram;
1019 if (total_ram <= 4ull * 1024ull * 1024ull * 1024ull)
1020 available_ram = total_ram / 2;
1021 else
1022 available_ram = total_ram * 3 / 4;
1023
1024 return available_ram;
1025 }
1026
1027 void
1028 tu_GetPhysicalDeviceMemoryProperties(
1029 VkPhysicalDevice physicalDevice,
1030 VkPhysicalDeviceMemoryProperties *pMemoryProperties)
1031 {
1032 pMemoryProperties->memoryHeapCount = 1;
1033 pMemoryProperties->memoryHeaps[0].size = tu_get_system_heap_size();
1034 pMemoryProperties->memoryHeaps[0].flags = VK_MEMORY_HEAP_DEVICE_LOCAL_BIT;
1035
1036 pMemoryProperties->memoryTypeCount = 1;
1037 pMemoryProperties->memoryTypes[0].propertyFlags =
1038 VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT |
1039 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT |
1040 VK_MEMORY_PROPERTY_HOST_COHERENT_BIT;
1041 pMemoryProperties->memoryTypes[0].heapIndex = 0;
1042 }
1043
1044 void
1045 tu_GetPhysicalDeviceMemoryProperties2(
1046 VkPhysicalDevice physicalDevice,
1047 VkPhysicalDeviceMemoryProperties2 *pMemoryProperties)
1048 {
1049 return tu_GetPhysicalDeviceMemoryProperties(
1050 physicalDevice, &pMemoryProperties->memoryProperties);
1051 }
1052
1053 static VkResult
1054 tu_queue_init(struct tu_device *device,
1055 struct tu_queue *queue,
1056 uint32_t queue_family_index,
1057 int idx,
1058 VkDeviceQueueCreateFlags flags)
1059 {
1060 queue->_loader_data.loaderMagic = ICD_LOADER_MAGIC;
1061 queue->device = device;
1062 queue->queue_family_index = queue_family_index;
1063 queue->queue_idx = idx;
1064 queue->flags = flags;
1065
1066 int ret = tu_drm_submitqueue_new(device, 0, &queue->msm_queue_id);
1067 if (ret)
1068 return VK_ERROR_INITIALIZATION_FAILED;
1069
1070 tu_fence_init(&queue->submit_fence, false);
1071
1072 return VK_SUCCESS;
1073 }
1074
1075 static void
1076 tu_queue_finish(struct tu_queue *queue)
1077 {
1078 tu_fence_finish(&queue->submit_fence);
1079 tu_drm_submitqueue_close(queue->device, queue->msm_queue_id);
1080 }
1081
1082 static int
1083 tu_get_device_extension_index(const char *name)
1084 {
1085 for (unsigned i = 0; i < TU_DEVICE_EXTENSION_COUNT; ++i) {
1086 if (strcmp(name, tu_device_extensions[i].extensionName) == 0)
1087 return i;
1088 }
1089 return -1;
1090 }
1091
1092 struct PACKED bcolor_entry {
1093 uint32_t fp32[4];
1094 uint16_t ui16[4];
1095 int16_t si16[4];
1096 uint16_t fp16[4];
1097 uint16_t rgb565;
1098 uint16_t rgb5a1;
1099 uint16_t rgba4;
1100 uint8_t __pad0[2];
1101 uint8_t ui8[4];
1102 int8_t si8[4];
1103 uint32_t rgb10a2;
1104 uint32_t z24; /* also s8? */
1105 uint16_t srgb[4]; /* appears to duplicate fp16[], but clamped, used for srgb */
1106 uint8_t __pad1[56];
1107 } border_color[] = {
1108 [VK_BORDER_COLOR_FLOAT_TRANSPARENT_BLACK] = {},
1109 [VK_BORDER_COLOR_INT_TRANSPARENT_BLACK] = {},
1110 [VK_BORDER_COLOR_FLOAT_OPAQUE_BLACK] = {
1111 .fp32[3] = 0x3f800000,
1112 .ui16[3] = 0xffff,
1113 .si16[3] = 0x7fff,
1114 .fp16[3] = 0x3c00,
1115 .rgb5a1 = 0x8000,
1116 .rgba4 = 0xf000,
1117 .ui8[3] = 0xff,
1118 .si8[3] = 0x7f,
1119 .rgb10a2 = 0xc0000000,
1120 .srgb[3] = 0x3c00,
1121 },
1122 [VK_BORDER_COLOR_INT_OPAQUE_BLACK] = {
1123 .fp32[3] = 1,
1124 .fp16[3] = 1,
1125 },
1126 [VK_BORDER_COLOR_FLOAT_OPAQUE_WHITE] = {
1127 .fp32[0 ... 3] = 0x3f800000,
1128 .ui16[0 ... 3] = 0xffff,
1129 .si16[0 ... 3] = 0x7fff,
1130 .fp16[0 ... 3] = 0x3c00,
1131 .rgb565 = 0xffff,
1132 .rgb5a1 = 0xffff,
1133 .rgba4 = 0xffff,
1134 .ui8[0 ... 3] = 0xff,
1135 .si8[0 ... 3] = 0x7f,
1136 .rgb10a2 = 0xffffffff,
1137 .z24 = 0xffffff,
1138 .srgb[0 ... 3] = 0x3c00,
1139 },
1140 [VK_BORDER_COLOR_INT_OPAQUE_WHITE] = {
1141 .fp32[0 ... 3] = 1,
1142 .fp16[0 ... 3] = 1,
1143 },
1144 };
1145
1146
1147 VkResult
1148 tu_CreateDevice(VkPhysicalDevice physicalDevice,
1149 const VkDeviceCreateInfo *pCreateInfo,
1150 const VkAllocationCallbacks *pAllocator,
1151 VkDevice *pDevice)
1152 {
1153 TU_FROM_HANDLE(tu_physical_device, physical_device, physicalDevice);
1154 VkResult result;
1155 struct tu_device *device;
1156
1157 /* Check enabled features */
1158 if (pCreateInfo->pEnabledFeatures) {
1159 VkPhysicalDeviceFeatures supported_features;
1160 tu_GetPhysicalDeviceFeatures(physicalDevice, &supported_features);
1161 VkBool32 *supported_feature = (VkBool32 *) &supported_features;
1162 VkBool32 *enabled_feature = (VkBool32 *) pCreateInfo->pEnabledFeatures;
1163 unsigned num_features =
1164 sizeof(VkPhysicalDeviceFeatures) / sizeof(VkBool32);
1165 for (uint32_t i = 0; i < num_features; i++) {
1166 if (enabled_feature[i] && !supported_feature[i])
1167 return vk_error(physical_device->instance,
1168 VK_ERROR_FEATURE_NOT_PRESENT);
1169 }
1170 }
1171
1172 device = vk_zalloc2(&physical_device->instance->alloc, pAllocator,
1173 sizeof(*device), 8, VK_SYSTEM_ALLOCATION_SCOPE_DEVICE);
1174 if (!device)
1175 return vk_error(physical_device->instance, VK_ERROR_OUT_OF_HOST_MEMORY);
1176
1177 device->_loader_data.loaderMagic = ICD_LOADER_MAGIC;
1178 device->instance = physical_device->instance;
1179 device->physical_device = physical_device;
1180
1181 if (pAllocator)
1182 device->alloc = *pAllocator;
1183 else
1184 device->alloc = physical_device->instance->alloc;
1185
1186 for (uint32_t i = 0; i < pCreateInfo->enabledExtensionCount; i++) {
1187 const char *ext_name = pCreateInfo->ppEnabledExtensionNames[i];
1188 int index = tu_get_device_extension_index(ext_name);
1189 if (index < 0 ||
1190 !physical_device->supported_extensions.extensions[index]) {
1191 vk_free(&device->alloc, device);
1192 return vk_error(physical_device->instance,
1193 VK_ERROR_EXTENSION_NOT_PRESENT);
1194 }
1195
1196 device->enabled_extensions.extensions[index] = true;
1197 }
1198
1199 for (unsigned i = 0; i < pCreateInfo->queueCreateInfoCount; i++) {
1200 const VkDeviceQueueCreateInfo *queue_create =
1201 &pCreateInfo->pQueueCreateInfos[i];
1202 uint32_t qfi = queue_create->queueFamilyIndex;
1203 device->queues[qfi] = vk_alloc(
1204 &device->alloc, queue_create->queueCount * sizeof(struct tu_queue),
1205 8, VK_SYSTEM_ALLOCATION_SCOPE_DEVICE);
1206 if (!device->queues[qfi]) {
1207 result = VK_ERROR_OUT_OF_HOST_MEMORY;
1208 goto fail_queues;
1209 }
1210
1211 memset(device->queues[qfi], 0,
1212 queue_create->queueCount * sizeof(struct tu_queue));
1213
1214 device->queue_count[qfi] = queue_create->queueCount;
1215
1216 for (unsigned q = 0; q < queue_create->queueCount; q++) {
1217 result = tu_queue_init(device, &device->queues[qfi][q], qfi, q,
1218 queue_create->flags);
1219 if (result != VK_SUCCESS)
1220 goto fail_queues;
1221 }
1222 }
1223
1224 device->compiler = ir3_compiler_create(NULL, physical_device->gpu_id);
1225 if (!device->compiler)
1226 goto fail_queues;
1227
1228 #define VSC_DRAW_STRM_SIZE(pitch) ((pitch) * 32 + 0x100) /* extra size to store VSC_SIZE */
1229 #define VSC_PRIM_STRM_SIZE(pitch) ((pitch) * 32)
1230
1231 device->vsc_draw_strm_pitch = 0x440 * 4;
1232 device->vsc_prim_strm_pitch = 0x1040 * 4;
1233
1234 result = tu_bo_init_new(device, &device->vsc_draw_strm, VSC_DRAW_STRM_SIZE(device->vsc_draw_strm_pitch));
1235 if (result != VK_SUCCESS)
1236 goto fail_vsc_data;
1237
1238 result = tu_bo_init_new(device, &device->vsc_prim_strm, VSC_PRIM_STRM_SIZE(device->vsc_prim_strm_pitch));
1239 if (result != VK_SUCCESS)
1240 goto fail_vsc_data2;
1241
1242 STATIC_ASSERT(sizeof(struct bcolor_entry) == 128);
1243 result = tu_bo_init_new(device, &device->border_color, sizeof(border_color));
1244 if (result != VK_SUCCESS)
1245 goto fail_border_color;
1246
1247 result = tu_bo_map(device, &device->border_color);
1248 if (result != VK_SUCCESS)
1249 goto fail_border_color_map;
1250
1251 memcpy(device->border_color.map, border_color, sizeof(border_color));
1252
1253 VkPipelineCacheCreateInfo ci;
1254 ci.sType = VK_STRUCTURE_TYPE_PIPELINE_CACHE_CREATE_INFO;
1255 ci.pNext = NULL;
1256 ci.flags = 0;
1257 ci.pInitialData = NULL;
1258 ci.initialDataSize = 0;
1259 VkPipelineCache pc;
1260 result =
1261 tu_CreatePipelineCache(tu_device_to_handle(device), &ci, NULL, &pc);
1262 if (result != VK_SUCCESS)
1263 goto fail_pipeline_cache;
1264
1265 device->mem_cache = tu_pipeline_cache_from_handle(pc);
1266
1267 for (unsigned i = 0; i < ARRAY_SIZE(device->scratch_bos); i++)
1268 mtx_init(&device->scratch_bos[i].construct_mtx, mtx_plain);
1269
1270 *pDevice = tu_device_to_handle(device);
1271 return VK_SUCCESS;
1272
1273 fail_pipeline_cache:
1274 fail_border_color_map:
1275 tu_bo_finish(device, &device->border_color);
1276
1277 fail_border_color:
1278 tu_bo_finish(device, &device->vsc_prim_strm);
1279
1280 fail_vsc_data2:
1281 tu_bo_finish(device, &device->vsc_draw_strm);
1282
1283 fail_vsc_data:
1284 ralloc_free(device->compiler);
1285
1286 fail_queues:
1287 for (unsigned i = 0; i < TU_MAX_QUEUE_FAMILIES; i++) {
1288 for (unsigned q = 0; q < device->queue_count[i]; q++)
1289 tu_queue_finish(&device->queues[i][q]);
1290 if (device->queue_count[i])
1291 vk_free(&device->alloc, device->queues[i]);
1292 }
1293
1294 vk_free(&device->alloc, device);
1295 return result;
1296 }
1297
1298 void
1299 tu_DestroyDevice(VkDevice _device, const VkAllocationCallbacks *pAllocator)
1300 {
1301 TU_FROM_HANDLE(tu_device, device, _device);
1302
1303 if (!device)
1304 return;
1305
1306 tu_bo_finish(device, &device->vsc_draw_strm);
1307 tu_bo_finish(device, &device->vsc_prim_strm);
1308
1309 for (unsigned i = 0; i < TU_MAX_QUEUE_FAMILIES; i++) {
1310 for (unsigned q = 0; q < device->queue_count[i]; q++)
1311 tu_queue_finish(&device->queues[i][q]);
1312 if (device->queue_count[i])
1313 vk_free(&device->alloc, device->queues[i]);
1314 }
1315
1316 for (unsigned i = 0; i < ARRAY_SIZE(device->scratch_bos); i++) {
1317 if (device->scratch_bos[i].initialized)
1318 tu_bo_finish(device, &device->scratch_bos[i].bo);
1319 }
1320
1321 /* the compiler does not use pAllocator */
1322 ralloc_free(device->compiler);
1323
1324 VkPipelineCache pc = tu_pipeline_cache_to_handle(device->mem_cache);
1325 tu_DestroyPipelineCache(tu_device_to_handle(device), pc, NULL);
1326
1327 vk_free(&device->alloc, device);
1328 }
1329
1330 VkResult
1331 tu_get_scratch_bo(struct tu_device *dev, uint64_t size, struct tu_bo **bo)
1332 {
1333 unsigned size_log2 = MAX2(util_logbase2_ceil64(size), MIN_SCRATCH_BO_SIZE_LOG2);
1334 unsigned index = size_log2 - MIN_SCRATCH_BO_SIZE_LOG2;
1335 assert(index < ARRAY_SIZE(dev->scratch_bos));
1336
1337 for (unsigned i = index; i < ARRAY_SIZE(dev->scratch_bos); i++) {
1338 if (p_atomic_read(&dev->scratch_bos[i].initialized)) {
1339 /* Fast path: just return the already-allocated BO. */
1340 *bo = &dev->scratch_bos[i].bo;
1341 return VK_SUCCESS;
1342 }
1343 }
1344
1345 /* Slow path: actually allocate the BO. We take a lock because the process
1346 * of allocating it is slow, and we don't want to block the CPU while it
1347 * finishes.
1348 */
1349 mtx_lock(&dev->scratch_bos[index].construct_mtx);
1350
1351 /* Another thread may have allocated it already while we were waiting on
1352 * the lock. We need to check this in order to avoid double-allocating.
1353 */
1354 if (dev->scratch_bos[index].initialized) {
1355 mtx_unlock(&dev->scratch_bos[index].construct_mtx);
1356 *bo = &dev->scratch_bos[index].bo;
1357 return VK_SUCCESS;
1358 }
1359
1360 unsigned bo_size = 1ull << size_log2;
1361 VkResult result = tu_bo_init_new(dev, &dev->scratch_bos[index].bo, bo_size);
1362 if (result != VK_SUCCESS) {
1363 mtx_unlock(&dev->scratch_bos[index].construct_mtx);
1364 return result;
1365 }
1366
1367 p_atomic_set(&dev->scratch_bos[index].initialized, true);
1368
1369 mtx_unlock(&dev->scratch_bos[index].construct_mtx);
1370
1371 *bo = &dev->scratch_bos[index].bo;
1372 return VK_SUCCESS;
1373 }
1374
1375 VkResult
1376 tu_EnumerateInstanceLayerProperties(uint32_t *pPropertyCount,
1377 VkLayerProperties *pProperties)
1378 {
1379 *pPropertyCount = 0;
1380 return VK_SUCCESS;
1381 }
1382
1383 VkResult
1384 tu_EnumerateDeviceLayerProperties(VkPhysicalDevice physicalDevice,
1385 uint32_t *pPropertyCount,
1386 VkLayerProperties *pProperties)
1387 {
1388 *pPropertyCount = 0;
1389 return VK_SUCCESS;
1390 }
1391
1392 void
1393 tu_GetDeviceQueue2(VkDevice _device,
1394 const VkDeviceQueueInfo2 *pQueueInfo,
1395 VkQueue *pQueue)
1396 {
1397 TU_FROM_HANDLE(tu_device, device, _device);
1398 struct tu_queue *queue;
1399
1400 queue =
1401 &device->queues[pQueueInfo->queueFamilyIndex][pQueueInfo->queueIndex];
1402 if (pQueueInfo->flags != queue->flags) {
1403 /* From the Vulkan 1.1.70 spec:
1404 *
1405 * "The queue returned by vkGetDeviceQueue2 must have the same
1406 * flags value from this structure as that used at device
1407 * creation time in a VkDeviceQueueCreateInfo instance. If no
1408 * matching flags were specified at device creation time then
1409 * pQueue will return VK_NULL_HANDLE."
1410 */
1411 *pQueue = VK_NULL_HANDLE;
1412 return;
1413 }
1414
1415 *pQueue = tu_queue_to_handle(queue);
1416 }
1417
1418 void
1419 tu_GetDeviceQueue(VkDevice _device,
1420 uint32_t queueFamilyIndex,
1421 uint32_t queueIndex,
1422 VkQueue *pQueue)
1423 {
1424 const VkDeviceQueueInfo2 info =
1425 (VkDeviceQueueInfo2) { .sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_INFO_2,
1426 .queueFamilyIndex = queueFamilyIndex,
1427 .queueIndex = queueIndex };
1428
1429 tu_GetDeviceQueue2(_device, &info, pQueue);
1430 }
1431
1432 VkResult
1433 tu_QueueSubmit(VkQueue _queue,
1434 uint32_t submitCount,
1435 const VkSubmitInfo *pSubmits,
1436 VkFence _fence)
1437 {
1438 TU_FROM_HANDLE(tu_queue, queue, _queue);
1439
1440 for (uint32_t i = 0; i < submitCount; ++i) {
1441 const VkSubmitInfo *submit = pSubmits + i;
1442 const bool last_submit = (i == submitCount - 1);
1443 struct tu_bo_list bo_list;
1444 tu_bo_list_init(&bo_list);
1445
1446 uint32_t entry_count = 0;
1447 for (uint32_t j = 0; j < submit->commandBufferCount; ++j) {
1448 TU_FROM_HANDLE(tu_cmd_buffer, cmdbuf, submit->pCommandBuffers[j]);
1449 entry_count += cmdbuf->cs.entry_count;
1450 }
1451
1452 struct drm_msm_gem_submit_cmd cmds[entry_count];
1453 uint32_t entry_idx = 0;
1454 for (uint32_t j = 0; j < submit->commandBufferCount; ++j) {
1455 TU_FROM_HANDLE(tu_cmd_buffer, cmdbuf, submit->pCommandBuffers[j]);
1456 struct tu_cs *cs = &cmdbuf->cs;
1457 for (unsigned i = 0; i < cs->entry_count; ++i, ++entry_idx) {
1458 cmds[entry_idx].type = MSM_SUBMIT_CMD_BUF;
1459 cmds[entry_idx].submit_idx =
1460 tu_bo_list_add(&bo_list, cs->entries[i].bo,
1461 MSM_SUBMIT_BO_READ | MSM_SUBMIT_BO_DUMP);
1462 cmds[entry_idx].submit_offset = cs->entries[i].offset;
1463 cmds[entry_idx].size = cs->entries[i].size;
1464 cmds[entry_idx].pad = 0;
1465 cmds[entry_idx].nr_relocs = 0;
1466 cmds[entry_idx].relocs = 0;
1467 }
1468
1469 tu_bo_list_merge(&bo_list, &cmdbuf->bo_list);
1470 }
1471
1472 uint32_t flags = MSM_PIPE_3D0;
1473 if (last_submit) {
1474 flags |= MSM_SUBMIT_FENCE_FD_OUT;
1475 }
1476
1477 struct drm_msm_gem_submit req = {
1478 .flags = flags,
1479 .queueid = queue->msm_queue_id,
1480 .bos = (uint64_t)(uintptr_t) bo_list.bo_infos,
1481 .nr_bos = bo_list.count,
1482 .cmds = (uint64_t)(uintptr_t)cmds,
1483 .nr_cmds = entry_count,
1484 };
1485
1486 int ret = drmCommandWriteRead(queue->device->physical_device->local_fd,
1487 DRM_MSM_GEM_SUBMIT,
1488 &req, sizeof(req));
1489 if (ret) {
1490 fprintf(stderr, "submit failed: %s\n", strerror(errno));
1491 abort();
1492 }
1493
1494 tu_bo_list_destroy(&bo_list);
1495
1496 if (last_submit) {
1497 /* no need to merge fences as queue execution is serialized */
1498 tu_fence_update_fd(&queue->submit_fence, req.fence_fd);
1499 }
1500 }
1501
1502 if (_fence != VK_NULL_HANDLE) {
1503 TU_FROM_HANDLE(tu_fence, fence, _fence);
1504 tu_fence_copy(fence, &queue->submit_fence);
1505 }
1506
1507 return VK_SUCCESS;
1508 }
1509
1510 VkResult
1511 tu_QueueWaitIdle(VkQueue _queue)
1512 {
1513 TU_FROM_HANDLE(tu_queue, queue, _queue);
1514
1515 tu_fence_wait_idle(&queue->submit_fence);
1516
1517 return VK_SUCCESS;
1518 }
1519
1520 VkResult
1521 tu_DeviceWaitIdle(VkDevice _device)
1522 {
1523 TU_FROM_HANDLE(tu_device, device, _device);
1524
1525 for (unsigned i = 0; i < TU_MAX_QUEUE_FAMILIES; i++) {
1526 for (unsigned q = 0; q < device->queue_count[i]; q++) {
1527 tu_QueueWaitIdle(tu_queue_to_handle(&device->queues[i][q]));
1528 }
1529 }
1530 return VK_SUCCESS;
1531 }
1532
1533 VkResult
1534 tu_ImportSemaphoreFdKHR(VkDevice _device,
1535 const VkImportSemaphoreFdInfoKHR *pImportSemaphoreFdInfo)
1536 {
1537 tu_stub();
1538
1539 return VK_SUCCESS;
1540 }
1541
1542 VkResult
1543 tu_GetSemaphoreFdKHR(VkDevice _device,
1544 const VkSemaphoreGetFdInfoKHR *pGetFdInfo,
1545 int *pFd)
1546 {
1547 tu_stub();
1548
1549 return VK_SUCCESS;
1550 }
1551
1552 VkResult
1553 tu_ImportFenceFdKHR(VkDevice _device,
1554 const VkImportFenceFdInfoKHR *pImportFenceFdInfo)
1555 {
1556 tu_stub();
1557
1558 return VK_SUCCESS;
1559 }
1560
1561 VkResult
1562 tu_GetFenceFdKHR(VkDevice _device,
1563 const VkFenceGetFdInfoKHR *pGetFdInfo,
1564 int *pFd)
1565 {
1566 tu_stub();
1567
1568 return VK_SUCCESS;
1569 }
1570
1571 VkResult
1572 tu_EnumerateInstanceExtensionProperties(const char *pLayerName,
1573 uint32_t *pPropertyCount,
1574 VkExtensionProperties *pProperties)
1575 {
1576 VK_OUTARRAY_MAKE(out, pProperties, pPropertyCount);
1577
1578 /* We spport no lyaers */
1579 if (pLayerName)
1580 return vk_error(NULL, VK_ERROR_LAYER_NOT_PRESENT);
1581
1582 for (int i = 0; i < TU_INSTANCE_EXTENSION_COUNT; i++) {
1583 if (tu_instance_extensions_supported.extensions[i]) {
1584 vk_outarray_append(&out, prop) { *prop = tu_instance_extensions[i]; }
1585 }
1586 }
1587
1588 return vk_outarray_status(&out);
1589 }
1590
1591 VkResult
1592 tu_EnumerateDeviceExtensionProperties(VkPhysicalDevice physicalDevice,
1593 const char *pLayerName,
1594 uint32_t *pPropertyCount,
1595 VkExtensionProperties *pProperties)
1596 {
1597 /* We spport no lyaers */
1598 TU_FROM_HANDLE(tu_physical_device, device, physicalDevice);
1599 VK_OUTARRAY_MAKE(out, pProperties, pPropertyCount);
1600
1601 /* We spport no lyaers */
1602 if (pLayerName)
1603 return vk_error(NULL, VK_ERROR_LAYER_NOT_PRESENT);
1604
1605 for (int i = 0; i < TU_DEVICE_EXTENSION_COUNT; i++) {
1606 if (device->supported_extensions.extensions[i]) {
1607 vk_outarray_append(&out, prop) { *prop = tu_device_extensions[i]; }
1608 }
1609 }
1610
1611 return vk_outarray_status(&out);
1612 }
1613
1614 PFN_vkVoidFunction
1615 tu_GetInstanceProcAddr(VkInstance _instance, const char *pName)
1616 {
1617 TU_FROM_HANDLE(tu_instance, instance, _instance);
1618
1619 return tu_lookup_entrypoint_checked(
1620 pName, instance ? instance->api_version : 0,
1621 instance ? &instance->enabled_extensions : NULL, NULL);
1622 }
1623
1624 /* The loader wants us to expose a second GetInstanceProcAddr function
1625 * to work around certain LD_PRELOAD issues seen in apps.
1626 */
1627 PUBLIC
1628 VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL
1629 vk_icdGetInstanceProcAddr(VkInstance instance, const char *pName);
1630
1631 PUBLIC
1632 VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL
1633 vk_icdGetInstanceProcAddr(VkInstance instance, const char *pName)
1634 {
1635 return tu_GetInstanceProcAddr(instance, pName);
1636 }
1637
1638 PFN_vkVoidFunction
1639 tu_GetDeviceProcAddr(VkDevice _device, const char *pName)
1640 {
1641 TU_FROM_HANDLE(tu_device, device, _device);
1642
1643 return tu_lookup_entrypoint_checked(pName, device->instance->api_version,
1644 &device->instance->enabled_extensions,
1645 &device->enabled_extensions);
1646 }
1647
1648 static VkResult
1649 tu_alloc_memory(struct tu_device *device,
1650 const VkMemoryAllocateInfo *pAllocateInfo,
1651 const VkAllocationCallbacks *pAllocator,
1652 VkDeviceMemory *pMem)
1653 {
1654 struct tu_device_memory *mem;
1655 VkResult result;
1656
1657 assert(pAllocateInfo->sType == VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO);
1658
1659 if (pAllocateInfo->allocationSize == 0) {
1660 /* Apparently, this is allowed */
1661 *pMem = VK_NULL_HANDLE;
1662 return VK_SUCCESS;
1663 }
1664
1665 mem = vk_alloc2(&device->alloc, pAllocator, sizeof(*mem), 8,
1666 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
1667 if (mem == NULL)
1668 return vk_error(device->instance, VK_ERROR_OUT_OF_HOST_MEMORY);
1669
1670 const VkImportMemoryFdInfoKHR *fd_info =
1671 vk_find_struct_const(pAllocateInfo->pNext, IMPORT_MEMORY_FD_INFO_KHR);
1672 if (fd_info && !fd_info->handleType)
1673 fd_info = NULL;
1674
1675 if (fd_info) {
1676 assert(fd_info->handleType ==
1677 VK_EXTERNAL_MEMORY_HANDLE_TYPE_OPAQUE_FD_BIT ||
1678 fd_info->handleType ==
1679 VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_EXT);
1680
1681 /*
1682 * TODO Importing the same fd twice gives us the same handle without
1683 * reference counting. We need to maintain a per-instance handle-to-bo
1684 * table and add reference count to tu_bo.
1685 */
1686 result = tu_bo_init_dmabuf(device, &mem->bo,
1687 pAllocateInfo->allocationSize, fd_info->fd);
1688 if (result == VK_SUCCESS) {
1689 /* take ownership and close the fd */
1690 close(fd_info->fd);
1691 }
1692 } else {
1693 result =
1694 tu_bo_init_new(device, &mem->bo, pAllocateInfo->allocationSize);
1695 }
1696
1697 if (result != VK_SUCCESS) {
1698 vk_free2(&device->alloc, pAllocator, mem);
1699 return result;
1700 }
1701
1702 mem->size = pAllocateInfo->allocationSize;
1703 mem->type_index = pAllocateInfo->memoryTypeIndex;
1704
1705 mem->map = NULL;
1706 mem->user_ptr = NULL;
1707
1708 *pMem = tu_device_memory_to_handle(mem);
1709
1710 return VK_SUCCESS;
1711 }
1712
1713 VkResult
1714 tu_AllocateMemory(VkDevice _device,
1715 const VkMemoryAllocateInfo *pAllocateInfo,
1716 const VkAllocationCallbacks *pAllocator,
1717 VkDeviceMemory *pMem)
1718 {
1719 TU_FROM_HANDLE(tu_device, device, _device);
1720 return tu_alloc_memory(device, pAllocateInfo, pAllocator, pMem);
1721 }
1722
1723 void
1724 tu_FreeMemory(VkDevice _device,
1725 VkDeviceMemory _mem,
1726 const VkAllocationCallbacks *pAllocator)
1727 {
1728 TU_FROM_HANDLE(tu_device, device, _device);
1729 TU_FROM_HANDLE(tu_device_memory, mem, _mem);
1730
1731 if (mem == NULL)
1732 return;
1733
1734 tu_bo_finish(device, &mem->bo);
1735 vk_free2(&device->alloc, pAllocator, mem);
1736 }
1737
1738 VkResult
1739 tu_MapMemory(VkDevice _device,
1740 VkDeviceMemory _memory,
1741 VkDeviceSize offset,
1742 VkDeviceSize size,
1743 VkMemoryMapFlags flags,
1744 void **ppData)
1745 {
1746 TU_FROM_HANDLE(tu_device, device, _device);
1747 TU_FROM_HANDLE(tu_device_memory, mem, _memory);
1748 VkResult result;
1749
1750 if (mem == NULL) {
1751 *ppData = NULL;
1752 return VK_SUCCESS;
1753 }
1754
1755 if (mem->user_ptr) {
1756 *ppData = mem->user_ptr;
1757 } else if (!mem->map) {
1758 result = tu_bo_map(device, &mem->bo);
1759 if (result != VK_SUCCESS)
1760 return result;
1761 *ppData = mem->map = mem->bo.map;
1762 } else
1763 *ppData = mem->map;
1764
1765 if (*ppData) {
1766 *ppData += offset;
1767 return VK_SUCCESS;
1768 }
1769
1770 return vk_error(device->instance, VK_ERROR_MEMORY_MAP_FAILED);
1771 }
1772
1773 void
1774 tu_UnmapMemory(VkDevice _device, VkDeviceMemory _memory)
1775 {
1776 /* I do not see any unmapping done by the freedreno Gallium driver. */
1777 }
1778
1779 VkResult
1780 tu_FlushMappedMemoryRanges(VkDevice _device,
1781 uint32_t memoryRangeCount,
1782 const VkMappedMemoryRange *pMemoryRanges)
1783 {
1784 return VK_SUCCESS;
1785 }
1786
1787 VkResult
1788 tu_InvalidateMappedMemoryRanges(VkDevice _device,
1789 uint32_t memoryRangeCount,
1790 const VkMappedMemoryRange *pMemoryRanges)
1791 {
1792 return VK_SUCCESS;
1793 }
1794
1795 void
1796 tu_GetBufferMemoryRequirements(VkDevice _device,
1797 VkBuffer _buffer,
1798 VkMemoryRequirements *pMemoryRequirements)
1799 {
1800 TU_FROM_HANDLE(tu_buffer, buffer, _buffer);
1801
1802 pMemoryRequirements->memoryTypeBits = 1;
1803 pMemoryRequirements->alignment = 64;
1804 pMemoryRequirements->size =
1805 align64(buffer->size, pMemoryRequirements->alignment);
1806 }
1807
1808 void
1809 tu_GetBufferMemoryRequirements2(
1810 VkDevice device,
1811 const VkBufferMemoryRequirementsInfo2 *pInfo,
1812 VkMemoryRequirements2 *pMemoryRequirements)
1813 {
1814 tu_GetBufferMemoryRequirements(device, pInfo->buffer,
1815 &pMemoryRequirements->memoryRequirements);
1816 }
1817
1818 void
1819 tu_GetImageMemoryRequirements(VkDevice _device,
1820 VkImage _image,
1821 VkMemoryRequirements *pMemoryRequirements)
1822 {
1823 TU_FROM_HANDLE(tu_image, image, _image);
1824
1825 pMemoryRequirements->memoryTypeBits = 1;
1826 pMemoryRequirements->size = image->layout.size;
1827 pMemoryRequirements->alignment = image->layout.base_align;
1828 }
1829
1830 void
1831 tu_GetImageMemoryRequirements2(VkDevice device,
1832 const VkImageMemoryRequirementsInfo2 *pInfo,
1833 VkMemoryRequirements2 *pMemoryRequirements)
1834 {
1835 tu_GetImageMemoryRequirements(device, pInfo->image,
1836 &pMemoryRequirements->memoryRequirements);
1837 }
1838
1839 void
1840 tu_GetImageSparseMemoryRequirements(
1841 VkDevice device,
1842 VkImage image,
1843 uint32_t *pSparseMemoryRequirementCount,
1844 VkSparseImageMemoryRequirements *pSparseMemoryRequirements)
1845 {
1846 tu_stub();
1847 }
1848
1849 void
1850 tu_GetImageSparseMemoryRequirements2(
1851 VkDevice device,
1852 const VkImageSparseMemoryRequirementsInfo2 *pInfo,
1853 uint32_t *pSparseMemoryRequirementCount,
1854 VkSparseImageMemoryRequirements2 *pSparseMemoryRequirements)
1855 {
1856 tu_stub();
1857 }
1858
1859 void
1860 tu_GetDeviceMemoryCommitment(VkDevice device,
1861 VkDeviceMemory memory,
1862 VkDeviceSize *pCommittedMemoryInBytes)
1863 {
1864 *pCommittedMemoryInBytes = 0;
1865 }
1866
1867 VkResult
1868 tu_BindBufferMemory2(VkDevice device,
1869 uint32_t bindInfoCount,
1870 const VkBindBufferMemoryInfo *pBindInfos)
1871 {
1872 for (uint32_t i = 0; i < bindInfoCount; ++i) {
1873 TU_FROM_HANDLE(tu_device_memory, mem, pBindInfos[i].memory);
1874 TU_FROM_HANDLE(tu_buffer, buffer, pBindInfos[i].buffer);
1875
1876 if (mem) {
1877 buffer->bo = &mem->bo;
1878 buffer->bo_offset = pBindInfos[i].memoryOffset;
1879 } else {
1880 buffer->bo = NULL;
1881 }
1882 }
1883 return VK_SUCCESS;
1884 }
1885
1886 VkResult
1887 tu_BindBufferMemory(VkDevice device,
1888 VkBuffer buffer,
1889 VkDeviceMemory memory,
1890 VkDeviceSize memoryOffset)
1891 {
1892 const VkBindBufferMemoryInfo info = {
1893 .sType = VK_STRUCTURE_TYPE_BIND_BUFFER_MEMORY_INFO,
1894 .buffer = buffer,
1895 .memory = memory,
1896 .memoryOffset = memoryOffset
1897 };
1898
1899 return tu_BindBufferMemory2(device, 1, &info);
1900 }
1901
1902 VkResult
1903 tu_BindImageMemory2(VkDevice device,
1904 uint32_t bindInfoCount,
1905 const VkBindImageMemoryInfo *pBindInfos)
1906 {
1907 for (uint32_t i = 0; i < bindInfoCount; ++i) {
1908 TU_FROM_HANDLE(tu_image, image, pBindInfos[i].image);
1909 TU_FROM_HANDLE(tu_device_memory, mem, pBindInfos[i].memory);
1910
1911 if (mem) {
1912 image->bo = &mem->bo;
1913 image->bo_offset = pBindInfos[i].memoryOffset;
1914 } else {
1915 image->bo = NULL;
1916 image->bo_offset = 0;
1917 }
1918 }
1919
1920 return VK_SUCCESS;
1921 }
1922
1923 VkResult
1924 tu_BindImageMemory(VkDevice device,
1925 VkImage image,
1926 VkDeviceMemory memory,
1927 VkDeviceSize memoryOffset)
1928 {
1929 const VkBindImageMemoryInfo info = {
1930 .sType = VK_STRUCTURE_TYPE_BIND_BUFFER_MEMORY_INFO,
1931 .image = image,
1932 .memory = memory,
1933 .memoryOffset = memoryOffset
1934 };
1935
1936 return tu_BindImageMemory2(device, 1, &info);
1937 }
1938
1939 VkResult
1940 tu_QueueBindSparse(VkQueue _queue,
1941 uint32_t bindInfoCount,
1942 const VkBindSparseInfo *pBindInfo,
1943 VkFence _fence)
1944 {
1945 return VK_SUCCESS;
1946 }
1947
1948 // Queue semaphore functions
1949
1950 VkResult
1951 tu_CreateSemaphore(VkDevice _device,
1952 const VkSemaphoreCreateInfo *pCreateInfo,
1953 const VkAllocationCallbacks *pAllocator,
1954 VkSemaphore *pSemaphore)
1955 {
1956 TU_FROM_HANDLE(tu_device, device, _device);
1957
1958 struct tu_semaphore *sem =
1959 vk_alloc2(&device->alloc, pAllocator, sizeof(*sem), 8,
1960 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
1961 if (!sem)
1962 return vk_error(device->instance, VK_ERROR_OUT_OF_HOST_MEMORY);
1963
1964 *pSemaphore = tu_semaphore_to_handle(sem);
1965 return VK_SUCCESS;
1966 }
1967
1968 void
1969 tu_DestroySemaphore(VkDevice _device,
1970 VkSemaphore _semaphore,
1971 const VkAllocationCallbacks *pAllocator)
1972 {
1973 TU_FROM_HANDLE(tu_device, device, _device);
1974 TU_FROM_HANDLE(tu_semaphore, sem, _semaphore);
1975 if (!_semaphore)
1976 return;
1977
1978 vk_free2(&device->alloc, pAllocator, sem);
1979 }
1980
1981 VkResult
1982 tu_CreateEvent(VkDevice _device,
1983 const VkEventCreateInfo *pCreateInfo,
1984 const VkAllocationCallbacks *pAllocator,
1985 VkEvent *pEvent)
1986 {
1987 TU_FROM_HANDLE(tu_device, device, _device);
1988 struct tu_event *event =
1989 vk_alloc2(&device->alloc, pAllocator, sizeof(*event), 8,
1990 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
1991
1992 if (!event)
1993 return vk_error(device->instance, VK_ERROR_OUT_OF_HOST_MEMORY);
1994
1995 VkResult result = tu_bo_init_new(device, &event->bo, 0x1000);
1996 if (result != VK_SUCCESS)
1997 goto fail_alloc;
1998
1999 result = tu_bo_map(device, &event->bo);
2000 if (result != VK_SUCCESS)
2001 goto fail_map;
2002
2003 *pEvent = tu_event_to_handle(event);
2004
2005 return VK_SUCCESS;
2006
2007 fail_map:
2008 tu_bo_finish(device, &event->bo);
2009 fail_alloc:
2010 vk_free2(&device->alloc, pAllocator, event);
2011 return vk_error(device->instance, VK_ERROR_OUT_OF_HOST_MEMORY);
2012 }
2013
2014 void
2015 tu_DestroyEvent(VkDevice _device,
2016 VkEvent _event,
2017 const VkAllocationCallbacks *pAllocator)
2018 {
2019 TU_FROM_HANDLE(tu_device, device, _device);
2020 TU_FROM_HANDLE(tu_event, event, _event);
2021
2022 if (!event)
2023 return;
2024
2025 tu_bo_finish(device, &event->bo);
2026 vk_free2(&device->alloc, pAllocator, event);
2027 }
2028
2029 VkResult
2030 tu_GetEventStatus(VkDevice _device, VkEvent _event)
2031 {
2032 TU_FROM_HANDLE(tu_event, event, _event);
2033
2034 if (*(uint64_t*) event->bo.map == 1)
2035 return VK_EVENT_SET;
2036 return VK_EVENT_RESET;
2037 }
2038
2039 VkResult
2040 tu_SetEvent(VkDevice _device, VkEvent _event)
2041 {
2042 TU_FROM_HANDLE(tu_event, event, _event);
2043 *(uint64_t*) event->bo.map = 1;
2044
2045 return VK_SUCCESS;
2046 }
2047
2048 VkResult
2049 tu_ResetEvent(VkDevice _device, VkEvent _event)
2050 {
2051 TU_FROM_HANDLE(tu_event, event, _event);
2052 *(uint64_t*) event->bo.map = 0;
2053
2054 return VK_SUCCESS;
2055 }
2056
2057 VkResult
2058 tu_CreateBuffer(VkDevice _device,
2059 const VkBufferCreateInfo *pCreateInfo,
2060 const VkAllocationCallbacks *pAllocator,
2061 VkBuffer *pBuffer)
2062 {
2063 TU_FROM_HANDLE(tu_device, device, _device);
2064 struct tu_buffer *buffer;
2065
2066 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO);
2067
2068 buffer = vk_alloc2(&device->alloc, pAllocator, sizeof(*buffer), 8,
2069 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
2070 if (buffer == NULL)
2071 return vk_error(device->instance, VK_ERROR_OUT_OF_HOST_MEMORY);
2072
2073 buffer->size = pCreateInfo->size;
2074 buffer->usage = pCreateInfo->usage;
2075 buffer->flags = pCreateInfo->flags;
2076
2077 *pBuffer = tu_buffer_to_handle(buffer);
2078
2079 return VK_SUCCESS;
2080 }
2081
2082 void
2083 tu_DestroyBuffer(VkDevice _device,
2084 VkBuffer _buffer,
2085 const VkAllocationCallbacks *pAllocator)
2086 {
2087 TU_FROM_HANDLE(tu_device, device, _device);
2088 TU_FROM_HANDLE(tu_buffer, buffer, _buffer);
2089
2090 if (!buffer)
2091 return;
2092
2093 vk_free2(&device->alloc, pAllocator, buffer);
2094 }
2095
2096 VkResult
2097 tu_CreateFramebuffer(VkDevice _device,
2098 const VkFramebufferCreateInfo *pCreateInfo,
2099 const VkAllocationCallbacks *pAllocator,
2100 VkFramebuffer *pFramebuffer)
2101 {
2102 TU_FROM_HANDLE(tu_device, device, _device);
2103 struct tu_framebuffer *framebuffer;
2104
2105 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO);
2106
2107 size_t size = sizeof(*framebuffer) + sizeof(struct tu_attachment_info) *
2108 pCreateInfo->attachmentCount;
2109 framebuffer = vk_alloc2(&device->alloc, pAllocator, size, 8,
2110 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
2111 if (framebuffer == NULL)
2112 return vk_error(device->instance, VK_ERROR_OUT_OF_HOST_MEMORY);
2113
2114 framebuffer->attachment_count = pCreateInfo->attachmentCount;
2115 framebuffer->width = pCreateInfo->width;
2116 framebuffer->height = pCreateInfo->height;
2117 framebuffer->layers = pCreateInfo->layers;
2118 for (uint32_t i = 0; i < pCreateInfo->attachmentCount; i++) {
2119 VkImageView _iview = pCreateInfo->pAttachments[i];
2120 struct tu_image_view *iview = tu_image_view_from_handle(_iview);
2121 framebuffer->attachments[i].attachment = iview;
2122 }
2123
2124 *pFramebuffer = tu_framebuffer_to_handle(framebuffer);
2125 return VK_SUCCESS;
2126 }
2127
2128 void
2129 tu_DestroyFramebuffer(VkDevice _device,
2130 VkFramebuffer _fb,
2131 const VkAllocationCallbacks *pAllocator)
2132 {
2133 TU_FROM_HANDLE(tu_device, device, _device);
2134 TU_FROM_HANDLE(tu_framebuffer, fb, _fb);
2135
2136 if (!fb)
2137 return;
2138 vk_free2(&device->alloc, pAllocator, fb);
2139 }
2140
2141 static void
2142 tu_init_sampler(struct tu_device *device,
2143 struct tu_sampler *sampler,
2144 const VkSamplerCreateInfo *pCreateInfo)
2145 {
2146 const struct VkSamplerReductionModeCreateInfo *reduction =
2147 vk_find_struct_const(pCreateInfo->pNext, SAMPLER_REDUCTION_MODE_CREATE_INFO);
2148 const struct VkSamplerYcbcrConversionInfo *ycbcr_conversion =
2149 vk_find_struct_const(pCreateInfo->pNext, SAMPLER_YCBCR_CONVERSION_INFO);
2150
2151 unsigned aniso = pCreateInfo->anisotropyEnable ?
2152 util_last_bit(MIN2((uint32_t)pCreateInfo->maxAnisotropy >> 1, 8)) : 0;
2153 bool miplinear = (pCreateInfo->mipmapMode == VK_SAMPLER_MIPMAP_MODE_LINEAR);
2154 float min_lod = CLAMP(pCreateInfo->minLod, 0.0f, 4095.0f / 256.0f);
2155 float max_lod = CLAMP(pCreateInfo->maxLod, 0.0f, 4095.0f / 256.0f);
2156
2157 sampler->descriptor[0] =
2158 COND(miplinear, A6XX_TEX_SAMP_0_MIPFILTER_LINEAR_NEAR) |
2159 A6XX_TEX_SAMP_0_XY_MAG(tu6_tex_filter(pCreateInfo->magFilter, aniso)) |
2160 A6XX_TEX_SAMP_0_XY_MIN(tu6_tex_filter(pCreateInfo->minFilter, aniso)) |
2161 A6XX_TEX_SAMP_0_ANISO(aniso) |
2162 A6XX_TEX_SAMP_0_WRAP_S(tu6_tex_wrap(pCreateInfo->addressModeU)) |
2163 A6XX_TEX_SAMP_0_WRAP_T(tu6_tex_wrap(pCreateInfo->addressModeV)) |
2164 A6XX_TEX_SAMP_0_WRAP_R(tu6_tex_wrap(pCreateInfo->addressModeW)) |
2165 A6XX_TEX_SAMP_0_LOD_BIAS(pCreateInfo->mipLodBias);
2166 sampler->descriptor[1] =
2167 /* COND(!cso->seamless_cube_map, A6XX_TEX_SAMP_1_CUBEMAPSEAMLESSFILTOFF) | */
2168 COND(pCreateInfo->unnormalizedCoordinates, A6XX_TEX_SAMP_1_UNNORM_COORDS) |
2169 A6XX_TEX_SAMP_1_MIN_LOD(min_lod) |
2170 A6XX_TEX_SAMP_1_MAX_LOD(max_lod) |
2171 COND(pCreateInfo->compareEnable,
2172 A6XX_TEX_SAMP_1_COMPARE_FUNC(tu6_compare_func(pCreateInfo->compareOp)));
2173 /* This is an offset into the border_color BO, which we fill with all the
2174 * possible Vulkan border colors in the correct order, so we can just use
2175 * the Vulkan enum with no translation necessary.
2176 */
2177 sampler->descriptor[2] =
2178 A6XX_TEX_SAMP_2_BCOLOR_OFFSET((unsigned) pCreateInfo->borderColor *
2179 sizeof(struct bcolor_entry));
2180 sampler->descriptor[3] = 0;
2181
2182 if (reduction) {
2183 sampler->descriptor[2] |= A6XX_TEX_SAMP_2_REDUCTION_MODE(
2184 tu6_reduction_mode(reduction->reductionMode));
2185 }
2186
2187 sampler->ycbcr_sampler = ycbcr_conversion ?
2188 tu_sampler_ycbcr_conversion_from_handle(ycbcr_conversion->conversion) : NULL;
2189
2190 if (sampler->ycbcr_sampler &&
2191 sampler->ycbcr_sampler->chroma_filter == VK_FILTER_LINEAR) {
2192 sampler->descriptor[2] |= A6XX_TEX_SAMP_2_CHROMA_LINEAR;
2193 }
2194
2195 /* TODO:
2196 * A6XX_TEX_SAMP_1_MIPFILTER_LINEAR_FAR disables mipmapping, but vk has no NONE mipfilter?
2197 */
2198 }
2199
2200 VkResult
2201 tu_CreateSampler(VkDevice _device,
2202 const VkSamplerCreateInfo *pCreateInfo,
2203 const VkAllocationCallbacks *pAllocator,
2204 VkSampler *pSampler)
2205 {
2206 TU_FROM_HANDLE(tu_device, device, _device);
2207 struct tu_sampler *sampler;
2208
2209 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO);
2210
2211 sampler = vk_alloc2(&device->alloc, pAllocator, sizeof(*sampler), 8,
2212 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
2213 if (!sampler)
2214 return vk_error(device->instance, VK_ERROR_OUT_OF_HOST_MEMORY);
2215
2216 tu_init_sampler(device, sampler, pCreateInfo);
2217 *pSampler = tu_sampler_to_handle(sampler);
2218
2219 return VK_SUCCESS;
2220 }
2221
2222 void
2223 tu_DestroySampler(VkDevice _device,
2224 VkSampler _sampler,
2225 const VkAllocationCallbacks *pAllocator)
2226 {
2227 TU_FROM_HANDLE(tu_device, device, _device);
2228 TU_FROM_HANDLE(tu_sampler, sampler, _sampler);
2229
2230 if (!sampler)
2231 return;
2232 vk_free2(&device->alloc, pAllocator, sampler);
2233 }
2234
2235 /* vk_icd.h does not declare this function, so we declare it here to
2236 * suppress Wmissing-prototypes.
2237 */
2238 PUBLIC VKAPI_ATTR VkResult VKAPI_CALL
2239 vk_icdNegotiateLoaderICDInterfaceVersion(uint32_t *pSupportedVersion);
2240
2241 PUBLIC VKAPI_ATTR VkResult VKAPI_CALL
2242 vk_icdNegotiateLoaderICDInterfaceVersion(uint32_t *pSupportedVersion)
2243 {
2244 /* For the full details on loader interface versioning, see
2245 * <https://github.com/KhronosGroup/Vulkan-LoaderAndValidationLayers/blob/master/loader/LoaderAndLayerInterface.md>.
2246 * What follows is a condensed summary, to help you navigate the large and
2247 * confusing official doc.
2248 *
2249 * - Loader interface v0 is incompatible with later versions. We don't
2250 * support it.
2251 *
2252 * - In loader interface v1:
2253 * - The first ICD entrypoint called by the loader is
2254 * vk_icdGetInstanceProcAddr(). The ICD must statically expose this
2255 * entrypoint.
2256 * - The ICD must statically expose no other Vulkan symbol unless it
2257 * is linked with -Bsymbolic.
2258 * - Each dispatchable Vulkan handle created by the ICD must be
2259 * a pointer to a struct whose first member is VK_LOADER_DATA. The
2260 * ICD must initialize VK_LOADER_DATA.loadMagic to
2261 * ICD_LOADER_MAGIC.
2262 * - The loader implements vkCreate{PLATFORM}SurfaceKHR() and
2263 * vkDestroySurfaceKHR(). The ICD must be capable of working with
2264 * such loader-managed surfaces.
2265 *
2266 * - Loader interface v2 differs from v1 in:
2267 * - The first ICD entrypoint called by the loader is
2268 * vk_icdNegotiateLoaderICDInterfaceVersion(). The ICD must
2269 * statically expose this entrypoint.
2270 *
2271 * - Loader interface v3 differs from v2 in:
2272 * - The ICD must implement vkCreate{PLATFORM}SurfaceKHR(),
2273 * vkDestroySurfaceKHR(), and other API which uses VKSurfaceKHR,
2274 * because the loader no longer does so.
2275 */
2276 *pSupportedVersion = MIN2(*pSupportedVersion, 3u);
2277 return VK_SUCCESS;
2278 }
2279
2280 VkResult
2281 tu_GetMemoryFdKHR(VkDevice _device,
2282 const VkMemoryGetFdInfoKHR *pGetFdInfo,
2283 int *pFd)
2284 {
2285 TU_FROM_HANDLE(tu_device, device, _device);
2286 TU_FROM_HANDLE(tu_device_memory, memory, pGetFdInfo->memory);
2287
2288 assert(pGetFdInfo->sType == VK_STRUCTURE_TYPE_MEMORY_GET_FD_INFO_KHR);
2289
2290 /* At the moment, we support only the below handle types. */
2291 assert(pGetFdInfo->handleType ==
2292 VK_EXTERNAL_MEMORY_HANDLE_TYPE_OPAQUE_FD_BIT ||
2293 pGetFdInfo->handleType ==
2294 VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_EXT);
2295
2296 int prime_fd = tu_bo_export_dmabuf(device, &memory->bo);
2297 if (prime_fd < 0)
2298 return vk_error(device->instance, VK_ERROR_OUT_OF_DEVICE_MEMORY);
2299
2300 *pFd = prime_fd;
2301 return VK_SUCCESS;
2302 }
2303
2304 VkResult
2305 tu_GetMemoryFdPropertiesKHR(VkDevice _device,
2306 VkExternalMemoryHandleTypeFlagBits handleType,
2307 int fd,
2308 VkMemoryFdPropertiesKHR *pMemoryFdProperties)
2309 {
2310 assert(handleType == VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_EXT);
2311 pMemoryFdProperties->memoryTypeBits = 1;
2312 return VK_SUCCESS;
2313 }
2314
2315 void
2316 tu_GetPhysicalDeviceExternalSemaphoreProperties(
2317 VkPhysicalDevice physicalDevice,
2318 const VkPhysicalDeviceExternalSemaphoreInfo *pExternalSemaphoreInfo,
2319 VkExternalSemaphoreProperties *pExternalSemaphoreProperties)
2320 {
2321 pExternalSemaphoreProperties->exportFromImportedHandleTypes = 0;
2322 pExternalSemaphoreProperties->compatibleHandleTypes = 0;
2323 pExternalSemaphoreProperties->externalSemaphoreFeatures = 0;
2324 }
2325
2326 void
2327 tu_GetPhysicalDeviceExternalFenceProperties(
2328 VkPhysicalDevice physicalDevice,
2329 const VkPhysicalDeviceExternalFenceInfo *pExternalFenceInfo,
2330 VkExternalFenceProperties *pExternalFenceProperties)
2331 {
2332 pExternalFenceProperties->exportFromImportedHandleTypes = 0;
2333 pExternalFenceProperties->compatibleHandleTypes = 0;
2334 pExternalFenceProperties->externalFenceFeatures = 0;
2335 }
2336
2337 VkResult
2338 tu_CreateDebugReportCallbackEXT(
2339 VkInstance _instance,
2340 const VkDebugReportCallbackCreateInfoEXT *pCreateInfo,
2341 const VkAllocationCallbacks *pAllocator,
2342 VkDebugReportCallbackEXT *pCallback)
2343 {
2344 TU_FROM_HANDLE(tu_instance, instance, _instance);
2345 return vk_create_debug_report_callback(&instance->debug_report_callbacks,
2346 pCreateInfo, pAllocator,
2347 &instance->alloc, pCallback);
2348 }
2349
2350 void
2351 tu_DestroyDebugReportCallbackEXT(VkInstance _instance,
2352 VkDebugReportCallbackEXT _callback,
2353 const VkAllocationCallbacks *pAllocator)
2354 {
2355 TU_FROM_HANDLE(tu_instance, instance, _instance);
2356 vk_destroy_debug_report_callback(&instance->debug_report_callbacks,
2357 _callback, pAllocator, &instance->alloc);
2358 }
2359
2360 void
2361 tu_DebugReportMessageEXT(VkInstance _instance,
2362 VkDebugReportFlagsEXT flags,
2363 VkDebugReportObjectTypeEXT objectType,
2364 uint64_t object,
2365 size_t location,
2366 int32_t messageCode,
2367 const char *pLayerPrefix,
2368 const char *pMessage)
2369 {
2370 TU_FROM_HANDLE(tu_instance, instance, _instance);
2371 vk_debug_report(&instance->debug_report_callbacks, flags, objectType,
2372 object, location, messageCode, pLayerPrefix, pMessage);
2373 }
2374
2375 void
2376 tu_GetDeviceGroupPeerMemoryFeatures(
2377 VkDevice device,
2378 uint32_t heapIndex,
2379 uint32_t localDeviceIndex,
2380 uint32_t remoteDeviceIndex,
2381 VkPeerMemoryFeatureFlags *pPeerMemoryFeatures)
2382 {
2383 assert(localDeviceIndex == remoteDeviceIndex);
2384
2385 *pPeerMemoryFeatures = VK_PEER_MEMORY_FEATURE_COPY_SRC_BIT |
2386 VK_PEER_MEMORY_FEATURE_COPY_DST_BIT |
2387 VK_PEER_MEMORY_FEATURE_GENERIC_SRC_BIT |
2388 VK_PEER_MEMORY_FEATURE_GENERIC_DST_BIT;
2389 }
2390
2391 void tu_GetPhysicalDeviceMultisamplePropertiesEXT(
2392 VkPhysicalDevice physicalDevice,
2393 VkSampleCountFlagBits samples,
2394 VkMultisamplePropertiesEXT* pMultisampleProperties)
2395 {
2396 TU_FROM_HANDLE(tu_physical_device, pdevice, physicalDevice);
2397
2398 if (samples <= VK_SAMPLE_COUNT_4_BIT && pdevice->supported_extensions.EXT_sample_locations)
2399 pMultisampleProperties->maxSampleLocationGridSize = (VkExtent2D){ 1, 1 };
2400 else
2401 pMultisampleProperties->maxSampleLocationGridSize = (VkExtent2D){ 0, 0 };
2402 }