2 * Mesa 3-D graphics library
5 * Copyright (C) 1999-2005 Brian Paul All Rights Reserved.
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 shall be included
15 * in all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
21 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 * Keith Whitwell <keith@tungstengraphics.com>
29 /* Template for building functions to plug into the driver interface
31 * ctx->Driver.QuadFunc
32 * ctx->Driver.TriangleFunc
33 * ctx->Driver.LineFunc
34 * ctx->Driver.PointsFunc
36 * DO_TWOSIDE: Plug back-color values from the VB into backfacing triangles,
37 * and restore vertices afterwards.
38 * DO_OFFSET: Calculate offset for triangles and adjust vertices. Restore
39 * vertices after rendering.
40 * DO_FLAT: For hardware without native flatshading, copy provoking colors
41 * into the other vertices. Restore after rendering.
42 * DO_UNFILLED: Decompose triangles to lines and points where appropriate.
43 * DO_TWOSTENCIL:Gross hack for two-sided stencil.
45 * HAVE_SPEC: Vertices have secondary rgba values.
47 * VERT_X(v): Alias for vertex x value.
48 * VERT_Y(v): Alias for vertex y value.
49 * VERT_Z(v): Alias for vertex z value.
50 * DEPTH_SCALE: Scale for constant offset.
51 * REVERSE_DEPTH: Viewport depth range reversed.
53 * VERTEX: Hardware vertex type.
54 * GET_VERTEX(n): Retreive vertex with index n.
55 * AREA_IS_CCW(a): Return true if triangle with signed area a is ccw.
57 * VERT_SET_RGBA: Assign vertex rgba from VB color.
58 * VERT_COPY_RGBA: Copy vertex rgba another vertex.
59 * VERT_SAVE_RGBA: Save vertex rgba to a local variable.
60 * VERT_RESTORE_RGBA: Restore vertex rgba from a local variable.
61 * --> Similar for SPEC.
63 * LOCAL_VARS(n): (At least) define local vars for save/restore rgba.
68 #define VERT_SET_RGBA( v, c )
72 #define VERT_SET_SPEC( v, c ) (void) c
73 #define VERT_COPY_SPEC( v0, v1 )
74 #define VERT_SAVE_SPEC( idx )
75 #define VERT_RESTORE_SPEC( idx )
77 #define VERT_COPY_SPEC1( v )
81 #define VERT_SET_SPEC( v, c )
86 #define VERT_COPY_SPEC1( v )
87 #define VERT_COPY_RGBA1( v )
90 #ifndef INSANE_VERTICES
91 #define VERT_SET_Z(v,val) VERT_Z(v) = val
92 #define VERT_Z_ADD(v,val) VERT_Z(v) += val
96 #define REVERSE_DEPTH 0
99 /* disable twostencil for un-aware drivers */
100 #ifndef HAVE_STENCIL_TWOSIDE
101 #define HAVE_STENCIL_TWOSIDE 0
103 #ifndef DO_TWOSTENCIL
104 #define DO_TWOSTENCIL 0
106 #ifndef SETUP_STENCIL
107 #define SETUP_STENCIL(f)
109 #ifndef UNSET_STENCIL
110 #define UNSET_STENCIL(f)
114 static void TAG(triangle
)( GLcontext
*ctx
, GLuint e0
, GLuint e1
, GLuint e2
)
116 struct vertex_buffer
*VB
= &TNL_CONTEXT( ctx
)->vb
;
119 GLfloat z
[3] = { 0 };
120 GLenum mode
= GL_FILL
;
124 /* fprintf(stderr, "%s\n", __FUNCTION__); */
126 v
[0] = (VERTEX
*)GET_VERTEX(e0
);
127 v
[1] = (VERTEX
*)GET_VERTEX(e1
);
128 v
[2] = (VERTEX
*)GET_VERTEX(e2
);
130 if (DO_TWOSIDE
|| DO_OFFSET
|| DO_UNFILLED
|| DO_TWOSTENCIL
)
132 GLfloat ex
= VERT_X(v
[0]) - VERT_X(v
[2]);
133 GLfloat ey
= VERT_Y(v
[0]) - VERT_Y(v
[2]);
134 GLfloat fx
= VERT_X(v
[1]) - VERT_X(v
[2]);
135 GLfloat fy
= VERT_Y(v
[1]) - VERT_Y(v
[2]);
136 GLfloat cc
= ex
*fy
- ey
*fx
;
138 if (DO_TWOSIDE
|| DO_UNFILLED
|| DO_TWOSTENCIL
)
140 facing
= AREA_IS_CCW( cc
) ^ ctx
->Polygon
._FrontBit
;
144 mode
= ctx
->Polygon
.BackMode
;
145 if (ctx
->Polygon
.CullFlag
&&
146 ctx
->Polygon
.CullFaceMode
!= GL_FRONT
) {
150 mode
= ctx
->Polygon
.FrontMode
;
151 if (ctx
->Polygon
.CullFlag
&&
152 ctx
->Polygon
.CullFaceMode
!= GL_BACK
) {
158 if (DO_TWOSIDE
&& facing
== 1)
160 if (HAVE_BACK_COLORS
) {
164 VERT_COPY_RGBA1( v
[0] );
165 VERT_COPY_RGBA1( v
[1] );
168 VERT_COPY_RGBA1( v
[2] );
173 VERT_COPY_SPEC1( v
[0] );
174 VERT_COPY_SPEC1( v
[1] );
177 VERT_COPY_SPEC1( v
[2] );
181 GLfloat (*vbcolor
)[4] = VB
->BackfaceColorPtr
->data
;
190 if (VB
->BackfaceColorPtr
->stride
) {
191 ASSERT(VB
->BackfaceColorPtr
->stride
== 4*sizeof(GLfloat
));
194 VERT_SET_RGBA( v
[0], vbcolor
[e0
] );
195 VERT_SET_RGBA( v
[1], vbcolor
[e1
] );
197 VERT_SET_RGBA( v
[2], vbcolor
[e2
] );
201 VERT_SET_RGBA( v
[0], vbcolor
[0] );
202 VERT_SET_RGBA( v
[1], vbcolor
[0] );
204 VERT_SET_RGBA( v
[2], vbcolor
[0] );
207 if (HAVE_SPEC
&& VB
->BackfaceSecondaryColorPtr
) {
208 GLfloat (*vbspec
)[4] = VB
->BackfaceSecondaryColorPtr
->data
;
209 ASSERT(VB
->BackfaceSecondaryColorPtr
->stride
== 4*sizeof(GLfloat
));
214 VERT_SET_SPEC( v
[0], vbspec
[e0
] );
215 VERT_SET_SPEC( v
[1], vbspec
[e1
] );
218 VERT_SET_SPEC( v
[2], vbspec
[e2
] );
227 offset
= ctx
->Polygon
.OffsetUnits
* DEPTH_SCALE
;
231 if (cc
* cc
> 1e-16) {
232 GLfloat ic
= 1.0 / cc
;
233 GLfloat ez
= z
[0] - z
[2];
234 GLfloat fz
= z
[1] - z
[2];
235 GLfloat a
= ey
*fz
- ez
*fy
;
236 GLfloat b
= ez
*fx
- ex
*fz
;
239 if ( ac
< 0.0f
) ac
= -ac
;
240 if ( bc
< 0.0f
) bc
= -bc
;
241 offset
+= MAX2( ac
, bc
) * ctx
->Polygon
.OffsetFactor
/ ctx
->DrawBuffer
->_MRD
;
243 offset
*= ctx
->DrawBuffer
->_MRD
* (REVERSE_DEPTH
? -1.0 : 1.0);
250 VERT_COPY_RGBA( v
[0], v
[2] );
251 VERT_COPY_RGBA( v
[1], v
[2] );
252 if (HAVE_SPEC
&& VB
->AttribPtr
[_TNL_ATTRIB_COLOR1
]) {
255 VERT_COPY_SPEC( v
[0], v
[2] );
256 VERT_COPY_SPEC( v
[1], v
[2] );
260 if (mode
== GL_POINT
) {
261 if (DO_OFFSET
&& ctx
->Polygon
.OffsetPoint
) {
262 VERT_Z_ADD(v
[0], offset
);
263 VERT_Z_ADD(v
[1], offset
);
264 VERT_Z_ADD(v
[2], offset
);
266 if (DO_TWOSTENCIL
&& !HAVE_STENCIL_TWOSIDE
&& ctx
->Stencil
.TestTwoSide
) {
267 SETUP_STENCIL(facing
);
268 UNFILLED_TRI( ctx
, GL_POINT
, e0
, e1
, e2
);
269 UNSET_STENCIL(facing
);
271 UNFILLED_TRI( ctx
, GL_POINT
, e0
, e1
, e2
);
273 } else if (mode
== GL_LINE
) {
274 if (DO_OFFSET
&& ctx
->Polygon
.OffsetLine
) {
275 VERT_Z_ADD(v
[0], offset
);
276 VERT_Z_ADD(v
[1], offset
);
277 VERT_Z_ADD(v
[2], offset
);
279 if (DO_TWOSTENCIL
&& !HAVE_STENCIL_TWOSIDE
&& ctx
->Stencil
.TestTwoSide
) {
280 SETUP_STENCIL(facing
);
281 UNFILLED_TRI( ctx
, GL_LINE
, e0
, e1
, e2
);
282 UNSET_STENCIL(facing
);
284 UNFILLED_TRI( ctx
, GL_LINE
, e0
, e1
, e2
);
287 if (DO_OFFSET
&& ctx
->Polygon
.OffsetFill
) {
288 VERT_Z_ADD(v
[0], offset
);
289 VERT_Z_ADD(v
[1], offset
);
290 VERT_Z_ADD(v
[2], offset
);
293 RASTERIZE( GL_TRIANGLES
);
294 if (DO_TWOSTENCIL
&& !HAVE_STENCIL_TWOSIDE
&& ctx
->Stencil
.TestTwoSide
) {
295 SETUP_STENCIL(facing
);
296 TRI( v
[0], v
[1], v
[2] );
297 UNSET_STENCIL(facing
);
299 TRI( v
[0], v
[1], v
[2] );
305 VERT_SET_Z(v
[0], z
[0]);
306 VERT_SET_Z(v
[1], z
[1]);
307 VERT_SET_Z(v
[2], z
[2]);
310 if (DO_TWOSIDE
&& facing
== 1)
313 VERT_RESTORE_RGBA( 0 );
314 VERT_RESTORE_RGBA( 1 );
316 VERT_RESTORE_RGBA( 2 );
319 VERT_RESTORE_SPEC( 0 );
320 VERT_RESTORE_SPEC( 1 );
322 VERT_RESTORE_SPEC( 2 );
328 VERT_RESTORE_RGBA( 0 );
329 VERT_RESTORE_RGBA( 1 );
330 if (HAVE_SPEC
&& VB
->AttribPtr
[_TNL_ATTRIB_COLOR1
]) {
331 VERT_RESTORE_SPEC( 0 );
332 VERT_RESTORE_SPEC( 1 );
340 static void TAG(quadr
)( GLcontext
*ctx
,
341 GLuint e0
, GLuint e1
, GLuint e2
, GLuint e3
)
343 struct vertex_buffer
*VB
= &TNL_CONTEXT( ctx
)->vb
;
346 GLfloat z
[4] = { 0 };
347 GLenum mode
= GL_FILL
;
351 v
[0] = (VERTEX
*)GET_VERTEX(e0
);
352 v
[1] = (VERTEX
*)GET_VERTEX(e1
);
353 v
[2] = (VERTEX
*)GET_VERTEX(e2
);
354 v
[3] = (VERTEX
*)GET_VERTEX(e3
);
356 if (DO_TWOSIDE
|| DO_OFFSET
|| DO_UNFILLED
|| DO_TWOSTENCIL
)
358 GLfloat ex
= VERT_X(v
[2]) - VERT_X(v
[0]);
359 GLfloat ey
= VERT_Y(v
[2]) - VERT_Y(v
[0]);
360 GLfloat fx
= VERT_X(v
[3]) - VERT_X(v
[1]);
361 GLfloat fy
= VERT_Y(v
[3]) - VERT_Y(v
[1]);
362 GLfloat cc
= ex
*fy
- ey
*fx
;
364 if (DO_TWOSIDE
|| DO_UNFILLED
|| DO_TWOSTENCIL
)
366 facing
= AREA_IS_CCW( cc
) ^ ctx
->Polygon
._FrontBit
;
370 mode
= ctx
->Polygon
.BackMode
;
371 if (ctx
->Polygon
.CullFlag
&&
372 ctx
->Polygon
.CullFaceMode
!= GL_FRONT
) {
376 mode
= ctx
->Polygon
.FrontMode
;
377 if (ctx
->Polygon
.CullFlag
&&
378 ctx
->Polygon
.CullFaceMode
!= GL_BACK
) {
384 if (DO_TWOSIDE
&& facing
== 1)
386 GLfloat (*vbcolor
)[4] = VB
->BackfaceColorPtr
->data
;
389 if (HAVE_BACK_COLORS
) {
394 VERT_COPY_RGBA1( v
[0] );
395 VERT_COPY_RGBA1( v
[1] );
396 VERT_COPY_RGBA1( v
[2] );
399 VERT_COPY_RGBA1( v
[3] );
405 VERT_COPY_SPEC1( v
[0] );
406 VERT_COPY_SPEC1( v
[1] );
407 VERT_COPY_SPEC1( v
[2] );
410 VERT_COPY_SPEC1( v
[3] );
421 if (VB
->BackfaceColorPtr
->stride
) {
423 VERT_SET_RGBA( v
[0], vbcolor
[e0
] );
424 VERT_SET_RGBA( v
[1], vbcolor
[e1
] );
425 VERT_SET_RGBA( v
[2], vbcolor
[e2
] );
427 VERT_SET_RGBA( v
[3], vbcolor
[e3
] );
431 VERT_SET_RGBA( v
[0], vbcolor
[0] );
432 VERT_SET_RGBA( v
[1], vbcolor
[0] );
433 VERT_SET_RGBA( v
[2], vbcolor
[0] );
435 VERT_SET_RGBA( v
[3], vbcolor
[0] );
438 if (HAVE_SPEC
&& VB
->BackfaceSecondaryColorPtr
) {
439 GLfloat (*vbspec
)[4] = VB
->BackfaceSecondaryColorPtr
->data
;
440 ASSERT(VB
->BackfaceSecondaryColorPtr
->stride
==4*sizeof(GLfloat
));
446 VERT_SET_SPEC( v
[0], vbspec
[e0
] );
447 VERT_SET_SPEC( v
[1], vbspec
[e1
] );
448 VERT_SET_SPEC( v
[2], vbspec
[e2
] );
451 VERT_SET_SPEC( v
[3], vbspec
[e3
] );
460 offset
= ctx
->Polygon
.OffsetUnits
* DEPTH_SCALE
;
465 if (cc
* cc
> 1e-16) {
466 GLfloat ez
= z
[2] - z
[0];
467 GLfloat fz
= z
[3] - z
[1];
468 GLfloat a
= ey
*fz
- ez
*fy
;
469 GLfloat b
= ez
*fx
- ex
*fz
;
470 GLfloat ic
= 1.0 / cc
;
473 if ( ac
< 0.0f
) ac
= -ac
;
474 if ( bc
< 0.0f
) bc
= -bc
;
475 offset
+= MAX2( ac
, bc
) * ctx
->Polygon
.OffsetFactor
/ ctx
->DrawBuffer
->_MRD
;
477 offset
*= ctx
->DrawBuffer
->_MRD
* (REVERSE_DEPTH
? -1.0 : 1.0);
485 VERT_COPY_RGBA( v
[0], v
[3] );
486 VERT_COPY_RGBA( v
[1], v
[3] );
487 VERT_COPY_RGBA( v
[2], v
[3] );
488 if (HAVE_SPEC
&& VB
->AttribPtr
[_TNL_ATTRIB_COLOR1
]) {
492 VERT_COPY_SPEC( v
[0], v
[3] );
493 VERT_COPY_SPEC( v
[1], v
[3] );
494 VERT_COPY_SPEC( v
[2], v
[3] );
498 if (mode
== GL_POINT
) {
499 if (( DO_OFFSET
) && ctx
->Polygon
.OffsetPoint
) {
500 VERT_Z_ADD(v
[0], offset
);
501 VERT_Z_ADD(v
[1], offset
);
502 VERT_Z_ADD(v
[2], offset
);
503 VERT_Z_ADD(v
[3], offset
);
505 if (DO_TWOSTENCIL
&& !HAVE_STENCIL_TWOSIDE
&& ctx
->Stencil
.TestTwoSide
) {
506 SETUP_STENCIL(facing
);
507 UNFILLED_QUAD( ctx
, GL_POINT
, e0
, e1
, e2
, e3
);
508 UNSET_STENCIL(facing
);
510 UNFILLED_QUAD( ctx
, GL_POINT
, e0
, e1
, e2
, e3
);
512 } else if (mode
== GL_LINE
) {
513 if (DO_OFFSET
&& ctx
->Polygon
.OffsetLine
) {
514 VERT_Z_ADD(v
[0], offset
);
515 VERT_Z_ADD(v
[1], offset
);
516 VERT_Z_ADD(v
[2], offset
);
517 VERT_Z_ADD(v
[3], offset
);
519 if (DO_TWOSTENCIL
&& !HAVE_STENCIL_TWOSIDE
&& ctx
->Stencil
.TestTwoSide
) {
520 SETUP_STENCIL(facing
);
521 UNFILLED_QUAD( ctx
, GL_LINE
, e0
, e1
, e2
, e3
);
522 UNSET_STENCIL(facing
);
524 UNFILLED_QUAD( ctx
, GL_LINE
, e0
, e1
, e2
, e3
);
527 if (DO_OFFSET
&& ctx
->Polygon
.OffsetFill
) {
528 VERT_Z_ADD(v
[0], offset
);
529 VERT_Z_ADD(v
[1], offset
);
530 VERT_Z_ADD(v
[2], offset
);
531 VERT_Z_ADD(v
[3], offset
);
533 RASTERIZE( GL_QUADS
);
534 if (DO_TWOSTENCIL
&& !HAVE_STENCIL_TWOSIDE
&& ctx
->Stencil
.TestTwoSide
) {
535 SETUP_STENCIL(facing
);
536 QUAD( (v
[0]), (v
[1]), (v
[2]), (v
[3]) );
537 UNSET_STENCIL(facing
);
539 QUAD( (v
[0]), (v
[1]), (v
[2]), (v
[3]) );
545 VERT_SET_Z(v
[0], z
[0]);
546 VERT_SET_Z(v
[1], z
[1]);
547 VERT_SET_Z(v
[2], z
[2]);
548 VERT_SET_Z(v
[3], z
[3]);
551 if (DO_TWOSIDE
&& facing
== 1)
554 VERT_RESTORE_RGBA( 0 );
555 VERT_RESTORE_RGBA( 1 );
556 VERT_RESTORE_RGBA( 2 );
558 VERT_RESTORE_RGBA( 3 );
561 VERT_RESTORE_SPEC( 0 );
562 VERT_RESTORE_SPEC( 1 );
563 VERT_RESTORE_SPEC( 2 );
565 VERT_RESTORE_SPEC( 3 );
571 VERT_RESTORE_RGBA( 0 );
572 VERT_RESTORE_RGBA( 1 );
573 VERT_RESTORE_RGBA( 2 );
574 if (HAVE_SPEC
&& VB
->AttribPtr
[_TNL_ATTRIB_COLOR1
]) {
575 VERT_RESTORE_SPEC( 0 );
576 VERT_RESTORE_SPEC( 1 );
577 VERT_RESTORE_SPEC( 2 );
582 static void TAG(quadr
)( GLcontext
*ctx
, GLuint e0
,
583 GLuint e1
, GLuint e2
, GLuint e3
)
586 struct vertex_buffer
*VB
= &TNL_CONTEXT(ctx
)->vb
;
587 GLubyte ef1
= VB
->EdgeFlag
[e1
];
588 GLubyte ef3
= VB
->EdgeFlag
[e3
];
589 VB
->EdgeFlag
[e1
] = 0;
590 TAG(triangle
)( ctx
, e0
, e1
, e3
);
591 VB
->EdgeFlag
[e1
] = ef1
;
592 VB
->EdgeFlag
[e3
] = 0;
593 TAG(triangle
)( ctx
, e1
, e2
, e3
);
594 VB
->EdgeFlag
[e3
] = ef3
;
596 TAG(triangle
)( ctx
, e0
, e1
, e3
);
597 TAG(triangle
)( ctx
, e1
, e2
, e3
);
604 static void TAG(line
)( GLcontext
*ctx
, GLuint e0
, GLuint e1
)
606 struct vertex_buffer
*VB
= &TNL_CONTEXT(ctx
)->vb
;
610 v
[0] = (VERTEX
*)GET_VERTEX(e0
);
611 v
[1] = (VERTEX
*)GET_VERTEX(e1
);
615 VERT_COPY_RGBA( v
[0], v
[1] );
616 if (HAVE_SPEC
&& VB
->AttribPtr
[_TNL_ATTRIB_COLOR1
]) {
618 VERT_COPY_SPEC( v
[0], v
[1] );
625 VERT_RESTORE_RGBA( 0 );
627 if (HAVE_SPEC
&& VB
->AttribPtr
[_TNL_ATTRIB_COLOR1
]) {
628 VERT_RESTORE_SPEC( 0 );
635 static void TAG(points
)( GLcontext
*ctx
, GLuint first
, GLuint last
)
637 struct vertex_buffer
*VB
= &TNL_CONTEXT( ctx
)->vb
;
642 for ( i
= first
; i
< last
; i
++ ) {
643 if ( VB
->ClipMask
[i
] == 0 ) {
644 VERTEX
*v
= (VERTEX
*)GET_VERTEX(i
);
649 for ( i
= first
; i
< last
; i
++ ) {
650 GLuint e
= VB
->Elts
[i
];
651 if ( VB
->ClipMask
[e
] == 0 ) {
652 VERTEX
*v
= (VERTEX
*)GET_VERTEX(e
);
660 static void TAG(init
)( void )
663 TAB
[IND
].quad
= TAG(quadr
);
666 TAB
[IND
].triangle
= TAG(triangle
);
669 TAB
[IND
].line
= TAG(line
);
672 TAB
[IND
].points
= TAG(points
);
685 #undef VERT_COPY_SPEC
686 #undef VERT_SAVE_SPEC
687 #undef VERT_RESTORE_SPEC
689 #undef VERT_COPY_SPEC1
697 #if !HAVE_BACK_COLORS
698 #undef VERT_COPY_SPEC1
699 #undef VERT_COPY_RGBA1
702 #ifndef INSANE_VERTICES