freedreno: rework blit API
[mesa.git] / src / gallium / drivers / freedreno / a6xx / fd6_blitter.c
1 /*
2 * Copyright (C) 2017 Rob Clark <robclark@freedesktop.org>
3 * Copyright © 2018 Google, Inc.
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
14 * Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 *
24 * Authors:
25 * Rob Clark <robclark@freedesktop.org>
26 */
27
28 #include "util/u_dump.h"
29
30 #include "freedreno_blitter.h"
31 #include "freedreno_fence.h"
32 #include "freedreno_resource.h"
33
34 #include "fd6_blitter.h"
35 #include "fd6_format.h"
36 #include "fd6_emit.h"
37
38 /* Make sure none of the requested dimensions extend beyond the size of the
39 * resource. Not entirely sure why this happens, but sometimes it does, and
40 * w/ 2d blt doesn't have wrap modes like a sampler, so force those cases
41 * back to u_blitter
42 */
43 static bool
44 ok_dims(const struct pipe_resource *r, const struct pipe_box *b, int lvl)
45 {
46 int last_layer =
47 r->target == PIPE_TEXTURE_3D ? u_minify(r->depth0, lvl)
48 : r->array_size;
49
50 return (b->x >= 0) && (b->x + b->width <= u_minify(r->width0, lvl)) &&
51 (b->y >= 0) && (b->y + b->height <= u_minify(r->height0, lvl)) &&
52 (b->z >= 0) && (b->z + b->depth <= last_layer);
53 }
54
55 static bool
56 ok_format(enum pipe_format pfmt)
57 {
58 enum a6xx_color_fmt fmt = fd6_pipe2color(pfmt);
59 if (fmt == ~0)
60 return false;
61
62 if (fd6_ifmt(fmt) == 0)
63 return false;
64
65 return true;
66 }
67
68 #define DEBUG_BLIT_FALLBACK 0
69 #define fail_if(cond) \
70 do { \
71 if (cond) { \
72 if (DEBUG_BLIT_FALLBACK) { \
73 fprintf(stderr, "falling back: %s for blit:\n", #cond); \
74 util_dump_blit_info(stderr, info); \
75 fprintf(stderr, "\nsrc: "); \
76 util_dump_resource(stderr, info->src.resource); \
77 fprintf(stderr, "\ndst: "); \
78 util_dump_resource(stderr, info->dst.resource); \
79 fprintf(stderr, "\n"); \
80 } \
81 return false; \
82 } \
83 } while (0)
84
85 static bool
86 can_do_blit(const struct pipe_blit_info *info)
87 {
88 /* I think we can do scaling, but not in z dimension since that would
89 * require blending..
90 */
91 fail_if(info->dst.box.depth != info->src.box.depth);
92
93 /* We can blit if both or neither formats are compressed formats... */
94 fail_if(util_format_is_compressed(info->src.format) !=
95 util_format_is_compressed(info->src.format));
96
97 /* Fail if unsupported format: */
98 fail_if(!ok_format(info->src.format));
99 fail_if(!ok_format(info->dst.format));
100
101 /* ... but only if they're the same compression format. */
102 fail_if(util_format_is_compressed(info->src.format) &&
103 info->src.format != info->dst.format);
104
105 /* hw ignores {SRC,DST}_INFO.COLOR_SWAP if {SRC,DST}_INFO.TILE_MODE
106 * is set (not linear). We can kind of get around that when tiling/
107 * untiling by setting both src and dst COLOR_SWAP=WZYX, but that
108 * means the formats must match:
109 */
110 fail_if((fd_resource(info->dst.resource)->tile_mode ||
111 fd_resource(info->src.resource)->tile_mode) &&
112 info->dst.format != info->src.format);
113
114 /* src box can be inverted, which we don't support.. dst box cannot: */
115 fail_if((info->src.box.width < 0) || (info->src.box.height < 0));
116
117 fail_if(!ok_dims(info->src.resource, &info->src.box, info->src.level));
118
119 fail_if(!ok_dims(info->dst.resource, &info->dst.box, info->dst.level));
120
121 debug_assert(info->dst.box.width >= 0);
122 debug_assert(info->dst.box.height >= 0);
123 debug_assert(info->dst.box.depth >= 0);
124
125 fail_if(info->dst.resource->nr_samples + info->src.resource->nr_samples > 2);
126
127 fail_if(info->window_rectangle_include);
128
129 fail_if(info->render_condition_enable);
130
131 fail_if(info->alpha_blend);
132
133 fail_if(info->mask != util_format_get_mask(info->src.format));
134
135 fail_if(info->mask != util_format_get_mask(info->dst.format));
136
137 return true;
138 }
139
140 static void
141 emit_setup(struct fd_ringbuffer *ring)
142 {
143 OUT_PKT7(ring, CP_EVENT_WRITE, 1);
144 OUT_RING(ring, PC_CCU_INVALIDATE_COLOR);
145
146 OUT_PKT7(ring, CP_EVENT_WRITE, 1);
147 OUT_RING(ring, LRZ_FLUSH);
148
149 OUT_PKT7(ring, CP_SKIP_IB2_ENABLE_GLOBAL, 1);
150 OUT_RING(ring, 0x0);
151
152 OUT_WFI5(ring);
153
154 OUT_PKT4(ring, REG_A6XX_RB_CCU_CNTL, 1);
155 OUT_RING(ring, 0x10000000);
156 }
157
158 static uint32_t
159 blit_control(enum a6xx_color_fmt fmt)
160 {
161 unsigned blit_cntl = 0xf00000;
162 blit_cntl |= A6XX_RB_2D_BLIT_CNTL_COLOR_FORMAT(fmt);
163 blit_cntl |= A6XX_RB_2D_BLIT_CNTL_IFMT(fd6_ifmt(fmt));
164 return blit_cntl;
165 }
166
167 /* buffers need to be handled specially since x/width can exceed the bounds
168 * supported by hw.. if necessary decompose into (potentially) two 2D blits
169 */
170 static void
171 emit_blit_buffer(struct fd_ringbuffer *ring, const struct pipe_blit_info *info)
172 {
173 const struct pipe_box *sbox = &info->src.box;
174 const struct pipe_box *dbox = &info->dst.box;
175 struct fd_resource *src, *dst;
176 unsigned sshift, dshift;
177
178 if (DEBUG_BLIT_FALLBACK) {
179 fprintf(stderr, "buffer blit: ");
180 util_dump_blit_info(stderr, info);
181 fprintf(stderr, "\ndst resource: ");
182 util_dump_resource(stderr, info->dst.resource);
183 fprintf(stderr, "\nsrc resource: ");
184 util_dump_resource(stderr, info->src.resource);
185 fprintf(stderr, "\n");
186 }
187
188 src = fd_resource(info->src.resource);
189 dst = fd_resource(info->dst.resource);
190
191 debug_assert(src->cpp == 1);
192 debug_assert(dst->cpp == 1);
193 debug_assert(info->src.resource->format == info->dst.resource->format);
194 debug_assert((sbox->y == 0) && (sbox->height == 1));
195 debug_assert((dbox->y == 0) && (dbox->height == 1));
196 debug_assert((sbox->z == 0) && (sbox->depth == 1));
197 debug_assert((dbox->z == 0) && (dbox->depth == 1));
198 debug_assert(sbox->width == dbox->width);
199 debug_assert(info->src.level == 0);
200 debug_assert(info->dst.level == 0);
201
202 /*
203 * Buffers can have dimensions bigger than max width, remap into
204 * multiple 1d blits to fit within max dimension
205 *
206 * Note that blob uses .ARRAY_PITCH=128 for blitting buffers, which
207 * seems to prevent overfetch related faults. Not quite sure what
208 * the deal is there.
209 *
210 * Low 6 bits of SRC/DST addresses need to be zero (ie. address
211 * aligned to 64) so we need to shift src/dst x1/x2 to make up the
212 * difference. On top of already splitting up the blit so width
213 * isn't > 16k.
214 *
215 * We perhaps could do a bit better, if src and dst are aligned but
216 * in the worst case this means we have to split the copy up into
217 * 16k (0x4000) minus 64 (0x40).
218 */
219
220 sshift = sbox->x & 0x3f;
221 dshift = dbox->x & 0x3f;
222
223 OUT_PKT7(ring, CP_SET_MARKER, 1);
224 OUT_RING(ring, A2XX_CP_SET_MARKER_0_MODE(RM6_BLIT2DSCALE));
225
226 uint32_t blit_cntl = blit_control(RB6_R8_UNORM) | 0x20000000;
227 OUT_PKT4(ring, REG_A6XX_RB_2D_BLIT_CNTL, 1);
228 OUT_RING(ring, blit_cntl);
229
230 OUT_PKT4(ring, REG_A6XX_GRAS_2D_BLIT_CNTL, 1);
231 OUT_RING(ring, blit_cntl);
232
233 for (unsigned off = 0; off < sbox->width; off += (0x4000 - 0x40)) {
234 unsigned soff, doff, w, p;
235
236 soff = (sbox->x + off) & ~0x3f;
237 doff = (dbox->x + off) & ~0x3f;
238
239 w = MIN2(sbox->width - off, (0x4000 - 0x40));
240 p = align(w, 64);
241
242 debug_assert((soff + w) <= fd_bo_size(src->bo));
243 debug_assert((doff + w) <= fd_bo_size(dst->bo));
244
245 /*
246 * Emit source:
247 */
248 OUT_PKT4(ring, REG_A6XX_SP_PS_2D_SRC_INFO, 13);
249 OUT_RING(ring, A6XX_SP_PS_2D_SRC_INFO_COLOR_FORMAT(RB6_R8_UNORM) |
250 A6XX_SP_PS_2D_SRC_INFO_TILE_MODE(TILE6_LINEAR) |
251 A6XX_SP_PS_2D_SRC_INFO_COLOR_SWAP(WZYX) | 0x500000);
252 OUT_RING(ring, A6XX_SP_PS_2D_SRC_SIZE_WIDTH(sshift + w) |
253 A6XX_SP_PS_2D_SRC_SIZE_HEIGHT(1)); /* SP_PS_2D_SRC_SIZE */
254 OUT_RELOC(ring, src->bo, soff, 0, 0); /* SP_PS_2D_SRC_LO/HI */
255 OUT_RING(ring, A6XX_SP_PS_2D_SRC_PITCH_PITCH(p));
256
257 OUT_RING(ring, 0x00000000);
258 OUT_RING(ring, 0x00000000);
259 OUT_RING(ring, 0x00000000);
260 OUT_RING(ring, 0x00000000);
261 OUT_RING(ring, 0x00000000);
262
263 OUT_RING(ring, 0x00000000);
264 OUT_RING(ring, 0x00000000);
265 OUT_RING(ring, 0x00000000);
266
267 /*
268 * Emit destination:
269 */
270 OUT_PKT4(ring, REG_A6XX_RB_2D_DST_INFO, 9);
271 OUT_RING(ring, A6XX_RB_2D_DST_INFO_COLOR_FORMAT(RB6_R8_UNORM) |
272 A6XX_RB_2D_DST_INFO_TILE_MODE(TILE6_LINEAR) |
273 A6XX_RB_2D_DST_INFO_COLOR_SWAP(WZYX));
274 OUT_RELOC(ring, dst->bo, doff, 0, 0); /* RB_2D_DST_LO/HI */
275 OUT_RING(ring, A6XX_RB_2D_DST_SIZE_PITCH(p));
276 OUT_RING(ring, 0x00000000);
277 OUT_RING(ring, 0x00000000);
278 OUT_RING(ring, 0x00000000);
279 OUT_RING(ring, 0x00000000);
280 OUT_RING(ring, 0x00000000);
281
282 /*
283 * Blit command:
284 */
285 OUT_PKT4(ring, REG_A6XX_GRAS_2D_SRC_TL_X, 4);
286 OUT_RING(ring, A6XX_GRAS_2D_SRC_TL_X_X(sshift));
287 OUT_RING(ring, A6XX_GRAS_2D_SRC_BR_X_X(sshift + w - 1));
288 OUT_RING(ring, A6XX_GRAS_2D_SRC_TL_Y_Y(0));
289 OUT_RING(ring, A6XX_GRAS_2D_SRC_BR_Y_Y(0));
290
291 OUT_PKT4(ring, REG_A6XX_GRAS_2D_DST_TL, 2);
292 OUT_RING(ring, A6XX_GRAS_2D_DST_TL_X(dshift) | A6XX_GRAS_2D_DST_TL_Y(0));
293 OUT_RING(ring, A6XX_GRAS_2D_DST_BR_X(dshift + w - 1) | A6XX_GRAS_2D_DST_BR_Y(0));
294
295 OUT_PKT7(ring, CP_EVENT_WRITE, 1);
296 OUT_RING(ring, 0x3f);
297 OUT_WFI5(ring);
298
299 OUT_PKT4(ring, REG_A6XX_RB_UNKNOWN_8C01, 1);
300 OUT_RING(ring, 0);
301
302 OUT_PKT4(ring, REG_A6XX_SP_2D_SRC_FORMAT, 1);
303 OUT_RING(ring, 0xf180);
304
305 OUT_PKT4(ring, REG_A6XX_RB_UNKNOWN_8E04, 1);
306 OUT_RING(ring, 0x01000000);
307
308 OUT_PKT7(ring, CP_BLIT, 1);
309 OUT_RING(ring, CP_BLIT_0_OP(BLIT_OP_SCALE));
310
311 OUT_WFI5(ring);
312
313 OUT_PKT4(ring, REG_A6XX_RB_UNKNOWN_8E04, 1);
314 OUT_RING(ring, 0);
315 }
316 }
317
318 static void
319 emit_blit_texture(struct fd_ringbuffer *ring, const struct pipe_blit_info *info)
320 {
321 const struct pipe_box *sbox = &info->src.box;
322 const struct pipe_box *dbox = &info->dst.box;
323 struct fd_resource *src, *dst;
324 struct fd_resource_slice *sslice, *dslice;
325 enum a6xx_color_fmt sfmt, dfmt;
326 enum a6xx_tile_mode stile, dtile;
327 enum a3xx_color_swap sswap, dswap;
328 unsigned spitch, dpitch;
329 unsigned sx1, sy1, sx2, sy2;
330 unsigned dx1, dy1, dx2, dy2;
331
332 if (DEBUG_BLIT_FALLBACK) {
333 fprintf(stderr, "texture blit: ");
334 util_dump_blit_info(stderr, info);
335 fprintf(stderr, "\ndst resource: ");
336 util_dump_resource(stderr, info->dst.resource);
337 fprintf(stderr, "\nsrc resource: ");
338 util_dump_resource(stderr, info->src.resource);
339 fprintf(stderr, "\n");
340 }
341
342 src = fd_resource(info->src.resource);
343 dst = fd_resource(info->dst.resource);
344
345 sslice = fd_resource_slice(src, info->src.level);
346 dslice = fd_resource_slice(dst, info->dst.level);
347
348 sfmt = fd6_pipe2color(info->src.format);
349 dfmt = fd6_pipe2color(info->dst.format);
350
351 int blocksize = util_format_get_blocksize(info->src.format);
352 int blockwidth = util_format_get_blockwidth(info->src.format);
353 int blockheight = util_format_get_blockheight(info->src.format);
354 int nelements;
355
356 stile = fd_resource_level_linear(info->src.resource, info->src.level) ?
357 TILE6_LINEAR : src->tile_mode;
358 dtile = fd_resource_level_linear(info->dst.resource, info->dst.level) ?
359 TILE6_LINEAR : dst->tile_mode;
360
361 sswap = fd6_pipe2swap(info->src.format);
362 dswap = fd6_pipe2swap(info->dst.format);
363
364 if (util_format_is_compressed(info->src.format)) {
365 debug_assert(info->src.format == info->dst.format);
366 sfmt = dfmt = RB6_R8_UNORM;
367 nelements = blocksize;
368 } else {
369 debug_assert(!util_format_is_compressed(info->dst.format));
370 nelements = 1;
371 }
372
373 spitch = DIV_ROUND_UP(sslice->pitch, blockwidth) * src->cpp;
374 dpitch = DIV_ROUND_UP(dslice->pitch, blockwidth) * dst->cpp;
375
376 sx1 = sbox->x / blockwidth * nelements;
377 sy1 = sbox->y / blockheight;
378 sx2 = DIV_ROUND_UP(sbox->x + sbox->width, blockwidth) * nelements - 1;
379 sy2 = DIV_ROUND_UP(sbox->y + sbox->height, blockheight) - 1;
380
381 dx1 = dbox->x / blockwidth * nelements;
382 dy1 = dbox->y / blockheight;
383 dx2 = DIV_ROUND_UP(dbox->x + dbox->width, blockwidth) * nelements - 1;
384 dy2 = DIV_ROUND_UP(dbox->y + dbox->height, blockheight) - 1;
385
386 uint32_t width = DIV_ROUND_UP(u_minify(src->base.width0, info->src.level), blockwidth) * nelements;
387 uint32_t height = DIV_ROUND_UP(u_minify(src->base.height0, info->src.level), blockheight);
388
389 /* if dtile, then dswap ignored by hw, and likewise if stile then sswap
390 * ignored by hw.. but in this case we have already rejected the blit
391 * if src and dst formats differ, so juse use WZYX for both src and
392 * dst swap mode (so we don't change component order)
393 */
394 if (stile || dtile) {
395 debug_assert(info->src.format == info->dst.format);
396 sswap = dswap = WZYX;
397 }
398
399 OUT_PKT7(ring, CP_SET_MARKER, 1);
400 OUT_RING(ring, A2XX_CP_SET_MARKER_0_MODE(RM6_BLIT2DSCALE));
401
402 uint32_t blit_cntl = blit_control(dfmt);
403
404 if (dtile != stile)
405 blit_cntl |= 0x20000000;
406
407 if (info->scissor_enable) {
408 OUT_PKT4(ring, REG_A6XX_GRAS_RESOLVE_CNTL_1, 2);
409 OUT_RING(ring, A6XX_GRAS_RESOLVE_CNTL_1_X(info->scissor.minx) |
410 A6XX_GRAS_RESOLVE_CNTL_1_Y(info->scissor.miny));
411 OUT_RING(ring, A6XX_GRAS_RESOLVE_CNTL_1_X(info->scissor.maxx - 1) |
412 A6XX_GRAS_RESOLVE_CNTL_1_Y(info->scissor.maxy - 1));
413 blit_cntl |= A6XX_RB_2D_BLIT_CNTL_SCISSOR;
414 }
415
416 OUT_PKT4(ring, REG_A6XX_RB_2D_BLIT_CNTL, 1);
417 OUT_RING(ring, blit_cntl);
418
419 OUT_PKT4(ring, REG_A6XX_GRAS_2D_BLIT_CNTL, 1);
420 OUT_RING(ring, blit_cntl);
421
422 for (unsigned i = 0; i < info->dst.box.depth; i++) {
423 unsigned soff = fd_resource_offset(src, info->src.level, sbox->z + i);
424 unsigned doff = fd_resource_offset(dst, info->dst.level, dbox->z + i);
425
426 /*
427 * Emit source:
428 */
429 uint32_t filter = 0;
430 if (info->filter == PIPE_TEX_FILTER_LINEAR)
431 filter = A6XX_SP_PS_2D_SRC_INFO_FILTER;
432
433 OUT_PKT4(ring, REG_A6XX_SP_PS_2D_SRC_INFO, 13);
434 OUT_RING(ring, A6XX_SP_PS_2D_SRC_INFO_COLOR_FORMAT(sfmt) |
435 A6XX_SP_PS_2D_SRC_INFO_TILE_MODE(stile) |
436 A6XX_SP_PS_2D_SRC_INFO_COLOR_SWAP(sswap) | 0x500000 | filter);
437 OUT_RING(ring, A6XX_SP_PS_2D_SRC_SIZE_WIDTH(width) |
438 A6XX_SP_PS_2D_SRC_SIZE_HEIGHT(height)); /* SP_PS_2D_SRC_SIZE */
439 OUT_RELOC(ring, src->bo, soff, 0, 0); /* SP_PS_2D_SRC_LO/HI */
440 OUT_RING(ring, A6XX_SP_PS_2D_SRC_PITCH_PITCH(spitch));
441 OUT_RING(ring, 0x00000000);
442 OUT_RING(ring, 0x00000000);
443 OUT_RING(ring, 0x00000000);
444 OUT_RING(ring, 0x00000000);
445 OUT_RING(ring, 0x00000000);
446
447 OUT_RING(ring, 0x00000000);
448 OUT_RING(ring, 0x00000000);
449 OUT_RING(ring, 0x00000000);
450
451 /*
452 * Emit destination:
453 */
454 OUT_PKT4(ring, REG_A6XX_RB_2D_DST_INFO, 9);
455 OUT_RING(ring, A6XX_RB_2D_DST_INFO_COLOR_FORMAT(dfmt) |
456 A6XX_RB_2D_DST_INFO_TILE_MODE(dtile) |
457 A6XX_RB_2D_DST_INFO_COLOR_SWAP(dswap));
458 OUT_RELOC(ring, dst->bo, doff, 0, 0); /* RB_2D_DST_LO/HI */
459 OUT_RING(ring, A6XX_RB_2D_DST_SIZE_PITCH(dpitch));
460 OUT_RING(ring, 0x00000000);
461 OUT_RING(ring, 0x00000000);
462 OUT_RING(ring, 0x00000000);
463 OUT_RING(ring, 0x00000000);
464 OUT_RING(ring, 0x00000000);
465
466 /*
467 * Blit command:
468 */
469 OUT_PKT4(ring, REG_A6XX_GRAS_2D_SRC_TL_X, 4);
470 OUT_RING(ring, A6XX_GRAS_2D_SRC_TL_X_X(sx1));
471 OUT_RING(ring, A6XX_GRAS_2D_SRC_BR_X_X(sx2));
472 OUT_RING(ring, A6XX_GRAS_2D_SRC_TL_Y_Y(sy1));
473 OUT_RING(ring, A6XX_GRAS_2D_SRC_BR_Y_Y(sy2));
474
475 OUT_PKT4(ring, REG_A6XX_GRAS_2D_DST_TL, 2);
476 OUT_RING(ring, A6XX_GRAS_2D_DST_TL_X(dx1) | A6XX_GRAS_2D_DST_TL_Y(dy1));
477 OUT_RING(ring, A6XX_GRAS_2D_DST_BR_X(dx2) | A6XX_GRAS_2D_DST_BR_Y(dy2));
478
479 OUT_PKT7(ring, CP_EVENT_WRITE, 1);
480 OUT_RING(ring, 0x3f);
481 OUT_WFI5(ring);
482
483 OUT_PKT4(ring, REG_A6XX_RB_UNKNOWN_8C01, 1);
484 OUT_RING(ring, 0);
485
486 OUT_PKT4(ring, REG_A6XX_SP_2D_SRC_FORMAT, 1);
487 OUT_RING(ring, A6XX_SP_2D_SRC_FORMAT_COLOR_FORMAT(sfmt) |
488 COND(util_format_is_pure_sint(info->src.format),
489 A6XX_SP_2D_SRC_FORMAT_SINT) |
490 COND(util_format_is_pure_uint(info->src.format),
491 A6XX_SP_2D_SRC_FORMAT_UINT) |
492 COND(util_format_is_snorm(info->src.format),
493 A6XX_SP_2D_SRC_FORMAT_SINT |
494 A6XX_SP_2D_SRC_FORMAT_NORM) |
495 COND(util_format_is_unorm(info->src.format),
496 // TODO sometimes blob uses UINT+NORM but dEQP seems unhappy about that
497 // A6XX_SP_2D_SRC_FORMAT_UINT |
498 A6XX_SP_2D_SRC_FORMAT_NORM) |
499 0xf000);
500
501 OUT_PKT4(ring, REG_A6XX_RB_UNKNOWN_8E04, 1);
502 OUT_RING(ring, 0x01000000);
503
504 OUT_PKT7(ring, CP_BLIT, 1);
505 OUT_RING(ring, CP_BLIT_0_OP(BLIT_OP_SCALE));
506
507 OUT_WFI5(ring);
508
509 OUT_PKT4(ring, REG_A6XX_RB_UNKNOWN_8E04, 1);
510 OUT_RING(ring, 0);
511 }
512 }
513
514 static void
515 emit_blit(struct pipe_context *pctx, const struct pipe_blit_info *info)
516 {
517 struct fd_context *ctx = fd_context(pctx);
518 struct fd_batch *batch;
519
520 fd_fence_ref(pctx->screen, &ctx->last_fence, NULL);
521
522 batch = fd_bc_alloc_batch(&ctx->screen->batch_cache, ctx, true);
523
524 fd6_emit_restore(batch, batch->draw);
525 fd6_emit_lrz_flush(batch->draw);
526
527 mtx_lock(&ctx->screen->lock);
528
529 fd_batch_resource_used(batch, fd_resource(info->src.resource), false);
530 fd_batch_resource_used(batch, fd_resource(info->dst.resource), true);
531
532 mtx_unlock(&ctx->screen->lock);
533
534 emit_setup(batch->draw);
535
536 if ((info->src.resource->target == PIPE_BUFFER) &&
537 (info->dst.resource->target == PIPE_BUFFER)) {
538 assert(fd_resource(info->src.resource)->tile_mode == TILE6_LINEAR);
539 assert(fd_resource(info->dst.resource)->tile_mode == TILE6_LINEAR);
540 emit_blit_buffer(batch->draw, info);
541 } else {
542 /* I don't *think* we need to handle blits between buffer <-> !buffer */
543 debug_assert(info->src.resource->target != PIPE_BUFFER);
544 debug_assert(info->dst.resource->target != PIPE_BUFFER);
545 emit_blit_texture(batch->draw, info);
546 }
547
548 fd6_event_write(batch, batch->draw, 0x1d, true);
549 fd6_event_write(batch, batch->draw, FACENESS_FLUSH, true);
550 fd6_event_write(batch, batch->draw, CACHE_FLUSH_TS, true);
551
552 fd_resource(info->dst.resource)->valid = true;
553 batch->needs_flush = true;
554
555 fd_batch_flush(batch, false, false);
556 fd_batch_reference(&batch, NULL);
557 }
558
559 static void
560 fd6_blit(struct pipe_context *pctx, const struct pipe_blit_info *info)
561 {
562 struct fd_context *ctx = fd_context(pctx);
563
564 if (!can_do_blit(info)) {
565 fd_blitter_blit(ctx, info);
566 return;
567 }
568
569 emit_blit(pctx, info);
570 }
571
572 static void
573 fd6_resource_copy_region(struct pipe_context *pctx,
574 struct pipe_resource *dst,
575 unsigned dst_level,
576 unsigned dstx, unsigned dsty, unsigned dstz,
577 struct pipe_resource *src,
578 unsigned src_level,
579 const struct pipe_box *src_box)
580 {
581 struct pipe_blit_info info;
582
583 debug_assert(src->format == dst->format);
584
585 memset(&info, 0, sizeof info);
586 info.dst.resource = dst;
587 info.dst.level = dst_level;
588 info.dst.box.x = dstx;
589 info.dst.box.y = dsty;
590 info.dst.box.z = dstz;
591 info.dst.box.width = src_box->width;
592 info.dst.box.height = src_box->height;
593 assert(info.dst.box.width >= 0);
594 assert(info.dst.box.height >= 0);
595 info.dst.box.depth = 1;
596 info.dst.format = dst->format;
597 info.src.resource = src;
598 info.src.level = src_level;
599 info.src.box = *src_box;
600 info.src.format = src->format;
601 info.mask = util_format_get_mask(src->format);
602 info.filter = PIPE_TEX_FILTER_NEAREST;
603 info.scissor_enable = 0;
604
605 if (!can_do_blit(&info)) {
606 fd_resource_copy_region(pctx,
607 dst, dst_level, dstx, dsty, dstz,
608 src, src_level, src_box);
609 return;
610 }
611
612 emit_blit(pctx, &info);
613 }
614
615 void
616 fd6_blitter_init(struct pipe_context *pctx)
617 {
618 if (fd_mesa_debug & FD_DBG_NOBLIT)
619 return;
620
621 pctx->resource_copy_region = fd6_resource_copy_region;
622 pctx->blit = fd6_blit;
623 }
624
625 unsigned
626 fd6_tile_mode(const struct pipe_resource *tmpl)
627 {
628 /* basically just has to be a format we can blit, so uploads/downloads
629 * via linear staging buffer works:
630 */
631 return TILE6_3;
632 }