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", (void *)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 mode
= GR_TRIANGLE_STRIP
;
714 fxMesa
->Glide
.grDrawVertexArrayContiguous( mode
, count
-start
,
719 static void tdfx_render_vb_tri_fan( GLcontext
*ctx
,
724 tdfxContextPtr fxMesa
= TDFX_CONTEXT(ctx
);
725 GLuint shift
= fxMesa
->vertex_stride_shift
;
726 GLubyte
*fxVB
= fxMesa
->verts
+ (start
<< shift
);
729 fxMesa
->Glide
.grDrawVertexArrayContiguous( GR_TRIANGLE_FAN
, count
-start
,
733 static void tdfx_render_vb_quads( GLcontext
*ctx
,
738 tdfxContextPtr fxMesa
= TDFX_CONTEXT(ctx
);
739 GLuint shift
= fxMesa
->vertex_stride_shift
;
740 GLubyte
*fxVB
= fxMesa
->verts
;
744 for (i
= start
; i
< count
-3 ; i
+= 4 ) {
745 #define VERT(x) (fxVB + ((x)<<shift))
746 fxMesa
->Glide
.grDrawTriangle( VERT(i
), VERT(i
+1), VERT(i
+3) );
747 fxMesa
->Glide
.grDrawTriangle( VERT(i
+1), VERT(i
+2), VERT(i
+3) );
752 static void tdfx_render_vb_quad_strip( GLcontext
*ctx
,
757 tdfxContextPtr fxMesa
= TDFX_CONTEXT(ctx
);
758 GLuint shift
= fxMesa
->vertex_stride_shift
;
759 GLubyte
*fxVB
= fxMesa
->verts
+ (start
<< shift
);
762 count
-= (count
-start
)&1;
764 fxMesa
->Glide
.grDrawVertexArrayContiguous( GR_TRIANGLE_STRIP
,
765 count
-start
, fxVB
, 1<<shift
);
768 static void tdfx_render_vb_poly( GLcontext
*ctx
,
773 tdfxContextPtr fxMesa
= TDFX_CONTEXT(ctx
);
774 GLuint shift
= fxMesa
->vertex_stride_shift
;
775 GLubyte
*fxVB
= fxMesa
->verts
+ (start
<< shift
);
778 fxMesa
->Glide
.grDrawVertexArrayContiguous( GR_POLYGON
, count
-start
,
782 static void tdfx_render_vb_noop( GLcontext
*ctx
,
787 (void) (ctx
&& start
&& count
&& flags
);
790 static void (*tdfx_render_tab_verts
[GL_POLYGON
+2])(GLcontext
*,
795 tdfx_render_vb_points
,
796 tdfx_render_vb_lines
,
797 tdfx_render_vb_line_loop
,
798 tdfx_render_vb_line_strip
,
799 tdfx_render_vb_triangles
,
800 tdfx_render_vb_tri_strip
,
801 tdfx_render_vb_tri_fan
,
802 tdfx_render_vb_quads
,
803 tdfx_render_vb_quad_strip
,
809 /**********************************************************************/
810 /* Render whole (indexed) begin/end objects */
811 /**********************************************************************/
814 #define VERT(x) (tdfxVertex *)(vertptr + ((x)<<vertshift))
816 #define RENDER_POINTS( start, count ) \
817 for ( ; start < count ; start++) \
818 fxMesa->Glide.grDrawPoint( VERT(ELT(start)) );
820 #define RENDER_LINE( v0, v1 ) \
821 fxMesa->Glide.grDrawLine( VERT(v0), VERT(v1) )
823 #define RENDER_TRI( v0, v1, v2 ) \
824 fxMesa->Glide.grDrawTriangle( VERT(v0), VERT(v1), VERT(v2) )
826 #define RENDER_QUAD( v0, v1, v2, v3 ) \
827 tdfx_draw_quad( fxMesa, VERT(v0), VERT(v1), VERT(v2), VERT(v3) )
829 #define INIT(x) tdfxRenderPrimitive( ctx, x )
833 tdfxContextPtr fxMesa = TDFX_CONTEXT(ctx); \
834 GLubyte *vertptr = (GLubyte *)fxMesa->verts; \
835 const GLuint vertshift = fxMesa->vertex_stride_shift; \
836 const GLuint * const elt = TNL_CONTEXT(ctx)->vb.Elts; \
839 #define RESET_STIPPLE
840 #define RESET_OCCLUSION
841 #define PRESERVE_VB_DEFS
843 /* Elts, no clipping.
847 #define TAG(x) tdfx_##x##_elts
848 #define ELT(x) elt[x]
849 #include "tnl_dd/t_dd_rendertmp.h"
853 /**********************************************************************/
854 /* Render clipped primitives */
855 /**********************************************************************/
859 static void tdfxRenderClippedPoly( GLcontext
*ctx
, const GLuint
*elts
,
862 tdfxContextPtr fxMesa
= TDFX_CONTEXT(ctx
);
863 TNLcontext
*tnl
= TNL_CONTEXT(ctx
);
864 struct vertex_buffer
*VB
= &tnl
->vb
;
865 GLuint prim
= fxMesa
->render_primitive
;
867 /* Render the new vertices as an unclipped polygon.
870 GLuint
*tmp
= VB
->Elts
;
871 VB
->Elts
= (GLuint
*)elts
;
872 tnl
->Driver
.Render
.PrimTabElts
[GL_POLYGON
]( ctx
, 0, n
, PRIM_BEGIN
|PRIM_END
);
876 /* Restore the render primitive
878 if (prim
!= GL_POLYGON
)
879 tnl
->Driver
.Render
.PrimitiveNotify( ctx
, prim
);
882 static void tdfxRenderClippedLine( GLcontext
*ctx
, GLuint ii
, GLuint jj
)
884 TNLcontext
*tnl
= TNL_CONTEXT(ctx
);
885 tnl
->Driver
.Render
.Line( ctx
, ii
, jj
);
888 static void tdfxFastRenderClippedPoly( GLcontext
*ctx
, const GLuint
*elts
,
891 tdfxContextPtr fxMesa
= TDFX_CONTEXT( ctx
);
892 GLubyte
*vertptr
= (GLubyte
*)fxMesa
->verts
;
893 const GLuint vertshift
= fxMesa
->vertex_stride_shift
;
894 const GLuint
*start
= (const GLuint
*)VERT(elts
[0]);
897 for (i
= 2 ; i
< n
; i
++) {
898 fxMesa
->Glide
.grDrawTriangle( VERT(elts
[i
-1]), VERT(elts
[i
]), start
);
902 /**********************************************************************/
903 /* Choose render functions */
904 /**********************************************************************/
907 #define POINT_FALLBACK (DD_POINT_SMOOTH)
908 #define LINE_FALLBACK (DD_LINE_STIPPLE)
909 #define TRI_FALLBACK (DD_TRI_SMOOTH)
910 #define ANY_FALLBACK_FLAGS (POINT_FALLBACK|LINE_FALLBACK|TRI_FALLBACK|DD_TRI_STIPPLE)
911 #define ANY_RASTER_FLAGS (DD_FLATSHADE|DD_TRI_LIGHT_TWOSIDE|DD_TRI_OFFSET| \
915 /* All state referenced below:
917 #define _TDFX_NEW_RENDERSTATE (_DD_NEW_POINT_SMOOTH | \
918 _DD_NEW_LINE_STIPPLE | \
919 _DD_NEW_TRI_SMOOTH | \
920 _DD_NEW_FLATSHADE | \
921 _DD_NEW_TRI_UNFILLED | \
922 _DD_NEW_TRI_LIGHT_TWOSIDE | \
923 _DD_NEW_TRI_OFFSET | \
924 _DD_NEW_TRI_STIPPLE | \
928 static void tdfxChooseRenderState(GLcontext
*ctx
)
930 TNLcontext
*tnl
= TNL_CONTEXT(ctx
);
931 tdfxContextPtr fxMesa
= TDFX_CONTEXT(ctx
);
932 GLuint flags
= ctx
->_TriangleCaps
;
936 fxMesa
->draw_point
= tdfx_draw_point
;
937 fxMesa
->draw_line
= tdfx_draw_line
;
938 fxMesa
->draw_triangle
= tdfx_draw_triangle
;
939 index
|= TDFX_FALLBACK_BIT
;
942 if (flags
& (ANY_FALLBACK_FLAGS
|ANY_RASTER_FLAGS
)) {
943 if (flags
& ANY_RASTER_FLAGS
) {
944 if (flags
& DD_TRI_LIGHT_TWOSIDE
) index
|= TDFX_TWOSIDE_BIT
;
945 if (flags
& DD_TRI_OFFSET
) index
|= TDFX_OFFSET_BIT
;
946 if (flags
& DD_TRI_UNFILLED
) index
|= TDFX_UNFILLED_BIT
;
947 if (flags
& DD_FLATSHADE
) index
|= TDFX_FLAT_BIT
;
950 fxMesa
->draw_point
= tdfx_draw_point
;
951 fxMesa
->draw_line
= tdfx_draw_line
;
952 fxMesa
->draw_triangle
= tdfx_draw_triangle
;
954 /* Hook in fallbacks for specific primitives.
956 * DD_TRI_UNFILLED is here because the unfilled_tri functions use
957 * fxMesa->draw_tri *always*, and thus can't use the multipass
958 * approach to cliprects.
961 if (flags
& (POINT_FALLBACK
|
967 if (flags
& POINT_FALLBACK
)
968 fxMesa
->draw_point
= tdfx_fallback_point
;
970 if (flags
& LINE_FALLBACK
)
971 fxMesa
->draw_line
= tdfx_fallback_line
;
973 if (flags
& TRI_FALLBACK
)
974 fxMesa
->draw_triangle
= tdfx_fallback_tri
;
976 if ((flags
& DD_TRI_STIPPLE
) && !fxMesa
->haveHwStipple
)
977 fxMesa
->draw_triangle
= tdfx_fallback_tri
;
979 index
|= TDFX_FALLBACK_BIT
;
983 if (fxMesa
->RenderIndex
!= index
) {
984 fxMesa
->RenderIndex
= index
;
986 tnl
->Driver
.Render
.Points
= rast_tab
[index
].points
;
987 tnl
->Driver
.Render
.Line
= rast_tab
[index
].line
;
988 tnl
->Driver
.Render
.Triangle
= rast_tab
[index
].triangle
;
989 tnl
->Driver
.Render
.Quad
= rast_tab
[index
].quad
;
992 tnl
->Driver
.Render
.PrimTabVerts
= tdfx_render_tab_verts
;
993 tnl
->Driver
.Render
.PrimTabElts
= tdfx_render_tab_elts
;
994 tnl
->Driver
.Render
.ClippedLine
= line
; /* from tritmp.h */
995 tnl
->Driver
.Render
.ClippedPolygon
= tdfxFastRenderClippedPoly
;
997 tnl
->Driver
.Render
.PrimTabVerts
= _tnl_render_tab_verts
;
998 tnl
->Driver
.Render
.PrimTabElts
= _tnl_render_tab_elts
;
999 tnl
->Driver
.Render
.ClippedLine
= tdfxRenderClippedLine
;
1000 tnl
->Driver
.Render
.ClippedPolygon
= tdfxRenderClippedPoly
;
1005 /**********************************************************************/
1006 /* Use multipass rendering for cliprects */
1007 /**********************************************************************/
1011 /* TODO: Benchmark this.
1012 * TODO: Use single back-buffer cliprect where possible.
1013 * NOTE: <pass> starts at 1, not zero!
1015 static GLboolean
multipass_cliprect( GLcontext
*ctx
, GLuint pass
)
1017 tdfxContextPtr fxMesa
= TDFX_CONTEXT(ctx
);
1018 if (pass
>= fxMesa
->numClipRects
)
1021 fxMesa
->Glide
.grClipWindow(fxMesa
->pClipRects
[pass
].x1
,
1022 fxMesa
->screen_height
- fxMesa
->pClipRects
[pass
].y2
,
1023 fxMesa
->pClipRects
[pass
].x2
,
1024 fxMesa
->screen_height
- fxMesa
->pClipRects
[pass
].y1
);
1031 /**********************************************************************/
1032 /* Runtime render state and callbacks */
1033 /**********************************************************************/
1035 static void tdfxRunPipeline( GLcontext
*ctx
)
1037 tdfxContextPtr fxMesa
= TDFX_CONTEXT(ctx
);
1039 if (fxMesa
->new_state
) {
1040 tdfxDDUpdateHwState( ctx
);
1043 if (!fxMesa
->Fallback
&& fxMesa
->new_gl_state
) {
1044 if (fxMesa
->new_gl_state
& _TDFX_NEW_RASTERSETUP
)
1045 tdfxChooseVertexState( ctx
);
1047 if (fxMesa
->new_gl_state
& _TDFX_NEW_RENDERSTATE
)
1048 tdfxChooseRenderState( ctx
);
1050 fxMesa
->new_gl_state
= 0;
1053 _tnl_run_pipeline( ctx
);
1057 static void tdfxRenderStart( GLcontext
*ctx
)
1059 TNLcontext
*tnl
= TNL_CONTEXT(ctx
);
1060 tdfxContextPtr fxMesa
= TDFX_CONTEXT(ctx
);
1062 tdfxCheckTexSizes( ctx
);
1064 LOCK_HARDWARE(fxMesa
);
1066 /* Make sure vertex format changes get uploaded before we start
1067 * sending triangles.
1069 if (fxMesa
->dirty
) {
1070 tdfxEmitHwStateLocked( fxMesa
);
1073 if (fxMesa
->numClipRects
&& !(fxMesa
->RenderIndex
& TDFX_FALLBACK_BIT
)) {
1074 fxMesa
->Glide
.grClipWindow(fxMesa
->pClipRects
[0].x1
,
1075 fxMesa
->screen_height
- fxMesa
->pClipRects
[0].y2
,
1076 fxMesa
->pClipRects
[0].x2
,
1077 fxMesa
->screen_height
- fxMesa
->pClipRects
[0].y1
);
1078 if (fxMesa
->numClipRects
> 1)
1079 tnl
->Driver
.Render
.Multipass
= multipass_cliprect
;
1081 tnl
->Driver
.Render
.Multipass
= NULL
;
1084 tnl
->Driver
.Render
.Multipass
= NULL
;
1088 static GLenum reduced_prim
[GL_POLYGON
+1] = {
1103 /* Always called between RenderStart and RenderFinish --> We already
1106 static void tdfxRasterPrimitive( GLcontext
*ctx
, GLenum prim
)
1108 tdfxContextPtr fxMesa
= TDFX_CONTEXT( ctx
);
1110 FLUSH_BATCH( fxMesa
);
1112 fxMesa
->raster_primitive
= prim
;
1114 tdfxUpdateCull(ctx
);
1115 if ( fxMesa
->dirty
& TDFX_UPLOAD_CULL
) {
1116 fxMesa
->Glide
.grCullMode( fxMesa
->CullMode
);
1117 fxMesa
->dirty
&= ~TDFX_UPLOAD_CULL
;
1120 tdfxUpdateStipple(ctx
);
1121 if ( fxMesa
->dirty
& TDFX_UPLOAD_STIPPLE
) {
1122 fxMesa
->Glide
.grStipplePattern ( fxMesa
->Stipple
.Pattern
);
1123 fxMesa
->Glide
.grStippleMode ( fxMesa
->Stipple
.Mode
);
1124 fxMesa
->dirty
&= ~TDFX_UPLOAD_STIPPLE
;
1130 /* Determine the rasterized primitive when not drawing unfilled
1133 * Used only for the default render stage which always decomposes
1134 * primitives to trianges/lines/points. For the accelerated stage,
1135 * which renders strips as strips, the equivalent calculations are
1136 * performed in tdfx_render.c.
1138 static void tdfxRenderPrimitive( GLcontext
*ctx
, GLenum prim
)
1140 tdfxContextPtr fxMesa
= TDFX_CONTEXT(ctx
);
1141 GLuint rprim
= reduced_prim
[prim
];
1143 fxMesa
->render_primitive
= prim
;
1145 if (rprim
== GL_TRIANGLES
&& (ctx
->_TriangleCaps
& DD_TRI_UNFILLED
))
1148 if (fxMesa
->raster_primitive
!= rprim
) {
1149 tdfxRasterPrimitive( ctx
, rprim
);
1153 static void tdfxRenderFinish( GLcontext
*ctx
)
1155 tdfxContextPtr fxMesa
= TDFX_CONTEXT(ctx
);
1157 if (fxMesa
->RenderIndex
& TDFX_FALLBACK_BIT
)
1158 _swrast_flush( ctx
);
1160 UNLOCK_HARDWARE(fxMesa
);
1164 /**********************************************************************/
1165 /* Manage total rasterization fallbacks */
1166 /**********************************************************************/
1168 static char *fallbackStrings
[] = {
1169 "1D/3D Texture map",
1170 "glDrawBuffer(GL_FRONT_AND_BACK)",
1171 "Separate specular color",
1172 "glEnable/Disable(GL_STENCIL_TEST)",
1173 "glRenderMode(selection or feedback)",
1183 static char *getFallbackString(GLuint bit
)
1190 return fallbackStrings
[i
];
1194 void tdfxFallback( GLcontext
*ctx
, GLuint bit
, GLboolean mode
)
1196 TNLcontext
*tnl
= TNL_CONTEXT(ctx
);
1197 tdfxContextPtr fxMesa
= TDFX_CONTEXT(ctx
);
1198 GLuint oldfallback
= fxMesa
->Fallback
;
1201 fxMesa
->Fallback
|= bit
;
1202 if (oldfallback
== 0) {
1203 /*printf("Go to software rendering, bit = 0x%x\n", bit);*/
1204 FLUSH_BATCH(fxMesa
);
1205 _swsetup_Wakeup( ctx
);
1206 fxMesa
->RenderIndex
= ~0;
1207 if (fxMesa
->debugFallbacks
) {
1208 fprintf(stderr
, "Tdfx begin software fallback: 0x%x %s\n",
1209 bit
, getFallbackString(bit
));
1214 fxMesa
->Fallback
&= ~bit
;
1215 if (oldfallback
== bit
) {
1216 /*printf("Go to hardware rendering, bit = 0x%x\n", bit);*/
1217 _swrast_flush( ctx
);
1218 tnl
->Driver
.Render
.Start
= tdfxRenderStart
;
1219 tnl
->Driver
.Render
.PrimitiveNotify
= tdfxRenderPrimitive
;
1220 tnl
->Driver
.Render
.Finish
= tdfxRenderFinish
;
1221 tnl
->Driver
.Render
.BuildVertices
= tdfxBuildVertices
;
1222 fxMesa
->new_gl_state
|= (_TDFX_NEW_RENDERSTATE
|
1223 _TDFX_NEW_RASTERSETUP
);
1224 if (fxMesa
->debugFallbacks
) {
1225 fprintf(stderr
, "Tdfx end software fallback: 0x%x %s\n",
1226 bit
, getFallbackString(bit
));
1233 void tdfxDDInitTriFuncs( GLcontext
*ctx
)
1235 TNLcontext
*tnl
= TNL_CONTEXT(ctx
);
1236 tdfxContextPtr fxMesa
= TDFX_CONTEXT(ctx
);
1237 static int firsttime
= 1;
1244 fxMesa
->RenderIndex
= ~0;
1246 tnl
->Driver
.RunPipeline
= tdfxRunPipeline
;
1247 tnl
->Driver
.Render
.Start
= tdfxRenderStart
;
1248 tnl
->Driver
.Render
.Finish
= tdfxRenderFinish
;
1249 tnl
->Driver
.Render
.PrimitiveNotify
= tdfxRenderPrimitive
;
1250 tnl
->Driver
.Render
.ResetLineStipple
= _swrast_ResetLineStipple
;
1251 tnl
->Driver
.Render
.BuildVertices
= tdfxBuildVertices
;
1252 tnl
->Driver
.Render
.Multipass
= NULL
;
1254 (void) tdfx_print_vertex
;