• 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 "genxml/gen_macros.h"
30 #include "panvk_private.h"
31 
32 #include "util/debug.h"
33 #include "util/u_atomic.h"
34 #include "vk_format.h"
35 #include "vk_object.h"
36 #include "vk_util.h"
37 #include "drm-uapi/drm_fourcc.h"
38 
39 static enum mali_texture_dimension
panvk_view_type_to_mali_tex_dim(VkImageViewType type)40 panvk_view_type_to_mali_tex_dim(VkImageViewType type)
41 {
42    switch (type) {
43    case VK_IMAGE_VIEW_TYPE_1D:
44    case VK_IMAGE_VIEW_TYPE_1D_ARRAY:
45       return MALI_TEXTURE_DIMENSION_1D;
46    case VK_IMAGE_VIEW_TYPE_2D:
47    case VK_IMAGE_VIEW_TYPE_2D_ARRAY:
48       return MALI_TEXTURE_DIMENSION_2D;
49    case VK_IMAGE_VIEW_TYPE_3D:
50       return MALI_TEXTURE_DIMENSION_3D;
51    case VK_IMAGE_VIEW_TYPE_CUBE:
52    case VK_IMAGE_VIEW_TYPE_CUBE_ARRAY:
53       return MALI_TEXTURE_DIMENSION_CUBE;
54    default:
55       unreachable("Invalid view type");
56    }
57 }
58 
59 static void
panvk_convert_swizzle(const VkComponentMapping * in,unsigned char * out)60 panvk_convert_swizzle(const VkComponentMapping *in,
61                       unsigned char *out)
62 {
63    const VkComponentSwizzle *comp = &in->r;
64    for (unsigned i = 0; i < 4; i++) {
65       switch (comp[i]) {
66       case VK_COMPONENT_SWIZZLE_ZERO:
67          out[i] = PIPE_SWIZZLE_0;
68          break;
69       case VK_COMPONENT_SWIZZLE_ONE:
70          out[i] = PIPE_SWIZZLE_1;
71          break;
72       case VK_COMPONENT_SWIZZLE_R:
73          out[i] = PIPE_SWIZZLE_X;
74          break;
75       case VK_COMPONENT_SWIZZLE_G:
76          out[i] = PIPE_SWIZZLE_Y;
77          break;
78       case VK_COMPONENT_SWIZZLE_B:
79          out[i] = PIPE_SWIZZLE_Z;
80          break;
81       case VK_COMPONENT_SWIZZLE_A:
82          out[i] = PIPE_SWIZZLE_W;
83          break;
84       default:
85          unreachable("Invalid swizzle");
86       }
87    }
88 }
89 
90 VkResult
panvk_per_arch(CreateImageView)91 panvk_per_arch(CreateImageView)(VkDevice _device,
92                                 const VkImageViewCreateInfo *pCreateInfo,
93                                 const VkAllocationCallbacks *pAllocator,
94                                 VkImageView *pView)
95 {
96    VK_FROM_HANDLE(panvk_device, device, _device);
97    VK_FROM_HANDLE(panvk_image, image, pCreateInfo->image);
98    struct panvk_image_view *view;
99 
100    view = vk_image_view_create(&device->vk, false, pCreateInfo,
101                                pAllocator, sizeof(*view));
102    if (view == NULL)
103       return vk_error(device, VK_ERROR_OUT_OF_HOST_MEMORY);
104 
105    view->pview = (struct pan_image_view) {
106       .image = &image->pimage,
107       .format = vk_format_to_pipe_format(view->vk.view_format),
108       .dim = panvk_view_type_to_mali_tex_dim(view->vk.view_type),
109       .nr_samples = image->pimage.layout.nr_samples,
110       .first_level = view->vk.base_mip_level,
111       .last_level = view->vk.base_mip_level +
112                     view->vk.level_count - 1,
113       .first_layer = view->vk.base_array_layer,
114       .last_layer = view->vk.base_array_layer +
115                     view->vk.layer_count - 1,
116    };
117    panvk_convert_swizzle(&view->vk.swizzle, view->pview.swizzle);
118 
119    struct panfrost_device *pdev = &device->physical_device->pdev;
120 
121    if (view->vk.usage &
122        (VK_IMAGE_USAGE_SAMPLED_BIT | VK_IMAGE_USAGE_INPUT_ATTACHMENT_BIT)) {
123       unsigned bo_size =
124          GENX(panfrost_estimate_texture_payload_size)(&view->pview) +
125          pan_size(TEXTURE);
126 
127       view->bo = panfrost_bo_create(pdev, bo_size, 0, "Texture descriptor");
128 
129       STATIC_ASSERT(sizeof(view->descs.tex) >= pan_size(TEXTURE));
130       GENX(panfrost_new_texture)(pdev, &view->pview, &view->descs.tex, &view->bo->ptr);
131    }
132 
133    if (view->vk.usage & VK_IMAGE_USAGE_STORAGE_BIT) {
134       uint8_t *attrib_buf = (uint8_t *)view->descs.img_attrib_buf;
135       bool is_3d = image->pimage.layout.dim == MALI_TEXTURE_DIMENSION_3D;
136       unsigned offset = image->pimage.data.offset;
137       offset += panfrost_texture_offset(&image->pimage.layout,
138                                         view->pview.first_level,
139                                         is_3d ? 0 : view->pview.first_layer,
140                                         is_3d ? view->pview.first_layer : 0);
141 
142       pan_pack(attrib_buf, ATTRIBUTE_BUFFER, cfg) {
143          cfg.type = image->pimage.layout.modifier == DRM_FORMAT_MOD_LINEAR ?
144                     MALI_ATTRIBUTE_TYPE_3D_LINEAR : MALI_ATTRIBUTE_TYPE_3D_INTERLEAVED;
145          cfg.pointer = image->pimage.data.bo->ptr.gpu + offset;
146          cfg.stride = util_format_get_blocksize(view->pview.format);
147          cfg.size = image->pimage.data.bo->size - offset;
148       }
149 
150       attrib_buf += pan_size(ATTRIBUTE_BUFFER);
151       pan_pack(attrib_buf, ATTRIBUTE_BUFFER_CONTINUATION_3D, cfg) {
152          unsigned level = view->pview.first_level;
153 
154          cfg.s_dimension = u_minify(image->pimage.layout.width, level);
155          cfg.t_dimension = u_minify(image->pimage.layout.height, level);
156          cfg.r_dimension =
157             view->pview.dim == MALI_TEXTURE_DIMENSION_3D ?
158             u_minify(image->pimage.layout.depth, level) :
159             (view->pview.last_layer - view->pview.first_layer + 1);
160          cfg.row_stride = image->pimage.layout.slices[level].row_stride;
161          if (cfg.r_dimension > 1) {
162             cfg.slice_stride =
163                panfrost_get_layer_stride(&image->pimage.layout, level);
164          }
165       }
166    }
167 
168    *pView = panvk_image_view_to_handle(view);
169    return VK_SUCCESS;
170 }
171 
172 VkResult
panvk_per_arch(CreateBufferView)173 panvk_per_arch(CreateBufferView)(VkDevice _device,
174                                  const VkBufferViewCreateInfo *pCreateInfo,
175                                  const VkAllocationCallbacks *pAllocator,
176                                  VkBufferView *pView)
177 {
178    VK_FROM_HANDLE(panvk_device, device, _device);
179    VK_FROM_HANDLE(panvk_buffer, buffer, pCreateInfo->buffer);
180 
181    struct panvk_buffer_view *view =
182       vk_object_zalloc(&device->vk, pAllocator, sizeof(*view),
183                        VK_OBJECT_TYPE_BUFFER_VIEW);
184 
185    if (!view)
186       return vk_error(device->instance, VK_ERROR_OUT_OF_HOST_MEMORY);
187 
188    view->fmt = vk_format_to_pipe_format(pCreateInfo->format);
189 
190    struct panfrost_device *pdev = &device->physical_device->pdev;
191    mali_ptr address = panvk_buffer_gpu_ptr(buffer, pCreateInfo->offset);
192    unsigned size = panvk_buffer_range(buffer, pCreateInfo->offset,
193                                       pCreateInfo->range);
194    unsigned blksz = util_format_get_blocksize(view->fmt);
195    view->elems = size / blksz;
196 
197    assert(!(address & 63));
198 
199    if (buffer->vk.usage & VK_BUFFER_USAGE_UNIFORM_TEXEL_BUFFER_BIT) {
200       unsigned bo_size = pan_size(SURFACE_WITH_STRIDE);
201       view->bo = panfrost_bo_create(pdev, bo_size, 0, "Texture descriptor");
202 
203       pan_pack(&view->bo->ptr.cpu, SURFACE_WITH_STRIDE, cfg) {
204          cfg.pointer = address;
205       }
206 
207       pan_pack(&view->descs.tex, TEXTURE, cfg) {
208          cfg.dimension = MALI_TEXTURE_DIMENSION_1D;
209          cfg.format = pdev->formats[view->fmt].hw;
210          cfg.width = view->elems;
211          cfg.depth = cfg.height = 1;
212          cfg.swizzle = PAN_V6_SWIZZLE(R, G, B, A);
213          cfg.texel_ordering = MALI_TEXTURE_LAYOUT_LINEAR;
214          cfg.levels = 1;
215          cfg.array_size = 1;
216          cfg.surfaces = view->bo->ptr.gpu;
217          cfg.maximum_lod = cfg.minimum_lod = FIXED_16(0, false);
218       }
219    }
220 
221    if (buffer->vk.usage & VK_BUFFER_USAGE_STORAGE_TEXEL_BUFFER_BIT) {
222       uint8_t *attrib_buf = (uint8_t *)view->descs.img_attrib_buf;
223 
224       pan_pack(attrib_buf, ATTRIBUTE_BUFFER, cfg) {
225          cfg.type = MALI_ATTRIBUTE_TYPE_3D_LINEAR;
226          cfg.pointer = address;
227          cfg.stride = blksz;
228          cfg.size = view->elems * blksz;
229       }
230 
231       attrib_buf += pan_size(ATTRIBUTE_BUFFER);
232       pan_pack(attrib_buf, ATTRIBUTE_BUFFER_CONTINUATION_3D, cfg) {
233          cfg.s_dimension = view->elems;
234          cfg.t_dimension = 1;
235          cfg.r_dimension = 1;
236          cfg.row_stride = view->elems * blksz;
237       }
238    }
239 
240    *pView = panvk_buffer_view_to_handle(view);
241    return VK_SUCCESS;
242 }
243