egl: rename _eglMatchDriver() to _eglInitializeDisplay()
[mesa.git] / src / mesa / swrast / s_masking.c
1 /*
2 * Mesa 3-D graphics library
3 *
4 * Copyright (C) 1999-2006 Brian Paul All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included
14 * in all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
20 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22 * 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(struct gl_context *ctx, struct gl_renderbuffer *rb,
44 SWspan *span, GLuint buf)
45 {
46 const GLuint n = span->end;
47 void *rbPixels;
48
49 assert(n < SWRAST_MAX_WIDTH);
50 assert(span->arrayMask & SPAN_RGBA);
51
52 rbPixels = _swrast_get_dest_rgba(ctx, rb, span);
53
54 /*
55 * Do component masking.
56 * Note that we're not using span->array->mask[] here. We could...
57 */
58 if (span->array->ChanType == GL_UNSIGNED_BYTE) {
59 const GLubyte colormask[4] = {
60 GET_COLORMASK_BIT(ctx->Color.ColorMask, buf, 0) ? 0xff : 0,
61 GET_COLORMASK_BIT(ctx->Color.ColorMask, buf, 1) ? 0xff : 0,
62 GET_COLORMASK_BIT(ctx->Color.ColorMask, buf, 2) ? 0xff : 0,
63 GET_COLORMASK_BIT(ctx->Color.ColorMask, buf, 3) ? 0xff : 0,
64 };
65 GLuint srcMask;
66 memcpy(&srcMask, colormask, sizeof(srcMask));
67 const GLuint dstMask = ~srcMask;
68 const GLuint *dst = (const GLuint *) rbPixels;
69 GLuint *src = (GLuint *) span->array->rgba8;
70 GLuint i;
71 for (i = 0; i < n; i++) {
72 src[i] = (src[i] & srcMask) | (dst[i] & dstMask);
73 }
74 }
75 else if (span->array->ChanType == GL_UNSIGNED_SHORT) {
76 /* 2-byte components */
77 /* XXX try to use 64-bit arithmetic someday */
78 const GLushort rMask = GET_COLORMASK_BIT(ctx->Color.ColorMask, buf, 0) ? 0xffff : 0x0;
79 const GLushort gMask = GET_COLORMASK_BIT(ctx->Color.ColorMask, buf, 1) ? 0xffff : 0x0;
80 const GLushort bMask = GET_COLORMASK_BIT(ctx->Color.ColorMask, buf, 2) ? 0xffff : 0x0;
81 const GLushort aMask = GET_COLORMASK_BIT(ctx->Color.ColorMask, buf, 3) ? 0xffff : 0x0;
82 const GLushort (*dst)[4] = (const GLushort (*)[4]) rbPixels;
83 GLushort (*src)[4] = span->array->rgba16;
84 GLuint i;
85 for (i = 0; i < n; i++) {
86 src[i][RCOMP] = (src[i][RCOMP] & rMask) | (dst[i][RCOMP] & ~rMask);
87 src[i][GCOMP] = (src[i][GCOMP] & gMask) | (dst[i][GCOMP] & ~gMask);
88 src[i][BCOMP] = (src[i][BCOMP] & bMask) | (dst[i][BCOMP] & ~bMask);
89 src[i][ACOMP] = (src[i][ACOMP] & aMask) | (dst[i][ACOMP] & ~aMask);
90 }
91 }
92 else {
93 /* 4-byte components */
94 const GLuint rMask = GET_COLORMASK_BIT(ctx->Color.ColorMask, buf, 0) ? ~0x0 : 0x0;
95 const GLuint gMask = GET_COLORMASK_BIT(ctx->Color.ColorMask, buf, 1) ? ~0x0 : 0x0;
96 const GLuint bMask = GET_COLORMASK_BIT(ctx->Color.ColorMask, buf, 2) ? ~0x0 : 0x0;
97 const GLuint aMask = GET_COLORMASK_BIT(ctx->Color.ColorMask, buf, 3) ? ~0x0 : 0x0;
98 const GLuint (*dst)[4] = (const GLuint (*)[4]) rbPixels;
99 GLuint (*src)[4] = (GLuint (*)[4]) span->array->attribs[VARYING_SLOT_COL0];
100 GLuint i;
101 for (i = 0; i < n; i++) {
102 src[i][RCOMP] = (src[i][RCOMP] & rMask) | (dst[i][RCOMP] & ~rMask);
103 src[i][GCOMP] = (src[i][GCOMP] & gMask) | (dst[i][GCOMP] & ~gMask);
104 src[i][BCOMP] = (src[i][BCOMP] & bMask) | (dst[i][BCOMP] & ~bMask);
105 src[i][ACOMP] = (src[i][ACOMP] & aMask) | (dst[i][ACOMP] & ~aMask);
106 }
107 }
108 }