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_util.h"
42 static enum pc_di_primtype
43 mode2primtype(unsigned mode
)
46 case PIPE_PRIM_POINTS
: return DI_PT_POINTLIST_A2XX
;
47 case PIPE_PRIM_LINES
: return DI_PT_LINELIST
;
48 case PIPE_PRIM_LINE_STRIP
: return DI_PT_LINESTRIP
;
49 case PIPE_PRIM_TRIANGLES
: return DI_PT_TRILIST
;
50 case PIPE_PRIM_TRIANGLE_STRIP
: return DI_PT_TRISTRIP
;
51 case PIPE_PRIM_TRIANGLE_FAN
: return DI_PT_TRIFAN
;
52 case PIPE_PRIM_QUADS
: return DI_PT_QUADLIST
;
53 case PIPE_PRIM_QUAD_STRIP
: return DI_PT_QUADSTRIP
;
54 case PIPE_PRIM_POLYGON
: return DI_PT_POLYGON
;
56 DBG("unsupported mode: (%s) %d", u_prim_name(mode
), mode
);
61 static enum pc_di_index_size
62 size2indextype(unsigned index_size
)
65 case 1: return INDEX_SIZE_8_BIT
;
66 case 2: return INDEX_SIZE_16_BIT
;
67 case 4: return INDEX_SIZE_32_BIT
;
69 DBG("unsupported index size: %d", index_size
);
71 return INDEX_SIZE_IGN
;
74 /* this is same for a2xx/a3xx, so split into helper: */
76 fd_draw_emit(struct fd_context
*ctx
, const struct pipe_draw_info
*info
)
78 struct pipe_index_buffer
*idx
= &ctx
->indexbuf
;
79 struct fd_bo
*idx_bo
= NULL
;
80 enum pc_di_index_size idx_type
= INDEX_SIZE_IGN
;
81 enum pc_di_src_sel src_sel
;
82 uint32_t idx_size
, idx_offset
;
85 assert(!idx
->user_buffer
);
87 idx_bo
= fd_resource(idx
->buffer
)->bo
;
88 idx_type
= size2indextype(idx
->index_size
);
89 idx_size
= idx
->index_size
* info
->count
;
90 idx_offset
= idx
->offset
;
91 src_sel
= DI_SRC_SEL_DMA
;
94 idx_type
= INDEX_SIZE_IGN
;
97 src_sel
= DI_SRC_SEL_AUTO_INDEX
;
100 fd_draw(ctx
, mode2primtype(info
->mode
), src_sel
, info
->count
,
101 idx_type
, idx_size
, idx_offset
, idx_bo
);
105 fd_draw_vbo(struct pipe_context
*pctx
, const struct pipe_draw_info
*info
)
107 struct fd_context
*ctx
= fd_context(pctx
);
108 struct pipe_framebuffer_state
*pfb
= &ctx
->framebuffer
;
109 struct pipe_scissor_state
*scissor
= fd_context_get_scissor(ctx
);
110 unsigned i
, buffers
= 0;
112 /* if we supported transform feedback, we'd have to disable this: */
113 if (((scissor
->maxx
- scissor
->minx
) *
114 (scissor
->maxy
- scissor
->miny
)) == 0) {
118 ctx
->needs_flush
= true;
121 * Figure out the buffers/features we need:
124 if (fd_depth_enabled(ctx
)) {
125 buffers
|= FD_BUFFER_DEPTH
;
126 fd_resource(pfb
->zsbuf
->texture
)->dirty
= true;
127 ctx
->gmem_reason
|= FD_GMEM_DEPTH_ENABLED
;
130 if (fd_stencil_enabled(ctx
)) {
131 buffers
|= FD_BUFFER_STENCIL
;
132 fd_resource(pfb
->zsbuf
->texture
)->dirty
= true;
133 ctx
->gmem_reason
|= FD_GMEM_STENCIL_ENABLED
;
136 if (fd_logicop_enabled(ctx
))
137 ctx
->gmem_reason
|= FD_GMEM_LOGICOP_ENABLED
;
139 for (i
= 0; i
< pfb
->nr_cbufs
; i
++) {
140 struct pipe_resource
*surf
= pfb
->cbufs
[i
]->texture
;
142 fd_resource(surf
)->dirty
= true;
143 buffers
|= FD_BUFFER_COLOR
;
145 if (surf
->nr_samples
> 1)
146 ctx
->gmem_reason
|= FD_GMEM_MSAA_ENABLED
;
148 if (fd_blend_enabled(ctx
, i
))
149 ctx
->gmem_reason
|= FD_GMEM_BLEND_ENABLED
;
154 /* any buffers that haven't been cleared, we need to restore: */
155 ctx
->restore
|= buffers
& (FD_BUFFER_ALL
& ~ctx
->cleared
);
156 /* and any buffers used, need to be resolved: */
157 ctx
->resolve
|= buffers
;
159 ctx
->draw(ctx
, info
);
162 /* TODO figure out how to make better use of existing state mechanism
163 * for clear (and possibly gmem->mem / mem->gmem) so we can (a) keep
164 * track of what state really actually changes, and (b) reduce the code
165 * in the a2xx/a3xx parts.
169 fd_clear(struct pipe_context
*pctx
, unsigned buffers
,
170 const union pipe_color_union
*color
, double depth
, unsigned stencil
)
172 struct fd_context
*ctx
= fd_context(pctx
);
173 struct pipe_framebuffer_state
*pfb
= &ctx
->framebuffer
;
175 ctx
->cleared
|= buffers
;
176 ctx
->resolve
|= buffers
;
177 ctx
->needs_flush
= true;
179 if (buffers
& PIPE_CLEAR_COLOR
)
180 fd_resource(pfb
->cbufs
[0]->texture
)->dirty
= true;
182 if (buffers
& (PIPE_CLEAR_DEPTH
| PIPE_CLEAR_STENCIL
)) {
183 fd_resource(pfb
->zsbuf
->texture
)->dirty
= true;
184 ctx
->gmem_reason
|= FD_GMEM_CLEARS_DEPTH_STENCIL
;
187 DBG("%x depth=%f, stencil=%u (%s/%s)", buffers
, depth
, stencil
,
188 util_format_short_name(pipe_surface_format(pfb
->cbufs
[0])),
189 util_format_short_name(pipe_surface_format(pfb
->zsbuf
)));
191 ctx
->clear(ctx
, buffers
, color
, depth
, stencil
);
193 ctx
->dirty
|= FD_DIRTY_ZSA
|
194 FD_DIRTY_RASTERIZER
|
195 FD_DIRTY_SAMPLE_MASK
|
200 if (fd_mesa_debug
& FD_DBG_DCLEAR
)
201 ctx
->dirty
= 0xffffffff;
205 fd_clear_render_target(struct pipe_context
*pctx
, struct pipe_surface
*ps
,
206 const union pipe_color_union
*color
,
207 unsigned x
, unsigned y
, unsigned w
, unsigned h
)
209 DBG("TODO: x=%u, y=%u, w=%u, h=%u", x
, y
, w
, h
);
213 fd_clear_depth_stencil(struct pipe_context
*pctx
, struct pipe_surface
*ps
,
214 unsigned buffers
, double depth
, unsigned stencil
,
215 unsigned x
, unsigned y
, unsigned w
, unsigned h
)
217 DBG("TODO: buffers=%u, depth=%f, stencil=%u, x=%u, y=%u, w=%u, h=%u",
218 buffers
, depth
, stencil
, x
, y
, w
, h
);
222 fd_draw_init(struct pipe_context
*pctx
)
224 pctx
->draw_vbo
= fd_draw_vbo
;
225 pctx
->clear
= fd_clear
;
226 pctx
->clear_render_target
= fd_clear_render_target
;
227 pctx
->clear_depth_stencil
= fd_clear_depth_stencil
;