Cleanup of derived state calculation prior to seperating software T&L
[mesa.git] / src / mesa / swrast / s_aatriangle.c
1 /* $Id: s_aatriangle.c,v 1.3 2000/11/13 20:02:57 keithw Exp $ */
2
3 /*
4 * Mesa 3-D graphics library
5 * Version: 3.5
6 *
7 * Copyright (C) 1999-2000 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
27
28 /*
29 * Antialiased Triangle rasterizers
30 */
31
32
33 #include "s_aatriangle.h"
34 #include "s_context.h"
35 #include "s_span.h"
36
37
38 /*
39 * Compute coefficients of a plane using the X,Y coords of the v0, v1, v2
40 * vertices and the given Z values.
41 */
42 static INLINE void
43 compute_plane(const GLfloat v0[], const GLfloat v1[], const GLfloat v2[],
44 GLfloat z0, GLfloat z1, GLfloat z2, GLfloat plane[4])
45 {
46 const GLfloat px = v1[0] - v0[0];
47 const GLfloat py = v1[1] - v0[1];
48 const GLfloat pz = z1 - z0;
49
50 const GLfloat qx = v2[0] - v0[0];
51 const GLfloat qy = v2[1] - v0[1];
52 const GLfloat qz = z2 - z0;
53
54 const GLfloat a = py * qz - pz * qy;
55 const GLfloat b = pz * qx - px * qz;
56 const GLfloat c = px * qy - py * qx;
57 const GLfloat d = -(a * v0[0] + b * v0[1] + c * z0);
58
59 plane[0] = a;
60 plane[1] = b;
61 plane[2] = c;
62 plane[3] = d;
63 }
64
65
66 /*
67 * Compute coefficients of a plane with a constant Z value.
68 */
69 static INLINE void
70 constant_plane(GLfloat value, GLfloat plane[4])
71 {
72 plane[0] = 0.0;
73 plane[1] = 0.0;
74 plane[2] = -1.0;
75 plane[3] = value;
76 }
77
78 #define CONSTANT_PLANE(VALUE, PLANE) \
79 do { \
80 PLANE[0] = 0.0F; \
81 PLANE[1] = 0.0F; \
82 PLANE[2] = -1.0F; \
83 PLANE[3] = VALUE; \
84 } while (0)
85
86
87
88 /*
89 * Solve plane equation for Z at (X,Y).
90 */
91 static INLINE GLfloat
92 solve_plane(GLfloat x, GLfloat y, const GLfloat plane[4])
93 {
94 GLfloat z = (plane[3] + plane[0] * x + plane[1] * y) / -plane[2];
95 return z;
96 }
97
98
99 #define SOLVE_PLANE(X, Y, PLANE) \
100 ((PLANE[3] + PLANE[0] * (X) + PLANE[1] * (Y)) / -PLANE[2])
101
102
103 /*
104 * Return 1 / solve_plane().
105 */
106 static INLINE GLfloat
107 solve_plane_recip(GLfloat x, GLfloat y, const GLfloat plane[4])
108 {
109 GLfloat z = -plane[2] / (plane[3] + plane[0] * x + plane[1] * y);
110 return z;
111 }
112
113
114
115 /*
116 * Solve plane and return clamped GLchan value.
117 */
118 static INLINE GLchan
119 solve_plane_chan(GLfloat x, GLfloat y, const GLfloat plane[4])
120 {
121 GLfloat z = (plane[3] + plane[0] * x + plane[1] * y) / -plane[2] + 0.5F;
122 if (z < 0.0F)
123 return 0;
124 else if (z > CHAN_MAXF)
125 return CHAN_MAXF;
126 return (GLchan) (GLint) z;
127 }
128
129
130
131 /*
132 * Compute how much (area) of the given pixel is inside the triangle.
133 * Vertices MUST be specified in counter-clockwise order.
134 * Return: coverage in [0, 1].
135 */
136 static GLfloat
137 compute_coveragef(const GLfloat v0[3], const GLfloat v1[3],
138 const GLfloat v2[3], GLint winx, GLint winy)
139 {
140 static const GLfloat samples[16][2] = {
141 /* start with the four corners */
142 { 0.00, 0.00 },
143 { 0.75, 0.00 },
144 { 0.00, 0.75 },
145 { 0.75, 0.75 },
146 /* continue with interior samples */
147 { 0.25, 0.00 },
148 { 0.50, 0.00 },
149 { 0.00, 0.25 },
150 { 0.25, 0.25 },
151 { 0.50, 0.25 },
152 { 0.75, 0.25 },
153 { 0.00, 0.50 },
154 { 0.25, 0.50 },
155 { 0.50, 0.50 },
156 { 0.75, 0.50 },
157 { 0.25, 0.75 },
158 { 0.50, 0.75 }
159 };
160 const GLfloat x = (GLfloat) winx;
161 const GLfloat y = (GLfloat) winy;
162 const GLfloat dx0 = v1[0] - v0[0];
163 const GLfloat dy0 = v1[1] - v0[1];
164 const GLfloat dx1 = v2[0] - v1[0];
165 const GLfloat dy1 = v2[1] - v1[1];
166 const GLfloat dx2 = v0[0] - v2[0];
167 const GLfloat dy2 = v0[1] - v2[1];
168 GLint stop = 4, i;
169 GLfloat insideCount = 16.0F;
170
171 #ifdef DEBUG
172 {
173 const GLfloat area = dx0 * dy1 - dx1 * dy0;
174 assert(area >= 0.0);
175 }
176 #endif
177
178 for (i = 0; i < stop; i++) {
179 const GLfloat sx = x + samples[i][0];
180 const GLfloat sy = y + samples[i][1];
181 const GLfloat fx0 = sx - v0[0];
182 const GLfloat fy0 = sy - v0[1];
183 const GLfloat fx1 = sx - v1[0];
184 const GLfloat fy1 = sy - v1[1];
185 const GLfloat fx2 = sx - v2[0];
186 const GLfloat fy2 = sy - v2[1];
187 /* cross product determines if sample is inside or outside each edge */
188 GLfloat cross0 = (dx0 * fy0 - dy0 * fx0);
189 GLfloat cross1 = (dx1 * fy1 - dy1 * fx1);
190 GLfloat cross2 = (dx2 * fy2 - dy2 * fx2);
191 /* Check if the sample is exactly on an edge. If so, let cross be a
192 * positive or negative value depending on the direction of the edge.
193 */
194 if (cross0 == 0.0F)
195 cross0 = dx0 + dy0;
196 if (cross1 == 0.0F)
197 cross1 = dx1 + dy1;
198 if (cross2 == 0.0F)
199 cross2 = dx2 + dy2;
200 if (cross0 < 0.0F || cross1 < 0.0F || cross2 < 0.0F) {
201 /* point is outside triangle */
202 insideCount -= 1.0F;
203 stop = 16;
204 }
205 }
206 if (stop == 4)
207 return 1.0F;
208 else
209 return insideCount * (1.0F / 16.0F);
210 }
211
212
213
214 /*
215 * Compute how much (area) of the given pixel is inside the triangle.
216 * Vertices MUST be specified in counter-clockwise order.
217 * Return: coverage in [0, 15].
218 */
219 static GLint
220 compute_coveragei(const GLfloat v0[3], const GLfloat v1[3],
221 const GLfloat v2[3], GLint winx, GLint winy)
222 {
223 /* NOTE: 15 samples instead of 16.
224 * A better sample distribution could be used.
225 */
226 static const GLfloat samples[15][2] = {
227 /* start with the four corners */
228 { 0.00, 0.00 },
229 { 0.75, 0.00 },
230 { 0.00, 0.75 },
231 { 0.75, 0.75 },
232 /* continue with interior samples */
233 { 0.25, 0.00 },
234 { 0.50, 0.00 },
235 { 0.00, 0.25 },
236 { 0.25, 0.25 },
237 { 0.50, 0.25 },
238 { 0.75, 0.25 },
239 { 0.00, 0.50 },
240 { 0.25, 0.50 },
241 /*{ 0.50, 0.50 },*/
242 { 0.75, 0.50 },
243 { 0.25, 0.75 },
244 { 0.50, 0.75 }
245 };
246 const GLfloat x = (GLfloat) winx;
247 const GLfloat y = (GLfloat) winy;
248 const GLfloat dx0 = v1[0] - v0[0];
249 const GLfloat dy0 = v1[1] - v0[1];
250 const GLfloat dx1 = v2[0] - v1[0];
251 const GLfloat dy1 = v2[1] - v1[1];
252 const GLfloat dx2 = v0[0] - v2[0];
253 const GLfloat dy2 = v0[1] - v2[1];
254 GLint stop = 4, i;
255 GLint insideCount = 15;
256
257 #ifdef DEBUG
258 {
259 const GLfloat area = dx0 * dy1 - dx1 * dy0;
260 assert(area >= 0.0);
261 }
262 #endif
263
264 for (i = 0; i < stop; i++) {
265 const GLfloat sx = x + samples[i][0];
266 const GLfloat sy = y + samples[i][1];
267 const GLfloat fx0 = sx - v0[0];
268 const GLfloat fy0 = sy - v0[1];
269 const GLfloat fx1 = sx - v1[0];
270 const GLfloat fy1 = sy - v1[1];
271 const GLfloat fx2 = sx - v2[0];
272 const GLfloat fy2 = sy - v2[1];
273 /* cross product determines if sample is inside or outside each edge */
274 GLfloat cross0 = (dx0 * fy0 - dy0 * fx0);
275 GLfloat cross1 = (dx1 * fy1 - dy1 * fx1);
276 GLfloat cross2 = (dx2 * fy2 - dy2 * fx2);
277 /* Check if the sample is exactly on an edge. If so, let cross be a
278 * positive or negative value depending on the direction of the edge.
279 */
280 if (cross0 == 0.0F)
281 cross0 = dx0 + dy0;
282 if (cross1 == 0.0F)
283 cross1 = dx1 + dy1;
284 if (cross2 == 0.0F)
285 cross2 = dx2 + dy2;
286 if (cross0 < 0.0F || cross1 < 0.0F || cross2 < 0.0F) {
287 /* point is outside triangle */
288 insideCount--;
289 stop = 15;
290 }
291 }
292 if (stop == 4)
293 return 15;
294 else
295 return insideCount;
296 }
297
298
299
300 static void
301 rgba_aa_tri(GLcontext *ctx,
302 SWvertex *v0,
303 SWvertex *v1,
304 SWvertex *v2)
305 {
306 #define DO_Z
307 #define DO_RGBA
308 #include "s_aatritemp.h"
309 }
310
311
312 static void
313 index_aa_tri(GLcontext *ctx,
314 SWvertex *v0,
315 SWvertex *v1,
316 SWvertex *v2)
317 {
318 #define DO_Z
319 #define DO_INDEX
320 #include "s_aatritemp.h"
321 }
322
323
324 /*
325 * Compute mipmap level of detail.
326 */
327 static INLINE GLfloat
328 compute_lambda(const GLfloat sPlane[4], const GLfloat tPlane[4],
329 GLfloat invQ, GLfloat width, GLfloat height)
330 {
331 GLfloat dudx = sPlane[0] / sPlane[2] * invQ * width;
332 GLfloat dudy = sPlane[1] / sPlane[2] * invQ * width;
333 GLfloat dvdx = tPlane[0] / tPlane[2] * invQ * height;
334 GLfloat dvdy = tPlane[1] / tPlane[2] * invQ * height;
335 GLfloat r1 = dudx * dudx + dudy * dudy;
336 GLfloat r2 = dvdx * dvdx + dvdy * dvdy;
337 GLfloat rho2 = r1 + r2;
338 /* return log base 2 of rho */
339 return log(rho2) * 1.442695 * 0.5; /* 1.442695 = 1/log(2) */
340 }
341
342
343 static void
344 tex_aa_tri(GLcontext *ctx,
345 SWvertex *v0,
346 SWvertex *v1,
347 SWvertex *v2)
348 {
349 #define DO_Z
350 #define DO_RGBA
351 #define DO_TEX
352 #include "s_aatritemp.h"
353 }
354
355
356 static void
357 spec_tex_aa_tri(GLcontext *ctx,
358 SWvertex *v0,
359 SWvertex *v1,
360 SWvertex *v2)
361 {
362 #define DO_Z
363 #define DO_RGBA
364 #define DO_TEX
365 #define DO_SPEC
366 #include "s_aatritemp.h"
367 }
368
369
370 static void
371 multitex_aa_tri(GLcontext *ctx,
372 SWvertex *v0,
373 SWvertex *v1,
374 SWvertex *v2)
375 {
376 #define DO_Z
377 #define DO_RGBA
378 #define DO_MULTITEX
379 #include "s_aatritemp.h"
380 }
381
382 static void
383 spec_multitex_aa_tri(GLcontext *ctx,
384 SWvertex *v0,
385 SWvertex *v1,
386 SWvertex *v2)
387 {
388 #define DO_Z
389 #define DO_RGBA
390 #define DO_MULTITEX
391 #define DO_SPEC
392 #include "s_aatritemp.h"
393 }
394
395
396 /*
397 * Examine GL state and set ctx->Driver.TriangleFunc to an
398 * appropriate antialiased triangle rasterizer function.
399 */
400 void
401 _mesa_set_aa_triangle_function(GLcontext *ctx)
402 {
403 SWcontext *swrast = SWRAST_CONTEXT(ctx);
404 ASSERT(ctx->Polygon.SmoothFlag);
405
406 if (ctx->Texture._ReallyEnabled) {
407 if (ctx->Light.Enabled &&
408 (ctx->Light.Model.ColorControl == GL_SEPARATE_SPECULAR_COLOR ||
409 ctx->Fog.ColorSumEnabled)) {
410 if (swrast->_MultiTextureEnabled) {
411 SWRAST_CONTEXT(ctx)->Triangle = spec_multitex_aa_tri;
412 }
413 else {
414 SWRAST_CONTEXT(ctx)->Triangle = spec_tex_aa_tri;
415 }
416 }
417 else {
418 if (swrast->_MultiTextureEnabled) {
419 SWRAST_CONTEXT(ctx)->Triangle = multitex_aa_tri;
420 }
421 else {
422 SWRAST_CONTEXT(ctx)->Triangle = tex_aa_tri;
423 }
424 }
425 }
426 else {
427 if (ctx->Visual.RGBAflag) {
428 SWRAST_CONTEXT(ctx)->Triangle = rgba_aa_tri;
429 }
430 else {
431 SWRAST_CONTEXT(ctx)->Triangle = index_aa_tri;
432 }
433 }
434 ASSERT(SWRAST_CONTEXT(ctx)->Triangle);
435 }