1 /*
2 * Copyright © 2012 Rob Clark <[email protected]>
3 * SPDX-License-Identifier: MIT
4 *
5 * Authors:
6 * Rob Clark <[email protected]>
7 */
8
9 #include "pipe/p_state.h"
10 #include "util/format/u_format.h"
11 #include "util/hash_table.h"
12 #include "util/macros.h"
13 #include "util/u_debug.h"
14 #include "util/u_dump.h"
15 #include "util/u_inlines.h"
16 #include "util/u_memory.h"
17 #include "util/u_string.h"
18 #include "util/u_trace_gallium.h"
19 #include "u_tracepoints.h"
20
21 #include "freedreno_context.h"
22 #include "freedreno_fence.h"
23 #include "freedreno_gmem.h"
24 #include "freedreno_query_hw.h"
25 #include "freedreno_resource.h"
26 #include "freedreno_tracepoints.h"
27 #include "freedreno_util.h"
28
29 /*
30 * GMEM is the small (ie. 256KiB for a200, 512KiB for a220, etc) tile buffer
31 * inside the GPU. All rendering happens to GMEM. Larger render targets
32 * are split into tiles that are small enough for the color (and depth and/or
33 * stencil, if enabled) buffers to fit within GMEM. Before rendering a tile,
34 * if there was not a clear invalidating the previous tile contents, we need
35 * to restore the previous tiles contents (system mem -> GMEM), and after all
36 * the draw calls, before moving to the next tile, we need to save the tile
37 * contents (GMEM -> system mem).
38 *
39 * The code in this file handles dealing with GMEM and tiling.
40 *
41 * The structure of the ringbuffer ends up being:
42 *
43 * +--<---<-- IB ---<---+---<---+---<---<---<--+
44 * | | | |
45 * v ^ ^ ^
46 * ------------------------------------------------------
47 * | clear/draw cmds | Tile0 | Tile1 | .... | TileN |
48 * ------------------------------------------------------
49 * ^
50 * |
51 * address submitted in issueibcmds
52 *
53 * Where the per-tile section handles scissor setup, mem2gmem restore (if
54 * needed), IB to draw cmds earlier in the ringbuffer, and then gmem2mem
55 * resolve.
56 */
57
58 #ifndef BIN_DEBUG
59 #define BIN_DEBUG 0
60 #endif
61
62 /*
63 * GMEM Cache:
64 *
65 * Caches GMEM state based on a given framebuffer state. The key is
66 * meant to be the minimal set of data that results in a unique gmem
67 * configuration, avoiding multiple keys arriving at the same gmem
68 * state. For example, the render target format is not part of the
69 * key, only the size per pixel. And the max_scissor bounds is not
70 * part of they key, only the minx/miny (after clamping to tile
71 * alignment) and width/height. This ensures that slightly different
72 * max_scissor which would result in the same gmem state, do not
73 * become different keys that map to the same state.
74 */
75
76 struct gmem_key {
77 uint16_t minx, miny;
78 uint16_t width, height;
79 uint8_t gmem_page_align; /* alignment in multiples of 0x1000 to reduce key size */
80 uint8_t nr_cbufs;
81 uint8_t cbuf_cpp[MAX_RENDER_TARGETS];
82 uint8_t zsbuf_cpp[2];
83 };
84
85 static uint32_t
gmem_key_hash(const void * _key)86 gmem_key_hash(const void *_key)
87 {
88 const struct gmem_key *key = _key;
89 return _mesa_hash_data(key, sizeof(*key));
90 }
91
92 static bool
gmem_key_equals(const void * _a,const void * _b)93 gmem_key_equals(const void *_a, const void *_b)
94 {
95 const struct gmem_key *a = _a;
96 const struct gmem_key *b = _b;
97 return memcmp(a, b, sizeof(*a)) == 0;
98 }
99
100 static void
dump_gmem_key(const struct gmem_key * key)101 dump_gmem_key(const struct gmem_key *key)
102 {
103 printf("{ .minx=%u, .miny=%u, .width=%u, .height=%u", key->minx, key->miny,
104 key->width, key->height);
105 printf(", .gmem_page_align=%u, .nr_cbufs=%u", key->gmem_page_align,
106 key->nr_cbufs);
107 printf(", .cbuf_cpp = {");
108 for (unsigned i = 0; i < ARRAY_SIZE(key->cbuf_cpp); i++)
109 printf("%u,", key->cbuf_cpp[i]);
110 printf("}, .zsbuf_cpp = {");
111 for (unsigned i = 0; i < ARRAY_SIZE(key->zsbuf_cpp); i++)
112 printf("%u,", key->zsbuf_cpp[i]);
113 printf("}},\n");
114 }
115
116 static void
dump_gmem_state(const struct fd_gmem_stateobj * gmem)117 dump_gmem_state(const struct fd_gmem_stateobj *gmem)
118 {
119 unsigned total = 0;
120 printf("GMEM LAYOUT: bin=%ux%u, nbins=%ux%u\n", gmem->bin_w, gmem->bin_h,
121 gmem->nbins_x, gmem->nbins_y);
122 for (int i = 0; i < ARRAY_SIZE(gmem->cbuf_base); i++) {
123 if (!gmem->cbuf_cpp[i])
124 continue;
125
126 unsigned size = gmem->cbuf_cpp[i] * gmem->bin_w * gmem->bin_h;
127 printf(" cbuf[%d]: base=0x%06x, size=0x%x, cpp=%u\n", i,
128 gmem->cbuf_base[i], size, gmem->cbuf_cpp[i]);
129
130 total = gmem->cbuf_base[i] + size;
131 }
132
133 for (int i = 0; i < ARRAY_SIZE(gmem->zsbuf_base); i++) {
134 if (!gmem->zsbuf_cpp[i])
135 continue;
136
137 unsigned size = gmem->zsbuf_cpp[i] * gmem->bin_w * gmem->bin_h;
138 printf(" zsbuf[%d]: base=0x%06x, size=0x%x, cpp=%u\n", i,
139 gmem->zsbuf_base[i], size, gmem->zsbuf_cpp[i]);
140
141 total = gmem->zsbuf_base[i] + size;
142 }
143
144 printf("total: 0x%06x (of 0x%06x)\n", total, gmem->screen->gmemsize_bytes);
145 }
146
147 static unsigned
div_align(unsigned num,unsigned denom,unsigned al)148 div_align(unsigned num, unsigned denom, unsigned al)
149 {
150 return util_align_npot(DIV_ROUND_UP(num, denom), al);
151 }
152
153 static bool
layout_gmem(struct gmem_key * key,uint32_t nbins_x,uint32_t nbins_y,struct fd_gmem_stateobj * gmem)154 layout_gmem(struct gmem_key *key, uint32_t nbins_x, uint32_t nbins_y,
155 struct fd_gmem_stateobj *gmem)
156 {
157 struct fd_screen *screen = gmem->screen;
158 uint32_t gmem_align = key->gmem_page_align * 0x1000;
159 uint32_t total = 0, i;
160
161 if ((nbins_x == 0) || (nbins_y == 0))
162 return false;
163
164 uint32_t bin_w, bin_h;
165 bin_w = div_align(key->width, nbins_x, screen->info->tile_align_w);
166 bin_h = div_align(key->height, nbins_y, screen->info->tile_align_h);
167
168 if (bin_w > screen->info->tile_max_w)
169 return false;
170
171 if (bin_h > screen->info->tile_max_h)
172 return false;
173
174 gmem->bin_w = bin_w;
175 gmem->bin_h = bin_h;
176
177 /* due to aligning bin_w/h, we could end up with one too
178 * many bins in either dimension, so recalculate:
179 */
180 gmem->nbins_x = DIV_ROUND_UP(key->width, bin_w);
181 gmem->nbins_y = DIV_ROUND_UP(key->height, bin_h);
182
183 for (i = 0; i < MAX_RENDER_TARGETS; i++) {
184 if (key->cbuf_cpp[i]) {
185 gmem->cbuf_base[i] = util_align_npot(total, gmem_align);
186 total = gmem->cbuf_base[i] + key->cbuf_cpp[i] * bin_w * bin_h;
187 }
188 }
189
190 if (key->zsbuf_cpp[0]) {
191 gmem->zsbuf_base[0] = util_align_npot(total, gmem_align);
192 total = gmem->zsbuf_base[0] + key->zsbuf_cpp[0] * bin_w * bin_h;
193 }
194
195 if (key->zsbuf_cpp[1]) {
196 gmem->zsbuf_base[1] = util_align_npot(total, gmem_align);
197 total = gmem->zsbuf_base[1] + key->zsbuf_cpp[1] * bin_w * bin_h;
198 }
199
200 return total <= screen->gmemsize_bytes;
201 }
202
203 static void
calc_nbins(struct gmem_key * key,struct fd_gmem_stateobj * gmem)204 calc_nbins(struct gmem_key *key, struct fd_gmem_stateobj *gmem)
205 {
206 struct fd_screen *screen = gmem->screen;
207 uint32_t nbins_x = 1, nbins_y = 1;
208 uint32_t max_width = screen->info->tile_max_w;
209 uint32_t max_height = screen->info->tile_max_h;
210
211 if (FD_DBG(MSGS)) {
212 debug_printf("binning input: cbuf cpp:");
213 for (unsigned i = 0; i < key->nr_cbufs; i++)
214 debug_printf(" %d", key->cbuf_cpp[i]);
215 debug_printf(", zsbuf cpp: %d; %dx%d\n", key->zsbuf_cpp[0], key->width,
216 key->height);
217 }
218
219 /* first, find a bin size that satisfies the maximum width/
220 * height restrictions:
221 */
222 while (div_align(key->width, nbins_x, screen->info->tile_align_w) >
223 max_width) {
224 nbins_x++;
225 }
226
227 while (div_align(key->height, nbins_y, screen->info->tile_align_h) >
228 max_height) {
229 nbins_y++;
230 }
231
232 /* then find a bin width/height that satisfies the memory
233 * constraints:
234 */
235 while (!layout_gmem(key, nbins_x, nbins_y, gmem)) {
236 if (nbins_y > nbins_x) {
237 nbins_x++;
238 } else {
239 nbins_y++;
240 }
241 }
242
243 /* Lets see if we can tweak the layout a bit and come up with
244 * something better:
245 */
246 if ((((nbins_x - 1) * (nbins_y + 1)) < (nbins_x * nbins_y)) &&
247 layout_gmem(key, nbins_x - 1, nbins_y + 1, gmem)) {
248 nbins_x--;
249 nbins_y++;
250 } else if ((((nbins_x + 1) * (nbins_y - 1)) < (nbins_x * nbins_y)) &&
251 layout_gmem(key, nbins_x + 1, nbins_y - 1, gmem)) {
252 nbins_x++;
253 nbins_y--;
254 }
255
256 layout_gmem(key, nbins_x, nbins_y, gmem);
257 }
258
259 static struct fd_gmem_stateobj *
gmem_stateobj_init(struct fd_screen * screen,struct gmem_key * key)260 gmem_stateobj_init(struct fd_screen *screen, struct gmem_key *key)
261 {
262 struct fd_gmem_stateobj *gmem =
263 rzalloc(screen->gmem_cache.ht, struct fd_gmem_stateobj);
264 pipe_reference_init(&gmem->reference, 1);
265 gmem->screen = screen;
266 gmem->key = key;
267 list_inithead(&gmem->node);
268
269 const unsigned npipes = screen->info->num_vsc_pipes;
270 uint32_t i, j, t, xoff, yoff;
271 uint32_t tpp_x, tpp_y;
272 int tile_n[npipes];
273
274 calc_nbins(key, gmem);
275
276 DBG("using %d bins of size %dx%d", gmem->nbins_x * gmem->nbins_y,
277 gmem->bin_w, gmem->bin_h);
278
279 memcpy(gmem->cbuf_cpp, key->cbuf_cpp, sizeof(key->cbuf_cpp));
280 memcpy(gmem->zsbuf_cpp, key->zsbuf_cpp, sizeof(key->zsbuf_cpp));
281 gmem->minx = key->minx;
282 gmem->miny = key->miny;
283 gmem->width = key->width;
284 gmem->height = key->height;
285
286 gmem->tile = rzalloc_array(gmem, struct fd_tile, gmem->nbins_x * gmem->nbins_y);
287
288 if (BIN_DEBUG) {
289 dump_gmem_state(gmem);
290 dump_gmem_key(key);
291 }
292
293 /*
294 * Assign tiles and pipes:
295 *
296 * At some point it might be worth playing with different
297 * strategies and seeing if that makes much impact on
298 * performance.
299 */
300
301 /* figure out number of tiles per pipe: */
302 if (is_a20x(screen)) {
303 /* for a20x we want to minimize the number of "pipes"
304 * binning data has 3 bits for x/y (8x8) but the edges are used to
305 * cull off-screen vertices with hw binning, so we have 6x6 pipes
306 */
307 tpp_x = 6;
308 tpp_y = 6;
309 } else {
310 tpp_x = tpp_y = 1;
311 while (DIV_ROUND_UP(gmem->nbins_y, tpp_y) > npipes)
312 tpp_y += 2;
313 while ((DIV_ROUND_UP(gmem->nbins_y, tpp_y) *
314 DIV_ROUND_UP(gmem->nbins_x, tpp_x)) > npipes)
315 tpp_x += 1;
316 }
317
318 #if MESA_DEBUG
319 tpp_x = debug_get_num_option("TPP_X", tpp_x);
320 tpp_y = debug_get_num_option("TPP_Y", tpp_x);
321 #endif
322
323 gmem->maxpw = tpp_x;
324 gmem->maxph = tpp_y;
325
326 /* configure pipes: */
327 xoff = yoff = 0;
328 for (i = 0; i < npipes; i++) {
329 struct fd_vsc_pipe *pipe = &gmem->vsc_pipe[i];
330
331 if (xoff >= gmem->nbins_x) {
332 xoff = 0;
333 yoff += tpp_y;
334 }
335
336 if (yoff >= gmem->nbins_y) {
337 break;
338 }
339
340 pipe->x = xoff;
341 pipe->y = yoff;
342 pipe->w = MIN2(tpp_x, gmem->nbins_x - xoff);
343 pipe->h = MIN2(tpp_y, gmem->nbins_y - yoff);
344
345 xoff += tpp_x;
346 }
347
348 /* number of pipes to use for a20x */
349 gmem->num_vsc_pipes = MAX2(1, i);
350
351 for (; i < npipes; i++) {
352 struct fd_vsc_pipe *pipe = &gmem->vsc_pipe[i];
353 pipe->x = pipe->y = pipe->w = pipe->h = 0;
354 }
355
356 if (BIN_DEBUG) {
357 printf("%dx%d ... tpp=%dx%d\n", gmem->nbins_x, gmem->nbins_y, tpp_x,
358 tpp_y);
359 for (i = 0; i < ARRAY_SIZE(gmem->vsc_pipe); i++) {
360 struct fd_vsc_pipe *pipe = &gmem->vsc_pipe[i];
361 printf("pipe[%d]: %ux%u @ %u,%u\n", i, pipe->w, pipe->h, pipe->x,
362 pipe->y);
363 }
364 }
365
366 /* configure tiles: */
367 t = 0;
368 yoff = key->miny;
369 memset(tile_n, 0, sizeof(tile_n));
370 for (i = 0; i < gmem->nbins_y; i++) {
371 int bw, bh;
372
373 xoff = key->minx;
374
375 /* clip bin height: */
376 bh = MIN2(gmem->bin_h, key->miny + key->height - yoff);
377 assert(bh > 0);
378
379 for (j = 0; j < gmem->nbins_x; j++) {
380 struct fd_tile *tile = &gmem->tile[t];
381 uint32_t p;
382
383 /* pipe number: */
384 p = ((i / tpp_y) * DIV_ROUND_UP(gmem->nbins_x, tpp_x)) + (j / tpp_x);
385 assert(p < gmem->num_vsc_pipes);
386
387 /* clip bin width: */
388 bw = MIN2(gmem->bin_w, key->minx + key->width - xoff);
389 assert(bw > 0);
390
391 tile->n = !is_a20x(screen) ? tile_n[p]++
392 : ((i % tpp_y + 1) << 3 | (j % tpp_x + 1));
393 tile->p = p;
394 tile->bin_w = bw;
395 tile->bin_h = bh;
396 tile->xoff = xoff;
397 tile->yoff = yoff;
398
399 if (BIN_DEBUG) {
400 printf("tile[%d]: p=%u, bin=%ux%u+%u+%u\n", t, p, bw, bh, xoff,
401 yoff);
402 }
403
404 t++;
405
406 xoff += bw;
407 }
408
409 yoff += bh;
410 }
411
412 /* Swap the order of alternating rows to form an 'S' pattern, to improve
413 * cache access patterns (ie. adjacent bins are likely to access adjacent
414 * portions of textures)
415 */
416 if (!FD_DBG(NOSBIN)) {
417 for (i = 0; i < gmem->nbins_y; i+=2) {
418 unsigned col0 = gmem->nbins_x * i;
419 for (j = 0; j < gmem->nbins_x/2; j++) {
420 SWAP(gmem->tile[col0 + j], gmem->tile[col0 + gmem->nbins_x - j - 1]);
421 }
422 }
423 }
424
425 if (BIN_DEBUG) {
426 t = 0;
427 for (i = 0; i < gmem->nbins_y; i++) {
428 for (j = 0; j < gmem->nbins_x; j++) {
429 struct fd_tile *tile = &gmem->tile[t++];
430 printf("|p:%u n:%u|", tile->p, tile->n);
431 }
432 printf("\n");
433 }
434 }
435
436 return gmem;
437 }
438
439 void
__fd_gmem_destroy(struct fd_gmem_stateobj * gmem)440 __fd_gmem_destroy(struct fd_gmem_stateobj *gmem)
441 {
442 struct fd_gmem_cache *cache = &gmem->screen->gmem_cache;
443
444 fd_screen_assert_locked(gmem->screen);
445
446 _mesa_hash_table_remove_key(cache->ht, gmem->key);
447 list_del(&gmem->node);
448
449 ralloc_free(gmem->key);
450 ralloc_free(gmem);
451 }
452
453 static struct gmem_key *
gmem_key_init(struct fd_batch * batch,bool assume_zs,bool no_scis_opt)454 gmem_key_init(struct fd_batch *batch, bool assume_zs, bool no_scis_opt)
455 {
456 struct fd_screen *screen = batch->ctx->screen;
457 struct pipe_framebuffer_state *pfb = &batch->framebuffer;
458 bool has_zs = pfb->zsbuf &&
459 !!(batch->gmem_reason & (FD_GMEM_DEPTH_ENABLED | FD_GMEM_STENCIL_ENABLED |
460 FD_GMEM_CLEARS_DEPTH_STENCIL));
461 struct gmem_key *key = rzalloc(screen->gmem_cache.ht, struct gmem_key);
462
463 if (has_zs || assume_zs) {
464 struct fd_resource *rsc = fd_resource(pfb->zsbuf->texture);
465 key->zsbuf_cpp[0] = rsc->layout.cpp * pfb->samples;
466 if (rsc->stencil)
467 key->zsbuf_cpp[1] = rsc->stencil->layout.cpp * pfb->samples;
468
469 /* If we clear z or s but not both, and we are using z24s8 (ie.
470 * !separate_stencil) then we need to restore the other, even if
471 * batch_draw_tracking_for_dirty_bits() never saw a draw with
472 * depth or stencil enabled.
473 *
474 * This only applies to the fast-clear path, clears done with
475 * u_blitter will show up as a normal draw with depth and/or
476 * stencil enabled.
477 */
478 unsigned zsclear = batch->cleared & (FD_BUFFER_DEPTH | FD_BUFFER_STENCIL);
479 if (zsclear) {
480 const struct util_format_description *desc =
481 util_format_description(pfb->zsbuf->format);
482 if (util_format_has_depth(desc) && !(zsclear & FD_BUFFER_DEPTH))
483 batch->restore |= FD_BUFFER_DEPTH;
484 if (util_format_has_stencil(desc) && !(zsclear & FD_BUFFER_STENCIL))
485 batch->restore |= FD_BUFFER_STENCIL;
486 }
487 } else {
488 /* we might have a zsbuf, but it isn't used */
489 batch->restore &= ~(FD_BUFFER_DEPTH | FD_BUFFER_STENCIL);
490 batch->resolve &= ~(FD_BUFFER_DEPTH | FD_BUFFER_STENCIL);
491 }
492
493 key->nr_cbufs = pfb->nr_cbufs;
494 for (unsigned i = 0; i < pfb->nr_cbufs; i++) {
495 if (pfb->cbufs[i])
496 key->cbuf_cpp[i] = util_format_get_blocksize(pfb->cbufs[i]->format);
497 else
498 key->cbuf_cpp[i] = 4;
499 /* if MSAA, color buffers are super-sampled in GMEM: */
500 key->cbuf_cpp[i] *= pfb->samples;
501 }
502
503 /* NOTE: on a6xx, the max-scissor-rect is handled in fd6_gmem, and
504 * we just rely on CP_COND_EXEC to skip bins with no geometry.
505 */
506 if (no_scis_opt || is_a6xx(screen)) {
507 key->minx = 0;
508 key->miny = 0;
509 key->width = pfb->width;
510 key->height = pfb->height;
511 } else {
512 struct pipe_scissor_state *scissor = &batch->max_scissor;
513
514 if (FD_DBG(NOSCIS)) {
515 scissor->minx = 0;
516 scissor->miny = 0;
517 scissor->maxx = pfb->width - 1;
518 scissor->maxy = pfb->height - 1;
519 }
520
521 /* round down to multiple of alignment: */
522 key->minx = scissor->minx & ~(screen->info->gmem_align_w - 1);
523 key->miny = scissor->miny & ~(screen->info->gmem_align_h - 1);
524 key->width = scissor->maxx + 1 - key->minx;
525 key->height = scissor->maxy + 1 - key->miny;
526 }
527
528 if (is_a20x(screen) && batch->cleared) {
529 /* under normal circumstances the requirement would be 4K
530 * but the fast clear path requires an alignment of 32K
531 */
532 key->gmem_page_align = 8;
533 } else if (is_a6xx(screen)) {
534 key->gmem_page_align = screen->info->num_ccu;
535 } else {
536 // TODO re-check this across gens.. maybe it should only
537 // be a single page in some cases:
538 key->gmem_page_align = 4;
539 }
540
541 return key;
542 }
543
544 static struct fd_gmem_stateobj *
lookup_gmem_state(struct fd_batch * batch,bool assume_zs,bool no_scis_opt)545 lookup_gmem_state(struct fd_batch *batch, bool assume_zs, bool no_scis_opt)
546 {
547 struct fd_screen *screen = batch->ctx->screen;
548 struct fd_gmem_cache *cache = &screen->gmem_cache;
549 struct fd_gmem_stateobj *gmem = NULL;
550
551 /* Lock before allocating gmem_key, since that a screen-wide
552 * ralloc pool and ralloc itself is not thread-safe.
553 */
554 fd_screen_lock(screen);
555
556 struct gmem_key *key = gmem_key_init(batch, assume_zs, no_scis_opt);
557 uint32_t hash = gmem_key_hash(key);
558
559 struct hash_entry *entry =
560 _mesa_hash_table_search_pre_hashed(cache->ht, hash, key);
561 if (entry) {
562 ralloc_free(key);
563 goto found;
564 }
565
566 /* limit the # of cached gmem states, discarding the least
567 * recently used state if needed:
568 */
569 if (cache->ht->entries >= 20) {
570 struct fd_gmem_stateobj *last =
571 list_last_entry(&cache->lru, struct fd_gmem_stateobj, node);
572 fd_gmem_reference(&last, NULL);
573 }
574
575 entry = _mesa_hash_table_insert_pre_hashed(cache->ht, hash, key,
576 gmem_stateobj_init(screen, key));
577
578 found:
579 fd_gmem_reference(&gmem, entry->data);
580 /* Move to the head of the LRU: */
581 list_delinit(&gmem->node);
582 list_add(&gmem->node, &cache->lru);
583
584 fd_screen_unlock(screen);
585
586 return gmem;
587 }
588
589 /*
590 * GMEM render pass
591 */
592
593 static void
render_tiles(struct fd_batch * batch,struct fd_gmem_stateobj * gmem)594 render_tiles(struct fd_batch *batch, struct fd_gmem_stateobj *gmem) assert_dt
595 {
596 struct fd_context *ctx = batch->ctx;
597 int i;
598
599 simple_mtx_lock(&ctx->gmem_lock);
600
601 ctx->emit_tile_init(batch);
602
603 if (batch->restore)
604 ctx->stats.batch_restore++;
605
606 for (i = 0; i < (gmem->nbins_x * gmem->nbins_y); i++) {
607 struct fd_tile *tile = &gmem->tile[i];
608
609 trace_start_tile(&batch->trace, batch->gmem, tile->bin_h, tile->yoff, tile->bin_w,
610 tile->xoff);
611
612 ctx->emit_tile_prep(batch, tile);
613
614 if (batch->restore) {
615 ctx->emit_tile_mem2gmem(batch, tile);
616 }
617
618 ctx->emit_tile_renderprep(batch, tile);
619
620 if (ctx->query_prepare_tile)
621 ctx->query_prepare_tile(batch, i, batch->gmem);
622
623 /* emit IB to drawcmds: */
624 trace_start_draw_ib(&batch->trace, batch->gmem);
625 if (ctx->emit_tile) {
626 ctx->emit_tile(batch, tile);
627 } else {
628 ctx->screen->emit_ib(batch->gmem, batch->draw);
629 }
630 trace_end_draw_ib(&batch->trace, batch->gmem);
631 fd_reset_wfi(batch);
632
633 /* emit gmem2mem to transfer tile back to system memory: */
634 ctx->emit_tile_gmem2mem(batch, tile);
635 }
636
637 if (ctx->emit_tile_fini)
638 ctx->emit_tile_fini(batch);
639
640 simple_mtx_unlock(&ctx->gmem_lock);
641 }
642
643 static void
render_sysmem(struct fd_batch * batch)644 render_sysmem(struct fd_batch *batch) assert_dt
645 {
646 struct fd_context *ctx = batch->ctx;
647
648 ctx->emit_sysmem_prep(batch);
649
650 if (ctx->query_prepare_tile)
651 ctx->query_prepare_tile(batch, 0, batch->gmem);
652
653 if (!batch->nondraw) {
654 trace_start_draw_ib(&batch->trace, batch->gmem);
655 }
656 /* emit IB to drawcmds: */
657 if (ctx->emit_sysmem) {
658 ctx->emit_sysmem(batch);
659 } else {
660 ctx->screen->emit_ib(batch->gmem, batch->draw);
661 }
662
663 if (!batch->nondraw) {
664 trace_end_draw_ib(&batch->trace, batch->gmem);
665 }
666
667 fd_reset_wfi(batch);
668
669 if (ctx->emit_sysmem_fini)
670 ctx->emit_sysmem_fini(batch);
671 }
672
673 static void
flush_ring(struct fd_batch * batch)674 flush_ring(struct fd_batch *batch)
675 {
676 bool use_fence_fd = false;
677 if (batch->fence)
678 use_fence_fd = batch->fence->use_fence_fd;
679
680 struct fd_fence *fence;
681
682 if (FD_DBG(NOHW)) {
683 /* construct a dummy fence: */
684 fence = fd_fence_new(batch->ctx->pipe, use_fence_fd);
685 } else {
686 fence = fd_submit_flush(batch->submit, batch->in_fence_fd, use_fence_fd);
687 }
688
689 if (batch->fence) {
690 fd_pipe_fence_set_submit_fence(batch->fence, fence);
691 } else {
692 fd_fence_del(fence);
693 }
694 }
695
696 void
fd_gmem_render_tiles(struct fd_batch * batch)697 fd_gmem_render_tiles(struct fd_batch *batch)
698 {
699 struct fd_context *ctx = batch->ctx;
700 struct pipe_framebuffer_state *pfb = &batch->framebuffer;
701 bool sysmem = false;
702
703 ctx->submit_count++;
704
705 /* Sometimes we need to flush a batch just to get a fence, with no
706 * clears or draws.. in this case promote to nondraw:
707 */
708 if (!(batch->cleared || batch->num_draws))
709 sysmem = true;
710
711 if (!batch->nondraw) {
712 #if HAVE_PERFETTO
713 /* For non-draw batches, we don't really have a good place to
714 * match up the api event submit-id to the on-gpu rendering,
715 * so skip this for non-draw batches.
716 */
717 fd_perfetto_submit(ctx);
718 #endif
719 trace_flush_batch(&batch->trace, batch->gmem, batch, batch->cleared,
720 batch->gmem_reason, batch->num_draws);
721 trace_framebuffer_state(&batch->trace, batch->gmem, pfb);
722 }
723
724 if (ctx->emit_sysmem_prep && !batch->nondraw) {
725 if (fd_autotune_use_bypass(&ctx->autotune, batch) && !FD_DBG(GMEM)) {
726 sysmem = true;
727 }
728
729 /* For ARB_framebuffer_no_attachments: */
730 if ((pfb->nr_cbufs == 0) && !pfb->zsbuf) {
731 sysmem = true;
732 }
733 }
734
735 if (FD_DBG(SYSMEM))
736 sysmem = true;
737
738 /* Layered rendering always needs bypass. */
739 for (unsigned i = 0; i < pfb->nr_cbufs; i++) {
740 struct pipe_surface *psurf = pfb->cbufs[i];
741 if (!psurf)
742 continue;
743 if (psurf->u.tex.first_layer < psurf->u.tex.last_layer)
744 sysmem = true;
745 }
746 if (pfb->zsbuf && pfb->zsbuf->u.tex.first_layer < pfb->zsbuf->u.tex.last_layer)
747 sysmem = true;
748
749 /* Tessellation doesn't seem to support tiled rendering so fall back to
750 * bypass.
751 */
752 if (batch->tessellation) {
753 assert(ctx->emit_sysmem_prep);
754 sysmem = true;
755 }
756
757 fd_reset_wfi(batch);
758
759 ctx->stats.batch_total++;
760
761 if (batch->nondraw) {
762 DBG("%p: rendering non-draw", batch);
763 if (!fd_ringbuffer_empty(batch->draw))
764 render_sysmem(batch);
765 ctx->stats.batch_nondraw++;
766 } else if (sysmem) {
767 trace_render_sysmem(&batch->trace, batch->gmem);
768 trace_start_render_pass(&batch->trace, batch->gmem,
769 ctx->submit_count, pipe_surface_format(pfb->cbufs[0]),
770 pipe_surface_format(pfb->zsbuf), pfb->width, pfb->height,
771 pfb->nr_cbufs, pfb->samples, 0, 0, 0);
772 if (ctx->query_prepare)
773 ctx->query_prepare(batch, 1);
774 render_sysmem(batch);
775 trace_end_render_pass(&batch->trace, batch->gmem);
776 ctx->stats.batch_sysmem++;
777 } else {
778 struct fd_gmem_stateobj *gmem = lookup_gmem_state(batch, false, false);
779 batch->gmem_state = gmem;
780 trace_render_gmem(&batch->trace, batch->gmem, gmem->nbins_x, gmem->nbins_y,
781 gmem->bin_w, gmem->bin_h);
782 trace_start_render_pass(&batch->trace, batch->gmem,
783 ctx->submit_count, pipe_surface_format(pfb->cbufs[0]),
784 pipe_surface_format(pfb->zsbuf), pfb->width, pfb->height,
785 pfb->nr_cbufs, pfb->samples, gmem->nbins_x * gmem->nbins_y,
786 gmem->bin_w, gmem->bin_h);
787 if (ctx->query_prepare)
788 ctx->query_prepare(batch, gmem->nbins_x * gmem->nbins_y);
789 render_tiles(batch, gmem);
790 trace_end_render_pass(&batch->trace, batch->gmem);
791 batch->gmem_state = NULL;
792
793 fd_screen_lock(ctx->screen);
794 fd_gmem_reference(&gmem, NULL);
795 fd_screen_unlock(ctx->screen);
796
797 ctx->stats.batch_gmem++;
798 }
799
800 flush_ring(batch);
801
802 u_trace_flush(&batch->trace, NULL, U_TRACE_FRAME_UNKNOWN, false);
803 }
804
805 /* Determine a worst-case estimate (ie. assuming we don't eliminate an
806 * unused depth/stencil) number of bins per vsc pipe.
807 */
808 unsigned
fd_gmem_estimate_bins_per_pipe(struct fd_batch * batch)809 fd_gmem_estimate_bins_per_pipe(struct fd_batch *batch)
810 {
811 struct pipe_framebuffer_state *pfb = &batch->framebuffer;
812 struct fd_screen *screen = batch->ctx->screen;
813 struct fd_gmem_stateobj *gmem = lookup_gmem_state(batch, !!pfb->zsbuf, true);
814 unsigned nbins = gmem->maxpw * gmem->maxph;
815
816 fd_screen_lock(screen);
817 fd_gmem_reference(&gmem, NULL);
818 fd_screen_unlock(screen);
819
820 return nbins;
821 }
822
823 /* When deciding whether a tile needs mem2gmem, we need to take into
824 * account the scissor rect(s) that were cleared. To simplify we only
825 * consider the last scissor rect for each buffer, since the common
826 * case would be a single clear.
827 */
828 bool
fd_gmem_needs_restore(struct fd_batch * batch,const struct fd_tile * tile,uint32_t buffers)829 fd_gmem_needs_restore(struct fd_batch *batch, const struct fd_tile *tile,
830 uint32_t buffers)
831 {
832 if (!(batch->restore & buffers))
833 return false;
834
835 return true;
836 }
837
838 void
fd_gmem_screen_init(struct pipe_screen * pscreen)839 fd_gmem_screen_init(struct pipe_screen *pscreen)
840 {
841 struct fd_gmem_cache *cache = &fd_screen(pscreen)->gmem_cache;
842
843 cache->ht = _mesa_hash_table_create(NULL, gmem_key_hash, gmem_key_equals);
844 list_inithead(&cache->lru);
845 }
846
847 void
fd_gmem_screen_fini(struct pipe_screen * pscreen)848 fd_gmem_screen_fini(struct pipe_screen *pscreen)
849 {
850 struct fd_gmem_cache *cache = &fd_screen(pscreen)->gmem_cache;
851
852 _mesa_hash_table_destroy(cache->ht, NULL);
853 }
854