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