Merge branch 'width0'
[mesa.git] / src / mesa / swrast_setup / ss_triangle.c
1 /*
2 * Mesa 3-D graphics library
3 * Version: 7.1
4 *
5 * Copyright (C) 1999-2007 Brian Paul All Rights Reserved.
6 *
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:
13 *
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
16 *
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.
23 *
24 * Authors:
25 * Keith Whitwell <keith@tungstengraphics.com>
26 */
27
28 #include "main/glheader.h"
29 #include "main/colormac.h"
30 #include "main/macros.h"
31 #include "main/mtypes.h"
32
33 #include "tnl/t_context.h"
34
35 #include "ss_triangle.h"
36 #include "ss_context.h"
37
38 #define SS_RGBA_BIT 0x1
39 #define SS_OFFSET_BIT 0x2
40 #define SS_TWOSIDE_BIT 0x4
41 #define SS_UNFILLED_BIT 0x8
42 #define SS_MAX_TRIFUNC 0x10
43
44 static tnl_triangle_func tri_tab[SS_MAX_TRIFUNC];
45 static tnl_quad_func quad_tab[SS_MAX_TRIFUNC];
46
47
48 /*
49 * Render a triangle respecting edge flags.
50 */
51 typedef void (* swsetup_edge_render_prim_tri)(GLcontext *ctx,
52 const GLubyte *ef,
53 GLuint e0,
54 GLuint e1,
55 GLuint e2,
56 const SWvertex *v0,
57 const SWvertex *v1,
58 const SWvertex *v2);
59
60 /*
61 * Render a triangle using lines and respecting edge flags.
62 */
63 static void
64 _swsetup_edge_render_line_tri(GLcontext *ctx,
65 const GLubyte *ef,
66 GLuint e0,
67 GLuint e1,
68 GLuint e2,
69 const SWvertex *v0,
70 const SWvertex *v1,
71 const SWvertex *v2)
72 {
73 SScontext *swsetup = SWSETUP_CONTEXT(ctx);
74
75 if (swsetup->render_prim == GL_POLYGON) {
76 if (ef[e2]) _swrast_Line( ctx, v2, v0 );
77 if (ef[e0]) _swrast_Line( ctx, v0, v1 );
78 if (ef[e1]) _swrast_Line( ctx, v1, v2 );
79 } else {
80 if (ef[e0]) _swrast_Line( ctx, v0, v1 );
81 if (ef[e1]) _swrast_Line( ctx, v1, v2 );
82 if (ef[e2]) _swrast_Line( ctx, v2, v0 );
83 }
84 }
85
86 /*
87 * Render a triangle using points and respecting edge flags.
88 */
89 static void
90 _swsetup_edge_render_point_tri(GLcontext *ctx,
91 const GLubyte *ef,
92 GLuint e0,
93 GLuint e1,
94 GLuint e2,
95 const SWvertex *v0,
96 const SWvertex *v1,
97 const SWvertex *v2)
98 {
99 if (ef[e0]) _swrast_Point( ctx, v0 );
100 if (ef[e1]) _swrast_Point( ctx, v1 );
101 if (ef[e2]) _swrast_Point( ctx, v2 );
102
103 _swrast_flush(ctx);
104 }
105
106 /*
107 * Render a triangle respecting cull and shade model.
108 */
109 static void _swsetup_render_tri(GLcontext *ctx,
110 GLuint e0,
111 GLuint e1,
112 GLuint e2,
113 GLuint facing,
114 swsetup_edge_render_prim_tri render)
115 {
116 SScontext *swsetup = SWSETUP_CONTEXT(ctx);
117 struct vertex_buffer *VB = &TNL_CONTEXT(ctx)->vb;
118 GLubyte *ef = VB->EdgeFlag;
119 SWvertex *verts = swsetup->verts;
120 SWvertex *v0 = &verts[e0];
121 SWvertex *v1 = &verts[e1];
122 SWvertex *v2 = &verts[e2];
123
124 /* cull testing */
125 if (ctx->Polygon.CullFlag) {
126 if (facing == 1 && ctx->Polygon.CullFaceMode != GL_FRONT)
127 return;
128 if (facing == 0 && ctx->Polygon.CullFaceMode != GL_BACK)
129 return;
130 }
131
132 _swrast_SetFacing(ctx, facing);
133
134 if (ctx->Light.ShadeModel == GL_FLAT) {
135 GLchan c[2][4];
136 GLfloat s[2][4];
137 GLfloat i[2];
138
139 /* save colors/indexes for v0, v1 vertices */
140 COPY_CHAN4(c[0], v0->color);
141 COPY_CHAN4(c[1], v1->color);
142 COPY_4V(s[0], v0->attrib[FRAG_ATTRIB_COL1]);
143 COPY_4V(s[1], v1->attrib[FRAG_ATTRIB_COL1]);
144 i[0] = v0->attrib[FRAG_ATTRIB_CI][0];
145 i[1] = v1->attrib[FRAG_ATTRIB_CI][0];
146
147 /* copy v2 color/indexes to v0, v1 indexes */
148 COPY_CHAN4(v0->color, v2->color);
149 COPY_CHAN4(v1->color, v2->color);
150 COPY_4V(v0->attrib[FRAG_ATTRIB_COL1], v2->attrib[FRAG_ATTRIB_COL1]);
151 COPY_4V(v1->attrib[FRAG_ATTRIB_COL1], v2->attrib[FRAG_ATTRIB_COL1]);
152 v0->attrib[FRAG_ATTRIB_CI][0] = v2->attrib[FRAG_ATTRIB_CI][0];
153 v1->attrib[FRAG_ATTRIB_CI][0] = v2->attrib[FRAG_ATTRIB_CI][0];
154
155 render(ctx, ef, e0, e1, e2, v0, v1, v2);
156
157 COPY_CHAN4(v0->color, c[0]);
158 COPY_CHAN4(v1->color, c[1]);
159 COPY_4V(v0->attrib[FRAG_ATTRIB_COL1], s[0]);
160 COPY_4V(v1->attrib[FRAG_ATTRIB_COL1], s[1]);
161 v0->attrib[FRAG_ATTRIB_CI][0] = i[0];
162 v1->attrib[FRAG_ATTRIB_CI][0] = i[1];
163 }
164 else {
165 render(ctx, ef, e0, e1, e2, v0, v1, v2);
166 }
167 }
168
169 #define SS_COLOR(a,b) UNCLAMPED_FLOAT_TO_RGBA_CHAN(a,b)
170 #define SS_SPEC(a,b) UNCLAMPED_FLOAT_TO_RGB_CHAN(a,b)
171 #define SS_IND(a,b) (a = b)
172
173 #define IND (0)
174 #define TAG(x) x
175 #include "ss_tritmp.h"
176
177 #define IND (SS_OFFSET_BIT)
178 #define TAG(x) x##_offset
179 #include "ss_tritmp.h"
180
181 #define IND (SS_TWOSIDE_BIT)
182 #define TAG(x) x##_twoside
183 #include "ss_tritmp.h"
184
185 #define IND (SS_OFFSET_BIT|SS_TWOSIDE_BIT)
186 #define TAG(x) x##_offset_twoside
187 #include "ss_tritmp.h"
188
189 #define IND (SS_UNFILLED_BIT)
190 #define TAG(x) x##_unfilled
191 #include "ss_tritmp.h"
192
193 #define IND (SS_OFFSET_BIT|SS_UNFILLED_BIT)
194 #define TAG(x) x##_offset_unfilled
195 #include "ss_tritmp.h"
196
197 #define IND (SS_TWOSIDE_BIT|SS_UNFILLED_BIT)
198 #define TAG(x) x##_twoside_unfilled
199 #include "ss_tritmp.h"
200
201 #define IND (SS_OFFSET_BIT|SS_TWOSIDE_BIT|SS_UNFILLED_BIT)
202 #define TAG(x) x##_offset_twoside_unfilled
203 #include "ss_tritmp.h"
204
205 #define IND (0|SS_RGBA_BIT)
206 #define TAG(x) x##_rgba
207 #include "ss_tritmp.h"
208
209 #define IND (SS_OFFSET_BIT|SS_RGBA_BIT)
210 #define TAG(x) x##_offset_rgba
211 #include "ss_tritmp.h"
212
213 #define IND (SS_TWOSIDE_BIT|SS_RGBA_BIT)
214 #define TAG(x) x##_twoside_rgba
215 #include "ss_tritmp.h"
216
217 #define IND (SS_OFFSET_BIT|SS_TWOSIDE_BIT|SS_RGBA_BIT)
218 #define TAG(x) x##_offset_twoside_rgba
219 #include "ss_tritmp.h"
220
221 #define IND (SS_UNFILLED_BIT|SS_RGBA_BIT)
222 #define TAG(x) x##_unfilled_rgba
223 #include "ss_tritmp.h"
224
225 #define IND (SS_OFFSET_BIT|SS_UNFILLED_BIT|SS_RGBA_BIT)
226 #define TAG(x) x##_offset_unfilled_rgba
227 #include "ss_tritmp.h"
228
229 #define IND (SS_TWOSIDE_BIT|SS_UNFILLED_BIT|SS_RGBA_BIT)
230 #define TAG(x) x##_twoside_unfilled_rgba
231 #include "ss_tritmp.h"
232
233 #define IND (SS_OFFSET_BIT|SS_TWOSIDE_BIT|SS_UNFILLED_BIT|SS_RGBA_BIT)
234 #define TAG(x) x##_offset_twoside_unfilled_rgba
235 #include "ss_tritmp.h"
236
237
238 void _swsetup_trifuncs_init( GLcontext *ctx )
239 {
240 (void) ctx;
241
242 init();
243 init_offset();
244 init_twoside();
245 init_offset_twoside();
246 init_unfilled();
247 init_offset_unfilled();
248 init_twoside_unfilled();
249 init_offset_twoside_unfilled();
250
251 init_rgba();
252 init_offset_rgba();
253 init_twoside_rgba();
254 init_offset_twoside_rgba();
255 init_unfilled_rgba();
256 init_offset_unfilled_rgba();
257 init_twoside_unfilled_rgba();
258 init_offset_twoside_unfilled_rgba();
259 }
260
261
262 static void swsetup_points( GLcontext *ctx, GLuint first, GLuint last )
263 {
264 struct vertex_buffer *VB = &TNL_CONTEXT(ctx)->vb;
265 SWvertex *verts = SWSETUP_CONTEXT(ctx)->verts;
266 GLuint i;
267
268 if (VB->Elts) {
269 for (i = first; i < last; i++)
270 if (VB->ClipMask[VB->Elts[i]] == 0)
271 _swrast_Point( ctx, &verts[VB->Elts[i]] );
272 }
273 else {
274 for (i = first; i < last; i++)
275 if (VB->ClipMask[i] == 0)
276 _swrast_Point( ctx, &verts[i] );
277 }
278 }
279
280 static void swsetup_line( GLcontext *ctx, GLuint v0, GLuint v1 )
281 {
282 SWvertex *verts = SWSETUP_CONTEXT(ctx)->verts;
283 _swrast_Line( ctx, &verts[v0], &verts[v1] );
284 }
285
286
287
288 void _swsetup_choose_trifuncs( GLcontext *ctx )
289 {
290 TNLcontext *tnl = TNL_CONTEXT(ctx);
291 GLuint ind = 0;
292
293 if (ctx->Polygon.OffsetPoint ||
294 ctx->Polygon.OffsetLine ||
295 ctx->Polygon.OffsetFill)
296 ind |= SS_OFFSET_BIT;
297
298 if ((ctx->Light.Enabled && ctx->Light.Model.TwoSide) ||
299 (ctx->VertexProgram._Current && ctx->VertexProgram.TwoSideEnabled))
300 ind |= SS_TWOSIDE_BIT;
301
302 /* We piggyback the two-sided stencil front/back determination on the
303 * unfilled triangle path.
304 */
305 if (ctx->Polygon.FrontMode != GL_FILL ||
306 ctx->Polygon.BackMode != GL_FILL ||
307 (ctx->Stencil.Enabled && ctx->Stencil._TestTwoSide))
308 ind |= SS_UNFILLED_BIT;
309
310 if (ctx->Visual.rgbMode)
311 ind |= SS_RGBA_BIT;
312
313 tnl->Driver.Render.Triangle = tri_tab[ind];
314 tnl->Driver.Render.Quad = quad_tab[ind];
315 tnl->Driver.Render.Line = swsetup_line;
316 tnl->Driver.Render.Points = swsetup_points;
317 }