turnip: Use tu6_reduction_mode() to avoid warning
[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 = false,
585 .fullDrawIndexUint32 = true,
586 .imageCubeArray = true,
587 .independentBlend = true,
588 .geometryShader = true,
589 .tessellationShader = false,
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 = false,
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 default:
720 break;
721 }
722 }
723 return tu_GetPhysicalDeviceFeatures(physicalDevice, &pFeatures->features);
724 }
725
726 void
727 tu_GetPhysicalDeviceProperties(VkPhysicalDevice physicalDevice,
728 VkPhysicalDeviceProperties *pProperties)
729 {
730 TU_FROM_HANDLE(tu_physical_device, pdevice, physicalDevice);
731 VkSampleCountFlags sample_counts =
732 VK_SAMPLE_COUNT_1_BIT | VK_SAMPLE_COUNT_2_BIT | VK_SAMPLE_COUNT_4_BIT;
733
734 /* I have no idea what the maximum size is, but the hardware supports very
735 * large numbers of descriptors (at least 2^16). This limit is based on
736 * CP_LOAD_STATE6, which has a 28-bit field for the DWORD offset, so that
737 * we don't have to think about what to do if that overflows, but really
738 * nothing is likely to get close to this.
739 */
740 const size_t max_descriptor_set_size = (1 << 28) / A6XX_TEX_CONST_DWORDS;
741
742 VkPhysicalDeviceLimits limits = {
743 .maxImageDimension1D = (1 << 14),
744 .maxImageDimension2D = (1 << 14),
745 .maxImageDimension3D = (1 << 11),
746 .maxImageDimensionCube = (1 << 14),
747 .maxImageArrayLayers = (1 << 11),
748 .maxTexelBufferElements = 128 * 1024 * 1024,
749 .maxUniformBufferRange = MAX_UNIFORM_BUFFER_RANGE,
750 .maxStorageBufferRange = MAX_STORAGE_BUFFER_RANGE,
751 .maxPushConstantsSize = MAX_PUSH_CONSTANTS_SIZE,
752 .maxMemoryAllocationCount = UINT32_MAX,
753 .maxSamplerAllocationCount = 64 * 1024,
754 .bufferImageGranularity = 64, /* A cache line */
755 .sparseAddressSpaceSize = 0xffffffffu, /* buffer max size */
756 .maxBoundDescriptorSets = MAX_SETS,
757 .maxPerStageDescriptorSamplers = max_descriptor_set_size,
758 .maxPerStageDescriptorUniformBuffers = max_descriptor_set_size,
759 .maxPerStageDescriptorStorageBuffers = max_descriptor_set_size,
760 .maxPerStageDescriptorSampledImages = max_descriptor_set_size,
761 .maxPerStageDescriptorStorageImages = max_descriptor_set_size,
762 .maxPerStageDescriptorInputAttachments = MAX_RTS,
763 .maxPerStageResources = max_descriptor_set_size,
764 .maxDescriptorSetSamplers = max_descriptor_set_size,
765 .maxDescriptorSetUniformBuffers = max_descriptor_set_size,
766 .maxDescriptorSetUniformBuffersDynamic = MAX_DYNAMIC_UNIFORM_BUFFERS,
767 .maxDescriptorSetStorageBuffers = max_descriptor_set_size,
768 .maxDescriptorSetStorageBuffersDynamic = MAX_DYNAMIC_STORAGE_BUFFERS,
769 .maxDescriptorSetSampledImages = max_descriptor_set_size,
770 .maxDescriptorSetStorageImages = max_descriptor_set_size,
771 .maxDescriptorSetInputAttachments = MAX_RTS,
772 .maxVertexInputAttributes = 32,
773 .maxVertexInputBindings = 32,
774 .maxVertexInputAttributeOffset = 4095,
775 .maxVertexInputBindingStride = 2048,
776 .maxVertexOutputComponents = 128,
777 .maxTessellationGenerationLevel = 64,
778 .maxTessellationPatchSize = 32,
779 .maxTessellationControlPerVertexInputComponents = 128,
780 .maxTessellationControlPerVertexOutputComponents = 128,
781 .maxTessellationControlPerPatchOutputComponents = 120,
782 .maxTessellationControlTotalOutputComponents = 4096,
783 .maxTessellationEvaluationInputComponents = 128,
784 .maxTessellationEvaluationOutputComponents = 128,
785 .maxGeometryShaderInvocations = 32,
786 .maxGeometryInputComponents = 64,
787 .maxGeometryOutputComponents = 128,
788 .maxGeometryOutputVertices = 256,
789 .maxGeometryTotalOutputComponents = 1024,
790 .maxFragmentInputComponents = 124,
791 .maxFragmentOutputAttachments = 8,
792 .maxFragmentDualSrcAttachments = 1,
793 .maxFragmentCombinedOutputResources = 8,
794 .maxComputeSharedMemorySize = 32768,
795 .maxComputeWorkGroupCount = { 65535, 65535, 65535 },
796 .maxComputeWorkGroupInvocations = 2048,
797 .maxComputeWorkGroupSize = { 2048, 2048, 2048 },
798 .subPixelPrecisionBits = 8,
799 .subTexelPrecisionBits = 4 /* FIXME */,
800 .mipmapPrecisionBits = 4 /* FIXME */,
801 .maxDrawIndexedIndexValue = UINT32_MAX,
802 .maxDrawIndirectCount = UINT32_MAX,
803 .maxSamplerLodBias = 16,
804 .maxSamplerAnisotropy = 16,
805 .maxViewports = MAX_VIEWPORTS,
806 .maxViewportDimensions = { (1 << 14), (1 << 14) },
807 .viewportBoundsRange = { INT16_MIN, INT16_MAX },
808 .viewportSubPixelBits = 8,
809 .minMemoryMapAlignment = 4096, /* A page */
810 .minTexelBufferOffsetAlignment = 64,
811 .minUniformBufferOffsetAlignment = 64,
812 .minStorageBufferOffsetAlignment = 64,
813 .minTexelOffset = -32,
814 .maxTexelOffset = 31,
815 .minTexelGatherOffset = -32,
816 .maxTexelGatherOffset = 31,
817 .minInterpolationOffset = -2,
818 .maxInterpolationOffset = 2,
819 .subPixelInterpolationOffsetBits = 8,
820 .maxFramebufferWidth = (1 << 14),
821 .maxFramebufferHeight = (1 << 14),
822 .maxFramebufferLayers = (1 << 10),
823 .framebufferColorSampleCounts = sample_counts,
824 .framebufferDepthSampleCounts = sample_counts,
825 .framebufferStencilSampleCounts = sample_counts,
826 .framebufferNoAttachmentsSampleCounts = sample_counts,
827 .maxColorAttachments = MAX_RTS,
828 .sampledImageColorSampleCounts = sample_counts,
829 .sampledImageIntegerSampleCounts = VK_SAMPLE_COUNT_1_BIT,
830 .sampledImageDepthSampleCounts = sample_counts,
831 .sampledImageStencilSampleCounts = sample_counts,
832 .storageImageSampleCounts = VK_SAMPLE_COUNT_1_BIT,
833 .maxSampleMaskWords = 1,
834 .timestampComputeAndGraphics = true,
835 .timestampPeriod = 1000000000.0 / 19200000.0, /* CP_ALWAYS_ON_COUNTER is fixed 19.2MHz */
836 .maxClipDistances = 8,
837 .maxCullDistances = 8,
838 .maxCombinedClipAndCullDistances = 8,
839 .discreteQueuePriorities = 1,
840 .pointSizeRange = { 0.125, 255.875 },
841 .lineWidthRange = { 0.0, 7.9921875 },
842 .pointSizeGranularity = (1.0 / 8.0),
843 .lineWidthGranularity = (1.0 / 128.0),
844 .strictLines = false, /* FINISHME */
845 .standardSampleLocations = true,
846 .optimalBufferCopyOffsetAlignment = 128,
847 .optimalBufferCopyRowPitchAlignment = 128,
848 .nonCoherentAtomSize = 64,
849 };
850
851 *pProperties = (VkPhysicalDeviceProperties) {
852 .apiVersion = tu_physical_device_api_version(pdevice),
853 .driverVersion = vk_get_driver_version(),
854 .vendorID = 0, /* TODO */
855 .deviceID = 0,
856 .deviceType = VK_PHYSICAL_DEVICE_TYPE_INTEGRATED_GPU,
857 .limits = limits,
858 .sparseProperties = { 0 },
859 };
860
861 strcpy(pProperties->deviceName, pdevice->name);
862 memcpy(pProperties->pipelineCacheUUID, pdevice->cache_uuid, VK_UUID_SIZE);
863 }
864
865 void
866 tu_GetPhysicalDeviceProperties2(VkPhysicalDevice physicalDevice,
867 VkPhysicalDeviceProperties2 *pProperties)
868 {
869 TU_FROM_HANDLE(tu_physical_device, pdevice, physicalDevice);
870 tu_GetPhysicalDeviceProperties(physicalDevice, &pProperties->properties);
871
872 vk_foreach_struct(ext, pProperties->pNext)
873 {
874 switch (ext->sType) {
875 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PUSH_DESCRIPTOR_PROPERTIES_KHR: {
876 VkPhysicalDevicePushDescriptorPropertiesKHR *properties =
877 (VkPhysicalDevicePushDescriptorPropertiesKHR *) ext;
878 properties->maxPushDescriptors = MAX_PUSH_DESCRIPTORS;
879 break;
880 }
881 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_ID_PROPERTIES: {
882 VkPhysicalDeviceIDProperties *properties =
883 (VkPhysicalDeviceIDProperties *) ext;
884 memcpy(properties->driverUUID, pdevice->driver_uuid, VK_UUID_SIZE);
885 memcpy(properties->deviceUUID, pdevice->device_uuid, VK_UUID_SIZE);
886 properties->deviceLUIDValid = false;
887 break;
888 }
889 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_MULTIVIEW_PROPERTIES: {
890 VkPhysicalDeviceMultiviewProperties *properties =
891 (VkPhysicalDeviceMultiviewProperties *) ext;
892 properties->maxMultiviewViewCount = MAX_VIEWS;
893 properties->maxMultiviewInstanceIndex = INT_MAX;
894 break;
895 }
896 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_POINT_CLIPPING_PROPERTIES: {
897 VkPhysicalDevicePointClippingProperties *properties =
898 (VkPhysicalDevicePointClippingProperties *) ext;
899 properties->pointClippingBehavior =
900 VK_POINT_CLIPPING_BEHAVIOR_ALL_CLIP_PLANES;
901 break;
902 }
903 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_MAINTENANCE_3_PROPERTIES: {
904 VkPhysicalDeviceMaintenance3Properties *properties =
905 (VkPhysicalDeviceMaintenance3Properties *) ext;
906 /* Make sure everything is addressable by a signed 32-bit int, and
907 * our largest descriptors are 96 bytes. */
908 properties->maxPerSetDescriptors = (1ull << 31) / 96;
909 /* Our buffer size fields allow only this much */
910 properties->maxMemoryAllocationSize = 0xFFFFFFFFull;
911 break;
912 }
913 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_TRANSFORM_FEEDBACK_PROPERTIES_EXT: {
914 VkPhysicalDeviceTransformFeedbackPropertiesEXT *properties =
915 (VkPhysicalDeviceTransformFeedbackPropertiesEXT *)ext;
916
917 properties->maxTransformFeedbackStreams = IR3_MAX_SO_STREAMS;
918 properties->maxTransformFeedbackBuffers = IR3_MAX_SO_BUFFERS;
919 properties->maxTransformFeedbackBufferSize = UINT32_MAX;
920 properties->maxTransformFeedbackStreamDataSize = 512;
921 properties->maxTransformFeedbackBufferDataSize = 512;
922 properties->maxTransformFeedbackBufferDataStride = 512;
923 properties->transformFeedbackQueries = true;
924 properties->transformFeedbackStreamsLinesTriangles = false;
925 properties->transformFeedbackRasterizationStreamSelect = false;
926 properties->transformFeedbackDraw = true;
927 break;
928 }
929 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SAMPLE_LOCATIONS_PROPERTIES_EXT: {
930 VkPhysicalDeviceSampleLocationsPropertiesEXT *properties =
931 (VkPhysicalDeviceSampleLocationsPropertiesEXT *)ext;
932 properties->sampleLocationSampleCounts = 0;
933 if (pdevice->supported_extensions.EXT_sample_locations) {
934 properties->sampleLocationSampleCounts =
935 VK_SAMPLE_COUNT_1_BIT | VK_SAMPLE_COUNT_2_BIT | VK_SAMPLE_COUNT_4_BIT;
936 }
937 properties->maxSampleLocationGridSize = (VkExtent2D) { 1 , 1 };
938 properties->sampleLocationCoordinateRange[0] = 0.0f;
939 properties->sampleLocationCoordinateRange[1] = 0.9375f;
940 properties->sampleLocationSubPixelBits = 4;
941 properties->variableSampleLocations = true;
942 break;
943 }
944 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SAMPLER_FILTER_MINMAX_PROPERTIES: {
945 VkPhysicalDeviceSamplerFilterMinmaxProperties *properties =
946 (VkPhysicalDeviceSamplerFilterMinmaxProperties *)ext;
947 properties->filterMinmaxImageComponentMapping = true;
948 properties->filterMinmaxSingleComponentFormats = true;
949 break;
950 }
951
952 default:
953 break;
954 }
955 }
956 }
957
958 static const VkQueueFamilyProperties tu_queue_family_properties = {
959 .queueFlags =
960 VK_QUEUE_GRAPHICS_BIT | VK_QUEUE_COMPUTE_BIT | VK_QUEUE_TRANSFER_BIT,
961 .queueCount = 1,
962 .timestampValidBits = 48,
963 .minImageTransferGranularity = { 1, 1, 1 },
964 };
965
966 void
967 tu_GetPhysicalDeviceQueueFamilyProperties(
968 VkPhysicalDevice physicalDevice,
969 uint32_t *pQueueFamilyPropertyCount,
970 VkQueueFamilyProperties *pQueueFamilyProperties)
971 {
972 VK_OUTARRAY_MAKE(out, pQueueFamilyProperties, pQueueFamilyPropertyCount);
973
974 vk_outarray_append(&out, p) { *p = tu_queue_family_properties; }
975 }
976
977 void
978 tu_GetPhysicalDeviceQueueFamilyProperties2(
979 VkPhysicalDevice physicalDevice,
980 uint32_t *pQueueFamilyPropertyCount,
981 VkQueueFamilyProperties2 *pQueueFamilyProperties)
982 {
983 VK_OUTARRAY_MAKE(out, pQueueFamilyProperties, pQueueFamilyPropertyCount);
984
985 vk_outarray_append(&out, p)
986 {
987 p->queueFamilyProperties = tu_queue_family_properties;
988 }
989 }
990
991 static uint64_t
992 tu_get_system_heap_size()
993 {
994 struct sysinfo info;
995 sysinfo(&info);
996
997 uint64_t total_ram = (uint64_t) info.totalram * (uint64_t) info.mem_unit;
998
999 /* We don't want to burn too much ram with the GPU. If the user has 4GiB
1000 * or less, we use at most half. If they have more than 4GiB, we use 3/4.
1001 */
1002 uint64_t available_ram;
1003 if (total_ram <= 4ull * 1024ull * 1024ull * 1024ull)
1004 available_ram = total_ram / 2;
1005 else
1006 available_ram = total_ram * 3 / 4;
1007
1008 return available_ram;
1009 }
1010
1011 void
1012 tu_GetPhysicalDeviceMemoryProperties(
1013 VkPhysicalDevice physicalDevice,
1014 VkPhysicalDeviceMemoryProperties *pMemoryProperties)
1015 {
1016 pMemoryProperties->memoryHeapCount = 1;
1017 pMemoryProperties->memoryHeaps[0].size = tu_get_system_heap_size();
1018 pMemoryProperties->memoryHeaps[0].flags = VK_MEMORY_HEAP_DEVICE_LOCAL_BIT;
1019
1020 pMemoryProperties->memoryTypeCount = 1;
1021 pMemoryProperties->memoryTypes[0].propertyFlags =
1022 VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT |
1023 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT |
1024 VK_MEMORY_PROPERTY_HOST_COHERENT_BIT;
1025 pMemoryProperties->memoryTypes[0].heapIndex = 0;
1026 }
1027
1028 void
1029 tu_GetPhysicalDeviceMemoryProperties2(
1030 VkPhysicalDevice physicalDevice,
1031 VkPhysicalDeviceMemoryProperties2 *pMemoryProperties)
1032 {
1033 return tu_GetPhysicalDeviceMemoryProperties(
1034 physicalDevice, &pMemoryProperties->memoryProperties);
1035 }
1036
1037 static VkResult
1038 tu_queue_init(struct tu_device *device,
1039 struct tu_queue *queue,
1040 uint32_t queue_family_index,
1041 int idx,
1042 VkDeviceQueueCreateFlags flags)
1043 {
1044 queue->_loader_data.loaderMagic = ICD_LOADER_MAGIC;
1045 queue->device = device;
1046 queue->queue_family_index = queue_family_index;
1047 queue->queue_idx = idx;
1048 queue->flags = flags;
1049
1050 int ret = tu_drm_submitqueue_new(device, 0, &queue->msm_queue_id);
1051 if (ret)
1052 return VK_ERROR_INITIALIZATION_FAILED;
1053
1054 tu_fence_init(&queue->submit_fence, false);
1055
1056 return VK_SUCCESS;
1057 }
1058
1059 static void
1060 tu_queue_finish(struct tu_queue *queue)
1061 {
1062 tu_fence_finish(&queue->submit_fence);
1063 tu_drm_submitqueue_close(queue->device, queue->msm_queue_id);
1064 }
1065
1066 static int
1067 tu_get_device_extension_index(const char *name)
1068 {
1069 for (unsigned i = 0; i < TU_DEVICE_EXTENSION_COUNT; ++i) {
1070 if (strcmp(name, tu_device_extensions[i].extensionName) == 0)
1071 return i;
1072 }
1073 return -1;
1074 }
1075
1076 struct PACKED bcolor_entry {
1077 uint32_t fp32[4];
1078 uint16_t ui16[4];
1079 int16_t si16[4];
1080 uint16_t fp16[4];
1081 uint16_t rgb565;
1082 uint16_t rgb5a1;
1083 uint16_t rgba4;
1084 uint8_t __pad0[2];
1085 uint8_t ui8[4];
1086 int8_t si8[4];
1087 uint32_t rgb10a2;
1088 uint32_t z24; /* also s8? */
1089 uint16_t srgb[4]; /* appears to duplicate fp16[], but clamped, used for srgb */
1090 uint8_t __pad1[56];
1091 } border_color[] = {
1092 [VK_BORDER_COLOR_FLOAT_TRANSPARENT_BLACK] = {},
1093 [VK_BORDER_COLOR_INT_TRANSPARENT_BLACK] = {},
1094 [VK_BORDER_COLOR_FLOAT_OPAQUE_BLACK] = {
1095 .fp32[3] = 0x3f800000,
1096 .ui16[3] = 0xffff,
1097 .si16[3] = 0x7fff,
1098 .fp16[3] = 0x3c00,
1099 .rgb5a1 = 0x8000,
1100 .rgba4 = 0xf000,
1101 .ui8[3] = 0xff,
1102 .si8[3] = 0x7f,
1103 .rgb10a2 = 0xc0000000,
1104 .srgb[3] = 0x3c00,
1105 },
1106 [VK_BORDER_COLOR_INT_OPAQUE_BLACK] = {
1107 .fp32[3] = 1,
1108 .fp16[3] = 1,
1109 },
1110 [VK_BORDER_COLOR_FLOAT_OPAQUE_WHITE] = {
1111 .fp32[0 ... 3] = 0x3f800000,
1112 .ui16[0 ... 3] = 0xffff,
1113 .si16[0 ... 3] = 0x7fff,
1114 .fp16[0 ... 3] = 0x3c00,
1115 .rgb565 = 0xffff,
1116 .rgb5a1 = 0xffff,
1117 .rgba4 = 0xffff,
1118 .ui8[0 ... 3] = 0xff,
1119 .si8[0 ... 3] = 0x7f,
1120 .rgb10a2 = 0xffffffff,
1121 .z24 = 0xffffff,
1122 .srgb[0 ... 3] = 0x3c00,
1123 },
1124 [VK_BORDER_COLOR_INT_OPAQUE_WHITE] = {
1125 .fp32[0 ... 3] = 1,
1126 .fp16[0 ... 3] = 1,
1127 },
1128 };
1129
1130
1131 VkResult
1132 tu_CreateDevice(VkPhysicalDevice physicalDevice,
1133 const VkDeviceCreateInfo *pCreateInfo,
1134 const VkAllocationCallbacks *pAllocator,
1135 VkDevice *pDevice)
1136 {
1137 TU_FROM_HANDLE(tu_physical_device, physical_device, physicalDevice);
1138 VkResult result;
1139 struct tu_device *device;
1140
1141 /* Check enabled features */
1142 if (pCreateInfo->pEnabledFeatures) {
1143 VkPhysicalDeviceFeatures supported_features;
1144 tu_GetPhysicalDeviceFeatures(physicalDevice, &supported_features);
1145 VkBool32 *supported_feature = (VkBool32 *) &supported_features;
1146 VkBool32 *enabled_feature = (VkBool32 *) pCreateInfo->pEnabledFeatures;
1147 unsigned num_features =
1148 sizeof(VkPhysicalDeviceFeatures) / sizeof(VkBool32);
1149 for (uint32_t i = 0; i < num_features; i++) {
1150 if (enabled_feature[i] && !supported_feature[i])
1151 return vk_error(physical_device->instance,
1152 VK_ERROR_FEATURE_NOT_PRESENT);
1153 }
1154 }
1155
1156 device = vk_zalloc2(&physical_device->instance->alloc, pAllocator,
1157 sizeof(*device), 8, VK_SYSTEM_ALLOCATION_SCOPE_DEVICE);
1158 if (!device)
1159 return vk_error(physical_device->instance, VK_ERROR_OUT_OF_HOST_MEMORY);
1160
1161 device->_loader_data.loaderMagic = ICD_LOADER_MAGIC;
1162 device->instance = physical_device->instance;
1163 device->physical_device = physical_device;
1164
1165 if (pAllocator)
1166 device->alloc = *pAllocator;
1167 else
1168 device->alloc = physical_device->instance->alloc;
1169
1170 for (uint32_t i = 0; i < pCreateInfo->enabledExtensionCount; i++) {
1171 const char *ext_name = pCreateInfo->ppEnabledExtensionNames[i];
1172 int index = tu_get_device_extension_index(ext_name);
1173 if (index < 0 ||
1174 !physical_device->supported_extensions.extensions[index]) {
1175 vk_free(&device->alloc, device);
1176 return vk_error(physical_device->instance,
1177 VK_ERROR_EXTENSION_NOT_PRESENT);
1178 }
1179
1180 device->enabled_extensions.extensions[index] = true;
1181 }
1182
1183 for (unsigned i = 0; i < pCreateInfo->queueCreateInfoCount; i++) {
1184 const VkDeviceQueueCreateInfo *queue_create =
1185 &pCreateInfo->pQueueCreateInfos[i];
1186 uint32_t qfi = queue_create->queueFamilyIndex;
1187 device->queues[qfi] = vk_alloc(
1188 &device->alloc, queue_create->queueCount * sizeof(struct tu_queue),
1189 8, VK_SYSTEM_ALLOCATION_SCOPE_DEVICE);
1190 if (!device->queues[qfi]) {
1191 result = VK_ERROR_OUT_OF_HOST_MEMORY;
1192 goto fail_queues;
1193 }
1194
1195 memset(device->queues[qfi], 0,
1196 queue_create->queueCount * sizeof(struct tu_queue));
1197
1198 device->queue_count[qfi] = queue_create->queueCount;
1199
1200 for (unsigned q = 0; q < queue_create->queueCount; q++) {
1201 result = tu_queue_init(device, &device->queues[qfi][q], qfi, q,
1202 queue_create->flags);
1203 if (result != VK_SUCCESS)
1204 goto fail_queues;
1205 }
1206 }
1207
1208 device->compiler = ir3_compiler_create(NULL, physical_device->gpu_id);
1209 if (!device->compiler)
1210 goto fail_queues;
1211
1212 #define VSC_DRAW_STRM_SIZE(pitch) ((pitch) * 32 + 0x100) /* extra size to store VSC_SIZE */
1213 #define VSC_PRIM_STRM_SIZE(pitch) ((pitch) * 32)
1214
1215 device->vsc_draw_strm_pitch = 0x440 * 4;
1216 device->vsc_prim_strm_pitch = 0x1040 * 4;
1217
1218 result = tu_bo_init_new(device, &device->vsc_draw_strm, VSC_DRAW_STRM_SIZE(device->vsc_draw_strm_pitch));
1219 if (result != VK_SUCCESS)
1220 goto fail_vsc_data;
1221
1222 result = tu_bo_init_new(device, &device->vsc_prim_strm, VSC_PRIM_STRM_SIZE(device->vsc_prim_strm_pitch));
1223 if (result != VK_SUCCESS)
1224 goto fail_vsc_data2;
1225
1226 STATIC_ASSERT(sizeof(struct bcolor_entry) == 128);
1227 result = tu_bo_init_new(device, &device->border_color, sizeof(border_color));
1228 if (result != VK_SUCCESS)
1229 goto fail_border_color;
1230
1231 result = tu_bo_map(device, &device->border_color);
1232 if (result != VK_SUCCESS)
1233 goto fail_border_color_map;
1234
1235 memcpy(device->border_color.map, border_color, sizeof(border_color));
1236
1237 VkPipelineCacheCreateInfo ci;
1238 ci.sType = VK_STRUCTURE_TYPE_PIPELINE_CACHE_CREATE_INFO;
1239 ci.pNext = NULL;
1240 ci.flags = 0;
1241 ci.pInitialData = NULL;
1242 ci.initialDataSize = 0;
1243 VkPipelineCache pc;
1244 result =
1245 tu_CreatePipelineCache(tu_device_to_handle(device), &ci, NULL, &pc);
1246 if (result != VK_SUCCESS)
1247 goto fail_pipeline_cache;
1248
1249 device->mem_cache = tu_pipeline_cache_from_handle(pc);
1250
1251 for (unsigned i = 0; i < ARRAY_SIZE(device->scratch_bos); i++)
1252 mtx_init(&device->scratch_bos[i].construct_mtx, mtx_plain);
1253
1254 *pDevice = tu_device_to_handle(device);
1255 return VK_SUCCESS;
1256
1257 fail_pipeline_cache:
1258 fail_border_color_map:
1259 tu_bo_finish(device, &device->border_color);
1260
1261 fail_border_color:
1262 tu_bo_finish(device, &device->vsc_prim_strm);
1263
1264 fail_vsc_data2:
1265 tu_bo_finish(device, &device->vsc_draw_strm);
1266
1267 fail_vsc_data:
1268 ralloc_free(device->compiler);
1269
1270 fail_queues:
1271 for (unsigned i = 0; i < TU_MAX_QUEUE_FAMILIES; i++) {
1272 for (unsigned q = 0; q < device->queue_count[i]; q++)
1273 tu_queue_finish(&device->queues[i][q]);
1274 if (device->queue_count[i])
1275 vk_free(&device->alloc, device->queues[i]);
1276 }
1277
1278 vk_free(&device->alloc, device);
1279 return result;
1280 }
1281
1282 void
1283 tu_DestroyDevice(VkDevice _device, const VkAllocationCallbacks *pAllocator)
1284 {
1285 TU_FROM_HANDLE(tu_device, device, _device);
1286
1287 if (!device)
1288 return;
1289
1290 tu_bo_finish(device, &device->vsc_draw_strm);
1291 tu_bo_finish(device, &device->vsc_prim_strm);
1292
1293 for (unsigned i = 0; i < TU_MAX_QUEUE_FAMILIES; i++) {
1294 for (unsigned q = 0; q < device->queue_count[i]; q++)
1295 tu_queue_finish(&device->queues[i][q]);
1296 if (device->queue_count[i])
1297 vk_free(&device->alloc, device->queues[i]);
1298 }
1299
1300 for (unsigned i = 0; i < ARRAY_SIZE(device->scratch_bos); i++) {
1301 if (device->scratch_bos[i].initialized)
1302 tu_bo_finish(device, &device->scratch_bos[i].bo);
1303 }
1304
1305 /* the compiler does not use pAllocator */
1306 ralloc_free(device->compiler);
1307
1308 VkPipelineCache pc = tu_pipeline_cache_to_handle(device->mem_cache);
1309 tu_DestroyPipelineCache(tu_device_to_handle(device), pc, NULL);
1310
1311 vk_free(&device->alloc, device);
1312 }
1313
1314 VkResult
1315 tu_get_scratch_bo(struct tu_device *dev, uint64_t size, struct tu_bo **bo)
1316 {
1317 unsigned size_log2 = MAX2(util_logbase2_ceil64(size), MIN_SCRATCH_BO_SIZE_LOG2);
1318 unsigned index = size_log2 - MIN_SCRATCH_BO_SIZE_LOG2;
1319 assert(index < ARRAY_SIZE(dev->scratch_bos));
1320
1321 for (unsigned i = index; i < ARRAY_SIZE(dev->scratch_bos); i++) {
1322 if (p_atomic_read(&dev->scratch_bos[i].initialized)) {
1323 /* Fast path: just return the already-allocated BO. */
1324 *bo = &dev->scratch_bos[i].bo;
1325 return VK_SUCCESS;
1326 }
1327 }
1328
1329 /* Slow path: actually allocate the BO. We take a lock because the process
1330 * of allocating it is slow, and we don't want to block the CPU while it
1331 * finishes.
1332 */
1333 mtx_lock(&dev->scratch_bos[index].construct_mtx);
1334
1335 /* Another thread may have allocated it already while we were waiting on
1336 * the lock. We need to check this in order to avoid double-allocating.
1337 */
1338 if (dev->scratch_bos[index].initialized) {
1339 mtx_unlock(&dev->scratch_bos[index].construct_mtx);
1340 *bo = &dev->scratch_bos[index].bo;
1341 return VK_SUCCESS;
1342 }
1343
1344 unsigned bo_size = 1ull << size_log2;
1345 VkResult result = tu_bo_init_new(dev, &dev->scratch_bos[index].bo, bo_size);
1346 if (result != VK_SUCCESS) {
1347 mtx_unlock(&dev->scratch_bos[index].construct_mtx);
1348 return result;
1349 }
1350
1351 p_atomic_set(&dev->scratch_bos[index].initialized, true);
1352
1353 mtx_unlock(&dev->scratch_bos[index].construct_mtx);
1354
1355 *bo = &dev->scratch_bos[index].bo;
1356 return VK_SUCCESS;
1357 }
1358
1359 VkResult
1360 tu_EnumerateInstanceLayerProperties(uint32_t *pPropertyCount,
1361 VkLayerProperties *pProperties)
1362 {
1363 *pPropertyCount = 0;
1364 return VK_SUCCESS;
1365 }
1366
1367 VkResult
1368 tu_EnumerateDeviceLayerProperties(VkPhysicalDevice physicalDevice,
1369 uint32_t *pPropertyCount,
1370 VkLayerProperties *pProperties)
1371 {
1372 *pPropertyCount = 0;
1373 return VK_SUCCESS;
1374 }
1375
1376 void
1377 tu_GetDeviceQueue2(VkDevice _device,
1378 const VkDeviceQueueInfo2 *pQueueInfo,
1379 VkQueue *pQueue)
1380 {
1381 TU_FROM_HANDLE(tu_device, device, _device);
1382 struct tu_queue *queue;
1383
1384 queue =
1385 &device->queues[pQueueInfo->queueFamilyIndex][pQueueInfo->queueIndex];
1386 if (pQueueInfo->flags != queue->flags) {
1387 /* From the Vulkan 1.1.70 spec:
1388 *
1389 * "The queue returned by vkGetDeviceQueue2 must have the same
1390 * flags value from this structure as that used at device
1391 * creation time in a VkDeviceQueueCreateInfo instance. If no
1392 * matching flags were specified at device creation time then
1393 * pQueue will return VK_NULL_HANDLE."
1394 */
1395 *pQueue = VK_NULL_HANDLE;
1396 return;
1397 }
1398
1399 *pQueue = tu_queue_to_handle(queue);
1400 }
1401
1402 void
1403 tu_GetDeviceQueue(VkDevice _device,
1404 uint32_t queueFamilyIndex,
1405 uint32_t queueIndex,
1406 VkQueue *pQueue)
1407 {
1408 const VkDeviceQueueInfo2 info =
1409 (VkDeviceQueueInfo2) { .sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_INFO_2,
1410 .queueFamilyIndex = queueFamilyIndex,
1411 .queueIndex = queueIndex };
1412
1413 tu_GetDeviceQueue2(_device, &info, pQueue);
1414 }
1415
1416 VkResult
1417 tu_QueueSubmit(VkQueue _queue,
1418 uint32_t submitCount,
1419 const VkSubmitInfo *pSubmits,
1420 VkFence _fence)
1421 {
1422 TU_FROM_HANDLE(tu_queue, queue, _queue);
1423
1424 for (uint32_t i = 0; i < submitCount; ++i) {
1425 const VkSubmitInfo *submit = pSubmits + i;
1426 const bool last_submit = (i == submitCount - 1);
1427 struct tu_bo_list bo_list;
1428 tu_bo_list_init(&bo_list);
1429
1430 uint32_t entry_count = 0;
1431 for (uint32_t j = 0; j < submit->commandBufferCount; ++j) {
1432 TU_FROM_HANDLE(tu_cmd_buffer, cmdbuf, submit->pCommandBuffers[j]);
1433 entry_count += cmdbuf->cs.entry_count;
1434 }
1435
1436 struct drm_msm_gem_submit_cmd cmds[entry_count];
1437 uint32_t entry_idx = 0;
1438 for (uint32_t j = 0; j < submit->commandBufferCount; ++j) {
1439 TU_FROM_HANDLE(tu_cmd_buffer, cmdbuf, submit->pCommandBuffers[j]);
1440 struct tu_cs *cs = &cmdbuf->cs;
1441 for (unsigned i = 0; i < cs->entry_count; ++i, ++entry_idx) {
1442 cmds[entry_idx].type = MSM_SUBMIT_CMD_BUF;
1443 cmds[entry_idx].submit_idx =
1444 tu_bo_list_add(&bo_list, cs->entries[i].bo,
1445 MSM_SUBMIT_BO_READ | MSM_SUBMIT_BO_DUMP);
1446 cmds[entry_idx].submit_offset = cs->entries[i].offset;
1447 cmds[entry_idx].size = cs->entries[i].size;
1448 cmds[entry_idx].pad = 0;
1449 cmds[entry_idx].nr_relocs = 0;
1450 cmds[entry_idx].relocs = 0;
1451 }
1452
1453 tu_bo_list_merge(&bo_list, &cmdbuf->bo_list);
1454 }
1455
1456 uint32_t flags = MSM_PIPE_3D0;
1457 if (last_submit) {
1458 flags |= MSM_SUBMIT_FENCE_FD_OUT;
1459 }
1460
1461 struct drm_msm_gem_submit req = {
1462 .flags = flags,
1463 .queueid = queue->msm_queue_id,
1464 .bos = (uint64_t)(uintptr_t) bo_list.bo_infos,
1465 .nr_bos = bo_list.count,
1466 .cmds = (uint64_t)(uintptr_t)cmds,
1467 .nr_cmds = entry_count,
1468 };
1469
1470 int ret = drmCommandWriteRead(queue->device->physical_device->local_fd,
1471 DRM_MSM_GEM_SUBMIT,
1472 &req, sizeof(req));
1473 if (ret) {
1474 fprintf(stderr, "submit failed: %s\n", strerror(errno));
1475 abort();
1476 }
1477
1478 tu_bo_list_destroy(&bo_list);
1479
1480 if (last_submit) {
1481 /* no need to merge fences as queue execution is serialized */
1482 tu_fence_update_fd(&queue->submit_fence, req.fence_fd);
1483 }
1484 }
1485
1486 if (_fence != VK_NULL_HANDLE) {
1487 TU_FROM_HANDLE(tu_fence, fence, _fence);
1488 tu_fence_copy(fence, &queue->submit_fence);
1489 }
1490
1491 return VK_SUCCESS;
1492 }
1493
1494 VkResult
1495 tu_QueueWaitIdle(VkQueue _queue)
1496 {
1497 TU_FROM_HANDLE(tu_queue, queue, _queue);
1498
1499 tu_fence_wait_idle(&queue->submit_fence);
1500
1501 return VK_SUCCESS;
1502 }
1503
1504 VkResult
1505 tu_DeviceWaitIdle(VkDevice _device)
1506 {
1507 TU_FROM_HANDLE(tu_device, device, _device);
1508
1509 for (unsigned i = 0; i < TU_MAX_QUEUE_FAMILIES; i++) {
1510 for (unsigned q = 0; q < device->queue_count[i]; q++) {
1511 tu_QueueWaitIdle(tu_queue_to_handle(&device->queues[i][q]));
1512 }
1513 }
1514 return VK_SUCCESS;
1515 }
1516
1517 VkResult
1518 tu_ImportSemaphoreFdKHR(VkDevice _device,
1519 const VkImportSemaphoreFdInfoKHR *pImportSemaphoreFdInfo)
1520 {
1521 tu_stub();
1522
1523 return VK_SUCCESS;
1524 }
1525
1526 VkResult
1527 tu_GetSemaphoreFdKHR(VkDevice _device,
1528 const VkSemaphoreGetFdInfoKHR *pGetFdInfo,
1529 int *pFd)
1530 {
1531 tu_stub();
1532
1533 return VK_SUCCESS;
1534 }
1535
1536 VkResult
1537 tu_ImportFenceFdKHR(VkDevice _device,
1538 const VkImportFenceFdInfoKHR *pImportFenceFdInfo)
1539 {
1540 tu_stub();
1541
1542 return VK_SUCCESS;
1543 }
1544
1545 VkResult
1546 tu_GetFenceFdKHR(VkDevice _device,
1547 const VkFenceGetFdInfoKHR *pGetFdInfo,
1548 int *pFd)
1549 {
1550 tu_stub();
1551
1552 return VK_SUCCESS;
1553 }
1554
1555 VkResult
1556 tu_EnumerateInstanceExtensionProperties(const char *pLayerName,
1557 uint32_t *pPropertyCount,
1558 VkExtensionProperties *pProperties)
1559 {
1560 VK_OUTARRAY_MAKE(out, pProperties, pPropertyCount);
1561
1562 /* We spport no lyaers */
1563 if (pLayerName)
1564 return vk_error(NULL, VK_ERROR_LAYER_NOT_PRESENT);
1565
1566 for (int i = 0; i < TU_INSTANCE_EXTENSION_COUNT; i++) {
1567 if (tu_instance_extensions_supported.extensions[i]) {
1568 vk_outarray_append(&out, prop) { *prop = tu_instance_extensions[i]; }
1569 }
1570 }
1571
1572 return vk_outarray_status(&out);
1573 }
1574
1575 VkResult
1576 tu_EnumerateDeviceExtensionProperties(VkPhysicalDevice physicalDevice,
1577 const char *pLayerName,
1578 uint32_t *pPropertyCount,
1579 VkExtensionProperties *pProperties)
1580 {
1581 /* We spport no lyaers */
1582 TU_FROM_HANDLE(tu_physical_device, device, physicalDevice);
1583 VK_OUTARRAY_MAKE(out, pProperties, pPropertyCount);
1584
1585 /* We spport no lyaers */
1586 if (pLayerName)
1587 return vk_error(NULL, VK_ERROR_LAYER_NOT_PRESENT);
1588
1589 for (int i = 0; i < TU_DEVICE_EXTENSION_COUNT; i++) {
1590 if (device->supported_extensions.extensions[i]) {
1591 vk_outarray_append(&out, prop) { *prop = tu_device_extensions[i]; }
1592 }
1593 }
1594
1595 return vk_outarray_status(&out);
1596 }
1597
1598 PFN_vkVoidFunction
1599 tu_GetInstanceProcAddr(VkInstance _instance, const char *pName)
1600 {
1601 TU_FROM_HANDLE(tu_instance, instance, _instance);
1602
1603 return tu_lookup_entrypoint_checked(
1604 pName, instance ? instance->api_version : 0,
1605 instance ? &instance->enabled_extensions : NULL, NULL);
1606 }
1607
1608 /* The loader wants us to expose a second GetInstanceProcAddr function
1609 * to work around certain LD_PRELOAD issues seen in apps.
1610 */
1611 PUBLIC
1612 VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL
1613 vk_icdGetInstanceProcAddr(VkInstance instance, const char *pName);
1614
1615 PUBLIC
1616 VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL
1617 vk_icdGetInstanceProcAddr(VkInstance instance, const char *pName)
1618 {
1619 return tu_GetInstanceProcAddr(instance, pName);
1620 }
1621
1622 PFN_vkVoidFunction
1623 tu_GetDeviceProcAddr(VkDevice _device, const char *pName)
1624 {
1625 TU_FROM_HANDLE(tu_device, device, _device);
1626
1627 return tu_lookup_entrypoint_checked(pName, device->instance->api_version,
1628 &device->instance->enabled_extensions,
1629 &device->enabled_extensions);
1630 }
1631
1632 static VkResult
1633 tu_alloc_memory(struct tu_device *device,
1634 const VkMemoryAllocateInfo *pAllocateInfo,
1635 const VkAllocationCallbacks *pAllocator,
1636 VkDeviceMemory *pMem)
1637 {
1638 struct tu_device_memory *mem;
1639 VkResult result;
1640
1641 assert(pAllocateInfo->sType == VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO);
1642
1643 if (pAllocateInfo->allocationSize == 0) {
1644 /* Apparently, this is allowed */
1645 *pMem = VK_NULL_HANDLE;
1646 return VK_SUCCESS;
1647 }
1648
1649 mem = vk_alloc2(&device->alloc, pAllocator, sizeof(*mem), 8,
1650 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
1651 if (mem == NULL)
1652 return vk_error(device->instance, VK_ERROR_OUT_OF_HOST_MEMORY);
1653
1654 const VkImportMemoryFdInfoKHR *fd_info =
1655 vk_find_struct_const(pAllocateInfo->pNext, IMPORT_MEMORY_FD_INFO_KHR);
1656 if (fd_info && !fd_info->handleType)
1657 fd_info = NULL;
1658
1659 if (fd_info) {
1660 assert(fd_info->handleType ==
1661 VK_EXTERNAL_MEMORY_HANDLE_TYPE_OPAQUE_FD_BIT ||
1662 fd_info->handleType ==
1663 VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_EXT);
1664
1665 /*
1666 * TODO Importing the same fd twice gives us the same handle without
1667 * reference counting. We need to maintain a per-instance handle-to-bo
1668 * table and add reference count to tu_bo.
1669 */
1670 result = tu_bo_init_dmabuf(device, &mem->bo,
1671 pAllocateInfo->allocationSize, fd_info->fd);
1672 if (result == VK_SUCCESS) {
1673 /* take ownership and close the fd */
1674 close(fd_info->fd);
1675 }
1676 } else {
1677 result =
1678 tu_bo_init_new(device, &mem->bo, pAllocateInfo->allocationSize);
1679 }
1680
1681 if (result != VK_SUCCESS) {
1682 vk_free2(&device->alloc, pAllocator, mem);
1683 return result;
1684 }
1685
1686 mem->size = pAllocateInfo->allocationSize;
1687 mem->type_index = pAllocateInfo->memoryTypeIndex;
1688
1689 mem->map = NULL;
1690 mem->user_ptr = NULL;
1691
1692 *pMem = tu_device_memory_to_handle(mem);
1693
1694 return VK_SUCCESS;
1695 }
1696
1697 VkResult
1698 tu_AllocateMemory(VkDevice _device,
1699 const VkMemoryAllocateInfo *pAllocateInfo,
1700 const VkAllocationCallbacks *pAllocator,
1701 VkDeviceMemory *pMem)
1702 {
1703 TU_FROM_HANDLE(tu_device, device, _device);
1704 return tu_alloc_memory(device, pAllocateInfo, pAllocator, pMem);
1705 }
1706
1707 void
1708 tu_FreeMemory(VkDevice _device,
1709 VkDeviceMemory _mem,
1710 const VkAllocationCallbacks *pAllocator)
1711 {
1712 TU_FROM_HANDLE(tu_device, device, _device);
1713 TU_FROM_HANDLE(tu_device_memory, mem, _mem);
1714
1715 if (mem == NULL)
1716 return;
1717
1718 tu_bo_finish(device, &mem->bo);
1719 vk_free2(&device->alloc, pAllocator, mem);
1720 }
1721
1722 VkResult
1723 tu_MapMemory(VkDevice _device,
1724 VkDeviceMemory _memory,
1725 VkDeviceSize offset,
1726 VkDeviceSize size,
1727 VkMemoryMapFlags flags,
1728 void **ppData)
1729 {
1730 TU_FROM_HANDLE(tu_device, device, _device);
1731 TU_FROM_HANDLE(tu_device_memory, mem, _memory);
1732 VkResult result;
1733
1734 if (mem == NULL) {
1735 *ppData = NULL;
1736 return VK_SUCCESS;
1737 }
1738
1739 if (mem->user_ptr) {
1740 *ppData = mem->user_ptr;
1741 } else if (!mem->map) {
1742 result = tu_bo_map(device, &mem->bo);
1743 if (result != VK_SUCCESS)
1744 return result;
1745 *ppData = mem->map = mem->bo.map;
1746 } else
1747 *ppData = mem->map;
1748
1749 if (*ppData) {
1750 *ppData += offset;
1751 return VK_SUCCESS;
1752 }
1753
1754 return vk_error(device->instance, VK_ERROR_MEMORY_MAP_FAILED);
1755 }
1756
1757 void
1758 tu_UnmapMemory(VkDevice _device, VkDeviceMemory _memory)
1759 {
1760 /* I do not see any unmapping done by the freedreno Gallium driver. */
1761 }
1762
1763 VkResult
1764 tu_FlushMappedMemoryRanges(VkDevice _device,
1765 uint32_t memoryRangeCount,
1766 const VkMappedMemoryRange *pMemoryRanges)
1767 {
1768 return VK_SUCCESS;
1769 }
1770
1771 VkResult
1772 tu_InvalidateMappedMemoryRanges(VkDevice _device,
1773 uint32_t memoryRangeCount,
1774 const VkMappedMemoryRange *pMemoryRanges)
1775 {
1776 return VK_SUCCESS;
1777 }
1778
1779 void
1780 tu_GetBufferMemoryRequirements(VkDevice _device,
1781 VkBuffer _buffer,
1782 VkMemoryRequirements *pMemoryRequirements)
1783 {
1784 TU_FROM_HANDLE(tu_buffer, buffer, _buffer);
1785
1786 pMemoryRequirements->memoryTypeBits = 1;
1787 pMemoryRequirements->alignment = 64;
1788 pMemoryRequirements->size =
1789 align64(buffer->size, pMemoryRequirements->alignment);
1790 }
1791
1792 void
1793 tu_GetBufferMemoryRequirements2(
1794 VkDevice device,
1795 const VkBufferMemoryRequirementsInfo2 *pInfo,
1796 VkMemoryRequirements2 *pMemoryRequirements)
1797 {
1798 tu_GetBufferMemoryRequirements(device, pInfo->buffer,
1799 &pMemoryRequirements->memoryRequirements);
1800 }
1801
1802 void
1803 tu_GetImageMemoryRequirements(VkDevice _device,
1804 VkImage _image,
1805 VkMemoryRequirements *pMemoryRequirements)
1806 {
1807 TU_FROM_HANDLE(tu_image, image, _image);
1808
1809 pMemoryRequirements->memoryTypeBits = 1;
1810 pMemoryRequirements->size = image->layout.size;
1811 pMemoryRequirements->alignment = image->layout.base_align;
1812 }
1813
1814 void
1815 tu_GetImageMemoryRequirements2(VkDevice device,
1816 const VkImageMemoryRequirementsInfo2 *pInfo,
1817 VkMemoryRequirements2 *pMemoryRequirements)
1818 {
1819 tu_GetImageMemoryRequirements(device, pInfo->image,
1820 &pMemoryRequirements->memoryRequirements);
1821 }
1822
1823 void
1824 tu_GetImageSparseMemoryRequirements(
1825 VkDevice device,
1826 VkImage image,
1827 uint32_t *pSparseMemoryRequirementCount,
1828 VkSparseImageMemoryRequirements *pSparseMemoryRequirements)
1829 {
1830 tu_stub();
1831 }
1832
1833 void
1834 tu_GetImageSparseMemoryRequirements2(
1835 VkDevice device,
1836 const VkImageSparseMemoryRequirementsInfo2 *pInfo,
1837 uint32_t *pSparseMemoryRequirementCount,
1838 VkSparseImageMemoryRequirements2 *pSparseMemoryRequirements)
1839 {
1840 tu_stub();
1841 }
1842
1843 void
1844 tu_GetDeviceMemoryCommitment(VkDevice device,
1845 VkDeviceMemory memory,
1846 VkDeviceSize *pCommittedMemoryInBytes)
1847 {
1848 *pCommittedMemoryInBytes = 0;
1849 }
1850
1851 VkResult
1852 tu_BindBufferMemory2(VkDevice device,
1853 uint32_t bindInfoCount,
1854 const VkBindBufferMemoryInfo *pBindInfos)
1855 {
1856 for (uint32_t i = 0; i < bindInfoCount; ++i) {
1857 TU_FROM_HANDLE(tu_device_memory, mem, pBindInfos[i].memory);
1858 TU_FROM_HANDLE(tu_buffer, buffer, pBindInfos[i].buffer);
1859
1860 if (mem) {
1861 buffer->bo = &mem->bo;
1862 buffer->bo_offset = pBindInfos[i].memoryOffset;
1863 } else {
1864 buffer->bo = NULL;
1865 }
1866 }
1867 return VK_SUCCESS;
1868 }
1869
1870 VkResult
1871 tu_BindBufferMemory(VkDevice device,
1872 VkBuffer buffer,
1873 VkDeviceMemory memory,
1874 VkDeviceSize memoryOffset)
1875 {
1876 const VkBindBufferMemoryInfo info = {
1877 .sType = VK_STRUCTURE_TYPE_BIND_BUFFER_MEMORY_INFO,
1878 .buffer = buffer,
1879 .memory = memory,
1880 .memoryOffset = memoryOffset
1881 };
1882
1883 return tu_BindBufferMemory2(device, 1, &info);
1884 }
1885
1886 VkResult
1887 tu_BindImageMemory2(VkDevice device,
1888 uint32_t bindInfoCount,
1889 const VkBindImageMemoryInfo *pBindInfos)
1890 {
1891 for (uint32_t i = 0; i < bindInfoCount; ++i) {
1892 TU_FROM_HANDLE(tu_image, image, pBindInfos[i].image);
1893 TU_FROM_HANDLE(tu_device_memory, mem, pBindInfos[i].memory);
1894
1895 if (mem) {
1896 image->bo = &mem->bo;
1897 image->bo_offset = pBindInfos[i].memoryOffset;
1898 } else {
1899 image->bo = NULL;
1900 image->bo_offset = 0;
1901 }
1902 }
1903
1904 return VK_SUCCESS;
1905 }
1906
1907 VkResult
1908 tu_BindImageMemory(VkDevice device,
1909 VkImage image,
1910 VkDeviceMemory memory,
1911 VkDeviceSize memoryOffset)
1912 {
1913 const VkBindImageMemoryInfo info = {
1914 .sType = VK_STRUCTURE_TYPE_BIND_BUFFER_MEMORY_INFO,
1915 .image = image,
1916 .memory = memory,
1917 .memoryOffset = memoryOffset
1918 };
1919
1920 return tu_BindImageMemory2(device, 1, &info);
1921 }
1922
1923 VkResult
1924 tu_QueueBindSparse(VkQueue _queue,
1925 uint32_t bindInfoCount,
1926 const VkBindSparseInfo *pBindInfo,
1927 VkFence _fence)
1928 {
1929 return VK_SUCCESS;
1930 }
1931
1932 // Queue semaphore functions
1933
1934 VkResult
1935 tu_CreateSemaphore(VkDevice _device,
1936 const VkSemaphoreCreateInfo *pCreateInfo,
1937 const VkAllocationCallbacks *pAllocator,
1938 VkSemaphore *pSemaphore)
1939 {
1940 TU_FROM_HANDLE(tu_device, device, _device);
1941
1942 struct tu_semaphore *sem =
1943 vk_alloc2(&device->alloc, pAllocator, sizeof(*sem), 8,
1944 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
1945 if (!sem)
1946 return vk_error(device->instance, VK_ERROR_OUT_OF_HOST_MEMORY);
1947
1948 *pSemaphore = tu_semaphore_to_handle(sem);
1949 return VK_SUCCESS;
1950 }
1951
1952 void
1953 tu_DestroySemaphore(VkDevice _device,
1954 VkSemaphore _semaphore,
1955 const VkAllocationCallbacks *pAllocator)
1956 {
1957 TU_FROM_HANDLE(tu_device, device, _device);
1958 TU_FROM_HANDLE(tu_semaphore, sem, _semaphore);
1959 if (!_semaphore)
1960 return;
1961
1962 vk_free2(&device->alloc, pAllocator, sem);
1963 }
1964
1965 VkResult
1966 tu_CreateEvent(VkDevice _device,
1967 const VkEventCreateInfo *pCreateInfo,
1968 const VkAllocationCallbacks *pAllocator,
1969 VkEvent *pEvent)
1970 {
1971 TU_FROM_HANDLE(tu_device, device, _device);
1972 struct tu_event *event =
1973 vk_alloc2(&device->alloc, pAllocator, sizeof(*event), 8,
1974 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
1975
1976 if (!event)
1977 return vk_error(device->instance, VK_ERROR_OUT_OF_HOST_MEMORY);
1978
1979 VkResult result = tu_bo_init_new(device, &event->bo, 0x1000);
1980 if (result != VK_SUCCESS)
1981 goto fail_alloc;
1982
1983 result = tu_bo_map(device, &event->bo);
1984 if (result != VK_SUCCESS)
1985 goto fail_map;
1986
1987 *pEvent = tu_event_to_handle(event);
1988
1989 return VK_SUCCESS;
1990
1991 fail_map:
1992 tu_bo_finish(device, &event->bo);
1993 fail_alloc:
1994 vk_free2(&device->alloc, pAllocator, event);
1995 return vk_error(device->instance, VK_ERROR_OUT_OF_HOST_MEMORY);
1996 }
1997
1998 void
1999 tu_DestroyEvent(VkDevice _device,
2000 VkEvent _event,
2001 const VkAllocationCallbacks *pAllocator)
2002 {
2003 TU_FROM_HANDLE(tu_device, device, _device);
2004 TU_FROM_HANDLE(tu_event, event, _event);
2005
2006 if (!event)
2007 return;
2008
2009 tu_bo_finish(device, &event->bo);
2010 vk_free2(&device->alloc, pAllocator, event);
2011 }
2012
2013 VkResult
2014 tu_GetEventStatus(VkDevice _device, VkEvent _event)
2015 {
2016 TU_FROM_HANDLE(tu_event, event, _event);
2017
2018 if (*(uint64_t*) event->bo.map == 1)
2019 return VK_EVENT_SET;
2020 return VK_EVENT_RESET;
2021 }
2022
2023 VkResult
2024 tu_SetEvent(VkDevice _device, VkEvent _event)
2025 {
2026 TU_FROM_HANDLE(tu_event, event, _event);
2027 *(uint64_t*) event->bo.map = 1;
2028
2029 return VK_SUCCESS;
2030 }
2031
2032 VkResult
2033 tu_ResetEvent(VkDevice _device, VkEvent _event)
2034 {
2035 TU_FROM_HANDLE(tu_event, event, _event);
2036 *(uint64_t*) event->bo.map = 0;
2037
2038 return VK_SUCCESS;
2039 }
2040
2041 VkResult
2042 tu_CreateBuffer(VkDevice _device,
2043 const VkBufferCreateInfo *pCreateInfo,
2044 const VkAllocationCallbacks *pAllocator,
2045 VkBuffer *pBuffer)
2046 {
2047 TU_FROM_HANDLE(tu_device, device, _device);
2048 struct tu_buffer *buffer;
2049
2050 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO);
2051
2052 buffer = vk_alloc2(&device->alloc, pAllocator, sizeof(*buffer), 8,
2053 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
2054 if (buffer == NULL)
2055 return vk_error(device->instance, VK_ERROR_OUT_OF_HOST_MEMORY);
2056
2057 buffer->size = pCreateInfo->size;
2058 buffer->usage = pCreateInfo->usage;
2059 buffer->flags = pCreateInfo->flags;
2060
2061 *pBuffer = tu_buffer_to_handle(buffer);
2062
2063 return VK_SUCCESS;
2064 }
2065
2066 void
2067 tu_DestroyBuffer(VkDevice _device,
2068 VkBuffer _buffer,
2069 const VkAllocationCallbacks *pAllocator)
2070 {
2071 TU_FROM_HANDLE(tu_device, device, _device);
2072 TU_FROM_HANDLE(tu_buffer, buffer, _buffer);
2073
2074 if (!buffer)
2075 return;
2076
2077 vk_free2(&device->alloc, pAllocator, buffer);
2078 }
2079
2080 VkResult
2081 tu_CreateFramebuffer(VkDevice _device,
2082 const VkFramebufferCreateInfo *pCreateInfo,
2083 const VkAllocationCallbacks *pAllocator,
2084 VkFramebuffer *pFramebuffer)
2085 {
2086 TU_FROM_HANDLE(tu_device, device, _device);
2087 struct tu_framebuffer *framebuffer;
2088
2089 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO);
2090
2091 size_t size = sizeof(*framebuffer) + sizeof(struct tu_attachment_info) *
2092 pCreateInfo->attachmentCount;
2093 framebuffer = vk_alloc2(&device->alloc, pAllocator, size, 8,
2094 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
2095 if (framebuffer == NULL)
2096 return vk_error(device->instance, VK_ERROR_OUT_OF_HOST_MEMORY);
2097
2098 framebuffer->attachment_count = pCreateInfo->attachmentCount;
2099 framebuffer->width = pCreateInfo->width;
2100 framebuffer->height = pCreateInfo->height;
2101 framebuffer->layers = pCreateInfo->layers;
2102 for (uint32_t i = 0; i < pCreateInfo->attachmentCount; i++) {
2103 VkImageView _iview = pCreateInfo->pAttachments[i];
2104 struct tu_image_view *iview = tu_image_view_from_handle(_iview);
2105 framebuffer->attachments[i].attachment = iview;
2106 }
2107
2108 *pFramebuffer = tu_framebuffer_to_handle(framebuffer);
2109 return VK_SUCCESS;
2110 }
2111
2112 void
2113 tu_DestroyFramebuffer(VkDevice _device,
2114 VkFramebuffer _fb,
2115 const VkAllocationCallbacks *pAllocator)
2116 {
2117 TU_FROM_HANDLE(tu_device, device, _device);
2118 TU_FROM_HANDLE(tu_framebuffer, fb, _fb);
2119
2120 if (!fb)
2121 return;
2122 vk_free2(&device->alloc, pAllocator, fb);
2123 }
2124
2125 static enum a6xx_tex_clamp
2126 tu6_tex_wrap(VkSamplerAddressMode address_mode)
2127 {
2128 switch (address_mode) {
2129 case VK_SAMPLER_ADDRESS_MODE_REPEAT:
2130 return A6XX_TEX_REPEAT;
2131 case VK_SAMPLER_ADDRESS_MODE_MIRRORED_REPEAT:
2132 return A6XX_TEX_MIRROR_REPEAT;
2133 case VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE:
2134 return A6XX_TEX_CLAMP_TO_EDGE;
2135 case VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_BORDER:
2136 return A6XX_TEX_CLAMP_TO_BORDER;
2137 case VK_SAMPLER_ADDRESS_MODE_MIRROR_CLAMP_TO_EDGE:
2138 /* only works for PoT.. need to emulate otherwise! */
2139 return A6XX_TEX_MIRROR_CLAMP;
2140 default:
2141 unreachable("illegal tex wrap mode");
2142 break;
2143 }
2144 }
2145
2146 static enum a6xx_tex_filter
2147 tu6_tex_filter(VkFilter filter, unsigned aniso)
2148 {
2149 switch (filter) {
2150 case VK_FILTER_NEAREST:
2151 return A6XX_TEX_NEAREST;
2152 case VK_FILTER_LINEAR:
2153 return aniso ? A6XX_TEX_ANISO : A6XX_TEX_LINEAR;
2154 case VK_FILTER_CUBIC_EXT:
2155 return A6XX_TEX_CUBIC;
2156 default:
2157 unreachable("illegal texture filter");
2158 break;
2159 }
2160 }
2161
2162 static inline enum adreno_compare_func
2163 tu6_compare_func(VkCompareOp op)
2164 {
2165 return (enum adreno_compare_func) op;
2166 }
2167
2168 static inline enum a6xx_reduction_mode
2169 tu6_reduction_mode(VkSamplerReductionMode reduction_mode)
2170 {
2171 /* note: vulkan enum matches hw */
2172
2173 return (enum a6xx_reduction_mode) reduction_mode;
2174 }
2175
2176 static void
2177 tu_init_sampler(struct tu_device *device,
2178 struct tu_sampler *sampler,
2179 const VkSamplerCreateInfo *pCreateInfo)
2180 {
2181 const struct VkSamplerReductionModeCreateInfo *reduction =
2182 vk_find_struct_const(pCreateInfo->pNext, SAMPLER_REDUCTION_MODE_CREATE_INFO);
2183 const struct VkSamplerYcbcrConversionInfo *ycbcr_conversion =
2184 vk_find_struct_const(pCreateInfo->pNext, SAMPLER_YCBCR_CONVERSION_INFO);
2185
2186 unsigned aniso = pCreateInfo->anisotropyEnable ?
2187 util_last_bit(MIN2((uint32_t)pCreateInfo->maxAnisotropy >> 1, 8)) : 0;
2188 bool miplinear = (pCreateInfo->mipmapMode == VK_SAMPLER_MIPMAP_MODE_LINEAR);
2189
2190 sampler->descriptor[0] =
2191 COND(miplinear, A6XX_TEX_SAMP_0_MIPFILTER_LINEAR_NEAR) |
2192 A6XX_TEX_SAMP_0_XY_MAG(tu6_tex_filter(pCreateInfo->magFilter, aniso)) |
2193 A6XX_TEX_SAMP_0_XY_MIN(tu6_tex_filter(pCreateInfo->minFilter, aniso)) |
2194 A6XX_TEX_SAMP_0_ANISO(aniso) |
2195 A6XX_TEX_SAMP_0_WRAP_S(tu6_tex_wrap(pCreateInfo->addressModeU)) |
2196 A6XX_TEX_SAMP_0_WRAP_T(tu6_tex_wrap(pCreateInfo->addressModeV)) |
2197 A6XX_TEX_SAMP_0_WRAP_R(tu6_tex_wrap(pCreateInfo->addressModeW)) |
2198 A6XX_TEX_SAMP_0_LOD_BIAS(pCreateInfo->mipLodBias);
2199 sampler->descriptor[1] =
2200 /* COND(!cso->seamless_cube_map, A6XX_TEX_SAMP_1_CUBEMAPSEAMLESSFILTOFF) | */
2201 COND(pCreateInfo->unnormalizedCoordinates, A6XX_TEX_SAMP_1_UNNORM_COORDS) |
2202 A6XX_TEX_SAMP_1_MIN_LOD(pCreateInfo->minLod) |
2203 A6XX_TEX_SAMP_1_MAX_LOD(pCreateInfo->maxLod) |
2204 COND(pCreateInfo->compareEnable,
2205 A6XX_TEX_SAMP_1_COMPARE_FUNC(tu6_compare_func(pCreateInfo->compareOp)));
2206 /* This is an offset into the border_color BO, which we fill with all the
2207 * possible Vulkan border colors in the correct order, so we can just use
2208 * the Vulkan enum with no translation necessary.
2209 */
2210 sampler->descriptor[2] =
2211 A6XX_TEX_SAMP_2_BCOLOR_OFFSET((unsigned) pCreateInfo->borderColor *
2212 sizeof(struct bcolor_entry));
2213 sampler->descriptor[3] = 0;
2214
2215 if (reduction) {
2216 sampler->descriptor[2] |= A6XX_TEX_SAMP_2_REDUCTION_MODE(
2217 tu6_reduction_mode(reduction->reductionMode));
2218 }
2219
2220 sampler->ycbcr_sampler = ycbcr_conversion ?
2221 tu_sampler_ycbcr_conversion_from_handle(ycbcr_conversion->conversion) : NULL;
2222
2223 if (sampler->ycbcr_sampler &&
2224 sampler->ycbcr_sampler->chroma_filter == VK_FILTER_LINEAR) {
2225 sampler->descriptor[2] |= A6XX_TEX_SAMP_2_CHROMA_LINEAR;
2226 }
2227
2228 /* TODO:
2229 * A6XX_TEX_SAMP_1_MIPFILTER_LINEAR_FAR disables mipmapping, but vk has no NONE mipfilter?
2230 */
2231 }
2232
2233 VkResult
2234 tu_CreateSampler(VkDevice _device,
2235 const VkSamplerCreateInfo *pCreateInfo,
2236 const VkAllocationCallbacks *pAllocator,
2237 VkSampler *pSampler)
2238 {
2239 TU_FROM_HANDLE(tu_device, device, _device);
2240 struct tu_sampler *sampler;
2241
2242 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO);
2243
2244 sampler = vk_alloc2(&device->alloc, pAllocator, sizeof(*sampler), 8,
2245 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
2246 if (!sampler)
2247 return vk_error(device->instance, VK_ERROR_OUT_OF_HOST_MEMORY);
2248
2249 tu_init_sampler(device, sampler, pCreateInfo);
2250 *pSampler = tu_sampler_to_handle(sampler);
2251
2252 return VK_SUCCESS;
2253 }
2254
2255 void
2256 tu_DestroySampler(VkDevice _device,
2257 VkSampler _sampler,
2258 const VkAllocationCallbacks *pAllocator)
2259 {
2260 TU_FROM_HANDLE(tu_device, device, _device);
2261 TU_FROM_HANDLE(tu_sampler, sampler, _sampler);
2262
2263 if (!sampler)
2264 return;
2265 vk_free2(&device->alloc, pAllocator, sampler);
2266 }
2267
2268 /* vk_icd.h does not declare this function, so we declare it here to
2269 * suppress Wmissing-prototypes.
2270 */
2271 PUBLIC VKAPI_ATTR VkResult VKAPI_CALL
2272 vk_icdNegotiateLoaderICDInterfaceVersion(uint32_t *pSupportedVersion);
2273
2274 PUBLIC VKAPI_ATTR VkResult VKAPI_CALL
2275 vk_icdNegotiateLoaderICDInterfaceVersion(uint32_t *pSupportedVersion)
2276 {
2277 /* For the full details on loader interface versioning, see
2278 * <https://github.com/KhronosGroup/Vulkan-LoaderAndValidationLayers/blob/master/loader/LoaderAndLayerInterface.md>.
2279 * What follows is a condensed summary, to help you navigate the large and
2280 * confusing official doc.
2281 *
2282 * - Loader interface v0 is incompatible with later versions. We don't
2283 * support it.
2284 *
2285 * - In loader interface v1:
2286 * - The first ICD entrypoint called by the loader is
2287 * vk_icdGetInstanceProcAddr(). The ICD must statically expose this
2288 * entrypoint.
2289 * - The ICD must statically expose no other Vulkan symbol unless it
2290 * is linked with -Bsymbolic.
2291 * - Each dispatchable Vulkan handle created by the ICD must be
2292 * a pointer to a struct whose first member is VK_LOADER_DATA. The
2293 * ICD must initialize VK_LOADER_DATA.loadMagic to
2294 * ICD_LOADER_MAGIC.
2295 * - The loader implements vkCreate{PLATFORM}SurfaceKHR() and
2296 * vkDestroySurfaceKHR(). The ICD must be capable of working with
2297 * such loader-managed surfaces.
2298 *
2299 * - Loader interface v2 differs from v1 in:
2300 * - The first ICD entrypoint called by the loader is
2301 * vk_icdNegotiateLoaderICDInterfaceVersion(). The ICD must
2302 * statically expose this entrypoint.
2303 *
2304 * - Loader interface v3 differs from v2 in:
2305 * - The ICD must implement vkCreate{PLATFORM}SurfaceKHR(),
2306 * vkDestroySurfaceKHR(), and other API which uses VKSurfaceKHR,
2307 * because the loader no longer does so.
2308 */
2309 *pSupportedVersion = MIN2(*pSupportedVersion, 3u);
2310 return VK_SUCCESS;
2311 }
2312
2313 VkResult
2314 tu_GetMemoryFdKHR(VkDevice _device,
2315 const VkMemoryGetFdInfoKHR *pGetFdInfo,
2316 int *pFd)
2317 {
2318 TU_FROM_HANDLE(tu_device, device, _device);
2319 TU_FROM_HANDLE(tu_device_memory, memory, pGetFdInfo->memory);
2320
2321 assert(pGetFdInfo->sType == VK_STRUCTURE_TYPE_MEMORY_GET_FD_INFO_KHR);
2322
2323 /* At the moment, we support only the below handle types. */
2324 assert(pGetFdInfo->handleType ==
2325 VK_EXTERNAL_MEMORY_HANDLE_TYPE_OPAQUE_FD_BIT ||
2326 pGetFdInfo->handleType ==
2327 VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_EXT);
2328
2329 int prime_fd = tu_bo_export_dmabuf(device, &memory->bo);
2330 if (prime_fd < 0)
2331 return vk_error(device->instance, VK_ERROR_OUT_OF_DEVICE_MEMORY);
2332
2333 *pFd = prime_fd;
2334 return VK_SUCCESS;
2335 }
2336
2337 VkResult
2338 tu_GetMemoryFdPropertiesKHR(VkDevice _device,
2339 VkExternalMemoryHandleTypeFlagBits handleType,
2340 int fd,
2341 VkMemoryFdPropertiesKHR *pMemoryFdProperties)
2342 {
2343 assert(handleType == VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_EXT);
2344 pMemoryFdProperties->memoryTypeBits = 1;
2345 return VK_SUCCESS;
2346 }
2347
2348 void
2349 tu_GetPhysicalDeviceExternalSemaphoreProperties(
2350 VkPhysicalDevice physicalDevice,
2351 const VkPhysicalDeviceExternalSemaphoreInfo *pExternalSemaphoreInfo,
2352 VkExternalSemaphoreProperties *pExternalSemaphoreProperties)
2353 {
2354 pExternalSemaphoreProperties->exportFromImportedHandleTypes = 0;
2355 pExternalSemaphoreProperties->compatibleHandleTypes = 0;
2356 pExternalSemaphoreProperties->externalSemaphoreFeatures = 0;
2357 }
2358
2359 void
2360 tu_GetPhysicalDeviceExternalFenceProperties(
2361 VkPhysicalDevice physicalDevice,
2362 const VkPhysicalDeviceExternalFenceInfo *pExternalFenceInfo,
2363 VkExternalFenceProperties *pExternalFenceProperties)
2364 {
2365 pExternalFenceProperties->exportFromImportedHandleTypes = 0;
2366 pExternalFenceProperties->compatibleHandleTypes = 0;
2367 pExternalFenceProperties->externalFenceFeatures = 0;
2368 }
2369
2370 VkResult
2371 tu_CreateDebugReportCallbackEXT(
2372 VkInstance _instance,
2373 const VkDebugReportCallbackCreateInfoEXT *pCreateInfo,
2374 const VkAllocationCallbacks *pAllocator,
2375 VkDebugReportCallbackEXT *pCallback)
2376 {
2377 TU_FROM_HANDLE(tu_instance, instance, _instance);
2378 return vk_create_debug_report_callback(&instance->debug_report_callbacks,
2379 pCreateInfo, pAllocator,
2380 &instance->alloc, pCallback);
2381 }
2382
2383 void
2384 tu_DestroyDebugReportCallbackEXT(VkInstance _instance,
2385 VkDebugReportCallbackEXT _callback,
2386 const VkAllocationCallbacks *pAllocator)
2387 {
2388 TU_FROM_HANDLE(tu_instance, instance, _instance);
2389 vk_destroy_debug_report_callback(&instance->debug_report_callbacks,
2390 _callback, pAllocator, &instance->alloc);
2391 }
2392
2393 void
2394 tu_DebugReportMessageEXT(VkInstance _instance,
2395 VkDebugReportFlagsEXT flags,
2396 VkDebugReportObjectTypeEXT objectType,
2397 uint64_t object,
2398 size_t location,
2399 int32_t messageCode,
2400 const char *pLayerPrefix,
2401 const char *pMessage)
2402 {
2403 TU_FROM_HANDLE(tu_instance, instance, _instance);
2404 vk_debug_report(&instance->debug_report_callbacks, flags, objectType,
2405 object, location, messageCode, pLayerPrefix, pMessage);
2406 }
2407
2408 void
2409 tu_GetDeviceGroupPeerMemoryFeatures(
2410 VkDevice device,
2411 uint32_t heapIndex,
2412 uint32_t localDeviceIndex,
2413 uint32_t remoteDeviceIndex,
2414 VkPeerMemoryFeatureFlags *pPeerMemoryFeatures)
2415 {
2416 assert(localDeviceIndex == remoteDeviceIndex);
2417
2418 *pPeerMemoryFeatures = VK_PEER_MEMORY_FEATURE_COPY_SRC_BIT |
2419 VK_PEER_MEMORY_FEATURE_COPY_DST_BIT |
2420 VK_PEER_MEMORY_FEATURE_GENERIC_SRC_BIT |
2421 VK_PEER_MEMORY_FEATURE_GENERIC_DST_BIT;
2422 }
2423
2424 void tu_GetPhysicalDeviceMultisamplePropertiesEXT(
2425 VkPhysicalDevice physicalDevice,
2426 VkSampleCountFlagBits samples,
2427 VkMultisamplePropertiesEXT* pMultisampleProperties)
2428 {
2429 TU_FROM_HANDLE(tu_physical_device, pdevice, physicalDevice);
2430
2431 if (samples <= VK_SAMPLE_COUNT_4_BIT && pdevice->supported_extensions.EXT_sample_locations)
2432 pMultisampleProperties->maxSampleLocationGridSize = (VkExtent2D){ 1, 1 };
2433 else
2434 pMultisampleProperties->maxSampleLocationGridSize = (VkExtent2D){ 0, 0 };
2435 }