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