radv: Special case the initial preamble.
[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 <dlfcn.h>
29 #include <stdbool.h>
30 #include <string.h>
31 #include <unistd.h>
32 #include <fcntl.h>
33 #include <sys/stat.h>
34 #include "radv_private.h"
35 #include "radv_cs.h"
36 #include "util/strtod.h"
37
38 #include <xf86drm.h>
39 #include <amdgpu.h>
40 #include <amdgpu_drm.h>
41 #include "amdgpu_id.h"
42 #include "winsys/amdgpu/radv_amdgpu_winsys_public.h"
43 #include "ac_llvm_util.h"
44 #include "vk_format.h"
45 #include "sid.h"
46 #include "util/debug.h"
47 struct radv_dispatch_table dtable;
48
49 static int
50 radv_get_function_timestamp(void *ptr, uint32_t* timestamp)
51 {
52 Dl_info info;
53 struct stat st;
54 if (!dladdr(ptr, &info) || !info.dli_fname) {
55 return -1;
56 }
57 if (stat(info.dli_fname, &st)) {
58 return -1;
59 }
60 *timestamp = st.st_mtim.tv_sec;
61 return 0;
62 }
63
64 static int
65 radv_device_get_cache_uuid(enum radeon_family family, void *uuid)
66 {
67 uint32_t mesa_timestamp, llvm_timestamp;
68 uint16_t f = family;
69 memset(uuid, 0, VK_UUID_SIZE);
70 if (radv_get_function_timestamp(radv_device_get_cache_uuid, &mesa_timestamp) ||
71 radv_get_function_timestamp(LLVMInitializeAMDGPUTargetInfo, &llvm_timestamp))
72 return -1;
73
74 memcpy(uuid, &mesa_timestamp, 4);
75 memcpy((char*)uuid + 4, &llvm_timestamp, 4);
76 memcpy((char*)uuid + 8, &f, 2);
77 snprintf((char*)uuid + 10, VK_UUID_SIZE - 10, "radv");
78 return 0;
79 }
80
81 static const VkExtensionProperties instance_extensions[] = {
82 {
83 .extensionName = VK_KHR_SURFACE_EXTENSION_NAME,
84 .specVersion = 25,
85 },
86 #ifdef VK_USE_PLATFORM_XCB_KHR
87 {
88 .extensionName = VK_KHR_XCB_SURFACE_EXTENSION_NAME,
89 .specVersion = 6,
90 },
91 #endif
92 #ifdef VK_USE_PLATFORM_XLIB_KHR
93 {
94 .extensionName = VK_KHR_XLIB_SURFACE_EXTENSION_NAME,
95 .specVersion = 6,
96 },
97 #endif
98 #ifdef VK_USE_PLATFORM_WAYLAND_KHR
99 {
100 .extensionName = VK_KHR_WAYLAND_SURFACE_EXTENSION_NAME,
101 .specVersion = 5,
102 },
103 #endif
104 };
105
106 static const VkExtensionProperties common_device_extensions[] = {
107 {
108 .extensionName = VK_KHR_MAINTENANCE1_EXTENSION_NAME,
109 .specVersion = 1,
110 },
111 {
112 .extensionName = VK_KHR_SAMPLER_MIRROR_CLAMP_TO_EDGE_EXTENSION_NAME,
113 .specVersion = 1,
114 },
115 {
116 .extensionName = VK_KHR_SWAPCHAIN_EXTENSION_NAME,
117 .specVersion = 68,
118 },
119 {
120 .extensionName = VK_AMD_DRAW_INDIRECT_COUNT_EXTENSION_NAME,
121 .specVersion = 1,
122 },
123 {
124 .extensionName = VK_KHR_GET_PHYSICAL_DEVICE_PROPERTIES_2_EXTENSION_NAME,
125 .specVersion = 1,
126 },
127 {
128 .extensionName = VK_KHR_SHADER_DRAW_PARAMETERS_EXTENSION_NAME,
129 .specVersion = 1,
130 },
131 };
132
133 static VkResult
134 radv_extensions_register(struct radv_instance *instance,
135 struct radv_extensions *extensions,
136 const VkExtensionProperties *new_ext,
137 uint32_t num_ext)
138 {
139 size_t new_size;
140 VkExtensionProperties *new_ptr;
141
142 assert(new_ext && num_ext > 0);
143
144 if (!new_ext)
145 return VK_ERROR_INITIALIZATION_FAILED;
146
147 new_size = (extensions->num_ext + num_ext) * sizeof(VkExtensionProperties);
148 new_ptr = vk_realloc(&instance->alloc, extensions->ext_array,
149 new_size, 8, VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
150
151 /* Old array continues to be valid, update nothing */
152 if (!new_ptr)
153 return VK_ERROR_OUT_OF_HOST_MEMORY;
154
155 memcpy(&new_ptr[extensions->num_ext], new_ext,
156 num_ext * sizeof(VkExtensionProperties));
157 extensions->ext_array = new_ptr;
158 extensions->num_ext += num_ext;
159
160 return VK_SUCCESS;
161 }
162
163 static void
164 radv_extensions_finish(struct radv_instance *instance,
165 struct radv_extensions *extensions)
166 {
167 assert(extensions);
168
169 if (!extensions)
170 radv_loge("Attemted to free invalid extension struct\n");
171
172 if (extensions->ext_array)
173 vk_free(&instance->alloc, extensions->ext_array);
174 }
175
176 static bool
177 is_extension_enabled(const VkExtensionProperties *extensions,
178 size_t num_ext,
179 const char *name)
180 {
181 assert(extensions && name);
182
183 for (uint32_t i = 0; i < num_ext; i++) {
184 if (strcmp(name, extensions[i].extensionName) == 0)
185 return true;
186 }
187
188 return false;
189 }
190
191 static VkResult
192 radv_physical_device_init(struct radv_physical_device *device,
193 struct radv_instance *instance,
194 const char *path)
195 {
196 VkResult result;
197 drmVersionPtr version;
198 int fd;
199
200 fd = open(path, O_RDWR | O_CLOEXEC);
201 if (fd < 0)
202 return VK_ERROR_INCOMPATIBLE_DRIVER;
203
204 version = drmGetVersion(fd);
205 if (!version) {
206 close(fd);
207 return vk_errorf(VK_ERROR_INCOMPATIBLE_DRIVER,
208 "failed to get version %s: %m", path);
209 }
210
211 if (strcmp(version->name, "amdgpu")) {
212 drmFreeVersion(version);
213 close(fd);
214 return VK_ERROR_INCOMPATIBLE_DRIVER;
215 }
216 drmFreeVersion(version);
217
218 device->_loader_data.loaderMagic = ICD_LOADER_MAGIC;
219 device->instance = instance;
220 assert(strlen(path) < ARRAY_SIZE(device->path));
221 strncpy(device->path, path, ARRAY_SIZE(device->path));
222
223 device->ws = radv_amdgpu_winsys_create(fd);
224 if (!device->ws) {
225 result = VK_ERROR_INCOMPATIBLE_DRIVER;
226 goto fail;
227 }
228
229 device->local_fd = fd;
230 device->ws->query_info(device->ws, &device->rad_info);
231 result = radv_init_wsi(device);
232 if (result != VK_SUCCESS) {
233 device->ws->destroy(device->ws);
234 goto fail;
235 }
236
237 if (radv_device_get_cache_uuid(device->rad_info.family, device->uuid)) {
238 radv_finish_wsi(device);
239 device->ws->destroy(device->ws);
240 result = vk_errorf(VK_ERROR_INITIALIZATION_FAILED,
241 "cannot generate UUID");
242 goto fail;
243 }
244
245 result = radv_extensions_register(instance,
246 &device->extensions,
247 common_device_extensions,
248 ARRAY_SIZE(common_device_extensions));
249 if (result != VK_SUCCESS)
250 goto fail;
251
252 fprintf(stderr, "WARNING: radv is not a conformant vulkan implementation, testing use only.\n");
253 device->name = device->rad_info.name;
254
255 return VK_SUCCESS;
256
257 fail:
258 close(fd);
259 return result;
260 }
261
262 static void
263 radv_physical_device_finish(struct radv_physical_device *device)
264 {
265 radv_extensions_finish(device->instance, &device->extensions);
266 radv_finish_wsi(device);
267 device->ws->destroy(device->ws);
268 close(device->local_fd);
269 }
270
271
272 static void *
273 default_alloc_func(void *pUserData, size_t size, size_t align,
274 VkSystemAllocationScope allocationScope)
275 {
276 return malloc(size);
277 }
278
279 static void *
280 default_realloc_func(void *pUserData, void *pOriginal, size_t size,
281 size_t align, VkSystemAllocationScope allocationScope)
282 {
283 return realloc(pOriginal, size);
284 }
285
286 static void
287 default_free_func(void *pUserData, void *pMemory)
288 {
289 free(pMemory);
290 }
291
292 static const VkAllocationCallbacks default_alloc = {
293 .pUserData = NULL,
294 .pfnAllocation = default_alloc_func,
295 .pfnReallocation = default_realloc_func,
296 .pfnFree = default_free_func,
297 };
298
299 static const struct debug_control radv_debug_options[] = {
300 {"nofastclears", RADV_DEBUG_NO_FAST_CLEARS},
301 {"nodcc", RADV_DEBUG_NO_DCC},
302 {"shaders", RADV_DEBUG_DUMP_SHADERS},
303 {"nocache", RADV_DEBUG_NO_CACHE},
304 {"shaderstats", RADV_DEBUG_DUMP_SHADER_STATS},
305 {"nohiz", RADV_DEBUG_NO_HIZ},
306 {"nocompute", RADV_DEBUG_NO_COMPUTE_QUEUE},
307 {"unsafemath", RADV_DEBUG_UNSAFE_MATH},
308 {NULL, 0}
309 };
310
311 VkResult radv_CreateInstance(
312 const VkInstanceCreateInfo* pCreateInfo,
313 const VkAllocationCallbacks* pAllocator,
314 VkInstance* pInstance)
315 {
316 struct radv_instance *instance;
317
318 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO);
319
320 uint32_t client_version;
321 if (pCreateInfo->pApplicationInfo &&
322 pCreateInfo->pApplicationInfo->apiVersion != 0) {
323 client_version = pCreateInfo->pApplicationInfo->apiVersion;
324 } else {
325 client_version = VK_MAKE_VERSION(1, 0, 0);
326 }
327
328 if (VK_MAKE_VERSION(1, 0, 0) > client_version ||
329 client_version > VK_MAKE_VERSION(1, 0, 0xfff)) {
330 return vk_errorf(VK_ERROR_INCOMPATIBLE_DRIVER,
331 "Client requested version %d.%d.%d",
332 VK_VERSION_MAJOR(client_version),
333 VK_VERSION_MINOR(client_version),
334 VK_VERSION_PATCH(client_version));
335 }
336
337 for (uint32_t i = 0; i < pCreateInfo->enabledExtensionCount; i++) {
338 if (!is_extension_enabled(instance_extensions,
339 ARRAY_SIZE(instance_extensions),
340 pCreateInfo->ppEnabledExtensionNames[i]))
341 return vk_error(VK_ERROR_EXTENSION_NOT_PRESENT);
342 }
343
344 instance = vk_alloc2(&default_alloc, pAllocator, sizeof(*instance), 8,
345 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
346 if (!instance)
347 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
348
349 memset(instance, 0, sizeof(*instance));
350
351 instance->_loader_data.loaderMagic = ICD_LOADER_MAGIC;
352
353 if (pAllocator)
354 instance->alloc = *pAllocator;
355 else
356 instance->alloc = default_alloc;
357
358 instance->apiVersion = client_version;
359 instance->physicalDeviceCount = -1;
360
361 _mesa_locale_init();
362
363 VG(VALGRIND_CREATE_MEMPOOL(instance, 0, false));
364
365 instance->debug_flags = parse_debug_string(getenv("RADV_DEBUG"),
366 radv_debug_options);
367
368 *pInstance = radv_instance_to_handle(instance);
369
370 return VK_SUCCESS;
371 }
372
373 void radv_DestroyInstance(
374 VkInstance _instance,
375 const VkAllocationCallbacks* pAllocator)
376 {
377 RADV_FROM_HANDLE(radv_instance, instance, _instance);
378
379 for (int i = 0; i < instance->physicalDeviceCount; ++i) {
380 radv_physical_device_finish(instance->physicalDevices + i);
381 }
382
383 VG(VALGRIND_DESTROY_MEMPOOL(instance));
384
385 _mesa_locale_fini();
386
387 vk_free(&instance->alloc, instance);
388 }
389
390 VkResult radv_EnumeratePhysicalDevices(
391 VkInstance _instance,
392 uint32_t* pPhysicalDeviceCount,
393 VkPhysicalDevice* pPhysicalDevices)
394 {
395 RADV_FROM_HANDLE(radv_instance, instance, _instance);
396 VkResult result;
397
398 if (instance->physicalDeviceCount < 0) {
399 char path[20];
400 instance->physicalDeviceCount = 0;
401 for (unsigned i = 0; i < RADV_MAX_DRM_DEVICES; i++) {
402 snprintf(path, sizeof(path), "/dev/dri/renderD%d", 128 + i);
403 result = radv_physical_device_init(instance->physicalDevices +
404 instance->physicalDeviceCount,
405 instance, path);
406 if (result == VK_SUCCESS)
407 ++instance->physicalDeviceCount;
408 else if (result != VK_ERROR_INCOMPATIBLE_DRIVER)
409 return result;
410 }
411 }
412
413 if (!pPhysicalDevices) {
414 *pPhysicalDeviceCount = instance->physicalDeviceCount;
415 } else {
416 *pPhysicalDeviceCount = MIN2(*pPhysicalDeviceCount, instance->physicalDeviceCount);
417 for (unsigned i = 0; i < *pPhysicalDeviceCount; ++i)
418 pPhysicalDevices[i] = radv_physical_device_to_handle(instance->physicalDevices + i);
419 }
420
421 return *pPhysicalDeviceCount < instance->physicalDeviceCount ? VK_INCOMPLETE
422 : VK_SUCCESS;
423 }
424
425 void radv_GetPhysicalDeviceFeatures(
426 VkPhysicalDevice physicalDevice,
427 VkPhysicalDeviceFeatures* pFeatures)
428 {
429 // RADV_FROM_HANDLE(radv_physical_device, pdevice, physicalDevice);
430
431 memset(pFeatures, 0, sizeof(*pFeatures));
432
433 *pFeatures = (VkPhysicalDeviceFeatures) {
434 .robustBufferAccess = true,
435 .fullDrawIndexUint32 = true,
436 .imageCubeArray = true,
437 .independentBlend = true,
438 .geometryShader = true,
439 .tessellationShader = false,
440 .sampleRateShading = false,
441 .dualSrcBlend = true,
442 .logicOp = true,
443 .multiDrawIndirect = true,
444 .drawIndirectFirstInstance = true,
445 .depthClamp = true,
446 .depthBiasClamp = true,
447 .fillModeNonSolid = true,
448 .depthBounds = true,
449 .wideLines = true,
450 .largePoints = true,
451 .alphaToOne = true,
452 .multiViewport = true,
453 .samplerAnisotropy = true,
454 .textureCompressionETC2 = false,
455 .textureCompressionASTC_LDR = false,
456 .textureCompressionBC = true,
457 .occlusionQueryPrecise = true,
458 .pipelineStatisticsQuery = false,
459 .vertexPipelineStoresAndAtomics = true,
460 .fragmentStoresAndAtomics = true,
461 .shaderTessellationAndGeometryPointSize = true,
462 .shaderImageGatherExtended = true,
463 .shaderStorageImageExtendedFormats = true,
464 .shaderStorageImageMultisample = false,
465 .shaderUniformBufferArrayDynamicIndexing = true,
466 .shaderSampledImageArrayDynamicIndexing = true,
467 .shaderStorageBufferArrayDynamicIndexing = true,
468 .shaderStorageImageArrayDynamicIndexing = true,
469 .shaderStorageImageReadWithoutFormat = true,
470 .shaderStorageImageWriteWithoutFormat = true,
471 .shaderClipDistance = true,
472 .shaderCullDistance = true,
473 .shaderFloat64 = true,
474 .shaderInt64 = false,
475 .shaderInt16 = false,
476 .alphaToOne = true,
477 .variableMultisampleRate = false,
478 .inheritedQueries = false,
479 };
480 }
481
482 void radv_GetPhysicalDeviceFeatures2KHR(
483 VkPhysicalDevice physicalDevice,
484 VkPhysicalDeviceFeatures2KHR *pFeatures)
485 {
486 return radv_GetPhysicalDeviceFeatures(physicalDevice, &pFeatures->features);
487 }
488
489 void radv_GetPhysicalDeviceProperties(
490 VkPhysicalDevice physicalDevice,
491 VkPhysicalDeviceProperties* pProperties)
492 {
493 RADV_FROM_HANDLE(radv_physical_device, pdevice, physicalDevice);
494 VkSampleCountFlags sample_counts = 0xf;
495 VkPhysicalDeviceLimits limits = {
496 .maxImageDimension1D = (1 << 14),
497 .maxImageDimension2D = (1 << 14),
498 .maxImageDimension3D = (1 << 11),
499 .maxImageDimensionCube = (1 << 14),
500 .maxImageArrayLayers = (1 << 11),
501 .maxTexelBufferElements = 128 * 1024 * 1024,
502 .maxUniformBufferRange = UINT32_MAX,
503 .maxStorageBufferRange = UINT32_MAX,
504 .maxPushConstantsSize = MAX_PUSH_CONSTANTS_SIZE,
505 .maxMemoryAllocationCount = UINT32_MAX,
506 .maxSamplerAllocationCount = 64 * 1024,
507 .bufferImageGranularity = 64, /* A cache line */
508 .sparseAddressSpaceSize = 0,
509 .maxBoundDescriptorSets = MAX_SETS,
510 .maxPerStageDescriptorSamplers = 64,
511 .maxPerStageDescriptorUniformBuffers = 64,
512 .maxPerStageDescriptorStorageBuffers = 64,
513 .maxPerStageDescriptorSampledImages = 64,
514 .maxPerStageDescriptorStorageImages = 64,
515 .maxPerStageDescriptorInputAttachments = 64,
516 .maxPerStageResources = 128,
517 .maxDescriptorSetSamplers = 256,
518 .maxDescriptorSetUniformBuffers = 256,
519 .maxDescriptorSetUniformBuffersDynamic = 256,
520 .maxDescriptorSetStorageBuffers = 256,
521 .maxDescriptorSetStorageBuffersDynamic = 256,
522 .maxDescriptorSetSampledImages = 256,
523 .maxDescriptorSetStorageImages = 256,
524 .maxDescriptorSetInputAttachments = 256,
525 .maxVertexInputAttributes = 32,
526 .maxVertexInputBindings = 32,
527 .maxVertexInputAttributeOffset = 2047,
528 .maxVertexInputBindingStride = 2048,
529 .maxVertexOutputComponents = 128,
530 .maxTessellationGenerationLevel = 0,
531 .maxTessellationPatchSize = 0,
532 .maxTessellationControlPerVertexInputComponents = 0,
533 .maxTessellationControlPerVertexOutputComponents = 0,
534 .maxTessellationControlPerPatchOutputComponents = 0,
535 .maxTessellationControlTotalOutputComponents = 0,
536 .maxTessellationEvaluationInputComponents = 0,
537 .maxTessellationEvaluationOutputComponents = 0,
538 .maxGeometryShaderInvocations = 32,
539 .maxGeometryInputComponents = 64,
540 .maxGeometryOutputComponents = 128,
541 .maxGeometryOutputVertices = 256,
542 .maxGeometryTotalOutputComponents = 1024,
543 .maxFragmentInputComponents = 128,
544 .maxFragmentOutputAttachments = 8,
545 .maxFragmentDualSrcAttachments = 1,
546 .maxFragmentCombinedOutputResources = 8,
547 .maxComputeSharedMemorySize = 32768,
548 .maxComputeWorkGroupCount = { 65535, 65535, 65535 },
549 .maxComputeWorkGroupInvocations = 2048,
550 .maxComputeWorkGroupSize = {
551 2048,
552 2048,
553 2048
554 },
555 .subPixelPrecisionBits = 4 /* FIXME */,
556 .subTexelPrecisionBits = 4 /* FIXME */,
557 .mipmapPrecisionBits = 4 /* FIXME */,
558 .maxDrawIndexedIndexValue = UINT32_MAX,
559 .maxDrawIndirectCount = UINT32_MAX,
560 .maxSamplerLodBias = 16,
561 .maxSamplerAnisotropy = 16,
562 .maxViewports = MAX_VIEWPORTS,
563 .maxViewportDimensions = { (1 << 14), (1 << 14) },
564 .viewportBoundsRange = { INT16_MIN, INT16_MAX },
565 .viewportSubPixelBits = 13, /* We take a float? */
566 .minMemoryMapAlignment = 4096, /* A page */
567 .minTexelBufferOffsetAlignment = 1,
568 .minUniformBufferOffsetAlignment = 4,
569 .minStorageBufferOffsetAlignment = 4,
570 .minTexelOffset = -32,
571 .maxTexelOffset = 31,
572 .minTexelGatherOffset = -32,
573 .maxTexelGatherOffset = 31,
574 .minInterpolationOffset = -2,
575 .maxInterpolationOffset = 2,
576 .subPixelInterpolationOffsetBits = 8,
577 .maxFramebufferWidth = (1 << 14),
578 .maxFramebufferHeight = (1 << 14),
579 .maxFramebufferLayers = (1 << 10),
580 .framebufferColorSampleCounts = sample_counts,
581 .framebufferDepthSampleCounts = sample_counts,
582 .framebufferStencilSampleCounts = sample_counts,
583 .framebufferNoAttachmentsSampleCounts = sample_counts,
584 .maxColorAttachments = MAX_RTS,
585 .sampledImageColorSampleCounts = sample_counts,
586 .sampledImageIntegerSampleCounts = VK_SAMPLE_COUNT_1_BIT,
587 .sampledImageDepthSampleCounts = sample_counts,
588 .sampledImageStencilSampleCounts = sample_counts,
589 .storageImageSampleCounts = VK_SAMPLE_COUNT_1_BIT,
590 .maxSampleMaskWords = 1,
591 .timestampComputeAndGraphics = false,
592 .timestampPeriod = 100000.0 / pdevice->rad_info.clock_crystal_freq,
593 .maxClipDistances = 8,
594 .maxCullDistances = 8,
595 .maxCombinedClipAndCullDistances = 8,
596 .discreteQueuePriorities = 1,
597 .pointSizeRange = { 0.125, 255.875 },
598 .lineWidthRange = { 0.0, 7.9921875 },
599 .pointSizeGranularity = (1.0 / 8.0),
600 .lineWidthGranularity = (1.0 / 128.0),
601 .strictLines = false, /* FINISHME */
602 .standardSampleLocations = true,
603 .optimalBufferCopyOffsetAlignment = 128,
604 .optimalBufferCopyRowPitchAlignment = 128,
605 .nonCoherentAtomSize = 64,
606 };
607
608 *pProperties = (VkPhysicalDeviceProperties) {
609 .apiVersion = VK_MAKE_VERSION(1, 0, 5),
610 .driverVersion = 1,
611 .vendorID = 0x1002,
612 .deviceID = pdevice->rad_info.pci_id,
613 .deviceType = VK_PHYSICAL_DEVICE_TYPE_DISCRETE_GPU,
614 .limits = limits,
615 .sparseProperties = {0}, /* Broadwell doesn't do sparse. */
616 };
617
618 strcpy(pProperties->deviceName, pdevice->name);
619 memcpy(pProperties->pipelineCacheUUID, pdevice->uuid, VK_UUID_SIZE);
620 }
621
622 void radv_GetPhysicalDeviceProperties2KHR(
623 VkPhysicalDevice physicalDevice,
624 VkPhysicalDeviceProperties2KHR *pProperties)
625 {
626 return radv_GetPhysicalDeviceProperties(physicalDevice, &pProperties->properties);
627 }
628
629 static void radv_get_physical_device_queue_family_properties(
630 struct radv_physical_device* pdevice,
631 uint32_t* pCount,
632 VkQueueFamilyProperties** pQueueFamilyProperties)
633 {
634 int num_queue_families = 1;
635 int idx;
636 if (pdevice->rad_info.compute_rings > 0 &&
637 pdevice->rad_info.chip_class >= CIK &&
638 !(pdevice->instance->debug_flags & RADV_DEBUG_NO_COMPUTE_QUEUE))
639 num_queue_families++;
640
641 if (pQueueFamilyProperties == NULL) {
642 *pCount = num_queue_families;
643 return;
644 }
645
646 if (!*pCount)
647 return;
648
649 idx = 0;
650 if (*pCount >= 1) {
651 *pQueueFamilyProperties[idx] = (VkQueueFamilyProperties) {
652 .queueFlags = VK_QUEUE_GRAPHICS_BIT |
653 VK_QUEUE_COMPUTE_BIT |
654 VK_QUEUE_TRANSFER_BIT,
655 .queueCount = 1,
656 .timestampValidBits = 64,
657 .minImageTransferGranularity = (VkExtent3D) { 1, 1, 1 },
658 };
659 idx++;
660 }
661
662 if (pdevice->rad_info.compute_rings > 0 &&
663 pdevice->rad_info.chip_class >= CIK &&
664 !(pdevice->instance->debug_flags & RADV_DEBUG_NO_COMPUTE_QUEUE)) {
665 if (*pCount > idx) {
666 *pQueueFamilyProperties[idx] = (VkQueueFamilyProperties) {
667 .queueFlags = VK_QUEUE_COMPUTE_BIT | VK_QUEUE_TRANSFER_BIT,
668 .queueCount = pdevice->rad_info.compute_rings,
669 .timestampValidBits = 64,
670 .minImageTransferGranularity = (VkExtent3D) { 1, 1, 1 },
671 };
672 idx++;
673 }
674 }
675 *pCount = idx;
676 }
677
678 void radv_GetPhysicalDeviceQueueFamilyProperties(
679 VkPhysicalDevice physicalDevice,
680 uint32_t* pCount,
681 VkQueueFamilyProperties* pQueueFamilyProperties)
682 {
683 RADV_FROM_HANDLE(radv_physical_device, pdevice, physicalDevice);
684 if (!pQueueFamilyProperties) {
685 return radv_get_physical_device_queue_family_properties(pdevice, pCount, NULL);
686 return;
687 }
688 VkQueueFamilyProperties *properties[] = {
689 pQueueFamilyProperties + 0,
690 pQueueFamilyProperties + 1,
691 pQueueFamilyProperties + 2,
692 };
693 radv_get_physical_device_queue_family_properties(pdevice, pCount, properties);
694 assert(*pCount <= 3);
695 }
696
697 void radv_GetPhysicalDeviceQueueFamilyProperties2KHR(
698 VkPhysicalDevice physicalDevice,
699 uint32_t* pCount,
700 VkQueueFamilyProperties2KHR *pQueueFamilyProperties)
701 {
702 RADV_FROM_HANDLE(radv_physical_device, pdevice, physicalDevice);
703 if (!pQueueFamilyProperties) {
704 return radv_get_physical_device_queue_family_properties(pdevice, pCount, NULL);
705 return;
706 }
707 VkQueueFamilyProperties *properties[] = {
708 &pQueueFamilyProperties[0].queueFamilyProperties,
709 &pQueueFamilyProperties[1].queueFamilyProperties,
710 &pQueueFamilyProperties[2].queueFamilyProperties,
711 };
712 radv_get_physical_device_queue_family_properties(pdevice, pCount, properties);
713 assert(*pCount <= 3);
714 }
715
716 void radv_GetPhysicalDeviceMemoryProperties(
717 VkPhysicalDevice physicalDevice,
718 VkPhysicalDeviceMemoryProperties *pMemoryProperties)
719 {
720 RADV_FROM_HANDLE(radv_physical_device, physical_device, physicalDevice);
721
722 STATIC_ASSERT(RADV_MEM_TYPE_COUNT <= VK_MAX_MEMORY_TYPES);
723
724 pMemoryProperties->memoryTypeCount = RADV_MEM_TYPE_COUNT;
725 pMemoryProperties->memoryTypes[RADV_MEM_TYPE_VRAM] = (VkMemoryType) {
726 .propertyFlags = VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT,
727 .heapIndex = RADV_MEM_HEAP_VRAM,
728 };
729 pMemoryProperties->memoryTypes[RADV_MEM_TYPE_GTT_WRITE_COMBINE] = (VkMemoryType) {
730 .propertyFlags = VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT |
731 VK_MEMORY_PROPERTY_HOST_COHERENT_BIT,
732 .heapIndex = RADV_MEM_HEAP_GTT,
733 };
734 pMemoryProperties->memoryTypes[RADV_MEM_TYPE_VRAM_CPU_ACCESS] = (VkMemoryType) {
735 .propertyFlags = VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT |
736 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT |
737 VK_MEMORY_PROPERTY_HOST_COHERENT_BIT,
738 .heapIndex = RADV_MEM_HEAP_VRAM_CPU_ACCESS,
739 };
740 pMemoryProperties->memoryTypes[RADV_MEM_TYPE_GTT_CACHED] = (VkMemoryType) {
741 .propertyFlags = VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT |
742 VK_MEMORY_PROPERTY_HOST_COHERENT_BIT |
743 VK_MEMORY_PROPERTY_HOST_CACHED_BIT,
744 .heapIndex = RADV_MEM_HEAP_GTT,
745 };
746
747 STATIC_ASSERT(RADV_MEM_HEAP_COUNT <= VK_MAX_MEMORY_HEAPS);
748
749 pMemoryProperties->memoryHeapCount = RADV_MEM_HEAP_COUNT;
750 pMemoryProperties->memoryHeaps[RADV_MEM_HEAP_VRAM] = (VkMemoryHeap) {
751 .size = physical_device->rad_info.vram_size -
752 physical_device->rad_info.visible_vram_size,
753 .flags = VK_MEMORY_HEAP_DEVICE_LOCAL_BIT,
754 };
755 pMemoryProperties->memoryHeaps[RADV_MEM_HEAP_VRAM_CPU_ACCESS] = (VkMemoryHeap) {
756 .size = physical_device->rad_info.visible_vram_size,
757 .flags = VK_MEMORY_HEAP_DEVICE_LOCAL_BIT,
758 };
759 pMemoryProperties->memoryHeaps[RADV_MEM_HEAP_GTT] = (VkMemoryHeap) {
760 .size = physical_device->rad_info.gart_size,
761 .flags = 0,
762 };
763 }
764
765 void radv_GetPhysicalDeviceMemoryProperties2KHR(
766 VkPhysicalDevice physicalDevice,
767 VkPhysicalDeviceMemoryProperties2KHR *pMemoryProperties)
768 {
769 return radv_GetPhysicalDeviceMemoryProperties(physicalDevice,
770 &pMemoryProperties->memoryProperties);
771 }
772
773 static int
774 radv_queue_init(struct radv_device *device, struct radv_queue *queue,
775 int queue_family_index, int idx)
776 {
777 queue->_loader_data.loaderMagic = ICD_LOADER_MAGIC;
778 queue->device = device;
779 queue->queue_family_index = queue_family_index;
780 queue->queue_idx = idx;
781
782 queue->hw_ctx = device->ws->ctx_create(device->ws);
783 if (!queue->hw_ctx)
784 return VK_ERROR_OUT_OF_HOST_MEMORY;
785
786 return VK_SUCCESS;
787 }
788
789 static void
790 radv_queue_finish(struct radv_queue *queue)
791 {
792 if (queue->hw_ctx)
793 queue->device->ws->ctx_destroy(queue->hw_ctx);
794
795 if (queue->preamble_cs)
796 queue->device->ws->cs_destroy(queue->preamble_cs);
797 if (queue->descriptor_bo)
798 queue->device->ws->buffer_destroy(queue->descriptor_bo);
799 if (queue->scratch_bo)
800 queue->device->ws->buffer_destroy(queue->scratch_bo);
801 if (queue->esgs_ring_bo)
802 queue->device->ws->buffer_destroy(queue->esgs_ring_bo);
803 if (queue->gsvs_ring_bo)
804 queue->device->ws->buffer_destroy(queue->gsvs_ring_bo);
805 if (queue->compute_scratch_bo)
806 queue->device->ws->buffer_destroy(queue->compute_scratch_bo);
807 }
808
809 static void
810 radv_device_init_gs_info(struct radv_device *device)
811 {
812 switch (device->physical_device->rad_info.family) {
813 case CHIP_OLAND:
814 case CHIP_HAINAN:
815 case CHIP_KAVERI:
816 case CHIP_KABINI:
817 case CHIP_MULLINS:
818 case CHIP_ICELAND:
819 case CHIP_CARRIZO:
820 case CHIP_STONEY:
821 device->gs_table_depth = 16;
822 return;
823 case CHIP_TAHITI:
824 case CHIP_PITCAIRN:
825 case CHIP_VERDE:
826 case CHIP_BONAIRE:
827 case CHIP_HAWAII:
828 case CHIP_TONGA:
829 case CHIP_FIJI:
830 case CHIP_POLARIS10:
831 case CHIP_POLARIS11:
832 device->gs_table_depth = 32;
833 return;
834 default:
835 unreachable("unknown GPU");
836 }
837 }
838
839 VkResult radv_CreateDevice(
840 VkPhysicalDevice physicalDevice,
841 const VkDeviceCreateInfo* pCreateInfo,
842 const VkAllocationCallbacks* pAllocator,
843 VkDevice* pDevice)
844 {
845 RADV_FROM_HANDLE(radv_physical_device, physical_device, physicalDevice);
846 VkResult result;
847 struct radv_device *device;
848
849 for (uint32_t i = 0; i < pCreateInfo->enabledExtensionCount; i++) {
850 if (!is_extension_enabled(physical_device->extensions.ext_array,
851 physical_device->extensions.num_ext,
852 pCreateInfo->ppEnabledExtensionNames[i]))
853 return vk_error(VK_ERROR_EXTENSION_NOT_PRESENT);
854 }
855
856 device = vk_alloc2(&physical_device->instance->alloc, pAllocator,
857 sizeof(*device), 8,
858 VK_SYSTEM_ALLOCATION_SCOPE_DEVICE);
859 if (!device)
860 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
861
862 memset(device, 0, sizeof(*device));
863
864 device->_loader_data.loaderMagic = ICD_LOADER_MAGIC;
865 device->instance = physical_device->instance;
866 device->physical_device = physical_device;
867
868 device->debug_flags = device->instance->debug_flags;
869
870 device->ws = physical_device->ws;
871 if (pAllocator)
872 device->alloc = *pAllocator;
873 else
874 device->alloc = physical_device->instance->alloc;
875
876 for (unsigned i = 0; i < pCreateInfo->queueCreateInfoCount; i++) {
877 const VkDeviceQueueCreateInfo *queue_create = &pCreateInfo->pQueueCreateInfos[i];
878 uint32_t qfi = queue_create->queueFamilyIndex;
879
880 device->queues[qfi] = vk_alloc(&device->alloc,
881 queue_create->queueCount * sizeof(struct radv_queue), 8, VK_SYSTEM_ALLOCATION_SCOPE_DEVICE);
882 if (!device->queues[qfi]) {
883 result = VK_ERROR_OUT_OF_HOST_MEMORY;
884 goto fail;
885 }
886
887 memset(device->queues[qfi], 0, queue_create->queueCount * sizeof(struct radv_queue));
888
889 device->queue_count[qfi] = queue_create->queueCount;
890
891 for (unsigned q = 0; q < queue_create->queueCount; q++) {
892 result = radv_queue_init(device, &device->queues[qfi][q], qfi, q);
893 if (result != VK_SUCCESS)
894 goto fail;
895 }
896 }
897
898 #if HAVE_LLVM < 0x0400
899 device->llvm_supports_spill = false;
900 #else
901 device->llvm_supports_spill = true;
902 #endif
903
904 /* The maximum number of scratch waves. Scratch space isn't divided
905 * evenly between CUs. The number is only a function of the number of CUs.
906 * We can decrease the constant to decrease the scratch buffer size.
907 *
908 * sctx->scratch_waves must be >= the maximum posible size of
909 * 1 threadgroup, so that the hw doesn't hang from being unable
910 * to start any.
911 *
912 * The recommended value is 4 per CU at most. Higher numbers don't
913 * bring much benefit, but they still occupy chip resources (think
914 * async compute). I've seen ~2% performance difference between 4 and 32.
915 */
916 uint32_t max_threads_per_block = 2048;
917 device->scratch_waves = MAX2(32 * physical_device->rad_info.num_good_compute_units,
918 max_threads_per_block / 64);
919
920 radv_device_init_gs_info(device);
921
922 result = radv_device_init_meta(device);
923 if (result != VK_SUCCESS)
924 goto fail;
925
926 radv_device_init_msaa(device);
927
928 for (int family = 0; family < RADV_MAX_QUEUE_FAMILIES; ++family) {
929 device->empty_cs[family] = device->ws->cs_create(device->ws, family);
930 switch (family) {
931 case RADV_QUEUE_GENERAL:
932 radeon_emit(device->empty_cs[family], PKT3(PKT3_CONTEXT_CONTROL, 1, 0));
933 radeon_emit(device->empty_cs[family], CONTEXT_CONTROL_LOAD_ENABLE(1));
934 radeon_emit(device->empty_cs[family], CONTEXT_CONTROL_SHADOW_ENABLE(1));
935 break;
936 case RADV_QUEUE_COMPUTE:
937 radeon_emit(device->empty_cs[family], PKT3(PKT3_NOP, 0, 0));
938 radeon_emit(device->empty_cs[family], 0);
939 break;
940 }
941 device->ws->cs_finalize(device->empty_cs[family]);
942 }
943
944 if (getenv("RADV_TRACE_FILE")) {
945 device->trace_bo = device->ws->buffer_create(device->ws, 4096, 8,
946 RADEON_DOMAIN_VRAM, RADEON_FLAG_CPU_ACCESS);
947 if (!device->trace_bo)
948 goto fail;
949
950 device->trace_id_ptr = device->ws->buffer_map(device->trace_bo);
951 if (!device->trace_id_ptr)
952 goto fail;
953 }
954
955 if (device->physical_device->rad_info.chip_class >= CIK)
956 cik_create_gfx_config(device);
957
958 *pDevice = radv_device_to_handle(device);
959 return VK_SUCCESS;
960
961 fail:
962 if (device->trace_bo)
963 device->ws->buffer_destroy(device->trace_bo);
964
965 if (device->gfx_init)
966 device->ws->buffer_destroy(device->gfx_init);
967
968 for (unsigned i = 0; i < RADV_MAX_QUEUE_FAMILIES; i++) {
969 for (unsigned q = 0; q < device->queue_count[i]; q++)
970 radv_queue_finish(&device->queues[i][q]);
971 if (device->queue_count[i])
972 vk_free(&device->alloc, device->queues[i]);
973 }
974
975 vk_free(&device->alloc, device);
976 return result;
977 }
978
979 void radv_DestroyDevice(
980 VkDevice _device,
981 const VkAllocationCallbacks* pAllocator)
982 {
983 RADV_FROM_HANDLE(radv_device, device, _device);
984
985 if (device->trace_bo)
986 device->ws->buffer_destroy(device->trace_bo);
987
988 if (device->gfx_init)
989 device->ws->buffer_destroy(device->gfx_init);
990
991 for (unsigned i = 0; i < RADV_MAX_QUEUE_FAMILIES; i++) {
992 for (unsigned q = 0; q < device->queue_count[i]; q++)
993 radv_queue_finish(&device->queues[i][q]);
994 if (device->queue_count[i])
995 vk_free(&device->alloc, device->queues[i]);
996 if (device->empty_cs[i])
997 device->ws->cs_destroy(device->empty_cs[i]);
998 }
999 radv_device_finish_meta(device);
1000
1001 vk_free(&device->alloc, device);
1002 }
1003
1004 VkResult radv_EnumerateInstanceExtensionProperties(
1005 const char* pLayerName,
1006 uint32_t* pPropertyCount,
1007 VkExtensionProperties* pProperties)
1008 {
1009 if (pProperties == NULL) {
1010 *pPropertyCount = ARRAY_SIZE(instance_extensions);
1011 return VK_SUCCESS;
1012 }
1013
1014 *pPropertyCount = MIN2(*pPropertyCount, ARRAY_SIZE(instance_extensions));
1015 typed_memcpy(pProperties, instance_extensions, *pPropertyCount);
1016
1017 if (*pPropertyCount < ARRAY_SIZE(instance_extensions))
1018 return VK_INCOMPLETE;
1019
1020 return VK_SUCCESS;
1021 }
1022
1023 VkResult radv_EnumerateDeviceExtensionProperties(
1024 VkPhysicalDevice physicalDevice,
1025 const char* pLayerName,
1026 uint32_t* pPropertyCount,
1027 VkExtensionProperties* pProperties)
1028 {
1029 RADV_FROM_HANDLE(radv_physical_device, pdevice, physicalDevice);
1030
1031 if (pProperties == NULL) {
1032 *pPropertyCount = pdevice->extensions.num_ext;
1033 return VK_SUCCESS;
1034 }
1035
1036 *pPropertyCount = MIN2(*pPropertyCount, pdevice->extensions.num_ext);
1037 typed_memcpy(pProperties, pdevice->extensions.ext_array, *pPropertyCount);
1038
1039 if (*pPropertyCount < pdevice->extensions.num_ext)
1040 return VK_INCOMPLETE;
1041
1042 return VK_SUCCESS;
1043 }
1044
1045 VkResult radv_EnumerateInstanceLayerProperties(
1046 uint32_t* pPropertyCount,
1047 VkLayerProperties* pProperties)
1048 {
1049 if (pProperties == NULL) {
1050 *pPropertyCount = 0;
1051 return VK_SUCCESS;
1052 }
1053
1054 /* None supported at this time */
1055 return vk_error(VK_ERROR_LAYER_NOT_PRESENT);
1056 }
1057
1058 VkResult radv_EnumerateDeviceLayerProperties(
1059 VkPhysicalDevice physicalDevice,
1060 uint32_t* pPropertyCount,
1061 VkLayerProperties* pProperties)
1062 {
1063 if (pProperties == NULL) {
1064 *pPropertyCount = 0;
1065 return VK_SUCCESS;
1066 }
1067
1068 /* None supported at this time */
1069 return vk_error(VK_ERROR_LAYER_NOT_PRESENT);
1070 }
1071
1072 void radv_GetDeviceQueue(
1073 VkDevice _device,
1074 uint32_t queueFamilyIndex,
1075 uint32_t queueIndex,
1076 VkQueue* pQueue)
1077 {
1078 RADV_FROM_HANDLE(radv_device, device, _device);
1079
1080 *pQueue = radv_queue_to_handle(&device->queues[queueFamilyIndex][queueIndex]);
1081 }
1082
1083 static void radv_dump_trace(struct radv_device *device,
1084 struct radeon_winsys_cs *cs)
1085 {
1086 const char *filename = getenv("RADV_TRACE_FILE");
1087 FILE *f = fopen(filename, "w");
1088 if (!f) {
1089 fprintf(stderr, "Failed to write trace dump to %s\n", filename);
1090 return;
1091 }
1092
1093 fprintf(f, "Trace ID: %x\n", *device->trace_id_ptr);
1094 device->ws->cs_dump(cs, f, *device->trace_id_ptr);
1095 fclose(f);
1096 }
1097
1098 static void
1099 fill_geom_rings(struct radv_queue *queue,
1100 uint32_t *map,
1101 uint32_t esgs_ring_size,
1102 struct radeon_winsys_bo *esgs_ring_bo,
1103 uint32_t gsvs_ring_size,
1104 struct radeon_winsys_bo *gsvs_ring_bo)
1105 {
1106 uint64_t esgs_va = 0, gsvs_va = 0;
1107 uint32_t *desc = &map[4];
1108
1109 if (esgs_ring_bo)
1110 esgs_va = queue->device->ws->buffer_get_va(esgs_ring_bo);
1111 if (gsvs_ring_bo)
1112 gsvs_va = queue->device->ws->buffer_get_va(gsvs_ring_bo);
1113
1114 /* stride 0, num records - size, add tid, swizzle, elsize4,
1115 index stride 64 */
1116 desc[0] = esgs_va;
1117 desc[1] = S_008F04_BASE_ADDRESS_HI(esgs_va >> 32) |
1118 S_008F04_STRIDE(0) |
1119 S_008F04_SWIZZLE_ENABLE(true);
1120 desc[2] = esgs_ring_size;
1121 desc[3] = S_008F0C_DST_SEL_X(V_008F0C_SQ_SEL_X) |
1122 S_008F0C_DST_SEL_Y(V_008F0C_SQ_SEL_Y) |
1123 S_008F0C_DST_SEL_Z(V_008F0C_SQ_SEL_Z) |
1124 S_008F0C_DST_SEL_W(V_008F0C_SQ_SEL_W) |
1125 S_008F0C_NUM_FORMAT(V_008F0C_BUF_NUM_FORMAT_FLOAT) |
1126 S_008F0C_DATA_FORMAT(V_008F0C_BUF_DATA_FORMAT_32) |
1127 S_008F0C_ELEMENT_SIZE(1) |
1128 S_008F0C_INDEX_STRIDE(3) |
1129 S_008F0C_ADD_TID_ENABLE(true);
1130
1131 desc += 4;
1132 /* GS entry for ES->GS ring */
1133 /* stride 0, num records - size, elsize0,
1134 index stride 0 */
1135 desc[0] = esgs_va;
1136 desc[1] = S_008F04_BASE_ADDRESS_HI(esgs_va >> 32)|
1137 S_008F04_STRIDE(0) |
1138 S_008F04_SWIZZLE_ENABLE(false);
1139 desc[2] = esgs_ring_size;
1140 desc[3] = S_008F0C_DST_SEL_X(V_008F0C_SQ_SEL_X) |
1141 S_008F0C_DST_SEL_Y(V_008F0C_SQ_SEL_Y) |
1142 S_008F0C_DST_SEL_Z(V_008F0C_SQ_SEL_Z) |
1143 S_008F0C_DST_SEL_W(V_008F0C_SQ_SEL_W) |
1144 S_008F0C_NUM_FORMAT(V_008F0C_BUF_NUM_FORMAT_FLOAT) |
1145 S_008F0C_DATA_FORMAT(V_008F0C_BUF_DATA_FORMAT_32) |
1146 S_008F0C_ELEMENT_SIZE(0) |
1147 S_008F0C_INDEX_STRIDE(0) |
1148 S_008F0C_ADD_TID_ENABLE(false);
1149
1150 desc += 4;
1151 /* VS entry for GS->VS ring */
1152 /* stride 0, num records - size, elsize0,
1153 index stride 0 */
1154 desc[0] = gsvs_va;
1155 desc[1] = S_008F04_BASE_ADDRESS_HI(gsvs_va >> 32)|
1156 S_008F04_STRIDE(0) |
1157 S_008F04_SWIZZLE_ENABLE(false);
1158 desc[2] = gsvs_ring_size;
1159 desc[3] = S_008F0C_DST_SEL_X(V_008F0C_SQ_SEL_X) |
1160 S_008F0C_DST_SEL_Y(V_008F0C_SQ_SEL_Y) |
1161 S_008F0C_DST_SEL_Z(V_008F0C_SQ_SEL_Z) |
1162 S_008F0C_DST_SEL_W(V_008F0C_SQ_SEL_W) |
1163 S_008F0C_NUM_FORMAT(V_008F0C_BUF_NUM_FORMAT_FLOAT) |
1164 S_008F0C_DATA_FORMAT(V_008F0C_BUF_DATA_FORMAT_32) |
1165 S_008F0C_ELEMENT_SIZE(0) |
1166 S_008F0C_INDEX_STRIDE(0) |
1167 S_008F0C_ADD_TID_ENABLE(false);
1168 desc += 4;
1169
1170 /* stride gsvs_itemsize, num records 64
1171 elsize 4, index stride 16 */
1172 /* shader will patch stride and desc[2] */
1173 desc[0] = gsvs_va;
1174 desc[1] = S_008F04_BASE_ADDRESS_HI(gsvs_va >> 32)|
1175 S_008F04_STRIDE(0) |
1176 S_008F04_SWIZZLE_ENABLE(true);
1177 desc[2] = 0;
1178 desc[3] = S_008F0C_DST_SEL_X(V_008F0C_SQ_SEL_X) |
1179 S_008F0C_DST_SEL_Y(V_008F0C_SQ_SEL_Y) |
1180 S_008F0C_DST_SEL_Z(V_008F0C_SQ_SEL_Z) |
1181 S_008F0C_DST_SEL_W(V_008F0C_SQ_SEL_W) |
1182 S_008F0C_NUM_FORMAT(V_008F0C_BUF_NUM_FORMAT_FLOAT) |
1183 S_008F0C_DATA_FORMAT(V_008F0C_BUF_DATA_FORMAT_32) |
1184 S_008F0C_ELEMENT_SIZE(1) |
1185 S_008F0C_INDEX_STRIDE(1) |
1186 S_008F0C_ADD_TID_ENABLE(true);
1187 }
1188
1189 static VkResult
1190 radv_get_preamble_cs(struct radv_queue *queue,
1191 uint32_t scratch_size,
1192 uint32_t compute_scratch_size,
1193 uint32_t esgs_ring_size,
1194 uint32_t gsvs_ring_size,
1195 struct radeon_winsys_cs **preamble_cs)
1196 {
1197 struct radeon_winsys_bo *scratch_bo = NULL;
1198 struct radeon_winsys_bo *descriptor_bo = NULL;
1199 struct radeon_winsys_bo *compute_scratch_bo = NULL;
1200 struct radeon_winsys_bo *esgs_ring_bo = NULL;
1201 struct radeon_winsys_bo *gsvs_ring_bo = NULL;
1202 struct radeon_winsys_cs *cs = NULL;
1203
1204 if (!scratch_size && !compute_scratch_size && !esgs_ring_size && !gsvs_ring_size) {
1205 *preamble_cs = NULL;
1206 return VK_SUCCESS;
1207 }
1208
1209 if (scratch_size <= queue->scratch_size &&
1210 compute_scratch_size <= queue->compute_scratch_size &&
1211 esgs_ring_size <= queue->esgs_ring_size &&
1212 gsvs_ring_size <= queue->gsvs_ring_size) {
1213 *preamble_cs = queue->preamble_cs;
1214 return VK_SUCCESS;
1215 }
1216
1217 if (scratch_size > queue->scratch_size) {
1218 scratch_bo = queue->device->ws->buffer_create(queue->device->ws,
1219 scratch_size,
1220 4096,
1221 RADEON_DOMAIN_VRAM,
1222 RADEON_FLAG_NO_CPU_ACCESS);
1223 if (!scratch_bo)
1224 goto fail;
1225 } else
1226 scratch_bo = queue->scratch_bo;
1227
1228 if (compute_scratch_size > queue->compute_scratch_size) {
1229 compute_scratch_bo = queue->device->ws->buffer_create(queue->device->ws,
1230 compute_scratch_size,
1231 4096,
1232 RADEON_DOMAIN_VRAM,
1233 RADEON_FLAG_NO_CPU_ACCESS);
1234 if (!compute_scratch_bo)
1235 goto fail;
1236
1237 } else
1238 compute_scratch_bo = queue->compute_scratch_bo;
1239
1240 if (esgs_ring_size > queue->esgs_ring_size) {
1241 esgs_ring_bo = queue->device->ws->buffer_create(queue->device->ws,
1242 esgs_ring_size,
1243 4096,
1244 RADEON_DOMAIN_VRAM,
1245 RADEON_FLAG_NO_CPU_ACCESS);
1246 if (!esgs_ring_bo)
1247 goto fail;
1248 } else {
1249 esgs_ring_bo = queue->esgs_ring_bo;
1250 esgs_ring_size = queue->esgs_ring_size;
1251 }
1252
1253 if (gsvs_ring_size > queue->gsvs_ring_size) {
1254 gsvs_ring_bo = queue->device->ws->buffer_create(queue->device->ws,
1255 gsvs_ring_size,
1256 4096,
1257 RADEON_DOMAIN_VRAM,
1258 RADEON_FLAG_NO_CPU_ACCESS);
1259 if (!gsvs_ring_bo)
1260 goto fail;
1261 } else {
1262 gsvs_ring_bo = queue->gsvs_ring_bo;
1263 gsvs_ring_size = queue->gsvs_ring_size;
1264 }
1265
1266 if (scratch_bo != queue->scratch_bo ||
1267 esgs_ring_bo != queue->esgs_ring_bo ||
1268 gsvs_ring_bo != queue->gsvs_ring_bo) {
1269 uint32_t size = 0;
1270 if (gsvs_ring_bo || esgs_ring_bo)
1271 size = 80; /* 2 dword + 2 padding + 4 dword * 4 */
1272 else if (scratch_bo)
1273 size = 8; /* 2 dword */
1274
1275 descriptor_bo = queue->device->ws->buffer_create(queue->device->ws,
1276 size,
1277 4096,
1278 RADEON_DOMAIN_VRAM,
1279 RADEON_FLAG_CPU_ACCESS);
1280 if (!descriptor_bo)
1281 goto fail;
1282 } else
1283 descriptor_bo = queue->descriptor_bo;
1284
1285 cs = queue->device->ws->cs_create(queue->device->ws,
1286 queue->queue_family_index ? RING_COMPUTE : RING_GFX);
1287 if (!cs)
1288 goto fail;
1289
1290
1291 if (scratch_bo)
1292 queue->device->ws->cs_add_buffer(cs, scratch_bo, 8);
1293
1294 if (esgs_ring_bo)
1295 queue->device->ws->cs_add_buffer(cs, esgs_ring_bo, 8);
1296
1297 if (gsvs_ring_bo)
1298 queue->device->ws->cs_add_buffer(cs, gsvs_ring_bo, 8);
1299
1300 if (descriptor_bo)
1301 queue->device->ws->cs_add_buffer(cs, descriptor_bo, 8);
1302
1303 if (descriptor_bo != queue->descriptor_bo) {
1304 uint32_t *map = (uint32_t*)queue->device->ws->buffer_map(descriptor_bo);
1305
1306 if (scratch_bo) {
1307 uint64_t scratch_va = queue->device->ws->buffer_get_va(scratch_bo);
1308 uint32_t rsrc1 = S_008F04_BASE_ADDRESS_HI(scratch_va >> 32) |
1309 S_008F04_SWIZZLE_ENABLE(1);
1310 map[0] = scratch_va;
1311 map[1] = rsrc1;
1312 }
1313
1314 if (esgs_ring_bo || gsvs_ring_bo)
1315 fill_geom_rings(queue, map, esgs_ring_size, esgs_ring_bo, gsvs_ring_size, gsvs_ring_bo);
1316
1317 queue->device->ws->buffer_unmap(descriptor_bo);
1318 }
1319
1320 if (esgs_ring_bo || gsvs_ring_bo) {
1321 radeon_emit(cs, PKT3(PKT3_EVENT_WRITE, 0, 0));
1322 radeon_emit(cs, EVENT_TYPE(V_028A90_VS_PARTIAL_FLUSH) | EVENT_INDEX(4));
1323 radeon_emit(cs, PKT3(PKT3_EVENT_WRITE, 0, 0));
1324 radeon_emit(cs, EVENT_TYPE(V_028A90_VGT_FLUSH) | EVENT_INDEX(0));
1325
1326 if (queue->device->physical_device->rad_info.chip_class >= CIK) {
1327 radeon_set_uconfig_reg_seq(cs, R_030900_VGT_ESGS_RING_SIZE, 2);
1328 radeon_emit(cs, esgs_ring_size >> 8);
1329 radeon_emit(cs, gsvs_ring_size >> 8);
1330 } else {
1331 radeon_set_config_reg_seq(cs, R_0088C8_VGT_ESGS_RING_SIZE, 2);
1332 radeon_emit(cs, esgs_ring_size >> 8);
1333 radeon_emit(cs, gsvs_ring_size >> 8);
1334 }
1335 }
1336
1337 if (descriptor_bo) {
1338 uint32_t regs[] = {R_00B030_SPI_SHADER_USER_DATA_PS_0,
1339 R_00B130_SPI_SHADER_USER_DATA_VS_0,
1340 R_00B230_SPI_SHADER_USER_DATA_GS_0,
1341 R_00B330_SPI_SHADER_USER_DATA_ES_0,
1342 R_00B430_SPI_SHADER_USER_DATA_HS_0,
1343 R_00B530_SPI_SHADER_USER_DATA_LS_0};
1344
1345 uint64_t va = queue->device->ws->buffer_get_va(descriptor_bo);
1346
1347 for (int i = 0; i < ARRAY_SIZE(regs); ++i) {
1348 radeon_set_sh_reg_seq(cs, regs[i], 2);
1349 radeon_emit(cs, va);
1350 radeon_emit(cs, va >> 32);
1351 }
1352 }
1353
1354 if (compute_scratch_bo) {
1355 uint64_t scratch_va = queue->device->ws->buffer_get_va(compute_scratch_bo);
1356 uint32_t rsrc1 = S_008F04_BASE_ADDRESS_HI(scratch_va >> 32) |
1357 S_008F04_SWIZZLE_ENABLE(1);
1358
1359 queue->device->ws->cs_add_buffer(cs, compute_scratch_bo, 8);
1360
1361 radeon_set_sh_reg_seq(cs, R_00B900_COMPUTE_USER_DATA_0, 2);
1362 radeon_emit(cs, scratch_va);
1363 radeon_emit(cs, rsrc1);
1364 }
1365
1366 if (!queue->device->ws->cs_finalize(cs))
1367 goto fail;
1368
1369 if (queue->preamble_cs)
1370 queue->device->ws->cs_destroy(queue->preamble_cs);
1371
1372 queue->preamble_cs = cs;
1373
1374 if (scratch_bo != queue->scratch_bo) {
1375 if (queue->scratch_bo)
1376 queue->device->ws->buffer_destroy(queue->scratch_bo);
1377 queue->scratch_bo = scratch_bo;
1378 queue->scratch_size = scratch_size;
1379 }
1380
1381 if (compute_scratch_bo != queue->compute_scratch_bo) {
1382 if (queue->compute_scratch_bo)
1383 queue->device->ws->buffer_destroy(queue->compute_scratch_bo);
1384 queue->compute_scratch_bo = compute_scratch_bo;
1385 queue->compute_scratch_size = compute_scratch_size;
1386 }
1387
1388 if (esgs_ring_bo != queue->esgs_ring_bo) {
1389 if (queue->esgs_ring_bo)
1390 queue->device->ws->buffer_destroy(queue->esgs_ring_bo);
1391 queue->esgs_ring_bo = esgs_ring_bo;
1392 queue->esgs_ring_size = esgs_ring_size;
1393 }
1394
1395 if (gsvs_ring_bo != queue->gsvs_ring_bo) {
1396 if (queue->gsvs_ring_bo)
1397 queue->device->ws->buffer_destroy(queue->gsvs_ring_bo);
1398 queue->gsvs_ring_bo = gsvs_ring_bo;
1399 queue->gsvs_ring_size = gsvs_ring_size;
1400 }
1401
1402 if (descriptor_bo != queue->descriptor_bo) {
1403 if (queue->descriptor_bo)
1404 queue->device->ws->buffer_destroy(queue->descriptor_bo);
1405
1406 queue->descriptor_bo = descriptor_bo;
1407 }
1408
1409 *preamble_cs = cs;
1410 return VK_SUCCESS;
1411 fail:
1412 if (cs)
1413 queue->device->ws->cs_destroy(cs);
1414 if (descriptor_bo && descriptor_bo != queue->descriptor_bo)
1415 queue->device->ws->buffer_destroy(descriptor_bo);
1416 if (scratch_bo && scratch_bo != queue->scratch_bo)
1417 queue->device->ws->buffer_destroy(scratch_bo);
1418 if (compute_scratch_bo && compute_scratch_bo != queue->compute_scratch_bo)
1419 queue->device->ws->buffer_destroy(compute_scratch_bo);
1420 if (esgs_ring_bo && esgs_ring_bo != queue->esgs_ring_bo)
1421 queue->device->ws->buffer_destroy(esgs_ring_bo);
1422 if (gsvs_ring_bo && gsvs_ring_bo != queue->gsvs_ring_bo)
1423 queue->device->ws->buffer_destroy(gsvs_ring_bo);
1424 return VK_ERROR_OUT_OF_DEVICE_MEMORY;
1425 }
1426
1427 VkResult radv_QueueSubmit(
1428 VkQueue _queue,
1429 uint32_t submitCount,
1430 const VkSubmitInfo* pSubmits,
1431 VkFence _fence)
1432 {
1433 RADV_FROM_HANDLE(radv_queue, queue, _queue);
1434 RADV_FROM_HANDLE(radv_fence, fence, _fence);
1435 struct radeon_winsys_fence *base_fence = fence ? fence->fence : NULL;
1436 struct radeon_winsys_ctx *ctx = queue->hw_ctx;
1437 int ret;
1438 uint32_t max_cs_submission = queue->device->trace_bo ? 1 : UINT32_MAX;
1439 uint32_t scratch_size = 0;
1440 uint32_t compute_scratch_size = 0;
1441 uint32_t esgs_ring_size = 0, gsvs_ring_size = 0;
1442 struct radeon_winsys_cs *preamble_cs = NULL;
1443 VkResult result;
1444 bool fence_emitted = false;
1445
1446 /* Do this first so failing to allocate scratch buffers can't result in
1447 * partially executed submissions. */
1448 for (uint32_t i = 0; i < submitCount; i++) {
1449 for (uint32_t j = 0; j < pSubmits[i].commandBufferCount; j++) {
1450 RADV_FROM_HANDLE(radv_cmd_buffer, cmd_buffer,
1451 pSubmits[i].pCommandBuffers[j]);
1452
1453 scratch_size = MAX2(scratch_size, cmd_buffer->scratch_size_needed);
1454 compute_scratch_size = MAX2(compute_scratch_size,
1455 cmd_buffer->compute_scratch_size_needed);
1456 esgs_ring_size = MAX2(esgs_ring_size, cmd_buffer->esgs_ring_size_needed);
1457 gsvs_ring_size = MAX2(gsvs_ring_size, cmd_buffer->gsvs_ring_size_needed);
1458 }
1459 }
1460
1461 result = radv_get_preamble_cs(queue, scratch_size, compute_scratch_size, esgs_ring_size, gsvs_ring_size, &preamble_cs);
1462 if (result != VK_SUCCESS)
1463 return result;
1464
1465 for (uint32_t i = 0; i < submitCount; i++) {
1466 struct radeon_winsys_cs **cs_array;
1467 bool can_patch = true;
1468 uint32_t advance;
1469
1470 if (!pSubmits[i].commandBufferCount) {
1471 if (pSubmits[i].waitSemaphoreCount || pSubmits[i].signalSemaphoreCount) {
1472 ret = queue->device->ws->cs_submit(ctx, queue->queue_idx,
1473 &queue->device->empty_cs[queue->queue_family_index],
1474 1, NULL, NULL,
1475 (struct radeon_winsys_sem **)pSubmits[i].pWaitSemaphores,
1476 pSubmits[i].waitSemaphoreCount,
1477 (struct radeon_winsys_sem **)pSubmits[i].pSignalSemaphores,
1478 pSubmits[i].signalSemaphoreCount,
1479 false, base_fence);
1480 if (ret) {
1481 radv_loge("failed to submit CS %d\n", i);
1482 abort();
1483 }
1484 fence_emitted = true;
1485 }
1486 continue;
1487 }
1488
1489 cs_array = malloc(sizeof(struct radeon_winsys_cs *) *
1490 pSubmits[i].commandBufferCount);
1491
1492 for (uint32_t j = 0; j < pSubmits[i].commandBufferCount; j++) {
1493 RADV_FROM_HANDLE(radv_cmd_buffer, cmd_buffer,
1494 pSubmits[i].pCommandBuffers[j]);
1495 assert(cmd_buffer->level == VK_COMMAND_BUFFER_LEVEL_PRIMARY);
1496
1497 cs_array[j] = cmd_buffer->cs;
1498 if ((cmd_buffer->usage_flags & VK_COMMAND_BUFFER_USAGE_SIMULTANEOUS_USE_BIT))
1499 can_patch = false;
1500 }
1501
1502 for (uint32_t j = 0; j < pSubmits[i].commandBufferCount; j += advance) {
1503 advance = MIN2(max_cs_submission,
1504 pSubmits[i].commandBufferCount - j);
1505 bool b = j == 0;
1506 bool e = j + advance == pSubmits[i].commandBufferCount;
1507
1508 if (queue->device->trace_bo)
1509 *queue->device->trace_id_ptr = 0;
1510
1511 ret = queue->device->ws->cs_submit(ctx, queue->queue_idx, cs_array + j,
1512 advance, preamble_cs, preamble_cs,
1513 (struct radeon_winsys_sem **)pSubmits[i].pWaitSemaphores,
1514 b ? pSubmits[i].waitSemaphoreCount : 0,
1515 (struct radeon_winsys_sem **)pSubmits[i].pSignalSemaphores,
1516 e ? pSubmits[i].signalSemaphoreCount : 0,
1517 can_patch, base_fence);
1518
1519 if (ret) {
1520 radv_loge("failed to submit CS %d\n", i);
1521 abort();
1522 }
1523 fence_emitted = true;
1524 if (queue->device->trace_bo) {
1525 bool success = queue->device->ws->ctx_wait_idle(
1526 queue->hw_ctx,
1527 radv_queue_family_to_ring(
1528 queue->queue_family_index),
1529 queue->queue_idx);
1530
1531 if (!success) { /* Hang */
1532 radv_dump_trace(queue->device, cs_array[j]);
1533 abort();
1534 }
1535 }
1536 }
1537 free(cs_array);
1538 }
1539
1540 if (fence) {
1541 if (!fence_emitted)
1542 ret = queue->device->ws->cs_submit(ctx, queue->queue_idx,
1543 &queue->device->empty_cs[queue->queue_family_index],
1544 1, NULL, NULL, NULL, 0, NULL, 0,
1545 false, base_fence);
1546
1547 fence->submitted = true;
1548 }
1549
1550 return VK_SUCCESS;
1551 }
1552
1553 VkResult radv_QueueWaitIdle(
1554 VkQueue _queue)
1555 {
1556 RADV_FROM_HANDLE(radv_queue, queue, _queue);
1557
1558 queue->device->ws->ctx_wait_idle(queue->hw_ctx,
1559 radv_queue_family_to_ring(queue->queue_family_index),
1560 queue->queue_idx);
1561 return VK_SUCCESS;
1562 }
1563
1564 VkResult radv_DeviceWaitIdle(
1565 VkDevice _device)
1566 {
1567 RADV_FROM_HANDLE(radv_device, device, _device);
1568
1569 for (unsigned i = 0; i < RADV_MAX_QUEUE_FAMILIES; i++) {
1570 for (unsigned q = 0; q < device->queue_count[i]; q++) {
1571 radv_QueueWaitIdle(radv_queue_to_handle(&device->queues[i][q]));
1572 }
1573 }
1574 return VK_SUCCESS;
1575 }
1576
1577 PFN_vkVoidFunction radv_GetInstanceProcAddr(
1578 VkInstance instance,
1579 const char* pName)
1580 {
1581 return radv_lookup_entrypoint(pName);
1582 }
1583
1584 /* The loader wants us to expose a second GetInstanceProcAddr function
1585 * to work around certain LD_PRELOAD issues seen in apps.
1586 */
1587 PUBLIC
1588 VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL vk_icdGetInstanceProcAddr(
1589 VkInstance instance,
1590 const char* pName);
1591
1592 PUBLIC
1593 VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL vk_icdGetInstanceProcAddr(
1594 VkInstance instance,
1595 const char* pName)
1596 {
1597 return radv_GetInstanceProcAddr(instance, pName);
1598 }
1599
1600 PFN_vkVoidFunction radv_GetDeviceProcAddr(
1601 VkDevice device,
1602 const char* pName)
1603 {
1604 return radv_lookup_entrypoint(pName);
1605 }
1606
1607 VkResult radv_AllocateMemory(
1608 VkDevice _device,
1609 const VkMemoryAllocateInfo* pAllocateInfo,
1610 const VkAllocationCallbacks* pAllocator,
1611 VkDeviceMemory* pMem)
1612 {
1613 RADV_FROM_HANDLE(radv_device, device, _device);
1614 struct radv_device_memory *mem;
1615 VkResult result;
1616 enum radeon_bo_domain domain;
1617 uint32_t flags = 0;
1618 assert(pAllocateInfo->sType == VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO);
1619
1620 if (pAllocateInfo->allocationSize == 0) {
1621 /* Apparently, this is allowed */
1622 *pMem = VK_NULL_HANDLE;
1623 return VK_SUCCESS;
1624 }
1625
1626 mem = vk_alloc2(&device->alloc, pAllocator, sizeof(*mem), 8,
1627 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
1628 if (mem == NULL)
1629 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
1630
1631 uint64_t alloc_size = align_u64(pAllocateInfo->allocationSize, 4096);
1632 if (pAllocateInfo->memoryTypeIndex == RADV_MEM_TYPE_GTT_WRITE_COMBINE ||
1633 pAllocateInfo->memoryTypeIndex == RADV_MEM_TYPE_GTT_CACHED)
1634 domain = RADEON_DOMAIN_GTT;
1635 else
1636 domain = RADEON_DOMAIN_VRAM;
1637
1638 if (pAllocateInfo->memoryTypeIndex == RADV_MEM_TYPE_VRAM)
1639 flags |= RADEON_FLAG_NO_CPU_ACCESS;
1640 else
1641 flags |= RADEON_FLAG_CPU_ACCESS;
1642
1643 if (pAllocateInfo->memoryTypeIndex == RADV_MEM_TYPE_GTT_WRITE_COMBINE)
1644 flags |= RADEON_FLAG_GTT_WC;
1645
1646 mem->bo = device->ws->buffer_create(device->ws, alloc_size, 65536,
1647 domain, flags);
1648
1649 if (!mem->bo) {
1650 result = VK_ERROR_OUT_OF_DEVICE_MEMORY;
1651 goto fail;
1652 }
1653 mem->type_index = pAllocateInfo->memoryTypeIndex;
1654
1655 *pMem = radv_device_memory_to_handle(mem);
1656
1657 return VK_SUCCESS;
1658
1659 fail:
1660 vk_free2(&device->alloc, pAllocator, mem);
1661
1662 return result;
1663 }
1664
1665 void radv_FreeMemory(
1666 VkDevice _device,
1667 VkDeviceMemory _mem,
1668 const VkAllocationCallbacks* pAllocator)
1669 {
1670 RADV_FROM_HANDLE(radv_device, device, _device);
1671 RADV_FROM_HANDLE(radv_device_memory, mem, _mem);
1672
1673 if (mem == NULL)
1674 return;
1675
1676 device->ws->buffer_destroy(mem->bo);
1677 mem->bo = NULL;
1678
1679 vk_free2(&device->alloc, pAllocator, mem);
1680 }
1681
1682 VkResult radv_MapMemory(
1683 VkDevice _device,
1684 VkDeviceMemory _memory,
1685 VkDeviceSize offset,
1686 VkDeviceSize size,
1687 VkMemoryMapFlags flags,
1688 void** ppData)
1689 {
1690 RADV_FROM_HANDLE(radv_device, device, _device);
1691 RADV_FROM_HANDLE(radv_device_memory, mem, _memory);
1692
1693 if (mem == NULL) {
1694 *ppData = NULL;
1695 return VK_SUCCESS;
1696 }
1697
1698 *ppData = device->ws->buffer_map(mem->bo);
1699 if (*ppData) {
1700 *ppData += offset;
1701 return VK_SUCCESS;
1702 }
1703
1704 return VK_ERROR_MEMORY_MAP_FAILED;
1705 }
1706
1707 void radv_UnmapMemory(
1708 VkDevice _device,
1709 VkDeviceMemory _memory)
1710 {
1711 RADV_FROM_HANDLE(radv_device, device, _device);
1712 RADV_FROM_HANDLE(radv_device_memory, mem, _memory);
1713
1714 if (mem == NULL)
1715 return;
1716
1717 device->ws->buffer_unmap(mem->bo);
1718 }
1719
1720 VkResult radv_FlushMappedMemoryRanges(
1721 VkDevice _device,
1722 uint32_t memoryRangeCount,
1723 const VkMappedMemoryRange* pMemoryRanges)
1724 {
1725 return VK_SUCCESS;
1726 }
1727
1728 VkResult radv_InvalidateMappedMemoryRanges(
1729 VkDevice _device,
1730 uint32_t memoryRangeCount,
1731 const VkMappedMemoryRange* pMemoryRanges)
1732 {
1733 return VK_SUCCESS;
1734 }
1735
1736 void radv_GetBufferMemoryRequirements(
1737 VkDevice device,
1738 VkBuffer _buffer,
1739 VkMemoryRequirements* pMemoryRequirements)
1740 {
1741 RADV_FROM_HANDLE(radv_buffer, buffer, _buffer);
1742
1743 pMemoryRequirements->memoryTypeBits = (1u << RADV_MEM_TYPE_COUNT) - 1;
1744
1745 pMemoryRequirements->size = buffer->size;
1746 pMemoryRequirements->alignment = 16;
1747 }
1748
1749 void radv_GetImageMemoryRequirements(
1750 VkDevice device,
1751 VkImage _image,
1752 VkMemoryRequirements* pMemoryRequirements)
1753 {
1754 RADV_FROM_HANDLE(radv_image, image, _image);
1755
1756 pMemoryRequirements->memoryTypeBits = (1u << RADV_MEM_TYPE_COUNT) - 1;
1757
1758 pMemoryRequirements->size = image->size;
1759 pMemoryRequirements->alignment = image->alignment;
1760 }
1761
1762 void radv_GetImageSparseMemoryRequirements(
1763 VkDevice device,
1764 VkImage image,
1765 uint32_t* pSparseMemoryRequirementCount,
1766 VkSparseImageMemoryRequirements* pSparseMemoryRequirements)
1767 {
1768 stub();
1769 }
1770
1771 void radv_GetDeviceMemoryCommitment(
1772 VkDevice device,
1773 VkDeviceMemory memory,
1774 VkDeviceSize* pCommittedMemoryInBytes)
1775 {
1776 *pCommittedMemoryInBytes = 0;
1777 }
1778
1779 VkResult radv_BindBufferMemory(
1780 VkDevice device,
1781 VkBuffer _buffer,
1782 VkDeviceMemory _memory,
1783 VkDeviceSize memoryOffset)
1784 {
1785 RADV_FROM_HANDLE(radv_device_memory, mem, _memory);
1786 RADV_FROM_HANDLE(radv_buffer, buffer, _buffer);
1787
1788 if (mem) {
1789 buffer->bo = mem->bo;
1790 buffer->offset = memoryOffset;
1791 } else {
1792 buffer->bo = NULL;
1793 buffer->offset = 0;
1794 }
1795
1796 return VK_SUCCESS;
1797 }
1798
1799 VkResult radv_BindImageMemory(
1800 VkDevice device,
1801 VkImage _image,
1802 VkDeviceMemory _memory,
1803 VkDeviceSize memoryOffset)
1804 {
1805 RADV_FROM_HANDLE(radv_device_memory, mem, _memory);
1806 RADV_FROM_HANDLE(radv_image, image, _image);
1807
1808 if (mem) {
1809 image->bo = mem->bo;
1810 image->offset = memoryOffset;
1811 } else {
1812 image->bo = NULL;
1813 image->offset = 0;
1814 }
1815
1816 return VK_SUCCESS;
1817 }
1818
1819 VkResult radv_QueueBindSparse(
1820 VkQueue queue,
1821 uint32_t bindInfoCount,
1822 const VkBindSparseInfo* pBindInfo,
1823 VkFence fence)
1824 {
1825 stub_return(VK_ERROR_INCOMPATIBLE_DRIVER);
1826 }
1827
1828 VkResult radv_CreateFence(
1829 VkDevice _device,
1830 const VkFenceCreateInfo* pCreateInfo,
1831 const VkAllocationCallbacks* pAllocator,
1832 VkFence* pFence)
1833 {
1834 RADV_FROM_HANDLE(radv_device, device, _device);
1835 struct radv_fence *fence = vk_alloc2(&device->alloc, pAllocator,
1836 sizeof(*fence), 8,
1837 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
1838
1839 if (!fence)
1840 return VK_ERROR_OUT_OF_HOST_MEMORY;
1841
1842 memset(fence, 0, sizeof(*fence));
1843 fence->submitted = false;
1844 fence->signalled = !!(pCreateInfo->flags & VK_FENCE_CREATE_SIGNALED_BIT);
1845 fence->fence = device->ws->create_fence();
1846 if (!fence->fence) {
1847 vk_free2(&device->alloc, pAllocator, fence);
1848 return VK_ERROR_OUT_OF_HOST_MEMORY;
1849 }
1850
1851 *pFence = radv_fence_to_handle(fence);
1852
1853 return VK_SUCCESS;
1854 }
1855
1856 void radv_DestroyFence(
1857 VkDevice _device,
1858 VkFence _fence,
1859 const VkAllocationCallbacks* pAllocator)
1860 {
1861 RADV_FROM_HANDLE(radv_device, device, _device);
1862 RADV_FROM_HANDLE(radv_fence, fence, _fence);
1863
1864 if (!fence)
1865 return;
1866 device->ws->destroy_fence(fence->fence);
1867 vk_free2(&device->alloc, pAllocator, fence);
1868 }
1869
1870 static uint64_t radv_get_absolute_timeout(uint64_t timeout)
1871 {
1872 uint64_t current_time;
1873 struct timespec tv;
1874
1875 clock_gettime(CLOCK_MONOTONIC, &tv);
1876 current_time = tv.tv_nsec + tv.tv_sec*1000000000ull;
1877
1878 timeout = MIN2(UINT64_MAX - current_time, timeout);
1879
1880 return current_time + timeout;
1881 }
1882
1883 VkResult radv_WaitForFences(
1884 VkDevice _device,
1885 uint32_t fenceCount,
1886 const VkFence* pFences,
1887 VkBool32 waitAll,
1888 uint64_t timeout)
1889 {
1890 RADV_FROM_HANDLE(radv_device, device, _device);
1891 timeout = radv_get_absolute_timeout(timeout);
1892
1893 if (!waitAll && fenceCount > 1) {
1894 fprintf(stderr, "radv: WaitForFences without waitAll not implemented yet\n");
1895 }
1896
1897 for (uint32_t i = 0; i < fenceCount; ++i) {
1898 RADV_FROM_HANDLE(radv_fence, fence, pFences[i]);
1899 bool expired = false;
1900
1901 if (fence->signalled)
1902 continue;
1903
1904 if (!fence->submitted)
1905 return VK_TIMEOUT;
1906
1907 expired = device->ws->fence_wait(device->ws, fence->fence, true, timeout);
1908 if (!expired)
1909 return VK_TIMEOUT;
1910
1911 fence->signalled = true;
1912 }
1913
1914 return VK_SUCCESS;
1915 }
1916
1917 VkResult radv_ResetFences(VkDevice device,
1918 uint32_t fenceCount,
1919 const VkFence *pFences)
1920 {
1921 for (unsigned i = 0; i < fenceCount; ++i) {
1922 RADV_FROM_HANDLE(radv_fence, fence, pFences[i]);
1923 fence->submitted = fence->signalled = false;
1924 }
1925
1926 return VK_SUCCESS;
1927 }
1928
1929 VkResult radv_GetFenceStatus(VkDevice _device, VkFence _fence)
1930 {
1931 RADV_FROM_HANDLE(radv_device, device, _device);
1932 RADV_FROM_HANDLE(radv_fence, fence, _fence);
1933
1934 if (fence->signalled)
1935 return VK_SUCCESS;
1936 if (!fence->submitted)
1937 return VK_NOT_READY;
1938
1939 if (!device->ws->fence_wait(device->ws, fence->fence, false, 0))
1940 return VK_NOT_READY;
1941
1942 return VK_SUCCESS;
1943 }
1944
1945
1946 // Queue semaphore functions
1947
1948 VkResult radv_CreateSemaphore(
1949 VkDevice _device,
1950 const VkSemaphoreCreateInfo* pCreateInfo,
1951 const VkAllocationCallbacks* pAllocator,
1952 VkSemaphore* pSemaphore)
1953 {
1954 RADV_FROM_HANDLE(radv_device, device, _device);
1955 struct radeon_winsys_sem *sem;
1956
1957 sem = device->ws->create_sem(device->ws);
1958 if (!sem)
1959 return VK_ERROR_OUT_OF_HOST_MEMORY;
1960
1961 *pSemaphore = (VkSemaphore)sem;
1962 return VK_SUCCESS;
1963 }
1964
1965 void radv_DestroySemaphore(
1966 VkDevice _device,
1967 VkSemaphore _semaphore,
1968 const VkAllocationCallbacks* pAllocator)
1969 {
1970 RADV_FROM_HANDLE(radv_device, device, _device);
1971 struct radeon_winsys_sem *sem;
1972 if (!_semaphore)
1973 return;
1974
1975 sem = (struct radeon_winsys_sem *)_semaphore;
1976 device->ws->destroy_sem(sem);
1977 }
1978
1979 VkResult radv_CreateEvent(
1980 VkDevice _device,
1981 const VkEventCreateInfo* pCreateInfo,
1982 const VkAllocationCallbacks* pAllocator,
1983 VkEvent* pEvent)
1984 {
1985 RADV_FROM_HANDLE(radv_device, device, _device);
1986 struct radv_event *event = vk_alloc2(&device->alloc, pAllocator,
1987 sizeof(*event), 8,
1988 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
1989
1990 if (!event)
1991 return VK_ERROR_OUT_OF_HOST_MEMORY;
1992
1993 event->bo = device->ws->buffer_create(device->ws, 8, 8,
1994 RADEON_DOMAIN_GTT,
1995 RADEON_FLAG_CPU_ACCESS);
1996 if (!event->bo) {
1997 vk_free2(&device->alloc, pAllocator, event);
1998 return VK_ERROR_OUT_OF_DEVICE_MEMORY;
1999 }
2000
2001 event->map = (uint64_t*)device->ws->buffer_map(event->bo);
2002
2003 *pEvent = radv_event_to_handle(event);
2004
2005 return VK_SUCCESS;
2006 }
2007
2008 void radv_DestroyEvent(
2009 VkDevice _device,
2010 VkEvent _event,
2011 const VkAllocationCallbacks* pAllocator)
2012 {
2013 RADV_FROM_HANDLE(radv_device, device, _device);
2014 RADV_FROM_HANDLE(radv_event, event, _event);
2015
2016 if (!event)
2017 return;
2018 device->ws->buffer_destroy(event->bo);
2019 vk_free2(&device->alloc, pAllocator, event);
2020 }
2021
2022 VkResult radv_GetEventStatus(
2023 VkDevice _device,
2024 VkEvent _event)
2025 {
2026 RADV_FROM_HANDLE(radv_event, event, _event);
2027
2028 if (*event->map == 1)
2029 return VK_EVENT_SET;
2030 return VK_EVENT_RESET;
2031 }
2032
2033 VkResult radv_SetEvent(
2034 VkDevice _device,
2035 VkEvent _event)
2036 {
2037 RADV_FROM_HANDLE(radv_event, event, _event);
2038 *event->map = 1;
2039
2040 return VK_SUCCESS;
2041 }
2042
2043 VkResult radv_ResetEvent(
2044 VkDevice _device,
2045 VkEvent _event)
2046 {
2047 RADV_FROM_HANDLE(radv_event, event, _event);
2048 *event->map = 0;
2049
2050 return VK_SUCCESS;
2051 }
2052
2053 VkResult radv_CreateBuffer(
2054 VkDevice _device,
2055 const VkBufferCreateInfo* pCreateInfo,
2056 const VkAllocationCallbacks* pAllocator,
2057 VkBuffer* pBuffer)
2058 {
2059 RADV_FROM_HANDLE(radv_device, device, _device);
2060 struct radv_buffer *buffer;
2061
2062 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO);
2063
2064 buffer = vk_alloc2(&device->alloc, pAllocator, sizeof(*buffer), 8,
2065 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
2066 if (buffer == NULL)
2067 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
2068
2069 buffer->size = pCreateInfo->size;
2070 buffer->usage = pCreateInfo->usage;
2071 buffer->bo = NULL;
2072 buffer->offset = 0;
2073
2074 *pBuffer = radv_buffer_to_handle(buffer);
2075
2076 return VK_SUCCESS;
2077 }
2078
2079 void radv_DestroyBuffer(
2080 VkDevice _device,
2081 VkBuffer _buffer,
2082 const VkAllocationCallbacks* pAllocator)
2083 {
2084 RADV_FROM_HANDLE(radv_device, device, _device);
2085 RADV_FROM_HANDLE(radv_buffer, buffer, _buffer);
2086
2087 if (!buffer)
2088 return;
2089
2090 vk_free2(&device->alloc, pAllocator, buffer);
2091 }
2092
2093 static inline unsigned
2094 si_tile_mode_index(const struct radv_image *image, unsigned level, bool stencil)
2095 {
2096 if (stencil)
2097 return image->surface.stencil_tiling_index[level];
2098 else
2099 return image->surface.tiling_index[level];
2100 }
2101
2102 static uint32_t radv_surface_layer_count(struct radv_image_view *iview)
2103 {
2104 return iview->type == VK_IMAGE_VIEW_TYPE_3D ? iview->extent.depth : iview->layer_count;
2105 }
2106
2107 static void
2108 radv_initialise_color_surface(struct radv_device *device,
2109 struct radv_color_buffer_info *cb,
2110 struct radv_image_view *iview)
2111 {
2112 const struct vk_format_description *desc;
2113 unsigned ntype, format, swap, endian;
2114 unsigned blend_clamp = 0, blend_bypass = 0;
2115 unsigned pitch_tile_max, slice_tile_max, tile_mode_index;
2116 uint64_t va;
2117 const struct radeon_surf *surf = &iview->image->surface;
2118 const struct radeon_surf_level *level_info = &surf->level[iview->base_mip];
2119
2120 desc = vk_format_description(iview->vk_format);
2121
2122 memset(cb, 0, sizeof(*cb));
2123
2124 va = device->ws->buffer_get_va(iview->bo) + iview->image->offset;
2125 va += level_info->offset;
2126 cb->cb_color_base = va >> 8;
2127
2128 /* CMASK variables */
2129 va = device->ws->buffer_get_va(iview->bo) + iview->image->offset;
2130 va += iview->image->cmask.offset;
2131 cb->cb_color_cmask = va >> 8;
2132 cb->cb_color_cmask_slice = iview->image->cmask.slice_tile_max;
2133
2134 va = device->ws->buffer_get_va(iview->bo) + iview->image->offset;
2135 va += iview->image->dcc_offset;
2136 cb->cb_dcc_base = va >> 8;
2137
2138 uint32_t max_slice = radv_surface_layer_count(iview);
2139 cb->cb_color_view = S_028C6C_SLICE_START(iview->base_layer) |
2140 S_028C6C_SLICE_MAX(iview->base_layer + max_slice - 1);
2141
2142 cb->micro_tile_mode = iview->image->surface.micro_tile_mode;
2143 pitch_tile_max = level_info->nblk_x / 8 - 1;
2144 slice_tile_max = (level_info->nblk_x * level_info->nblk_y) / 64 - 1;
2145 tile_mode_index = si_tile_mode_index(iview->image, iview->base_mip, false);
2146
2147 cb->cb_color_pitch = S_028C64_TILE_MAX(pitch_tile_max);
2148 cb->cb_color_slice = S_028C68_TILE_MAX(slice_tile_max);
2149
2150 /* Intensity is implemented as Red, so treat it that way. */
2151 cb->cb_color_attrib = S_028C74_FORCE_DST_ALPHA_1(desc->swizzle[3] == VK_SWIZZLE_1) |
2152 S_028C74_TILE_MODE_INDEX(tile_mode_index);
2153
2154 if (iview->image->samples > 1) {
2155 unsigned log_samples = util_logbase2(iview->image->samples);
2156
2157 cb->cb_color_attrib |= S_028C74_NUM_SAMPLES(log_samples) |
2158 S_028C74_NUM_FRAGMENTS(log_samples);
2159 }
2160
2161 if (iview->image->fmask.size) {
2162 va = device->ws->buffer_get_va(iview->bo) + iview->image->offset + iview->image->fmask.offset;
2163 if (device->physical_device->rad_info.chip_class >= CIK)
2164 cb->cb_color_pitch |= S_028C64_FMASK_TILE_MAX(iview->image->fmask.pitch_in_pixels / 8 - 1);
2165 cb->cb_color_attrib |= S_028C74_FMASK_TILE_MODE_INDEX(iview->image->fmask.tile_mode_index);
2166 cb->cb_color_fmask = va >> 8;
2167 cb->cb_color_fmask_slice = S_028C88_TILE_MAX(iview->image->fmask.slice_tile_max);
2168 } else {
2169 /* This must be set for fast clear to work without FMASK. */
2170 if (device->physical_device->rad_info.chip_class >= CIK)
2171 cb->cb_color_pitch |= S_028C64_FMASK_TILE_MAX(pitch_tile_max);
2172 cb->cb_color_attrib |= S_028C74_FMASK_TILE_MODE_INDEX(tile_mode_index);
2173 cb->cb_color_fmask = cb->cb_color_base;
2174 cb->cb_color_fmask_slice = S_028C88_TILE_MAX(slice_tile_max);
2175 }
2176
2177 ntype = radv_translate_color_numformat(iview->vk_format,
2178 desc,
2179 vk_format_get_first_non_void_channel(iview->vk_format));
2180 format = radv_translate_colorformat(iview->vk_format);
2181 if (format == V_028C70_COLOR_INVALID || ntype == ~0u)
2182 radv_finishme("Illegal color\n");
2183 swap = radv_translate_colorswap(iview->vk_format, FALSE);
2184 endian = radv_colorformat_endian_swap(format);
2185
2186 /* blend clamp should be set for all NORM/SRGB types */
2187 if (ntype == V_028C70_NUMBER_UNORM ||
2188 ntype == V_028C70_NUMBER_SNORM ||
2189 ntype == V_028C70_NUMBER_SRGB)
2190 blend_clamp = 1;
2191
2192 /* set blend bypass according to docs if SINT/UINT or
2193 8/24 COLOR variants */
2194 if (ntype == V_028C70_NUMBER_UINT || ntype == V_028C70_NUMBER_SINT ||
2195 format == V_028C70_COLOR_8_24 || format == V_028C70_COLOR_24_8 ||
2196 format == V_028C70_COLOR_X24_8_32_FLOAT) {
2197 blend_clamp = 0;
2198 blend_bypass = 1;
2199 }
2200 #if 0
2201 if ((ntype == V_028C70_NUMBER_UINT || ntype == V_028C70_NUMBER_SINT) &&
2202 (format == V_028C70_COLOR_8 ||
2203 format == V_028C70_COLOR_8_8 ||
2204 format == V_028C70_COLOR_8_8_8_8))
2205 ->color_is_int8 = true;
2206 #endif
2207 cb->cb_color_info = S_028C70_FORMAT(format) |
2208 S_028C70_COMP_SWAP(swap) |
2209 S_028C70_BLEND_CLAMP(blend_clamp) |
2210 S_028C70_BLEND_BYPASS(blend_bypass) |
2211 S_028C70_SIMPLE_FLOAT(1) |
2212 S_028C70_ROUND_MODE(ntype != V_028C70_NUMBER_UNORM &&
2213 ntype != V_028C70_NUMBER_SNORM &&
2214 ntype != V_028C70_NUMBER_SRGB &&
2215 format != V_028C70_COLOR_8_24 &&
2216 format != V_028C70_COLOR_24_8) |
2217 S_028C70_NUMBER_TYPE(ntype) |
2218 S_028C70_ENDIAN(endian);
2219 if (iview->image->samples > 1)
2220 if (iview->image->fmask.size)
2221 cb->cb_color_info |= S_028C70_COMPRESSION(1);
2222
2223 if (iview->image->cmask.size &&
2224 !(device->debug_flags & RADV_DEBUG_NO_FAST_CLEARS))
2225 cb->cb_color_info |= S_028C70_FAST_CLEAR(1);
2226
2227 if (iview->image->surface.dcc_size && level_info->dcc_enabled)
2228 cb->cb_color_info |= S_028C70_DCC_ENABLE(1);
2229
2230 if (device->physical_device->rad_info.chip_class >= VI) {
2231 unsigned max_uncompressed_block_size = 2;
2232 if (iview->image->samples > 1) {
2233 if (iview->image->surface.bpe == 1)
2234 max_uncompressed_block_size = 0;
2235 else if (iview->image->surface.bpe == 2)
2236 max_uncompressed_block_size = 1;
2237 }
2238
2239 cb->cb_dcc_control = S_028C78_MAX_UNCOMPRESSED_BLOCK_SIZE(max_uncompressed_block_size) |
2240 S_028C78_INDEPENDENT_64B_BLOCKS(1);
2241 }
2242
2243 /* This must be set for fast clear to work without FMASK. */
2244 if (!iview->image->fmask.size &&
2245 device->physical_device->rad_info.chip_class == SI) {
2246 unsigned bankh = util_logbase2(iview->image->surface.bankh);
2247 cb->cb_color_attrib |= S_028C74_FMASK_BANK_HEIGHT(bankh);
2248 }
2249 }
2250
2251 static void
2252 radv_initialise_ds_surface(struct radv_device *device,
2253 struct radv_ds_buffer_info *ds,
2254 struct radv_image_view *iview)
2255 {
2256 unsigned level = iview->base_mip;
2257 unsigned format;
2258 uint64_t va, s_offs, z_offs;
2259 const struct radeon_surf_level *level_info = &iview->image->surface.level[level];
2260 memset(ds, 0, sizeof(*ds));
2261 switch (iview->vk_format) {
2262 case VK_FORMAT_D24_UNORM_S8_UINT:
2263 case VK_FORMAT_X8_D24_UNORM_PACK32:
2264 ds->pa_su_poly_offset_db_fmt_cntl = S_028B78_POLY_OFFSET_NEG_NUM_DB_BITS(-24);
2265 ds->offset_scale = 2.0f;
2266 break;
2267 case VK_FORMAT_D16_UNORM:
2268 case VK_FORMAT_D16_UNORM_S8_UINT:
2269 ds->pa_su_poly_offset_db_fmt_cntl = S_028B78_POLY_OFFSET_NEG_NUM_DB_BITS(-16);
2270 ds->offset_scale = 4.0f;
2271 break;
2272 case VK_FORMAT_D32_SFLOAT:
2273 case VK_FORMAT_D32_SFLOAT_S8_UINT:
2274 ds->pa_su_poly_offset_db_fmt_cntl = S_028B78_POLY_OFFSET_NEG_NUM_DB_BITS(-23) |
2275 S_028B78_POLY_OFFSET_DB_IS_FLOAT_FMT(1);
2276 ds->offset_scale = 1.0f;
2277 break;
2278 default:
2279 break;
2280 }
2281
2282 format = radv_translate_dbformat(iview->vk_format);
2283 if (format == V_028040_Z_INVALID) {
2284 fprintf(stderr, "Invalid DB format: %d, disabling DB.\n", iview->vk_format);
2285 }
2286
2287 va = device->ws->buffer_get_va(iview->bo) + iview->image->offset;
2288 s_offs = z_offs = va;
2289 z_offs += iview->image->surface.level[level].offset;
2290 s_offs += iview->image->surface.stencil_level[level].offset;
2291
2292 uint32_t max_slice = radv_surface_layer_count(iview);
2293 ds->db_depth_view = S_028008_SLICE_START(iview->base_layer) |
2294 S_028008_SLICE_MAX(iview->base_layer + max_slice - 1);
2295 ds->db_depth_info = S_02803C_ADDR5_SWIZZLE_MASK(1);
2296 ds->db_z_info = S_028040_FORMAT(format) | S_028040_ZRANGE_PRECISION(1);
2297
2298 if (iview->image->samples > 1)
2299 ds->db_z_info |= S_028040_NUM_SAMPLES(util_logbase2(iview->image->samples));
2300
2301 if (iview->image->surface.flags & RADEON_SURF_SBUFFER)
2302 ds->db_stencil_info = S_028044_FORMAT(V_028044_STENCIL_8);
2303 else
2304 ds->db_stencil_info = S_028044_FORMAT(V_028044_STENCIL_INVALID);
2305
2306 if (device->physical_device->rad_info.chip_class >= CIK) {
2307 struct radeon_info *info = &device->physical_device->rad_info;
2308 unsigned tiling_index = iview->image->surface.tiling_index[level];
2309 unsigned stencil_index = iview->image->surface.stencil_tiling_index[level];
2310 unsigned macro_index = iview->image->surface.macro_tile_index;
2311 unsigned tile_mode = info->si_tile_mode_array[tiling_index];
2312 unsigned stencil_tile_mode = info->si_tile_mode_array[stencil_index];
2313 unsigned macro_mode = info->cik_macrotile_mode_array[macro_index];
2314
2315 ds->db_depth_info |=
2316 S_02803C_ARRAY_MODE(G_009910_ARRAY_MODE(tile_mode)) |
2317 S_02803C_PIPE_CONFIG(G_009910_PIPE_CONFIG(tile_mode)) |
2318 S_02803C_BANK_WIDTH(G_009990_BANK_WIDTH(macro_mode)) |
2319 S_02803C_BANK_HEIGHT(G_009990_BANK_HEIGHT(macro_mode)) |
2320 S_02803C_MACRO_TILE_ASPECT(G_009990_MACRO_TILE_ASPECT(macro_mode)) |
2321 S_02803C_NUM_BANKS(G_009990_NUM_BANKS(macro_mode));
2322 ds->db_z_info |= S_028040_TILE_SPLIT(G_009910_TILE_SPLIT(tile_mode));
2323 ds->db_stencil_info |= S_028044_TILE_SPLIT(G_009910_TILE_SPLIT(stencil_tile_mode));
2324 } else {
2325 unsigned tile_mode_index = si_tile_mode_index(iview->image, level, false);
2326 ds->db_z_info |= S_028040_TILE_MODE_INDEX(tile_mode_index);
2327 tile_mode_index = si_tile_mode_index(iview->image, level, true);
2328 ds->db_stencil_info |= S_028044_TILE_MODE_INDEX(tile_mode_index);
2329 }
2330
2331 if (iview->image->htile.size && !level) {
2332 ds->db_z_info |= S_028040_TILE_SURFACE_ENABLE(1) |
2333 S_028040_ALLOW_EXPCLEAR(1);
2334
2335 if (iview->image->surface.flags & RADEON_SURF_SBUFFER) {
2336 /* Workaround: For a not yet understood reason, the
2337 * combination of MSAA, fast stencil clear and stencil
2338 * decompress messes with subsequent stencil buffer
2339 * uses. Problem was reproduced on Verde, Bonaire,
2340 * Tonga, and Carrizo.
2341 *
2342 * Disabling EXPCLEAR works around the problem.
2343 *
2344 * Check piglit's arb_texture_multisample-stencil-clear
2345 * test if you want to try changing this.
2346 */
2347 if (iview->image->samples <= 1)
2348 ds->db_stencil_info |= S_028044_ALLOW_EXPCLEAR(1);
2349 } else
2350 /* Use all of the htile_buffer for depth if there's no stencil. */
2351 ds->db_stencil_info |= S_028044_TILE_STENCIL_DISABLE(1);
2352
2353 va = device->ws->buffer_get_va(iview->bo) + iview->image->offset +
2354 iview->image->htile.offset;
2355 ds->db_htile_data_base = va >> 8;
2356 ds->db_htile_surface = S_028ABC_FULL_CACHE(1);
2357 } else {
2358 ds->db_htile_data_base = 0;
2359 ds->db_htile_surface = 0;
2360 }
2361
2362 ds->db_z_read_base = ds->db_z_write_base = z_offs >> 8;
2363 ds->db_stencil_read_base = ds->db_stencil_write_base = s_offs >> 8;
2364
2365 ds->db_depth_size = S_028058_PITCH_TILE_MAX((level_info->nblk_x / 8) - 1) |
2366 S_028058_HEIGHT_TILE_MAX((level_info->nblk_y / 8) - 1);
2367 ds->db_depth_slice = S_02805C_SLICE_TILE_MAX((level_info->nblk_x * level_info->nblk_y) / 64 - 1);
2368 }
2369
2370 VkResult radv_CreateFramebuffer(
2371 VkDevice _device,
2372 const VkFramebufferCreateInfo* pCreateInfo,
2373 const VkAllocationCallbacks* pAllocator,
2374 VkFramebuffer* pFramebuffer)
2375 {
2376 RADV_FROM_HANDLE(radv_device, device, _device);
2377 struct radv_framebuffer *framebuffer;
2378
2379 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO);
2380
2381 size_t size = sizeof(*framebuffer) +
2382 sizeof(struct radv_attachment_info) * pCreateInfo->attachmentCount;
2383 framebuffer = vk_alloc2(&device->alloc, pAllocator, size, 8,
2384 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
2385 if (framebuffer == NULL)
2386 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
2387
2388 framebuffer->attachment_count = pCreateInfo->attachmentCount;
2389 framebuffer->width = pCreateInfo->width;
2390 framebuffer->height = pCreateInfo->height;
2391 framebuffer->layers = pCreateInfo->layers;
2392 for (uint32_t i = 0; i < pCreateInfo->attachmentCount; i++) {
2393 VkImageView _iview = pCreateInfo->pAttachments[i];
2394 struct radv_image_view *iview = radv_image_view_from_handle(_iview);
2395 framebuffer->attachments[i].attachment = iview;
2396 if (iview->aspect_mask & VK_IMAGE_ASPECT_COLOR_BIT) {
2397 radv_initialise_color_surface(device, &framebuffer->attachments[i].cb, iview);
2398 } else if (iview->aspect_mask & (VK_IMAGE_ASPECT_DEPTH_BIT | VK_IMAGE_ASPECT_STENCIL_BIT)) {
2399 radv_initialise_ds_surface(device, &framebuffer->attachments[i].ds, iview);
2400 }
2401 framebuffer->width = MIN2(framebuffer->width, iview->extent.width);
2402 framebuffer->height = MIN2(framebuffer->height, iview->extent.height);
2403 framebuffer->layers = MIN2(framebuffer->layers, radv_surface_layer_count(iview));
2404 }
2405
2406 *pFramebuffer = radv_framebuffer_to_handle(framebuffer);
2407 return VK_SUCCESS;
2408 }
2409
2410 void radv_DestroyFramebuffer(
2411 VkDevice _device,
2412 VkFramebuffer _fb,
2413 const VkAllocationCallbacks* pAllocator)
2414 {
2415 RADV_FROM_HANDLE(radv_device, device, _device);
2416 RADV_FROM_HANDLE(radv_framebuffer, fb, _fb);
2417
2418 if (!fb)
2419 return;
2420 vk_free2(&device->alloc, pAllocator, fb);
2421 }
2422
2423 static unsigned radv_tex_wrap(VkSamplerAddressMode address_mode)
2424 {
2425 switch (address_mode) {
2426 case VK_SAMPLER_ADDRESS_MODE_REPEAT:
2427 return V_008F30_SQ_TEX_WRAP;
2428 case VK_SAMPLER_ADDRESS_MODE_MIRRORED_REPEAT:
2429 return V_008F30_SQ_TEX_MIRROR;
2430 case VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE:
2431 return V_008F30_SQ_TEX_CLAMP_LAST_TEXEL;
2432 case VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_BORDER:
2433 return V_008F30_SQ_TEX_CLAMP_BORDER;
2434 case VK_SAMPLER_ADDRESS_MODE_MIRROR_CLAMP_TO_EDGE:
2435 return V_008F30_SQ_TEX_MIRROR_ONCE_LAST_TEXEL;
2436 default:
2437 unreachable("illegal tex wrap mode");
2438 break;
2439 }
2440 }
2441
2442 static unsigned
2443 radv_tex_compare(VkCompareOp op)
2444 {
2445 switch (op) {
2446 case VK_COMPARE_OP_NEVER:
2447 return V_008F30_SQ_TEX_DEPTH_COMPARE_NEVER;
2448 case VK_COMPARE_OP_LESS:
2449 return V_008F30_SQ_TEX_DEPTH_COMPARE_LESS;
2450 case VK_COMPARE_OP_EQUAL:
2451 return V_008F30_SQ_TEX_DEPTH_COMPARE_EQUAL;
2452 case VK_COMPARE_OP_LESS_OR_EQUAL:
2453 return V_008F30_SQ_TEX_DEPTH_COMPARE_LESSEQUAL;
2454 case VK_COMPARE_OP_GREATER:
2455 return V_008F30_SQ_TEX_DEPTH_COMPARE_GREATER;
2456 case VK_COMPARE_OP_NOT_EQUAL:
2457 return V_008F30_SQ_TEX_DEPTH_COMPARE_NOTEQUAL;
2458 case VK_COMPARE_OP_GREATER_OR_EQUAL:
2459 return V_008F30_SQ_TEX_DEPTH_COMPARE_GREATEREQUAL;
2460 case VK_COMPARE_OP_ALWAYS:
2461 return V_008F30_SQ_TEX_DEPTH_COMPARE_ALWAYS;
2462 default:
2463 unreachable("illegal compare mode");
2464 break;
2465 }
2466 }
2467
2468 static unsigned
2469 radv_tex_filter(VkFilter filter, unsigned max_ansio)
2470 {
2471 switch (filter) {
2472 case VK_FILTER_NEAREST:
2473 return (max_ansio > 1 ? V_008F38_SQ_TEX_XY_FILTER_ANISO_POINT :
2474 V_008F38_SQ_TEX_XY_FILTER_POINT);
2475 case VK_FILTER_LINEAR:
2476 return (max_ansio > 1 ? V_008F38_SQ_TEX_XY_FILTER_ANISO_BILINEAR :
2477 V_008F38_SQ_TEX_XY_FILTER_BILINEAR);
2478 case VK_FILTER_CUBIC_IMG:
2479 default:
2480 fprintf(stderr, "illegal texture filter");
2481 return 0;
2482 }
2483 }
2484
2485 static unsigned
2486 radv_tex_mipfilter(VkSamplerMipmapMode mode)
2487 {
2488 switch (mode) {
2489 case VK_SAMPLER_MIPMAP_MODE_NEAREST:
2490 return V_008F38_SQ_TEX_Z_FILTER_POINT;
2491 case VK_SAMPLER_MIPMAP_MODE_LINEAR:
2492 return V_008F38_SQ_TEX_Z_FILTER_LINEAR;
2493 default:
2494 return V_008F38_SQ_TEX_Z_FILTER_NONE;
2495 }
2496 }
2497
2498 static unsigned
2499 radv_tex_bordercolor(VkBorderColor bcolor)
2500 {
2501 switch (bcolor) {
2502 case VK_BORDER_COLOR_FLOAT_TRANSPARENT_BLACK:
2503 case VK_BORDER_COLOR_INT_TRANSPARENT_BLACK:
2504 return V_008F3C_SQ_TEX_BORDER_COLOR_TRANS_BLACK;
2505 case VK_BORDER_COLOR_FLOAT_OPAQUE_BLACK:
2506 case VK_BORDER_COLOR_INT_OPAQUE_BLACK:
2507 return V_008F3C_SQ_TEX_BORDER_COLOR_OPAQUE_BLACK;
2508 case VK_BORDER_COLOR_FLOAT_OPAQUE_WHITE:
2509 case VK_BORDER_COLOR_INT_OPAQUE_WHITE:
2510 return V_008F3C_SQ_TEX_BORDER_COLOR_OPAQUE_WHITE;
2511 default:
2512 break;
2513 }
2514 return 0;
2515 }
2516
2517 static unsigned
2518 radv_tex_aniso_filter(unsigned filter)
2519 {
2520 if (filter < 2)
2521 return 0;
2522 if (filter < 4)
2523 return 1;
2524 if (filter < 8)
2525 return 2;
2526 if (filter < 16)
2527 return 3;
2528 return 4;
2529 }
2530
2531 static void
2532 radv_init_sampler(struct radv_device *device,
2533 struct radv_sampler *sampler,
2534 const VkSamplerCreateInfo *pCreateInfo)
2535 {
2536 uint32_t max_aniso = pCreateInfo->anisotropyEnable && pCreateInfo->maxAnisotropy > 1.0 ?
2537 (uint32_t) pCreateInfo->maxAnisotropy : 0;
2538 uint32_t max_aniso_ratio = radv_tex_aniso_filter(max_aniso);
2539 bool is_vi = (device->physical_device->rad_info.chip_class >= VI);
2540
2541 sampler->state[0] = (S_008F30_CLAMP_X(radv_tex_wrap(pCreateInfo->addressModeU)) |
2542 S_008F30_CLAMP_Y(radv_tex_wrap(pCreateInfo->addressModeV)) |
2543 S_008F30_CLAMP_Z(radv_tex_wrap(pCreateInfo->addressModeW)) |
2544 S_008F30_MAX_ANISO_RATIO(max_aniso_ratio) |
2545 S_008F30_DEPTH_COMPARE_FUNC(radv_tex_compare(pCreateInfo->compareOp)) |
2546 S_008F30_FORCE_UNNORMALIZED(pCreateInfo->unnormalizedCoordinates ? 1 : 0) |
2547 S_008F30_ANISO_THRESHOLD(max_aniso_ratio >> 1) |
2548 S_008F30_ANISO_BIAS(max_aniso_ratio) |
2549 S_008F30_DISABLE_CUBE_WRAP(0) |
2550 S_008F30_COMPAT_MODE(is_vi));
2551 sampler->state[1] = (S_008F34_MIN_LOD(S_FIXED(CLAMP(pCreateInfo->minLod, 0, 15), 8)) |
2552 S_008F34_MAX_LOD(S_FIXED(CLAMP(pCreateInfo->maxLod, 0, 15), 8)) |
2553 S_008F34_PERF_MIP(max_aniso_ratio ? max_aniso_ratio + 6 : 0));
2554 sampler->state[2] = (S_008F38_LOD_BIAS(S_FIXED(CLAMP(pCreateInfo->mipLodBias, -16, 16), 8)) |
2555 S_008F38_XY_MAG_FILTER(radv_tex_filter(pCreateInfo->magFilter, max_aniso)) |
2556 S_008F38_XY_MIN_FILTER(radv_tex_filter(pCreateInfo->minFilter, max_aniso)) |
2557 S_008F38_MIP_FILTER(radv_tex_mipfilter(pCreateInfo->mipmapMode)) |
2558 S_008F38_MIP_POINT_PRECLAMP(1) |
2559 S_008F38_DISABLE_LSB_CEIL(1) |
2560 S_008F38_FILTER_PREC_FIX(1) |
2561 S_008F38_ANISO_OVERRIDE(is_vi));
2562 sampler->state[3] = (S_008F3C_BORDER_COLOR_PTR(0) |
2563 S_008F3C_BORDER_COLOR_TYPE(radv_tex_bordercolor(pCreateInfo->borderColor)));
2564 }
2565
2566 VkResult radv_CreateSampler(
2567 VkDevice _device,
2568 const VkSamplerCreateInfo* pCreateInfo,
2569 const VkAllocationCallbacks* pAllocator,
2570 VkSampler* pSampler)
2571 {
2572 RADV_FROM_HANDLE(radv_device, device, _device);
2573 struct radv_sampler *sampler;
2574
2575 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO);
2576
2577 sampler = vk_alloc2(&device->alloc, pAllocator, sizeof(*sampler), 8,
2578 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
2579 if (!sampler)
2580 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
2581
2582 radv_init_sampler(device, sampler, pCreateInfo);
2583 *pSampler = radv_sampler_to_handle(sampler);
2584
2585 return VK_SUCCESS;
2586 }
2587
2588 void radv_DestroySampler(
2589 VkDevice _device,
2590 VkSampler _sampler,
2591 const VkAllocationCallbacks* pAllocator)
2592 {
2593 RADV_FROM_HANDLE(radv_device, device, _device);
2594 RADV_FROM_HANDLE(radv_sampler, sampler, _sampler);
2595
2596 if (!sampler)
2597 return;
2598 vk_free2(&device->alloc, pAllocator, sampler);
2599 }
2600
2601
2602 /* vk_icd.h does not declare this function, so we declare it here to
2603 * suppress Wmissing-prototypes.
2604 */
2605 PUBLIC VKAPI_ATTR VkResult VKAPI_CALL
2606 vk_icdNegotiateLoaderICDInterfaceVersion(uint32_t *pSupportedVersion);
2607
2608 PUBLIC VKAPI_ATTR VkResult VKAPI_CALL
2609 vk_icdNegotiateLoaderICDInterfaceVersion(uint32_t *pSupportedVersion)
2610 {
2611 /* For the full details on loader interface versioning, see
2612 * <https://github.com/KhronosGroup/Vulkan-LoaderAndValidationLayers/blob/master/loader/LoaderAndLayerInterface.md>.
2613 * What follows is a condensed summary, to help you navigate the large and
2614 * confusing official doc.
2615 *
2616 * - Loader interface v0 is incompatible with later versions. We don't
2617 * support it.
2618 *
2619 * - In loader interface v1:
2620 * - The first ICD entrypoint called by the loader is
2621 * vk_icdGetInstanceProcAddr(). The ICD must statically expose this
2622 * entrypoint.
2623 * - The ICD must statically expose no other Vulkan symbol unless it is
2624 * linked with -Bsymbolic.
2625 * - Each dispatchable Vulkan handle created by the ICD must be
2626 * a pointer to a struct whose first member is VK_LOADER_DATA. The
2627 * ICD must initialize VK_LOADER_DATA.loadMagic to ICD_LOADER_MAGIC.
2628 * - The loader implements vkCreate{PLATFORM}SurfaceKHR() and
2629 * vkDestroySurfaceKHR(). The ICD must be capable of working with
2630 * such loader-managed surfaces.
2631 *
2632 * - Loader interface v2 differs from v1 in:
2633 * - The first ICD entrypoint called by the loader is
2634 * vk_icdNegotiateLoaderICDInterfaceVersion(). The ICD must
2635 * statically expose this entrypoint.
2636 *
2637 * - Loader interface v3 differs from v2 in:
2638 * - The ICD must implement vkCreate{PLATFORM}SurfaceKHR(),
2639 * vkDestroySurfaceKHR(), and other API which uses VKSurfaceKHR,
2640 * because the loader no longer does so.
2641 */
2642 *pSupportedVersion = MIN2(*pSupportedVersion, 3u);
2643 return VK_SUCCESS;
2644 }