freedreno: push resource tracking down into batch
[mesa.git] / src / gallium / drivers / freedreno / freedreno_draw.c
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_prim.h"
33 #include "util/u_format.h"
34
35 #include "freedreno_draw.h"
36 #include "freedreno_context.h"
37 #include "freedreno_state.h"
38 #include "freedreno_resource.h"
39 #include "freedreno_query_hw.h"
40 #include "freedreno_util.h"
41
42 static void
43 resource_read(struct fd_context *ctx, struct pipe_resource *prsc)
44 {
45 if (!prsc)
46 return;
47 fd_batch_resource_used(ctx->batch, fd_resource(prsc), FD_PENDING_READ);
48 }
49
50 static void
51 resource_written(struct fd_context *ctx, struct pipe_resource *prsc)
52 {
53 if (!prsc)
54 return;
55 fd_batch_resource_used(ctx->batch, fd_resource(prsc), FD_PENDING_WRITE);
56 }
57
58 static void
59 fd_draw_vbo(struct pipe_context *pctx, const struct pipe_draw_info *info)
60 {
61 struct fd_context *ctx = fd_context(pctx);
62 struct pipe_framebuffer_state *pfb = &ctx->framebuffer;
63 struct pipe_scissor_state *scissor = fd_context_get_scissor(ctx);
64 unsigned i, prims, buffers = 0;
65
66 /* if we supported transform feedback, we'd have to disable this: */
67 if (((scissor->maxx - scissor->minx) *
68 (scissor->maxy - scissor->miny)) == 0) {
69 return;
70 }
71
72 /* TODO: push down the region versions into the tiles */
73 if (!fd_render_condition_check(pctx))
74 return;
75
76 /* emulate unsupported primitives: */
77 if (!fd_supported_prim(ctx, info->mode)) {
78 if (ctx->streamout.num_targets > 0)
79 debug_error("stream-out with emulated prims");
80 util_primconvert_save_index_buffer(ctx->primconvert, &ctx->indexbuf);
81 util_primconvert_save_rasterizer_state(ctx->primconvert, ctx->rasterizer);
82 util_primconvert_draw_vbo(ctx->primconvert, info);
83 return;
84 }
85
86 /*
87 * Figure out the buffers/features we need:
88 */
89
90 if (fd_depth_enabled(ctx)) {
91 buffers |= FD_BUFFER_DEPTH;
92 resource_written(ctx, pfb->zsbuf->texture);
93 ctx->gmem_reason |= FD_GMEM_DEPTH_ENABLED;
94 }
95
96 if (fd_stencil_enabled(ctx)) {
97 buffers |= FD_BUFFER_STENCIL;
98 resource_written(ctx, pfb->zsbuf->texture);
99 ctx->gmem_reason |= FD_GMEM_STENCIL_ENABLED;
100 }
101
102 if (fd_logicop_enabled(ctx))
103 ctx->gmem_reason |= FD_GMEM_LOGICOP_ENABLED;
104
105 for (i = 0; i < pfb->nr_cbufs; i++) {
106 struct pipe_resource *surf;
107
108 if (!pfb->cbufs[i])
109 continue;
110
111 surf = pfb->cbufs[i]->texture;
112
113 resource_written(ctx, surf);
114 buffers |= PIPE_CLEAR_COLOR0 << i;
115
116 if (surf->nr_samples > 1)
117 ctx->gmem_reason |= FD_GMEM_MSAA_ENABLED;
118
119 if (fd_blend_enabled(ctx, i))
120 ctx->gmem_reason |= FD_GMEM_BLEND_ENABLED;
121 }
122
123 /* Skip over buffer 0, that is sent along with the command stream */
124 for (i = 1; i < PIPE_MAX_CONSTANT_BUFFERS; i++) {
125 resource_read(ctx, ctx->constbuf[PIPE_SHADER_VERTEX].cb[i].buffer);
126 resource_read(ctx, ctx->constbuf[PIPE_SHADER_FRAGMENT].cb[i].buffer);
127 }
128
129 /* Mark VBOs as being read */
130 for (i = 0; i < ctx->vtx.vertexbuf.count; i++) {
131 assert(!ctx->vtx.vertexbuf.vb[i].user_buffer);
132 resource_read(ctx, ctx->vtx.vertexbuf.vb[i].buffer);
133 }
134
135 /* Mark index buffer as being read */
136 resource_read(ctx, ctx->indexbuf.buffer);
137
138 /* Mark textures as being read */
139 for (i = 0; i < ctx->verttex.num_textures; i++)
140 if (ctx->verttex.textures[i])
141 resource_read(ctx, ctx->verttex.textures[i]->texture);
142 for (i = 0; i < ctx->fragtex.num_textures; i++)
143 if (ctx->fragtex.textures[i])
144 resource_read(ctx, ctx->fragtex.textures[i]->texture);
145
146 /* Mark streamout buffers as being written.. */
147 for (i = 0; i < ctx->streamout.num_targets; i++)
148 if (ctx->streamout.targets[i])
149 resource_written(ctx, ctx->streamout.targets[i]->buffer);
150
151 ctx->num_draws++;
152
153 prims = u_reduced_prims_for_vertices(info->mode, info->count);
154
155 ctx->stats.draw_calls++;
156
157 /* TODO prims_emitted should be clipped when the stream-out buffer is
158 * not large enough. See max_tf_vtx().. probably need to move that
159 * into common code. Although a bit more annoying since a2xx doesn't
160 * use ir3 so no common way to get at the pipe_stream_output_info
161 * which is needed for this calculation.
162 */
163 if (ctx->streamout.num_targets > 0)
164 ctx->stats.prims_emitted += prims;
165 ctx->stats.prims_generated += prims;
166
167 /* any buffers that haven't been cleared yet, we need to restore: */
168 ctx->restore |= buffers & (FD_BUFFER_ALL & ~ctx->cleared);
169 /* and any buffers used, need to be resolved: */
170 ctx->resolve |= buffers;
171
172 DBG("%x num_draws=%u (%s/%s)", buffers, ctx->num_draws,
173 util_format_short_name(pipe_surface_format(pfb->cbufs[0])),
174 util_format_short_name(pipe_surface_format(pfb->zsbuf)));
175
176 fd_hw_query_set_stage(ctx, ctx->batch->draw, FD_STAGE_DRAW);
177 if (ctx->draw_vbo(ctx, info))
178 ctx->needs_flush = true;
179
180 for (i = 0; i < ctx->streamout.num_targets; i++)
181 ctx->streamout.offsets[i] += info->count;
182
183 if (fd_mesa_debug & FD_DBG_DDRAW)
184 ctx->dirty = 0xffffffff;
185
186 fd_batch_check_size(ctx->batch);
187 }
188
189 /* TODO figure out how to make better use of existing state mechanism
190 * for clear (and possibly gmem->mem / mem->gmem) so we can (a) keep
191 * track of what state really actually changes, and (b) reduce the code
192 * in the a2xx/a3xx parts.
193 */
194
195 static void
196 fd_clear(struct pipe_context *pctx, unsigned buffers,
197 const union pipe_color_union *color, double depth, unsigned stencil)
198 {
199 struct fd_context *ctx = fd_context(pctx);
200 struct pipe_framebuffer_state *pfb = &ctx->framebuffer;
201 struct pipe_scissor_state *scissor = fd_context_get_scissor(ctx);
202 unsigned cleared_buffers;
203 int i;
204
205 /* TODO: push down the region versions into the tiles */
206 if (!fd_render_condition_check(pctx))
207 return;
208
209 /* for bookkeeping about which buffers have been cleared (and thus
210 * can fully or partially skip mem2gmem) we need to ignore buffers
211 * that have already had a draw, in case apps do silly things like
212 * clear after draw (ie. if you only clear the color buffer, but
213 * something like alpha-test causes side effects from the draw in
214 * the depth buffer, etc)
215 */
216 cleared_buffers = buffers & (FD_BUFFER_ALL & ~ctx->restore);
217
218 /* do we have full-screen scissor? */
219 if (!memcmp(scissor, &ctx->disabled_scissor, sizeof(*scissor))) {
220 ctx->cleared |= cleared_buffers;
221 } else {
222 ctx->partial_cleared |= cleared_buffers;
223 if (cleared_buffers & PIPE_CLEAR_COLOR)
224 ctx->cleared_scissor.color = *scissor;
225 if (cleared_buffers & PIPE_CLEAR_DEPTH)
226 ctx->cleared_scissor.depth = *scissor;
227 if (cleared_buffers & PIPE_CLEAR_STENCIL)
228 ctx->cleared_scissor.stencil = *scissor;
229 }
230 ctx->resolve |= buffers;
231 ctx->needs_flush = true;
232
233 if (buffers & PIPE_CLEAR_COLOR)
234 for (i = 0; i < pfb->nr_cbufs; i++)
235 if (buffers & (PIPE_CLEAR_COLOR0 << i))
236 resource_written(ctx, pfb->cbufs[i]->texture);
237
238 if (buffers & (PIPE_CLEAR_DEPTH | PIPE_CLEAR_STENCIL)) {
239 resource_written(ctx, pfb->zsbuf->texture);
240 ctx->gmem_reason |= FD_GMEM_CLEARS_DEPTH_STENCIL;
241 }
242
243 DBG("%x depth=%f, stencil=%u (%s/%s)", buffers, depth, stencil,
244 util_format_short_name(pipe_surface_format(pfb->cbufs[0])),
245 util_format_short_name(pipe_surface_format(pfb->zsbuf)));
246
247 fd_hw_query_set_stage(ctx, ctx->batch->draw, FD_STAGE_CLEAR);
248
249 ctx->clear(ctx, buffers, color, depth, stencil);
250
251 ctx->dirty |= FD_DIRTY_ZSA |
252 FD_DIRTY_VIEWPORT |
253 FD_DIRTY_RASTERIZER |
254 FD_DIRTY_SAMPLE_MASK |
255 FD_DIRTY_PROG |
256 FD_DIRTY_CONSTBUF |
257 FD_DIRTY_BLEND |
258 FD_DIRTY_FRAMEBUFFER;
259
260 if (fd_mesa_debug & FD_DBG_DCLEAR)
261 ctx->dirty = 0xffffffff;
262 }
263
264 static void
265 fd_clear_render_target(struct pipe_context *pctx, struct pipe_surface *ps,
266 const union pipe_color_union *color,
267 unsigned x, unsigned y, unsigned w, unsigned h)
268 {
269 DBG("TODO: x=%u, y=%u, w=%u, h=%u", x, y, w, h);
270 }
271
272 static void
273 fd_clear_depth_stencil(struct pipe_context *pctx, struct pipe_surface *ps,
274 unsigned buffers, double depth, unsigned stencil,
275 unsigned x, unsigned y, unsigned w, unsigned h)
276 {
277 DBG("TODO: buffers=%u, depth=%f, stencil=%u, x=%u, y=%u, w=%u, h=%u",
278 buffers, depth, stencil, x, y, w, h);
279 }
280
281 void
282 fd_draw_init(struct pipe_context *pctx)
283 {
284 pctx->draw_vbo = fd_draw_vbo;
285 pctx->clear = fd_clear;
286 pctx->clear_render_target = fd_clear_render_target;
287 pctx->clear_depth_stencil = fd_clear_depth_stencil;
288 }