changes towards GL_ARB_point_sprite (two-zero)
[mesa.git] / src / mesa / swrast / s_pointtemp.h
1 /*
2 * Mesa 3-D graphics library
3 * Version: 6.1
4 *
5 * Copyright (C) 1999-2004 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 struct sw_span *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;
123 for (u = 0; u < ctx->Const.MaxTextureUnits; u++) {
124 if (ctx->Texture.Unit[u]._ReallyEnabled) {
125 const GLfloat q = vert->texcoord[u][3];
126 const GLfloat invQ = (q == 0.0F || q == 1.0F) ? 1.0F : (1.0F / q);
127 texcoord[u][0] = vert->texcoord[u][0] * invQ;
128 texcoord[u][1] = vert->texcoord[u][1] * invQ;
129 texcoord[u][2] = vert->texcoord[u][2] * invQ;
130 texcoord[u][3] = q;
131 }
132 }
133 #endif
134 #if FLAGS & SMOOTH
135 span->arrayMask |= SPAN_COVERAGE;
136 #endif
137 #if FLAGS & SPRITE
138 span->arrayMask |= SPAN_TEXTURE;
139 #endif
140
141 #if FLAGS & ATTENUATE
142 if (vert->pointSize >= ctx->Point.Threshold) {
143 size = MIN2(vert->pointSize, ctx->Point.MaxSize);
144 #if (FLAGS & RGBA) && (FLAGS & SMOOTH)
145 alphaAtten = 1.0F;
146 #endif
147 }
148 else {
149 #if (FLAGS & RGBA) && (FLAGS & SMOOTH)
150 GLfloat dsize = vert->pointSize / ctx->Point.Threshold;
151 alphaAtten = dsize * dsize;
152 #endif
153 size = MAX2(ctx->Point.Threshold, ctx->Point.MinSize);
154 }
155 #elif FLAGS & (LARGE | SMOOTH | SPRITE)
156 size = ctx->Point._Size;
157 #endif
158
159 #if FLAGS & (ATTENUATE | LARGE | SMOOTH | SPRITE)
160 /*
161 * Multi-pixel points
162 */
163 {{
164 GLint x, y;
165 const GLfloat radius = 0.5F * size;
166 const GLint z = (GLint) (vert->win[2] + 0.5F);
167 GLuint count;
168 #if FLAGS & SMOOTH
169 const GLfloat rmin = radius - 0.7071F; /* 0.7071 = sqrt(2)/2 */
170 const GLfloat rmax = radius + 0.7071F;
171 const GLfloat rmin2 = MAX2(0.0F, rmin * rmin);
172 const GLfloat rmax2 = rmax * rmax;
173 const GLfloat cscale = 1.0F / (rmax2 - rmin2);
174 const GLint xmin = (GLint) (vert->win[0] - radius);
175 const GLint xmax = (GLint) (vert->win[0] + radius);
176 const GLint ymin = (GLint) (vert->win[1] - radius);
177 const GLint ymax = (GLint) (vert->win[1] + radius);
178 #else
179 /* non-smooth */
180 GLint xmin, xmax, ymin, ymax;
181 GLint iSize = (GLint) (size + 0.5F);
182 GLint iRadius;
183 iSize = MAX2(1, iSize);
184 iRadius = iSize / 2;
185 if (iSize & 1) {
186 /* odd size */
187 xmin = (GLint) (vert->win[0] - iRadius);
188 xmax = (GLint) (vert->win[0] + iRadius);
189 ymin = (GLint) (vert->win[1] - iRadius);
190 ymax = (GLint) (vert->win[1] + iRadius);
191 }
192 else {
193 /* even size */
194 xmin = (GLint) vert->win[0] - iRadius + 1;
195 xmax = xmin + iSize - 1;
196 ymin = (GLint) vert->win[1] - iRadius + 1;
197 ymax = ymin + iSize - 1;
198 }
199 #endif /*SMOOTH*/
200
201 /* check if we need to flush */
202 if (span->end + (xmax-xmin+1) * (ymax-ymin+1) >= MAX_WIDTH ||
203 (swrast->_RasterMask & (BLEND_BIT | LOGIC_OP_BIT | MASKING_BIT))) {
204 #if FLAGS & (TEXTURE | SPRITE)
205 if (ctx->Texture._EnabledUnits)
206 _swrast_write_texture_span(ctx, span);
207 else
208 _swrast_write_rgba_span(ctx, span);
209 #elif FLAGS & RGBA
210 _swrast_write_rgba_span(ctx, span);
211 #else
212 _swrast_write_index_span(ctx, span);
213 #endif
214 span->end = 0;
215 }
216
217 /*
218 * OK, generate fragments
219 */
220 count = span->end;
221 (void) radius;
222 for (y = ymin; y <= ymax; y++) {
223 for (x = xmin; x <= xmax; x++) {
224 #if FLAGS & (SPRITE | TEXTURE)
225 GLuint u;
226 #endif
227
228 #if FLAGS & RGBA
229 span->array->rgba[count][RCOMP] = red;
230 span->array->rgba[count][GCOMP] = green;
231 span->array->rgba[count][BCOMP] = blue;
232 span->array->rgba[count][ACOMP] = alpha;
233 #endif
234 #if FLAGS & SPECULAR
235 span->array->spec[count][RCOMP] = specRed;
236 span->array->spec[count][GCOMP] = specGreen;
237 span->array->spec[count][BCOMP] = specBlue;
238 #endif
239 #if FLAGS & INDEX
240 span->array->index[count] = colorIndex;
241 #endif
242 #if FLAGS & TEXTURE
243 for (u = 0; u < ctx->Const.MaxTextureUnits; u++) {
244 if (ctx->Texture.Unit[u]._ReallyEnabled) {
245 COPY_4V(span->array->texcoords[u][count], texcoord[u]);
246 }
247 }
248 #endif
249
250 #if FLAGS & SMOOTH
251 /* compute coverage */
252 {
253 const GLfloat dx = x - vert->win[0] + 0.5F;
254 const GLfloat dy = y - vert->win[1] + 0.5F;
255 const GLfloat dist2 = dx * dx + dy * dy;
256 if (dist2 < rmax2) {
257 if (dist2 >= rmin2) {
258 /* compute partial coverage */
259 span->array->coverage[count] = 1.0F - (dist2 - rmin2) * cscale;
260 #if FLAGS & INDEX
261 /* coverage in [0,15] */
262 span->array->coverage[count] *= 15.0;
263 #endif
264 }
265 else {
266 /* full coverage */
267 span->array->coverage[count] = 1.0F;
268 }
269
270 span->array->x[count] = x;
271 span->array->y[count] = y;
272 span->array->z[count] = z;
273
274 #if (FLAGS & ATTENUATE) && (FLAGS & RGBA)
275 span->array->rgba[count][ACOMP] = (GLchan) (alpha * alphaAtten);
276 #elif FLAGS & RGBA
277 span->array->rgba[count][ACOMP] = alpha;
278 #endif /*ATTENUATE*/
279 count++;
280 } /*if*/
281 }
282
283 #else /*SMOOTH*/
284
285 /* not smooth (square points) */
286 span->array->x[count] = x;
287 span->array->y[count] = y;
288 span->array->z[count] = z;
289
290 #if FLAGS & SPRITE
291 for (u = 0; u < ctx->Const.MaxTextureUnits; u++) {
292 if (ctx->Texture.Unit[u]._ReallyEnabled) {
293 if (ctx->Point.CoordReplace[u]) {
294 GLfloat s = 0.5F + (x + 0.5F - vert->win[0]) / size;
295 GLfloat t;
296 #if GL_VERSION_2_0
297 if (ctx->Point.SpriteOrigin == GL_LOWER_LEFT)
298 t = 0.5F + (y + 0.5F - vert->win[1]) / size;
299 else /* GL_UPPER_LEFT */
300 #endif
301 t = 0.5F - (y + 0.5F - vert->win[1]) / size;
302 span->array->texcoords[u][count][0] = s;
303 span->array->texcoords[u][count][1] = t;
304 span->array->texcoords[u][count][3] = 1.0F;
305 if (ctx->Point.SpriteRMode == GL_ZERO)
306 span->array->texcoords[u][count][2] = 0.0F;
307 else if (ctx->Point.SpriteRMode == GL_S)
308 span->array->texcoords[u][count][2] = vert->texcoord[u][0];
309 else /* GL_R */
310 span->array->texcoords[u][count][2] = vert->texcoord[u][2];
311 }
312 else {
313 COPY_4V(span->array->texcoords[u][count], vert->texcoord[u]);
314 }
315 }
316 }
317 #endif /*SPRITE*/
318
319 count++; /* square point */
320
321 #endif /*SMOOTH*/
322
323 } /*for x*/
324 } /*for y*/
325 span->end = count;
326 }}
327
328 #else /* LARGE | ATTENUATE | SMOOTH | SPRITE */
329
330 /*
331 * Single-pixel points
332 */
333 {{
334 GLuint count;
335
336 /* check if we need to flush */
337 if (span->end >= MAX_WIDTH ||
338 (swrast->_RasterMask & (BLEND_BIT | LOGIC_OP_BIT | MASKING_BIT))) {
339 #if FLAGS & (TEXTURE | SPRITE)
340 if (ctx->Texture._EnabledUnits)
341 _swrast_write_texture_span(ctx, span);
342 else
343 _swrast_write_rgba_span(ctx, span);
344 #elif FLAGS & RGBA
345 _swrast_write_rgba_span(ctx, span);
346 #else
347 _swrast_write_index_span(ctx, span);
348 #endif
349 span->end = 0;
350 }
351
352 count = span->end;
353
354 #if FLAGS & RGBA
355 span->array->rgba[count][RCOMP] = red;
356 span->array->rgba[count][GCOMP] = green;
357 span->array->rgba[count][BCOMP] = blue;
358 span->array->rgba[count][ACOMP] = alpha;
359 #endif
360 #if FLAGS & SPECULAR
361 span->array->spec[count][RCOMP] = specRed;
362 span->array->spec[count][GCOMP] = specGreen;
363 span->array->spec[count][BCOMP] = specBlue;
364 #endif
365 #if FLAGS & INDEX
366 span->array->index[count] = colorIndex;
367 #endif
368 #if FLAGS & TEXTURE
369 for (u = 0; u < ctx->Const.MaxTextureUnits; u++) {
370 if (ctx->Texture.Unit[u]._ReallyEnabled) {
371 COPY_4V(span->array->texcoords[u][count], texcoord[u]);
372 }
373 }
374 #endif
375
376 span->array->x[count] = (GLint) vert->win[0];
377 span->array->y[count] = (GLint) vert->win[1];
378 span->array->z[count] = (GLint) (vert->win[2] + 0.5F);
379 span->end = count + 1;
380 }}
381
382 #endif /* LARGE || ATTENUATE || SMOOTH */
383
384 ASSERT(span->end <= MAX_WIDTH);
385 }
386
387
388 #undef FLAGS
389 #undef NAME