1e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell/* 2e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell * Mesa 3-D graphics library 322144ab7552f0799bcfca506bf4ffa7f70a06649Gareth Hughes * 443715c711d2c1d1e7624cd7c9c8a44b8866510fdBrian Paul * Copyright (C) 1999-2006 Brian Paul All Rights Reserved. 522144ab7552f0799bcfca506bf4ffa7f70a06649Gareth Hughes * 6e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell * Permission is hereby granted, free of charge, to any person obtaining a 7e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell * copy of this software and associated documentation files (the "Software"), 8e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell * to deal in the Software without restriction, including without limitation 9e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell * the rights to use, copy, modify, merge, publish, distribute, sublicense, 10e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell * and/or sell copies of the Software, and to permit persons to whom the 11e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell * Software is furnished to do so, subject to the following conditions: 1222144ab7552f0799bcfca506bf4ffa7f70a06649Gareth Hughes * 13e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell * The above copyright notice and this permission notice shall be included 14e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell * in all copies or substantial portions of the Software. 1522144ab7552f0799bcfca506bf4ffa7f70a06649Gareth Hughes * 16e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 17e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 193d8d5b298a268b119d840bc9bae0ee9e0c9244a9Kenneth Graunke * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR 203d8d5b298a268b119d840bc9bae0ee9e0c9244a9Kenneth Graunke * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 213d8d5b298a268b119d840bc9bae0ee9e0c9244a9Kenneth Graunke * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 223d8d5b298a268b119d840bc9bae0ee9e0c9244a9Kenneth Graunke * 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 49bfcdb843830bba0190e00e35e3c5c18c4bdb5de1Matt Turner assert(n < SWRAST_MAX_WIDTH); 50bfcdb843830bba0190e00e35e3c5c18c4bdb5de1Matt Turner 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) { 59a1e9b72102f21426b6c4e6b47ac1742eb79d904fMatt Turner GLuint srcMask; 60a1e9b72102f21426b6c4e6b47ac1742eb79d904fMatt Turner memcpy(&srcMask, ctx->Color.ColorMask[buf], sizeof(srcMask)); 61a50b7dbc3ba1db8c92b4bbb4f7f06de8d6c039c4Brian Paul const GLuint dstMask = ~srcMask; 62a50b7dbc3ba1db8c92b4bbb4f7f06de8d6c039c4Brian Paul const GLuint *dst = (const GLuint *) rbPixels; 639e8a961dd7d7b717a9fb4ecdea1c1b60ea355efeBrian Paul 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 Paul 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; 93eed6baf7621fa94e7888f8079b155fc67a08540cPaul Berry GLuint (*src)[4] = (GLuint (*)[4]) span->array->attribs[VARYING_SLOT_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