| 1 | /* $Id: alphabuf.c,v 1.2 2000-03-01 18:49:22 jeroen Exp $ */ | 
|---|
| 2 |  | 
|---|
| 3 | /* | 
|---|
| 4 | * Mesa 3-D graphics library | 
|---|
| 5 | * Version:  3.1 | 
|---|
| 6 | * | 
|---|
| 7 | * Copyright (C) 1999  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 |  | 
|---|
| 30 |  | 
|---|
| 31 | /* | 
|---|
| 32 | * Software alpha planes.  Many frame buffers don't have alpha bits so | 
|---|
| 33 | * we simulate them in software. | 
|---|
| 34 | */ | 
|---|
| 35 |  | 
|---|
| 36 | #include <stdlib.h> | 
|---|
| 37 |  | 
|---|
| 38 | #ifdef PC_HEADER | 
|---|
| 39 | #include "all.h" | 
|---|
| 40 | #else | 
|---|
| 41 | #include "types.h" | 
|---|
| 42 | #include "alphabuf.h" | 
|---|
| 43 | #include "context.h" | 
|---|
| 44 | #include "macros.h" | 
|---|
| 45 | #endif | 
|---|
| 46 |  | 
|---|
| 47 |  | 
|---|
| 48 |  | 
|---|
| 49 | #define ALPHA_ADDR(X,Y)  (ctx->Buffer->Alpha + (Y) * ctx->Buffer->Width + (X)) | 
|---|
| 50 |  | 
|---|
| 51 |  | 
|---|
| 52 |  | 
|---|
| 53 | /* | 
|---|
| 54 | * Allocate a new front and back alpha buffer. | 
|---|
| 55 | */ | 
|---|
| 56 | void gl_alloc_alpha_buffers( GLcontext *ctx ) | 
|---|
| 57 | { | 
|---|
| 58 | GLint bytes = ctx->Buffer->Width * ctx->Buffer->Height * sizeof(GLubyte); | 
|---|
| 59 |  | 
|---|
| 60 | ASSERT(ctx->Visual->SoftwareAlpha); | 
|---|
| 61 |  | 
|---|
| 62 | if (ctx->Buffer->FrontLeftAlpha) { | 
|---|
| 63 | FREE( ctx->Buffer->FrontLeftAlpha ); | 
|---|
| 64 | } | 
|---|
| 65 | ctx->Buffer->FrontLeftAlpha = (GLubyte *) MALLOC( bytes ); | 
|---|
| 66 | if (!ctx->Buffer->FrontLeftAlpha) { | 
|---|
| 67 | /* out of memory */ | 
|---|
| 68 | gl_error( ctx, GL_OUT_OF_MEMORY, | 
|---|
| 69 | "Couldn't allocate front-left alpha buffer" ); | 
|---|
| 70 | } | 
|---|
| 71 |  | 
|---|
| 72 | if (ctx->Visual->DBflag) { | 
|---|
| 73 | if (ctx->Buffer->BackLeftAlpha) { | 
|---|
| 74 | FREE( ctx->Buffer->BackLeftAlpha ); | 
|---|
| 75 | } | 
|---|
| 76 | ctx->Buffer->BackLeftAlpha = (GLubyte *) MALLOC( bytes ); | 
|---|
| 77 | if (!ctx->Buffer->BackLeftAlpha) { | 
|---|
| 78 | /* out of memory */ | 
|---|
| 79 | gl_error( ctx, GL_OUT_OF_MEMORY, | 
|---|
| 80 | "Couldn't allocate back-left alpha buffer" ); | 
|---|
| 81 | } | 
|---|
| 82 | } | 
|---|
| 83 |  | 
|---|
| 84 | if (ctx->Visual->StereoFlag) { | 
|---|
| 85 | if (ctx->Buffer->FrontRightAlpha) { | 
|---|
| 86 | FREE( ctx->Buffer->FrontRightAlpha ); | 
|---|
| 87 | } | 
|---|
| 88 | ctx->Buffer->FrontRightAlpha = (GLubyte *) MALLOC( bytes ); | 
|---|
| 89 | if (!ctx->Buffer->FrontRightAlpha) { | 
|---|
| 90 | /* out of memory */ | 
|---|
| 91 | gl_error( ctx, GL_OUT_OF_MEMORY, | 
|---|
| 92 | "Couldn't allocate front-right alpha buffer" ); | 
|---|
| 93 | } | 
|---|
| 94 |  | 
|---|
| 95 | if (ctx->Visual->DBflag) { | 
|---|
| 96 | if (ctx->Buffer->BackRightAlpha) { | 
|---|
| 97 | FREE( ctx->Buffer->BackRightAlpha ); | 
|---|
| 98 | } | 
|---|
| 99 | ctx->Buffer->BackRightAlpha = (GLubyte *) MALLOC( bytes ); | 
|---|
| 100 | if (!ctx->Buffer->BackRightAlpha) { | 
|---|
| 101 | /* out of memory */ | 
|---|
| 102 | gl_error( ctx, GL_OUT_OF_MEMORY, | 
|---|
| 103 | "Couldn't allocate back-right alpha buffer" ); | 
|---|
| 104 | } | 
|---|
| 105 | } | 
|---|
| 106 | } | 
|---|
| 107 |  | 
|---|
| 108 | if (ctx->Color.DriverDrawBuffer == GL_FRONT_LEFT) | 
|---|
| 109 | ctx->Buffer->Alpha = ctx->Buffer->FrontLeftAlpha; | 
|---|
| 110 | else if (ctx->Color.DriverDrawBuffer == GL_BACK_LEFT) | 
|---|
| 111 | ctx->Buffer->Alpha = ctx->Buffer->BackLeftAlpha; | 
|---|
| 112 | else if (ctx->Color.DriverDrawBuffer == GL_FRONT_RIGHT) | 
|---|
| 113 | ctx->Buffer->Alpha = ctx->Buffer->FrontRightAlpha; | 
|---|
| 114 | else if (ctx->Color.DriverDrawBuffer == GL_BACK_RIGHT) | 
|---|
| 115 | ctx->Buffer->Alpha = ctx->Buffer->BackRightAlpha; | 
|---|
| 116 | } | 
|---|
| 117 |  | 
|---|
| 118 |  | 
|---|
| 119 | /* | 
|---|
| 120 | * Clear all the alpha buffers | 
|---|
| 121 | */ | 
|---|
| 122 | void gl_clear_alpha_buffers( GLcontext *ctx ) | 
|---|
| 123 | { | 
|---|
| 124 | const GLubyte aclear = (GLint) (ctx->Color.ClearColor[3] * 255.0F); | 
|---|
| 125 | GLuint bufferBit; | 
|---|
| 126 |  | 
|---|
| 127 | ASSERT(ctx->Visual->SoftwareAlpha); | 
|---|
| 128 | ASSERT(ctx->Color.ColorMask[ACOMP]); | 
|---|
| 129 |  | 
|---|
| 130 | /* loop over four possible alpha buffers */ | 
|---|
| 131 | for (bufferBit = 1; bufferBit <= 8; bufferBit = bufferBit << 1) { | 
|---|
| 132 | if (bufferBit & ctx->Color.DrawDestMask) { | 
|---|
| 133 | GLubyte *buffer; | 
|---|
| 134 | if (bufferBit == FRONT_LEFT_BIT) { | 
|---|
| 135 | buffer = ctx->Buffer->FrontLeftAlpha; | 
|---|
| 136 | } | 
|---|
| 137 | else if (bufferBit == FRONT_RIGHT_BIT) { | 
|---|
| 138 | buffer = ctx->Buffer->FrontRightAlpha; | 
|---|
| 139 | } | 
|---|
| 140 | else if (bufferBit == BACK_LEFT_BIT) { | 
|---|
| 141 | buffer = ctx->Buffer->BackLeftAlpha; | 
|---|
| 142 | } | 
|---|
| 143 | else { | 
|---|
| 144 | buffer = ctx->Buffer->BackRightAlpha; | 
|---|
| 145 | } | 
|---|
| 146 |  | 
|---|
| 147 | if (ctx->Scissor.Enabled) { | 
|---|
| 148 | /* clear scissor region */ | 
|---|
| 149 | GLint j; | 
|---|
| 150 | GLint rowLen = ctx->Buffer->Xmax - ctx->Buffer->Xmin + 1; | 
|---|
| 151 | GLint rows = ctx->Buffer->Ymax - ctx->Buffer->Ymin + 1; | 
|---|
| 152 | GLubyte *aptr = buffer + ctx->Buffer->Ymin * ctx->Buffer->Width | 
|---|
| 153 | + ctx->Buffer->Xmin; | 
|---|
| 154 | for (j = 0; j < rows; j++) { | 
|---|
| 155 | MEMSET( aptr, aclear, rowLen ); | 
|---|
| 156 | aptr += rowLen; | 
|---|
| 157 | } | 
|---|
| 158 | } | 
|---|
| 159 | else { | 
|---|
| 160 | /* clear whole buffer */ | 
|---|
| 161 | MEMSET( buffer, aclear, ctx->Buffer->Width * ctx->Buffer->Height ); | 
|---|
| 162 | } | 
|---|
| 163 | } | 
|---|
| 164 | } | 
|---|
| 165 | } | 
|---|
| 166 |  | 
|---|
| 167 |  | 
|---|
| 168 |  | 
|---|
| 169 | void gl_write_alpha_span( GLcontext *ctx, GLuint n, GLint x, GLint y, | 
|---|
| 170 | CONST GLubyte rgba[][4], const GLubyte mask[] ) | 
|---|
| 171 | { | 
|---|
| 172 | GLubyte *aptr = ALPHA_ADDR( x, y ); | 
|---|
| 173 | GLuint i; | 
|---|
| 174 |  | 
|---|
| 175 | if (mask) { | 
|---|
| 176 | for (i=0;i<n;i++) { | 
|---|
| 177 | if (mask[i]) { | 
|---|
| 178 | *aptr = rgba[i][ACOMP]; | 
|---|
| 179 | } | 
|---|
| 180 | aptr++; | 
|---|
| 181 | } | 
|---|
| 182 | } | 
|---|
| 183 | else { | 
|---|
| 184 | for (i=0;i<n;i++) { | 
|---|
| 185 | *aptr++ = rgba[i][ACOMP]; | 
|---|
| 186 | } | 
|---|
| 187 | } | 
|---|
| 188 | } | 
|---|
| 189 |  | 
|---|
| 190 |  | 
|---|
| 191 | void gl_write_mono_alpha_span( GLcontext *ctx, GLuint n, GLint x, GLint y, | 
|---|
| 192 | GLubyte alpha, const GLubyte mask[] ) | 
|---|
| 193 | { | 
|---|
| 194 | GLubyte *aptr = ALPHA_ADDR( x, y ); | 
|---|
| 195 | GLuint i; | 
|---|
| 196 |  | 
|---|
| 197 | if (mask) { | 
|---|
| 198 | for (i=0;i<n;i++) { | 
|---|
| 199 | if (mask[i]) { | 
|---|
| 200 | *aptr = alpha; | 
|---|
| 201 | } | 
|---|
| 202 | aptr++; | 
|---|
| 203 | } | 
|---|
| 204 | } | 
|---|
| 205 | else { | 
|---|
| 206 | for (i=0;i<n;i++) { | 
|---|
| 207 | *aptr++ = alpha; | 
|---|
| 208 | } | 
|---|
| 209 | } | 
|---|
| 210 | } | 
|---|
| 211 |  | 
|---|
| 212 |  | 
|---|
| 213 | void gl_write_alpha_pixels( GLcontext *ctx, | 
|---|
| 214 | GLuint n, const GLint x[], const GLint y[], | 
|---|
| 215 | CONST GLubyte rgba[][4], const GLubyte mask[] ) | 
|---|
| 216 | { | 
|---|
| 217 | GLuint i; | 
|---|
| 218 |  | 
|---|
| 219 | if (mask) { | 
|---|
| 220 | for (i=0;i<n;i++) { | 
|---|
| 221 | if (mask[i]) { | 
|---|
| 222 | GLubyte *aptr = ALPHA_ADDR( x[i], y[i] ); | 
|---|
| 223 | *aptr = rgba[i][ACOMP]; | 
|---|
| 224 | } | 
|---|
| 225 | } | 
|---|
| 226 | } | 
|---|
| 227 | else { | 
|---|
| 228 | for (i=0;i<n;i++) { | 
|---|
| 229 | GLubyte *aptr = ALPHA_ADDR( x[i], y[i] ); | 
|---|
| 230 | *aptr = rgba[i][ACOMP]; | 
|---|
| 231 | } | 
|---|
| 232 | } | 
|---|
| 233 | } | 
|---|
| 234 |  | 
|---|
| 235 |  | 
|---|
| 236 | void gl_write_mono_alpha_pixels( GLcontext *ctx, | 
|---|
| 237 | GLuint n, const GLint x[], const GLint y[], | 
|---|
| 238 | GLubyte alpha, const GLubyte mask[] ) | 
|---|
| 239 | { | 
|---|
| 240 | GLuint i; | 
|---|
| 241 |  | 
|---|
| 242 | if (mask) { | 
|---|
| 243 | for (i=0;i<n;i++) { | 
|---|
| 244 | if (mask[i]) { | 
|---|
| 245 | GLubyte *aptr = ALPHA_ADDR( x[i], y[i] ); | 
|---|
| 246 | *aptr = alpha; | 
|---|
| 247 | } | 
|---|
| 248 | } | 
|---|
| 249 | } | 
|---|
| 250 | else { | 
|---|
| 251 | for (i=0;i<n;i++) { | 
|---|
| 252 | GLubyte *aptr = ALPHA_ADDR( x[i], y[i] ); | 
|---|
| 253 | *aptr = alpha; | 
|---|
| 254 | } | 
|---|
| 255 | } | 
|---|
| 256 | } | 
|---|
| 257 |  | 
|---|
| 258 |  | 
|---|
| 259 |  | 
|---|
| 260 | void gl_read_alpha_span( GLcontext *ctx, | 
|---|
| 261 | GLuint n, GLint x, GLint y, GLubyte rgba[][4] ) | 
|---|
| 262 | { | 
|---|
| 263 | GLubyte *aptr = ALPHA_ADDR( x, y ); | 
|---|
| 264 | GLuint i; | 
|---|
| 265 | for (i=0;i<n;i++) { | 
|---|
| 266 | rgba[i][ACOMP] = *aptr++; | 
|---|
| 267 | } | 
|---|
| 268 | } | 
|---|
| 269 |  | 
|---|
| 270 |  | 
|---|
| 271 | void gl_read_alpha_pixels( GLcontext *ctx, | 
|---|
| 272 | GLuint n, const GLint x[], const GLint y[], | 
|---|
| 273 | GLubyte rgba[][4], const GLubyte mask[] ) | 
|---|
| 274 | { | 
|---|
| 275 | GLuint i; | 
|---|
| 276 | for (i=0;i<n;i++) { | 
|---|
| 277 | if (mask[i]) { | 
|---|
| 278 | GLubyte *aptr = ALPHA_ADDR( x[i], y[i] ); | 
|---|
| 279 | rgba[i][ACOMP] = *aptr; | 
|---|
| 280 | } | 
|---|
| 281 | } | 
|---|
| 282 | } | 
|---|
| 283 |  | 
|---|
| 284 |  | 
|---|
| 285 |  | 
|---|