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"
31 #include "s_context.h"
32 #include "s_feedback.h"
38 * Used to cull points with invalid coords
40 #define CULL_INVALID(V) \
42 float tmp = (V)->attrib[FRAG_ATTRIB_WPOS][0] \
43 + (V)->attrib[FRAG_ATTRIB_WPOS][1]; \
44 if (IS_INF_OR_NAN(tmp)) \
51 * Get/compute the point size.
52 * The size may come from a vertex shader, or computed with attentuation
53 * or just the glPointSize value.
54 * Must also clamp to user-defined range and implmentation limits.
57 get_size(const GLcontext
*ctx
, const SWvertex
*vert
, GLboolean smoothed
)
61 if (ctx
->Point
._Attenuated
|| ctx
->VertexProgram
.PointSizeEnabled
) {
62 /* use vertex's point size */
63 size
= vert
->pointSize
;
66 /* use constant point size */
67 size
= ctx
->Point
.Size
;
69 /* always clamp to user-specified limits */
70 size
= CLAMP(size
, ctx
->Point
.MinSize
, ctx
->Point
.MaxSize
);
71 /* clamp to implementation limits */
73 size
= CLAMP(size
, ctx
->Const
.MinPointSizeAA
, ctx
->Const
.MaxPointSizeAA
);
75 size
= CLAMP(size
, ctx
->Const
.MinPointSize
, ctx
->Const
.MaxPointSize
);
85 sprite_point(GLcontext
*ctx
, const SWvertex
*vert
)
87 SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
90 GLuint tCoords
[MAX_TEXTURE_COORD_UNITS
+ 1];
91 GLuint numTcoords
= 0;
97 if (ctx
->DrawBuffer
->Visual
.depthBits
<= 16)
98 span
.z
= FloatToFixed(vert
->attrib
[FRAG_ATTRIB_WPOS
][2] + 0.5F
);
100 span
.z
= (GLuint
) (vert
->attrib
[FRAG_ATTRIB_WPOS
][2] + 0.5F
);
103 size
= get_size(ctx
, vert
, GL_FALSE
);
106 INIT_SPAN(span
, GL_POINT
);
107 span
.interpMask
= SPAN_Z
| SPAN_RGBA
;
109 span
.facing
= swrast
->PointLineFacing
;
111 span
.red
= ChanToFixed(vert
->color
[0]);
112 span
.green
= ChanToFixed(vert
->color
[1]);
113 span
.blue
= ChanToFixed(vert
->color
[2]);
114 span
.alpha
= ChanToFixed(vert
->color
[3]);
120 /* need these for fragment programs */
121 span
.attrStart
[FRAG_ATTRIB_WPOS
][3] = 1.0F
;
122 span
.attrStepX
[FRAG_ATTRIB_WPOS
][3] = 0.0F
;
123 span
.attrStepY
[FRAG_ATTRIB_WPOS
][3] = 0.0F
;
128 /* texcoord / pointcoord interpolants */
131 if (ctx
->Point
.SpriteOrigin
== GL_LOWER_LEFT
) {
142 if (attr
>= FRAG_ATTRIB_TEX0
&& attr
< FRAG_ATTRIB_VAR0
) {
143 const GLuint u
= attr
- FRAG_ATTRIB_TEX0
;
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_FOGC
) {
174 /* GLSL gl_PointCoord is stored in fog.zw */
175 span
.attrStart
[FRAG_ATTRIB_FOGC
][2] = 0.0;
176 span
.attrStart
[FRAG_ATTRIB_FOGC
][3] = 0.0; /* t0 set below */
177 span
.attrStepX
[FRAG_ATTRIB_FOGC
][2] = dsdx
;
178 span
.attrStepX
[FRAG_ATTRIB_FOGC
][3] = 0.0;
179 span
.attrStepY
[FRAG_ATTRIB_FOGC
][2] = 0.0;
180 span
.attrStepY
[FRAG_ATTRIB_FOGC
][3] = dtdy
;
181 tCoords
[numTcoords
++] = FRAG_ATTRIB_FOGC
;
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 if (tCoords
[i
] == FRAG_ATTRIB_FOGC
)
225 span
.attrStart
[FRAG_ATTRIB_FOGC
][3] = tcoord
;
227 span
.attrStart
[tCoords
[i
]][1] = tcoord
;
230 /* these might get changed by span clipping */
233 span
.end
= xmax
- xmin
+ 1;
235 _swrast_write_rgba_span(ctx
, &span
);
244 * Draw smooth/antialiased point. RGB or CI mode.
247 smooth_point(GLcontext
*ctx
, const SWvertex
*vert
)
249 SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
250 const GLboolean ciMode
= !ctx
->Visual
.rgbMode
;
252 GLfloat size
, alphaAtten
;
257 if (ctx
->DrawBuffer
->Visual
.depthBits
<= 16)
258 span
.z
= FloatToFixed(vert
->attrib
[FRAG_ATTRIB_WPOS
][2] + 0.5F
);
260 span
.z
= (GLuint
) (vert
->attrib
[FRAG_ATTRIB_WPOS
][2] + 0.5F
);
263 size
= get_size(ctx
, vert
, GL_TRUE
);
265 /* alpha attenuation / fade factor */
266 if (ctx
->Multisample
._Enabled
) {
267 if (vert
->pointSize
>= ctx
->Point
.Threshold
) {
271 GLfloat dsize
= vert
->pointSize
/ ctx
->Point
.Threshold
;
272 alphaAtten
= dsize
* dsize
;
278 (void) alphaAtten
; /* not used */
281 INIT_SPAN(span
, GL_POINT
);
282 span
.interpMask
= SPAN_Z
| SPAN_RGBA
;
283 span
.arrayMask
= SPAN_COVERAGE
| SPAN_MASK
;
285 span
.facing
= swrast
->PointLineFacing
;
287 span
.red
= ChanToFixed(vert
->color
[0]);
288 span
.green
= ChanToFixed(vert
->color
[1]);
289 span
.blue
= ChanToFixed(vert
->color
[2]);
290 span
.alpha
= ChanToFixed(vert
->color
[3]);
296 /* need these for fragment programs */
297 span
.attrStart
[FRAG_ATTRIB_WPOS
][3] = 1.0F
;
298 span
.attrStepX
[FRAG_ATTRIB_WPOS
][3] = 0.0F
;
299 span
.attrStepY
[FRAG_ATTRIB_WPOS
][3] = 0.0F
;
302 COPY_4V(span
.attrStart
[attr
], vert
->attrib
[attr
]);
303 ASSIGN_4V(span
.attrStepX
[attr
], 0, 0, 0, 0);
304 ASSIGN_4V(span
.attrStepY
[attr
], 0, 0, 0, 0);
307 /* compute pos, bounds and render */
309 const GLfloat x
= vert
->attrib
[FRAG_ATTRIB_WPOS
][0];
310 const GLfloat y
= vert
->attrib
[FRAG_ATTRIB_WPOS
][1];
311 const GLfloat radius
= 0.5F
* size
;
312 const GLfloat rmin
= radius
- 0.7071F
; /* 0.7071 = sqrt(2)/2 */
313 const GLfloat rmax
= radius
+ 0.7071F
;
314 const GLfloat rmin2
= MAX2(0.0F
, rmin
* rmin
);
315 const GLfloat rmax2
= rmax
* rmax
;
316 const GLfloat cscale
= 1.0F
/ (rmax2
- rmin2
);
317 const GLint xmin
= (GLint
) (x
- radius
);
318 const GLint xmax
= (GLint
) (x
+ radius
);
319 const GLint ymin
= (GLint
) (y
- radius
);
320 const GLint ymax
= (GLint
) (y
+ radius
);
323 for (iy
= ymin
; iy
<= ymax
; iy
++) {
325 /* these might get changed by span clipping */
328 span
.end
= xmax
- xmin
+ 1;
330 /* compute coverage for each pixel in span */
331 for (ix
= xmin
; ix
<= xmax
; ix
++) {
332 const GLfloat dx
= ix
- x
+ 0.5F
;
333 const GLfloat dy
= iy
- y
+ 0.5F
;
334 const GLfloat dist2
= dx
* dx
+ dy
* dy
;
338 if (dist2
>= rmin2
) {
339 /* compute partial coverage */
340 coverage
= 1.0F
- (dist2
- rmin2
) * cscale
;
342 /* coverage in [0,15] */
350 span
.array
->mask
[ix
- xmin
] = 1;
353 /* zero coverage - fragment outside the radius */
355 span
.array
->mask
[ix
- xmin
] = 0;
357 span
.array
->coverage
[ix
- xmin
] = coverage
;
361 _swrast_write_rgba_span(ctx
, &span
);
369 * Draw large (size >= 1) non-AA point. RGB or CI mode.
372 large_point(GLcontext
*ctx
, const SWvertex
*vert
)
374 SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
375 const GLboolean ciMode
= !ctx
->Visual
.rgbMode
;
382 if (ctx
->DrawBuffer
->Visual
.depthBits
<= 16)
383 span
.z
= FloatToFixed(vert
->attrib
[FRAG_ATTRIB_WPOS
][2] + 0.5F
);
385 span
.z
= (GLuint
) (vert
->attrib
[FRAG_ATTRIB_WPOS
][2] + 0.5F
);
388 size
= get_size(ctx
, vert
, GL_FALSE
);
391 INIT_SPAN(span
, GL_POINT
);
392 span
.arrayMask
= SPAN_XY
;
393 span
.facing
= swrast
->PointLineFacing
;
396 span
.interpMask
= SPAN_Z
| SPAN_INDEX
;
397 span
.index
= FloatToFixed(vert
->attrib
[FRAG_ATTRIB_CI
][0]);
401 span
.interpMask
= SPAN_Z
| SPAN_RGBA
;
402 span
.red
= ChanToFixed(vert
->color
[0]);
403 span
.green
= ChanToFixed(vert
->color
[1]);
404 span
.blue
= ChanToFixed(vert
->color
[2]);
405 span
.alpha
= ChanToFixed(vert
->color
[3]);
412 /* need these for fragment programs */
413 span
.attrStart
[FRAG_ATTRIB_WPOS
][3] = 1.0F
;
414 span
.attrStepX
[FRAG_ATTRIB_WPOS
][3] = 0.0F
;
415 span
.attrStepY
[FRAG_ATTRIB_WPOS
][3] = 0.0F
;
418 COPY_4V(span
.attrStart
[attr
], vert
->attrib
[attr
]);
419 ASSIGN_4V(span
.attrStepX
[attr
], 0, 0, 0, 0);
420 ASSIGN_4V(span
.attrStepY
[attr
], 0, 0, 0, 0);
423 /* compute pos, bounds and render */
425 const GLfloat x
= vert
->attrib
[FRAG_ATTRIB_WPOS
][0];
426 const GLfloat y
= vert
->attrib
[FRAG_ATTRIB_WPOS
][1];
427 GLint iSize
= (GLint
) (size
+ 0.5F
);
428 GLint xmin
, xmax
, ymin
, ymax
, ix
, iy
;
431 iSize
= MAX2(1, iSize
);
436 xmin
= (GLint
) (x
- iRadius
);
437 xmax
= (GLint
) (x
+ iRadius
);
438 ymin
= (GLint
) (y
- iRadius
);
439 ymax
= (GLint
) (y
+ iRadius
);
443 /* 0.501 factor allows conformance to pass */
444 xmin
= (GLint
) (x
+ 0.501) - iRadius
;
445 xmax
= xmin
+ iSize
- 1;
446 ymin
= (GLint
) (y
+ 0.501) - iRadius
;
447 ymax
= ymin
+ iSize
- 1;
450 /* generate fragments */
452 for (iy
= ymin
; iy
<= ymax
; iy
++) {
453 for (ix
= xmin
; ix
<= xmax
; ix
++) {
454 span
.array
->x
[span
.end
] = ix
;
455 span
.array
->y
[span
.end
] = iy
;
459 assert(span
.end
<= MAX_WIDTH
);
460 _swrast_write_rgba_span(ctx
, &span
);
466 * Draw size=1, single-pixel point
469 pixel_point(GLcontext
*ctx
, const SWvertex
*vert
)
471 SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
472 const GLboolean ciMode
= !ctx
->Visual
.rgbMode
;
474 * Note that unlike the other functions, we put single-pixel points
475 * into a special span array in order to render as many points as
476 * possible with a single _swrast_write_rgba_span() call.
478 SWspan
*span
= &(swrast
->PointSpan
);
484 span
->interpMask
= 0;
485 span
->arrayMask
= SPAN_XY
| SPAN_Z
;
487 span
->arrayMask
|= SPAN_INDEX
;
489 span
->arrayMask
|= SPAN_RGBA
;
490 /*span->arrayMask |= SPAN_LAMBDA;*/
491 span
->arrayAttribs
= swrast
->_ActiveAttribMask
; /* we'll produce these vals */
493 /* need these for fragment programs */
494 span
->attrStart
[FRAG_ATTRIB_WPOS
][3] = 1.0F
;
495 span
->attrStepX
[FRAG_ATTRIB_WPOS
][3] = 0.0F
;
496 span
->attrStepY
[FRAG_ATTRIB_WPOS
][3] = 0.0F
;
498 /* check if we need to flush */
499 if (span
->end
>= MAX_WIDTH
||
500 (swrast
->_RasterMask
& (BLEND_BIT
| LOGIC_OP_BIT
| MASKING_BIT
)) ||
501 span
->facing
!= swrast
->PointLineFacing
) {
504 _swrast_write_index_span(ctx
, span
);
506 _swrast_write_rgba_span(ctx
, span
);
513 span
->facing
= swrast
->PointLineFacing
;
515 /* fragment attributes */
517 span
->array
->index
[count
] = (GLuint
) vert
->attrib
[FRAG_ATTRIB_CI
][0];
520 span
->array
->rgba
[count
][RCOMP
] = vert
->color
[0];
521 span
->array
->rgba
[count
][GCOMP
] = vert
->color
[1];
522 span
->array
->rgba
[count
][BCOMP
] = vert
->color
[2];
523 span
->array
->rgba
[count
][ACOMP
] = vert
->color
[3];
526 COPY_4V(span
->array
->attribs
[attr
][count
], vert
->attrib
[attr
]);
529 /* fragment position */
530 span
->array
->x
[count
] = (GLint
) vert
->attrib
[FRAG_ATTRIB_WPOS
][0];
531 span
->array
->y
[count
] = (GLint
) vert
->attrib
[FRAG_ATTRIB_WPOS
][1];
532 span
->array
->z
[count
] = (GLint
) (vert
->attrib
[FRAG_ATTRIB_WPOS
][2] + 0.5F
);
534 span
->end
= count
+ 1;
535 ASSERT(span
->end
<= MAX_WIDTH
);
540 * Add specular color to primary color, draw point, restore original
544 _swrast_add_spec_terms_point(GLcontext
*ctx
, const SWvertex
*v0
)
546 SWvertex
*ncv0
= (SWvertex
*) v0
; /* cast away const */
547 GLfloat rSum
, gSum
, bSum
;
551 COPY_CHAN4(cSave
, ncv0
->color
);
553 rSum
= CHAN_TO_FLOAT(ncv0
->color
[0]) + ncv0
->attrib
[FRAG_ATTRIB_COL1
][0];
554 gSum
= CHAN_TO_FLOAT(ncv0
->color
[1]) + ncv0
->attrib
[FRAG_ATTRIB_COL1
][1];
555 bSum
= CHAN_TO_FLOAT(ncv0
->color
[2]) + ncv0
->attrib
[FRAG_ATTRIB_COL1
][2];
556 UNCLAMPED_FLOAT_TO_CHAN(ncv0
->color
[0], rSum
);
557 UNCLAMPED_FLOAT_TO_CHAN(ncv0
->color
[1], gSum
);
558 UNCLAMPED_FLOAT_TO_CHAN(ncv0
->color
[2], bSum
);
560 SWRAST_CONTEXT(ctx
)->SpecPoint(ctx
, ncv0
);
562 COPY_CHAN4(ncv0
->color
, cSave
);
567 * Examine current state to determine which point drawing function to use.
570 _swrast_choose_point(GLcontext
*ctx
)
572 SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
574 if (ctx
->RenderMode
== GL_RENDER
) {
575 if (ctx
->Point
.PointSprite
) {
576 swrast
->Point
= sprite_point
;
578 else if (ctx
->Point
.SmoothFlag
) {
579 swrast
->Point
= smooth_point
;
581 else if (ctx
->Point
.Size
> 1.0 ||
582 ctx
->Point
._Attenuated
||
583 ctx
->VertexProgram
.PointSizeEnabled
) {
584 swrast
->Point
= large_point
;
587 swrast
->Point
= pixel_point
;
590 else if (ctx
->RenderMode
== GL_FEEDBACK
) {
591 swrast
->Point
= _swrast_feedback_point
;
595 swrast
->Point
= _swrast_select_point
;