Merge commit 'origin/perrtblend'
[mesa.git] / src / gallium / drivers / i965 / brw_draw.c
1 /**************************************************************************
2 *
3 * Copyright 2003 Tungsten Graphics, Inc., Cedar Park, Texas.
4 * All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 *
26 **************************************************************************/
27
28
29 #include "util/u_prim.h"
30 #include "util/u_upload_mgr.h"
31
32 #include "brw_draw.h"
33 #include "brw_defines.h"
34 #include "brw_context.h"
35 #include "brw_state.h"
36 #include "brw_debug.h"
37
38 #include "brw_batchbuffer.h"
39
40
41 static uint32_t prim_to_hw_prim[PIPE_PRIM_POLYGON+1] = {
42 _3DPRIM_POINTLIST,
43 _3DPRIM_LINELIST,
44 _3DPRIM_LINELOOP,
45 _3DPRIM_LINESTRIP,
46 _3DPRIM_TRILIST,
47 _3DPRIM_TRISTRIP,
48 _3DPRIM_TRIFAN,
49 _3DPRIM_QUADLIST,
50 _3DPRIM_QUADSTRIP,
51 _3DPRIM_POLYGON
52 };
53
54
55
56 /* When the primitive changes, set a state bit and re-validate. Not
57 * the nicest and would rather deal with this by having all the
58 * programs be immune to the active primitive (ie. cope with all
59 * possibilities). That may not be realistic however.
60 */
61 static int brw_set_prim(struct brw_context *brw, unsigned prim )
62 {
63
64 if (BRW_DEBUG & DEBUG_PRIMS)
65 debug_printf("PRIM: %s\n", u_prim_name(prim));
66
67 if (prim != brw->primitive) {
68 unsigned reduced_prim;
69
70 brw->primitive = prim;
71 brw->state.dirty.brw |= BRW_NEW_PRIMITIVE;
72
73 reduced_prim = u_reduced_prim(prim);
74 if (reduced_prim != brw->reduced_primitive) {
75 brw->reduced_primitive = reduced_prim;
76 brw->state.dirty.brw |= BRW_NEW_REDUCED_PRIMITIVE;
77 }
78 }
79
80 return prim_to_hw_prim[prim];
81 }
82
83
84
85 static int brw_emit_prim(struct brw_context *brw,
86 unsigned start,
87 unsigned count,
88 boolean indexed,
89 uint32_t hw_prim)
90 {
91 struct brw_3d_primitive prim_packet;
92 int ret;
93
94 if (BRW_DEBUG & DEBUG_PRIMS)
95 debug_printf("%s start %d count %d indexed %d hw_prim %d\n",
96 __FUNCTION__, start, count, indexed, hw_prim);
97
98 prim_packet.header.opcode = CMD_3D_PRIM;
99 prim_packet.header.length = sizeof(prim_packet)/4 - 2;
100 prim_packet.header.pad = 0;
101 prim_packet.header.topology = hw_prim;
102 prim_packet.header.indexed = indexed;
103
104 prim_packet.verts_per_instance = count;
105 prim_packet.start_vert_location = start;
106 if (indexed)
107 prim_packet.start_vert_location += brw->ib.start_vertex_offset;
108 prim_packet.instance_count = 1;
109 prim_packet.start_instance_location = 0;
110 prim_packet.base_vert_location = 0; /* prim->basevertex; XXX: add this to gallium */
111
112
113 /* If we're set to always flush, do it before and after the primitive emit.
114 * We want to catch both missed flushes that hurt instruction/state cache
115 * and missed flushes of the render cache as it heads to other parts of
116 * the besides the draw code.
117 */
118 if (0) {
119 BEGIN_BATCH(1, IGNORE_CLIPRECTS);
120 OUT_BATCH((CMD_MI_FLUSH << 16) | BRW_FLUSH_STATE_CACHE);
121 ADVANCE_BATCH();
122 }
123 if (prim_packet.verts_per_instance) {
124 ret = brw_batchbuffer_data( brw->batch, &prim_packet,
125 sizeof(prim_packet), LOOP_CLIPRECTS);
126 if (ret)
127 return ret;
128 }
129 if (0) {
130 BEGIN_BATCH(1, IGNORE_CLIPRECTS);
131 OUT_BATCH((CMD_MI_FLUSH << 16) | BRW_FLUSH_STATE_CACHE);
132 ADVANCE_BATCH();
133 }
134
135 return 0;
136 }
137
138
139 /* May fail if out of video memory for texture or vbo upload, or on
140 * fallback conditions.
141 */
142 static int
143 try_draw_range_elements(struct brw_context *brw,
144 struct pipe_buffer *index_buffer,
145 unsigned hw_prim,
146 unsigned start, unsigned count)
147 {
148 int ret;
149
150 ret = brw_validate_state(brw);
151 if (ret)
152 return ret;
153
154 /* Check that we can fit our state in with our existing batchbuffer, or
155 * flush otherwise.
156 */
157 ret = brw->sws->check_aperture_space(brw->sws,
158 brw->state.validated_bos,
159 brw->state.validated_bo_count);
160 if (ret)
161 return ret;
162
163 ret = brw_upload_state(brw);
164 if (ret)
165 return ret;
166
167 ret = brw_emit_prim(brw, start, count, index_buffer != NULL, hw_prim);
168 if (ret)
169 return ret;
170
171 if (brw->flags.always_flush_batch)
172 brw_context_flush( brw );
173
174 return 0;
175 }
176
177
178 static void
179 brw_draw_range_elements(struct pipe_context *pipe,
180 struct pipe_buffer *index_buffer,
181 unsigned index_size,
182 unsigned min_index,
183 unsigned max_index,
184 unsigned mode, unsigned start, unsigned count)
185 {
186 struct brw_context *brw = brw_context(pipe);
187 int ret;
188 uint32_t hw_prim;
189
190 hw_prim = brw_set_prim(brw, mode);
191
192 if (BRW_DEBUG & DEBUG_PRIMS)
193 debug_printf("PRIM: %s start %d count %d index_buffer %p\n",
194 u_prim_name(mode), start, count, (void *)index_buffer);
195
196 /* Potentially trigger upload of new index buffer.
197 *
198 * XXX: do we need to go through state validation to achieve this?
199 * Could just call upload code directly.
200 */
201 if (brw->curr.index_buffer != index_buffer ||
202 brw->curr.index_size != index_size) {
203 pipe_buffer_reference( &brw->curr.index_buffer, index_buffer );
204 brw->curr.index_size = index_size;
205 brw->state.dirty.mesa |= PIPE_NEW_INDEX_BUFFER;
206 }
207
208 /* XXX: do we really care?
209 */
210 if (brw->curr.min_index != min_index ||
211 brw->curr.max_index != max_index)
212 {
213 brw->curr.min_index = min_index;
214 brw->curr.max_index = max_index;
215 brw->state.dirty.mesa |= PIPE_NEW_INDEX_RANGE;
216 }
217
218
219 /* Make a first attempt at drawing:
220 */
221 ret = try_draw_range_elements(brw, index_buffer, hw_prim, start, count );
222
223 /* Otherwise, flush and retry:
224 */
225 if (ret != 0) {
226 brw_context_flush( brw );
227 ret = try_draw_range_elements(brw, index_buffer, hw_prim, start, count );
228 assert(ret == 0);
229 }
230 }
231
232 static void
233 brw_draw_elements(struct pipe_context *pipe,
234 struct pipe_buffer *index_buffer,
235 unsigned index_size,
236 unsigned mode,
237 unsigned start, unsigned count)
238 {
239 brw_draw_range_elements( pipe, index_buffer,
240 index_size,
241 0, 0xffffffff,
242 mode,
243 start, count );
244 }
245
246 static void
247 brw_draw_arrays(struct pipe_context *pipe, unsigned mode,
248 unsigned start, unsigned count)
249 {
250 brw_draw_elements(pipe, NULL, 0, mode, start, count);
251 }
252
253
254
255 boolean brw_draw_init( struct brw_context *brw )
256 {
257 /* Register our drawing function:
258 */
259 brw->base.draw_arrays = brw_draw_arrays;
260 brw->base.draw_elements = brw_draw_elements;
261 brw->base.draw_range_elements = brw_draw_range_elements;
262
263 /* Create helpers for uploading data in user buffers:
264 */
265 brw->vb.upload_vertex = u_upload_create( brw->base.screen,
266 128 * 1024,
267 64,
268 PIPE_BUFFER_USAGE_VERTEX );
269 if (brw->vb.upload_vertex == NULL)
270 return FALSE;
271
272 brw->vb.upload_index = u_upload_create( brw->base.screen,
273 32 * 1024,
274 64,
275 PIPE_BUFFER_USAGE_INDEX );
276 if (brw->vb.upload_index == NULL)
277 return FALSE;
278
279 return TRUE;
280 }
281
282 void brw_draw_cleanup( struct brw_context *brw )
283 {
284 u_upload_destroy( brw->vb.upload_vertex );
285 u_upload_destroy( brw->vb.upload_index );
286
287 bo_reference(&brw->ib.bo, NULL);
288 }