• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright © 2021 Collabora Ltd.
3  *
4  * Derived from tu_image.c which is:
5  * Copyright © 2016 Red Hat.
6  * Copyright © 2016 Bas Nieuwenhuizen
7  * Copyright © 2015 Intel Corporation
8  *
9  * Permission is hereby granted, free of charge, to any person obtaining a
10  * copy of this software and associated documentation files (the "Software"),
11  * to deal in the Software without restriction, including without limitation
12  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
13  * and/or sell copies of the Software, and to permit persons to whom the
14  * Software is furnished to do so, subject to the following conditions:
15  *
16  * The above copyright notice and this permission notice (including the next
17  * paragraph) shall be included in all copies or substantial portions of the
18  * Software.
19  *
20  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
23  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
25  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
26  * DEALINGS IN THE SOFTWARE.
27  */
28 
29 #include "pan_props.h"
30 
31 #include "panvk_private.h"
32 
33 #include "drm-uapi/drm_fourcc.h"
34 #include "util/u_atomic.h"
35 #include "util/u_debug.h"
36 #include "vk_format.h"
37 #include "vk_object.h"
38 #include "vk_util.h"
39 
40 unsigned
panvk_image_get_plane_size(const struct panvk_image * image,unsigned plane)41 panvk_image_get_plane_size(const struct panvk_image *image, unsigned plane)
42 {
43    assert(!plane);
44    return image->pimage.layout.data_size;
45 }
46 
47 unsigned
panvk_image_get_total_size(const struct panvk_image * image)48 panvk_image_get_total_size(const struct panvk_image *image)
49 {
50    assert(util_format_get_num_planes(image->pimage.layout.format) == 1);
51    return image->pimage.layout.data_size;
52 }
53 
54 static enum mali_texture_dimension
panvk_image_type_to_mali_tex_dim(VkImageType type)55 panvk_image_type_to_mali_tex_dim(VkImageType type)
56 {
57    switch (type) {
58    case VK_IMAGE_TYPE_1D:
59       return MALI_TEXTURE_DIMENSION_1D;
60    case VK_IMAGE_TYPE_2D:
61       return MALI_TEXTURE_DIMENSION_2D;
62    case VK_IMAGE_TYPE_3D:
63       return MALI_TEXTURE_DIMENSION_3D;
64    default:
65       unreachable("Invalid image type");
66    }
67 }
68 
69 static VkResult
panvk_image_create(VkDevice _device,const VkImageCreateInfo * pCreateInfo,const VkAllocationCallbacks * alloc,VkImage * pImage,uint64_t modifier,const VkSubresourceLayout * plane_layouts)70 panvk_image_create(VkDevice _device, const VkImageCreateInfo *pCreateInfo,
71                    const VkAllocationCallbacks *alloc, VkImage *pImage,
72                    uint64_t modifier, const VkSubresourceLayout *plane_layouts)
73 {
74    VK_FROM_HANDLE(panvk_device, device, _device);
75    struct panvk_image *image = NULL;
76 
77    image = vk_image_create(&device->vk, pCreateInfo, alloc, sizeof(*image));
78    if (!image)
79       return vk_error(device, VK_ERROR_OUT_OF_HOST_MEMORY);
80 
81    image->pimage.layout = (struct pan_image_layout){
82       .modifier = modifier,
83       .format = vk_format_to_pipe_format(image->vk.format),
84       .dim = panvk_image_type_to_mali_tex_dim(image->vk.image_type),
85       .width = image->vk.extent.width,
86       .height = image->vk.extent.height,
87       .depth = image->vk.extent.depth,
88       .array_size = image->vk.array_layers,
89       .nr_samples = image->vk.samples,
90       .nr_slices = image->vk.mip_levels,
91    };
92 
93    unsigned arch = pan_arch(device->physical_device->kmod.props.gpu_prod_id);
94    pan_image_layout_init(arch, &image->pimage.layout, NULL);
95 
96    *pImage = panvk_image_to_handle(image);
97    return VK_SUCCESS;
98 }
99 
100 static uint64_t
panvk_image_select_mod(VkDevice _device,const VkImageCreateInfo * pCreateInfo,const VkSubresourceLayout ** plane_layouts)101 panvk_image_select_mod(VkDevice _device, const VkImageCreateInfo *pCreateInfo,
102                        const VkSubresourceLayout **plane_layouts)
103 {
104    VK_FROM_HANDLE(panvk_device, device, _device);
105    enum pipe_format fmt = vk_format_to_pipe_format(pCreateInfo->format);
106    bool noafbc =
107       !(device->physical_device->instance->debug_flags & PANVK_DEBUG_AFBC);
108    bool linear =
109       device->physical_device->instance->debug_flags & PANVK_DEBUG_LINEAR;
110 
111    *plane_layouts = NULL;
112 
113    if (pCreateInfo->tiling == VK_IMAGE_TILING_LINEAR)
114       return DRM_FORMAT_MOD_LINEAR;
115 
116    if (pCreateInfo->tiling == VK_IMAGE_TILING_DRM_FORMAT_MODIFIER_EXT) {
117       const VkImageDrmFormatModifierListCreateInfoEXT *mod_info =
118          vk_find_struct_const(pCreateInfo->pNext,
119                               IMAGE_DRM_FORMAT_MODIFIER_LIST_CREATE_INFO_EXT);
120       const VkImageDrmFormatModifierExplicitCreateInfoEXT *drm_explicit_info =
121          vk_find_struct_const(
122             pCreateInfo->pNext,
123             IMAGE_DRM_FORMAT_MODIFIER_EXPLICIT_CREATE_INFO_EXT);
124 
125       assert(mod_info || drm_explicit_info);
126 
127       uint64_t modifier;
128 
129       if (mod_info) {
130          modifier = DRM_FORMAT_MOD_LINEAR;
131          for (unsigned i = 0; i < mod_info->drmFormatModifierCount; i++) {
132             if (drm_is_afbc(mod_info->pDrmFormatModifiers[i]) && !noafbc) {
133                modifier = mod_info->pDrmFormatModifiers[i];
134                break;
135             }
136          }
137       } else {
138          modifier = drm_explicit_info->drmFormatModifier;
139          assert(modifier == DRM_FORMAT_MOD_LINEAR ||
140                 modifier == DRM_FORMAT_MOD_ARM_16X16_BLOCK_U_INTERLEAVED ||
141                 (drm_is_afbc(modifier) && !noafbc));
142          *plane_layouts = drm_explicit_info->pPlaneLayouts;
143       }
144 
145       return modifier;
146    }
147 
148    const struct wsi_image_create_info *wsi_info =
149       vk_find_struct_const(pCreateInfo->pNext, WSI_IMAGE_CREATE_INFO_MESA);
150    if (wsi_info && wsi_info->scanout)
151       return DRM_FORMAT_MOD_LINEAR;
152 
153    assert(pCreateInfo->tiling == VK_IMAGE_TILING_OPTIMAL);
154 
155    if (linear)
156       return DRM_FORMAT_MOD_LINEAR;
157 
158    /* Image store don't work on AFBC images */
159    if (pCreateInfo->usage & VK_IMAGE_USAGE_STORAGE_BIT)
160       return DRM_FORMAT_MOD_ARM_16X16_BLOCK_U_INTERLEAVED;
161 
162    /* AFBC does not support layered multisampling */
163    if (pCreateInfo->samples > 1)
164       return DRM_FORMAT_MOD_ARM_16X16_BLOCK_U_INTERLEAVED;
165 
166    if (!panfrost_query_afbc(&device->physical_device->kmod.props))
167       return DRM_FORMAT_MOD_ARM_16X16_BLOCK_U_INTERLEAVED;
168 
169    /* Only a small selection of formats are AFBC'able */
170    unsigned arch = pan_arch(device->physical_device->kmod.props.gpu_prod_id);
171    if (!panfrost_format_supports_afbc(arch, fmt))
172       return DRM_FORMAT_MOD_ARM_16X16_BLOCK_U_INTERLEAVED;
173 
174    /* 3D AFBC is only supported on Bifrost v7+. It's supposed to
175     * be supported on Midgard but it doesn't seem to work.
176     */
177    if (pCreateInfo->imageType == VK_IMAGE_TYPE_3D && arch < 7)
178       return DRM_FORMAT_MOD_ARM_16X16_BLOCK_U_INTERLEAVED;
179 
180    /* For one tile, AFBC is a loss compared to u-interleaved */
181    if (pCreateInfo->extent.width <= 16 && pCreateInfo->extent.height <= 16)
182       return DRM_FORMAT_MOD_ARM_16X16_BLOCK_U_INTERLEAVED;
183 
184    if (noafbc)
185       return DRM_FORMAT_MOD_ARM_16X16_BLOCK_U_INTERLEAVED;
186 
187    uint64_t afbc_type =
188       AFBC_FORMAT_MOD_BLOCK_SIZE_16x16 | AFBC_FORMAT_MOD_SPARSE;
189 
190    if (panfrost_afbc_can_ytr(fmt))
191       afbc_type |= AFBC_FORMAT_MOD_YTR;
192 
193    return DRM_FORMAT_MOD_ARM_AFBC(afbc_type);
194 }
195 
196 VkResult
panvk_CreateImage(VkDevice device,const VkImageCreateInfo * pCreateInfo,const VkAllocationCallbacks * pAllocator,VkImage * pImage)197 panvk_CreateImage(VkDevice device, const VkImageCreateInfo *pCreateInfo,
198                   const VkAllocationCallbacks *pAllocator, VkImage *pImage)
199 {
200    const VkSubresourceLayout *plane_layouts;
201    uint64_t modifier =
202       panvk_image_select_mod(device, pCreateInfo, &plane_layouts);
203 
204    return panvk_image_create(device, pCreateInfo, pAllocator, pImage, modifier,
205                              plane_layouts);
206 }
207 
208 void
panvk_DestroyImage(VkDevice _device,VkImage _image,const VkAllocationCallbacks * pAllocator)209 panvk_DestroyImage(VkDevice _device, VkImage _image,
210                    const VkAllocationCallbacks *pAllocator)
211 {
212    VK_FROM_HANDLE(panvk_device, device, _device);
213    VK_FROM_HANDLE(panvk_image, image, _image);
214 
215    if (!image)
216       return;
217 
218    if (image->bo)
219       pan_kmod_bo_put(image->bo);
220 
221    vk_image_destroy(&device->vk, pAllocator, &image->vk);
222 }
223 
224 static unsigned
panvk_plane_index(VkFormat format,VkImageAspectFlags aspect_mask)225 panvk_plane_index(VkFormat format, VkImageAspectFlags aspect_mask)
226 {
227    switch (aspect_mask) {
228    default:
229       return 0;
230    case VK_IMAGE_ASPECT_PLANE_1_BIT:
231       return 1;
232    case VK_IMAGE_ASPECT_PLANE_2_BIT:
233       return 2;
234    case VK_IMAGE_ASPECT_STENCIL_BIT:
235       return format == VK_FORMAT_D32_SFLOAT_S8_UINT;
236    }
237 }
238 
239 void
panvk_GetImageSubresourceLayout(VkDevice _device,VkImage _image,const VkImageSubresource * pSubresource,VkSubresourceLayout * pLayout)240 panvk_GetImageSubresourceLayout(VkDevice _device, VkImage _image,
241                                 const VkImageSubresource *pSubresource,
242                                 VkSubresourceLayout *pLayout)
243 {
244    VK_FROM_HANDLE(panvk_image, image, _image);
245 
246    unsigned plane =
247       panvk_plane_index(image->vk.format, pSubresource->aspectMask);
248    assert(plane < PANVK_MAX_PLANES);
249 
250    const struct pan_image_slice_layout *slice_layout =
251       &image->pimage.layout.slices[pSubresource->mipLevel];
252 
253    pLayout->offset = slice_layout->offset + (pSubresource->arrayLayer *
254                                              image->pimage.layout.array_stride);
255    pLayout->size = slice_layout->size;
256    pLayout->rowPitch = slice_layout->row_stride;
257    pLayout->arrayPitch = image->pimage.layout.array_stride;
258    pLayout->depthPitch = slice_layout->surface_stride;
259 }
260 
261 void
panvk_DestroyImageView(VkDevice _device,VkImageView _view,const VkAllocationCallbacks * pAllocator)262 panvk_DestroyImageView(VkDevice _device, VkImageView _view,
263                        const VkAllocationCallbacks *pAllocator)
264 {
265    VK_FROM_HANDLE(panvk_device, device, _device);
266    VK_FROM_HANDLE(panvk_image_view, view, _view);
267 
268    if (!view)
269       return;
270 
271    panvk_priv_bo_destroy(view->bo, NULL);
272    vk_image_view_destroy(&device->vk, pAllocator, &view->vk);
273 }
274 
275 void
panvk_DestroyBufferView(VkDevice _device,VkBufferView bufferView,const VkAllocationCallbacks * pAllocator)276 panvk_DestroyBufferView(VkDevice _device, VkBufferView bufferView,
277                         const VkAllocationCallbacks *pAllocator)
278 {
279    VK_FROM_HANDLE(panvk_device, device, _device);
280    VK_FROM_HANDLE(panvk_buffer_view, view, bufferView);
281 
282    if (!view)
283       return;
284 
285    panvk_priv_bo_destroy(view->bo, pAllocator);
286    vk_object_free(&device->vk, pAllocator, view);
287 }
288 
289 VkResult
panvk_GetImageDrmFormatModifierPropertiesEXT(VkDevice device,VkImage _image,VkImageDrmFormatModifierPropertiesEXT * pProperties)290 panvk_GetImageDrmFormatModifierPropertiesEXT(
291    VkDevice device, VkImage _image,
292    VkImageDrmFormatModifierPropertiesEXT *pProperties)
293 {
294    VK_FROM_HANDLE(panvk_image, image, _image);
295 
296    assert(pProperties->sType ==
297           VK_STRUCTURE_TYPE_IMAGE_DRM_FORMAT_MODIFIER_PROPERTIES_EXT);
298 
299    pProperties->drmFormatModifier = image->pimage.layout.modifier;
300    return VK_SUCCESS;
301 }
302