summaryrefslogtreecommitdiffstats
path: root/src/mesa/swrast/s_pointtemp.h
blob: 206085b5b871bdbeed0de7bc83934943cd30be2d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
/*
 * Mesa 3-D graphics library
 * Version:  6.5.3
 *
 * Copyright (C) 1999-2007  Brian Paul   All Rights Reserved.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included
 * in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

/*
 * Regarding GL_NV_point_sprite:
 *
 * Portions of this software may use or implement intellectual
 * property owned and licensed by NVIDIA Corporation. NVIDIA disclaims
 * any and all warranties with respect to such intellectual property,
 * including any use thereof or modifications thereto.
 */


/*
 * Point rendering template code.
 *
 * Set FLAGS = bitwise-OR of the following tokens:
 *
 *   RGBA = do rgba instead of color index
 *   SMOOTH = do antialiasing
 *   ATTRIBS = general attributes (texcoords, etc)
 *   LARGE = do points with diameter > 1 pixel
 *   ATTENUATE = compute point size attenuation
 *   SPRITE = GL_ARB_point_sprite / GL_NV_point_sprite
 *
 * Notes: LARGE and ATTENUATE are exclusive of each other.
 *        ATTRIBS requires RGBA
 */


/*
 * NOTES on antialiased point rasterization:
 *
 * Let d = distance of fragment center from vertex.
 * if d < rmin2 then
 *    fragment has 100% coverage
 * else if d > rmax2 then
 *    fragment has 0% coverage
 * else
 *    fragment has % coverage = (d - rmin2) / (rmax2 - rmin2)
 */


static void
NAME ( GLcontext *ctx, const SWvertex *vert )
{
#if FLAGS & (ATTENUATE | LARGE | SMOOTH | SPRITE)
   GLfloat size;
#endif
#if FLAGS & RGBA
#if (FLAGS & ATTENUATE) && (FLAGS & SMOOTH)
   GLfloat alphaAtten;
#endif
   const GLchan red   = vert->color[0];
   const GLchan green = vert->color[1];
   const GLchan blue  = vert->color[2];
   const GLchan alpha = vert->color[3];
#endif
#if FLAGS & INDEX
   const GLuint colorIndex = (GLuint) vert->attrib[FRAG_ATTRIB_CI][0]; /* XXX round? */
#endif
#if FLAGS & ATTRIBS
   GLfloat attrib[FRAG_ATTRIB_MAX][4]; /* texture & varying */
#endif
   SWcontext *swrast = SWRAST_CONTEXT(ctx);
   SWspan *span = &(swrast->PointSpan);

   /*
   printf("%s  %g %g %g %g\n", __FUNCTION__,
          vert->attrib[FRAG_ATTRIB_COL1][0],
          vert->attrib[FRAG_ATTRIB_COL1][1],
          vert->attrib[FRAG_ATTRIB_COL1][2],
          vert->attrib[FRAG_ATTRIB_COL1][3]);
   if ( vert->attrib[FRAG_ATTRIB_COL1][0] == 0.0 &&
        vert->attrib[FRAG_ATTRIB_COL1][1] == 1.0 &&
        vert->attrib[FRAG_ATTRIB_COL1][2] == 0.0)
      foo();
   */

   /* Cull primitives with malformed coordinates.
    */
   {
      float tmp = vert->attrib[FRAG_ATTRIB_WPOS][0] + vert->attrib[FRAG_ATTRIB_WPOS][1];
      if (IS_INF_OR_NAN(tmp))
	 return;
   }

   /*
    * Span init
    */
   span->interpMask = 0;
   span->arrayMask = SPAN_XY | SPAN_Z;
#if FLAGS & RGBA
   span->arrayMask |= SPAN_RGBA;
#endif
#if FLAGS & INDEX
   span->arrayMask |= SPAN_INDEX;
#endif
#if FLAGS & ATTRIBS
   span->arrayMask |= SPAN_LAMBDA;

   /* we're filling in the attrib arrays: */
   span->arrayAttribs = swrast->_ActiveAttribMask;

   ATTRIB_LOOP_BEGIN
      COPY_4V(attrib[attr], vert->attrib[attr]);
   ATTRIB_LOOP_END

   /* need these for fragment programs */
   span->attrStart[FRAG_ATTRIB_WPOS][3] = 1.0F;
   span->attrStepX[FRAG_ATTRIB_WPOS][3] = 0.0F;
   span->attrStepY[FRAG_ATTRIB_WPOS][3] = 0.0F;
#else
   assert((swrast->_ActiveAttribMask & FRAG_BIT_COL1) == 0);
#endif

#if FLAGS & SMOOTH
   span->arrayMask |= SPAN_COVERAGE;
#endif
#if FLAGS & SPRITE
   span->arrayMask |= SPAN_LAMBDA;
#endif

   /* Compute point size if not known to be one */
#if FLAGS & ATTENUATE
   /* first, clamp attenuated size to the user-specifed range */
   size = CLAMP(vert->pointSize, ctx->Point.MinSize, ctx->Point.MaxSize);
#if (FLAGS & RGBA) && (FLAGS & SMOOTH)
   /* only if multisampling, compute the fade factor */
   if (ctx->Multisample.Enabled) {
      if (vert->pointSize >= ctx->Point.Threshold) {
         alphaAtten = 1.0F;
      }
      else {
         GLfloat dsize = vert->pointSize / ctx->Point.Threshold;
         alphaAtten = dsize * dsize;
      }
   }
   else {
      alphaAtten = 1.0;
   }
#endif
#elif FLAGS & (LARGE | SMOOTH | SPRITE)
   /* constant, non-attenuated size */
   size = ctx->Point._Size; /* this is already clamped */
#endif


#if FLAGS & (ATTENUATE | LARGE | SMOOTH | SPRITE)
   /***
    *** Multi-pixel points
    ***/

   /* do final clamping now */
   if (ctx->Point.SmoothFlag) {
      size = CLAMP(size, ctx->Const.MinPointSizeAA, ctx->Const.MaxPointSizeAA);
   }
   else {
      size = CLAMP(size, ctx->Const.MinPointSize, ctx->Const.MaxPointSize);
   }

   {{
      GLint x, y;
      const GLfloat radius = 0.5F * size;
      const GLuint z = (GLuint) (vert->attrib[FRAG_ATTRIB_WPOS][2] + 0.5F);
      GLuint count;
#if FLAGS & SMOOTH
      const GLfloat rmin = radius - 0.7071F;  /* 0.7071 = sqrt(2)/2 */
      const GLfloat rmax = radius + 0.7071F;
      const GLfloat rmin2 = MAX2(0.0F, rmin * rmin);
      const GLfloat rmax2 = rmax * rmax;
      const GLfloat cscale = 1.0F / (rmax2 - rmin2);
      const GLint xmin = (GLint) (vert->attrib[FRAG_ATTRIB_WPOS][0] - radius);
      const GLint xmax = (GLint) (vert->attrib[FRAG_ATTRIB_WPOS][0] + radius);
      const GLint ymin = (GLint) (vert->attrib[FRAG_ATTRIB_WPOS][1] - radius);
      const GLint ymax = (GLint) (vert->attrib[FRAG_ATTRIB_WPOS][1] + radius);
#else
      /* non-smooth */
      GLint xmin, xmax, ymin, ymax;
      GLint iSize = (GLint) (size + 0.5F);
      GLint iRadius;
      iSize = MAX2(1, iSize);
      iRadius = iSize / 2;
      if (iSize & 1) {
         /* odd size */
         xmin = (GLint) (vert->attrib[FRAG_ATTRIB_WPOS][0] - iRadius);
         xmax = (GLint) (vert->attrib[FRAG_ATTRIB_WPOS][0] + iRadius);
         ymin = (GLint) (vert->attrib[FRAG_ATTRIB_WPOS][1] - iRadius);
         ymax = (GLint) (vert->attrib[FRAG_ATTRIB_WPOS][1] + iRadius);
      }
      else {
         /* even size */
         xmin = (GLint) vert->attrib[FRAG_ATTRIB_WPOS][0] - iRadius + 1;
         xmax = xmin + iSize - 1;
         ymin = (GLint) vert->attrib[FRAG_ATTRIB_WPOS][1] - iRadius + 1;
         ymax = ymin + iSize - 1;
      }
#endif /*SMOOTH*/

      /* check if we need to flush */
      if (span->end + (xmax-xmin+1) * (ymax-ymin+1) >= MAX_WIDTH ||
          (swrast->_RasterMask & (BLEND_BIT | LOGIC_OP_BIT | MASKING_BIT))) {
         if (span->end > 0) {
#if FLAGS & RGBA
            _swrast_write_rgba_span(ctx, span);
#else
            _swrast_write_index_span(ctx, span);
#endif
            span->end = 0;
         }
      }

      /*
       * OK, generate fragments
       */
      count = span->end;
      (void) radius;
      for (y = ymin; y <= ymax; y++) {
         /* check if we need to flush */
         if (count + (xmax-xmin+1) >= MAX_WIDTH) {
	     span->end = count;
#if FLAGS & RGBA
            _swrast_write_rgba_span(ctx, span);
#else
            _swrast_write_index_span(ctx, span);
#endif
            count = span->end = 0;
         }
         for (x = xmin; x <= xmax; x++) {
#if FLAGS & SPRITE
            GLuint u;
#endif

#if FLAGS & RGBA
            span->array->rgba[count][RCOMP] = red;
            span->array->rgba[count][GCOMP] = green;
            span->array->rgba[count][BCOMP] = blue;
            span->array->rgba[count][ACOMP] = alpha;
#endif
#if FLAGS & INDEX
            span->array->index[count] = colorIndex;
#endif
#if FLAGS & ATTRIBS
            ATTRIB_LOOP_BEGIN
               COPY_4V(span->array->attribs[attr][count], attrib[attr]);
            /**
               if (attr < FRAG_ATTRIB_VAR0) {
                  const GLuint u = attr - FRAG_ATTRIB_TEX0;
                  span->array->lambda[u][count] = 0.0;
               }
            **/
            ATTRIB_LOOP_END
#endif

#if FLAGS & SMOOTH
            /* compute coverage */
            {
               const GLfloat dx = x - vert->attrib[FRAG_ATTRIB_WPOS][0] + 0.5F;
               const GLfloat dy = y - vert->attrib[FRAG_ATTRIB_WPOS][1] + 0.5F;
               const GLfloat dist2 = dx * dx + dy * dy;
               if (dist2 < rmax2) {
                  if (dist2 >= rmin2) {
                     /* compute partial coverage */
                     span->array->coverage[count] = 1.0F - (dist2 - rmin2) * cscale;
#if FLAGS & INDEX
                     /* coverage in [0,15] */
                     span->array->coverage[count] *= 15.0;
#endif
                  }
                  else {
                     /* full coverage */
                     span->array->coverage[count] = 1.0F;
                  }

                  span->array->x[count] = x;
                  span->array->y[count] = y;
                  span->array->z[count] = z;

#if (FLAGS & ATTENUATE) && (FLAGS & RGBA)
                  span->array->rgba[count][ACOMP] = (GLchan) (alpha * alphaAtten);
#elif FLAGS & RGBA
                  span->array->rgba[count][ACOMP] = alpha;
#endif /*ATTENUATE*/
                  count++;
               } /*if*/
            }

#else /*SMOOTH*/

            /* not smooth (square points) */
            span->array->x[count] = x;
            span->array->y[count] = y;
            span->array->z[count] = z;

#if FLAGS & SPRITE
            for (u = 0; u < ctx->Const.MaxTextureUnits; u++) {
               GLuint attr = FRAG_ATTRIB_TEX0 + u;
               if (ctx->Texture.Unit[u]._ReallyEnabled) {
                  if (ctx->Point.CoordReplace[u]) {
                     GLfloat s = 0.5F + (x + 0.5F - vert->attrib[FRAG_ATTRIB_WPOS][0]) / size;
                     GLfloat t, r;
                     if (ctx->Point.SpriteOrigin == GL_LOWER_LEFT)
                        t = 0.5F + (y + 0.5F - vert->attrib[FRAG_ATTRIB_WPOS][1]) / size;
                     else /* GL_UPPER_LEFT */
                        t = 0.5F - (y + 0.5F - vert->attrib[FRAG_ATTRIB_WPOS][1]) / size;
                     if (ctx->Point.SpriteRMode == GL_ZERO)
                        r = 0.0F;
                     else if (ctx->Point.SpriteRMode == GL_S)
                        r = vert->attrib[attr][0];
                     else /* GL_R */
                        r = vert->attrib[attr][2];
                     span->array->attribs[attr][count][0] = s;
                     span->array->attribs[attr][count][1] = t;
                     span->array->attribs[attr][count][2] = r;
                     span->array->attribs[attr][count][3] = 1.0F;
                     span->array->lambda[u][count] = 0.0; /* XXX fix? */
                  }
                  else {
                     COPY_4V(span->array->attribs[attr][count],
                             vert->attrib[attr]);
                  }
               }
            }
#endif /*SPRITE*/

            count++;  /* square point */

#endif /*SMOOTH*/

	 } /*for x*/
      } /*for y*/
      span->end = count;
   }}

#else /* LARGE | ATTENUATE | SMOOTH | SPRITE */

   /***
    *** Single-pixel points
    ***/
   {{
      GLuint count;

      /* check if we need to flush */
      if (span->end >= MAX_WIDTH ||
          (swrast->_RasterMask & (BLEND_BIT | LOGIC_OP_BIT | MASKING_BIT))) {
#if FLAGS & RGBA
         _swrast_write_rgba_span(ctx, span);
#else
         _swrast_write_index_span(ctx, span);
#endif
         span->end = 0;
      }

      count = span->end;

#if FLAGS & RGBA
      span->array->rgba[count][RCOMP] = red;
      span->array->rgba[count][GCOMP] = green;
      span->array->rgba[count][BCOMP] = blue;
      span->array->rgba[count][ACOMP] = alpha;
#endif
#if FLAGS & INDEX
      span->array->index[count] = colorIndex;
#endif
#if FLAGS & ATTRIBS
      ATTRIB_LOOP_BEGIN
         COPY_4V(span->array->attribs[attr][count], attribs[attr]);
      ATTRIB_LOOP_END
#endif

      span->array->x[count] = (GLint) vert->attrib[FRAG_ATTRIB_WPOS][0];
      span->array->y[count] = (GLint) vert->attrib[FRAG_ATTRIB_WPOS][1];
      span->array->z[count] = (GLint) (vert->attrib[FRAG_ATTRIB_WPOS][2] + 0.5F);

      span->end = count + 1;
   }}

#endif /* LARGE || ATTENUATE || SMOOTH */

   ASSERT(span->end <= MAX_WIDTH);
}


#undef FLAGS
#undef NAME