ddc4db143d5b11cf0691fa3cccc26c9384f256be
[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/sysinfo.h>
35 #include <unistd.h>
36 #include <xf86drm.h>
37
38 #include "compiler/glsl_types.h"
39 #include "util/debug.h"
40 #include "util/disk_cache.h"
41 #include "util/u_atomic.h"
42 #include "vk_format.h"
43 #include "vk_util.h"
44
45 #include "drm-uapi/msm_drm.h"
46
47 /* for fd_get_driver/device_uuid() */
48 #include "freedreno/common/freedreno_uuid.h"
49
50 static int
51 tu_device_get_cache_uuid(uint16_t family, void *uuid)
52 {
53 uint32_t mesa_timestamp;
54 uint16_t f = family;
55 memset(uuid, 0, VK_UUID_SIZE);
56 if (!disk_cache_get_function_timestamp(tu_device_get_cache_uuid,
57 &mesa_timestamp))
58 return -1;
59
60 memcpy(uuid, &mesa_timestamp, 4);
61 memcpy((char *) uuid + 4, &f, 2);
62 snprintf((char *) uuid + 6, VK_UUID_SIZE - 10, "tu");
63 return 0;
64 }
65
66 VkResult
67 tu_physical_device_init(struct tu_physical_device *device,
68 struct tu_instance *instance)
69 {
70 VkResult result = VK_SUCCESS;
71
72 memset(device->name, 0, sizeof(device->name));
73 sprintf(device->name, "FD%d", device->gpu_id);
74
75 device->limited_z24s8 = (device->gpu_id == 630);
76
77 switch (device->gpu_id) {
78 case 618:
79 device->ccu_offset_gmem = 0x7c000; /* 0x7e000 in some cases? */
80 device->ccu_offset_bypass = 0x10000;
81 device->tile_align_w = 32;
82 device->magic.PC_UNKNOWN_9805 = 0x0;
83 device->magic.SP_UNKNOWN_A0F8 = 0x0;
84 break;
85 case 630:
86 case 640:
87 device->ccu_offset_gmem = 0xf8000;
88 device->ccu_offset_bypass = 0x20000;
89 device->tile_align_w = 32;
90 device->magic.PC_UNKNOWN_9805 = 0x1;
91 device->magic.SP_UNKNOWN_A0F8 = 0x1;
92 break;
93 case 650:
94 device->ccu_offset_gmem = 0x114000;
95 device->ccu_offset_bypass = 0x30000;
96 device->tile_align_w = 96;
97 device->magic.PC_UNKNOWN_9805 = 0x2;
98 device->magic.SP_UNKNOWN_A0F8 = 0x2;
99 break;
100 default:
101 result = vk_errorf(instance, VK_ERROR_INITIALIZATION_FAILED,
102 "device %s is unsupported", device->name);
103 goto fail;
104 }
105 if (tu_device_get_cache_uuid(device->gpu_id, device->cache_uuid)) {
106 result = vk_errorf(instance, VK_ERROR_INITIALIZATION_FAILED,
107 "cannot generate UUID");
108 goto fail;
109 }
110
111 /* The gpu id is already embedded in the uuid so we just pass "tu"
112 * when creating the cache.
113 */
114 char buf[VK_UUID_SIZE * 2 + 1];
115 disk_cache_format_hex_id(buf, device->cache_uuid, VK_UUID_SIZE * 2);
116 device->disk_cache = disk_cache_create(device->name, buf, 0);
117
118 fprintf(stderr, "WARNING: tu is not a conformant vulkan implementation, "
119 "testing use only.\n");
120
121 fd_get_driver_uuid(device->driver_uuid);
122 fd_get_device_uuid(device->device_uuid, device->gpu_id);
123
124 tu_physical_device_get_supported_extensions(device, &device->supported_extensions);
125
126 if (result != VK_SUCCESS) {
127 vk_error(instance, result);
128 goto fail;
129 }
130
131 result = tu_wsi_init(device);
132 if (result != VK_SUCCESS) {
133 vk_error(instance, result);
134 goto fail;
135 }
136
137 return VK_SUCCESS;
138
139 fail:
140 close(device->local_fd);
141 if (device->master_fd != -1)
142 close(device->master_fd);
143 return result;
144 }
145
146 static void
147 tu_physical_device_finish(struct tu_physical_device *device)
148 {
149 tu_wsi_finish(device);
150
151 disk_cache_destroy(device->disk_cache);
152 close(device->local_fd);
153 if (device->master_fd != -1)
154 close(device->master_fd);
155
156 vk_object_base_finish(&device->base);
157 }
158
159 static VKAPI_ATTR void *
160 default_alloc_func(void *pUserData,
161 size_t size,
162 size_t align,
163 VkSystemAllocationScope allocationScope)
164 {
165 return malloc(size);
166 }
167
168 static VKAPI_ATTR void *
169 default_realloc_func(void *pUserData,
170 void *pOriginal,
171 size_t size,
172 size_t align,
173 VkSystemAllocationScope allocationScope)
174 {
175 return realloc(pOriginal, size);
176 }
177
178 static VKAPI_ATTR void
179 default_free_func(void *pUserData, void *pMemory)
180 {
181 free(pMemory);
182 }
183
184 static const VkAllocationCallbacks default_alloc = {
185 .pUserData = NULL,
186 .pfnAllocation = default_alloc_func,
187 .pfnReallocation = default_realloc_func,
188 .pfnFree = default_free_func,
189 };
190
191 static const struct debug_control tu_debug_options[] = {
192 { "startup", TU_DEBUG_STARTUP },
193 { "nir", TU_DEBUG_NIR },
194 { "ir3", TU_DEBUG_IR3 },
195 { "nobin", TU_DEBUG_NOBIN },
196 { "sysmem", TU_DEBUG_SYSMEM },
197 { "forcebin", TU_DEBUG_FORCEBIN },
198 { "noubwc", TU_DEBUG_NOUBWC },
199 { NULL, 0 }
200 };
201
202 const char *
203 tu_get_debug_option_name(int id)
204 {
205 assert(id < ARRAY_SIZE(tu_debug_options) - 1);
206 return tu_debug_options[id].string;
207 }
208
209 static int
210 tu_get_instance_extension_index(const char *name)
211 {
212 for (unsigned i = 0; i < TU_INSTANCE_EXTENSION_COUNT; ++i) {
213 if (strcmp(name, tu_instance_extensions[i].extensionName) == 0)
214 return i;
215 }
216 return -1;
217 }
218
219 VkResult
220 tu_CreateInstance(const VkInstanceCreateInfo *pCreateInfo,
221 const VkAllocationCallbacks *pAllocator,
222 VkInstance *pInstance)
223 {
224 struct tu_instance *instance;
225 VkResult result;
226
227 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO);
228
229 uint32_t client_version;
230 if (pCreateInfo->pApplicationInfo &&
231 pCreateInfo->pApplicationInfo->apiVersion != 0) {
232 client_version = pCreateInfo->pApplicationInfo->apiVersion;
233 } else {
234 tu_EnumerateInstanceVersion(&client_version);
235 }
236
237 instance = vk_zalloc2(&default_alloc, pAllocator, sizeof(*instance), 8,
238 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
239
240 if (!instance)
241 return vk_error(NULL, VK_ERROR_OUT_OF_HOST_MEMORY);
242
243 vk_object_base_init(NULL, &instance->base, VK_OBJECT_TYPE_INSTANCE);
244
245 if (pAllocator)
246 instance->alloc = *pAllocator;
247 else
248 instance->alloc = default_alloc;
249
250 instance->api_version = client_version;
251 instance->physical_device_count = -1;
252
253 instance->debug_flags =
254 parse_debug_string(getenv("TU_DEBUG"), tu_debug_options);
255
256 if (instance->debug_flags & TU_DEBUG_STARTUP)
257 tu_logi("Created an instance");
258
259 for (uint32_t i = 0; i < pCreateInfo->enabledExtensionCount; i++) {
260 const char *ext_name = pCreateInfo->ppEnabledExtensionNames[i];
261 int index = tu_get_instance_extension_index(ext_name);
262
263 if (index < 0 || !tu_instance_extensions_supported.extensions[index]) {
264 vk_object_base_finish(&instance->base);
265 vk_free2(&default_alloc, pAllocator, instance);
266 return vk_error(instance, VK_ERROR_EXTENSION_NOT_PRESENT);
267 }
268
269 instance->enabled_extensions.extensions[index] = true;
270 }
271
272 result = vk_debug_report_instance_init(&instance->debug_report_callbacks);
273 if (result != VK_SUCCESS) {
274 vk_object_base_finish(&instance->base);
275 vk_free2(&default_alloc, pAllocator, instance);
276 return vk_error(instance, result);
277 }
278
279 glsl_type_singleton_init_or_ref();
280
281 VG(VALGRIND_CREATE_MEMPOOL(instance, 0, false));
282
283 *pInstance = tu_instance_to_handle(instance);
284
285 return VK_SUCCESS;
286 }
287
288 void
289 tu_DestroyInstance(VkInstance _instance,
290 const VkAllocationCallbacks *pAllocator)
291 {
292 TU_FROM_HANDLE(tu_instance, instance, _instance);
293
294 if (!instance)
295 return;
296
297 for (int i = 0; i < instance->physical_device_count; ++i) {
298 tu_physical_device_finish(instance->physical_devices + i);
299 }
300
301 VG(VALGRIND_DESTROY_MEMPOOL(instance));
302
303 glsl_type_singleton_decref();
304
305 vk_debug_report_instance_destroy(&instance->debug_report_callbacks);
306
307 vk_object_base_finish(&instance->base);
308 vk_free(&instance->alloc, instance);
309 }
310
311 VkResult
312 tu_EnumeratePhysicalDevices(VkInstance _instance,
313 uint32_t *pPhysicalDeviceCount,
314 VkPhysicalDevice *pPhysicalDevices)
315 {
316 TU_FROM_HANDLE(tu_instance, instance, _instance);
317 VK_OUTARRAY_MAKE(out, pPhysicalDevices, pPhysicalDeviceCount);
318
319 VkResult result;
320
321 if (instance->physical_device_count < 0) {
322 result = tu_enumerate_devices(instance);
323 if (result != VK_SUCCESS && result != VK_ERROR_INCOMPATIBLE_DRIVER)
324 return result;
325 }
326
327 for (uint32_t i = 0; i < instance->physical_device_count; ++i) {
328 vk_outarray_append(&out, p)
329 {
330 *p = tu_physical_device_to_handle(instance->physical_devices + i);
331 }
332 }
333
334 return vk_outarray_status(&out);
335 }
336
337 VkResult
338 tu_EnumeratePhysicalDeviceGroups(
339 VkInstance _instance,
340 uint32_t *pPhysicalDeviceGroupCount,
341 VkPhysicalDeviceGroupProperties *pPhysicalDeviceGroupProperties)
342 {
343 TU_FROM_HANDLE(tu_instance, instance, _instance);
344 VK_OUTARRAY_MAKE(out, pPhysicalDeviceGroupProperties,
345 pPhysicalDeviceGroupCount);
346 VkResult result;
347
348 if (instance->physical_device_count < 0) {
349 result = tu_enumerate_devices(instance);
350 if (result != VK_SUCCESS && result != VK_ERROR_INCOMPATIBLE_DRIVER)
351 return result;
352 }
353
354 for (uint32_t i = 0; i < instance->physical_device_count; ++i) {
355 vk_outarray_append(&out, p)
356 {
357 p->physicalDeviceCount = 1;
358 p->physicalDevices[0] =
359 tu_physical_device_to_handle(instance->physical_devices + i);
360 p->subsetAllocation = false;
361 }
362 }
363
364 return vk_outarray_status(&out);
365 }
366
367 void
368 tu_GetPhysicalDeviceFeatures(VkPhysicalDevice physicalDevice,
369 VkPhysicalDeviceFeatures *pFeatures)
370 {
371 memset(pFeatures, 0, sizeof(*pFeatures));
372
373 *pFeatures = (VkPhysicalDeviceFeatures) {
374 .robustBufferAccess = true,
375 .fullDrawIndexUint32 = true,
376 .imageCubeArray = true,
377 .independentBlend = true,
378 .geometryShader = true,
379 .tessellationShader = true,
380 .sampleRateShading = true,
381 .dualSrcBlend = true,
382 .logicOp = true,
383 .multiDrawIndirect = true,
384 .drawIndirectFirstInstance = true,
385 .depthClamp = true,
386 .depthBiasClamp = true,
387 .fillModeNonSolid = true,
388 .depthBounds = true,
389 .wideLines = false,
390 .largePoints = true,
391 .alphaToOne = true,
392 .multiViewport = false,
393 .samplerAnisotropy = true,
394 .textureCompressionETC2 = true,
395 .textureCompressionASTC_LDR = true,
396 .textureCompressionBC = true,
397 .occlusionQueryPrecise = true,
398 .pipelineStatisticsQuery = false,
399 .vertexPipelineStoresAndAtomics = true,
400 .fragmentStoresAndAtomics = true,
401 .shaderTessellationAndGeometryPointSize = false,
402 .shaderImageGatherExtended = false,
403 .shaderStorageImageExtendedFormats = false,
404 .shaderStorageImageMultisample = false,
405 .shaderUniformBufferArrayDynamicIndexing = true,
406 .shaderSampledImageArrayDynamicIndexing = true,
407 .shaderStorageBufferArrayDynamicIndexing = true,
408 .shaderStorageImageArrayDynamicIndexing = true,
409 .shaderStorageImageReadWithoutFormat = false,
410 .shaderStorageImageWriteWithoutFormat = false,
411 .shaderClipDistance = false,
412 .shaderCullDistance = false,
413 .shaderFloat64 = false,
414 .shaderInt64 = false,
415 .shaderInt16 = false,
416 .sparseBinding = false,
417 .variableMultisampleRate = false,
418 .inheritedQueries = false,
419 };
420 }
421
422 void
423 tu_GetPhysicalDeviceFeatures2(VkPhysicalDevice physicalDevice,
424 VkPhysicalDeviceFeatures2 *pFeatures)
425 {
426 vk_foreach_struct(ext, pFeatures->pNext)
427 {
428 switch (ext->sType) {
429 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VULKAN_1_1_FEATURES: {
430 VkPhysicalDeviceVulkan11Features *features = (void *) ext;
431 features->storageBuffer16BitAccess = false;
432 features->uniformAndStorageBuffer16BitAccess = false;
433 features->storagePushConstant16 = false;
434 features->storageInputOutput16 = false;
435 features->multiview = false;
436 features->multiviewGeometryShader = false;
437 features->multiviewTessellationShader = false;
438 features->variablePointersStorageBuffer = true;
439 features->variablePointers = true;
440 features->protectedMemory = false;
441 features->samplerYcbcrConversion = true;
442 features->shaderDrawParameters = true;
443 break;
444 }
445 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VULKAN_1_2_FEATURES: {
446 VkPhysicalDeviceVulkan12Features *features = (void *) ext;
447 features->samplerMirrorClampToEdge = true;
448 features->drawIndirectCount = true;
449 features->storageBuffer8BitAccess = false;
450 features->uniformAndStorageBuffer8BitAccess = false;
451 features->storagePushConstant8 = false;
452 features->shaderBufferInt64Atomics = false;
453 features->shaderSharedInt64Atomics = false;
454 features->shaderFloat16 = false;
455 features->shaderInt8 = false;
456
457 features->descriptorIndexing = false;
458 features->shaderInputAttachmentArrayDynamicIndexing = false;
459 features->shaderUniformTexelBufferArrayDynamicIndexing = false;
460 features->shaderStorageTexelBufferArrayDynamicIndexing = false;
461 features->shaderUniformBufferArrayNonUniformIndexing = false;
462 features->shaderSampledImageArrayNonUniformIndexing = false;
463 features->shaderStorageBufferArrayNonUniformIndexing = false;
464 features->shaderStorageImageArrayNonUniformIndexing = false;
465 features->shaderInputAttachmentArrayNonUniformIndexing = false;
466 features->shaderUniformTexelBufferArrayNonUniformIndexing = false;
467 features->shaderStorageTexelBufferArrayNonUniformIndexing = false;
468 features->descriptorBindingUniformBufferUpdateAfterBind = false;
469 features->descriptorBindingSampledImageUpdateAfterBind = false;
470 features->descriptorBindingStorageImageUpdateAfterBind = false;
471 features->descriptorBindingStorageBufferUpdateAfterBind = false;
472 features->descriptorBindingUniformTexelBufferUpdateAfterBind = false;
473 features->descriptorBindingStorageTexelBufferUpdateAfterBind = false;
474 features->descriptorBindingUpdateUnusedWhilePending = false;
475 features->descriptorBindingPartiallyBound = false;
476 features->descriptorBindingVariableDescriptorCount = false;
477 features->runtimeDescriptorArray = false;
478
479 features->samplerFilterMinmax = true;
480 features->scalarBlockLayout = false;
481 features->imagelessFramebuffer = false;
482 features->uniformBufferStandardLayout = false;
483 features->shaderSubgroupExtendedTypes = false;
484 features->separateDepthStencilLayouts = false;
485 features->hostQueryReset = false;
486 features->timelineSemaphore = false;
487 features->bufferDeviceAddress = false;
488 features->bufferDeviceAddressCaptureReplay = false;
489 features->bufferDeviceAddressMultiDevice = false;
490 features->vulkanMemoryModel = false;
491 features->vulkanMemoryModelDeviceScope = false;
492 features->vulkanMemoryModelAvailabilityVisibilityChains = false;
493 features->shaderOutputViewportIndex = false;
494 features->shaderOutputLayer = false;
495 features->subgroupBroadcastDynamicId = false;
496 break;
497 }
498 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VARIABLE_POINTERS_FEATURES: {
499 VkPhysicalDeviceVariablePointersFeatures *features = (void *) ext;
500 features->variablePointersStorageBuffer = true;
501 features->variablePointers = true;
502 break;
503 }
504 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_MULTIVIEW_FEATURES: {
505 VkPhysicalDeviceMultiviewFeatures *features =
506 (VkPhysicalDeviceMultiviewFeatures *) ext;
507 features->multiview = false;
508 features->multiviewGeometryShader = false;
509 features->multiviewTessellationShader = false;
510 break;
511 }
512 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SHADER_DRAW_PARAMETERS_FEATURES: {
513 VkPhysicalDeviceShaderDrawParametersFeatures *features =
514 (VkPhysicalDeviceShaderDrawParametersFeatures *) ext;
515 features->shaderDrawParameters = true;
516 break;
517 }
518 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PROTECTED_MEMORY_FEATURES: {
519 VkPhysicalDeviceProtectedMemoryFeatures *features =
520 (VkPhysicalDeviceProtectedMemoryFeatures *) ext;
521 features->protectedMemory = false;
522 break;
523 }
524 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_16BIT_STORAGE_FEATURES: {
525 VkPhysicalDevice16BitStorageFeatures *features =
526 (VkPhysicalDevice16BitStorageFeatures *) ext;
527 features->storageBuffer16BitAccess = false;
528 features->uniformAndStorageBuffer16BitAccess = false;
529 features->storagePushConstant16 = false;
530 features->storageInputOutput16 = false;
531 break;
532 }
533 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SAMPLER_YCBCR_CONVERSION_FEATURES: {
534 VkPhysicalDeviceSamplerYcbcrConversionFeatures *features =
535 (VkPhysicalDeviceSamplerYcbcrConversionFeatures *) ext;
536 features->samplerYcbcrConversion = true;
537 break;
538 }
539 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_DESCRIPTOR_INDEXING_FEATURES_EXT: {
540 VkPhysicalDeviceDescriptorIndexingFeaturesEXT *features =
541 (VkPhysicalDeviceDescriptorIndexingFeaturesEXT *) ext;
542 features->shaderInputAttachmentArrayDynamicIndexing = false;
543 features->shaderUniformTexelBufferArrayDynamicIndexing = false;
544 features->shaderStorageTexelBufferArrayDynamicIndexing = false;
545 features->shaderUniformBufferArrayNonUniformIndexing = false;
546 features->shaderSampledImageArrayNonUniformIndexing = false;
547 features->shaderStorageBufferArrayNonUniformIndexing = false;
548 features->shaderStorageImageArrayNonUniformIndexing = false;
549 features->shaderInputAttachmentArrayNonUniformIndexing = false;
550 features->shaderUniformTexelBufferArrayNonUniformIndexing = false;
551 features->shaderStorageTexelBufferArrayNonUniformIndexing = false;
552 features->descriptorBindingUniformBufferUpdateAfterBind = false;
553 features->descriptorBindingSampledImageUpdateAfterBind = false;
554 features->descriptorBindingStorageImageUpdateAfterBind = false;
555 features->descriptorBindingStorageBufferUpdateAfterBind = false;
556 features->descriptorBindingUniformTexelBufferUpdateAfterBind = false;
557 features->descriptorBindingStorageTexelBufferUpdateAfterBind = false;
558 features->descriptorBindingUpdateUnusedWhilePending = false;
559 features->descriptorBindingPartiallyBound = false;
560 features->descriptorBindingVariableDescriptorCount = false;
561 features->runtimeDescriptorArray = false;
562 break;
563 }
564 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_CONDITIONAL_RENDERING_FEATURES_EXT: {
565 VkPhysicalDeviceConditionalRenderingFeaturesEXT *features =
566 (VkPhysicalDeviceConditionalRenderingFeaturesEXT *) ext;
567 features->conditionalRendering = true;
568 features->inheritedConditionalRendering = true;
569 break;
570 }
571 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_TRANSFORM_FEEDBACK_FEATURES_EXT: {
572 VkPhysicalDeviceTransformFeedbackFeaturesEXT *features =
573 (VkPhysicalDeviceTransformFeedbackFeaturesEXT *) ext;
574 features->transformFeedback = true;
575 features->geometryStreams = false;
576 break;
577 }
578 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_INDEX_TYPE_UINT8_FEATURES_EXT: {
579 VkPhysicalDeviceIndexTypeUint8FeaturesEXT *features =
580 (VkPhysicalDeviceIndexTypeUint8FeaturesEXT *)ext;
581 features->indexTypeUint8 = true;
582 break;
583 }
584 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VERTEX_ATTRIBUTE_DIVISOR_FEATURES_EXT: {
585 VkPhysicalDeviceVertexAttributeDivisorFeaturesEXT *features =
586 (VkPhysicalDeviceVertexAttributeDivisorFeaturesEXT *)ext;
587 features->vertexAttributeInstanceRateDivisor = true;
588 features->vertexAttributeInstanceRateZeroDivisor = true;
589 break;
590 }
591 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PRIVATE_DATA_FEATURES_EXT: {
592 VkPhysicalDevicePrivateDataFeaturesEXT *features =
593 (VkPhysicalDevicePrivateDataFeaturesEXT *)ext;
594 features->privateData = true;
595 break;
596 }
597 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_DEPTH_CLIP_ENABLE_FEATURES_EXT: {
598 VkPhysicalDeviceDepthClipEnableFeaturesEXT *features =
599 (VkPhysicalDeviceDepthClipEnableFeaturesEXT *)ext;
600 features->depthClipEnable = true;
601 break;
602 }
603 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_4444_FORMATS_FEATURES_EXT: {
604 VkPhysicalDevice4444FormatsFeaturesEXT *features = (void *)ext;
605 features->formatA4R4G4B4 = true;
606 features->formatA4B4G4R4 = true;
607 break;
608 }
609 default:
610 break;
611 }
612 }
613 return tu_GetPhysicalDeviceFeatures(physicalDevice, &pFeatures->features);
614 }
615
616 void
617 tu_GetPhysicalDeviceProperties(VkPhysicalDevice physicalDevice,
618 VkPhysicalDeviceProperties *pProperties)
619 {
620 TU_FROM_HANDLE(tu_physical_device, pdevice, physicalDevice);
621 VkSampleCountFlags sample_counts =
622 VK_SAMPLE_COUNT_1_BIT | VK_SAMPLE_COUNT_2_BIT | VK_SAMPLE_COUNT_4_BIT;
623
624 /* I have no idea what the maximum size is, but the hardware supports very
625 * large numbers of descriptors (at least 2^16). This limit is based on
626 * CP_LOAD_STATE6, which has a 28-bit field for the DWORD offset, so that
627 * we don't have to think about what to do if that overflows, but really
628 * nothing is likely to get close to this.
629 */
630 const size_t max_descriptor_set_size = (1 << 28) / A6XX_TEX_CONST_DWORDS;
631
632 VkPhysicalDeviceLimits limits = {
633 .maxImageDimension1D = (1 << 14),
634 .maxImageDimension2D = (1 << 14),
635 .maxImageDimension3D = (1 << 11),
636 .maxImageDimensionCube = (1 << 14),
637 .maxImageArrayLayers = (1 << 11),
638 .maxTexelBufferElements = 128 * 1024 * 1024,
639 .maxUniformBufferRange = MAX_UNIFORM_BUFFER_RANGE,
640 .maxStorageBufferRange = MAX_STORAGE_BUFFER_RANGE,
641 .maxPushConstantsSize = MAX_PUSH_CONSTANTS_SIZE,
642 .maxMemoryAllocationCount = UINT32_MAX,
643 .maxSamplerAllocationCount = 64 * 1024,
644 .bufferImageGranularity = 64, /* A cache line */
645 .sparseAddressSpaceSize = 0xffffffffu, /* buffer max size */
646 .maxBoundDescriptorSets = MAX_SETS,
647 .maxPerStageDescriptorSamplers = max_descriptor_set_size,
648 .maxPerStageDescriptorUniformBuffers = max_descriptor_set_size,
649 .maxPerStageDescriptorStorageBuffers = max_descriptor_set_size,
650 .maxPerStageDescriptorSampledImages = max_descriptor_set_size,
651 .maxPerStageDescriptorStorageImages = max_descriptor_set_size,
652 .maxPerStageDescriptorInputAttachments = MAX_RTS,
653 .maxPerStageResources = max_descriptor_set_size,
654 .maxDescriptorSetSamplers = max_descriptor_set_size,
655 .maxDescriptorSetUniformBuffers = max_descriptor_set_size,
656 .maxDescriptorSetUniformBuffersDynamic = MAX_DYNAMIC_UNIFORM_BUFFERS,
657 .maxDescriptorSetStorageBuffers = max_descriptor_set_size,
658 .maxDescriptorSetStorageBuffersDynamic = MAX_DYNAMIC_STORAGE_BUFFERS,
659 .maxDescriptorSetSampledImages = max_descriptor_set_size,
660 .maxDescriptorSetStorageImages = max_descriptor_set_size,
661 .maxDescriptorSetInputAttachments = MAX_RTS,
662 .maxVertexInputAttributes = 32,
663 .maxVertexInputBindings = 32,
664 .maxVertexInputAttributeOffset = 4095,
665 .maxVertexInputBindingStride = 2048,
666 .maxVertexOutputComponents = 128,
667 .maxTessellationGenerationLevel = 64,
668 .maxTessellationPatchSize = 32,
669 .maxTessellationControlPerVertexInputComponents = 128,
670 .maxTessellationControlPerVertexOutputComponents = 128,
671 .maxTessellationControlPerPatchOutputComponents = 120,
672 .maxTessellationControlTotalOutputComponents = 4096,
673 .maxTessellationEvaluationInputComponents = 128,
674 .maxTessellationEvaluationOutputComponents = 128,
675 .maxGeometryShaderInvocations = 32,
676 .maxGeometryInputComponents = 64,
677 .maxGeometryOutputComponents = 128,
678 .maxGeometryOutputVertices = 256,
679 .maxGeometryTotalOutputComponents = 1024,
680 .maxFragmentInputComponents = 124,
681 .maxFragmentOutputAttachments = 8,
682 .maxFragmentDualSrcAttachments = 1,
683 .maxFragmentCombinedOutputResources = 8,
684 .maxComputeSharedMemorySize = 32768,
685 .maxComputeWorkGroupCount = { 65535, 65535, 65535 },
686 .maxComputeWorkGroupInvocations = 2048,
687 .maxComputeWorkGroupSize = { 2048, 2048, 2048 },
688 .subPixelPrecisionBits = 8,
689 .subTexelPrecisionBits = 8,
690 .mipmapPrecisionBits = 8,
691 .maxDrawIndexedIndexValue = UINT32_MAX,
692 .maxDrawIndirectCount = UINT32_MAX,
693 .maxSamplerLodBias = 4095.0 / 256.0, /* [-16, 15.99609375] */
694 .maxSamplerAnisotropy = 16,
695 .maxViewports = MAX_VIEWPORTS,
696 .maxViewportDimensions = { (1 << 14), (1 << 14) },
697 .viewportBoundsRange = { INT16_MIN, INT16_MAX },
698 .viewportSubPixelBits = 8,
699 .minMemoryMapAlignment = 4096, /* A page */
700 .minTexelBufferOffsetAlignment = 64,
701 .minUniformBufferOffsetAlignment = 64,
702 .minStorageBufferOffsetAlignment = 64,
703 .minTexelOffset = -16,
704 .maxTexelOffset = 15,
705 .minTexelGatherOffset = -32,
706 .maxTexelGatherOffset = 31,
707 .minInterpolationOffset = -0.5,
708 .maxInterpolationOffset = 0.4375,
709 .subPixelInterpolationOffsetBits = 4,
710 .maxFramebufferWidth = (1 << 14),
711 .maxFramebufferHeight = (1 << 14),
712 .maxFramebufferLayers = (1 << 10),
713 .framebufferColorSampleCounts = sample_counts,
714 .framebufferDepthSampleCounts = sample_counts,
715 .framebufferStencilSampleCounts = sample_counts,
716 .framebufferNoAttachmentsSampleCounts = sample_counts,
717 .maxColorAttachments = MAX_RTS,
718 .sampledImageColorSampleCounts = sample_counts,
719 .sampledImageIntegerSampleCounts = VK_SAMPLE_COUNT_1_BIT,
720 .sampledImageDepthSampleCounts = sample_counts,
721 .sampledImageStencilSampleCounts = sample_counts,
722 .storageImageSampleCounts = VK_SAMPLE_COUNT_1_BIT,
723 .maxSampleMaskWords = 1,
724 .timestampComputeAndGraphics = true,
725 .timestampPeriod = 1000000000.0 / 19200000.0, /* CP_ALWAYS_ON_COUNTER is fixed 19.2MHz */
726 .maxClipDistances = 8,
727 .maxCullDistances = 8,
728 .maxCombinedClipAndCullDistances = 8,
729 .discreteQueuePriorities = 1,
730 .pointSizeRange = { 1, 4092 },
731 .lineWidthRange = { 0.0, 7.9921875 },
732 .pointSizeGranularity = 0.0625,
733 .lineWidthGranularity = (1.0 / 128.0),
734 .strictLines = false, /* FINISHME */
735 .standardSampleLocations = true,
736 .optimalBufferCopyOffsetAlignment = 128,
737 .optimalBufferCopyRowPitchAlignment = 128,
738 .nonCoherentAtomSize = 64,
739 };
740
741 *pProperties = (VkPhysicalDeviceProperties) {
742 .apiVersion = tu_physical_device_api_version(pdevice),
743 .driverVersion = vk_get_driver_version(),
744 .vendorID = 0, /* TODO */
745 .deviceID = 0,
746 .deviceType = VK_PHYSICAL_DEVICE_TYPE_INTEGRATED_GPU,
747 .limits = limits,
748 .sparseProperties = { 0 },
749 };
750
751 strcpy(pProperties->deviceName, pdevice->name);
752 memcpy(pProperties->pipelineCacheUUID, pdevice->cache_uuid, VK_UUID_SIZE);
753 }
754
755 void
756 tu_GetPhysicalDeviceProperties2(VkPhysicalDevice physicalDevice,
757 VkPhysicalDeviceProperties2 *pProperties)
758 {
759 TU_FROM_HANDLE(tu_physical_device, pdevice, physicalDevice);
760 tu_GetPhysicalDeviceProperties(physicalDevice, &pProperties->properties);
761
762 vk_foreach_struct(ext, pProperties->pNext)
763 {
764 switch (ext->sType) {
765 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PUSH_DESCRIPTOR_PROPERTIES_KHR: {
766 VkPhysicalDevicePushDescriptorPropertiesKHR *properties =
767 (VkPhysicalDevicePushDescriptorPropertiesKHR *) ext;
768 properties->maxPushDescriptors = MAX_PUSH_DESCRIPTORS;
769 break;
770 }
771 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_ID_PROPERTIES: {
772 VkPhysicalDeviceIDProperties *properties =
773 (VkPhysicalDeviceIDProperties *) ext;
774 memcpy(properties->driverUUID, pdevice->driver_uuid, VK_UUID_SIZE);
775 memcpy(properties->deviceUUID, pdevice->device_uuid, VK_UUID_SIZE);
776 properties->deviceLUIDValid = false;
777 break;
778 }
779 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_MULTIVIEW_PROPERTIES: {
780 VkPhysicalDeviceMultiviewProperties *properties =
781 (VkPhysicalDeviceMultiviewProperties *) ext;
782 properties->maxMultiviewViewCount = MAX_VIEWS;
783 properties->maxMultiviewInstanceIndex = INT_MAX;
784 break;
785 }
786 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_POINT_CLIPPING_PROPERTIES: {
787 VkPhysicalDevicePointClippingProperties *properties =
788 (VkPhysicalDevicePointClippingProperties *) ext;
789 properties->pointClippingBehavior =
790 VK_POINT_CLIPPING_BEHAVIOR_ALL_CLIP_PLANES;
791 break;
792 }
793 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_MAINTENANCE_3_PROPERTIES: {
794 VkPhysicalDeviceMaintenance3Properties *properties =
795 (VkPhysicalDeviceMaintenance3Properties *) ext;
796 /* Make sure everything is addressable by a signed 32-bit int, and
797 * our largest descriptors are 96 bytes. */
798 properties->maxPerSetDescriptors = (1ull << 31) / 96;
799 /* Our buffer size fields allow only this much */
800 properties->maxMemoryAllocationSize = 0xFFFFFFFFull;
801 break;
802 }
803 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_TRANSFORM_FEEDBACK_PROPERTIES_EXT: {
804 VkPhysicalDeviceTransformFeedbackPropertiesEXT *properties =
805 (VkPhysicalDeviceTransformFeedbackPropertiesEXT *)ext;
806
807 properties->maxTransformFeedbackStreams = IR3_MAX_SO_STREAMS;
808 properties->maxTransformFeedbackBuffers = IR3_MAX_SO_BUFFERS;
809 properties->maxTransformFeedbackBufferSize = UINT32_MAX;
810 properties->maxTransformFeedbackStreamDataSize = 512;
811 properties->maxTransformFeedbackBufferDataSize = 512;
812 properties->maxTransformFeedbackBufferDataStride = 512;
813 properties->transformFeedbackQueries = true;
814 properties->transformFeedbackStreamsLinesTriangles = false;
815 properties->transformFeedbackRasterizationStreamSelect = false;
816 properties->transformFeedbackDraw = true;
817 break;
818 }
819 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SAMPLE_LOCATIONS_PROPERTIES_EXT: {
820 VkPhysicalDeviceSampleLocationsPropertiesEXT *properties =
821 (VkPhysicalDeviceSampleLocationsPropertiesEXT *)ext;
822 properties->sampleLocationSampleCounts = 0;
823 if (pdevice->supported_extensions.EXT_sample_locations) {
824 properties->sampleLocationSampleCounts =
825 VK_SAMPLE_COUNT_1_BIT | VK_SAMPLE_COUNT_2_BIT | VK_SAMPLE_COUNT_4_BIT;
826 }
827 properties->maxSampleLocationGridSize = (VkExtent2D) { 1 , 1 };
828 properties->sampleLocationCoordinateRange[0] = 0.0f;
829 properties->sampleLocationCoordinateRange[1] = 0.9375f;
830 properties->sampleLocationSubPixelBits = 4;
831 properties->variableSampleLocations = true;
832 break;
833 }
834 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SAMPLER_FILTER_MINMAX_PROPERTIES: {
835 VkPhysicalDeviceSamplerFilterMinmaxProperties *properties =
836 (VkPhysicalDeviceSamplerFilterMinmaxProperties *)ext;
837 properties->filterMinmaxImageComponentMapping = true;
838 properties->filterMinmaxSingleComponentFormats = true;
839 break;
840 }
841 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SUBGROUP_PROPERTIES: {
842 VkPhysicalDeviceSubgroupProperties *properties =
843 (VkPhysicalDeviceSubgroupProperties *)ext;
844 properties->subgroupSize = 64;
845 properties->supportedStages = VK_SHADER_STAGE_COMPUTE_BIT;
846 properties->supportedOperations = VK_SUBGROUP_FEATURE_BASIC_BIT |
847 VK_SUBGROUP_FEATURE_VOTE_BIT;
848 properties->quadOperationsInAllStages = false;
849 break;
850 }
851 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VERTEX_ATTRIBUTE_DIVISOR_PROPERTIES_EXT: {
852 VkPhysicalDeviceVertexAttributeDivisorPropertiesEXT *props =
853 (VkPhysicalDeviceVertexAttributeDivisorPropertiesEXT *)ext;
854 props->maxVertexAttribDivisor = UINT32_MAX;
855 break;
856 }
857 default:
858 break;
859 }
860 }
861 }
862
863 static const VkQueueFamilyProperties tu_queue_family_properties = {
864 .queueFlags =
865 VK_QUEUE_GRAPHICS_BIT | VK_QUEUE_COMPUTE_BIT | VK_QUEUE_TRANSFER_BIT,
866 .queueCount = 1,
867 .timestampValidBits = 48,
868 .minImageTransferGranularity = { 1, 1, 1 },
869 };
870
871 void
872 tu_GetPhysicalDeviceQueueFamilyProperties(
873 VkPhysicalDevice physicalDevice,
874 uint32_t *pQueueFamilyPropertyCount,
875 VkQueueFamilyProperties *pQueueFamilyProperties)
876 {
877 VK_OUTARRAY_MAKE(out, pQueueFamilyProperties, pQueueFamilyPropertyCount);
878
879 vk_outarray_append(&out, p) { *p = tu_queue_family_properties; }
880 }
881
882 void
883 tu_GetPhysicalDeviceQueueFamilyProperties2(
884 VkPhysicalDevice physicalDevice,
885 uint32_t *pQueueFamilyPropertyCount,
886 VkQueueFamilyProperties2 *pQueueFamilyProperties)
887 {
888 VK_OUTARRAY_MAKE(out, pQueueFamilyProperties, pQueueFamilyPropertyCount);
889
890 vk_outarray_append(&out, p)
891 {
892 p->queueFamilyProperties = tu_queue_family_properties;
893 }
894 }
895
896 static uint64_t
897 tu_get_system_heap_size()
898 {
899 struct sysinfo info;
900 sysinfo(&info);
901
902 uint64_t total_ram = (uint64_t) info.totalram * (uint64_t) info.mem_unit;
903
904 /* We don't want to burn too much ram with the GPU. If the user has 4GiB
905 * or less, we use at most half. If they have more than 4GiB, we use 3/4.
906 */
907 uint64_t available_ram;
908 if (total_ram <= 4ull * 1024ull * 1024ull * 1024ull)
909 available_ram = total_ram / 2;
910 else
911 available_ram = total_ram * 3 / 4;
912
913 return available_ram;
914 }
915
916 void
917 tu_GetPhysicalDeviceMemoryProperties(
918 VkPhysicalDevice physicalDevice,
919 VkPhysicalDeviceMemoryProperties *pMemoryProperties)
920 {
921 pMemoryProperties->memoryHeapCount = 1;
922 pMemoryProperties->memoryHeaps[0].size = tu_get_system_heap_size();
923 pMemoryProperties->memoryHeaps[0].flags = VK_MEMORY_HEAP_DEVICE_LOCAL_BIT;
924
925 pMemoryProperties->memoryTypeCount = 1;
926 pMemoryProperties->memoryTypes[0].propertyFlags =
927 VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT |
928 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT |
929 VK_MEMORY_PROPERTY_HOST_COHERENT_BIT;
930 pMemoryProperties->memoryTypes[0].heapIndex = 0;
931 }
932
933 void
934 tu_GetPhysicalDeviceMemoryProperties2(
935 VkPhysicalDevice physicalDevice,
936 VkPhysicalDeviceMemoryProperties2 *pMemoryProperties)
937 {
938 return tu_GetPhysicalDeviceMemoryProperties(
939 physicalDevice, &pMemoryProperties->memoryProperties);
940 }
941
942 static VkResult
943 tu_queue_init(struct tu_device *device,
944 struct tu_queue *queue,
945 uint32_t queue_family_index,
946 int idx,
947 VkDeviceQueueCreateFlags flags)
948 {
949 vk_object_base_init(&device->vk, &queue->base, VK_OBJECT_TYPE_QUEUE);
950
951 queue->device = device;
952 queue->queue_family_index = queue_family_index;
953 queue->queue_idx = idx;
954 queue->flags = flags;
955
956 int ret = tu_drm_submitqueue_new(device, 0, &queue->msm_queue_id);
957 if (ret)
958 return VK_ERROR_INITIALIZATION_FAILED;
959
960 tu_fence_init(&queue->submit_fence, false);
961
962 return VK_SUCCESS;
963 }
964
965 static void
966 tu_queue_finish(struct tu_queue *queue)
967 {
968 tu_fence_finish(&queue->submit_fence);
969 tu_drm_submitqueue_close(queue->device, queue->msm_queue_id);
970 }
971
972 static int
973 tu_get_device_extension_index(const char *name)
974 {
975 for (unsigned i = 0; i < TU_DEVICE_EXTENSION_COUNT; ++i) {
976 if (strcmp(name, tu_device_extensions[i].extensionName) == 0)
977 return i;
978 }
979 return -1;
980 }
981
982 struct PACKED bcolor_entry {
983 uint32_t fp32[4];
984 uint16_t ui16[4];
985 int16_t si16[4];
986 uint16_t fp16[4];
987 uint16_t rgb565;
988 uint16_t rgb5a1;
989 uint16_t rgba4;
990 uint8_t __pad0[2];
991 uint8_t ui8[4];
992 int8_t si8[4];
993 uint32_t rgb10a2;
994 uint32_t z24; /* also s8? */
995 uint16_t srgb[4]; /* appears to duplicate fp16[], but clamped, used for srgb */
996 uint8_t __pad1[56];
997 } border_color[] = {
998 [VK_BORDER_COLOR_FLOAT_TRANSPARENT_BLACK] = {},
999 [VK_BORDER_COLOR_INT_TRANSPARENT_BLACK] = {},
1000 [VK_BORDER_COLOR_FLOAT_OPAQUE_BLACK] = {
1001 .fp32[3] = 0x3f800000,
1002 .ui16[3] = 0xffff,
1003 .si16[3] = 0x7fff,
1004 .fp16[3] = 0x3c00,
1005 .rgb5a1 = 0x8000,
1006 .rgba4 = 0xf000,
1007 .ui8[3] = 0xff,
1008 .si8[3] = 0x7f,
1009 .rgb10a2 = 0xc0000000,
1010 .srgb[3] = 0x3c00,
1011 },
1012 [VK_BORDER_COLOR_INT_OPAQUE_BLACK] = {
1013 .fp32[3] = 1,
1014 .fp16[3] = 1,
1015 },
1016 [VK_BORDER_COLOR_FLOAT_OPAQUE_WHITE] = {
1017 .fp32[0 ... 3] = 0x3f800000,
1018 .ui16[0 ... 3] = 0xffff,
1019 .si16[0 ... 3] = 0x7fff,
1020 .fp16[0 ... 3] = 0x3c00,
1021 .rgb565 = 0xffff,
1022 .rgb5a1 = 0xffff,
1023 .rgba4 = 0xffff,
1024 .ui8[0 ... 3] = 0xff,
1025 .si8[0 ... 3] = 0x7f,
1026 .rgb10a2 = 0xffffffff,
1027 .z24 = 0xffffff,
1028 .srgb[0 ... 3] = 0x3c00,
1029 },
1030 [VK_BORDER_COLOR_INT_OPAQUE_WHITE] = {
1031 .fp32[0 ... 3] = 1,
1032 .fp16[0 ... 3] = 1,
1033 },
1034 };
1035
1036 VkResult
1037 tu_CreateDevice(VkPhysicalDevice physicalDevice,
1038 const VkDeviceCreateInfo *pCreateInfo,
1039 const VkAllocationCallbacks *pAllocator,
1040 VkDevice *pDevice)
1041 {
1042 TU_FROM_HANDLE(tu_physical_device, physical_device, physicalDevice);
1043 VkResult result;
1044 struct tu_device *device;
1045
1046 /* Check enabled features */
1047 if (pCreateInfo->pEnabledFeatures) {
1048 VkPhysicalDeviceFeatures supported_features;
1049 tu_GetPhysicalDeviceFeatures(physicalDevice, &supported_features);
1050 VkBool32 *supported_feature = (VkBool32 *) &supported_features;
1051 VkBool32 *enabled_feature = (VkBool32 *) pCreateInfo->pEnabledFeatures;
1052 unsigned num_features =
1053 sizeof(VkPhysicalDeviceFeatures) / sizeof(VkBool32);
1054 for (uint32_t i = 0; i < num_features; i++) {
1055 if (enabled_feature[i] && !supported_feature[i])
1056 return vk_error(physical_device->instance,
1057 VK_ERROR_FEATURE_NOT_PRESENT);
1058 }
1059 }
1060
1061 device = vk_zalloc2(&physical_device->instance->alloc, pAllocator,
1062 sizeof(*device), 8, VK_SYSTEM_ALLOCATION_SCOPE_DEVICE);
1063 if (!device)
1064 return vk_error(physical_device->instance, VK_ERROR_OUT_OF_HOST_MEMORY);
1065
1066 vk_device_init(&device->vk, pCreateInfo,
1067 &physical_device->instance->alloc, pAllocator);
1068
1069 device->instance = physical_device->instance;
1070 device->physical_device = physical_device;
1071 device->_lost = false;
1072
1073 for (uint32_t i = 0; i < pCreateInfo->enabledExtensionCount; i++) {
1074 const char *ext_name = pCreateInfo->ppEnabledExtensionNames[i];
1075 int index = tu_get_device_extension_index(ext_name);
1076 if (index < 0 ||
1077 !physical_device->supported_extensions.extensions[index]) {
1078 vk_free(&device->vk.alloc, device);
1079 return vk_error(physical_device->instance,
1080 VK_ERROR_EXTENSION_NOT_PRESENT);
1081 }
1082
1083 device->enabled_extensions.extensions[index] = true;
1084 }
1085
1086 for (unsigned i = 0; i < pCreateInfo->queueCreateInfoCount; i++) {
1087 const VkDeviceQueueCreateInfo *queue_create =
1088 &pCreateInfo->pQueueCreateInfos[i];
1089 uint32_t qfi = queue_create->queueFamilyIndex;
1090 device->queues[qfi] = vk_alloc(
1091 &device->vk.alloc, queue_create->queueCount * sizeof(struct tu_queue),
1092 8, VK_SYSTEM_ALLOCATION_SCOPE_DEVICE);
1093 if (!device->queues[qfi]) {
1094 result = VK_ERROR_OUT_OF_HOST_MEMORY;
1095 goto fail_queues;
1096 }
1097
1098 memset(device->queues[qfi], 0,
1099 queue_create->queueCount * sizeof(struct tu_queue));
1100
1101 device->queue_count[qfi] = queue_create->queueCount;
1102
1103 for (unsigned q = 0; q < queue_create->queueCount; q++) {
1104 result = tu_queue_init(device, &device->queues[qfi][q], qfi, q,
1105 queue_create->flags);
1106 if (result != VK_SUCCESS)
1107 goto fail_queues;
1108 }
1109 }
1110
1111 device->compiler = ir3_compiler_create(NULL, physical_device->gpu_id);
1112 if (!device->compiler)
1113 goto fail_queues;
1114
1115 /* initial sizes, these will increase if there is overflow */
1116 device->vsc_draw_strm_pitch = 0x1000 + VSC_PAD;
1117 device->vsc_prim_strm_pitch = 0x4000 + VSC_PAD;
1118
1119 STATIC_ASSERT(sizeof(border_color) == sizeof(((struct tu6_global*) 0)->border_color));
1120 result = tu_bo_init_new(device, &device->global_bo, sizeof(struct tu6_global));
1121 if (result != VK_SUCCESS)
1122 goto fail_global_bo;
1123
1124 result = tu_bo_map(device, &device->global_bo);
1125 if (result != VK_SUCCESS)
1126 goto fail_global_bo_map;
1127
1128 struct tu6_global *global = device->global_bo.map;
1129 memcpy(global->border_color, border_color, sizeof(border_color));
1130 global->predicate = 0;
1131 tu_init_clear_blit_shaders(global);
1132
1133 VkPipelineCacheCreateInfo ci;
1134 ci.sType = VK_STRUCTURE_TYPE_PIPELINE_CACHE_CREATE_INFO;
1135 ci.pNext = NULL;
1136 ci.flags = 0;
1137 ci.pInitialData = NULL;
1138 ci.initialDataSize = 0;
1139 VkPipelineCache pc;
1140 result =
1141 tu_CreatePipelineCache(tu_device_to_handle(device), &ci, NULL, &pc);
1142 if (result != VK_SUCCESS)
1143 goto fail_pipeline_cache;
1144
1145 device->mem_cache = tu_pipeline_cache_from_handle(pc);
1146
1147 for (unsigned i = 0; i < ARRAY_SIZE(device->scratch_bos); i++)
1148 mtx_init(&device->scratch_bos[i].construct_mtx, mtx_plain);
1149
1150 mtx_init(&device->vsc_pitch_mtx, mtx_plain);
1151
1152 *pDevice = tu_device_to_handle(device);
1153 return VK_SUCCESS;
1154
1155 fail_pipeline_cache:
1156 fail_global_bo_map:
1157 tu_bo_finish(device, &device->global_bo);
1158
1159 fail_global_bo:
1160 ralloc_free(device->compiler);
1161
1162 fail_queues:
1163 for (unsigned i = 0; i < TU_MAX_QUEUE_FAMILIES; i++) {
1164 for (unsigned q = 0; q < device->queue_count[i]; q++)
1165 tu_queue_finish(&device->queues[i][q]);
1166 if (device->queue_count[i])
1167 vk_object_free(&device->vk, NULL, device->queues[i]);
1168 }
1169
1170 vk_free(&device->vk.alloc, device);
1171 return result;
1172 }
1173
1174 void
1175 tu_DestroyDevice(VkDevice _device, const VkAllocationCallbacks *pAllocator)
1176 {
1177 TU_FROM_HANDLE(tu_device, device, _device);
1178
1179 if (!device)
1180 return;
1181
1182 for (unsigned i = 0; i < TU_MAX_QUEUE_FAMILIES; i++) {
1183 for (unsigned q = 0; q < device->queue_count[i]; q++)
1184 tu_queue_finish(&device->queues[i][q]);
1185 if (device->queue_count[i])
1186 vk_object_free(&device->vk, NULL, device->queues[i]);
1187 }
1188
1189 for (unsigned i = 0; i < ARRAY_SIZE(device->scratch_bos); i++) {
1190 if (device->scratch_bos[i].initialized)
1191 tu_bo_finish(device, &device->scratch_bos[i].bo);
1192 }
1193
1194 ir3_compiler_destroy(device->compiler);
1195
1196 VkPipelineCache pc = tu_pipeline_cache_to_handle(device->mem_cache);
1197 tu_DestroyPipelineCache(tu_device_to_handle(device), pc, NULL);
1198
1199 vk_free(&device->vk.alloc, device);
1200 }
1201
1202 VkResult
1203 _tu_device_set_lost(struct tu_device *device,
1204 const char *file, int line,
1205 const char *msg, ...)
1206 {
1207 /* Set the flag indicating that waits should return in finite time even
1208 * after device loss.
1209 */
1210 p_atomic_inc(&device->_lost);
1211
1212 /* TODO: Report the log message through VkDebugReportCallbackEXT instead */
1213 fprintf(stderr, "%s:%d: ", file, line);
1214 va_list ap;
1215 va_start(ap, msg);
1216 vfprintf(stderr, msg, ap);
1217 va_end(ap);
1218
1219 if (env_var_as_boolean("TU_ABORT_ON_DEVICE_LOSS", false))
1220 abort();
1221
1222 return VK_ERROR_DEVICE_LOST;
1223 }
1224
1225 VkResult
1226 tu_get_scratch_bo(struct tu_device *dev, uint64_t size, struct tu_bo **bo)
1227 {
1228 unsigned size_log2 = MAX2(util_logbase2_ceil64(size), MIN_SCRATCH_BO_SIZE_LOG2);
1229 unsigned index = size_log2 - MIN_SCRATCH_BO_SIZE_LOG2;
1230 assert(index < ARRAY_SIZE(dev->scratch_bos));
1231
1232 for (unsigned i = index; i < ARRAY_SIZE(dev->scratch_bos); i++) {
1233 if (p_atomic_read(&dev->scratch_bos[i].initialized)) {
1234 /* Fast path: just return the already-allocated BO. */
1235 *bo = &dev->scratch_bos[i].bo;
1236 return VK_SUCCESS;
1237 }
1238 }
1239
1240 /* Slow path: actually allocate the BO. We take a lock because the process
1241 * of allocating it is slow, and we don't want to block the CPU while it
1242 * finishes.
1243 */
1244 mtx_lock(&dev->scratch_bos[index].construct_mtx);
1245
1246 /* Another thread may have allocated it already while we were waiting on
1247 * the lock. We need to check this in order to avoid double-allocating.
1248 */
1249 if (dev->scratch_bos[index].initialized) {
1250 mtx_unlock(&dev->scratch_bos[index].construct_mtx);
1251 *bo = &dev->scratch_bos[index].bo;
1252 return VK_SUCCESS;
1253 }
1254
1255 unsigned bo_size = 1ull << size_log2;
1256 VkResult result = tu_bo_init_new(dev, &dev->scratch_bos[index].bo, bo_size);
1257 if (result != VK_SUCCESS) {
1258 mtx_unlock(&dev->scratch_bos[index].construct_mtx);
1259 return result;
1260 }
1261
1262 p_atomic_set(&dev->scratch_bos[index].initialized, true);
1263
1264 mtx_unlock(&dev->scratch_bos[index].construct_mtx);
1265
1266 *bo = &dev->scratch_bos[index].bo;
1267 return VK_SUCCESS;
1268 }
1269
1270 VkResult
1271 tu_EnumerateInstanceLayerProperties(uint32_t *pPropertyCount,
1272 VkLayerProperties *pProperties)
1273 {
1274 *pPropertyCount = 0;
1275 return VK_SUCCESS;
1276 }
1277
1278 VkResult
1279 tu_EnumerateDeviceLayerProperties(VkPhysicalDevice physicalDevice,
1280 uint32_t *pPropertyCount,
1281 VkLayerProperties *pProperties)
1282 {
1283 *pPropertyCount = 0;
1284 return VK_SUCCESS;
1285 }
1286
1287 void
1288 tu_GetDeviceQueue2(VkDevice _device,
1289 const VkDeviceQueueInfo2 *pQueueInfo,
1290 VkQueue *pQueue)
1291 {
1292 TU_FROM_HANDLE(tu_device, device, _device);
1293 struct tu_queue *queue;
1294
1295 queue =
1296 &device->queues[pQueueInfo->queueFamilyIndex][pQueueInfo->queueIndex];
1297 if (pQueueInfo->flags != queue->flags) {
1298 /* From the Vulkan 1.1.70 spec:
1299 *
1300 * "The queue returned by vkGetDeviceQueue2 must have the same
1301 * flags value from this structure as that used at device
1302 * creation time in a VkDeviceQueueCreateInfo instance. If no
1303 * matching flags were specified at device creation time then
1304 * pQueue will return VK_NULL_HANDLE."
1305 */
1306 *pQueue = VK_NULL_HANDLE;
1307 return;
1308 }
1309
1310 *pQueue = tu_queue_to_handle(queue);
1311 }
1312
1313 void
1314 tu_GetDeviceQueue(VkDevice _device,
1315 uint32_t queueFamilyIndex,
1316 uint32_t queueIndex,
1317 VkQueue *pQueue)
1318 {
1319 const VkDeviceQueueInfo2 info =
1320 (VkDeviceQueueInfo2) { .sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_INFO_2,
1321 .queueFamilyIndex = queueFamilyIndex,
1322 .queueIndex = queueIndex };
1323
1324 tu_GetDeviceQueue2(_device, &info, pQueue);
1325 }
1326
1327 VkResult
1328 tu_QueueWaitIdle(VkQueue _queue)
1329 {
1330 TU_FROM_HANDLE(tu_queue, queue, _queue);
1331
1332 if (tu_device_is_lost(queue->device))
1333 return VK_ERROR_DEVICE_LOST;
1334
1335 tu_fence_wait_idle(&queue->submit_fence);
1336
1337 return VK_SUCCESS;
1338 }
1339
1340 VkResult
1341 tu_DeviceWaitIdle(VkDevice _device)
1342 {
1343 TU_FROM_HANDLE(tu_device, device, _device);
1344
1345 if (tu_device_is_lost(device))
1346 return VK_ERROR_DEVICE_LOST;
1347
1348 for (unsigned i = 0; i < TU_MAX_QUEUE_FAMILIES; i++) {
1349 for (unsigned q = 0; q < device->queue_count[i]; q++) {
1350 tu_QueueWaitIdle(tu_queue_to_handle(&device->queues[i][q]));
1351 }
1352 }
1353 return VK_SUCCESS;
1354 }
1355
1356 VkResult
1357 tu_EnumerateInstanceExtensionProperties(const char *pLayerName,
1358 uint32_t *pPropertyCount,
1359 VkExtensionProperties *pProperties)
1360 {
1361 VK_OUTARRAY_MAKE(out, pProperties, pPropertyCount);
1362
1363 /* We spport no lyaers */
1364 if (pLayerName)
1365 return vk_error(NULL, VK_ERROR_LAYER_NOT_PRESENT);
1366
1367 for (int i = 0; i < TU_INSTANCE_EXTENSION_COUNT; i++) {
1368 if (tu_instance_extensions_supported.extensions[i]) {
1369 vk_outarray_append(&out, prop) { *prop = tu_instance_extensions[i]; }
1370 }
1371 }
1372
1373 return vk_outarray_status(&out);
1374 }
1375
1376 VkResult
1377 tu_EnumerateDeviceExtensionProperties(VkPhysicalDevice physicalDevice,
1378 const char *pLayerName,
1379 uint32_t *pPropertyCount,
1380 VkExtensionProperties *pProperties)
1381 {
1382 /* We spport no lyaers */
1383 TU_FROM_HANDLE(tu_physical_device, device, physicalDevice);
1384 VK_OUTARRAY_MAKE(out, pProperties, pPropertyCount);
1385
1386 /* We spport no lyaers */
1387 if (pLayerName)
1388 return vk_error(NULL, VK_ERROR_LAYER_NOT_PRESENT);
1389
1390 for (int i = 0; i < TU_DEVICE_EXTENSION_COUNT; i++) {
1391 if (device->supported_extensions.extensions[i]) {
1392 vk_outarray_append(&out, prop) { *prop = tu_device_extensions[i]; }
1393 }
1394 }
1395
1396 return vk_outarray_status(&out);
1397 }
1398
1399 PFN_vkVoidFunction
1400 tu_GetInstanceProcAddr(VkInstance _instance, const char *pName)
1401 {
1402 TU_FROM_HANDLE(tu_instance, instance, _instance);
1403
1404 return tu_lookup_entrypoint_checked(
1405 pName, instance ? instance->api_version : 0,
1406 instance ? &instance->enabled_extensions : NULL, NULL);
1407 }
1408
1409 /* The loader wants us to expose a second GetInstanceProcAddr function
1410 * to work around certain LD_PRELOAD issues seen in apps.
1411 */
1412 PUBLIC
1413 VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL
1414 vk_icdGetInstanceProcAddr(VkInstance instance, const char *pName);
1415
1416 PUBLIC
1417 VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL
1418 vk_icdGetInstanceProcAddr(VkInstance instance, const char *pName)
1419 {
1420 return tu_GetInstanceProcAddr(instance, pName);
1421 }
1422
1423 PFN_vkVoidFunction
1424 tu_GetDeviceProcAddr(VkDevice _device, const char *pName)
1425 {
1426 TU_FROM_HANDLE(tu_device, device, _device);
1427
1428 return tu_lookup_entrypoint_checked(pName, device->instance->api_version,
1429 &device->instance->enabled_extensions,
1430 &device->enabled_extensions);
1431 }
1432
1433 static VkResult
1434 tu_alloc_memory(struct tu_device *device,
1435 const VkMemoryAllocateInfo *pAllocateInfo,
1436 const VkAllocationCallbacks *pAllocator,
1437 VkDeviceMemory *pMem)
1438 {
1439 struct tu_device_memory *mem;
1440 VkResult result;
1441
1442 assert(pAllocateInfo->sType == VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO);
1443
1444 if (pAllocateInfo->allocationSize == 0) {
1445 /* Apparently, this is allowed */
1446 *pMem = VK_NULL_HANDLE;
1447 return VK_SUCCESS;
1448 }
1449
1450 mem = vk_object_alloc(&device->vk, pAllocator, sizeof(*mem),
1451 VK_OBJECT_TYPE_DEVICE_MEMORY);
1452 if (mem == NULL)
1453 return vk_error(device->instance, VK_ERROR_OUT_OF_HOST_MEMORY);
1454
1455 const VkImportMemoryFdInfoKHR *fd_info =
1456 vk_find_struct_const(pAllocateInfo->pNext, IMPORT_MEMORY_FD_INFO_KHR);
1457 if (fd_info && !fd_info->handleType)
1458 fd_info = NULL;
1459
1460 if (fd_info) {
1461 assert(fd_info->handleType ==
1462 VK_EXTERNAL_MEMORY_HANDLE_TYPE_OPAQUE_FD_BIT ||
1463 fd_info->handleType ==
1464 VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_EXT);
1465
1466 /*
1467 * TODO Importing the same fd twice gives us the same handle without
1468 * reference counting. We need to maintain a per-instance handle-to-bo
1469 * table and add reference count to tu_bo.
1470 */
1471 result = tu_bo_init_dmabuf(device, &mem->bo,
1472 pAllocateInfo->allocationSize, fd_info->fd);
1473 if (result == VK_SUCCESS) {
1474 /* take ownership and close the fd */
1475 close(fd_info->fd);
1476 }
1477 } else {
1478 result =
1479 tu_bo_init_new(device, &mem->bo, pAllocateInfo->allocationSize);
1480 }
1481
1482 if (result != VK_SUCCESS) {
1483 vk_object_free(&device->vk, pAllocator, mem);
1484 return result;
1485 }
1486
1487 mem->size = pAllocateInfo->allocationSize;
1488 mem->type_index = pAllocateInfo->memoryTypeIndex;
1489
1490 mem->map = NULL;
1491 mem->user_ptr = NULL;
1492
1493 *pMem = tu_device_memory_to_handle(mem);
1494
1495 return VK_SUCCESS;
1496 }
1497
1498 VkResult
1499 tu_AllocateMemory(VkDevice _device,
1500 const VkMemoryAllocateInfo *pAllocateInfo,
1501 const VkAllocationCallbacks *pAllocator,
1502 VkDeviceMemory *pMem)
1503 {
1504 TU_FROM_HANDLE(tu_device, device, _device);
1505 return tu_alloc_memory(device, pAllocateInfo, pAllocator, pMem);
1506 }
1507
1508 void
1509 tu_FreeMemory(VkDevice _device,
1510 VkDeviceMemory _mem,
1511 const VkAllocationCallbacks *pAllocator)
1512 {
1513 TU_FROM_HANDLE(tu_device, device, _device);
1514 TU_FROM_HANDLE(tu_device_memory, mem, _mem);
1515
1516 if (mem == NULL)
1517 return;
1518
1519 tu_bo_finish(device, &mem->bo);
1520 vk_object_free(&device->vk, pAllocator, mem);
1521 }
1522
1523 VkResult
1524 tu_MapMemory(VkDevice _device,
1525 VkDeviceMemory _memory,
1526 VkDeviceSize offset,
1527 VkDeviceSize size,
1528 VkMemoryMapFlags flags,
1529 void **ppData)
1530 {
1531 TU_FROM_HANDLE(tu_device, device, _device);
1532 TU_FROM_HANDLE(tu_device_memory, mem, _memory);
1533 VkResult result;
1534
1535 if (mem == NULL) {
1536 *ppData = NULL;
1537 return VK_SUCCESS;
1538 }
1539
1540 if (mem->user_ptr) {
1541 *ppData = mem->user_ptr;
1542 } else if (!mem->map) {
1543 result = tu_bo_map(device, &mem->bo);
1544 if (result != VK_SUCCESS)
1545 return result;
1546 *ppData = mem->map = mem->bo.map;
1547 } else
1548 *ppData = mem->map;
1549
1550 if (*ppData) {
1551 *ppData += offset;
1552 return VK_SUCCESS;
1553 }
1554
1555 return vk_error(device->instance, VK_ERROR_MEMORY_MAP_FAILED);
1556 }
1557
1558 void
1559 tu_UnmapMemory(VkDevice _device, VkDeviceMemory _memory)
1560 {
1561 /* I do not see any unmapping done by the freedreno Gallium driver. */
1562 }
1563
1564 VkResult
1565 tu_FlushMappedMemoryRanges(VkDevice _device,
1566 uint32_t memoryRangeCount,
1567 const VkMappedMemoryRange *pMemoryRanges)
1568 {
1569 return VK_SUCCESS;
1570 }
1571
1572 VkResult
1573 tu_InvalidateMappedMemoryRanges(VkDevice _device,
1574 uint32_t memoryRangeCount,
1575 const VkMappedMemoryRange *pMemoryRanges)
1576 {
1577 return VK_SUCCESS;
1578 }
1579
1580 void
1581 tu_GetBufferMemoryRequirements(VkDevice _device,
1582 VkBuffer _buffer,
1583 VkMemoryRequirements *pMemoryRequirements)
1584 {
1585 TU_FROM_HANDLE(tu_buffer, buffer, _buffer);
1586
1587 pMemoryRequirements->memoryTypeBits = 1;
1588 pMemoryRequirements->alignment = 64;
1589 pMemoryRequirements->size =
1590 align64(buffer->size, pMemoryRequirements->alignment);
1591 }
1592
1593 void
1594 tu_GetBufferMemoryRequirements2(
1595 VkDevice device,
1596 const VkBufferMemoryRequirementsInfo2 *pInfo,
1597 VkMemoryRequirements2 *pMemoryRequirements)
1598 {
1599 tu_GetBufferMemoryRequirements(device, pInfo->buffer,
1600 &pMemoryRequirements->memoryRequirements);
1601 }
1602
1603 void
1604 tu_GetImageMemoryRequirements(VkDevice _device,
1605 VkImage _image,
1606 VkMemoryRequirements *pMemoryRequirements)
1607 {
1608 TU_FROM_HANDLE(tu_image, image, _image);
1609
1610 pMemoryRequirements->memoryTypeBits = 1;
1611 pMemoryRequirements->size = image->total_size;
1612 pMemoryRequirements->alignment = image->layout[0].base_align;
1613 }
1614
1615 void
1616 tu_GetImageMemoryRequirements2(VkDevice device,
1617 const VkImageMemoryRequirementsInfo2 *pInfo,
1618 VkMemoryRequirements2 *pMemoryRequirements)
1619 {
1620 tu_GetImageMemoryRequirements(device, pInfo->image,
1621 &pMemoryRequirements->memoryRequirements);
1622 }
1623
1624 void
1625 tu_GetImageSparseMemoryRequirements(
1626 VkDevice device,
1627 VkImage image,
1628 uint32_t *pSparseMemoryRequirementCount,
1629 VkSparseImageMemoryRequirements *pSparseMemoryRequirements)
1630 {
1631 tu_stub();
1632 }
1633
1634 void
1635 tu_GetImageSparseMemoryRequirements2(
1636 VkDevice device,
1637 const VkImageSparseMemoryRequirementsInfo2 *pInfo,
1638 uint32_t *pSparseMemoryRequirementCount,
1639 VkSparseImageMemoryRequirements2 *pSparseMemoryRequirements)
1640 {
1641 tu_stub();
1642 }
1643
1644 void
1645 tu_GetDeviceMemoryCommitment(VkDevice device,
1646 VkDeviceMemory memory,
1647 VkDeviceSize *pCommittedMemoryInBytes)
1648 {
1649 *pCommittedMemoryInBytes = 0;
1650 }
1651
1652 VkResult
1653 tu_BindBufferMemory2(VkDevice device,
1654 uint32_t bindInfoCount,
1655 const VkBindBufferMemoryInfo *pBindInfos)
1656 {
1657 for (uint32_t i = 0; i < bindInfoCount; ++i) {
1658 TU_FROM_HANDLE(tu_device_memory, mem, pBindInfos[i].memory);
1659 TU_FROM_HANDLE(tu_buffer, buffer, pBindInfos[i].buffer);
1660
1661 if (mem) {
1662 buffer->bo = &mem->bo;
1663 buffer->bo_offset = pBindInfos[i].memoryOffset;
1664 } else {
1665 buffer->bo = NULL;
1666 }
1667 }
1668 return VK_SUCCESS;
1669 }
1670
1671 VkResult
1672 tu_BindBufferMemory(VkDevice device,
1673 VkBuffer buffer,
1674 VkDeviceMemory memory,
1675 VkDeviceSize memoryOffset)
1676 {
1677 const VkBindBufferMemoryInfo info = {
1678 .sType = VK_STRUCTURE_TYPE_BIND_BUFFER_MEMORY_INFO,
1679 .buffer = buffer,
1680 .memory = memory,
1681 .memoryOffset = memoryOffset
1682 };
1683
1684 return tu_BindBufferMemory2(device, 1, &info);
1685 }
1686
1687 VkResult
1688 tu_BindImageMemory2(VkDevice device,
1689 uint32_t bindInfoCount,
1690 const VkBindImageMemoryInfo *pBindInfos)
1691 {
1692 for (uint32_t i = 0; i < bindInfoCount; ++i) {
1693 TU_FROM_HANDLE(tu_image, image, pBindInfos[i].image);
1694 TU_FROM_HANDLE(tu_device_memory, mem, pBindInfos[i].memory);
1695
1696 if (mem) {
1697 image->bo = &mem->bo;
1698 image->bo_offset = pBindInfos[i].memoryOffset;
1699 } else {
1700 image->bo = NULL;
1701 image->bo_offset = 0;
1702 }
1703 }
1704
1705 return VK_SUCCESS;
1706 }
1707
1708 VkResult
1709 tu_BindImageMemory(VkDevice device,
1710 VkImage image,
1711 VkDeviceMemory memory,
1712 VkDeviceSize memoryOffset)
1713 {
1714 const VkBindImageMemoryInfo info = {
1715 .sType = VK_STRUCTURE_TYPE_BIND_BUFFER_MEMORY_INFO,
1716 .image = image,
1717 .memory = memory,
1718 .memoryOffset = memoryOffset
1719 };
1720
1721 return tu_BindImageMemory2(device, 1, &info);
1722 }
1723
1724 VkResult
1725 tu_QueueBindSparse(VkQueue _queue,
1726 uint32_t bindInfoCount,
1727 const VkBindSparseInfo *pBindInfo,
1728 VkFence _fence)
1729 {
1730 return VK_SUCCESS;
1731 }
1732
1733
1734 VkResult
1735 tu_CreateEvent(VkDevice _device,
1736 const VkEventCreateInfo *pCreateInfo,
1737 const VkAllocationCallbacks *pAllocator,
1738 VkEvent *pEvent)
1739 {
1740 TU_FROM_HANDLE(tu_device, device, _device);
1741
1742 struct tu_event *event =
1743 vk_object_alloc(&device->vk, pAllocator, sizeof(*event),
1744 VK_OBJECT_TYPE_EVENT);
1745 if (!event)
1746 return vk_error(device->instance, VK_ERROR_OUT_OF_HOST_MEMORY);
1747
1748 VkResult result = tu_bo_init_new(device, &event->bo, 0x1000);
1749 if (result != VK_SUCCESS)
1750 goto fail_alloc;
1751
1752 result = tu_bo_map(device, &event->bo);
1753 if (result != VK_SUCCESS)
1754 goto fail_map;
1755
1756 *pEvent = tu_event_to_handle(event);
1757
1758 return VK_SUCCESS;
1759
1760 fail_map:
1761 tu_bo_finish(device, &event->bo);
1762 fail_alloc:
1763 vk_object_free(&device->vk, pAllocator, event);
1764 return vk_error(device->instance, VK_ERROR_OUT_OF_HOST_MEMORY);
1765 }
1766
1767 void
1768 tu_DestroyEvent(VkDevice _device,
1769 VkEvent _event,
1770 const VkAllocationCallbacks *pAllocator)
1771 {
1772 TU_FROM_HANDLE(tu_device, device, _device);
1773 TU_FROM_HANDLE(tu_event, event, _event);
1774
1775 if (!event)
1776 return;
1777
1778 tu_bo_finish(device, &event->bo);
1779 vk_object_free(&device->vk, pAllocator, event);
1780 }
1781
1782 VkResult
1783 tu_GetEventStatus(VkDevice _device, VkEvent _event)
1784 {
1785 TU_FROM_HANDLE(tu_event, event, _event);
1786
1787 if (*(uint64_t*) event->bo.map == 1)
1788 return VK_EVENT_SET;
1789 return VK_EVENT_RESET;
1790 }
1791
1792 VkResult
1793 tu_SetEvent(VkDevice _device, VkEvent _event)
1794 {
1795 TU_FROM_HANDLE(tu_event, event, _event);
1796 *(uint64_t*) event->bo.map = 1;
1797
1798 return VK_SUCCESS;
1799 }
1800
1801 VkResult
1802 tu_ResetEvent(VkDevice _device, VkEvent _event)
1803 {
1804 TU_FROM_HANDLE(tu_event, event, _event);
1805 *(uint64_t*) event->bo.map = 0;
1806
1807 return VK_SUCCESS;
1808 }
1809
1810 VkResult
1811 tu_CreateBuffer(VkDevice _device,
1812 const VkBufferCreateInfo *pCreateInfo,
1813 const VkAllocationCallbacks *pAllocator,
1814 VkBuffer *pBuffer)
1815 {
1816 TU_FROM_HANDLE(tu_device, device, _device);
1817 struct tu_buffer *buffer;
1818
1819 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO);
1820
1821 buffer = vk_object_alloc(&device->vk, pAllocator, sizeof(*buffer),
1822 VK_OBJECT_TYPE_BUFFER);
1823 if (buffer == NULL)
1824 return vk_error(device->instance, VK_ERROR_OUT_OF_HOST_MEMORY);
1825
1826 buffer->size = pCreateInfo->size;
1827 buffer->usage = pCreateInfo->usage;
1828 buffer->flags = pCreateInfo->flags;
1829
1830 *pBuffer = tu_buffer_to_handle(buffer);
1831
1832 return VK_SUCCESS;
1833 }
1834
1835 void
1836 tu_DestroyBuffer(VkDevice _device,
1837 VkBuffer _buffer,
1838 const VkAllocationCallbacks *pAllocator)
1839 {
1840 TU_FROM_HANDLE(tu_device, device, _device);
1841 TU_FROM_HANDLE(tu_buffer, buffer, _buffer);
1842
1843 if (!buffer)
1844 return;
1845
1846 vk_object_free(&device->vk, pAllocator, buffer);
1847 }
1848
1849 VkResult
1850 tu_CreateFramebuffer(VkDevice _device,
1851 const VkFramebufferCreateInfo *pCreateInfo,
1852 const VkAllocationCallbacks *pAllocator,
1853 VkFramebuffer *pFramebuffer)
1854 {
1855 TU_FROM_HANDLE(tu_device, device, _device);
1856 TU_FROM_HANDLE(tu_render_pass, pass, pCreateInfo->renderPass);
1857 struct tu_framebuffer *framebuffer;
1858
1859 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO);
1860
1861 size_t size = sizeof(*framebuffer) + sizeof(struct tu_attachment_info) *
1862 pCreateInfo->attachmentCount;
1863 framebuffer = vk_object_alloc(&device->vk, pAllocator, size,
1864 VK_OBJECT_TYPE_FRAMEBUFFER);
1865 if (framebuffer == NULL)
1866 return vk_error(device->instance, VK_ERROR_OUT_OF_HOST_MEMORY);
1867
1868 framebuffer->attachment_count = pCreateInfo->attachmentCount;
1869 framebuffer->width = pCreateInfo->width;
1870 framebuffer->height = pCreateInfo->height;
1871 framebuffer->layers = pCreateInfo->layers;
1872 for (uint32_t i = 0; i < pCreateInfo->attachmentCount; i++) {
1873 VkImageView _iview = pCreateInfo->pAttachments[i];
1874 struct tu_image_view *iview = tu_image_view_from_handle(_iview);
1875 framebuffer->attachments[i].attachment = iview;
1876 }
1877
1878 tu_framebuffer_tiling_config(framebuffer, device, pass);
1879
1880 *pFramebuffer = tu_framebuffer_to_handle(framebuffer);
1881 return VK_SUCCESS;
1882 }
1883
1884 void
1885 tu_DestroyFramebuffer(VkDevice _device,
1886 VkFramebuffer _fb,
1887 const VkAllocationCallbacks *pAllocator)
1888 {
1889 TU_FROM_HANDLE(tu_device, device, _device);
1890 TU_FROM_HANDLE(tu_framebuffer, fb, _fb);
1891
1892 if (!fb)
1893 return;
1894
1895 vk_object_free(&device->vk, pAllocator, fb);
1896 }
1897
1898 static void
1899 tu_init_sampler(struct tu_device *device,
1900 struct tu_sampler *sampler,
1901 const VkSamplerCreateInfo *pCreateInfo)
1902 {
1903 const struct VkSamplerReductionModeCreateInfo *reduction =
1904 vk_find_struct_const(pCreateInfo->pNext, SAMPLER_REDUCTION_MODE_CREATE_INFO);
1905 const struct VkSamplerYcbcrConversionInfo *ycbcr_conversion =
1906 vk_find_struct_const(pCreateInfo->pNext, SAMPLER_YCBCR_CONVERSION_INFO);
1907
1908 unsigned aniso = pCreateInfo->anisotropyEnable ?
1909 util_last_bit(MIN2((uint32_t)pCreateInfo->maxAnisotropy >> 1, 8)) : 0;
1910 bool miplinear = (pCreateInfo->mipmapMode == VK_SAMPLER_MIPMAP_MODE_LINEAR);
1911 float min_lod = CLAMP(pCreateInfo->minLod, 0.0f, 4095.0f / 256.0f);
1912 float max_lod = CLAMP(pCreateInfo->maxLod, 0.0f, 4095.0f / 256.0f);
1913
1914 sampler->descriptor[0] =
1915 COND(miplinear, A6XX_TEX_SAMP_0_MIPFILTER_LINEAR_NEAR) |
1916 A6XX_TEX_SAMP_0_XY_MAG(tu6_tex_filter(pCreateInfo->magFilter, aniso)) |
1917 A6XX_TEX_SAMP_0_XY_MIN(tu6_tex_filter(pCreateInfo->minFilter, aniso)) |
1918 A6XX_TEX_SAMP_0_ANISO(aniso) |
1919 A6XX_TEX_SAMP_0_WRAP_S(tu6_tex_wrap(pCreateInfo->addressModeU)) |
1920 A6XX_TEX_SAMP_0_WRAP_T(tu6_tex_wrap(pCreateInfo->addressModeV)) |
1921 A6XX_TEX_SAMP_0_WRAP_R(tu6_tex_wrap(pCreateInfo->addressModeW)) |
1922 A6XX_TEX_SAMP_0_LOD_BIAS(pCreateInfo->mipLodBias);
1923 sampler->descriptor[1] =
1924 /* COND(!cso->seamless_cube_map, A6XX_TEX_SAMP_1_CUBEMAPSEAMLESSFILTOFF) | */
1925 COND(pCreateInfo->unnormalizedCoordinates, A6XX_TEX_SAMP_1_UNNORM_COORDS) |
1926 A6XX_TEX_SAMP_1_MIN_LOD(min_lod) |
1927 A6XX_TEX_SAMP_1_MAX_LOD(max_lod) |
1928 COND(pCreateInfo->compareEnable,
1929 A6XX_TEX_SAMP_1_COMPARE_FUNC(tu6_compare_func(pCreateInfo->compareOp)));
1930 /* This is an offset into the border_color BO, which we fill with all the
1931 * possible Vulkan border colors in the correct order, so we can just use
1932 * the Vulkan enum with no translation necessary.
1933 */
1934 sampler->descriptor[2] =
1935 A6XX_TEX_SAMP_2_BCOLOR_OFFSET((unsigned) pCreateInfo->borderColor *
1936 sizeof(struct bcolor_entry));
1937 sampler->descriptor[3] = 0;
1938
1939 if (reduction) {
1940 sampler->descriptor[2] |= A6XX_TEX_SAMP_2_REDUCTION_MODE(
1941 tu6_reduction_mode(reduction->reductionMode));
1942 }
1943
1944 sampler->ycbcr_sampler = ycbcr_conversion ?
1945 tu_sampler_ycbcr_conversion_from_handle(ycbcr_conversion->conversion) : NULL;
1946
1947 if (sampler->ycbcr_sampler &&
1948 sampler->ycbcr_sampler->chroma_filter == VK_FILTER_LINEAR) {
1949 sampler->descriptor[2] |= A6XX_TEX_SAMP_2_CHROMA_LINEAR;
1950 }
1951
1952 /* TODO:
1953 * A6XX_TEX_SAMP_1_MIPFILTER_LINEAR_FAR disables mipmapping, but vk has no NONE mipfilter?
1954 */
1955 }
1956
1957 VkResult
1958 tu_CreateSampler(VkDevice _device,
1959 const VkSamplerCreateInfo *pCreateInfo,
1960 const VkAllocationCallbacks *pAllocator,
1961 VkSampler *pSampler)
1962 {
1963 TU_FROM_HANDLE(tu_device, device, _device);
1964 struct tu_sampler *sampler;
1965
1966 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO);
1967
1968 sampler = vk_object_alloc(&device->vk, pAllocator, sizeof(*sampler),
1969 VK_OBJECT_TYPE_SAMPLER);
1970 if (!sampler)
1971 return vk_error(device->instance, VK_ERROR_OUT_OF_HOST_MEMORY);
1972
1973 tu_init_sampler(device, sampler, pCreateInfo);
1974 *pSampler = tu_sampler_to_handle(sampler);
1975
1976 return VK_SUCCESS;
1977 }
1978
1979 void
1980 tu_DestroySampler(VkDevice _device,
1981 VkSampler _sampler,
1982 const VkAllocationCallbacks *pAllocator)
1983 {
1984 TU_FROM_HANDLE(tu_device, device, _device);
1985 TU_FROM_HANDLE(tu_sampler, sampler, _sampler);
1986
1987 if (!sampler)
1988 return;
1989
1990 vk_object_free(&device->vk, pAllocator, sampler);
1991 }
1992
1993 /* vk_icd.h does not declare this function, so we declare it here to
1994 * suppress Wmissing-prototypes.
1995 */
1996 PUBLIC VKAPI_ATTR VkResult VKAPI_CALL
1997 vk_icdNegotiateLoaderICDInterfaceVersion(uint32_t *pSupportedVersion);
1998
1999 PUBLIC VKAPI_ATTR VkResult VKAPI_CALL
2000 vk_icdNegotiateLoaderICDInterfaceVersion(uint32_t *pSupportedVersion)
2001 {
2002 /* For the full details on loader interface versioning, see
2003 * <https://github.com/KhronosGroup/Vulkan-LoaderAndValidationLayers/blob/master/loader/LoaderAndLayerInterface.md>.
2004 * What follows is a condensed summary, to help you navigate the large and
2005 * confusing official doc.
2006 *
2007 * - Loader interface v0 is incompatible with later versions. We don't
2008 * support it.
2009 *
2010 * - In loader interface v1:
2011 * - The first ICD entrypoint called by the loader is
2012 * vk_icdGetInstanceProcAddr(). The ICD must statically expose this
2013 * entrypoint.
2014 * - The ICD must statically expose no other Vulkan symbol unless it
2015 * is linked with -Bsymbolic.
2016 * - Each dispatchable Vulkan handle created by the ICD must be
2017 * a pointer to a struct whose first member is VK_LOADER_DATA. The
2018 * ICD must initialize VK_LOADER_DATA.loadMagic to
2019 * ICD_LOADER_MAGIC.
2020 * - The loader implements vkCreate{PLATFORM}SurfaceKHR() and
2021 * vkDestroySurfaceKHR(). The ICD must be capable of working with
2022 * such loader-managed surfaces.
2023 *
2024 * - Loader interface v2 differs from v1 in:
2025 * - The first ICD entrypoint called by the loader is
2026 * vk_icdNegotiateLoaderICDInterfaceVersion(). The ICD must
2027 * statically expose this entrypoint.
2028 *
2029 * - Loader interface v3 differs from v2 in:
2030 * - The ICD must implement vkCreate{PLATFORM}SurfaceKHR(),
2031 * vkDestroySurfaceKHR(), and other API which uses VKSurfaceKHR,
2032 * because the loader no longer does so.
2033 */
2034 *pSupportedVersion = MIN2(*pSupportedVersion, 3u);
2035 return VK_SUCCESS;
2036 }
2037
2038 VkResult
2039 tu_GetMemoryFdKHR(VkDevice _device,
2040 const VkMemoryGetFdInfoKHR *pGetFdInfo,
2041 int *pFd)
2042 {
2043 TU_FROM_HANDLE(tu_device, device, _device);
2044 TU_FROM_HANDLE(tu_device_memory, memory, pGetFdInfo->memory);
2045
2046 assert(pGetFdInfo->sType == VK_STRUCTURE_TYPE_MEMORY_GET_FD_INFO_KHR);
2047
2048 /* At the moment, we support only the below handle types. */
2049 assert(pGetFdInfo->handleType ==
2050 VK_EXTERNAL_MEMORY_HANDLE_TYPE_OPAQUE_FD_BIT ||
2051 pGetFdInfo->handleType ==
2052 VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_EXT);
2053
2054 int prime_fd = tu_bo_export_dmabuf(device, &memory->bo);
2055 if (prime_fd < 0)
2056 return vk_error(device->instance, VK_ERROR_OUT_OF_DEVICE_MEMORY);
2057
2058 *pFd = prime_fd;
2059 return VK_SUCCESS;
2060 }
2061
2062 VkResult
2063 tu_GetMemoryFdPropertiesKHR(VkDevice _device,
2064 VkExternalMemoryHandleTypeFlagBits handleType,
2065 int fd,
2066 VkMemoryFdPropertiesKHR *pMemoryFdProperties)
2067 {
2068 assert(handleType == VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_EXT);
2069 pMemoryFdProperties->memoryTypeBits = 1;
2070 return VK_SUCCESS;
2071 }
2072
2073 VkResult
2074 tu_ImportFenceFdKHR(VkDevice _device,
2075 const VkImportFenceFdInfoKHR *pImportFenceFdInfo)
2076 {
2077 tu_stub();
2078
2079 return VK_SUCCESS;
2080 }
2081
2082 VkResult
2083 tu_GetFenceFdKHR(VkDevice _device,
2084 const VkFenceGetFdInfoKHR *pGetFdInfo,
2085 int *pFd)
2086 {
2087 tu_stub();
2088
2089 return VK_SUCCESS;
2090 }
2091
2092 void
2093 tu_GetPhysicalDeviceExternalFenceProperties(
2094 VkPhysicalDevice physicalDevice,
2095 const VkPhysicalDeviceExternalFenceInfo *pExternalFenceInfo,
2096 VkExternalFenceProperties *pExternalFenceProperties)
2097 {
2098 pExternalFenceProperties->exportFromImportedHandleTypes = 0;
2099 pExternalFenceProperties->compatibleHandleTypes = 0;
2100 pExternalFenceProperties->externalFenceFeatures = 0;
2101 }
2102
2103 VkResult
2104 tu_CreateDebugReportCallbackEXT(
2105 VkInstance _instance,
2106 const VkDebugReportCallbackCreateInfoEXT *pCreateInfo,
2107 const VkAllocationCallbacks *pAllocator,
2108 VkDebugReportCallbackEXT *pCallback)
2109 {
2110 TU_FROM_HANDLE(tu_instance, instance, _instance);
2111 return vk_create_debug_report_callback(&instance->debug_report_callbacks,
2112 pCreateInfo, pAllocator,
2113 &instance->alloc, pCallback);
2114 }
2115
2116 void
2117 tu_DestroyDebugReportCallbackEXT(VkInstance _instance,
2118 VkDebugReportCallbackEXT _callback,
2119 const VkAllocationCallbacks *pAllocator)
2120 {
2121 TU_FROM_HANDLE(tu_instance, instance, _instance);
2122 vk_destroy_debug_report_callback(&instance->debug_report_callbacks,
2123 _callback, pAllocator, &instance->alloc);
2124 }
2125
2126 void
2127 tu_DebugReportMessageEXT(VkInstance _instance,
2128 VkDebugReportFlagsEXT flags,
2129 VkDebugReportObjectTypeEXT objectType,
2130 uint64_t object,
2131 size_t location,
2132 int32_t messageCode,
2133 const char *pLayerPrefix,
2134 const char *pMessage)
2135 {
2136 TU_FROM_HANDLE(tu_instance, instance, _instance);
2137 vk_debug_report(&instance->debug_report_callbacks, flags, objectType,
2138 object, location, messageCode, pLayerPrefix, pMessage);
2139 }
2140
2141 void
2142 tu_GetDeviceGroupPeerMemoryFeatures(
2143 VkDevice device,
2144 uint32_t heapIndex,
2145 uint32_t localDeviceIndex,
2146 uint32_t remoteDeviceIndex,
2147 VkPeerMemoryFeatureFlags *pPeerMemoryFeatures)
2148 {
2149 assert(localDeviceIndex == remoteDeviceIndex);
2150
2151 *pPeerMemoryFeatures = VK_PEER_MEMORY_FEATURE_COPY_SRC_BIT |
2152 VK_PEER_MEMORY_FEATURE_COPY_DST_BIT |
2153 VK_PEER_MEMORY_FEATURE_GENERIC_SRC_BIT |
2154 VK_PEER_MEMORY_FEATURE_GENERIC_DST_BIT;
2155 }
2156
2157 void tu_GetPhysicalDeviceMultisamplePropertiesEXT(
2158 VkPhysicalDevice physicalDevice,
2159 VkSampleCountFlagBits samples,
2160 VkMultisamplePropertiesEXT* pMultisampleProperties)
2161 {
2162 TU_FROM_HANDLE(tu_physical_device, pdevice, physicalDevice);
2163
2164 if (samples <= VK_SAMPLE_COUNT_4_BIT && pdevice->supported_extensions.EXT_sample_locations)
2165 pMultisampleProperties->maxSampleLocationGridSize = (VkExtent2D){ 1, 1 };
2166 else
2167 pMultisampleProperties->maxSampleLocationGridSize = (VkExtent2D){ 0, 0 };
2168 }
2169
2170
2171 VkResult
2172 tu_CreatePrivateDataSlotEXT(VkDevice _device,
2173 const VkPrivateDataSlotCreateInfoEXT* pCreateInfo,
2174 const VkAllocationCallbacks* pAllocator,
2175 VkPrivateDataSlotEXT* pPrivateDataSlot)
2176 {
2177 TU_FROM_HANDLE(tu_device, device, _device);
2178 return vk_private_data_slot_create(&device->vk,
2179 pCreateInfo,
2180 pAllocator,
2181 pPrivateDataSlot);
2182 }
2183
2184 void
2185 tu_DestroyPrivateDataSlotEXT(VkDevice _device,
2186 VkPrivateDataSlotEXT privateDataSlot,
2187 const VkAllocationCallbacks* pAllocator)
2188 {
2189 TU_FROM_HANDLE(tu_device, device, _device);
2190 vk_private_data_slot_destroy(&device->vk, privateDataSlot, pAllocator);
2191 }
2192
2193 VkResult
2194 tu_SetPrivateDataEXT(VkDevice _device,
2195 VkObjectType objectType,
2196 uint64_t objectHandle,
2197 VkPrivateDataSlotEXT privateDataSlot,
2198 uint64_t data)
2199 {
2200 TU_FROM_HANDLE(tu_device, device, _device);
2201 return vk_object_base_set_private_data(&device->vk,
2202 objectType,
2203 objectHandle,
2204 privateDataSlot,
2205 data);
2206 }
2207
2208 void
2209 tu_GetPrivateDataEXT(VkDevice _device,
2210 VkObjectType objectType,
2211 uint64_t objectHandle,
2212 VkPrivateDataSlotEXT privateDataSlot,
2213 uint64_t* pData)
2214 {
2215 TU_FROM_HANDLE(tu_device, device, _device);
2216 vk_object_base_get_private_data(&device->vk,
2217 objectType,
2218 objectHandle,
2219 privateDataSlot,
2220 pData);
2221 }