2 * Copyright 2000-2001 VA Linux Systems, Inc.
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * on the rights to use, copy, modify, merge, publish, distribute, sub
9 * license, and/or sell copies of the Software, and to permit persons to whom
10 * the Software is furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
19 * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
20 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22 * OTHER DEALINGS IN THE SOFTWARE.
25 * Keith Whitwell <keith@tungstengraphics.com>
27 /* $XFree86: xc/lib/GL/mesa/src/drv/mga/mgatris.c,v 1.10 2002/10/30 12:51:36 alanh Exp $ */
32 #include "swrast/swrast.h"
33 #include "swrast_setup/swrast_setup.h"
34 #include "tnl/t_context.h"
35 #include "tnl/t_pipeline.h"
38 #include "mgacontext.h"
45 static void mgaRenderPrimitive( GLcontext
*ctx
, GLenum prim
);
47 /***********************************************************************
48 * Functions to draw basic primitives *
49 ***********************************************************************/
52 #if defined (USE_X86_ASM)
53 #define EMIT_VERT( j, vb, vertex_size, v ) \
55 __asm__ __volatile__( "rep ; movsl" \
56 : "=%c" (j), "=D" (vb), "=S" (__tmp) \
57 : "0" (vertex_size), \
62 #define EMIT_VERT( j, vb, vertex_size, v ) \
64 for ( j = 0 ; j < vertex_size ; j++ ) \
70 static void __inline__
mga_draw_triangle( mgaContextPtr mmesa
,
75 GLuint vertex_size
= mmesa
->vertex_size
;
76 GLuint
*vb
= mgaAllocDmaLow( mmesa
, 3 * 4 * vertex_size
);
79 EMIT_VERT( j
, vb
, vertex_size
, v0
);
80 EMIT_VERT( j
, vb
, vertex_size
, v1
);
81 EMIT_VERT( j
, vb
, vertex_size
, v2
);
85 static void __inline__
mga_draw_quad( mgaContextPtr mmesa
,
91 GLuint vertex_size
= mmesa
->vertex_size
;
92 GLuint
*vb
= mgaAllocDmaLow( mmesa
, 6 * 4 * vertex_size
);
95 EMIT_VERT( j
, vb
, vertex_size
, v0
);
96 EMIT_VERT( j
, vb
, vertex_size
, v1
);
97 EMIT_VERT( j
, vb
, vertex_size
, v3
);
98 EMIT_VERT( j
, vb
, vertex_size
, v1
);
99 EMIT_VERT( j
, vb
, vertex_size
, v2
);
100 EMIT_VERT( j
, vb
, vertex_size
, v3
);
104 static __inline__
void mga_draw_point( mgaContextPtr mmesa
,
107 GLfloat sz
= mmesa
->glCtx
->Point
._Size
* .5;
108 int vertex_size
= mmesa
->vertex_size
;
109 GLuint
*vb
= mgaAllocDmaLow( mmesa
, 6 * 4 * vertex_size
);
113 v0
->v
.x
+= PNT_X_OFFSET
- TRI_X_OFFSET
;
114 v0
->v
.y
+= PNT_Y_OFFSET
- TRI_Y_OFFSET
;
117 /* Draw a point as two triangles.
119 *(float *)&vb
[0] = tmp
->v
.x
- sz
;
120 *(float *)&vb
[1] = tmp
->v
.y
- sz
;
121 for (j
= 2 ; j
< vertex_size
; j
++)
125 *(float *)&vb
[0] = tmp
->v
.x
+ sz
;
126 *(float *)&vb
[1] = tmp
->v
.y
- sz
;
127 for (j
= 2 ; j
< vertex_size
; j
++)
131 *(float *)&vb
[0] = tmp
->v
.x
+ sz
;
132 *(float *)&vb
[1] = tmp
->v
.y
+ sz
;
133 for (j
= 2 ; j
< vertex_size
; j
++)
137 *(float *)&vb
[0] = tmp
->v
.x
+ sz
;
138 *(float *)&vb
[1] = tmp
->v
.y
+ sz
;
139 for (j
= 2 ; j
< vertex_size
; j
++)
143 *(float *)&vb
[0] = tmp
->v
.x
- sz
;
144 *(float *)&vb
[1] = tmp
->v
.y
+ sz
;
145 for (j
= 2 ; j
< vertex_size
; j
++)
149 *(float *)&vb
[0] = tmp
->v
.x
- sz
;
150 *(float *)&vb
[1] = tmp
->v
.y
- sz
;
151 for (j
= 2 ; j
< vertex_size
; j
++)
155 v0
->v
.x
-= PNT_X_OFFSET
- TRI_X_OFFSET
;
156 v0
->v
.y
-= PNT_Y_OFFSET
- TRI_Y_OFFSET
;
161 static __inline__
void mga_draw_line( mgaContextPtr mmesa
,
165 GLuint vertex_size
= mmesa
->vertex_size
;
166 GLuint
*vb
= mgaAllocDmaLow( mmesa
, 6 * 4 * vertex_size
);
167 GLfloat dx
, dy
, ix
, iy
;
168 GLfloat width
= mmesa
->glCtx
->Line
._Width
;
172 v0
->v
.x
+= LINE_X_OFFSET
- TRI_X_OFFSET
;
173 v0
->v
.y
+= LINE_Y_OFFSET
- TRI_Y_OFFSET
;
174 v1
->v
.x
+= LINE_X_OFFSET
- TRI_X_OFFSET
;
175 v1
->v
.y
+= LINE_Y_OFFSET
- TRI_Y_OFFSET
;
178 dx
= v0
->v
.x
- v1
->v
.x
;
179 dy
= v0
->v
.y
- v1
->v
.y
;
181 ix
= width
* .5; iy
= 0;
182 if (dx
* dx
> dy
* dy
) {
186 *(float *)&vb
[0] = v0
->v
.x
- ix
;
187 *(float *)&vb
[1] = v0
->v
.y
- iy
;
188 for (j
= 2 ; j
< vertex_size
; j
++)
192 *(float *)&vb
[0] = v1
->v
.x
+ ix
;
193 *(float *)&vb
[1] = v1
->v
.y
+ iy
;
194 for (j
= 2 ; j
< vertex_size
; j
++)
198 *(float *)&vb
[0] = v0
->v
.x
+ ix
;
199 *(float *)&vb
[1] = v0
->v
.y
+ iy
;
200 for (j
= 2 ; j
< vertex_size
; j
++)
204 *(float *)&vb
[0] = v0
->v
.x
- ix
;
205 *(float *)&vb
[1] = v0
->v
.y
- iy
;
206 for (j
= 2 ; j
< vertex_size
; j
++)
210 *(float *)&vb
[0] = v1
->v
.x
- ix
;
211 *(float *)&vb
[1] = v1
->v
.y
- iy
;
212 for (j
= 2 ; j
< vertex_size
; j
++)
216 *(float *)&vb
[0] = v1
->v
.x
+ ix
;
217 *(float *)&vb
[1] = v1
->v
.y
+ iy
;
218 for (j
= 2 ; j
< vertex_size
; j
++)
223 v0
->v
.x
-= LINE_X_OFFSET
- TRI_X_OFFSET
;
224 v0
->v
.y
-= LINE_Y_OFFSET
- TRI_Y_OFFSET
;
225 v1
->v
.x
-= LINE_X_OFFSET
- TRI_X_OFFSET
;
226 v1
->v
.y
-= LINE_Y_OFFSET
- TRI_Y_OFFSET
;
230 /***********************************************************************
231 * Macros for t_dd_tritmp.h to draw basic primitives *
232 ***********************************************************************/
234 #define TRI( a, b, c ) \
237 mmesa->draw_tri( mmesa, a, b, c ); \
239 mga_draw_triangle( mmesa, a, b, c ); \
242 #define QUAD( a, b, c, d ) \
245 mmesa->draw_tri( mmesa, a, b, d ); \
246 mmesa->draw_tri( mmesa, b, c, d ); \
248 mga_draw_quad( mmesa, a, b, c, d ); \
252 #define LINE( v0, v1 ) \
255 mmesa->draw_line( mmesa, v0, v1 ); \
257 mga_draw_line( mmesa, v0, v1 ); \
261 #define POINT( v0 ) \
264 mmesa->draw_point( mmesa, v0 ); \
266 mga_draw_point( mmesa, v0 ); \
271 /***********************************************************************
272 * Fallback to swrast for basic primitives *
273 ***********************************************************************/
275 /* This code is hit only when a mix of accelerated and unaccelerated
276 * primitives are being drawn, and only for the unaccelerated
281 mga_fallback_tri( mgaContextPtr mmesa
,
286 GLcontext
*ctx
= mmesa
->glCtx
;
288 mga_translate_vertex( ctx
, v0
, &v
[0] );
289 mga_translate_vertex( ctx
, v1
, &v
[1] );
290 mga_translate_vertex( ctx
, v2
, &v
[2] );
291 _swrast_Triangle( ctx
, &v
[0], &v
[1], &v
[2] );
296 mga_fallback_line( mgaContextPtr mmesa
,
300 GLcontext
*ctx
= mmesa
->glCtx
;
302 mga_translate_vertex( ctx
, v0
, &v
[0] );
303 mga_translate_vertex( ctx
, v1
, &v
[1] );
304 _swrast_Line( ctx
, &v
[0], &v
[1] );
309 mga_fallback_point( mgaContextPtr mmesa
,
312 GLcontext
*ctx
= mmesa
->glCtx
;
314 mga_translate_vertex( ctx
, v0
, &v
[0] );
315 _swrast_Point( ctx
, &v
[0] );
318 /***********************************************************************
319 * Build render functions from dd templates *
320 ***********************************************************************/
323 #define MGA_UNFILLED_BIT 0x1
324 #define MGA_OFFSET_BIT 0x2
325 #define MGA_TWOSIDE_BIT 0x4
326 #define MGA_FLAT_BIT 0x8 /* mga can't flatshade? */
327 #define MGA_FALLBACK_BIT 0x10
328 #define MGA_MAX_TRIFUNC 0x20
333 triangle_func triangle
;
335 } rast_tab
[MGA_MAX_TRIFUNC
];
337 #define DO_FALLBACK (IND & MGA_FALLBACK_BIT)
338 #define DO_OFFSET (IND & MGA_OFFSET_BIT)
339 #define DO_UNFILLED (IND & MGA_UNFILLED_BIT)
340 #define DO_TWOSIDE (IND & MGA_TWOSIDE_BIT)
341 #define DO_FLAT (IND & MGA_FLAT_BIT)
346 #define DO_FULL_QUAD 1
349 #define HAVE_BACK_COLORS 0
351 #define HAVE_HW_FLATSHADE 0
352 #define VERTEX mgaVertex
355 #define MGA_COLOR( dst, src ) \
363 #define MGA_SPEC( dst, src ) \
370 #define DEPTH_SCALE mmesa->depth_scale
371 #define UNFILLED_TRI unfilled_tri
372 #define UNFILLED_QUAD unfilled_quad
373 #define VERT_X(_v) _v->v.x
374 #define VERT_Y(_v) _v->v.y
375 #define VERT_Z(_v) _v->v.z
376 #define AREA_IS_CCW( a ) (a > 0)
377 #define GET_VERTEX(e) (mmesa->verts + (e<<mmesa->vertex_stride_shift))
379 #define VERT_SET_RGBA( v, c ) MGA_COLOR( v->ub4[4], c )
380 #define VERT_COPY_RGBA( v0, v1 ) v0->ui[4] = v1->ui[4]
381 #define VERT_SAVE_RGBA( idx ) color[idx] = v[idx]->ui[4]
382 #define VERT_RESTORE_RGBA( idx ) v[idx]->ui[4] = color[idx]
384 #define VERT_SET_SPEC( v, c ) MGA_SPEC( v->ub4[5], c )
385 #define VERT_COPY_SPEC( v0, v1 ) COPY_3V(v0->ub4[5], v1->ub4[5])
386 #define VERT_SAVE_SPEC( idx ) spec[idx] = v[idx]->ui[5]
387 #define VERT_RESTORE_SPEC( idx ) v[idx]->ui[5] = spec[idx]
389 #define LOCAL_VARS(n) \
390 mgaContextPtr mmesa = MGA_CONTEXT(ctx); \
391 GLuint color[n], spec[n]; \
392 (void) color; (void) spec;
396 /***********************************************************************
397 * Functions to draw basic unfilled primitives *
398 ***********************************************************************/
400 #define RASTERIZE(x) if (mmesa->raster_primitive != x) \
401 mgaRasterPrimitive( ctx, x, MGA_WA_TRIANGLES )
402 #define RENDER_PRIMITIVE mmesa->render_primitive
403 #define IND MGA_FALLBACK_BIT
405 #include "tnl_dd/t_dd_unfilled.h"
408 /***********************************************************************
409 * Functions to draw GL primitives *
410 ***********************************************************************/
414 #include "tnl_dd/t_dd_tritmp.h"
416 #define IND (MGA_OFFSET_BIT)
417 #define TAG(x) x##_offset
418 #include "tnl_dd/t_dd_tritmp.h"
420 #define IND (MGA_TWOSIDE_BIT)
421 #define TAG(x) x##_twoside
422 #include "tnl_dd/t_dd_tritmp.h"
424 #define IND (MGA_TWOSIDE_BIT|MGA_OFFSET_BIT)
425 #define TAG(x) x##_twoside_offset
426 #include "tnl_dd/t_dd_tritmp.h"
428 #define IND (MGA_UNFILLED_BIT)
429 #define TAG(x) x##_unfilled
430 #include "tnl_dd/t_dd_tritmp.h"
432 #define IND (MGA_OFFSET_BIT|MGA_UNFILLED_BIT)
433 #define TAG(x) x##_offset_unfilled
434 #include "tnl_dd/t_dd_tritmp.h"
436 #define IND (MGA_TWOSIDE_BIT|MGA_UNFILLED_BIT)
437 #define TAG(x) x##_twoside_unfilled
438 #include "tnl_dd/t_dd_tritmp.h"
440 #define IND (MGA_TWOSIDE_BIT|MGA_OFFSET_BIT|MGA_UNFILLED_BIT)
441 #define TAG(x) x##_twoside_offset_unfilled
442 #include "tnl_dd/t_dd_tritmp.h"
444 #define IND (MGA_FALLBACK_BIT)
445 #define TAG(x) x##_fallback
446 #include "tnl_dd/t_dd_tritmp.h"
448 #define IND (MGA_OFFSET_BIT|MGA_FALLBACK_BIT)
449 #define TAG(x) x##_offset_fallback
450 #include "tnl_dd/t_dd_tritmp.h"
452 #define IND (MGA_TWOSIDE_BIT|MGA_FALLBACK_BIT)
453 #define TAG(x) x##_twoside_fallback
454 #include "tnl_dd/t_dd_tritmp.h"
456 #define IND (MGA_TWOSIDE_BIT|MGA_OFFSET_BIT|MGA_FALLBACK_BIT)
457 #define TAG(x) x##_twoside_offset_fallback
458 #include "tnl_dd/t_dd_tritmp.h"
460 #define IND (MGA_UNFILLED_BIT|MGA_FALLBACK_BIT)
461 #define TAG(x) x##_unfilled_fallback
462 #include "tnl_dd/t_dd_tritmp.h"
464 #define IND (MGA_OFFSET_BIT|MGA_UNFILLED_BIT|MGA_FALLBACK_BIT)
465 #define TAG(x) x##_offset_unfilled_fallback
466 #include "tnl_dd/t_dd_tritmp.h"
468 #define IND (MGA_TWOSIDE_BIT|MGA_UNFILLED_BIT|MGA_FALLBACK_BIT)
469 #define TAG(x) x##_twoside_unfilled_fallback
470 #include "tnl_dd/t_dd_tritmp.h"
472 #define IND (MGA_TWOSIDE_BIT|MGA_OFFSET_BIT|MGA_UNFILLED_BIT| \
474 #define TAG(x) x##_twoside_offset_unfilled_fallback
475 #include "tnl_dd/t_dd_tritmp.h"
478 /* Mga doesn't support provoking-vertex flat-shading?
480 #define IND (MGA_FLAT_BIT)
481 #define TAG(x) x##_flat
482 #include "tnl_dd/t_dd_tritmp.h"
484 #define IND (MGA_OFFSET_BIT|MGA_FLAT_BIT)
485 #define TAG(x) x##_offset_flat
486 #include "tnl_dd/t_dd_tritmp.h"
488 #define IND (MGA_TWOSIDE_BIT|MGA_FLAT_BIT)
489 #define TAG(x) x##_twoside_flat
490 #include "tnl_dd/t_dd_tritmp.h"
492 #define IND (MGA_TWOSIDE_BIT|MGA_OFFSET_BIT|MGA_FLAT_BIT)
493 #define TAG(x) x##_twoside_offset_flat
494 #include "tnl_dd/t_dd_tritmp.h"
496 #define IND (MGA_UNFILLED_BIT|MGA_FLAT_BIT)
497 #define TAG(x) x##_unfilled_flat
498 #include "tnl_dd/t_dd_tritmp.h"
500 #define IND (MGA_OFFSET_BIT|MGA_UNFILLED_BIT|MGA_FLAT_BIT)
501 #define TAG(x) x##_offset_unfilled_flat
502 #include "tnl_dd/t_dd_tritmp.h"
504 #define IND (MGA_TWOSIDE_BIT|MGA_UNFILLED_BIT|MGA_FLAT_BIT)
505 #define TAG(x) x##_twoside_unfilled_flat
506 #include "tnl_dd/t_dd_tritmp.h"
508 #define IND (MGA_TWOSIDE_BIT|MGA_OFFSET_BIT|MGA_UNFILLED_BIT|MGA_FLAT_BIT)
509 #define TAG(x) x##_twoside_offset_unfilled_flat
510 #include "tnl_dd/t_dd_tritmp.h"
512 #define IND (MGA_FALLBACK_BIT|MGA_FLAT_BIT)
513 #define TAG(x) x##_fallback_flat
514 #include "tnl_dd/t_dd_tritmp.h"
516 #define IND (MGA_OFFSET_BIT|MGA_FALLBACK_BIT|MGA_FLAT_BIT)
517 #define TAG(x) x##_offset_fallback_flat
518 #include "tnl_dd/t_dd_tritmp.h"
520 #define IND (MGA_TWOSIDE_BIT|MGA_FALLBACK_BIT|MGA_FLAT_BIT)
521 #define TAG(x) x##_twoside_fallback_flat
522 #include "tnl_dd/t_dd_tritmp.h"
524 #define IND (MGA_TWOSIDE_BIT|MGA_OFFSET_BIT|MGA_FALLBACK_BIT|MGA_FLAT_BIT)
525 #define TAG(x) x##_twoside_offset_fallback_flat
526 #include "tnl_dd/t_dd_tritmp.h"
528 #define IND (MGA_UNFILLED_BIT|MGA_FALLBACK_BIT|MGA_FLAT_BIT)
529 #define TAG(x) x##_unfilled_fallback_flat
530 #include "tnl_dd/t_dd_tritmp.h"
532 #define IND (MGA_OFFSET_BIT|MGA_UNFILLED_BIT|MGA_FALLBACK_BIT|MGA_FLAT_BIT)
533 #define TAG(x) x##_offset_unfilled_fallback_flat
534 #include "tnl_dd/t_dd_tritmp.h"
536 #define IND (MGA_TWOSIDE_BIT|MGA_UNFILLED_BIT|MGA_FALLBACK_BIT|MGA_FLAT_BIT)
537 #define TAG(x) x##_twoside_unfilled_fallback_flat
538 #include "tnl_dd/t_dd_tritmp.h"
540 #define IND (MGA_TWOSIDE_BIT|MGA_OFFSET_BIT|MGA_UNFILLED_BIT| \
541 MGA_FALLBACK_BIT|MGA_FLAT_BIT)
542 #define TAG(x) x##_twoside_offset_unfilled_fallback_flat
543 #include "tnl_dd/t_dd_tritmp.h"
546 static void init_rast_tab( void )
551 init_twoside_offset();
553 init_offset_unfilled();
554 init_twoside_unfilled();
555 init_twoside_offset_unfilled();
557 init_offset_fallback();
558 init_twoside_fallback();
559 init_twoside_offset_fallback();
560 init_unfilled_fallback();
561 init_offset_unfilled_fallback();
562 init_twoside_unfilled_fallback();
563 init_twoside_offset_unfilled_fallback();
568 init_twoside_offset_flat();
569 init_unfilled_flat();
570 init_offset_unfilled_flat();
571 init_twoside_unfilled_flat();
572 init_twoside_offset_unfilled_flat();
573 init_fallback_flat();
574 init_offset_fallback_flat();
575 init_twoside_fallback_flat();
576 init_twoside_offset_fallback_flat();
577 init_unfilled_fallback_flat();
578 init_offset_unfilled_fallback_flat();
579 init_twoside_unfilled_fallback_flat();
580 init_twoside_offset_unfilled_fallback_flat();
583 /**********************************************************************/
584 /* Render whole begin/end objects */
585 /**********************************************************************/
588 #define VERT(x) (mgaVertex *)(vertptr + ((x)<<vertshift))
589 #define RENDER_POINTS( start, count ) \
590 for ( ; start < count ; start++) \
591 mga_draw_point( mmesa, VERT(ELT(start)) );
592 #define RENDER_LINE( v0, v1 ) \
593 mga_draw_line( mmesa, VERT(v0), VERT(v1) )
594 #define RENDER_TRI( v0, v1, v2 ) \
595 mga_draw_triangle( mmesa, VERT(v0), VERT(v1), VERT(v2) )
596 #define RENDER_QUAD( v0, v1, v2, v3 ) \
597 mga_draw_quad( mmesa, VERT(v0), VERT(v1), VERT(v2), VERT(v3) )
598 #define INIT(x) mgaRenderPrimitive( ctx, x )
601 mgaContextPtr mmesa = MGA_CONTEXT(ctx); \
602 GLubyte *vertptr = (GLubyte *)mmesa->verts; \
603 const GLuint vertshift = mmesa->vertex_stride_shift; \
604 const GLuint * const elt = TNL_CONTEXT(ctx)->vb.Elts; \
606 #define RESET_STIPPLE
607 #define RESET_OCCLUSION
608 #define PRESERVE_VB_DEFS
610 #define TAG(x) mga_##x##_verts
611 #include "tnl/t_vb_rendertmp.h"
614 #define TAG(x) mga_##x##_elts
615 #define ELT(x) elt[x]
616 #include "tnl/t_vb_rendertmp.h"
619 /**********************************************************************/
620 /* Render clipped primitives */
621 /**********************************************************************/
625 static void mgaRenderClippedPoly( GLcontext
*ctx
, const GLuint
*elts
, GLuint n
)
627 mgaContextPtr mmesa
= MGA_CONTEXT(ctx
);
628 TNLcontext
*tnl
= TNL_CONTEXT(ctx
);
629 struct vertex_buffer
*VB
= &tnl
->vb
;
630 GLuint prim
= mmesa
->render_primitive
;
632 /* Render the new vertices as an unclipped polygon.
635 GLuint
*tmp
= VB
->Elts
;
636 VB
->Elts
= (GLuint
*)elts
;
637 tnl
->Driver
.Render
.PrimTabElts
[GL_POLYGON
]( ctx
, 0, n
, PRIM_BEGIN
|PRIM_END
);
641 /* Restore the render primitive
643 if (prim
!= GL_POLYGON
)
644 tnl
->Driver
.Render
.PrimitiveNotify( ctx
, prim
);
647 static void mgaRenderClippedLine( GLcontext
*ctx
, GLuint ii
, GLuint jj
)
649 TNLcontext
*tnl
= TNL_CONTEXT(ctx
);
650 tnl
->Driver
.Render
.Line( ctx
, ii
, jj
);
653 static void mgaFastRenderClippedPoly( GLcontext
*ctx
, const GLuint
*elts
,
656 mgaContextPtr mmesa
= MGA_CONTEXT( ctx
);
657 GLuint vertex_size
= mmesa
->vertex_size
;
658 GLuint
*vb
= mgaAllocDmaLow( mmesa
, (n
-2) * 3 * 4 * vertex_size
);
659 GLubyte
*vertptr
= (GLubyte
*)mmesa
->verts
;
660 const GLuint vertshift
= mmesa
->vertex_stride_shift
;
661 const GLuint
*start
= (const GLuint
*)VERT(elts
[0]);
664 for (i
= 2 ; i
< n
; i
++) {
665 EMIT_VERT( j
, vb
, vertex_size
, (mgaVertexPtr
) VERT(elts
[i
-1]) );
666 EMIT_VERT( j
, vb
, vertex_size
, (mgaVertexPtr
) VERT(elts
[i
]) );
667 EMIT_VERT( j
, vb
, vertex_size
, (mgaVertexPtr
) start
);
671 /**********************************************************************/
672 /* Choose render functions */
673 /**********************************************************************/
676 #define POINT_FALLBACK (DD_POINT_SMOOTH)
677 #define LINE_FALLBACK (DD_LINE_SMOOTH | DD_LINE_STIPPLE)
678 #define TRI_FALLBACK (DD_TRI_SMOOTH | DD_TRI_UNFILLED)
679 #define ANY_FALLBACK_FLAGS (POINT_FALLBACK|LINE_FALLBACK|TRI_FALLBACK| \
681 #define ANY_RASTER_FLAGS (DD_FLATSHADE|DD_TRI_LIGHT_TWOSIDE|DD_TRI_OFFSET| \
684 void mgaChooseRenderState(GLcontext
*ctx
)
686 TNLcontext
*tnl
= TNL_CONTEXT(ctx
);
687 mgaContextPtr mmesa
= MGA_CONTEXT(ctx
);
688 GLuint flags
= ctx
->_TriangleCaps
;
691 if (flags
& (ANY_FALLBACK_FLAGS
|ANY_RASTER_FLAGS
)) {
692 if (flags
& ANY_RASTER_FLAGS
) {
693 if (flags
& DD_TRI_LIGHT_TWOSIDE
) index
|= MGA_TWOSIDE_BIT
;
694 if (flags
& DD_TRI_OFFSET
) index
|= MGA_OFFSET_BIT
;
695 if (flags
& DD_TRI_UNFILLED
) index
|= MGA_UNFILLED_BIT
;
696 if (flags
& DD_FLATSHADE
) index
|= MGA_FLAT_BIT
;
699 mmesa
->draw_point
= mga_draw_point
;
700 mmesa
->draw_line
= mga_draw_line
;
701 mmesa
->draw_tri
= mga_draw_triangle
;
703 /* Hook in fallbacks for specific primitives.
705 if (flags
& ANY_FALLBACK_FLAGS
)
707 if (flags
& POINT_FALLBACK
)
708 mmesa
->draw_point
= mga_fallback_point
;
710 if (flags
& LINE_FALLBACK
)
711 mmesa
->draw_line
= mga_fallback_line
;
713 if (flags
& TRI_FALLBACK
)
714 mmesa
->draw_tri
= mga_fallback_tri
;
716 if ((flags
& DD_TRI_STIPPLE
) && !mmesa
->haveHwStipple
)
717 mmesa
->draw_tri
= mga_fallback_tri
;
719 index
|= MGA_FALLBACK_BIT
;
723 if (mmesa
->RenderIndex
!= index
) {
724 mmesa
->RenderIndex
= index
;
726 tnl
->Driver
.Render
.Points
= rast_tab
[index
].points
;
727 tnl
->Driver
.Render
.Line
= rast_tab
[index
].line
;
728 tnl
->Driver
.Render
.Triangle
= rast_tab
[index
].triangle
;
729 tnl
->Driver
.Render
.Quad
= rast_tab
[index
].quad
;
732 tnl
->Driver
.Render
.PrimTabVerts
= mga_render_tab_verts
;
733 tnl
->Driver
.Render
.PrimTabElts
= mga_render_tab_elts
;
734 tnl
->Driver
.Render
.ClippedLine
= line
; /* from tritmp.h */
735 tnl
->Driver
.Render
.ClippedPolygon
= mgaFastRenderClippedPoly
;
737 tnl
->Driver
.Render
.PrimTabVerts
= _tnl_render_tab_verts
;
738 tnl
->Driver
.Render
.PrimTabElts
= _tnl_render_tab_elts
;
739 tnl
->Driver
.Render
.ClippedLine
= mgaRenderClippedLine
;
740 tnl
->Driver
.Render
.ClippedPolygon
= mgaRenderClippedPoly
;
745 /**********************************************************************/
746 /* Runtime render state and callbacks */
747 /**********************************************************************/
750 static GLenum reduced_prim
[GL_POLYGON
+1] = {
765 /* Always called between RenderStart and RenderFinish --> We already
768 void mgaRasterPrimitive( GLcontext
*ctx
, GLenum prim
, GLuint hwprim
)
770 mgaContextPtr mmesa
= MGA_CONTEXT( ctx
);
772 FLUSH_BATCH( mmesa
);
775 if (mmesa
->raster_primitive
!= prim
)
776 mmesa
->dirty
|= MGA_UPLOAD_CONTEXT
;
778 mmesa
->raster_primitive
= prim
;
779 /* mmesa->hw_primitive = hwprim; */
780 mmesa
->hw_primitive
= MGA_WA_TRIANGLES
; /* disable mgarender.c for now */
782 if (ctx
->Polygon
.StippleFlag
&& mmesa
->haveHwStipple
)
784 mmesa
->dirty
|= MGA_UPLOAD_CONTEXT
;
785 if (mmesa
->raster_primitive
== GL_TRIANGLES
)
786 mmesa
->setup
.dwgctl
|= mmesa
->poly_stipple
;
788 mmesa
->setup
.dwgctl
&= ~(0xf<<20);
794 /* Determine the rasterized primitive when not drawing unfilled
797 * Used only for the default render stage which always decomposes
798 * primitives to trianges/lines/points. For the accelerated stage,
799 * which renders strips as strips, the equivalent calculations are
800 * performed in mgarender.c.
802 static void mgaRenderPrimitive( GLcontext
*ctx
, GLenum prim
)
804 mgaContextPtr mmesa
= MGA_CONTEXT(ctx
);
805 GLuint rprim
= reduced_prim
[prim
];
807 mmesa
->render_primitive
= prim
;
809 if (rprim
== GL_TRIANGLES
&& (ctx
->_TriangleCaps
& DD_TRI_UNFILLED
))
812 if (mmesa
->raster_primitive
!= rprim
) {
813 mgaRasterPrimitive( ctx
, rprim
, MGA_WA_TRIANGLES
);
817 static void mgaRenderFinish( GLcontext
*ctx
)
819 if (MGA_CONTEXT(ctx
)->RenderIndex
& MGA_FALLBACK_BIT
)
820 _swrast_flush( ctx
);
825 /**********************************************************************/
826 /* Manage total rasterization fallbacks */
827 /**********************************************************************/
829 static const char * const fallbackStrings
[] = {
831 "glDrawBuffer(GL_FRONT_AND_BACK)",
833 "LogicOp != GL_COPY",
834 "glRenderMode(selection or feedback)",
835 "No hardware stencil",
836 "glDepthFunc( GL_NEVER )",
837 "Mixing GL_CLAMP_TO_EDGE and GL_CLAMP"
840 static const char *getFallbackString(GLuint bit
)
847 return fallbackStrings
[i
];
851 void mgaFallback( GLcontext
*ctx
, GLuint bit
, GLboolean mode
)
853 TNLcontext
*tnl
= TNL_CONTEXT(ctx
);
854 mgaContextPtr mmesa
= MGA_CONTEXT(ctx
);
855 GLuint oldfallback
= mmesa
->Fallback
;
858 mmesa
->Fallback
|= bit
;
859 if (oldfallback
== 0) {
861 _swsetup_Wakeup( ctx
);
862 mmesa
->RenderIndex
= ~0;
863 if (MGA_DEBUG
& DEBUG_VERBOSE_FALLBACK
) {
864 fprintf(stderr
, "MGA begin rasterization fallback: 0x%x %s\n",
865 bit
, getFallbackString(bit
));
870 mmesa
->Fallback
&= ~bit
;
871 if (oldfallback
== bit
) {
872 _swrast_flush( ctx
);
873 tnl
->Driver
.Render
.Start
= mgaCheckTexSizes
;
874 tnl
->Driver
.Render
.PrimitiveNotify
= mgaRenderPrimitive
;
875 tnl
->Driver
.Render
.Finish
= mgaRenderFinish
;
876 tnl
->Driver
.Render
.BuildVertices
= mgaBuildVertices
;
877 mmesa
->NewGLState
|= (_MGA_NEW_RENDERSTATE
|
878 _MGA_NEW_RASTERSETUP
);
879 if (MGA_DEBUG
& DEBUG_VERBOSE_FALLBACK
) {
880 fprintf(stderr
, "MGA end rasterization fallback: 0x%x %s\n",
881 bit
, getFallbackString(bit
));
888 void mgaDDInitTriFuncs( GLcontext
*ctx
)
890 TNLcontext
*tnl
= TNL_CONTEXT(ctx
);
891 mgaContextPtr mmesa
= MGA_CONTEXT(ctx
);
892 static int firsttime
= 1;
898 mmesa
->RenderIndex
= ~0;
900 tnl
->Driver
.Render
.Start
= mgaCheckTexSizes
;
901 tnl
->Driver
.Render
.Finish
= mgaRenderFinish
;
902 tnl
->Driver
.Render
.PrimitiveNotify
= mgaRenderPrimitive
;
903 tnl
->Driver
.Render
.ResetLineStipple
= _swrast_ResetLineStipple
;
904 tnl
->Driver
.Render
.BuildVertices
= mgaBuildVertices
;
905 tnl
->Driver
.Render
.Multipass
= NULL
;