f96ddd45b5fde01764d73472437142186c842700
[mesa.git] / src / mesa / swrast / s_tritemp.h
1 /*
2 * Mesa 3-D graphics library
3 * Version: 7.0
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 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
21 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
22 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23 * OTHER DEALINGS IN THE SOFTWARE.
24 */
25
26 /*
27 * Triangle Rasterizer Template
28 *
29 * This file is #include'd to generate custom triangle rasterizers.
30 *
31 * The following macros may be defined to indicate what auxillary information
32 * must be interpolated across the triangle:
33 * INTERP_Z - if defined, interpolate integer Z values
34 * INTERP_RGB - if defined, interpolate integer RGB values
35 * INTERP_ALPHA - if defined, interpolate integer Alpha values
36 * INTERP_INT_TEX - if defined, interpolate integer ST texcoords
37 * (fast, simple 2-D texture mapping, without
38 * perspective correction)
39 * INTERP_ATTRIBS - if defined, interpolate arbitrary attribs (texcoords,
40 * varying vars, etc) This also causes W to be
41 * computed for perspective correction).
42 *
43 * When one can directly address pixels in the color buffer the following
44 * macros can be defined and used to compute pixel addresses during
45 * rasterization (see pRow):
46 * PIXEL_TYPE - the datatype of a pixel (GLubyte, GLushort, GLuint)
47 * BYTES_PER_ROW - number of bytes per row in the color buffer
48 * PIXEL_ADDRESS(X,Y) - returns the address of pixel at (X,Y) where
49 * Y==0 at bottom of screen and increases upward.
50 *
51 * Similarly, for direct depth buffer access, this type is used for depth
52 * buffer addressing (see zRow):
53 * DEPTH_TYPE - either GLushort or GLuint
54 *
55 * Optionally, one may provide one-time setup code per triangle:
56 * SETUP_CODE - code which is to be executed once per triangle
57 *
58 * The following macro MUST be defined:
59 * RENDER_SPAN(span) - code to write a span of pixels.
60 *
61 * This code was designed for the origin to be in the lower-left corner.
62 *
63 * Inspired by triangle rasterizer code written by Allen Akin. Thanks Allen!
64 *
65 *
66 * Some notes on rasterization accuracy:
67 *
68 * This code uses fixed point arithmetic (the GLfixed type) to iterate
69 * over the triangle edges and interpolate ancillary data (such as Z,
70 * color, secondary color, etc). The number of fractional bits in
71 * GLfixed and the value of SUB_PIXEL_BITS has a direct bearing on the
72 * accuracy of rasterization.
73 *
74 * If SUB_PIXEL_BITS=4 then we'll snap the vertices to the nearest
75 * 1/16 of a pixel. If we're walking up a long, nearly vertical edge
76 * (dx=1/16, dy=1024) we'll need 4 + 10 = 14 fractional bits in
77 * GLfixed to walk the edge without error. If the maximum viewport
78 * height is 4K pixels, then we'll need 4 + 12 = 16 fractional bits.
79 *
80 * Historically, Mesa has used 11 fractional bits in GLfixed, snaps
81 * vertices to 1/16 pixel and allowed a maximum viewport height of 2K
82 * pixels. 11 fractional bits is actually insufficient for accurately
83 * rasterizing some triangles. More recently, the maximum viewport
84 * height was increased to 4K pixels. Thus, Mesa should be using 16
85 * fractional bits in GLfixed. Unfortunately, there may be some issues
86 * with setting FIXED_FRAC_BITS=16, such as multiplication overflow.
87 * This will have to be examined in some detail...
88 *
89 * For now, if you find rasterization errors, particularly with tall,
90 * sliver triangles, try increasing FIXED_FRAC_BITS and/or decreasing
91 * SUB_PIXEL_BITS.
92 */
93
94
95 /*
96 * Some code we unfortunately need to prevent negative interpolated colors.
97 */
98 #ifndef CLAMP_INTERPOLANT
99 #define CLAMP_INTERPOLANT(CHANNEL, CHANNELSTEP, LEN) \
100 do { \
101 GLfixed endVal = span.CHANNEL + (LEN) * span.CHANNELSTEP; \
102 if (endVal < 0) { \
103 span.CHANNEL -= endVal; \
104 } \
105 if (span.CHANNEL < 0) { \
106 span.CHANNEL = 0; \
107 } \
108 } while (0)
109 #endif
110
111
112 static void NAME(struct gl_context *ctx, const SWvertex *v0,
113 const SWvertex *v1,
114 const SWvertex *v2 )
115 {
116 typedef struct {
117 const SWvertex *v0, *v1; /* Y(v0) < Y(v1) */
118 GLfloat dx; /* X(v1) - X(v0) */
119 GLfloat dy; /* Y(v1) - Y(v0) */
120 GLfloat dxdy; /* dx/dy */
121 GLfixed fdxdy; /* dx/dy in fixed-point */
122 GLfloat adjy; /* adjust from v[0]->fy to fsy, scaled */
123 GLfixed fsx; /* first sample point x coord */
124 GLfixed fsy;
125 GLfixed fx0; /* fixed pt X of lower endpoint */
126 GLint lines; /* number of lines to be sampled on this edge */
127 } EdgeT;
128
129 const SWcontext *swrast = SWRAST_CONTEXT(ctx);
130 #ifdef INTERP_Z
131 const GLint depthBits = ctx->DrawBuffer->Visual.depthBits;
132 const GLint fixedToDepthShift = depthBits <= 16 ? FIXED_SHIFT : 0;
133 const GLfloat maxDepth = ctx->DrawBuffer->_DepthMaxF;
134 #define FixedToDepth(F) ((F) >> fixedToDepthShift)
135 #endif
136 EdgeT eMaj, eTop, eBot;
137 GLfloat oneOverArea;
138 const SWvertex *vMin, *vMid, *vMax; /* Y(vMin)<=Y(vMid)<=Y(vMax) */
139 GLfloat bf = SWRAST_CONTEXT(ctx)->_BackfaceSign;
140 const GLint snapMask = ~((FIXED_ONE / (1 << SUB_PIXEL_BITS)) - 1); /* for x/y coord snapping */
141 GLfixed vMin_fx, vMin_fy, vMid_fx, vMid_fy, vMax_fx, vMax_fy;
142
143 SWspan span;
144
145 (void) swrast;
146
147 INIT_SPAN(span, GL_POLYGON);
148 span.y = 0; /* silence warnings */
149
150 #ifdef INTERP_Z
151 (void) fixedToDepthShift;
152 #endif
153
154 /*
155 printf("%s()\n", __FUNCTION__);
156 printf(" %g, %g, %g\n",
157 v0->attrib[VARYING_SLOT_POS][0],
158 v0->attrib[VARYING_SLOT_POS][1],
159 v0->attrib[VARYING_SLOT_POS][2]);
160 printf(" %g, %g, %g\n",
161 v1->attrib[VARYING_SLOT_POS][0],
162 v1->attrib[VARYING_SLOT_POS][1],
163 v1->attrib[VARYING_SLOT_POS][2]);
164 printf(" %g, %g, %g\n",
165 v2->attrib[VARYING_SLOT_POS][0],
166 v2->attrib[VARYING_SLOT_POS][1],
167 v2->attrib[VARYING_SLOT_POS][2]);
168 */
169
170 /* Compute fixed point x,y coords w/ half-pixel offsets and snapping.
171 * And find the order of the 3 vertices along the Y axis.
172 */
173 {
174 const GLfixed fy0 = FloatToFixed(v0->attrib[VARYING_SLOT_POS][1] - 0.5F) & snapMask;
175 const GLfixed fy1 = FloatToFixed(v1->attrib[VARYING_SLOT_POS][1] - 0.5F) & snapMask;
176 const GLfixed fy2 = FloatToFixed(v2->attrib[VARYING_SLOT_POS][1] - 0.5F) & snapMask;
177 if (fy0 <= fy1) {
178 if (fy1 <= fy2) {
179 /* y0 <= y1 <= y2 */
180 vMin = v0; vMid = v1; vMax = v2;
181 vMin_fy = fy0; vMid_fy = fy1; vMax_fy = fy2;
182 }
183 else if (fy2 <= fy0) {
184 /* y2 <= y0 <= y1 */
185 vMin = v2; vMid = v0; vMax = v1;
186 vMin_fy = fy2; vMid_fy = fy0; vMax_fy = fy1;
187 }
188 else {
189 /* y0 <= y2 <= y1 */
190 vMin = v0; vMid = v2; vMax = v1;
191 vMin_fy = fy0; vMid_fy = fy2; vMax_fy = fy1;
192 bf = -bf;
193 }
194 }
195 else {
196 if (fy0 <= fy2) {
197 /* y1 <= y0 <= y2 */
198 vMin = v1; vMid = v0; vMax = v2;
199 vMin_fy = fy1; vMid_fy = fy0; vMax_fy = fy2;
200 bf = -bf;
201 }
202 else if (fy2 <= fy1) {
203 /* y2 <= y1 <= y0 */
204 vMin = v2; vMid = v1; vMax = v0;
205 vMin_fy = fy2; vMid_fy = fy1; vMax_fy = fy0;
206 bf = -bf;
207 }
208 else {
209 /* y1 <= y2 <= y0 */
210 vMin = v1; vMid = v2; vMax = v0;
211 vMin_fy = fy1; vMid_fy = fy2; vMax_fy = fy0;
212 }
213 }
214
215 /* fixed point X coords */
216 vMin_fx = FloatToFixed(vMin->attrib[VARYING_SLOT_POS][0] + 0.5F) & snapMask;
217 vMid_fx = FloatToFixed(vMid->attrib[VARYING_SLOT_POS][0] + 0.5F) & snapMask;
218 vMax_fx = FloatToFixed(vMax->attrib[VARYING_SLOT_POS][0] + 0.5F) & snapMask;
219 }
220
221 /* vertex/edge relationship */
222 eMaj.v0 = vMin; eMaj.v1 = vMax; /*TODO: .v1's not needed */
223 eTop.v0 = vMid; eTop.v1 = vMax;
224 eBot.v0 = vMin; eBot.v1 = vMid;
225
226 /* compute deltas for each edge: vertex[upper] - vertex[lower] */
227 eMaj.dx = FixedToFloat(vMax_fx - vMin_fx);
228 eMaj.dy = FixedToFloat(vMax_fy - vMin_fy);
229 eTop.dx = FixedToFloat(vMax_fx - vMid_fx);
230 eTop.dy = FixedToFloat(vMax_fy - vMid_fy);
231 eBot.dx = FixedToFloat(vMid_fx - vMin_fx);
232 eBot.dy = FixedToFloat(vMid_fy - vMin_fy);
233
234 /* compute area, oneOverArea and perform backface culling */
235 {
236 const GLfloat area = eMaj.dx * eBot.dy - eBot.dx * eMaj.dy;
237
238 if (IS_INF_OR_NAN(area) || area == 0.0F)
239 return;
240
241 if (area * bf * swrast->_BackfaceCullSign < 0.0)
242 return;
243
244 oneOverArea = 1.0F / area;
245
246 /* 0 = front, 1 = back */
247 span.facing = oneOverArea * bf > 0.0F;
248 }
249
250 /* Edge setup. For a triangle strip these could be reused... */
251 {
252 eMaj.fsy = FixedCeil(vMin_fy);
253 eMaj.lines = FixedToInt(FixedCeil(vMax_fy - eMaj.fsy));
254 if (eMaj.lines > 0) {
255 eMaj.dxdy = eMaj.dx / eMaj.dy;
256 eMaj.fdxdy = SignedFloatToFixed(eMaj.dxdy);
257 eMaj.adjy = (GLfloat) (eMaj.fsy - vMin_fy); /* SCALED! */
258 eMaj.fx0 = vMin_fx;
259 eMaj.fsx = eMaj.fx0 + (GLfixed) (eMaj.adjy * eMaj.dxdy);
260 }
261 else {
262 return; /*CULLED*/
263 }
264
265 eTop.fsy = FixedCeil(vMid_fy);
266 eTop.lines = FixedToInt(FixedCeil(vMax_fy - eTop.fsy));
267 if (eTop.lines > 0) {
268 eTop.dxdy = eTop.dx / eTop.dy;
269 eTop.fdxdy = SignedFloatToFixed(eTop.dxdy);
270 eTop.adjy = (GLfloat) (eTop.fsy - vMid_fy); /* SCALED! */
271 eTop.fx0 = vMid_fx;
272 eTop.fsx = eTop.fx0 + (GLfixed) (eTop.adjy * eTop.dxdy);
273 }
274
275 eBot.fsy = FixedCeil(vMin_fy);
276 eBot.lines = FixedToInt(FixedCeil(vMid_fy - eBot.fsy));
277 if (eBot.lines > 0) {
278 eBot.dxdy = eBot.dx / eBot.dy;
279 eBot.fdxdy = SignedFloatToFixed(eBot.dxdy);
280 eBot.adjy = (GLfloat) (eBot.fsy - vMin_fy); /* SCALED! */
281 eBot.fx0 = vMin_fx;
282 eBot.fsx = eBot.fx0 + (GLfixed) (eBot.adjy * eBot.dxdy);
283 }
284 }
285
286 /*
287 * Conceptually, we view a triangle as two subtriangles
288 * separated by a perfectly horizontal line. The edge that is
289 * intersected by this line is one with maximal absolute dy; we
290 * call it a ``major'' edge. The other two edges are the
291 * ``top'' edge (for the upper subtriangle) and the ``bottom''
292 * edge (for the lower subtriangle). If either of these two
293 * edges is horizontal or very close to horizontal, the
294 * corresponding subtriangle might cover zero sample points;
295 * we take care to handle such cases, for performance as well
296 * as correctness.
297 *
298 * By stepping rasterization parameters along the major edge,
299 * we can avoid recomputing them at the discontinuity where
300 * the top and bottom edges meet. However, this forces us to
301 * be able to scan both left-to-right and right-to-left.
302 * Also, we must determine whether the major edge is at the
303 * left or right side of the triangle. We do this by
304 * computing the magnitude of the cross-product of the major
305 * and top edges. Since this magnitude depends on the sine of
306 * the angle between the two edges, its sign tells us whether
307 * we turn to the left or to the right when travelling along
308 * the major edge to the top edge, and from this we infer
309 * whether the major edge is on the left or the right.
310 *
311 * Serendipitously, this cross-product magnitude is also a
312 * value we need to compute the iteration parameter
313 * derivatives for the triangle, and it can be used to perform
314 * backface culling because its sign tells us whether the
315 * triangle is clockwise or counterclockwise. In this code we
316 * refer to it as ``area'' because it's also proportional to
317 * the pixel area of the triangle.
318 */
319
320 {
321 GLint scan_from_left_to_right; /* true if scanning left-to-right */
322
323 /*
324 * Execute user-supplied setup code
325 */
326 #ifdef SETUP_CODE
327 SETUP_CODE
328 #endif
329
330 scan_from_left_to_right = (oneOverArea < 0.0F);
331
332
333 /* compute d?/dx and d?/dy derivatives */
334 #ifdef INTERP_Z
335 span.interpMask |= SPAN_Z;
336 {
337 GLfloat eMaj_dz = vMax->attrib[VARYING_SLOT_POS][2] - vMin->attrib[VARYING_SLOT_POS][2];
338 GLfloat eBot_dz = vMid->attrib[VARYING_SLOT_POS][2] - vMin->attrib[VARYING_SLOT_POS][2];
339 span.attrStepX[VARYING_SLOT_POS][2] = oneOverArea * (eMaj_dz * eBot.dy - eMaj.dy * eBot_dz);
340 if (span.attrStepX[VARYING_SLOT_POS][2] > maxDepth ||
341 span.attrStepX[VARYING_SLOT_POS][2] < -maxDepth) {
342 /* probably a sliver triangle */
343 span.attrStepX[VARYING_SLOT_POS][2] = 0.0;
344 span.attrStepY[VARYING_SLOT_POS][2] = 0.0;
345 }
346 else {
347 span.attrStepY[VARYING_SLOT_POS][2] = oneOverArea * (eMaj.dx * eBot_dz - eMaj_dz * eBot.dx);
348 }
349 if (depthBits <= 16)
350 span.zStep = SignedFloatToFixed(span.attrStepX[VARYING_SLOT_POS][2]);
351 else
352 span.zStep = (GLint) span.attrStepX[VARYING_SLOT_POS][2];
353 }
354 #endif
355 #ifdef INTERP_RGB
356 span.interpMask |= SPAN_RGBA;
357 if (ctx->Light.ShadeModel == GL_SMOOTH) {
358 GLfloat eMaj_dr = (GLfloat) (vMax->color[RCOMP] - vMin->color[RCOMP]);
359 GLfloat eBot_dr = (GLfloat) (vMid->color[RCOMP] - vMin->color[RCOMP]);
360 GLfloat eMaj_dg = (GLfloat) (vMax->color[GCOMP] - vMin->color[GCOMP]);
361 GLfloat eBot_dg = (GLfloat) (vMid->color[GCOMP] - vMin->color[GCOMP]);
362 GLfloat eMaj_db = (GLfloat) (vMax->color[BCOMP] - vMin->color[BCOMP]);
363 GLfloat eBot_db = (GLfloat) (vMid->color[BCOMP] - vMin->color[BCOMP]);
364 # ifdef INTERP_ALPHA
365 GLfloat eMaj_da = (GLfloat) (vMax->color[ACOMP] - vMin->color[ACOMP]);
366 GLfloat eBot_da = (GLfloat) (vMid->color[ACOMP] - vMin->color[ACOMP]);
367 # endif
368 span.attrStepX[VARYING_SLOT_COL0][0] = oneOverArea * (eMaj_dr * eBot.dy - eMaj.dy * eBot_dr);
369 span.attrStepY[VARYING_SLOT_COL0][0] = oneOverArea * (eMaj.dx * eBot_dr - eMaj_dr * eBot.dx);
370 span.attrStepX[VARYING_SLOT_COL0][1] = oneOverArea * (eMaj_dg * eBot.dy - eMaj.dy * eBot_dg);
371 span.attrStepY[VARYING_SLOT_COL0][1] = oneOverArea * (eMaj.dx * eBot_dg - eMaj_dg * eBot.dx);
372 span.attrStepX[VARYING_SLOT_COL0][2] = oneOverArea * (eMaj_db * eBot.dy - eMaj.dy * eBot_db);
373 span.attrStepY[VARYING_SLOT_COL0][2] = oneOverArea * (eMaj.dx * eBot_db - eMaj_db * eBot.dx);
374 span.redStep = SignedFloatToFixed(span.attrStepX[VARYING_SLOT_COL0][0]);
375 span.greenStep = SignedFloatToFixed(span.attrStepX[VARYING_SLOT_COL0][1]);
376 span.blueStep = SignedFloatToFixed(span.attrStepX[VARYING_SLOT_COL0][2]);
377 # ifdef INTERP_ALPHA
378 span.attrStepX[VARYING_SLOT_COL0][3] = oneOverArea * (eMaj_da * eBot.dy - eMaj.dy * eBot_da);
379 span.attrStepY[VARYING_SLOT_COL0][3] = oneOverArea * (eMaj.dx * eBot_da - eMaj_da * eBot.dx);
380 span.alphaStep = SignedFloatToFixed(span.attrStepX[VARYING_SLOT_COL0][3]);
381 # endif /* INTERP_ALPHA */
382 }
383 else {
384 ASSERT(ctx->Light.ShadeModel == GL_FLAT);
385 span.interpMask |= SPAN_FLAT;
386 span.attrStepX[VARYING_SLOT_COL0][0] = span.attrStepY[VARYING_SLOT_COL0][0] = 0.0F;
387 span.attrStepX[VARYING_SLOT_COL0][1] = span.attrStepY[VARYING_SLOT_COL0][1] = 0.0F;
388 span.attrStepX[VARYING_SLOT_COL0][2] = span.attrStepY[VARYING_SLOT_COL0][2] = 0.0F;
389 span.redStep = 0;
390 span.greenStep = 0;
391 span.blueStep = 0;
392 # ifdef INTERP_ALPHA
393 span.attrStepX[VARYING_SLOT_COL0][3] = span.attrStepY[VARYING_SLOT_COL0][3] = 0.0F;
394 span.alphaStep = 0;
395 # endif
396 }
397 #endif /* INTERP_RGB */
398 #ifdef INTERP_INT_TEX
399 {
400 GLfloat eMaj_ds = (vMax->attrib[VARYING_SLOT_TEX0][0] - vMin->attrib[VARYING_SLOT_TEX0][0]) * S_SCALE;
401 GLfloat eBot_ds = (vMid->attrib[VARYING_SLOT_TEX0][0] - vMin->attrib[VARYING_SLOT_TEX0][0]) * S_SCALE;
402 GLfloat eMaj_dt = (vMax->attrib[VARYING_SLOT_TEX0][1] - vMin->attrib[VARYING_SLOT_TEX0][1]) * T_SCALE;
403 GLfloat eBot_dt = (vMid->attrib[VARYING_SLOT_TEX0][1] - vMin->attrib[VARYING_SLOT_TEX0][1]) * T_SCALE;
404 span.attrStepX[VARYING_SLOT_TEX0][0] = oneOverArea * (eMaj_ds * eBot.dy - eMaj.dy * eBot_ds);
405 span.attrStepY[VARYING_SLOT_TEX0][0] = oneOverArea * (eMaj.dx * eBot_ds - eMaj_ds * eBot.dx);
406 span.attrStepX[VARYING_SLOT_TEX0][1] = oneOverArea * (eMaj_dt * eBot.dy - eMaj.dy * eBot_dt);
407 span.attrStepY[VARYING_SLOT_TEX0][1] = oneOverArea * (eMaj.dx * eBot_dt - eMaj_dt * eBot.dx);
408 span.intTexStep[0] = SignedFloatToFixed(span.attrStepX[VARYING_SLOT_TEX0][0]);
409 span.intTexStep[1] = SignedFloatToFixed(span.attrStepX[VARYING_SLOT_TEX0][1]);
410 }
411 #endif
412 #ifdef INTERP_ATTRIBS
413 {
414 /* attrib[VARYING_SLOT_POS][3] is 1/W */
415 const GLfloat wMax = vMax->attrib[VARYING_SLOT_POS][3];
416 const GLfloat wMin = vMin->attrib[VARYING_SLOT_POS][3];
417 const GLfloat wMid = vMid->attrib[VARYING_SLOT_POS][3];
418 {
419 const GLfloat eMaj_dw = wMax - wMin;
420 const GLfloat eBot_dw = wMid - wMin;
421 span.attrStepX[VARYING_SLOT_POS][3] = oneOverArea * (eMaj_dw * eBot.dy - eMaj.dy * eBot_dw);
422 span.attrStepY[VARYING_SLOT_POS][3] = oneOverArea * (eMaj.dx * eBot_dw - eMaj_dw * eBot.dx);
423 }
424 ATTRIB_LOOP_BEGIN
425 if (swrast->_InterpMode[attr] == GL_FLAT) {
426 ASSIGN_4V(span.attrStepX[attr], 0.0, 0.0, 0.0, 0.0);
427 ASSIGN_4V(span.attrStepY[attr], 0.0, 0.0, 0.0, 0.0);
428 }
429 else {
430 GLuint c;
431 for (c = 0; c < 4; c++) {
432 GLfloat eMaj_da = vMax->attrib[attr][c] * wMax - vMin->attrib[attr][c] * wMin;
433 GLfloat eBot_da = vMid->attrib[attr][c] * wMid - vMin->attrib[attr][c] * wMin;
434 span.attrStepX[attr][c] = oneOverArea * (eMaj_da * eBot.dy - eMaj.dy * eBot_da);
435 span.attrStepY[attr][c] = oneOverArea * (eMaj.dx * eBot_da - eMaj_da * eBot.dx);
436 }
437 }
438 ATTRIB_LOOP_END
439 }
440 #endif
441
442 /*
443 * We always sample at pixel centers. However, we avoid
444 * explicit half-pixel offsets in this code by incorporating
445 * the proper offset in each of x and y during the
446 * transformation to window coordinates.
447 *
448 * We also apply the usual rasterization rules to prevent
449 * cracks and overlaps. A pixel is considered inside a
450 * subtriangle if it meets all of four conditions: it is on or
451 * to the right of the left edge, strictly to the left of the
452 * right edge, on or below the top edge, and strictly above
453 * the bottom edge. (Some edges may be degenerate.)
454 *
455 * The following discussion assumes left-to-right scanning
456 * (that is, the major edge is on the left); the right-to-left
457 * case is a straightforward variation.
458 *
459 * We start by finding the half-integral y coordinate that is
460 * at or below the top of the triangle. This gives us the
461 * first scan line that could possibly contain pixels that are
462 * inside the triangle.
463 *
464 * Next we creep down the major edge until we reach that y,
465 * and compute the corresponding x coordinate on the edge.
466 * Then we find the half-integral x that lies on or just
467 * inside the edge. This is the first pixel that might lie in
468 * the interior of the triangle. (We won't know for sure
469 * until we check the other edges.)
470 *
471 * As we rasterize the triangle, we'll step down the major
472 * edge. For each step in y, we'll move an integer number
473 * of steps in x. There are two possible x step sizes, which
474 * we'll call the ``inner'' step (guaranteed to land on the
475 * edge or inside it) and the ``outer'' step (guaranteed to
476 * land on the edge or outside it). The inner and outer steps
477 * differ by one. During rasterization we maintain an error
478 * term that indicates our distance from the true edge, and
479 * select either the inner step or the outer step, whichever
480 * gets us to the first pixel that falls inside the triangle.
481 *
482 * All parameters (z, red, etc.) as well as the buffer
483 * addresses for color and z have inner and outer step values,
484 * so that we can increment them appropriately. This method
485 * eliminates the need to adjust parameters by creeping a
486 * sub-pixel amount into the triangle at each scanline.
487 */
488
489 {
490 GLint subTriangle;
491 GLfixed fxLeftEdge = 0, fxRightEdge = 0;
492 GLfixed fdxLeftEdge = 0, fdxRightEdge = 0;
493 GLfixed fError = 0, fdError = 0;
494 #ifdef PIXEL_ADDRESS
495 PIXEL_TYPE *pRow = NULL;
496 GLint dPRowOuter = 0, dPRowInner; /* offset in bytes */
497 #endif
498 #ifdef INTERP_Z
499 # ifdef DEPTH_TYPE
500 struct gl_renderbuffer *zrb
501 = ctx->DrawBuffer->Attachment[BUFFER_DEPTH].Renderbuffer;
502 DEPTH_TYPE *zRow = NULL;
503 GLint dZRowOuter = 0, dZRowInner; /* offset in bytes */
504 # endif
505 GLuint zLeft = 0;
506 GLfixed fdzOuter = 0, fdzInner;
507 #endif
508 #ifdef INTERP_RGB
509 GLint rLeft = 0, fdrOuter = 0, fdrInner;
510 GLint gLeft = 0, fdgOuter = 0, fdgInner;
511 GLint bLeft = 0, fdbOuter = 0, fdbInner;
512 #endif
513 #ifdef INTERP_ALPHA
514 GLint aLeft = 0, fdaOuter = 0, fdaInner;
515 #endif
516 #ifdef INTERP_INT_TEX
517 GLfixed sLeft=0, dsOuter=0, dsInner;
518 GLfixed tLeft=0, dtOuter=0, dtInner;
519 #endif
520 #ifdef INTERP_ATTRIBS
521 GLfloat wLeft = 0, dwOuter = 0, dwInner;
522 GLfloat attrLeft[VARYING_SLOT_MAX][4];
523 GLfloat daOuter[VARYING_SLOT_MAX][4], daInner[VARYING_SLOT_MAX][4];
524 #endif
525
526 for (subTriangle=0; subTriangle<=1; subTriangle++) {
527 EdgeT *eLeft, *eRight;
528 int setupLeft, setupRight;
529 int lines;
530
531 if (subTriangle==0) {
532 /* bottom half */
533 if (scan_from_left_to_right) {
534 eLeft = &eMaj;
535 eRight = &eBot;
536 lines = eRight->lines;
537 setupLeft = 1;
538 setupRight = 1;
539 }
540 else {
541 eLeft = &eBot;
542 eRight = &eMaj;
543 lines = eLeft->lines;
544 setupLeft = 1;
545 setupRight = 1;
546 }
547 }
548 else {
549 /* top half */
550 if (scan_from_left_to_right) {
551 eLeft = &eMaj;
552 eRight = &eTop;
553 lines = eRight->lines;
554 setupLeft = 0;
555 setupRight = 1;
556 }
557 else {
558 eLeft = &eTop;
559 eRight = &eMaj;
560 lines = eLeft->lines;
561 setupLeft = 1;
562 setupRight = 0;
563 }
564 if (lines == 0)
565 return;
566 }
567
568 if (setupLeft && eLeft->lines > 0) {
569 const SWvertex *vLower = eLeft->v0;
570 const GLfixed fsy = eLeft->fsy;
571 const GLfixed fsx = eLeft->fsx; /* no fractional part */
572 const GLfixed fx = FixedCeil(fsx); /* no fractional part */
573 const GLfixed adjx = (GLfixed) (fx - eLeft->fx0); /* SCALED! */
574 const GLfixed adjy = (GLfixed) eLeft->adjy; /* SCALED! */
575 GLint idxOuter;
576 GLfloat dxOuter;
577 GLfixed fdxOuter;
578
579 fError = fx - fsx - FIXED_ONE;
580 fxLeftEdge = fsx - FIXED_EPSILON;
581 fdxLeftEdge = eLeft->fdxdy;
582 fdxOuter = FixedFloor(fdxLeftEdge - FIXED_EPSILON);
583 fdError = fdxOuter - fdxLeftEdge + FIXED_ONE;
584 idxOuter = FixedToInt(fdxOuter);
585 dxOuter = (GLfloat) idxOuter;
586 span.y = FixedToInt(fsy);
587
588 /* silence warnings on some compilers */
589 (void) dxOuter;
590 (void) adjx;
591 (void) adjy;
592 (void) vLower;
593
594 #ifdef PIXEL_ADDRESS
595 {
596 pRow = (PIXEL_TYPE *) PIXEL_ADDRESS(FixedToInt(fxLeftEdge), span.y);
597 dPRowOuter = -((int)BYTES_PER_ROW) + idxOuter * sizeof(PIXEL_TYPE);
598 /* negative because Y=0 at bottom and increases upward */
599 }
600 #endif
601 /*
602 * Now we need the set of parameter (z, color, etc.) values at
603 * the point (fx, fsy). This gives us properly-sampled parameter
604 * values that we can step from pixel to pixel. Furthermore,
605 * although we might have intermediate results that overflow
606 * the normal parameter range when we step temporarily outside
607 * the triangle, we shouldn't overflow or underflow for any
608 * pixel that's actually inside the triangle.
609 */
610
611 #ifdef INTERP_Z
612 {
613 GLfloat z0 = vLower->attrib[VARYING_SLOT_POS][2];
614 if (depthBits <= 16) {
615 /* interpolate fixed-pt values */
616 GLfloat tmp = (z0 * FIXED_SCALE
617 + span.attrStepX[VARYING_SLOT_POS][2] * adjx
618 + span.attrStepY[VARYING_SLOT_POS][2] * adjy) + FIXED_HALF;
619 if (tmp < MAX_GLUINT / 2)
620 zLeft = (GLfixed) tmp;
621 else
622 zLeft = MAX_GLUINT / 2;
623 fdzOuter = SignedFloatToFixed(span.attrStepY[VARYING_SLOT_POS][2] +
624 dxOuter * span.attrStepX[VARYING_SLOT_POS][2]);
625 }
626 else {
627 /* interpolate depth values w/out scaling */
628 zLeft = (GLuint) (z0 + span.attrStepX[VARYING_SLOT_POS][2] * FixedToFloat(adjx)
629 + span.attrStepY[VARYING_SLOT_POS][2] * FixedToFloat(adjy));
630 fdzOuter = (GLint) (span.attrStepY[VARYING_SLOT_POS][2] +
631 dxOuter * span.attrStepX[VARYING_SLOT_POS][2]);
632 }
633 # ifdef DEPTH_TYPE
634 zRow = (DEPTH_TYPE *)
635 _swrast_pixel_address(zrb, FixedToInt(fxLeftEdge), span.y);
636 dZRowOuter = (ctx->DrawBuffer->Width + idxOuter) * sizeof(DEPTH_TYPE);
637 # endif
638 }
639 #endif
640 #ifdef INTERP_RGB
641 if (ctx->Light.ShadeModel == GL_SMOOTH) {
642 rLeft = (GLint)(ChanToFixed(vLower->color[RCOMP])
643 + span.attrStepX[VARYING_SLOT_COL0][0] * adjx
644 + span.attrStepY[VARYING_SLOT_COL0][0] * adjy) + FIXED_HALF;
645 gLeft = (GLint)(ChanToFixed(vLower->color[GCOMP])
646 + span.attrStepX[VARYING_SLOT_COL0][1] * adjx
647 + span.attrStepY[VARYING_SLOT_COL0][1] * adjy) + FIXED_HALF;
648 bLeft = (GLint)(ChanToFixed(vLower->color[BCOMP])
649 + span.attrStepX[VARYING_SLOT_COL0][2] * adjx
650 + span.attrStepY[VARYING_SLOT_COL0][2] * adjy) + FIXED_HALF;
651 fdrOuter = SignedFloatToFixed(span.attrStepY[VARYING_SLOT_COL0][0]
652 + dxOuter * span.attrStepX[VARYING_SLOT_COL0][0]);
653 fdgOuter = SignedFloatToFixed(span.attrStepY[VARYING_SLOT_COL0][1]
654 + dxOuter * span.attrStepX[VARYING_SLOT_COL0][1]);
655 fdbOuter = SignedFloatToFixed(span.attrStepY[VARYING_SLOT_COL0][2]
656 + dxOuter * span.attrStepX[VARYING_SLOT_COL0][2]);
657 # ifdef INTERP_ALPHA
658 aLeft = (GLint)(ChanToFixed(vLower->color[ACOMP])
659 + span.attrStepX[VARYING_SLOT_COL0][3] * adjx
660 + span.attrStepY[VARYING_SLOT_COL0][3] * adjy) + FIXED_HALF;
661 fdaOuter = SignedFloatToFixed(span.attrStepY[VARYING_SLOT_COL0][3]
662 + dxOuter * span.attrStepX[VARYING_SLOT_COL0][3]);
663 # endif
664 }
665 else {
666 ASSERT(ctx->Light.ShadeModel == GL_FLAT);
667 rLeft = ChanToFixed(v2->color[RCOMP]);
668 gLeft = ChanToFixed(v2->color[GCOMP]);
669 bLeft = ChanToFixed(v2->color[BCOMP]);
670 fdrOuter = fdgOuter = fdbOuter = 0;
671 # ifdef INTERP_ALPHA
672 aLeft = ChanToFixed(v2->color[ACOMP]);
673 fdaOuter = 0;
674 # endif
675 }
676 #endif /* INTERP_RGB */
677
678
679 #ifdef INTERP_INT_TEX
680 {
681 GLfloat s0, t0;
682 s0 = vLower->attrib[VARYING_SLOT_TEX0][0] * S_SCALE;
683 sLeft = (GLfixed)(s0 * FIXED_SCALE + span.attrStepX[VARYING_SLOT_TEX0][0] * adjx
684 + span.attrStepY[VARYING_SLOT_TEX0][0] * adjy) + FIXED_HALF;
685 dsOuter = SignedFloatToFixed(span.attrStepY[VARYING_SLOT_TEX0][0]
686 + dxOuter * span.attrStepX[VARYING_SLOT_TEX0][0]);
687
688 t0 = vLower->attrib[VARYING_SLOT_TEX0][1] * T_SCALE;
689 tLeft = (GLfixed)(t0 * FIXED_SCALE + span.attrStepX[VARYING_SLOT_TEX0][1] * adjx
690 + span.attrStepY[VARYING_SLOT_TEX0][1] * adjy) + FIXED_HALF;
691 dtOuter = SignedFloatToFixed(span.attrStepY[VARYING_SLOT_TEX0][1]
692 + dxOuter * span.attrStepX[VARYING_SLOT_TEX0][1]);
693 }
694 #endif
695 #ifdef INTERP_ATTRIBS
696 {
697 const GLuint attr = VARYING_SLOT_POS;
698 wLeft = vLower->attrib[VARYING_SLOT_POS][3]
699 + (span.attrStepX[attr][3] * adjx
700 + span.attrStepY[attr][3] * adjy) * (1.0F/FIXED_SCALE);
701 dwOuter = span.attrStepY[attr][3] + dxOuter * span.attrStepX[attr][3];
702 }
703 ATTRIB_LOOP_BEGIN
704 const GLfloat invW = vLower->attrib[VARYING_SLOT_POS][3];
705 if (swrast->_InterpMode[attr] == GL_FLAT) {
706 GLuint c;
707 for (c = 0; c < 4; c++) {
708 attrLeft[attr][c] = v2->attrib[attr][c] * invW;
709 daOuter[attr][c] = 0.0;
710 }
711 }
712 else {
713 GLuint c;
714 for (c = 0; c < 4; c++) {
715 const GLfloat a = vLower->attrib[attr][c] * invW;
716 attrLeft[attr][c] = a + ( span.attrStepX[attr][c] * adjx
717 + span.attrStepY[attr][c] * adjy) * (1.0F/FIXED_SCALE);
718 daOuter[attr][c] = span.attrStepY[attr][c] + dxOuter * span.attrStepX[attr][c];
719 }
720 }
721 ATTRIB_LOOP_END
722 #endif
723 } /*if setupLeft*/
724
725
726 if (setupRight && eRight->lines>0) {
727 fxRightEdge = eRight->fsx - FIXED_EPSILON;
728 fdxRightEdge = eRight->fdxdy;
729 }
730
731 if (lines==0) {
732 continue;
733 }
734
735
736 /* Rasterize setup */
737 #ifdef PIXEL_ADDRESS
738 dPRowInner = dPRowOuter + sizeof(PIXEL_TYPE);
739 #endif
740 #ifdef INTERP_Z
741 # ifdef DEPTH_TYPE
742 dZRowInner = dZRowOuter + sizeof(DEPTH_TYPE);
743 # endif
744 fdzInner = fdzOuter + span.zStep;
745 #endif
746 #ifdef INTERP_RGB
747 fdrInner = fdrOuter + span.redStep;
748 fdgInner = fdgOuter + span.greenStep;
749 fdbInner = fdbOuter + span.blueStep;
750 #endif
751 #ifdef INTERP_ALPHA
752 fdaInner = fdaOuter + span.alphaStep;
753 #endif
754 #ifdef INTERP_INT_TEX
755 dsInner = dsOuter + span.intTexStep[0];
756 dtInner = dtOuter + span.intTexStep[1];
757 #endif
758 #ifdef INTERP_ATTRIBS
759 dwInner = dwOuter + span.attrStepX[VARYING_SLOT_POS][3];
760 ATTRIB_LOOP_BEGIN
761 GLuint c;
762 for (c = 0; c < 4; c++) {
763 daInner[attr][c] = daOuter[attr][c] + span.attrStepX[attr][c];
764 }
765 ATTRIB_LOOP_END
766 #endif
767
768 while (lines > 0) {
769 /* initialize the span interpolants to the leftmost value */
770 /* ff = fixed-pt fragment */
771 const GLint right = FixedToInt(fxRightEdge);
772 span.x = FixedToInt(fxLeftEdge);
773 if (right <= span.x)
774 span.end = 0;
775 else
776 span.end = right - span.x;
777
778 #ifdef INTERP_Z
779 span.z = zLeft;
780 #endif
781 #ifdef INTERP_RGB
782 span.red = rLeft;
783 span.green = gLeft;
784 span.blue = bLeft;
785 #endif
786 #ifdef INTERP_ALPHA
787 span.alpha = aLeft;
788 #endif
789 #ifdef INTERP_INT_TEX
790 span.intTex[0] = sLeft;
791 span.intTex[1] = tLeft;
792 #endif
793
794 #ifdef INTERP_ATTRIBS
795 span.attrStart[VARYING_SLOT_POS][3] = wLeft;
796 ATTRIB_LOOP_BEGIN
797 GLuint c;
798 for (c = 0; c < 4; c++) {
799 span.attrStart[attr][c] = attrLeft[attr][c];
800 }
801 ATTRIB_LOOP_END
802 #endif
803
804 /* This is where we actually generate fragments */
805 /* XXX the test for span.y > 0 _shouldn't_ be needed but
806 * it fixes a problem on 64-bit Opterons (bug 4842).
807 */
808 if (span.end > 0 && span.y >= 0) {
809 const GLint len = span.end - 1;
810 (void) len;
811 #ifdef INTERP_RGB
812 CLAMP_INTERPOLANT(red, redStep, len);
813 CLAMP_INTERPOLANT(green, greenStep, len);
814 CLAMP_INTERPOLANT(blue, blueStep, len);
815 #endif
816 #ifdef INTERP_ALPHA
817 CLAMP_INTERPOLANT(alpha, alphaStep, len);
818 #endif
819 {
820 RENDER_SPAN( span );
821 }
822 }
823
824 /*
825 * Advance to the next scan line. Compute the
826 * new edge coordinates, and adjust the
827 * pixel-center x coordinate so that it stays
828 * on or inside the major edge.
829 */
830 span.y++;
831 lines--;
832
833 fxLeftEdge += fdxLeftEdge;
834 fxRightEdge += fdxRightEdge;
835
836 fError += fdError;
837 if (fError >= 0) {
838 fError -= FIXED_ONE;
839
840 #ifdef PIXEL_ADDRESS
841 pRow = (PIXEL_TYPE *) ((GLubyte *) pRow + dPRowOuter);
842 #endif
843 #ifdef INTERP_Z
844 # ifdef DEPTH_TYPE
845 zRow = (DEPTH_TYPE *) ((GLubyte *) zRow + dZRowOuter);
846 # endif
847 zLeft += fdzOuter;
848 #endif
849 #ifdef INTERP_RGB
850 rLeft += fdrOuter;
851 gLeft += fdgOuter;
852 bLeft += fdbOuter;
853 #endif
854 #ifdef INTERP_ALPHA
855 aLeft += fdaOuter;
856 #endif
857 #ifdef INTERP_INT_TEX
858 sLeft += dsOuter;
859 tLeft += dtOuter;
860 #endif
861 #ifdef INTERP_ATTRIBS
862 wLeft += dwOuter;
863 ATTRIB_LOOP_BEGIN
864 GLuint c;
865 for (c = 0; c < 4; c++) {
866 attrLeft[attr][c] += daOuter[attr][c];
867 }
868 ATTRIB_LOOP_END
869 #endif
870 }
871 else {
872 #ifdef PIXEL_ADDRESS
873 pRow = (PIXEL_TYPE *) ((GLubyte *) pRow + dPRowInner);
874 #endif
875 #ifdef INTERP_Z
876 # ifdef DEPTH_TYPE
877 zRow = (DEPTH_TYPE *) ((GLubyte *) zRow + dZRowInner);
878 # endif
879 zLeft += fdzInner;
880 #endif
881 #ifdef INTERP_RGB
882 rLeft += fdrInner;
883 gLeft += fdgInner;
884 bLeft += fdbInner;
885 #endif
886 #ifdef INTERP_ALPHA
887 aLeft += fdaInner;
888 #endif
889 #ifdef INTERP_INT_TEX
890 sLeft += dsInner;
891 tLeft += dtInner;
892 #endif
893 #ifdef INTERP_ATTRIBS
894 wLeft += dwInner;
895 ATTRIB_LOOP_BEGIN
896 GLuint c;
897 for (c = 0; c < 4; c++) {
898 attrLeft[attr][c] += daInner[attr][c];
899 }
900 ATTRIB_LOOP_END
901 #endif
902 }
903 } /*while lines>0*/
904
905 } /* for subTriangle */
906
907 }
908 }
909 }
910
911 #undef SETUP_CODE
912 #undef RENDER_SPAN
913
914 #undef PIXEL_TYPE
915 #undef BYTES_PER_ROW
916 #undef PIXEL_ADDRESS
917 #undef DEPTH_TYPE
918
919 #undef INTERP_Z
920 #undef INTERP_RGB
921 #undef INTERP_ALPHA
922 #undef INTERP_INT_TEX
923 #undef INTERP_ATTRIBS
924
925 #undef S_SCALE
926 #undef T_SCALE
927
928 #undef FixedToDepth
929
930 #undef NAME