pass in the renderbuffer
[mesa.git] / src / mesa / swrast / s_masking.c
1 /*
2 * Mesa 3-D graphics library
3 * Version: 6.3
4 *
5 * Copyright (C) 1999-2005 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 "glheader.h"
32 #include "enums.h"
33 #include "macros.h"
34
35 #include "s_context.h"
36 #include "s_masking.h"
37 #include "s_span.h"
38
39
40
41 void
42 _swrast_mask_rgba_span(GLcontext *ctx, struct gl_renderbuffer *rb,
43 const struct sw_span *span, GLchan rgba[][4])
44 {
45 GLchan dest[MAX_WIDTH][4];
46 #if CHAN_BITS == 8
47 GLuint srcMask = *((GLuint*)ctx->Color.ColorMask);
48 GLuint dstMask = ~srcMask;
49 GLuint *rgba32 = (GLuint *) rgba;
50 GLuint *dest32 = (GLuint *) dest;
51 #else
52 const GLboolean rMask = ctx->Color.ColorMask[RCOMP];
53 const GLboolean gMask = ctx->Color.ColorMask[GCOMP];
54 const GLboolean bMask = ctx->Color.ColorMask[BCOMP];
55 const GLboolean aMask = ctx->Color.ColorMask[ACOMP];
56 #endif
57 const GLuint n = span->end;
58 GLuint i;
59
60 ASSERT(n < MAX_WIDTH);
61 ASSERT(span->arrayMask & SPAN_RGBA);
62
63 if (span->arrayMask & SPAN_XY) {
64 rb->GetValues(ctx, rb, n, span->array->x, span->array->y, dest);
65 }
66 else {
67 _swrast_read_rgba_span(ctx, rb, n, span->x, span->y, dest);
68 }
69
70 #if CHAN_BITS == 8
71 for (i = 0; i < n; i++) {
72 rgba32[i] = (rgba32[i] & srcMask) | (dest32[i] & dstMask);
73 }
74 #else
75 for (i = 0; i < n; i++) {
76 if (!rMask) rgba[i][RCOMP] = dest[i][RCOMP];
77 if (!gMask) rgba[i][GCOMP] = dest[i][GCOMP];
78 if (!bMask) rgba[i][BCOMP] = dest[i][BCOMP];
79 if (!aMask) rgba[i][ACOMP] = dest[i][ACOMP];
80 }
81 #endif
82 }
83
84
85 /*
86 * Apply glColorMask to a span of RGBA pixels.
87 */
88 void
89 _swrast_mask_rgba_array(GLcontext *ctx, struct gl_renderbuffer *rb,
90 GLuint n, GLint x, GLint y, GLchan rgba[][4])
91 {
92 GLchan dest[MAX_WIDTH][4];
93 GLuint i;
94
95 #if CHAN_BITS == 8
96
97 GLuint srcMask = *((GLuint*)ctx->Color.ColorMask);
98 GLuint dstMask = ~srcMask;
99 GLuint *rgba32 = (GLuint *) rgba;
100 GLuint *dest32 = (GLuint *) dest;
101
102 _swrast_read_rgba_span( ctx, rb, n, x, y, dest );
103 for (i = 0; i < n; i++) {
104 rgba32[i] = (rgba32[i] & srcMask) | (dest32[i] & dstMask);
105 }
106
107 #else
108
109 const GLint rMask = ctx->Color.ColorMask[RCOMP];
110 const GLint gMask = ctx->Color.ColorMask[GCOMP];
111 const GLint bMask = ctx->Color.ColorMask[BCOMP];
112 const GLint aMask = ctx->Color.ColorMask[ACOMP];
113
114 _swrast_read_rgba_span( ctx, rb, n, x, y, dest );
115 for (i = 0; i < n; i++) {
116 if (!rMask) rgba[i][RCOMP] = dest[i][RCOMP];
117 if (!gMask) rgba[i][GCOMP] = dest[i][GCOMP];
118 if (!bMask) rgba[i][BCOMP] = dest[i][BCOMP];
119 if (!aMask) rgba[i][ACOMP] = dest[i][ACOMP];
120 }
121
122 #endif
123 }
124
125
126
127 void
128 _swrast_mask_ci_span(GLcontext *ctx, struct gl_renderbuffer *rb,
129 const struct sw_span *span, GLuint index[])
130 {
131 const GLuint srcMask = ctx->Color.IndexMask;
132 const GLuint dstMask = ~srcMask;
133 GLuint dest[MAX_WIDTH];
134 GLuint i;
135
136 ASSERT(span->arrayMask & SPAN_INDEX);
137 ASSERT(span->end < MAX_WIDTH);
138 ASSERT(rb->DataType == GL_UNSIGNED_INT);
139
140 if (span->arrayMask & SPAN_XY) {
141 rb->GetValues(ctx, rb, span->end, span->array->x, span->array->y, dest);
142 }
143 else {
144 _swrast_read_index_span(ctx, rb, span->end, span->x, span->y, dest);
145 }
146
147 for (i = 0; i < span->end; i++) {
148 index[i] = (index[i] & srcMask) | (dest[i] & dstMask);
149 }
150 }
151
152
153 /*
154 * Apply glIndexMask to an array of CI pixels.
155 */
156 void
157 _swrast_mask_ci_array(GLcontext *ctx, struct gl_renderbuffer *rb,
158 GLuint n, GLint x, GLint y, GLuint index[])
159 {
160 const GLuint srcMask = ctx->Color.IndexMask;
161 const GLuint dstMask = ~srcMask;
162 GLuint dest[MAX_WIDTH];
163 GLuint i;
164
165 _swrast_read_index_span(ctx, rb, n, x, y, dest);
166
167 for (i=0;i<n;i++) {
168 index[i] = (index[i] & srcMask) | (dest[i] & dstMask);
169 }
170 }