1 /* -*- mode: C; c-file-style: "k&r"; tab-width 4; indent-tabs-mode: t; -*- */
4 * Copyright (C) 2012 Rob Clark <robclark@freedesktop.org>
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:
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
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
26 * Rob Clark <robclark@freedesktop.org>
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"
36 #include "freedreno_draw.h"
37 #include "freedreno_context.h"
38 #include "freedreno_state.h"
39 #include "freedreno_resource.h"
40 #include "freedreno_query_acc.h"
41 #include "freedreno_query_hw.h"
42 #include "freedreno_util.h"
45 resource_read(struct fd_batch
*batch
, struct pipe_resource
*prsc
)
49 fd_batch_resource_used(batch
, fd_resource(prsc
), false);
53 resource_written(struct fd_batch
*batch
, struct pipe_resource
*prsc
)
57 fd_batch_resource_used(batch
, fd_resource(prsc
), true);
61 fd_draw_vbo(struct pipe_context
*pctx
, const struct pipe_draw_info
*info
)
63 struct fd_context
*ctx
= fd_context(pctx
);
64 struct fd_batch
*batch
= ctx
->batch
;
65 struct pipe_framebuffer_state
*pfb
= &batch
->framebuffer
;
66 struct pipe_scissor_state
*scissor
= fd_context_get_scissor(ctx
);
67 unsigned i
, prims
, buffers
= 0;
69 if (!info
->count_from_stream_output
&& !info
->indirect
&&
70 !info
->primitive_restart
&&
71 !u_trim_pipe_prim(info
->mode
, (unsigned*)&info
->count
))
74 /* if we supported transform feedback, we'd have to disable this: */
75 if (((scissor
->maxx
- scissor
->minx
) *
76 (scissor
->maxy
- scissor
->miny
)) == 0) {
80 /* TODO: push down the region versions into the tiles */
81 if (!fd_render_condition_check(pctx
))
84 /* emulate unsupported primitives: */
85 if (!fd_supported_prim(ctx
, info
->mode
)) {
86 if (ctx
->streamout
.num_targets
> 0)
87 debug_error("stream-out with emulated prims");
88 util_primconvert_save_rasterizer_state(ctx
->primconvert
, ctx
->rasterizer
);
89 util_primconvert_draw_vbo(ctx
->primconvert
, info
);
93 /* Upload a user index buffer. */
94 struct pipe_resource
*indexbuf
= NULL
;
95 unsigned index_offset
= 0;
96 struct pipe_draw_info new_info
;
97 if (info
->index_size
) {
98 if (info
->has_user_indices
) {
99 if (!util_upload_index_buffer(pctx
, info
, &indexbuf
, &index_offset
))
102 new_info
.index
.resource
= indexbuf
;
103 new_info
.has_user_indices
= false;
106 indexbuf
= info
->index
.resource
;
111 fd_batch_reset(batch
);
112 fd_context_all_dirty(ctx
);
115 batch
->blit
= ctx
->in_blit
;
116 batch
->back_blit
= ctx
->in_shadow
;
118 /* NOTE: needs to be before resource_written(batch->query_buf), otherwise
119 * query_buf may not be created yet.
121 fd_batch_set_stage(batch
, FD_STAGE_DRAW
);
124 * Figure out the buffers/features we need:
127 mtx_lock(&ctx
->screen
->lock
);
129 if (fd_depth_enabled(ctx
)) {
130 buffers
|= FD_BUFFER_DEPTH
;
131 resource_written(batch
, pfb
->zsbuf
->texture
);
132 batch
->gmem_reason
|= FD_GMEM_DEPTH_ENABLED
;
135 if (fd_stencil_enabled(ctx
)) {
136 buffers
|= FD_BUFFER_STENCIL
;
137 resource_written(batch
, pfb
->zsbuf
->texture
);
138 batch
->gmem_reason
|= FD_GMEM_STENCIL_ENABLED
;
141 if (fd_logicop_enabled(ctx
))
142 batch
->gmem_reason
|= FD_GMEM_LOGICOP_ENABLED
;
144 for (i
= 0; i
< pfb
->nr_cbufs
; i
++) {
145 struct pipe_resource
*surf
;
150 surf
= pfb
->cbufs
[i
]->texture
;
152 resource_written(batch
, surf
);
153 buffers
|= PIPE_CLEAR_COLOR0
<< i
;
155 if (surf
->nr_samples
> 1)
156 batch
->gmem_reason
|= FD_GMEM_MSAA_ENABLED
;
158 if (fd_blend_enabled(ctx
, i
))
159 batch
->gmem_reason
|= FD_GMEM_BLEND_ENABLED
;
162 /* Mark SSBOs as being written.. we don't actually know which ones are
163 * read vs written, so just assume the worst
165 foreach_bit(i
, ctx
->shaderbuf
[PIPE_SHADER_FRAGMENT
].enabled_mask
)
166 resource_written(batch
, ctx
->shaderbuf
[PIPE_SHADER_FRAGMENT
].sb
[i
].buffer
);
168 foreach_bit(i
, ctx
->shaderimg
[PIPE_SHADER_FRAGMENT
].enabled_mask
) {
169 struct pipe_image_view
*img
=
170 &ctx
->shaderimg
[PIPE_SHADER_FRAGMENT
].si
[i
];
171 if (img
->access
& PIPE_IMAGE_ACCESS_WRITE
)
172 resource_written(batch
, img
->resource
);
174 resource_read(batch
, img
->resource
);
177 foreach_bit(i
, ctx
->constbuf
[PIPE_SHADER_VERTEX
].enabled_mask
)
178 resource_read(batch
, ctx
->constbuf
[PIPE_SHADER_VERTEX
].cb
[i
].buffer
);
179 foreach_bit(i
, ctx
->constbuf
[PIPE_SHADER_FRAGMENT
].enabled_mask
)
180 resource_read(batch
, ctx
->constbuf
[PIPE_SHADER_FRAGMENT
].cb
[i
].buffer
);
182 /* Mark VBOs as being read */
183 foreach_bit(i
, ctx
->vtx
.vertexbuf
.enabled_mask
) {
184 assert(!ctx
->vtx
.vertexbuf
.vb
[i
].is_user_buffer
);
185 resource_read(batch
, ctx
->vtx
.vertexbuf
.vb
[i
].buffer
.resource
);
188 /* Mark index buffer as being read */
189 resource_read(batch
, indexbuf
);
191 /* Mark textures as being read */
192 foreach_bit(i
, ctx
->tex
[PIPE_SHADER_VERTEX
].valid_textures
)
193 resource_read(batch
, ctx
->tex
[PIPE_SHADER_VERTEX
].textures
[i
]->texture
);
194 foreach_bit(i
, ctx
->tex
[PIPE_SHADER_FRAGMENT
].valid_textures
)
195 resource_read(batch
, ctx
->tex
[PIPE_SHADER_FRAGMENT
].textures
[i
]->texture
);
197 /* Mark streamout buffers as being written.. */
198 for (i
= 0; i
< ctx
->streamout
.num_targets
; i
++)
199 if (ctx
->streamout
.targets
[i
])
200 resource_written(batch
, ctx
->streamout
.targets
[i
]->buffer
);
202 resource_written(batch
, batch
->query_buf
);
204 list_for_each_entry(struct fd_acc_query
, aq
, &ctx
->acc_active_queries
, node
)
205 resource_written(batch
, aq
->prsc
);
207 mtx_unlock(&ctx
->screen
->lock
);
211 prims
= u_reduced_prims_for_vertices(info
->mode
, info
->count
);
213 ctx
->stats
.draw_calls
++;
215 /* TODO prims_emitted should be clipped when the stream-out buffer is
216 * not large enough. See max_tf_vtx().. probably need to move that
217 * into common code. Although a bit more annoying since a2xx doesn't
218 * use ir3 so no common way to get at the pipe_stream_output_info
219 * which is needed for this calculation.
221 if (ctx
->streamout
.num_targets
> 0)
222 ctx
->stats
.prims_emitted
+= prims
;
223 ctx
->stats
.prims_generated
+= prims
;
225 /* any buffers that haven't been cleared yet, we need to restore: */
226 batch
->restore
|= buffers
& (FD_BUFFER_ALL
& ~batch
->cleared
);
227 /* and any buffers used, need to be resolved: */
228 batch
->resolve
|= buffers
;
230 DBG("%p: %x %ux%u num_draws=%u (%s/%s)", batch
, buffers
,
231 pfb
->width
, pfb
->height
, batch
->num_draws
,
232 util_format_short_name(pipe_surface_format(pfb
->cbufs
[0])),
233 util_format_short_name(pipe_surface_format(pfb
->zsbuf
)));
235 if (ctx
->draw_vbo(ctx
, info
, index_offset
))
236 batch
->needs_flush
= true;
238 for (i
= 0; i
< ctx
->streamout
.num_targets
; i
++)
239 ctx
->streamout
.offsets
[i
] += info
->count
;
241 if (fd_mesa_debug
& FD_DBG_DDRAW
)
242 fd_context_all_dirty(ctx
);
244 fd_batch_check_size(batch
);
246 if (info
== &new_info
)
247 pipe_resource_reference(&indexbuf
, NULL
);
250 /* Generic clear implementation (partially) using u_blitter: */
252 fd_blitter_clear(struct pipe_context
*pctx
, unsigned buffers
,
253 const union pipe_color_union
*color
, double depth
, unsigned stencil
)
255 struct fd_context
*ctx
= fd_context(pctx
);
256 struct pipe_framebuffer_state
*pfb
= &ctx
->batch
->framebuffer
;
257 struct blitter_context
*blitter
= ctx
->blitter
;
259 fd_blitter_pipe_begin(ctx
, false, true, FD_STAGE_CLEAR
);
261 util_blitter_common_clear_setup(blitter
, pfb
->width
, pfb
->height
,
262 buffers
, NULL
, NULL
);
264 struct pipe_stencil_ref sr
= {
265 .ref_value
= { stencil
& 0xff }
267 pctx
->set_stencil_ref(pctx
, &sr
);
269 struct pipe_constant_buffer cb
= {
271 .user_buffer
= &color
->ui
,
273 pctx
->set_constant_buffer(pctx
, PIPE_SHADER_FRAGMENT
, 0, &cb
);
275 if (!ctx
->clear_rs_state
) {
276 const struct pipe_rasterizer_state tmpl
= {
277 .cull_face
= PIPE_FACE_NONE
,
278 .half_pixel_center
= 1,
279 .bottom_edge_rule
= 1,
283 ctx
->clear_rs_state
= pctx
->create_rasterizer_state(pctx
, &tmpl
);
285 pctx
->bind_rasterizer_state(pctx
, ctx
->clear_rs_state
);
287 struct pipe_viewport_state vp
= {
288 .scale
= { 0.5f
* pfb
->width
, -0.5f
* pfb
->height
, depth
},
289 .translate
= { 0.5f
* pfb
->width
, 0.5f
* pfb
->height
, 0.0f
},
291 pctx
->set_viewport_states(pctx
, 0, 1, &vp
);
293 pctx
->bind_vertex_elements_state(pctx
, ctx
->solid_vbuf_state
.vtx
);
294 pctx
->set_vertex_buffers(pctx
, blitter
->vb_slot
, 1,
295 &ctx
->solid_vbuf_state
.vertexbuf
.vb
[0]);
296 pctx
->set_stream_output_targets(pctx
, 0, NULL
, NULL
);
297 pctx
->bind_vs_state(pctx
, ctx
->solid_prog
.vp
);
298 pctx
->bind_fs_state(pctx
, ctx
->solid_prog
.fp
);
300 struct pipe_draw_info info
= {
301 .mode
= PIPE_PRIM_MAX
, /* maps to DI_PT_RECTLIST */
306 ctx
->draw_vbo(ctx
, &info
, 0);
308 util_blitter_restore_constant_buffer_state(blitter
);
309 util_blitter_restore_vertex_states(blitter
);
310 util_blitter_restore_fragment_states(blitter
);
311 util_blitter_restore_textures(blitter
);
312 util_blitter_restore_fb_state(blitter
);
313 util_blitter_restore_render_cond(blitter
);
314 util_blitter_unset_running_flag(blitter
);
316 fd_blitter_pipe_end(ctx
);
319 /* TODO figure out how to make better use of existing state mechanism
320 * for clear (and possibly gmem->mem / mem->gmem) so we can (a) keep
321 * track of what state really actually changes, and (b) reduce the code
322 * in the a2xx/a3xx parts.
326 fd_clear(struct pipe_context
*pctx
, unsigned buffers
,
327 const union pipe_color_union
*color
, double depth
, unsigned stencil
)
329 struct fd_context
*ctx
= fd_context(pctx
);
330 struct fd_batch
*batch
= ctx
->batch
;
331 struct pipe_framebuffer_state
*pfb
= &batch
->framebuffer
;
332 struct pipe_scissor_state
*scissor
= fd_context_get_scissor(ctx
);
333 unsigned cleared_buffers
;
336 /* TODO: push down the region versions into the tiles */
337 if (!fd_render_condition_check(pctx
))
341 fd_batch_reset(batch
);
342 fd_context_all_dirty(ctx
);
345 /* for bookkeeping about which buffers have been cleared (and thus
346 * can fully or partially skip mem2gmem) we need to ignore buffers
347 * that have already had a draw, in case apps do silly things like
348 * clear after draw (ie. if you only clear the color buffer, but
349 * something like alpha-test causes side effects from the draw in
350 * the depth buffer, etc)
352 cleared_buffers
= buffers
& (FD_BUFFER_ALL
& ~batch
->restore
);
354 /* do we have full-screen scissor? */
355 if (!memcmp(scissor
, &ctx
->disabled_scissor
, sizeof(*scissor
))) {
356 batch
->cleared
|= cleared_buffers
;
358 batch
->partial_cleared
|= cleared_buffers
;
359 if (cleared_buffers
& PIPE_CLEAR_COLOR
)
360 batch
->cleared_scissor
.color
= *scissor
;
361 if (cleared_buffers
& PIPE_CLEAR_DEPTH
)
362 batch
->cleared_scissor
.depth
= *scissor
;
363 if (cleared_buffers
& PIPE_CLEAR_STENCIL
)
364 batch
->cleared_scissor
.stencil
= *scissor
;
366 batch
->resolve
|= buffers
;
367 batch
->needs_flush
= true;
369 mtx_lock(&ctx
->screen
->lock
);
371 if (buffers
& PIPE_CLEAR_COLOR
)
372 for (i
= 0; i
< pfb
->nr_cbufs
; i
++)
373 if (buffers
& (PIPE_CLEAR_COLOR0
<< i
))
374 resource_written(batch
, pfb
->cbufs
[i
]->texture
);
376 if (buffers
& (PIPE_CLEAR_DEPTH
| PIPE_CLEAR_STENCIL
)) {
377 resource_written(batch
, pfb
->zsbuf
->texture
);
378 batch
->gmem_reason
|= FD_GMEM_CLEARS_DEPTH_STENCIL
;
381 resource_written(batch
, batch
->query_buf
);
383 list_for_each_entry(struct fd_acc_query
, aq
, &ctx
->acc_active_queries
, node
)
384 resource_written(batch
, aq
->prsc
);
386 mtx_unlock(&ctx
->screen
->lock
);
388 DBG("%p: %x %ux%u depth=%f, stencil=%u (%s/%s)", batch
, buffers
,
389 pfb
->width
, pfb
->height
, depth
, stencil
,
390 util_format_short_name(pipe_surface_format(pfb
->cbufs
[0])),
391 util_format_short_name(pipe_surface_format(pfb
->zsbuf
)));
393 /* if per-gen backend doesn't implement ctx->clear() generic
396 bool fallback
= true;
399 fd_batch_set_stage(batch
, FD_STAGE_CLEAR
);
401 if (ctx
->clear(ctx
, buffers
, color
, depth
, stencil
)) {
402 if (fd_mesa_debug
& FD_DBG_DCLEAR
)
403 fd_context_all_dirty(ctx
);
410 fd_blitter_clear(pctx
, buffers
, color
, depth
, stencil
);
415 fd_clear_render_target(struct pipe_context
*pctx
, struct pipe_surface
*ps
,
416 const union pipe_color_union
*color
,
417 unsigned x
, unsigned y
, unsigned w
, unsigned h
,
418 bool render_condition_enabled
)
420 DBG("TODO: x=%u, y=%u, w=%u, h=%u", x
, y
, w
, h
);
424 fd_clear_depth_stencil(struct pipe_context
*pctx
, struct pipe_surface
*ps
,
425 unsigned buffers
, double depth
, unsigned stencil
,
426 unsigned x
, unsigned y
, unsigned w
, unsigned h
,
427 bool render_condition_enabled
)
429 DBG("TODO: buffers=%u, depth=%f, stencil=%u, x=%u, y=%u, w=%u, h=%u",
430 buffers
, depth
, stencil
, x
, y
, w
, h
);
434 fd_launch_grid(struct pipe_context
*pctx
, const struct pipe_grid_info
*info
)
436 struct fd_context
*ctx
= fd_context(pctx
);
437 struct fd_batch
*batch
, *save_batch
= NULL
;
440 batch
= fd_batch_create(ctx
);
441 fd_batch_reference(&save_batch
, ctx
->batch
);
442 fd_batch_reference(&ctx
->batch
, batch
);
444 mtx_lock(&ctx
->screen
->lock
);
446 /* Mark SSBOs as being written.. we don't actually know which ones are
447 * read vs written, so just assume the worst
449 foreach_bit(i
, ctx
->shaderbuf
[PIPE_SHADER_COMPUTE
].enabled_mask
)
450 resource_read(batch
, ctx
->shaderbuf
[PIPE_SHADER_COMPUTE
].sb
[i
].buffer
);
452 foreach_bit(i
, ctx
->shaderimg
[PIPE_SHADER_COMPUTE
].enabled_mask
) {
453 struct pipe_image_view
*img
=
454 &ctx
->shaderimg
[PIPE_SHADER_COMPUTE
].si
[i
];
455 if (img
->access
& PIPE_IMAGE_ACCESS_WRITE
)
456 resource_written(batch
, img
->resource
);
458 resource_read(batch
, img
->resource
);
462 foreach_bit(i
, ctx
->constbuf
[PIPE_SHADER_COMPUTE
].enabled_mask
)
463 resource_read(batch
, ctx
->constbuf
[PIPE_SHADER_COMPUTE
].cb
[i
].buffer
);
465 /* Mark textures as being read */
466 foreach_bit(i
, ctx
->tex
[PIPE_SHADER_COMPUTE
].valid_textures
)
467 resource_read(batch
, ctx
->tex
[PIPE_SHADER_COMPUTE
].textures
[i
]->texture
);
469 mtx_unlock(&ctx
->screen
->lock
);
471 ctx
->launch_grid(ctx
, info
);
473 fd_gmem_flush_compute(batch
);
475 fd_batch_reference(&ctx
->batch
, save_batch
);
476 fd_batch_reference(&save_batch
, NULL
);
480 fd_draw_init(struct pipe_context
*pctx
)
482 pctx
->draw_vbo
= fd_draw_vbo
;
483 pctx
->clear
= fd_clear
;
484 pctx
->clear_render_target
= fd_clear_render_target
;
485 pctx
->clear_depth_stencil
= fd_clear_depth_stencil
;
487 if (has_compute(fd_screen(pctx
->screen
))) {
488 pctx
->launch_grid
= fd_launch_grid
;