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