1 /* $Id: s_linetemp.h,v 1.8 2001/05/03 22:13:32 brianp Exp $ */
4 * Mesa 3-D graphics library
7 * Copyright (C) 1999-2001 Brian Paul All Rights Reserved.
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:
16 * The above copyright notice and this permission notice shall be included
17 * in all copies or substantial portions of the Software.
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.
29 * Line Rasterizer Template
31 * This file is #include'd to generate custom line rasterizers.
33 * The following macros may be defined to indicate what auxillary information
34 * must be interplated along the line:
35 * INTERP_Z - if defined, interpolate Z values
36 * INTERP_FOG - if defined, interpolate FOG values
37 * INTERP_RGB - if defined, interpolate RGB values
38 * INTERP_SPEC - if defined, interpolate specular RGB values
39 * INTERP_ALPHA - if defined, interpolate Alpha values
40 * INTERP_INDEX - if defined, interpolate color index values
41 * INTERP_TEX - if defined, interpolate unit 0 texcoords
42 * INTERP_MULTITEX - if defined, interpolate multi-texcoords
44 * When one can directly address pixels in the color buffer the following
45 * macros can be defined and used to directly compute pixel addresses during
46 * rasterization (see pixelPtr):
47 * PIXEL_TYPE - the datatype of a pixel (GLubyte, GLushort, GLuint)
48 * BYTES_PER_ROW - number of bytes per row in the color buffer
49 * PIXEL_ADDRESS(X,Y) - returns the address of pixel at (X,Y) where
50 * Y==0 at bottom of screen and increases upward.
52 * Similarly, for direct depth buffer access, this type is used for depth
54 * DEPTH_TYPE - either GLushort or GLuint
56 * Optionally, one may provide one-time setup code
57 * SETUP_CODE - code which is to be executed once per line
59 * To enable line stippling define STIPPLE = 1
60 * To enable wide lines define WIDE = 1
62 * To actually "plot" each pixel either the PLOT macro or
63 * (XMAJOR_PLOT and YMAJOR_PLOT macros) must be defined...
64 * PLOT(X,Y) - code to plot a pixel. Example:
67 * color = pack_rgb( FixedToInt(r0), FixedToInt(g0),
69 * put_pixel( X, Y, color );
72 * This code was designed for the origin to be in the lower-left corner.
77 /*void line( GLcontext *ctx, const SWvertex *vert0, const SWvertex *vert1 )*/
79 GLint x0
= (GLint
) vert0
->win
[0];
80 GLint x1
= (GLint
) vert1
->win
[0];
81 GLint y0
= (GLint
) vert0
->win
[1];
82 GLint y1
= (GLint
) vert1
->win
[1];
89 const GLint depthBits
= ctx
->Visual
.depthBits
;
90 const GLint fixedToDepthShift
= depthBits
<= 16 ? FIXED_SHIFT
: 0;
91 # define FixedToDepth(F) ((F) >> fixedToDepthShift)
93 GLint zPtrXstep
, zPtrYstep
;
98 GLfloat fog0
= vert0
->fog
;
99 GLfloat dfog
= vert1
->fog
- fog0
;
102 GLfixed r0
= IntToFixed(vert0
->color
[0]);
103 GLfixed dr
= IntToFixed(vert1
->color
[0]) - r0
;
104 GLfixed g0
= IntToFixed(vert0
->color
[1]);
105 GLfixed dg
= IntToFixed(vert1
->color
[1]) - g0
;
106 GLfixed b0
= IntToFixed(vert0
->color
[2]);
107 GLfixed db
= IntToFixed(vert1
->color
[2]) - b0
;
110 GLfixed sr0
= IntToFixed(vert0
->specular
[0]);
111 GLfixed dsr
= IntToFixed(vert1
->specular
[0]) - sr0
;
112 GLfixed sg0
= IntToFixed(vert0
->specular
[1]);
113 GLfixed dsg
= IntToFixed(vert1
->specular
[1]) - sg0
;
114 GLfixed sb0
= IntToFixed(vert0
->specular
[2]);
115 GLfixed dsb
= IntToFixed(vert1
->specular
[2]) - sb0
;
118 GLfixed a0
= IntToFixed(vert0
->color
[3]);
119 GLfixed da
= IntToFixed(vert1
->color
[3]) - a0
;
122 GLint i0
= vert0
->index
<< 8;
123 GLint di
= (GLint
) (vert1
->index
<< 8) - i0
;
126 const GLfloat invw0
= vert0
->win
[3];
127 const GLfloat invw1
= vert1
->win
[3];
130 GLfloat fragTexcoord
[4];
132 #ifdef INTERP_MULTITEX
133 const GLfloat invw0
= vert0
->win
[3];
134 const GLfloat invw1
= vert1
->win
[3];
135 GLfloat tex
[MAX_TEXTURE_UNITS
][4];
136 GLfloat dtex
[MAX_TEXTURE_UNITS
][4];
137 GLfloat fragTexcoord
[MAX_TEXTURE_UNITS
][4];
140 PIXEL_TYPE
*pixelPtr
;
141 GLint pixelXstep
, pixelYstep
;
144 SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
147 /* for wide lines, draw all X in [x+min, x+max] or Y in [y+min, y+max] */
148 GLint width
, min
, max
;
149 width
= (GLint
) CLAMP( ctx
->Line
.Width
, MIN_LINE_WIDTH
, MAX_LINE_WIDTH
);
150 min
= (width
-1) / -2;
151 max
= min
+ width
- 1;
155 tex
[0] = invw0
* vert0
->texcoord
[0][0];
156 dtex
[0] = invw1
* vert1
->texcoord
[0][0] - tex
[0];
157 tex
[1] = invw0
* vert0
->texcoord
[0][1];
158 dtex
[1] = invw1
* vert1
->texcoord
[0][1] - tex
[1];
159 tex
[2] = invw0
* vert0
->texcoord
[0][2];
160 dtex
[2] = invw1
* vert1
->texcoord
[0][2] - tex
[2];
161 tex
[3] = invw0
* vert0
->texcoord
[0][3];
162 dtex
[3] = invw1
* vert1
->texcoord
[0][3] - tex
[3];
165 #ifdef INTERP_MULTITEX
168 for (u
= 0; u
< ctx
->Const
.MaxTextureUnits
; u
++) {
169 if (ctx
->Texture
.Unit
[u
]._ReallyEnabled
) {
170 tex
[u
][0] = invw0
* vert0
->texcoord
[u
][0];
171 dtex
[u
][0] = invw1
* vert1
->texcoord
[u
][0] - tex
[u
][0];
172 tex
[u
][1] = invw0
* vert0
->texcoord
[u
][1];
173 dtex
[u
][1] = invw1
* vert1
->texcoord
[u
][1] - tex
[u
][1];
174 tex
[u
][2] = invw0
* vert0
->texcoord
[u
][2];
175 dtex
[u
][2] = invw1
* vert1
->texcoord
[u
][2] - tex
[u
][2];
176 tex
[u
][3] = invw0
* vert0
->texcoord
[u
][3];
177 dtex
[u
][3] = invw1
* vert1
->texcoord
[u
][3] - tex
[u
][3];
185 * Despite being clipped to the view volume, the line's window coordinates
186 * may just lie outside the window bounds. That is, if the legal window
187 * coordinates are [0,W-1][0,H-1], it's possible for x==W and/or y==H.
188 * This quick and dirty code nudges the endpoints inside the window if
193 GLint w
= ctx
->DrawBuffer
->Width
;
194 GLint h
= ctx
->DrawBuffer
->Height
;
195 if ((x0
==w
) | (x1
==w
)) {
196 if ((x0
==w
) & (x1
==w
))
201 if ((y0
==h
) | (y1
==h
)) {
202 if ((y0
==h
) & (y1
==h
))
211 if (dx
==0 && dy
==0) {
224 zPtr
= (DEPTH_TYPE
*) _mesa_zbuffer_address(ctx
, x0
, y0
);
226 if (depthBits
<= 16) {
227 z0
= FloatToFixed(vert0
->win
[2]);
228 z1
= FloatToFixed(vert1
->win
[2]);
231 z0
= (int) vert0
->win
[2];
232 z1
= (int) vert1
->win
[2];
236 pixelPtr
= (PIXEL_TYPE
*) PIXEL_ADDRESS(x0
,y0
);
240 dx
= -dx
; /* make positive */
244 #if defined(INTERP_Z) && defined(DEPTH_TYPE)
245 zPtrXstep
= -((GLint
)sizeof(DEPTH_TYPE
));
248 pixelXstep
= -((GLint
)sizeof(PIXEL_TYPE
));
255 #if defined(INTERP_Z) && defined(DEPTH_TYPE)
256 zPtrXstep
= ((GLint
)sizeof(DEPTH_TYPE
));
259 pixelXstep
= ((GLint
)sizeof(PIXEL_TYPE
));
264 dy
= -dy
; /* make positive */
268 #if defined(INTERP_Z) && defined(DEPTH_TYPE)
269 zPtrYstep
= -ctx
->DrawBuffer
->Width
* ((GLint
)sizeof(DEPTH_TYPE
));
272 pixelYstep
= BYTES_PER_ROW
;
279 #if defined(INTERP_Z) && defined(DEPTH_TYPE)
280 zPtrYstep
= ctx
->DrawBuffer
->Width
* ((GLint
)sizeof(DEPTH_TYPE
));
283 pixelYstep
= -(BYTES_PER_ROW
);
292 /*** X-major line ***/
294 GLint errorInc
= dy
+dy
;
295 GLint error
= errorInc
-dx
;
296 GLint errorDec
= error
-dx
;
304 dr
/= dx
; /* convert from whole line delta to per-pixel delta */
309 dsr
/= dx
; /* convert from whole line delta to per-pixel delta */
321 const GLfloat invDx
= 1.0F
/ (GLfloat
) dx
;
328 #ifdef INTERP_MULTITEX
330 const GLfloat invDx
= 1.0F
/ (GLfloat
) dx
;
332 for (u
= 0; u
< ctx
->Const
.MaxTextureUnits
; u
++) {
333 if (ctx
->Texture
.Unit
[u
]._ReallyEnabled
) {
346 m
= 1 << ((swrast
->StippleCounter
/ctx
->Line
.StippleFactor
) & 0xf);
347 if (ctx
->Line
.StipplePattern
& m
) {
350 GLdepth Z
= FixedToDepth(z0
);
357 const GLfloat invQ
= tex
[3] ? (1.0F
/ tex
[3]) : 1.0F
;
358 fragTexcoord
[0] = tex
[0] * invQ
;
359 fragTexcoord
[1] = tex
[1] * invQ
;
360 fragTexcoord
[2] = tex
[2] * invQ
;
361 fragTexcoord
[3] = tex
[3];
364 #ifdef INTERP_MULTITEX
367 for (u
= 0; u
< ctx
->Const
.MaxTextureUnits
; u
++) {
368 if (ctx
->Texture
.Unit
[u
]._ReallyEnabled
) {
369 const GLfloat invQ
= 1.0F
/ tex
[u
][3];
370 fragTexcoord
[u
][0] = tex
[u
][0] * invQ
;
371 fragTexcoord
[u
][1] = tex
[u
][1] * invQ
;
372 fragTexcoord
[u
][2] = tex
[u
][2] * invQ
;
373 fragTexcoord
[u
][3] = tex
[u
][3];
381 GLint ymin
= y0
+ min
;
382 GLint ymax
= y0
+ max
;
383 for (yy
=ymin
;yy
<=ymax
;yy
++) {
389 XMAJOR_PLOT( x0
, y0
);
396 swrast
->StippleCounter
++;
403 zPtr
= (DEPTH_TYPE
*) ((GLubyte
*) zPtr
+ zPtrXstep
);
432 #ifdef INTERP_MULTITEX
435 for (u
= 0; u
< ctx
->Const
.MaxTextureUnits
; u
++) {
436 if (ctx
->Texture
.Unit
[u
]._ReallyEnabled
) {
437 tex
[u
][0] += dtex
[u
][0];
438 tex
[u
][1] += dtex
[u
][1];
439 tex
[u
][2] += dtex
[u
][2];
440 tex
[u
][3] += dtex
[u
][3];
447 pixelPtr
= (PIXEL_TYPE
*) ((GLubyte
*) pixelPtr
+ pixelXstep
);
457 #if defined(INTERP_Z) && defined(DEPTH_TYPE)
458 zPtr
= (DEPTH_TYPE
*) ((GLubyte
*) zPtr
+ zPtrYstep
);
461 pixelPtr
= (PIXEL_TYPE
*) ((GLubyte
*) pixelPtr
+ pixelYstep
);
467 /*** Y-major line ***/
469 GLint errorInc
= dx
+dx
;
470 GLint error
= errorInc
-dy
;
471 GLint errorDec
= error
-dy
;
479 dr
/= dy
; /* convert from whole line delta to per-pixel delta */
484 dsr
/= dy
; /* convert from whole line delta to per-pixel delta */
496 const GLfloat invDy
= 1.0F
/ (GLfloat
) dy
;
503 #ifdef INTERP_MULTITEX
505 const GLfloat invDy
= 1.0F
/ (GLfloat
) dy
;
507 for (u
= 0; u
< ctx
->Const
.MaxTextureUnits
; u
++) {
508 if (ctx
->Texture
.Unit
[u
]._ReallyEnabled
) {
521 m
= 1 << ((swrast
->StippleCounter
/ctx
->Line
.StippleFactor
) & 0xf);
522 if (ctx
->Line
.StipplePattern
& m
) {
525 GLdepth Z
= FixedToDepth(z0
);
532 const GLfloat invQ
= tex
[3] ? (1.0F
/ tex
[3]) : 1.0F
;
533 fragTexcoord
[0] = tex
[0] * invQ
;
534 fragTexcoord
[1] = tex
[1] * invQ
;
535 fragTexcoord
[2] = tex
[2] * invQ
;
536 fragTexcoord
[3] = tex
[3];
539 #ifdef INTERP_MULTITEX
542 for (u
= 0; u
< ctx
->Const
.MaxTextureUnits
; u
++) {
543 if (ctx
->Texture
.Unit
[u
]._ReallyEnabled
) {
544 const GLfloat invQ
= 1.0F
/ tex
[u
][3];
545 fragTexcoord
[u
][0] = tex
[u
][0] * invQ
;
546 fragTexcoord
[u
][1] = tex
[u
][1] * invQ
;
547 fragTexcoord
[u
][2] = tex
[u
][2] * invQ
;
548 fragTexcoord
[u
][3] = tex
[u
][3];
556 GLint xmin
= x0
+ min
;
557 GLint xmax
= x0
+ max
;
558 for (xx
=xmin
;xx
<=xmax
;xx
++) {
564 YMAJOR_PLOT( x0
, y0
);
571 swrast
->StippleCounter
++;
578 zPtr
= (DEPTH_TYPE
*) ((GLubyte
*) zPtr
+ zPtrYstep
);
607 #ifdef INTERP_MULTITEX
610 for (u
= 0; u
< ctx
->Const
.MaxTextureUnits
; u
++) {
611 if (ctx
->Texture
.Unit
[u
]._ReallyEnabled
) {
612 tex
[u
][0] += dtex
[u
][0];
613 tex
[u
][1] += dtex
[u
][1];
614 tex
[u
][2] += dtex
[u
][2];
615 tex
[u
][3] += dtex
[u
][3];
621 pixelPtr
= (PIXEL_TYPE
*) ((GLubyte
*) pixelPtr
+ pixelYstep
);
631 #if defined(INTERP_Z) && defined(DEPTH_TYPE)
632 zPtr
= (DEPTH_TYPE
*) ((GLubyte
*) zPtr
+ zPtrXstep
);
635 pixelPtr
= (PIXEL_TYPE
*) ((GLubyte
*) pixelPtr
+ pixelXstep
);
651 #undef INTERP_MULTITEX