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