| 1 |
/* |
| 2 |
* Mesa 3-D graphics library |
| 3 |
* Version: 6.4.1 |
| 4 |
* |
| 5 |
* Copyright (C) 1999-2005 Brian Paul All Rights Reserved. |
| 6 |
* |
| 7 |
* Permission is hereby granted, free of charge, to any person obtaining a |
| 8 |
* copy of this software and associated documentation files (the "Software"), |
| 9 |
* to deal in the Software without restriction, including without limitation |
| 10 |
* the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 11 |
* and/or sell copies of the Software, and to permit persons to whom the |
| 12 |
* Software is furnished to do so, subject to the following conditions: |
| 13 |
* |
| 14 |
* The above copyright notice and this permission notice shall be included |
| 15 |
* in all copies or substantial portions of the Software. |
| 16 |
* |
| 17 |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
| 18 |
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 19 |
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 20 |
* BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN |
| 21 |
* AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
| 22 |
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 23 |
*/ |
| 24 |
|
| 25 |
|
| 26 |
#include "glheader.h" |
| 27 |
#include "context.h" |
| 28 |
#include "macros.h" |
| 29 |
#include "imports.h" |
| 30 |
#include "fbobject.h" |
| 31 |
|
| 32 |
#include "s_context.h" |
| 33 |
#include "s_depth.h" |
| 34 |
#include "s_stencil.h" |
| 35 |
#include "s_span.h" |
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
/* Stencil Logic: |
| 40 |
|
| 41 |
IF stencil test fails THEN |
| 42 |
Apply fail-op to stencil value |
| 43 |
Don't write the pixel (RGBA,Z) |
| 44 |
ELSE |
| 45 |
IF doing depth test && depth test fails THEN |
| 46 |
Apply zfail-op to stencil value |
| 47 |
Write RGBA and Z to appropriate buffers |
| 48 |
ELSE |
| 49 |
Apply zpass-op to stencil value |
| 50 |
ENDIF |
| 51 |
|
| 52 |
*/ |
| 53 |
|
| 54 |
|
| 55 |
/** |
| 56 |
* Apply the given stencil operator to the array of stencil values. |
| 57 |
* Don't touch stencil[i] if mask[i] is zero. |
| 58 |
* Input: n - size of stencil array |
| 59 |
* oper - the stencil buffer operator |
| 60 |
* face - 0 or 1 for front or back face operation |
| 61 |
* stencil - array of stencil values |
| 62 |
* mask - array [n] of flag: 1=apply operator, 0=don't apply operator |
| 63 |
* Output: stencil - modified values |
| 64 |
*/ |
| 65 |
static void |
| 66 |
apply_stencil_op( const GLcontext *ctx, GLenum oper, GLuint face, |
| 67 |
GLuint n, GLstencil stencil[], const GLubyte mask[] ) |
| 68 |
{ |
| 69 |
const GLstencil ref = ctx->Stencil.Ref[face]; |
| 70 |
const GLstencil wrtmask = ctx->Stencil.WriteMask[face]; |
| 71 |
const GLstencil invmask = (GLstencil) (~wrtmask); |
| 72 |
GLuint i; |
| 73 |
|
| 74 |
switch (oper) { |
| 75 |
case GL_KEEP: |
| 76 |
/* do nothing */ |
| 77 |
break; |
| 78 |
case GL_ZERO: |
| 79 |
if (invmask==0) { |
| 80 |
for (i=0;i<n;i++) { |
| 81 |
if (mask[i]) { |
| 82 |
stencil[i] = 0; |
| 83 |
} |
| 84 |
} |
| 85 |
} |
| 86 |
else { |
| 87 |
for (i=0;i<n;i++) { |
| 88 |
if (mask[i]) { |
| 89 |
stencil[i] = (GLstencil) (stencil[i] & invmask); |
| 90 |
} |
| 91 |
} |
| 92 |
} |
| 93 |
break; |
| 94 |
case GL_REPLACE: |
| 95 |
if (invmask==0) { |
| 96 |
for (i=0;i<n;i++) { |
| 97 |
if (mask[i]) { |
| 98 |
stencil[i] = ref; |
| 99 |
} |
| 100 |
} |
| 101 |
} |
| 102 |
else { |
| 103 |
for (i=0;i<n;i++) { |
| 104 |
if (mask[i]) { |
| 105 |
GLstencil s = stencil[i]; |
| 106 |
stencil[i] = (GLstencil) ((invmask & s ) | (wrtmask & ref)); |
| 107 |
} |
| 108 |
} |
| 109 |
} |
| 110 |
break; |
| 111 |
case GL_INCR: |
| 112 |
if (invmask==0) { |
| 113 |
for (i=0;i<n;i++) { |
| 114 |
if (mask[i]) { |
| 115 |
GLstencil s = stencil[i]; |
| 116 |
if (s < STENCIL_MAX) { |
| 117 |
stencil[i] = (GLstencil) (s+1); |
| 118 |
} |
| 119 |
} |
| 120 |
} |
| 121 |
} |
| 122 |
else { |
| 123 |
for (i=0;i<n;i++) { |
| 124 |
if (mask[i]) { |
| 125 |
/* VERIFY logic of adding 1 to a write-masked value */ |
| 126 |
GLstencil s = stencil[i]; |
| 127 |
if (s < STENCIL_MAX) { |
| 128 |
stencil[i] = (GLstencil) ((invmask & s) | (wrtmask & (s+1))); |
| 129 |
} |
| 130 |
} |
| 131 |
} |
| 132 |
} |
| 133 |
break; |
| 134 |
case GL_DECR: |
| 135 |
if (invmask==0) { |
| 136 |
for (i=0;i<n;i++) { |
| 137 |
if (mask[i]) { |
| 138 |
GLstencil s = stencil[i]; |
| 139 |
if (s>0) { |
| 140 |
stencil[i] = (GLstencil) (s-1); |
| 141 |
} |
| 142 |
} |
| 143 |
} |
| 144 |
} |
| 145 |
else { |
| 146 |
for (i=0;i<n;i++) { |
| 147 |
if (mask[i]) { |
| 148 |
/* VERIFY logic of subtracting 1 to a write-masked value */ |
| 149 |
GLstencil s = stencil[i]; |
| 150 |
if (s>0) { |
| 151 |
stencil[i] = (GLstencil) ((invmask & s) | (wrtmask & (s-1))); |
| 152 |
} |
| 153 |
} |
| 154 |
} |
| 155 |
} |
| 156 |
break; |
| 157 |
case GL_INCR_WRAP_EXT: |
| 158 |
if (invmask==0) { |
| 159 |
for (i=0;i<n;i++) { |
| 160 |
if (mask[i]) { |
| 161 |
stencil[i]++; |
| 162 |
} |
| 163 |
} |
| 164 |
} |
| 165 |
else { |
| 166 |
for (i=0;i<n;i++) { |
| 167 |
if (mask[i]) { |
| 168 |
GLstencil s = stencil[i]; |
| 169 |
stencil[i] = (GLstencil) ((invmask & s) | (wrtmask & (s+1))); |
| 170 |
} |
| 171 |
} |
| 172 |
} |
| 173 |
break; |
| 174 |
case GL_DECR_WRAP_EXT: |
| 175 |
if (invmask==0) { |
| 176 |
for (i=0;i<n;i++) { |
| 177 |
if (mask[i]) { |
| 178 |
stencil[i]--; |
| 179 |
} |
| 180 |
} |
| 181 |
} |
| 182 |
else { |
| 183 |
for (i=0;i<n;i++) { |
| 184 |
if (mask[i]) { |
| 185 |
GLstencil s = stencil[i]; |
| 186 |
stencil[i] = (GLstencil) ((invmask & s) | (wrtmask & (s-1))); |
| 187 |
} |
| 188 |
} |
| 189 |
} |
| 190 |
break; |
| 191 |
case GL_INVERT: |
| 192 |
if (invmask==0) { |
| 193 |
for (i=0;i<n;i++) { |
| 194 |
if (mask[i]) { |
| 195 |
GLstencil s = stencil[i]; |
| 196 |
stencil[i] = (GLstencil) ~s; |
| 197 |
} |
| 198 |
} |
| 199 |
} |
| 200 |
else { |
| 201 |
for (i=0;i<n;i++) { |
| 202 |
if (mask[i]) { |
| 203 |
GLstencil s = stencil[i]; |
| 204 |
stencil[i] = (GLstencil) ((invmask & s) | (wrtmask & ~s)); |
| 205 |
} |
| 206 |
} |
| 207 |
} |
| 208 |
break; |
| 209 |
default: |
| 210 |
_mesa_problem(ctx, "Bad stencil op in apply_stencil_op"); |
| 211 |
} |
| 212 |
} |
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
/** |
| 218 |
* Apply stencil test to an array of stencil values (before depth buffering). |
| 219 |
* Input: face - 0 or 1 for front or back-face polygons |
| 220 |
* n - number of pixels in the array |
| 221 |
* stencil - array of [n] stencil values |
| 222 |
* mask - array [n] of flag: 0=skip the pixel, 1=stencil the pixel |
| 223 |
* Output: mask - pixels which fail the stencil test will have their |
| 224 |
* mask flag set to 0. |
| 225 |
* stencil - updated stencil values (where the test passed) |
| 226 |
* Return: GL_FALSE = all pixels failed, GL_TRUE = zero or more pixels passed. |
| 227 |
*/ |
| 228 |
static GLboolean |
| 229 |
do_stencil_test( GLcontext *ctx, GLuint face, GLuint n, GLstencil stencil[], |
| 230 |
GLubyte mask[] ) |
| 231 |
{ |
| 232 |
GLubyte fail[MAX_WIDTH]; |
| 233 |
GLboolean allfail = GL_FALSE; |
| 234 |
GLuint i; |
| 235 |
GLstencil r, s; |
| 236 |
const GLuint valueMask = ctx->Stencil.ValueMask[face]; |
| 237 |
|
| 238 |
ASSERT(n <= MAX_WIDTH); |
| 239 |
|
| 240 |
/* |
| 241 |
* Perform stencil test. The results of this operation are stored |
| 242 |
* in the fail[] array: |
| 243 |
* IF fail[i] is non-zero THEN |
| 244 |
* the stencil fail operator is to be applied |
| 245 |
* ELSE |
| 246 |
* the stencil fail operator is not to be applied |
| 247 |
* ENDIF |
| 248 |
*/ |
| 249 |
switch (ctx->Stencil.Function[face]) { |
| 250 |
case GL_NEVER: |
| 251 |
/* never pass; always fail */ |
| 252 |
for (i=0;i<n;i++) { |
| 253 |
if (mask[i]) { |
| 254 |
mask[i] = 0; |
| 255 |
fail[i] = 1; |
| 256 |
} |
| 257 |
else { |
| 258 |
fail[i] = 0; |
| 259 |
} |
| 260 |
} |
| 261 |
allfail = GL_TRUE; |
| 262 |
break; |
| 263 |
case GL_LESS: |
| 264 |
r = (GLstencil) (ctx->Stencil.Ref[face] & valueMask); |
| 265 |
for (i=0;i<n;i++) { |
| 266 |
if (mask[i]) { |
| 267 |
s = (GLstencil) (stencil[i] & valueMask); |
| 268 |
if (r < s) { |
| 269 |
/* passed */ |
| 270 |
fail[i] = 0; |
| 271 |
} |
| 272 |
else { |
| 273 |
fail[i] = 1; |
| 274 |
mask[i] = 0; |
| 275 |
} |
| 276 |
} |
| 277 |
else { |
| 278 |
fail[i] = 0; |
| 279 |
} |
| 280 |
} |
| 281 |
break; |
| 282 |
case GL_LEQUAL: |
| 283 |
r = (GLstencil) (ctx->Stencil.Ref[face] & valueMask); |
| 284 |
for (i=0;i<n;i++) { |
| 285 |
if (mask[i]) { |
| 286 |
s = (GLstencil) (stencil[i] & valueMask); |
| 287 |
if (r <= s) { |
| 288 |
/* pass */ |
| 289 |
fail[i] = 0; |
| 290 |
} |
| 291 |
else { |
| 292 |
fail[i] = 1; |
| 293 |
mask[i] = 0; |
| 294 |
} |
| 295 |
} |
| 296 |
else { |
| 297 |
fail[i] = 0; |
| 298 |
} |
| 299 |
} |
| 300 |
break; |
| 301 |
case GL_GREATER: |
| 302 |
r = (GLstencil) (ctx->Stencil.Ref[face] & valueMask); |
| 303 |
for (i=0;i<n;i++) { |
| 304 |
if (mask[i]) { |
| 305 |
s = (GLstencil) (stencil[i] & valueMask); |
| 306 |
if (r > s) { |
| 307 |
/* passed */ |
| 308 |
fail[i] = 0; |
| 309 |
} |
| 310 |
else { |
| 311 |
fail[i] = 1; |
| 312 |
mask[i] = 0; |
| 313 |
} |
| 314 |
} |
| 315 |
else { |
| 316 |
fail[i] = 0; |
| 317 |
} |
| 318 |
} |
| 319 |
break; |
| 320 |
case GL_GEQUAL: |
| 321 |
r = (GLstencil) (ctx->Stencil.Ref[face] & valueMask); |
| 322 |
for (i=0;i<n;i++) { |
| 323 |
if (mask[i]) { |
| 324 |
s = (GLstencil) (stencil[i] & valueMask); |
| 325 |
if (r >= s) { |
| 326 |
/* passed */ |
| 327 |
fail[i] = 0; |
| 328 |
} |
| 329 |
else { |
| 330 |
fail[i] = 1; |
| 331 |
mask[i] = 0; |
| 332 |
} |
| 333 |
} |
| 334 |
else { |
| 335 |
fail[i] = 0; |
| 336 |
} |
| 337 |
} |
| 338 |
break; |
| 339 |
case GL_EQUAL: |
| 340 |
r = (GLstencil) (ctx->Stencil.Ref[face] & valueMask); |
| 341 |
for (i=0;i<n;i++) { |
| 342 |
if (mask[i]) { |
| 343 |
s = (GLstencil) (stencil[i] & valueMask); |
| 344 |
if (r == s) { |
| 345 |
/* passed */ |
| 346 |
fail[i] = 0; |
| 347 |
} |
| 348 |
else { |
| 349 |
fail[i] = 1; |
| 350 |
mask[i] = 0; |
| 351 |
} |
| 352 |
} |
| 353 |
else { |
| 354 |
fail[i] = 0; |
| 355 |
} |
| 356 |
} |
| 357 |
break; |
| 358 |
case GL_NOTEQUAL: |
| 359 |
r = (GLstencil) (ctx->Stencil.Ref[face] & valueMask); |
| 360 |
for (i=0;i<n;i++) { |
| 361 |
if (mask[i]) { |
| 362 |
s = (GLstencil) (stencil[i] & valueMask); |
| 363 |
if (r != s) { |
| 364 |
/* passed */ |
| 365 |
fail[i] = 0; |
| 366 |
} |
| 367 |
else { |
| 368 |
fail[i] = 1; |
| 369 |
mask[i] = 0; |
| 370 |
} |
| 371 |
} |
| 372 |
else { |
| 373 |
fail[i] = 0; |
| 374 |
} |
| 375 |
} |
| 376 |
break; |
| 377 |
case GL_ALWAYS: |
| 378 |
/* always pass */ |
| 379 |
for (i=0;i<n;i++) { |
| 380 |
fail[i] = 0; |
| 381 |
} |
| 382 |
break; |
| 383 |
default: |
| 384 |
_mesa_problem(ctx, "Bad stencil func in gl_stencil_span"); |
| 385 |
return 0; |
| 386 |
} |
| 387 |
|
| 388 |
if (ctx->Stencil.FailFunc[face] != GL_KEEP) { |
| 389 |
apply_stencil_op( ctx, ctx->Stencil.FailFunc[face], face, n, stencil, fail ); |
| 390 |
} |
| 391 |
|
| 392 |
return !allfail; |
| 393 |
} |
| 394 |
|
| 395 |
|
| 396 |
|
| 397 |
/** |
| 398 |
* Apply stencil and depth testing to the span of pixels. |
| 399 |
* Both software and hardware stencil buffers are acceptable. |
| 400 |
* Input: n - number of pixels in the span |
| 401 |
* x, y - location of leftmost pixel in span |
| 402 |
* z - array [n] of z values |
| 403 |
* mask - array [n] of flags (1=test this pixel, 0=skip the pixel) |
| 404 |
* Output: mask - array [n] of flags (1=stencil and depth test passed) |
| 405 |
* Return: GL_FALSE - all fragments failed the testing |
| 406 |
* GL_TRUE - one or more fragments passed the testing |
| 407 |
* |
| 408 |
*/ |
| 409 |
static GLboolean |
| 410 |
stencil_and_ztest_span(GLcontext *ctx, struct sw_span *span, GLuint face) |
| 411 |
{ |
| 412 |
struct gl_framebuffer *fb = ctx->DrawBuffer; |
| 413 |
struct gl_renderbuffer *rb = fb->Attachment[BUFFER_STENCIL].Renderbuffer; |
| 414 |
GLstencil stencilRow[MAX_WIDTH]; |
| 415 |
GLstencil *stencil; |
| 416 |
const GLuint n = span->end; |
| 417 |
const GLint x = span->x; |
| 418 |
const GLint y = span->y; |
| 419 |
GLubyte *mask = span->array->mask; |
| 420 |
|
| 421 |
ASSERT((span->arrayMask & SPAN_XY) == 0); |
| 422 |
ASSERT(ctx->Stencil.Enabled); |
| 423 |
ASSERT(n <= MAX_WIDTH); |
| 424 |
#ifdef DEBUG |
| 425 |
if (ctx->Depth.Test) { |
| 426 |
ASSERT(span->arrayMask & SPAN_Z); |
| 427 |
} |
| 428 |
#endif |
| 429 |
|
| 430 |
stencil = rb->GetPointer(ctx, rb, x, y); |
| 431 |
if (!stencil) { |
| 432 |
rb->GetRow(ctx, rb, n, x, y, stencilRow); |
| 433 |
stencil = stencilRow; |
| 434 |
} |
| 435 |
|
| 436 |
/* |
| 437 |
* Apply the stencil test to the fragments. |
| 438 |
* failMask[i] is 1 if the stencil test failed. |
| 439 |
*/ |
| 440 |
if (do_stencil_test( ctx, face, n, stencil, mask ) == GL_FALSE) { |
| 441 |
/* all fragments failed the stencil test, we're done. */ |
| 442 |
span->writeAll = GL_FALSE; |
| 443 |
if (!rb->GetPointer(ctx, rb, 0, 0)) { |
| 444 |
/* put updated stencil values into buffer */ |
| 445 |
rb->PutRow(ctx, rb, n, x, y, stencil, NULL); |
| 446 |
} |
| 447 |
return GL_FALSE; |
| 448 |
} |
| 449 |
|
| 450 |
/* |
| 451 |
* Some fragments passed the stencil test, apply depth test to them |
| 452 |
* and apply Zpass and Zfail stencil ops. |
| 453 |
*/ |
| 454 |
if (ctx->Depth.Test == GL_FALSE) { |
| 455 |
/* |
| 456 |
* No depth buffer, just apply zpass stencil function to active pixels. |
| 457 |
*/ |
| 458 |
apply_stencil_op( ctx, ctx->Stencil.ZPassFunc[face], face, n, stencil, mask ); |
| 459 |
} |
| 460 |
else { |
| 461 |
/* |
| 462 |
* Perform depth buffering, then apply zpass or zfail stencil function. |
| 463 |
*/ |
| 464 |
GLubyte passmask[MAX_WIDTH], failmask[MAX_WIDTH], oldmask[MAX_WIDTH]; |
| 465 |
GLuint i; |
| 466 |
|
| 467 |
/* save the current mask bits */ |
| 468 |
_mesa_memcpy(oldmask, mask, n * sizeof(GLubyte)); |
| 469 |
|
| 470 |
/* apply the depth test */ |
| 471 |
_swrast_depth_test_span(ctx, span); |
| 472 |
|
| 473 |
/* Set the stencil pass/fail flags according to result of depth testing. |
| 474 |
* if oldmask[i] == 0 then |
| 475 |
* Don't touch the stencil value |
| 476 |
* else if oldmask[i] and newmask[i] then |
| 477 |
* Depth test passed |
| 478 |
* else |
| 479 |
* assert(oldmask[i] && !newmask[i]) |
| 480 |
* Depth test failed |
| 481 |
* endif |
| 482 |
*/ |
| 483 |
for (i=0;i<n;i++) { |
| 484 |
ASSERT(mask[i] == 0 || mask[i] == 1); |
| 485 |
passmask[i] = oldmask[i] & mask[i]; |
| 486 |
failmask[i] = oldmask[i] & (mask[i] ^ 1); |
| 487 |
} |
| 488 |
|
| 489 |
/* apply the pass and fail operations */ |
| 490 |
if (ctx->Stencil.ZFailFunc[face] != GL_KEEP) { |
| 491 |
apply_stencil_op( ctx, ctx->Stencil.ZFailFunc[face], face, |
| 492 |
n, stencil, failmask ); |
| 493 |
} |
| 494 |
if (ctx->Stencil.ZPassFunc[face] != GL_KEEP) { |
| 495 |
apply_stencil_op( ctx, ctx->Stencil.ZPassFunc[face], face, |
| 496 |
n, stencil, passmask ); |
| 497 |
} |
| 498 |
} |
| 499 |
|
| 500 |
/* |
| 501 |
* Write updated stencil values back into hardware stencil buffer. |
| 502 |
*/ |
| 503 |
if (!rb->GetPointer(ctx, rb, 0, 0)) { |
| 504 |
rb->PutRow(ctx, rb, n, x, y, stencil, NULL); |
| 505 |
} |
| 506 |
|
| 507 |
span->writeAll = GL_FALSE; |
| 508 |
|
| 509 |
return GL_TRUE; /* one or more fragments passed both tests */ |
| 510 |
} |
| 511 |
|
| 512 |
|
| 513 |
|
| 514 |
/* |
| 515 |
* Return the address of a stencil buffer value given the window coords: |
| 516 |
*/ |
| 517 |
#define STENCIL_ADDRESS(X, Y) (stencilStart + (Y) * stride + (X)) |
| 518 |
|
| 519 |
|
| 520 |
|
| 521 |
/** |
| 522 |
* Apply the given stencil operator for each pixel in the array whose |
| 523 |
* mask flag is set. |
| 524 |
* \note This is for software stencil buffers only. |
| 525 |
* Input: n - number of pixels in the span |
| 526 |
* x, y - array of [n] pixels |
| 527 |
* operator - the stencil buffer operator |
| 528 |
* mask - array [n] of flag: 1=apply operator, 0=don't apply operator |
| 529 |
*/ |
| 530 |
static void |
| 531 |
apply_stencil_op_to_pixels( GLcontext *ctx, |
| 532 |
GLuint n, const GLint x[], const GLint y[], |
| 533 |
GLenum oper, GLuint face, const GLubyte mask[] ) |
| 534 |
{ |
| 535 |
struct gl_framebuffer *fb = ctx->DrawBuffer; |
| 536 |
struct gl_renderbuffer *rb = fb->Attachment[BUFFER_STENCIL].Renderbuffer; |
| 537 |
const GLstencil ref = ctx->Stencil.Ref[face]; |
| 538 |
const GLstencil wrtmask = ctx->Stencil.WriteMask[face]; |
| 539 |
const GLstencil invmask = (GLstencil) (~wrtmask); |
| 540 |
GLuint i; |
| 541 |
GLstencil *stencilStart = (GLubyte *) rb->Data; |
| 542 |
const GLuint stride = rb->Width; |
| 543 |
|
| 544 |
ASSERT(rb->GetPointer(ctx, rb, 0, 0)); |
| 545 |
ASSERT(sizeof(GLstencil) == 1); |
| 546 |
|
| 547 |
switch (oper) { |
| 548 |
case GL_KEEP: |
| 549 |
/* do nothing */ |
| 550 |
break; |
| 551 |
case GL_ZERO: |
| 552 |
if (invmask==0) { |
| 553 |
for (i=0;i<n;i++) { |
| 554 |
if (mask[i]) { |
| 555 |
GLstencil *sptr = STENCIL_ADDRESS( x[i], y[i] ); |
| 556 |
*sptr = 0; |
| 557 |
} |
| 558 |
} |
| 559 |
} |
| 560 |
else { |
| 561 |
for (i=0;i<n;i++) { |
| 562 |
if (mask[i]) { |
| 563 |
GLstencil *sptr = STENCIL_ADDRESS( x[i], y[i] ); |
| 564 |
*sptr = (GLstencil) (invmask & *sptr); |
| 565 |
} |
| 566 |
} |
| 567 |
} |
| 568 |
break; |
| 569 |
case GL_REPLACE: |
| 570 |
if (invmask==0) { |
| 571 |
for (i=0;i<n;i++) { |
| 572 |
if (mask[i]) { |
| 573 |
GLstencil *sptr = STENCIL_ADDRESS( x[i], y[i] ); |
| 574 |
*sptr = ref; |
| 575 |
} |
| 576 |
} |
| 577 |
} |
| 578 |
else { |
| 579 |
for (i=0;i<n;i++) { |
| 580 |
if (mask[i]) { |
| 581 |
GLstencil *sptr = STENCIL_ADDRESS( x[i], y[i] ); |
| 582 |
*sptr = (GLstencil) ((invmask & *sptr ) | (wrtmask & ref)); |
| 583 |
} |
| 584 |
} |
| 585 |
} |
| 586 |
break; |
| 587 |
case GL_INCR: |
| 588 |
if (invmask==0) { |
| 589 |
for (i=0;i<n;i++) { |
| 590 |
if (mask[i]) { |
| 591 |
GLstencil *sptr = STENCIL_ADDRESS( x[i], y[i] ); |
| 592 |
if (*sptr < STENCIL_MAX) { |
| 593 |
*sptr = (GLstencil) (*sptr + 1); |
| 594 |
} |
| 595 |
} |
| 596 |
} |
| 597 |
} |
| 598 |
else { |
| 599 |
for (i=0;i<n;i++) { |
| 600 |
if (mask[i]) { |
| 601 |
GLstencil *sptr = STENCIL_ADDRESS( x[i], y[i] ); |
| 602 |
if (*sptr < STENCIL_MAX) { |
| 603 |
*sptr = (GLstencil) ((invmask & *sptr) | (wrtmask & (*sptr+1))); |
| 604 |
} |
| 605 |
} |
| 606 |
} |
| 607 |
} |
| 608 |
break; |
| 609 |
case GL_DECR: |
| 610 |
if (invmask==0) { |
| 611 |
for (i=0;i<n;i++) { |
| 612 |
if (mask[i]) { |
| 613 |
GLstencil *sptr = STENCIL_ADDRESS( x[i], y[i] ); |
| 614 |
if (*sptr>0) { |
| 615 |
*sptr = (GLstencil) (*sptr - 1); |
| 616 |
} |
| 617 |
} |
| 618 |
} |
| 619 |
} |
| 620 |
else { |
| 621 |
for (i=0;i<n;i++) { |
| 622 |
if (mask[i]) { |
| 623 |
GLstencil *sptr = STENCIL_ADDRESS( x[i], y[i] ); |
| 624 |
if (*sptr>0) { |
| 625 |
*sptr = (GLstencil) ((invmask & *sptr) | (wrtmask & (*sptr-1))); |
| 626 |
} |
| 627 |
} |
| 628 |
} |
| 629 |
} |
| 630 |
break; |
| 631 |
case GL_INCR_WRAP_EXT: |
| 632 |
if (invmask==0) { |
| 633 |
for (i=0;i<n;i++) { |
| 634 |
if (mask[i]) { |
| 635 |
GLstencil *sptr = STENCIL_ADDRESS( x[i], y[i] ); |
| 636 |
*sptr = (GLstencil) (*sptr + 1); |
| 637 |
} |
| 638 |
} |
| 639 |
} |
| 640 |
else { |
| 641 |
for (i=0;i<n;i++) { |
| 642 |
if (mask[i]) { |
| 643 |
GLstencil *sptr = STENCIL_ADDRESS( x[i], y[i] ); |
| 644 |
*sptr = (GLstencil) ((invmask & *sptr) | (wrtmask & (*sptr+1))); |
| 645 |
} |
| 646 |
} |
| 647 |
} |
| 648 |
break; |
| 649 |
case GL_DECR_WRAP_EXT: |
| 650 |
if (invmask==0) { |
| 651 |
for (i=0;i<n;i++) { |
| 652 |
if (mask[i]) { |
| 653 |
GLstencil *sptr = STENCIL_ADDRESS( x[i], y[i] ); |
| 654 |
*sptr = (GLstencil) (*sptr - 1); |
| 655 |
} |
| 656 |
} |
| 657 |
} |
| 658 |
else { |
| 659 |
for (i=0;i<n;i++) { |
| 660 |
if (mask[i]) { |
| 661 |
GLstencil *sptr = STENCIL_ADDRESS( x[i], y[i] ); |
| 662 |
*sptr = (GLstencil) ((invmask & *sptr) | (wrtmask & (*sptr-1))); |
| 663 |
} |
| 664 |
} |
| 665 |
} |
| 666 |
break; |
| 667 |
case GL_INVERT: |
| 668 |
if (invmask==0) { |
| 669 |
for (i=0;i<n;i++) { |
| 670 |
if (mask[i]) { |
| 671 |
GLstencil *sptr = STENCIL_ADDRESS( x[i], y[i] ); |
| 672 |
*sptr = (GLstencil) (~*sptr); |
| 673 |
} |
| 674 |
} |
| 675 |
} |
| 676 |
else { |
| 677 |
for (i=0;i<n;i++) { |
| 678 |
if (mask[i]) { |
| 679 |
GLstencil *sptr = STENCIL_ADDRESS( x[i], y[i] ); |
| 680 |
*sptr = (GLstencil) ((invmask & *sptr) | (wrtmask & ~*sptr)); |
| 681 |
} |
| 682 |
} |
| 683 |
} |
| 684 |
break; |
| 685 |
default: |
| 686 |
_mesa_problem(ctx, "Bad stencilop in apply_stencil_op_to_pixels"); |
| 687 |
} |
| 688 |
} |
| 689 |
|
| 690 |
|
| 691 |
|
| 692 |
/** |
| 693 |
* Apply stencil test to an array of pixels before depth buffering. |
| 694 |
* |
| 695 |
* \note Used for software stencil buffer only. |
| 696 |
* Input: n - number of pixels in the span |
| 697 |
* x, y - array of [n] pixels to stencil |
| 698 |
* mask - array [n] of flag: 0=skip the pixel, 1=stencil the pixel |
| 699 |
* Output: mask - pixels which fail the stencil test will have their |
| 700 |
* mask flag set to 0. |
| 701 |
* \return GL_FALSE = all pixels failed, GL_TRUE = zero or more pixels passed. |
| 702 |
*/ |
| 703 |
static GLboolean |
| 704 |
stencil_test_pixels( GLcontext *ctx, GLuint face, GLuint n, |
| 705 |
const GLint x[], const GLint y[], GLubyte mask[] ) |
| 706 |
{ |
| 707 |
const struct gl_framebuffer *fb = ctx->DrawBuffer; |
| 708 |
struct gl_renderbuffer *rb = fb->Attachment[BUFFER_STENCIL].Renderbuffer; |
| 709 |
GLubyte fail[MAX_WIDTH]; |
| 710 |
GLstencil r, s; |
| 711 |
GLuint i; |
| 712 |
GLboolean allfail = GL_FALSE; |
| 713 |
const GLuint valueMask = ctx->Stencil.ValueMask[face]; |
| 714 |
const GLstencil *stencilStart = (GLstencil *) rb->Data; |
| 715 |
const GLuint stride = rb->Width; |
| 716 |
|
| 717 |
ASSERT(rb->GetPointer(ctx, rb, 0, 0)); |
| 718 |
ASSERT(sizeof(GLstencil) == 1); |
| 719 |
|
| 720 |
/* |
| 721 |
* Perform stencil test. The results of this operation are stored |
| 722 |
* in the fail[] array: |
| 723 |
* IF fail[i] is non-zero THEN |
| 724 |
* the stencil fail operator is to be applied |
| 725 |
* ELSE |
| 726 |
* the stencil fail operator is not to be applied |
| 727 |
* ENDIF |
| 728 |
*/ |
| 729 |
|
| 730 |
switch (ctx->Stencil.Function[face]) { |
| 731 |
case GL_NEVER: |
| 732 |
/* always fail */ |
| 733 |
for (i=0;i<n;i++) { |
| 734 |
if (mask[i]) { |
| 735 |
mask[i] = 0; |
| 736 |
fail[i] = 1; |
| 737 |
} |
| 738 |
else { |
| 739 |
fail[i] = 0; |
| 740 |
} |
| 741 |
} |
| 742 |
allfail = GL_TRUE; |
| 743 |
break; |
| 744 |
case GL_LESS: |
| 745 |
r = (GLstencil) (ctx->Stencil.Ref[face] & valueMask); |
| 746 |
for (i=0;i<n;i++) { |
| 747 |
if (mask[i]) { |
| 748 |
const GLstencil *sptr = STENCIL_ADDRESS(x[i],y[i]); |
| 749 |
s = (GLstencil) (*sptr & valueMask); |
| 750 |
if (r < s) { |
| 751 |
/* passed */ |
| 752 |
fail[i] = 0; |
| 753 |
} |
| 754 |
else { |
| 755 |
fail[i] = 1; |
| 756 |
mask[i] = 0; |
| 757 |
} |
| 758 |
} |
| 759 |
else { |
| 760 |
fail[i] = 0; |
| 761 |
} |
| 762 |
} |
| 763 |
break; |
| 764 |
case GL_LEQUAL: |
| 765 |
r = (GLstencil) (ctx->Stencil.Ref[face] & valueMask); |
| 766 |
for (i=0;i<n;i++) { |
| 767 |
if (mask[i]) { |
| 768 |
const GLstencil *sptr = STENCIL_ADDRESS(x[i],y[i]); |
| 769 |
s = (GLstencil) (*sptr & valueMask); |
| 770 |
if (r <= s) { |
| 771 |
/* pass */ |
| 772 |
fail[i] = 0; |
| 773 |
} |
| 774 |
else { |
| 775 |
fail[i] = 1; |
| 776 |
mask[i] = 0; |
| 777 |
} |
| 778 |
} |
| 779 |
else { |
| 780 |
fail[i] = 0; |
| 781 |
} |
| 782 |
} |
| 783 |
break; |
| 784 |
case GL_GREATER: |
| 785 |
r = (GLstencil) (ctx->Stencil.Ref[face] & valueMask); |
| 786 |
for (i=0;i<n;i++) { |
| 787 |
if (mask[i]) { |
| 788 |
const GLstencil *sptr = STENCIL_ADDRESS(x[i],y[i]); |
| 789 |
s = (GLstencil) (*sptr & valueMask); |
| 790 |
if (r > s) { |
| 791 |
/* passed */ |
| 792 |
fail[i] = 0; |
| 793 |
} |
| 794 |
else { |
| 795 |
fail[i] = 1; |
| 796 |
mask[i] = 0; |
| 797 |
} |
| 798 |
} |
| 799 |
else { |
| 800 |
fail[i] = 0; |
| 801 |
} |
| 802 |
} |
| 803 |
break; |
| 804 |
case GL_GEQUAL: |
| 805 |
r = (GLstencil) (ctx->Stencil.Ref[face] & valueMask); |
| 806 |
for (i=0;i<n;i++) { |
| 807 |
if (mask[i]) { |
| 808 |
const GLstencil *sptr = STENCIL_ADDRESS(x[i],y[i]); |
| 809 |
s = (GLstencil) (*sptr & valueMask); |
| 810 |
if (r >= s) { |
| 811 |
/* passed */ |
| 812 |
fail[i] = 0; |
| 813 |
} |
| 814 |
else { |
| 815 |
fail[i] = 1; |
| 816 |
mask[i] = 0; |
| 817 |
} |
| 818 |
} |
| 819 |
else { |
| 820 |
fail[i] = 0; |
| 821 |
} |
| 822 |
} |
| 823 |
break; |
| 824 |
case GL_EQUAL: |
| 825 |
r = (GLstencil) (ctx->Stencil.Ref[face] & valueMask); |
| 826 |
for (i=0;i<n;i++) { |
| 827 |
if (mask[i]) { |
| 828 |
const GLstencil *sptr = STENCIL_ADDRESS(x[i],y[i]); |
| 829 |
s = (GLstencil) (*sptr & valueMask); |
| 830 |
if (r == s) { |
| 831 |
/* passed */ |
| 832 |
fail[i] = 0; |
| 833 |
} |
| 834 |
else { |
| 835 |
fail[i] = 1; |
| 836 |
mask[i] = 0; |
| 837 |
} |
| 838 |
} |
| 839 |
else { |
| 840 |
fail[i] = 0; |
| 841 |
} |
| 842 |
} |
| 843 |
break; |
| 844 |
case GL_NOTEQUAL: |
| 845 |
r = (GLstencil) (ctx->Stencil.Ref[face] & valueMask); |
| 846 |
for (i=0;i<n;i++) { |
| 847 |
if (mask[i]) { |
| 848 |
const GLstencil *sptr = STENCIL_ADDRESS(x[i],y[i]); |
| 849 |
s = (GLstencil) (*sptr & valueMask); |
| 850 |
if (r != s) { |
| 851 |
/* passed */ |
| 852 |
fail[i] = 0; |
| 853 |
} |
| 854 |
else { |
| 855 |
fail[i] = 1; |
| 856 |
mask[i] = 0; |
| 857 |
} |
| 858 |
} |
| 859 |
else { |
| 860 |
fail[i] = 0; |
| 861 |
} |
| 862 |
} |
| 863 |
break; |
| 864 |
case GL_ALWAYS: |
| 865 |
/* always pass */ |
| 866 |
for (i=0;i<n;i++) { |
| 867 |
fail[i] = 0; |
| 868 |
} |
| 869 |
break; |
| 870 |
default: |
| 871 |
_mesa_problem(ctx, "Bad stencil func in gl_stencil_pixels"); |
| 872 |
return 0; |
| 873 |
} |
| 874 |
|
| 875 |
if (ctx->Stencil.FailFunc[face] != GL_KEEP) { |
| 876 |
apply_stencil_op_to_pixels( ctx, n, x, y, ctx->Stencil.FailFunc[face], |
| 877 |
face, fail ); |
| 878 |
} |
| 879 |
|
| 880 |
return !allfail; |
| 881 |
} |
| 882 |
|
| 883 |
|
| 884 |
|
| 885 |
|
| 886 |
/** |
| 887 |
* Apply stencil and depth testing to an array of pixels. |
| 888 |
* This is used both for software and hardware stencil buffers. |
| 889 |
* |
| 890 |
* The comments in this function are a bit sparse but the code is |
| 891 |
* almost identical to stencil_and_ztest_span(), which is well |
| 892 |
* commented. |
| 893 |
* |
| 894 |
* Input: n - number of pixels in the array |
| 895 |
* x, y - array of [n] pixel positions |
| 896 |
* z - array [n] of z values |
| 897 |
* mask - array [n] of flags (1=test this pixel, 0=skip the pixel) |
| 898 |
* Output: mask - array [n] of flags (1=stencil and depth test passed) |
| 899 |
* Return: GL_FALSE - all fragments failed the testing |
| 900 |
* GL_TRUE - one or more fragments passed the testing |
| 901 |
*/ |
| 902 |
static GLboolean |
| 903 |
stencil_and_ztest_pixels( GLcontext *ctx, struct sw_span *span, GLuint face ) |
| 904 |
{ |
| 905 |
struct gl_framebuffer *fb = ctx->DrawBuffer; |
| 906 |
struct gl_renderbuffer *rb = fb->Attachment[BUFFER_STENCIL].Renderbuffer; |
| 907 |
const GLuint n = span->end; |
| 908 |
const GLint *x = span->array->x; |
| 909 |
const GLint *y = span->array->y; |
| 910 |
GLubyte *mask = span->array->mask; |
| 911 |
|
| 912 |
ASSERT(span->arrayMask & SPAN_XY); |
| 913 |
ASSERT(ctx->Stencil.Enabled); |
| 914 |
ASSERT(n <= MAX_WIDTH); |
| 915 |
|
| 916 |
if (!rb->GetPointer(ctx, rb, 0, 0)) { |
| 917 |
/* No direct access */ |
| 918 |
GLstencil stencil[MAX_WIDTH]; |
| 919 |
GLubyte origMask[MAX_WIDTH]; |
| 920 |
|
| 921 |
ASSERT(rb->DataType == GL_UNSIGNED_BYTE); |
| 922 |
_swrast_get_values(ctx, rb, n, x, y, stencil, sizeof(GLubyte)); |
| 923 |
|
| 924 |
_mesa_memcpy(origMask, mask, n * sizeof(GLubyte)); |
| 925 |
|
| 926 |
(void) do_stencil_test(ctx, face, n, stencil, mask); |
| 927 |
|
| 928 |
if (ctx->Depth.Test == GL_FALSE) { |
| 929 |
apply_stencil_op(ctx, ctx->Stencil.ZPassFunc[face], face, |
| 930 |
n, stencil, mask); |
| 931 |
} |
| 932 |
else { |
| 933 |
_swrast_depth_test_span(ctx, span); |
| 934 |
|
| 935 |
if (ctx->Stencil.ZFailFunc[face] != GL_KEEP) { |
| 936 |
GLubyte failmask[MAX_WIDTH]; |
| 937 |
GLuint i; |
| 938 |
for (i = 0; i < n; i++) { |
| 939 |
ASSERT(mask[i] == 0 || mask[i] == 1); |
| 940 |
failmask[i] = origMask[i] & (mask[i] ^ 1); |
| 941 |
} |
| 942 |
apply_stencil_op(ctx, ctx->Stencil.ZFailFunc[face], face, |
| 943 |
n, stencil, failmask); |
| 944 |
} |
| 945 |
if (ctx->Stencil.ZPassFunc[face] != GL_KEEP) { |
| 946 |
GLubyte passmask[MAX_WIDTH]; |
| 947 |
GLuint i; |
| 948 |
for (i = 0; i < n; i++) { |
| 949 |
ASSERT(mask[i] == 0 || mask[i] == 1); |
| 950 |
passmask[i] = origMask[i] & mask[i]; |
| 951 |
} |
| 952 |
apply_stencil_op(ctx, ctx->Stencil.ZPassFunc[face], face, |
| 953 |
n, stencil, passmask); |
| 954 |
} |
| 955 |
} |
| 956 |
|
| 957 |
/* Write updated stencil values into hardware stencil buffer */ |
| 958 |
rb->PutValues(ctx, rb, n, x, y, stencil, origMask); |
| 959 |
|
| 960 |
return GL_TRUE; |
| 961 |
} |
| 962 |
else { |
| 963 |
/* Direct access to stencil buffer */ |
| 964 |
|
| 965 |
if (stencil_test_pixels(ctx, face, n, x, y, mask) == GL_FALSE) { |
| 966 |
/* all fragments failed the stencil test, we're done. */ |
| 967 |
return GL_FALSE; |
| 968 |
} |
| 969 |
|
| 970 |
if (ctx->Depth.Test==GL_FALSE) { |
| 971 |
apply_stencil_op_to_pixels(ctx, n, x, y, |
| 972 |
ctx->Stencil.ZPassFunc[face], face, mask); |
| 973 |
} |
| 974 |
else { |
| 975 |
GLubyte passmask[MAX_WIDTH], failmask[MAX_WIDTH], oldmask[MAX_WIDTH]; |
| 976 |
GLuint i; |
| 977 |
|
| 978 |
_mesa_memcpy(oldmask, mask, n * sizeof(GLubyte)); |
| 979 |
|
| 980 |
_swrast_depth_test_span(ctx, span); |
| 981 |
|
| 982 |
for (i=0;i<n;i++) { |
| 983 |
ASSERT(mask[i] == 0 || mask[i] == 1); |
| 984 |
passmask[i] = oldmask[i] & mask[i]; |
| 985 |
failmask[i] = oldmask[i] & (mask[i] ^ 1); |
| 986 |
} |
| 987 |
|
| 988 |
if (ctx->Stencil.ZFailFunc[face] != GL_KEEP) { |
| 989 |
apply_stencil_op_to_pixels(ctx, n, x, y, |
| 990 |
ctx->Stencil.ZFailFunc[face], |
| 991 |
face, failmask); |
| 992 |
} |
| 993 |
if (ctx->Stencil.ZPassFunc[face] != GL_KEEP) { |
| 994 |
apply_stencil_op_to_pixels(ctx, n, x, y, |
| 995 |
ctx->Stencil.ZPassFunc[face], |
| 996 |
face, passmask); |
| 997 |
} |
| 998 |
} |
| 999 |
|
| 1000 |
return GL_TRUE; /* one or more fragments passed both tests */ |
| 1001 |
} |
| 1002 |
} |
| 1003 |
|
| 1004 |
|
| 1005 |
/** |
| 1006 |
* /return GL_TRUE = one or more fragments passed, |
| 1007 |
* GL_FALSE = all fragments failed. |
| 1008 |
*/ |
| 1009 |
GLboolean |
| 1010 |
_swrast_stencil_and_ztest_span(GLcontext *ctx, struct sw_span *span) |
| 1011 |
{ |
| 1012 |
/* span->facing can only be non-zero if using two-sided stencil */ |
| 1013 |
ASSERT(ctx->Stencil.TestTwoSide || span->facing == 0); |
| 1014 |
if (span->arrayMask & SPAN_XY) |
| 1015 |
return stencil_and_ztest_pixels(ctx, span, span->facing); |
| 1016 |
else |
| 1017 |
return stencil_and_ztest_span(ctx, span, span->facing); |
| 1018 |
} |
| 1019 |
|
| 1020 |
|
| 1021 |
#if 0 |
| 1022 |
GLuint |
| 1023 |
clip_span(GLuint bufferWidth, GLuint bufferHeight, |
| 1024 |
GLint x, GLint y, GLuint *count) |
| 1025 |
{ |
| 1026 |
GLuint n = *count; |
| 1027 |
GLuint skipPixels = 0; |
| 1028 |
|
| 1029 |
if (y < 0 || y >= bufferHeight || x + n <= 0 || x >= bufferWidth) { |
| 1030 |
/* totally out of bounds */ |
| 1031 |
n = 0; |
| 1032 |
} |
| 1033 |
else { |
| 1034 |
/* left clip */ |
| 1035 |
if (x < 0) { |
| 1036 |
skipPixels = -x; |
| 1037 |
x = 0; |
| 1038 |
n -= skipPixels; |
| 1039 |
} |
| 1040 |
/* right clip */ |
| 1041 |
if (x + n > bufferWidth) { |
| 1042 |
GLint dx = x + n - bufferWidth; |
| 1043 |
n -= dx; |
| 1044 |
} |
| 1045 |
} |
| 1046 |
|
| 1047 |
*count = n; |
| 1048 |
|
| 1049 |
return skipPixels; |
| 1050 |
} |
| 1051 |
#endif |
| 1052 |
|
| 1053 |
|
| 1054 |
/** |
| 1055 |
* Return a span of stencil values from the stencil buffer. |
| 1056 |
* Used for glRead/CopyPixels |
| 1057 |
* Input: n - how many pixels |
| 1058 |
* x,y - location of first pixel |
| 1059 |
* Output: stencil - the array of stencil values |
| 1060 |
*/ |
| 1061 |
void |
| 1062 |
_swrast_read_stencil_span(GLcontext *ctx, struct gl_renderbuffer *rb, |
| 1063 |
GLint n, GLint x, GLint y, GLstencil stencil[]) |
| 1064 |
{ |
| 1065 |
if (y < 0 || y >= rb->Height || x + n <= 0 || x >= rb->Width) { |
| 1066 |
/* span is completely outside framebuffer */ |
| 1067 |
return; /* undefined values OK */ |
| 1068 |
} |
| 1069 |
|
| 1070 |
if (x < 0) { |
| 1071 |
GLint dx = -x; |
| 1072 |
x = 0; |
| 1073 |
n -= dx; |
| 1074 |
stencil += dx; |
| 1075 |
} |
| 1076 |
if (x + n > rb->Width) { |
| 1077 |
GLint dx = x + n - rb->Width; |
| 1078 |
n -= dx; |
| 1079 |
} |
| 1080 |
if (n <= 0) { |
| 1081 |
return; |
| 1082 |
} |
| 1083 |
|
| 1084 |
rb->GetRow(ctx, rb, n, x, y, stencil); |
| 1085 |
} |
| 1086 |
|
| 1087 |
|
| 1088 |
|
| 1089 |
/** |
| 1090 |
* Write a span of stencil values to the stencil buffer. |
| 1091 |
* Used for glDraw/CopyPixels |
| 1092 |
* Input: n - how many pixels |
| 1093 |
* x, y - location of first pixel |
| 1094 |
* stencil - the array of stencil values |
| 1095 |
*/ |
| 1096 |
void |
| 1097 |
_swrast_write_stencil_span(GLcontext *ctx, GLint n, GLint x, GLint y, |
| 1098 |
const GLstencil stencil[] ) |
| 1099 |
{ |
| 1100 |
struct gl_framebuffer *fb = ctx->DrawBuffer; |
| 1101 |
struct gl_renderbuffer *rb = fb->Attachment[BUFFER_STENCIL].Renderbuffer; |
| 1102 |
const GLuint stencilMax = (1 << fb->Visual.stencilBits) - 1; |
| 1103 |
const GLuint stencilMask = ctx->Stencil.WriteMask[0]; |
| 1104 |
|
| 1105 |
if (y < 0 || y >= rb->Height || x + n <= 0 || x >= rb->Width) { |
| 1106 |
/* span is completely outside framebuffer */ |
| 1107 |
return; /* undefined values OK */ |
| 1108 |
} |
| 1109 |
if (x < 0) { |
| 1110 |
GLint dx = -x; |
| 1111 |
x = 0; |
| 1112 |
n -= dx; |
| 1113 |
stencil += dx; |
| 1114 |
} |
| 1115 |
if (x + n > rb->Width) { |
| 1116 |
GLint dx = x + n - rb->Width; |
| 1117 |
n -= dx; |
| 1118 |
} |
| 1119 |
if (n <= 0) { |
| 1120 |
return; |
| 1121 |
} |
| 1122 |
|
| 1123 |
if ((stencilMask & stencilMax) != stencilMax) { |
| 1124 |
/* need to apply writemask */ |
| 1125 |
GLstencil destVals[MAX_WIDTH], newVals[MAX_WIDTH]; |
| 1126 |
GLint i; |
| 1127 |
rb->GetRow(ctx, rb, n, x, y, destVals); |
| 1128 |
for (i = 0; i < n; i++) { |
| 1129 |
newVals[i] |
| 1130 |
= (stencil[i] & stencilMask) | (destVals[i] & ~stencilMask); |
| 1131 |
} |
| 1132 |
rb->PutRow(ctx, rb, n, x, y, newVals, NULL); |
| 1133 |
} |
| 1134 |
else { |
| 1135 |
rb->PutRow(ctx, rb, n, x, y, stencil, NULL); |
| 1136 |
} |
| 1137 |
} |
| 1138 |
|
| 1139 |
|
| 1140 |
|
| 1141 |
/** |
| 1142 |
* Clear the stencil buffer. |
| 1143 |
*/ |
| 1144 |
void |
| 1145 |
_swrast_clear_stencil_buffer( GLcontext *ctx, struct gl_renderbuffer *rb ) |
| 1146 |
{ |
| 1147 |
const GLstencil mask = ctx->Stencil.WriteMask[0]; |
| 1148 |
const GLstencil invMask = ~mask; |
| 1149 |
const GLstencil clearVal = (ctx->Stencil.Clear & mask); |
| 1150 |
GLint x, y, width, height; |
| 1151 |
|
| 1152 |
if (!rb || mask == 0) |
| 1153 |
return; |
| 1154 |
|
| 1155 |
ASSERT(rb->DataType == GL_UNSIGNED_BYTE); |
| 1156 |
ASSERT(rb->_BaseFormat == GL_STENCIL_INDEX); |
| 1157 |
|
| 1158 |
/* compute region to clear */ |
| 1159 |
x = ctx->DrawBuffer->_Xmin; |
| 1160 |
y = ctx->DrawBuffer->_Ymin; |
| 1161 |
width = ctx->DrawBuffer->_Xmax - ctx->DrawBuffer->_Xmin; |
| 1162 |
height = ctx->DrawBuffer->_Ymax - ctx->DrawBuffer->_Ymin; |
| 1163 |
|
| 1164 |
if (rb->GetPointer(ctx, rb, 0, 0)) { |
| 1165 |
/* Direct buffer access */ |
| 1166 |
if (ctx->Stencil.WriteMask[0] != STENCIL_MAX) { |
| 1167 |
/* need to mask the clear */ |
| 1168 |
GLint i, j; |
| 1169 |
for (i = 0; i < height; i++) { |
| 1170 |
GLubyte *stencil = rb->GetPointer(ctx, rb, x, y + i); |
| 1171 |
for (j = 0; j < width; j++) { |
| 1172 |
stencil[j] = (stencil[j] & invMask) | clearVal; |
| 1173 |
} |
| 1174 |
} |
| 1175 |
} |
| 1176 |
else { |
| 1177 |
/* no bit masking */ |
| 1178 |
if (width == rb->Width && |
| 1179 |
rb->InternalFormat == GL_STENCIL_INDEX8_EXT) { |
| 1180 |
/* optimized case */ |
| 1181 |
GLubyte *stencil = rb->GetPointer(ctx, rb, x, y); |
| 1182 |
GLuint len = width * height * sizeof(GLubyte); |
| 1183 |
_mesa_memset(stencil, clearVal, len); |
| 1184 |
} |
| 1185 |
else { |
| 1186 |
/* general case */ |
| 1187 |
GLint i, j; |
| 1188 |
for (i = 0; i < height; i++) { |
| 1189 |
GLubyte *stencil = rb->GetPointer(ctx, rb, x, y + i); |
| 1190 |
for (j = 0; j < width; j++) { |
| 1191 |
stencil[j] = clearVal; |
| 1192 |
} |
| 1193 |
} |
| 1194 |
} |
| 1195 |
} |
| 1196 |
} |
| 1197 |
else { |
| 1198 |
/* no direct access */ |
| 1199 |
if (ctx->Stencil.WriteMask[0] != STENCIL_MAX) { |
| 1200 |
/* need to mask the clear */ |
| 1201 |
GLint i, j; |
| 1202 |
for (i = 0; i < height; i++) { |
| 1203 |
GLubyte stencil[MAX_WIDTH]; |
| 1204 |
rb->GetRow(ctx, rb, width, x, y + i, stencil); |
| 1205 |
for (j = 0; j < width; j++) { |
| 1206 |
stencil[j] = (stencil[j] & invMask) | clearVal; |
| 1207 |
} |
| 1208 |
rb->PutRow(ctx, rb, width, x, y + i, stencil, NULL); |
| 1209 |
} |
| 1210 |
} |
| 1211 |
else { |
| 1212 |
/* no bit masking */ |
| 1213 |
const GLubyte clear8 = clearVal; |
| 1214 |
GLint i; |
| 1215 |
for (i = 0; i < height; i++) { |
| 1216 |
rb->PutMonoRow(ctx, rb, width, x, y + i, &clear8, NULL); |
| 1217 |
} |
| 1218 |
} |
| 1219 |
} |
| 1220 |
} |
| 1221 |
|