home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / mesa5.zip / mesa5src.zip / swrast / s_masking.cpp < prev    next >
C/C++ Source or Header  |  2002-08-06  |  6KB  |  193 lines

  1. /* $Id: s_masking.c,v 1.8 2002/08/07 00:45:07 brianp Exp $ */
  2.  
  3. /*
  4.  * Mesa 3-D graphics library
  5.  * Version:  4.1
  6.  *
  7.  * Copyright (C) 1999-2002  Brian Paul   All Rights Reserved.
  8.  *
  9.  * Permission is hereby granted, free of charge, to any person obtaining a
  10.  * copy of this software and associated documentation files (the "Software"),
  11.  * to deal in the Software without restriction, including without limitation
  12.  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  13.  * and/or sell copies of the Software, and to permit persons to whom the
  14.  * Software is furnished to do so, subject to the following conditions:
  15.  *
  16.  * The above copyright notice and this permission notice shall be included
  17.  * in all copies or substantial portions of the Software.
  18.  *
  19.  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  20.  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21.  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
  22.  * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
  23.  * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  24.  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  25.  */
  26.  
  27.  
  28. /*
  29.  * Implement the effect of glColorMask and glIndexMask in software.
  30.  */
  31.  
  32.  
  33. #include "glheader.h"
  34. #include "enums.h"
  35. #include "macros.h"
  36.  
  37. #include "s_alphabuf.h"
  38. #include "s_context.h"
  39. #include "s_masking.h"
  40. #include "s_span.h"
  41.  
  42.  
  43.  
  44. void
  45. _mesa_mask_rgba_span( GLcontext *ctx, const struct sw_span *span,
  46.                      GLchan rgba[][4] )
  47. {
  48.    SWcontext *swrast = SWRAST_CONTEXT(ctx);
  49.    GLchan dest[MAX_WIDTH][4];
  50. #if CHAN_BITS == 8
  51.    GLuint srcMask = *((GLuint*)ctx->Color.ColorMask);
  52.    GLuint dstMask = ~srcMask;
  53.    GLuint *rgba32 = (GLuint *) rgba;
  54.    GLuint *dest32 = (GLuint *) dest;
  55. #else
  56.    const GLboolean rMask = ctx->Color.ColorMask[RCOMP];
  57.    const GLboolean gMask = ctx->Color.ColorMask[GCOMP];
  58.    const GLboolean bMask = ctx->Color.ColorMask[BCOMP];
  59.    const GLboolean aMask = ctx->Color.ColorMask[ACOMP];
  60. #endif
  61.    const GLuint n = span->end;
  62.    GLuint i;
  63.  
  64.    ASSERT(n < MAX_WIDTH);
  65.    ASSERT(span->arrayMask & SPAN_RGBA);
  66.  
  67.    if (span->arrayMask & SPAN_XY) {
  68.       (*swrast->Driver.ReadRGBAPixels)(ctx, n, span->array->x, span->array->y,
  69.                                        dest, span->array->mask);
  70.       if (SWRAST_CONTEXT(ctx)->_RasterMask & ALPHABUF_BIT) {
  71.          _mesa_read_alpha_pixels(ctx, n, span->array->x, span->array->y,
  72.                                  dest, span->array->mask);
  73.       }
  74.    }
  75.    else {
  76.       _mesa_read_rgba_span(ctx, ctx->DrawBuffer, n, span->x, span->y, dest);
  77.    }
  78.  
  79. #if CHAN_BITS == 8
  80.    for (i = 0; i < n; i++) {
  81.       rgba32[i] = (rgba32[i] & srcMask) | (dest32[i] & dstMask);
  82.    }
  83. #else
  84.    for (i = 0; i < n; i++) {
  85.       if (!rMask)  rgba[i][RCOMP] = dest[i][RCOMP];
  86.       if (!gMask)  rgba[i][GCOMP] = dest[i][GCOMP];
  87.       if (!bMask)  rgba[i][BCOMP] = dest[i][BCOMP];
  88.       if (!aMask)  rgba[i][ACOMP] = dest[i][ACOMP];
  89.    }
  90. #endif
  91. }
  92.  
  93.  
  94.  
  95.  
  96. /*
  97.  * Apply glColorMask to a span of RGBA pixels.
  98.  */
  99. void
  100. _mesa_mask_rgba_array( GLcontext *ctx,
  101.                        GLuint n, GLint x, GLint y, GLchan rgba[][4] )
  102. {
  103.    GLchan dest[MAX_WIDTH][4];
  104.    GLuint i;
  105.  
  106. #if CHAN_BITS == 8
  107.  
  108.    GLuint srcMask = *((GLuint*)ctx->Color.ColorMask);
  109.    GLuint dstMask = ~srcMask;
  110.    GLuint *rgba32 = (GLuint *) rgba;
  111.    GLuint *dest32 = (GLuint *) dest;
  112.  
  113.    _mesa_read_rgba_span( ctx, ctx->DrawBuffer, n, x, y, dest );
  114.    for (i = 0; i < n; i++) {
  115.       rgba32[i] = (rgba32[i] & srcMask) | (dest32[i] & dstMask);
  116.    }
  117.  
  118. #else
  119.  
  120.    const GLint rMask = ctx->Color.ColorMask[RCOMP];
  121.    const GLint gMask = ctx->Color.ColorMask[GCOMP];
  122.    const GLint bMask = ctx->Color.ColorMask[BCOMP];
  123.    const GLint aMask = ctx->Color.ColorMask[ACOMP];
  124.  
  125.    _mesa_read_rgba_span( ctx, ctx->DrawBuffer, n, x, y, dest );
  126.    for (i = 0; i < n; i++) {
  127.       if (!rMask)  rgba[i][RCOMP] = dest[i][RCOMP];
  128.       if (!gMask)  rgba[i][GCOMP] = dest[i][GCOMP];
  129.       if (!bMask)  rgba[i][BCOMP] = dest[i][BCOMP];
  130.       if (!aMask)  rgba[i][ACOMP] = dest[i][ACOMP];
  131.    }
  132.  
  133. #endif
  134. }
  135.  
  136.  
  137.  
  138. void
  139. _mesa_mask_index_span( GLcontext *ctx, const struct sw_span *span,
  140.                        GLuint index[] )
  141. {
  142.    SWcontext *swrast = SWRAST_CONTEXT(ctx);
  143.    const GLuint msrc = ctx->Color.IndexMask;
  144.    const GLuint mdest = ~msrc;
  145.    GLuint fbindexes[MAX_WIDTH];
  146.    GLuint i;
  147.  
  148.    ASSERT(span->arrayMask & SPAN_INDEX);
  149.    ASSERT(span->end < MAX_WIDTH);
  150.  
  151.    if (span->arrayMask & SPAN_XY) {
  152.  
  153.       (*swrast->Driver.ReadCI32Pixels)(ctx, span->end, span->array->x,
  154.                                        span->array->y, fbindexes,
  155.                                        span->array->mask);
  156.  
  157.       for (i = 0; i < span->end; i++) {
  158.          index[i] = (index[i] & msrc) | (fbindexes[i] & mdest);
  159.       }
  160.    }
  161.    else {
  162.       _mesa_read_index_span(ctx, ctx->DrawBuffer, span->end, span->x, span->y,
  163.                             fbindexes );
  164.  
  165.       for (i = 0; i < span->end; i++) {
  166.          index[i] = (index[i] & msrc) | (fbindexes[i] & mdest);
  167.       }
  168.    }
  169. }
  170.  
  171.  
  172.  
  173. /*
  174.  * Apply glIndexMask to a span of CI pixels.
  175.  */
  176. void
  177. _mesa_mask_index_array( GLcontext *ctx,
  178.                         GLuint n, GLint x, GLint y, GLuint index[] )
  179. {
  180.    GLuint i;
  181.    GLuint fbindexes[MAX_WIDTH];
  182.    GLuint msrc, mdest;
  183.  
  184.    _mesa_read_index_span( ctx, ctx->DrawBuffer, n, x, y, fbindexes );
  185.  
  186.    msrc = ctx->Color.IndexMask;
  187.    mdest = ~msrc;
  188.  
  189.    for (i=0;i<n;i++) {
  190.       index[i] = (index[i] & msrc) | (fbindexes[i] & mdest);
  191.    }
  192. }
  193.