Tizen 2.0 Release
[profile/ivi/osmesa.git] / src / gallium / drivers / softpipe / sp_quad_depth_test.c
1 /**************************************************************************
2  * 
3  * Copyright 2007 Tungsten Graphics, Inc., Cedar Park, Texas.
4  * Copyright 2010 VMware, Inc.
5  * 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
9  * "Software"), to deal in the Software without restriction, including
10  * without limitation the rights to use, copy, modify, merge, publish,
11  * distribute, sub license, and/or sell copies of the Software, and to
12  * permit persons to whom the Software is furnished to do so, subject to
13  * the following conditions:
14  * 
15  * The above copyright notice and this permission notice (including the
16  * next paragraph) shall be included in all copies or substantial portions
17  * of the Software.
18  * 
19  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
20  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
22  * IN NO EVENT SHALL THE AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR
23  * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
24  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
25  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26  * 
27  **************************************************************************/
28
29 /**
30  * \brief  Quad depth / stencil testing
31  */
32
33 #include "pipe/p_defines.h"
34 #include "util/u_format.h"
35 #include "util/u_memory.h"
36 #include "tgsi/tgsi_scan.h"
37 #include "sp_context.h"
38 #include "sp_quad.h"
39 #include "sp_quad_pipe.h"
40 #include "sp_tile_cache.h"
41 #include "sp_state.h"           /* for sp_fragment_shader */
42
43
44 struct depth_data {
45    struct pipe_surface *ps;
46    enum pipe_format format;
47    unsigned bzzzz[QUAD_SIZE];  /**< Z values fetched from depth buffer */
48    unsigned qzzzz[QUAD_SIZE];  /**< Z values from the quad */
49    ubyte stencilVals[QUAD_SIZE];
50    boolean use_shader_stencil_refs;
51    ubyte shader_stencil_refs[QUAD_SIZE];
52    struct softpipe_cached_tile *tile;
53 };
54
55
56
57 static void
58 get_depth_stencil_values( struct depth_data *data,
59                           const struct quad_header *quad )
60 {
61    unsigned j;
62    const struct softpipe_cached_tile *tile = data->tile;
63
64    switch (data->format) {
65    case PIPE_FORMAT_Z16_UNORM:
66       for (j = 0; j < QUAD_SIZE; j++) {
67          int x = quad->input.x0 % TILE_SIZE + (j & 1);
68          int y = quad->input.y0 % TILE_SIZE + (j >> 1);
69          data->bzzzz[j] = tile->data.depth16[y][x];
70       }
71       break;
72    case PIPE_FORMAT_Z32_UNORM:
73       for (j = 0; j < QUAD_SIZE; j++) {
74          int x = quad->input.x0 % TILE_SIZE + (j & 1);
75          int y = quad->input.y0 % TILE_SIZE + (j >> 1);
76          data->bzzzz[j] = tile->data.depth32[y][x];
77       }
78       break;
79    case PIPE_FORMAT_Z24X8_UNORM:
80    case PIPE_FORMAT_Z24_UNORM_S8_USCALED:
81       for (j = 0; j < QUAD_SIZE; j++) {
82          int x = quad->input.x0 % TILE_SIZE + (j & 1);
83          int y = quad->input.y0 % TILE_SIZE + (j >> 1);
84          data->bzzzz[j] = tile->data.depth32[y][x] & 0xffffff;
85          data->stencilVals[j] = tile->data.depth32[y][x] >> 24;
86       }
87       break;
88    case PIPE_FORMAT_X8Z24_UNORM:
89    case PIPE_FORMAT_S8_USCALED_Z24_UNORM:
90       for (j = 0; j < QUAD_SIZE; j++) {
91          int x = quad->input.x0 % TILE_SIZE + (j & 1);
92          int y = quad->input.y0 % TILE_SIZE + (j >> 1);
93          data->bzzzz[j] = tile->data.depth32[y][x] >> 8;
94          data->stencilVals[j] = tile->data.depth32[y][x] & 0xff;
95       }
96       break;
97    case PIPE_FORMAT_S8_USCALED:
98       for (j = 0; j < QUAD_SIZE; j++) {
99          int x = quad->input.x0 % TILE_SIZE + (j & 1);
100          int y = quad->input.y0 % TILE_SIZE + (j >> 1);
101          data->bzzzz[j] = 0;
102          data->stencilVals[j] = tile->data.stencil8[y][x];
103       }
104       break;
105    default:
106       assert(0);
107    }
108 }
109
110
111 /**
112  * If the shader has not been run, interpolate the depth values
113  * ourselves.
114  */
115 static void
116 interpolate_quad_depth( struct quad_header *quad )
117 {
118    const float fx = (float) quad->input.x0;
119    const float fy = (float) quad->input.y0;
120    const float dzdx = quad->posCoef->dadx[2];
121    const float dzdy = quad->posCoef->dady[2];
122    const float z0 = quad->posCoef->a0[2] + dzdx * fx + dzdy * fy;
123
124    quad->output.depth[0] = z0;
125    quad->output.depth[1] = z0 + dzdx;
126    quad->output.depth[2] = z0 + dzdy;
127    quad->output.depth[3] = z0 + dzdx + dzdy;
128 }
129
130
131 /**
132  * Compute the depth_data::qzzzz[] values from the float fragment Z values.
133  */
134 static void
135 convert_quad_depth( struct depth_data *data, 
136                     const struct quad_header *quad )
137 {
138    unsigned j;
139
140    /* Convert quad's float depth values to int depth values (qzzzz).
141     * If the Z buffer stores integer values, we _have_ to do the depth
142     * compares with integers (not floats).  Otherwise, the float->int->float
143     * conversion of Z values (which isn't an identity function) will cause
144     * Z-fighting errors.
145     */
146    switch (data->format) {
147    case PIPE_FORMAT_Z16_UNORM:
148       {
149          float scale = 65535.0;
150
151          for (j = 0; j < QUAD_SIZE; j++) {
152             data->qzzzz[j] = (unsigned) (quad->output.depth[j] * scale);
153          }
154       }
155       break;
156    case PIPE_FORMAT_Z32_UNORM:
157       {
158          double scale = (double) (uint) ~0UL;
159
160          for (j = 0; j < QUAD_SIZE; j++) {
161             data->qzzzz[j] = (unsigned) (quad->output.depth[j] * scale);
162          }
163       }
164       break;
165    case PIPE_FORMAT_Z24X8_UNORM:
166    case PIPE_FORMAT_Z24_UNORM_S8_USCALED:
167       {
168          float scale = (float) ((1 << 24) - 1);
169
170          for (j = 0; j < QUAD_SIZE; j++) {
171             data->qzzzz[j] = (unsigned) (quad->output.depth[j] * scale);
172          }
173       }
174       break;
175    case PIPE_FORMAT_X8Z24_UNORM:
176    case PIPE_FORMAT_S8_USCALED_Z24_UNORM:
177       {
178          float scale = (float) ((1 << 24) - 1);
179
180          for (j = 0; j < QUAD_SIZE; j++) {
181             data->qzzzz[j] = (unsigned) (quad->output.depth[j] * scale);
182          }
183       }
184       break;
185    default:
186       assert(0);
187    }
188 }
189
190
191 /**
192  * Compute the depth_data::shader_stencil_refs[] values from the float fragment stencil values.
193  */
194 static void
195 convert_quad_stencil( struct depth_data *data, 
196                       const struct quad_header *quad )
197 {
198    unsigned j;
199
200    data->use_shader_stencil_refs = TRUE;
201    /* Copy quads stencil values
202     */
203    switch (data->format) {
204    case PIPE_FORMAT_Z24X8_UNORM:
205    case PIPE_FORMAT_Z24_UNORM_S8_USCALED:
206    case PIPE_FORMAT_X8Z24_UNORM:
207    case PIPE_FORMAT_S8_USCALED_Z24_UNORM:
208    case PIPE_FORMAT_S8_USCALED:
209       for (j = 0; j < QUAD_SIZE; j++) {
210          data->shader_stencil_refs[j] = ((unsigned)(quad->output.stencil[j]));
211       }
212       break;
213    default:
214       assert(0);
215    }
216 }
217
218 /**
219  * Write data->bzzzz[] values and data->stencilVals into the Z/stencil buffer.
220  */
221 static void
222 write_depth_stencil_values( struct depth_data *data,
223                             struct quad_header *quad )
224 {
225    struct softpipe_cached_tile *tile = data->tile;
226    unsigned j;
227
228    /* put updated Z values back into cached tile */
229    switch (data->format) {
230    case PIPE_FORMAT_Z16_UNORM:
231       for (j = 0; j < QUAD_SIZE; j++) {
232          int x = quad->input.x0 % TILE_SIZE + (j & 1);
233          int y = quad->input.y0 % TILE_SIZE + (j >> 1);
234          tile->data.depth16[y][x] = (ushort) data->bzzzz[j];
235       }
236       break;
237    case PIPE_FORMAT_Z24X8_UNORM:
238    case PIPE_FORMAT_Z32_UNORM:
239       for (j = 0; j < QUAD_SIZE; j++) {
240          int x = quad->input.x0 % TILE_SIZE + (j & 1);
241          int y = quad->input.y0 % TILE_SIZE + (j >> 1);
242          tile->data.depth32[y][x] = data->bzzzz[j];
243       }
244       break;
245    case PIPE_FORMAT_Z24_UNORM_S8_USCALED:
246       for (j = 0; j < QUAD_SIZE; j++) {
247          int x = quad->input.x0 % TILE_SIZE + (j & 1);
248          int y = quad->input.y0 % TILE_SIZE + (j >> 1);
249          tile->data.depth32[y][x] = (data->stencilVals[j] << 24) | data->bzzzz[j];
250       }
251       break;
252    case PIPE_FORMAT_S8_USCALED_Z24_UNORM:
253       for (j = 0; j < QUAD_SIZE; j++) {
254          int x = quad->input.x0 % TILE_SIZE + (j & 1);
255          int y = quad->input.y0 % TILE_SIZE + (j >> 1);
256          tile->data.depth32[y][x] = (data->bzzzz[j] << 8) | data->stencilVals[j];
257       }
258       break;
259    case PIPE_FORMAT_X8Z24_UNORM:
260       for (j = 0; j < QUAD_SIZE; j++) {
261          int x = quad->input.x0 % TILE_SIZE + (j & 1);
262          int y = quad->input.y0 % TILE_SIZE + (j >> 1);
263          tile->data.depth32[y][x] = data->bzzzz[j] << 8;
264       }
265       break;
266    case PIPE_FORMAT_S8_USCALED:
267       for (j = 0; j < QUAD_SIZE; j++) {
268          int x = quad->input.x0 % TILE_SIZE + (j & 1);
269          int y = quad->input.y0 % TILE_SIZE + (j >> 1);
270          tile->data.stencil8[y][x] = data->stencilVals[j];
271       }
272       break;
273
274    default:
275       assert(0);
276    }
277 }
278
279
280
281 /** Only 8-bit stencil supported */
282 #define STENCIL_MAX 0xff
283
284
285 /**
286  * Do the basic stencil test (compare stencil buffer values against the
287  * reference value.
288  *
289  * \param data->stencilVals  the stencil values from the stencil buffer
290  * \param func  the stencil func (PIPE_FUNC_x)
291  * \param ref  the stencil reference value
292  * \param valMask  the stencil value mask indicating which bits of the stencil
293  *                 values and ref value are to be used.
294  * \return mask indicating which pixels passed the stencil test
295  */
296 static unsigned
297 do_stencil_test(struct depth_data *data,
298                 unsigned func,
299                 unsigned ref, unsigned valMask)
300 {
301    unsigned passMask = 0x0;
302    unsigned j;
303    ubyte refs[QUAD_SIZE];
304
305    for (j = 0; j < QUAD_SIZE; j++) {
306       if (data->use_shader_stencil_refs)
307          refs[j] = data->shader_stencil_refs[j] & valMask;
308       else 
309          refs[j] = ref & valMask;
310    }
311
312    switch (func) {
313    case PIPE_FUNC_NEVER:
314       /* passMask = 0x0 */
315       break;
316    case PIPE_FUNC_LESS:
317       for (j = 0; j < QUAD_SIZE; j++) {
318          if (refs[j] < (data->stencilVals[j] & valMask)) {
319             passMask |= (1 << j);
320          }
321       }
322       break;
323    case PIPE_FUNC_EQUAL:
324       for (j = 0; j < QUAD_SIZE; j++) {
325          if (refs[j] == (data->stencilVals[j] & valMask)) {
326             passMask |= (1 << j);
327          }
328       }
329       break;
330    case PIPE_FUNC_LEQUAL:
331       for (j = 0; j < QUAD_SIZE; j++) {
332          if (refs[j] <= (data->stencilVals[j] & valMask)) {
333             passMask |= (1 << j);
334          }
335       }
336       break;
337    case PIPE_FUNC_GREATER:
338       for (j = 0; j < QUAD_SIZE; j++) {
339          if (refs[j] > (data->stencilVals[j] & valMask)) {
340             passMask |= (1 << j);
341          }
342       }
343       break;
344    case PIPE_FUNC_NOTEQUAL:
345       for (j = 0; j < QUAD_SIZE; j++) {
346          if (refs[j] != (data->stencilVals[j] & valMask)) {
347             passMask |= (1 << j);
348          }
349       }
350       break;
351    case PIPE_FUNC_GEQUAL:
352       for (j = 0; j < QUAD_SIZE; j++) {
353          if (refs[j] >= (data->stencilVals[j] & valMask)) {
354             passMask |= (1 << j);
355          }
356       }
357       break;
358    case PIPE_FUNC_ALWAYS:
359       passMask = MASK_ALL;
360       break;
361    default:
362       assert(0);
363    }
364
365    return passMask;
366 }
367
368
369 /**
370  * Apply the stencil operator to stencil values.
371  *
372  * \param data->stencilVals  the stencil buffer values (read and written)
373  * \param mask  indicates which pixels to update
374  * \param op  the stencil operator (PIPE_STENCIL_OP_x)
375  * \param ref  the stencil reference value
376  * \param wrtMask  writemask controlling which bits are changed in the
377  *                 stencil values
378  */
379 static void
380 apply_stencil_op(struct depth_data *data,
381                  unsigned mask, unsigned op, ubyte ref, ubyte wrtMask)
382 {
383    unsigned j;
384    ubyte newstencil[QUAD_SIZE];
385    ubyte refs[QUAD_SIZE];
386
387    for (j = 0; j < QUAD_SIZE; j++) {
388       newstencil[j] = data->stencilVals[j];
389       if (data->use_shader_stencil_refs)
390          refs[j] = data->shader_stencil_refs[j];
391       else
392          refs[j] = ref;
393    }
394
395    switch (op) {
396    case PIPE_STENCIL_OP_KEEP:
397       /* no-op */
398       break;
399    case PIPE_STENCIL_OP_ZERO:
400       for (j = 0; j < QUAD_SIZE; j++) {
401          if (mask & (1 << j)) {
402             newstencil[j] = 0;
403          }
404       }
405       break;
406    case PIPE_STENCIL_OP_REPLACE:
407       for (j = 0; j < QUAD_SIZE; j++) {
408          if (mask & (1 << j)) {
409             newstencil[j] = refs[j];
410          }
411       }
412       break;
413    case PIPE_STENCIL_OP_INCR:
414       for (j = 0; j < QUAD_SIZE; j++) {
415          if (mask & (1 << j)) {
416             if (data->stencilVals[j] < STENCIL_MAX) {
417                newstencil[j] = data->stencilVals[j] + 1;
418             }
419          }
420       }
421       break;
422    case PIPE_STENCIL_OP_DECR:
423       for (j = 0; j < QUAD_SIZE; j++) {
424          if (mask & (1 << j)) {
425             if (data->stencilVals[j] > 0) {
426                newstencil[j] = data->stencilVals[j] - 1;
427             }
428          }
429       }
430       break;
431    case PIPE_STENCIL_OP_INCR_WRAP:
432       for (j = 0; j < QUAD_SIZE; j++) {
433          if (mask & (1 << j)) {
434             newstencil[j] = data->stencilVals[j] + 1;
435          }
436       }
437       break;
438    case PIPE_STENCIL_OP_DECR_WRAP:
439       for (j = 0; j < QUAD_SIZE; j++) {
440          if (mask & (1 << j)) {
441             newstencil[j] = data->stencilVals[j] - 1;
442          }
443       }
444       break;
445    case PIPE_STENCIL_OP_INVERT:
446       for (j = 0; j < QUAD_SIZE; j++) {
447          if (mask & (1 << j)) {
448             newstencil[j] = ~data->stencilVals[j];
449          }
450       }
451       break;
452    default:
453       assert(0);
454    }
455
456    /*
457     * update the stencil values
458     */
459    if (wrtMask != STENCIL_MAX) {
460       /* apply bit-wise stencil buffer writemask */
461       for (j = 0; j < QUAD_SIZE; j++) {
462          data->stencilVals[j] = (wrtMask & newstencil[j]) | (~wrtMask & data->stencilVals[j]);
463       }
464    }
465    else {
466       for (j = 0; j < QUAD_SIZE; j++) {
467          data->stencilVals[j] = newstencil[j];
468       }
469    }
470 }
471
472    
473
474 /**
475  * To increase efficiency, we should probably have multiple versions
476  * of this function that are specifically for Z16, Z32 and FP Z buffers.
477  * Try to effectively do that with codegen...
478  */
479 static boolean
480 depth_test_quad(struct quad_stage *qs, 
481                 struct depth_data *data,
482                 struct quad_header *quad)
483 {
484    struct softpipe_context *softpipe = qs->softpipe;
485    unsigned zmask = 0;
486    unsigned j;
487
488    switch (softpipe->depth_stencil->depth.func) {
489    case PIPE_FUNC_NEVER:
490       /* zmask = 0 */
491       break;
492    case PIPE_FUNC_LESS:
493       /* Note this is pretty much a single sse or cell instruction.  
494        * Like this:  quad->mask &= (quad->outputs.depth < zzzz);
495        */
496       for (j = 0; j < QUAD_SIZE; j++) {
497          if (data->qzzzz[j] < data->bzzzz[j]) 
498             zmask |= 1 << j;
499       }
500       break;
501    case PIPE_FUNC_EQUAL:
502       for (j = 0; j < QUAD_SIZE; j++) {
503          if (data->qzzzz[j] == data->bzzzz[j]) 
504             zmask |= 1 << j;
505       }
506       break;
507    case PIPE_FUNC_LEQUAL:
508       for (j = 0; j < QUAD_SIZE; j++) {
509          if (data->qzzzz[j] <= data->bzzzz[j]) 
510             zmask |= (1 << j);
511       }
512       break;
513    case PIPE_FUNC_GREATER:
514       for (j = 0; j < QUAD_SIZE; j++) {
515          if (data->qzzzz[j] > data->bzzzz[j]) 
516             zmask |= (1 << j);
517       }
518       break;
519    case PIPE_FUNC_NOTEQUAL:
520       for (j = 0; j < QUAD_SIZE; j++) {
521          if (data->qzzzz[j] != data->bzzzz[j]) 
522             zmask |= (1 << j);
523       }
524       break;
525    case PIPE_FUNC_GEQUAL:
526       for (j = 0; j < QUAD_SIZE; j++) {
527          if (data->qzzzz[j] >= data->bzzzz[j]) 
528             zmask |= (1 << j);
529       }
530       break;
531    case PIPE_FUNC_ALWAYS:
532       zmask = MASK_ALL;
533       break;
534    default:
535       assert(0);
536    }
537
538    quad->inout.mask &= zmask;
539    if (quad->inout.mask == 0)
540       return FALSE;
541
542    /* Update our internal copy only if writemask set.  Even if
543     * depth.writemask is FALSE, may still need to write out buffer
544     * data due to stencil changes.
545     */
546    if (softpipe->depth_stencil->depth.writemask) {
547       for (j = 0; j < QUAD_SIZE; j++) {
548          if (quad->inout.mask & (1 << j)) {
549             data->bzzzz[j] = data->qzzzz[j];
550          }
551       }
552    }
553
554    return TRUE;
555 }
556
557
558
559 /**
560  * Do stencil (and depth) testing.  Stenciling depends on the outcome of
561  * depth testing.
562  */
563 static void
564 depth_stencil_test_quad(struct quad_stage *qs, 
565                         struct depth_data *data,
566                         struct quad_header *quad)
567 {
568    struct softpipe_context *softpipe = qs->softpipe;
569    unsigned func, zFailOp, zPassOp, failOp;
570    ubyte ref, wrtMask, valMask;
571    uint face = quad->input.facing;
572
573    if (!softpipe->depth_stencil->stencil[1].enabled) {
574       /* single-sided stencil test, use front (face=0) state */
575       face = 0;
576    }
577
578    /* 0 = front-face, 1 = back-face */
579    assert(face == 0 || face == 1);
580
581    /* choose front or back face function, operator, etc */
582    /* XXX we could do these initializations once per primitive */
583    func    = softpipe->depth_stencil->stencil[face].func;
584    failOp  = softpipe->depth_stencil->stencil[face].fail_op;
585    zFailOp = softpipe->depth_stencil->stencil[face].zfail_op;
586    zPassOp = softpipe->depth_stencil->stencil[face].zpass_op;
587    ref     = softpipe->stencil_ref.ref_value[face];
588    wrtMask = softpipe->depth_stencil->stencil[face].writemask;
589    valMask = softpipe->depth_stencil->stencil[face].valuemask;
590
591    /* do the stencil test first */
592    {
593       unsigned passMask, failMask;
594       passMask = do_stencil_test(data, func, ref, valMask);
595       failMask = quad->inout.mask & ~passMask;
596       quad->inout.mask &= passMask;
597
598       if (failOp != PIPE_STENCIL_OP_KEEP) {
599          apply_stencil_op(data, failMask, failOp, ref, wrtMask);
600       }
601    }
602
603    if (quad->inout.mask) {
604       /* now the pixels that passed the stencil test are depth tested */
605       if (softpipe->depth_stencil->depth.enabled) {
606          const unsigned origMask = quad->inout.mask;
607
608          depth_test_quad(qs, data, quad);  /* quad->mask is updated */
609
610          /* update stencil buffer values according to z pass/fail result */
611          if (zFailOp != PIPE_STENCIL_OP_KEEP) {
612             const unsigned zFailMask = origMask & ~quad->inout.mask;
613             apply_stencil_op(data, zFailMask, zFailOp, ref, wrtMask);
614          }
615
616          if (zPassOp != PIPE_STENCIL_OP_KEEP) {
617             const unsigned zPassMask = origMask & quad->inout.mask;
618             apply_stencil_op(data, zPassMask, zPassOp, ref, wrtMask);
619          }
620       }
621       else {
622          /* no depth test, apply Zpass operator to stencil buffer values */
623          apply_stencil_op(data, quad->inout.mask, zPassOp, ref, wrtMask);
624       }
625    }
626 }
627
628
629 #define ALPHATEST( FUNC, COMP )                                         \
630    static int                                                           \
631    alpha_test_quads_##FUNC( struct quad_stage *qs,                      \
632                            struct quad_header *quads[],                 \
633                            unsigned nr )                                \
634    {                                                                    \
635       const float ref = qs->softpipe->depth_stencil->alpha.ref_value;   \
636       const uint cbuf = 0; /* only output[0].alpha is tested */         \
637       unsigned pass_nr = 0;                                             \
638       unsigned i;                                                       \
639                                                                         \
640       for (i = 0; i < nr; i++) {                                        \
641          const float *aaaa = quads[i]->output.color[cbuf][3];           \
642          unsigned passMask = 0;                                         \
643                                                                         \
644          if (aaaa[0] COMP ref) passMask |= (1 << 0);                    \
645          if (aaaa[1] COMP ref) passMask |= (1 << 1);                    \
646          if (aaaa[2] COMP ref) passMask |= (1 << 2);                    \
647          if (aaaa[3] COMP ref) passMask |= (1 << 3);                    \
648                                                                         \
649          quads[i]->inout.mask &= passMask;                              \
650                                                                         \
651          if (quads[i]->inout.mask)                                      \
652             quads[pass_nr++] = quads[i];                                \
653       }                                                                 \
654                                                                         \
655       return pass_nr;                                                   \
656    }
657
658
659 ALPHATEST( LESS,     < )
660 ALPHATEST( EQUAL,    == )
661 ALPHATEST( LEQUAL,   <= )
662 ALPHATEST( GREATER,  > )
663 ALPHATEST( NOTEQUAL, != )
664 ALPHATEST( GEQUAL,   >= )
665
666
667 /* XXX: Incorporate into shader using KILP.
668  */
669 static int
670 alpha_test_quads(struct quad_stage *qs, 
671                  struct quad_header *quads[], 
672                  unsigned nr)
673 {
674    switch (qs->softpipe->depth_stencil->alpha.func) {
675    case PIPE_FUNC_LESS:
676       return alpha_test_quads_LESS( qs, quads, nr );
677    case PIPE_FUNC_EQUAL:
678       return alpha_test_quads_EQUAL( qs, quads, nr );
679       break;
680    case PIPE_FUNC_LEQUAL:
681       return alpha_test_quads_LEQUAL( qs, quads, nr );
682    case PIPE_FUNC_GREATER:
683       return alpha_test_quads_GREATER( qs, quads, nr );
684    case PIPE_FUNC_NOTEQUAL:
685       return alpha_test_quads_NOTEQUAL( qs, quads, nr );
686    case PIPE_FUNC_GEQUAL:
687       return alpha_test_quads_GEQUAL( qs, quads, nr );
688    case PIPE_FUNC_ALWAYS:
689       return nr;
690    case PIPE_FUNC_NEVER:
691    default:
692       return 0;
693    }
694 }
695
696
697 static unsigned mask_count[16] = 
698 {
699    0,                           /* 0x0 */
700    1,                           /* 0x1 */
701    1,                           /* 0x2 */
702    2,                           /* 0x3 */
703    1,                           /* 0x4 */
704    2,                           /* 0x5 */
705    2,                           /* 0x6 */
706    3,                           /* 0x7 */
707    1,                           /* 0x8 */
708    2,                           /* 0x9 */
709    2,                           /* 0xa */
710    3,                           /* 0xb */
711    2,                           /* 0xc */
712    3,                           /* 0xd */
713    3,                           /* 0xe */
714    4,                           /* 0xf */
715 };
716
717
718
719 /**
720  * General depth/stencil test function.  Used when there's no fast-path.
721  */
722 static void
723 depth_test_quads_fallback(struct quad_stage *qs, 
724                           struct quad_header *quads[],
725                           unsigned nr)
726 {
727    unsigned i, pass = 0;
728    const struct sp_fragment_shader *fs = qs->softpipe->fs;
729    boolean interp_depth = !fs->info.writes_z;
730    boolean shader_stencil_ref = fs->info.writes_stencil;
731    struct depth_data data;
732
733    data.use_shader_stencil_refs = FALSE;
734
735    if (qs->softpipe->depth_stencil->alpha.enabled) {
736       nr = alpha_test_quads(qs, quads, nr);
737    }
738
739    if (qs->softpipe->framebuffer.zsbuf &&
740          (qs->softpipe->depth_stencil->depth.enabled ||
741           qs->softpipe->depth_stencil->stencil[0].enabled)) {
742
743       data.ps = qs->softpipe->framebuffer.zsbuf;
744       data.format = data.ps->format;
745       data.tile = sp_get_cached_tile(qs->softpipe->zsbuf_cache, 
746                                      quads[0]->input.x0, 
747                                      quads[0]->input.y0);
748
749       for (i = 0; i < nr; i++) {
750          get_depth_stencil_values(&data, quads[i]);
751
752          if (qs->softpipe->depth_stencil->depth.enabled) {
753             if (interp_depth)
754                interpolate_quad_depth(quads[i]);
755
756             convert_quad_depth(&data, quads[i]);
757          }
758
759          if (qs->softpipe->depth_stencil->stencil[0].enabled) {
760             if (shader_stencil_ref)
761                convert_quad_stencil(&data, quads[i]);
762             
763             depth_stencil_test_quad(qs, &data, quads[i]);
764             write_depth_stencil_values(&data, quads[i]);
765          }
766          else {
767             if (!depth_test_quad(qs, &data, quads[i]))
768                continue;
769
770             if (qs->softpipe->depth_stencil->depth.writemask)
771                write_depth_stencil_values(&data, quads[i]);
772          }
773
774          quads[pass++] = quads[i];
775       }
776
777       nr = pass;
778    }
779
780    if (qs->softpipe->active_query_count) {
781       for (i = 0; i < nr; i++) 
782          qs->softpipe->occlusion_count += mask_count[quads[i]->inout.mask];
783    }
784
785    if (nr)
786       qs->next->run(qs->next, quads, nr);
787 }
788
789
790 /**
791  * Special-case Z testing for 16-bit Zbuffer and Z buffer writes enabled.
792  */
793
794 #define NAME depth_interp_z16_less_write
795 #define OPERATOR <
796 #include "sp_quad_depth_test_tmp.h"
797
798 #define NAME depth_interp_z16_equal_write
799 #define OPERATOR ==
800 #include "sp_quad_depth_test_tmp.h"
801
802 #define NAME depth_interp_z16_lequal_write
803 #define OPERATOR <=
804 #include "sp_quad_depth_test_tmp.h"
805
806 #define NAME depth_interp_z16_greater_write
807 #define OPERATOR >
808 #include "sp_quad_depth_test_tmp.h"
809
810 #define NAME depth_interp_z16_notequal_write
811 #define OPERATOR !=
812 #include "sp_quad_depth_test_tmp.h"
813
814 #define NAME depth_interp_z16_gequal_write
815 #define OPERATOR >=
816 #include "sp_quad_depth_test_tmp.h"
817
818 #define NAME depth_interp_z16_always_write
819 #define ALWAYS 1
820 #include "sp_quad_depth_test_tmp.h"
821
822
823
824 static void
825 depth_noop(struct quad_stage *qs, 
826            struct quad_header *quads[],
827            unsigned nr)
828 {
829    qs->next->run(qs->next, quads, nr);
830 }
831
832
833
834 static void
835 choose_depth_test(struct quad_stage *qs, 
836                   struct quad_header *quads[],
837                   unsigned nr)
838 {
839    boolean interp_depth = !qs->softpipe->fs->info.writes_z;
840
841    boolean alpha = qs->softpipe->depth_stencil->alpha.enabled;
842
843    boolean depth = qs->softpipe->depth_stencil->depth.enabled;
844
845    unsigned depthfunc = qs->softpipe->depth_stencil->depth.func;
846
847    boolean stencil = qs->softpipe->depth_stencil->stencil[0].enabled;
848
849    boolean depthwrite = qs->softpipe->depth_stencil->depth.writemask;
850
851    boolean occlusion = qs->softpipe->active_query_count;
852
853    if(!qs->softpipe->framebuffer.zsbuf)
854       depth = depthwrite = stencil = FALSE;
855
856    /* default */
857    qs->run = depth_test_quads_fallback;
858
859    /* look for special cases */
860    if (!alpha &&
861        !depth &&
862        !occlusion &&
863        !stencil) {
864       qs->run = depth_noop;
865    }
866    else if (!alpha && 
867             interp_depth && 
868             depth && 
869             depthwrite && 
870             !occlusion &&
871             !stencil) 
872    {
873       if (qs->softpipe->framebuffer.zsbuf->format == PIPE_FORMAT_Z16_UNORM) {
874          switch (depthfunc) {
875          case PIPE_FUNC_NEVER:
876             qs->run = depth_test_quads_fallback;
877             break;
878          case PIPE_FUNC_LESS:
879             qs->run = depth_interp_z16_less_write;
880             break;
881          case PIPE_FUNC_EQUAL:
882             qs->run = depth_interp_z16_equal_write;
883             break;
884          case PIPE_FUNC_LEQUAL:
885             qs->run = depth_interp_z16_lequal_write;
886             break;
887          case PIPE_FUNC_GREATER:
888             qs->run = depth_interp_z16_greater_write;
889             break;
890          case PIPE_FUNC_NOTEQUAL:
891             qs->run = depth_interp_z16_notequal_write;
892             break;
893          case PIPE_FUNC_GEQUAL:
894             qs->run = depth_interp_z16_gequal_write;
895             break;
896          case PIPE_FUNC_ALWAYS:
897             qs->run = depth_interp_z16_always_write;
898             break;
899          default:
900             qs->run = depth_test_quads_fallback;
901             break;
902          }
903       }
904    }
905
906    /* next quad/fragment stage */
907    qs->run( qs, quads, nr );
908 }
909
910
911
912 static void
913 depth_test_begin(struct quad_stage *qs)
914 {
915    qs->run = choose_depth_test;
916    qs->next->begin(qs->next);
917 }
918
919
920 static void
921 depth_test_destroy(struct quad_stage *qs)
922 {
923    FREE( qs );
924 }
925
926
927 struct quad_stage *
928 sp_quad_depth_test_stage(struct softpipe_context *softpipe)
929 {
930    struct quad_stage *stage = CALLOC_STRUCT(quad_stage);
931
932    stage->softpipe = softpipe;
933    stage->begin = depth_test_begin;
934    stage->run = choose_depth_test;
935    stage->destroy = depth_test_destroy;
936
937    return stage;
938 }