2 * Mesa 3-D graphics library
5 * Copyright (C) 1999-2007 Brian Paul All Rights Reserved.
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
21 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 #include "main/glheader.h"
27 #include "main/context.h"
28 #include "main/imports.h"
30 #include "s_context.h"
32 #include "s_stencil.h"
39 IF stencil test fails THEN
40 Apply fail-op to stencil value
41 Don't write the pixel (RGBA,Z)
43 IF doing depth test && depth test fails THEN
44 Apply zfail-op to stencil value
45 Write RGBA and Z to appropriate buffers
47 Apply zpass-op to stencil value
54 * Apply the given stencil operator to the array of stencil values.
55 * Don't touch stencil[i] if mask[i] is zero.
56 * Input: n - size of stencil array
57 * oper - the stencil buffer operator
58 * face - 0 or 1 for front or back face operation
59 * stencil - array of stencil values
60 * mask - array [n] of flag: 1=apply operator, 0=don't apply operator
61 * Output: stencil - modified values
64 apply_stencil_op( const GLcontext
*ctx
, GLenum oper
, GLuint face
,
65 GLuint n
, GLstencil stencil
[], const GLubyte mask
[] )
67 const GLstencil ref
= ctx
->Stencil
.Ref
[face
];
68 const GLstencil wrtmask
= ctx
->Stencil
.WriteMask
[face
];
69 const GLstencil invmask
= (GLstencil
) (~wrtmask
);
70 const GLstencil stencilMax
= (1 << ctx
->DrawBuffer
->Visual
.stencilBits
) - 1;
88 stencil
[i
] = (GLstencil
) (stencil
[i
] & invmask
);
104 GLstencil s
= stencil
[i
];
105 stencil
[i
] = (GLstencil
) ((invmask
& s
) | (wrtmask
& ref
));
114 GLstencil s
= stencil
[i
];
115 if (s
< stencilMax
) {
116 stencil
[i
] = (GLstencil
) (s
+1);
124 /* VERIFY logic of adding 1 to a write-masked value */
125 GLstencil s
= stencil
[i
];
126 if (s
< stencilMax
) {
127 stencil
[i
] = (GLstencil
) ((invmask
& s
) | (wrtmask
& (s
+1)));
137 GLstencil s
= stencil
[i
];
139 stencil
[i
] = (GLstencil
) (s
-1);
147 /* VERIFY logic of subtracting 1 to a write-masked value */
148 GLstencil s
= stencil
[i
];
150 stencil
[i
] = (GLstencil
) ((invmask
& s
) | (wrtmask
& (s
-1)));
156 case GL_INCR_WRAP_EXT
:
167 GLstencil s
= stencil
[i
];
168 stencil
[i
] = (GLstencil
) ((invmask
& s
) | (wrtmask
& (s
+1)));
173 case GL_DECR_WRAP_EXT
:
184 GLstencil s
= stencil
[i
];
185 stencil
[i
] = (GLstencil
) ((invmask
& s
) | (wrtmask
& (s
-1)));
194 GLstencil s
= stencil
[i
];
195 stencil
[i
] = (GLstencil
) ~s
;
202 GLstencil s
= stencil
[i
];
203 stencil
[i
] = (GLstencil
) ((invmask
& s
) | (wrtmask
& ~s
));
209 _mesa_problem(ctx
, "Bad stencil op in apply_stencil_op");
217 * Apply stencil test to an array of stencil values (before depth buffering).
218 * Input: face - 0 or 1 for front or back-face polygons
219 * n - number of pixels in the array
220 * stencil - array of [n] stencil values
221 * mask - array [n] of flag: 0=skip the pixel, 1=stencil the pixel
222 * Output: mask - pixels which fail the stencil test will have their
223 * mask flag set to 0.
224 * stencil - updated stencil values (where the test passed)
225 * Return: GL_FALSE = all pixels failed, GL_TRUE = zero or more pixels passed.
228 do_stencil_test( GLcontext
*ctx
, GLuint face
, GLuint n
, GLstencil stencil
[],
231 GLubyte fail
[MAX_WIDTH
];
232 GLboolean allfail
= GL_FALSE
;
234 const GLuint valueMask
= ctx
->Stencil
.ValueMask
[face
];
235 const GLstencil r
= (GLstencil
) (ctx
->Stencil
.Ref
[face
] & valueMask
);
238 ASSERT(n
<= MAX_WIDTH
);
241 * Perform stencil test. The results of this operation are stored
242 * in the fail[] array:
243 * IF fail[i] is non-zero THEN
244 * the stencil fail operator is to be applied
246 * the stencil fail operator is not to be applied
249 switch (ctx
->Stencil
.Function
[face
]) {
251 /* never pass; always fail */
266 s
= (GLstencil
) (stencil
[i
] & valueMask
);
284 s
= (GLstencil
) (stencil
[i
] & valueMask
);
302 s
= (GLstencil
) (stencil
[i
] & valueMask
);
320 s
= (GLstencil
) (stencil
[i
] & valueMask
);
338 s
= (GLstencil
) (stencil
[i
] & valueMask
);
356 s
= (GLstencil
) (stencil
[i
] & valueMask
);
378 _mesa_problem(ctx
, "Bad stencil func in gl_stencil_span");
382 if (ctx
->Stencil
.FailFunc
[face
] != GL_KEEP
) {
383 apply_stencil_op( ctx
, ctx
->Stencil
.FailFunc
[face
], face
, n
, stencil
, fail
);
391 * Compute the zpass/zfail masks by comparing the pre- and post-depth test
395 compute_pass_fail_masks(GLuint n
, const GLubyte origMask
[],
396 const GLubyte newMask
[],
397 GLubyte passMask
[], GLubyte failMask
[])
400 for (i
= 0; i
< n
; i
++) {
401 ASSERT(newMask
[i
] == 0 || newMask
[i
] == 1);
402 passMask
[i
] = origMask
[i
] & newMask
[i
];
403 failMask
[i
] = origMask
[i
] & (newMask
[i
] ^ 1);
409 * Apply stencil and depth testing to the span of pixels.
410 * Both software and hardware stencil buffers are acceptable.
411 * Input: n - number of pixels in the span
412 * x, y - location of leftmost pixel in span
413 * z - array [n] of z values
414 * mask - array [n] of flags (1=test this pixel, 0=skip the pixel)
415 * Output: mask - array [n] of flags (1=stencil and depth test passed)
416 * Return: GL_FALSE - all fragments failed the testing
417 * GL_TRUE - one or more fragments passed the testing
421 stencil_and_ztest_span(GLcontext
*ctx
, SWspan
*span
, GLuint face
)
423 struct gl_framebuffer
*fb
= ctx
->DrawBuffer
;
424 struct gl_renderbuffer
*rb
= fb
->_StencilBuffer
;
425 GLstencil stencilRow
[MAX_WIDTH
];
427 const GLuint n
= span
->end
;
428 const GLint x
= span
->x
;
429 const GLint y
= span
->y
;
430 GLubyte
*mask
= span
->array
->mask
;
432 ASSERT((span
->arrayMask
& SPAN_XY
) == 0);
433 ASSERT(ctx
->Stencil
.Enabled
);
434 ASSERT(n
<= MAX_WIDTH
);
436 if (ctx
->Depth
.Test
) {
437 ASSERT(span
->arrayMask
& SPAN_Z
);
441 stencil
= (GLstencil
*) rb
->GetPointer(ctx
, rb
, x
, y
);
443 rb
->GetRow(ctx
, rb
, n
, x
, y
, stencilRow
);
444 stencil
= stencilRow
;
448 * Apply the stencil test to the fragments.
449 * failMask[i] is 1 if the stencil test failed.
451 if (do_stencil_test( ctx
, face
, n
, stencil
, mask
) == GL_FALSE
) {
452 /* all fragments failed the stencil test, we're done. */
453 span
->writeAll
= GL_FALSE
;
454 if (!rb
->GetPointer(ctx
, rb
, 0, 0)) {
455 /* put updated stencil values into buffer */
456 rb
->PutRow(ctx
, rb
, n
, x
, y
, stencil
, NULL
);
462 * Some fragments passed the stencil test, apply depth test to them
463 * and apply Zpass and Zfail stencil ops.
465 if (ctx
->Depth
.Test
== GL_FALSE
) {
467 * No depth buffer, just apply zpass stencil function to active pixels.
469 apply_stencil_op( ctx
, ctx
->Stencil
.ZPassFunc
[face
], face
, n
, stencil
, mask
);
473 * Perform depth buffering, then apply zpass or zfail stencil function.
475 GLubyte passMask
[MAX_WIDTH
], failMask
[MAX_WIDTH
], origMask
[MAX_WIDTH
];
477 /* save the current mask bits */
478 _mesa_memcpy(origMask
, mask
, n
* sizeof(GLubyte
));
480 /* apply the depth test */
481 _swrast_depth_test_span(ctx
, span
);
483 compute_pass_fail_masks(n
, origMask
, mask
, passMask
, failMask
);
485 /* apply the pass and fail operations */
486 if (ctx
->Stencil
.ZFailFunc
[face
] != GL_KEEP
) {
487 apply_stencil_op( ctx
, ctx
->Stencil
.ZFailFunc
[face
], face
,
488 n
, stencil
, failMask
);
490 if (ctx
->Stencil
.ZPassFunc
[face
] != GL_KEEP
) {
491 apply_stencil_op( ctx
, ctx
->Stencil
.ZPassFunc
[face
], face
,
492 n
, stencil
, passMask
);
497 * Write updated stencil values back into hardware stencil buffer.
499 if (!rb
->GetPointer(ctx
, rb
, 0, 0)) {
500 rb
->PutRow(ctx
, rb
, n
, x
, y
, stencil
, NULL
);
503 span
->writeAll
= GL_FALSE
;
505 return GL_TRUE
; /* one or more fragments passed both tests */
511 * Return the address of a stencil buffer value given the window coords:
513 #define STENCIL_ADDRESS(X, Y) (stencilStart + (Y) * stride + (X))
518 * Apply the given stencil operator for each pixel in the array whose
520 * \note This is for software stencil buffers only.
521 * Input: n - number of pixels in the span
522 * x, y - array of [n] pixels
523 * operator - the stencil buffer operator
524 * mask - array [n] of flag: 1=apply operator, 0=don't apply operator
527 apply_stencil_op_to_pixels( GLcontext
*ctx
,
528 GLuint n
, const GLint x
[], const GLint y
[],
529 GLenum oper
, GLuint face
, const GLubyte mask
[] )
531 struct gl_framebuffer
*fb
= ctx
->DrawBuffer
;
532 struct gl_renderbuffer
*rb
= fb
->_StencilBuffer
;
533 const GLstencil stencilMax
= (1 << fb
->Visual
.stencilBits
) - 1;
534 const GLstencil ref
= ctx
->Stencil
.Ref
[face
];
535 const GLstencil wrtmask
= ctx
->Stencil
.WriteMask
[face
];
536 const GLstencil invmask
= (GLstencil
) (~wrtmask
);
538 GLstencil
*stencilStart
= (GLubyte
*) rb
->Data
;
539 const GLuint stride
= rb
->Width
;
541 ASSERT(rb
->GetPointer(ctx
, rb
, 0, 0));
542 ASSERT(sizeof(GLstencil
) == 1);
552 GLstencil
*sptr
= STENCIL_ADDRESS( x
[i
], y
[i
] );
560 GLstencil
*sptr
= STENCIL_ADDRESS( x
[i
], y
[i
] );
561 *sptr
= (GLstencil
) (invmask
& *sptr
);
570 GLstencil
*sptr
= STENCIL_ADDRESS( x
[i
], y
[i
] );
578 GLstencil
*sptr
= STENCIL_ADDRESS( x
[i
], y
[i
] );
579 *sptr
= (GLstencil
) ((invmask
& *sptr
) | (wrtmask
& ref
));
588 GLstencil
*sptr
= STENCIL_ADDRESS( x
[i
], y
[i
] );
589 if (*sptr
< stencilMax
) {
590 *sptr
= (GLstencil
) (*sptr
+ 1);
598 GLstencil
*sptr
= STENCIL_ADDRESS( x
[i
], y
[i
] );
599 if (*sptr
< stencilMax
) {
600 *sptr
= (GLstencil
) ((invmask
& *sptr
) | (wrtmask
& (*sptr
+1)));
610 GLstencil
*sptr
= STENCIL_ADDRESS( x
[i
], y
[i
] );
612 *sptr
= (GLstencil
) (*sptr
- 1);
620 GLstencil
*sptr
= STENCIL_ADDRESS( x
[i
], y
[i
] );
622 *sptr
= (GLstencil
) ((invmask
& *sptr
) | (wrtmask
& (*sptr
-1)));
628 case GL_INCR_WRAP_EXT
:
632 GLstencil
*sptr
= STENCIL_ADDRESS( x
[i
], y
[i
] );
633 *sptr
= (GLstencil
) (*sptr
+ 1);
640 GLstencil
*sptr
= STENCIL_ADDRESS( x
[i
], y
[i
] );
641 *sptr
= (GLstencil
) ((invmask
& *sptr
) | (wrtmask
& (*sptr
+1)));
646 case GL_DECR_WRAP_EXT
:
650 GLstencil
*sptr
= STENCIL_ADDRESS( x
[i
], y
[i
] );
651 *sptr
= (GLstencil
) (*sptr
- 1);
658 GLstencil
*sptr
= STENCIL_ADDRESS( x
[i
], y
[i
] );
659 *sptr
= (GLstencil
) ((invmask
& *sptr
) | (wrtmask
& (*sptr
-1)));
668 GLstencil
*sptr
= STENCIL_ADDRESS( x
[i
], y
[i
] );
669 *sptr
= (GLstencil
) (~*sptr
);
676 GLstencil
*sptr
= STENCIL_ADDRESS( x
[i
], y
[i
] );
677 *sptr
= (GLstencil
) ((invmask
& *sptr
) | (wrtmask
& ~*sptr
));
683 _mesa_problem(ctx
, "Bad stencilop in apply_stencil_op_to_pixels");
690 * Apply stencil test to an array of pixels before depth buffering.
692 * \note Used for software stencil buffer only.
693 * Input: n - number of pixels in the span
694 * x, y - array of [n] pixels to stencil
695 * mask - array [n] of flag: 0=skip the pixel, 1=stencil the pixel
696 * Output: mask - pixels which fail the stencil test will have their
697 * mask flag set to 0.
698 * \return GL_FALSE = all pixels failed, GL_TRUE = zero or more pixels passed.
701 stencil_test_pixels( GLcontext
*ctx
, GLuint face
, GLuint n
,
702 const GLint x
[], const GLint y
[], GLubyte mask
[] )
704 const struct gl_framebuffer
*fb
= ctx
->DrawBuffer
;
705 struct gl_renderbuffer
*rb
= fb
->_StencilBuffer
;
706 GLubyte fail
[MAX_WIDTH
];
709 GLboolean allfail
= GL_FALSE
;
710 const GLuint valueMask
= ctx
->Stencil
.ValueMask
[face
];
711 const GLstencil
*stencilStart
= (GLstencil
*) rb
->Data
;
712 const GLuint stride
= rb
->Width
;
714 ASSERT(rb
->GetPointer(ctx
, rb
, 0, 0));
715 ASSERT(sizeof(GLstencil
) == 1);
718 * Perform stencil test. The results of this operation are stored
719 * in the fail[] array:
720 * IF fail[i] is non-zero THEN
721 * the stencil fail operator is to be applied
723 * the stencil fail operator is not to be applied
727 switch (ctx
->Stencil
.Function
[face
]) {
742 r
= (GLstencil
) (ctx
->Stencil
.Ref
[face
] & valueMask
);
745 const GLstencil
*sptr
= STENCIL_ADDRESS(x
[i
],y
[i
]);
746 s
= (GLstencil
) (*sptr
& valueMask
);
762 r
= (GLstencil
) (ctx
->Stencil
.Ref
[face
] & valueMask
);
765 const GLstencil
*sptr
= STENCIL_ADDRESS(x
[i
],y
[i
]);
766 s
= (GLstencil
) (*sptr
& valueMask
);
782 r
= (GLstencil
) (ctx
->Stencil
.Ref
[face
] & valueMask
);
785 const GLstencil
*sptr
= STENCIL_ADDRESS(x
[i
],y
[i
]);
786 s
= (GLstencil
) (*sptr
& valueMask
);
802 r
= (GLstencil
) (ctx
->Stencil
.Ref
[face
] & valueMask
);
805 const GLstencil
*sptr
= STENCIL_ADDRESS(x
[i
],y
[i
]);
806 s
= (GLstencil
) (*sptr
& valueMask
);
822 r
= (GLstencil
) (ctx
->Stencil
.Ref
[face
] & valueMask
);
825 const GLstencil
*sptr
= STENCIL_ADDRESS(x
[i
],y
[i
]);
826 s
= (GLstencil
) (*sptr
& valueMask
);
842 r
= (GLstencil
) (ctx
->Stencil
.Ref
[face
] & valueMask
);
845 const GLstencil
*sptr
= STENCIL_ADDRESS(x
[i
],y
[i
]);
846 s
= (GLstencil
) (*sptr
& valueMask
);
868 _mesa_problem(ctx
, "Bad stencil func in gl_stencil_pixels");
872 if (ctx
->Stencil
.FailFunc
[face
] != GL_KEEP
) {
873 apply_stencil_op_to_pixels( ctx
, n
, x
, y
, ctx
->Stencil
.FailFunc
[face
],
884 * Apply stencil and depth testing to an array of pixels.
885 * This is used both for software and hardware stencil buffers.
887 * The comments in this function are a bit sparse but the code is
888 * almost identical to stencil_and_ztest_span(), which is well
891 * Input: n - number of pixels in the array
892 * x, y - array of [n] pixel positions
893 * z - array [n] of z values
894 * mask - array [n] of flags (1=test this pixel, 0=skip the pixel)
895 * Output: mask - array [n] of flags (1=stencil and depth test passed)
896 * Return: GL_FALSE - all fragments failed the testing
897 * GL_TRUE - one or more fragments passed the testing
900 stencil_and_ztest_pixels( GLcontext
*ctx
, SWspan
*span
, GLuint face
)
902 GLubyte passMask
[MAX_WIDTH
], failMask
[MAX_WIDTH
], origMask
[MAX_WIDTH
];
903 struct gl_framebuffer
*fb
= ctx
->DrawBuffer
;
904 struct gl_renderbuffer
*rb
= fb
->_StencilBuffer
;
905 const GLuint n
= span
->end
;
906 const GLint
*x
= span
->array
->x
;
907 const GLint
*y
= span
->array
->y
;
908 GLubyte
*mask
= span
->array
->mask
;
910 ASSERT(span
->arrayMask
& SPAN_XY
);
911 ASSERT(ctx
->Stencil
.Enabled
);
912 ASSERT(n
<= MAX_WIDTH
);
914 if (!rb
->GetPointer(ctx
, rb
, 0, 0)) {
915 /* No direct access */
916 GLstencil stencil
[MAX_WIDTH
];
918 ASSERT(rb
->DataType
== GL_UNSIGNED_BYTE
);
919 _swrast_get_values(ctx
, rb
, n
, x
, y
, stencil
, sizeof(GLubyte
));
921 _mesa_memcpy(origMask
, mask
, n
* sizeof(GLubyte
));
923 (void) do_stencil_test(ctx
, face
, n
, stencil
, mask
);
925 if (ctx
->Depth
.Test
== GL_FALSE
) {
926 apply_stencil_op(ctx
, ctx
->Stencil
.ZPassFunc
[face
], face
,
930 GLubyte tmpMask
[MAX_WIDTH
];
931 _mesa_memcpy(tmpMask
, mask
, n
* sizeof(GLubyte
));
933 _swrast_depth_test_span(ctx
, span
);
935 compute_pass_fail_masks(n
, tmpMask
, mask
, passMask
, failMask
);
937 if (ctx
->Stencil
.ZFailFunc
[face
] != GL_KEEP
) {
938 apply_stencil_op(ctx
, ctx
->Stencil
.ZFailFunc
[face
], face
,
939 n
, stencil
, failMask
);
941 if (ctx
->Stencil
.ZPassFunc
[face
] != GL_KEEP
) {
942 apply_stencil_op(ctx
, ctx
->Stencil
.ZPassFunc
[face
], face
,
943 n
, stencil
, passMask
);
947 /* Write updated stencil values into hardware stencil buffer */
948 rb
->PutValues(ctx
, rb
, n
, x
, y
, stencil
, origMask
);
953 /* Direct access to stencil buffer */
955 if (stencil_test_pixels(ctx
, face
, n
, x
, y
, mask
) == GL_FALSE
) {
956 /* all fragments failed the stencil test, we're done. */
960 if (ctx
->Depth
.Test
==GL_FALSE
) {
961 apply_stencil_op_to_pixels(ctx
, n
, x
, y
,
962 ctx
->Stencil
.ZPassFunc
[face
], face
, mask
);
965 _mesa_memcpy(origMask
, mask
, n
* sizeof(GLubyte
));
967 _swrast_depth_test_span(ctx
, span
);
969 compute_pass_fail_masks(n
, origMask
, mask
, passMask
, failMask
);
971 if (ctx
->Stencil
.ZFailFunc
[face
] != GL_KEEP
) {
972 apply_stencil_op_to_pixels(ctx
, n
, x
, y
,
973 ctx
->Stencil
.ZFailFunc
[face
],
976 if (ctx
->Stencil
.ZPassFunc
[face
] != GL_KEEP
) {
977 apply_stencil_op_to_pixels(ctx
, n
, x
, y
,
978 ctx
->Stencil
.ZPassFunc
[face
],
983 return GL_TRUE
; /* one or more fragments passed both tests */
989 * /return GL_TRUE = one or more fragments passed,
990 * GL_FALSE = all fragments failed.
993 _swrast_stencil_and_ztest_span(GLcontext
*ctx
, SWspan
*span
)
995 const GLuint face
= (span
->facing
== 0) ? 0 : ctx
->Stencil
._BackFace
;
997 if (span
->arrayMask
& SPAN_XY
)
998 return stencil_and_ztest_pixels(ctx
, span
, face
);
1000 return stencil_and_ztest_span(ctx
, span
, face
);
1006 clip_span(GLuint bufferWidth
, GLuint bufferHeight
,
1007 GLint x
, GLint y
, GLuint
*count
)
1010 GLuint skipPixels
= 0;
1012 if (y
< 0 || y
>= bufferHeight
|| x
+ n
<= 0 || x
>= bufferWidth
) {
1013 /* totally out of bounds */
1024 if (x
+ n
> bufferWidth
) {
1025 GLint dx
= x
+ n
- bufferWidth
;
1038 * Return a span of stencil values from the stencil buffer.
1039 * Used for glRead/CopyPixels
1040 * Input: n - how many pixels
1041 * x,y - location of first pixel
1042 * Output: stencil - the array of stencil values
1045 _swrast_read_stencil_span(GLcontext
*ctx
, struct gl_renderbuffer
*rb
,
1046 GLint n
, GLint x
, GLint y
, GLstencil stencil
[])
1048 if (y
< 0 || y
>= (GLint
) rb
->Height
||
1049 x
+ n
<= 0 || x
>= (GLint
) rb
->Width
) {
1050 /* span is completely outside framebuffer */
1051 return; /* undefined values OK */
1060 if (x
+ n
> (GLint
) rb
->Width
) {
1061 GLint dx
= x
+ n
- rb
->Width
;
1068 rb
->GetRow(ctx
, rb
, n
, x
, y
, stencil
);
1074 * Write a span of stencil values to the stencil buffer. This function
1075 * applies the stencil write mask when needed.
1076 * Used for glDraw/CopyPixels
1077 * Input: n - how many pixels
1078 * x, y - location of first pixel
1079 * stencil - the array of stencil values
1082 _swrast_write_stencil_span(GLcontext
*ctx
, GLint n
, GLint x
, GLint y
,
1083 const GLstencil stencil
[] )
1085 struct gl_framebuffer
*fb
= ctx
->DrawBuffer
;
1086 struct gl_renderbuffer
*rb
= fb
->_StencilBuffer
;
1087 const GLuint stencilMax
= (1 << fb
->Visual
.stencilBits
) - 1;
1088 const GLuint stencilMask
= ctx
->Stencil
.WriteMask
[0];
1090 if (y
< 0 || y
>= (GLint
) rb
->Height
||
1091 x
+ n
<= 0 || x
>= (GLint
) rb
->Width
) {
1092 /* span is completely outside framebuffer */
1093 return; /* undefined values OK */
1101 if (x
+ n
> (GLint
) rb
->Width
) {
1102 GLint dx
= x
+ n
- rb
->Width
;
1109 if ((stencilMask
& stencilMax
) != stencilMax
) {
1110 /* need to apply writemask */
1111 GLstencil destVals
[MAX_WIDTH
], newVals
[MAX_WIDTH
];
1113 rb
->GetRow(ctx
, rb
, n
, x
, y
, destVals
);
1114 for (i
= 0; i
< n
; i
++) {
1116 = (stencil
[i
] & stencilMask
) | (destVals
[i
] & ~stencilMask
);
1118 rb
->PutRow(ctx
, rb
, n
, x
, y
, newVals
, NULL
);
1121 rb
->PutRow(ctx
, rb
, n
, x
, y
, stencil
, NULL
);
1128 * Clear the stencil buffer.
1131 _swrast_clear_stencil_buffer( GLcontext
*ctx
, struct gl_renderbuffer
*rb
)
1133 const GLubyte stencilBits
= ctx
->DrawBuffer
->Visual
.stencilBits
;
1134 const GLuint mask
= ctx
->Stencil
.WriteMask
[0];
1135 const GLuint invMask
= ~mask
;
1136 const GLuint clearVal
= (ctx
->Stencil
.Clear
& mask
);
1137 const GLuint stencilMax
= (1 << stencilBits
) - 1;
1138 GLint x
, y
, width
, height
;
1140 if (!rb
|| mask
== 0)
1143 ASSERT(rb
->DataType
== GL_UNSIGNED_BYTE
||
1144 rb
->DataType
== GL_UNSIGNED_SHORT
);
1146 ASSERT(rb
->_BaseFormat
== GL_STENCIL_INDEX
);
1148 /* compute region to clear */
1149 x
= ctx
->DrawBuffer
->_Xmin
;
1150 y
= ctx
->DrawBuffer
->_Ymin
;
1151 width
= ctx
->DrawBuffer
->_Xmax
- ctx
->DrawBuffer
->_Xmin
;
1152 height
= ctx
->DrawBuffer
->_Ymax
- ctx
->DrawBuffer
->_Ymin
;
1154 if (rb
->GetPointer(ctx
, rb
, 0, 0)) {
1155 /* Direct buffer access */
1156 if ((mask
& stencilMax
) != stencilMax
) {
1157 /* need to mask the clear */
1158 if (rb
->DataType
== GL_UNSIGNED_BYTE
) {
1160 for (i
= 0; i
< height
; i
++) {
1161 GLubyte
*stencil
= (GLubyte
*) rb
->GetPointer(ctx
, rb
, x
, y
+ i
);
1162 for (j
= 0; j
< width
; j
++) {
1163 stencil
[j
] = (stencil
[j
] & invMask
) | clearVal
;
1169 for (i
= 0; i
< height
; i
++) {
1170 GLushort
*stencil
= (GLushort
*) rb
->GetPointer(ctx
, rb
, x
, y
+ i
);
1171 for (j
= 0; j
< width
; j
++) {
1172 stencil
[j
] = (stencil
[j
] & invMask
) | clearVal
;
1178 /* no bit masking */
1179 if (width
== (GLint
) rb
->Width
&& rb
->DataType
== GL_UNSIGNED_BYTE
) {
1180 /* optimized case */
1181 /* Note: bottom-to-top raster assumed! */
1182 GLubyte
*stencil
= (GLubyte
*) rb
->GetPointer(ctx
, rb
, x
, y
);
1183 GLuint len
= width
* height
* sizeof(GLubyte
);
1184 _mesa_memset(stencil
, clearVal
, len
);
1189 for (i
= 0; i
< height
; i
++) {
1190 GLvoid
*stencil
= rb
->GetPointer(ctx
, rb
, x
, y
+ i
);
1191 if (rb
->DataType
== GL_UNSIGNED_BYTE
) {
1192 _mesa_memset(stencil
, clearVal
, width
);
1195 _mesa_memset16((short unsigned int*) stencil
, clearVal
, width
);
1202 /* no direct access */
1203 if ((mask
& stencilMax
) != stencilMax
) {
1204 /* need to mask the clear */
1205 if (rb
->DataType
== GL_UNSIGNED_BYTE
) {
1207 for (i
= 0; i
< height
; i
++) {
1208 GLubyte stencil
[MAX_WIDTH
];
1209 rb
->GetRow(ctx
, rb
, width
, x
, y
+ i
, stencil
);
1210 for (j
= 0; j
< width
; j
++) {
1211 stencil
[j
] = (stencil
[j
] & invMask
) | clearVal
;
1213 rb
->PutRow(ctx
, rb
, width
, x
, y
+ i
, stencil
, NULL
);
1218 for (i
= 0; i
< height
; i
++) {
1219 GLushort stencil
[MAX_WIDTH
];
1220 rb
->GetRow(ctx
, rb
, width
, x
, y
+ i
, stencil
);
1221 for (j
= 0; j
< width
; j
++) {
1222 stencil
[j
] = (stencil
[j
] & invMask
) | clearVal
;
1224 rb
->PutRow(ctx
, rb
, width
, x
, y
+ i
, stencil
, NULL
);
1229 /* no bit masking */
1230 const GLubyte clear8
= (GLubyte
) clearVal
;
1231 const GLushort clear16
= (GLushort
) clearVal
;
1234 if (rb
->DataType
== GL_UNSIGNED_BYTE
) {
1240 for (i
= 0; i
< height
; i
++) {
1241 rb
->PutMonoRow(ctx
, rb
, width
, x
, y
+ i
, clear
, NULL
);