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_surface.h"
36 #include "etnaviv_tiling.h"
37 #include "etnaviv_translate.h"
38 #include "etnaviv_util.h"
39
40 #include "pipe/p_defines.h"
41 #include "pipe/p_state.h"
42 #include "util/compiler.h"
43 #include "util/u_blitter.h"
44 #include "util/u_inlines.h"
45 #include "util/u_memory.h"
46 #include "util/u_surface.h"
47
48 #include "hw/common.xml.h"
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, chipMinorFeatures6, 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, chipMinorFeatures4, 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, chipMinorFeatures6, 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, chipMinorFeatures6, 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, chipMinorFeatures7, 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
282 /* Generate clear command for a surface (non-fast clear case) */
283 static void
etna_rs_gen_clear_surface(struct etna_context * ctx,struct etna_surface * surf,uint64_t clear_value)284 etna_rs_gen_clear_surface(struct etna_context *ctx, struct etna_surface *surf,
285 uint64_t clear_value)
286 {
287 ASSERTED struct etna_screen *screen = ctx->screen;
288 struct etna_resource *dst = etna_resource(surf->base.texture);
289 uint32_t format;
290
291 switch (util_format_get_blocksizebits(surf->base.format)) {
292 case 16:
293 format = RS_FORMAT_A4R4G4B4;
294 break;
295 case 32:
296 format = RS_FORMAT_A8R8G8B8;
297 break;
298 case 64:
299 assert(screen->specs.halti >= 2);
300 format = RS_FORMAT_64BPP_CLEAR;
301 break;
302 default:
303 unreachable("bpp not supported for clear by RS");
304 break;
305 }
306
307 /* use tiled clear if width is multiple of 16 */
308 bool tiled_clear = (surf->level->padded_width & ETNA_RS_WIDTH_MASK) == 0 &&
309 (surf->level->padded_height & ETNA_RS_HEIGHT_MASK) == 0;
310
311 etna_compile_rs_state( ctx, &surf->clear_command, &(struct rs_state) {
312 .source_format = format,
313 .dest_format = format,
314 .dest = dst->bo,
315 .dest_offset = surf->offset,
316 .dest_stride = surf->level->stride,
317 .dest_padded_height = surf->level->padded_height,
318 .dest_tiling = tiled_clear ? dst->layout : ETNA_LAYOUT_LINEAR,
319 .dither = {0xffffffff, 0xffffffff},
320 .width = surf->level->padded_width, /* These must be padded to 16x4 if !LINEAR, otherwise RS will hang */
321 .height = surf->level->padded_height,
322 .clear_value = {clear_value, clear_value >> 32, clear_value, clear_value >> 32},
323 .clear_mode = VIVS_RS_CLEAR_CONTROL_MODE_ENABLED1,
324 .clear_bits = 0xffff
325 });
326 }
327
328 static void
etna_blit_clear_color_rs(struct pipe_context * pctx,struct pipe_surface * dst,const union pipe_color_union * color)329 etna_blit_clear_color_rs(struct pipe_context *pctx, struct pipe_surface *dst,
330 const union pipe_color_union *color)
331 {
332 struct etna_context *ctx = etna_context(pctx);
333 struct etna_surface *surf = etna_surface(dst);
334 uint64_t new_clear_value = etna_clear_blit_pack_rgba(surf->base.format, color);
335
336 if (!surf->clear_command.valid)
337 etna_rs_gen_clear_surface(ctx, surf, surf->level->clear_value);
338
339 if (surf->level->ts_size) { /* TS: use precompiled clear command */
340 ctx->framebuffer.TS_COLOR_CLEAR_VALUE = new_clear_value;
341 ctx->framebuffer.TS_COLOR_CLEAR_VALUE_EXT = new_clear_value >> 32;
342
343 if (VIV_FEATURE(ctx->screen, chipMinorFeatures1, AUTO_DISABLE)) {
344 /* Set number of color tiles to be filled */
345 etna_set_state(ctx->stream, VIVS_TS_COLOR_AUTO_DISABLE_COUNT,
346 surf->level->padded_width * surf->level->padded_height / 16);
347 ctx->framebuffer.TS_MEM_CONFIG |= VIVS_TS_MEM_CONFIG_COLOR_AUTO_DISABLE;
348 }
349
350 /* update clear color in SW meta area of the buffer if TS is exported */
351 if (unlikely(new_clear_value != surf->level->clear_value &&
352 etna_resource_ext_ts(etna_resource(dst->texture))))
353 surf->level->ts_meta->v0.clear_value = new_clear_value;
354
355 etna_resource_level_ts_mark_valid(surf->level);
356 ctx->dirty |= ETNA_DIRTY_TS | ETNA_DIRTY_DERIVE_TS;
357 } else if (unlikely(new_clear_value != surf->level->clear_value)) { /* Queue normal RS clear for non-TS surfaces */
358 /* If clear color changed, re-generate stored command */
359 etna_rs_gen_clear_surface(ctx, surf, new_clear_value);
360 }
361
362 etna_submit_rs_state(ctx, &surf->clear_command);
363
364 surf->level->clear_value = new_clear_value;
365 resource_written(ctx, surf->base.texture);
366 etna_resource_level_mark_changed(surf->level);
367 }
368
369 static void
etna_blit_clear_zs_rs(struct pipe_context * pctx,struct pipe_surface * dst,unsigned buffers,double depth,unsigned stencil)370 etna_blit_clear_zs_rs(struct pipe_context *pctx, struct pipe_surface *dst,
371 unsigned buffers, double depth, unsigned stencil)
372 {
373 struct etna_context *ctx = etna_context(pctx);
374 struct etna_surface *surf = etna_surface(dst);
375 uint32_t new_clear_value = translate_clear_depth_stencil(surf->base.format, depth, stencil);
376 uint32_t new_clear_bits = 0, clear_bits_depth, clear_bits_stencil;
377
378 if (!surf->clear_command.valid)
379 etna_rs_gen_clear_surface(ctx, surf, surf->level->clear_value);
380
381 /* Get the channels to clear */
382 switch (surf->base.format) {
383 case PIPE_FORMAT_Z16_UNORM:
384 clear_bits_depth = 0xffff;
385 clear_bits_stencil = 0;
386 break;
387 case PIPE_FORMAT_X8Z24_UNORM:
388 case PIPE_FORMAT_S8_UINT_Z24_UNORM:
389 clear_bits_depth = 0xeeee;
390 clear_bits_stencil = 0x1111;
391 break;
392 default:
393 clear_bits_depth = clear_bits_stencil = 0xffff;
394 break;
395 }
396
397 if (buffers & PIPE_CLEAR_DEPTH)
398 new_clear_bits |= clear_bits_depth;
399 if (buffers & PIPE_CLEAR_STENCIL)
400 new_clear_bits |= clear_bits_stencil;
401 /* FIXME: when tile status is enabled, this becomes more complex as
402 * we may separately clear the depth from the stencil. In this case,
403 * we want to resolve the surface, and avoid using the tile status.
404 * We may be better off recording the pending clear operation,
405 * delaying the actual clear to the first use. This way, we can merge
406 * consecutive clears together. */
407 if (surf->level->ts_size) { /* TS: use precompiled clear command */
408 /* Set new clear depth value */
409 ctx->framebuffer.TS_DEPTH_CLEAR_VALUE = new_clear_value;
410 if (VIV_FEATURE(ctx->screen, chipMinorFeatures1, AUTO_DISABLE)) {
411 /* Set number of depth tiles to be filled */
412 etna_set_state(ctx->stream, VIVS_TS_DEPTH_AUTO_DISABLE_COUNT,
413 surf->level->padded_width * surf->level->padded_height / 16);
414 ctx->framebuffer.TS_MEM_CONFIG |= VIVS_TS_MEM_CONFIG_DEPTH_AUTO_DISABLE;
415 }
416
417 etna_resource_level_ts_mark_valid(surf->level);
418 ctx->dirty |= ETNA_DIRTY_TS | ETNA_DIRTY_DERIVE_TS;
419 } else {
420 if (unlikely(new_clear_value != surf->level->clear_value)) { /* Queue normal RS clear for non-TS surfaces */
421 /* If clear depth value changed, re-generate stored command */
422 etna_rs_gen_clear_surface(ctx, surf, new_clear_value);
423 }
424 /* Update the channels to be cleared */
425 etna_modify_rs_clearbits(&surf->clear_command, new_clear_bits);
426 }
427
428 etna_submit_rs_state(ctx, &surf->clear_command);
429
430 surf->level->clear_value = new_clear_value;
431 resource_written(ctx, surf->base.texture);
432 etna_resource_level_mark_changed(surf->level);
433 }
434
435 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)436 etna_clear_rs(struct pipe_context *pctx, unsigned buffers, const struct pipe_scissor_state *scissor_state,
437 const union pipe_color_union *color, double depth, unsigned stencil)
438 {
439 struct etna_context *ctx = etna_context(pctx);
440
441 if (!etna_render_condition_check(pctx))
442 return;
443
444 /* Flush color and depth cache before clearing anything.
445 * This is especially important when coming from another surface, as
446 * otherwise it may clear part of the old surface instead. */
447 etna_set_state(ctx->stream, VIVS_GL_FLUSH_CACHE, VIVS_GL_FLUSH_CACHE_COLOR | VIVS_GL_FLUSH_CACHE_DEPTH);
448 etna_stall(ctx->stream, SYNC_RECIPIENT_RA, SYNC_RECIPIENT_PE);
449
450 /* Preparation: Flush the TS if needed. This must be done after flushing
451 * color and depth, otherwise it can result in crashes */
452 bool need_ts_flush = false;
453 if ((buffers & PIPE_CLEAR_COLOR) && ctx->framebuffer_s.nr_cbufs) {
454 struct etna_surface *surf = etna_surface(ctx->framebuffer_s.cbufs[0]);
455
456 if (surf->level->ts_size)
457 need_ts_flush = true;
458 }
459 if ((buffers & PIPE_CLEAR_DEPTHSTENCIL) && ctx->framebuffer_s.zsbuf != NULL) {
460 struct etna_surface *surf = etna_surface(ctx->framebuffer_s.zsbuf);
461
462 if (surf->level->ts_size)
463 need_ts_flush = true;
464 }
465
466 if (need_ts_flush)
467 etna_set_state(ctx->stream, VIVS_TS_FLUSH_CACHE, VIVS_TS_FLUSH_CACHE_FLUSH);
468
469 /* No need to set up the TS here as RS clear operations (in contrast to
470 * resolve and copy) do not require the TS state.
471 */
472 if (buffers & PIPE_CLEAR_COLOR) {
473 for (int idx = 0; idx < ctx->framebuffer_s.nr_cbufs; ++idx) {
474 struct etna_surface *surf = etna_surface(ctx->framebuffer_s.cbufs[idx]);
475
476 etna_blit_clear_color_rs(pctx, ctx->framebuffer_s.cbufs[idx],
477 &color[idx]);
478
479 if (!etna_resource(surf->prsc)->explicit_flush)
480 etna_context_add_flush_resource(ctx, surf->prsc);
481 }
482 }
483
484 /* Flush the color and depth caches before each RS clear operation
485 * This fixes a hang on GC600. */
486 if (buffers & PIPE_CLEAR_DEPTHSTENCIL && buffers & PIPE_CLEAR_COLOR)
487 etna_set_state(ctx->stream, VIVS_GL_FLUSH_CACHE,
488 VIVS_GL_FLUSH_CACHE_COLOR | VIVS_GL_FLUSH_CACHE_DEPTH);
489
490 if ((buffers & PIPE_CLEAR_DEPTHSTENCIL) && ctx->framebuffer_s.zsbuf != NULL)
491 etna_blit_clear_zs_rs(pctx, ctx->framebuffer_s.zsbuf, buffers, depth, stencil);
492
493 etna_stall(ctx->stream, SYNC_RECIPIENT_RA, SYNC_RECIPIENT_PE);
494 }
495
496 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)497 etna_manual_blit(struct etna_resource *dst, struct etna_resource_level *dst_lev,
498 unsigned int dst_offset, struct etna_resource *src,
499 struct etna_resource_level *src_lev, unsigned int src_offset,
500 const struct pipe_blit_info *blit_info)
501 {
502 void *smap, *srow, *dmap, *drow;
503 size_t tile_size;
504
505 assert(src->layout == ETNA_LAYOUT_TILED);
506 assert(dst->layout == ETNA_LAYOUT_TILED);
507 assert(src->base.nr_samples == 0);
508 assert(dst->base.nr_samples == 0);
509
510 tile_size = util_format_get_blocksize(blit_info->src.format) * 4 * 4;
511
512 smap = etna_bo_map(src->bo);
513 if (!smap)
514 return false;
515
516 dmap = etna_bo_map(dst->bo);
517 if (!dmap)
518 return false;
519
520 srow = smap + src_offset;
521 drow = dmap + dst_offset;
522
523 etna_bo_cpu_prep(src->bo, DRM_ETNA_PREP_READ);
524 etna_bo_cpu_prep(dst->bo, DRM_ETNA_PREP_WRITE);
525
526 for (int y = 0; y < blit_info->src.box.height; y += 4) {
527 memcpy(drow, srow, tile_size * blit_info->src.box.width);
528 srow += src_lev->stride * 4;
529 drow += dst_lev->stride * 4;
530 }
531
532 etna_bo_cpu_fini(dst->bo);
533 etna_bo_cpu_fini(src->bo);
534
535 return true;
536 }
537
538 static inline size_t
etna_compute_tileoffset(const struct pipe_box * box,enum pipe_format format,size_t stride,enum etna_surface_layout layout)539 etna_compute_tileoffset(const struct pipe_box *box, enum pipe_format format,
540 size_t stride, enum etna_surface_layout layout)
541 {
542 size_t offset;
543 unsigned int x = box->x, y = box->y;
544 unsigned int blocksize = util_format_get_blocksize(format);
545
546 switch (layout) {
547 case ETNA_LAYOUT_LINEAR:
548 offset = y * stride + x * blocksize;
549 break;
550 case ETNA_LAYOUT_MULTI_TILED:
551 y >>= 1;
552 FALLTHROUGH;
553 case ETNA_LAYOUT_TILED:
554 assert(!(x & 0x03) && !(y & 0x03));
555 offset = (y & ~0x03) * stride + blocksize * ((x & ~0x03) << 2);
556 break;
557 case ETNA_LAYOUT_MULTI_SUPERTILED:
558 y >>= 1;
559 FALLTHROUGH;
560 case ETNA_LAYOUT_SUPER_TILED:
561 assert(!(x & 0x3f) && !(y & 0x3f));
562 offset = (y & ~0x3f) * stride + blocksize * ((x & ~0x3f) << 6);
563 break;
564 default:
565 unreachable("invalid resource layout");
566 }
567
568 return offset;
569 }
570
571 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)572 etna_get_rs_alignment_mask(const struct etna_context *ctx,
573 const enum etna_surface_layout layout,
574 unsigned int *width_mask, unsigned int *height_mask)
575 {
576 struct etna_screen *screen = ctx->screen;
577 unsigned int h_align, w_align;
578
579 if (layout & ETNA_LAYOUT_BIT_SUPER) {
580 w_align = 64;
581 h_align = 64 * screen->specs.pixel_pipes;
582 } else {
583 w_align = ETNA_RS_WIDTH_MASK + 1;
584 h_align = ETNA_RS_HEIGHT_MASK + 1;
585 }
586
587 *width_mask = w_align - 1;
588 *height_mask = h_align -1;
589 }
590
591 static bool
etna_try_rs_blit(struct pipe_context * pctx,const struct pipe_blit_info * blit_info)592 etna_try_rs_blit(struct pipe_context *pctx,
593 const struct pipe_blit_info *blit_info)
594 {
595 struct etna_context *ctx = etna_context(pctx);
596 struct etna_resource *src = etna_resource(blit_info->src.resource);
597 struct etna_resource *dst = etna_resource(blit_info->dst.resource);
598 struct compiled_rs_state copy_to_screen;
599 int src_xscale, src_yscale, dst_xscale, dst_yscale;
600 bool downsample_x = false, downsample_y = false;
601
602 /* Ensure that the level is valid */
603 assert(blit_info->src.level <= src->base.last_level);
604 assert(blit_info->dst.level <= dst->base.last_level);
605
606 if (!translate_samples_to_xyscale(src->base.nr_samples, &src_xscale, &src_yscale))
607 return false;
608 if (!translate_samples_to_xyscale(dst->base.nr_samples, &dst_xscale, &dst_yscale))
609 return false;
610
611 /* RS does not support upscaling */
612 if ((src_xscale < dst_xscale) || (src_yscale < dst_yscale))
613 return false;
614
615 if (src_xscale > dst_xscale)
616 downsample_x = true;
617 if (src_yscale > dst_yscale)
618 downsample_y = true;
619
620 /* The width/height are in pixels; they do not change as a result of
621 * multi-sampling. So, when blitting from a 4x multisampled surface
622 * to a non-multisampled surface, the width and height will be
623 * identical. As we do not support scaling, reject different sizes. */
624 if (blit_info->dst.box.width != blit_info->src.box.width ||
625 blit_info->dst.box.height != blit_info->src.box.height) {
626 DBG("scaling requested: source %dx%d destination %dx%d",
627 blit_info->src.box.width, blit_info->src.box.height,
628 blit_info->dst.box.width, blit_info->dst.box.height);
629 return false;
630 }
631
632 /* No masks - RS can't copy specific channels */
633 unsigned mask = util_format_get_mask(blit_info->dst.format);
634 if ((blit_info->mask & mask) != mask) {
635 DBG("sub-mask requested: 0x%02x vs format mask 0x%02x", blit_info->mask, mask);
636 return false;
637 }
638
639 /* Only support same format (used tiling/detiling) blits for now.
640 * TODO: figure out which different-format blits are possible and test them
641 * - fail if swizzle needed
642 * - avoid trying to convert between float/int formats?
643 */
644 if (blit_info->src.format != blit_info->dst.format)
645 return false;
646
647 /* try to find a exact format match first */
648 uint32_t format = translate_rs_format(blit_info->dst.format);
649 /* When not resolving MSAA, but only doing a layout conversion, we can get
650 * away with a fallback format of matching size.
651 */
652 if (format == ETNA_NO_MATCH && !downsample_x && !downsample_y)
653 format = etna_compatible_rs_format(blit_info->dst.format);
654 if (format == ETNA_NO_MATCH)
655 return false;
656
657 if (blit_info->scissor_enable ||
658 blit_info->dst.box.depth != blit_info->src.box.depth ||
659 blit_info->dst.box.depth != 1) {
660 return false;
661 }
662
663 unsigned w_mask, h_mask;
664
665 etna_get_rs_alignment_mask(ctx, src->layout, &w_mask, &h_mask);
666 if ((blit_info->src.box.x & w_mask) || (blit_info->src.box.y & h_mask))
667 return false;
668
669 etna_get_rs_alignment_mask(ctx, dst->layout, &w_mask, &h_mask);
670 if ((blit_info->dst.box.x & w_mask) || (blit_info->dst.box.y & h_mask))
671 return false;
672
673 struct etna_resource_level *src_lev = &src->levels[blit_info->src.level];
674 struct etna_resource_level *dst_lev = &dst->levels[blit_info->dst.level];
675
676 /* we may be given coordinates up to the padded width to avoid
677 * any alignment issues with different tiling formats */
678 assert((blit_info->src.box.x + blit_info->src.box.width) * src_xscale <= src_lev->padded_width);
679 assert((blit_info->src.box.y + blit_info->src.box.height) * src_yscale <= src_lev->padded_height);
680 assert(blit_info->dst.box.x + blit_info->dst.box.width <= dst_lev->padded_width);
681 assert(blit_info->dst.box.y + blit_info->dst.box.height <= dst_lev->padded_height);
682
683 unsigned src_offset = src_lev->offset +
684 blit_info->src.box.z * src_lev->layer_stride +
685 etna_compute_tileoffset(&blit_info->src.box,
686 blit_info->src.format,
687 src_lev->stride,
688 src->layout);
689 unsigned dst_offset = dst_lev->offset +
690 blit_info->dst.box.z * dst_lev->layer_stride +
691 etna_compute_tileoffset(&blit_info->dst.box,
692 blit_info->dst.format,
693 dst_lev->stride,
694 dst->layout);
695
696 if (src_lev->padded_width <= ETNA_RS_WIDTH_MASK ||
697 dst_lev->padded_width <= ETNA_RS_WIDTH_MASK ||
698 src_lev->padded_height <= ETNA_RS_HEIGHT_MASK ||
699 dst_lev->padded_height <= ETNA_RS_HEIGHT_MASK)
700 goto manual;
701
702 /* If the width is not aligned to the RS width, but is within our
703 * padding, adjust the width to suite the RS width restriction.
704 * Note: the RS width/height are converted to source samples here. */
705 unsigned int width = blit_info->src.box.width * src_xscale;
706 unsigned int height = blit_info->src.box.height * src_yscale;
707 unsigned int w_align = (ETNA_RS_WIDTH_MASK + 1) * src_xscale;
708 unsigned int h_align = (ETNA_RS_HEIGHT_MASK + 1) * src_yscale;
709
710 if (width & (w_align - 1) && width >= src_lev->width * src_xscale && width >= dst_lev->width)
711 width = align(width, w_align);
712
713 if (height & (h_align - 1) && height >= src_lev->height * src_yscale && height >= dst_lev->height) {
714 if (!ctx->screen->specs.single_buffer &&
715 align(height, h_align * ctx->screen->specs.pixel_pipes) <=
716 dst_lev->padded_height * src_yscale)
717 height = align(height, h_align * ctx->screen->specs.pixel_pipes);
718 else
719 height = align(height, h_align);
720 }
721
722 /* The padded dimensions are in samples */
723 if (width > src_lev->padded_width ||
724 width > dst_lev->padded_width * src_xscale ||
725 height > src_lev->padded_height ||
726 height > dst_lev->padded_height * src_yscale ||
727 width & (w_align - 1) || height & (h_align - 1))
728 goto manual;
729
730 /* Flush destination, as the blit will invalidate any pending TS changes. */
731 if (dst != src && etna_resource_level_needs_flush(dst_lev))
732 etna_copy_resource(pctx, &dst->base, &dst->base,
733 blit_info->dst.level, blit_info->dst.level);
734
735 /* Always flush color and depth cache together before resolving. This makes
736 * sure that all previous cache content written by the PE is flushed out
737 * before RS uses the pixel pipes, which invalidates those caches. */
738 etna_set_state(ctx->stream, VIVS_GL_FLUSH_CACHE,
739 VIVS_GL_FLUSH_CACHE_COLOR | VIVS_GL_FLUSH_CACHE_DEPTH);
740 etna_stall(ctx->stream, SYNC_RECIPIENT_RA, SYNC_RECIPIENT_PE);
741
742 /* Set up color TS to source surface before blit, if needed */
743 bool source_ts_valid = false;
744 if (etna_resource_level_ts_valid(src_lev)) {
745 struct etna_reloc reloc;
746 unsigned ts_offset =
747 src_lev->ts_offset + blit_info->src.box.z * src_lev->ts_layer_stride;
748 uint32_t ts_mem_config = 0;
749
750 /* flush TS cache before changing to another TS configuration */
751 etna_set_state(ctx->stream, VIVS_TS_FLUSH_CACHE, VIVS_TS_FLUSH_CACHE_FLUSH);
752
753 if (src_lev->ts_compress_fmt >= 0) {
754 ts_mem_config |= VIVS_TS_MEM_CONFIG_COLOR_COMPRESSION |
755 VIVS_TS_MEM_CONFIG_COLOR_COMPRESSION_FORMAT(src_lev->ts_compress_fmt);
756 }
757
758 etna_set_state(ctx->stream, VIVS_TS_MEM_CONFIG,
759 VIVS_TS_MEM_CONFIG_COLOR_FAST_CLEAR | ts_mem_config);
760
761 memset(&reloc, 0, sizeof(struct etna_reloc));
762 reloc.bo = src->ts_bo;
763 reloc.offset = ts_offset;
764 reloc.flags = ETNA_RELOC_READ;
765 etna_set_state_reloc(ctx->stream, VIVS_TS_COLOR_STATUS_BASE, &reloc);
766
767 memset(&reloc, 0, sizeof(struct etna_reloc));
768 reloc.bo = src->bo;
769 reloc.offset = src_lev->offset +
770 blit_info->src.box.z * src_lev->layer_stride;
771 reloc.flags = ETNA_RELOC_READ;
772 etna_set_state_reloc(ctx->stream, VIVS_TS_COLOR_SURFACE_BASE, &reloc);
773
774 etna_set_state(ctx->stream, VIVS_TS_COLOR_CLEAR_VALUE, src_lev->clear_value);
775 etna_set_state(ctx->stream, VIVS_TS_COLOR_CLEAR_VALUE_EXT, src_lev->clear_value >> 32);
776
777 source_ts_valid = true;
778 } else {
779 etna_set_state(ctx->stream, VIVS_TS_MEM_CONFIG, 0);
780 }
781 ctx->dirty |= ETNA_DIRTY_TS;
782
783 /* Kick off RS here */
784 etna_compile_rs_state(ctx, ©_to_screen, &(struct rs_state) {
785 .source_format = format,
786 .source_tiling = src->layout,
787 .source = src->bo,
788 .source_offset = src_offset,
789 .source_stride = src_lev->stride,
790 .source_padded_width = src_lev->padded_width,
791 .source_padded_height = src_lev->padded_height,
792 .source_ts_valid = source_ts_valid,
793 .source_ts_mode = src_lev->ts_mode,
794 .source_ts_compressed = src_lev->ts_compress_fmt >= 0,
795 .dest_format = format,
796 .dest_tiling = dst->layout,
797 .dest = dst->bo,
798 .dest_offset = dst_offset,
799 .dest_stride = dst_lev->stride,
800 .dest_padded_height = dst_lev->padded_height,
801 .downsample_x = downsample_x,
802 .downsample_y = downsample_y,
803 .swap_rb = translate_rb_src_dst_swap(src->base.format, dst->base.format),
804 .dither = {0xffffffff, 0xffffffff}, // XXX dither when going from 24 to 16 bit?
805 .clear_mode = VIVS_RS_CLEAR_CONTROL_MODE_DISABLED,
806 .width = width,
807 .height = height,
808 .tile_count = src_lev->layer_stride /
809 etna_screen_get_tile_size(ctx->screen, src_lev->ts_mode,
810 src->base.nr_samples > 1),
811 });
812
813 etna_submit_rs_state(ctx, ©_to_screen);
814 resource_read(ctx, &src->base);
815 resource_written(ctx, &dst->base);
816 etna_resource_level_mark_changed(dst_lev);
817
818 /* We don't need to mark the TS as invalid if this was just a flush without
819 * compression, as in that case only clear tiles are filled and the tile
820 * status still matches the blit target buffer. For compressed formats the
821 * tiles are decompressed, so tile status doesn't match anymore.
822 */
823 if (src != dst || src_lev->ts_compress_fmt >= 0)
824 etna_resource_level_ts_mark_invalid(dst_lev);
825
826 ctx->dirty |= ETNA_DIRTY_DERIVE_TS;
827
828 return true;
829
830 manual:
831 if (src->layout == ETNA_LAYOUT_TILED && dst->layout == ETNA_LAYOUT_TILED) {
832 if ((etna_resource_status(ctx, src) & ETNA_PENDING_WRITE) ||
833 (etna_resource_status(ctx, dst) & ETNA_PENDING_WRITE))
834 etna_flush(pctx, NULL, 0, true);
835
836 perf_debug_ctx(ctx, "RS blit falls back to sw");
837
838 return etna_manual_blit(dst, dst_lev, dst_offset, src, src_lev, src_offset, blit_info);
839 }
840
841 return false;
842 }
843
844 void
etna_clear_blit_rs_init(struct pipe_context * pctx)845 etna_clear_blit_rs_init(struct pipe_context *pctx)
846 {
847 struct etna_context *ctx = etna_context(pctx);
848
849 DBG("etnaviv: Using RS blit engine");
850 pctx->clear = etna_clear_rs;
851 ctx->blit = etna_try_rs_blit;
852 }
853