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
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
|
/*
* Mesa 3-D graphics library
* Version: 7.1
*
* 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
* THE AUTHORS OR COPYRIGHT HOLDERS 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.
*/
#include "main/glheader.h"
#include "main/colormac.h"
#include "main/macros.h"
#include "s_context.h"
#include "s_feedback.h"
#include "s_points.h"
#include "s_span.h"
/**
* Used to cull points with invalid coords
*/
#define CULL_INVALID(V) \
do { \
float tmp = (V)->attrib[VARYING_SLOT_POS][0] \
+ (V)->attrib[VARYING_SLOT_POS][1]; \
if (IS_INF_OR_NAN(tmp)) \
return; \
} while(0)
/**
* Get/compute the point size.
* The size may come from a vertex shader, or computed with attentuation
* or just the glPointSize value.
* Must also clamp to user-defined range and implmentation limits.
*/
static inline GLfloat
get_size(const struct gl_context *ctx, const SWvertex *vert, GLboolean smoothed)
{
GLfloat size;
if (ctx->Point._Attenuated || ctx->VertexProgram.PointSizeEnabled) {
/* use vertex's point size */
size = vert->pointSize;
}
else {
/* use constant point size */
size = ctx->Point.Size;
}
/* always clamp to user-specified limits */
size = CLAMP(size, ctx->Point.MinSize, ctx->Point.MaxSize);
/* clamp to implementation limits */
if (smoothed)
size = CLAMP(size, ctx->Const.MinPointSizeAA, ctx->Const.MaxPointSizeAA);
else
size = CLAMP(size, ctx->Const.MinPointSize, ctx->Const.MaxPointSize);
return size;
}
/**
* Draw a point sprite
*/
static void
sprite_point(struct gl_context *ctx, const SWvertex *vert)
{
SWcontext *swrast = SWRAST_CONTEXT(ctx);
SWspan span;
GLfloat size;
GLuint tCoords[MAX_TEXTURE_COORD_UNITS + 1];
GLuint numTcoords = 0;
GLfloat t0, dtdy;
CULL_INVALID(vert);
/* z coord */
if (ctx->DrawBuffer->Visual.depthBits <= 16)
span.z = FloatToFixed(vert->attrib[VARYING_SLOT_POS][2] + 0.5F);
else
span.z = (GLuint) (vert->attrib[VARYING_SLOT_POS][2] + 0.5F);
span.zStep = 0;
size = get_size(ctx, vert, GL_FALSE);
/* span init */
INIT_SPAN(span, GL_POINT);
span.interpMask = SPAN_Z | SPAN_RGBA;
span.facing = swrast->PointLineFacing;
span.red = ChanToFixed(vert->color[0]);
span.green = ChanToFixed(vert->color[1]);
span.blue = ChanToFixed(vert->color[2]);
span.alpha = ChanToFixed(vert->color[3]);
span.redStep = 0;
span.greenStep = 0;
span.blueStep = 0;
span.alphaStep = 0;
/* need these for fragment programs */
span.attrStart[VARYING_SLOT_POS][3] = 1.0F;
span.attrStepX[VARYING_SLOT_POS][3] = 0.0F;
span.attrStepY[VARYING_SLOT_POS][3] = 0.0F;
{
GLfloat s, r, dsdx;
/* texcoord / pointcoord interpolants */
s = 0.0F;
dsdx = 1.0F / size;
if (ctx->Point.SpriteOrigin == GL_LOWER_LEFT) {
dtdy = 1.0F / size;
t0 = 0.5F * dtdy;
}
else {
/* GL_UPPER_LEFT */
dtdy = -1.0F / size;
t0 = 1.0F + 0.5F * dtdy;
}
ATTRIB_LOOP_BEGIN
if (attr >= VARYING_SLOT_TEX0 && attr <= VARYING_SLOT_TEX7) {
/* a texcoord attribute */
const GLuint u = attr - VARYING_SLOT_TEX0;
ASSERT(u < Elements(ctx->Point.CoordReplace));
if (ctx->Point.CoordReplace[u]) {
tCoords[numTcoords++] = attr;
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.attrStart[attr][0] = s;
span.attrStart[attr][1] = 0.0; /* overwritten below */
span.attrStart[attr][2] = r;
span.attrStart[attr][3] = 1.0;
span.attrStepX[attr][0] = dsdx;
span.attrStepX[attr][1] = 0.0;
span.attrStepX[attr][2] = 0.0;
span.attrStepX[attr][3] = 0.0;
span.attrStepY[attr][0] = 0.0;
span.attrStepY[attr][1] = dtdy;
span.attrStepY[attr][2] = 0.0;
span.attrStepY[attr][3] = 0.0;
continue;
}
}
else if (attr == VARYING_SLOT_PNTC) {
/* GLSL gl_PointCoord.xy (.zw undefined) */
span.attrStart[VARYING_SLOT_PNTC][0] = 0.0;
span.attrStart[VARYING_SLOT_PNTC][1] = 0.0; /* t0 set below */
span.attrStepX[VARYING_SLOT_PNTC][0] = dsdx;
span.attrStepX[VARYING_SLOT_PNTC][1] = 0.0;
span.attrStepY[VARYING_SLOT_PNTC][0] = 0.0;
span.attrStepY[VARYING_SLOT_PNTC][1] = dtdy;
tCoords[numTcoords++] = VARYING_SLOT_PNTC;
continue;
}
/* use vertex's texcoord/attrib */
COPY_4V(span.attrStart[attr], vert->attrib[attr]);
ASSIGN_4V(span.attrStepX[attr], 0, 0, 0, 0);
ASSIGN_4V(span.attrStepY[attr], 0, 0, 0, 0);
ATTRIB_LOOP_END;
}
/* compute pos, bounds and render */
{
const GLfloat x = vert->attrib[VARYING_SLOT_POS][0];
const GLfloat y = vert->attrib[VARYING_SLOT_POS][1];
GLint iSize = (GLint) (size + 0.5F);
GLint xmin, xmax, ymin, ymax, iy;
GLint iRadius;
GLfloat tcoord = t0;
iSize = MAX2(1, iSize);
iRadius = iSize / 2;
if (iSize & 1) {
/* odd size */
xmin = (GLint) (x - iRadius);
xmax = (GLint) (x + iRadius);
ymin = (GLint) (y - iRadius);
ymax = (GLint) (y + iRadius);
}
else {
/* even size */
/* 0.501 factor allows conformance to pass */
xmin = (GLint) (x + 0.501) - iRadius;
xmax = xmin + iSize - 1;
ymin = (GLint) (y + 0.501) - iRadius;
ymax = ymin + iSize - 1;
}
/* render spans */
for (iy = ymin; iy <= ymax; iy++) {
GLuint i;
/* setup texcoord T for this row */
for (i = 0; i < numTcoords; i++) {
span.attrStart[tCoords[i]][1] = tcoord;
}
/* these might get changed by span clipping */
span.x = xmin;
span.y = iy;
span.end = xmax - xmin + 1;
_swrast_write_rgba_span(ctx, &span);
tcoord += dtdy;
}
}
}
/**
* Draw smooth/antialiased point. RGB or CI mode.
*/
static void
smooth_point(struct gl_context *ctx, const SWvertex *vert)
{
SWcontext *swrast = SWRAST_CONTEXT(ctx);
SWspan span;
GLfloat size, alphaAtten;
CULL_INVALID(vert);
/* z coord */
if (ctx->DrawBuffer->Visual.depthBits <= 16)
span.z = FloatToFixed(vert->attrib[VARYING_SLOT_POS][2] + 0.5F);
else
span.z = (GLuint) (vert->attrib[VARYING_SLOT_POS][2] + 0.5F);
span.zStep = 0;
size = get_size(ctx, vert, GL_TRUE);
/* alpha attenuation / 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;
}
(void) alphaAtten; /* not used */
/* span init */
INIT_SPAN(span, GL_POINT);
span.interpMask = SPAN_Z | SPAN_RGBA;
span.arrayMask = SPAN_COVERAGE | SPAN_MASK;
span.facing = swrast->PointLineFacing;
span.red = ChanToFixed(vert->color[0]);
span.green = ChanToFixed(vert->color[1]);
span.blue = ChanToFixed(vert->color[2]);
span.alpha = ChanToFixed(vert->color[3]);
span.redStep = 0;
span.greenStep = 0;
span.blueStep = 0;
span.alphaStep = 0;
/* need these for fragment programs */
span.attrStart[VARYING_SLOT_POS][3] = 1.0F;
span.attrStepX[VARYING_SLOT_POS][3] = 0.0F;
span.attrStepY[VARYING_SLOT_POS][3] = 0.0F;
ATTRIB_LOOP_BEGIN
COPY_4V(span.attrStart[attr], vert->attrib[attr]);
ASSIGN_4V(span.attrStepX[attr], 0, 0, 0, 0);
ASSIGN_4V(span.attrStepY[attr], 0, 0, 0, 0);
ATTRIB_LOOP_END
/* compute pos, bounds and render */
{
const GLfloat x = vert->attrib[VARYING_SLOT_POS][0];
const GLfloat y = vert->attrib[VARYING_SLOT_POS][1];
const GLfloat radius = 0.5F * size;
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) (x - radius);
const GLint xmax = (GLint) (x + radius);
const GLint ymin = (GLint) (y - radius);
const GLint ymax = (GLint) (y + radius);
GLint ix, iy;
for (iy = ymin; iy <= ymax; iy++) {
/* these might get changed by span clipping */
span.x = xmin;
span.y = iy;
span.end = xmax - xmin + 1;
/* compute coverage for each pixel in span */
for (ix = xmin; ix <= xmax; ix++) {
const GLfloat dx = ix - x + 0.5F;
const GLfloat dy = iy - y + 0.5F;
const GLfloat dist2 = dx * dx + dy * dy;
GLfloat coverage;
if (dist2 < rmax2) {
if (dist2 >= rmin2) {
/* compute partial coverage */
coverage = 1.0F - (dist2 - rmin2) * cscale;
}
else {
/* full coverage */
coverage = 1.0F;
}
span.array->mask[ix - xmin] = 1;
}
else {
/* zero coverage - fragment outside the radius */
coverage = 0.0;
span.array->mask[ix - xmin] = 0;
}
span.array->coverage[ix - xmin] = coverage;
}
/* render span */
_swrast_write_rgba_span(ctx, &span);
}
}
}
/**
* Draw large (size >= 1) non-AA point. RGB or CI mode.
*/
static void
large_point(struct gl_context *ctx, const SWvertex *vert)
{
SWcontext *swrast = SWRAST_CONTEXT(ctx);
SWspan span;
GLfloat size;
CULL_INVALID(vert);
/* z coord */
if (ctx->DrawBuffer->Visual.depthBits <= 16)
span.z = FloatToFixed(vert->attrib[VARYING_SLOT_POS][2] + 0.5F);
else
span.z = (GLuint) (vert->attrib[VARYING_SLOT_POS][2] + 0.5F);
span.zStep = 0;
size = get_size(ctx, vert, GL_FALSE);
/* span init */
INIT_SPAN(span, GL_POINT);
span.arrayMask = SPAN_XY;
span.facing = swrast->PointLineFacing;
span.interpMask = SPAN_Z | SPAN_RGBA;
span.red = ChanToFixed(vert->color[0]);
span.green = ChanToFixed(vert->color[1]);
span.blue = ChanToFixed(vert->color[2]);
span.alpha = ChanToFixed(vert->color[3]);
span.redStep = 0;
span.greenStep = 0;
span.blueStep = 0;
span.alphaStep = 0;
/* need these for fragment programs */
span.attrStart[VARYING_SLOT_POS][3] = 1.0F;
span.attrStepX[VARYING_SLOT_POS][3] = 0.0F;
span.attrStepY[VARYING_SLOT_POS][3] = 0.0F;
ATTRIB_LOOP_BEGIN
COPY_4V(span.attrStart[attr], vert->attrib[attr]);
ASSIGN_4V(span.attrStepX[attr], 0, 0, 0, 0);
ASSIGN_4V(span.attrStepY[attr], 0, 0, 0, 0);
ATTRIB_LOOP_END
/* compute pos, bounds and render */
{
const GLfloat x = vert->attrib[VARYING_SLOT_POS][0];
const GLfloat y = vert->attrib[VARYING_SLOT_POS][1];
GLint iSize = (GLint) (size + 0.5F);
GLint xmin, xmax, ymin, ymax, ix, iy;
GLint iRadius;
iSize = MAX2(1, iSize);
iRadius = iSize / 2;
if (iSize & 1) {
/* odd size */
xmin = (GLint) (x - iRadius);
xmax = (GLint) (x + iRadius);
ymin = (GLint) (y - iRadius);
ymax = (GLint) (y + iRadius);
}
else {
/* even size */
/* 0.501 factor allows conformance to pass */
xmin = (GLint) (x + 0.501) - iRadius;
xmax = xmin + iSize - 1;
ymin = (GLint) (y + 0.501) - iRadius;
ymax = ymin + iSize - 1;
}
/* generate fragments */
span.end = 0;
for (iy = ymin; iy <= ymax; iy++) {
for (ix = xmin; ix <= xmax; ix++) {
span.array->x[span.end] = ix;
span.array->y[span.end] = iy;
span.end++;
}
}
assert(span.end <= SWRAST_MAX_WIDTH);
_swrast_write_rgba_span(ctx, &span);
}
}
/**
* Draw size=1, single-pixel point
*/
static void
pixel_point(struct gl_context *ctx, const SWvertex *vert)
{
SWcontext *swrast = SWRAST_CONTEXT(ctx);
/*
* Note that unlike the other functions, we put single-pixel points
* into a special span array in order to render as many points as
* possible with a single _swrast_write_rgba_span() call.
*/
SWspan *span = &(swrast->PointSpan);
GLuint count;
CULL_INVALID(vert);
/* Span init */
span->interpMask = 0;
span->arrayMask = SPAN_XY | SPAN_Z;
span->arrayMask |= SPAN_RGBA;
/*span->arrayMask |= SPAN_LAMBDA;*/
span->arrayAttribs = swrast->_ActiveAttribMask; /* we'll produce these vals */
/* need these for fragment programs */
span->attrStart[VARYING_SLOT_POS][3] = 1.0F;
span->attrStepX[VARYING_SLOT_POS][3] = 0.0F;
span->attrStepY[VARYING_SLOT_POS][3] = 0.0F;
/* check if we need to flush */
if (span->end >= SWRAST_MAX_WIDTH ||
(swrast->_RasterMask & (BLEND_BIT | LOGIC_OP_BIT | MASKING_BIT)) ||
span->facing != swrast->PointLineFacing) {
if (span->end > 0) {
_swrast_write_rgba_span(ctx, span);
span->end = 0;
}
}
count = span->end;
span->facing = swrast->PointLineFacing;
/* fragment attributes */
span->array->rgba[count][RCOMP] = vert->color[0];
span->array->rgba[count][GCOMP] = vert->color[1];
span->array->rgba[count][BCOMP] = vert->color[2];
span->array->rgba[count][ACOMP] = vert->color[3];
ATTRIB_LOOP_BEGIN
COPY_4V(span->array->attribs[attr][count], vert->attrib[attr]);
ATTRIB_LOOP_END
/* fragment position */
span->array->x[count] = (GLint) vert->attrib[VARYING_SLOT_POS][0];
span->array->y[count] = (GLint) vert->attrib[VARYING_SLOT_POS][1];
span->array->z[count] = (GLint) (vert->attrib[VARYING_SLOT_POS][2] + 0.5F);
span->end = count + 1;
ASSERT(span->end <= SWRAST_MAX_WIDTH);
}
/**
* Add specular color to primary color, draw point, restore original
* primary color.
*/
void
_swrast_add_spec_terms_point(struct gl_context *ctx, const SWvertex *v0)
{
SWvertex *ncv0 = (SWvertex *) v0; /* cast away const */
GLfloat rSum, gSum, bSum;
GLchan cSave[4];
/* save */
COPY_CHAN4(cSave, ncv0->color);
/* sum */
rSum = CHAN_TO_FLOAT(ncv0->color[0]) + ncv0->attrib[VARYING_SLOT_COL1][0];
gSum = CHAN_TO_FLOAT(ncv0->color[1]) + ncv0->attrib[VARYING_SLOT_COL1][1];
bSum = CHAN_TO_FLOAT(ncv0->color[2]) + ncv0->attrib[VARYING_SLOT_COL1][2];
UNCLAMPED_FLOAT_TO_CHAN(ncv0->color[0], rSum);
UNCLAMPED_FLOAT_TO_CHAN(ncv0->color[1], gSum);
UNCLAMPED_FLOAT_TO_CHAN(ncv0->color[2], bSum);
/* draw */
SWRAST_CONTEXT(ctx)->SpecPoint(ctx, ncv0);
/* restore */
COPY_CHAN4(ncv0->color, cSave);
}
/**
* Examine current state to determine which point drawing function to use.
*/
void
_swrast_choose_point(struct gl_context *ctx)
{
SWcontext *swrast = SWRAST_CONTEXT(ctx);
const GLfloat size = CLAMP(ctx->Point.Size,
ctx->Point.MinSize,
ctx->Point.MaxSize);
if (ctx->RenderMode == GL_RENDER) {
if (ctx->Point.PointSprite) {
swrast->Point = sprite_point;
}
else if (ctx->Point.SmoothFlag) {
swrast->Point = smooth_point;
}
else if (size > 1.0 ||
ctx->Point._Attenuated ||
ctx->VertexProgram.PointSizeEnabled) {
swrast->Point = large_point;
}
else {
swrast->Point = pixel_point;
}
}
else if (ctx->RenderMode == GL_FEEDBACK) {
swrast->Point = _swrast_feedback_point;
}
else {
/* GL_SELECT mode */
swrast->Point = _swrast_select_point;
}
}
|