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