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
32 #include "radv_private.h"
33 #include "util/strtod.h"
37 #include <amdgpu_drm.h>
38 #include "amdgpu_id.h"
39 #include "winsys/amdgpu/radv_amdgpu_winsys_public.h"
40 #include "ac_llvm_util.h"
41 #include "vk_format.h"
43 #include "radv_timestamp.h"
44 #include "util/debug.h"
45 struct radv_dispatch_table dtable
;
48 struct radeon_winsys_fence
*fence
;
54 radv_physical_device_init(struct radv_physical_device
*device
,
55 struct radv_instance
*instance
,
59 drmVersionPtr version
;
62 fd
= open(path
, O_RDWR
| O_CLOEXEC
);
64 return vk_errorf(VK_ERROR_INCOMPATIBLE_DRIVER
,
65 "failed to open %s: %m", path
);
67 version
= drmGetVersion(fd
);
70 return vk_errorf(VK_ERROR_INCOMPATIBLE_DRIVER
,
71 "failed to get version %s: %m", path
);
74 if (strcmp(version
->name
, "amdgpu")) {
75 drmFreeVersion(version
);
77 return VK_ERROR_INCOMPATIBLE_DRIVER
;
79 drmFreeVersion(version
);
81 device
->_loader_data
.loaderMagic
= ICD_LOADER_MAGIC
;
82 device
->instance
= instance
;
83 assert(strlen(path
) < ARRAY_SIZE(device
->path
));
84 strncpy(device
->path
, path
, ARRAY_SIZE(device
->path
));
86 device
->ws
= radv_amdgpu_winsys_create(fd
);
88 result
= VK_ERROR_INCOMPATIBLE_DRIVER
;
91 device
->ws
->query_info(device
->ws
, &device
->rad_info
);
92 result
= radv_init_wsi(device
);
93 if (result
!= VK_SUCCESS
) {
94 device
->ws
->destroy(device
->ws
);
98 fprintf(stderr
, "WARNING: radv is not a conformant vulkan implementation, testing use only.\n");
99 device
->name
= device
->rad_info
.name
;
108 radv_physical_device_finish(struct radv_physical_device
*device
)
110 radv_finish_wsi(device
);
111 device
->ws
->destroy(device
->ws
);
114 static const VkExtensionProperties global_extensions
[] = {
116 .extensionName
= VK_KHR_SURFACE_EXTENSION_NAME
,
119 #ifdef VK_USE_PLATFORM_XCB_KHR
121 .extensionName
= VK_KHR_XCB_SURFACE_EXTENSION_NAME
,
125 #ifdef VK_USE_PLATFORM_WAYLAND_KHR
127 .extensionName
= VK_KHR_WAYLAND_SURFACE_EXTENSION_NAME
,
133 static const VkExtensionProperties device_extensions
[] = {
135 .extensionName
= VK_KHR_SWAPCHAIN_EXTENSION_NAME
,
141 default_alloc_func(void *pUserData
, size_t size
, size_t align
,
142 VkSystemAllocationScope allocationScope
)
148 default_realloc_func(void *pUserData
, void *pOriginal
, size_t size
,
149 size_t align
, VkSystemAllocationScope allocationScope
)
151 return realloc(pOriginal
, size
);
155 default_free_func(void *pUserData
, void *pMemory
)
160 static const VkAllocationCallbacks default_alloc
= {
162 .pfnAllocation
= default_alloc_func
,
163 .pfnReallocation
= default_realloc_func
,
164 .pfnFree
= default_free_func
,
167 VkResult
radv_CreateInstance(
168 const VkInstanceCreateInfo
* pCreateInfo
,
169 const VkAllocationCallbacks
* pAllocator
,
170 VkInstance
* pInstance
)
172 struct radv_instance
*instance
;
174 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO
);
176 uint32_t client_version
;
177 if (pCreateInfo
->pApplicationInfo
&&
178 pCreateInfo
->pApplicationInfo
->apiVersion
!= 0) {
179 client_version
= pCreateInfo
->pApplicationInfo
->apiVersion
;
181 client_version
= VK_MAKE_VERSION(1, 0, 0);
184 if (VK_MAKE_VERSION(1, 0, 0) > client_version
||
185 client_version
> VK_MAKE_VERSION(1, 0, 0xfff)) {
186 return vk_errorf(VK_ERROR_INCOMPATIBLE_DRIVER
,
187 "Client requested version %d.%d.%d",
188 VK_VERSION_MAJOR(client_version
),
189 VK_VERSION_MINOR(client_version
),
190 VK_VERSION_PATCH(client_version
));
193 for (uint32_t i
= 0; i
< pCreateInfo
->enabledExtensionCount
; i
++) {
195 for (uint32_t j
= 0; j
< ARRAY_SIZE(global_extensions
); j
++) {
196 if (strcmp(pCreateInfo
->ppEnabledExtensionNames
[i
],
197 global_extensions
[j
].extensionName
) == 0) {
203 return vk_error(VK_ERROR_EXTENSION_NOT_PRESENT
);
206 instance
= radv_alloc2(&default_alloc
, pAllocator
, sizeof(*instance
), 8,
207 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE
);
209 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
211 instance
->_loader_data
.loaderMagic
= ICD_LOADER_MAGIC
;
214 instance
->alloc
= *pAllocator
;
216 instance
->alloc
= default_alloc
;
218 instance
->apiVersion
= client_version
;
219 instance
->physicalDeviceCount
= -1;
223 VG(VALGRIND_CREATE_MEMPOOL(instance
, 0, false));
225 *pInstance
= radv_instance_to_handle(instance
);
230 void radv_DestroyInstance(
231 VkInstance _instance
,
232 const VkAllocationCallbacks
* pAllocator
)
234 RADV_FROM_HANDLE(radv_instance
, instance
, _instance
);
236 if (instance
->physicalDeviceCount
> 0) {
237 /* We support at most one physical device. */
238 assert(instance
->physicalDeviceCount
== 1);
239 radv_physical_device_finish(&instance
->physicalDevice
);
242 VG(VALGRIND_DESTROY_MEMPOOL(instance
));
246 radv_free(&instance
->alloc
, instance
);
249 VkResult
radv_EnumeratePhysicalDevices(
250 VkInstance _instance
,
251 uint32_t* pPhysicalDeviceCount
,
252 VkPhysicalDevice
* pPhysicalDevices
)
254 RADV_FROM_HANDLE(radv_instance
, instance
, _instance
);
257 if (instance
->physicalDeviceCount
< 0) {
259 for (unsigned i
= 0; i
< 8; i
++) {
260 snprintf(path
, sizeof(path
), "/dev/dri/renderD%d", 128 + i
);
261 result
= radv_physical_device_init(&instance
->physicalDevice
,
263 if (result
!= VK_ERROR_INCOMPATIBLE_DRIVER
)
267 if (result
== VK_ERROR_INCOMPATIBLE_DRIVER
) {
268 instance
->physicalDeviceCount
= 0;
269 } else if (result
== VK_SUCCESS
) {
270 instance
->physicalDeviceCount
= 1;
276 /* pPhysicalDeviceCount is an out parameter if pPhysicalDevices is NULL;
277 * otherwise it's an inout parameter.
279 * The Vulkan spec (git aaed022) says:
281 * pPhysicalDeviceCount is a pointer to an unsigned integer variable
282 * that is initialized with the number of devices the application is
283 * prepared to receive handles to. pname:pPhysicalDevices is pointer to
284 * an array of at least this many VkPhysicalDevice handles [...].
286 * Upon success, if pPhysicalDevices is NULL, vkEnumeratePhysicalDevices
287 * overwrites the contents of the variable pointed to by
288 * pPhysicalDeviceCount with the number of physical devices in in the
289 * instance; otherwise, vkEnumeratePhysicalDevices overwrites
290 * pPhysicalDeviceCount with the number of physical handles written to
293 if (!pPhysicalDevices
) {
294 *pPhysicalDeviceCount
= instance
->physicalDeviceCount
;
295 } else if (*pPhysicalDeviceCount
>= 1) {
296 pPhysicalDevices
[0] = radv_physical_device_to_handle(&instance
->physicalDevice
);
297 *pPhysicalDeviceCount
= 1;
298 } else if (*pPhysicalDeviceCount
< instance
->physicalDeviceCount
) {
299 return VK_INCOMPLETE
;
301 *pPhysicalDeviceCount
= 0;
307 void radv_GetPhysicalDeviceFeatures(
308 VkPhysicalDevice physicalDevice
,
309 VkPhysicalDeviceFeatures
* pFeatures
)
311 // RADV_FROM_HANDLE(radv_physical_device, pdevice, physicalDevice);
313 memset(pFeatures
, 0, sizeof(*pFeatures
));
315 *pFeatures
= (VkPhysicalDeviceFeatures
) {
316 .robustBufferAccess
= true,
317 .fullDrawIndexUint32
= true,
318 .imageCubeArray
= true,
319 .independentBlend
= true,
320 .geometryShader
= false,
321 .tessellationShader
= false,
322 .sampleRateShading
= false,
323 .dualSrcBlend
= true,
325 .multiDrawIndirect
= true,
326 .drawIndirectFirstInstance
= true,
328 .depthBiasClamp
= true,
329 .fillModeNonSolid
= true,
334 .multiViewport
= false,
335 .samplerAnisotropy
= false, /* FINISHME */
336 .textureCompressionETC2
= false,
337 .textureCompressionASTC_LDR
= false,
338 .textureCompressionBC
= true,
339 .occlusionQueryPrecise
= true,
340 .pipelineStatisticsQuery
= false,
341 .vertexPipelineStoresAndAtomics
= true,
342 .fragmentStoresAndAtomics
= true,
343 .shaderTessellationAndGeometryPointSize
= true,
344 .shaderImageGatherExtended
= false,
345 .shaderStorageImageExtendedFormats
= false,
346 .shaderStorageImageMultisample
= false,
347 .shaderUniformBufferArrayDynamicIndexing
= true,
348 .shaderSampledImageArrayDynamicIndexing
= true,
349 .shaderStorageBufferArrayDynamicIndexing
= true,
350 .shaderStorageImageArrayDynamicIndexing
= true,
351 .shaderStorageImageReadWithoutFormat
= false,
352 .shaderStorageImageWriteWithoutFormat
= true,
353 .shaderClipDistance
= true,
354 .shaderCullDistance
= true,
355 .shaderFloat64
= false,
356 .shaderInt64
= false,
357 .shaderInt16
= false,
359 .variableMultisampleRate
= false,
360 .inheritedQueries
= false,
365 radv_device_get_cache_uuid(void *uuid
)
367 memset(uuid
, 0, VK_UUID_SIZE
);
368 snprintf(uuid
, VK_UUID_SIZE
, "radv-%s", RADV_TIMESTAMP
);
371 void radv_GetPhysicalDeviceProperties(
372 VkPhysicalDevice physicalDevice
,
373 VkPhysicalDeviceProperties
* pProperties
)
375 RADV_FROM_HANDLE(radv_physical_device
, pdevice
, physicalDevice
);
376 VkSampleCountFlags sample_counts
= 0xf;
377 VkPhysicalDeviceLimits limits
= {
378 .maxImageDimension1D
= (1 << 14),
379 .maxImageDimension2D
= (1 << 14),
380 .maxImageDimension3D
= (1 << 11),
381 .maxImageDimensionCube
= (1 << 14),
382 .maxImageArrayLayers
= (1 << 11),
383 .maxTexelBufferElements
= 128 * 1024 * 1024,
384 .maxUniformBufferRange
= UINT32_MAX
,
385 .maxStorageBufferRange
= UINT32_MAX
,
386 .maxPushConstantsSize
= MAX_PUSH_CONSTANTS_SIZE
,
387 .maxMemoryAllocationCount
= UINT32_MAX
,
388 .maxSamplerAllocationCount
= 64 * 1024,
389 .bufferImageGranularity
= 64, /* A cache line */
390 .sparseAddressSpaceSize
= 0,
391 .maxBoundDescriptorSets
= MAX_SETS
,
392 .maxPerStageDescriptorSamplers
= 64,
393 .maxPerStageDescriptorUniformBuffers
= 64,
394 .maxPerStageDescriptorStorageBuffers
= 64,
395 .maxPerStageDescriptorSampledImages
= 64,
396 .maxPerStageDescriptorStorageImages
= 64,
397 .maxPerStageDescriptorInputAttachments
= 64,
398 .maxPerStageResources
= 128,
399 .maxDescriptorSetSamplers
= 256,
400 .maxDescriptorSetUniformBuffers
= 256,
401 .maxDescriptorSetUniformBuffersDynamic
= 256,
402 .maxDescriptorSetStorageBuffers
= 256,
403 .maxDescriptorSetStorageBuffersDynamic
= 256,
404 .maxDescriptorSetSampledImages
= 256,
405 .maxDescriptorSetStorageImages
= 256,
406 .maxDescriptorSetInputAttachments
= 256,
407 .maxVertexInputAttributes
= 32,
408 .maxVertexInputBindings
= 32,
409 .maxVertexInputAttributeOffset
= 2047,
410 .maxVertexInputBindingStride
= 2048,
411 .maxVertexOutputComponents
= 128,
412 .maxTessellationGenerationLevel
= 0,
413 .maxTessellationPatchSize
= 0,
414 .maxTessellationControlPerVertexInputComponents
= 0,
415 .maxTessellationControlPerVertexOutputComponents
= 0,
416 .maxTessellationControlPerPatchOutputComponents
= 0,
417 .maxTessellationControlTotalOutputComponents
= 0,
418 .maxTessellationEvaluationInputComponents
= 0,
419 .maxTessellationEvaluationOutputComponents
= 0,
420 .maxGeometryShaderInvocations
= 32,
421 .maxGeometryInputComponents
= 64,
422 .maxGeometryOutputComponents
= 128,
423 .maxGeometryOutputVertices
= 256,
424 .maxGeometryTotalOutputComponents
= 1024,
425 .maxFragmentInputComponents
= 128,
426 .maxFragmentOutputAttachments
= 8,
427 .maxFragmentDualSrcAttachments
= 2,
428 .maxFragmentCombinedOutputResources
= 8,
429 .maxComputeSharedMemorySize
= 32768,
430 .maxComputeWorkGroupCount
= { 65535, 65535, 65535 },
431 .maxComputeWorkGroupInvocations
= 16 * 1024,
432 .maxComputeWorkGroupSize
= {
433 16 * 1024/*devinfo->max_cs_threads*/,
437 .subPixelPrecisionBits
= 4 /* FIXME */,
438 .subTexelPrecisionBits
= 4 /* FIXME */,
439 .mipmapPrecisionBits
= 4 /* FIXME */,
440 .maxDrawIndexedIndexValue
= UINT32_MAX
,
441 .maxDrawIndirectCount
= UINT32_MAX
,
442 .maxSamplerLodBias
= 16,
443 .maxSamplerAnisotropy
= 16,
444 .maxViewports
= MAX_VIEWPORTS
,
445 .maxViewportDimensions
= { (1 << 14), (1 << 14) },
446 .viewportBoundsRange
= { INT16_MIN
, INT16_MAX
},
447 .viewportSubPixelBits
= 13, /* We take a float? */
448 .minMemoryMapAlignment
= 4096, /* A page */
449 .minTexelBufferOffsetAlignment
= 1,
450 .minUniformBufferOffsetAlignment
= 4,
451 .minStorageBufferOffsetAlignment
= 4,
452 .minTexelOffset
= -8,
454 .minTexelGatherOffset
= -8,
455 .maxTexelGatherOffset
= 7,
456 .minInterpolationOffset
= 0, /* FIXME */
457 .maxInterpolationOffset
= 0, /* FIXME */
458 .subPixelInterpolationOffsetBits
= 0, /* FIXME */
459 .maxFramebufferWidth
= (1 << 14),
460 .maxFramebufferHeight
= (1 << 14),
461 .maxFramebufferLayers
= (1 << 10),
462 .framebufferColorSampleCounts
= sample_counts
,
463 .framebufferDepthSampleCounts
= sample_counts
,
464 .framebufferStencilSampleCounts
= sample_counts
,
465 .framebufferNoAttachmentsSampleCounts
= sample_counts
,
466 .maxColorAttachments
= MAX_RTS
,
467 .sampledImageColorSampleCounts
= sample_counts
,
468 .sampledImageIntegerSampleCounts
= VK_SAMPLE_COUNT_1_BIT
,
469 .sampledImageDepthSampleCounts
= sample_counts
,
470 .sampledImageStencilSampleCounts
= sample_counts
,
471 .storageImageSampleCounts
= VK_SAMPLE_COUNT_1_BIT
,
472 .maxSampleMaskWords
= 1,
473 .timestampComputeAndGraphics
= false,
474 .timestampPeriod
= 100000.0 / pdevice
->rad_info
.clock_crystal_freq
,
475 .maxClipDistances
= 8,
476 .maxCullDistances
= 8,
477 .maxCombinedClipAndCullDistances
= 8,
478 .discreteQueuePriorities
= 1,
479 .pointSizeRange
= { 0.125, 255.875 },
480 .lineWidthRange
= { 0.0, 7.9921875 },
481 .pointSizeGranularity
= (1.0 / 8.0),
482 .lineWidthGranularity
= (1.0 / 128.0),
483 .strictLines
= false, /* FINISHME */
484 .standardSampleLocations
= true,
485 .optimalBufferCopyOffsetAlignment
= 128,
486 .optimalBufferCopyRowPitchAlignment
= 128,
487 .nonCoherentAtomSize
= 64,
490 *pProperties
= (VkPhysicalDeviceProperties
) {
491 .apiVersion
= VK_MAKE_VERSION(1, 0, 5),
494 .deviceID
= pdevice
->rad_info
.pci_id
,
495 .deviceType
= VK_PHYSICAL_DEVICE_TYPE_DISCRETE_GPU
,
497 .sparseProperties
= {0}, /* Broadwell doesn't do sparse. */
500 strcpy(pProperties
->deviceName
, pdevice
->name
);
501 radv_device_get_cache_uuid(pProperties
->pipelineCacheUUID
);
504 void radv_GetPhysicalDeviceQueueFamilyProperties(
505 VkPhysicalDevice physicalDevice
,
507 VkQueueFamilyProperties
* pQueueFamilyProperties
)
509 if (pQueueFamilyProperties
== NULL
) {
513 assert(*pCount
>= 1);
515 *pQueueFamilyProperties
= (VkQueueFamilyProperties
) {
516 .queueFlags
= VK_QUEUE_GRAPHICS_BIT
|
517 VK_QUEUE_COMPUTE_BIT
|
518 VK_QUEUE_TRANSFER_BIT
,
520 .timestampValidBits
= 64,
521 .minImageTransferGranularity
= (VkExtent3D
) { 1, 1, 1 },
525 void radv_GetPhysicalDeviceMemoryProperties(
526 VkPhysicalDevice physicalDevice
,
527 VkPhysicalDeviceMemoryProperties
* pMemoryProperties
)
529 RADV_FROM_HANDLE(radv_physical_device
, physical_device
, physicalDevice
);
531 pMemoryProperties
->memoryTypeCount
= 3;
532 pMemoryProperties
->memoryTypes
[0] = (VkMemoryType
) {
533 .propertyFlags
= VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT
,
536 pMemoryProperties
->memoryTypes
[1] = (VkMemoryType
) {
537 .propertyFlags
= VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT
|
538 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT
|
539 VK_MEMORY_PROPERTY_HOST_COHERENT_BIT
,
542 pMemoryProperties
->memoryTypes
[2] = (VkMemoryType
) {
543 .propertyFlags
= VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT
|
544 VK_MEMORY_PROPERTY_HOST_COHERENT_BIT
|
545 VK_MEMORY_PROPERTY_HOST_CACHED_BIT
,
549 pMemoryProperties
->memoryHeapCount
= 2;
550 pMemoryProperties
->memoryHeaps
[0] = (VkMemoryHeap
) {
551 .size
= physical_device
->rad_info
.vram_size
,
552 .flags
= VK_MEMORY_HEAP_DEVICE_LOCAL_BIT
,
554 pMemoryProperties
->memoryHeaps
[1] = (VkMemoryHeap
) {
555 .size
= physical_device
->rad_info
.gart_size
,
561 radv_queue_init(struct radv_device
*device
, struct radv_queue
*queue
)
563 queue
->_loader_data
.loaderMagic
= ICD_LOADER_MAGIC
;
564 queue
->device
= device
;
570 radv_queue_finish(struct radv_queue
*queue
)
574 VkResult
radv_CreateDevice(
575 VkPhysicalDevice physicalDevice
,
576 const VkDeviceCreateInfo
* pCreateInfo
,
577 const VkAllocationCallbacks
* pAllocator
,
580 RADV_FROM_HANDLE(radv_physical_device
, physical_device
, physicalDevice
);
582 struct radv_device
*device
;
584 for (uint32_t i
= 0; i
< pCreateInfo
->enabledExtensionCount
; i
++) {
586 for (uint32_t j
= 0; j
< ARRAY_SIZE(device_extensions
); j
++) {
587 if (strcmp(pCreateInfo
->ppEnabledExtensionNames
[i
],
588 device_extensions
[j
].extensionName
) == 0) {
594 return vk_error(VK_ERROR_EXTENSION_NOT_PRESENT
);
597 device
= radv_alloc2(&physical_device
->instance
->alloc
, pAllocator
,
599 VK_SYSTEM_ALLOCATION_SCOPE_DEVICE
);
601 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
603 device
->_loader_data
.loaderMagic
= ICD_LOADER_MAGIC
;
604 device
->instance
= physical_device
->instance
;
606 device
->ws
= physical_device
->ws
;
608 device
->alloc
= *pAllocator
;
610 device
->alloc
= physical_device
->instance
->alloc
;
612 device
->hw_ctx
= device
->ws
->ctx_create(device
->ws
);
613 if (!device
->hw_ctx
) {
614 result
= VK_ERROR_OUT_OF_HOST_MEMORY
;
618 radv_queue_init(device
, &device
->queue
);
620 result
= radv_device_init_meta(device
);
621 if (result
!= VK_SUCCESS
) {
622 device
->ws
->ctx_destroy(device
->hw_ctx
);
625 device
->allow_fast_clears
= env_var_as_boolean("RADV_FAST_CLEARS", false);
626 device
->allow_dcc
= !env_var_as_boolean("RADV_DCC_DISABLE", false);
628 if (device
->allow_fast_clears
&& device
->allow_dcc
)
629 radv_finishme("DCC fast clears have not been tested\n");
631 radv_device_init_msaa(device
);
632 device
->empty_cs
= device
->ws
->cs_create(device
->ws
, RING_GFX
);
633 radeon_emit(device
->empty_cs
, PKT3(PKT3_CONTEXT_CONTROL
, 1, 0));
634 radeon_emit(device
->empty_cs
, CONTEXT_CONTROL_LOAD_ENABLE(1));
635 radeon_emit(device
->empty_cs
, CONTEXT_CONTROL_SHADOW_ENABLE(1));
636 device
->ws
->cs_finalize(device
->empty_cs
);
637 *pDevice
= radv_device_to_handle(device
);
640 radv_free(&device
->alloc
, device
);
644 void radv_DestroyDevice(
646 const VkAllocationCallbacks
* pAllocator
)
648 RADV_FROM_HANDLE(radv_device
, device
, _device
);
650 device
->ws
->ctx_destroy(device
->hw_ctx
);
651 radv_queue_finish(&device
->queue
);
652 radv_device_finish_meta(device
);
654 radv_free(&device
->alloc
, device
);
657 VkResult
radv_EnumerateInstanceExtensionProperties(
658 const char* pLayerName
,
659 uint32_t* pPropertyCount
,
660 VkExtensionProperties
* pProperties
)
663 if (pProperties
== NULL
) {
664 *pPropertyCount
= ARRAY_SIZE(global_extensions
);
668 for (i
= 0; i
< *pPropertyCount
; i
++)
669 memcpy(&pProperties
[i
], &global_extensions
[i
], sizeof(VkExtensionProperties
));
672 if (i
< ARRAY_SIZE(global_extensions
))
673 return VK_INCOMPLETE
;
678 VkResult
radv_EnumerateDeviceExtensionProperties(
679 VkPhysicalDevice physicalDevice
,
680 const char* pLayerName
,
681 uint32_t* pPropertyCount
,
682 VkExtensionProperties
* pProperties
)
686 if (pProperties
== NULL
) {
687 *pPropertyCount
= ARRAY_SIZE(device_extensions
);
691 for (i
= 0; i
< *pPropertyCount
; i
++)
692 memcpy(&pProperties
[i
], &device_extensions
[i
], sizeof(VkExtensionProperties
));
695 if (i
< ARRAY_SIZE(device_extensions
))
696 return VK_INCOMPLETE
;
700 VkResult
radv_EnumerateInstanceLayerProperties(
701 uint32_t* pPropertyCount
,
702 VkLayerProperties
* pProperties
)
704 if (pProperties
== NULL
) {
709 /* None supported at this time */
710 return vk_error(VK_ERROR_LAYER_NOT_PRESENT
);
713 VkResult
radv_EnumerateDeviceLayerProperties(
714 VkPhysicalDevice physicalDevice
,
715 uint32_t* pPropertyCount
,
716 VkLayerProperties
* pProperties
)
718 if (pProperties
== NULL
) {
723 /* None supported at this time */
724 return vk_error(VK_ERROR_LAYER_NOT_PRESENT
);
727 void radv_GetDeviceQueue(
729 uint32_t queueNodeIndex
,
733 RADV_FROM_HANDLE(radv_device
, device
, _device
);
735 assert(queueIndex
== 0);
737 *pQueue
= radv_queue_to_handle(&device
->queue
);
740 VkResult
radv_QueueSubmit(
742 uint32_t submitCount
,
743 const VkSubmitInfo
* pSubmits
,
746 RADV_FROM_HANDLE(radv_queue
, queue
, _queue
);
747 RADV_FROM_HANDLE(radv_fence
, fence
, _fence
);
748 struct radeon_winsys_fence
*base_fence
= fence
? fence
->fence
: NULL
;
749 struct radeon_winsys_ctx
*ctx
= queue
->device
->hw_ctx
;
752 for (uint32_t i
= 0; i
< submitCount
; i
++) {
753 struct radeon_winsys_cs
**cs_array
;
754 bool can_patch
= true;
756 if (!pSubmits
[i
].commandBufferCount
)
759 cs_array
= malloc(sizeof(struct radeon_winsys_cs
*) *
760 pSubmits
[i
].commandBufferCount
);
762 for (uint32_t j
= 0; j
< pSubmits
[i
].commandBufferCount
; j
++) {
763 RADV_FROM_HANDLE(radv_cmd_buffer
, cmd_buffer
,
764 pSubmits
[i
].pCommandBuffers
[j
]);
765 assert(cmd_buffer
->level
== VK_COMMAND_BUFFER_LEVEL_PRIMARY
);
767 cs_array
[j
] = cmd_buffer
->cs
;
768 if ((cmd_buffer
->usage_flags
& VK_COMMAND_BUFFER_USAGE_SIMULTANEOUS_USE_BIT
))
771 ret
= queue
->device
->ws
->cs_submit(ctx
, cs_array
,
772 pSubmits
[i
].commandBufferCount
,
773 can_patch
, base_fence
);
775 radv_loge("failed to submit CS %d\n", i
);
781 ret
= queue
->device
->ws
->cs_submit(ctx
, &queue
->device
->empty_cs
,
782 1, false, base_fence
);
784 fence
->submitted
= true;
790 VkResult
radv_QueueWaitIdle(
793 RADV_FROM_HANDLE(radv_queue
, queue
, _queue
);
795 queue
->device
->ws
->ctx_wait_idle(queue
->device
->hw_ctx
);
799 VkResult
radv_DeviceWaitIdle(
802 RADV_FROM_HANDLE(radv_device
, device
, _device
);
804 device
->ws
->ctx_wait_idle(device
->hw_ctx
);
808 PFN_vkVoidFunction
radv_GetInstanceProcAddr(
812 return radv_lookup_entrypoint(pName
);
815 /* The loader wants us to expose a second GetInstanceProcAddr function
816 * to work around certain LD_PRELOAD issues seen in apps.
819 VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL
vk_icdGetInstanceProcAddr(
824 VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL
vk_icdGetInstanceProcAddr(
828 return radv_GetInstanceProcAddr(instance
, pName
);
831 PFN_vkVoidFunction
radv_GetDeviceProcAddr(
835 return radv_lookup_entrypoint(pName
);
838 VkResult
radv_AllocateMemory(
840 const VkMemoryAllocateInfo
* pAllocateInfo
,
841 const VkAllocationCallbacks
* pAllocator
,
842 VkDeviceMemory
* pMem
)
844 RADV_FROM_HANDLE(radv_device
, device
, _device
);
845 struct radv_device_memory
*mem
;
847 enum radeon_bo_domain domain
;
849 assert(pAllocateInfo
->sType
== VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO
);
851 if (pAllocateInfo
->allocationSize
== 0) {
852 /* Apparently, this is allowed */
853 *pMem
= VK_NULL_HANDLE
;
857 mem
= radv_alloc2(&device
->alloc
, pAllocator
, sizeof(*mem
), 8,
858 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
860 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
862 uint64_t alloc_size
= align_u64(pAllocateInfo
->allocationSize
, 4096);
863 if (pAllocateInfo
->memoryTypeIndex
== 2)
864 domain
= RADEON_DOMAIN_GTT
;
866 domain
= RADEON_DOMAIN_VRAM
;
868 if (pAllocateInfo
->memoryTypeIndex
== 0)
869 flags
|= RADEON_FLAG_NO_CPU_ACCESS
;
871 flags
|= RADEON_FLAG_CPU_ACCESS
;
872 mem
->bo
= device
->ws
->buffer_create(device
->ws
, alloc_size
, 32768,
876 result
= VK_ERROR_OUT_OF_DEVICE_MEMORY
;
879 mem
->type_index
= pAllocateInfo
->memoryTypeIndex
;
881 *pMem
= radv_device_memory_to_handle(mem
);
886 radv_free2(&device
->alloc
, pAllocator
, mem
);
891 void radv_FreeMemory(
894 const VkAllocationCallbacks
* pAllocator
)
896 RADV_FROM_HANDLE(radv_device
, device
, _device
);
897 RADV_FROM_HANDLE(radv_device_memory
, mem
, _mem
);
902 device
->ws
->buffer_destroy(mem
->bo
);
905 radv_free2(&device
->alloc
, pAllocator
, mem
);
908 VkResult
radv_MapMemory(
910 VkDeviceMemory _memory
,
913 VkMemoryMapFlags flags
,
916 RADV_FROM_HANDLE(radv_device
, device
, _device
);
917 RADV_FROM_HANDLE(radv_device_memory
, mem
, _memory
);
924 *ppData
= device
->ws
->buffer_map(mem
->bo
);
930 return VK_ERROR_MEMORY_MAP_FAILED
;
933 void radv_UnmapMemory(
935 VkDeviceMemory _memory
)
937 RADV_FROM_HANDLE(radv_device
, device
, _device
);
938 RADV_FROM_HANDLE(radv_device_memory
, mem
, _memory
);
943 device
->ws
->buffer_unmap(mem
->bo
);
946 VkResult
radv_FlushMappedMemoryRanges(
948 uint32_t memoryRangeCount
,
949 const VkMappedMemoryRange
* pMemoryRanges
)
954 VkResult
radv_InvalidateMappedMemoryRanges(
956 uint32_t memoryRangeCount
,
957 const VkMappedMemoryRange
* pMemoryRanges
)
962 void radv_GetBufferMemoryRequirements(
965 VkMemoryRequirements
* pMemoryRequirements
)
967 RADV_FROM_HANDLE(radv_buffer
, buffer
, _buffer
);
969 /* The Vulkan spec (git aaed022) says:
971 * memoryTypeBits is a bitfield and contains one bit set for every
972 * supported memory type for the resource. The bit `1<<i` is set if and
973 * only if the memory type `i` in the VkPhysicalDeviceMemoryProperties
974 * structure for the physical device is supported.
976 * We support exactly one memory type.
978 pMemoryRequirements
->memoryTypeBits
= 0x7;
980 pMemoryRequirements
->size
= buffer
->size
;
981 pMemoryRequirements
->alignment
= 16;
984 void radv_GetImageMemoryRequirements(
987 VkMemoryRequirements
* pMemoryRequirements
)
989 RADV_FROM_HANDLE(radv_image
, image
, _image
);
991 /* The Vulkan spec (git aaed022) says:
993 * memoryTypeBits is a bitfield and contains one bit set for every
994 * supported memory type for the resource. The bit `1<<i` is set if and
995 * only if the memory type `i` in the VkPhysicalDeviceMemoryProperties
996 * structure for the physical device is supported.
998 * We support exactly one memory type.
1000 pMemoryRequirements
->memoryTypeBits
= 0x7;
1002 pMemoryRequirements
->size
= image
->size
;
1003 pMemoryRequirements
->alignment
= image
->alignment
;
1006 void radv_GetImageSparseMemoryRequirements(
1009 uint32_t* pSparseMemoryRequirementCount
,
1010 VkSparseImageMemoryRequirements
* pSparseMemoryRequirements
)
1015 void radv_GetDeviceMemoryCommitment(
1017 VkDeviceMemory memory
,
1018 VkDeviceSize
* pCommittedMemoryInBytes
)
1020 *pCommittedMemoryInBytes
= 0;
1023 VkResult
radv_BindBufferMemory(
1026 VkDeviceMemory _memory
,
1027 VkDeviceSize memoryOffset
)
1029 RADV_FROM_HANDLE(radv_device_memory
, mem
, _memory
);
1030 RADV_FROM_HANDLE(radv_buffer
, buffer
, _buffer
);
1033 buffer
->bo
= mem
->bo
;
1034 buffer
->offset
= memoryOffset
;
1043 VkResult
radv_BindImageMemory(
1046 VkDeviceMemory _memory
,
1047 VkDeviceSize memoryOffset
)
1049 RADV_FROM_HANDLE(radv_device_memory
, mem
, _memory
);
1050 RADV_FROM_HANDLE(radv_image
, image
, _image
);
1053 image
->bo
= mem
->bo
;
1054 image
->offset
= memoryOffset
;
1063 VkResult
radv_QueueBindSparse(
1065 uint32_t bindInfoCount
,
1066 const VkBindSparseInfo
* pBindInfo
,
1069 stub_return(VK_ERROR_INCOMPATIBLE_DRIVER
);
1072 VkResult
radv_CreateFence(
1074 const VkFenceCreateInfo
* pCreateInfo
,
1075 const VkAllocationCallbacks
* pAllocator
,
1078 RADV_FROM_HANDLE(radv_device
, device
, _device
);
1079 struct radv_fence
*fence
= radv_alloc2(&device
->alloc
, pAllocator
,
1081 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
1084 return VK_ERROR_OUT_OF_HOST_MEMORY
;
1086 memset(fence
, 0, sizeof(*fence
));
1087 fence
->submitted
= false;
1088 fence
->signalled
= !!(pCreateInfo
->flags
& VK_FENCE_CREATE_SIGNALED_BIT
);
1089 fence
->fence
= device
->ws
->create_fence();
1092 *pFence
= radv_fence_to_handle(fence
);
1097 void radv_DestroyFence(
1100 const VkAllocationCallbacks
* pAllocator
)
1102 RADV_FROM_HANDLE(radv_device
, device
, _device
);
1103 RADV_FROM_HANDLE(radv_fence
, fence
, _fence
);
1107 device
->ws
->destroy_fence(fence
->fence
);
1108 radv_free2(&device
->alloc
, pAllocator
, fence
);
1111 static uint64_t radv_get_absolute_timeout(uint64_t timeout
)
1113 uint64_t current_time
;
1116 clock_gettime(CLOCK_MONOTONIC
, &tv
);
1117 current_time
= tv
.tv_nsec
+ tv
.tv_sec
*1000000000ull;
1119 timeout
= MIN2(UINT64_MAX
- current_time
, timeout
);
1121 return current_time
+ timeout
;
1124 VkResult
radv_WaitForFences(
1126 uint32_t fenceCount
,
1127 const VkFence
* pFences
,
1131 RADV_FROM_HANDLE(radv_device
, device
, _device
);
1132 timeout
= radv_get_absolute_timeout(timeout
);
1134 if (!waitAll
&& fenceCount
> 1) {
1135 fprintf(stderr
, "radv: WaitForFences without waitAll not implemented yet\n");
1138 for (uint32_t i
= 0; i
< fenceCount
; ++i
) {
1139 RADV_FROM_HANDLE(radv_fence
, fence
, pFences
[i
]);
1140 bool expired
= false;
1142 if (fence
->signalled
)
1145 if (!fence
->submitted
)
1148 expired
= device
->ws
->fence_wait(device
->ws
, fence
->fence
, true, timeout
);
1152 fence
->signalled
= true;
1158 VkResult
radv_ResetFences(VkDevice device
,
1159 uint32_t fenceCount
,
1160 const VkFence
*pFences
)
1162 for (unsigned i
= 0; i
< fenceCount
; ++i
) {
1163 RADV_FROM_HANDLE(radv_fence
, fence
, pFences
[i
]);
1164 fence
->submitted
= fence
->signalled
= false;
1170 VkResult
radv_GetFenceStatus(VkDevice _device
, VkFence _fence
)
1172 RADV_FROM_HANDLE(radv_device
, device
, _device
);
1173 RADV_FROM_HANDLE(radv_fence
, fence
, _fence
);
1175 if (!fence
->submitted
)
1176 return VK_NOT_READY
;
1178 if (!device
->ws
->fence_wait(device
->ws
, fence
->fence
, false, 0))
1179 return VK_NOT_READY
;
1185 // Queue semaphore functions
1187 VkResult
radv_CreateSemaphore(
1189 const VkSemaphoreCreateInfo
* pCreateInfo
,
1190 const VkAllocationCallbacks
* pAllocator
,
1191 VkSemaphore
* pSemaphore
)
1193 /* The DRM execbuffer ioctl always execute in-oder, even between different
1194 * rings. As such, there's nothing to do for the user space semaphore.
1197 *pSemaphore
= (VkSemaphore
)1;
1202 void radv_DestroySemaphore(
1204 VkSemaphore semaphore
,
1205 const VkAllocationCallbacks
* pAllocator
)
1209 VkResult
radv_CreateEvent(
1211 const VkEventCreateInfo
* pCreateInfo
,
1212 const VkAllocationCallbacks
* pAllocator
,
1215 RADV_FROM_HANDLE(radv_device
, device
, _device
);
1216 struct radv_event
*event
= radv_alloc2(&device
->alloc
, pAllocator
,
1218 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
1221 return VK_ERROR_OUT_OF_HOST_MEMORY
;
1223 event
->bo
= device
->ws
->buffer_create(device
->ws
, 8, 8,
1225 RADEON_FLAG_CPU_ACCESS
);
1227 radv_free2(&device
->alloc
, pAllocator
, event
);
1228 return VK_ERROR_OUT_OF_DEVICE_MEMORY
;
1231 event
->map
= (uint64_t*)device
->ws
->buffer_map(event
->bo
);
1233 *pEvent
= radv_event_to_handle(event
);
1238 void radv_DestroyEvent(
1241 const VkAllocationCallbacks
* pAllocator
)
1243 RADV_FROM_HANDLE(radv_device
, device
, _device
);
1244 RADV_FROM_HANDLE(radv_event
, event
, _event
);
1248 device
->ws
->buffer_destroy(event
->bo
);
1249 radv_free2(&device
->alloc
, pAllocator
, event
);
1252 VkResult
radv_GetEventStatus(
1256 RADV_FROM_HANDLE(radv_event
, event
, _event
);
1258 if (*event
->map
== 1)
1259 return VK_EVENT_SET
;
1260 return VK_EVENT_RESET
;
1263 VkResult
radv_SetEvent(
1267 RADV_FROM_HANDLE(radv_event
, event
, _event
);
1273 VkResult
radv_ResetEvent(
1277 RADV_FROM_HANDLE(radv_event
, event
, _event
);
1283 VkResult
radv_CreateBuffer(
1285 const VkBufferCreateInfo
* pCreateInfo
,
1286 const VkAllocationCallbacks
* pAllocator
,
1289 RADV_FROM_HANDLE(radv_device
, device
, _device
);
1290 struct radv_buffer
*buffer
;
1292 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO
);
1294 buffer
= radv_alloc2(&device
->alloc
, pAllocator
, sizeof(*buffer
), 8,
1295 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
1297 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
1299 buffer
->size
= pCreateInfo
->size
;
1300 buffer
->usage
= pCreateInfo
->usage
;
1304 *pBuffer
= radv_buffer_to_handle(buffer
);
1309 void radv_DestroyBuffer(
1312 const VkAllocationCallbacks
* pAllocator
)
1314 RADV_FROM_HANDLE(radv_device
, device
, _device
);
1315 RADV_FROM_HANDLE(radv_buffer
, buffer
, _buffer
);
1320 radv_free2(&device
->alloc
, pAllocator
, buffer
);
1323 static inline unsigned
1324 si_tile_mode_index(const struct radv_image
*image
, unsigned level
, bool stencil
)
1327 return image
->surface
.stencil_tiling_index
[level
];
1329 return image
->surface
.tiling_index
[level
];
1333 radv_initialise_color_surface(struct radv_device
*device
,
1334 struct radv_color_buffer_info
*cb
,
1335 struct radv_image_view
*iview
)
1337 const struct vk_format_description
*desc
;
1338 unsigned ntype
, format
, swap
, endian
;
1339 unsigned blend_clamp
= 0, blend_bypass
= 0;
1340 unsigned pitch_tile_max
, slice_tile_max
, tile_mode_index
;
1342 const struct radeon_surf
*surf
= &iview
->image
->surface
;
1343 const struct radeon_surf_level
*level_info
= &surf
->level
[iview
->base_mip
];
1345 desc
= vk_format_description(iview
->vk_format
);
1347 memset(cb
, 0, sizeof(*cb
));
1349 va
= device
->ws
->buffer_get_va(iview
->bo
) + iview
->image
->offset
;
1350 va
+= level_info
->offset
;
1351 cb
->cb_color_base
= va
>> 8;
1353 /* CMASK variables */
1354 va
= device
->ws
->buffer_get_va(iview
->bo
) + iview
->image
->offset
;
1355 va
+= iview
->image
->cmask
.offset
;
1356 cb
->cb_color_cmask
= va
>> 8;
1357 cb
->cb_color_cmask_slice
= iview
->image
->cmask
.slice_tile_max
;
1359 va
= device
->ws
->buffer_get_va(iview
->bo
) + iview
->image
->offset
;
1360 va
+= iview
->image
->dcc_offset
;
1361 cb
->cb_dcc_base
= va
>> 8;
1363 cb
->cb_color_view
= S_028C6C_SLICE_START(iview
->base_layer
) |
1364 S_028C6C_SLICE_MAX(iview
->base_layer
+ iview
->extent
.depth
- 1);
1366 cb
->micro_tile_mode
= iview
->image
->surface
.micro_tile_mode
;
1367 pitch_tile_max
= level_info
->nblk_x
/ 8 - 1;
1368 slice_tile_max
= (level_info
->nblk_x
* level_info
->nblk_y
) / 64 - 1;
1369 tile_mode_index
= si_tile_mode_index(iview
->image
, iview
->base_mip
, false);
1371 cb
->cb_color_pitch
= S_028C64_TILE_MAX(pitch_tile_max
);
1372 cb
->cb_color_slice
= S_028C68_TILE_MAX(slice_tile_max
);
1374 /* Intensity is implemented as Red, so treat it that way. */
1375 cb
->cb_color_attrib
= S_028C74_FORCE_DST_ALPHA_1(desc
->swizzle
[3] == VK_SWIZZLE_1
) |
1376 S_028C74_TILE_MODE_INDEX(tile_mode_index
);
1378 if (iview
->image
->samples
> 1) {
1379 unsigned log_samples
= util_logbase2(iview
->image
->samples
);
1381 cb
->cb_color_attrib
|= S_028C74_NUM_SAMPLES(log_samples
) |
1382 S_028C74_NUM_FRAGMENTS(log_samples
);
1385 if (iview
->image
->fmask
.size
) {
1386 va
= device
->ws
->buffer_get_va(iview
->bo
) + iview
->image
->offset
+ iview
->image
->fmask
.offset
;
1387 if (device
->instance
->physicalDevice
.rad_info
.chip_class
>= CIK
)
1388 cb
->cb_color_pitch
|= S_028C64_FMASK_TILE_MAX(iview
->image
->fmask
.pitch_in_pixels
/ 8 - 1);
1389 cb
->cb_color_attrib
|= S_028C74_FMASK_TILE_MODE_INDEX(iview
->image
->fmask
.tile_mode_index
);
1390 cb
->cb_color_fmask
= va
>> 8;
1391 cb
->cb_color_fmask_slice
= S_028C88_TILE_MAX(iview
->image
->fmask
.slice_tile_max
);
1393 /* This must be set for fast clear to work without FMASK. */
1394 if (device
->instance
->physicalDevice
.rad_info
.chip_class
>= CIK
)
1395 cb
->cb_color_pitch
|= S_028C64_FMASK_TILE_MAX(pitch_tile_max
);
1396 cb
->cb_color_attrib
|= S_028C74_FMASK_TILE_MODE_INDEX(tile_mode_index
);
1397 cb
->cb_color_fmask
= cb
->cb_color_base
;
1398 cb
->cb_color_fmask_slice
= S_028C88_TILE_MAX(slice_tile_max
);
1401 ntype
= radv_translate_color_numformat(iview
->vk_format
,
1403 vk_format_get_first_non_void_channel(iview
->vk_format
));
1404 format
= radv_translate_colorformat(iview
->vk_format
);
1405 if (format
== V_028C70_COLOR_INVALID
|| ntype
== ~0u)
1406 radv_finishme("Illegal color\n");
1407 swap
= radv_translate_colorswap(iview
->vk_format
, FALSE
);
1408 endian
= radv_colorformat_endian_swap(format
);
1410 /* blend clamp should be set for all NORM/SRGB types */
1411 if (ntype
== V_028C70_NUMBER_UNORM
||
1412 ntype
== V_028C70_NUMBER_SNORM
||
1413 ntype
== V_028C70_NUMBER_SRGB
)
1416 /* set blend bypass according to docs if SINT/UINT or
1417 8/24 COLOR variants */
1418 if (ntype
== V_028C70_NUMBER_UINT
|| ntype
== V_028C70_NUMBER_SINT
||
1419 format
== V_028C70_COLOR_8_24
|| format
== V_028C70_COLOR_24_8
||
1420 format
== V_028C70_COLOR_X24_8_32_FLOAT
) {
1425 if ((ntype
== V_028C70_NUMBER_UINT
|| ntype
== V_028C70_NUMBER_SINT
) &&
1426 (format
== V_028C70_COLOR_8
||
1427 format
== V_028C70_COLOR_8_8
||
1428 format
== V_028C70_COLOR_8_8_8_8
))
1429 ->color_is_int8
= true;
1431 cb
->cb_color_info
= S_028C70_FORMAT(format
) |
1432 S_028C70_COMP_SWAP(swap
) |
1433 S_028C70_BLEND_CLAMP(blend_clamp
) |
1434 S_028C70_BLEND_BYPASS(blend_bypass
) |
1435 S_028C70_SIMPLE_FLOAT(1) |
1436 S_028C70_ROUND_MODE(ntype
!= V_028C70_NUMBER_UNORM
&&
1437 ntype
!= V_028C70_NUMBER_SNORM
&&
1438 ntype
!= V_028C70_NUMBER_SRGB
&&
1439 format
!= V_028C70_COLOR_8_24
&&
1440 format
!= V_028C70_COLOR_24_8
) |
1441 S_028C70_NUMBER_TYPE(ntype
) |
1442 S_028C70_ENDIAN(endian
);
1443 if (iview
->image
->samples
> 1)
1444 if (iview
->image
->fmask
.size
)
1445 cb
->cb_color_info
|= S_028C70_COMPRESSION(1);
1447 if (iview
->image
->cmask
.size
&& device
->allow_fast_clears
)
1448 cb
->cb_color_info
|= S_028C70_FAST_CLEAR(1);
1450 if (iview
->image
->surface
.dcc_size
&& level_info
->dcc_enabled
)
1451 cb
->cb_color_info
|= S_028C70_DCC_ENABLE(1);
1453 if (device
->instance
->physicalDevice
.rad_info
.chip_class
>= VI
) {
1454 unsigned max_uncompressed_block_size
= 2;
1455 if (iview
->image
->samples
> 1) {
1456 if (iview
->image
->surface
.bpe
== 1)
1457 max_uncompressed_block_size
= 0;
1458 else if (iview
->image
->surface
.bpe
== 2)
1459 max_uncompressed_block_size
= 1;
1462 cb
->cb_dcc_control
= S_028C78_MAX_UNCOMPRESSED_BLOCK_SIZE(max_uncompressed_block_size
) |
1463 S_028C78_INDEPENDENT_64B_BLOCKS(1);
1466 /* This must be set for fast clear to work without FMASK. */
1467 if (!iview
->image
->fmask
.size
&&
1468 device
->instance
->physicalDevice
.rad_info
.chip_class
== SI
) {
1469 unsigned bankh
= util_logbase2(iview
->image
->surface
.bankh
);
1470 cb
->cb_color_attrib
|= S_028C74_FMASK_BANK_HEIGHT(bankh
);
1475 radv_initialise_ds_surface(struct radv_device
*device
,
1476 struct radv_ds_buffer_info
*ds
,
1477 struct radv_image_view
*iview
)
1479 unsigned level
= iview
->base_mip
;
1481 uint64_t va
, s_offs
, z_offs
;
1482 const struct radeon_surf_level
*level_info
= &iview
->image
->surface
.level
[level
];
1483 memset(ds
, 0, sizeof(*ds
));
1484 switch (iview
->vk_format
) {
1485 case VK_FORMAT_D24_UNORM_S8_UINT
:
1486 case VK_FORMAT_X8_D24_UNORM_PACK32
:
1487 ds
->pa_su_poly_offset_db_fmt_cntl
= S_028B78_POLY_OFFSET_NEG_NUM_DB_BITS(-24);
1488 ds
->offset_scale
= 2.0f
;
1490 case VK_FORMAT_D16_UNORM
:
1491 case VK_FORMAT_D16_UNORM_S8_UINT
:
1492 ds
->pa_su_poly_offset_db_fmt_cntl
= S_028B78_POLY_OFFSET_NEG_NUM_DB_BITS(-16);
1493 ds
->offset_scale
= 4.0f
;
1495 case VK_FORMAT_D32_SFLOAT
:
1496 case VK_FORMAT_D32_SFLOAT_S8_UINT
:
1497 ds
->pa_su_poly_offset_db_fmt_cntl
= S_028B78_POLY_OFFSET_NEG_NUM_DB_BITS(-23) |
1498 S_028B78_POLY_OFFSET_DB_IS_FLOAT_FMT(1);
1499 ds
->offset_scale
= 1.0f
;
1505 format
= radv_translate_dbformat(iview
->vk_format
);
1506 if (format
== V_028040_Z_INVALID
) {
1507 fprintf(stderr
, "Invalid DB format: %d, disabling DB.\n", iview
->vk_format
);
1510 va
= device
->ws
->buffer_get_va(iview
->bo
) + iview
->image
->offset
;
1511 s_offs
= z_offs
= va
;
1512 z_offs
+= iview
->image
->surface
.level
[level
].offset
;
1513 s_offs
+= iview
->image
->surface
.stencil_level
[level
].offset
;
1515 ds
->db_depth_view
= S_028008_SLICE_START(iview
->base_layer
) |
1516 S_028008_SLICE_MAX(iview
->base_layer
+ iview
->extent
.depth
- 1);
1517 ds
->db_depth_info
= S_02803C_ADDR5_SWIZZLE_MASK(1);
1518 ds
->db_z_info
= S_028040_FORMAT(format
) | S_028040_ZRANGE_PRECISION(1);
1520 if (iview
->image
->samples
> 1)
1521 ds
->db_z_info
|= S_028040_NUM_SAMPLES(util_logbase2(iview
->image
->samples
));
1523 if (iview
->image
->surface
.flags
& RADEON_SURF_SBUFFER
)
1524 ds
->db_stencil_info
= S_028044_FORMAT(V_028044_STENCIL_8
);
1526 ds
->db_stencil_info
= S_028044_FORMAT(V_028044_STENCIL_INVALID
);
1528 if (device
->instance
->physicalDevice
.rad_info
.chip_class
>= CIK
) {
1529 struct radeon_info
*info
= &device
->instance
->physicalDevice
.rad_info
;
1530 unsigned tiling_index
= iview
->image
->surface
.tiling_index
[level
];
1531 unsigned stencil_index
= iview
->image
->surface
.stencil_tiling_index
[level
];
1532 unsigned macro_index
= iview
->image
->surface
.macro_tile_index
;
1533 unsigned tile_mode
= info
->si_tile_mode_array
[tiling_index
];
1534 unsigned stencil_tile_mode
= info
->si_tile_mode_array
[stencil_index
];
1535 unsigned macro_mode
= info
->cik_macrotile_mode_array
[macro_index
];
1537 ds
->db_depth_info
|=
1538 S_02803C_ARRAY_MODE(G_009910_ARRAY_MODE(tile_mode
)) |
1539 S_02803C_PIPE_CONFIG(G_009910_PIPE_CONFIG(tile_mode
)) |
1540 S_02803C_BANK_WIDTH(G_009990_BANK_WIDTH(macro_mode
)) |
1541 S_02803C_BANK_HEIGHT(G_009990_BANK_HEIGHT(macro_mode
)) |
1542 S_02803C_MACRO_TILE_ASPECT(G_009990_MACRO_TILE_ASPECT(macro_mode
)) |
1543 S_02803C_NUM_BANKS(G_009990_NUM_BANKS(macro_mode
));
1544 ds
->db_z_info
|= S_028040_TILE_SPLIT(G_009910_TILE_SPLIT(tile_mode
));
1545 ds
->db_stencil_info
|= S_028044_TILE_SPLIT(G_009910_TILE_SPLIT(stencil_tile_mode
));
1547 unsigned tile_mode_index
= si_tile_mode_index(iview
->image
, level
, false);
1548 ds
->db_z_info
|= S_028040_TILE_MODE_INDEX(tile_mode_index
);
1549 tile_mode_index
= si_tile_mode_index(iview
->image
, level
, true);
1550 ds
->db_stencil_info
|= S_028044_TILE_MODE_INDEX(tile_mode_index
);
1553 if (iview
->image
->htile
.size
&& !level
) {
1554 ds
->db_z_info
|= S_028040_TILE_SURFACE_ENABLE(1) |
1555 S_028040_ALLOW_EXPCLEAR(1);
1557 if (iview
->image
->surface
.flags
& RADEON_SURF_SBUFFER
) {
1558 /* Workaround: For a not yet understood reason, the
1559 * combination of MSAA, fast stencil clear and stencil
1560 * decompress messes with subsequent stencil buffer
1561 * uses. Problem was reproduced on Verde, Bonaire,
1562 * Tonga, and Carrizo.
1564 * Disabling EXPCLEAR works around the problem.
1566 * Check piglit's arb_texture_multisample-stencil-clear
1567 * test if you want to try changing this.
1569 if (iview
->image
->samples
<= 1)
1570 ds
->db_stencil_info
|= S_028044_ALLOW_EXPCLEAR(1);
1572 /* Use all of the htile_buffer for depth if there's no stencil. */
1573 ds
->db_stencil_info
|= S_028044_TILE_STENCIL_DISABLE(1);
1575 va
= device
->ws
->buffer_get_va(iview
->bo
) + iview
->image
->offset
+
1576 iview
->image
->htile
.offset
;
1577 ds
->db_htile_data_base
= va
>> 8;
1578 ds
->db_htile_surface
= S_028ABC_FULL_CACHE(1);
1580 ds
->db_htile_data_base
= 0;
1581 ds
->db_htile_surface
= 0;
1584 ds
->db_z_read_base
= ds
->db_z_write_base
= z_offs
>> 8;
1585 ds
->db_stencil_read_base
= ds
->db_stencil_write_base
= s_offs
>> 8;
1587 ds
->db_depth_size
= S_028058_PITCH_TILE_MAX((level_info
->nblk_x
/ 8) - 1) |
1588 S_028058_HEIGHT_TILE_MAX((level_info
->nblk_y
/ 8) - 1);
1589 ds
->db_depth_slice
= S_02805C_SLICE_TILE_MAX((level_info
->nblk_x
* level_info
->nblk_y
) / 64 - 1);
1592 VkResult
radv_CreateFramebuffer(
1594 const VkFramebufferCreateInfo
* pCreateInfo
,
1595 const VkAllocationCallbacks
* pAllocator
,
1596 VkFramebuffer
* pFramebuffer
)
1598 RADV_FROM_HANDLE(radv_device
, device
, _device
);
1599 struct radv_framebuffer
*framebuffer
;
1601 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO
);
1603 size_t size
= sizeof(*framebuffer
) +
1604 sizeof(struct radv_attachment_info
) * pCreateInfo
->attachmentCount
;
1605 framebuffer
= radv_alloc2(&device
->alloc
, pAllocator
, size
, 8,
1606 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
1607 if (framebuffer
== NULL
)
1608 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
1610 framebuffer
->attachment_count
= pCreateInfo
->attachmentCount
;
1611 for (uint32_t i
= 0; i
< pCreateInfo
->attachmentCount
; i
++) {
1612 VkImageView _iview
= pCreateInfo
->pAttachments
[i
];
1613 struct radv_image_view
*iview
= radv_image_view_from_handle(_iview
);
1614 framebuffer
->attachments
[i
].attachment
= iview
;
1615 if (iview
->aspect_mask
& VK_IMAGE_ASPECT_COLOR_BIT
) {
1616 radv_initialise_color_surface(device
, &framebuffer
->attachments
[i
].cb
, iview
);
1617 } else if (iview
->aspect_mask
& (VK_IMAGE_ASPECT_DEPTH_BIT
| VK_IMAGE_ASPECT_STENCIL_BIT
)) {
1618 radv_initialise_ds_surface(device
, &framebuffer
->attachments
[i
].ds
, iview
);
1622 framebuffer
->width
= pCreateInfo
->width
;
1623 framebuffer
->height
= pCreateInfo
->height
;
1624 framebuffer
->layers
= pCreateInfo
->layers
;
1626 *pFramebuffer
= radv_framebuffer_to_handle(framebuffer
);
1630 void radv_DestroyFramebuffer(
1633 const VkAllocationCallbacks
* pAllocator
)
1635 RADV_FROM_HANDLE(radv_device
, device
, _device
);
1636 RADV_FROM_HANDLE(radv_framebuffer
, fb
, _fb
);
1640 radv_free2(&device
->alloc
, pAllocator
, fb
);
1643 static unsigned radv_tex_wrap(VkSamplerAddressMode address_mode
)
1645 switch (address_mode
) {
1646 case VK_SAMPLER_ADDRESS_MODE_REPEAT
:
1647 return V_008F30_SQ_TEX_WRAP
;
1648 case VK_SAMPLER_ADDRESS_MODE_MIRRORED_REPEAT
:
1649 return V_008F30_SQ_TEX_MIRROR
;
1650 case VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE
:
1651 return V_008F30_SQ_TEX_CLAMP_LAST_TEXEL
;
1652 case VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_BORDER
:
1653 return V_008F30_SQ_TEX_CLAMP_BORDER
;
1654 case VK_SAMPLER_ADDRESS_MODE_MIRROR_CLAMP_TO_EDGE
:
1655 return V_008F30_SQ_TEX_MIRROR_ONCE_LAST_TEXEL
;
1657 unreachable("illegal tex wrap mode");
1663 radv_tex_compare(VkCompareOp op
)
1666 case VK_COMPARE_OP_NEVER
:
1667 return V_008F30_SQ_TEX_DEPTH_COMPARE_NEVER
;
1668 case VK_COMPARE_OP_LESS
:
1669 return V_008F30_SQ_TEX_DEPTH_COMPARE_LESS
;
1670 case VK_COMPARE_OP_EQUAL
:
1671 return V_008F30_SQ_TEX_DEPTH_COMPARE_EQUAL
;
1672 case VK_COMPARE_OP_LESS_OR_EQUAL
:
1673 return V_008F30_SQ_TEX_DEPTH_COMPARE_LESSEQUAL
;
1674 case VK_COMPARE_OP_GREATER
:
1675 return V_008F30_SQ_TEX_DEPTH_COMPARE_GREATER
;
1676 case VK_COMPARE_OP_NOT_EQUAL
:
1677 return V_008F30_SQ_TEX_DEPTH_COMPARE_NOTEQUAL
;
1678 case VK_COMPARE_OP_GREATER_OR_EQUAL
:
1679 return V_008F30_SQ_TEX_DEPTH_COMPARE_GREATEREQUAL
;
1680 case VK_COMPARE_OP_ALWAYS
:
1681 return V_008F30_SQ_TEX_DEPTH_COMPARE_ALWAYS
;
1683 unreachable("illegal compare mode");
1689 radv_tex_filter(VkFilter filter
, unsigned max_ansio
)
1692 case VK_FILTER_NEAREST
:
1693 return (max_ansio
> 1 ? V_008F38_SQ_TEX_XY_FILTER_ANISO_POINT
:
1694 V_008F38_SQ_TEX_XY_FILTER_POINT
);
1695 case VK_FILTER_LINEAR
:
1696 return (max_ansio
> 1 ? V_008F38_SQ_TEX_XY_FILTER_ANISO_BILINEAR
:
1697 V_008F38_SQ_TEX_XY_FILTER_BILINEAR
);
1698 case VK_FILTER_CUBIC_IMG
:
1700 fprintf(stderr
, "illegal texture filter");
1706 radv_tex_mipfilter(VkSamplerMipmapMode mode
)
1709 case VK_SAMPLER_MIPMAP_MODE_NEAREST
:
1710 return V_008F38_SQ_TEX_Z_FILTER_POINT
;
1711 case VK_SAMPLER_MIPMAP_MODE_LINEAR
:
1712 return V_008F38_SQ_TEX_Z_FILTER_LINEAR
;
1714 return V_008F38_SQ_TEX_Z_FILTER_NONE
;
1719 radv_tex_bordercolor(VkBorderColor bcolor
)
1722 case VK_BORDER_COLOR_FLOAT_TRANSPARENT_BLACK
:
1723 case VK_BORDER_COLOR_INT_TRANSPARENT_BLACK
:
1724 return V_008F3C_SQ_TEX_BORDER_COLOR_TRANS_BLACK
;
1725 case VK_BORDER_COLOR_FLOAT_OPAQUE_BLACK
:
1726 case VK_BORDER_COLOR_INT_OPAQUE_BLACK
:
1727 return V_008F3C_SQ_TEX_BORDER_COLOR_OPAQUE_BLACK
;
1728 case VK_BORDER_COLOR_FLOAT_OPAQUE_WHITE
:
1729 case VK_BORDER_COLOR_INT_OPAQUE_WHITE
:
1730 return V_008F3C_SQ_TEX_BORDER_COLOR_OPAQUE_WHITE
;
1738 radv_init_sampler(struct radv_device
*device
,
1739 struct radv_sampler
*sampler
,
1740 const VkSamplerCreateInfo
*pCreateInfo
)
1742 uint32_t max_aniso
= 0;
1743 uint32_t max_aniso_ratio
= 0;//TODO
1745 is_vi
= (device
->instance
->physicalDevice
.rad_info
.chip_class
>= VI
);
1747 sampler
->state
[0] = (S_008F30_CLAMP_X(radv_tex_wrap(pCreateInfo
->addressModeU
)) |
1748 S_008F30_CLAMP_Y(radv_tex_wrap(pCreateInfo
->addressModeV
)) |
1749 S_008F30_CLAMP_Z(radv_tex_wrap(pCreateInfo
->addressModeW
)) |
1750 S_008F30_MAX_ANISO_RATIO(max_aniso_ratio
) |
1751 S_008F30_DEPTH_COMPARE_FUNC(radv_tex_compare(pCreateInfo
->compareOp
)) |
1752 S_008F30_FORCE_UNNORMALIZED(pCreateInfo
->unnormalizedCoordinates
? 1 : 0) |
1753 S_008F30_DISABLE_CUBE_WRAP(0) |
1754 S_008F30_COMPAT_MODE(is_vi
));
1755 sampler
->state
[1] = (S_008F34_MIN_LOD(S_FIXED(CLAMP(pCreateInfo
->minLod
, 0, 15), 8)) |
1756 S_008F34_MAX_LOD(S_FIXED(CLAMP(pCreateInfo
->maxLod
, 0, 15), 8)));
1757 sampler
->state
[2] = (S_008F38_LOD_BIAS(S_FIXED(CLAMP(pCreateInfo
->mipLodBias
, -16, 16), 8)) |
1758 S_008F38_XY_MAG_FILTER(radv_tex_filter(pCreateInfo
->magFilter
, max_aniso
)) |
1759 S_008F38_XY_MIN_FILTER(radv_tex_filter(pCreateInfo
->minFilter
, max_aniso
)) |
1760 S_008F38_MIP_FILTER(radv_tex_mipfilter(pCreateInfo
->mipmapMode
)) |
1761 S_008F38_MIP_POINT_PRECLAMP(1) |
1762 S_008F38_DISABLE_LSB_CEIL(1) |
1763 S_008F38_FILTER_PREC_FIX(1) |
1764 S_008F38_ANISO_OVERRIDE(is_vi
));
1765 sampler
->state
[3] = (S_008F3C_BORDER_COLOR_PTR(0) |
1766 S_008F3C_BORDER_COLOR_TYPE(radv_tex_bordercolor(pCreateInfo
->borderColor
)));
1769 VkResult
radv_CreateSampler(
1771 const VkSamplerCreateInfo
* pCreateInfo
,
1772 const VkAllocationCallbacks
* pAllocator
,
1773 VkSampler
* pSampler
)
1775 RADV_FROM_HANDLE(radv_device
, device
, _device
);
1776 struct radv_sampler
*sampler
;
1778 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO
);
1780 sampler
= radv_alloc2(&device
->alloc
, pAllocator
, sizeof(*sampler
), 8,
1781 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
1783 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
1785 radv_init_sampler(device
, sampler
, pCreateInfo
);
1786 *pSampler
= radv_sampler_to_handle(sampler
);
1791 void radv_DestroySampler(
1794 const VkAllocationCallbacks
* pAllocator
)
1796 RADV_FROM_HANDLE(radv_device
, device
, _device
);
1797 RADV_FROM_HANDLE(radv_sampler
, sampler
, _sampler
);
1801 radv_free2(&device
->alloc
, pAllocator
, sampler
);