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 * \file swrast/s_alpha.c 27848b8605Smrg * \brief Functions to apply alpha test. 28848b8605Smrg */ 29848b8605Smrg 30848b8605Smrg#include "main/glheader.h" 31848b8605Smrg#include "main/context.h" 32848b8605Smrg#include "main/macros.h" 33848b8605Smrg 34848b8605Smrg#include "s_alpha.h" 35848b8605Smrg#include "s_context.h" 36848b8605Smrg 37848b8605Smrg 38848b8605Smrg#define ALPHA_TEST(ALPHA, LOOP_CODE) \ 39848b8605Smrgdo { \ 40848b8605Smrg switch (ctx->Color.AlphaFunc) { \ 41848b8605Smrg case GL_LESS: \ 42848b8605Smrg for (i = 0; i < n; i++) { \ 43848b8605Smrg mask[i] &= (ALPHA < ref); \ 44848b8605Smrg LOOP_CODE; \ 45848b8605Smrg } \ 46848b8605Smrg break; \ 47848b8605Smrg case GL_LEQUAL: \ 48848b8605Smrg for (i = 0; i < n; i++) { \ 49848b8605Smrg mask[i] &= (ALPHA <= ref); \ 50848b8605Smrg LOOP_CODE; \ 51848b8605Smrg } \ 52848b8605Smrg break; \ 53848b8605Smrg case GL_GEQUAL: \ 54848b8605Smrg for (i = 0; i < n; i++) { \ 55848b8605Smrg mask[i] &= (ALPHA >= ref); \ 56848b8605Smrg LOOP_CODE; \ 57848b8605Smrg } \ 58848b8605Smrg break; \ 59848b8605Smrg case GL_GREATER: \ 60848b8605Smrg for (i = 0; i < n; i++) { \ 61848b8605Smrg mask[i] &= (ALPHA > ref); \ 62848b8605Smrg LOOP_CODE; \ 63848b8605Smrg } \ 64848b8605Smrg break; \ 65848b8605Smrg case GL_NOTEQUAL: \ 66848b8605Smrg for (i = 0; i < n; i++) { \ 67848b8605Smrg mask[i] &= (ALPHA != ref); \ 68848b8605Smrg LOOP_CODE; \ 69848b8605Smrg } \ 70848b8605Smrg break; \ 71848b8605Smrg case GL_EQUAL: \ 72848b8605Smrg for (i = 0; i < n; i++) { \ 73848b8605Smrg mask[i] &= (ALPHA == ref); \ 74848b8605Smrg LOOP_CODE; \ 75848b8605Smrg } \ 76848b8605Smrg break; \ 77848b8605Smrg default: \ 78848b8605Smrg _mesa_problem(ctx, "Invalid alpha test in _swrast_alpha_test" ); \ 79848b8605Smrg return 0; \ 80848b8605Smrg } \ 81848b8605Smrg} while (0) 82848b8605Smrg 83848b8605Smrg 84848b8605Smrg 85848b8605Smrg/** 86848b8605Smrg * Perform the alpha test for an array of pixels. 87848b8605Smrg * For pixels that fail the test, mask[i] will be set to 0. 88848b8605Smrg * \return 0 if all pixels in the span failed the alpha test, 89848b8605Smrg * 1 if one or more pixels passed the alpha test. 90848b8605Smrg */ 91848b8605SmrgGLint 92848b8605Smrg_swrast_alpha_test(const struct gl_context *ctx, SWspan *span) 93848b8605Smrg{ 94848b8605Smrg const GLuint n = span->end; 95848b8605Smrg GLubyte *mask = span->array->mask; 96848b8605Smrg GLuint i; 97848b8605Smrg 98848b8605Smrg if (ctx->Color.AlphaFunc == GL_ALWAYS) { 99848b8605Smrg /* do nothing */ 100848b8605Smrg return 1; 101848b8605Smrg } 102848b8605Smrg else if (ctx->Color.AlphaFunc == GL_NEVER) { 103848b8605Smrg /* All pixels failed - caller should check for this return value and 104848b8605Smrg * act accordingly. 105848b8605Smrg */ 106848b8605Smrg span->writeAll = GL_FALSE; 107848b8605Smrg return 0; 108848b8605Smrg } 109848b8605Smrg 110848b8605Smrg if (span->arrayMask & SPAN_RGBA) { 111848b8605Smrg /* Use array's alpha values */ 112848b8605Smrg if (span->array->ChanType == GL_UNSIGNED_BYTE) { 113848b8605Smrg GLubyte (*rgba)[4] = span->array->rgba8; 114848b8605Smrg GLubyte ref; 115848b8605Smrg CLAMPED_FLOAT_TO_UBYTE(ref, ctx->Color.AlphaRef); 116848b8605Smrg ALPHA_TEST(rgba[i][ACOMP], ;); 117848b8605Smrg } 118848b8605Smrg else if (span->array->ChanType == GL_UNSIGNED_SHORT) { 119848b8605Smrg GLushort (*rgba)[4] = span->array->rgba16; 120848b8605Smrg GLushort ref; 121848b8605Smrg CLAMPED_FLOAT_TO_USHORT(ref, ctx->Color.AlphaRef); 122848b8605Smrg ALPHA_TEST(rgba[i][ACOMP], ;); 123848b8605Smrg } 124848b8605Smrg else { 125848b8605Smrg GLfloat (*rgba)[4] = span->array->attribs[VARYING_SLOT_COL0]; 126848b8605Smrg const GLfloat ref = ctx->Color.AlphaRef; 127848b8605Smrg ALPHA_TEST(rgba[i][ACOMP], ;); 128848b8605Smrg } 129848b8605Smrg } 130848b8605Smrg else { 131848b8605Smrg /* Interpolate alpha values */ 132b8e80941Smrg assert(span->interpMask & SPAN_RGBA); 133848b8605Smrg if (span->array->ChanType == GL_UNSIGNED_BYTE) { 134848b8605Smrg const GLfixed alphaStep = span->alphaStep; 135848b8605Smrg GLfixed alpha = span->alpha; 136848b8605Smrg GLubyte ref; 137848b8605Smrg CLAMPED_FLOAT_TO_UBYTE(ref, ctx->Color.AlphaRef); 138848b8605Smrg ALPHA_TEST(FixedToInt(alpha), alpha += alphaStep); 139848b8605Smrg } 140848b8605Smrg else if (span->array->ChanType == GL_UNSIGNED_SHORT) { 141848b8605Smrg const GLfixed alphaStep = span->alphaStep; 142848b8605Smrg GLfixed alpha = span->alpha; 143848b8605Smrg GLushort ref; 144848b8605Smrg CLAMPED_FLOAT_TO_USHORT(ref, ctx->Color.AlphaRef); 145848b8605Smrg ALPHA_TEST(FixedToInt(alpha), alpha += alphaStep); 146848b8605Smrg } 147848b8605Smrg else { 148848b8605Smrg const GLfloat alphaStep = FixedToFloat(span->alphaStep); 149848b8605Smrg GLfloat alpha = FixedToFloat(span->alpha); 150848b8605Smrg const GLfloat ref = ctx->Color.AlphaRef; 151848b8605Smrg ALPHA_TEST(alpha, alpha += alphaStep); 152848b8605Smrg } 153848b8605Smrg } 154848b8605Smrg 155848b8605Smrg span->writeAll = GL_FALSE; 156848b8605Smrg 157848b8605Smrg /* XXX examine mask[] values? */ 158848b8605Smrg return 1; 159848b8605Smrg} 160