etnaviv: add resource subregion copy
[mesa.git] / src / gallium / drivers / etnaviv / etnaviv_clear_blit.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_clear_blit.h"
28
29 #include "hw/common.xml.h"
30
31 #include "etnaviv_context.h"
32 #include "etnaviv_emit.h"
33 #include "etnaviv_emit.h"
34 #include "etnaviv_format.h"
35 #include "etnaviv_resource.h"
36 #include "etnaviv_surface.h"
37 #include "etnaviv_translate.h"
38
39 #include "pipe/p_defines.h"
40 #include "pipe/p_state.h"
41 #include "util/u_blitter.h"
42 #include "util/u_inlines.h"
43 #include "util/u_memory.h"
44 #include "util/u_surface.h"
45
46 /* Save current state for blitter operation */
47 static void
48 etna_blit_save_state(struct etna_context *ctx)
49 {
50 util_blitter_save_vertex_buffer_slot(ctx->blitter, ctx->vertex_buffer.vb);
51 util_blitter_save_vertex_elements(ctx->blitter, ctx->vertex_elements);
52 util_blitter_save_vertex_shader(ctx->blitter, ctx->shader.bind_vs);
53 util_blitter_save_rasterizer(ctx->blitter, ctx->rasterizer);
54 util_blitter_save_viewport(ctx->blitter, &ctx->viewport_s);
55 util_blitter_save_scissor(ctx->blitter, &ctx->scissor_s);
56 util_blitter_save_fragment_shader(ctx->blitter, ctx->shader.bind_fs);
57 util_blitter_save_blend(ctx->blitter, ctx->blend);
58 util_blitter_save_depth_stencil_alpha(ctx->blitter, ctx->zsa);
59 util_blitter_save_stencil_ref(ctx->blitter, &ctx->stencil_ref_s);
60 util_blitter_save_sample_mask(ctx->blitter, ctx->sample_mask);
61 util_blitter_save_framebuffer(ctx->blitter, &ctx->framebuffer_s);
62 util_blitter_save_fragment_sampler_states(ctx->blitter,
63 ctx->num_fragment_samplers, (void **)ctx->sampler);
64 util_blitter_save_fragment_sampler_views(ctx->blitter,
65 ctx->num_fragment_sampler_views, ctx->sampler_view);
66 }
67
68 /* Generate clear command for a surface (non-fast clear case) */
69 void
70 etna_rs_gen_clear_surface(struct etna_context *ctx, struct etna_surface *surf,
71 uint32_t clear_value)
72 {
73 struct etna_resource *dst = etna_resource(surf->base.texture);
74 uint32_t format = translate_rs_format(surf->base.format);
75
76 if (format == ETNA_NO_MATCH) {
77 BUG("etna_rs_gen_clear_surface: Unhandled clear fmt %s", util_format_name(surf->base.format));
78 format = RS_FORMAT_A8R8G8B8;
79 assert(0);
80 }
81
82 /* use tiled clear if width is multiple of 16 */
83 bool tiled_clear = (surf->surf.padded_width & ETNA_RS_WIDTH_MASK) == 0 &&
84 (surf->surf.padded_height & ETNA_RS_HEIGHT_MASK) == 0;
85
86 etna_compile_rs_state( ctx, &surf->clear_command, &(struct rs_state) {
87 .source_format = format,
88 .dest_format = format,
89 .dest = dst->bo,
90 .dest_offset = surf->surf.offset,
91 .dest_stride = surf->surf.stride,
92 .dest_padded_height = surf->surf.padded_height,
93 .dest_tiling = tiled_clear ? dst->layout : ETNA_LAYOUT_LINEAR,
94 .dither = {0xffffffff, 0xffffffff},
95 .width = surf->surf.padded_width, /* These must be padded to 16x4 if !LINEAR, otherwise RS will hang */
96 .height = surf->surf.padded_height,
97 .clear_value = {clear_value},
98 .clear_mode = VIVS_RS_CLEAR_CONTROL_MODE_ENABLED1,
99 .clear_bits = 0xffff
100 });
101 }
102
103 static inline uint32_t
104 pack_rgba(enum pipe_format format, const float *rgba)
105 {
106 union util_color uc;
107 util_pack_color(rgba, format, &uc);
108 if (util_format_get_blocksize(format) == 2)
109 return uc.ui[0] << 16 | (uc.ui[0] & 0xffff);
110 else
111 return uc.ui[0];
112 }
113
114 static void
115 etna_blit_clear_color(struct pipe_context *pctx, struct pipe_surface *dst,
116 const union pipe_color_union *color)
117 {
118 struct etna_context *ctx = etna_context(pctx);
119 struct etna_surface *surf = etna_surface(dst);
120 uint32_t new_clear_value = pack_rgba(surf->base.format, color->f);
121
122 if (surf->surf.ts_size) { /* TS: use precompiled clear command */
123 ctx->framebuffer.TS_COLOR_CLEAR_VALUE = new_clear_value;
124
125 if (VIV_FEATURE(ctx->screen, chipMinorFeatures1, AUTO_DISABLE)) {
126 /* Set number of color tiles to be filled */
127 etna_set_state(ctx->stream, VIVS_TS_COLOR_AUTO_DISABLE_COUNT,
128 surf->surf.padded_width * surf->surf.padded_height / 16);
129 ctx->framebuffer.TS_MEM_CONFIG |= VIVS_TS_MEM_CONFIG_COLOR_AUTO_DISABLE;
130 }
131
132 surf->level->ts_valid = true;
133 ctx->dirty |= ETNA_DIRTY_TS;
134 } else if (unlikely(new_clear_value != surf->level->clear_value)) { /* Queue normal RS clear for non-TS surfaces */
135 /* If clear color changed, re-generate stored command */
136 etna_rs_gen_clear_surface(ctx, surf, new_clear_value);
137 }
138
139 etna_submit_rs_state(ctx, &surf->clear_command);
140 surf->level->clear_value = new_clear_value;
141 resource_written(ctx, surf->base.texture);
142 etna_resource(surf->base.texture)->seqno++;
143 }
144
145 static void
146 etna_blit_clear_zs(struct pipe_context *pctx, struct pipe_surface *dst,
147 unsigned buffers, double depth, unsigned stencil)
148 {
149 struct etna_context *ctx = etna_context(pctx);
150 struct etna_surface *surf = etna_surface(dst);
151 uint32_t new_clear_value = translate_clear_depth_stencil(surf->base.format, depth, stencil);
152 uint32_t new_clear_bits = 0, clear_bits_depth, clear_bits_stencil;
153
154 /* Get the channels to clear */
155 switch (surf->base.format) {
156 case PIPE_FORMAT_Z16_UNORM:
157 clear_bits_depth = 0xffff;
158 clear_bits_stencil = 0;
159 break;
160 case PIPE_FORMAT_X8Z24_UNORM:
161 case PIPE_FORMAT_S8_UINT_Z24_UNORM:
162 clear_bits_depth = 0xeeee;
163 clear_bits_stencil = 0x1111;
164 break;
165 default:
166 clear_bits_depth = clear_bits_stencil = 0xffff;
167 break;
168 }
169
170 if (buffers & PIPE_CLEAR_DEPTH)
171 new_clear_bits |= clear_bits_depth;
172 if (buffers & PIPE_CLEAR_STENCIL)
173 new_clear_bits |= clear_bits_stencil;
174
175 /* FIXME: when tile status is enabled, this becomes more complex as
176 * we may separately clear the depth from the stencil. In this case,
177 * we want to resolve the surface, and avoid using the tile status.
178 * We may be better off recording the pending clear operation,
179 * delaying the actual clear to the first use. This way, we can merge
180 * consecutive clears together. */
181 if (surf->surf.ts_size) { /* TS: use precompiled clear command */
182 /* Set new clear depth value */
183 ctx->framebuffer.TS_DEPTH_CLEAR_VALUE = new_clear_value;
184 if (VIV_FEATURE(ctx->screen, chipMinorFeatures1, AUTO_DISABLE)) {
185 /* Set number of depth tiles to be filled */
186 etna_set_state(ctx->stream, VIVS_TS_DEPTH_AUTO_DISABLE_COUNT,
187 surf->surf.padded_width * surf->surf.padded_height / 16);
188 ctx->framebuffer.TS_MEM_CONFIG |= VIVS_TS_MEM_CONFIG_DEPTH_AUTO_DISABLE;
189 }
190
191 surf->level->ts_valid = true;
192 ctx->dirty |= ETNA_DIRTY_TS;
193 } else {
194 if (unlikely(new_clear_value != surf->level->clear_value)) { /* Queue normal RS clear for non-TS surfaces */
195 /* If clear depth value changed, re-generate stored command */
196 etna_rs_gen_clear_surface(ctx, surf, new_clear_value);
197 }
198 /* Update the channels to be cleared */
199 etna_modify_rs_clearbits(&surf->clear_command, new_clear_bits);
200 }
201
202 etna_submit_rs_state(ctx, &surf->clear_command);
203 surf->level->clear_value = new_clear_value;
204 resource_written(ctx, surf->base.texture);
205 etna_resource(surf->base.texture)->seqno++;
206 }
207
208 static void
209 etna_clear(struct pipe_context *pctx, unsigned buffers,
210 const union pipe_color_union *color, double depth, unsigned stencil)
211 {
212 struct etna_context *ctx = etna_context(pctx);
213
214 /* Flush color and depth cache before clearing anything.
215 * This is especially important when coming from another surface, as
216 * otherwise it may clear part of the old surface instead. */
217 etna_set_state(ctx->stream, VIVS_GL_FLUSH_CACHE, VIVS_GL_FLUSH_CACHE_COLOR | VIVS_GL_FLUSH_CACHE_DEPTH);
218 etna_stall(ctx->stream, SYNC_RECIPIENT_RA, SYNC_RECIPIENT_PE);
219
220 /* Preparation: Flush the TS if needed. This must be done after flushing
221 * color and depth, otherwise it can result in crashes */
222 bool need_ts_flush = false;
223 if ((buffers & PIPE_CLEAR_COLOR) && ctx->framebuffer_s.nr_cbufs) {
224 struct etna_surface *surf = etna_surface(ctx->framebuffer_s.cbufs[0]);
225 if (surf->surf.ts_size)
226 need_ts_flush = true;
227 }
228 if ((buffers & PIPE_CLEAR_DEPTHSTENCIL) && ctx->framebuffer_s.zsbuf != NULL) {
229 struct etna_surface *surf = etna_surface(ctx->framebuffer_s.zsbuf);
230
231 if (surf->surf.ts_size)
232 need_ts_flush = true;
233 }
234
235 if (need_ts_flush)
236 etna_set_state(ctx->stream, VIVS_TS_FLUSH_CACHE, VIVS_TS_FLUSH_CACHE_FLUSH);
237
238 /* No need to set up the TS here as RS clear operations (in contrast to
239 * resolve and copy) do not require the TS state.
240 */
241 if (buffers & PIPE_CLEAR_COLOR) {
242 for (int idx = 0; idx < ctx->framebuffer_s.nr_cbufs; ++idx) {
243 etna_blit_clear_color(pctx, ctx->framebuffer_s.cbufs[idx],
244 &color[idx]);
245 }
246 }
247
248 /* Flush the color and depth caches before each RS clear operation
249 * This fixes a hang on GC600. */
250 if (buffers & PIPE_CLEAR_DEPTHSTENCIL && buffers & PIPE_CLEAR_COLOR)
251 etna_set_state(ctx->stream, VIVS_GL_FLUSH_CACHE,
252 VIVS_GL_FLUSH_CACHE_COLOR | VIVS_GL_FLUSH_CACHE_DEPTH);
253
254 if ((buffers & PIPE_CLEAR_DEPTHSTENCIL) && ctx->framebuffer_s.zsbuf != NULL)
255 etna_blit_clear_zs(pctx, ctx->framebuffer_s.zsbuf, buffers, depth, stencil);
256
257 etna_stall(ctx->stream, SYNC_RECIPIENT_RA, SYNC_RECIPIENT_PE);
258 }
259
260 static void
261 etna_clear_render_target(struct pipe_context *pctx, struct pipe_surface *dst,
262 const union pipe_color_union *color, unsigned dstx,
263 unsigned dsty, unsigned width, unsigned height,
264 bool render_condition_enabled)
265 {
266 struct etna_context *ctx = etna_context(pctx);
267
268 /* XXX could fall back to RS when target area is full screen / resolveable
269 * and no TS. */
270 etna_blit_save_state(ctx);
271 util_blitter_clear_render_target(ctx->blitter, dst, color, dstx, dsty, width, height);
272 }
273
274 static void
275 etna_clear_depth_stencil(struct pipe_context *pctx, struct pipe_surface *dst,
276 unsigned clear_flags, double depth, unsigned stencil,
277 unsigned dstx, unsigned dsty, unsigned width,
278 unsigned height, bool render_condition_enabled)
279 {
280 struct etna_context *ctx = etna_context(pctx);
281
282 /* XXX could fall back to RS when target area is full screen / resolveable
283 * and no TS. */
284 etna_blit_save_state(ctx);
285 util_blitter_clear_depth_stencil(ctx->blitter, dst, clear_flags, depth,
286 stencil, dstx, dsty, width, height);
287 }
288
289 static void
290 etna_resource_copy_region(struct pipe_context *pctx, struct pipe_resource *dst,
291 unsigned dst_level, unsigned dstx, unsigned dsty,
292 unsigned dstz, struct pipe_resource *src,
293 unsigned src_level, const struct pipe_box *src_box)
294 {
295 struct etna_context *ctx = etna_context(pctx);
296
297 /* The resource must be of the same format. */
298 assert(src->format == dst->format);
299
300 /* XXX we can use the RS as a literal copy engine here
301 * the only complexity is tiling; the size of the boxes needs to be aligned
302 * to the tile size
303 * how to handle the case where a resource is copied from/to a non-aligned
304 * position?
305 * from non-aligned: can fall back to rendering-based copy?
306 * to non-aligned: can fall back to rendering-based copy?
307 * XXX this goes wrong when source surface is supertiled.
308 */
309 if (util_blitter_is_copy_supported(ctx->blitter, dst, src)) {
310 etna_blit_save_state(ctx);
311 util_blitter_copy_texture(ctx->blitter, dst, dst_level, dstx, dsty, dstz,
312 src, src_level, src_box);
313 } else {
314 util_resource_copy_region(pctx, dst, dst_level, dstx, dsty, dstz, src,
315 src_level, src_box);
316 }
317 }
318
319 static bool
320 etna_manual_blit(struct etna_resource *dst, struct etna_resource_level *dst_lev,
321 unsigned int dst_offset, struct etna_resource *src,
322 struct etna_resource_level *src_lev, unsigned int src_offset,
323 const struct pipe_blit_info *blit_info)
324 {
325 void *smap, *srow, *dmap, *drow;
326 size_t tile_size;
327
328 assert(src->layout == ETNA_LAYOUT_TILED);
329 assert(dst->layout == ETNA_LAYOUT_TILED);
330 assert(src->base.nr_samples == 0);
331 assert(dst->base.nr_samples == 0);
332
333 tile_size = util_format_get_blocksize(blit_info->src.format) * 4 * 4;
334
335 smap = etna_bo_map(src->bo);
336 if (!smap)
337 return false;
338
339 dmap = etna_bo_map(dst->bo);
340 if (!dmap)
341 return false;
342
343 srow = smap + src_offset;
344 drow = dmap + dst_offset;
345
346 etna_bo_cpu_prep(src->bo, DRM_ETNA_PREP_READ);
347 etna_bo_cpu_prep(dst->bo, DRM_ETNA_PREP_WRITE);
348
349 for (int y = 0; y < blit_info->src.box.height; y += 4) {
350 memcpy(drow, srow, tile_size * blit_info->src.box.width);
351 srow += src_lev->stride * 4;
352 drow += dst_lev->stride * 4;
353 }
354
355 etna_bo_cpu_fini(dst->bo);
356 etna_bo_cpu_fini(src->bo);
357
358 return true;
359 }
360
361 static inline size_t
362 etna_compute_tileoffset(const struct pipe_box *box, enum pipe_format format,
363 size_t stride, enum etna_surface_layout layout)
364 {
365 size_t offset;
366 unsigned int x = box->x, y = box->y;
367 unsigned int blocksize = util_format_get_blocksize(format);
368
369 switch (layout) {
370 case ETNA_LAYOUT_LINEAR:
371 offset = y * stride + x * blocksize;
372 break;
373 case ETNA_LAYOUT_MULTI_TILED:
374 y >>= 1;
375 /* fall-through */
376 case ETNA_LAYOUT_TILED:
377 assert(!(x & 0x03) && !(y & 0x03));
378 offset = (y & ~0x03) * stride + blocksize * ((x & ~0x03) << 2);
379 break;
380 case ETNA_LAYOUT_MULTI_SUPERTILED:
381 y >>= 1;
382 /* fall-through */
383 case ETNA_LAYOUT_SUPER_TILED:
384 assert(!(x & 0x3f) && !(y & 0x3f));
385 offset = (y & ~0x3f) * stride + blocksize * ((x & ~0x3f) << 6);
386 break;
387 default:
388 unreachable("invalid resource layout");
389 }
390
391 return offset;
392 }
393
394 static inline void
395 etna_get_rs_alignment_mask(const struct etna_context *ctx,
396 const enum etna_surface_layout layout,
397 unsigned int *width_mask, unsigned int *height_mask)
398 {
399 unsigned int h_align, w_align;
400
401 if (layout & ETNA_LAYOUT_BIT_SUPER) {
402 w_align = h_align = 64;
403 } else {
404 w_align = ETNA_RS_WIDTH_MASK + 1;
405 h_align = ETNA_RS_HEIGHT_MASK + 1;
406 }
407
408 h_align *= ctx->screen->specs.pixel_pipes;
409
410 *width_mask = w_align - 1;
411 *height_mask = h_align -1;
412 }
413
414 static bool
415 etna_try_rs_blit(struct pipe_context *pctx,
416 const struct pipe_blit_info *blit_info)
417 {
418 struct etna_context *ctx = etna_context(pctx);
419 struct etna_resource *src = etna_resource(blit_info->src.resource);
420 struct etna_resource *dst = etna_resource(blit_info->dst.resource);
421 struct compiled_rs_state copy_to_screen;
422 uint32_t ts_mem_config = 0;
423 int msaa_xscale = 1, msaa_yscale = 1;
424
425 /* Ensure that the level is valid */
426 assert(blit_info->src.level <= src->base.last_level);
427 assert(blit_info->dst.level <= dst->base.last_level);
428
429 if (!translate_samples_to_xyscale(src->base.nr_samples, &msaa_xscale, &msaa_yscale, NULL))
430 return FALSE;
431
432 /* The width/height are in pixels; they do not change as a result of
433 * multi-sampling. So, when blitting from a 4x multisampled surface
434 * to a non-multisampled surface, the width and height will be
435 * identical. As we do not support scaling, reject different sizes. */
436 if (blit_info->dst.box.width != blit_info->src.box.width ||
437 blit_info->dst.box.height != blit_info->src.box.height) {
438 DBG("scaling requested: source %dx%d destination %dx%d",
439 blit_info->src.box.width, blit_info->src.box.height,
440 blit_info->dst.box.width, blit_info->dst.box.height);
441 return FALSE;
442 }
443
444 /* No masks - RS can't copy specific channels */
445 unsigned mask = util_format_get_mask(blit_info->dst.format);
446 if ((blit_info->mask & mask) != mask) {
447 DBG("sub-mask requested: 0x%02x vs format mask 0x%02x", blit_info->mask, mask);
448 return FALSE;
449 }
450
451 unsigned src_format = etna_compatible_rs_format(blit_info->src.format);
452 unsigned dst_format = etna_compatible_rs_format(blit_info->dst.format);
453 if (translate_rs_format(src_format) == ETNA_NO_MATCH ||
454 translate_rs_format(dst_format) == ETNA_NO_MATCH ||
455 blit_info->scissor_enable ||
456 blit_info->dst.box.depth != blit_info->src.box.depth ||
457 blit_info->dst.box.depth != 1) {
458 return FALSE;
459 }
460
461 unsigned w_mask, h_mask;
462
463 etna_get_rs_alignment_mask(ctx, src->layout, &w_mask, &h_mask);
464 if ((blit_info->src.box.x & w_mask) || (blit_info->src.box.y & h_mask))
465 return FALSE;
466
467 etna_get_rs_alignment_mask(ctx, dst->layout, &w_mask, &h_mask);
468 if ((blit_info->dst.box.x & w_mask) || (blit_info->dst.box.y & h_mask))
469 return FALSE;
470
471 /* Ensure that the Z coordinate is sane */
472 if (dst->base.target != PIPE_TEXTURE_CUBE)
473 assert(blit_info->dst.box.z == 0);
474 if (src->base.target != PIPE_TEXTURE_CUBE)
475 assert(blit_info->src.box.z == 0);
476
477 assert(blit_info->src.box.z < src->base.array_size);
478 assert(blit_info->dst.box.z < dst->base.array_size);
479
480 struct etna_resource_level *src_lev = &src->levels[blit_info->src.level];
481 struct etna_resource_level *dst_lev = &dst->levels[blit_info->dst.level];
482
483 /* we may be given coordinates up to the padded width to avoid
484 * any alignment issues with different tiling formats */
485 assert((blit_info->src.box.x + blit_info->src.box.width) * msaa_xscale <= src_lev->padded_width);
486 assert((blit_info->src.box.y + blit_info->src.box.height) * msaa_yscale <= src_lev->padded_height);
487 assert(blit_info->dst.box.x + blit_info->dst.box.width <= dst_lev->padded_width);
488 assert(blit_info->dst.box.y + blit_info->dst.box.height <= dst_lev->padded_height);
489
490 unsigned src_offset = src_lev->offset +
491 blit_info->src.box.z * src_lev->layer_stride +
492 etna_compute_tileoffset(&blit_info->src.box,
493 blit_info->src.format,
494 src_lev->stride,
495 src->layout);
496 unsigned dst_offset = dst_lev->offset +
497 blit_info->dst.box.z * dst_lev->layer_stride +
498 etna_compute_tileoffset(&blit_info->dst.box,
499 blit_info->dst.format,
500 dst_lev->stride,
501 dst->layout);
502
503 if (src_lev->padded_width <= ETNA_RS_WIDTH_MASK ||
504 dst_lev->padded_width <= ETNA_RS_WIDTH_MASK ||
505 src_lev->padded_height <= ETNA_RS_HEIGHT_MASK ||
506 dst_lev->padded_height <= ETNA_RS_HEIGHT_MASK)
507 goto manual;
508
509 /* If the width is not aligned to the RS width, but is within our
510 * padding, adjust the width to suite the RS width restriction.
511 * Note: the RS width/height are converted to source samples here. */
512 unsigned int width = blit_info->src.box.width * msaa_xscale;
513 unsigned int height = blit_info->src.box.height * msaa_yscale;
514 unsigned int w_align = ETNA_RS_WIDTH_MASK + 1;
515 unsigned int h_align = (ETNA_RS_HEIGHT_MASK + 1) * ctx->specs.pixel_pipes;
516
517 if (width & (w_align - 1) && width >= src_lev->width * msaa_xscale && width >= dst_lev->width)
518 width = align(width, w_align);
519
520 if (height & (h_align - 1) && height >= src_lev->height * msaa_yscale && height >= dst_lev->height)
521 height = align(height, h_align);
522
523 /* The padded dimensions are in samples */
524 if (width > src_lev->padded_width ||
525 width > dst_lev->padded_width * msaa_xscale ||
526 height > src_lev->padded_height ||
527 height > dst_lev->padded_height * msaa_yscale ||
528 width & (w_align - 1) || height & (h_align - 1))
529 goto manual;
530
531 if (src->base.nr_samples > 1) {
532 uint32_t msaa_format = translate_msaa_format(src_format);
533 assert(msaa_format != ETNA_NO_MATCH);
534 ts_mem_config |= VIVS_TS_MEM_CONFIG_MSAA | msaa_format;
535 }
536
537 /* Always flush color and depth cache together before resolving. This works
538 * around artifacts that appear in some cases when scanning out a texture
539 * directly after it has been rendered to, such as rendering an animated web
540 * page in a QtWebEngine based WebView on GC2000. The artifacts look like
541 * the texture sampler samples zeroes instead of texture data in a small,
542 * irregular triangle in the lower right of each browser tile quad. Other
543 * attempts to avoid these artifacts, including a pipeline stall before the
544 * color flush or a TS cache flush afterwards, or flushing multiple times,
545 * with stalls before and after each flush, have shown no effect. */
546 if (src->base.bind & PIPE_BIND_RENDER_TARGET ||
547 src->base.bind & PIPE_BIND_DEPTH_STENCIL) {
548 etna_set_state(ctx->stream, VIVS_GL_FLUSH_CACHE,
549 VIVS_GL_FLUSH_CACHE_COLOR | VIVS_GL_FLUSH_CACHE_DEPTH);
550 etna_stall(ctx->stream, SYNC_RECIPIENT_RA, SYNC_RECIPIENT_PE);
551
552 if (src->levels[blit_info->src.level].ts_size &&
553 src->levels[blit_info->src.level].ts_valid)
554 etna_set_state(ctx->stream, VIVS_TS_FLUSH_CACHE, VIVS_TS_FLUSH_CACHE_FLUSH);
555 }
556
557 /* Set up color TS to source surface before blit, if needed */
558 if (src->levels[blit_info->src.level].ts_size &&
559 src->levels[blit_info->src.level].ts_valid) {
560 struct etna_reloc reloc;
561 unsigned ts_offset =
562 src_lev->ts_offset + blit_info->src.box.z * src_lev->ts_layer_stride;
563
564 etna_set_state(ctx->stream, VIVS_TS_MEM_CONFIG,
565 VIVS_TS_MEM_CONFIG_COLOR_FAST_CLEAR | ts_mem_config);
566
567 memset(&reloc, 0, sizeof(struct etna_reloc));
568 reloc.bo = src->ts_bo;
569 reloc.offset = ts_offset;
570 reloc.flags = ETNA_RELOC_READ;
571 etna_set_state_reloc(ctx->stream, VIVS_TS_COLOR_STATUS_BASE, &reloc);
572
573 memset(&reloc, 0, sizeof(struct etna_reloc));
574 reloc.bo = src->bo;
575 reloc.offset = src_lev->offset +
576 blit_info->src.box.z * src_lev->layer_stride;
577 reloc.flags = ETNA_RELOC_READ;
578 etna_set_state_reloc(ctx->stream, VIVS_TS_COLOR_SURFACE_BASE, &reloc);
579
580 etna_set_state(ctx->stream, VIVS_TS_COLOR_CLEAR_VALUE,
581 src->levels[blit_info->src.level].clear_value);
582 } else {
583 etna_set_state(ctx->stream, VIVS_TS_MEM_CONFIG, ts_mem_config);
584 }
585 ctx->dirty |= ETNA_DIRTY_TS;
586
587 /* Kick off RS here */
588 etna_compile_rs_state(ctx, &copy_to_screen, &(struct rs_state) {
589 .source_format = translate_rs_format(src_format),
590 .source_tiling = src->layout,
591 .source = src->bo,
592 .source_offset = src_offset,
593 .source_stride = src_lev->stride,
594 .source_padded_height = src_lev->padded_height,
595 .dest_format = translate_rs_format(dst_format),
596 .dest_tiling = dst->layout,
597 .dest = dst->bo,
598 .dest_offset = dst_offset,
599 .dest_stride = dst_lev->stride,
600 .dest_padded_height = dst_lev->padded_height,
601 .downsample_x = msaa_xscale > 1,
602 .downsample_y = msaa_yscale > 1,
603 .swap_rb = translate_rb_src_dst_swap(src->base.format, dst->base.format),
604 .dither = {0xffffffff, 0xffffffff}, // XXX dither when going from 24 to 16 bit?
605 .clear_mode = VIVS_RS_CLEAR_CONTROL_MODE_DISABLED,
606 .width = width,
607 .height = height
608 });
609
610 etna_submit_rs_state(ctx, &copy_to_screen);
611 resource_written(ctx, &dst->base);
612 dst->seqno++;
613 dst->levels[blit_info->dst.level].ts_valid = false;
614
615 return TRUE;
616
617 manual:
618 if (src->layout == ETNA_LAYOUT_TILED && dst->layout == ETNA_LAYOUT_TILED) {
619 if ((src->status & ETNA_PENDING_WRITE) ||
620 (dst->status & ETNA_PENDING_WRITE))
621 pctx->flush(pctx, NULL, 0);
622 return etna_manual_blit(dst, dst_lev, dst_offset, src, src_lev, src_offset, blit_info);
623 }
624
625 return FALSE;
626 }
627
628 static void
629 etna_blit(struct pipe_context *pctx, const struct pipe_blit_info *blit_info)
630 {
631 /* This is a more extended version of resource_copy_region */
632 /* TODO Some cases can be handled by RS; if not, fall back to rendering or
633 * even CPU copy block of pixels from info->src to info->dst
634 * (resource, level, box, format);
635 * function is used for scaling, flipping in x and y direction (negative
636 * width/height), format conversion, mask and filter and even a scissor rectangle
637 *
638 * What can the RS do for us:
639 * convert between tiling formats (layouts)
640 * downsample 2x in x and y
641 * convert between a limited number of pixel formats
642 *
643 * For the rest, fall back to util_blitter
644 * XXX this goes wrong when source surface is supertiled. */
645 struct etna_context *ctx = etna_context(pctx);
646 struct pipe_blit_info info = *blit_info;
647
648 if (info.src.resource->nr_samples > 1 &&
649 info.dst.resource->nr_samples <= 1 &&
650 !util_format_is_depth_or_stencil(info.src.resource->format) &&
651 !util_format_is_pure_integer(info.src.resource->format)) {
652 DBG("color resolve unimplemented");
653 return;
654 }
655
656 if (etna_try_rs_blit(pctx, blit_info))
657 return;
658
659 if (util_try_blit_via_copy_region(pctx, blit_info))
660 return;
661
662 if (info.mask & PIPE_MASK_S) {
663 DBG("cannot blit stencil, skipping");
664 info.mask &= ~PIPE_MASK_S;
665 }
666
667 if (!util_blitter_is_blit_supported(ctx->blitter, &info)) {
668 DBG("blit unsupported %s -> %s",
669 util_format_short_name(info.src.resource->format),
670 util_format_short_name(info.dst.resource->format));
671 return;
672 }
673
674 etna_blit_save_state(ctx);
675 util_blitter_blit(ctx->blitter, &info);
676 }
677
678 static void
679 etna_flush_resource(struct pipe_context *pctx, struct pipe_resource *prsc)
680 {
681 struct etna_resource *rsc = etna_resource(prsc);
682
683 if (rsc->external) {
684 if (etna_resource_older(etna_resource(rsc->external), rsc)) {
685 etna_copy_resource(pctx, rsc->external, prsc, 0, 0);
686 etna_resource(rsc->external)->seqno = rsc->seqno;
687 }
688 } else if (etna_resource_needs_flush(rsc)) {
689 etna_copy_resource(pctx, prsc, prsc, 0, 0);
690 rsc->flush_seqno = rsc->seqno;
691 }
692 }
693
694 void
695 etna_copy_resource(struct pipe_context *pctx, struct pipe_resource *dst,
696 struct pipe_resource *src, int first_level, int last_level)
697 {
698 struct etna_resource *src_priv = etna_resource(src);
699 struct etna_resource *dst_priv = etna_resource(dst);
700
701 assert(src->format == dst->format);
702 assert(src->array_size == dst->array_size);
703 assert(last_level <= dst->last_level && last_level <= src->last_level);
704
705 struct pipe_blit_info blit = {};
706 blit.mask = util_format_get_mask(dst->format);
707 blit.filter = PIPE_TEX_FILTER_NEAREST;
708 blit.src.resource = src;
709 blit.src.format = src->format;
710 blit.dst.resource = dst;
711 blit.dst.format = dst->format;
712 blit.dst.box.depth = blit.src.box.depth = 1;
713
714 /* Copy each level and each layer */
715 for (int level = first_level; level <= last_level; level++) {
716 blit.src.level = blit.dst.level = level;
717 blit.src.box.width = blit.dst.box.width =
718 MIN2(src_priv->levels[level].padded_width, dst_priv->levels[level].padded_width);
719 blit.src.box.height = blit.dst.box.height =
720 MIN2(src_priv->levels[level].padded_height, dst_priv->levels[level].padded_height);
721
722 for (int layer = 0; layer < dst->array_size; layer++) {
723 blit.src.box.z = blit.dst.box.z = layer;
724 pctx->blit(pctx, &blit);
725 }
726 }
727 }
728
729 void
730 etna_copy_resource_box(struct pipe_context *pctx, struct pipe_resource *dst,
731 struct pipe_resource *src, int level,
732 struct pipe_box *box)
733 {
734 assert(src->format == dst->format);
735 assert(src->array_size == dst->array_size);
736
737 struct pipe_blit_info blit = {};
738 blit.mask = util_format_get_mask(dst->format);
739 blit.filter = PIPE_TEX_FILTER_NEAREST;
740 blit.src.resource = src;
741 blit.src.format = src->format;
742 blit.src.box = *box;
743 blit.dst.resource = dst;
744 blit.dst.format = dst->format;
745 blit.dst.box = *box;
746
747 blit.dst.box.depth = blit.src.box.depth = 1;
748 blit.src.level = blit.dst.level = level;
749
750 for (int layer = 0; layer < dst->array_size; layer++) {
751 blit.src.box.z = blit.dst.box.z = layer;
752 pctx->blit(pctx, &blit);
753 }
754 }
755
756 void
757 etna_clear_blit_init(struct pipe_context *pctx)
758 {
759 pctx->clear = etna_clear;
760 pctx->clear_render_target = etna_clear_render_target;
761 pctx->clear_depth_stencil = etna_clear_depth_stencil;
762 pctx->resource_copy_region = etna_resource_copy_region;
763 pctx->blit = etna_blit;
764 pctx->flush_resource = etna_flush_resource;
765 }