Merge commit 'origin/gallium-0.1' into gallium-0.2
[mesa.git] / src / mesa / drivers / dri / 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 #include <stdlib.h>
29
30 #include "glheader.h"
31 #include "context.h"
32 #include "state.h"
33 #include "api_validate.h"
34 #include "enums.h"
35
36 #include "brw_draw.h"
37 #include "brw_defines.h"
38 #include "brw_context.h"
39 #include "brw_state.h"
40 #include "brw_fallback.h"
41
42 #include "intel_batchbuffer.h"
43 #include "intel_buffer_objects.h"
44
45 #include "tnl/tnl.h"
46 #include "vbo/vbo_context.h"
47 #include "swrast/swrast.h"
48 #include "swrast_setup/swrast_setup.h"
49
50 #define FILE_DEBUG_FLAG DEBUG_BATCH
51
52 static GLuint hw_prim[GL_POLYGON+1] = {
53 _3DPRIM_POINTLIST,
54 _3DPRIM_LINELIST,
55 _3DPRIM_LINELOOP,
56 _3DPRIM_LINESTRIP,
57 _3DPRIM_TRILIST,
58 _3DPRIM_TRISTRIP,
59 _3DPRIM_TRIFAN,
60 _3DPRIM_QUADLIST,
61 _3DPRIM_QUADSTRIP,
62 _3DPRIM_POLYGON
63 };
64
65
66 static const GLenum reduced_prim[GL_POLYGON+1] = {
67 GL_POINTS,
68 GL_LINES,
69 GL_LINES,
70 GL_LINES,
71 GL_TRIANGLES,
72 GL_TRIANGLES,
73 GL_TRIANGLES,
74 GL_TRIANGLES,
75 GL_TRIANGLES,
76 GL_TRIANGLES
77 };
78
79
80 /* When the primitive changes, set a state bit and re-validate. Not
81 * the nicest and would rather deal with this by having all the
82 * programs be immune to the active primitive (ie. cope with all
83 * possibilities). That may not be realistic however.
84 */
85 static GLuint brw_set_prim(struct brw_context *brw, GLenum prim)
86 {
87 if (INTEL_DEBUG & DEBUG_PRIMS)
88 _mesa_printf("PRIM: %s\n", _mesa_lookup_enum_by_nr(prim));
89
90 /* Slight optimization to avoid the GS program when not needed:
91 */
92 if (prim == GL_QUAD_STRIP &&
93 brw->attribs.Light->ShadeModel != GL_FLAT &&
94 brw->attribs.Polygon->FrontMode == GL_FILL &&
95 brw->attribs.Polygon->BackMode == GL_FILL)
96 prim = GL_TRIANGLE_STRIP;
97
98 if (prim != brw->primitive) {
99 brw->primitive = prim;
100 brw->state.dirty.brw |= BRW_NEW_PRIMITIVE;
101
102 if (reduced_prim[prim] != brw->intel.reduced_primitive) {
103 brw->intel.reduced_primitive = reduced_prim[prim];
104 brw->state.dirty.brw |= BRW_NEW_REDUCED_PRIMITIVE;
105 }
106
107 brw_validate_state(brw);
108 }
109
110 return hw_prim[prim];
111 }
112
113
114 static GLuint trim(GLenum prim, GLuint length)
115 {
116 if (prim == GL_QUAD_STRIP)
117 return length > 3 ? (length - length % 2) : 0;
118 else if (prim == GL_QUADS)
119 return length - length % 4;
120 else
121 return length;
122 }
123
124
125 static void brw_emit_prim( struct brw_context *brw,
126 const struct _mesa_prim *prim )
127
128 {
129 struct brw_3d_primitive prim_packet;
130
131 if (INTEL_DEBUG & DEBUG_PRIMS)
132 _mesa_printf("PRIM: %s %d %d\n", _mesa_lookup_enum_by_nr(prim->mode),
133 prim->start, prim->count);
134
135 prim_packet.header.opcode = CMD_3D_PRIM;
136 prim_packet.header.length = sizeof(prim_packet)/4 - 2;
137 prim_packet.header.pad = 0;
138 prim_packet.header.topology = brw_set_prim(brw, prim->mode);
139 prim_packet.header.indexed = prim->indexed;
140
141 prim_packet.verts_per_instance = trim(prim->mode, prim->count);
142 prim_packet.start_vert_location = prim->start;
143 prim_packet.instance_count = 1;
144 prim_packet.start_instance_location = 0;
145 prim_packet.base_vert_location = 0;
146
147 /* Can't wrap here, since we rely on the validated state. */
148 brw->no_batch_wrap = GL_TRUE;
149 if (prim_packet.verts_per_instance) {
150 intel_batchbuffer_data( brw->intel.batch, &prim_packet,
151 sizeof(prim_packet), LOOP_CLIPRECTS);
152 }
153 brw->no_batch_wrap = GL_FALSE;
154 }
155
156 static void brw_merge_inputs( struct brw_context *brw,
157 const struct gl_client_array *arrays[])
158 {
159 struct brw_vertex_element *inputs = brw->vb.inputs;
160 struct brw_vertex_info old = brw->vb.info;
161 GLuint i;
162
163 memset(inputs, 0, sizeof(*inputs));
164 memset(&brw->vb.info, 0, sizeof(brw->vb.info));
165
166 for (i = 0; i < VERT_ATTRIB_MAX; i++) {
167 brw->vb.inputs[i].glarray = arrays[i];
168
169 /* XXX: metaops passes null arrays */
170 if (arrays[i]) {
171 if (arrays[i]->StrideB != 0)
172 brw->vb.info.varying |= 1 << i;
173
174 brw->vb.info.sizes[i/16] |= (inputs[i].glarray->Size - 1) << ((i%16) * 2);
175 }
176 }
177
178 /* Raise statechanges if input sizes and varying have changed:
179 */
180 if (memcmp(brw->vb.info.sizes, old.sizes, sizeof(old.sizes)) != 0)
181 brw->state.dirty.brw |= BRW_NEW_INPUT_DIMENSIONS;
182
183 if (brw->vb.info.varying != old.varying)
184 brw->state.dirty.brw |= BRW_NEW_INPUT_VARYING;
185 }
186
187 /* XXX: could split the primitive list to fallback only on the
188 * non-conformant primitives.
189 */
190 static GLboolean check_fallbacks( struct brw_context *brw,
191 const struct _mesa_prim *prim,
192 GLuint nr_prims )
193 {
194 GLuint i;
195
196 if (!brw->intel.strict_conformance)
197 return GL_FALSE;
198
199 if (brw->attribs.Polygon->SmoothFlag) {
200 for (i = 0; i < nr_prims; i++)
201 if (reduced_prim[prim[i].mode] == GL_TRIANGLES)
202 return GL_TRUE;
203 }
204
205 /* BRW hardware will do AA lines, but they are non-conformant it
206 * seems. TBD whether we keep this fallback:
207 */
208 if (brw->attribs.Line->SmoothFlag) {
209 for (i = 0; i < nr_prims; i++)
210 if (reduced_prim[prim[i].mode] == GL_LINES)
211 return GL_TRUE;
212 }
213
214 /* Stipple -- these fallbacks could be resolved with a little
215 * bit of work?
216 */
217 if (brw->attribs.Line->StippleFlag) {
218 for (i = 0; i < nr_prims; i++) {
219 /* GS doesn't get enough information to know when to reset
220 * the stipple counter?!?
221 */
222 if (prim[i].mode == GL_LINE_LOOP)
223 return GL_TRUE;
224
225 if (prim[i].mode == GL_POLYGON &&
226 (brw->attribs.Polygon->FrontMode == GL_LINE ||
227 brw->attribs.Polygon->BackMode == GL_LINE))
228 return GL_TRUE;
229 }
230 }
231
232
233 if (brw->attribs.Point->SmoothFlag) {
234 for (i = 0; i < nr_prims; i++)
235 if (prim[i].mode == GL_POINTS)
236 return GL_TRUE;
237 }
238
239 return GL_FALSE;
240 }
241
242 /* May fail if out of video memory for texture or vbo upload, or on
243 * fallback conditions.
244 */
245 static GLboolean brw_try_draw_prims( GLcontext *ctx,
246 const struct gl_client_array *arrays[],
247 const struct _mesa_prim *prim,
248 GLuint nr_prims,
249 const struct _mesa_index_buffer *ib,
250 GLuint min_index,
251 GLuint max_index )
252 {
253 struct intel_context *intel = intel_context(ctx);
254 struct brw_context *brw = brw_context(ctx);
255 GLboolean retval = GL_FALSE;
256 GLuint i;
257
258 if (ctx->NewState)
259 _mesa_update_state( ctx );
260
261 brw_validate_textures( brw );
262
263 /* Bind all inputs, derive varying and size information:
264 */
265 brw_merge_inputs( brw, arrays );
266
267 brw->ib.ib = ib;
268 brw->state.dirty.brw |= BRW_NEW_INDICES;
269
270 brw->vb.min_index = min_index;
271 brw->vb.max_index = max_index;
272 brw->state.dirty.brw |= BRW_NEW_VERTICES;
273 /* Have to validate state quite late. Will rebuild tnl_program,
274 * which depends on varying information.
275 *
276 * Note this is where brw->vs->prog_data.inputs_read is calculated,
277 * so can't access it earlier.
278 */
279
280 LOCK_HARDWARE(intel);
281
282 if (brw->intel.numClipRects == 0) {
283 UNLOCK_HARDWARE(intel);
284 return GL_TRUE;
285 }
286
287 {
288 /* Flush the batch if it's approaching full, so that we don't wrap while
289 * we've got validated state that needs to be in the same batch as the
290 * primitives. This fraction is just a guess (minimal full state plus
291 * a primitive is around 512 bytes), and would be better if we had
292 * an upper bound of how much we might emit in a single
293 * brw_try_draw_prims().
294 */
295 if (intel->batch->ptr - intel->batch->map > intel->batch->size * 3 / 4
296 /* brw_emit_prim may change the cliprect_mode to LOOP_CLIPRECTS */
297 || intel->batch->cliprect_mode != LOOP_CLIPRECTS)
298 intel_batchbuffer_flush(intel->batch);
299
300 /* Set the first primitive early, ahead of validate_state:
301 */
302 brw_set_prim(brw, prim[0].mode);
303
304 /* XXX: Need to separate validate and upload of state.
305 */
306 brw_validate_state( brw );
307
308 /* Various fallback checks:
309 */
310 if (brw->intel.Fallback)
311 goto out;
312
313 if (check_fallbacks( brw, prim, nr_prims ))
314 goto out;
315
316 for (i = 0; i < nr_prims; i++) {
317 brw_emit_prim(brw, &prim[i]);
318 }
319
320 retval = GL_TRUE;
321 }
322
323 out:
324 UNLOCK_HARDWARE(intel);
325
326 if (!retval)
327 DBG("%s failed\n", __FUNCTION__);
328
329 return retval;
330 }
331
332 static GLboolean brw_need_rebase( GLcontext *ctx,
333 const struct gl_client_array *arrays[],
334 const struct _mesa_index_buffer *ib,
335 GLuint min_index )
336 {
337 if (min_index == 0)
338 return GL_FALSE;
339
340 if (ib) {
341 if (!vbo_all_varyings_in_vbos(arrays))
342 return GL_TRUE;
343 else
344 return GL_FALSE;
345 }
346 else {
347 /* Hmm. This isn't quite what I wanted. BRW can actually
348 * handle the mixed case well enough that we shouldn't need to
349 * rebase. However, it's probably not very common, nor hugely
350 * expensive to do it this way:
351 */
352 if (!vbo_all_varyings_in_vbos(arrays))
353 return GL_TRUE;
354 else
355 return GL_FALSE;
356 }
357 }
358
359
360 void brw_draw_prims( GLcontext *ctx,
361 const struct gl_client_array *arrays[],
362 const struct _mesa_prim *prim,
363 GLuint nr_prims,
364 const struct _mesa_index_buffer *ib,
365 GLuint min_index,
366 GLuint max_index )
367 {
368 GLboolean retval;
369
370 /* Decide if we want to rebase. If so we end up recursing once
371 * only into this function.
372 */
373 if (brw_need_rebase( ctx, arrays, ib, min_index )) {
374 vbo_rebase_prims( ctx, arrays,
375 prim, nr_prims,
376 ib, min_index, max_index,
377 brw_draw_prims );
378
379 return;
380 }
381
382
383 /* Make a first attempt at drawing:
384 */
385 retval = brw_try_draw_prims(ctx, arrays, prim, nr_prims, ib, min_index, max_index);
386
387 /* Otherwise, we really are out of memory. Pass the drawing
388 * command to the software tnl module and which will in turn call
389 * swrast to do the drawing.
390 */
391 if (!retval) {
392 _swsetup_Wakeup(ctx);
393 _tnl_draw_prims(ctx, arrays, prim, nr_prims, ib, min_index, max_index);
394 }
395 }
396
397 void brw_draw_init( struct brw_context *brw )
398 {
399 GLcontext *ctx = &brw->intel.ctx;
400 struct vbo_context *vbo = vbo_context(ctx);
401
402 /* Register our drawing function:
403 */
404 vbo->draw_prims = brw_draw_prims;
405 }
406
407 void brw_draw_destroy( struct brw_context *brw )
408 {
409 if (brw->vb.upload.bo != NULL) {
410 dri_bo_unreference(brw->vb.upload.bo);
411 brw->vb.upload.bo = NULL;
412 }
413 }