stencil.c 36.5 KB
Newer Older
1
/* $Id: stencil.c,v 1.16.4.5 2002/01/08 14:55:48 brianp Exp $ */
jtg's avatar
jtg committed
2
3
4

/*
 * Mesa 3-D graphics library
5
 * Version:  3.4.2
jtg's avatar
jtg committed
6
 * 
7
 * Copyright (C) 1999-2001  Brian Paul   All Rights Reserved.
jtg's avatar
jtg committed
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included
 * in all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */


#ifdef PC_HEADER
#include "all.h"
#else
31
#include "glheader.h"
jtg's avatar
jtg committed
32
#include "context.h"
33
#include "depth.h"
34
#include "mem.h"
jtg's avatar
jtg committed
35
36
37
#include "pb.h"
#include "stencil.h"
#include "types.h"
Ted Jump's avatar
Ted Jump committed
38
#include "enable.h"
jtg's avatar
jtg committed
39
40
41
42
43
#endif




44
45
void
_mesa_ClearStencil( GLint s )
jtg's avatar
jtg committed
46
{
47
   GET_CURRENT_CONTEXT(ctx);
jtg's avatar
jtg committed
48
49
50
51
52
53
54
55
56
57
   ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx, "glClearStencil");
   ctx->Stencil.Clear = (GLstencil) s;

   if (ctx->Driver.ClearStencil) {
      (*ctx->Driver.ClearStencil)( ctx, s );
   }
}



58
59
void
_mesa_StencilFunc( GLenum func, GLint ref, GLuint mask )
jtg's avatar
jtg committed
60
{
61
   GET_CURRENT_CONTEXT(ctx);
jtg's avatar
jtg committed
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
   GLint maxref;

   ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx, "glStencilFunc");

   switch (func) {
      case GL_NEVER:
      case GL_LESS:
      case GL_LEQUAL:
      case GL_GREATER:
      case GL_GEQUAL:
      case GL_EQUAL:
      case GL_NOTEQUAL:
      case GL_ALWAYS:
         ctx->Stencil.Function = func;
         break;
      default:
         gl_error( ctx, GL_INVALID_ENUM, "glStencilFunc" );
         return;
   }

   maxref = (1 << STENCIL_BITS) - 1;
Brian Paul's avatar
Brian Paul committed
83
84
   ctx->Stencil.Ref = (GLstencil) CLAMP( ref, 0, maxref );
   ctx->Stencil.ValueMask = (GLstencil) mask;
jtg's avatar
jtg committed
85
86
87
88
89
90
91
92

   if (ctx->Driver.StencilFunc) {
      (*ctx->Driver.StencilFunc)( ctx, func, ctx->Stencil.Ref, mask );
   }
}



93
94
void
_mesa_StencilMask( GLuint mask )
jtg's avatar
jtg committed
95
{
96
   GET_CURRENT_CONTEXT(ctx);
jtg's avatar
jtg committed
97
98
99
100
101
102
103
104
105
106
   ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx, "glStencilMask");
   ctx->Stencil.WriteMask = (GLstencil) mask;

   if (ctx->Driver.StencilMask) {
      (*ctx->Driver.StencilMask)( ctx, mask );
   }
}



107
108
void
_mesa_StencilOp( GLenum fail, GLenum zfail, GLenum zpass )
jtg's avatar
jtg committed
109
{
110
   GET_CURRENT_CONTEXT(ctx);
jtg's avatar
jtg committed
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
   ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx, "glStencilOp");
   switch (fail) {
      case GL_KEEP:
      case GL_ZERO:
      case GL_REPLACE:
      case GL_INCR:
      case GL_DECR:
      case GL_INVERT:
      case GL_INCR_WRAP_EXT:
      case GL_DECR_WRAP_EXT:
         ctx->Stencil.FailFunc = fail;
         break;
      default:
         gl_error( ctx, GL_INVALID_ENUM, "glStencilOp" );
         return;
   }
   switch (zfail) {
      case GL_KEEP:
      case GL_ZERO:
      case GL_REPLACE:
      case GL_INCR:
      case GL_DECR:
      case GL_INVERT:
      case GL_INCR_WRAP_EXT:
      case GL_DECR_WRAP_EXT:
         ctx->Stencil.ZFailFunc = zfail;
         break;
      default:
         gl_error( ctx, GL_INVALID_ENUM, "glStencilOp" );
         return;
   }
   switch (zpass) {
      case GL_KEEP:
      case GL_ZERO:
      case GL_REPLACE:
      case GL_INCR:
      case GL_DECR:
      case GL_INVERT:
      case GL_INCR_WRAP_EXT:
      case GL_DECR_WRAP_EXT:
         ctx->Stencil.ZPassFunc = zpass;
         break;
      default:
         gl_error( ctx, GL_INVALID_ENUM, "glStencilOp" );
         return;
   }

   if (ctx->Driver.StencilOp) {
      (*ctx->Driver.StencilOp)( ctx, fail, zfail, zpass );
   }
}



/* Stencil Logic:

IF stencil test fails THEN
168
   Apply fail-op to stencil value   
jtg's avatar
jtg committed
169
170
   Don't write the pixel (RGBA,Z)
ELSE
171
172
173
174
175
   IF doing depth test && depth test fails THEN
      Apply zfail-op to stencil value   
      Write RGBA and Z to appropriate buffers
   ELSE
      Apply zpass-op to stencil value
jtg's avatar
jtg committed
176
177
178
179
180
181
ENDIF

*/



182

183
184
185
186
187
188
189
/*
 * Return the address of a stencil buffer value given the window coords:
 */
#define STENCIL_ADDRESS(X,Y)  \
       (ctx->DrawBuffer->Stencil + ctx->DrawBuffer->Width * (Y) + (X))


jtg's avatar
jtg committed
190
191

/*
192
193
 * Apply the given stencil operator to the array of stencil values.
 * Don't touch stencil[i] if mask[i] is zero.
194
 * Input:  n - size of stencil array
jtg's avatar
jtg committed
195
 *         oper - the stencil buffer operator
196
 *         stencil - array of stencil values
jtg's avatar
jtg committed
197
 *         mask - array [n] of flag:  1=apply operator, 0=don't apply operator
198
 * Output:  stencil - modified values
jtg's avatar
jtg committed
199
 */
200
201
202
static void apply_stencil_op( const GLcontext *ctx, GLenum oper,
                              GLuint n, GLstencil stencil[],
                              const GLubyte mask[] )
jtg's avatar
jtg committed
203
204
205
{
   const GLstencil ref = ctx->Stencil.Ref;
   const GLstencil wrtmask = ctx->Stencil.WriteMask;
Brian Paul's avatar
Brian Paul committed
206
   const GLstencil invmask = (GLstencil) (~ctx->Stencil.WriteMask);
jtg's avatar
jtg committed
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
   GLuint i;

   switch (oper) {
      case GL_KEEP:
         /* do nothing */
         break;
      case GL_ZERO:
	 if (invmask==0) {
	    for (i=0;i<n;i++) {
	       if (mask[i]) {
		  stencil[i] = 0;
	       }
	    }
	 }
	 else {
	    for (i=0;i<n;i++) {
	       if (mask[i]) {
Brian Paul's avatar
Brian Paul committed
224
		  stencil[i] = (GLstencil) (stencil[i] & invmask);
jtg's avatar
jtg committed
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
	       }
	    }
	 }
	 break;
      case GL_REPLACE:
	 if (invmask==0) {
	    for (i=0;i<n;i++) {
	       if (mask[i]) {
                  stencil[i] = ref;
	       }
	    }
	 }
	 else {
	    for (i=0;i<n;i++) {
	       if (mask[i]) {
		  GLstencil s = stencil[i];
Brian Paul's avatar
Brian Paul committed
241
		  stencil[i] = (GLstencil) ((invmask & s ) | (wrtmask & ref));
jtg's avatar
jtg committed
242
243
244
245
246
247
248
249
250
251
	       }
	    }
	 }
	 break;
      case GL_INCR:
	 if (invmask==0) {
	    for (i=0;i<n;i++) {
	       if (mask[i]) {
		  GLstencil s = stencil[i];
		  if (s < STENCIL_MAX) {
Brian Paul's avatar
Brian Paul committed
252
		     stencil[i] = (GLstencil) (s+1);
jtg's avatar
jtg committed
253
254
255
256
257
258
259
260
261
262
		  }
	       }
	    }
	 }
	 else {
	    for (i=0;i<n;i++) {
	       if (mask[i]) {
		  /* VERIFY logic of adding 1 to a write-masked value */
		  GLstencil s = stencil[i];
		  if (s < STENCIL_MAX) {
Brian Paul's avatar
Brian Paul committed
263
		     stencil[i] = (GLstencil) ((invmask & s) | (wrtmask & (s+1)));
jtg's avatar
jtg committed
264
265
266
267
268
269
270
271
272
273
274
		  }
	       }
	    }
	 }
	 break;
      case GL_DECR:
	 if (invmask==0) {
	    for (i=0;i<n;i++) {
	       if (mask[i]) {
		  GLstencil s = stencil[i];
		  if (s>0) {
Brian Paul's avatar
Brian Paul committed
275
		     stencil[i] = (GLstencil) (s-1);
jtg's avatar
jtg committed
276
277
278
279
280
281
282
283
284
285
		  }
	       }
	    }
	 }
	 else {
	    for (i=0;i<n;i++) {
	       if (mask[i]) {
		  /* VERIFY logic of subtracting 1 to a write-masked value */
		  GLstencil s = stencil[i];
		  if (s>0) {
Brian Paul's avatar
Brian Paul committed
286
		     stencil[i] = (GLstencil) ((invmask & s) | (wrtmask & (s-1)));
jtg's avatar
jtg committed
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
		  }
	       }
	    }
	 }
	 break;
      case GL_INCR_WRAP_EXT:
	 if (invmask==0) {
	    for (i=0;i<n;i++) {
	       if (mask[i]) {
                  stencil[i]++;
	       }
	    }
	 }
	 else {
	    for (i=0;i<n;i++) {
	       if (mask[i]) {
                  GLstencil s = stencil[i];
304
                  stencil[i] = (GLstencil) ((invmask & s) | (wrtmask & (s+1)));
jtg's avatar
jtg committed
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
	       }
	    }
	 }
	 break;
      case GL_DECR_WRAP_EXT:
	 if (invmask==0) {
	    for (i=0;i<n;i++) {
	       if (mask[i]) {
		  stencil[i]--;
	       }
	    }
	 }
	 else {
	    for (i=0;i<n;i++) {
	       if (mask[i]) {
                  GLstencil s = stencil[i];
321
                  stencil[i] = (GLstencil) ((invmask & s) | (wrtmask & (s-1)));
jtg's avatar
jtg committed
322
323
324
325
326
327
328
329
330
	       }
	    }
	 }
	 break;
      case GL_INVERT:
	 if (invmask==0) {
	    for (i=0;i<n;i++) {
	       if (mask[i]) {
		  GLstencil s = stencil[i];
Brian Paul's avatar
Brian Paul committed
331
		  stencil[i] = (GLstencil) ~s;
jtg's avatar
jtg committed
332
333
334
335
336
337
338
	       }
	    }
	 }
	 else {
	    for (i=0;i<n;i++) {
	       if (mask[i]) {
		  GLstencil s = stencil[i];
Brian Paul's avatar
Brian Paul committed
339
		  stencil[i] = (GLstencil) ((invmask & s) | (wrtmask & ~s));
jtg's avatar
jtg committed
340
341
342
343
344
	       }
	    }
	 }
	 break;
      default:
345
         gl_problem(ctx, "Bad stencil op in apply_stencil_op");
jtg's avatar
jtg committed
346
347
348
349
350
351
352
   }
}




/*
353
354
355
 * Apply stencil test to an array of stencil values (before depth buffering).
 * Input:  n - number of pixels in the array
 *         stencil - array of [n] stencil values
jtg's avatar
jtg committed
356
357
358
 *         mask - array [n] of flag:  0=skip the pixel, 1=stencil the pixel
 * Output:  mask - pixels which fail the stencil test will have their
 *                 mask flag set to 0.
359
360
 *          stencil - updated stencil values (where the test passed)
 * Return:  GL_FALSE = all pixels failed, GL_TRUE = zero or more pixels passed.
jtg's avatar
jtg committed
361
 */
362
363
364
static GLboolean
do_stencil_test( GLcontext *ctx, GLuint n, GLstencil stencil[],
                 GLubyte mask[] )
jtg's avatar
jtg committed
365
{
366
367
   GLubyte fail[PB_SIZE];
   GLboolean allfail = GL_FALSE;
jtg's avatar
jtg committed
368
369
370
   GLuint i;
   GLstencil r, s;

371
   ASSERT(n <= PB_SIZE);
jtg's avatar
jtg committed
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393

   /*
    * Perform stencil test.  The results of this operation are stored
    * in the fail[] array:
    *   IF fail[i] is non-zero THEN
    *       the stencil fail operator is to be applied
    *   ELSE
    *       the stencil fail operator is not to be applied
    *   ENDIF
    */
   switch (ctx->Stencil.Function) {
      case GL_NEVER:
         /* always fail */
         for (i=0;i<n;i++) {
	    if (mask[i]) {
	       mask[i] = 0;
	       fail[i] = 1;
	    }
	    else {
	       fail[i] = 0;
	    }
	 }
394
	 allfail = GL_TRUE;
jtg's avatar
jtg committed
395
396
	 break;
      case GL_LESS:
Brian Paul's avatar
Brian Paul committed
397
	 r = (GLstencil) (ctx->Stencil.Ref & ctx->Stencil.ValueMask);
jtg's avatar
jtg committed
398
399
	 for (i=0;i<n;i++) {
	    if (mask[i]) {
Brian Paul's avatar
Brian Paul committed
400
	       s = (GLstencil) (stencil[i] & ctx->Stencil.ValueMask);
jtg's avatar
jtg committed
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
	       if (r < s) {
		  /* passed */
		  fail[i] = 0;
	       }
	       else {
		  fail[i] = 1;
		  mask[i] = 0;
	       }
	    }
	    else {
	       fail[i] = 0;
	    }
	 }
	 break;
      case GL_LEQUAL:
Brian Paul's avatar
Brian Paul committed
416
	 r = (GLstencil) (ctx->Stencil.Ref & ctx->Stencil.ValueMask);
jtg's avatar
jtg committed
417
418
	 for (i=0;i<n;i++) {
	    if (mask[i]) {
Brian Paul's avatar
Brian Paul committed
419
	       s = (GLstencil) (stencil[i] & ctx->Stencil.ValueMask);
jtg's avatar
jtg committed
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
	       if (r <= s) {
		  /* pass */
		  fail[i] = 0;
	       }
	       else {
		  fail[i] = 1;
		  mask[i] = 0;
	       }
	    }
	    else {
	       fail[i] = 0;
	    }
	 }
	 break;
      case GL_GREATER:
Brian Paul's avatar
Brian Paul committed
435
	 r = (GLstencil) (ctx->Stencil.Ref & ctx->Stencil.ValueMask);
jtg's avatar
jtg committed
436
437
	 for (i=0;i<n;i++) {
	    if (mask[i]) {
Brian Paul's avatar
Brian Paul committed
438
	       s = (GLstencil) (stencil[i] & ctx->Stencil.ValueMask);
jtg's avatar
jtg committed
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
	       if (r > s) {
		  /* passed */
		  fail[i] = 0;
	       }
	       else {
		  fail[i] = 1;
		  mask[i] = 0;
	       }
	    }
	    else {
	       fail[i] = 0;
	    }
	 }
	 break;
      case GL_GEQUAL:
Brian Paul's avatar
Brian Paul committed
454
	 r = (GLstencil) (ctx->Stencil.Ref & ctx->Stencil.ValueMask);
jtg's avatar
jtg committed
455
456
	 for (i=0;i<n;i++) {
	    if (mask[i]) {
Brian Paul's avatar
Brian Paul committed
457
	       s = (GLstencil) (stencil[i] & ctx->Stencil.ValueMask);
jtg's avatar
jtg committed
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
	       if (r >= s) {
		  /* passed */
		  fail[i] = 0;
	       }
	       else {
		  fail[i] = 1;
		  mask[i] = 0;
	       }
	    }
	    else {
	       fail[i] = 0;
	    }
	 }
	 break;
      case GL_EQUAL:
Brian Paul's avatar
Brian Paul committed
473
	 r = (GLstencil) (ctx->Stencil.Ref & ctx->Stencil.ValueMask);
jtg's avatar
jtg committed
474
475
	 for (i=0;i<n;i++) {
	    if (mask[i]) {
Brian Paul's avatar
Brian Paul committed
476
	       s = (GLstencil) (stencil[i] & ctx->Stencil.ValueMask);
jtg's avatar
jtg committed
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
	       if (r == s) {
		  /* passed */
		  fail[i] = 0;
	       }
	       else {
		  fail[i] = 1;
		  mask[i] = 0;
	       }
	    }
	    else {
	       fail[i] = 0;
	    }
	 }
	 break;
      case GL_NOTEQUAL:
Brian Paul's avatar
Brian Paul committed
492
	 r = (GLstencil) (ctx->Stencil.Ref & ctx->Stencil.ValueMask);
jtg's avatar
jtg committed
493
494
	 for (i=0;i<n;i++) {
	    if (mask[i]) {
Brian Paul's avatar
Brian Paul committed
495
	       s = (GLstencil) (stencil[i] & ctx->Stencil.ValueMask);
jtg's avatar
jtg committed
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
	       if (r != s) {
		  /* passed */
		  fail[i] = 0;
	       }
	       else {
		  fail[i] = 1;
		  mask[i] = 0;
	       }
	    }
	    else {
	       fail[i] = 0;
	    }
	 }
	 break;
      case GL_ALWAYS:
	 /* always pass */
	 for (i=0;i<n;i++) {
	    fail[i] = 0;
	 }
	 break;
      default:
         gl_problem(ctx, "Bad stencil func in gl_stencil_span");
         return 0;
   }

521
522
523
   if (ctx->Stencil.FailFunc != GL_KEEP) {
      apply_stencil_op( ctx, ctx->Stencil.FailFunc, n, stencil, fail );
   }
jtg's avatar
jtg committed
524

525
   return !allfail;
jtg's avatar
jtg committed
526
527
528
529
530
531
}




/*
532
533
 * Apply stencil and depth testing to an array of pixels.
 * Hardware or software stencil buffer acceptable.
jtg's avatar
jtg committed
534
535
 * Input:  n - number of pixels in the span
 *         z - array [n] of z values
536
537
538
539
540
541
542
 *         stencil - array [n] of stencil values
 *         mask - array [n] of flags  (1=test this pixel, 0=skip the pixel)
 * Output:  stencil - modified stencil values
 *          mask - array [n] of flags (1=stencil and depth test passed)
 * Return: GL_TRUE - all fragments failed the testing
 *         GL_FALSE - one or more fragments passed the testing
 * 
jtg's avatar
jtg committed
543
 */
544
static GLboolean
Brian Paul's avatar
Brian Paul committed
545
546
547
stencil_and_ztest_span( GLcontext *ctx, GLuint n, GLint x, GLint y,
                        const GLdepth z[], GLstencil stencil[],
                        GLubyte mask[] )
jtg's avatar
jtg committed
548
{
549
550
551
552
553
554
555
556
557
558
559
560
   ASSERT(ctx->Stencil.Enabled);
   ASSERT(n <= PB_SIZE);

   /*
    * Apply the stencil test to the fragments.
    * failMask[i] is 1 if the stencil test failed.
    */
   if (do_stencil_test( ctx, n, stencil, mask ) == GL_FALSE) {
      /* all fragments failed the stencil test, we're done. */
      return GL_FALSE;
   }

561

562
563
564
565
   /*
    * Some fragments passed the stencil test, apply depth test to them
    * and apply Zpass and Zfail stencil ops.
    */
jtg's avatar
jtg committed
566
567
568
569
   if (ctx->Depth.Test==GL_FALSE) {
      /*
       * No depth buffer, just apply zpass stencil function to active pixels.
       */
570
      apply_stencil_op( ctx, ctx->Stencil.ZPassFunc, n, stencil, mask );
jtg's avatar
jtg committed
571
572
573
574
575
576
577
578
   }
   else {
      /*
       * Perform depth buffering, then apply zpass or zfail stencil function.
       */
      GLubyte passmask[MAX_WIDTH], failmask[MAX_WIDTH], oldmask[MAX_WIDTH];
      GLuint i;

579
580
      /* save the current mask bits */
      MEMCPY(oldmask, mask, n * sizeof(GLubyte));
jtg's avatar
jtg committed
581
582

      /* apply the depth test */
583
      _mesa_depth_test_span(ctx, n, x, y, z, mask);
jtg's avatar
jtg committed
584

585
586
587
588
589
590
591
592
593
594
      /* Set the stencil pass/fail flags according to result of depth testing.
       * if oldmask[i] == 0 then
       *    Don't touch the stencil value
       * else if oldmask[i] and newmask[i] then
       *    Depth test passed
       * else
       *    assert(oldmask[i] && !newmask[i])
       *    Depth test failed
       * endif
       */
jtg's avatar
jtg committed
595
      for (i=0;i<n;i++) {
596
597
598
         ASSERT(mask[i] == 0 || mask[i] == 1);
         passmask[i] = oldmask[i] & mask[i];
         failmask[i] = oldmask[i] & (mask[i] ^ 1);
jtg's avatar
jtg committed
599
600
601
      }

      /* apply the pass and fail operations */
602
603
604
605
606
607
      if (ctx->Stencil.ZFailFunc != GL_KEEP) {
         apply_stencil_op( ctx, ctx->Stencil.ZFailFunc, n, stencil, failmask );
      }
      if (ctx->Stencil.ZPassFunc != GL_KEEP) {
         apply_stencil_op( ctx, ctx->Stencil.ZPassFunc, n, stencil, passmask );
      }
jtg's avatar
jtg committed
608
   }
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627

   return GL_TRUE;  /* one or more fragments passed both tests */
}



/*
 * Apply stencil and depth testing to the span of pixels.
 * Both software and hardware stencil buffers are acceptable.
 * Input:  n - number of pixels in the span
 *         x, y - location of leftmost pixel in span
 *         z - array [n] of z values
 *         mask - array [n] of flags  (1=test this pixel, 0=skip the pixel)
 * Output:  mask - array [n] of flags (1=stencil and depth test passed)
 * Return: GL_TRUE - all fragments failed the testing
 *         GL_FALSE - one or more fragments passed the testing
 * 
 */
GLboolean
Brian Paul's avatar
Brian Paul committed
628
629
_mesa_stencil_and_ztest_span( GLcontext *ctx, GLuint n, GLint x, GLint y,
                              const GLdepth z[], GLubyte mask[] )
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
{
   GLstencil stencilRow[MAX_WIDTH];
   GLstencil *stencil;
   GLboolean result;

   ASSERT(ctx->Stencil.Enabled);
   ASSERT(n <= MAX_WIDTH);

   /* Get initial stencil values */
   if (ctx->Driver.WriteStencilSpan) {
      ASSERT(ctx->Driver.ReadStencilSpan);
      /* Get stencil values from the hardware stencil buffer */
      (*ctx->Driver.ReadStencilSpan)(ctx, n, x, y, stencilRow);
      stencil = stencilRow;
   }
   else {
      /* software stencil buffer */
      stencil = STENCIL_ADDRESS(x, y);
   }

   /* do all the stencil/depth testing/updating */
Brian Paul's avatar
Brian Paul committed
651
   result = stencil_and_ztest_span( ctx, n, x, y, z, stencil, mask );
652
653
654
655
656
657
658

   if (ctx->Driver.WriteStencilSpan) {
      /* Write updated stencil values into hardware stencil buffer */
      (ctx->Driver.WriteStencilSpan)(ctx, n, x, y, stencil, mask );
   }

   return result;
jtg's avatar
jtg committed
659
660
661
662
663
664
665
}




/*
 * Apply the given stencil operator for each pixel in the array whose
666
 * mask flag is set.  This is for software stencil buffers only.
jtg's avatar
jtg committed
667
668
669
670
671
 * Input:  n - number of pixels in the span
 *         x, y - array of [n] pixels
 *         operator - the stencil buffer operator
 *         mask - array [n] of flag:  1=apply operator, 0=don't apply operator
 */
672
673
674
675
static void
apply_stencil_op_to_pixels( const GLcontext *ctx,
                            GLuint n, const GLint x[], const GLint y[],
                            GLenum oper, const GLubyte mask[] )
jtg's avatar
jtg committed
676
{
677
678
679
   const GLstencil ref = ctx->Stencil.Ref;
   const GLstencil wrtmask = ctx->Stencil.WriteMask;
   const GLstencil invmask = (GLstencil) (~ctx->Stencil.WriteMask);
jtg's avatar
jtg committed
680
681
   GLuint i;

682
   ASSERT(!ctx->Driver.WriteStencilSpan);  /* software stencil buffer only! */
jtg's avatar
jtg committed
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700

   switch (oper) {
      case GL_KEEP:
         /* do nothing */
         break;
      case GL_ZERO:
	 if (invmask==0) {
	    for (i=0;i<n;i++) {
	       if (mask[i]) {
                  GLstencil *sptr = STENCIL_ADDRESS( x[i], y[i] );
                  *sptr = 0;
	       }
	    }
	 }
	 else {
	    for (i=0;i<n;i++) {
	       if (mask[i]) {
                  GLstencil *sptr = STENCIL_ADDRESS( x[i], y[i] );
Brian Paul's avatar
Brian Paul committed
701
		  *sptr = (GLstencil) (invmask & *sptr);
jtg's avatar
jtg committed
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
	       }
	    }
	 }
	 break;
      case GL_REPLACE:
	 if (invmask==0) {
	    for (i=0;i<n;i++) {
	       if (mask[i]) {
                  GLstencil *sptr = STENCIL_ADDRESS( x[i], y[i] );
                  *sptr = ref;
	       }
	    }
	 }
	 else {
	    for (i=0;i<n;i++) {
	       if (mask[i]) {
                  GLstencil *sptr = STENCIL_ADDRESS( x[i], y[i] );
Brian Paul's avatar
Brian Paul committed
719
		  *sptr = (GLstencil) ((invmask & *sptr ) | (wrtmask & ref));
jtg's avatar
jtg committed
720
721
722
723
724
725
726
727
728
729
	       }
	    }
	 }
	 break;
      case GL_INCR:
	 if (invmask==0) {
	    for (i=0;i<n;i++) {
	       if (mask[i]) {
                  GLstencil *sptr = STENCIL_ADDRESS( x[i], y[i] );
		  if (*sptr < STENCIL_MAX) {
Brian Paul's avatar
Brian Paul committed
730
		     *sptr = (GLstencil) (*sptr + 1);
jtg's avatar
jtg committed
731
732
733
734
735
736
737
738
739
		  }
	       }
	    }
	 }
	 else {
	    for (i=0;i<n;i++) {
	       if (mask[i]) {
                  GLstencil *sptr = STENCIL_ADDRESS( x[i], y[i] );
		  if (*sptr < STENCIL_MAX) {
Brian Paul's avatar
Brian Paul committed
740
		     *sptr = (GLstencil) ((invmask & *sptr) | (wrtmask & (*sptr+1)));
jtg's avatar
jtg committed
741
742
743
744
745
746
747
748
749
750
751
		  }
	       }
	    }
	 }
	 break;
      case GL_DECR:
	 if (invmask==0) {
	    for (i=0;i<n;i++) {
	       if (mask[i]) {
                  GLstencil *sptr = STENCIL_ADDRESS( x[i], y[i] );
		  if (*sptr>0) {
Brian Paul's avatar
Brian Paul committed
752
		     *sptr = (GLstencil) (*sptr - 1);
jtg's avatar
jtg committed
753
754
755
756
757
758
759
760
761
		  }
	       }
	    }
	 }
	 else {
	    for (i=0;i<n;i++) {
	       if (mask[i]) {
                  GLstencil *sptr = STENCIL_ADDRESS( x[i], y[i] );
		  if (*sptr>0) {
Brian Paul's avatar
Brian Paul committed
762
		     *sptr = (GLstencil) ((invmask & *sptr) | (wrtmask & (*sptr-1)));
jtg's avatar
jtg committed
763
764
765
766
767
768
769
770
771
772
		  }
	       }
	    }
	 }
	 break;
      case GL_INCR_WRAP_EXT:
	 if (invmask==0) {
	    for (i=0;i<n;i++) {
	       if (mask[i]) {
                  GLstencil *sptr = STENCIL_ADDRESS( x[i], y[i] );
Brian Paul's avatar
Brian Paul committed
773
                  *sptr = (GLstencil) (*sptr + 1);
jtg's avatar
jtg committed
774
775
776
777
778
779
780
	       }
	    }
	 }
	 else {
	    for (i=0;i<n;i++) {
	       if (mask[i]) {
                  GLstencil *sptr = STENCIL_ADDRESS( x[i], y[i] );
Brian Paul's avatar
Brian Paul committed
781
                  *sptr = (GLstencil) ((invmask & *sptr) | (wrtmask & (*sptr+1)));
jtg's avatar
jtg committed
782
783
784
785
786
787
788
789
790
	       }
	    }
	 }
	 break;
      case GL_DECR_WRAP_EXT:
	 if (invmask==0) {
	    for (i=0;i<n;i++) {
	       if (mask[i]) {
                  GLstencil *sptr = STENCIL_ADDRESS( x[i], y[i] );
Brian Paul's avatar
Brian Paul committed
791
                  *sptr = (GLstencil) (*sptr - 1);
jtg's avatar
jtg committed
792
793
794
795
796
797
798
	       }
	    }
	 }
	 else {
	    for (i=0;i<n;i++) {
	       if (mask[i]) {
                  GLstencil *sptr = STENCIL_ADDRESS( x[i], y[i] );
Brian Paul's avatar
Brian Paul committed
799
                  *sptr = (GLstencil) ((invmask & *sptr) | (wrtmask & (*sptr-1)));
jtg's avatar
jtg committed
800
801
802
803
804
805
806
807
808
	       }
	    }
	 }
	 break;
      case GL_INVERT:
	 if (invmask==0) {
	    for (i=0;i<n;i++) {
	       if (mask[i]) {
                  GLstencil *sptr = STENCIL_ADDRESS( x[i], y[i] );
Brian Paul's avatar
Brian Paul committed
809
                  *sptr = (GLstencil) (~*sptr);
jtg's avatar
jtg committed
810
811
812
813
814
815
816
	       }
	    }
	 }
	 else {
	    for (i=0;i<n;i++) {
	       if (mask[i]) {
                  GLstencil *sptr = STENCIL_ADDRESS( x[i], y[i] );
Brian Paul's avatar
Brian Paul committed
817
                  *sptr = (GLstencil) ((invmask & *sptr) | (wrtmask & ~*sptr));
jtg's avatar
jtg committed
818
819
820
821
822
823
824
825
826
827
828
829
830
	       }
	    }
	 }
	 break;
      default:
         gl_problem(ctx, "Bad stencilop in apply_stencil_op_to_pixels");
   }
}



/*
 * Apply stencil test to an array of pixels before depth buffering.
831
 * Used for software stencil buffer only.
jtg's avatar
jtg committed
832
833
834
835
836
837
838
 * Input:  n - number of pixels in the span
 *         x, y - array of [n] pixels to stencil
 *         mask - array [n] of flag:  0=skip the pixel, 1=stencil the pixel
 * Output:  mask - pixels which fail the stencil test will have their
 *                 mask flag set to 0.
 * Return:  0 = all pixels failed, 1 = zero or more pixels passed.
 */
839
840
841
static GLboolean
stencil_test_pixels( GLcontext *ctx, GLuint n,
                     const GLint x[], const GLint y[], GLubyte mask[] )
jtg's avatar
jtg committed
842
843
844
845
{
   GLubyte fail[PB_SIZE];
   GLstencil r, s;
   GLuint i;
846
847
848
   GLboolean allfail = GL_FALSE;

   ASSERT(!ctx->Driver.WriteStencilSpan);  /* software stencil buffer only! */
jtg's avatar
jtg committed
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871

   /*
    * Perform stencil test.  The results of this operation are stored
    * in the fail[] array:
    *   IF fail[i] is non-zero THEN
    *       the stencil fail operator is to be applied
    *   ELSE
    *       the stencil fail operator is not to be applied
    *   ENDIF
    */

   switch (ctx->Stencil.Function) {
      case GL_NEVER:
         /* always fail */
         for (i=0;i<n;i++) {
	    if (mask[i]) {
	       mask[i] = 0;
	       fail[i] = 1;
	    }
	    else {
	       fail[i] = 0;
	    }
	 }
872
	 allfail = GL_TRUE;
jtg's avatar
jtg committed
873
874
	 break;
      case GL_LESS:
Brian Paul's avatar
Brian Paul committed
875
	 r = (GLstencil) (ctx->Stencil.Ref & ctx->Stencil.ValueMask);
jtg's avatar
jtg committed
876
877
878
	 for (i=0;i<n;i++) {
	    if (mask[i]) {
               GLstencil *sptr = STENCIL_ADDRESS(x[i],y[i]);
Brian Paul's avatar
Brian Paul committed
879
	       s = (GLstencil) (*sptr & ctx->Stencil.ValueMask);
jtg's avatar
jtg committed
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
	       if (r < s) {
		  /* passed */
		  fail[i] = 0;
	       }
	       else {
		  fail[i] = 1;
		  mask[i] = 0;
	       }
	    }
	    else {
	       fail[i] = 0;
	    }
	 }
	 break;
      case GL_LEQUAL:
Brian Paul's avatar
Brian Paul committed
895
	 r = (GLstencil) (ctx->Stencil.Ref & ctx->Stencil.ValueMask);
jtg's avatar
jtg committed
896
897
898
	 for (i=0;i<n;i++) {
	    if (mask[i]) {
               GLstencil *sptr = STENCIL_ADDRESS(x[i],y[i]);
Brian Paul's avatar
Brian Paul committed
899
	       s = (GLstencil) (*sptr & ctx->Stencil.ValueMask);
jtg's avatar
jtg committed
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
	       if (r <= s) {
		  /* pass */
		  fail[i] = 0;
	       }
	       else {
		  fail[i] = 1;
		  mask[i] = 0;
	       }
	    }
	    else {
	       fail[i] = 0;
	    }
	 }
	 break;
      case GL_GREATER:
Brian Paul's avatar
Brian Paul committed
915
	 r = (GLstencil) (ctx->Stencil.Ref & ctx->Stencil.ValueMask);
jtg's avatar
jtg committed
916
917
918
	 for (i=0;i<n;i++) {
	    if (mask[i]) {
               GLstencil *sptr = STENCIL_ADDRESS(x[i],y[i]);
Brian Paul's avatar
Brian Paul committed
919
	       s = (GLstencil) (*sptr & ctx->Stencil.ValueMask);
jtg's avatar
jtg committed
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
	       if (r > s) {
		  /* passed */
		  fail[i] = 0;
	       }
	       else {
		  fail[i] = 1;
		  mask[i] = 0;
	       }
	    }
	    else {
	       fail[i] = 0;
	    }
	 }
	 break;
      case GL_GEQUAL:
Brian Paul's avatar
Brian Paul committed
935
	 r = (GLstencil) (ctx->Stencil.Ref & ctx->Stencil.ValueMask);
jtg's avatar
jtg committed
936
937
938
	 for (i=0;i<n;i++) {
	    if (mask[i]) {
               GLstencil *sptr = STENCIL_ADDRESS(x[i],y[i]);
Brian Paul's avatar
Brian Paul committed
939
	       s = (GLstencil) (*sptr & ctx->Stencil.ValueMask);
jtg's avatar
jtg committed
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
	       if (r >= s) {
		  /* passed */
		  fail[i] = 0;
	       }
	       else {
		  fail[i] = 1;
		  mask[i] = 0;
	       }
	    }
	    else {
	       fail[i] = 0;
	    }
	 }
	 break;
      case GL_EQUAL:
Brian Paul's avatar
Brian Paul committed
955
	 r = (GLstencil) (ctx->Stencil.Ref & ctx->Stencil.ValueMask);
jtg's avatar
jtg committed
956
957
958
	 for (i=0;i<n;i++) {
	    if (mask[i]) {
               GLstencil *sptr = STENCIL_ADDRESS(x[i],y[i]);
Brian Paul's avatar
Brian Paul committed
959
	       s = (GLstencil) (*sptr & ctx->Stencil.ValueMask);
jtg's avatar
jtg committed
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
	       if (r == s) {
		  /* passed */
		  fail[i] = 0;
	       }
	       else {
		  fail[i] = 1;
		  mask[i] = 0;
	       }
	    }
	    else {
	       fail[i] = 0;
	    }
	 }
	 break;
      case GL_NOTEQUAL:
Brian Paul's avatar
Brian Paul committed
975
	 r = (GLstencil) (ctx->Stencil.Ref & ctx->Stencil.ValueMask);
jtg's avatar
jtg committed
976
977
978
	 for (i=0;i<n;i++) {
	    if (mask[i]) {
               GLstencil *sptr = STENCIL_ADDRESS(x[i],y[i]);
Brian Paul's avatar
Brian Paul committed
979
	       s = (GLstencil) (*sptr & ctx->Stencil.ValueMask);
jtg's avatar
jtg committed
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
	       if (r != s) {
		  /* passed */
		  fail[i] = 0;
	       }
	       else {
		  fail[i] = 1;
		  mask[i] = 0;
	       }
	    }
	    else {
	       fail[i] = 0;
	    }
	 }
	 break;
      case GL_ALWAYS:
	 /* always pass */
	 for (i=0;i<n;i++) {
	    fail[i] = 0;
	 }
	 break;
      default:
         gl_problem(ctx, "Bad stencil func in gl_stencil_pixels");
         return 0;
   }

1005
1006
1007
   if (ctx->Stencil.FailFunc != GL_KEEP) {
      apply_stencil_op_to_pixels( ctx, n, x, y, ctx->Stencil.FailFunc, fail );
   }
jtg's avatar
jtg committed
1008

1009
   return !allfail;
jtg's avatar
jtg committed
1010
1011
1012
1013
1014
1015
}




/*
1016
1017
1018
1019
 * Apply stencil and depth testing to an array of pixels.
 * This is used both for software and hardware stencil buffers.
 *
 * The comments in this function are a bit sparse but the code is
Brian Paul's avatar
Brian Paul committed
1020
 * almost identical to stencil_and_ztest_span(), which is well
1021
1022
1023
1024
 * commented.
 * 
 * Input:  n - number of pixels in the array
 *         x, y - array of [n] pixel positions
jtg's avatar
jtg committed
1025
 *         z - array [n] of z values
1026
1027
1028
1029
 *         mask - array [n] of flags  (1=test this pixel, 0=skip the pixel)
 * Output: mask - array [n] of flags (1=stencil and depth test passed)
 * Return: GL_TRUE - all fragments failed the testing
 *         GL_FALSE - one or more fragments passed the testing
jtg's avatar
jtg committed
1030
 */
1031
GLboolean
Brian Paul's avatar
Brian Paul committed
1032
1033
1034
_mesa_stencil_and_ztest_pixels( GLcontext *ctx,
                                GLuint n, const GLint x[], const GLint y[],
                                const GLdepth z[], GLubyte mask[] )
jtg's avatar
jtg committed
1035
{
1036
1037
1038
1039
1040
1041
   ASSERT(ctx->Stencil.Enabled);
   ASSERT(n <= PB_SIZE);

   if (ctx->Driver.WriteStencilPixels) {
      /*** Hardware stencil buffer ***/
      GLstencil stencil[PB_SIZE];
1042
      GLubyte origMask[PB_SIZE];
1043
1044
1045
1046

      ASSERT(ctx->Driver.ReadStencilPixels);
      (*ctx->Driver.ReadStencilPixels)(ctx, n, x, y, stencil);

1047
1048
      MEMCPY(origMask, mask, n * sizeof(GLubyte));

1049
1050
      (void) do_stencil_test( ctx, n, stencil, mask );

1051
1052
1053
1054
      if (ctx->Depth.Test == GL_FALSE) {
         apply_stencil_op( ctx, ctx->Stencil.ZPassFunc, n, stencil, mask );
      }
      else {
1055
         _mesa_depth_test_pixels(ctx, n, x, y, z, mask);
1056
1057

         if (ctx->Stencil.ZFailFunc != GL_KEEP) {
1058
1059
1060
1061
1062
1063
1064
            GLubyte failmask[PB_SIZE];
            GLuint i;
            for (i = 0; i < n; i++) {
               ASSERT(mask[i] == 0 || mask[i] == 1);
               failmask[i] = origMask[i] & (mask[i] ^ 1);
            }
            apply_stencil_op(ctx, ctx->Stencil.ZFailFunc, n, stencil, failmask);
1065
1066
         }
         if (ctx->Stencil.ZPassFunc != GL_KEEP) {
1067
1068
1069
1070
1071
1072
1073
            GLubyte passmask[PB_SIZE];
            GLuint i;
            for (i = 0; i < n; i++) {
               ASSERT(mask[i] == 0 || mask[i] == 1);
               passmask[i] = origMask[i] & mask[i];
            }
            apply_stencil_op(ctx, ctx->Stencil.ZPassFunc, n, stencil, passmask);
1074
1075
1076
1077
         }
      }

      /* Write updated stencil values into hardware stencil buffer */
1078
      (ctx->Driver.WriteStencilPixels)(ctx, n, x, y, stencil, origMask );
1079
1080

      return GL_TRUE;
jtg's avatar
jtg committed
1081
1082
   }
   else {
1083
      /*** Software stencil buffer ***/
jtg's avatar
jtg committed
1084

1085
1086
1087
1088
      if (stencil_test_pixels(ctx, n, x, y, mask) == GL_FALSE) {
         /* all fragments failed the stencil test, we're done. */
         return GL_FALSE;
      }
jtg's avatar
jtg committed
1089

1090
1091
      if (ctx->Depth.Test==GL_FALSE) {
         apply_stencil_op_to_pixels( ctx, n, x, y, ctx->Stencil.ZPassFunc, mask );
jtg's avatar
jtg committed
1092
      }
1093
1094
1095
      else {
         GLubyte passmask[PB_SIZE], failmask[PB_SIZE], oldmask[PB_SIZE];
         GLuint i;
jtg's avatar
jtg committed
1096

1097
         MEMCPY(oldmask, mask, n * sizeof(GLubyte));
jtg's avatar
jtg committed
1098

1099
         _mesa_depth_test_pixels(ctx, n, x, y, z, mask);
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118

         for (i=0;i<n;i++) {
            ASSERT(mask[i] == 0 || mask[i] == 1);
            passmask[i] = oldmask[i] & mask[i];
            failmask[i] = oldmask[i] & (mask[i] ^ 1);
         }

         if (ctx->Stencil.ZFailFunc != GL_KEEP) {
            apply_stencil_op_to_pixels( ctx, n, x, y,
                                        ctx->Stencil.ZFailFunc, failmask );
         }
         if (ctx->Stencil.ZPassFunc != GL_KEEP) {
            apply_stencil_op_to_pixels( ctx, n, x, y,
                                        ctx->Stencil.ZPassFunc, passmask );
         }
      }

      return GL_TRUE;  /* one or more fragments passed both tests */
   }
jtg's avatar
jtg committed
1119
1120
1121
1122
1123
1124
}



/*
 * Return a span of stencil values from the stencil buffer.
1125
 * Used for glRead/CopyPixels
jtg's avatar
jtg committed
1126
1127
1128
1129
 * Input:  n - how many pixels
 *         x,y - location of first pixel
 * Output:  stencil - the array of stencil values
 */
1130
1131
1132
void
_mesa_read_stencil_span( GLcontext *ctx,
                         GLint n, GLint x, GLint y, GLstencil stencil[] )
jtg's avatar
jtg committed
1133
{
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
   if (y < 0 || y >= ctx->DrawBuffer->Height ||
       x + n <= 0 || x >= ctx->DrawBuffer->Width) {
      /* span is completely outside framebuffer */
      return; /* undefined values OK */
   }

   if (x < 0) {
      GLint dx = -x;
      x = 0;
      n -= dx;
      stencil += dx;
   }
   if (x + n > ctx->DrawBuffer->Width) {
      GLint dx = x + n - ctx->DrawBuffer->Width;
      n -= dx;
   }
   if (n <= 0) {
      return;
   }


1155
   ASSERT(n >= 0);
1156
1157
1158
1159
1160
   if (ctx->Driver.ReadStencilSpan) {
      (*ctx->Driver.ReadStencilSpan)( ctx, (GLuint) n, x, y, stencil );
   }
   else if (ctx->DrawBuffer->Stencil) {
      const GLstencil *s = STENCIL_ADDRESS( x, y );
jtg's avatar
jtg committed
1161
#if STENCIL_BITS == 8
1162
      MEMCPY( stencil, s, n * sizeof(GLstencil) );
jtg's avatar
jtg committed
1163
#else
1164
1165
1166
      GLuint i;
      for (i=0;i<n;i++)
         stencil[i] = s[i];
jtg's avatar
jtg committed
1167
1168
1169
1170
1171
1172
1173
1174
#endif
   }
}



/*
 * Write a span of stencil values to the stencil buffer.
1175
 * Used for glDraw/CopyPixels
jtg's avatar
jtg committed
1176
 * Input:  n - how many pixels
1177
 *         x, y - location of first pixel
jtg's avatar
jtg committed
1178
1179
 *         stencil - the array of stencil values
 */
1180
1181
1182
void
_mesa_write_stencil_span( GLcontext *ctx, GLint n, GLint x, GLint y,
                          const GLstencil stencil[] )
jtg's avatar
jtg committed
1183
{
1184
1185
   const GLstencil *ssrc = stencil;

1186
1187
1188
1189
1190
   if (y < 0 || y >= ctx->DrawBuffer->Height ||
       x + n <= 0 || x >= ctx->DrawBuffer->Width) {
      /* span is completely outside framebuffer */
      return; /* undefined values OK */
   }
1191

1192
1193
1194
1195
   if (x < 0) {
      GLint dx = -x;
      x = 0;
      n -= dx;
1196
      ssrc += dx;
1197
1198
1199
1200
1201
1202
1203
1204
   }
   if (x + n > ctx->DrawBuffer->Width) {
      GLint dx = x + n - ctx->DrawBuffer->Width;
      n -= dx;
   }
   if (n <= 0) {
      return;
   }
1205

1206
   if (ctx->Driver.WriteStencilSpan) {
1207
      (*ctx->Driver.WriteStencilSpan)( ctx, n, x, y, ssrc, NULL );
1208
1209
1210
   }
   else if (ctx->DrawBuffer->Stencil) {
      GLstencil *s = STENCIL_ADDRESS( x, y );
jtg's avatar
jtg committed
1211
#if STENCIL_BITS == 8
1212
      MEMCPY( s, ssrc, n * sizeof(GLstencil) );
jtg's avatar
jtg committed
1213
#else
1214
1215
      GLuint i;
      for (i=0;i<n;i++)
1216
         s[i] = ssrc[i];
jtg's avatar
jtg committed
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
#endif
   }
}



/*
 * Allocate a new stencil buffer.  If there's an old one it will be
 * deallocated first.  The new stencil buffer will be uninitialized.
 */
Brian Paul's avatar
Brian Paul committed
1227
1228
void
_mesa_alloc_stencil_buffer( GLcontext *ctx )
jtg's avatar
jtg committed
1229
{
1230
   GLuint buffersize = ctx->DrawBuffer->Width * ctx->DrawBuffer->Height;
jtg's avatar
jtg committed
1231
1232

   /* deallocate current stencil buffer if present */
1233
1234
1235
   if (ctx->DrawBuffer->Stencil) {
      FREE(ctx->DrawBuffer->Stencil);
      ctx->DrawBuffer->Stencil = NULL;
jtg's avatar
jtg committed
1236
1237
1238
   }

   /* allocate new stencil buffer */
1239
1240
   ctx->DrawBuffer->Stencil = (GLstencil *) MALLOC(buffersize * sizeof(GLstencil));
   if (!ctx->DrawBuffer->Stencil) {
jtg's avatar
jtg committed
1241
      /* out of memory */
1242
      _mesa_set_enable( ctx, GL_STENCIL_TEST, GL_FALSE );
Brian Paul's avatar
Brian Paul committed
1243
      gl_error( ctx, GL_OUT_OF_MEMORY, "_mesa_alloc_stencil_buffer" );
jtg's avatar
jtg committed
1244
1245
1246
1247
1248
   }
}



1249
1250
1251
1252
1253
/*
 * Clear the software (malloc'd) stencil buffer.
 */
static void
clear_software_stencil_buffer( GLcontext *ctx )
jtg's avatar
jtg committed
1254
{
1255
   if (ctx->Visual->StencilBits==0 || !ctx->DrawBuffer->Stencil) {
jtg's avatar
jtg committed
1256
1257
1258
1259
1260
1261
      /* no stencil buffer */
      return;
   }

   if (ctx->Scissor.Enabled) {
      /* clear scissor region only */
1262
1263
1264
1265
      const GLint width = ctx->DrawBuffer->Xmax - ctx->DrawBuffer->Xmin + 1;
      if (ctx->Stencil.WriteMask != STENCIL_MAX) {
         /* must apply mask to the clear */
         GLint y;
1266
         for (y = ctx->DrawBuffer->Ymin; y <= ctx->DrawBuffer->Ymax; y++) {
1267
1268
1269
            const GLstencil mask = ctx->Stencil.WriteMask;
            const GLstencil invMask = ~mask;
            const GLstencil clearVal = (ctx->Stencil.Clear & mask);
1270
1271
1272
1273
            GLstencil *stencil = STENCIL_ADDRESS( ctx->DrawBuffer->Xmin, y );
            GLint i;
            for (i = 0; i < width; i++) {
               stencil[i] = (stencil[i] & invMask) | clearVal;
1274
1275
1276
1277
1278
1279
            }
         }
      }
      else {
         /* no masking */
         GLint y;
1280
1281
         for (y = ctx->DrawBuffer->Ymin; y <= ctx->DrawBuffer->Ymax; y++) {
            GLstencil *stencil = STENCIL_ADDRESS( ctx->DrawBuffer->Xmin, y );
jtg's avatar
jtg committed
1282
#if STENCIL_BITS==8
1283
            MEMSET( stencil, ctx->Stencil.Clear, width * sizeof(GLstencil) );
jtg's avatar
jtg committed
1284
#else
1285
1286
1287
            GLint i;
            for (i = 0; i < width; i++)
               stencil[x] = ctx->Stencil.Clear;
jtg's avatar
jtg committed
1288
#endif
1289
         }
jtg's avatar
jtg committed
1290
1291
1292
1293
      }
   }
   else {
      /* clear whole stencil buffer */
1294
1295
1296
      if (ctx->Stencil.WriteMask != STENCIL_MAX) {
         /* must apply mask to the clear */
         const GLuint n = ctx->DrawBuffer->Width * ctx->DrawBuffer->Height;
1297
         GLstencil *stencil = ctx->DrawBuffer->Stencil;
1298
1299
1300
1301
1302
         const GLstencil mask = ctx->Stencil.WriteMask;
         const GLstencil invMask = ~mask;
         const GLstencil clearVal = (ctx->Stencil.Clear & mask);
         GLuint i;
         for (i = 0; i < n; i++) {
1303
            stencil[i] = (stencil[i] & invMask) | clearVal;
1304
1305
1306
1307
1308
         }
      }
      else {
         /* clear whole buffer without masking */
         const GLuint n = ctx->DrawBuffer->Width * ctx->DrawBuffer->Height;
1309
         GLstencil *stencil = ctx->DrawBuffer->Stencil;
1310

jtg's avatar
jtg committed
1311
#if STENCIL_BITS==8
1312
         MEMSET(stencil, ctx->Stencil.Clear, n * sizeof(GLstencil) );
jtg's avatar
jtg committed
1313
#else
1314
1315
         GLuint i;
         for (i = 0; i < n; i++) {
1316
            stencil[i] = ctx->Stencil.Clear;
1317
         }
jtg's avatar
jtg committed
1318
#endif
1319
      }
jtg's avatar
jtg committed
1320
1321
   }
}
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352



/*
 * Clear the hardware (in graphics card) stencil buffer.
 * This is done with the Driver.WriteStencilSpan() and Driver.ReadStencilSpan()
 * functions.
 * Actually, if there is a hardware stencil buffer it really should have
 * been cleared in Driver.Clear()!  However, if the hardware does not
 * support scissored clears or masked clears (i.e. glStencilMask) then
 * we have to use the span-based functions.
 */
static void
clear_hardware_stencil_buffer( GLcontext *ctx )
{
   ASSERT(ctx->Driver.WriteStencilSpan);
   ASSERT(ctx->Driver.ReadStencilSpan);

   if (ctx->Scissor.Enabled) {
      /* clear scissor region only */
      const GLint x = ctx->DrawBuffer->Xmin;
      const GLint width = ctx->DrawBuffer->Xmax - ctx->DrawBuffer->Xmin + 1;
      if (ctx->Stencil.WriteMask != STENCIL_MAX) {
         /* must apply mask to the clear */
         GLint y;
         for (y = ctx->DrawBuffer->Ymin; y <= ctx->DrawBuffer->Ymax; y++) {
            const GLstencil mask = ctx->Stencil.WriteMask;
            const GLstencil invMask = ~mask;
            const GLstencil clearVal = (ctx->Stencil.Clear & mask);
            GLstencil stencil[MAX_WIDTH];
            GLint i;
1353
            (*ctx->Driver.ReadStencilSpan)(ctx, width, x, y, stencil);
1354
1355
1356
            for (i = 0; i < width; i++) {
               stencil[i] = (stencil[i] & invMask) | clearVal;
            }
1357
            (*ctx->Driver.WriteStencilSpan)(ctx, width, x, y, stencil, NULL);
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
         }
      }
      else {
         /* no masking */
         GLstencil stencil[MAX_WIDTH];
         GLint y, i;
         for (i = 0; i < width; i++) {
            stencil[i] = ctx->Stencil.Clear;
         }
         for (y = ctx->DrawBuffer->Ymin; y <= ctx->DrawBuffer->Ymax; y++) {
1368
            (*ctx->Driver.WriteStencilSpan)(ctx, width, x, y, stencil, NULL);
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
         }
      }
   }
   else {
      /* clear whole stencil buffer */
      if (ctx->Stencil.WriteMask != STENCIL_MAX) {
         /* must apply mask to the clear */
         const GLstencil mask = ctx->Stencil.WriteMask;
         const GLstencil invMask = ~mask;
         const GLstencil clearVal = (ctx->Stencil.Clear & mask);
         const GLint width = ctx->DrawBuffer->Width;
         const GLint height = ctx->DrawBuffer->Height;
         const GLint x = ctx->DrawBuffer->Xmin;
         GLint y;
         for (y = 0; y < height; y++) {
            GLstencil stencil[MAX_WIDTH];
            GLuint i;
1386
            (*ctx->Driver.ReadStencilSpan)(ctx, width, x, y, stencil);
1387
1388
1389
            for (i = 0; i < width; i++) {
               stencil[i] = (stencil[i] & invMask) | clearVal;
            }
1390
            (*ctx->Driver.WriteStencilSpan)(ctx, width, x, y, stencil, NULL);
1391
1392
1393
1394
1395
         }
      }
      else {
         /* clear whole buffer without masking */
         const GLint width = ctx->DrawBuffer->Width;
1396
         const GLint height = ctx->DrawBuffer->Height;
1397
1398
1399
1400
1401
1402
1403
         const GLint x = ctx->DrawBuffer->Xmin;
         GLstencil stencil[MAX_WIDTH];
         GLint y, i;
         for (i = 0; i < width; i++) {
            stencil[i] = ctx->Stencil.Clear;
         }
         for (y = 0; y < height; y++) {
1404
            (*ctx->Driver.WriteStencilSpan)(ctx, width, x, y, stencil, NULL);
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
         }
      }
   }
}



/*
 * Clear the stencil buffer.
 */
Brian Paul's avatar
Brian Paul committed
1415
1416
void
_mesa_clear_stencil_buffer( GLcontext *ctx )
1417
1418
1419
1420
1421