2 * Copyright © 2016 Red Hat.
3 * Copyright © 2016 Bas Nieuwenhuizen
5 * based in part on anv driver which is:
6 * Copyright © 2015 Intel Corporation
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:
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
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
34 #include "radv_private.h"
36 #include "util/strtod.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"
46 #include "util/debug.h"
47 struct radv_dispatch_table dtable
;
50 radv_get_function_timestamp(void *ptr
, uint32_t* timestamp
)
54 if (!dladdr(ptr
, &info
) || !info
.dli_fname
) {
57 if (stat(info
.dli_fname
, &st
)) {
60 *timestamp
= st
.st_mtim
.tv_sec
;
65 radv_device_get_cache_uuid(enum radeon_family family
, void *uuid
)
67 uint32_t mesa_timestamp
, llvm_timestamp
;
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
))
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");
81 static const VkExtensionProperties instance_extensions
[] = {
83 .extensionName
= VK_KHR_SURFACE_EXTENSION_NAME
,
86 #ifdef VK_USE_PLATFORM_XCB_KHR
88 .extensionName
= VK_KHR_XCB_SURFACE_EXTENSION_NAME
,
92 #ifdef VK_USE_PLATFORM_XLIB_KHR
94 .extensionName
= VK_KHR_XLIB_SURFACE_EXTENSION_NAME
,
98 #ifdef VK_USE_PLATFORM_WAYLAND_KHR
100 .extensionName
= VK_KHR_WAYLAND_SURFACE_EXTENSION_NAME
,
106 static const VkExtensionProperties common_device_extensions
[] = {
108 .extensionName
= VK_KHR_SAMPLER_MIRROR_CLAMP_TO_EDGE_EXTENSION_NAME
,
112 .extensionName
= VK_KHR_SWAPCHAIN_EXTENSION_NAME
,
116 .extensionName
= VK_AMD_DRAW_INDIRECT_COUNT_EXTENSION_NAME
,
120 .extensionName
= VK_KHR_GET_PHYSICAL_DEVICE_PROPERTIES_2_EXTENSION_NAME
,
126 radv_extensions_register(struct radv_instance
*instance
,
127 struct radv_extensions
*extensions
,
128 const VkExtensionProperties
*new_ext
,
132 VkExtensionProperties
*new_ptr
;
134 assert(new_ext
&& num_ext
> 0);
137 return VK_ERROR_INITIALIZATION_FAILED
;
139 new_size
= (extensions
->num_ext
+ num_ext
) * sizeof(VkExtensionProperties
);
140 new_ptr
= vk_realloc(&instance
->alloc
, extensions
->ext_array
,
141 new_size
, 8, VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE
);
143 /* Old array continues to be valid, update nothing */
145 return VK_ERROR_OUT_OF_HOST_MEMORY
;
147 memcpy(&new_ptr
[extensions
->num_ext
], new_ext
,
148 num_ext
* sizeof(VkExtensionProperties
));
149 extensions
->ext_array
= new_ptr
;
150 extensions
->num_ext
+= num_ext
;
156 radv_extensions_finish(struct radv_instance
*instance
,
157 struct radv_extensions
*extensions
)
162 radv_loge("Attemted to free invalid extension struct\n");
164 if (extensions
->ext_array
)
165 vk_free(&instance
->alloc
, extensions
->ext_array
);
169 is_extension_enabled(const VkExtensionProperties
*extensions
,
173 assert(extensions
&& name
);
175 for (uint32_t i
= 0; i
< num_ext
; i
++) {
176 if (strcmp(name
, extensions
[i
].extensionName
) == 0)
184 radv_physical_device_init(struct radv_physical_device
*device
,
185 struct radv_instance
*instance
,
189 drmVersionPtr version
;
192 fd
= open(path
, O_RDWR
| O_CLOEXEC
);
194 return VK_ERROR_INCOMPATIBLE_DRIVER
;
196 version
= drmGetVersion(fd
);
199 return vk_errorf(VK_ERROR_INCOMPATIBLE_DRIVER
,
200 "failed to get version %s: %m", path
);
203 if (strcmp(version
->name
, "amdgpu")) {
204 drmFreeVersion(version
);
206 return VK_ERROR_INCOMPATIBLE_DRIVER
;
208 drmFreeVersion(version
);
210 device
->_loader_data
.loaderMagic
= ICD_LOADER_MAGIC
;
211 device
->instance
= instance
;
212 assert(strlen(path
) < ARRAY_SIZE(device
->path
));
213 strncpy(device
->path
, path
, ARRAY_SIZE(device
->path
));
215 device
->ws
= radv_amdgpu_winsys_create(fd
);
217 result
= VK_ERROR_INCOMPATIBLE_DRIVER
;
220 device
->ws
->query_info(device
->ws
, &device
->rad_info
);
221 result
= radv_init_wsi(device
);
222 if (result
!= VK_SUCCESS
) {
223 device
->ws
->destroy(device
->ws
);
227 if (radv_device_get_cache_uuid(device
->rad_info
.family
, device
->uuid
)) {
228 radv_finish_wsi(device
);
229 device
->ws
->destroy(device
->ws
);
230 result
= vk_errorf(VK_ERROR_INITIALIZATION_FAILED
,
231 "cannot generate UUID");
235 result
= radv_extensions_register(instance
,
237 common_device_extensions
,
238 ARRAY_SIZE(common_device_extensions
));
239 if (result
!= VK_SUCCESS
)
242 fprintf(stderr
, "WARNING: radv is not a conformant vulkan implementation, testing use only.\n");
243 device
->name
= device
->rad_info
.name
;
253 radv_physical_device_finish(struct radv_physical_device
*device
)
255 radv_extensions_finish(device
->instance
, &device
->extensions
);
256 radv_finish_wsi(device
);
257 device
->ws
->destroy(device
->ws
);
262 default_alloc_func(void *pUserData
, size_t size
, size_t align
,
263 VkSystemAllocationScope allocationScope
)
269 default_realloc_func(void *pUserData
, void *pOriginal
, size_t size
,
270 size_t align
, VkSystemAllocationScope allocationScope
)
272 return realloc(pOriginal
, size
);
276 default_free_func(void *pUserData
, void *pMemory
)
281 static const VkAllocationCallbacks default_alloc
= {
283 .pfnAllocation
= default_alloc_func
,
284 .pfnReallocation
= default_realloc_func
,
285 .pfnFree
= default_free_func
,
288 static const struct debug_control radv_debug_options
[] = {
289 {"fastclears", RADV_DEBUG_FAST_CLEARS
},
290 {"nodcc", RADV_DEBUG_NO_DCC
},
291 {"shaders", RADV_DEBUG_DUMP_SHADERS
},
292 {"nocache", RADV_DEBUG_NO_CACHE
},
293 {"shaderstats", RADV_DEBUG_DUMP_SHADER_STATS
},
294 {"nohiz", RADV_DEBUG_NO_HIZ
},
295 {"nocompute", RADV_DEBUG_NO_COMPUTE_QUEUE
},
296 {"unsafemath", RADV_DEBUG_UNSAFE_MATH
},
300 VkResult
radv_CreateInstance(
301 const VkInstanceCreateInfo
* pCreateInfo
,
302 const VkAllocationCallbacks
* pAllocator
,
303 VkInstance
* pInstance
)
305 struct radv_instance
*instance
;
307 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO
);
309 uint32_t client_version
;
310 if (pCreateInfo
->pApplicationInfo
&&
311 pCreateInfo
->pApplicationInfo
->apiVersion
!= 0) {
312 client_version
= pCreateInfo
->pApplicationInfo
->apiVersion
;
314 client_version
= VK_MAKE_VERSION(1, 0, 0);
317 if (VK_MAKE_VERSION(1, 0, 0) > client_version
||
318 client_version
> VK_MAKE_VERSION(1, 0, 0xfff)) {
319 return vk_errorf(VK_ERROR_INCOMPATIBLE_DRIVER
,
320 "Client requested version %d.%d.%d",
321 VK_VERSION_MAJOR(client_version
),
322 VK_VERSION_MINOR(client_version
),
323 VK_VERSION_PATCH(client_version
));
326 for (uint32_t i
= 0; i
< pCreateInfo
->enabledExtensionCount
; i
++) {
327 if (!is_extension_enabled(instance_extensions
,
328 ARRAY_SIZE(instance_extensions
),
329 pCreateInfo
->ppEnabledExtensionNames
[i
]))
330 return vk_error(VK_ERROR_EXTENSION_NOT_PRESENT
);
333 instance
= vk_alloc2(&default_alloc
, pAllocator
, sizeof(*instance
), 8,
334 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE
);
336 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
338 memset(instance
, 0, sizeof(*instance
));
340 instance
->_loader_data
.loaderMagic
= ICD_LOADER_MAGIC
;
343 instance
->alloc
= *pAllocator
;
345 instance
->alloc
= default_alloc
;
347 instance
->apiVersion
= client_version
;
348 instance
->physicalDeviceCount
= -1;
352 VG(VALGRIND_CREATE_MEMPOOL(instance
, 0, false));
354 instance
->debug_flags
= parse_debug_string(getenv("RADV_DEBUG"),
357 *pInstance
= radv_instance_to_handle(instance
);
362 void radv_DestroyInstance(
363 VkInstance _instance
,
364 const VkAllocationCallbacks
* pAllocator
)
366 RADV_FROM_HANDLE(radv_instance
, instance
, _instance
);
368 for (int i
= 0; i
< instance
->physicalDeviceCount
; ++i
) {
369 radv_physical_device_finish(instance
->physicalDevices
+ i
);
372 VG(VALGRIND_DESTROY_MEMPOOL(instance
));
376 vk_free(&instance
->alloc
, instance
);
379 VkResult
radv_EnumeratePhysicalDevices(
380 VkInstance _instance
,
381 uint32_t* pPhysicalDeviceCount
,
382 VkPhysicalDevice
* pPhysicalDevices
)
384 RADV_FROM_HANDLE(radv_instance
, instance
, _instance
);
387 if (instance
->physicalDeviceCount
< 0) {
389 instance
->physicalDeviceCount
= 0;
390 for (unsigned i
= 0; i
< RADV_MAX_DRM_DEVICES
; i
++) {
391 snprintf(path
, sizeof(path
), "/dev/dri/renderD%d", 128 + i
);
392 result
= radv_physical_device_init(instance
->physicalDevices
+
393 instance
->physicalDeviceCount
,
395 if (result
== VK_SUCCESS
)
396 ++instance
->physicalDeviceCount
;
397 else if (result
!= VK_ERROR_INCOMPATIBLE_DRIVER
)
402 if (!pPhysicalDevices
) {
403 *pPhysicalDeviceCount
= instance
->physicalDeviceCount
;
405 *pPhysicalDeviceCount
= MIN2(*pPhysicalDeviceCount
, instance
->physicalDeviceCount
);
406 for (unsigned i
= 0; i
< *pPhysicalDeviceCount
; ++i
)
407 pPhysicalDevices
[i
] = radv_physical_device_to_handle(instance
->physicalDevices
+ i
);
410 return *pPhysicalDeviceCount
< instance
->physicalDeviceCount
? VK_INCOMPLETE
414 void radv_GetPhysicalDeviceFeatures(
415 VkPhysicalDevice physicalDevice
,
416 VkPhysicalDeviceFeatures
* pFeatures
)
418 // RADV_FROM_HANDLE(radv_physical_device, pdevice, physicalDevice);
420 memset(pFeatures
, 0, sizeof(*pFeatures
));
422 *pFeatures
= (VkPhysicalDeviceFeatures
) {
423 .robustBufferAccess
= true,
424 .fullDrawIndexUint32
= true,
425 .imageCubeArray
= true,
426 .independentBlend
= true,
427 .geometryShader
= false,
428 .tessellationShader
= false,
429 .sampleRateShading
= false,
430 .dualSrcBlend
= true,
432 .multiDrawIndirect
= true,
433 .drawIndirectFirstInstance
= true,
435 .depthBiasClamp
= true,
436 .fillModeNonSolid
= true,
441 .multiViewport
= false,
442 .samplerAnisotropy
= true,
443 .textureCompressionETC2
= false,
444 .textureCompressionASTC_LDR
= false,
445 .textureCompressionBC
= true,
446 .occlusionQueryPrecise
= true,
447 .pipelineStatisticsQuery
= false,
448 .vertexPipelineStoresAndAtomics
= true,
449 .fragmentStoresAndAtomics
= true,
450 .shaderTessellationAndGeometryPointSize
= true,
451 .shaderImageGatherExtended
= true,
452 .shaderStorageImageExtendedFormats
= true,
453 .shaderStorageImageMultisample
= false,
454 .shaderUniformBufferArrayDynamicIndexing
= true,
455 .shaderSampledImageArrayDynamicIndexing
= true,
456 .shaderStorageBufferArrayDynamicIndexing
= true,
457 .shaderStorageImageArrayDynamicIndexing
= true,
458 .shaderStorageImageReadWithoutFormat
= false,
459 .shaderStorageImageWriteWithoutFormat
= false,
460 .shaderClipDistance
= true,
461 .shaderCullDistance
= true,
462 .shaderFloat64
= false,
463 .shaderInt64
= false,
464 .shaderInt16
= false,
466 .variableMultisampleRate
= false,
467 .inheritedQueries
= false,
471 void radv_GetPhysicalDeviceFeatures2KHR(
472 VkPhysicalDevice physicalDevice
,
473 VkPhysicalDeviceFeatures2KHR
*pFeatures
)
475 return radv_GetPhysicalDeviceFeatures(physicalDevice
, &pFeatures
->features
);
478 void radv_GetPhysicalDeviceProperties(
479 VkPhysicalDevice physicalDevice
,
480 VkPhysicalDeviceProperties
* pProperties
)
482 RADV_FROM_HANDLE(radv_physical_device
, pdevice
, physicalDevice
);
483 VkSampleCountFlags sample_counts
= 0xf;
484 VkPhysicalDeviceLimits limits
= {
485 .maxImageDimension1D
= (1 << 14),
486 .maxImageDimension2D
= (1 << 14),
487 .maxImageDimension3D
= (1 << 11),
488 .maxImageDimensionCube
= (1 << 14),
489 .maxImageArrayLayers
= (1 << 11),
490 .maxTexelBufferElements
= 128 * 1024 * 1024,
491 .maxUniformBufferRange
= UINT32_MAX
,
492 .maxStorageBufferRange
= UINT32_MAX
,
493 .maxPushConstantsSize
= MAX_PUSH_CONSTANTS_SIZE
,
494 .maxMemoryAllocationCount
= UINT32_MAX
,
495 .maxSamplerAllocationCount
= 64 * 1024,
496 .bufferImageGranularity
= 64, /* A cache line */
497 .sparseAddressSpaceSize
= 0,
498 .maxBoundDescriptorSets
= MAX_SETS
,
499 .maxPerStageDescriptorSamplers
= 64,
500 .maxPerStageDescriptorUniformBuffers
= 64,
501 .maxPerStageDescriptorStorageBuffers
= 64,
502 .maxPerStageDescriptorSampledImages
= 64,
503 .maxPerStageDescriptorStorageImages
= 64,
504 .maxPerStageDescriptorInputAttachments
= 64,
505 .maxPerStageResources
= 128,
506 .maxDescriptorSetSamplers
= 256,
507 .maxDescriptorSetUniformBuffers
= 256,
508 .maxDescriptorSetUniformBuffersDynamic
= 256,
509 .maxDescriptorSetStorageBuffers
= 256,
510 .maxDescriptorSetStorageBuffersDynamic
= 256,
511 .maxDescriptorSetSampledImages
= 256,
512 .maxDescriptorSetStorageImages
= 256,
513 .maxDescriptorSetInputAttachments
= 256,
514 .maxVertexInputAttributes
= 32,
515 .maxVertexInputBindings
= 32,
516 .maxVertexInputAttributeOffset
= 2047,
517 .maxVertexInputBindingStride
= 2048,
518 .maxVertexOutputComponents
= 128,
519 .maxTessellationGenerationLevel
= 0,
520 .maxTessellationPatchSize
= 0,
521 .maxTessellationControlPerVertexInputComponents
= 0,
522 .maxTessellationControlPerVertexOutputComponents
= 0,
523 .maxTessellationControlPerPatchOutputComponents
= 0,
524 .maxTessellationControlTotalOutputComponents
= 0,
525 .maxTessellationEvaluationInputComponents
= 0,
526 .maxTessellationEvaluationOutputComponents
= 0,
527 .maxGeometryShaderInvocations
= 32,
528 .maxGeometryInputComponents
= 64,
529 .maxGeometryOutputComponents
= 128,
530 .maxGeometryOutputVertices
= 256,
531 .maxGeometryTotalOutputComponents
= 1024,
532 .maxFragmentInputComponents
= 128,
533 .maxFragmentOutputAttachments
= 8,
534 .maxFragmentDualSrcAttachments
= 1,
535 .maxFragmentCombinedOutputResources
= 8,
536 .maxComputeSharedMemorySize
= 32768,
537 .maxComputeWorkGroupCount
= { 65535, 65535, 65535 },
538 .maxComputeWorkGroupInvocations
= 2048,
539 .maxComputeWorkGroupSize
= {
544 .subPixelPrecisionBits
= 4 /* FIXME */,
545 .subTexelPrecisionBits
= 4 /* FIXME */,
546 .mipmapPrecisionBits
= 4 /* FIXME */,
547 .maxDrawIndexedIndexValue
= UINT32_MAX
,
548 .maxDrawIndirectCount
= UINT32_MAX
,
549 .maxSamplerLodBias
= 16,
550 .maxSamplerAnisotropy
= 16,
551 .maxViewports
= MAX_VIEWPORTS
,
552 .maxViewportDimensions
= { (1 << 14), (1 << 14) },
553 .viewportBoundsRange
= { INT16_MIN
, INT16_MAX
},
554 .viewportSubPixelBits
= 13, /* We take a float? */
555 .minMemoryMapAlignment
= 4096, /* A page */
556 .minTexelBufferOffsetAlignment
= 1,
557 .minUniformBufferOffsetAlignment
= 4,
558 .minStorageBufferOffsetAlignment
= 4,
559 .minTexelOffset
= -32,
560 .maxTexelOffset
= 31,
561 .minTexelGatherOffset
= -32,
562 .maxTexelGatherOffset
= 31,
563 .minInterpolationOffset
= -2,
564 .maxInterpolationOffset
= 2,
565 .subPixelInterpolationOffsetBits
= 8,
566 .maxFramebufferWidth
= (1 << 14),
567 .maxFramebufferHeight
= (1 << 14),
568 .maxFramebufferLayers
= (1 << 10),
569 .framebufferColorSampleCounts
= sample_counts
,
570 .framebufferDepthSampleCounts
= sample_counts
,
571 .framebufferStencilSampleCounts
= sample_counts
,
572 .framebufferNoAttachmentsSampleCounts
= sample_counts
,
573 .maxColorAttachments
= MAX_RTS
,
574 .sampledImageColorSampleCounts
= sample_counts
,
575 .sampledImageIntegerSampleCounts
= VK_SAMPLE_COUNT_1_BIT
,
576 .sampledImageDepthSampleCounts
= sample_counts
,
577 .sampledImageStencilSampleCounts
= sample_counts
,
578 .storageImageSampleCounts
= VK_SAMPLE_COUNT_1_BIT
,
579 .maxSampleMaskWords
= 1,
580 .timestampComputeAndGraphics
= false,
581 .timestampPeriod
= 100000.0 / pdevice
->rad_info
.clock_crystal_freq
,
582 .maxClipDistances
= 8,
583 .maxCullDistances
= 8,
584 .maxCombinedClipAndCullDistances
= 8,
585 .discreteQueuePriorities
= 1,
586 .pointSizeRange
= { 0.125, 255.875 },
587 .lineWidthRange
= { 0.0, 7.9921875 },
588 .pointSizeGranularity
= (1.0 / 8.0),
589 .lineWidthGranularity
= (1.0 / 128.0),
590 .strictLines
= false, /* FINISHME */
591 .standardSampleLocations
= true,
592 .optimalBufferCopyOffsetAlignment
= 128,
593 .optimalBufferCopyRowPitchAlignment
= 128,
594 .nonCoherentAtomSize
= 64,
597 *pProperties
= (VkPhysicalDeviceProperties
) {
598 .apiVersion
= VK_MAKE_VERSION(1, 0, 5),
601 .deviceID
= pdevice
->rad_info
.pci_id
,
602 .deviceType
= VK_PHYSICAL_DEVICE_TYPE_DISCRETE_GPU
,
604 .sparseProperties
= {0}, /* Broadwell doesn't do sparse. */
607 strcpy(pProperties
->deviceName
, pdevice
->name
);
608 memcpy(pProperties
->pipelineCacheUUID
, pdevice
->uuid
, VK_UUID_SIZE
);
611 void radv_GetPhysicalDeviceProperties2KHR(
612 VkPhysicalDevice physicalDevice
,
613 VkPhysicalDeviceProperties2KHR
*pProperties
)
615 return radv_GetPhysicalDeviceProperties(physicalDevice
, &pProperties
->properties
);
618 void radv_GetPhysicalDeviceQueueFamilyProperties(
619 VkPhysicalDevice physicalDevice
,
621 VkQueueFamilyProperties
* pQueueFamilyProperties
)
623 RADV_FROM_HANDLE(radv_physical_device
, pdevice
, physicalDevice
);
624 int num_queue_families
= 1;
626 if (pdevice
->rad_info
.compute_rings
> 0 &&
627 pdevice
->rad_info
.chip_class
>= CIK
&&
628 !(pdevice
->instance
->debug_flags
& RADV_DEBUG_NO_COMPUTE_QUEUE
))
629 num_queue_families
++;
631 if (pQueueFamilyProperties
== NULL
) {
632 *pCount
= num_queue_families
;
641 pQueueFamilyProperties
[idx
] = (VkQueueFamilyProperties
) {
642 .queueFlags
= VK_QUEUE_GRAPHICS_BIT
|
643 VK_QUEUE_COMPUTE_BIT
|
644 VK_QUEUE_TRANSFER_BIT
,
646 .timestampValidBits
= 64,
647 .minImageTransferGranularity
= (VkExtent3D
) { 1, 1, 1 },
652 if (pdevice
->rad_info
.compute_rings
> 0 &&
653 pdevice
->rad_info
.chip_class
>= CIK
&&
654 !(pdevice
->instance
->debug_flags
& RADV_DEBUG_NO_COMPUTE_QUEUE
)) {
656 pQueueFamilyProperties
[idx
] = (VkQueueFamilyProperties
) {
657 .queueFlags
= VK_QUEUE_COMPUTE_BIT
| VK_QUEUE_TRANSFER_BIT
,
658 .queueCount
= pdevice
->rad_info
.compute_rings
,
659 .timestampValidBits
= 64,
660 .minImageTransferGranularity
= (VkExtent3D
) { 1, 1, 1 },
668 void radv_GetPhysicalDeviceQueueFamilyProperties2KHR(
669 VkPhysicalDevice physicalDevice
,
671 VkQueueFamilyProperties2KHR
*pQueueFamilyProperties
)
673 return radv_GetPhysicalDeviceQueueFamilyProperties(physicalDevice
,
675 &pQueueFamilyProperties
->queueFamilyProperties
);
678 void radv_GetPhysicalDeviceMemoryProperties(
679 VkPhysicalDevice physicalDevice
,
680 VkPhysicalDeviceMemoryProperties
*pMemoryProperties
)
682 RADV_FROM_HANDLE(radv_physical_device
, physical_device
, physicalDevice
);
684 STATIC_ASSERT(RADV_MEM_TYPE_COUNT
<= VK_MAX_MEMORY_TYPES
);
686 pMemoryProperties
->memoryTypeCount
= RADV_MEM_TYPE_COUNT
;
687 pMemoryProperties
->memoryTypes
[RADV_MEM_TYPE_VRAM
] = (VkMemoryType
) {
688 .propertyFlags
= VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT
,
689 .heapIndex
= RADV_MEM_HEAP_VRAM
,
691 pMemoryProperties
->memoryTypes
[RADV_MEM_TYPE_GTT_WRITE_COMBINE
] = (VkMemoryType
) {
692 .propertyFlags
= VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT
|
693 VK_MEMORY_PROPERTY_HOST_COHERENT_BIT
,
694 .heapIndex
= RADV_MEM_HEAP_GTT
,
696 pMemoryProperties
->memoryTypes
[RADV_MEM_TYPE_VRAM_CPU_ACCESS
] = (VkMemoryType
) {
697 .propertyFlags
= VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT
|
698 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT
|
699 VK_MEMORY_PROPERTY_HOST_COHERENT_BIT
,
700 .heapIndex
= RADV_MEM_HEAP_VRAM_CPU_ACCESS
,
702 pMemoryProperties
->memoryTypes
[RADV_MEM_TYPE_GTT_CACHED
] = (VkMemoryType
) {
703 .propertyFlags
= VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT
|
704 VK_MEMORY_PROPERTY_HOST_COHERENT_BIT
|
705 VK_MEMORY_PROPERTY_HOST_CACHED_BIT
,
706 .heapIndex
= RADV_MEM_HEAP_GTT
,
709 STATIC_ASSERT(RADV_MEM_HEAP_COUNT
<= VK_MAX_MEMORY_HEAPS
);
711 pMemoryProperties
->memoryHeapCount
= RADV_MEM_HEAP_COUNT
;
712 pMemoryProperties
->memoryHeaps
[RADV_MEM_HEAP_VRAM
] = (VkMemoryHeap
) {
713 .size
= physical_device
->rad_info
.vram_size
-
714 physical_device
->rad_info
.visible_vram_size
,
715 .flags
= VK_MEMORY_HEAP_DEVICE_LOCAL_BIT
,
717 pMemoryProperties
->memoryHeaps
[RADV_MEM_HEAP_VRAM_CPU_ACCESS
] = (VkMemoryHeap
) {
718 .size
= physical_device
->rad_info
.visible_vram_size
,
719 .flags
= VK_MEMORY_HEAP_DEVICE_LOCAL_BIT
,
721 pMemoryProperties
->memoryHeaps
[RADV_MEM_HEAP_GTT
] = (VkMemoryHeap
) {
722 .size
= physical_device
->rad_info
.gart_size
,
727 void radv_GetPhysicalDeviceMemoryProperties2KHR(
728 VkPhysicalDevice physicalDevice
,
729 VkPhysicalDeviceMemoryProperties2KHR
*pMemoryProperties
)
731 return radv_GetPhysicalDeviceMemoryProperties(physicalDevice
,
732 &pMemoryProperties
->memoryProperties
);
736 radv_queue_init(struct radv_device
*device
, struct radv_queue
*queue
,
737 int queue_family_index
, int idx
)
739 queue
->_loader_data
.loaderMagic
= ICD_LOADER_MAGIC
;
740 queue
->device
= device
;
741 queue
->queue_family_index
= queue_family_index
;
742 queue
->queue_idx
= idx
;
744 queue
->hw_ctx
= device
->ws
->ctx_create(device
->ws
);
746 return VK_ERROR_OUT_OF_HOST_MEMORY
;
752 radv_queue_finish(struct radv_queue
*queue
)
755 queue
->device
->ws
->ctx_destroy(queue
->hw_ctx
);
757 if (queue
->preamble_cs
)
758 queue
->device
->ws
->cs_destroy(queue
->preamble_cs
);
759 if (queue
->descriptor_bo
)
760 queue
->device
->ws
->buffer_destroy(queue
->descriptor_bo
);
761 if (queue
->scratch_bo
)
762 queue
->device
->ws
->buffer_destroy(queue
->scratch_bo
);
763 if (queue
->compute_scratch_bo
)
764 queue
->device
->ws
->buffer_destroy(queue
->compute_scratch_bo
);
767 VkResult
radv_CreateDevice(
768 VkPhysicalDevice physicalDevice
,
769 const VkDeviceCreateInfo
* pCreateInfo
,
770 const VkAllocationCallbacks
* pAllocator
,
773 RADV_FROM_HANDLE(radv_physical_device
, physical_device
, physicalDevice
);
775 struct radv_device
*device
;
777 for (uint32_t i
= 0; i
< pCreateInfo
->enabledExtensionCount
; i
++) {
778 if (!is_extension_enabled(physical_device
->extensions
.ext_array
,
779 physical_device
->extensions
.num_ext
,
780 pCreateInfo
->ppEnabledExtensionNames
[i
]))
781 return vk_error(VK_ERROR_EXTENSION_NOT_PRESENT
);
784 device
= vk_alloc2(&physical_device
->instance
->alloc
, pAllocator
,
786 VK_SYSTEM_ALLOCATION_SCOPE_DEVICE
);
788 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
790 memset(device
, 0, sizeof(*device
));
792 device
->_loader_data
.loaderMagic
= ICD_LOADER_MAGIC
;
793 device
->instance
= physical_device
->instance
;
794 device
->physical_device
= physical_device
;
796 device
->debug_flags
= device
->instance
->debug_flags
;
798 device
->ws
= physical_device
->ws
;
800 device
->alloc
= *pAllocator
;
802 device
->alloc
= physical_device
->instance
->alloc
;
804 for (unsigned i
= 0; i
< pCreateInfo
->queueCreateInfoCount
; i
++) {
805 const VkDeviceQueueCreateInfo
*queue_create
= &pCreateInfo
->pQueueCreateInfos
[i
];
806 uint32_t qfi
= queue_create
->queueFamilyIndex
;
808 device
->queues
[qfi
] = vk_alloc(&device
->alloc
,
809 queue_create
->queueCount
* sizeof(struct radv_queue
), 8, VK_SYSTEM_ALLOCATION_SCOPE_DEVICE
);
810 if (!device
->queues
[qfi
]) {
811 result
= VK_ERROR_OUT_OF_HOST_MEMORY
;
815 memset(device
->queues
[qfi
], 0, queue_create
->queueCount
* sizeof(struct radv_queue
));
817 device
->queue_count
[qfi
] = queue_create
->queueCount
;
819 for (unsigned q
= 0; q
< queue_create
->queueCount
; q
++) {
820 result
= radv_queue_init(device
, &device
->queues
[qfi
][q
], qfi
, q
);
821 if (result
!= VK_SUCCESS
)
826 #if HAVE_LLVM < 0x0400
827 device
->llvm_supports_spill
= false;
829 device
->llvm_supports_spill
= true;
832 /* The maximum number of scratch waves. Scratch space isn't divided
833 * evenly between CUs. The number is only a function of the number of CUs.
834 * We can decrease the constant to decrease the scratch buffer size.
836 * sctx->scratch_waves must be >= the maximum posible size of
837 * 1 threadgroup, so that the hw doesn't hang from being unable
840 * The recommended value is 4 per CU at most. Higher numbers don't
841 * bring much benefit, but they still occupy chip resources (think
842 * async compute). I've seen ~2% performance difference between 4 and 32.
844 uint32_t max_threads_per_block
= 2048;
845 device
->scratch_waves
= MAX2(32 * physical_device
->rad_info
.num_good_compute_units
,
846 max_threads_per_block
/ 64);
848 result
= radv_device_init_meta(device
);
849 if (result
!= VK_SUCCESS
)
852 radv_device_init_msaa(device
);
854 for (int family
= 0; family
< RADV_MAX_QUEUE_FAMILIES
; ++family
) {
855 device
->empty_cs
[family
] = device
->ws
->cs_create(device
->ws
, family
);
857 case RADV_QUEUE_GENERAL
:
858 radeon_emit(device
->empty_cs
[family
], PKT3(PKT3_CONTEXT_CONTROL
, 1, 0));
859 radeon_emit(device
->empty_cs
[family
], CONTEXT_CONTROL_LOAD_ENABLE(1));
860 radeon_emit(device
->empty_cs
[family
], CONTEXT_CONTROL_SHADOW_ENABLE(1));
862 case RADV_QUEUE_COMPUTE
:
863 radeon_emit(device
->empty_cs
[family
], PKT3(PKT3_NOP
, 0, 0));
864 radeon_emit(device
->empty_cs
[family
], 0);
867 device
->ws
->cs_finalize(device
->empty_cs
[family
]);
870 if (getenv("RADV_TRACE_FILE")) {
871 device
->trace_bo
= device
->ws
->buffer_create(device
->ws
, 4096, 8,
872 RADEON_DOMAIN_VRAM
, RADEON_FLAG_CPU_ACCESS
);
873 if (!device
->trace_bo
)
876 device
->trace_id_ptr
= device
->ws
->buffer_map(device
->trace_bo
);
877 if (!device
->trace_id_ptr
)
881 *pDevice
= radv_device_to_handle(device
);
885 if (device
->trace_bo
)
886 device
->ws
->buffer_destroy(device
->trace_bo
);
888 for (unsigned i
= 0; i
< RADV_MAX_QUEUE_FAMILIES
; i
++) {
889 for (unsigned q
= 0; q
< device
->queue_count
[i
]; q
++)
890 radv_queue_finish(&device
->queues
[i
][q
]);
891 if (device
->queue_count
[i
])
892 vk_free(&device
->alloc
, device
->queues
[i
]);
895 vk_free(&device
->alloc
, device
);
899 void radv_DestroyDevice(
901 const VkAllocationCallbacks
* pAllocator
)
903 RADV_FROM_HANDLE(radv_device
, device
, _device
);
905 if (device
->trace_bo
)
906 device
->ws
->buffer_destroy(device
->trace_bo
);
908 for (unsigned i
= 0; i
< RADV_MAX_QUEUE_FAMILIES
; i
++) {
909 for (unsigned q
= 0; q
< device
->queue_count
[i
]; q
++)
910 radv_queue_finish(&device
->queues
[i
][q
]);
911 if (device
->queue_count
[i
])
912 vk_free(&device
->alloc
, device
->queues
[i
]);
914 radv_device_finish_meta(device
);
916 vk_free(&device
->alloc
, device
);
919 VkResult
radv_EnumerateInstanceExtensionProperties(
920 const char* pLayerName
,
921 uint32_t* pPropertyCount
,
922 VkExtensionProperties
* pProperties
)
924 if (pProperties
== NULL
) {
925 *pPropertyCount
= ARRAY_SIZE(instance_extensions
);
929 *pPropertyCount
= MIN2(*pPropertyCount
, ARRAY_SIZE(instance_extensions
));
930 typed_memcpy(pProperties
, instance_extensions
, *pPropertyCount
);
932 if (*pPropertyCount
< ARRAY_SIZE(instance_extensions
))
933 return VK_INCOMPLETE
;
938 VkResult
radv_EnumerateDeviceExtensionProperties(
939 VkPhysicalDevice physicalDevice
,
940 const char* pLayerName
,
941 uint32_t* pPropertyCount
,
942 VkExtensionProperties
* pProperties
)
944 RADV_FROM_HANDLE(radv_physical_device
, pdevice
, physicalDevice
);
946 if (pProperties
== NULL
) {
947 *pPropertyCount
= pdevice
->extensions
.num_ext
;
951 *pPropertyCount
= MIN2(*pPropertyCount
, pdevice
->extensions
.num_ext
);
952 typed_memcpy(pProperties
, pdevice
->extensions
.ext_array
, *pPropertyCount
);
954 if (*pPropertyCount
< pdevice
->extensions
.num_ext
)
955 return VK_INCOMPLETE
;
960 VkResult
radv_EnumerateInstanceLayerProperties(
961 uint32_t* pPropertyCount
,
962 VkLayerProperties
* pProperties
)
964 if (pProperties
== NULL
) {
969 /* None supported at this time */
970 return vk_error(VK_ERROR_LAYER_NOT_PRESENT
);
973 VkResult
radv_EnumerateDeviceLayerProperties(
974 VkPhysicalDevice physicalDevice
,
975 uint32_t* pPropertyCount
,
976 VkLayerProperties
* pProperties
)
978 if (pProperties
== NULL
) {
983 /* None supported at this time */
984 return vk_error(VK_ERROR_LAYER_NOT_PRESENT
);
987 void radv_GetDeviceQueue(
989 uint32_t queueFamilyIndex
,
993 RADV_FROM_HANDLE(radv_device
, device
, _device
);
995 *pQueue
= radv_queue_to_handle(&device
->queues
[queueFamilyIndex
][queueIndex
]);
998 static void radv_dump_trace(struct radv_device
*device
,
999 struct radeon_winsys_cs
*cs
)
1001 const char *filename
= getenv("RADV_TRACE_FILE");
1002 FILE *f
= fopen(filename
, "w");
1004 fprintf(stderr
, "Failed to write trace dump to %s\n", filename
);
1008 fprintf(f
, "Trace ID: %x\n", *device
->trace_id_ptr
);
1009 device
->ws
->cs_dump(cs
, f
, *device
->trace_id_ptr
);
1014 radv_get_preamble_cs(struct radv_queue
*queue
,
1015 uint32_t scratch_size
,
1016 uint32_t compute_scratch_size
,
1017 struct radeon_winsys_cs
**preamble_cs
)
1019 struct radeon_winsys_bo
*scratch_bo
= NULL
;
1020 struct radeon_winsys_bo
*descriptor_bo
= NULL
;
1021 struct radeon_winsys_bo
*compute_scratch_bo
= NULL
;
1022 struct radeon_winsys_cs
*cs
= NULL
;
1024 if (!scratch_size
&& !compute_scratch_size
) {
1025 *preamble_cs
= NULL
;
1029 if (scratch_size
<= queue
->scratch_size
&&
1030 compute_scratch_size
<= queue
->compute_scratch_size
) {
1031 *preamble_cs
= queue
->preamble_cs
;
1035 if (scratch_size
> queue
->scratch_size
) {
1036 scratch_bo
= queue
->device
->ws
->buffer_create(queue
->device
->ws
,
1040 RADEON_FLAG_NO_CPU_ACCESS
);
1044 scratch_bo
= queue
->scratch_bo
;
1046 if (compute_scratch_size
> queue
->compute_scratch_size
) {
1047 compute_scratch_bo
= queue
->device
->ws
->buffer_create(queue
->device
->ws
,
1048 compute_scratch_size
,
1051 RADEON_FLAG_NO_CPU_ACCESS
);
1052 if (!compute_scratch_bo
)
1056 compute_scratch_bo
= queue
->compute_scratch_bo
;
1058 if (scratch_bo
!= queue
->scratch_bo
) {
1059 descriptor_bo
= queue
->device
->ws
->buffer_create(queue
->device
->ws
,
1063 RADEON_FLAG_CPU_ACCESS
);
1067 descriptor_bo
= queue
->descriptor_bo
;
1069 cs
= queue
->device
->ws
->cs_create(queue
->device
->ws
,
1070 queue
->queue_family_index
? RING_COMPUTE
: RING_GFX
);
1076 queue
->device
->ws
->cs_add_buffer(cs
, scratch_bo
, 8);
1079 queue
->device
->ws
->cs_add_buffer(cs
, descriptor_bo
, 8);
1081 if (descriptor_bo
!= queue
->descriptor_bo
) {
1082 uint64_t scratch_va
= queue
->device
->ws
->buffer_get_va(scratch_bo
);
1083 uint32_t rsrc1
= S_008F04_BASE_ADDRESS_HI(scratch_va
>> 32) |
1084 S_008F04_SWIZZLE_ENABLE(1);
1086 uint32_t *map
= (uint32_t*)queue
->device
->ws
->buffer_map(descriptor_bo
);
1088 map
[0] = scratch_va
;
1091 queue
->device
->ws
->buffer_unmap(descriptor_bo
);
1094 if (descriptor_bo
) {
1095 uint32_t regs
[] = {R_00B030_SPI_SHADER_USER_DATA_PS_0
,
1096 R_00B130_SPI_SHADER_USER_DATA_VS_0
,
1097 R_00B230_SPI_SHADER_USER_DATA_GS_0
,
1098 R_00B330_SPI_SHADER_USER_DATA_ES_0
,
1099 R_00B430_SPI_SHADER_USER_DATA_HS_0
,
1100 R_00B530_SPI_SHADER_USER_DATA_LS_0
};
1102 uint64_t va
= queue
->device
->ws
->buffer_get_va(descriptor_bo
);
1104 for (int i
= 0; i
< ARRAY_SIZE(regs
); ++i
) {
1105 radeon_set_sh_reg_seq(cs
, regs
[i
], 2);
1106 radeon_emit(cs
, va
);
1107 radeon_emit(cs
, va
>> 32);
1111 if (compute_scratch_bo
) {
1112 uint64_t scratch_va
= queue
->device
->ws
->buffer_get_va(compute_scratch_bo
);
1113 uint32_t rsrc1
= S_008F04_BASE_ADDRESS_HI(scratch_va
>> 32) |
1114 S_008F04_SWIZZLE_ENABLE(1);
1116 queue
->device
->ws
->cs_add_buffer(cs
, compute_scratch_bo
, 8);
1118 radeon_set_sh_reg_seq(cs
, R_00B900_COMPUTE_USER_DATA_0
, 2);
1119 radeon_emit(cs
, scratch_va
);
1120 radeon_emit(cs
, rsrc1
);
1123 if (!queue
->device
->ws
->cs_finalize(cs
))
1126 if (queue
->preamble_cs
)
1127 queue
->device
->ws
->cs_destroy(queue
->preamble_cs
);
1129 queue
->preamble_cs
= cs
;
1131 if (scratch_bo
!= queue
->scratch_bo
) {
1132 if (queue
->scratch_bo
)
1133 queue
->device
->ws
->buffer_destroy(queue
->scratch_bo
);
1134 queue
->scratch_bo
= scratch_bo
;
1135 queue
->scratch_size
= scratch_size
;
1138 if (compute_scratch_bo
!= queue
->compute_scratch_bo
) {
1139 if (queue
->compute_scratch_bo
)
1140 queue
->device
->ws
->buffer_destroy(queue
->compute_scratch_bo
);
1141 queue
->compute_scratch_bo
= compute_scratch_bo
;
1142 queue
->compute_scratch_size
= compute_scratch_size
;
1145 if (descriptor_bo
!= queue
->descriptor_bo
) {
1146 if (queue
->descriptor_bo
)
1147 queue
->device
->ws
->buffer_destroy(queue
->descriptor_bo
);
1149 queue
->descriptor_bo
= descriptor_bo
;
1156 queue
->device
->ws
->cs_destroy(cs
);
1157 if (descriptor_bo
&& descriptor_bo
!= queue
->descriptor_bo
)
1158 queue
->device
->ws
->buffer_destroy(descriptor_bo
);
1159 if (scratch_bo
&& scratch_bo
!= queue
->scratch_bo
)
1160 queue
->device
->ws
->buffer_destroy(scratch_bo
);
1161 if (compute_scratch_bo
&& compute_scratch_bo
!= queue
->compute_scratch_bo
)
1162 queue
->device
->ws
->buffer_destroy(compute_scratch_bo
);
1163 return VK_ERROR_OUT_OF_DEVICE_MEMORY
;
1166 VkResult
radv_QueueSubmit(
1168 uint32_t submitCount
,
1169 const VkSubmitInfo
* pSubmits
,
1172 RADV_FROM_HANDLE(radv_queue
, queue
, _queue
);
1173 RADV_FROM_HANDLE(radv_fence
, fence
, _fence
);
1174 struct radeon_winsys_fence
*base_fence
= fence
? fence
->fence
: NULL
;
1175 struct radeon_winsys_ctx
*ctx
= queue
->hw_ctx
;
1177 uint32_t max_cs_submission
= queue
->device
->trace_bo
? 1 : UINT32_MAX
;
1178 uint32_t scratch_size
= 0;
1179 uint32_t compute_scratch_size
= 0;
1180 struct radeon_winsys_cs
*preamble_cs
= NULL
;
1183 /* Do this first so failing to allocate scratch buffers can't result in
1184 * partially executed submissions. */
1185 for (uint32_t i
= 0; i
< submitCount
; i
++) {
1186 for (uint32_t j
= 0; j
< pSubmits
[i
].commandBufferCount
; j
++) {
1187 RADV_FROM_HANDLE(radv_cmd_buffer
, cmd_buffer
,
1188 pSubmits
[i
].pCommandBuffers
[j
]);
1190 scratch_size
= MAX2(scratch_size
, cmd_buffer
->scratch_size_needed
);
1191 compute_scratch_size
= MAX2(compute_scratch_size
,
1192 cmd_buffer
->compute_scratch_size_needed
);
1196 result
= radv_get_preamble_cs(queue
, scratch_size
, compute_scratch_size
, &preamble_cs
);
1197 if (result
!= VK_SUCCESS
)
1200 for (uint32_t i
= 0; i
< submitCount
; i
++) {
1201 struct radeon_winsys_cs
**cs_array
;
1202 bool can_patch
= true;
1205 if (!pSubmits
[i
].commandBufferCount
)
1208 cs_array
= malloc(sizeof(struct radeon_winsys_cs
*) *
1209 pSubmits
[i
].commandBufferCount
);
1211 for (uint32_t j
= 0; j
< pSubmits
[i
].commandBufferCount
; j
++) {
1212 RADV_FROM_HANDLE(radv_cmd_buffer
, cmd_buffer
,
1213 pSubmits
[i
].pCommandBuffers
[j
]);
1214 assert(cmd_buffer
->level
== VK_COMMAND_BUFFER_LEVEL_PRIMARY
);
1216 cs_array
[j
] = cmd_buffer
->cs
;
1217 if ((cmd_buffer
->usage_flags
& VK_COMMAND_BUFFER_USAGE_SIMULTANEOUS_USE_BIT
))
1221 for (uint32_t j
= 0; j
< pSubmits
[i
].commandBufferCount
; j
+= advance
) {
1222 advance
= MIN2(max_cs_submission
,
1223 pSubmits
[i
].commandBufferCount
- j
);
1225 bool e
= j
+ advance
== pSubmits
[i
].commandBufferCount
;
1227 if (queue
->device
->trace_bo
)
1228 *queue
->device
->trace_id_ptr
= 0;
1230 ret
= queue
->device
->ws
->cs_submit(ctx
, queue
->queue_idx
, cs_array
+ j
,
1231 advance
, preamble_cs
,
1232 (struct radeon_winsys_sem
**)pSubmits
[i
].pWaitSemaphores
,
1233 b
? pSubmits
[i
].waitSemaphoreCount
: 0,
1234 (struct radeon_winsys_sem
**)pSubmits
[i
].pSignalSemaphores
,
1235 e
? pSubmits
[i
].signalSemaphoreCount
: 0,
1236 can_patch
, base_fence
);
1239 radv_loge("failed to submit CS %d\n", i
);
1242 if (queue
->device
->trace_bo
) {
1243 bool success
= queue
->device
->ws
->ctx_wait_idle(
1245 radv_queue_family_to_ring(
1246 queue
->queue_family_index
),
1249 if (!success
) { /* Hang */
1250 radv_dump_trace(queue
->device
, cs_array
[j
]);
1260 ret
= queue
->device
->ws
->cs_submit(ctx
, queue
->queue_idx
,
1261 &queue
->device
->empty_cs
[queue
->queue_family_index
],
1262 1, NULL
, NULL
, 0, NULL
, 0,
1265 fence
->submitted
= true;
1271 VkResult
radv_QueueWaitIdle(
1274 RADV_FROM_HANDLE(radv_queue
, queue
, _queue
);
1276 queue
->device
->ws
->ctx_wait_idle(queue
->hw_ctx
,
1277 radv_queue_family_to_ring(queue
->queue_family_index
),
1282 VkResult
radv_DeviceWaitIdle(
1285 RADV_FROM_HANDLE(radv_device
, device
, _device
);
1287 for (unsigned i
= 0; i
< RADV_MAX_QUEUE_FAMILIES
; i
++) {
1288 for (unsigned q
= 0; q
< device
->queue_count
[i
]; q
++) {
1289 radv_QueueWaitIdle(radv_queue_to_handle(&device
->queues
[i
][q
]));
1295 PFN_vkVoidFunction
radv_GetInstanceProcAddr(
1296 VkInstance instance
,
1299 return radv_lookup_entrypoint(pName
);
1302 /* The loader wants us to expose a second GetInstanceProcAddr function
1303 * to work around certain LD_PRELOAD issues seen in apps.
1306 VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL
vk_icdGetInstanceProcAddr(
1307 VkInstance instance
,
1311 VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL
vk_icdGetInstanceProcAddr(
1312 VkInstance instance
,
1315 return radv_GetInstanceProcAddr(instance
, pName
);
1318 PFN_vkVoidFunction
radv_GetDeviceProcAddr(
1322 return radv_lookup_entrypoint(pName
);
1325 VkResult
radv_AllocateMemory(
1327 const VkMemoryAllocateInfo
* pAllocateInfo
,
1328 const VkAllocationCallbacks
* pAllocator
,
1329 VkDeviceMemory
* pMem
)
1331 RADV_FROM_HANDLE(radv_device
, device
, _device
);
1332 struct radv_device_memory
*mem
;
1334 enum radeon_bo_domain domain
;
1336 assert(pAllocateInfo
->sType
== VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO
);
1338 if (pAllocateInfo
->allocationSize
== 0) {
1339 /* Apparently, this is allowed */
1340 *pMem
= VK_NULL_HANDLE
;
1344 mem
= vk_alloc2(&device
->alloc
, pAllocator
, sizeof(*mem
), 8,
1345 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
1347 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
1349 uint64_t alloc_size
= align_u64(pAllocateInfo
->allocationSize
, 4096);
1350 if (pAllocateInfo
->memoryTypeIndex
== RADV_MEM_TYPE_GTT_WRITE_COMBINE
||
1351 pAllocateInfo
->memoryTypeIndex
== RADV_MEM_TYPE_GTT_CACHED
)
1352 domain
= RADEON_DOMAIN_GTT
;
1354 domain
= RADEON_DOMAIN_VRAM
;
1356 if (pAllocateInfo
->memoryTypeIndex
== RADV_MEM_TYPE_VRAM
)
1357 flags
|= RADEON_FLAG_NO_CPU_ACCESS
;
1359 flags
|= RADEON_FLAG_CPU_ACCESS
;
1361 if (pAllocateInfo
->memoryTypeIndex
== RADV_MEM_TYPE_GTT_WRITE_COMBINE
)
1362 flags
|= RADEON_FLAG_GTT_WC
;
1364 mem
->bo
= device
->ws
->buffer_create(device
->ws
, alloc_size
, 32768,
1368 result
= VK_ERROR_OUT_OF_DEVICE_MEMORY
;
1371 mem
->type_index
= pAllocateInfo
->memoryTypeIndex
;
1373 *pMem
= radv_device_memory_to_handle(mem
);
1378 vk_free2(&device
->alloc
, pAllocator
, mem
);
1383 void radv_FreeMemory(
1385 VkDeviceMemory _mem
,
1386 const VkAllocationCallbacks
* pAllocator
)
1388 RADV_FROM_HANDLE(radv_device
, device
, _device
);
1389 RADV_FROM_HANDLE(radv_device_memory
, mem
, _mem
);
1394 device
->ws
->buffer_destroy(mem
->bo
);
1397 vk_free2(&device
->alloc
, pAllocator
, mem
);
1400 VkResult
radv_MapMemory(
1402 VkDeviceMemory _memory
,
1403 VkDeviceSize offset
,
1405 VkMemoryMapFlags flags
,
1408 RADV_FROM_HANDLE(radv_device
, device
, _device
);
1409 RADV_FROM_HANDLE(radv_device_memory
, mem
, _memory
);
1416 *ppData
= device
->ws
->buffer_map(mem
->bo
);
1422 return VK_ERROR_MEMORY_MAP_FAILED
;
1425 void radv_UnmapMemory(
1427 VkDeviceMemory _memory
)
1429 RADV_FROM_HANDLE(radv_device
, device
, _device
);
1430 RADV_FROM_HANDLE(radv_device_memory
, mem
, _memory
);
1435 device
->ws
->buffer_unmap(mem
->bo
);
1438 VkResult
radv_FlushMappedMemoryRanges(
1440 uint32_t memoryRangeCount
,
1441 const VkMappedMemoryRange
* pMemoryRanges
)
1446 VkResult
radv_InvalidateMappedMemoryRanges(
1448 uint32_t memoryRangeCount
,
1449 const VkMappedMemoryRange
* pMemoryRanges
)
1454 void radv_GetBufferMemoryRequirements(
1457 VkMemoryRequirements
* pMemoryRequirements
)
1459 RADV_FROM_HANDLE(radv_buffer
, buffer
, _buffer
);
1461 pMemoryRequirements
->memoryTypeBits
= (1u << RADV_MEM_TYPE_COUNT
) - 1;
1463 pMemoryRequirements
->size
= buffer
->size
;
1464 pMemoryRequirements
->alignment
= 16;
1467 void radv_GetImageMemoryRequirements(
1470 VkMemoryRequirements
* pMemoryRequirements
)
1472 RADV_FROM_HANDLE(radv_image
, image
, _image
);
1474 pMemoryRequirements
->memoryTypeBits
= (1u << RADV_MEM_TYPE_COUNT
) - 1;
1476 pMemoryRequirements
->size
= image
->size
;
1477 pMemoryRequirements
->alignment
= image
->alignment
;
1480 void radv_GetImageSparseMemoryRequirements(
1483 uint32_t* pSparseMemoryRequirementCount
,
1484 VkSparseImageMemoryRequirements
* pSparseMemoryRequirements
)
1489 void radv_GetDeviceMemoryCommitment(
1491 VkDeviceMemory memory
,
1492 VkDeviceSize
* pCommittedMemoryInBytes
)
1494 *pCommittedMemoryInBytes
= 0;
1497 VkResult
radv_BindBufferMemory(
1500 VkDeviceMemory _memory
,
1501 VkDeviceSize memoryOffset
)
1503 RADV_FROM_HANDLE(radv_device_memory
, mem
, _memory
);
1504 RADV_FROM_HANDLE(radv_buffer
, buffer
, _buffer
);
1507 buffer
->bo
= mem
->bo
;
1508 buffer
->offset
= memoryOffset
;
1517 VkResult
radv_BindImageMemory(
1520 VkDeviceMemory _memory
,
1521 VkDeviceSize memoryOffset
)
1523 RADV_FROM_HANDLE(radv_device_memory
, mem
, _memory
);
1524 RADV_FROM_HANDLE(radv_image
, image
, _image
);
1527 image
->bo
= mem
->bo
;
1528 image
->offset
= memoryOffset
;
1537 VkResult
radv_QueueBindSparse(
1539 uint32_t bindInfoCount
,
1540 const VkBindSparseInfo
* pBindInfo
,
1543 stub_return(VK_ERROR_INCOMPATIBLE_DRIVER
);
1546 VkResult
radv_CreateFence(
1548 const VkFenceCreateInfo
* pCreateInfo
,
1549 const VkAllocationCallbacks
* pAllocator
,
1552 RADV_FROM_HANDLE(radv_device
, device
, _device
);
1553 struct radv_fence
*fence
= vk_alloc2(&device
->alloc
, pAllocator
,
1555 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
1558 return VK_ERROR_OUT_OF_HOST_MEMORY
;
1560 memset(fence
, 0, sizeof(*fence
));
1561 fence
->submitted
= false;
1562 fence
->signalled
= !!(pCreateInfo
->flags
& VK_FENCE_CREATE_SIGNALED_BIT
);
1563 fence
->fence
= device
->ws
->create_fence();
1564 if (!fence
->fence
) {
1565 vk_free2(&device
->alloc
, pAllocator
, fence
);
1566 return VK_ERROR_OUT_OF_HOST_MEMORY
;
1569 *pFence
= radv_fence_to_handle(fence
);
1574 void radv_DestroyFence(
1577 const VkAllocationCallbacks
* pAllocator
)
1579 RADV_FROM_HANDLE(radv_device
, device
, _device
);
1580 RADV_FROM_HANDLE(radv_fence
, fence
, _fence
);
1584 device
->ws
->destroy_fence(fence
->fence
);
1585 vk_free2(&device
->alloc
, pAllocator
, fence
);
1588 static uint64_t radv_get_absolute_timeout(uint64_t timeout
)
1590 uint64_t current_time
;
1593 clock_gettime(CLOCK_MONOTONIC
, &tv
);
1594 current_time
= tv
.tv_nsec
+ tv
.tv_sec
*1000000000ull;
1596 timeout
= MIN2(UINT64_MAX
- current_time
, timeout
);
1598 return current_time
+ timeout
;
1601 VkResult
radv_WaitForFences(
1603 uint32_t fenceCount
,
1604 const VkFence
* pFences
,
1608 RADV_FROM_HANDLE(radv_device
, device
, _device
);
1609 timeout
= radv_get_absolute_timeout(timeout
);
1611 if (!waitAll
&& fenceCount
> 1) {
1612 fprintf(stderr
, "radv: WaitForFences without waitAll not implemented yet\n");
1615 for (uint32_t i
= 0; i
< fenceCount
; ++i
) {
1616 RADV_FROM_HANDLE(radv_fence
, fence
, pFences
[i
]);
1617 bool expired
= false;
1619 if (fence
->signalled
)
1622 if (!fence
->submitted
)
1625 expired
= device
->ws
->fence_wait(device
->ws
, fence
->fence
, true, timeout
);
1629 fence
->signalled
= true;
1635 VkResult
radv_ResetFences(VkDevice device
,
1636 uint32_t fenceCount
,
1637 const VkFence
*pFences
)
1639 for (unsigned i
= 0; i
< fenceCount
; ++i
) {
1640 RADV_FROM_HANDLE(radv_fence
, fence
, pFences
[i
]);
1641 fence
->submitted
= fence
->signalled
= false;
1647 VkResult
radv_GetFenceStatus(VkDevice _device
, VkFence _fence
)
1649 RADV_FROM_HANDLE(radv_device
, device
, _device
);
1650 RADV_FROM_HANDLE(radv_fence
, fence
, _fence
);
1652 if (fence
->signalled
)
1654 if (!fence
->submitted
)
1655 return VK_NOT_READY
;
1657 if (!device
->ws
->fence_wait(device
->ws
, fence
->fence
, false, 0))
1658 return VK_NOT_READY
;
1664 // Queue semaphore functions
1666 VkResult
radv_CreateSemaphore(
1668 const VkSemaphoreCreateInfo
* pCreateInfo
,
1669 const VkAllocationCallbacks
* pAllocator
,
1670 VkSemaphore
* pSemaphore
)
1672 RADV_FROM_HANDLE(radv_device
, device
, _device
);
1673 struct radeon_winsys_sem
*sem
;
1675 sem
= device
->ws
->create_sem(device
->ws
);
1677 return VK_ERROR_OUT_OF_HOST_MEMORY
;
1679 *pSemaphore
= (VkSemaphore
)sem
;
1683 void radv_DestroySemaphore(
1685 VkSemaphore _semaphore
,
1686 const VkAllocationCallbacks
* pAllocator
)
1688 RADV_FROM_HANDLE(radv_device
, device
, _device
);
1689 struct radeon_winsys_sem
*sem
;
1693 sem
= (struct radeon_winsys_sem
*)_semaphore
;
1694 device
->ws
->destroy_sem(sem
);
1697 VkResult
radv_CreateEvent(
1699 const VkEventCreateInfo
* pCreateInfo
,
1700 const VkAllocationCallbacks
* pAllocator
,
1703 RADV_FROM_HANDLE(radv_device
, device
, _device
);
1704 struct radv_event
*event
= vk_alloc2(&device
->alloc
, pAllocator
,
1706 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
1709 return VK_ERROR_OUT_OF_HOST_MEMORY
;
1711 event
->bo
= device
->ws
->buffer_create(device
->ws
, 8, 8,
1713 RADEON_FLAG_CPU_ACCESS
);
1715 vk_free2(&device
->alloc
, pAllocator
, event
);
1716 return VK_ERROR_OUT_OF_DEVICE_MEMORY
;
1719 event
->map
= (uint64_t*)device
->ws
->buffer_map(event
->bo
);
1721 *pEvent
= radv_event_to_handle(event
);
1726 void radv_DestroyEvent(
1729 const VkAllocationCallbacks
* pAllocator
)
1731 RADV_FROM_HANDLE(radv_device
, device
, _device
);
1732 RADV_FROM_HANDLE(radv_event
, event
, _event
);
1736 device
->ws
->buffer_destroy(event
->bo
);
1737 vk_free2(&device
->alloc
, pAllocator
, event
);
1740 VkResult
radv_GetEventStatus(
1744 RADV_FROM_HANDLE(radv_event
, event
, _event
);
1746 if (*event
->map
== 1)
1747 return VK_EVENT_SET
;
1748 return VK_EVENT_RESET
;
1751 VkResult
radv_SetEvent(
1755 RADV_FROM_HANDLE(radv_event
, event
, _event
);
1761 VkResult
radv_ResetEvent(
1765 RADV_FROM_HANDLE(radv_event
, event
, _event
);
1771 VkResult
radv_CreateBuffer(
1773 const VkBufferCreateInfo
* pCreateInfo
,
1774 const VkAllocationCallbacks
* pAllocator
,
1777 RADV_FROM_HANDLE(radv_device
, device
, _device
);
1778 struct radv_buffer
*buffer
;
1780 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO
);
1782 buffer
= vk_alloc2(&device
->alloc
, pAllocator
, sizeof(*buffer
), 8,
1783 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
1785 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
1787 buffer
->size
= pCreateInfo
->size
;
1788 buffer
->usage
= pCreateInfo
->usage
;
1792 *pBuffer
= radv_buffer_to_handle(buffer
);
1797 void radv_DestroyBuffer(
1800 const VkAllocationCallbacks
* pAllocator
)
1802 RADV_FROM_HANDLE(radv_device
, device
, _device
);
1803 RADV_FROM_HANDLE(radv_buffer
, buffer
, _buffer
);
1808 vk_free2(&device
->alloc
, pAllocator
, buffer
);
1811 static inline unsigned
1812 si_tile_mode_index(const struct radv_image
*image
, unsigned level
, bool stencil
)
1815 return image
->surface
.stencil_tiling_index
[level
];
1817 return image
->surface
.tiling_index
[level
];
1821 radv_initialise_color_surface(struct radv_device
*device
,
1822 struct radv_color_buffer_info
*cb
,
1823 struct radv_image_view
*iview
)
1825 const struct vk_format_description
*desc
;
1826 unsigned ntype
, format
, swap
, endian
;
1827 unsigned blend_clamp
= 0, blend_bypass
= 0;
1828 unsigned pitch_tile_max
, slice_tile_max
, tile_mode_index
;
1830 const struct radeon_surf
*surf
= &iview
->image
->surface
;
1831 const struct radeon_surf_level
*level_info
= &surf
->level
[iview
->base_mip
];
1833 desc
= vk_format_description(iview
->vk_format
);
1835 memset(cb
, 0, sizeof(*cb
));
1837 va
= device
->ws
->buffer_get_va(iview
->bo
) + iview
->image
->offset
;
1838 va
+= level_info
->offset
;
1839 cb
->cb_color_base
= va
>> 8;
1841 /* CMASK variables */
1842 va
= device
->ws
->buffer_get_va(iview
->bo
) + iview
->image
->offset
;
1843 va
+= iview
->image
->cmask
.offset
;
1844 cb
->cb_color_cmask
= va
>> 8;
1845 cb
->cb_color_cmask_slice
= iview
->image
->cmask
.slice_tile_max
;
1847 va
= device
->ws
->buffer_get_va(iview
->bo
) + iview
->image
->offset
;
1848 va
+= iview
->image
->dcc_offset
;
1849 cb
->cb_dcc_base
= va
>> 8;
1851 uint32_t max_slice
= iview
->type
== VK_IMAGE_VIEW_TYPE_3D
? iview
->extent
.depth
: iview
->layer_count
;
1852 cb
->cb_color_view
= S_028C6C_SLICE_START(iview
->base_layer
) |
1853 S_028C6C_SLICE_MAX(iview
->base_layer
+ max_slice
- 1);
1855 cb
->micro_tile_mode
= iview
->image
->surface
.micro_tile_mode
;
1856 pitch_tile_max
= level_info
->nblk_x
/ 8 - 1;
1857 slice_tile_max
= (level_info
->nblk_x
* level_info
->nblk_y
) / 64 - 1;
1858 tile_mode_index
= si_tile_mode_index(iview
->image
, iview
->base_mip
, false);
1860 cb
->cb_color_pitch
= S_028C64_TILE_MAX(pitch_tile_max
);
1861 cb
->cb_color_slice
= S_028C68_TILE_MAX(slice_tile_max
);
1863 /* Intensity is implemented as Red, so treat it that way. */
1864 cb
->cb_color_attrib
= S_028C74_FORCE_DST_ALPHA_1(desc
->swizzle
[3] == VK_SWIZZLE_1
) |
1865 S_028C74_TILE_MODE_INDEX(tile_mode_index
);
1867 if (iview
->image
->samples
> 1) {
1868 unsigned log_samples
= util_logbase2(iview
->image
->samples
);
1870 cb
->cb_color_attrib
|= S_028C74_NUM_SAMPLES(log_samples
) |
1871 S_028C74_NUM_FRAGMENTS(log_samples
);
1874 if (iview
->image
->fmask
.size
) {
1875 va
= device
->ws
->buffer_get_va(iview
->bo
) + iview
->image
->offset
+ iview
->image
->fmask
.offset
;
1876 if (device
->physical_device
->rad_info
.chip_class
>= CIK
)
1877 cb
->cb_color_pitch
|= S_028C64_FMASK_TILE_MAX(iview
->image
->fmask
.pitch_in_pixels
/ 8 - 1);
1878 cb
->cb_color_attrib
|= S_028C74_FMASK_TILE_MODE_INDEX(iview
->image
->fmask
.tile_mode_index
);
1879 cb
->cb_color_fmask
= va
>> 8;
1880 cb
->cb_color_fmask_slice
= S_028C88_TILE_MAX(iview
->image
->fmask
.slice_tile_max
);
1882 /* This must be set for fast clear to work without FMASK. */
1883 if (device
->physical_device
->rad_info
.chip_class
>= CIK
)
1884 cb
->cb_color_pitch
|= S_028C64_FMASK_TILE_MAX(pitch_tile_max
);
1885 cb
->cb_color_attrib
|= S_028C74_FMASK_TILE_MODE_INDEX(tile_mode_index
);
1886 cb
->cb_color_fmask
= cb
->cb_color_base
;
1887 cb
->cb_color_fmask_slice
= S_028C88_TILE_MAX(slice_tile_max
);
1890 ntype
= radv_translate_color_numformat(iview
->vk_format
,
1892 vk_format_get_first_non_void_channel(iview
->vk_format
));
1893 format
= radv_translate_colorformat(iview
->vk_format
);
1894 if (format
== V_028C70_COLOR_INVALID
|| ntype
== ~0u)
1895 radv_finishme("Illegal color\n");
1896 swap
= radv_translate_colorswap(iview
->vk_format
, FALSE
);
1897 endian
= radv_colorformat_endian_swap(format
);
1899 /* blend clamp should be set for all NORM/SRGB types */
1900 if (ntype
== V_028C70_NUMBER_UNORM
||
1901 ntype
== V_028C70_NUMBER_SNORM
||
1902 ntype
== V_028C70_NUMBER_SRGB
)
1905 /* set blend bypass according to docs if SINT/UINT or
1906 8/24 COLOR variants */
1907 if (ntype
== V_028C70_NUMBER_UINT
|| ntype
== V_028C70_NUMBER_SINT
||
1908 format
== V_028C70_COLOR_8_24
|| format
== V_028C70_COLOR_24_8
||
1909 format
== V_028C70_COLOR_X24_8_32_FLOAT
) {
1914 if ((ntype
== V_028C70_NUMBER_UINT
|| ntype
== V_028C70_NUMBER_SINT
) &&
1915 (format
== V_028C70_COLOR_8
||
1916 format
== V_028C70_COLOR_8_8
||
1917 format
== V_028C70_COLOR_8_8_8_8
))
1918 ->color_is_int8
= true;
1920 cb
->cb_color_info
= S_028C70_FORMAT(format
) |
1921 S_028C70_COMP_SWAP(swap
) |
1922 S_028C70_BLEND_CLAMP(blend_clamp
) |
1923 S_028C70_BLEND_BYPASS(blend_bypass
) |
1924 S_028C70_SIMPLE_FLOAT(1) |
1925 S_028C70_ROUND_MODE(ntype
!= V_028C70_NUMBER_UNORM
&&
1926 ntype
!= V_028C70_NUMBER_SNORM
&&
1927 ntype
!= V_028C70_NUMBER_SRGB
&&
1928 format
!= V_028C70_COLOR_8_24
&&
1929 format
!= V_028C70_COLOR_24_8
) |
1930 S_028C70_NUMBER_TYPE(ntype
) |
1931 S_028C70_ENDIAN(endian
);
1932 if (iview
->image
->samples
> 1)
1933 if (iview
->image
->fmask
.size
)
1934 cb
->cb_color_info
|= S_028C70_COMPRESSION(1);
1936 if (iview
->image
->cmask
.size
&&
1937 (device
->debug_flags
& RADV_DEBUG_FAST_CLEARS
))
1938 cb
->cb_color_info
|= S_028C70_FAST_CLEAR(1);
1940 if (iview
->image
->surface
.dcc_size
&& level_info
->dcc_enabled
)
1941 cb
->cb_color_info
|= S_028C70_DCC_ENABLE(1);
1943 if (device
->physical_device
->rad_info
.chip_class
>= VI
) {
1944 unsigned max_uncompressed_block_size
= 2;
1945 if (iview
->image
->samples
> 1) {
1946 if (iview
->image
->surface
.bpe
== 1)
1947 max_uncompressed_block_size
= 0;
1948 else if (iview
->image
->surface
.bpe
== 2)
1949 max_uncompressed_block_size
= 1;
1952 cb
->cb_dcc_control
= S_028C78_MAX_UNCOMPRESSED_BLOCK_SIZE(max_uncompressed_block_size
) |
1953 S_028C78_INDEPENDENT_64B_BLOCKS(1);
1956 /* This must be set for fast clear to work without FMASK. */
1957 if (!iview
->image
->fmask
.size
&&
1958 device
->physical_device
->rad_info
.chip_class
== SI
) {
1959 unsigned bankh
= util_logbase2(iview
->image
->surface
.bankh
);
1960 cb
->cb_color_attrib
|= S_028C74_FMASK_BANK_HEIGHT(bankh
);
1965 radv_initialise_ds_surface(struct radv_device
*device
,
1966 struct radv_ds_buffer_info
*ds
,
1967 struct radv_image_view
*iview
)
1969 unsigned level
= iview
->base_mip
;
1971 uint64_t va
, s_offs
, z_offs
;
1972 const struct radeon_surf_level
*level_info
= &iview
->image
->surface
.level
[level
];
1973 memset(ds
, 0, sizeof(*ds
));
1974 switch (iview
->vk_format
) {
1975 case VK_FORMAT_D24_UNORM_S8_UINT
:
1976 case VK_FORMAT_X8_D24_UNORM_PACK32
:
1977 ds
->pa_su_poly_offset_db_fmt_cntl
= S_028B78_POLY_OFFSET_NEG_NUM_DB_BITS(-24);
1978 ds
->offset_scale
= 2.0f
;
1980 case VK_FORMAT_D16_UNORM
:
1981 case VK_FORMAT_D16_UNORM_S8_UINT
:
1982 ds
->pa_su_poly_offset_db_fmt_cntl
= S_028B78_POLY_OFFSET_NEG_NUM_DB_BITS(-16);
1983 ds
->offset_scale
= 4.0f
;
1985 case VK_FORMAT_D32_SFLOAT
:
1986 case VK_FORMAT_D32_SFLOAT_S8_UINT
:
1987 ds
->pa_su_poly_offset_db_fmt_cntl
= S_028B78_POLY_OFFSET_NEG_NUM_DB_BITS(-23) |
1988 S_028B78_POLY_OFFSET_DB_IS_FLOAT_FMT(1);
1989 ds
->offset_scale
= 1.0f
;
1995 format
= radv_translate_dbformat(iview
->vk_format
);
1996 if (format
== V_028040_Z_INVALID
) {
1997 fprintf(stderr
, "Invalid DB format: %d, disabling DB.\n", iview
->vk_format
);
2000 va
= device
->ws
->buffer_get_va(iview
->bo
) + iview
->image
->offset
;
2001 s_offs
= z_offs
= va
;
2002 z_offs
+= iview
->image
->surface
.level
[level
].offset
;
2003 s_offs
+= iview
->image
->surface
.stencil_level
[level
].offset
;
2005 uint32_t max_slice
= iview
->type
== VK_IMAGE_VIEW_TYPE_3D
? iview
->extent
.depth
: iview
->layer_count
;
2006 ds
->db_depth_view
= S_028008_SLICE_START(iview
->base_layer
) |
2007 S_028008_SLICE_MAX(iview
->base_layer
+ max_slice
- 1);
2008 ds
->db_depth_info
= S_02803C_ADDR5_SWIZZLE_MASK(1);
2009 ds
->db_z_info
= S_028040_FORMAT(format
) | S_028040_ZRANGE_PRECISION(1);
2011 if (iview
->image
->samples
> 1)
2012 ds
->db_z_info
|= S_028040_NUM_SAMPLES(util_logbase2(iview
->image
->samples
));
2014 if (iview
->image
->surface
.flags
& RADEON_SURF_SBUFFER
)
2015 ds
->db_stencil_info
= S_028044_FORMAT(V_028044_STENCIL_8
);
2017 ds
->db_stencil_info
= S_028044_FORMAT(V_028044_STENCIL_INVALID
);
2019 if (device
->physical_device
->rad_info
.chip_class
>= CIK
) {
2020 struct radeon_info
*info
= &device
->physical_device
->rad_info
;
2021 unsigned tiling_index
= iview
->image
->surface
.tiling_index
[level
];
2022 unsigned stencil_index
= iview
->image
->surface
.stencil_tiling_index
[level
];
2023 unsigned macro_index
= iview
->image
->surface
.macro_tile_index
;
2024 unsigned tile_mode
= info
->si_tile_mode_array
[tiling_index
];
2025 unsigned stencil_tile_mode
= info
->si_tile_mode_array
[stencil_index
];
2026 unsigned macro_mode
= info
->cik_macrotile_mode_array
[macro_index
];
2028 ds
->db_depth_info
|=
2029 S_02803C_ARRAY_MODE(G_009910_ARRAY_MODE(tile_mode
)) |
2030 S_02803C_PIPE_CONFIG(G_009910_PIPE_CONFIG(tile_mode
)) |
2031 S_02803C_BANK_WIDTH(G_009990_BANK_WIDTH(macro_mode
)) |
2032 S_02803C_BANK_HEIGHT(G_009990_BANK_HEIGHT(macro_mode
)) |
2033 S_02803C_MACRO_TILE_ASPECT(G_009990_MACRO_TILE_ASPECT(macro_mode
)) |
2034 S_02803C_NUM_BANKS(G_009990_NUM_BANKS(macro_mode
));
2035 ds
->db_z_info
|= S_028040_TILE_SPLIT(G_009910_TILE_SPLIT(tile_mode
));
2036 ds
->db_stencil_info
|= S_028044_TILE_SPLIT(G_009910_TILE_SPLIT(stencil_tile_mode
));
2038 unsigned tile_mode_index
= si_tile_mode_index(iview
->image
, level
, false);
2039 ds
->db_z_info
|= S_028040_TILE_MODE_INDEX(tile_mode_index
);
2040 tile_mode_index
= si_tile_mode_index(iview
->image
, level
, true);
2041 ds
->db_stencil_info
|= S_028044_TILE_MODE_INDEX(tile_mode_index
);
2044 if (iview
->image
->htile
.size
&& !level
) {
2045 ds
->db_z_info
|= S_028040_TILE_SURFACE_ENABLE(1) |
2046 S_028040_ALLOW_EXPCLEAR(1);
2048 if (iview
->image
->surface
.flags
& RADEON_SURF_SBUFFER
) {
2049 /* Workaround: For a not yet understood reason, the
2050 * combination of MSAA, fast stencil clear and stencil
2051 * decompress messes with subsequent stencil buffer
2052 * uses. Problem was reproduced on Verde, Bonaire,
2053 * Tonga, and Carrizo.
2055 * Disabling EXPCLEAR works around the problem.
2057 * Check piglit's arb_texture_multisample-stencil-clear
2058 * test if you want to try changing this.
2060 if (iview
->image
->samples
<= 1)
2061 ds
->db_stencil_info
|= S_028044_ALLOW_EXPCLEAR(1);
2063 /* Use all of the htile_buffer for depth if there's no stencil. */
2064 ds
->db_stencil_info
|= S_028044_TILE_STENCIL_DISABLE(1);
2066 va
= device
->ws
->buffer_get_va(iview
->bo
) + iview
->image
->offset
+
2067 iview
->image
->htile
.offset
;
2068 ds
->db_htile_data_base
= va
>> 8;
2069 ds
->db_htile_surface
= S_028ABC_FULL_CACHE(1);
2071 ds
->db_htile_data_base
= 0;
2072 ds
->db_htile_surface
= 0;
2075 ds
->db_z_read_base
= ds
->db_z_write_base
= z_offs
>> 8;
2076 ds
->db_stencil_read_base
= ds
->db_stencil_write_base
= s_offs
>> 8;
2078 ds
->db_depth_size
= S_028058_PITCH_TILE_MAX((level_info
->nblk_x
/ 8) - 1) |
2079 S_028058_HEIGHT_TILE_MAX((level_info
->nblk_y
/ 8) - 1);
2080 ds
->db_depth_slice
= S_02805C_SLICE_TILE_MAX((level_info
->nblk_x
* level_info
->nblk_y
) / 64 - 1);
2083 VkResult
radv_CreateFramebuffer(
2085 const VkFramebufferCreateInfo
* pCreateInfo
,
2086 const VkAllocationCallbacks
* pAllocator
,
2087 VkFramebuffer
* pFramebuffer
)
2089 RADV_FROM_HANDLE(radv_device
, device
, _device
);
2090 struct radv_framebuffer
*framebuffer
;
2092 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO
);
2094 size_t size
= sizeof(*framebuffer
) +
2095 sizeof(struct radv_attachment_info
) * pCreateInfo
->attachmentCount
;
2096 framebuffer
= vk_alloc2(&device
->alloc
, pAllocator
, size
, 8,
2097 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
2098 if (framebuffer
== NULL
)
2099 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
2101 framebuffer
->attachment_count
= pCreateInfo
->attachmentCount
;
2102 for (uint32_t i
= 0; i
< pCreateInfo
->attachmentCount
; i
++) {
2103 VkImageView _iview
= pCreateInfo
->pAttachments
[i
];
2104 struct radv_image_view
*iview
= radv_image_view_from_handle(_iview
);
2105 framebuffer
->attachments
[i
].attachment
= iview
;
2106 if (iview
->aspect_mask
& VK_IMAGE_ASPECT_COLOR_BIT
) {
2107 radv_initialise_color_surface(device
, &framebuffer
->attachments
[i
].cb
, iview
);
2108 } else if (iview
->aspect_mask
& (VK_IMAGE_ASPECT_DEPTH_BIT
| VK_IMAGE_ASPECT_STENCIL_BIT
)) {
2109 radv_initialise_ds_surface(device
, &framebuffer
->attachments
[i
].ds
, iview
);
2113 framebuffer
->width
= pCreateInfo
->width
;
2114 framebuffer
->height
= pCreateInfo
->height
;
2115 framebuffer
->layers
= pCreateInfo
->layers
;
2117 *pFramebuffer
= radv_framebuffer_to_handle(framebuffer
);
2121 void radv_DestroyFramebuffer(
2124 const VkAllocationCallbacks
* pAllocator
)
2126 RADV_FROM_HANDLE(radv_device
, device
, _device
);
2127 RADV_FROM_HANDLE(radv_framebuffer
, fb
, _fb
);
2131 vk_free2(&device
->alloc
, pAllocator
, fb
);
2134 static unsigned radv_tex_wrap(VkSamplerAddressMode address_mode
)
2136 switch (address_mode
) {
2137 case VK_SAMPLER_ADDRESS_MODE_REPEAT
:
2138 return V_008F30_SQ_TEX_WRAP
;
2139 case VK_SAMPLER_ADDRESS_MODE_MIRRORED_REPEAT
:
2140 return V_008F30_SQ_TEX_MIRROR
;
2141 case VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE
:
2142 return V_008F30_SQ_TEX_CLAMP_LAST_TEXEL
;
2143 case VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_BORDER
:
2144 return V_008F30_SQ_TEX_CLAMP_BORDER
;
2145 case VK_SAMPLER_ADDRESS_MODE_MIRROR_CLAMP_TO_EDGE
:
2146 return V_008F30_SQ_TEX_MIRROR_ONCE_LAST_TEXEL
;
2148 unreachable("illegal tex wrap mode");
2154 radv_tex_compare(VkCompareOp op
)
2157 case VK_COMPARE_OP_NEVER
:
2158 return V_008F30_SQ_TEX_DEPTH_COMPARE_NEVER
;
2159 case VK_COMPARE_OP_LESS
:
2160 return V_008F30_SQ_TEX_DEPTH_COMPARE_LESS
;
2161 case VK_COMPARE_OP_EQUAL
:
2162 return V_008F30_SQ_TEX_DEPTH_COMPARE_EQUAL
;
2163 case VK_COMPARE_OP_LESS_OR_EQUAL
:
2164 return V_008F30_SQ_TEX_DEPTH_COMPARE_LESSEQUAL
;
2165 case VK_COMPARE_OP_GREATER
:
2166 return V_008F30_SQ_TEX_DEPTH_COMPARE_GREATER
;
2167 case VK_COMPARE_OP_NOT_EQUAL
:
2168 return V_008F30_SQ_TEX_DEPTH_COMPARE_NOTEQUAL
;
2169 case VK_COMPARE_OP_GREATER_OR_EQUAL
:
2170 return V_008F30_SQ_TEX_DEPTH_COMPARE_GREATEREQUAL
;
2171 case VK_COMPARE_OP_ALWAYS
:
2172 return V_008F30_SQ_TEX_DEPTH_COMPARE_ALWAYS
;
2174 unreachable("illegal compare mode");
2180 radv_tex_filter(VkFilter filter
, unsigned max_ansio
)
2183 case VK_FILTER_NEAREST
:
2184 return (max_ansio
> 1 ? V_008F38_SQ_TEX_XY_FILTER_ANISO_POINT
:
2185 V_008F38_SQ_TEX_XY_FILTER_POINT
);
2186 case VK_FILTER_LINEAR
:
2187 return (max_ansio
> 1 ? V_008F38_SQ_TEX_XY_FILTER_ANISO_BILINEAR
:
2188 V_008F38_SQ_TEX_XY_FILTER_BILINEAR
);
2189 case VK_FILTER_CUBIC_IMG
:
2191 fprintf(stderr
, "illegal texture filter");
2197 radv_tex_mipfilter(VkSamplerMipmapMode mode
)
2200 case VK_SAMPLER_MIPMAP_MODE_NEAREST
:
2201 return V_008F38_SQ_TEX_Z_FILTER_POINT
;
2202 case VK_SAMPLER_MIPMAP_MODE_LINEAR
:
2203 return V_008F38_SQ_TEX_Z_FILTER_LINEAR
;
2205 return V_008F38_SQ_TEX_Z_FILTER_NONE
;
2210 radv_tex_bordercolor(VkBorderColor bcolor
)
2213 case VK_BORDER_COLOR_FLOAT_TRANSPARENT_BLACK
:
2214 case VK_BORDER_COLOR_INT_TRANSPARENT_BLACK
:
2215 return V_008F3C_SQ_TEX_BORDER_COLOR_TRANS_BLACK
;
2216 case VK_BORDER_COLOR_FLOAT_OPAQUE_BLACK
:
2217 case VK_BORDER_COLOR_INT_OPAQUE_BLACK
:
2218 return V_008F3C_SQ_TEX_BORDER_COLOR_OPAQUE_BLACK
;
2219 case VK_BORDER_COLOR_FLOAT_OPAQUE_WHITE
:
2220 case VK_BORDER_COLOR_INT_OPAQUE_WHITE
:
2221 return V_008F3C_SQ_TEX_BORDER_COLOR_OPAQUE_WHITE
;
2229 radv_tex_aniso_filter(unsigned filter
)
2243 radv_init_sampler(struct radv_device
*device
,
2244 struct radv_sampler
*sampler
,
2245 const VkSamplerCreateInfo
*pCreateInfo
)
2247 uint32_t max_aniso
= pCreateInfo
->anisotropyEnable
&& pCreateInfo
->maxAnisotropy
> 1.0 ?
2248 (uint32_t) pCreateInfo
->maxAnisotropy
: 0;
2249 uint32_t max_aniso_ratio
= radv_tex_aniso_filter(max_aniso
);
2250 bool is_vi
= (device
->physical_device
->rad_info
.chip_class
>= VI
);
2252 sampler
->state
[0] = (S_008F30_CLAMP_X(radv_tex_wrap(pCreateInfo
->addressModeU
)) |
2253 S_008F30_CLAMP_Y(radv_tex_wrap(pCreateInfo
->addressModeV
)) |
2254 S_008F30_CLAMP_Z(radv_tex_wrap(pCreateInfo
->addressModeW
)) |
2255 S_008F30_MAX_ANISO_RATIO(max_aniso_ratio
) |
2256 S_008F30_DEPTH_COMPARE_FUNC(radv_tex_compare(pCreateInfo
->compareOp
)) |
2257 S_008F30_FORCE_UNNORMALIZED(pCreateInfo
->unnormalizedCoordinates
? 1 : 0) |
2258 S_008F30_ANISO_THRESHOLD(max_aniso_ratio
>> 1) |
2259 S_008F30_ANISO_BIAS(max_aniso_ratio
) |
2260 S_008F30_DISABLE_CUBE_WRAP(0) |
2261 S_008F30_COMPAT_MODE(is_vi
));
2262 sampler
->state
[1] = (S_008F34_MIN_LOD(S_FIXED(CLAMP(pCreateInfo
->minLod
, 0, 15), 8)) |
2263 S_008F34_MAX_LOD(S_FIXED(CLAMP(pCreateInfo
->maxLod
, 0, 15), 8)) |
2264 S_008F34_PERF_MIP(max_aniso_ratio
? max_aniso_ratio
+ 6 : 0));
2265 sampler
->state
[2] = (S_008F38_LOD_BIAS(S_FIXED(CLAMP(pCreateInfo
->mipLodBias
, -16, 16), 8)) |
2266 S_008F38_XY_MAG_FILTER(radv_tex_filter(pCreateInfo
->magFilter
, max_aniso
)) |
2267 S_008F38_XY_MIN_FILTER(radv_tex_filter(pCreateInfo
->minFilter
, max_aniso
)) |
2268 S_008F38_MIP_FILTER(radv_tex_mipfilter(pCreateInfo
->mipmapMode
)) |
2269 S_008F38_MIP_POINT_PRECLAMP(1) |
2270 S_008F38_DISABLE_LSB_CEIL(1) |
2271 S_008F38_FILTER_PREC_FIX(1) |
2272 S_008F38_ANISO_OVERRIDE(is_vi
));
2273 sampler
->state
[3] = (S_008F3C_BORDER_COLOR_PTR(0) |
2274 S_008F3C_BORDER_COLOR_TYPE(radv_tex_bordercolor(pCreateInfo
->borderColor
)));
2277 VkResult
radv_CreateSampler(
2279 const VkSamplerCreateInfo
* pCreateInfo
,
2280 const VkAllocationCallbacks
* pAllocator
,
2281 VkSampler
* pSampler
)
2283 RADV_FROM_HANDLE(radv_device
, device
, _device
);
2284 struct radv_sampler
*sampler
;
2286 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO
);
2288 sampler
= vk_alloc2(&device
->alloc
, pAllocator
, sizeof(*sampler
), 8,
2289 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
2291 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
2293 radv_init_sampler(device
, sampler
, pCreateInfo
);
2294 *pSampler
= radv_sampler_to_handle(sampler
);
2299 void radv_DestroySampler(
2302 const VkAllocationCallbacks
* pAllocator
)
2304 RADV_FROM_HANDLE(radv_device
, device
, _device
);
2305 RADV_FROM_HANDLE(radv_sampler
, sampler
, _sampler
);
2309 vk_free2(&device
->alloc
, pAllocator
, sampler
);
2313 /* vk_icd.h does not declare this function, so we declare it here to
2314 * suppress Wmissing-prototypes.
2316 PUBLIC VKAPI_ATTR VkResult VKAPI_CALL
2317 vk_icdNegotiateLoaderICDInterfaceVersion(uint32_t *pSupportedVersion
);
2319 PUBLIC VKAPI_ATTR VkResult VKAPI_CALL
2320 vk_icdNegotiateLoaderICDInterfaceVersion(uint32_t *pSupportedVersion
)
2322 /* For the full details on loader interface versioning, see
2323 * <https://github.com/KhronosGroup/Vulkan-LoaderAndValidationLayers/blob/master/loader/LoaderAndLayerInterface.md>.
2324 * What follows is a condensed summary, to help you navigate the large and
2325 * confusing official doc.
2327 * - Loader interface v0 is incompatible with later versions. We don't
2330 * - In loader interface v1:
2331 * - The first ICD entrypoint called by the loader is
2332 * vk_icdGetInstanceProcAddr(). The ICD must statically expose this
2334 * - The ICD must statically expose no other Vulkan symbol unless it is
2335 * linked with -Bsymbolic.
2336 * - Each dispatchable Vulkan handle created by the ICD must be
2337 * a pointer to a struct whose first member is VK_LOADER_DATA. The
2338 * ICD must initialize VK_LOADER_DATA.loadMagic to ICD_LOADER_MAGIC.
2339 * - The loader implements vkCreate{PLATFORM}SurfaceKHR() and
2340 * vkDestroySurfaceKHR(). The ICD must be capable of working with
2341 * such loader-managed surfaces.
2343 * - Loader interface v2 differs from v1 in:
2344 * - The first ICD entrypoint called by the loader is
2345 * vk_icdNegotiateLoaderICDInterfaceVersion(). The ICD must
2346 * statically expose this entrypoint.
2348 * - Loader interface v3 differs from v2 in:
2349 * - The ICD must implement vkCreate{PLATFORM}SurfaceKHR(),
2350 * vkDestroySurfaceKHR(), and other API which uses VKSurfaceKHR,
2351 * because the loader no longer does so.
2353 *pSupportedVersion
= MIN2(*pSupportedVersion
, 3u);