9633469df1cc1eabfee205319b7a28e2ad3b3db6
[mesa.git] / src / mesa / swrast_setup / ss_triangle.c
1 /* $Id: ss_triangle.c,v 1.19 2002/10/29 20:29:00 brianp Exp $ */
2
3 /*
4 * Mesa 3-D graphics library
5 * Version: 3.5
6 *
7 * Copyright (C) 1999-2001 Brian Paul All Rights Reserved.
8 *
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:
15 *
16 * The above copyright notice and this permission notice shall be included
17 * in all copies or substantial portions of the Software.
18 *
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.
25 *
26 * Authors:
27 * Keith Whitwell <keith@tungstengraphics.com>
28 */
29
30 #include "glheader.h"
31 #include "colormac.h"
32 #include "macros.h"
33 #include "mtypes.h"
34
35 #include "tnl/t_context.h"
36
37 #include "ss_triangle.h"
38 #include "ss_context.h"
39
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
45
46 static triangle_func tri_tab[SS_MAX_TRIFUNC];
47 static quad_func quad_tab[SS_MAX_TRIFUNC];
48
49
50 static void _swsetup_render_line_tri( GLcontext *ctx,
51 GLuint e0, GLuint e1, GLuint e2 )
52 {
53 SScontext *swsetup = SWSETUP_CONTEXT(ctx);
54 struct vertex_buffer *VB = &TNL_CONTEXT(ctx)->vb;
55 GLubyte *ef = VB->EdgeFlag;
56 SWvertex *verts = swsetup->verts;
57 SWvertex *v0 = &verts[e0];
58 SWvertex *v1 = &verts[e1];
59 SWvertex *v2 = &verts[e2];
60 GLchan c[2][4];
61 GLchan s[2][4];
62 GLuint i[2];
63
64 if (ctx->_TriangleCaps & DD_FLATSHADE) {
65 COPY_CHAN4(c[0], v0->color);
66 COPY_CHAN4(c[1], v1->color);
67 COPY_CHAN4(s[0], v0->specular);
68 COPY_CHAN4(s[1], v1->specular);
69 i[0] = v0->index;
70 i[1] = v1->index;
71
72 COPY_CHAN4(v0->color, v2->color);
73 COPY_CHAN4(v1->color, v2->color);
74 COPY_CHAN4(v0->specular, v2->specular);
75 COPY_CHAN4(v1->specular, v2->specular);
76 v0->index = v2->index;
77 v1->index = v2->index;
78 }
79
80 if (swsetup->render_prim == GL_POLYGON) {
81 if (ef[e2]) _swrast_Line( ctx, v2, v0 );
82 if (ef[e0]) _swrast_Line( ctx, v0, v1 );
83 if (ef[e1]) _swrast_Line( ctx, v1, v2 );
84 } else {
85 if (ef[e0]) _swrast_Line( ctx, v0, v1 );
86 if (ef[e1]) _swrast_Line( ctx, v1, v2 );
87 if (ef[e2]) _swrast_Line( ctx, v2, v0 );
88 }
89
90 if (ctx->_TriangleCaps & DD_FLATSHADE) {
91 COPY_CHAN4(v0->color, c[0]);
92 COPY_CHAN4(v1->color, c[1]);
93 COPY_CHAN4(v0->specular, s[0]);
94 COPY_CHAN4(v1->specular, s[1]);
95 v0->index = i[0];
96 v1->index = i[1];
97 }
98 }
99
100 static void _swsetup_render_point_tri( GLcontext *ctx,
101 GLuint e0, GLuint e1, GLuint e2 )
102 {
103 SScontext *swsetup = SWSETUP_CONTEXT(ctx);
104 struct vertex_buffer *VB = &TNL_CONTEXT(ctx)->vb;
105 GLubyte *ef = VB->EdgeFlag;
106 SWvertex *verts = swsetup->verts;
107 SWvertex *v0 = &verts[e0];
108 SWvertex *v1 = &verts[e1];
109 SWvertex *v2 = &verts[e2];
110 GLchan c[2][4];
111 GLchan s[2][4];
112 GLuint i[2];
113
114 if (ctx->_TriangleCaps & DD_FLATSHADE) {
115 COPY_CHAN4(c[0], v0->color);
116 COPY_CHAN4(c[1], v1->color);
117 COPY_CHAN4(s[0], v0->specular);
118 COPY_CHAN4(s[1], v1->specular);
119 i[0] = v0->index;
120 i[1] = v1->index;
121
122 COPY_CHAN4(v0->color, v2->color);
123 COPY_CHAN4(v1->color, v2->color);
124 COPY_CHAN4(v0->specular, v2->specular);
125 COPY_CHAN4(v1->specular, v2->specular);
126 v0->index = v2->index;
127 v1->index = v2->index;
128 }
129
130 if (ef[e0]) _swrast_Point( ctx, v0 );
131 if (ef[e1]) _swrast_Point( ctx, v1 );
132 if (ef[e2]) _swrast_Point( ctx, v2 );
133
134 if (ctx->_TriangleCaps & DD_FLATSHADE) {
135 COPY_CHAN4(v0->color, c[0]);
136 COPY_CHAN4(v1->color, c[1]);
137 COPY_CHAN4(v0->specular, s[0]);
138 COPY_CHAN4(v1->specular, s[1]);
139 v0->index = i[0];
140 v1->index = i[1];
141 }
142 _swrast_flush(ctx);
143 }
144
145 #define SS_COLOR(a,b) COPY_CHAN4(a,b)
146 #define SS_SPEC(a,b) COPY_3V(a,b)
147 #define SS_IND(a,b) (a = b)
148
149 #define IND (0)
150 #define TAG(x) x
151 #include "ss_tritmp.h"
152
153 #define IND (SS_OFFSET_BIT)
154 #define TAG(x) x##_offset
155 #include "ss_tritmp.h"
156
157 #define IND (SS_TWOSIDE_BIT)
158 #define TAG(x) x##_twoside
159 #include "ss_tritmp.h"
160
161 #define IND (SS_OFFSET_BIT|SS_TWOSIDE_BIT)
162 #define TAG(x) x##_offset_twoside
163 #include "ss_tritmp.h"
164
165 #define IND (SS_UNFILLED_BIT)
166 #define TAG(x) x##_unfilled
167 #include "ss_tritmp.h"
168
169 #define IND (SS_OFFSET_BIT|SS_UNFILLED_BIT)
170 #define TAG(x) x##_offset_unfilled
171 #include "ss_tritmp.h"
172
173 #define IND (SS_TWOSIDE_BIT|SS_UNFILLED_BIT)
174 #define TAG(x) x##_twoside_unfilled
175 #include "ss_tritmp.h"
176
177 #define IND (SS_OFFSET_BIT|SS_TWOSIDE_BIT|SS_UNFILLED_BIT)
178 #define TAG(x) x##_offset_twoside_unfilled
179 #include "ss_tritmp.h"
180
181 #define IND (0|SS_RGBA_BIT)
182 #define TAG(x) x##_rgba
183 #include "ss_tritmp.h"
184
185 #define IND (SS_OFFSET_BIT|SS_RGBA_BIT)
186 #define TAG(x) x##_offset_rgba
187 #include "ss_tritmp.h"
188
189 #define IND (SS_TWOSIDE_BIT|SS_RGBA_BIT)
190 #define TAG(x) x##_twoside_rgba
191 #include "ss_tritmp.h"
192
193 #define IND (SS_OFFSET_BIT|SS_TWOSIDE_BIT|SS_RGBA_BIT)
194 #define TAG(x) x##_offset_twoside_rgba
195 #include "ss_tritmp.h"
196
197 #define IND (SS_UNFILLED_BIT|SS_RGBA_BIT)
198 #define TAG(x) x##_unfilled_rgba
199 #include "ss_tritmp.h"
200
201 #define IND (SS_OFFSET_BIT|SS_UNFILLED_BIT|SS_RGBA_BIT)
202 #define TAG(x) x##_offset_unfilled_rgba
203 #include "ss_tritmp.h"
204
205 #define IND (SS_TWOSIDE_BIT|SS_UNFILLED_BIT|SS_RGBA_BIT)
206 #define TAG(x) x##_twoside_unfilled_rgba
207 #include "ss_tritmp.h"
208
209 #define IND (SS_OFFSET_BIT|SS_TWOSIDE_BIT|SS_UNFILLED_BIT|SS_RGBA_BIT)
210 #define TAG(x) x##_offset_twoside_unfilled_rgba
211 #include "ss_tritmp.h"
212
213
214 void _swsetup_trifuncs_init( GLcontext *ctx )
215 {
216 (void) ctx;
217
218 init();
219 init_offset();
220 init_twoside();
221 init_offset_twoside();
222 init_unfilled();
223 init_offset_unfilled();
224 init_twoside_unfilled();
225 init_offset_twoside_unfilled();
226
227 init_rgba();
228 init_offset_rgba();
229 init_twoside_rgba();
230 init_offset_twoside_rgba();
231 init_unfilled_rgba();
232 init_offset_unfilled_rgba();
233 init_twoside_unfilled_rgba();
234 init_offset_twoside_unfilled_rgba();
235 }
236
237
238 static void swsetup_points( GLcontext *ctx, GLuint first, GLuint last )
239 {
240 struct vertex_buffer *VB = &TNL_CONTEXT(ctx)->vb;
241 SWvertex *verts = SWSETUP_CONTEXT(ctx)->verts;
242 GLuint i;
243
244 if (VB->Elts) {
245 for (i = first; i < last; i++)
246 if (VB->ClipMask[VB->Elts[i]] == 0)
247 _swrast_Point( ctx, &verts[VB->Elts[i]] );
248 }
249 else {
250 for (i = first; i < last; i++)
251 if (VB->ClipMask[i] == 0)
252 _swrast_Point( ctx, &verts[i] );
253 }
254 }
255
256 static void swsetup_line( GLcontext *ctx, GLuint v0, GLuint v1 )
257 {
258 SWvertex *verts = SWSETUP_CONTEXT(ctx)->verts;
259 _swrast_Line( ctx, &verts[v0], &verts[v1] );
260 }
261
262
263
264 void _swsetup_choose_trifuncs( GLcontext *ctx )
265 {
266 TNLcontext *tnl = TNL_CONTEXT(ctx);
267 GLuint ind = 0;
268
269 if (ctx->Polygon.OffsetPoint ||
270 ctx->Polygon.OffsetLine ||
271 ctx->Polygon.OffsetFill)
272 ind |= SS_OFFSET_BIT;
273
274 if (ctx->Light.Enabled && ctx->Light.Model.TwoSide)
275 ind |= SS_TWOSIDE_BIT;
276
277 /* We piggyback the two-sided stencil front/back determination on the
278 * unfilled triangle path.
279 */
280 if ((ctx->_TriangleCaps & DD_TRI_UNFILLED) ||
281 (ctx->Stencil.Enabled && ctx->Stencil.TestTwoSide))
282 ind |= SS_UNFILLED_BIT;
283
284 if (ctx->Visual.rgbMode)
285 ind |= SS_RGBA_BIT;
286
287 tnl->Driver.Render.Triangle = tri_tab[ind];
288 tnl->Driver.Render.Quad = quad_tab[ind];
289 tnl->Driver.Render.Line = swsetup_line;
290 tnl->Driver.Render.Points = swsetup_points;
291
292 ctx->_Facing = 0;
293 }