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