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