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 radv_physical_device_init(struct radv_physical_device
*device
,
49 struct radv_instance
*instance
,
53 drmVersionPtr version
;
56 fd
= open(path
, O_RDWR
| O_CLOEXEC
);
58 return vk_errorf(VK_ERROR_INCOMPATIBLE_DRIVER
,
59 "failed to open %s: %m", path
);
61 version
= drmGetVersion(fd
);
64 return vk_errorf(VK_ERROR_INCOMPATIBLE_DRIVER
,
65 "failed to get version %s: %m", path
);
68 if (strcmp(version
->name
, "amdgpu")) {
69 drmFreeVersion(version
);
71 return VK_ERROR_INCOMPATIBLE_DRIVER
;
73 drmFreeVersion(version
);
75 device
->_loader_data
.loaderMagic
= ICD_LOADER_MAGIC
;
76 device
->instance
= instance
;
77 assert(strlen(path
) < ARRAY_SIZE(device
->path
));
78 strncpy(device
->path
, path
, ARRAY_SIZE(device
->path
));
80 device
->ws
= radv_amdgpu_winsys_create(fd
);
82 result
= VK_ERROR_INCOMPATIBLE_DRIVER
;
85 device
->ws
->query_info(device
->ws
, &device
->rad_info
);
86 result
= radv_init_wsi(device
);
87 if (result
!= VK_SUCCESS
) {
88 device
->ws
->destroy(device
->ws
);
92 fprintf(stderr
, "WARNING: radv is not a conformant vulkan implementation, testing use only.\n");
93 device
->name
= device
->rad_info
.name
;
102 radv_physical_device_finish(struct radv_physical_device
*device
)
104 radv_finish_wsi(device
);
105 device
->ws
->destroy(device
->ws
);
108 static const VkExtensionProperties global_extensions
[] = {
110 .extensionName
= VK_KHR_SURFACE_EXTENSION_NAME
,
113 #ifdef VK_USE_PLATFORM_XCB_KHR
115 .extensionName
= VK_KHR_XCB_SURFACE_EXTENSION_NAME
,
119 #ifdef VK_USE_PLATFORM_WAYLAND_KHR
121 .extensionName
= VK_KHR_WAYLAND_SURFACE_EXTENSION_NAME
,
127 static const VkExtensionProperties device_extensions
[] = {
129 .extensionName
= VK_KHR_SWAPCHAIN_EXTENSION_NAME
,
135 default_alloc_func(void *pUserData
, size_t size
, size_t align
,
136 VkSystemAllocationScope allocationScope
)
142 default_realloc_func(void *pUserData
, void *pOriginal
, size_t size
,
143 size_t align
, VkSystemAllocationScope allocationScope
)
145 return realloc(pOriginal
, size
);
149 default_free_func(void *pUserData
, void *pMemory
)
154 static const VkAllocationCallbacks default_alloc
= {
156 .pfnAllocation
= default_alloc_func
,
157 .pfnReallocation
= default_realloc_func
,
158 .pfnFree
= default_free_func
,
161 VkResult
radv_CreateInstance(
162 const VkInstanceCreateInfo
* pCreateInfo
,
163 const VkAllocationCallbacks
* pAllocator
,
164 VkInstance
* pInstance
)
166 struct radv_instance
*instance
;
168 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO
);
170 uint32_t client_version
;
171 if (pCreateInfo
->pApplicationInfo
&&
172 pCreateInfo
->pApplicationInfo
->apiVersion
!= 0) {
173 client_version
= pCreateInfo
->pApplicationInfo
->apiVersion
;
175 client_version
= VK_MAKE_VERSION(1, 0, 0);
178 if (VK_MAKE_VERSION(1, 0, 0) > client_version
||
179 client_version
> VK_MAKE_VERSION(1, 0, 0xfff)) {
180 return vk_errorf(VK_ERROR_INCOMPATIBLE_DRIVER
,
181 "Client requested version %d.%d.%d",
182 VK_VERSION_MAJOR(client_version
),
183 VK_VERSION_MINOR(client_version
),
184 VK_VERSION_PATCH(client_version
));
187 for (uint32_t i
= 0; i
< pCreateInfo
->enabledExtensionCount
; i
++) {
189 for (uint32_t j
= 0; j
< ARRAY_SIZE(global_extensions
); j
++) {
190 if (strcmp(pCreateInfo
->ppEnabledExtensionNames
[i
],
191 global_extensions
[j
].extensionName
) == 0) {
197 return vk_error(VK_ERROR_EXTENSION_NOT_PRESENT
);
200 instance
= vk_alloc2(&default_alloc
, pAllocator
, sizeof(*instance
), 8,
201 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE
);
203 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
205 instance
->_loader_data
.loaderMagic
= ICD_LOADER_MAGIC
;
208 instance
->alloc
= *pAllocator
;
210 instance
->alloc
= default_alloc
;
212 instance
->apiVersion
= client_version
;
213 instance
->physicalDeviceCount
= -1;
217 VG(VALGRIND_CREATE_MEMPOOL(instance
, 0, false));
219 *pInstance
= radv_instance_to_handle(instance
);
224 void radv_DestroyInstance(
225 VkInstance _instance
,
226 const VkAllocationCallbacks
* pAllocator
)
228 RADV_FROM_HANDLE(radv_instance
, instance
, _instance
);
230 if (instance
->physicalDeviceCount
> 0) {
231 /* We support at most one physical device. */
232 assert(instance
->physicalDeviceCount
== 1);
233 radv_physical_device_finish(&instance
->physicalDevice
);
236 VG(VALGRIND_DESTROY_MEMPOOL(instance
));
240 vk_free(&instance
->alloc
, instance
);
243 VkResult
radv_EnumeratePhysicalDevices(
244 VkInstance _instance
,
245 uint32_t* pPhysicalDeviceCount
,
246 VkPhysicalDevice
* pPhysicalDevices
)
248 RADV_FROM_HANDLE(radv_instance
, instance
, _instance
);
251 if (instance
->physicalDeviceCount
< 0) {
253 for (unsigned i
= 0; i
< 8; i
++) {
254 snprintf(path
, sizeof(path
), "/dev/dri/renderD%d", 128 + i
);
255 result
= radv_physical_device_init(&instance
->physicalDevice
,
257 if (result
!= VK_ERROR_INCOMPATIBLE_DRIVER
)
261 if (result
== VK_ERROR_INCOMPATIBLE_DRIVER
) {
262 instance
->physicalDeviceCount
= 0;
263 } else if (result
== VK_SUCCESS
) {
264 instance
->physicalDeviceCount
= 1;
270 /* pPhysicalDeviceCount is an out parameter if pPhysicalDevices is NULL;
271 * otherwise it's an inout parameter.
273 * The Vulkan spec (git aaed022) says:
275 * pPhysicalDeviceCount is a pointer to an unsigned integer variable
276 * that is initialized with the number of devices the application is
277 * prepared to receive handles to. pname:pPhysicalDevices is pointer to
278 * an array of at least this many VkPhysicalDevice handles [...].
280 * Upon success, if pPhysicalDevices is NULL, vkEnumeratePhysicalDevices
281 * overwrites the contents of the variable pointed to by
282 * pPhysicalDeviceCount with the number of physical devices in in the
283 * instance; otherwise, vkEnumeratePhysicalDevices overwrites
284 * pPhysicalDeviceCount with the number of physical handles written to
287 if (!pPhysicalDevices
) {
288 *pPhysicalDeviceCount
= instance
->physicalDeviceCount
;
289 } else if (*pPhysicalDeviceCount
>= 1) {
290 pPhysicalDevices
[0] = radv_physical_device_to_handle(&instance
->physicalDevice
);
291 *pPhysicalDeviceCount
= 1;
292 } else if (*pPhysicalDeviceCount
< instance
->physicalDeviceCount
) {
293 return VK_INCOMPLETE
;
295 *pPhysicalDeviceCount
= 0;
301 void radv_GetPhysicalDeviceFeatures(
302 VkPhysicalDevice physicalDevice
,
303 VkPhysicalDeviceFeatures
* pFeatures
)
305 // RADV_FROM_HANDLE(radv_physical_device, pdevice, physicalDevice);
307 memset(pFeatures
, 0, sizeof(*pFeatures
));
309 *pFeatures
= (VkPhysicalDeviceFeatures
) {
310 .robustBufferAccess
= true,
311 .fullDrawIndexUint32
= true,
312 .imageCubeArray
= true,
313 .independentBlend
= true,
314 .geometryShader
= false,
315 .tessellationShader
= false,
316 .sampleRateShading
= false,
317 .dualSrcBlend
= true,
319 .multiDrawIndirect
= true,
320 .drawIndirectFirstInstance
= true,
322 .depthBiasClamp
= true,
323 .fillModeNonSolid
= true,
328 .multiViewport
= false,
329 .samplerAnisotropy
= false, /* FINISHME */
330 .textureCompressionETC2
= false,
331 .textureCompressionASTC_LDR
= false,
332 .textureCompressionBC
= true,
333 .occlusionQueryPrecise
= true,
334 .pipelineStatisticsQuery
= false,
335 .vertexPipelineStoresAndAtomics
= true,
336 .fragmentStoresAndAtomics
= true,
337 .shaderTessellationAndGeometryPointSize
= true,
338 .shaderImageGatherExtended
= false,
339 .shaderStorageImageExtendedFormats
= false,
340 .shaderStorageImageMultisample
= false,
341 .shaderUniformBufferArrayDynamicIndexing
= true,
342 .shaderSampledImageArrayDynamicIndexing
= true,
343 .shaderStorageBufferArrayDynamicIndexing
= true,
344 .shaderStorageImageArrayDynamicIndexing
= true,
345 .shaderStorageImageReadWithoutFormat
= false,
346 .shaderStorageImageWriteWithoutFormat
= true,
347 .shaderClipDistance
= true,
348 .shaderCullDistance
= true,
349 .shaderFloat64
= false,
350 .shaderInt64
= false,
351 .shaderInt16
= false,
353 .variableMultisampleRate
= false,
354 .inheritedQueries
= false,
359 radv_device_get_cache_uuid(void *uuid
)
361 memset(uuid
, 0, VK_UUID_SIZE
);
362 snprintf(uuid
, VK_UUID_SIZE
, "radv-%s", RADV_TIMESTAMP
);
365 void radv_GetPhysicalDeviceProperties(
366 VkPhysicalDevice physicalDevice
,
367 VkPhysicalDeviceProperties
* pProperties
)
369 RADV_FROM_HANDLE(radv_physical_device
, pdevice
, physicalDevice
);
370 VkSampleCountFlags sample_counts
= 0xf;
371 VkPhysicalDeviceLimits limits
= {
372 .maxImageDimension1D
= (1 << 14),
373 .maxImageDimension2D
= (1 << 14),
374 .maxImageDimension3D
= (1 << 11),
375 .maxImageDimensionCube
= (1 << 14),
376 .maxImageArrayLayers
= (1 << 11),
377 .maxTexelBufferElements
= 128 * 1024 * 1024,
378 .maxUniformBufferRange
= UINT32_MAX
,
379 .maxStorageBufferRange
= UINT32_MAX
,
380 .maxPushConstantsSize
= MAX_PUSH_CONSTANTS_SIZE
,
381 .maxMemoryAllocationCount
= UINT32_MAX
,
382 .maxSamplerAllocationCount
= 64 * 1024,
383 .bufferImageGranularity
= 64, /* A cache line */
384 .sparseAddressSpaceSize
= 0,
385 .maxBoundDescriptorSets
= MAX_SETS
,
386 .maxPerStageDescriptorSamplers
= 64,
387 .maxPerStageDescriptorUniformBuffers
= 64,
388 .maxPerStageDescriptorStorageBuffers
= 64,
389 .maxPerStageDescriptorSampledImages
= 64,
390 .maxPerStageDescriptorStorageImages
= 64,
391 .maxPerStageDescriptorInputAttachments
= 64,
392 .maxPerStageResources
= 128,
393 .maxDescriptorSetSamplers
= 256,
394 .maxDescriptorSetUniformBuffers
= 256,
395 .maxDescriptorSetUniformBuffersDynamic
= 256,
396 .maxDescriptorSetStorageBuffers
= 256,
397 .maxDescriptorSetStorageBuffersDynamic
= 256,
398 .maxDescriptorSetSampledImages
= 256,
399 .maxDescriptorSetStorageImages
= 256,
400 .maxDescriptorSetInputAttachments
= 256,
401 .maxVertexInputAttributes
= 32,
402 .maxVertexInputBindings
= 32,
403 .maxVertexInputAttributeOffset
= 2047,
404 .maxVertexInputBindingStride
= 2048,
405 .maxVertexOutputComponents
= 128,
406 .maxTessellationGenerationLevel
= 0,
407 .maxTessellationPatchSize
= 0,
408 .maxTessellationControlPerVertexInputComponents
= 0,
409 .maxTessellationControlPerVertexOutputComponents
= 0,
410 .maxTessellationControlPerPatchOutputComponents
= 0,
411 .maxTessellationControlTotalOutputComponents
= 0,
412 .maxTessellationEvaluationInputComponents
= 0,
413 .maxTessellationEvaluationOutputComponents
= 0,
414 .maxGeometryShaderInvocations
= 32,
415 .maxGeometryInputComponents
= 64,
416 .maxGeometryOutputComponents
= 128,
417 .maxGeometryOutputVertices
= 256,
418 .maxGeometryTotalOutputComponents
= 1024,
419 .maxFragmentInputComponents
= 128,
420 .maxFragmentOutputAttachments
= 8,
421 .maxFragmentDualSrcAttachments
= 2,
422 .maxFragmentCombinedOutputResources
= 8,
423 .maxComputeSharedMemorySize
= 32768,
424 .maxComputeWorkGroupCount
= { 65535, 65535, 65535 },
425 .maxComputeWorkGroupInvocations
= 16 * 1024,
426 .maxComputeWorkGroupSize
= {
427 16 * 1024/*devinfo->max_cs_threads*/,
431 .subPixelPrecisionBits
= 4 /* FIXME */,
432 .subTexelPrecisionBits
= 4 /* FIXME */,
433 .mipmapPrecisionBits
= 4 /* FIXME */,
434 .maxDrawIndexedIndexValue
= UINT32_MAX
,
435 .maxDrawIndirectCount
= UINT32_MAX
,
436 .maxSamplerLodBias
= 16,
437 .maxSamplerAnisotropy
= 16,
438 .maxViewports
= MAX_VIEWPORTS
,
439 .maxViewportDimensions
= { (1 << 14), (1 << 14) },
440 .viewportBoundsRange
= { INT16_MIN
, INT16_MAX
},
441 .viewportSubPixelBits
= 13, /* We take a float? */
442 .minMemoryMapAlignment
= 4096, /* A page */
443 .minTexelBufferOffsetAlignment
= 1,
444 .minUniformBufferOffsetAlignment
= 4,
445 .minStorageBufferOffsetAlignment
= 4,
446 .minTexelOffset
= -8,
448 .minTexelGatherOffset
= -8,
449 .maxTexelGatherOffset
= 7,
450 .minInterpolationOffset
= 0, /* FIXME */
451 .maxInterpolationOffset
= 0, /* FIXME */
452 .subPixelInterpolationOffsetBits
= 0, /* FIXME */
453 .maxFramebufferWidth
= (1 << 14),
454 .maxFramebufferHeight
= (1 << 14),
455 .maxFramebufferLayers
= (1 << 10),
456 .framebufferColorSampleCounts
= sample_counts
,
457 .framebufferDepthSampleCounts
= sample_counts
,
458 .framebufferStencilSampleCounts
= sample_counts
,
459 .framebufferNoAttachmentsSampleCounts
= sample_counts
,
460 .maxColorAttachments
= MAX_RTS
,
461 .sampledImageColorSampleCounts
= sample_counts
,
462 .sampledImageIntegerSampleCounts
= VK_SAMPLE_COUNT_1_BIT
,
463 .sampledImageDepthSampleCounts
= sample_counts
,
464 .sampledImageStencilSampleCounts
= sample_counts
,
465 .storageImageSampleCounts
= VK_SAMPLE_COUNT_1_BIT
,
466 .maxSampleMaskWords
= 1,
467 .timestampComputeAndGraphics
= false,
468 .timestampPeriod
= 100000.0 / pdevice
->rad_info
.clock_crystal_freq
,
469 .maxClipDistances
= 8,
470 .maxCullDistances
= 8,
471 .maxCombinedClipAndCullDistances
= 8,
472 .discreteQueuePriorities
= 1,
473 .pointSizeRange
= { 0.125, 255.875 },
474 .lineWidthRange
= { 0.0, 7.9921875 },
475 .pointSizeGranularity
= (1.0 / 8.0),
476 .lineWidthGranularity
= (1.0 / 128.0),
477 .strictLines
= false, /* FINISHME */
478 .standardSampleLocations
= true,
479 .optimalBufferCopyOffsetAlignment
= 128,
480 .optimalBufferCopyRowPitchAlignment
= 128,
481 .nonCoherentAtomSize
= 64,
484 *pProperties
= (VkPhysicalDeviceProperties
) {
485 .apiVersion
= VK_MAKE_VERSION(1, 0, 5),
488 .deviceID
= pdevice
->rad_info
.pci_id
,
489 .deviceType
= VK_PHYSICAL_DEVICE_TYPE_DISCRETE_GPU
,
491 .sparseProperties
= {0}, /* Broadwell doesn't do sparse. */
494 strcpy(pProperties
->deviceName
, pdevice
->name
);
495 radv_device_get_cache_uuid(pProperties
->pipelineCacheUUID
);
498 void radv_GetPhysicalDeviceQueueFamilyProperties(
499 VkPhysicalDevice physicalDevice
,
501 VkQueueFamilyProperties
* pQueueFamilyProperties
)
503 if (pQueueFamilyProperties
== NULL
) {
507 assert(*pCount
>= 1);
509 *pQueueFamilyProperties
= (VkQueueFamilyProperties
) {
510 .queueFlags
= VK_QUEUE_GRAPHICS_BIT
|
511 VK_QUEUE_COMPUTE_BIT
|
512 VK_QUEUE_TRANSFER_BIT
,
514 .timestampValidBits
= 64,
515 .minImageTransferGranularity
= (VkExtent3D
) { 1, 1, 1 },
519 void radv_GetPhysicalDeviceMemoryProperties(
520 VkPhysicalDevice physicalDevice
,
521 VkPhysicalDeviceMemoryProperties
* pMemoryProperties
)
523 RADV_FROM_HANDLE(radv_physical_device
, physical_device
, physicalDevice
);
525 pMemoryProperties
->memoryTypeCount
= 4;
526 pMemoryProperties
->memoryTypes
[0] = (VkMemoryType
) {
527 .propertyFlags
= VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT
,
530 pMemoryProperties
->memoryTypes
[1] = (VkMemoryType
) {
531 .propertyFlags
= VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT
|
532 VK_MEMORY_PROPERTY_HOST_COHERENT_BIT
,
535 pMemoryProperties
->memoryTypes
[2] = (VkMemoryType
) {
536 .propertyFlags
= VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT
|
537 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT
|
538 VK_MEMORY_PROPERTY_HOST_COHERENT_BIT
,
541 pMemoryProperties
->memoryTypes
[3] = (VkMemoryType
) {
542 .propertyFlags
= VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT
|
543 VK_MEMORY_PROPERTY_HOST_COHERENT_BIT
|
544 VK_MEMORY_PROPERTY_HOST_CACHED_BIT
,
548 pMemoryProperties
->memoryHeapCount
= 2;
549 pMemoryProperties
->memoryHeaps
[0] = (VkMemoryHeap
) {
550 .size
= physical_device
->rad_info
.vram_size
,
551 .flags
= VK_MEMORY_HEAP_DEVICE_LOCAL_BIT
,
553 pMemoryProperties
->memoryHeaps
[1] = (VkMemoryHeap
) {
554 .size
= physical_device
->rad_info
.gart_size
,
560 radv_queue_init(struct radv_device
*device
, struct radv_queue
*queue
)
562 queue
->_loader_data
.loaderMagic
= ICD_LOADER_MAGIC
;
563 queue
->device
= device
;
569 radv_queue_finish(struct radv_queue
*queue
)
573 VkResult
radv_CreateDevice(
574 VkPhysicalDevice physicalDevice
,
575 const VkDeviceCreateInfo
* pCreateInfo
,
576 const VkAllocationCallbacks
* pAllocator
,
579 RADV_FROM_HANDLE(radv_physical_device
, physical_device
, physicalDevice
);
581 struct radv_device
*device
;
583 for (uint32_t i
= 0; i
< pCreateInfo
->enabledExtensionCount
; i
++) {
585 for (uint32_t j
= 0; j
< ARRAY_SIZE(device_extensions
); j
++) {
586 if (strcmp(pCreateInfo
->ppEnabledExtensionNames
[i
],
587 device_extensions
[j
].extensionName
) == 0) {
593 return vk_error(VK_ERROR_EXTENSION_NOT_PRESENT
);
596 device
= vk_alloc2(&physical_device
->instance
->alloc
, pAllocator
,
598 VK_SYSTEM_ALLOCATION_SCOPE_DEVICE
);
600 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
602 device
->_loader_data
.loaderMagic
= ICD_LOADER_MAGIC
;
603 device
->instance
= physical_device
->instance
;
605 device
->ws
= physical_device
->ws
;
607 device
->alloc
= *pAllocator
;
609 device
->alloc
= physical_device
->instance
->alloc
;
611 device
->hw_ctx
= device
->ws
->ctx_create(device
->ws
);
612 if (!device
->hw_ctx
) {
613 result
= VK_ERROR_OUT_OF_HOST_MEMORY
;
617 radv_queue_init(device
, &device
->queue
);
619 result
= radv_device_init_meta(device
);
620 if (result
!= VK_SUCCESS
) {
621 device
->ws
->ctx_destroy(device
->hw_ctx
);
624 device
->allow_fast_clears
= env_var_as_boolean("RADV_FAST_CLEARS", false);
625 device
->allow_dcc
= !env_var_as_boolean("RADV_DCC_DISABLE", false);
627 if (device
->allow_fast_clears
&& device
->allow_dcc
)
628 radv_finishme("DCC fast clears have not been tested\n");
630 radv_device_init_msaa(device
);
631 device
->empty_cs
= device
->ws
->cs_create(device
->ws
, RING_GFX
);
632 radeon_emit(device
->empty_cs
, PKT3(PKT3_CONTEXT_CONTROL
, 1, 0));
633 radeon_emit(device
->empty_cs
, CONTEXT_CONTROL_LOAD_ENABLE(1));
634 radeon_emit(device
->empty_cs
, CONTEXT_CONTROL_SHADOW_ENABLE(1));
635 device
->ws
->cs_finalize(device
->empty_cs
);
636 *pDevice
= radv_device_to_handle(device
);
639 vk_free(&device
->alloc
, device
);
643 void radv_DestroyDevice(
645 const VkAllocationCallbacks
* pAllocator
)
647 RADV_FROM_HANDLE(radv_device
, device
, _device
);
649 device
->ws
->ctx_destroy(device
->hw_ctx
);
650 radv_queue_finish(&device
->queue
);
651 radv_device_finish_meta(device
);
653 vk_free(&device
->alloc
, device
);
656 VkResult
radv_EnumerateInstanceExtensionProperties(
657 const char* pLayerName
,
658 uint32_t* pPropertyCount
,
659 VkExtensionProperties
* pProperties
)
662 if (pProperties
== NULL
) {
663 *pPropertyCount
= ARRAY_SIZE(global_extensions
);
667 for (i
= 0; i
< *pPropertyCount
; i
++)
668 memcpy(&pProperties
[i
], &global_extensions
[i
], sizeof(VkExtensionProperties
));
671 if (i
< ARRAY_SIZE(global_extensions
))
672 return VK_INCOMPLETE
;
677 VkResult
radv_EnumerateDeviceExtensionProperties(
678 VkPhysicalDevice physicalDevice
,
679 const char* pLayerName
,
680 uint32_t* pPropertyCount
,
681 VkExtensionProperties
* pProperties
)
685 if (pProperties
== NULL
) {
686 *pPropertyCount
= ARRAY_SIZE(device_extensions
);
690 for (i
= 0; i
< *pPropertyCount
; i
++)
691 memcpy(&pProperties
[i
], &device_extensions
[i
], sizeof(VkExtensionProperties
));
694 if (i
< ARRAY_SIZE(device_extensions
))
695 return VK_INCOMPLETE
;
699 VkResult
radv_EnumerateInstanceLayerProperties(
700 uint32_t* pPropertyCount
,
701 VkLayerProperties
* pProperties
)
703 if (pProperties
== NULL
) {
708 /* None supported at this time */
709 return vk_error(VK_ERROR_LAYER_NOT_PRESENT
);
712 VkResult
radv_EnumerateDeviceLayerProperties(
713 VkPhysicalDevice physicalDevice
,
714 uint32_t* pPropertyCount
,
715 VkLayerProperties
* pProperties
)
717 if (pProperties
== NULL
) {
722 /* None supported at this time */
723 return vk_error(VK_ERROR_LAYER_NOT_PRESENT
);
726 void radv_GetDeviceQueue(
728 uint32_t queueNodeIndex
,
732 RADV_FROM_HANDLE(radv_device
, device
, _device
);
734 assert(queueIndex
== 0);
736 *pQueue
= radv_queue_to_handle(&device
->queue
);
739 VkResult
radv_QueueSubmit(
741 uint32_t submitCount
,
742 const VkSubmitInfo
* pSubmits
,
745 RADV_FROM_HANDLE(radv_queue
, queue
, _queue
);
746 RADV_FROM_HANDLE(radv_fence
, fence
, _fence
);
747 struct radeon_winsys_fence
*base_fence
= fence
? fence
->fence
: NULL
;
748 struct radeon_winsys_ctx
*ctx
= queue
->device
->hw_ctx
;
751 for (uint32_t i
= 0; i
< submitCount
; i
++) {
752 struct radeon_winsys_cs
**cs_array
;
753 bool can_patch
= true;
755 if (!pSubmits
[i
].commandBufferCount
)
758 cs_array
= malloc(sizeof(struct radeon_winsys_cs
*) *
759 pSubmits
[i
].commandBufferCount
);
761 for (uint32_t j
= 0; j
< pSubmits
[i
].commandBufferCount
; j
++) {
762 RADV_FROM_HANDLE(radv_cmd_buffer
, cmd_buffer
,
763 pSubmits
[i
].pCommandBuffers
[j
]);
764 assert(cmd_buffer
->level
== VK_COMMAND_BUFFER_LEVEL_PRIMARY
);
766 cs_array
[j
] = cmd_buffer
->cs
;
767 if ((cmd_buffer
->usage_flags
& VK_COMMAND_BUFFER_USAGE_SIMULTANEOUS_USE_BIT
))
770 ret
= queue
->device
->ws
->cs_submit(ctx
, cs_array
,
771 pSubmits
[i
].commandBufferCount
,
772 can_patch
, base_fence
);
774 radv_loge("failed to submit CS %d\n", i
);
780 ret
= queue
->device
->ws
->cs_submit(ctx
, &queue
->device
->empty_cs
,
781 1, false, base_fence
);
783 fence
->submitted
= true;
789 VkResult
radv_QueueWaitIdle(
792 RADV_FROM_HANDLE(radv_queue
, queue
, _queue
);
794 queue
->device
->ws
->ctx_wait_idle(queue
->device
->hw_ctx
);
798 VkResult
radv_DeviceWaitIdle(
801 RADV_FROM_HANDLE(radv_device
, device
, _device
);
803 device
->ws
->ctx_wait_idle(device
->hw_ctx
);
807 PFN_vkVoidFunction
radv_GetInstanceProcAddr(
811 return radv_lookup_entrypoint(pName
);
814 /* The loader wants us to expose a second GetInstanceProcAddr function
815 * to work around certain LD_PRELOAD issues seen in apps.
818 VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL
vk_icdGetInstanceProcAddr(
823 VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL
vk_icdGetInstanceProcAddr(
827 return radv_GetInstanceProcAddr(instance
, pName
);
830 PFN_vkVoidFunction
radv_GetDeviceProcAddr(
834 return radv_lookup_entrypoint(pName
);
837 VkResult
radv_AllocateMemory(
839 const VkMemoryAllocateInfo
* pAllocateInfo
,
840 const VkAllocationCallbacks
* pAllocator
,
841 VkDeviceMemory
* pMem
)
843 RADV_FROM_HANDLE(radv_device
, device
, _device
);
844 struct radv_device_memory
*mem
;
846 enum radeon_bo_domain domain
;
848 assert(pAllocateInfo
->sType
== VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO
);
850 if (pAllocateInfo
->allocationSize
== 0) {
851 /* Apparently, this is allowed */
852 *pMem
= VK_NULL_HANDLE
;
856 mem
= vk_alloc2(&device
->alloc
, pAllocator
, sizeof(*mem
), 8,
857 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
859 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
861 uint64_t alloc_size
= align_u64(pAllocateInfo
->allocationSize
, 4096);
862 if (pAllocateInfo
->memoryTypeIndex
== 1 || pAllocateInfo
->memoryTypeIndex
== 3)
863 domain
= RADEON_DOMAIN_GTT
;
865 domain
= RADEON_DOMAIN_VRAM
;
867 if (pAllocateInfo
->memoryTypeIndex
== 0)
868 flags
|= RADEON_FLAG_NO_CPU_ACCESS
;
870 flags
|= RADEON_FLAG_CPU_ACCESS
;
872 if (pAllocateInfo
->memoryTypeIndex
== 1)
873 flags
|= RADEON_FLAG_GTT_WC
;
875 mem
->bo
= device
->ws
->buffer_create(device
->ws
, alloc_size
, 32768,
879 result
= VK_ERROR_OUT_OF_DEVICE_MEMORY
;
882 mem
->type_index
= pAllocateInfo
->memoryTypeIndex
;
884 *pMem
= radv_device_memory_to_handle(mem
);
889 vk_free2(&device
->alloc
, pAllocator
, mem
);
894 void radv_FreeMemory(
897 const VkAllocationCallbacks
* pAllocator
)
899 RADV_FROM_HANDLE(radv_device
, device
, _device
);
900 RADV_FROM_HANDLE(radv_device_memory
, mem
, _mem
);
905 device
->ws
->buffer_destroy(mem
->bo
);
908 vk_free2(&device
->alloc
, pAllocator
, mem
);
911 VkResult
radv_MapMemory(
913 VkDeviceMemory _memory
,
916 VkMemoryMapFlags flags
,
919 RADV_FROM_HANDLE(radv_device
, device
, _device
);
920 RADV_FROM_HANDLE(radv_device_memory
, mem
, _memory
);
927 *ppData
= device
->ws
->buffer_map(mem
->bo
);
933 return VK_ERROR_MEMORY_MAP_FAILED
;
936 void radv_UnmapMemory(
938 VkDeviceMemory _memory
)
940 RADV_FROM_HANDLE(radv_device
, device
, _device
);
941 RADV_FROM_HANDLE(radv_device_memory
, mem
, _memory
);
946 device
->ws
->buffer_unmap(mem
->bo
);
949 VkResult
radv_FlushMappedMemoryRanges(
951 uint32_t memoryRangeCount
,
952 const VkMappedMemoryRange
* pMemoryRanges
)
957 VkResult
radv_InvalidateMappedMemoryRanges(
959 uint32_t memoryRangeCount
,
960 const VkMappedMemoryRange
* pMemoryRanges
)
965 void radv_GetBufferMemoryRequirements(
968 VkMemoryRequirements
* pMemoryRequirements
)
970 RADV_FROM_HANDLE(radv_buffer
, buffer
, _buffer
);
972 /* The Vulkan spec (git aaed022) says:
974 * memoryTypeBits is a bitfield and contains one bit set for every
975 * supported memory type for the resource. The bit `1<<i` is set if and
976 * only if the memory type `i` in the VkPhysicalDeviceMemoryProperties
977 * structure for the physical device is supported.
979 * We support exactly one memory type.
981 pMemoryRequirements
->memoryTypeBits
= 0x7;
983 pMemoryRequirements
->size
= buffer
->size
;
984 pMemoryRequirements
->alignment
= 16;
987 void radv_GetImageMemoryRequirements(
990 VkMemoryRequirements
* pMemoryRequirements
)
992 RADV_FROM_HANDLE(radv_image
, image
, _image
);
994 /* The Vulkan spec (git aaed022) says:
996 * memoryTypeBits is a bitfield and contains one bit set for every
997 * supported memory type for the resource. The bit `1<<i` is set if and
998 * only if the memory type `i` in the VkPhysicalDeviceMemoryProperties
999 * structure for the physical device is supported.
1001 * We support exactly one memory type.
1003 pMemoryRequirements
->memoryTypeBits
= 0x7;
1005 pMemoryRequirements
->size
= image
->size
;
1006 pMemoryRequirements
->alignment
= image
->alignment
;
1009 void radv_GetImageSparseMemoryRequirements(
1012 uint32_t* pSparseMemoryRequirementCount
,
1013 VkSparseImageMemoryRequirements
* pSparseMemoryRequirements
)
1018 void radv_GetDeviceMemoryCommitment(
1020 VkDeviceMemory memory
,
1021 VkDeviceSize
* pCommittedMemoryInBytes
)
1023 *pCommittedMemoryInBytes
= 0;
1026 VkResult
radv_BindBufferMemory(
1029 VkDeviceMemory _memory
,
1030 VkDeviceSize memoryOffset
)
1032 RADV_FROM_HANDLE(radv_device_memory
, mem
, _memory
);
1033 RADV_FROM_HANDLE(radv_buffer
, buffer
, _buffer
);
1036 buffer
->bo
= mem
->bo
;
1037 buffer
->offset
= memoryOffset
;
1046 VkResult
radv_BindImageMemory(
1049 VkDeviceMemory _memory
,
1050 VkDeviceSize memoryOffset
)
1052 RADV_FROM_HANDLE(radv_device_memory
, mem
, _memory
);
1053 RADV_FROM_HANDLE(radv_image
, image
, _image
);
1056 image
->bo
= mem
->bo
;
1057 image
->offset
= memoryOffset
;
1066 VkResult
radv_QueueBindSparse(
1068 uint32_t bindInfoCount
,
1069 const VkBindSparseInfo
* pBindInfo
,
1072 stub_return(VK_ERROR_INCOMPATIBLE_DRIVER
);
1075 VkResult
radv_CreateFence(
1077 const VkFenceCreateInfo
* pCreateInfo
,
1078 const VkAllocationCallbacks
* pAllocator
,
1081 RADV_FROM_HANDLE(radv_device
, device
, _device
);
1082 struct radv_fence
*fence
= vk_alloc2(&device
->alloc
, pAllocator
,
1084 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
1087 return VK_ERROR_OUT_OF_HOST_MEMORY
;
1089 memset(fence
, 0, sizeof(*fence
));
1090 fence
->submitted
= false;
1091 fence
->signalled
= !!(pCreateInfo
->flags
& VK_FENCE_CREATE_SIGNALED_BIT
);
1092 fence
->fence
= device
->ws
->create_fence();
1095 *pFence
= radv_fence_to_handle(fence
);
1100 void radv_DestroyFence(
1103 const VkAllocationCallbacks
* pAllocator
)
1105 RADV_FROM_HANDLE(radv_device
, device
, _device
);
1106 RADV_FROM_HANDLE(radv_fence
, fence
, _fence
);
1110 device
->ws
->destroy_fence(fence
->fence
);
1111 vk_free2(&device
->alloc
, pAllocator
, fence
);
1114 static uint64_t radv_get_absolute_timeout(uint64_t timeout
)
1116 uint64_t current_time
;
1119 clock_gettime(CLOCK_MONOTONIC
, &tv
);
1120 current_time
= tv
.tv_nsec
+ tv
.tv_sec
*1000000000ull;
1122 timeout
= MIN2(UINT64_MAX
- current_time
, timeout
);
1124 return current_time
+ timeout
;
1127 VkResult
radv_WaitForFences(
1129 uint32_t fenceCount
,
1130 const VkFence
* pFences
,
1134 RADV_FROM_HANDLE(radv_device
, device
, _device
);
1135 timeout
= radv_get_absolute_timeout(timeout
);
1137 if (!waitAll
&& fenceCount
> 1) {
1138 fprintf(stderr
, "radv: WaitForFences without waitAll not implemented yet\n");
1141 for (uint32_t i
= 0; i
< fenceCount
; ++i
) {
1142 RADV_FROM_HANDLE(radv_fence
, fence
, pFences
[i
]);
1143 bool expired
= false;
1145 if (fence
->signalled
)
1148 if (!fence
->submitted
)
1151 expired
= device
->ws
->fence_wait(device
->ws
, fence
->fence
, true, timeout
);
1155 fence
->signalled
= true;
1161 VkResult
radv_ResetFences(VkDevice device
,
1162 uint32_t fenceCount
,
1163 const VkFence
*pFences
)
1165 for (unsigned i
= 0; i
< fenceCount
; ++i
) {
1166 RADV_FROM_HANDLE(radv_fence
, fence
, pFences
[i
]);
1167 fence
->submitted
= fence
->signalled
= false;
1173 VkResult
radv_GetFenceStatus(VkDevice _device
, VkFence _fence
)
1175 RADV_FROM_HANDLE(radv_device
, device
, _device
);
1176 RADV_FROM_HANDLE(radv_fence
, fence
, _fence
);
1178 if (!fence
->submitted
)
1179 return VK_NOT_READY
;
1181 if (!device
->ws
->fence_wait(device
->ws
, fence
->fence
, false, 0))
1182 return VK_NOT_READY
;
1188 // Queue semaphore functions
1190 VkResult
radv_CreateSemaphore(
1192 const VkSemaphoreCreateInfo
* pCreateInfo
,
1193 const VkAllocationCallbacks
* pAllocator
,
1194 VkSemaphore
* pSemaphore
)
1196 /* The DRM execbuffer ioctl always execute in-oder, even between different
1197 * rings. As such, there's nothing to do for the user space semaphore.
1200 *pSemaphore
= (VkSemaphore
)1;
1205 void radv_DestroySemaphore(
1207 VkSemaphore semaphore
,
1208 const VkAllocationCallbacks
* pAllocator
)
1212 VkResult
radv_CreateEvent(
1214 const VkEventCreateInfo
* pCreateInfo
,
1215 const VkAllocationCallbacks
* pAllocator
,
1218 RADV_FROM_HANDLE(radv_device
, device
, _device
);
1219 struct radv_event
*event
= vk_alloc2(&device
->alloc
, pAllocator
,
1221 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
1224 return VK_ERROR_OUT_OF_HOST_MEMORY
;
1226 event
->bo
= device
->ws
->buffer_create(device
->ws
, 8, 8,
1228 RADEON_FLAG_CPU_ACCESS
);
1230 vk_free2(&device
->alloc
, pAllocator
, event
);
1231 return VK_ERROR_OUT_OF_DEVICE_MEMORY
;
1234 event
->map
= (uint64_t*)device
->ws
->buffer_map(event
->bo
);
1236 *pEvent
= radv_event_to_handle(event
);
1241 void radv_DestroyEvent(
1244 const VkAllocationCallbacks
* pAllocator
)
1246 RADV_FROM_HANDLE(radv_device
, device
, _device
);
1247 RADV_FROM_HANDLE(radv_event
, event
, _event
);
1251 device
->ws
->buffer_destroy(event
->bo
);
1252 vk_free2(&device
->alloc
, pAllocator
, event
);
1255 VkResult
radv_GetEventStatus(
1259 RADV_FROM_HANDLE(radv_event
, event
, _event
);
1261 if (*event
->map
== 1)
1262 return VK_EVENT_SET
;
1263 return VK_EVENT_RESET
;
1266 VkResult
radv_SetEvent(
1270 RADV_FROM_HANDLE(radv_event
, event
, _event
);
1276 VkResult
radv_ResetEvent(
1280 RADV_FROM_HANDLE(radv_event
, event
, _event
);
1286 VkResult
radv_CreateBuffer(
1288 const VkBufferCreateInfo
* pCreateInfo
,
1289 const VkAllocationCallbacks
* pAllocator
,
1292 RADV_FROM_HANDLE(radv_device
, device
, _device
);
1293 struct radv_buffer
*buffer
;
1295 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO
);
1297 buffer
= vk_alloc2(&device
->alloc
, pAllocator
, sizeof(*buffer
), 8,
1298 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
1300 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
1302 buffer
->size
= pCreateInfo
->size
;
1303 buffer
->usage
= pCreateInfo
->usage
;
1307 *pBuffer
= radv_buffer_to_handle(buffer
);
1312 void radv_DestroyBuffer(
1315 const VkAllocationCallbacks
* pAllocator
)
1317 RADV_FROM_HANDLE(radv_device
, device
, _device
);
1318 RADV_FROM_HANDLE(radv_buffer
, buffer
, _buffer
);
1323 vk_free2(&device
->alloc
, pAllocator
, buffer
);
1326 static inline unsigned
1327 si_tile_mode_index(const struct radv_image
*image
, unsigned level
, bool stencil
)
1330 return image
->surface
.stencil_tiling_index
[level
];
1332 return image
->surface
.tiling_index
[level
];
1336 radv_initialise_color_surface(struct radv_device
*device
,
1337 struct radv_color_buffer_info
*cb
,
1338 struct radv_image_view
*iview
)
1340 const struct vk_format_description
*desc
;
1341 unsigned ntype
, format
, swap
, endian
;
1342 unsigned blend_clamp
= 0, blend_bypass
= 0;
1343 unsigned pitch_tile_max
, slice_tile_max
, tile_mode_index
;
1345 const struct radeon_surf
*surf
= &iview
->image
->surface
;
1346 const struct radeon_surf_level
*level_info
= &surf
->level
[iview
->base_mip
];
1348 desc
= vk_format_description(iview
->vk_format
);
1350 memset(cb
, 0, sizeof(*cb
));
1352 va
= device
->ws
->buffer_get_va(iview
->bo
) + iview
->image
->offset
;
1353 va
+= level_info
->offset
;
1354 cb
->cb_color_base
= va
>> 8;
1356 /* CMASK variables */
1357 va
= device
->ws
->buffer_get_va(iview
->bo
) + iview
->image
->offset
;
1358 va
+= iview
->image
->cmask
.offset
;
1359 cb
->cb_color_cmask
= va
>> 8;
1360 cb
->cb_color_cmask_slice
= iview
->image
->cmask
.slice_tile_max
;
1362 va
= device
->ws
->buffer_get_va(iview
->bo
) + iview
->image
->offset
;
1363 va
+= iview
->image
->dcc_offset
;
1364 cb
->cb_dcc_base
= va
>> 8;
1366 cb
->cb_color_view
= S_028C6C_SLICE_START(iview
->base_layer
) |
1367 S_028C6C_SLICE_MAX(iview
->base_layer
+ iview
->extent
.depth
- 1);
1369 cb
->micro_tile_mode
= iview
->image
->surface
.micro_tile_mode
;
1370 pitch_tile_max
= level_info
->nblk_x
/ 8 - 1;
1371 slice_tile_max
= (level_info
->nblk_x
* level_info
->nblk_y
) / 64 - 1;
1372 tile_mode_index
= si_tile_mode_index(iview
->image
, iview
->base_mip
, false);
1374 cb
->cb_color_pitch
= S_028C64_TILE_MAX(pitch_tile_max
);
1375 cb
->cb_color_slice
= S_028C68_TILE_MAX(slice_tile_max
);
1377 /* Intensity is implemented as Red, so treat it that way. */
1378 cb
->cb_color_attrib
= S_028C74_FORCE_DST_ALPHA_1(desc
->swizzle
[3] == VK_SWIZZLE_1
) |
1379 S_028C74_TILE_MODE_INDEX(tile_mode_index
);
1381 if (iview
->image
->samples
> 1) {
1382 unsigned log_samples
= util_logbase2(iview
->image
->samples
);
1384 cb
->cb_color_attrib
|= S_028C74_NUM_SAMPLES(log_samples
) |
1385 S_028C74_NUM_FRAGMENTS(log_samples
);
1388 if (iview
->image
->fmask
.size
) {
1389 va
= device
->ws
->buffer_get_va(iview
->bo
) + iview
->image
->offset
+ iview
->image
->fmask
.offset
;
1390 if (device
->instance
->physicalDevice
.rad_info
.chip_class
>= CIK
)
1391 cb
->cb_color_pitch
|= S_028C64_FMASK_TILE_MAX(iview
->image
->fmask
.pitch_in_pixels
/ 8 - 1);
1392 cb
->cb_color_attrib
|= S_028C74_FMASK_TILE_MODE_INDEX(iview
->image
->fmask
.tile_mode_index
);
1393 cb
->cb_color_fmask
= va
>> 8;
1394 cb
->cb_color_fmask_slice
= S_028C88_TILE_MAX(iview
->image
->fmask
.slice_tile_max
);
1396 /* This must be set for fast clear to work without FMASK. */
1397 if (device
->instance
->physicalDevice
.rad_info
.chip_class
>= CIK
)
1398 cb
->cb_color_pitch
|= S_028C64_FMASK_TILE_MAX(pitch_tile_max
);
1399 cb
->cb_color_attrib
|= S_028C74_FMASK_TILE_MODE_INDEX(tile_mode_index
);
1400 cb
->cb_color_fmask
= cb
->cb_color_base
;
1401 cb
->cb_color_fmask_slice
= S_028C88_TILE_MAX(slice_tile_max
);
1404 ntype
= radv_translate_color_numformat(iview
->vk_format
,
1406 vk_format_get_first_non_void_channel(iview
->vk_format
));
1407 format
= radv_translate_colorformat(iview
->vk_format
);
1408 if (format
== V_028C70_COLOR_INVALID
|| ntype
== ~0u)
1409 radv_finishme("Illegal color\n");
1410 swap
= radv_translate_colorswap(iview
->vk_format
, FALSE
);
1411 endian
= radv_colorformat_endian_swap(format
);
1413 /* blend clamp should be set for all NORM/SRGB types */
1414 if (ntype
== V_028C70_NUMBER_UNORM
||
1415 ntype
== V_028C70_NUMBER_SNORM
||
1416 ntype
== V_028C70_NUMBER_SRGB
)
1419 /* set blend bypass according to docs if SINT/UINT or
1420 8/24 COLOR variants */
1421 if (ntype
== V_028C70_NUMBER_UINT
|| ntype
== V_028C70_NUMBER_SINT
||
1422 format
== V_028C70_COLOR_8_24
|| format
== V_028C70_COLOR_24_8
||
1423 format
== V_028C70_COLOR_X24_8_32_FLOAT
) {
1428 if ((ntype
== V_028C70_NUMBER_UINT
|| ntype
== V_028C70_NUMBER_SINT
) &&
1429 (format
== V_028C70_COLOR_8
||
1430 format
== V_028C70_COLOR_8_8
||
1431 format
== V_028C70_COLOR_8_8_8_8
))
1432 ->color_is_int8
= true;
1434 cb
->cb_color_info
= S_028C70_FORMAT(format
) |
1435 S_028C70_COMP_SWAP(swap
) |
1436 S_028C70_BLEND_CLAMP(blend_clamp
) |
1437 S_028C70_BLEND_BYPASS(blend_bypass
) |
1438 S_028C70_SIMPLE_FLOAT(1) |
1439 S_028C70_ROUND_MODE(ntype
!= V_028C70_NUMBER_UNORM
&&
1440 ntype
!= V_028C70_NUMBER_SNORM
&&
1441 ntype
!= V_028C70_NUMBER_SRGB
&&
1442 format
!= V_028C70_COLOR_8_24
&&
1443 format
!= V_028C70_COLOR_24_8
) |
1444 S_028C70_NUMBER_TYPE(ntype
) |
1445 S_028C70_ENDIAN(endian
);
1446 if (iview
->image
->samples
> 1)
1447 if (iview
->image
->fmask
.size
)
1448 cb
->cb_color_info
|= S_028C70_COMPRESSION(1);
1450 if (iview
->image
->cmask
.size
&& device
->allow_fast_clears
)
1451 cb
->cb_color_info
|= S_028C70_FAST_CLEAR(1);
1453 if (iview
->image
->surface
.dcc_size
&& level_info
->dcc_enabled
)
1454 cb
->cb_color_info
|= S_028C70_DCC_ENABLE(1);
1456 if (device
->instance
->physicalDevice
.rad_info
.chip_class
>= VI
) {
1457 unsigned max_uncompressed_block_size
= 2;
1458 if (iview
->image
->samples
> 1) {
1459 if (iview
->image
->surface
.bpe
== 1)
1460 max_uncompressed_block_size
= 0;
1461 else if (iview
->image
->surface
.bpe
== 2)
1462 max_uncompressed_block_size
= 1;
1465 cb
->cb_dcc_control
= S_028C78_MAX_UNCOMPRESSED_BLOCK_SIZE(max_uncompressed_block_size
) |
1466 S_028C78_INDEPENDENT_64B_BLOCKS(1);
1469 /* This must be set for fast clear to work without FMASK. */
1470 if (!iview
->image
->fmask
.size
&&
1471 device
->instance
->physicalDevice
.rad_info
.chip_class
== SI
) {
1472 unsigned bankh
= util_logbase2(iview
->image
->surface
.bankh
);
1473 cb
->cb_color_attrib
|= S_028C74_FMASK_BANK_HEIGHT(bankh
);
1478 radv_initialise_ds_surface(struct radv_device
*device
,
1479 struct radv_ds_buffer_info
*ds
,
1480 struct radv_image_view
*iview
)
1482 unsigned level
= iview
->base_mip
;
1484 uint64_t va
, s_offs
, z_offs
;
1485 const struct radeon_surf_level
*level_info
= &iview
->image
->surface
.level
[level
];
1486 memset(ds
, 0, sizeof(*ds
));
1487 switch (iview
->vk_format
) {
1488 case VK_FORMAT_D24_UNORM_S8_UINT
:
1489 case VK_FORMAT_X8_D24_UNORM_PACK32
:
1490 ds
->pa_su_poly_offset_db_fmt_cntl
= S_028B78_POLY_OFFSET_NEG_NUM_DB_BITS(-24);
1491 ds
->offset_scale
= 2.0f
;
1493 case VK_FORMAT_D16_UNORM
:
1494 case VK_FORMAT_D16_UNORM_S8_UINT
:
1495 ds
->pa_su_poly_offset_db_fmt_cntl
= S_028B78_POLY_OFFSET_NEG_NUM_DB_BITS(-16);
1496 ds
->offset_scale
= 4.0f
;
1498 case VK_FORMAT_D32_SFLOAT
:
1499 case VK_FORMAT_D32_SFLOAT_S8_UINT
:
1500 ds
->pa_su_poly_offset_db_fmt_cntl
= S_028B78_POLY_OFFSET_NEG_NUM_DB_BITS(-23) |
1501 S_028B78_POLY_OFFSET_DB_IS_FLOAT_FMT(1);
1502 ds
->offset_scale
= 1.0f
;
1508 format
= radv_translate_dbformat(iview
->vk_format
);
1509 if (format
== V_028040_Z_INVALID
) {
1510 fprintf(stderr
, "Invalid DB format: %d, disabling DB.\n", iview
->vk_format
);
1513 va
= device
->ws
->buffer_get_va(iview
->bo
) + iview
->image
->offset
;
1514 s_offs
= z_offs
= va
;
1515 z_offs
+= iview
->image
->surface
.level
[level
].offset
;
1516 s_offs
+= iview
->image
->surface
.stencil_level
[level
].offset
;
1518 ds
->db_depth_view
= S_028008_SLICE_START(iview
->base_layer
) |
1519 S_028008_SLICE_MAX(iview
->base_layer
+ iview
->extent
.depth
- 1);
1520 ds
->db_depth_info
= S_02803C_ADDR5_SWIZZLE_MASK(1);
1521 ds
->db_z_info
= S_028040_FORMAT(format
) | S_028040_ZRANGE_PRECISION(1);
1523 if (iview
->image
->samples
> 1)
1524 ds
->db_z_info
|= S_028040_NUM_SAMPLES(util_logbase2(iview
->image
->samples
));
1526 if (iview
->image
->surface
.flags
& RADEON_SURF_SBUFFER
)
1527 ds
->db_stencil_info
= S_028044_FORMAT(V_028044_STENCIL_8
);
1529 ds
->db_stencil_info
= S_028044_FORMAT(V_028044_STENCIL_INVALID
);
1531 if (device
->instance
->physicalDevice
.rad_info
.chip_class
>= CIK
) {
1532 struct radeon_info
*info
= &device
->instance
->physicalDevice
.rad_info
;
1533 unsigned tiling_index
= iview
->image
->surface
.tiling_index
[level
];
1534 unsigned stencil_index
= iview
->image
->surface
.stencil_tiling_index
[level
];
1535 unsigned macro_index
= iview
->image
->surface
.macro_tile_index
;
1536 unsigned tile_mode
= info
->si_tile_mode_array
[tiling_index
];
1537 unsigned stencil_tile_mode
= info
->si_tile_mode_array
[stencil_index
];
1538 unsigned macro_mode
= info
->cik_macrotile_mode_array
[macro_index
];
1540 ds
->db_depth_info
|=
1541 S_02803C_ARRAY_MODE(G_009910_ARRAY_MODE(tile_mode
)) |
1542 S_02803C_PIPE_CONFIG(G_009910_PIPE_CONFIG(tile_mode
)) |
1543 S_02803C_BANK_WIDTH(G_009990_BANK_WIDTH(macro_mode
)) |
1544 S_02803C_BANK_HEIGHT(G_009990_BANK_HEIGHT(macro_mode
)) |
1545 S_02803C_MACRO_TILE_ASPECT(G_009990_MACRO_TILE_ASPECT(macro_mode
)) |
1546 S_02803C_NUM_BANKS(G_009990_NUM_BANKS(macro_mode
));
1547 ds
->db_z_info
|= S_028040_TILE_SPLIT(G_009910_TILE_SPLIT(tile_mode
));
1548 ds
->db_stencil_info
|= S_028044_TILE_SPLIT(G_009910_TILE_SPLIT(stencil_tile_mode
));
1550 unsigned tile_mode_index
= si_tile_mode_index(iview
->image
, level
, false);
1551 ds
->db_z_info
|= S_028040_TILE_MODE_INDEX(tile_mode_index
);
1552 tile_mode_index
= si_tile_mode_index(iview
->image
, level
, true);
1553 ds
->db_stencil_info
|= S_028044_TILE_MODE_INDEX(tile_mode_index
);
1556 if (iview
->image
->htile
.size
&& !level
) {
1557 ds
->db_z_info
|= S_028040_TILE_SURFACE_ENABLE(1) |
1558 S_028040_ALLOW_EXPCLEAR(1);
1560 if (iview
->image
->surface
.flags
& RADEON_SURF_SBUFFER
) {
1561 /* Workaround: For a not yet understood reason, the
1562 * combination of MSAA, fast stencil clear and stencil
1563 * decompress messes with subsequent stencil buffer
1564 * uses. Problem was reproduced on Verde, Bonaire,
1565 * Tonga, and Carrizo.
1567 * Disabling EXPCLEAR works around the problem.
1569 * Check piglit's arb_texture_multisample-stencil-clear
1570 * test if you want to try changing this.
1572 if (iview
->image
->samples
<= 1)
1573 ds
->db_stencil_info
|= S_028044_ALLOW_EXPCLEAR(1);
1575 /* Use all of the htile_buffer for depth if there's no stencil. */
1576 ds
->db_stencil_info
|= S_028044_TILE_STENCIL_DISABLE(1);
1578 va
= device
->ws
->buffer_get_va(iview
->bo
) + iview
->image
->offset
+
1579 iview
->image
->htile
.offset
;
1580 ds
->db_htile_data_base
= va
>> 8;
1581 ds
->db_htile_surface
= S_028ABC_FULL_CACHE(1);
1583 ds
->db_htile_data_base
= 0;
1584 ds
->db_htile_surface
= 0;
1587 ds
->db_z_read_base
= ds
->db_z_write_base
= z_offs
>> 8;
1588 ds
->db_stencil_read_base
= ds
->db_stencil_write_base
= s_offs
>> 8;
1590 ds
->db_depth_size
= S_028058_PITCH_TILE_MAX((level_info
->nblk_x
/ 8) - 1) |
1591 S_028058_HEIGHT_TILE_MAX((level_info
->nblk_y
/ 8) - 1);
1592 ds
->db_depth_slice
= S_02805C_SLICE_TILE_MAX((level_info
->nblk_x
* level_info
->nblk_y
) / 64 - 1);
1595 VkResult
radv_CreateFramebuffer(
1597 const VkFramebufferCreateInfo
* pCreateInfo
,
1598 const VkAllocationCallbacks
* pAllocator
,
1599 VkFramebuffer
* pFramebuffer
)
1601 RADV_FROM_HANDLE(radv_device
, device
, _device
);
1602 struct radv_framebuffer
*framebuffer
;
1604 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO
);
1606 size_t size
= sizeof(*framebuffer
) +
1607 sizeof(struct radv_attachment_info
) * pCreateInfo
->attachmentCount
;
1608 framebuffer
= vk_alloc2(&device
->alloc
, pAllocator
, size
, 8,
1609 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
1610 if (framebuffer
== NULL
)
1611 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
1613 framebuffer
->attachment_count
= pCreateInfo
->attachmentCount
;
1614 for (uint32_t i
= 0; i
< pCreateInfo
->attachmentCount
; i
++) {
1615 VkImageView _iview
= pCreateInfo
->pAttachments
[i
];
1616 struct radv_image_view
*iview
= radv_image_view_from_handle(_iview
);
1617 framebuffer
->attachments
[i
].attachment
= iview
;
1618 if (iview
->aspect_mask
& VK_IMAGE_ASPECT_COLOR_BIT
) {
1619 radv_initialise_color_surface(device
, &framebuffer
->attachments
[i
].cb
, iview
);
1620 } else if (iview
->aspect_mask
& (VK_IMAGE_ASPECT_DEPTH_BIT
| VK_IMAGE_ASPECT_STENCIL_BIT
)) {
1621 radv_initialise_ds_surface(device
, &framebuffer
->attachments
[i
].ds
, iview
);
1625 framebuffer
->width
= pCreateInfo
->width
;
1626 framebuffer
->height
= pCreateInfo
->height
;
1627 framebuffer
->layers
= pCreateInfo
->layers
;
1629 *pFramebuffer
= radv_framebuffer_to_handle(framebuffer
);
1633 void radv_DestroyFramebuffer(
1636 const VkAllocationCallbacks
* pAllocator
)
1638 RADV_FROM_HANDLE(radv_device
, device
, _device
);
1639 RADV_FROM_HANDLE(radv_framebuffer
, fb
, _fb
);
1643 vk_free2(&device
->alloc
, pAllocator
, fb
);
1646 static unsigned radv_tex_wrap(VkSamplerAddressMode address_mode
)
1648 switch (address_mode
) {
1649 case VK_SAMPLER_ADDRESS_MODE_REPEAT
:
1650 return V_008F30_SQ_TEX_WRAP
;
1651 case VK_SAMPLER_ADDRESS_MODE_MIRRORED_REPEAT
:
1652 return V_008F30_SQ_TEX_MIRROR
;
1653 case VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE
:
1654 return V_008F30_SQ_TEX_CLAMP_LAST_TEXEL
;
1655 case VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_BORDER
:
1656 return V_008F30_SQ_TEX_CLAMP_BORDER
;
1657 case VK_SAMPLER_ADDRESS_MODE_MIRROR_CLAMP_TO_EDGE
:
1658 return V_008F30_SQ_TEX_MIRROR_ONCE_LAST_TEXEL
;
1660 unreachable("illegal tex wrap mode");
1666 radv_tex_compare(VkCompareOp op
)
1669 case VK_COMPARE_OP_NEVER
:
1670 return V_008F30_SQ_TEX_DEPTH_COMPARE_NEVER
;
1671 case VK_COMPARE_OP_LESS
:
1672 return V_008F30_SQ_TEX_DEPTH_COMPARE_LESS
;
1673 case VK_COMPARE_OP_EQUAL
:
1674 return V_008F30_SQ_TEX_DEPTH_COMPARE_EQUAL
;
1675 case VK_COMPARE_OP_LESS_OR_EQUAL
:
1676 return V_008F30_SQ_TEX_DEPTH_COMPARE_LESSEQUAL
;
1677 case VK_COMPARE_OP_GREATER
:
1678 return V_008F30_SQ_TEX_DEPTH_COMPARE_GREATER
;
1679 case VK_COMPARE_OP_NOT_EQUAL
:
1680 return V_008F30_SQ_TEX_DEPTH_COMPARE_NOTEQUAL
;
1681 case VK_COMPARE_OP_GREATER_OR_EQUAL
:
1682 return V_008F30_SQ_TEX_DEPTH_COMPARE_GREATEREQUAL
;
1683 case VK_COMPARE_OP_ALWAYS
:
1684 return V_008F30_SQ_TEX_DEPTH_COMPARE_ALWAYS
;
1686 unreachable("illegal compare mode");
1692 radv_tex_filter(VkFilter filter
, unsigned max_ansio
)
1695 case VK_FILTER_NEAREST
:
1696 return (max_ansio
> 1 ? V_008F38_SQ_TEX_XY_FILTER_ANISO_POINT
:
1697 V_008F38_SQ_TEX_XY_FILTER_POINT
);
1698 case VK_FILTER_LINEAR
:
1699 return (max_ansio
> 1 ? V_008F38_SQ_TEX_XY_FILTER_ANISO_BILINEAR
:
1700 V_008F38_SQ_TEX_XY_FILTER_BILINEAR
);
1701 case VK_FILTER_CUBIC_IMG
:
1703 fprintf(stderr
, "illegal texture filter");
1709 radv_tex_mipfilter(VkSamplerMipmapMode mode
)
1712 case VK_SAMPLER_MIPMAP_MODE_NEAREST
:
1713 return V_008F38_SQ_TEX_Z_FILTER_POINT
;
1714 case VK_SAMPLER_MIPMAP_MODE_LINEAR
:
1715 return V_008F38_SQ_TEX_Z_FILTER_LINEAR
;
1717 return V_008F38_SQ_TEX_Z_FILTER_NONE
;
1722 radv_tex_bordercolor(VkBorderColor bcolor
)
1725 case VK_BORDER_COLOR_FLOAT_TRANSPARENT_BLACK
:
1726 case VK_BORDER_COLOR_INT_TRANSPARENT_BLACK
:
1727 return V_008F3C_SQ_TEX_BORDER_COLOR_TRANS_BLACK
;
1728 case VK_BORDER_COLOR_FLOAT_OPAQUE_BLACK
:
1729 case VK_BORDER_COLOR_INT_OPAQUE_BLACK
:
1730 return V_008F3C_SQ_TEX_BORDER_COLOR_OPAQUE_BLACK
;
1731 case VK_BORDER_COLOR_FLOAT_OPAQUE_WHITE
:
1732 case VK_BORDER_COLOR_INT_OPAQUE_WHITE
:
1733 return V_008F3C_SQ_TEX_BORDER_COLOR_OPAQUE_WHITE
;
1741 radv_init_sampler(struct radv_device
*device
,
1742 struct radv_sampler
*sampler
,
1743 const VkSamplerCreateInfo
*pCreateInfo
)
1745 uint32_t max_aniso
= 0;
1746 uint32_t max_aniso_ratio
= 0;//TODO
1748 is_vi
= (device
->instance
->physicalDevice
.rad_info
.chip_class
>= VI
);
1750 sampler
->state
[0] = (S_008F30_CLAMP_X(radv_tex_wrap(pCreateInfo
->addressModeU
)) |
1751 S_008F30_CLAMP_Y(radv_tex_wrap(pCreateInfo
->addressModeV
)) |
1752 S_008F30_CLAMP_Z(radv_tex_wrap(pCreateInfo
->addressModeW
)) |
1753 S_008F30_MAX_ANISO_RATIO(max_aniso_ratio
) |
1754 S_008F30_DEPTH_COMPARE_FUNC(radv_tex_compare(pCreateInfo
->compareOp
)) |
1755 S_008F30_FORCE_UNNORMALIZED(pCreateInfo
->unnormalizedCoordinates
? 1 : 0) |
1756 S_008F30_DISABLE_CUBE_WRAP(0) |
1757 S_008F30_COMPAT_MODE(is_vi
));
1758 sampler
->state
[1] = (S_008F34_MIN_LOD(S_FIXED(CLAMP(pCreateInfo
->minLod
, 0, 15), 8)) |
1759 S_008F34_MAX_LOD(S_FIXED(CLAMP(pCreateInfo
->maxLod
, 0, 15), 8)));
1760 sampler
->state
[2] = (S_008F38_LOD_BIAS(S_FIXED(CLAMP(pCreateInfo
->mipLodBias
, -16, 16), 8)) |
1761 S_008F38_XY_MAG_FILTER(radv_tex_filter(pCreateInfo
->magFilter
, max_aniso
)) |
1762 S_008F38_XY_MIN_FILTER(radv_tex_filter(pCreateInfo
->minFilter
, max_aniso
)) |
1763 S_008F38_MIP_FILTER(radv_tex_mipfilter(pCreateInfo
->mipmapMode
)) |
1764 S_008F38_MIP_POINT_PRECLAMP(1) |
1765 S_008F38_DISABLE_LSB_CEIL(1) |
1766 S_008F38_FILTER_PREC_FIX(1) |
1767 S_008F38_ANISO_OVERRIDE(is_vi
));
1768 sampler
->state
[3] = (S_008F3C_BORDER_COLOR_PTR(0) |
1769 S_008F3C_BORDER_COLOR_TYPE(radv_tex_bordercolor(pCreateInfo
->borderColor
)));
1772 VkResult
radv_CreateSampler(
1774 const VkSamplerCreateInfo
* pCreateInfo
,
1775 const VkAllocationCallbacks
* pAllocator
,
1776 VkSampler
* pSampler
)
1778 RADV_FROM_HANDLE(radv_device
, device
, _device
);
1779 struct radv_sampler
*sampler
;
1781 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO
);
1783 sampler
= vk_alloc2(&device
->alloc
, pAllocator
, sizeof(*sampler
), 8,
1784 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
1786 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
1788 radv_init_sampler(device
, sampler
, pCreateInfo
);
1789 *pSampler
= radv_sampler_to_handle(sampler
);
1794 void radv_DestroySampler(
1797 const VkAllocationCallbacks
* pAllocator
)
1799 RADV_FROM_HANDLE(radv_device
, device
, _device
);
1800 RADV_FROM_HANDLE(radv_sampler
, sampler
, _sampler
);
1804 vk_free2(&device
->alloc
, pAllocator
, sampler
);