1 /* $Id: s_span.c,v 1.5 2000/12/12 00:27:51 brianp Exp $ */
4 * Mesa 3-D graphics library
7 * Copyright (C) 1999-2000 Brian Paul All Rights Reserved.
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:
16 * The above copyright notice and this permission notice shall be included
17 * in all copies or substantial portions of the Software.
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.
29 * pixel span rasterization:
30 * These functions implement the rasterization pipeline.
40 #include "s_alphabuf.h"
42 #include "s_context.h"
46 #include "s_masking.h"
47 #include "s_scissor.h"
49 #include "s_stencil.h"
50 #include "s_texture.h"
56 * Apply the current polygon stipple pattern to a span of pixels.
58 static void stipple_polygon_span( GLcontext
*ctx
,
59 GLuint n
, GLint x
, GLint y
, GLubyte mask
[] )
61 const GLuint highbit
= 0x80000000;
64 stipple
= ctx
->PolygonStipple
[y
% 32];
65 m
= highbit
>> (GLuint
) (x
% 32);
67 for (i
= 0; i
< n
; i
++) {
68 if ((m
& stipple
) == 0) {
81 * Clip a pixel span to the current buffer/window boundaries.
82 * Return: 'n' such that pixel 'n', 'n+1' etc. are clipped,
84 * 0 = all pixels clipped
86 static GLuint
clip_span( GLcontext
*ctx
,
87 GLint n
, GLint x
, GLint y
, GLubyte mask
[] )
89 /* Clip to top and bottom */
90 if (y
< 0 || y
>= ctx
->DrawBuffer
->Height
) {
94 /* Clip to the left */
97 /* completely off left side */
101 /* partially off left side */
107 if (x
+ n
> ctx
->DrawBuffer
->Width
) {
108 if (x
>= ctx
->DrawBuffer
->Width
) {
109 /* completely off right side */
113 /* partially off right side */
114 return ctx
->DrawBuffer
->Width
- x
;
124 * Draw to more than one color buffer (or none).
126 static void multi_write_index_span( GLcontext
*ctx
, GLuint n
,
127 GLint x
, GLint y
, const GLuint indexes
[],
128 const GLubyte mask
[] )
132 if (ctx
->Color
.DrawBuffer
== GL_NONE
)
135 /* loop over four possible dest color buffers */
136 for (bufferBit
= 1; bufferBit
<= 8; bufferBit
= bufferBit
<< 1) {
137 if (bufferBit
& ctx
->Color
.DrawDestMask
) {
138 GLuint indexTmp
[MAX_WIDTH
];
139 ASSERT(n
< MAX_WIDTH
);
141 if (bufferBit
== FRONT_LEFT_BIT
)
142 (void) (*ctx
->Driver
.SetDrawBuffer
)( ctx
, GL_FRONT_LEFT
);
143 else if (bufferBit
== FRONT_RIGHT_BIT
)
144 (void) (*ctx
->Driver
.SetDrawBuffer
)( ctx
, GL_FRONT_RIGHT
);
145 else if (bufferBit
== BACK_LEFT_BIT
)
146 (void) (*ctx
->Driver
.SetDrawBuffer
)( ctx
, GL_BACK_LEFT
);
148 (void) (*ctx
->Driver
.SetDrawBuffer
)( ctx
, GL_BACK_RIGHT
);
150 /* make copy of incoming indexes */
151 MEMCPY( indexTmp
, indexes
, n
* sizeof(GLuint
) );
152 if (ctx
->Color
.IndexLogicOpEnabled
) {
153 _mesa_logicop_ci_span( ctx
, n
, x
, y
, indexTmp
, mask
);
155 if (ctx
->Color
.IndexMask
== 0) {
158 else if (ctx
->Color
.IndexMask
!= 0xffffffff) {
159 _mesa_mask_index_span( ctx
, n
, x
, y
, indexTmp
);
161 (*ctx
->Driver
.WriteCI32Span
)( ctx
, n
, x
, y
, indexTmp
, mask
);
165 /* restore default dest buffer */
166 (void) (*ctx
->Driver
.SetDrawBuffer
)( ctx
, ctx
->Color
.DriverDrawBuffer
);
172 * Write a horizontal span of color index pixels to the frame buffer.
173 * Stenciling, Depth-testing, etc. are done as needed.
174 * Input: n - number of pixels in the span
175 * x, y - location of leftmost pixel in the span
176 * z - array of [n] z-values
177 * index - array of [n] color indexes
178 * primitive - either GL_POINT, GL_LINE, GL_POLYGON, or GL_BITMAP
180 void gl_write_index_span( GLcontext
*ctx
,
181 GLuint n
, GLint x
, GLint y
, const GLdepth z
[],
183 GLuint indexIn
[], GLenum primitive
)
185 const GLuint modBits
= FOG_BIT
| BLEND_BIT
| MASKING_BIT
| LOGIC_OP_BIT
;
186 GLubyte mask
[MAX_WIDTH
];
187 GLuint indexBackup
[MAX_WIDTH
];
188 GLuint
*index
; /* points to indexIn or indexBackup */
189 SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
191 /* init mask to 1's (all pixels are to be written) */
194 if ((swrast
->_RasterMask
& WINCLIP_BIT
) || primitive
==GL_BITMAP
) {
195 if ((n
= clip_span(ctx
,n
,x
,y
,mask
)) == 0) {
200 if ((primitive
==GL_BITMAP
&& (swrast
->_RasterMask
& modBits
))
201 || (swrast
->_RasterMask
& MULTI_DRAW_BIT
)) {
202 /* Make copy of color indexes */
203 MEMCPY( indexBackup
, indexIn
, n
* sizeof(GLuint
) );
211 /* Do the scissor test */
212 if (ctx
->Scissor
.Enabled
) {
213 if (gl_scissor_span( ctx
, n
, x
, y
, mask
) == 0) {
218 /* Polygon Stippling */
219 if (ctx
->Polygon
.StippleFlag
&& primitive
==GL_POLYGON
) {
220 stipple_polygon_span( ctx
, n
, x
, y
, mask
);
223 if (ctx
->Stencil
.Enabled
) {
224 /* first stencil test */
225 if (_mesa_stencil_and_ztest_span(ctx
, n
, x
, y
, z
, mask
) == GL_FALSE
) {
229 else if (ctx
->Depth
.Test
) {
230 /* regular depth testing */
231 if (_mesa_depth_test_span( ctx
, n
, x
, y
, z
, mask
) == 0)
235 /* if we get here, something passed the depth test */
236 ctx
->OcclusionResult
= GL_TRUE
;
239 if (ctx
->Fog
.Enabled
) {
240 if (fog
&& !swrast
->_PreferPixelFog
)
241 _mesa_fog_ci_pixels( ctx
, n
, fog
, index
);
243 _mesa_depth_fog_ci_pixels( ctx
, n
, z
, index
);
246 if (swrast
->_RasterMask
& MULTI_DRAW_BIT
) {
247 /* draw to zero or two or more buffers */
248 multi_write_index_span( ctx
, n
, x
, y
, index
, mask
);
251 /* normal situation: draw to exactly one buffer */
252 if (ctx
->Color
.IndexLogicOpEnabled
) {
253 _mesa_logicop_ci_span( ctx
, n
, x
, y
, index
, mask
);
256 if (ctx
->Color
.IndexMask
== 0) {
259 else if (ctx
->Color
.IndexMask
!= 0xffffffff) {
260 _mesa_mask_index_span( ctx
, n
, x
, y
, index
);
264 (*ctx
->Driver
.WriteCI32Span
)( ctx
, n
, x
, y
, index
, mask
);
271 void gl_write_monoindex_span( GLcontext
*ctx
,
272 GLuint n
, GLint x
, GLint y
,
275 GLuint index
, GLenum primitive
)
277 SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
278 GLubyte mask
[MAX_WIDTH
];
281 /* init mask to 1's (all pixels are to be written) */
284 if ((swrast
->_RasterMask
& WINCLIP_BIT
) || primitive
==GL_BITMAP
) {
285 if ((n
= clip_span( ctx
, n
, x
, y
, mask
)) == 0) {
290 /* Do the scissor test */
291 if (ctx
->Scissor
.Enabled
) {
292 if (gl_scissor_span( ctx
, n
, x
, y
, mask
) == 0) {
297 /* Polygon Stippling */
298 if (ctx
->Polygon
.StippleFlag
&& primitive
==GL_POLYGON
) {
299 stipple_polygon_span( ctx
, n
, x
, y
, mask
);
302 if (ctx
->Stencil
.Enabled
) {
303 /* first stencil test */
304 if (_mesa_stencil_and_ztest_span(ctx
, n
, x
, y
, z
, mask
) == GL_FALSE
) {
308 else if (ctx
->Depth
.Test
) {
309 /* regular depth testing */
310 if (_mesa_depth_test_span( ctx
, n
, x
, y
, z
, mask
) == 0)
314 /* if we get here, something passed the depth test */
315 ctx
->OcclusionResult
= GL_TRUE
;
317 if (ctx
->Color
.DrawBuffer
== GL_NONE
) {
318 /* write no pixels */
323 || ctx
->Color
.IndexLogicOpEnabled
324 || ctx
->Color
.IndexMask
!= 0xffffffff) {
325 /* different index per pixel */
326 GLuint indexes
[MAX_WIDTH
];
327 for (i
= 0; i
< n
; i
++) {
331 if (ctx
->Fog
.Enabled
) {
332 if (fog
&& !swrast
->_PreferPixelFog
)
333 _mesa_fog_ci_pixels( ctx
, n
, fog
, indexes
);
335 _mesa_depth_fog_ci_pixels( ctx
, n
, z
, indexes
);
338 if (ctx
->Color
.IndexLogicOpEnabled
) {
339 _mesa_logicop_ci_span( ctx
, n
, x
, y
, indexes
, mask
);
342 if (swrast
->_RasterMask
& MULTI_DRAW_BIT
) {
343 /* draw to zero or two or more buffers */
344 multi_write_index_span( ctx
, n
, x
, y
, indexes
, mask
);
347 /* normal situation: draw to exactly one buffer */
348 if (ctx
->Color
.IndexLogicOpEnabled
) {
349 _mesa_logicop_ci_span( ctx
, n
, x
, y
, indexes
, mask
);
351 if (ctx
->Color
.IndexMask
== 0) {
354 else if (ctx
->Color
.IndexMask
!= 0xffffffff) {
355 _mesa_mask_index_span( ctx
, n
, x
, y
, indexes
);
357 (*ctx
->Driver
.WriteCI32Span
)( ctx
, n
, x
, y
, indexes
, mask
);
361 /* same color index for all pixels */
362 ASSERT(!ctx
->Color
.IndexLogicOpEnabled
);
363 ASSERT(ctx
->Color
.IndexMask
== 0xffffffff);
364 if (swrast
->_RasterMask
& MULTI_DRAW_BIT
) {
365 /* draw to zero or two or more buffers */
366 GLuint indexes
[MAX_WIDTH
];
367 for (i
= 0; i
< n
; i
++)
369 multi_write_index_span( ctx
, n
, x
, y
, indexes
, mask
);
372 /* normal situation: draw to exactly one buffer */
373 (*ctx
->Driver
.WriteMonoCISpan
)( ctx
, n
, x
, y
, index
, mask
);
381 * Draw to more than one RGBA color buffer (or none).
383 static void multi_write_rgba_span( GLcontext
*ctx
, GLuint n
,
384 GLint x
, GLint y
, CONST GLchan rgba
[][4],
385 const GLubyte mask
[] )
387 const GLuint colorMask
= *((GLuint
*) ctx
->Color
.ColorMask
);
389 SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
391 if (ctx
->Color
.DrawBuffer
== GL_NONE
)
394 /* loop over four possible dest color buffers */
395 for (bufferBit
= 1; bufferBit
<= 8; bufferBit
= bufferBit
<< 1) {
396 if (bufferBit
& ctx
->Color
.DrawDestMask
) {
397 GLchan rgbaTmp
[MAX_WIDTH
][4];
398 ASSERT(n
< MAX_WIDTH
);
400 if (bufferBit
== FRONT_LEFT_BIT
) {
401 (void) (*ctx
->Driver
.SetDrawBuffer
)( ctx
, GL_FRONT_LEFT
);
402 ctx
->DrawBuffer
->Alpha
= ctx
->DrawBuffer
->FrontLeftAlpha
;
404 else if (bufferBit
== FRONT_RIGHT_BIT
) {
405 (void) (*ctx
->Driver
.SetDrawBuffer
)( ctx
, GL_FRONT_RIGHT
);
406 ctx
->DrawBuffer
->Alpha
= ctx
->DrawBuffer
->FrontRightAlpha
;
408 else if (bufferBit
== BACK_LEFT_BIT
) {
409 (void) (*ctx
->Driver
.SetDrawBuffer
)( ctx
, GL_BACK_LEFT
);
410 ctx
->DrawBuffer
->Alpha
= ctx
->DrawBuffer
->BackLeftAlpha
;
413 (void) (*ctx
->Driver
.SetDrawBuffer
)( ctx
, GL_BACK_RIGHT
);
414 ctx
->DrawBuffer
->Alpha
= ctx
->DrawBuffer
->BackRightAlpha
;
417 /* make copy of incoming colors */
418 MEMCPY( rgbaTmp
, rgba
, 4 * n
* sizeof(GLchan
) );
420 if (ctx
->Color
.ColorLogicOpEnabled
) {
421 _mesa_logicop_rgba_span( ctx
, n
, x
, y
, rgbaTmp
, mask
);
423 else if (ctx
->Color
.BlendEnabled
) {
424 _mesa_blend_span( ctx
, n
, x
, y
, rgbaTmp
, mask
);
426 if (colorMask
== 0x0) {
429 else if (colorMask
!= 0xffffffff) {
430 _mesa_mask_rgba_span( ctx
, n
, x
, y
, rgbaTmp
);
433 (*ctx
->Driver
.WriteRGBASpan
)( ctx
, n
, x
, y
,
434 (const GLchan (*)[4]) rgbaTmp
, mask
);
435 if (swrast
->_RasterMask
& ALPHABUF_BIT
) {
436 _mesa_write_alpha_span( ctx
, n
, x
, y
,
437 (const GLchan (*)[4])rgbaTmp
, mask
);
442 /* restore default dest buffer */
443 (void) (*ctx
->Driver
.SetDrawBuffer
)( ctx
, ctx
->Color
.DriverDrawBuffer
);
448 void gl_write_rgba_span( GLcontext
*ctx
,
449 GLuint n
, GLint x
, GLint y
, const GLdepth z
[],
454 const GLuint modBits
= FOG_BIT
| BLEND_BIT
| MASKING_BIT
|
455 LOGIC_OP_BIT
| TEXTURE_BIT
;
456 GLubyte mask
[MAX_WIDTH
];
457 GLboolean write_all
= GL_TRUE
;
458 GLchan rgbaBackup
[MAX_WIDTH
][4];
460 const GLubyte
*Null
= 0;
461 SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
463 /* init mask to 1's (all pixels are to be written) */
466 if ((swrast
->_RasterMask
& WINCLIP_BIT
) || primitive
==GL_BITMAP
) {
467 if ((n
= clip_span( ctx
,n
,x
,y
,mask
)) == 0) {
471 write_all
= GL_FALSE
;
474 if ((primitive
==GL_BITMAP
&& (swrast
->_RasterMask
& modBits
))
475 || (swrast
->_RasterMask
& MULTI_DRAW_BIT
)) {
476 /* must make a copy of the colors since they may be modified */
477 MEMCPY( rgbaBackup
, rgbaIn
, 4 * n
* sizeof(GLchan
) );
484 /* Do the scissor test */
485 if (ctx
->Scissor
.Enabled
) {
486 if (gl_scissor_span( ctx
, n
, x
, y
, mask
) == 0) {
489 write_all
= GL_FALSE
;
492 /* Polygon Stippling */
493 if (ctx
->Polygon
.StippleFlag
&& primitive
==GL_POLYGON
) {
494 stipple_polygon_span( ctx
, n
, x
, y
, mask
);
495 write_all
= GL_FALSE
;
498 /* Do the alpha test */
499 if (ctx
->Color
.AlphaEnabled
) {
500 if (_mesa_alpha_test( ctx
, n
, (const GLchan (*)[4]) rgba
, mask
) == 0) {
503 write_all
= GL_FALSE
;
506 if (ctx
->Stencil
.Enabled
) {
507 /* first stencil test */
508 if (_mesa_stencil_and_ztest_span(ctx
, n
, x
, y
, z
, mask
) == GL_FALSE
) {
511 write_all
= GL_FALSE
;
513 else if (ctx
->Depth
.Test
) {
514 /* regular depth testing */
515 GLuint m
= _mesa_depth_test_span( ctx
, n
, x
, y
, z
, mask
);
520 write_all
= GL_FALSE
;
524 /* if we get here, something passed the depth test */
525 ctx
->OcclusionResult
= GL_TRUE
;
528 if (ctx
->Fog
.Enabled
) {
529 if (fog
&& !swrast
->_PreferPixelFog
)
530 _mesa_fog_rgba_pixels( ctx
, n
, fog
, rgba
);
532 _mesa_depth_fog_rgba_pixels( ctx
, n
, z
, rgba
);
535 if (swrast
->_RasterMask
& MULTI_DRAW_BIT
) {
536 multi_write_rgba_span( ctx
, n
, x
, y
, (const GLchan (*)[4]) rgba
, mask
);
539 /* normal: write to exactly one buffer */
540 /* logic op or blending */
541 const GLuint colorMask
= *((GLuint
*) ctx
->Color
.ColorMask
);
543 if (ctx
->Color
.ColorLogicOpEnabled
) {
544 _mesa_logicop_rgba_span( ctx
, n
, x
, y
, rgba
, mask
);
546 else if (ctx
->Color
.BlendEnabled
) {
547 _mesa_blend_span( ctx
, n
, x
, y
, rgba
, mask
);
550 /* Color component masking */
551 if (colorMask
== 0x0) {
554 else if (colorMask
!= 0xffffffff) {
555 _mesa_mask_rgba_span( ctx
, n
, x
, y
, rgba
);
559 (*ctx
->Driver
.WriteRGBASpan
)( ctx
, n
, x
, y
,
560 (const GLchan (*)[4]) rgba
,
561 write_all
? Null
: mask
);
563 if (swrast
->_RasterMask
& ALPHABUF_BIT
) {
564 _mesa_write_alpha_span( ctx
, n
, x
, y
,
565 (const GLchan (*)[4]) rgba
,
566 write_all
? Null
: mask
);
574 * Write a horizontal span of color pixels to the frame buffer.
575 * The color is initially constant for the whole span.
576 * Alpha-testing, stenciling, depth-testing, and blending are done as needed.
577 * Input: n - number of pixels in the span
578 * x, y - location of leftmost pixel in the span
579 * z - array of [n] z-values
580 * r, g, b, a - the color of the pixels
581 * primitive - either GL_POINT, GL_LINE, GL_POLYGON or GL_BITMAP.
583 void gl_write_monocolor_span( GLcontext
*ctx
,
584 GLuint n
, GLint x
, GLint y
, const GLdepth z
[],
586 const GLchan color
[4],
589 const GLuint colorMask
= *((GLuint
*) ctx
->Color
.ColorMask
);
591 GLubyte mask
[MAX_WIDTH
];
592 GLboolean write_all
= GL_TRUE
;
593 GLchan rgba
[MAX_WIDTH
][4];
594 const GLubyte
*Null
= 0;
595 SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
597 /* init mask to 1's (all pixels are to be written) */
600 if ((swrast
->_RasterMask
& WINCLIP_BIT
) || primitive
==GL_BITMAP
) {
601 if ((n
= clip_span( ctx
,n
,x
,y
,mask
)) == 0) {
605 write_all
= GL_FALSE
;
608 /* Do the scissor test */
609 if (ctx
->Scissor
.Enabled
) {
610 if (gl_scissor_span( ctx
, n
, x
, y
, mask
) == 0) {
613 write_all
= GL_FALSE
;
616 /* Polygon Stippling */
617 if (ctx
->Polygon
.StippleFlag
&& primitive
==GL_POLYGON
) {
618 stipple_polygon_span( ctx
, n
, x
, y
, mask
);
619 write_all
= GL_FALSE
;
622 /* Do the alpha test */
623 if (ctx
->Color
.AlphaEnabled
) {
624 for (i
= 0; i
< n
; i
++) {
625 rgba
[i
][ACOMP
] = color
[ACOMP
];
627 if (_mesa_alpha_test( ctx
, n
, (const GLchan (*)[4])rgba
, mask
) == 0) {
630 write_all
= GL_FALSE
;
633 if (ctx
->Stencil
.Enabled
) {
634 /* first stencil test */
635 if (_mesa_stencil_and_ztest_span(ctx
, n
, x
, y
, z
, mask
) == GL_FALSE
) {
638 write_all
= GL_FALSE
;
640 else if (ctx
->Depth
.Test
) {
641 /* regular depth testing */
642 GLuint m
= _mesa_depth_test_span( ctx
, n
, x
, y
, z
, mask
);
647 write_all
= GL_FALSE
;
651 /* if we get here, something passed the depth test */
652 ctx
->OcclusionResult
= GL_TRUE
;
654 if (ctx
->Color
.DrawBuffer
== GL_NONE
) {
655 /* write no pixels */
659 if (ctx
->Color
.ColorLogicOpEnabled
|| colorMask
!= 0xffffffff ||
660 (swrast
->_RasterMask
& (BLEND_BIT
| FOG_BIT
))) {
661 /* assign same color to each pixel */
662 for (i
= 0; i
< n
; i
++) {
664 COPY_CHAN4(rgba
[i
], color
);
669 if (ctx
->Fog
.Enabled
) {
670 if (fog
&& !swrast
->_PreferPixelFog
)
671 _mesa_fog_rgba_pixels( ctx
, n
, fog
, rgba
);
673 _mesa_depth_fog_rgba_pixels( ctx
, n
, z
, rgba
);
676 if (swrast
->_RasterMask
& MULTI_DRAW_BIT
) {
677 multi_write_rgba_span( ctx
, n
, x
, y
,
678 (const GLchan (*)[4]) rgba
, mask
);
681 /* normal: write to exactly one buffer */
682 if (ctx
->Color
.ColorLogicOpEnabled
) {
683 _mesa_logicop_rgba_span( ctx
, n
, x
, y
, rgba
, mask
);
685 else if (ctx
->Color
.BlendEnabled
) {
686 _mesa_blend_span( ctx
, n
, x
, y
, rgba
, mask
);
689 /* Color component masking */
690 if (colorMask
== 0x0) {
693 else if (colorMask
!= 0xffffffff) {
694 _mesa_mask_rgba_span( ctx
, n
, x
, y
, rgba
);
698 (*ctx
->Driver
.WriteRGBASpan
)( ctx
, n
, x
, y
,
699 (const GLchan (*)[4]) rgba
,
700 write_all
? Null
: mask
);
701 if (swrast
->_RasterMask
& ALPHABUF_BIT
) {
702 _mesa_write_alpha_span( ctx
, n
, x
, y
,
703 (const GLchan (*)[4]) rgba
,
704 write_all
? Null
: mask
);
709 /* same color for all pixels */
710 ASSERT(!ctx
->Color
.BlendEnabled
);
711 ASSERT(!ctx
->Color
.ColorLogicOpEnabled
);
713 if (swrast
->_RasterMask
& MULTI_DRAW_BIT
) {
714 for (i
= 0; i
< n
; i
++) {
716 COPY_CHAN4(rgba
[i
], color
);
719 multi_write_rgba_span( ctx
, n
, x
, y
,
720 (const GLchan (*)[4]) rgba
, mask
);
723 (*ctx
->Driver
.WriteMonoRGBASpan
)( ctx
, n
, x
, y
, color
, mask
);
724 if (swrast
->_RasterMask
& ALPHABUF_BIT
) {
725 _mesa_write_mono_alpha_span( ctx
, n
, x
, y
, (GLchan
) color
[ACOMP
],
726 write_all
? Null
: mask
);
735 * Add specular color to base color. This is used only when
736 * GL_LIGHT_MODEL_COLOR_CONTROL = GL_SEPARATE_SPECULAR_COLOR.
738 static void add_colors(GLuint n
, GLchan rgba
[][4], CONST GLchan specular
[][4] )
741 for (i
= 0; i
< n
; i
++) {
742 GLint r
= rgba
[i
][RCOMP
] + specular
[i
][RCOMP
];
743 GLint g
= rgba
[i
][GCOMP
] + specular
[i
][GCOMP
];
744 GLint b
= rgba
[i
][BCOMP
] + specular
[i
][BCOMP
];
745 rgba
[i
][RCOMP
] = (GLchan
) MIN2(r
, CHAN_MAX
);
746 rgba
[i
][GCOMP
] = (GLchan
) MIN2(g
, CHAN_MAX
);
747 rgba
[i
][BCOMP
] = (GLchan
) MIN2(b
, CHAN_MAX
);
753 * Write a horizontal span of textured pixels to the frame buffer.
754 * The color of each pixel is different.
755 * Alpha-testing, stenciling, depth-testing, and blending are done
757 * Input: n - number of pixels in the span
758 * x, y - location of leftmost pixel in the span
759 * z - array of [n] z-values
760 * s, t - array of (s,t) texture coordinates for each pixel
761 * lambda - array of texture lambda values
762 * rgba - array of [n] color components
763 * primitive - either GL_POINT, GL_LINE, GL_POLYGON or GL_BITMAP.
765 void gl_write_texture_span( GLcontext
*ctx
,
766 GLuint n
, GLint x
, GLint y
, const GLdepth z
[],
768 const GLfloat s
[], const GLfloat t
[],
769 const GLfloat u
[], GLfloat lambda
[],
770 GLchan rgbaIn
[][4], CONST GLchan spec
[][4],
773 const GLuint colorMask
= *((GLuint
*) ctx
->Color
.ColorMask
);
774 GLubyte mask
[MAX_WIDTH
];
775 GLboolean write_all
= GL_TRUE
;
776 GLchan rgbaBackup
[MAX_WIDTH
][4];
777 GLchan (*rgba
)[4]; /* points to either rgbaIn or rgbaBackup */
778 const GLubyte
*Null
= 0;
779 SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
781 /* init mask to 1's (all pixels are to be written) */
784 if ((swrast
->_RasterMask
& WINCLIP_BIT
) || primitive
==GL_BITMAP
) {
785 if ((n
=clip_span(ctx
, n
, x
, y
, mask
)) == 0) {
789 write_all
= GL_FALSE
;
793 if (primitive
==GL_BITMAP
|| (swrast
->_RasterMask
& MULTI_DRAW_BIT
)) {
794 /* must make a copy of the colors since they may be modified */
795 MEMCPY(rgbaBackup
, rgbaIn
, 4 * n
* sizeof(GLchan
));
802 /* Do the scissor test */
803 if (ctx
->Scissor
.Enabled
) {
804 if (gl_scissor_span( ctx
, n
, x
, y
, mask
) == 0) {
807 write_all
= GL_FALSE
;
810 /* Polygon Stippling */
811 if (ctx
->Polygon
.StippleFlag
&& primitive
==GL_POLYGON
) {
812 stipple_polygon_span( ctx
, n
, x
, y
, mask
);
813 write_all
= GL_FALSE
;
816 /* Texture with alpha test*/
817 if (ctx
->Color
.AlphaEnabled
) {
818 /* Texturing without alpha is done after depth-testing which
819 gives a potential speed-up. */
820 ASSERT(ctx
->Texture
._ReallyEnabled
);
821 gl_texture_pixels( ctx
, 0, n
, s
, t
, u
, lambda
, rgba
, rgba
);
823 /* Do the alpha test */
824 if (_mesa_alpha_test( ctx
, n
, (const GLchan (*)[4]) rgba
, mask
) == 0) {
827 write_all
= GL_FALSE
;
830 if (ctx
->Stencil
.Enabled
) {
831 /* first stencil test */
832 if (_mesa_stencil_and_ztest_span(ctx
, n
, x
, y
, z
, mask
) == GL_FALSE
) {
835 write_all
= GL_FALSE
;
837 else if (ctx
->Depth
.Test
) {
838 /* regular depth testing */
839 GLuint m
= _mesa_depth_test_span( ctx
, n
, x
, y
, z
, mask
);
844 write_all
= GL_FALSE
;
848 /* if we get here, something passed the depth test */
849 ctx
->OcclusionResult
= GL_TRUE
;
851 /* Texture without alpha test */
852 if (! ctx
->Color
.AlphaEnabled
) {
853 ASSERT(ctx
->Texture
._ReallyEnabled
);
854 gl_texture_pixels( ctx
, 0, n
, s
, t
, u
, lambda
, rgba
, rgba
);
857 /* Add base and specular colors */
859 (ctx
->Fog
.ColorSumEnabled
||
860 (ctx
->Light
.Enabled
&& ctx
->Light
.Model
.ColorControl
== GL_SEPARATE_SPECULAR_COLOR
)))
861 add_colors( n
, rgba
, spec
); /* rgba = rgba + spec */
864 if (ctx
->Fog
.Enabled
) {
865 if (fog
&& !swrast
->_PreferPixelFog
)
866 _mesa_fog_rgba_pixels( ctx
, n
, fog
, rgba
);
868 _mesa_depth_fog_rgba_pixels( ctx
, n
, z
, rgba
);
871 if (swrast
->_RasterMask
& MULTI_DRAW_BIT
) {
872 multi_write_rgba_span( ctx
, n
, x
, y
, (const GLchan (*)[4]) rgba
, mask
);
875 /* normal: write to exactly one buffer */
876 if (ctx
->Color
.ColorLogicOpEnabled
) {
877 _mesa_logicop_rgba_span( ctx
, n
, x
, y
, rgba
, mask
);
879 else if (ctx
->Color
.BlendEnabled
) {
880 _mesa_blend_span( ctx
, n
, x
, y
, rgba
, mask
);
882 if (colorMask
== 0x0) {
885 else if (colorMask
!= 0xffffffff) {
886 _mesa_mask_rgba_span( ctx
, n
, x
, y
, rgba
);
889 (*ctx
->Driver
.WriteRGBASpan
)( ctx
, n
, x
, y
, (const GLchan (*)[4])rgba
,
890 write_all
? Null
: mask
);
891 if (swrast
->_RasterMask
& ALPHABUF_BIT
) {
892 _mesa_write_alpha_span( ctx
, n
, x
, y
, (const GLchan (*)[4]) rgba
,
893 write_all
? Null
: mask
);
901 * As above but perform multiple stages of texture application.
904 gl_write_multitexture_span( GLcontext
*ctx
,
905 GLuint n
, GLint x
, GLint y
,
908 CONST GLfloat s
[MAX_TEXTURE_UNITS
][MAX_WIDTH
],
909 CONST GLfloat t
[MAX_TEXTURE_UNITS
][MAX_WIDTH
],
910 CONST GLfloat u
[MAX_TEXTURE_UNITS
][MAX_WIDTH
],
911 GLfloat lambda
[][MAX_WIDTH
],
912 GLchan rgbaIn
[MAX_TEXTURE_UNITS
][4],
913 CONST GLchan spec
[MAX_TEXTURE_UNITS
][4],
916 GLubyte mask
[MAX_WIDTH
];
917 GLboolean write_all
= GL_TRUE
;
918 GLchan rgbaBackup
[MAX_WIDTH
][4];
919 GLchan (*rgba
)[4]; /* points to either rgbaIn or rgbaBackup */
921 const GLubyte
*Null
= 0;
922 const GLuint texUnits
= ctx
->Const
.MaxTextureUnits
;
923 SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
925 /* init mask to 1's (all pixels are to be written) */
928 if ((swrast
->_RasterMask
& WINCLIP_BIT
) || primitive
==GL_BITMAP
) {
929 if ((n
=clip_span(ctx
, n
, x
, y
, mask
)) == 0) {
933 write_all
= GL_FALSE
;
937 if (primitive
==GL_BITMAP
|| (swrast
->_RasterMask
& MULTI_DRAW_BIT
)
939 /* must make a copy of the colors since they may be modified */
940 MEMCPY(rgbaBackup
, rgbaIn
, 4 * n
* sizeof(GLchan
));
947 /* Do the scissor test */
948 if (ctx
->Scissor
.Enabled
) {
949 if (gl_scissor_span( ctx
, n
, x
, y
, mask
) == 0) {
952 write_all
= GL_FALSE
;
955 /* Polygon Stippling */
956 if (ctx
->Polygon
.StippleFlag
&& primitive
==GL_POLYGON
) {
957 stipple_polygon_span( ctx
, n
, x
, y
, mask
);
958 write_all
= GL_FALSE
;
961 /* Texture with alpha test*/
962 if (ctx
->Color
.AlphaEnabled
) {
963 /* Texturing without alpha is done after depth-testing which
964 * gives a potential speed-up.
966 ASSERT(ctx
->Texture
._ReallyEnabled
);
967 for (i
= 0; i
< texUnits
; i
++)
968 gl_texture_pixels( ctx
, i
, n
, s
[i
], t
[i
], u
[i
], lambda
[i
], rgbaIn
, rgba
);
970 /* Do the alpha test */
971 if (_mesa_alpha_test( ctx
, n
, (const GLchan (*)[4])rgba
, mask
) == 0) {
974 write_all
= GL_FALSE
;
977 if (ctx
->Stencil
.Enabled
) {
978 /* first stencil test */
979 if (_mesa_stencil_and_ztest_span(ctx
, n
, x
, y
, z
, mask
) == GL_FALSE
) {
982 write_all
= GL_FALSE
;
984 else if (ctx
->Depth
.Test
) {
985 /* regular depth testing */
986 GLuint m
= _mesa_depth_test_span( ctx
, n
, x
, y
, z
, mask
);
991 write_all
= GL_FALSE
;
995 /* if we get here, something passed the depth test */
996 ctx
->OcclusionResult
= GL_TRUE
;
998 /* Texture without alpha test */
999 if (! ctx
->Color
.AlphaEnabled
) {
1000 ASSERT(ctx
->Texture
._ReallyEnabled
);
1001 for (i
= 0; i
< texUnits
; i
++)
1002 gl_texture_pixels( ctx
, i
, n
, s
[i
], t
[i
], u
[i
],
1003 lambda
[i
], rgbaIn
, rgba
);
1006 /* Add base and specular colors */
1008 (ctx
->Fog
.ColorSumEnabled
||
1009 (ctx
->Light
.Enabled
&&
1010 ctx
->Light
.Model
.ColorControl
== GL_SEPARATE_SPECULAR_COLOR
)))
1011 add_colors( n
, rgba
, spec
); /* rgba = rgba + spec */
1014 if (ctx
->Fog
.Enabled
) {
1015 if (fog
&& !swrast
->_PreferPixelFog
)
1016 _mesa_fog_rgba_pixels( ctx
, n
, fog
, rgba
);
1018 _mesa_depth_fog_rgba_pixels( ctx
, n
, z
, rgba
);
1021 if (swrast
->_RasterMask
& MULTI_DRAW_BIT
) {
1022 multi_write_rgba_span( ctx
, n
, x
, y
, (const GLchan (*)[4]) rgba
, mask
);
1025 /* normal: write to exactly one buffer */
1026 const GLuint colorMask
= *((GLuint
*) ctx
->Color
.ColorMask
);
1028 if (ctx
->Color
.ColorLogicOpEnabled
) {
1029 _mesa_logicop_rgba_span( ctx
, n
, x
, y
, rgba
, mask
);
1031 else if (ctx
->Color
.BlendEnabled
) {
1032 _mesa_blend_span( ctx
, n
, x
, y
, rgba
, mask
);
1035 if (colorMask
== 0x0) {
1038 else if (colorMask
!= 0xffffffff) {
1039 _mesa_mask_rgba_span( ctx
, n
, x
, y
, rgba
);
1042 (*ctx
->Driver
.WriteRGBASpan
)( ctx
, n
, x
, y
, (const GLchan (*)[4])rgba
,
1043 write_all
? Null
: mask
);
1044 if (swrast
->_RasterMask
& ALPHABUF_BIT
) {
1045 _mesa_write_alpha_span( ctx
, n
, x
, y
, (const GLchan (*)[4])rgba
,
1046 write_all
? Null
: mask
);
1054 * Read RGBA pixels from frame buffer. Clipping will be done to prevent
1055 * reading ouside the buffer's boundaries.
1057 void gl_read_rgba_span( GLcontext
*ctx
, GLframebuffer
*buffer
,
1058 GLuint n
, GLint x
, GLint y
,
1061 if (y
< 0 || y
>= buffer
->Height
1062 || x
+ (GLint
) n
< 0 || x
>= buffer
->Width
) {
1063 /* completely above, below, or right */
1064 /* XXX maybe leave undefined? */
1065 BZERO(rgba
, 4 * n
* sizeof(GLchan
));
1070 /* left edge clippping */
1072 length
= (GLint
) n
- skip
;
1074 /* completely left of window */
1077 if (length
> buffer
->Width
) {
1078 length
= buffer
->Width
;
1081 else if ((GLint
) (x
+ n
) > buffer
->Width
) {
1082 /* right edge clipping */
1084 length
= buffer
->Width
- x
;
1086 /* completely to right of window */
1096 (*ctx
->Driver
.ReadRGBASpan
)( ctx
, length
, x
+ skip
, y
, rgba
+ skip
);
1097 if (buffer
->UseSoftwareAlphaBuffers
) {
1098 _mesa_read_alpha_span( ctx
, length
, x
+ skip
, y
, rgba
+ skip
);
1107 * Read CI pixels from frame buffer. Clipping will be done to prevent
1108 * reading ouside the buffer's boundaries.
1110 void gl_read_index_span( GLcontext
*ctx
, GLframebuffer
*buffer
,
1111 GLuint n
, GLint x
, GLint y
, GLuint indx
[] )
1113 if (y
< 0 || y
>= buffer
->Height
1114 || x
+ (GLint
) n
< 0 || x
>= buffer
->Width
) {
1115 /* completely above, below, or right */
1116 BZERO(indx
, n
* sizeof(GLuint
));
1121 /* left edge clippping */
1123 length
= (GLint
) n
- skip
;
1125 /* completely left of window */
1128 if (length
> buffer
->Width
) {
1129 length
= buffer
->Width
;
1132 else if ((GLint
) (x
+ n
) > buffer
->Width
) {
1133 /* right edge clipping */
1135 length
= buffer
->Width
- x
;
1137 /* completely to right of window */
1147 (*ctx
->Driver
.ReadCI32Span
)( ctx
, length
, skip
+ x
, y
, indx
+ skip
);