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"
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"
43 static enum pc_di_index_size
44 size2indextype(unsigned index_size
)
47 case 1: return INDEX_SIZE_8_BIT
;
48 case 2: return INDEX_SIZE_16_BIT
;
49 case 4: return INDEX_SIZE_32_BIT
;
51 DBG("unsupported index size: %d", index_size
);
53 return INDEX_SIZE_IGN
;
56 /* this is same for a2xx/a3xx, so split into helper: */
58 fd_draw_emit(struct fd_context
*ctx
, struct fd_ringbuffer
*ring
,
59 enum pc_di_vis_cull_mode vismode
,
60 const struct pipe_draw_info
*info
)
62 struct pipe_index_buffer
*idx
= &ctx
->indexbuf
;
63 struct fd_bo
*idx_bo
= NULL
;
64 enum pc_di_index_size idx_type
= INDEX_SIZE_IGN
;
65 enum pc_di_src_sel src_sel
;
66 uint32_t idx_size
, idx_offset
;
69 assert(!idx
->user_buffer
);
71 idx_bo
= fd_resource(idx
->buffer
)->bo
;
72 idx_type
= size2indextype(idx
->index_size
);
73 idx_size
= idx
->index_size
* info
->count
;
74 idx_offset
= idx
->offset
+ (info
->start
* idx
->index_size
);
75 src_sel
= DI_SRC_SEL_DMA
;
78 idx_type
= INDEX_SIZE_IGN
;
81 src_sel
= DI_SRC_SEL_AUTO_INDEX
;
84 fd_draw(ctx
, ring
, ctx
->primtypes
[info
->mode
], vismode
, src_sel
,
85 info
->count
, idx_type
, idx_size
, idx_offset
, idx_bo
);
89 fd_draw_vbo(struct pipe_context
*pctx
, const struct pipe_draw_info
*info
)
91 struct fd_context
*ctx
= fd_context(pctx
);
92 struct pipe_framebuffer_state
*pfb
= &ctx
->framebuffer
;
93 struct pipe_scissor_state
*scissor
= fd_context_get_scissor(ctx
);
94 unsigned i
, buffers
= 0;
96 /* if we supported transform feedback, we'd have to disable this: */
97 if (((scissor
->maxx
- scissor
->minx
) *
98 (scissor
->maxy
- scissor
->miny
)) == 0) {
102 /* emulate unsupported primitives: */
103 if (!fd_supported_prim(ctx
, info
->mode
)) {
104 util_primconvert_save_index_buffer(ctx
->primconvert
, &ctx
->indexbuf
);
105 util_primconvert_save_rasterizer_state(ctx
->primconvert
, ctx
->rasterizer
);
106 util_primconvert_draw_vbo(ctx
->primconvert
, info
);
110 ctx
->needs_flush
= true;
113 * Figure out the buffers/features we need:
116 if (fd_depth_enabled(ctx
)) {
117 buffers
|= FD_BUFFER_DEPTH
;
118 fd_resource(pfb
->zsbuf
->texture
)->dirty
= true;
119 ctx
->gmem_reason
|= FD_GMEM_DEPTH_ENABLED
;
122 if (fd_stencil_enabled(ctx
)) {
123 buffers
|= FD_BUFFER_STENCIL
;
124 fd_resource(pfb
->zsbuf
->texture
)->dirty
= true;
125 ctx
->gmem_reason
|= FD_GMEM_STENCIL_ENABLED
;
128 if (fd_logicop_enabled(ctx
))
129 ctx
->gmem_reason
|= FD_GMEM_LOGICOP_ENABLED
;
131 for (i
= 0; i
< pfb
->nr_cbufs
; i
++) {
132 struct pipe_resource
*surf
;
137 surf
= pfb
->cbufs
[i
]->texture
;
139 fd_resource(surf
)->dirty
= true;
140 buffers
|= FD_BUFFER_COLOR
;
142 if (surf
->nr_samples
> 1)
143 ctx
->gmem_reason
|= FD_GMEM_MSAA_ENABLED
;
145 if (fd_blend_enabled(ctx
, i
))
146 ctx
->gmem_reason
|= FD_GMEM_BLEND_ENABLED
;
151 ctx
->stats
.draw_calls
++;
152 ctx
->stats
.prims_emitted
+=
153 u_reduced_prims_for_vertices(info
->mode
, info
->count
);
155 /* any buffers that haven't been cleared, we need to restore: */
156 ctx
->restore
|= buffers
& (FD_BUFFER_ALL
& ~ctx
->cleared
);
157 /* and any buffers used, need to be resolved: */
158 ctx
->resolve
|= buffers
;
160 DBG("%x num_draws=%u (%s/%s)", buffers
, ctx
->num_draws
,
161 util_format_short_name(pipe_surface_format(pfb
->cbufs
[0])),
162 util_format_short_name(pipe_surface_format(pfb
->zsbuf
)));
164 fd_hw_query_set_stage(ctx
, ctx
->ring
, FD_STAGE_DRAW
);
165 ctx
->draw(ctx
, info
);
167 /* if an app (or, well, piglit test) does many thousands of draws
168 * without flush (or anything which implicitly flushes, like
169 * changing render targets), we can exceed the ringbuffer size.
170 * Since we don't currently have a sane way to wrapparound, and
171 * we use the same buffer for both draw and tiling commands, for
172 * now we need to do this hack and trigger flush if we are running
173 * low on remaining space for cmds:
175 if (((ctx
->ring
->cur
- ctx
->ring
->start
) >
176 (ctx
->ring
->size
/4 - FD_TILING_COMMANDS_DWORDS
)) ||
177 (fd_mesa_debug
& FD_DBG_FLUSH
))
178 fd_context_render(pctx
);
181 /* TODO figure out how to make better use of existing state mechanism
182 * for clear (and possibly gmem->mem / mem->gmem) so we can (a) keep
183 * track of what state really actually changes, and (b) reduce the code
184 * in the a2xx/a3xx parts.
188 fd_clear(struct pipe_context
*pctx
, unsigned buffers
,
189 const union pipe_color_union
*color
, double depth
, unsigned stencil
)
191 struct fd_context
*ctx
= fd_context(pctx
);
192 struct pipe_framebuffer_state
*pfb
= &ctx
->framebuffer
;
194 ctx
->cleared
|= buffers
;
195 ctx
->resolve
|= buffers
;
196 ctx
->needs_flush
= true;
198 if (buffers
& PIPE_CLEAR_COLOR
)
199 fd_resource(pfb
->cbufs
[0]->texture
)->dirty
= true;
201 if (buffers
& (PIPE_CLEAR_DEPTH
| PIPE_CLEAR_STENCIL
)) {
202 fd_resource(pfb
->zsbuf
->texture
)->dirty
= true;
203 ctx
->gmem_reason
|= FD_GMEM_CLEARS_DEPTH_STENCIL
;
206 DBG("%x depth=%f, stencil=%u (%s/%s)", buffers
, depth
, stencil
,
207 util_format_short_name(pipe_surface_format(pfb
->cbufs
[0])),
208 util_format_short_name(pipe_surface_format(pfb
->zsbuf
)));
210 fd_hw_query_set_stage(ctx
, ctx
->ring
, FD_STAGE_CLEAR
);
212 ctx
->clear(ctx
, buffers
, color
, depth
, stencil
);
214 ctx
->dirty
|= FD_DIRTY_ZSA
|
216 FD_DIRTY_RASTERIZER
|
217 FD_DIRTY_SAMPLE_MASK
|
222 if (fd_mesa_debug
& FD_DBG_DCLEAR
)
223 ctx
->dirty
= 0xffffffff;
227 fd_clear_render_target(struct pipe_context
*pctx
, struct pipe_surface
*ps
,
228 const union pipe_color_union
*color
,
229 unsigned x
, unsigned y
, unsigned w
, unsigned h
)
231 DBG("TODO: x=%u, y=%u, w=%u, h=%u", x
, y
, w
, h
);
235 fd_clear_depth_stencil(struct pipe_context
*pctx
, struct pipe_surface
*ps
,
236 unsigned buffers
, double depth
, unsigned stencil
,
237 unsigned x
, unsigned y
, unsigned w
, unsigned h
)
239 DBG("TODO: buffers=%u, depth=%f, stencil=%u, x=%u, y=%u, w=%u, h=%u",
240 buffers
, depth
, stencil
, x
, y
, w
, h
);
244 fd_draw_init(struct pipe_context
*pctx
)
246 pctx
->draw_vbo
= fd_draw_vbo
;
247 pctx
->clear
= fd_clear
;
248 pctx
->clear_render_target
= fd_clear_render_target
;
249 pctx
->clear_depth_stencil
= fd_clear_depth_stencil
;