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