Merge branch 'mesa_7_7_branch'
[mesa.git] / src / mesa / swrast / s_masking.c
1 /*
2 * Mesa 3-D graphics library
3 * Version: 6.5.2
4 *
5 * Copyright (C) 1999-2006 Brian Paul All Rights Reserved.
6 *
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:
13 *
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
16 *
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.
23 */
24
25
26 /*
27 * Implement the effect of glColorMask and glIndexMask in software.
28 */
29
30
31 #include "main/glheader.h"
32 #include "main/macros.h"
33
34 #include "s_context.h"
35 #include "s_masking.h"
36 #include "s_span.h"
37
38
39 /**
40 * Apply the color mask to a span of rgba values.
41 */
42 void
43 _swrast_mask_rgba_span(GLcontext *ctx, struct gl_renderbuffer *rb,
44 SWspan *span, GLuint buf)
45 {
46 const GLuint n = span->end;
47 void *rbPixels;
48
49 ASSERT(n < MAX_WIDTH);
50 ASSERT(span->arrayMask & SPAN_RGBA);
51 ASSERT(rb->DataType == span->array->ChanType);
52
53 rbPixels = _swrast_get_dest_rgba(ctx, rb, span);
54
55 /*
56 * Do component masking.
57 * Note that we're not using span->array->mask[] here. We could...
58 */
59 if (span->array->ChanType == GL_UNSIGNED_BYTE) {
60 /* treat 4xGLubyte as 1xGLuint */
61 const GLuint srcMask = *((GLuint *) ctx->Color.ColorMask[buf]);
62 const GLuint dstMask = ~srcMask;
63 const GLuint *dst = (const GLuint *) rbPixels;
64 GLuint *src = (GLuint *) span->array->rgba8;
65 GLuint i;
66 for (i = 0; i < n; i++) {
67 src[i] = (src[i] & srcMask) | (dst[i] & dstMask);
68 }
69 }
70 else if (span->array->ChanType == GL_UNSIGNED_SHORT) {
71 /* 2-byte components */
72 /* XXX try to use 64-bit arithmetic someday */
73 const GLushort rMask = ctx->Color.ColorMask[buf][RCOMP] ? 0xffff : 0x0;
74 const GLushort gMask = ctx->Color.ColorMask[buf][GCOMP] ? 0xffff : 0x0;
75 const GLushort bMask = ctx->Color.ColorMask[buf][BCOMP] ? 0xffff : 0x0;
76 const GLushort aMask = ctx->Color.ColorMask[buf][ACOMP] ? 0xffff : 0x0;
77 const GLushort (*dst)[4] = (const GLushort (*)[4]) rbPixels;
78 GLushort (*src)[4] = span->array->rgba16;
79 GLuint i;
80 for (i = 0; i < n; i++) {
81 src[i][RCOMP] = (src[i][RCOMP] & rMask) | (dst[i][RCOMP] & ~rMask);
82 src[i][GCOMP] = (src[i][GCOMP] & gMask) | (dst[i][GCOMP] & ~gMask);
83 src[i][BCOMP] = (src[i][BCOMP] & bMask) | (dst[i][BCOMP] & ~bMask);
84 src[i][ACOMP] = (src[i][ACOMP] & aMask) | (dst[i][ACOMP] & ~aMask);
85 }
86 }
87 else {
88 /* 4-byte components */
89 const GLuint rMask = ctx->Color.ColorMask[buf][RCOMP] ? ~0x0 : 0x0;
90 const GLuint gMask = ctx->Color.ColorMask[buf][GCOMP] ? ~0x0 : 0x0;
91 const GLuint bMask = ctx->Color.ColorMask[buf][BCOMP] ? ~0x0 : 0x0;
92 const GLuint aMask = ctx->Color.ColorMask[buf][ACOMP] ? ~0x0 : 0x0;
93 const GLuint (*dst)[4] = (const GLuint (*)[4]) rbPixels;
94 GLuint (*src)[4] = (GLuint (*)[4]) span->array->attribs[FRAG_ATTRIB_COL0];
95 GLuint i;
96 for (i = 0; i < n; i++) {
97 src[i][RCOMP] = (src[i][RCOMP] & rMask) | (dst[i][RCOMP] & ~rMask);
98 src[i][GCOMP] = (src[i][GCOMP] & gMask) | (dst[i][GCOMP] & ~gMask);
99 src[i][BCOMP] = (src[i][BCOMP] & bMask) | (dst[i][BCOMP] & ~bMask);
100 src[i][ACOMP] = (src[i][ACOMP] & aMask) | (dst[i][ACOMP] & ~aMask);
101 }
102 }
103 }
104
105
106 /**
107 * Apply the index mask to a span of color index values.
108 */
109 void
110 _swrast_mask_ci_span(GLcontext *ctx, struct gl_renderbuffer *rb,
111 SWspan *span)
112 {
113 const GLuint srcMask = ctx->Color.IndexMask;
114 const GLuint dstMask = ~srcMask;
115 GLuint *index = span->array->index;
116 GLuint dest[MAX_WIDTH];
117 GLuint i;
118
119 ASSERT(span->arrayMask & SPAN_INDEX);
120 ASSERT(span->end <= MAX_WIDTH);
121 ASSERT(rb->DataType == GL_UNSIGNED_INT);
122
123 if (span->arrayMask & SPAN_XY) {
124 _swrast_get_values(ctx, rb, span->end, span->array->x, span->array->y,
125 dest, sizeof(GLuint));
126 }
127 else {
128 _swrast_read_index_span(ctx, rb, span->end, span->x, span->y, dest);
129 }
130
131 for (i = 0; i < span->end; i++) {
132 index[i] = (index[i] & srcMask) | (dest[i] & dstMask);
133 }
134 }