Debian Codegraph @

src/panfrost/lib/pan_texture.c

raw
1 /*
2 * Copyright (C) 2008 VMware, Inc.
3 * Copyright (C) 2014 Broadcom
4 * Copyright (C) 2018-2019 Alyssa Rosenzweig
5 * Copyright (C) 2019-2020 Collabora, Ltd.
6 * Copyright (C) 2024 Arm Ltd.
7 * SPDX-License-Identifier: MIT
8 */
9
10 #include "pan_texture.h"
11 #include "util/macros.h"
12 #include "util/u_math.h"
13 #include "pan_afbc.h"
14 #include "pan_afrc.h"
15 #include "pan_desc.h"
16 #include "pan_format.h"
17 #include "pan_image.h"
18 #include "pan_pool.h"
19 #include "pan_util.h"
20
21 #if PAN_ARCH >= 5
22 /*
23 * Arm Scalable Texture Compression (ASTC) corresponds to just a few formats.
24 * The block dimension is not part of the format. Instead, it is encoded as a
25 * 6-bit tag on the payload pointer. Map the block size for a single dimension.
26 */
27 static inline enum mali_astc_2d_dimension
28 pan_astc_dim_2d(unsigned dim)
29 {
30 switch (dim) {
31 case 4:
32 return MALI_ASTC_2D_DIMENSION_4;
33 case 5:
34 return MALI_ASTC_2D_DIMENSION_5;
35 case 6:
36 return MALI_ASTC_2D_DIMENSION_6;
37 case 8:
38 return MALI_ASTC_2D_DIMENSION_8;
39 case 10:
40 return MALI_ASTC_2D_DIMENSION_10;
41 case 12:
42 return MALI_ASTC_2D_DIMENSION_12;
43 default:
44 UNREACHABLE("Invalid ASTC dimension");
45 }
46 }
47
48 static inline enum mali_astc_3d_dimension
49 pan_astc_dim_3d(unsigned dim)
50 {
51 switch (dim) {
52 case 3:
53 return MALI_ASTC_3D_DIMENSION_3;
54 case 4:
55 return MALI_ASTC_3D_DIMENSION_4;
56 case 5:
57 return MALI_ASTC_3D_DIMENSION_5;
58 case 6:
59 return MALI_ASTC_3D_DIMENSION_6;
60 default:
61 UNREACHABLE("Invalid ASTC dimension");
62 }
63 }
64 #endif
65
66 #if PAN_ARCH >= 5 && PAN_ARCH <= 8
67 /* Texture addresses are tagged with information about compressed formats.
68 * AFBC uses a bit for whether the colorspace transform is enabled (RGB and
69 * RGBA only). */
70 static unsigned
71 afbc_compression_tag(enum mali_texture_dimension dim, uint64_t modifier)
72 {
73 unsigned flags =
74 (modifier & AFBC_FORMAT_MOD_YTR) ? MALI_AFBC_SURFACE_FLAG_YTR : 0;
75
76 #if PAN_ARCH >= 6
77 /* Prefetch enable */
78 flags |= MALI_AFBC_SURFACE_FLAG_PREFETCH;
79
80 if (pan_afbc_is_wide(modifier))
81 flags |= MALI_AFBC_SURFACE_FLAG_WIDE_BLOCK;
82
83 if (modifier & AFBC_FORMAT_MOD_SPLIT)
84 flags |= MALI_AFBC_SURFACE_FLAG_SPLIT_BLOCK;
85 #endif
86
87 #if PAN_ARCH >= 7
88 /* Tiled headers */
89 if (modifier & AFBC_FORMAT_MOD_TILED)
90 flags |= MALI_AFBC_SURFACE_FLAG_TILED_HEADER;
91
92 /* Used to make sure AFBC headers don't point outside the AFBC
93 * body. HW is using the AFBC surface stride to do this check,
94 * which doesn't work for 3D textures because the surface
95 * stride does not cover the body. Only supported on v7+.
96 */
97 if (dim != MALI_TEXTURE_DIMENSION_3D)
98 flags |= MALI_AFBC_SURFACE_FLAG_CHECK_PAYLOAD_RANGE;
99 #endif
100
101 return flags;
102 }
103
104 /* For ASTC, this is a "stretch factor" encoding the block size. */
105 static unsigned
106 astc_compression_tag(const struct util_format_description *desc)
107 {
108 if (desc->block.depth > 1) {
109 return (pan_astc_dim_3d(desc->block.depth) << 4) |
110 (pan_astc_dim_3d(desc->block.height) << 2) |
111 pan_astc_dim_3d(desc->block.width);
112 } else {
113 return (pan_astc_dim_2d(desc->block.height) << 3) |
114 pan_astc_dim_2d(desc->block.width);
115 }
116 }
117 #endif
118
119 /* Following the texture descriptor is a number of descriptors. How many? */
120
121 static unsigned
122 pan_texture_num_elements(const struct pan_image_view *iview)
123 {
124 unsigned levels = 1 + iview->last_level - iview->first_level;
125 unsigned layers = 1 + iview->last_layer - iview->first_layer;
126 unsigned nr_samples = pan_image_view_get_nr_samples(iview);
127
128 return levels * layers * MAX2(nr_samples, 1);
129 }
130
131 /* Conservative estimate of the size of the texture payload a priori.
132 * Average case, size equal to the actual size. Worst case, off by 2x (if
133 * a manual stride is not needed on a linear texture). Returned value
134 * must be greater than or equal to the actual size, so it's safe to use
135 * as an allocation amount */
136
137 unsigned
138 GENX(pan_texture_estimate_payload_size)(const struct pan_image_view *iview)
139 {
140 size_t element_size;
141
142 #if PAN_ARCH >= 9
143 /* All plane descriptors are the same size. */
144 element_size = pan_size(NULL_PLANE);
145
146 /* 2-plane and 3-plane YUV use two plane descriptors. */
147 if (pan_format_is_yuv(iview->format) && iview->planes[1].image != NULL)
148 element_size *= 2;
149 #elif PAN_ARCH == 7
150 if (pan_format_is_yuv(iview->format))
151 element_size = pan_size(MULTIPLANAR_SURFACE);
152 else
153 element_size = pan_size(SURFACE_WITH_STRIDE);
154 #else
155 /* Assume worst case. Overestimates on Midgard, but that's ok. */
156 element_size = pan_size(SURFACE_WITH_STRIDE);
157 #endif
158
159 unsigned elements = pan_texture_num_elements(iview);
160
161 return element_size * elements;
162 }
163
164 #if PAN_ARCH >= 9
165
166 /* clang-format off */
167 #define CLUMP_FMT(pipe, mali) [PIPE_FORMAT_ ## pipe] = MALI_CLUMP_FORMAT_ ## mali
168 static enum mali_clump_format special_clump_formats[PIPE_FORMAT_COUNT] = {
169 CLUMP_FMT(X32_S8X24_UINT, X32S8X24),
170 CLUMP_FMT(X24S8_UINT, X24S8),
171 CLUMP_FMT(S8X24_UINT, S8X24),
172 CLUMP_FMT(S8_UINT, S8),
173 CLUMP_FMT(L4A4_UNORM, L4A4),
174 CLUMP_FMT(L8A8_UNORM, L8A8),
175 CLUMP_FMT(L8A8_UINT, L8A8),
176 CLUMP_FMT(L8A8_SINT, L8A8),
177 CLUMP_FMT(A8_UNORM, A8),
178 CLUMP_FMT(A8_UINT, A8),
179 CLUMP_FMT(A8_SINT, A8),
180 CLUMP_FMT(ETC1_RGB8, ETC2_RGB8),
181 CLUMP_FMT(ETC2_RGB8, ETC2_RGB8),
182 CLUMP_FMT(ETC2_SRGB8, ETC2_RGB8),
183 CLUMP_FMT(ETC2_RGB8A1, ETC2_RGB8A1),
184 CLUMP_FMT(ETC2_SRGB8A1, ETC2_RGB8A1),
185 CLUMP_FMT(ETC2_RGBA8, ETC2_RGBA8),
186 CLUMP_FMT(ETC2_SRGBA8, ETC2_RGBA8),
187 CLUMP_FMT(ETC2_R11_UNORM, ETC2_R11_UNORM),
188 CLUMP_FMT(ETC2_R11_SNORM, ETC2_R11_SNORM),
189 CLUMP_FMT(ETC2_RG11_UNORM, ETC2_RG11_UNORM),
190 CLUMP_FMT(ETC2_RG11_SNORM, ETC2_RG11_SNORM),
191 CLUMP_FMT(DXT1_RGB, BC1_UNORM),
192 CLUMP_FMT(DXT1_RGBA, BC1_UNORM),
193 CLUMP_FMT(DXT1_SRGB, BC1_UNORM),
194 CLUMP_FMT(DXT1_SRGBA, BC1_UNORM),
195 CLUMP_FMT(DXT3_RGBA, BC2_UNORM),
196 CLUMP_FMT(DXT3_SRGBA, BC2_UNORM),
197 CLUMP_FMT(DXT5_RGBA, BC3_UNORM),
198 CLUMP_FMT(DXT5_SRGBA, BC3_UNORM),
199 CLUMP_FMT(RGTC1_UNORM, BC4_UNORM),
200 CLUMP_FMT(RGTC1_SNORM, BC4_SNORM),
201 CLUMP_FMT(RGTC2_UNORM, BC5_UNORM),
202 CLUMP_FMT(RGTC2_SNORM, BC5_SNORM),
203 CLUMP_FMT(BPTC_RGB_FLOAT, BC6H_SF16),
204 CLUMP_FMT(BPTC_RGB_UFLOAT, BC6H_UF16),
205 CLUMP_FMT(BPTC_RGBA_UNORM, BC7_UNORM),
206 CLUMP_FMT(BPTC_SRGBA, BC7_UNORM),
207 };
208 #undef CLUMP_FMT
209 /* clang-format on */
210
211 static enum mali_clump_format
212 pan_clump_format(enum pipe_format format)
213 {
214 /* First, try a special clump format. Note that the 0 encoding is for a
215 * raw clump format, which will never be in the special table.
216 */
217 if (special_clump_formats[format])
218 return special_clump_formats[format];
219
220 /* Else, it's a raw format. Raw formats must not be compressed. */
221 assert(!util_format_is_compressed(format));
222
223 /* YUV-sampling has special cases */
224 if (pan_format_is_yuv(format)) {
225 switch (format) {
226 case PIPE_FORMAT_R8G8_R8B8_UNORM:
227 case PIPE_FORMAT_G8R8_B8R8_UNORM:
228 case PIPE_FORMAT_R8B8_R8G8_UNORM:
229 case PIPE_FORMAT_B8R8_G8R8_UNORM:
230 case PIPE_FORMAT_R8_G8B8_422_UNORM:
231 case PIPE_FORMAT_R8_B8G8_422_UNORM:
232 return MALI_CLUMP_FORMAT_Y8_UV8_422;
233 case PIPE_FORMAT_R8_G8B8_420_UNORM:
234 case PIPE_FORMAT_R8_B8G8_420_UNORM:
235 case PIPE_FORMAT_R8_G8_B8_420_UNORM:
236 case PIPE_FORMAT_R8_B8_G8_420_UNORM:
237 case PIPE_FORMAT_R8G8B8_420_UNORM_PACKED:
238 return MALI_CLUMP_FORMAT_Y8_UV8_420;
239 case PIPE_FORMAT_R10_G10B10_420_UNORM:
240 case PIPE_FORMAT_R10G10B10_420_UNORM_PACKED:
241 return MALI_CLUMP_FORMAT_Y10_UV10_420;
242 case PIPE_FORMAT_R10_G10B10_422_UNORM:
243 case PIPE_FORMAT_X6R10X6G10_X6R10X6B10_422_UNORM:
244 return MALI_CLUMP_FORMAT_Y10_UV10_422;
245 default:
246 UNREACHABLE("unhandled clump format");
247 }
248 }
249
250 /* Select the appropriate raw format. */
251 switch (util_format_get_blocksize(format)) {
252 case 1:
253 return MALI_CLUMP_FORMAT_RAW8;
254 case 2:
255 return MALI_CLUMP_FORMAT_RAW16;
256 case 3:
257 return MALI_CLUMP_FORMAT_RAW24;
258 case 4:
259 return MALI_CLUMP_FORMAT_RAW32;
260 case 6:
261 return MALI_CLUMP_FORMAT_RAW48;
262 case 8:
263 return MALI_CLUMP_FORMAT_RAW64;
264 case 12:
265 return MALI_CLUMP_FORMAT_RAW96;
266 case 16:
267 return MALI_CLUMP_FORMAT_RAW128;
268 default:
269 UNREACHABLE("Invalid bpp");
270 }
271 }
272
273 static enum mali_clump_ordering
274 modifier_clump_ordering(uint64_t modifier)
275 {
276 switch (modifier) {
277 case DRM_FORMAT_MOD_ARM_16X16_BLOCK_U_INTERLEAVED:
278 return MALI_CLUMP_ORDERING_TILED_U_INTERLEAVED;
279 #if PAN_ARCH >= 10
280 case DRM_FORMAT_MOD_ARM_INTERLEAVED_64K:
281 return MALI_CLUMP_ORDERING_INTERLEAVED_64K;
282 #endif
283 case DRM_FORMAT_MOD_LINEAR:
284 return MALI_CLUMP_ORDERING_LINEAR;
285 default:
286 UNREACHABLE("");
287 }
288 }
289
290 static enum mali_afbc_superblock_size
291 translate_superblock_size(uint64_t modifier)
292 {
293 assert(drm_is_afbc(modifier));
294
295 switch (modifier & AFBC_FORMAT_MOD_BLOCK_SIZE_MASK) {
296 case AFBC_FORMAT_MOD_BLOCK_SIZE_16x16:
297 return MALI_AFBC_SUPERBLOCK_SIZE_16X16;
298 case AFBC_FORMAT_MOD_BLOCK_SIZE_32x8:
299 return MALI_AFBC_SUPERBLOCK_SIZE_32X8;
300 case AFBC_FORMAT_MOD_BLOCK_SIZE_64x4:
301 return MALI_AFBC_SUPERBLOCK_SIZE_64X4;
302 default:
303 UNREACHABLE("Invalid superblock size");
304 }
305 }
306
307 #if PAN_ARCH >= 10
308 #define PLANE_SET_EXTENT(__cfg, w, h) \
309 do { \
310 (__cfg).width = w; \
311 (__cfg).height = h; \
312 } while (0)
313 #else
314 #define PLANE_SET_EXTENT(cfg, w, h) \
315 do { \
316 } while (0)
317 #endif
318
319 #if PAN_ARCH > 10
320 #define PLANE_SET_SIZE(cfg__, size__) \
321 do { \
322 (cfg__).size = size__ & BITFIELD_MASK(32); \
323 (cfg__).size_hi = size__ >> 32; \
324 } while (0)
325 #define PLANE_SET_SLICE_STRIDE(cfg__, size__) \
326 do { \
327 (cfg__).slice_stride = size__ & BITFIELD_MASK(32); \
328 (cfg__).slice_stride_hi = size__ >> 32; \
329 } while (0)
330 #elif PAN_ARCH >= 9
331 #define PLANE_SET_SIZE(cfg__, size__) (cfg__).size = size__
332 #define PLANE_SET_SLICE_STRIDE(cfg__, size__) \
333 (cfg__).slice_stride = size__
334 #endif
335
336 static void
337 get_linear_or_u_tiled_plane_props(const struct pan_image_view *iview,
338 int plane_idx, unsigned mip_level,
339 unsigned layer_or_z_slice, uint64_t *pointer,
340 uint32_t *row_stride, uint64_t *slice_stride,
341 uint64_t *size)
342 {
343 const struct util_format_description *desc =
344 util_format_description(iview->format);
345 const struct pan_image_plane_ref pref =
346 util_format_has_stencil(desc)
347 ? pan_image_view_get_s_plane(iview)
348 : pan_image_view_get_plane(iview, plane_idx);
349
350 assert(pref.image != NULL);
351
352 const struct pan_image_plane *plane = pref.image->planes[pref.plane_idx];
353 const struct pan_image_slice_layout *slayout =
354 &plane->layout.slices[mip_level];
355
356 *pointer = plane->base + slayout->offset_B;
357 *size = slayout->size_B;
358 *row_stride = slayout->tiled_or_linear.row_stride_B;
359
360 if (pref.image->props.dim == MALI_TEXTURE_DIMENSION_3D) {
361 *pointer += layer_or_z_slice * slayout->tiled_or_linear.surface_stride_B;
362 *size -= layer_or_z_slice * slayout->tiled_or_linear.surface_stride_B;
363 *slice_stride = slayout->tiled_or_linear.surface_stride_B;
364 } else {
365 *pointer += layer_or_z_slice * plane->layout.array_stride_B;
366 *slice_stride = pref.image->props.nr_samples > 1
367 ? slayout->tiled_or_linear.surface_stride_B
368 : 0;
369 }
370 }
371
372 static void
373 emit_generic_plane(const struct pan_image_view *iview, int plane_idx,
374 unsigned mip_level, unsigned layer_or_z_slice, void *payload)
375 {
376 const struct util_format_description *desc =
377 util_format_description(iview->format);
378 const struct pan_image_plane_ref pref =
379 util_format_has_stencil(desc)
380 ? pan_image_view_get_s_plane(iview)
381 : pan_image_view_get_plane(iview, plane_idx);
382 const struct pan_image_props *props = &pref.image->props;
383 uint64_t plane_addr, plane_size, slice_stride;
384 uint32_t row_stride;
385
386 /* 3-planar formats must use Chroma 2p planes for the U V planes. */
387 assert(plane_idx == 0 || desc->layout != UTIL_FORMAT_LAYOUT_PLANAR3);
388 assert(props->modifier == DRM_FORMAT_MOD_LINEAR ||
389 props->modifier == DRM_FORMAT_MOD_ARM_INTERLEAVED_64K ||
390 props->modifier == DRM_FORMAT_MOD_ARM_16X16_BLOCK_U_INTERLEAVED);
391
392 get_linear_or_u_tiled_plane_props(iview, plane_idx, mip_level,
393 layer_or_z_slice, &plane_addr, &row_stride,
394 &slice_stride, &plane_size);
395
396 pan_cast_and_pack(payload, GENERIC_PLANE, cfg) {
397 cfg.clump_ordering = modifier_clump_ordering(props->modifier);
398 cfg.clump_format = pan_clump_format(iview->format);
399 PLANE_SET_SIZE(cfg, plane_size);
400 cfg.pointer = plane_addr;
401 cfg.row_stride = row_stride;
402 PLANE_SET_SLICE_STRIDE(cfg, slice_stride);
403 PLANE_SET_EXTENT(cfg, u_minify(props->extent_px.width, mip_level),
404 u_minify(props->extent_px.height, mip_level));
405 }
406 }
407
408 static void
409 emit_astc_plane(const struct pan_image_view *iview, int plane_idx,
410 unsigned mip_level, unsigned layer_or_z_slice, void *payload)
411 {
412 const struct util_format_description *desc =
413 util_format_description(iview->format);
414 const struct pan_image_plane_ref pref =
415 pan_image_view_get_plane(iview, plane_idx);
416 const struct pan_image_props *props = &pref.image->props;
417 bool srgb = (desc->colorspace == UTIL_FORMAT_COLORSPACE_SRGB);
418 uint64_t plane_addr, plane_size, slice_stride;
419 uint32_t row_stride;
420
421 /* sRGB formats decode to RGBA8 sRGB, which is narrow.
422 *
423 * Non-sRGB formats decode to RGBA16F which is wide except if decode
424 * precision is set to GL_RGBA8 for that texture.
425 */
426 bool wide = !srgb && !iview->astc.narrow;
427
428 assert(desc->layout == UTIL_FORMAT_LAYOUT_ASTC && desc->block.depth == 1);
429 assert(props->modifier == DRM_FORMAT_MOD_LINEAR ||
430 props->modifier == DRM_FORMAT_MOD_ARM_INTERLEAVED_64K ||
431 props->modifier == DRM_FORMAT_MOD_ARM_16X16_BLOCK_U_INTERLEAVED);
432
433 get_linear_or_u_tiled_plane_props(iview, plane_idx, mip_level,
434 layer_or_z_slice, &plane_addr, &row_stride,
435 &slice_stride, &plane_size);
436
437 #define ASTC_PLANE_SET_COMMON_PROPS() \
438 cfg.clump_ordering = modifier_clump_ordering(props->modifier); \
439 cfg.decode_hdr = iview->astc.hdr; \
440 cfg.decode_wide = wide; \
441 PLANE_SET_SIZE(cfg, plane_size); \
442 cfg.pointer = plane_addr; \
443 cfg.row_stride = row_stride; \
444 PLANE_SET_SLICE_STRIDE(cfg, slice_stride); \
445 PLANE_SET_EXTENT(cfg, u_minify(props->extent_px.width, mip_level), \
446 u_minify(props->extent_px.height, mip_level))
447
448 if (desc->block.depth > 1) {
449 pan_cast_and_pack(payload, ASTC_3D_PLANE, cfg) {
450 ASTC_PLANE_SET_COMMON_PROPS();
451 cfg.block_width = pan_astc_dim_3d(desc->block.width);
452 cfg.block_height = pan_astc_dim_3d(desc->block.height);
453 cfg.block_depth = pan_astc_dim_3d(desc->block.depth);
454 }
455 } else {
456 pan_cast_and_pack(payload, ASTC_2D_PLANE, cfg) {
457 ASTC_PLANE_SET_COMMON_PROPS();
458 cfg.block_width = pan_astc_dim_2d(desc->block.width);
459 cfg.block_height = pan_astc_dim_2d(desc->block.height);
460 }
461 }
462
463 #undef ASTC_PLANE_SET_COMMON_PROPS
464 }
465
466 static void
467 emit_linear_or_u_tiled_plane(const struct pan_image_view *iview,
468 int plane_index, unsigned mip_level,
469 unsigned layer_or_z_slice, void *payload)
470 {
471 const struct util_format_description *desc =
472 util_format_description(iview->format);
473
474 if (desc->layout == UTIL_FORMAT_LAYOUT_ASTC) {
475 emit_astc_plane(iview, plane_index, mip_level, layer_or_z_slice, payload);
476 } else {
477 emit_generic_plane(iview, plane_index, mip_level, layer_or_z_slice,
478 payload);
479 }
480 }
481
482 #define emit_linear_plane emit_linear_or_u_tiled_plane
483 #define emit_u_tiled_plane emit_linear_or_u_tiled_plane
484
485 static void
486 emit_linear_or_u_tiled_chroma_2p_plane(const struct pan_image_view *iview,
487 unsigned mip_level,
488 unsigned layer_or_z_slice, void *payload)
489 {
490 ASSERTED const struct util_format_description *desc =
491 util_format_description(iview->format);
492 const struct pan_image_plane_ref pref1 = pan_image_view_get_plane(iview, 1);
493 const struct pan_image_props *props = &pref1.image->props;
494 uint64_t cplane1_addr, cplane2_addr, cplane1_slice_stride, cplane1_size;
495 ASSERTED uint64_t cplane2_slice_stride, cplane2_size;
496 ASSERTED uint32_t cplane2_row_stride;
497 uint32_t cplane1_row_stride;
498
499 get_linear_or_u_tiled_plane_props(iview, 1, mip_level,
500 layer_or_z_slice, &cplane1_addr, &cplane1_row_stride,
501 &cplane1_slice_stride, &cplane1_size);
502 get_linear_or_u_tiled_plane_props(iview, 2, mip_level,
503 layer_or_z_slice, &cplane2_addr, &cplane2_row_stride,
504 &cplane2_slice_stride, &cplane2_size);
505
506 assert(cplane1_size == cplane2_size &&
507 cplane1_row_stride == cplane2_row_stride &&
508 cplane1_slice_stride == cplane2_slice_stride);
509
510 assert(desc->layout == UTIL_FORMAT_LAYOUT_PLANAR3);
511 assert(props->modifier == DRM_FORMAT_MOD_LINEAR ||
512 props->modifier == DRM_FORMAT_MOD_ARM_INTERLEAVED_64K ||
513 props->modifier == DRM_FORMAT_MOD_ARM_16X16_BLOCK_U_INTERLEAVED);
514
515 pan_cast_and_pack(payload, CHROMA_2P_PLANE, cfg) {
516 cfg.clump_ordering = modifier_clump_ordering(props->modifier);
517 cfg.clump_format = pan_clump_format(iview->format);
518 PLANE_SET_SIZE(cfg, cplane1_size);
519 cfg.pointer = cplane1_addr;
520 cfg.row_stride = cplane1_row_stride;
521 PLANE_SET_EXTENT(cfg, u_minify(props->extent_px.width, mip_level),
522 u_minify(props->extent_px.height, mip_level));
523 cfg.secondary_pointer = cplane2_addr;
524 }
525 }
526
527 #define emit_linear_chroma_2p_plane emit_linear_or_u_tiled_chroma_2p_plane
528 #define emit_u_tiled_chroma_2p_plane emit_linear_or_u_tiled_chroma_2p_plane
529
530 static void
531 get_afbc_plane_props(const struct pan_image_view *iview, int plane_idx,
532 unsigned mip_level, unsigned layer_or_z_slice,
533 uint64_t *header_pointer, uint32_t *header_row_stride,
534 uint32_t *header_slice_size, uint64_t *header_slice_stride,
535 uint64_t *size)
536 {
537 const struct util_format_description *desc =
538 util_format_description(iview->format);
539 const struct pan_image_plane_ref pref =
540 util_format_has_stencil(desc)
541 ? pan_image_view_get_s_plane(iview)
542 : pan_image_view_get_plane(iview, plane_idx);
543
544 assert(pref.image != NULL);
545
546 const struct pan_image_plane *plane = pref.image->planes[pref.plane_idx];
547 const struct pan_image_slice_layout *slayout =
548 &plane->layout.slices[mip_level];
549
550 *header_pointer = plane->base + slayout->offset_B;
551 *header_row_stride = slayout->afbc.header.row_stride_B;
552 *header_slice_size = slayout->afbc.header.surface_size_B;
553 *header_slice_stride = 0;
554 *size = slayout->size_B;
555
556 if (iview->dim == MALI_TEXTURE_DIMENSION_3D) {
557 assert(pref.image->props.dim == MALI_TEXTURE_DIMENSION_3D);
558 assert(layer_or_z_slice == 0);
559
560 *header_slice_stride = slayout->afbc.surface_stride_B;
561 } else if (pref.image->props.dim == MALI_TEXTURE_DIMENSION_3D) {
562 assert(iview->dim == MALI_TEXTURE_DIMENSION_2D);
563 /* When viewing 3D image as 2D-array, each plane describes a single Z
564 * slice. The header pointer is moved to the right slice, and the size is
565 * set to a single slice. */
566 *header_pointer += layer_or_z_slice * slayout->afbc.surface_stride_B;
567 *header_slice_stride = slayout->afbc.surface_stride_B;
568 *size = slayout->afbc.surface_stride_B;
569 } else {
570 *header_pointer += layer_or_z_slice * plane->layout.array_stride_B;
571 }
572 }
573
574 static void
575 emit_afbc_plane(const struct pan_image_view *iview, int plane_idx,
576 unsigned mip_level, unsigned layer_or_z_slice, void *payload)
577 {
578 const struct util_format_description *desc =
579 util_format_description(iview->format);
580 const struct pan_image_plane_ref pref =
581 util_format_has_stencil(desc)
582 ? pan_image_view_get_s_plane(iview)
583 : pan_image_view_get_plane(iview, plane_idx);
584 const struct pan_image *image = pref.image;
585 const struct pan_image_props *props = &image->props;
586 uint64_t header_slice_stride, plane_size, header_addr;
587 uint32_t header_slice_size, header_row_stride;
588
589 get_afbc_plane_props(iview, plane_idx, mip_level, layer_or_z_slice,
590 &header_addr, &header_row_stride, &header_slice_size,
591 &header_slice_stride, &plane_size);
592
593 /* We can't do 3-planar formats with AFBC. */
594 assert(desc->layout != UTIL_FORMAT_LAYOUT_PLANAR3);
595 assert(drm_is_afbc(props->modifier));
596 assert(props->nr_samples == 1);
597
598 pan_cast_and_pack(payload, AFBC_PLANE, cfg) {
599 cfg.superblock_size = translate_superblock_size(props->modifier);
600 cfg.ytr = (props->modifier & AFBC_FORMAT_MOD_YTR);
601 cfg.split_block = (props->modifier & AFBC_FORMAT_MOD_SPLIT);
602 cfg.tiled_header = (props->modifier & AFBC_FORMAT_MOD_TILED);
603 cfg.prefetch = true;
604 cfg.compression_mode = pan_afbc_decompression_mode(
605 iview->format, image->planes[pref.plane_idx]->layout.afbc.mode);
606 PLANE_SET_SIZE(cfg, plane_size);
607 cfg.pointer = header_addr;
608 cfg.header_row_stride = header_row_stride;
609 cfg.header_slice_size = header_slice_size;
610 #if PAN_ARCH <= 10
611 cfg.header_slice_stride = header_slice_stride;
612 #else
613 cfg.header_slice_stride = header_slice_stride & BITFIELD_MASK(32);
614 cfg.header_slice_stride_hi = header_slice_stride >> 32;
615 #endif
616 PLANE_SET_EXTENT(cfg, u_minify(props->extent_px.width, mip_level),
617 u_minify(props->extent_px.height, mip_level));
618 }
619 }
620
621 static void
622 emit_afbc_chroma_2p_plane(const struct pan_image_view *iview,
623 unsigned mip_level, unsigned layer_or_z_slice,
624 void *payload)
625 {
626 UNREACHABLE("AFBC chroma 2p plane not supported");
627 }
628
629 #if PAN_ARCH >= 10
630 static void
631 get_afrc_plane_props(const struct pan_image_view *iview, int plane_idx,
632 unsigned mip_level, unsigned layer_or_z_slice,
633 uint64_t *pointer, uint32_t *row_stride,
634 uint32_t *slice_stride, uint32_t *size)
635 {
636 const struct util_format_description *desc =
637 util_format_description(iview->format);
638 const struct pan_image_plane_ref pref =
639 util_format_has_stencil(desc)
640 ? pan_image_view_get_s_plane(iview)
641 : pan_image_view_get_plane(iview, plane_idx);
642
643 assert(pref.image != NULL);
644
645 const struct pan_image_plane *plane = pref.image->planes[pref.plane_idx];
646 const struct pan_image_slice_layout *slayout =
647 &plane->layout.slices[mip_level];
648
649 *pointer = plane->base + slayout->offset_B;
650 *size = slayout->size_B;
651 *row_stride = slayout->tiled_or_linear.row_stride_B;
652 *slice_stride = slayout->tiled_or_linear.surface_stride_B;
653
654 if (pref.image->props.dim == MALI_TEXTURE_DIMENSION_3D) {
655 *pointer += layer_or_z_slice * slayout->tiled_or_linear.surface_stride_B;
656 *size -= layer_or_z_slice * slayout->tiled_or_linear.surface_stride_B;
657 } else {
658 *pointer += layer_or_z_slice * plane->layout.array_stride_B;
659 }
660 }
661
662 static void
663 emit_afrc_plane(const struct pan_image_view *iview, int plane_index,
664 unsigned mip_level, unsigned layer_or_z_slice, void *payload)
665 {
666 const struct pan_afrc_format_info finfo =
667 pan_afrc_get_format_info(iview->format);
668 const struct pan_image_plane_ref pref =
669 pan_image_view_get_plane(iview, plane_index);
670 const struct pan_image *image = pref.image;
671 const struct pan_image_props *props = &image->props;
672 uint32_t plane_size, plane_row_stride, plane_slice_stride;
673 uint64_t plane_addr;
674
675 get_afrc_plane_props(iview, plane_index, mip_level, layer_or_z_slice,
676 &plane_addr, &plane_row_stride, &plane_slice_stride,
677 &plane_size);
678
679 assert(drm_is_afrc(props->modifier));
680
681 pan_cast_and_pack(payload, AFRC_PLANE, cfg) {
682 cfg.block_size = pan_afrc_block_size(props->modifier, plane_index);
683 cfg.format = pan_afrc_format(finfo, props->modifier, plane_index);
684 cfg.size = plane_size;
685 cfg.pointer = plane_addr;
686 cfg.row_stride = plane_row_stride;
687 cfg.slice_stride = plane_slice_stride;
688 PLANE_SET_EXTENT(cfg, u_minify(props->extent_px.width, mip_level),
689 u_minify(props->extent_px.height, mip_level));
690 }
691 }
692
693 static void
694 emit_afrc_chroma_2p_plane(const struct pan_image_view *iview,
695 unsigned mip_level, unsigned layer_or_z_slice,
696 void *payload)
697 {
698 const struct pan_afrc_format_info finfo =
699 pan_afrc_get_format_info(iview->format);
700 ASSERTED const struct util_format_description *desc =
701 util_format_description(iview->format);
702 const struct pan_image_plane_ref pref1 =
703 pan_image_view_get_plane(iview, 1);
704 ASSERTED const struct pan_image_plane_ref pref2 =
705 pan_image_view_get_plane(iview, 2);
706
707 assert(pref1.image != NULL && pref2.image != NULL);
708
709 const struct pan_image_props *props = &pref1.image->props;
710 uint32_t cplane1_row_stride, cplane1_slice_stride, cplane1_size;
711 ASSERTED uint32_t cplane2_row_stride, cplane2_slice_stride, cplane2_size;
712 uint64_t cplane1_addr, cplane2_addr;
713
714 get_afrc_plane_props(iview, 1, mip_level, layer_or_z_slice, &cplane1_addr,
715 &cplane1_row_stride, &cplane1_slice_stride,
716 &cplane1_size);
717 get_afrc_plane_props(iview, 2, mip_level, layer_or_z_slice, &cplane2_addr,
718 &cplane2_row_stride, &cplane2_slice_stride,
719 &cplane2_size);
720
721 assert(cplane1_size == cplane2_size &&
722 cplane1_slice_stride == cplane2_slice_stride &&
723 cplane1_row_stride == cplane2_row_stride);
724
725 assert(desc->layout == UTIL_FORMAT_LAYOUT_PLANAR3);
726 assert(props->modifier == DRM_FORMAT_MOD_LINEAR ||
727 props->modifier == DRM_FORMAT_MOD_ARM_16X16_BLOCK_U_INTERLEAVED);
728
729 pan_cast_and_pack(payload, AFRC_CHROMA_2P_PLANE, cfg) {
730 cfg.block_size = pan_afrc_block_size(props->modifier, 1);
731 cfg.format = pan_afrc_format(finfo, props->modifier, 1);
732 cfg.size = cplane1_size;
733 cfg.pointer = cplane1_addr;
734 cfg.row_stride = cplane1_row_stride;
735 PLANE_SET_EXTENT(cfg, u_minify(props->extent_px.width, mip_level),
736 u_minify(props->extent_px.height, mip_level));
737 cfg.secondary_pointer = cplane2_addr;
738 }
739 }
740
741 #define emit_interleaved_64k_plane emit_linear_or_u_tiled_plane
742
743 static void
744 emit_interleaved_64k_chroma_2p_plane(const struct pan_image_view *iview,
745 unsigned mip_level,
746 unsigned layer_or_z_slice, void *payload)
747 {
748 UNREACHABLE("not implemented");
749 }
750 #endif
751
752 #else
753 static void
754 get_linear_or_u_tiled_surface_props(const struct pan_image_view *iview,
755 unsigned plane_idx, unsigned mip_level,
756 unsigned layer_or_z_slice, unsigned sample,
757 uint64_t *pointer, uint32_t *row_stride,
758 uint32_t *surf_stride)
759 {
760 const struct util_format_description *desc =
761 util_format_description(iview->format);
762 const struct pan_image_plane_ref pref =
763 util_format_has_stencil(desc)
764 ? pan_image_view_get_s_plane(iview)
765 : pan_image_view_get_plane(iview, plane_idx);
766
767 assert(pref.image != NULL);
768
769 const struct pan_image_plane *plane = pref.image->planes[pref.plane_idx];
770 const struct pan_image_slice_layout *slayout =
771 &plane->layout.slices[mip_level];
772 uint64_t plane_addr = plane->base + slayout->offset_B;
773 unsigned tag = 0;
774
775 #if PAN_ARCH >= 5
776 if (desc->layout == UTIL_FORMAT_LAYOUT_ASTC)
777 tag = astc_compression_tag(desc);
778 #endif
779
780 if (pref.image->props.dim == MALI_TEXTURE_DIMENSION_3D) {
781 plane_addr +=
782 layer_or_z_slice * slayout->tiled_or_linear.surface_stride_B;
783 } else {
784 plane_addr += (layer_or_z_slice * plane->layout.array_stride_B) +
785 (sample * slayout->tiled_or_linear.surface_stride_B);
786 }
787
788 *pointer = plane_addr | tag;
789 *row_stride = slayout->tiled_or_linear.row_stride_B;
790 *surf_stride = slayout->tiled_or_linear.surface_stride_B;
791 }
792
793 static void
794 get_afbc_surface_props(const struct pan_image_view *iview,
795 unsigned plane_idx, unsigned mip_level,
796 unsigned layer_or_z_slice, unsigned sample,
797 uint64_t *pointer, uint32_t *row_stride,
798 uint32_t *surf_stride)
799 {
800 const struct util_format_description *desc =
801 util_format_description(iview->format);
802 const struct pan_image_plane_ref pref =
803 util_format_has_stencil(desc)
804 ? pan_image_view_get_s_plane(iview)
805 : pan_image_view_get_plane(iview, plane_idx);
806
807 assert(pref.image != NULL);
808
809 const struct pan_image_plane *plane = pref.image->planes[pref.plane_idx];
810 const struct pan_image_slice_layout *slayout =
811 &plane->layout.slices[mip_level];
812 uint64_t stride_B = pref.image->props.dim == MALI_TEXTURE_DIMENSION_3D
813 ? slayout->afbc.surface_stride_B
814 : plane->layout.array_stride_B;
815 uint64_t plane_header_addr =
816 plane->base + slayout->offset_B + (stride_B * layer_or_z_slice);
817 unsigned tag = 0;
818
819 #if PAN_ARCH >= 5
820 tag =
821 afbc_compression_tag(pref.image->props.dim, pref.image->props.modifier);
822 #endif
823
824 assert(sample == 0);
825
826 /* On 2D views, surface stride is used to do a bound check, so we can't set
827 * it to zero.
828 */
829 *surf_stride = slayout->afbc.surface_stride_B;
830 *pointer = plane_header_addr | tag;
831 *row_stride = slayout->afbc.header.row_stride_B;
832 }
833
834 static void
835 emit_linear_or_u_tiled_surface(const struct pan_image_view *iview,
836 unsigned mip_level, unsigned layer_or_z_slice,
837 unsigned sample, void *payload)
838 {
839 uint32_t row_stride, surf_stride;
840 uint64_t pointer;
841
842 get_linear_or_u_tiled_surface_props(iview, 0, mip_level, layer_or_z_slice,
843 sample, &pointer, &row_stride,
844 &surf_stride);
845
846 pan_cast_and_pack(payload, SURFACE_WITH_STRIDE, cfg) {
847 cfg.pointer = pointer;
848 cfg.row_stride = row_stride;
849 cfg.surface_stride = surf_stride;
850 }
851 }
852
853 #define emit_linear_surface emit_linear_or_u_tiled_surface
854 #define emit_u_tiled_surface emit_linear_or_u_tiled_surface
855
856 static void
857 emit_afbc_surface(const struct pan_image_view *iview, unsigned mip_level,
858 unsigned layer_or_z_slice, unsigned sample, void *payload)
859 {
860 uint32_t row_stride, surf_stride;
861 uint64_t pointer;
862
863 get_afbc_surface_props(iview, 0, mip_level, layer_or_z_slice, sample,
864 &pointer, &row_stride, &surf_stride);
865
866 pan_cast_and_pack(payload, SURFACE_WITH_STRIDE, cfg) {
867 cfg.pointer = pointer;
868 cfg.row_stride = row_stride;
869 cfg.surface_stride = surf_stride;
870 }
871 }
872
873 #if PAN_ARCH >= 7
874 static void
875 emit_linear_or_u_tiled_multiplane_surface(const struct pan_image_view *iview,
876 unsigned mip_level,
877 unsigned layer_or_z_slice,
878 unsigned sample, void *payload)
879 {
880 uint64_t yplane_addr, cplane1_addr, cplane2_addr = 0;
881 uint32_t yplane_row_stride, cplane1_row_stride, cplane2_row_stride = 0;
882 uint32_t yplane_surf_stride, cplane1_surf_stride, cplane2_surf_stride = 0;
883 unsigned nplanes = util_format_get_num_planes(iview->format);
884
885 assert(nplanes == 2 || nplanes == 3);
886
887 get_linear_or_u_tiled_surface_props(iview, 0, mip_level, layer_or_z_slice,
888 sample, &yplane_addr, &yplane_row_stride,
889 &yplane_surf_stride);
890 get_linear_or_u_tiled_surface_props(iview, 1, mip_level, layer_or_z_slice,
891 sample, &cplane1_addr, &cplane1_row_stride,
892 &cplane1_surf_stride);
893 if (nplanes == 3) {
894 get_linear_or_u_tiled_surface_props(
895 iview, 2, mip_level, layer_or_z_slice, sample, &cplane2_addr,
896 &cplane2_row_stride, &cplane2_surf_stride);
897 assert(cplane2_row_stride == cplane1_row_stride);
898 }
899
900 pan_cast_and_pack(payload, MULTIPLANAR_SURFACE, cfg) {
901 cfg.plane_0_pointer = yplane_addr;
902 cfg.plane_0_row_stride = yplane_row_stride;
903 cfg.plane_1_2_row_stride = cplane1_row_stride;
904 cfg.plane_1_pointer = cplane1_addr;
905 cfg.plane_2_pointer = cplane2_addr;
906 }
907 }
908
909 #define emit_linear_multiplane_surface emit_linear_or_u_tiled_multiplane_surface
910 #define emit_u_tiled_multiplane_surface \
911 emit_linear_or_u_tiled_multiplane_surface
912
913 static void
914 emit_afbc_multiplane_surface(const struct pan_image_view *iview,
915 unsigned mip_level, unsigned layer_or_z_slice,
916 unsigned sample, void *payload)
917 {
918 uint64_t yplane_addr, cplane1_addr, cplane2_addr = 0;
919 uint32_t yplane_row_stride, cplane1_row_stride, cplane2_row_stride = 0;
920 uint32_t yplane_surf_stride, cplane1_surf_stride, cplane2_surf_stride = 0;
921 unsigned nplanes = util_format_get_num_planes(iview->format);
922
923 assert(nplanes == 2 || nplanes == 3);
924
925 get_afbc_surface_props(iview, 0, mip_level, layer_or_z_slice, sample,
926 &yplane_addr, &yplane_row_stride,
927 &yplane_surf_stride);
928 get_afbc_surface_props(iview, 1, mip_level, layer_or_z_slice, sample,
929 &cplane1_addr, &cplane1_row_stride,
930 &cplane1_surf_stride);
931 if (nplanes == 3) {
932 get_afbc_surface_props(iview, 2, mip_level, layer_or_z_slice, sample,
933 &cplane2_addr, &cplane2_row_stride,
934 &cplane2_surf_stride);
935 assert(cplane2_row_stride == cplane1_row_stride);
936 }
937
938 pan_cast_and_pack(payload, MULTIPLANAR_SURFACE, cfg) {
939 cfg.plane_0_pointer = yplane_addr;
940 cfg.plane_0_row_stride = yplane_row_stride;
941 cfg.plane_1_2_row_stride = cplane1_row_stride;
942 cfg.plane_1_pointer = cplane1_addr;
943 cfg.plane_2_pointer = cplane2_addr;
944 }
945 }
946 #endif
947
948 #endif
949
950 #if PAN_ARCH >= 9
951 #define PAN_TEX_EMIT_HELPER(mod) \
952 void GENX(pan_tex_emit_##mod##_payload_entry)( \
953 const struct pan_image_view *iview, unsigned mip_level, \
954 unsigned layer_or_z_slice, unsigned sample, void **payload) \
955 { \
956 assert(sample == 0); \
957 const unsigned nplanes = util_format_get_num_planes(iview->format); \
958 \
959 emit_##mod##_plane(iview, 0, mip_level, layer_or_z_slice, *payload); \
960 \
961 /* We use NULL_PLANE here, but we could use any other kind of \
962 * descriptor, since they are all the same size. */ \
963 *payload += pan_size(NULL_PLANE); \
964 \
965 if (nplanes == 2) { \
966 emit_##mod##_plane(iview, 1, mip_level, layer_or_z_slice, *payload); \
967 *payload += pan_size(NULL_PLANE); \
968 } else if (nplanes == 3) { \
969 emit_##mod##_chroma_2p_plane(iview, mip_level, layer_or_z_slice, \
970 *payload); \
971 *payload += pan_size(NULL_PLANE); \
972 } \
973 }
974 #elif PAN_ARCH >= 7
975 #define PAN_TEX_EMIT_HELPER(mod) \
976 void GENX(pan_tex_emit_##mod##_payload_entry)( \
977 const struct pan_image_view *iview, unsigned mip_level, \
978 unsigned layer_or_z_slice, unsigned sample, void **payload) \
979 { \
980 if (util_format_get_num_planes(iview->format) == 1) { \
981 emit_##mod##_surface(iview, mip_level, layer_or_z_slice, sample, \
982 *payload); \
983 *payload += pan_size(SURFACE_WITH_STRIDE); \
984 } else { \
985 emit_##mod##_multiplane_surface(iview, mip_level, layer_or_z_slice, \
986 sample, *payload); \
987 *payload += pan_size(MULTIPLANAR_SURFACE); \
988 } \
989 }
990 #else
991 #define PAN_TEX_EMIT_HELPER(mod) \
992 void GENX(pan_tex_emit_##mod##_payload_entry)( \
993 const struct pan_image_view *iview, unsigned mip_level, \
994 unsigned layer_or_z_slice, unsigned sample, void **payload) \
995 { \
996 assert(util_format_get_num_planes(iview->format) == 1); \
997 emit_##mod##_surface(iview, mip_level, layer_or_z_slice, sample, \
998 *payload); \
999 *payload += pan_size(SURFACE_WITH_STRIDE); \
1000 }
1001 #endif
1002
1003 PAN_TEX_EMIT_HELPER(linear)
1004 PAN_TEX_EMIT_HELPER(u_tiled)
1005 PAN_TEX_EMIT_HELPER(afbc)
1006 #if PAN_ARCH >= 10
1007 PAN_TEX_EMIT_HELPER(interleaved_64k)
1008 PAN_TEX_EMIT_HELPER(afrc)
1009 #endif
1010
1011 static void
1012 pan_emit_iview_texture_payload(const struct pan_image_view *iview,
1013 void *payload)
1014 {
1015 const struct pan_image_plane_ref pref =
1016 pan_image_view_get_first_plane(iview);
1017 const struct pan_mod_handler *mod_handler = pref.image->mod_handler;
1018 unsigned nr_samples =
1019 PAN_ARCH < 9 ? pan_image_view_get_nr_samples(iview) : 1;
1020
1021 /* Inject the addresses in, interleaving array indices, mip levels,
1022 * cube faces, and strides in that order. On Bifrost and older, each
1023 * sample had its own surface descriptor; on Valhall, they are fused
1024 * into a single plane descriptor.
1025 */
1026
1027 #if PAN_ARCH >= 7
1028 /* V7 and later treats faces as extra layers */
1029 for (int layer = iview->first_layer; layer <= iview->last_layer; ++layer) {
1030 for (int sample = 0; sample < nr_samples; ++sample) {
1031 for (int level = iview->first_level; level <= iview->last_level;
1032 ++level) {
1033 mod_handler->emit_tex_payload_entry(iview, level, layer, sample,
1034 &payload);
1035 }
1036 }
1037 }
1038 #else
1039 unsigned first_layer = iview->first_layer, last_layer = iview->last_layer;
1040 unsigned face_count = 1;
1041
1042 if (iview->dim == MALI_TEXTURE_DIMENSION_CUBE) {
1043 first_layer /= 6;
1044 last_layer /= 6;
1045 face_count = 6;
1046 }
1047
1048 /* V6 and earlier has a different memory-layout */
1049 for (int layer = first_layer; layer <= last_layer; ++layer) {
1050 for (int level = iview->first_level; level <= iview->last_level;
1051 ++level) {
1052 /* order of face and sample doesn't matter; we can only have multiple
1053 * of one or the other (no support for multisampled cubemaps)
1054 */
1055 for (int face = 0; face < face_count; ++face) {
1056 for (int sample = 0; sample < nr_samples; ++sample) {
1057 mod_handler->emit_tex_payload_entry(
1058 iview, level, (face_count * layer) + face, sample, &payload);
1059 }
1060 }
1061 }
1062 }
1063 #endif
1064 }
1065
1066 #if PAN_ARCH < 9
1067 /* Map modifiers to mali_texture_layout for packing in a texture descriptor */
1068
1069 static enum mali_texture_layout
1070 pan_modifier_to_layout(uint64_t modifier)
1071 {
1072 if (drm_is_afbc(modifier))
1073 return MALI_TEXTURE_LAYOUT_AFBC;
1074 else if (modifier == DRM_FORMAT_MOD_ARM_16X16_BLOCK_U_INTERLEAVED)
1075 return MALI_TEXTURE_LAYOUT_TILED;
1076 else if (modifier == DRM_FORMAT_MOD_LINEAR)
1077 return MALI_TEXTURE_LAYOUT_LINEAR;
1078 else
1079 UNREACHABLE("Invalid modifer");
1080 }
1081 #endif
1082
1083 #if PAN_ARCH >= 7
1084 void
1085 GENX(pan_texture_swizzle_replicate_x)(struct pan_image_view *iview)
1086 {
1087 /* v7+ doesn't have an _RRRR component order, combine the
1088 * user swizzle with a .XXXX swizzle to emulate that. */
1089 assert(util_format_is_depth_or_stencil(iview->format));
1090
1091 static const unsigned char replicate_x[4] = {
1092 PIPE_SWIZZLE_X,
1093 PIPE_SWIZZLE_X,
1094 PIPE_SWIZZLE_X,
1095 PIPE_SWIZZLE_X,
1096 };
1097
1098 util_format_compose_swizzles(replicate_x, iview->swizzle, iview->swizzle);
1099 }
1100 #endif
1101
1102 #if PAN_ARCH == 7
1103 void
1104 GENX(pan_texture_afbc_reswizzle)(struct pan_image_view *iview)
1105 {
1106 /* v7 (only) restricts component orders when AFBC is in use.
1107 * Rather than restrict AFBC for all non-canonical component orders, we use
1108 * an allowed component order with an invertible swizzle composed.
1109 * This allows us to support AFBC(BGR) as well as AFBC(RGB).
1110 */
1111 assert(!util_format_is_depth_or_stencil(iview->format));
1112 assert(!pan_format_is_yuv(iview->format));
1113 assert(pan_afbc_supports_format(PAN_ARCH, iview->format));
1114
1115 uint32_t mali_format = GENX(pan_format_from_pipe_format)(iview->format)->hw;
1116
1117 enum mali_rgb_component_order orig = mali_format & BITFIELD_MASK(12);
1118 struct pan_decomposed_swizzle decomposed = GENX(pan_decompose_swizzle)(orig);
1119
1120 /* Apply the new component order */
1121 if (orig != decomposed.pre)
1122 iview->format = util_format_rgb_to_bgr(iview->format);
1123 /* Only RGB<->BGR should be allowed for AFBC */
1124 assert(iview->format != PIPE_FORMAT_NONE);
1125 assert(decomposed.pre ==
1126 (GENX(pan_format_from_pipe_format)(iview->format)->hw &
1127 BITFIELD_MASK(12)));
1128
1129 /* Compose the new swizzle */
1130 util_format_compose_swizzles(decomposed.post, iview->swizzle,
1131 iview->swizzle);
1132 }
1133 #endif
1134
1135 static unsigned
1136 pan_texture_get_array_size(const struct pan_image_view *iview)
1137 {
1138 unsigned array_size = iview->last_layer - iview->first_layer + 1;
1139
1140 /* If this is a cubemap, we expect the number of layers to be a multiple
1141 * of 6.
1142 */
1143 if (iview->dim == MALI_TEXTURE_DIMENSION_CUBE) {
1144 assert(array_size % 6 == 0);
1145 array_size /= 6;
1146 }
1147
1148 /* Multiplanar YUV textures require 2 surface descriptors. */
1149 if (pan_format_is_yuv(iview->format) && PAN_ARCH >= 9 &&
1150 pan_image_view_get_plane(iview, 1).image != NULL)
1151 array_size *= 2;
1152
1153 return array_size;
1154 }
1155
1156 static struct pan_image_extent
1157 pan_texture_get_extent(const struct pan_image_view *iview,
1158 const struct pan_image_props *iprops)
1159 {
1160 struct pan_image_extent extent_px = {
1161 .width = u_minify(iprops->extent_px.width, iview->first_level),
1162 .height = u_minify(iprops->extent_px.height, iview->first_level),
1163 .depth = u_minify(iprops->extent_px.depth, iview->first_level),
1164 };
1165
1166 if (util_format_is_compressed(iprops->format) &&
1167 !util_format_is_compressed(iview->format)) {
1168 extent_px.width = DIV_ROUND_UP(
1169 extent_px.width, util_format_get_blockwidth(iprops->format));
1170 extent_px.height = DIV_ROUND_UP(
1171 extent_px.height, util_format_get_blockheight(iprops->format));
1172 extent_px.depth = DIV_ROUND_UP(
1173 extent_px.depth, util_format_get_blockdepth(iprops->format));
1174 assert(util_format_get_blockwidth(iview->format) == 1);
1175 assert(util_format_get_blockheight(iview->format) == 1);
1176 assert(util_format_get_blockheight(iview->format) == 1);
1177 assert(iview->last_level == iview->first_level);
1178 }
1179
1180 return extent_px;
1181 }
1182
1183 /*
1184 * Generates a texture descriptor. Ideally, descriptors are immutable after the
1185 * texture is created, so we can keep these hanging around in GPU memory in a
1186 * dedicated BO and not have to worry. In practice there are some minor gotchas
1187 * with this (the driver sometimes will change the format of a texture on the
1188 * fly for compression) but it's fast enough to just regenerate the descriptor
1189 * in those cases, rather than monkeypatching at drawtime. A texture descriptor
1190 * consists of a 32-byte header followed by pointers.
1191 */
1192 void
1193 GENX(pan_sampled_texture_emit)(const struct pan_image_view *iview,
1194 struct mali_texture_packed *out,
1195 const struct pan_ptr *payload)
1196 {
1197 pan_image_view_check(iview);
1198
1199 const struct util_format_description *desc =
1200 util_format_description(iview->format);
1201 const struct pan_image_plane_ref first_plane = pan_image_view_get_first_plane(iview);
1202 const struct pan_image_props *props = &first_plane.image->props;
1203 uint32_t mali_format = GENX(pan_format_from_pipe_format)(iview->format)->hw;
1204
1205 if (desc->layout == UTIL_FORMAT_LAYOUT_ASTC && iview->astc.narrow &&
1206 desc->colorspace != UTIL_FORMAT_COLORSPACE_SRGB) {
1207 mali_format = MALI_PACK_FMT(RGBA8_UNORM, RGBA, L);
1208 }
1209
1210 pan_emit_iview_texture_payload(iview, payload->cpu);
1211
1212 unsigned array_size = pan_texture_get_array_size(iview);
1213
1214 struct pan_image_extent extent_px =
1215 pan_texture_get_extent(iview, props);
1216
1217 pan_pack(out, TEXTURE, cfg) {
1218 cfg.dimension = iview->dim;
1219 cfg.format = mali_format;
1220 cfg.width = extent_px.width;
1221 cfg.height = extent_px.height;
1222 if (iview->dim == MALI_TEXTURE_DIMENSION_3D)
1223 cfg.depth = extent_px.depth;
1224 else
1225 cfg.sample_count = props->nr_samples;
1226 cfg.swizzle = pan_translate_swizzle_4(iview->swizzle);
1227 #if PAN_ARCH >= 9
1228 cfg.texel_interleave = (props->modifier != DRM_FORMAT_MOD_LINEAR) ||
1229 util_format_is_compressed(iview->format);
1230 #else
1231 cfg.texel_ordering = pan_modifier_to_layout(props->modifier);
1232 #endif
1233 cfg.levels = iview->last_level - iview->first_level + 1;
1234 cfg.array_size = array_size;
1235
1236 #if PAN_ARCH >= 6
1237 cfg.surfaces = payload->gpu;
1238
1239 cfg.minimum_lod = MAX2(0.0f, iview->min_lod - iview->first_level);
1240 cfg.maximum_lod = cfg.levels - 1;
1241 #endif
1242 }
1243 }
1244
1245 #if PAN_ARCH >= 9
1246 void
1247 GENX(pan_storage_texture_emit)(const struct pan_image_view *iview,
1248 struct mali_texture_packed *out,
1249 const struct pan_ptr *payload)
1250 {
1251 pan_image_view_check(iview);
1252
1253 const struct pan_image_plane_ref first_plane =
1254 pan_image_view_get_first_plane(iview);
1255 const struct pan_image_props *props = &first_plane.image->props;
1256
1257 /* AFBC and AFRC cannot be used in storage operations. */
1258 assert(!drm_is_afbc(props->modifier));
1259 assert(!drm_is_afrc(props->modifier));
1260
1261 /* Compressed formats can't be used with storage operations */
1262 assert(!util_format_is_compressed(iview->format));
1263
1264 uint32_t mali_format =
1265 GENX(pan_format_from_pipe_format)(iview->format)->hw;
1266
1267 pan_emit_iview_texture_payload(iview, payload->cpu);
1268
1269 unsigned array_size = pan_texture_get_array_size(iview);
1270
1271 struct pan_image_extent extent_px =
1272 pan_texture_get_extent(iview, props);
1273
1274 static const unsigned char rgba_swizzle[4] = {
1275 PIPE_SWIZZLE_X,
1276 PIPE_SWIZZLE_Y,
1277 PIPE_SWIZZLE_Z,
1278 PIPE_SWIZZLE_W,
1279 };
1280
1281 pan_pack(out, TEXTURE, cfg) {
1282 cfg.dimension = iview->dim;
1283 cfg.format = mali_format;
1284 cfg.width = extent_px.width;
1285 cfg.height = extent_px.height;
1286 if (iview->dim == MALI_TEXTURE_DIMENSION_3D)
1287 cfg.depth = extent_px.depth;
1288 else
1289 cfg.sample_count = props->nr_samples;
1290 cfg.texel_interleave = (props->modifier != DRM_FORMAT_MOD_LINEAR) ||
1291 util_format_is_compressed(iview->format);
1292 cfg.levels = iview->last_level - iview->first_level + 1;
1293 cfg.array_size = array_size;
1294
1295 cfg.surfaces = payload->gpu;
1296
1297 /* Requirements for storage image use. */
1298 cfg.minimum_lod = 0;
1299 cfg.maximum_lod = 0;
1300 cfg.minimum_level = 0;
1301 cfg.swizzle = pan_translate_swizzle_4(rgba_swizzle);
1302 }
1303 }
1304 #endif