freedreno_gmem.c revision 848b8605
1/* -*- mode: C; c-file-style: "k&r"; tab-width 4; indent-tabs-mode: t; -*- */
2
3/*
4 * Copyright (C) 2012 Rob Clark <robclark@freedesktop.org>
5 *
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:
12 *
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
15 * Software.
16 *
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
23 * SOFTWARE.
24 *
25 * Authors:
26 *    Rob Clark <robclark@freedesktop.org>
27 */
28
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"
34
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"
40
41/*
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).
50 *
51 * The code in this file handles dealing with GMEM and tiling.
52 *
53 * The structure of the ringbuffer ends up being:
54 *
55 *     +--<---<-- IB ---<---+---<---+---<---<---<--+
56 *     |                    |       |              |
57 *     v                    ^       ^              ^
58 *   ------------------------------------------------------
59 *     | clear/draw cmds | Tile0 | Tile1 | .... | TileN |
60 *   ------------------------------------------------------
61 *                       ^
62 *                       |
63 *                       address submitted in issueibcmds
64 *
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
67 * resolve.
68 */
69
70static uint32_t bin_width(struct fd_context *ctx)
71{
72	if (ctx->screen->gpu_id >= 300)
73		return 992;
74	return 512;
75}
76
77static void
78calculate_tiles(struct fd_context *ctx)
79{
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);
88	uint32_t cpp = 4;
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));
92
93	if (pfb->cbufs[0])
94		cpp = util_format_get_blocksize(pfb->cbufs[0]->format);
95
96	if ((gmem->cpp == cpp) && (gmem->has_zs == has_zs) &&
97			!memcmp(&gmem->scissor, scissor, sizeof(gmem->scissor))) {
98		/* everything is up-to-date */
99		return;
100	}
101
102	/* if have depth/stencil, we need to leave room: */
103	if (has_zs) {
104		gmem_size /= 2;
105		max_width /= 2;
106	}
107
108	if (fd_mesa_debug & FD_DBG_DSCIS) {
109		minx = 0;
110		miny = 0;
111		width = pfb->width;
112		height = pfb->height;
113	} else {
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;
118	}
119
120	bin_w = align(width, 32);
121	bin_h = align(height, 32);
122
123	/* first, find a bin width that satisfies the maximum width
124	 * restrictions:
125	 */
126	while (bin_w > max_width) {
127		nbins_x++;
128		bin_w = align(width / nbins_x, 32);
129	}
130
131	/* then find a bin width/height that satisfies the memory
132	 * constraints:
133	 */
134	while ((bin_w * bin_h * cpp) > gmem_size) {
135		if (bin_w > bin_h) {
136			nbins_x++;
137			bin_w = align(width / nbins_x, 32);
138		} else {
139			nbins_y++;
140			bin_h = align(height / nbins_y, 32);
141		}
142	}
143
144	DBG("using %d bins of size %dx%d", nbins_x*nbins_y, bin_w, bin_h);
145
146	gmem->scissor = *scissor;
147	gmem->cpp = cpp;
148	gmem->has_zs = has_zs;
149	gmem->bin_h = bin_h;
150	gmem->bin_w = bin_w;
151	gmem->nbins_x = nbins_x;
152	gmem->nbins_y = nbins_y;
153	gmem->minx = minx;
154	gmem->miny = miny;
155	gmem->width = width;
156	gmem->height = height;
157
158	/*
159	 * Assign tiles and pipes:
160	 *
161	 * At some point it might be worth playing with different
162	 * strategies and seeing if that makes much impact on
163	 * performance.
164	 */
165
166#define div_round_up(v, a)  (((v) + (a) - 1) / (a))
167	/* figure out number of tiles per pipe: */
168	tpp_x = tpp_y = 1;
169	while (div_round_up(nbins_y, tpp_y) > 8)
170		tpp_y += 2;
171	while ((div_round_up(nbins_y, tpp_y) *
172			div_round_up(nbins_x, tpp_x)) > 8)
173		tpp_x += 1;
174
175	/* configure pipes: */
176	xoff = yoff = 0;
177	for (i = 0; i < ARRAY_SIZE(ctx->pipe); i++) {
178		struct fd_vsc_pipe *pipe = &ctx->pipe[i];
179
180		if (xoff >= nbins_x) {
181			xoff = 0;
182			yoff += tpp_y;
183		}
184
185		if (yoff >= nbins_y) {
186			break;
187		}
188
189		pipe->x = xoff;
190		pipe->y = yoff;
191		pipe->w = MIN2(tpp_x, nbins_x - xoff);
192		pipe->h = MIN2(tpp_y, nbins_y - yoff);
193
194		xoff += tpp_x;
195	}
196
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;
200	}
201
202#if 0 /* debug */
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);
208	}
209#endif
210
211	/* configure tiles: */
212	t = 0;
213	yoff = miny;
214	for (i = 0; i < nbins_y; i++) {
215		uint32_t bw, bh;
216
217		xoff = minx;
218
219		/* clip bin height: */
220		bh = MIN2(bin_h, miny + height - yoff);
221
222		for (j = 0; j < nbins_x; j++) {
223			struct fd_tile *tile = &ctx->tile[t];
224			uint32_t n, p;
225
226			assert(t < ARRAY_SIZE(ctx->tile));
227
228			/* pipe number: */
229			p = ((i / tpp_y) * div_round_up(nbins_x, tpp_x)) + (j / tpp_x);
230
231			/* slot number: */
232			n = ((i % tpp_y) * tpp_x) + (j % tpp_x);
233
234			/* clip bin width: */
235			bw = MIN2(bin_w, minx + width - xoff);
236
237			tile->n = n;
238			tile->p = p;
239			tile->bin_w = bw;
240			tile->bin_h = bh;
241			tile->xoff = xoff;
242			tile->yoff = yoff;
243
244			t++;
245
246			xoff += bw;
247		}
248
249		yoff += bh;
250	}
251
252#if 0 /* debug */
253	t = 0;
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);
258		}
259		printf("\n");
260	}
261#endif
262}
263
264static void
265render_tiles(struct fd_context *ctx)
266{
267	struct fd_gmem_stateobj *gmem = &ctx->gmem;
268	int i;
269
270	ctx->emit_tile_init(ctx);
271
272	if (ctx->restore)
273		ctx->stats.batch_restore++;
274
275	for (i = 0; i < (gmem->nbins_x * gmem->nbins_y); i++) {
276		struct fd_tile *tile = &ctx->tile[i];
277
278		DBG("bin_h=%d, yoff=%d, bin_w=%d, xoff=%d",
279			tile->bin_h, tile->yoff, tile->bin_w, tile->xoff);
280
281		ctx->emit_tile_prep(ctx, tile);
282
283		if (ctx->restore) {
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);
287		}
288
289		ctx->emit_tile_renderprep(ctx, tile);
290
291		fd_hw_query_prepare_tile(ctx, i, ctx->ring);
292
293		/* emit IB to drawcmds: */
294		OUT_IB(ctx->ring, ctx->draw_start, ctx->draw_end);
295		fd_reset_wfi(ctx);
296
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);
301	}
302}
303
304static void
305render_sysmem(struct fd_context *ctx)
306{
307	ctx->emit_sysmem_prep(ctx);
308
309	fd_hw_query_prepare_tile(ctx, 0, ctx->ring);
310
311	/* emit IB to drawcmds: */
312	OUT_IB(ctx->ring, ctx->draw_start, ctx->draw_end);
313	fd_reset_wfi(ctx);
314}
315
316void
317fd_gmem_render_tiles(struct fd_context *ctx)
318{
319	struct pipe_framebuffer_state *pfb = &ctx->framebuffer;
320	uint32_t timestamp = 0;
321	bool sysmem = false;
322
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)) {
328			sysmem = true;
329		}
330	}
331
332	/* close out the draw cmds by making sure any active queries are
333	 * paused:
334	 */
335	fd_hw_query_set_stage(ctx, ctx->ring, FD_STAGE_NULL);
336
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);
340
341	fd_reset_wfi(ctx);
342
343	ctx->stats.batch_total++;
344
345	if (sysmem) {
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);
350		render_sysmem(ctx);
351		ctx->stats.batch_sysmem++;
352	} else {
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);
359		render_tiles(ctx);
360		ctx->stats.batch_gmem++;
361	}
362
363	/* GPU executes starting from tile cmds, which IB back to draw cmds: */
364	fd_ringmarker_flush(ctx->draw_end);
365
366	/* mark start for next draw/binning cmds: */
367	fd_ringmarker_mark(ctx->draw_start);
368	fd_ringmarker_mark(ctx->binning_start);
369
370	fd_reset_wfi(ctx);
371
372	/* update timestamps on render targets: */
373	timestamp = fd_ringbuffer_timestamp(ctx->ring);
374	if (pfb->cbufs[0])
375		fd_resource(pfb->cbufs[0]->texture)->timestamp = timestamp;
376	if (pfb->zsbuf)
377		fd_resource(pfb->zsbuf->texture)->timestamp = timestamp;
378
379	/* reset maximal bounds: */
380	ctx->max_scissor.minx = ctx->max_scissor.miny = ~0;
381	ctx->max_scissor.maxx = ctx->max_scissor.maxy = 0;
382
383	ctx->dirty = ~0;
384}
385
386/* tile needs restore if it isn't completely contained within the
387 * cleared scissor:
388 */
389static bool
390skip_restore(struct pipe_scissor_state *scissor, struct fd_tile *tile)
391{
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);
398}
399
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.
404 */
405bool
406fd_gmem_needs_restore(struct fd_context *ctx, struct fd_tile *tile,
407		uint32_t buffers)
408{
409	if (!(ctx->restore & buffers))
410		return false;
411
412	/* if buffers partially cleared, then slow-path to figure out
413	 * if this particular tile needs restoring:
414	 */
415	if ((buffers & FD_BUFFER_COLOR) &&
416			(ctx->partial_cleared & FD_BUFFER_COLOR) &&
417			skip_restore(&ctx->cleared_scissor.color, tile))
418		return false;
419	if ((buffers & FD_BUFFER_DEPTH) &&
420			(ctx->partial_cleared & FD_BUFFER_DEPTH) &&
421			skip_restore(&ctx->cleared_scissor.depth, tile))
422		return false;
423	if ((buffers & FD_BUFFER_STENCIL) &&
424			(ctx->partial_cleared & FD_BUFFER_STENCIL) &&
425			skip_restore(&ctx->cleared_scissor.stencil, tile))
426		return false;
427
428	return true;
429}
430