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