b03681cc5c0c7d0881b55e80ee4b5b1203246d29
[mesa.git] / src / gallium / drivers / etnaviv / etnaviv_resource.c
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
40 #include "drm-uapi/drm_fourcc.h"
41
42 static enum etna_surface_layout modifier_to_layout(uint64_t modifier)
43 {
44 switch (modifier) {
45 case DRM_FORMAT_MOD_VIVANTE_TILED:
46 return ETNA_LAYOUT_TILED;
47 case DRM_FORMAT_MOD_VIVANTE_SUPER_TILED:
48 return ETNA_LAYOUT_SUPER_TILED;
49 case DRM_FORMAT_MOD_VIVANTE_SPLIT_TILED:
50 return ETNA_LAYOUT_MULTI_TILED;
51 case DRM_FORMAT_MOD_VIVANTE_SPLIT_SUPER_TILED:
52 return ETNA_LAYOUT_MULTI_SUPERTILED;
53 case DRM_FORMAT_MOD_LINEAR:
54 default:
55 return ETNA_LAYOUT_LINEAR;
56 }
57 }
58
59 static uint64_t layout_to_modifier(enum etna_surface_layout layout)
60 {
61 switch (layout) {
62 case ETNA_LAYOUT_TILED:
63 return DRM_FORMAT_MOD_VIVANTE_TILED;
64 case ETNA_LAYOUT_SUPER_TILED:
65 return DRM_FORMAT_MOD_VIVANTE_SUPER_TILED;
66 case ETNA_LAYOUT_MULTI_TILED:
67 return DRM_FORMAT_MOD_VIVANTE_SPLIT_TILED;
68 case ETNA_LAYOUT_MULTI_SUPERTILED:
69 return DRM_FORMAT_MOD_VIVANTE_SPLIT_SUPER_TILED;
70 case ETNA_LAYOUT_LINEAR:
71 return DRM_FORMAT_MOD_LINEAR;
72 default:
73 return DRM_FORMAT_MOD_INVALID;
74 }
75 }
76
77 /* A tile is 4x4 pixels, having 'screen->specs.bits_per_tile' of tile status.
78 * So, in a buffer of N pixels, there are N / (4 * 4) tiles.
79 * We need N * screen->specs.bits_per_tile / (4 * 4) bits of tile status, or
80 * N * screen->specs.bits_per_tile / (4 * 4 * 8) bytes.
81 */
82 bool
83 etna_screen_resource_alloc_ts(struct pipe_screen *pscreen,
84 struct etna_resource *rsc)
85 {
86 struct etna_screen *screen = etna_screen(pscreen);
87 size_t rt_ts_size, ts_layer_stride;
88 size_t ts_bits_per_tile, bytes_per_tile;
89 uint8_t ts_mode = TS_MODE_128B; /* only used by halti5 */
90 int8_t ts_compress_fmt;
91
92 assert(!rsc->ts_bo);
93
94 /* pre-v4 compression is largely useless, so disable it when not wanted for MSAA
95 * v4 compression can be enabled everywhere without any known drawback,
96 * except that in-place resolve must go through a slower path
97 */
98 ts_compress_fmt = (screen->specs.v4_compression || rsc->base.nr_samples > 1) ?
99 translate_ts_format(rsc->base.format) : -1;
100
101 if (screen->specs.halti >= 5) {
102 /* enable 256B ts mode with compression, as it improves performance
103 * the size of the resource might also determine if we want to use it or not
104 */
105 if (ts_compress_fmt >= 0)
106 ts_mode = TS_MODE_256B;
107
108 ts_bits_per_tile = 4;
109 bytes_per_tile = ts_mode == TS_MODE_256B ? 256 : 128;
110 } else {
111 ts_bits_per_tile = screen->specs.bits_per_tile;
112 bytes_per_tile = 64;
113 }
114
115 ts_layer_stride = align(DIV_ROUND_UP(rsc->levels[0].layer_stride,
116 bytes_per_tile * 8 / ts_bits_per_tile),
117 0x100 * screen->specs.pixel_pipes);
118 rt_ts_size = ts_layer_stride * rsc->base.array_size;
119 if (rt_ts_size == 0)
120 return true;
121
122 DBG_F(ETNA_DBG_RESOURCE_MSGS, "%p: Allocating tile status of size %zu",
123 rsc, rt_ts_size);
124
125 struct etna_bo *rt_ts;
126 rt_ts = etna_bo_new(screen->dev, rt_ts_size, DRM_ETNA_GEM_CACHE_WC);
127
128 if (unlikely(!rt_ts)) {
129 BUG("Problem allocating tile status for resource");
130 return false;
131 }
132
133 rsc->ts_bo = rt_ts;
134 rsc->levels[0].ts_offset = 0;
135 rsc->levels[0].ts_layer_stride = ts_layer_stride;
136 rsc->levels[0].ts_size = rt_ts_size;
137 rsc->levels[0].ts_mode = ts_mode;
138 rsc->levels[0].ts_compress_fmt = ts_compress_fmt;
139
140 return true;
141 }
142
143 static bool
144 etna_screen_can_create_resource(struct pipe_screen *pscreen,
145 const struct pipe_resource *templat)
146 {
147 struct etna_screen *screen = etna_screen(pscreen);
148 if (!translate_samples_to_xyscale(templat->nr_samples, NULL, NULL, NULL))
149 return false;
150
151 /* templat->bind is not set here, so we must use the minimum sizes */
152 uint max_size =
153 MIN2(screen->specs.max_rendertarget_size, screen->specs.max_texture_size);
154
155 if (templat->width0 > max_size || templat->height0 > max_size)
156 return false;
157
158 return true;
159 }
160
161 static unsigned
162 setup_miptree(struct etna_resource *rsc, unsigned paddingX, unsigned paddingY,
163 unsigned msaa_xscale, unsigned msaa_yscale)
164 {
165 struct pipe_resource *prsc = &rsc->base;
166 unsigned level, size = 0;
167 unsigned width = prsc->width0;
168 unsigned height = prsc->height0;
169 unsigned depth = prsc->depth0;
170
171 for (level = 0; level <= prsc->last_level; level++) {
172 struct etna_resource_level *mip = &rsc->levels[level];
173
174 mip->width = width;
175 mip->height = height;
176 mip->depth = depth;
177 mip->padded_width = align(width * msaa_xscale, paddingX);
178 mip->padded_height = align(height * msaa_yscale, paddingY);
179 mip->stride = util_format_get_stride(prsc->format, mip->padded_width);
180 mip->offset = size;
181 mip->layer_stride = mip->stride * util_format_get_nblocksy(prsc->format, mip->padded_height);
182 mip->size = prsc->array_size * mip->layer_stride;
183
184 /* align levels to 64 bytes to be able to render to them */
185 size += align(mip->size, ETNA_PE_ALIGNMENT) * depth;
186
187 width = u_minify(width, 1);
188 height = u_minify(height, 1);
189 depth = u_minify(depth, 1);
190 }
191
192 return size;
193 }
194
195 /* Is rs alignment needed? */
196 static bool is_rs_align(struct etna_screen *screen,
197 const struct pipe_resource *tmpl)
198 {
199 return screen->specs.use_blt ? false : (
200 VIV_FEATURE(screen, chipMinorFeatures1, TEXTURE_HALIGN) ||
201 !etna_resource_sampler_only(tmpl));
202 }
203
204 /* Create a new resource object, using the given template info */
205 struct pipe_resource *
206 etna_resource_alloc(struct pipe_screen *pscreen, unsigned layout,
207 enum etna_resource_addressing_mode mode, uint64_t modifier,
208 const struct pipe_resource *templat)
209 {
210 struct etna_screen *screen = etna_screen(pscreen);
211 struct etna_resource *rsc;
212 unsigned size;
213
214 DBG_F(ETNA_DBG_RESOURCE_MSGS,
215 "target=%d, format=%s, %ux%ux%u, array_size=%u, "
216 "last_level=%u, nr_samples=%u, usage=%u, bind=%x, flags=%x",
217 templat->target, util_format_name(templat->format), templat->width0,
218 templat->height0, templat->depth0, templat->array_size,
219 templat->last_level, templat->nr_samples, templat->usage,
220 templat->bind, templat->flags);
221
222 /* Determine scaling for antialiasing, allow override using debug flag */
223 int nr_samples = templat->nr_samples;
224 if ((templat->bind & (PIPE_BIND_RENDER_TARGET | PIPE_BIND_DEPTH_STENCIL)) &&
225 !(templat->bind & PIPE_BIND_SAMPLER_VIEW)) {
226 if (DBG_ENABLED(ETNA_DBG_MSAA_2X))
227 nr_samples = 2;
228 if (DBG_ENABLED(ETNA_DBG_MSAA_4X))
229 nr_samples = 4;
230 }
231
232 int msaa_xscale = 1, msaa_yscale = 1;
233 if (!translate_samples_to_xyscale(nr_samples, &msaa_xscale, &msaa_yscale, NULL)) {
234 /* Number of samples not supported */
235 return NULL;
236 }
237
238 /* Determine needed padding (alignment of height/width) */
239 unsigned paddingX = 0, paddingY = 0;
240 unsigned halign = TEXTURE_HALIGN_FOUR;
241 if (!util_format_is_compressed(templat->format)) {
242 /* If we have the TEXTURE_HALIGN feature, we can always align to the
243 * resolve engine's width. If not, we must not align resources used
244 * only for textures. If this GPU uses the BLT engine, never do RS align.
245 */
246 etna_layout_multiple(layout, screen->specs.pixel_pipes,
247 is_rs_align (screen, templat),
248 &paddingX, &paddingY, &halign);
249 assert(paddingX && paddingY);
250 } else {
251 /* Compressed textures are padded to their block size, but we don't have
252 * to do anything special for that. */
253 paddingX = 1;
254 paddingY = 1;
255 }
256
257 if (!screen->specs.use_blt && templat->target != PIPE_BUFFER && layout == ETNA_LAYOUT_LINEAR)
258 paddingY = align(paddingY, ETNA_RS_HEIGHT_MASK + 1);
259
260 if (templat->bind & PIPE_BIND_SCANOUT && screen->ro->kms_fd >= 0) {
261 struct pipe_resource scanout_templat = *templat;
262 struct renderonly_scanout *scanout;
263 struct winsys_handle handle;
264
265 /* pad scanout buffer size to be compatible with the RS */
266 if (!screen->specs.use_blt && modifier == DRM_FORMAT_MOD_LINEAR) {
267 paddingX = align(paddingX, ETNA_RS_WIDTH_MASK + 1);
268 paddingY = align(paddingY, ETNA_RS_HEIGHT_MASK + 1);
269 }
270
271 scanout_templat.width0 = align(scanout_templat.width0, paddingX);
272 scanout_templat.height0 = align(scanout_templat.height0, paddingY);
273
274 scanout = renderonly_scanout_for_resource(&scanout_templat,
275 screen->ro, &handle);
276 if (!scanout)
277 return NULL;
278
279 assert(handle.type == WINSYS_HANDLE_TYPE_FD);
280 handle.modifier = modifier;
281 rsc = etna_resource(pscreen->resource_from_handle(pscreen, templat,
282 &handle,
283 PIPE_HANDLE_USAGE_FRAMEBUFFER_WRITE));
284 close(handle.handle);
285 if (!rsc)
286 return NULL;
287
288 rsc->scanout = scanout;
289
290 return &rsc->base;
291 }
292
293 rsc = CALLOC_STRUCT(etna_resource);
294 if (!rsc)
295 return NULL;
296
297 rsc->base = *templat;
298 rsc->base.screen = pscreen;
299 rsc->base.nr_samples = nr_samples;
300 rsc->layout = layout;
301 rsc->halign = halign;
302 rsc->addressing_mode = mode;
303
304 pipe_reference_init(&rsc->base.reference, 1);
305
306 size = setup_miptree(rsc, paddingX, paddingY, msaa_xscale, msaa_yscale);
307
308 uint32_t flags = DRM_ETNA_GEM_CACHE_WC;
309 if (templat->bind & PIPE_BIND_VERTEX_BUFFER)
310 flags |= DRM_ETNA_GEM_FORCE_MMU;
311 struct etna_bo *bo = etna_bo_new(screen->dev, size, flags);
312 if (unlikely(bo == NULL)) {
313 BUG("Problem allocating video memory for resource");
314 goto free_rsc;
315 }
316
317 rsc->bo = bo;
318 rsc->ts_bo = 0; /* TS is only created when first bound to surface */
319
320 if (DBG_ENABLED(ETNA_DBG_ZERO)) {
321 void *map = etna_bo_map(bo);
322 memset(map, 0, size);
323 }
324
325 rsc->pending_ctx = _mesa_set_create(NULL, _mesa_hash_pointer,
326 _mesa_key_pointer_equal);
327 if (!rsc->pending_ctx)
328 goto free_rsc;
329
330 return &rsc->base;
331
332 free_rsc:
333 FREE(rsc);
334 return NULL;
335 }
336
337 static struct pipe_resource *
338 etna_resource_create(struct pipe_screen *pscreen,
339 const struct pipe_resource *templat)
340 {
341 struct etna_screen *screen = etna_screen(pscreen);
342
343 /* Figure out what tiling and address mode to use -- for now, assume that
344 * texture cannot be linear. there is a capability LINEAR_TEXTURE_SUPPORT
345 * (supported on gc880 and gc2000 at least), but not sure how it works.
346 * Buffers always have LINEAR layout.
347 */
348 unsigned layout = ETNA_LAYOUT_LINEAR;
349 enum etna_resource_addressing_mode mode = ETNA_ADDRESSING_MODE_TILED;
350
351 if (etna_resource_sampler_only(templat)) {
352 /* The buffer is only used for texturing, so create something
353 * directly compatible with the sampler. Such a buffer can
354 * never be rendered to. */
355 layout = ETNA_LAYOUT_TILED;
356
357 if (util_format_is_compressed(templat->format))
358 layout = ETNA_LAYOUT_LINEAR;
359 } else if (templat->target != PIPE_BUFFER) {
360 bool want_multitiled = false;
361 bool want_supertiled = screen->specs.can_supertile;
362
363 /* When this GPU supports single-buffer rendering, don't ever enable
364 * multi-tiling. This replicates the blob behavior on GC3000.
365 */
366 if (!screen->specs.single_buffer)
367 want_multitiled = screen->specs.pixel_pipes > 1;
368
369 /* Keep single byte blocksized resources as tiled, since we
370 * are unable to use the RS blit to de-tile them. However,
371 * if they're used as a render target or depth/stencil, they
372 * must be multi-tiled for GPUs with multiple pixel pipes.
373 * Ignore depth/stencil here, but it is an error for a render
374 * target.
375 */
376 if (util_format_get_blocksize(templat->format) == 1 &&
377 !(templat->bind & PIPE_BIND_DEPTH_STENCIL)) {
378 assert(!(templat->bind & PIPE_BIND_RENDER_TARGET && want_multitiled));
379 want_multitiled = want_supertiled = false;
380 }
381
382 layout = ETNA_LAYOUT_BIT_TILE;
383 if (want_multitiled)
384 layout |= ETNA_LAYOUT_BIT_MULTI;
385 if (want_supertiled)
386 layout |= ETNA_LAYOUT_BIT_SUPER;
387 }
388
389 if (templat->target == PIPE_TEXTURE_3D)
390 layout = ETNA_LAYOUT_LINEAR;
391
392 /* modifier is only used for scanout surfaces, so safe to use LINEAR here */
393 return etna_resource_alloc(pscreen, layout, mode, DRM_FORMAT_MOD_LINEAR, templat);
394 }
395
396 enum modifier_priority {
397 MODIFIER_PRIORITY_INVALID = 0,
398 MODIFIER_PRIORITY_LINEAR,
399 MODIFIER_PRIORITY_SPLIT_TILED,
400 MODIFIER_PRIORITY_SPLIT_SUPER_TILED,
401 MODIFIER_PRIORITY_TILED,
402 MODIFIER_PRIORITY_SUPER_TILED,
403 };
404
405 const uint64_t priority_to_modifier[] = {
406 [MODIFIER_PRIORITY_INVALID] = DRM_FORMAT_MOD_INVALID,
407 [MODIFIER_PRIORITY_LINEAR] = DRM_FORMAT_MOD_LINEAR,
408 [MODIFIER_PRIORITY_SPLIT_TILED] = DRM_FORMAT_MOD_VIVANTE_SPLIT_TILED,
409 [MODIFIER_PRIORITY_SPLIT_SUPER_TILED] = DRM_FORMAT_MOD_VIVANTE_SPLIT_SUPER_TILED,
410 [MODIFIER_PRIORITY_TILED] = DRM_FORMAT_MOD_VIVANTE_TILED,
411 [MODIFIER_PRIORITY_SUPER_TILED] = DRM_FORMAT_MOD_VIVANTE_SUPER_TILED,
412 };
413
414 static uint64_t
415 select_best_modifier(const struct etna_screen * screen,
416 const uint64_t *modifiers, const unsigned count)
417 {
418 enum modifier_priority prio = MODIFIER_PRIORITY_INVALID;
419
420 for (int i = 0; i < count; i++) {
421 switch (modifiers[i]) {
422 case DRM_FORMAT_MOD_VIVANTE_SUPER_TILED:
423 if ((screen->specs.pixel_pipes > 1 && !screen->specs.single_buffer) ||
424 !screen->specs.can_supertile)
425 break;
426 prio = MAX2(prio, MODIFIER_PRIORITY_SUPER_TILED);
427 break;
428 case DRM_FORMAT_MOD_VIVANTE_TILED:
429 if (screen->specs.pixel_pipes > 1 && !screen->specs.single_buffer)
430 break;
431 prio = MAX2(prio, MODIFIER_PRIORITY_TILED);
432 break;
433 case DRM_FORMAT_MOD_VIVANTE_SPLIT_SUPER_TILED:
434 if ((screen->specs.pixel_pipes < 2) || !screen->specs.can_supertile)
435 break;
436 prio = MAX2(prio, MODIFIER_PRIORITY_SPLIT_SUPER_TILED);
437 break;
438 case DRM_FORMAT_MOD_VIVANTE_SPLIT_TILED:
439 if (screen->specs.pixel_pipes < 2)
440 break;
441 prio = MAX2(prio, MODIFIER_PRIORITY_SPLIT_TILED);
442 break;
443 case DRM_FORMAT_MOD_LINEAR:
444 prio = MAX2(prio, MODIFIER_PRIORITY_LINEAR);
445 break;
446 case DRM_FORMAT_MOD_INVALID:
447 default:
448 break;
449 }
450 }
451
452 return priority_to_modifier[prio];
453 }
454
455 static struct pipe_resource *
456 etna_resource_create_modifiers(struct pipe_screen *pscreen,
457 const struct pipe_resource *templat,
458 const uint64_t *modifiers, int count)
459 {
460 struct etna_screen *screen = etna_screen(pscreen);
461 struct pipe_resource tmpl = *templat;
462 uint64_t modifier = select_best_modifier(screen, modifiers, count);
463
464 if (modifier == DRM_FORMAT_MOD_INVALID)
465 return NULL;
466
467 /*
468 * We currently assume that all buffers allocated through this interface
469 * should be scanout enabled.
470 */
471 tmpl.bind |= PIPE_BIND_SCANOUT;
472
473 return etna_resource_alloc(pscreen, modifier_to_layout(modifier),
474 ETNA_ADDRESSING_MODE_TILED, modifier, &tmpl);
475 }
476
477 static void
478 etna_resource_changed(struct pipe_screen *pscreen, struct pipe_resource *prsc)
479 {
480 struct etna_resource *res = etna_resource(prsc);
481
482 if (res->external)
483 etna_resource(res->external)->seqno++;
484 else
485 res->seqno++;
486 }
487
488 static void
489 etna_resource_destroy(struct pipe_screen *pscreen, struct pipe_resource *prsc)
490 {
491 struct etna_screen *screen = etna_screen(pscreen);
492 struct etna_resource *rsc = etna_resource(prsc);
493
494 mtx_lock(&screen->lock);
495 _mesa_set_remove_key(screen->used_resources, rsc);
496 _mesa_set_destroy(rsc->pending_ctx, NULL);
497 mtx_unlock(&screen->lock);
498
499 if (rsc->bo)
500 etna_bo_del(rsc->bo);
501
502 if (rsc->ts_bo)
503 etna_bo_del(rsc->ts_bo);
504
505 if (rsc->scanout)
506 renderonly_scanout_destroy(rsc->scanout, etna_screen(pscreen)->ro);
507
508 pipe_resource_reference(&rsc->texture, NULL);
509 pipe_resource_reference(&rsc->external, NULL);
510
511 for (unsigned i = 0; i < ETNA_NUM_LOD; i++)
512 FREE(rsc->levels[i].patch_offsets);
513
514 FREE(rsc);
515 }
516
517 static struct pipe_resource *
518 etna_resource_from_handle(struct pipe_screen *pscreen,
519 const struct pipe_resource *tmpl,
520 struct winsys_handle *handle, unsigned usage)
521 {
522 struct etna_screen *screen = etna_screen(pscreen);
523 struct etna_resource *rsc;
524 struct etna_resource_level *level;
525 struct pipe_resource *prsc;
526 struct pipe_resource *ptiled = NULL;
527
528 DBG("target=%d, format=%s, %ux%ux%u, array_size=%u, last_level=%u, "
529 "nr_samples=%u, usage=%u, bind=%x, flags=%x",
530 tmpl->target, util_format_name(tmpl->format), tmpl->width0,
531 tmpl->height0, tmpl->depth0, tmpl->array_size, tmpl->last_level,
532 tmpl->nr_samples, tmpl->usage, tmpl->bind, tmpl->flags);
533
534 rsc = CALLOC_STRUCT(etna_resource);
535 if (!rsc)
536 return NULL;
537
538 level = &rsc->levels[0];
539 prsc = &rsc->base;
540
541 *prsc = *tmpl;
542
543 pipe_reference_init(&prsc->reference, 1);
544 prsc->screen = pscreen;
545
546 rsc->bo = etna_screen_bo_from_handle(pscreen, handle, &level->stride);
547 if (!rsc->bo)
548 goto fail;
549
550 rsc->seqno = 1;
551 rsc->layout = modifier_to_layout(handle->modifier);
552 rsc->halign = TEXTURE_HALIGN_FOUR;
553 rsc->addressing_mode = ETNA_ADDRESSING_MODE_TILED;
554
555
556 level->width = tmpl->width0;
557 level->height = tmpl->height0;
558 level->depth = tmpl->depth0;
559 level->offset = handle->offset;
560
561 /* Determine padding of the imported resource. */
562 unsigned paddingX = 0, paddingY = 0;
563 etna_layout_multiple(rsc->layout, screen->specs.pixel_pipes,
564 is_rs_align(screen, tmpl),
565 &paddingX, &paddingY, &rsc->halign);
566
567 if (!screen->specs.use_blt && rsc->layout == ETNA_LAYOUT_LINEAR)
568 paddingY = align(paddingY, ETNA_RS_HEIGHT_MASK + 1);
569 level->padded_width = align(level->width, paddingX);
570 level->padded_height = align(level->height, paddingY);
571
572 level->layer_stride = level->stride * util_format_get_nblocksy(prsc->format,
573 level->padded_height);
574 level->size = level->layer_stride;
575
576 /* The DDX must give us a BO which conforms to our padding size.
577 * The stride of the BO must be greater or equal to our padded
578 * stride. The size of the BO must accomodate the padded height. */
579 if (level->stride < util_format_get_stride(tmpl->format, level->padded_width)) {
580 BUG("BO stride %u is too small for RS engine width padding (%zu, format %s)",
581 level->stride, util_format_get_stride(tmpl->format, level->padded_width),
582 util_format_name(tmpl->format));
583 goto fail;
584 }
585 if (etna_bo_size(rsc->bo) < level->stride * level->padded_height) {
586 BUG("BO size %u is too small for RS engine height padding (%u, format %s)",
587 etna_bo_size(rsc->bo), level->stride * level->padded_height,
588 util_format_name(tmpl->format));
589 goto fail;
590 }
591
592 rsc->pending_ctx = _mesa_set_create(NULL, _mesa_hash_pointer,
593 _mesa_key_pointer_equal);
594 if (!rsc->pending_ctx)
595 goto fail;
596
597 if (rsc->layout == ETNA_LAYOUT_LINEAR) {
598 /*
599 * Both sampler and pixel pipes can't handle linear, create a compatible
600 * base resource, where we can attach the imported buffer as an external
601 * resource.
602 */
603 struct pipe_resource tiled_templat = *tmpl;
604
605 /*
606 * Remove BIND_SCANOUT to avoid recursion, as etna_resource_create uses
607 * this function to import the scanout buffer and get a tiled resource.
608 */
609 tiled_templat.bind &= ~PIPE_BIND_SCANOUT;
610
611 ptiled = etna_resource_create(pscreen, &tiled_templat);
612 if (!ptiled)
613 goto fail;
614
615 etna_resource(ptiled)->external = prsc;
616
617 return ptiled;
618 }
619
620 return prsc;
621
622 fail:
623 etna_resource_destroy(pscreen, prsc);
624 if (ptiled)
625 etna_resource_destroy(pscreen, ptiled);
626
627 return NULL;
628 }
629
630 static bool
631 etna_resource_get_handle(struct pipe_screen *pscreen,
632 struct pipe_context *pctx,
633 struct pipe_resource *prsc,
634 struct winsys_handle *handle, unsigned usage)
635 {
636 struct etna_resource *rsc = etna_resource(prsc);
637 /* Scanout is always attached to the base resource */
638 struct renderonly_scanout *scanout = rsc->scanout;
639
640 /*
641 * External resources are preferred, so a import->export chain of
642 * render/sampler incompatible buffers yield the same handle.
643 */
644 if (rsc->external)
645 rsc = etna_resource(rsc->external);
646
647 handle->stride = rsc->levels[0].stride;
648 handle->offset = rsc->levels[0].offset;
649 handle->modifier = layout_to_modifier(rsc->layout);
650
651 if (handle->type == WINSYS_HANDLE_TYPE_SHARED) {
652 return etna_bo_get_name(rsc->bo, &handle->handle) == 0;
653 } else if (handle->type == WINSYS_HANDLE_TYPE_KMS) {
654 if (renderonly_get_handle(scanout, handle)) {
655 return true;
656 } else {
657 handle->handle = etna_bo_handle(rsc->bo);
658 return true;
659 }
660 } else if (handle->type == WINSYS_HANDLE_TYPE_FD) {
661 handle->handle = etna_bo_dmabuf(rsc->bo);
662 return true;
663 } else {
664 return false;
665 }
666 }
667
668 void
669 etna_resource_used(struct etna_context *ctx, struct pipe_resource *prsc,
670 enum etna_resource_status status)
671 {
672 struct etna_screen *screen = ctx->screen;
673 struct etna_resource *rsc;
674
675 if (!prsc)
676 return;
677
678 rsc = etna_resource(prsc);
679
680 mtx_lock(&screen->lock);
681
682 /*
683 * if we are pending read or write by any other context or
684 * if reading a resource pending a write, then
685 * flush all the contexts to maintain coherency
686 */
687 if (((status & ETNA_PENDING_WRITE) && rsc->status) ||
688 ((status & ETNA_PENDING_READ) && (rsc->status & ETNA_PENDING_WRITE))) {
689 set_foreach(rsc->pending_ctx, entry) {
690 struct etna_context *extctx = (struct etna_context *)entry->key;
691 struct pipe_context *pctx = &extctx->base;
692
693 if (extctx == ctx)
694 continue;
695
696 pctx->flush(pctx, NULL, 0);
697 /* It's safe to clear the status here. If we need to flush it means
698 * either another context had the resource in exclusive (write) use,
699 * or we transition the resource to exclusive use in our context.
700 * In both cases the new status accurately reflects the resource use
701 * after the flush.
702 */
703 rsc->status = 0;
704 }
705 }
706
707 rsc->status |= status;
708
709 _mesa_set_add(screen->used_resources, rsc);
710 _mesa_set_add(rsc->pending_ctx, ctx);
711
712 mtx_unlock(&screen->lock);
713 }
714
715 bool
716 etna_resource_has_valid_ts(struct etna_resource *rsc)
717 {
718 if (!rsc->ts_bo)
719 return false;
720
721 for (int level = 0; level <= rsc->base.last_level; level++)
722 if (rsc->levels[level].ts_valid)
723 return true;
724
725 return false;
726 }
727
728 void
729 etna_resource_screen_init(struct pipe_screen *pscreen)
730 {
731 pscreen->can_create_resource = etna_screen_can_create_resource;
732 pscreen->resource_create = etna_resource_create;
733 pscreen->resource_create_with_modifiers = etna_resource_create_modifiers;
734 pscreen->resource_from_handle = etna_resource_from_handle;
735 pscreen->resource_get_handle = etna_resource_get_handle;
736 pscreen->resource_changed = etna_resource_changed;
737 pscreen->resource_destroy = etna_resource_destroy;
738 }