1 /* -*- mode: C; c-file-style: "k&r"; tab-width 4; indent-tabs-mode: t; -*- */
4 * Copyright (C) 2012 Rob Clark <robclark@freedesktop.org>
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice (including the next
14 * paragraph) shall be included in all copies or substantial portions of the
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
26 * Rob Clark <robclark@freedesktop.org>
29 #include "pipe/p_state.h"
30 #include "util/u_string.h"
31 #include "util/u_memory.h"
32 #include "util/u_inlines.h"
33 #include "util/u_format.h"
35 #include "freedreno_gmem.h"
36 #include "freedreno_context.h"
37 #include "freedreno_resource.h"
38 #include "freedreno_query_hw.h"
39 #include "freedreno_util.h"
42 * GMEM is the small (ie. 256KiB for a200, 512KiB for a220, etc) tile buffer
43 * inside the GPU. All rendering happens to GMEM. Larger render targets
44 * are split into tiles that are small enough for the color (and depth and/or
45 * stencil, if enabled) buffers to fit within GMEM. Before rendering a tile,
46 * if there was not a clear invalidating the previous tile contents, we need
47 * to restore the previous tiles contents (system mem -> GMEM), and after all
48 * the draw calls, before moving to the next tile, we need to save the tile
49 * contents (GMEM -> system mem).
51 * The code in this file handles dealing with GMEM and tiling.
53 * The structure of the ringbuffer ends up being:
55 * +--<---<-- IB ---<---+---<---+---<---<---<--+
58 * ------------------------------------------------------
59 * | clear/draw cmds | Tile0 | Tile1 | .... | TileN |
60 * ------------------------------------------------------
63 * address submitted in issueibcmds
65 * Where the per-tile section handles scissor setup, mem2gmem restore (if
66 * needed), IB to draw cmds earlier in the ringbuffer, and then gmem2mem
70 static uint32_t bin_width(struct fd_screen
*screen
)
80 total_size(uint8_t cbuf_cpp
[], uint8_t zsbuf_cpp
[2],
81 uint32_t bin_w
, uint32_t bin_h
, struct fd_gmem_stateobj
*gmem
)
83 uint32_t total
= 0, i
;
85 for (i
= 0; i
< MAX_RENDER_TARGETS
; i
++) {
87 gmem
->cbuf_base
[i
] = align(total
, 0x4000);
88 total
= gmem
->cbuf_base
[i
] + cbuf_cpp
[i
] * bin_w
* bin_h
;
93 gmem
->zsbuf_base
[0] = align(total
, 0x4000);
94 total
= gmem
->zsbuf_base
[0] + zsbuf_cpp
[0] * bin_w
* bin_h
;
98 gmem
->zsbuf_base
[1] = align(total
, 0x4000);
99 total
= gmem
->zsbuf_base
[1] + zsbuf_cpp
[1] * bin_w
* bin_h
;
106 calculate_tiles(struct fd_batch
*batch
)
108 struct fd_context
*ctx
= batch
->ctx
;
109 struct fd_gmem_stateobj
*gmem
= &ctx
->gmem
;
110 struct pipe_scissor_state
*scissor
= &batch
->max_scissor
;
111 struct pipe_framebuffer_state
*pfb
= &batch
->framebuffer
;
112 uint32_t gmem_size
= ctx
->screen
->gmemsize_bytes
;
113 uint32_t minx
, miny
, width
, height
;
114 uint32_t nbins_x
= 1, nbins_y
= 1;
115 uint32_t bin_w
, bin_h
;
116 uint32_t max_width
= bin_width(ctx
->screen
);
117 uint8_t cbuf_cpp
[MAX_RENDER_TARGETS
] = {0}, zsbuf_cpp
[2] = {0};
118 uint32_t i
, j
, t
, xoff
, yoff
;
119 uint32_t tpp_x
, tpp_y
;
120 bool has_zs
= !!(batch
->resolve
& (FD_BUFFER_DEPTH
| FD_BUFFER_STENCIL
));
121 int tile_n
[ARRAY_SIZE(ctx
->pipe
)];
124 struct fd_resource
*rsc
= fd_resource(pfb
->zsbuf
->texture
);
125 zsbuf_cpp
[0] = rsc
->cpp
;
127 zsbuf_cpp
[1] = rsc
->stencil
->cpp
;
129 for (i
= 0; i
< pfb
->nr_cbufs
; i
++) {
131 cbuf_cpp
[i
] = util_format_get_blocksize(pfb
->cbufs
[i
]->format
);
136 if (!memcmp(gmem
->zsbuf_cpp
, zsbuf_cpp
, sizeof(zsbuf_cpp
)) &&
137 !memcmp(gmem
->cbuf_cpp
, cbuf_cpp
, sizeof(cbuf_cpp
)) &&
138 !memcmp(&gmem
->scissor
, scissor
, sizeof(gmem
->scissor
))) {
139 /* everything is up-to-date */
143 if (fd_mesa_debug
& FD_DBG_NOSCIS
) {
147 height
= pfb
->height
;
149 minx
= scissor
->minx
& ~31; /* round down to multiple of 32 */
150 miny
= scissor
->miny
& ~31;
151 width
= scissor
->maxx
- minx
;
152 height
= scissor
->maxy
- miny
;
155 bin_w
= align(width
, 32);
156 bin_h
= align(height
, 32);
158 /* first, find a bin width that satisfies the maximum width
161 while (bin_w
> max_width
) {
163 bin_w
= align(width
/ nbins_x
, 32);
166 if (fd_mesa_debug
& FD_DBG_MSGS
) {
167 debug_printf("binning input: cbuf cpp:");
168 for (i
= 0; i
< pfb
->nr_cbufs
; i
++)
169 debug_printf(" %d", cbuf_cpp
[i
]);
170 debug_printf(", zsbuf cpp: %d; %dx%d\n",
171 zsbuf_cpp
[0], width
, height
);
174 /* then find a bin width/height that satisfies the memory
177 while (total_size(cbuf_cpp
, zsbuf_cpp
, bin_w
, bin_h
, gmem
) > gmem_size
) {
180 bin_w
= align(width
/ nbins_x
, 32);
183 bin_h
= align(height
/ nbins_y
, 32);
187 DBG("using %d bins of size %dx%d", nbins_x
*nbins_y
, bin_w
, bin_h
);
189 gmem
->scissor
= *scissor
;
190 memcpy(gmem
->cbuf_cpp
, cbuf_cpp
, sizeof(cbuf_cpp
));
191 memcpy(gmem
->zsbuf_cpp
, zsbuf_cpp
, sizeof(zsbuf_cpp
));
194 gmem
->nbins_x
= nbins_x
;
195 gmem
->nbins_y
= nbins_y
;
199 gmem
->height
= height
;
202 * Assign tiles and pipes:
204 * At some point it might be worth playing with different
205 * strategies and seeing if that makes much impact on
209 #define div_round_up(v, a) (((v) + (a) - 1) / (a))
210 /* figure out number of tiles per pipe: */
212 while (div_round_up(nbins_y
, tpp_y
) > 8)
214 while ((div_round_up(nbins_y
, tpp_y
) *
215 div_round_up(nbins_x
, tpp_x
)) > 8)
218 /* configure pipes: */
220 for (i
= 0; i
< ARRAY_SIZE(ctx
->pipe
); i
++) {
221 struct fd_vsc_pipe
*pipe
= &ctx
->pipe
[i
];
223 if (xoff
>= nbins_x
) {
228 if (yoff
>= nbins_y
) {
234 pipe
->w
= MIN2(tpp_x
, nbins_x
- xoff
);
235 pipe
->h
= MIN2(tpp_y
, nbins_y
- yoff
);
240 for (; i
< ARRAY_SIZE(ctx
->pipe
); i
++) {
241 struct fd_vsc_pipe
*pipe
= &ctx
->pipe
[i
];
242 pipe
->x
= pipe
->y
= pipe
->w
= pipe
->h
= 0;
246 printf("%dx%d ... tpp=%dx%d\n", nbins_x
, nbins_y
, tpp_x
, tpp_y
);
247 for (i
= 0; i
< 8; i
++) {
248 struct fd_vsc_pipe
*pipe
= &ctx
->pipe
[i
];
249 printf("pipe[%d]: %ux%u @ %u,%u\n", i
,
250 pipe
->w
, pipe
->h
, pipe
->x
, pipe
->y
);
254 /* configure tiles: */
257 memset(tile_n
, 0, sizeof(tile_n
));
258 for (i
= 0; i
< nbins_y
; i
++) {
263 /* clip bin height: */
264 bh
= MIN2(bin_h
, miny
+ height
- yoff
);
266 for (j
= 0; j
< nbins_x
; j
++) {
267 struct fd_tile
*tile
= &ctx
->tile
[t
];
270 assert(t
< ARRAY_SIZE(ctx
->tile
));
273 p
= ((i
/ tpp_y
) * div_round_up(nbins_x
, tpp_x
)) + (j
/ tpp_x
);
275 /* clip bin width: */
276 bw
= MIN2(bin_w
, minx
+ width
- xoff
);
278 tile
->n
= tile_n
[p
]++;
295 for (i
= 0; i
< nbins_y
; i
++) {
296 for (j
= 0; j
< nbins_x
; j
++) {
297 struct fd_tile
*tile
= &ctx
->tile
[t
++];
298 printf("|p:%u n:%u|", tile
->p
, tile
->n
);
306 render_tiles(struct fd_batch
*batch
)
308 struct fd_context
*ctx
= batch
->ctx
;
309 struct fd_gmem_stateobj
*gmem
= &ctx
->gmem
;
312 ctx
->emit_tile_init(batch
);
315 ctx
->stats
.batch_restore
++;
317 for (i
= 0; i
< (gmem
->nbins_x
* gmem
->nbins_y
); i
++) {
318 struct fd_tile
*tile
= &ctx
->tile
[i
];
320 DBG("bin_h=%d, yoff=%d, bin_w=%d, xoff=%d",
321 tile
->bin_h
, tile
->yoff
, tile
->bin_w
, tile
->xoff
);
323 ctx
->emit_tile_prep(batch
, tile
);
325 if (batch
->restore
) {
326 fd_hw_query_set_stage(ctx
, batch
->gmem
, FD_STAGE_MEM2GMEM
);
327 ctx
->emit_tile_mem2gmem(batch
, tile
);
328 fd_hw_query_set_stage(ctx
, batch
->gmem
, FD_STAGE_NULL
);
331 ctx
->emit_tile_renderprep(batch
, tile
);
333 fd_hw_query_prepare_tile(ctx
, i
, batch
->gmem
);
335 /* emit IB to drawcmds: */
336 ctx
->emit_ib(batch
->gmem
, batch
->draw
);
339 /* emit gmem2mem to transfer tile back to system memory: */
340 fd_hw_query_set_stage(ctx
, batch
->gmem
, FD_STAGE_GMEM2MEM
);
341 ctx
->emit_tile_gmem2mem(batch
, tile
);
342 fd_hw_query_set_stage(ctx
, batch
->gmem
, FD_STAGE_NULL
);
347 render_sysmem(struct fd_batch
*batch
)
349 struct fd_context
*ctx
= batch
->ctx
;
351 ctx
->emit_sysmem_prep(batch
);
353 fd_hw_query_prepare_tile(ctx
, 0, batch
->gmem
);
355 /* emit IB to drawcmds: */
356 ctx
->emit_ib(batch
->gmem
, batch
->draw
);
361 fd_gmem_render_tiles(struct fd_batch
*batch
)
363 struct fd_context
*ctx
= batch
->ctx
;
364 struct pipe_framebuffer_state
*pfb
= &batch
->framebuffer
;
367 if (ctx
->emit_sysmem_prep
) {
368 if (batch
->cleared
|| batch
->gmem_reason
|| (batch
->num_draws
> 5)) {
369 DBG("GMEM: cleared=%x, gmem_reason=%x, num_draws=%u",
370 batch
->cleared
, batch
->gmem_reason
, batch
->num_draws
);
371 } else if (!(fd_mesa_debug
& FD_DBG_NOBYPASS
)) {
376 /* close out the draw cmds by making sure any active queries are
379 fd_hw_query_set_stage(ctx
, batch
->draw
, FD_STAGE_NULL
);
383 ctx
->stats
.batch_total
++;
386 DBG("rendering sysmem (%s/%s)",
387 util_format_short_name(pipe_surface_format(pfb
->cbufs
[0])),
388 util_format_short_name(pipe_surface_format(pfb
->zsbuf
)));
389 fd_hw_query_prepare(ctx
, 1);
390 render_sysmem(batch
);
391 ctx
->stats
.batch_sysmem
++;
393 struct fd_gmem_stateobj
*gmem
= &ctx
->gmem
;
394 calculate_tiles(batch
);
395 DBG("rendering %dx%d tiles (%s/%s)", gmem
->nbins_x
, gmem
->nbins_y
,
396 util_format_short_name(pipe_surface_format(pfb
->cbufs
[0])),
397 util_format_short_name(pipe_surface_format(pfb
->zsbuf
)));
398 fd_hw_query_prepare(ctx
, gmem
->nbins_x
* gmem
->nbins_y
);
400 ctx
->stats
.batch_gmem
++;
403 fd_ringbuffer_flush(batch
->gmem
);
410 /* tile needs restore if it isn't completely contained within the
414 skip_restore(struct pipe_scissor_state
*scissor
, struct fd_tile
*tile
)
416 unsigned minx
= tile
->xoff
;
417 unsigned maxx
= tile
->xoff
+ tile
->bin_w
;
418 unsigned miny
= tile
->yoff
;
419 unsigned maxy
= tile
->yoff
+ tile
->bin_h
;
420 return (minx
>= scissor
->minx
) && (maxx
<= scissor
->maxx
) &&
421 (miny
>= scissor
->miny
) && (maxy
<= scissor
->maxy
);
424 /* When deciding whether a tile needs mem2gmem, we need to take into
425 * account the scissor rect(s) that were cleared. To simplify we only
426 * consider the last scissor rect for each buffer, since the common
427 * case would be a single clear.
430 fd_gmem_needs_restore(struct fd_batch
*batch
, struct fd_tile
*tile
,
433 if (!(batch
->restore
& buffers
))
436 /* if buffers partially cleared, then slow-path to figure out
437 * if this particular tile needs restoring:
439 if ((buffers
& FD_BUFFER_COLOR
) &&
440 (batch
->partial_cleared
& FD_BUFFER_COLOR
) &&
441 skip_restore(&batch
->cleared_scissor
.color
, tile
))
443 if ((buffers
& FD_BUFFER_DEPTH
) &&
444 (batch
->partial_cleared
& FD_BUFFER_DEPTH
) &&
445 skip_restore(&batch
->cleared_scissor
.depth
, tile
))
447 if ((buffers
& FD_BUFFER_STENCIL
) &&
448 (batch
->partial_cleared
& FD_BUFFER_STENCIL
) &&
449 skip_restore(&batch
->cleared_scissor
.stencil
, tile
))