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