Tizen 2.0 Release
[profile/ivi/osmesa.git] / src / mesa / drivers / dri / mga / mgatris.c
1 /*
2  * Copyright 2000-2001 VA Linux Systems, Inc.
3  * All Rights Reserved.
4  *
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:
11  *
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
14  * Software.
15  *
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.
23  *
24  * Authors:
25  *    Keith Whitwell <keith@tungstengraphics.com>
26  */
27
28 #include "main/mtypes.h"
29 #include "main/macros.h"
30 #include "main/colormac.h"
31 #include "main/mm.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"
36
37 #include "mgacontext.h"
38 #include "mgaioctl.h"
39 #include "mgatris.h"
40 #include "mgavb.h"
41
42
43 static void mgaRenderPrimitive( struct gl_context *ctx, GLenum prim );
44
45 /***********************************************************************
46  *                 Functions to draw basic primitives                  *
47  ***********************************************************************/
48
49
50 #if defined (USE_X86_ASM)
51 #define EMIT_VERT( j, vb, vertex_size, v )              \
52 do {    int __tmp;                                      \
53         __asm__ __volatile__( "rep ; movsl"             \
54                          : "=%c" (j), "=D" (vb), "=S" (__tmp)           \
55                          : "0" (vertex_size),           \
56                            "D" ((long)vb),              \
57                            "S" ((long)v));              \
58 } while (0)
59 #else
60 #define EMIT_VERT( j, vb, vertex_size, v )      \
61 do {                                            \
62    for ( j = 0 ; j < vertex_size ; j++ )        \
63       vb[j] = (v)->ui[j];                       \
64    vb += vertex_size;                           \
65 } while (0)
66 #endif
67
68 static void INLINE mga_draw_triangle( mgaContextPtr mmesa,
69                                            mgaVertexPtr v0,
70                                            mgaVertexPtr v1,
71                                            mgaVertexPtr v2 )
72 {
73    GLuint vertex_size = mmesa->vertex_size;
74    GLuint *vb = mgaAllocDmaLow( mmesa, 3 * 4 * vertex_size );
75    int j;
76
77    EMIT_VERT( j, vb, vertex_size, v0 );
78    EMIT_VERT( j, vb, vertex_size, v1 );
79    EMIT_VERT( j, vb, vertex_size, v2 );
80 }
81
82
83 static void INLINE mga_draw_quad( mgaContextPtr mmesa,
84                                        mgaVertexPtr v0,
85                                        mgaVertexPtr v1,
86                                        mgaVertexPtr v2,
87                                        mgaVertexPtr v3 )
88 {
89    GLuint vertex_size = mmesa->vertex_size;
90    GLuint *vb = mgaAllocDmaLow( mmesa, 6 * 4 * vertex_size );
91    int j;
92
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 );
99 }
100
101
102 static INLINE void mga_draw_point( mgaContextPtr mmesa,
103                                         mgaVertexPtr tmp )
104 {
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 );
110    int j;
111    
112 #if 0
113    v0->v.x += PNT_X_OFFSET - TRI_X_OFFSET;
114    v0->v.y += PNT_Y_OFFSET - TRI_Y_OFFSET;
115 #endif
116
117    /* Draw a point as two triangles.
118     */
119    *(float *)&vb[0] = tmp->v.x - sz;
120    *(float *)&vb[1] = tmp->v.y - sz;
121    for (j = 2 ; j < vertex_size ; j++) 
122       vb[j] = tmp->ui[j];
123    vb += vertex_size;
124
125    *(float *)&vb[0] = tmp->v.x + sz;
126    *(float *)&vb[1] = tmp->v.y - sz;
127    for (j = 2 ; j < vertex_size ; j++) 
128       vb[j] = tmp->ui[j];
129    vb += vertex_size;
130
131    *(float *)&vb[0] = tmp->v.x + sz;
132    *(float *)&vb[1] = tmp->v.y + sz;
133    for (j = 2 ; j < vertex_size ; j++) 
134       vb[j] = tmp->ui[j];
135    vb += vertex_size;
136
137    *(float *)&vb[0] = tmp->v.x + sz;
138    *(float *)&vb[1] = tmp->v.y + sz;
139    for (j = 2 ; j < vertex_size ; j++) 
140       vb[j] = tmp->ui[j];
141    vb += vertex_size;
142
143    *(float *)&vb[0] = tmp->v.x - sz;
144    *(float *)&vb[1] = tmp->v.y + sz;
145    for (j = 2 ; j < vertex_size ; j++) 
146       vb[j] = tmp->ui[j];
147    vb += vertex_size;
148
149    *(float *)&vb[0] = tmp->v.x - sz;
150    *(float *)&vb[1] = tmp->v.y - sz;
151    for (j = 2 ; j < vertex_size ; j++) 
152       vb[j] = tmp->ui[j];
153
154 #if 0
155    v0->v.x -= PNT_X_OFFSET - TRI_X_OFFSET;
156    v0->v.y -= PNT_Y_OFFSET - TRI_Y_OFFSET;
157 #endif
158 }
159
160
161 static INLINE void mga_draw_line( mgaContextPtr mmesa,
162                                       mgaVertexPtr v0,
163                                       mgaVertexPtr v1 )
164 {
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);
171    GLint j;
172
173 #if 0
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;
178 #endif
179
180    dx = v0->v.x - v1->v.x;
181    dy = v0->v.y - v1->v.y;
182    
183    ix = width * .5; iy = 0;
184    if (dx * dx > dy * dy) {
185       iy = ix; ix = 0;
186    }
187
188    *(float *)&vb[0] = v0->v.x - ix;
189    *(float *)&vb[1] = v0->v.y - iy;
190    for (j = 2 ; j < vertex_size ; j++) 
191       vb[j] = v0->ui[j];
192    vb += vertex_size;
193
194    *(float *)&vb[0] = v1->v.x + ix;
195    *(float *)&vb[1] = v1->v.y + iy;
196    for (j = 2 ; j < vertex_size ; j++) 
197       vb[j] = v1->ui[j];
198    vb += vertex_size;
199
200    *(float *)&vb[0] = v0->v.x + ix;
201    *(float *)&vb[1] = v0->v.y + iy;
202    for (j = 2 ; j < vertex_size ; j++) 
203       vb[j] = v0->ui[j];
204    vb += vertex_size;
205          
206    *(float *)&vb[0] = v0->v.x - ix;
207    *(float *)&vb[1] = v0->v.y - iy;
208    for (j = 2 ; j < vertex_size ; j++) 
209       vb[j] = v0->ui[j];
210    vb += vertex_size;
211
212    *(float *)&vb[0] = v1->v.x - ix;
213    *(float *)&vb[1] = v1->v.y - iy;
214    for (j = 2 ; j < vertex_size ; j++) 
215       vb[j] = v1->ui[j];
216    vb += vertex_size;
217
218    *(float *)&vb[0] = v1->v.x + ix;
219    *(float *)&vb[1] = v1->v.y + iy;
220    for (j = 2 ; j < vertex_size ; j++) 
221       vb[j] = v1->ui[j];
222    vb += vertex_size;
223
224 #if 0
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;
229 #endif
230 }
231
232 /***********************************************************************
233  *          Macros for t_dd_tritmp.h to draw basic primitives          *
234  ***********************************************************************/
235
236 #define TRI( a, b, c )                          \
237 do {                                            \
238    if (DO_FALLBACK)                             \
239       mmesa->draw_tri( mmesa, a, b, c );        \
240    else                                         \
241       mga_draw_triangle( mmesa, a, b, c );      \
242 } while (0)
243
244 #define QUAD( a, b, c, d )                      \
245 do {                                            \
246    if (DO_FALLBACK) {                           \
247       mmesa->draw_tri( mmesa, a, b, d );        \
248       mmesa->draw_tri( mmesa, b, c, d );        \
249    } else {                                     \
250       mga_draw_quad( mmesa, a, b, c, d );       \
251    }                                            \
252 } while (0)
253
254 #define LINE( v0, v1 )                          \
255 do {                                            \
256    if (DO_FALLBACK)                             \
257       mmesa->draw_line( mmesa, v0, v1 );        \
258    else {                                       \
259       mga_draw_line( mmesa, v0, v1 );           \
260    }                                            \
261 } while (0)
262
263 #define POINT( v0 )                             \
264 do {                                            \
265    if (DO_FALLBACK)                             \
266       mmesa->draw_point( mmesa, v0 );           \
267    else {                                       \
268       mga_draw_point( mmesa, v0 );              \
269    }                                            \
270 } while (0)
271
272
273 /***********************************************************************
274  *              Fallback to swrast for basic primitives                *
275  ***********************************************************************/
276
277 /* This code is hit only when a mix of accelerated and unaccelerated
278  * primitives are being drawn, and only for the unaccelerated
279  * primitives.  
280  */
281
282 static void 
283 mga_fallback_tri( mgaContextPtr mmesa, 
284                    mgaVertex *v0, 
285                    mgaVertex *v1, 
286                    mgaVertex *v2 )
287 {
288    struct gl_context *ctx = mmesa->glCtx;
289    SWvertex v[3];
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] );
294 }
295
296
297 static void 
298 mga_fallback_line( mgaContextPtr mmesa,
299                     mgaVertex *v0,
300                     mgaVertex *v1 )
301 {
302    struct gl_context *ctx = mmesa->glCtx;
303    SWvertex v[2];
304    mga_translate_vertex( ctx, v0, &v[0] );
305    mga_translate_vertex( ctx, v1, &v[1] );
306    _swrast_Line( ctx, &v[0], &v[1] );
307 }
308
309
310 static void 
311 mga_fallback_point( mgaContextPtr mmesa, 
312                      mgaVertex *v0 )
313 {
314    struct gl_context *ctx = mmesa->glCtx;
315    SWvertex v[1];
316    mga_translate_vertex( ctx, v0, &v[0] );
317    _swrast_Point( ctx, &v[0] );
318 }
319
320 /***********************************************************************
321  *              Build render functions from dd templates               *
322  ***********************************************************************/
323
324
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
331
332 static struct {
333    tnl_points_func              points;
334    tnl_line_func                line;
335    tnl_triangle_func    triangle;
336    tnl_quad_func                quad;
337 } rast_tab[MGA_MAX_TRIFUNC];
338
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)
344 #define DO_TRI       1
345 #define DO_QUAD      1
346 #define DO_LINE      1
347 #define DO_POINTS    1
348 #define DO_FULL_QUAD 1
349
350 #define HAVE_BACK_COLORS  0
351 #define HAVE_SPEC         1
352 #define HAVE_HW_FLATSHADE 0
353 #define VERTEX mgaVertex
354 #define TAB rast_tab
355
356
357 #define DEPTH_SCALE mmesa->depth_scale
358 #define UNFILLED_TRI unfilled_tri
359 #define UNFILLED_QUAD unfilled_quad
360 #define VERT_X(_v) _v->v.x
361 #define VERT_Y(_v) _v->v.y
362 #define VERT_Z(_v) _v->v.z
363 #define AREA_IS_CCW( a ) (a > 0)
364 #define GET_VERTEX(e) (mmesa->verts + (e * mmesa->vertex_size * sizeof(int)))
365
366 #define VERT_SET_RGBA( v, c )                                   \
367 do {                                                            \
368    mga_color_t *color = (mga_color_t *)&((v)->ui[4]);   \
369    UNCLAMPED_FLOAT_TO_UBYTE(color->red, (c)[0]);                \
370    UNCLAMPED_FLOAT_TO_UBYTE(color->green, (c)[1]);              \
371    UNCLAMPED_FLOAT_TO_UBYTE(color->blue, (c)[2]);               \
372    UNCLAMPED_FLOAT_TO_UBYTE(color->alpha, (c)[3]);              \
373 } while (0)
374
375 #define VERT_COPY_RGBA( v0, v1 ) v0->ui[4] = v1->ui[4]
376
377 #define VERT_SET_SPEC( v0, c )                                  \
378 do {                                                            \
379    UNCLAMPED_FLOAT_TO_UBYTE(v0->v.specular.red, (c)[0]);        \
380    UNCLAMPED_FLOAT_TO_UBYTE(v0->v.specular.green, (c)[1]);      \
381    UNCLAMPED_FLOAT_TO_UBYTE(v0->v.specular.blue, (c)[2]);       \
382 } while (0)
383
384 #define VERT_COPY_SPEC( v0, v1 )                \
385 do {                                            \
386    v0->v.specular.red   = v1->v.specular.red;   \
387    v0->v.specular.green = v1->v.specular.green; \
388    v0->v.specular.blue  = v1->v.specular.blue;  \
389 } while (0)
390
391 #define VERT_SAVE_RGBA( idx )    color[idx] = v[idx]->ui[4]
392 #define VERT_RESTORE_RGBA( idx ) v[idx]->ui[4] = color[idx]
393 #define VERT_SAVE_SPEC( idx )    spec[idx] = v[idx]->ui[5]
394 #define VERT_RESTORE_SPEC( idx ) v[idx]->ui[5] = spec[idx]
395
396 #define LOCAL_VARS(n)                                   \
397    mgaContextPtr mmesa = MGA_CONTEXT(ctx);              \
398    GLuint color[n] = { 0 };                             \
399    GLuint spec[n] = { 0 };                              \
400    (void) color; (void) spec;
401
402
403
404 /***********************************************************************
405  *            Functions to draw basic unfilled primitives              *
406  ***********************************************************************/
407
408 #define RASTERIZE(x) if (mmesa->raster_primitive != x) \
409                         mgaRasterPrimitive( ctx, x, MGA_WA_TRIANGLES )
410 #define RENDER_PRIMITIVE mmesa->render_primitive
411 #define IND MGA_FALLBACK_BIT
412 #define TAG(x) x
413 #include "tnl_dd/t_dd_unfilled.h"
414 #undef IND
415
416 /***********************************************************************
417  *                 Functions to draw GL primitives                     *
418  ***********************************************************************/
419
420 #define IND (0)
421 #define TAG(x) x
422 #include "tnl_dd/t_dd_tritmp.h"
423
424 #define IND (MGA_OFFSET_BIT)
425 #define TAG(x) x##_offset
426 #include "tnl_dd/t_dd_tritmp.h"
427
428 #define IND (MGA_TWOSIDE_BIT)
429 #define TAG(x) x##_twoside
430 #include "tnl_dd/t_dd_tritmp.h"
431
432 #define IND (MGA_TWOSIDE_BIT|MGA_OFFSET_BIT)
433 #define TAG(x) x##_twoside_offset
434 #include "tnl_dd/t_dd_tritmp.h"
435
436 #define IND (MGA_UNFILLED_BIT)
437 #define TAG(x) x##_unfilled
438 #include "tnl_dd/t_dd_tritmp.h"
439
440 #define IND (MGA_OFFSET_BIT|MGA_UNFILLED_BIT)
441 #define TAG(x) x##_offset_unfilled
442 #include "tnl_dd/t_dd_tritmp.h"
443
444 #define IND (MGA_TWOSIDE_BIT|MGA_UNFILLED_BIT)
445 #define TAG(x) x##_twoside_unfilled
446 #include "tnl_dd/t_dd_tritmp.h"
447
448 #define IND (MGA_TWOSIDE_BIT|MGA_OFFSET_BIT|MGA_UNFILLED_BIT)
449 #define TAG(x) x##_twoside_offset_unfilled
450 #include "tnl_dd/t_dd_tritmp.h"
451
452 #define IND (MGA_FALLBACK_BIT)
453 #define TAG(x) x##_fallback
454 #include "tnl_dd/t_dd_tritmp.h"
455
456 #define IND (MGA_OFFSET_BIT|MGA_FALLBACK_BIT)
457 #define TAG(x) x##_offset_fallback
458 #include "tnl_dd/t_dd_tritmp.h"
459
460 #define IND (MGA_TWOSIDE_BIT|MGA_FALLBACK_BIT)
461 #define TAG(x) x##_twoside_fallback
462 #include "tnl_dd/t_dd_tritmp.h"
463
464 #define IND (MGA_TWOSIDE_BIT|MGA_OFFSET_BIT|MGA_FALLBACK_BIT)
465 #define TAG(x) x##_twoside_offset_fallback
466 #include "tnl_dd/t_dd_tritmp.h"
467
468 #define IND (MGA_UNFILLED_BIT|MGA_FALLBACK_BIT)
469 #define TAG(x) x##_unfilled_fallback
470 #include "tnl_dd/t_dd_tritmp.h"
471
472 #define IND (MGA_OFFSET_BIT|MGA_UNFILLED_BIT|MGA_FALLBACK_BIT)
473 #define TAG(x) x##_offset_unfilled_fallback
474 #include "tnl_dd/t_dd_tritmp.h"
475
476 #define IND (MGA_TWOSIDE_BIT|MGA_UNFILLED_BIT|MGA_FALLBACK_BIT)
477 #define TAG(x) x##_twoside_unfilled_fallback
478 #include "tnl_dd/t_dd_tritmp.h"
479
480 #define IND (MGA_TWOSIDE_BIT|MGA_OFFSET_BIT|MGA_UNFILLED_BIT| \
481              MGA_FALLBACK_BIT)
482 #define TAG(x) x##_twoside_offset_unfilled_fallback
483 #include "tnl_dd/t_dd_tritmp.h"
484
485
486 /* Mga doesn't support provoking-vertex flat-shading?
487  */
488 #define IND (MGA_FLAT_BIT)
489 #define TAG(x) x##_flat
490 #include "tnl_dd/t_dd_tritmp.h"
491
492 #define IND (MGA_OFFSET_BIT|MGA_FLAT_BIT)
493 #define TAG(x) x##_offset_flat
494 #include "tnl_dd/t_dd_tritmp.h"
495
496 #define IND (MGA_TWOSIDE_BIT|MGA_FLAT_BIT)
497 #define TAG(x) x##_twoside_flat
498 #include "tnl_dd/t_dd_tritmp.h"
499
500 #define IND (MGA_TWOSIDE_BIT|MGA_OFFSET_BIT|MGA_FLAT_BIT)
501 #define TAG(x) x##_twoside_offset_flat
502 #include "tnl_dd/t_dd_tritmp.h"
503
504 #define IND (MGA_UNFILLED_BIT|MGA_FLAT_BIT)
505 #define TAG(x) x##_unfilled_flat
506 #include "tnl_dd/t_dd_tritmp.h"
507
508 #define IND (MGA_OFFSET_BIT|MGA_UNFILLED_BIT|MGA_FLAT_BIT)
509 #define TAG(x) x##_offset_unfilled_flat
510 #include "tnl_dd/t_dd_tritmp.h"
511
512 #define IND (MGA_TWOSIDE_BIT|MGA_UNFILLED_BIT|MGA_FLAT_BIT)
513 #define TAG(x) x##_twoside_unfilled_flat
514 #include "tnl_dd/t_dd_tritmp.h"
515
516 #define IND (MGA_TWOSIDE_BIT|MGA_OFFSET_BIT|MGA_UNFILLED_BIT|MGA_FLAT_BIT)
517 #define TAG(x) x##_twoside_offset_unfilled_flat
518 #include "tnl_dd/t_dd_tritmp.h"
519
520 #define IND (MGA_FALLBACK_BIT|MGA_FLAT_BIT)
521 #define TAG(x) x##_fallback_flat
522 #include "tnl_dd/t_dd_tritmp.h"
523
524 #define IND (MGA_OFFSET_BIT|MGA_FALLBACK_BIT|MGA_FLAT_BIT)
525 #define TAG(x) x##_offset_fallback_flat
526 #include "tnl_dd/t_dd_tritmp.h"
527
528 #define IND (MGA_TWOSIDE_BIT|MGA_FALLBACK_BIT|MGA_FLAT_BIT)
529 #define TAG(x) x##_twoside_fallback_flat
530 #include "tnl_dd/t_dd_tritmp.h"
531
532 #define IND (MGA_TWOSIDE_BIT|MGA_OFFSET_BIT|MGA_FALLBACK_BIT|MGA_FLAT_BIT)
533 #define TAG(x) x##_twoside_offset_fallback_flat
534 #include "tnl_dd/t_dd_tritmp.h"
535
536 #define IND (MGA_UNFILLED_BIT|MGA_FALLBACK_BIT|MGA_FLAT_BIT)
537 #define TAG(x) x##_unfilled_fallback_flat
538 #include "tnl_dd/t_dd_tritmp.h"
539
540 #define IND (MGA_OFFSET_BIT|MGA_UNFILLED_BIT|MGA_FALLBACK_BIT|MGA_FLAT_BIT)
541 #define TAG(x) x##_offset_unfilled_fallback_flat
542 #include "tnl_dd/t_dd_tritmp.h"
543
544 #define IND (MGA_TWOSIDE_BIT|MGA_UNFILLED_BIT|MGA_FALLBACK_BIT|MGA_FLAT_BIT)
545 #define TAG(x) x##_twoside_unfilled_fallback_flat
546 #include "tnl_dd/t_dd_tritmp.h"
547
548 #define IND (MGA_TWOSIDE_BIT|MGA_OFFSET_BIT|MGA_UNFILLED_BIT| \
549              MGA_FALLBACK_BIT|MGA_FLAT_BIT)
550 #define TAG(x) x##_twoside_offset_unfilled_fallback_flat
551 #include "tnl_dd/t_dd_tritmp.h"
552
553
554 static void init_rast_tab( void )
555 {
556    init();
557    init_offset();
558    init_twoside();
559    init_twoside_offset();
560    init_unfilled();
561    init_offset_unfilled();
562    init_twoside_unfilled();
563    init_twoside_offset_unfilled();
564    init_fallback();
565    init_offset_fallback();
566    init_twoside_fallback();
567    init_twoside_offset_fallback();
568    init_unfilled_fallback();
569    init_offset_unfilled_fallback();
570    init_twoside_unfilled_fallback();
571    init_twoside_offset_unfilled_fallback();
572
573    init_flat();
574    init_offset_flat();
575    init_twoside_flat();
576    init_twoside_offset_flat();
577    init_unfilled_flat();
578    init_offset_unfilled_flat();
579    init_twoside_unfilled_flat();
580    init_twoside_offset_unfilled_flat();
581    init_fallback_flat();
582    init_offset_fallback_flat();
583    init_twoside_fallback_flat();
584    init_twoside_offset_fallback_flat();
585    init_unfilled_fallback_flat();
586    init_offset_unfilled_fallback_flat();
587    init_twoside_unfilled_fallback_flat();
588    init_twoside_offset_unfilled_fallback_flat();
589 }
590
591 /**********************************************************************/
592 /*                 Render whole begin/end objects                     */
593 /**********************************************************************/
594
595
596 #define VERT(x) (mgaVertex *)(vertptr + ((x)*vertex_size*sizeof(int)))
597 #define RENDER_POINTS( start, count )           \
598    for ( ; start < count ; start++)             \
599       mga_draw_point( mmesa, VERT(ELT(start)) );
600 #define RENDER_LINE( v0, v1 ) \
601    mga_draw_line( mmesa, VERT(v0), VERT(v1) )
602 #define RENDER_TRI( v0, v1, v2 )  \
603    mga_draw_triangle( mmesa, VERT(v0), VERT(v1), VERT(v2) )
604 #define RENDER_QUAD( v0, v1, v2, v3 ) \
605    mga_draw_quad( mmesa, VERT(v0), VERT(v1), VERT(v2), VERT(v3) )
606 #define INIT(x) mgaRenderPrimitive( ctx, x )
607 #undef LOCAL_VARS
608 #define LOCAL_VARS                                              \
609     mgaContextPtr mmesa = MGA_CONTEXT(ctx);                     \
610     GLubyte *vertptr = (GLubyte *)mmesa->verts;                 \
611     const GLuint vertex_size = mmesa->vertex_size;              \
612     const GLuint * const elt = TNL_CONTEXT(ctx)->vb.Elts;       \
613     (void) elt;
614 #define RESET_STIPPLE 
615 #define RESET_OCCLUSION 
616 #define PRESERVE_VB_DEFS
617 #define ELT(x) x
618 #define TAG(x) mga_##x##_verts
619 #include "tnl/t_vb_rendertmp.h"
620 #undef ELT
621 #undef TAG
622 #define TAG(x) mga_##x##_elts
623 #define ELT(x) elt[x]
624 #include "tnl/t_vb_rendertmp.h"
625
626
627 /**********************************************************************/
628 /*                   Render clipped primitives                        */
629 /**********************************************************************/
630
631
632
633 static void mgaRenderClippedPoly( struct gl_context *ctx, const GLuint *elts, GLuint n )
634 {
635    mgaContextPtr mmesa = MGA_CONTEXT(ctx);
636    TNLcontext *tnl = TNL_CONTEXT(ctx);
637    struct vertex_buffer *VB = &tnl->vb;
638    GLuint prim = mmesa->render_primitive;
639
640    /* Render the new vertices as an unclipped polygon. 
641     */
642    {
643       GLuint *tmp = VB->Elts;
644       VB->Elts = (GLuint *)elts;
645       tnl->Driver.Render.PrimTabElts[GL_POLYGON]( ctx, 0, n, PRIM_BEGIN|PRIM_END );
646       VB->Elts = tmp;
647    }
648
649    /* Restore the render primitive
650     */
651    if (prim != GL_POLYGON)
652       tnl->Driver.Render.PrimitiveNotify( ctx, prim );
653 }
654
655 static void mgaRenderClippedLine( struct gl_context *ctx, GLuint ii, GLuint jj )
656 {
657    TNLcontext *tnl = TNL_CONTEXT(ctx);
658    tnl->Driver.Render.Line( ctx, ii, jj );
659 }
660
661 static void mgaFastRenderClippedPoly( struct gl_context *ctx, const GLuint *elts, 
662                                        GLuint n )
663 {
664    mgaContextPtr mmesa = MGA_CONTEXT( ctx );
665    GLuint vertex_size = mmesa->vertex_size;
666    GLuint *vb = mgaAllocDmaLow( mmesa, (n-2) * 3 * 4 * vertex_size );
667    GLubyte *vertptr = (GLubyte *)mmesa->verts;                  
668    const GLuint *start = (const GLuint *)VERT(elts[0]);
669    int i,j;
670
671    for (i = 2 ; i < n ; i++) {
672       EMIT_VERT( j, vb, vertex_size, (mgaVertexPtr) VERT(elts[i-1]) );
673       EMIT_VERT( j, vb, vertex_size, (mgaVertexPtr) VERT(elts[i]) );
674       EMIT_VERT( j, vb, vertex_size, (mgaVertexPtr) start );
675    }
676 }
677
678 /**********************************************************************/
679 /*                    Choose render functions                         */
680 /**********************************************************************/
681
682
683 #define POINT_FALLBACK (DD_POINT_SMOOTH)
684 #define LINE_FALLBACK (DD_LINE_SMOOTH | DD_LINE_STIPPLE)
685 #define TRI_FALLBACK (DD_TRI_SMOOTH | DD_TRI_UNFILLED)
686 #define ANY_FALLBACK_FLAGS (POINT_FALLBACK|LINE_FALLBACK|TRI_FALLBACK)
687 #define ANY_RASTER_FLAGS (DD_FLATSHADE|DD_TRI_LIGHT_TWOSIDE|DD_TRI_OFFSET| \
688                           DD_TRI_UNFILLED)
689
690 void mgaChooseRenderState(struct gl_context *ctx)
691 {
692    TNLcontext *tnl = TNL_CONTEXT(ctx);
693    mgaContextPtr mmesa = MGA_CONTEXT(ctx);
694    GLuint flags = ctx->_TriangleCaps;
695    GLuint index = 0;
696
697    if (flags & (ANY_FALLBACK_FLAGS|ANY_RASTER_FLAGS|DD_TRI_STIPPLE)) {
698       if (flags & ANY_RASTER_FLAGS) {
699          if (flags & DD_TRI_LIGHT_TWOSIDE)    index |= MGA_TWOSIDE_BIT;
700          if (flags & DD_TRI_OFFSET)           index |= MGA_OFFSET_BIT;
701          if (flags & DD_TRI_UNFILLED)         index |= MGA_UNFILLED_BIT;
702          if (flags & DD_FLATSHADE)            index |= MGA_FLAT_BIT;
703       }
704
705       mmesa->draw_point = mga_draw_point;
706       mmesa->draw_line = mga_draw_line;
707       mmesa->draw_tri = mga_draw_triangle;
708
709       /* Hook in fallbacks for specific primitives.
710        */
711       if (flags & ANY_FALLBACK_FLAGS)
712       {
713          if (flags & POINT_FALLBACK) 
714             mmesa->draw_point = mga_fallback_point;
715          
716          if (flags & LINE_FALLBACK) 
717             mmesa->draw_line = mga_fallback_line;
718          
719          if (flags & TRI_FALLBACK) 
720             mmesa->draw_tri = mga_fallback_tri;
721          
722          index |= MGA_FALLBACK_BIT;
723       }
724
725       if ((flags & DD_TRI_STIPPLE) && !mmesa->haveHwStipple) {
726          mmesa->draw_tri = mga_fallback_tri;
727          index |= MGA_FALLBACK_BIT;
728       }
729    }
730
731    if (mmesa->RenderIndex != index) {
732       mmesa->RenderIndex = index;
733
734       tnl->Driver.Render.Points = rast_tab[index].points;
735       tnl->Driver.Render.Line = rast_tab[index].line;
736       tnl->Driver.Render.Triangle = rast_tab[index].triangle;
737       tnl->Driver.Render.Quad = rast_tab[index].quad;
738          
739       if (index == 0) {
740          tnl->Driver.Render.PrimTabVerts = mga_render_tab_verts;
741          tnl->Driver.Render.PrimTabElts = mga_render_tab_elts;
742          tnl->Driver.Render.ClippedLine = line; /* from tritmp.h */
743          tnl->Driver.Render.ClippedPolygon = mgaFastRenderClippedPoly;
744       } else {
745          tnl->Driver.Render.PrimTabVerts = _tnl_render_tab_verts;
746          tnl->Driver.Render.PrimTabElts = _tnl_render_tab_elts;
747          tnl->Driver.Render.ClippedLine = mgaRenderClippedLine;
748          tnl->Driver.Render.ClippedPolygon = mgaRenderClippedPoly;
749       }
750    }
751 }
752
753 /**********************************************************************/
754 /*                Runtime render state and callbacks                  */
755 /**********************************************************************/
756
757
758 static GLenum reduced_prim[GL_POLYGON+1] = {
759    GL_POINTS,
760    GL_LINES,
761    GL_LINES,
762    GL_LINES,
763    GL_TRIANGLES,
764    GL_TRIANGLES,
765    GL_TRIANGLES,
766    GL_TRIANGLES,
767    GL_TRIANGLES,
768    GL_TRIANGLES
769 };
770
771
772
773 /* Always called between RenderStart and RenderFinish --> We already
774  * hold the lock.
775  */
776 void mgaRasterPrimitive( struct gl_context *ctx, GLenum prim, GLuint hwprim )
777 {
778    mgaContextPtr mmesa = MGA_CONTEXT( ctx );
779
780    FLUSH_BATCH( mmesa );
781
782    /* Update culling */
783    if (mmesa->raster_primitive != prim)
784       mmesa->dirty |= MGA_UPLOAD_CONTEXT;
785
786    mmesa->raster_primitive = prim;
787 /*     mmesa->hw_primitive = hwprim; */
788    mmesa->hw_primitive = MGA_WA_TRIANGLES; /* disable mgarender.c for now */
789
790    if (ctx->Polygon.StippleFlag && mmesa->haveHwStipple)
791    {
792       mmesa->dirty |= MGA_UPLOAD_CONTEXT;
793       mmesa->setup.dwgctl &= ~(0xf<<20);
794       if (mmesa->raster_primitive == GL_TRIANGLES)
795          mmesa->setup.dwgctl |= mmesa->poly_stipple;
796    }
797 }
798
799
800
801 /* Determine the rasterized primitive when not drawing unfilled 
802  * polygons.
803  *
804  * Used only for the default render stage which always decomposes
805  * primitives to trianges/lines/points.  For the accelerated stage,
806  * which renders strips as strips, the equivalent calculations are
807  * performed in mgarender.c.
808  */
809 static void mgaRenderPrimitive( struct gl_context *ctx, GLenum prim )
810 {
811    mgaContextPtr mmesa = MGA_CONTEXT(ctx);
812    GLuint rprim = reduced_prim[prim];
813
814    mmesa->render_primitive = prim;
815
816    if (rprim == GL_TRIANGLES && (ctx->_TriangleCaps & DD_TRI_UNFILLED))
817       return;
818        
819    if (mmesa->raster_primitive != rprim) {
820       mgaRasterPrimitive( ctx, rprim, MGA_WA_TRIANGLES );
821    }
822 }
823
824 static void mgaRenderFinish( struct gl_context *ctx )
825 {
826    if (MGA_CONTEXT(ctx)->RenderIndex & MGA_FALLBACK_BIT)
827       _swrast_flush( ctx );
828 }
829
830
831
832 /**********************************************************************/
833 /*               Manage total rasterization fallbacks                 */
834 /**********************************************************************/
835
836 static const char * const fallbackStrings[] = {
837    "Texture mode",
838    "glDrawBuffer(GL_FRONT_AND_BACK)",
839    "read buffer",
840    "glBlendFunc(GL_SRC_ALPHA_SATURATE, GL_ZERO)",
841    "glRenderMode(selection or feedback)",
842    "No hardware stencil",
843    "glDepthFunc( GL_NEVER )",
844    "Mixing GL_CLAMP_TO_EDGE and GL_CLAMP",
845    "rasterization fallback option"
846 };
847
848 static const char *getFallbackString(GLuint bit)
849 {
850    int i = 0;
851    while (bit > 1) {
852       i++;
853       bit >>= 1;
854    }
855    return fallbackStrings[i];
856 }
857
858
859 void mgaFallback( struct gl_context *ctx, GLuint bit, GLboolean mode )
860 {
861    TNLcontext *tnl = TNL_CONTEXT(ctx);
862    mgaContextPtr mmesa = MGA_CONTEXT(ctx);
863    GLuint oldfallback = mmesa->Fallback;
864
865    if (mode) {
866       mmesa->Fallback |= bit;
867       if (oldfallback == 0) {
868          FLUSH_BATCH(mmesa);
869          _swsetup_Wakeup( ctx );
870          mmesa->RenderIndex = ~0;
871          if (MGA_DEBUG & DEBUG_VERBOSE_FALLBACK) {
872             fprintf(stderr, "MGA begin rasterization fallback: 0x%x %s\n",
873                     bit, getFallbackString(bit));
874          }
875       }
876    }
877    else {
878       mmesa->Fallback &= ~bit;
879       if (oldfallback == bit) {
880          _swrast_flush( ctx );
881          tnl->Driver.Render.Start = mgaCheckTexSizes;
882          tnl->Driver.Render.PrimitiveNotify = mgaRenderPrimitive;
883          tnl->Driver.Render.Finish = mgaRenderFinish;
884          tnl->Driver.Render.BuildVertices = mgaBuildVertices;
885          mmesa->NewGLState |= (_MGA_NEW_RENDERSTATE |
886                                _MGA_NEW_RASTERSETUP);
887          if (MGA_DEBUG & DEBUG_VERBOSE_FALLBACK) {
888             fprintf(stderr, "MGA end rasterization fallback: 0x%x %s\n",
889                     bit, getFallbackString(bit));
890          }
891       }
892    }
893 }
894
895
896 void mgaDDInitTriFuncs( struct gl_context *ctx )
897 {
898    TNLcontext *tnl = TNL_CONTEXT(ctx);
899    mgaContextPtr mmesa = MGA_CONTEXT(ctx);
900    static int firsttime = 1;
901    if (firsttime) {
902       init_rast_tab();
903       firsttime = 0;
904    }
905
906    mmesa->RenderIndex = ~0;
907         
908    tnl->Driver.Render.Start              = mgaCheckTexSizes;
909    tnl->Driver.Render.Finish             = mgaRenderFinish; 
910    tnl->Driver.Render.PrimitiveNotify    = mgaRenderPrimitive;
911    tnl->Driver.Render.ResetLineStipple   = _swrast_ResetLineStipple;
912    tnl->Driver.Render.BuildVertices      = mgaBuildVertices;
913    tnl->Driver.Render.Multipass          = NULL;
914 }