1848b8605Smrg/*
2848b8605Smrg * Mesa 3-D graphics library
3848b8605Smrg *
4848b8605Smrg * Copyright (C) 1999-2006  Brian Paul   All Rights Reserved.
5848b8605Smrg *
6848b8605Smrg * Permission is hereby granted, free of charge, to any person obtaining a
7848b8605Smrg * copy of this software and associated documentation files (the "Software"),
8848b8605Smrg * to deal in the Software without restriction, including without limitation
9848b8605Smrg * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10848b8605Smrg * and/or sell copies of the Software, and to permit persons to whom the
11848b8605Smrg * Software is furnished to do so, subject to the following conditions:
12848b8605Smrg *
13848b8605Smrg * The above copyright notice and this permission notice shall be included
14848b8605Smrg * in all copies or substantial portions of the Software.
15848b8605Smrg *
16848b8605Smrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17848b8605Smrg * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18848b8605Smrg * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
19848b8605Smrg * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
20848b8605Smrg * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21848b8605Smrg * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22848b8605Smrg * OTHER DEALINGS IN THE SOFTWARE.
23848b8605Smrg */
24848b8605Smrg
25848b8605Smrg
26848b8605Smrg/*
27848b8605Smrg * Implement the effect of glColorMask and glIndexMask in software.
28848b8605Smrg */
29848b8605Smrg
30848b8605Smrg
31848b8605Smrg#include "main/glheader.h"
32848b8605Smrg#include "main/macros.h"
33848b8605Smrg
34848b8605Smrg#include "s_context.h"
35848b8605Smrg#include "s_masking.h"
36848b8605Smrg#include "s_span.h"
37848b8605Smrg
38848b8605Smrg
39848b8605Smrg/**
40848b8605Smrg * Apply the color mask to a span of rgba values.
41848b8605Smrg */
42848b8605Smrgvoid
43848b8605Smrg_swrast_mask_rgba_span(struct gl_context *ctx, struct gl_renderbuffer *rb,
44848b8605Smrg                       SWspan *span, GLuint buf)
45848b8605Smrg{
46848b8605Smrg   const GLuint n = span->end;
47848b8605Smrg   void *rbPixels;
48848b8605Smrg
49b8e80941Smrg   assert(n < SWRAST_MAX_WIDTH);
50b8e80941Smrg   assert(span->arrayMask & SPAN_RGBA);
51848b8605Smrg
52848b8605Smrg   rbPixels = _swrast_get_dest_rgba(ctx, rb, span);
53848b8605Smrg
54848b8605Smrg   /*
55848b8605Smrg    * Do component masking.
56848b8605Smrg    * Note that we're not using span->array->mask[] here.  We could...
57848b8605Smrg    */
58848b8605Smrg   if (span->array->ChanType == GL_UNSIGNED_BYTE) {
59b8e80941Smrg      const GLubyte colormask[4] = {
60b8e80941Smrg         GET_COLORMASK_BIT(ctx->Color.ColorMask, buf, 0) ? 0xff : 0,
61b8e80941Smrg         GET_COLORMASK_BIT(ctx->Color.ColorMask, buf, 1) ? 0xff : 0,
62b8e80941Smrg         GET_COLORMASK_BIT(ctx->Color.ColorMask, buf, 2) ? 0xff : 0,
63b8e80941Smrg         GET_COLORMASK_BIT(ctx->Color.ColorMask, buf, 3) ? 0xff : 0,
64b8e80941Smrg      };
65b8e80941Smrg      GLuint srcMask;
66b8e80941Smrg      memcpy(&srcMask, colormask, sizeof(srcMask));
67848b8605Smrg      const GLuint dstMask = ~srcMask;
68848b8605Smrg      const GLuint *dst = (const GLuint *) rbPixels;
69848b8605Smrg      GLuint *src = (GLuint *) span->array->rgba8;
70848b8605Smrg      GLuint i;
71848b8605Smrg      for (i = 0; i < n; i++) {
72848b8605Smrg         src[i] = (src[i] & srcMask) | (dst[i] & dstMask);
73848b8605Smrg      }
74848b8605Smrg   }
75848b8605Smrg   else if (span->array->ChanType == GL_UNSIGNED_SHORT) {
76848b8605Smrg      /* 2-byte components */
77848b8605Smrg      /* XXX try to use 64-bit arithmetic someday */
78b8e80941Smrg      const GLushort rMask = GET_COLORMASK_BIT(ctx->Color.ColorMask, buf, 0) ? 0xffff : 0x0;
79b8e80941Smrg      const GLushort gMask = GET_COLORMASK_BIT(ctx->Color.ColorMask, buf, 1) ? 0xffff : 0x0;
80b8e80941Smrg      const GLushort bMask = GET_COLORMASK_BIT(ctx->Color.ColorMask, buf, 2) ? 0xffff : 0x0;
81b8e80941Smrg      const GLushort aMask = GET_COLORMASK_BIT(ctx->Color.ColorMask, buf, 3) ? 0xffff : 0x0;
82848b8605Smrg      const GLushort (*dst)[4] = (const GLushort (*)[4]) rbPixels;
83848b8605Smrg      GLushort (*src)[4] = span->array->rgba16;
84848b8605Smrg      GLuint i;
85848b8605Smrg      for (i = 0; i < n; i++) {
86848b8605Smrg         src[i][RCOMP] = (src[i][RCOMP] & rMask) | (dst[i][RCOMP] & ~rMask);
87848b8605Smrg         src[i][GCOMP] = (src[i][GCOMP] & gMask) | (dst[i][GCOMP] & ~gMask);
88848b8605Smrg         src[i][BCOMP] = (src[i][BCOMP] & bMask) | (dst[i][BCOMP] & ~bMask);
89848b8605Smrg         src[i][ACOMP] = (src[i][ACOMP] & aMask) | (dst[i][ACOMP] & ~aMask);
90848b8605Smrg      }
91848b8605Smrg   }
92848b8605Smrg   else {
93848b8605Smrg      /* 4-byte components */
94b8e80941Smrg      const GLuint rMask = GET_COLORMASK_BIT(ctx->Color.ColorMask, buf, 0) ? ~0x0 : 0x0;
95b8e80941Smrg      const GLuint gMask = GET_COLORMASK_BIT(ctx->Color.ColorMask, buf, 1) ? ~0x0 : 0x0;
96b8e80941Smrg      const GLuint bMask = GET_COLORMASK_BIT(ctx->Color.ColorMask, buf, 2) ? ~0x0 : 0x0;
97b8e80941Smrg      const GLuint aMask = GET_COLORMASK_BIT(ctx->Color.ColorMask, buf, 3) ? ~0x0 : 0x0;
98848b8605Smrg      const GLuint (*dst)[4] = (const GLuint (*)[4]) rbPixels;
99848b8605Smrg      GLuint (*src)[4] = (GLuint (*)[4]) span->array->attribs[VARYING_SLOT_COL0];
100848b8605Smrg      GLuint i;
101848b8605Smrg      for (i = 0; i < n; i++) {
102848b8605Smrg         src[i][RCOMP] = (src[i][RCOMP] & rMask) | (dst[i][RCOMP] & ~rMask);
103848b8605Smrg         src[i][GCOMP] = (src[i][GCOMP] & gMask) | (dst[i][GCOMP] & ~gMask);
104848b8605Smrg         src[i][BCOMP] = (src[i][BCOMP] & bMask) | (dst[i][BCOMP] & ~bMask);
105848b8605Smrg         src[i][ACOMP] = (src[i][ACOMP] & aMask) | (dst[i][ACOMP] & ~aMask);
106848b8605Smrg      }
107848b8605Smrg   }
108848b8605Smrg}
109