• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2017 Etnaviv Project
3  * Copyright (C) 2017 Zodiac Inflight Innovations
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a
6  * copy of this software and associated documentation files (the "Software"),
7  * to deal in the Software without restriction, including without limitation
8  * the rights to use, copy, modify, merge, publish, distribute, sub license,
9  * and/or sell copies of the Software, and to permit persons to whom the
10  * Software is furnished to do so, subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice (including the
13  * next paragraph) shall be included in all copies or substantial portions
14  * of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
19  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22  * DEALINGS IN THE SOFTWARE.
23  *
24  * Authors:
25  *    Wladimir J. van der Laan <laanwj@gmail.com>
26  */
27 #include "etnaviv_blt.h"
28 
29 #include "etnaviv_emit.h"
30 #include "etnaviv_clear_blit.h"
31 #include "etnaviv_context.h"
32 #include "etnaviv_emit.h"
33 #include "etnaviv_format.h"
34 #include "etnaviv_resource.h"
35 #include "etnaviv_surface.h"
36 #include "etnaviv_translate.h"
37 
38 #include "util/u_math.h"
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 #include "hw/common_3d.xml.h"
47 #include "hw/state_blt.xml.h"
48 #include "hw/common.xml.h"
49 
50 #include <assert.h>
51 
52 static uint32_t
etna_compatible_blt_format(enum pipe_format fmt)53 etna_compatible_blt_format(enum pipe_format fmt)
54 {
55    /* YUYV and UYVY are blocksize 4, but 2 bytes per pixel */
56    if (fmt == PIPE_FORMAT_YUYV || fmt == PIPE_FORMAT_UYVY)
57       return BLT_FORMAT_R8G8;
58 
59    switch (util_format_get_blocksize(fmt)) {
60    case 1: return BLT_FORMAT_R8;
61    case 2: return BLT_FORMAT_R8G8;
62    case 4: return BLT_FORMAT_A8R8G8B8;
63    case 8: return BLT_FORMAT_A16R16G16B16;
64    default: return ETNA_NO_MATCH;
65    }
66 }
67 
68 static inline uint32_t
blt_compute_stride_bits(const struct blt_imginfo * img)69 blt_compute_stride_bits(const struct blt_imginfo *img)
70 {
71    return VIVS_BLT_DEST_STRIDE_TILING(img->tiling == ETNA_LAYOUT_LINEAR ? 0 : 3) | /* 1/3? */
72           VIVS_BLT_DEST_STRIDE_FORMAT(img->format) |
73           VIVS_BLT_DEST_STRIDE_STRIDE(img->stride) |
74           COND(img->downsample_x, VIVS_BLT_SRC_STRIDE_DOWNSAMPLE_X) |
75           COND(img->downsample_y, VIVS_BLT_SRC_STRIDE_DOWNSAMPLE_Y);
76 }
77 
78 static inline uint32_t
blt_compute_img_config_bits(const struct blt_imginfo * img,bool for_dest)79 blt_compute_img_config_bits(const struct blt_imginfo *img, bool for_dest)
80 {
81    uint32_t tiling_bits = 0;
82    if (img->tiling == ETNA_LAYOUT_SUPER_TILED) {
83       tiling_bits |= for_dest ? BLT_IMAGE_CONFIG_TO_SUPER_TILED : BLT_IMAGE_CONFIG_FROM_SUPER_TILED;
84    }
85 
86    return BLT_IMAGE_CONFIG_TS_MODE(img->ts_mode) |
87           COND(img->use_ts, BLT_IMAGE_CONFIG_TS) |
88           COND(img->use_ts && img->ts_compress_fmt >= 0, BLT_IMAGE_CONFIG_COMPRESSION) |
89           BLT_IMAGE_CONFIG_COMPRESSION_FORMAT(img->ts_compress_fmt) |
90           COND(for_dest, BLT_IMAGE_CONFIG_UNK22) |
91           BLT_IMAGE_CONFIG_SWIZ_R(0) | /* not used? */
92           BLT_IMAGE_CONFIG_SWIZ_G(1) |
93           BLT_IMAGE_CONFIG_SWIZ_B(2) |
94           BLT_IMAGE_CONFIG_SWIZ_A(3) |
95           tiling_bits;
96 }
97 
98 static inline uint32_t
blt_compute_swizzle_bits(const struct blt_imginfo * img,bool for_dest)99 blt_compute_swizzle_bits(const struct blt_imginfo *img, bool for_dest)
100 {
101    uint32_t swiz = VIVS_BLT_SWIZZLE_SRC_R(img->swizzle[0]) |
102                    VIVS_BLT_SWIZZLE_SRC_G(img->swizzle[1]) |
103                    VIVS_BLT_SWIZZLE_SRC_B(img->swizzle[2]) |
104                    VIVS_BLT_SWIZZLE_SRC_A(img->swizzle[3]);
105    return for_dest ? (swiz << 12) : swiz;
106 }
107 
108 /* Clear (part of) an image */
109 static void
emit_blt_clearimage(struct etna_cmd_stream * stream,const struct blt_clear_op * op)110 emit_blt_clearimage(struct etna_cmd_stream *stream, const struct blt_clear_op *op)
111 {
112    etna_cmd_stream_reserve(stream, 64*2); /* Make sure BLT op doesn't get broken up */
113 
114    etna_set_state(stream, VIVS_BLT_ENABLE, 0x00000001);
115    assert(op->dest.bpp);
116    etna_set_state(stream, VIVS_BLT_CONFIG, VIVS_BLT_CONFIG_CLEAR_BPP(op->dest.bpp-1));
117    /* NB: blob sets format to 1 in dest/src config for clear, and the swizzle to RRRR.
118     * does this matter? It seems to just be ignored. But if we run into issues with BLT
119     * behaving stragely, it's something to look at.
120     */
121    etna_set_state(stream, VIVS_BLT_DEST_STRIDE, blt_compute_stride_bits(&op->dest));
122    etna_set_state(stream, VIVS_BLT_DEST_CONFIG, blt_compute_img_config_bits(&op->dest, true));
123    etna_set_state_reloc(stream, VIVS_BLT_DEST_ADDR, &op->dest.addr);
124    etna_set_state(stream, VIVS_BLT_SRC_STRIDE, blt_compute_stride_bits(&op->dest));
125    etna_set_state(stream, VIVS_BLT_SRC_CONFIG, blt_compute_img_config_bits(&op->dest, false));
126    etna_set_state_reloc(stream, VIVS_BLT_SRC_ADDR, &op->dest.addr);
127    etna_set_state(stream, VIVS_BLT_DEST_POS, VIVS_BLT_DEST_POS_X(op->rect_x) | VIVS_BLT_DEST_POS_Y(op->rect_y));
128    etna_set_state(stream, VIVS_BLT_IMAGE_SIZE, VIVS_BLT_IMAGE_SIZE_WIDTH(op->rect_w) | VIVS_BLT_IMAGE_SIZE_HEIGHT(op->rect_h));
129    etna_set_state(stream, VIVS_BLT_CLEAR_COLOR0, op->clear_value[0]);
130    etna_set_state(stream, VIVS_BLT_CLEAR_COLOR1, op->clear_value[1]);
131    etna_set_state(stream, VIVS_BLT_CLEAR_BITS0, op->clear_bits[0]);
132    etna_set_state(stream, VIVS_BLT_CLEAR_BITS1, op->clear_bits[1]);
133    if (op->dest.use_ts) {
134       etna_set_state_reloc(stream, VIVS_BLT_DEST_TS, &op->dest.ts_addr);
135       etna_set_state_reloc(stream, VIVS_BLT_SRC_TS, &op->dest.ts_addr);
136       etna_set_state(stream, VIVS_BLT_DEST_TS_CLEAR_VALUE0, op->dest.ts_clear_value[0]);
137       etna_set_state(stream, VIVS_BLT_DEST_TS_CLEAR_VALUE1, op->dest.ts_clear_value[1]);
138       etna_set_state(stream, VIVS_BLT_SRC_TS_CLEAR_VALUE0, op->dest.ts_clear_value[0]);
139       etna_set_state(stream, VIVS_BLT_SRC_TS_CLEAR_VALUE1, op->dest.ts_clear_value[1]);
140    }
141    etna_set_state(stream, VIVS_BLT_SET_COMMAND, 0x00000003);
142    etna_set_state(stream, VIVS_BLT_COMMAND, VIVS_BLT_COMMAND_COMMAND_CLEAR_IMAGE);
143    etna_set_state(stream, VIVS_BLT_SET_COMMAND, 0x00000003);
144    etna_set_state(stream, VIVS_BLT_ENABLE, 0x00000000);
145 
146    if (DBG_ENABLED(ETNA_DBG_DRAW_STALL))
147       etna_stall(stream, SYNC_RECIPIENT_FE, SYNC_RECIPIENT_PE);
148 }
149 
150 /* Copy (a subset of) an image to another image. */
151 static void
emit_blt_copyimage(struct etna_cmd_stream * stream,const struct blt_imgcopy_op * op)152 emit_blt_copyimage(struct etna_cmd_stream *stream, const struct blt_imgcopy_op *op)
153 {
154    etna_cmd_stream_reserve(stream, 64*2); /* Never allow BLT sequences to be broken up */
155 
156    etna_set_state(stream, VIVS_BLT_ENABLE, 0x00000001);
157    etna_set_state(stream, VIVS_BLT_CONFIG,
158            VIVS_BLT_CONFIG_SRC_ENDIAN(op->src.endian_mode) |
159            VIVS_BLT_CONFIG_DEST_ENDIAN(op->dest.endian_mode));
160    etna_set_state(stream, VIVS_BLT_SRC_STRIDE, blt_compute_stride_bits(&op->src));
161    etna_set_state(stream, VIVS_BLT_SRC_CONFIG, blt_compute_img_config_bits(&op->src, false));
162    etna_set_state(stream, VIVS_BLT_SWIZZLE,
163            blt_compute_swizzle_bits(&op->src, false) |
164            blt_compute_swizzle_bits(&op->dest, true));
165    etna_set_state(stream, VIVS_BLT_UNK140A0, 0x00040004);
166    etna_set_state(stream, VIVS_BLT_UNK1409C, 0x00400040);
167    if (op->src.use_ts) {
168       etna_set_state_reloc(stream, VIVS_BLT_SRC_TS, &op->src.ts_addr);
169       etna_set_state(stream, VIVS_BLT_SRC_TS_CLEAR_VALUE0, op->src.ts_clear_value[0]);
170       etna_set_state(stream, VIVS_BLT_SRC_TS_CLEAR_VALUE1, op->src.ts_clear_value[1]);
171    }
172    etna_set_state_reloc(stream, VIVS_BLT_SRC_ADDR, &op->src.addr);
173    etna_set_state(stream, VIVS_BLT_DEST_STRIDE, blt_compute_stride_bits(&op->dest));
174    etna_set_state(stream, VIVS_BLT_DEST_CONFIG,
175          blt_compute_img_config_bits(&op->dest, true) |
176          COND(op->flip_y, BLT_IMAGE_CONFIG_FLIP_Y));
177    assert(!op->dest.use_ts); /* Dest TS path doesn't work for copies? */
178    if (op->dest.use_ts) {
179       etna_set_state_reloc(stream, VIVS_BLT_DEST_TS, &op->dest.ts_addr);
180       etna_set_state(stream, VIVS_BLT_DEST_TS_CLEAR_VALUE0, op->dest.ts_clear_value[0]);
181       etna_set_state(stream, VIVS_BLT_DEST_TS_CLEAR_VALUE1, op->dest.ts_clear_value[1]);
182    }
183    etna_set_state_reloc(stream, VIVS_BLT_DEST_ADDR, &op->dest.addr);
184    etna_set_state(stream, VIVS_BLT_SRC_POS, VIVS_BLT_DEST_POS_X(op->src_x) | VIVS_BLT_DEST_POS_Y(op->src_y));
185    etna_set_state(stream, VIVS_BLT_DEST_POS, VIVS_BLT_DEST_POS_X(op->dest_x) | VIVS_BLT_DEST_POS_Y(op->dest_y));
186    etna_set_state(stream, VIVS_BLT_IMAGE_SIZE, VIVS_BLT_IMAGE_SIZE_WIDTH(op->rect_w) | VIVS_BLT_IMAGE_SIZE_HEIGHT(op->rect_h));
187    etna_set_state(stream, VIVS_BLT_UNK14058, 0xffffffff);
188    etna_set_state(stream, VIVS_BLT_UNK1405C, 0xffffffff);
189    etna_set_state(stream, VIVS_BLT_SET_COMMAND, 0x00000003);
190    etna_set_state(stream, VIVS_BLT_COMMAND, VIVS_BLT_COMMAND_COMMAND_COPY_IMAGE);
191    etna_set_state(stream, VIVS_BLT_SET_COMMAND, 0x00000003);
192    etna_set_state(stream, VIVS_BLT_ENABLE, 0x00000000);
193 
194    if (DBG_ENABLED(ETNA_DBG_DRAW_STALL))
195       etna_stall(stream, SYNC_RECIPIENT_FE, SYNC_RECIPIENT_PE);
196 }
197 
198 /* Emit in-place resolve using BLT. */
199 static void
emit_blt_inplace(struct etna_cmd_stream * stream,const struct blt_inplace_op * op)200 emit_blt_inplace(struct etna_cmd_stream *stream, const struct blt_inplace_op *op)
201 {
202    assert(op->bpp > 0 && util_is_power_of_two_nonzero(op->bpp));
203    etna_cmd_stream_reserve(stream, 64*2); /* Never allow BLT sequences to be broken up */
204    etna_set_state(stream, VIVS_BLT_ENABLE, 0x00000001);
205    etna_set_state(stream, VIVS_BLT_CONFIG,
206          VIVS_BLT_CONFIG_INPLACE_TS_MODE(op->ts_mode) |
207          VIVS_BLT_CONFIG_INPLACE_BOTH |
208          (util_logbase2(op->bpp) << VIVS_BLT_CONFIG_INPLACE_BPP__SHIFT));
209    etna_set_state(stream, VIVS_BLT_DEST_TS_CLEAR_VALUE0, op->ts_clear_value[0]);
210    etna_set_state(stream, VIVS_BLT_DEST_TS_CLEAR_VALUE1, op->ts_clear_value[1]);
211    etna_set_state_reloc(stream, VIVS_BLT_DEST_ADDR, &op->addr);
212    etna_set_state_reloc(stream, VIVS_BLT_DEST_TS, &op->ts_addr);
213    etna_set_state(stream, 0x14068, op->num_tiles);
214    etna_set_state(stream, VIVS_BLT_SET_COMMAND, 0x00000003);
215    etna_set_state(stream, VIVS_BLT_COMMAND, 0x00000004);
216    etna_set_state(stream, VIVS_BLT_SET_COMMAND, 0x00000003);
217    etna_set_state(stream, VIVS_BLT_ENABLE, 0x00000000);
218 
219    if (DBG_ENABLED(ETNA_DBG_DRAW_STALL))
220       etna_stall(stream, SYNC_RECIPIENT_FE, SYNC_RECIPIENT_PE);
221 }
222 
223 static void
etna_blit_clear_color_blt(struct pipe_context * pctx,unsigned idx,const union pipe_color_union * color)224 etna_blit_clear_color_blt(struct pipe_context *pctx, unsigned idx,
225                       const union pipe_color_union *color)
226 {
227    struct etna_context *ctx = etna_context(pctx);
228    struct pipe_surface *dst = ctx->framebuffer_s.cbufs[idx];
229    struct etna_surface *surf = etna_surface(dst);
230    uint64_t new_clear_value = etna_clear_blit_pack_rgba(surf->base.format, color);
231    int msaa_xscale = 1, msaa_yscale = 1;
232 
233    translate_samples_to_xyscale(surf->base.texture->nr_samples,
234                                 &msaa_xscale, &msaa_yscale);
235 
236    struct etna_resource *res = etna_resource(surf->base.texture);
237    struct blt_clear_op clr = {};
238    clr.dest.addr.bo = res->bo;
239    clr.dest.addr.offset = surf->offset;
240    clr.dest.addr.flags = ETNA_RELOC_WRITE;
241    clr.dest.bpp = util_format_get_blocksize(surf->base.format);
242    clr.dest.stride = surf->level->stride;
243    clr.dest.tiling = res->layout;
244 
245    if (surf->level->ts_size) {
246       clr.dest.use_ts = 1;
247       clr.dest.ts_addr.bo = res->ts_bo;
248       clr.dest.ts_addr.offset = surf->ts_offset;
249       clr.dest.ts_addr.flags = ETNA_RELOC_WRITE;
250       clr.dest.ts_clear_value[0] = new_clear_value;
251       clr.dest.ts_clear_value[1] = new_clear_value >> 32;
252       clr.dest.ts_mode = surf->level->ts_mode;
253       clr.dest.ts_compress_fmt = surf->level->ts_compress_fmt;
254    }
255 
256    clr.clear_value[0] = new_clear_value;
257    clr.clear_value[1] = new_clear_value >> 32;
258    clr.clear_bits[0] = 0xffffffff; /* TODO: Might want to clear only specific channels? */
259    clr.clear_bits[1] = 0xffffffff;
260    clr.rect_x = 0; /* What about scissors? */
261    clr.rect_y = 0;
262    clr.rect_w = surf->level->width * msaa_xscale;
263    clr.rect_h = surf->level->height * msaa_yscale;
264 
265    emit_blt_clearimage(ctx->stream, &clr);
266 
267    /* This made the TS valid */
268    if (surf->level->ts_size) {
269       if (idx == 0) {
270          ctx->framebuffer.TS_COLOR_CLEAR_VALUE = new_clear_value;
271          ctx->framebuffer.TS_COLOR_CLEAR_VALUE_EXT = new_clear_value >> 32;
272       } else {
273          ctx->framebuffer.RT_TS_COLOR_CLEAR_VALUE[idx - 1] = new_clear_value;
274          ctx->framebuffer.RT_TS_COLOR_CLEAR_VALUE_EXT[idx - 1] = new_clear_value >> 32;
275       }
276 
277       /* update clear color in SW meta area of the buffer if TS is exported */
278       if (unlikely(new_clear_value != surf->level->clear_value &&
279           etna_resource_ext_ts(etna_resource(dst->texture))))
280          surf->level->ts_meta->v0.clear_value = new_clear_value;
281 
282       etna_resource_level_ts_mark_valid(surf->level);
283       etna_resource_level_mark_unflushed(surf->level);
284       ctx->dirty |= ETNA_DIRTY_TS | ETNA_DIRTY_DERIVE_TS;
285    }
286 
287    surf->level->clear_value = new_clear_value;
288    resource_written(ctx, surf->base.texture);
289    etna_resource_level_mark_changed(surf->level);
290 }
291 
292 static void
etna_blit_clear_zs_blt(struct pipe_context * pctx,struct pipe_surface * dst,unsigned buffers,double depth,unsigned stencil)293 etna_blit_clear_zs_blt(struct pipe_context *pctx, struct pipe_surface *dst,
294                    unsigned buffers, double depth, unsigned stencil)
295 {
296    struct etna_context *ctx = etna_context(pctx);
297    struct etna_surface *surf = etna_surface(dst);
298    uint32_t new_clear_value = translate_clear_depth_stencil(surf->base.format, depth, stencil);
299    uint32_t new_clear_bits = 0, clear_bits_depth, clear_bits_stencil;
300    int msaa_xscale = 1, msaa_yscale = 1;
301 
302    translate_samples_to_xyscale(surf->base.texture->nr_samples,
303                                 &msaa_xscale, &msaa_yscale);
304 
305    /* Get the channels to clear */
306    switch (surf->base.format) {
307    case PIPE_FORMAT_Z16_UNORM:
308    case PIPE_FORMAT_X8Z24_UNORM:
309       clear_bits_depth = 0xffffffff;
310       clear_bits_stencil = 0x00000000;
311       break;
312    case PIPE_FORMAT_S8_UINT_Z24_UNORM:
313       clear_bits_depth = 0xffffff00;
314       clear_bits_stencil = 0x000000ff;
315       break;
316    default:
317       clear_bits_depth = clear_bits_stencil = 0xffffffff;
318       break;
319    }
320 
321    if (buffers & PIPE_CLEAR_DEPTH)
322       new_clear_bits |= clear_bits_depth;
323    if (buffers & PIPE_CLEAR_STENCIL)
324       new_clear_bits |= clear_bits_stencil;
325 
326    /* if all bits are cleared, update TS clear value */
327    if (new_clear_bits == 0xffffffff)
328       surf->level->clear_value = new_clear_value;
329 
330    /* TODO unduplicate this */
331    struct etna_resource *res = etna_resource(surf->base.texture);
332    struct blt_clear_op clr = {};
333    clr.dest.addr.bo = res->bo;
334    clr.dest.addr.offset = surf->offset;
335    clr.dest.addr.flags = ETNA_RELOC_WRITE;
336    clr.dest.bpp = util_format_get_blocksize(surf->base.format);
337    clr.dest.stride = surf->level->stride;
338    clr.dest.tiling = res->layout;
339 
340    if (surf->level->ts_size) {
341       clr.dest.use_ts = 1;
342       clr.dest.ts_addr.bo = res->ts_bo;
343       clr.dest.ts_addr.offset = surf->ts_offset;
344       clr.dest.ts_addr.flags = ETNA_RELOC_WRITE;
345       clr.dest.ts_clear_value[0] = surf->level->clear_value;
346       clr.dest.ts_clear_value[1] = surf->level->clear_value;
347       clr.dest.ts_mode = surf->level->ts_mode;
348       clr.dest.ts_compress_fmt = surf->level->ts_compress_fmt;
349    }
350 
351    clr.clear_value[0] = new_clear_value;
352    clr.clear_value[1] = new_clear_value;
353    clr.clear_bits[0] = new_clear_bits;
354    clr.clear_bits[1] = new_clear_bits;
355    clr.rect_x = 0; /* What about scissors? */
356    clr.rect_y = 0;
357    clr.rect_w = surf->level->width * msaa_xscale;
358    clr.rect_h = surf->level->height * msaa_yscale;
359 
360    emit_blt_clearimage(ctx->stream, &clr);
361 
362    /* This made the TS valid */
363    if (surf->level->ts_size) {
364       ctx->framebuffer.TS_DEPTH_CLEAR_VALUE = surf->level->clear_value;
365       etna_resource_level_ts_mark_valid(surf->level);
366       etna_resource_level_mark_unflushed(surf->level);
367       ctx->dirty |= ETNA_DIRTY_TS | ETNA_DIRTY_DERIVE_TS;
368    }
369 
370    resource_written(ctx, surf->base.texture);
371    etna_resource_level_mark_changed(surf->level);
372 }
373 
374 static void
etna_clear_blt(struct pipe_context * pctx,unsigned buffers,const struct pipe_scissor_state * scissor_state,const union pipe_color_union * color,double depth,unsigned stencil)375 etna_clear_blt(struct pipe_context *pctx, unsigned buffers, const struct pipe_scissor_state *scissor_state,
376            const union pipe_color_union *color, double depth, unsigned stencil)
377 {
378    struct etna_context *ctx = etna_context(pctx);
379 
380    if (!etna_render_condition_check(pctx))
381       return;
382 
383    etna_set_state(ctx->stream, VIVS_GL_FLUSH_CACHE, 0x00000c23);
384    etna_set_state(ctx->stream, VIVS_TS_FLUSH_CACHE, VIVS_TS_FLUSH_CACHE_FLUSH);
385 
386    if (buffers & PIPE_CLEAR_COLOR) {
387       for (int idx = 0; idx < ctx->framebuffer_s.nr_cbufs; ++idx) {
388          struct etna_surface *surf = etna_surface(ctx->framebuffer_s.cbufs[idx]);
389 
390          if (!surf)
391             continue;
392 
393          etna_blit_clear_color_blt(pctx, idx, color);
394 
395          if (!etna_resource(surf->prsc)->explicit_flush)
396             etna_context_add_flush_resource(ctx, surf->prsc);
397       }
398    }
399 
400    if ((buffers & PIPE_CLEAR_DEPTHSTENCIL) && ctx->framebuffer_s.zsbuf != NULL)
401       etna_blit_clear_zs_blt(pctx, ctx->framebuffer_s.zsbuf, buffers, depth, stencil);
402 
403    etna_stall(ctx->stream, SYNC_RECIPIENT_RA, SYNC_RECIPIENT_BLT);
404 
405    if ((buffers & PIPE_CLEAR_COLOR) && (buffers & PIPE_CLEAR_DEPTH))
406       etna_set_state(ctx->stream, VIVS_GL_FLUSH_CACHE, 0x00000c23);
407    else
408       etna_set_state(ctx->stream, VIVS_GL_FLUSH_CACHE, 0x00000002);
409 }
410 
411 static bool
etna_try_blt_blit(struct pipe_context * pctx,const struct pipe_blit_info * blit_info)412 etna_try_blt_blit(struct pipe_context *pctx,
413                  const struct pipe_blit_info *blit_info)
414 {
415    struct etna_context *ctx = etna_context(pctx);
416    struct etna_resource *src = etna_resource(blit_info->src.resource);
417    struct etna_resource *dst = etna_resource(blit_info->dst.resource);
418    int src_xscale, src_yscale, dst_xscale, dst_yscale;
419    bool downsample_x = false, downsample_y = false;
420 
421    /* Ensure that the level is valid */
422    assert(blit_info->src.level <= src->base.last_level);
423    assert(blit_info->dst.level <= dst->base.last_level);
424 
425    if (!translate_samples_to_xyscale(src->base.nr_samples, &src_xscale, &src_yscale))
426       return false;
427    if (!translate_samples_to_xyscale(dst->base.nr_samples, &dst_xscale, &dst_yscale))
428       return false;
429 
430    /* BLT does not support upscaling */
431    if ((src_xscale < dst_xscale) || (src_yscale < dst_yscale)) {
432       DBG("upscaling requested: source %dx%d destination %dx%d",
433           src_xscale, src_yscale, dst_xscale, dst_yscale);
434       return false;
435    }
436 
437    if (src_xscale > dst_xscale)
438       downsample_x = true;
439    if (src_yscale > dst_yscale)
440       downsample_y = true;
441 
442    /* The width/height are in pixels; they do not change as a result of
443     * multi-sampling. So, when blitting from a 4x multisampled surface
444     * to a non-multisampled surface, the width and height will be
445     * identical. As we do not support scaling, reject different sizes.
446     * TODO: could handle 2x downsample here with emit_blt_genmipmaps */
447    if (blit_info->dst.box.width != blit_info->src.box.width ||
448        blit_info->dst.box.height != abs(blit_info->src.box.height)) { /* allow y flip for glTexImage2D */
449       DBG("scaling requested: source %dx%d destination %dx%d",
450           blit_info->src.box.width, blit_info->src.box.height,
451           blit_info->dst.box.width, blit_info->dst.box.height);
452       return false;
453    }
454 
455    /* No masks - not sure if BLT can copy individual channels */
456    unsigned mask = util_format_get_mask(blit_info->dst.format);
457    if ((blit_info->mask & mask) != mask) {
458       DBG("sub-mask requested: 0x%02x vs format mask 0x%02x", blit_info->mask, mask);
459       return false;
460    }
461 
462    /* Only support same format (used tiling/detiling) blits for now.
463     * TODO: figure out which different-format blits are possible and test them
464     *  - need to use correct swizzle
465     *  - set sRGB bits correctly
466     *  - avoid trying to convert between float/int formats?
467     */
468    if (blit_info->src.format != blit_info->dst.format) {
469       DBG("non matching formats: %s vs %s",
470           util_format_short_name(blit_info->src.format),
471           util_format_short_name(blit_info->dst.format));
472       return false;
473    }
474 
475    /* try to find a exact format match first */
476    uint32_t format = translate_blt_format(blit_info->dst.format);
477    /* When not resolving MSAA, but only doing a layout conversion, we can get
478     * away with a fallback format of matching size.
479     */
480    if (format == ETNA_NO_MATCH && !downsample_x && !downsample_y)
481       format = etna_compatible_blt_format(blit_info->dst.format);
482    if (format == ETNA_NO_MATCH) {
483       DBG("format not supported: %s", util_format_short_name(blit_info->dst.format));
484       return false;
485    }
486 
487    if (blit_info->scissor_enable ||
488        blit_info->swizzle_enable ||
489        blit_info->dst.box.depth != blit_info->src.box.depth ||
490        blit_info->dst.box.depth != 1) {
491       return false;
492    }
493 
494    struct etna_resource_level *src_lev = &src->levels[blit_info->src.level];
495    struct etna_resource_level *dst_lev = &dst->levels[blit_info->dst.level];
496 
497    /* if we asked for in-place resolve, return immediately if ts isn't valid
498     * do this check separately because it applies when compression is used, but
499     * we can't use inplace resolve path with compression
500     */
501    if (src == dst) {
502       if (memcmp(&blit_info->src, &blit_info->dst, sizeof(blit_info->src)))
503          return false;
504 
505       if (!etna_resource_level_ts_valid(src_lev)) /* No TS, no worries */
506          return true;
507    }
508 
509    /* Flush destination, as the blit will invalidate any pending TS changes. */
510    if (dst != src && etna_resource_level_needs_flush(dst_lev))
511       etna_copy_resource(pctx, &dst->base, &dst->base,
512                          blit_info->dst.level, blit_info->dst.level);
513 
514    /* Kick off BLT here */
515    if (src == dst && src_lev->ts_compress_fmt < 0) {
516       /* Resolve-in-place */
517       struct blt_inplace_op op = {};
518       size_t tile_size = etna_screen_get_tile_size(ctx->screen, src_lev->ts_mode,
519                                                    src->base.nr_samples > 1);
520 
521       op.addr.bo = src->bo;
522       op.addr.offset = src_lev->offset + blit_info->src.box.z * src_lev->layer_stride;
523       op.addr.flags = ETNA_RELOC_READ | ETNA_RELOC_WRITE;
524       op.ts_addr.bo = src->ts_bo;
525       op.ts_addr.offset = src_lev->ts_offset + blit_info->src.box.z * src_lev->ts_layer_stride;
526       op.ts_addr.flags = ETNA_RELOC_READ;
527       op.ts_clear_value[0] = src_lev->clear_value;
528       op.ts_clear_value[1] = src_lev->clear_value >> 32;
529       op.ts_mode = src_lev->ts_mode;
530       op.num_tiles = DIV_ROUND_UP(src_lev->size, tile_size);
531       op.bpp = util_format_get_blocksize(src->base.format);
532 
533       etna_set_state(ctx->stream, VIVS_GL_FLUSH_CACHE, 0x00000c23);
534       etna_set_state(ctx->stream, VIVS_TS_FLUSH_CACHE, 0x00000001);
535       emit_blt_inplace(ctx->stream, &op);
536    } else {
537       /* Copy op */
538       struct blt_imgcopy_op op = {};
539 
540       op.src.addr.bo = src->bo;
541       op.src.addr.offset = src_lev->offset + blit_info->src.box.z * src_lev->layer_stride;
542       op.src.addr.flags = ETNA_RELOC_READ;
543       op.src.format = format;
544       op.src.stride = src_lev->stride;
545       op.src.tiling = src->layout;
546       op.src.downsample_x = downsample_x;
547       op.src.downsample_y = downsample_y;
548       for (unsigned x=0; x<4; ++x)
549          op.src.swizzle[x] = x;
550 
551       if (etna_resource_level_ts_valid(src_lev)) {
552          op.src.use_ts = 1;
553          op.src.ts_addr.bo = src->ts_bo;
554          op.src.ts_addr.offset = src_lev->ts_offset + blit_info->src.box.z * src_lev->ts_layer_stride;
555          op.src.ts_addr.flags = ETNA_RELOC_READ;
556          op.src.ts_clear_value[0] = src_lev->clear_value;
557          op.src.ts_clear_value[1] = src_lev->clear_value >> 32;
558          op.src.ts_mode = src_lev->ts_mode;
559          op.src.ts_compress_fmt = src_lev->ts_compress_fmt;
560       }
561 
562       op.dest.addr.bo = dst->bo;
563       op.dest.addr.offset = dst_lev->offset + blit_info->dst.box.z * dst_lev->layer_stride;
564       op.dest.addr.flags = ETNA_RELOC_WRITE;
565       op.dest.format = format;
566       op.dest.stride = dst_lev->stride;
567       op.dest.tiling = dst->layout;
568       for (unsigned x=0; x<4; ++x)
569          op.dest.swizzle[x] = x;
570 
571       op.dest_x = blit_info->dst.box.x;
572       op.dest_y = blit_info->dst.box.y;
573       op.src_x = blit_info->src.box.x;
574       op.src_y = blit_info->src.box.y;
575       op.rect_w = blit_info->dst.box.width;
576       op.rect_h = blit_info->dst.box.height;
577 
578       assert(op.dest_x < dst_lev->padded_width);
579       assert(op.dest_y < dst_lev->padded_height);
580       assert((op.dest_x + op.rect_w) <= dst_lev->padded_width);
581       assert((op.dest_y + op.rect_h) <= dst_lev->padded_height);
582 
583       if (blit_info->src.box.height < 0) { /* flipped? fix up base y */
584          op.flip_y = 1;
585          op.src_y += blit_info->src.box.height;
586       }
587 
588       op.src_x *= src_xscale;
589       op.src_y *= src_yscale;
590       op.rect_w *= src_xscale;
591       op.rect_h *= src_yscale;
592 
593       assert(op.src_x < src_lev->padded_width);
594       assert(op.src_y < src_lev->padded_height);
595       assert((op.src_x + op.rect_w) <= src_lev->padded_width);
596       assert((op.src_y + op.rect_h) <= src_lev->padded_height);
597 
598       etna_set_state(ctx->stream, VIVS_GL_FLUSH_CACHE, 0x00000c23);
599       etna_set_state(ctx->stream, VIVS_TS_FLUSH_CACHE, 0x00000001);
600       emit_blt_copyimage(ctx->stream, &op);
601    }
602 
603    /* Make FE wait for BLT, in case we want to do something with the image next.
604     * This probably shouldn't be here, and depend on what is done with the resource.
605     */
606    etna_stall(ctx->stream, SYNC_RECIPIENT_FE, SYNC_RECIPIENT_BLT);
607    etna_set_state(ctx->stream, VIVS_GL_FLUSH_CACHE, 0x00000c23);
608 
609    resource_read(ctx, &src->base);
610    resource_written(ctx, &dst->base);
611 
612    etna_resource_level_mark_changed(dst_lev);
613 
614    /* We don't need to mark the TS as invalid if this was just a flush without
615     * compression, as in that case only clear tiles are filled and the tile
616     * status still matches the blit target buffer. For compressed formats the
617     * tiles are decompressed, so tile status doesn't match anymore.
618     */
619    if (src != dst || src_lev->ts_compress_fmt >= 0)
620       etna_resource_level_ts_mark_invalid(dst_lev);
621 
622    return true;
623 }
624 
625 void
etna_clear_blit_blt_init(struct pipe_context * pctx)626 etna_clear_blit_blt_init(struct pipe_context *pctx)
627 {
628    struct etna_context *ctx = etna_context(pctx);
629 
630    DBG("etnaviv: Using BLT blit engine");
631    pctx->clear = etna_clear_blt;
632    ctx->blit = etna_try_blt_blit;
633 }
634