1e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell/*
2e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell * Mesa 3-D graphics library
343715c711d2c1d1e7624cd7c9c8a44b8866510fdBrian Paul * Version:  6.5.2
422144ab7552f0799bcfca506bf4ffa7f70a06649Gareth Hughes *
543715c711d2c1d1e7624cd7c9c8a44b8866510fdBrian Paul * Copyright (C) 1999-2006  Brian Paul   All Rights Reserved.
622144ab7552f0799bcfca506bf4ffa7f70a06649Gareth Hughes *
7e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell * Permission is hereby granted, free of charge, to any person obtaining a
8e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell * copy of this software and associated documentation files (the "Software"),
9e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell * to deal in the Software without restriction, including without limitation
10e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell * and/or sell copies of the Software, and to permit persons to whom the
12e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell * Software is furnished to do so, subject to the following conditions:
1322144ab7552f0799bcfca506bf4ffa7f70a06649Gareth Hughes *
14e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell * The above copyright notice and this permission notice shall be included
15e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell * in all copies or substantial portions of the Software.
1622144ab7552f0799bcfca506bf4ffa7f70a06649Gareth Hughes *
17e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
20e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
21e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell */
24e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell
25e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell
26e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell/*
27e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell * Implement the effect of glColorMask and glIndexMask in software.
28e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell */
29e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell
30e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell
31bbd287103dad776d8a45c87c4e51fbc26d9b80d5Brian Paul#include "main/glheader.h"
32bbd287103dad776d8a45c87c4e51fbc26d9b80d5Brian Paul#include "main/macros.h"
33e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell
34cd03ed4f54444d96e4e47cdb118a3dfd94d92bb0Keith Whitwell#include "s_context.h"
35e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell#include "s_masking.h"
36e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell#include "s_span.h"
37e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell
38e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell
3943715c711d2c1d1e7624cd7c9c8a44b8866510fdBrian Paul/**
4043715c711d2c1d1e7624cd7c9c8a44b8866510fdBrian Paul * Apply the color mask to a span of rgba values.
4143715c711d2c1d1e7624cd7c9c8a44b8866510fdBrian Paul */
42733a4b602bbbfda83ee03b7ae4f3737bbe659034Brian Paulvoid
43f9995b30756140724f41daf963fa06167912be7fKristian Høgsberg_swrast_mask_rgba_span(struct gl_context *ctx, struct gl_renderbuffer *rb,
44fd5511d27fc44096117c47ab503fb5b47f993061Brian Paul                       SWspan *span, GLuint buf)
45733a4b602bbbfda83ee03b7ae4f3737bbe659034Brian Paul{
46733a4b602bbbfda83ee03b7ae4f3737bbe659034Brian Paul   const GLuint n = span->end;
47a50b7dbc3ba1db8c92b4bbb4f7f06de8d6c039c4Brian Paul   void *rbPixels;
48733a4b602bbbfda83ee03b7ae4f3737bbe659034Brian Paul
4947d88ef204b42a9220c6be3e98c92df9c9aa0860Brian Paul   ASSERT(n < SWRAST_MAX_WIDTH);
50733a4b602bbbfda83ee03b7ae4f3737bbe659034Brian Paul   ASSERT(span->arrayMask & SPAN_RGBA);
51733a4b602bbbfda83ee03b7ae4f3737bbe659034Brian Paul
52a50b7dbc3ba1db8c92b4bbb4f7f06de8d6c039c4Brian Paul   rbPixels = _swrast_get_dest_rgba(ctx, rb, span);
53733a4b602bbbfda83ee03b7ae4f3737bbe659034Brian Paul
54a50b7dbc3ba1db8c92b4bbb4f7f06de8d6c039c4Brian Paul   /*
55a50b7dbc3ba1db8c92b4bbb4f7f06de8d6c039c4Brian Paul    * Do component masking.
56a50b7dbc3ba1db8c92b4bbb4f7f06de8d6c039c4Brian Paul    * Note that we're not using span->array->mask[] here.  We could...
57a50b7dbc3ba1db8c92b4bbb4f7f06de8d6c039c4Brian Paul    */
58a50b7dbc3ba1db8c92b4bbb4f7f06de8d6c039c4Brian Paul   if (span->array->ChanType == GL_UNSIGNED_BYTE) {
59a50b7dbc3ba1db8c92b4bbb4f7f06de8d6c039c4Brian Paul      /* treat 4xGLubyte as 1xGLuint */
60fd5511d27fc44096117c47ab503fb5b47f993061Brian Paul      const GLuint srcMask = *((GLuint *) ctx->Color.ColorMask[buf]);
61a50b7dbc3ba1db8c92b4bbb4f7f06de8d6c039c4Brian Paul      const GLuint dstMask = ~srcMask;
62a50b7dbc3ba1db8c92b4bbb4f7f06de8d6c039c4Brian Paul      const GLuint *dst = (const GLuint *) rbPixels;
639e8a961dd7d7b717a9fb4ecdea1c1b60ea355efeBrian      GLuint *src = (GLuint *) span->array->rgba8;
64a50b7dbc3ba1db8c92b4bbb4f7f06de8d6c039c4Brian Paul      GLuint i;
65a50b7dbc3ba1db8c92b4bbb4f7f06de8d6c039c4Brian Paul      for (i = 0; i < n; i++) {
66a50b7dbc3ba1db8c92b4bbb4f7f06de8d6c039c4Brian Paul         src[i] = (src[i] & srcMask) | (dst[i] & dstMask);
67a50b7dbc3ba1db8c92b4bbb4f7f06de8d6c039c4Brian Paul      }
68a50b7dbc3ba1db8c92b4bbb4f7f06de8d6c039c4Brian Paul   }
69a50b7dbc3ba1db8c92b4bbb4f7f06de8d6c039c4Brian Paul   else if (span->array->ChanType == GL_UNSIGNED_SHORT) {
70a50b7dbc3ba1db8c92b4bbb4f7f06de8d6c039c4Brian Paul      /* 2-byte components */
71a50b7dbc3ba1db8c92b4bbb4f7f06de8d6c039c4Brian Paul      /* XXX try to use 64-bit arithmetic someday */
72fd5511d27fc44096117c47ab503fb5b47f993061Brian Paul      const GLushort rMask = ctx->Color.ColorMask[buf][RCOMP] ? 0xffff : 0x0;
73fd5511d27fc44096117c47ab503fb5b47f993061Brian Paul      const GLushort gMask = ctx->Color.ColorMask[buf][GCOMP] ? 0xffff : 0x0;
74fd5511d27fc44096117c47ab503fb5b47f993061Brian Paul      const GLushort bMask = ctx->Color.ColorMask[buf][BCOMP] ? 0xffff : 0x0;
75fd5511d27fc44096117c47ab503fb5b47f993061Brian Paul      const GLushort aMask = ctx->Color.ColorMask[buf][ACOMP] ? 0xffff : 0x0;
76a50b7dbc3ba1db8c92b4bbb4f7f06de8d6c039c4Brian Paul      const GLushort (*dst)[4] = (const GLushort (*)[4]) rbPixels;
779e8a961dd7d7b717a9fb4ecdea1c1b60ea355efeBrian      GLushort (*src)[4] = span->array->rgba16;
78a50b7dbc3ba1db8c92b4bbb4f7f06de8d6c039c4Brian Paul      GLuint i;
79a50b7dbc3ba1db8c92b4bbb4f7f06de8d6c039c4Brian Paul      for (i = 0; i < n; i++) {
80a50b7dbc3ba1db8c92b4bbb4f7f06de8d6c039c4Brian Paul         src[i][RCOMP] = (src[i][RCOMP] & rMask) | (dst[i][RCOMP] & ~rMask);
81a50b7dbc3ba1db8c92b4bbb4f7f06de8d6c039c4Brian Paul         src[i][GCOMP] = (src[i][GCOMP] & gMask) | (dst[i][GCOMP] & ~gMask);
82a50b7dbc3ba1db8c92b4bbb4f7f06de8d6c039c4Brian Paul         src[i][BCOMP] = (src[i][BCOMP] & bMask) | (dst[i][BCOMP] & ~bMask);
83a50b7dbc3ba1db8c92b4bbb4f7f06de8d6c039c4Brian Paul         src[i][ACOMP] = (src[i][ACOMP] & aMask) | (dst[i][ACOMP] & ~aMask);
84a50b7dbc3ba1db8c92b4bbb4f7f06de8d6c039c4Brian Paul      }
85733a4b602bbbfda83ee03b7ae4f3737bbe659034Brian Paul   }
86a50b7dbc3ba1db8c92b4bbb4f7f06de8d6c039c4Brian Paul   else {
87a50b7dbc3ba1db8c92b4bbb4f7f06de8d6c039c4Brian Paul      /* 4-byte components */
88fd5511d27fc44096117c47ab503fb5b47f993061Brian Paul      const GLuint rMask = ctx->Color.ColorMask[buf][RCOMP] ? ~0x0 : 0x0;
89fd5511d27fc44096117c47ab503fb5b47f993061Brian Paul      const GLuint gMask = ctx->Color.ColorMask[buf][GCOMP] ? ~0x0 : 0x0;
90fd5511d27fc44096117c47ab503fb5b47f993061Brian Paul      const GLuint bMask = ctx->Color.ColorMask[buf][BCOMP] ? ~0x0 : 0x0;
91fd5511d27fc44096117c47ab503fb5b47f993061Brian Paul      const GLuint aMask = ctx->Color.ColorMask[buf][ACOMP] ? ~0x0 : 0x0;
92a50b7dbc3ba1db8c92b4bbb4f7f06de8d6c039c4Brian Paul      const GLuint (*dst)[4] = (const GLuint (*)[4]) rbPixels;
93f3e507ef9f75dbfc58ccd07b5fe8cfca10d9a9e3Brian      GLuint (*src)[4] = (GLuint (*)[4]) span->array->attribs[FRAG_ATTRIB_COL0];
94a50b7dbc3ba1db8c92b4bbb4f7f06de8d6c039c4Brian Paul      GLuint i;
95a50b7dbc3ba1db8c92b4bbb4f7f06de8d6c039c4Brian Paul      for (i = 0; i < n; i++) {
96a50b7dbc3ba1db8c92b4bbb4f7f06de8d6c039c4Brian Paul         src[i][RCOMP] = (src[i][RCOMP] & rMask) | (dst[i][RCOMP] & ~rMask);
97a50b7dbc3ba1db8c92b4bbb4f7f06de8d6c039c4Brian Paul         src[i][GCOMP] = (src[i][GCOMP] & gMask) | (dst[i][GCOMP] & ~gMask);
98a50b7dbc3ba1db8c92b4bbb4f7f06de8d6c039c4Brian Paul         src[i][BCOMP] = (src[i][BCOMP] & bMask) | (dst[i][BCOMP] & ~bMask);
99a50b7dbc3ba1db8c92b4bbb4f7f06de8d6c039c4Brian Paul         src[i][ACOMP] = (src[i][ACOMP] & aMask) | (dst[i][ACOMP] & ~aMask);
100a50b7dbc3ba1db8c92b4bbb4f7f06de8d6c039c4Brian Paul      }
101733a4b602bbbfda83ee03b7ae4f3737bbe659034Brian Paul   }
102733a4b602bbbfda83ee03b7ae4f3737bbe659034Brian Paul}
103