2 * Mesa 3-D graphics library
5 * Copyright (C) 1999-2007 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 #include "main/glheader.h"
27 #include "main/colormac.h"
28 #include "main/context.h"
29 #include "main/macros.h"
30 #include "s_context.h"
31 #include "s_feedback.h"
37 * Used to cull points with invalid coords
39 #define CULL_INVALID(V) \
41 float tmp = (V)->attrib[FRAG_ATTRIB_WPOS][0] \
42 + (V)->attrib[FRAG_ATTRIB_WPOS][1]; \
43 if (IS_INF_OR_NAN(tmp)) \
50 * Get/compute the point size.
51 * The size may come from a vertex shader, or computed with attentuation
52 * or just the glPointSize value.
53 * Must also clamp to user-defined range and implmentation limits.
56 get_size(const GLcontext
*ctx
, const SWvertex
*vert
, GLboolean smoothed
)
60 if (ctx
->Point
._Attenuated
|| ctx
->VertexProgram
.PointSizeEnabled
) {
61 /* use vertex's point size */
62 size
= vert
->pointSize
;
65 /* use constant point size */
66 size
= ctx
->Point
.Size
;
68 /* always clamp to user-specified limits */
69 size
= CLAMP(size
, ctx
->Point
.MinSize
, ctx
->Point
.MaxSize
);
70 /* clamp to implementation limits */
72 size
= CLAMP(size
, ctx
->Const
.MinPointSizeAA
, ctx
->Const
.MaxPointSizeAA
);
74 size
= CLAMP(size
, ctx
->Const
.MinPointSize
, ctx
->Const
.MaxPointSize
);
84 sprite_point(GLcontext
*ctx
, const SWvertex
*vert
)
86 SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
89 GLuint tCoords
[MAX_TEXTURE_COORD_UNITS
+ 1];
90 GLuint numTcoords
= 0;
96 if (ctx
->DrawBuffer
->Visual
.depthBits
<= 16)
97 span
.z
= FloatToFixed(vert
->attrib
[FRAG_ATTRIB_WPOS
][2] + 0.5F
);
99 span
.z
= (GLuint
) (vert
->attrib
[FRAG_ATTRIB_WPOS
][2] + 0.5F
);
102 size
= get_size(ctx
, vert
, GL_FALSE
);
105 INIT_SPAN(span
, GL_POINT
);
106 span
.interpMask
= SPAN_Z
| SPAN_RGBA
;
108 span
.facing
= swrast
->PointLineFacing
;
110 span
.red
= ChanToFixed(vert
->color
[0]);
111 span
.green
= ChanToFixed(vert
->color
[1]);
112 span
.blue
= ChanToFixed(vert
->color
[2]);
113 span
.alpha
= ChanToFixed(vert
->color
[3]);
119 /* need these for fragment programs */
120 span
.attrStart
[FRAG_ATTRIB_WPOS
][3] = 1.0F
;
121 span
.attrStepX
[FRAG_ATTRIB_WPOS
][3] = 0.0F
;
122 span
.attrStepY
[FRAG_ATTRIB_WPOS
][3] = 0.0F
;
127 /* texcoord / pointcoord interpolants */
130 if (ctx
->Point
.SpriteOrigin
== GL_LOWER_LEFT
) {
137 t0
= 1.0F
+ 0.5F
* dtdy
;
141 if (attr
>= FRAG_ATTRIB_TEX0
&& attr
<= FRAG_ATTRIB_TEX7
) {
142 /* a texcoord attribute */
143 const GLuint u
= attr
- FRAG_ATTRIB_TEX0
;
144 ASSERT(u
< Elements(ctx
->Point
.CoordReplace
));
145 if (ctx
->Point
.CoordReplace
[u
]) {
146 tCoords
[numTcoords
++] = attr
;
148 if (ctx
->Point
.SpriteRMode
== GL_ZERO
)
150 else if (ctx
->Point
.SpriteRMode
== GL_S
)
151 r
= vert
->attrib
[attr
][0];
153 r
= vert
->attrib
[attr
][2];
155 span
.attrStart
[attr
][0] = s
;
156 span
.attrStart
[attr
][1] = 0.0; /* overwritten below */
157 span
.attrStart
[attr
][2] = r
;
158 span
.attrStart
[attr
][3] = 1.0;
160 span
.attrStepX
[attr
][0] = dsdx
;
161 span
.attrStepX
[attr
][1] = 0.0;
162 span
.attrStepX
[attr
][2] = 0.0;
163 span
.attrStepX
[attr
][3] = 0.0;
165 span
.attrStepY
[attr
][0] = 0.0;
166 span
.attrStepY
[attr
][1] = dtdy
;
167 span
.attrStepY
[attr
][2] = 0.0;
168 span
.attrStepY
[attr
][3] = 0.0;
173 else if (attr
== FRAG_ATTRIB_PNTC
) {
174 /* GLSL gl_PointCoord.xy (.zw undefined) */
175 span
.attrStart
[FRAG_ATTRIB_PNTC
][0] = 0.0;
176 span
.attrStart
[FRAG_ATTRIB_PNTC
][1] = 0.0; /* t0 set below */
177 span
.attrStepX
[FRAG_ATTRIB_PNTC
][0] = dsdx
;
178 span
.attrStepX
[FRAG_ATTRIB_PNTC
][1] = 0.0;
179 span
.attrStepY
[FRAG_ATTRIB_PNTC
][0] = 0.0;
180 span
.attrStepY
[FRAG_ATTRIB_PNTC
][1] = dtdy
;
181 tCoords
[numTcoords
++] = FRAG_ATTRIB_PNTC
;
184 /* use vertex's texcoord/attrib */
185 COPY_4V(span
.attrStart
[attr
], vert
->attrib
[attr
]);
186 ASSIGN_4V(span
.attrStepX
[attr
], 0, 0, 0, 0);
187 ASSIGN_4V(span
.attrStepY
[attr
], 0, 0, 0, 0);
191 /* compute pos, bounds and render */
193 const GLfloat x
= vert
->attrib
[FRAG_ATTRIB_WPOS
][0];
194 const GLfloat y
= vert
->attrib
[FRAG_ATTRIB_WPOS
][1];
195 GLint iSize
= (GLint
) (size
+ 0.5F
);
196 GLint xmin
, xmax
, ymin
, ymax
, iy
;
200 iSize
= MAX2(1, iSize
);
205 xmin
= (GLint
) (x
- iRadius
);
206 xmax
= (GLint
) (x
+ iRadius
);
207 ymin
= (GLint
) (y
- iRadius
);
208 ymax
= (GLint
) (y
+ iRadius
);
212 /* 0.501 factor allows conformance to pass */
213 xmin
= (GLint
) (x
+ 0.501) - iRadius
;
214 xmax
= xmin
+ iSize
- 1;
215 ymin
= (GLint
) (y
+ 0.501) - iRadius
;
216 ymax
= ymin
+ iSize
- 1;
220 for (iy
= ymin
; iy
<= ymax
; iy
++) {
222 /* setup texcoord T for this row */
223 for (i
= 0; i
< numTcoords
; i
++) {
224 span
.attrStart
[tCoords
[i
]][1] = tcoord
;
227 /* these might get changed by span clipping */
230 span
.end
= xmax
- xmin
+ 1;
232 _swrast_write_rgba_span(ctx
, &span
);
241 * Draw smooth/antialiased point. RGB or CI mode.
244 smooth_point(GLcontext
*ctx
, const SWvertex
*vert
)
246 SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
248 GLfloat size
, alphaAtten
;
253 if (ctx
->DrawBuffer
->Visual
.depthBits
<= 16)
254 span
.z
= FloatToFixed(vert
->attrib
[FRAG_ATTRIB_WPOS
][2] + 0.5F
);
256 span
.z
= (GLuint
) (vert
->attrib
[FRAG_ATTRIB_WPOS
][2] + 0.5F
);
259 size
= get_size(ctx
, vert
, GL_TRUE
);
261 /* alpha attenuation / fade factor */
262 if (ctx
->Multisample
._Enabled
) {
263 if (vert
->pointSize
>= ctx
->Point
.Threshold
) {
267 GLfloat dsize
= vert
->pointSize
/ ctx
->Point
.Threshold
;
268 alphaAtten
= dsize
* dsize
;
274 (void) alphaAtten
; /* not used */
277 INIT_SPAN(span
, GL_POINT
);
278 span
.interpMask
= SPAN_Z
| SPAN_RGBA
;
279 span
.arrayMask
= SPAN_COVERAGE
| SPAN_MASK
;
281 span
.facing
= swrast
->PointLineFacing
;
283 span
.red
= ChanToFixed(vert
->color
[0]);
284 span
.green
= ChanToFixed(vert
->color
[1]);
285 span
.blue
= ChanToFixed(vert
->color
[2]);
286 span
.alpha
= ChanToFixed(vert
->color
[3]);
292 /* need these for fragment programs */
293 span
.attrStart
[FRAG_ATTRIB_WPOS
][3] = 1.0F
;
294 span
.attrStepX
[FRAG_ATTRIB_WPOS
][3] = 0.0F
;
295 span
.attrStepY
[FRAG_ATTRIB_WPOS
][3] = 0.0F
;
298 COPY_4V(span
.attrStart
[attr
], vert
->attrib
[attr
]);
299 ASSIGN_4V(span
.attrStepX
[attr
], 0, 0, 0, 0);
300 ASSIGN_4V(span
.attrStepY
[attr
], 0, 0, 0, 0);
303 /* compute pos, bounds and render */
305 const GLfloat x
= vert
->attrib
[FRAG_ATTRIB_WPOS
][0];
306 const GLfloat y
= vert
->attrib
[FRAG_ATTRIB_WPOS
][1];
307 const GLfloat radius
= 0.5F
* size
;
308 const GLfloat rmin
= radius
- 0.7071F
; /* 0.7071 = sqrt(2)/2 */
309 const GLfloat rmax
= radius
+ 0.7071F
;
310 const GLfloat rmin2
= MAX2(0.0F
, rmin
* rmin
);
311 const GLfloat rmax2
= rmax
* rmax
;
312 const GLfloat cscale
= 1.0F
/ (rmax2
- rmin2
);
313 const GLint xmin
= (GLint
) (x
- radius
);
314 const GLint xmax
= (GLint
) (x
+ radius
);
315 const GLint ymin
= (GLint
) (y
- radius
);
316 const GLint ymax
= (GLint
) (y
+ radius
);
319 for (iy
= ymin
; iy
<= ymax
; iy
++) {
321 /* these might get changed by span clipping */
324 span
.end
= xmax
- xmin
+ 1;
326 /* compute coverage for each pixel in span */
327 for (ix
= xmin
; ix
<= xmax
; ix
++) {
328 const GLfloat dx
= ix
- x
+ 0.5F
;
329 const GLfloat dy
= iy
- y
+ 0.5F
;
330 const GLfloat dist2
= dx
* dx
+ dy
* dy
;
334 if (dist2
>= rmin2
) {
335 /* compute partial coverage */
336 coverage
= 1.0F
- (dist2
- rmin2
) * cscale
;
342 span
.array
->mask
[ix
- xmin
] = 1;
345 /* zero coverage - fragment outside the radius */
347 span
.array
->mask
[ix
- xmin
] = 0;
349 span
.array
->coverage
[ix
- xmin
] = coverage
;
353 _swrast_write_rgba_span(ctx
, &span
);
361 * Draw large (size >= 1) non-AA point. RGB or CI mode.
364 large_point(GLcontext
*ctx
, const SWvertex
*vert
)
366 SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
373 if (ctx
->DrawBuffer
->Visual
.depthBits
<= 16)
374 span
.z
= FloatToFixed(vert
->attrib
[FRAG_ATTRIB_WPOS
][2] + 0.5F
);
376 span
.z
= (GLuint
) (vert
->attrib
[FRAG_ATTRIB_WPOS
][2] + 0.5F
);
379 size
= get_size(ctx
, vert
, GL_FALSE
);
382 INIT_SPAN(span
, GL_POINT
);
383 span
.arrayMask
= SPAN_XY
;
384 span
.facing
= swrast
->PointLineFacing
;
386 span
.interpMask
= SPAN_Z
| SPAN_RGBA
;
387 span
.red
= ChanToFixed(vert
->color
[0]);
388 span
.green
= ChanToFixed(vert
->color
[1]);
389 span
.blue
= ChanToFixed(vert
->color
[2]);
390 span
.alpha
= ChanToFixed(vert
->color
[3]);
396 /* need these for fragment programs */
397 span
.attrStart
[FRAG_ATTRIB_WPOS
][3] = 1.0F
;
398 span
.attrStepX
[FRAG_ATTRIB_WPOS
][3] = 0.0F
;
399 span
.attrStepY
[FRAG_ATTRIB_WPOS
][3] = 0.0F
;
402 COPY_4V(span
.attrStart
[attr
], vert
->attrib
[attr
]);
403 ASSIGN_4V(span
.attrStepX
[attr
], 0, 0, 0, 0);
404 ASSIGN_4V(span
.attrStepY
[attr
], 0, 0, 0, 0);
407 /* compute pos, bounds and render */
409 const GLfloat x
= vert
->attrib
[FRAG_ATTRIB_WPOS
][0];
410 const GLfloat y
= vert
->attrib
[FRAG_ATTRIB_WPOS
][1];
411 GLint iSize
= (GLint
) (size
+ 0.5F
);
412 GLint xmin
, xmax
, ymin
, ymax
, ix
, iy
;
415 iSize
= MAX2(1, iSize
);
420 xmin
= (GLint
) (x
- iRadius
);
421 xmax
= (GLint
) (x
+ iRadius
);
422 ymin
= (GLint
) (y
- iRadius
);
423 ymax
= (GLint
) (y
+ iRadius
);
427 /* 0.501 factor allows conformance to pass */
428 xmin
= (GLint
) (x
+ 0.501) - iRadius
;
429 xmax
= xmin
+ iSize
- 1;
430 ymin
= (GLint
) (y
+ 0.501) - iRadius
;
431 ymax
= ymin
+ iSize
- 1;
434 /* generate fragments */
436 for (iy
= ymin
; iy
<= ymax
; iy
++) {
437 for (ix
= xmin
; ix
<= xmax
; ix
++) {
438 span
.array
->x
[span
.end
] = ix
;
439 span
.array
->y
[span
.end
] = iy
;
443 assert(span
.end
<= MAX_WIDTH
);
444 _swrast_write_rgba_span(ctx
, &span
);
450 * Draw size=1, single-pixel point
453 pixel_point(GLcontext
*ctx
, const SWvertex
*vert
)
455 SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
457 * Note that unlike the other functions, we put single-pixel points
458 * into a special span array in order to render as many points as
459 * possible with a single _swrast_write_rgba_span() call.
461 SWspan
*span
= &(swrast
->PointSpan
);
467 span
->interpMask
= 0;
468 span
->arrayMask
= SPAN_XY
| SPAN_Z
;
469 span
->arrayMask
|= SPAN_RGBA
;
470 /*span->arrayMask |= SPAN_LAMBDA;*/
471 span
->arrayAttribs
= swrast
->_ActiveAttribMask
; /* we'll produce these vals */
473 /* need these for fragment programs */
474 span
->attrStart
[FRAG_ATTRIB_WPOS
][3] = 1.0F
;
475 span
->attrStepX
[FRAG_ATTRIB_WPOS
][3] = 0.0F
;
476 span
->attrStepY
[FRAG_ATTRIB_WPOS
][3] = 0.0F
;
478 /* check if we need to flush */
479 if (span
->end
>= MAX_WIDTH
||
480 (swrast
->_RasterMask
& (BLEND_BIT
| LOGIC_OP_BIT
| MASKING_BIT
)) ||
481 span
->facing
!= swrast
->PointLineFacing
) {
483 _swrast_write_rgba_span(ctx
, span
);
490 span
->facing
= swrast
->PointLineFacing
;
492 /* fragment attributes */
493 span
->array
->rgba
[count
][RCOMP
] = vert
->color
[0];
494 span
->array
->rgba
[count
][GCOMP
] = vert
->color
[1];
495 span
->array
->rgba
[count
][BCOMP
] = vert
->color
[2];
496 span
->array
->rgba
[count
][ACOMP
] = vert
->color
[3];
499 COPY_4V(span
->array
->attribs
[attr
][count
], vert
->attrib
[attr
]);
502 /* fragment position */
503 span
->array
->x
[count
] = (GLint
) vert
->attrib
[FRAG_ATTRIB_WPOS
][0];
504 span
->array
->y
[count
] = (GLint
) vert
->attrib
[FRAG_ATTRIB_WPOS
][1];
505 span
->array
->z
[count
] = (GLint
) (vert
->attrib
[FRAG_ATTRIB_WPOS
][2] + 0.5F
);
507 span
->end
= count
+ 1;
508 ASSERT(span
->end
<= MAX_WIDTH
);
513 * Add specular color to primary color, draw point, restore original
517 _swrast_add_spec_terms_point(GLcontext
*ctx
, const SWvertex
*v0
)
519 SWvertex
*ncv0
= (SWvertex
*) v0
; /* cast away const */
520 GLfloat rSum
, gSum
, bSum
;
524 COPY_CHAN4(cSave
, ncv0
->color
);
526 rSum
= CHAN_TO_FLOAT(ncv0
->color
[0]) + ncv0
->attrib
[FRAG_ATTRIB_COL1
][0];
527 gSum
= CHAN_TO_FLOAT(ncv0
->color
[1]) + ncv0
->attrib
[FRAG_ATTRIB_COL1
][1];
528 bSum
= CHAN_TO_FLOAT(ncv0
->color
[2]) + ncv0
->attrib
[FRAG_ATTRIB_COL1
][2];
529 UNCLAMPED_FLOAT_TO_CHAN(ncv0
->color
[0], rSum
);
530 UNCLAMPED_FLOAT_TO_CHAN(ncv0
->color
[1], gSum
);
531 UNCLAMPED_FLOAT_TO_CHAN(ncv0
->color
[2], bSum
);
533 SWRAST_CONTEXT(ctx
)->SpecPoint(ctx
, ncv0
);
535 COPY_CHAN4(ncv0
->color
, cSave
);
540 * Examine current state to determine which point drawing function to use.
543 _swrast_choose_point(GLcontext
*ctx
)
545 SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
546 const GLfloat size
= CLAMP(ctx
->Point
.Size
,
550 if (ctx
->RenderMode
== GL_RENDER
) {
551 if (ctx
->Point
.PointSprite
) {
552 swrast
->Point
= sprite_point
;
554 else if (ctx
->Point
.SmoothFlag
) {
555 swrast
->Point
= smooth_point
;
557 else if (size
> 1.0 ||
558 ctx
->Point
._Attenuated
||
559 ctx
->VertexProgram
.PointSizeEnabled
) {
560 swrast
->Point
= large_point
;
563 swrast
->Point
= pixel_point
;
566 else if (ctx
->RenderMode
== GL_FEEDBACK
) {
567 swrast
->Point
= _swrast_feedback_point
;
571 swrast
->Point
= _swrast_select_point
;