Debian Codegraph @

src/panfrost/lib/pan_desc.c

raw
1 /*
2 * Copyright (C) 2021 Collabora, Ltd.
3 * SPDX-License-Identifier: MIT
4 */
5
6 #include "util/macros.h"
7
8 #include "genxml/gen_macros.h"
9
10 #include "pan_afbc.h"
11 #include "pan_afrc.h"
12 #include "pan_desc.h"
13 #include "pan_encoder.h"
14 #include "pan_props.h"
15 #include "pan_texture.h"
16 #include "pan_trace.h"
17 #include "pan_util.h"
18
19 #define PAN_BIN_LEVEL_COUNT 12
20
21 static enum mali_msaa
22 mali_sampling_mode(const struct pan_image_view *view)
23 {
24 unsigned nr_samples = pan_image_view_get_nr_samples(view);
25
26 if (nr_samples > 1) {
27 ASSERTED const struct pan_image_plane_ref pref =
28 pan_image_view_get_first_plane(view);
29
30 assert(view->nr_samples == pref.image->props.nr_samples);
31 return MALI_MSAA_LAYERED;
32 }
33
34 if (view->nr_samples > nr_samples) {
35 assert(nr_samples == 1);
36 return MALI_MSAA_AVERAGE;
37 }
38
39 assert(view->nr_samples == nr_samples);
40 assert(view->nr_samples == 1);
41
42 return MALI_MSAA_SINGLE;
43 }
44
45 static bool
46 pan_afbc_superblock_exceeds_tile_size(uint64_t drm_mod,
47 unsigned tile_size_px)
48 {
49 assert(drm_is_afbc(drm_mod));
50 assert(tile_size_px <= pan_max_effective_tile_size(PAN_ARCH));
51
52 struct pan_image_block_size tileblk_sz =
53 pan_effective_tile_block_size(tile_size_px);
54 struct pan_image_block_size superblk_sz =
55 pan_afbc_superblock_size(drm_mod);
56
57 return tileblk_sz.width < superblk_sz.width ||
58 tileblk_sz.height < superblk_sz.height;
59 }
60
61 /* There is an issue with AFBC that can occur when the tile size is not a
62 * multiple of the superblock size. If multiple tiles access the same AFBC
63 * superblock and also read from that superblock, then the read in one tile
64 * may race with the write at the end of the other tile, resulting in reading
65 * inconsistent AFBC data and a resulting rendering corruption.
66 *
67 * Starting with v7, there is a bit in the Z/S and color target descriptors
68 * to tell the tiler to be more careful about tile order and compression to
69 * ensure this doesn't happen.
70 *
71 * TODO: Right now, we always assume that the target may be read but there may
72 * be cases when the driver can prove otherwise. (Unfortunately, thanks to
73 * incremental rendering, those cases are pretty limited.)
74 */
75 static bool
76 pan_needs_afbc_reverse_issue_order(const struct pan_attachment_info *att,
77 const struct pan_image *image)
78 {
79 return pan_afbc_superblock_exceeds_tile_size(image->props.modifier,
80 att->fb_tile_size_px);
81 }
82
83 #if PAN_ARCH <= 6
84 static void
85 pan_warn_on_afbc_reverse_issue_order(const struct pan_attachment_info *att,
86 const struct pan_image *image)
87 {
88 if (pan_needs_afbc_reverse_issue_order(att, image)) {
89 mesa_logw_once("panfrost: Tile size is smaller than AFBC superblock."
90 " Rendering may not be correct");
91 }
92 }
93 #endif
94
95 static bool
96 renderblock_fits_in_single_pass(const struct pan_image_view *view,
97 unsigned tile_size)
98 {
99 const struct pan_image_plane_ref pref = pan_image_view_get_first_plane(view);
100 uint64_t mod = pref.image->props.modifier;
101
102 if (!drm_is_afbc(mod))
103 return tile_size >= 16 * 16;
104
105 struct pan_image_block_size renderblk_sz = pan_afbc_renderblock_size(mod);
106 return tile_size >= renderblk_sz.width * renderblk_sz.height;
107 }
108
109 int
110 GENX(pan_select_crc_rt)(const struct pan_fb_info *fb, unsigned tile_size)
111 {
112 /* Disable CRC when the tile size is smaller than 16x16. In the hardware,
113 * CRC tiles are the same size as the tiles of the framebuffer. However,
114 * our code only handles 16x16 tiles. Therefore under the current
115 * implementation, we must disable CRC when 16x16 tiles are not used.
116 *
117 * This may hurt performance. However, smaller tile sizes are rare, and
118 * CRCs are more expensive at smaller tile sizes, reducing the benefit.
119 * Restricting CRC to 16x16 should work in practice.
120 */
121 if (tile_size < 16 * 16)
122 return -1;
123
124 #if PAN_ARCH <= 6
125 if (fb->rt_count == 1 && fb->rts[0].view && !fb->rts[0].discard &&
126 pan_image_view_has_crc(fb->rts[0].view))
127 return 0;
128
129 return -1;
130 #else
131 bool best_rt_valid = false;
132 int best_rt = -1;
133
134 for (unsigned i = 0; i < fb->rt_count; i++) {
135 if (!fb->rts[i].view || fb->rts[i].discard ||
136 !pan_image_view_has_crc(fb->rts[i].view))
137 continue;
138
139 if (!renderblock_fits_in_single_pass(fb->rts[i].view, tile_size))
140 continue;
141
142 bool valid = *(fb->rts[i].crc_valid);
143 bool full = !fb->draw_extent.minx && !fb->draw_extent.miny &&
144 fb->draw_extent.maxx == (fb->width - 1) &&
145 fb->draw_extent.maxy == (fb->height - 1);
146 if (!full && !valid)
147 continue;
148
149 if (best_rt < 0 || (valid && !best_rt_valid)) {
150 best_rt = i;
151 best_rt_valid = valid;
152 }
153
154 if (valid)
155 break;
156 }
157
158 return best_rt;
159 #endif
160 }
161
162 static enum mali_zs_format
163 translate_zs_format(enum pipe_format in)
164 {
165 switch (in) {
166 case PIPE_FORMAT_Z16_UNORM:
167 return MALI_ZS_FORMAT_D16;
168 case PIPE_FORMAT_Z24_UNORM_S8_UINT:
169 return MALI_ZS_FORMAT_D24S8;
170 case PIPE_FORMAT_Z24X8_UNORM:
171 return MALI_ZS_FORMAT_D24X8;
172 case PIPE_FORMAT_Z24_UNORM_PACKED:
173 return MALI_ZS_FORMAT_D24;
174 case PIPE_FORMAT_Z32_FLOAT:
175 return MALI_ZS_FORMAT_D32;
176 #if PAN_ARCH < 9
177 case PIPE_FORMAT_Z32_FLOAT_S8X24_UINT:
178 return MALI_ZS_FORMAT_D32_S8X24;
179 #endif
180 default:
181 UNREACHABLE("Unsupported depth/stencil format.");
182 }
183 }
184
185 #if PAN_ARCH >= 5
186 static enum mali_s_format
187 translate_s_format(enum pipe_format in)
188 {
189 switch (in) {
190 case PIPE_FORMAT_S8_UINT:
191 return MALI_S_FORMAT_S8;
192 case PIPE_FORMAT_Z24_UNORM_S8_UINT:
193 case PIPE_FORMAT_X24S8_UINT:
194 return MALI_S_FORMAT_X24S8;
195
196 #if PAN_ARCH < 9
197 case PIPE_FORMAT_S8_UINT_Z24_UNORM:
198 case PIPE_FORMAT_S8X24_UINT:
199 return MALI_S_FORMAT_S8X24;
200 case PIPE_FORMAT_Z32_FLOAT_S8X24_UINT:
201 case PIPE_FORMAT_X32_S8X24_UINT:
202 return MALI_S_FORMAT_X32_S8X24;
203 #endif
204
205 default:
206 UNREACHABLE("Unsupported stencil format.");
207 }
208 }
209
210 static void
211 get_tiled_or_linear_att_mem_props(struct pan_image_plane_ref pref,
212 unsigned mip_level, unsigned layer_or_z_slice,
213 uint64_t *base, uint64_t *row_stride,
214 uint64_t *surf_stride)
215 {
216 const struct pan_image *image = pref.image;
217 const struct pan_image_plane *plane = image->planes[pref.plane_idx];
218 const struct pan_image_slice_layout *slayout =
219 &plane->layout.slices[mip_level];
220 const unsigned array_idx =
221 image->props.dim == MALI_TEXTURE_DIMENSION_3D ? 0 : layer_or_z_slice;
222 const unsigned surf_idx =
223 image->props.dim == MALI_TEXTURE_DIMENSION_3D ? layer_or_z_slice : 0;
224
225 *base = plane->base + slayout->offset_B +
226 (array_idx * plane->layout.array_stride_B) +
227 (surf_idx * slayout->tiled_or_linear.surface_stride_B);
228 *row_stride = slayout->tiled_or_linear.row_stride_B;
229 *surf_stride = slayout->tiled_or_linear.surface_stride_B;
230 }
231
232 static enum mali_block_format
233 get_afbc_block_format(uint64_t mod)
234 {
235 #if PAN_ARCH >= 7
236 if (mod & AFBC_FORMAT_MOD_TILED)
237 return MALI_BLOCK_FORMAT_AFBC_TILED;
238 #endif
239
240 assert(!(mod & AFBC_FORMAT_MOD_TILED));
241 return MALI_BLOCK_FORMAT_AFBC;
242 }
243
244 static void
245 get_afbc_att_mem_props(struct pan_image_plane_ref pref, unsigned mip_level,
246 unsigned layer_or_z_slice, uint64_t *header,
247 uint64_t *body_offset, uint64_t *row_stride)
248 {
249 const struct pan_image *image = pref.image;
250 const struct pan_image_plane *plane = image->planes[pref.plane_idx];
251 const struct pan_image_slice_layout *slayout =
252 &plane->layout.slices[mip_level];
253 const uint64_t stride_B = image->props.dim == MALI_TEXTURE_DIMENSION_3D
254 ? slayout->afbc.surface_stride_B
255 : plane->layout.array_stride_B;
256
257 *row_stride = slayout->afbc.header.row_stride_B;
258 *body_offset = pan_afbc_body_offset(PAN_ARCH, image->props.modifier,
259 slayout->afbc.header.surface_size_B);
260 *header = plane->base + slayout->offset_B + (stride_B * layer_or_z_slice);
261 }
262
263 #if PAN_ARCH <= 10
264 #define SET_SURFACE_STRIDE(cfg__, val__) (cfg__).surface_stride = val__
265 #else
266 #define SET_SURFACE_STRIDE(cfg__, val__) \
267 do { \
268 (cfg__).surface_stride = val__ & BITFIELD_MASK(32); \
269 (cfg__).surface_stride_hi = val__ >> 32; \
270 } while (0)
271 #endif
272
273 void
274 GENX(pan_emit_linear_s_attachment)(const struct pan_attachment_info *att,
275 void *payload)
276 {
277 const struct pan_image_view *s = att->iview;
278 uint64_t base, row_stride, surf_stride;
279
280 get_tiled_or_linear_att_mem_props(pan_image_view_get_s_plane(s),
281 s->first_level, att->layer_or_z_slice,
282 &base, &row_stride, &surf_stride);
283 pan_cast_and_pack(payload, S_TARGET, cfg) {
284 cfg.write_format = translate_s_format(s->format);
285 cfg.block_format = MALI_BLOCK_FORMAT_LINEAR;
286 cfg.base = base;
287 cfg.row_stride = row_stride;
288 SET_SURFACE_STRIDE(cfg, surf_stride);
289 }
290 }
291
292 void
293 GENX(pan_emit_afbc_s_attachment)(const struct pan_attachment_info *att,
294 void *payload)
295 {
296 assert(PAN_ARCH >= 9);
297
298 #if PAN_ARCH >= 9
299 const struct pan_image_view *s = att->iview;
300 const struct pan_image_plane_ref pref = pan_image_view_get_s_plane(s);
301 uint64_t header, body_offset, hdr_row_stride;
302
303 get_afbc_att_mem_props(pref, s->first_level, att->layer_or_z_slice,
304 &header, &body_offset, &hdr_row_stride);
305 pan_cast_and_pack(payload, AFBC_S_TARGET, cfg) {
306 cfg.write_format = translate_s_format(s->format);
307 cfg.block_format = get_afbc_block_format(pref.image->props.modifier);
308 cfg.reverse_issue_order =
309 pan_needs_afbc_reverse_issue_order(att, pref.image);
310 cfg.header = header;
311 cfg.body_offset = body_offset;
312 cfg.header_row_stride = hdr_row_stride;
313 }
314 #endif
315 }
316
317 void
318 GENX(pan_emit_u_tiled_s_attachment)(const struct pan_attachment_info *att,
319 void *payload)
320 {
321 const struct pan_image_view *s = att->iview;
322 uint64_t base, row_stride, surf_stride;
323
324 get_tiled_or_linear_att_mem_props(pan_image_view_get_s_plane(s),
325 s->first_level, att->layer_or_z_slice,
326 &base, &row_stride, &surf_stride);
327 pan_cast_and_pack(payload, S_TARGET, cfg) {
328 cfg.write_format = translate_s_format(s->format);
329 cfg.block_format = MALI_BLOCK_FORMAT_TILED_U_INTERLEAVED;
330 cfg.base = base;
331 cfg.row_stride = row_stride;
332 SET_SURFACE_STRIDE(cfg, surf_stride);
333 }
334 }
335
336 void
337 GENX(pan_emit_linear_zs_attachment)(const struct pan_attachment_info *att,
338 void *payload)
339 {
340 const struct pan_image_view *zs = att->iview;
341 uint64_t base, row_stride, surf_stride;
342
343 get_tiled_or_linear_att_mem_props(pan_image_view_get_zs_plane(zs),
344 zs->first_level, att->layer_or_z_slice,
345 &base, &row_stride, &surf_stride);
346 pan_cast_and_pack(payload, ZS_TARGET, cfg) {
347 cfg.write_format = translate_zs_format(zs->format);
348 cfg.block_format = MALI_BLOCK_FORMAT_LINEAR;
349 cfg.base = base;
350 cfg.row_stride = row_stride;
351 SET_SURFACE_STRIDE(cfg, surf_stride);
352 }
353 }
354
355 void
356 GENX(pan_emit_u_tiled_zs_attachment)(const struct pan_attachment_info *att,
357 void *payload)
358 {
359 const struct pan_image_view *zs = att->iview;
360 uint64_t base, row_stride, surf_stride;
361
362 get_tiled_or_linear_att_mem_props(pan_image_view_get_zs_plane(zs),
363 zs->first_level, att->layer_or_z_slice,
364 &base, &row_stride, &surf_stride);
365 pan_cast_and_pack(payload, ZS_TARGET, cfg) {
366 cfg.write_format = translate_zs_format(zs->format);
367 cfg.block_format = MALI_BLOCK_FORMAT_TILED_U_INTERLEAVED;
368 cfg.base = base;
369 cfg.row_stride = row_stride;
370 SET_SURFACE_STRIDE(cfg, surf_stride);
371 }
372 }
373
374 void
375 GENX(pan_emit_afbc_zs_attachment)(const struct pan_attachment_info *att,
376 void *payload)
377 {
378 const struct pan_image_view *zs = att->iview;
379 const struct pan_image_plane_ref pref = pan_image_view_get_zs_plane(zs);
380 uint64_t header, body_offset, hdr_row_stride;
381
382 get_afbc_att_mem_props(pref, zs->first_level, att->layer_or_z_slice,
383 &header, &body_offset, &hdr_row_stride);
384
385 pan_cast_and_pack(payload, AFBC_ZS_TARGET, cfg) {
386 cfg.write_format = translate_zs_format(zs->format);
387 cfg.block_format = get_afbc_block_format(pref.image->props.modifier);
388 #if PAN_ARCH >= 7
389 cfg.reverse_issue_order =
390 pan_needs_afbc_reverse_issue_order(att, pref.image);
391 #else
392 pan_warn_on_afbc_reverse_issue_order(att, pref.image);
393 #endif
394
395 #if PAN_ARCH >= 9
396 cfg.header = header;
397 cfg.body_offset = body_offset;
398 cfg.header_row_stride = hdr_row_stride;
399 #else
400 cfg.header = header;
401 cfg.body = header + body_offset;
402
403 #if PAN_ARCH >= 6
404 cfg.header_row_stride =
405 pan_afbc_stride_blocks(pref.image->props.format,
406 pref.image->props.modifier, hdr_row_stride);
407 #else
408 cfg.body_size = 0x1000;
409 cfg.chunk_size = 9;
410 cfg.sparse = true;
411 #endif
412 #endif
413 }
414 }
415
416 #if PAN_ARCH >= 10
417 void
418 GENX(pan_emit_interleaved_64k_s_attachment)(const struct pan_attachment_info *att,
419 void *payload)
420 {
421 const struct pan_image_view *s = att->iview;
422 uint64_t base, row_stride, surf_stride;
423
424 get_tiled_or_linear_att_mem_props(pan_image_view_get_s_plane(s),
425 s->first_level, att->layer_or_z_slice,
426 &base, &row_stride, &surf_stride);
427 pan_cast_and_pack(payload, S_TARGET, cfg) {
428 cfg.write_format = translate_s_format(s->format);
429 cfg.block_format = MALI_BLOCK_FORMAT_INTERLEAVED_64K;
430 cfg.base = base;
431 cfg.row_stride = row_stride;
432 SET_SURFACE_STRIDE(cfg, surf_stride);
433 }
434 }
435
436 void
437 GENX(pan_emit_interleaved_64k_zs_attachment)(const struct pan_attachment_info *att,
438 void *payload)
439 {
440 const struct pan_image_view *zs = att->iview;
441 uint64_t base, row_stride, surf_stride;
442
443 get_tiled_or_linear_att_mem_props(pan_image_view_get_zs_plane(zs),
444 zs->first_level, att->layer_or_z_slice,
445 &base, &row_stride, &surf_stride);
446 pan_cast_and_pack(payload, ZS_TARGET, cfg) {
447 cfg.write_format = translate_zs_format(zs->format);
448 cfg.block_format = MALI_BLOCK_FORMAT_INTERLEAVED_64K;
449 cfg.base = base;
450 cfg.row_stride = row_stride;
451 SET_SURFACE_STRIDE(cfg, surf_stride);
452 }
453 }
454 #endif
455
456 static void
457 pan_prepare_crc(const struct pan_fb_info *fb, int rt_crc,
458 struct MALI_CRC *crc)
459 {
460 if (rt_crc < 0)
461 return;
462
463 assert(rt_crc < fb->rt_count);
464
465 const struct pan_image_view *rt = fb->rts[rt_crc].view;
466 const struct pan_image_plane_ref pref = pan_image_view_get_color_plane(rt);
467 const struct pan_image *image = pref.image;
468 const struct pan_image_plane *plane = image->planes[pref.plane_idx];
469 const struct pan_image_slice_layout *slice =
470 &plane->layout.slices[rt->first_level];
471
472 crc->base = plane->base + slice->crc.offset_B;
473 crc->row_stride = slice->crc.stride_B;
474
475 #if PAN_ARCH >= 7
476 crc->render_target = rt_crc;
477
478 if (fb->rts[rt_crc].clear) {
479 uint32_t clear_val = fb->rts[rt_crc].clear_value[0];
480 crc->clear_color = clear_val | 0xc000000000000000 |
481 (((uint64_t)clear_val & 0xffff) << 32);
482 }
483 #endif
484 }
485
486 static void
487 pan_emit_zs_crc_ext(const struct pan_fb_info *fb, unsigned layer_idx,
488 int rt_crc,
489 struct mali_zs_crc_extension_packed *zs_crc_ext,
490 struct pan_clean_tile clean_tile)
491 {
492 struct mali_zs_crc_extension_packed desc;
493
494 pan_pack(&desc, ZS_CRC_EXTENSION, cfg) {
495 pan_prepare_crc(fb, rt_crc, &cfg.crc);
496 #if PAN_ARCH == 5
497 cfg.zs.clean_pixel_write_enable =
498 pan_clean_tile_write_zs_enabled(clean_tile);
499 #elif PAN_ARCH >= 6
500 cfg.zs.clean_tile_write_enable =
501 pan_clean_tile_write_zs_enabled(clean_tile);
502 #endif
503
504 cfg.zs.msaa = fb->zs.view.zs ? mali_sampling_mode(fb->zs.view.zs)
505 : MALI_MSAA_SINGLE;
506 cfg.s.msaa = fb->zs.view.s ? mali_sampling_mode(fb->zs.view.s)
507 : MALI_MSAA_SINGLE;
508 }
509
510 if (fb->zs.view.zs) {
511 const struct pan_image_plane_ref pref =
512 pan_image_view_get_zs_plane(fb->zs.view.zs);
513 const struct pan_mod_handler *mod_handler = pref.image->mod_handler;
514
515 const struct pan_attachment_info att = {
516 .iview = fb->zs.view.zs,
517 .layer_or_z_slice = layer_idx + fb->zs.view.zs->first_layer,
518 .fb_tile_size_px = fb->tile_size,
519 };
520
521 struct mali_zs_crc_extension_packed zs_part;
522 mod_handler->emit_zs_attachment(&att, &zs_part);
523 pan_merge(&desc, &zs_part, ZS_CRC_EXTENSION);
524 }
525
526 if (fb->zs.view.s) {
527 const struct pan_image_plane_ref pref =
528 pan_image_view_get_s_plane(fb->zs.view.s);
529 const struct pan_mod_handler *mod_handler = pref.image->mod_handler;
530
531 const struct pan_attachment_info att = {
532 .iview = fb->zs.view.s,
533 .layer_or_z_slice = layer_idx + fb->zs.view.s->first_layer,
534 .fb_tile_size_px = fb->tile_size,
535 };
536
537 struct mali_zs_crc_extension_packed s_part;
538 mod_handler->emit_s_attachment(&att, &s_part);
539 pan_merge(&desc, &s_part, ZS_CRC_EXTENSION);
540 }
541
542 *zs_crc_ext = desc;
543 }
544
545 /* Measure format as it appears in the tile buffer */
546
547 static unsigned
548 pan_bytes_per_pixel_tib(enum pipe_format format)
549 {
550 const struct pan_blendable_format *bf =
551 GENX(pan_blendable_format_from_pipe_format)(format);
552 return pan_format_tib_size(format, bf->internal);
553 }
554
555 static unsigned
556 pan_cbuf_bytes_per_pixel(const struct pan_fb_info *fb)
557 {
558 bool need_dummy = false;
559 unsigned sum = 0;
560
561 for (int cb = 0; cb < MAX2(fb->rt_count, 1); ++cb) {
562 const struct pan_image_view *rt = fb->rts[cb].view;
563 if (rt)
564 sum += pan_bytes_per_pixel_tib(rt->format) * rt->nr_samples;
565 else
566 need_dummy = true;
567 }
568
569 if (need_dummy) {
570 /* dummy/non-existent render-targets use RGBA8 UNORM, e.g 4 bytes */
571 sum = MAX2(sum, 4 * fb->nr_samples);
572 }
573
574 if (fb->pls_enabled) {
575 /* need at least 16 bytes per pixel for pixel local storage */
576 sum = MAX2(sum, 16);
577 }
578
579 return sum;
580 }
581
582 static unsigned
583 pan_zsbuf_bytes_per_pixel(const struct pan_fb_info *fb)
584 {
585 unsigned samples = fb->nr_samples;
586
587 const struct pan_image_view *zs_view = fb->zs.view.zs;
588 if (zs_view)
589 samples = zs_view->nr_samples;
590
591 const struct pan_image_view *s_view = fb->zs.view.s;
592 if (s_view)
593 samples = MAX2(samples, s_view->nr_samples);
594
595 /* Depth is always stored in a 32-bit float. Stencil requires depth to
596 * be allocated, but doesn't have it's own budget; it's tied to the
597 * depth buffer.
598 */
599 return sizeof(float) * samples;
600 }
601
602 /*
603 * Select the largest tile size that fits within the tilebuffer budget.
604 * Formally, maximize (pixels per tile) such that it is a power of two and
605 *
606 * (bytes per pixel) (pixels per tile) <= (max bytes per tile)
607 *
608 * A bit of algebra gives the following formula.
609 *
610 * Calculate the color buffer allocation size as well.
611 */
612 void
613 GENX(pan_select_tile_size)(struct pan_fb_info *fb)
614 {
615 unsigned bytes_per_pixel;
616
617 assert(util_is_power_of_two_nonzero(fb->tile_buf_budget));
618 assert(fb->tile_buf_budget >= 1024);
619
620 bytes_per_pixel = pan_cbuf_bytes_per_pixel(fb);
621 fb->tile_size = fb->tile_buf_budget >> util_logbase2_ceil(bytes_per_pixel);
622
623 unsigned zs_bytes_per_pixel = pan_zsbuf_bytes_per_pixel(fb);
624 if (zs_bytes_per_pixel > 0) {
625 assert(util_is_power_of_two_nonzero(fb->z_tile_buf_budget));
626 assert(fb->z_tile_buf_budget >= 1024);
627
628 fb->tile_size =
629 MIN2(fb->tile_size,
630 fb->z_tile_buf_budget >> util_logbase2_ceil(zs_bytes_per_pixel));
631 }
632
633 #if PAN_ARCH != 6
634 /* Check if we're using too much tile-memory; if we are, try disabling
635 * pipelining. This works because we're starting with an optimistic half
636 * of the tile-budget, so we actually have another half that can be used.
637 *
638 * On v6 GPUs, doing this is not allowed; they *have* to pipeline.
639 */
640 if (fb->tile_size < 4 * 4)
641 fb->tile_size *= 2;
642 #endif
643
644 /* Clamp tile size to hardware limits */
645 fb->tile_size = MIN2(fb->tile_size, pan_max_effective_tile_size(PAN_ARCH));
646 assert(fb->tile_size >= 4 * 4);
647
648 /* Colour buffer allocations must be 1K aligned. */
649 fb->cbuf_allocation = ALIGN_POT(bytes_per_pixel * fb->tile_size, 1024);
650 #if PAN_ARCH == 6
651 assert(fb->cbuf_allocation <= fb->tile_buf_budget && "tile too big");
652 #else
653 assert(fb->cbuf_allocation <= fb->tile_buf_budget * 2 && "tile too big");
654 #endif
655 }
656
657 static enum mali_color_format
658 pan_mfbd_raw_format(unsigned bits)
659 {
660 /* clang-format off */
661 switch (bits) {
662 case 8: return MALI_COLOR_FORMAT_RAW8;
663 case 16: return MALI_COLOR_FORMAT_RAW16;
664 case 24: return MALI_COLOR_FORMAT_RAW24;
665 case 32: return MALI_COLOR_FORMAT_RAW32;
666 case 48: return MALI_COLOR_FORMAT_RAW48;
667 case 64: return MALI_COLOR_FORMAT_RAW64;
668 case 96: return MALI_COLOR_FORMAT_RAW96;
669 case 128: return MALI_COLOR_FORMAT_RAW128;
670 case 192: return MALI_COLOR_FORMAT_RAW192;
671 case 256: return MALI_COLOR_FORMAT_RAW256;
672 case 384: return MALI_COLOR_FORMAT_RAW384;
673 case 512: return MALI_COLOR_FORMAT_RAW512;
674 case 768: return MALI_COLOR_FORMAT_RAW768;
675 case 1024: return MALI_COLOR_FORMAT_RAW1024;
676 case 1536: return MALI_COLOR_FORMAT_RAW1536;
677 case 2048: return MALI_COLOR_FORMAT_RAW2048;
678 default: UNREACHABLE("invalid raw bpp");
679 }
680 /* clang-format on */
681 }
682
683 static void
684 get_rt_formats(enum pipe_format pfmt, uint32_t *writeback, uint32_t *internal,
685 uint32_t *pswizzle)
686 {
687 /* Explode details on the format */
688 const struct util_format_description *desc = util_format_description(pfmt);
689
690 /* The swizzle for rendering is inverted from texturing */
691 unsigned char swizzle[4] = {
692 PIPE_SWIZZLE_X,
693 PIPE_SWIZZLE_Y,
694 PIPE_SWIZZLE_Z,
695 PIPE_SWIZZLE_W,
696 };
697
698 const struct pan_blendable_format *bfmt =
699 GENX(pan_blendable_format_from_pipe_format)(pfmt);
700
701 if (bfmt->internal) {
702 *internal = bfmt->internal;
703 *writeback = bfmt->writeback;
704 pan_invert_swizzle(desc->swizzle, swizzle);
705 } else {
706 /* Construct RAW internal/writeback, where internal is
707 * specified logarithmically (round to next power-of-two).
708 * Offset specified from RAW8, where 8 = 2^3 */
709
710 unsigned bits = desc->block.bits;
711 assert(bits >= 8 && bits <= 128);
712 unsigned offset = util_logbase2_ceil(bits) - 3;
713 assert(offset <= 4);
714
715 *internal = MALI_COLOR_BUFFER_INTERNAL_FORMAT_RAW8 + offset;
716 *writeback = pan_mfbd_raw_format(bits);
717 }
718
719 *pswizzle = pan_translate_swizzle_4(swizzle);
720 }
721
722 void
723 GENX(pan_emit_default_color_attachment)(enum pipe_format format,
724 void *payload)
725 {
726 pan_cast_and_pack(payload, RGB_RENDER_TARGET, cfg) {
727 get_rt_formats(format, &cfg.writeback_format, &cfg.internal_format,
728 &cfg.swizzle);
729 cfg.srgb = util_format_is_srgb(format);
730 }
731 }
732
733 void
734 GENX(pan_emit_afbc_color_attachment)(const struct pan_attachment_info *att,
735 void *payload)
736 {
737 const struct pan_image_view *iview = att->iview;
738 const struct pan_image_plane_ref pref = pan_image_view_get_color_plane(iview);
739 const struct pan_image *image = pref.image;
740 uint64_t header, body_offset, hdr_row_stride;
741
742 get_afbc_att_mem_props(pref, iview->first_level, att->layer_or_z_slice,
743 &header, &body_offset, &hdr_row_stride);
744
745 /* TODO: YUV RT. */
746 assert(!pan_format_is_yuv(iview->format));
747 pan_cast_and_pack(payload, AFBC_RGB_RENDER_TARGET, cfg) {
748 cfg.write_enable = true;
749 get_rt_formats(iview->format, &cfg.writeback_format, &cfg.internal_format,
750 &cfg.swizzle);
751
752 /* AFBC+RAW24 is not supported on v9+. Use RAW32 instead, and let the AFBC
753 * compression mode select the actual size.
754 */
755 if (PAN_ARCH >= 9 && cfg.writeback_format == MALI_COLOR_FORMAT_RAW24)
756 cfg.writeback_format = MALI_COLOR_FORMAT_RAW32;
757
758 cfg.srgb = util_format_is_srgb(iview->format);
759 cfg.writeback_block_format = get_afbc_block_format(image->props.modifier);
760 cfg.yuv_transform = image->props.modifier & AFBC_FORMAT_MOD_YTR;
761 #if PAN_ARCH >= 6
762 cfg.wide_block = pan_afbc_is_wide(image->props.modifier);
763 cfg.split_block = (image->props.modifier & AFBC_FORMAT_MOD_SPLIT);
764 #endif
765 #if PAN_ARCH >= 7
766 cfg.reverse_issue_order =
767 pan_needs_afbc_reverse_issue_order(att, pref.image);
768 #else
769 pan_warn_on_afbc_reverse_issue_order(att, pref.image);
770 #endif
771
772 #if PAN_ARCH >= 9
773 cfg.header = header;
774 cfg.body_offset = body_offset;
775 cfg.row_stride = hdr_row_stride;
776 cfg.compression_mode = pan_afbc_compression_mode(
777 image->planes[pref.plane_idx]->layout.afbc.mode);
778 #else
779 cfg.header = header;
780 cfg.body = header + body_offset;
781
782 #if PAN_ARCH >= 6
783 cfg.row_stride =
784 pan_afbc_stride_blocks(image->props.format,
785 image->props.modifier, hdr_row_stride);
786 #else
787 const struct pan_image_plane *plane = image->planes[pref.plane_idx];
788 const struct pan_image_slice_layout *slayout =
789 &plane->layout.slices[iview->first_level];
790
791 cfg.body_size = slayout->afbc.surface_stride_B -
792 pan_afbc_body_offset(PAN_ARCH, image->props.modifier,
793 slayout->afbc.header.surface_size_B);
794 cfg.chunk_size = 9;
795 cfg.sparse = true;
796 #endif
797 #endif
798 }
799 }
800
801 void
802 GENX(pan_emit_u_tiled_color_attachment)(const struct pan_attachment_info *att,
803 void *payload)
804 {
805 const struct pan_image_view *iview = att->iview;
806 uint64_t base, row_stride, surf_stride;
807
808 get_tiled_or_linear_att_mem_props(pan_image_view_get_color_plane(iview),
809 iview->first_level, att->layer_or_z_slice,
810 &base, &row_stride, &surf_stride);
811
812 /* TODO: YUV RT. */
813 assert(!pan_format_is_yuv(iview->format));
814 pan_cast_and_pack(payload, RGB_RENDER_TARGET, cfg) {
815 cfg.write_enable = true;
816 cfg.writeback_block_format = MALI_BLOCK_FORMAT_TILED_U_INTERLEAVED;
817 get_rt_formats(iview->format, &cfg.writeback_format, &cfg.internal_format,
818 &cfg.swizzle);
819 cfg.srgb = util_format_is_srgb(iview->format);
820 cfg.writeback_buffer.base = base;
821 cfg.writeback_buffer.row_stride = row_stride;
822 cfg.writeback_buffer.surface_stride = surf_stride;
823 }
824 }
825
826 void
827 GENX(pan_emit_linear_color_attachment)(const struct pan_attachment_info *att,
828 void *payload)
829 {
830 const struct pan_image_view *iview = att->iview;
831 uint64_t base, row_stride, surf_stride;
832
833 get_tiled_or_linear_att_mem_props(pan_image_view_get_color_plane(iview),
834 iview->first_level, att->layer_or_z_slice,
835 &base, &row_stride, &surf_stride);
836
837 /* TODO: YUV RT. */
838 assert(!pan_format_is_yuv(iview->format));
839 pan_cast_and_pack(payload, RGB_RENDER_TARGET, cfg) {
840 cfg.write_enable = true;
841 cfg.writeback_block_format = MALI_BLOCK_FORMAT_LINEAR;
842 get_rt_formats(iview->format, &cfg.writeback_format, &cfg.internal_format,
843 &cfg.swizzle);
844 cfg.srgb = util_format_is_srgb(iview->format);
845 cfg.writeback_buffer.base = base;
846 cfg.writeback_buffer.row_stride = row_stride;
847 cfg.writeback_buffer.surface_stride = surf_stride;
848 }
849 }
850
851 #if PAN_ARCH >= 10
852 void
853 GENX(pan_emit_interleaved_64k_color_attachment)(const struct pan_attachment_info *att,
854 void *payload)
855 {
856 const struct pan_image_view *iview = att->iview;
857 uint64_t base, row_stride, surf_stride;
858
859 get_tiled_or_linear_att_mem_props(pan_image_view_get_color_plane(iview),
860 iview->first_level, att->layer_or_z_slice,
861 &base, &row_stride, &surf_stride);
862
863 /* TODO: YUV RT. */
864 assert(!pan_format_is_yuv(iview->format));
865 pan_cast_and_pack(payload, RGB_RENDER_TARGET, cfg) {
866 cfg.write_enable = true;
867 cfg.writeback_block_format = MALI_BLOCK_FORMAT_INTERLEAVED_64K;
868 get_rt_formats(iview->format, &cfg.writeback_format, &cfg.internal_format,
869 &cfg.swizzle);
870 cfg.srgb = util_format_is_srgb(iview->format);
871 cfg.writeback_buffer.base = base;
872 cfg.writeback_buffer.row_stride = row_stride;
873 cfg.writeback_buffer.surface_stride = surf_stride;
874 }
875 }
876
877 void
878 GENX(pan_emit_afrc_color_attachment)(const struct pan_attachment_info *att,
879 void *payload)
880 {
881 const struct pan_image_view *iview = att->iview;
882 const struct pan_image_plane_ref pref = pan_image_view_get_color_plane(iview);
883 const struct pan_image *image = pref.image;
884 struct pan_afrc_format_info finfo =
885 pan_afrc_get_format_info(image->props.format);
886 uint64_t base, row_stride, surf_stride;
887
888 get_tiled_or_linear_att_mem_props(pref,
889 iview->first_level, att->layer_or_z_slice,
890 &base, &row_stride, &surf_stride);
891
892 /* TODO: YUV RT. */
893 assert(!pan_format_is_yuv(iview->format));
894 pan_cast_and_pack(payload, AFRC_RGB_RENDER_TARGET, cfg) {
895 cfg.afrc_block_size = pan_afrc_block_size(image->props.modifier, 0);
896 cfg.afrc_format = pan_afrc_format(finfo, image->props.modifier, 0);
897 get_rt_formats(iview->format, &cfg.writeback_format, &cfg.internal_format,
898 &cfg.swizzle);
899 cfg.srgb = util_format_is_srgb(iview->format);
900 cfg.writeback_buffer.base = base;
901 cfg.writeback_buffer.row_stride = row_stride;
902 cfg.writeback_buffer.surface_stride = surf_stride;
903 }
904 }
905 #endif
906 #endif
907
908 void
909 GENX(pan_emit_tls)(const struct pan_tls_info *info,
910 struct mali_local_storage_packed *out)
911 {
912 pan_pack(out, LOCAL_STORAGE, cfg) {
913 if (info->tls.size) {
914 unsigned shift = pan_get_stack_shift(info->tls.size);
915
916 cfg.tls_size = shift;
917 #if PAN_ARCH >= 9
918 /* For now, always use packed TLS addressing. This is
919 * better for the cache and requires no fix up code in
920 * the shader. We may need to revisit this someday for
921 * OpenCL generic pointer support.
922 */
923 cfg.tls_address_mode = MALI_ADDRESS_MODE_PACKED;
924
925 assert((info->tls.ptr & 4095) == 0);
926 cfg.tls_base_pointer = info->tls.ptr >> 8;
927 #else
928 cfg.tls_base_pointer = info->tls.ptr;
929 #endif
930 }
931
932 if (info->wls.size) {
933 assert(!(info->wls.ptr & 4095));
934 assert((info->wls.ptr & 0xffffffff00000000ULL) ==
935 ((info->wls.ptr + info->wls.size - 1) & 0xffffffff00000000ULL));
936 cfg.wls_base_pointer = info->wls.ptr;
937 unsigned wls_size = pan_wls_adjust_size(info->wls.size);
938 cfg.wls_instances = info->wls.instances;
939 cfg.wls_size_scale = util_logbase2(wls_size) + 1;
940 } else {
941 cfg.wls_instances = MALI_LOCAL_STORAGE_NO_WORKGROUP_MEM;
942 }
943 }
944 }
945
946 #if PAN_ARCH <= 5
947 static void
948 pan_emit_midgard_tiler(const struct pan_fb_info *fb,
949 const struct pan_tiler_context *tiler_ctx,
950 struct mali_tiler_context_packed *out)
951 {
952 bool hierarchy = !tiler_ctx->midgard.no_hierarchical_tiling;
953
954 assert(tiler_ctx->midgard.polygon_list);
955
956 pan_pack(out, TILER_CONTEXT, cfg) {
957 unsigned header_size;
958
959 if (tiler_ctx->midgard.disable) {
960 cfg.hierarchy_mask =
961 hierarchy ? MALI_MIDGARD_TILER_DISABLED : MALI_MIDGARD_TILER_USER;
962 header_size = MALI_MIDGARD_TILER_MINIMUM_HEADER_SIZE;
963 cfg.polygon_list_size = header_size + (hierarchy ? 0 : 4);
964 cfg.heap_start = tiler_ctx->midgard.polygon_list;
965 cfg.heap_end = tiler_ctx->midgard.polygon_list;
966 } else {
967 cfg.hierarchy_mask = pan_choose_hierarchy_mask(
968 fb->width, fb->height, tiler_ctx->midgard.vertex_count, hierarchy);
969 header_size = pan_tiler_header_size(fb->width, fb->height,
970 cfg.hierarchy_mask, hierarchy);
971 cfg.polygon_list_size = pan_tiler_full_size(
972 fb->width, fb->height, cfg.hierarchy_mask, hierarchy);
973 cfg.heap_start = tiler_ctx->midgard.heap.start;
974 cfg.heap_end = cfg.heap_start + tiler_ctx->midgard.heap.size;
975 }
976
977 cfg.polygon_list = tiler_ctx->midgard.polygon_list;
978 cfg.polygon_list_body = cfg.polygon_list + header_size;
979 }
980 }
981 #endif
982
983 #if PAN_ARCH >= 5
984 static struct MALI_RT_CLEAR
985 rt_clear(const struct pan_fb_color_attachment *rt)
986 {
987 if (!rt->clear)
988 return (struct MALI_RT_CLEAR){0};
989
990 return (struct MALI_RT_CLEAR){
991 .color_0 = rt->clear_value[0],
992 .color_1 = rt->clear_value[1],
993 .color_2 = rt->clear_value[2],
994 .color_3 = rt->clear_value[3],
995 };
996 }
997
998 static void
999 pan_emit_rt(const struct pan_fb_info *fb, unsigned layer_idx, unsigned idx,
1000 unsigned cbuf_offset, struct mali_render_target_packed *out,
1001 struct pan_clean_tile clean_tile)
1002 {
1003 const struct pan_image_view *rt = fb->rts[idx].view;
1004
1005 if (!rt || fb->rts[idx].discard) {
1006 pan_cast_and_pack(out, RGB_RENDER_TARGET, cfg) {
1007 #if PAN_ARCH == 5
1008 cfg.clean_pixel_write_enable =
1009 pan_clean_tile_write_rt_enabled(clean_tile, idx);
1010 #elif PAN_ARCH >= 6
1011 cfg.clean_tile_write_enable =
1012 pan_clean_tile_write_rt_enabled(clean_tile, idx);
1013 #endif
1014 cfg.internal_buffer_offset = cbuf_offset;
1015 cfg.clear = rt_clear(&fb->rts[idx]);
1016 cfg.dithering_enable = true;
1017 cfg.internal_format = MALI_COLOR_BUFFER_INTERNAL_FORMAT_R8G8B8A8;
1018 #if PAN_ARCH >= 7
1019 cfg.writeback_block_format = MALI_BLOCK_FORMAT_TILED_U_INTERLEAVED;
1020 #endif
1021 }
1022
1023 return;
1024 }
1025
1026 struct mali_rgb_render_target_packed common;
1027 pan_pack(&common, RGB_RENDER_TARGET, cfg) {
1028 assert(fb->rts[idx].view != NULL);
1029 #if PAN_ARCH == 5
1030 cfg.clean_pixel_write_enable =
1031 pan_clean_tile_write_rt_enabled(clean_tile, idx);
1032 #elif PAN_ARCH >= 6
1033 cfg.clean_tile_write_enable =
1034 pan_clean_tile_write_rt_enabled(clean_tile, idx);
1035 #endif
1036 cfg.internal_buffer_offset = cbuf_offset;
1037 cfg.clear = rt_clear(&fb->rts[idx]);
1038 cfg.dithering_enable = true;
1039 cfg.writeback_msaa = mali_sampling_mode(fb->rts[idx].view);
1040 }
1041
1042 struct pan_image_plane_ref pref = pan_image_view_get_color_plane(rt);
1043 assert(pref.image);
1044 const struct pan_mod_handler *mod_handler = pref.image->mod_handler;
1045 assert(mod_handler);
1046
1047 ASSERTED unsigned layer_count = rt->dim == MALI_TEXTURE_DIMENSION_3D
1048 ? pref.image->props.extent_px.depth
1049 : rt->last_layer - rt->first_layer + 1;
1050
1051 assert(rt->last_level == rt->first_level);
1052 assert(layer_idx < layer_count);
1053 const struct pan_attachment_info att = {
1054 .iview = fb->rts[idx].view,
1055 .layer_or_z_slice = layer_idx + rt->first_layer,
1056 .fb_tile_size_px = fb->tile_size,
1057 };
1058
1059 struct mali_render_target_packed desc;
1060 mod_handler->emit_color_attachment(&att, &desc);
1061
1062 /* Avoid mixing loads and stores on write-combined memory. */
1063 pan_merge(&desc, &common, RGB_RENDER_TARGET);
1064 *out = desc;
1065 }
1066
1067 #if PAN_ARCH >= 6
1068 /* All GPUs starting from Bifrost are affected by issue TSIX-2033:
1069 *
1070 * Forcing clean_tile_writes breaks INTERSECT readbacks
1071 *
1072 * To workaround, use the pre-frame shader mode ALWAYS instead of INTERSECT if
1073 * clean_tile_write_enable is set on either one of the color, depth or stencil
1074 * buffers. Since INTERSECT is a hint that the hardware may ignore, this
1075 * cannot affect correctness, only performance. */
1076
1077 static enum mali_pre_post_frame_shader_mode
1078 pan_fix_frame_shader_mode(enum mali_pre_post_frame_shader_mode mode,
1079 bool force_clean_tile)
1080 {
1081 if (force_clean_tile && mode == MALI_PRE_POST_FRAME_SHADER_MODE_INTERSECT)
1082 return MALI_PRE_POST_FRAME_SHADER_MODE_ALWAYS;
1083 else
1084 return mode;
1085 }
1086 #endif
1087
1088 /* Clean tiles must be written back for AFBC buffers (color, z/s) when either
1089 * one of the effective tile size dimension is smaller than the superblock
1090 * dimension.
1091 *
1092 * This isn't just about making sure we render the whole superblock. If we
1093 * don't, set clean_tile_write_enable and only part of the superblock gets
1094 * written, compression can go wrong and the results may be garbage.
1095 */
1096 bool
1097 GENX(pan_force_clean_write_on)(const struct pan_image *image,
1098 unsigned fb_tile_size_px)
1099 {
1100 #if PAN_ARCH >= 6
1101 if (!image)
1102 return false;
1103
1104 if (!drm_is_afbc(image->props.modifier))
1105 return false;
1106
1107 return pan_afbc_superblock_exceeds_tile_size(image->props.modifier,
1108 fb_tile_size_px);
1109 #else
1110 return false;
1111 #endif
1112 }
1113
1114 static struct pan_clean_tile
1115 pan_get_clean_tile_info(const struct pan_fb_info *fb)
1116 {
1117 struct pan_clean_tile clean_tile = { 0, };
1118 const struct pan_image *img;
1119
1120 /* Force write-back of each color, depth and stencil attachment if cleared
1121 * or forced, unless it's discarded. */
1122
1123 for (unsigned i = 0; i < fb->rt_count; ++i) {
1124 if (fb->rts[i].discard)
1125 continue;
1126
1127 img = fb->rts[i].view ?
1128 pan_image_view_get_color_plane(fb->rts[i].view).image : NULL;
1129 if (fb->rts[i].clear ||
1130 GENX(pan_force_clean_write_on)(img, fb->tile_size))
1131 clean_tile.write_rt_mask |= 1 << i;
1132 }
1133
1134 if (!fb->zs.discard.z) {
1135 img = fb->zs.view.zs ?
1136 pan_image_view_get_zs_plane(fb->zs.view.zs).image : NULL;
1137 if (fb->zs.clear.z || GENX(pan_force_clean_write_on)(img, fb->tile_size))
1138 clean_tile.write_zs = 1;
1139 const bool zs_has_stencil = img &&
1140 util_format_has_stencil(util_format_description(img->props.format));
1141 if (zs_has_stencil && fb->zs.clear.s)
1142 clean_tile.write_zs = 1;
1143 }
1144
1145 if (!fb->zs.discard.s) {
1146 img = fb->zs.view.s ?
1147 pan_image_view_get_s_plane(fb->zs.view.s).image : NULL;
1148 if (fb->zs.clear.s || GENX(pan_force_clean_write_on)(img, fb->tile_size))
1149 clean_tile.write_zs = 1;
1150 }
1151
1152 return clean_tile;
1153 }
1154
1155 static void
1156 check_fb_attachments(const struct pan_fb_info *fb)
1157 {
1158 #ifndef NDEBUG
1159 for (unsigned i = 0; i < fb->rt_count; i++) {
1160 if (fb->rts[i].view) {
1161 pan_image_view_check(fb->rts[i].view);
1162 assert(fb->rts[i].view->nr_samples == fb->nr_samples);
1163 }
1164 }
1165
1166 if (fb->zs.view.zs) {
1167 pan_image_view_check(fb->zs.view.zs);
1168 assert(fb->zs.view.zs->nr_samples == fb->nr_samples);
1169 }
1170 if (fb->zs.view.s)
1171 pan_image_view_check(fb->zs.view.s);
1172 #endif
1173 }
1174
1175 unsigned
1176 GENX(pan_emit_fbd)(const struct pan_fb_info *fb, unsigned layer_idx,
1177 const struct pan_tls_info *tls,
1178 const struct pan_tiler_context *tiler_ctx, void *out)
1179 {
1180 PAN_TRACE_FUNC(PAN_TRACE_LIB_DESC);
1181
1182 check_fb_attachments(fb);
1183
1184 void *fbd = out;
1185 void *rtd = out + pan_size(FRAMEBUFFER);
1186
1187 #if PAN_ARCH <= 5
1188 GENX(pan_emit_tls)(tls, pan_section_ptr(fbd, FRAMEBUFFER, LOCAL_STORAGE));
1189 #endif
1190
1191 int crc_rt = GENX(pan_select_crc_rt)(fb, fb->tile_size);
1192 bool has_zs_crc_ext = (fb->zs.view.zs || fb->zs.view.s || crc_rt >= 0);
1193 struct pan_clean_tile clean_tile = pan_get_clean_tile_info(fb);
1194
1195 pan_section_pack(fbd, FRAMEBUFFER, PARAMETERS, cfg) {
1196 #if PAN_ARCH >= 6
1197 cfg.sample_locations = fb->sample_positions;
1198 cfg.pre_frame_0 = pan_fix_frame_shader_mode(fb->bifrost.pre_post.modes[0],
1199 pan_clean_tile_write_any_set(clean_tile));
1200 cfg.pre_frame_1 = pan_fix_frame_shader_mode(fb->bifrost.pre_post.modes[1],
1201 pan_clean_tile_write_any_set(clean_tile));
1202 cfg.post_frame = fb->bifrost.pre_post.modes[2];
1203 cfg.frame_shader_dcds = fb->bifrost.pre_post.dcds.gpu;
1204 cfg.tiler =
1205 PAN_ARCH >= 9 ? tiler_ctx->valhall.desc : tiler_ctx->bifrost.desc;
1206 #endif
1207 cfg.width = fb->width;
1208 cfg.height = fb->height;
1209 cfg.bound_min_x = fb->frame_bounding_box.minx;
1210 cfg.bound_min_y = fb->frame_bounding_box.miny;
1211 cfg.bound_max_x = fb->frame_bounding_box.maxx;
1212 cfg.bound_max_y = fb->frame_bounding_box.maxy;
1213 cfg.effective_tile_size = fb->tile_size;
1214 /* Ensure we cover the samples on the edge for 16x MSAA */
1215 cfg.tie_break_rule = fb->nr_samples == 16 ?
1216 MALI_TIE_BREAK_RULE_MINUS_180_OUT_0_IN :
1217 MALI_TIE_BREAK_RULE_MINUS_180_IN_0_OUT;
1218 cfg.render_target_count = MAX2(fb->rt_count, 1);
1219
1220 /* Default to 24 bit depth if there's no surface. */
1221 cfg.z_internal_format =
1222 fb->zs.view.zs ? pan_get_z_internal_format(fb->zs.view.zs->format)
1223 : MALI_Z_INTERNAL_FORMAT_D24;
1224
1225 cfg.z_clear = fb->zs.clear_value.depth;
1226 cfg.s_clear = fb->zs.clear_value.stencil;
1227 cfg.color_buffer_allocation = fb->cbuf_allocation;
1228
1229 /* The force_samples setting dictates the sample-count that is used
1230 * for rasterization, and works like D3D11's ForcedSampleCount feature:
1231 *
1232 * - If force_samples == 0: Let nr_samples dictate sample count
1233 * - If force_samples == 1: force single-sampled rasterization
1234 * - If force_samples >= 1: force multi-sampled rasterization
1235 *
1236 * This can be used to read SYSTEM_VALUE_SAMPLE_MASK_IN from the
1237 * fragment shader, even when performing single-sampled rendering.
1238 */
1239 if (fb->pls_enabled) {
1240 cfg.sample_count = 4;
1241 cfg.sample_pattern = pan_sample_pattern(1);
1242 } else if (!fb->force_samples) {
1243 cfg.sample_count = fb->nr_samples;
1244 cfg.sample_pattern = pan_sample_pattern(fb->nr_samples);
1245 } else if (fb->force_samples == 1) {
1246 cfg.sample_count = fb->nr_samples;
1247 cfg.sample_pattern = pan_sample_pattern(1);
1248 } else {
1249 cfg.sample_count = 1;
1250 cfg.sample_pattern = pan_sample_pattern(fb->force_samples);
1251 }
1252
1253 cfg.z_write_enable = (fb->zs.view.zs && !fb->zs.discard.z);
1254 cfg.s_write_enable = (fb->zs.view.s && !fb->zs.discard.s);
1255 cfg.has_zs_crc_extension = has_zs_crc_ext;
1256
1257 if (crc_rt >= 0) {
1258 bool *valid = fb->rts[crc_rt].crc_valid;
1259 bool full = !fb->draw_extent.minx && !fb->draw_extent.miny &&
1260 fb->draw_extent.maxx == (fb->width - 1) &&
1261 fb->draw_extent.maxy == (fb->height - 1);
1262
1263 /* If the CRC was valid it stays valid, if it wasn't, we must ensure
1264 * the render operation covers the full frame, and clean tiles are
1265 * pushed to memory. */
1266 bool new_valid = *valid |
1267 (full && pan_clean_tile_write_rt_enabled(clean_tile, crc_rt));
1268
1269 cfg.crc_read_enable = *valid;
1270
1271 /* If the data is currently invalid, still write CRC
1272 * data if we are doing a full write, so that it is
1273 * valid for next time. */
1274 cfg.crc_write_enable = new_valid;
1275
1276 *valid = new_valid;
1277 }
1278
1279 #if PAN_ARCH >= 9
1280 cfg.point_sprite_coord_origin_max_y = fb->sprite_coord_origin;
1281 cfg.first_provoking_vertex = fb->first_provoking_vertex;
1282
1283 /* internal_layer_index is used to select the right primitive list in the
1284 * tiler context, and frame_arg is the value that's passed to the fragment
1285 * shader through r62-r63, which we use to pass gl_Layer. Since the
1286 * layer_idx only takes 8-bits, we might use the extra 56-bits we have
1287 * in frame_argument to pass other information to the fragment shader at
1288 * some point. */
1289 assert(layer_idx >= tiler_ctx->valhall.layer_offset);
1290 cfg.internal_layer_index = layer_idx - tiler_ctx->valhall.layer_offset;
1291 cfg.frame_argument = layer_idx;
1292 #endif
1293
1294 #if PAN_ARCH >= 13
1295 unsigned zs_bytes_per_pixel = pan_zsbuf_bytes_per_pixel(fb);
1296 /* We can interleave HSR if we have space for two ZS tiles in the tile
1297 * buffer. */
1298 unsigned max_zs_tile_size_interleave =
1299 fb->z_tile_buf_budget >> util_logbase2_ceil(zs_bytes_per_pixel);
1300 bool hsr_can_interleave = fb->tile_size <= max_zs_tile_size_interleave;
1301
1302 /* Enabling prepass without interleave is generally not good for
1303 * performance, so disable HSR in that case. */
1304 cfg.hsr_prepass_enable = fb->allow_hsr_prepass && hsr_can_interleave;
1305 cfg.hsr_prepass_interleaving_enable = hsr_can_interleave;
1306 cfg.hsr_prepass_filter_enable = true;
1307 cfg.hsr_hierarchical_optimizations_enable = true;
1308 #endif
1309 }
1310
1311 #if PAN_ARCH >= 6
1312 pan_section_pack(fbd, FRAMEBUFFER, PADDING, padding)
1313 ;
1314 #else
1315 pan_emit_midgard_tiler(fb, tiler_ctx,
1316 pan_section_ptr(fbd, FRAMEBUFFER, TILER));
1317
1318 /* All weights set to 0, nothing to do here */
1319 pan_section_pack(fbd, FRAMEBUFFER, TILER_WEIGHTS, w)
1320 ;
1321 #endif
1322
1323 if (has_zs_crc_ext) {
1324 struct mali_zs_crc_extension_packed *zs_crc_ext =
1325 out + pan_size(FRAMEBUFFER);
1326
1327 pan_emit_zs_crc_ext(fb, layer_idx, crc_rt, zs_crc_ext, clean_tile);
1328 rtd += pan_size(ZS_CRC_EXTENSION);
1329 }
1330
1331 unsigned rt_count = MAX2(fb->rt_count, 1);
1332 unsigned cbuf_offset = 0;
1333 for (unsigned i = 0; i < rt_count; i++) {
1334 pan_emit_rt(fb, layer_idx, i, cbuf_offset, rtd, clean_tile);
1335 rtd += pan_size(RENDER_TARGET);
1336 if (!fb->rts[i].view)
1337 continue;
1338
1339 cbuf_offset += pan_bytes_per_pixel_tib(fb->rts[i].view->format) *
1340 fb->tile_size *
1341 pan_image_view_get_nr_samples(fb->rts[i].view);
1342
1343 if (i != crc_rt && fb->rts[i].crc_valid != NULL)
1344 *(fb->rts[i].crc_valid) = false;
1345 }
1346
1347 struct mali_framebuffer_pointer_packed tag;
1348 pan_pack(&tag, FRAMEBUFFER_POINTER, cfg) {
1349 cfg.zs_crc_extension_present = has_zs_crc_ext;
1350 cfg.render_target_count = MAX2(fb->rt_count, 1);
1351 }
1352 return tag.opaque[0];
1353 }
1354 #else /* PAN_ARCH == 4 */
1355 static enum mali_color_format
1356 pan_sfbd_raw_format(unsigned bits)
1357 {
1358 /* clang-format off */
1359 switch (bits) {
1360 case 16: return MALI_COLOR_FORMAT_1_16B_CHANNEL;
1361 case 32: return MALI_COLOR_FORMAT_1_32B_CHANNEL;
1362 case 48: return MALI_COLOR_FORMAT_3_16B_CHANNELS;
1363 case 64: return MALI_COLOR_FORMAT_2_32B_CHANNELS;
1364 case 96: return MALI_COLOR_FORMAT_3_32B_CHANNELS;
1365 case 128: return MALI_COLOR_FORMAT_4_32B_CHANNELS;
1366 default: UNREACHABLE("invalid raw bpp");
1367 }
1368 /* clang-format on */
1369 }
1370
1371 void
1372 GENX(pan_select_tile_size)(struct pan_fb_info *fb)
1373 {
1374 /* Tile size and color buffer allocation are not configurable on gen 4 */
1375 fb->tile_size = 16 * 16;
1376 }
1377
1378 unsigned
1379 GENX(pan_emit_fbd)(const struct pan_fb_info *fb, unsigned layer_idx,
1380 const struct pan_tls_info *tls,
1381 const struct pan_tiler_context *tiler_ctx, void *fbd)
1382 {
1383 PAN_TRACE_FUNC(PAN_TRACE_LIB_DESC);
1384
1385 assert(fb->rt_count <= 1);
1386
1387 GENX(pan_emit_tls)(tls, pan_section_ptr(fbd, FRAMEBUFFER, LOCAL_STORAGE));
1388 pan_section_pack(fbd, FRAMEBUFFER, PARAMETERS, cfg) {
1389 cfg.bound_max_x = fb->width - 1;
1390 cfg.bound_max_y = fb->height - 1;
1391 cfg.dithering_enable = true;
1392 cfg.tie_break_rule = MALI_TIE_BREAK_RULE_MINUS_180_IN_0_OUT;
1393 if (fb->rts[0].clear) {
1394 cfg.clean_pixel_write_enable = true;
1395 cfg.clear_color_0 = fb->rts[0].clear_value[0];
1396 cfg.clear_color_1 = fb->rts[0].clear_value[1];
1397 cfg.clear_color_2 = fb->rts[0].clear_value[2];
1398 cfg.clear_color_3 = fb->rts[0].clear_value[3];
1399 }
1400
1401 if (fb->zs.clear.z)
1402 cfg.z_clear = fb->zs.clear_value.depth;
1403
1404 if (fb->zs.clear.s)
1405 cfg.s_clear = fb->zs.clear_value.stencil;
1406
1407 if (fb->rt_count && fb->rts[0].view) {
1408 const struct pan_image_view *rt = fb->rts[0].view;
1409 const struct pan_image_plane_ref pref =
1410 pan_image_view_get_color_plane(rt);
1411 const struct pan_image *image = pref.image;
1412 const struct pan_image_plane *plane = image->planes[pref.plane_idx];
1413 const struct pan_image_slice_layout *slayout =
1414 &plane->layout.slices[rt->first_level];
1415 const unsigned array_idx =
1416 image->props.dim == MALI_TEXTURE_DIMENSION_3D ? 0 : rt->first_layer;
1417 const unsigned surf_idx =
1418 image->props.dim == MALI_TEXTURE_DIMENSION_3D ? rt->first_layer : 0;
1419
1420 const struct util_format_description *desc =
1421 util_format_description(rt->format);
1422
1423 /* The swizzle for rendering is inverted from texturing */
1424 unsigned char swizzle[4];
1425 pan_invert_swizzle(desc->swizzle, swizzle);
1426 cfg.swizzle = pan_translate_swizzle_4(swizzle);
1427
1428 struct pan_blendable_format fmt =
1429 *GENX(pan_blendable_format_from_pipe_format)(rt->format);
1430
1431 if (fmt.internal) {
1432 cfg.internal_format = fmt.internal;
1433 cfg.color_writeback_format = fmt.writeback;
1434 } else {
1435 /* Construct RAW internal/writeback */
1436 unsigned bits = desc->block.bits;
1437
1438 cfg.internal_format = MALI_COLOR_BUFFER_INTERNAL_FORMAT_RAW_VALUE;
1439 cfg.color_writeback_format = pan_sfbd_raw_format(bits);
1440 }
1441
1442 cfg.color_write_enable = !fb->rts[0].discard;
1443 cfg.color_writeback.base =
1444 plane->base + slayout->offset_B +
1445 (array_idx * plane->layout.array_stride_B) +
1446 (surf_idx * slayout->tiled_or_linear.surface_stride_B);
1447 cfg.color_writeback.row_stride = slayout->tiled_or_linear.row_stride_B;
1448
1449 assert(image->props.modifier == DRM_FORMAT_MOD_LINEAR ||
1450 image->props.modifier ==
1451 DRM_FORMAT_MOD_ARM_16X16_BLOCK_U_INTERLEAVED);
1452 cfg.color_block_format =
1453 image->props.modifier ==
1454 DRM_FORMAT_MOD_ARM_16X16_BLOCK_U_INTERLEAVED
1455 ? MALI_BLOCK_FORMAT_TILED_U_INTERLEAVED
1456 : MALI_BLOCK_FORMAT_LINEAR;
1457
1458 if (pan_image_view_has_crc(rt)) {
1459 cfg.crc_buffer.row_stride = slayout->crc.stride_B;
1460 cfg.crc_buffer.base = plane->base + slayout->crc.offset_B;
1461 }
1462 }
1463
1464 if (fb->zs.view.zs) {
1465 const struct pan_image_view *zs = fb->zs.view.zs;
1466 const struct pan_image_plane_ref pref =
1467 pan_image_view_get_zs_plane(zs);
1468 const struct pan_image *image = pref.image;
1469 const struct pan_image_plane *plane = image->planes[pref.plane_idx];
1470 const struct pan_image_slice_layout *slayout =
1471 &plane->layout.slices[zs->first_level];
1472 const unsigned array_idx =
1473 image->props.dim == MALI_TEXTURE_DIMENSION_3D ? 0 : zs->first_layer;
1474 const unsigned surf_idx =
1475 image->props.dim == MALI_TEXTURE_DIMENSION_3D ? zs->first_layer : 0;
1476
1477 cfg.zs_write_enable = !fb->zs.discard.z;
1478 cfg.zs_writeback.base =
1479 plane->base + slayout->offset_B +
1480 (array_idx * plane->layout.array_stride_B) +
1481 (surf_idx * slayout->tiled_or_linear.surface_stride_B);
1482 cfg.zs_writeback.row_stride = slayout->tiled_or_linear.row_stride_B;
1483 assert(image->props.modifier == DRM_FORMAT_MOD_LINEAR ||
1484 image->props.modifier ==
1485 DRM_FORMAT_MOD_ARM_16X16_BLOCK_U_INTERLEAVED);
1486 cfg.zs_block_format =
1487 image->props.modifier ==
1488 DRM_FORMAT_MOD_ARM_16X16_BLOCK_U_INTERLEAVED
1489 ? MALI_BLOCK_FORMAT_TILED_U_INTERLEAVED
1490 : MALI_BLOCK_FORMAT_LINEAR;
1491
1492 cfg.zs_format = translate_zs_format(zs->format);
1493 }
1494
1495 cfg.sample_count = fb->nr_samples;
1496
1497 if (fb->rt_count)
1498 cfg.msaa = mali_sampling_mode(fb->rts[0].view);
1499 }
1500
1501 pan_emit_midgard_tiler(fb, tiler_ctx,
1502 pan_section_ptr(fbd, FRAMEBUFFER, TILER));
1503
1504 /* All weights set to 0, nothing to do here */
1505 pan_section_pack(fbd, FRAMEBUFFER, TILER_WEIGHTS, w)
1506 ;
1507
1508 pan_section_pack(fbd, FRAMEBUFFER, PADDING_1, padding)
1509 ;
1510 pan_section_pack(fbd, FRAMEBUFFER, PADDING_2, padding)
1511 ;
1512 return 0;
1513 }
1514 #endif
1515
1516 #if PAN_ARCH <= 9
1517 void
1518 GENX(pan_emit_fragment_job_payload)(const struct pan_fb_info *fb, uint64_t fbd,
1519 void *out)
1520 {
1521 pan_section_pack(out, FRAGMENT_JOB, PAYLOAD, payload) {
1522 payload.bound_min_x = fb->draw_extent.minx >> MALI_TILE_SHIFT;
1523 payload.bound_min_y = fb->draw_extent.miny >> MALI_TILE_SHIFT;
1524 payload.bound_max_x = fb->draw_extent.maxx >> MALI_TILE_SHIFT;
1525 payload.bound_max_y = fb->draw_extent.maxy >> MALI_TILE_SHIFT;
1526 payload.framebuffer = fbd;
1527
1528 #if PAN_ARCH >= 5
1529 if (fb->tile_map.base) {
1530 payload.has_tile_enable_map = true;
1531 payload.tile_enable_map = fb->tile_map.base;
1532 payload.tile_enable_map_row_stride = fb->tile_map.stride;
1533 }
1534 #endif
1535 }
1536 }
1537 #endif
1538
1539 #if PAN_ARCH >= 6
1540 static uint32_t
1541 pan_calc_bins_pointer_size(uint32_t width, uint32_t height, uint32_t tile_size,
1542 uint32_t hierarchy_mask)
1543 {
1544 const uint32_t bin_ptr_size = PAN_ARCH >= 12 ? 16 : 8;
1545
1546 uint32_t bins_x[PAN_BIN_LEVEL_COUNT];
1547 uint32_t bins_y[PAN_BIN_LEVEL_COUNT];
1548 uint32_t bins[PAN_BIN_LEVEL_COUNT];
1549 uint32_t bins_enabled;
1550
1551 /* On v12+, hierarchy_mask is only used if 4 levels are used at most,
1552 * otherwise it selects another mask (0xAC with a tile_size greater than
1553 * 32x32, 0xAC with 32x32 and lower) */
1554 if ((hierarchy_mask == 0 || util_bitcount(hierarchy_mask) > 4) &&
1555 PAN_ARCH >= 12) {
1556 if (tile_size > 32 * 32)
1557 hierarchy_mask = 0xAC;
1558 else
1559 hierarchy_mask = 0xAA;
1560 }
1561
1562 bins_x[0] = DIV_ROUND_UP(width, 16);
1563 bins_y[0] = DIV_ROUND_UP(height, 16);
1564 bins[0] = bins_x[0] * bins_y[0];
1565
1566 for (uint32_t i = 1; i < ARRAY_SIZE(bins); i++) {
1567 bins_x[i] = DIV_ROUND_UP(bins_x[i - 1], 2);
1568 bins_y[i] = DIV_ROUND_UP(bins_y[i - 1], 2);
1569 bins[i] = bins_x[i] * bins_y[i];
1570 }
1571
1572 bins_enabled = 0;
1573 for (uint32_t i = 0; i < ARRAY_SIZE(bins); i++) {
1574 if ((hierarchy_mask & (1 << i)) != 0)
1575 bins_enabled += bins[i];
1576 }
1577
1578 return DIV_ROUND_UP(bins_enabled, 8) * 8 * bin_ptr_size;
1579 }
1580
1581 unsigned
1582 GENX(pan_select_tiler_hierarchy_mask)(unsigned width, unsigned height,
1583 unsigned max_levels, unsigned tile_size,
1584 unsigned mem_budget)
1585 {
1586 /* On v12+, the hierarchy_mask is deprecated and letting the hardware decide
1587 * is prefered. We attempt to use hierarchy_mask of 0 in case the bins can
1588 * fit in our memory budget.
1589 */
1590 if (PAN_ARCH >= 12 &&
1591 pan_calc_bins_pointer_size(width, height, tile_size, 0) <= mem_budget)
1592 return 0;
1593
1594 uint32_t max_fb_wh = MAX2(width, height);
1595 uint32_t last_hierarchy_bit = util_last_bit(DIV_ROUND_UP(max_fb_wh, 16));
1596 uint32_t hierarchy_mask;
1597
1598 if (max_levels < 8) {
1599 /* spread the bits out somewhat */
1600 static uint32_t default_mask[] = {
1601 0, 0x80, 0x82, 0xa2,
1602 0xaa, 0xea, 0xee, 0xfe
1603 };
1604 hierarchy_mask = default_mask[max_levels];
1605 max_levels = 8; /* the high bit of the mask is always set */
1606 } else {
1607 hierarchy_mask = BITFIELD_MASK(max_levels);
1608 }
1609
1610 /* Always enable the level covering the whole FB, and disable the finest
1611 * levels if we don't have enough to cover everything.
1612 * This is suboptimal for small primitives, since it might force
1613 * primitives to be walked multiple times even if they don't cover the
1614 * the tile being processed. On the other hand, it's hard to guess
1615 * the draw pattern, so it's probably good enough for now.
1616 */
1617 if (last_hierarchy_bit > max_levels)
1618 hierarchy_mask <<= last_hierarchy_bit - max_levels;
1619
1620 /* Disable hierarchies falling under the effective tile size. */
1621 uint32_t disable_hierarchies;
1622 for (disable_hierarchies = 0;
1623 tile_size > (16 * 16) << (disable_hierarchies * 2);
1624 disable_hierarchies++)
1625 ;
1626 hierarchy_mask &= ~BITFIELD_MASK(disable_hierarchies);
1627
1628 /* Disable hierachies that would cause the bins to fit in our budget */
1629 while (disable_hierarchies < PAN_BIN_LEVEL_COUNT) {
1630 uint32_t bins_ptr_size =
1631 pan_calc_bins_pointer_size(width, height, tile_size, hierarchy_mask);
1632
1633 if (bins_ptr_size < mem_budget)
1634 break;
1635
1636 disable_hierarchies++;
1637 hierarchy_mask &= ~BITFIELD_MASK(disable_hierarchies);
1638 }
1639
1640 /* We should fit in our budget at this point */
1641 assert(pan_calc_bins_pointer_size(width, height, tile_size,
1642 hierarchy_mask) <= mem_budget);
1643
1644 /* Before v12, at least one hierarchy level must be enabled. */
1645 assert(hierarchy_mask != 0 || PAN_ARCH >= 12);
1646
1647 return hierarchy_mask;
1648 }
1649 #endif