patch to import Jon Smirl's work from Bitkeeper
[mesa.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 /* $XFree86: xc/lib/GL/mesa/src/drv/mga/mgatris.c,v 1.10 2002/10/30 12:51:36 alanh Exp $ */
28
29 #include "mtypes.h"
30 #include "macros.h"
31 #include "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"
36
37 #include "mm.h"
38 #include "mgacontext.h"
39 #include "mgaioctl.h"
40 #include "mgatris.h"
41 #include "mgavb.h"
42 #include "mgastate.h"
43
44
45 static void mgaRenderPrimitive( GLcontext *ctx, GLenum prim );
46
47 /***********************************************************************
48 * Functions to draw basic primitives *
49 ***********************************************************************/
50
51
52 #if defined (USE_X86_ASM)
53 #define EMIT_VERT( j, vb, vertex_size, v ) \
54 do { int __tmp; \
55 __asm__ __volatile__( "rep ; movsl" \
56 : "=%c" (j), "=D" (vb), "=S" (__tmp) \
57 : "0" (vertex_size), \
58 "D" ((long)vb), \
59 "S" ((long)v)); \
60 } while (0)
61 #else
62 #define EMIT_VERT( j, vb, vertex_size, v ) \
63 do { \
64 for ( j = 0 ; j < vertex_size ; j++ ) \
65 vb[j] = (v)->ui[j]; \
66 vb += vertex_size; \
67 } while (0)
68 #endif
69
70 static void __inline__ mga_draw_triangle( mgaContextPtr mmesa,
71 mgaVertexPtr v0,
72 mgaVertexPtr v1,
73 mgaVertexPtr v2 )
74 {
75 GLuint vertex_size = mmesa->vertex_size;
76 GLuint *vb = mgaAllocDmaLow( mmesa, 3 * 4 * vertex_size );
77 int j;
78
79 EMIT_VERT( j, vb, vertex_size, v0 );
80 EMIT_VERT( j, vb, vertex_size, v1 );
81 EMIT_VERT( j, vb, vertex_size, v2 );
82 }
83
84
85 static void __inline__ mga_draw_quad( mgaContextPtr mmesa,
86 mgaVertexPtr v0,
87 mgaVertexPtr v1,
88 mgaVertexPtr v2,
89 mgaVertexPtr v3 )
90 {
91 GLuint vertex_size = mmesa->vertex_size;
92 GLuint *vb = mgaAllocDmaLow( mmesa, 6 * 4 * vertex_size );
93 int j;
94
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 );
101 }
102
103
104 static __inline__ void mga_draw_point( mgaContextPtr mmesa,
105 mgaVertexPtr tmp )
106 {
107 GLfloat sz = mmesa->glCtx->Point._Size * .5;
108 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 GLfloat width = mmesa->glCtx->Line._Width;
169 GLint j;
170
171 #if 0
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;
176 #endif
177
178 dx = v0->v.x - v1->v.x;
179 dy = v0->v.y - v1->v.y;
180
181 ix = width * .5; iy = 0;
182 if (dx * dx > dy * dy) {
183 iy = ix; ix = 0;
184 }
185
186 *(float *)&vb[0] = v0->v.x - ix;
187 *(float *)&vb[1] = v0->v.y - iy;
188 for (j = 2 ; j < vertex_size ; j++)
189 vb[j] = v0->ui[j];
190 vb += vertex_size;
191
192 *(float *)&vb[0] = v1->v.x + ix;
193 *(float *)&vb[1] = v1->v.y + iy;
194 for (j = 2 ; j < vertex_size ; j++)
195 vb[j] = v1->ui[j];
196 vb += vertex_size;
197
198 *(float *)&vb[0] = v0->v.x + ix;
199 *(float *)&vb[1] = v0->v.y + iy;
200 for (j = 2 ; j < vertex_size ; j++)
201 vb[j] = v0->ui[j];
202 vb += vertex_size;
203
204 *(float *)&vb[0] = v0->v.x - ix;
205 *(float *)&vb[1] = v0->v.y - iy;
206 for (j = 2 ; j < vertex_size ; j++)
207 vb[j] = v0->ui[j];
208 vb += vertex_size;
209
210 *(float *)&vb[0] = v1->v.x - ix;
211 *(float *)&vb[1] = v1->v.y - iy;
212 for (j = 2 ; j < vertex_size ; j++)
213 vb[j] = v1->ui[j];
214 vb += vertex_size;
215
216 *(float *)&vb[0] = v1->v.x + ix;
217 *(float *)&vb[1] = v1->v.y + iy;
218 for (j = 2 ; j < vertex_size ; j++)
219 vb[j] = v1->ui[j];
220 vb += vertex_size;
221
222 #if 0
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;
227 #endif
228 }
229
230 /***********************************************************************
231 * Macros for t_dd_tritmp.h to draw basic primitives *
232 ***********************************************************************/
233
234 #define TRI( a, b, c ) \
235 do { \
236 if (DO_FALLBACK) \
237 mmesa->draw_tri( mmesa, a, b, c ); \
238 else \
239 mga_draw_triangle( mmesa, a, b, c ); \
240 } while (0)
241
242 #define QUAD( a, b, c, d ) \
243 do { \
244 if (DO_FALLBACK) { \
245 mmesa->draw_tri( mmesa, a, b, d ); \
246 mmesa->draw_tri( mmesa, b, c, d ); \
247 } else { \
248 mga_draw_quad( mmesa, a, b, c, d ); \
249 } \
250 } while (0)
251
252 #define LINE( v0, v1 ) \
253 do { \
254 if (DO_FALLBACK) \
255 mmesa->draw_line( mmesa, v0, v1 ); \
256 else { \
257 mga_draw_line( mmesa, v0, v1 ); \
258 } \
259 } while (0)
260
261 #define POINT( v0 ) \
262 do { \
263 if (DO_FALLBACK) \
264 mmesa->draw_point( mmesa, v0 ); \
265 else { \
266 mga_draw_point( mmesa, v0 ); \
267 } \
268 } while (0)
269
270
271 /***********************************************************************
272 * Fallback to swrast for basic primitives *
273 ***********************************************************************/
274
275 /* This code is hit only when a mix of accelerated and unaccelerated
276 * primitives are being drawn, and only for the unaccelerated
277 * primitives.
278 */
279
280 static void
281 mga_fallback_tri( mgaContextPtr mmesa,
282 mgaVertex *v0,
283 mgaVertex *v1,
284 mgaVertex *v2 )
285 {
286 GLcontext *ctx = mmesa->glCtx;
287 SWvertex v[3];
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] );
292 }
293
294
295 static void
296 mga_fallback_line( mgaContextPtr mmesa,
297 mgaVertex *v0,
298 mgaVertex *v1 )
299 {
300 GLcontext *ctx = mmesa->glCtx;
301 SWvertex v[2];
302 mga_translate_vertex( ctx, v0, &v[0] );
303 mga_translate_vertex( ctx, v1, &v[1] );
304 _swrast_Line( ctx, &v[0], &v[1] );
305 }
306
307
308 static void
309 mga_fallback_point( mgaContextPtr mmesa,
310 mgaVertex *v0 )
311 {
312 GLcontext *ctx = mmesa->glCtx;
313 SWvertex v[1];
314 mga_translate_vertex( ctx, v0, &v[0] );
315 _swrast_Point( ctx, &v[0] );
316 }
317
318 /***********************************************************************
319 * Build render functions from dd templates *
320 ***********************************************************************/
321
322
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
329
330 static struct {
331 points_func points;
332 line_func line;
333 triangle_func triangle;
334 quad_func quad;
335 } rast_tab[MGA_MAX_TRIFUNC];
336
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)
342 #define DO_TRI 1
343 #define DO_QUAD 1
344 #define DO_LINE 1
345 #define DO_POINTS 1
346 #define DO_FULL_QUAD 1
347
348 #define HAVE_RGBA 1
349 #define HAVE_BACK_COLORS 0
350 #define HAVE_SPEC 1
351 #define HAVE_HW_FLATSHADE 0
352 #define VERTEX mgaVertex
353 #define TAB rast_tab
354
355 #define MGA_COLOR( dst, src ) \
356 do { \
357 dst[0] = src[2]; \
358 dst[1] = src[1]; \
359 dst[2] = src[0]; \
360 dst[3] = src[3]; \
361 } while (0)
362
363 #define MGA_SPEC( dst, src ) \
364 do { \
365 dst[0] = src[2]; \
366 dst[1] = src[1]; \
367 dst[2] = src[0]; \
368 } while (0)
369
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))
378
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]
383
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]
388
389 #define LOCAL_VARS(n) \
390 mgaContextPtr mmesa = MGA_CONTEXT(ctx); \
391 GLuint color[n], spec[n]; \
392 (void) color; (void) spec;
393
394
395
396 /***********************************************************************
397 * Functions to draw basic unfilled primitives *
398 ***********************************************************************/
399
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
404 #define TAG(x) x
405 #include "tnl_dd/t_dd_unfilled.h"
406 #undef IND
407
408 /***********************************************************************
409 * Functions to draw GL primitives *
410 ***********************************************************************/
411
412 #define IND (0)
413 #define TAG(x) x
414 #include "tnl_dd/t_dd_tritmp.h"
415
416 #define IND (MGA_OFFSET_BIT)
417 #define TAG(x) x##_offset
418 #include "tnl_dd/t_dd_tritmp.h"
419
420 #define IND (MGA_TWOSIDE_BIT)
421 #define TAG(x) x##_twoside
422 #include "tnl_dd/t_dd_tritmp.h"
423
424 #define IND (MGA_TWOSIDE_BIT|MGA_OFFSET_BIT)
425 #define TAG(x) x##_twoside_offset
426 #include "tnl_dd/t_dd_tritmp.h"
427
428 #define IND (MGA_UNFILLED_BIT)
429 #define TAG(x) x##_unfilled
430 #include "tnl_dd/t_dd_tritmp.h"
431
432 #define IND (MGA_OFFSET_BIT|MGA_UNFILLED_BIT)
433 #define TAG(x) x##_offset_unfilled
434 #include "tnl_dd/t_dd_tritmp.h"
435
436 #define IND (MGA_TWOSIDE_BIT|MGA_UNFILLED_BIT)
437 #define TAG(x) x##_twoside_unfilled
438 #include "tnl_dd/t_dd_tritmp.h"
439
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"
443
444 #define IND (MGA_FALLBACK_BIT)
445 #define TAG(x) x##_fallback
446 #include "tnl_dd/t_dd_tritmp.h"
447
448 #define IND (MGA_OFFSET_BIT|MGA_FALLBACK_BIT)
449 #define TAG(x) x##_offset_fallback
450 #include "tnl_dd/t_dd_tritmp.h"
451
452 #define IND (MGA_TWOSIDE_BIT|MGA_FALLBACK_BIT)
453 #define TAG(x) x##_twoside_fallback
454 #include "tnl_dd/t_dd_tritmp.h"
455
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"
459
460 #define IND (MGA_UNFILLED_BIT|MGA_FALLBACK_BIT)
461 #define TAG(x) x##_unfilled_fallback
462 #include "tnl_dd/t_dd_tritmp.h"
463
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"
467
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"
471
472 #define IND (MGA_TWOSIDE_BIT|MGA_OFFSET_BIT|MGA_UNFILLED_BIT| \
473 MGA_FALLBACK_BIT)
474 #define TAG(x) x##_twoside_offset_unfilled_fallback
475 #include "tnl_dd/t_dd_tritmp.h"
476
477
478 /* Mga doesn't support provoking-vertex flat-shading?
479 */
480 #define IND (MGA_FLAT_BIT)
481 #define TAG(x) x##_flat
482 #include "tnl_dd/t_dd_tritmp.h"
483
484 #define IND (MGA_OFFSET_BIT|MGA_FLAT_BIT)
485 #define TAG(x) x##_offset_flat
486 #include "tnl_dd/t_dd_tritmp.h"
487
488 #define IND (MGA_TWOSIDE_BIT|MGA_FLAT_BIT)
489 #define TAG(x) x##_twoside_flat
490 #include "tnl_dd/t_dd_tritmp.h"
491
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"
495
496 #define IND (MGA_UNFILLED_BIT|MGA_FLAT_BIT)
497 #define TAG(x) x##_unfilled_flat
498 #include "tnl_dd/t_dd_tritmp.h"
499
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"
503
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"
507
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"
511
512 #define IND (MGA_FALLBACK_BIT|MGA_FLAT_BIT)
513 #define TAG(x) x##_fallback_flat
514 #include "tnl_dd/t_dd_tritmp.h"
515
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"
519
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"
523
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"
527
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"
531
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"
535
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"
539
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"
544
545
546 static void init_rast_tab( void )
547 {
548 init();
549 init_offset();
550 init_twoside();
551 init_twoside_offset();
552 init_unfilled();
553 init_offset_unfilled();
554 init_twoside_unfilled();
555 init_twoside_offset_unfilled();
556 init_fallback();
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();
564
565 init_flat();
566 init_offset_flat();
567 init_twoside_flat();
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();
581 }
582
583 /**********************************************************************/
584 /* Render whole begin/end objects */
585 /**********************************************************************/
586
587
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 )
599 #undef LOCAL_VARS
600 #define LOCAL_VARS \
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; \
605 (void) elt;
606 #define RESET_STIPPLE
607 #define RESET_OCCLUSION
608 #define PRESERVE_VB_DEFS
609 #define ELT(x) x
610 #define TAG(x) mga_##x##_verts
611 #include "tnl/t_vb_rendertmp.h"
612 #undef ELT
613 #undef TAG
614 #define TAG(x) mga_##x##_elts
615 #define ELT(x) elt[x]
616 #include "tnl/t_vb_rendertmp.h"
617
618
619 /**********************************************************************/
620 /* Render clipped primitives */
621 /**********************************************************************/
622
623
624
625 static void mgaRenderClippedPoly( GLcontext *ctx, const GLuint *elts, GLuint n )
626 {
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;
631
632 /* Render the new vertices as an unclipped polygon.
633 */
634 {
635 GLuint *tmp = VB->Elts;
636 VB->Elts = (GLuint *)elts;
637 tnl->Driver.Render.PrimTabElts[GL_POLYGON]( ctx, 0, n, PRIM_BEGIN|PRIM_END );
638 VB->Elts = tmp;
639 }
640
641 /* Restore the render primitive
642 */
643 if (prim != GL_POLYGON)
644 tnl->Driver.Render.PrimitiveNotify( ctx, prim );
645 }
646
647 static void mgaRenderClippedLine( GLcontext *ctx, GLuint ii, GLuint jj )
648 {
649 TNLcontext *tnl = TNL_CONTEXT(ctx);
650 tnl->Driver.Render.Line( ctx, ii, jj );
651 }
652
653 static void mgaFastRenderClippedPoly( GLcontext *ctx, const GLuint *elts,
654 GLuint n )
655 {
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]);
662 int i,j;
663
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 );
668 }
669 }
670
671 /**********************************************************************/
672 /* Choose render functions */
673 /**********************************************************************/
674
675
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| \
680 DD_TRI_STIPPLE)
681 #define ANY_RASTER_FLAGS (DD_FLATSHADE|DD_TRI_LIGHT_TWOSIDE|DD_TRI_OFFSET| \
682 DD_TRI_UNFILLED)
683
684 void mgaChooseRenderState(GLcontext *ctx)
685 {
686 TNLcontext *tnl = TNL_CONTEXT(ctx);
687 mgaContextPtr mmesa = MGA_CONTEXT(ctx);
688 GLuint flags = ctx->_TriangleCaps;
689 GLuint index = 0;
690
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;
697 }
698
699 mmesa->draw_point = mga_draw_point;
700 mmesa->draw_line = mga_draw_line;
701 mmesa->draw_tri = mga_draw_triangle;
702
703 /* Hook in fallbacks for specific primitives.
704 */
705 if (flags & ANY_FALLBACK_FLAGS)
706 {
707 if (flags & POINT_FALLBACK)
708 mmesa->draw_point = mga_fallback_point;
709
710 if (flags & LINE_FALLBACK)
711 mmesa->draw_line = mga_fallback_line;
712
713 if (flags & TRI_FALLBACK)
714 mmesa->draw_tri = mga_fallback_tri;
715
716 if ((flags & DD_TRI_STIPPLE) && !mmesa->haveHwStipple)
717 mmesa->draw_tri = mga_fallback_tri;
718
719 index |= MGA_FALLBACK_BIT;
720 }
721 }
722
723 if (mmesa->RenderIndex != index) {
724 mmesa->RenderIndex = index;
725
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;
730
731 if (index == 0) {
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;
736 } else {
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;
741 }
742 }
743 }
744
745 /**********************************************************************/
746 /* Runtime render state and callbacks */
747 /**********************************************************************/
748
749
750 static GLenum reduced_prim[GL_POLYGON+1] = {
751 GL_POINTS,
752 GL_LINES,
753 GL_LINES,
754 GL_LINES,
755 GL_TRIANGLES,
756 GL_TRIANGLES,
757 GL_TRIANGLES,
758 GL_TRIANGLES,
759 GL_TRIANGLES,
760 GL_TRIANGLES
761 };
762
763
764
765 /* Always called between RenderStart and RenderFinish --> We already
766 * hold the lock.
767 */
768 void mgaRasterPrimitive( GLcontext *ctx, GLenum prim, GLuint hwprim )
769 {
770 mgaContextPtr mmesa = MGA_CONTEXT( ctx );
771
772 FLUSH_BATCH( mmesa );
773
774 /* Update culling */
775 if (mmesa->raster_primitive != prim)
776 mmesa->dirty |= MGA_UPLOAD_CONTEXT;
777
778 mmesa->raster_primitive = prim;
779 /* mmesa->hw_primitive = hwprim; */
780 mmesa->hw_primitive = MGA_WA_TRIANGLES; /* disable mgarender.c for now */
781
782 if (ctx->Polygon.StippleFlag && mmesa->haveHwStipple)
783 {
784 mmesa->dirty |= MGA_UPLOAD_CONTEXT;
785 if (mmesa->raster_primitive == GL_TRIANGLES)
786 mmesa->setup.dwgctl |= mmesa->poly_stipple;
787 else
788 mmesa->setup.dwgctl &= ~(0xf<<20);
789 }
790 }
791
792
793
794 /* Determine the rasterized primitive when not drawing unfilled
795 * polygons.
796 *
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.
801 */
802 static void mgaRenderPrimitive( GLcontext *ctx, GLenum prim )
803 {
804 mgaContextPtr mmesa = MGA_CONTEXT(ctx);
805 GLuint rprim = reduced_prim[prim];
806
807 mmesa->render_primitive = prim;
808
809 if (rprim == GL_TRIANGLES && (ctx->_TriangleCaps & DD_TRI_UNFILLED))
810 return;
811
812 if (mmesa->raster_primitive != rprim) {
813 mgaRasterPrimitive( ctx, rprim, MGA_WA_TRIANGLES );
814 }
815 }
816
817 static void mgaRenderFinish( GLcontext *ctx )
818 {
819 if (MGA_CONTEXT(ctx)->RenderIndex & MGA_FALLBACK_BIT)
820 _swrast_flush( ctx );
821 }
822
823
824
825 /**********************************************************************/
826 /* Manage total rasterization fallbacks */
827 /**********************************************************************/
828
829 static const char * const fallbackStrings[] = {
830 "Texture mode",
831 "glDrawBuffer(GL_FRONT_AND_BACK)",
832 "read buffer",
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"
838 };
839
840 static const char *getFallbackString(GLuint bit)
841 {
842 int i = 0;
843 while (bit > 1) {
844 i++;
845 bit >>= 1;
846 }
847 return fallbackStrings[i];
848 }
849
850
851 void mgaFallback( GLcontext *ctx, GLuint bit, GLboolean mode )
852 {
853 TNLcontext *tnl = TNL_CONTEXT(ctx);
854 mgaContextPtr mmesa = MGA_CONTEXT(ctx);
855 GLuint oldfallback = mmesa->Fallback;
856
857 if (mode) {
858 mmesa->Fallback |= bit;
859 if (oldfallback == 0) {
860 FLUSH_BATCH(mmesa);
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));
866 }
867 }
868 }
869 else {
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));
882 }
883 }
884 }
885 }
886
887
888 void mgaDDInitTriFuncs( GLcontext *ctx )
889 {
890 TNLcontext *tnl = TNL_CONTEXT(ctx);
891 mgaContextPtr mmesa = MGA_CONTEXT(ctx);
892 static int firsttime = 1;
893 if (firsttime) {
894 init_rast_tab();
895 firsttime = 0;
896 }
897
898 mmesa->RenderIndex = ~0;
899
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;
906 }