1 /*
2 * Copyright (c) 2012-2017 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_rs.h"
28
29 #include "etnaviv_clear_blit.h"
30 #include "etnaviv_context.h"
31 #include "etnaviv_emit.h"
32 #include "etnaviv_format.h"
33 #include "etnaviv_resource.h"
34 #include "etnaviv_screen.h"
35 #include "etnaviv_state.h"
36 #include "etnaviv_surface.h"
37 #include "etnaviv_tiling.h"
38 #include "etnaviv_translate.h"
39 #include "etnaviv_util.h"
40
41 #include "pipe/p_defines.h"
42 #include "pipe/p_state.h"
43 #include "util/compiler.h"
44 #include "util/u_blitter.h"
45 #include "util/u_inlines.h"
46 #include "util/u_memory.h"
47 #include "util/u_surface.h"
48
49 #include "hw/state.xml.h"
50 #include "hw/state_3d.xml.h"
51
52 #include <assert.h>
53
54 /* return a RS "compatible" format for use when copying */
55 static uint32_t
etna_compatible_rs_format(enum pipe_format fmt)56 etna_compatible_rs_format(enum pipe_format fmt)
57 {
58 /* YUYV and UYVY are blocksize 4, but 2 bytes per pixel */
59 if (fmt == PIPE_FORMAT_YUYV || fmt == PIPE_FORMAT_UYVY)
60 return RS_FORMAT_A4R4G4B4;
61
62 switch (util_format_get_blocksize(fmt)) {
63 case 2: return RS_FORMAT_A4R4G4B4;
64 case 4: return RS_FORMAT_A8R8G8B8;
65 default: return ETNA_NO_MATCH;
66 }
67 }
68
69 void
etna_compile_rs_state(struct etna_context * ctx,struct compiled_rs_state * cs,const struct rs_state * rs)70 etna_compile_rs_state(struct etna_context *ctx, struct compiled_rs_state *cs,
71 const struct rs_state *rs)
72 {
73 struct etna_screen *screen = ctx->screen;
74
75 memset(cs, 0, sizeof(*cs));
76
77 /* TILED and SUPERTILED layout have their strides multiplied with 4 in RS */
78 unsigned source_stride_shift = COND(rs->source_tiling != ETNA_LAYOUT_LINEAR, 2);
79 unsigned dest_stride_shift = COND(rs->dest_tiling != ETNA_LAYOUT_LINEAR, 2);
80
81 bool src_tiled = rs->source_tiling & ETNA_LAYOUT_BIT_TILE;
82 bool dst_tiled = rs->dest_tiling & ETNA_LAYOUT_BIT_TILE;
83 bool src_super = rs->source_tiling & ETNA_LAYOUT_BIT_SUPER;
84 bool dst_super = rs->dest_tiling & ETNA_LAYOUT_BIT_SUPER;
85 bool src_multi = rs->source_tiling & ETNA_LAYOUT_BIT_MULTI;
86 bool dst_multi = rs->dest_tiling & ETNA_LAYOUT_BIT_MULTI;
87
88 /* Vivante RS needs widths to be a multiple of 16 or bad things
89 * happen, such as scribbing over memory, or the GPU hanging,
90 * even for non-tiled formats. As this is serious, use abort().
91 */
92 if (rs->width & ETNA_RS_WIDTH_MASK)
93 abort();
94
95 /* TODO could just pre-generate command buffer, would simply submit to one memcpy */
96 cs->RS_CONFIG = VIVS_RS_CONFIG_SOURCE_FORMAT(rs->source_format) |
97 COND(rs->downsample_x, VIVS_RS_CONFIG_DOWNSAMPLE_X) |
98 COND(rs->downsample_y, VIVS_RS_CONFIG_DOWNSAMPLE_Y) |
99 COND(src_tiled, VIVS_RS_CONFIG_SOURCE_TILED) |
100 VIVS_RS_CONFIG_DEST_FORMAT(rs->dest_format) |
101 COND(dst_tiled, VIVS_RS_CONFIG_DEST_TILED) |
102 COND(rs->swap_rb, VIVS_RS_CONFIG_SWAP_RB) |
103 COND(rs->flip, VIVS_RS_CONFIG_FLIP);
104
105 cs->RS_SOURCE_STRIDE = (rs->source_stride << source_stride_shift) |
106 COND(src_super, VIVS_RS_SOURCE_STRIDE_TILING) |
107 COND(src_multi, VIVS_RS_SOURCE_STRIDE_MULTI);
108
109 if (VIV_FEATURE(ctx->screen, ETNA_FEATURE_CACHE128B256BPERLINE))
110 cs->RS_SOURCE_STRIDE |= VIVS_RS_SOURCE_STRIDE_TS_MODE(rs->source_ts_mode) |
111 COND(src_super, VIVS_RS_SOURCE_STRIDE_SUPER_TILED_NEW);
112 else if ((rs->downsample_x || rs->downsample_y) && VIV_FEATURE(screen, ETNA_FEATURE_SMALL_MSAA))
113 cs->RS_SOURCE_STRIDE |= VIVS_RS_SOURCE_STRIDE_TS_MODE(TS_MODE_256B);
114
115 /* Initially all pipes are set to the base address of the source and
116 * destination buffer respectively. This will be overridden below as
117 * necessary for the multi-pipe, multi-tiled case.
118 */
119 for (unsigned pipe = 0; pipe < screen->specs.pixel_pipes; ++pipe) {
120 cs->source[pipe].bo = rs->source;
121 cs->source[pipe].offset = rs->source_offset;
122 cs->source[pipe].flags = ETNA_RELOC_READ;
123
124 cs->dest[pipe].bo = rs->dest;
125 cs->dest[pipe].offset = rs->dest_offset;
126 cs->dest[pipe].flags = ETNA_RELOC_WRITE;
127
128 cs->RS_PIPE_OFFSET[pipe] = VIVS_RS_PIPE_OFFSET_X(0) | VIVS_RS_PIPE_OFFSET_Y(0);
129 }
130
131 cs->RS_DEST_STRIDE = (rs->dest_stride << dest_stride_shift) |
132 COND(dst_super, VIVS_RS_DEST_STRIDE_TILING) |
133 COND(dst_multi, VIVS_RS_DEST_STRIDE_MULTI);
134
135 if (VIV_FEATURE(ctx->screen, ETNA_FEATURE_CACHE128B256BPERLINE))
136 cs->RS_DEST_STRIDE |= COND(dst_super, VIVS_RS_DEST_STRIDE_SUPER_TILED_NEW);
137
138 if (src_multi)
139 cs->source[1].offset = rs->source_offset + rs->source_stride * rs->source_padded_height / 2;
140
141 if (dst_multi)
142 cs->dest[1].offset = rs->dest_offset + rs->dest_stride * rs->dest_padded_height / 2;
143
144 cs->RS_WINDOW_SIZE = VIVS_RS_WINDOW_SIZE_WIDTH(rs->width) |
145 VIVS_RS_WINDOW_SIZE_HEIGHT(rs->height);
146
147 /* use dual pipe mode when required */
148 if (!screen->specs.single_buffer && screen->specs.pixel_pipes == 2 &&
149 !(rs->height & (rs->downsample_y ? 0xf : 0x7))) {
150 cs->RS_WINDOW_SIZE = VIVS_RS_WINDOW_SIZE_WIDTH(rs->width) |
151 VIVS_RS_WINDOW_SIZE_HEIGHT(rs->height / 2);
152 cs->RS_PIPE_OFFSET[1] = VIVS_RS_PIPE_OFFSET_X(0) | VIVS_RS_PIPE_OFFSET_Y(rs->height / 2);
153 }
154
155 cs->RS_DITHER[0] = rs->dither[0];
156 cs->RS_DITHER[1] = rs->dither[1];
157 cs->RS_CLEAR_CONTROL = VIVS_RS_CLEAR_CONTROL_BITS(rs->clear_bits) | rs->clear_mode;
158 cs->RS_FILL_VALUE[0] = rs->clear_value[0];
159 cs->RS_FILL_VALUE[1] = rs->clear_value[1];
160 cs->RS_FILL_VALUE[2] = rs->clear_value[2];
161 cs->RS_FILL_VALUE[3] = rs->clear_value[3];
162 cs->RS_EXTRA_CONFIG = VIVS_RS_EXTRA_CONFIG_AA(rs->aa) |
163 VIVS_RS_EXTRA_CONFIG_ENDIAN(rs->endian_mode);
164
165 /* If source the same as destination, and the hardware supports this,
166 * do an in-place resolve to fill in unrendered tiles.
167 */
168 if (screen->specs.single_buffer && rs->source == rs->dest &&
169 rs->source_offset == rs->dest_offset &&
170 rs->source_format == rs->dest_format &&
171 rs->source_tiling == rs->dest_tiling &&
172 src_super &&
173 rs->source_stride == rs->dest_stride &&
174 !rs->downsample_x && !rs->downsample_y &&
175 !rs->swap_rb && !rs->flip &&
176 !rs->clear_mode && rs->source_padded_width &&
177 !rs->source_ts_compressed) {
178 if (VIV_FEATURE(ctx->screen, ETNA_FEATURE_CACHE128B256BPERLINE))
179 cs->RS_EXTRA_CONFIG |= VIVS_RS_EXTRA_CONFIG_TS_MODE(rs->source_ts_mode);
180 /* Total number of tiles (same as for autodisable) */
181 cs->RS_KICKER_INPLACE = rs->tile_count;
182 }
183 cs->source_ts_valid = rs->source_ts_valid;
184 cs->valid = true;
185 }
186
187 /* modify the clear bits value in the compiled RS state */
188 static void
etna_modify_rs_clearbits(struct compiled_rs_state * cs,uint32_t clear_bits)189 etna_modify_rs_clearbits(struct compiled_rs_state *cs, uint32_t clear_bits)
190 {
191 cs->RS_CLEAR_CONTROL &= ~VIVS_RS_CLEAR_CONTROL_BITS__MASK;
192 cs->RS_CLEAR_CONTROL |= VIVS_RS_CLEAR_CONTROL_BITS(clear_bits);
193 }
194
195 #define EMIT_STATE(state_name, src_value) \
196 etna_coalsence_emit(stream, &coalesce, VIVS_##state_name, src_value)
197
198 #define EMIT_STATE_FIXP(state_name, src_value) \
199 etna_coalsence_emit_fixp(stream, &coalesce, VIVS_##state_name, src_value)
200
201 #define EMIT_STATE_RELOC(state_name, src_value) \
202 etna_coalsence_emit_reloc(stream, &coalesce, VIVS_##state_name, src_value)
203
204 /* submit RS state, without any processing and no dependence on context
205 * except TS if this is a source-to-destination blit. */
206 static void
etna_submit_rs_state(struct etna_context * ctx,const struct compiled_rs_state * cs)207 etna_submit_rs_state(struct etna_context *ctx,
208 const struct compiled_rs_state *cs)
209 {
210 struct etna_screen *screen = etna_screen(ctx->base.screen);
211 struct etna_cmd_stream *stream = ctx->stream;
212 struct etna_coalesce coalesce;
213
214 if (cs->RS_KICKER_INPLACE && !cs->source_ts_valid)
215 /* Inplace resolve is no-op if TS is not configured */
216 return;
217
218 ctx->stats.rs_operations++;
219
220 if (cs->RS_KICKER_INPLACE) {
221 etna_cmd_stream_reserve(stream, 6);
222 etna_coalesce_start(stream, &coalesce);
223 /* 0/1 */ EMIT_STATE(RS_EXTRA_CONFIG, cs->RS_EXTRA_CONFIG);
224 /* 2/3 */ EMIT_STATE(RS_SOURCE_STRIDE, cs->RS_SOURCE_STRIDE);
225 /* 4/5 */ EMIT_STATE(RS_KICKER_INPLACE, cs->RS_KICKER_INPLACE);
226 etna_coalesce_end(stream, &coalesce);
227 } else if (screen->specs.pixel_pipes > 1 ||
228 VIV_FEATURE(screen, ETNA_FEATURE_RS_NEW_BASEADDR)) {
229 etna_cmd_stream_reserve(stream, 34); /* worst case - both pipes multi=1 */
230 etna_coalesce_start(stream, &coalesce);
231 /* 0/1 */ EMIT_STATE(RS_CONFIG, cs->RS_CONFIG);
232 /* 2/3 */ EMIT_STATE(RS_SOURCE_STRIDE, cs->RS_SOURCE_STRIDE);
233 /* 4/5 */ EMIT_STATE(RS_DEST_STRIDE, cs->RS_DEST_STRIDE);
234 /* 6/7 */ EMIT_STATE_RELOC(RS_PIPE_SOURCE_ADDR(0), &cs->source[0]);
235 if (cs->RS_SOURCE_STRIDE & VIVS_RS_SOURCE_STRIDE_MULTI) {
236 /*8 */ EMIT_STATE_RELOC(RS_PIPE_SOURCE_ADDR(1), &cs->source[1]);
237 /*9 - pad */
238 }
239 /*10/11*/ EMIT_STATE_RELOC(RS_PIPE_DEST_ADDR(0), &cs->dest[0]);
240 if (cs->RS_DEST_STRIDE & VIVS_RS_DEST_STRIDE_MULTI) {
241 /*12*/ EMIT_STATE_RELOC(RS_PIPE_DEST_ADDR(1), &cs->dest[1]);
242 /*13 - pad */
243 }
244 /*14/15*/ EMIT_STATE(RS_PIPE_OFFSET(0), cs->RS_PIPE_OFFSET[0]);
245 /*16 */ EMIT_STATE(RS_PIPE_OFFSET(1), cs->RS_PIPE_OFFSET[1]);
246 /*17 - pad */
247 /*18/19*/ EMIT_STATE(RS_WINDOW_SIZE, cs->RS_WINDOW_SIZE);
248 /*20/21*/ EMIT_STATE(RS_DITHER(0), cs->RS_DITHER[0]);
249 /*22 */ EMIT_STATE(RS_DITHER(1), cs->RS_DITHER[1]);
250 /*23 - pad */
251 /*24/25*/ EMIT_STATE(RS_CLEAR_CONTROL, cs->RS_CLEAR_CONTROL);
252 /*26 */ EMIT_STATE(RS_FILL_VALUE(0), cs->RS_FILL_VALUE[0]);
253 /*27 */ EMIT_STATE(RS_FILL_VALUE(1), cs->RS_FILL_VALUE[1]);
254 /*28 */ EMIT_STATE(RS_FILL_VALUE(2), cs->RS_FILL_VALUE[2]);
255 /*29 */ EMIT_STATE(RS_FILL_VALUE(3), cs->RS_FILL_VALUE[3]);
256 /*30/31*/ EMIT_STATE(RS_EXTRA_CONFIG, cs->RS_EXTRA_CONFIG);
257 /*32/33*/ EMIT_STATE(RS_KICKER, 0xbeebbeeb);
258 etna_coalesce_end(stream, &coalesce);
259 } else {
260 etna_cmd_stream_reserve(stream, 22);
261 etna_coalesce_start(stream, &coalesce);
262 /* 0/1 */ EMIT_STATE(RS_CONFIG, cs->RS_CONFIG);
263 /* 2 */ EMIT_STATE_RELOC(RS_SOURCE_ADDR, &cs->source[0]);
264 /* 3 */ EMIT_STATE(RS_SOURCE_STRIDE, cs->RS_SOURCE_STRIDE);
265 /* 4 */ EMIT_STATE_RELOC(RS_DEST_ADDR, &cs->dest[0]);
266 /* 5 */ EMIT_STATE(RS_DEST_STRIDE, cs->RS_DEST_STRIDE);
267 /* 6/7 */ EMIT_STATE(RS_WINDOW_SIZE, cs->RS_WINDOW_SIZE);
268 /* 8/9 */ EMIT_STATE(RS_DITHER(0), cs->RS_DITHER[0]);
269 /*10 */ EMIT_STATE(RS_DITHER(1), cs->RS_DITHER[1]);
270 /*11 - pad */
271 /*12/13*/ EMIT_STATE(RS_CLEAR_CONTROL, cs->RS_CLEAR_CONTROL);
272 /*14 */ EMIT_STATE(RS_FILL_VALUE(0), cs->RS_FILL_VALUE[0]);
273 /*15 */ EMIT_STATE(RS_FILL_VALUE(1), cs->RS_FILL_VALUE[1]);
274 /*16 */ EMIT_STATE(RS_FILL_VALUE(2), cs->RS_FILL_VALUE[2]);
275 /*17 */ EMIT_STATE(RS_FILL_VALUE(3), cs->RS_FILL_VALUE[3]);
276 /*18/19*/ EMIT_STATE(RS_EXTRA_CONFIG, cs->RS_EXTRA_CONFIG);
277 /*20/21*/ EMIT_STATE(RS_KICKER, 0xbeebbeeb);
278 etna_coalesce_end(stream, &coalesce);
279 }
280
281 if (DBG_ENABLED(ETNA_DBG_DRAW_STALL))
282 etna_stall(stream, SYNC_RECIPIENT_FE, SYNC_RECIPIENT_PE);
283 }
284
285 /* Generate clear command for a surface (non-fast clear case) */
286 static void
etna_rs_gen_clear_surface(struct etna_context * ctx,struct etna_surface * surf,uint64_t clear_value)287 etna_rs_gen_clear_surface(struct etna_context *ctx, struct etna_surface *surf,
288 uint64_t clear_value)
289 {
290 ASSERTED struct etna_screen *screen = ctx->screen;
291 struct etna_resource *dst = etna_resource(surf->base.texture);
292 uint32_t format;
293
294 switch (util_format_get_blocksizebits(surf->base.format)) {
295 case 16:
296 format = RS_FORMAT_A4R4G4B4;
297 break;
298 case 32:
299 format = RS_FORMAT_A8R8G8B8;
300 break;
301 case 64:
302 assert(screen->info->halti >= 2);
303 format = RS_FORMAT_64BPP_CLEAR;
304 break;
305 default:
306 unreachable("bpp not supported for clear by RS");
307 break;
308 }
309
310 /* use tiled clear if width is multiple of 16 */
311 bool tiled_clear = (surf->level->padded_width & ETNA_RS_WIDTH_MASK) == 0 &&
312 (surf->level->padded_height & ETNA_RS_HEIGHT_MASK) == 0;
313
314 etna_compile_rs_state( ctx, &surf->clear_command, &(struct rs_state) {
315 .source_format = format,
316 .dest_format = format,
317 .dest = dst->bo,
318 .dest_offset = surf->offset,
319 .dest_stride = surf->level->stride,
320 .dest_padded_height = surf->level->padded_height,
321 .dest_tiling = tiled_clear ? dst->layout : ETNA_LAYOUT_LINEAR,
322 .dither = {0xffffffff, 0xffffffff},
323 .width = surf->level->padded_width, /* These must be padded to 16x4 if !LINEAR, otherwise RS will hang */
324 .height = surf->level->padded_height,
325 .clear_value = {clear_value, clear_value >> 32, clear_value, clear_value >> 32},
326 .clear_mode = VIVS_RS_CLEAR_CONTROL_MODE_ENABLED1,
327 .clear_bits = 0xffff
328 });
329 }
330
331 static void
etna_blit_clear_color_rs(struct pipe_context * pctx,unsigned idx,const union pipe_color_union * color,bool use_ts)332 etna_blit_clear_color_rs(struct pipe_context *pctx, unsigned idx,
333 const union pipe_color_union *color, bool use_ts)
334 {
335 struct etna_context *ctx = etna_context(pctx);
336 struct pipe_surface *dst = ctx->framebuffer_s.cbufs[idx];
337 struct etna_surface *surf = etna_surface(dst);
338 uint64_t new_clear_value = etna_clear_blit_pack_rgba(surf->base.format, color);
339
340 if (use_ts && surf->level->ts_size) { /* TS: use precompiled clear command */
341 if (idx == 0) {
342 ctx->framebuffer.TS_COLOR_CLEAR_VALUE = new_clear_value;
343 ctx->framebuffer.TS_COLOR_CLEAR_VALUE_EXT = new_clear_value >> 32;
344 } else {
345 ctx->framebuffer.RT_TS_COLOR_CLEAR_VALUE[idx - 1] = new_clear_value;
346 ctx->framebuffer.RT_TS_COLOR_CLEAR_VALUE_EXT[idx - 1] = new_clear_value >> 32;
347 }
348
349 if (VIV_FEATURE(ctx->screen, ETNA_FEATURE_AUTO_DISABLE)) {
350 /* Set number of color tiles to be filled */
351 etna_set_state(ctx->stream, VIVS_TS_COLOR_AUTO_DISABLE_COUNT,
352 surf->level->padded_width * surf->level->padded_height / 16);
353 ctx->framebuffer.TS_MEM_CONFIG |= VIVS_TS_MEM_CONFIG_COLOR_AUTO_DISABLE;
354 }
355
356 /* update clear color in SW meta area of the buffer if TS is exported */
357 if (unlikely(new_clear_value != surf->level->clear_value &&
358 etna_resource_ext_ts(etna_resource(dst->texture))))
359 surf->level->ts_meta->v0.clear_value = new_clear_value;
360
361 assert(surf->ts_clear_command.valid);
362 etna_submit_rs_state(ctx, &surf->ts_clear_command);
363
364 etna_resource_level_ts_mark_valid(surf->level);
365 etna_resource_level_mark_unflushed(surf->level);
366 ctx->dirty |= ETNA_DIRTY_TS;
367 } else { /* Queue normal RS clear for non-TS surfaces */
368 /* If clear color changed or no valid command yet (re-)generate
369 * stored command */
370 if (unlikely(new_clear_value != surf->level->clear_value ||
371 !surf->clear_command.valid))
372 etna_rs_gen_clear_surface(ctx, surf, new_clear_value);
373
374 etna_submit_rs_state(ctx, &surf->clear_command);
375 etna_resource_level_ts_mark_invalid(surf->level);
376 }
377
378 ctx->dirty |= ETNA_DIRTY_DERIVE_TS;
379 surf->level->clear_value = new_clear_value;
380 resource_written(ctx, surf->base.texture);
381 etna_resource_level_mark_changed(surf->level);
382 }
383
384 static void
etna_blit_clear_zs_rs(struct pipe_context * pctx,struct pipe_surface * dst,unsigned buffers,double depth,unsigned stencil)385 etna_blit_clear_zs_rs(struct pipe_context *pctx, struct pipe_surface *dst,
386 unsigned buffers, double depth, unsigned stencil)
387 {
388 struct etna_context *ctx = etna_context(pctx);
389 struct etna_surface *surf = etna_surface(dst);
390 uint32_t new_clear_value = translate_clear_depth_stencil(surf->base.format, depth, stencil);
391 uint32_t new_clear_bits = 0, clear_bits_depth, clear_bits_stencil;
392
393 /* Get the channels to clear */
394 switch (surf->base.format) {
395 case PIPE_FORMAT_Z16_UNORM:
396 case PIPE_FORMAT_X8Z24_UNORM:
397 clear_bits_depth = 0xffff;
398 clear_bits_stencil = 0;
399 break;
400 case PIPE_FORMAT_S8_UINT_Z24_UNORM:
401 clear_bits_depth = 0xeeee;
402 clear_bits_stencil = 0x1111;
403 break;
404 default:
405 clear_bits_depth = clear_bits_stencil = 0xffff;
406 break;
407 }
408
409 if (buffers & PIPE_CLEAR_DEPTH)
410 new_clear_bits |= clear_bits_depth;
411 if (buffers & PIPE_CLEAR_STENCIL)
412 new_clear_bits |= clear_bits_stencil;
413
414 if (surf->level->ts_size && new_clear_bits == 0xffff) {
415 /* Set new clear depth value */
416 ctx->framebuffer.TS_DEPTH_CLEAR_VALUE = new_clear_value;
417 if (VIV_FEATURE(ctx->screen, ETNA_FEATURE_AUTO_DISABLE)) {
418 /* Set number of depth tiles to be filled */
419 etna_set_state(ctx->stream, VIVS_TS_DEPTH_AUTO_DISABLE_COUNT,
420 surf->level->padded_width * surf->level->padded_height / 16);
421 ctx->framebuffer.TS_MEM_CONFIG |= VIVS_TS_MEM_CONFIG_DEPTH_AUTO_DISABLE;
422 }
423
424 assert(surf->ts_clear_command.valid);
425 etna_submit_rs_state(ctx, &surf->ts_clear_command);
426
427 etna_resource_level_ts_mark_valid(surf->level);
428 etna_resource_level_mark_unflushed(surf->level);
429 ctx->dirty |= ETNA_DIRTY_TS;
430 } else { /* Queue normal RS clear for non-TS surfaces */
431 /* If the level has valid TS state we need to flush it, as the regular
432 * clear will not update the state and we must therefore invalidate it. */
433 etna_copy_resource(pctx, surf->base.texture, surf->base.texture,
434 surf->base.u.tex.level, surf->base.u.tex.level);
435
436 if (unlikely(new_clear_value != surf->level->clear_value ||
437 !surf->clear_command.valid)) {
438 /* If clear depth/stencil value changed or no valid command yet
439 * (re)-generate stored command */
440 etna_rs_gen_clear_surface(ctx, surf, new_clear_value);
441 }
442 /* Update the channels to be cleared */
443 etna_modify_rs_clearbits(&surf->clear_command, new_clear_bits);
444
445 etna_submit_rs_state(ctx, &surf->clear_command);
446
447 etna_resource_level_ts_mark_invalid(surf->level);
448 }
449
450 surf->level->clear_value = new_clear_value;
451 resource_written(ctx, surf->base.texture);
452 etna_resource_level_mark_changed(surf->level);
453 ctx->dirty |= ETNA_DIRTY_DERIVE_TS;
454 }
455
456 static void
etna_clear_rs(struct pipe_context * pctx,unsigned buffers,const struct pipe_scissor_state * scissor_state,const union pipe_color_union * color,double depth,unsigned stencil)457 etna_clear_rs(struct pipe_context *pctx, unsigned buffers, const struct pipe_scissor_state *scissor_state,
458 const union pipe_color_union *color, double depth, unsigned stencil)
459 {
460 struct etna_context *ctx = etna_context(pctx);
461
462 if (!etna_render_condition_check(pctx))
463 return;
464
465 /* Flush color and depth cache before clearing anything.
466 * This is especially important when coming from another surface, as
467 * otherwise it may clear part of the old surface instead. */
468 etna_set_state(ctx->stream, VIVS_GL_FLUSH_CACHE, VIVS_GL_FLUSH_CACHE_COLOR | VIVS_GL_FLUSH_CACHE_DEPTH);
469 etna_stall(ctx->stream, SYNC_RECIPIENT_RA, SYNC_RECIPIENT_PE);
470
471 /* Preparation: Flush the TS if needed. This must be done after flushing
472 * color and depth, otherwise it can result in crashes */
473 bool need_ts_flush = false;
474 if (buffers & PIPE_CLEAR_COLOR) {
475 for (int idx = 0; idx < ctx->framebuffer_s.nr_cbufs; ++idx) {
476 struct etna_surface *surf = etna_surface(ctx->framebuffer_s.cbufs[idx]);
477
478 if (!surf)
479 continue;
480
481 if (surf->level->ts_size)
482 need_ts_flush = true;
483 }
484 }
485 if ((buffers & PIPE_CLEAR_DEPTHSTENCIL) && ctx->framebuffer_s.zsbuf != NULL) {
486 struct etna_surface *surf = etna_surface(ctx->framebuffer_s.zsbuf);
487
488 if (surf->level->ts_size)
489 need_ts_flush = true;
490 }
491
492 if (need_ts_flush)
493 etna_set_state(ctx->stream, VIVS_TS_FLUSH_CACHE, VIVS_TS_FLUSH_CACHE_FLUSH);
494
495 /* No need to set up the TS here as RS clear operations (in contrast to
496 * resolve and copy) do not require the TS state.
497 */
498 if (buffers & PIPE_CLEAR_COLOR) {
499 const bool use_ts = etna_use_ts_for_mrt(ctx->screen, &ctx->framebuffer_s);
500
501 for (int idx = 0; idx < ctx->framebuffer_s.nr_cbufs; ++idx) {
502 struct etna_surface *surf = etna_surface(ctx->framebuffer_s.cbufs[idx]);
503
504 if (!surf)
505 continue;
506
507 etna_blit_clear_color_rs(pctx, idx, color, use_ts);
508
509 if (!etna_resource(surf->prsc)->explicit_flush)
510 etna_context_add_flush_resource(ctx, surf->prsc);
511 }
512 }
513
514 /* Flush the color and depth caches before each RS clear operation
515 * This fixes a hang on GC600. */
516 if (buffers & PIPE_CLEAR_DEPTHSTENCIL && buffers & PIPE_CLEAR_COLOR)
517 etna_set_state(ctx->stream, VIVS_GL_FLUSH_CACHE,
518 VIVS_GL_FLUSH_CACHE_COLOR | VIVS_GL_FLUSH_CACHE_DEPTH);
519
520 if ((buffers & PIPE_CLEAR_DEPTHSTENCIL) && ctx->framebuffer_s.zsbuf != NULL)
521 etna_blit_clear_zs_rs(pctx, ctx->framebuffer_s.zsbuf, buffers, depth, stencil);
522
523 etna_stall(ctx->stream, SYNC_RECIPIENT_RA, SYNC_RECIPIENT_PE);
524 }
525
526 static bool
etna_manual_blit(struct etna_resource * dst,struct etna_resource_level * dst_lev,unsigned int dst_offset,struct etna_resource * src,struct etna_resource_level * src_lev,unsigned int src_offset,const struct pipe_blit_info * blit_info)527 etna_manual_blit(struct etna_resource *dst, struct etna_resource_level *dst_lev,
528 unsigned int dst_offset, struct etna_resource *src,
529 struct etna_resource_level *src_lev, unsigned int src_offset,
530 const struct pipe_blit_info *blit_info)
531 {
532 void *smap, *srow, *dmap, *drow;
533 size_t tile_size;
534
535 assert(src->layout == ETNA_LAYOUT_TILED);
536 assert(dst->layout == ETNA_LAYOUT_TILED);
537 assert(src->base.nr_samples == 0);
538 assert(dst->base.nr_samples == 0);
539
540 tile_size = util_format_get_blocksize(blit_info->src.format) * 4 * 4;
541
542 smap = etna_bo_map(src->bo);
543 if (!smap)
544 return false;
545
546 dmap = etna_bo_map(dst->bo);
547 if (!dmap)
548 return false;
549
550 srow = smap + src_offset;
551 drow = dmap + dst_offset;
552
553 etna_bo_cpu_prep(src->bo, DRM_ETNA_PREP_READ);
554 etna_bo_cpu_prep(dst->bo, DRM_ETNA_PREP_WRITE);
555
556 for (int y = 0; y < blit_info->src.box.height; y += 4) {
557 memcpy(drow, srow, tile_size * blit_info->src.box.width);
558 srow += src_lev->stride * 4;
559 drow += dst_lev->stride * 4;
560 }
561
562 etna_bo_cpu_fini(dst->bo);
563 etna_bo_cpu_fini(src->bo);
564
565 return true;
566 }
567
568 static inline size_t
etna_compute_tileoffset(const struct pipe_box * box,enum pipe_format format,size_t stride,enum etna_surface_layout layout)569 etna_compute_tileoffset(const struct pipe_box *box, enum pipe_format format,
570 size_t stride, enum etna_surface_layout layout)
571 {
572 size_t offset;
573 unsigned int x = box->x, y = box->y;
574 unsigned int blocksize = util_format_get_blocksize(format);
575
576 switch (layout) {
577 case ETNA_LAYOUT_LINEAR:
578 offset = y * stride + x * blocksize;
579 break;
580 case ETNA_LAYOUT_MULTI_TILED:
581 y >>= 1;
582 FALLTHROUGH;
583 case ETNA_LAYOUT_TILED:
584 assert(!(x & 0x03) && !(y & 0x03));
585 offset = (y & ~0x03) * stride + blocksize * ((x & ~0x03) << 2);
586 break;
587 case ETNA_LAYOUT_MULTI_SUPERTILED:
588 y >>= 1;
589 FALLTHROUGH;
590 case ETNA_LAYOUT_SUPER_TILED:
591 assert(!(x & 0x3f) && !(y & 0x3f));
592 offset = (y & ~0x3f) * stride + blocksize * ((x & ~0x3f) << 6);
593 break;
594 default:
595 unreachable("invalid resource layout");
596 }
597
598 return offset;
599 }
600
601 static inline void
etna_get_rs_alignment_mask(const struct etna_context * ctx,const enum etna_surface_layout layout,unsigned int * width_mask,unsigned int * height_mask)602 etna_get_rs_alignment_mask(const struct etna_context *ctx,
603 const enum etna_surface_layout layout,
604 unsigned int *width_mask, unsigned int *height_mask)
605 {
606 struct etna_screen *screen = ctx->screen;
607 unsigned int h_align, w_align;
608
609 if (layout & ETNA_LAYOUT_BIT_SUPER) {
610 w_align = 64;
611 h_align = 64 * screen->specs.pixel_pipes;
612 } else {
613 w_align = ETNA_RS_WIDTH_MASK + 1;
614 h_align = ETNA_RS_HEIGHT_MASK + 1;
615 }
616
617 *width_mask = w_align - 1;
618 *height_mask = h_align -1;
619 }
620
621 static bool
etna_try_rs_blit(struct pipe_context * pctx,const struct pipe_blit_info * blit_info)622 etna_try_rs_blit(struct pipe_context *pctx,
623 const struct pipe_blit_info *blit_info)
624 {
625 struct etna_context *ctx = etna_context(pctx);
626 struct etna_resource *src = etna_resource(blit_info->src.resource);
627 struct etna_resource *dst = etna_resource(blit_info->dst.resource);
628 struct compiled_rs_state copy_to_screen;
629 int src_xscale, src_yscale, dst_xscale, dst_yscale;
630 bool downsample_x = false, downsample_y = false;
631
632 /* Ensure that the level is valid */
633 assert(blit_info->src.level <= src->base.last_level);
634 assert(blit_info->dst.level <= dst->base.last_level);
635
636 if (!translate_samples_to_xyscale(src->base.nr_samples, &src_xscale, &src_yscale))
637 return false;
638 if (!translate_samples_to_xyscale(dst->base.nr_samples, &dst_xscale, &dst_yscale))
639 return false;
640
641 /* RS does not support upscaling */
642 if ((src_xscale < dst_xscale) || (src_yscale < dst_yscale)) {
643 DBG("upscaling requested: source %dx%d destination %dx%d",
644 src_xscale, src_yscale, dst_xscale, dst_yscale);
645 return false;
646 }
647
648 if (src_xscale > dst_xscale)
649 downsample_x = true;
650 if (src_yscale > dst_yscale)
651 downsample_y = true;
652
653 /* The width/height are in pixels; they do not change as a result of
654 * multi-sampling. So, when blitting from a 4x multisampled surface
655 * to a non-multisampled surface, the width and height will be
656 * identical. As we do not support scaling, reject different sizes. */
657 if (blit_info->dst.box.width != blit_info->src.box.width ||
658 blit_info->dst.box.height != blit_info->src.box.height) {
659 DBG("scaling requested: source %dx%d destination %dx%d",
660 blit_info->src.box.width, blit_info->src.box.height,
661 blit_info->dst.box.width, blit_info->dst.box.height);
662 return false;
663 }
664
665 /* No masks - RS can't copy specific channels */
666 unsigned mask = util_format_get_mask(blit_info->dst.format);
667 if ((blit_info->mask & mask) != mask) {
668 DBG("sub-mask requested: 0x%02x vs format mask 0x%02x", blit_info->mask, mask);
669 return false;
670 }
671
672 /* Only support same format (used tiling/detiling) blits for now.
673 * TODO: figure out which different-format blits are possible and test them
674 * - fail if swizzle needed
675 * - avoid trying to convert between float/int formats?
676 */
677 if (blit_info->src.format != blit_info->dst.format) {
678 DBG("non matching formats: %s vs %s",
679 util_format_short_name(blit_info->src.format),
680 util_format_short_name(blit_info->dst.format));
681 return false;
682 }
683
684 /* try to find a exact format match first */
685 uint32_t format = translate_rs_format(blit_info->dst.format);
686 /* When not resolving MSAA, but only doing a layout conversion, we can get
687 * away with a fallback format of matching size.
688 */
689 if (format == ETNA_NO_MATCH && !downsample_x && !downsample_y)
690 format = etna_compatible_rs_format(blit_info->dst.format);
691 if (format == ETNA_NO_MATCH) {
692 DBG("format not supported: %s", util_format_short_name(blit_info->dst.format));
693 return false;
694 }
695
696 if (blit_info->scissor_enable ||
697 blit_info->swizzle_enable ||
698 blit_info->dst.box.depth != blit_info->src.box.depth ||
699 blit_info->dst.box.depth != 1) {
700 return false;
701 }
702
703 unsigned w_mask, h_mask;
704
705 etna_get_rs_alignment_mask(ctx, src->layout, &w_mask, &h_mask);
706 if ((blit_info->src.box.x & w_mask) || (blit_info->src.box.y & h_mask)) {
707 DBG("src x/y not properly aligned: %d %d",
708 blit_info->src.box.x,
709 blit_info->src.box.y);
710 return false;
711 }
712
713 etna_get_rs_alignment_mask(ctx, dst->layout, &w_mask, &h_mask);
714 if ((blit_info->dst.box.x & w_mask) || (blit_info->dst.box.y & h_mask)) {
715 DBG("dst x/y not properly aligned: %d %d",
716 blit_info->src.box.x,
717 blit_info->src.box.y);
718 return false;
719 }
720
721 struct etna_resource_level *src_lev = &src->levels[blit_info->src.level];
722 struct etna_resource_level *dst_lev = &dst->levels[blit_info->dst.level];
723
724 /* we may be given coordinates up to the padded width to avoid
725 * any alignment issues with different tiling formats */
726 assert((blit_info->src.box.x + blit_info->src.box.width) * src_xscale <= src_lev->padded_width);
727 assert((blit_info->src.box.y + blit_info->src.box.height) * src_yscale <= src_lev->padded_height);
728 assert(blit_info->dst.box.x + blit_info->dst.box.width <= dst_lev->padded_width);
729 assert(blit_info->dst.box.y + blit_info->dst.box.height <= dst_lev->padded_height);
730
731 unsigned src_offset = src_lev->offset +
732 blit_info->src.box.z * src_lev->layer_stride +
733 etna_compute_tileoffset(&blit_info->src.box,
734 blit_info->src.format,
735 src_lev->stride,
736 src->layout);
737 unsigned dst_offset = dst_lev->offset +
738 blit_info->dst.box.z * dst_lev->layer_stride +
739 etna_compute_tileoffset(&blit_info->dst.box,
740 blit_info->dst.format,
741 dst_lev->stride,
742 dst->layout);
743
744 if (src_lev->padded_width <= ETNA_RS_WIDTH_MASK ||
745 dst_lev->padded_width <= ETNA_RS_WIDTH_MASK ||
746 src_lev->padded_height <= ETNA_RS_HEIGHT_MASK ||
747 dst_lev->padded_height <= ETNA_RS_HEIGHT_MASK)
748 goto manual;
749
750 /* If the width is not aligned to the RS width, but is within our
751 * padding, adjust the width to suite the RS width restriction.
752 * Note: the RS width/height are converted to source samples here. */
753 unsigned int width = blit_info->src.box.width * src_xscale;
754 unsigned int height = blit_info->src.box.height * src_yscale;
755 unsigned int w_align = (ETNA_RS_WIDTH_MASK + 1) * src_xscale;
756 unsigned int h_align = (ETNA_RS_HEIGHT_MASK + 1) * src_yscale;
757
758 if (width & (w_align - 1) && width >= src_lev->width * src_xscale && width >= dst_lev->width)
759 width = align(width, w_align);
760
761 if (height & (h_align - 1) && height >= src_lev->height * src_yscale && height >= dst_lev->height) {
762 height = align(height, h_align);
763
764 /* Try to increase alignment to multi-pipe requirements to unlock
765 * multi-pipe resolve for increased performance. */
766 if (!ctx->screen->specs.single_buffer) {
767 unsigned int pipe_align = align(height, h_align * ctx->screen->specs.pixel_pipes);
768
769 if (pipe_align <= src_lev->padded_height &&
770 pipe_align <= dst_lev->padded_height * src_yscale)
771 height = pipe_align;
772 }
773 }
774
775 /* The padded dimensions are in samples */
776 if (width > src_lev->padded_width ||
777 width > dst_lev->padded_width * src_xscale ||
778 height > src_lev->padded_height ||
779 height > dst_lev->padded_height * src_yscale ||
780 width & (w_align - 1) || height & (h_align - 1))
781 goto manual;
782
783 /* Flush destination, as the blit will invalidate any pending TS changes. */
784 if (dst != src && etna_resource_level_needs_flush(dst_lev))
785 etna_copy_resource(pctx, &dst->base, &dst->base,
786 blit_info->dst.level, blit_info->dst.level);
787
788 /* Always flush color and depth cache together before resolving. This makes
789 * sure that all previous cache content written by the PE is flushed out
790 * before RS uses the pixel pipes, which invalidates those caches. */
791 etna_set_state(ctx->stream, VIVS_GL_FLUSH_CACHE,
792 VIVS_GL_FLUSH_CACHE_COLOR | VIVS_GL_FLUSH_CACHE_DEPTH);
793 etna_stall(ctx->stream, SYNC_RECIPIENT_RA, SYNC_RECIPIENT_PE);
794
795 /* Set up color TS to source surface before blit, if needed */
796 bool source_ts_valid = false;
797 if (etna_resource_level_ts_valid(src_lev)) {
798 struct etna_reloc reloc;
799 unsigned ts_offset =
800 src_lev->ts_offset + blit_info->src.box.z * src_lev->ts_layer_stride;
801 uint32_t ts_mem_config = 0;
802
803 /* flush TS cache before changing to another TS configuration */
804 etna_set_state(ctx->stream, VIVS_TS_FLUSH_CACHE, VIVS_TS_FLUSH_CACHE_FLUSH);
805
806 if (src_lev->ts_compress_fmt >= 0) {
807 ts_mem_config |= VIVS_TS_MEM_CONFIG_COLOR_COMPRESSION |
808 VIVS_TS_MEM_CONFIG_COLOR_COMPRESSION_FORMAT(src_lev->ts_compress_fmt);
809 }
810
811 etna_set_state(ctx->stream, VIVS_TS_MEM_CONFIG,
812 VIVS_TS_MEM_CONFIG_COLOR_FAST_CLEAR | ts_mem_config);
813
814 memset(&reloc, 0, sizeof(struct etna_reloc));
815 reloc.bo = src->ts_bo;
816 reloc.offset = ts_offset;
817 reloc.flags = ETNA_RELOC_READ;
818 etna_set_state_reloc(ctx->stream, VIVS_TS_COLOR_STATUS_BASE, &reloc);
819
820 memset(&reloc, 0, sizeof(struct etna_reloc));
821 reloc.bo = src->bo;
822 reloc.offset = src_lev->offset +
823 blit_info->src.box.z * src_lev->layer_stride;
824 reloc.flags = ETNA_RELOC_READ;
825 etna_set_state_reloc(ctx->stream, VIVS_TS_COLOR_SURFACE_BASE, &reloc);
826
827 etna_set_state(ctx->stream, VIVS_TS_COLOR_CLEAR_VALUE, src_lev->clear_value);
828 etna_set_state(ctx->stream, VIVS_TS_COLOR_CLEAR_VALUE_EXT, src_lev->clear_value >> 32);
829
830 source_ts_valid = true;
831 } else {
832 etna_set_state(ctx->stream, VIVS_TS_MEM_CONFIG, 0);
833 }
834 ctx->dirty |= ETNA_DIRTY_TS;
835
836 /* Kick off RS here */
837 etna_compile_rs_state(ctx, ©_to_screen, &(struct rs_state) {
838 .source_format = format,
839 .source_tiling = src->layout,
840 .source = src->bo,
841 .source_offset = src_offset,
842 .source_stride = src_lev->stride,
843 .source_padded_width = src_lev->padded_width,
844 .source_padded_height = src_lev->padded_height,
845 .source_ts_valid = source_ts_valid,
846 .source_ts_mode = src_lev->ts_mode,
847 .source_ts_compressed = src_lev->ts_compress_fmt >= 0,
848 .dest_format = format,
849 .dest_tiling = dst->layout,
850 .dest = dst->bo,
851 .dest_offset = dst_offset,
852 .dest_stride = dst_lev->stride,
853 .dest_padded_height = dst_lev->padded_height,
854 .downsample_x = downsample_x,
855 .downsample_y = downsample_y,
856 .swap_rb = translate_rb_src_dst_swap(src->base.format, dst->base.format),
857 .dither = {0xffffffff, 0xffffffff}, // XXX dither when going from 24 to 16 bit?
858 .clear_mode = VIVS_RS_CLEAR_CONTROL_MODE_DISABLED,
859 .width = width,
860 .height = height,
861 .tile_count = src_lev->layer_stride /
862 etna_screen_get_tile_size(ctx->screen, src_lev->ts_mode,
863 src->base.nr_samples > 1),
864 });
865
866 etna_submit_rs_state(ctx, ©_to_screen);
867 resource_read(ctx, &src->base);
868 resource_written(ctx, &dst->base);
869 etna_resource_level_mark_changed(dst_lev);
870
871 /* We don't need to mark the TS as invalid if this was just a flush without
872 * compression, as in that case only clear tiles are filled and the tile
873 * status still matches the blit target buffer. For compressed formats the
874 * tiles are decompressed, so tile status doesn't match anymore.
875 */
876 if (src != dst || src_lev->ts_compress_fmt >= 0)
877 etna_resource_level_ts_mark_invalid(dst_lev);
878
879 ctx->dirty |= ETNA_DIRTY_DERIVE_TS;
880
881 return true;
882
883 manual:
884 if (src->layout == ETNA_LAYOUT_TILED && dst->layout == ETNA_LAYOUT_TILED) {
885 if ((etna_resource_status(ctx, src) & ETNA_PENDING_WRITE) ||
886 (etna_resource_status(ctx, dst) & ETNA_PENDING_WRITE))
887 etna_flush(pctx, NULL, 0, true);
888
889 perf_debug_ctx(ctx, "RS blit falls back to sw");
890
891 return etna_manual_blit(dst, dst_lev, dst_offset, src, src_lev, src_offset, blit_info);
892 }
893
894 return false;
895 }
896
897 void
etna_clear_blit_rs_init(struct pipe_context * pctx)898 etna_clear_blit_rs_init(struct pipe_context *pctx)
899 {
900 struct etna_context *ctx = etna_context(pctx);
901
902 DBG("etnaviv: Using RS blit engine");
903 pctx->clear = etna_clear_rs;
904 ctx->blit = etna_try_rs_blit;
905 }
906