| 1 |
/* $Id: span.c,v 1.2 2000/10/27 15:21:40 mholst Exp $ */ |
| 2 |
|
| 3 |
/* |
| 4 |
* Mesa 3-D graphics library |
| 5 |
* Version: 2.2 |
| 6 |
* Copyright (C) 1995-1997 Brian Paul |
| 7 |
* |
| 8 |
* This library is free software; you can redistribute it and/or |
| 9 |
* modify it under the terms of the GNU Library General Public |
| 10 |
* License as published by the Free Software Foundation; either |
| 11 |
* version 2 of the License, or (at your option) any later version. |
| 12 |
* |
| 13 |
* This library is distributed in the hope that it will be useful, |
| 14 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 16 |
* Library General Public License for more details. |
| 17 |
* |
| 18 |
* You should have received a copy of the GNU Library General Public |
| 19 |
* License along with this library; if not, write to the Free |
| 20 |
* Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
| 21 |
*/ |
| 22 |
|
| 23 |
/* |
| 24 |
* pixel span rasterization: |
| 25 |
* These functions simulate the rasterization pipeline. |
| 26 |
*/ |
| 27 |
|
| 28 |
|
| 29 |
#include <string.h> |
| 30 |
#include "alpha.h" |
| 31 |
#include "alphabuf.h" |
| 32 |
#include "blend.h" |
| 33 |
#include "depth.h" |
| 34 |
#include "fog.h" |
| 35 |
#include "logic.h" |
| 36 |
#include "macros.h" |
| 37 |
#include "masking.h" |
| 38 |
#include "scissor.h" |
| 39 |
#include "span.h" |
| 40 |
#include "stencil.h" |
| 41 |
#include "texture.h" |
| 42 |
#include "types.h" |
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
/* |
| 48 |
* Apply the current polygon stipple pattern to a span of pixels. |
| 49 |
*/ |
| 50 |
static void stipple_polygon_span( GLcontext *ctx, |
| 51 |
GLuint n, GLint x, GLint y, GLubyte mask[] ) |
| 52 |
{ |
| 53 |
register GLuint i, m, stipple, highbit=0x80000000; |
| 54 |
|
| 55 |
stipple = ctx->PolygonStipple[y % 32]; |
| 56 |
m = highbit >> (GLuint) (x % 32); |
| 57 |
|
| 58 |
for (i=0;i<n;i++) { |
| 59 |
if ((m & stipple)==0) { |
| 60 |
mask[i] = 0; |
| 61 |
} |
| 62 |
m = m >> 1; |
| 63 |
if (m==0) { |
| 64 |
m = 0x80000000; |
| 65 |
} |
| 66 |
} |
| 67 |
} |
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
/* |
| 72 |
* Clip a pixel span to the current buffer/window boundaries. |
| 73 |
* Return: 0 = all pixels clipped |
| 74 |
* 1 = at least one pixel is visible |
| 75 |
*/ |
| 76 |
static GLuint clip_span( GLcontext *ctx, |
| 77 |
GLint n, GLint x, GLint y, GLubyte mask[] ) |
| 78 |
{ |
| 79 |
GLint i; |
| 80 |
|
| 81 |
/* Clip to top and bottom */ |
| 82 |
if (y<0 || y>=ctx->Buffer->Height) { |
| 83 |
return 0; |
| 84 |
} |
| 85 |
|
| 86 |
/* Clip to left and right */ |
| 87 |
if (x>=0 && x+n<=ctx->Buffer->Width) { |
| 88 |
/* no clipping needed */ |
| 89 |
return 1; |
| 90 |
} |
| 91 |
else if (x+n<=0) { |
| 92 |
/* completely off left side */ |
| 93 |
return 0; |
| 94 |
} |
| 95 |
else if (x>=ctx->Buffer->Width) { |
| 96 |
/* completely off right side */ |
| 97 |
return 0; |
| 98 |
} |
| 99 |
else { |
| 100 |
/* clip-test each pixel, this could be done better */ |
| 101 |
for (i=0;i<n;i++) { |
| 102 |
if (x+i<0 || x+i>=ctx->Buffer->Width) { |
| 103 |
mask[i] = 0; |
| 104 |
} |
| 105 |
} |
| 106 |
return 1; |
| 107 |
} |
| 108 |
} |
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
/* |
| 113 |
* Write a horizontal span of color index pixels to the frame buffer. |
| 114 |
* Stenciling, Depth-testing, etc. are done as needed. |
| 115 |
* Input: n - number of pixels in the span |
| 116 |
* x, y - location of leftmost pixel in the span |
| 117 |
* z - array of [n] z-values |
| 118 |
* index - array of [n] color indexes |
| 119 |
* primitive - either GL_POINT, GL_LINE, GL_POLYGON, or GL_BITMAP |
| 120 |
*/ |
| 121 |
void gl_write_index_span( GLcontext *ctx, |
| 122 |
GLuint n, GLint x, GLint y, GLdepth z[], |
| 123 |
GLuint index[], GLenum primitive ) |
| 124 |
{ |
| 125 |
GLuint i; |
| 126 |
GLubyte mask[MAX_WIDTH]; |
| 127 |
GLuint index_save[MAX_WIDTH]; |
| 128 |
|
| 129 |
/* init mask to 1's (all pixels are to be written) */ |
| 130 |
for (i=0;i<n;i++) |
| 131 |
mask[i] = 1; |
| 132 |
|
| 133 |
if ((ctx->RasterMask & WINCLIP_BIT) || primitive==GL_BITMAP) { |
| 134 |
if (clip_span(ctx,n,x,y,mask)==0) { |
| 135 |
return; |
| 136 |
} |
| 137 |
} |
| 138 |
|
| 139 |
/* Per-pixel fog */ |
| 140 |
if (ctx->Fog.Enabled |
| 141 |
&& (ctx->Hint.Fog==GL_NICEST || primitive==GL_BITMAP)) { |
| 142 |
gl_fog_index_pixels( ctx, n, z, index ); |
| 143 |
} |
| 144 |
|
| 145 |
/* Do the scissor test */ |
| 146 |
if (ctx->Scissor.Enabled) { |
| 147 |
if (gl_scissor_span( ctx, n, x, y, mask )==0) { |
| 148 |
return; |
| 149 |
} |
| 150 |
} |
| 151 |
|
| 152 |
/* Polygon Stippling */ |
| 153 |
if (ctx->Polygon.StippleFlag && primitive==GL_POLYGON) { |
| 154 |
stipple_polygon_span( ctx, n, x, y, mask ); |
| 155 |
} |
| 156 |
|
| 157 |
if (ctx->Stencil.Enabled) { |
| 158 |
/* first stencil test */ |
| 159 |
if (gl_stencil_span( ctx, n, x, y, mask )==0) { |
| 160 |
return; |
| 161 |
} |
| 162 |
/* depth buffering w/ stencil */ |
| 163 |
gl_depth_stencil_span( ctx, n, x, y, z, mask ); |
| 164 |
} |
| 165 |
else if (ctx->Depth.Test) { |
| 166 |
/* regular depth testing */ |
| 167 |
if ((*ctx->Driver.DepthTestSpan)( ctx, n, x, y, z, mask )==0) return; |
| 168 |
} |
| 169 |
|
| 170 |
if (ctx->RasterMask & NO_DRAW_BIT) { |
| 171 |
/* write no pixels */ |
| 172 |
return; |
| 173 |
} |
| 174 |
|
| 175 |
if (ctx->RasterMask & FRONT_AND_BACK_BIT) { |
| 176 |
/* Save a copy of the indexes since LogicOp and IndexMask |
| 177 |
* may change them |
| 178 |
*/ |
| 179 |
MEMCPY( index_save, index, n * sizeof(GLuint) ); |
| 180 |
} |
| 181 |
|
| 182 |
if (ctx->Color.SWLogicOpEnabled) { |
| 183 |
gl_logicop_ci_span( ctx, n, x, y, index, mask ); |
| 184 |
} |
| 185 |
if (ctx->Color.SWmasking) { |
| 186 |
gl_mask_index_span( ctx, n, x, y, index ); |
| 187 |
} |
| 188 |
|
| 189 |
/* write pixels */ |
| 190 |
(*ctx->Driver.WriteIndexSpan)( ctx, n, x, y, index, mask ); |
| 191 |
|
| 192 |
|
| 193 |
if (ctx->RasterMask & FRONT_AND_BACK_BIT) { |
| 194 |
/*** Also draw to back buffer ***/ |
| 195 |
(*ctx->Driver.SetBuffer)( ctx, GL_BACK ); |
| 196 |
MEMCPY( index, index_save, n * sizeof(GLuint) ); |
| 197 |
if (ctx->Color.SWLogicOpEnabled) { |
| 198 |
gl_logicop_ci_span( ctx, n, x, y, index, mask ); |
| 199 |
} |
| 200 |
if (ctx->Color.SWmasking) { |
| 201 |
gl_mask_index_span( ctx, n, x, y, index ); |
| 202 |
} |
| 203 |
(*ctx->Driver.WriteIndexSpan)( ctx, n, x, y, index, mask ); |
| 204 |
(*ctx->Driver.SetBuffer)( ctx, GL_FRONT ); |
| 205 |
} |
| 206 |
} |
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
void gl_write_monoindex_span( GLcontext *ctx, |
| 212 |
GLuint n, GLint x, GLint y, GLdepth z[], |
| 213 |
GLuint index, GLenum primitive ) |
| 214 |
{ |
| 215 |
GLuint i; |
| 216 |
GLubyte mask[MAX_WIDTH]; |
| 217 |
GLuint index_save[MAX_WIDTH]; |
| 218 |
|
| 219 |
/* init mask to 1's (all pixels are to be written) */ |
| 220 |
for (i=0;i<n;i++) |
| 221 |
mask[i] = 1; |
| 222 |
|
| 223 |
if ((ctx->RasterMask & WINCLIP_BIT) || primitive==GL_BITMAP) { |
| 224 |
if (clip_span( ctx,n,x,y,mask)==0) { |
| 225 |
return; |
| 226 |
} |
| 227 |
} |
| 228 |
|
| 229 |
/* Do the scissor test */ |
| 230 |
if (ctx->Scissor.Enabled) { |
| 231 |
if (gl_scissor_span( ctx, n, x, y, mask )==0) { |
| 232 |
return; |
| 233 |
} |
| 234 |
} |
| 235 |
|
| 236 |
/* Polygon Stippling */ |
| 237 |
if (ctx->Polygon.StippleFlag && primitive==GL_POLYGON) { |
| 238 |
stipple_polygon_span( ctx, n, x, y, mask ); |
| 239 |
} |
| 240 |
|
| 241 |
if (ctx->Stencil.Enabled) { |
| 242 |
/* first stencil test */ |
| 243 |
if (gl_stencil_span( ctx, n, x, y, mask )==0) { |
| 244 |
return; |
| 245 |
} |
| 246 |
/* depth buffering w/ stencil */ |
| 247 |
gl_depth_stencil_span( ctx, n, x, y, z, mask ); |
| 248 |
} |
| 249 |
else if (ctx->Depth.Test) { |
| 250 |
/* regular depth testing */ |
| 251 |
if ((*ctx->Driver.DepthTestSpan)( ctx, n, x, y, z, mask )==0) return; |
| 252 |
} |
| 253 |
|
| 254 |
if (ctx->RasterMask & NO_DRAW_BIT) { |
| 255 |
/* write no pixels */ |
| 256 |
return; |
| 257 |
} |
| 258 |
|
| 259 |
if ((ctx->Fog.Enabled && (ctx->Hint.Fog==GL_NICEST || primitive==GL_BITMAP)) |
| 260 |
|| ctx->Color.SWLogicOpEnabled || ctx->Color.SWmasking) { |
| 261 |
GLuint ispan[MAX_WIDTH]; |
| 262 |
/* index may change, replicate single index into an array */ |
| 263 |
for (i=0;i<n;i++) { |
| 264 |
ispan[i] = index; |
| 265 |
} |
| 266 |
|
| 267 |
if (ctx->Fog.Enabled |
| 268 |
&& (ctx->Hint.Fog==GL_NICEST || primitive==GL_BITMAP)) { |
| 269 |
gl_fog_index_pixels( ctx, n, z, ispan ); |
| 270 |
} |
| 271 |
|
| 272 |
if (ctx->RasterMask & FRONT_AND_BACK_BIT) { |
| 273 |
MEMCPY( index_save, index, n * sizeof(GLuint) ); |
| 274 |
} |
| 275 |
|
| 276 |
if (ctx->Color.SWLogicOpEnabled) { |
| 277 |
gl_logicop_ci_span( ctx, n, x, y, ispan, mask ); |
| 278 |
} |
| 279 |
|
| 280 |
if (ctx->Color.SWmasking) { |
| 281 |
gl_mask_index_span( ctx, n, x, y, ispan ); |
| 282 |
} |
| 283 |
|
| 284 |
(*ctx->Driver.WriteIndexSpan)( ctx, n, x, y, ispan, mask ); |
| 285 |
|
| 286 |
if (ctx->RasterMask & FRONT_AND_BACK_BIT) { |
| 287 |
/*** Also draw to back buffer ***/ |
| 288 |
(*ctx->Driver.SetBuffer)( ctx, GL_BACK ); |
| 289 |
for (i=0;i<n;i++) { |
| 290 |
ispan[i] = index; |
| 291 |
} |
| 292 |
if (ctx->Color.SWLogicOpEnabled) { |
| 293 |
gl_logicop_ci_span( ctx, n, x, y, ispan, mask ); |
| 294 |
} |
| 295 |
if (ctx->Color.SWmasking) { |
| 296 |
gl_mask_index_span( ctx, n, x, y, ispan ); |
| 297 |
} |
| 298 |
(*ctx->Driver.WriteIndexSpan)( ctx, n, x, y, ispan, mask ); |
| 299 |
(*ctx->Driver.SetBuffer)( ctx, GL_FRONT ); |
| 300 |
} |
| 301 |
} |
| 302 |
else { |
| 303 |
(*ctx->Driver.WriteMonoindexSpan)( ctx, n, x, y, mask ); |
| 304 |
|
| 305 |
if (ctx->RasterMask & FRONT_AND_BACK_BIT) { |
| 306 |
/*** Also draw to back buffer ***/ |
| 307 |
(*ctx->Driver.SetBuffer)( ctx, GL_BACK ); |
| 308 |
(*ctx->Driver.WriteMonoindexSpan)( ctx, n, x, y, mask ); |
| 309 |
(*ctx->Driver.SetBuffer)( ctx, GL_FRONT ); |
| 310 |
} |
| 311 |
} |
| 312 |
} |
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
void gl_write_color_span( GLcontext *ctx, |
| 317 |
GLuint n, GLint x, GLint y, GLdepth z[], |
| 318 |
GLubyte r[], GLubyte g[], |
| 319 |
GLubyte b[], GLubyte a[], |
| 320 |
GLenum primitive ) |
| 321 |
{ |
| 322 |
GLuint i; |
| 323 |
GLubyte mask[MAX_WIDTH]; |
| 324 |
GLboolean write_all = GL_TRUE; |
| 325 |
GLubyte rtmp[MAX_WIDTH], gtmp[MAX_WIDTH], btmp[MAX_WIDTH], atmp[MAX_WIDTH]; |
| 326 |
GLubyte *red, *green, *blue, *alpha; |
| 327 |
|
| 328 |
/* init mask to 1's (all pixels are to be written) */ |
| 329 |
for (i=0;i<n;i++) |
| 330 |
mask[i] = 1; |
| 331 |
|
| 332 |
if ((ctx->RasterMask & WINCLIP_BIT) || primitive==GL_BITMAP) { |
| 333 |
if (clip_span( ctx,n,x,y,mask)==0) { |
| 334 |
return; |
| 335 |
} |
| 336 |
write_all = GL_FALSE; |
| 337 |
} |
| 338 |
|
| 339 |
if ((primitive==GL_BITMAP && ctx->MutablePixels) |
| 340 |
|| (ctx->RasterMask & FRONT_AND_BACK_BIT)) { |
| 341 |
/* must make a copy of the colors since they may be modified */ |
| 342 |
MEMCPY( rtmp, r, n * sizeof(GLubyte) ); |
| 343 |
MEMCPY( gtmp, g, n * sizeof(GLubyte) ); |
| 344 |
MEMCPY( btmp, b, n * sizeof(GLubyte) ); |
| 345 |
MEMCPY( atmp, a, n * sizeof(GLubyte) ); |
| 346 |
red = rtmp; |
| 347 |
green = gtmp; |
| 348 |
blue = btmp; |
| 349 |
alpha = atmp; |
| 350 |
} |
| 351 |
else { |
| 352 |
red = r; |
| 353 |
green = g; |
| 354 |
blue = b; |
| 355 |
alpha = a; |
| 356 |
} |
| 357 |
|
| 358 |
/* Per-pixel fog */ |
| 359 |
if (ctx->Fog.Enabled |
| 360 |
&& (ctx->Hint.Fog==GL_NICEST || primitive==GL_BITMAP)) { |
| 361 |
gl_fog_color_pixels( ctx, n, z, red, green, blue, alpha ); |
| 362 |
} |
| 363 |
|
| 364 |
/* Do the scissor test */ |
| 365 |
if (ctx->Scissor.Enabled) { |
| 366 |
if (gl_scissor_span( ctx, n, x, y, mask )==0) { |
| 367 |
return; |
| 368 |
} |
| 369 |
write_all = GL_FALSE; |
| 370 |
} |
| 371 |
|
| 372 |
/* Polygon Stippling */ |
| 373 |
if (ctx->Polygon.StippleFlag && primitive==GL_POLYGON) { |
| 374 |
stipple_polygon_span( ctx, n, x, y, mask ); |
| 375 |
write_all = GL_FALSE; |
| 376 |
} |
| 377 |
|
| 378 |
/* Do the alpha test */ |
| 379 |
if (ctx->Color.AlphaEnabled) { |
| 380 |
if (gl_alpha_test( ctx, n, alpha, mask )==0) { |
| 381 |
return; |
| 382 |
} |
| 383 |
write_all = GL_FALSE; |
| 384 |
} |
| 385 |
|
| 386 |
if (ctx->Stencil.Enabled) { |
| 387 |
/* first stencil test */ |
| 388 |
if (gl_stencil_span( ctx, n, x, y, mask )==0) { |
| 389 |
return; |
| 390 |
} |
| 391 |
/* depth buffering w/ stencil */ |
| 392 |
gl_depth_stencil_span( ctx, n, x, y, z, mask ); |
| 393 |
write_all = GL_FALSE; |
| 394 |
} |
| 395 |
else if (ctx->Depth.Test) { |
| 396 |
/* regular depth testing */ |
| 397 |
GLuint m = (*ctx->Driver.DepthTestSpan)( ctx, n, x, y, z, mask ); |
| 398 |
if (m==0) { |
| 399 |
return; |
| 400 |
} |
| 401 |
if (m<n) { |
| 402 |
write_all = GL_FALSE; |
| 403 |
} |
| 404 |
} |
| 405 |
|
| 406 |
if (ctx->RasterMask & NO_DRAW_BIT) { |
| 407 |
/* write no pixels */ |
| 408 |
return; |
| 409 |
} |
| 410 |
|
| 411 |
/* logic op or blending */ |
| 412 |
if (ctx->Color.SWLogicOpEnabled) { |
| 413 |
gl_logicop_rgba_span( ctx, n, x, y, red, green, blue, alpha, mask ); |
| 414 |
} |
| 415 |
else if (ctx->Color.BlendEnabled) { |
| 416 |
gl_blend_span( ctx, n, x, y, red, green, blue, alpha, mask ); |
| 417 |
} |
| 418 |
|
| 419 |
/* Color component masking */ |
| 420 |
if (ctx->Color.SWmasking) { |
| 421 |
gl_mask_color_span( ctx, n, x, y, red, green, blue, alpha ); |
| 422 |
} |
| 423 |
|
| 424 |
/* write pixels */ |
| 425 |
(*ctx->Driver.WriteColorSpan)( ctx, n, x, y, red, green, blue, alpha, |
| 426 |
write_all ? NULL : mask ); |
| 427 |
if (ctx->RasterMask & ALPHABUF_BIT) { |
| 428 |
gl_write_alpha_span( ctx, n, x, y, alpha, write_all ? NULL : mask ); |
| 429 |
} |
| 430 |
|
| 431 |
if (ctx->RasterMask & FRONT_AND_BACK_BIT) { |
| 432 |
/*** Also render to back buffer ***/ |
| 433 |
MEMCPY( rtmp, r, n * sizeof(GLubyte) ); |
| 434 |
MEMCPY( gtmp, g, n * sizeof(GLubyte) ); |
| 435 |
MEMCPY( btmp, b, n * sizeof(GLubyte) ); |
| 436 |
MEMCPY( atmp, a, n * sizeof(GLubyte) ); |
| 437 |
(*ctx->Driver.SetBuffer)( ctx, GL_BACK ); |
| 438 |
if (ctx->Color.SWLogicOpEnabled) { |
| 439 |
gl_logicop_rgba_span( ctx, n, x, y, red, green, blue, alpha, mask ); |
| 440 |
} |
| 441 |
else if (ctx->Color.BlendEnabled) { |
| 442 |
gl_blend_span( ctx, n, x, y, red, green, blue, alpha, mask ); |
| 443 |
} |
| 444 |
if (ctx->Color.SWmasking) { |
| 445 |
gl_mask_color_span( ctx, n, x, y, red, green, blue, alpha ); |
| 446 |
} |
| 447 |
(*ctx->Driver.WriteColorSpan)( ctx, n, x, y, red, green, blue, alpha, |
| 448 |
write_all ? NULL : mask ); |
| 449 |
if (ctx->RasterMask & ALPHABUF_BIT) { |
| 450 |
ctx->Buffer->Alpha = ctx->Buffer->BackAlpha; |
| 451 |
gl_write_alpha_span( ctx, n, x, y, alpha, write_all ? NULL : mask ); |
| 452 |
ctx->Buffer->Alpha = ctx->Buffer->FrontAlpha; |
| 453 |
} |
| 454 |
(*ctx->Driver.SetBuffer)( ctx, GL_FRONT ); |
| 455 |
} |
| 456 |
|
| 457 |
} |
| 458 |
|
| 459 |
|
| 460 |
|
| 461 |
/* |
| 462 |
* Write a horizontal span of color pixels to the frame buffer. |
| 463 |
* The color is initially constant for the whole span. |
| 464 |
* Alpha-testing, stenciling, depth-testing, and blending are done as needed. |
| 465 |
* Input: n - number of pixels in the span |
| 466 |
* x, y - location of leftmost pixel in the span |
| 467 |
* z - array of [n] z-values |
| 468 |
* r, g, b, a - the color of the pixels |
| 469 |
* primitive - either GL_POINT, GL_LINE, GL_POLYGON or GL_BITMAP. |
| 470 |
*/ |
| 471 |
void gl_write_monocolor_span( GLcontext *ctx, |
| 472 |
GLuint n, GLint x, GLint y, GLdepth z[], |
| 473 |
GLint r, GLint g, GLint b, GLint a, |
| 474 |
GLenum primitive ) |
| 475 |
{ |
| 476 |
GLuint i; |
| 477 |
GLubyte mask[MAX_WIDTH]; |
| 478 |
GLboolean write_all = GL_TRUE; |
| 479 |
GLubyte red[MAX_WIDTH], green[MAX_WIDTH], blue[MAX_WIDTH], alpha[MAX_WIDTH]; |
| 480 |
|
| 481 |
/* init mask to 1's (all pixels are to be written) */ |
| 482 |
for (i=0;i<n;i++) |
| 483 |
mask[i] = 1; |
| 484 |
|
| 485 |
if ((ctx->RasterMask & WINCLIP_BIT) || primitive==GL_BITMAP) { |
| 486 |
if (clip_span( ctx,n,x,y,mask)==0) { |
| 487 |
return; |
| 488 |
} |
| 489 |
write_all = GL_FALSE; |
| 490 |
} |
| 491 |
|
| 492 |
/* Do the scissor test */ |
| 493 |
if (ctx->Scissor.Enabled) { |
| 494 |
if (gl_scissor_span( ctx, n, x, y, mask )==0) { |
| 495 |
return; |
| 496 |
} |
| 497 |
write_all = GL_FALSE; |
| 498 |
} |
| 499 |
|
| 500 |
/* Polygon Stippling */ |
| 501 |
if (ctx->Polygon.StippleFlag && primitive==GL_POLYGON) { |
| 502 |
stipple_polygon_span( ctx, n, x, y, mask ); |
| 503 |
write_all = GL_FALSE; |
| 504 |
} |
| 505 |
|
| 506 |
/* Do the alpha test */ |
| 507 |
if (ctx->Color.AlphaEnabled) { |
| 508 |
GLubyte alpha[MAX_WIDTH]; |
| 509 |
for (i=0;i<n;i++) { |
| 510 |
alpha[i] = a; |
| 511 |
} |
| 512 |
if (gl_alpha_test( ctx, n, alpha, mask )==0) { |
| 513 |
return; |
| 514 |
} |
| 515 |
write_all = GL_FALSE; |
| 516 |
} |
| 517 |
|
| 518 |
if (ctx->Stencil.Enabled) { |
| 519 |
/* first stencil test */ |
| 520 |
if (gl_stencil_span( ctx, n, x, y, mask )==0) { |
| 521 |
return; |
| 522 |
} |
| 523 |
/* depth buffering w/ stencil */ |
| 524 |
gl_depth_stencil_span( ctx, n, x, y, z, mask ); |
| 525 |
write_all = GL_FALSE; |
| 526 |
} |
| 527 |
else if (ctx->Depth.Test) { |
| 528 |
/* regular depth testing */ |
| 529 |
GLuint m = (*ctx->Driver.DepthTestSpan)( ctx, n, x, y, z, mask ); |
| 530 |
if (m==0) { |
| 531 |
return; |
| 532 |
} |
| 533 |
if (m<n) { |
| 534 |
write_all = GL_FALSE; |
| 535 |
} |
| 536 |
} |
| 537 |
|
| 538 |
if (ctx->RasterMask & NO_DRAW_BIT) { |
| 539 |
/* write no pixels */ |
| 540 |
return; |
| 541 |
} |
| 542 |
|
| 543 |
if (ctx->Color.BlendEnabled || ctx->Color.SWLogicOpEnabled |
| 544 |
|| ctx->Color.SWmasking) { |
| 545 |
/* assign same color to each pixel */ |
| 546 |
for (i=0;i<n;i++) { |
| 547 |
if (mask[i]) { |
| 548 |
red[i] = r; |
| 549 |
green[i] = g; |
| 550 |
blue[i] = b; |
| 551 |
alpha[i] = a; |
| 552 |
} |
| 553 |
} |
| 554 |
|
| 555 |
if (ctx->Color.SWLogicOpEnabled) { |
| 556 |
gl_logicop_rgba_span( ctx, n, x, y, red, green, blue, alpha, mask ); |
| 557 |
} |
| 558 |
else if (ctx->Color.BlendEnabled) { |
| 559 |
gl_blend_span( ctx, n, x, y, red, green, blue, alpha, mask ); |
| 560 |
} |
| 561 |
|
| 562 |
/* Color component masking */ |
| 563 |
if (ctx->Color.SWmasking) { |
| 564 |
gl_mask_color_span( ctx, n, x, y, red, green, blue, alpha ); |
| 565 |
} |
| 566 |
|
| 567 |
/* write pixels */ |
| 568 |
(*ctx->Driver.WriteColorSpan)( ctx, n, x, y, red, green, blue, alpha, |
| 569 |
write_all ? NULL : mask ); |
| 570 |
if (ctx->RasterMask & ALPHABUF_BIT) { |
| 571 |
gl_write_alpha_span( ctx, n, x, y, alpha, write_all ? NULL : mask ); |
| 572 |
} |
| 573 |
|
| 574 |
if (ctx->RasterMask & FRONT_AND_BACK_BIT) { |
| 575 |
/*** Also draw to back buffer ***/ |
| 576 |
for (i=0;i<n;i++) { |
| 577 |
if (mask[i]) { |
| 578 |
red[i] = r; |
| 579 |
green[i] = g; |
| 580 |
blue[i] = b; |
| 581 |
alpha[i] = a; |
| 582 |
} |
| 583 |
} |
| 584 |
(*ctx->Driver.SetBuffer)( ctx, GL_BACK ); |
| 585 |
if (ctx->Color.SWLogicOpEnabled) { |
| 586 |
gl_logicop_rgba_span( ctx, n, x, y, red, green, blue, alpha, mask); |
| 587 |
} |
| 588 |
else if (ctx->Color.BlendEnabled) { |
| 589 |
gl_blend_span( ctx, n, x, y, red, green, blue, alpha, mask ); |
| 590 |
} |
| 591 |
if (ctx->Color.SWmasking) { |
| 592 |
gl_mask_color_span( ctx, n, x, y, red, green, blue, alpha ); |
| 593 |
} |
| 594 |
(*ctx->Driver.WriteColorSpan)( ctx, n, x, y, red, green, blue, alpha, |
| 595 |
write_all ? NULL : mask ); |
| 596 |
(*ctx->Driver.SetBuffer)( ctx, GL_FRONT ); |
| 597 |
if (ctx->RasterMask & ALPHABUF_BIT) { |
| 598 |
ctx->Buffer->Alpha = ctx->Buffer->BackAlpha; |
| 599 |
gl_write_alpha_span( ctx, n, x, y, alpha, |
| 600 |
write_all ? NULL : mask ); |
| 601 |
ctx->Buffer->Alpha = ctx->Buffer->FrontAlpha; |
| 602 |
} |
| 603 |
} |
| 604 |
} |
| 605 |
else { |
| 606 |
(*ctx->Driver.WriteMonocolorSpan)( ctx, n, x, y, mask ); |
| 607 |
if (ctx->RasterMask & ALPHABUF_BIT) { |
| 608 |
gl_write_mono_alpha_span( ctx, n, x, y, a, write_all ? NULL : mask ); |
| 609 |
} |
| 610 |
if (ctx->RasterMask & FRONT_AND_BACK_BIT) { |
| 611 |
/* Also draw to back buffer */ |
| 612 |
(*ctx->Driver.SetBuffer)( ctx, GL_BACK ); |
| 613 |
(*ctx->Driver.WriteMonocolorSpan)( ctx, n, x, y, mask ); |
| 614 |
(*ctx->Driver.SetBuffer)( ctx, GL_FRONT ); |
| 615 |
if (ctx->RasterMask & ALPHABUF_BIT) { |
| 616 |
ctx->Buffer->Alpha = ctx->Buffer->BackAlpha; |
| 617 |
gl_write_mono_alpha_span( ctx, n, x, y, a, |
| 618 |
write_all ? NULL : mask ); |
| 619 |
ctx->Buffer->Alpha = ctx->Buffer->FrontAlpha; |
| 620 |
} |
| 621 |
} |
| 622 |
} |
| 623 |
} |
| 624 |
|
| 625 |
|
| 626 |
|
| 627 |
/* |
| 628 |
* Write a horizontal span of textured pixels to the frame buffer. |
| 629 |
* The color of each pixel is different. |
| 630 |
* Alpha-testing, stenciling, depth-testing, and blending are done |
| 631 |
* as needed. |
| 632 |
* Input: n - number of pixels in the span |
| 633 |
* x, y - location of leftmost pixel in the span |
| 634 |
* z - array of [n] z-values |
| 635 |
* s, t - array of (s,t) texture coordinates for each pixel |
| 636 |
* lambda - array of texture lambda values |
| 637 |
* red, green, blue, alpha - array of [n] color components |
| 638 |
* primitive - either GL_POINT, GL_LINE, GL_POLYGON or GL_BITMAP. |
| 639 |
*/ |
| 640 |
void gl_write_texture_span( GLcontext *ctx, |
| 641 |
GLuint n, GLint x, GLint y, GLdepth z[], |
| 642 |
GLfloat s[], GLfloat t[], GLfloat u[], |
| 643 |
GLfloat lambda[], |
| 644 |
GLubyte r[], GLubyte g[], |
| 645 |
GLubyte b[], GLubyte a[], |
| 646 |
GLenum primitive ) |
| 647 |
{ |
| 648 |
GLuint i; |
| 649 |
GLubyte mask[MAX_WIDTH]; |
| 650 |
GLboolean write_all = GL_TRUE; |
| 651 |
GLubyte rtmp[MAX_WIDTH], gtmp[MAX_WIDTH], btmp[MAX_WIDTH], atmp[MAX_WIDTH]; |
| 652 |
GLubyte *red, *green, *blue, *alpha; |
| 653 |
|
| 654 |
/* init mask to 1's (all pixels are to be written) */ |
| 655 |
for (i=0;i<n;i++) |
| 656 |
mask[i] = 1; |
| 657 |
|
| 658 |
if ((ctx->RasterMask & WINCLIP_BIT) || primitive==GL_BITMAP) { |
| 659 |
if (clip_span( ctx,n,x,y,mask)==0) { |
| 660 |
return; |
| 661 |
} |
| 662 |
write_all = GL_FALSE; |
| 663 |
} |
| 664 |
|
| 665 |
|
| 666 |
if (primitive==GL_BITMAP || (ctx->RasterMask & FRONT_AND_BACK_BIT)) { |
| 667 |
/* must make a copy of the colors since they may be modified */ |
| 668 |
MEMCPY( rtmp, r, n * sizeof(GLubyte) ); |
| 669 |
MEMCPY( gtmp, g, n * sizeof(GLubyte) ); |
| 670 |
MEMCPY( btmp, b, n * sizeof(GLubyte) ); |
| 671 |
MEMCPY( atmp, a, n * sizeof(GLubyte) ); |
| 672 |
red = rtmp; |
| 673 |
green = gtmp; |
| 674 |
blue = btmp; |
| 675 |
alpha = atmp; |
| 676 |
} |
| 677 |
else { |
| 678 |
red = r; |
| 679 |
green = g; |
| 680 |
blue = b; |
| 681 |
alpha = a; |
| 682 |
} |
| 683 |
|
| 684 |
/* Texture */ |
| 685 |
if (ctx->Texture.Enabled & TEXTURE_3D) { |
| 686 |
gl_texture_pixels_3d( ctx, n, s, t, u, lambda, red, green, blue, alpha ); |
| 687 |
} |
| 688 |
else if (ctx->Texture.Enabled & TEXTURE_2D) { |
| 689 |
gl_texture_pixels_2d( ctx, n, s, t, lambda, red, green, blue, alpha ); |
| 690 |
} |
| 691 |
else if (ctx->Texture.Enabled & TEXTURE_1D) { |
| 692 |
gl_texture_pixels_1d( ctx, n, s, lambda, red, green, blue, alpha ); |
| 693 |
} |
| 694 |
|
| 695 |
|
| 696 |
/* Per-pixel fog */ |
| 697 |
if (ctx->Fog.Enabled |
| 698 |
&& (ctx->Hint.Fog==GL_NICEST || primitive==GL_BITMAP)) { |
| 699 |
gl_fog_color_pixels( ctx, n, z, red, green, blue, alpha ); |
| 700 |
} |
| 701 |
|
| 702 |
/* Do the scissor test */ |
| 703 |
if (ctx->Scissor.Enabled) { |
| 704 |
if (gl_scissor_span( ctx, n, x, y, mask )==0) { |
| 705 |
return; |
| 706 |
} |
| 707 |
write_all = GL_FALSE; |
| 708 |
} |
| 709 |
|
| 710 |
/* Polygon Stippling */ |
| 711 |
if (ctx->Polygon.StippleFlag && primitive==GL_POLYGON) { |
| 712 |
stipple_polygon_span( ctx, n, x, y, mask ); |
| 713 |
write_all = GL_FALSE; |
| 714 |
} |
| 715 |
|
| 716 |
/* Do the alpha test */ |
| 717 |
if (ctx->Color.AlphaEnabled) { |
| 718 |
if (gl_alpha_test( ctx, n, alpha, mask )==0) { |
| 719 |
return; |
| 720 |
} |
| 721 |
write_all = GL_FALSE; |
| 722 |
} |
| 723 |
|
| 724 |
if (ctx->Stencil.Enabled) { |
| 725 |
/* first stencil test */ |
| 726 |
if (gl_stencil_span( ctx, n, x, y, mask )==0) { |
| 727 |
return; |
| 728 |
} |
| 729 |
/* depth buffering w/ stencil */ |
| 730 |
gl_depth_stencil_span( ctx, n, x, y, z, mask ); |
| 731 |
write_all = GL_FALSE; |
| 732 |
} |
| 733 |
else if (ctx->Depth.Test) { |
| 734 |
/* regular depth testing */ |
| 735 |
GLuint m = (*ctx->Driver.DepthTestSpan)( ctx, n, x, y, z, mask ); |
| 736 |
if (m==0) { |
| 737 |
return; |
| 738 |
} |
| 739 |
if (m<n) { |
| 740 |
write_all = GL_FALSE; |
| 741 |
} |
| 742 |
} |
| 743 |
|
| 744 |
if (ctx->RasterMask & NO_DRAW_BIT) { |
| 745 |
/* write no pixels */ |
| 746 |
return; |
| 747 |
} |
| 748 |
|
| 749 |
/* blending */ |
| 750 |
if (ctx->Color.SWLogicOpEnabled) { |
| 751 |
gl_logicop_rgba_span( ctx, n, x, y, red, green, blue, alpha, mask ); |
| 752 |
} |
| 753 |
else if (ctx->Color.BlendEnabled) { |
| 754 |
gl_blend_span( ctx, n, x, y, red, green, blue, alpha, mask ); |
| 755 |
} |
| 756 |
|
| 757 |
if (ctx->Color.SWmasking) { |
| 758 |
gl_mask_color_span( ctx, n, x, y, red, green, blue, alpha ); |
| 759 |
} |
| 760 |
|
| 761 |
/* write pixels */ |
| 762 |
(*ctx->Driver.WriteColorSpan)( ctx, n, x, y, red, green, blue, alpha, |
| 763 |
write_all ? NULL : mask ); |
| 764 |
if (ctx->RasterMask & ALPHABUF_BIT) { |
| 765 |
gl_write_alpha_span( ctx, n, x, y, alpha, write_all ? NULL : mask ); |
| 766 |
} |
| 767 |
|
| 768 |
if (ctx->RasterMask & FRONT_AND_BACK_BIT) { |
| 769 |
/* Also draw to back buffer */ |
| 770 |
MEMCPY( rtmp, r, n * sizeof(GLubyte) ); |
| 771 |
MEMCPY( gtmp, g, n * sizeof(GLubyte) ); |
| 772 |
MEMCPY( btmp, b, n * sizeof(GLubyte) ); |
| 773 |
MEMCPY( atmp, a, n * sizeof(GLubyte) ); |
| 774 |
(*ctx->Driver.SetBuffer)( ctx, GL_BACK ); |
| 775 |
if (ctx->Color.SWLogicOpEnabled) { |
| 776 |
gl_logicop_rgba_span( ctx, n, x, y, red, green, blue, alpha, mask ); |
| 777 |
} |
| 778 |
else if (ctx->Color.BlendEnabled) { |
| 779 |
gl_blend_span( ctx, n, x, y, red, green, blue, alpha, mask ); |
| 780 |
} |
| 781 |
if (ctx->Color.SWmasking) { |
| 782 |
gl_mask_color_span( ctx, n, x, y, red, green, blue, alpha ); |
| 783 |
} |
| 784 |
(*ctx->Driver.WriteColorSpan)( ctx, n, x, y, red, green, blue, alpha, |
| 785 |
write_all ? NULL : mask ); |
| 786 |
(*ctx->Driver.SetBuffer)( ctx, GL_FRONT ); |
| 787 |
if (ctx->RasterMask & ALPHABUF_BIT) { |
| 788 |
ctx->Buffer->Alpha = ctx->Buffer->BackAlpha; |
| 789 |
gl_write_alpha_span( ctx, n, x, y, alpha, write_all ? NULL : mask ); |
| 790 |
ctx->Buffer->Alpha = ctx->Buffer->FrontAlpha; |
| 791 |
} |
| 792 |
} |
| 793 |
} |
| 794 |
|
| 795 |
|
| 796 |
|
| 797 |
/* |
| 798 |
* Read RGBA pixels from frame buffer. Clipping will be done to prevent |
| 799 |
* reading ouside the buffer's boundaries. |
| 800 |
*/ |
| 801 |
void gl_read_color_span( GLcontext *ctx, |
| 802 |
GLuint n, GLint x, GLint y, |
| 803 |
GLubyte red[], GLubyte green[], |
| 804 |
GLubyte blue[], GLubyte alpha[] ) |
| 805 |
{ |
| 806 |
register GLuint i; |
| 807 |
|
| 808 |
if (y<0 || y>=ctx->Buffer->Height || x>=ctx->Buffer->Width) { |
| 809 |
/* completely above, below, or right */ |
| 810 |
for (i=0;i<n;i++) { |
| 811 |
red[i] = green[i] = blue[i] = alpha[i] = 0; |
| 812 |
} |
| 813 |
} |
| 814 |
else { |
| 815 |
if (x>=0 && x+n<=ctx->Buffer->Width) { |
| 816 |
/* OK */ |
| 817 |
(*ctx->Driver.ReadColorSpan)( ctx, n, x, y, red, green, blue, alpha ); |
| 818 |
if (ctx->RasterMask & ALPHABUF_BIT) { |
| 819 |
gl_read_alpha_span( ctx, n, x, y, alpha ); |
| 820 |
} |
| 821 |
} |
| 822 |
else { |
| 823 |
i = 0; |
| 824 |
if (x<0) { |
| 825 |
while (x<0 && n>0) { |
| 826 |
red[i] = green[i] = blue[i] = alpha[i] = 0; |
| 827 |
x++; |
| 828 |
n--; |
| 829 |
i++; |
| 830 |
} |
| 831 |
} |
| 832 |
n = MIN2( n, ctx->Buffer->Width - x ); |
| 833 |
(*ctx->Driver.ReadColorSpan)( ctx, n, x, y, red+i, green+i, blue+i, alpha+i); |
| 834 |
if (ctx->RasterMask & ALPHABUF_BIT) { |
| 835 |
gl_read_alpha_span( ctx, n, x, y, alpha+i ); |
| 836 |
} |
| 837 |
} |
| 838 |
} |
| 839 |
} |
| 840 |
|
| 841 |
|
| 842 |
|
| 843 |
|
| 844 |
/* |
| 845 |
* Read CI pixels from frame buffer. Clipping will be done to prevent |
| 846 |
* reading ouside the buffer's boundaries. |
| 847 |
*/ |
| 848 |
void gl_read_index_span( GLcontext *ctx, |
| 849 |
GLuint n, GLint x, GLint y, GLuint indx[] ) |
| 850 |
{ |
| 851 |
register GLuint i; |
| 852 |
|
| 853 |
if (y<0 || y>=ctx->Buffer->Height || x>=ctx->Buffer->Width) { |
| 854 |
/* completely above, below, or right */ |
| 855 |
for (i=0;i<n;i++) { |
| 856 |
indx[i] = 0; |
| 857 |
} |
| 858 |
} |
| 859 |
else { |
| 860 |
if (x>=0 && x+n<=ctx->Buffer->Width) { |
| 861 |
/* OK */ |
| 862 |
(*ctx->Driver.ReadIndexSpan)( ctx, n, x, y, indx ); |
| 863 |
} |
| 864 |
else { |
| 865 |
i = 0; |
| 866 |
if (x<0) { |
| 867 |
while (x<0 && n>0) { |
| 868 |
indx[i] = 0; |
| 869 |
x++; |
| 870 |
n--; |
| 871 |
i++; |
| 872 |
} |
| 873 |
} |
| 874 |
n = MIN2( n, ctx->Buffer->Width - x ); |
| 875 |
(*ctx->Driver.ReadIndexSpan)( ctx, n, x, y, indx+i ); |
| 876 |
} |
| 877 |
} |
| 878 |
} |
| 879 |
|
| 880 |
|