1 /*
2 * Copyright (c) 2012-2015 Etnaviv Project
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sub license,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the
12 * next paragraph) shall be included in all copies or substantial portions
13 * of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
22 *
23 * Authors:
24 * Wladimir J. van der Laan <laanwj@gmail.com>
25 */
26
27 #include "etnaviv_resource.h"
28
29 #include "hw/common.xml.h"
30
31 #include "etnaviv_context.h"
32 #include "etnaviv_debug.h"
33 #include "etnaviv_screen.h"
34 #include "etnaviv_translate.h"
35
36 #include "util/hash_table.h"
37 #include "util/u_inlines.h"
38 #include "util/u_memory.h"
39
modifier_to_layout(uint64_t modifier)40 static enum etna_surface_layout modifier_to_layout(uint64_t modifier)
41 {
42 switch (modifier & ~VIVANTE_MOD_EXT_MASK) {
43 case DRM_FORMAT_MOD_VIVANTE_TILED:
44 return ETNA_LAYOUT_TILED;
45 case DRM_FORMAT_MOD_VIVANTE_SUPER_TILED:
46 return ETNA_LAYOUT_SUPER_TILED;
47 case DRM_FORMAT_MOD_VIVANTE_SPLIT_TILED:
48 return ETNA_LAYOUT_MULTI_TILED;
49 case DRM_FORMAT_MOD_VIVANTE_SPLIT_SUPER_TILED:
50 return ETNA_LAYOUT_MULTI_SUPERTILED;
51 case DRM_FORMAT_MOD_LINEAR:
52 return ETNA_LAYOUT_LINEAR;
53 default:
54 unreachable("unhandled modifier");
55 }
56 }
57
layout_to_modifier(enum etna_surface_layout layout)58 static uint64_t layout_to_modifier(enum etna_surface_layout layout)
59 {
60 switch (layout) {
61 case ETNA_LAYOUT_TILED:
62 return DRM_FORMAT_MOD_VIVANTE_TILED;
63 case ETNA_LAYOUT_SUPER_TILED:
64 return DRM_FORMAT_MOD_VIVANTE_SUPER_TILED;
65 case ETNA_LAYOUT_MULTI_TILED:
66 return DRM_FORMAT_MOD_VIVANTE_SPLIT_TILED;
67 case ETNA_LAYOUT_MULTI_SUPERTILED:
68 return DRM_FORMAT_MOD_VIVANTE_SPLIT_SUPER_TILED;
69 case ETNA_LAYOUT_LINEAR:
70 return DRM_FORMAT_MOD_LINEAR;
71 default:
72 return DRM_FORMAT_MOD_INVALID;
73 }
74 }
75
etna_resource_modifier(struct etna_resource * rsc)76 static uint64_t etna_resource_modifier(struct etna_resource *rsc)
77 {
78 if (etna_resource_ext_ts(rsc))
79 return rsc->modifier;
80
81 return layout_to_modifier(rsc->layout);
82 }
83
84 /* A tile is either 64 bytes or, when the GPU has the CACHE128B256BPERLINE
85 * feature, 128/256 bytes of color/depth data, tracked by
86 * 'screen->specs.bits_per_tile' bits of tile status.
87 */
88 bool
etna_screen_resource_alloc_ts(struct pipe_screen * pscreen,struct etna_resource * rsc,uint64_t modifier)89 etna_screen_resource_alloc_ts(struct pipe_screen *pscreen,
90 struct etna_resource *rsc, uint64_t modifier)
91 {
92 struct etna_screen *screen = etna_screen(pscreen);
93 struct etna_resource_level *lvl = &rsc->levels[0];
94 struct pipe_resource *prsc = &rsc->base;
95 size_t tile_size, ts_size, ts_bo_size, ts_layer_stride, ts_data_offset = 0;
96 uint8_t ts_mode = TS_MODE_128B;
97 int8_t ts_compress_fmt = -1;
98 unsigned layers;
99
100 assert(!rsc->ts_bo);
101
102 /* pre-v4 compression is largely useless, so disable it when not wanted for MSAA
103 * v4 compression can be enabled everywhere without any known drawback,
104 * except that in-place resolve must go through a slower path
105 */
106 if ((screen->specs.v4_compression &&
107 (!modifier || (modifier & VIVANTE_MOD_COMP_DEC400))) ||
108 (!modifier && prsc->nr_samples > 1))
109 ts_compress_fmt = translate_ts_format(prsc->format);
110
111 /* enable 256B ts mode with compression, as it improves performance
112 * the size of the resource might also determine if we want to use it or not
113 */
114 if (VIV_FEATURE(screen, chipMinorFeatures6, CACHE128B256BPERLINE)) {
115 if ((modifier & VIVANTE_MOD_TS_MASK) == VIVANTE_MOD_TS_128_4)
116 ts_mode = TS_MODE_128B;
117 else if ((modifier & VIVANTE_MOD_TS_MASK) == VIVANTE_MOD_TS_256_4)
118 ts_mode = TS_MODE_256B;
119 else {
120 /* Without a TS modifier TS is only internal, so we can choose the
121 * mode to use freely. Enable 256B ts mode with compression, as it
122 * improves performance. The size of the resource might also determine
123 * if we want to use it or not.
124 */
125 if (ts_compress_fmt >= 0 &&
126 (rsc->layout != ETNA_LAYOUT_LINEAR || lvl->stride % 256 == 0))
127 ts_mode = TS_MODE_256B;
128 else
129 ts_mode = TS_MODE_128B;
130 }
131 }
132
133 tile_size = etna_screen_get_tile_size(screen, ts_mode, prsc->nr_samples > 1);
134 layers = prsc->target == PIPE_TEXTURE_3D ? prsc->depth0 : prsc->array_size;
135 ts_layer_stride = align(DIV_ROUND_UP(lvl->layer_stride,
136 tile_size * 8 / screen->specs.bits_per_tile),
137 0x100 * screen->specs.pixel_pipes);
138 ts_size = ts_bo_size = ts_layer_stride * layers;
139 if (ts_size == 0)
140 return true;
141
142 /* add space for the software meta */
143 if (modifier & VIVANTE_MOD_TS_MASK) {
144 /* The offset is a educated guess for a safe value based on the experience
145 * that various object pointers on the GPU need to be 64B aligned. Maybe
146 * some GPU needs even more alignment, or we could drop this to 32B. 64B
147 * has worked well across various GPU generations so far.
148 */
149 ts_data_offset = 64;
150 assert(ts_data_offset >= sizeof(struct etna_ts_sw_meta));
151 ts_bo_size += ts_data_offset;
152 }
153
154 DBG_F(ETNA_DBG_RESOURCE_MSGS, "%p: Allocating tile status of size %zu",
155 rsc, ts_bo_size);
156
157 if ((rsc->base.bind & PIPE_BIND_SCANOUT) && screen->ro) {
158 struct pipe_resource scanout_templat;
159 struct winsys_handle handle;
160
161 scanout_templat.format = PIPE_FORMAT_R8_UNORM;
162 scanout_templat.width0 = align(ts_bo_size, 4096);
163 scanout_templat.height0 = 1;
164
165 rsc->ts_scanout = renderonly_scanout_for_resource(&scanout_templat,
166 screen->ro, &handle);
167 if (!rsc->ts_scanout) {
168 BUG("Problem allocating kms memory for TS resource");
169 return false;
170 }
171
172 assert(handle.type == WINSYS_HANDLE_TYPE_FD);
173 rsc->ts_bo = etna_screen_bo_from_handle(pscreen, &handle);
174 close(handle.handle);
175 } else {
176 rsc->ts_bo = etna_bo_new(screen->dev, ts_bo_size, DRM_ETNA_GEM_CACHE_WC);
177 }
178
179 if (unlikely(!rsc->ts_bo)) {
180 BUG("Problem allocating tile status for resource");
181 return false;
182 }
183
184 lvl->ts_offset = ts_data_offset;
185 lvl->ts_layer_stride = ts_layer_stride;
186 lvl->ts_size = ts_size;
187 lvl->ts_mode = ts_mode;
188 lvl->ts_compress_fmt = ts_compress_fmt;
189
190 /* fill software meta */
191 if (modifier & VIVANTE_MOD_TS_MASK) {
192 lvl->ts_meta = etna_bo_map(rsc->ts_bo);
193 memset(lvl->ts_meta, 0, sizeof(struct etna_ts_sw_meta));
194 lvl->ts_meta->version = 0;
195 lvl->ts_meta->v0.data_size = ts_size;
196 lvl->ts_meta->v0.data_offset = ts_data_offset;
197 lvl->ts_meta->v0.layer_stride = ts_layer_stride;
198 lvl->ts_meta->v0.comp_format = ts_format_to_drmfourcc(ts_compress_fmt);
199 }
200 return true;
201 }
202
203 static bool
etna_screen_can_create_resource(struct pipe_screen * pscreen,const struct pipe_resource * templat)204 etna_screen_can_create_resource(struct pipe_screen *pscreen,
205 const struct pipe_resource *templat)
206 {
207 struct etna_screen *screen = etna_screen(pscreen);
208 if (!translate_samples_to_xyscale(templat->nr_samples, NULL, NULL))
209 return false;
210
211 /* templat->bind is not set here, so we must use the minimum sizes */
212 uint max_size =
213 MIN2(screen->specs.max_rendertarget_size, screen->specs.max_texture_size);
214
215 if (templat->width0 > max_size || templat->height0 > max_size)
216 return false;
217
218 return true;
219 }
220
221 static unsigned
setup_miptree(struct etna_resource * rsc,unsigned paddingX,unsigned paddingY,unsigned msaa_xscale,unsigned msaa_yscale)222 setup_miptree(struct etna_resource *rsc, unsigned paddingX, unsigned paddingY,
223 unsigned msaa_xscale, unsigned msaa_yscale)
224 {
225 struct pipe_resource *prsc = &rsc->base;
226 unsigned level, size = 0;
227 unsigned width = prsc->width0;
228 unsigned height = prsc->height0;
229 unsigned depth = prsc->depth0;
230
231 for (level = 0; level <= prsc->last_level; level++) {
232 struct etna_resource_level *mip = &rsc->levels[level];
233
234 mip->width = width;
235 mip->height = height;
236 mip->depth = depth;
237 mip->padded_width = align(width * msaa_xscale, paddingX);
238 mip->padded_height = align(height * msaa_yscale, paddingY);
239 mip->stride = util_format_get_stride(prsc->format, mip->padded_width);
240 mip->offset = size;
241 mip->layer_stride = mip->stride * util_format_get_nblocksy(prsc->format, mip->padded_height);
242 mip->size = prsc->array_size * mip->layer_stride;
243
244 /* align levels to 64 bytes to be able to render to them */
245 size += align(mip->size, ETNA_PE_ALIGNMENT) * depth;
246
247 width = u_minify(width, 1);
248 height = u_minify(height, 1);
249 depth = u_minify(depth, 1);
250 }
251
252 return size;
253 }
254
255 /* Compute the slice/miplevel alignment (in pixels) and the texture sampler
256 * HALIGN parameter from the resource parameters and the target layout.
257 */
258 static void
etna_layout_multiple(const struct etna_screen * screen,const struct pipe_resource * templat,unsigned layout,unsigned * paddingX,unsigned * paddingY,unsigned * halign)259 etna_layout_multiple(const struct etna_screen *screen,
260 const struct pipe_resource *templat, unsigned layout,
261 unsigned *paddingX, unsigned *paddingY, unsigned *halign)
262 {
263 const struct etna_specs *specs = &screen->specs;
264 /* If we have the TEXTURE_HALIGN feature, we can always align to the resolve
265 * engine's width. If not, we must not align resources used only for
266 * textures. If this GPU uses the BLT engine, never do RS align.
267 */
268 bool rs_align = !specs->use_blt && (!etna_resource_sampler_only(templat) ||
269 VIV_FEATURE(screen, chipMinorFeatures1, TEXTURE_HALIGN));
270 int msaa_xscale = 1, msaa_yscale = 1;
271
272 /* Compressed textures are padded to their block size, but we don't have
273 * to do anything special for that.
274 */
275 if (unlikely(util_format_is_compressed(templat->format))) {
276 assert(layout == ETNA_LAYOUT_LINEAR);
277 *paddingX = 1;
278 *paddingY = 1;
279 *halign = TEXTURE_HALIGN_FOUR;
280 return;
281 }
282
283 translate_samples_to_xyscale(templat->nr_samples, &msaa_xscale, &msaa_yscale);
284
285 switch (layout) {
286 case ETNA_LAYOUT_LINEAR:
287 *paddingX = rs_align ? 16 : 4;
288 *paddingY = !specs->use_blt && templat->target != PIPE_BUFFER ? 4 : 1;
289 *halign = rs_align ? TEXTURE_HALIGN_SIXTEEN : TEXTURE_HALIGN_FOUR;
290 break;
291 case ETNA_LAYOUT_TILED:
292 *paddingX = rs_align ? 16 * msaa_xscale : 4;
293 *paddingY = 4 * msaa_yscale;
294 *halign = rs_align ? TEXTURE_HALIGN_SIXTEEN : TEXTURE_HALIGN_FOUR;
295 break;
296 case ETNA_LAYOUT_SUPER_TILED:
297 *paddingX = 64;
298 *paddingY = 64;
299 *halign = TEXTURE_HALIGN_SUPER_TILED;
300 break;
301 case ETNA_LAYOUT_MULTI_TILED:
302 *paddingX = 16 * msaa_xscale;
303 *paddingY = 4 * msaa_yscale * specs->pixel_pipes;
304 *halign = TEXTURE_HALIGN_SPLIT_TILED;
305 break;
306 case ETNA_LAYOUT_MULTI_SUPERTILED:
307 *paddingX = 64;
308 *paddingY = 64 * specs->pixel_pipes;
309 *halign = TEXTURE_HALIGN_SPLIT_SUPER_TILED;
310 break;
311 default:
312 unreachable("Unhandled layout");
313 }
314 }
315
316 /* Create a new resource object, using the given template info */
317 struct pipe_resource *
etna_resource_alloc(struct pipe_screen * pscreen,unsigned layout,uint64_t modifier,const struct pipe_resource * templat)318 etna_resource_alloc(struct pipe_screen *pscreen, unsigned layout,
319 uint64_t modifier, const struct pipe_resource *templat)
320 {
321 struct etna_screen *screen = etna_screen(pscreen);
322 struct etna_resource *rsc;
323 unsigned size;
324
325 DBG_F(ETNA_DBG_RESOURCE_MSGS,
326 "target=%d, format=%s, %ux%ux%u, array_size=%u, "
327 "last_level=%u, nr_samples=%u, usage=%u, bind=%x, flags=%x",
328 templat->target, util_format_name(templat->format), templat->width0,
329 templat->height0, templat->depth0, templat->array_size,
330 templat->last_level, templat->nr_samples, templat->usage,
331 templat->bind, templat->flags);
332
333 /* Determine scaling for antialiasing */
334 int msaa_xscale = 1, msaa_yscale = 1;
335 if (!translate_samples_to_xyscale(templat->nr_samples, &msaa_xscale, &msaa_yscale)) {
336 /* Number of samples not supported */
337 return NULL;
338 }
339
340 /* Determine needed padding (alignment of height/width) */
341 unsigned paddingX, paddingY, halign;
342 etna_layout_multiple(screen, templat, layout, &paddingX, &paddingY, &halign);
343
344 rsc = CALLOC_STRUCT(etna_resource);
345 if (!rsc)
346 return NULL;
347
348 rsc->base = *templat;
349 rsc->base.screen = pscreen;
350 rsc->base.nr_samples = templat->nr_samples;
351 rsc->layout = layout;
352 rsc->modifier = modifier;
353 rsc->halign = halign;
354 rsc->explicit_flush = true;
355
356 pipe_reference_init(&rsc->base.reference, 1);
357 util_range_init(&rsc->valid_buffer_range);
358
359 size = setup_miptree(rsc, paddingX, paddingY, msaa_xscale, msaa_yscale);
360
361 if (unlikely(templat->bind & PIPE_BIND_SCANOUT) && screen->ro) {
362 struct pipe_resource scanout_templat = *templat;
363 struct winsys_handle handle;
364
365 scanout_templat.width0 = align(scanout_templat.width0, paddingX);
366 scanout_templat.height0 = align(scanout_templat.height0, paddingY);
367
368 rsc->scanout = renderonly_scanout_for_resource(&scanout_templat,
369 screen->ro, &handle);
370 if (!rsc->scanout) {
371 BUG("Problem allocating kms memory for resource");
372 goto free_rsc;
373 }
374
375 assert(handle.type == WINSYS_HANDLE_TYPE_FD);
376 rsc->levels[0].stride = handle.stride;
377 rsc->bo = etna_screen_bo_from_handle(pscreen, &handle);
378 close(handle.handle);
379 if (unlikely(!rsc->bo))
380 goto free_rsc;
381 } else {
382 uint32_t flags = DRM_ETNA_GEM_CACHE_WC;
383
384 if (templat->bind & PIPE_BIND_VERTEX_BUFFER)
385 flags |= DRM_ETNA_GEM_FORCE_MMU;
386
387 rsc->bo = etna_bo_new(screen->dev, size, flags);
388 if (unlikely(!rsc->bo)) {
389 BUG("Problem allocating video memory for resource");
390 goto free_rsc;
391 }
392 }
393
394 /* If TS is externally visible set it up now, so it can be exported before
395 * the first rendering to a surface.
396 */
397 if (etna_resource_ext_ts(rsc))
398 etna_screen_resource_alloc_ts(pscreen, rsc, modifier);
399
400 if (DBG_ENABLED(ETNA_DBG_ZERO)) {
401 void *map = etna_bo_map(rsc->bo);
402 etna_bo_cpu_prep(rsc->bo, DRM_ETNA_PREP_WRITE);
403 memset(map, 0, size);
404 etna_bo_cpu_fini(rsc->bo);
405 }
406
407 return &rsc->base;
408
409 free_rsc:
410 FREE(rsc);
411 return NULL;
412 }
413
414 static struct pipe_resource *
etna_resource_create(struct pipe_screen * pscreen,const struct pipe_resource * templat)415 etna_resource_create(struct pipe_screen *pscreen,
416 const struct pipe_resource *templat)
417 {
418 struct etna_screen *screen = etna_screen(pscreen);
419 unsigned layout = ETNA_LAYOUT_TILED;
420
421 /* At this point we don't know if the resource will be used as a texture,
422 * render target, or both, because gallium sets the bits whenever possible
423 * This matters because on some GPUs (GC2000) there is no tiling that is
424 * compatible with both TE and PE.
425 *
426 * We expect that depth/stencil buffers will always be used by PE (rendering),
427 * and any other non-scanout resource will be used as a texture at some point,
428 * So allocate a render-compatible base buffer for scanout/depthstencil buffers,
429 * and a texture-compatible base buffer in other cases
430 *
431 */
432 if (templat->bind & PIPE_BIND_DEPTH_STENCIL) {
433 if (screen->specs.pixel_pipes > 1 && !screen->specs.single_buffer)
434 layout |= ETNA_LAYOUT_BIT_MULTI;
435 if (screen->specs.can_supertile)
436 layout |= ETNA_LAYOUT_BIT_SUPER;
437 } else if (screen->specs.can_supertile &&
438 VIV_FEATURE(screen, chipMinorFeatures2, SUPERTILED_TEXTURE) &&
439 etna_resource_hw_tileable(screen->specs.use_blt, templat)) {
440 layout |= ETNA_LAYOUT_BIT_SUPER;
441 }
442
443 if (/* MSAA render target */
444 (templat->nr_samples > 1) &&
445 (templat->bind & (PIPE_BIND_RENDER_TARGET | PIPE_BIND_DEPTH_STENCIL))) {
446 if (screen->specs.pixel_pipes > 1 && !screen->specs.single_buffer)
447 layout |= ETNA_LAYOUT_BIT_MULTI;
448 if (screen->specs.can_supertile)
449 layout |= ETNA_LAYOUT_BIT_SUPER;
450 }
451
452 if (/* linear base or scanout without modifier requested */
453 (templat->bind & (PIPE_BIND_LINEAR | PIPE_BIND_SCANOUT)) ||
454 templat->target == PIPE_BUFFER || /* buffer always linear */
455 /* compressed textures don't use tiling, they have their own "tiles" */
456 util_format_is_compressed(templat->format)) {
457 layout = ETNA_LAYOUT_LINEAR;
458 }
459
460 /* modifier is only used for scanout surfaces, so safe to use LINEAR here */
461 return etna_resource_alloc(pscreen, layout, DRM_FORMAT_MOD_LINEAR, templat);
462 }
463
464 enum modifier_priority {
465 MODIFIER_PRIORITY_INVALID = 0,
466 MODIFIER_PRIORITY_LINEAR,
467 MODIFIER_PRIORITY_SPLIT_TILED,
468 MODIFIER_PRIORITY_SPLIT_SUPER_TILED,
469 MODIFIER_PRIORITY_TILED,
470 MODIFIER_PRIORITY_SUPER_TILED,
471 };
472
473 static const uint64_t priority_to_modifier[] = {
474 [MODIFIER_PRIORITY_INVALID] = DRM_FORMAT_MOD_INVALID,
475 [MODIFIER_PRIORITY_LINEAR] = DRM_FORMAT_MOD_LINEAR,
476 [MODIFIER_PRIORITY_SPLIT_TILED] = DRM_FORMAT_MOD_VIVANTE_SPLIT_TILED,
477 [MODIFIER_PRIORITY_SPLIT_SUPER_TILED] = DRM_FORMAT_MOD_VIVANTE_SPLIT_SUPER_TILED,
478 [MODIFIER_PRIORITY_TILED] = DRM_FORMAT_MOD_VIVANTE_TILED,
479 [MODIFIER_PRIORITY_SUPER_TILED] = DRM_FORMAT_MOD_VIVANTE_SUPER_TILED,
480 };
481
482 static uint64_t
select_best_modifier(const struct etna_screen * screen,const uint64_t * modifiers,const unsigned count)483 select_best_modifier(const struct etna_screen * screen,
484 const uint64_t *modifiers, const unsigned count)
485 {
486 enum modifier_priority prio = MODIFIER_PRIORITY_INVALID;
487 uint64_t best_modifier, base_modifier;
488
489 for (int i = 0; i < count; i++) {
490 switch (modifiers[i] & ~VIVANTE_MOD_EXT_MASK) {
491 case DRM_FORMAT_MOD_VIVANTE_SUPER_TILED:
492 if ((screen->specs.pixel_pipes > 1 && !screen->specs.single_buffer) ||
493 !screen->specs.can_supertile)
494 break;
495 prio = MAX2(prio, MODIFIER_PRIORITY_SUPER_TILED);
496 break;
497 case DRM_FORMAT_MOD_VIVANTE_TILED:
498 if (screen->specs.pixel_pipes > 1 && !screen->specs.single_buffer)
499 break;
500 prio = MAX2(prio, MODIFIER_PRIORITY_TILED);
501 break;
502 case DRM_FORMAT_MOD_VIVANTE_SPLIT_SUPER_TILED:
503 if ((screen->specs.pixel_pipes < 2) || !screen->specs.can_supertile)
504 break;
505 prio = MAX2(prio, MODIFIER_PRIORITY_SPLIT_SUPER_TILED);
506 break;
507 case DRM_FORMAT_MOD_VIVANTE_SPLIT_TILED:
508 if (screen->specs.pixel_pipes < 2)
509 break;
510 prio = MAX2(prio, MODIFIER_PRIORITY_SPLIT_TILED);
511 break;
512 case DRM_FORMAT_MOD_LINEAR:
513 prio = MAX2(prio, MODIFIER_PRIORITY_LINEAR);
514 break;
515 case DRM_FORMAT_MOD_INVALID:
516 default:
517 break;
518 }
519 }
520
521 best_modifier = base_modifier = priority_to_modifier[prio];
522
523 if (!DBG_ENABLED(ETNA_DBG_SHARED_TS) ||
524 !VIV_FEATURE(screen, chipFeatures, FAST_CLEAR))
525 return best_modifier;
526
527 /* Make a second pass to try and find the best TS modifier if any. */
528 for (int i = 0; i < count; i++) {
529 if ((modifiers[i] & ~VIVANTE_MOD_EXT_MASK) == base_modifier)
530 if ((modifiers[i] & VIVANTE_MOD_TS_MASK) >
531 (best_modifier & VIVANTE_MOD_TS_MASK))
532 best_modifier = modifiers[i];
533 }
534
535 /* If no modifier with TS was found, there is no point in looking further,
536 * as compression depends on TS.
537 */
538 if (best_modifier == base_modifier)
539 return best_modifier;
540
541 /* Make a third pass to find a modifier allowing compression. */
542 base_modifier = best_modifier;
543 for (int i = 0; i < count; i++) {
544 if ((modifiers[i] & ~VIVANTE_MOD_COMP_MASK) == base_modifier)
545 if ((modifiers[i] & VIVANTE_MOD_COMP_MASK) >
546 (best_modifier & VIVANTE_MOD_COMP_MASK))
547 best_modifier = modifiers[i];
548 }
549
550 return best_modifier;
551 }
552
553 static struct pipe_resource *
etna_resource_create_modifiers(struct pipe_screen * pscreen,const struct pipe_resource * templat,const uint64_t * modifiers,int count)554 etna_resource_create_modifiers(struct pipe_screen *pscreen,
555 const struct pipe_resource *templat,
556 const uint64_t *modifiers, int count)
557 {
558 struct etna_screen *screen = etna_screen(pscreen);
559 struct pipe_resource tmpl = *templat;
560 uint64_t modifier = select_best_modifier(screen, modifiers, count);
561
562 if (modifier == DRM_FORMAT_MOD_INVALID)
563 return NULL;
564
565 return etna_resource_alloc(pscreen, modifier_to_layout(modifier), modifier, &tmpl);
566 }
567
568 static void
etna_resource_changed(struct pipe_screen * pscreen,struct pipe_resource * prsc)569 etna_resource_changed(struct pipe_screen *pscreen, struct pipe_resource *prsc)
570 {
571 struct etna_resource *rsc = etna_resource(prsc);
572
573 for (int level = 0; level <= prsc->last_level; level++)
574 etna_resource_level_mark_changed(&rsc->levels[level]);
575 }
576
577 static void
etna_resource_destroy(struct pipe_screen * pscreen,struct pipe_resource * prsc)578 etna_resource_destroy(struct pipe_screen *pscreen, struct pipe_resource *prsc)
579 {
580 struct etna_resource *rsc = etna_resource(prsc);
581
582 if (rsc->bo)
583 etna_bo_del(rsc->bo);
584
585 if (rsc->ts_bo)
586 etna_bo_del(rsc->ts_bo);
587
588 if (rsc->scanout)
589 renderonly_scanout_destroy(rsc->scanout, etna_screen(pscreen)->ro);
590
591 if (rsc->ts_scanout)
592 renderonly_scanout_destroy(rsc->ts_scanout, etna_screen(pscreen)->ro);
593
594 util_range_destroy(&rsc->valid_buffer_range);
595
596 pipe_resource_reference(&rsc->texture, NULL);
597 pipe_resource_reference(&rsc->render, NULL);
598
599 for (unsigned i = 0; i < ETNA_NUM_LOD; i++)
600 FREE(rsc->levels[i].patch_offsets);
601
602 FREE(rsc);
603 }
604
etna_resource_finish_ts_import(struct etna_screen * screen,struct etna_resource * rsc)605 static void etna_resource_finish_ts_import(struct etna_screen *screen,
606 struct etna_resource *rsc)
607 {
608 struct etna_resource *ts_rsc = etna_resource(rsc->base.next);
609 uint64_t ts_modifier = rsc->modifier & VIVANTE_MOD_TS_MASK;
610 struct etna_resource_level *lvl = &rsc->levels[0];
611 uint8_t ts_mode = 0;
612
613 if (ts_rsc->bo == rsc->bo)
614 fprintf(stderr, "etnaviv: application bug: importing shared TS resource "
615 "with TS BO matching color BO, expect rendering corruption!\n");
616
617 if (ts_modifier == VIVANTE_MOD_TS_256_4)
618 ts_mode = TS_MODE_256B;
619
620 rsc->ts_bo = etna_bo_ref(ts_rsc->bo);
621
622 rsc->ts_scanout = ts_rsc->scanout;
623 ts_rsc->scanout = NULL;
624
625 /* Get TS layout/usage information from the SW meta. FIXME: clear color may
626 * change over the lifetime of the resource, so might need to look this up
627 * at a few other places. For now it's not an issue, as buffers with shared
628 * TS get re-imported all the time. */
629 lvl->ts_meta = etna_bo_map(rsc->ts_bo) + ts_rsc->levels[0].offset;
630 lvl->ts_compress_fmt = drmfourcc_to_ts_format(lvl->ts_meta->v0.comp_format);
631 lvl->ts_offset = ts_rsc->levels[0].offset + lvl->ts_meta->v0.data_offset;
632 lvl->ts_layer_stride = lvl->ts_meta->v0.layer_stride;
633 lvl->clear_value = lvl->ts_meta->v0.clear_value;
634 lvl->ts_size = lvl->ts_meta->v0.data_size;
635 lvl->ts_mode = ts_mode;
636
637 etna_resource_destroy(&screen->base, rsc->base.next);
638 rsc->base.next = NULL;
639 }
640
641 static struct pipe_resource *
etna_resource_from_handle(struct pipe_screen * pscreen,const struct pipe_resource * tmpl,struct winsys_handle * handle,unsigned usage)642 etna_resource_from_handle(struct pipe_screen *pscreen,
643 const struct pipe_resource *tmpl,
644 struct winsys_handle *handle, unsigned usage)
645 {
646 struct etna_screen *screen = etna_screen(pscreen);
647 struct etna_resource *rsc;
648 struct etna_resource_level *level;
649 struct pipe_resource *prsc;
650 uint64_t modifier = handle->modifier;
651
652 DBG("target=%d, format=%s, %ux%ux%u, array_size=%u, last_level=%u, "
653 "nr_samples=%u, usage=%u, bind=%x, flags=%x",
654 tmpl->target, util_format_name(tmpl->format), tmpl->width0,
655 tmpl->height0, tmpl->depth0, tmpl->array_size, tmpl->last_level,
656 tmpl->nr_samples, tmpl->usage, tmpl->bind, tmpl->flags);
657
658 rsc = CALLOC_STRUCT(etna_resource);
659 if (!rsc)
660 return NULL;
661
662 level = &rsc->levels[0];
663 prsc = &rsc->base;
664
665 *prsc = *tmpl;
666
667 pipe_reference_init(&prsc->reference, 1);
668 util_range_init(&rsc->valid_buffer_range);
669 prsc->screen = pscreen;
670
671 rsc->bo = etna_screen_bo_from_handle(pscreen, handle);
672 if (!rsc->bo)
673 goto fail;
674
675 if (modifier == DRM_FORMAT_MOD_INVALID)
676 modifier = DRM_FORMAT_MOD_LINEAR;
677
678 rsc->layout = modifier_to_layout(modifier);
679 rsc->modifier = modifier;
680
681 rsc->shared = true;
682 if (usage & PIPE_HANDLE_USAGE_EXPLICIT_FLUSH)
683 rsc->explicit_flush = true;
684
685 level->width = tmpl->width0;
686 level->height = tmpl->height0;
687 level->depth = tmpl->depth0;
688 level->stride = handle->stride;
689 level->offset = handle->offset;
690 level->seqno = 1;
691
692 /* Determine padding of the imported resource. */
693 unsigned paddingX, paddingY;
694 etna_layout_multiple(screen, tmpl, rsc->layout,
695 &paddingX, &paddingY, &rsc->halign);
696
697 level->padded_width = align(level->width, paddingX);
698 level->padded_height = align(level->height, paddingY);
699
700 level->layer_stride = level->stride * util_format_get_nblocksy(prsc->format,
701 level->padded_height);
702 level->size = level->layer_stride;
703
704 if (screen->ro)
705 rsc->scanout = renderonly_create_gpu_import_for_resource(prsc, screen->ro,
706 NULL);
707
708 /* If the buffer is for a TS plane, skip the RS compatible checks */
709 if (handle->plane >= util_format_get_num_planes(prsc->format))
710 return prsc;
711
712 /* The DDX must give us a BO which conforms to our padding size.
713 * The stride of the BO must be greater or equal to our padded
714 * stride. The size of the BO must accomodate the padded height. */
715 if (level->stride < util_format_get_stride(tmpl->format, level->padded_width)) {
716 BUG("BO stride %u is too small for RS engine width padding (%u, format %s)",
717 level->stride, util_format_get_stride(tmpl->format, level->padded_width),
718 util_format_name(tmpl->format));
719 goto fail;
720 }
721 if (etna_bo_size(rsc->bo) < level->stride * level->padded_height) {
722 BUG("BO size %u is too small for RS engine height padding (%u, format %s)",
723 etna_bo_size(rsc->bo), level->stride * level->padded_height,
724 util_format_name(tmpl->format));
725 goto fail;
726 }
727
728 if (handle->plane == 0 && etna_resource_ext_ts(rsc))
729 etna_resource_finish_ts_import(screen, rsc);
730
731 return prsc;
732
733 fail:
734 etna_resource_destroy(pscreen, prsc);
735
736 return NULL;
737 }
738
739 static bool
etna_resource_get_handle(struct pipe_screen * pscreen,struct pipe_context * pctx,struct pipe_resource * prsc,struct winsys_handle * handle,unsigned usage)740 etna_resource_get_handle(struct pipe_screen *pscreen,
741 struct pipe_context *pctx,
742 struct pipe_resource *prsc,
743 struct winsys_handle *handle, unsigned usage)
744 {
745 struct etna_screen *screen = etna_screen(pscreen);
746 struct etna_resource *rsc = etna_resource(prsc);
747 bool wants_ts = etna_resource_ext_ts(rsc) &&
748 handle->plane >= util_format_get_num_planes(prsc->format);
749 struct renderonly_scanout *scanout;
750 struct etna_resource_level *lvl;
751 struct etna_bo *bo;
752
753 if (handle->plane && !wants_ts) {
754 struct pipe_resource *cur = prsc;
755
756 for (int i = 0; i < handle->plane; i++) {
757 cur = cur->next;
758 if (!cur)
759 return false;
760 }
761 rsc = etna_resource(cur);
762 }
763
764 /* Scanout is always attached to the base resource */
765 scanout = rsc->scanout;
766
767 lvl = &rsc->levels[0];
768
769 if (wants_ts) {
770 handle->stride = DIV_ROUND_UP(lvl->stride,
771 etna_screen_get_tile_size(screen, lvl->ts_mode, false)
772 * 8 / screen->specs.bits_per_tile);
773 handle->offset = lvl->ts_offset - lvl->ts_meta->v0.data_offset;
774 scanout = rsc->ts_scanout;
775 bo = rsc->ts_bo;
776 } else {
777 handle->stride = lvl->stride;
778 handle->offset = lvl->offset;
779 scanout = rsc->scanout;
780 bo = rsc->bo;
781 }
782 handle->modifier = etna_resource_modifier(rsc);
783
784 rsc->shared = true;
785 if (!(usage & PIPE_HANDLE_USAGE_EXPLICIT_FLUSH))
786 rsc->explicit_flush = false;
787
788 if (handle->type == WINSYS_HANDLE_TYPE_SHARED) {
789 return etna_bo_get_name(bo, &handle->handle) == 0;
790 } else if (handle->type == WINSYS_HANDLE_TYPE_KMS) {
791 if (screen->ro) {
792 return renderonly_get_handle(scanout, handle);
793 } else {
794 handle->handle = etna_bo_handle(bo);
795 return true;
796 }
797 } else if (handle->type == WINSYS_HANDLE_TYPE_FD) {
798 handle->handle = etna_bo_dmabuf(bo);
799 return true;
800 } else {
801 return false;
802 }
803 }
804
805 static bool
etna_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)806 etna_resource_get_param(struct pipe_screen *pscreen,
807 struct pipe_context *pctx, struct pipe_resource *prsc,
808 unsigned plane, unsigned layer, unsigned level,
809 enum pipe_resource_param param,
810 unsigned usage, uint64_t *value)
811 {
812 struct etna_screen *screen = etna_screen(pscreen);
813 struct etna_resource *rsc = etna_resource(prsc);
814 bool wants_ts = etna_resource_ext_ts(rsc) &&
815 plane >= util_format_get_num_planes(prsc->format);
816 struct etna_resource_level *lvl;
817
818 if (param == PIPE_RESOURCE_PARAM_NPLANES) {
819 if (etna_resource_ext_ts(rsc)) {
820 *value = 2;
821 } else {
822 unsigned count = 0;
823
824 for (struct pipe_resource *cur = prsc; cur; cur = cur->next)
825 count++;
826 *value = count;
827 }
828 return true;
829 }
830
831 if (!wants_ts) {
832 struct pipe_resource *cur = prsc;
833 for (int i = 0; i < plane; i++) {
834 cur = cur->next;
835 if (!cur)
836 return false;
837 }
838 rsc = etna_resource(cur);
839 }
840
841 lvl = &rsc->levels[0];
842
843 switch (param) {
844 case PIPE_RESOURCE_PARAM_STRIDE:
845 if (wants_ts) {
846 *value = DIV_ROUND_UP(lvl->stride,
847 etna_screen_get_tile_size(screen, lvl->ts_mode,
848 prsc->nr_samples > 1)
849 * 8 / screen->specs.bits_per_tile);
850 } else {
851 *value = lvl->stride;
852 }
853 return true;
854 case PIPE_RESOURCE_PARAM_OFFSET:
855 if (wants_ts)
856 *value = lvl->ts_offset - lvl->ts_meta->v0.data_offset;
857 else
858 *value = lvl->offset;
859 return true;
860 case PIPE_RESOURCE_PARAM_MODIFIER:
861 *value = etna_resource_modifier(rsc);
862 return true;
863 default:
864 return false;
865 }
866 }
867
868 void
etna_resource_used(struct etna_context * ctx,struct pipe_resource * prsc,enum etna_resource_status status)869 etna_resource_used(struct etna_context *ctx, struct pipe_resource *prsc,
870 enum etna_resource_status status)
871 {
872 struct etna_resource *rsc;
873 struct hash_entry *entry;
874 uint32_t hash;
875
876 if (!prsc)
877 return;
878
879 rsc = etna_resource(prsc);
880 hash = _mesa_hash_pointer(rsc);
881 entry = _mesa_hash_table_search_pre_hashed(ctx->pending_resources,
882 hash, rsc);
883
884 if (entry) {
885 enum etna_resource_status tmp = (uintptr_t)entry->data;
886 tmp |= status;
887 entry->data = (void *)(uintptr_t)tmp;
888 } else {
889 _mesa_hash_table_insert_pre_hashed(ctx->pending_resources, hash, rsc,
890 (void *)(uintptr_t)status);
891 }
892 }
893
894 enum etna_resource_status
etna_resource_status(struct etna_context * ctx,struct etna_resource * res)895 etna_resource_status(struct etna_context *ctx, struct etna_resource *res)
896 {
897 struct hash_entry *entry = _mesa_hash_table_search(ctx->pending_resources, res);
898
899 if (entry)
900 return (enum etna_resource_status)(uintptr_t)entry->data;
901 else
902 return 0;
903 }
904
905 bool
etna_resource_needs_flush(struct etna_resource * rsc)906 etna_resource_needs_flush(struct etna_resource *rsc)
907 {
908 if (!rsc->ts_bo)
909 return false;
910
911 for (int level = 0; level <= rsc->base.last_level; level++)
912 if (etna_resource_level_needs_flush(&rsc->levels[level]))
913 return true;
914
915 return false;
916 }
917
918 void
etna_resource_screen_init(struct pipe_screen * pscreen)919 etna_resource_screen_init(struct pipe_screen *pscreen)
920 {
921 pscreen->can_create_resource = etna_screen_can_create_resource;
922 pscreen->resource_create = etna_resource_create;
923 pscreen->resource_create_with_modifiers = etna_resource_create_modifiers;
924 pscreen->resource_from_handle = etna_resource_from_handle;
925 pscreen->resource_get_handle = etna_resource_get_handle;
926 pscreen->resource_get_param = etna_resource_get_param;
927 pscreen->resource_changed = etna_resource_changed;
928 pscreen->resource_destroy = etna_resource_destroy;
929 }
930