f709494a7e285a8188b8745adf4d4169a6c34079
[mesa.git] / src / mesa / swrast / s_span.c
1 /* $Id: s_span.c,v 1.27 2002/01/28 04:25:56 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 /* XXX try to simplify the fog code! */
652 if (ctx->Fog.Enabled) {
653 if ((span->arrayMask & SPAN_FOG) && !swrast->_PreferPixelFog)
654 _mesa_fog_ci_pixels_with_array( ctx, span, span->fogArray,
655 span->color.index);
656 else if ((span->interpMask & SPAN_FOG) && !swrast->_PreferPixelFog)
657 _mesa_fog_ci_pixels( ctx, span, span->color.index);
658 else
659 _mesa_depth_fog_ci_pixels( ctx, span, span->color.index);
660 }
661
662 /* Antialias coverage application */
663 if (span->arrayMask & SPAN_COVERAGE) {
664 GLuint i;
665 GLuint *index = span->color.index;
666 for (i = 0; i < span->end; i++) {
667 ASSERT(span->coverage[i] < 16);
668 index[i] = (index[i] & ~0xf) | ((GLuint) (span->coverage[i]));
669 }
670 }
671
672 if (swrast->_RasterMask & MULTI_DRAW_BIT) {
673 /* draw to zero or two or more buffers */
674 multi_write_index_span( ctx, span->end, span->x, span->y,
675 span->color.index, span->mask );
676 }
677 else {
678 /* normal situation: draw to exactly one buffer */
679 if (ctx->Color.IndexLogicOpEnabled) {
680 _mesa_logicop_ci_span( ctx, span->end, span->x, span->y,
681 span->color.index, span->mask );
682 }
683
684 if (ctx->Color.IndexMask != 0xffffffff) {
685 _mesa_mask_index_span( ctx, span->end, span->x, span->y,
686 span->color.index );
687 }
688
689 /* write pixels */
690 if ((span->interpMask & SPAN_INDEX) && span->indexStep == 0) {
691 /* all pixels have same color index */
692 (*swrast->Driver.WriteMonoCISpan)( ctx, span->end, span->x, span->y,
693 FixedToInt(span->index),
694 span->mask );
695 }
696 else {
697 (*swrast->Driver.WriteCI32Span)( ctx, span->end, span->x, span->y,
698 span->color.index, span->mask );
699 }
700 }
701
702 span->interpMask = origInterpMask;
703 span->arrayMask = origArrayMask;
704 }
705
706
707 /*
708 * This function may modify any of the array values in the span.
709 * span->interpMask and span->arrayMask may be changed but will be restored
710 * to their original values before returning.
711 */
712 void
713 _mesa_write_rgba_span( GLcontext *ctx, struct sw_span *span,
714 GLenum primitive)
715 {
716 SWcontext *swrast = SWRAST_CONTEXT(ctx);
717 const GLuint colorMask = *((GLuint *) ctx->Color.ColorMask);
718 const GLuint origInterpMask = span->interpMask;
719 const GLuint origArrayMask = span->arrayMask;
720 GLboolean monoColor;
721
722 ASSERT((span->interpMask & span->arrayMask) == 0);
723
724 MEMSET(span->mask, 1, span->end);
725 span->writeAll = GL_TRUE;
726
727 /* Determine if we have mono-chromatic colors */
728 monoColor = (span->interpMask & SPAN_RGBA) &&
729 span->redStep == 0 && span->greenStep == 0 &&
730 span->blueStep == 0 && span->alphaStep == 0;
731
732 /* Window clipping */
733 if ((swrast->_RasterMask & WINCLIP_BIT) || primitive == GL_BITMAP) {
734 if (clip_span(ctx, span) == GL_FALSE) {
735 return;
736 }
737 }
738
739 /* Scissor test */
740 if (ctx->Scissor.Enabled) {
741 if (!_mesa_scissor_span(ctx, span)) {
742 return;
743 }
744 }
745
746 /* Polygon Stippling */
747 if (ctx->Polygon.StippleFlag && primitive == GL_POLYGON) {
748 stipple_polygon_span(ctx, span);
749 }
750
751 /* Now we may need to interpolate the colors */
752 if ((span->interpMask & SPAN_RGBA) && (span->arrayMask & SPAN_RGBA) == 0) {
753 interpolate_colors(ctx, span);
754 /* clear the bit - this allows the WriteMonoCISpan optimization below */
755 span->interpMask &= ~SPAN_RGBA;
756 }
757
758 /* Do the alpha test */
759 if (ctx->Color.AlphaEnabled) {
760 if (!_mesa_alpha_test(ctx, span,
761 (const GLchan (*)[4]) span->color.rgba)) {
762 span->interpMask = origInterpMask;
763 span->arrayMask = origArrayMask;
764 return;
765 }
766 }
767
768 /* Stencil and Z testing */
769 if (ctx->Stencil.Enabled || ctx->Depth.Test) {
770 if (span->interpMask & SPAN_Z)
771 interpolate_z(ctx, span);
772
773 if (ctx->Stencil.Enabled) {
774 if (!_mesa_stencil_and_ztest_span(ctx, span)) {
775 span->interpMask = origInterpMask;
776 span->arrayMask = origArrayMask;
777 return;
778 }
779 }
780 else {
781 ASSERT(ctx->Depth.Test);
782 ASSERT(span->arrayMask & SPAN_Z);
783 /* regular depth testing */
784 if (!_mesa_depth_test_span(ctx, span)) {
785 span->interpMask = origInterpMask;
786 span->arrayMask = origArrayMask;
787 return;
788 }
789 }
790 }
791
792 /* if we get here, something passed the depth test */
793 ctx->OcclusionResult = GL_TRUE;
794
795 /* can't abort span-writing until after occlusion testing */
796 if (colorMask == 0x0) {
797 span->interpMask = origInterpMask;
798 span->arrayMask = origArrayMask;
799 return;
800 }
801
802 /* Fog */
803 /* XXX try to simplify the fog code! */
804 if (ctx->Fog.Enabled) {
805 if ((span->arrayMask & SPAN_FOG) && !swrast->_PreferPixelFog)
806 _mesa_fog_rgba_pixels_with_array(ctx, span, span->fogArray,
807 span->color.rgba);
808 else if ((span->interpMask & SPAN_FOG) && !swrast->_PreferPixelFog)
809 _mesa_fog_rgba_pixels(ctx, span, span->color.rgba);
810 else {
811 if ((span->interpMask & SPAN_Z) && (span->arrayMask & SPAN_Z) == 0)
812 interpolate_z(ctx, span);
813 _mesa_depth_fog_rgba_pixels(ctx, span, span->color.rgba);
814 }
815 monoColor = GL_FALSE;
816 }
817
818 /* Antialias coverage application */
819 if (span->arrayMask & SPAN_COVERAGE) {
820 GLchan (*rgba)[4] = span->color.rgba;
821 GLuint i;
822 for (i = 0; i < span->end; i++) {
823 rgba[i][ACOMP] = (GLchan) (rgba[i][ACOMP] * span->coverage[i]);
824 }
825 monoColor = GL_FALSE;
826 }
827
828 if (swrast->_RasterMask & MULTI_DRAW_BIT) {
829 multi_write_rgba_span( ctx, span->end, span->x, span->y,
830 (const GLchan (*)[4]) span->color.rgba,
831 span->mask );
832 }
833 else {
834 /* normal: write to exactly one buffer */
835 if (ctx->Color.ColorLogicOpEnabled) {
836 _mesa_logicop_rgba_span( ctx, span->end, span->x, span->y,
837 span->color.rgba, span->mask );
838 monoColor = GL_FALSE;
839 }
840 else if (ctx->Color.BlendEnabled) {
841 _mesa_blend_span( ctx, span->end, span->x, span->y,
842 span->color.rgba, span->mask );
843 monoColor = GL_FALSE;
844 }
845
846 /* Color component masking */
847 if (colorMask != 0xffffffff) {
848 _mesa_mask_rgba_span( ctx, span->end, span->x, span->y,
849 span->color.rgba );
850 monoColor = GL_FALSE;
851 }
852
853 /* write pixels */
854 if (monoColor) {
855 /* all pixels have same color */
856 GLchan color[4];
857 color[RCOMP] = FixedToChan(span->red);
858 color[GCOMP] = FixedToChan(span->green);
859 color[BCOMP] = FixedToChan(span->blue);
860 color[ACOMP] = FixedToChan(span->alpha);
861 (*swrast->Driver.WriteMonoRGBASpan)( ctx, span->end, span->x, span->y,
862 color, span->mask);
863 }
864 else {
865 (*swrast->Driver.WriteRGBASpan)( ctx, span->end, span->x, span->y,
866 (const GLchan (*)[4]) span->color.rgba,
867 span->writeAll ? ((const GLubyte *) NULL) : span->mask );
868 }
869
870 if (swrast->_RasterMask & ALPHABUF_BIT) {
871 _mesa_write_alpha_span( ctx, span->end, span->x, span->y,
872 (const GLchan (*)[4]) span->color.rgba,
873 span->writeAll ? ((const GLubyte *) NULL) : span->mask );
874 }
875 }
876
877 span->interpMask = origInterpMask;
878 span->arrayMask = origArrayMask;
879 }
880
881
882 /*
883 * Add specular color to base color. This is used only when
884 * GL_LIGHT_MODEL_COLOR_CONTROL = GL_SEPARATE_SPECULAR_COLOR.
885 */
886 static void
887 add_colors(GLuint n, GLchan rgba[][4], GLchan specular[][4] )
888 {
889 GLuint i;
890 for (i = 0; i < n; i++) {
891 #if CHAN_TYPE == GL_FLOAT
892 /* no clamping */
893 rgba[i][RCOMP] += specular[i][RCOMP];
894 rgba[i][GCOMP] += specular[i][GCOMP];
895 rgba[i][BCOMP] += specular[i][BCOMP];
896 #else
897 GLint r = rgba[i][RCOMP] + specular[i][RCOMP];
898 GLint g = rgba[i][GCOMP] + specular[i][GCOMP];
899 GLint b = rgba[i][BCOMP] + specular[i][BCOMP];
900 rgba[i][RCOMP] = (GLchan) MIN2(r, CHAN_MAX);
901 rgba[i][GCOMP] = (GLchan) MIN2(g, CHAN_MAX);
902 rgba[i][BCOMP] = (GLchan) MIN2(b, CHAN_MAX);
903 #endif
904 }
905 }
906
907
908 /*
909 * This function may modify any of the array values in the span.
910 * span->interpMask and span->arrayMask may be changed but will be restored
911 * to their original values before returning.
912 */
913 void
914 _mesa_write_texture_span( GLcontext *ctx, struct sw_span *span,
915 GLenum primitive )
916 {
917 const GLuint colorMask = *((GLuint *) ctx->Color.ColorMask);
918 SWcontext *swrast = SWRAST_CONTEXT(ctx);
919 const GLuint origArrayMask = span->arrayMask;
920
921 ASSERT((span->interpMask & span->arrayMask) == 0);
922
923 /* printf("%s() interp 0x%x array 0x%x\n", __FUNCTION__, span->interpMask, span->arrayMask);*/
924
925 ASSERT(ctx->Texture._ReallyEnabled);
926
927 MEMSET(span->mask, 1, span->end);
928 span->writeAll = GL_TRUE;
929
930 /* clip against window bounds */
931 if ((swrast->_RasterMask & WINCLIP_BIT) || primitive==GL_BITMAP) {
932 if (clip_span(ctx,span) == GL_FALSE) {
933 return;
934 }
935 }
936
937 /* Scissor test */
938 if (ctx->Scissor.Enabled) {
939 if (_mesa_scissor_span( ctx, span ) == GL_FALSE) {
940 return;
941 }
942 }
943
944 /* Polygon Stippling */
945 if (ctx->Polygon.StippleFlag && primitive==GL_POLYGON) {
946 stipple_polygon_span( ctx, span);
947 }
948
949 /* Need texture coordinates now */
950 if ((span->interpMask & SPAN_TEXTURE)
951 && (span->arrayMask & SPAN_TEXTURE) == 0)
952 interpolate_texcoords(ctx, span);
953
954 /* Texture with alpha test */
955 if (ctx->Color.AlphaEnabled) {
956
957 /* Now we need the rgba array, fill it in if needed */
958 if ((span->interpMask & SPAN_RGBA) && (span->arrayMask & SPAN_RGBA) == 0)
959 interpolate_colors(ctx, span);
960
961 /* Texturing without alpha is done after depth-testing which
962 * gives a potential speed-up.
963 */
964 _swrast_multitexture_fragments( ctx, span );
965
966 /* Do the alpha test */
967 if (!_old_alpha_test(ctx, span->end,
968 (CONST GLchan (*)[4]) span->color.rgba,
969 span->mask)) {
970 span->arrayMask = origArrayMask;
971 return;
972 }
973 }
974
975 /* Stencil and Z testing */
976 if (ctx->Stencil.Enabled || ctx->Depth.Test) {
977 if (span->interpMask & SPAN_Z)
978 interpolate_z(ctx, span);
979
980 if (ctx->Stencil.Enabled) {
981 if (!_mesa_stencil_and_ztest_span(ctx, span)) {
982 span->arrayMask = origArrayMask;
983 return;
984 }
985 }
986 else {
987 ASSERT(ctx->Depth.Test);
988 ASSERT(span->arrayMask & SPAN_Z);
989 /* regular depth testing */
990 if (!_mesa_depth_test_span(ctx, span)) {
991 span->arrayMask = origArrayMask;
992 return;
993 }
994 }
995 }
996
997 /* if we get here, some fragments passed the depth test */
998 ctx->OcclusionResult = GL_TRUE;
999
1000 /* We had to wait until now to check for glColorMask(F,F,F,F) because of
1001 * the occlusion test.
1002 */
1003 if (colorMask == 0x0) {
1004 span->arrayMask = origArrayMask;
1005 return;
1006 }
1007
1008 /* Texture without alpha test */
1009 if (!ctx->Color.AlphaEnabled) {
1010
1011 /* Now we need the rgba array, fill it in if needed */
1012 if ((span->interpMask & SPAN_RGBA) && (span->arrayMask & SPAN_RGBA) == 0)
1013 interpolate_colors(ctx, span);
1014
1015 _swrast_multitexture_fragments( ctx, span );
1016 }
1017
1018 ASSERT(span->arrayMask & SPAN_RGBA);
1019
1020 /* Add base and specular colors */
1021 if (ctx->Fog.ColorSumEnabled ||
1022 (ctx->Light.Enabled &&
1023 ctx->Light.Model.ColorControl == GL_SEPARATE_SPECULAR_COLOR)) {
1024 if (span->interpMask & SPAN_SPEC) {
1025 interpolate_specular(ctx, span);
1026 }
1027 ASSERT(span->arrayMask & SPAN_SPEC);
1028 add_colors( span->end, span->color.rgba, span->specArray );
1029 }
1030
1031 /* Fog */
1032 /* XXX try to simplify the fog code! */
1033 if (ctx->Fog.Enabled) {
1034 if ((span->arrayMask & SPAN_FOG) && !swrast->_PreferPixelFog)
1035 _mesa_fog_rgba_pixels_with_array( ctx, span, span->fogArray,
1036 span->color.rgba);
1037 else if ((span->interpMask & SPAN_FOG) && !swrast->_PreferPixelFog)
1038 _mesa_fog_rgba_pixels( ctx, span, span->color.rgba );
1039 else {
1040 if ((span->interpMask & SPAN_Z) && (span->arrayMask & SPAN_Z) == 0)
1041 interpolate_z(ctx, span);
1042 _mesa_depth_fog_rgba_pixels(ctx, span, span->color.rgba);
1043 }
1044 }
1045
1046 /* Antialias coverage application */
1047 if (span->arrayMask & SPAN_COVERAGE) {
1048 GLchan (*rgba)[4] = span->color.rgba;
1049 GLuint i;
1050 for (i = 0; i < span->end; i++) {
1051 rgba[i][ACOMP] = (GLchan) (rgba[i][ACOMP] * span->coverage[i]);
1052 }
1053 }
1054
1055 if (swrast->_RasterMask & MULTI_DRAW_BIT) {
1056 multi_write_rgba_span( ctx, span->end, span->x, span->y,
1057 (const GLchan (*)[4]) span->color.rgba,
1058 span->mask );
1059 }
1060 else {
1061 /* normal: write to exactly one buffer */
1062 if (ctx->Color.ColorLogicOpEnabled) {
1063 _mesa_logicop_rgba_span( ctx, span->end, span->x, span->y,
1064 span->color.rgba, span->mask );
1065 }
1066 else if (ctx->Color.BlendEnabled) {
1067 _mesa_blend_span( ctx, span->end, span->x, span->y,
1068 span->color.rgba, span->mask);
1069 }
1070
1071 if (colorMask != 0xffffffff) {
1072 _mesa_mask_rgba_span( ctx, span->end, span->x, span->y,
1073 span->color.rgba );
1074 }
1075
1076 (*swrast->Driver.WriteRGBASpan)( ctx, span->end, span->x, span->y,
1077 (const GLchan (*)[4]) span->color.rgba,
1078 span->writeAll ? NULL : span->mask );
1079 if (swrast->_RasterMask & ALPHABUF_BIT) {
1080 _mesa_write_alpha_span( ctx, span->end, span->x, span->y,
1081 (const GLchan (*)[4]) span->color.rgba,
1082 span->writeAll ? NULL : span->mask );
1083 }
1084 }
1085
1086 span->arrayMask = origArrayMask;
1087 }
1088
1089
1090
1091 /*
1092 * Read RGBA pixels from frame buffer. Clipping will be done to prevent
1093 * reading ouside the buffer's boundaries.
1094 */
1095 void
1096 _mesa_read_rgba_span( GLcontext *ctx, GLframebuffer *buffer,
1097 GLuint n, GLint x, GLint y, GLchan rgba[][4] )
1098 {
1099 SWcontext *swrast = SWRAST_CONTEXT(ctx);
1100 if (y < 0 || y >= buffer->Height
1101 || x + (GLint) n < 0 || x >= buffer->Width) {
1102 /* completely above, below, or right */
1103 /* XXX maybe leave undefined? */
1104 BZERO(rgba, 4 * n * sizeof(GLchan));
1105 }
1106 else {
1107 GLint skip, length;
1108 if (x < 0) {
1109 /* left edge clippping */
1110 skip = -x;
1111 length = (GLint) n - skip;
1112 if (length < 0) {
1113 /* completely left of window */
1114 return;
1115 }
1116 if (length > buffer->Width) {
1117 length = buffer->Width;
1118 }
1119 }
1120 else if ((GLint) (x + n) > buffer->Width) {
1121 /* right edge clipping */
1122 skip = 0;
1123 length = buffer->Width - x;
1124 if (length < 0) {
1125 /* completely to right of window */
1126 return;
1127 }
1128 }
1129 else {
1130 /* no clipping */
1131 skip = 0;
1132 length = (GLint) n;
1133 }
1134
1135 (*swrast->Driver.ReadRGBASpan)( ctx, length, x + skip, y, rgba + skip );
1136 if (buffer->UseSoftwareAlphaBuffers) {
1137 _mesa_read_alpha_span( ctx, length, x + skip, y, rgba + skip );
1138 }
1139 }
1140 }
1141
1142
1143
1144
1145 /*
1146 * Read CI pixels from frame buffer. Clipping will be done to prevent
1147 * reading ouside the buffer's boundaries.
1148 */
1149 void
1150 _mesa_read_index_span( GLcontext *ctx, GLframebuffer *buffer,
1151 GLuint n, GLint x, GLint y, GLuint indx[] )
1152 {
1153 SWcontext *swrast = SWRAST_CONTEXT(ctx);
1154 if (y < 0 || y >= buffer->Height
1155 || x + (GLint) n < 0 || x >= buffer->Width) {
1156 /* completely above, below, or right */
1157 BZERO(indx, n * sizeof(GLuint));
1158 }
1159 else {
1160 GLint skip, length;
1161 if (x < 0) {
1162 /* left edge clippping */
1163 skip = -x;
1164 length = (GLint) n - skip;
1165 if (length < 0) {
1166 /* completely left of window */
1167 return;
1168 }
1169 if (length > buffer->Width) {
1170 length = buffer->Width;
1171 }
1172 }
1173 else if ((GLint) (x + n) > buffer->Width) {
1174 /* right edge clipping */
1175 skip = 0;
1176 length = buffer->Width - x;
1177 if (length < 0) {
1178 /* completely to right of window */
1179 return;
1180 }
1181 }
1182 else {
1183 /* no clipping */
1184 skip = 0;
1185 length = (GLint) n;
1186 }
1187
1188 (*swrast->Driver.ReadCI32Span)( ctx, length, skip + x, y, indx + skip );
1189 }
1190 }