radv: make descriptor BOs read-only for the GPU
[mesa.git] / src / amd / vulkan / radv_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 DEALINGS
25 * IN THE SOFTWARE.
26 */
27
28 #include <stdbool.h>
29 #include <string.h>
30 #include <unistd.h>
31 #include <fcntl.h>
32 #include "radv_debug.h"
33 #include "radv_private.h"
34 #include "radv_shader.h"
35 #include "radv_cs.h"
36 #include "util/disk_cache.h"
37 #include "util/strtod.h"
38 #include "vk_util.h"
39 #include <xf86drm.h>
40 #include <amdgpu.h>
41 #include <amdgpu_drm.h>
42 #include "winsys/amdgpu/radv_amdgpu_winsys_public.h"
43 #include "ac_llvm_util.h"
44 #include "vk_format.h"
45 #include "sid.h"
46 #include "gfx9d.h"
47 #include "util/debug.h"
48
49 static int
50 radv_device_get_cache_uuid(enum radeon_family family, void *uuid)
51 {
52 uint32_t mesa_timestamp, llvm_timestamp;
53 uint16_t f = family;
54 memset(uuid, 0, VK_UUID_SIZE);
55 if (!disk_cache_get_function_timestamp(radv_device_get_cache_uuid, &mesa_timestamp) ||
56 !disk_cache_get_function_timestamp(LLVMInitializeAMDGPUTargetInfo, &llvm_timestamp))
57 return -1;
58
59 memcpy(uuid, &mesa_timestamp, 4);
60 memcpy((char*)uuid + 4, &llvm_timestamp, 4);
61 memcpy((char*)uuid + 8, &f, 2);
62 snprintf((char*)uuid + 10, VK_UUID_SIZE - 10, "radv");
63 return 0;
64 }
65
66 static void
67 radv_get_driver_uuid(void *uuid)
68 {
69 ac_compute_driver_uuid(uuid, VK_UUID_SIZE);
70 }
71
72 static void
73 radv_get_device_uuid(struct radeon_info *info, void *uuid)
74 {
75 ac_compute_device_uuid(info, uuid, VK_UUID_SIZE);
76 }
77
78 static void
79 radv_get_device_name(enum radeon_family family, char *name, size_t name_len)
80 {
81 const char *chip_string;
82 char llvm_string[32] = {};
83
84 switch (family) {
85 case CHIP_TAHITI: chip_string = "AMD RADV TAHITI"; break;
86 case CHIP_PITCAIRN: chip_string = "AMD RADV PITCAIRN"; break;
87 case CHIP_VERDE: chip_string = "AMD RADV CAPE VERDE"; break;
88 case CHIP_OLAND: chip_string = "AMD RADV OLAND"; break;
89 case CHIP_HAINAN: chip_string = "AMD RADV HAINAN"; break;
90 case CHIP_BONAIRE: chip_string = "AMD RADV BONAIRE"; break;
91 case CHIP_KAVERI: chip_string = "AMD RADV KAVERI"; break;
92 case CHIP_KABINI: chip_string = "AMD RADV KABINI"; break;
93 case CHIP_HAWAII: chip_string = "AMD RADV HAWAII"; break;
94 case CHIP_MULLINS: chip_string = "AMD RADV MULLINS"; break;
95 case CHIP_TONGA: chip_string = "AMD RADV TONGA"; break;
96 case CHIP_ICELAND: chip_string = "AMD RADV ICELAND"; break;
97 case CHIP_CARRIZO: chip_string = "AMD RADV CARRIZO"; break;
98 case CHIP_FIJI: chip_string = "AMD RADV FIJI"; break;
99 case CHIP_POLARIS10: chip_string = "AMD RADV POLARIS10"; break;
100 case CHIP_POLARIS11: chip_string = "AMD RADV POLARIS11"; break;
101 case CHIP_POLARIS12: chip_string = "AMD RADV POLARIS12"; break;
102 case CHIP_STONEY: chip_string = "AMD RADV STONEY"; break;
103 case CHIP_VEGA10: chip_string = "AMD RADV VEGA"; break;
104 case CHIP_RAVEN: chip_string = "AMD RADV RAVEN"; break;
105 default: chip_string = "AMD RADV unknown"; break;
106 }
107
108 if (HAVE_LLVM > 0) {
109 snprintf(llvm_string, sizeof(llvm_string),
110 " (LLVM %i.%i.%i)", (HAVE_LLVM >> 8) & 0xff,
111 HAVE_LLVM & 0xff, MESA_LLVM_VERSION_PATCH);
112 }
113
114 snprintf(name, name_len, "%s%s", chip_string, llvm_string);
115 }
116
117 static void
118 radv_physical_device_init_mem_types(struct radv_physical_device *device)
119 {
120 STATIC_ASSERT(RADV_MEM_HEAP_COUNT <= VK_MAX_MEMORY_HEAPS);
121 uint64_t visible_vram_size = MIN2(device->rad_info.vram_size,
122 device->rad_info.vram_vis_size);
123
124 int vram_index = -1, visible_vram_index = -1, gart_index = -1;
125 device->memory_properties.memoryHeapCount = 0;
126 if (device->rad_info.vram_size - visible_vram_size > 0) {
127 vram_index = device->memory_properties.memoryHeapCount++;
128 device->memory_properties.memoryHeaps[vram_index] = (VkMemoryHeap) {
129 .size = device->rad_info.vram_size - visible_vram_size,
130 .flags = VK_MEMORY_HEAP_DEVICE_LOCAL_BIT,
131 };
132 }
133 if (visible_vram_size) {
134 visible_vram_index = device->memory_properties.memoryHeapCount++;
135 device->memory_properties.memoryHeaps[visible_vram_index] = (VkMemoryHeap) {
136 .size = visible_vram_size,
137 .flags = VK_MEMORY_HEAP_DEVICE_LOCAL_BIT,
138 };
139 }
140 if (device->rad_info.gart_size > 0) {
141 gart_index = device->memory_properties.memoryHeapCount++;
142 device->memory_properties.memoryHeaps[gart_index] = (VkMemoryHeap) {
143 .size = device->rad_info.gart_size,
144 .flags = 0,
145 };
146 }
147
148 STATIC_ASSERT(RADV_MEM_TYPE_COUNT <= VK_MAX_MEMORY_TYPES);
149 unsigned type_count = 0;
150 if (vram_index >= 0) {
151 device->mem_type_indices[type_count] = RADV_MEM_TYPE_VRAM;
152 device->memory_properties.memoryTypes[type_count++] = (VkMemoryType) {
153 .propertyFlags = VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT,
154 .heapIndex = vram_index,
155 };
156 }
157 if (gart_index >= 0) {
158 device->mem_type_indices[type_count] = RADV_MEM_TYPE_GTT_WRITE_COMBINE;
159 device->memory_properties.memoryTypes[type_count++] = (VkMemoryType) {
160 .propertyFlags = VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT |
161 VK_MEMORY_PROPERTY_HOST_COHERENT_BIT,
162 .heapIndex = gart_index,
163 };
164 }
165 if (visible_vram_index >= 0) {
166 device->mem_type_indices[type_count] = RADV_MEM_TYPE_VRAM_CPU_ACCESS;
167 device->memory_properties.memoryTypes[type_count++] = (VkMemoryType) {
168 .propertyFlags = VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT |
169 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT |
170 VK_MEMORY_PROPERTY_HOST_COHERENT_BIT,
171 .heapIndex = visible_vram_index,
172 };
173 }
174 if (gart_index >= 0) {
175 device->mem_type_indices[type_count] = RADV_MEM_TYPE_GTT_CACHED;
176 device->memory_properties.memoryTypes[type_count++] = (VkMemoryType) {
177 .propertyFlags = VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT |
178 VK_MEMORY_PROPERTY_HOST_COHERENT_BIT |
179 VK_MEMORY_PROPERTY_HOST_CACHED_BIT,
180 .heapIndex = gart_index,
181 };
182 }
183 device->memory_properties.memoryTypeCount = type_count;
184 }
185
186 static VkResult
187 radv_physical_device_init(struct radv_physical_device *device,
188 struct radv_instance *instance,
189 drmDevicePtr drm_device)
190 {
191 const char *path = drm_device->nodes[DRM_NODE_RENDER];
192 VkResult result;
193 drmVersionPtr version;
194 int fd;
195
196 fd = open(path, O_RDWR | O_CLOEXEC);
197 if (fd < 0)
198 return vk_error(VK_ERROR_INCOMPATIBLE_DRIVER);
199
200 version = drmGetVersion(fd);
201 if (!version) {
202 close(fd);
203 return vk_errorf(VK_ERROR_INCOMPATIBLE_DRIVER,
204 "failed to get version %s: %m", path);
205 }
206
207 if (strcmp(version->name, "amdgpu")) {
208 drmFreeVersion(version);
209 close(fd);
210 return VK_ERROR_INCOMPATIBLE_DRIVER;
211 }
212 drmFreeVersion(version);
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 device->ws = radv_amdgpu_winsys_create(fd, instance->debug_flags,
220 instance->perftest_flags);
221 if (!device->ws) {
222 result = VK_ERROR_INCOMPATIBLE_DRIVER;
223 goto fail;
224 }
225
226 device->local_fd = fd;
227 device->ws->query_info(device->ws, &device->rad_info);
228
229 radv_get_device_name(device->rad_info.family, device->name, sizeof(device->name));
230
231 if (radv_device_get_cache_uuid(device->rad_info.family, device->cache_uuid)) {
232 device->ws->destroy(device->ws);
233 result = vk_errorf(VK_ERROR_INITIALIZATION_FAILED,
234 "cannot generate UUID");
235 goto fail;
236 }
237
238 /* These flags affect shader compilation. */
239 uint64_t shader_env_flags =
240 (device->instance->perftest_flags & RADV_PERFTEST_SISCHED ? 0x1 : 0) |
241 (device->instance->debug_flags & RADV_DEBUG_UNSAFE_MATH ? 0x2 : 0);
242
243 /* The gpu id is already embeded in the uuid so we just pass "radv"
244 * when creating the cache.
245 */
246 char buf[VK_UUID_SIZE * 2 + 1];
247 disk_cache_format_hex_id(buf, device->cache_uuid, VK_UUID_SIZE * 2);
248 device->disk_cache = disk_cache_create(device->name, buf, shader_env_flags);
249
250 fprintf(stderr, "WARNING: radv is not a conformant vulkan implementation, testing use only.\n");
251
252 radv_get_driver_uuid(&device->device_uuid);
253 radv_get_device_uuid(&device->rad_info, &device->device_uuid);
254
255 if (device->rad_info.family == CHIP_STONEY ||
256 device->rad_info.chip_class >= GFX9) {
257 device->has_rbplus = true;
258 device->rbplus_allowed = device->rad_info.family == CHIP_STONEY;
259 }
260
261 /* The mere presense of CLEAR_STATE in the IB causes random GPU hangs
262 * on SI.
263 */
264 device->has_clear_state = device->rad_info.chip_class >= CIK;
265
266 radv_physical_device_init_mem_types(device);
267
268 result = radv_init_wsi(device);
269 if (result != VK_SUCCESS) {
270 device->ws->destroy(device->ws);
271 goto fail;
272 }
273
274 return VK_SUCCESS;
275
276 fail:
277 close(fd);
278 return result;
279 }
280
281 static void
282 radv_physical_device_finish(struct radv_physical_device *device)
283 {
284 radv_finish_wsi(device);
285 device->ws->destroy(device->ws);
286 disk_cache_destroy(device->disk_cache);
287 close(device->local_fd);
288 }
289
290 static void *
291 default_alloc_func(void *pUserData, size_t size, size_t align,
292 VkSystemAllocationScope allocationScope)
293 {
294 return malloc(size);
295 }
296
297 static void *
298 default_realloc_func(void *pUserData, void *pOriginal, size_t size,
299 size_t align, VkSystemAllocationScope allocationScope)
300 {
301 return realloc(pOriginal, size);
302 }
303
304 static void
305 default_free_func(void *pUserData, void *pMemory)
306 {
307 free(pMemory);
308 }
309
310 static const VkAllocationCallbacks default_alloc = {
311 .pUserData = NULL,
312 .pfnAllocation = default_alloc_func,
313 .pfnReallocation = default_realloc_func,
314 .pfnFree = default_free_func,
315 };
316
317 static const struct debug_control radv_debug_options[] = {
318 {"nofastclears", RADV_DEBUG_NO_FAST_CLEARS},
319 {"nodcc", RADV_DEBUG_NO_DCC},
320 {"shaders", RADV_DEBUG_DUMP_SHADERS},
321 {"nocache", RADV_DEBUG_NO_CACHE},
322 {"shaderstats", RADV_DEBUG_DUMP_SHADER_STATS},
323 {"nohiz", RADV_DEBUG_NO_HIZ},
324 {"nocompute", RADV_DEBUG_NO_COMPUTE_QUEUE},
325 {"unsafemath", RADV_DEBUG_UNSAFE_MATH},
326 {"allbos", RADV_DEBUG_ALL_BOS},
327 {"noibs", RADV_DEBUG_NO_IBS},
328 {"spirv", RADV_DEBUG_DUMP_SPIRV},
329 {"vmfaults", RADV_DEBUG_VM_FAULTS},
330 {"zerovram", RADV_DEBUG_ZERO_VRAM},
331 {"syncshaders", RADV_DEBUG_SYNC_SHADERS},
332 {"nosisched", RADV_DEBUG_NO_SISCHED},
333 {NULL, 0}
334 };
335
336 const char *
337 radv_get_debug_option_name(int id)
338 {
339 assert(id < ARRAY_SIZE(radv_debug_options) - 1);
340 return radv_debug_options[id].string;
341 }
342
343 static const struct debug_control radv_perftest_options[] = {
344 {"nobatchchain", RADV_PERFTEST_NO_BATCHCHAIN},
345 {"sisched", RADV_PERFTEST_SISCHED},
346 {"localbos", RADV_PERFTEST_LOCAL_BOS},
347 {"binning", RADV_PERFTEST_BINNING},
348 {NULL, 0}
349 };
350
351 const char *
352 radv_get_perftest_option_name(int id)
353 {
354 assert(id < ARRAY_SIZE(radv_debug_options) - 1);
355 return radv_perftest_options[id].string;
356 }
357
358 static void
359 radv_handle_per_app_options(struct radv_instance *instance,
360 const VkApplicationInfo *info)
361 {
362 const char *name = info ? info->pApplicationName : NULL;
363
364 if (!name)
365 return;
366
367 if (!strcmp(name, "Talos - Linux - 32bit") ||
368 !strcmp(name, "Talos - Linux - 64bit")) {
369 /* Force enable LLVM sisched for Talos because it looks safe
370 * and it gives few more FPS.
371 */
372 instance->perftest_flags |= RADV_PERFTEST_SISCHED;
373 }
374 }
375
376 VkResult radv_CreateInstance(
377 const VkInstanceCreateInfo* pCreateInfo,
378 const VkAllocationCallbacks* pAllocator,
379 VkInstance* pInstance)
380 {
381 struct radv_instance *instance;
382
383 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO);
384
385 uint32_t client_version;
386 if (pCreateInfo->pApplicationInfo &&
387 pCreateInfo->pApplicationInfo->apiVersion != 0) {
388 client_version = pCreateInfo->pApplicationInfo->apiVersion;
389 } else {
390 client_version = VK_MAKE_VERSION(1, 0, 0);
391 }
392
393 if (VK_MAKE_VERSION(1, 0, 0) > client_version ||
394 client_version > VK_MAKE_VERSION(1, 0, 0xfff)) {
395 return vk_errorf(VK_ERROR_INCOMPATIBLE_DRIVER,
396 "Client requested version %d.%d.%d",
397 VK_VERSION_MAJOR(client_version),
398 VK_VERSION_MINOR(client_version),
399 VK_VERSION_PATCH(client_version));
400 }
401
402 for (uint32_t i = 0; i < pCreateInfo->enabledExtensionCount; i++) {
403 const char *ext_name = pCreateInfo->ppEnabledExtensionNames[i];
404 if (!radv_instance_extension_supported(ext_name))
405 return vk_error(VK_ERROR_EXTENSION_NOT_PRESENT);
406 }
407
408 instance = vk_zalloc2(&default_alloc, pAllocator, sizeof(*instance), 8,
409 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
410 if (!instance)
411 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
412
413 instance->_loader_data.loaderMagic = ICD_LOADER_MAGIC;
414
415 if (pAllocator)
416 instance->alloc = *pAllocator;
417 else
418 instance->alloc = default_alloc;
419
420 instance->apiVersion = client_version;
421 instance->physicalDeviceCount = -1;
422
423 _mesa_locale_init();
424
425 VG(VALGRIND_CREATE_MEMPOOL(instance, 0, false));
426
427 instance->debug_flags = parse_debug_string(getenv("RADV_DEBUG"),
428 radv_debug_options);
429
430 instance->perftest_flags = parse_debug_string(getenv("RADV_PERFTEST"),
431 radv_perftest_options);
432
433 radv_handle_per_app_options(instance, pCreateInfo->pApplicationInfo);
434
435 if (instance->debug_flags & RADV_DEBUG_NO_SISCHED) {
436 /* Disable sisched when the user requests it, this is mostly
437 * useful when the driver force-enable sisched for the given
438 * application.
439 */
440 instance->perftest_flags &= ~RADV_PERFTEST_SISCHED;
441 }
442
443 *pInstance = radv_instance_to_handle(instance);
444
445 return VK_SUCCESS;
446 }
447
448 void radv_DestroyInstance(
449 VkInstance _instance,
450 const VkAllocationCallbacks* pAllocator)
451 {
452 RADV_FROM_HANDLE(radv_instance, instance, _instance);
453
454 if (!instance)
455 return;
456
457 for (int i = 0; i < instance->physicalDeviceCount; ++i) {
458 radv_physical_device_finish(instance->physicalDevices + i);
459 }
460
461 VG(VALGRIND_DESTROY_MEMPOOL(instance));
462
463 _mesa_locale_fini();
464
465 vk_free(&instance->alloc, instance);
466 }
467
468 static VkResult
469 radv_enumerate_devices(struct radv_instance *instance)
470 {
471 /* TODO: Check for more devices ? */
472 drmDevicePtr devices[8];
473 VkResult result = VK_ERROR_INCOMPATIBLE_DRIVER;
474 int max_devices;
475
476 instance->physicalDeviceCount = 0;
477
478 max_devices = drmGetDevices2(0, devices, ARRAY_SIZE(devices));
479 if (max_devices < 1)
480 return vk_error(VK_ERROR_INCOMPATIBLE_DRIVER);
481
482 for (unsigned i = 0; i < (unsigned)max_devices; i++) {
483 if (devices[i]->available_nodes & 1 << DRM_NODE_RENDER &&
484 devices[i]->bustype == DRM_BUS_PCI &&
485 devices[i]->deviceinfo.pci->vendor_id == ATI_VENDOR_ID) {
486
487 result = radv_physical_device_init(instance->physicalDevices +
488 instance->physicalDeviceCount,
489 instance,
490 devices[i]);
491 if (result == VK_SUCCESS)
492 ++instance->physicalDeviceCount;
493 else if (result != VK_ERROR_INCOMPATIBLE_DRIVER)
494 break;
495 }
496 }
497 drmFreeDevices(devices, max_devices);
498
499 return result;
500 }
501
502 VkResult radv_EnumeratePhysicalDevices(
503 VkInstance _instance,
504 uint32_t* pPhysicalDeviceCount,
505 VkPhysicalDevice* pPhysicalDevices)
506 {
507 RADV_FROM_HANDLE(radv_instance, instance, _instance);
508 VkResult result;
509
510 if (instance->physicalDeviceCount < 0) {
511 result = radv_enumerate_devices(instance);
512 if (result != VK_SUCCESS &&
513 result != VK_ERROR_INCOMPATIBLE_DRIVER)
514 return result;
515 }
516
517 if (!pPhysicalDevices) {
518 *pPhysicalDeviceCount = instance->physicalDeviceCount;
519 } else {
520 *pPhysicalDeviceCount = MIN2(*pPhysicalDeviceCount, instance->physicalDeviceCount);
521 for (unsigned i = 0; i < *pPhysicalDeviceCount; ++i)
522 pPhysicalDevices[i] = radv_physical_device_to_handle(instance->physicalDevices + i);
523 }
524
525 return *pPhysicalDeviceCount < instance->physicalDeviceCount ? VK_INCOMPLETE
526 : VK_SUCCESS;
527 }
528
529 void radv_GetPhysicalDeviceFeatures(
530 VkPhysicalDevice physicalDevice,
531 VkPhysicalDeviceFeatures* pFeatures)
532 {
533 memset(pFeatures, 0, sizeof(*pFeatures));
534
535 *pFeatures = (VkPhysicalDeviceFeatures) {
536 .robustBufferAccess = true,
537 .fullDrawIndexUint32 = true,
538 .imageCubeArray = true,
539 .independentBlend = true,
540 .geometryShader = true,
541 .tessellationShader = true,
542 .sampleRateShading = true,
543 .dualSrcBlend = true,
544 .logicOp = true,
545 .multiDrawIndirect = true,
546 .drawIndirectFirstInstance = true,
547 .depthClamp = true,
548 .depthBiasClamp = true,
549 .fillModeNonSolid = true,
550 .depthBounds = true,
551 .wideLines = true,
552 .largePoints = true,
553 .alphaToOne = true,
554 .multiViewport = true,
555 .samplerAnisotropy = true,
556 .textureCompressionETC2 = false,
557 .textureCompressionASTC_LDR = false,
558 .textureCompressionBC = true,
559 .occlusionQueryPrecise = true,
560 .pipelineStatisticsQuery = true,
561 .vertexPipelineStoresAndAtomics = true,
562 .fragmentStoresAndAtomics = true,
563 .shaderTessellationAndGeometryPointSize = true,
564 .shaderImageGatherExtended = true,
565 .shaderStorageImageExtendedFormats = true,
566 .shaderStorageImageMultisample = false,
567 .shaderUniformBufferArrayDynamicIndexing = true,
568 .shaderSampledImageArrayDynamicIndexing = true,
569 .shaderStorageBufferArrayDynamicIndexing = true,
570 .shaderStorageImageArrayDynamicIndexing = true,
571 .shaderStorageImageReadWithoutFormat = true,
572 .shaderStorageImageWriteWithoutFormat = true,
573 .shaderClipDistance = true,
574 .shaderCullDistance = true,
575 .shaderFloat64 = true,
576 .shaderInt64 = true,
577 .shaderInt16 = false,
578 .sparseBinding = true,
579 .variableMultisampleRate = true,
580 .inheritedQueries = true,
581 };
582 }
583
584 void radv_GetPhysicalDeviceFeatures2KHR(
585 VkPhysicalDevice physicalDevice,
586 VkPhysicalDeviceFeatures2KHR *pFeatures)
587 {
588 vk_foreach_struct(ext, pFeatures->pNext) {
589 switch (ext->sType) {
590 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VARIABLE_POINTER_FEATURES_KHR: {
591 VkPhysicalDeviceVariablePointerFeaturesKHR *features = (void *)ext;
592 features->variablePointersStorageBuffer = true;
593 features->variablePointers = false;
594 break;
595 }
596 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_MULTIVIEW_FEATURES_KHX: {
597 VkPhysicalDeviceMultiviewFeaturesKHX *features = (VkPhysicalDeviceMultiviewFeaturesKHX*)ext;
598 features->multiview = true;
599 features->multiviewGeometryShader = true;
600 features->multiviewTessellationShader = true;
601 break;
602 }
603 default:
604 break;
605 }
606 }
607 return radv_GetPhysicalDeviceFeatures(physicalDevice, &pFeatures->features);
608 }
609
610 void radv_GetPhysicalDeviceProperties(
611 VkPhysicalDevice physicalDevice,
612 VkPhysicalDeviceProperties* pProperties)
613 {
614 RADV_FROM_HANDLE(radv_physical_device, pdevice, physicalDevice);
615 VkSampleCountFlags sample_counts = 0xf;
616
617 /* make sure that the entire descriptor set is addressable with a signed
618 * 32-bit int. So the sum of all limits scaled by descriptor size has to
619 * be at most 2 GiB. the combined image & samples object count as one of
620 * both. This limit is for the pipeline layout, not for the set layout, but
621 * there is no set limit, so we just set a pipeline limit. I don't think
622 * any app is going to hit this soon. */
623 size_t max_descriptor_set_size = ((1ull << 31) - 16 * MAX_DYNAMIC_BUFFERS) /
624 (32 /* uniform buffer, 32 due to potential space wasted on alignement */ +
625 32 /* storage buffer, 32 due to potential space wasted on alignement */ +
626 32 /* sampler, largest when combined with image */ +
627 64 /* sampled image */ +
628 64 /* storage image */);
629
630 VkPhysicalDeviceLimits limits = {
631 .maxImageDimension1D = (1 << 14),
632 .maxImageDimension2D = (1 << 14),
633 .maxImageDimension3D = (1 << 11),
634 .maxImageDimensionCube = (1 << 14),
635 .maxImageArrayLayers = (1 << 11),
636 .maxTexelBufferElements = 128 * 1024 * 1024,
637 .maxUniformBufferRange = UINT32_MAX,
638 .maxStorageBufferRange = UINT32_MAX,
639 .maxPushConstantsSize = MAX_PUSH_CONSTANTS_SIZE,
640 .maxMemoryAllocationCount = UINT32_MAX,
641 .maxSamplerAllocationCount = 64 * 1024,
642 .bufferImageGranularity = 64, /* A cache line */
643 .sparseAddressSpaceSize = 0xffffffffu, /* buffer max size */
644 .maxBoundDescriptorSets = MAX_SETS,
645 .maxPerStageDescriptorSamplers = max_descriptor_set_size,
646 .maxPerStageDescriptorUniformBuffers = max_descriptor_set_size,
647 .maxPerStageDescriptorStorageBuffers = max_descriptor_set_size,
648 .maxPerStageDescriptorSampledImages = max_descriptor_set_size,
649 .maxPerStageDescriptorStorageImages = max_descriptor_set_size,
650 .maxPerStageDescriptorInputAttachments = max_descriptor_set_size,
651 .maxPerStageResources = max_descriptor_set_size,
652 .maxDescriptorSetSamplers = max_descriptor_set_size,
653 .maxDescriptorSetUniformBuffers = max_descriptor_set_size,
654 .maxDescriptorSetUniformBuffersDynamic = MAX_DYNAMIC_BUFFERS / 2,
655 .maxDescriptorSetStorageBuffers = max_descriptor_set_size,
656 .maxDescriptorSetStorageBuffersDynamic = MAX_DYNAMIC_BUFFERS / 2,
657 .maxDescriptorSetSampledImages = max_descriptor_set_size,
658 .maxDescriptorSetStorageImages = max_descriptor_set_size,
659 .maxDescriptorSetInputAttachments = max_descriptor_set_size,
660 .maxVertexInputAttributes = 32,
661 .maxVertexInputBindings = 32,
662 .maxVertexInputAttributeOffset = 2047,
663 .maxVertexInputBindingStride = 2048,
664 .maxVertexOutputComponents = 128,
665 .maxTessellationGenerationLevel = 64,
666 .maxTessellationPatchSize = 32,
667 .maxTessellationControlPerVertexInputComponents = 128,
668 .maxTessellationControlPerVertexOutputComponents = 128,
669 .maxTessellationControlPerPatchOutputComponents = 120,
670 .maxTessellationControlTotalOutputComponents = 4096,
671 .maxTessellationEvaluationInputComponents = 128,
672 .maxTessellationEvaluationOutputComponents = 128,
673 .maxGeometryShaderInvocations = 127,
674 .maxGeometryInputComponents = 64,
675 .maxGeometryOutputComponents = 128,
676 .maxGeometryOutputVertices = 256,
677 .maxGeometryTotalOutputComponents = 1024,
678 .maxFragmentInputComponents = 128,
679 .maxFragmentOutputAttachments = 8,
680 .maxFragmentDualSrcAttachments = 1,
681 .maxFragmentCombinedOutputResources = 8,
682 .maxComputeSharedMemorySize = 32768,
683 .maxComputeWorkGroupCount = { 65535, 65535, 65535 },
684 .maxComputeWorkGroupInvocations = 2048,
685 .maxComputeWorkGroupSize = {
686 2048,
687 2048,
688 2048
689 },
690 .subPixelPrecisionBits = 4 /* FIXME */,
691 .subTexelPrecisionBits = 4 /* FIXME */,
692 .mipmapPrecisionBits = 4 /* FIXME */,
693 .maxDrawIndexedIndexValue = UINT32_MAX,
694 .maxDrawIndirectCount = UINT32_MAX,
695 .maxSamplerLodBias = 16,
696 .maxSamplerAnisotropy = 16,
697 .maxViewports = MAX_VIEWPORTS,
698 .maxViewportDimensions = { (1 << 14), (1 << 14) },
699 .viewportBoundsRange = { INT16_MIN, INT16_MAX },
700 .viewportSubPixelBits = 13, /* We take a float? */
701 .minMemoryMapAlignment = 4096, /* A page */
702 .minTexelBufferOffsetAlignment = 1,
703 .minUniformBufferOffsetAlignment = 4,
704 .minStorageBufferOffsetAlignment = 4,
705 .minTexelOffset = -32,
706 .maxTexelOffset = 31,
707 .minTexelGatherOffset = -32,
708 .maxTexelGatherOffset = 31,
709 .minInterpolationOffset = -2,
710 .maxInterpolationOffset = 2,
711 .subPixelInterpolationOffsetBits = 8,
712 .maxFramebufferWidth = (1 << 14),
713 .maxFramebufferHeight = (1 << 14),
714 .maxFramebufferLayers = (1 << 10),
715 .framebufferColorSampleCounts = sample_counts,
716 .framebufferDepthSampleCounts = sample_counts,
717 .framebufferStencilSampleCounts = sample_counts,
718 .framebufferNoAttachmentsSampleCounts = sample_counts,
719 .maxColorAttachments = MAX_RTS,
720 .sampledImageColorSampleCounts = sample_counts,
721 .sampledImageIntegerSampleCounts = VK_SAMPLE_COUNT_1_BIT,
722 .sampledImageDepthSampleCounts = sample_counts,
723 .sampledImageStencilSampleCounts = sample_counts,
724 .storageImageSampleCounts = VK_SAMPLE_COUNT_1_BIT,
725 .maxSampleMaskWords = 1,
726 .timestampComputeAndGraphics = true,
727 .timestampPeriod = 1000000.0 / pdevice->rad_info.clock_crystal_freq,
728 .maxClipDistances = 8,
729 .maxCullDistances = 8,
730 .maxCombinedClipAndCullDistances = 8,
731 .discreteQueuePriorities = 1,
732 .pointSizeRange = { 0.125, 255.875 },
733 .lineWidthRange = { 0.0, 7.9921875 },
734 .pointSizeGranularity = (1.0 / 8.0),
735 .lineWidthGranularity = (1.0 / 128.0),
736 .strictLines = false, /* FINISHME */
737 .standardSampleLocations = true,
738 .optimalBufferCopyOffsetAlignment = 128,
739 .optimalBufferCopyRowPitchAlignment = 128,
740 .nonCoherentAtomSize = 64,
741 };
742
743 *pProperties = (VkPhysicalDeviceProperties) {
744 .apiVersion = radv_physical_device_api_version(pdevice),
745 .driverVersion = vk_get_driver_version(),
746 .vendorID = ATI_VENDOR_ID,
747 .deviceID = pdevice->rad_info.pci_id,
748 .deviceType = pdevice->rad_info.has_dedicated_vram ? VK_PHYSICAL_DEVICE_TYPE_DISCRETE_GPU : VK_PHYSICAL_DEVICE_TYPE_INTEGRATED_GPU,
749 .limits = limits,
750 .sparseProperties = {0},
751 };
752
753 strcpy(pProperties->deviceName, pdevice->name);
754 memcpy(pProperties->pipelineCacheUUID, pdevice->cache_uuid, VK_UUID_SIZE);
755 }
756
757 void radv_GetPhysicalDeviceProperties2KHR(
758 VkPhysicalDevice physicalDevice,
759 VkPhysicalDeviceProperties2KHR *pProperties)
760 {
761 RADV_FROM_HANDLE(radv_physical_device, pdevice, physicalDevice);
762 radv_GetPhysicalDeviceProperties(physicalDevice, &pProperties->properties);
763
764 vk_foreach_struct(ext, pProperties->pNext) {
765 switch (ext->sType) {
766 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PUSH_DESCRIPTOR_PROPERTIES_KHR: {
767 VkPhysicalDevicePushDescriptorPropertiesKHR *properties =
768 (VkPhysicalDevicePushDescriptorPropertiesKHR *) ext;
769 properties->maxPushDescriptors = MAX_PUSH_DESCRIPTORS;
770 break;
771 }
772 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_ID_PROPERTIES_KHR: {
773 VkPhysicalDeviceIDPropertiesKHR *properties = (VkPhysicalDeviceIDPropertiesKHR*)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_KHX: {
780 VkPhysicalDeviceMultiviewPropertiesKHX *properties = (VkPhysicalDeviceMultiviewPropertiesKHX*)ext;
781 properties->maxMultiviewViewCount = MAX_VIEWS;
782 properties->maxMultiviewInstanceIndex = INT_MAX;
783 break;
784 }
785 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_POINT_CLIPPING_PROPERTIES_KHR: {
786 VkPhysicalDevicePointClippingPropertiesKHR *properties =
787 (VkPhysicalDevicePointClippingPropertiesKHR*)ext;
788 properties->pointClippingBehavior = VK_POINT_CLIPPING_BEHAVIOR_ALL_CLIP_PLANES_KHR;
789 break;
790 }
791 default:
792 break;
793 }
794 }
795 }
796
797 static void radv_get_physical_device_queue_family_properties(
798 struct radv_physical_device* pdevice,
799 uint32_t* pCount,
800 VkQueueFamilyProperties** pQueueFamilyProperties)
801 {
802 int num_queue_families = 1;
803 int idx;
804 if (pdevice->rad_info.num_compute_rings > 0 &&
805 pdevice->rad_info.chip_class >= CIK &&
806 !(pdevice->instance->debug_flags & RADV_DEBUG_NO_COMPUTE_QUEUE))
807 num_queue_families++;
808
809 if (pQueueFamilyProperties == NULL) {
810 *pCount = num_queue_families;
811 return;
812 }
813
814 if (!*pCount)
815 return;
816
817 idx = 0;
818 if (*pCount >= 1) {
819 *pQueueFamilyProperties[idx] = (VkQueueFamilyProperties) {
820 .queueFlags = VK_QUEUE_GRAPHICS_BIT |
821 VK_QUEUE_COMPUTE_BIT |
822 VK_QUEUE_TRANSFER_BIT |
823 VK_QUEUE_SPARSE_BINDING_BIT,
824 .queueCount = 1,
825 .timestampValidBits = 64,
826 .minImageTransferGranularity = (VkExtent3D) { 1, 1, 1 },
827 };
828 idx++;
829 }
830
831 if (pdevice->rad_info.num_compute_rings > 0 &&
832 pdevice->rad_info.chip_class >= CIK &&
833 !(pdevice->instance->debug_flags & RADV_DEBUG_NO_COMPUTE_QUEUE)) {
834 if (*pCount > idx) {
835 *pQueueFamilyProperties[idx] = (VkQueueFamilyProperties) {
836 .queueFlags = VK_QUEUE_COMPUTE_BIT |
837 VK_QUEUE_TRANSFER_BIT |
838 VK_QUEUE_SPARSE_BINDING_BIT,
839 .queueCount = pdevice->rad_info.num_compute_rings,
840 .timestampValidBits = 64,
841 .minImageTransferGranularity = (VkExtent3D) { 1, 1, 1 },
842 };
843 idx++;
844 }
845 }
846 *pCount = idx;
847 }
848
849 void radv_GetPhysicalDeviceQueueFamilyProperties(
850 VkPhysicalDevice physicalDevice,
851 uint32_t* pCount,
852 VkQueueFamilyProperties* pQueueFamilyProperties)
853 {
854 RADV_FROM_HANDLE(radv_physical_device, pdevice, physicalDevice);
855 if (!pQueueFamilyProperties) {
856 return radv_get_physical_device_queue_family_properties(pdevice, pCount, NULL);
857 return;
858 }
859 VkQueueFamilyProperties *properties[] = {
860 pQueueFamilyProperties + 0,
861 pQueueFamilyProperties + 1,
862 pQueueFamilyProperties + 2,
863 };
864 radv_get_physical_device_queue_family_properties(pdevice, pCount, properties);
865 assert(*pCount <= 3);
866 }
867
868 void radv_GetPhysicalDeviceQueueFamilyProperties2KHR(
869 VkPhysicalDevice physicalDevice,
870 uint32_t* pCount,
871 VkQueueFamilyProperties2KHR *pQueueFamilyProperties)
872 {
873 RADV_FROM_HANDLE(radv_physical_device, pdevice, physicalDevice);
874 if (!pQueueFamilyProperties) {
875 return radv_get_physical_device_queue_family_properties(pdevice, pCount, NULL);
876 return;
877 }
878 VkQueueFamilyProperties *properties[] = {
879 &pQueueFamilyProperties[0].queueFamilyProperties,
880 &pQueueFamilyProperties[1].queueFamilyProperties,
881 &pQueueFamilyProperties[2].queueFamilyProperties,
882 };
883 radv_get_physical_device_queue_family_properties(pdevice, pCount, properties);
884 assert(*pCount <= 3);
885 }
886
887 void radv_GetPhysicalDeviceMemoryProperties(
888 VkPhysicalDevice physicalDevice,
889 VkPhysicalDeviceMemoryProperties *pMemoryProperties)
890 {
891 RADV_FROM_HANDLE(radv_physical_device, physical_device, physicalDevice);
892
893 *pMemoryProperties = physical_device->memory_properties;
894 }
895
896 void radv_GetPhysicalDeviceMemoryProperties2KHR(
897 VkPhysicalDevice physicalDevice,
898 VkPhysicalDeviceMemoryProperties2KHR *pMemoryProperties)
899 {
900 return radv_GetPhysicalDeviceMemoryProperties(physicalDevice,
901 &pMemoryProperties->memoryProperties);
902 }
903
904 static enum radeon_ctx_priority
905 radv_get_queue_global_priority(const VkDeviceQueueGlobalPriorityCreateInfoEXT *pObj)
906 {
907 /* Default to MEDIUM when a specific global priority isn't requested */
908 if (!pObj)
909 return RADEON_CTX_PRIORITY_MEDIUM;
910
911 switch(pObj->globalPriority) {
912 case VK_QUEUE_GLOBAL_PRIORITY_REALTIME_EXT:
913 return RADEON_CTX_PRIORITY_REALTIME;
914 case VK_QUEUE_GLOBAL_PRIORITY_HIGH_EXT:
915 return RADEON_CTX_PRIORITY_HIGH;
916 case VK_QUEUE_GLOBAL_PRIORITY_MEDIUM_EXT:
917 return RADEON_CTX_PRIORITY_MEDIUM;
918 case VK_QUEUE_GLOBAL_PRIORITY_LOW_EXT:
919 return RADEON_CTX_PRIORITY_LOW;
920 default:
921 unreachable("Illegal global priority value");
922 return RADEON_CTX_PRIORITY_INVALID;
923 }
924 }
925
926 static int
927 radv_queue_init(struct radv_device *device, struct radv_queue *queue,
928 uint32_t queue_family_index, int idx,
929 const VkDeviceQueueGlobalPriorityCreateInfoEXT *global_priority)
930 {
931 queue->_loader_data.loaderMagic = ICD_LOADER_MAGIC;
932 queue->device = device;
933 queue->queue_family_index = queue_family_index;
934 queue->queue_idx = idx;
935 queue->priority = radv_get_queue_global_priority(global_priority);
936
937 queue->hw_ctx = device->ws->ctx_create(device->ws, queue->priority);
938 if (!queue->hw_ctx)
939 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
940
941 return VK_SUCCESS;
942 }
943
944 static void
945 radv_queue_finish(struct radv_queue *queue)
946 {
947 if (queue->hw_ctx)
948 queue->device->ws->ctx_destroy(queue->hw_ctx);
949
950 if (queue->initial_full_flush_preamble_cs)
951 queue->device->ws->cs_destroy(queue->initial_full_flush_preamble_cs);
952 if (queue->initial_preamble_cs)
953 queue->device->ws->cs_destroy(queue->initial_preamble_cs);
954 if (queue->continue_preamble_cs)
955 queue->device->ws->cs_destroy(queue->continue_preamble_cs);
956 if (queue->descriptor_bo)
957 queue->device->ws->buffer_destroy(queue->descriptor_bo);
958 if (queue->scratch_bo)
959 queue->device->ws->buffer_destroy(queue->scratch_bo);
960 if (queue->esgs_ring_bo)
961 queue->device->ws->buffer_destroy(queue->esgs_ring_bo);
962 if (queue->gsvs_ring_bo)
963 queue->device->ws->buffer_destroy(queue->gsvs_ring_bo);
964 if (queue->tess_factor_ring_bo)
965 queue->device->ws->buffer_destroy(queue->tess_factor_ring_bo);
966 if (queue->tess_offchip_ring_bo)
967 queue->device->ws->buffer_destroy(queue->tess_offchip_ring_bo);
968 if (queue->compute_scratch_bo)
969 queue->device->ws->buffer_destroy(queue->compute_scratch_bo);
970 }
971
972 static void
973 radv_device_init_gs_info(struct radv_device *device)
974 {
975 switch (device->physical_device->rad_info.family) {
976 case CHIP_OLAND:
977 case CHIP_HAINAN:
978 case CHIP_KAVERI:
979 case CHIP_KABINI:
980 case CHIP_MULLINS:
981 case CHIP_ICELAND:
982 case CHIP_CARRIZO:
983 case CHIP_STONEY:
984 device->gs_table_depth = 16;
985 return;
986 case CHIP_TAHITI:
987 case CHIP_PITCAIRN:
988 case CHIP_VERDE:
989 case CHIP_BONAIRE:
990 case CHIP_HAWAII:
991 case CHIP_TONGA:
992 case CHIP_FIJI:
993 case CHIP_POLARIS10:
994 case CHIP_POLARIS11:
995 case CHIP_POLARIS12:
996 case CHIP_VEGA10:
997 case CHIP_RAVEN:
998 device->gs_table_depth = 32;
999 return;
1000 default:
1001 unreachable("unknown GPU");
1002 }
1003 }
1004
1005 VkResult radv_CreateDevice(
1006 VkPhysicalDevice physicalDevice,
1007 const VkDeviceCreateInfo* pCreateInfo,
1008 const VkAllocationCallbacks* pAllocator,
1009 VkDevice* pDevice)
1010 {
1011 RADV_FROM_HANDLE(radv_physical_device, physical_device, physicalDevice);
1012 VkResult result;
1013 struct radv_device *device;
1014
1015 bool keep_shader_info = false;
1016
1017 for (uint32_t i = 0; i < pCreateInfo->enabledExtensionCount; i++) {
1018 const char *ext_name = pCreateInfo->ppEnabledExtensionNames[i];
1019 if (!radv_physical_device_extension_supported(physical_device, ext_name))
1020 return vk_error(VK_ERROR_EXTENSION_NOT_PRESENT);
1021
1022 if (strcmp(ext_name, VK_AMD_SHADER_INFO_EXTENSION_NAME) == 0)
1023 keep_shader_info = true;
1024 }
1025
1026 /* Check enabled features */
1027 if (pCreateInfo->pEnabledFeatures) {
1028 VkPhysicalDeviceFeatures supported_features;
1029 radv_GetPhysicalDeviceFeatures(physicalDevice, &supported_features);
1030 VkBool32 *supported_feature = (VkBool32 *)&supported_features;
1031 VkBool32 *enabled_feature = (VkBool32 *)pCreateInfo->pEnabledFeatures;
1032 unsigned num_features = sizeof(VkPhysicalDeviceFeatures) / sizeof(VkBool32);
1033 for (uint32_t i = 0; i < num_features; i++) {
1034 if (enabled_feature[i] && !supported_feature[i])
1035 return vk_error(VK_ERROR_FEATURE_NOT_PRESENT);
1036 }
1037 }
1038
1039 device = vk_zalloc2(&physical_device->instance->alloc, pAllocator,
1040 sizeof(*device), 8,
1041 VK_SYSTEM_ALLOCATION_SCOPE_DEVICE);
1042 if (!device)
1043 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
1044
1045 device->_loader_data.loaderMagic = ICD_LOADER_MAGIC;
1046 device->instance = physical_device->instance;
1047 device->physical_device = physical_device;
1048
1049 device->ws = physical_device->ws;
1050 if (pAllocator)
1051 device->alloc = *pAllocator;
1052 else
1053 device->alloc = physical_device->instance->alloc;
1054
1055 mtx_init(&device->shader_slab_mutex, mtx_plain);
1056 list_inithead(&device->shader_slabs);
1057
1058 for (unsigned i = 0; i < pCreateInfo->queueCreateInfoCount; i++) {
1059 const VkDeviceQueueCreateInfo *queue_create = &pCreateInfo->pQueueCreateInfos[i];
1060 uint32_t qfi = queue_create->queueFamilyIndex;
1061 const VkDeviceQueueGlobalPriorityCreateInfoEXT *global_priority =
1062 vk_find_struct_const(queue_create->pNext, DEVICE_QUEUE_GLOBAL_PRIORITY_CREATE_INFO_EXT);
1063
1064 assert(!global_priority || device->physical_device->rad_info.has_ctx_priority);
1065
1066 device->queues[qfi] = vk_alloc(&device->alloc,
1067 queue_create->queueCount * sizeof(struct radv_queue), 8, VK_SYSTEM_ALLOCATION_SCOPE_DEVICE);
1068 if (!device->queues[qfi]) {
1069 result = VK_ERROR_OUT_OF_HOST_MEMORY;
1070 goto fail;
1071 }
1072
1073 memset(device->queues[qfi], 0, queue_create->queueCount * sizeof(struct radv_queue));
1074
1075 device->queue_count[qfi] = queue_create->queueCount;
1076
1077 for (unsigned q = 0; q < queue_create->queueCount; q++) {
1078 result = radv_queue_init(device, &device->queues[qfi][q], qfi, q, global_priority);
1079 if (result != VK_SUCCESS)
1080 goto fail;
1081 }
1082 }
1083
1084 device->pbb_allowed = device->physical_device->rad_info.chip_class >= GFX9 &&
1085 (device->instance->perftest_flags & RADV_PERFTEST_BINNING);
1086
1087 /* Disabled and not implemented for now. */
1088 device->dfsm_allowed = device->pbb_allowed && false;
1089
1090
1091 #if HAVE_LLVM < 0x0400
1092 device->llvm_supports_spill = false;
1093 #else
1094 device->llvm_supports_spill = true;
1095 #endif
1096
1097 /* The maximum number of scratch waves. Scratch space isn't divided
1098 * evenly between CUs. The number is only a function of the number of CUs.
1099 * We can decrease the constant to decrease the scratch buffer size.
1100 *
1101 * sctx->scratch_waves must be >= the maximum posible size of
1102 * 1 threadgroup, so that the hw doesn't hang from being unable
1103 * to start any.
1104 *
1105 * The recommended value is 4 per CU at most. Higher numbers don't
1106 * bring much benefit, but they still occupy chip resources (think
1107 * async compute). I've seen ~2% performance difference between 4 and 32.
1108 */
1109 uint32_t max_threads_per_block = 2048;
1110 device->scratch_waves = MAX2(32 * physical_device->rad_info.num_good_compute_units,
1111 max_threads_per_block / 64);
1112
1113 device->dispatch_initiator = S_00B800_COMPUTE_SHADER_EN(1) |
1114 S_00B800_FORCE_START_AT_000(1);
1115
1116 if (device->physical_device->rad_info.chip_class >= CIK) {
1117 /* If the KMD allows it (there is a KMD hw register for it),
1118 * allow launching waves out-of-order.
1119 */
1120 device->dispatch_initiator |= S_00B800_ORDER_MODE(1);
1121 }
1122
1123 radv_device_init_gs_info(device);
1124
1125 device->tess_offchip_block_dw_size =
1126 device->physical_device->rad_info.family == CHIP_HAWAII ? 4096 : 8192;
1127 device->has_distributed_tess =
1128 device->physical_device->rad_info.chip_class >= VI &&
1129 device->physical_device->rad_info.max_se >= 2;
1130
1131 if (getenv("RADV_TRACE_FILE")) {
1132 keep_shader_info = true;
1133
1134 if (!radv_init_trace(device))
1135 goto fail;
1136 }
1137
1138 device->keep_shader_info = keep_shader_info;
1139
1140 result = radv_device_init_meta(device);
1141 if (result != VK_SUCCESS)
1142 goto fail;
1143
1144 radv_device_init_msaa(device);
1145
1146 for (int family = 0; family < RADV_MAX_QUEUE_FAMILIES; ++family) {
1147 device->empty_cs[family] = device->ws->cs_create(device->ws, family);
1148 switch (family) {
1149 case RADV_QUEUE_GENERAL:
1150 radeon_emit(device->empty_cs[family], PKT3(PKT3_CONTEXT_CONTROL, 1, 0));
1151 radeon_emit(device->empty_cs[family], CONTEXT_CONTROL_LOAD_ENABLE(1));
1152 radeon_emit(device->empty_cs[family], CONTEXT_CONTROL_SHADOW_ENABLE(1));
1153 break;
1154 case RADV_QUEUE_COMPUTE:
1155 radeon_emit(device->empty_cs[family], PKT3(PKT3_NOP, 0, 0));
1156 radeon_emit(device->empty_cs[family], 0);
1157 break;
1158 }
1159 device->ws->cs_finalize(device->empty_cs[family]);
1160 }
1161
1162 if (device->physical_device->rad_info.chip_class >= CIK)
1163 cik_create_gfx_config(device);
1164
1165 VkPipelineCacheCreateInfo ci;
1166 ci.sType = VK_STRUCTURE_TYPE_PIPELINE_CACHE_CREATE_INFO;
1167 ci.pNext = NULL;
1168 ci.flags = 0;
1169 ci.pInitialData = NULL;
1170 ci.initialDataSize = 0;
1171 VkPipelineCache pc;
1172 result = radv_CreatePipelineCache(radv_device_to_handle(device),
1173 &ci, NULL, &pc);
1174 if (result != VK_SUCCESS)
1175 goto fail;
1176
1177 device->mem_cache = radv_pipeline_cache_from_handle(pc);
1178
1179 *pDevice = radv_device_to_handle(device);
1180 return VK_SUCCESS;
1181
1182 fail:
1183 if (device->trace_bo)
1184 device->ws->buffer_destroy(device->trace_bo);
1185
1186 if (device->gfx_init)
1187 device->ws->buffer_destroy(device->gfx_init);
1188
1189 for (unsigned i = 0; i < RADV_MAX_QUEUE_FAMILIES; i++) {
1190 for (unsigned q = 0; q < device->queue_count[i]; q++)
1191 radv_queue_finish(&device->queues[i][q]);
1192 if (device->queue_count[i])
1193 vk_free(&device->alloc, device->queues[i]);
1194 }
1195
1196 vk_free(&device->alloc, device);
1197 return result;
1198 }
1199
1200 void radv_DestroyDevice(
1201 VkDevice _device,
1202 const VkAllocationCallbacks* pAllocator)
1203 {
1204 RADV_FROM_HANDLE(radv_device, device, _device);
1205
1206 if (!device)
1207 return;
1208
1209 if (device->trace_bo)
1210 device->ws->buffer_destroy(device->trace_bo);
1211
1212 if (device->gfx_init)
1213 device->ws->buffer_destroy(device->gfx_init);
1214
1215 for (unsigned i = 0; i < RADV_MAX_QUEUE_FAMILIES; i++) {
1216 for (unsigned q = 0; q < device->queue_count[i]; q++)
1217 radv_queue_finish(&device->queues[i][q]);
1218 if (device->queue_count[i])
1219 vk_free(&device->alloc, device->queues[i]);
1220 if (device->empty_cs[i])
1221 device->ws->cs_destroy(device->empty_cs[i]);
1222 }
1223 radv_device_finish_meta(device);
1224
1225 VkPipelineCache pc = radv_pipeline_cache_to_handle(device->mem_cache);
1226 radv_DestroyPipelineCache(radv_device_to_handle(device), pc, NULL);
1227
1228 radv_destroy_shader_slabs(device);
1229
1230 vk_free(&device->alloc, device);
1231 }
1232
1233 VkResult radv_EnumerateInstanceLayerProperties(
1234 uint32_t* pPropertyCount,
1235 VkLayerProperties* pProperties)
1236 {
1237 if (pProperties == NULL) {
1238 *pPropertyCount = 0;
1239 return VK_SUCCESS;
1240 }
1241
1242 /* None supported at this time */
1243 return vk_error(VK_ERROR_LAYER_NOT_PRESENT);
1244 }
1245
1246 VkResult radv_EnumerateDeviceLayerProperties(
1247 VkPhysicalDevice physicalDevice,
1248 uint32_t* pPropertyCount,
1249 VkLayerProperties* pProperties)
1250 {
1251 if (pProperties == NULL) {
1252 *pPropertyCount = 0;
1253 return VK_SUCCESS;
1254 }
1255
1256 /* None supported at this time */
1257 return vk_error(VK_ERROR_LAYER_NOT_PRESENT);
1258 }
1259
1260 void radv_GetDeviceQueue(
1261 VkDevice _device,
1262 uint32_t queueFamilyIndex,
1263 uint32_t queueIndex,
1264 VkQueue* pQueue)
1265 {
1266 RADV_FROM_HANDLE(radv_device, device, _device);
1267
1268 *pQueue = radv_queue_to_handle(&device->queues[queueFamilyIndex][queueIndex]);
1269 }
1270
1271 static void
1272 fill_geom_tess_rings(struct radv_queue *queue,
1273 uint32_t *map,
1274 bool add_sample_positions,
1275 uint32_t esgs_ring_size,
1276 struct radeon_winsys_bo *esgs_ring_bo,
1277 uint32_t gsvs_ring_size,
1278 struct radeon_winsys_bo *gsvs_ring_bo,
1279 uint32_t tess_factor_ring_size,
1280 struct radeon_winsys_bo *tess_factor_ring_bo,
1281 uint32_t tess_offchip_ring_size,
1282 struct radeon_winsys_bo *tess_offchip_ring_bo)
1283 {
1284 uint64_t esgs_va = 0, gsvs_va = 0;
1285 uint64_t tess_factor_va = 0, tess_offchip_va = 0;
1286 uint32_t *desc = &map[4];
1287
1288 if (esgs_ring_bo)
1289 esgs_va = radv_buffer_get_va(esgs_ring_bo);
1290 if (gsvs_ring_bo)
1291 gsvs_va = radv_buffer_get_va(gsvs_ring_bo);
1292 if (tess_factor_ring_bo)
1293 tess_factor_va = radv_buffer_get_va(tess_factor_ring_bo);
1294 if (tess_offchip_ring_bo)
1295 tess_offchip_va = radv_buffer_get_va(tess_offchip_ring_bo);
1296
1297 /* stride 0, num records - size, add tid, swizzle, elsize4,
1298 index stride 64 */
1299 desc[0] = esgs_va;
1300 desc[1] = S_008F04_BASE_ADDRESS_HI(esgs_va >> 32) |
1301 S_008F04_STRIDE(0) |
1302 S_008F04_SWIZZLE_ENABLE(true);
1303 desc[2] = esgs_ring_size;
1304 desc[3] = S_008F0C_DST_SEL_X(V_008F0C_SQ_SEL_X) |
1305 S_008F0C_DST_SEL_Y(V_008F0C_SQ_SEL_Y) |
1306 S_008F0C_DST_SEL_Z(V_008F0C_SQ_SEL_Z) |
1307 S_008F0C_DST_SEL_W(V_008F0C_SQ_SEL_W) |
1308 S_008F0C_NUM_FORMAT(V_008F0C_BUF_NUM_FORMAT_FLOAT) |
1309 S_008F0C_DATA_FORMAT(V_008F0C_BUF_DATA_FORMAT_32) |
1310 S_008F0C_ELEMENT_SIZE(1) |
1311 S_008F0C_INDEX_STRIDE(3) |
1312 S_008F0C_ADD_TID_ENABLE(true);
1313
1314 desc += 4;
1315 /* GS entry for ES->GS ring */
1316 /* stride 0, num records - size, elsize0,
1317 index stride 0 */
1318 desc[0] = esgs_va;
1319 desc[1] = S_008F04_BASE_ADDRESS_HI(esgs_va >> 32)|
1320 S_008F04_STRIDE(0) |
1321 S_008F04_SWIZZLE_ENABLE(false);
1322 desc[2] = esgs_ring_size;
1323 desc[3] = S_008F0C_DST_SEL_X(V_008F0C_SQ_SEL_X) |
1324 S_008F0C_DST_SEL_Y(V_008F0C_SQ_SEL_Y) |
1325 S_008F0C_DST_SEL_Z(V_008F0C_SQ_SEL_Z) |
1326 S_008F0C_DST_SEL_W(V_008F0C_SQ_SEL_W) |
1327 S_008F0C_NUM_FORMAT(V_008F0C_BUF_NUM_FORMAT_FLOAT) |
1328 S_008F0C_DATA_FORMAT(V_008F0C_BUF_DATA_FORMAT_32) |
1329 S_008F0C_ELEMENT_SIZE(0) |
1330 S_008F0C_INDEX_STRIDE(0) |
1331 S_008F0C_ADD_TID_ENABLE(false);
1332
1333 desc += 4;
1334 /* VS entry for GS->VS ring */
1335 /* stride 0, num records - size, elsize0,
1336 index stride 0 */
1337 desc[0] = gsvs_va;
1338 desc[1] = S_008F04_BASE_ADDRESS_HI(gsvs_va >> 32)|
1339 S_008F04_STRIDE(0) |
1340 S_008F04_SWIZZLE_ENABLE(false);
1341 desc[2] = gsvs_ring_size;
1342 desc[3] = S_008F0C_DST_SEL_X(V_008F0C_SQ_SEL_X) |
1343 S_008F0C_DST_SEL_Y(V_008F0C_SQ_SEL_Y) |
1344 S_008F0C_DST_SEL_Z(V_008F0C_SQ_SEL_Z) |
1345 S_008F0C_DST_SEL_W(V_008F0C_SQ_SEL_W) |
1346 S_008F0C_NUM_FORMAT(V_008F0C_BUF_NUM_FORMAT_FLOAT) |
1347 S_008F0C_DATA_FORMAT(V_008F0C_BUF_DATA_FORMAT_32) |
1348 S_008F0C_ELEMENT_SIZE(0) |
1349 S_008F0C_INDEX_STRIDE(0) |
1350 S_008F0C_ADD_TID_ENABLE(false);
1351 desc += 4;
1352
1353 /* stride gsvs_itemsize, num records 64
1354 elsize 4, index stride 16 */
1355 /* shader will patch stride and desc[2] */
1356 desc[0] = gsvs_va;
1357 desc[1] = S_008F04_BASE_ADDRESS_HI(gsvs_va >> 32)|
1358 S_008F04_STRIDE(0) |
1359 S_008F04_SWIZZLE_ENABLE(true);
1360 desc[2] = 0;
1361 desc[3] = S_008F0C_DST_SEL_X(V_008F0C_SQ_SEL_X) |
1362 S_008F0C_DST_SEL_Y(V_008F0C_SQ_SEL_Y) |
1363 S_008F0C_DST_SEL_Z(V_008F0C_SQ_SEL_Z) |
1364 S_008F0C_DST_SEL_W(V_008F0C_SQ_SEL_W) |
1365 S_008F0C_NUM_FORMAT(V_008F0C_BUF_NUM_FORMAT_FLOAT) |
1366 S_008F0C_DATA_FORMAT(V_008F0C_BUF_DATA_FORMAT_32) |
1367 S_008F0C_ELEMENT_SIZE(1) |
1368 S_008F0C_INDEX_STRIDE(1) |
1369 S_008F0C_ADD_TID_ENABLE(true);
1370 desc += 4;
1371
1372 desc[0] = tess_factor_va;
1373 desc[1] = S_008F04_BASE_ADDRESS_HI(tess_factor_va >> 32) |
1374 S_008F04_STRIDE(0) |
1375 S_008F04_SWIZZLE_ENABLE(false);
1376 desc[2] = tess_factor_ring_size;
1377 desc[3] = S_008F0C_DST_SEL_X(V_008F0C_SQ_SEL_X) |
1378 S_008F0C_DST_SEL_Y(V_008F0C_SQ_SEL_Y) |
1379 S_008F0C_DST_SEL_Z(V_008F0C_SQ_SEL_Z) |
1380 S_008F0C_DST_SEL_W(V_008F0C_SQ_SEL_W) |
1381 S_008F0C_NUM_FORMAT(V_008F0C_BUF_NUM_FORMAT_FLOAT) |
1382 S_008F0C_DATA_FORMAT(V_008F0C_BUF_DATA_FORMAT_32) |
1383 S_008F0C_ELEMENT_SIZE(0) |
1384 S_008F0C_INDEX_STRIDE(0) |
1385 S_008F0C_ADD_TID_ENABLE(false);
1386 desc += 4;
1387
1388 desc[0] = tess_offchip_va;
1389 desc[1] = S_008F04_BASE_ADDRESS_HI(tess_offchip_va >> 32) |
1390 S_008F04_STRIDE(0) |
1391 S_008F04_SWIZZLE_ENABLE(false);
1392 desc[2] = tess_offchip_ring_size;
1393 desc[3] = S_008F0C_DST_SEL_X(V_008F0C_SQ_SEL_X) |
1394 S_008F0C_DST_SEL_Y(V_008F0C_SQ_SEL_Y) |
1395 S_008F0C_DST_SEL_Z(V_008F0C_SQ_SEL_Z) |
1396 S_008F0C_DST_SEL_W(V_008F0C_SQ_SEL_W) |
1397 S_008F0C_NUM_FORMAT(V_008F0C_BUF_NUM_FORMAT_FLOAT) |
1398 S_008F0C_DATA_FORMAT(V_008F0C_BUF_DATA_FORMAT_32) |
1399 S_008F0C_ELEMENT_SIZE(0) |
1400 S_008F0C_INDEX_STRIDE(0) |
1401 S_008F0C_ADD_TID_ENABLE(false);
1402 desc += 4;
1403
1404 /* add sample positions after all rings */
1405 memcpy(desc, queue->device->sample_locations_1x, 8);
1406 desc += 2;
1407 memcpy(desc, queue->device->sample_locations_2x, 16);
1408 desc += 4;
1409 memcpy(desc, queue->device->sample_locations_4x, 32);
1410 desc += 8;
1411 memcpy(desc, queue->device->sample_locations_8x, 64);
1412 desc += 16;
1413 memcpy(desc, queue->device->sample_locations_16x, 128);
1414 }
1415
1416 static unsigned
1417 radv_get_hs_offchip_param(struct radv_device *device, uint32_t *max_offchip_buffers_p)
1418 {
1419 bool double_offchip_buffers = device->physical_device->rad_info.chip_class >= CIK &&
1420 device->physical_device->rad_info.family != CHIP_CARRIZO &&
1421 device->physical_device->rad_info.family != CHIP_STONEY;
1422 unsigned max_offchip_buffers_per_se = double_offchip_buffers ? 128 : 64;
1423 unsigned max_offchip_buffers = max_offchip_buffers_per_se *
1424 device->physical_device->rad_info.max_se;
1425 unsigned offchip_granularity;
1426 unsigned hs_offchip_param;
1427 switch (device->tess_offchip_block_dw_size) {
1428 default:
1429 assert(0);
1430 /* fall through */
1431 case 8192:
1432 offchip_granularity = V_03093C_X_8K_DWORDS;
1433 break;
1434 case 4096:
1435 offchip_granularity = V_03093C_X_4K_DWORDS;
1436 break;
1437 }
1438
1439 switch (device->physical_device->rad_info.chip_class) {
1440 case SI:
1441 max_offchip_buffers = MIN2(max_offchip_buffers, 126);
1442 break;
1443 case CIK:
1444 case VI:
1445 case GFX9:
1446 default:
1447 max_offchip_buffers = MIN2(max_offchip_buffers, 508);
1448 break;
1449 }
1450
1451 *max_offchip_buffers_p = max_offchip_buffers;
1452 if (device->physical_device->rad_info.chip_class >= CIK) {
1453 if (device->physical_device->rad_info.chip_class >= VI)
1454 --max_offchip_buffers;
1455 hs_offchip_param =
1456 S_03093C_OFFCHIP_BUFFERING(max_offchip_buffers) |
1457 S_03093C_OFFCHIP_GRANULARITY(offchip_granularity);
1458 } else {
1459 hs_offchip_param =
1460 S_0089B0_OFFCHIP_BUFFERING(max_offchip_buffers);
1461 }
1462 return hs_offchip_param;
1463 }
1464
1465 static VkResult
1466 radv_get_preamble_cs(struct radv_queue *queue,
1467 uint32_t scratch_size,
1468 uint32_t compute_scratch_size,
1469 uint32_t esgs_ring_size,
1470 uint32_t gsvs_ring_size,
1471 bool needs_tess_rings,
1472 bool needs_sample_positions,
1473 struct radeon_winsys_cs **initial_full_flush_preamble_cs,
1474 struct radeon_winsys_cs **initial_preamble_cs,
1475 struct radeon_winsys_cs **continue_preamble_cs)
1476 {
1477 struct radeon_winsys_bo *scratch_bo = NULL;
1478 struct radeon_winsys_bo *descriptor_bo = NULL;
1479 struct radeon_winsys_bo *compute_scratch_bo = NULL;
1480 struct radeon_winsys_bo *esgs_ring_bo = NULL;
1481 struct radeon_winsys_bo *gsvs_ring_bo = NULL;
1482 struct radeon_winsys_bo *tess_factor_ring_bo = NULL;
1483 struct radeon_winsys_bo *tess_offchip_ring_bo = NULL;
1484 struct radeon_winsys_cs *dest_cs[3] = {0};
1485 bool add_tess_rings = false, add_sample_positions = false;
1486 unsigned tess_factor_ring_size = 0, tess_offchip_ring_size = 0;
1487 unsigned max_offchip_buffers;
1488 unsigned hs_offchip_param = 0;
1489 uint32_t ring_bo_flags = RADEON_FLAG_NO_CPU_ACCESS | RADEON_FLAG_NO_INTERPROCESS_SHARING;
1490 if (!queue->has_tess_rings) {
1491 if (needs_tess_rings)
1492 add_tess_rings = true;
1493 }
1494 if (!queue->has_sample_positions) {
1495 if (needs_sample_positions)
1496 add_sample_positions = true;
1497 }
1498 tess_factor_ring_size = 32768 * queue->device->physical_device->rad_info.max_se;
1499 hs_offchip_param = radv_get_hs_offchip_param(queue->device,
1500 &max_offchip_buffers);
1501 tess_offchip_ring_size = max_offchip_buffers *
1502 queue->device->tess_offchip_block_dw_size * 4;
1503
1504 if (scratch_size <= queue->scratch_size &&
1505 compute_scratch_size <= queue->compute_scratch_size &&
1506 esgs_ring_size <= queue->esgs_ring_size &&
1507 gsvs_ring_size <= queue->gsvs_ring_size &&
1508 !add_tess_rings && !add_sample_positions &&
1509 queue->initial_preamble_cs) {
1510 *initial_full_flush_preamble_cs = queue->initial_full_flush_preamble_cs;
1511 *initial_preamble_cs = queue->initial_preamble_cs;
1512 *continue_preamble_cs = queue->continue_preamble_cs;
1513 if (!scratch_size && !compute_scratch_size && !esgs_ring_size && !gsvs_ring_size)
1514 *continue_preamble_cs = NULL;
1515 return VK_SUCCESS;
1516 }
1517
1518 if (scratch_size > queue->scratch_size) {
1519 scratch_bo = queue->device->ws->buffer_create(queue->device->ws,
1520 scratch_size,
1521 4096,
1522 RADEON_DOMAIN_VRAM,
1523 ring_bo_flags);
1524 if (!scratch_bo)
1525 goto fail;
1526 } else
1527 scratch_bo = queue->scratch_bo;
1528
1529 if (compute_scratch_size > queue->compute_scratch_size) {
1530 compute_scratch_bo = queue->device->ws->buffer_create(queue->device->ws,
1531 compute_scratch_size,
1532 4096,
1533 RADEON_DOMAIN_VRAM,
1534 ring_bo_flags);
1535 if (!compute_scratch_bo)
1536 goto fail;
1537
1538 } else
1539 compute_scratch_bo = queue->compute_scratch_bo;
1540
1541 if (esgs_ring_size > queue->esgs_ring_size) {
1542 esgs_ring_bo = queue->device->ws->buffer_create(queue->device->ws,
1543 esgs_ring_size,
1544 4096,
1545 RADEON_DOMAIN_VRAM,
1546 ring_bo_flags);
1547 if (!esgs_ring_bo)
1548 goto fail;
1549 } else {
1550 esgs_ring_bo = queue->esgs_ring_bo;
1551 esgs_ring_size = queue->esgs_ring_size;
1552 }
1553
1554 if (gsvs_ring_size > queue->gsvs_ring_size) {
1555 gsvs_ring_bo = queue->device->ws->buffer_create(queue->device->ws,
1556 gsvs_ring_size,
1557 4096,
1558 RADEON_DOMAIN_VRAM,
1559 ring_bo_flags);
1560 if (!gsvs_ring_bo)
1561 goto fail;
1562 } else {
1563 gsvs_ring_bo = queue->gsvs_ring_bo;
1564 gsvs_ring_size = queue->gsvs_ring_size;
1565 }
1566
1567 if (add_tess_rings) {
1568 tess_factor_ring_bo = queue->device->ws->buffer_create(queue->device->ws,
1569 tess_factor_ring_size,
1570 256,
1571 RADEON_DOMAIN_VRAM,
1572 ring_bo_flags);
1573 if (!tess_factor_ring_bo)
1574 goto fail;
1575 tess_offchip_ring_bo = queue->device->ws->buffer_create(queue->device->ws,
1576 tess_offchip_ring_size,
1577 256,
1578 RADEON_DOMAIN_VRAM,
1579 ring_bo_flags);
1580 if (!tess_offchip_ring_bo)
1581 goto fail;
1582 } else {
1583 tess_factor_ring_bo = queue->tess_factor_ring_bo;
1584 tess_offchip_ring_bo = queue->tess_offchip_ring_bo;
1585 }
1586
1587 if (scratch_bo != queue->scratch_bo ||
1588 esgs_ring_bo != queue->esgs_ring_bo ||
1589 gsvs_ring_bo != queue->gsvs_ring_bo ||
1590 tess_factor_ring_bo != queue->tess_factor_ring_bo ||
1591 tess_offchip_ring_bo != queue->tess_offchip_ring_bo || add_sample_positions) {
1592 uint32_t size = 0;
1593 if (gsvs_ring_bo || esgs_ring_bo ||
1594 tess_factor_ring_bo || tess_offchip_ring_bo || add_sample_positions) {
1595 size = 112; /* 2 dword + 2 padding + 4 dword * 6 */
1596 if (add_sample_positions)
1597 size += 256; /* 32+16+8+4+2+1 samples * 4 * 2 = 248 bytes. */
1598 }
1599 else if (scratch_bo)
1600 size = 8; /* 2 dword */
1601
1602 descriptor_bo = queue->device->ws->buffer_create(queue->device->ws,
1603 size,
1604 4096,
1605 RADEON_DOMAIN_VRAM,
1606 RADEON_FLAG_CPU_ACCESS |
1607 RADEON_FLAG_NO_INTERPROCESS_SHARING |
1608 RADEON_FLAG_READ_ONLY);
1609 if (!descriptor_bo)
1610 goto fail;
1611 } else
1612 descriptor_bo = queue->descriptor_bo;
1613
1614 for(int i = 0; i < 3; ++i) {
1615 struct radeon_winsys_cs *cs = NULL;
1616 cs = queue->device->ws->cs_create(queue->device->ws,
1617 queue->queue_family_index ? RING_COMPUTE : RING_GFX);
1618 if (!cs)
1619 goto fail;
1620
1621 dest_cs[i] = cs;
1622
1623 if (scratch_bo)
1624 radv_cs_add_buffer(queue->device->ws, cs, scratch_bo, 8);
1625
1626 if (esgs_ring_bo)
1627 radv_cs_add_buffer(queue->device->ws, cs, esgs_ring_bo, 8);
1628
1629 if (gsvs_ring_bo)
1630 radv_cs_add_buffer(queue->device->ws, cs, gsvs_ring_bo, 8);
1631
1632 if (tess_factor_ring_bo)
1633 radv_cs_add_buffer(queue->device->ws, cs, tess_factor_ring_bo, 8);
1634
1635 if (tess_offchip_ring_bo)
1636 radv_cs_add_buffer(queue->device->ws, cs, tess_offchip_ring_bo, 8);
1637
1638 if (descriptor_bo)
1639 radv_cs_add_buffer(queue->device->ws, cs, descriptor_bo, 8);
1640
1641 if (descriptor_bo != queue->descriptor_bo) {
1642 uint32_t *map = (uint32_t*)queue->device->ws->buffer_map(descriptor_bo);
1643
1644 if (scratch_bo) {
1645 uint64_t scratch_va = radv_buffer_get_va(scratch_bo);
1646 uint32_t rsrc1 = S_008F04_BASE_ADDRESS_HI(scratch_va >> 32) |
1647 S_008F04_SWIZZLE_ENABLE(1);
1648 map[0] = scratch_va;
1649 map[1] = rsrc1;
1650 }
1651
1652 if (esgs_ring_bo || gsvs_ring_bo || tess_factor_ring_bo || tess_offchip_ring_bo ||
1653 add_sample_positions)
1654 fill_geom_tess_rings(queue, map, add_sample_positions,
1655 esgs_ring_size, esgs_ring_bo,
1656 gsvs_ring_size, gsvs_ring_bo,
1657 tess_factor_ring_size, tess_factor_ring_bo,
1658 tess_offchip_ring_size, tess_offchip_ring_bo);
1659
1660 queue->device->ws->buffer_unmap(descriptor_bo);
1661 }
1662
1663 if (esgs_ring_bo || gsvs_ring_bo || tess_factor_ring_bo || tess_offchip_ring_bo) {
1664 radeon_emit(cs, PKT3(PKT3_EVENT_WRITE, 0, 0));
1665 radeon_emit(cs, EVENT_TYPE(V_028A90_VS_PARTIAL_FLUSH) | EVENT_INDEX(4));
1666 radeon_emit(cs, PKT3(PKT3_EVENT_WRITE, 0, 0));
1667 radeon_emit(cs, EVENT_TYPE(V_028A90_VGT_FLUSH) | EVENT_INDEX(0));
1668 }
1669
1670 if (esgs_ring_bo || gsvs_ring_bo) {
1671 if (queue->device->physical_device->rad_info.chip_class >= CIK) {
1672 radeon_set_uconfig_reg_seq(cs, R_030900_VGT_ESGS_RING_SIZE, 2);
1673 radeon_emit(cs, esgs_ring_size >> 8);
1674 radeon_emit(cs, gsvs_ring_size >> 8);
1675 } else {
1676 radeon_set_config_reg_seq(cs, R_0088C8_VGT_ESGS_RING_SIZE, 2);
1677 radeon_emit(cs, esgs_ring_size >> 8);
1678 radeon_emit(cs, gsvs_ring_size >> 8);
1679 }
1680 }
1681
1682 if (tess_factor_ring_bo) {
1683 uint64_t tf_va = radv_buffer_get_va(tess_factor_ring_bo);
1684 if (queue->device->physical_device->rad_info.chip_class >= CIK) {
1685 radeon_set_uconfig_reg(cs, R_030938_VGT_TF_RING_SIZE,
1686 S_030938_SIZE(tess_factor_ring_size / 4));
1687 radeon_set_uconfig_reg(cs, R_030940_VGT_TF_MEMORY_BASE,
1688 tf_va >> 8);
1689 if (queue->device->physical_device->rad_info.chip_class >= GFX9) {
1690 radeon_set_uconfig_reg(cs, R_030944_VGT_TF_MEMORY_BASE_HI,
1691 tf_va >> 40);
1692 }
1693 radeon_set_uconfig_reg(cs, R_03093C_VGT_HS_OFFCHIP_PARAM, hs_offchip_param);
1694 } else {
1695 radeon_set_config_reg(cs, R_008988_VGT_TF_RING_SIZE,
1696 S_008988_SIZE(tess_factor_ring_size / 4));
1697 radeon_set_config_reg(cs, R_0089B8_VGT_TF_MEMORY_BASE,
1698 tf_va >> 8);
1699 radeon_set_config_reg(cs, R_0089B0_VGT_HS_OFFCHIP_PARAM,
1700 hs_offchip_param);
1701 }
1702 }
1703
1704 if (descriptor_bo) {
1705 uint64_t va = radv_buffer_get_va(descriptor_bo);
1706 if (queue->device->physical_device->rad_info.chip_class >= GFX9) {
1707 uint32_t regs[] = {R_00B030_SPI_SHADER_USER_DATA_PS_0,
1708 R_00B130_SPI_SHADER_USER_DATA_VS_0,
1709 R_00B208_SPI_SHADER_USER_DATA_ADDR_LO_GS,
1710 R_00B408_SPI_SHADER_USER_DATA_ADDR_LO_HS};
1711
1712 for (int i = 0; i < ARRAY_SIZE(regs); ++i) {
1713 radeon_set_sh_reg_seq(cs, regs[i], 2);
1714 radeon_emit(cs, va);
1715 radeon_emit(cs, va >> 32);
1716 }
1717 } else {
1718 uint32_t regs[] = {R_00B030_SPI_SHADER_USER_DATA_PS_0,
1719 R_00B130_SPI_SHADER_USER_DATA_VS_0,
1720 R_00B230_SPI_SHADER_USER_DATA_GS_0,
1721 R_00B330_SPI_SHADER_USER_DATA_ES_0,
1722 R_00B430_SPI_SHADER_USER_DATA_HS_0,
1723 R_00B530_SPI_SHADER_USER_DATA_LS_0};
1724
1725 for (int i = 0; i < ARRAY_SIZE(regs); ++i) {
1726 radeon_set_sh_reg_seq(cs, regs[i], 2);
1727 radeon_emit(cs, va);
1728 radeon_emit(cs, va >> 32);
1729 }
1730 }
1731 }
1732
1733 if (compute_scratch_bo) {
1734 uint64_t scratch_va = radv_buffer_get_va(compute_scratch_bo);
1735 uint32_t rsrc1 = S_008F04_BASE_ADDRESS_HI(scratch_va >> 32) |
1736 S_008F04_SWIZZLE_ENABLE(1);
1737
1738 radv_cs_add_buffer(queue->device->ws, cs, compute_scratch_bo, 8);
1739
1740 radeon_set_sh_reg_seq(cs, R_00B900_COMPUTE_USER_DATA_0, 2);
1741 radeon_emit(cs, scratch_va);
1742 radeon_emit(cs, rsrc1);
1743 }
1744
1745 if (i == 0) {
1746 si_cs_emit_cache_flush(cs,
1747 false,
1748 queue->device->physical_device->rad_info.chip_class,
1749 NULL, 0,
1750 queue->queue_family_index == RING_COMPUTE &&
1751 queue->device->physical_device->rad_info.chip_class >= CIK,
1752 (queue->queue_family_index == RADV_QUEUE_COMPUTE ? RADV_CMD_FLAG_CS_PARTIAL_FLUSH : (RADV_CMD_FLAG_CS_PARTIAL_FLUSH | RADV_CMD_FLAG_PS_PARTIAL_FLUSH)) |
1753 RADV_CMD_FLAG_INV_ICACHE |
1754 RADV_CMD_FLAG_INV_SMEM_L1 |
1755 RADV_CMD_FLAG_INV_VMEM_L1 |
1756 RADV_CMD_FLAG_INV_GLOBAL_L2);
1757 } else if (i == 1) {
1758 si_cs_emit_cache_flush(cs,
1759 false,
1760 queue->device->physical_device->rad_info.chip_class,
1761 NULL, 0,
1762 queue->queue_family_index == RING_COMPUTE &&
1763 queue->device->physical_device->rad_info.chip_class >= CIK,
1764 RADV_CMD_FLAG_INV_ICACHE |
1765 RADV_CMD_FLAG_INV_SMEM_L1 |
1766 RADV_CMD_FLAG_INV_VMEM_L1 |
1767 RADV_CMD_FLAG_INV_GLOBAL_L2);
1768 }
1769
1770 if (!queue->device->ws->cs_finalize(cs))
1771 goto fail;
1772 }
1773
1774 if (queue->initial_full_flush_preamble_cs)
1775 queue->device->ws->cs_destroy(queue->initial_full_flush_preamble_cs);
1776
1777 if (queue->initial_preamble_cs)
1778 queue->device->ws->cs_destroy(queue->initial_preamble_cs);
1779
1780 if (queue->continue_preamble_cs)
1781 queue->device->ws->cs_destroy(queue->continue_preamble_cs);
1782
1783 queue->initial_full_flush_preamble_cs = dest_cs[0];
1784 queue->initial_preamble_cs = dest_cs[1];
1785 queue->continue_preamble_cs = dest_cs[2];
1786
1787 if (scratch_bo != queue->scratch_bo) {
1788 if (queue->scratch_bo)
1789 queue->device->ws->buffer_destroy(queue->scratch_bo);
1790 queue->scratch_bo = scratch_bo;
1791 queue->scratch_size = scratch_size;
1792 }
1793
1794 if (compute_scratch_bo != queue->compute_scratch_bo) {
1795 if (queue->compute_scratch_bo)
1796 queue->device->ws->buffer_destroy(queue->compute_scratch_bo);
1797 queue->compute_scratch_bo = compute_scratch_bo;
1798 queue->compute_scratch_size = compute_scratch_size;
1799 }
1800
1801 if (esgs_ring_bo != queue->esgs_ring_bo) {
1802 if (queue->esgs_ring_bo)
1803 queue->device->ws->buffer_destroy(queue->esgs_ring_bo);
1804 queue->esgs_ring_bo = esgs_ring_bo;
1805 queue->esgs_ring_size = esgs_ring_size;
1806 }
1807
1808 if (gsvs_ring_bo != queue->gsvs_ring_bo) {
1809 if (queue->gsvs_ring_bo)
1810 queue->device->ws->buffer_destroy(queue->gsvs_ring_bo);
1811 queue->gsvs_ring_bo = gsvs_ring_bo;
1812 queue->gsvs_ring_size = gsvs_ring_size;
1813 }
1814
1815 if (tess_factor_ring_bo != queue->tess_factor_ring_bo) {
1816 queue->tess_factor_ring_bo = tess_factor_ring_bo;
1817 }
1818
1819 if (tess_offchip_ring_bo != queue->tess_offchip_ring_bo) {
1820 queue->tess_offchip_ring_bo = tess_offchip_ring_bo;
1821 queue->has_tess_rings = true;
1822 }
1823
1824 if (descriptor_bo != queue->descriptor_bo) {
1825 if (queue->descriptor_bo)
1826 queue->device->ws->buffer_destroy(queue->descriptor_bo);
1827
1828 queue->descriptor_bo = descriptor_bo;
1829 }
1830
1831 if (add_sample_positions)
1832 queue->has_sample_positions = true;
1833
1834 *initial_full_flush_preamble_cs = queue->initial_full_flush_preamble_cs;
1835 *initial_preamble_cs = queue->initial_preamble_cs;
1836 *continue_preamble_cs = queue->continue_preamble_cs;
1837 if (!scratch_size && !compute_scratch_size && !esgs_ring_size && !gsvs_ring_size)
1838 *continue_preamble_cs = NULL;
1839 return VK_SUCCESS;
1840 fail:
1841 for (int i = 0; i < ARRAY_SIZE(dest_cs); ++i)
1842 if (dest_cs[i])
1843 queue->device->ws->cs_destroy(dest_cs[i]);
1844 if (descriptor_bo && descriptor_bo != queue->descriptor_bo)
1845 queue->device->ws->buffer_destroy(descriptor_bo);
1846 if (scratch_bo && scratch_bo != queue->scratch_bo)
1847 queue->device->ws->buffer_destroy(scratch_bo);
1848 if (compute_scratch_bo && compute_scratch_bo != queue->compute_scratch_bo)
1849 queue->device->ws->buffer_destroy(compute_scratch_bo);
1850 if (esgs_ring_bo && esgs_ring_bo != queue->esgs_ring_bo)
1851 queue->device->ws->buffer_destroy(esgs_ring_bo);
1852 if (gsvs_ring_bo && gsvs_ring_bo != queue->gsvs_ring_bo)
1853 queue->device->ws->buffer_destroy(gsvs_ring_bo);
1854 if (tess_factor_ring_bo && tess_factor_ring_bo != queue->tess_factor_ring_bo)
1855 queue->device->ws->buffer_destroy(tess_factor_ring_bo);
1856 if (tess_offchip_ring_bo && tess_offchip_ring_bo != queue->tess_offchip_ring_bo)
1857 queue->device->ws->buffer_destroy(tess_offchip_ring_bo);
1858 return vk_error(VK_ERROR_OUT_OF_DEVICE_MEMORY);
1859 }
1860
1861 static VkResult radv_alloc_sem_counts(struct radv_winsys_sem_counts *counts,
1862 int num_sems,
1863 const VkSemaphore *sems,
1864 VkFence _fence,
1865 bool reset_temp)
1866 {
1867 int syncobj_idx = 0, sem_idx = 0;
1868
1869 if (num_sems == 0 && _fence == VK_NULL_HANDLE)
1870 return VK_SUCCESS;
1871
1872 for (uint32_t i = 0; i < num_sems; i++) {
1873 RADV_FROM_HANDLE(radv_semaphore, sem, sems[i]);
1874
1875 if (sem->temp_syncobj || sem->syncobj)
1876 counts->syncobj_count++;
1877 else
1878 counts->sem_count++;
1879 }
1880
1881 if (_fence != VK_NULL_HANDLE) {
1882 RADV_FROM_HANDLE(radv_fence, fence, _fence);
1883 if (fence->temp_syncobj || fence->syncobj)
1884 counts->syncobj_count++;
1885 }
1886
1887 if (counts->syncobj_count) {
1888 counts->syncobj = (uint32_t *)malloc(sizeof(uint32_t) * counts->syncobj_count);
1889 if (!counts->syncobj)
1890 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
1891 }
1892
1893 if (counts->sem_count) {
1894 counts->sem = (struct radeon_winsys_sem **)malloc(sizeof(struct radeon_winsys_sem *) * counts->sem_count);
1895 if (!counts->sem) {
1896 free(counts->syncobj);
1897 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
1898 }
1899 }
1900
1901 for (uint32_t i = 0; i < num_sems; i++) {
1902 RADV_FROM_HANDLE(radv_semaphore, sem, sems[i]);
1903
1904 if (sem->temp_syncobj) {
1905 counts->syncobj[syncobj_idx++] = sem->temp_syncobj;
1906 }
1907 else if (sem->syncobj)
1908 counts->syncobj[syncobj_idx++] = sem->syncobj;
1909 else {
1910 assert(sem->sem);
1911 counts->sem[sem_idx++] = sem->sem;
1912 }
1913 }
1914
1915 if (_fence != VK_NULL_HANDLE) {
1916 RADV_FROM_HANDLE(radv_fence, fence, _fence);
1917 if (fence->temp_syncobj)
1918 counts->syncobj[syncobj_idx++] = fence->temp_syncobj;
1919 else if (fence->syncobj)
1920 counts->syncobj[syncobj_idx++] = fence->syncobj;
1921 }
1922
1923 return VK_SUCCESS;
1924 }
1925
1926 void radv_free_sem_info(struct radv_winsys_sem_info *sem_info)
1927 {
1928 free(sem_info->wait.syncobj);
1929 free(sem_info->wait.sem);
1930 free(sem_info->signal.syncobj);
1931 free(sem_info->signal.sem);
1932 }
1933
1934
1935 static void radv_free_temp_syncobjs(struct radv_device *device,
1936 int num_sems,
1937 const VkSemaphore *sems)
1938 {
1939 for (uint32_t i = 0; i < num_sems; i++) {
1940 RADV_FROM_HANDLE(radv_semaphore, sem, sems[i]);
1941
1942 if (sem->temp_syncobj) {
1943 device->ws->destroy_syncobj(device->ws, sem->temp_syncobj);
1944 sem->temp_syncobj = 0;
1945 }
1946 }
1947 }
1948
1949 VkResult radv_alloc_sem_info(struct radv_winsys_sem_info *sem_info,
1950 int num_wait_sems,
1951 const VkSemaphore *wait_sems,
1952 int num_signal_sems,
1953 const VkSemaphore *signal_sems,
1954 VkFence fence)
1955 {
1956 VkResult ret;
1957 memset(sem_info, 0, sizeof(*sem_info));
1958
1959 ret = radv_alloc_sem_counts(&sem_info->wait, num_wait_sems, wait_sems, VK_NULL_HANDLE, true);
1960 if (ret)
1961 return ret;
1962 ret = radv_alloc_sem_counts(&sem_info->signal, num_signal_sems, signal_sems, fence, false);
1963 if (ret)
1964 radv_free_sem_info(sem_info);
1965
1966 /* caller can override these */
1967 sem_info->cs_emit_wait = true;
1968 sem_info->cs_emit_signal = true;
1969 return ret;
1970 }
1971
1972 VkResult radv_QueueSubmit(
1973 VkQueue _queue,
1974 uint32_t submitCount,
1975 const VkSubmitInfo* pSubmits,
1976 VkFence _fence)
1977 {
1978 RADV_FROM_HANDLE(radv_queue, queue, _queue);
1979 RADV_FROM_HANDLE(radv_fence, fence, _fence);
1980 struct radeon_winsys_fence *base_fence = fence ? fence->fence : NULL;
1981 struct radeon_winsys_ctx *ctx = queue->hw_ctx;
1982 int ret;
1983 uint32_t max_cs_submission = queue->device->trace_bo ? 1 : UINT32_MAX;
1984 uint32_t scratch_size = 0;
1985 uint32_t compute_scratch_size = 0;
1986 uint32_t esgs_ring_size = 0, gsvs_ring_size = 0;
1987 struct radeon_winsys_cs *initial_preamble_cs = NULL, *initial_flush_preamble_cs = NULL, *continue_preamble_cs = NULL;
1988 VkResult result;
1989 bool fence_emitted = false;
1990 bool tess_rings_needed = false;
1991 bool sample_positions_needed = false;
1992
1993 /* Do this first so failing to allocate scratch buffers can't result in
1994 * partially executed submissions. */
1995 for (uint32_t i = 0; i < submitCount; i++) {
1996 for (uint32_t j = 0; j < pSubmits[i].commandBufferCount; j++) {
1997 RADV_FROM_HANDLE(radv_cmd_buffer, cmd_buffer,
1998 pSubmits[i].pCommandBuffers[j]);
1999
2000 scratch_size = MAX2(scratch_size, cmd_buffer->scratch_size_needed);
2001 compute_scratch_size = MAX2(compute_scratch_size,
2002 cmd_buffer->compute_scratch_size_needed);
2003 esgs_ring_size = MAX2(esgs_ring_size, cmd_buffer->esgs_ring_size_needed);
2004 gsvs_ring_size = MAX2(gsvs_ring_size, cmd_buffer->gsvs_ring_size_needed);
2005 tess_rings_needed |= cmd_buffer->tess_rings_needed;
2006 sample_positions_needed |= cmd_buffer->sample_positions_needed;
2007 }
2008 }
2009
2010 result = radv_get_preamble_cs(queue, scratch_size, compute_scratch_size,
2011 esgs_ring_size, gsvs_ring_size, tess_rings_needed,
2012 sample_positions_needed, &initial_flush_preamble_cs,
2013 &initial_preamble_cs, &continue_preamble_cs);
2014 if (result != VK_SUCCESS)
2015 return result;
2016
2017 for (uint32_t i = 0; i < submitCount; i++) {
2018 struct radeon_winsys_cs **cs_array;
2019 bool do_flush = !i || pSubmits[i].pWaitDstStageMask;
2020 bool can_patch = true;
2021 uint32_t advance;
2022 struct radv_winsys_sem_info sem_info;
2023
2024 result = radv_alloc_sem_info(&sem_info,
2025 pSubmits[i].waitSemaphoreCount,
2026 pSubmits[i].pWaitSemaphores,
2027 pSubmits[i].signalSemaphoreCount,
2028 pSubmits[i].pSignalSemaphores,
2029 _fence);
2030 if (result != VK_SUCCESS)
2031 return result;
2032
2033 if (!pSubmits[i].commandBufferCount) {
2034 if (pSubmits[i].waitSemaphoreCount || pSubmits[i].signalSemaphoreCount) {
2035 ret = queue->device->ws->cs_submit(ctx, queue->queue_idx,
2036 &queue->device->empty_cs[queue->queue_family_index],
2037 1, NULL, NULL,
2038 &sem_info,
2039 false, base_fence);
2040 if (ret) {
2041 radv_loge("failed to submit CS %d\n", i);
2042 abort();
2043 }
2044 fence_emitted = true;
2045 }
2046 radv_free_sem_info(&sem_info);
2047 continue;
2048 }
2049
2050 cs_array = malloc(sizeof(struct radeon_winsys_cs *) *
2051 (pSubmits[i].commandBufferCount));
2052
2053 for (uint32_t j = 0; j < pSubmits[i].commandBufferCount; j++) {
2054 RADV_FROM_HANDLE(radv_cmd_buffer, cmd_buffer,
2055 pSubmits[i].pCommandBuffers[j]);
2056 assert(cmd_buffer->level == VK_COMMAND_BUFFER_LEVEL_PRIMARY);
2057
2058 cs_array[j] = cmd_buffer->cs;
2059 if ((cmd_buffer->usage_flags & VK_COMMAND_BUFFER_USAGE_SIMULTANEOUS_USE_BIT))
2060 can_patch = false;
2061
2062 cmd_buffer->status = RADV_CMD_BUFFER_STATUS_PENDING;
2063 }
2064
2065 for (uint32_t j = 0; j < pSubmits[i].commandBufferCount; j += advance) {
2066 struct radeon_winsys_cs *initial_preamble = (do_flush && !j) ? initial_flush_preamble_cs : initial_preamble_cs;
2067 advance = MIN2(max_cs_submission,
2068 pSubmits[i].commandBufferCount - j);
2069
2070 if (queue->device->trace_bo)
2071 *queue->device->trace_id_ptr = 0;
2072
2073 sem_info.cs_emit_wait = j == 0;
2074 sem_info.cs_emit_signal = j + advance == pSubmits[i].commandBufferCount;
2075
2076 ret = queue->device->ws->cs_submit(ctx, queue->queue_idx, cs_array + j,
2077 advance, initial_preamble, continue_preamble_cs,
2078 &sem_info,
2079 can_patch, base_fence);
2080
2081 if (ret) {
2082 radv_loge("failed to submit CS %d\n", i);
2083 abort();
2084 }
2085 fence_emitted = true;
2086 if (queue->device->trace_bo) {
2087 radv_check_gpu_hangs(queue, cs_array[j]);
2088 }
2089 }
2090
2091 radv_free_temp_syncobjs(queue->device,
2092 pSubmits[i].waitSemaphoreCount,
2093 pSubmits[i].pWaitSemaphores);
2094 radv_free_sem_info(&sem_info);
2095 free(cs_array);
2096 }
2097
2098 if (fence) {
2099 if (!fence_emitted) {
2100 struct radv_winsys_sem_info sem_info;
2101
2102 result = radv_alloc_sem_info(&sem_info, 0, NULL, 0, NULL,
2103 _fence);
2104 if (result != VK_SUCCESS)
2105 return result;
2106
2107 ret = queue->device->ws->cs_submit(ctx, queue->queue_idx,
2108 &queue->device->empty_cs[queue->queue_family_index],
2109 1, NULL, NULL, &sem_info,
2110 false, base_fence);
2111 radv_free_sem_info(&sem_info);
2112 }
2113 fence->submitted = true;
2114 }
2115
2116 return VK_SUCCESS;
2117 }
2118
2119 VkResult radv_QueueWaitIdle(
2120 VkQueue _queue)
2121 {
2122 RADV_FROM_HANDLE(radv_queue, queue, _queue);
2123
2124 queue->device->ws->ctx_wait_idle(queue->hw_ctx,
2125 radv_queue_family_to_ring(queue->queue_family_index),
2126 queue->queue_idx);
2127 return VK_SUCCESS;
2128 }
2129
2130 VkResult radv_DeviceWaitIdle(
2131 VkDevice _device)
2132 {
2133 RADV_FROM_HANDLE(radv_device, device, _device);
2134
2135 for (unsigned i = 0; i < RADV_MAX_QUEUE_FAMILIES; i++) {
2136 for (unsigned q = 0; q < device->queue_count[i]; q++) {
2137 radv_QueueWaitIdle(radv_queue_to_handle(&device->queues[i][q]));
2138 }
2139 }
2140 return VK_SUCCESS;
2141 }
2142
2143 PFN_vkVoidFunction radv_GetInstanceProcAddr(
2144 VkInstance instance,
2145 const char* pName)
2146 {
2147 return radv_lookup_entrypoint(pName);
2148 }
2149
2150 /* The loader wants us to expose a second GetInstanceProcAddr function
2151 * to work around certain LD_PRELOAD issues seen in apps.
2152 */
2153 PUBLIC
2154 VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL vk_icdGetInstanceProcAddr(
2155 VkInstance instance,
2156 const char* pName);
2157
2158 PUBLIC
2159 VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL vk_icdGetInstanceProcAddr(
2160 VkInstance instance,
2161 const char* pName)
2162 {
2163 return radv_GetInstanceProcAddr(instance, pName);
2164 }
2165
2166 PFN_vkVoidFunction radv_GetDeviceProcAddr(
2167 VkDevice device,
2168 const char* pName)
2169 {
2170 return radv_lookup_entrypoint(pName);
2171 }
2172
2173 bool radv_get_memory_fd(struct radv_device *device,
2174 struct radv_device_memory *memory,
2175 int *pFD)
2176 {
2177 struct radeon_bo_metadata metadata;
2178
2179 if (memory->image) {
2180 radv_init_metadata(device, memory->image, &metadata);
2181 device->ws->buffer_set_metadata(memory->bo, &metadata);
2182 }
2183
2184 return device->ws->buffer_get_fd(device->ws, memory->bo,
2185 pFD);
2186 }
2187
2188 static VkResult radv_alloc_memory(struct radv_device *device,
2189 const VkMemoryAllocateInfo* pAllocateInfo,
2190 const VkAllocationCallbacks* pAllocator,
2191 VkDeviceMemory* pMem)
2192 {
2193 struct radv_device_memory *mem;
2194 VkResult result;
2195 enum radeon_bo_domain domain;
2196 uint32_t flags = 0;
2197 enum radv_mem_type mem_type_index = device->physical_device->mem_type_indices[pAllocateInfo->memoryTypeIndex];
2198
2199 assert(pAllocateInfo->sType == VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO);
2200
2201 if (pAllocateInfo->allocationSize == 0) {
2202 /* Apparently, this is allowed */
2203 *pMem = VK_NULL_HANDLE;
2204 return VK_SUCCESS;
2205 }
2206
2207 const VkImportMemoryFdInfoKHR *import_info =
2208 vk_find_struct_const(pAllocateInfo->pNext, IMPORT_MEMORY_FD_INFO_KHR);
2209 const VkMemoryDedicatedAllocateInfoKHR *dedicate_info =
2210 vk_find_struct_const(pAllocateInfo->pNext, MEMORY_DEDICATED_ALLOCATE_INFO_KHR);
2211 const VkExportMemoryAllocateInfoKHR *export_info =
2212 vk_find_struct_const(pAllocateInfo->pNext, EXPORT_MEMORY_ALLOCATE_INFO_KHR);
2213
2214 const struct wsi_memory_allocate_info *wsi_info =
2215 vk_find_struct_const(pAllocateInfo->pNext, WSI_MEMORY_ALLOCATE_INFO_MESA);
2216
2217 mem = vk_alloc2(&device->alloc, pAllocator, sizeof(*mem), 8,
2218 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
2219 if (mem == NULL)
2220 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
2221
2222 if (wsi_info && wsi_info->implicit_sync)
2223 flags |= RADEON_FLAG_IMPLICIT_SYNC;
2224
2225 if (dedicate_info) {
2226 mem->image = radv_image_from_handle(dedicate_info->image);
2227 mem->buffer = radv_buffer_from_handle(dedicate_info->buffer);
2228 } else {
2229 mem->image = NULL;
2230 mem->buffer = NULL;
2231 }
2232
2233 if (import_info) {
2234 assert(import_info->handleType ==
2235 VK_EXTERNAL_MEMORY_HANDLE_TYPE_OPAQUE_FD_BIT_KHR ||
2236 import_info->handleType ==
2237 VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_EXT);
2238 mem->bo = device->ws->buffer_from_fd(device->ws, import_info->fd,
2239 NULL, NULL);
2240 if (!mem->bo) {
2241 result = VK_ERROR_INVALID_EXTERNAL_HANDLE_KHR;
2242 goto fail;
2243 } else {
2244 close(import_info->fd);
2245 goto out_success;
2246 }
2247 }
2248
2249 uint64_t alloc_size = align_u64(pAllocateInfo->allocationSize, 4096);
2250 if (mem_type_index == RADV_MEM_TYPE_GTT_WRITE_COMBINE ||
2251 mem_type_index == RADV_MEM_TYPE_GTT_CACHED)
2252 domain = RADEON_DOMAIN_GTT;
2253 else
2254 domain = RADEON_DOMAIN_VRAM;
2255
2256 if (mem_type_index == RADV_MEM_TYPE_VRAM)
2257 flags |= RADEON_FLAG_NO_CPU_ACCESS;
2258 else
2259 flags |= RADEON_FLAG_CPU_ACCESS;
2260
2261 if (mem_type_index == RADV_MEM_TYPE_GTT_WRITE_COMBINE)
2262 flags |= RADEON_FLAG_GTT_WC;
2263
2264 if (!dedicate_info && !import_info && (!export_info || !export_info->handleTypes))
2265 flags |= RADEON_FLAG_NO_INTERPROCESS_SHARING;
2266
2267 mem->bo = device->ws->buffer_create(device->ws, alloc_size, device->physical_device->rad_info.max_alignment,
2268 domain, flags);
2269
2270 if (!mem->bo) {
2271 result = VK_ERROR_OUT_OF_DEVICE_MEMORY;
2272 goto fail;
2273 }
2274 mem->type_index = mem_type_index;
2275 out_success:
2276 *pMem = radv_device_memory_to_handle(mem);
2277
2278 return VK_SUCCESS;
2279
2280 fail:
2281 vk_free2(&device->alloc, pAllocator, mem);
2282
2283 return result;
2284 }
2285
2286 VkResult radv_AllocateMemory(
2287 VkDevice _device,
2288 const VkMemoryAllocateInfo* pAllocateInfo,
2289 const VkAllocationCallbacks* pAllocator,
2290 VkDeviceMemory* pMem)
2291 {
2292 RADV_FROM_HANDLE(radv_device, device, _device);
2293 return radv_alloc_memory(device, pAllocateInfo, pAllocator, pMem);
2294 }
2295
2296 void radv_FreeMemory(
2297 VkDevice _device,
2298 VkDeviceMemory _mem,
2299 const VkAllocationCallbacks* pAllocator)
2300 {
2301 RADV_FROM_HANDLE(radv_device, device, _device);
2302 RADV_FROM_HANDLE(radv_device_memory, mem, _mem);
2303
2304 if (mem == NULL)
2305 return;
2306
2307 device->ws->buffer_destroy(mem->bo);
2308 mem->bo = NULL;
2309
2310 vk_free2(&device->alloc, pAllocator, mem);
2311 }
2312
2313 VkResult radv_MapMemory(
2314 VkDevice _device,
2315 VkDeviceMemory _memory,
2316 VkDeviceSize offset,
2317 VkDeviceSize size,
2318 VkMemoryMapFlags flags,
2319 void** ppData)
2320 {
2321 RADV_FROM_HANDLE(radv_device, device, _device);
2322 RADV_FROM_HANDLE(radv_device_memory, mem, _memory);
2323
2324 if (mem == NULL) {
2325 *ppData = NULL;
2326 return VK_SUCCESS;
2327 }
2328
2329 *ppData = device->ws->buffer_map(mem->bo);
2330 if (*ppData) {
2331 *ppData += offset;
2332 return VK_SUCCESS;
2333 }
2334
2335 return vk_error(VK_ERROR_MEMORY_MAP_FAILED);
2336 }
2337
2338 void radv_UnmapMemory(
2339 VkDevice _device,
2340 VkDeviceMemory _memory)
2341 {
2342 RADV_FROM_HANDLE(radv_device, device, _device);
2343 RADV_FROM_HANDLE(radv_device_memory, mem, _memory);
2344
2345 if (mem == NULL)
2346 return;
2347
2348 device->ws->buffer_unmap(mem->bo);
2349 }
2350
2351 VkResult radv_FlushMappedMemoryRanges(
2352 VkDevice _device,
2353 uint32_t memoryRangeCount,
2354 const VkMappedMemoryRange* pMemoryRanges)
2355 {
2356 return VK_SUCCESS;
2357 }
2358
2359 VkResult radv_InvalidateMappedMemoryRanges(
2360 VkDevice _device,
2361 uint32_t memoryRangeCount,
2362 const VkMappedMemoryRange* pMemoryRanges)
2363 {
2364 return VK_SUCCESS;
2365 }
2366
2367 void radv_GetBufferMemoryRequirements(
2368 VkDevice _device,
2369 VkBuffer _buffer,
2370 VkMemoryRequirements* pMemoryRequirements)
2371 {
2372 RADV_FROM_HANDLE(radv_device, device, _device);
2373 RADV_FROM_HANDLE(radv_buffer, buffer, _buffer);
2374
2375 pMemoryRequirements->memoryTypeBits = (1u << device->physical_device->memory_properties.memoryTypeCount) - 1;
2376
2377 if (buffer->flags & VK_BUFFER_CREATE_SPARSE_BINDING_BIT)
2378 pMemoryRequirements->alignment = 4096;
2379 else
2380 pMemoryRequirements->alignment = 16;
2381
2382 pMemoryRequirements->size = align64(buffer->size, pMemoryRequirements->alignment);
2383 }
2384
2385 void radv_GetBufferMemoryRequirements2KHR(
2386 VkDevice device,
2387 const VkBufferMemoryRequirementsInfo2KHR* pInfo,
2388 VkMemoryRequirements2KHR* pMemoryRequirements)
2389 {
2390 radv_GetBufferMemoryRequirements(device, pInfo->buffer,
2391 &pMemoryRequirements->memoryRequirements);
2392 RADV_FROM_HANDLE(radv_buffer, buffer, pInfo->buffer);
2393 vk_foreach_struct(ext, pMemoryRequirements->pNext) {
2394 switch (ext->sType) {
2395 case VK_STRUCTURE_TYPE_MEMORY_DEDICATED_REQUIREMENTS_KHR: {
2396 VkMemoryDedicatedRequirementsKHR *req =
2397 (VkMemoryDedicatedRequirementsKHR *) ext;
2398 req->requiresDedicatedAllocation = buffer->shareable;
2399 req->prefersDedicatedAllocation = req->requiresDedicatedAllocation;
2400 break;
2401 }
2402 default:
2403 break;
2404 }
2405 }
2406 }
2407
2408 void radv_GetImageMemoryRequirements(
2409 VkDevice _device,
2410 VkImage _image,
2411 VkMemoryRequirements* pMemoryRequirements)
2412 {
2413 RADV_FROM_HANDLE(radv_device, device, _device);
2414 RADV_FROM_HANDLE(radv_image, image, _image);
2415
2416 pMemoryRequirements->memoryTypeBits = (1u << device->physical_device->memory_properties.memoryTypeCount) - 1;
2417
2418 pMemoryRequirements->size = image->size;
2419 pMemoryRequirements->alignment = image->alignment;
2420 }
2421
2422 void radv_GetImageMemoryRequirements2KHR(
2423 VkDevice device,
2424 const VkImageMemoryRequirementsInfo2KHR* pInfo,
2425 VkMemoryRequirements2KHR* pMemoryRequirements)
2426 {
2427 radv_GetImageMemoryRequirements(device, pInfo->image,
2428 &pMemoryRequirements->memoryRequirements);
2429
2430 RADV_FROM_HANDLE(radv_image, image, pInfo->image);
2431
2432 vk_foreach_struct(ext, pMemoryRequirements->pNext) {
2433 switch (ext->sType) {
2434 case VK_STRUCTURE_TYPE_MEMORY_DEDICATED_REQUIREMENTS_KHR: {
2435 VkMemoryDedicatedRequirementsKHR *req =
2436 (VkMemoryDedicatedRequirementsKHR *) ext;
2437 req->requiresDedicatedAllocation = image->shareable;
2438 req->prefersDedicatedAllocation = req->requiresDedicatedAllocation;
2439 break;
2440 }
2441 default:
2442 break;
2443 }
2444 }
2445 }
2446
2447 void radv_GetImageSparseMemoryRequirements(
2448 VkDevice device,
2449 VkImage image,
2450 uint32_t* pSparseMemoryRequirementCount,
2451 VkSparseImageMemoryRequirements* pSparseMemoryRequirements)
2452 {
2453 stub();
2454 }
2455
2456 void radv_GetImageSparseMemoryRequirements2KHR(
2457 VkDevice device,
2458 const VkImageSparseMemoryRequirementsInfo2KHR* pInfo,
2459 uint32_t* pSparseMemoryRequirementCount,
2460 VkSparseImageMemoryRequirements2KHR* pSparseMemoryRequirements)
2461 {
2462 stub();
2463 }
2464
2465 void radv_GetDeviceMemoryCommitment(
2466 VkDevice device,
2467 VkDeviceMemory memory,
2468 VkDeviceSize* pCommittedMemoryInBytes)
2469 {
2470 *pCommittedMemoryInBytes = 0;
2471 }
2472
2473 VkResult radv_BindBufferMemory2KHR(VkDevice device,
2474 uint32_t bindInfoCount,
2475 const VkBindBufferMemoryInfoKHR *pBindInfos)
2476 {
2477 for (uint32_t i = 0; i < bindInfoCount; ++i) {
2478 RADV_FROM_HANDLE(radv_device_memory, mem, pBindInfos[i].memory);
2479 RADV_FROM_HANDLE(radv_buffer, buffer, pBindInfos[i].buffer);
2480
2481 if (mem) {
2482 buffer->bo = mem->bo;
2483 buffer->offset = pBindInfos[i].memoryOffset;
2484 } else {
2485 buffer->bo = NULL;
2486 }
2487 }
2488 return VK_SUCCESS;
2489 }
2490
2491 VkResult radv_BindBufferMemory(
2492 VkDevice device,
2493 VkBuffer buffer,
2494 VkDeviceMemory memory,
2495 VkDeviceSize memoryOffset)
2496 {
2497 const VkBindBufferMemoryInfoKHR info = {
2498 .sType = VK_STRUCTURE_TYPE_BIND_BUFFER_MEMORY_INFO_KHR,
2499 .buffer = buffer,
2500 .memory = memory,
2501 .memoryOffset = memoryOffset
2502 };
2503
2504 return radv_BindBufferMemory2KHR(device, 1, &info);
2505 }
2506
2507 VkResult radv_BindImageMemory2KHR(VkDevice device,
2508 uint32_t bindInfoCount,
2509 const VkBindImageMemoryInfoKHR *pBindInfos)
2510 {
2511 for (uint32_t i = 0; i < bindInfoCount; ++i) {
2512 RADV_FROM_HANDLE(radv_device_memory, mem, pBindInfos[i].memory);
2513 RADV_FROM_HANDLE(radv_image, image, pBindInfos[i].image);
2514
2515 if (mem) {
2516 image->bo = mem->bo;
2517 image->offset = pBindInfos[i].memoryOffset;
2518 } else {
2519 image->bo = NULL;
2520 image->offset = 0;
2521 }
2522 }
2523 return VK_SUCCESS;
2524 }
2525
2526
2527 VkResult radv_BindImageMemory(
2528 VkDevice device,
2529 VkImage image,
2530 VkDeviceMemory memory,
2531 VkDeviceSize memoryOffset)
2532 {
2533 const VkBindImageMemoryInfoKHR info = {
2534 .sType = VK_STRUCTURE_TYPE_BIND_BUFFER_MEMORY_INFO_KHR,
2535 .image = image,
2536 .memory = memory,
2537 .memoryOffset = memoryOffset
2538 };
2539
2540 return radv_BindImageMemory2KHR(device, 1, &info);
2541 }
2542
2543
2544 static void
2545 radv_sparse_buffer_bind_memory(struct radv_device *device,
2546 const VkSparseBufferMemoryBindInfo *bind)
2547 {
2548 RADV_FROM_HANDLE(radv_buffer, buffer, bind->buffer);
2549
2550 for (uint32_t i = 0; i < bind->bindCount; ++i) {
2551 struct radv_device_memory *mem = NULL;
2552
2553 if (bind->pBinds[i].memory != VK_NULL_HANDLE)
2554 mem = radv_device_memory_from_handle(bind->pBinds[i].memory);
2555
2556 device->ws->buffer_virtual_bind(buffer->bo,
2557 bind->pBinds[i].resourceOffset,
2558 bind->pBinds[i].size,
2559 mem ? mem->bo : NULL,
2560 bind->pBinds[i].memoryOffset);
2561 }
2562 }
2563
2564 static void
2565 radv_sparse_image_opaque_bind_memory(struct radv_device *device,
2566 const VkSparseImageOpaqueMemoryBindInfo *bind)
2567 {
2568 RADV_FROM_HANDLE(radv_image, image, bind->image);
2569
2570 for (uint32_t i = 0; i < bind->bindCount; ++i) {
2571 struct radv_device_memory *mem = NULL;
2572
2573 if (bind->pBinds[i].memory != VK_NULL_HANDLE)
2574 mem = radv_device_memory_from_handle(bind->pBinds[i].memory);
2575
2576 device->ws->buffer_virtual_bind(image->bo,
2577 bind->pBinds[i].resourceOffset,
2578 bind->pBinds[i].size,
2579 mem ? mem->bo : NULL,
2580 bind->pBinds[i].memoryOffset);
2581 }
2582 }
2583
2584 VkResult radv_QueueBindSparse(
2585 VkQueue _queue,
2586 uint32_t bindInfoCount,
2587 const VkBindSparseInfo* pBindInfo,
2588 VkFence _fence)
2589 {
2590 RADV_FROM_HANDLE(radv_fence, fence, _fence);
2591 RADV_FROM_HANDLE(radv_queue, queue, _queue);
2592 struct radeon_winsys_fence *base_fence = fence ? fence->fence : NULL;
2593 bool fence_emitted = false;
2594
2595 for (uint32_t i = 0; i < bindInfoCount; ++i) {
2596 struct radv_winsys_sem_info sem_info;
2597 for (uint32_t j = 0; j < pBindInfo[i].bufferBindCount; ++j) {
2598 radv_sparse_buffer_bind_memory(queue->device,
2599 pBindInfo[i].pBufferBinds + j);
2600 }
2601
2602 for (uint32_t j = 0; j < pBindInfo[i].imageOpaqueBindCount; ++j) {
2603 radv_sparse_image_opaque_bind_memory(queue->device,
2604 pBindInfo[i].pImageOpaqueBinds + j);
2605 }
2606
2607 VkResult result;
2608 result = radv_alloc_sem_info(&sem_info,
2609 pBindInfo[i].waitSemaphoreCount,
2610 pBindInfo[i].pWaitSemaphores,
2611 pBindInfo[i].signalSemaphoreCount,
2612 pBindInfo[i].pSignalSemaphores,
2613 _fence);
2614 if (result != VK_SUCCESS)
2615 return result;
2616
2617 if (pBindInfo[i].waitSemaphoreCount || pBindInfo[i].signalSemaphoreCount) {
2618 queue->device->ws->cs_submit(queue->hw_ctx, queue->queue_idx,
2619 &queue->device->empty_cs[queue->queue_family_index],
2620 1, NULL, NULL,
2621 &sem_info,
2622 false, base_fence);
2623 fence_emitted = true;
2624 if (fence)
2625 fence->submitted = true;
2626 }
2627
2628 radv_free_sem_info(&sem_info);
2629
2630 }
2631
2632 if (fence && !fence_emitted) {
2633 fence->signalled = true;
2634 }
2635
2636 return VK_SUCCESS;
2637 }
2638
2639 VkResult radv_CreateFence(
2640 VkDevice _device,
2641 const VkFenceCreateInfo* pCreateInfo,
2642 const VkAllocationCallbacks* pAllocator,
2643 VkFence* pFence)
2644 {
2645 RADV_FROM_HANDLE(radv_device, device, _device);
2646 const VkExportFenceCreateInfoKHR *export =
2647 vk_find_struct_const(pCreateInfo->pNext, EXPORT_FENCE_CREATE_INFO_KHR);
2648 VkExternalFenceHandleTypeFlagsKHR handleTypes =
2649 export ? export->handleTypes : 0;
2650
2651 struct radv_fence *fence = vk_alloc2(&device->alloc, pAllocator,
2652 sizeof(*fence), 8,
2653 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
2654
2655 if (!fence)
2656 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
2657
2658 fence->submitted = false;
2659 fence->signalled = !!(pCreateInfo->flags & VK_FENCE_CREATE_SIGNALED_BIT);
2660 fence->temp_syncobj = 0;
2661 if (handleTypes) {
2662 int ret = device->ws->create_syncobj(device->ws, &fence->syncobj);
2663 if (ret) {
2664 vk_free2(&device->alloc, pAllocator, fence);
2665 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
2666 }
2667 if (pCreateInfo->flags & VK_FENCE_CREATE_SIGNALED_BIT) {
2668 device->ws->signal_syncobj(device->ws, fence->syncobj);
2669 }
2670 fence->fence = NULL;
2671 } else {
2672 fence->fence = device->ws->create_fence();
2673 if (!fence->fence) {
2674 vk_free2(&device->alloc, pAllocator, fence);
2675 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
2676 }
2677 fence->syncobj = 0;
2678 }
2679
2680 *pFence = radv_fence_to_handle(fence);
2681
2682 return VK_SUCCESS;
2683 }
2684
2685 void radv_DestroyFence(
2686 VkDevice _device,
2687 VkFence _fence,
2688 const VkAllocationCallbacks* pAllocator)
2689 {
2690 RADV_FROM_HANDLE(radv_device, device, _device);
2691 RADV_FROM_HANDLE(radv_fence, fence, _fence);
2692
2693 if (!fence)
2694 return;
2695
2696 if (fence->temp_syncobj)
2697 device->ws->destroy_syncobj(device->ws, fence->temp_syncobj);
2698 if (fence->syncobj)
2699 device->ws->destroy_syncobj(device->ws, fence->syncobj);
2700 if (fence->fence)
2701 device->ws->destroy_fence(fence->fence);
2702 vk_free2(&device->alloc, pAllocator, fence);
2703 }
2704
2705 static uint64_t radv_get_absolute_timeout(uint64_t timeout)
2706 {
2707 uint64_t current_time;
2708 struct timespec tv;
2709
2710 clock_gettime(CLOCK_MONOTONIC, &tv);
2711 current_time = tv.tv_nsec + tv.tv_sec*1000000000ull;
2712
2713 timeout = MIN2(UINT64_MAX - current_time, timeout);
2714
2715 return current_time + timeout;
2716 }
2717
2718 VkResult radv_WaitForFences(
2719 VkDevice _device,
2720 uint32_t fenceCount,
2721 const VkFence* pFences,
2722 VkBool32 waitAll,
2723 uint64_t timeout)
2724 {
2725 RADV_FROM_HANDLE(radv_device, device, _device);
2726 timeout = radv_get_absolute_timeout(timeout);
2727
2728 if (!waitAll && fenceCount > 1) {
2729 fprintf(stderr, "radv: WaitForFences without waitAll not implemented yet\n");
2730 }
2731
2732 for (uint32_t i = 0; i < fenceCount; ++i) {
2733 RADV_FROM_HANDLE(radv_fence, fence, pFences[i]);
2734 bool expired = false;
2735
2736 if (fence->temp_syncobj) {
2737 if (!device->ws->wait_syncobj(device->ws, fence->temp_syncobj, timeout))
2738 return VK_TIMEOUT;
2739 continue;
2740 }
2741
2742 if (fence->syncobj) {
2743 if (!device->ws->wait_syncobj(device->ws, fence->syncobj, timeout))
2744 return VK_TIMEOUT;
2745 continue;
2746 }
2747
2748 if (fence->signalled)
2749 continue;
2750
2751 if (!fence->submitted)
2752 return VK_TIMEOUT;
2753
2754 expired = device->ws->fence_wait(device->ws, fence->fence, true, timeout);
2755 if (!expired)
2756 return VK_TIMEOUT;
2757
2758 fence->signalled = true;
2759 }
2760
2761 return VK_SUCCESS;
2762 }
2763
2764 VkResult radv_ResetFences(VkDevice _device,
2765 uint32_t fenceCount,
2766 const VkFence *pFences)
2767 {
2768 RADV_FROM_HANDLE(radv_device, device, _device);
2769
2770 for (unsigned i = 0; i < fenceCount; ++i) {
2771 RADV_FROM_HANDLE(radv_fence, fence, pFences[i]);
2772 fence->submitted = fence->signalled = false;
2773
2774 /* Per spec, we first restore the permanent payload, and then reset, so
2775 * having a temp syncobj should not skip resetting the permanent syncobj. */
2776 if (fence->temp_syncobj) {
2777 device->ws->destroy_syncobj(device->ws, fence->temp_syncobj);
2778 fence->temp_syncobj = 0;
2779 }
2780
2781 if (fence->syncobj) {
2782 device->ws->reset_syncobj(device->ws, fence->syncobj);
2783 }
2784 }
2785
2786 return VK_SUCCESS;
2787 }
2788
2789 VkResult radv_GetFenceStatus(VkDevice _device, VkFence _fence)
2790 {
2791 RADV_FROM_HANDLE(radv_device, device, _device);
2792 RADV_FROM_HANDLE(radv_fence, fence, _fence);
2793
2794 if (fence->temp_syncobj) {
2795 bool success = device->ws->wait_syncobj(device->ws, fence->temp_syncobj, 0);
2796 return success ? VK_SUCCESS : VK_NOT_READY;
2797 }
2798
2799 if (fence->syncobj) {
2800 bool success = device->ws->wait_syncobj(device->ws, fence->syncobj, 0);
2801 return success ? VK_SUCCESS : VK_NOT_READY;
2802 }
2803
2804 if (fence->signalled)
2805 return VK_SUCCESS;
2806 if (!fence->submitted)
2807 return VK_NOT_READY;
2808 if (!device->ws->fence_wait(device->ws, fence->fence, false, 0))
2809 return VK_NOT_READY;
2810
2811 return VK_SUCCESS;
2812 }
2813
2814
2815 // Queue semaphore functions
2816
2817 VkResult radv_CreateSemaphore(
2818 VkDevice _device,
2819 const VkSemaphoreCreateInfo* pCreateInfo,
2820 const VkAllocationCallbacks* pAllocator,
2821 VkSemaphore* pSemaphore)
2822 {
2823 RADV_FROM_HANDLE(radv_device, device, _device);
2824 const VkExportSemaphoreCreateInfoKHR *export =
2825 vk_find_struct_const(pCreateInfo->pNext, EXPORT_SEMAPHORE_CREATE_INFO_KHR);
2826 VkExternalSemaphoreHandleTypeFlagsKHR handleTypes =
2827 export ? export->handleTypes : 0;
2828
2829 struct radv_semaphore *sem = vk_alloc2(&device->alloc, pAllocator,
2830 sizeof(*sem), 8,
2831 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
2832 if (!sem)
2833 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
2834
2835 sem->temp_syncobj = 0;
2836 /* create a syncobject if we are going to export this semaphore */
2837 if (handleTypes) {
2838 assert (device->physical_device->rad_info.has_syncobj);
2839 int ret = device->ws->create_syncobj(device->ws, &sem->syncobj);
2840 if (ret) {
2841 vk_free2(&device->alloc, pAllocator, sem);
2842 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
2843 }
2844 sem->sem = NULL;
2845 } else {
2846 sem->sem = device->ws->create_sem(device->ws);
2847 if (!sem->sem) {
2848 vk_free2(&device->alloc, pAllocator, sem);
2849 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
2850 }
2851 sem->syncobj = 0;
2852 }
2853
2854 *pSemaphore = radv_semaphore_to_handle(sem);
2855 return VK_SUCCESS;
2856 }
2857
2858 void radv_DestroySemaphore(
2859 VkDevice _device,
2860 VkSemaphore _semaphore,
2861 const VkAllocationCallbacks* pAllocator)
2862 {
2863 RADV_FROM_HANDLE(radv_device, device, _device);
2864 RADV_FROM_HANDLE(radv_semaphore, sem, _semaphore);
2865 if (!_semaphore)
2866 return;
2867
2868 if (sem->syncobj)
2869 device->ws->destroy_syncobj(device->ws, sem->syncobj);
2870 else
2871 device->ws->destroy_sem(sem->sem);
2872 vk_free2(&device->alloc, pAllocator, sem);
2873 }
2874
2875 VkResult radv_CreateEvent(
2876 VkDevice _device,
2877 const VkEventCreateInfo* pCreateInfo,
2878 const VkAllocationCallbacks* pAllocator,
2879 VkEvent* pEvent)
2880 {
2881 RADV_FROM_HANDLE(radv_device, device, _device);
2882 struct radv_event *event = vk_alloc2(&device->alloc, pAllocator,
2883 sizeof(*event), 8,
2884 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
2885
2886 if (!event)
2887 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
2888
2889 event->bo = device->ws->buffer_create(device->ws, 8, 8,
2890 RADEON_DOMAIN_GTT,
2891 RADEON_FLAG_VA_UNCACHED | RADEON_FLAG_CPU_ACCESS | RADEON_FLAG_NO_INTERPROCESS_SHARING);
2892 if (!event->bo) {
2893 vk_free2(&device->alloc, pAllocator, event);
2894 return vk_error(VK_ERROR_OUT_OF_DEVICE_MEMORY);
2895 }
2896
2897 event->map = (uint64_t*)device->ws->buffer_map(event->bo);
2898
2899 *pEvent = radv_event_to_handle(event);
2900
2901 return VK_SUCCESS;
2902 }
2903
2904 void radv_DestroyEvent(
2905 VkDevice _device,
2906 VkEvent _event,
2907 const VkAllocationCallbacks* pAllocator)
2908 {
2909 RADV_FROM_HANDLE(radv_device, device, _device);
2910 RADV_FROM_HANDLE(radv_event, event, _event);
2911
2912 if (!event)
2913 return;
2914 device->ws->buffer_destroy(event->bo);
2915 vk_free2(&device->alloc, pAllocator, event);
2916 }
2917
2918 VkResult radv_GetEventStatus(
2919 VkDevice _device,
2920 VkEvent _event)
2921 {
2922 RADV_FROM_HANDLE(radv_event, event, _event);
2923
2924 if (*event->map == 1)
2925 return VK_EVENT_SET;
2926 return VK_EVENT_RESET;
2927 }
2928
2929 VkResult radv_SetEvent(
2930 VkDevice _device,
2931 VkEvent _event)
2932 {
2933 RADV_FROM_HANDLE(radv_event, event, _event);
2934 *event->map = 1;
2935
2936 return VK_SUCCESS;
2937 }
2938
2939 VkResult radv_ResetEvent(
2940 VkDevice _device,
2941 VkEvent _event)
2942 {
2943 RADV_FROM_HANDLE(radv_event, event, _event);
2944 *event->map = 0;
2945
2946 return VK_SUCCESS;
2947 }
2948
2949 VkResult radv_CreateBuffer(
2950 VkDevice _device,
2951 const VkBufferCreateInfo* pCreateInfo,
2952 const VkAllocationCallbacks* pAllocator,
2953 VkBuffer* pBuffer)
2954 {
2955 RADV_FROM_HANDLE(radv_device, device, _device);
2956 struct radv_buffer *buffer;
2957
2958 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO);
2959
2960 buffer = vk_alloc2(&device->alloc, pAllocator, sizeof(*buffer), 8,
2961 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
2962 if (buffer == NULL)
2963 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
2964
2965 buffer->size = pCreateInfo->size;
2966 buffer->usage = pCreateInfo->usage;
2967 buffer->bo = NULL;
2968 buffer->offset = 0;
2969 buffer->flags = pCreateInfo->flags;
2970
2971 buffer->shareable = vk_find_struct_const(pCreateInfo->pNext,
2972 EXTERNAL_MEMORY_BUFFER_CREATE_INFO_KHR) != NULL;
2973
2974 if (pCreateInfo->flags & VK_BUFFER_CREATE_SPARSE_BINDING_BIT) {
2975 buffer->bo = device->ws->buffer_create(device->ws,
2976 align64(buffer->size, 4096),
2977 4096, 0, RADEON_FLAG_VIRTUAL);
2978 if (!buffer->bo) {
2979 vk_free2(&device->alloc, pAllocator, buffer);
2980 return vk_error(VK_ERROR_OUT_OF_DEVICE_MEMORY);
2981 }
2982 }
2983
2984 *pBuffer = radv_buffer_to_handle(buffer);
2985
2986 return VK_SUCCESS;
2987 }
2988
2989 void radv_DestroyBuffer(
2990 VkDevice _device,
2991 VkBuffer _buffer,
2992 const VkAllocationCallbacks* pAllocator)
2993 {
2994 RADV_FROM_HANDLE(radv_device, device, _device);
2995 RADV_FROM_HANDLE(radv_buffer, buffer, _buffer);
2996
2997 if (!buffer)
2998 return;
2999
3000 if (buffer->flags & VK_BUFFER_CREATE_SPARSE_BINDING_BIT)
3001 device->ws->buffer_destroy(buffer->bo);
3002
3003 vk_free2(&device->alloc, pAllocator, buffer);
3004 }
3005
3006 static inline unsigned
3007 si_tile_mode_index(const struct radv_image *image, unsigned level, bool stencil)
3008 {
3009 if (stencil)
3010 return image->surface.u.legacy.stencil_tiling_index[level];
3011 else
3012 return image->surface.u.legacy.tiling_index[level];
3013 }
3014
3015 static uint32_t radv_surface_max_layer_count(struct radv_image_view *iview)
3016 {
3017 return iview->type == VK_IMAGE_VIEW_TYPE_3D ? iview->extent.depth : (iview->base_layer + iview->layer_count);
3018 }
3019
3020 static void
3021 radv_initialise_color_surface(struct radv_device *device,
3022 struct radv_color_buffer_info *cb,
3023 struct radv_image_view *iview)
3024 {
3025 const struct vk_format_description *desc;
3026 unsigned ntype, format, swap, endian;
3027 unsigned blend_clamp = 0, blend_bypass = 0;
3028 uint64_t va;
3029 const struct radeon_surf *surf = &iview->image->surface;
3030
3031 desc = vk_format_description(iview->vk_format);
3032
3033 memset(cb, 0, sizeof(*cb));
3034
3035 /* Intensity is implemented as Red, so treat it that way. */
3036 cb->cb_color_attrib = S_028C74_FORCE_DST_ALPHA_1(desc->swizzle[3] == VK_SWIZZLE_1);
3037
3038 va = radv_buffer_get_va(iview->bo) + iview->image->offset;
3039
3040 cb->cb_color_base = va >> 8;
3041
3042 if (device->physical_device->rad_info.chip_class >= GFX9) {
3043 struct gfx9_surf_meta_flags meta;
3044 if (iview->image->dcc_offset)
3045 meta = iview->image->surface.u.gfx9.dcc;
3046 else
3047 meta = iview->image->surface.u.gfx9.cmask;
3048
3049 cb->cb_color_attrib |= S_028C74_COLOR_SW_MODE(iview->image->surface.u.gfx9.surf.swizzle_mode) |
3050 S_028C74_FMASK_SW_MODE(iview->image->surface.u.gfx9.fmask.swizzle_mode) |
3051 S_028C74_RB_ALIGNED(meta.rb_aligned) |
3052 S_028C74_PIPE_ALIGNED(meta.pipe_aligned);
3053
3054 cb->cb_color_base += iview->image->surface.u.gfx9.surf_offset >> 8;
3055 cb->cb_color_base |= iview->image->surface.tile_swizzle;
3056 } else {
3057 const struct legacy_surf_level *level_info = &surf->u.legacy.level[iview->base_mip];
3058 unsigned pitch_tile_max, slice_tile_max, tile_mode_index;
3059
3060 cb->cb_color_base += level_info->offset >> 8;
3061 if (level_info->mode == RADEON_SURF_MODE_2D)
3062 cb->cb_color_base |= iview->image->surface.tile_swizzle;
3063
3064 pitch_tile_max = level_info->nblk_x / 8 - 1;
3065 slice_tile_max = (level_info->nblk_x * level_info->nblk_y) / 64 - 1;
3066 tile_mode_index = si_tile_mode_index(iview->image, iview->base_mip, false);
3067
3068 cb->cb_color_pitch = S_028C64_TILE_MAX(pitch_tile_max);
3069 cb->cb_color_slice = S_028C68_TILE_MAX(slice_tile_max);
3070 cb->cb_color_cmask_slice = iview->image->cmask.slice_tile_max;
3071
3072 cb->cb_color_attrib |= S_028C74_TILE_MODE_INDEX(tile_mode_index);
3073
3074 if (iview->image->fmask.size) {
3075 if (device->physical_device->rad_info.chip_class >= CIK)
3076 cb->cb_color_pitch |= S_028C64_FMASK_TILE_MAX(iview->image->fmask.pitch_in_pixels / 8 - 1);
3077 cb->cb_color_attrib |= S_028C74_FMASK_TILE_MODE_INDEX(iview->image->fmask.tile_mode_index);
3078 cb->cb_color_fmask_slice = S_028C88_TILE_MAX(iview->image->fmask.slice_tile_max);
3079 } else {
3080 /* This must be set for fast clear to work without FMASK. */
3081 if (device->physical_device->rad_info.chip_class >= CIK)
3082 cb->cb_color_pitch |= S_028C64_FMASK_TILE_MAX(pitch_tile_max);
3083 cb->cb_color_attrib |= S_028C74_FMASK_TILE_MODE_INDEX(tile_mode_index);
3084 cb->cb_color_fmask_slice = S_028C88_TILE_MAX(slice_tile_max);
3085 }
3086 }
3087
3088 /* CMASK variables */
3089 va = radv_buffer_get_va(iview->bo) + iview->image->offset;
3090 va += iview->image->cmask.offset;
3091 cb->cb_color_cmask = va >> 8;
3092
3093 va = radv_buffer_get_va(iview->bo) + iview->image->offset;
3094 va += iview->image->dcc_offset;
3095 cb->cb_dcc_base = va >> 8;
3096 cb->cb_dcc_base |= iview->image->surface.tile_swizzle;
3097
3098 uint32_t max_slice = radv_surface_max_layer_count(iview) - 1;
3099 cb->cb_color_view = S_028C6C_SLICE_START(iview->base_layer) |
3100 S_028C6C_SLICE_MAX(max_slice);
3101
3102 if (iview->image->info.samples > 1) {
3103 unsigned log_samples = util_logbase2(iview->image->info.samples);
3104
3105 cb->cb_color_attrib |= S_028C74_NUM_SAMPLES(log_samples) |
3106 S_028C74_NUM_FRAGMENTS(log_samples);
3107 }
3108
3109 if (iview->image->fmask.size) {
3110 va = radv_buffer_get_va(iview->bo) + iview->image->offset + iview->image->fmask.offset;
3111 cb->cb_color_fmask = va >> 8;
3112 cb->cb_color_fmask |= iview->image->fmask.tile_swizzle;
3113 } else {
3114 cb->cb_color_fmask = cb->cb_color_base;
3115 }
3116
3117 ntype = radv_translate_color_numformat(iview->vk_format,
3118 desc,
3119 vk_format_get_first_non_void_channel(iview->vk_format));
3120 format = radv_translate_colorformat(iview->vk_format);
3121 if (format == V_028C70_COLOR_INVALID || ntype == ~0u)
3122 radv_finishme("Illegal color\n");
3123 swap = radv_translate_colorswap(iview->vk_format, FALSE);
3124 endian = radv_colorformat_endian_swap(format);
3125
3126 /* blend clamp should be set for all NORM/SRGB types */
3127 if (ntype == V_028C70_NUMBER_UNORM ||
3128 ntype == V_028C70_NUMBER_SNORM ||
3129 ntype == V_028C70_NUMBER_SRGB)
3130 blend_clamp = 1;
3131
3132 /* set blend bypass according to docs if SINT/UINT or
3133 8/24 COLOR variants */
3134 if (ntype == V_028C70_NUMBER_UINT || ntype == V_028C70_NUMBER_SINT ||
3135 format == V_028C70_COLOR_8_24 || format == V_028C70_COLOR_24_8 ||
3136 format == V_028C70_COLOR_X24_8_32_FLOAT) {
3137 blend_clamp = 0;
3138 blend_bypass = 1;
3139 }
3140 #if 0
3141 if ((ntype == V_028C70_NUMBER_UINT || ntype == V_028C70_NUMBER_SINT) &&
3142 (format == V_028C70_COLOR_8 ||
3143 format == V_028C70_COLOR_8_8 ||
3144 format == V_028C70_COLOR_8_8_8_8))
3145 ->color_is_int8 = true;
3146 #endif
3147 cb->cb_color_info = S_028C70_FORMAT(format) |
3148 S_028C70_COMP_SWAP(swap) |
3149 S_028C70_BLEND_CLAMP(blend_clamp) |
3150 S_028C70_BLEND_BYPASS(blend_bypass) |
3151 S_028C70_SIMPLE_FLOAT(1) |
3152 S_028C70_ROUND_MODE(ntype != V_028C70_NUMBER_UNORM &&
3153 ntype != V_028C70_NUMBER_SNORM &&
3154 ntype != V_028C70_NUMBER_SRGB &&
3155 format != V_028C70_COLOR_8_24 &&
3156 format != V_028C70_COLOR_24_8) |
3157 S_028C70_NUMBER_TYPE(ntype) |
3158 S_028C70_ENDIAN(endian);
3159 if ((iview->image->info.samples > 1) && iview->image->fmask.size) {
3160 cb->cb_color_info |= S_028C70_COMPRESSION(1);
3161 if (device->physical_device->rad_info.chip_class == SI) {
3162 unsigned fmask_bankh = util_logbase2(iview->image->fmask.bank_height);
3163 cb->cb_color_attrib |= S_028C74_FMASK_BANK_HEIGHT(fmask_bankh);
3164 }
3165 }
3166
3167 if (iview->image->cmask.size &&
3168 !(device->instance->debug_flags & RADV_DEBUG_NO_FAST_CLEARS))
3169 cb->cb_color_info |= S_028C70_FAST_CLEAR(1);
3170
3171 if (radv_vi_dcc_enabled(iview->image, iview->base_mip))
3172 cb->cb_color_info |= S_028C70_DCC_ENABLE(1);
3173
3174 if (device->physical_device->rad_info.chip_class >= VI) {
3175 unsigned max_uncompressed_block_size = V_028C78_MAX_BLOCK_SIZE_256B;
3176 unsigned min_compressed_block_size = V_028C78_MIN_BLOCK_SIZE_32B;
3177 unsigned independent_64b_blocks = 0;
3178 unsigned max_compressed_block_size;
3179
3180 /* amdvlk: [min-compressed-block-size] should be set to 32 for dGPU and
3181 64 for APU because all of our APUs to date use DIMMs which have
3182 a request granularity size of 64B while all other chips have a
3183 32B request size */
3184 if (!device->physical_device->rad_info.has_dedicated_vram)
3185 min_compressed_block_size = V_028C78_MIN_BLOCK_SIZE_64B;
3186
3187 if (iview->image->info.samples > 1) {
3188 if (iview->image->surface.bpe == 1)
3189 max_uncompressed_block_size = V_028C78_MAX_BLOCK_SIZE_64B;
3190 else if (iview->image->surface.bpe == 2)
3191 max_uncompressed_block_size = V_028C78_MAX_BLOCK_SIZE_128B;
3192 }
3193
3194 if (iview->image->usage & (VK_IMAGE_USAGE_SAMPLED_BIT | VK_IMAGE_USAGE_TRANSFER_SRC_BIT |
3195 VK_IMAGE_USAGE_INPUT_ATTACHMENT_BIT)) {
3196 independent_64b_blocks = 1;
3197 max_compressed_block_size = V_028C78_MAX_BLOCK_SIZE_64B;
3198 } else
3199 max_compressed_block_size = max_uncompressed_block_size;
3200
3201 cb->cb_dcc_control = S_028C78_MAX_UNCOMPRESSED_BLOCK_SIZE(max_uncompressed_block_size) |
3202 S_028C78_MAX_COMPRESSED_BLOCK_SIZE(max_compressed_block_size) |
3203 S_028C78_MIN_COMPRESSED_BLOCK_SIZE(min_compressed_block_size) |
3204 S_028C78_INDEPENDENT_64B_BLOCKS(independent_64b_blocks);
3205 }
3206
3207 /* This must be set for fast clear to work without FMASK. */
3208 if (!iview->image->fmask.size &&
3209 device->physical_device->rad_info.chip_class == SI) {
3210 unsigned bankh = util_logbase2(iview->image->surface.u.legacy.bankh);
3211 cb->cb_color_attrib |= S_028C74_FMASK_BANK_HEIGHT(bankh);
3212 }
3213
3214 if (device->physical_device->rad_info.chip_class >= GFX9) {
3215 unsigned mip0_depth = iview->image->type == VK_IMAGE_TYPE_3D ?
3216 (iview->extent.depth - 1) : (iview->image->info.array_size - 1);
3217
3218 cb->cb_color_view |= S_028C6C_MIP_LEVEL(iview->base_mip);
3219 cb->cb_color_attrib |= S_028C74_MIP0_DEPTH(mip0_depth) |
3220 S_028C74_RESOURCE_TYPE(iview->image->surface.u.gfx9.resource_type);
3221 cb->cb_color_attrib2 = S_028C68_MIP0_WIDTH(iview->extent.width - 1) |
3222 S_028C68_MIP0_HEIGHT(iview->extent.height - 1) |
3223 S_028C68_MAX_MIP(iview->image->info.levels - 1);
3224 }
3225 }
3226
3227 static void
3228 radv_initialise_ds_surface(struct radv_device *device,
3229 struct radv_ds_buffer_info *ds,
3230 struct radv_image_view *iview)
3231 {
3232 unsigned level = iview->base_mip;
3233 unsigned format, stencil_format;
3234 uint64_t va, s_offs, z_offs;
3235 bool stencil_only = false;
3236 memset(ds, 0, sizeof(*ds));
3237 switch (iview->image->vk_format) {
3238 case VK_FORMAT_D24_UNORM_S8_UINT:
3239 case VK_FORMAT_X8_D24_UNORM_PACK32:
3240 ds->pa_su_poly_offset_db_fmt_cntl = S_028B78_POLY_OFFSET_NEG_NUM_DB_BITS(-24);
3241 ds->offset_scale = 2.0f;
3242 break;
3243 case VK_FORMAT_D16_UNORM:
3244 case VK_FORMAT_D16_UNORM_S8_UINT:
3245 ds->pa_su_poly_offset_db_fmt_cntl = S_028B78_POLY_OFFSET_NEG_NUM_DB_BITS(-16);
3246 ds->offset_scale = 4.0f;
3247 break;
3248 case VK_FORMAT_D32_SFLOAT:
3249 case VK_FORMAT_D32_SFLOAT_S8_UINT:
3250 ds->pa_su_poly_offset_db_fmt_cntl = S_028B78_POLY_OFFSET_NEG_NUM_DB_BITS(-23) |
3251 S_028B78_POLY_OFFSET_DB_IS_FLOAT_FMT(1);
3252 ds->offset_scale = 1.0f;
3253 break;
3254 case VK_FORMAT_S8_UINT:
3255 stencil_only = true;
3256 break;
3257 default:
3258 break;
3259 }
3260
3261 format = radv_translate_dbformat(iview->image->vk_format);
3262 stencil_format = iview->image->surface.has_stencil ?
3263 V_028044_STENCIL_8 : V_028044_STENCIL_INVALID;
3264
3265 uint32_t max_slice = radv_surface_max_layer_count(iview) - 1;
3266 ds->db_depth_view = S_028008_SLICE_START(iview->base_layer) |
3267 S_028008_SLICE_MAX(max_slice);
3268
3269 ds->db_htile_data_base = 0;
3270 ds->db_htile_surface = 0;
3271
3272 va = radv_buffer_get_va(iview->bo) + iview->image->offset;
3273 s_offs = z_offs = va;
3274
3275 if (device->physical_device->rad_info.chip_class >= GFX9) {
3276 assert(iview->image->surface.u.gfx9.surf_offset == 0);
3277 s_offs += iview->image->surface.u.gfx9.stencil_offset;
3278
3279 ds->db_z_info = S_028038_FORMAT(format) |
3280 S_028038_NUM_SAMPLES(util_logbase2(iview->image->info.samples)) |
3281 S_028038_SW_MODE(iview->image->surface.u.gfx9.surf.swizzle_mode) |
3282 S_028038_MAXMIP(iview->image->info.levels - 1);
3283 ds->db_stencil_info = S_02803C_FORMAT(stencil_format) |
3284 S_02803C_SW_MODE(iview->image->surface.u.gfx9.stencil.swizzle_mode);
3285
3286 ds->db_z_info2 = S_028068_EPITCH(iview->image->surface.u.gfx9.surf.epitch);
3287 ds->db_stencil_info2 = S_02806C_EPITCH(iview->image->surface.u.gfx9.stencil.epitch);
3288 ds->db_depth_view |= S_028008_MIPID(level);
3289
3290 ds->db_depth_size = S_02801C_X_MAX(iview->image->info.width - 1) |
3291 S_02801C_Y_MAX(iview->image->info.height - 1);
3292
3293 if (radv_htile_enabled(iview->image, level)) {
3294 ds->db_z_info |= S_028038_TILE_SURFACE_ENABLE(1);
3295
3296 if (iview->image->tc_compatible_htile) {
3297 unsigned max_zplanes = 4;
3298
3299 if (iview->vk_format == VK_FORMAT_D16_UNORM &&
3300 iview->image->info.samples > 1)
3301 max_zplanes = 2;
3302
3303 ds->db_z_info |= S_028038_DECOMPRESS_ON_N_ZPLANES(max_zplanes + 1) |
3304 S_028038_ITERATE_FLUSH(1);
3305 ds->db_stencil_info |= S_02803C_ITERATE_FLUSH(1);
3306 }
3307
3308 if (!iview->image->surface.has_stencil)
3309 /* Use all of the htile_buffer for depth if there's no stencil. */
3310 ds->db_stencil_info |= S_02803C_TILE_STENCIL_DISABLE(1);
3311 va = radv_buffer_get_va(iview->bo) + iview->image->offset +
3312 iview->image->htile_offset;
3313 ds->db_htile_data_base = va >> 8;
3314 ds->db_htile_surface = S_028ABC_FULL_CACHE(1) |
3315 S_028ABC_PIPE_ALIGNED(iview->image->surface.u.gfx9.htile.pipe_aligned) |
3316 S_028ABC_RB_ALIGNED(iview->image->surface.u.gfx9.htile.rb_aligned);
3317 }
3318 } else {
3319 const struct legacy_surf_level *level_info = &iview->image->surface.u.legacy.level[level];
3320
3321 if (stencil_only)
3322 level_info = &iview->image->surface.u.legacy.stencil_level[level];
3323
3324 z_offs += iview->image->surface.u.legacy.level[level].offset;
3325 s_offs += iview->image->surface.u.legacy.stencil_level[level].offset;
3326
3327 ds->db_depth_info = S_02803C_ADDR5_SWIZZLE_MASK(!iview->image->tc_compatible_htile);
3328 ds->db_z_info = S_028040_FORMAT(format) | S_028040_ZRANGE_PRECISION(1);
3329 ds->db_stencil_info = S_028044_FORMAT(stencil_format);
3330
3331 if (iview->image->info.samples > 1)
3332 ds->db_z_info |= S_028040_NUM_SAMPLES(util_logbase2(iview->image->info.samples));
3333
3334 if (device->physical_device->rad_info.chip_class >= CIK) {
3335 struct radeon_info *info = &device->physical_device->rad_info;
3336 unsigned tiling_index = iview->image->surface.u.legacy.tiling_index[level];
3337 unsigned stencil_index = iview->image->surface.u.legacy.stencil_tiling_index[level];
3338 unsigned macro_index = iview->image->surface.u.legacy.macro_tile_index;
3339 unsigned tile_mode = info->si_tile_mode_array[tiling_index];
3340 unsigned stencil_tile_mode = info->si_tile_mode_array[stencil_index];
3341 unsigned macro_mode = info->cik_macrotile_mode_array[macro_index];
3342
3343 if (stencil_only)
3344 tile_mode = stencil_tile_mode;
3345
3346 ds->db_depth_info |=
3347 S_02803C_ARRAY_MODE(G_009910_ARRAY_MODE(tile_mode)) |
3348 S_02803C_PIPE_CONFIG(G_009910_PIPE_CONFIG(tile_mode)) |
3349 S_02803C_BANK_WIDTH(G_009990_BANK_WIDTH(macro_mode)) |
3350 S_02803C_BANK_HEIGHT(G_009990_BANK_HEIGHT(macro_mode)) |
3351 S_02803C_MACRO_TILE_ASPECT(G_009990_MACRO_TILE_ASPECT(macro_mode)) |
3352 S_02803C_NUM_BANKS(G_009990_NUM_BANKS(macro_mode));
3353 ds->db_z_info |= S_028040_TILE_SPLIT(G_009910_TILE_SPLIT(tile_mode));
3354 ds->db_stencil_info |= S_028044_TILE_SPLIT(G_009910_TILE_SPLIT(stencil_tile_mode));
3355 } else {
3356 unsigned tile_mode_index = si_tile_mode_index(iview->image, level, false);
3357 ds->db_z_info |= S_028040_TILE_MODE_INDEX(tile_mode_index);
3358 tile_mode_index = si_tile_mode_index(iview->image, level, true);
3359 ds->db_stencil_info |= S_028044_TILE_MODE_INDEX(tile_mode_index);
3360 if (stencil_only)
3361 ds->db_z_info |= S_028040_TILE_MODE_INDEX(tile_mode_index);
3362 }
3363
3364 ds->db_depth_size = S_028058_PITCH_TILE_MAX((level_info->nblk_x / 8) - 1) |
3365 S_028058_HEIGHT_TILE_MAX((level_info->nblk_y / 8) - 1);
3366 ds->db_depth_slice = S_02805C_SLICE_TILE_MAX((level_info->nblk_x * level_info->nblk_y) / 64 - 1);
3367
3368 if (radv_htile_enabled(iview->image, level)) {
3369 ds->db_z_info |= S_028040_TILE_SURFACE_ENABLE(1);
3370
3371 if (!iview->image->surface.has_stencil &&
3372 !iview->image->tc_compatible_htile)
3373 /* Use all of the htile_buffer for depth if there's no stencil. */
3374 ds->db_stencil_info |= S_028044_TILE_STENCIL_DISABLE(1);
3375
3376 va = radv_buffer_get_va(iview->bo) + iview->image->offset +
3377 iview->image->htile_offset;
3378 ds->db_htile_data_base = va >> 8;
3379 ds->db_htile_surface = S_028ABC_FULL_CACHE(1);
3380
3381 if (iview->image->tc_compatible_htile) {
3382 ds->db_htile_surface |= S_028ABC_TC_COMPATIBLE(1);
3383
3384 if (iview->image->info.samples <= 1)
3385 ds->db_z_info |= S_028040_DECOMPRESS_ON_N_ZPLANES(5);
3386 else if (iview->image->info.samples <= 4)
3387 ds->db_z_info |= S_028040_DECOMPRESS_ON_N_ZPLANES(3);
3388 else
3389 ds->db_z_info|= S_028040_DECOMPRESS_ON_N_ZPLANES(2);
3390 }
3391 }
3392 }
3393
3394 ds->db_z_read_base = ds->db_z_write_base = z_offs >> 8;
3395 ds->db_stencil_read_base = ds->db_stencil_write_base = s_offs >> 8;
3396 }
3397
3398 VkResult radv_CreateFramebuffer(
3399 VkDevice _device,
3400 const VkFramebufferCreateInfo* pCreateInfo,
3401 const VkAllocationCallbacks* pAllocator,
3402 VkFramebuffer* pFramebuffer)
3403 {
3404 RADV_FROM_HANDLE(radv_device, device, _device);
3405 struct radv_framebuffer *framebuffer;
3406
3407 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO);
3408
3409 size_t size = sizeof(*framebuffer) +
3410 sizeof(struct radv_attachment_info) * pCreateInfo->attachmentCount;
3411 framebuffer = vk_alloc2(&device->alloc, pAllocator, size, 8,
3412 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
3413 if (framebuffer == NULL)
3414 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
3415
3416 framebuffer->attachment_count = pCreateInfo->attachmentCount;
3417 framebuffer->width = pCreateInfo->width;
3418 framebuffer->height = pCreateInfo->height;
3419 framebuffer->layers = pCreateInfo->layers;
3420 for (uint32_t i = 0; i < pCreateInfo->attachmentCount; i++) {
3421 VkImageView _iview = pCreateInfo->pAttachments[i];
3422 struct radv_image_view *iview = radv_image_view_from_handle(_iview);
3423 framebuffer->attachments[i].attachment = iview;
3424 if (iview->aspect_mask & VK_IMAGE_ASPECT_COLOR_BIT) {
3425 radv_initialise_color_surface(device, &framebuffer->attachments[i].cb, iview);
3426 } else if (iview->aspect_mask & (VK_IMAGE_ASPECT_DEPTH_BIT | VK_IMAGE_ASPECT_STENCIL_BIT)) {
3427 radv_initialise_ds_surface(device, &framebuffer->attachments[i].ds, iview);
3428 }
3429 framebuffer->width = MIN2(framebuffer->width, iview->extent.width);
3430 framebuffer->height = MIN2(framebuffer->height, iview->extent.height);
3431 framebuffer->layers = MIN2(framebuffer->layers, radv_surface_max_layer_count(iview));
3432 }
3433
3434 *pFramebuffer = radv_framebuffer_to_handle(framebuffer);
3435 return VK_SUCCESS;
3436 }
3437
3438 void radv_DestroyFramebuffer(
3439 VkDevice _device,
3440 VkFramebuffer _fb,
3441 const VkAllocationCallbacks* pAllocator)
3442 {
3443 RADV_FROM_HANDLE(radv_device, device, _device);
3444 RADV_FROM_HANDLE(radv_framebuffer, fb, _fb);
3445
3446 if (!fb)
3447 return;
3448 vk_free2(&device->alloc, pAllocator, fb);
3449 }
3450
3451 static unsigned radv_tex_wrap(VkSamplerAddressMode address_mode)
3452 {
3453 switch (address_mode) {
3454 case VK_SAMPLER_ADDRESS_MODE_REPEAT:
3455 return V_008F30_SQ_TEX_WRAP;
3456 case VK_SAMPLER_ADDRESS_MODE_MIRRORED_REPEAT:
3457 return V_008F30_SQ_TEX_MIRROR;
3458 case VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE:
3459 return V_008F30_SQ_TEX_CLAMP_LAST_TEXEL;
3460 case VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_BORDER:
3461 return V_008F30_SQ_TEX_CLAMP_BORDER;
3462 case VK_SAMPLER_ADDRESS_MODE_MIRROR_CLAMP_TO_EDGE:
3463 return V_008F30_SQ_TEX_MIRROR_ONCE_LAST_TEXEL;
3464 default:
3465 unreachable("illegal tex wrap mode");
3466 break;
3467 }
3468 }
3469
3470 static unsigned
3471 radv_tex_compare(VkCompareOp op)
3472 {
3473 switch (op) {
3474 case VK_COMPARE_OP_NEVER:
3475 return V_008F30_SQ_TEX_DEPTH_COMPARE_NEVER;
3476 case VK_COMPARE_OP_LESS:
3477 return V_008F30_SQ_TEX_DEPTH_COMPARE_LESS;
3478 case VK_COMPARE_OP_EQUAL:
3479 return V_008F30_SQ_TEX_DEPTH_COMPARE_EQUAL;
3480 case VK_COMPARE_OP_LESS_OR_EQUAL:
3481 return V_008F30_SQ_TEX_DEPTH_COMPARE_LESSEQUAL;
3482 case VK_COMPARE_OP_GREATER:
3483 return V_008F30_SQ_TEX_DEPTH_COMPARE_GREATER;
3484 case VK_COMPARE_OP_NOT_EQUAL:
3485 return V_008F30_SQ_TEX_DEPTH_COMPARE_NOTEQUAL;
3486 case VK_COMPARE_OP_GREATER_OR_EQUAL:
3487 return V_008F30_SQ_TEX_DEPTH_COMPARE_GREATEREQUAL;
3488 case VK_COMPARE_OP_ALWAYS:
3489 return V_008F30_SQ_TEX_DEPTH_COMPARE_ALWAYS;
3490 default:
3491 unreachable("illegal compare mode");
3492 break;
3493 }
3494 }
3495
3496 static unsigned
3497 radv_tex_filter(VkFilter filter, unsigned max_ansio)
3498 {
3499 switch (filter) {
3500 case VK_FILTER_NEAREST:
3501 return (max_ansio > 1 ? V_008F38_SQ_TEX_XY_FILTER_ANISO_POINT :
3502 V_008F38_SQ_TEX_XY_FILTER_POINT);
3503 case VK_FILTER_LINEAR:
3504 return (max_ansio > 1 ? V_008F38_SQ_TEX_XY_FILTER_ANISO_BILINEAR :
3505 V_008F38_SQ_TEX_XY_FILTER_BILINEAR);
3506 case VK_FILTER_CUBIC_IMG:
3507 default:
3508 fprintf(stderr, "illegal texture filter");
3509 return 0;
3510 }
3511 }
3512
3513 static unsigned
3514 radv_tex_mipfilter(VkSamplerMipmapMode mode)
3515 {
3516 switch (mode) {
3517 case VK_SAMPLER_MIPMAP_MODE_NEAREST:
3518 return V_008F38_SQ_TEX_Z_FILTER_POINT;
3519 case VK_SAMPLER_MIPMAP_MODE_LINEAR:
3520 return V_008F38_SQ_TEX_Z_FILTER_LINEAR;
3521 default:
3522 return V_008F38_SQ_TEX_Z_FILTER_NONE;
3523 }
3524 }
3525
3526 static unsigned
3527 radv_tex_bordercolor(VkBorderColor bcolor)
3528 {
3529 switch (bcolor) {
3530 case VK_BORDER_COLOR_FLOAT_TRANSPARENT_BLACK:
3531 case VK_BORDER_COLOR_INT_TRANSPARENT_BLACK:
3532 return V_008F3C_SQ_TEX_BORDER_COLOR_TRANS_BLACK;
3533 case VK_BORDER_COLOR_FLOAT_OPAQUE_BLACK:
3534 case VK_BORDER_COLOR_INT_OPAQUE_BLACK:
3535 return V_008F3C_SQ_TEX_BORDER_COLOR_OPAQUE_BLACK;
3536 case VK_BORDER_COLOR_FLOAT_OPAQUE_WHITE:
3537 case VK_BORDER_COLOR_INT_OPAQUE_WHITE:
3538 return V_008F3C_SQ_TEX_BORDER_COLOR_OPAQUE_WHITE;
3539 default:
3540 break;
3541 }
3542 return 0;
3543 }
3544
3545 static unsigned
3546 radv_tex_aniso_filter(unsigned filter)
3547 {
3548 if (filter < 2)
3549 return 0;
3550 if (filter < 4)
3551 return 1;
3552 if (filter < 8)
3553 return 2;
3554 if (filter < 16)
3555 return 3;
3556 return 4;
3557 }
3558
3559 static void
3560 radv_init_sampler(struct radv_device *device,
3561 struct radv_sampler *sampler,
3562 const VkSamplerCreateInfo *pCreateInfo)
3563 {
3564 uint32_t max_aniso = pCreateInfo->anisotropyEnable && pCreateInfo->maxAnisotropy > 1.0 ?
3565 (uint32_t) pCreateInfo->maxAnisotropy : 0;
3566 uint32_t max_aniso_ratio = radv_tex_aniso_filter(max_aniso);
3567 bool is_vi = (device->physical_device->rad_info.chip_class >= VI);
3568
3569 sampler->state[0] = (S_008F30_CLAMP_X(radv_tex_wrap(pCreateInfo->addressModeU)) |
3570 S_008F30_CLAMP_Y(radv_tex_wrap(pCreateInfo->addressModeV)) |
3571 S_008F30_CLAMP_Z(radv_tex_wrap(pCreateInfo->addressModeW)) |
3572 S_008F30_MAX_ANISO_RATIO(max_aniso_ratio) |
3573 S_008F30_DEPTH_COMPARE_FUNC(radv_tex_compare(pCreateInfo->compareOp)) |
3574 S_008F30_FORCE_UNNORMALIZED(pCreateInfo->unnormalizedCoordinates ? 1 : 0) |
3575 S_008F30_ANISO_THRESHOLD(max_aniso_ratio >> 1) |
3576 S_008F30_ANISO_BIAS(max_aniso_ratio) |
3577 S_008F30_DISABLE_CUBE_WRAP(0) |
3578 S_008F30_COMPAT_MODE(is_vi));
3579 sampler->state[1] = (S_008F34_MIN_LOD(S_FIXED(CLAMP(pCreateInfo->minLod, 0, 15), 8)) |
3580 S_008F34_MAX_LOD(S_FIXED(CLAMP(pCreateInfo->maxLod, 0, 15), 8)) |
3581 S_008F34_PERF_MIP(max_aniso_ratio ? max_aniso_ratio + 6 : 0));
3582 sampler->state[2] = (S_008F38_LOD_BIAS(S_FIXED(CLAMP(pCreateInfo->mipLodBias, -16, 16), 8)) |
3583 S_008F38_XY_MAG_FILTER(radv_tex_filter(pCreateInfo->magFilter, max_aniso)) |
3584 S_008F38_XY_MIN_FILTER(radv_tex_filter(pCreateInfo->minFilter, max_aniso)) |
3585 S_008F38_MIP_FILTER(radv_tex_mipfilter(pCreateInfo->mipmapMode)) |
3586 S_008F38_MIP_POINT_PRECLAMP(0) |
3587 S_008F38_DISABLE_LSB_CEIL(device->physical_device->rad_info.chip_class <= VI) |
3588 S_008F38_FILTER_PREC_FIX(1) |
3589 S_008F38_ANISO_OVERRIDE(is_vi));
3590 sampler->state[3] = (S_008F3C_BORDER_COLOR_PTR(0) |
3591 S_008F3C_BORDER_COLOR_TYPE(radv_tex_bordercolor(pCreateInfo->borderColor)));
3592 }
3593
3594 VkResult radv_CreateSampler(
3595 VkDevice _device,
3596 const VkSamplerCreateInfo* pCreateInfo,
3597 const VkAllocationCallbacks* pAllocator,
3598 VkSampler* pSampler)
3599 {
3600 RADV_FROM_HANDLE(radv_device, device, _device);
3601 struct radv_sampler *sampler;
3602
3603 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO);
3604
3605 sampler = vk_alloc2(&device->alloc, pAllocator, sizeof(*sampler), 8,
3606 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
3607 if (!sampler)
3608 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
3609
3610 radv_init_sampler(device, sampler, pCreateInfo);
3611 *pSampler = radv_sampler_to_handle(sampler);
3612
3613 return VK_SUCCESS;
3614 }
3615
3616 void radv_DestroySampler(
3617 VkDevice _device,
3618 VkSampler _sampler,
3619 const VkAllocationCallbacks* pAllocator)
3620 {
3621 RADV_FROM_HANDLE(radv_device, device, _device);
3622 RADV_FROM_HANDLE(radv_sampler, sampler, _sampler);
3623
3624 if (!sampler)
3625 return;
3626 vk_free2(&device->alloc, pAllocator, sampler);
3627 }
3628
3629 /* vk_icd.h does not declare this function, so we declare it here to
3630 * suppress Wmissing-prototypes.
3631 */
3632 PUBLIC VKAPI_ATTR VkResult VKAPI_CALL
3633 vk_icdNegotiateLoaderICDInterfaceVersion(uint32_t *pSupportedVersion);
3634
3635 PUBLIC VKAPI_ATTR VkResult VKAPI_CALL
3636 vk_icdNegotiateLoaderICDInterfaceVersion(uint32_t *pSupportedVersion)
3637 {
3638 /* For the full details on loader interface versioning, see
3639 * <https://github.com/KhronosGroup/Vulkan-LoaderAndValidationLayers/blob/master/loader/LoaderAndLayerInterface.md>.
3640 * What follows is a condensed summary, to help you navigate the large and
3641 * confusing official doc.
3642 *
3643 * - Loader interface v0 is incompatible with later versions. We don't
3644 * support it.
3645 *
3646 * - In loader interface v1:
3647 * - The first ICD entrypoint called by the loader is
3648 * vk_icdGetInstanceProcAddr(). The ICD must statically expose this
3649 * entrypoint.
3650 * - The ICD must statically expose no other Vulkan symbol unless it is
3651 * linked with -Bsymbolic.
3652 * - Each dispatchable Vulkan handle created by the ICD must be
3653 * a pointer to a struct whose first member is VK_LOADER_DATA. The
3654 * ICD must initialize VK_LOADER_DATA.loadMagic to ICD_LOADER_MAGIC.
3655 * - The loader implements vkCreate{PLATFORM}SurfaceKHR() and
3656 * vkDestroySurfaceKHR(). The ICD must be capable of working with
3657 * such loader-managed surfaces.
3658 *
3659 * - Loader interface v2 differs from v1 in:
3660 * - The first ICD entrypoint called by the loader is
3661 * vk_icdNegotiateLoaderICDInterfaceVersion(). The ICD must
3662 * statically expose this entrypoint.
3663 *
3664 * - Loader interface v3 differs from v2 in:
3665 * - The ICD must implement vkCreate{PLATFORM}SurfaceKHR(),
3666 * vkDestroySurfaceKHR(), and other API which uses VKSurfaceKHR,
3667 * because the loader no longer does so.
3668 */
3669 *pSupportedVersion = MIN2(*pSupportedVersion, 3u);
3670 return VK_SUCCESS;
3671 }
3672
3673 VkResult radv_GetMemoryFdKHR(VkDevice _device,
3674 const VkMemoryGetFdInfoKHR *pGetFdInfo,
3675 int *pFD)
3676 {
3677 RADV_FROM_HANDLE(radv_device, device, _device);
3678 RADV_FROM_HANDLE(radv_device_memory, memory, pGetFdInfo->memory);
3679
3680 assert(pGetFdInfo->sType == VK_STRUCTURE_TYPE_MEMORY_GET_FD_INFO_KHR);
3681
3682 /* At the moment, we support only the below handle types. */
3683 assert(pGetFdInfo->handleType ==
3684 VK_EXTERNAL_MEMORY_HANDLE_TYPE_OPAQUE_FD_BIT_KHR ||
3685 pGetFdInfo->handleType ==
3686 VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_EXT);
3687
3688 bool ret = radv_get_memory_fd(device, memory, pFD);
3689 if (ret == false)
3690 return vk_error(VK_ERROR_OUT_OF_DEVICE_MEMORY);
3691 return VK_SUCCESS;
3692 }
3693
3694 VkResult radv_GetMemoryFdPropertiesKHR(VkDevice _device,
3695 VkExternalMemoryHandleTypeFlagBitsKHR handleType,
3696 int fd,
3697 VkMemoryFdPropertiesKHR *pMemoryFdProperties)
3698 {
3699 switch (handleType) {
3700 case VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_EXT:
3701 pMemoryFdProperties->memoryTypeBits = (1 << RADV_MEM_TYPE_COUNT) - 1;
3702 return VK_SUCCESS;
3703
3704 default:
3705 /* The valid usage section for this function says:
3706 *
3707 * "handleType must not be one of the handle types defined as
3708 * opaque."
3709 *
3710 * So opaque handle types fall into the default "unsupported" case.
3711 */
3712 return vk_error(VK_ERROR_INVALID_EXTERNAL_HANDLE_KHR);
3713 }
3714 }
3715
3716 static VkResult radv_import_opaque_fd(struct radv_device *device,
3717 int fd,
3718 uint32_t *syncobj)
3719 {
3720 uint32_t syncobj_handle = 0;
3721 int ret = device->ws->import_syncobj(device->ws, fd, &syncobj_handle);
3722 if (ret != 0)
3723 return vk_error(VK_ERROR_INVALID_EXTERNAL_HANDLE_KHR);
3724
3725 if (*syncobj)
3726 device->ws->destroy_syncobj(device->ws, *syncobj);
3727
3728 *syncobj = syncobj_handle;
3729 close(fd);
3730
3731 return VK_SUCCESS;
3732 }
3733
3734 static VkResult radv_import_sync_fd(struct radv_device *device,
3735 int fd,
3736 uint32_t *syncobj)
3737 {
3738 /* If we create a syncobj we do it locally so that if we have an error, we don't
3739 * leave a syncobj in an undetermined state in the fence. */
3740 uint32_t syncobj_handle = *syncobj;
3741 if (!syncobj_handle) {
3742 int ret = device->ws->create_syncobj(device->ws, &syncobj_handle);
3743 if (ret) {
3744 return vk_error(VK_ERROR_INVALID_EXTERNAL_HANDLE_KHR);
3745 }
3746 }
3747
3748 if (fd == -1) {
3749 device->ws->signal_syncobj(device->ws, syncobj_handle);
3750 } else {
3751 int ret = device->ws->import_syncobj_from_sync_file(device->ws, syncobj_handle, fd);
3752 if (ret != 0)
3753 return vk_error(VK_ERROR_INVALID_EXTERNAL_HANDLE_KHR);
3754 }
3755
3756 *syncobj = syncobj_handle;
3757 if (fd != -1)
3758 close(fd);
3759
3760 return VK_SUCCESS;
3761 }
3762
3763 VkResult radv_ImportSemaphoreFdKHR(VkDevice _device,
3764 const VkImportSemaphoreFdInfoKHR *pImportSemaphoreFdInfo)
3765 {
3766 RADV_FROM_HANDLE(radv_device, device, _device);
3767 RADV_FROM_HANDLE(radv_semaphore, sem, pImportSemaphoreFdInfo->semaphore);
3768 uint32_t *syncobj_dst = NULL;
3769
3770 if (pImportSemaphoreFdInfo->flags & VK_SEMAPHORE_IMPORT_TEMPORARY_BIT_KHR) {
3771 syncobj_dst = &sem->temp_syncobj;
3772 } else {
3773 syncobj_dst = &sem->syncobj;
3774 }
3775
3776 switch(pImportSemaphoreFdInfo->handleType) {
3777 case VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_OPAQUE_FD_BIT_KHR:
3778 return radv_import_opaque_fd(device, pImportSemaphoreFdInfo->fd, syncobj_dst);
3779 case VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_SYNC_FD_BIT_KHR:
3780 return radv_import_sync_fd(device, pImportSemaphoreFdInfo->fd, syncobj_dst);
3781 default:
3782 unreachable("Unhandled semaphore handle type");
3783 }
3784 }
3785
3786 VkResult radv_GetSemaphoreFdKHR(VkDevice _device,
3787 const VkSemaphoreGetFdInfoKHR *pGetFdInfo,
3788 int *pFd)
3789 {
3790 RADV_FROM_HANDLE(radv_device, device, _device);
3791 RADV_FROM_HANDLE(radv_semaphore, sem, pGetFdInfo->semaphore);
3792 int ret;
3793 uint32_t syncobj_handle;
3794
3795 if (sem->temp_syncobj)
3796 syncobj_handle = sem->temp_syncobj;
3797 else
3798 syncobj_handle = sem->syncobj;
3799
3800 switch(pGetFdInfo->handleType) {
3801 case VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_OPAQUE_FD_BIT_KHR:
3802 ret = device->ws->export_syncobj(device->ws, syncobj_handle, pFd);
3803 break;
3804 case VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_SYNC_FD_BIT_KHR:
3805 ret = device->ws->export_syncobj_to_sync_file(device->ws, syncobj_handle, pFd);
3806 break;
3807 default:
3808 unreachable("Unhandled semaphore handle type");
3809 }
3810
3811 if (ret)
3812 return vk_error(VK_ERROR_INVALID_EXTERNAL_HANDLE_KHR);
3813 return VK_SUCCESS;
3814 }
3815
3816 void radv_GetPhysicalDeviceExternalSemaphorePropertiesKHR(
3817 VkPhysicalDevice physicalDevice,
3818 const VkPhysicalDeviceExternalSemaphoreInfoKHR* pExternalSemaphoreInfo,
3819 VkExternalSemaphorePropertiesKHR* pExternalSemaphoreProperties)
3820 {
3821 RADV_FROM_HANDLE(radv_physical_device, pdevice, physicalDevice);
3822
3823 /* Require has_syncobj_wait_for_submit for the syncobj signal ioctl introduced at virtually the same time */
3824 if (pdevice->rad_info.has_syncobj_wait_for_submit &&
3825 (pExternalSemaphoreInfo->handleType == VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_OPAQUE_FD_BIT_KHR ||
3826 pExternalSemaphoreInfo->handleType == VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_SYNC_FD_BIT_KHR)) {
3827 pExternalSemaphoreProperties->exportFromImportedHandleTypes = VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_OPAQUE_FD_BIT_KHR | VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_SYNC_FD_BIT_KHR;
3828 pExternalSemaphoreProperties->compatibleHandleTypes = VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_OPAQUE_FD_BIT_KHR | VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_SYNC_FD_BIT_KHR;
3829 pExternalSemaphoreProperties->externalSemaphoreFeatures = VK_EXTERNAL_SEMAPHORE_FEATURE_EXPORTABLE_BIT_KHR |
3830 VK_EXTERNAL_SEMAPHORE_FEATURE_IMPORTABLE_BIT_KHR;
3831 } else if (pExternalSemaphoreInfo->handleType == VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_OPAQUE_FD_BIT_KHR) {
3832 pExternalSemaphoreProperties->exportFromImportedHandleTypes = VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_OPAQUE_FD_BIT_KHR;
3833 pExternalSemaphoreProperties->compatibleHandleTypes = VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_OPAQUE_FD_BIT_KHR;
3834 pExternalSemaphoreProperties->externalSemaphoreFeatures = VK_EXTERNAL_SEMAPHORE_FEATURE_EXPORTABLE_BIT_KHR |
3835 VK_EXTERNAL_SEMAPHORE_FEATURE_IMPORTABLE_BIT_KHR;
3836 } else {
3837 pExternalSemaphoreProperties->exportFromImportedHandleTypes = 0;
3838 pExternalSemaphoreProperties->compatibleHandleTypes = 0;
3839 pExternalSemaphoreProperties->externalSemaphoreFeatures = 0;
3840 }
3841 }
3842
3843 VkResult radv_ImportFenceFdKHR(VkDevice _device,
3844 const VkImportFenceFdInfoKHR *pImportFenceFdInfo)
3845 {
3846 RADV_FROM_HANDLE(radv_device, device, _device);
3847 RADV_FROM_HANDLE(radv_fence, fence, pImportFenceFdInfo->fence);
3848 uint32_t *syncobj_dst = NULL;
3849
3850
3851 if (pImportFenceFdInfo->flags & VK_FENCE_IMPORT_TEMPORARY_BIT_KHR) {
3852 syncobj_dst = &fence->temp_syncobj;
3853 } else {
3854 syncobj_dst = &fence->syncobj;
3855 }
3856
3857 switch(pImportFenceFdInfo->handleType) {
3858 case VK_EXTERNAL_FENCE_HANDLE_TYPE_OPAQUE_FD_BIT_KHR:
3859 return radv_import_opaque_fd(device, pImportFenceFdInfo->fd, syncobj_dst);
3860 case VK_EXTERNAL_FENCE_HANDLE_TYPE_SYNC_FD_BIT_KHR:
3861 return radv_import_sync_fd(device, pImportFenceFdInfo->fd, syncobj_dst);
3862 default:
3863 unreachable("Unhandled fence handle type");
3864 }
3865 }
3866
3867 VkResult radv_GetFenceFdKHR(VkDevice _device,
3868 const VkFenceGetFdInfoKHR *pGetFdInfo,
3869 int *pFd)
3870 {
3871 RADV_FROM_HANDLE(radv_device, device, _device);
3872 RADV_FROM_HANDLE(radv_fence, fence, pGetFdInfo->fence);
3873 int ret;
3874 uint32_t syncobj_handle;
3875
3876 if (fence->temp_syncobj)
3877 syncobj_handle = fence->temp_syncobj;
3878 else
3879 syncobj_handle = fence->syncobj;
3880
3881 switch(pGetFdInfo->handleType) {
3882 case VK_EXTERNAL_FENCE_HANDLE_TYPE_OPAQUE_FD_BIT_KHR:
3883 ret = device->ws->export_syncobj(device->ws, syncobj_handle, pFd);
3884 break;
3885 case VK_EXTERNAL_FENCE_HANDLE_TYPE_SYNC_FD_BIT_KHR:
3886 ret = device->ws->export_syncobj_to_sync_file(device->ws, syncobj_handle, pFd);
3887 break;
3888 default:
3889 unreachable("Unhandled fence handle type");
3890 }
3891
3892 if (ret)
3893 return vk_error(VK_ERROR_INVALID_EXTERNAL_HANDLE_KHR);
3894 return VK_SUCCESS;
3895 }
3896
3897 void radv_GetPhysicalDeviceExternalFencePropertiesKHR(
3898 VkPhysicalDevice physicalDevice,
3899 const VkPhysicalDeviceExternalFenceInfoKHR* pExternalFenceInfo,
3900 VkExternalFencePropertiesKHR* pExternalFenceProperties)
3901 {
3902 RADV_FROM_HANDLE(radv_physical_device, pdevice, physicalDevice);
3903
3904 if (pdevice->rad_info.has_syncobj_wait_for_submit &&
3905 (pExternalFenceInfo->handleType == VK_EXTERNAL_FENCE_HANDLE_TYPE_OPAQUE_FD_BIT_KHR ||
3906 pExternalFenceInfo->handleType == VK_EXTERNAL_FENCE_HANDLE_TYPE_SYNC_FD_BIT_KHR)) {
3907 pExternalFenceProperties->exportFromImportedHandleTypes = VK_EXTERNAL_FENCE_HANDLE_TYPE_OPAQUE_FD_BIT_KHR | VK_EXTERNAL_FENCE_HANDLE_TYPE_SYNC_FD_BIT_KHR;
3908 pExternalFenceProperties->compatibleHandleTypes = VK_EXTERNAL_FENCE_HANDLE_TYPE_OPAQUE_FD_BIT_KHR | VK_EXTERNAL_FENCE_HANDLE_TYPE_SYNC_FD_BIT_KHR;
3909 pExternalFenceProperties->externalFenceFeatures = VK_EXTERNAL_FENCE_FEATURE_EXPORTABLE_BIT_KHR |
3910 VK_EXTERNAL_SEMAPHORE_FEATURE_IMPORTABLE_BIT_KHR;
3911 } else {
3912 pExternalFenceProperties->exportFromImportedHandleTypes = 0;
3913 pExternalFenceProperties->compatibleHandleTypes = 0;
3914 pExternalFenceProperties->externalFenceFeatures = 0;
3915 }
3916 }