[3598] | 1 | /* $Id: alphabuf.c,v 1.3 2000-05-23 20:40:20 jeroen Exp $ */
|
---|
[2938] | 2 |
|
---|
| 3 | /*
|
---|
| 4 | * Mesa 3-D graphics library
|
---|
[3598] | 5 | * Version: 3.3
|
---|
[2938] | 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
|
---|
[3598] | 41 | #include "glheader.h"
|
---|
[2962] | 42 | #include "types.h"
|
---|
[2938] | 43 | #include "alphabuf.h"
|
---|
| 44 | #include "context.h"
|
---|
| 45 | #include "macros.h"
|
---|
[3598] | 46 | #include "mem.h"
|
---|
[2938] | 47 | #endif
|
---|
| 48 |
|
---|
| 49 |
|
---|
| 50 |
|
---|
[3598] | 51 | #define ALPHA_DRAW_ADDR(X,Y) \
|
---|
| 52 | (ctx->DrawBuffer->Alpha + (Y) * ctx->DrawBuffer->Width + (X))
|
---|
[2938] | 53 |
|
---|
[3598] | 54 | #define ALPHA_READ_ADDR(X,Y) \
|
---|
| 55 | (ctx->ReadBuffer->Alpha + (Y) * ctx->ReadBuffer->Width + (X))
|
---|
[2938] | 56 |
|
---|
| 57 |
|
---|
[3598] | 58 |
|
---|
[2938] | 59 | /*
|
---|
[3598] | 60 | * Allocate new front/back/left/right alpha buffers.
|
---|
| 61 | * Input: ctx - the context
|
---|
[2938] | 62 | */
|
---|
[3598] | 63 | static void
|
---|
| 64 | alloc_alpha_buffers( GLcontext *ctx, GLframebuffer *buf )
|
---|
[2938] | 65 | {
|
---|
[3598] | 66 | GLint bytes = buf->Width * buf->Height * sizeof(GLubyte);
|
---|
[2938] | 67 |
|
---|
| 68 | ASSERT(ctx->Visual->SoftwareAlpha);
|
---|
| 69 |
|
---|
[3598] | 70 | if (buf->FrontLeftAlpha) {
|
---|
| 71 | FREE( buf->FrontLeftAlpha );
|
---|
[2938] | 72 | }
|
---|
[3598] | 73 | buf->FrontLeftAlpha = (GLubyte *) MALLOC( bytes );
|
---|
| 74 | if (!buf->FrontLeftAlpha) {
|
---|
[2938] | 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) {
|
---|
[3598] | 81 | if (buf->BackLeftAlpha) {
|
---|
| 82 | FREE( buf->BackLeftAlpha );
|
---|
[2938] | 83 | }
|
---|
[3598] | 84 | buf->BackLeftAlpha = (GLubyte *) MALLOC( bytes );
|
---|
| 85 | if (!buf->BackLeftAlpha) {
|
---|
[2938] | 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) {
|
---|
[3598] | 93 | if (buf->FrontRightAlpha) {
|
---|
| 94 | FREE( buf->FrontRightAlpha );
|
---|
[2938] | 95 | }
|
---|
[3598] | 96 | buf->FrontRightAlpha = (GLubyte *) MALLOC( bytes );
|
---|
| 97 | if (!buf->FrontRightAlpha) {
|
---|
[2938] | 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) {
|
---|
[3598] | 104 | if (buf->BackRightAlpha) {
|
---|
| 105 | FREE( buf->BackRightAlpha );
|
---|
[2938] | 106 | }
|
---|
[3598] | 107 | buf->BackRightAlpha = (GLubyte *) MALLOC( bytes );
|
---|
| 108 | if (!buf->BackRightAlpha) {
|
---|
[2938] | 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)
|
---|
[3598] | 117 | buf->Alpha = buf->FrontLeftAlpha;
|
---|
[2938] | 118 | else if (ctx->Color.DriverDrawBuffer == GL_BACK_LEFT)
|
---|
[3598] | 119 | buf->Alpha = buf->BackLeftAlpha;
|
---|
[2938] | 120 | else if (ctx->Color.DriverDrawBuffer == GL_FRONT_RIGHT)
|
---|
[3598] | 121 | buf->Alpha = buf->FrontRightAlpha;
|
---|
[2938] | 122 | else if (ctx->Color.DriverDrawBuffer == GL_BACK_RIGHT)
|
---|
[3598] | 123 | buf->Alpha = buf->BackRightAlpha;
|
---|
[2938] | 124 | }
|
---|
| 125 |
|
---|
| 126 |
|
---|
| 127 | /*
|
---|
[3598] | 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 | /*
|
---|
[2938] | 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) {
|
---|
[3598] | 155 | buffer = ctx->DrawBuffer->FrontLeftAlpha;
|
---|
[2938] | 156 | }
|
---|
| 157 | else if (bufferBit == FRONT_RIGHT_BIT) {
|
---|
[3598] | 158 | buffer = ctx->DrawBuffer->FrontRightAlpha;
|
---|
[2938] | 159 | }
|
---|
| 160 | else if (bufferBit == BACK_LEFT_BIT) {
|
---|
[3598] | 161 | buffer = ctx->DrawBuffer->BackLeftAlpha;
|
---|
[2938] | 162 | }
|
---|
| 163 | else {
|
---|
[3598] | 164 | buffer = ctx->DrawBuffer->BackRightAlpha;
|
---|
[2938] | 165 | }
|
---|
| 166 |
|
---|
| 167 | if (ctx->Scissor.Enabled) {
|
---|
| 168 | /* clear scissor region */
|
---|
| 169 | GLint j;
|
---|
[3598] | 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;
|
---|
[2938] | 175 | for (j = 0; j < rows; j++) {
|
---|
| 176 | MEMSET( aptr, aclear, rowLen );
|
---|
| 177 | aptr += rowLen;
|
---|
| 178 | }
|
---|
| 179 | }
|
---|
| 180 | else {
|
---|
| 181 | /* clear whole buffer */
|
---|
[3598] | 182 | GLuint bytes = ctx->DrawBuffer->Width * ctx->DrawBuffer->Height;
|
---|
| 183 | MEMSET( buffer, aclear, bytes );
|
---|
[2938] | 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 | {
|
---|
[3598] | 194 | GLubyte *aptr = ALPHA_DRAW_ADDR( x, y );
|
---|
[2938] | 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 | {
|
---|
[3598] | 216 | GLubyte *aptr = ALPHA_DRAW_ADDR( x, y );
|
---|
[2938] | 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]) {
|
---|
[3598] | 244 | GLubyte *aptr = ALPHA_DRAW_ADDR( x[i], y[i] );
|
---|
[2938] | 245 | *aptr = rgba[i][ACOMP];
|
---|
| 246 | }
|
---|
| 247 | }
|
---|
| 248 | }
|
---|
| 249 | else {
|
---|
| 250 | for (i=0;i<n;i++) {
|
---|
[3598] | 251 | GLubyte *aptr = ALPHA_DRAW_ADDR( x[i], y[i] );
|
---|
[2938] | 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]) {
|
---|
[3598] | 267 | GLubyte *aptr = ALPHA_DRAW_ADDR( x[i], y[i] );
|
---|
[2938] | 268 | *aptr = alpha;
|
---|
| 269 | }
|
---|
| 270 | }
|
---|
| 271 | }
|
---|
| 272 | else {
|
---|
| 273 | for (i=0;i<n;i++) {
|
---|
[3598] | 274 | GLubyte *aptr = ALPHA_DRAW_ADDR( x[i], y[i] );
|
---|
[2938] | 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 | {
|
---|
[3598] | 285 | GLubyte *aptr = ALPHA_READ_ADDR( x, y );
|
---|
[2938] | 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]) {
|
---|
[3598] | 300 | GLubyte *aptr = ALPHA_READ_ADDR( x[i], y[i] );
|
---|
[2938] | 301 | rgba[i][ACOMP] = *aptr;
|
---|
| 302 | }
|
---|
| 303 | }
|
---|
| 304 | }
|
---|
| 305 |
|
---|
| 306 |
|
---|
| 307 |
|
---|