1 /*
2 * Copyright (C) 2008 VMware, Inc.
3 * Copyright (C) 2012 Rob Clark <[email protected]>
4 * Copyright (C) 2014-2017 Broadcom
5 * Copyright (C) 2018-2019 Alyssa Rosenzweig
6 * Copyright (C) 2019 Collabora, Ltd.
7 * Copyright (C) 2023 Amazon.com, Inc. or its affiliates
8 *
9 * Permission is hereby granted, free of charge, to any person obtaining a
10 * copy of this software and associated documentation files (the "Software"),
11 * to deal in the Software without restriction, including without limitation
12 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
13 * and/or sell copies of the Software, and to permit persons to whom the
14 * Software is furnished to do so, subject to the following conditions:
15 *
16 * The above copyright notice and this permission notice (including the next
17 * paragraph) shall be included in all copies or substantial portions of the
18 * Software.
19 *
20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
23 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
26 * SOFTWARE.
27 *
28 * Authors (Collabora):
29 * Tomeu Vizoso <[email protected]>
30 * Alyssa Rosenzweig <[email protected]>
31 *
32 */
33
34 #include <fcntl.h>
35 #include <xf86drm.h>
36 #include "drm-uapi/drm_fourcc.h"
37
38 #include "frontend/winsys_handle.h"
39 #include "util/format/u_format.h"
40 #include "util/u_debug_image.h"
41 #include "util/u_drm.h"
42 #include "util/u_gen_mipmap.h"
43 #include "util/u_memory.h"
44 #include "util/u_resource.h"
45 #include "util/u_surface.h"
46 #include "util/u_transfer.h"
47 #include "util/u_transfer_helper.h"
48
49 #include "decode.h"
50 #include "pan_bo.h"
51 #include "pan_context.h"
52 #include "pan_resource.h"
53 #include "pan_screen.h"
54 #include "pan_tiling.h"
55 #include "pan_util.h"
56
57 static void
panfrost_clear_depth_stencil(struct pipe_context * pipe,struct pipe_surface * dst,unsigned clear_flags,double depth,unsigned stencil,unsigned dstx,unsigned dsty,unsigned width,unsigned height,bool render_condition_enabled)58 panfrost_clear_depth_stencil(struct pipe_context *pipe,
59 struct pipe_surface *dst, unsigned clear_flags,
60 double depth, unsigned stencil, unsigned dstx,
61 unsigned dsty, unsigned width, unsigned height,
62 bool render_condition_enabled)
63 {
64 struct panfrost_context *ctx = pan_context(pipe);
65
66 if (render_condition_enabled && !panfrost_render_condition_check(ctx))
67 return;
68
69 panfrost_blitter_save(
70 ctx, render_condition_enabled ? PAN_RENDER_COND : PAN_RENDER_BASE);
71 util_blitter_clear_depth_stencil(ctx->blitter, dst, clear_flags, depth,
72 stencil, dstx, dsty, width, height);
73 }
74
75 static void
panfrost_clear_render_target(struct pipe_context * pipe,struct pipe_surface * dst,const union pipe_color_union * color,unsigned dstx,unsigned dsty,unsigned width,unsigned height,bool render_condition_enabled)76 panfrost_clear_render_target(struct pipe_context *pipe,
77 struct pipe_surface *dst,
78 const union pipe_color_union *color, unsigned dstx,
79 unsigned dsty, unsigned width, unsigned height,
80 bool render_condition_enabled)
81 {
82 struct panfrost_context *ctx = pan_context(pipe);
83
84 if (render_condition_enabled && !panfrost_render_condition_check(ctx))
85 return;
86
87 panfrost_blitter_save(
88 ctx, render_condition_enabled ? PAN_RENDER_COND : PAN_RENDER_BASE);
89 util_blitter_clear_render_target(ctx->blitter, dst, color, dstx, dsty, width,
90 height);
91 }
92
93 static struct pipe_resource *
panfrost_resource_from_handle(struct pipe_screen * pscreen,const struct pipe_resource * templat,struct winsys_handle * whandle,unsigned usage)94 panfrost_resource_from_handle(struct pipe_screen *pscreen,
95 const struct pipe_resource *templat,
96 struct winsys_handle *whandle, unsigned usage)
97 {
98 struct panfrost_device *dev = pan_device(pscreen);
99 struct panfrost_resource *rsc;
100 struct pipe_resource *prsc;
101
102 assert(whandle->type == WINSYS_HANDLE_TYPE_FD);
103
104 rsc = CALLOC_STRUCT(panfrost_resource);
105 if (!rsc)
106 return NULL;
107
108 prsc = &rsc->base;
109
110 *prsc = *templat;
111
112 pipe_reference_init(&prsc->reference, 1);
113 prsc->screen = pscreen;
114
115 uint64_t mod = whandle->modifier == DRM_FORMAT_MOD_INVALID
116 ? DRM_FORMAT_MOD_LINEAR
117 : whandle->modifier;
118 enum mali_texture_dimension dim =
119 panfrost_translate_texture_dimension(templat->target);
120 struct pan_image_explicit_layout explicit_layout = {
121 .offset = whandle->offset,
122 .row_stride =
123 panfrost_from_legacy_stride(whandle->stride, templat->format, mod),
124 };
125
126 rsc->image.layout = (struct pan_image_layout){
127 .modifier = mod,
128 .format = templat->format,
129 .dim = dim,
130 .width = prsc->width0,
131 .height = prsc->height0,
132 .depth = prsc->depth0,
133 .array_size = prsc->array_size,
134 .nr_samples = MAX2(prsc->nr_samples, 1),
135 .nr_slices = 1,
136 };
137
138 bool valid =
139 pan_image_layout_init(dev->arch, &rsc->image.layout, &explicit_layout);
140
141 if (!valid) {
142 FREE(rsc);
143 return NULL;
144 }
145
146 rsc->bo = panfrost_bo_import(dev, whandle->handle);
147 /* Sometimes an import can fail e.g. on an invalid buffer fd, out of
148 * memory space to mmap it etc.
149 */
150 if (!rsc->bo) {
151 FREE(rsc);
152 return NULL;
153 }
154
155 rsc->image.data.base = rsc->bo->ptr.gpu;
156 rsc->modifier_constant = true;
157
158 BITSET_SET(rsc->valid.data, 0);
159 panfrost_resource_set_damage_region(pscreen, &rsc->base, 0, NULL);
160
161 if (dev->ro) {
162 rsc->scanout =
163 renderonly_create_gpu_import_for_resource(prsc, dev->ro, NULL);
164 /* failure is expected in some cases.. */
165 }
166
167 return prsc;
168 }
169
170 static bool
panfrost_resource_get_handle(struct pipe_screen * pscreen,struct pipe_context * ctx,struct pipe_resource * pt,struct winsys_handle * handle,unsigned usage)171 panfrost_resource_get_handle(struct pipe_screen *pscreen,
172 struct pipe_context *ctx, struct pipe_resource *pt,
173 struct winsys_handle *handle, unsigned usage)
174 {
175 struct panfrost_device *dev = pan_device(pscreen);
176 struct panfrost_resource *rsrc;
177 struct renderonly_scanout *scanout;
178 struct pipe_resource *plane_res =
179 util_resource_at_index(pt, handle->plane);
180
181 if (!plane_res)
182 return false;
183
184 rsrc = pan_resource(plane_res);
185 scanout = rsrc->scanout;
186
187 handle->modifier = rsrc->image.layout.modifier;
188 rsrc->modifier_constant = true;
189
190 if (handle->type == WINSYS_HANDLE_TYPE_KMS && dev->ro) {
191 return renderonly_get_handle(scanout, handle);
192 } else if (handle->type == WINSYS_HANDLE_TYPE_KMS) {
193 handle->handle = panfrost_bo_handle(rsrc->bo);
194 } else if (handle->type == WINSYS_HANDLE_TYPE_FD) {
195 int fd = panfrost_bo_export(rsrc->bo);
196
197 if (fd < 0)
198 return false;
199
200 handle->handle = fd;
201 } else {
202 /* Other handle types not supported */
203 return false;
204 }
205
206 handle->stride = panfrost_get_legacy_stride(&rsrc->image.layout, 0);
207 handle->offset = rsrc->image.layout.slices[0].offset;
208 return true;
209 }
210
211 static bool
panfrost_resource_get_param(struct pipe_screen * pscreen,struct pipe_context * pctx,struct pipe_resource * prsc,unsigned plane,unsigned layer,unsigned level,enum pipe_resource_param param,unsigned usage,uint64_t * value)212 panfrost_resource_get_param(struct pipe_screen *pscreen,
213 struct pipe_context *pctx,
214 struct pipe_resource *prsc, unsigned plane,
215 unsigned layer, unsigned level,
216 enum pipe_resource_param param, unsigned usage,
217 uint64_t *value)
218 {
219 struct pipe_resource *plane_res = util_resource_at_index(prsc, plane);
220 struct panfrost_resource *rsrc = pan_resource(plane_res);
221
222 switch (param) {
223 case PIPE_RESOURCE_PARAM_STRIDE:
224 *value = panfrost_get_legacy_stride(&rsrc->image.layout, level);
225 return true;
226 case PIPE_RESOURCE_PARAM_OFFSET:
227 *value = rsrc->image.layout.slices[level].offset;
228 return true;
229 case PIPE_RESOURCE_PARAM_MODIFIER:
230 *value = rsrc->image.layout.modifier;
231 return true;
232 case PIPE_RESOURCE_PARAM_NPLANES:
233 *value = util_resource_num(prsc);
234 return true;
235 default:
236 return false;
237 }
238 }
239
240 static void
panfrost_flush_resource(struct pipe_context * pctx,struct pipe_resource * prsc)241 panfrost_flush_resource(struct pipe_context *pctx, struct pipe_resource *prsc)
242 {
243 /* TODO */
244 }
245
246 static struct pipe_surface *
panfrost_create_surface(struct pipe_context * pipe,struct pipe_resource * pt,const struct pipe_surface * surf_tmpl)247 panfrost_create_surface(struct pipe_context *pipe, struct pipe_resource *pt,
248 const struct pipe_surface *surf_tmpl)
249 {
250 struct pipe_surface *ps = NULL;
251
252 ps = CALLOC_STRUCT(pipe_surface);
253
254 if (ps) {
255 pipe_reference_init(&ps->reference, 1);
256 pipe_resource_reference(&ps->texture, pt);
257 ps->context = pipe;
258 ps->format = surf_tmpl->format;
259
260 if (pt->target != PIPE_BUFFER) {
261 assert(surf_tmpl->u.tex.level <= pt->last_level);
262 ps->width = u_minify(pt->width0, surf_tmpl->u.tex.level);
263 ps->height = u_minify(pt->height0, surf_tmpl->u.tex.level);
264 ps->nr_samples = surf_tmpl->nr_samples;
265 ps->u.tex.level = surf_tmpl->u.tex.level;
266 ps->u.tex.first_layer = surf_tmpl->u.tex.first_layer;
267 ps->u.tex.last_layer = surf_tmpl->u.tex.last_layer;
268 } else {
269 /* setting width as number of elements should get us correct
270 * renderbuffer width */
271 ps->width =
272 surf_tmpl->u.buf.last_element - surf_tmpl->u.buf.first_element + 1;
273 ps->height = pt->height0;
274 ps->u.buf.first_element = surf_tmpl->u.buf.first_element;
275 ps->u.buf.last_element = surf_tmpl->u.buf.last_element;
276 assert(ps->u.buf.first_element <= ps->u.buf.last_element);
277 assert(ps->u.buf.last_element < ps->width);
278 }
279 }
280
281 return ps;
282 }
283
284 static void
panfrost_surface_destroy(struct pipe_context * pipe,struct pipe_surface * surf)285 panfrost_surface_destroy(struct pipe_context *pipe, struct pipe_surface *surf)
286 {
287 assert(surf->texture);
288 pipe_resource_reference(&surf->texture, NULL);
289 free(surf);
290 }
291
292 static inline bool
panfrost_is_2d(const struct panfrost_resource * pres)293 panfrost_is_2d(const struct panfrost_resource *pres)
294 {
295 return (pres->base.target == PIPE_TEXTURE_2D) ||
296 (pres->base.target == PIPE_TEXTURE_RECT);
297 }
298
299 /* Based on the usage, determine if it makes sense to use u-inteleaved tiling.
300 * We only have routines to tile 2D textures of sane bpps. On the hardware
301 * level, not all usages are valid for tiling. Finally, if the app is hinting
302 * that the contents frequently change, tiling will be a loss.
303 *
304 * On platforms where it is supported, AFBC is even better. */
305
306 static bool
panfrost_should_afbc(struct panfrost_device * dev,const struct panfrost_resource * pres,enum pipe_format fmt)307 panfrost_should_afbc(struct panfrost_device *dev,
308 const struct panfrost_resource *pres, enum pipe_format fmt)
309 {
310 /* AFBC resources may be rendered to, textured from, or shared across
311 * processes, but may not be used as e.g buffers */
312 const unsigned valid_binding =
313 PIPE_BIND_DEPTH_STENCIL | PIPE_BIND_RENDER_TARGET | PIPE_BIND_BLENDABLE |
314 PIPE_BIND_SAMPLER_VIEW | PIPE_BIND_DISPLAY_TARGET | PIPE_BIND_SCANOUT |
315 PIPE_BIND_SHARED;
316
317 if (pres->base.bind & ~valid_binding)
318 return false;
319
320 /* AFBC support is optional */
321 if (!dev->has_afbc)
322 return false;
323
324 /* AFBC<-->staging is expensive */
325 if (pres->base.usage == PIPE_USAGE_STREAM)
326 return false;
327
328 /* If constant (non-data-dependent) format is requested, don't AFBC: */
329 if (pres->base.bind & PIPE_BIND_CONST_BW)
330 return false;
331
332 /* Only a small selection of formats are AFBC'able */
333 if (!panfrost_format_supports_afbc(dev->arch, fmt))
334 return false;
335
336 /* AFBC does not support layered (GLES3 style) multisampling. Use
337 * EXT_multisampled_render_to_texture instead */
338 if (pres->base.nr_samples > 1)
339 return false;
340
341 switch (pres->base.target) {
342 case PIPE_TEXTURE_2D:
343 case PIPE_TEXTURE_RECT:
344 case PIPE_TEXTURE_2D_ARRAY:
345 case PIPE_TEXTURE_CUBE:
346 case PIPE_TEXTURE_CUBE_ARRAY:
347 break;
348
349 case PIPE_TEXTURE_3D:
350 /* 3D AFBC is only supported on Bifrost v7+. It's supposed to
351 * be supported on Midgard but it doesn't seem to work */
352 if (dev->arch != 7)
353 return false;
354
355 break;
356
357 default:
358 return false;
359 }
360
361 /* For one tile, AFBC is a loss compared to u-interleaved */
362 if (pres->base.width0 <= 16 && pres->base.height0 <= 16)
363 return false;
364
365 /* Otherwise, we'd prefer AFBC as it is dramatically more efficient
366 * than linear or usually even u-interleaved */
367 return true;
368 }
369
370 /*
371 * For a resource we want to use AFBC with, should we use AFBC with tiled
372 * headers? On GPUs that support it, this is believed to be beneficial for
373 * images that are at least 128x128.
374 */
375 static bool
panfrost_should_tile_afbc(const struct panfrost_device * dev,const struct panfrost_resource * pres)376 panfrost_should_tile_afbc(const struct panfrost_device *dev,
377 const struct panfrost_resource *pres)
378 {
379 return panfrost_afbc_can_tile(dev->arch) && pres->base.width0 >= 128 &&
380 pres->base.height0 >= 128 && !(dev->debug & PAN_DBG_FORCE_PACK);
381 }
382
383 bool
panfrost_should_pack_afbc(struct panfrost_device * dev,const struct panfrost_resource * prsrc)384 panfrost_should_pack_afbc(struct panfrost_device *dev,
385 const struct panfrost_resource *prsrc)
386 {
387 const unsigned valid_binding = PIPE_BIND_DEPTH_STENCIL |
388 PIPE_BIND_RENDER_TARGET |
389 PIPE_BIND_SAMPLER_VIEW;
390
391 return panfrost_afbc_can_pack(prsrc->base.format) && panfrost_is_2d(prsrc) &&
392 drm_is_afbc(prsrc->image.layout.modifier) &&
393 (prsrc->image.layout.modifier & AFBC_FORMAT_MOD_SPARSE) &&
394 (prsrc->base.bind & ~valid_binding) == 0 &&
395 !prsrc->modifier_constant && prsrc->base.width0 >= 32 &&
396 prsrc->base.height0 >= 32;
397 }
398
399 static bool
panfrost_should_tile(struct panfrost_device * dev,const struct panfrost_resource * pres,enum pipe_format fmt)400 panfrost_should_tile(struct panfrost_device *dev,
401 const struct panfrost_resource *pres, enum pipe_format fmt)
402 {
403 const unsigned valid_binding =
404 PIPE_BIND_DEPTH_STENCIL | PIPE_BIND_RENDER_TARGET | PIPE_BIND_BLENDABLE |
405 PIPE_BIND_SAMPLER_VIEW | PIPE_BIND_DISPLAY_TARGET | PIPE_BIND_SCANOUT |
406 PIPE_BIND_SHARED;
407
408 /* The purpose of tiling is improving locality in both X- and
409 * Y-directions. If there is only a single pixel in either direction,
410 * tiling does not make sense; using a linear layout instead is optimal
411 * for both memory usage and performance.
412 */
413 if (MIN2(pres->base.width0, pres->base.height0) < 2)
414 return false;
415
416 bool can_tile = (pres->base.target != PIPE_BUFFER) &&
417 ((pres->base.bind & ~valid_binding) == 0);
418
419 return can_tile && (pres->base.usage != PIPE_USAGE_STREAM);
420 }
421
422 static bool
panfrost_should_afrc(struct panfrost_device * dev,const struct panfrost_resource * pres,enum pipe_format fmt)423 panfrost_should_afrc(struct panfrost_device *dev,
424 const struct panfrost_resource *pres, enum pipe_format fmt)
425 {
426 const unsigned valid_binding = PIPE_BIND_RENDER_TARGET |
427 PIPE_BIND_BLENDABLE | PIPE_BIND_SAMPLER_VIEW |
428 PIPE_BIND_DISPLAY_TARGET | PIPE_BIND_SHARED;
429
430 if (pres->base.bind & ~valid_binding)
431 return false;
432
433 /* AFRC support is optional */
434 if (!dev->has_afrc)
435 return false;
436
437 /* AFRC<-->staging is expensive */
438 if (pres->base.usage == PIPE_USAGE_STREAM)
439 return false;
440
441 /* Only a small selection of formats are AFRC'able */
442 if (!panfrost_format_supports_afrc(fmt))
443 return false;
444
445 /* AFRC does not support layered (GLES3 style) multisampling. Use
446 * EXT_multisampled_render_to_texture instead */
447 if (pres->base.nr_samples > 1)
448 return false;
449
450 switch (pres->base.target) {
451 case PIPE_TEXTURE_2D:
452 case PIPE_TEXTURE_RECT:
453 case PIPE_TEXTURE_2D_ARRAY:
454 case PIPE_TEXTURE_CUBE:
455 case PIPE_TEXTURE_CUBE_ARRAY:
456 case PIPE_TEXTURE_3D:
457 break;
458
459 default:
460 return false;
461 }
462
463 return true;
464 }
465
466 static uint64_t
panfrost_best_modifier(struct pipe_screen * pscreen,const struct panfrost_resource * pres,enum pipe_format fmt)467 panfrost_best_modifier(struct pipe_screen *pscreen,
468 const struct panfrost_resource *pres,
469 enum pipe_format fmt)
470 {
471 struct panfrost_screen *screen = pan_screen(pscreen);
472 struct panfrost_device *dev = pan_device(pscreen);
473
474 /* Force linear textures when debugging tiling/compression */
475 if (unlikely(dev->debug & PAN_DBG_LINEAR))
476 return DRM_FORMAT_MOD_LINEAR;
477
478 int afrc_rate = screen->force_afrc_rate;
479 if (afrc_rate < 0)
480 afrc_rate = pres->base.compression_rate;
481 if (afrc_rate > PIPE_COMPRESSION_FIXED_RATE_NONE &&
482 panfrost_should_afrc(dev, pres, fmt)) {
483 /* It's not really possible to decide on a global AFRC-rate,
484 * because the set of valid AFRC rates varies from format to
485 * format. So instead, treat this as a minimum rate, and search
486 * for the next valid one.
487 */
488 for (int i = afrc_rate; i < 12; ++i) {
489 if (panfrost_afrc_get_modifiers(fmt, i, 0, NULL)) {
490 afrc_rate = i;
491 break;
492 }
493 }
494 }
495
496 if (afrc_rate != PIPE_COMPRESSION_FIXED_RATE_NONE &&
497 panfrost_should_afrc(dev, pres, fmt)) {
498 uint64_t mod;
499 unsigned num_mods = 0;
500
501 STATIC_ASSERT(PIPE_COMPRESSION_FIXED_RATE_DEFAULT == PAN_AFRC_RATE_DEFAULT);
502 num_mods = panfrost_afrc_get_modifiers(fmt, afrc_rate, 1, &mod);
503 if (num_mods > 0) {
504 return mod;
505 }
506 }
507
508 if (panfrost_should_afbc(dev, pres, fmt)) {
509 uint64_t afbc = AFBC_FORMAT_MOD_BLOCK_SIZE_16x16 | AFBC_FORMAT_MOD_SPARSE;
510
511 if (panfrost_afbc_can_ytr(pres->base.format))
512 afbc |= AFBC_FORMAT_MOD_YTR;
513
514 if (panfrost_should_tile_afbc(dev, pres))
515 afbc |= AFBC_FORMAT_MOD_TILED | AFBC_FORMAT_MOD_SC;
516
517 return DRM_FORMAT_MOD_ARM_AFBC(afbc);
518 } else if (panfrost_should_tile(dev, pres, fmt))
519 return DRM_FORMAT_MOD_ARM_16X16_BLOCK_U_INTERLEAVED;
520 else
521 return DRM_FORMAT_MOD_LINEAR;
522 }
523
524 static bool
panfrost_should_checksum(const struct panfrost_device * dev,const struct panfrost_resource * pres)525 panfrost_should_checksum(const struct panfrost_device *dev,
526 const struct panfrost_resource *pres)
527 {
528 /* Checksumming is disabled by default due to fundamental unsoundness */
529 if (!(dev->debug & PAN_DBG_CRC))
530 return false;
531
532 /* When checksumming is enabled, the tile data must fit in the
533 * size of the writeback buffer, so don't checksum formats
534 * that use too much space. */
535
536 unsigned bytes_per_pixel_max = (dev->arch == 6) ? 6 : 4;
537
538 unsigned bytes_per_pixel = MAX2(pres->base.nr_samples, 1) *
539 util_format_get_blocksize(pres->base.format);
540
541 return pres->base.bind & PIPE_BIND_RENDER_TARGET && panfrost_is_2d(pres) &&
542 bytes_per_pixel <= bytes_per_pixel_max && pres->base.last_level == 0;
543 }
544
545 static void
panfrost_resource_setup(struct pipe_screen * screen,struct panfrost_resource * pres,uint64_t modifier,enum pipe_format fmt)546 panfrost_resource_setup(struct pipe_screen *screen,
547 struct panfrost_resource *pres, uint64_t modifier,
548 enum pipe_format fmt)
549 {
550 struct panfrost_device *dev = pan_device(screen);
551 uint64_t chosen_mod = modifier != DRM_FORMAT_MOD_INVALID
552 ? modifier
553 : panfrost_best_modifier(screen, pres, fmt);
554 enum mali_texture_dimension dim =
555 panfrost_translate_texture_dimension(pres->base.target);
556
557 /* We can only switch tiled->linear if the resource isn't already
558 * linear and if we control the modifier */
559 pres->modifier_constant = !(chosen_mod != DRM_FORMAT_MOD_LINEAR &&
560 modifier == DRM_FORMAT_MOD_INVALID);
561
562 /* Z32_S8X24 variants are actually stored in 2 planes (one per
563 * component), we have to adjust the format on the first plane.
564 */
565 if (fmt == PIPE_FORMAT_Z32_FLOAT_S8X24_UINT)
566 fmt = PIPE_FORMAT_Z32_FLOAT;
567
568 pres->image.layout = (struct pan_image_layout){
569 .modifier = chosen_mod,
570 .format = fmt,
571 .dim = dim,
572 .width = pres->base.width0,
573 .height = pres->base.height0,
574 .depth = pres->base.depth0,
575 .array_size = pres->base.array_size,
576 .nr_samples = MAX2(pres->base.nr_samples, 1),
577 .nr_slices = pres->base.last_level + 1,
578 .crc = panfrost_should_checksum(dev, pres),
579 };
580
581 /* Update the compression rate with the correct value as we
582 * want the real bitrate and not DEFAULT */
583 pres->base.compression_rate = panfrost_afrc_get_rate(fmt, chosen_mod);
584
585 ASSERTED bool valid =
586 pan_image_layout_init(dev->arch, &pres->image.layout, NULL);
587 assert(valid);
588 }
589
590 static void
panfrost_resource_init_afbc_headers(struct panfrost_resource * pres)591 panfrost_resource_init_afbc_headers(struct panfrost_resource *pres)
592 {
593 panfrost_bo_mmap(pres->bo);
594
595 unsigned nr_samples = MAX2(pres->base.nr_samples, 1);
596
597 for (unsigned i = 0; i < pres->base.array_size; ++i) {
598 for (unsigned l = 0; l <= pres->base.last_level; ++l) {
599 struct pan_image_slice_layout *slice = &pres->image.layout.slices[l];
600
601 for (unsigned s = 0; s < nr_samples; ++s) {
602 void *ptr = pres->bo->ptr.cpu +
603 (i * pres->image.layout.array_stride) + slice->offset +
604 (s * slice->afbc.surface_stride);
605
606 /* Zero-ed AFBC headers seem to encode a plain
607 * black. Let's use this pattern to keep the
608 * initialization simple.
609 */
610 memset(ptr, 0, slice->afbc.header_size);
611 }
612 }
613 }
614 }
615
616 void
panfrost_resource_set_damage_region(struct pipe_screen * screen,struct pipe_resource * res,unsigned int nrects,const struct pipe_box * rects)617 panfrost_resource_set_damage_region(struct pipe_screen *screen,
618 struct pipe_resource *res,
619 unsigned int nrects,
620 const struct pipe_box *rects)
621 {
622 struct panfrost_device *dev = pan_device(screen);
623 struct panfrost_resource *pres = pan_resource(res);
624 struct pipe_scissor_state *damage_extent = &pres->damage.extent;
625 unsigned int i;
626
627 /* Partial updates are implemented with a tile enable map only on v5.
628 * Later architectures have a more efficient method of implementing
629 * partial updates (frame shaders), while earlier architectures lack
630 * tile enable maps altogether.
631 */
632 if (dev->arch == 5 && nrects > 1) {
633 if (!pres->damage.tile_map.data) {
634 pres->damage.tile_map.stride =
635 ALIGN_POT(DIV_ROUND_UP(res->width0, 32 * 8), 64);
636 pres->damage.tile_map.size =
637 pres->damage.tile_map.stride * DIV_ROUND_UP(res->height0, 32);
638 pres->damage.tile_map.data = malloc(pres->damage.tile_map.size);
639 }
640
641 memset(pres->damage.tile_map.data, 0, pres->damage.tile_map.size);
642 pres->damage.tile_map.enable = true;
643 } else {
644 pres->damage.tile_map.enable = false;
645 }
646
647 /* Track the damage extent: the quad including all damage regions. Will
648 * be used restrict the rendering area */
649
650 damage_extent->minx = 0xffff;
651 damage_extent->miny = 0xffff;
652
653 unsigned enable_count = 0;
654
655 for (i = 0; i < nrects; i++) {
656 int x = rects[i].x, w = rects[i].width, h = rects[i].height;
657 int y = res->height0 - (rects[i].y + h);
658
659 damage_extent->minx = MIN2(damage_extent->minx, x);
660 damage_extent->miny = MIN2(damage_extent->miny, y);
661 damage_extent->maxx = MAX2(damage_extent->maxx, MIN2(x + w, res->width0));
662 damage_extent->maxy =
663 MAX2(damage_extent->maxy, MIN2(y + h, res->height0));
664
665 if (!pres->damage.tile_map.enable)
666 continue;
667
668 unsigned t_x_start = x / 32;
669 unsigned t_x_end = (x + w - 1) / 32;
670 unsigned t_y_start = y / 32;
671 unsigned t_y_end = (y + h - 1) / 32;
672
673 for (unsigned t_y = t_y_start; t_y <= t_y_end; t_y++) {
674 for (unsigned t_x = t_x_start; t_x <= t_x_end; t_x++) {
675 unsigned b = (t_y * pres->damage.tile_map.stride * 8) + t_x;
676
677 if (BITSET_TEST(pres->damage.tile_map.data, b))
678 continue;
679
680 BITSET_SET(pres->damage.tile_map.data, b);
681 enable_count++;
682 }
683 }
684 }
685
686 if (nrects == 0) {
687 damage_extent->minx = 0;
688 damage_extent->miny = 0;
689 damage_extent->maxx = res->width0;
690 damage_extent->maxy = res->height0;
691 }
692
693 if (pres->damage.tile_map.enable) {
694 unsigned t_x_start = damage_extent->minx / 32;
695 unsigned t_x_end = damage_extent->maxx / 32;
696 unsigned t_y_start = damage_extent->miny / 32;
697 unsigned t_y_end = damage_extent->maxy / 32;
698 unsigned tile_count =
699 (t_x_end - t_x_start + 1) * (t_y_end - t_y_start + 1);
700
701 /* Don't bother passing a tile-enable-map if the amount of
702 * tiles to reload is to close to the total number of tiles.
703 */
704 if (tile_count - enable_count < 10)
705 pres->damage.tile_map.enable = false;
706 }
707 }
708
709 struct pipe_resource *
panfrost_resource_create_with_modifier(struct pipe_screen * screen,const struct pipe_resource * template,uint64_t modifier)710 panfrost_resource_create_with_modifier(struct pipe_screen *screen,
711 const struct pipe_resource *template,
712 uint64_t modifier)
713 {
714 struct panfrost_device *dev = pan_device(screen);
715
716 struct panfrost_resource *so = CALLOC_STRUCT(panfrost_resource);
717
718 if (!so)
719 return NULL;
720
721 so->base = *template;
722 so->base.screen = screen;
723
724 pipe_reference_init(&so->base.reference, 1);
725
726 util_range_init(&so->valid_buffer_range);
727
728 if (template->bind & PAN_BIND_SHARED_MASK) {
729 /* For compatibility with older consumers that may not be
730 * modifiers aware, treat INVALID as LINEAR for shared
731 * resources.
732 */
733 if (modifier == DRM_FORMAT_MOD_INVALID)
734 modifier = DRM_FORMAT_MOD_LINEAR;
735
736 /* At any rate, we can't change the modifier later for shared
737 * resources, since we have no way to propagate the modifier
738 * change.
739 */
740 so->modifier_constant = true;
741 }
742
743 panfrost_resource_setup(screen, so, modifier, template->format);
744
745 /* Guess a label based on the bind */
746 unsigned bind = template->bind;
747 const char *label = (bind & PIPE_BIND_INDEX_BUFFER) ? "Index buffer"
748 : (bind & PIPE_BIND_SCANOUT) ? "Scanout"
749 : (bind & PIPE_BIND_DISPLAY_TARGET) ? "Display target"
750 : (bind & PIPE_BIND_SHARED) ? "Shared resource"
751 : (bind & PIPE_BIND_RENDER_TARGET) ? "Render target"
752 : (bind & PIPE_BIND_DEPTH_STENCIL)
753 ? "Depth/stencil buffer"
754 : (bind & PIPE_BIND_SAMPLER_VIEW) ? "Texture"
755 : (bind & PIPE_BIND_VERTEX_BUFFER) ? "Vertex buffer"
756 : (bind & PIPE_BIND_CONSTANT_BUFFER) ? "Constant buffer"
757 : (bind & PIPE_BIND_GLOBAL) ? "Global memory"
758 : (bind & PIPE_BIND_SHADER_BUFFER) ? "Shader buffer"
759 : (bind & PIPE_BIND_SHADER_IMAGE) ? "Shader image"
760 : "Other resource";
761
762 if (dev->ro && (template->bind & PIPE_BIND_SCANOUT)) {
763 struct winsys_handle handle;
764 struct pan_block_size blocksize =
765 panfrost_block_size(modifier, template->format);
766
767 /* Block-based texture formats are only used for texture
768 * compression (not framebuffer compression!), which doesn't
769 * make sense to share across processes.
770 */
771 assert(util_format_get_blockwidth(template->format) == 1);
772 assert(util_format_get_blockheight(template->format) == 1);
773
774 /* Present a resource with similar dimensions that, if allocated
775 * as a linear image, is big enough to fit the resource in the
776 * actual layout. For linear images, this is a no-op. For 16x16
777 * tiling, this aligns the dimensions to 16x16.
778 *
779 * For AFBC, this aligns the width to the superblock width (as
780 * expected) and adds extra rows to account for the header. This
781 * is a bit of a lie, but it's the best we can do with dumb
782 * buffers, which are extremely not meant for AFBC. And yet this
783 * has to work anyway...
784 *
785 * Moral of the story: if you're reading this comment, that
786 * means you're working on WSI and so it's already too late for
787 * you. I'm sorry.
788 */
789 unsigned width = ALIGN_POT(template->width0, blocksize.width);
790 unsigned stride = ALIGN_POT(template->width0, blocksize.width) *
791 util_format_get_blocksize(template->format);
792 unsigned size = so->image.layout.data_size;
793 unsigned effective_rows = DIV_ROUND_UP(size, stride);
794
795 struct pipe_resource scanout_tmpl = {
796 .target = so->base.target,
797 .format = template->format,
798 .width0 = width,
799 .height0 = effective_rows,
800 .depth0 = 1,
801 .array_size = 1,
802 };
803
804 so->scanout =
805 renderonly_scanout_for_resource(&scanout_tmpl, dev->ro, &handle);
806
807 if (!so->scanout) {
808 fprintf(stderr, "Failed to create scanout resource\n");
809 FREE(so);
810 return NULL;
811 }
812 assert(handle.type == WINSYS_HANDLE_TYPE_FD);
813 so->bo = panfrost_bo_import(dev, handle.handle);
814 close(handle.handle);
815
816 if (!so->bo) {
817 FREE(so);
818 return NULL;
819 }
820
821 so->image.data.base = so->bo->ptr.gpu;
822 } else {
823 /* We create a BO immediately but don't bother mapping, since we don't
824 * care to map e.g. FBOs which the CPU probably won't touch */
825 uint32_t flags = PAN_BO_DELAY_MMAP;
826
827 /* If the resource is never exported, we can make the BO private. */
828 if (template->bind & PIPE_BIND_SHARED)
829 flags |= PAN_BO_SHAREABLE;
830
831 so->bo =
832 panfrost_bo_create(dev, so->image.layout.data_size, flags, label);
833
834 if (!so->bo) {
835 FREE(so);
836 return NULL;
837 }
838
839 so->image.data.base = so->bo->ptr.gpu;
840
841 so->constant_stencil = true;
842 }
843
844 if (drm_is_afbc(so->image.layout.modifier))
845 panfrost_resource_init_afbc_headers(so);
846
847 panfrost_resource_set_damage_region(screen, &so->base, 0, NULL);
848
849 if (template->bind & PIPE_BIND_INDEX_BUFFER)
850 so->index_cache = CALLOC_STRUCT(panfrost_minmax_cache);
851
852 return (struct pipe_resource *)so;
853 }
854
855 /* Default is to create a resource as don't care */
856
857 static struct pipe_resource *
panfrost_resource_create(struct pipe_screen * screen,const struct pipe_resource * template)858 panfrost_resource_create(struct pipe_screen *screen,
859 const struct pipe_resource *template)
860 {
861 return panfrost_resource_create_with_modifier(screen, template,
862 DRM_FORMAT_MOD_INVALID);
863 }
864
865 /* If no modifier is specified, we'll choose. Otherwise, the order of
866 * preference is compressed, tiled, linear. */
867
868 static struct pipe_resource *
panfrost_resource_create_with_modifiers(struct pipe_screen * screen,const struct pipe_resource * template,const uint64_t * modifiers,int count)869 panfrost_resource_create_with_modifiers(struct pipe_screen *screen,
870 const struct pipe_resource *template,
871 const uint64_t *modifiers, int count)
872 {
873 for (unsigned i = 0; i < PAN_MODIFIER_COUNT; ++i) {
874 if (drm_find_modifier(pan_best_modifiers[i], modifiers, count)) {
875 return panfrost_resource_create_with_modifier(screen, template,
876 pan_best_modifiers[i]);
877 }
878 }
879
880 /* If we didn't find one, app specified invalid */
881 assert(count == 1 && modifiers[0] == DRM_FORMAT_MOD_INVALID);
882 return panfrost_resource_create(screen, template);
883 }
884
885 static void
panfrost_resource_destroy(struct pipe_screen * screen,struct pipe_resource * pt)886 panfrost_resource_destroy(struct pipe_screen *screen, struct pipe_resource *pt)
887 {
888 struct panfrost_device *dev = pan_device(screen);
889 struct panfrost_resource *rsrc = (struct panfrost_resource *)pt;
890
891 if (rsrc->scanout)
892 renderonly_scanout_destroy(rsrc->scanout, dev->ro);
893
894 if (rsrc->bo)
895 panfrost_bo_unreference(rsrc->bo);
896
897 free(rsrc->index_cache);
898 free(rsrc->damage.tile_map.data);
899
900 util_range_destroy(&rsrc->valid_buffer_range);
901 free(rsrc);
902 }
903
904 /* Most of the time we can do CPU-side transfers, but sometimes we need to use
905 * the 3D pipe for this. Let's wrap u_blitter to blit to/from staging textures.
906 * Code adapted from freedreno */
907
908 static struct panfrost_resource *
pan_alloc_staging(struct panfrost_context * ctx,struct panfrost_resource * rsc,unsigned level,const struct pipe_box * box)909 pan_alloc_staging(struct panfrost_context *ctx, struct panfrost_resource *rsc,
910 unsigned level, const struct pipe_box *box)
911 {
912 struct pipe_context *pctx = &ctx->base;
913 struct pipe_resource tmpl = rsc->base;
914
915 tmpl.width0 = box->width;
916 tmpl.height0 = box->height;
917 /* for array textures, box->depth is the array_size, otherwise
918 * for 3d textures, it is the depth:
919 */
920 if (tmpl.array_size > 1) {
921 if (tmpl.target == PIPE_TEXTURE_CUBE)
922 tmpl.target = PIPE_TEXTURE_2D_ARRAY;
923 tmpl.array_size = box->depth;
924 tmpl.depth0 = 1;
925 } else {
926 tmpl.array_size = 1;
927 tmpl.depth0 = box->depth;
928 }
929 tmpl.last_level = 0;
930 tmpl.bind |= PIPE_BIND_LINEAR;
931 tmpl.bind &= ~PAN_BIND_SHARED_MASK;
932 tmpl.compression_rate = PIPE_COMPRESSION_FIXED_RATE_NONE;
933
934 struct pipe_resource *pstaging =
935 pctx->screen->resource_create(pctx->screen, &tmpl);
936 if (!pstaging)
937 return NULL;
938
939 return pan_resource(pstaging);
940 }
941
942 static void
pan_blit_from_staging(struct pipe_context * pctx,struct panfrost_transfer * trans)943 pan_blit_from_staging(struct pipe_context *pctx,
944 struct panfrost_transfer *trans)
945 {
946 struct pipe_resource *dst = trans->base.resource;
947 struct pipe_blit_info blit = {0};
948
949 blit.dst.resource = dst;
950 blit.dst.format = dst->format;
951 blit.dst.level = trans->base.level;
952 blit.dst.box = trans->base.box;
953 blit.src.resource = trans->staging.rsrc;
954 blit.src.format = trans->staging.rsrc->format;
955 blit.src.level = 0;
956 blit.src.box = trans->staging.box;
957 blit.mask = util_format_get_mask(blit.src.format);
958 blit.filter = PIPE_TEX_FILTER_NEAREST;
959
960 panfrost_blit_no_afbc_legalization(pctx, &blit);
961 }
962
963 static void
pan_blit_to_staging(struct pipe_context * pctx,struct panfrost_transfer * trans)964 pan_blit_to_staging(struct pipe_context *pctx, struct panfrost_transfer *trans)
965 {
966 struct pipe_resource *src = trans->base.resource;
967 struct pipe_blit_info blit = {0};
968
969 blit.src.resource = src;
970 blit.src.format = src->format;
971 blit.src.level = trans->base.level;
972 blit.src.box = trans->base.box;
973 blit.dst.resource = trans->staging.rsrc;
974 blit.dst.format = trans->staging.rsrc->format;
975 blit.dst.level = 0;
976 blit.dst.box = trans->staging.box;
977 blit.mask = util_format_get_mask(blit.dst.format);
978 blit.filter = PIPE_TEX_FILTER_NEAREST;
979
980 panfrost_blit_no_afbc_legalization(pctx, &blit);
981 }
982
983 static void
panfrost_load_tiled_images(struct panfrost_transfer * transfer,struct panfrost_resource * rsrc)984 panfrost_load_tiled_images(struct panfrost_transfer *transfer,
985 struct panfrost_resource *rsrc)
986 {
987 struct pipe_transfer *ptrans = &transfer->base;
988 unsigned level = ptrans->level;
989
990 /* If the requested level of the image is uninitialized, it's not
991 * necessary to copy it. Leave the result unintiialized too.
992 */
993 if (!BITSET_TEST(rsrc->valid.data, level))
994 return;
995
996 struct panfrost_bo *bo = rsrc->bo;
997 unsigned stride = panfrost_get_layer_stride(&rsrc->image.layout, level);
998
999 /* Otherwise, load each layer separately, required to load from 3D and
1000 * array textures.
1001 */
1002 for (unsigned z = 0; z < ptrans->box.depth; ++z) {
1003 void *dst = transfer->map + (ptrans->layer_stride * z);
1004 uint8_t *map = bo->ptr.cpu + rsrc->image.layout.slices[level].offset +
1005 (z + ptrans->box.z) * stride;
1006
1007 panfrost_load_tiled_image(dst, map, ptrans->box.x, ptrans->box.y,
1008 ptrans->box.width, ptrans->box.height,
1009 ptrans->stride,
1010 rsrc->image.layout.slices[level].row_stride,
1011 rsrc->image.layout.format);
1012 }
1013 }
1014
1015 #if MESA_DEBUG
1016
1017 static unsigned
get_superblock_size(uint32_t * hdr,unsigned uncompressed_size)1018 get_superblock_size(uint32_t *hdr, unsigned uncompressed_size)
1019 {
1020 /* AFBC superblock layout 0 */
1021 unsigned body_base_ptr_len = 32;
1022 unsigned nr_subblocks = 16;
1023 unsigned sz_len = 6; /* bits */
1024 unsigned mask = (1 << sz_len) - 1;
1025 unsigned size = 0;
1026
1027 /* Sum up all of the subblock sizes */
1028 for (int i = 0; i < nr_subblocks; i++) {
1029 unsigned bitoffset = body_base_ptr_len + (i * sz_len);
1030 unsigned start = bitoffset / 32;
1031 unsigned end = (bitoffset + (sz_len - 1)) / 32;
1032 unsigned offset = bitoffset % 32;
1033 unsigned subblock_size;
1034
1035 if (start != end)
1036 subblock_size = (hdr[start] >> offset) | (hdr[end] << (32 - offset));
1037 else
1038 subblock_size = hdr[start] >> offset;
1039 subblock_size = (subblock_size == 1) ? uncompressed_size : subblock_size;
1040 size += subblock_size & mask;
1041
1042 if (i == 0 && size == 0)
1043 return 0;
1044 }
1045
1046 return size;
1047 }
1048
1049 static void
dump_block(struct panfrost_resource * rsrc,uint32_t idx)1050 dump_block(struct panfrost_resource *rsrc, uint32_t idx)
1051 {
1052 panfrost_bo_wait(rsrc->bo, INT64_MAX, false);
1053
1054 uint8_t *ptr = rsrc->bo->ptr.cpu;
1055 uint32_t *header = (uint32_t *)(ptr + (idx * AFBC_HEADER_BYTES_PER_TILE));
1056 uint32_t body_base_ptr = header[0];
1057 uint32_t *body = (uint32_t *)(ptr + body_base_ptr);
1058 struct pan_block_size block_sz =
1059 panfrost_afbc_subblock_size(rsrc->image.layout.modifier);
1060 unsigned pixel_sz = util_format_get_blocksize(rsrc->base.format);
1061 unsigned uncompressed_size = pixel_sz * block_sz.width * block_sz.height;
1062 unsigned size = get_superblock_size(header, uncompressed_size);
1063
1064 fprintf(stderr, " Header: %08x %08x %08x %08x (size: %u bytes)\n",
1065 header[0], header[1], header[2], header[3], size);
1066 if (size > 0) {
1067 fprintf(stderr, " Body: %08x %08x %08x %08x\n", body[0], body[1],
1068 body[2], body[3]);
1069 } else {
1070 uint8_t *comp = (uint8_t *)(header + 2);
1071 fprintf(stderr, " Color: 0x%02x%02x%02x%02x\n", comp[0], comp[1],
1072 comp[2], comp[3]);
1073 }
1074 fprintf(stderr, "\n");
1075 }
1076
1077 void
pan_dump_resource(struct panfrost_context * ctx,struct panfrost_resource * rsc)1078 pan_dump_resource(struct panfrost_context *ctx, struct panfrost_resource *rsc)
1079 {
1080 struct pipe_context *pctx = &ctx->base;
1081 struct pipe_resource tmpl = rsc->base;
1082 struct pipe_resource *plinear = NULL;
1083 struct panfrost_resource *linear = rsc;
1084 struct pipe_blit_info blit = {0};
1085 struct pipe_box box;
1086 char buffer[1024];
1087
1088 if (rsc->image.layout.modifier != DRM_FORMAT_MOD_LINEAR) {
1089 tmpl.bind |= PIPE_BIND_LINEAR;
1090 tmpl.bind &= ~PAN_BIND_SHARED_MASK;
1091
1092 plinear = pctx->screen->resource_create(pctx->screen, &tmpl);
1093 u_box_2d(0, 0, rsc->base.width0, rsc->base.height0, &box);
1094
1095 blit.src.resource = &rsc->base;
1096 blit.src.format = rsc->base.format;
1097 blit.src.level = 0;
1098 blit.src.box = box;
1099 blit.dst.resource = plinear;
1100 blit.dst.format = rsc->base.format;
1101 blit.dst.level = 0;
1102 blit.dst.box = box;
1103 blit.mask = util_format_get_mask(blit.dst.format);
1104 blit.filter = PIPE_TEX_FILTER_NEAREST;
1105
1106 panfrost_blit(pctx, &blit);
1107
1108 linear = pan_resource(plinear);
1109 }
1110
1111 panfrost_flush_writer(ctx, linear, "dump image");
1112 panfrost_bo_wait(linear->bo, INT64_MAX, false);
1113 panfrost_bo_mmap(linear->bo);
1114
1115 static unsigned frame_count = 0;
1116 frame_count++;
1117 snprintf(buffer, sizeof(buffer), "dump_image.%04d", frame_count);
1118
1119 debug_dump_image(buffer, rsc->base.format, 0 /* UNUSED */, rsc->base.width0,
1120 rsc->base.height0,
1121 linear->image.layout.slices[0].row_stride,
1122 linear->bo->ptr.cpu);
1123
1124 if (plinear)
1125 pipe_resource_reference(&plinear, NULL);
1126 }
1127
1128 #endif
1129
1130 /* Get scan-order index from (x, y) position when blocks are
1131 * arranged in z-order in 8x8 tiles */
1132 static unsigned
get_morton_index(unsigned x,unsigned y,unsigned stride)1133 get_morton_index(unsigned x, unsigned y, unsigned stride)
1134 {
1135 unsigned i = ((x << 0) & 1) | ((y << 1) & 2) | ((x << 1) & 4) |
1136 ((y << 2) & 8) | ((x << 2) & 16) | ((y << 3) & 32);
1137 return (((y & ~7) * stride) + ((x & ~7) << 3)) + i;
1138 }
1139
1140 static void
panfrost_store_tiled_images(struct panfrost_transfer * transfer,struct panfrost_resource * rsrc)1141 panfrost_store_tiled_images(struct panfrost_transfer *transfer,
1142 struct panfrost_resource *rsrc)
1143 {
1144 struct panfrost_bo *bo = rsrc->bo;
1145 struct pipe_transfer *ptrans = &transfer->base;
1146 unsigned level = ptrans->level;
1147 unsigned stride = panfrost_get_layer_stride(&rsrc->image.layout, level);
1148
1149 /* Otherwise, store each layer separately, required to store to 3D and
1150 * array textures.
1151 */
1152 for (unsigned z = 0; z < ptrans->box.depth; ++z) {
1153 void *src = transfer->map + (ptrans->layer_stride * z);
1154 uint8_t *map = bo->ptr.cpu + rsrc->image.layout.slices[level].offset +
1155 (z + ptrans->box.z) * stride;
1156
1157 panfrost_store_tiled_image(map, src, ptrans->box.x, ptrans->box.y,
1158 ptrans->box.width, ptrans->box.height,
1159 rsrc->image.layout.slices[level].row_stride,
1160 ptrans->stride, rsrc->image.layout.format);
1161 }
1162 }
1163
1164 static bool
panfrost_box_covers_resource(const struct pipe_resource * resource,const struct pipe_box * box)1165 panfrost_box_covers_resource(const struct pipe_resource *resource,
1166 const struct pipe_box *box)
1167 {
1168 return resource->last_level == 0 &&
1169 util_texrange_covers_whole_level(resource, 0, box->x, box->y, box->z,
1170 box->width, box->height, box->depth);
1171 }
1172
1173 static bool
panfrost_can_discard(struct pipe_resource * resource,const struct pipe_box * box,unsigned usage)1174 panfrost_can_discard(struct pipe_resource *resource, const struct pipe_box *box,
1175 unsigned usage)
1176 {
1177 struct panfrost_resource *rsrc = pan_resource(resource);
1178
1179 return ((usage & PIPE_MAP_DISCARD_RANGE) &&
1180 !(usage & PIPE_MAP_UNSYNCHRONIZED) &&
1181 !(resource->flags & PIPE_RESOURCE_FLAG_MAP_PERSISTENT) &&
1182 panfrost_box_covers_resource(resource, box) &&
1183 !(rsrc->bo->flags & PAN_BO_SHARED));
1184 }
1185
1186 static void *
panfrost_ptr_map(struct pipe_context * pctx,struct pipe_resource * resource,unsigned level,unsigned usage,const struct pipe_box * box,struct pipe_transfer ** out_transfer)1187 panfrost_ptr_map(struct pipe_context *pctx, struct pipe_resource *resource,
1188 unsigned level,
1189 unsigned usage, /* a combination of PIPE_MAP_x */
1190 const struct pipe_box *box,
1191 struct pipe_transfer **out_transfer)
1192 {
1193 struct panfrost_context *ctx = pan_context(pctx);
1194 struct panfrost_device *dev = pan_device(pctx->screen);
1195 struct panfrost_resource *rsrc = pan_resource(resource);
1196 enum pipe_format format = rsrc->image.layout.format;
1197 int bytes_per_block = util_format_get_blocksize(format);
1198 struct panfrost_bo *bo = rsrc->bo;
1199
1200 /* Can't map tiled/compressed directly */
1201 if ((usage & PIPE_MAP_DIRECTLY) &&
1202 rsrc->image.layout.modifier != DRM_FORMAT_MOD_LINEAR)
1203 return NULL;
1204
1205 struct panfrost_transfer *transfer = rzalloc(pctx, struct panfrost_transfer);
1206 transfer->base.level = level;
1207 transfer->base.usage = usage;
1208 transfer->base.box = *box;
1209
1210 pipe_resource_reference(&transfer->base.resource, resource);
1211 *out_transfer = &transfer->base;
1212
1213 if (usage & PIPE_MAP_WRITE)
1214 rsrc->constant_stencil = false;
1215
1216 /* We don't have s/w routines for AFBC/AFRC, so use a staging texture */
1217 if (drm_is_afbc(rsrc->image.layout.modifier) ||
1218 drm_is_afrc(rsrc->image.layout.modifier)) {
1219 struct panfrost_resource *staging =
1220 pan_alloc_staging(ctx, rsrc, level, box);
1221 assert(staging);
1222
1223 /* Staging resources have one LOD: level 0. Query the strides
1224 * on this LOD.
1225 */
1226 transfer->base.stride = staging->image.layout.slices[0].row_stride;
1227 transfer->base.layer_stride =
1228 panfrost_get_layer_stride(&staging->image.layout, 0);
1229
1230 transfer->staging.rsrc = &staging->base;
1231
1232 transfer->staging.box = *box;
1233 transfer->staging.box.x = 0;
1234 transfer->staging.box.y = 0;
1235 transfer->staging.box.z = 0;
1236
1237 assert(transfer->staging.rsrc != NULL);
1238
1239 bool valid = BITSET_TEST(rsrc->valid.data, level);
1240
1241 if ((usage & PIPE_MAP_READ) &&
1242 (valid || panfrost_any_batch_writes_rsrc(ctx, rsrc))) {
1243 pan_blit_to_staging(pctx, transfer);
1244 panfrost_flush_writer(ctx, staging, "AFBC/AFRC tex read staging blit");
1245 panfrost_bo_wait(staging->bo, INT64_MAX, false);
1246 }
1247
1248 panfrost_bo_mmap(staging->bo);
1249 return staging->bo->ptr.cpu;
1250 }
1251
1252 bool already_mapped = bo->ptr.cpu != NULL;
1253
1254 /* If we haven't already mmaped, now's the time */
1255 panfrost_bo_mmap(bo);
1256
1257 if (dev->debug & (PAN_DBG_TRACE | PAN_DBG_SYNC)) {
1258 pandecode_inject_mmap(dev->decode_ctx, bo->ptr.gpu, bo->ptr.cpu,
1259 panfrost_bo_size(bo), NULL);
1260 }
1261
1262 /* Upgrade writes to uninitialized ranges to UNSYNCHRONIZED */
1263 if ((usage & PIPE_MAP_WRITE) && resource->target == PIPE_BUFFER &&
1264 !util_ranges_intersect(&rsrc->valid_buffer_range, box->x,
1265 box->x + box->width)) {
1266
1267 usage |= PIPE_MAP_UNSYNCHRONIZED;
1268 }
1269
1270 /* Upgrade DISCARD_RANGE to WHOLE_RESOURCE if the whole resource is
1271 * being mapped.
1272 */
1273 if (panfrost_can_discard(resource, box, usage)) {
1274 usage |= PIPE_MAP_DISCARD_WHOLE_RESOURCE;
1275 }
1276
1277 bool create_new_bo = usage & PIPE_MAP_DISCARD_WHOLE_RESOURCE;
1278 bool copy_resource = false;
1279
1280 if (!(usage & PIPE_MAP_UNSYNCHRONIZED) &&
1281 !(resource->flags & PIPE_RESOURCE_FLAG_MAP_PERSISTENT) &&
1282 (usage & PIPE_MAP_WRITE) && panfrost_any_batch_reads_rsrc(ctx, rsrc)) {
1283 /* When a resource to be modified is already being used by a
1284 * pending batch, it is often faster to copy the whole BO than
1285 * to flush and split the frame in two.
1286 */
1287
1288 panfrost_flush_writer(ctx, rsrc, "Shadow resource creation");
1289 panfrost_bo_wait(bo, INT64_MAX, false);
1290
1291 create_new_bo = true;
1292 copy_resource = !(usage & PIPE_MAP_DISCARD_WHOLE_RESOURCE);
1293 }
1294
1295 /* Shadowing with separate stencil may require additional accounting.
1296 * Bail in these exotic cases.
1297 */
1298 if (rsrc->separate_stencil) {
1299 create_new_bo = false;
1300 copy_resource = false;
1301 }
1302
1303 if (create_new_bo &&
1304 (!(resource->flags & PIPE_RESOURCE_FLAG_MAP_PERSISTENT) ||
1305 !already_mapped)) {
1306 /* Make sure we re-emit any descriptors using this resource */
1307 panfrost_dirty_state_all(ctx);
1308
1309 /* If the BO is used by one of the pending batches or if it's
1310 * not ready yet (still accessed by one of the already flushed
1311 * batches), we try to allocate a new one to avoid waiting.
1312 */
1313 if (panfrost_any_batch_reads_rsrc(ctx, rsrc) ||
1314 !panfrost_bo_wait(bo, 0, true)) {
1315 /* We want the BO to be MMAPed. */
1316 uint32_t flags = bo->flags & ~PAN_BO_DELAY_MMAP;
1317 struct panfrost_bo *newbo = NULL;
1318
1319 /* When the BO has been imported/exported, we can't
1320 * replace it by another one, otherwise the
1321 * importer/exporter wouldn't see the change we're
1322 * doing to it.
1323 */
1324 if (!(bo->flags & PAN_BO_SHARED)) {
1325 newbo =
1326 panfrost_bo_create(dev, panfrost_bo_size(bo), flags, bo->label);
1327 }
1328
1329 if (newbo) {
1330 if (copy_resource) {
1331 memcpy(newbo->ptr.cpu, rsrc->bo->ptr.cpu, panfrost_bo_size(bo));
1332 }
1333
1334 /* Swap the pointers, dropping a reference to
1335 * the old BO which is no long referenced from
1336 * the resource.
1337 */
1338 panfrost_bo_unreference(rsrc->bo);
1339 rsrc->bo = newbo;
1340 rsrc->image.data.base = newbo->ptr.gpu;
1341
1342 if (!copy_resource && drm_is_afbc(rsrc->image.layout.modifier))
1343 panfrost_resource_init_afbc_headers(rsrc);
1344
1345 bo = newbo;
1346 } else {
1347 /* Allocation failed or was impossible, let's
1348 * fall back on a flush+wait.
1349 */
1350 panfrost_flush_batches_accessing_rsrc(
1351 ctx, rsrc, "Resource access with high memory pressure");
1352 panfrost_bo_wait(bo, INT64_MAX, true);
1353 }
1354 }
1355 } else if (!(usage & PIPE_MAP_UNSYNCHRONIZED)) {
1356 if (usage & PIPE_MAP_WRITE) {
1357 panfrost_flush_batches_accessing_rsrc(ctx, rsrc, "Synchronized write");
1358 panfrost_bo_wait(bo, INT64_MAX, true);
1359 } else if (usage & PIPE_MAP_READ) {
1360 panfrost_flush_writer(ctx, rsrc, "Synchronized read");
1361 panfrost_bo_wait(bo, INT64_MAX, false);
1362 }
1363 }
1364
1365 /* For access to compressed textures, we want the (x, y, w, h)
1366 * region-of-interest in blocks, not pixels. Then we compute the stride
1367 * between rows of blocks as the width in blocks times the width per
1368 * block, etc.
1369 */
1370 struct pipe_box box_blocks;
1371 u_box_pixels_to_blocks(&box_blocks, box, format);
1372
1373 if (rsrc->image.layout.modifier ==
1374 DRM_FORMAT_MOD_ARM_16X16_BLOCK_U_INTERLEAVED) {
1375 transfer->base.stride = box_blocks.width * bytes_per_block;
1376 transfer->base.layer_stride = transfer->base.stride * box_blocks.height;
1377 transfer->map =
1378 ralloc_size(transfer, transfer->base.layer_stride * box->depth);
1379
1380 if (usage & PIPE_MAP_READ)
1381 panfrost_load_tiled_images(transfer, rsrc);
1382
1383 return transfer->map;
1384 } else {
1385 assert(rsrc->image.layout.modifier == DRM_FORMAT_MOD_LINEAR);
1386
1387 /* Direct, persistent writes create holes in time for
1388 * caching... I don't know if this is actually possible but we
1389 * should still get it right */
1390
1391 unsigned dpw = PIPE_MAP_DIRECTLY | PIPE_MAP_WRITE | PIPE_MAP_PERSISTENT;
1392
1393 if ((usage & dpw) == dpw && rsrc->index_cache)
1394 return NULL;
1395
1396 transfer->base.stride = rsrc->image.layout.slices[level].row_stride;
1397 transfer->base.layer_stride =
1398 panfrost_get_layer_stride(&rsrc->image.layout, level);
1399
1400 /* By mapping direct-write, we're implicitly already
1401 * initialized (maybe), so be conservative */
1402
1403 if (usage & PIPE_MAP_WRITE) {
1404 BITSET_SET(rsrc->valid.data, level);
1405 panfrost_minmax_cache_invalidate(
1406 rsrc->index_cache, transfer->base.box.x, transfer->base.box.width);
1407 }
1408
1409 return bo->ptr.cpu + rsrc->image.layout.slices[level].offset +
1410 box->z * transfer->base.layer_stride +
1411 box_blocks.y * rsrc->image.layout.slices[level].row_stride +
1412 box_blocks.x * bytes_per_block;
1413 }
1414 }
1415
1416 void
pan_resource_modifier_convert(struct panfrost_context * ctx,struct panfrost_resource * rsrc,uint64_t modifier,bool copy_resource,const char * reason)1417 pan_resource_modifier_convert(struct panfrost_context *ctx,
1418 struct panfrost_resource *rsrc, uint64_t modifier,
1419 bool copy_resource, const char *reason)
1420 {
1421 assert(!rsrc->modifier_constant);
1422
1423 struct pipe_resource *tmp_prsrc = panfrost_resource_create_with_modifier(
1424 ctx->base.screen, &rsrc->base, modifier);
1425 struct panfrost_resource *tmp_rsrc = pan_resource(tmp_prsrc);
1426
1427 if (copy_resource) {
1428 struct pipe_blit_info blit = {
1429 .dst.resource = &tmp_rsrc->base,
1430 .dst.format = tmp_rsrc->base.format,
1431 .src.resource = &rsrc->base,
1432 .src.format = rsrc->base.format,
1433 .mask = util_format_get_mask(tmp_rsrc->base.format),
1434 .filter = PIPE_TEX_FILTER_NEAREST,
1435 };
1436
1437 /* data_valid is not valid until flushed */
1438 panfrost_flush_writer(ctx, rsrc, "AFBC/AFRC decompressing blit");
1439
1440 for (int i = 0; i <= rsrc->base.last_level; i++) {
1441 if (BITSET_TEST(rsrc->valid.data, i)) {
1442 blit.dst.level = blit.src.level = i;
1443
1444 u_box_3d(0, 0, 0, u_minify(rsrc->base.width0, i),
1445 u_minify(rsrc->base.height0, i),
1446 util_num_layers(&rsrc->base, i), &blit.dst.box);
1447 blit.src.box = blit.dst.box;
1448
1449 panfrost_blit_no_afbc_legalization(&ctx->base, &blit);
1450 }
1451 }
1452
1453 /* we lose track of tmp_rsrc after this point, and the BO migration
1454 * (from tmp_rsrc to rsrc) doesn't transfer the last_writer to rsrc
1455 */
1456 panfrost_flush_writer(ctx, tmp_rsrc, "AFBC/AFRC decompressing blit");
1457 }
1458
1459 panfrost_bo_unreference(rsrc->bo);
1460
1461 rsrc->bo = tmp_rsrc->bo;
1462 rsrc->image.data.base = rsrc->bo->ptr.gpu;
1463 panfrost_bo_reference(rsrc->bo);
1464
1465 panfrost_resource_setup(ctx->base.screen, rsrc, modifier,
1466 tmp_rsrc->base.format);
1467 /* panfrost_resource_setup will force the modifier to stay constant when
1468 * called with a specific modifier. We don't want that here, we want to
1469 * be able to convert back to another modifier if needed */
1470 rsrc->modifier_constant = false;
1471 pipe_resource_reference(&tmp_prsrc, NULL);
1472 }
1473
1474 /* Validate that an AFBC/AFRC resource may be used as a particular format. If it
1475 * may not, decompress it on the fly. Failure to do so can produce wrong results
1476 * or invalid data faults when sampling or rendering to AFBC */
1477
1478 void
pan_legalize_format(struct panfrost_context * ctx,struct panfrost_resource * rsrc,enum pipe_format format,bool write,bool discard)1479 pan_legalize_format(struct panfrost_context *ctx,
1480 struct panfrost_resource *rsrc, enum pipe_format format,
1481 bool write, bool discard)
1482 {
1483 struct panfrost_device *dev = pan_device(ctx->base.screen);
1484 enum pipe_format old_format = rsrc->base.format;
1485 enum pipe_format new_format = format;
1486 bool compatible = true;
1487
1488 if (!drm_is_afbc(rsrc->image.layout.modifier) &&
1489 !drm_is_afrc(rsrc->image.layout.modifier))
1490 return;
1491
1492 if (drm_is_afbc(rsrc->image.layout.modifier)) {
1493 compatible = (panfrost_afbc_format(dev->arch, old_format) ==
1494 panfrost_afbc_format(dev->arch, new_format));
1495 } else if (drm_is_afrc(rsrc->image.layout.modifier)) {
1496 struct pan_afrc_format_info old_info =
1497 panfrost_afrc_get_format_info(old_format);
1498 struct pan_afrc_format_info new_info =
1499 panfrost_afrc_get_format_info(new_format);
1500 compatible = !memcmp(&old_info, &new_info, sizeof(old_info));
1501 }
1502
1503 if (!compatible) {
1504 pan_resource_modifier_convert(
1505 ctx, rsrc, DRM_FORMAT_MOD_ARM_16X16_BLOCK_U_INTERLEAVED, !discard,
1506 drm_is_afbc(rsrc->image.layout.modifier)
1507 ? "Reinterpreting AFBC surface as incompatible format"
1508 : "Reinterpreting AFRC surface as incompatible format");
1509 return;
1510 }
1511
1512 /* Can't write to AFBC-P resources */
1513 if (write && drm_is_afbc(rsrc->image.layout.modifier) &&
1514 (rsrc->image.layout.modifier & AFBC_FORMAT_MOD_SPARSE) == 0) {
1515 pan_resource_modifier_convert(
1516 ctx, rsrc, rsrc->image.layout.modifier | AFBC_FORMAT_MOD_SPARSE,
1517 !discard, "Legalizing resource to allow writing");
1518 }
1519 }
1520
1521 static bool
panfrost_should_linear_convert(struct panfrost_context * ctx,struct panfrost_resource * prsrc,struct pipe_transfer * transfer)1522 panfrost_should_linear_convert(struct panfrost_context *ctx,
1523 struct panfrost_resource *prsrc,
1524 struct pipe_transfer *transfer)
1525 {
1526 if (prsrc->modifier_constant)
1527 return false;
1528
1529 /* Overwriting the entire resource indicates streaming, for which
1530 * linear layout is most efficient due to the lack of expensive
1531 * conversion.
1532 *
1533 * For now we just switch to linear after a number of complete
1534 * overwrites to keep things simple, but we could do better.
1535 *
1536 * This mechanism is only implemented for 2D resources. This suffices
1537 * for video players, its intended use case.
1538 */
1539
1540 bool entire_overwrite = panfrost_is_2d(prsrc) &&
1541 prsrc->base.last_level == 0 &&
1542 transfer->box.width == prsrc->base.width0 &&
1543 transfer->box.height == prsrc->base.height0 &&
1544 transfer->box.x == 0 && transfer->box.y == 0;
1545
1546 if (entire_overwrite)
1547 ++prsrc->modifier_updates;
1548
1549 if (prsrc->modifier_updates >= LAYOUT_CONVERT_THRESHOLD) {
1550 perf_debug(ctx, "Transitioning to linear due to streaming usage");
1551 return true;
1552 } else {
1553 return false;
1554 }
1555 }
1556
1557 struct panfrost_bo *
panfrost_get_afbc_superblock_sizes(struct panfrost_context * ctx,struct panfrost_resource * rsrc,unsigned first_level,unsigned last_level,unsigned * out_offsets)1558 panfrost_get_afbc_superblock_sizes(struct panfrost_context *ctx,
1559 struct panfrost_resource *rsrc,
1560 unsigned first_level, unsigned last_level,
1561 unsigned *out_offsets)
1562 {
1563 struct panfrost_screen *screen = pan_screen(ctx->base.screen);
1564 struct panfrost_device *dev = pan_device(ctx->base.screen);
1565 struct panfrost_batch *batch;
1566 struct panfrost_bo *bo;
1567 unsigned metadata_size = 0;
1568
1569 for (int level = first_level; level <= last_level; ++level) {
1570 struct pan_image_slice_layout *slice = &rsrc->image.layout.slices[level];
1571 unsigned sz = slice->afbc.nr_blocks * sizeof(struct pan_afbc_block_info);
1572 out_offsets[level - first_level] = metadata_size;
1573 metadata_size += sz;
1574 }
1575
1576 panfrost_flush_batches_accessing_rsrc(ctx, rsrc, "AFBC before size flush");
1577 batch = panfrost_get_fresh_batch_for_fbo(ctx, "AFBC superblock sizes");
1578 bo = panfrost_bo_create(dev, metadata_size, 0, "AFBC superblock sizes");
1579 assert(bo);
1580
1581 for (int level = first_level; level <= last_level; ++level) {
1582 unsigned offset = out_offsets[level - first_level];
1583 screen->vtbl.afbc_size(batch, rsrc, bo, offset, level);
1584 }
1585
1586 panfrost_flush_batches_accessing_rsrc(ctx, rsrc, "AFBC after size flush");
1587
1588 return bo;
1589 }
1590
1591 void
panfrost_pack_afbc(struct panfrost_context * ctx,struct panfrost_resource * prsrc)1592 panfrost_pack_afbc(struct panfrost_context *ctx,
1593 struct panfrost_resource *prsrc)
1594 {
1595 struct panfrost_screen *screen = pan_screen(ctx->base.screen);
1596 struct panfrost_device *dev = pan_device(ctx->base.screen);
1597 struct panfrost_bo *metadata_bo;
1598 unsigned metadata_offsets[PIPE_MAX_TEXTURE_LEVELS];
1599
1600 uint64_t src_modifier = prsrc->image.layout.modifier;
1601 uint64_t dst_modifier =
1602 src_modifier & ~(AFBC_FORMAT_MOD_TILED | AFBC_FORMAT_MOD_SPARSE);
1603 bool is_tiled = src_modifier & AFBC_FORMAT_MOD_TILED;
1604 unsigned last_level = prsrc->base.last_level;
1605 struct pan_image_slice_layout slice_infos[PIPE_MAX_TEXTURE_LEVELS] = {0};
1606 unsigned total_size = 0;
1607
1608 /* It doesn't make sense to pack everything if we need to unpack right
1609 * away to upload data to another level */
1610 for (int i = 0; i <= last_level; i++) {
1611 if (!BITSET_TEST(prsrc->valid.data, i))
1612 return;
1613 }
1614
1615 metadata_bo = panfrost_get_afbc_superblock_sizes(ctx, prsrc, 0, last_level,
1616 metadata_offsets);
1617 panfrost_bo_wait(metadata_bo, INT64_MAX, false);
1618
1619 for (unsigned level = 0; level <= last_level; ++level) {
1620 struct pan_image_slice_layout *src_slice =
1621 &prsrc->image.layout.slices[level];
1622 struct pan_image_slice_layout *dst_slice = &slice_infos[level];
1623
1624 unsigned width = u_minify(prsrc->base.width0, level);
1625 unsigned height = u_minify(prsrc->base.height0, level);
1626 unsigned src_stride =
1627 pan_afbc_stride_blocks(src_modifier, src_slice->row_stride);
1628 unsigned dst_stride =
1629 DIV_ROUND_UP(width, panfrost_afbc_superblock_width(dst_modifier));
1630 unsigned dst_height =
1631 DIV_ROUND_UP(height, panfrost_afbc_superblock_height(dst_modifier));
1632
1633 uint32_t offset = 0;
1634 struct pan_afbc_block_info *meta =
1635 metadata_bo->ptr.cpu + metadata_offsets[level];
1636
1637 for (unsigned y = 0, i = 0; y < dst_height; ++y) {
1638 for (unsigned x = 0; x < dst_stride; ++x, ++i) {
1639 unsigned idx = is_tiled ? get_morton_index(x, y, src_stride) : i;
1640 uint32_t size = meta[idx].size;
1641 meta[idx].offset = offset; /* write the start offset */
1642 offset += size;
1643 }
1644 }
1645
1646 total_size = ALIGN_POT(total_size, pan_slice_align(dst_modifier));
1647 {
1648 dst_slice->afbc.stride = dst_stride;
1649 dst_slice->afbc.nr_blocks = dst_stride * dst_height;
1650 dst_slice->afbc.header_size =
1651 ALIGN_POT(dst_stride * dst_height * AFBC_HEADER_BYTES_PER_TILE,
1652 pan_afbc_body_align(dst_modifier));
1653 dst_slice->afbc.body_size = offset;
1654 dst_slice->afbc.surface_stride = dst_slice->afbc.header_size + offset;
1655
1656 dst_slice->offset = total_size;
1657 dst_slice->row_stride = dst_stride * AFBC_HEADER_BYTES_PER_TILE;
1658 dst_slice->surface_stride = dst_slice->afbc.surface_stride;
1659 dst_slice->size = dst_slice->afbc.surface_stride;
1660 }
1661 total_size += dst_slice->afbc.surface_stride;
1662 }
1663
1664 unsigned new_size = ALIGN_POT(total_size, 4096); // FIXME
1665 unsigned old_size = panfrost_bo_size(prsrc->bo);
1666 unsigned ratio = 100 * new_size / old_size;
1667
1668 if (ratio > screen->max_afbc_packing_ratio)
1669 return;
1670
1671 perf_debug(ctx, "%i%%: %i KB -> %i KB\n", ratio, old_size / 1024,
1672 new_size / 1024);
1673
1674 struct panfrost_bo *dst =
1675 panfrost_bo_create(dev, new_size, 0, "AFBC compact texture");
1676 assert(dst);
1677 struct panfrost_batch *batch =
1678 panfrost_get_fresh_batch_for_fbo(ctx, "AFBC compaction");
1679
1680 for (unsigned level = 0; level <= last_level; ++level) {
1681 struct pan_image_slice_layout *slice = &slice_infos[level];
1682 screen->vtbl.afbc_pack(batch, prsrc, dst, slice, metadata_bo,
1683 metadata_offsets[level], level);
1684 prsrc->image.layout.slices[level] = *slice;
1685 }
1686
1687 panfrost_flush_batches_accessing_rsrc(ctx, prsrc, "AFBC compaction flush");
1688
1689 prsrc->image.layout.modifier = dst_modifier;
1690 panfrost_bo_unreference(prsrc->bo);
1691 prsrc->bo = dst;
1692 prsrc->image.data.base = dst->ptr.gpu;
1693 panfrost_bo_unreference(metadata_bo);
1694 }
1695
1696 static void
panfrost_ptr_unmap(struct pipe_context * pctx,struct pipe_transfer * transfer)1697 panfrost_ptr_unmap(struct pipe_context *pctx, struct pipe_transfer *transfer)
1698 {
1699 /* Gallium expects writeback here, so we tile */
1700
1701 struct panfrost_context *ctx = pan_context(pctx);
1702 struct pipe_screen *screen = ctx->base.screen;
1703 struct panfrost_transfer *trans = pan_transfer(transfer);
1704 struct panfrost_resource *prsrc =
1705 (struct panfrost_resource *)transfer->resource;
1706 struct panfrost_device *dev = pan_device(pctx->screen);
1707
1708 if (transfer->usage & PIPE_MAP_WRITE)
1709 prsrc->valid.crc = false;
1710
1711 /* AFBC/AFRC will use a staging resource. `initialized` will be set when
1712 * the fragment job is created; this is deferred to prevent useless surface
1713 * reloads that can cascade into DATA_INVALID_FAULTs due to reading
1714 * malformed AFBC/AFRC data if uninitialized */
1715
1716 if (trans->staging.rsrc) {
1717 if (transfer->usage & PIPE_MAP_WRITE) {
1718 if (panfrost_should_linear_convert(ctx, prsrc, transfer)) {
1719
1720 panfrost_bo_unreference(prsrc->bo);
1721
1722 panfrost_resource_setup(screen, prsrc, DRM_FORMAT_MOD_LINEAR,
1723 prsrc->image.layout.format);
1724
1725 prsrc->bo = pan_resource(trans->staging.rsrc)->bo;
1726 prsrc->image.data.base = prsrc->bo->ptr.gpu;
1727 panfrost_bo_reference(prsrc->bo);
1728 } else {
1729 bool discard = panfrost_can_discard(&prsrc->base, &transfer->box,
1730 transfer->usage);
1731 pan_legalize_format(ctx, prsrc, prsrc->image.layout.format, true,
1732 discard);
1733 pan_blit_from_staging(pctx, trans);
1734 panfrost_flush_batches_accessing_rsrc(
1735 ctx, pan_resource(trans->staging.rsrc),
1736 "AFBC write staging blit");
1737
1738 if (pan_screen(pctx->screen)->force_afbc_packing) {
1739 if (panfrost_should_pack_afbc(dev, prsrc))
1740 panfrost_pack_afbc(ctx, prsrc);
1741 }
1742 }
1743 }
1744
1745 pipe_resource_reference(&trans->staging.rsrc, NULL);
1746 }
1747
1748 /* Tiling will occur in software from a staging cpu buffer */
1749 if (trans->map) {
1750 struct panfrost_bo *bo = prsrc->bo;
1751
1752 if (transfer->usage & PIPE_MAP_WRITE) {
1753 BITSET_SET(prsrc->valid.data, transfer->level);
1754
1755 if (prsrc->image.layout.modifier ==
1756 DRM_FORMAT_MOD_ARM_16X16_BLOCK_U_INTERLEAVED) {
1757 if (panfrost_should_linear_convert(ctx, prsrc, transfer)) {
1758 panfrost_resource_setup(screen, prsrc, DRM_FORMAT_MOD_LINEAR,
1759 prsrc->image.layout.format);
1760 if (prsrc->image.layout.data_size > panfrost_bo_size(bo)) {
1761 const char *label = bo->label;
1762 panfrost_bo_unreference(bo);
1763 bo = prsrc->bo = panfrost_bo_create(
1764 dev, prsrc->image.layout.data_size, 0, label);
1765 prsrc->image.data.base = prsrc->bo->ptr.gpu;
1766 assert(bo);
1767 }
1768
1769 util_copy_rect(
1770 bo->ptr.cpu + prsrc->image.layout.slices[0].offset,
1771 prsrc->base.format, prsrc->image.layout.slices[0].row_stride,
1772 0, 0, transfer->box.width, transfer->box.height, trans->map,
1773 transfer->stride, 0, 0);
1774 } else {
1775 panfrost_store_tiled_images(trans, prsrc);
1776 }
1777 }
1778 }
1779 }
1780
1781 util_range_add(&prsrc->base, &prsrc->valid_buffer_range, transfer->box.x,
1782 transfer->box.x + transfer->box.width);
1783
1784 if (transfer->usage & PIPE_MAP_WRITE) {
1785 panfrost_minmax_cache_invalidate(prsrc->index_cache, transfer->box.x,
1786 transfer->box.width);
1787 }
1788
1789 /* Derefence the resource */
1790 pipe_resource_reference(&transfer->resource, NULL);
1791
1792 /* Transfer itself is RALLOCed at the moment */
1793 ralloc_free(transfer);
1794 }
1795
1796 static void
panfrost_ptr_flush_region(struct pipe_context * pctx,struct pipe_transfer * transfer,const struct pipe_box * box)1797 panfrost_ptr_flush_region(struct pipe_context *pctx,
1798 struct pipe_transfer *transfer,
1799 const struct pipe_box *box)
1800 {
1801 struct panfrost_resource *rsc = pan_resource(transfer->resource);
1802
1803 if (transfer->resource->target == PIPE_BUFFER) {
1804 util_range_add(&rsc->base, &rsc->valid_buffer_range,
1805 transfer->box.x + box->x,
1806 transfer->box.x + box->x + box->width);
1807 } else {
1808 BITSET_SET(rsc->valid.data, transfer->level);
1809 }
1810 }
1811
1812 static void
panfrost_invalidate_resource(struct pipe_context * pctx,struct pipe_resource * prsrc)1813 panfrost_invalidate_resource(struct pipe_context *pctx,
1814 struct pipe_resource *prsrc)
1815 {
1816 struct panfrost_context *ctx = pan_context(pctx);
1817 struct panfrost_batch *batch = panfrost_get_batch_for_fbo(ctx);
1818 struct panfrost_resource *rsrc = pan_resource(prsrc);
1819
1820 rsrc->constant_stencil = true;
1821
1822 /* Handle the glInvalidateFramebuffer case */
1823 if (batch->key.zsbuf && batch->key.zsbuf->texture == prsrc)
1824 batch->resolve &= ~PIPE_CLEAR_DEPTHSTENCIL;
1825
1826 for (unsigned i = 0; i < batch->key.nr_cbufs; ++i) {
1827 struct pipe_surface *surf = batch->key.cbufs[i];
1828
1829 if (surf && surf->texture == prsrc)
1830 batch->resolve &= ~(PIPE_CLEAR_COLOR0 << i);
1831 }
1832 }
1833
1834 static enum pipe_format
panfrost_resource_get_internal_format(struct pipe_resource * rsrc)1835 panfrost_resource_get_internal_format(struct pipe_resource *rsrc)
1836 {
1837 struct panfrost_resource *prsrc = (struct panfrost_resource *)rsrc;
1838 return prsrc->image.layout.format;
1839 }
1840
1841 void
panfrost_set_image_view_planes(struct pan_image_view * iview,struct pipe_resource * texture)1842 panfrost_set_image_view_planes(struct pan_image_view *iview,
1843 struct pipe_resource *texture)
1844 {
1845 struct panfrost_resource *prsrc_plane = (struct panfrost_resource *)texture;
1846
1847 for (int i = 0; i < MAX_IMAGE_PLANES && prsrc_plane; i++) {
1848 iview->planes[i] = &prsrc_plane->image;
1849 prsrc_plane = (struct panfrost_resource *)prsrc_plane->base.next;
1850 }
1851 }
1852
1853 static bool
panfrost_generate_mipmap(struct pipe_context * pctx,struct pipe_resource * prsrc,enum pipe_format format,unsigned base_level,unsigned last_level,unsigned first_layer,unsigned last_layer)1854 panfrost_generate_mipmap(struct pipe_context *pctx, struct pipe_resource *prsrc,
1855 enum pipe_format format, unsigned base_level,
1856 unsigned last_level, unsigned first_layer,
1857 unsigned last_layer)
1858 {
1859 struct panfrost_resource *rsrc = pan_resource(prsrc);
1860
1861 perf_debug(pan_context(pctx), "Unoptimized mipmap generation");
1862
1863 /* Generating a mipmap invalidates the written levels, so make that
1864 * explicit so we don't try to wallpaper them back and end up with
1865 * u_blitter recursion */
1866
1867 assert(rsrc->bo);
1868 for (unsigned l = base_level + 1; l <= last_level; ++l)
1869 BITSET_CLEAR(rsrc->valid.data, l);
1870
1871 /* Beyond that, we just delegate the hard stuff. */
1872
1873 bool blit_res =
1874 util_gen_mipmap(pctx, prsrc, format, base_level, last_level, first_layer,
1875 last_layer, PIPE_TEX_FILTER_LINEAR);
1876
1877 return blit_res;
1878 }
1879
1880 static void
panfrost_resource_set_stencil(struct pipe_resource * prsrc,struct pipe_resource * stencil)1881 panfrost_resource_set_stencil(struct pipe_resource *prsrc,
1882 struct pipe_resource *stencil)
1883 {
1884 pan_resource(prsrc)->separate_stencil = pan_resource(stencil);
1885 }
1886
1887 static struct pipe_resource *
panfrost_resource_get_stencil(struct pipe_resource * prsrc)1888 panfrost_resource_get_stencil(struct pipe_resource *prsrc)
1889 {
1890 if (!pan_resource(prsrc)->separate_stencil)
1891 return NULL;
1892
1893 return &pan_resource(prsrc)->separate_stencil->base;
1894 }
1895
1896 static const struct u_transfer_vtbl transfer_vtbl = {
1897 .resource_create = panfrost_resource_create,
1898 .resource_destroy = panfrost_resource_destroy,
1899 .transfer_map = panfrost_ptr_map,
1900 .transfer_unmap = panfrost_ptr_unmap,
1901 .transfer_flush_region = panfrost_ptr_flush_region,
1902 .get_internal_format = panfrost_resource_get_internal_format,
1903 .set_stencil = panfrost_resource_set_stencil,
1904 .get_stencil = panfrost_resource_get_stencil,
1905 };
1906
1907 void
panfrost_resource_screen_init(struct pipe_screen * pscreen)1908 panfrost_resource_screen_init(struct pipe_screen *pscreen)
1909 {
1910 pscreen->resource_create_with_modifiers =
1911 panfrost_resource_create_with_modifiers;
1912 pscreen->resource_create = u_transfer_helper_resource_create;
1913 pscreen->resource_destroy = u_transfer_helper_resource_destroy;
1914 pscreen->resource_from_handle = panfrost_resource_from_handle;
1915 pscreen->resource_get_handle = panfrost_resource_get_handle;
1916 pscreen->resource_get_param = panfrost_resource_get_param;
1917 pscreen->transfer_helper = u_transfer_helper_create(
1918 &transfer_vtbl,
1919 U_TRANSFER_HELPER_SEPARATE_Z32S8 | U_TRANSFER_HELPER_MSAA_MAP);
1920 }
1921 void
panfrost_resource_screen_destroy(struct pipe_screen * pscreen)1922 panfrost_resource_screen_destroy(struct pipe_screen *pscreen)
1923 {
1924 u_transfer_helper_destroy(pscreen->transfer_helper);
1925 }
1926
1927 void
panfrost_resource_context_init(struct pipe_context * pctx)1928 panfrost_resource_context_init(struct pipe_context *pctx)
1929 {
1930 pctx->buffer_map = u_transfer_helper_transfer_map;
1931 pctx->buffer_unmap = u_transfer_helper_transfer_unmap;
1932 pctx->texture_map = u_transfer_helper_transfer_map;
1933 pctx->texture_unmap = u_transfer_helper_transfer_unmap;
1934 pctx->create_surface = panfrost_create_surface;
1935 pctx->surface_destroy = panfrost_surface_destroy;
1936 pctx->resource_copy_region = util_resource_copy_region;
1937 pctx->blit = panfrost_blit;
1938 pctx->generate_mipmap = panfrost_generate_mipmap;
1939 pctx->flush_resource = panfrost_flush_resource;
1940 pctx->invalidate_resource = panfrost_invalidate_resource;
1941 pctx->transfer_flush_region = u_transfer_helper_transfer_flush_region;
1942 pctx->buffer_subdata = u_default_buffer_subdata;
1943 pctx->texture_subdata = u_default_texture_subdata;
1944 pctx->clear_buffer = u_default_clear_buffer;
1945 pctx->clear_render_target = panfrost_clear_render_target;
1946 pctx->clear_depth_stencil = panfrost_clear_depth_stencil;
1947 }
1948