87b6aa40ee95a573980abea279edaa31a1299353
[mesa.git] / src / mesa / swrast / s_span.c
1 /* $Id: s_span.c,v 1.26 2002/01/28 03:42:28 brianp Exp $ */
2
3 /*
4 * Mesa 3-D graphics library
5 * Version: 4.1
6 *
7 * Copyright (C) 1999-2002 Brian Paul All Rights Reserved.
8 *
9 * Permission is hereby granted, free of charge, to any person obtaining a
10 * copy of this software and associated documentation files (the "Software"),
11 * to deal in the Software without restriction, including without limitation
12 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
13 * and/or sell copies of the Software, and to permit persons to whom the
14 * Software is furnished to do so, subject to the following conditions:
15 *
16 * The above copyright notice and this permission notice shall be included
17 * in all copies or substantial portions of the Software.
18 *
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
20 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
22 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
23 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
24 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 */
26
27
28 /*
29 * pixel span rasterization:
30 * These functions implement the rasterization pipeline.
31 */
32
33
34 #include "glheader.h"
35 #include "colormac.h"
36 #include "context.h"
37 #include "macros.h"
38 #include "mem.h"
39
40 #include "s_alpha.h"
41 #include "s_alphabuf.h"
42 #include "s_blend.h"
43 #include "s_context.h"
44 #include "s_depth.h"
45 #include "s_fog.h"
46 #include "s_logic.h"
47 #include "s_masking.h"
48 #include "s_scissor.h"
49 #include "s_span.h"
50 #include "s_stencil.h"
51 #include "s_texture.h"
52
53
54 /*
55 * Init span's Z interpolation values to the RasterPos Z.
56 * Used during setup for glDraw/CopyPixels.
57 */
58 void
59 _mesa_span_default_z( GLcontext *ctx, struct sw_span *span )
60 {
61 if (ctx->Visual.depthBits <= 16)
62 span->z = FloatToFixed(ctx->Current.RasterPos[2] * ctx->DepthMax);
63 else
64 span->z = (GLint) (ctx->Current.RasterPos[2] * ctx->DepthMax);
65 span->zStep = 0;
66 span->interpMask |= SPAN_Z;
67 }
68
69
70 /*
71 * Init span's fog interpolation values to the RasterPos fog.
72 * Used during setup for glDraw/CopyPixels.
73 */
74 void
75 _mesa_span_default_fog( GLcontext *ctx, struct sw_span *span )
76 {
77 if (ctx->Fog.FogCoordinateSource == GL_FOG_COORDINATE_EXT)
78 span->fog = _mesa_z_to_fogfactor(ctx, ctx->Current.RasterFogCoord);
79 else
80 span->fog = _mesa_z_to_fogfactor(ctx, ctx->Current.RasterDistance);
81 span->fogStep = 0;
82 span->interpMask |= SPAN_FOG;
83 }
84
85
86 /*
87 * Init span's color or index interpolation values to the RasterPos color.
88 * Used during setup for glDraw/CopyPixels.
89 */
90 void
91 _mesa_span_default_color( GLcontext *ctx, struct sw_span *span )
92 {
93 if (ctx->Visual.rgbMode) {
94 GLchan r, g, b, a;
95 UNCLAMPED_FLOAT_TO_CHAN(r, ctx->Current.RasterColor[0]);
96 UNCLAMPED_FLOAT_TO_CHAN(g, ctx->Current.RasterColor[1]);
97 UNCLAMPED_FLOAT_TO_CHAN(b, ctx->Current.RasterColor[2]);
98 UNCLAMPED_FLOAT_TO_CHAN(a, ctx->Current.RasterColor[3]);
99 #if CHAN_TYPE == GL_FLOAT
100 span->red = r;
101 span->green = g;
102 span->blue = b;
103 span->alpha = a;
104 #else
105 span->red = IntToFixed(r);
106 span->green = IntToFixed(g);
107 span->blue = IntToFixed(b);
108 span->alpha = IntToFixed(a);
109 #endif
110 span->redStep = 0;
111 span->greenStep = 0;
112 span->blueStep = 0;
113 span->alphaStep = 0;
114 span->interpMask |= SPAN_RGBA;
115 }
116 else {
117 span->index = IntToFixed(ctx->Current.RasterIndex);
118 span->indexStep = 0;
119 span->interpMask |= SPAN_INDEX;
120 }
121 }
122
123
124 /* Fill in the span.color.rgba array from the interpolation values */
125 static void
126 interpolate_colors(GLcontext *ctx, struct sw_span *span)
127 {
128 GLfixed r = span->red;
129 GLfixed g = span->green;
130 GLfixed b = span->blue;
131 GLfixed a = span->alpha;
132 const GLint dr = span->redStep;
133 const GLint dg = span->greenStep;
134 const GLint db = span->blueStep;
135 const GLint da = span->alphaStep;
136 const GLuint n = span->end;
137 GLchan (*rgba)[4] = span->color.rgba;
138 GLuint i;
139
140 ASSERT(span->interpMask & SPAN_RGBA);
141
142 if (span->interpMask & SPAN_FLAT) {
143 /* constant color */
144 GLchan color[4];
145 color[RCOMP] = FixedToChan(r);
146 color[GCOMP] = FixedToChan(g);
147 color[BCOMP] = FixedToChan(b);
148 color[ACOMP] = FixedToChan(a);
149 for (i = 0; i < n; i++) {
150 COPY_CHAN4(span->color.rgba[i], color);
151 }
152 }
153 else {
154 /* interpolate */
155 for (i = 0; i < n; i++) {
156 rgba[i][RCOMP] = FixedToChan(r);
157 rgba[i][GCOMP] = FixedToChan(g);
158 rgba[i][BCOMP] = FixedToChan(b);
159 rgba[i][ACOMP] = FixedToChan(a);
160 r += dr;
161 g += dg;
162 b += db;
163 a += da;
164 }
165 }
166 span->arrayMask |= SPAN_RGBA;
167 }
168
169
170 /* Fill in the span.color.index array from the interpolation values */
171 static void
172 interpolate_indexes(GLcontext *ctx, struct sw_span *span)
173 {
174 GLfixed index = span->index;
175 const GLint indexStep = span->indexStep;
176 const GLuint n = span->end;
177 GLuint *indexes = span->color.index;
178 GLuint i;
179 ASSERT(span->interpMask & SPAN_INDEX);
180
181 if ((span->interpMask & SPAN_FLAT) || (indexStep == 0)) {
182 /* constant color */
183 index = FixedToInt(index);
184 for (i = 0; i < n; i++) {
185 indexes[i] = index;
186 }
187 }
188 else {
189 /* interpolate */
190 for (i = 0; i < n; i++) {
191 indexes[i] = FixedToInt(index);
192 index += indexStep;
193 }
194 }
195 span->arrayMask |= SPAN_INDEX;
196 }
197
198
199 /* Fill in the span.specArray array from the interpolation values */
200 static void
201 interpolate_specular(GLcontext *ctx, struct sw_span *span)
202 {
203 if (span->interpMask & SPAN_FLAT) {
204 /* constant color */
205 const GLchan r = FixedToChan(span->specRed);
206 const GLchan g = FixedToChan(span->specGreen);
207 const GLchan b = FixedToChan(span->specBlue);
208 GLuint i;
209 for (i = 0; i < span->end; i++) {
210 span->specArray[i][RCOMP] = r;
211 span->specArray[i][GCOMP] = g;
212 span->specArray[i][BCOMP] = b;
213 }
214 }
215 else {
216 /* interpolate */
217 #if CHAN_TYPE == GL_FLOAT
218 GLfloat r = span->specRed;
219 GLfloat g = span->specGreen;
220 GLfloat b = span->specBlue;
221 #else
222 GLfixed r = span->specRed;
223 GLfixed g = span->specGreen;
224 GLfixed b = span->specBlue;
225 #endif
226 GLuint i;
227 for (i = 0; i < span->end; i++) {
228 span->specArray[i][RCOMP] = FixedToChan(r);
229 span->specArray[i][GCOMP] = FixedToChan(g);
230 span->specArray[i][BCOMP] = FixedToChan(b);
231 r += span->specRedStep;
232 g += span->specGreenStep;
233 b += span->specBlueStep;
234 }
235 }
236 span->arrayMask |= SPAN_SPEC;
237 }
238
239
240 /* Fill in the span.zArray array from the interpolation values */
241 static void
242 interpolate_z(GLcontext *ctx, struct sw_span *span)
243 {
244 const GLuint n = span->end;
245 GLuint i;
246
247 ASSERT(span->interpMask & SPAN_Z);
248
249 if (ctx->Visual.depthBits <= 16) {
250 GLfixed zval = span->z;
251 for (i = 0; i < n; i++) {
252 span->zArray[i] = FixedToInt(zval);
253 zval += span->zStep;
254 }
255 }
256 else {
257 /* Deep Z buffer, no fixed->int shift */
258 GLfixed zval = span->z;
259 for (i = 0; i < n; i++) {
260 span->zArray[i] = zval;
261 zval += span->zStep;
262 }
263 }
264 span->arrayMask |= SPAN_Z;
265 }
266
267
268
269 /* Fill in the span.texcoords array from the interpolation values */
270 static void
271 interpolate_texcoords(GLcontext *ctx, struct sw_span *span)
272 {
273 ASSERT(span->interpMask & SPAN_TEXTURE);
274
275 if (ctx->Texture._ReallyEnabled & ~TEXTURE0_ANY) {
276 if (span->interpMask & SPAN_LAMBDA) {
277 /* multitexture, lambda */
278 GLuint u;
279 for (u = 0; u < ctx->Const.MaxTextureUnits; u++) {
280 if (ctx->Texture.Unit[u]._ReallyEnabled) {
281 const GLfloat ds = span->texStep[u][0];
282 const GLfloat dt = span->texStep[u][1];
283 const GLfloat dr = span->texStep[u][2];
284 const GLfloat dq = span->texStep[u][3];
285 GLfloat s = span->tex[u][0];
286 GLfloat t = span->tex[u][1];
287 GLfloat r = span->tex[u][2];
288 GLfloat q = span->tex[u][3];
289 GLuint i;
290 for (i = 0; i < span->end; i++) {
291 const GLfloat invQ = (q == 0.0F) ? 1.0F : (1.0F / q);
292 span->texcoords[u][i][0] = s * invQ;
293 span->texcoords[u][i][1] = t * invQ;
294 span->texcoords[u][i][2] = r * invQ;
295 span->lambda[u][i] = (GLfloat)
296 (log(span->rho[u] * invQ * invQ) * 1.442695F * 0.5F);
297 s += ds;
298 t += dt;
299 r += dr;
300 q += dq;
301 }
302 }
303 }
304 span->arrayMask |= SPAN_LAMBDA;
305 }
306 else {
307 /* multitexture, no lambda */
308 GLuint u;
309 for (u = 0; u < ctx->Const.MaxTextureUnits; u++) {
310 if (ctx->Texture.Unit[u]._ReallyEnabled) {
311 const GLfloat ds = span->texStep[u][0];
312 const GLfloat dt = span->texStep[u][1];
313 const GLfloat dr = span->texStep[u][2];
314 const GLfloat dq = span->texStep[u][3];
315 GLfloat s = span->tex[u][0];
316 GLfloat t = span->tex[u][1];
317 GLfloat r = span->tex[u][2];
318 GLfloat q = span->tex[u][3];
319 GLuint i;
320 for (i = 0; i < span->end; i++) {
321 const GLfloat invQ = (q == 0.0F) ? 1.0F : (1.0F / q);
322 span->texcoords[u][i][0] = s * invQ;
323 span->texcoords[u][i][1] = t * invQ;
324 span->texcoords[u][i][2] = r * invQ;
325 s += ds;
326 t += dt;
327 r += dr;
328 q += dq;
329 }
330 }
331 }
332 }
333 }
334 else {
335 if (span->interpMask & SPAN_LAMBDA) {
336 /* just texture unit 0, with lambda */
337 const GLfloat ds = span->texStep[0][0];
338 const GLfloat dt = span->texStep[0][1];
339 const GLfloat dr = span->texStep[0][2];
340 const GLfloat dq = span->texStep[0][3];
341 GLfloat s = span->tex[0][0];
342 GLfloat t = span->tex[0][1];
343 GLfloat r = span->tex[0][2];
344 GLfloat q = span->tex[0][3];
345 GLuint i;
346 for (i = 0; i < span->end; i++) {
347 const GLfloat invQ = (q == 0.0F) ? 1.0F : (1.0F / q);
348 span->texcoords[0][i][0] = s * invQ;
349 span->texcoords[0][i][1] = t * invQ;
350 span->texcoords[0][i][2] = r * invQ;
351 span->lambda[0][i] = (GLfloat)
352 (log(span->rho[0] * invQ * invQ) * 1.442695F * 0.5F);
353 s += ds;
354 t += dt;
355 r += dr;
356 q += dq;
357 }
358 span->arrayMask |= SPAN_LAMBDA;
359 }
360 else {
361 /* just texture 0, witout lambda */
362 const GLfloat ds = span->texStep[0][0];
363 const GLfloat dt = span->texStep[0][1];
364 const GLfloat dr = span->texStep[0][2];
365 const GLfloat dq = span->texStep[0][3];
366 GLfloat s = span->tex[0][0];
367 GLfloat t = span->tex[0][1];
368 GLfloat r = span->tex[0][2];
369 GLfloat q = span->tex[0][3];
370 GLuint i;
371 for (i = 0; i < span->end; i++) {
372 const GLfloat invQ = (q == 0.0F) ? 1.0F : (1.0F / q);
373 span->texcoords[0][i][0] = s * invQ;
374 span->texcoords[0][i][1] = t * invQ;
375 span->texcoords[0][i][2] = r * invQ;
376 s += ds;
377 t += dt;
378 r += dr;
379 q += dq;
380 }
381 }
382 }
383 }
384
385
386 /*
387 * Apply the current polygon stipple pattern to a span of pixels.
388 */
389 static void
390 stipple_polygon_span( GLcontext *ctx, struct sw_span *span)
391 {
392 const GLuint highbit = 0x80000000;
393 GLuint i, m, stipple;
394
395 stipple = ctx->PolygonStipple[span->y % 32];
396 m = highbit >> (GLuint) (span->x % 32);
397
398 for (i = 0; i < span->end; i++) {
399 if ((m & stipple) == 0) {
400 span->mask[i] = 0;
401 }
402 m = m >> 1;
403 if (m == 0) {
404 m = highbit;
405 }
406 }
407 span->writeAll = GL_FALSE;
408 }
409
410
411 /*
412 * Clip a pixel span to the current buffer/window boundaries.
413 * Return: GL_TRUE some pixel still visible
414 * GL_FALSE nothing visible
415 */
416 static GLuint
417 clip_span( GLcontext *ctx, struct sw_span *span)
418 {
419 GLint x = span->x, y = span->y, n = span->end;
420
421 /* Clip to top and bottom */
422 if (y < 0 || y >= ctx->DrawBuffer->Height) {
423 span->end = 0;
424 return GL_FALSE;
425 }
426
427 /* Clip to the left */
428 if (x < 0) {
429 if (x + n <= 0) {
430 /* completely off left side */
431 span->end = 0;
432 return GL_FALSE;
433 }
434 else {
435 /* partially off left side */
436 span->writeAll = GL_FALSE;
437 BZERO(span->mask, -x * sizeof(GLubyte));
438 return GL_TRUE;
439 }
440 }
441
442 /* Clip to right */
443 if (x + n > ctx->DrawBuffer->Width) {
444 if (x >= ctx->DrawBuffer->Width) {
445 /* completely off right side */
446 span->end = 0;
447 return GL_FALSE;
448 }
449 else {
450 /* partially off right side */
451 span->end = ctx->DrawBuffer->Width - x;
452 return GL_TRUE;
453 }
454 }
455
456 return GL_TRUE;
457 }
458
459
460
461 /*
462 * Draw to more than one color buffer (or none).
463 */
464 static void
465 multi_write_index_span( GLcontext *ctx, GLuint n, GLint x, GLint y,
466 const GLuint indexes[], const GLubyte mask[] )
467 {
468 SWcontext *swrast = SWRAST_CONTEXT(ctx);
469 GLuint bufferBit;
470
471 if (ctx->Color.DrawBuffer == GL_NONE)
472 return;
473
474 /* loop over four possible dest color buffers */
475 for (bufferBit = 1; bufferBit <= 8; bufferBit = bufferBit << 1) {
476 if (bufferBit & ctx->Color.DrawDestMask) {
477 GLuint indexTmp[MAX_WIDTH];
478 ASSERT(n < MAX_WIDTH);
479
480 if (bufferBit == FRONT_LEFT_BIT)
481 (void) (*ctx->Driver.SetDrawBuffer)( ctx, GL_FRONT_LEFT);
482 else if (bufferBit == FRONT_RIGHT_BIT)
483 (void) (*ctx->Driver.SetDrawBuffer)( ctx, GL_FRONT_RIGHT);
484 else if (bufferBit == BACK_LEFT_BIT)
485 (void) (*ctx->Driver.SetDrawBuffer)( ctx, GL_BACK_LEFT);
486 else
487 (void) (*ctx->Driver.SetDrawBuffer)( ctx, GL_BACK_RIGHT);
488
489 /* make copy of incoming indexes */
490 MEMCPY( indexTmp, indexes, n * sizeof(GLuint) );
491 if (ctx->Color.IndexLogicOpEnabled) {
492 _mesa_logicop_ci_span( ctx, n, x, y, indexTmp, mask );
493 }
494 if (ctx->Color.IndexMask != 0xffffffff) {
495 _mesa_mask_index_span( ctx, n, x, y, indexTmp );
496 }
497 (*swrast->Driver.WriteCI32Span)( ctx, n, x, y, indexTmp, mask );
498 }
499 }
500
501 /* restore default dest buffer */
502 (void) (*ctx->Driver.SetDrawBuffer)( ctx, ctx->Color.DriverDrawBuffer);
503 }
504
505
506 /*
507 * Draw to more than one RGBA color buffer (or none).
508 * All fragment operations, up to (but not) blending/logicop should
509 * have been done first.
510 */
511 static void
512 multi_write_rgba_span( GLcontext *ctx, GLuint n, GLint x, GLint y,
513 CONST GLchan rgba[][4], const GLubyte mask[] )
514 {
515 const GLuint colorMask = *((GLuint *) ctx->Color.ColorMask);
516 GLuint bufferBit;
517 SWcontext *swrast = SWRAST_CONTEXT(ctx);
518
519 if (ctx->Color.DrawBuffer == GL_NONE)
520 return;
521
522 /* loop over four possible dest color buffers */
523 for (bufferBit = 1; bufferBit <= 8; bufferBit = bufferBit << 1) {
524 if (bufferBit & ctx->Color.DrawDestMask) {
525 GLchan rgbaTmp[MAX_WIDTH][4];
526 ASSERT(n < MAX_WIDTH);
527
528 if (bufferBit == FRONT_LEFT_BIT) {
529 (void) (*ctx->Driver.SetDrawBuffer)( ctx, GL_FRONT_LEFT);
530 ctx->DrawBuffer->Alpha = ctx->DrawBuffer->FrontLeftAlpha;
531 }
532 else if (bufferBit == FRONT_RIGHT_BIT) {
533 (void) (*ctx->Driver.SetDrawBuffer)( ctx, GL_FRONT_RIGHT);
534 ctx->DrawBuffer->Alpha = ctx->DrawBuffer->FrontRightAlpha;
535 }
536 else if (bufferBit == BACK_LEFT_BIT) {
537 (void) (*ctx->Driver.SetDrawBuffer)( ctx, GL_BACK_LEFT);
538 ctx->DrawBuffer->Alpha = ctx->DrawBuffer->BackLeftAlpha;
539 }
540 else {
541 (void) (*ctx->Driver.SetDrawBuffer)( ctx, GL_BACK_RIGHT);
542 ctx->DrawBuffer->Alpha = ctx->DrawBuffer->BackRightAlpha;
543 }
544
545 /* make copy of incoming colors */
546 MEMCPY( rgbaTmp, rgba, 4 * n * sizeof(GLchan) );
547
548 if (ctx->Color.ColorLogicOpEnabled) {
549 _mesa_logicop_rgba_span( ctx, n, x, y, rgbaTmp, mask );
550 }
551 else if (ctx->Color.BlendEnabled) {
552 _mesa_blend_span( ctx, n, x, y, rgbaTmp, mask );
553 }
554 if (colorMask == 0x0) {
555 break;
556 }
557 else if (colorMask != 0xffffffff) {
558 _mesa_mask_rgba_span( ctx, n, x, y, rgbaTmp );
559 }
560
561 (*swrast->Driver.WriteRGBASpan)( ctx, n, x, y,
562 (const GLchan (*)[4]) rgbaTmp, mask );
563 if (swrast->_RasterMask & ALPHABUF_BIT) {
564 _mesa_write_alpha_span( ctx, n, x, y,
565 (const GLchan (*)[4])rgbaTmp, mask );
566 }
567 }
568 }
569
570 /* restore default dest buffer */
571 (void) (*ctx->Driver.SetDrawBuffer)( ctx, ctx->Color.DriverDrawBuffer );
572 }
573
574
575
576 /*
577 * This function may modify any of the array values in the span.
578 * span->interpMask and span->arrayMask may be changed but will be restored
579 * to their original values before returning.
580 */
581 void
582 _mesa_write_index_span( GLcontext *ctx, struct sw_span *span,
583 GLenum primitive)
584 {
585 SWcontext *swrast = SWRAST_CONTEXT(ctx);
586 const GLuint origInterpMask = span->interpMask;
587 const GLuint origArrayMask = span->arrayMask;
588
589 ASSERT((span->interpMask & span->arrayMask) == 0);
590
591 MEMSET(span->mask, 1, span->end);
592 span->writeAll = GL_TRUE;
593
594 /* Window clipping */
595 if ((swrast->_RasterMask & WINCLIP_BIT) || primitive==GL_BITMAP) {
596 if (clip_span(ctx,span) == GL_FALSE) {
597 return;
598 }
599 }
600
601 /* Scissor test */
602 if (ctx->Scissor.Enabled) {
603 if (_mesa_scissor_span( ctx, span ) == GL_FALSE) {
604 return;
605 }
606 }
607
608 /* Polygon Stippling */
609 if (ctx->Polygon.StippleFlag && primitive==GL_POLYGON) {
610 stipple_polygon_span(ctx, span);
611 }
612
613 /* Depth test and stencil */
614 if (ctx->Depth.Test || ctx->Stencil.Enabled) {
615 if (span->interpMask & SPAN_Z)
616 interpolate_z(ctx, span);
617
618 if (ctx->Stencil.Enabled) {
619 if (_mesa_stencil_and_ztest_span(ctx, span) == GL_FALSE) {
620 span->arrayMask = origArrayMask;
621 return;
622 }
623 }
624 else {
625 ASSERT(ctx->Depth.Test);
626 if (_mesa_depth_test_span(ctx, span) == 0) {
627 span->arrayMask = origArrayMask;
628 return;
629 }
630 }
631 }
632
633 /* if we get here, something passed the depth test */
634 ctx->OcclusionResult = GL_TRUE;
635
636 /* we have to wait until after occlusion to do this test */
637 if (ctx->Color.DrawBuffer == GL_NONE || ctx->Color.IndexMask == 0) {
638 /* write no pixels */
639 span->arrayMask = origArrayMask;
640 return;
641 }
642
643 /* Interpolate the color indexes if needed */
644 if (span->interpMask & SPAN_INDEX) {
645 interpolate_indexes(ctx, span);
646 /* clear the bit - this allows the WriteMonoCISpan optimization below */
647 span->interpMask &= ~SPAN_INDEX;
648 }
649
650 /* Fog */
651 if (ctx->Fog.Enabled) {
652 if ((span->arrayMask & SPAN_FOG) && !swrast->_PreferPixelFog)
653 _mesa_fog_ci_pixels_with_array( ctx, span, span->fogArray,
654 span->color.index);
655 else if ((span->interpMask & SPAN_FOG) && !swrast->_PreferPixelFog)
656 _mesa_fog_ci_pixels( ctx, span, span->color.index);
657 else
658 _mesa_depth_fog_ci_pixels( ctx, span, span->color.index);
659 }
660
661 /* Antialias coverage application */
662 if (span->arrayMask & SPAN_COVERAGE) {
663 GLuint i;
664 GLuint *index = span->color.index;
665 for (i = 0; i < span->end; i++) {
666 ASSERT(span->coverage[i] < 16);
667 index[i] = (index[i] & ~0xf) | ((GLuint) (span->coverage[i]));
668 }
669 }
670
671 if (swrast->_RasterMask & MULTI_DRAW_BIT) {
672 /* draw to zero or two or more buffers */
673 multi_write_index_span( ctx, span->end, span->x, span->y,
674 span->color.index, span->mask );
675 }
676 else {
677 /* normal situation: draw to exactly one buffer */
678 if (ctx->Color.IndexLogicOpEnabled) {
679 _mesa_logicop_ci_span( ctx, span->end, span->x, span->y,
680 span->color.index, span->mask );
681 }
682
683 if (ctx->Color.IndexMask != 0xffffffff) {
684 _mesa_mask_index_span( ctx, span->end, span->x, span->y,
685 span->color.index );
686 }
687
688 /* write pixels */
689 if ((span->interpMask & SPAN_INDEX) && span->indexStep == 0) {
690 /* all pixels have same color index */
691 (*swrast->Driver.WriteMonoCISpan)( ctx, span->end, span->x, span->y,
692 FixedToInt(span->index),
693 span->mask );
694 }
695 else {
696 (*swrast->Driver.WriteCI32Span)( ctx, span->end, span->x, span->y,
697 span->color.index, span->mask );
698 }
699 }
700
701 span->interpMask = origInterpMask;
702 span->arrayMask = origArrayMask;
703 }
704
705
706 /*
707 * This function may modify any of the array values in the span.
708 * span->interpMask and span->arrayMask may be changed but will be restored
709 * to their original values before returning.
710 */
711 void
712 _mesa_write_rgba_span( GLcontext *ctx, struct sw_span *span,
713 GLenum primitive)
714 {
715 SWcontext *swrast = SWRAST_CONTEXT(ctx);
716 const GLuint colorMask = *((GLuint *) ctx->Color.ColorMask);
717 const GLuint origInterpMask = span->interpMask;
718 const GLuint origArrayMask = span->arrayMask;
719 GLboolean monoColor;
720
721 ASSERT((span->interpMask & span->arrayMask) == 0);
722
723 MEMSET(span->mask, 1, span->end);
724 span->writeAll = GL_TRUE;
725
726 /* Determine if we have mono-chromatic colors */
727 monoColor = (span->interpMask & SPAN_RGBA) &&
728 span->redStep == 0 && span->greenStep == 0 &&
729 span->blueStep == 0 && span->alphaStep == 0;
730
731 /* Window clipping */
732 if ((swrast->_RasterMask & WINCLIP_BIT) || primitive == GL_BITMAP) {
733 if (clip_span(ctx, span) == GL_FALSE) {
734 return;
735 }
736 }
737
738 /* Scissor test */
739 if (ctx->Scissor.Enabled) {
740 if (!_mesa_scissor_span(ctx, span)) {
741 return;
742 }
743 }
744
745 /* Polygon Stippling */
746 if (ctx->Polygon.StippleFlag && primitive == GL_POLYGON) {
747 stipple_polygon_span(ctx, span);
748 }
749
750 /* Now we may need to interpolate the colors */
751 if ((span->interpMask & SPAN_RGBA) && (span->arrayMask & SPAN_RGBA) == 0) {
752 interpolate_colors(ctx, span);
753 /* clear the bit - this allows the WriteMonoCISpan optimization below */
754 span->interpMask &= ~SPAN_RGBA;
755 }
756
757 /* Do the alpha test */
758 if (ctx->Color.AlphaEnabled) {
759 if (!_mesa_alpha_test(ctx, span,
760 (const GLchan (*)[4]) span->color.rgba)) {
761 span->interpMask = origInterpMask;
762 span->arrayMask = origArrayMask;
763 return;
764 }
765 }
766
767 /* Stencil and Z testing */
768 if (ctx->Stencil.Enabled || ctx->Depth.Test) {
769 if (span->interpMask & SPAN_Z)
770 interpolate_z(ctx, span);
771
772 if (ctx->Stencil.Enabled) {
773 if (!_mesa_stencil_and_ztest_span(ctx, span)) {
774 span->interpMask = origInterpMask;
775 span->arrayMask = origArrayMask;
776 return;
777 }
778 }
779 else {
780 ASSERT(ctx->Depth.Test);
781 ASSERT(span->arrayMask & SPAN_Z);
782 /* regular depth testing */
783 if (!_mesa_depth_test_span(ctx, span)) {
784 span->interpMask = origInterpMask;
785 span->arrayMask = origArrayMask;
786 return;
787 }
788 }
789 }
790
791 /* if we get here, something passed the depth test */
792 ctx->OcclusionResult = GL_TRUE;
793
794 /* can't abort span-writing until after occlusion testing */
795 if (colorMask == 0x0) {
796 span->interpMask = origInterpMask;
797 span->arrayMask = origArrayMask;
798 return;
799 }
800
801 /* Per-pixel fog */
802 if (ctx->Fog.Enabled) {
803 if ((span->arrayMask & SPAN_FOG) && !swrast->_PreferPixelFog)
804 _mesa_fog_rgba_pixels_with_array(ctx, span, span->fogArray,
805 span->color.rgba);
806 else if ((span->interpMask & SPAN_FOG) && !swrast->_PreferPixelFog)
807 _mesa_fog_rgba_pixels(ctx, span, span->color.rgba);
808 else {
809 if ((span->interpMask & SPAN_Z) && (span->arrayMask & SPAN_Z) == 0)
810 interpolate_z(ctx, span);
811 _mesa_depth_fog_rgba_pixels(ctx, span, span->color.rgba);
812 }
813 monoColor = GL_FALSE;
814 }
815
816 /* Antialias coverage application */
817 if (span->arrayMask & SPAN_COVERAGE) {
818 GLchan (*rgba)[4] = span->color.rgba;
819 GLuint i;
820 for (i = 0; i < span->end; i++) {
821 rgba[i][ACOMP] = (GLchan) (rgba[i][ACOMP] * span->coverage[i]);
822 }
823 monoColor = GL_FALSE;
824 }
825
826 if (swrast->_RasterMask & MULTI_DRAW_BIT) {
827 multi_write_rgba_span( ctx, span->end, span->x, span->y,
828 (const GLchan (*)[4]) span->color.rgba,
829 span->mask );
830 }
831 else {
832 /* normal: write to exactly one buffer */
833 if (ctx->Color.ColorLogicOpEnabled) {
834 _mesa_logicop_rgba_span( ctx, span->end, span->x, span->y,
835 span->color.rgba, span->mask );
836 monoColor = GL_FALSE;
837 }
838 else if (ctx->Color.BlendEnabled) {
839 _mesa_blend_span( ctx, span->end, span->x, span->y,
840 span->color.rgba, span->mask );
841 monoColor = GL_FALSE;
842 }
843
844 /* Color component masking */
845 if (colorMask != 0xffffffff) {
846 _mesa_mask_rgba_span( ctx, span->end, span->x, span->y,
847 span->color.rgba );
848 monoColor = GL_FALSE;
849 }
850
851 /* write pixels */
852 if (monoColor) {
853 /* all pixels have same color */
854 GLchan color[4];
855 color[RCOMP] = FixedToChan(span->red);
856 color[GCOMP] = FixedToChan(span->green);
857 color[BCOMP] = FixedToChan(span->blue);
858 color[ACOMP] = FixedToChan(span->alpha);
859 (*swrast->Driver.WriteMonoRGBASpan)( ctx, span->end, span->x, span->y,
860 color, span->mask);
861 }
862 else {
863 (*swrast->Driver.WriteRGBASpan)( ctx, span->end, span->x, span->y,
864 (const GLchan (*)[4]) span->color.rgba,
865 span->writeAll ? ((const GLubyte *) NULL) : span->mask );
866 }
867
868 if (swrast->_RasterMask & ALPHABUF_BIT) {
869 _mesa_write_alpha_span( ctx, span->end, span->x, span->y,
870 (const GLchan (*)[4]) span->color.rgba,
871 span->writeAll ? ((const GLubyte *) NULL) : span->mask );
872 }
873 }
874
875 span->interpMask = origInterpMask;
876 span->arrayMask = origArrayMask;
877 }
878
879
880 /*
881 * Add specular color to base color. This is used only when
882 * GL_LIGHT_MODEL_COLOR_CONTROL = GL_SEPARATE_SPECULAR_COLOR.
883 */
884 static void
885 add_colors(GLuint n, GLchan rgba[][4], GLchan specular[][4] )
886 {
887 GLuint i;
888 for (i = 0; i < n; i++) {
889 #if CHAN_TYPE == GL_FLOAT
890 /* no clamping */
891 rgba[i][RCOMP] += specular[i][RCOMP];
892 rgba[i][GCOMP] += specular[i][GCOMP];
893 rgba[i][BCOMP] += specular[i][BCOMP];
894 #else
895 GLint r = rgba[i][RCOMP] + specular[i][RCOMP];
896 GLint g = rgba[i][GCOMP] + specular[i][GCOMP];
897 GLint b = rgba[i][BCOMP] + specular[i][BCOMP];
898 rgba[i][RCOMP] = (GLchan) MIN2(r, CHAN_MAX);
899 rgba[i][GCOMP] = (GLchan) MIN2(g, CHAN_MAX);
900 rgba[i][BCOMP] = (GLchan) MIN2(b, CHAN_MAX);
901 #endif
902 }
903 }
904
905
906 /*
907 * This function may modify any of the array values in the span.
908 * span->interpMask and span->arrayMask may be changed but will be restored
909 * to their original values before returning.
910 */
911 void
912 _mesa_write_texture_span( GLcontext *ctx, struct sw_span *span,
913 GLenum primitive )
914 {
915 const GLuint colorMask = *((GLuint *) ctx->Color.ColorMask);
916 SWcontext *swrast = SWRAST_CONTEXT(ctx);
917 const GLuint origArrayMask = span->arrayMask;
918
919 ASSERT((span->interpMask & span->arrayMask) == 0);
920
921 /* printf("%s() interp 0x%x array 0x%x\n", __FUNCTION__, span->interpMask, span->arrayMask);*/
922
923 ASSERT(ctx->Texture._ReallyEnabled);
924
925 MEMSET(span->mask, 1, span->end);
926 span->writeAll = GL_TRUE;
927
928 /* clip against window bounds */
929 if ((swrast->_RasterMask & WINCLIP_BIT) || primitive==GL_BITMAP) {
930 if (clip_span(ctx,span) == GL_FALSE) {
931 return;
932 }
933 }
934
935 /* Scissor test */
936 if (ctx->Scissor.Enabled) {
937 if (_mesa_scissor_span( ctx, span ) == GL_FALSE) {
938 return;
939 }
940 }
941
942 /* Polygon Stippling */
943 if (ctx->Polygon.StippleFlag && primitive==GL_POLYGON) {
944 stipple_polygon_span( ctx, span);
945 }
946
947 /* Need texture coordinates now */
948 if ((span->interpMask & SPAN_TEXTURE)
949 && (span->arrayMask & SPAN_TEXTURE) == 0)
950 interpolate_texcoords(ctx, span);
951
952 /* Texture with alpha test */
953 if (ctx->Color.AlphaEnabled) {
954
955 /* Now we need the rgba array, fill it in if needed */
956 if ((span->interpMask & SPAN_RGBA) && (span->arrayMask & SPAN_RGBA) == 0)
957 interpolate_colors(ctx, span);
958
959 /* Texturing without alpha is done after depth-testing which
960 * gives a potential speed-up.
961 */
962 _swrast_multitexture_fragments( ctx, span );
963
964 /* Do the alpha test */
965 if (!_old_alpha_test(ctx, span->end,
966 (CONST GLchan (*)[4]) span->color.rgba,
967 span->mask)) {
968 span->arrayMask = origArrayMask;
969 return;
970 }
971 }
972
973 /* Stencil and Z testing */
974 if (ctx->Stencil.Enabled || ctx->Depth.Test) {
975 if (span->interpMask & SPAN_Z)
976 interpolate_z(ctx, span);
977
978 if (ctx->Stencil.Enabled) {
979 if (!_mesa_stencil_and_ztest_span(ctx, span)) {
980 span->arrayMask = origArrayMask;
981 return;
982 }
983 }
984 else {
985 ASSERT(ctx->Depth.Test);
986 ASSERT(span->arrayMask & SPAN_Z);
987 /* regular depth testing */
988 if (!_mesa_depth_test_span(ctx, span)) {
989 span->arrayMask = origArrayMask;
990 return;
991 }
992 }
993 }
994
995 /* if we get here, some fragments passed the depth test */
996 ctx->OcclusionResult = GL_TRUE;
997
998 /* We had to wait until now to check for glColorMask(F,F,F,F) because of
999 * the occlusion test.
1000 */
1001 if (colorMask == 0x0) {
1002 span->arrayMask = origArrayMask;
1003 return;
1004 }
1005
1006 /* Texture without alpha test */
1007 if (!ctx->Color.AlphaEnabled) {
1008
1009 /* Now we need the rgba array, fill it in if needed */
1010 if ((span->interpMask & SPAN_RGBA) && (span->arrayMask & SPAN_RGBA) == 0)
1011 interpolate_colors(ctx, span);
1012
1013 _swrast_multitexture_fragments( ctx, span );
1014 }
1015
1016 ASSERT(span->arrayMask & SPAN_RGBA);
1017
1018 /* Add base and specular colors */
1019 if (ctx->Fog.ColorSumEnabled ||
1020 (ctx->Light.Enabled &&
1021 ctx->Light.Model.ColorControl == GL_SEPARATE_SPECULAR_COLOR)) {
1022 if (span->interpMask & SPAN_SPEC) {
1023 interpolate_specular(ctx, span);
1024 }
1025 ASSERT(span->arrayMask & SPAN_SPEC);
1026 add_colors( span->end, span->color.rgba, span->specArray );
1027 }
1028
1029 /* Per-pixel fog */
1030 if (ctx->Fog.Enabled) {
1031 #if 0
1032 if ((span->interpMask & SPAN_FOG) && (span->arrayMask & SPAN_FOG) == 0)
1033 interpolate_fog(ctx, span);
1034 #endif
1035 if ((span->arrayMask & SPAN_FOG) && !swrast->_PreferPixelFog)
1036 _mesa_fog_rgba_pixels_with_array( ctx, span, span->fogArray,
1037 span->color.rgba);
1038 else if ((span->interpMask & SPAN_FOG) && !swrast->_PreferPixelFog)
1039 _mesa_fog_rgba_pixels( ctx, span, span->color.rgba );
1040 else {
1041 if ((span->interpMask & SPAN_Z) && (span->arrayMask & SPAN_Z) == 0)
1042 interpolate_z(ctx, span);
1043 _mesa_depth_fog_rgba_pixels(ctx, span, span->color.rgba);
1044 }
1045 }
1046
1047 /* Antialias coverage application */
1048 if (span->arrayMask & SPAN_COVERAGE) {
1049 GLchan (*rgba)[4] = span->color.rgba;
1050 GLuint i;
1051 for (i = 0; i < span->end; i++) {
1052 rgba[i][ACOMP] = (GLchan) (rgba[i][ACOMP] * span->coverage[i]);
1053 }
1054 }
1055
1056 if (swrast->_RasterMask & MULTI_DRAW_BIT) {
1057 multi_write_rgba_span( ctx, span->end, span->x, span->y,
1058 (const GLchan (*)[4]) span->color.rgba,
1059 span->mask );
1060 }
1061 else {
1062 /* normal: write to exactly one buffer */
1063 if (ctx->Color.ColorLogicOpEnabled) {
1064 _mesa_logicop_rgba_span( ctx, span->end, span->x, span->y,
1065 span->color.rgba, span->mask );
1066 }
1067 else if (ctx->Color.BlendEnabled) {
1068 _mesa_blend_span( ctx, span->end, span->x, span->y,
1069 span->color.rgba, span->mask);
1070 }
1071
1072 if (colorMask != 0xffffffff) {
1073 _mesa_mask_rgba_span( ctx, span->end, span->x, span->y,
1074 span->color.rgba );
1075 }
1076
1077 (*swrast->Driver.WriteRGBASpan)( ctx, span->end, span->x, span->y,
1078 (const GLchan (*)[4]) span->color.rgba,
1079 span->writeAll ? NULL : span->mask );
1080 if (swrast->_RasterMask & ALPHABUF_BIT) {
1081 _mesa_write_alpha_span( ctx, span->end, span->x, span->y,
1082 (const GLchan (*)[4]) span->color.rgba,
1083 span->writeAll ? NULL : span->mask );
1084 }
1085 }
1086
1087 span->arrayMask = origArrayMask;
1088 }
1089
1090
1091
1092 /*
1093 * Read RGBA pixels from frame buffer. Clipping will be done to prevent
1094 * reading ouside the buffer's boundaries.
1095 */
1096 void
1097 _mesa_read_rgba_span( GLcontext *ctx, GLframebuffer *buffer,
1098 GLuint n, GLint x, GLint y, GLchan rgba[][4] )
1099 {
1100 SWcontext *swrast = SWRAST_CONTEXT(ctx);
1101 if (y < 0 || y >= buffer->Height
1102 || x + (GLint) n < 0 || x >= buffer->Width) {
1103 /* completely above, below, or right */
1104 /* XXX maybe leave undefined? */
1105 BZERO(rgba, 4 * n * sizeof(GLchan));
1106 }
1107 else {
1108 GLint skip, length;
1109 if (x < 0) {
1110 /* left edge clippping */
1111 skip = -x;
1112 length = (GLint) n - skip;
1113 if (length < 0) {
1114 /* completely left of window */
1115 return;
1116 }
1117 if (length > buffer->Width) {
1118 length = buffer->Width;
1119 }
1120 }
1121 else if ((GLint) (x + n) > buffer->Width) {
1122 /* right edge clipping */
1123 skip = 0;
1124 length = buffer->Width - x;
1125 if (length < 0) {
1126 /* completely to right of window */
1127 return;
1128 }
1129 }
1130 else {
1131 /* no clipping */
1132 skip = 0;
1133 length = (GLint) n;
1134 }
1135
1136 (*swrast->Driver.ReadRGBASpan)( ctx, length, x + skip, y, rgba + skip );
1137 if (buffer->UseSoftwareAlphaBuffers) {
1138 _mesa_read_alpha_span( ctx, length, x + skip, y, rgba + skip );
1139 }
1140 }
1141 }
1142
1143
1144
1145
1146 /*
1147 * Read CI pixels from frame buffer. Clipping will be done to prevent
1148 * reading ouside the buffer's boundaries.
1149 */
1150 void
1151 _mesa_read_index_span( GLcontext *ctx, GLframebuffer *buffer,
1152 GLuint n, GLint x, GLint y, GLuint indx[] )
1153 {
1154 SWcontext *swrast = SWRAST_CONTEXT(ctx);
1155 if (y < 0 || y >= buffer->Height
1156 || x + (GLint) n < 0 || x >= buffer->Width) {
1157 /* completely above, below, or right */
1158 BZERO(indx, n * sizeof(GLuint));
1159 }
1160 else {
1161 GLint skip, length;
1162 if (x < 0) {
1163 /* left edge clippping */
1164 skip = -x;
1165 length = (GLint) n - skip;
1166 if (length < 0) {
1167 /* completely left of window */
1168 return;
1169 }
1170 if (length > buffer->Width) {
1171 length = buffer->Width;
1172 }
1173 }
1174 else if ((GLint) (x + n) > buffer->Width) {
1175 /* right edge clipping */
1176 skip = 0;
1177 length = buffer->Width - x;
1178 if (length < 0) {
1179 /* completely to right of window */
1180 return;
1181 }
1182 }
1183 else {
1184 /* no clipping */
1185 skip = 0;
1186 length = (GLint) n;
1187 }
1188
1189 (*swrast->Driver.ReadCI32Span)( ctx, length, skip + x, y, indx + skip );
1190 }
1191 }