1 /* $Id: ss_triangle.c,v 1.20 2002/10/29 22:25:57 brianp Exp $ */
4 * Mesa 3-D graphics library
7 * Copyright (C) 1999-2001 Brian Paul All Rights Reserved.
9 * Permission is hereby granted, free of charge, to any person obtaining a
10 * copy of this software and associated documentation files (the "Software"),
11 * to deal in the Software without restriction, including without limitation
12 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
13 * and/or sell copies of the Software, and to permit persons to whom the
14 * Software is furnished to do so, subject to the following conditions:
16 * The above copyright notice and this permission notice shall be included
17 * in all copies or substantial portions of the Software.
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
20 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
22 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
23 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
24 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 * Keith Whitwell <keith@tungstengraphics.com>
35 #include "tnl/t_context.h"
37 #include "ss_triangle.h"
38 #include "ss_context.h"
40 #define SS_RGBA_BIT 0x1
41 #define SS_OFFSET_BIT 0x2
42 #define SS_TWOSIDE_BIT 0x4
43 #define SS_UNFILLED_BIT 0x8
44 #define SS_MAX_TRIFUNC 0x10
46 static triangle_func tri_tab
[SS_MAX_TRIFUNC
];
47 static quad_func quad_tab
[SS_MAX_TRIFUNC
];
50 static void _swsetup_render_line_tri( GLcontext
*ctx
,
51 GLuint e0
, GLuint e1
, GLuint e2
,
54 SScontext
*swsetup
= SWSETUP_CONTEXT(ctx
);
55 struct vertex_buffer
*VB
= &TNL_CONTEXT(ctx
)->vb
;
56 GLubyte
*ef
= VB
->EdgeFlag
;
57 SWvertex
*verts
= swsetup
->verts
;
58 SWvertex
*v0
= &verts
[e0
];
59 SWvertex
*v1
= &verts
[e1
];
60 SWvertex
*v2
= &verts
[e2
];
66 if (ctx
->Polygon
.CullFlag
) {
67 if (facing
== 1 && ctx
->Polygon
.CullFaceMode
!= GL_FRONT
)
69 if (facing
== 0 && ctx
->Polygon
.CullFaceMode
!= GL_BACK
)
73 if (ctx
->_TriangleCaps
& DD_FLATSHADE
) {
74 COPY_CHAN4(c
[0], v0
->color
);
75 COPY_CHAN4(c
[1], v1
->color
);
76 COPY_CHAN4(s
[0], v0
->specular
);
77 COPY_CHAN4(s
[1], v1
->specular
);
81 COPY_CHAN4(v0
->color
, v2
->color
);
82 COPY_CHAN4(v1
->color
, v2
->color
);
83 COPY_CHAN4(v0
->specular
, v2
->specular
);
84 COPY_CHAN4(v1
->specular
, v2
->specular
);
85 v0
->index
= v2
->index
;
86 v1
->index
= v2
->index
;
89 if (swsetup
->render_prim
== GL_POLYGON
) {
90 if (ef
[e2
]) _swrast_Line( ctx
, v2
, v0
);
91 if (ef
[e0
]) _swrast_Line( ctx
, v0
, v1
);
92 if (ef
[e1
]) _swrast_Line( ctx
, v1
, v2
);
94 if (ef
[e0
]) _swrast_Line( ctx
, v0
, v1
);
95 if (ef
[e1
]) _swrast_Line( ctx
, v1
, v2
);
96 if (ef
[e2
]) _swrast_Line( ctx
, v2
, v0
);
99 if (ctx
->_TriangleCaps
& DD_FLATSHADE
) {
100 COPY_CHAN4(v0
->color
, c
[0]);
101 COPY_CHAN4(v1
->color
, c
[1]);
102 COPY_CHAN4(v0
->specular
, s
[0]);
103 COPY_CHAN4(v1
->specular
, s
[1]);
109 static void _swsetup_render_point_tri( GLcontext
*ctx
,
110 GLuint e0
, GLuint e1
, GLuint e2
,
113 SScontext
*swsetup
= SWSETUP_CONTEXT(ctx
);
114 struct vertex_buffer
*VB
= &TNL_CONTEXT(ctx
)->vb
;
115 GLubyte
*ef
= VB
->EdgeFlag
;
116 SWvertex
*verts
= swsetup
->verts
;
117 SWvertex
*v0
= &verts
[e0
];
118 SWvertex
*v1
= &verts
[e1
];
119 SWvertex
*v2
= &verts
[e2
];
125 if (ctx
->Polygon
.CullFlag
) {
126 if (facing
== 1 && ctx
->Polygon
.CullFaceMode
!= GL_FRONT
)
128 if (facing
== 0 && ctx
->Polygon
.CullFaceMode
!= GL_BACK
)
132 if (ctx
->_TriangleCaps
& DD_FLATSHADE
) {
133 COPY_CHAN4(c
[0], v0
->color
);
134 COPY_CHAN4(c
[1], v1
->color
);
135 COPY_CHAN4(s
[0], v0
->specular
);
136 COPY_CHAN4(s
[1], v1
->specular
);
140 COPY_CHAN4(v0
->color
, v2
->color
);
141 COPY_CHAN4(v1
->color
, v2
->color
);
142 COPY_CHAN4(v0
->specular
, v2
->specular
);
143 COPY_CHAN4(v1
->specular
, v2
->specular
);
144 v0
->index
= v2
->index
;
145 v1
->index
= v2
->index
;
148 if (ef
[e0
]) _swrast_Point( ctx
, v0
);
149 if (ef
[e1
]) _swrast_Point( ctx
, v1
);
150 if (ef
[e2
]) _swrast_Point( ctx
, v2
);
152 if (ctx
->_TriangleCaps
& DD_FLATSHADE
) {
153 COPY_CHAN4(v0
->color
, c
[0]);
154 COPY_CHAN4(v1
->color
, c
[1]);
155 COPY_CHAN4(v0
->specular
, s
[0]);
156 COPY_CHAN4(v1
->specular
, s
[1]);
163 #define SS_COLOR(a,b) COPY_CHAN4(a,b)
164 #define SS_SPEC(a,b) COPY_3V(a,b)
165 #define SS_IND(a,b) (a = b)
169 #include "ss_tritmp.h"
171 #define IND (SS_OFFSET_BIT)
172 #define TAG(x) x##_offset
173 #include "ss_tritmp.h"
175 #define IND (SS_TWOSIDE_BIT)
176 #define TAG(x) x##_twoside
177 #include "ss_tritmp.h"
179 #define IND (SS_OFFSET_BIT|SS_TWOSIDE_BIT)
180 #define TAG(x) x##_offset_twoside
181 #include "ss_tritmp.h"
183 #define IND (SS_UNFILLED_BIT)
184 #define TAG(x) x##_unfilled
185 #include "ss_tritmp.h"
187 #define IND (SS_OFFSET_BIT|SS_UNFILLED_BIT)
188 #define TAG(x) x##_offset_unfilled
189 #include "ss_tritmp.h"
191 #define IND (SS_TWOSIDE_BIT|SS_UNFILLED_BIT)
192 #define TAG(x) x##_twoside_unfilled
193 #include "ss_tritmp.h"
195 #define IND (SS_OFFSET_BIT|SS_TWOSIDE_BIT|SS_UNFILLED_BIT)
196 #define TAG(x) x##_offset_twoside_unfilled
197 #include "ss_tritmp.h"
199 #define IND (0|SS_RGBA_BIT)
200 #define TAG(x) x##_rgba
201 #include "ss_tritmp.h"
203 #define IND (SS_OFFSET_BIT|SS_RGBA_BIT)
204 #define TAG(x) x##_offset_rgba
205 #include "ss_tritmp.h"
207 #define IND (SS_TWOSIDE_BIT|SS_RGBA_BIT)
208 #define TAG(x) x##_twoside_rgba
209 #include "ss_tritmp.h"
211 #define IND (SS_OFFSET_BIT|SS_TWOSIDE_BIT|SS_RGBA_BIT)
212 #define TAG(x) x##_offset_twoside_rgba
213 #include "ss_tritmp.h"
215 #define IND (SS_UNFILLED_BIT|SS_RGBA_BIT)
216 #define TAG(x) x##_unfilled_rgba
217 #include "ss_tritmp.h"
219 #define IND (SS_OFFSET_BIT|SS_UNFILLED_BIT|SS_RGBA_BIT)
220 #define TAG(x) x##_offset_unfilled_rgba
221 #include "ss_tritmp.h"
223 #define IND (SS_TWOSIDE_BIT|SS_UNFILLED_BIT|SS_RGBA_BIT)
224 #define TAG(x) x##_twoside_unfilled_rgba
225 #include "ss_tritmp.h"
227 #define IND (SS_OFFSET_BIT|SS_TWOSIDE_BIT|SS_UNFILLED_BIT|SS_RGBA_BIT)
228 #define TAG(x) x##_offset_twoside_unfilled_rgba
229 #include "ss_tritmp.h"
232 void _swsetup_trifuncs_init( GLcontext
*ctx
)
239 init_offset_twoside();
241 init_offset_unfilled();
242 init_twoside_unfilled();
243 init_offset_twoside_unfilled();
248 init_offset_twoside_rgba();
249 init_unfilled_rgba();
250 init_offset_unfilled_rgba();
251 init_twoside_unfilled_rgba();
252 init_offset_twoside_unfilled_rgba();
256 static void swsetup_points( GLcontext
*ctx
, GLuint first
, GLuint last
)
258 struct vertex_buffer
*VB
= &TNL_CONTEXT(ctx
)->vb
;
259 SWvertex
*verts
= SWSETUP_CONTEXT(ctx
)->verts
;
263 for (i
= first
; i
< last
; i
++)
264 if (VB
->ClipMask
[VB
->Elts
[i
]] == 0)
265 _swrast_Point( ctx
, &verts
[VB
->Elts
[i
]] );
268 for (i
= first
; i
< last
; i
++)
269 if (VB
->ClipMask
[i
] == 0)
270 _swrast_Point( ctx
, &verts
[i
] );
274 static void swsetup_line( GLcontext
*ctx
, GLuint v0
, GLuint v1
)
276 SWvertex
*verts
= SWSETUP_CONTEXT(ctx
)->verts
;
277 _swrast_Line( ctx
, &verts
[v0
], &verts
[v1
] );
282 void _swsetup_choose_trifuncs( GLcontext
*ctx
)
284 TNLcontext
*tnl
= TNL_CONTEXT(ctx
);
287 if (ctx
->Polygon
.OffsetPoint
||
288 ctx
->Polygon
.OffsetLine
||
289 ctx
->Polygon
.OffsetFill
)
290 ind
|= SS_OFFSET_BIT
;
292 if (ctx
->Light
.Enabled
&& ctx
->Light
.Model
.TwoSide
)
293 ind
|= SS_TWOSIDE_BIT
;
295 /* We piggyback the two-sided stencil front/back determination on the
296 * unfilled triangle path.
298 if ((ctx
->_TriangleCaps
& DD_TRI_UNFILLED
) ||
299 (ctx
->Stencil
.Enabled
&& ctx
->Stencil
.TestTwoSide
))
300 ind
|= SS_UNFILLED_BIT
;
302 if (ctx
->Visual
.rgbMode
)
305 tnl
->Driver
.Render
.Triangle
= tri_tab
[ind
];
306 tnl
->Driver
.Render
.Quad
= quad_tab
[ind
];
307 tnl
->Driver
.Render
.Line
= swsetup_line
;
308 tnl
->Driver
.Render
.Points
= swsetup_points
;