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>
28 #include "main/mtypes.h"
29 #include "main/macros.h"
30 #include "main/colormac.h"
32 #include "swrast/swrast.h"
33 #include "swrast_setup/swrast_setup.h"
34 #include "tnl/t_context.h"
35 #include "tnl/t_pipeline.h"
37 #include "mgacontext.h"
43 static void mgaRenderPrimitive( GLcontext
*ctx
, GLenum prim
);
45 /***********************************************************************
46 * Functions to draw basic primitives *
47 ***********************************************************************/
50 #if defined (USE_X86_ASM)
51 #define EMIT_VERT( j, vb, vertex_size, v ) \
53 __asm__ __volatile__( "rep ; movsl" \
54 : "=%c" (j), "=D" (vb), "=S" (__tmp) \
55 : "0" (vertex_size), \
60 #define EMIT_VERT( j, vb, vertex_size, v ) \
62 for ( j = 0 ; j < vertex_size ; j++ ) \
68 static void INLINE
mga_draw_triangle( mgaContextPtr mmesa
,
73 GLuint vertex_size
= mmesa
->vertex_size
;
74 GLuint
*vb
= mgaAllocDmaLow( mmesa
, 3 * 4 * vertex_size
);
77 EMIT_VERT( j
, vb
, vertex_size
, v0
);
78 EMIT_VERT( j
, vb
, vertex_size
, v1
);
79 EMIT_VERT( j
, vb
, vertex_size
, v2
);
83 static void INLINE
mga_draw_quad( mgaContextPtr mmesa
,
89 GLuint vertex_size
= mmesa
->vertex_size
;
90 GLuint
*vb
= mgaAllocDmaLow( mmesa
, 6 * 4 * vertex_size
);
93 EMIT_VERT( j
, vb
, vertex_size
, v0
);
94 EMIT_VERT( j
, vb
, vertex_size
, v1
);
95 EMIT_VERT( j
, vb
, vertex_size
, v3
);
96 EMIT_VERT( j
, vb
, vertex_size
, v1
);
97 EMIT_VERT( j
, vb
, vertex_size
, v2
);
98 EMIT_VERT( j
, vb
, vertex_size
, v3
);
102 static INLINE
void mga_draw_point( mgaContextPtr mmesa
,
105 const GLfloat sz
= 0.5 * CLAMP(mmesa
->glCtx
->Point
.Size
,
106 mmesa
->glCtx
->Const
.MinPointSize
,
107 mmesa
->glCtx
->Const
.MaxPointSize
);
108 const 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 const GLfloat width
= CLAMP(mmesa
->glCtx
->Line
.Width
,
169 mmesa
->glCtx
->Const
.MinLineWidth
,
170 mmesa
->glCtx
->Const
.MaxLineWidth
);
174 v0
->v
.x
+= LINE_X_OFFSET
- TRI_X_OFFSET
;
175 v0
->v
.y
+= LINE_Y_OFFSET
- TRI_Y_OFFSET
;
176 v1
->v
.x
+= LINE_X_OFFSET
- TRI_X_OFFSET
;
177 v1
->v
.y
+= LINE_Y_OFFSET
- TRI_Y_OFFSET
;
180 dx
= v0
->v
.x
- v1
->v
.x
;
181 dy
= v0
->v
.y
- v1
->v
.y
;
183 ix
= width
* .5; iy
= 0;
184 if (dx
* dx
> dy
* dy
) {
188 *(float *)&vb
[0] = v0
->v
.x
- ix
;
189 *(float *)&vb
[1] = v0
->v
.y
- iy
;
190 for (j
= 2 ; j
< vertex_size
; j
++)
194 *(float *)&vb
[0] = v1
->v
.x
+ ix
;
195 *(float *)&vb
[1] = v1
->v
.y
+ iy
;
196 for (j
= 2 ; j
< vertex_size
; j
++)
200 *(float *)&vb
[0] = v0
->v
.x
+ ix
;
201 *(float *)&vb
[1] = v0
->v
.y
+ iy
;
202 for (j
= 2 ; j
< vertex_size
; j
++)
206 *(float *)&vb
[0] = v0
->v
.x
- ix
;
207 *(float *)&vb
[1] = v0
->v
.y
- iy
;
208 for (j
= 2 ; j
< vertex_size
; j
++)
212 *(float *)&vb
[0] = v1
->v
.x
- ix
;
213 *(float *)&vb
[1] = v1
->v
.y
- iy
;
214 for (j
= 2 ; j
< vertex_size
; j
++)
218 *(float *)&vb
[0] = v1
->v
.x
+ ix
;
219 *(float *)&vb
[1] = v1
->v
.y
+ iy
;
220 for (j
= 2 ; j
< vertex_size
; j
++)
225 v0
->v
.x
-= LINE_X_OFFSET
- TRI_X_OFFSET
;
226 v0
->v
.y
-= LINE_Y_OFFSET
- TRI_Y_OFFSET
;
227 v1
->v
.x
-= LINE_X_OFFSET
- TRI_X_OFFSET
;
228 v1
->v
.y
-= LINE_Y_OFFSET
- TRI_Y_OFFSET
;
232 /***********************************************************************
233 * Macros for t_dd_tritmp.h to draw basic primitives *
234 ***********************************************************************/
236 #define TRI( a, b, c ) \
239 mmesa->draw_tri( mmesa, a, b, c ); \
241 mga_draw_triangle( mmesa, a, b, c ); \
244 #define QUAD( a, b, c, d ) \
247 mmesa->draw_tri( mmesa, a, b, d ); \
248 mmesa->draw_tri( mmesa, b, c, d ); \
250 mga_draw_quad( mmesa, a, b, c, d ); \
254 #define LINE( v0, v1 ) \
257 mmesa->draw_line( mmesa, v0, v1 ); \
259 mga_draw_line( mmesa, v0, v1 ); \
263 #define POINT( v0 ) \
266 mmesa->draw_point( mmesa, v0 ); \
268 mga_draw_point( mmesa, v0 ); \
273 /***********************************************************************
274 * Fallback to swrast for basic primitives *
275 ***********************************************************************/
277 /* This code is hit only when a mix of accelerated and unaccelerated
278 * primitives are being drawn, and only for the unaccelerated
283 mga_fallback_tri( mgaContextPtr mmesa
,
288 GLcontext
*ctx
= mmesa
->glCtx
;
290 mga_translate_vertex( ctx
, v0
, &v
[0] );
291 mga_translate_vertex( ctx
, v1
, &v
[1] );
292 mga_translate_vertex( ctx
, v2
, &v
[2] );
293 _swrast_Triangle( ctx
, &v
[0], &v
[1], &v
[2] );
298 mga_fallback_line( mgaContextPtr mmesa
,
302 GLcontext
*ctx
= mmesa
->glCtx
;
304 mga_translate_vertex( ctx
, v0
, &v
[0] );
305 mga_translate_vertex( ctx
, v1
, &v
[1] );
306 _swrast_Line( ctx
, &v
[0], &v
[1] );
311 mga_fallback_point( mgaContextPtr mmesa
,
314 GLcontext
*ctx
= mmesa
->glCtx
;
316 mga_translate_vertex( ctx
, v0
, &v
[0] );
317 _swrast_Point( ctx
, &v
[0] );
320 /***********************************************************************
321 * Build render functions from dd templates *
322 ***********************************************************************/
325 #define MGA_UNFILLED_BIT 0x1
326 #define MGA_OFFSET_BIT 0x2
327 #define MGA_TWOSIDE_BIT 0x4
328 #define MGA_FLAT_BIT 0x8 /* mga can't flatshade? */
329 #define MGA_FALLBACK_BIT 0x10
330 #define MGA_MAX_TRIFUNC 0x20
333 tnl_points_func points
;
335 tnl_triangle_func triangle
;
337 } rast_tab
[MGA_MAX_TRIFUNC
];
339 #define DO_FALLBACK (IND & MGA_FALLBACK_BIT)
340 #define DO_OFFSET (IND & MGA_OFFSET_BIT)
341 #define DO_UNFILLED (IND & MGA_UNFILLED_BIT)
342 #define DO_TWOSIDE (IND & MGA_TWOSIDE_BIT)
343 #define DO_FLAT (IND & MGA_FLAT_BIT)
348 #define DO_FULL_QUAD 1
351 #define HAVE_BACK_COLORS 0
353 #define HAVE_HW_FLATSHADE 0
354 #define VERTEX mgaVertex
358 #define DEPTH_SCALE mmesa->depth_scale
359 #define UNFILLED_TRI unfilled_tri
360 #define UNFILLED_QUAD unfilled_quad
361 #define VERT_X(_v) _v->v.x
362 #define VERT_Y(_v) _v->v.y
363 #define VERT_Z(_v) _v->v.z
364 #define AREA_IS_CCW( a ) (a > 0)
365 #define GET_VERTEX(e) (mmesa->verts + (e * mmesa->vertex_size * sizeof(int)))
367 #define VERT_SET_RGBA( v, c ) \
369 mga_color_t *color = (mga_color_t *)&((v)->ui[4]); \
370 UNCLAMPED_FLOAT_TO_UBYTE(color->red, (c)[0]); \
371 UNCLAMPED_FLOAT_TO_UBYTE(color->green, (c)[1]); \
372 UNCLAMPED_FLOAT_TO_UBYTE(color->blue, (c)[2]); \
373 UNCLAMPED_FLOAT_TO_UBYTE(color->alpha, (c)[3]); \
376 #define VERT_COPY_RGBA( v0, v1 ) v0->ui[4] = v1->ui[4]
378 #define VERT_SET_SPEC( v0, c ) \
380 UNCLAMPED_FLOAT_TO_UBYTE(v0->v.specular.red, (c)[0]); \
381 UNCLAMPED_FLOAT_TO_UBYTE(v0->v.specular.green, (c)[1]); \
382 UNCLAMPED_FLOAT_TO_UBYTE(v0->v.specular.blue, (c)[2]); \
385 #define VERT_COPY_SPEC( v0, v1 ) \
387 v0->v.specular.red = v1->v.specular.red; \
388 v0->v.specular.green = v1->v.specular.green; \
389 v0->v.specular.blue = v1->v.specular.blue; \
392 #define VERT_SAVE_RGBA( idx ) color[idx] = v[idx]->ui[4]
393 #define VERT_RESTORE_RGBA( idx ) v[idx]->ui[4] = color[idx]
394 #define VERT_SAVE_SPEC( idx ) spec[idx] = v[idx]->ui[5]
395 #define VERT_RESTORE_SPEC( idx ) v[idx]->ui[5] = spec[idx]
397 #define LOCAL_VARS(n) \
398 mgaContextPtr mmesa = MGA_CONTEXT(ctx); \
399 GLuint color[n] = { 0 }; \
400 GLuint spec[n] = { 0 }; \
401 (void) color; (void) spec;
405 /***********************************************************************
406 * Functions to draw basic unfilled primitives *
407 ***********************************************************************/
409 #define RASTERIZE(x) if (mmesa->raster_primitive != x) \
410 mgaRasterPrimitive( ctx, x, MGA_WA_TRIANGLES )
411 #define RENDER_PRIMITIVE mmesa->render_primitive
412 #define IND MGA_FALLBACK_BIT
414 #include "tnl_dd/t_dd_unfilled.h"
417 /***********************************************************************
418 * Functions to draw GL primitives *
419 ***********************************************************************/
423 #include "tnl_dd/t_dd_tritmp.h"
425 #define IND (MGA_OFFSET_BIT)
426 #define TAG(x) x##_offset
427 #include "tnl_dd/t_dd_tritmp.h"
429 #define IND (MGA_TWOSIDE_BIT)
430 #define TAG(x) x##_twoside
431 #include "tnl_dd/t_dd_tritmp.h"
433 #define IND (MGA_TWOSIDE_BIT|MGA_OFFSET_BIT)
434 #define TAG(x) x##_twoside_offset
435 #include "tnl_dd/t_dd_tritmp.h"
437 #define IND (MGA_UNFILLED_BIT)
438 #define TAG(x) x##_unfilled
439 #include "tnl_dd/t_dd_tritmp.h"
441 #define IND (MGA_OFFSET_BIT|MGA_UNFILLED_BIT)
442 #define TAG(x) x##_offset_unfilled
443 #include "tnl_dd/t_dd_tritmp.h"
445 #define IND (MGA_TWOSIDE_BIT|MGA_UNFILLED_BIT)
446 #define TAG(x) x##_twoside_unfilled
447 #include "tnl_dd/t_dd_tritmp.h"
449 #define IND (MGA_TWOSIDE_BIT|MGA_OFFSET_BIT|MGA_UNFILLED_BIT)
450 #define TAG(x) x##_twoside_offset_unfilled
451 #include "tnl_dd/t_dd_tritmp.h"
453 #define IND (MGA_FALLBACK_BIT)
454 #define TAG(x) x##_fallback
455 #include "tnl_dd/t_dd_tritmp.h"
457 #define IND (MGA_OFFSET_BIT|MGA_FALLBACK_BIT)
458 #define TAG(x) x##_offset_fallback
459 #include "tnl_dd/t_dd_tritmp.h"
461 #define IND (MGA_TWOSIDE_BIT|MGA_FALLBACK_BIT)
462 #define TAG(x) x##_twoside_fallback
463 #include "tnl_dd/t_dd_tritmp.h"
465 #define IND (MGA_TWOSIDE_BIT|MGA_OFFSET_BIT|MGA_FALLBACK_BIT)
466 #define TAG(x) x##_twoside_offset_fallback
467 #include "tnl_dd/t_dd_tritmp.h"
469 #define IND (MGA_UNFILLED_BIT|MGA_FALLBACK_BIT)
470 #define TAG(x) x##_unfilled_fallback
471 #include "tnl_dd/t_dd_tritmp.h"
473 #define IND (MGA_OFFSET_BIT|MGA_UNFILLED_BIT|MGA_FALLBACK_BIT)
474 #define TAG(x) x##_offset_unfilled_fallback
475 #include "tnl_dd/t_dd_tritmp.h"
477 #define IND (MGA_TWOSIDE_BIT|MGA_UNFILLED_BIT|MGA_FALLBACK_BIT)
478 #define TAG(x) x##_twoside_unfilled_fallback
479 #include "tnl_dd/t_dd_tritmp.h"
481 #define IND (MGA_TWOSIDE_BIT|MGA_OFFSET_BIT|MGA_UNFILLED_BIT| \
483 #define TAG(x) x##_twoside_offset_unfilled_fallback
484 #include "tnl_dd/t_dd_tritmp.h"
487 /* Mga doesn't support provoking-vertex flat-shading?
489 #define IND (MGA_FLAT_BIT)
490 #define TAG(x) x##_flat
491 #include "tnl_dd/t_dd_tritmp.h"
493 #define IND (MGA_OFFSET_BIT|MGA_FLAT_BIT)
494 #define TAG(x) x##_offset_flat
495 #include "tnl_dd/t_dd_tritmp.h"
497 #define IND (MGA_TWOSIDE_BIT|MGA_FLAT_BIT)
498 #define TAG(x) x##_twoside_flat
499 #include "tnl_dd/t_dd_tritmp.h"
501 #define IND (MGA_TWOSIDE_BIT|MGA_OFFSET_BIT|MGA_FLAT_BIT)
502 #define TAG(x) x##_twoside_offset_flat
503 #include "tnl_dd/t_dd_tritmp.h"
505 #define IND (MGA_UNFILLED_BIT|MGA_FLAT_BIT)
506 #define TAG(x) x##_unfilled_flat
507 #include "tnl_dd/t_dd_tritmp.h"
509 #define IND (MGA_OFFSET_BIT|MGA_UNFILLED_BIT|MGA_FLAT_BIT)
510 #define TAG(x) x##_offset_unfilled_flat
511 #include "tnl_dd/t_dd_tritmp.h"
513 #define IND (MGA_TWOSIDE_BIT|MGA_UNFILLED_BIT|MGA_FLAT_BIT)
514 #define TAG(x) x##_twoside_unfilled_flat
515 #include "tnl_dd/t_dd_tritmp.h"
517 #define IND (MGA_TWOSIDE_BIT|MGA_OFFSET_BIT|MGA_UNFILLED_BIT|MGA_FLAT_BIT)
518 #define TAG(x) x##_twoside_offset_unfilled_flat
519 #include "tnl_dd/t_dd_tritmp.h"
521 #define IND (MGA_FALLBACK_BIT|MGA_FLAT_BIT)
522 #define TAG(x) x##_fallback_flat
523 #include "tnl_dd/t_dd_tritmp.h"
525 #define IND (MGA_OFFSET_BIT|MGA_FALLBACK_BIT|MGA_FLAT_BIT)
526 #define TAG(x) x##_offset_fallback_flat
527 #include "tnl_dd/t_dd_tritmp.h"
529 #define IND (MGA_TWOSIDE_BIT|MGA_FALLBACK_BIT|MGA_FLAT_BIT)
530 #define TAG(x) x##_twoside_fallback_flat
531 #include "tnl_dd/t_dd_tritmp.h"
533 #define IND (MGA_TWOSIDE_BIT|MGA_OFFSET_BIT|MGA_FALLBACK_BIT|MGA_FLAT_BIT)
534 #define TAG(x) x##_twoside_offset_fallback_flat
535 #include "tnl_dd/t_dd_tritmp.h"
537 #define IND (MGA_UNFILLED_BIT|MGA_FALLBACK_BIT|MGA_FLAT_BIT)
538 #define TAG(x) x##_unfilled_fallback_flat
539 #include "tnl_dd/t_dd_tritmp.h"
541 #define IND (MGA_OFFSET_BIT|MGA_UNFILLED_BIT|MGA_FALLBACK_BIT|MGA_FLAT_BIT)
542 #define TAG(x) x##_offset_unfilled_fallback_flat
543 #include "tnl_dd/t_dd_tritmp.h"
545 #define IND (MGA_TWOSIDE_BIT|MGA_UNFILLED_BIT|MGA_FALLBACK_BIT|MGA_FLAT_BIT)
546 #define TAG(x) x##_twoside_unfilled_fallback_flat
547 #include "tnl_dd/t_dd_tritmp.h"
549 #define IND (MGA_TWOSIDE_BIT|MGA_OFFSET_BIT|MGA_UNFILLED_BIT| \
550 MGA_FALLBACK_BIT|MGA_FLAT_BIT)
551 #define TAG(x) x##_twoside_offset_unfilled_fallback_flat
552 #include "tnl_dd/t_dd_tritmp.h"
555 static void init_rast_tab( void )
560 init_twoside_offset();
562 init_offset_unfilled();
563 init_twoside_unfilled();
564 init_twoside_offset_unfilled();
566 init_offset_fallback();
567 init_twoside_fallback();
568 init_twoside_offset_fallback();
569 init_unfilled_fallback();
570 init_offset_unfilled_fallback();
571 init_twoside_unfilled_fallback();
572 init_twoside_offset_unfilled_fallback();
577 init_twoside_offset_flat();
578 init_unfilled_flat();
579 init_offset_unfilled_flat();
580 init_twoside_unfilled_flat();
581 init_twoside_offset_unfilled_flat();
582 init_fallback_flat();
583 init_offset_fallback_flat();
584 init_twoside_fallback_flat();
585 init_twoside_offset_fallback_flat();
586 init_unfilled_fallback_flat();
587 init_offset_unfilled_fallback_flat();
588 init_twoside_unfilled_fallback_flat();
589 init_twoside_offset_unfilled_fallback_flat();
592 /**********************************************************************/
593 /* Render whole begin/end objects */
594 /**********************************************************************/
597 #define VERT(x) (mgaVertex *)(vertptr + ((x)*vertex_size*sizeof(int)))
598 #define RENDER_POINTS( start, count ) \
599 for ( ; start < count ; start++) \
600 mga_draw_point( mmesa, VERT(ELT(start)) );
601 #define RENDER_LINE( v0, v1 ) \
602 mga_draw_line( mmesa, VERT(v0), VERT(v1) )
603 #define RENDER_TRI( v0, v1, v2 ) \
604 mga_draw_triangle( mmesa, VERT(v0), VERT(v1), VERT(v2) )
605 #define RENDER_QUAD( v0, v1, v2, v3 ) \
606 mga_draw_quad( mmesa, VERT(v0), VERT(v1), VERT(v2), VERT(v3) )
607 #define INIT(x) mgaRenderPrimitive( ctx, x )
610 mgaContextPtr mmesa = MGA_CONTEXT(ctx); \
611 GLubyte *vertptr = (GLubyte *)mmesa->verts; \
612 const GLuint vertex_size = mmesa->vertex_size; \
613 const GLuint * const elt = TNL_CONTEXT(ctx)->vb.Elts; \
615 #define RESET_STIPPLE
616 #define RESET_OCCLUSION
617 #define PRESERVE_VB_DEFS
619 #define TAG(x) mga_##x##_verts
620 #include "tnl/t_vb_rendertmp.h"
623 #define TAG(x) mga_##x##_elts
624 #define ELT(x) elt[x]
625 #include "tnl/t_vb_rendertmp.h"
628 /**********************************************************************/
629 /* Render clipped primitives */
630 /**********************************************************************/
634 static void mgaRenderClippedPoly( GLcontext
*ctx
, const GLuint
*elts
, GLuint n
)
636 mgaContextPtr mmesa
= MGA_CONTEXT(ctx
);
637 TNLcontext
*tnl
= TNL_CONTEXT(ctx
);
638 struct vertex_buffer
*VB
= &tnl
->vb
;
639 GLuint prim
= mmesa
->render_primitive
;
641 /* Render the new vertices as an unclipped polygon.
644 GLuint
*tmp
= VB
->Elts
;
645 VB
->Elts
= (GLuint
*)elts
;
646 tnl
->Driver
.Render
.PrimTabElts
[GL_POLYGON
]( ctx
, 0, n
, PRIM_BEGIN
|PRIM_END
);
650 /* Restore the render primitive
652 if (prim
!= GL_POLYGON
)
653 tnl
->Driver
.Render
.PrimitiveNotify( ctx
, prim
);
656 static void mgaRenderClippedLine( GLcontext
*ctx
, GLuint ii
, GLuint jj
)
658 TNLcontext
*tnl
= TNL_CONTEXT(ctx
);
659 tnl
->Driver
.Render
.Line( ctx
, ii
, jj
);
662 static void mgaFastRenderClippedPoly( GLcontext
*ctx
, const GLuint
*elts
,
665 mgaContextPtr mmesa
= MGA_CONTEXT( ctx
);
666 GLuint vertex_size
= mmesa
->vertex_size
;
667 GLuint
*vb
= mgaAllocDmaLow( mmesa
, (n
-2) * 3 * 4 * vertex_size
);
668 GLubyte
*vertptr
= (GLubyte
*)mmesa
->verts
;
669 const GLuint
*start
= (const GLuint
*)VERT(elts
[0]);
672 for (i
= 2 ; i
< n
; i
++) {
673 EMIT_VERT( j
, vb
, vertex_size
, (mgaVertexPtr
) VERT(elts
[i
-1]) );
674 EMIT_VERT( j
, vb
, vertex_size
, (mgaVertexPtr
) VERT(elts
[i
]) );
675 EMIT_VERT( j
, vb
, vertex_size
, (mgaVertexPtr
) start
);
679 /**********************************************************************/
680 /* Choose render functions */
681 /**********************************************************************/
684 #define POINT_FALLBACK (DD_POINT_SMOOTH)
685 #define LINE_FALLBACK (DD_LINE_SMOOTH | DD_LINE_STIPPLE)
686 #define TRI_FALLBACK (DD_TRI_SMOOTH | DD_TRI_UNFILLED)
687 #define ANY_FALLBACK_FLAGS (POINT_FALLBACK|LINE_FALLBACK|TRI_FALLBACK)
688 #define ANY_RASTER_FLAGS (DD_FLATSHADE|DD_TRI_LIGHT_TWOSIDE|DD_TRI_OFFSET| \
691 void mgaChooseRenderState(GLcontext
*ctx
)
693 TNLcontext
*tnl
= TNL_CONTEXT(ctx
);
694 mgaContextPtr mmesa
= MGA_CONTEXT(ctx
);
695 GLuint flags
= ctx
->_TriangleCaps
;
698 if (flags
& (ANY_FALLBACK_FLAGS
|ANY_RASTER_FLAGS
|DD_TRI_STIPPLE
)) {
699 if (flags
& ANY_RASTER_FLAGS
) {
700 if (flags
& DD_TRI_LIGHT_TWOSIDE
) index
|= MGA_TWOSIDE_BIT
;
701 if (flags
& DD_TRI_OFFSET
) index
|= MGA_OFFSET_BIT
;
702 if (flags
& DD_TRI_UNFILLED
) index
|= MGA_UNFILLED_BIT
;
703 if (flags
& DD_FLATSHADE
) index
|= MGA_FLAT_BIT
;
706 mmesa
->draw_point
= mga_draw_point
;
707 mmesa
->draw_line
= mga_draw_line
;
708 mmesa
->draw_tri
= mga_draw_triangle
;
710 /* Hook in fallbacks for specific primitives.
712 if (flags
& ANY_FALLBACK_FLAGS
)
714 if (flags
& POINT_FALLBACK
)
715 mmesa
->draw_point
= mga_fallback_point
;
717 if (flags
& LINE_FALLBACK
)
718 mmesa
->draw_line
= mga_fallback_line
;
720 if (flags
& TRI_FALLBACK
)
721 mmesa
->draw_tri
= mga_fallback_tri
;
723 index
|= MGA_FALLBACK_BIT
;
726 if ((flags
& DD_TRI_STIPPLE
) && !mmesa
->haveHwStipple
) {
727 mmesa
->draw_tri
= mga_fallback_tri
;
728 index
|= MGA_FALLBACK_BIT
;
732 if (mmesa
->RenderIndex
!= index
) {
733 mmesa
->RenderIndex
= index
;
735 tnl
->Driver
.Render
.Points
= rast_tab
[index
].points
;
736 tnl
->Driver
.Render
.Line
= rast_tab
[index
].line
;
737 tnl
->Driver
.Render
.Triangle
= rast_tab
[index
].triangle
;
738 tnl
->Driver
.Render
.Quad
= rast_tab
[index
].quad
;
741 tnl
->Driver
.Render
.PrimTabVerts
= mga_render_tab_verts
;
742 tnl
->Driver
.Render
.PrimTabElts
= mga_render_tab_elts
;
743 tnl
->Driver
.Render
.ClippedLine
= line
; /* from tritmp.h */
744 tnl
->Driver
.Render
.ClippedPolygon
= mgaFastRenderClippedPoly
;
746 tnl
->Driver
.Render
.PrimTabVerts
= _tnl_render_tab_verts
;
747 tnl
->Driver
.Render
.PrimTabElts
= _tnl_render_tab_elts
;
748 tnl
->Driver
.Render
.ClippedLine
= mgaRenderClippedLine
;
749 tnl
->Driver
.Render
.ClippedPolygon
= mgaRenderClippedPoly
;
754 /**********************************************************************/
755 /* Runtime render state and callbacks */
756 /**********************************************************************/
759 static GLenum reduced_prim
[GL_POLYGON
+1] = {
774 /* Always called between RenderStart and RenderFinish --> We already
777 void mgaRasterPrimitive( GLcontext
*ctx
, GLenum prim
, GLuint hwprim
)
779 mgaContextPtr mmesa
= MGA_CONTEXT( ctx
);
781 FLUSH_BATCH( mmesa
);
784 if (mmesa
->raster_primitive
!= prim
)
785 mmesa
->dirty
|= MGA_UPLOAD_CONTEXT
;
787 mmesa
->raster_primitive
= prim
;
788 /* mmesa->hw_primitive = hwprim; */
789 mmesa
->hw_primitive
= MGA_WA_TRIANGLES
; /* disable mgarender.c for now */
791 if (ctx
->Polygon
.StippleFlag
&& mmesa
->haveHwStipple
)
793 mmesa
->dirty
|= MGA_UPLOAD_CONTEXT
;
794 mmesa
->setup
.dwgctl
&= ~(0xf<<20);
795 if (mmesa
->raster_primitive
== GL_TRIANGLES
)
796 mmesa
->setup
.dwgctl
|= mmesa
->poly_stipple
;
802 /* Determine the rasterized primitive when not drawing unfilled
805 * Used only for the default render stage which always decomposes
806 * primitives to trianges/lines/points. For the accelerated stage,
807 * which renders strips as strips, the equivalent calculations are
808 * performed in mgarender.c.
810 static void mgaRenderPrimitive( GLcontext
*ctx
, GLenum prim
)
812 mgaContextPtr mmesa
= MGA_CONTEXT(ctx
);
813 GLuint rprim
= reduced_prim
[prim
];
815 mmesa
->render_primitive
= prim
;
817 if (rprim
== GL_TRIANGLES
&& (ctx
->_TriangleCaps
& DD_TRI_UNFILLED
))
820 if (mmesa
->raster_primitive
!= rprim
) {
821 mgaRasterPrimitive( ctx
, rprim
, MGA_WA_TRIANGLES
);
825 static void mgaRenderFinish( GLcontext
*ctx
)
827 if (MGA_CONTEXT(ctx
)->RenderIndex
& MGA_FALLBACK_BIT
)
828 _swrast_flush( ctx
);
833 /**********************************************************************/
834 /* Manage total rasterization fallbacks */
835 /**********************************************************************/
837 static const char * const fallbackStrings
[] = {
839 "glDrawBuffer(GL_FRONT_AND_BACK)",
841 "glBlendFunc(GL_SRC_ALPHA_SATURATE, GL_ZERO)",
842 "glRenderMode(selection or feedback)",
843 "No hardware stencil",
844 "glDepthFunc( GL_NEVER )",
845 "Mixing GL_CLAMP_TO_EDGE and GL_CLAMP",
846 "rasterization fallback option"
849 static const char *getFallbackString(GLuint bit
)
856 return fallbackStrings
[i
];
860 void mgaFallback( GLcontext
*ctx
, GLuint bit
, GLboolean mode
)
862 TNLcontext
*tnl
= TNL_CONTEXT(ctx
);
863 mgaContextPtr mmesa
= MGA_CONTEXT(ctx
);
864 GLuint oldfallback
= mmesa
->Fallback
;
867 mmesa
->Fallback
|= bit
;
868 if (oldfallback
== 0) {
870 _swsetup_Wakeup( ctx
);
871 mmesa
->RenderIndex
= ~0;
872 if (MGA_DEBUG
& DEBUG_VERBOSE_FALLBACK
) {
873 fprintf(stderr
, "MGA begin rasterization fallback: 0x%x %s\n",
874 bit
, getFallbackString(bit
));
879 mmesa
->Fallback
&= ~bit
;
880 if (oldfallback
== bit
) {
881 _swrast_flush( ctx
);
882 tnl
->Driver
.Render
.Start
= mgaCheckTexSizes
;
883 tnl
->Driver
.Render
.PrimitiveNotify
= mgaRenderPrimitive
;
884 tnl
->Driver
.Render
.Finish
= mgaRenderFinish
;
885 tnl
->Driver
.Render
.BuildVertices
= mgaBuildVertices
;
886 mmesa
->NewGLState
|= (_MGA_NEW_RENDERSTATE
|
887 _MGA_NEW_RASTERSETUP
);
888 if (MGA_DEBUG
& DEBUG_VERBOSE_FALLBACK
) {
889 fprintf(stderr
, "MGA end rasterization fallback: 0x%x %s\n",
890 bit
, getFallbackString(bit
));
897 void mgaDDInitTriFuncs( GLcontext
*ctx
)
899 TNLcontext
*tnl
= TNL_CONTEXT(ctx
);
900 mgaContextPtr mmesa
= MGA_CONTEXT(ctx
);
901 static int firsttime
= 1;
907 mmesa
->RenderIndex
= ~0;
909 tnl
->Driver
.Render
.Start
= mgaCheckTexSizes
;
910 tnl
->Driver
.Render
.Finish
= mgaRenderFinish
;
911 tnl
->Driver
.Render
.PrimitiveNotify
= mgaRenderPrimitive
;
912 tnl
->Driver
.Render
.ResetLineStipple
= _swrast_ResetLineStipple
;
913 tnl
->Driver
.Render
.BuildVertices
= mgaBuildVertices
;
914 tnl
->Driver
.Render
.Multipass
= NULL
;