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