Improve the code for interpolating fragment attributes a little. More to come...
[mesa.git] / src / mesa / swrast / s_linetemp.h
1 /*
2 * Mesa 3-D graphics library
3 * Version: 6.5.3
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
25
26 /*
27 * Line Rasterizer Template
28 *
29 * This file is #include'd to generate custom line rasterizers.
30 *
31 * The following macros may be defined to indicate what auxillary information
32 * must be interplated along the line:
33 * INTERP_Z - if defined, interpolate Z values
34 * INTERP_FOG - if defined, interpolate FOG values
35 * INTERP_RGBA - if defined, interpolate RGBA values
36 * INTERP_SPEC - if defined, interpolate specular RGB values
37 * INTERP_INDEX - if defined, interpolate color index values
38 * INTERP_TEX - if defined, interpolate unit 0 texcoords
39 * INTERP_MULTITEX - if defined, interpolate multi-texcoords
40 * INTERP_VARYING - if defined, interpolate GLSL varyings
41 *
42 * When one can directly address pixels in the color buffer the following
43 * macros can be defined and used to directly compute pixel addresses during
44 * rasterization (see pixelPtr):
45 * PIXEL_TYPE - the datatype of a pixel (GLubyte, GLushort, GLuint)
46 * BYTES_PER_ROW - number of bytes per row in the color buffer
47 * PIXEL_ADDRESS(X,Y) - returns the address of pixel at (X,Y) where
48 * Y==0 at bottom of screen and increases upward.
49 *
50 * Similarly, for direct depth buffer access, this type is used for depth
51 * buffer addressing:
52 * DEPTH_TYPE - either GLushort or GLuint
53 *
54 * Optionally, one may provide one-time setup code
55 * SETUP_CODE - code which is to be executed once per line
56 *
57 * To actually "plot" each pixel the PLOT macro must be defined...
58 * PLOT(X,Y) - code to plot a pixel. Example:
59 * if (Z < *zPtr) {
60 * *zPtr = Z;
61 * color = pack_rgb( FixedToInt(r0), FixedToInt(g0),
62 * FixedToInt(b0) );
63 * put_pixel( X, Y, color );
64 * }
65 *
66 * This code was designed for the origin to be in the lower-left corner.
67 *
68 */
69
70
71 static void
72 NAME( GLcontext *ctx, const SWvertex *vert0, const SWvertex *vert1 )
73 {
74 const SWcontext *swrast = SWRAST_CONTEXT(ctx);
75 SWspan span;
76 GLuint interpFlags = 0;
77 GLint x0 = (GLint) vert0->win[0];
78 GLint x1 = (GLint) vert1->win[0];
79 GLint y0 = (GLint) vert0->win[1];
80 GLint y1 = (GLint) vert1->win[1];
81 GLint dx, dy;
82 GLint numPixels;
83 GLint xstep, ystep;
84 #if defined(DEPTH_TYPE)
85 const GLint depthBits = ctx->DrawBuffer->Visual.depthBits;
86 const GLint fixedToDepthShift = depthBits <= 16 ? FIXED_SHIFT : 0;
87 struct gl_renderbuffer *zrb = ctx->DrawBuffer->Attachment[BUFFER_DEPTH].Renderbuffer;
88 #define FixedToDepth(F) ((F) >> fixedToDepthShift)
89 GLint zPtrXstep, zPtrYstep;
90 DEPTH_TYPE *zPtr;
91 #elif defined(INTERP_Z)
92 const GLint depthBits = ctx->DrawBuffer->Visual.depthBits;
93 /*ctx->Visual.depthBits;*/
94 #endif
95 #ifdef PIXEL_ADDRESS
96 PIXEL_TYPE *pixelPtr;
97 GLint pixelXstep, pixelYstep;
98 #endif
99
100 #ifdef SETUP_CODE
101 SETUP_CODE
102 #endif
103
104 (void) swrast;
105
106 /* Cull primitives with malformed coordinates.
107 */
108 {
109 GLfloat tmp = vert0->win[0] + vert0->win[1]
110 + vert1->win[0] + vert1->win[1];
111 if (IS_INF_OR_NAN(tmp))
112 return;
113 }
114
115 /*
116 printf("%s():\n", __FUNCTION__);
117 printf(" (%f, %f, %f) -> (%f, %f, %f)\n",
118 vert0->win[0], vert0->win[1], vert0->win[2],
119 vert1->win[0], vert1->win[1], vert1->win[2]);
120 printf(" (%d, %d, %d) -> (%d, %d, %d)\n",
121 vert0->color[0], vert0->color[1], vert0->color[2],
122 vert1->color[0], vert1->color[1], vert1->color[2]);
123 printf(" (%d, %d, %d) -> (%d, %d, %d)\n",
124 vert0->specular[0], vert0->specular[1], vert0->specular[2],
125 vert1->specular[0], vert1->specular[1], vert1->specular[2]);
126 */
127
128 /*
129 * Despite being clipped to the view volume, the line's window coordinates
130 * may just lie outside the window bounds. That is, if the legal window
131 * coordinates are [0,W-1][0,H-1], it's possible for x==W and/or y==H.
132 * This quick and dirty code nudges the endpoints inside the window if
133 * necessary.
134 */
135 #ifdef CLIP_HACK
136 {
137 GLint w = ctx->DrawBuffer->Width;
138 GLint h = ctx->DrawBuffer->Height;
139 if ((x0==w) | (x1==w)) {
140 if ((x0==w) & (x1==w))
141 return;
142 x0 -= x0==w;
143 x1 -= x1==w;
144 }
145 if ((y0==h) | (y1==h)) {
146 if ((y0==h) & (y1==h))
147 return;
148 y0 -= y0==h;
149 y1 -= y1==h;
150 }
151 }
152 #endif
153
154 dx = x1 - x0;
155 dy = y1 - y0;
156 if (dx == 0 && dy == 0)
157 return;
158
159 #ifdef DEPTH_TYPE
160 zPtr = (DEPTH_TYPE *) zrb->GetPointer(ctx, zrb, x0, y0);
161 #endif
162 #ifdef PIXEL_ADDRESS
163 pixelPtr = (PIXEL_TYPE *) PIXEL_ADDRESS(x0,y0);
164 #endif
165
166 if (dx<0) {
167 dx = -dx; /* make positive */
168 xstep = -1;
169 #ifdef DEPTH_TYPE
170 zPtrXstep = -((GLint)sizeof(DEPTH_TYPE));
171 #endif
172 #ifdef PIXEL_ADDRESS
173 pixelXstep = -((GLint)sizeof(PIXEL_TYPE));
174 #endif
175 }
176 else {
177 xstep = 1;
178 #ifdef DEPTH_TYPE
179 zPtrXstep = ((GLint)sizeof(DEPTH_TYPE));
180 #endif
181 #ifdef PIXEL_ADDRESS
182 pixelXstep = ((GLint)sizeof(PIXEL_TYPE));
183 #endif
184 }
185
186 if (dy<0) {
187 dy = -dy; /* make positive */
188 ystep = -1;
189 #ifdef DEPTH_TYPE
190 zPtrYstep = -((GLint) (ctx->DrawBuffer->Width * sizeof(DEPTH_TYPE)));
191 #endif
192 #ifdef PIXEL_ADDRESS
193 pixelYstep = BYTES_PER_ROW;
194 #endif
195 }
196 else {
197 ystep = 1;
198 #ifdef DEPTH_TYPE
199 zPtrYstep = (GLint) (ctx->DrawBuffer->Width * sizeof(DEPTH_TYPE));
200 #endif
201 #ifdef PIXEL_ADDRESS
202 pixelYstep = -(BYTES_PER_ROW);
203 #endif
204 }
205
206 ASSERT(dx >= 0);
207 ASSERT(dy >= 0);
208
209 numPixels = MAX2(dx, dy);
210
211 /*
212 * Span setup: compute start and step values for all interpolated values.
213 */
214 #ifdef INTERP_RGBA
215 interpFlags |= SPAN_RGBA;
216 if (ctx->Light.ShadeModel == GL_SMOOTH) {
217 span.red = ChanToFixed(vert0->color[0]);
218 span.green = ChanToFixed(vert0->color[1]);
219 span.blue = ChanToFixed(vert0->color[2]);
220 span.alpha = ChanToFixed(vert0->color[3]);
221 span.redStep = (ChanToFixed(vert1->color[0]) - span.red ) / numPixels;
222 span.greenStep = (ChanToFixed(vert1->color[1]) - span.green) / numPixels;
223 span.blueStep = (ChanToFixed(vert1->color[2]) - span.blue ) / numPixels;
224 span.alphaStep = (ChanToFixed(vert1->color[3]) - span.alpha) / numPixels;
225 }
226 else {
227 span.red = ChanToFixed(vert1->color[0]);
228 span.green = ChanToFixed(vert1->color[1]);
229 span.blue = ChanToFixed(vert1->color[2]);
230 span.alpha = ChanToFixed(vert1->color[3]);
231 span.redStep = 0;
232 span.greenStep = 0;
233 span.blueStep = 0;
234 span.alphaStep = 0;
235 }
236 #endif
237 #ifdef INTERP_SPEC
238 interpFlags |= SPAN_SPEC;
239 if (ctx->Light.ShadeModel == GL_SMOOTH) {
240 span.specRed = ChanToFixed(vert0->specular[0]);
241 span.specGreen = ChanToFixed(vert0->specular[1]);
242 span.specBlue = ChanToFixed(vert0->specular[2]);
243 span.specRedStep = (ChanToFixed(vert1->specular[0]) - span.specRed) / numPixels;
244 span.specGreenStep = (ChanToFixed(vert1->specular[1]) - span.specBlue) / numPixels;
245 span.specBlueStep = (ChanToFixed(vert1->specular[2]) - span.specGreen) / numPixels;
246 }
247 else {
248 span.specRed = ChanToFixed(vert1->specular[0]);
249 span.specGreen = ChanToFixed(vert1->specular[1]);
250 span.specBlue = ChanToFixed(vert1->specular[2]);
251 span.specRedStep = 0;
252 span.specGreenStep = 0;
253 span.specBlueStep = 0;
254 }
255 #endif
256 #ifdef INTERP_INDEX
257 interpFlags |= SPAN_INDEX;
258 if (ctx->Light.ShadeModel == GL_SMOOTH) {
259 span.index = FloatToFixed(vert0->index);
260 span.indexStep = FloatToFixed(vert1->index - vert0->index) / numPixels;
261 }
262 else {
263 span.index = FloatToFixed(vert1->index);
264 span.indexStep = 0;
265 }
266 #endif
267 #if defined(INTERP_Z) || defined(DEPTH_TYPE)
268 interpFlags |= SPAN_Z;
269 {
270 if (depthBits <= 16) {
271 span.z = FloatToFixed(vert0->win[2]) + FIXED_HALF;
272 span.zStep = FloatToFixed(vert1->win[2] - vert0->win[2]) / numPixels;
273 }
274 else {
275 /* don't use fixed point */
276 span.z = (GLuint) vert0->win[2];
277 span.zStep = (GLint) ((vert1->win[2] - vert0->win[2]) / numPixels);
278 }
279 }
280 #endif
281 #ifdef INTERP_FOG
282 interpFlags |= SPAN_FOG;
283 span.attrStart[FRAG_ATTRIB_FOGC][0] = vert0->fog;
284 span.attrStepX[FRAG_ATTRIB_FOGC][0] = (vert1->fog - vert0->fog) / numPixels;
285 #endif
286 #ifdef INTERP_TEX
287 interpFlags |= SPAN_TEXTURE;
288 {
289 const GLfloat invw0 = vert0->win[3];
290 const GLfloat invw1 = vert1->win[3];
291 const GLfloat invLen = 1.0F / numPixels;
292 GLfloat ds, dt, dr, dq;
293 span.attrStart[FRAG_ATTRIB_TEX0][0] = invw0 * vert0->attrib[FRAG_ATTRIB_TEX0][0];
294 span.attrStart[FRAG_ATTRIB_TEX0][1] = invw0 * vert0->attrib[FRAG_ATTRIB_TEX0][1];
295 span.attrStart[FRAG_ATTRIB_TEX0][2] = invw0 * vert0->attrib[FRAG_ATTRIB_TEX0][2];
296 span.attrStart[FRAG_ATTRIB_TEX0][3] = invw0 * vert0->attrib[FRAG_ATTRIB_TEX0][3];
297 ds = (invw1 * vert1->attrib[FRAG_ATTRIB_TEX0][0]) - span.attrStart[FRAG_ATTRIB_TEX0][0];
298 dt = (invw1 * vert1->attrib[FRAG_ATTRIB_TEX0][1]) - span.attrStart[FRAG_ATTRIB_TEX0][1];
299 dr = (invw1 * vert1->attrib[FRAG_ATTRIB_TEX0][2]) - span.attrStart[FRAG_ATTRIB_TEX0][2];
300 dq = (invw1 * vert1->attrib[FRAG_ATTRIB_TEX0][3]) - span.attrStart[FRAG_ATTRIB_TEX0][3];
301 span.attrStepX[FRAG_ATTRIB_TEX0][0] = ds * invLen;
302 span.attrStepX[FRAG_ATTRIB_TEX0][1] = dt * invLen;
303 span.attrStepX[FRAG_ATTRIB_TEX0][2] = dr * invLen;
304 span.attrStepX[FRAG_ATTRIB_TEX0][3] = dq * invLen;
305 span.attrStepY[FRAG_ATTRIB_TEX0][0] = 0.0F;
306 span.attrStepY[FRAG_ATTRIB_TEX0][1] = 0.0F;
307 span.attrStepY[FRAG_ATTRIB_TEX0][2] = 0.0F;
308 span.attrStepY[FRAG_ATTRIB_TEX0][3] = 0.0F;
309 }
310 #endif
311 #if defined(INTERP_MULTITEX) || defined(INTERP_VARYING)
312 interpFlags |= (SPAN_TEXTURE | SPAN_VARYING);
313 {
314 const GLfloat invLen = 1.0F / numPixels;
315 const GLfloat invw0 = vert0->win[3];
316 const GLfloat invw1 = vert1->win[3];
317 ATTRIB_LOOP_BEGIN
318 GLfloat ds, dt, dr, dq;
319 span.attrStart[attr][0] = invw0 * vert0->attrib[attr][0];
320 span.attrStart[attr][1] = invw0 * vert0->attrib[attr][1];
321 span.attrStart[attr][2] = invw0 * vert0->attrib[attr][2];
322 span.attrStart[attr][3] = invw0 * vert0->attrib[attr][3];
323 ds = (invw1 * vert1->attrib[attr][0]) - span.attrStart[attr][0];
324 dt = (invw1 * vert1->attrib[attr][1]) - span.attrStart[attr][1];
325 dr = (invw1 * vert1->attrib[attr][2]) - span.attrStart[attr][2];
326 dq = (invw1 * vert1->attrib[attr][3]) - span.attrStart[attr][3];
327 span.attrStepX[attr][0] = ds * invLen;
328 span.attrStepX[attr][1] = dt * invLen;
329 span.attrStepX[attr][2] = dr * invLen;
330 span.attrStepX[attr][3] = dq * invLen;
331 span.attrStepY[attr][0] = 0.0F;
332 span.attrStepY[attr][1] = 0.0F;
333 span.attrStepY[attr][2] = 0.0F;
334 span.attrStepY[attr][3] = 0.0F;
335 ATTRIB_LOOP_END
336 }
337 #endif
338
339 INIT_SPAN(span, GL_LINE, numPixels, interpFlags, SPAN_XY);
340
341 /* Need these for fragment prog texcoord interpolation */
342 span.attrStart[FRAG_ATTRIB_WPOS][3] = 1.0F;
343 span.attrStepX[FRAG_ATTRIB_WPOS][3] = 0.0F;
344 span.attrStepY[FRAG_ATTRIB_WPOS][3] = 0.0F;
345
346 /*
347 * Draw
348 */
349
350 if (dx > dy) {
351 /*** X-major line ***/
352 GLint i;
353 GLint errorInc = dy+dy;
354 GLint error = errorInc-dx;
355 GLint errorDec = error-dx;
356
357 for (i = 0; i < dx; i++) {
358 #ifdef DEPTH_TYPE
359 GLuint Z = FixedToDepth(span.z);
360 #endif
361 #ifdef PLOT
362 PLOT( x0, y0 );
363 #else
364 span.array->x[i] = x0;
365 span.array->y[i] = y0;
366 #endif
367 x0 += xstep;
368 #ifdef DEPTH_TYPE
369 zPtr = (DEPTH_TYPE *) ((GLubyte*) zPtr + zPtrXstep);
370 span.z += span.zStep;
371 #endif
372 #ifdef PIXEL_ADDRESS
373 pixelPtr = (PIXEL_TYPE*) ((GLubyte*) pixelPtr + pixelXstep);
374 #endif
375 if (error<0) {
376 error += errorInc;
377 }
378 else {
379 error += errorDec;
380 y0 += ystep;
381 #ifdef DEPTH_TYPE
382 zPtr = (DEPTH_TYPE *) ((GLubyte*) zPtr + zPtrYstep);
383 #endif
384 #ifdef PIXEL_ADDRESS
385 pixelPtr = (PIXEL_TYPE*) ((GLubyte*) pixelPtr + pixelYstep);
386 #endif
387 }
388 }
389 }
390 else {
391 /*** Y-major line ***/
392 GLint i;
393 GLint errorInc = dx+dx;
394 GLint error = errorInc-dy;
395 GLint errorDec = error-dy;
396
397 for (i=0;i<dy;i++) {
398 #ifdef DEPTH_TYPE
399 GLuint Z = FixedToDepth(span.z);
400 #endif
401 #ifdef PLOT
402 PLOT( x0, y0 );
403 #else
404 span.array->x[i] = x0;
405 span.array->y[i] = y0;
406 #endif
407 y0 += ystep;
408 #ifdef DEPTH_TYPE
409 zPtr = (DEPTH_TYPE *) ((GLubyte*) zPtr + zPtrYstep);
410 span.z += span.zStep;
411 #endif
412 #ifdef PIXEL_ADDRESS
413 pixelPtr = (PIXEL_TYPE*) ((GLubyte*) pixelPtr + pixelYstep);
414 #endif
415 if (error<0) {
416 error += errorInc;
417 }
418 else {
419 error += errorDec;
420 x0 += xstep;
421 #ifdef DEPTH_TYPE
422 zPtr = (DEPTH_TYPE *) ((GLubyte*) zPtr + zPtrXstep);
423 #endif
424 #ifdef PIXEL_ADDRESS
425 pixelPtr = (PIXEL_TYPE*) ((GLubyte*) pixelPtr + pixelXstep);
426 #endif
427 }
428 }
429 }
430
431 #ifdef RENDER_SPAN
432 RENDER_SPAN( span );
433 #endif
434
435 (void)span;
436
437 }
438
439
440 #undef NAME
441 #undef INTERP_Z
442 #undef INTERP_FOG
443 #undef INTERP_RGBA
444 #undef INTERP_SPEC
445 #undef INTERP_TEX
446 #undef INTERP_MULTITEX
447 #undef INTERP_INDEX
448 #undef PIXEL_ADDRESS
449 #undef PIXEL_TYPE
450 #undef DEPTH_TYPE
451 #undef BYTES_PER_ROW
452 #undef SETUP_CODE
453 #undef PLOT
454 #undef CLIP_HACK
455 #undef FixedToDepth
456 #undef RENDER_SPAN