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