5d5b7c11beb14b3fbccf0e828e573baa97ae2102
[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 #include "util/u_helpers.h"
35
36 #include "freedreno_draw.h"
37 #include "freedreno_context.h"
38 #include "freedreno_state.h"
39 #include "freedreno_resource.h"
40 #include "freedreno_query_hw.h"
41 #include "freedreno_util.h"
42
43 static void
44 resource_read(struct fd_batch *batch, struct pipe_resource *prsc)
45 {
46 if (!prsc)
47 return;
48 fd_batch_resource_used(batch, fd_resource(prsc), false);
49 }
50
51 static void
52 resource_written(struct fd_batch *batch, struct pipe_resource *prsc)
53 {
54 if (!prsc)
55 return;
56 fd_batch_resource_used(batch, fd_resource(prsc), true);
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 fd_batch *batch = ctx->batch;
64 struct pipe_framebuffer_state *pfb = &batch->framebuffer;
65 struct pipe_scissor_state *scissor = fd_context_get_scissor(ctx);
66 unsigned i, prims, buffers = 0;
67
68 /* if we supported transform feedback, we'd have to disable this: */
69 if (((scissor->maxx - scissor->minx) *
70 (scissor->maxy - scissor->miny)) == 0) {
71 return;
72 }
73
74 /* TODO: push down the region versions into the tiles */
75 if (!fd_render_condition_check(pctx))
76 return;
77
78 /* emulate unsupported primitives: */
79 if (!fd_supported_prim(ctx, info->mode)) {
80 if (ctx->streamout.num_targets > 0)
81 debug_error("stream-out with emulated prims");
82 util_primconvert_save_index_buffer(ctx->primconvert, &ctx->indexbuf);
83 util_primconvert_save_rasterizer_state(ctx->primconvert, ctx->rasterizer);
84 util_primconvert_draw_vbo(ctx->primconvert, info);
85 return;
86 }
87
88 /* Upload a user index buffer. */
89 struct pipe_index_buffer ibuffer_saved = {};
90 if (info->indexed && ctx->indexbuf.user_buffer &&
91 !util_save_and_upload_index_buffer(pctx, info, &ctx->indexbuf,
92 &ibuffer_saved)) {
93 return;
94 }
95
96 if (ctx->in_blit) {
97 fd_batch_reset(batch);
98 ctx->dirty = ~0;
99 }
100
101 batch->blit = ctx->in_blit;
102 batch->back_blit = ctx->in_shadow;
103
104 /* NOTE: needs to be before resource_written(batch->query_buf), otherwise
105 * query_buf may not be created yet.
106 */
107 fd_hw_query_set_stage(batch, batch->draw, FD_STAGE_DRAW);
108
109 /*
110 * Figure out the buffers/features we need:
111 */
112
113 pipe_mutex_lock(ctx->screen->lock);
114
115 if (fd_depth_enabled(ctx)) {
116 buffers |= FD_BUFFER_DEPTH;
117 resource_written(batch, pfb->zsbuf->texture);
118 batch->gmem_reason |= FD_GMEM_DEPTH_ENABLED;
119 }
120
121 if (fd_stencil_enabled(ctx)) {
122 buffers |= FD_BUFFER_STENCIL;
123 resource_written(batch, pfb->zsbuf->texture);
124 batch->gmem_reason |= FD_GMEM_STENCIL_ENABLED;
125 }
126
127 if (fd_logicop_enabled(ctx))
128 batch->gmem_reason |= FD_GMEM_LOGICOP_ENABLED;
129
130 for (i = 0; i < pfb->nr_cbufs; i++) {
131 struct pipe_resource *surf;
132
133 if (!pfb->cbufs[i])
134 continue;
135
136 surf = pfb->cbufs[i]->texture;
137
138 resource_written(batch, surf);
139 buffers |= PIPE_CLEAR_COLOR0 << i;
140
141 if (surf->nr_samples > 1)
142 batch->gmem_reason |= FD_GMEM_MSAA_ENABLED;
143
144 if (fd_blend_enabled(ctx, i))
145 batch->gmem_reason |= FD_GMEM_BLEND_ENABLED;
146 }
147
148 foreach_bit(i, ctx->constbuf[PIPE_SHADER_VERTEX].enabled_mask)
149 resource_read(batch, ctx->constbuf[PIPE_SHADER_VERTEX].cb[i].buffer);
150 foreach_bit(i, ctx->constbuf[PIPE_SHADER_FRAGMENT].enabled_mask)
151 resource_read(batch, ctx->constbuf[PIPE_SHADER_FRAGMENT].cb[i].buffer);
152
153 /* Mark VBOs as being read */
154 foreach_bit(i, ctx->vtx.vertexbuf.enabled_mask) {
155 assert(!ctx->vtx.vertexbuf.vb[i].user_buffer);
156 resource_read(batch, ctx->vtx.vertexbuf.vb[i].buffer);
157 }
158
159 /* Mark index buffer as being read */
160 resource_read(batch, ctx->indexbuf.buffer);
161
162 /* Mark textures as being read */
163 foreach_bit(i, ctx->verttex.valid_textures)
164 resource_read(batch, ctx->verttex.textures[i]->texture);
165 foreach_bit(i, ctx->fragtex.valid_textures)
166 resource_read(batch, ctx->fragtex.textures[i]->texture);
167
168 /* Mark streamout buffers as being written.. */
169 for (i = 0; i < ctx->streamout.num_targets; i++)
170 if (ctx->streamout.targets[i])
171 resource_written(batch, ctx->streamout.targets[i]->buffer);
172
173 resource_written(batch, batch->query_buf);
174
175 pipe_mutex_unlock(ctx->screen->lock);
176
177 batch->num_draws++;
178
179 prims = u_reduced_prims_for_vertices(info->mode, info->count);
180
181 ctx->stats.draw_calls++;
182
183 /* TODO prims_emitted should be clipped when the stream-out buffer is
184 * not large enough. See max_tf_vtx().. probably need to move that
185 * into common code. Although a bit more annoying since a2xx doesn't
186 * use ir3 so no common way to get at the pipe_stream_output_info
187 * which is needed for this calculation.
188 */
189 if (ctx->streamout.num_targets > 0)
190 ctx->stats.prims_emitted += prims;
191 ctx->stats.prims_generated += prims;
192
193 /* any buffers that haven't been cleared yet, we need to restore: */
194 batch->restore |= buffers & (FD_BUFFER_ALL & ~batch->cleared);
195 /* and any buffers used, need to be resolved: */
196 batch->resolve |= buffers;
197
198 DBG("%p: %x %ux%u num_draws=%u (%s/%s)", batch, buffers,
199 pfb->width, pfb->height, batch->num_draws,
200 util_format_short_name(pipe_surface_format(pfb->cbufs[0])),
201 util_format_short_name(pipe_surface_format(pfb->zsbuf)));
202
203 if (ctx->draw_vbo(ctx, info))
204 batch->needs_flush = true;
205
206 for (i = 0; i < ctx->streamout.num_targets; i++)
207 ctx->streamout.offsets[i] += info->count;
208
209 if (fd_mesa_debug & FD_DBG_DDRAW)
210 ctx->dirty = 0xffffffff;
211
212 fd_batch_check_size(batch);
213
214 if (info->indexed && ibuffer_saved.user_buffer)
215 pctx->set_index_buffer(pctx, &ibuffer_saved);
216 }
217
218 /* Generic clear implementation (partially) using u_blitter: */
219 static void
220 fd_blitter_clear(struct pipe_context *pctx, unsigned buffers,
221 const union pipe_color_union *color, double depth, unsigned stencil)
222 {
223 struct fd_context *ctx = fd_context(pctx);
224 struct pipe_framebuffer_state *pfb = &ctx->batch->framebuffer;
225 struct blitter_context *blitter = ctx->blitter;
226
227 fd_blitter_pipe_begin(ctx, false, true, FD_STAGE_CLEAR);
228
229 util_blitter_common_clear_setup(blitter, pfb->width, pfb->height,
230 buffers, NULL, NULL);
231
232 struct pipe_stencil_ref sr = {
233 .ref_value = { stencil & 0xff }
234 };
235 pctx->set_stencil_ref(pctx, &sr);
236
237 struct pipe_constant_buffer cb = {
238 .buffer_size = 16,
239 .user_buffer = &color->ui,
240 };
241 pctx->set_constant_buffer(pctx, PIPE_SHADER_FRAGMENT, 0, &cb);
242
243 if (!ctx->clear_rs_state) {
244 const struct pipe_rasterizer_state tmpl = {
245 .cull_face = PIPE_FACE_NONE,
246 .half_pixel_center = 1,
247 .bottom_edge_rule = 1,
248 .flatshade = 1,
249 .depth_clip = 1,
250 };
251 ctx->clear_rs_state = pctx->create_rasterizer_state(pctx, &tmpl);
252 }
253 pctx->bind_rasterizer_state(pctx, ctx->clear_rs_state);
254
255 struct pipe_viewport_state vp = {
256 .scale = { 0.5f * pfb->width, -0.5f * pfb->height, depth },
257 .translate = { 0.5f * pfb->width, 0.5f * pfb->height, 0.0f },
258 };
259 pctx->set_viewport_states(pctx, 0, 1, &vp);
260
261 pctx->bind_vertex_elements_state(pctx, ctx->solid_vbuf_state.vtx);
262 pctx->set_vertex_buffers(pctx, blitter->vb_slot, 1,
263 &ctx->solid_vbuf_state.vertexbuf.vb[0]);
264 pctx->set_stream_output_targets(pctx, 0, NULL, NULL);
265 pctx->bind_vs_state(pctx, ctx->solid_prog.vp);
266 pctx->bind_fs_state(pctx, ctx->solid_prog.fp);
267
268 struct pipe_draw_info info = {
269 .mode = PIPE_PRIM_MAX, /* maps to DI_PT_RECTLIST */
270 .count = 2,
271 .max_index = 1,
272 .instance_count = 1,
273 };
274 ctx->draw_vbo(ctx, &info);
275
276 util_blitter_restore_constant_buffer_state(blitter);
277 util_blitter_restore_vertex_states(blitter);
278 util_blitter_restore_fragment_states(blitter);
279 util_blitter_restore_render_cond(blitter);
280 util_blitter_unset_running_flag(blitter);
281
282 fd_blitter_pipe_end(ctx);
283 }
284
285 /* TODO figure out how to make better use of existing state mechanism
286 * for clear (and possibly gmem->mem / mem->gmem) so we can (a) keep
287 * track of what state really actually changes, and (b) reduce the code
288 * in the a2xx/a3xx parts.
289 */
290
291 static void
292 fd_clear(struct pipe_context *pctx, unsigned buffers,
293 const union pipe_color_union *color, double depth, unsigned stencil)
294 {
295 struct fd_context *ctx = fd_context(pctx);
296 struct fd_batch *batch = ctx->batch;
297 struct pipe_framebuffer_state *pfb = &batch->framebuffer;
298 struct pipe_scissor_state *scissor = fd_context_get_scissor(ctx);
299 unsigned cleared_buffers;
300 int i;
301
302 /* TODO: push down the region versions into the tiles */
303 if (!fd_render_condition_check(pctx))
304 return;
305
306 if (ctx->in_blit) {
307 fd_batch_reset(batch);
308 ctx->dirty = ~0;
309 }
310
311 /* for bookkeeping about which buffers have been cleared (and thus
312 * can fully or partially skip mem2gmem) we need to ignore buffers
313 * that have already had a draw, in case apps do silly things like
314 * clear after draw (ie. if you only clear the color buffer, but
315 * something like alpha-test causes side effects from the draw in
316 * the depth buffer, etc)
317 */
318 cleared_buffers = buffers & (FD_BUFFER_ALL & ~batch->restore);
319
320 /* do we have full-screen scissor? */
321 if (!memcmp(scissor, &ctx->disabled_scissor, sizeof(*scissor))) {
322 batch->cleared |= cleared_buffers;
323 } else {
324 batch->partial_cleared |= cleared_buffers;
325 if (cleared_buffers & PIPE_CLEAR_COLOR)
326 batch->cleared_scissor.color = *scissor;
327 if (cleared_buffers & PIPE_CLEAR_DEPTH)
328 batch->cleared_scissor.depth = *scissor;
329 if (cleared_buffers & PIPE_CLEAR_STENCIL)
330 batch->cleared_scissor.stencil = *scissor;
331 }
332 batch->resolve |= buffers;
333 batch->needs_flush = true;
334
335 pipe_mutex_lock(ctx->screen->lock);
336
337 if (buffers & PIPE_CLEAR_COLOR)
338 for (i = 0; i < pfb->nr_cbufs; i++)
339 if (buffers & (PIPE_CLEAR_COLOR0 << i))
340 resource_written(batch, pfb->cbufs[i]->texture);
341
342 if (buffers & (PIPE_CLEAR_DEPTH | PIPE_CLEAR_STENCIL)) {
343 resource_written(batch, pfb->zsbuf->texture);
344 batch->gmem_reason |= FD_GMEM_CLEARS_DEPTH_STENCIL;
345 }
346
347 resource_written(batch, batch->query_buf);
348
349 pipe_mutex_unlock(ctx->screen->lock);
350
351 DBG("%p: %x %ux%u depth=%f, stencil=%u (%s/%s)", batch, buffers,
352 pfb->width, pfb->height, depth, stencil,
353 util_format_short_name(pipe_surface_format(pfb->cbufs[0])),
354 util_format_short_name(pipe_surface_format(pfb->zsbuf)));
355
356 /* if per-gen backend doesn't implement ctx->clear() generic
357 * blitter clear:
358 */
359 if (!ctx->clear) {
360 fd_blitter_clear(pctx, buffers, color, depth, stencil);
361 return;
362 }
363
364 fd_hw_query_set_stage(batch, batch->draw, FD_STAGE_CLEAR);
365
366 ctx->clear(ctx, buffers, color, depth, stencil);
367
368 ctx->dirty |= FD_DIRTY_ZSA |
369 FD_DIRTY_VIEWPORT |
370 FD_DIRTY_RASTERIZER |
371 FD_DIRTY_SAMPLE_MASK |
372 FD_DIRTY_PROG |
373 FD_DIRTY_CONSTBUF |
374 FD_DIRTY_BLEND |
375 FD_DIRTY_FRAMEBUFFER;
376
377 if (fd_mesa_debug & FD_DBG_DCLEAR)
378 ctx->dirty = 0xffffffff;
379 }
380
381 static void
382 fd_clear_render_target(struct pipe_context *pctx, struct pipe_surface *ps,
383 const union pipe_color_union *color,
384 unsigned x, unsigned y, unsigned w, unsigned h,
385 bool render_condition_enabled)
386 {
387 DBG("TODO: x=%u, y=%u, w=%u, h=%u", x, y, w, h);
388 }
389
390 static void
391 fd_clear_depth_stencil(struct pipe_context *pctx, struct pipe_surface *ps,
392 unsigned buffers, double depth, unsigned stencil,
393 unsigned x, unsigned y, unsigned w, unsigned h,
394 bool render_condition_enabled)
395 {
396 DBG("TODO: buffers=%u, depth=%f, stencil=%u, x=%u, y=%u, w=%u, h=%u",
397 buffers, depth, stencil, x, y, w, h);
398 }
399
400 void
401 fd_draw_init(struct pipe_context *pctx)
402 {
403 pctx->draw_vbo = fd_draw_vbo;
404 pctx->clear = fd_clear;
405 pctx->clear_render_target = fd_clear_render_target;
406 pctx->clear_depth_stencil = fd_clear_depth_stencil;
407 }