Merge commit 'origin/gallium-0.1' into gallium-0.2
[mesa.git] / src / mesa / swrast / s_points.c
1 /*
2 * Mesa 3-D graphics library
3 * Version: 7.1
4 *
5 * Copyright (C) 1999-2007 Brian Paul All Rights Reserved.
6 *
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:
13 *
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
16 *
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.
23 */
24
25
26 #include "main/glheader.h"
27 #include "main/colormac.h"
28 #include "main/context.h"
29 #include "main/macros.h"
30 #include "main/texstate.h"
31 #include "s_context.h"
32 #include "s_feedback.h"
33 #include "s_points.h"
34 #include "s_span.h"
35
36
37 /**
38 * Used to cull points with invalid coords
39 */
40 #define CULL_INVALID(V) \
41 do { \
42 float tmp = (V)->attrib[FRAG_ATTRIB_WPOS][0] \
43 + (V)->attrib[FRAG_ATTRIB_WPOS][1]; \
44 if (IS_INF_OR_NAN(tmp)) \
45 return; \
46 } while(0)
47
48
49
50 /**
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.
55 */
56 static INLINE GLfloat
57 get_size(const GLcontext *ctx, const SWvertex *vert, GLboolean smoothed)
58 {
59 GLfloat size;
60
61 if (ctx->Point._Attenuated || ctx->VertexProgram.PointSizeEnabled) {
62 /* use vertex's point size */
63 size = vert->pointSize;
64 }
65 else {
66 /* use constant point size */
67 size = ctx->Point.Size;
68 }
69 /* always clamp to user-specified limits */
70 size = CLAMP(size, ctx->Point.MinSize, ctx->Point.MaxSize);
71 /* clamp to implementation limits */
72 if (smoothed)
73 size = CLAMP(size, ctx->Const.MinPointSizeAA, ctx->Const.MaxPointSizeAA);
74 else
75 size = CLAMP(size, ctx->Const.MinPointSize, ctx->Const.MaxPointSize);
76
77 return size;
78 }
79
80
81 /**
82 * Draw a point sprite
83 */
84 static void
85 sprite_point(GLcontext *ctx, const SWvertex *vert)
86 {
87 SWcontext *swrast = SWRAST_CONTEXT(ctx);
88 SWspan span;
89 GLfloat size;
90 GLuint tCoords[MAX_TEXTURE_COORD_UNITS + 1];
91 GLuint numTcoords = 0;
92 GLfloat t0, dtdy;
93
94 CULL_INVALID(vert);
95
96 /* z coord */
97 if (ctx->DrawBuffer->Visual.depthBits <= 16)
98 span.z = FloatToFixed(vert->attrib[FRAG_ATTRIB_WPOS][2] + 0.5F);
99 else
100 span.z = (GLuint) (vert->attrib[FRAG_ATTRIB_WPOS][2] + 0.5F);
101 span.zStep = 0;
102
103 size = get_size(ctx, vert, GL_FALSE);
104
105 /* span init */
106 INIT_SPAN(span, GL_POINT);
107 span.interpMask = SPAN_Z | SPAN_RGBA;
108
109 span.facing = swrast->PointLineFacing;
110
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]);
115 span.redStep = 0;
116 span.greenStep = 0;
117 span.blueStep = 0;
118 span.alphaStep = 0;
119
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;
124
125 {
126 GLfloat s, r, dsdx;
127
128 /* texcoord / pointcoord interpolants */
129 s = 0.0;
130 dsdx = 1.0 / size;
131 if (ctx->Point.SpriteOrigin == GL_LOWER_LEFT) {
132 t0 = 0.0;
133 dtdy = 1.0 / size;
134 }
135 else {
136 /* GL_UPPER_LEFT */
137 t0 = 1.0;
138 dtdy = -1.0 / size;
139 }
140
141 ATTRIB_LOOP_BEGIN
142 if (attr >= FRAG_ATTRIB_TEX0 && attr < FRAG_ATTRIB_VAR0) {
143 const GLuint u = attr - FRAG_ATTRIB_TEX0;
144 /* a texcoord */
145 if (ctx->Point.CoordReplace[u]) {
146 tCoords[numTcoords++] = attr;
147
148 if (ctx->Point.SpriteRMode == GL_ZERO)
149 r = 0.0F;
150 else if (ctx->Point.SpriteRMode == GL_S)
151 r = vert->attrib[attr][0];
152 else /* GL_R */
153 r = vert->attrib[attr][2];
154
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;
159
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;
164
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;
169
170 continue;
171 }
172 }
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;
182 continue;
183 }
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);
188 ATTRIB_LOOP_END;
189 }
190
191 /* compute pos, bounds and render */
192 {
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;
197 GLint iRadius;
198 GLfloat tcoord = t0;
199
200 iSize = MAX2(1, iSize);
201 iRadius = iSize / 2;
202
203 if (iSize & 1) {
204 /* odd size */
205 xmin = (GLint) (x - iRadius);
206 xmax = (GLint) (x + iRadius);
207 ymin = (GLint) (y - iRadius);
208 ymax = (GLint) (y + iRadius);
209 }
210 else {
211 /* even size */
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;
217 }
218
219 /* render spans */
220 for (iy = ymin; iy <= ymax; iy++) {
221 GLuint i;
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;
226 else
227 span.attrStart[tCoords[i]][1] = tcoord;
228 }
229
230 /* these might get changed by span clipping */
231 span.x = xmin;
232 span.y = iy;
233 span.end = xmax - xmin + 1;
234
235 _swrast_write_rgba_span(ctx, &span);
236
237 tcoord += dtdy;
238 }
239 }
240 }
241
242
243 /**
244 * Draw smooth/antialiased point. RGB or CI mode.
245 */
246 static void
247 smooth_point(GLcontext *ctx, const SWvertex *vert)
248 {
249 SWcontext *swrast = SWRAST_CONTEXT(ctx);
250 const GLboolean ciMode = !ctx->Visual.rgbMode;
251 SWspan span;
252 GLfloat size, alphaAtten;
253
254 CULL_INVALID(vert);
255
256 /* z coord */
257 if (ctx->DrawBuffer->Visual.depthBits <= 16)
258 span.z = FloatToFixed(vert->attrib[FRAG_ATTRIB_WPOS][2] + 0.5F);
259 else
260 span.z = (GLuint) (vert->attrib[FRAG_ATTRIB_WPOS][2] + 0.5F);
261 span.zStep = 0;
262
263 size = get_size(ctx, vert, GL_TRUE);
264
265 /* alpha attenuation / fade factor */
266 if (ctx->Multisample._Enabled) {
267 if (vert->pointSize >= ctx->Point.Threshold) {
268 alphaAtten = 1.0F;
269 }
270 else {
271 GLfloat dsize = vert->pointSize / ctx->Point.Threshold;
272 alphaAtten = dsize * dsize;
273 }
274 }
275 else {
276 alphaAtten = 1.0;
277 }
278 (void) alphaAtten; /* not used */
279
280 /* span init */
281 INIT_SPAN(span, GL_POINT);
282 span.interpMask = SPAN_Z | SPAN_RGBA;
283 span.arrayMask = SPAN_COVERAGE | SPAN_MASK;
284
285 span.facing = swrast->PointLineFacing;
286
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]);
291 span.redStep = 0;
292 span.greenStep = 0;
293 span.blueStep = 0;
294 span.alphaStep = 0;
295
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;
300
301 ATTRIB_LOOP_BEGIN
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);
305 ATTRIB_LOOP_END
306
307 /* compute pos, bounds and render */
308 {
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);
321 GLint ix, iy;
322
323 for (iy = ymin; iy <= ymax; iy++) {
324
325 /* these might get changed by span clipping */
326 span.x = xmin;
327 span.y = iy;
328 span.end = xmax - xmin + 1;
329
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;
335 GLfloat coverage;
336
337 if (dist2 < rmax2) {
338 if (dist2 >= rmin2) {
339 /* compute partial coverage */
340 coverage = 1.0F - (dist2 - rmin2) * cscale;
341 if (ciMode) {
342 /* coverage in [0,15] */
343 coverage *= 15.0;
344 }
345 }
346 else {
347 /* full coverage */
348 coverage = 1.0F;
349 }
350 span.array->mask[ix - xmin] = 1;
351 }
352 else {
353 /* zero coverage - fragment outside the radius */
354 coverage = 0.0;
355 span.array->mask[ix - xmin] = 0;
356 }
357 span.array->coverage[ix - xmin] = coverage;
358 }
359
360 /* render span */
361 _swrast_write_rgba_span(ctx, &span);
362
363 }
364 }
365 }
366
367
368 /**
369 * Draw large (size >= 1) non-AA point. RGB or CI mode.
370 */
371 static void
372 large_point(GLcontext *ctx, const SWvertex *vert)
373 {
374 SWcontext *swrast = SWRAST_CONTEXT(ctx);
375 const GLboolean ciMode = !ctx->Visual.rgbMode;
376 SWspan span;
377 GLfloat size;
378
379 CULL_INVALID(vert);
380
381 /* z coord */
382 if (ctx->DrawBuffer->Visual.depthBits <= 16)
383 span.z = FloatToFixed(vert->attrib[FRAG_ATTRIB_WPOS][2] + 0.5F);
384 else
385 span.z = (GLuint) (vert->attrib[FRAG_ATTRIB_WPOS][2] + 0.5F);
386 span.zStep = 0;
387
388 size = get_size(ctx, vert, GL_FALSE);
389
390 /* span init */
391 INIT_SPAN(span, GL_POINT);
392 span.arrayMask = SPAN_XY;
393 span.facing = swrast->PointLineFacing;
394
395 if (ciMode) {
396 span.interpMask = SPAN_Z | SPAN_INDEX;
397 span.index = FloatToFixed(vert->attrib[FRAG_ATTRIB_CI][0]);
398 span.indexStep = 0;
399 }
400 else {
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]);
406 span.redStep = 0;
407 span.greenStep = 0;
408 span.blueStep = 0;
409 span.alphaStep = 0;
410 }
411
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;
416
417 ATTRIB_LOOP_BEGIN
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);
421 ATTRIB_LOOP_END
422
423 /* compute pos, bounds and render */
424 {
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;
429 GLint iRadius;
430
431 iSize = MAX2(1, iSize);
432 iRadius = iSize / 2;
433
434 if (iSize & 1) {
435 /* odd size */
436 xmin = (GLint) (x - iRadius);
437 xmax = (GLint) (x + iRadius);
438 ymin = (GLint) (y - iRadius);
439 ymax = (GLint) (y + iRadius);
440 }
441 else {
442 /* even size */
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;
448 }
449
450 /* generate fragments */
451 span.end = 0;
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;
456 span.end++;
457 }
458 }
459 assert(span.end <= MAX_WIDTH);
460 _swrast_write_rgba_span(ctx, &span);
461 }
462 }
463
464
465 /**
466 * Draw size=1, single-pixel point
467 */
468 static void
469 pixel_point(GLcontext *ctx, const SWvertex *vert)
470 {
471 SWcontext *swrast = SWRAST_CONTEXT(ctx);
472 const GLboolean ciMode = !ctx->Visual.rgbMode;
473 /*
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.
477 */
478 SWspan *span = &(swrast->PointSpan);
479 GLuint count;
480
481 CULL_INVALID(vert);
482
483 /* Span init */
484 span->interpMask = 0;
485 span->arrayMask = SPAN_XY | SPAN_Z;
486 if (ciMode)
487 span->arrayMask |= SPAN_INDEX;
488 else
489 span->arrayMask |= SPAN_RGBA;
490 /*span->arrayMask |= SPAN_LAMBDA;*/
491 span->arrayAttribs = swrast->_ActiveAttribMask; /* we'll produce these vals */
492
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;
497
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) {
502 if (span->end > 0) {
503 if (ciMode)
504 _swrast_write_index_span(ctx, span);
505 else
506 _swrast_write_rgba_span(ctx, span);
507 span->end = 0;
508 }
509 }
510
511 count = span->end;
512
513 span->facing = swrast->PointLineFacing;
514
515 /* fragment attributes */
516 if (ciMode) {
517 span->array->index[count] = (GLuint) vert->attrib[FRAG_ATTRIB_CI][0];
518 }
519 else {
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];
524 }
525 ATTRIB_LOOP_BEGIN
526 COPY_4V(span->array->attribs[attr][count], vert->attrib[attr]);
527 ATTRIB_LOOP_END
528
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);
533
534 span->end = count + 1;
535 ASSERT(span->end <= MAX_WIDTH);
536 }
537
538
539 /**
540 * Add specular color to primary color, draw point, restore original
541 * primary color.
542 */
543 void
544 _swrast_add_spec_terms_point(GLcontext *ctx, const SWvertex *v0)
545 {
546 SWvertex *ncv0 = (SWvertex *) v0; /* cast away const */
547 GLfloat rSum, gSum, bSum;
548 GLchan cSave[4];
549
550 /* save */
551 COPY_CHAN4(cSave, ncv0->color);
552 /* sum */
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);
559 /* draw */
560 SWRAST_CONTEXT(ctx)->SpecPoint(ctx, ncv0);
561 /* restore */
562 COPY_CHAN4(ncv0->color, cSave);
563 }
564
565
566 /**
567 * Examine current state to determine which point drawing function to use.
568 */
569 void
570 _swrast_choose_point(GLcontext *ctx)
571 {
572 SWcontext *swrast = SWRAST_CONTEXT(ctx);
573
574 if (ctx->RenderMode == GL_RENDER) {
575 if (ctx->Point.PointSprite) {
576 swrast->Point = sprite_point;
577 }
578 else if (ctx->Point.SmoothFlag) {
579 swrast->Point = smooth_point;
580 }
581 else if (ctx->Point.Size > 1.0 ||
582 ctx->Point._Attenuated ||
583 ctx->VertexProgram.PointSizeEnabled) {
584 swrast->Point = large_point;
585 }
586 else {
587 swrast->Point = pixel_point;
588 }
589 }
590 else if (ctx->RenderMode == GL_FEEDBACK) {
591 swrast->Point = _swrast_feedback_point;
592 }
593 else {
594 /* GL_SELECT mode */
595 swrast->Point = _swrast_select_point;
596 }
597 }