Debian Codegraph @

src/panfrost/vulkan/panvk_vX_image_view.c

raw
1 /*
2 * Copyright © 2025 Arm Ltd.
3 * Copyright © 2021 Collabora Ltd.
4 *
5 * Derived from tu_image.c which is:
6 * Copyright © 2016 Red Hat.
7 * Copyright © 2016 Bas Nieuwenhuizen
8 * Copyright © 2015 Intel Corporation
9 *
10 * SPDX-License-Identifier: MIT
11 */
12
13 #include "vk_format.h"
14 #include "vk_log.h"
15
16 #include "panvk_device.h"
17 #include "panvk_entrypoints.h"
18 #include "panvk_image.h"
19 #include "panvk_image_view.h"
20 #include "panvk_priv_bo.h"
21
22 #include "pan_afbc.h"
23 #include "pan_texture.h"
24
25 #include "genxml/gen_macros.h"
26
27 static enum mali_texture_dimension
28 panvk_view_type_to_mali_tex_dim(VkImageViewType type)
29 {
30 switch (type) {
31 case VK_IMAGE_VIEW_TYPE_1D:
32 case VK_IMAGE_VIEW_TYPE_1D_ARRAY:
33 return MALI_TEXTURE_DIMENSION_1D;
34 case VK_IMAGE_VIEW_TYPE_2D:
35 case VK_IMAGE_VIEW_TYPE_2D_ARRAY:
36 return MALI_TEXTURE_DIMENSION_2D;
37 case VK_IMAGE_VIEW_TYPE_3D:
38 return MALI_TEXTURE_DIMENSION_3D;
39 case VK_IMAGE_VIEW_TYPE_CUBE:
40 case VK_IMAGE_VIEW_TYPE_CUBE_ARRAY:
41 return MALI_TEXTURE_DIMENSION_CUBE;
42 default:
43 UNREACHABLE("Invalid view type");
44 }
45 }
46
47 static void
48 panvk_convert_swizzle(const VkComponentMapping *in, unsigned char *out)
49 {
50 const VkComponentSwizzle *comp = &in->r;
51 for (unsigned i = 0; i < 4; i++) {
52 switch (comp[i]) {
53 case VK_COMPONENT_SWIZZLE_ZERO:
54 out[i] = PIPE_SWIZZLE_0;
55 break;
56 case VK_COMPONENT_SWIZZLE_ONE:
57 out[i] = PIPE_SWIZZLE_1;
58 break;
59 case VK_COMPONENT_SWIZZLE_R:
60 out[i] = PIPE_SWIZZLE_X;
61 break;
62 case VK_COMPONENT_SWIZZLE_G:
63 out[i] = PIPE_SWIZZLE_Y;
64 break;
65 case VK_COMPONENT_SWIZZLE_B:
66 out[i] = PIPE_SWIZZLE_Z;
67 break;
68 case VK_COMPONENT_SWIZZLE_A:
69 out[i] = PIPE_SWIZZLE_W;
70 break;
71 default:
72 UNREACHABLE("Invalid swizzle");
73 }
74 }
75 }
76
77 static VkResult
78 prepare_tex_descs(struct panvk_image_view *view)
79 {
80 /* Use a temporary pan_image_view so we can tweak it for texture
81 * descriptor emission without changing the original definition.
82 */
83 struct pan_image_view pview = view->pview;
84 struct panvk_image *image =
85 container_of(view->vk.image, struct panvk_image, vk);
86 struct panvk_device *dev = to_panvk_device(view->vk.base.device);
87 #if PAN_ARCH >= 9
88 bool has_storage = (view->vk.usage & VK_IMAGE_USAGE_STORAGE_BIT) &&
89 !vk_format_is_compressed(view->vk.format);
90 #endif
91
92 if (util_format_is_depth_or_stencil(view->pview.format)) {
93 /* Vulkan wants R001, where the depth/stencil is stored in the red
94 * component. Tweak the swizzle so we get what Vulkan wants.
95 */
96 static const unsigned char r001[4] = {
97 PIPE_SWIZZLE_X,
98 PIPE_SWIZZLE_0,
99 PIPE_SWIZZLE_0,
100 PIPE_SWIZZLE_1,
101 };
102
103 util_format_compose_swizzles(r001, view->pview.swizzle, pview.swizzle);
104 }
105 #if PAN_ARCH == 7
106 /* v7 requires AFBC reswizzle. */
107 else if (!pan_format_is_yuv(view->pview.format) &&
108 pan_afbc_supports_format(PAN_ARCH, view->pview.format))
109 GENX(pan_texture_afbc_reswizzle)(&pview);
110 #endif
111
112 /* If the view contains both stencil and depth, we need to keep only the
113 * depth. We'll create another texture with only the stencil.
114 */
115 if (view->vk.aspects & VK_IMAGE_ASPECT_DEPTH_BIT) {
116 /* View and image formats must match. */
117 assert(view->vk.format == vk_format_depth_only(image->vk.format) ||
118 view->vk.format == image->vk.format);
119 pview.format = panvk_image_depth_only_pfmt(image);
120 }
121
122 uint32_t plane_count = vk_format_get_plane_count(view->vk.format);
123 uint32_t tex_payload_size = GENX(pan_texture_estimate_payload_size)(&pview);
124
125 struct panvk_pool_alloc_info alloc_info = {
126 #if PAN_ARCH == 6
127 .alignment = pan_alignment(SURFACE_WITH_STRIDE),
128 #elif PAN_ARCH == 7
129 .alignment = (plane_count > 1)
130 ? pan_alignment(MULTIPLANAR_SURFACE)
131 : pan_alignment(SURFACE_WITH_STRIDE),
132 #else
133 .alignment = pan_alignment(NULL_PLANE) * (plane_count > 1 ? 2 : 1),
134 #endif
135
136 .size = tex_payload_size * plane_count,
137 };
138
139 #if PAN_ARCH >= 9
140 uint32_t storage_payload_size = 0;
141 if (has_storage) {
142 /* We'll need a second set of Texture Descriptors for storage use. */
143 storage_payload_size = tex_payload_size * plane_count;
144 alloc_info.size += storage_payload_size;
145 }
146 #endif
147
148 view->mem = panvk_pool_alloc_mem(&dev->mempools.rw, alloc_info);
149 if (!panvk_priv_mem_check_alloc(view->mem))
150 return panvk_error(dev, VK_ERROR_OUT_OF_DEVICE_MEMORY);
151
152 panvk_priv_mem_write_array(view->mem, 0, uint8_t, alloc_info.size, cpu_ptr) {
153 struct pan_ptr ptr = {
154 .gpu = panvk_priv_mem_dev_addr(view->mem),
155 .cpu = cpu_ptr,
156 };
157
158 #if PAN_ARCH >= 9
159 struct pan_ptr storage_ptr = ptr;
160 if (has_storage) {
161 uint32_t storage_payload_offset =
162 alloc_info.size - storage_payload_size;
163 storage_ptr = pan_ptr_offset(storage_ptr, storage_payload_offset);
164 }
165 #endif
166
167 if (plane_count > 1) {
168 memset(pview.planes, 0, sizeof(pview.planes));
169
170 for (uint32_t plane = 0; plane < plane_count; plane++) {
171 VkFormat plane_format =
172 vk_format_get_plane_format(view->vk.view_format, plane);
173
174 /* We need a per-plane pview. */
175 pview.planes[0] = view->pview.planes[plane];
176 pview.format = vk_format_to_pipe_format(plane_format);
177
178 GENX(pan_sampled_texture_emit)(&pview, &view->descs.tex[plane],
179 &ptr);
180 #if PAN_ARCH >= 9
181 if (has_storage) {
182 GENX(pan_storage_texture_emit)(
183 &pview, &view->descs.storage_tex[plane], &storage_ptr);
184 storage_ptr = pan_ptr_offset(storage_ptr, tex_payload_size);
185 }
186 #endif
187
188 ptr = pan_ptr_offset(ptr, tex_payload_size);
189 }
190 } else {
191 GENX(pan_sampled_texture_emit)(&pview, &view->descs.tex[0], &ptr);
192 #if PAN_ARCH >= 9
193 if (has_storage)
194 GENX(pan_storage_texture_emit)(&pview, &view->descs.storage_tex[0],
195 &storage_ptr);
196 #endif
197 }
198 }
199
200 return VK_SUCCESS;
201 }
202
203 #if PAN_ARCH < 9
204 static void
205 prepare_attr_buf_descs(struct panvk_image_view *view)
206 {
207 struct panvk_image *image =
208 container_of(view->vk.image, struct panvk_image, vk);
209 unsigned plane_idx = panvk_image_view_plane_index(view);
210
211 const struct pan_image_props *plane_props =
212 &image->planes[plane_idx].image.props;
213 const struct pan_image_layout *plane_layout =
214 &image->planes[plane_idx].plane.layout;
215 const struct pan_image_slice_layout *slayout =
216 &plane_layout->slices[view->pview.first_level];
217 bool is_3d = plane_props->dim == MALI_TEXTURE_DIMENSION_3D;
218 unsigned offset =
219 slayout->offset_B + (view->pview.first_layer *
220 (is_3d ? slayout->tiled_or_linear.surface_stride_B
221 : plane_layout->array_stride_B));
222
223 unsigned nr_samples = image->planes[plane_idx].image.props.nr_samples;
224 nr_samples = (nr_samples > 0) ? nr_samples : 1;
225 unsigned log2_nr_samples = util_logbase2(nr_samples);
226
227 pan_pack(&view->descs.img_attrib_buf[0], ATTRIBUTE_BUFFER, cfg) {
228 /* The format is the only thing we lack to emit attribute descriptors
229 * when copying from the set to the attribute tables. Instead of
230 * making the descriptor size to store an extra format, we pack
231 * the 22-bit format with the texel stride, which is expected to be
232 * fit in 7 bits, followed by 3 bits for log2(nr_samples), which we
233 * need in order to reconstruct the number of layers in multisampled
234 * arrays.
235 */
236 uint32_t fmt_blksize = util_format_get_blocksize(view->pview.format);
237 uint32_t hw_fmt =
238 GENX(pan_format_from_pipe_format)(view->pview.format)->hw;
239
240 assert(fmt_blksize <= BITFIELD_MASK(7));
241 assert(log2_nr_samples <= BITFIELD_MASK(3));
242 assert(hw_fmt <= BITFIELD_MASK(22));
243
244 cfg.type = image->vk.drm_format_mod == DRM_FORMAT_MOD_LINEAR
245 ? MALI_ATTRIBUTE_TYPE_3D_LINEAR
246 : MALI_ATTRIBUTE_TYPE_3D_INTERLEAVED;
247 cfg.pointer = image->planes[plane_idx].plane.base + offset;
248 cfg.stride = fmt_blksize | (log2_nr_samples << 7) | (hw_fmt << 10);
249 cfg.size = pan_image_mip_level_size(&image->planes[plane_idx].image, 0,
250 view->pview.first_level);
251 }
252
253 struct mali_attribute_buffer_packed *buf = &view->descs.img_attrib_buf[1];
254 pan_cast_and_pack(buf, ATTRIBUTE_BUFFER_CONTINUATION_3D, cfg) {
255 VkExtent3D extent = view->vk.extent;
256
257 cfg.s_dimension = extent.width;
258 cfg.t_dimension = extent.height;
259 cfg.r_dimension =
260 view->pview.dim == MALI_TEXTURE_DIMENSION_3D
261 ? extent.depth
262 : (view->pview.last_layer - view->pview.first_layer + 1);
263 cfg.row_stride = slayout->tiled_or_linear.row_stride_B;
264 if (cfg.r_dimension > 1 || nr_samples > 1) {
265 cfg.slice_stride = view->pview.dim == MALI_TEXTURE_DIMENSION_3D
266 ? slayout->tiled_or_linear.surface_stride_B
267 : plane_layout->array_stride_B;
268 }
269 if (nr_samples > 1) {
270 cfg.r_dimension *= nr_samples;
271 cfg.slice_stride /= nr_samples;
272 }
273 }
274 }
275 #endif
276
277 static void
278 create_ms_views(struct panvk_device *dev, struct panvk_image_view *view,
279 const VkImageViewCreateInfo *pCreateInfo,
280 const VkAllocationCallbacks *pAllocator)
281 {
282 /* Don't create extra views for internal views. */
283 if (pCreateInfo->flags & VK_IMAGE_VIEW_CREATE_DRIVER_INTERNAL_BIT_MESA)
284 return;
285
286 struct panvk_image *source_img =
287 panvk_image_from_handle(vk_image_to_handle(view->vk.image));
288 const VkImage *target_images = source_img->ms_imgs;
289
290 VkImageViewCreateInfo create_info = *pCreateInfo;
291
292 for (uint32_t idx = 0; idx < ARRAY_SIZE(source_img->ms_imgs); ++idx) {
293 if (target_images[idx] == VK_NULL_HANDLE)
294 continue;
295
296 create_info.image = target_images[idx];
297
298 assert(vk_image_from_handle(create_info.image)->format ==
299 source_img->vk.format);
300 assert(view->vk.format == create_info.format);
301 assert(!(vk_image_from_handle(create_info.image)->create_flags &
302 VK_IMAGE_CREATE_MULTISAMPLED_RENDER_TO_SINGLE_SAMPLED_BIT_EXT));
303
304 panvk_per_arch(CreateImageView)(panvk_device_to_handle(dev), &create_info,
305 pAllocator, &view->ms_views[idx]);
306 }
307 }
308
309 VKAPI_ATTR VkResult VKAPI_CALL
310 panvk_per_arch(CreateImageView)(VkDevice _device,
311 const VkImageViewCreateInfo *pCreateInfo,
312 const VkAllocationCallbacks *pAllocator,
313 VkImageView *pView)
314 {
315 VK_FROM_HANDLE(panvk_device, device, _device);
316 VK_FROM_HANDLE(panvk_image, image, pCreateInfo->image);
317 struct panvk_image_view *view;
318 VkResult result;
319
320 view = vk_image_view_create(&device->vk, pCreateInfo,
321 pAllocator, sizeof(*view));
322 if (view == NULL)
323 return panvk_error(device, VK_ERROR_OUT_OF_HOST_MEMORY);
324
325 /* vk_image_view_init() sanitizes depth/stencil formats to use the
326 * single-plane format, which panvk rely on. It doesn't do this with
327 * driver-internal images, though. We have to do that ourselves.
328 */
329 if (view->vk.create_flags & VK_IMAGE_VIEW_CREATE_DRIVER_INTERNAL_BIT_MESA) {
330 if (view->vk.aspects == VK_IMAGE_ASPECT_DEPTH_BIT)
331 view->vk.view_format = vk_format_depth_only(view->vk.view_format);
332 else if (view->vk.aspects == VK_IMAGE_ASPECT_STENCIL_BIT)
333 view->vk.view_format = vk_format_stencil_only(view->vk.view_format);
334 }
335
336 enum pipe_format pfmt = vk_format_to_pipe_format(view->vk.view_format);
337 const VkImageViewASTCDecodeModeEXT *astc_decode =
338 vk_find_struct_const(pCreateInfo->pNext, IMAGE_VIEW_ASTC_DECODE_MODE_EXT);
339
340 view->pview = (struct pan_image_view){
341 .format = pfmt,
342 .astc.hdr = util_format_is_astc_hdr(pfmt),
343 .astc.narrow = astc_decode &&
344 astc_decode->decodeMode == VK_FORMAT_R8G8B8A8_UNORM,
345 .dim = panvk_view_type_to_mali_tex_dim(view->vk.view_type),
346 .nr_samples = image->vk.samples,
347 .first_level = view->vk.base_mip_level,
348 /* MIPmapping in YUV formats is not supported by the HW. */
349 .last_level = vk_format_get_ycbcr_info(view->vk.format)
350 ? view->vk.base_mip_level
351 : view->vk.base_mip_level + view->vk.level_count - 1,
352 .first_layer = view->vk.base_array_layer,
353 .last_layer = view->vk.base_array_layer + view->vk.layer_count - 1,
354 .min_lod = view->vk.min_lod,
355 };
356 panvk_convert_swizzle(&view->vk.swizzle, view->pview.swizzle);
357
358 u_foreach_bit(aspect_bit, view->vk.aspects) {
359 uint8_t image_plane = panvk_plane_index(image, 1u << aspect_bit);
360
361 /* Place the view plane at index 0 for single-plane views of multiplane
362 * formats. Does not apply to YCbCr views of multiplane images since
363 * view->vk.aspects for those will contain the full set of plane aspects.
364 */
365 uint8_t view_plane = (view->vk.aspects == VK_IMAGE_ASPECT_PLANE_1_BIT ||
366 view->vk.aspects == VK_IMAGE_ASPECT_PLANE_2_BIT) ?
367 0 : image_plane;
368 view->pview.planes[view_plane] = (struct pan_image_plane_ref) {
369 .image = &image->planes[image_plane].image,
370 .plane_idx = 0,
371 };
372 }
373
374 /* Depth/stencil are viewed as color for copies. */
375 if (view->vk.aspects == VK_IMAGE_ASPECT_COLOR_BIT &&
376 panvk_image_is_planar_depth_stencil(image) &&
377 vk_format_get_blocksize(view->vk.view_format) == 1) {
378 view->pview.planes[0] = (struct pan_image_plane_ref) {
379 .image = &image->planes[1].image,
380 .plane_idx = 0,
381 };
382 }
383
384 /* We need to patch the view format when the image contains both
385 * depth and stencil but the view only contains one of these components, so
386 * we can ignore the component we don't use.
387 */
388 if (view->vk.aspects == VK_IMAGE_ASPECT_STENCIL_BIT)
389 view->pview.format = panvk_image_stencil_only_pfmt(image);
390 else if (view->vk.aspects == VK_IMAGE_ASPECT_DEPTH_BIT)
391 view->pview.format = panvk_image_depth_only_pfmt(image);
392
393 VkImageUsageFlags tex_usage_mask = VK_IMAGE_USAGE_SAMPLED_BIT;
394
395 #if PAN_ARCH >= 9
396 /* Valhall passes a texture descriptor to LEA_TEX. */
397 tex_usage_mask |= VK_IMAGE_USAGE_STORAGE_BIT;
398 #endif
399
400 if (view->vk.usage & tex_usage_mask) {
401 result = prepare_tex_descs(view);
402 if (result != VK_SUCCESS)
403 goto err_destroy_iview;
404 }
405
406 #if PAN_ARCH < 9
407 if (view->vk.usage & VK_IMAGE_USAGE_STORAGE_BIT)
408 prepare_attr_buf_descs(view);
409 #endif
410
411 if (view->vk.image->create_flags &
412 VK_IMAGE_CREATE_MULTISAMPLED_RENDER_TO_SINGLE_SAMPLED_BIT_EXT)
413 create_ms_views(device, view, pCreateInfo, pAllocator);
414
415 *pView = panvk_image_view_to_handle(view);
416 return VK_SUCCESS;
417
418 err_destroy_iview:
419 vk_image_view_destroy(&device->vk, pAllocator, &view->vk);
420 return result;
421 }
422
423 VKAPI_ATTR void VKAPI_CALL
424 panvk_per_arch(DestroyImageView)(VkDevice _device, VkImageView _view,
425 const VkAllocationCallbacks *pAllocator)
426 {
427 VK_FROM_HANDLE(panvk_device, device, _device);
428 VK_FROM_HANDLE(panvk_image_view, view, _view);
429
430 if (!view)
431 return;
432
433 for (uint32_t i = 0; i < ARRAY_SIZE(view->ms_views); ++i) {
434 panvk_per_arch(DestroyImageView)(_device, view->ms_views[i],
435 pAllocator);
436 }
437
438 panvk_pool_free_mem(&view->mem);
439 vk_image_view_destroy(&device->vk, pAllocator, &view->vk);
440 }