2 * Mesa 3-D graphics library
5 * Copyright (C) 1999-2005 Brian Paul All Rights Reserved.
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:
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
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.
26 * Regarding GL_NV_point_sprite:
28 * Portions of this software may use or implement intellectual
29 * property owned and licensed by NVIDIA Corporation. NVIDIA disclaims
30 * any and all warranties with respect to such intellectual property,
31 * including any use thereof or modifications thereto.
36 * Point rendering template code.
38 * Set FLAGS = bitwise-OR of the following tokens:
40 * RGBA = do rgba instead of color index
41 * SMOOTH = do antialiasing
42 * TEXTURE = do texture coords
43 * SPECULAR = do separate specular color
44 * LARGE = do points with diameter > 1 pixel
45 * ATTENUATE = compute point size attenuation
46 * SPRITE = GL_ARB_point_sprite / GL_NV_point_sprite
48 * Notes: LARGE and ATTENUATE are exclusive of each other.
49 * TEXTURE requires RGBA
54 * NOTES on antialiased point rasterization:
56 * Let d = distance of fragment center from vertex.
58 * fragment has 100% coverage
59 * else if d > rmax2 then
60 * fragment has 0% coverage
62 * fragment has % coverage = (d - rmin2) / (rmax2 - rmin2)
68 NAME ( GLcontext
*ctx
, const SWvertex
*vert
)
70 #if FLAGS & (ATTENUATE | LARGE | SMOOTH | SPRITE)
74 #if (FLAGS & ATTENUATE) && (FLAGS & SMOOTH)
77 const GLchan red
= vert
->color
[0];
78 const GLchan green
= vert
->color
[1];
79 const GLchan blue
= vert
->color
[2];
80 const GLchan alpha
= vert
->color
[3];
83 const GLchan specRed
= vert
->specular
[0];
84 const GLchan specGreen
= vert
->specular
[1];
85 const GLchan specBlue
= vert
->specular
[2];
88 const GLuint colorIndex
= (GLuint
) vert
->index
; /* XXX round? */
91 GLfloat texcoord
[MAX_TEXTURE_COORD_UNITS
][4];
94 SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
95 struct sw_span
*span
= &(swrast
->PointSpan
);
97 /* Cull primitives with malformed coordinates.
100 float tmp
= vert
->win
[0] + vert
->win
[1];
101 if (IS_INF_OR_NAN(tmp
))
108 span
->interpMask
= SPAN_FOG
;
109 span
->arrayMask
= SPAN_XY
| SPAN_Z
;
110 span
->fog
= vert
->fog
;
113 span
->arrayMask
|= SPAN_RGBA
;
116 span
->arrayMask
|= SPAN_SPEC
;
119 span
->arrayMask
|= SPAN_INDEX
;
122 span
->arrayMask
|= SPAN_TEXTURE
;
123 if (ctx
->FragmentProgram
._Active
) {
124 /* Don't divide texture s,t,r by q (use TXP to do that) */
125 for (u
= 0; u
< ctx
->Const
.MaxTextureUnits
; u
++) {
126 if (ctx
->Texture
._EnabledCoordUnits
& (1 << u
)) {
127 COPY_4V(texcoord
[u
], vert
->texcoord
[u
]);
132 /* Divide texture s,t,r by q here */
133 for (u
= 0; u
< ctx
->Const
.MaxTextureUnits
; u
++) {
134 if (ctx
->Texture
._EnabledCoordUnits
& (1 << u
)) {
135 const GLfloat q
= vert
->texcoord
[u
][3];
136 const GLfloat invQ
= (q
== 0.0F
|| q
== 1.0F
) ? 1.0F
: (1.0F
/ q
);
137 texcoord
[u
][0] = vert
->texcoord
[u
][0] * invQ
;
138 texcoord
[u
][1] = vert
->texcoord
[u
][1] * invQ
;
139 texcoord
[u
][2] = vert
->texcoord
[u
][2] * invQ
;
144 /* need these for fragment programs */
150 span
->arrayMask
|= SPAN_COVERAGE
;
153 span
->arrayMask
|= SPAN_TEXTURE
;
156 /* Compute point size if not known to be one */
157 #if FLAGS & ATTENUATE
158 /* first, clamp attenuated size to the user-specifed range */
159 size
= CLAMP(vert
->pointSize
, ctx
->Point
.MinSize
, ctx
->Point
.MaxSize
);
160 #if (FLAGS & RGBA) && (FLAGS & SMOOTH)
161 /* only if multisampling, compute the fade factor */
162 if (ctx
->Multisample
.Enabled
) {
163 if (vert
->pointSize
>= ctx
->Point
.Threshold
) {
167 GLfloat dsize
= vert
->pointSize
/ ctx
->Point
.Threshold
;
168 alphaAtten
= dsize
* dsize
;
175 #elif FLAGS & (LARGE | SMOOTH | SPRITE)
176 /* constant, non-attenuated size */
177 size
= ctx
->Point
._Size
; /* this is already clamped */
181 #if FLAGS & (ATTENUATE | LARGE | SMOOTH | SPRITE)
183 *** Multi-pixel points
186 /* do final clamping now */
187 if (ctx
->Point
.SmoothFlag
) {
188 size
= CLAMP(size
, ctx
->Const
.MinPointSizeAA
, ctx
->Const
.MaxPointSizeAA
);
191 size
= CLAMP(size
, ctx
->Const
.MinPointSize
, ctx
->Const
.MaxPointSize
);
196 const GLfloat radius
= 0.5F
* size
;
197 const GLint z
= (GLint
) (vert
->win
[2] + 0.5F
);
200 const GLfloat rmin
= radius
- 0.7071F
; /* 0.7071 = sqrt(2)/2 */
201 const GLfloat rmax
= radius
+ 0.7071F
;
202 const GLfloat rmin2
= MAX2(0.0F
, rmin
* rmin
);
203 const GLfloat rmax2
= rmax
* rmax
;
204 const GLfloat cscale
= 1.0F
/ (rmax2
- rmin2
);
205 const GLint xmin
= (GLint
) (vert
->win
[0] - radius
);
206 const GLint xmax
= (GLint
) (vert
->win
[0] + radius
);
207 const GLint ymin
= (GLint
) (vert
->win
[1] - radius
);
208 const GLint ymax
= (GLint
) (vert
->win
[1] + radius
);
211 GLint xmin
, xmax
, ymin
, ymax
;
212 GLint iSize
= (GLint
) (size
+ 0.5F
);
214 iSize
= MAX2(1, iSize
);
218 xmin
= (GLint
) (vert
->win
[0] - iRadius
);
219 xmax
= (GLint
) (vert
->win
[0] + iRadius
);
220 ymin
= (GLint
) (vert
->win
[1] - iRadius
);
221 ymax
= (GLint
) (vert
->win
[1] + iRadius
);
225 xmin
= (GLint
) vert
->win
[0] - iRadius
+ 1;
226 xmax
= xmin
+ iSize
- 1;
227 ymin
= (GLint
) vert
->win
[1] - iRadius
+ 1;
228 ymax
= ymin
+ iSize
- 1;
232 /* check if we need to flush */
233 if (span
->end
+ (xmax
-xmin
+1) * (ymax
-ymin
+1) >= MAX_WIDTH
||
234 (swrast
->_RasterMask
& (BLEND_BIT
| LOGIC_OP_BIT
| MASKING_BIT
))) {
236 _swrast_write_rgba_span(ctx
, span
);
238 _swrast_write_index_span(ctx
, span
);
244 * OK, generate fragments
248 for (y
= ymin
; y
<= ymax
; y
++) {
249 /* check if we need to flush */
250 if (count
+ (xmax
-xmin
+1) >= MAX_WIDTH
) {
253 _swrast_write_rgba_span(ctx
, span
);
255 _swrast_write_index_span(ctx
, span
);
257 count
= span
->end
= 0;
259 for (x
= xmin
; x
<= xmax
; x
++) {
260 #if FLAGS & (SPRITE | TEXTURE)
265 span
->array
->rgba
[count
][RCOMP
] = red
;
266 span
->array
->rgba
[count
][GCOMP
] = green
;
267 span
->array
->rgba
[count
][BCOMP
] = blue
;
268 span
->array
->rgba
[count
][ACOMP
] = alpha
;
271 span
->array
->spec
[count
][RCOMP
] = specRed
;
272 span
->array
->spec
[count
][GCOMP
] = specGreen
;
273 span
->array
->spec
[count
][BCOMP
] = specBlue
;
276 span
->array
->index
[count
] = colorIndex
;
279 for (u
= 0; u
< ctx
->Const
.MaxTextureUnits
; u
++) {
280 if (ctx
->Texture
._EnabledCoordUnits
& (1 << u
)) {
281 COPY_4V(span
->array
->texcoords
[u
][count
], texcoord
[u
]);
287 /* compute coverage */
289 const GLfloat dx
= x
- vert
->win
[0] + 0.5F
;
290 const GLfloat dy
= y
- vert
->win
[1] + 0.5F
;
291 const GLfloat dist2
= dx
* dx
+ dy
* dy
;
293 if (dist2
>= rmin2
) {
294 /* compute partial coverage */
295 span
->array
->coverage
[count
] = 1.0F
- (dist2
- rmin2
) * cscale
;
297 /* coverage in [0,15] */
298 span
->array
->coverage
[count
] *= 15.0;
303 span
->array
->coverage
[count
] = 1.0F
;
306 span
->array
->x
[count
] = x
;
307 span
->array
->y
[count
] = y
;
308 span
->array
->z
[count
] = z
;
310 #if (FLAGS & ATTENUATE) && (FLAGS & RGBA)
311 span
->array
->rgba
[count
][ACOMP
] = (GLchan
) (alpha
* alphaAtten
);
313 span
->array
->rgba
[count
][ACOMP
] = alpha
;
321 /* not smooth (square points) */
322 span
->array
->x
[count
] = x
;
323 span
->array
->y
[count
] = y
;
324 span
->array
->z
[count
] = z
;
327 for (u
= 0; u
< ctx
->Const
.MaxTextureUnits
; u
++) {
328 if (ctx
->Texture
.Unit
[u
]._ReallyEnabled
) {
329 if (ctx
->Point
.CoordReplace
[u
]) {
330 GLfloat s
= 0.5F
+ (x
+ 0.5F
- vert
->win
[0]) / size
;
332 if (ctx
->Point
.SpriteOrigin
== GL_LOWER_LEFT
)
333 t
= 0.5F
+ (y
+ 0.5F
- vert
->win
[1]) / size
;
334 else /* GL_UPPER_LEFT */
335 t
= 0.5F
- (y
+ 0.5F
- vert
->win
[1]) / size
;
336 if (ctx
->Point
.SpriteRMode
== GL_ZERO
)
338 else if (ctx
->Point
.SpriteRMode
== GL_S
)
339 r
= vert
->texcoord
[u
][0];
341 r
= vert
->texcoord
[u
][2];
342 span
->array
->texcoords
[u
][count
][0] = s
;
343 span
->array
->texcoords
[u
][count
][1] = t
;
344 span
->array
->texcoords
[u
][count
][2] = r
;
345 span
->array
->texcoords
[u
][count
][3] = 1.0F
;
348 COPY_4V(span
->array
->texcoords
[u
][count
], vert
->texcoord
[u
]);
354 count
++; /* square point */
363 #else /* LARGE | ATTENUATE | SMOOTH | SPRITE */
366 *** Single-pixel points
371 /* check if we need to flush */
372 if (span
->end
>= MAX_WIDTH
||
373 (swrast
->_RasterMask
& (BLEND_BIT
| LOGIC_OP_BIT
| MASKING_BIT
))) {
375 _swrast_write_rgba_span(ctx
, span
);
377 _swrast_write_index_span(ctx
, span
);
385 span
->array
->rgba
[count
][RCOMP
] = red
;
386 span
->array
->rgba
[count
][GCOMP
] = green
;
387 span
->array
->rgba
[count
][BCOMP
] = blue
;
388 span
->array
->rgba
[count
][ACOMP
] = alpha
;
391 span
->array
->spec
[count
][RCOMP
] = specRed
;
392 span
->array
->spec
[count
][GCOMP
] = specGreen
;
393 span
->array
->spec
[count
][BCOMP
] = specBlue
;
396 span
->array
->index
[count
] = colorIndex
;
399 for (u
= 0; u
< ctx
->Const
.MaxTextureUnits
; u
++) {
400 if (ctx
->Texture
.Unit
[u
]._ReallyEnabled
) {
401 COPY_4V(span
->array
->texcoords
[u
][count
], texcoord
[u
]);
406 span
->array
->x
[count
] = (GLint
) vert
->win
[0];
407 span
->array
->y
[count
] = (GLint
) vert
->win
[1];
408 span
->array
->z
[count
] = (GLint
) (vert
->win
[2] + 0.5F
);
409 span
->end
= count
+ 1;
412 #endif /* LARGE || ATTENUATE || SMOOTH */
414 ASSERT(span
->end
<= MAX_WIDTH
);