bbb4e0f3cdaff53f934bd631f8a813821665e8fa
[mesa.git] / src / mesa / drivers / dri / i915 / intel_tris.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 "glheader.h"
29 #include "context.h"
30 #include "macros.h"
31 #include "enums.h"
32 #include "texobj.h"
33 #include "state.h"
34 #include "dd.h"
35
36 #include "swrast/swrast.h"
37 #include "swrast_setup/swrast_setup.h"
38 #include "tnl/t_context.h"
39 #include "tnl/t_pipeline.h"
40 #include "tnl/t_vertex.h"
41
42 #include "intel_screen.h"
43 #include "intel_context.h"
44 #include "intel_tris.h"
45 #include "intel_batchbuffer.h"
46 #include "intel_buffers.h"
47 #include "intel_reg.h"
48 #include "intel_span.h"
49 #include "intel_tex.h"
50
51 static void intelRenderPrimitive(GLcontext * ctx, GLenum prim);
52 static void intelRasterPrimitive(GLcontext * ctx, GLenum rprim,
53 GLuint hwprim);
54
55 /*
56 */
57 static void
58 intel_flush_inline_primitive(struct intel_context *intel)
59 {
60 GLuint used = intel->batch->ptr - intel->prim.start_ptr;
61
62 assert(intel->prim.primitive != ~0);
63
64 /* _mesa_printf("/\n"); */
65
66 if (used < 8)
67 goto do_discard;
68
69 *(int *) intel->prim.start_ptr = (_3DPRIMITIVE |
70 intel->prim.primitive | (used / 4 - 2));
71
72 goto finished;
73
74 do_discard:
75 intel->batch->ptr -= used;
76
77 finished:
78 intel->prim.primitive = ~0;
79 intel->prim.start_ptr = 0;
80 intel->prim.flush = 0;
81 }
82
83
84 /* Emit a primitive referencing vertices in a vertex buffer.
85 */
86 void
87 intelStartInlinePrimitive(struct intel_context *intel,
88 GLuint prim, GLuint batch_flags)
89 {
90 BATCH_LOCALS;
91
92 intel_wait_flips(intel);
93
94 intel->vtbl.emit_state(intel);
95
96 intel->no_batch_wrap = GL_TRUE;
97
98 /* _mesa_printf("%s *", __progname); */
99
100 /* Emit a slot which will be filled with the inline primitive
101 * command later.
102 */
103 BEGIN_BATCH(2, batch_flags);
104 OUT_BATCH(0);
105
106 assert((intel->batch->dirty_state & (1<<1)) == 0);
107
108 intel->prim.start_ptr = intel->batch->ptr;
109 intel->prim.primitive = prim;
110 intel->prim.flush = intel_flush_inline_primitive;
111
112 OUT_BATCH(0);
113 ADVANCE_BATCH();
114
115 intel->no_batch_wrap = GL_FALSE;
116
117 /* _mesa_printf(">"); */
118 }
119
120
121 void
122 intelWrapInlinePrimitive(struct intel_context *intel)
123 {
124 GLuint prim = intel->prim.primitive;
125 enum cliprect_mode cliprect_mode = intel->batch->cliprect_mode;
126
127 intel_flush_inline_primitive(intel);
128 intel_batchbuffer_flush(intel->batch);
129 intelStartInlinePrimitive(intel, prim, cliprect_mode); /* ??? */
130 }
131
132 GLuint *
133 intelExtendInlinePrimitive(struct intel_context *intel, GLuint dwords)
134 {
135 GLuint sz = dwords * sizeof(GLuint);
136 GLuint *ptr;
137
138 assert(intel->prim.flush == intel_flush_inline_primitive);
139
140 if (intel_batchbuffer_space(intel->batch) < sz)
141 intelWrapInlinePrimitive(intel);
142
143 /* _mesa_printf("."); */
144
145 intel->vtbl.assert_not_dirty(intel);
146
147 ptr = (GLuint *) intel->batch->ptr;
148 intel->batch->ptr += sz;
149
150 return ptr;
151 }
152
153
154
155 /***********************************************************************
156 * Emit primitives as inline vertices *
157 ***********************************************************************/
158
159 #ifdef __i386__
160 #define COPY_DWORDS( j, vb, vertsize, v ) \
161 do { \
162 int __tmp; \
163 __asm__ __volatile__( "rep ; movsl" \
164 : "=%c" (j), "=D" (vb), "=S" (__tmp) \
165 : "0" (vertsize), \
166 "D" ((long)vb), \
167 "S" ((long)v) ); \
168 } while (0)
169 #else
170 #define COPY_DWORDS( j, vb, vertsize, v ) \
171 do { \
172 for ( j = 0 ; j < vertsize ; j++ ) { \
173 vb[j] = ((GLuint *)v)[j]; \
174 } \
175 vb += vertsize; \
176 } while (0)
177 #endif
178
179 static void
180 intel_draw_quad(struct intel_context *intel,
181 intelVertexPtr v0,
182 intelVertexPtr v1, intelVertexPtr v2, intelVertexPtr v3)
183 {
184 GLuint vertsize = intel->vertex_size;
185 GLuint *vb = intelExtendInlinePrimitive(intel, 6 * vertsize);
186 int j;
187
188 COPY_DWORDS(j, vb, vertsize, v0);
189 COPY_DWORDS(j, vb, vertsize, v1);
190
191 /* If smooth shading, draw like a trifan which gives better
192 * rasterization. Otherwise draw as two triangles with provoking
193 * vertex in third position as required for flat shading.
194 */
195 if (intel->ctx.Light.ShadeModel == GL_FLAT) {
196 COPY_DWORDS(j, vb, vertsize, v3);
197 COPY_DWORDS(j, vb, vertsize, v1);
198 }
199 else {
200 COPY_DWORDS(j, vb, vertsize, v2);
201 COPY_DWORDS(j, vb, vertsize, v0);
202 }
203
204 COPY_DWORDS(j, vb, vertsize, v2);
205 COPY_DWORDS(j, vb, vertsize, v3);
206 }
207
208 static void
209 intel_draw_triangle(struct intel_context *intel,
210 intelVertexPtr v0, intelVertexPtr v1, intelVertexPtr v2)
211 {
212 GLuint vertsize = intel->vertex_size;
213 GLuint *vb = intelExtendInlinePrimitive(intel, 3 * vertsize);
214 int j;
215
216 COPY_DWORDS(j, vb, vertsize, v0);
217 COPY_DWORDS(j, vb, vertsize, v1);
218 COPY_DWORDS(j, vb, vertsize, v2);
219 }
220
221
222 static void
223 intel_draw_line(struct intel_context *intel,
224 intelVertexPtr v0, intelVertexPtr v1)
225 {
226 GLuint vertsize = intel->vertex_size;
227 GLuint *vb = intelExtendInlinePrimitive(intel, 2 * vertsize);
228 int j;
229
230 COPY_DWORDS(j, vb, vertsize, v0);
231 COPY_DWORDS(j, vb, vertsize, v1);
232 }
233
234
235 static void
236 intel_draw_point(struct intel_context *intel, intelVertexPtr v0)
237 {
238 GLuint vertsize = intel->vertex_size;
239 GLuint *vb = intelExtendInlinePrimitive(intel, vertsize);
240 int j;
241
242 /* Adjust for sub pixel position -- still required for conform. */
243 *(float *) &vb[0] = v0->v.x;
244 *(float *) &vb[1] = v0->v.y;
245 for (j = 2; j < vertsize; j++)
246 vb[j] = v0->ui[j];
247 }
248
249
250
251 /***********************************************************************
252 * Fixup for ARB_point_parameters *
253 ***********************************************************************/
254
255 /* Currently not working - VERT_ATTRIB_POINTSIZE isn't correctly
256 * represented in the fragment program InputsRead field.
257 */
258 static void
259 intel_atten_point(struct intel_context *intel, intelVertexPtr v0)
260 {
261 GLcontext *ctx = &intel->ctx;
262 GLfloat psz[4], col[4], restore_psz, restore_alpha;
263
264 _tnl_get_attr(ctx, v0, _TNL_ATTRIB_POINTSIZE, psz);
265 _tnl_get_attr(ctx, v0, _TNL_ATTRIB_COLOR0, col);
266
267 restore_psz = psz[0];
268 restore_alpha = col[3];
269
270 if (psz[0] >= ctx->Point.Threshold) {
271 psz[0] = MIN2(psz[0], ctx->Point.MaxSize);
272 }
273 else {
274 GLfloat dsize = psz[0] / ctx->Point.Threshold;
275 psz[0] = MAX2(ctx->Point.Threshold, ctx->Point.MinSize);
276 col[3] *= dsize * dsize;
277 }
278
279 if (psz[0] < 1.0)
280 psz[0] = 1.0;
281
282 if (restore_psz != psz[0] || restore_alpha != col[3]) {
283 _tnl_set_attr(ctx, v0, _TNL_ATTRIB_POINTSIZE, psz);
284 _tnl_set_attr(ctx, v0, _TNL_ATTRIB_COLOR0, col);
285
286 intel_draw_point(intel, v0);
287
288 psz[0] = restore_psz;
289 col[3] = restore_alpha;
290
291 _tnl_set_attr(ctx, v0, _TNL_ATTRIB_POINTSIZE, psz);
292 _tnl_set_attr(ctx, v0, _TNL_ATTRIB_COLOR0, col);
293 }
294 else
295 intel_draw_point(intel, v0);
296 }
297
298
299
300
301
302 /***********************************************************************
303 * Fixup for I915 WPOS texture coordinate *
304 ***********************************************************************/
305
306
307
308 static void
309 intel_wpos_triangle(struct intel_context *intel,
310 intelVertexPtr v0, intelVertexPtr v1, intelVertexPtr v2)
311 {
312 GLuint offset = intel->wpos_offset;
313 GLuint size = intel->wpos_size;
314 GLfloat *v0_wpos = (GLfloat *)((char *)v0 + offset);
315 GLfloat *v1_wpos = (GLfloat *)((char *)v1 + offset);
316 GLfloat *v2_wpos = (GLfloat *)((char *)v2 + offset);
317
318 __memcpy(v0_wpos, v0, size);
319 __memcpy(v1_wpos, v1, size);
320 __memcpy(v2_wpos, v2, size);
321
322 v0_wpos[1] = -v0_wpos[1] + intel->driDrawable->h;
323 v1_wpos[1] = -v1_wpos[1] + intel->driDrawable->h;
324 v2_wpos[1] = -v2_wpos[1] + intel->driDrawable->h;
325
326
327 intel_draw_triangle(intel, v0, v1, v2);
328 }
329
330
331 static void
332 intel_wpos_line(struct intel_context *intel,
333 intelVertexPtr v0, intelVertexPtr v1)
334 {
335 GLuint offset = intel->wpos_offset;
336 GLuint size = intel->wpos_size;
337 GLfloat *v0_wpos = (GLfloat *)((char *)v0 + offset);
338 GLfloat *v1_wpos = (GLfloat *)((char *)v1 + offset);
339
340 __memcpy(v0_wpos, v0, size);
341 __memcpy(v1_wpos, v1, size);
342
343 v0_wpos[1] = -v0_wpos[1] + intel->driDrawable->h;
344 v1_wpos[1] = -v1_wpos[1] + intel->driDrawable->h;
345
346 intel_draw_line(intel, v0, v1);
347 }
348
349
350 static void
351 intel_wpos_point(struct intel_context *intel, intelVertexPtr v0)
352 {
353 GLuint offset = intel->wpos_offset;
354 GLuint size = intel->wpos_size;
355 GLfloat *v0_wpos = (GLfloat *)((char *)v0 + offset);
356
357 __memcpy(v0_wpos, v0, size);
358 v0_wpos[1] = -v0_wpos[1] + intel->driDrawable->h;
359
360 intel_draw_point(intel, v0);
361 }
362
363
364
365
366
367
368 /***********************************************************************
369 * Macros for t_dd_tritmp.h to draw basic primitives *
370 ***********************************************************************/
371
372 #define TRI( a, b, c ) \
373 do { \
374 if (DO_FALLBACK) \
375 intel->draw_tri( intel, a, b, c ); \
376 else \
377 intel_draw_triangle( intel, a, b, c ); \
378 } while (0)
379
380 #define QUAD( a, b, c, d ) \
381 do { \
382 if (DO_FALLBACK) { \
383 intel->draw_tri( intel, a, b, d ); \
384 intel->draw_tri( intel, b, c, d ); \
385 } else \
386 intel_draw_quad( intel, a, b, c, d ); \
387 } while (0)
388
389 #define LINE( v0, v1 ) \
390 do { \
391 if (DO_FALLBACK) \
392 intel->draw_line( intel, v0, v1 ); \
393 else \
394 intel_draw_line( intel, v0, v1 ); \
395 } while (0)
396
397 #define POINT( v0 ) \
398 do { \
399 if (DO_FALLBACK) \
400 intel->draw_point( intel, v0 ); \
401 else \
402 intel_draw_point( intel, v0 ); \
403 } while (0)
404
405
406 /***********************************************************************
407 * Build render functions from dd templates *
408 ***********************************************************************/
409
410 #define INTEL_OFFSET_BIT 0x01
411 #define INTEL_TWOSIDE_BIT 0x02
412 #define INTEL_UNFILLED_BIT 0x04
413 #define INTEL_FALLBACK_BIT 0x08
414 #define INTEL_MAX_TRIFUNC 0x10
415
416
417 static struct
418 {
419 tnl_points_func points;
420 tnl_line_func line;
421 tnl_triangle_func triangle;
422 tnl_quad_func quad;
423 } rast_tab[INTEL_MAX_TRIFUNC];
424
425
426 #define DO_FALLBACK (IND & INTEL_FALLBACK_BIT)
427 #define DO_OFFSET (IND & INTEL_OFFSET_BIT)
428 #define DO_UNFILLED (IND & INTEL_UNFILLED_BIT)
429 #define DO_TWOSIDE (IND & INTEL_TWOSIDE_BIT)
430 #define DO_FLAT 0
431 #define DO_TRI 1
432 #define DO_QUAD 1
433 #define DO_LINE 1
434 #define DO_POINTS 1
435 #define DO_FULL_QUAD 1
436
437 #define HAVE_RGBA 1
438 #define HAVE_SPEC 1
439 #define HAVE_BACK_COLORS 0
440 #define HAVE_HW_FLATSHADE 1
441 #define VERTEX intelVertex
442 #define TAB rast_tab
443
444 /* Only used to pull back colors into vertices (ie, we know color is
445 * floating point).
446 */
447 #define INTEL_COLOR( dst, src ) \
448 do { \
449 UNCLAMPED_FLOAT_TO_UBYTE((dst)[0], (src)[2]); \
450 UNCLAMPED_FLOAT_TO_UBYTE((dst)[1], (src)[1]); \
451 UNCLAMPED_FLOAT_TO_UBYTE((dst)[2], (src)[0]); \
452 UNCLAMPED_FLOAT_TO_UBYTE((dst)[3], (src)[3]); \
453 } while (0)
454
455 #define INTEL_SPEC( dst, src ) \
456 do { \
457 UNCLAMPED_FLOAT_TO_UBYTE((dst)[0], (src)[2]); \
458 UNCLAMPED_FLOAT_TO_UBYTE((dst)[1], (src)[1]); \
459 UNCLAMPED_FLOAT_TO_UBYTE((dst)[2], (src)[0]); \
460 } while (0)
461
462
463 #define DEPTH_SCALE intel->polygon_offset_scale
464 #define UNFILLED_TRI unfilled_tri
465 #define UNFILLED_QUAD unfilled_quad
466 #define VERT_X(_v) _v->v.x
467 #define VERT_Y(_v) _v->v.y
468 #define VERT_Z(_v) _v->v.z
469 #define AREA_IS_CCW( a ) (a > 0)
470 #define GET_VERTEX(e) (intel->verts + (e * intel->vertex_size * sizeof(GLuint)))
471
472 #define VERT_SET_RGBA( v, c ) if (coloroffset) INTEL_COLOR( v->ub4[coloroffset], c )
473 #define VERT_COPY_RGBA( v0, v1 ) if (coloroffset) v0->ui[coloroffset] = v1->ui[coloroffset]
474 #define VERT_SAVE_RGBA( idx ) if (coloroffset) color[idx] = v[idx]->ui[coloroffset]
475 #define VERT_RESTORE_RGBA( idx ) if (coloroffset) v[idx]->ui[coloroffset] = color[idx]
476
477 #define VERT_SET_SPEC( v, c ) if (specoffset) INTEL_SPEC( v->ub4[specoffset], c )
478 #define VERT_COPY_SPEC( v0, v1 ) if (specoffset) COPY_3V(v0->ub4[specoffset], v1->ub4[specoffset])
479 #define VERT_SAVE_SPEC( idx ) if (specoffset) spec[idx] = v[idx]->ui[specoffset]
480 #define VERT_RESTORE_SPEC( idx ) if (specoffset) v[idx]->ui[specoffset] = spec[idx]
481
482 #define LOCAL_VARS(n) \
483 struct intel_context *intel = intel_context(ctx); \
484 GLuint color[n] = { 0, }, spec[n] = { 0, }; \
485 GLuint coloroffset = intel->coloroffset; \
486 GLboolean specoffset = intel->specoffset; \
487 (void) color; (void) spec; (void) coloroffset; (void) specoffset;
488
489
490 /***********************************************************************
491 * Helpers for rendering unfilled primitives *
492 ***********************************************************************/
493
494 static const GLuint hw_prim[GL_POLYGON + 1] = {
495 PRIM3D_POINTLIST,
496 PRIM3D_LINELIST,
497 PRIM3D_LINELIST,
498 PRIM3D_LINELIST,
499 PRIM3D_TRILIST,
500 PRIM3D_TRILIST,
501 PRIM3D_TRILIST,
502 PRIM3D_TRILIST,
503 PRIM3D_TRILIST,
504 PRIM3D_TRILIST
505 };
506
507 #define RASTERIZE(x) intelRasterPrimitive( ctx, x, hw_prim[x] )
508 #define RENDER_PRIMITIVE intel->render_primitive
509 #define TAG(x) x
510 #define IND INTEL_FALLBACK_BIT
511 #include "tnl_dd/t_dd_unfilled.h"
512 #undef IND
513
514 /***********************************************************************
515 * Generate GL render functions *
516 ***********************************************************************/
517
518 #define IND (0)
519 #define TAG(x) x
520 #include "tnl_dd/t_dd_tritmp.h"
521
522 #define IND (INTEL_OFFSET_BIT)
523 #define TAG(x) x##_offset
524 #include "tnl_dd/t_dd_tritmp.h"
525
526 #define IND (INTEL_TWOSIDE_BIT)
527 #define TAG(x) x##_twoside
528 #include "tnl_dd/t_dd_tritmp.h"
529
530 #define IND (INTEL_TWOSIDE_BIT|INTEL_OFFSET_BIT)
531 #define TAG(x) x##_twoside_offset
532 #include "tnl_dd/t_dd_tritmp.h"
533
534 #define IND (INTEL_UNFILLED_BIT)
535 #define TAG(x) x##_unfilled
536 #include "tnl_dd/t_dd_tritmp.h"
537
538 #define IND (INTEL_OFFSET_BIT|INTEL_UNFILLED_BIT)
539 #define TAG(x) x##_offset_unfilled
540 #include "tnl_dd/t_dd_tritmp.h"
541
542 #define IND (INTEL_TWOSIDE_BIT|INTEL_UNFILLED_BIT)
543 #define TAG(x) x##_twoside_unfilled
544 #include "tnl_dd/t_dd_tritmp.h"
545
546 #define IND (INTEL_TWOSIDE_BIT|INTEL_OFFSET_BIT|INTEL_UNFILLED_BIT)
547 #define TAG(x) x##_twoside_offset_unfilled
548 #include "tnl_dd/t_dd_tritmp.h"
549
550 #define IND (INTEL_FALLBACK_BIT)
551 #define TAG(x) x##_fallback
552 #include "tnl_dd/t_dd_tritmp.h"
553
554 #define IND (INTEL_OFFSET_BIT|INTEL_FALLBACK_BIT)
555 #define TAG(x) x##_offset_fallback
556 #include "tnl_dd/t_dd_tritmp.h"
557
558 #define IND (INTEL_TWOSIDE_BIT|INTEL_FALLBACK_BIT)
559 #define TAG(x) x##_twoside_fallback
560 #include "tnl_dd/t_dd_tritmp.h"
561
562 #define IND (INTEL_TWOSIDE_BIT|INTEL_OFFSET_BIT|INTEL_FALLBACK_BIT)
563 #define TAG(x) x##_twoside_offset_fallback
564 #include "tnl_dd/t_dd_tritmp.h"
565
566 #define IND (INTEL_UNFILLED_BIT|INTEL_FALLBACK_BIT)
567 #define TAG(x) x##_unfilled_fallback
568 #include "tnl_dd/t_dd_tritmp.h"
569
570 #define IND (INTEL_OFFSET_BIT|INTEL_UNFILLED_BIT|INTEL_FALLBACK_BIT)
571 #define TAG(x) x##_offset_unfilled_fallback
572 #include "tnl_dd/t_dd_tritmp.h"
573
574 #define IND (INTEL_TWOSIDE_BIT|INTEL_UNFILLED_BIT|INTEL_FALLBACK_BIT)
575 #define TAG(x) x##_twoside_unfilled_fallback
576 #include "tnl_dd/t_dd_tritmp.h"
577
578 #define IND (INTEL_TWOSIDE_BIT|INTEL_OFFSET_BIT|INTEL_UNFILLED_BIT| \
579 INTEL_FALLBACK_BIT)
580 #define TAG(x) x##_twoside_offset_unfilled_fallback
581 #include "tnl_dd/t_dd_tritmp.h"
582
583
584 static void
585 init_rast_tab(void)
586 {
587 init();
588 init_offset();
589 init_twoside();
590 init_twoside_offset();
591 init_unfilled();
592 init_offset_unfilled();
593 init_twoside_unfilled();
594 init_twoside_offset_unfilled();
595 init_fallback();
596 init_offset_fallback();
597 init_twoside_fallback();
598 init_twoside_offset_fallback();
599 init_unfilled_fallback();
600 init_offset_unfilled_fallback();
601 init_twoside_unfilled_fallback();
602 init_twoside_offset_unfilled_fallback();
603 }
604
605
606 /***********************************************************************
607 * Rasterization fallback helpers *
608 ***********************************************************************/
609
610
611 /* This code is hit only when a mix of accelerated and unaccelerated
612 * primitives are being drawn, and only for the unaccelerated
613 * primitives.
614 */
615 static void
616 intel_fallback_tri(struct intel_context *intel,
617 intelVertex * v0, intelVertex * v1, intelVertex * v2)
618 {
619 GLcontext *ctx = &intel->ctx;
620 SWvertex v[3];
621
622 if (0)
623 fprintf(stderr, "\n%s\n", __FUNCTION__);
624
625 INTEL_FIREVERTICES(intel);
626
627 _swsetup_Translate(ctx, v0, &v[0]);
628 _swsetup_Translate(ctx, v1, &v[1]);
629 _swsetup_Translate(ctx, v2, &v[2]);
630 intelSpanRenderStart(ctx);
631 _swrast_Triangle(ctx, &v[0], &v[1], &v[2]);
632 intelSpanRenderFinish(ctx);
633 }
634
635
636 static void
637 intel_fallback_line(struct intel_context *intel,
638 intelVertex * v0, intelVertex * v1)
639 {
640 GLcontext *ctx = &intel->ctx;
641 SWvertex v[2];
642
643 if (0)
644 fprintf(stderr, "\n%s\n", __FUNCTION__);
645
646 INTEL_FIREVERTICES(intel);
647
648 _swsetup_Translate(ctx, v0, &v[0]);
649 _swsetup_Translate(ctx, v1, &v[1]);
650 intelSpanRenderStart(ctx);
651 _swrast_Line(ctx, &v[0], &v[1]);
652 intelSpanRenderFinish(ctx);
653 }
654
655 static void
656 intel_fallback_point(struct intel_context *intel,
657 intelVertex * v0)
658 {
659 GLcontext *ctx = &intel->ctx;
660 SWvertex v[1];
661
662 if (0)
663 fprintf(stderr, "\n%s\n", __FUNCTION__);
664
665 INTEL_FIREVERTICES(intel);
666
667 _swsetup_Translate(ctx, v0, &v[0]);
668 intelSpanRenderStart(ctx);
669 _swrast_Point(ctx, &v[0]);
670 intelSpanRenderFinish(ctx);
671 }
672
673
674 /**********************************************************************/
675 /* Render unclipped begin/end objects */
676 /**********************************************************************/
677
678 #define IND 0
679 #define V(x) (intelVertex *)(vertptr + ((x)*vertsize*sizeof(GLuint)))
680 #define RENDER_POINTS( start, count ) \
681 for ( ; start < count ; start++) POINT( V(ELT(start)) );
682 #define RENDER_LINE( v0, v1 ) LINE( V(v0), V(v1) )
683 #define RENDER_TRI( v0, v1, v2 ) TRI( V(v0), V(v1), V(v2) )
684 #define RENDER_QUAD( v0, v1, v2, v3 ) QUAD( V(v0), V(v1), V(v2), V(v3) )
685 #define INIT(x) intelRenderPrimitive( ctx, x )
686 #undef LOCAL_VARS
687 #define LOCAL_VARS \
688 struct intel_context *intel = intel_context(ctx); \
689 GLubyte *vertptr = (GLubyte *)intel->verts; \
690 const GLuint vertsize = intel->vertex_size; \
691 const GLuint * const elt = TNL_CONTEXT(ctx)->vb.Elts; \
692 (void) elt;
693 #define RESET_STIPPLE
694 #define RESET_OCCLUSION
695 #define PRESERVE_VB_DEFS
696 #define ELT(x) x
697 #define TAG(x) intel_##x##_verts
698 #include "tnl/t_vb_rendertmp.h"
699 #undef ELT
700 #undef TAG
701 #define TAG(x) intel_##x##_elts
702 #define ELT(x) elt[x]
703 #include "tnl/t_vb_rendertmp.h"
704
705 /**********************************************************************/
706 /* Render clipped primitives */
707 /**********************************************************************/
708
709
710
711 static void
712 intelRenderClippedPoly(GLcontext * ctx, const GLuint * elts, GLuint n)
713 {
714 struct intel_context *intel = intel_context(ctx);
715 TNLcontext *tnl = TNL_CONTEXT(ctx);
716 struct vertex_buffer *VB = &TNL_CONTEXT(ctx)->vb;
717 GLuint prim = intel->render_primitive;
718
719 /* Render the new vertices as an unclipped polygon.
720 */
721 {
722 GLuint *tmp = VB->Elts;
723 VB->Elts = (GLuint *) elts;
724 tnl->Driver.Render.PrimTabElts[GL_POLYGON] (ctx, 0, n,
725 PRIM_BEGIN | PRIM_END);
726 VB->Elts = tmp;
727 }
728
729 /* Restore the render primitive
730 */
731 if (prim != GL_POLYGON)
732 tnl->Driver.Render.PrimitiveNotify(ctx, prim);
733 }
734
735 static void
736 intelRenderClippedLine(GLcontext * ctx, GLuint ii, GLuint jj)
737 {
738 TNLcontext *tnl = TNL_CONTEXT(ctx);
739
740 tnl->Driver.Render.Line(ctx, ii, jj);
741 }
742
743 static void
744 intelFastRenderClippedPoly(GLcontext * ctx, const GLuint * elts, GLuint n)
745 {
746 struct intel_context *intel = intel_context(ctx);
747 const GLuint vertsize = intel->vertex_size;
748 GLuint *vb = intelExtendInlinePrimitive(intel, (n - 2) * 3 * vertsize);
749 GLubyte *vertptr = (GLubyte *) intel->verts;
750 const GLuint *start = (const GLuint *) V(elts[0]);
751 int i, j;
752
753 for (i = 2; i < n; i++) {
754 COPY_DWORDS(j, vb, vertsize, V(elts[i - 1]));
755 COPY_DWORDS(j, vb, vertsize, V(elts[i]));
756 COPY_DWORDS(j, vb, vertsize, start);
757 }
758 }
759
760 /**********************************************************************/
761 /* Choose render functions */
762 /**********************************************************************/
763
764
765
766
767 #define ANY_FALLBACK_FLAGS (DD_LINE_STIPPLE | DD_TRI_STIPPLE | DD_POINT_ATTEN | DD_POINT_SMOOTH | DD_TRI_SMOOTH)
768 #define ANY_RASTER_FLAGS (DD_TRI_LIGHT_TWOSIDE | DD_TRI_OFFSET | DD_TRI_UNFILLED)
769
770 void
771 intelChooseRenderState(GLcontext * ctx)
772 {
773 TNLcontext *tnl = TNL_CONTEXT(ctx);
774 struct intel_context *intel = intel_context(ctx);
775 GLuint flags = ctx->_TriangleCaps;
776 const struct gl_fragment_program *fprog = ctx->FragmentProgram._Current;
777 GLboolean have_wpos = (fprog && (fprog->Base.InputsRead & FRAG_BIT_WPOS));
778 GLuint index = 0;
779
780 if (INTEL_DEBUG & DEBUG_STATE)
781 fprintf(stderr, "\n%s\n", __FUNCTION__);
782
783 if ((flags & (ANY_FALLBACK_FLAGS | ANY_RASTER_FLAGS)) || have_wpos) {
784
785 if (flags & ANY_RASTER_FLAGS) {
786 if (flags & DD_TRI_LIGHT_TWOSIDE)
787 index |= INTEL_TWOSIDE_BIT;
788 if (flags & DD_TRI_OFFSET)
789 index |= INTEL_OFFSET_BIT;
790 if (flags & DD_TRI_UNFILLED)
791 index |= INTEL_UNFILLED_BIT;
792 }
793
794 if (have_wpos) {
795 intel->draw_point = intel_wpos_point;
796 intel->draw_line = intel_wpos_line;
797 intel->draw_tri = intel_wpos_triangle;
798
799 /* Make sure these get called:
800 */
801 index |= INTEL_FALLBACK_BIT;
802 }
803 else {
804 intel->draw_point = intel_draw_point;
805 intel->draw_line = intel_draw_line;
806 intel->draw_tri = intel_draw_triangle;
807 }
808
809 /* Hook in fallbacks for specific primitives.
810 */
811 if (flags & ANY_FALLBACK_FLAGS) {
812 if (flags & DD_LINE_STIPPLE)
813 intel->draw_line = intel_fallback_line;
814
815 if ((flags & DD_TRI_STIPPLE) && !intel->hw_stipple)
816 intel->draw_tri = intel_fallback_tri;
817
818 if (flags & DD_TRI_SMOOTH) {
819 if (intel->strict_conformance)
820 intel->draw_tri = intel_fallback_tri;
821 }
822
823 if (flags & DD_POINT_ATTEN) {
824 if (0)
825 intel->draw_point = intel_atten_point;
826 else
827 intel->draw_point = intel_fallback_point;
828 }
829
830 if (flags & DD_POINT_SMOOTH) {
831 if (intel->strict_conformance)
832 intel->draw_point = intel_fallback_point;
833 }
834
835 index |= INTEL_FALLBACK_BIT;
836 }
837 }
838
839 if (intel->RenderIndex != index) {
840 intel->RenderIndex = index;
841
842 tnl->Driver.Render.Points = rast_tab[index].points;
843 tnl->Driver.Render.Line = rast_tab[index].line;
844 tnl->Driver.Render.Triangle = rast_tab[index].triangle;
845 tnl->Driver.Render.Quad = rast_tab[index].quad;
846
847 if (index == 0) {
848 tnl->Driver.Render.PrimTabVerts = intel_render_tab_verts;
849 tnl->Driver.Render.PrimTabElts = intel_render_tab_elts;
850 tnl->Driver.Render.ClippedLine = line; /* from tritmp.h */
851 tnl->Driver.Render.ClippedPolygon = intelFastRenderClippedPoly;
852 }
853 else {
854 tnl->Driver.Render.PrimTabVerts = _tnl_render_tab_verts;
855 tnl->Driver.Render.PrimTabElts = _tnl_render_tab_elts;
856 tnl->Driver.Render.ClippedLine = intelRenderClippedLine;
857 tnl->Driver.Render.ClippedPolygon = intelRenderClippedPoly;
858 }
859 }
860 }
861
862 static const GLenum reduced_prim[GL_POLYGON + 1] = {
863 GL_POINTS,
864 GL_LINES,
865 GL_LINES,
866 GL_LINES,
867 GL_TRIANGLES,
868 GL_TRIANGLES,
869 GL_TRIANGLES,
870 GL_TRIANGLES,
871 GL_TRIANGLES,
872 GL_TRIANGLES
873 };
874
875
876 /**********************************************************************/
877 /* High level hooks for t_vb_render.c */
878 /**********************************************************************/
879
880
881
882
883 static void
884 intelRunPipeline(GLcontext * ctx)
885 {
886 struct intel_context *intel = intel_context(ctx);
887
888 _mesa_lock_context_textures(ctx);
889
890 if (ctx->NewState)
891 _mesa_update_state_locked(ctx);
892
893 if (intel->NewGLState) {
894 if (intel->NewGLState & _NEW_TEXTURE) {
895 intel->vtbl.update_texture_state(intel);
896 }
897
898 if (!intel->Fallback) {
899 if (intel->NewGLState & _INTEL_NEW_RENDERSTATE)
900 intelChooseRenderState(ctx);
901 }
902
903 intel->NewGLState = 0;
904 }
905
906 _tnl_run_pipeline(ctx);
907
908 _mesa_unlock_context_textures(ctx);
909 }
910
911 static void
912 intelRenderStart(GLcontext * ctx)
913 {
914 struct intel_context *intel = intel_context(ctx);
915
916 intel->vtbl.render_start(intel_context(ctx));
917 intel->vtbl.emit_state(intel);
918 }
919
920 static void
921 intelRenderFinish(GLcontext * ctx)
922 {
923 struct intel_context *intel = intel_context(ctx);
924
925 if (intel->RenderIndex & INTEL_FALLBACK_BIT)
926 _swrast_flush(ctx);
927
928 INTEL_FIREVERTICES(intel);
929 }
930
931
932
933
934 /* System to flush dma and emit state changes based on the rasterized
935 * primitive.
936 */
937 static void
938 intelRasterPrimitive(GLcontext * ctx, GLenum rprim, GLuint hwprim)
939 {
940 struct intel_context *intel = intel_context(ctx);
941
942 if (0)
943 fprintf(stderr, "%s %s %x\n", __FUNCTION__,
944 _mesa_lookup_enum_by_nr(rprim), hwprim);
945
946 intel->vtbl.reduced_primitive_state(intel, rprim);
947
948 /* Start a new primitive. Arrange to have it flushed later on.
949 */
950 if (hwprim != intel->prim.primitive) {
951 INTEL_FIREVERTICES(intel);
952
953 intelStartInlinePrimitive(intel, hwprim, LOOP_CLIPRECTS);
954 }
955 }
956
957
958 /*
959 */
960 static void
961 intelRenderPrimitive(GLcontext * ctx, GLenum prim)
962 {
963 struct intel_context *intel = intel_context(ctx);
964
965 if (0)
966 fprintf(stderr, "%s %s\n", __FUNCTION__, _mesa_lookup_enum_by_nr(prim));
967
968 /* Let some clipping routines know which primitive they're dealing
969 * with.
970 */
971 intel->render_primitive = prim;
972
973 /* Shortcircuit this when called from t_dd_rendertmp.h for unfilled
974 * triangles. The rasterized primitive will always be reset by
975 * lower level functions in that case, potentially pingponging the
976 * state:
977 */
978 if (reduced_prim[prim] == GL_TRIANGLES &&
979 (ctx->_TriangleCaps & DD_TRI_UNFILLED))
980 return;
981
982 /* Set some primitive-dependent state and Start? a new primitive.
983 */
984 intelRasterPrimitive(ctx, reduced_prim[prim], hw_prim[prim]);
985 }
986
987
988 /**********************************************************************/
989 /* Transition to/from hardware rasterization. */
990 /**********************************************************************/
991
992 static char *fallbackStrings[] = {
993 [0] = "Draw buffer",
994 [1] = "Read buffer",
995 [2] = "Depth buffer",
996 [3] = "Stencil buffer",
997 [4] = "User disable",
998 [5] = "Render mode",
999
1000 [12] = "Texture",
1001 [13] = "Color mask",
1002 [14] = "Stencil",
1003 [15] = "Stipple",
1004 [16] = "Program",
1005 [17] = "Logic op",
1006 [18] = "Smooth polygon",
1007 [19] = "Smooth point",
1008 };
1009
1010
1011 static char *
1012 getFallbackString(GLuint bit)
1013 {
1014 int i = 0;
1015 while (bit > 1) {
1016 i++;
1017 bit >>= 1;
1018 }
1019 return fallbackStrings[i];
1020 }
1021
1022
1023
1024 void
1025 intelFallback(struct intel_context *intel, GLuint bit, GLboolean mode)
1026 {
1027 GLcontext *ctx = &intel->ctx;
1028 TNLcontext *tnl = TNL_CONTEXT(ctx);
1029 GLuint oldfallback = intel->Fallback;
1030
1031 if (mode) {
1032 intel->Fallback |= bit;
1033 if (oldfallback == 0) {
1034 intelFlush(ctx);
1035 if (INTEL_DEBUG & DEBUG_FALLBACKS)
1036 fprintf(stderr, "ENTER FALLBACK %x: %s\n",
1037 bit, getFallbackString(bit));
1038 _swsetup_Wakeup(ctx);
1039 intel->RenderIndex = ~0;
1040 }
1041 }
1042 else {
1043 intel->Fallback &= ~bit;
1044 if (oldfallback == bit) {
1045 _swrast_flush(ctx);
1046 if (INTEL_DEBUG & DEBUG_FALLBACKS)
1047 fprintf(stderr, "LEAVE FALLBACK %s\n", getFallbackString(bit));
1048 tnl->Driver.Render.Start = intelRenderStart;
1049 tnl->Driver.Render.PrimitiveNotify = intelRenderPrimitive;
1050 tnl->Driver.Render.Finish = intelRenderFinish;
1051 tnl->Driver.Render.BuildVertices = _tnl_build_vertices;
1052 tnl->Driver.Render.CopyPV = _tnl_copy_pv;
1053 tnl->Driver.Render.Interp = _tnl_interp;
1054
1055 _tnl_invalidate_vertex_state(ctx, ~0);
1056 _tnl_invalidate_vertices(ctx, ~0);
1057 _tnl_install_attrs(ctx,
1058 intel->vertex_attrs,
1059 intel->vertex_attr_count,
1060 intel->ViewportMatrix.m, 0);
1061
1062 intel->NewGLState |= _INTEL_NEW_RENDERSTATE;
1063 }
1064 }
1065 }
1066
1067 union fi
1068 {
1069 GLfloat f;
1070 GLint i;
1071 };
1072
1073
1074 /**********************************************************************/
1075 /* Used only with the metaops callbacks. */
1076 /**********************************************************************/
1077 static void
1078 intel_meta_draw_poly(struct intel_context *intel,
1079 GLuint n,
1080 GLfloat xy[][2],
1081 GLfloat z, GLuint color, GLfloat tex[][2])
1082 {
1083 union fi *vb;
1084 GLint i;
1085 GLboolean was_locked = intel->locked;
1086
1087 if (!was_locked)
1088 LOCK_HARDWARE(intel);
1089
1090 /* All 3d primitives should be emitted with LOOP_CLIPRECTS,
1091 * otherwise the drawing origin (DR4) might not be set correctly.
1092 */
1093 intelStartInlinePrimitive(intel, PRIM3D_TRIFAN, LOOP_CLIPRECTS);
1094 vb = (union fi *) intelExtendInlinePrimitive(intel, n * 6);
1095
1096 for (i = 0; i < n; i++) {
1097 vb[0].f = xy[i][0];
1098 vb[1].f = xy[i][1];
1099 vb[2].f = z;
1100 vb[3].i = color;
1101 vb[4].f = tex[i][0];
1102 vb[5].f = tex[i][1];
1103 vb += 6;
1104 }
1105
1106 INTEL_FIREVERTICES(intel);
1107
1108 if (!was_locked)
1109 UNLOCK_HARDWARE(intel);
1110 }
1111
1112 static void
1113 intel_meta_draw_quad(struct intel_context *intel,
1114 GLfloat x0, GLfloat x1,
1115 GLfloat y0, GLfloat y1,
1116 GLfloat z,
1117 GLuint color,
1118 GLfloat s0, GLfloat s1, GLfloat t0, GLfloat t1)
1119 {
1120 GLfloat xy[4][2];
1121 GLfloat tex[4][2];
1122
1123 xy[0][0] = x0;
1124 xy[0][1] = y0;
1125 xy[1][0] = x1;
1126 xy[1][1] = y0;
1127 xy[2][0] = x1;
1128 xy[2][1] = y1;
1129 xy[3][0] = x0;
1130 xy[3][1] = y1;
1131
1132 tex[0][0] = s0;
1133 tex[0][1] = t0;
1134 tex[1][0] = s1;
1135 tex[1][1] = t0;
1136 tex[2][0] = s1;
1137 tex[2][1] = t1;
1138 tex[3][0] = s0;
1139 tex[3][1] = t1;
1140
1141 intel_meta_draw_poly(intel, 4, xy, z, color, tex);
1142 }
1143
1144
1145
1146 /**********************************************************************/
1147 /* Initialization. */
1148 /**********************************************************************/
1149
1150
1151 void
1152 intelInitTriFuncs(GLcontext * ctx)
1153 {
1154 struct intel_context *intel = intel_context(ctx);
1155 TNLcontext *tnl = TNL_CONTEXT(ctx);
1156 static int firsttime = 1;
1157
1158 if (firsttime) {
1159 init_rast_tab();
1160 firsttime = 0;
1161 }
1162
1163 tnl->Driver.RunPipeline = intelRunPipeline;
1164 tnl->Driver.Render.Start = intelRenderStart;
1165 tnl->Driver.Render.Finish = intelRenderFinish;
1166 tnl->Driver.Render.PrimitiveNotify = intelRenderPrimitive;
1167 tnl->Driver.Render.ResetLineStipple = _swrast_ResetLineStipple;
1168 tnl->Driver.Render.BuildVertices = _tnl_build_vertices;
1169 tnl->Driver.Render.CopyPV = _tnl_copy_pv;
1170 tnl->Driver.Render.Interp = _tnl_interp;
1171
1172 intel->vtbl.meta_draw_quad = intel_meta_draw_quad;
1173 }