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