1 /* -*- mode: c; c-basic-offset: 3 -*-
3 * Copyright 2000 VA Linux Systems Inc., Fremont, California.
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice (including the next
15 * paragraph) shall be included in all copies or substantial portions of the
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21 * VA LINUX SYSTEMS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
22 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
23 * OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
26 /* $XFree86: xc/lib/GL/mesa/src/drv/tdfx/tdfx_tris.c,v 1.4 2002/10/30 12:52:01 alanh Exp $ */
29 * Keith Whitwell <keith@tungstengraphics.com>
37 #include "swrast/swrast.h"
38 #include "swrast_setup/swrast_setup.h"
39 #include "swrast_setup/ss_context.h"
40 #include "tnl/t_context.h"
41 #include "tnl/t_pipeline.h"
43 #include "tdfx_tris.h"
44 #include "tdfx_state.h"
46 #include "tdfx_lock.h"
47 #include "tdfx_render.h"
50 static void tdfxRasterPrimitive( GLcontext
*ctx
, GLenum prim
);
51 static void tdfxRenderPrimitive( GLcontext
*ctx
, GLenum prim
);
53 /***********************************************************************
54 * Macros for t_dd_tritmp.h to draw basic primitives *
55 ***********************************************************************/
57 #define TRI( a, b, c ) \
60 fxMesa->draw_triangle( fxMesa, a, b, c ); \
62 fxMesa->Glide.grDrawTriangle( a, b, c ); \
65 #define QUAD( a, b, c, d ) \
68 fxMesa->draw_triangle( fxMesa, a, b, d ); \
69 fxMesa->draw_triangle( fxMesa, b, c, d ); \
71 fxMesa->Glide.grDrawTriangle( a, b, d ); \
72 fxMesa->Glide.grDrawTriangle( b, c, d ); \
76 #define LINE( v0, v1 ) \
79 fxMesa->draw_line( fxMesa, v0, v1 ); \
81 v0->v.x += LINE_X_OFFSET - TRI_X_OFFSET; \
82 v0->v.y += LINE_Y_OFFSET - TRI_Y_OFFSET; \
83 v1->v.x += LINE_X_OFFSET - TRI_X_OFFSET; \
84 v1->v.y += LINE_Y_OFFSET - TRI_Y_OFFSET; \
85 fxMesa->Glide.grDrawLine( v0, v1 ); \
86 v0->v.x -= LINE_X_OFFSET - TRI_X_OFFSET; \
87 v0->v.y -= LINE_Y_OFFSET - TRI_Y_OFFSET; \
88 v1->v.x -= LINE_X_OFFSET - TRI_X_OFFSET; \
89 v1->v.y -= LINE_Y_OFFSET - TRI_Y_OFFSET; \
96 fxMesa->draw_point( fxMesa, v0 ); \
98 v0->v.x += PNT_X_OFFSET - TRI_X_OFFSET; \
99 v0->v.y += PNT_Y_OFFSET - TRI_Y_OFFSET; \
100 fxMesa->Glide.grDrawPoint( v0 ); \
101 v0->v.x -= PNT_X_OFFSET - TRI_X_OFFSET; \
102 v0->v.y -= PNT_Y_OFFSET - TRI_Y_OFFSET; \
107 /***********************************************************************
108 * Fallback to swrast for basic primitives *
109 ***********************************************************************/
111 /* Build an SWvertex from a hardware vertex.
113 * This code is hit only when a mix of accelerated and unaccelerated
114 * primitives are being drawn, and only for the unaccelerated
118 tdfx_translate_vertex( GLcontext
*ctx
, const tdfxVertex
*src
, SWvertex
*dst
)
120 tdfxContextPtr fxMesa
= TDFX_CONTEXT(ctx
);
122 if (fxMesa
->vertexFormat
== TDFX_LAYOUT_TINY
) {
123 dst
->win
[0] = src
->tv
.x
- fxMesa
->x_offset
;
124 dst
->win
[1] = src
->tv
.y
- (fxMesa
->screen_height
- fxMesa
->height
- fxMesa
->y_offset
);
125 dst
->win
[2] = src
->tv
.z
;
128 dst
->color
[0] = src
->tv
.color
.red
;
129 dst
->color
[1] = src
->tv
.color
.green
;
130 dst
->color
[2] = src
->tv
.color
.blue
;
131 dst
->color
[3] = src
->tv
.color
.alpha
;
134 GLfloat w
= 1.0 / src
->v
.rhw
;
136 dst
->win
[0] = src
->v
.x
- fxMesa
->x_offset
;
137 dst
->win
[1] = fxMesa
->screen_height
- fxMesa
->y_offset
- src
->v
.y
;
138 dst
->win
[2] = src
->v
.z
;
139 dst
->win
[3] = src
->v
.rhw
;
141 dst
->color
[0] = src
->v
.color
.red
;
142 dst
->color
[1] = src
->v
.color
.green
;
143 dst
->color
[2] = src
->v
.color
.blue
;
144 dst
->color
[3] = src
->v
.color
.alpha
;
146 if (fxMesa
->vertexFormat
== TDFX_LAYOUT_PROJECT
) {
147 dst
->texcoord
[0][0] = fxMesa
->sScale0
* w
* src
->pv
.tu0
;
148 dst
->texcoord
[0][1] = fxMesa
->tScale0
* w
* src
->pv
.tv0
;
149 dst
->texcoord
[0][3] = w
* src
->pv
.tq0
;
151 if (fxMesa
->SetupIndex
& TDFX_TEX1_BIT
) {
152 dst
->texcoord
[1][0] = fxMesa
->sScale1
* w
* src
->pv
.tu1
;
153 dst
->texcoord
[1][1] = fxMesa
->tScale1
* w
* src
->pv
.tv1
;
154 dst
->texcoord
[1][3] = w
* src
->pv
.tq1
;
156 } else if (fxMesa
->SetupIndex
& TDFX_TEX0_BIT
) {
157 dst
->texcoord
[0][0] = fxMesa
->sScale0
* w
* src
->v
.tu0
;
158 dst
->texcoord
[0][1] = fxMesa
->tScale0
* w
* src
->v
.tv0
;
159 dst
->texcoord
[0][3] = 1.0;
161 if (fxMesa
->SetupIndex
& TDFX_TEX1_BIT
) {
162 dst
->texcoord
[1][0] = fxMesa
->sScale1
* w
* src
->v
.tu1
;
163 dst
->texcoord
[1][1] = fxMesa
->tScale1
* w
* src
->v
.tv1
;
164 dst
->texcoord
[1][3] = 1.0;
169 dst
->pointSize
= ctx
->Point
._Size
;
174 tdfx_fallback_tri( tdfxContextPtr fxMesa
,
179 GLcontext
*ctx
= fxMesa
->glCtx
;
181 tdfx_translate_vertex( ctx
, v0
, &v
[0] );
182 tdfx_translate_vertex( ctx
, v1
, &v
[1] );
183 tdfx_translate_vertex( ctx
, v2
, &v
[2] );
184 _swrast_Triangle( ctx
, &v
[0], &v
[1], &v
[2] );
189 tdfx_fallback_line( tdfxContextPtr fxMesa
,
193 GLcontext
*ctx
= fxMesa
->glCtx
;
195 tdfx_translate_vertex( ctx
, v0
, &v
[0] );
196 tdfx_translate_vertex( ctx
, v1
, &v
[1] );
197 _swrast_Line( ctx
, &v
[0], &v
[1] );
202 tdfx_fallback_point( tdfxContextPtr fxMesa
,
205 GLcontext
*ctx
= fxMesa
->glCtx
;
207 tdfx_translate_vertex( ctx
, v0
, &v
[0] );
208 _swrast_Point( ctx
, &v
[0] );
211 /***********************************************************************
212 * Functions to draw basic primitives *
213 ***********************************************************************/
215 static void tdfx_print_vertex( GLcontext
*ctx
, const tdfxVertex
*v
)
217 tdfxContextPtr imesa
= TDFX_CONTEXT( ctx
);
219 fprintf(stderr
, "vertex at %p\n", v
);
221 if (imesa
->vertexFormat
== TDFX_LAYOUT_TINY
) {
222 fprintf(stderr
, "x %f y %f z %f\n", v
->v
.x
, v
->v
.y
, v
->v
.z
);
223 fprintf(stderr
, "r %d g %d b %d a %d\n",
230 fprintf(stderr
, "x %f y %f z %f oow %f\n",
231 v
->v
.x
, v
->v
.y
, v
->v
.z
, v
->v
.rhw
);
232 fprintf(stderr
, "r %d g %d b %d a %d\n",
239 fprintf(stderr
, "\n");
242 #define DO_FALLBACK 0
244 /* Need to do clip loop at each triangle when mixing swrast and hw
245 * rendering. These functions are only used when mixed-mode rendering
248 static void tdfx_draw_quad( tdfxContextPtr fxMesa
,
254 /* fprintf(stderr, "%s\n", __FUNCTION__); */
255 BEGIN_CLIP_LOOP_LOCKED(fxMesa
) {
256 QUAD( v0
, v1
, v2
, v3
);
257 } END_CLIP_LOOP_LOCKED(fxMesa
);
260 static void tdfx_draw_triangle( tdfxContextPtr fxMesa
,
265 /* fprintf(stderr, "%s\n", __FUNCTION__); */
266 /* tdfx_print_vertex( fxMesa->glCtx, v0 ); */
267 /* tdfx_print_vertex( fxMesa->glCtx, v1 ); */
268 /* tdfx_print_vertex( fxMesa->glCtx, v2 ); */
269 BEGIN_CLIP_LOOP_LOCKED(fxMesa
) {
271 } END_CLIP_LOOP_LOCKED(fxMesa
);
274 static void tdfx_draw_line( tdfxContextPtr fxMesa
,
278 /* No support for wide lines (avoid wide/aa line fallback).
280 BEGIN_CLIP_LOOP_LOCKED(fxMesa
) {
282 } END_CLIP_LOOP_LOCKED(fxMesa
);
285 static void tdfx_draw_point( tdfxContextPtr fxMesa
,
288 /* No support for wide points.
290 BEGIN_CLIP_LOOP_LOCKED(fxMesa
) {
292 } END_CLIP_LOOP_LOCKED(fxMesa
);
298 #define TDFX_UNFILLED_BIT 0x1
299 #define TDFX_OFFSET_BIT 0x2
300 #define TDFX_TWOSIDE_BIT 0x4
301 #define TDFX_FLAT_BIT 0x8
302 #define TDFX_FALLBACK_BIT 0x10
303 #define TDFX_MAX_TRIFUNC 0x20
308 triangle_func triangle
;
310 } rast_tab
[TDFX_MAX_TRIFUNC
];
312 #define DO_FALLBACK (IND & TDFX_FALLBACK_BIT)
313 #define DO_OFFSET (IND & TDFX_OFFSET_BIT)
314 #define DO_UNFILLED (IND & TDFX_UNFILLED_BIT)
315 #define DO_TWOSIDE (IND & TDFX_TWOSIDE_BIT)
316 #define DO_FLAT (IND & TDFX_FLAT_BIT)
321 #define DO_FULL_QUAD 1
325 #define HAVE_HW_FLATSHADE 0
326 #define HAVE_BACK_COLORS 0
327 #define VERTEX tdfxVertex
330 #define TDFX_COLOR( dst, src ) \
338 #define DEPTH_SCALE 1.0
339 #define UNFILLED_TRI unfilled_tri
340 #define UNFILLED_QUAD unfilled_quad
341 #define VERT_X(_v) _v->v.x
342 #define VERT_Y(_v) _v->v.y
343 #define VERT_Z(_v) _v->v.z
344 #define AREA_IS_CCW( a ) (a < 0)
345 #define GET_VERTEX(e) (fxMesa->verts + (e<<fxMesa->vertex_stride_shift))
347 #define VERT_SET_RGBA( v, c ) TDFX_COLOR( v->ub4[coloroffset], c )
348 #define VERT_COPY_RGBA( v0, v1 ) v0->ui[coloroffset] = v1->ui[coloroffset]
349 #define VERT_SAVE_RGBA( idx ) color[idx] = v[idx]->ui[coloroffset]
350 #define VERT_RESTORE_RGBA( idx ) v[idx]->ui[coloroffset] = color[idx]
352 #define LOCAL_VARS(n) \
353 tdfxContextPtr fxMesa = TDFX_CONTEXT(ctx); \
354 GLuint coloroffset = (fxMesa->vertexFormat == \
355 TDFX_LAYOUT_TINY) ? 3 : 4; \
357 (void) color; (void)coloroffset
361 /***********************************************************************
362 * Functions to draw basic unfilled primitives *
363 ***********************************************************************/
365 #define RASTERIZE(x) if (fxMesa->raster_primitive != x) \
366 tdfxRasterPrimitive( ctx, x )
367 #define RENDER_PRIMITIVE fxMesa->render_primitive
368 #define IND TDFX_FALLBACK_BIT
370 #include "tnl_dd/t_dd_unfilled.h"
373 /***********************************************************************
374 * Functions to draw GL primitives *
375 ***********************************************************************/
379 #include "tnl_dd/t_dd_tritmp.h"
381 #define IND (TDFX_OFFSET_BIT)
382 #define TAG(x) x##_offset
383 #include "tnl_dd/t_dd_tritmp.h"
385 #define IND (TDFX_TWOSIDE_BIT)
386 #define TAG(x) x##_twoside
387 #include "tnl_dd/t_dd_tritmp.h"
389 #define IND (TDFX_TWOSIDE_BIT|TDFX_OFFSET_BIT)
390 #define TAG(x) x##_twoside_offset
391 #include "tnl_dd/t_dd_tritmp.h"
393 #define IND (TDFX_UNFILLED_BIT)
394 #define TAG(x) x##_unfilled
395 #include "tnl_dd/t_dd_tritmp.h"
397 #define IND (TDFX_OFFSET_BIT|TDFX_UNFILLED_BIT)
398 #define TAG(x) x##_offset_unfilled
399 #include "tnl_dd/t_dd_tritmp.h"
401 #define IND (TDFX_TWOSIDE_BIT|TDFX_UNFILLED_BIT)
402 #define TAG(x) x##_twoside_unfilled
403 #include "tnl_dd/t_dd_tritmp.h"
405 #define IND (TDFX_TWOSIDE_BIT|TDFX_OFFSET_BIT|TDFX_UNFILLED_BIT)
406 #define TAG(x) x##_twoside_offset_unfilled
407 #include "tnl_dd/t_dd_tritmp.h"
409 #define IND (TDFX_FALLBACK_BIT)
410 #define TAG(x) x##_fallback
411 #include "tnl_dd/t_dd_tritmp.h"
413 #define IND (TDFX_OFFSET_BIT|TDFX_FALLBACK_BIT)
414 #define TAG(x) x##_offset_fallback
415 #include "tnl_dd/t_dd_tritmp.h"
417 #define IND (TDFX_TWOSIDE_BIT|TDFX_FALLBACK_BIT)
418 #define TAG(x) x##_twoside_fallback
419 #include "tnl_dd/t_dd_tritmp.h"
421 #define IND (TDFX_TWOSIDE_BIT|TDFX_OFFSET_BIT|TDFX_FALLBACK_BIT)
422 #define TAG(x) x##_twoside_offset_fallback
423 #include "tnl_dd/t_dd_tritmp.h"
425 #define IND (TDFX_UNFILLED_BIT|TDFX_FALLBACK_BIT)
426 #define TAG(x) x##_unfilled_fallback
427 #include "tnl_dd/t_dd_tritmp.h"
429 #define IND (TDFX_OFFSET_BIT|TDFX_UNFILLED_BIT|TDFX_FALLBACK_BIT)
430 #define TAG(x) x##_offset_unfilled_fallback
431 #include "tnl_dd/t_dd_tritmp.h"
433 #define IND (TDFX_TWOSIDE_BIT|TDFX_UNFILLED_BIT|TDFX_FALLBACK_BIT)
434 #define TAG(x) x##_twoside_unfilled_fallback
435 #include "tnl_dd/t_dd_tritmp.h"
437 #define IND (TDFX_TWOSIDE_BIT|TDFX_OFFSET_BIT|TDFX_UNFILLED_BIT| \
439 #define TAG(x) x##_twoside_offset_unfilled_fallback
440 #include "tnl_dd/t_dd_tritmp.h"
443 /* Tdfx doesn't support provoking-vertex flat-shading?
445 #define IND (TDFX_FLAT_BIT)
446 #define TAG(x) x##_flat
447 #include "tnl_dd/t_dd_tritmp.h"
449 #define IND (TDFX_OFFSET_BIT|TDFX_FLAT_BIT)
450 #define TAG(x) x##_offset_flat
451 #include "tnl_dd/t_dd_tritmp.h"
453 #define IND (TDFX_TWOSIDE_BIT|TDFX_FLAT_BIT)
454 #define TAG(x) x##_twoside_flat
455 #include "tnl_dd/t_dd_tritmp.h"
457 #define IND (TDFX_TWOSIDE_BIT|TDFX_OFFSET_BIT|TDFX_FLAT_BIT)
458 #define TAG(x) x##_twoside_offset_flat
459 #include "tnl_dd/t_dd_tritmp.h"
461 #define IND (TDFX_UNFILLED_BIT|TDFX_FLAT_BIT)
462 #define TAG(x) x##_unfilled_flat
463 #include "tnl_dd/t_dd_tritmp.h"
465 #define IND (TDFX_OFFSET_BIT|TDFX_UNFILLED_BIT|TDFX_FLAT_BIT)
466 #define TAG(x) x##_offset_unfilled_flat
467 #include "tnl_dd/t_dd_tritmp.h"
469 #define IND (TDFX_TWOSIDE_BIT|TDFX_UNFILLED_BIT|TDFX_FLAT_BIT)
470 #define TAG(x) x##_twoside_unfilled_flat
471 #include "tnl_dd/t_dd_tritmp.h"
473 #define IND (TDFX_TWOSIDE_BIT|TDFX_OFFSET_BIT|TDFX_UNFILLED_BIT|TDFX_FLAT_BIT)
474 #define TAG(x) x##_twoside_offset_unfilled_flat
475 #include "tnl_dd/t_dd_tritmp.h"
477 #define IND (TDFX_FALLBACK_BIT|TDFX_FLAT_BIT)
478 #define TAG(x) x##_fallback_flat
479 #include "tnl_dd/t_dd_tritmp.h"
481 #define IND (TDFX_OFFSET_BIT|TDFX_FALLBACK_BIT|TDFX_FLAT_BIT)
482 #define TAG(x) x##_offset_fallback_flat
483 #include "tnl_dd/t_dd_tritmp.h"
485 #define IND (TDFX_TWOSIDE_BIT|TDFX_FALLBACK_BIT|TDFX_FLAT_BIT)
486 #define TAG(x) x##_twoside_fallback_flat
487 #include "tnl_dd/t_dd_tritmp.h"
489 #define IND (TDFX_TWOSIDE_BIT|TDFX_OFFSET_BIT|TDFX_FALLBACK_BIT|TDFX_FLAT_BIT)
490 #define TAG(x) x##_twoside_offset_fallback_flat
491 #include "tnl_dd/t_dd_tritmp.h"
493 #define IND (TDFX_UNFILLED_BIT|TDFX_FALLBACK_BIT|TDFX_FLAT_BIT)
494 #define TAG(x) x##_unfilled_fallback_flat
495 #include "tnl_dd/t_dd_tritmp.h"
497 #define IND (TDFX_OFFSET_BIT|TDFX_UNFILLED_BIT|TDFX_FALLBACK_BIT|TDFX_FLAT_BIT)
498 #define TAG(x) x##_offset_unfilled_fallback_flat
499 #include "tnl_dd/t_dd_tritmp.h"
501 #define IND (TDFX_TWOSIDE_BIT|TDFX_UNFILLED_BIT|TDFX_FALLBACK_BIT|TDFX_FLAT_BIT)
502 #define TAG(x) x##_twoside_unfilled_fallback_flat
503 #include "tnl_dd/t_dd_tritmp.h"
505 #define IND (TDFX_TWOSIDE_BIT|TDFX_OFFSET_BIT|TDFX_UNFILLED_BIT| \
506 TDFX_FALLBACK_BIT|TDFX_FLAT_BIT)
507 #define TAG(x) x##_twoside_offset_unfilled_fallback_flat
508 #include "tnl_dd/t_dd_tritmp.h"
511 static void init_rast_tab( void )
516 init_twoside_offset();
518 init_offset_unfilled();
519 init_twoside_unfilled();
520 init_twoside_offset_unfilled();
522 init_offset_fallback();
523 init_twoside_fallback();
524 init_twoside_offset_fallback();
525 init_unfilled_fallback();
526 init_offset_unfilled_fallback();
527 init_twoside_unfilled_fallback();
528 init_twoside_offset_unfilled_fallback();
533 init_twoside_offset_flat();
534 init_unfilled_flat();
535 init_offset_unfilled_flat();
536 init_twoside_unfilled_flat();
537 init_twoside_offset_unfilled_flat();
538 init_fallback_flat();
539 init_offset_fallback_flat();
540 init_twoside_fallback_flat();
541 init_twoside_offset_fallback_flat();
542 init_unfilled_fallback_flat();
543 init_offset_unfilled_fallback_flat();
544 init_twoside_unfilled_fallback_flat();
545 init_twoside_offset_unfilled_fallback_flat();
549 /**********************************************************************/
550 /* Render whole begin/end objects */
551 /**********************************************************************/
554 /* Accelerate vertex buffer rendering when renderindex == 0 and
555 * there is no clipping.
558 static void tdfx_render_vb_points( GLcontext
*ctx
,
563 tdfxContextPtr fxMesa
= TDFX_CONTEXT(ctx
);
564 GLuint shift
= fxMesa
->vertex_stride_shift
;
565 GLubyte
*fxVB
= fxMesa
->verts
+ (start
<< shift
);
566 int stride
= 1<<shift
;
571 /* Adjust point coords */
572 for (i
= start
, tmp
= fxVB
; i
< count
; i
++, tmp
+= stride
) {
573 ((tdfxVertexPtr
)tmp
)->v
.x
+= PNT_X_OFFSET
- TRI_X_OFFSET
;
574 ((tdfxVertexPtr
)tmp
)->v
.y
+= PNT_Y_OFFSET
- TRI_Y_OFFSET
;
577 fxMesa
->Glide
.grDrawVertexArrayContiguous( GR_POINTS
, count
-start
,
579 /* restore point coords */
580 for (i
= start
, tmp
= fxVB
; i
< count
; i
++, tmp
+= stride
) {
581 ((tdfxVertexPtr
)tmp
)->v
.x
-= PNT_X_OFFSET
- TRI_X_OFFSET
;
582 ((tdfxVertexPtr
)tmp
)->v
.y
-= PNT_Y_OFFSET
- TRI_Y_OFFSET
;
586 static void tdfx_render_vb_line_strip( GLcontext
*ctx
,
591 tdfxContextPtr fxMesa
= TDFX_CONTEXT(ctx
);
592 GLuint shift
= fxMesa
->vertex_stride_shift
;
593 GLubyte
*fxVB
= fxMesa
->verts
+ (start
<< shift
);
594 int stride
= 1<<shift
;
599 /* adjust line coords */
600 for (i
= start
, tmp
= fxVB
; i
< count
; i
++, tmp
+= stride
) {
601 ((tdfxVertexPtr
)tmp
)->v
.x
+= LINE_X_OFFSET
- TRI_X_OFFSET
;
602 ((tdfxVertexPtr
)tmp
)->v
.y
+= LINE_Y_OFFSET
- TRI_Y_OFFSET
;
605 fxMesa
->Glide
.grDrawVertexArrayContiguous( GR_LINE_STRIP
, count
-start
,
608 /* restore line coords */
609 for (i
= start
, tmp
= fxVB
; i
< count
; i
++, tmp
+= stride
) {
610 ((tdfxVertexPtr
)tmp
)->v
.x
-= LINE_X_OFFSET
- TRI_X_OFFSET
;
611 ((tdfxVertexPtr
)tmp
)->v
.y
-= LINE_Y_OFFSET
- TRI_Y_OFFSET
;
615 static void tdfx_render_vb_line_loop( GLcontext
*ctx
,
620 tdfxContextPtr fxMesa
= TDFX_CONTEXT(ctx
);
621 GLuint shift
= fxMesa
->vertex_stride_shift
;
622 GLubyte
*fxVB
= fxMesa
->verts
+ (start
<< shift
);
623 int stride
= 1<<shift
;
624 GLubyte
*tmp
, *tmp2
= fxVB
;
629 if (!(flags
& PRIM_BEGIN
)) {
630 fxVB
+= (1 << shift
);
634 /* adjust line coords */
635 for (i
= start
, tmp
= tmp2
; i
< count
; i
++, tmp
+= stride
) {
636 ((tdfxVertexPtr
)tmp
)->v
.x
+= LINE_X_OFFSET
- TRI_X_OFFSET
;
637 ((tdfxVertexPtr
)tmp
)->v
.y
+= LINE_Y_OFFSET
- TRI_Y_OFFSET
;
640 fxMesa
->Glide
.grDrawVertexArrayContiguous( GR_LINE_STRIP
, count
-j
,
643 if (flags
& PRIM_END
)
644 fxMesa
->Glide
.grDrawLine( fxMesa
->verts
+ ((count
- 1)<<shift
),
645 fxMesa
->verts
+ (start
<<shift
) );
647 /* restore line coords */
648 for (i
= start
, tmp
= tmp2
; i
< count
; i
++, tmp
+= stride
) {
649 ((tdfxVertexPtr
)tmp
)->v
.x
-= LINE_X_OFFSET
- TRI_X_OFFSET
;
650 ((tdfxVertexPtr
)tmp
)->v
.y
-= LINE_Y_OFFSET
- TRI_Y_OFFSET
;
654 static void tdfx_render_vb_lines( GLcontext
*ctx
,
659 tdfxContextPtr fxMesa
= TDFX_CONTEXT(ctx
);
660 GLuint shift
= fxMesa
->vertex_stride_shift
;
661 GLubyte
*fxVB
= fxMesa
->verts
+ (start
<< shift
);
662 int stride
= 1<<shift
;
667 /* adjust line coords */
668 for (i
= start
, tmp
= fxVB
; i
< count
; i
++, tmp
+= stride
) {
669 ((tdfxVertexPtr
)tmp
)->v
.x
+= LINE_X_OFFSET
- TRI_X_OFFSET
;
670 ((tdfxVertexPtr
)tmp
)->v
.y
+= LINE_Y_OFFSET
- TRI_Y_OFFSET
;
673 fxMesa
->Glide
.grDrawVertexArrayContiguous( GR_LINES
, count
-start
,
676 /* restore line coords */
677 for (i
= start
, tmp
= fxVB
; i
< count
; i
++, tmp
+= stride
) {
678 ((tdfxVertexPtr
)tmp
)->v
.x
-= LINE_X_OFFSET
- TRI_X_OFFSET
;
679 ((tdfxVertexPtr
)tmp
)->v
.y
-= LINE_Y_OFFSET
- TRI_Y_OFFSET
;
683 static void tdfx_render_vb_triangles( GLcontext
*ctx
,
688 tdfxContextPtr fxMesa
= TDFX_CONTEXT(ctx
);
689 GLuint shift
= fxMesa
->vertex_stride_shift
;
690 GLubyte
*fxVB
= fxMesa
->verts
+ (start
<< shift
);
693 fxMesa
->Glide
.grDrawVertexArrayContiguous( GR_TRIANGLES
, count
-start
,
698 static void tdfx_render_vb_tri_strip( GLcontext
*ctx
,
703 tdfxContextPtr fxMesa
= TDFX_CONTEXT(ctx
);
704 GLuint shift
= fxMesa
->vertex_stride_shift
;
705 GLubyte
*fxVB
= fxMesa
->verts
+ (start
<< shift
);
709 /* fprintf(stderr, "%s/%d\n", __FUNCTION__, 1<<shift); */
710 /* if(!prevLockLine) abort(); */
712 if (flags
& PRIM_PARITY
)
713 mode
= GR_TRIANGLE_STRIP_CONTINUE
;
715 mode
= GR_TRIANGLE_STRIP
;
717 fxMesa
->Glide
.grDrawVertexArrayContiguous( mode
, count
-start
,
722 static void tdfx_render_vb_tri_fan( GLcontext
*ctx
,
727 tdfxContextPtr fxMesa
= TDFX_CONTEXT(ctx
);
728 GLuint shift
= fxMesa
->vertex_stride_shift
;
729 GLubyte
*fxVB
= fxMesa
->verts
+ (start
<< shift
);
732 fxMesa
->Glide
.grDrawVertexArrayContiguous( GR_TRIANGLE_FAN
, count
-start
,
736 static void tdfx_render_vb_quads( GLcontext
*ctx
,
741 tdfxContextPtr fxMesa
= TDFX_CONTEXT(ctx
);
742 GLuint shift
= fxMesa
->vertex_stride_shift
;
743 GLubyte
*fxVB
= fxMesa
->verts
;
747 for (i
= start
; i
< count
-3 ; i
+= 4 ) {
748 #define VERT(x) (fxVB + ((x)<<shift))
749 fxMesa
->Glide
.grDrawTriangle( VERT(i
), VERT(i
+1), VERT(i
+3) );
750 fxMesa
->Glide
.grDrawTriangle( VERT(i
+1), VERT(i
+2), VERT(i
+3) );
755 static void tdfx_render_vb_quad_strip( GLcontext
*ctx
,
760 tdfxContextPtr fxMesa
= TDFX_CONTEXT(ctx
);
761 GLuint shift
= fxMesa
->vertex_stride_shift
;
762 GLubyte
*fxVB
= fxMesa
->verts
+ (start
<< shift
);
765 count
-= (count
-start
)&1;
767 fxMesa
->Glide
.grDrawVertexArrayContiguous( GR_TRIANGLE_STRIP
,
768 count
-start
, fxVB
, 1<<shift
);
771 static void tdfx_render_vb_poly( GLcontext
*ctx
,
776 tdfxContextPtr fxMesa
= TDFX_CONTEXT(ctx
);
777 GLuint shift
= fxMesa
->vertex_stride_shift
;
778 GLubyte
*fxVB
= fxMesa
->verts
+ (start
<< shift
);
781 fxMesa
->Glide
.grDrawVertexArrayContiguous( GR_POLYGON
, count
-start
,
785 static void tdfx_render_vb_noop( GLcontext
*ctx
,
790 (void) (ctx
&& start
&& count
&& flags
);
793 static void (*tdfx_render_tab_verts
[GL_POLYGON
+2])(GLcontext
*,
798 tdfx_render_vb_points
,
799 tdfx_render_vb_lines
,
800 tdfx_render_vb_line_loop
,
801 tdfx_render_vb_line_strip
,
802 tdfx_render_vb_triangles
,
803 tdfx_render_vb_tri_strip
,
804 tdfx_render_vb_tri_fan
,
805 tdfx_render_vb_quads
,
806 tdfx_render_vb_quad_strip
,
812 /**********************************************************************/
813 /* Render whole (indexed) begin/end objects */
814 /**********************************************************************/
817 #define VERT(x) (tdfxVertex *)(vertptr + ((x)<<vertshift))
819 #define RENDER_POINTS( start, count ) \
820 for ( ; start < count ; start++) \
821 fxMesa->Glide.grDrawPoint( VERT(ELT(start)) );
823 #define RENDER_LINE( v0, v1 ) \
824 fxMesa->Glide.grDrawLine( VERT(v0), VERT(v1) )
826 #define RENDER_TRI( v0, v1, v2 ) \
827 fxMesa->Glide.grDrawTriangle( VERT(v0), VERT(v1), VERT(v2) )
829 #define RENDER_QUAD( v0, v1, v2, v3 ) \
830 tdfx_draw_quad( fxMesa, VERT(v0), VERT(v1), VERT(v2), VERT(v3) )
832 #define INIT(x) tdfxRenderPrimitive( ctx, x )
836 tdfxContextPtr fxMesa = TDFX_CONTEXT(ctx); \
837 GLubyte *vertptr = (GLubyte *)fxMesa->verts; \
838 const GLuint vertshift = fxMesa->vertex_stride_shift; \
839 const GLuint * const elt = TNL_CONTEXT(ctx)->vb.Elts; \
842 #define RESET_STIPPLE
843 #define RESET_OCCLUSION
844 #define PRESERVE_VB_DEFS
846 /* Elts, no clipping.
850 #define TAG(x) tdfx_##x##_elts
851 #define ELT(x) elt[x]
852 #include "tnl_dd/t_dd_rendertmp.h"
856 /**********************************************************************/
857 /* Render clipped primitives */
858 /**********************************************************************/
862 static void tdfxRenderClippedPoly( GLcontext
*ctx
, const GLuint
*elts
,
865 tdfxContextPtr fxMesa
= TDFX_CONTEXT(ctx
);
866 TNLcontext
*tnl
= TNL_CONTEXT(ctx
);
867 struct vertex_buffer
*VB
= &tnl
->vb
;
868 GLuint prim
= fxMesa
->render_primitive
;
870 /* Render the new vertices as an unclipped polygon.
873 GLuint
*tmp
= VB
->Elts
;
874 VB
->Elts
= (GLuint
*)elts
;
875 tnl
->Driver
.Render
.PrimTabElts
[GL_POLYGON
]( ctx
, 0, n
, PRIM_BEGIN
|PRIM_END
);
879 /* Restore the render primitive
881 if (prim
!= GL_POLYGON
)
882 tnl
->Driver
.Render
.PrimitiveNotify( ctx
, prim
);
885 static void tdfxRenderClippedLine( GLcontext
*ctx
, GLuint ii
, GLuint jj
)
887 TNLcontext
*tnl
= TNL_CONTEXT(ctx
);
888 tnl
->Driver
.Render
.Line( ctx
, ii
, jj
);
891 static void tdfxFastRenderClippedPoly( GLcontext
*ctx
, const GLuint
*elts
,
894 tdfxContextPtr fxMesa
= TDFX_CONTEXT( ctx
);
895 GLubyte
*vertptr
= (GLubyte
*)fxMesa
->verts
;
896 const GLuint vertshift
= fxMesa
->vertex_stride_shift
;
897 const GLuint
*start
= (const GLuint
*)VERT(elts
[0]);
900 for (i
= 2 ; i
< n
; i
++) {
901 fxMesa
->Glide
.grDrawTriangle( VERT(elts
[i
-1]), VERT(elts
[i
]), start
);
905 /**********************************************************************/
906 /* Choose render functions */
907 /**********************************************************************/
910 #define POINT_FALLBACK (DD_POINT_SMOOTH)
911 #define LINE_FALLBACK (DD_LINE_STIPPLE)
912 #define TRI_FALLBACK (DD_TRI_SMOOTH)
913 #define ANY_FALLBACK_FLAGS (POINT_FALLBACK|LINE_FALLBACK|TRI_FALLBACK|DD_TRI_STIPPLE)
914 #define ANY_RASTER_FLAGS (DD_FLATSHADE|DD_TRI_LIGHT_TWOSIDE|DD_TRI_OFFSET| \
918 /* All state referenced below:
920 #define _TDFX_NEW_RENDERSTATE (_DD_NEW_POINT_SMOOTH | \
921 _DD_NEW_LINE_STIPPLE | \
922 _DD_NEW_TRI_SMOOTH | \
923 _DD_NEW_FLATSHADE | \
924 _DD_NEW_TRI_UNFILLED | \
925 _DD_NEW_TRI_LIGHT_TWOSIDE | \
926 _DD_NEW_TRI_OFFSET | \
927 _DD_NEW_TRI_STIPPLE | \
931 static void tdfxChooseRenderState(GLcontext
*ctx
)
933 TNLcontext
*tnl
= TNL_CONTEXT(ctx
);
934 tdfxContextPtr fxMesa
= TDFX_CONTEXT(ctx
);
935 GLuint flags
= ctx
->_TriangleCaps
;
939 fxMesa
->draw_point
= tdfx_draw_point
;
940 fxMesa
->draw_line
= tdfx_draw_line
;
941 fxMesa
->draw_triangle
= tdfx_draw_triangle
;
942 index
|= TDFX_FALLBACK_BIT
;
945 if (flags
& (ANY_FALLBACK_FLAGS
|ANY_RASTER_FLAGS
)) {
946 if (flags
& ANY_RASTER_FLAGS
) {
947 if (flags
& DD_TRI_LIGHT_TWOSIDE
) index
|= TDFX_TWOSIDE_BIT
;
948 if (flags
& DD_TRI_OFFSET
) index
|= TDFX_OFFSET_BIT
;
949 if (flags
& DD_TRI_UNFILLED
) index
|= TDFX_UNFILLED_BIT
;
950 if (flags
& DD_FLATSHADE
) index
|= TDFX_FLAT_BIT
;
953 fxMesa
->draw_point
= tdfx_draw_point
;
954 fxMesa
->draw_line
= tdfx_draw_line
;
955 fxMesa
->draw_triangle
= tdfx_draw_triangle
;
957 /* Hook in fallbacks for specific primitives.
959 * DD_TRI_UNFILLED is here because the unfilled_tri functions use
960 * fxMesa->draw_tri *always*, and thus can't use the multipass
961 * approach to cliprects.
964 if (flags
& (POINT_FALLBACK
|
970 if (flags
& POINT_FALLBACK
)
971 fxMesa
->draw_point
= tdfx_fallback_point
;
973 if (flags
& LINE_FALLBACK
)
974 fxMesa
->draw_line
= tdfx_fallback_line
;
976 if (flags
& TRI_FALLBACK
)
977 fxMesa
->draw_triangle
= tdfx_fallback_tri
;
979 if ((flags
& DD_TRI_STIPPLE
) && !fxMesa
->haveHwStipple
)
980 fxMesa
->draw_triangle
= tdfx_fallback_tri
;
982 index
|= TDFX_FALLBACK_BIT
;
986 if (fxMesa
->RenderIndex
!= index
) {
987 fxMesa
->RenderIndex
= index
;
989 tnl
->Driver
.Render
.Points
= rast_tab
[index
].points
;
990 tnl
->Driver
.Render
.Line
= rast_tab
[index
].line
;
991 tnl
->Driver
.Render
.Triangle
= rast_tab
[index
].triangle
;
992 tnl
->Driver
.Render
.Quad
= rast_tab
[index
].quad
;
995 tnl
->Driver
.Render
.PrimTabVerts
= tdfx_render_tab_verts
;
996 tnl
->Driver
.Render
.PrimTabElts
= tdfx_render_tab_elts
;
997 tnl
->Driver
.Render
.ClippedLine
= line
; /* from tritmp.h */
998 tnl
->Driver
.Render
.ClippedPolygon
= tdfxFastRenderClippedPoly
;
1000 tnl
->Driver
.Render
.PrimTabVerts
= _tnl_render_tab_verts
;
1001 tnl
->Driver
.Render
.PrimTabElts
= _tnl_render_tab_elts
;
1002 tnl
->Driver
.Render
.ClippedLine
= tdfxRenderClippedLine
;
1003 tnl
->Driver
.Render
.ClippedPolygon
= tdfxRenderClippedPoly
;
1008 /**********************************************************************/
1009 /* Use multipass rendering for cliprects */
1010 /**********************************************************************/
1014 /* TODO: Benchmark this.
1015 * TODO: Use single back-buffer cliprect where possible.
1016 * NOTE: <pass> starts at 1, not zero!
1018 static GLboolean
multipass_cliprect( GLcontext
*ctx
, GLuint pass
)
1020 tdfxContextPtr fxMesa
= TDFX_CONTEXT(ctx
);
1021 if (pass
>= fxMesa
->numClipRects
)
1024 fxMesa
->Glide
.grClipWindow(fxMesa
->pClipRects
[pass
].x1
,
1025 fxMesa
->screen_height
- fxMesa
->pClipRects
[pass
].y2
,
1026 fxMesa
->pClipRects
[pass
].x2
,
1027 fxMesa
->screen_height
- fxMesa
->pClipRects
[pass
].y1
);
1034 /**********************************************************************/
1035 /* Runtime render state and callbacks */
1036 /**********************************************************************/
1038 static void tdfxRunPipeline( GLcontext
*ctx
)
1040 tdfxContextPtr fxMesa
= TDFX_CONTEXT(ctx
);
1042 if (fxMesa
->new_state
) {
1043 tdfxDDUpdateHwState( ctx
);
1046 if (!fxMesa
->Fallback
&& fxMesa
->new_gl_state
) {
1047 if (fxMesa
->new_gl_state
& _TDFX_NEW_RASTERSETUP
)
1048 tdfxChooseVertexState( ctx
);
1050 if (fxMesa
->new_gl_state
& _TDFX_NEW_RENDERSTATE
)
1051 tdfxChooseRenderState( ctx
);
1053 fxMesa
->new_gl_state
= 0;
1056 _tnl_run_pipeline( ctx
);
1060 static void tdfxRenderStart( GLcontext
*ctx
)
1062 TNLcontext
*tnl
= TNL_CONTEXT(ctx
);
1063 tdfxContextPtr fxMesa
= TDFX_CONTEXT(ctx
);
1065 tdfxCheckTexSizes( ctx
);
1067 LOCK_HARDWARE(fxMesa
);
1069 /* Make sure vertex format changes get uploaded before we start
1070 * sending triangles.
1072 if (fxMesa
->dirty
) {
1073 tdfxEmitHwStateLocked( fxMesa
);
1076 if (fxMesa
->numClipRects
&& !(fxMesa
->RenderIndex
& TDFX_FALLBACK_BIT
)) {
1077 fxMesa
->Glide
.grClipWindow(fxMesa
->pClipRects
[0].x1
,
1078 fxMesa
->screen_height
- fxMesa
->pClipRects
[0].y2
,
1079 fxMesa
->pClipRects
[0].x2
,
1080 fxMesa
->screen_height
- fxMesa
->pClipRects
[0].y1
);
1081 if (fxMesa
->numClipRects
> 1)
1082 tnl
->Driver
.Render
.Multipass
= multipass_cliprect
;
1084 tnl
->Driver
.Render
.Multipass
= NULL
;
1087 tnl
->Driver
.Render
.Multipass
= NULL
;
1091 static GLenum reduced_prim
[GL_POLYGON
+1] = {
1106 /* Always called between RenderStart and RenderFinish --> We already
1109 static void tdfxRasterPrimitive( GLcontext
*ctx
, GLenum prim
)
1111 tdfxContextPtr fxMesa
= TDFX_CONTEXT( ctx
);
1113 FLUSH_BATCH( fxMesa
);
1115 fxMesa
->raster_primitive
= prim
;
1117 tdfxUpdateCull(ctx
);
1118 if ( fxMesa
->dirty
& TDFX_UPLOAD_CULL
) {
1119 fxMesa
->Glide
.grCullMode( fxMesa
->CullMode
);
1120 fxMesa
->dirty
&= ~TDFX_UPLOAD_CULL
;
1123 tdfxUpdateStipple(ctx
);
1124 if ( fxMesa
->dirty
& TDFX_UPLOAD_STIPPLE
) {
1125 fxMesa
->Glide
.grStipplePattern ( fxMesa
->Stipple
.Pattern
);
1126 fxMesa
->Glide
.grStippleMode ( fxMesa
->Stipple
.Mode
);
1127 fxMesa
->dirty
&= ~TDFX_UPLOAD_STIPPLE
;
1133 /* Determine the rasterized primitive when not drawing unfilled
1136 * Used only for the default render stage which always decomposes
1137 * primitives to trianges/lines/points. For the accelerated stage,
1138 * which renders strips as strips, the equivalent calculations are
1139 * performed in tdfx_render.c.
1141 static void tdfxRenderPrimitive( GLcontext
*ctx
, GLenum prim
)
1143 tdfxContextPtr fxMesa
= TDFX_CONTEXT(ctx
);
1144 GLuint rprim
= reduced_prim
[prim
];
1146 fxMesa
->render_primitive
= prim
;
1148 if (rprim
== GL_TRIANGLES
&& (ctx
->_TriangleCaps
& DD_TRI_UNFILLED
))
1151 if (fxMesa
->raster_primitive
!= rprim
) {
1152 tdfxRasterPrimitive( ctx
, rprim
);
1156 static void tdfxRenderFinish( GLcontext
*ctx
)
1158 tdfxContextPtr fxMesa
= TDFX_CONTEXT(ctx
);
1160 if (fxMesa
->RenderIndex
& TDFX_FALLBACK_BIT
)
1161 _swrast_flush( ctx
);
1163 UNLOCK_HARDWARE(fxMesa
);
1167 /**********************************************************************/
1168 /* Manage total rasterization fallbacks */
1169 /**********************************************************************/
1171 static char *fallbackStrings
[] = {
1172 "1D/3D Texture map",
1173 "glDrawBuffer(GL_FRONT_AND_BACK)",
1174 "Separate specular color",
1175 "glEnable/Disable(GL_STENCIL_TEST)",
1176 "glRenderMode(selection or feedback)",
1186 static char *getFallbackString(GLuint bit
)
1193 return fallbackStrings
[i
];
1197 void tdfxFallback( GLcontext
*ctx
, GLuint bit
, GLboolean mode
)
1199 TNLcontext
*tnl
= TNL_CONTEXT(ctx
);
1200 tdfxContextPtr fxMesa
= TDFX_CONTEXT(ctx
);
1201 GLuint oldfallback
= fxMesa
->Fallback
;
1204 fxMesa
->Fallback
|= bit
;
1205 if (oldfallback
== 0) {
1206 /*printf("Go to software rendering, bit = 0x%x\n", bit);*/
1207 FLUSH_BATCH(fxMesa
);
1208 _swsetup_Wakeup( ctx
);
1209 fxMesa
->RenderIndex
= ~0;
1210 if (fxMesa
->debugFallbacks
) {
1211 fprintf(stderr
, "Tdfx begin software fallback: 0x%x %s\n",
1212 bit
, getFallbackString(bit
));
1217 fxMesa
->Fallback
&= ~bit
;
1218 if (oldfallback
== bit
) {
1219 /*printf("Go to hardware rendering, bit = 0x%x\n", bit);*/
1220 _swrast_flush( ctx
);
1221 tnl
->Driver
.Render
.Start
= tdfxRenderStart
;
1222 tnl
->Driver
.Render
.PrimitiveNotify
= tdfxRenderPrimitive
;
1223 tnl
->Driver
.Render
.Finish
= tdfxRenderFinish
;
1224 tnl
->Driver
.Render
.BuildVertices
= tdfxBuildVertices
;
1225 fxMesa
->new_gl_state
|= (_TDFX_NEW_RENDERSTATE
|
1226 _TDFX_NEW_RASTERSETUP
);
1227 if (fxMesa
->debugFallbacks
) {
1228 fprintf(stderr
, "Tdfx end software fallback: 0x%x %s\n",
1229 bit
, getFallbackString(bit
));
1236 void tdfxDDInitTriFuncs( GLcontext
*ctx
)
1238 TNLcontext
*tnl
= TNL_CONTEXT(ctx
);
1239 tdfxContextPtr fxMesa
= TDFX_CONTEXT(ctx
);
1240 static int firsttime
= 1;
1247 fxMesa
->RenderIndex
= ~0;
1249 tnl
->Driver
.RunPipeline
= tdfxRunPipeline
;
1250 tnl
->Driver
.Render
.Start
= tdfxRenderStart
;
1251 tnl
->Driver
.Render
.Finish
= tdfxRenderFinish
;
1252 tnl
->Driver
.Render
.PrimitiveNotify
= tdfxRenderPrimitive
;
1253 tnl
->Driver
.Render
.ResetLineStipple
= _swrast_ResetLineStipple
;
1254 tnl
->Driver
.Render
.BuildVertices
= tdfxBuildVertices
;
1255 tnl
->Driver
.Render
.Multipass
= NULL
;
1257 (void) tdfx_print_vertex
;