7f6c8476cdb1a8614cd477686d4ff02c75449580
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_context
*ctx
)
72 if (ctx
->screen
->gpu_id
>= 300)
78 calculate_tiles(struct fd_context
*ctx
)
80 struct fd_gmem_stateobj
*gmem
= &ctx
->gmem
;
81 struct pipe_scissor_state
*scissor
= &ctx
->max_scissor
;
82 struct pipe_framebuffer_state
*pfb
= &ctx
->framebuffer
;
83 uint32_t gmem_size
= ctx
->screen
->gmemsize_bytes
;
84 uint32_t minx
, miny
, width
, height
;
85 uint32_t nbins_x
= 1, nbins_y
= 1;
86 uint32_t bin_w
, bin_h
;
87 uint32_t max_width
= bin_width(ctx
);
89 uint32_t i
, j
, t
, xoff
, yoff
;
90 uint32_t tpp_x
, tpp_y
;
91 bool has_zs
= !!(ctx
->resolve
& (FD_BUFFER_DEPTH
| FD_BUFFER_STENCIL
));
94 cpp
= util_format_get_blocksize(pfb
->cbufs
[0]->format
);
96 if ((gmem
->cpp
== cpp
) && (gmem
->has_zs
== has_zs
) &&
97 !memcmp(&gmem
->scissor
, scissor
, sizeof(gmem
->scissor
))) {
98 /* everything is up-to-date */
102 /* if have depth/stencil, we need to leave room: */
108 if (fd_mesa_debug
& FD_DBG_DSCIS
) {
112 height
= pfb
->height
;
114 minx
= scissor
->minx
& ~31; /* round down to multiple of 32 */
115 miny
= scissor
->miny
& ~31;
116 width
= scissor
->maxx
- minx
;
117 height
= scissor
->maxy
- miny
;
120 bin_w
= align(width
, 32);
121 bin_h
= align(height
, 32);
123 /* first, find a bin width that satisfies the maximum width
126 while (bin_w
> max_width
) {
128 bin_w
= align(width
/ nbins_x
, 32);
131 /* then find a bin width/height that satisfies the memory
134 while ((bin_w
* bin_h
* cpp
) > gmem_size
) {
137 bin_w
= align(width
/ nbins_x
, 32);
140 bin_h
= align(height
/ nbins_y
, 32);
144 DBG("using %d bins of size %dx%d", nbins_x
*nbins_y
, bin_w
, bin_h
);
146 gmem
->scissor
= *scissor
;
148 gmem
->has_zs
= has_zs
;
151 gmem
->nbins_x
= nbins_x
;
152 gmem
->nbins_y
= nbins_y
;
156 gmem
->height
= height
;
159 * Assign tiles and pipes:
161 * At some point it might be worth playing with different
162 * strategies and seeing if that makes much impact on
166 #define div_round_up(v, a) (((v) + (a) - 1) / (a))
167 /* figure out number of tiles per pipe: */
169 while (div_round_up(nbins_y
, tpp_y
) > 8)
171 while ((div_round_up(nbins_y
, tpp_y
) *
172 div_round_up(nbins_x
, tpp_x
)) > 8)
175 /* configure pipes: */
177 for (i
= 0; i
< ARRAY_SIZE(ctx
->pipe
); i
++) {
178 struct fd_vsc_pipe
*pipe
= &ctx
->pipe
[i
];
180 if (xoff
>= nbins_x
) {
185 if (yoff
>= nbins_y
) {
191 pipe
->w
= MIN2(tpp_x
, nbins_x
- xoff
);
192 pipe
->h
= MIN2(tpp_y
, nbins_y
- yoff
);
197 for (; i
< ARRAY_SIZE(ctx
->pipe
); i
++) {
198 struct fd_vsc_pipe
*pipe
= &ctx
->pipe
[i
];
199 pipe
->x
= pipe
->y
= pipe
->w
= pipe
->h
= 0;
203 printf("%dx%d ... tpp=%dx%d\n", nbins_x
, nbins_y
, tpp_x
, tpp_y
);
204 for (i
= 0; i
< 8; i
++) {
205 struct fd_vsc_pipe
*pipe
= &ctx
->pipe
[i
];
206 printf("pipe[%d]: %ux%u @ %u,%u\n", i
,
207 pipe
->w
, pipe
->h
, pipe
->x
, pipe
->y
);
211 /* configure tiles: */
214 for (i
= 0; i
< nbins_y
; i
++) {
219 /* clip bin height: */
220 bh
= MIN2(bin_h
, miny
+ height
- yoff
);
222 for (j
= 0; j
< nbins_x
; j
++) {
223 struct fd_tile
*tile
= &ctx
->tile
[t
];
226 assert(t
< ARRAY_SIZE(ctx
->tile
));
229 p
= ((i
/ tpp_y
) * div_round_up(nbins_x
, tpp_x
)) + (j
/ tpp_x
);
232 n
= ((i
% tpp_y
) * tpp_x
) + (j
% tpp_x
);
234 /* clip bin width: */
235 bw
= MIN2(bin_w
, minx
+ width
- xoff
);
254 for (i
= 0; i
< nbins_y
; i
++) {
255 for (j
= 0; j
< nbins_x
; j
++) {
256 struct fd_tile
*tile
= &ctx
->tile
[t
++];
257 printf("|p:%u n:%u|", tile
->p
, tile
->n
);
265 render_tiles(struct fd_context
*ctx
)
267 struct fd_gmem_stateobj
*gmem
= &ctx
->gmem
;
270 ctx
->emit_tile_init(ctx
);
273 ctx
->stats
.batch_restore
++;
275 for (i
= 0; i
< (gmem
->nbins_x
* gmem
->nbins_y
); i
++) {
276 struct fd_tile
*tile
= &ctx
->tile
[i
];
278 DBG("bin_h=%d, yoff=%d, bin_w=%d, xoff=%d",
279 tile
->bin_h
, tile
->yoff
, tile
->bin_w
, tile
->xoff
);
281 ctx
->emit_tile_prep(ctx
, tile
);
284 fd_hw_query_set_stage(ctx
, ctx
->ring
, FD_STAGE_MEM2GMEM
);
285 ctx
->emit_tile_mem2gmem(ctx
, tile
);
286 fd_hw_query_set_stage(ctx
, ctx
->ring
, FD_STAGE_NULL
);
289 ctx
->emit_tile_renderprep(ctx
, tile
);
291 fd_hw_query_prepare_tile(ctx
, i
, ctx
->ring
);
293 /* emit IB to drawcmds: */
294 OUT_IB(ctx
->ring
, ctx
->draw_start
, ctx
->draw_end
);
297 /* emit gmem2mem to transfer tile back to system memory: */
298 fd_hw_query_set_stage(ctx
, ctx
->ring
, FD_STAGE_GMEM2MEM
);
299 ctx
->emit_tile_gmem2mem(ctx
, tile
);
300 fd_hw_query_set_stage(ctx
, ctx
->ring
, FD_STAGE_NULL
);
305 render_sysmem(struct fd_context
*ctx
)
307 ctx
->emit_sysmem_prep(ctx
);
309 fd_hw_query_prepare_tile(ctx
, 0, ctx
->ring
);
311 /* emit IB to drawcmds: */
312 OUT_IB(ctx
->ring
, ctx
->draw_start
, ctx
->draw_end
);
317 fd_gmem_render_tiles(struct fd_context
*ctx
)
319 struct pipe_framebuffer_state
*pfb
= &ctx
->framebuffer
;
320 uint32_t timestamp
= 0;
323 if (ctx
->emit_sysmem_prep
) {
324 if (ctx
->cleared
|| ctx
->gmem_reason
|| (ctx
->num_draws
> 5)) {
325 DBG("GMEM: cleared=%x, gmem_reason=%x, num_draws=%u",
326 ctx
->cleared
, ctx
->gmem_reason
, ctx
->num_draws
);
327 } else if (!(fd_mesa_debug
& FD_DBG_DBYPASS
)) {
332 /* close out the draw cmds by making sure any active queries are
335 fd_hw_query_set_stage(ctx
, ctx
->ring
, FD_STAGE_NULL
);
337 /* mark the end of the clear/draw cmds before emitting per-tile cmds: */
338 fd_ringmarker_mark(ctx
->draw_end
);
339 fd_ringmarker_mark(ctx
->binning_end
);
343 ctx
->stats
.batch_total
++;
346 DBG("rendering sysmem (%s/%s)",
347 util_format_short_name(pipe_surface_format(pfb
->cbufs
[0])),
348 util_format_short_name(pipe_surface_format(pfb
->zsbuf
)));
349 fd_hw_query_prepare(ctx
, 1);
351 ctx
->stats
.batch_sysmem
++;
353 struct fd_gmem_stateobj
*gmem
= &ctx
->gmem
;
354 calculate_tiles(ctx
);
355 DBG("rendering %dx%d tiles (%s/%s)", gmem
->nbins_x
, gmem
->nbins_y
,
356 util_format_short_name(pipe_surface_format(pfb
->cbufs
[0])),
357 util_format_short_name(pipe_surface_format(pfb
->zsbuf
)));
358 fd_hw_query_prepare(ctx
, gmem
->nbins_x
* gmem
->nbins_y
);
360 ctx
->stats
.batch_gmem
++;
363 /* GPU executes starting from tile cmds, which IB back to draw cmds: */
364 fd_ringmarker_flush(ctx
->draw_end
);
366 /* mark start for next draw/binning cmds: */
367 fd_ringmarker_mark(ctx
->draw_start
);
368 fd_ringmarker_mark(ctx
->binning_start
);
372 /* update timestamps on render targets: */
373 timestamp
= fd_ringbuffer_timestamp(ctx
->ring
);
375 fd_resource(pfb
->cbufs
[0]->texture
)->timestamp
= timestamp
;
377 fd_resource(pfb
->zsbuf
->texture
)->timestamp
= timestamp
;
379 /* reset maximal bounds: */
380 ctx
->max_scissor
.minx
= ctx
->max_scissor
.miny
= ~0;
381 ctx
->max_scissor
.maxx
= ctx
->max_scissor
.maxy
= 0;
386 /* tile needs restore if it isn't completely contained within the
390 skip_restore(struct pipe_scissor_state
*scissor
, struct fd_tile
*tile
)
392 unsigned minx
= tile
->xoff
;
393 unsigned maxx
= tile
->xoff
+ tile
->bin_w
;
394 unsigned miny
= tile
->yoff
;
395 unsigned maxy
= tile
->yoff
+ tile
->bin_h
;
396 return (minx
>= scissor
->minx
) && (maxx
<= scissor
->maxx
) &&
397 (miny
>= scissor
->miny
) && (maxy
<= scissor
->maxy
);
400 /* When deciding whether a tile needs mem2gmem, we need to take into
401 * account the scissor rect(s) that were cleared. To simplify we only
402 * consider the last scissor rect for each buffer, since the common
403 * case would be a single clear.
406 fd_gmem_needs_restore(struct fd_context
*ctx
, struct fd_tile
*tile
,
409 if (!(ctx
->restore
& buffers
))
412 /* if buffers partially cleared, then slow-path to figure out
413 * if this particular tile needs restoring:
415 if ((buffers
& FD_BUFFER_COLOR
) &&
416 (ctx
->partial_cleared
& FD_BUFFER_COLOR
) &&
417 skip_restore(&ctx
->cleared_scissor
.color
, tile
))
419 if ((buffers
& FD_BUFFER_DEPTH
) &&
420 (ctx
->partial_cleared
& FD_BUFFER_DEPTH
) &&
421 skip_restore(&ctx
->cleared_scissor
.depth
, tile
))
423 if ((buffers
& FD_BUFFER_STENCIL
) &&
424 (ctx
->partial_cleared
& FD_BUFFER_STENCIL
) &&
425 skip_restore(&ctx
->cleared_scissor
.stencil
, tile
))