1bf215546Sopenharmony_ci/*
2bf215546Sopenharmony_ci * Copyright (c) 2012-2017 Etnaviv Project
3bf215546Sopenharmony_ci *
4bf215546Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a
5bf215546Sopenharmony_ci * copy of this software and associated documentation files (the "Software"),
6bf215546Sopenharmony_ci * to deal in the Software without restriction, including without limitation
7bf215546Sopenharmony_ci * the rights to use, copy, modify, merge, publish, distribute, sub license,
8bf215546Sopenharmony_ci * and/or sell copies of the Software, and to permit persons to whom the
9bf215546Sopenharmony_ci * Software is furnished to do so, subject to the following conditions:
10bf215546Sopenharmony_ci *
11bf215546Sopenharmony_ci * The above copyright notice and this permission notice (including the
12bf215546Sopenharmony_ci * next paragraph) shall be included in all copies or substantial portions
13bf215546Sopenharmony_ci * of the Software.
14bf215546Sopenharmony_ci *
15bf215546Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16bf215546Sopenharmony_ci * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17bf215546Sopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
18bf215546Sopenharmony_ci * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19bf215546Sopenharmony_ci * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20bf215546Sopenharmony_ci * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21bf215546Sopenharmony_ci * DEALINGS IN THE SOFTWARE.
22bf215546Sopenharmony_ci *
23bf215546Sopenharmony_ci * Authors:
24bf215546Sopenharmony_ci *    Wladimir J. van der Laan <laanwj@gmail.com>
25bf215546Sopenharmony_ci */
26bf215546Sopenharmony_ci
27bf215546Sopenharmony_ci#include "etnaviv_rs.h"
28bf215546Sopenharmony_ci
29bf215546Sopenharmony_ci#include "etnaviv_clear_blit.h"
30bf215546Sopenharmony_ci#include "etnaviv_context.h"
31bf215546Sopenharmony_ci#include "etnaviv_emit.h"
32bf215546Sopenharmony_ci#include "etnaviv_format.h"
33bf215546Sopenharmony_ci#include "etnaviv_resource.h"
34bf215546Sopenharmony_ci#include "etnaviv_screen.h"
35bf215546Sopenharmony_ci#include "etnaviv_surface.h"
36bf215546Sopenharmony_ci#include "etnaviv_tiling.h"
37bf215546Sopenharmony_ci#include "etnaviv_translate.h"
38bf215546Sopenharmony_ci#include "etnaviv_util.h"
39bf215546Sopenharmony_ci
40bf215546Sopenharmony_ci#include "pipe/p_defines.h"
41bf215546Sopenharmony_ci#include "pipe/p_state.h"
42bf215546Sopenharmony_ci#include "util/compiler.h"
43bf215546Sopenharmony_ci#include "util/u_blitter.h"
44bf215546Sopenharmony_ci#include "util/u_inlines.h"
45bf215546Sopenharmony_ci#include "util/u_memory.h"
46bf215546Sopenharmony_ci#include "util/u_surface.h"
47bf215546Sopenharmony_ci
48bf215546Sopenharmony_ci#include "hw/common.xml.h"
49bf215546Sopenharmony_ci#include "hw/state.xml.h"
50bf215546Sopenharmony_ci#include "hw/state_3d.xml.h"
51bf215546Sopenharmony_ci
52bf215546Sopenharmony_ci#include <assert.h>
53bf215546Sopenharmony_ci
54bf215546Sopenharmony_ci/* return a RS "compatible" format for use when copying */
55bf215546Sopenharmony_cistatic uint32_t
56bf215546Sopenharmony_cietna_compatible_rs_format(enum pipe_format fmt)
57bf215546Sopenharmony_ci{
58bf215546Sopenharmony_ci   /* YUYV and UYVY are blocksize 4, but 2 bytes per pixel */
59bf215546Sopenharmony_ci   if (fmt == PIPE_FORMAT_YUYV || fmt == PIPE_FORMAT_UYVY)
60bf215546Sopenharmony_ci      return RS_FORMAT_A4R4G4B4;
61bf215546Sopenharmony_ci
62bf215546Sopenharmony_ci   switch (util_format_get_blocksize(fmt)) {
63bf215546Sopenharmony_ci   case 2: return RS_FORMAT_A4R4G4B4;
64bf215546Sopenharmony_ci   case 4: return RS_FORMAT_A8R8G8B8;
65bf215546Sopenharmony_ci   default: return ETNA_NO_MATCH;
66bf215546Sopenharmony_ci   }
67bf215546Sopenharmony_ci}
68bf215546Sopenharmony_ci
69bf215546Sopenharmony_civoid
70bf215546Sopenharmony_cietna_compile_rs_state(struct etna_context *ctx, struct compiled_rs_state *cs,
71bf215546Sopenharmony_ci                      const struct rs_state *rs)
72bf215546Sopenharmony_ci{
73bf215546Sopenharmony_ci   struct etna_screen *screen = ctx->screen;
74bf215546Sopenharmony_ci
75bf215546Sopenharmony_ci   memset(cs, 0, sizeof(*cs));
76bf215546Sopenharmony_ci
77bf215546Sopenharmony_ci   /* TILED and SUPERTILED layout have their strides multiplied with 4 in RS */
78bf215546Sopenharmony_ci   unsigned source_stride_shift = COND(rs->source_tiling != ETNA_LAYOUT_LINEAR, 2);
79bf215546Sopenharmony_ci   unsigned dest_stride_shift = COND(rs->dest_tiling != ETNA_LAYOUT_LINEAR, 2);
80bf215546Sopenharmony_ci
81bf215546Sopenharmony_ci   bool src_tiled = rs->source_tiling & ETNA_LAYOUT_BIT_TILE;
82bf215546Sopenharmony_ci   bool dst_tiled = rs->dest_tiling & ETNA_LAYOUT_BIT_TILE;
83bf215546Sopenharmony_ci   bool src_super = rs->source_tiling & ETNA_LAYOUT_BIT_SUPER;
84bf215546Sopenharmony_ci   bool dst_super = rs->dest_tiling & ETNA_LAYOUT_BIT_SUPER;
85bf215546Sopenharmony_ci   bool src_multi = rs->source_tiling & ETNA_LAYOUT_BIT_MULTI;
86bf215546Sopenharmony_ci   bool dst_multi = rs->dest_tiling & ETNA_LAYOUT_BIT_MULTI;
87bf215546Sopenharmony_ci
88bf215546Sopenharmony_ci   /* Vivante RS needs widths to be a multiple of 16 or bad things
89bf215546Sopenharmony_ci    * happen, such as scribbing over memory, or the GPU hanging,
90bf215546Sopenharmony_ci    * even for non-tiled formats.  As this is serious, use abort().
91bf215546Sopenharmony_ci    */
92bf215546Sopenharmony_ci   if (rs->width & ETNA_RS_WIDTH_MASK)
93bf215546Sopenharmony_ci      abort();
94bf215546Sopenharmony_ci
95bf215546Sopenharmony_ci   /* TODO could just pre-generate command buffer, would simply submit to one memcpy */
96bf215546Sopenharmony_ci   cs->RS_CONFIG = VIVS_RS_CONFIG_SOURCE_FORMAT(rs->source_format) |
97bf215546Sopenharmony_ci                   COND(rs->downsample_x, VIVS_RS_CONFIG_DOWNSAMPLE_X) |
98bf215546Sopenharmony_ci                   COND(rs->downsample_y, VIVS_RS_CONFIG_DOWNSAMPLE_Y) |
99bf215546Sopenharmony_ci                   COND(src_tiled, VIVS_RS_CONFIG_SOURCE_TILED) |
100bf215546Sopenharmony_ci                   VIVS_RS_CONFIG_DEST_FORMAT(rs->dest_format) |
101bf215546Sopenharmony_ci                   COND(dst_tiled, VIVS_RS_CONFIG_DEST_TILED) |
102bf215546Sopenharmony_ci                   COND(rs->swap_rb, VIVS_RS_CONFIG_SWAP_RB) |
103bf215546Sopenharmony_ci                   COND(rs->flip, VIVS_RS_CONFIG_FLIP);
104bf215546Sopenharmony_ci
105bf215546Sopenharmony_ci   cs->RS_SOURCE_STRIDE = (rs->source_stride << source_stride_shift) |
106bf215546Sopenharmony_ci                          COND(src_super, VIVS_RS_SOURCE_STRIDE_TILING) |
107bf215546Sopenharmony_ci                          COND(src_multi, VIVS_RS_SOURCE_STRIDE_MULTI);
108bf215546Sopenharmony_ci
109bf215546Sopenharmony_ci   if (VIV_FEATURE(ctx->screen, chipMinorFeatures6, CACHE128B256BPERLINE))
110bf215546Sopenharmony_ci      cs->RS_SOURCE_STRIDE |= VIVS_RS_SOURCE_STRIDE_TS_MODE(rs->source_ts_mode) |
111bf215546Sopenharmony_ci                              COND(src_super, VIVS_RS_SOURCE_STRIDE_SUPER_TILED_NEW);
112bf215546Sopenharmony_ci
113bf215546Sopenharmony_ci   /* Initially all pipes are set to the base address of the source and
114bf215546Sopenharmony_ci    * destination buffer respectively. This will be overridden below as
115bf215546Sopenharmony_ci    * necessary for the multi-pipe, multi-tiled case.
116bf215546Sopenharmony_ci    */
117bf215546Sopenharmony_ci   for (unsigned pipe = 0; pipe < screen->specs.pixel_pipes; ++pipe) {
118bf215546Sopenharmony_ci      cs->source[pipe].bo = rs->source;
119bf215546Sopenharmony_ci      cs->source[pipe].offset = rs->source_offset;
120bf215546Sopenharmony_ci      cs->source[pipe].flags = ETNA_RELOC_READ;
121bf215546Sopenharmony_ci
122bf215546Sopenharmony_ci      cs->dest[pipe].bo = rs->dest;
123bf215546Sopenharmony_ci      cs->dest[pipe].offset = rs->dest_offset;
124bf215546Sopenharmony_ci      cs->dest[pipe].flags = ETNA_RELOC_WRITE;
125bf215546Sopenharmony_ci
126bf215546Sopenharmony_ci      cs->RS_PIPE_OFFSET[pipe] = VIVS_RS_PIPE_OFFSET_X(0) | VIVS_RS_PIPE_OFFSET_Y(0);
127bf215546Sopenharmony_ci   }
128bf215546Sopenharmony_ci
129bf215546Sopenharmony_ci   cs->RS_DEST_STRIDE = (rs->dest_stride << dest_stride_shift) |
130bf215546Sopenharmony_ci                        COND(dst_super, VIVS_RS_DEST_STRIDE_TILING) |
131bf215546Sopenharmony_ci                        COND(dst_multi, VIVS_RS_DEST_STRIDE_MULTI);
132bf215546Sopenharmony_ci
133bf215546Sopenharmony_ci   if (VIV_FEATURE(ctx->screen, chipMinorFeatures6, CACHE128B256BPERLINE))
134bf215546Sopenharmony_ci      cs->RS_DEST_STRIDE |= COND(dst_super, VIVS_RS_DEST_STRIDE_SUPER_TILED_NEW);
135bf215546Sopenharmony_ci
136bf215546Sopenharmony_ci   if (src_multi)
137bf215546Sopenharmony_ci      cs->source[1].offset = rs->source_offset + rs->source_stride * rs->source_padded_height / 2;
138bf215546Sopenharmony_ci
139bf215546Sopenharmony_ci   if (dst_multi)
140bf215546Sopenharmony_ci      cs->dest[1].offset = rs->dest_offset + rs->dest_stride * rs->dest_padded_height / 2;
141bf215546Sopenharmony_ci
142bf215546Sopenharmony_ci   cs->RS_WINDOW_SIZE = VIVS_RS_WINDOW_SIZE_WIDTH(rs->width) |
143bf215546Sopenharmony_ci                        VIVS_RS_WINDOW_SIZE_HEIGHT(rs->height);
144bf215546Sopenharmony_ci
145bf215546Sopenharmony_ci   /* use dual pipe mode when required */
146bf215546Sopenharmony_ci   if (!screen->specs.single_buffer && screen->specs.pixel_pipes == 2 && !(rs->height & 7)) {
147bf215546Sopenharmony_ci      cs->RS_WINDOW_SIZE = VIVS_RS_WINDOW_SIZE_WIDTH(rs->width) |
148bf215546Sopenharmony_ci                              VIVS_RS_WINDOW_SIZE_HEIGHT(rs->height / 2);
149bf215546Sopenharmony_ci      cs->RS_PIPE_OFFSET[1] = VIVS_RS_PIPE_OFFSET_X(0) | VIVS_RS_PIPE_OFFSET_Y(rs->height / 2);
150bf215546Sopenharmony_ci   }
151bf215546Sopenharmony_ci
152bf215546Sopenharmony_ci   cs->RS_DITHER[0] = rs->dither[0];
153bf215546Sopenharmony_ci   cs->RS_DITHER[1] = rs->dither[1];
154bf215546Sopenharmony_ci   cs->RS_CLEAR_CONTROL = VIVS_RS_CLEAR_CONTROL_BITS(rs->clear_bits) | rs->clear_mode;
155bf215546Sopenharmony_ci   cs->RS_FILL_VALUE[0] = rs->clear_value[0];
156bf215546Sopenharmony_ci   cs->RS_FILL_VALUE[1] = rs->clear_value[1];
157bf215546Sopenharmony_ci   cs->RS_FILL_VALUE[2] = rs->clear_value[2];
158bf215546Sopenharmony_ci   cs->RS_FILL_VALUE[3] = rs->clear_value[3];
159bf215546Sopenharmony_ci   cs->RS_EXTRA_CONFIG = VIVS_RS_EXTRA_CONFIG_AA(rs->aa) |
160bf215546Sopenharmony_ci                         VIVS_RS_EXTRA_CONFIG_ENDIAN(rs->endian_mode);
161bf215546Sopenharmony_ci
162bf215546Sopenharmony_ci   /* If source the same as destination, and the hardware supports this,
163bf215546Sopenharmony_ci    * do an in-place resolve to fill in unrendered tiles.
164bf215546Sopenharmony_ci    */
165bf215546Sopenharmony_ci   if (screen->specs.single_buffer && rs->source == rs->dest &&
166bf215546Sopenharmony_ci         rs->source_offset == rs->dest_offset &&
167bf215546Sopenharmony_ci         rs->source_format == rs->dest_format &&
168bf215546Sopenharmony_ci         rs->source_tiling == rs->dest_tiling &&
169bf215546Sopenharmony_ci         src_super &&
170bf215546Sopenharmony_ci         rs->source_stride == rs->dest_stride &&
171bf215546Sopenharmony_ci         !rs->downsample_x && !rs->downsample_y &&
172bf215546Sopenharmony_ci         !rs->swap_rb && !rs->flip &&
173bf215546Sopenharmony_ci         !rs->clear_mode && rs->source_padded_width &&
174bf215546Sopenharmony_ci         !rs->source_ts_compressed) {
175bf215546Sopenharmony_ci      /* Total number of tiles (same as for autodisable) */
176bf215546Sopenharmony_ci      cs->RS_KICKER_INPLACE = rs->tile_count;
177bf215546Sopenharmony_ci   }
178bf215546Sopenharmony_ci   cs->source_ts_valid = rs->source_ts_valid;
179bf215546Sopenharmony_ci}
180bf215546Sopenharmony_ci
181bf215546Sopenharmony_ci/* modify the clear bits value in the compiled RS state */
182bf215546Sopenharmony_cistatic void
183bf215546Sopenharmony_cietna_modify_rs_clearbits(struct compiled_rs_state *cs, uint32_t clear_bits)
184bf215546Sopenharmony_ci{
185bf215546Sopenharmony_ci   cs->RS_CLEAR_CONTROL &= ~VIVS_RS_CLEAR_CONTROL_BITS__MASK;
186bf215546Sopenharmony_ci   cs->RS_CLEAR_CONTROL |= VIVS_RS_CLEAR_CONTROL_BITS(clear_bits);
187bf215546Sopenharmony_ci}
188bf215546Sopenharmony_ci
189bf215546Sopenharmony_ci#define EMIT_STATE(state_name, src_value) \
190bf215546Sopenharmony_ci   etna_coalsence_emit(stream, &coalesce, VIVS_##state_name, src_value)
191bf215546Sopenharmony_ci
192bf215546Sopenharmony_ci#define EMIT_STATE_FIXP(state_name, src_value) \
193bf215546Sopenharmony_ci   etna_coalsence_emit_fixp(stream, &coalesce, VIVS_##state_name, src_value)
194bf215546Sopenharmony_ci
195bf215546Sopenharmony_ci#define EMIT_STATE_RELOC(state_name, src_value) \
196bf215546Sopenharmony_ci   etna_coalsence_emit_reloc(stream, &coalesce, VIVS_##state_name, src_value)
197bf215546Sopenharmony_ci
198bf215546Sopenharmony_ci/* submit RS state, without any processing and no dependence on context
199bf215546Sopenharmony_ci * except TS if this is a source-to-destination blit. */
200bf215546Sopenharmony_cistatic void
201bf215546Sopenharmony_cietna_submit_rs_state(struct etna_context *ctx,
202bf215546Sopenharmony_ci                     const struct compiled_rs_state *cs)
203bf215546Sopenharmony_ci{
204bf215546Sopenharmony_ci   struct etna_screen *screen = etna_screen(ctx->base.screen);
205bf215546Sopenharmony_ci   struct etna_cmd_stream *stream = ctx->stream;
206bf215546Sopenharmony_ci   struct etna_coalesce coalesce;
207bf215546Sopenharmony_ci
208bf215546Sopenharmony_ci   if (cs->RS_KICKER_INPLACE && !cs->source_ts_valid)
209bf215546Sopenharmony_ci      /* Inplace resolve is no-op if TS is not configured */
210bf215546Sopenharmony_ci      return;
211bf215546Sopenharmony_ci
212bf215546Sopenharmony_ci   ctx->stats.rs_operations++;
213bf215546Sopenharmony_ci
214bf215546Sopenharmony_ci   if (cs->RS_KICKER_INPLACE) {
215bf215546Sopenharmony_ci      etna_cmd_stream_reserve(stream, 6);
216bf215546Sopenharmony_ci      etna_coalesce_start(stream, &coalesce);
217bf215546Sopenharmony_ci      /* 0/1 */ EMIT_STATE(RS_EXTRA_CONFIG, cs->RS_EXTRA_CONFIG);
218bf215546Sopenharmony_ci      /* 2/3 */ EMIT_STATE(RS_SOURCE_STRIDE, cs->RS_SOURCE_STRIDE);
219bf215546Sopenharmony_ci      /* 4/5 */ EMIT_STATE(RS_KICKER_INPLACE, cs->RS_KICKER_INPLACE);
220bf215546Sopenharmony_ci      etna_coalesce_end(stream, &coalesce);
221bf215546Sopenharmony_ci   } else if (screen->specs.pixel_pipes > 1 ||
222bf215546Sopenharmony_ci              VIV_FEATURE(screen, chipMinorFeatures7, RS_NEW_BASEADDR)) {
223bf215546Sopenharmony_ci      etna_cmd_stream_reserve(stream, 34); /* worst case - both pipes multi=1 */
224bf215546Sopenharmony_ci      etna_coalesce_start(stream, &coalesce);
225bf215546Sopenharmony_ci      /* 0/1 */ EMIT_STATE(RS_CONFIG, cs->RS_CONFIG);
226bf215546Sopenharmony_ci      /* 2/3 */ EMIT_STATE(RS_SOURCE_STRIDE, cs->RS_SOURCE_STRIDE);
227bf215546Sopenharmony_ci      /* 4/5 */ EMIT_STATE(RS_DEST_STRIDE, cs->RS_DEST_STRIDE);
228bf215546Sopenharmony_ci      /* 6/7 */ EMIT_STATE_RELOC(RS_PIPE_SOURCE_ADDR(0), &cs->source[0]);
229bf215546Sopenharmony_ci      if (cs->RS_SOURCE_STRIDE & VIVS_RS_SOURCE_STRIDE_MULTI) {
230bf215546Sopenharmony_ci         /*8 */ EMIT_STATE_RELOC(RS_PIPE_SOURCE_ADDR(1), &cs->source[1]);
231bf215546Sopenharmony_ci         /*9 - pad */
232bf215546Sopenharmony_ci      }
233bf215546Sopenharmony_ci      /*10/11*/ EMIT_STATE_RELOC(RS_PIPE_DEST_ADDR(0), &cs->dest[0]);
234bf215546Sopenharmony_ci      if (cs->RS_DEST_STRIDE & VIVS_RS_DEST_STRIDE_MULTI) {
235bf215546Sopenharmony_ci         /*12*/ EMIT_STATE_RELOC(RS_PIPE_DEST_ADDR(1), &cs->dest[1]);
236bf215546Sopenharmony_ci         /*13 - pad */
237bf215546Sopenharmony_ci      }
238bf215546Sopenharmony_ci      /*14/15*/ EMIT_STATE(RS_PIPE_OFFSET(0), cs->RS_PIPE_OFFSET[0]);
239bf215546Sopenharmony_ci      /*16   */ EMIT_STATE(RS_PIPE_OFFSET(1), cs->RS_PIPE_OFFSET[1]);
240bf215546Sopenharmony_ci      /*17 - pad */
241bf215546Sopenharmony_ci      /*18/19*/ EMIT_STATE(RS_WINDOW_SIZE, cs->RS_WINDOW_SIZE);
242bf215546Sopenharmony_ci      /*20/21*/ EMIT_STATE(RS_DITHER(0), cs->RS_DITHER[0]);
243bf215546Sopenharmony_ci      /*22   */ EMIT_STATE(RS_DITHER(1), cs->RS_DITHER[1]);
244bf215546Sopenharmony_ci      /*23 - pad */
245bf215546Sopenharmony_ci      /*24/25*/ EMIT_STATE(RS_CLEAR_CONTROL, cs->RS_CLEAR_CONTROL);
246bf215546Sopenharmony_ci      /*26   */ EMIT_STATE(RS_FILL_VALUE(0), cs->RS_FILL_VALUE[0]);
247bf215546Sopenharmony_ci      /*27   */ EMIT_STATE(RS_FILL_VALUE(1), cs->RS_FILL_VALUE[1]);
248bf215546Sopenharmony_ci      /*28   */ EMIT_STATE(RS_FILL_VALUE(2), cs->RS_FILL_VALUE[2]);
249bf215546Sopenharmony_ci      /*29   */ EMIT_STATE(RS_FILL_VALUE(3), cs->RS_FILL_VALUE[3]);
250bf215546Sopenharmony_ci      /*30/31*/ EMIT_STATE(RS_EXTRA_CONFIG, cs->RS_EXTRA_CONFIG);
251bf215546Sopenharmony_ci      /*32/33*/ EMIT_STATE(RS_KICKER, 0xbeebbeeb);
252bf215546Sopenharmony_ci      etna_coalesce_end(stream, &coalesce);
253bf215546Sopenharmony_ci   } else {
254bf215546Sopenharmony_ci      etna_cmd_stream_reserve(stream, 22);
255bf215546Sopenharmony_ci      etna_coalesce_start(stream, &coalesce);
256bf215546Sopenharmony_ci      /* 0/1 */ EMIT_STATE(RS_CONFIG, cs->RS_CONFIG);
257bf215546Sopenharmony_ci      /* 2   */ EMIT_STATE_RELOC(RS_SOURCE_ADDR, &cs->source[0]);
258bf215546Sopenharmony_ci      /* 3   */ EMIT_STATE(RS_SOURCE_STRIDE, cs->RS_SOURCE_STRIDE);
259bf215546Sopenharmony_ci      /* 4   */ EMIT_STATE_RELOC(RS_DEST_ADDR, &cs->dest[0]);
260bf215546Sopenharmony_ci      /* 5   */ EMIT_STATE(RS_DEST_STRIDE, cs->RS_DEST_STRIDE);
261bf215546Sopenharmony_ci      /* 6/7 */ EMIT_STATE(RS_WINDOW_SIZE, cs->RS_WINDOW_SIZE);
262bf215546Sopenharmony_ci      /* 8/9 */ EMIT_STATE(RS_DITHER(0), cs->RS_DITHER[0]);
263bf215546Sopenharmony_ci      /*10   */ EMIT_STATE(RS_DITHER(1), cs->RS_DITHER[1]);
264bf215546Sopenharmony_ci      /*11 - pad */
265bf215546Sopenharmony_ci      /*12/13*/ EMIT_STATE(RS_CLEAR_CONTROL, cs->RS_CLEAR_CONTROL);
266bf215546Sopenharmony_ci      /*14   */ EMIT_STATE(RS_FILL_VALUE(0), cs->RS_FILL_VALUE[0]);
267bf215546Sopenharmony_ci      /*15   */ EMIT_STATE(RS_FILL_VALUE(1), cs->RS_FILL_VALUE[1]);
268bf215546Sopenharmony_ci      /*16   */ EMIT_STATE(RS_FILL_VALUE(2), cs->RS_FILL_VALUE[2]);
269bf215546Sopenharmony_ci      /*17   */ EMIT_STATE(RS_FILL_VALUE(3), cs->RS_FILL_VALUE[3]);
270bf215546Sopenharmony_ci      /*18/19*/ EMIT_STATE(RS_EXTRA_CONFIG, cs->RS_EXTRA_CONFIG);
271bf215546Sopenharmony_ci      /*20/21*/ EMIT_STATE(RS_KICKER, 0xbeebbeeb);
272bf215546Sopenharmony_ci      etna_coalesce_end(stream, &coalesce);
273bf215546Sopenharmony_ci   }
274bf215546Sopenharmony_ci}
275bf215546Sopenharmony_ci
276bf215546Sopenharmony_ci/* Generate clear command for a surface (non-fast clear case) */
277bf215546Sopenharmony_civoid
278bf215546Sopenharmony_cietna_rs_gen_clear_surface(struct etna_context *ctx, struct etna_surface *surf,
279bf215546Sopenharmony_ci                          uint64_t clear_value)
280bf215546Sopenharmony_ci{
281bf215546Sopenharmony_ci   ASSERTED struct etna_screen *screen = ctx->screen;
282bf215546Sopenharmony_ci   struct etna_resource *dst = etna_resource(surf->base.texture);
283bf215546Sopenharmony_ci   uint32_t format;
284bf215546Sopenharmony_ci
285bf215546Sopenharmony_ci   switch (util_format_get_blocksizebits(surf->base.format)) {
286bf215546Sopenharmony_ci   case 16:
287bf215546Sopenharmony_ci      format = RS_FORMAT_A4R4G4B4;
288bf215546Sopenharmony_ci      break;
289bf215546Sopenharmony_ci   case 32:
290bf215546Sopenharmony_ci      format = RS_FORMAT_A8R8G8B8;
291bf215546Sopenharmony_ci      break;
292bf215546Sopenharmony_ci   case 64:
293bf215546Sopenharmony_ci      assert(screen->specs.halti >= 2);
294bf215546Sopenharmony_ci      format = RS_FORMAT_64BPP_CLEAR;
295bf215546Sopenharmony_ci      break;
296bf215546Sopenharmony_ci   default:
297bf215546Sopenharmony_ci      unreachable("bpp not supported for clear by RS");
298bf215546Sopenharmony_ci      break;
299bf215546Sopenharmony_ci   }
300bf215546Sopenharmony_ci
301bf215546Sopenharmony_ci   /* use tiled clear if width is multiple of 16 */
302bf215546Sopenharmony_ci   bool tiled_clear = (surf->surf.padded_width & ETNA_RS_WIDTH_MASK) == 0 &&
303bf215546Sopenharmony_ci                      (surf->surf.padded_height & ETNA_RS_HEIGHT_MASK) == 0;
304bf215546Sopenharmony_ci
305bf215546Sopenharmony_ci   etna_compile_rs_state( ctx, &surf->clear_command, &(struct rs_state) {
306bf215546Sopenharmony_ci      .source_format = format,
307bf215546Sopenharmony_ci      .dest_format = format,
308bf215546Sopenharmony_ci      .dest = dst->bo,
309bf215546Sopenharmony_ci      .dest_offset = surf->surf.offset,
310bf215546Sopenharmony_ci      .dest_stride = surf->surf.stride,
311bf215546Sopenharmony_ci      .dest_padded_height = surf->surf.padded_height,
312bf215546Sopenharmony_ci      .dest_tiling = tiled_clear ? dst->layout : ETNA_LAYOUT_LINEAR,
313bf215546Sopenharmony_ci      .dither = {0xffffffff, 0xffffffff},
314bf215546Sopenharmony_ci      .width = surf->surf.padded_width, /* These must be padded to 16x4 if !LINEAR, otherwise RS will hang */
315bf215546Sopenharmony_ci      .height = surf->surf.padded_height,
316bf215546Sopenharmony_ci      .clear_value = {clear_value, clear_value >> 32, clear_value, clear_value >> 32},
317bf215546Sopenharmony_ci      .clear_mode = VIVS_RS_CLEAR_CONTROL_MODE_ENABLED1,
318bf215546Sopenharmony_ci      .clear_bits = 0xffff
319bf215546Sopenharmony_ci   });
320bf215546Sopenharmony_ci}
321bf215546Sopenharmony_ci
322bf215546Sopenharmony_cistatic void
323bf215546Sopenharmony_cietna_blit_clear_color_rs(struct pipe_context *pctx, struct pipe_surface *dst,
324bf215546Sopenharmony_ci                      const union pipe_color_union *color)
325bf215546Sopenharmony_ci{
326bf215546Sopenharmony_ci   struct etna_context *ctx = etna_context(pctx);
327bf215546Sopenharmony_ci   struct etna_surface *surf = etna_surface(dst);
328bf215546Sopenharmony_ci   uint64_t new_clear_value = etna_clear_blit_pack_rgba(surf->base.format, color);
329bf215546Sopenharmony_ci
330bf215546Sopenharmony_ci   if (surf->surf.ts_size) { /* TS: use precompiled clear command */
331bf215546Sopenharmony_ci      ctx->framebuffer.TS_COLOR_CLEAR_VALUE = new_clear_value;
332bf215546Sopenharmony_ci      ctx->framebuffer.TS_COLOR_CLEAR_VALUE_EXT = new_clear_value >> 32;
333bf215546Sopenharmony_ci
334bf215546Sopenharmony_ci      if (VIV_FEATURE(ctx->screen, chipMinorFeatures1, AUTO_DISABLE)) {
335bf215546Sopenharmony_ci         /* Set number of color tiles to be filled */
336bf215546Sopenharmony_ci         etna_set_state(ctx->stream, VIVS_TS_COLOR_AUTO_DISABLE_COUNT,
337bf215546Sopenharmony_ci                        surf->surf.padded_width * surf->surf.padded_height / 16);
338bf215546Sopenharmony_ci         ctx->framebuffer.TS_MEM_CONFIG |= VIVS_TS_MEM_CONFIG_COLOR_AUTO_DISABLE;
339bf215546Sopenharmony_ci      }
340bf215546Sopenharmony_ci
341bf215546Sopenharmony_ci      surf->level->ts_valid = true;
342bf215546Sopenharmony_ci      ctx->dirty |= ETNA_DIRTY_TS | ETNA_DIRTY_DERIVE_TS;
343bf215546Sopenharmony_ci   } else if (unlikely(new_clear_value != surf->level->clear_value)) { /* Queue normal RS clear for non-TS surfaces */
344bf215546Sopenharmony_ci      /* If clear color changed, re-generate stored command */
345bf215546Sopenharmony_ci      etna_rs_gen_clear_surface(ctx, surf, new_clear_value);
346bf215546Sopenharmony_ci   }
347bf215546Sopenharmony_ci
348bf215546Sopenharmony_ci   etna_submit_rs_state(ctx, &surf->clear_command);
349bf215546Sopenharmony_ci
350bf215546Sopenharmony_ci   surf->level->clear_value = new_clear_value;
351bf215546Sopenharmony_ci   resource_written(ctx, surf->base.texture);
352bf215546Sopenharmony_ci   etna_resource(surf->base.texture)->seqno++;
353bf215546Sopenharmony_ci}
354bf215546Sopenharmony_ci
355bf215546Sopenharmony_cistatic void
356bf215546Sopenharmony_cietna_blit_clear_zs_rs(struct pipe_context *pctx, struct pipe_surface *dst,
357bf215546Sopenharmony_ci                   unsigned buffers, double depth, unsigned stencil)
358bf215546Sopenharmony_ci{
359bf215546Sopenharmony_ci   struct etna_context *ctx = etna_context(pctx);
360bf215546Sopenharmony_ci   struct etna_surface *surf = etna_surface(dst);
361bf215546Sopenharmony_ci   uint32_t new_clear_value = translate_clear_depth_stencil(surf->base.format, depth, stencil);
362bf215546Sopenharmony_ci   uint32_t new_clear_bits = 0, clear_bits_depth, clear_bits_stencil;
363bf215546Sopenharmony_ci
364bf215546Sopenharmony_ci   /* Get the channels to clear */
365bf215546Sopenharmony_ci   switch (surf->base.format) {
366bf215546Sopenharmony_ci   case PIPE_FORMAT_Z16_UNORM:
367bf215546Sopenharmony_ci      clear_bits_depth = 0xffff;
368bf215546Sopenharmony_ci      clear_bits_stencil = 0;
369bf215546Sopenharmony_ci      break;
370bf215546Sopenharmony_ci   case PIPE_FORMAT_X8Z24_UNORM:
371bf215546Sopenharmony_ci   case PIPE_FORMAT_S8_UINT_Z24_UNORM:
372bf215546Sopenharmony_ci      clear_bits_depth = 0xeeee;
373bf215546Sopenharmony_ci      clear_bits_stencil = 0x1111;
374bf215546Sopenharmony_ci      break;
375bf215546Sopenharmony_ci   default:
376bf215546Sopenharmony_ci      clear_bits_depth = clear_bits_stencil = 0xffff;
377bf215546Sopenharmony_ci      break;
378bf215546Sopenharmony_ci   }
379bf215546Sopenharmony_ci
380bf215546Sopenharmony_ci   if (buffers & PIPE_CLEAR_DEPTH)
381bf215546Sopenharmony_ci      new_clear_bits |= clear_bits_depth;
382bf215546Sopenharmony_ci   if (buffers & PIPE_CLEAR_STENCIL)
383bf215546Sopenharmony_ci      new_clear_bits |= clear_bits_stencil;
384bf215546Sopenharmony_ci   /* FIXME: when tile status is enabled, this becomes more complex as
385bf215546Sopenharmony_ci    * we may separately clear the depth from the stencil.  In this case,
386bf215546Sopenharmony_ci    * we want to resolve the surface, and avoid using the tile status.
387bf215546Sopenharmony_ci    * We may be better off recording the pending clear operation,
388bf215546Sopenharmony_ci    * delaying the actual clear to the first use.  This way, we can merge
389bf215546Sopenharmony_ci    * consecutive clears together. */
390bf215546Sopenharmony_ci   if (surf->surf.ts_size) { /* TS: use precompiled clear command */
391bf215546Sopenharmony_ci      /* Set new clear depth value */
392bf215546Sopenharmony_ci      ctx->framebuffer.TS_DEPTH_CLEAR_VALUE = new_clear_value;
393bf215546Sopenharmony_ci      if (VIV_FEATURE(ctx->screen, chipMinorFeatures1, AUTO_DISABLE)) {
394bf215546Sopenharmony_ci         /* Set number of depth tiles to be filled */
395bf215546Sopenharmony_ci         etna_set_state(ctx->stream, VIVS_TS_DEPTH_AUTO_DISABLE_COUNT,
396bf215546Sopenharmony_ci                        surf->surf.padded_width * surf->surf.padded_height / 16);
397bf215546Sopenharmony_ci         ctx->framebuffer.TS_MEM_CONFIG |= VIVS_TS_MEM_CONFIG_DEPTH_AUTO_DISABLE;
398bf215546Sopenharmony_ci      }
399bf215546Sopenharmony_ci
400bf215546Sopenharmony_ci      surf->level->ts_valid = true;
401bf215546Sopenharmony_ci      ctx->dirty |= ETNA_DIRTY_TS | ETNA_DIRTY_DERIVE_TS;
402bf215546Sopenharmony_ci   } else {
403bf215546Sopenharmony_ci      if (unlikely(new_clear_value != surf->level->clear_value)) { /* Queue normal RS clear for non-TS surfaces */
404bf215546Sopenharmony_ci         /* If clear depth value changed, re-generate stored command */
405bf215546Sopenharmony_ci         etna_rs_gen_clear_surface(ctx, surf, new_clear_value);
406bf215546Sopenharmony_ci      }
407bf215546Sopenharmony_ci      /* Update the channels to be cleared */
408bf215546Sopenharmony_ci      etna_modify_rs_clearbits(&surf->clear_command, new_clear_bits);
409bf215546Sopenharmony_ci   }
410bf215546Sopenharmony_ci
411bf215546Sopenharmony_ci   etna_submit_rs_state(ctx, &surf->clear_command);
412bf215546Sopenharmony_ci
413bf215546Sopenharmony_ci   surf->level->clear_value = new_clear_value;
414bf215546Sopenharmony_ci   resource_written(ctx, surf->base.texture);
415bf215546Sopenharmony_ci   etna_resource(surf->base.texture)->seqno++;
416bf215546Sopenharmony_ci}
417bf215546Sopenharmony_ci
418bf215546Sopenharmony_cistatic void
419bf215546Sopenharmony_cietna_clear_rs(struct pipe_context *pctx, unsigned buffers, const struct pipe_scissor_state *scissor_state,
420bf215546Sopenharmony_ci           const union pipe_color_union *color, double depth, unsigned stencil)
421bf215546Sopenharmony_ci{
422bf215546Sopenharmony_ci   struct etna_context *ctx = etna_context(pctx);
423bf215546Sopenharmony_ci
424bf215546Sopenharmony_ci   /* Flush color and depth cache before clearing anything.
425bf215546Sopenharmony_ci    * This is especially important when coming from another surface, as
426bf215546Sopenharmony_ci    * otherwise it may clear part of the old surface instead. */
427bf215546Sopenharmony_ci   etna_set_state(ctx->stream, VIVS_GL_FLUSH_CACHE, VIVS_GL_FLUSH_CACHE_COLOR | VIVS_GL_FLUSH_CACHE_DEPTH);
428bf215546Sopenharmony_ci   etna_stall(ctx->stream, SYNC_RECIPIENT_RA, SYNC_RECIPIENT_PE);
429bf215546Sopenharmony_ci
430bf215546Sopenharmony_ci   /* Preparation: Flush the TS if needed. This must be done after flushing
431bf215546Sopenharmony_ci    * color and depth, otherwise it can result in crashes */
432bf215546Sopenharmony_ci   bool need_ts_flush = false;
433bf215546Sopenharmony_ci   if ((buffers & PIPE_CLEAR_COLOR) && ctx->framebuffer_s.nr_cbufs) {
434bf215546Sopenharmony_ci      struct etna_surface *surf = etna_surface(ctx->framebuffer_s.cbufs[0]);
435bf215546Sopenharmony_ci      if (surf->surf.ts_size)
436bf215546Sopenharmony_ci         need_ts_flush = true;
437bf215546Sopenharmony_ci   }
438bf215546Sopenharmony_ci   if ((buffers & PIPE_CLEAR_DEPTHSTENCIL) && ctx->framebuffer_s.zsbuf != NULL) {
439bf215546Sopenharmony_ci      struct etna_surface *surf = etna_surface(ctx->framebuffer_s.zsbuf);
440bf215546Sopenharmony_ci
441bf215546Sopenharmony_ci      if (surf->surf.ts_size)
442bf215546Sopenharmony_ci         need_ts_flush = true;
443bf215546Sopenharmony_ci   }
444bf215546Sopenharmony_ci
445bf215546Sopenharmony_ci   if (need_ts_flush)
446bf215546Sopenharmony_ci      etna_set_state(ctx->stream, VIVS_TS_FLUSH_CACHE, VIVS_TS_FLUSH_CACHE_FLUSH);
447bf215546Sopenharmony_ci
448bf215546Sopenharmony_ci   /* No need to set up the TS here as RS clear operations (in contrast to
449bf215546Sopenharmony_ci    * resolve and copy) do not require the TS state.
450bf215546Sopenharmony_ci    */
451bf215546Sopenharmony_ci   if (buffers & PIPE_CLEAR_COLOR) {
452bf215546Sopenharmony_ci      for (int idx = 0; idx < ctx->framebuffer_s.nr_cbufs; ++idx) {
453bf215546Sopenharmony_ci         etna_blit_clear_color_rs(pctx, ctx->framebuffer_s.cbufs[idx],
454bf215546Sopenharmony_ci                               &color[idx]);
455bf215546Sopenharmony_ci      }
456bf215546Sopenharmony_ci   }
457bf215546Sopenharmony_ci
458bf215546Sopenharmony_ci   /* Flush the color and depth caches before each RS clear operation
459bf215546Sopenharmony_ci    * This fixes a hang on GC600. */
460bf215546Sopenharmony_ci   if (buffers & PIPE_CLEAR_DEPTHSTENCIL && buffers & PIPE_CLEAR_COLOR)
461bf215546Sopenharmony_ci      etna_set_state(ctx->stream, VIVS_GL_FLUSH_CACHE,
462bf215546Sopenharmony_ci                     VIVS_GL_FLUSH_CACHE_COLOR | VIVS_GL_FLUSH_CACHE_DEPTH);
463bf215546Sopenharmony_ci
464bf215546Sopenharmony_ci   if ((buffers & PIPE_CLEAR_DEPTHSTENCIL) && ctx->framebuffer_s.zsbuf != NULL)
465bf215546Sopenharmony_ci      etna_blit_clear_zs_rs(pctx, ctx->framebuffer_s.zsbuf, buffers, depth, stencil);
466bf215546Sopenharmony_ci
467bf215546Sopenharmony_ci   etna_stall(ctx->stream, SYNC_RECIPIENT_RA, SYNC_RECIPIENT_PE);
468bf215546Sopenharmony_ci}
469bf215546Sopenharmony_ci
470bf215546Sopenharmony_cistatic bool
471bf215546Sopenharmony_cietna_manual_blit(struct etna_resource *dst, struct etna_resource_level *dst_lev,
472bf215546Sopenharmony_ci                 unsigned int dst_offset, struct etna_resource *src,
473bf215546Sopenharmony_ci                 struct etna_resource_level *src_lev, unsigned int src_offset,
474bf215546Sopenharmony_ci                 const struct pipe_blit_info *blit_info)
475bf215546Sopenharmony_ci{
476bf215546Sopenharmony_ci   void *smap, *srow, *dmap, *drow;
477bf215546Sopenharmony_ci   size_t tile_size;
478bf215546Sopenharmony_ci
479bf215546Sopenharmony_ci   assert(src->layout == ETNA_LAYOUT_TILED);
480bf215546Sopenharmony_ci   assert(dst->layout == ETNA_LAYOUT_TILED);
481bf215546Sopenharmony_ci   assert(src->base.nr_samples == 0);
482bf215546Sopenharmony_ci   assert(dst->base.nr_samples == 0);
483bf215546Sopenharmony_ci
484bf215546Sopenharmony_ci   tile_size = util_format_get_blocksize(blit_info->src.format) * 4 * 4;
485bf215546Sopenharmony_ci
486bf215546Sopenharmony_ci   smap = etna_bo_map(src->bo);
487bf215546Sopenharmony_ci   if (!smap)
488bf215546Sopenharmony_ci      return false;
489bf215546Sopenharmony_ci
490bf215546Sopenharmony_ci   dmap = etna_bo_map(dst->bo);
491bf215546Sopenharmony_ci   if (!dmap)
492bf215546Sopenharmony_ci      return false;
493bf215546Sopenharmony_ci
494bf215546Sopenharmony_ci   srow = smap + src_offset;
495bf215546Sopenharmony_ci   drow = dmap + dst_offset;
496bf215546Sopenharmony_ci
497bf215546Sopenharmony_ci   etna_bo_cpu_prep(src->bo, DRM_ETNA_PREP_READ);
498bf215546Sopenharmony_ci   etna_bo_cpu_prep(dst->bo, DRM_ETNA_PREP_WRITE);
499bf215546Sopenharmony_ci
500bf215546Sopenharmony_ci   for (int y = 0; y < blit_info->src.box.height; y += 4) {
501bf215546Sopenharmony_ci      memcpy(drow, srow, tile_size * blit_info->src.box.width);
502bf215546Sopenharmony_ci      srow += src_lev->stride * 4;
503bf215546Sopenharmony_ci      drow += dst_lev->stride * 4;
504bf215546Sopenharmony_ci   }
505bf215546Sopenharmony_ci
506bf215546Sopenharmony_ci   etna_bo_cpu_fini(dst->bo);
507bf215546Sopenharmony_ci   etna_bo_cpu_fini(src->bo);
508bf215546Sopenharmony_ci
509bf215546Sopenharmony_ci   return true;
510bf215546Sopenharmony_ci}
511bf215546Sopenharmony_ci
512bf215546Sopenharmony_cistatic inline size_t
513bf215546Sopenharmony_cietna_compute_tileoffset(const struct pipe_box *box, enum pipe_format format,
514bf215546Sopenharmony_ci                        size_t stride, enum etna_surface_layout layout)
515bf215546Sopenharmony_ci{
516bf215546Sopenharmony_ci   size_t offset;
517bf215546Sopenharmony_ci   unsigned int x = box->x, y = box->y;
518bf215546Sopenharmony_ci   unsigned int blocksize = util_format_get_blocksize(format);
519bf215546Sopenharmony_ci
520bf215546Sopenharmony_ci   switch (layout) {
521bf215546Sopenharmony_ci   case ETNA_LAYOUT_LINEAR:
522bf215546Sopenharmony_ci      offset = y * stride + x * blocksize;
523bf215546Sopenharmony_ci      break;
524bf215546Sopenharmony_ci   case ETNA_LAYOUT_MULTI_TILED:
525bf215546Sopenharmony_ci      y >>= 1;
526bf215546Sopenharmony_ci      FALLTHROUGH;
527bf215546Sopenharmony_ci   case ETNA_LAYOUT_TILED:
528bf215546Sopenharmony_ci      assert(!(x & 0x03) && !(y & 0x03));
529bf215546Sopenharmony_ci      offset = (y & ~0x03) * stride + blocksize * ((x & ~0x03) << 2);
530bf215546Sopenharmony_ci      break;
531bf215546Sopenharmony_ci   case ETNA_LAYOUT_MULTI_SUPERTILED:
532bf215546Sopenharmony_ci      y >>= 1;
533bf215546Sopenharmony_ci      FALLTHROUGH;
534bf215546Sopenharmony_ci   case ETNA_LAYOUT_SUPER_TILED:
535bf215546Sopenharmony_ci      assert(!(x & 0x3f) && !(y & 0x3f));
536bf215546Sopenharmony_ci      offset = (y & ~0x3f) * stride + blocksize * ((x & ~0x3f) << 6);
537bf215546Sopenharmony_ci      break;
538bf215546Sopenharmony_ci   default:
539bf215546Sopenharmony_ci      unreachable("invalid resource layout");
540bf215546Sopenharmony_ci   }
541bf215546Sopenharmony_ci
542bf215546Sopenharmony_ci   return offset;
543bf215546Sopenharmony_ci}
544bf215546Sopenharmony_ci
545bf215546Sopenharmony_cistatic inline void
546bf215546Sopenharmony_cietna_get_rs_alignment_mask(const struct etna_context *ctx,
547bf215546Sopenharmony_ci                           const enum etna_surface_layout layout,
548bf215546Sopenharmony_ci                           unsigned int *width_mask, unsigned int *height_mask)
549bf215546Sopenharmony_ci{
550bf215546Sopenharmony_ci   struct etna_screen *screen = ctx->screen;
551bf215546Sopenharmony_ci   unsigned int h_align, w_align;
552bf215546Sopenharmony_ci
553bf215546Sopenharmony_ci   if (layout & ETNA_LAYOUT_BIT_SUPER) {
554bf215546Sopenharmony_ci      w_align = 64;
555bf215546Sopenharmony_ci      h_align = 64 * screen->specs.pixel_pipes;
556bf215546Sopenharmony_ci   } else {
557bf215546Sopenharmony_ci      w_align = ETNA_RS_WIDTH_MASK + 1;
558bf215546Sopenharmony_ci      h_align = ETNA_RS_HEIGHT_MASK + 1;
559bf215546Sopenharmony_ci   }
560bf215546Sopenharmony_ci
561bf215546Sopenharmony_ci   *width_mask = w_align - 1;
562bf215546Sopenharmony_ci   *height_mask = h_align -1;
563bf215546Sopenharmony_ci}
564bf215546Sopenharmony_ci
565bf215546Sopenharmony_cistatic bool msaa_config(const struct pipe_resource *src,
566bf215546Sopenharmony_ci                        const struct pipe_resource *dst,
567bf215546Sopenharmony_ci                        int *msaa_xscale,
568bf215546Sopenharmony_ci                        int *msaa_yscale)
569bf215546Sopenharmony_ci{
570bf215546Sopenharmony_ci   int src_xscale = 1, src_yscale = 1;
571bf215546Sopenharmony_ci   int dst_xscale = 1, dst_yscale = 1;
572bf215546Sopenharmony_ci
573bf215546Sopenharmony_ci   assert(src->nr_samples <= 4);
574bf215546Sopenharmony_ci   assert(dst->nr_samples <= 4);
575bf215546Sopenharmony_ci
576bf215546Sopenharmony_ci   translate_samples_to_xyscale(src->nr_samples, &src_xscale, &src_yscale);
577bf215546Sopenharmony_ci   translate_samples_to_xyscale(dst->nr_samples, &dst_xscale, &dst_yscale);
578bf215546Sopenharmony_ci
579bf215546Sopenharmony_ci   /* RS does not support upscaling */
580bf215546Sopenharmony_ci   if ((src_xscale < dst_xscale) || (src_yscale < dst_yscale))
581bf215546Sopenharmony_ci      return false;
582bf215546Sopenharmony_ci
583bf215546Sopenharmony_ci   *msaa_xscale = src_xscale - dst_xscale + 1;
584bf215546Sopenharmony_ci   *msaa_yscale = src_yscale - dst_yscale + 1;
585bf215546Sopenharmony_ci
586bf215546Sopenharmony_ci   return true;
587bf215546Sopenharmony_ci}
588bf215546Sopenharmony_ci
589bf215546Sopenharmony_cistatic bool
590bf215546Sopenharmony_cietna_try_rs_blit(struct pipe_context *pctx,
591bf215546Sopenharmony_ci                 const struct pipe_blit_info *blit_info)
592bf215546Sopenharmony_ci{
593bf215546Sopenharmony_ci   struct etna_context *ctx = etna_context(pctx);
594bf215546Sopenharmony_ci   struct etna_resource *src = etna_resource(blit_info->src.resource);
595bf215546Sopenharmony_ci   struct etna_resource *dst = etna_resource(blit_info->dst.resource);
596bf215546Sopenharmony_ci   struct compiled_rs_state copy_to_screen;
597bf215546Sopenharmony_ci   int msaa_xscale = 1, msaa_yscale = 1;
598bf215546Sopenharmony_ci
599bf215546Sopenharmony_ci   /* Ensure that the level is valid */
600bf215546Sopenharmony_ci   assert(blit_info->src.level <= src->base.last_level);
601bf215546Sopenharmony_ci   assert(blit_info->dst.level <= dst->base.last_level);
602bf215546Sopenharmony_ci
603bf215546Sopenharmony_ci   if (!msaa_config(&src->base, &dst->base, &msaa_xscale, &msaa_yscale)) {
604bf215546Sopenharmony_ci      DBG("upsampling not supported");
605bf215546Sopenharmony_ci      return false;
606bf215546Sopenharmony_ci   }
607bf215546Sopenharmony_ci
608bf215546Sopenharmony_ci   /* The width/height are in pixels; they do not change as a result of
609bf215546Sopenharmony_ci    * multi-sampling. So, when blitting from a 4x multisampled surface
610bf215546Sopenharmony_ci    * to a non-multisampled surface, the width and height will be
611bf215546Sopenharmony_ci    * identical. As we do not support scaling, reject different sizes. */
612bf215546Sopenharmony_ci   if (blit_info->dst.box.width != blit_info->src.box.width ||
613bf215546Sopenharmony_ci       blit_info->dst.box.height != blit_info->src.box.height) {
614bf215546Sopenharmony_ci      DBG("scaling requested: source %dx%d destination %dx%d",
615bf215546Sopenharmony_ci          blit_info->src.box.width, blit_info->src.box.height,
616bf215546Sopenharmony_ci          blit_info->dst.box.width, blit_info->dst.box.height);
617bf215546Sopenharmony_ci      return false;
618bf215546Sopenharmony_ci   }
619bf215546Sopenharmony_ci
620bf215546Sopenharmony_ci   /* No masks - RS can't copy specific channels */
621bf215546Sopenharmony_ci   unsigned mask = util_format_get_mask(blit_info->dst.format);
622bf215546Sopenharmony_ci   if ((blit_info->mask & mask) != mask) {
623bf215546Sopenharmony_ci      DBG("sub-mask requested: 0x%02x vs format mask 0x%02x", blit_info->mask, mask);
624bf215546Sopenharmony_ci      return false;
625bf215546Sopenharmony_ci   }
626bf215546Sopenharmony_ci
627bf215546Sopenharmony_ci   /* Only support same format (used tiling/detiling) blits for now.
628bf215546Sopenharmony_ci    * TODO: figure out which different-format blits are possible and test them
629bf215546Sopenharmony_ci    *  - fail if swizzle needed
630bf215546Sopenharmony_ci    *  - avoid trying to convert between float/int formats?
631bf215546Sopenharmony_ci    */
632bf215546Sopenharmony_ci   if (blit_info->src.format != blit_info->dst.format)
633bf215546Sopenharmony_ci      return false;
634bf215546Sopenharmony_ci
635bf215546Sopenharmony_ci   uint32_t format = etna_compatible_rs_format(blit_info->dst.format);
636bf215546Sopenharmony_ci   if (format == ETNA_NO_MATCH)
637bf215546Sopenharmony_ci      return false;
638bf215546Sopenharmony_ci
639bf215546Sopenharmony_ci   if (blit_info->scissor_enable ||
640bf215546Sopenharmony_ci       blit_info->dst.box.depth != blit_info->src.box.depth ||
641bf215546Sopenharmony_ci       blit_info->dst.box.depth != 1) {
642bf215546Sopenharmony_ci      return false;
643bf215546Sopenharmony_ci   }
644bf215546Sopenharmony_ci
645bf215546Sopenharmony_ci   unsigned w_mask, h_mask;
646bf215546Sopenharmony_ci
647bf215546Sopenharmony_ci   etna_get_rs_alignment_mask(ctx, src->layout, &w_mask, &h_mask);
648bf215546Sopenharmony_ci   if ((blit_info->src.box.x & w_mask) || (blit_info->src.box.y & h_mask))
649bf215546Sopenharmony_ci      return false;
650bf215546Sopenharmony_ci
651bf215546Sopenharmony_ci   etna_get_rs_alignment_mask(ctx, dst->layout, &w_mask, &h_mask);
652bf215546Sopenharmony_ci   if ((blit_info->dst.box.x & w_mask) || (blit_info->dst.box.y & h_mask))
653bf215546Sopenharmony_ci      return false;
654bf215546Sopenharmony_ci
655bf215546Sopenharmony_ci   struct etna_resource_level *src_lev = &src->levels[blit_info->src.level];
656bf215546Sopenharmony_ci   struct etna_resource_level *dst_lev = &dst->levels[blit_info->dst.level];
657bf215546Sopenharmony_ci
658bf215546Sopenharmony_ci   /* we may be given coordinates up to the padded width to avoid
659bf215546Sopenharmony_ci    * any alignment issues with different tiling formats */
660bf215546Sopenharmony_ci   assert((blit_info->src.box.x + blit_info->src.box.width) * msaa_xscale <= src_lev->padded_width);
661bf215546Sopenharmony_ci   assert((blit_info->src.box.y + blit_info->src.box.height) * msaa_yscale <= src_lev->padded_height);
662bf215546Sopenharmony_ci   assert(blit_info->dst.box.x + blit_info->dst.box.width <= dst_lev->padded_width);
663bf215546Sopenharmony_ci   assert(blit_info->dst.box.y + blit_info->dst.box.height <= dst_lev->padded_height);
664bf215546Sopenharmony_ci
665bf215546Sopenharmony_ci   unsigned src_offset = src_lev->offset +
666bf215546Sopenharmony_ci                         blit_info->src.box.z * src_lev->layer_stride +
667bf215546Sopenharmony_ci                         etna_compute_tileoffset(&blit_info->src.box,
668bf215546Sopenharmony_ci                                                 blit_info->src.format,
669bf215546Sopenharmony_ci                                                 src_lev->stride,
670bf215546Sopenharmony_ci                                                 src->layout);
671bf215546Sopenharmony_ci   unsigned dst_offset = dst_lev->offset +
672bf215546Sopenharmony_ci                         blit_info->dst.box.z * dst_lev->layer_stride +
673bf215546Sopenharmony_ci                         etna_compute_tileoffset(&blit_info->dst.box,
674bf215546Sopenharmony_ci                                                 blit_info->dst.format,
675bf215546Sopenharmony_ci                                                 dst_lev->stride,
676bf215546Sopenharmony_ci                                                 dst->layout);
677bf215546Sopenharmony_ci
678bf215546Sopenharmony_ci   if (src_lev->padded_width <= ETNA_RS_WIDTH_MASK ||
679bf215546Sopenharmony_ci       dst_lev->padded_width <= ETNA_RS_WIDTH_MASK ||
680bf215546Sopenharmony_ci       src_lev->padded_height <= ETNA_RS_HEIGHT_MASK ||
681bf215546Sopenharmony_ci       dst_lev->padded_height <= ETNA_RS_HEIGHT_MASK)
682bf215546Sopenharmony_ci      goto manual;
683bf215546Sopenharmony_ci
684bf215546Sopenharmony_ci   /* If the width is not aligned to the RS width, but is within our
685bf215546Sopenharmony_ci    * padding, adjust the width to suite the RS width restriction.
686bf215546Sopenharmony_ci    * Note: the RS width/height are converted to source samples here. */
687bf215546Sopenharmony_ci   unsigned int width = blit_info->src.box.width * msaa_xscale;
688bf215546Sopenharmony_ci   unsigned int height = blit_info->src.box.height * msaa_yscale;
689bf215546Sopenharmony_ci   unsigned int w_align = ETNA_RS_WIDTH_MASK + 1;
690bf215546Sopenharmony_ci   unsigned int h_align = ETNA_RS_HEIGHT_MASK + 1;
691bf215546Sopenharmony_ci
692bf215546Sopenharmony_ci   if (width & (w_align - 1) && width >= src_lev->width * msaa_xscale && width >= dst_lev->width)
693bf215546Sopenharmony_ci      width = align(width, w_align);
694bf215546Sopenharmony_ci
695bf215546Sopenharmony_ci   if (height & (h_align - 1) && height >= src_lev->height * msaa_yscale && height >= dst_lev->height)
696bf215546Sopenharmony_ci      height = align(height, h_align);
697bf215546Sopenharmony_ci
698bf215546Sopenharmony_ci   /* The padded dimensions are in samples */
699bf215546Sopenharmony_ci   if (width > src_lev->padded_width ||
700bf215546Sopenharmony_ci       width > dst_lev->padded_width * msaa_xscale ||
701bf215546Sopenharmony_ci       height > src_lev->padded_height ||
702bf215546Sopenharmony_ci       height > dst_lev->padded_height * msaa_yscale ||
703bf215546Sopenharmony_ci       width & (w_align - 1) || height & (h_align - 1))
704bf215546Sopenharmony_ci      goto manual;
705bf215546Sopenharmony_ci
706bf215546Sopenharmony_ci   /* Always flush color and depth cache together before resolving. This works
707bf215546Sopenharmony_ci    * around artifacts that appear in some cases when scanning out a texture
708bf215546Sopenharmony_ci    * directly after it has been rendered to, such as rendering an animated web
709bf215546Sopenharmony_ci    * page in a QtWebEngine based WebView on GC2000. The artifacts look like
710bf215546Sopenharmony_ci    * the texture sampler samples zeroes instead of texture data in a small,
711bf215546Sopenharmony_ci    * irregular triangle in the lower right of each browser tile quad. Other
712bf215546Sopenharmony_ci    * attempts to avoid these artifacts, including a pipeline stall before the
713bf215546Sopenharmony_ci    * color flush or a TS cache flush afterwards, or flushing multiple times,
714bf215546Sopenharmony_ci    * with stalls before and after each flush, have shown no effect. */
715bf215546Sopenharmony_ci   if (src->base.bind & PIPE_BIND_RENDER_TARGET ||
716bf215546Sopenharmony_ci       src->base.bind & PIPE_BIND_DEPTH_STENCIL) {
717bf215546Sopenharmony_ci      etna_set_state(ctx->stream, VIVS_GL_FLUSH_CACHE,
718bf215546Sopenharmony_ci		     VIVS_GL_FLUSH_CACHE_COLOR | VIVS_GL_FLUSH_CACHE_DEPTH);
719bf215546Sopenharmony_ci      etna_stall(ctx->stream, SYNC_RECIPIENT_RA, SYNC_RECIPIENT_PE);
720bf215546Sopenharmony_ci
721bf215546Sopenharmony_ci      if (src_lev->ts_size && src_lev->ts_valid)
722bf215546Sopenharmony_ci         etna_set_state(ctx->stream, VIVS_TS_FLUSH_CACHE, VIVS_TS_FLUSH_CACHE_FLUSH);
723bf215546Sopenharmony_ci   }
724bf215546Sopenharmony_ci
725bf215546Sopenharmony_ci   /* Set up color TS to source surface before blit, if needed */
726bf215546Sopenharmony_ci   bool source_ts_valid = false;
727bf215546Sopenharmony_ci   if (src_lev->ts_size && src_lev->ts_valid) {
728bf215546Sopenharmony_ci      struct etna_reloc reloc;
729bf215546Sopenharmony_ci      unsigned ts_offset =
730bf215546Sopenharmony_ci         src_lev->ts_offset + blit_info->src.box.z * src_lev->ts_layer_stride;
731bf215546Sopenharmony_ci      uint32_t ts_mem_config = 0;
732bf215546Sopenharmony_ci
733bf215546Sopenharmony_ci      if (src_lev->ts_compress_fmt >= 0) {
734bf215546Sopenharmony_ci         ts_mem_config |= VIVS_TS_MEM_CONFIG_COLOR_COMPRESSION |
735bf215546Sopenharmony_ci                          VIVS_TS_MEM_CONFIG_COLOR_COMPRESSION_FORMAT(src_lev->ts_compress_fmt);
736bf215546Sopenharmony_ci      }
737bf215546Sopenharmony_ci
738bf215546Sopenharmony_ci      etna_set_state(ctx->stream, VIVS_TS_MEM_CONFIG,
739bf215546Sopenharmony_ci                     VIVS_TS_MEM_CONFIG_COLOR_FAST_CLEAR | ts_mem_config);
740bf215546Sopenharmony_ci
741bf215546Sopenharmony_ci      memset(&reloc, 0, sizeof(struct etna_reloc));
742bf215546Sopenharmony_ci      reloc.bo = src->ts_bo;
743bf215546Sopenharmony_ci      reloc.offset = ts_offset;
744bf215546Sopenharmony_ci      reloc.flags = ETNA_RELOC_READ;
745bf215546Sopenharmony_ci      etna_set_state_reloc(ctx->stream, VIVS_TS_COLOR_STATUS_BASE, &reloc);
746bf215546Sopenharmony_ci
747bf215546Sopenharmony_ci      memset(&reloc, 0, sizeof(struct etna_reloc));
748bf215546Sopenharmony_ci      reloc.bo = src->bo;
749bf215546Sopenharmony_ci      reloc.offset = src_lev->offset +
750bf215546Sopenharmony_ci                     blit_info->src.box.z * src_lev->layer_stride;
751bf215546Sopenharmony_ci      reloc.flags = ETNA_RELOC_READ;
752bf215546Sopenharmony_ci      etna_set_state_reloc(ctx->stream, VIVS_TS_COLOR_SURFACE_BASE, &reloc);
753bf215546Sopenharmony_ci
754bf215546Sopenharmony_ci      etna_set_state(ctx->stream, VIVS_TS_COLOR_CLEAR_VALUE, src_lev->clear_value);
755bf215546Sopenharmony_ci      etna_set_state(ctx->stream, VIVS_TS_COLOR_CLEAR_VALUE_EXT, src_lev->clear_value >> 32);
756bf215546Sopenharmony_ci
757bf215546Sopenharmony_ci      source_ts_valid = true;
758bf215546Sopenharmony_ci   } else {
759bf215546Sopenharmony_ci      etna_set_state(ctx->stream, VIVS_TS_MEM_CONFIG, 0);
760bf215546Sopenharmony_ci   }
761bf215546Sopenharmony_ci   ctx->dirty |= ETNA_DIRTY_TS;
762bf215546Sopenharmony_ci
763bf215546Sopenharmony_ci   /* Kick off RS here */
764bf215546Sopenharmony_ci   etna_compile_rs_state(ctx, &copy_to_screen, &(struct rs_state) {
765bf215546Sopenharmony_ci      .source_format = format,
766bf215546Sopenharmony_ci      .source_tiling = src->layout,
767bf215546Sopenharmony_ci      .source = src->bo,
768bf215546Sopenharmony_ci      .source_offset = src_offset,
769bf215546Sopenharmony_ci      .source_stride = src_lev->stride,
770bf215546Sopenharmony_ci      .source_padded_width = src_lev->padded_width,
771bf215546Sopenharmony_ci      .source_padded_height = src_lev->padded_height,
772bf215546Sopenharmony_ci      .source_ts_valid = source_ts_valid,
773bf215546Sopenharmony_ci      .source_ts_mode = src_lev->ts_mode,
774bf215546Sopenharmony_ci      .source_ts_compressed = src_lev->ts_compress_fmt >= 0,
775bf215546Sopenharmony_ci      .dest_format = format,
776bf215546Sopenharmony_ci      .dest_tiling = dst->layout,
777bf215546Sopenharmony_ci      .dest = dst->bo,
778bf215546Sopenharmony_ci      .dest_offset = dst_offset,
779bf215546Sopenharmony_ci      .dest_stride = dst_lev->stride,
780bf215546Sopenharmony_ci      .dest_padded_height = dst_lev->padded_height,
781bf215546Sopenharmony_ci      .downsample_x = msaa_xscale > 1,
782bf215546Sopenharmony_ci      .downsample_y = msaa_yscale > 1,
783bf215546Sopenharmony_ci      .swap_rb = translate_rb_src_dst_swap(src->base.format, dst->base.format),
784bf215546Sopenharmony_ci      .dither = {0xffffffff, 0xffffffff}, // XXX dither when going from 24 to 16 bit?
785bf215546Sopenharmony_ci      .clear_mode = VIVS_RS_CLEAR_CONTROL_MODE_DISABLED,
786bf215546Sopenharmony_ci      .width = width,
787bf215546Sopenharmony_ci      .height = height,
788bf215546Sopenharmony_ci      .tile_count = src_lev->layer_stride /
789bf215546Sopenharmony_ci                    etna_screen_get_tile_size(ctx->screen, src_lev->ts_mode),
790bf215546Sopenharmony_ci   });
791bf215546Sopenharmony_ci
792bf215546Sopenharmony_ci   etna_submit_rs_state(ctx, &copy_to_screen);
793bf215546Sopenharmony_ci   resource_read(ctx, &src->base);
794bf215546Sopenharmony_ci   resource_written(ctx, &dst->base);
795bf215546Sopenharmony_ci   dst->seqno++;
796bf215546Sopenharmony_ci   dst_lev->ts_valid = false;
797bf215546Sopenharmony_ci   ctx->dirty |= ETNA_DIRTY_DERIVE_TS;
798bf215546Sopenharmony_ci
799bf215546Sopenharmony_ci   return true;
800bf215546Sopenharmony_ci
801bf215546Sopenharmony_cimanual:
802bf215546Sopenharmony_ci   if (src->layout == ETNA_LAYOUT_TILED && dst->layout == ETNA_LAYOUT_TILED) {
803bf215546Sopenharmony_ci      if ((etna_resource_status(ctx, src) & ETNA_PENDING_WRITE) ||
804bf215546Sopenharmony_ci          (etna_resource_status(ctx, dst) & ETNA_PENDING_WRITE))
805bf215546Sopenharmony_ci         pctx->flush(pctx, NULL, 0);
806bf215546Sopenharmony_ci      return etna_manual_blit(dst, dst_lev, dst_offset, src, src_lev, src_offset, blit_info);
807bf215546Sopenharmony_ci   }
808bf215546Sopenharmony_ci
809bf215546Sopenharmony_ci   return false;
810bf215546Sopenharmony_ci}
811bf215546Sopenharmony_ci
812bf215546Sopenharmony_cistatic bool
813bf215546Sopenharmony_cietna_blit_rs(struct pipe_context *pctx, const struct pipe_blit_info *blit_info)
814bf215546Sopenharmony_ci{
815bf215546Sopenharmony_ci   /* This is a more extended version of resource_copy_region */
816bf215546Sopenharmony_ci   /* TODO Some cases can be handled by RS; if not, fall back to rendering or
817bf215546Sopenharmony_ci    * even CPU copy block of pixels from info->src to info->dst
818bf215546Sopenharmony_ci    * (resource, level, box, format);
819bf215546Sopenharmony_ci    * function is used for scaling, flipping in x and y direction (negative
820bf215546Sopenharmony_ci    * width/height), format conversion, mask and filter and even a scissor rectangle
821bf215546Sopenharmony_ci    *
822bf215546Sopenharmony_ci    * What can the RS do for us:
823bf215546Sopenharmony_ci    *   convert between tiling formats (layouts)
824bf215546Sopenharmony_ci    *   downsample 2x in x and y
825bf215546Sopenharmony_ci    *   convert between a limited number of pixel formats
826bf215546Sopenharmony_ci    *
827bf215546Sopenharmony_ci    * For the rest, fall back to util_blitter
828bf215546Sopenharmony_ci    * XXX this goes wrong when source surface is supertiled. */
829bf215546Sopenharmony_ci
830bf215546Sopenharmony_ci   if (blit_info->src.resource->nr_samples > 1 &&
831bf215546Sopenharmony_ci       blit_info->dst.resource->nr_samples <= 1 &&
832bf215546Sopenharmony_ci       !util_format_is_depth_or_stencil(blit_info->src.resource->format) &&
833bf215546Sopenharmony_ci       !util_format_is_pure_integer(blit_info->src.resource->format)) {
834bf215546Sopenharmony_ci      DBG("color resolve unimplemented");
835bf215546Sopenharmony_ci      return false;
836bf215546Sopenharmony_ci   }
837bf215546Sopenharmony_ci
838bf215546Sopenharmony_ci   return etna_try_rs_blit(pctx, blit_info);
839bf215546Sopenharmony_ci}
840bf215546Sopenharmony_ci
841bf215546Sopenharmony_civoid
842bf215546Sopenharmony_cietna_clear_blit_rs_init(struct pipe_context *pctx)
843bf215546Sopenharmony_ci{
844bf215546Sopenharmony_ci   struct etna_context *ctx = etna_context(pctx);
845bf215546Sopenharmony_ci
846bf215546Sopenharmony_ci   DBG("etnaviv: Using RS blit engine");
847bf215546Sopenharmony_ci   pctx->clear = etna_clear_rs;
848bf215546Sopenharmony_ci   ctx->blit = etna_blit_rs;
849bf215546Sopenharmony_ci}
850