radv: When importing an image, redo the layout based on the metadata.
authorBas Nieuwenhuizen <bas@basnieuwenhuizen.nl>
Mon, 4 May 2020 15:04:00 +0000 (17:04 +0200)
committerMarge Bot <eric+marge@anholt.net>
Fri, 14 Aug 2020 10:29:44 +0000 (10:29 +0000)
When importing a DMA-BUF, the image layout created in vkImageCreate may
not match the imported BO's. To make this work we redo the layout based
on the metadata of the imported image.

The original patch did a delayed allocation just as for AHB, but that
does not work for images that are not imported (but e.g. exported only).

Original patch by Simon Ser <contact@emersion.fr>

Closes: https://gitlab.freedesktop.org/mesa/mesa/-/issues/2801
CC: mesa-stable
Reviewed-by: Samuel Pitoiset <samuel.pitoiset@gmail.com>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/6274>

src/amd/vulkan/radv_device.c
src/amd/vulkan/radv_image.c

index d36f9a188d66e1297d9c4c3a76e8368ec561e29d..fd28343638d6409c79581ec275558f2bf94bf7c8 100644 (file)
@@ -5079,6 +5079,26 @@ static VkResult radv_alloc_memory(struct radv_device *device,
                } else {
                        close(import_info->fd);
                }
+
+               if (mem->image && mem->image->plane_count == 1 &&
+                   !vk_format_is_depth_or_stencil(mem->image->vk_format)) {
+                       struct radeon_bo_metadata metadata;
+                       device->ws->buffer_get_metadata(mem->bo, &metadata);
+
+                       struct radv_image_create_info create_info = {
+                               .no_metadata_planes = true,
+                               .bo_metadata = &metadata
+                       };
+
+                       /* This gives a basic ability to import radeonsi images
+                        * that don't have DCC. This is not guaranteed by any
+                        * spec and can be removed after we support modifiers. */
+                       result = radv_image_create_layout(device, create_info, mem->image);
+                       if (result != VK_SUCCESS) {
+                               device->ws->buffer_destroy(mem->bo);
+                               goto fail;
+                       }
+               }
        } else if (host_ptr_info) {
                assert(host_ptr_info->handleType == VK_EXTERNAL_MEMORY_HANDLE_TYPE_HOST_ALLOCATION_BIT_EXT);
                mem->bo = device->ws->buffer_from_ptr(device->ws, host_ptr_info->pHostPointer,
index 7782e77d1fe70e9f3f24e2f4f27eb555412f21eb..8f5b3ef37a8a6bfd4f922d612bc01970d2d7661e 100644 (file)
@@ -1283,14 +1283,40 @@ radv_image_alloc_values(const struct radv_device *device, struct radv_image *ima
        }
 }
 
+
+static void
+radv_image_reset_layout(struct radv_image *image)
+{
+       image->size = 0;
+       image->alignment = 1;
+
+       image->tc_compatible_cmask = image->tc_compatible_htile = 0;
+       image->fce_pred_offset = image->dcc_pred_offset = 0;
+       image->clear_value_offset = image->tc_compat_zrange_offset = 0;
+
+       for (unsigned i = 0; i < image->plane_count; ++i) {
+               VkFormat format = vk_format_get_plane_format(image->vk_format, i);
+
+               uint32_t flags = image->planes[i].surface.flags;
+               memset(image->planes + i, 0, sizeof(image->planes[i]));
+
+               image->planes[i].surface.flags = flags;
+               image->planes[i].surface.blk_w = vk_format_get_blockwidth(format);
+               image->planes[i].surface.blk_h = vk_format_get_blockheight(format);
+               image->planes[i].surface.bpe = vk_format_get_blocksize(vk_format_depth_only(format));
+
+               /* align byte per element on dword */
+               if (image->planes[i].surface.bpe == 3) {
+                       image->planes[i].surface.bpe = 4;
+               }
+       }
+}
+
 VkResult
 radv_image_create_layout(struct radv_device *device,
                          struct radv_image_create_info create_info,
                          struct radv_image *image)
 {
-       /* Check that we did not initialize things earlier */
-       assert(!image->planes[0].surface.surf_size);
-
        /* Clear the pCreateInfo pointer so we catch issues in the delayed case when we test in the
         * common internal case. */
        create_info.vk_info = NULL;
@@ -1300,8 +1326,8 @@ radv_image_create_layout(struct radv_device *device,
        if (result != VK_SUCCESS)
                return result;
 
-       image->size = 0;
-       image->alignment = 1;
+       radv_image_reset_layout(image);
+
        for (unsigned plane = 0; plane < image->plane_count; ++plane) {
                struct ac_surf_info info = image_info;