radv/gfx10: disable geometry and tessellation shaders
[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 "git_sha1.h"
47 #include "util/build_id.h"
48 #include "util/debug.h"
49 #include "util/mesa-sha1.h"
50 #include "compiler/glsl_types.h"
51 #include "util/xmlpool.h"
52
53 static int
54 radv_device_get_cache_uuid(enum radeon_family family, void *uuid)
55 {
56 struct mesa_sha1 ctx;
57 unsigned char sha1[20];
58 unsigned ptr_size = sizeof(void*);
59
60 memset(uuid, 0, VK_UUID_SIZE);
61 _mesa_sha1_init(&ctx);
62
63 if (!disk_cache_get_function_identifier(radv_device_get_cache_uuid, &ctx) ||
64 !disk_cache_get_function_identifier(LLVMInitializeAMDGPUTargetInfo, &ctx))
65 return -1;
66
67 _mesa_sha1_update(&ctx, &family, sizeof(family));
68 _mesa_sha1_update(&ctx, &ptr_size, sizeof(ptr_size));
69 _mesa_sha1_final(&ctx, sha1);
70
71 memcpy(uuid, sha1, VK_UUID_SIZE);
72 return 0;
73 }
74
75 static void
76 radv_get_driver_uuid(void *uuid)
77 {
78 ac_compute_driver_uuid(uuid, VK_UUID_SIZE);
79 }
80
81 static void
82 radv_get_device_uuid(struct radeon_info *info, void *uuid)
83 {
84 ac_compute_device_uuid(info, uuid, VK_UUID_SIZE);
85 }
86
87 static void
88 radv_get_device_name(enum radeon_family family, char *name, size_t name_len)
89 {
90 const char *chip_string;
91
92 switch (family) {
93 case CHIP_TAHITI: chip_string = "AMD RADV TAHITI"; break;
94 case CHIP_PITCAIRN: chip_string = "AMD RADV PITCAIRN"; break;
95 case CHIP_VERDE: chip_string = "AMD RADV CAPE VERDE"; break;
96 case CHIP_OLAND: chip_string = "AMD RADV OLAND"; break;
97 case CHIP_HAINAN: chip_string = "AMD RADV HAINAN"; break;
98 case CHIP_BONAIRE: chip_string = "AMD RADV BONAIRE"; break;
99 case CHIP_KAVERI: chip_string = "AMD RADV KAVERI"; break;
100 case CHIP_KABINI: chip_string = "AMD RADV KABINI"; break;
101 case CHIP_HAWAII: chip_string = "AMD RADV HAWAII"; break;
102 case CHIP_TONGA: chip_string = "AMD RADV TONGA"; break;
103 case CHIP_ICELAND: chip_string = "AMD RADV ICELAND"; break;
104 case CHIP_CARRIZO: chip_string = "AMD RADV CARRIZO"; break;
105 case CHIP_FIJI: chip_string = "AMD RADV FIJI"; break;
106 case CHIP_POLARIS10: chip_string = "AMD RADV POLARIS10"; break;
107 case CHIP_POLARIS11: chip_string = "AMD RADV POLARIS11"; break;
108 case CHIP_POLARIS12: chip_string = "AMD RADV POLARIS12"; break;
109 case CHIP_STONEY: chip_string = "AMD RADV STONEY"; break;
110 case CHIP_VEGAM: chip_string = "AMD RADV VEGA M"; break;
111 case CHIP_VEGA10: chip_string = "AMD RADV VEGA10"; break;
112 case CHIP_VEGA12: chip_string = "AMD RADV VEGA12"; break;
113 case CHIP_VEGA20: chip_string = "AMD RADV VEGA20"; break;
114 case CHIP_RAVEN: chip_string = "AMD RADV RAVEN"; break;
115 case CHIP_RAVEN2: chip_string = "AMD RADV RAVEN2"; break;
116 case CHIP_NAVI10: chip_string = "AMD RADV NAVI10"; break;
117 case CHIP_NAVI12: chip_string = "AMD RADV NAVI12"; break;
118 case CHIP_NAVI14: chip_string = "AMD RADV NAVI14"; break;
119 default: chip_string = "AMD RADV unknown"; break;
120 }
121
122 snprintf(name, name_len, "%s (LLVM " MESA_LLVM_VERSION_STRING ")", chip_string);
123 }
124
125 static uint64_t
126 radv_get_visible_vram_size(struct radv_physical_device *device)
127 {
128 return MIN2(device->rad_info.vram_size, device->rad_info.vram_vis_size);
129 }
130
131 static uint64_t
132 radv_get_vram_size(struct radv_physical_device *device)
133 {
134 return device->rad_info.vram_size - radv_get_visible_vram_size(device);
135 }
136
137 static void
138 radv_physical_device_init_mem_types(struct radv_physical_device *device)
139 {
140 STATIC_ASSERT(RADV_MEM_HEAP_COUNT <= VK_MAX_MEMORY_HEAPS);
141 uint64_t visible_vram_size = radv_get_visible_vram_size(device);
142 uint64_t vram_size = radv_get_vram_size(device);
143 int vram_index = -1, visible_vram_index = -1, gart_index = -1;
144 device->memory_properties.memoryHeapCount = 0;
145 if (vram_size > 0) {
146 vram_index = device->memory_properties.memoryHeapCount++;
147 device->memory_properties.memoryHeaps[vram_index] = (VkMemoryHeap) {
148 .size = vram_size,
149 .flags = VK_MEMORY_HEAP_DEVICE_LOCAL_BIT,
150 };
151 }
152 if (visible_vram_size) {
153 visible_vram_index = device->memory_properties.memoryHeapCount++;
154 device->memory_properties.memoryHeaps[visible_vram_index] = (VkMemoryHeap) {
155 .size = visible_vram_size,
156 .flags = VK_MEMORY_HEAP_DEVICE_LOCAL_BIT,
157 };
158 }
159 if (device->rad_info.gart_size > 0) {
160 gart_index = device->memory_properties.memoryHeapCount++;
161 device->memory_properties.memoryHeaps[gart_index] = (VkMemoryHeap) {
162 .size = device->rad_info.gart_size,
163 .flags = device->rad_info.has_dedicated_vram ? 0 : VK_MEMORY_HEAP_DEVICE_LOCAL_BIT,
164 };
165 }
166
167 STATIC_ASSERT(RADV_MEM_TYPE_COUNT <= VK_MAX_MEMORY_TYPES);
168 unsigned type_count = 0;
169 if (vram_index >= 0) {
170 device->mem_type_indices[type_count] = RADV_MEM_TYPE_VRAM;
171 device->memory_properties.memoryTypes[type_count++] = (VkMemoryType) {
172 .propertyFlags = VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT,
173 .heapIndex = vram_index,
174 };
175 }
176 if (gart_index >= 0) {
177 device->mem_type_indices[type_count] = RADV_MEM_TYPE_GTT_WRITE_COMBINE;
178 device->memory_properties.memoryTypes[type_count++] = (VkMemoryType) {
179 .propertyFlags = VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT |
180 VK_MEMORY_PROPERTY_HOST_COHERENT_BIT |
181 (device->rad_info.has_dedicated_vram ? 0 : VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT),
182 .heapIndex = gart_index,
183 };
184 }
185 if (visible_vram_index >= 0) {
186 device->mem_type_indices[type_count] = RADV_MEM_TYPE_VRAM_CPU_ACCESS;
187 device->memory_properties.memoryTypes[type_count++] = (VkMemoryType) {
188 .propertyFlags = VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT |
189 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT |
190 VK_MEMORY_PROPERTY_HOST_COHERENT_BIT,
191 .heapIndex = visible_vram_index,
192 };
193 }
194 if (gart_index >= 0) {
195 device->mem_type_indices[type_count] = RADV_MEM_TYPE_GTT_CACHED;
196 device->memory_properties.memoryTypes[type_count++] = (VkMemoryType) {
197 .propertyFlags = VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT |
198 VK_MEMORY_PROPERTY_HOST_COHERENT_BIT |
199 VK_MEMORY_PROPERTY_HOST_CACHED_BIT |
200 (device->rad_info.has_dedicated_vram ? 0 : VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT),
201 .heapIndex = gart_index,
202 };
203 }
204 device->memory_properties.memoryTypeCount = type_count;
205 }
206
207 static void
208 radv_handle_env_var_force_family(struct radv_physical_device *device)
209 {
210 const char *family = getenv("RADV_FORCE_FAMILY");
211 unsigned i;
212
213 if (!family)
214 return;
215
216 for (i = CHIP_TAHITI; i < CHIP_LAST; i++) {
217 if (!strcmp(family, ac_get_llvm_processor_name(i))) {
218 /* Override family and chip_class. */
219 device->rad_info.family = i;
220
221 if (i >= CHIP_NAVI10)
222 device->rad_info.chip_class = GFX10;
223 else if (i >= CHIP_VEGA10)
224 device->rad_info.chip_class = GFX9;
225 else if (i >= CHIP_TONGA)
226 device->rad_info.chip_class = GFX8;
227 else if (i >= CHIP_BONAIRE)
228 device->rad_info.chip_class = GFX7;
229 else
230 device->rad_info.chip_class = GFX6;
231
232 return;
233 }
234 }
235
236 fprintf(stderr, "radv: Unknown family: %s\n", family);
237 exit(1);
238 }
239
240 static VkResult
241 radv_physical_device_init(struct radv_physical_device *device,
242 struct radv_instance *instance,
243 drmDevicePtr drm_device)
244 {
245 const char *path = drm_device->nodes[DRM_NODE_RENDER];
246 VkResult result;
247 drmVersionPtr version;
248 int fd;
249 int master_fd = -1;
250
251 fd = open(path, O_RDWR | O_CLOEXEC);
252 if (fd < 0) {
253 if (instance->debug_flags & RADV_DEBUG_STARTUP)
254 radv_logi("Could not open device '%s'", path);
255
256 return vk_error(instance, VK_ERROR_INCOMPATIBLE_DRIVER);
257 }
258
259 version = drmGetVersion(fd);
260 if (!version) {
261 close(fd);
262
263 if (instance->debug_flags & RADV_DEBUG_STARTUP)
264 radv_logi("Could not get the kernel driver version for device '%s'", path);
265
266 return vk_errorf(instance, VK_ERROR_INCOMPATIBLE_DRIVER,
267 "failed to get version %s: %m", path);
268 }
269
270 if (strcmp(version->name, "amdgpu")) {
271 drmFreeVersion(version);
272 close(fd);
273
274 if (instance->debug_flags & RADV_DEBUG_STARTUP)
275 radv_logi("Device '%s' is not using the amdgpu kernel driver.", path);
276
277 return VK_ERROR_INCOMPATIBLE_DRIVER;
278 }
279 drmFreeVersion(version);
280
281 if (instance->debug_flags & RADV_DEBUG_STARTUP)
282 radv_logi("Found compatible device '%s'.", path);
283
284 device->_loader_data.loaderMagic = ICD_LOADER_MAGIC;
285 device->instance = instance;
286
287 device->ws = radv_amdgpu_winsys_create(fd, instance->debug_flags,
288 instance->perftest_flags);
289 if (!device->ws) {
290 result = vk_error(instance, VK_ERROR_INCOMPATIBLE_DRIVER);
291 goto fail;
292 }
293
294 if (instance->enabled_extensions.KHR_display) {
295 master_fd = open(drm_device->nodes[DRM_NODE_PRIMARY], O_RDWR | O_CLOEXEC);
296 if (master_fd >= 0) {
297 uint32_t accel_working = 0;
298 struct drm_amdgpu_info request = {
299 .return_pointer = (uintptr_t)&accel_working,
300 .return_size = sizeof(accel_working),
301 .query = AMDGPU_INFO_ACCEL_WORKING
302 };
303
304 if (drmCommandWrite(master_fd, DRM_AMDGPU_INFO, &request, sizeof (struct drm_amdgpu_info)) < 0 || !accel_working) {
305 close(master_fd);
306 master_fd = -1;
307 }
308 }
309 }
310
311 device->master_fd = master_fd;
312 device->local_fd = fd;
313 device->ws->query_info(device->ws, &device->rad_info);
314
315 radv_handle_env_var_force_family(device);
316
317 radv_get_device_name(device->rad_info.family, device->name, sizeof(device->name));
318
319 if (radv_device_get_cache_uuid(device->rad_info.family, device->cache_uuid)) {
320 device->ws->destroy(device->ws);
321 result = vk_errorf(instance, VK_ERROR_INITIALIZATION_FAILED,
322 "cannot generate UUID");
323 goto fail;
324 }
325
326 /* These flags affect shader compilation. */
327 uint64_t shader_env_flags =
328 (device->instance->perftest_flags & RADV_PERFTEST_SISCHED ? 0x1 : 0) |
329 (device->instance->debug_flags & RADV_DEBUG_UNSAFE_MATH ? 0x2 : 0);
330
331 /* The gpu id is already embedded in the uuid so we just pass "radv"
332 * when creating the cache.
333 */
334 char buf[VK_UUID_SIZE * 2 + 1];
335 disk_cache_format_hex_id(buf, device->cache_uuid, VK_UUID_SIZE * 2);
336 device->disk_cache = disk_cache_create(device->name, buf, shader_env_flags);
337
338 if (device->rad_info.chip_class < GFX8 ||
339 device->rad_info.chip_class > GFX9)
340 fprintf(stderr, "WARNING: radv is not a conformant vulkan implementation, testing use only.\n");
341
342 radv_get_driver_uuid(&device->driver_uuid);
343 radv_get_device_uuid(&device->rad_info, &device->device_uuid);
344
345 if (device->rad_info.family == CHIP_STONEY ||
346 device->rad_info.chip_class >= GFX9) {
347 device->has_rbplus = true;
348 device->rbplus_allowed = device->rad_info.family == CHIP_STONEY ||
349 device->rad_info.family == CHIP_VEGA12 ||
350 device->rad_info.family == CHIP_RAVEN ||
351 device->rad_info.family == CHIP_RAVEN2;
352 }
353
354 /* The mere presence of CLEAR_STATE in the IB causes random GPU hangs
355 * on GFX6.
356 */
357 device->has_clear_state = device->rad_info.chip_class >= GFX7 &&
358 device->rad_info.chip_class <= GFX9;
359
360 device->cpdma_prefetch_writes_memory = device->rad_info.chip_class <= GFX8;
361
362 /* Vega10/Raven need a special workaround for a hardware bug. */
363 device->has_scissor_bug = device->rad_info.family == CHIP_VEGA10 ||
364 device->rad_info.family == CHIP_RAVEN;
365
366 /* Out-of-order primitive rasterization. */
367 device->has_out_of_order_rast = device->rad_info.chip_class >= GFX8 &&
368 device->rad_info.max_se >= 2;
369 device->out_of_order_rast_allowed = device->has_out_of_order_rast &&
370 !(device->instance->debug_flags & RADV_DEBUG_NO_OUT_OF_ORDER);
371
372 device->dcc_msaa_allowed =
373 (device->instance->perftest_flags & RADV_PERFTEST_DCC_MSAA);
374
375 /* TODO: Figure out how to use LOAD_CONTEXT_REG on GFX6-GFX7. */
376 device->has_load_ctx_reg_pkt = device->rad_info.chip_class >= GFX9 ||
377 (device->rad_info.chip_class >= GFX8 &&
378 device->rad_info.me_fw_feature >= 41);
379
380 device->has_dcc_constant_encode = device->rad_info.family == CHIP_RAVEN2 ||
381 device->rad_info.chip_class >= GFX10;
382
383 device->use_shader_ballot = device->instance->perftest_flags & RADV_PERFTEST_SHADER_BALLOT;
384
385 radv_physical_device_init_mem_types(device);
386 radv_fill_device_extension_table(device, &device->supported_extensions);
387
388 device->bus_info = *drm_device->businfo.pci;
389
390 if ((device->instance->debug_flags & RADV_DEBUG_INFO))
391 ac_print_gpu_info(&device->rad_info);
392
393 /* The WSI is structured as a layer on top of the driver, so this has
394 * to be the last part of initialization (at least until we get other
395 * semi-layers).
396 */
397 result = radv_init_wsi(device);
398 if (result != VK_SUCCESS) {
399 device->ws->destroy(device->ws);
400 vk_error(instance, result);
401 goto fail;
402 }
403
404 return VK_SUCCESS;
405
406 fail:
407 close(fd);
408 if (master_fd != -1)
409 close(master_fd);
410 return result;
411 }
412
413 static void
414 radv_physical_device_finish(struct radv_physical_device *device)
415 {
416 radv_finish_wsi(device);
417 device->ws->destroy(device->ws);
418 disk_cache_destroy(device->disk_cache);
419 close(device->local_fd);
420 if (device->master_fd != -1)
421 close(device->master_fd);
422 }
423
424 static void *
425 default_alloc_func(void *pUserData, size_t size, size_t align,
426 VkSystemAllocationScope allocationScope)
427 {
428 return malloc(size);
429 }
430
431 static void *
432 default_realloc_func(void *pUserData, void *pOriginal, size_t size,
433 size_t align, VkSystemAllocationScope allocationScope)
434 {
435 return realloc(pOriginal, size);
436 }
437
438 static void
439 default_free_func(void *pUserData, void *pMemory)
440 {
441 free(pMemory);
442 }
443
444 static const VkAllocationCallbacks default_alloc = {
445 .pUserData = NULL,
446 .pfnAllocation = default_alloc_func,
447 .pfnReallocation = default_realloc_func,
448 .pfnFree = default_free_func,
449 };
450
451 static const struct debug_control radv_debug_options[] = {
452 {"nofastclears", RADV_DEBUG_NO_FAST_CLEARS},
453 {"nodcc", RADV_DEBUG_NO_DCC},
454 {"shaders", RADV_DEBUG_DUMP_SHADERS},
455 {"nocache", RADV_DEBUG_NO_CACHE},
456 {"shaderstats", RADV_DEBUG_DUMP_SHADER_STATS},
457 {"nohiz", RADV_DEBUG_NO_HIZ},
458 {"nocompute", RADV_DEBUG_NO_COMPUTE_QUEUE},
459 {"unsafemath", RADV_DEBUG_UNSAFE_MATH},
460 {"allbos", RADV_DEBUG_ALL_BOS},
461 {"noibs", RADV_DEBUG_NO_IBS},
462 {"spirv", RADV_DEBUG_DUMP_SPIRV},
463 {"vmfaults", RADV_DEBUG_VM_FAULTS},
464 {"zerovram", RADV_DEBUG_ZERO_VRAM},
465 {"syncshaders", RADV_DEBUG_SYNC_SHADERS},
466 {"nosisched", RADV_DEBUG_NO_SISCHED},
467 {"preoptir", RADV_DEBUG_PREOPTIR},
468 {"nodynamicbounds", RADV_DEBUG_NO_DYNAMIC_BOUNDS},
469 {"nooutoforder", RADV_DEBUG_NO_OUT_OF_ORDER},
470 {"info", RADV_DEBUG_INFO},
471 {"errors", RADV_DEBUG_ERRORS},
472 {"startup", RADV_DEBUG_STARTUP},
473 {"checkir", RADV_DEBUG_CHECKIR},
474 {"nothreadllvm", RADV_DEBUG_NOTHREADLLVM},
475 {"nobinning", RADV_DEBUG_NOBINNING},
476 {"noloadstoreopt", RADV_DEBUG_NO_LOAD_STORE_OPT},
477 {NULL, 0}
478 };
479
480 const char *
481 radv_get_debug_option_name(int id)
482 {
483 assert(id < ARRAY_SIZE(radv_debug_options) - 1);
484 return radv_debug_options[id].string;
485 }
486
487 static const struct debug_control radv_perftest_options[] = {
488 {"nobatchchain", RADV_PERFTEST_NO_BATCHCHAIN},
489 {"sisched", RADV_PERFTEST_SISCHED},
490 {"localbos", RADV_PERFTEST_LOCAL_BOS},
491 {"dccmsaa", RADV_PERFTEST_DCC_MSAA},
492 {"bolist", RADV_PERFTEST_BO_LIST},
493 {"shader_ballot", RADV_PERFTEST_SHADER_BALLOT},
494 {"tccompatcmask", RADV_PERFTEST_TC_COMPAT_CMASK},
495 {NULL, 0}
496 };
497
498 const char *
499 radv_get_perftest_option_name(int id)
500 {
501 assert(id < ARRAY_SIZE(radv_perftest_options) - 1);
502 return radv_perftest_options[id].string;
503 }
504
505 static void
506 radv_handle_per_app_options(struct radv_instance *instance,
507 const VkApplicationInfo *info)
508 {
509 const char *name = info ? info->pApplicationName : NULL;
510
511 if (!name)
512 return;
513
514 if (!strcmp(name, "Talos - Linux - 32bit") ||
515 !strcmp(name, "Talos - Linux - 64bit")) {
516 if (!(instance->debug_flags & RADV_DEBUG_NO_SISCHED)) {
517 /* Force enable LLVM sisched for Talos because it looks
518 * safe and it gives few more FPS.
519 */
520 instance->perftest_flags |= RADV_PERFTEST_SISCHED;
521 }
522 } else if (!strcmp(name, "DOOM_VFR")) {
523 /* Work around a Doom VFR game bug */
524 instance->debug_flags |= RADV_DEBUG_NO_DYNAMIC_BOUNDS;
525 } else if (!strcmp(name, "MonsterHunterWorld.exe")) {
526 /* Workaround for a WaW hazard when LLVM moves/merges
527 * load/store memory operations.
528 * See https://reviews.llvm.org/D61313
529 */
530 if (HAVE_LLVM < 0x900)
531 instance->debug_flags |= RADV_DEBUG_NO_LOAD_STORE_OPT;
532 }
533 }
534
535 static int radv_get_instance_extension_index(const char *name)
536 {
537 for (unsigned i = 0; i < RADV_INSTANCE_EXTENSION_COUNT; ++i) {
538 if (strcmp(name, radv_instance_extensions[i].extensionName) == 0)
539 return i;
540 }
541 return -1;
542 }
543
544 static const char radv_dri_options_xml[] =
545 DRI_CONF_BEGIN
546 DRI_CONF_SECTION_QUALITY
547 DRI_CONF_ADAPTIVE_SYNC("true")
548 DRI_CONF_SECTION_END
549 DRI_CONF_END;
550
551 static void radv_init_dri_options(struct radv_instance *instance)
552 {
553 driParseOptionInfo(&instance->available_dri_options, radv_dri_options_xml);
554 driParseConfigFiles(&instance->dri_options,
555 &instance->available_dri_options,
556 0, "radv", NULL);
557 }
558
559 VkResult radv_CreateInstance(
560 const VkInstanceCreateInfo* pCreateInfo,
561 const VkAllocationCallbacks* pAllocator,
562 VkInstance* pInstance)
563 {
564 struct radv_instance *instance;
565 VkResult result;
566
567 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO);
568
569 uint32_t client_version;
570 if (pCreateInfo->pApplicationInfo &&
571 pCreateInfo->pApplicationInfo->apiVersion != 0) {
572 client_version = pCreateInfo->pApplicationInfo->apiVersion;
573 } else {
574 client_version = VK_API_VERSION_1_0;
575 }
576
577 instance = vk_zalloc2(&default_alloc, pAllocator, sizeof(*instance), 8,
578 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
579 if (!instance)
580 return vk_error(NULL, VK_ERROR_OUT_OF_HOST_MEMORY);
581
582 instance->_loader_data.loaderMagic = ICD_LOADER_MAGIC;
583
584 if (pAllocator)
585 instance->alloc = *pAllocator;
586 else
587 instance->alloc = default_alloc;
588
589 instance->apiVersion = client_version;
590 instance->physicalDeviceCount = -1;
591
592 instance->debug_flags = parse_debug_string(getenv("RADV_DEBUG"),
593 radv_debug_options);
594
595 instance->perftest_flags = parse_debug_string(getenv("RADV_PERFTEST"),
596 radv_perftest_options);
597
598
599 if (instance->debug_flags & RADV_DEBUG_STARTUP)
600 radv_logi("Created an instance");
601
602 for (uint32_t i = 0; i < pCreateInfo->enabledExtensionCount; i++) {
603 const char *ext_name = pCreateInfo->ppEnabledExtensionNames[i];
604 int index = radv_get_instance_extension_index(ext_name);
605
606 if (index < 0 || !radv_supported_instance_extensions.extensions[index]) {
607 vk_free2(&default_alloc, pAllocator, instance);
608 return vk_error(instance, VK_ERROR_EXTENSION_NOT_PRESENT);
609 }
610
611 instance->enabled_extensions.extensions[index] = true;
612 }
613
614 result = vk_debug_report_instance_init(&instance->debug_report_callbacks);
615 if (result != VK_SUCCESS) {
616 vk_free2(&default_alloc, pAllocator, instance);
617 return vk_error(instance, result);
618 }
619
620 _mesa_locale_init();
621 glsl_type_singleton_init_or_ref();
622
623 VG(VALGRIND_CREATE_MEMPOOL(instance, 0, false));
624
625 radv_init_dri_options(instance);
626 radv_handle_per_app_options(instance, pCreateInfo->pApplicationInfo);
627
628 *pInstance = radv_instance_to_handle(instance);
629
630 return VK_SUCCESS;
631 }
632
633 void radv_DestroyInstance(
634 VkInstance _instance,
635 const VkAllocationCallbacks* pAllocator)
636 {
637 RADV_FROM_HANDLE(radv_instance, instance, _instance);
638
639 if (!instance)
640 return;
641
642 for (int i = 0; i < instance->physicalDeviceCount; ++i) {
643 radv_physical_device_finish(instance->physicalDevices + i);
644 }
645
646 VG(VALGRIND_DESTROY_MEMPOOL(instance));
647
648 glsl_type_singleton_decref();
649 _mesa_locale_fini();
650
651 driDestroyOptionCache(&instance->dri_options);
652 driDestroyOptionInfo(&instance->available_dri_options);
653
654 vk_debug_report_instance_destroy(&instance->debug_report_callbacks);
655
656 vk_free(&instance->alloc, instance);
657 }
658
659 static VkResult
660 radv_enumerate_devices(struct radv_instance *instance)
661 {
662 /* TODO: Check for more devices ? */
663 drmDevicePtr devices[8];
664 VkResult result = VK_ERROR_INCOMPATIBLE_DRIVER;
665 int max_devices;
666
667 instance->physicalDeviceCount = 0;
668
669 max_devices = drmGetDevices2(0, devices, ARRAY_SIZE(devices));
670
671 if (instance->debug_flags & RADV_DEBUG_STARTUP)
672 radv_logi("Found %d drm nodes", max_devices);
673
674 if (max_devices < 1)
675 return vk_error(instance, VK_ERROR_INCOMPATIBLE_DRIVER);
676
677 for (unsigned i = 0; i < (unsigned)max_devices; i++) {
678 if (devices[i]->available_nodes & 1 << DRM_NODE_RENDER &&
679 devices[i]->bustype == DRM_BUS_PCI &&
680 devices[i]->deviceinfo.pci->vendor_id == ATI_VENDOR_ID) {
681
682 result = radv_physical_device_init(instance->physicalDevices +
683 instance->physicalDeviceCount,
684 instance,
685 devices[i]);
686 if (result == VK_SUCCESS)
687 ++instance->physicalDeviceCount;
688 else if (result != VK_ERROR_INCOMPATIBLE_DRIVER)
689 break;
690 }
691 }
692 drmFreeDevices(devices, max_devices);
693
694 return result;
695 }
696
697 VkResult radv_EnumeratePhysicalDevices(
698 VkInstance _instance,
699 uint32_t* pPhysicalDeviceCount,
700 VkPhysicalDevice* pPhysicalDevices)
701 {
702 RADV_FROM_HANDLE(radv_instance, instance, _instance);
703 VkResult result;
704
705 if (instance->physicalDeviceCount < 0) {
706 result = radv_enumerate_devices(instance);
707 if (result != VK_SUCCESS &&
708 result != VK_ERROR_INCOMPATIBLE_DRIVER)
709 return result;
710 }
711
712 if (!pPhysicalDevices) {
713 *pPhysicalDeviceCount = instance->physicalDeviceCount;
714 } else {
715 *pPhysicalDeviceCount = MIN2(*pPhysicalDeviceCount, instance->physicalDeviceCount);
716 for (unsigned i = 0; i < *pPhysicalDeviceCount; ++i)
717 pPhysicalDevices[i] = radv_physical_device_to_handle(instance->physicalDevices + i);
718 }
719
720 return *pPhysicalDeviceCount < instance->physicalDeviceCount ? VK_INCOMPLETE
721 : VK_SUCCESS;
722 }
723
724 VkResult radv_EnumeratePhysicalDeviceGroups(
725 VkInstance _instance,
726 uint32_t* pPhysicalDeviceGroupCount,
727 VkPhysicalDeviceGroupProperties* pPhysicalDeviceGroupProperties)
728 {
729 RADV_FROM_HANDLE(radv_instance, instance, _instance);
730 VkResult result;
731
732 if (instance->physicalDeviceCount < 0) {
733 result = radv_enumerate_devices(instance);
734 if (result != VK_SUCCESS &&
735 result != VK_ERROR_INCOMPATIBLE_DRIVER)
736 return result;
737 }
738
739 if (!pPhysicalDeviceGroupProperties) {
740 *pPhysicalDeviceGroupCount = instance->physicalDeviceCount;
741 } else {
742 *pPhysicalDeviceGroupCount = MIN2(*pPhysicalDeviceGroupCount, instance->physicalDeviceCount);
743 for (unsigned i = 0; i < *pPhysicalDeviceGroupCount; ++i) {
744 pPhysicalDeviceGroupProperties[i].physicalDeviceCount = 1;
745 pPhysicalDeviceGroupProperties[i].physicalDevices[0] = radv_physical_device_to_handle(instance->physicalDevices + i);
746 pPhysicalDeviceGroupProperties[i].subsetAllocation = false;
747 }
748 }
749 return *pPhysicalDeviceGroupCount < instance->physicalDeviceCount ? VK_INCOMPLETE
750 : VK_SUCCESS;
751 }
752
753 void radv_GetPhysicalDeviceFeatures(
754 VkPhysicalDevice physicalDevice,
755 VkPhysicalDeviceFeatures* pFeatures)
756 {
757 RADV_FROM_HANDLE(radv_physical_device, pdevice, physicalDevice);
758 memset(pFeatures, 0, sizeof(*pFeatures));
759
760 *pFeatures = (VkPhysicalDeviceFeatures) {
761 .robustBufferAccess = true,
762 .fullDrawIndexUint32 = true,
763 .imageCubeArray = true,
764 .independentBlend = true,
765 .geometryShader = pdevice->rad_info.chip_class < GFX10,
766 .tessellationShader = pdevice->rad_info.chip_class < GFX10,
767 .sampleRateShading = true,
768 .dualSrcBlend = true,
769 .logicOp = true,
770 .multiDrawIndirect = true,
771 .drawIndirectFirstInstance = true,
772 .depthClamp = true,
773 .depthBiasClamp = true,
774 .fillModeNonSolid = true,
775 .depthBounds = true,
776 .wideLines = true,
777 .largePoints = true,
778 .alphaToOne = true,
779 .multiViewport = true,
780 .samplerAnisotropy = true,
781 .textureCompressionETC2 = radv_device_supports_etc(pdevice),
782 .textureCompressionASTC_LDR = false,
783 .textureCompressionBC = true,
784 .occlusionQueryPrecise = true,
785 .pipelineStatisticsQuery = true,
786 .vertexPipelineStoresAndAtomics = true,
787 .fragmentStoresAndAtomics = true,
788 .shaderTessellationAndGeometryPointSize = true,
789 .shaderImageGatherExtended = true,
790 .shaderStorageImageExtendedFormats = true,
791 .shaderStorageImageMultisample = pdevice->rad_info.chip_class >= GFX8,
792 .shaderUniformBufferArrayDynamicIndexing = true,
793 .shaderSampledImageArrayDynamicIndexing = true,
794 .shaderStorageBufferArrayDynamicIndexing = true,
795 .shaderStorageImageArrayDynamicIndexing = true,
796 .shaderStorageImageReadWithoutFormat = true,
797 .shaderStorageImageWriteWithoutFormat = true,
798 .shaderClipDistance = true,
799 .shaderCullDistance = true,
800 .shaderFloat64 = true,
801 .shaderInt64 = true,
802 .shaderInt16 = pdevice->rad_info.chip_class >= GFX9,
803 .sparseBinding = true,
804 .variableMultisampleRate = true,
805 .inheritedQueries = true,
806 };
807 }
808
809 void radv_GetPhysicalDeviceFeatures2(
810 VkPhysicalDevice physicalDevice,
811 VkPhysicalDeviceFeatures2 *pFeatures)
812 {
813 RADV_FROM_HANDLE(radv_physical_device, pdevice, physicalDevice);
814 vk_foreach_struct(ext, pFeatures->pNext) {
815 switch (ext->sType) {
816 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VARIABLE_POINTERS_FEATURES: {
817 VkPhysicalDeviceVariablePointersFeatures *features = (void *)ext;
818 features->variablePointersStorageBuffer = true;
819 features->variablePointers = true;
820 break;
821 }
822 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_MULTIVIEW_FEATURES: {
823 VkPhysicalDeviceMultiviewFeatures *features = (VkPhysicalDeviceMultiviewFeatures*)ext;
824 features->multiview = true;
825 features->multiviewGeometryShader = true;
826 features->multiviewTessellationShader = true;
827 break;
828 }
829 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SHADER_DRAW_PARAMETERS_FEATURES: {
830 VkPhysicalDeviceShaderDrawParametersFeatures *features =
831 (VkPhysicalDeviceShaderDrawParametersFeatures*)ext;
832 features->shaderDrawParameters = true;
833 break;
834 }
835 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PROTECTED_MEMORY_FEATURES: {
836 VkPhysicalDeviceProtectedMemoryFeatures *features =
837 (VkPhysicalDeviceProtectedMemoryFeatures*)ext;
838 features->protectedMemory = false;
839 break;
840 }
841 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_16BIT_STORAGE_FEATURES: {
842 VkPhysicalDevice16BitStorageFeatures *features =
843 (VkPhysicalDevice16BitStorageFeatures*)ext;
844 bool enabled = pdevice->rad_info.chip_class >= GFX8;
845 features->storageBuffer16BitAccess = enabled;
846 features->uniformAndStorageBuffer16BitAccess = enabled;
847 features->storagePushConstant16 = enabled;
848 features->storageInputOutput16 = enabled && HAVE_LLVM >= 0x900;
849 break;
850 }
851 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SAMPLER_YCBCR_CONVERSION_FEATURES: {
852 VkPhysicalDeviceSamplerYcbcrConversionFeatures *features =
853 (VkPhysicalDeviceSamplerYcbcrConversionFeatures*)ext;
854 features->samplerYcbcrConversion = true;
855 break;
856 }
857 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_DESCRIPTOR_INDEXING_FEATURES_EXT: {
858 VkPhysicalDeviceDescriptorIndexingFeaturesEXT *features =
859 (VkPhysicalDeviceDescriptorIndexingFeaturesEXT*)ext;
860 features->shaderInputAttachmentArrayDynamicIndexing = true;
861 features->shaderUniformTexelBufferArrayDynamicIndexing = true;
862 features->shaderStorageTexelBufferArrayDynamicIndexing = true;
863 features->shaderUniformBufferArrayNonUniformIndexing = true;
864 features->shaderSampledImageArrayNonUniformIndexing = true;
865 features->shaderStorageBufferArrayNonUniformIndexing = true;
866 features->shaderStorageImageArrayNonUniformIndexing = true;
867 features->shaderInputAttachmentArrayNonUniformIndexing = true;
868 features->shaderUniformTexelBufferArrayNonUniformIndexing = true;
869 features->shaderStorageTexelBufferArrayNonUniformIndexing = true;
870 features->descriptorBindingUniformBufferUpdateAfterBind = true;
871 features->descriptorBindingSampledImageUpdateAfterBind = true;
872 features->descriptorBindingStorageImageUpdateAfterBind = true;
873 features->descriptorBindingStorageBufferUpdateAfterBind = true;
874 features->descriptorBindingUniformTexelBufferUpdateAfterBind = true;
875 features->descriptorBindingStorageTexelBufferUpdateAfterBind = true;
876 features->descriptorBindingUpdateUnusedWhilePending = true;
877 features->descriptorBindingPartiallyBound = true;
878 features->descriptorBindingVariableDescriptorCount = true;
879 features->runtimeDescriptorArray = true;
880 break;
881 }
882 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_CONDITIONAL_RENDERING_FEATURES_EXT: {
883 VkPhysicalDeviceConditionalRenderingFeaturesEXT *features =
884 (VkPhysicalDeviceConditionalRenderingFeaturesEXT*)ext;
885 features->conditionalRendering = true;
886 features->inheritedConditionalRendering = false;
887 break;
888 }
889 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VERTEX_ATTRIBUTE_DIVISOR_FEATURES_EXT: {
890 VkPhysicalDeviceVertexAttributeDivisorFeaturesEXT *features =
891 (VkPhysicalDeviceVertexAttributeDivisorFeaturesEXT *)ext;
892 features->vertexAttributeInstanceRateDivisor = VK_TRUE;
893 features->vertexAttributeInstanceRateZeroDivisor = VK_TRUE;
894 break;
895 }
896 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_TRANSFORM_FEEDBACK_FEATURES_EXT: {
897 VkPhysicalDeviceTransformFeedbackFeaturesEXT *features =
898 (VkPhysicalDeviceTransformFeedbackFeaturesEXT*)ext;
899 features->transformFeedback = true;
900 features->geometryStreams = true;
901 break;
902 }
903 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SCALAR_BLOCK_LAYOUT_FEATURES_EXT: {
904 VkPhysicalDeviceScalarBlockLayoutFeaturesEXT *features =
905 (VkPhysicalDeviceScalarBlockLayoutFeaturesEXT *)ext;
906 features->scalarBlockLayout = pdevice->rad_info.chip_class >= GFX7;
907 break;
908 }
909 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_MEMORY_PRIORITY_FEATURES_EXT: {
910 VkPhysicalDeviceMemoryPriorityFeaturesEXT *features =
911 (VkPhysicalDeviceMemoryPriorityFeaturesEXT *)ext;
912 features->memoryPriority = VK_TRUE;
913 break;
914 }
915 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_BUFFER_DEVICE_ADDRESS_FEATURES_EXT: {
916 VkPhysicalDeviceBufferDeviceAddressFeaturesEXT *features =
917 (VkPhysicalDeviceBufferDeviceAddressFeaturesEXT *)ext;
918 features->bufferDeviceAddress = true;
919 features->bufferDeviceAddressCaptureReplay = false;
920 features->bufferDeviceAddressMultiDevice = false;
921 break;
922 }
923 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_DEPTH_CLIP_ENABLE_FEATURES_EXT: {
924 VkPhysicalDeviceDepthClipEnableFeaturesEXT *features =
925 (VkPhysicalDeviceDepthClipEnableFeaturesEXT *)ext;
926 features->depthClipEnable = true;
927 break;
928 }
929 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_HOST_QUERY_RESET_FEATURES_EXT: {
930 VkPhysicalDeviceHostQueryResetFeaturesEXT *features =
931 (VkPhysicalDeviceHostQueryResetFeaturesEXT *)ext;
932 features->hostQueryReset = true;
933 break;
934 }
935 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_8BIT_STORAGE_FEATURES_KHR: {
936 VkPhysicalDevice8BitStorageFeaturesKHR *features =
937 (VkPhysicalDevice8BitStorageFeaturesKHR*)ext;
938 bool enabled = pdevice->rad_info.chip_class >= GFX8;
939 features->storageBuffer8BitAccess = enabled;
940 features->uniformAndStorageBuffer8BitAccess = enabled;
941 features->storagePushConstant8 = enabled;
942 break;
943 }
944 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FLOAT16_INT8_FEATURES_KHR: {
945 VkPhysicalDeviceFloat16Int8FeaturesKHR *features =
946 (VkPhysicalDeviceFloat16Int8FeaturesKHR*)ext;
947 features->shaderFloat16 = pdevice->rad_info.chip_class >= GFX8 && HAVE_LLVM >= 0x0800;
948 features->shaderInt8 = true;
949 break;
950 }
951 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SHADER_ATOMIC_INT64_FEATURES_KHR: {
952 VkPhysicalDeviceShaderAtomicInt64FeaturesKHR *features =
953 (VkPhysicalDeviceShaderAtomicInt64FeaturesKHR *)ext;
954 /* TODO: Enable this once the driver supports 64-bit
955 * compare&swap atomic operations.
956 */
957 features->shaderBufferInt64Atomics = false;
958 features->shaderSharedInt64Atomics = false;
959 break;
960 }
961 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_INLINE_UNIFORM_BLOCK_FEATURES_EXT: {
962 VkPhysicalDeviceInlineUniformBlockFeaturesEXT *features =
963 (VkPhysicalDeviceInlineUniformBlockFeaturesEXT *)ext;
964
965 features->inlineUniformBlock = true;
966 features->descriptorBindingInlineUniformBlockUpdateAfterBind = true;
967 break;
968 }
969 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_COMPUTE_SHADER_DERIVATIVES_FEATURES_NV: {
970 VkPhysicalDeviceComputeShaderDerivativesFeaturesNV *features =
971 (VkPhysicalDeviceComputeShaderDerivativesFeaturesNV *)ext;
972 features->computeDerivativeGroupQuads = false;
973 features->computeDerivativeGroupLinear = true;
974 break;
975 }
976 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_YCBCR_IMAGE_ARRAYS_FEATURES_EXT: {
977 VkPhysicalDeviceYcbcrImageArraysFeaturesEXT *features =
978 (VkPhysicalDeviceYcbcrImageArraysFeaturesEXT*)ext;
979 features->ycbcrImageArrays = true;
980 break;
981 }
982 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_UNIFORM_BUFFER_STANDARD_LAYOUT_FEATURES_KHR: {
983 VkPhysicalDeviceUniformBufferStandardLayoutFeaturesKHR *features =
984 (VkPhysicalDeviceUniformBufferStandardLayoutFeaturesKHR *)ext;
985 features->uniformBufferStandardLayout = true;
986 break;
987 }
988 default:
989 break;
990 }
991 }
992 return radv_GetPhysicalDeviceFeatures(physicalDevice, &pFeatures->features);
993 }
994
995 void radv_GetPhysicalDeviceProperties(
996 VkPhysicalDevice physicalDevice,
997 VkPhysicalDeviceProperties* pProperties)
998 {
999 RADV_FROM_HANDLE(radv_physical_device, pdevice, physicalDevice);
1000 VkSampleCountFlags sample_counts = 0xf;
1001
1002 /* make sure that the entire descriptor set is addressable with a signed
1003 * 32-bit int. So the sum of all limits scaled by descriptor size has to
1004 * be at most 2 GiB. the combined image & samples object count as one of
1005 * both. This limit is for the pipeline layout, not for the set layout, but
1006 * there is no set limit, so we just set a pipeline limit. I don't think
1007 * any app is going to hit this soon. */
1008 size_t max_descriptor_set_size = ((1ull << 31) - 16 * MAX_DYNAMIC_BUFFERS) /
1009 (32 /* uniform buffer, 32 due to potential space wasted on alignment */ +
1010 32 /* storage buffer, 32 due to potential space wasted on alignment */ +
1011 32 /* sampler, largest when combined with image */ +
1012 64 /* sampled image */ +
1013 64 /* storage image */);
1014
1015 VkPhysicalDeviceLimits limits = {
1016 .maxImageDimension1D = (1 << 14),
1017 .maxImageDimension2D = (1 << 14),
1018 .maxImageDimension3D = (1 << 11),
1019 .maxImageDimensionCube = (1 << 14),
1020 .maxImageArrayLayers = (1 << 11),
1021 .maxTexelBufferElements = 128 * 1024 * 1024,
1022 .maxUniformBufferRange = UINT32_MAX,
1023 .maxStorageBufferRange = UINT32_MAX,
1024 .maxPushConstantsSize = MAX_PUSH_CONSTANTS_SIZE,
1025 .maxMemoryAllocationCount = UINT32_MAX,
1026 .maxSamplerAllocationCount = 64 * 1024,
1027 .bufferImageGranularity = 64, /* A cache line */
1028 .sparseAddressSpaceSize = 0xffffffffu, /* buffer max size */
1029 .maxBoundDescriptorSets = MAX_SETS,
1030 .maxPerStageDescriptorSamplers = max_descriptor_set_size,
1031 .maxPerStageDescriptorUniformBuffers = max_descriptor_set_size,
1032 .maxPerStageDescriptorStorageBuffers = max_descriptor_set_size,
1033 .maxPerStageDescriptorSampledImages = max_descriptor_set_size,
1034 .maxPerStageDescriptorStorageImages = max_descriptor_set_size,
1035 .maxPerStageDescriptorInputAttachments = max_descriptor_set_size,
1036 .maxPerStageResources = max_descriptor_set_size,
1037 .maxDescriptorSetSamplers = max_descriptor_set_size,
1038 .maxDescriptorSetUniformBuffers = max_descriptor_set_size,
1039 .maxDescriptorSetUniformBuffersDynamic = MAX_DYNAMIC_UNIFORM_BUFFERS,
1040 .maxDescriptorSetStorageBuffers = max_descriptor_set_size,
1041 .maxDescriptorSetStorageBuffersDynamic = MAX_DYNAMIC_STORAGE_BUFFERS,
1042 .maxDescriptorSetSampledImages = max_descriptor_set_size,
1043 .maxDescriptorSetStorageImages = max_descriptor_set_size,
1044 .maxDescriptorSetInputAttachments = max_descriptor_set_size,
1045 .maxVertexInputAttributes = MAX_VERTEX_ATTRIBS,
1046 .maxVertexInputBindings = MAX_VBS,
1047 .maxVertexInputAttributeOffset = 2047,
1048 .maxVertexInputBindingStride = 2048,
1049 .maxVertexOutputComponents = 128,
1050 .maxTessellationGenerationLevel = 64,
1051 .maxTessellationPatchSize = 32,
1052 .maxTessellationControlPerVertexInputComponents = 128,
1053 .maxTessellationControlPerVertexOutputComponents = 128,
1054 .maxTessellationControlPerPatchOutputComponents = 120,
1055 .maxTessellationControlTotalOutputComponents = 4096,
1056 .maxTessellationEvaluationInputComponents = 128,
1057 .maxTessellationEvaluationOutputComponents = 128,
1058 .maxGeometryShaderInvocations = 127,
1059 .maxGeometryInputComponents = 64,
1060 .maxGeometryOutputComponents = 128,
1061 .maxGeometryOutputVertices = 256,
1062 .maxGeometryTotalOutputComponents = 1024,
1063 .maxFragmentInputComponents = 128,
1064 .maxFragmentOutputAttachments = 8,
1065 .maxFragmentDualSrcAttachments = 1,
1066 .maxFragmentCombinedOutputResources = 8,
1067 .maxComputeSharedMemorySize = 32768,
1068 .maxComputeWorkGroupCount = { 65535, 65535, 65535 },
1069 .maxComputeWorkGroupInvocations = 2048,
1070 .maxComputeWorkGroupSize = {
1071 2048,
1072 2048,
1073 2048
1074 },
1075 .subPixelPrecisionBits = 8,
1076 .subTexelPrecisionBits = 8,
1077 .mipmapPrecisionBits = 8,
1078 .maxDrawIndexedIndexValue = UINT32_MAX,
1079 .maxDrawIndirectCount = UINT32_MAX,
1080 .maxSamplerLodBias = 16,
1081 .maxSamplerAnisotropy = 16,
1082 .maxViewports = MAX_VIEWPORTS,
1083 .maxViewportDimensions = { (1 << 14), (1 << 14) },
1084 .viewportBoundsRange = { INT16_MIN, INT16_MAX },
1085 .viewportSubPixelBits = 8,
1086 .minMemoryMapAlignment = 4096, /* A page */
1087 .minTexelBufferOffsetAlignment = 1,
1088 .minUniformBufferOffsetAlignment = 4,
1089 .minStorageBufferOffsetAlignment = 4,
1090 .minTexelOffset = -32,
1091 .maxTexelOffset = 31,
1092 .minTexelGatherOffset = -32,
1093 .maxTexelGatherOffset = 31,
1094 .minInterpolationOffset = -2,
1095 .maxInterpolationOffset = 2,
1096 .subPixelInterpolationOffsetBits = 8,
1097 .maxFramebufferWidth = (1 << 14),
1098 .maxFramebufferHeight = (1 << 14),
1099 .maxFramebufferLayers = (1 << 10),
1100 .framebufferColorSampleCounts = sample_counts,
1101 .framebufferDepthSampleCounts = sample_counts,
1102 .framebufferStencilSampleCounts = sample_counts,
1103 .framebufferNoAttachmentsSampleCounts = sample_counts,
1104 .maxColorAttachments = MAX_RTS,
1105 .sampledImageColorSampleCounts = sample_counts,
1106 .sampledImageIntegerSampleCounts = VK_SAMPLE_COUNT_1_BIT,
1107 .sampledImageDepthSampleCounts = sample_counts,
1108 .sampledImageStencilSampleCounts = sample_counts,
1109 .storageImageSampleCounts = pdevice->rad_info.chip_class >= GFX8 ? sample_counts : VK_SAMPLE_COUNT_1_BIT,
1110 .maxSampleMaskWords = 1,
1111 .timestampComputeAndGraphics = true,
1112 .timestampPeriod = 1000000.0 / pdevice->rad_info.clock_crystal_freq,
1113 .maxClipDistances = 8,
1114 .maxCullDistances = 8,
1115 .maxCombinedClipAndCullDistances = 8,
1116 .discreteQueuePriorities = 2,
1117 .pointSizeRange = { 0.0, 8192.0 },
1118 .lineWidthRange = { 0.0, 7.9921875 },
1119 .pointSizeGranularity = (1.0 / 8.0),
1120 .lineWidthGranularity = (1.0 / 128.0),
1121 .strictLines = false, /* FINISHME */
1122 .standardSampleLocations = true,
1123 .optimalBufferCopyOffsetAlignment = 128,
1124 .optimalBufferCopyRowPitchAlignment = 128,
1125 .nonCoherentAtomSize = 64,
1126 };
1127
1128 *pProperties = (VkPhysicalDeviceProperties) {
1129 .apiVersion = radv_physical_device_api_version(pdevice),
1130 .driverVersion = vk_get_driver_version(),
1131 .vendorID = ATI_VENDOR_ID,
1132 .deviceID = pdevice->rad_info.pci_id,
1133 .deviceType = pdevice->rad_info.has_dedicated_vram ? VK_PHYSICAL_DEVICE_TYPE_DISCRETE_GPU : VK_PHYSICAL_DEVICE_TYPE_INTEGRATED_GPU,
1134 .limits = limits,
1135 .sparseProperties = {0},
1136 };
1137
1138 strcpy(pProperties->deviceName, pdevice->name);
1139 memcpy(pProperties->pipelineCacheUUID, pdevice->cache_uuid, VK_UUID_SIZE);
1140 }
1141
1142 void radv_GetPhysicalDeviceProperties2(
1143 VkPhysicalDevice physicalDevice,
1144 VkPhysicalDeviceProperties2 *pProperties)
1145 {
1146 RADV_FROM_HANDLE(radv_physical_device, pdevice, physicalDevice);
1147 radv_GetPhysicalDeviceProperties(physicalDevice, &pProperties->properties);
1148
1149 vk_foreach_struct(ext, pProperties->pNext) {
1150 switch (ext->sType) {
1151 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PUSH_DESCRIPTOR_PROPERTIES_KHR: {
1152 VkPhysicalDevicePushDescriptorPropertiesKHR *properties =
1153 (VkPhysicalDevicePushDescriptorPropertiesKHR *) ext;
1154 properties->maxPushDescriptors = MAX_PUSH_DESCRIPTORS;
1155 break;
1156 }
1157 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_ID_PROPERTIES: {
1158 VkPhysicalDeviceIDProperties *properties = (VkPhysicalDeviceIDProperties*)ext;
1159 memcpy(properties->driverUUID, pdevice->driver_uuid, VK_UUID_SIZE);
1160 memcpy(properties->deviceUUID, pdevice->device_uuid, VK_UUID_SIZE);
1161 properties->deviceLUIDValid = false;
1162 break;
1163 }
1164 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_MULTIVIEW_PROPERTIES: {
1165 VkPhysicalDeviceMultiviewProperties *properties = (VkPhysicalDeviceMultiviewProperties*)ext;
1166 properties->maxMultiviewViewCount = MAX_VIEWS;
1167 properties->maxMultiviewInstanceIndex = INT_MAX;
1168 break;
1169 }
1170 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_POINT_CLIPPING_PROPERTIES: {
1171 VkPhysicalDevicePointClippingProperties *properties =
1172 (VkPhysicalDevicePointClippingProperties*)ext;
1173 properties->pointClippingBehavior = VK_POINT_CLIPPING_BEHAVIOR_ALL_CLIP_PLANES;
1174 break;
1175 }
1176 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_DISCARD_RECTANGLE_PROPERTIES_EXT: {
1177 VkPhysicalDeviceDiscardRectanglePropertiesEXT *properties =
1178 (VkPhysicalDeviceDiscardRectanglePropertiesEXT*)ext;
1179 properties->maxDiscardRectangles = MAX_DISCARD_RECTANGLES;
1180 break;
1181 }
1182 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_EXTERNAL_MEMORY_HOST_PROPERTIES_EXT: {
1183 VkPhysicalDeviceExternalMemoryHostPropertiesEXT *properties =
1184 (VkPhysicalDeviceExternalMemoryHostPropertiesEXT *) ext;
1185 properties->minImportedHostPointerAlignment = 4096;
1186 break;
1187 }
1188 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SUBGROUP_PROPERTIES: {
1189 VkPhysicalDeviceSubgroupProperties *properties =
1190 (VkPhysicalDeviceSubgroupProperties*)ext;
1191 properties->subgroupSize = 64;
1192 properties->supportedStages = VK_SHADER_STAGE_ALL;
1193 properties->supportedOperations =
1194 VK_SUBGROUP_FEATURE_BASIC_BIT |
1195 VK_SUBGROUP_FEATURE_BALLOT_BIT |
1196 VK_SUBGROUP_FEATURE_QUAD_BIT |
1197 VK_SUBGROUP_FEATURE_VOTE_BIT;
1198 if (pdevice->rad_info.chip_class >= GFX8) {
1199 properties->supportedOperations |=
1200 VK_SUBGROUP_FEATURE_ARITHMETIC_BIT |
1201 VK_SUBGROUP_FEATURE_SHUFFLE_BIT |
1202 VK_SUBGROUP_FEATURE_SHUFFLE_RELATIVE_BIT;
1203 }
1204 properties->quadOperationsInAllStages = true;
1205 break;
1206 }
1207 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_MAINTENANCE_3_PROPERTIES: {
1208 VkPhysicalDeviceMaintenance3Properties *properties =
1209 (VkPhysicalDeviceMaintenance3Properties*)ext;
1210 /* Make sure everything is addressable by a signed 32-bit int, and
1211 * our largest descriptors are 96 bytes. */
1212 properties->maxPerSetDescriptors = (1ull << 31) / 96;
1213 /* Our buffer size fields allow only this much */
1214 properties->maxMemoryAllocationSize = 0xFFFFFFFFull;
1215 break;
1216 }
1217 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SAMPLER_FILTER_MINMAX_PROPERTIES_EXT: {
1218 VkPhysicalDeviceSamplerFilterMinmaxPropertiesEXT *properties =
1219 (VkPhysicalDeviceSamplerFilterMinmaxPropertiesEXT *)ext;
1220 /* GFX6-8 only support single channel min/max filter. */
1221 properties->filterMinmaxImageComponentMapping = pdevice->rad_info.chip_class >= GFX9;
1222 properties->filterMinmaxSingleComponentFormats = true;
1223 break;
1224 }
1225 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SHADER_CORE_PROPERTIES_AMD: {
1226 VkPhysicalDeviceShaderCorePropertiesAMD *properties =
1227 (VkPhysicalDeviceShaderCorePropertiesAMD *)ext;
1228
1229 /* Shader engines. */
1230 properties->shaderEngineCount =
1231 pdevice->rad_info.max_se;
1232 properties->shaderArraysPerEngineCount =
1233 pdevice->rad_info.max_sh_per_se;
1234 properties->computeUnitsPerShaderArray =
1235 pdevice->rad_info.num_good_cu_per_sh;
1236 properties->simdPerComputeUnit = 4;
1237 properties->wavefrontsPerSimd =
1238 pdevice->rad_info.family == CHIP_TONGA ||
1239 pdevice->rad_info.family == CHIP_ICELAND ||
1240 pdevice->rad_info.family == CHIP_POLARIS10 ||
1241 pdevice->rad_info.family == CHIP_POLARIS11 ||
1242 pdevice->rad_info.family == CHIP_POLARIS12 ||
1243 pdevice->rad_info.family == CHIP_VEGAM ? 8 : 10;
1244 properties->wavefrontSize = 64;
1245
1246 /* SGPR. */
1247 properties->sgprsPerSimd =
1248 ac_get_num_physical_sgprs(pdevice->rad_info.chip_class);
1249 properties->minSgprAllocation =
1250 pdevice->rad_info.chip_class >= GFX8 ? 16 : 8;
1251 properties->maxSgprAllocation =
1252 pdevice->rad_info.family == CHIP_TONGA ||
1253 pdevice->rad_info.family == CHIP_ICELAND ? 96 : 104;
1254 properties->sgprAllocationGranularity =
1255 pdevice->rad_info.chip_class >= GFX8 ? 16 : 8;
1256
1257 /* VGPR. */
1258 properties->vgprsPerSimd = RADV_NUM_PHYSICAL_VGPRS;
1259 properties->minVgprAllocation = 4;
1260 properties->maxVgprAllocation = 256;
1261 properties->vgprAllocationGranularity = 4;
1262 break;
1263 }
1264 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VERTEX_ATTRIBUTE_DIVISOR_PROPERTIES_EXT: {
1265 VkPhysicalDeviceVertexAttributeDivisorPropertiesEXT *properties =
1266 (VkPhysicalDeviceVertexAttributeDivisorPropertiesEXT *)ext;
1267 properties->maxVertexAttribDivisor = UINT32_MAX;
1268 break;
1269 }
1270 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_DESCRIPTOR_INDEXING_PROPERTIES_EXT: {
1271 VkPhysicalDeviceDescriptorIndexingPropertiesEXT *properties =
1272 (VkPhysicalDeviceDescriptorIndexingPropertiesEXT*)ext;
1273 properties->maxUpdateAfterBindDescriptorsInAllPools = UINT32_MAX / 64;
1274 properties->shaderUniformBufferArrayNonUniformIndexingNative = false;
1275 properties->shaderSampledImageArrayNonUniformIndexingNative = false;
1276 properties->shaderStorageBufferArrayNonUniformIndexingNative = false;
1277 properties->shaderStorageImageArrayNonUniformIndexingNative = false;
1278 properties->shaderInputAttachmentArrayNonUniformIndexingNative = false;
1279 properties->robustBufferAccessUpdateAfterBind = false;
1280 properties->quadDivergentImplicitLod = false;
1281
1282 size_t max_descriptor_set_size = ((1ull << 31) - 16 * MAX_DYNAMIC_BUFFERS -
1283 MAX_INLINE_UNIFORM_BLOCK_SIZE * MAX_INLINE_UNIFORM_BLOCK_COUNT) /
1284 (32 /* uniform buffer, 32 due to potential space wasted on alignment */ +
1285 32 /* storage buffer, 32 due to potential space wasted on alignment */ +
1286 32 /* sampler, largest when combined with image */ +
1287 64 /* sampled image */ +
1288 64 /* storage image */);
1289 properties->maxPerStageDescriptorUpdateAfterBindSamplers = max_descriptor_set_size;
1290 properties->maxPerStageDescriptorUpdateAfterBindUniformBuffers = max_descriptor_set_size;
1291 properties->maxPerStageDescriptorUpdateAfterBindStorageBuffers = max_descriptor_set_size;
1292 properties->maxPerStageDescriptorUpdateAfterBindSampledImages = max_descriptor_set_size;
1293 properties->maxPerStageDescriptorUpdateAfterBindStorageImages = max_descriptor_set_size;
1294 properties->maxPerStageDescriptorUpdateAfterBindInputAttachments = max_descriptor_set_size;
1295 properties->maxPerStageUpdateAfterBindResources = max_descriptor_set_size;
1296 properties->maxDescriptorSetUpdateAfterBindSamplers = max_descriptor_set_size;
1297 properties->maxDescriptorSetUpdateAfterBindUniformBuffers = max_descriptor_set_size;
1298 properties->maxDescriptorSetUpdateAfterBindUniformBuffersDynamic = MAX_DYNAMIC_UNIFORM_BUFFERS;
1299 properties->maxDescriptorSetUpdateAfterBindStorageBuffers = max_descriptor_set_size;
1300 properties->maxDescriptorSetUpdateAfterBindStorageBuffersDynamic = MAX_DYNAMIC_STORAGE_BUFFERS;
1301 properties->maxDescriptorSetUpdateAfterBindSampledImages = max_descriptor_set_size;
1302 properties->maxDescriptorSetUpdateAfterBindStorageImages = max_descriptor_set_size;
1303 properties->maxDescriptorSetUpdateAfterBindInputAttachments = max_descriptor_set_size;
1304 break;
1305 }
1306 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PROTECTED_MEMORY_PROPERTIES: {
1307 VkPhysicalDeviceProtectedMemoryProperties *properties =
1308 (VkPhysicalDeviceProtectedMemoryProperties *)ext;
1309 properties->protectedNoFault = false;
1310 break;
1311 }
1312 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_CONSERVATIVE_RASTERIZATION_PROPERTIES_EXT: {
1313 VkPhysicalDeviceConservativeRasterizationPropertiesEXT *properties =
1314 (VkPhysicalDeviceConservativeRasterizationPropertiesEXT *)ext;
1315 properties->primitiveOverestimationSize = 0;
1316 properties->maxExtraPrimitiveOverestimationSize = 0;
1317 properties->extraPrimitiveOverestimationSizeGranularity = 0;
1318 properties->primitiveUnderestimation = VK_FALSE;
1319 properties->conservativePointAndLineRasterization = VK_FALSE;
1320 properties->degenerateTrianglesRasterized = VK_FALSE;
1321 properties->degenerateLinesRasterized = VK_FALSE;
1322 properties->fullyCoveredFragmentShaderInputVariable = VK_FALSE;
1323 properties->conservativeRasterizationPostDepthCoverage = VK_FALSE;
1324 break;
1325 }
1326 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PCI_BUS_INFO_PROPERTIES_EXT: {
1327 VkPhysicalDevicePCIBusInfoPropertiesEXT *properties =
1328 (VkPhysicalDevicePCIBusInfoPropertiesEXT *)ext;
1329 properties->pciDomain = pdevice->bus_info.domain;
1330 properties->pciBus = pdevice->bus_info.bus;
1331 properties->pciDevice = pdevice->bus_info.dev;
1332 properties->pciFunction = pdevice->bus_info.func;
1333 break;
1334 }
1335 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_DRIVER_PROPERTIES_KHR: {
1336 VkPhysicalDeviceDriverPropertiesKHR *driver_props =
1337 (VkPhysicalDeviceDriverPropertiesKHR *) ext;
1338
1339 driver_props->driverID = VK_DRIVER_ID_MESA_RADV_KHR;
1340 memset(driver_props->driverName, 0, VK_MAX_DRIVER_NAME_SIZE_KHR);
1341 strcpy(driver_props->driverName, "radv");
1342
1343 memset(driver_props->driverInfo, 0, VK_MAX_DRIVER_INFO_SIZE_KHR);
1344 snprintf(driver_props->driverInfo, VK_MAX_DRIVER_INFO_SIZE_KHR,
1345 "Mesa " PACKAGE_VERSION MESA_GIT_SHA1
1346 " (LLVM " MESA_LLVM_VERSION_STRING ")");
1347
1348 driver_props->conformanceVersion = (VkConformanceVersionKHR) {
1349 .major = 1,
1350 .minor = 1,
1351 .subminor = 2,
1352 .patch = 0,
1353 };
1354 break;
1355 }
1356 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_TRANSFORM_FEEDBACK_PROPERTIES_EXT: {
1357 VkPhysicalDeviceTransformFeedbackPropertiesEXT *properties =
1358 (VkPhysicalDeviceTransformFeedbackPropertiesEXT *)ext;
1359 properties->maxTransformFeedbackStreams = MAX_SO_STREAMS;
1360 properties->maxTransformFeedbackBuffers = MAX_SO_BUFFERS;
1361 properties->maxTransformFeedbackBufferSize = UINT32_MAX;
1362 properties->maxTransformFeedbackStreamDataSize = 512;
1363 properties->maxTransformFeedbackBufferDataSize = UINT32_MAX;
1364 properties->maxTransformFeedbackBufferDataStride = 512;
1365 properties->transformFeedbackQueries = true;
1366 properties->transformFeedbackStreamsLinesTriangles = true;
1367 properties->transformFeedbackRasterizationStreamSelect = false;
1368 properties->transformFeedbackDraw = true;
1369 break;
1370 }
1371 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_INLINE_UNIFORM_BLOCK_PROPERTIES_EXT: {
1372 VkPhysicalDeviceInlineUniformBlockPropertiesEXT *props =
1373 (VkPhysicalDeviceInlineUniformBlockPropertiesEXT *)ext;
1374
1375 props->maxInlineUniformBlockSize = MAX_INLINE_UNIFORM_BLOCK_SIZE;
1376 props->maxPerStageDescriptorInlineUniformBlocks = MAX_INLINE_UNIFORM_BLOCK_SIZE * MAX_SETS;
1377 props->maxPerStageDescriptorUpdateAfterBindInlineUniformBlocks = MAX_INLINE_UNIFORM_BLOCK_SIZE * MAX_SETS;
1378 props->maxDescriptorSetInlineUniformBlocks = MAX_INLINE_UNIFORM_BLOCK_COUNT;
1379 props->maxDescriptorSetUpdateAfterBindInlineUniformBlocks = MAX_INLINE_UNIFORM_BLOCK_COUNT;
1380 break;
1381 }
1382 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SAMPLE_LOCATIONS_PROPERTIES_EXT: {
1383 VkPhysicalDeviceSampleLocationsPropertiesEXT *properties =
1384 (VkPhysicalDeviceSampleLocationsPropertiesEXT *)ext;
1385 properties->sampleLocationSampleCounts = VK_SAMPLE_COUNT_2_BIT |
1386 VK_SAMPLE_COUNT_4_BIT |
1387 VK_SAMPLE_COUNT_8_BIT;
1388 properties->maxSampleLocationGridSize = (VkExtent2D){ 2 , 2 };
1389 properties->sampleLocationCoordinateRange[0] = 0.0f;
1390 properties->sampleLocationCoordinateRange[1] = 0.9375f;
1391 properties->sampleLocationSubPixelBits = 4;
1392 properties->variableSampleLocations = VK_FALSE;
1393 break;
1394 }
1395 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_DEPTH_STENCIL_RESOLVE_PROPERTIES_KHR: {
1396 VkPhysicalDeviceDepthStencilResolvePropertiesKHR *properties =
1397 (VkPhysicalDeviceDepthStencilResolvePropertiesKHR *)ext;
1398
1399 /* We support all of the depth resolve modes */
1400 properties->supportedDepthResolveModes =
1401 VK_RESOLVE_MODE_SAMPLE_ZERO_BIT_KHR |
1402 VK_RESOLVE_MODE_AVERAGE_BIT_KHR |
1403 VK_RESOLVE_MODE_MIN_BIT_KHR |
1404 VK_RESOLVE_MODE_MAX_BIT_KHR;
1405
1406 /* Average doesn't make sense for stencil so we don't support that */
1407 properties->supportedStencilResolveModes =
1408 VK_RESOLVE_MODE_SAMPLE_ZERO_BIT_KHR |
1409 VK_RESOLVE_MODE_MIN_BIT_KHR |
1410 VK_RESOLVE_MODE_MAX_BIT_KHR;
1411
1412 properties->independentResolveNone = VK_TRUE;
1413 properties->independentResolve = VK_TRUE;
1414 break;
1415 }
1416 default:
1417 break;
1418 }
1419 }
1420 }
1421
1422 static void radv_get_physical_device_queue_family_properties(
1423 struct radv_physical_device* pdevice,
1424 uint32_t* pCount,
1425 VkQueueFamilyProperties** pQueueFamilyProperties)
1426 {
1427 int num_queue_families = 1;
1428 int idx;
1429 if (pdevice->rad_info.num_compute_rings > 0 &&
1430 !(pdevice->instance->debug_flags & RADV_DEBUG_NO_COMPUTE_QUEUE))
1431 num_queue_families++;
1432
1433 if (pQueueFamilyProperties == NULL) {
1434 *pCount = num_queue_families;
1435 return;
1436 }
1437
1438 if (!*pCount)
1439 return;
1440
1441 idx = 0;
1442 if (*pCount >= 1) {
1443 *pQueueFamilyProperties[idx] = (VkQueueFamilyProperties) {
1444 .queueFlags = VK_QUEUE_GRAPHICS_BIT |
1445 VK_QUEUE_COMPUTE_BIT |
1446 VK_QUEUE_TRANSFER_BIT |
1447 VK_QUEUE_SPARSE_BINDING_BIT,
1448 .queueCount = 1,
1449 .timestampValidBits = 64,
1450 .minImageTransferGranularity = (VkExtent3D) { 1, 1, 1 },
1451 };
1452 idx++;
1453 }
1454
1455 if (pdevice->rad_info.num_compute_rings > 0 &&
1456 !(pdevice->instance->debug_flags & RADV_DEBUG_NO_COMPUTE_QUEUE)) {
1457 if (*pCount > idx) {
1458 *pQueueFamilyProperties[idx] = (VkQueueFamilyProperties) {
1459 .queueFlags = VK_QUEUE_COMPUTE_BIT |
1460 VK_QUEUE_TRANSFER_BIT |
1461 VK_QUEUE_SPARSE_BINDING_BIT,
1462 .queueCount = pdevice->rad_info.num_compute_rings,
1463 .timestampValidBits = 64,
1464 .minImageTransferGranularity = (VkExtent3D) { 1, 1, 1 },
1465 };
1466 idx++;
1467 }
1468 }
1469 *pCount = idx;
1470 }
1471
1472 void radv_GetPhysicalDeviceQueueFamilyProperties(
1473 VkPhysicalDevice physicalDevice,
1474 uint32_t* pCount,
1475 VkQueueFamilyProperties* pQueueFamilyProperties)
1476 {
1477 RADV_FROM_HANDLE(radv_physical_device, pdevice, physicalDevice);
1478 if (!pQueueFamilyProperties) {
1479 radv_get_physical_device_queue_family_properties(pdevice, pCount, NULL);
1480 return;
1481 }
1482 VkQueueFamilyProperties *properties[] = {
1483 pQueueFamilyProperties + 0,
1484 pQueueFamilyProperties + 1,
1485 pQueueFamilyProperties + 2,
1486 };
1487 radv_get_physical_device_queue_family_properties(pdevice, pCount, properties);
1488 assert(*pCount <= 3);
1489 }
1490
1491 void radv_GetPhysicalDeviceQueueFamilyProperties2(
1492 VkPhysicalDevice physicalDevice,
1493 uint32_t* pCount,
1494 VkQueueFamilyProperties2 *pQueueFamilyProperties)
1495 {
1496 RADV_FROM_HANDLE(radv_physical_device, pdevice, physicalDevice);
1497 if (!pQueueFamilyProperties) {
1498 radv_get_physical_device_queue_family_properties(pdevice, pCount, NULL);
1499 return;
1500 }
1501 VkQueueFamilyProperties *properties[] = {
1502 &pQueueFamilyProperties[0].queueFamilyProperties,
1503 &pQueueFamilyProperties[1].queueFamilyProperties,
1504 &pQueueFamilyProperties[2].queueFamilyProperties,
1505 };
1506 radv_get_physical_device_queue_family_properties(pdevice, pCount, properties);
1507 assert(*pCount <= 3);
1508 }
1509
1510 void radv_GetPhysicalDeviceMemoryProperties(
1511 VkPhysicalDevice physicalDevice,
1512 VkPhysicalDeviceMemoryProperties *pMemoryProperties)
1513 {
1514 RADV_FROM_HANDLE(radv_physical_device, physical_device, physicalDevice);
1515
1516 *pMemoryProperties = physical_device->memory_properties;
1517 }
1518
1519 static void
1520 radv_get_memory_budget_properties(VkPhysicalDevice physicalDevice,
1521 VkPhysicalDeviceMemoryBudgetPropertiesEXT *memoryBudget)
1522 {
1523 RADV_FROM_HANDLE(radv_physical_device, device, physicalDevice);
1524 VkPhysicalDeviceMemoryProperties *memory_properties = &device->memory_properties;
1525 uint64_t visible_vram_size = radv_get_visible_vram_size(device);
1526 uint64_t vram_size = radv_get_vram_size(device);
1527 uint64_t gtt_size = device->rad_info.gart_size;
1528 uint64_t heap_budget, heap_usage;
1529
1530 /* For all memory heaps, the computation of budget is as follow:
1531 * heap_budget = heap_size - global_heap_usage + app_heap_usage
1532 *
1533 * The Vulkan spec 1.1.97 says that the budget should include any
1534 * currently allocated device memory.
1535 *
1536 * Note that the application heap usages are not really accurate (eg.
1537 * in presence of shared buffers).
1538 */
1539 for (int i = 0; i < device->memory_properties.memoryTypeCount; i++) {
1540 uint32_t heap_index = device->memory_properties.memoryTypes[i].heapIndex;
1541
1542 switch (device->mem_type_indices[i]) {
1543 case RADV_MEM_TYPE_VRAM:
1544 heap_usage = device->ws->query_value(device->ws,
1545 RADEON_ALLOCATED_VRAM);
1546
1547 heap_budget = vram_size -
1548 device->ws->query_value(device->ws, RADEON_VRAM_USAGE) +
1549 heap_usage;
1550
1551 memoryBudget->heapBudget[heap_index] = heap_budget;
1552 memoryBudget->heapUsage[heap_index] = heap_usage;
1553 break;
1554 case RADV_MEM_TYPE_VRAM_CPU_ACCESS:
1555 heap_usage = device->ws->query_value(device->ws,
1556 RADEON_ALLOCATED_VRAM_VIS);
1557
1558 heap_budget = visible_vram_size -
1559 device->ws->query_value(device->ws, RADEON_VRAM_VIS_USAGE) +
1560 heap_usage;
1561
1562 memoryBudget->heapBudget[heap_index] = heap_budget;
1563 memoryBudget->heapUsage[heap_index] = heap_usage;
1564 break;
1565 case RADV_MEM_TYPE_GTT_WRITE_COMBINE:
1566 heap_usage = device->ws->query_value(device->ws,
1567 RADEON_ALLOCATED_GTT);
1568
1569 heap_budget = gtt_size -
1570 device->ws->query_value(device->ws, RADEON_GTT_USAGE) +
1571 heap_usage;
1572
1573 memoryBudget->heapBudget[heap_index] = heap_budget;
1574 memoryBudget->heapUsage[heap_index] = heap_usage;
1575 break;
1576 default:
1577 break;
1578 }
1579 }
1580
1581 /* The heapBudget and heapUsage values must be zero for array elements
1582 * greater than or equal to
1583 * VkPhysicalDeviceMemoryProperties::memoryHeapCount.
1584 */
1585 for (uint32_t i = memory_properties->memoryHeapCount; i < VK_MAX_MEMORY_HEAPS; i++) {
1586 memoryBudget->heapBudget[i] = 0;
1587 memoryBudget->heapUsage[i] = 0;
1588 }
1589 }
1590
1591 void radv_GetPhysicalDeviceMemoryProperties2(
1592 VkPhysicalDevice physicalDevice,
1593 VkPhysicalDeviceMemoryProperties2 *pMemoryProperties)
1594 {
1595 radv_GetPhysicalDeviceMemoryProperties(physicalDevice,
1596 &pMemoryProperties->memoryProperties);
1597
1598 VkPhysicalDeviceMemoryBudgetPropertiesEXT *memory_budget =
1599 vk_find_struct(pMemoryProperties->pNext,
1600 PHYSICAL_DEVICE_MEMORY_BUDGET_PROPERTIES_EXT);
1601 if (memory_budget)
1602 radv_get_memory_budget_properties(physicalDevice, memory_budget);
1603 }
1604
1605 VkResult radv_GetMemoryHostPointerPropertiesEXT(
1606 VkDevice _device,
1607 VkExternalMemoryHandleTypeFlagBits handleType,
1608 const void *pHostPointer,
1609 VkMemoryHostPointerPropertiesEXT *pMemoryHostPointerProperties)
1610 {
1611 RADV_FROM_HANDLE(radv_device, device, _device);
1612
1613 switch (handleType)
1614 {
1615 case VK_EXTERNAL_MEMORY_HANDLE_TYPE_HOST_ALLOCATION_BIT_EXT: {
1616 const struct radv_physical_device *physical_device = device->physical_device;
1617 uint32_t memoryTypeBits = 0;
1618 for (int i = 0; i < physical_device->memory_properties.memoryTypeCount; i++) {
1619 if (physical_device->mem_type_indices[i] == RADV_MEM_TYPE_GTT_CACHED) {
1620 memoryTypeBits = (1 << i);
1621 break;
1622 }
1623 }
1624 pMemoryHostPointerProperties->memoryTypeBits = memoryTypeBits;
1625 return VK_SUCCESS;
1626 }
1627 default:
1628 return VK_ERROR_INVALID_EXTERNAL_HANDLE;
1629 }
1630 }
1631
1632 static enum radeon_ctx_priority
1633 radv_get_queue_global_priority(const VkDeviceQueueGlobalPriorityCreateInfoEXT *pObj)
1634 {
1635 /* Default to MEDIUM when a specific global priority isn't requested */
1636 if (!pObj)
1637 return RADEON_CTX_PRIORITY_MEDIUM;
1638
1639 switch(pObj->globalPriority) {
1640 case VK_QUEUE_GLOBAL_PRIORITY_REALTIME_EXT:
1641 return RADEON_CTX_PRIORITY_REALTIME;
1642 case VK_QUEUE_GLOBAL_PRIORITY_HIGH_EXT:
1643 return RADEON_CTX_PRIORITY_HIGH;
1644 case VK_QUEUE_GLOBAL_PRIORITY_MEDIUM_EXT:
1645 return RADEON_CTX_PRIORITY_MEDIUM;
1646 case VK_QUEUE_GLOBAL_PRIORITY_LOW_EXT:
1647 return RADEON_CTX_PRIORITY_LOW;
1648 default:
1649 unreachable("Illegal global priority value");
1650 return RADEON_CTX_PRIORITY_INVALID;
1651 }
1652 }
1653
1654 static int
1655 radv_queue_init(struct radv_device *device, struct radv_queue *queue,
1656 uint32_t queue_family_index, int idx,
1657 VkDeviceQueueCreateFlags flags,
1658 const VkDeviceQueueGlobalPriorityCreateInfoEXT *global_priority)
1659 {
1660 queue->_loader_data.loaderMagic = ICD_LOADER_MAGIC;
1661 queue->device = device;
1662 queue->queue_family_index = queue_family_index;
1663 queue->queue_idx = idx;
1664 queue->priority = radv_get_queue_global_priority(global_priority);
1665 queue->flags = flags;
1666
1667 queue->hw_ctx = device->ws->ctx_create(device->ws, queue->priority);
1668 if (!queue->hw_ctx)
1669 return vk_error(device->instance, VK_ERROR_OUT_OF_HOST_MEMORY);
1670
1671 return VK_SUCCESS;
1672 }
1673
1674 static void
1675 radv_queue_finish(struct radv_queue *queue)
1676 {
1677 if (queue->hw_ctx)
1678 queue->device->ws->ctx_destroy(queue->hw_ctx);
1679
1680 if (queue->initial_full_flush_preamble_cs)
1681 queue->device->ws->cs_destroy(queue->initial_full_flush_preamble_cs);
1682 if (queue->initial_preamble_cs)
1683 queue->device->ws->cs_destroy(queue->initial_preamble_cs);
1684 if (queue->continue_preamble_cs)
1685 queue->device->ws->cs_destroy(queue->continue_preamble_cs);
1686 if (queue->descriptor_bo)
1687 queue->device->ws->buffer_destroy(queue->descriptor_bo);
1688 if (queue->scratch_bo)
1689 queue->device->ws->buffer_destroy(queue->scratch_bo);
1690 if (queue->esgs_ring_bo)
1691 queue->device->ws->buffer_destroy(queue->esgs_ring_bo);
1692 if (queue->gsvs_ring_bo)
1693 queue->device->ws->buffer_destroy(queue->gsvs_ring_bo);
1694 if (queue->tess_rings_bo)
1695 queue->device->ws->buffer_destroy(queue->tess_rings_bo);
1696 if (queue->compute_scratch_bo)
1697 queue->device->ws->buffer_destroy(queue->compute_scratch_bo);
1698 }
1699
1700 static void
1701 radv_bo_list_init(struct radv_bo_list *bo_list)
1702 {
1703 pthread_mutex_init(&bo_list->mutex, NULL);
1704 bo_list->list.count = bo_list->capacity = 0;
1705 bo_list->list.bos = NULL;
1706 }
1707
1708 static void
1709 radv_bo_list_finish(struct radv_bo_list *bo_list)
1710 {
1711 free(bo_list->list.bos);
1712 pthread_mutex_destroy(&bo_list->mutex);
1713 }
1714
1715 static VkResult radv_bo_list_add(struct radv_device *device,
1716 struct radeon_winsys_bo *bo)
1717 {
1718 struct radv_bo_list *bo_list = &device->bo_list;
1719
1720 if (bo->is_local)
1721 return VK_SUCCESS;
1722
1723 if (unlikely(!device->use_global_bo_list))
1724 return VK_SUCCESS;
1725
1726 pthread_mutex_lock(&bo_list->mutex);
1727 if (bo_list->list.count == bo_list->capacity) {
1728 unsigned capacity = MAX2(4, bo_list->capacity * 2);
1729 void *data = realloc(bo_list->list.bos, capacity * sizeof(struct radeon_winsys_bo*));
1730
1731 if (!data) {
1732 pthread_mutex_unlock(&bo_list->mutex);
1733 return VK_ERROR_OUT_OF_HOST_MEMORY;
1734 }
1735
1736 bo_list->list.bos = (struct radeon_winsys_bo**)data;
1737 bo_list->capacity = capacity;
1738 }
1739
1740 bo_list->list.bos[bo_list->list.count++] = bo;
1741 pthread_mutex_unlock(&bo_list->mutex);
1742 return VK_SUCCESS;
1743 }
1744
1745 static void radv_bo_list_remove(struct radv_device *device,
1746 struct radeon_winsys_bo *bo)
1747 {
1748 struct radv_bo_list *bo_list = &device->bo_list;
1749
1750 if (bo->is_local)
1751 return;
1752
1753 if (unlikely(!device->use_global_bo_list))
1754 return;
1755
1756 pthread_mutex_lock(&bo_list->mutex);
1757 for(unsigned i = 0; i < bo_list->list.count; ++i) {
1758 if (bo_list->list.bos[i] == bo) {
1759 bo_list->list.bos[i] = bo_list->list.bos[bo_list->list.count - 1];
1760 --bo_list->list.count;
1761 break;
1762 }
1763 }
1764 pthread_mutex_unlock(&bo_list->mutex);
1765 }
1766
1767 static void
1768 radv_device_init_gs_info(struct radv_device *device)
1769 {
1770 device->gs_table_depth = ac_get_gs_table_depth(device->physical_device->rad_info.chip_class,
1771 device->physical_device->rad_info.family);
1772 }
1773
1774 static int radv_get_device_extension_index(const char *name)
1775 {
1776 for (unsigned i = 0; i < RADV_DEVICE_EXTENSION_COUNT; ++i) {
1777 if (strcmp(name, radv_device_extensions[i].extensionName) == 0)
1778 return i;
1779 }
1780 return -1;
1781 }
1782
1783 static int
1784 radv_get_int_debug_option(const char *name, int default_value)
1785 {
1786 const char *str;
1787 int result;
1788
1789 str = getenv(name);
1790 if (!str) {
1791 result = default_value;
1792 } else {
1793 char *endptr;
1794
1795 result = strtol(str, &endptr, 0);
1796 if (str == endptr) {
1797 /* No digits founs. */
1798 result = default_value;
1799 }
1800 }
1801
1802 return result;
1803 }
1804
1805 VkResult radv_CreateDevice(
1806 VkPhysicalDevice physicalDevice,
1807 const VkDeviceCreateInfo* pCreateInfo,
1808 const VkAllocationCallbacks* pAllocator,
1809 VkDevice* pDevice)
1810 {
1811 RADV_FROM_HANDLE(radv_physical_device, physical_device, physicalDevice);
1812 VkResult result;
1813 struct radv_device *device;
1814
1815 bool keep_shader_info = false;
1816
1817 /* Check enabled features */
1818 if (pCreateInfo->pEnabledFeatures) {
1819 VkPhysicalDeviceFeatures supported_features;
1820 radv_GetPhysicalDeviceFeatures(physicalDevice, &supported_features);
1821 VkBool32 *supported_feature = (VkBool32 *)&supported_features;
1822 VkBool32 *enabled_feature = (VkBool32 *)pCreateInfo->pEnabledFeatures;
1823 unsigned num_features = sizeof(VkPhysicalDeviceFeatures) / sizeof(VkBool32);
1824 for (uint32_t i = 0; i < num_features; i++) {
1825 if (enabled_feature[i] && !supported_feature[i])
1826 return vk_error(physical_device->instance, VK_ERROR_FEATURE_NOT_PRESENT);
1827 }
1828 }
1829
1830 device = vk_zalloc2(&physical_device->instance->alloc, pAllocator,
1831 sizeof(*device), 8,
1832 VK_SYSTEM_ALLOCATION_SCOPE_DEVICE);
1833 if (!device)
1834 return vk_error(physical_device->instance, VK_ERROR_OUT_OF_HOST_MEMORY);
1835
1836 device->_loader_data.loaderMagic = ICD_LOADER_MAGIC;
1837 device->instance = physical_device->instance;
1838 device->physical_device = physical_device;
1839
1840 device->ws = physical_device->ws;
1841 if (pAllocator)
1842 device->alloc = *pAllocator;
1843 else
1844 device->alloc = physical_device->instance->alloc;
1845
1846 for (uint32_t i = 0; i < pCreateInfo->enabledExtensionCount; i++) {
1847 const char *ext_name = pCreateInfo->ppEnabledExtensionNames[i];
1848 int index = radv_get_device_extension_index(ext_name);
1849 if (index < 0 || !physical_device->supported_extensions.extensions[index]) {
1850 vk_free(&device->alloc, device);
1851 return vk_error(physical_device->instance, VK_ERROR_EXTENSION_NOT_PRESENT);
1852 }
1853
1854 device->enabled_extensions.extensions[index] = true;
1855 }
1856
1857 keep_shader_info = device->enabled_extensions.AMD_shader_info;
1858
1859 /* With update after bind we can't attach bo's to the command buffer
1860 * from the descriptor set anymore, so we have to use a global BO list.
1861 */
1862 device->use_global_bo_list =
1863 (device->instance->perftest_flags & RADV_PERFTEST_BO_LIST) ||
1864 device->enabled_extensions.EXT_descriptor_indexing ||
1865 device->enabled_extensions.EXT_buffer_device_address;
1866
1867 mtx_init(&device->shader_slab_mutex, mtx_plain);
1868 list_inithead(&device->shader_slabs);
1869
1870 radv_bo_list_init(&device->bo_list);
1871
1872 for (unsigned i = 0; i < pCreateInfo->queueCreateInfoCount; i++) {
1873 const VkDeviceQueueCreateInfo *queue_create = &pCreateInfo->pQueueCreateInfos[i];
1874 uint32_t qfi = queue_create->queueFamilyIndex;
1875 const VkDeviceQueueGlobalPriorityCreateInfoEXT *global_priority =
1876 vk_find_struct_const(queue_create->pNext, DEVICE_QUEUE_GLOBAL_PRIORITY_CREATE_INFO_EXT);
1877
1878 assert(!global_priority || device->physical_device->rad_info.has_ctx_priority);
1879
1880 device->queues[qfi] = vk_alloc(&device->alloc,
1881 queue_create->queueCount * sizeof(struct radv_queue), 8, VK_SYSTEM_ALLOCATION_SCOPE_DEVICE);
1882 if (!device->queues[qfi]) {
1883 result = VK_ERROR_OUT_OF_HOST_MEMORY;
1884 goto fail;
1885 }
1886
1887 memset(device->queues[qfi], 0, queue_create->queueCount * sizeof(struct radv_queue));
1888
1889 device->queue_count[qfi] = queue_create->queueCount;
1890
1891 for (unsigned q = 0; q < queue_create->queueCount; q++) {
1892 result = radv_queue_init(device, &device->queues[qfi][q],
1893 qfi, q, queue_create->flags,
1894 global_priority);
1895 if (result != VK_SUCCESS)
1896 goto fail;
1897 }
1898 }
1899
1900 /* TODO: Enable binning for GFX10. */
1901 device->pbb_allowed = device->physical_device->rad_info.chip_class == GFX9 &&
1902 !(device->instance->debug_flags & RADV_DEBUG_NOBINNING);
1903
1904 /* Disabled and not implemented for now. */
1905 device->dfsm_allowed = device->pbb_allowed &&
1906 (device->physical_device->rad_info.family == CHIP_RAVEN ||
1907 device->physical_device->rad_info.family == CHIP_RAVEN2);
1908
1909 #ifdef ANDROID
1910 device->always_use_syncobj = device->physical_device->rad_info.has_syncobj_wait_for_submit;
1911 #endif
1912
1913 /* The maximum number of scratch waves. Scratch space isn't divided
1914 * evenly between CUs. The number is only a function of the number of CUs.
1915 * We can decrease the constant to decrease the scratch buffer size.
1916 *
1917 * sctx->scratch_waves must be >= the maximum possible size of
1918 * 1 threadgroup, so that the hw doesn't hang from being unable
1919 * to start any.
1920 *
1921 * The recommended value is 4 per CU at most. Higher numbers don't
1922 * bring much benefit, but they still occupy chip resources (think
1923 * async compute). I've seen ~2% performance difference between 4 and 32.
1924 */
1925 uint32_t max_threads_per_block = 2048;
1926 device->scratch_waves = MAX2(32 * physical_device->rad_info.num_good_compute_units,
1927 max_threads_per_block / 64);
1928
1929 device->dispatch_initiator = S_00B800_COMPUTE_SHADER_EN(1);
1930
1931 if (device->physical_device->rad_info.chip_class >= GFX7) {
1932 /* If the KMD allows it (there is a KMD hw register for it),
1933 * allow launching waves out-of-order.
1934 */
1935 device->dispatch_initiator |= S_00B800_ORDER_MODE(1);
1936 }
1937
1938 radv_device_init_gs_info(device);
1939
1940 device->tess_offchip_block_dw_size =
1941 device->physical_device->rad_info.family == CHIP_HAWAII ? 4096 : 8192;
1942 device->has_distributed_tess =
1943 device->physical_device->rad_info.chip_class >= GFX8 &&
1944 device->physical_device->rad_info.max_se >= 2;
1945
1946 if (getenv("RADV_TRACE_FILE")) {
1947 const char *filename = getenv("RADV_TRACE_FILE");
1948
1949 keep_shader_info = true;
1950
1951 if (!radv_init_trace(device))
1952 goto fail;
1953
1954 fprintf(stderr, "*****************************************************************************\n");
1955 fprintf(stderr, "* WARNING: RADV_TRACE_FILE is costly and should only be used for debugging! *\n");
1956 fprintf(stderr, "*****************************************************************************\n");
1957
1958 fprintf(stderr, "Trace file will be dumped to %s\n", filename);
1959 radv_dump_enabled_options(device, stderr);
1960 }
1961
1962 device->keep_shader_info = keep_shader_info;
1963
1964 result = radv_device_init_meta(device);
1965 if (result != VK_SUCCESS)
1966 goto fail;
1967
1968 radv_device_init_msaa(device);
1969
1970 for (int family = 0; family < RADV_MAX_QUEUE_FAMILIES; ++family) {
1971 device->empty_cs[family] = device->ws->cs_create(device->ws, family);
1972 switch (family) {
1973 case RADV_QUEUE_GENERAL:
1974 radeon_emit(device->empty_cs[family], PKT3(PKT3_CONTEXT_CONTROL, 1, 0));
1975 radeon_emit(device->empty_cs[family], CONTEXT_CONTROL_LOAD_ENABLE(1));
1976 radeon_emit(device->empty_cs[family], CONTEXT_CONTROL_SHADOW_ENABLE(1));
1977 break;
1978 case RADV_QUEUE_COMPUTE:
1979 radeon_emit(device->empty_cs[family], PKT3(PKT3_NOP, 0, 0));
1980 radeon_emit(device->empty_cs[family], 0);
1981 break;
1982 }
1983 device->ws->cs_finalize(device->empty_cs[family]);
1984 }
1985
1986 if (device->physical_device->rad_info.chip_class >= GFX7)
1987 cik_create_gfx_config(device);
1988
1989 VkPipelineCacheCreateInfo ci;
1990 ci.sType = VK_STRUCTURE_TYPE_PIPELINE_CACHE_CREATE_INFO;
1991 ci.pNext = NULL;
1992 ci.flags = 0;
1993 ci.pInitialData = NULL;
1994 ci.initialDataSize = 0;
1995 VkPipelineCache pc;
1996 result = radv_CreatePipelineCache(radv_device_to_handle(device),
1997 &ci, NULL, &pc);
1998 if (result != VK_SUCCESS)
1999 goto fail_meta;
2000
2001 device->mem_cache = radv_pipeline_cache_from_handle(pc);
2002
2003 device->force_aniso =
2004 MIN2(16, radv_get_int_debug_option("RADV_TEX_ANISO", -1));
2005 if (device->force_aniso >= 0) {
2006 fprintf(stderr, "radv: Forcing anisotropy filter to %ix\n",
2007 1 << util_logbase2(device->force_aniso));
2008 }
2009
2010 *pDevice = radv_device_to_handle(device);
2011 return VK_SUCCESS;
2012
2013 fail_meta:
2014 radv_device_finish_meta(device);
2015 fail:
2016 radv_bo_list_finish(&device->bo_list);
2017
2018 if (device->trace_bo)
2019 device->ws->buffer_destroy(device->trace_bo);
2020
2021 if (device->gfx_init)
2022 device->ws->buffer_destroy(device->gfx_init);
2023
2024 for (unsigned i = 0; i < RADV_MAX_QUEUE_FAMILIES; i++) {
2025 for (unsigned q = 0; q < device->queue_count[i]; q++)
2026 radv_queue_finish(&device->queues[i][q]);
2027 if (device->queue_count[i])
2028 vk_free(&device->alloc, device->queues[i]);
2029 }
2030
2031 vk_free(&device->alloc, device);
2032 return result;
2033 }
2034
2035 void radv_DestroyDevice(
2036 VkDevice _device,
2037 const VkAllocationCallbacks* pAllocator)
2038 {
2039 RADV_FROM_HANDLE(radv_device, device, _device);
2040
2041 if (!device)
2042 return;
2043
2044 if (device->trace_bo)
2045 device->ws->buffer_destroy(device->trace_bo);
2046
2047 if (device->gfx_init)
2048 device->ws->buffer_destroy(device->gfx_init);
2049
2050 for (unsigned i = 0; i < RADV_MAX_QUEUE_FAMILIES; i++) {
2051 for (unsigned q = 0; q < device->queue_count[i]; q++)
2052 radv_queue_finish(&device->queues[i][q]);
2053 if (device->queue_count[i])
2054 vk_free(&device->alloc, device->queues[i]);
2055 if (device->empty_cs[i])
2056 device->ws->cs_destroy(device->empty_cs[i]);
2057 }
2058 radv_device_finish_meta(device);
2059
2060 VkPipelineCache pc = radv_pipeline_cache_to_handle(device->mem_cache);
2061 radv_DestroyPipelineCache(radv_device_to_handle(device), pc, NULL);
2062
2063 radv_destroy_shader_slabs(device);
2064
2065 radv_bo_list_finish(&device->bo_list);
2066 vk_free(&device->alloc, device);
2067 }
2068
2069 VkResult radv_EnumerateInstanceLayerProperties(
2070 uint32_t* pPropertyCount,
2071 VkLayerProperties* pProperties)
2072 {
2073 if (pProperties == NULL) {
2074 *pPropertyCount = 0;
2075 return VK_SUCCESS;
2076 }
2077
2078 /* None supported at this time */
2079 return vk_error(NULL, VK_ERROR_LAYER_NOT_PRESENT);
2080 }
2081
2082 VkResult radv_EnumerateDeviceLayerProperties(
2083 VkPhysicalDevice physicalDevice,
2084 uint32_t* pPropertyCount,
2085 VkLayerProperties* pProperties)
2086 {
2087 if (pProperties == NULL) {
2088 *pPropertyCount = 0;
2089 return VK_SUCCESS;
2090 }
2091
2092 /* None supported at this time */
2093 return vk_error(NULL, VK_ERROR_LAYER_NOT_PRESENT);
2094 }
2095
2096 void radv_GetDeviceQueue2(
2097 VkDevice _device,
2098 const VkDeviceQueueInfo2* pQueueInfo,
2099 VkQueue* pQueue)
2100 {
2101 RADV_FROM_HANDLE(radv_device, device, _device);
2102 struct radv_queue *queue;
2103
2104 queue = &device->queues[pQueueInfo->queueFamilyIndex][pQueueInfo->queueIndex];
2105 if (pQueueInfo->flags != queue->flags) {
2106 /* From the Vulkan 1.1.70 spec:
2107 *
2108 * "The queue returned by vkGetDeviceQueue2 must have the same
2109 * flags value from this structure as that used at device
2110 * creation time in a VkDeviceQueueCreateInfo instance. If no
2111 * matching flags were specified at device creation time then
2112 * pQueue will return VK_NULL_HANDLE."
2113 */
2114 *pQueue = VK_NULL_HANDLE;
2115 return;
2116 }
2117
2118 *pQueue = radv_queue_to_handle(queue);
2119 }
2120
2121 void radv_GetDeviceQueue(
2122 VkDevice _device,
2123 uint32_t queueFamilyIndex,
2124 uint32_t queueIndex,
2125 VkQueue* pQueue)
2126 {
2127 const VkDeviceQueueInfo2 info = (VkDeviceQueueInfo2) {
2128 .sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_INFO_2,
2129 .queueFamilyIndex = queueFamilyIndex,
2130 .queueIndex = queueIndex
2131 };
2132
2133 radv_GetDeviceQueue2(_device, &info, pQueue);
2134 }
2135
2136 static void
2137 fill_geom_tess_rings(struct radv_queue *queue,
2138 uint32_t *map,
2139 bool add_sample_positions,
2140 uint32_t esgs_ring_size,
2141 struct radeon_winsys_bo *esgs_ring_bo,
2142 uint32_t gsvs_ring_size,
2143 struct radeon_winsys_bo *gsvs_ring_bo,
2144 uint32_t tess_factor_ring_size,
2145 uint32_t tess_offchip_ring_offset,
2146 uint32_t tess_offchip_ring_size,
2147 struct radeon_winsys_bo *tess_rings_bo)
2148 {
2149 uint32_t *desc = &map[4];
2150
2151 if (esgs_ring_bo) {
2152 uint64_t esgs_va = radv_buffer_get_va(esgs_ring_bo);
2153
2154 /* stride 0, num records - size, add tid, swizzle, elsize4,
2155 index stride 64 */
2156 desc[0] = esgs_va;
2157 desc[1] = S_008F04_BASE_ADDRESS_HI(esgs_va >> 32) |
2158 S_008F04_STRIDE(0) |
2159 S_008F04_SWIZZLE_ENABLE(true);
2160 desc[2] = esgs_ring_size;
2161 desc[3] = S_008F0C_DST_SEL_X(V_008F0C_SQ_SEL_X) |
2162 S_008F0C_DST_SEL_Y(V_008F0C_SQ_SEL_Y) |
2163 S_008F0C_DST_SEL_Z(V_008F0C_SQ_SEL_Z) |
2164 S_008F0C_DST_SEL_W(V_008F0C_SQ_SEL_W) |
2165 S_008F0C_ELEMENT_SIZE(1) |
2166 S_008F0C_INDEX_STRIDE(3) |
2167 S_008F0C_ADD_TID_ENABLE(true);
2168
2169 if (queue->device->physical_device->rad_info.chip_class >= GFX10) {
2170 desc[3] |= S_008F0C_FORMAT(V_008F0C_IMG_FORMAT_32_FLOAT) |
2171 S_008F0C_OOB_SELECT(2) |
2172 S_008F0C_RESOURCE_LEVEL(1);
2173 } else {
2174 desc[3] |= S_008F0C_NUM_FORMAT(V_008F0C_BUF_NUM_FORMAT_FLOAT) |
2175 S_008F0C_DATA_FORMAT(V_008F0C_BUF_DATA_FORMAT_32);
2176 }
2177
2178 /* GS entry for ES->GS ring */
2179 /* stride 0, num records - size, elsize0,
2180 index stride 0 */
2181 desc[4] = esgs_va;
2182 desc[5] = S_008F04_BASE_ADDRESS_HI(esgs_va >> 32)|
2183 S_008F04_STRIDE(0) |
2184 S_008F04_SWIZZLE_ENABLE(false);
2185 desc[6] = esgs_ring_size;
2186 desc[7] = S_008F0C_DST_SEL_X(V_008F0C_SQ_SEL_X) |
2187 S_008F0C_DST_SEL_Y(V_008F0C_SQ_SEL_Y) |
2188 S_008F0C_DST_SEL_Z(V_008F0C_SQ_SEL_Z) |
2189 S_008F0C_DST_SEL_W(V_008F0C_SQ_SEL_W) |
2190 S_008F0C_ELEMENT_SIZE(0) |
2191 S_008F0C_INDEX_STRIDE(0) |
2192 S_008F0C_ADD_TID_ENABLE(false);
2193
2194 if (queue->device->physical_device->rad_info.chip_class >= GFX10) {
2195 desc[7] |= S_008F0C_FORMAT(V_008F0C_IMG_FORMAT_32_FLOAT) |
2196 S_008F0C_OOB_SELECT(2) |
2197 S_008F0C_RESOURCE_LEVEL(1);
2198 } else {
2199 desc[7] |= S_008F0C_NUM_FORMAT(V_008F0C_BUF_NUM_FORMAT_FLOAT) |
2200 S_008F0C_DATA_FORMAT(V_008F0C_BUF_DATA_FORMAT_32);
2201 }
2202 }
2203
2204 desc += 8;
2205
2206 if (gsvs_ring_bo) {
2207 uint64_t gsvs_va = radv_buffer_get_va(gsvs_ring_bo);
2208
2209 /* VS entry for GS->VS ring */
2210 /* stride 0, num records - size, elsize0,
2211 index stride 0 */
2212 desc[0] = gsvs_va;
2213 desc[1] = S_008F04_BASE_ADDRESS_HI(gsvs_va >> 32)|
2214 S_008F04_STRIDE(0) |
2215 S_008F04_SWIZZLE_ENABLE(false);
2216 desc[2] = gsvs_ring_size;
2217 desc[3] = S_008F0C_DST_SEL_X(V_008F0C_SQ_SEL_X) |
2218 S_008F0C_DST_SEL_Y(V_008F0C_SQ_SEL_Y) |
2219 S_008F0C_DST_SEL_Z(V_008F0C_SQ_SEL_Z) |
2220 S_008F0C_DST_SEL_W(V_008F0C_SQ_SEL_W) |
2221 S_008F0C_ELEMENT_SIZE(0) |
2222 S_008F0C_INDEX_STRIDE(0) |
2223 S_008F0C_ADD_TID_ENABLE(false);
2224
2225 if (queue->device->physical_device->rad_info.chip_class >= GFX10) {
2226 desc[3] |= S_008F0C_FORMAT(V_008F0C_IMG_FORMAT_32_FLOAT) |
2227 S_008F0C_OOB_SELECT(2) |
2228 S_008F0C_RESOURCE_LEVEL(1);
2229 } else {
2230 desc[3] |= S_008F0C_NUM_FORMAT(V_008F0C_BUF_NUM_FORMAT_FLOAT) |
2231 S_008F0C_DATA_FORMAT(V_008F0C_BUF_DATA_FORMAT_32);
2232 }
2233
2234 /* stride gsvs_itemsize, num records 64
2235 elsize 4, index stride 16 */
2236 /* shader will patch stride and desc[2] */
2237 desc[4] = gsvs_va;
2238 desc[5] = S_008F04_BASE_ADDRESS_HI(gsvs_va >> 32)|
2239 S_008F04_STRIDE(0) |
2240 S_008F04_SWIZZLE_ENABLE(true);
2241 desc[6] = 0;
2242 desc[7] = S_008F0C_DST_SEL_X(V_008F0C_SQ_SEL_X) |
2243 S_008F0C_DST_SEL_Y(V_008F0C_SQ_SEL_Y) |
2244 S_008F0C_DST_SEL_Z(V_008F0C_SQ_SEL_Z) |
2245 S_008F0C_DST_SEL_W(V_008F0C_SQ_SEL_W) |
2246 S_008F0C_ELEMENT_SIZE(1) |
2247 S_008F0C_INDEX_STRIDE(1) |
2248 S_008F0C_ADD_TID_ENABLE(true);
2249
2250 if (queue->device->physical_device->rad_info.chip_class >= GFX10) {
2251 desc[7] |= S_008F0C_FORMAT(V_008F0C_IMG_FORMAT_32_FLOAT) |
2252 S_008F0C_OOB_SELECT(2) |
2253 S_008F0C_RESOURCE_LEVEL(1);
2254 } else {
2255 desc[7] |= S_008F0C_NUM_FORMAT(V_008F0C_BUF_NUM_FORMAT_FLOAT) |
2256 S_008F0C_DATA_FORMAT(V_008F0C_BUF_DATA_FORMAT_32);
2257 }
2258
2259 }
2260
2261 desc += 8;
2262
2263 if (tess_rings_bo) {
2264 uint64_t tess_va = radv_buffer_get_va(tess_rings_bo);
2265 uint64_t tess_offchip_va = tess_va + tess_offchip_ring_offset;
2266
2267 desc[0] = tess_va;
2268 desc[1] = S_008F04_BASE_ADDRESS_HI(tess_va >> 32) |
2269 S_008F04_STRIDE(0) |
2270 S_008F04_SWIZZLE_ENABLE(false);
2271 desc[2] = tess_factor_ring_size;
2272 desc[3] = S_008F0C_DST_SEL_X(V_008F0C_SQ_SEL_X) |
2273 S_008F0C_DST_SEL_Y(V_008F0C_SQ_SEL_Y) |
2274 S_008F0C_DST_SEL_Z(V_008F0C_SQ_SEL_Z) |
2275 S_008F0C_DST_SEL_W(V_008F0C_SQ_SEL_W);
2276
2277 if (queue->device->physical_device->rad_info.chip_class >= GFX10) {
2278 desc[3] |= S_008F0C_FORMAT(V_008F0C_IMG_FORMAT_32_FLOAT) |
2279 S_008F0C_OOB_SELECT(3) |
2280 S_008F0C_RESOURCE_LEVEL(1);
2281 } else {
2282 desc[3] |= S_008F0C_NUM_FORMAT(V_008F0C_BUF_NUM_FORMAT_FLOAT) |
2283 S_008F0C_DATA_FORMAT(V_008F0C_BUF_DATA_FORMAT_32);
2284 }
2285
2286 desc[4] = tess_offchip_va;
2287 desc[5] = S_008F04_BASE_ADDRESS_HI(tess_offchip_va >> 32) |
2288 S_008F04_STRIDE(0) |
2289 S_008F04_SWIZZLE_ENABLE(false);
2290 desc[6] = tess_offchip_ring_size;
2291 desc[7] = S_008F0C_DST_SEL_X(V_008F0C_SQ_SEL_X) |
2292 S_008F0C_DST_SEL_Y(V_008F0C_SQ_SEL_Y) |
2293 S_008F0C_DST_SEL_Z(V_008F0C_SQ_SEL_Z) |
2294 S_008F0C_DST_SEL_W(V_008F0C_SQ_SEL_W);
2295
2296 if (queue->device->physical_device->rad_info.chip_class >= GFX10) {
2297 desc[7] |= S_008F0C_FORMAT(V_008F0C_IMG_FORMAT_32_FLOAT) |
2298 S_008F0C_OOB_SELECT(3) |
2299 S_008F0C_RESOURCE_LEVEL(1);
2300 } else {
2301 desc[7] |= S_008F0C_NUM_FORMAT(V_008F0C_BUF_NUM_FORMAT_FLOAT) |
2302 S_008F0C_DATA_FORMAT(V_008F0C_BUF_DATA_FORMAT_32);
2303 }
2304 }
2305
2306 desc += 8;
2307
2308 if (add_sample_positions) {
2309 /* add sample positions after all rings */
2310 memcpy(desc, queue->device->sample_locations_1x, 8);
2311 desc += 2;
2312 memcpy(desc, queue->device->sample_locations_2x, 16);
2313 desc += 4;
2314 memcpy(desc, queue->device->sample_locations_4x, 32);
2315 desc += 8;
2316 memcpy(desc, queue->device->sample_locations_8x, 64);
2317 }
2318 }
2319
2320 static unsigned
2321 radv_get_hs_offchip_param(struct radv_device *device, uint32_t *max_offchip_buffers_p)
2322 {
2323 bool double_offchip_buffers = device->physical_device->rad_info.chip_class >= GFX7 &&
2324 device->physical_device->rad_info.family != CHIP_CARRIZO &&
2325 device->physical_device->rad_info.family != CHIP_STONEY;
2326 unsigned max_offchip_buffers_per_se = double_offchip_buffers ? 128 : 64;
2327 unsigned max_offchip_buffers;
2328 unsigned offchip_granularity;
2329 unsigned hs_offchip_param;
2330
2331 /*
2332 * Per RadeonSI:
2333 * This must be one less than the maximum number due to a hw limitation.
2334 * Various hardware bugs need thGFX7
2335 *
2336 * Per AMDVLK:
2337 * Vega10 should limit max_offchip_buffers to 508 (4 * 127).
2338 * Gfx7 should limit max_offchip_buffers to 508
2339 * Gfx6 should limit max_offchip_buffers to 126 (2 * 63)
2340 *
2341 * Follow AMDVLK here.
2342 */
2343 if (device->physical_device->rad_info.family == CHIP_VEGA10 ||
2344 device->physical_device->rad_info.chip_class == GFX7 ||
2345 device->physical_device->rad_info.chip_class == GFX6)
2346 --max_offchip_buffers_per_se;
2347
2348 max_offchip_buffers = max_offchip_buffers_per_se *
2349 device->physical_device->rad_info.max_se;
2350
2351 /* Hawaii has a bug with offchip buffers > 256 that can be worked
2352 * around by setting 4K granularity.
2353 */
2354 if (device->tess_offchip_block_dw_size == 4096) {
2355 assert(device->physical_device->rad_info.family == CHIP_HAWAII);
2356 offchip_granularity = V_03093C_X_4K_DWORDS;
2357 } else {
2358 assert(device->tess_offchip_block_dw_size == 8192);
2359 offchip_granularity = V_03093C_X_8K_DWORDS;
2360 }
2361
2362 switch (device->physical_device->rad_info.chip_class) {
2363 case GFX6:
2364 max_offchip_buffers = MIN2(max_offchip_buffers, 126);
2365 break;
2366 case GFX7:
2367 case GFX8:
2368 case GFX9:
2369 default:
2370 max_offchip_buffers = MIN2(max_offchip_buffers, 508);
2371 break;
2372 }
2373
2374 *max_offchip_buffers_p = max_offchip_buffers;
2375 if (device->physical_device->rad_info.chip_class >= GFX7) {
2376 if (device->physical_device->rad_info.chip_class >= GFX8)
2377 --max_offchip_buffers;
2378 hs_offchip_param =
2379 S_03093C_OFFCHIP_BUFFERING(max_offchip_buffers) |
2380 S_03093C_OFFCHIP_GRANULARITY(offchip_granularity);
2381 } else {
2382 hs_offchip_param =
2383 S_0089B0_OFFCHIP_BUFFERING(max_offchip_buffers);
2384 }
2385 return hs_offchip_param;
2386 }
2387
2388 static void
2389 radv_emit_gs_ring_sizes(struct radv_queue *queue, struct radeon_cmdbuf *cs,
2390 struct radeon_winsys_bo *esgs_ring_bo,
2391 uint32_t esgs_ring_size,
2392 struct radeon_winsys_bo *gsvs_ring_bo,
2393 uint32_t gsvs_ring_size)
2394 {
2395 if (!esgs_ring_bo && !gsvs_ring_bo)
2396 return;
2397
2398 if (esgs_ring_bo)
2399 radv_cs_add_buffer(queue->device->ws, cs, esgs_ring_bo);
2400
2401 if (gsvs_ring_bo)
2402 radv_cs_add_buffer(queue->device->ws, cs, gsvs_ring_bo);
2403
2404 if (queue->device->physical_device->rad_info.chip_class >= GFX7) {
2405 radeon_set_uconfig_reg_seq(cs, R_030900_VGT_ESGS_RING_SIZE, 2);
2406 radeon_emit(cs, esgs_ring_size >> 8);
2407 radeon_emit(cs, gsvs_ring_size >> 8);
2408 } else {
2409 radeon_set_config_reg_seq(cs, R_0088C8_VGT_ESGS_RING_SIZE, 2);
2410 radeon_emit(cs, esgs_ring_size >> 8);
2411 radeon_emit(cs, gsvs_ring_size >> 8);
2412 }
2413 }
2414
2415 static void
2416 radv_emit_tess_factor_ring(struct radv_queue *queue, struct radeon_cmdbuf *cs,
2417 unsigned hs_offchip_param, unsigned tf_ring_size,
2418 struct radeon_winsys_bo *tess_rings_bo)
2419 {
2420 uint64_t tf_va;
2421
2422 if (!tess_rings_bo)
2423 return;
2424
2425 tf_va = radv_buffer_get_va(tess_rings_bo);
2426
2427 radv_cs_add_buffer(queue->device->ws, cs, tess_rings_bo);
2428
2429 if (queue->device->physical_device->rad_info.chip_class >= GFX7) {
2430 radeon_set_uconfig_reg(cs, R_030938_VGT_TF_RING_SIZE,
2431 S_030938_SIZE(tf_ring_size / 4));
2432 radeon_set_uconfig_reg(cs, R_030940_VGT_TF_MEMORY_BASE,
2433 tf_va >> 8);
2434
2435 if (queue->device->physical_device->rad_info.chip_class >= GFX10) {
2436 radeon_set_uconfig_reg(cs, R_030984_VGT_TF_MEMORY_BASE_HI_UMD,
2437 S_030984_BASE_HI(tf_va >> 40));
2438 } else if (queue->device->physical_device->rad_info.chip_class == GFX9) {
2439 radeon_set_uconfig_reg(cs, R_030944_VGT_TF_MEMORY_BASE_HI,
2440 S_030944_BASE_HI(tf_va >> 40));
2441 }
2442 radeon_set_uconfig_reg(cs, R_03093C_VGT_HS_OFFCHIP_PARAM,
2443 hs_offchip_param);
2444 } else {
2445 radeon_set_config_reg(cs, R_008988_VGT_TF_RING_SIZE,
2446 S_008988_SIZE(tf_ring_size / 4));
2447 radeon_set_config_reg(cs, R_0089B8_VGT_TF_MEMORY_BASE,
2448 tf_va >> 8);
2449 radeon_set_config_reg(cs, R_0089B0_VGT_HS_OFFCHIP_PARAM,
2450 hs_offchip_param);
2451 }
2452 }
2453
2454 static void
2455 radv_emit_compute_scratch(struct radv_queue *queue, struct radeon_cmdbuf *cs,
2456 struct radeon_winsys_bo *compute_scratch_bo)
2457 {
2458 uint64_t scratch_va;
2459
2460 if (!compute_scratch_bo)
2461 return;
2462
2463 scratch_va = radv_buffer_get_va(compute_scratch_bo);
2464
2465 radv_cs_add_buffer(queue->device->ws, cs, compute_scratch_bo);
2466
2467 radeon_set_sh_reg_seq(cs, R_00B900_COMPUTE_USER_DATA_0, 2);
2468 radeon_emit(cs, scratch_va);
2469 radeon_emit(cs, S_008F04_BASE_ADDRESS_HI(scratch_va >> 32) |
2470 S_008F04_SWIZZLE_ENABLE(1));
2471 }
2472
2473 static void
2474 radv_emit_global_shader_pointers(struct radv_queue *queue,
2475 struct radeon_cmdbuf *cs,
2476 struct radeon_winsys_bo *descriptor_bo)
2477 {
2478 uint64_t va;
2479
2480 if (!descriptor_bo)
2481 return;
2482
2483 va = radv_buffer_get_va(descriptor_bo);
2484
2485 radv_cs_add_buffer(queue->device->ws, cs, descriptor_bo);
2486
2487 if (queue->device->physical_device->rad_info.chip_class >= GFX10) {
2488 uint32_t regs[] = {R_00B030_SPI_SHADER_USER_DATA_PS_0,
2489 R_00B130_SPI_SHADER_USER_DATA_VS_0,
2490 R_00B230_SPI_SHADER_USER_DATA_GS_0,
2491 R_00B430_SPI_SHADER_USER_DATA_HS_0};
2492
2493 for (int i = 0; i < ARRAY_SIZE(regs); ++i) {
2494 radv_emit_shader_pointer(queue->device, cs, regs[i],
2495 va, true);
2496 }
2497 } else if (queue->device->physical_device->rad_info.chip_class >= GFX9) {
2498 uint32_t regs[] = {R_00B030_SPI_SHADER_USER_DATA_PS_0,
2499 R_00B130_SPI_SHADER_USER_DATA_VS_0,
2500 R_00B208_SPI_SHADER_USER_DATA_ADDR_LO_GS,
2501 R_00B408_SPI_SHADER_USER_DATA_ADDR_LO_HS};
2502
2503 for (int i = 0; i < ARRAY_SIZE(regs); ++i) {
2504 radv_emit_shader_pointer(queue->device, cs, regs[i],
2505 va, true);
2506 }
2507 } else {
2508 uint32_t regs[] = {R_00B030_SPI_SHADER_USER_DATA_PS_0,
2509 R_00B130_SPI_SHADER_USER_DATA_VS_0,
2510 R_00B230_SPI_SHADER_USER_DATA_GS_0,
2511 R_00B330_SPI_SHADER_USER_DATA_ES_0,
2512 R_00B430_SPI_SHADER_USER_DATA_HS_0,
2513 R_00B530_SPI_SHADER_USER_DATA_LS_0};
2514
2515 for (int i = 0; i < ARRAY_SIZE(regs); ++i) {
2516 radv_emit_shader_pointer(queue->device, cs, regs[i],
2517 va, true);
2518 }
2519 }
2520 }
2521
2522 static void
2523 radv_init_graphics_state(struct radeon_cmdbuf *cs, struct radv_queue *queue)
2524 {
2525 struct radv_device *device = queue->device;
2526
2527 if (device->gfx_init) {
2528 uint64_t va = radv_buffer_get_va(device->gfx_init);
2529
2530 radeon_emit(cs, PKT3(PKT3_INDIRECT_BUFFER_CIK, 2, 0));
2531 radeon_emit(cs, va);
2532 radeon_emit(cs, va >> 32);
2533 radeon_emit(cs, device->gfx_init_size_dw & 0xffff);
2534
2535 radv_cs_add_buffer(device->ws, cs, device->gfx_init);
2536 } else {
2537 struct radv_physical_device *physical_device = device->physical_device;
2538 si_emit_graphics(physical_device, cs);
2539 }
2540 }
2541
2542 static void
2543 radv_init_compute_state(struct radeon_cmdbuf *cs, struct radv_queue *queue)
2544 {
2545 struct radv_physical_device *physical_device = queue->device->physical_device;
2546 si_emit_compute(physical_device, cs);
2547 }
2548
2549 static VkResult
2550 radv_get_preamble_cs(struct radv_queue *queue,
2551 uint32_t scratch_size,
2552 uint32_t compute_scratch_size,
2553 uint32_t esgs_ring_size,
2554 uint32_t gsvs_ring_size,
2555 bool needs_tess_rings,
2556 bool needs_sample_positions,
2557 struct radeon_cmdbuf **initial_full_flush_preamble_cs,
2558 struct radeon_cmdbuf **initial_preamble_cs,
2559 struct radeon_cmdbuf **continue_preamble_cs)
2560 {
2561 struct radeon_winsys_bo *scratch_bo = NULL;
2562 struct radeon_winsys_bo *descriptor_bo = NULL;
2563 struct radeon_winsys_bo *compute_scratch_bo = NULL;
2564 struct radeon_winsys_bo *esgs_ring_bo = NULL;
2565 struct radeon_winsys_bo *gsvs_ring_bo = NULL;
2566 struct radeon_winsys_bo *tess_rings_bo = NULL;
2567 struct radeon_cmdbuf *dest_cs[3] = {0};
2568 bool add_tess_rings = false, add_sample_positions = false;
2569 unsigned tess_factor_ring_size = 0, tess_offchip_ring_size = 0;
2570 unsigned max_offchip_buffers;
2571 unsigned hs_offchip_param = 0;
2572 unsigned tess_offchip_ring_offset;
2573 uint32_t ring_bo_flags = RADEON_FLAG_NO_CPU_ACCESS | RADEON_FLAG_NO_INTERPROCESS_SHARING;
2574 if (!queue->has_tess_rings) {
2575 if (needs_tess_rings)
2576 add_tess_rings = true;
2577 }
2578 if (!queue->has_sample_positions) {
2579 if (needs_sample_positions)
2580 add_sample_positions = true;
2581 }
2582 tess_factor_ring_size = 32768 * queue->device->physical_device->rad_info.max_se;
2583 hs_offchip_param = radv_get_hs_offchip_param(queue->device,
2584 &max_offchip_buffers);
2585 tess_offchip_ring_offset = align(tess_factor_ring_size, 64 * 1024);
2586 tess_offchip_ring_size = max_offchip_buffers *
2587 queue->device->tess_offchip_block_dw_size * 4;
2588
2589 if (scratch_size <= queue->scratch_size &&
2590 compute_scratch_size <= queue->compute_scratch_size &&
2591 esgs_ring_size <= queue->esgs_ring_size &&
2592 gsvs_ring_size <= queue->gsvs_ring_size &&
2593 !add_tess_rings && !add_sample_positions &&
2594 queue->initial_preamble_cs) {
2595 *initial_full_flush_preamble_cs = queue->initial_full_flush_preamble_cs;
2596 *initial_preamble_cs = queue->initial_preamble_cs;
2597 *continue_preamble_cs = queue->continue_preamble_cs;
2598 if (!scratch_size && !compute_scratch_size && !esgs_ring_size && !gsvs_ring_size)
2599 *continue_preamble_cs = NULL;
2600 return VK_SUCCESS;
2601 }
2602
2603 if (scratch_size > queue->scratch_size) {
2604 scratch_bo = queue->device->ws->buffer_create(queue->device->ws,
2605 scratch_size,
2606 4096,
2607 RADEON_DOMAIN_VRAM,
2608 ring_bo_flags,
2609 RADV_BO_PRIORITY_SCRATCH);
2610 if (!scratch_bo)
2611 goto fail;
2612 } else
2613 scratch_bo = queue->scratch_bo;
2614
2615 if (compute_scratch_size > queue->compute_scratch_size) {
2616 compute_scratch_bo = queue->device->ws->buffer_create(queue->device->ws,
2617 compute_scratch_size,
2618 4096,
2619 RADEON_DOMAIN_VRAM,
2620 ring_bo_flags,
2621 RADV_BO_PRIORITY_SCRATCH);
2622 if (!compute_scratch_bo)
2623 goto fail;
2624
2625 } else
2626 compute_scratch_bo = queue->compute_scratch_bo;
2627
2628 if (esgs_ring_size > queue->esgs_ring_size) {
2629 esgs_ring_bo = queue->device->ws->buffer_create(queue->device->ws,
2630 esgs_ring_size,
2631 4096,
2632 RADEON_DOMAIN_VRAM,
2633 ring_bo_flags,
2634 RADV_BO_PRIORITY_SCRATCH);
2635 if (!esgs_ring_bo)
2636 goto fail;
2637 } else {
2638 esgs_ring_bo = queue->esgs_ring_bo;
2639 esgs_ring_size = queue->esgs_ring_size;
2640 }
2641
2642 if (gsvs_ring_size > queue->gsvs_ring_size) {
2643 gsvs_ring_bo = queue->device->ws->buffer_create(queue->device->ws,
2644 gsvs_ring_size,
2645 4096,
2646 RADEON_DOMAIN_VRAM,
2647 ring_bo_flags,
2648 RADV_BO_PRIORITY_SCRATCH);
2649 if (!gsvs_ring_bo)
2650 goto fail;
2651 } else {
2652 gsvs_ring_bo = queue->gsvs_ring_bo;
2653 gsvs_ring_size = queue->gsvs_ring_size;
2654 }
2655
2656 if (add_tess_rings) {
2657 tess_rings_bo = queue->device->ws->buffer_create(queue->device->ws,
2658 tess_offchip_ring_offset + tess_offchip_ring_size,
2659 256,
2660 RADEON_DOMAIN_VRAM,
2661 ring_bo_flags,
2662 RADV_BO_PRIORITY_SCRATCH);
2663 if (!tess_rings_bo)
2664 goto fail;
2665 } else {
2666 tess_rings_bo = queue->tess_rings_bo;
2667 }
2668
2669 if (scratch_bo != queue->scratch_bo ||
2670 esgs_ring_bo != queue->esgs_ring_bo ||
2671 gsvs_ring_bo != queue->gsvs_ring_bo ||
2672 tess_rings_bo != queue->tess_rings_bo ||
2673 add_sample_positions) {
2674 uint32_t size = 0;
2675 if (gsvs_ring_bo || esgs_ring_bo ||
2676 tess_rings_bo || add_sample_positions) {
2677 size = 112; /* 2 dword + 2 padding + 4 dword * 6 */
2678 if (add_sample_positions)
2679 size += 128; /* 64+32+16+8 = 120 bytes */
2680 }
2681 else if (scratch_bo)
2682 size = 8; /* 2 dword */
2683
2684 descriptor_bo = queue->device->ws->buffer_create(queue->device->ws,
2685 size,
2686 4096,
2687 RADEON_DOMAIN_VRAM,
2688 RADEON_FLAG_CPU_ACCESS |
2689 RADEON_FLAG_NO_INTERPROCESS_SHARING |
2690 RADEON_FLAG_READ_ONLY,
2691 RADV_BO_PRIORITY_DESCRIPTOR);
2692 if (!descriptor_bo)
2693 goto fail;
2694 } else
2695 descriptor_bo = queue->descriptor_bo;
2696
2697 if (descriptor_bo != queue->descriptor_bo) {
2698 uint32_t *map = (uint32_t*)queue->device->ws->buffer_map(descriptor_bo);
2699
2700 if (scratch_bo) {
2701 uint64_t scratch_va = radv_buffer_get_va(scratch_bo);
2702 uint32_t rsrc1 = S_008F04_BASE_ADDRESS_HI(scratch_va >> 32) |
2703 S_008F04_SWIZZLE_ENABLE(1);
2704 map[0] = scratch_va;
2705 map[1] = rsrc1;
2706 }
2707
2708 if (esgs_ring_bo || gsvs_ring_bo || tess_rings_bo || add_sample_positions)
2709 fill_geom_tess_rings(queue, map, add_sample_positions,
2710 esgs_ring_size, esgs_ring_bo,
2711 gsvs_ring_size, gsvs_ring_bo,
2712 tess_factor_ring_size,
2713 tess_offchip_ring_offset,
2714 tess_offchip_ring_size,
2715 tess_rings_bo);
2716
2717 queue->device->ws->buffer_unmap(descriptor_bo);
2718 }
2719
2720 for(int i = 0; i < 3; ++i) {
2721 struct radeon_cmdbuf *cs = NULL;
2722 cs = queue->device->ws->cs_create(queue->device->ws,
2723 queue->queue_family_index ? RING_COMPUTE : RING_GFX);
2724 if (!cs)
2725 goto fail;
2726
2727 dest_cs[i] = cs;
2728
2729 if (scratch_bo)
2730 radv_cs_add_buffer(queue->device->ws, cs, scratch_bo);
2731
2732 /* Emit initial configuration. */
2733 switch (queue->queue_family_index) {
2734 case RADV_QUEUE_GENERAL:
2735 radv_init_graphics_state(cs, queue);
2736 break;
2737 case RADV_QUEUE_COMPUTE:
2738 radv_init_compute_state(cs, queue);
2739 break;
2740 case RADV_QUEUE_TRANSFER:
2741 break;
2742 }
2743
2744 if (esgs_ring_bo || gsvs_ring_bo || tess_rings_bo) {
2745 radeon_emit(cs, PKT3(PKT3_EVENT_WRITE, 0, 0));
2746 radeon_emit(cs, EVENT_TYPE(V_028A90_VS_PARTIAL_FLUSH) | EVENT_INDEX(4));
2747 radeon_emit(cs, PKT3(PKT3_EVENT_WRITE, 0, 0));
2748 radeon_emit(cs, EVENT_TYPE(V_028A90_VGT_FLUSH) | EVENT_INDEX(0));
2749 }
2750
2751 radv_emit_gs_ring_sizes(queue, cs, esgs_ring_bo, esgs_ring_size,
2752 gsvs_ring_bo, gsvs_ring_size);
2753 radv_emit_tess_factor_ring(queue, cs, hs_offchip_param,
2754 tess_factor_ring_size, tess_rings_bo);
2755 radv_emit_global_shader_pointers(queue, cs, descriptor_bo);
2756 radv_emit_compute_scratch(queue, cs, compute_scratch_bo);
2757
2758 if (i == 0) {
2759 si_cs_emit_cache_flush(cs,
2760 queue->device->physical_device->rad_info.chip_class,
2761 NULL, 0,
2762 queue->queue_family_index == RING_COMPUTE &&
2763 queue->device->physical_device->rad_info.chip_class >= GFX7,
2764 (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)) |
2765 RADV_CMD_FLAG_INV_ICACHE |
2766 RADV_CMD_FLAG_INV_SCACHE |
2767 RADV_CMD_FLAG_INV_VCACHE |
2768 RADV_CMD_FLAG_INV_L2 |
2769 RADV_CMD_FLAG_START_PIPELINE_STATS, 0);
2770 } else if (i == 1) {
2771 si_cs_emit_cache_flush(cs,
2772 queue->device->physical_device->rad_info.chip_class,
2773 NULL, 0,
2774 queue->queue_family_index == RING_COMPUTE &&
2775 queue->device->physical_device->rad_info.chip_class >= GFX7,
2776 RADV_CMD_FLAG_INV_ICACHE |
2777 RADV_CMD_FLAG_INV_SCACHE |
2778 RADV_CMD_FLAG_INV_VCACHE |
2779 RADV_CMD_FLAG_INV_L2 |
2780 RADV_CMD_FLAG_START_PIPELINE_STATS, 0);
2781 }
2782
2783 if (!queue->device->ws->cs_finalize(cs))
2784 goto fail;
2785 }
2786
2787 if (queue->initial_full_flush_preamble_cs)
2788 queue->device->ws->cs_destroy(queue->initial_full_flush_preamble_cs);
2789
2790 if (queue->initial_preamble_cs)
2791 queue->device->ws->cs_destroy(queue->initial_preamble_cs);
2792
2793 if (queue->continue_preamble_cs)
2794 queue->device->ws->cs_destroy(queue->continue_preamble_cs);
2795
2796 queue->initial_full_flush_preamble_cs = dest_cs[0];
2797 queue->initial_preamble_cs = dest_cs[1];
2798 queue->continue_preamble_cs = dest_cs[2];
2799
2800 if (scratch_bo != queue->scratch_bo) {
2801 if (queue->scratch_bo)
2802 queue->device->ws->buffer_destroy(queue->scratch_bo);
2803 queue->scratch_bo = scratch_bo;
2804 queue->scratch_size = scratch_size;
2805 }
2806
2807 if (compute_scratch_bo != queue->compute_scratch_bo) {
2808 if (queue->compute_scratch_bo)
2809 queue->device->ws->buffer_destroy(queue->compute_scratch_bo);
2810 queue->compute_scratch_bo = compute_scratch_bo;
2811 queue->compute_scratch_size = compute_scratch_size;
2812 }
2813
2814 if (esgs_ring_bo != queue->esgs_ring_bo) {
2815 if (queue->esgs_ring_bo)
2816 queue->device->ws->buffer_destroy(queue->esgs_ring_bo);
2817 queue->esgs_ring_bo = esgs_ring_bo;
2818 queue->esgs_ring_size = esgs_ring_size;
2819 }
2820
2821 if (gsvs_ring_bo != queue->gsvs_ring_bo) {
2822 if (queue->gsvs_ring_bo)
2823 queue->device->ws->buffer_destroy(queue->gsvs_ring_bo);
2824 queue->gsvs_ring_bo = gsvs_ring_bo;
2825 queue->gsvs_ring_size = gsvs_ring_size;
2826 }
2827
2828 if (tess_rings_bo != queue->tess_rings_bo) {
2829 queue->tess_rings_bo = tess_rings_bo;
2830 queue->has_tess_rings = true;
2831 }
2832
2833 if (descriptor_bo != queue->descriptor_bo) {
2834 if (queue->descriptor_bo)
2835 queue->device->ws->buffer_destroy(queue->descriptor_bo);
2836
2837 queue->descriptor_bo = descriptor_bo;
2838 }
2839
2840 if (add_sample_positions)
2841 queue->has_sample_positions = true;
2842
2843 *initial_full_flush_preamble_cs = queue->initial_full_flush_preamble_cs;
2844 *initial_preamble_cs = queue->initial_preamble_cs;
2845 *continue_preamble_cs = queue->continue_preamble_cs;
2846 if (!scratch_size && !compute_scratch_size && !esgs_ring_size && !gsvs_ring_size)
2847 *continue_preamble_cs = NULL;
2848 return VK_SUCCESS;
2849 fail:
2850 for (int i = 0; i < ARRAY_SIZE(dest_cs); ++i)
2851 if (dest_cs[i])
2852 queue->device->ws->cs_destroy(dest_cs[i]);
2853 if (descriptor_bo && descriptor_bo != queue->descriptor_bo)
2854 queue->device->ws->buffer_destroy(descriptor_bo);
2855 if (scratch_bo && scratch_bo != queue->scratch_bo)
2856 queue->device->ws->buffer_destroy(scratch_bo);
2857 if (compute_scratch_bo && compute_scratch_bo != queue->compute_scratch_bo)
2858 queue->device->ws->buffer_destroy(compute_scratch_bo);
2859 if (esgs_ring_bo && esgs_ring_bo != queue->esgs_ring_bo)
2860 queue->device->ws->buffer_destroy(esgs_ring_bo);
2861 if (gsvs_ring_bo && gsvs_ring_bo != queue->gsvs_ring_bo)
2862 queue->device->ws->buffer_destroy(gsvs_ring_bo);
2863 if (tess_rings_bo && tess_rings_bo != queue->tess_rings_bo)
2864 queue->device->ws->buffer_destroy(tess_rings_bo);
2865 return vk_error(queue->device->instance, VK_ERROR_OUT_OF_DEVICE_MEMORY);
2866 }
2867
2868 static VkResult radv_alloc_sem_counts(struct radv_instance *instance,
2869 struct radv_winsys_sem_counts *counts,
2870 int num_sems,
2871 const VkSemaphore *sems,
2872 VkFence _fence,
2873 bool reset_temp)
2874 {
2875 int syncobj_idx = 0, sem_idx = 0;
2876
2877 if (num_sems == 0 && _fence == VK_NULL_HANDLE)
2878 return VK_SUCCESS;
2879
2880 for (uint32_t i = 0; i < num_sems; i++) {
2881 RADV_FROM_HANDLE(radv_semaphore, sem, sems[i]);
2882
2883 if (sem->temp_syncobj || sem->syncobj)
2884 counts->syncobj_count++;
2885 else
2886 counts->sem_count++;
2887 }
2888
2889 if (_fence != VK_NULL_HANDLE) {
2890 RADV_FROM_HANDLE(radv_fence, fence, _fence);
2891 if (fence->temp_syncobj || fence->syncobj)
2892 counts->syncobj_count++;
2893 }
2894
2895 if (counts->syncobj_count) {
2896 counts->syncobj = (uint32_t *)malloc(sizeof(uint32_t) * counts->syncobj_count);
2897 if (!counts->syncobj)
2898 return vk_error(instance, VK_ERROR_OUT_OF_HOST_MEMORY);
2899 }
2900
2901 if (counts->sem_count) {
2902 counts->sem = (struct radeon_winsys_sem **)malloc(sizeof(struct radeon_winsys_sem *) * counts->sem_count);
2903 if (!counts->sem) {
2904 free(counts->syncobj);
2905 return vk_error(instance, VK_ERROR_OUT_OF_HOST_MEMORY);
2906 }
2907 }
2908
2909 for (uint32_t i = 0; i < num_sems; i++) {
2910 RADV_FROM_HANDLE(radv_semaphore, sem, sems[i]);
2911
2912 if (sem->temp_syncobj) {
2913 counts->syncobj[syncobj_idx++] = sem->temp_syncobj;
2914 }
2915 else if (sem->syncobj)
2916 counts->syncobj[syncobj_idx++] = sem->syncobj;
2917 else {
2918 assert(sem->sem);
2919 counts->sem[sem_idx++] = sem->sem;
2920 }
2921 }
2922
2923 if (_fence != VK_NULL_HANDLE) {
2924 RADV_FROM_HANDLE(radv_fence, fence, _fence);
2925 if (fence->temp_syncobj)
2926 counts->syncobj[syncobj_idx++] = fence->temp_syncobj;
2927 else if (fence->syncobj)
2928 counts->syncobj[syncobj_idx++] = fence->syncobj;
2929 }
2930
2931 return VK_SUCCESS;
2932 }
2933
2934 static void
2935 radv_free_sem_info(struct radv_winsys_sem_info *sem_info)
2936 {
2937 free(sem_info->wait.syncobj);
2938 free(sem_info->wait.sem);
2939 free(sem_info->signal.syncobj);
2940 free(sem_info->signal.sem);
2941 }
2942
2943
2944 static void radv_free_temp_syncobjs(struct radv_device *device,
2945 int num_sems,
2946 const VkSemaphore *sems)
2947 {
2948 for (uint32_t i = 0; i < num_sems; i++) {
2949 RADV_FROM_HANDLE(radv_semaphore, sem, sems[i]);
2950
2951 if (sem->temp_syncobj) {
2952 device->ws->destroy_syncobj(device->ws, sem->temp_syncobj);
2953 sem->temp_syncobj = 0;
2954 }
2955 }
2956 }
2957
2958 static VkResult
2959 radv_alloc_sem_info(struct radv_instance *instance,
2960 struct radv_winsys_sem_info *sem_info,
2961 int num_wait_sems,
2962 const VkSemaphore *wait_sems,
2963 int num_signal_sems,
2964 const VkSemaphore *signal_sems,
2965 VkFence fence)
2966 {
2967 VkResult ret;
2968 memset(sem_info, 0, sizeof(*sem_info));
2969
2970 ret = radv_alloc_sem_counts(instance, &sem_info->wait, num_wait_sems, wait_sems, VK_NULL_HANDLE, true);
2971 if (ret)
2972 return ret;
2973 ret = radv_alloc_sem_counts(instance, &sem_info->signal, num_signal_sems, signal_sems, fence, false);
2974 if (ret)
2975 radv_free_sem_info(sem_info);
2976
2977 /* caller can override these */
2978 sem_info->cs_emit_wait = true;
2979 sem_info->cs_emit_signal = true;
2980 return ret;
2981 }
2982
2983 /* Signals fence as soon as all the work currently put on queue is done. */
2984 static VkResult radv_signal_fence(struct radv_queue *queue,
2985 struct radv_fence *fence)
2986 {
2987 int ret;
2988 VkResult result;
2989 struct radv_winsys_sem_info sem_info;
2990
2991 result = radv_alloc_sem_info(queue->device->instance, &sem_info, 0, NULL, 0, NULL,
2992 radv_fence_to_handle(fence));
2993 if (result != VK_SUCCESS)
2994 return result;
2995
2996 ret = queue->device->ws->cs_submit(queue->hw_ctx, queue->queue_idx,
2997 &queue->device->empty_cs[queue->queue_family_index],
2998 1, NULL, NULL, &sem_info, NULL,
2999 false, fence->fence);
3000 radv_free_sem_info(&sem_info);
3001
3002 if (ret)
3003 return vk_error(queue->device->instance, VK_ERROR_DEVICE_LOST);
3004
3005 return VK_SUCCESS;
3006 }
3007
3008 VkResult radv_QueueSubmit(
3009 VkQueue _queue,
3010 uint32_t submitCount,
3011 const VkSubmitInfo* pSubmits,
3012 VkFence _fence)
3013 {
3014 RADV_FROM_HANDLE(radv_queue, queue, _queue);
3015 RADV_FROM_HANDLE(radv_fence, fence, _fence);
3016 struct radeon_winsys_fence *base_fence = fence ? fence->fence : NULL;
3017 struct radeon_winsys_ctx *ctx = queue->hw_ctx;
3018 int ret;
3019 uint32_t max_cs_submission = queue->device->trace_bo ? 1 : RADV_MAX_IBS_PER_SUBMIT;
3020 uint32_t scratch_size = 0;
3021 uint32_t compute_scratch_size = 0;
3022 uint32_t esgs_ring_size = 0, gsvs_ring_size = 0;
3023 struct radeon_cmdbuf *initial_preamble_cs = NULL, *initial_flush_preamble_cs = NULL, *continue_preamble_cs = NULL;
3024 VkResult result;
3025 bool fence_emitted = false;
3026 bool tess_rings_needed = false;
3027 bool sample_positions_needed = false;
3028
3029 /* Do this first so failing to allocate scratch buffers can't result in
3030 * partially executed submissions. */
3031 for (uint32_t i = 0; i < submitCount; i++) {
3032 for (uint32_t j = 0; j < pSubmits[i].commandBufferCount; j++) {
3033 RADV_FROM_HANDLE(radv_cmd_buffer, cmd_buffer,
3034 pSubmits[i].pCommandBuffers[j]);
3035
3036 scratch_size = MAX2(scratch_size, cmd_buffer->scratch_size_needed);
3037 compute_scratch_size = MAX2(compute_scratch_size,
3038 cmd_buffer->compute_scratch_size_needed);
3039 esgs_ring_size = MAX2(esgs_ring_size, cmd_buffer->esgs_ring_size_needed);
3040 gsvs_ring_size = MAX2(gsvs_ring_size, cmd_buffer->gsvs_ring_size_needed);
3041 tess_rings_needed |= cmd_buffer->tess_rings_needed;
3042 sample_positions_needed |= cmd_buffer->sample_positions_needed;
3043 }
3044 }
3045
3046 result = radv_get_preamble_cs(queue, scratch_size, compute_scratch_size,
3047 esgs_ring_size, gsvs_ring_size, tess_rings_needed,
3048 sample_positions_needed, &initial_flush_preamble_cs,
3049 &initial_preamble_cs, &continue_preamble_cs);
3050 if (result != VK_SUCCESS)
3051 return result;
3052
3053 for (uint32_t i = 0; i < submitCount; i++) {
3054 struct radeon_cmdbuf **cs_array;
3055 bool do_flush = !i || pSubmits[i].pWaitDstStageMask;
3056 bool can_patch = true;
3057 uint32_t advance;
3058 struct radv_winsys_sem_info sem_info;
3059
3060 result = radv_alloc_sem_info(queue->device->instance,
3061 &sem_info,
3062 pSubmits[i].waitSemaphoreCount,
3063 pSubmits[i].pWaitSemaphores,
3064 pSubmits[i].signalSemaphoreCount,
3065 pSubmits[i].pSignalSemaphores,
3066 _fence);
3067 if (result != VK_SUCCESS)
3068 return result;
3069
3070 if (!pSubmits[i].commandBufferCount) {
3071 if (pSubmits[i].waitSemaphoreCount || pSubmits[i].signalSemaphoreCount) {
3072 ret = queue->device->ws->cs_submit(ctx, queue->queue_idx,
3073 &queue->device->empty_cs[queue->queue_family_index],
3074 1, NULL, NULL,
3075 &sem_info, NULL,
3076 false, base_fence);
3077 if (ret) {
3078 radv_loge("failed to submit CS %d\n", i);
3079 abort();
3080 }
3081 fence_emitted = true;
3082 }
3083 radv_free_sem_info(&sem_info);
3084 continue;
3085 }
3086
3087 cs_array = malloc(sizeof(struct radeon_cmdbuf *) *
3088 (pSubmits[i].commandBufferCount));
3089
3090 for (uint32_t j = 0; j < pSubmits[i].commandBufferCount; j++) {
3091 RADV_FROM_HANDLE(radv_cmd_buffer, cmd_buffer,
3092 pSubmits[i].pCommandBuffers[j]);
3093 assert(cmd_buffer->level == VK_COMMAND_BUFFER_LEVEL_PRIMARY);
3094
3095 cs_array[j] = cmd_buffer->cs;
3096 if ((cmd_buffer->usage_flags & VK_COMMAND_BUFFER_USAGE_SIMULTANEOUS_USE_BIT))
3097 can_patch = false;
3098
3099 cmd_buffer->status = RADV_CMD_BUFFER_STATUS_PENDING;
3100 }
3101
3102 for (uint32_t j = 0; j < pSubmits[i].commandBufferCount; j += advance) {
3103 struct radeon_cmdbuf *initial_preamble = (do_flush && !j) ? initial_flush_preamble_cs : initial_preamble_cs;
3104 const struct radv_winsys_bo_list *bo_list = NULL;
3105
3106 advance = MIN2(max_cs_submission,
3107 pSubmits[i].commandBufferCount - j);
3108
3109 if (queue->device->trace_bo)
3110 *queue->device->trace_id_ptr = 0;
3111
3112 sem_info.cs_emit_wait = j == 0;
3113 sem_info.cs_emit_signal = j + advance == pSubmits[i].commandBufferCount;
3114
3115 if (unlikely(queue->device->use_global_bo_list)) {
3116 pthread_mutex_lock(&queue->device->bo_list.mutex);
3117 bo_list = &queue->device->bo_list.list;
3118 }
3119
3120 ret = queue->device->ws->cs_submit(ctx, queue->queue_idx, cs_array + j,
3121 advance, initial_preamble, continue_preamble_cs,
3122 &sem_info, bo_list,
3123 can_patch, base_fence);
3124
3125 if (unlikely(queue->device->use_global_bo_list))
3126 pthread_mutex_unlock(&queue->device->bo_list.mutex);
3127
3128 if (ret) {
3129 radv_loge("failed to submit CS %d\n", i);
3130 abort();
3131 }
3132 fence_emitted = true;
3133 if (queue->device->trace_bo) {
3134 radv_check_gpu_hangs(queue, cs_array[j]);
3135 }
3136 }
3137
3138 radv_free_temp_syncobjs(queue->device,
3139 pSubmits[i].waitSemaphoreCount,
3140 pSubmits[i].pWaitSemaphores);
3141 radv_free_sem_info(&sem_info);
3142 free(cs_array);
3143 }
3144
3145 if (fence) {
3146 if (!fence_emitted) {
3147 result = radv_signal_fence(queue, fence);
3148 if (result != VK_SUCCESS)
3149 return result;
3150 }
3151 }
3152
3153 return VK_SUCCESS;
3154 }
3155
3156 VkResult radv_QueueWaitIdle(
3157 VkQueue _queue)
3158 {
3159 RADV_FROM_HANDLE(radv_queue, queue, _queue);
3160
3161 queue->device->ws->ctx_wait_idle(queue->hw_ctx,
3162 radv_queue_family_to_ring(queue->queue_family_index),
3163 queue->queue_idx);
3164 return VK_SUCCESS;
3165 }
3166
3167 VkResult radv_DeviceWaitIdle(
3168 VkDevice _device)
3169 {
3170 RADV_FROM_HANDLE(radv_device, device, _device);
3171
3172 for (unsigned i = 0; i < RADV_MAX_QUEUE_FAMILIES; i++) {
3173 for (unsigned q = 0; q < device->queue_count[i]; q++) {
3174 radv_QueueWaitIdle(radv_queue_to_handle(&device->queues[i][q]));
3175 }
3176 }
3177 return VK_SUCCESS;
3178 }
3179
3180 VkResult radv_EnumerateInstanceExtensionProperties(
3181 const char* pLayerName,
3182 uint32_t* pPropertyCount,
3183 VkExtensionProperties* pProperties)
3184 {
3185 VK_OUTARRAY_MAKE(out, pProperties, pPropertyCount);
3186
3187 for (int i = 0; i < RADV_INSTANCE_EXTENSION_COUNT; i++) {
3188 if (radv_supported_instance_extensions.extensions[i]) {
3189 vk_outarray_append(&out, prop) {
3190 *prop = radv_instance_extensions[i];
3191 }
3192 }
3193 }
3194
3195 return vk_outarray_status(&out);
3196 }
3197
3198 VkResult radv_EnumerateDeviceExtensionProperties(
3199 VkPhysicalDevice physicalDevice,
3200 const char* pLayerName,
3201 uint32_t* pPropertyCount,
3202 VkExtensionProperties* pProperties)
3203 {
3204 RADV_FROM_HANDLE(radv_physical_device, device, physicalDevice);
3205 VK_OUTARRAY_MAKE(out, pProperties, pPropertyCount);
3206
3207 for (int i = 0; i < RADV_DEVICE_EXTENSION_COUNT; i++) {
3208 if (device->supported_extensions.extensions[i]) {
3209 vk_outarray_append(&out, prop) {
3210 *prop = radv_device_extensions[i];
3211 }
3212 }
3213 }
3214
3215 return vk_outarray_status(&out);
3216 }
3217
3218 PFN_vkVoidFunction radv_GetInstanceProcAddr(
3219 VkInstance _instance,
3220 const char* pName)
3221 {
3222 RADV_FROM_HANDLE(radv_instance, instance, _instance);
3223
3224 return radv_lookup_entrypoint_checked(pName,
3225 instance ? instance->apiVersion : 0,
3226 instance ? &instance->enabled_extensions : NULL,
3227 NULL);
3228 }
3229
3230 /* The loader wants us to expose a second GetInstanceProcAddr function
3231 * to work around certain LD_PRELOAD issues seen in apps.
3232 */
3233 PUBLIC
3234 VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL vk_icdGetInstanceProcAddr(
3235 VkInstance instance,
3236 const char* pName);
3237
3238 PUBLIC
3239 VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL vk_icdGetInstanceProcAddr(
3240 VkInstance instance,
3241 const char* pName)
3242 {
3243 return radv_GetInstanceProcAddr(instance, pName);
3244 }
3245
3246 PUBLIC
3247 VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL vk_icdGetPhysicalDeviceProcAddr(
3248 VkInstance _instance,
3249 const char* pName);
3250
3251 PUBLIC
3252 VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL vk_icdGetPhysicalDeviceProcAddr(
3253 VkInstance _instance,
3254 const char* pName)
3255 {
3256 RADV_FROM_HANDLE(radv_instance, instance, _instance);
3257
3258 return radv_lookup_physical_device_entrypoint_checked(pName,
3259 instance ? instance->apiVersion : 0,
3260 instance ? &instance->enabled_extensions : NULL);
3261 }
3262
3263 PFN_vkVoidFunction radv_GetDeviceProcAddr(
3264 VkDevice _device,
3265 const char* pName)
3266 {
3267 RADV_FROM_HANDLE(radv_device, device, _device);
3268
3269 return radv_lookup_entrypoint_checked(pName,
3270 device->instance->apiVersion,
3271 &device->instance->enabled_extensions,
3272 &device->enabled_extensions);
3273 }
3274
3275 bool radv_get_memory_fd(struct radv_device *device,
3276 struct radv_device_memory *memory,
3277 int *pFD)
3278 {
3279 struct radeon_bo_metadata metadata;
3280
3281 if (memory->image) {
3282 radv_init_metadata(device, memory->image, &metadata);
3283 device->ws->buffer_set_metadata(memory->bo, &metadata);
3284 }
3285
3286 return device->ws->buffer_get_fd(device->ws, memory->bo,
3287 pFD);
3288 }
3289
3290 static VkResult radv_alloc_memory(struct radv_device *device,
3291 const VkMemoryAllocateInfo* pAllocateInfo,
3292 const VkAllocationCallbacks* pAllocator,
3293 VkDeviceMemory* pMem)
3294 {
3295 struct radv_device_memory *mem;
3296 VkResult result;
3297 enum radeon_bo_domain domain;
3298 uint32_t flags = 0;
3299 enum radv_mem_type mem_type_index = device->physical_device->mem_type_indices[pAllocateInfo->memoryTypeIndex];
3300
3301 assert(pAllocateInfo->sType == VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO);
3302
3303 if (pAllocateInfo->allocationSize == 0) {
3304 /* Apparently, this is allowed */
3305 *pMem = VK_NULL_HANDLE;
3306 return VK_SUCCESS;
3307 }
3308
3309 const VkImportMemoryFdInfoKHR *import_info =
3310 vk_find_struct_const(pAllocateInfo->pNext, IMPORT_MEMORY_FD_INFO_KHR);
3311 const VkMemoryDedicatedAllocateInfo *dedicate_info =
3312 vk_find_struct_const(pAllocateInfo->pNext, MEMORY_DEDICATED_ALLOCATE_INFO);
3313 const VkExportMemoryAllocateInfo *export_info =
3314 vk_find_struct_const(pAllocateInfo->pNext, EXPORT_MEMORY_ALLOCATE_INFO);
3315 const VkImportMemoryHostPointerInfoEXT *host_ptr_info =
3316 vk_find_struct_const(pAllocateInfo->pNext, IMPORT_MEMORY_HOST_POINTER_INFO_EXT);
3317
3318 const struct wsi_memory_allocate_info *wsi_info =
3319 vk_find_struct_const(pAllocateInfo->pNext, WSI_MEMORY_ALLOCATE_INFO_MESA);
3320
3321 mem = vk_alloc2(&device->alloc, pAllocator, sizeof(*mem), 8,
3322 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
3323 if (mem == NULL)
3324 return vk_error(device->instance, VK_ERROR_OUT_OF_HOST_MEMORY);
3325
3326 if (wsi_info && wsi_info->implicit_sync)
3327 flags |= RADEON_FLAG_IMPLICIT_SYNC;
3328
3329 if (dedicate_info) {
3330 mem->image = radv_image_from_handle(dedicate_info->image);
3331 mem->buffer = radv_buffer_from_handle(dedicate_info->buffer);
3332 } else {
3333 mem->image = NULL;
3334 mem->buffer = NULL;
3335 }
3336
3337 float priority_float = 0.5;
3338 const struct VkMemoryPriorityAllocateInfoEXT *priority_ext =
3339 vk_find_struct_const(pAllocateInfo->pNext,
3340 MEMORY_PRIORITY_ALLOCATE_INFO_EXT);
3341 if (priority_ext)
3342 priority_float = priority_ext->priority;
3343
3344 unsigned priority = MIN2(RADV_BO_PRIORITY_APPLICATION_MAX - 1,
3345 (int)(priority_float * RADV_BO_PRIORITY_APPLICATION_MAX));
3346
3347 mem->user_ptr = NULL;
3348
3349 if (import_info) {
3350 assert(import_info->handleType ==
3351 VK_EXTERNAL_MEMORY_HANDLE_TYPE_OPAQUE_FD_BIT ||
3352 import_info->handleType ==
3353 VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_EXT);
3354 mem->bo = device->ws->buffer_from_fd(device->ws, import_info->fd,
3355 priority, NULL, NULL);
3356 if (!mem->bo) {
3357 result = VK_ERROR_INVALID_EXTERNAL_HANDLE;
3358 goto fail;
3359 } else {
3360 close(import_info->fd);
3361 }
3362 } else if (host_ptr_info) {
3363 assert(host_ptr_info->handleType == VK_EXTERNAL_MEMORY_HANDLE_TYPE_HOST_ALLOCATION_BIT_EXT);
3364 assert(mem_type_index == RADV_MEM_TYPE_GTT_CACHED);
3365 mem->bo = device->ws->buffer_from_ptr(device->ws, host_ptr_info->pHostPointer,
3366 pAllocateInfo->allocationSize,
3367 priority);
3368 if (!mem->bo) {
3369 result = VK_ERROR_INVALID_EXTERNAL_HANDLE;
3370 goto fail;
3371 } else {
3372 mem->user_ptr = host_ptr_info->pHostPointer;
3373 }
3374 } else {
3375 uint64_t alloc_size = align_u64(pAllocateInfo->allocationSize, 4096);
3376 if (mem_type_index == RADV_MEM_TYPE_GTT_WRITE_COMBINE ||
3377 mem_type_index == RADV_MEM_TYPE_GTT_CACHED)
3378 domain = RADEON_DOMAIN_GTT;
3379 else
3380 domain = RADEON_DOMAIN_VRAM;
3381
3382 if (mem_type_index == RADV_MEM_TYPE_VRAM)
3383 flags |= RADEON_FLAG_NO_CPU_ACCESS;
3384 else
3385 flags |= RADEON_FLAG_CPU_ACCESS;
3386
3387 if (mem_type_index == RADV_MEM_TYPE_GTT_WRITE_COMBINE)
3388 flags |= RADEON_FLAG_GTT_WC;
3389
3390 if (!dedicate_info && !import_info && (!export_info || !export_info->handleTypes)) {
3391 flags |= RADEON_FLAG_NO_INTERPROCESS_SHARING;
3392 if (device->use_global_bo_list) {
3393 flags |= RADEON_FLAG_PREFER_LOCAL_BO;
3394 }
3395 }
3396
3397 mem->bo = device->ws->buffer_create(device->ws, alloc_size, device->physical_device->rad_info.max_alignment,
3398 domain, flags, priority);
3399
3400 if (!mem->bo) {
3401 result = VK_ERROR_OUT_OF_DEVICE_MEMORY;
3402 goto fail;
3403 }
3404 mem->type_index = mem_type_index;
3405 }
3406
3407 result = radv_bo_list_add(device, mem->bo);
3408 if (result != VK_SUCCESS)
3409 goto fail_bo;
3410
3411 *pMem = radv_device_memory_to_handle(mem);
3412
3413 return VK_SUCCESS;
3414
3415 fail_bo:
3416 device->ws->buffer_destroy(mem->bo);
3417 fail:
3418 vk_free2(&device->alloc, pAllocator, mem);
3419
3420 return result;
3421 }
3422
3423 VkResult radv_AllocateMemory(
3424 VkDevice _device,
3425 const VkMemoryAllocateInfo* pAllocateInfo,
3426 const VkAllocationCallbacks* pAllocator,
3427 VkDeviceMemory* pMem)
3428 {
3429 RADV_FROM_HANDLE(radv_device, device, _device);
3430 return radv_alloc_memory(device, pAllocateInfo, pAllocator, pMem);
3431 }
3432
3433 void radv_FreeMemory(
3434 VkDevice _device,
3435 VkDeviceMemory _mem,
3436 const VkAllocationCallbacks* pAllocator)
3437 {
3438 RADV_FROM_HANDLE(radv_device, device, _device);
3439 RADV_FROM_HANDLE(radv_device_memory, mem, _mem);
3440
3441 if (mem == NULL)
3442 return;
3443
3444 radv_bo_list_remove(device, mem->bo);
3445 device->ws->buffer_destroy(mem->bo);
3446 mem->bo = NULL;
3447
3448 vk_free2(&device->alloc, pAllocator, mem);
3449 }
3450
3451 VkResult radv_MapMemory(
3452 VkDevice _device,
3453 VkDeviceMemory _memory,
3454 VkDeviceSize offset,
3455 VkDeviceSize size,
3456 VkMemoryMapFlags flags,
3457 void** ppData)
3458 {
3459 RADV_FROM_HANDLE(radv_device, device, _device);
3460 RADV_FROM_HANDLE(radv_device_memory, mem, _memory);
3461
3462 if (mem == NULL) {
3463 *ppData = NULL;
3464 return VK_SUCCESS;
3465 }
3466
3467 if (mem->user_ptr)
3468 *ppData = mem->user_ptr;
3469 else
3470 *ppData = device->ws->buffer_map(mem->bo);
3471
3472 if (*ppData) {
3473 *ppData += offset;
3474 return VK_SUCCESS;
3475 }
3476
3477 return vk_error(device->instance, VK_ERROR_MEMORY_MAP_FAILED);
3478 }
3479
3480 void radv_UnmapMemory(
3481 VkDevice _device,
3482 VkDeviceMemory _memory)
3483 {
3484 RADV_FROM_HANDLE(radv_device, device, _device);
3485 RADV_FROM_HANDLE(radv_device_memory, mem, _memory);
3486
3487 if (mem == NULL)
3488 return;
3489
3490 if (mem->user_ptr == NULL)
3491 device->ws->buffer_unmap(mem->bo);
3492 }
3493
3494 VkResult radv_FlushMappedMemoryRanges(
3495 VkDevice _device,
3496 uint32_t memoryRangeCount,
3497 const VkMappedMemoryRange* pMemoryRanges)
3498 {
3499 return VK_SUCCESS;
3500 }
3501
3502 VkResult radv_InvalidateMappedMemoryRanges(
3503 VkDevice _device,
3504 uint32_t memoryRangeCount,
3505 const VkMappedMemoryRange* pMemoryRanges)
3506 {
3507 return VK_SUCCESS;
3508 }
3509
3510 void radv_GetBufferMemoryRequirements(
3511 VkDevice _device,
3512 VkBuffer _buffer,
3513 VkMemoryRequirements* pMemoryRequirements)
3514 {
3515 RADV_FROM_HANDLE(radv_device, device, _device);
3516 RADV_FROM_HANDLE(radv_buffer, buffer, _buffer);
3517
3518 pMemoryRequirements->memoryTypeBits = (1u << device->physical_device->memory_properties.memoryTypeCount) - 1;
3519
3520 if (buffer->flags & VK_BUFFER_CREATE_SPARSE_BINDING_BIT)
3521 pMemoryRequirements->alignment = 4096;
3522 else
3523 pMemoryRequirements->alignment = 16;
3524
3525 pMemoryRequirements->size = align64(buffer->size, pMemoryRequirements->alignment);
3526 }
3527
3528 void radv_GetBufferMemoryRequirements2(
3529 VkDevice device,
3530 const VkBufferMemoryRequirementsInfo2 *pInfo,
3531 VkMemoryRequirements2 *pMemoryRequirements)
3532 {
3533 radv_GetBufferMemoryRequirements(device, pInfo->buffer,
3534 &pMemoryRequirements->memoryRequirements);
3535 RADV_FROM_HANDLE(radv_buffer, buffer, pInfo->buffer);
3536 vk_foreach_struct(ext, pMemoryRequirements->pNext) {
3537 switch (ext->sType) {
3538 case VK_STRUCTURE_TYPE_MEMORY_DEDICATED_REQUIREMENTS: {
3539 VkMemoryDedicatedRequirements *req =
3540 (VkMemoryDedicatedRequirements *) ext;
3541 req->requiresDedicatedAllocation = buffer->shareable;
3542 req->prefersDedicatedAllocation = req->requiresDedicatedAllocation;
3543 break;
3544 }
3545 default:
3546 break;
3547 }
3548 }
3549 }
3550
3551 void radv_GetImageMemoryRequirements(
3552 VkDevice _device,
3553 VkImage _image,
3554 VkMemoryRequirements* pMemoryRequirements)
3555 {
3556 RADV_FROM_HANDLE(radv_device, device, _device);
3557 RADV_FROM_HANDLE(radv_image, image, _image);
3558
3559 pMemoryRequirements->memoryTypeBits = (1u << device->physical_device->memory_properties.memoryTypeCount) - 1;
3560
3561 pMemoryRequirements->size = image->size;
3562 pMemoryRequirements->alignment = image->alignment;
3563 }
3564
3565 void radv_GetImageMemoryRequirements2(
3566 VkDevice device,
3567 const VkImageMemoryRequirementsInfo2 *pInfo,
3568 VkMemoryRequirements2 *pMemoryRequirements)
3569 {
3570 radv_GetImageMemoryRequirements(device, pInfo->image,
3571 &pMemoryRequirements->memoryRequirements);
3572
3573 RADV_FROM_HANDLE(radv_image, image, pInfo->image);
3574
3575 vk_foreach_struct(ext, pMemoryRequirements->pNext) {
3576 switch (ext->sType) {
3577 case VK_STRUCTURE_TYPE_MEMORY_DEDICATED_REQUIREMENTS: {
3578 VkMemoryDedicatedRequirements *req =
3579 (VkMemoryDedicatedRequirements *) ext;
3580 req->requiresDedicatedAllocation = image->shareable;
3581 req->prefersDedicatedAllocation = req->requiresDedicatedAllocation;
3582 break;
3583 }
3584 default:
3585 break;
3586 }
3587 }
3588 }
3589
3590 void radv_GetImageSparseMemoryRequirements(
3591 VkDevice device,
3592 VkImage image,
3593 uint32_t* pSparseMemoryRequirementCount,
3594 VkSparseImageMemoryRequirements* pSparseMemoryRequirements)
3595 {
3596 stub();
3597 }
3598
3599 void radv_GetImageSparseMemoryRequirements2(
3600 VkDevice device,
3601 const VkImageSparseMemoryRequirementsInfo2 *pInfo,
3602 uint32_t* pSparseMemoryRequirementCount,
3603 VkSparseImageMemoryRequirements2 *pSparseMemoryRequirements)
3604 {
3605 stub();
3606 }
3607
3608 void radv_GetDeviceMemoryCommitment(
3609 VkDevice device,
3610 VkDeviceMemory memory,
3611 VkDeviceSize* pCommittedMemoryInBytes)
3612 {
3613 *pCommittedMemoryInBytes = 0;
3614 }
3615
3616 VkResult radv_BindBufferMemory2(VkDevice device,
3617 uint32_t bindInfoCount,
3618 const VkBindBufferMemoryInfo *pBindInfos)
3619 {
3620 for (uint32_t i = 0; i < bindInfoCount; ++i) {
3621 RADV_FROM_HANDLE(radv_device_memory, mem, pBindInfos[i].memory);
3622 RADV_FROM_HANDLE(radv_buffer, buffer, pBindInfos[i].buffer);
3623
3624 if (mem) {
3625 buffer->bo = mem->bo;
3626 buffer->offset = pBindInfos[i].memoryOffset;
3627 } else {
3628 buffer->bo = NULL;
3629 }
3630 }
3631 return VK_SUCCESS;
3632 }
3633
3634 VkResult radv_BindBufferMemory(
3635 VkDevice device,
3636 VkBuffer buffer,
3637 VkDeviceMemory memory,
3638 VkDeviceSize memoryOffset)
3639 {
3640 const VkBindBufferMemoryInfo info = {
3641 .sType = VK_STRUCTURE_TYPE_BIND_BUFFER_MEMORY_INFO,
3642 .buffer = buffer,
3643 .memory = memory,
3644 .memoryOffset = memoryOffset
3645 };
3646
3647 return radv_BindBufferMemory2(device, 1, &info);
3648 }
3649
3650 VkResult radv_BindImageMemory2(VkDevice device,
3651 uint32_t bindInfoCount,
3652 const VkBindImageMemoryInfo *pBindInfos)
3653 {
3654 for (uint32_t i = 0; i < bindInfoCount; ++i) {
3655 RADV_FROM_HANDLE(radv_device_memory, mem, pBindInfos[i].memory);
3656 RADV_FROM_HANDLE(radv_image, image, pBindInfos[i].image);
3657
3658 if (mem) {
3659 image->bo = mem->bo;
3660 image->offset = pBindInfos[i].memoryOffset;
3661 } else {
3662 image->bo = NULL;
3663 image->offset = 0;
3664 }
3665 }
3666 return VK_SUCCESS;
3667 }
3668
3669
3670 VkResult radv_BindImageMemory(
3671 VkDevice device,
3672 VkImage image,
3673 VkDeviceMemory memory,
3674 VkDeviceSize memoryOffset)
3675 {
3676 const VkBindImageMemoryInfo info = {
3677 .sType = VK_STRUCTURE_TYPE_BIND_BUFFER_MEMORY_INFO,
3678 .image = image,
3679 .memory = memory,
3680 .memoryOffset = memoryOffset
3681 };
3682
3683 return radv_BindImageMemory2(device, 1, &info);
3684 }
3685
3686
3687 static void
3688 radv_sparse_buffer_bind_memory(struct radv_device *device,
3689 const VkSparseBufferMemoryBindInfo *bind)
3690 {
3691 RADV_FROM_HANDLE(radv_buffer, buffer, bind->buffer);
3692
3693 for (uint32_t i = 0; i < bind->bindCount; ++i) {
3694 struct radv_device_memory *mem = NULL;
3695
3696 if (bind->pBinds[i].memory != VK_NULL_HANDLE)
3697 mem = radv_device_memory_from_handle(bind->pBinds[i].memory);
3698
3699 device->ws->buffer_virtual_bind(buffer->bo,
3700 bind->pBinds[i].resourceOffset,
3701 bind->pBinds[i].size,
3702 mem ? mem->bo : NULL,
3703 bind->pBinds[i].memoryOffset);
3704 }
3705 }
3706
3707 static void
3708 radv_sparse_image_opaque_bind_memory(struct radv_device *device,
3709 const VkSparseImageOpaqueMemoryBindInfo *bind)
3710 {
3711 RADV_FROM_HANDLE(radv_image, image, bind->image);
3712
3713 for (uint32_t i = 0; i < bind->bindCount; ++i) {
3714 struct radv_device_memory *mem = NULL;
3715
3716 if (bind->pBinds[i].memory != VK_NULL_HANDLE)
3717 mem = radv_device_memory_from_handle(bind->pBinds[i].memory);
3718
3719 device->ws->buffer_virtual_bind(image->bo,
3720 bind->pBinds[i].resourceOffset,
3721 bind->pBinds[i].size,
3722 mem ? mem->bo : NULL,
3723 bind->pBinds[i].memoryOffset);
3724 }
3725 }
3726
3727 VkResult radv_QueueBindSparse(
3728 VkQueue _queue,
3729 uint32_t bindInfoCount,
3730 const VkBindSparseInfo* pBindInfo,
3731 VkFence _fence)
3732 {
3733 RADV_FROM_HANDLE(radv_fence, fence, _fence);
3734 RADV_FROM_HANDLE(radv_queue, queue, _queue);
3735 struct radeon_winsys_fence *base_fence = fence ? fence->fence : NULL;
3736 bool fence_emitted = false;
3737 VkResult result;
3738 int ret;
3739
3740 for (uint32_t i = 0; i < bindInfoCount; ++i) {
3741 struct radv_winsys_sem_info sem_info;
3742 for (uint32_t j = 0; j < pBindInfo[i].bufferBindCount; ++j) {
3743 radv_sparse_buffer_bind_memory(queue->device,
3744 pBindInfo[i].pBufferBinds + j);
3745 }
3746
3747 for (uint32_t j = 0; j < pBindInfo[i].imageOpaqueBindCount; ++j) {
3748 radv_sparse_image_opaque_bind_memory(queue->device,
3749 pBindInfo[i].pImageOpaqueBinds + j);
3750 }
3751
3752 VkResult result;
3753 result = radv_alloc_sem_info(queue->device->instance,
3754 &sem_info,
3755 pBindInfo[i].waitSemaphoreCount,
3756 pBindInfo[i].pWaitSemaphores,
3757 pBindInfo[i].signalSemaphoreCount,
3758 pBindInfo[i].pSignalSemaphores,
3759 _fence);
3760 if (result != VK_SUCCESS)
3761 return result;
3762
3763 if (pBindInfo[i].waitSemaphoreCount || pBindInfo[i].signalSemaphoreCount) {
3764 ret = queue->device->ws->cs_submit(queue->hw_ctx, queue->queue_idx,
3765 &queue->device->empty_cs[queue->queue_family_index],
3766 1, NULL, NULL,
3767 &sem_info, NULL,
3768 false, base_fence);
3769 if (ret) {
3770 radv_loge("failed to submit CS %d\n", i);
3771 abort();
3772 }
3773
3774 fence_emitted = true;
3775 }
3776
3777 radv_free_sem_info(&sem_info);
3778
3779 }
3780
3781 if (fence) {
3782 if (!fence_emitted) {
3783 result = radv_signal_fence(queue, fence);
3784 if (result != VK_SUCCESS)
3785 return result;
3786 }
3787 }
3788
3789 return VK_SUCCESS;
3790 }
3791
3792 VkResult radv_CreateFence(
3793 VkDevice _device,
3794 const VkFenceCreateInfo* pCreateInfo,
3795 const VkAllocationCallbacks* pAllocator,
3796 VkFence* pFence)
3797 {
3798 RADV_FROM_HANDLE(radv_device, device, _device);
3799 const VkExportFenceCreateInfo *export =
3800 vk_find_struct_const(pCreateInfo->pNext, EXPORT_FENCE_CREATE_INFO);
3801 VkExternalFenceHandleTypeFlags handleTypes =
3802 export ? export->handleTypes : 0;
3803
3804 struct radv_fence *fence = vk_alloc2(&device->alloc, pAllocator,
3805 sizeof(*fence), 8,
3806 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
3807
3808 if (!fence)
3809 return vk_error(device->instance, VK_ERROR_OUT_OF_HOST_MEMORY);
3810
3811 fence->fence_wsi = NULL;
3812 fence->temp_syncobj = 0;
3813 if (device->always_use_syncobj || handleTypes) {
3814 int ret = device->ws->create_syncobj(device->ws, &fence->syncobj);
3815 if (ret) {
3816 vk_free2(&device->alloc, pAllocator, fence);
3817 return vk_error(device->instance, VK_ERROR_OUT_OF_HOST_MEMORY);
3818 }
3819 if (pCreateInfo->flags & VK_FENCE_CREATE_SIGNALED_BIT) {
3820 device->ws->signal_syncobj(device->ws, fence->syncobj);
3821 }
3822 fence->fence = NULL;
3823 } else {
3824 fence->fence = device->ws->create_fence();
3825 if (!fence->fence) {
3826 vk_free2(&device->alloc, pAllocator, fence);
3827 return vk_error(device->instance, VK_ERROR_OUT_OF_HOST_MEMORY);
3828 }
3829 fence->syncobj = 0;
3830 if (pCreateInfo->flags & VK_FENCE_CREATE_SIGNALED_BIT)
3831 device->ws->signal_fence(fence->fence);
3832 }
3833
3834 *pFence = radv_fence_to_handle(fence);
3835
3836 return VK_SUCCESS;
3837 }
3838
3839 void radv_DestroyFence(
3840 VkDevice _device,
3841 VkFence _fence,
3842 const VkAllocationCallbacks* pAllocator)
3843 {
3844 RADV_FROM_HANDLE(radv_device, device, _device);
3845 RADV_FROM_HANDLE(radv_fence, fence, _fence);
3846
3847 if (!fence)
3848 return;
3849
3850 if (fence->temp_syncobj)
3851 device->ws->destroy_syncobj(device->ws, fence->temp_syncobj);
3852 if (fence->syncobj)
3853 device->ws->destroy_syncobj(device->ws, fence->syncobj);
3854 if (fence->fence)
3855 device->ws->destroy_fence(fence->fence);
3856 if (fence->fence_wsi)
3857 fence->fence_wsi->destroy(fence->fence_wsi);
3858 vk_free2(&device->alloc, pAllocator, fence);
3859 }
3860
3861
3862 uint64_t radv_get_current_time(void)
3863 {
3864 struct timespec tv;
3865 clock_gettime(CLOCK_MONOTONIC, &tv);
3866 return tv.tv_nsec + tv.tv_sec*1000000000ull;
3867 }
3868
3869 static uint64_t radv_get_absolute_timeout(uint64_t timeout)
3870 {
3871 uint64_t current_time = radv_get_current_time();
3872
3873 timeout = MIN2(UINT64_MAX - current_time, timeout);
3874
3875 return current_time + timeout;
3876 }
3877
3878
3879 static bool radv_all_fences_plain_and_submitted(struct radv_device *device,
3880 uint32_t fenceCount, const VkFence *pFences)
3881 {
3882 for (uint32_t i = 0; i < fenceCount; ++i) {
3883 RADV_FROM_HANDLE(radv_fence, fence, pFences[i]);
3884 if (fence->fence == NULL || fence->syncobj ||
3885 fence->temp_syncobj || fence->fence_wsi ||
3886 (!device->ws->is_fence_waitable(fence->fence)))
3887 return false;
3888 }
3889 return true;
3890 }
3891
3892 static bool radv_all_fences_syncobj(uint32_t fenceCount, const VkFence *pFences)
3893 {
3894 for (uint32_t i = 0; i < fenceCount; ++i) {
3895 RADV_FROM_HANDLE(radv_fence, fence, pFences[i]);
3896 if (fence->syncobj == 0 && fence->temp_syncobj == 0)
3897 return false;
3898 }
3899 return true;
3900 }
3901
3902 VkResult radv_WaitForFences(
3903 VkDevice _device,
3904 uint32_t fenceCount,
3905 const VkFence* pFences,
3906 VkBool32 waitAll,
3907 uint64_t timeout)
3908 {
3909 RADV_FROM_HANDLE(radv_device, device, _device);
3910 timeout = radv_get_absolute_timeout(timeout);
3911
3912 if (device->always_use_syncobj &&
3913 radv_all_fences_syncobj(fenceCount, pFences))
3914 {
3915 uint32_t *handles = malloc(sizeof(uint32_t) * fenceCount);
3916 if (!handles)
3917 return vk_error(device->instance, VK_ERROR_OUT_OF_HOST_MEMORY);
3918
3919 for (uint32_t i = 0; i < fenceCount; ++i) {
3920 RADV_FROM_HANDLE(radv_fence, fence, pFences[i]);
3921 handles[i] = fence->temp_syncobj ? fence->temp_syncobj : fence->syncobj;
3922 }
3923
3924 bool success = device->ws->wait_syncobj(device->ws, handles, fenceCount, waitAll, timeout);
3925
3926 free(handles);
3927 return success ? VK_SUCCESS : VK_TIMEOUT;
3928 }
3929
3930 if (!waitAll && fenceCount > 1) {
3931 /* Not doing this by default for waitAll, due to needing to allocate twice. */
3932 if (device->physical_device->rad_info.drm_minor >= 10 && radv_all_fences_plain_and_submitted(device, fenceCount, pFences)) {
3933 uint32_t wait_count = 0;
3934 struct radeon_winsys_fence **fences = malloc(sizeof(struct radeon_winsys_fence *) * fenceCount);
3935 if (!fences)
3936 return vk_error(device->instance, VK_ERROR_OUT_OF_HOST_MEMORY);
3937
3938 for (uint32_t i = 0; i < fenceCount; ++i) {
3939 RADV_FROM_HANDLE(radv_fence, fence, pFences[i]);
3940
3941 if (device->ws->fence_wait(device->ws, fence->fence, false, 0)) {
3942 free(fences);
3943 return VK_SUCCESS;
3944 }
3945
3946 fences[wait_count++] = fence->fence;
3947 }
3948
3949 bool success = device->ws->fences_wait(device->ws, fences, wait_count,
3950 waitAll, timeout - radv_get_current_time());
3951
3952 free(fences);
3953 return success ? VK_SUCCESS : VK_TIMEOUT;
3954 }
3955
3956 while(radv_get_current_time() <= timeout) {
3957 for (uint32_t i = 0; i < fenceCount; ++i) {
3958 if (radv_GetFenceStatus(_device, pFences[i]) == VK_SUCCESS)
3959 return VK_SUCCESS;
3960 }
3961 }
3962 return VK_TIMEOUT;
3963 }
3964
3965 for (uint32_t i = 0; i < fenceCount; ++i) {
3966 RADV_FROM_HANDLE(radv_fence, fence, pFences[i]);
3967 bool expired = false;
3968
3969 if (fence->temp_syncobj) {
3970 if (!device->ws->wait_syncobj(device->ws, &fence->temp_syncobj, 1, true, timeout))
3971 return VK_TIMEOUT;
3972 continue;
3973 }
3974
3975 if (fence->syncobj) {
3976 if (!device->ws->wait_syncobj(device->ws, &fence->syncobj, 1, true, timeout))
3977 return VK_TIMEOUT;
3978 continue;
3979 }
3980
3981 if (fence->fence) {
3982 if (!device->ws->is_fence_waitable(fence->fence)) {
3983 while(!device->ws->is_fence_waitable(fence->fence) &&
3984 radv_get_current_time() <= timeout)
3985 /* Do nothing */;
3986 }
3987
3988 expired = device->ws->fence_wait(device->ws,
3989 fence->fence,
3990 true, timeout);
3991 if (!expired)
3992 return VK_TIMEOUT;
3993 }
3994
3995 if (fence->fence_wsi) {
3996 VkResult result = fence->fence_wsi->wait(fence->fence_wsi, timeout);
3997 if (result != VK_SUCCESS)
3998 return result;
3999 }
4000 }
4001
4002 return VK_SUCCESS;
4003 }
4004
4005 VkResult radv_ResetFences(VkDevice _device,
4006 uint32_t fenceCount,
4007 const VkFence *pFences)
4008 {
4009 RADV_FROM_HANDLE(radv_device, device, _device);
4010
4011 for (unsigned i = 0; i < fenceCount; ++i) {
4012 RADV_FROM_HANDLE(radv_fence, fence, pFences[i]);
4013 if (fence->fence)
4014 device->ws->reset_fence(fence->fence);
4015
4016 /* Per spec, we first restore the permanent payload, and then reset, so
4017 * having a temp syncobj should not skip resetting the permanent syncobj. */
4018 if (fence->temp_syncobj) {
4019 device->ws->destroy_syncobj(device->ws, fence->temp_syncobj);
4020 fence->temp_syncobj = 0;
4021 }
4022
4023 if (fence->syncobj) {
4024 device->ws->reset_syncobj(device->ws, fence->syncobj);
4025 }
4026 }
4027
4028 return VK_SUCCESS;
4029 }
4030
4031 VkResult radv_GetFenceStatus(VkDevice _device, VkFence _fence)
4032 {
4033 RADV_FROM_HANDLE(radv_device, device, _device);
4034 RADV_FROM_HANDLE(radv_fence, fence, _fence);
4035
4036 if (fence->temp_syncobj) {
4037 bool success = device->ws->wait_syncobj(device->ws, &fence->temp_syncobj, 1, true, 0);
4038 return success ? VK_SUCCESS : VK_NOT_READY;
4039 }
4040
4041 if (fence->syncobj) {
4042 bool success = device->ws->wait_syncobj(device->ws, &fence->syncobj, 1, true, 0);
4043 return success ? VK_SUCCESS : VK_NOT_READY;
4044 }
4045
4046 if (fence->fence) {
4047 if (!device->ws->fence_wait(device->ws, fence->fence, false, 0))
4048 return VK_NOT_READY;
4049 }
4050 if (fence->fence_wsi) {
4051 VkResult result = fence->fence_wsi->wait(fence->fence_wsi, 0);
4052
4053 if (result != VK_SUCCESS) {
4054 if (result == VK_TIMEOUT)
4055 return VK_NOT_READY;
4056 return result;
4057 }
4058 }
4059 return VK_SUCCESS;
4060 }
4061
4062
4063 // Queue semaphore functions
4064
4065 VkResult radv_CreateSemaphore(
4066 VkDevice _device,
4067 const VkSemaphoreCreateInfo* pCreateInfo,
4068 const VkAllocationCallbacks* pAllocator,
4069 VkSemaphore* pSemaphore)
4070 {
4071 RADV_FROM_HANDLE(radv_device, device, _device);
4072 const VkExportSemaphoreCreateInfo *export =
4073 vk_find_struct_const(pCreateInfo->pNext, EXPORT_SEMAPHORE_CREATE_INFO);
4074 VkExternalSemaphoreHandleTypeFlags handleTypes =
4075 export ? export->handleTypes : 0;
4076
4077 struct radv_semaphore *sem = vk_alloc2(&device->alloc, pAllocator,
4078 sizeof(*sem), 8,
4079 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
4080 if (!sem)
4081 return vk_error(device->instance, VK_ERROR_OUT_OF_HOST_MEMORY);
4082
4083 sem->temp_syncobj = 0;
4084 /* create a syncobject if we are going to export this semaphore */
4085 if (device->always_use_syncobj || handleTypes) {
4086 assert (device->physical_device->rad_info.has_syncobj);
4087 int ret = device->ws->create_syncobj(device->ws, &sem->syncobj);
4088 if (ret) {
4089 vk_free2(&device->alloc, pAllocator, sem);
4090 return vk_error(device->instance, VK_ERROR_OUT_OF_HOST_MEMORY);
4091 }
4092 sem->sem = NULL;
4093 } else {
4094 sem->sem = device->ws->create_sem(device->ws);
4095 if (!sem->sem) {
4096 vk_free2(&device->alloc, pAllocator, sem);
4097 return vk_error(device->instance, VK_ERROR_OUT_OF_HOST_MEMORY);
4098 }
4099 sem->syncobj = 0;
4100 }
4101
4102 *pSemaphore = radv_semaphore_to_handle(sem);
4103 return VK_SUCCESS;
4104 }
4105
4106 void radv_DestroySemaphore(
4107 VkDevice _device,
4108 VkSemaphore _semaphore,
4109 const VkAllocationCallbacks* pAllocator)
4110 {
4111 RADV_FROM_HANDLE(radv_device, device, _device);
4112 RADV_FROM_HANDLE(radv_semaphore, sem, _semaphore);
4113 if (!_semaphore)
4114 return;
4115
4116 if (sem->syncobj)
4117 device->ws->destroy_syncobj(device->ws, sem->syncobj);
4118 else
4119 device->ws->destroy_sem(sem->sem);
4120 vk_free2(&device->alloc, pAllocator, sem);
4121 }
4122
4123 VkResult radv_CreateEvent(
4124 VkDevice _device,
4125 const VkEventCreateInfo* pCreateInfo,
4126 const VkAllocationCallbacks* pAllocator,
4127 VkEvent* pEvent)
4128 {
4129 RADV_FROM_HANDLE(radv_device, device, _device);
4130 struct radv_event *event = vk_alloc2(&device->alloc, pAllocator,
4131 sizeof(*event), 8,
4132 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
4133
4134 if (!event)
4135 return vk_error(device->instance, VK_ERROR_OUT_OF_HOST_MEMORY);
4136
4137 event->bo = device->ws->buffer_create(device->ws, 8, 8,
4138 RADEON_DOMAIN_GTT,
4139 RADEON_FLAG_VA_UNCACHED | RADEON_FLAG_CPU_ACCESS | RADEON_FLAG_NO_INTERPROCESS_SHARING,
4140 RADV_BO_PRIORITY_FENCE);
4141 if (!event->bo) {
4142 vk_free2(&device->alloc, pAllocator, event);
4143 return vk_error(device->instance, VK_ERROR_OUT_OF_DEVICE_MEMORY);
4144 }
4145
4146 event->map = (uint64_t*)device->ws->buffer_map(event->bo);
4147
4148 *pEvent = radv_event_to_handle(event);
4149
4150 return VK_SUCCESS;
4151 }
4152
4153 void radv_DestroyEvent(
4154 VkDevice _device,
4155 VkEvent _event,
4156 const VkAllocationCallbacks* pAllocator)
4157 {
4158 RADV_FROM_HANDLE(radv_device, device, _device);
4159 RADV_FROM_HANDLE(radv_event, event, _event);
4160
4161 if (!event)
4162 return;
4163 device->ws->buffer_destroy(event->bo);
4164 vk_free2(&device->alloc, pAllocator, event);
4165 }
4166
4167 VkResult radv_GetEventStatus(
4168 VkDevice _device,
4169 VkEvent _event)
4170 {
4171 RADV_FROM_HANDLE(radv_event, event, _event);
4172
4173 if (*event->map == 1)
4174 return VK_EVENT_SET;
4175 return VK_EVENT_RESET;
4176 }
4177
4178 VkResult radv_SetEvent(
4179 VkDevice _device,
4180 VkEvent _event)
4181 {
4182 RADV_FROM_HANDLE(radv_event, event, _event);
4183 *event->map = 1;
4184
4185 return VK_SUCCESS;
4186 }
4187
4188 VkResult radv_ResetEvent(
4189 VkDevice _device,
4190 VkEvent _event)
4191 {
4192 RADV_FROM_HANDLE(radv_event, event, _event);
4193 *event->map = 0;
4194
4195 return VK_SUCCESS;
4196 }
4197
4198 VkResult radv_CreateBuffer(
4199 VkDevice _device,
4200 const VkBufferCreateInfo* pCreateInfo,
4201 const VkAllocationCallbacks* pAllocator,
4202 VkBuffer* pBuffer)
4203 {
4204 RADV_FROM_HANDLE(radv_device, device, _device);
4205 struct radv_buffer *buffer;
4206
4207 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO);
4208
4209 buffer = vk_alloc2(&device->alloc, pAllocator, sizeof(*buffer), 8,
4210 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
4211 if (buffer == NULL)
4212 return vk_error(device->instance, VK_ERROR_OUT_OF_HOST_MEMORY);
4213
4214 buffer->size = pCreateInfo->size;
4215 buffer->usage = pCreateInfo->usage;
4216 buffer->bo = NULL;
4217 buffer->offset = 0;
4218 buffer->flags = pCreateInfo->flags;
4219
4220 buffer->shareable = vk_find_struct_const(pCreateInfo->pNext,
4221 EXTERNAL_MEMORY_BUFFER_CREATE_INFO) != NULL;
4222
4223 if (pCreateInfo->flags & VK_BUFFER_CREATE_SPARSE_BINDING_BIT) {
4224 buffer->bo = device->ws->buffer_create(device->ws,
4225 align64(buffer->size, 4096),
4226 4096, 0, RADEON_FLAG_VIRTUAL,
4227 RADV_BO_PRIORITY_VIRTUAL);
4228 if (!buffer->bo) {
4229 vk_free2(&device->alloc, pAllocator, buffer);
4230 return vk_error(device->instance, VK_ERROR_OUT_OF_DEVICE_MEMORY);
4231 }
4232 }
4233
4234 *pBuffer = radv_buffer_to_handle(buffer);
4235
4236 return VK_SUCCESS;
4237 }
4238
4239 void radv_DestroyBuffer(
4240 VkDevice _device,
4241 VkBuffer _buffer,
4242 const VkAllocationCallbacks* pAllocator)
4243 {
4244 RADV_FROM_HANDLE(radv_device, device, _device);
4245 RADV_FROM_HANDLE(radv_buffer, buffer, _buffer);
4246
4247 if (!buffer)
4248 return;
4249
4250 if (buffer->flags & VK_BUFFER_CREATE_SPARSE_BINDING_BIT)
4251 device->ws->buffer_destroy(buffer->bo);
4252
4253 vk_free2(&device->alloc, pAllocator, buffer);
4254 }
4255
4256 VkDeviceAddress radv_GetBufferDeviceAddressEXT(
4257 VkDevice device,
4258 const VkBufferDeviceAddressInfoEXT* pInfo)
4259 {
4260 RADV_FROM_HANDLE(radv_buffer, buffer, pInfo->buffer);
4261 return radv_buffer_get_va(buffer->bo) + buffer->offset;
4262 }
4263
4264
4265 static inline unsigned
4266 si_tile_mode_index(const struct radv_image_plane *plane, unsigned level, bool stencil)
4267 {
4268 if (stencil)
4269 return plane->surface.u.legacy.stencil_tiling_index[level];
4270 else
4271 return plane->surface.u.legacy.tiling_index[level];
4272 }
4273
4274 static uint32_t radv_surface_max_layer_count(struct radv_image_view *iview)
4275 {
4276 return iview->type == VK_IMAGE_VIEW_TYPE_3D ? iview->extent.depth : (iview->base_layer + iview->layer_count);
4277 }
4278
4279 static uint32_t
4280 radv_init_dcc_control_reg(struct radv_device *device,
4281 struct radv_image_view *iview)
4282 {
4283 unsigned max_uncompressed_block_size = V_028C78_MAX_BLOCK_SIZE_256B;
4284 unsigned min_compressed_block_size = V_028C78_MIN_BLOCK_SIZE_32B;
4285 unsigned max_compressed_block_size;
4286 unsigned independent_128b_blocks;
4287 unsigned independent_64b_blocks;
4288
4289 if (!radv_dcc_enabled(iview->image, iview->base_mip))
4290 return 0;
4291
4292 if (!device->physical_device->rad_info.has_dedicated_vram) {
4293 /* amdvlk: [min-compressed-block-size] should be set to 32 for
4294 * dGPU and 64 for APU because all of our APUs to date use
4295 * DIMMs which have a request granularity size of 64B while all
4296 * other chips have a 32B request size.
4297 */
4298 min_compressed_block_size = V_028C78_MIN_BLOCK_SIZE_64B;
4299 }
4300
4301 if (device->physical_device->rad_info.chip_class >= GFX10) {
4302 max_compressed_block_size = V_028C78_MAX_BLOCK_SIZE_128B;
4303 independent_64b_blocks = 0;
4304 independent_128b_blocks = 1;
4305 } else {
4306 independent_128b_blocks = 0;
4307
4308 if (iview->image->info.samples > 1) {
4309 if (iview->image->planes[0].surface.bpe == 1)
4310 max_uncompressed_block_size = V_028C78_MAX_BLOCK_SIZE_64B;
4311 else if (iview->image->planes[0].surface.bpe == 2)
4312 max_uncompressed_block_size = V_028C78_MAX_BLOCK_SIZE_128B;
4313 }
4314
4315 if (iview->image->usage & (VK_IMAGE_USAGE_SAMPLED_BIT |
4316 VK_IMAGE_USAGE_TRANSFER_SRC_BIT |
4317 VK_IMAGE_USAGE_INPUT_ATTACHMENT_BIT)) {
4318 /* If this DCC image is potentially going to be used in texture
4319 * fetches, we need some special settings.
4320 */
4321 independent_64b_blocks = 1;
4322 max_compressed_block_size = V_028C78_MAX_BLOCK_SIZE_64B;
4323 } else {
4324 /* MAX_UNCOMPRESSED_BLOCK_SIZE must be >=
4325 * MAX_COMPRESSED_BLOCK_SIZE. Set MAX_COMPRESSED_BLOCK_SIZE as
4326 * big as possible for better compression state.
4327 */
4328 independent_64b_blocks = 0;
4329 max_compressed_block_size = max_uncompressed_block_size;
4330 }
4331 }
4332
4333 return S_028C78_MAX_UNCOMPRESSED_BLOCK_SIZE(max_uncompressed_block_size) |
4334 S_028C78_MAX_COMPRESSED_BLOCK_SIZE(max_compressed_block_size) |
4335 S_028C78_MIN_COMPRESSED_BLOCK_SIZE(min_compressed_block_size) |
4336 S_028C78_INDEPENDENT_64B_BLOCKS(independent_64b_blocks) |
4337 S_028C78_INDEPENDENT_128B_BLOCKS(independent_128b_blocks);
4338 }
4339
4340 static void
4341 radv_initialise_color_surface(struct radv_device *device,
4342 struct radv_color_buffer_info *cb,
4343 struct radv_image_view *iview)
4344 {
4345 const struct vk_format_description *desc;
4346 unsigned ntype, format, swap, endian;
4347 unsigned blend_clamp = 0, blend_bypass = 0;
4348 uint64_t va;
4349 const struct radv_image_plane *plane = &iview->image->planes[iview->plane_id];
4350 const struct radeon_surf *surf = &plane->surface;
4351
4352 desc = vk_format_description(iview->vk_format);
4353
4354 memset(cb, 0, sizeof(*cb));
4355
4356 /* Intensity is implemented as Red, so treat it that way. */
4357 cb->cb_color_attrib = S_028C74_FORCE_DST_ALPHA_1(desc->swizzle[3] == VK_SWIZZLE_1);
4358
4359 va = radv_buffer_get_va(iview->bo) + iview->image->offset + plane->offset;
4360
4361 cb->cb_color_base = va >> 8;
4362
4363 if (device->physical_device->rad_info.chip_class >= GFX9) {
4364 struct gfx9_surf_meta_flags meta;
4365 if (iview->image->dcc_offset)
4366 meta = surf->u.gfx9.dcc;
4367 else
4368 meta = surf->u.gfx9.cmask;
4369
4370 if (device->physical_device->rad_info.chip_class >= GFX10) {
4371 cb->cb_color_attrib3 |= S_028EE0_COLOR_SW_MODE(surf->u.gfx9.surf.swizzle_mode) |
4372 S_028EE0_FMASK_SW_MODE(surf->u.gfx9.fmask.swizzle_mode) |
4373 S_028EE0_CMASK_PIPE_ALIGNED(surf->u.gfx9.cmask.pipe_aligned) |
4374 S_028EE0_DCC_PIPE_ALIGNED(surf->u.gfx9.dcc.pipe_aligned);
4375 } else {
4376 cb->cb_color_attrib |= S_028C74_COLOR_SW_MODE(surf->u.gfx9.surf.swizzle_mode) |
4377 S_028C74_FMASK_SW_MODE(surf->u.gfx9.fmask.swizzle_mode) |
4378 S_028C74_RB_ALIGNED(meta.rb_aligned) |
4379 S_028C74_PIPE_ALIGNED(meta.pipe_aligned);
4380 cb->cb_mrt_epitch = S_0287A0_EPITCH(surf->u.gfx9.surf.epitch);
4381 }
4382
4383 cb->cb_color_base += surf->u.gfx9.surf_offset >> 8;
4384 cb->cb_color_base |= surf->tile_swizzle;
4385 } else {
4386 const struct legacy_surf_level *level_info = &surf->u.legacy.level[iview->base_mip];
4387 unsigned pitch_tile_max, slice_tile_max, tile_mode_index;
4388
4389 cb->cb_color_base += level_info->offset >> 8;
4390 if (level_info->mode == RADEON_SURF_MODE_2D)
4391 cb->cb_color_base |= surf->tile_swizzle;
4392
4393 pitch_tile_max = level_info->nblk_x / 8 - 1;
4394 slice_tile_max = (level_info->nblk_x * level_info->nblk_y) / 64 - 1;
4395 tile_mode_index = si_tile_mode_index(plane, iview->base_mip, false);
4396
4397 cb->cb_color_pitch = S_028C64_TILE_MAX(pitch_tile_max);
4398 cb->cb_color_slice = S_028C68_TILE_MAX(slice_tile_max);
4399 cb->cb_color_cmask_slice = iview->image->cmask.slice_tile_max;
4400
4401 cb->cb_color_attrib |= S_028C74_TILE_MODE_INDEX(tile_mode_index);
4402
4403 if (radv_image_has_fmask(iview->image)) {
4404 if (device->physical_device->rad_info.chip_class >= GFX7)
4405 cb->cb_color_pitch |= S_028C64_FMASK_TILE_MAX(iview->image->fmask.pitch_in_pixels / 8 - 1);
4406 cb->cb_color_attrib |= S_028C74_FMASK_TILE_MODE_INDEX(iview->image->fmask.tile_mode_index);
4407 cb->cb_color_fmask_slice = S_028C88_TILE_MAX(iview->image->fmask.slice_tile_max);
4408 } else {
4409 /* This must be set for fast clear to work without FMASK. */
4410 if (device->physical_device->rad_info.chip_class >= GFX7)
4411 cb->cb_color_pitch |= S_028C64_FMASK_TILE_MAX(pitch_tile_max);
4412 cb->cb_color_attrib |= S_028C74_FMASK_TILE_MODE_INDEX(tile_mode_index);
4413 cb->cb_color_fmask_slice = S_028C88_TILE_MAX(slice_tile_max);
4414 }
4415 }
4416
4417 /* CMASK variables */
4418 va = radv_buffer_get_va(iview->bo) + iview->image->offset;
4419 va += iview->image->cmask.offset;
4420 cb->cb_color_cmask = va >> 8;
4421
4422 va = radv_buffer_get_va(iview->bo) + iview->image->offset;
4423 va += iview->image->dcc_offset;
4424
4425 if (radv_dcc_enabled(iview->image, iview->base_mip) &&
4426 device->physical_device->rad_info.chip_class <= GFX8)
4427 va += plane->surface.u.legacy.level[iview->base_mip].dcc_offset;
4428
4429 unsigned dcc_tile_swizzle = surf->tile_swizzle;
4430 dcc_tile_swizzle &= (surf->dcc_alignment - 1) >> 8;
4431
4432 cb->cb_dcc_base = va >> 8;
4433 cb->cb_dcc_base |= dcc_tile_swizzle;
4434
4435 /* GFX10 field has the same base shift as the GFX6 field. */
4436 uint32_t max_slice = radv_surface_max_layer_count(iview) - 1;
4437 cb->cb_color_view = S_028C6C_SLICE_START(iview->base_layer) |
4438 S_028C6C_SLICE_MAX_GFX10(max_slice);
4439
4440 if (iview->image->info.samples > 1) {
4441 unsigned log_samples = util_logbase2(iview->image->info.samples);
4442
4443 cb->cb_color_attrib |= S_028C74_NUM_SAMPLES(log_samples) |
4444 S_028C74_NUM_FRAGMENTS(log_samples);
4445 }
4446
4447 if (radv_image_has_fmask(iview->image)) {
4448 va = radv_buffer_get_va(iview->bo) + iview->image->offset + iview->image->fmask.offset;
4449 cb->cb_color_fmask = va >> 8;
4450 cb->cb_color_fmask |= iview->image->fmask.tile_swizzle;
4451 } else {
4452 cb->cb_color_fmask = cb->cb_color_base;
4453 }
4454
4455 ntype = radv_translate_color_numformat(iview->vk_format,
4456 desc,
4457 vk_format_get_first_non_void_channel(iview->vk_format));
4458 format = radv_translate_colorformat(iview->vk_format);
4459 if (format == V_028C70_COLOR_INVALID || ntype == ~0u)
4460 radv_finishme("Illegal color\n");
4461 swap = radv_translate_colorswap(iview->vk_format, FALSE);
4462 endian = radv_colorformat_endian_swap(format);
4463
4464 /* blend clamp should be set for all NORM/SRGB types */
4465 if (ntype == V_028C70_NUMBER_UNORM ||
4466 ntype == V_028C70_NUMBER_SNORM ||
4467 ntype == V_028C70_NUMBER_SRGB)
4468 blend_clamp = 1;
4469
4470 /* set blend bypass according to docs if SINT/UINT or
4471 8/24 COLOR variants */
4472 if (ntype == V_028C70_NUMBER_UINT || ntype == V_028C70_NUMBER_SINT ||
4473 format == V_028C70_COLOR_8_24 || format == V_028C70_COLOR_24_8 ||
4474 format == V_028C70_COLOR_X24_8_32_FLOAT) {
4475 blend_clamp = 0;
4476 blend_bypass = 1;
4477 }
4478 #if 0
4479 if ((ntype == V_028C70_NUMBER_UINT || ntype == V_028C70_NUMBER_SINT) &&
4480 (format == V_028C70_COLOR_8 ||
4481 format == V_028C70_COLOR_8_8 ||
4482 format == V_028C70_COLOR_8_8_8_8))
4483 ->color_is_int8 = true;
4484 #endif
4485 cb->cb_color_info = S_028C70_FORMAT(format) |
4486 S_028C70_COMP_SWAP(swap) |
4487 S_028C70_BLEND_CLAMP(blend_clamp) |
4488 S_028C70_BLEND_BYPASS(blend_bypass) |
4489 S_028C70_SIMPLE_FLOAT(1) |
4490 S_028C70_ROUND_MODE(ntype != V_028C70_NUMBER_UNORM &&
4491 ntype != V_028C70_NUMBER_SNORM &&
4492 ntype != V_028C70_NUMBER_SRGB &&
4493 format != V_028C70_COLOR_8_24 &&
4494 format != V_028C70_COLOR_24_8) |
4495 S_028C70_NUMBER_TYPE(ntype) |
4496 S_028C70_ENDIAN(endian);
4497 if (radv_image_has_fmask(iview->image)) {
4498 cb->cb_color_info |= S_028C70_COMPRESSION(1);
4499 if (device->physical_device->rad_info.chip_class == GFX6) {
4500 unsigned fmask_bankh = util_logbase2(iview->image->fmask.bank_height);
4501 cb->cb_color_attrib |= S_028C74_FMASK_BANK_HEIGHT(fmask_bankh);
4502 }
4503
4504 if (radv_image_is_tc_compat_cmask(iview->image)) {
4505 /* Allow the texture block to read FMASK directly
4506 * without decompressing it. This bit must be cleared
4507 * when performing FMASK_DECOMPRESS or DCC_COMPRESS,
4508 * otherwise the operation doesn't happen.
4509 */
4510 cb->cb_color_info |= S_028C70_FMASK_COMPRESS_1FRAG_ONLY(1);
4511
4512 /* Set CMASK into a tiling format that allows the
4513 * texture block to read it.
4514 */
4515 cb->cb_color_info |= S_028C70_CMASK_ADDR_TYPE(2);
4516 }
4517 }
4518
4519 if (radv_image_has_cmask(iview->image) &&
4520 !(device->instance->debug_flags & RADV_DEBUG_NO_FAST_CLEARS))
4521 cb->cb_color_info |= S_028C70_FAST_CLEAR(1);
4522
4523 if (radv_dcc_enabled(iview->image, iview->base_mip))
4524 cb->cb_color_info |= S_028C70_DCC_ENABLE(1);
4525
4526 cb->cb_dcc_control = radv_init_dcc_control_reg(device, iview);
4527
4528 /* This must be set for fast clear to work without FMASK. */
4529 if (!radv_image_has_fmask(iview->image) &&
4530 device->physical_device->rad_info.chip_class == GFX6) {
4531 unsigned bankh = util_logbase2(surf->u.legacy.bankh);
4532 cb->cb_color_attrib |= S_028C74_FMASK_BANK_HEIGHT(bankh);
4533 }
4534
4535 if (device->physical_device->rad_info.chip_class >= GFX9) {
4536 const struct vk_format_description *format_desc = vk_format_description(iview->image->vk_format);
4537
4538 unsigned mip0_depth = iview->image->type == VK_IMAGE_TYPE_3D ?
4539 (iview->extent.depth - 1) : (iview->image->info.array_size - 1);
4540 unsigned width = iview->extent.width / (iview->plane_id ? format_desc->width_divisor : 1);
4541 unsigned height = iview->extent.height / (iview->plane_id ? format_desc->height_divisor : 1);
4542
4543 if (device->physical_device->rad_info.chip_class >= GFX10) {
4544 cb->cb_color_view |= S_028C6C_MIP_LEVEL_GFX10(iview->base_mip);
4545
4546 cb->cb_color_attrib3 |= S_028EE0_MIP0_DEPTH(mip0_depth) |
4547 S_028EE0_RESOURCE_TYPE(surf->u.gfx9.resource_type) |
4548 S_028EE0_RESOURCE_LEVEL(1);
4549 } else {
4550 cb->cb_color_view |= S_028C6C_MIP_LEVEL_GFX9(iview->base_mip);
4551 cb->cb_color_attrib |= S_028C74_MIP0_DEPTH(mip0_depth) |
4552 S_028C74_RESOURCE_TYPE(surf->u.gfx9.resource_type);
4553 }
4554
4555 cb->cb_color_attrib2 = S_028C68_MIP0_WIDTH(width - 1) |
4556 S_028C68_MIP0_HEIGHT(height - 1) |
4557 S_028C68_MAX_MIP(iview->image->info.levels - 1);
4558 }
4559 }
4560
4561 static unsigned
4562 radv_calc_decompress_on_z_planes(struct radv_device *device,
4563 struct radv_image_view *iview)
4564 {
4565 unsigned max_zplanes = 0;
4566
4567 assert(radv_image_is_tc_compat_htile(iview->image));
4568
4569 if (device->physical_device->rad_info.chip_class >= GFX9) {
4570 /* Default value for 32-bit depth surfaces. */
4571 max_zplanes = 4;
4572
4573 if (iview->vk_format == VK_FORMAT_D16_UNORM &&
4574 iview->image->info.samples > 1)
4575 max_zplanes = 2;
4576
4577 max_zplanes = max_zplanes + 1;
4578 } else {
4579 if (iview->vk_format == VK_FORMAT_D16_UNORM) {
4580 /* Do not enable Z plane compression for 16-bit depth
4581 * surfaces because isn't supported on GFX8. Only
4582 * 32-bit depth surfaces are supported by the hardware.
4583 * This allows to maintain shader compatibility and to
4584 * reduce the number of depth decompressions.
4585 */
4586 max_zplanes = 1;
4587 } else {
4588 if (iview->image->info.samples <= 1)
4589 max_zplanes = 5;
4590 else if (iview->image->info.samples <= 4)
4591 max_zplanes = 3;
4592 else
4593 max_zplanes = 2;
4594 }
4595 }
4596
4597 return max_zplanes;
4598 }
4599
4600 static void
4601 radv_initialise_ds_surface(struct radv_device *device,
4602 struct radv_ds_buffer_info *ds,
4603 struct radv_image_view *iview)
4604 {
4605 unsigned level = iview->base_mip;
4606 unsigned format, stencil_format;
4607 uint64_t va, s_offs, z_offs;
4608 bool stencil_only = false;
4609 const struct radv_image_plane *plane = &iview->image->planes[0];
4610 const struct radeon_surf *surf = &plane->surface;
4611
4612 assert(vk_format_get_plane_count(iview->image->vk_format) == 1);
4613
4614 memset(ds, 0, sizeof(*ds));
4615 switch (iview->image->vk_format) {
4616 case VK_FORMAT_D24_UNORM_S8_UINT:
4617 case VK_FORMAT_X8_D24_UNORM_PACK32:
4618 ds->pa_su_poly_offset_db_fmt_cntl = S_028B78_POLY_OFFSET_NEG_NUM_DB_BITS(-24);
4619 ds->offset_scale = 2.0f;
4620 break;
4621 case VK_FORMAT_D16_UNORM:
4622 case VK_FORMAT_D16_UNORM_S8_UINT:
4623 ds->pa_su_poly_offset_db_fmt_cntl = S_028B78_POLY_OFFSET_NEG_NUM_DB_BITS(-16);
4624 ds->offset_scale = 4.0f;
4625 break;
4626 case VK_FORMAT_D32_SFLOAT:
4627 case VK_FORMAT_D32_SFLOAT_S8_UINT:
4628 ds->pa_su_poly_offset_db_fmt_cntl = S_028B78_POLY_OFFSET_NEG_NUM_DB_BITS(-23) |
4629 S_028B78_POLY_OFFSET_DB_IS_FLOAT_FMT(1);
4630 ds->offset_scale = 1.0f;
4631 break;
4632 case VK_FORMAT_S8_UINT:
4633 stencil_only = true;
4634 break;
4635 default:
4636 break;
4637 }
4638
4639 format = radv_translate_dbformat(iview->image->vk_format);
4640 stencil_format = surf->has_stencil ?
4641 V_028044_STENCIL_8 : V_028044_STENCIL_INVALID;
4642
4643 uint32_t max_slice = radv_surface_max_layer_count(iview) - 1;
4644 ds->db_depth_view = S_028008_SLICE_START(iview->base_layer) |
4645 S_028008_SLICE_MAX(max_slice);
4646 if (device->physical_device->rad_info.chip_class >= GFX10) {
4647 ds->db_depth_view |= S_028008_SLICE_START_HI(iview->base_layer >> 11) |
4648 S_028008_SLICE_MAX_HI(max_slice >> 11);
4649 }
4650
4651 ds->db_htile_data_base = 0;
4652 ds->db_htile_surface = 0;
4653
4654 va = radv_buffer_get_va(iview->bo) + iview->image->offset;
4655 s_offs = z_offs = va;
4656
4657 if (device->physical_device->rad_info.chip_class >= GFX9) {
4658 assert(surf->u.gfx9.surf_offset == 0);
4659 s_offs += surf->u.gfx9.stencil_offset;
4660
4661 ds->db_z_info = S_028038_FORMAT(format) |
4662 S_028038_NUM_SAMPLES(util_logbase2(iview->image->info.samples)) |
4663 S_028038_SW_MODE(surf->u.gfx9.surf.swizzle_mode) |
4664 S_028038_MAXMIP(iview->image->info.levels - 1) |
4665 S_028038_ZRANGE_PRECISION(1);
4666 ds->db_stencil_info = S_02803C_FORMAT(stencil_format) |
4667 S_02803C_SW_MODE(surf->u.gfx9.stencil.swizzle_mode);
4668
4669 if (device->physical_device->rad_info.chip_class == GFX9) {
4670 ds->db_z_info2 = S_028068_EPITCH(surf->u.gfx9.surf.epitch);
4671 ds->db_stencil_info2 = S_02806C_EPITCH(surf->u.gfx9.stencil.epitch);
4672 }
4673
4674 ds->db_depth_view |= S_028008_MIPID(level);
4675 ds->db_depth_size = S_02801C_X_MAX(iview->image->info.width - 1) |
4676 S_02801C_Y_MAX(iview->image->info.height - 1);
4677
4678 if (radv_htile_enabled(iview->image, level)) {
4679 ds->db_z_info |= S_028038_TILE_SURFACE_ENABLE(1);
4680
4681 if (radv_image_is_tc_compat_htile(iview->image)) {
4682 unsigned max_zplanes =
4683 radv_calc_decompress_on_z_planes(device, iview);
4684
4685 ds->db_z_info |= S_028038_DECOMPRESS_ON_N_ZPLANES(max_zplanes);
4686
4687 if (device->physical_device->rad_info.chip_class >= GFX10) {
4688 ds->db_z_info |= S_028040_ITERATE_FLUSH(1);
4689 ds->db_stencil_info |= S_028044_ITERATE_FLUSH(1);
4690 } else {
4691 ds->db_z_info |= S_028038_ITERATE_FLUSH(1);
4692 ds->db_stencil_info |= S_02803C_ITERATE_FLUSH(1);
4693 }
4694 }
4695
4696 if (!surf->has_stencil)
4697 /* Use all of the htile_buffer for depth if there's no stencil. */
4698 ds->db_stencil_info |= S_02803C_TILE_STENCIL_DISABLE(1);
4699 va = radv_buffer_get_va(iview->bo) + iview->image->offset +
4700 iview->image->htile_offset;
4701 ds->db_htile_data_base = va >> 8;
4702 ds->db_htile_surface = S_028ABC_FULL_CACHE(1) |
4703 S_028ABC_PIPE_ALIGNED(surf->u.gfx9.htile.pipe_aligned);
4704
4705 if (device->physical_device->rad_info.chip_class == GFX9) {
4706 ds->db_htile_surface |= S_028ABC_RB_ALIGNED(surf->u.gfx9.htile.rb_aligned);
4707 }
4708 }
4709 } else {
4710 const struct legacy_surf_level *level_info = &surf->u.legacy.level[level];
4711
4712 if (stencil_only)
4713 level_info = &surf->u.legacy.stencil_level[level];
4714
4715 z_offs += surf->u.legacy.level[level].offset;
4716 s_offs += surf->u.legacy.stencil_level[level].offset;
4717
4718 ds->db_depth_info = S_02803C_ADDR5_SWIZZLE_MASK(!radv_image_is_tc_compat_htile(iview->image));
4719 ds->db_z_info = S_028040_FORMAT(format) | S_028040_ZRANGE_PRECISION(1);
4720 ds->db_stencil_info = S_028044_FORMAT(stencil_format);
4721
4722 if (iview->image->info.samples > 1)
4723 ds->db_z_info |= S_028040_NUM_SAMPLES(util_logbase2(iview->image->info.samples));
4724
4725 if (device->physical_device->rad_info.chip_class >= GFX7) {
4726 struct radeon_info *info = &device->physical_device->rad_info;
4727 unsigned tiling_index = surf->u.legacy.tiling_index[level];
4728 unsigned stencil_index = surf->u.legacy.stencil_tiling_index[level];
4729 unsigned macro_index = surf->u.legacy.macro_tile_index;
4730 unsigned tile_mode = info->si_tile_mode_array[tiling_index];
4731 unsigned stencil_tile_mode = info->si_tile_mode_array[stencil_index];
4732 unsigned macro_mode = info->cik_macrotile_mode_array[macro_index];
4733
4734 if (stencil_only)
4735 tile_mode = stencil_tile_mode;
4736
4737 ds->db_depth_info |=
4738 S_02803C_ARRAY_MODE(G_009910_ARRAY_MODE(tile_mode)) |
4739 S_02803C_PIPE_CONFIG(G_009910_PIPE_CONFIG(tile_mode)) |
4740 S_02803C_BANK_WIDTH(G_009990_BANK_WIDTH(macro_mode)) |
4741 S_02803C_BANK_HEIGHT(G_009990_BANK_HEIGHT(macro_mode)) |
4742 S_02803C_MACRO_TILE_ASPECT(G_009990_MACRO_TILE_ASPECT(macro_mode)) |
4743 S_02803C_NUM_BANKS(G_009990_NUM_BANKS(macro_mode));
4744 ds->db_z_info |= S_028040_TILE_SPLIT(G_009910_TILE_SPLIT(tile_mode));
4745 ds->db_stencil_info |= S_028044_TILE_SPLIT(G_009910_TILE_SPLIT(stencil_tile_mode));
4746 } else {
4747 unsigned tile_mode_index = si_tile_mode_index(&iview->image->planes[0], level, false);
4748 ds->db_z_info |= S_028040_TILE_MODE_INDEX(tile_mode_index);
4749 tile_mode_index = si_tile_mode_index(&iview->image->planes[0], level, true);
4750 ds->db_stencil_info |= S_028044_TILE_MODE_INDEX(tile_mode_index);
4751 if (stencil_only)
4752 ds->db_z_info |= S_028040_TILE_MODE_INDEX(tile_mode_index);
4753 }
4754
4755 ds->db_depth_size = S_028058_PITCH_TILE_MAX((level_info->nblk_x / 8) - 1) |
4756 S_028058_HEIGHT_TILE_MAX((level_info->nblk_y / 8) - 1);
4757 ds->db_depth_slice = S_02805C_SLICE_TILE_MAX((level_info->nblk_x * level_info->nblk_y) / 64 - 1);
4758
4759 if (radv_htile_enabled(iview->image, level)) {
4760 ds->db_z_info |= S_028040_TILE_SURFACE_ENABLE(1);
4761
4762 if (!surf->has_stencil &&
4763 !radv_image_is_tc_compat_htile(iview->image))
4764 /* Use all of the htile_buffer for depth if there's no stencil. */
4765 ds->db_stencil_info |= S_028044_TILE_STENCIL_DISABLE(1);
4766
4767 va = radv_buffer_get_va(iview->bo) + iview->image->offset +
4768 iview->image->htile_offset;
4769 ds->db_htile_data_base = va >> 8;
4770 ds->db_htile_surface = S_028ABC_FULL_CACHE(1);
4771
4772 if (radv_image_is_tc_compat_htile(iview->image)) {
4773 unsigned max_zplanes =
4774 radv_calc_decompress_on_z_planes(device, iview);
4775
4776 ds->db_htile_surface |= S_028ABC_TC_COMPATIBLE(1);
4777 ds->db_z_info |= S_028040_DECOMPRESS_ON_N_ZPLANES(max_zplanes);
4778 }
4779 }
4780 }
4781
4782 ds->db_z_read_base = ds->db_z_write_base = z_offs >> 8;
4783 ds->db_stencil_read_base = ds->db_stencil_write_base = s_offs >> 8;
4784 }
4785
4786 VkResult radv_CreateFramebuffer(
4787 VkDevice _device,
4788 const VkFramebufferCreateInfo* pCreateInfo,
4789 const VkAllocationCallbacks* pAllocator,
4790 VkFramebuffer* pFramebuffer)
4791 {
4792 RADV_FROM_HANDLE(radv_device, device, _device);
4793 struct radv_framebuffer *framebuffer;
4794
4795 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO);
4796
4797 size_t size = sizeof(*framebuffer) +
4798 sizeof(struct radv_attachment_info) * pCreateInfo->attachmentCount;
4799 framebuffer = vk_alloc2(&device->alloc, pAllocator, size, 8,
4800 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
4801 if (framebuffer == NULL)
4802 return vk_error(device->instance, VK_ERROR_OUT_OF_HOST_MEMORY);
4803
4804 framebuffer->attachment_count = pCreateInfo->attachmentCount;
4805 framebuffer->width = pCreateInfo->width;
4806 framebuffer->height = pCreateInfo->height;
4807 framebuffer->layers = pCreateInfo->layers;
4808 for (uint32_t i = 0; i < pCreateInfo->attachmentCount; i++) {
4809 VkImageView _iview = pCreateInfo->pAttachments[i];
4810 struct radv_image_view *iview = radv_image_view_from_handle(_iview);
4811 framebuffer->attachments[i].attachment = iview;
4812 if (iview->aspect_mask & (VK_IMAGE_ASPECT_DEPTH_BIT | VK_IMAGE_ASPECT_STENCIL_BIT)) {
4813 radv_initialise_ds_surface(device, &framebuffer->attachments[i].ds, iview);
4814 } else {
4815 radv_initialise_color_surface(device, &framebuffer->attachments[i].cb, iview);
4816 }
4817 framebuffer->width = MIN2(framebuffer->width, iview->extent.width);
4818 framebuffer->height = MIN2(framebuffer->height, iview->extent.height);
4819 framebuffer->layers = MIN2(framebuffer->layers, radv_surface_max_layer_count(iview));
4820 }
4821
4822 *pFramebuffer = radv_framebuffer_to_handle(framebuffer);
4823 return VK_SUCCESS;
4824 }
4825
4826 void radv_DestroyFramebuffer(
4827 VkDevice _device,
4828 VkFramebuffer _fb,
4829 const VkAllocationCallbacks* pAllocator)
4830 {
4831 RADV_FROM_HANDLE(radv_device, device, _device);
4832 RADV_FROM_HANDLE(radv_framebuffer, fb, _fb);
4833
4834 if (!fb)
4835 return;
4836 vk_free2(&device->alloc, pAllocator, fb);
4837 }
4838
4839 static unsigned radv_tex_wrap(VkSamplerAddressMode address_mode)
4840 {
4841 switch (address_mode) {
4842 case VK_SAMPLER_ADDRESS_MODE_REPEAT:
4843 return V_008F30_SQ_TEX_WRAP;
4844 case VK_SAMPLER_ADDRESS_MODE_MIRRORED_REPEAT:
4845 return V_008F30_SQ_TEX_MIRROR;
4846 case VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE:
4847 return V_008F30_SQ_TEX_CLAMP_LAST_TEXEL;
4848 case VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_BORDER:
4849 return V_008F30_SQ_TEX_CLAMP_BORDER;
4850 case VK_SAMPLER_ADDRESS_MODE_MIRROR_CLAMP_TO_EDGE:
4851 return V_008F30_SQ_TEX_MIRROR_ONCE_LAST_TEXEL;
4852 default:
4853 unreachable("illegal tex wrap mode");
4854 break;
4855 }
4856 }
4857
4858 static unsigned
4859 radv_tex_compare(VkCompareOp op)
4860 {
4861 switch (op) {
4862 case VK_COMPARE_OP_NEVER:
4863 return V_008F30_SQ_TEX_DEPTH_COMPARE_NEVER;
4864 case VK_COMPARE_OP_LESS:
4865 return V_008F30_SQ_TEX_DEPTH_COMPARE_LESS;
4866 case VK_COMPARE_OP_EQUAL:
4867 return V_008F30_SQ_TEX_DEPTH_COMPARE_EQUAL;
4868 case VK_COMPARE_OP_LESS_OR_EQUAL:
4869 return V_008F30_SQ_TEX_DEPTH_COMPARE_LESSEQUAL;
4870 case VK_COMPARE_OP_GREATER:
4871 return V_008F30_SQ_TEX_DEPTH_COMPARE_GREATER;
4872 case VK_COMPARE_OP_NOT_EQUAL:
4873 return V_008F30_SQ_TEX_DEPTH_COMPARE_NOTEQUAL;
4874 case VK_COMPARE_OP_GREATER_OR_EQUAL:
4875 return V_008F30_SQ_TEX_DEPTH_COMPARE_GREATEREQUAL;
4876 case VK_COMPARE_OP_ALWAYS:
4877 return V_008F30_SQ_TEX_DEPTH_COMPARE_ALWAYS;
4878 default:
4879 unreachable("illegal compare mode");
4880 break;
4881 }
4882 }
4883
4884 static unsigned
4885 radv_tex_filter(VkFilter filter, unsigned max_ansio)
4886 {
4887 switch (filter) {
4888 case VK_FILTER_NEAREST:
4889 return (max_ansio > 1 ? V_008F38_SQ_TEX_XY_FILTER_ANISO_POINT :
4890 V_008F38_SQ_TEX_XY_FILTER_POINT);
4891 case VK_FILTER_LINEAR:
4892 return (max_ansio > 1 ? V_008F38_SQ_TEX_XY_FILTER_ANISO_BILINEAR :
4893 V_008F38_SQ_TEX_XY_FILTER_BILINEAR);
4894 case VK_FILTER_CUBIC_IMG:
4895 default:
4896 fprintf(stderr, "illegal texture filter");
4897 return 0;
4898 }
4899 }
4900
4901 static unsigned
4902 radv_tex_mipfilter(VkSamplerMipmapMode mode)
4903 {
4904 switch (mode) {
4905 case VK_SAMPLER_MIPMAP_MODE_NEAREST:
4906 return V_008F38_SQ_TEX_Z_FILTER_POINT;
4907 case VK_SAMPLER_MIPMAP_MODE_LINEAR:
4908 return V_008F38_SQ_TEX_Z_FILTER_LINEAR;
4909 default:
4910 return V_008F38_SQ_TEX_Z_FILTER_NONE;
4911 }
4912 }
4913
4914 static unsigned
4915 radv_tex_bordercolor(VkBorderColor bcolor)
4916 {
4917 switch (bcolor) {
4918 case VK_BORDER_COLOR_FLOAT_TRANSPARENT_BLACK:
4919 case VK_BORDER_COLOR_INT_TRANSPARENT_BLACK:
4920 return V_008F3C_SQ_TEX_BORDER_COLOR_TRANS_BLACK;
4921 case VK_BORDER_COLOR_FLOAT_OPAQUE_BLACK:
4922 case VK_BORDER_COLOR_INT_OPAQUE_BLACK:
4923 return V_008F3C_SQ_TEX_BORDER_COLOR_OPAQUE_BLACK;
4924 case VK_BORDER_COLOR_FLOAT_OPAQUE_WHITE:
4925 case VK_BORDER_COLOR_INT_OPAQUE_WHITE:
4926 return V_008F3C_SQ_TEX_BORDER_COLOR_OPAQUE_WHITE;
4927 default:
4928 break;
4929 }
4930 return 0;
4931 }
4932
4933 static unsigned
4934 radv_tex_aniso_filter(unsigned filter)
4935 {
4936 if (filter < 2)
4937 return 0;
4938 if (filter < 4)
4939 return 1;
4940 if (filter < 8)
4941 return 2;
4942 if (filter < 16)
4943 return 3;
4944 return 4;
4945 }
4946
4947 static unsigned
4948 radv_tex_filter_mode(VkSamplerReductionModeEXT mode)
4949 {
4950 switch (mode) {
4951 case VK_SAMPLER_REDUCTION_MODE_WEIGHTED_AVERAGE_EXT:
4952 return V_008F30_SQ_IMG_FILTER_MODE_BLEND;
4953 case VK_SAMPLER_REDUCTION_MODE_MIN_EXT:
4954 return V_008F30_SQ_IMG_FILTER_MODE_MIN;
4955 case VK_SAMPLER_REDUCTION_MODE_MAX_EXT:
4956 return V_008F30_SQ_IMG_FILTER_MODE_MAX;
4957 default:
4958 break;
4959 }
4960 return 0;
4961 }
4962
4963 static uint32_t
4964 radv_get_max_anisotropy(struct radv_device *device,
4965 const VkSamplerCreateInfo *pCreateInfo)
4966 {
4967 if (device->force_aniso >= 0)
4968 return device->force_aniso;
4969
4970 if (pCreateInfo->anisotropyEnable &&
4971 pCreateInfo->maxAnisotropy > 1.0f)
4972 return (uint32_t)pCreateInfo->maxAnisotropy;
4973
4974 return 0;
4975 }
4976
4977 static void
4978 radv_init_sampler(struct radv_device *device,
4979 struct radv_sampler *sampler,
4980 const VkSamplerCreateInfo *pCreateInfo)
4981 {
4982 uint32_t max_aniso = radv_get_max_anisotropy(device, pCreateInfo);
4983 uint32_t max_aniso_ratio = radv_tex_aniso_filter(max_aniso);
4984 bool compat_mode = device->physical_device->rad_info.chip_class == GFX8 ||
4985 device->physical_device->rad_info.chip_class == GFX9;
4986 unsigned filter_mode = V_008F30_SQ_IMG_FILTER_MODE_BLEND;
4987
4988 const struct VkSamplerReductionModeCreateInfoEXT *sampler_reduction =
4989 vk_find_struct_const(pCreateInfo->pNext,
4990 SAMPLER_REDUCTION_MODE_CREATE_INFO_EXT);
4991 if (sampler_reduction)
4992 filter_mode = radv_tex_filter_mode(sampler_reduction->reductionMode);
4993
4994 sampler->state[0] = (S_008F30_CLAMP_X(radv_tex_wrap(pCreateInfo->addressModeU)) |
4995 S_008F30_CLAMP_Y(radv_tex_wrap(pCreateInfo->addressModeV)) |
4996 S_008F30_CLAMP_Z(radv_tex_wrap(pCreateInfo->addressModeW)) |
4997 S_008F30_MAX_ANISO_RATIO(max_aniso_ratio) |
4998 S_008F30_DEPTH_COMPARE_FUNC(radv_tex_compare(pCreateInfo->compareOp)) |
4999 S_008F30_FORCE_UNNORMALIZED(pCreateInfo->unnormalizedCoordinates ? 1 : 0) |
5000 S_008F30_ANISO_THRESHOLD(max_aniso_ratio >> 1) |
5001 S_008F30_ANISO_BIAS(max_aniso_ratio) |
5002 S_008F30_DISABLE_CUBE_WRAP(0) |
5003 S_008F30_COMPAT_MODE(compat_mode) |
5004 S_008F30_FILTER_MODE(filter_mode));
5005 sampler->state[1] = (S_008F34_MIN_LOD(S_FIXED(CLAMP(pCreateInfo->minLod, 0, 15), 8)) |
5006 S_008F34_MAX_LOD(S_FIXED(CLAMP(pCreateInfo->maxLod, 0, 15), 8)) |
5007 S_008F34_PERF_MIP(max_aniso_ratio ? max_aniso_ratio + 6 : 0));
5008 sampler->state[2] = (S_008F38_LOD_BIAS(S_FIXED(CLAMP(pCreateInfo->mipLodBias, -16, 16), 8)) |
5009 S_008F38_XY_MAG_FILTER(radv_tex_filter(pCreateInfo->magFilter, max_aniso)) |
5010 S_008F38_XY_MIN_FILTER(radv_tex_filter(pCreateInfo->minFilter, max_aniso)) |
5011 S_008F38_MIP_FILTER(radv_tex_mipfilter(pCreateInfo->mipmapMode)) |
5012 S_008F38_MIP_POINT_PRECLAMP(0));
5013 sampler->state[3] = (S_008F3C_BORDER_COLOR_PTR(0) |
5014 S_008F3C_BORDER_COLOR_TYPE(radv_tex_bordercolor(pCreateInfo->borderColor)));
5015
5016 if (device->physical_device->rad_info.chip_class >= GFX10) {
5017 sampler->state[2] |= S_008F38_ANISO_OVERRIDE_GFX10(1);
5018 } else {
5019 sampler->state[2] |=
5020 S_008F38_DISABLE_LSB_CEIL(device->physical_device->rad_info.chip_class <= GFX8) |
5021 S_008F38_FILTER_PREC_FIX(1) |
5022 S_008F38_ANISO_OVERRIDE_GFX6(device->physical_device->rad_info.chip_class >= GFX8);
5023 }
5024 }
5025
5026 VkResult radv_CreateSampler(
5027 VkDevice _device,
5028 const VkSamplerCreateInfo* pCreateInfo,
5029 const VkAllocationCallbacks* pAllocator,
5030 VkSampler* pSampler)
5031 {
5032 RADV_FROM_HANDLE(radv_device, device, _device);
5033 struct radv_sampler *sampler;
5034
5035 const struct VkSamplerYcbcrConversionInfo *ycbcr_conversion =
5036 vk_find_struct_const(pCreateInfo->pNext,
5037 SAMPLER_YCBCR_CONVERSION_INFO);
5038
5039 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO);
5040
5041 sampler = vk_alloc2(&device->alloc, pAllocator, sizeof(*sampler), 8,
5042 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
5043 if (!sampler)
5044 return vk_error(device->instance, VK_ERROR_OUT_OF_HOST_MEMORY);
5045
5046 radv_init_sampler(device, sampler, pCreateInfo);
5047
5048 sampler->ycbcr_sampler = ycbcr_conversion ? radv_sampler_ycbcr_conversion_from_handle(ycbcr_conversion->conversion): NULL;
5049 *pSampler = radv_sampler_to_handle(sampler);
5050
5051 return VK_SUCCESS;
5052 }
5053
5054 void radv_DestroySampler(
5055 VkDevice _device,
5056 VkSampler _sampler,
5057 const VkAllocationCallbacks* pAllocator)
5058 {
5059 RADV_FROM_HANDLE(radv_device, device, _device);
5060 RADV_FROM_HANDLE(radv_sampler, sampler, _sampler);
5061
5062 if (!sampler)
5063 return;
5064 vk_free2(&device->alloc, pAllocator, sampler);
5065 }
5066
5067 /* vk_icd.h does not declare this function, so we declare it here to
5068 * suppress Wmissing-prototypes.
5069 */
5070 PUBLIC VKAPI_ATTR VkResult VKAPI_CALL
5071 vk_icdNegotiateLoaderICDInterfaceVersion(uint32_t *pSupportedVersion);
5072
5073 PUBLIC VKAPI_ATTR VkResult VKAPI_CALL
5074 vk_icdNegotiateLoaderICDInterfaceVersion(uint32_t *pSupportedVersion)
5075 {
5076 /* For the full details on loader interface versioning, see
5077 * <https://github.com/KhronosGroup/Vulkan-LoaderAndValidationLayers/blob/master/loader/LoaderAndLayerInterface.md>.
5078 * What follows is a condensed summary, to help you navigate the large and
5079 * confusing official doc.
5080 *
5081 * - Loader interface v0 is incompatible with later versions. We don't
5082 * support it.
5083 *
5084 * - In loader interface v1:
5085 * - The first ICD entrypoint called by the loader is
5086 * vk_icdGetInstanceProcAddr(). The ICD must statically expose this
5087 * entrypoint.
5088 * - The ICD must statically expose no other Vulkan symbol unless it is
5089 * linked with -Bsymbolic.
5090 * - Each dispatchable Vulkan handle created by the ICD must be
5091 * a pointer to a struct whose first member is VK_LOADER_DATA. The
5092 * ICD must initialize VK_LOADER_DATA.loadMagic to ICD_LOADER_MAGIC.
5093 * - The loader implements vkCreate{PLATFORM}SurfaceKHR() and
5094 * vkDestroySurfaceKHR(). The ICD must be capable of working with
5095 * such loader-managed surfaces.
5096 *
5097 * - Loader interface v2 differs from v1 in:
5098 * - The first ICD entrypoint called by the loader is
5099 * vk_icdNegotiateLoaderICDInterfaceVersion(). The ICD must
5100 * statically expose this entrypoint.
5101 *
5102 * - Loader interface v3 differs from v2 in:
5103 * - The ICD must implement vkCreate{PLATFORM}SurfaceKHR(),
5104 * vkDestroySurfaceKHR(), and other API which uses VKSurfaceKHR,
5105 * because the loader no longer does so.
5106 */
5107 *pSupportedVersion = MIN2(*pSupportedVersion, 4u);
5108 return VK_SUCCESS;
5109 }
5110
5111 VkResult radv_GetMemoryFdKHR(VkDevice _device,
5112 const VkMemoryGetFdInfoKHR *pGetFdInfo,
5113 int *pFD)
5114 {
5115 RADV_FROM_HANDLE(radv_device, device, _device);
5116 RADV_FROM_HANDLE(radv_device_memory, memory, pGetFdInfo->memory);
5117
5118 assert(pGetFdInfo->sType == VK_STRUCTURE_TYPE_MEMORY_GET_FD_INFO_KHR);
5119
5120 /* At the moment, we support only the below handle types. */
5121 assert(pGetFdInfo->handleType ==
5122 VK_EXTERNAL_MEMORY_HANDLE_TYPE_OPAQUE_FD_BIT ||
5123 pGetFdInfo->handleType ==
5124 VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_EXT);
5125
5126 bool ret = radv_get_memory_fd(device, memory, pFD);
5127 if (ret == false)
5128 return vk_error(device->instance, VK_ERROR_OUT_OF_DEVICE_MEMORY);
5129 return VK_SUCCESS;
5130 }
5131
5132 VkResult radv_GetMemoryFdPropertiesKHR(VkDevice _device,
5133 VkExternalMemoryHandleTypeFlagBits handleType,
5134 int fd,
5135 VkMemoryFdPropertiesKHR *pMemoryFdProperties)
5136 {
5137 RADV_FROM_HANDLE(radv_device, device, _device);
5138
5139 switch (handleType) {
5140 case VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_EXT:
5141 pMemoryFdProperties->memoryTypeBits = (1 << RADV_MEM_TYPE_COUNT) - 1;
5142 return VK_SUCCESS;
5143
5144 default:
5145 /* The valid usage section for this function says:
5146 *
5147 * "handleType must not be one of the handle types defined as
5148 * opaque."
5149 *
5150 * So opaque handle types fall into the default "unsupported" case.
5151 */
5152 return vk_error(device->instance, VK_ERROR_INVALID_EXTERNAL_HANDLE);
5153 }
5154 }
5155
5156 static VkResult radv_import_opaque_fd(struct radv_device *device,
5157 int fd,
5158 uint32_t *syncobj)
5159 {
5160 uint32_t syncobj_handle = 0;
5161 int ret = device->ws->import_syncobj(device->ws, fd, &syncobj_handle);
5162 if (ret != 0)
5163 return vk_error(device->instance, VK_ERROR_INVALID_EXTERNAL_HANDLE);
5164
5165 if (*syncobj)
5166 device->ws->destroy_syncobj(device->ws, *syncobj);
5167
5168 *syncobj = syncobj_handle;
5169 close(fd);
5170
5171 return VK_SUCCESS;
5172 }
5173
5174 static VkResult radv_import_sync_fd(struct radv_device *device,
5175 int fd,
5176 uint32_t *syncobj)
5177 {
5178 /* If we create a syncobj we do it locally so that if we have an error, we don't
5179 * leave a syncobj in an undetermined state in the fence. */
5180 uint32_t syncobj_handle = *syncobj;
5181 if (!syncobj_handle) {
5182 int ret = device->ws->create_syncobj(device->ws, &syncobj_handle);
5183 if (ret) {
5184 return vk_error(device->instance, VK_ERROR_INVALID_EXTERNAL_HANDLE);
5185 }
5186 }
5187
5188 if (fd == -1) {
5189 device->ws->signal_syncobj(device->ws, syncobj_handle);
5190 } else {
5191 int ret = device->ws->import_syncobj_from_sync_file(device->ws, syncobj_handle, fd);
5192 if (ret != 0)
5193 return vk_error(device->instance, VK_ERROR_INVALID_EXTERNAL_HANDLE);
5194 }
5195
5196 *syncobj = syncobj_handle;
5197 if (fd != -1)
5198 close(fd);
5199
5200 return VK_SUCCESS;
5201 }
5202
5203 VkResult radv_ImportSemaphoreFdKHR(VkDevice _device,
5204 const VkImportSemaphoreFdInfoKHR *pImportSemaphoreFdInfo)
5205 {
5206 RADV_FROM_HANDLE(radv_device, device, _device);
5207 RADV_FROM_HANDLE(radv_semaphore, sem, pImportSemaphoreFdInfo->semaphore);
5208 uint32_t *syncobj_dst = NULL;
5209
5210 if (pImportSemaphoreFdInfo->flags & VK_SEMAPHORE_IMPORT_TEMPORARY_BIT) {
5211 syncobj_dst = &sem->temp_syncobj;
5212 } else {
5213 syncobj_dst = &sem->syncobj;
5214 }
5215
5216 switch(pImportSemaphoreFdInfo->handleType) {
5217 case VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_OPAQUE_FD_BIT:
5218 return radv_import_opaque_fd(device, pImportSemaphoreFdInfo->fd, syncobj_dst);
5219 case VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_SYNC_FD_BIT:
5220 return radv_import_sync_fd(device, pImportSemaphoreFdInfo->fd, syncobj_dst);
5221 default:
5222 unreachable("Unhandled semaphore handle type");
5223 }
5224 }
5225
5226 VkResult radv_GetSemaphoreFdKHR(VkDevice _device,
5227 const VkSemaphoreGetFdInfoKHR *pGetFdInfo,
5228 int *pFd)
5229 {
5230 RADV_FROM_HANDLE(radv_device, device, _device);
5231 RADV_FROM_HANDLE(radv_semaphore, sem, pGetFdInfo->semaphore);
5232 int ret;
5233 uint32_t syncobj_handle;
5234
5235 if (sem->temp_syncobj)
5236 syncobj_handle = sem->temp_syncobj;
5237 else
5238 syncobj_handle = sem->syncobj;
5239
5240 switch(pGetFdInfo->handleType) {
5241 case VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_OPAQUE_FD_BIT:
5242 ret = device->ws->export_syncobj(device->ws, syncobj_handle, pFd);
5243 break;
5244 case VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_SYNC_FD_BIT:
5245 ret = device->ws->export_syncobj_to_sync_file(device->ws, syncobj_handle, pFd);
5246 if (!ret) {
5247 if (sem->temp_syncobj) {
5248 close (sem->temp_syncobj);
5249 sem->temp_syncobj = 0;
5250 } else {
5251 device->ws->reset_syncobj(device->ws, syncobj_handle);
5252 }
5253 }
5254 break;
5255 default:
5256 unreachable("Unhandled semaphore handle type");
5257 }
5258
5259 if (ret)
5260 return vk_error(device->instance, VK_ERROR_INVALID_EXTERNAL_HANDLE);
5261 return VK_SUCCESS;
5262 }
5263
5264 void radv_GetPhysicalDeviceExternalSemaphoreProperties(
5265 VkPhysicalDevice physicalDevice,
5266 const VkPhysicalDeviceExternalSemaphoreInfo *pExternalSemaphoreInfo,
5267 VkExternalSemaphoreProperties *pExternalSemaphoreProperties)
5268 {
5269 RADV_FROM_HANDLE(radv_physical_device, pdevice, physicalDevice);
5270
5271 /* Require has_syncobj_wait_for_submit for the syncobj signal ioctl introduced at virtually the same time */
5272 if (pdevice->rad_info.has_syncobj_wait_for_submit &&
5273 (pExternalSemaphoreInfo->handleType == VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_OPAQUE_FD_BIT ||
5274 pExternalSemaphoreInfo->handleType == VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_SYNC_FD_BIT)) {
5275 pExternalSemaphoreProperties->exportFromImportedHandleTypes = VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_OPAQUE_FD_BIT | VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_SYNC_FD_BIT;
5276 pExternalSemaphoreProperties->compatibleHandleTypes = VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_OPAQUE_FD_BIT | VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_SYNC_FD_BIT;
5277 pExternalSemaphoreProperties->externalSemaphoreFeatures = VK_EXTERNAL_SEMAPHORE_FEATURE_EXPORTABLE_BIT |
5278 VK_EXTERNAL_SEMAPHORE_FEATURE_IMPORTABLE_BIT;
5279 } else if (pExternalSemaphoreInfo->handleType == VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_OPAQUE_FD_BIT) {
5280 pExternalSemaphoreProperties->exportFromImportedHandleTypes = VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_OPAQUE_FD_BIT;
5281 pExternalSemaphoreProperties->compatibleHandleTypes = VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_OPAQUE_FD_BIT;
5282 pExternalSemaphoreProperties->externalSemaphoreFeatures = VK_EXTERNAL_SEMAPHORE_FEATURE_EXPORTABLE_BIT |
5283 VK_EXTERNAL_SEMAPHORE_FEATURE_IMPORTABLE_BIT;
5284 } else {
5285 pExternalSemaphoreProperties->exportFromImportedHandleTypes = 0;
5286 pExternalSemaphoreProperties->compatibleHandleTypes = 0;
5287 pExternalSemaphoreProperties->externalSemaphoreFeatures = 0;
5288 }
5289 }
5290
5291 VkResult radv_ImportFenceFdKHR(VkDevice _device,
5292 const VkImportFenceFdInfoKHR *pImportFenceFdInfo)
5293 {
5294 RADV_FROM_HANDLE(radv_device, device, _device);
5295 RADV_FROM_HANDLE(radv_fence, fence, pImportFenceFdInfo->fence);
5296 uint32_t *syncobj_dst = NULL;
5297
5298
5299 if (pImportFenceFdInfo->flags & VK_FENCE_IMPORT_TEMPORARY_BIT) {
5300 syncobj_dst = &fence->temp_syncobj;
5301 } else {
5302 syncobj_dst = &fence->syncobj;
5303 }
5304
5305 switch(pImportFenceFdInfo->handleType) {
5306 case VK_EXTERNAL_FENCE_HANDLE_TYPE_OPAQUE_FD_BIT:
5307 return radv_import_opaque_fd(device, pImportFenceFdInfo->fd, syncobj_dst);
5308 case VK_EXTERNAL_FENCE_HANDLE_TYPE_SYNC_FD_BIT:
5309 return radv_import_sync_fd(device, pImportFenceFdInfo->fd, syncobj_dst);
5310 default:
5311 unreachable("Unhandled fence handle type");
5312 }
5313 }
5314
5315 VkResult radv_GetFenceFdKHR(VkDevice _device,
5316 const VkFenceGetFdInfoKHR *pGetFdInfo,
5317 int *pFd)
5318 {
5319 RADV_FROM_HANDLE(radv_device, device, _device);
5320 RADV_FROM_HANDLE(radv_fence, fence, pGetFdInfo->fence);
5321 int ret;
5322 uint32_t syncobj_handle;
5323
5324 if (fence->temp_syncobj)
5325 syncobj_handle = fence->temp_syncobj;
5326 else
5327 syncobj_handle = fence->syncobj;
5328
5329 switch(pGetFdInfo->handleType) {
5330 case VK_EXTERNAL_FENCE_HANDLE_TYPE_OPAQUE_FD_BIT:
5331 ret = device->ws->export_syncobj(device->ws, syncobj_handle, pFd);
5332 break;
5333 case VK_EXTERNAL_FENCE_HANDLE_TYPE_SYNC_FD_BIT:
5334 ret = device->ws->export_syncobj_to_sync_file(device->ws, syncobj_handle, pFd);
5335 if (!ret) {
5336 if (fence->temp_syncobj) {
5337 close (fence->temp_syncobj);
5338 fence->temp_syncobj = 0;
5339 } else {
5340 device->ws->reset_syncobj(device->ws, syncobj_handle);
5341 }
5342 }
5343 break;
5344 default:
5345 unreachable("Unhandled fence handle type");
5346 }
5347
5348 if (ret)
5349 return vk_error(device->instance, VK_ERROR_INVALID_EXTERNAL_HANDLE);
5350 return VK_SUCCESS;
5351 }
5352
5353 void radv_GetPhysicalDeviceExternalFenceProperties(
5354 VkPhysicalDevice physicalDevice,
5355 const VkPhysicalDeviceExternalFenceInfo *pExternalFenceInfo,
5356 VkExternalFenceProperties *pExternalFenceProperties)
5357 {
5358 RADV_FROM_HANDLE(radv_physical_device, pdevice, physicalDevice);
5359
5360 if (pdevice->rad_info.has_syncobj_wait_for_submit &&
5361 (pExternalFenceInfo->handleType == VK_EXTERNAL_FENCE_HANDLE_TYPE_OPAQUE_FD_BIT ||
5362 pExternalFenceInfo->handleType == VK_EXTERNAL_FENCE_HANDLE_TYPE_SYNC_FD_BIT)) {
5363 pExternalFenceProperties->exportFromImportedHandleTypes = VK_EXTERNAL_FENCE_HANDLE_TYPE_OPAQUE_FD_BIT | VK_EXTERNAL_FENCE_HANDLE_TYPE_SYNC_FD_BIT;
5364 pExternalFenceProperties->compatibleHandleTypes = VK_EXTERNAL_FENCE_HANDLE_TYPE_OPAQUE_FD_BIT | VK_EXTERNAL_FENCE_HANDLE_TYPE_SYNC_FD_BIT;
5365 pExternalFenceProperties->externalFenceFeatures = VK_EXTERNAL_FENCE_FEATURE_EXPORTABLE_BIT |
5366 VK_EXTERNAL_SEMAPHORE_FEATURE_IMPORTABLE_BIT;
5367 } else {
5368 pExternalFenceProperties->exportFromImportedHandleTypes = 0;
5369 pExternalFenceProperties->compatibleHandleTypes = 0;
5370 pExternalFenceProperties->externalFenceFeatures = 0;
5371 }
5372 }
5373
5374 VkResult
5375 radv_CreateDebugReportCallbackEXT(VkInstance _instance,
5376 const VkDebugReportCallbackCreateInfoEXT* pCreateInfo,
5377 const VkAllocationCallbacks* pAllocator,
5378 VkDebugReportCallbackEXT* pCallback)
5379 {
5380 RADV_FROM_HANDLE(radv_instance, instance, _instance);
5381 return vk_create_debug_report_callback(&instance->debug_report_callbacks,
5382 pCreateInfo, pAllocator, &instance->alloc,
5383 pCallback);
5384 }
5385
5386 void
5387 radv_DestroyDebugReportCallbackEXT(VkInstance _instance,
5388 VkDebugReportCallbackEXT _callback,
5389 const VkAllocationCallbacks* pAllocator)
5390 {
5391 RADV_FROM_HANDLE(radv_instance, instance, _instance);
5392 vk_destroy_debug_report_callback(&instance->debug_report_callbacks,
5393 _callback, pAllocator, &instance->alloc);
5394 }
5395
5396 void
5397 radv_DebugReportMessageEXT(VkInstance _instance,
5398 VkDebugReportFlagsEXT flags,
5399 VkDebugReportObjectTypeEXT objectType,
5400 uint64_t object,
5401 size_t location,
5402 int32_t messageCode,
5403 const char* pLayerPrefix,
5404 const char* pMessage)
5405 {
5406 RADV_FROM_HANDLE(radv_instance, instance, _instance);
5407 vk_debug_report(&instance->debug_report_callbacks, flags, objectType,
5408 object, location, messageCode, pLayerPrefix, pMessage);
5409 }
5410
5411 void
5412 radv_GetDeviceGroupPeerMemoryFeatures(
5413 VkDevice device,
5414 uint32_t heapIndex,
5415 uint32_t localDeviceIndex,
5416 uint32_t remoteDeviceIndex,
5417 VkPeerMemoryFeatureFlags* pPeerMemoryFeatures)
5418 {
5419 assert(localDeviceIndex == remoteDeviceIndex);
5420
5421 *pPeerMemoryFeatures = VK_PEER_MEMORY_FEATURE_COPY_SRC_BIT |
5422 VK_PEER_MEMORY_FEATURE_COPY_DST_BIT |
5423 VK_PEER_MEMORY_FEATURE_GENERIC_SRC_BIT |
5424 VK_PEER_MEMORY_FEATURE_GENERIC_DST_BIT;
5425 }
5426
5427 static const VkTimeDomainEXT radv_time_domains[] = {
5428 VK_TIME_DOMAIN_DEVICE_EXT,
5429 VK_TIME_DOMAIN_CLOCK_MONOTONIC_EXT,
5430 VK_TIME_DOMAIN_CLOCK_MONOTONIC_RAW_EXT,
5431 };
5432
5433 VkResult radv_GetPhysicalDeviceCalibrateableTimeDomainsEXT(
5434 VkPhysicalDevice physicalDevice,
5435 uint32_t *pTimeDomainCount,
5436 VkTimeDomainEXT *pTimeDomains)
5437 {
5438 int d;
5439 VK_OUTARRAY_MAKE(out, pTimeDomains, pTimeDomainCount);
5440
5441 for (d = 0; d < ARRAY_SIZE(radv_time_domains); d++) {
5442 vk_outarray_append(&out, i) {
5443 *i = radv_time_domains[d];
5444 }
5445 }
5446
5447 return vk_outarray_status(&out);
5448 }
5449
5450 static uint64_t
5451 radv_clock_gettime(clockid_t clock_id)
5452 {
5453 struct timespec current;
5454 int ret;
5455
5456 ret = clock_gettime(clock_id, &current);
5457 if (ret < 0 && clock_id == CLOCK_MONOTONIC_RAW)
5458 ret = clock_gettime(CLOCK_MONOTONIC, &current);
5459 if (ret < 0)
5460 return 0;
5461
5462 return (uint64_t) current.tv_sec * 1000000000ULL + current.tv_nsec;
5463 }
5464
5465 VkResult radv_GetCalibratedTimestampsEXT(
5466 VkDevice _device,
5467 uint32_t timestampCount,
5468 const VkCalibratedTimestampInfoEXT *pTimestampInfos,
5469 uint64_t *pTimestamps,
5470 uint64_t *pMaxDeviation)
5471 {
5472 RADV_FROM_HANDLE(radv_device, device, _device);
5473 uint32_t clock_crystal_freq = device->physical_device->rad_info.clock_crystal_freq;
5474 int d;
5475 uint64_t begin, end;
5476 uint64_t max_clock_period = 0;
5477
5478 begin = radv_clock_gettime(CLOCK_MONOTONIC_RAW);
5479
5480 for (d = 0; d < timestampCount; d++) {
5481 switch (pTimestampInfos[d].timeDomain) {
5482 case VK_TIME_DOMAIN_DEVICE_EXT:
5483 pTimestamps[d] = device->ws->query_value(device->ws,
5484 RADEON_TIMESTAMP);
5485 uint64_t device_period = DIV_ROUND_UP(1000000, clock_crystal_freq);
5486 max_clock_period = MAX2(max_clock_period, device_period);
5487 break;
5488 case VK_TIME_DOMAIN_CLOCK_MONOTONIC_EXT:
5489 pTimestamps[d] = radv_clock_gettime(CLOCK_MONOTONIC);
5490 max_clock_period = MAX2(max_clock_period, 1);
5491 break;
5492
5493 case VK_TIME_DOMAIN_CLOCK_MONOTONIC_RAW_EXT:
5494 pTimestamps[d] = begin;
5495 break;
5496 default:
5497 pTimestamps[d] = 0;
5498 break;
5499 }
5500 }
5501
5502 end = radv_clock_gettime(CLOCK_MONOTONIC_RAW);
5503
5504 /*
5505 * The maximum deviation is the sum of the interval over which we
5506 * perform the sampling and the maximum period of any sampled
5507 * clock. That's because the maximum skew between any two sampled
5508 * clock edges is when the sampled clock with the largest period is
5509 * sampled at the end of that period but right at the beginning of the
5510 * sampling interval and some other clock is sampled right at the
5511 * begining of its sampling period and right at the end of the
5512 * sampling interval. Let's assume the GPU has the longest clock
5513 * period and that the application is sampling GPU and monotonic:
5514 *
5515 * s e
5516 * w x y z 0 1 2 3 4 5 6 7 8 9 a b c d e f
5517 * Raw -_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-
5518 *
5519 * g
5520 * 0 1 2 3
5521 * GPU -----_____-----_____-----_____-----_____
5522 *
5523 * m
5524 * x y z 0 1 2 3 4 5 6 7 8 9 a b c
5525 * Monotonic -_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-
5526 *
5527 * Interval <----------------->
5528 * Deviation <-------------------------->
5529 *
5530 * s = read(raw) 2
5531 * g = read(GPU) 1
5532 * m = read(monotonic) 2
5533 * e = read(raw) b
5534 *
5535 * We round the sample interval up by one tick to cover sampling error
5536 * in the interval clock
5537 */
5538
5539 uint64_t sample_interval = end - begin + 1;
5540
5541 *pMaxDeviation = sample_interval + max_clock_period;
5542
5543 return VK_SUCCESS;
5544 }
5545
5546 void radv_GetPhysicalDeviceMultisamplePropertiesEXT(
5547 VkPhysicalDevice physicalDevice,
5548 VkSampleCountFlagBits samples,
5549 VkMultisamplePropertiesEXT* pMultisampleProperties)
5550 {
5551 if (samples & (VK_SAMPLE_COUNT_2_BIT |
5552 VK_SAMPLE_COUNT_4_BIT |
5553 VK_SAMPLE_COUNT_8_BIT)) {
5554 pMultisampleProperties->maxSampleLocationGridSize = (VkExtent2D){ 2, 2 };
5555 } else {
5556 pMultisampleProperties->maxSampleLocationGridSize = (VkExtent2D){ 0, 0 };
5557 }
5558 }