Fix TCL_LIGHT_MODEL_CTL setting in radeonColorMaterial.
[mesa.git] / src / mesa / swrast_setup / ss_triangle.c
1
2 /*
3 * Mesa 3-D graphics library
4 * Version: 3.5
5 *
6 * Copyright (C) 1999-2001 Brian Paul All Rights Reserved.
7 *
8 * Permission is hereby granted, free of charge, to any person obtaining a
9 * copy of this software and associated documentation files (the "Software"),
10 * to deal in the Software without restriction, including without limitation
11 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
12 * and/or sell copies of the Software, and to permit persons to whom the
13 * Software is furnished to do so, subject to the following conditions:
14 *
15 * The above copyright notice and this permission notice shall be included
16 * in all copies or substantial portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
22 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
23 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 *
25 * Authors:
26 * Keith Whitwell <keith@tungstengraphics.com>
27 */
28
29 #include "glheader.h"
30 #include "colormac.h"
31 #include "macros.h"
32 #include "mtypes.h"
33
34 #include "tnl/t_context.h"
35
36 #include "ss_triangle.h"
37 #include "ss_context.h"
38
39 #define SS_RGBA_BIT 0x1
40 #define SS_OFFSET_BIT 0x2
41 #define SS_TWOSIDE_BIT 0x4
42 #define SS_UNFILLED_BIT 0x8
43 #define SS_MAX_TRIFUNC 0x10
44
45 static triangle_func tri_tab[SS_MAX_TRIFUNC];
46 static quad_func quad_tab[SS_MAX_TRIFUNC];
47
48
49 static void _swsetup_render_line_tri( GLcontext *ctx,
50 GLuint e0, GLuint e1, GLuint e2,
51 GLuint facing )
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 /* cull testing */
65 if (ctx->Polygon.CullFlag) {
66 if (facing == 1 && ctx->Polygon.CullFaceMode != GL_FRONT)
67 return;
68 if (facing == 0 && ctx->Polygon.CullFaceMode != GL_BACK)
69 return;
70 }
71
72 if (ctx->Light.ShadeModel == GL_FLAT) {
73 COPY_CHAN4(c[0], v0->color);
74 COPY_CHAN4(c[1], v1->color);
75 COPY_CHAN4(s[0], v0->specular);
76 COPY_CHAN4(s[1], v1->specular);
77 i[0] = v0->index;
78 i[1] = v1->index;
79
80 COPY_CHAN4(v0->color, v2->color);
81 COPY_CHAN4(v1->color, v2->color);
82 COPY_CHAN4(v0->specular, v2->specular);
83 COPY_CHAN4(v1->specular, v2->specular);
84 v0->index = v2->index;
85 v1->index = v2->index;
86 }
87
88 if (swsetup->render_prim == GL_POLYGON) {
89 if (ef[e2]) _swrast_Line( ctx, v2, v0 );
90 if (ef[e0]) _swrast_Line( ctx, v0, v1 );
91 if (ef[e1]) _swrast_Line( ctx, v1, v2 );
92 } else {
93 if (ef[e0]) _swrast_Line( ctx, v0, v1 );
94 if (ef[e1]) _swrast_Line( ctx, v1, v2 );
95 if (ef[e2]) _swrast_Line( ctx, v2, v0 );
96 }
97
98 if (ctx->Light.ShadeModel == GL_FLAT) {
99 COPY_CHAN4(v0->color, c[0]);
100 COPY_CHAN4(v1->color, c[1]);
101 COPY_CHAN4(v0->specular, s[0]);
102 COPY_CHAN4(v1->specular, s[1]);
103 v0->index = i[0];
104 v1->index = i[1];
105 }
106 }
107
108 static void _swsetup_render_point_tri( GLcontext *ctx,
109 GLuint e0, GLuint e1, GLuint e2,
110 GLuint facing )
111 {
112 SScontext *swsetup = SWSETUP_CONTEXT(ctx);
113 struct vertex_buffer *VB = &TNL_CONTEXT(ctx)->vb;
114 GLubyte *ef = VB->EdgeFlag;
115 SWvertex *verts = swsetup->verts;
116 SWvertex *v0 = &verts[e0];
117 SWvertex *v1 = &verts[e1];
118 SWvertex *v2 = &verts[e2];
119 GLchan c[2][4];
120 GLchan s[2][4];
121 GLuint i[2];
122
123 /* cull testing */
124 if (ctx->Polygon.CullFlag) {
125 if (facing == 1 && ctx->Polygon.CullFaceMode != GL_FRONT)
126 return;
127 if (facing == 0 && ctx->Polygon.CullFaceMode != GL_BACK)
128 return;
129 }
130
131 if (ctx->Light.ShadeModel == GL_FLAT) {
132 /* save colors/indexes for v0, v1 vertices */
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);
137 i[0] = v0->index;
138 i[1] = v1->index;
139
140 /* copy v2 color/indexes to v0, v1 indexes */
141 COPY_CHAN4(v0->color, v2->color);
142 COPY_CHAN4(v1->color, v2->color);
143 COPY_CHAN4(v0->specular, v2->specular);
144 COPY_CHAN4(v1->specular, v2->specular);
145 v0->index = v2->index;
146 v1->index = v2->index;
147 }
148
149 if (ef[e0]) _swrast_Point( ctx, v0 );
150 if (ef[e1]) _swrast_Point( ctx, v1 );
151 if (ef[e2]) _swrast_Point( ctx, v2 );
152
153 if (ctx->Light.ShadeModel == GL_FLAT) {
154 /* restore v0, v1 colores/indexes */
155 COPY_CHAN4(v0->color, c[0]);
156 COPY_CHAN4(v1->color, c[1]);
157 COPY_CHAN4(v0->specular, s[0]);
158 COPY_CHAN4(v1->specular, s[1]);
159 v0->index = i[0];
160 v1->index = i[1];
161 }
162 _swrast_flush(ctx);
163 }
164
165 #define SS_COLOR(a,b) UNCLAMPED_FLOAT_TO_RGBA_CHAN(a,b)
166 #define SS_SPEC(a,b) UNCLAMPED_FLOAT_TO_RGB_CHAN(a,b)
167 #define SS_IND(a,b) (a = b)
168
169 #define IND (0)
170 #define TAG(x) x
171 #include "ss_tritmp.h"
172
173 #define IND (SS_OFFSET_BIT)
174 #define TAG(x) x##_offset
175 #include "ss_tritmp.h"
176
177 #define IND (SS_TWOSIDE_BIT)
178 #define TAG(x) x##_twoside
179 #include "ss_tritmp.h"
180
181 #define IND (SS_OFFSET_BIT|SS_TWOSIDE_BIT)
182 #define TAG(x) x##_offset_twoside
183 #include "ss_tritmp.h"
184
185 #define IND (SS_UNFILLED_BIT)
186 #define TAG(x) x##_unfilled
187 #include "ss_tritmp.h"
188
189 #define IND (SS_OFFSET_BIT|SS_UNFILLED_BIT)
190 #define TAG(x) x##_offset_unfilled
191 #include "ss_tritmp.h"
192
193 #define IND (SS_TWOSIDE_BIT|SS_UNFILLED_BIT)
194 #define TAG(x) x##_twoside_unfilled
195 #include "ss_tritmp.h"
196
197 #define IND (SS_OFFSET_BIT|SS_TWOSIDE_BIT|SS_UNFILLED_BIT)
198 #define TAG(x) x##_offset_twoside_unfilled
199 #include "ss_tritmp.h"
200
201 #define IND (0|SS_RGBA_BIT)
202 #define TAG(x) x##_rgba
203 #include "ss_tritmp.h"
204
205 #define IND (SS_OFFSET_BIT|SS_RGBA_BIT)
206 #define TAG(x) x##_offset_rgba
207 #include "ss_tritmp.h"
208
209 #define IND (SS_TWOSIDE_BIT|SS_RGBA_BIT)
210 #define TAG(x) x##_twoside_rgba
211 #include "ss_tritmp.h"
212
213 #define IND (SS_OFFSET_BIT|SS_TWOSIDE_BIT|SS_RGBA_BIT)
214 #define TAG(x) x##_offset_twoside_rgba
215 #include "ss_tritmp.h"
216
217 #define IND (SS_UNFILLED_BIT|SS_RGBA_BIT)
218 #define TAG(x) x##_unfilled_rgba
219 #include "ss_tritmp.h"
220
221 #define IND (SS_OFFSET_BIT|SS_UNFILLED_BIT|SS_RGBA_BIT)
222 #define TAG(x) x##_offset_unfilled_rgba
223 #include "ss_tritmp.h"
224
225 #define IND (SS_TWOSIDE_BIT|SS_UNFILLED_BIT|SS_RGBA_BIT)
226 #define TAG(x) x##_twoside_unfilled_rgba
227 #include "ss_tritmp.h"
228
229 #define IND (SS_OFFSET_BIT|SS_TWOSIDE_BIT|SS_UNFILLED_BIT|SS_RGBA_BIT)
230 #define TAG(x) x##_offset_twoside_unfilled_rgba
231 #include "ss_tritmp.h"
232
233
234 void _swsetup_trifuncs_init( GLcontext *ctx )
235 {
236 (void) ctx;
237
238 init();
239 init_offset();
240 init_twoside();
241 init_offset_twoside();
242 init_unfilled();
243 init_offset_unfilled();
244 init_twoside_unfilled();
245 init_offset_twoside_unfilled();
246
247 init_rgba();
248 init_offset_rgba();
249 init_twoside_rgba();
250 init_offset_twoside_rgba();
251 init_unfilled_rgba();
252 init_offset_unfilled_rgba();
253 init_twoside_unfilled_rgba();
254 init_offset_twoside_unfilled_rgba();
255 }
256
257
258 static void swsetup_points( GLcontext *ctx, GLuint first, GLuint last )
259 {
260 struct vertex_buffer *VB = &TNL_CONTEXT(ctx)->vb;
261 SWvertex *verts = SWSETUP_CONTEXT(ctx)->verts;
262 GLuint i;
263
264 if (VB->Elts) {
265 for (i = first; i < last; i++)
266 if (VB->ClipMask[VB->Elts[i]] == 0)
267 _swrast_Point( ctx, &verts[VB->Elts[i]] );
268 }
269 else {
270 for (i = first; i < last; i++)
271 if (VB->ClipMask[i] == 0)
272 _swrast_Point( ctx, &verts[i] );
273 }
274 }
275
276 static void swsetup_line( GLcontext *ctx, GLuint v0, GLuint v1 )
277 {
278 SWvertex *verts = SWSETUP_CONTEXT(ctx)->verts;
279 _swrast_Line( ctx, &verts[v0], &verts[v1] );
280 }
281
282
283
284 void _swsetup_choose_trifuncs( GLcontext *ctx )
285 {
286 TNLcontext *tnl = TNL_CONTEXT(ctx);
287 GLuint ind = 0;
288
289 if (ctx->Polygon.OffsetPoint ||
290 ctx->Polygon.OffsetLine ||
291 ctx->Polygon.OffsetFill)
292 ind |= SS_OFFSET_BIT;
293
294 if (ctx->Light.Enabled && ctx->Light.Model.TwoSide)
295 ind |= SS_TWOSIDE_BIT;
296
297 /* We piggyback the two-sided stencil front/back determination on the
298 * unfilled triangle path.
299 */
300 if (ctx->Polygon.FrontMode != GL_FILL ||
301 ctx->Polygon.BackMode != GL_FILL ||
302 (ctx->Stencil.Enabled && ctx->Stencil.TestTwoSide))
303 ind |= SS_UNFILLED_BIT;
304
305 if (ctx->Visual.rgbMode)
306 ind |= SS_RGBA_BIT;
307
308 tnl->Driver.Render.Triangle = tri_tab[ind];
309 tnl->Driver.Render.Quad = quad_tab[ind];
310 tnl->Driver.Render.Line = swsetup_line;
311 tnl->Driver.Render.Points = swsetup_points;
312
313 ctx->_Facing = 0;
314 }