New SWspanarrays attribs[] array.
[mesa.git] / src / mesa / swrast / s_pointtemp.h
1 /*
2 * Mesa 3-D graphics library
3 * Version: 6.5.2
4 *
5 * Copyright (C) 1999-2006 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 * Regarding GL_NV_point_sprite:
27 *
28 * Portions of this software may use or implement intellectual
29 * property owned and licensed by NVIDIA Corporation. NVIDIA disclaims
30 * any and all warranties with respect to such intellectual property,
31 * including any use thereof or modifications thereto.
32 */
33
34
35 /*
36 * Point rendering template code.
37 *
38 * Set FLAGS = bitwise-OR of the following tokens:
39 *
40 * RGBA = do rgba instead of color index
41 * SMOOTH = do antialiasing
42 * TEXTURE = do texture coords
43 * SPECULAR = do separate specular color
44 * LARGE = do points with diameter > 1 pixel
45 * ATTENUATE = compute point size attenuation
46 * SPRITE = GL_ARB_point_sprite / GL_NV_point_sprite
47 *
48 * Notes: LARGE and ATTENUATE are exclusive of each other.
49 * TEXTURE requires RGBA
50 */
51
52
53 /*
54 * NOTES on antialiased point rasterization:
55 *
56 * Let d = distance of fragment center from vertex.
57 * if d < rmin2 then
58 * fragment has 100% coverage
59 * else if d > rmax2 then
60 * fragment has 0% coverage
61 * else
62 * fragment has % coverage = (d - rmin2) / (rmax2 - rmin2)
63 */
64
65
66
67 static void
68 NAME ( GLcontext *ctx, const SWvertex *vert )
69 {
70 #if FLAGS & (ATTENUATE | LARGE | SMOOTH | SPRITE)
71 GLfloat size;
72 #endif
73 #if FLAGS & RGBA
74 #if (FLAGS & ATTENUATE) && (FLAGS & SMOOTH)
75 GLfloat alphaAtten;
76 #endif
77 const GLchan red = vert->color[0];
78 const GLchan green = vert->color[1];
79 const GLchan blue = vert->color[2];
80 const GLchan alpha = vert->color[3];
81 #endif
82 #if FLAGS & SPECULAR
83 const GLchan specRed = vert->specular[0];
84 const GLchan specGreen = vert->specular[1];
85 const GLchan specBlue = vert->specular[2];
86 #endif
87 #if FLAGS & INDEX
88 const GLuint colorIndex = (GLuint) vert->index; /* XXX round? */
89 #endif
90 #if FLAGS & TEXTURE
91 GLfloat texcoord[MAX_TEXTURE_COORD_UNITS][4];
92 GLuint u;
93 #endif
94 SWcontext *swrast = SWRAST_CONTEXT(ctx);
95 SWspan *span = &(swrast->PointSpan);
96
97 /* Cull primitives with malformed coordinates.
98 */
99 {
100 float tmp = vert->win[0] + vert->win[1];
101 if (IS_INF_OR_NAN(tmp))
102 return;
103 }
104
105 /*
106 * Span init
107 */
108 span->interpMask = SPAN_FOG;
109 span->arrayMask = SPAN_XY | SPAN_Z;
110 span->fog = vert->fog;
111 span->fogStep = 0.0;
112 #if FLAGS & RGBA
113 span->arrayMask |= SPAN_RGBA;
114 #endif
115 #if FLAGS & SPECULAR
116 span->arrayMask |= SPAN_SPEC;
117 #endif
118 #if FLAGS & INDEX
119 span->arrayMask |= SPAN_INDEX;
120 #endif
121 #if FLAGS & TEXTURE
122 span->arrayMask |= (SPAN_TEXTURE | SPAN_LAMBDA);
123 if (ctx->FragmentProgram._Current) {
124 /* Don't divide texture s,t,r by q (use TXP to do that) */
125 for (u = 0; u < ctx->Const.MaxTextureUnits; u++) {
126 if (ctx->Texture._EnabledCoordUnits & (1 << u)) {
127 COPY_4V(texcoord[u], vert->texcoord[u]);
128 }
129 }
130 }
131 else {
132 /* Divide texture s,t,r by q here */
133 for (u = 0; u < ctx->Const.MaxTextureUnits; u++) {
134 if (ctx->Texture._EnabledCoordUnits & (1 << u)) {
135 const GLfloat q = vert->texcoord[u][3];
136 const GLfloat invQ = (q == 0.0F || q == 1.0F) ? 1.0F : (1.0F / q);
137 texcoord[u][0] = vert->texcoord[u][0] * invQ;
138 texcoord[u][1] = vert->texcoord[u][1] * invQ;
139 texcoord[u][2] = vert->texcoord[u][2] * invQ;
140 texcoord[u][3] = q;
141 }
142 }
143 }
144 /* need these for fragment programs */
145 span->w = 1.0F;
146 span->dwdx = 0.0F;
147 span->dwdy = 0.0F;
148 #endif
149 #if FLAGS & SMOOTH
150 span->arrayMask |= SPAN_COVERAGE;
151 #endif
152 #if FLAGS & SPRITE
153 span->arrayMask |= (SPAN_TEXTURE | SPAN_LAMBDA);
154 #endif
155
156 /* Compute point size if not known to be one */
157 #if FLAGS & ATTENUATE
158 /* first, clamp attenuated size to the user-specifed range */
159 size = CLAMP(vert->pointSize, ctx->Point.MinSize, ctx->Point.MaxSize);
160 #if (FLAGS & RGBA) && (FLAGS & SMOOTH)
161 /* only if multisampling, compute the fade factor */
162 if (ctx->Multisample.Enabled) {
163 if (vert->pointSize >= ctx->Point.Threshold) {
164 alphaAtten = 1.0F;
165 }
166 else {
167 GLfloat dsize = vert->pointSize / ctx->Point.Threshold;
168 alphaAtten = dsize * dsize;
169 }
170 }
171 else {
172 alphaAtten = 1.0;
173 }
174 #endif
175 #elif FLAGS & (LARGE | SMOOTH | SPRITE)
176 /* constant, non-attenuated size */
177 size = ctx->Point._Size; /* this is already clamped */
178 #endif
179
180
181 #if FLAGS & (ATTENUATE | LARGE | SMOOTH | SPRITE)
182 /***
183 *** Multi-pixel points
184 ***/
185
186 /* do final clamping now */
187 if (ctx->Point.SmoothFlag) {
188 size = CLAMP(size, ctx->Const.MinPointSizeAA, ctx->Const.MaxPointSizeAA);
189 }
190 else {
191 size = CLAMP(size, ctx->Const.MinPointSize, ctx->Const.MaxPointSize);
192 }
193
194 {{
195 GLint x, y;
196 const GLfloat radius = 0.5F * size;
197 const GLint z = (GLint) (vert->win[2] + 0.5F);
198 GLuint count;
199 #if FLAGS & SMOOTH
200 const GLfloat rmin = radius - 0.7071F; /* 0.7071 = sqrt(2)/2 */
201 const GLfloat rmax = radius + 0.7071F;
202 const GLfloat rmin2 = MAX2(0.0F, rmin * rmin);
203 const GLfloat rmax2 = rmax * rmax;
204 const GLfloat cscale = 1.0F / (rmax2 - rmin2);
205 const GLint xmin = (GLint) (vert->win[0] - radius);
206 const GLint xmax = (GLint) (vert->win[0] + radius);
207 const GLint ymin = (GLint) (vert->win[1] - radius);
208 const GLint ymax = (GLint) (vert->win[1] + radius);
209 #else
210 /* non-smooth */
211 GLint xmin, xmax, ymin, ymax;
212 GLint iSize = (GLint) (size + 0.5F);
213 GLint iRadius;
214 iSize = MAX2(1, iSize);
215 iRadius = iSize / 2;
216 if (iSize & 1) {
217 /* odd size */
218 xmin = (GLint) (vert->win[0] - iRadius);
219 xmax = (GLint) (vert->win[0] + iRadius);
220 ymin = (GLint) (vert->win[1] - iRadius);
221 ymax = (GLint) (vert->win[1] + iRadius);
222 }
223 else {
224 /* even size */
225 xmin = (GLint) vert->win[0] - iRadius + 1;
226 xmax = xmin + iSize - 1;
227 ymin = (GLint) vert->win[1] - iRadius + 1;
228 ymax = ymin + iSize - 1;
229 }
230 #endif /*SMOOTH*/
231
232 /* check if we need to flush */
233 if (span->end + (xmax-xmin+1) * (ymax-ymin+1) >= MAX_WIDTH ||
234 (swrast->_RasterMask & (BLEND_BIT | LOGIC_OP_BIT | MASKING_BIT))) {
235 if (span->end > 0) {
236 #if FLAGS & RGBA
237 _swrast_write_rgba_span(ctx, span);
238 #else
239 _swrast_write_index_span(ctx, span);
240 #endif
241 span->end = 0;
242 }
243 }
244
245 /*
246 * OK, generate fragments
247 */
248 count = span->end;
249 (void) radius;
250 for (y = ymin; y <= ymax; y++) {
251 /* check if we need to flush */
252 if (count + (xmax-xmin+1) >= MAX_WIDTH) {
253 span->end = count;
254 #if FLAGS & RGBA
255 _swrast_write_rgba_span(ctx, span);
256 #else
257 _swrast_write_index_span(ctx, span);
258 #endif
259 count = span->end = 0;
260 }
261 for (x = xmin; x <= xmax; x++) {
262 #if FLAGS & (SPRITE | TEXTURE)
263 GLuint u;
264 #endif
265
266 #if FLAGS & RGBA
267 span->array->rgba[count][RCOMP] = red;
268 span->array->rgba[count][GCOMP] = green;
269 span->array->rgba[count][BCOMP] = blue;
270 span->array->rgba[count][ACOMP] = alpha;
271 #endif
272 #if FLAGS & SPECULAR
273 span->array->spec[count][RCOMP] = specRed;
274 span->array->spec[count][GCOMP] = specGreen;
275 span->array->spec[count][BCOMP] = specBlue;
276 #endif
277 #if FLAGS & INDEX
278 span->array->index[count] = colorIndex;
279 #endif
280 #if FLAGS & TEXTURE
281 for (u = 0; u < ctx->Const.MaxTextureUnits; u++) {
282 if (ctx->Texture._EnabledCoordUnits & (1 << u)) {
283 COPY_4V(span->array->attribs[FRAG_ATTRIB_TEX0 + u][count], texcoord[u]);
284 span->array->lambda[u][count] = 0.0;
285 }
286 }
287 #endif
288
289 #if FLAGS & SMOOTH
290 /* compute coverage */
291 {
292 const GLfloat dx = x - vert->win[0] + 0.5F;
293 const GLfloat dy = y - vert->win[1] + 0.5F;
294 const GLfloat dist2 = dx * dx + dy * dy;
295 if (dist2 < rmax2) {
296 if (dist2 >= rmin2) {
297 /* compute partial coverage */
298 span->array->coverage[count] = 1.0F - (dist2 - rmin2) * cscale;
299 #if FLAGS & INDEX
300 /* coverage in [0,15] */
301 span->array->coverage[count] *= 15.0;
302 #endif
303 }
304 else {
305 /* full coverage */
306 span->array->coverage[count] = 1.0F;
307 }
308
309 span->array->x[count] = x;
310 span->array->y[count] = y;
311 span->array->z[count] = z;
312
313 #if (FLAGS & ATTENUATE) && (FLAGS & RGBA)
314 span->array->rgba[count][ACOMP] = (GLchan) (alpha * alphaAtten);
315 #elif FLAGS & RGBA
316 span->array->rgba[count][ACOMP] = alpha;
317 #endif /*ATTENUATE*/
318 count++;
319 } /*if*/
320 }
321
322 #else /*SMOOTH*/
323
324 /* not smooth (square points) */
325 span->array->x[count] = x;
326 span->array->y[count] = y;
327 span->array->z[count] = z;
328
329 #if FLAGS & SPRITE
330 for (u = 0; u < ctx->Const.MaxTextureUnits; u++) {
331 if (ctx->Texture.Unit[u]._ReallyEnabled) {
332 if (ctx->Point.CoordReplace[u]) {
333 GLfloat s = 0.5F + (x + 0.5F - vert->win[0]) / size;
334 GLfloat t, r;
335 if (ctx->Point.SpriteOrigin == GL_LOWER_LEFT)
336 t = 0.5F + (y + 0.5F - vert->win[1]) / size;
337 else /* GL_UPPER_LEFT */
338 t = 0.5F - (y + 0.5F - vert->win[1]) / size;
339 if (ctx->Point.SpriteRMode == GL_ZERO)
340 r = 0.0F;
341 else if (ctx->Point.SpriteRMode == GL_S)
342 r = vert->texcoord[u][0];
343 else /* GL_R */
344 r = vert->texcoord[u][2];
345 span->array->attribs[FRAG_ATTRIB_TEX0 + u][count][0] = s;
346 span->array->attribs[FRAG_ATTRIB_TEX0 + u][count][1] = t;
347 span->array->attribs[FRAG_ATTRIB_TEX0 + u][count][2] = r;
348 span->array->attribs[FRAG_ATTRIB_TEX0 + u][count][3] = 1.0F;
349 span->array->lambda[u][count] = 0.0; /* XXX fix? */
350 }
351 else {
352 COPY_4V(span->array->attribs[FRAG_ATTRIB_TEX0 + u][count], vert->texcoord[u]);
353 }
354 }
355 }
356 #endif /*SPRITE*/
357
358 count++; /* square point */
359
360 #endif /*SMOOTH*/
361
362 } /*for x*/
363 } /*for y*/
364 span->end = count;
365 }}
366
367 #else /* LARGE | ATTENUATE | SMOOTH | SPRITE */
368
369 /***
370 *** Single-pixel points
371 ***/
372 {{
373 GLuint count;
374
375 /* check if we need to flush */
376 if (span->end >= MAX_WIDTH ||
377 (swrast->_RasterMask & (BLEND_BIT | LOGIC_OP_BIT | MASKING_BIT))) {
378 #if FLAGS & RGBA
379 _swrast_write_rgba_span(ctx, span);
380 #else
381 _swrast_write_index_span(ctx, span);
382 #endif
383 span->end = 0;
384 }
385
386 count = span->end;
387
388 #if FLAGS & RGBA
389 span->array->rgba[count][RCOMP] = red;
390 span->array->rgba[count][GCOMP] = green;
391 span->array->rgba[count][BCOMP] = blue;
392 span->array->rgba[count][ACOMP] = alpha;
393 #endif
394 #if FLAGS & SPECULAR
395 span->array->spec[count][RCOMP] = specRed;
396 span->array->spec[count][GCOMP] = specGreen;
397 span->array->spec[count][BCOMP] = specBlue;
398 #endif
399 #if FLAGS & INDEX
400 span->array->index[count] = colorIndex;
401 #endif
402 #if FLAGS & TEXTURE
403 for (u = 0; u < ctx->Const.MaxTextureUnits; u++) {
404 if (ctx->Texture.Unit[u]._ReallyEnabled) {
405 COPY_4V(span->array->attribs[FRAG_ATTRIB_TEX0 + u][count], texcoord[u]);
406 }
407 }
408 #endif
409
410 span->array->x[count] = (GLint) vert->win[0];
411 span->array->y[count] = (GLint) vert->win[1];
412 span->array->z[count] = (GLint) (vert->win[2] + 0.5F);
413 span->end = count + 1;
414 }}
415
416 #endif /* LARGE || ATTENUATE || SMOOTH */
417
418 ASSERT(span->end <= MAX_WIDTH);
419 }
420
421
422 #undef FLAGS
423 #undef NAME