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.0 + 0.5 * 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
);
247 const GLboolean ciMode
= !ctx
->Visual
.rgbMode
;
249 GLfloat size
, alphaAtten
;
254 if (ctx
->DrawBuffer
->Visual
.depthBits
<= 16)
255 span
.z
= FloatToFixed(vert
->attrib
[FRAG_ATTRIB_WPOS
][2] + 0.5F
);
257 span
.z
= (GLuint
) (vert
->attrib
[FRAG_ATTRIB_WPOS
][2] + 0.5F
);
260 size
= get_size(ctx
, vert
, GL_TRUE
);
262 /* alpha attenuation / fade factor */
263 if (ctx
->Multisample
._Enabled
) {
264 if (vert
->pointSize
>= ctx
->Point
.Threshold
) {
268 GLfloat dsize
= vert
->pointSize
/ ctx
->Point
.Threshold
;
269 alphaAtten
= dsize
* dsize
;
275 (void) alphaAtten
; /* not used */
278 INIT_SPAN(span
, GL_POINT
);
279 span
.interpMask
= SPAN_Z
| SPAN_RGBA
;
280 span
.arrayMask
= SPAN_COVERAGE
| SPAN_MASK
;
282 span
.facing
= swrast
->PointLineFacing
;
284 span
.red
= ChanToFixed(vert
->color
[0]);
285 span
.green
= ChanToFixed(vert
->color
[1]);
286 span
.blue
= ChanToFixed(vert
->color
[2]);
287 span
.alpha
= ChanToFixed(vert
->color
[3]);
293 /* need these for fragment programs */
294 span
.attrStart
[FRAG_ATTRIB_WPOS
][3] = 1.0F
;
295 span
.attrStepX
[FRAG_ATTRIB_WPOS
][3] = 0.0F
;
296 span
.attrStepY
[FRAG_ATTRIB_WPOS
][3] = 0.0F
;
299 COPY_4V(span
.attrStart
[attr
], vert
->attrib
[attr
]);
300 ASSIGN_4V(span
.attrStepX
[attr
], 0, 0, 0, 0);
301 ASSIGN_4V(span
.attrStepY
[attr
], 0, 0, 0, 0);
304 /* compute pos, bounds and render */
306 const GLfloat x
= vert
->attrib
[FRAG_ATTRIB_WPOS
][0];
307 const GLfloat y
= vert
->attrib
[FRAG_ATTRIB_WPOS
][1];
308 const GLfloat radius
= 0.5F
* size
;
309 const GLfloat rmin
= radius
- 0.7071F
; /* 0.7071 = sqrt(2)/2 */
310 const GLfloat rmax
= radius
+ 0.7071F
;
311 const GLfloat rmin2
= MAX2(0.0F
, rmin
* rmin
);
312 const GLfloat rmax2
= rmax
* rmax
;
313 const GLfloat cscale
= 1.0F
/ (rmax2
- rmin2
);
314 const GLint xmin
= (GLint
) (x
- radius
);
315 const GLint xmax
= (GLint
) (x
+ radius
);
316 const GLint ymin
= (GLint
) (y
- radius
);
317 const GLint ymax
= (GLint
) (y
+ radius
);
320 for (iy
= ymin
; iy
<= ymax
; iy
++) {
322 /* these might get changed by span clipping */
325 span
.end
= xmax
- xmin
+ 1;
327 /* compute coverage for each pixel in span */
328 for (ix
= xmin
; ix
<= xmax
; ix
++) {
329 const GLfloat dx
= ix
- x
+ 0.5F
;
330 const GLfloat dy
= iy
- y
+ 0.5F
;
331 const GLfloat dist2
= dx
* dx
+ dy
* dy
;
335 if (dist2
>= rmin2
) {
336 /* compute partial coverage */
337 coverage
= 1.0F
- (dist2
- rmin2
) * cscale
;
339 /* coverage in [0,15] */
347 span
.array
->mask
[ix
- xmin
] = 1;
350 /* zero coverage - fragment outside the radius */
352 span
.array
->mask
[ix
- xmin
] = 0;
354 span
.array
->coverage
[ix
- xmin
] = coverage
;
358 _swrast_write_rgba_span(ctx
, &span
);
366 * Draw large (size >= 1) non-AA point. RGB or CI mode.
369 large_point(GLcontext
*ctx
, const SWvertex
*vert
)
371 SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
372 const GLboolean ciMode
= !ctx
->Visual
.rgbMode
;
379 if (ctx
->DrawBuffer
->Visual
.depthBits
<= 16)
380 span
.z
= FloatToFixed(vert
->attrib
[FRAG_ATTRIB_WPOS
][2] + 0.5F
);
382 span
.z
= (GLuint
) (vert
->attrib
[FRAG_ATTRIB_WPOS
][2] + 0.5F
);
385 size
= get_size(ctx
, vert
, GL_FALSE
);
388 INIT_SPAN(span
, GL_POINT
);
389 span
.arrayMask
= SPAN_XY
;
390 span
.facing
= swrast
->PointLineFacing
;
393 span
.interpMask
= SPAN_Z
| SPAN_INDEX
;
394 span
.index
= FloatToFixed(vert
->attrib
[FRAG_ATTRIB_CI
][0]);
398 span
.interpMask
= SPAN_Z
| SPAN_RGBA
;
399 span
.red
= ChanToFixed(vert
->color
[0]);
400 span
.green
= ChanToFixed(vert
->color
[1]);
401 span
.blue
= ChanToFixed(vert
->color
[2]);
402 span
.alpha
= ChanToFixed(vert
->color
[3]);
409 /* need these for fragment programs */
410 span
.attrStart
[FRAG_ATTRIB_WPOS
][3] = 1.0F
;
411 span
.attrStepX
[FRAG_ATTRIB_WPOS
][3] = 0.0F
;
412 span
.attrStepY
[FRAG_ATTRIB_WPOS
][3] = 0.0F
;
415 COPY_4V(span
.attrStart
[attr
], vert
->attrib
[attr
]);
416 ASSIGN_4V(span
.attrStepX
[attr
], 0, 0, 0, 0);
417 ASSIGN_4V(span
.attrStepY
[attr
], 0, 0, 0, 0);
420 /* compute pos, bounds and render */
422 const GLfloat x
= vert
->attrib
[FRAG_ATTRIB_WPOS
][0];
423 const GLfloat y
= vert
->attrib
[FRAG_ATTRIB_WPOS
][1];
424 GLint iSize
= (GLint
) (size
+ 0.5F
);
425 GLint xmin
, xmax
, ymin
, ymax
, ix
, iy
;
428 iSize
= MAX2(1, iSize
);
433 xmin
= (GLint
) (x
- iRadius
);
434 xmax
= (GLint
) (x
+ iRadius
);
435 ymin
= (GLint
) (y
- iRadius
);
436 ymax
= (GLint
) (y
+ iRadius
);
440 /* 0.501 factor allows conformance to pass */
441 xmin
= (GLint
) (x
+ 0.501) - iRadius
;
442 xmax
= xmin
+ iSize
- 1;
443 ymin
= (GLint
) (y
+ 0.501) - iRadius
;
444 ymax
= ymin
+ iSize
- 1;
447 /* generate fragments */
449 for (iy
= ymin
; iy
<= ymax
; iy
++) {
450 for (ix
= xmin
; ix
<= xmax
; ix
++) {
451 span
.array
->x
[span
.end
] = ix
;
452 span
.array
->y
[span
.end
] = iy
;
456 assert(span
.end
<= MAX_WIDTH
);
457 _swrast_write_rgba_span(ctx
, &span
);
463 * Draw size=1, single-pixel point
466 pixel_point(GLcontext
*ctx
, const SWvertex
*vert
)
468 SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
469 const GLboolean ciMode
= !ctx
->Visual
.rgbMode
;
471 * Note that unlike the other functions, we put single-pixel points
472 * into a special span array in order to render as many points as
473 * possible with a single _swrast_write_rgba_span() call.
475 SWspan
*span
= &(swrast
->PointSpan
);
481 span
->interpMask
= 0;
482 span
->arrayMask
= SPAN_XY
| SPAN_Z
;
484 span
->arrayMask
|= SPAN_INDEX
;
486 span
->arrayMask
|= SPAN_RGBA
;
487 /*span->arrayMask |= SPAN_LAMBDA;*/
488 span
->arrayAttribs
= swrast
->_ActiveAttribMask
; /* we'll produce these vals */
490 /* need these for fragment programs */
491 span
->attrStart
[FRAG_ATTRIB_WPOS
][3] = 1.0F
;
492 span
->attrStepX
[FRAG_ATTRIB_WPOS
][3] = 0.0F
;
493 span
->attrStepY
[FRAG_ATTRIB_WPOS
][3] = 0.0F
;
495 /* check if we need to flush */
496 if (span
->end
>= MAX_WIDTH
||
497 (swrast
->_RasterMask
& (BLEND_BIT
| LOGIC_OP_BIT
| MASKING_BIT
)) ||
498 span
->facing
!= swrast
->PointLineFacing
) {
501 _swrast_write_index_span(ctx
, span
);
503 _swrast_write_rgba_span(ctx
, span
);
510 span
->facing
= swrast
->PointLineFacing
;
512 /* fragment attributes */
514 span
->array
->index
[count
] = (GLuint
) vert
->attrib
[FRAG_ATTRIB_CI
][0];
517 span
->array
->rgba
[count
][RCOMP
] = vert
->color
[0];
518 span
->array
->rgba
[count
][GCOMP
] = vert
->color
[1];
519 span
->array
->rgba
[count
][BCOMP
] = vert
->color
[2];
520 span
->array
->rgba
[count
][ACOMP
] = vert
->color
[3];
523 COPY_4V(span
->array
->attribs
[attr
][count
], vert
->attrib
[attr
]);
526 /* fragment position */
527 span
->array
->x
[count
] = (GLint
) vert
->attrib
[FRAG_ATTRIB_WPOS
][0];
528 span
->array
->y
[count
] = (GLint
) vert
->attrib
[FRAG_ATTRIB_WPOS
][1];
529 span
->array
->z
[count
] = (GLint
) (vert
->attrib
[FRAG_ATTRIB_WPOS
][2] + 0.5F
);
531 span
->end
= count
+ 1;
532 ASSERT(span
->end
<= MAX_WIDTH
);
537 * Add specular color to primary color, draw point, restore original
541 _swrast_add_spec_terms_point(GLcontext
*ctx
, const SWvertex
*v0
)
543 SWvertex
*ncv0
= (SWvertex
*) v0
; /* cast away const */
544 GLfloat rSum
, gSum
, bSum
;
548 COPY_CHAN4(cSave
, ncv0
->color
);
550 rSum
= CHAN_TO_FLOAT(ncv0
->color
[0]) + ncv0
->attrib
[FRAG_ATTRIB_COL1
][0];
551 gSum
= CHAN_TO_FLOAT(ncv0
->color
[1]) + ncv0
->attrib
[FRAG_ATTRIB_COL1
][1];
552 bSum
= CHAN_TO_FLOAT(ncv0
->color
[2]) + ncv0
->attrib
[FRAG_ATTRIB_COL1
][2];
553 UNCLAMPED_FLOAT_TO_CHAN(ncv0
->color
[0], rSum
);
554 UNCLAMPED_FLOAT_TO_CHAN(ncv0
->color
[1], gSum
);
555 UNCLAMPED_FLOAT_TO_CHAN(ncv0
->color
[2], bSum
);
557 SWRAST_CONTEXT(ctx
)->SpecPoint(ctx
, ncv0
);
559 COPY_CHAN4(ncv0
->color
, cSave
);
564 * Examine current state to determine which point drawing function to use.
567 _swrast_choose_point(GLcontext
*ctx
)
569 SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
570 const GLfloat size
= CLAMP(ctx
->Point
.Size
,
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 (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
;