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