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