i965: fix incorrect test for vertex position attribute
[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 "main/glheader.h"
31 #include "main/context.h"
32 #include "main/state.h"
33 #include "main/api_validate.h"
34 #include "main/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 prim_to_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 GLcontext *ctx = &brw->intel.ctx;
88
89 if (INTEL_DEBUG & DEBUG_PRIMS)
90 _mesa_printf("PRIM: %s\n", _mesa_lookup_enum_by_nr(prim));
91
92 /* Slight optimization to avoid the GS program when not needed:
93 */
94 if (prim == GL_QUAD_STRIP &&
95 ctx->Light.ShadeModel != GL_FLAT &&
96 ctx->Polygon.FrontMode == GL_FILL &&
97 ctx->Polygon.BackMode == GL_FILL)
98 prim = GL_TRIANGLE_STRIP;
99
100 if (prim != brw->primitive) {
101 brw->primitive = prim;
102 brw->state.dirty.brw |= BRW_NEW_PRIMITIVE;
103
104 if (reduced_prim[prim] != brw->intel.reduced_primitive) {
105 brw->intel.reduced_primitive = reduced_prim[prim];
106 brw->state.dirty.brw |= BRW_NEW_REDUCED_PRIMITIVE;
107 }
108 }
109
110 return prim_to_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 uint32_t hw_prim)
128 {
129 struct brw_3d_primitive prim_packet;
130 struct intel_context *intel = &brw->intel;
131
132 if (INTEL_DEBUG & DEBUG_PRIMS)
133 _mesa_printf("PRIM: %s %d %d\n", _mesa_lookup_enum_by_nr(prim->mode),
134 prim->start, prim->count);
135
136 prim_packet.header.opcode = CMD_3D_PRIM;
137 prim_packet.header.length = sizeof(prim_packet)/4 - 2;
138 prim_packet.header.pad = 0;
139 prim_packet.header.topology = hw_prim;
140 prim_packet.header.indexed = prim->indexed;
141
142 prim_packet.verts_per_instance = trim(prim->mode, prim->count);
143 prim_packet.start_vert_location = prim->start;
144 prim_packet.instance_count = 1;
145 prim_packet.start_instance_location = 0;
146 prim_packet.base_vert_location = 0;
147
148 /* Can't wrap here, since we rely on the validated state. */
149 brw->no_batch_wrap = GL_TRUE;
150
151 /* If we're set to always flush, do it before and after the primitive emit.
152 * We want to catch both missed flushes that hurt instruction/state cache
153 * and missed flushes of the render cache as it heads to other parts of
154 * the besides the draw code.
155 */
156 if (intel->always_flush_cache) {
157 BEGIN_BATCH(1, IGNORE_CLIPRECTS);
158 OUT_BATCH(intel->vtbl.flush_cmd());
159 ADVANCE_BATCH();
160 }
161 if (prim_packet.verts_per_instance) {
162 intel_batchbuffer_data( brw->intel.batch, &prim_packet,
163 sizeof(prim_packet), LOOP_CLIPRECTS);
164 }
165 if (intel->always_flush_cache) {
166 BEGIN_BATCH(1, IGNORE_CLIPRECTS);
167 OUT_BATCH(intel->vtbl.flush_cmd());
168 ADVANCE_BATCH();
169 }
170
171 brw->no_batch_wrap = GL_FALSE;
172 }
173
174 static void brw_merge_inputs( struct brw_context *brw,
175 const struct gl_client_array *arrays[])
176 {
177 struct brw_vertex_info old = brw->vb.info;
178 GLuint i;
179
180 for (i = 0; i < VERT_ATTRIB_MAX; i++)
181 dri_bo_unreference(brw->vb.inputs[i].bo);
182
183 memset(&brw->vb.inputs, 0, sizeof(brw->vb.inputs));
184 memset(&brw->vb.info, 0, sizeof(brw->vb.info));
185
186 for (i = 0; i < VERT_ATTRIB_MAX; i++) {
187 brw->vb.inputs[i].glarray = arrays[i];
188 brw->vb.inputs[i].attrib = (gl_vert_attrib) i;
189
190 if (arrays[i]->StrideB != 0)
191 brw->vb.info.varying |= 1 << i;
192
193 brw->vb.info.sizes[i/16] |= (brw->vb.inputs[i].glarray->Size - 1) <<
194 ((i%16) * 2);
195 }
196
197 /* Raise statechanges if input sizes and varying have changed:
198 */
199 if (memcmp(brw->vb.info.sizes, old.sizes, sizeof(old.sizes)) != 0)
200 brw->state.dirty.brw |= BRW_NEW_INPUT_DIMENSIONS;
201
202 if (brw->vb.info.varying != old.varying)
203 brw->state.dirty.brw |= BRW_NEW_INPUT_VARYING;
204 }
205
206 /* XXX: could split the primitive list to fallback only on the
207 * non-conformant primitives.
208 */
209 static GLboolean check_fallbacks( struct brw_context *brw,
210 const struct _mesa_prim *prim,
211 GLuint nr_prims )
212 {
213 GLcontext *ctx = &brw->intel.ctx;
214 GLuint i;
215
216 /* If we don't require strict OpenGL conformance, never
217 * use fallbacks. If we're forcing fallbacks, always
218 * use fallfacks.
219 */
220 if (brw->intel.conformance_mode == 0)
221 return GL_FALSE;
222
223 if (brw->intel.conformance_mode == 2)
224 return GL_TRUE;
225
226 if (ctx->Polygon.SmoothFlag) {
227 for (i = 0; i < nr_prims; i++)
228 if (reduced_prim[prim[i].mode] == GL_TRIANGLES)
229 return GL_TRUE;
230 }
231
232 /* BRW hardware will do AA lines, but they are non-conformant it
233 * seems. TBD whether we keep this fallback:
234 */
235 if (ctx->Line.SmoothFlag) {
236 for (i = 0; i < nr_prims; i++)
237 if (reduced_prim[prim[i].mode] == GL_LINES)
238 return GL_TRUE;
239 }
240
241 /* Stipple -- these fallbacks could be resolved with a little
242 * bit of work?
243 */
244 if (ctx->Line.StippleFlag) {
245 for (i = 0; i < nr_prims; i++) {
246 /* GS doesn't get enough information to know when to reset
247 * the stipple counter?!?
248 */
249 if (prim[i].mode == GL_LINE_LOOP || prim[i].mode == GL_LINE_STRIP)
250 return GL_TRUE;
251
252 if (prim[i].mode == GL_POLYGON &&
253 (ctx->Polygon.FrontMode == GL_LINE ||
254 ctx->Polygon.BackMode == GL_LINE))
255 return GL_TRUE;
256 }
257 }
258
259 if (ctx->Point.SmoothFlag) {
260 for (i = 0; i < nr_prims; i++)
261 if (prim[i].mode == GL_POINTS)
262 return GL_TRUE;
263 }
264
265 /* BRW hardware doesn't handle GL_CLAMP texturing correctly;
266 * brw_wm_sampler_state:translate_wrap_mode() treats GL_CLAMP
267 * as GL_CLAMP_TO_EDGE instead. If we're using GL_CLAMP, and
268 * we want strict conformance, force the fallback.
269 * Right now, we only do this for 2D textures.
270 */
271 {
272 int u;
273 for (u = 0; u < ctx->Const.MaxTextureCoordUnits; u++) {
274 struct gl_texture_unit *texUnit = &ctx->Texture.Unit[u];
275 if (texUnit->Enabled) {
276 if (texUnit->Enabled & TEXTURE_1D_BIT) {
277 if (texUnit->CurrentTex[TEXTURE_1D_INDEX]->WrapS == GL_CLAMP) {
278 return GL_TRUE;
279 }
280 }
281 if (texUnit->Enabled & TEXTURE_2D_BIT) {
282 if (texUnit->CurrentTex[TEXTURE_2D_INDEX]->WrapS == GL_CLAMP ||
283 texUnit->CurrentTex[TEXTURE_2D_INDEX]->WrapT == GL_CLAMP) {
284 return GL_TRUE;
285 }
286 }
287 if (texUnit->Enabled & TEXTURE_3D_BIT) {
288 if (texUnit->CurrentTex[TEXTURE_3D_INDEX]->WrapS == GL_CLAMP ||
289 texUnit->CurrentTex[TEXTURE_3D_INDEX]->WrapT == GL_CLAMP ||
290 texUnit->CurrentTex[TEXTURE_3D_INDEX]->WrapR == GL_CLAMP) {
291 return GL_TRUE;
292 }
293 }
294 }
295 }
296 }
297
298 /* Nothing stopping us from the fast path now */
299 return GL_FALSE;
300 }
301
302 /* May fail if out of video memory for texture or vbo upload, or on
303 * fallback conditions.
304 */
305 static GLboolean brw_try_draw_prims( GLcontext *ctx,
306 const struct gl_client_array *arrays[],
307 const struct _mesa_prim *prim,
308 GLuint nr_prims,
309 const struct _mesa_index_buffer *ib,
310 GLuint min_index,
311 GLuint max_index )
312 {
313 struct intel_context *intel = intel_context(ctx);
314 struct brw_context *brw = brw_context(ctx);
315 GLboolean retval = GL_FALSE;
316 GLboolean warn = GL_FALSE;
317 GLboolean first_time = GL_TRUE;
318 GLuint i;
319
320 if (ctx->NewState)
321 _mesa_update_state( ctx );
322
323 /* We have to validate the textures *before* checking for fallbacks;
324 * otherwise, the software fallback won't be able to rely on the
325 * texture state, the firstLevel and lastLevel fields won't be
326 * set in the intel texture object (they'll both be 0), and the
327 * software fallback will segfault if it attempts to access any
328 * texture level other than level 0.
329 */
330 brw_validate_textures( brw );
331
332 if (check_fallbacks(brw, prim, nr_prims))
333 return GL_FALSE;
334
335 /* Bind all inputs, derive varying and size information:
336 */
337 brw_merge_inputs( brw, arrays );
338
339 brw->ib.ib = ib;
340 brw->state.dirty.brw |= BRW_NEW_INDICES;
341
342 brw->vb.min_index = min_index;
343 brw->vb.max_index = max_index;
344 brw->state.dirty.brw |= BRW_NEW_VERTICES;
345
346 /* Have to validate state quite late. Will rebuild tnl_program,
347 * which depends on varying information.
348 *
349 * Note this is where brw->vs->prog_data.inputs_read is calculated,
350 * so can't access it earlier.
351 */
352
353 LOCK_HARDWARE(intel);
354
355 if (!intel->constant_cliprect && intel->driDrawable->numClipRects == 0) {
356 UNLOCK_HARDWARE(intel);
357 return GL_TRUE;
358 }
359
360 for (i = 0; i < nr_prims; i++) {
361 uint32_t hw_prim;
362
363 /* Flush the batch if it's approaching full, so that we don't wrap while
364 * we've got validated state that needs to be in the same batch as the
365 * primitives. This fraction is just a guess (minimal full state plus
366 * a primitive is around 512 bytes), and would be better if we had
367 * an upper bound of how much we might emit in a single
368 * brw_try_draw_prims().
369 */
370 intel_batchbuffer_require_space(intel->batch, intel->batch->size / 4,
371 LOOP_CLIPRECTS);
372
373 hw_prim = brw_set_prim(brw, prim[i].mode);
374
375 if (first_time || (brw->state.dirty.brw & BRW_NEW_PRIMITIVE)) {
376 first_time = GL_FALSE;
377
378 brw_validate_state(brw);
379
380 /* Various fallback checks: */
381 if (brw->intel.Fallback)
382 goto out;
383
384 /* Check that we can fit our state in with our existing batchbuffer, or
385 * flush otherwise.
386 */
387 if (dri_bufmgr_check_aperture_space(brw->state.validated_bos,
388 brw->state.validated_bo_count)) {
389 static GLboolean warned;
390 intel_batchbuffer_flush(intel->batch);
391
392 /* Validate the state after we flushed the batch (which would have
393 * changed the set of dirty state). If we still fail to
394 * check_aperture, warn of what's happening, but attempt to continue
395 * on since it may succeed anyway, and the user would probably rather
396 * see a failure and a warning than a fallback.
397 */
398 brw_validate_state(brw);
399 if (!warned &&
400 dri_bufmgr_check_aperture_space(brw->state.validated_bos,
401 brw->state.validated_bo_count)) {
402 warn = GL_TRUE;
403 warned = GL_TRUE;
404 }
405 }
406
407 brw_upload_state(brw);
408 }
409
410 brw_emit_prim(brw, &prim[i], hw_prim);
411
412 retval = GL_TRUE;
413 }
414
415 if (intel->always_flush_batch)
416 intel_batchbuffer_flush(intel->batch);
417 out:
418 UNLOCK_HARDWARE(intel);
419
420 if (warn)
421 fprintf(stderr, "i965: Single primitive emit potentially exceeded "
422 "available aperture space\n");
423
424 if (!retval)
425 DBG("%s failed\n", __FUNCTION__);
426
427 return retval;
428 }
429
430 static GLboolean brw_need_rebase( GLcontext *ctx,
431 const struct gl_client_array *arrays[],
432 const struct _mesa_index_buffer *ib,
433 GLuint min_index )
434 {
435 if (min_index == 0)
436 return GL_FALSE;
437
438 if (ib) {
439 if (!vbo_all_varyings_in_vbos(arrays))
440 return GL_TRUE;
441 else
442 return GL_FALSE;
443 }
444 else {
445 /* Hmm. This isn't quite what I wanted. BRW can actually
446 * handle the mixed case well enough that we shouldn't need to
447 * rebase. However, it's probably not very common, nor hugely
448 * expensive to do it this way:
449 */
450 if (!vbo_all_varyings_in_vbos(arrays))
451 return GL_TRUE;
452 else
453 return GL_FALSE;
454 }
455 }
456
457
458 void brw_draw_prims( GLcontext *ctx,
459 const struct gl_client_array *arrays[],
460 const struct _mesa_prim *prim,
461 GLuint nr_prims,
462 const struct _mesa_index_buffer *ib,
463 GLuint min_index,
464 GLuint max_index )
465 {
466 GLboolean retval;
467
468 /* Decide if we want to rebase. If so we end up recursing once
469 * only into this function.
470 */
471 if (brw_need_rebase( ctx, arrays, ib, min_index )) {
472 vbo_rebase_prims( ctx, arrays,
473 prim, nr_prims,
474 ib, min_index, max_index,
475 brw_draw_prims );
476
477 return;
478 }
479
480 /* Make a first attempt at drawing:
481 */
482 retval = brw_try_draw_prims(ctx, arrays, prim, nr_prims, ib, min_index, max_index);
483
484 /* Otherwise, we really are out of memory. Pass the drawing
485 * command to the software tnl module and which will in turn call
486 * swrast to do the drawing.
487 */
488 if (!retval) {
489 _swsetup_Wakeup(ctx);
490 _tnl_draw_prims(ctx, arrays, prim, nr_prims, ib, min_index, max_index);
491 }
492
493 }
494
495 void brw_draw_init( struct brw_context *brw )
496 {
497 GLcontext *ctx = &brw->intel.ctx;
498 struct vbo_context *vbo = vbo_context(ctx);
499
500 /* Register our drawing function:
501 */
502 vbo->draw_prims = brw_draw_prims;
503 }
504
505 void brw_draw_destroy( struct brw_context *brw )
506 {
507 int i;
508
509 if (brw->vb.upload.bo != NULL) {
510 dri_bo_unreference(brw->vb.upload.bo);
511 brw->vb.upload.bo = NULL;
512 }
513
514 for (i = 0; i < VERT_ATTRIB_MAX; i++) {
515 dri_bo_unreference(brw->vb.inputs[i].bo);
516 brw->vb.inputs[i].bo = NULL;
517 }
518
519 dri_bo_unreference(brw->ib.bo);
520 brw->ib.bo = NULL;
521 }