forked from GameTechDev/MaskedOcclusionCulling
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMaskedOcclusionCullingCommon.inl
1833 lines (1552 loc) · 82 KB
/
MaskedOcclusionCullingCommon.inl
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
/*
* Copyright 2017 Intel Corporation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http ://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and limitations under the License.
*/
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Common SIMD math utility functions
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
template<typename T> FORCE_INLINE T max(const T &a, const T &b) { return a > b ? a : b; }
template<typename T> FORCE_INLINE T min(const T &a, const T &b) { return a < b ? a : b; }
template<typename T, typename Y> FORCE_INLINE T simd_cast(Y A);
template<> FORCE_INLINE __m128 simd_cast<__m128>(float A) { return _mm_set1_ps(A); }
template<> FORCE_INLINE __m128 simd_cast<__m128>(__m128i A) { return _mm_castsi128_ps(A); }
template<> FORCE_INLINE __m128 simd_cast<__m128>(__m128 A) { return A; }
template<> FORCE_INLINE __m128i simd_cast<__m128i>(int A) { return _mm_set1_epi32(A); }
template<> FORCE_INLINE __m128i simd_cast<__m128i>(__m128 A) { return _mm_castps_si128(A); }
template<> FORCE_INLINE __m128i simd_cast<__m128i>(__m128i A) { return A; }
template<> FORCE_INLINE __m256 simd_cast<__m256>(float A) { return _mm256_set1_ps(A); }
template<> FORCE_INLINE __m256 simd_cast<__m256>(__m256i A) { return _mm256_castsi256_ps(A); }
template<> FORCE_INLINE __m256 simd_cast<__m256>(__m256 A) { return A; }
template<> FORCE_INLINE __m256i simd_cast<__m256i>(int A) { return _mm256_set1_epi32(A); }
template<> FORCE_INLINE __m256i simd_cast<__m256i>(__m256 A) { return _mm256_castps_si256(A); }
template<> FORCE_INLINE __m256i simd_cast<__m256i>(__m256i A) { return A; }
// Unary operators
static FORCE_INLINE __m128 operator-(const __m128 &A) { return _mm_xor_ps(A, _mm_set1_ps(-0.0f)); }
static FORCE_INLINE __m128i operator-(const __m128i &A) { return _mm_sub_epi32(_mm_set1_epi32(0), A); }
static FORCE_INLINE __m256 operator-(const __m256 &A) { return _mm256_xor_ps(A, _mm256_set1_ps(-0.0f)); }
static FORCE_INLINE __m256i operator-(const __m256i &A) { return _mm256_sub_epi32(_mm256_set1_epi32(0), A); }
static FORCE_INLINE __m128 operator~(const __m128 &A) { return _mm_xor_ps(A, _mm_castsi128_ps(_mm_set1_epi32(~0))); }
static FORCE_INLINE __m128i operator~(const __m128i &A) { return _mm_xor_si128(A, _mm_set1_epi32(~0)); }
static FORCE_INLINE __m256 operator~(const __m256 &A) { return _mm256_xor_ps(A, _mm256_castsi256_ps(_mm256_set1_epi32(~0))); }
static FORCE_INLINE __m256i operator~(const __m256i &A) { return _mm256_xor_si256(A, _mm256_set1_epi32(~0)); }
static FORCE_INLINE __m256 abs(const __m256 &a) { return _mm256_and_ps(a, _mm256_castsi256_ps(_mm256_set1_epi32(0x7FFFFFFF))); }
static FORCE_INLINE __m128 abs(const __m128 &a) { return _mm_and_ps(a, _mm_castsi128_ps(_mm_set1_epi32(0x7FFFFFFF))); }
// Binary operators
#define SIMD_BINARY_OP(SIMD_TYPE, BASE_TYPE, prefix, postfix, func, op) \
static FORCE_INLINE SIMD_TYPE operator##op(const SIMD_TYPE &A, const SIMD_TYPE &B) { return _##prefix##_##func##_##postfix(A, B); } \
static FORCE_INLINE SIMD_TYPE operator##op(const SIMD_TYPE &A, const BASE_TYPE B) { return _##prefix##_##func##_##postfix(A, simd_cast<SIMD_TYPE>(B)); } \
static FORCE_INLINE SIMD_TYPE operator##op(const BASE_TYPE &A, const SIMD_TYPE &B) { return _##prefix##_##func##_##postfix(simd_cast<SIMD_TYPE>(A), B); } \
static FORCE_INLINE SIMD_TYPE &operator##op##=(SIMD_TYPE &A, const SIMD_TYPE &B) { return (A = _##prefix##_##func##_##postfix(A, B)); } \
static FORCE_INLINE SIMD_TYPE &operator##op##=(SIMD_TYPE &A, const BASE_TYPE B) { return (A = _##prefix##_##func##_##postfix(A, simd_cast<SIMD_TYPE>(B))); }
#define ALL_SIMD_BINARY_OP(type_suffix, base_type, postfix, func, op) \
SIMD_BINARY_OP(__m128##type_suffix, base_type, mm, postfix, func, op) \
SIMD_BINARY_OP(__m256##type_suffix, base_type, mm256, postfix, func, op)
ALL_SIMD_BINARY_OP(, float, ps, add, +)
ALL_SIMD_BINARY_OP(, float, ps, sub, -)
ALL_SIMD_BINARY_OP(, float, ps, mul, *)
ALL_SIMD_BINARY_OP(, float, ps, div, / )
ALL_SIMD_BINARY_OP(i, int, epi32, add, +)
ALL_SIMD_BINARY_OP(i, int, epi32, sub, -)
ALL_SIMD_BINARY_OP(, float, ps, and, &)
ALL_SIMD_BINARY_OP(, float, ps, or , | )
ALL_SIMD_BINARY_OP(, float, ps, xor, ^)
SIMD_BINARY_OP(__m128i, int, mm, si128, and, &)
SIMD_BINARY_OP(__m128i, int, mm, si128, or , | )
SIMD_BINARY_OP(__m128i, int, mm, si128, xor, ^)
SIMD_BINARY_OP(__m256i, int, mm256, si256, and, &)
SIMD_BINARY_OP(__m256i, int, mm256, si256, or , | )
SIMD_BINARY_OP(__m256i, int, mm256, si256, xor, ^)
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Common defines and constants
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#define SIMD_ALL_LANES_MASK ((1 << SIMD_LANES) - 1)
// Tile dimensions are 32xN pixels. These values are not tweakable and the code must also be modified
// to support different tile sizes as it is tightly coupled with the SSE/AVX register size
#define TILE_WIDTH_SHIFT 5
#define TILE_WIDTH (1 << TILE_WIDTH_SHIFT)
#define TILE_HEIGHT (1 << TILE_HEIGHT_SHIFT)
// Sub-tiles (used for updating the masked HiZ buffer) are 8x4 tiles, so there are 4x2 sub-tiles in a tile
#define SUB_TILE_WIDTH 8
#define SUB_TILE_HEIGHT 4
// The number of fixed point bits used to represent vertex coordinates / edge slopes.
#if PRECISE_COVERAGE != 0
#define FP_BITS 8
#define FP_HALF_PIXEL (1 << (FP_BITS - 1))
#define FP_INV (1.0f / (float)(1 << FP_BITS))
#else
// Note that too low precision, without precise coverage, may cause overshoots / false coverage during rasterization.
#define FP_BITS 16
#endif
// Tile dimensions in fixed point coordinates
#define FP_TILE_HEIGHT_SHIFT (FP_BITS + TILE_HEIGHT_SHIFT)
#define FP_TILE_HEIGHT (1 << FP_TILE_HEIGHT_SHIFT)
// Maximum number of triangles that may be generated during clipping. We process SIMD_LANES triangles at a time and
// clip against 5 planes, so the max should be 5*8 = 40 (we immediately draw the first clipped triangle).
// This number must be a power of two.
#define MAX_CLIPPED 64
#define MAX_CLIPPED_WRAP (MAX_CLIPPED - 1)
// Size of guard band in pixels. Clipping doesn't seem to be very expensive so we use a small guard band
// to improve rasterization performance. It's not recommended to set the guard band to zero, as this may
// cause leakage along the screen border due to precision/rounding.
#define GUARD_BAND_PIXEL_SIZE 1.0f
// We classify triangles as big if the bounding box is wider than this given threshold and use a tighter
// but slightly more expensive traversal algorithm. This improves performance greatly for sliver triangles
#define BIG_TRIANGLE 3
// Only gather statistics if enabled.
#if ENABLE_STATS != 0
#define STATS_ADD(var, val) (var) += (val)
#else
#define STATS_ADD(var, val)
#endif
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// SIMD common defines (constant values)
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#define SIMD_BITS_ONE _mmw_set1_epi32(~0)
#define SIMD_BITS_ZERO _mmw_setzero_epi32()
#define SIMD_TILE_WIDTH _mmw_set1_epi32(TILE_WIDTH)
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Private class containing the implementation
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
class MaskedOcclusionCullingPrivate : public MaskedOcclusionCulling
{
public:
struct ZTile
{
__mw mZMin[2];
__mwi mMask;
};
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Member variables
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
__mw mHalfWidth;
__mw mHalfHeight;
__mw mCenterX;
__mw mCenterY;
__m128 mCSFrustumPlanes[5];
__m128 mIHalfSize;
__m128 mICenter;
__m128i mIScreenSize;
float mNearDist;
int mWidth;
int mHeight;
int mTilesWidth;
int mTilesHeight;
ZTile *mMaskedHiZBuffer;
ScissorRect mFullscreenScissor;
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Constructors and state handling
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
MaskedOcclusionCullingPrivate(pfnAlignedAlloc memAlloc, pfnAlignedFree memFree) : mFullscreenScissor(0, 0, 0, 0)
{
mMaskedHiZBuffer = nullptr;
mAlignedAllocCallback = memAlloc;
mAlignedFreeCallback = memFree;
SetNearClipPlane( 0.0f );
mCSFrustumPlanes[0] = _mm_setr_ps(0.0f, 0.0f, 1.0f, 0.0f);
mCSFrustumPlanes[1] = _mm_setr_ps(1.0f, 0.0f, 1.0f, 0.0f);
mCSFrustumPlanes[2] = _mm_setr_ps(-1.0f, 0.0f, 1.0f, 0.0f);
mCSFrustumPlanes[3] = _mm_setr_ps(0.0f, 1.0f, 1.0f, 0.0f);
mCSFrustumPlanes[4] = _mm_setr_ps(0.0f, -1.0f, 1.0f, 0.0f);
memset(&mStats, 0, sizeof(OcclusionCullingStatistics));
SetResolution( 0, 0 );
}
~MaskedOcclusionCullingPrivate() override
{
if (mMaskedHiZBuffer != nullptr)
mAlignedFreeCallback(mMaskedHiZBuffer);
mMaskedHiZBuffer = nullptr;
}
void SetResolution(unsigned int width, unsigned int height) override
{
// Resolution must be a multiple of the subtile size
assert(width % SUB_TILE_WIDTH == 0 && height % SUB_TILE_HEIGHT == 0);
// Test if combination of resolution & SLOPE_FP_BITS bits may cause 32-bit overflow
#if PRECISE_COVERAGE == 0
assert(7 * width < (1 << (31 - FP_BITS)));
#endif
// Delete current masked hierarchical Z buffer
if (mMaskedHiZBuffer != nullptr)
mAlignedFreeCallback(mMaskedHiZBuffer);
mMaskedHiZBuffer = nullptr;
// Setup various resolution dependent constant values
mWidth = (int)width;
mHeight = (int)height;
mTilesWidth = (int)(width + TILE_WIDTH - 1) >> TILE_WIDTH_SHIFT;
mTilesHeight = (int)(height + TILE_HEIGHT - 1) >> TILE_HEIGHT_SHIFT;
mCenterX = _mmw_set1_ps((float)mWidth * 0.5f);
mCenterY = _mmw_set1_ps((float)mHeight * 0.5f);
mICenter = _mm_setr_ps((float)mWidth * 0.5f, (float)mWidth * 0.5f, (float)mHeight * 0.5f, (float)mHeight * 0.5f);
mHalfWidth = _mmw_set1_ps((float)mWidth * 0.5f);
#if USE_D3D != 0
mHalfHeight = _mmw_set1_ps((float)-mHeight * 0.5f);
mIHalfSize = _mm_setr_ps((float)mWidth * 0.5f, (float)mWidth * 0.5f, (float)-mHeight * 0.5f, (float)-mHeight * 0.5f);
#else
mHalfHeight = _mmw_set1_ps((float)mHeight * 0.5f);
mIHalfSize = _mm_setr_ps((float)mWidth * 0.5f, (float)mWidth * 0.5f, (float)mHeight * 0.5f, (float)mHeight * 0.5f);
#endif
mIScreenSize = _mm_setr_epi32(mWidth - 1, mWidth - 1, mHeight - 1, mHeight - 1);
// Setup a full screen scissor rectangle
mFullscreenScissor.mMinX = 0;
mFullscreenScissor.mMinY = 0;
mFullscreenScissor.mMaxX = mTilesWidth << TILE_WIDTH_SHIFT;
mFullscreenScissor.mMaxY = mTilesHeight << TILE_HEIGHT_SHIFT;
// Adjust clip planes to include a small guard band to avoid clipping leaks
float guardBandWidth = (2.0f / (float)mWidth) * GUARD_BAND_PIXEL_SIZE;
float guardBandHeight = (2.0f / (float)mHeight) * GUARD_BAND_PIXEL_SIZE;
mCSFrustumPlanes[1] = _mm_setr_ps(1.0f - guardBandWidth, 0.0f, 1.0f, 0.0f);
mCSFrustumPlanes[2] = _mm_setr_ps(-1.0f + guardBandWidth, 0.0f, 1.0f, 0.0f);
mCSFrustumPlanes[3] = _mm_setr_ps(0.0f, 1.0f - guardBandHeight, 1.0f, 0.0f);
mCSFrustumPlanes[4] = _mm_setr_ps(0.0f, -1.0f + guardBandHeight, 1.0f, 0.0f);
// Allocate masked hierarchical Z buffer (if zero size leave at nullptr)
if( mTilesWidth * mTilesHeight > 0 )
mMaskedHiZBuffer = (ZTile *)mAlignedAllocCallback(32, sizeof(ZTile) * mTilesWidth * mTilesHeight);
}
void GetResolution(unsigned int &width, unsigned int &height) override
{
width = mWidth;
height = mHeight;
}
void ComputeBinWidthHeight( unsigned int nBinsW, unsigned int nBinsH, unsigned int & outBinWidth, unsigned int & outBinHeight ) override
{
outBinWidth = (mWidth / nBinsW) - ((mWidth / nBinsW) % TILE_WIDTH);
outBinHeight = (mHeight / nBinsH) - ((mHeight / nBinsH) % TILE_HEIGHT);
}
void SetNearClipPlane(float nearDist) override
{
// Setup the near frustum plane
mNearDist = nearDist;
mCSFrustumPlanes[0] = _mm_setr_ps(0.0f, 0.0f, 1.0f, -nearDist);
}
float GetNearClipPlane() override
{
return mNearDist;
}
void ClearBuffer() override
{
assert(mMaskedHiZBuffer != nullptr);
// Iterate through all depth tiles and clear to default values
for (int i = 0; i < mTilesWidth * mTilesHeight; i++)
{
mMaskedHiZBuffer[i].mMask = _mmw_setzero_epi32();
// Clear z0 to beyond infinity to ensure we never merge with clear data
mMaskedHiZBuffer[i].mZMin[0] = _mmw_set1_ps(-1.0f);
#if QUICK_MASK != 0
// Clear z1 to nearest depth value as it is pushed back on each update
mMaskedHiZBuffer[i].mZMin[1] = _mmw_set1_ps(FLT_MAX);
#else
mMaskedHiZBuffer[i].mZMin[1] = _mmw_setzero_ps();
#endif
}
#if ENABLE_STATS != 0
memset(&mStats, 0, sizeof(OcclusionCullingStatistics));
#endif
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Polygon clipping functions
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
FORCE_INLINE int ClipPolygon(__m128 *outVtx, __m128 *inVtx, const __m128 &plane, int n) const
{
__m128 p0 = inVtx[n - 1];
__m128 dist0 = _mmx_dp4_ps(p0, plane);
// Loop over all polygon edges and compute intersection with clip plane (if any)
int nout = 0;
for (int k = 0; k < n; k++)
{
__m128 p1 = inVtx[k];
__m128 dist1 = _mmx_dp4_ps(p1, plane);
int dist0Neg = _mm_movemask_ps(dist0);
if (!dist0Neg) // dist0 > 0.0f
outVtx[nout++] = p0;
// Edge intersects the clip plane if dist0 and dist1 have opposing signs
if (_mm_movemask_ps(dist0 ^ dist1))
{
// Always clip from the positive side to avoid T-junctions
if (!dist0Neg)
{
__m128 t = dist0 / (dist0 - dist1);
outVtx[nout++] = _mmx_fmadd_ps(p1 - p0, t, p0);
}
else
{
__m128 t = dist1 / (dist1 - dist0);
outVtx[nout++] = _mmx_fmadd_ps(p0 - p1, t, p1);
}
}
dist0 = dist1;
p0 = p1;
}
return nout;
}
template<ClipPlanes CLIP_PLANE> void TestClipPlane(__mw *vtxX, __mw *vtxY, __mw *vtxW, unsigned int &straddleMask, unsigned int &triMask, ClipPlanes clipPlaneMask)
{
straddleMask = 0;
// Skip masked clip planes
if (!(clipPlaneMask & CLIP_PLANE))
return;
// Evaluate all 3 vertices against the frustum plane
__mw planeDp[3];
for (int i = 0; i < 3; ++i)
{
switch (CLIP_PLANE)
{
case ClipPlanes::CLIP_PLANE_LEFT: planeDp[i] = vtxW[i] + vtxX[i]; break;
case ClipPlanes::CLIP_PLANE_RIGHT: planeDp[i] = vtxW[i] - vtxX[i]; break;
case ClipPlanes::CLIP_PLANE_BOTTOM: planeDp[i] = vtxW[i] + vtxY[i]; break;
case ClipPlanes::CLIP_PLANE_TOP: planeDp[i] = vtxW[i] - vtxY[i]; break;
case ClipPlanes::CLIP_PLANE_NEAR: planeDp[i] = vtxW[i] - _mmw_set1_ps(mNearDist); break;
}
}
// Look at FP sign and determine if tri is inside, outside or straddles the frustum plane
__mw inside = _mmw_andnot_ps(planeDp[0], _mmw_andnot_ps(planeDp[1], ~planeDp[2]));
__mw outside = planeDp[0] & planeDp[1] & planeDp[2];
unsigned int inMask = (unsigned int)_mmw_movemask_ps(inside);
unsigned int outMask = (unsigned int)_mmw_movemask_ps(outside);
straddleMask = (~outMask) & (~inMask);
triMask &= ~outMask;
}
FORCE_INLINE void ClipTriangleAndAddToBuffer(__mw *vtxX, __mw *vtxY, __mw *vtxW, __m128 *clippedTrisBuffer, int &clipWriteIdx, unsigned int &triMask, unsigned int triClipMask, ClipPlanes clipPlaneMask)
{
if (!triClipMask)
return;
// Inside test all 3 triangle vertices against all active frustum planes
unsigned int straddleMask[5];
TestClipPlane<ClipPlanes::CLIP_PLANE_NEAR>(vtxX, vtxY, vtxW, straddleMask[0], triMask, clipPlaneMask);
TestClipPlane<ClipPlanes::CLIP_PLANE_LEFT>(vtxX, vtxY, vtxW, straddleMask[1], triMask, clipPlaneMask);
TestClipPlane<ClipPlanes::CLIP_PLANE_RIGHT>(vtxX, vtxY, vtxW, straddleMask[2], triMask, clipPlaneMask);
TestClipPlane<ClipPlanes::CLIP_PLANE_BOTTOM>(vtxX, vtxY, vtxW, straddleMask[3], triMask, clipPlaneMask);
TestClipPlane<ClipPlanes::CLIP_PLANE_TOP>(vtxX, vtxY, vtxW, straddleMask[4], triMask, clipPlaneMask);
// Clip triangle against straddling planes and add to the clipped triangle buffer
__m128 vtxBuf[2][8];
unsigned int clipMask = (straddleMask[0] | straddleMask[1] | straddleMask[2] | straddleMask[3] | straddleMask[4]) & (triClipMask & triMask);
while (clipMask)
{
// Find and setup next triangle to clip
unsigned int triIdx = find_clear_lsb(&clipMask);
unsigned int triBit = (1U << triIdx);
assert(triIdx < SIMD_LANES);
int bufIdx = 0;
int nClippedVerts = 3;
for (int i = 0; i < 3; i++)
vtxBuf[0][i] = _mm_setr_ps(vtxX[i].mw_f32[triIdx], vtxY[i].mw_f32[triIdx], vtxW[i].mw_f32[triIdx], 1.0f);
// Clip triangle with straddling planes.
for (int i = 0; i < 5; ++i)
{
if ((straddleMask[i] & triBit) && (clipPlaneMask & (1 << i)))
{
nClippedVerts = ClipPolygon(vtxBuf[bufIdx ^ 1], vtxBuf[bufIdx], mCSFrustumPlanes[i], nClippedVerts);
bufIdx ^= 1;
}
}
if (nClippedVerts >= 3)
{
// Write the first triangle back into the list of currently processed triangles
for (int i = 0; i < 3; i++)
{
vtxX[i].mw_f32[triIdx] = vtxBuf[bufIdx][i].m128_f32[0];
vtxY[i].mw_f32[triIdx] = vtxBuf[bufIdx][i].m128_f32[1];
vtxW[i].mw_f32[triIdx] = vtxBuf[bufIdx][i].m128_f32[2];
}
// Write the remaining triangles into the clip buffer and process them next loop iteration
for (int i = 2; i < nClippedVerts - 1; i++)
{
clippedTrisBuffer[clipWriteIdx * 3 + 0] = vtxBuf[bufIdx][0];
clippedTrisBuffer[clipWriteIdx * 3 + 1] = vtxBuf[bufIdx][i];
clippedTrisBuffer[clipWriteIdx * 3 + 2] = vtxBuf[bufIdx][i + 1];
clipWriteIdx = (clipWriteIdx + 1) & (MAX_CLIPPED - 1);
}
}
else // Kill triangles that was removed by clipping
triMask &= ~triBit;
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Vertex transform & projection
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
FORCE_INLINE void TransformVerts(__mw *vtxX, __mw *vtxY, __mw *vtxW, const float *modelToClipMatrix)
{
if (modelToClipMatrix != nullptr)
{
for (int i = 0; i < 3; ++i)
{
__mw tmpX, tmpY, tmpW;
tmpX = _mmw_fmadd_ps(vtxX[i], _mmw_set1_ps(modelToClipMatrix[0]), _mmw_fmadd_ps(vtxY[i], _mmw_set1_ps(modelToClipMatrix[4]), _mmw_fmadd_ps(vtxW[i], _mmw_set1_ps(modelToClipMatrix[8]), _mmw_set1_ps(modelToClipMatrix[12]))));
tmpY = _mmw_fmadd_ps(vtxX[i], _mmw_set1_ps(modelToClipMatrix[1]), _mmw_fmadd_ps(vtxY[i], _mmw_set1_ps(modelToClipMatrix[5]), _mmw_fmadd_ps(vtxW[i], _mmw_set1_ps(modelToClipMatrix[9]), _mmw_set1_ps(modelToClipMatrix[13]))));
tmpW = _mmw_fmadd_ps(vtxX[i], _mmw_set1_ps(modelToClipMatrix[3]), _mmw_fmadd_ps(vtxY[i], _mmw_set1_ps(modelToClipMatrix[7]), _mmw_fmadd_ps(vtxW[i], _mmw_set1_ps(modelToClipMatrix[11]), _mmw_set1_ps(modelToClipMatrix[15]))));
vtxX[i] = tmpX; vtxY[i] = tmpY; vtxW[i] = tmpW;
}
}
}
#if PRECISE_COVERAGE != 0
FORCE_INLINE void ProjectVertices(__mwi *ipVtxX, __mwi *ipVtxY, __mw *pVtxX, __mw *pVtxY, __mw *pVtxZ, const __mw *vtxX, const __mw *vtxY, const __mw *vtxW)
{
#if USE_D3D != 0
static const int vertexOrder[] = {2, 1, 0};
#else
static const int vertexOrder[] = {0, 1, 2};
#endif
// Project vertices and transform to screen space. Snap to sub-pixel coordinates with FP_BITS precision.
for (int i = 0; i < 3; i++)
{
int idx = vertexOrder[i];
__mw rcpW = _mmw_set1_ps(1.0f) / vtxW[i];
ipVtxX[idx] = _mmw_cvtps_epi32(_mmw_fmadd_ps(vtxX[i] * mHalfWidth, rcpW, mCenterX) * _mmw_set1_ps(float(1 << FP_BITS)));
ipVtxY[idx] = _mmw_cvtps_epi32(_mmw_fmadd_ps(vtxY[i] * mHalfHeight, rcpW, mCenterY) * _mmw_set1_ps(float(1 << FP_BITS)));
pVtxX[idx] = _mmw_cvtepi32_ps(ipVtxX[idx]) * _mmw_set1_ps(FP_INV);
pVtxY[idx] = _mmw_cvtepi32_ps(ipVtxY[idx]) * _mmw_set1_ps(FP_INV);
pVtxZ[idx] = rcpW;
}
}
#else
FORCE_INLINE void ProjectVertices(__mw *pVtxX, __mw *pVtxY, __mw *pVtxZ, const __mw *vtxX, const __mw *vtxY, const __mw *vtxW)
{
#if USE_D3D != 0
static const int vertexOrder[] = {2, 1, 0};
#else
static const int vertexOrder[] = {0, 1, 2};
#endif
// Project vertices and transform to screen space. Round to nearest integer pixel coordinate
for (int i = 0; i < 3; i++)
{
int idx = vertexOrder[i];
__mw rcpW = _mmw_set1_ps(1.0f) / vtxW[i];
// The rounding modes are set to match HW rasterization with OpenGL. In practice our samples are placed
// in the (1,0) corner of each pixel, while HW rasterizer uses (0.5, 0.5). We get (1,0) because of the
// floor used when interpolating along triangle edges. The rounding modes match an offset of (0.5, -0.5)
pVtxX[idx] = _mmw_ceil_ps(_mmw_fmadd_ps(vtxX[i] * mHalfWidth, rcpW, mCenterX));
pVtxY[idx] = _mmw_floor_ps(_mmw_fmadd_ps(vtxY[i] * mHalfHeight, rcpW, mCenterY));
pVtxZ[idx] = rcpW;
}
}
#endif
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Common SSE/AVX input assembly functions, note that there are specialized gathers for the general case in the SSE/AVX specific files
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
template<int N> FORCE_INLINE void VtxFetch4(__mw *v, const unsigned int *inTrisPtr, int triVtx, const float *inVtx, int numLanes)
{
// Fetch 4 vectors (matching 1 sse part of the SIMD register), and continue to the next
const int ssePart = (SIMD_LANES / 4) - N;
for (int k = 0; k < 4; k++)
{
int lane = 4 * ssePart + k;
if (numLanes > lane)
v[k] = _mmw_insertf32x4_ps(v[k], _mm_loadu_ps(&inVtx[inTrisPtr[lane * 3 + triVtx] << 2]), ssePart);
}
VtxFetch4<N - 1>(v, inTrisPtr, triVtx, inVtx, numLanes);
}
template<> FORCE_INLINE void VtxFetch4<0>(__mw *v, const unsigned int *inTrisPtr, int triVtx, const float *inVtx, int numLanes) {}
FORCE_INLINE void GatherVerticesFast(__mw *vtxX, __mw *vtxY, __mw *vtxW, const float *inVtx, const unsigned int *inTrisPtr, int numLanes)
{
// This function assumes that the vertex layout is four packed x, y, z, w-values.
// Since the layout is known we can get some additional performance by using a
// more optimized gather strategy.
assert(numLanes >= 1);
// Gather vertices
__mw v[4], swz[4];
for (int i = 0; i < 3; i++)
{
// Load 4 (x,y,z,w) vectors per SSE part of the SIMD register (so 4 vectors for SSE, 8 vectors for AVX)
// this fetch uses templates to unroll the loop
VtxFetch4<SIMD_LANES / 4>(v, inTrisPtr, i, inVtx, numLanes);
// Transpose each individual SSE part of the SSE/AVX register (similar to _MM_TRANSPOSE4_PS)
swz[0] = _mmw_shuffle_ps(v[0], v[1], 0x44);
swz[2] = _mmw_shuffle_ps(v[0], v[1], 0xEE);
swz[1] = _mmw_shuffle_ps(v[2], v[3], 0x44);
swz[3] = _mmw_shuffle_ps(v[2], v[3], 0xEE);
vtxX[i] = _mmw_shuffle_ps(swz[0], swz[1], 0x88);
vtxY[i] = _mmw_shuffle_ps(swz[0], swz[1], 0xDD);
vtxW[i] = _mmw_shuffle_ps(swz[2], swz[3], 0xDD);
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Rasterization functions
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
FORCE_INLINE void ComputeBoundingBox(__mwi &bbminX, __mwi &bbminY, __mwi &bbmaxX, __mwi &bbmaxY, const __mw *vX, const __mw *vY, const ScissorRect *scissor)
{
static const __mwi SIMD_PAD_W_MASK = _mmw_set1_epi32(~(TILE_WIDTH - 1));
static const __mwi SIMD_PAD_H_MASK = _mmw_set1_epi32(~(TILE_HEIGHT - 1));
// Find Min/Max vertices
bbminX = _mmw_cvttps_epi32(_mmw_min_ps(vX[0], _mmw_min_ps(vX[1], vX[2])));
bbminY = _mmw_cvttps_epi32(_mmw_min_ps(vY[0], _mmw_min_ps(vY[1], vY[2])));
bbmaxX = _mmw_cvttps_epi32(_mmw_max_ps(vX[0], _mmw_max_ps(vX[1], vX[2])));
bbmaxY = _mmw_cvttps_epi32(_mmw_max_ps(vY[0], _mmw_max_ps(vY[1], vY[2])));
// Clamp to tile boundaries
bbminX = _mmw_max_epi32(bbminX & SIMD_PAD_W_MASK, _mmw_set1_epi32(scissor->mMinX));
bbmaxX = _mmw_min_epi32((bbmaxX + TILE_WIDTH) & SIMD_PAD_W_MASK, _mmw_set1_epi32(scissor->mMaxX));
bbminY = _mmw_max_epi32(bbminY & SIMD_PAD_H_MASK, _mmw_set1_epi32(scissor->mMinY));
bbmaxY = _mmw_min_epi32((bbmaxY + TILE_HEIGHT) & SIMD_PAD_H_MASK, _mmw_set1_epi32(scissor->mMaxY));
}
template<typename T> FORCE_INLINE void SortVertices(T *vX, T *vY)
{
// Rotate the triangle in the winding order until v0 is the vertex with lowest Y value
for (int i = 0; i < 2; i++)
{
T ey1 = vY[1] - vY[0];
T ey2 = vY[2] - vY[0];
__mw swapMask = (simd_cast<__mw>(ey1 | ey2) | simd_cast<__mw>(_mmw_cmpeq_epi32(simd_cast<__mwi>(ey2), SIMD_BITS_ZERO)));
T sX, sY;
sX = simd_cast<T>(_mmw_blendv_ps(simd_cast<__mw>(vX[2]), simd_cast<__mw>(vX[0]), swapMask));
vX[0] = simd_cast<T>(_mmw_blendv_ps(simd_cast<__mw>(vX[0]), simd_cast<__mw>(vX[1]), swapMask));
vX[1] = simd_cast<T>(_mmw_blendv_ps(simd_cast<__mw>(vX[1]), simd_cast<__mw>(vX[2]), swapMask));
vX[2] = sX;
sY = simd_cast<T>(_mmw_blendv_ps(simd_cast<__mw>(vY[2]), simd_cast<__mw>(vY[0]), swapMask));
vY[0] = simd_cast<T>(_mmw_blendv_ps(simd_cast<__mw>(vY[0]), simd_cast<__mw>(vY[1]), swapMask));
vY[1] = simd_cast<T>(_mmw_blendv_ps(simd_cast<__mw>(vY[1]), simd_cast<__mw>(vY[2]), swapMask));
vY[2] = sY;
}
}
FORCE_INLINE void ComputeDepthPlane(const __mw *pVtxX, const __mw *pVtxY, const __mw *pVtxZ, __mw &zPixelDx, __mw &zPixelDy) const
{
// Setup z(x,y) = z0 + dx*x + dy*y screen space depth plane equation
__mw x2 = pVtxX[2] - pVtxX[0];
__mw x1 = pVtxX[1] - pVtxX[0];
__mw y1 = pVtxY[1] - pVtxY[0];
__mw y2 = pVtxY[2] - pVtxY[0];
__mw z1 = pVtxZ[1] - pVtxZ[0];
__mw z2 = pVtxZ[2] - pVtxZ[0];
__mw d = _mmw_set1_ps(1.0f) / _mmw_fmsub_ps(x1, y2, y1 * x2);
zPixelDx = _mmw_fmsub_ps(z1, y2, y1 * z2) * d;
zPixelDy = _mmw_fmsub_ps(x1, z2, z1 * x2) * d;
}
FORCE_INLINE void UpdateTileQuick(int tileIdx, const __mwi &coverage, const __mw &zTriv)
{
// Update heuristic used in the paper "Masked Software Occlusion Culling",
// good balance between performance and accuracy
STATS_ADD(mStats.mOccluders.mNumTilesUpdated, 1);
assert(tileIdx >= 0 && tileIdx < mTilesWidth*mTilesHeight);
__mwi mask = mMaskedHiZBuffer[tileIdx].mMask;
__mw *zMin = mMaskedHiZBuffer[tileIdx].mZMin;
// Swizzle coverage mask to 8x4 subtiles and test if any subtiles are not covered at all
__mwi rastMask = _mmw_transpose_epi8(coverage);
__mwi deadLane = _mmw_cmpeq_epi32(rastMask, SIMD_BITS_ZERO);
// Mask out all subtiles failing the depth test (don't update these subtiles)
deadLane |= _mmw_srai_epi32(simd_cast<__mwi>(zTriv - zMin[0]), 31);
rastMask = _mmw_andnot_epi32(deadLane, rastMask);
// Use distance heuristic to discard layer 1 if incoming triangle is significantly nearer to observer
// than the buffer contents. See Section 3.2 in "Masked Software Occlusion Culling"
__mwi coveredLane = _mmw_cmpeq_epi32(rastMask, SIMD_BITS_ONE);
__mw diff = _mmw_fmsub_ps(zMin[1], _mmw_set1_ps(2.0f), zTriv + zMin[0]);
__mwi discardLayerMask = _mmw_andnot_epi32(deadLane, _mmw_srai_epi32(simd_cast<__mwi>(diff), 31) | coveredLane);
// Update the mask with incoming triangle coverage
mask = _mmw_andnot_epi32(discardLayerMask, mask) | rastMask;
__mwi maskFull = _mmw_cmpeq_epi32(mask, SIMD_BITS_ONE);
// Compute new value for zMin[1]. This has one of four outcomes: zMin[1] = min(zMin[1], zTriv), zMin[1] = zTriv,
// zMin[1] = FLT_MAX or unchanged, depending on if the layer is updated, discarded, fully covered, or not updated
__mw opA = _mmw_blendv_ps(zTriv, zMin[1], simd_cast<__mw>(deadLane));
__mw opB = _mmw_blendv_ps(zMin[1], zTriv, simd_cast<__mw>(discardLayerMask));
__mw z1min = _mmw_min_ps(opA, opB);
zMin[1] = _mmw_blendv_ps(z1min, _mmw_set1_ps(FLT_MAX), simd_cast<__mw>(maskFull));
// Propagate zMin[1] back to zMin[0] if tile was fully covered, and update the mask
zMin[0] = _mmw_blendv_ps(zMin[0], z1min, simd_cast<__mw>(maskFull));
mMaskedHiZBuffer[tileIdx].mMask = _mmw_andnot_epi32(maskFull, mask);
}
FORCE_INLINE void UpdateTileAccurate(int tileIdx, const __mwi &coverage, const __mw &zTriv)
{
assert(tileIdx >= 0 && tileIdx < mTilesWidth*mTilesHeight);
__mw *zMin = mMaskedHiZBuffer[tileIdx].mZMin;
__mwi &mask = mMaskedHiZBuffer[tileIdx].mMask;
// Swizzle coverage mask to 8x4 subtiles
__mwi rastMask = _mmw_transpose_epi8(coverage);
// Perform individual depth tests with layer 0 & 1 and mask out all failing pixels
__mw sdist0 = zMin[0] - zTriv;
__mw sdist1 = zMin[1] - zTriv;
__mwi sign0 = _mmw_srai_epi32(simd_cast<__mwi>(sdist0), 31);
__mwi sign1 = _mmw_srai_epi32(simd_cast<__mwi>(sdist1), 31);
__mwi triMask = rastMask & (_mmw_andnot_epi32(mask, sign0) | (mask & sign1));
// Early out if no pixels survived the depth test (this test is more accurate than
// the early culling test in TraverseScanline())
__mwi t0 = _mmw_cmpeq_epi32(triMask, SIMD_BITS_ZERO);
__mwi t0inv = ~t0;
if (_mmw_testz_epi32(t0inv, t0inv))
return;
STATS_ADD(mStats.mOccluders.mNumTilesUpdated, 1);
__mw zTri = _mmw_blendv_ps(zTriv, zMin[0], simd_cast<__mw>(t0));
// Test if incoming triangle completely overwrites layer 0 or 1
__mwi layerMask0 = _mmw_andnot_epi32(triMask, ~mask);
__mwi layerMask1 = _mmw_andnot_epi32(triMask, mask);
__mwi lm0 = _mmw_cmpeq_epi32(layerMask0, SIMD_BITS_ZERO);
__mwi lm1 = _mmw_cmpeq_epi32(layerMask1, SIMD_BITS_ZERO);
__mw z0 = _mmw_blendv_ps(zMin[0], zTri, simd_cast<__mw>(lm0));
__mw z1 = _mmw_blendv_ps(zMin[1], zTri, simd_cast<__mw>(lm1));
// Compute distances used for merging heuristic
__mw d0 = abs(sdist0);
__mw d1 = abs(sdist1);
__mw d2 = abs(z0 - z1);
// Find minimum distance
__mwi c01 = simd_cast<__mwi>(d0 - d1);
__mwi c02 = simd_cast<__mwi>(d0 - d2);
__mwi c12 = simd_cast<__mwi>(d1 - d2);
// Two tests indicating which layer the incoming triangle will merge with or
// overwrite. d0min indicates that the triangle will overwrite layer 0, and
// d1min flags that the triangle will overwrite layer 1.
__mwi d0min = (c01 & c02) | (lm0 | t0);
__mwi d1min = _mmw_andnot_epi32(d0min, c12 | lm1);
///////////////////////////////////////////////////////////////////////////////
// Update depth buffer entry. NOTE: we always merge into layer 0, so if the
// triangle should be merged with layer 1, we first swap layer 0 & 1 and then
// merge into layer 0.
///////////////////////////////////////////////////////////////////////////////
// Update mask based on which layer the triangle overwrites or was merged into
__mw inner = _mmw_blendv_ps(simd_cast<__mw>(triMask), simd_cast<__mw>(layerMask1), simd_cast<__mw>(d0min));
mask = simd_cast<__mwi>(_mmw_blendv_ps(inner, simd_cast<__mw>(layerMask0), simd_cast<__mw>(d1min)));
// Update the zMin[0] value. There are four outcomes: overwrite with layer 1,
// merge with layer 1, merge with zTri or overwrite with layer 1 and then merge
// with zTri.
__mw e0 = _mmw_blendv_ps(z0, z1, simd_cast<__mw>(d1min));
__mw e1 = _mmw_blendv_ps(z1, zTri, simd_cast<__mw>(d1min | d0min));
zMin[0] = _mmw_min_ps(e0, e1);
// Update the zMin[1] value. There are three outcomes: keep current value,
// overwrite with zTri, or overwrite with z1
__mw z1t = _mmw_blendv_ps(zTri, z1, simd_cast<__mw>(d0min));
zMin[1] = _mmw_blendv_ps(z1t, z0, simd_cast<__mw>(d1min));
}
template<int TEST_Z, int NRIGHT, int NLEFT>
FORCE_INLINE int TraverseScanline(int leftOffset, int rightOffset, int tileIdx, int rightEvent, int leftEvent, const __mwi *events, const __mw &zTriMin, const __mw &zTriMax, const __mw &iz0, float zx)
{
// Floor edge events to integer pixel coordinates (shift out fixed point bits)
int eventOffset = leftOffset << TILE_WIDTH_SHIFT;
__mwi right[NRIGHT], left[NLEFT];
for (int i = 0; i < NRIGHT; ++i)
right[i] = _mmw_max_epi32(_mmw_srai_epi32(events[rightEvent + i], FP_BITS) - eventOffset, SIMD_BITS_ZERO);
for (int i = 0; i < NLEFT; ++i)
left[i] = _mmw_max_epi32(_mmw_srai_epi32(events[leftEvent - i], FP_BITS) - eventOffset, SIMD_BITS_ZERO);
__mw z0 = iz0 + zx*leftOffset;
int tileIdxEnd = tileIdx + rightOffset;
tileIdx += leftOffset;
for (;;)
{
if (TEST_Z)
STATS_ADD(mStats.mOccludees.mNumTilesTraversed, 1);
else
STATS_ADD(mStats.mOccluders.mNumTilesTraversed, 1);
// Perform a coarse test to quickly discard occluded tiles
#if QUICK_MASK != 0
// Only use the reference layer (layer 0) to cull as it is always conservative
__mw zMinBuf = mMaskedHiZBuffer[tileIdx].mZMin[0];
#else
// Compute zMin for the overlapped layers
__mwi mask = mMaskedHiZBuffer[tileIdx].mMask;
__mw zMin0 = _mmw_blendv_ps(mMaskedHiZBuffer[tileIdx].mZMin[0], mMaskedHiZBuffer[tileIdx].mZMin[1], simd_cast<__mw>(_mmw_cmpeq_epi32(mask, _mmw_set1_epi32(~0))));
__mw zMin1 = _mmw_blendv_ps(mMaskedHiZBuffer[tileIdx].mZMin[1], mMaskedHiZBuffer[tileIdx].mZMin[0], simd_cast<__mw>(_mmw_cmpeq_epi32(mask, _mmw_setzero_epi32())));
__mw zMinBuf = _mmw_min_ps(zMin0, zMin1);
#endif
__mw dist0 = zTriMax - zMinBuf;
if (_mmw_movemask_ps(dist0) != SIMD_ALL_LANES_MASK)
{
// Compute coverage mask for entire 32xN using shift operations
__mwi accumulatedMask = _mmw_sllv_ones(left[0]);
for (int i = 1; i < NLEFT; ++i)
accumulatedMask = accumulatedMask & _mmw_sllv_ones(left[i]);
for (int i = 0; i < NRIGHT; ++i)
accumulatedMask = _mmw_andnot_epi32(_mmw_sllv_ones(right[i]), accumulatedMask);
if (TEST_Z)
{
// Perform a conservative visibility test (test zMax against buffer for each covered 8x4 subtile)
__mw zSubTileMax = _mmw_min_ps(z0, zTriMax);
__mwi zPass = simd_cast<__mwi>(_mmw_cmpge_ps(zSubTileMax, zMinBuf));
__mwi rastMask = _mmw_transpose_epi8(accumulatedMask);
__mwi deadLane = _mmw_cmpeq_epi32(rastMask, SIMD_BITS_ZERO);
zPass = _mmw_andnot_epi32(deadLane, zPass);
if (!_mmw_testz_epi32(zPass, zPass))
return CullingResult::VISIBLE;
}
else
{
// Compute interpolated min for each 8x4 subtile and update the masked hierarchical z buffer entry
__mw zSubTileMin = _mmw_max_ps(z0, zTriMin);
#if QUICK_MASK != 0
UpdateTileQuick(tileIdx, accumulatedMask, zSubTileMin);
#else
UpdateTileAccurate(tileIdx, accumulatedMask, zSubTileMin);
#endif
}
}
// Update buffer address, interpolate z and edge events
tileIdx++;
if (tileIdx >= tileIdxEnd)
break;
z0 += zx;
for (int i = 0; i < NRIGHT; ++i)
right[i] = _mmw_subs_epu16(right[i], SIMD_TILE_WIDTH); // Trick, use sub saturated to avoid checking against < 0 for shift (values should fit in 16 bits)
for (int i = 0; i < NLEFT; ++i)
left[i] = _mmw_subs_epu16(left[i], SIMD_TILE_WIDTH);
}
return TEST_Z ? CullingResult::OCCLUDED : CullingResult::VISIBLE;
}
template<int TEST_Z, int TIGHT_TRAVERSAL, int MID_VTX_RIGHT>
#if PRECISE_COVERAGE != 0
FORCE_INLINE int RasterizeTriangle(unsigned int triIdx, int bbWidth, int tileRowIdx, int tileMidRowIdx, int tileEndRowIdx, const __mwi *eventStart, const __mw *slope, const __mwi *slopeTileDelta, const __mw &zTriMin, const __mw &zTriMax, __mw &z0, float zx, float zy, const __mwi *edgeY, const __mwi *absEdgeX, const __mwi *slopeSign, const __mwi *eventStartRemainder, const __mwi *slopeTileRemainder)
#else
FORCE_INLINE int RasterizeTriangle(unsigned int triIdx, int bbWidth, int tileRowIdx, int tileMidRowIdx, int tileEndRowIdx, const __mwi *eventStart, const __mwi *slope, const __mwi *slopeTileDelta, const __mw &zTriMin, const __mw &zTriMax, __mw &z0, float zx, float zy)
#endif
{
if (TEST_Z)
STATS_ADD(mStats.mOccludees.mNumRasterizedTriangles, 1);
else
STATS_ADD(mStats.mOccluders.mNumRasterizedTriangles, 1);
int cullResult;
#if PRECISE_COVERAGE != 0
#define LEFT_EDGE_BIAS -1
#define RIGHT_EDGE_BIAS 1
#define UPDATE_TILE_EVENTS_Y(i) \
triEventRemainder[i] -= triSlopeTileRemainder[i]; \
__mwi overflow##i = _mmw_srai_epi32(triEventRemainder[i], 31); \
triEventRemainder[i] += overflow##i & triEdgeY[i]; \
triEvent[i] += triSlopeTileDelta[i] + (overflow##i & triSlopeSign[i])
__mwi triEvent[3], triSlopeSign[3], triSlopeTileDelta[3], triEdgeY[3], triSlopeTileRemainder[3], triEventRemainder[3];
for (int i = 0; i < 3; ++i)
{
triSlopeSign[i] = _mmw_set1_epi32(slopeSign[i].mw_i32[triIdx]);
triSlopeTileDelta[i] = _mmw_set1_epi32(slopeTileDelta[i].mw_i32[triIdx]);
triEdgeY[i] = _mmw_set1_epi32(edgeY[i].mw_i32[triIdx]);
triSlopeTileRemainder[i] = _mmw_set1_epi32(slopeTileRemainder[i].mw_i32[triIdx]);
__mw triSlope = _mmw_set1_ps(slope[i].mw_f32[triIdx]);
__mwi triAbsEdgeX = _mmw_set1_epi32(absEdgeX[i].mw_i32[triIdx]);
__mwi triStartRemainder = _mmw_set1_epi32(eventStartRemainder[i].mw_i32[triIdx]);
__mwi triEventStart = _mmw_set1_epi32(eventStart[i].mw_i32[triIdx]);
__mwi scanlineDelta = _mmw_cvttps_epi32(triSlope * SIMD_LANE_YCOORD_F);
__mwi scanlineSlopeRemainder = (_mmw_mullo_epi32(triAbsEdgeX, SIMD_LANE_YCOORD_I) - _mmw_mullo_epi32(_mmw_abs_epi32(scanlineDelta), triEdgeY[i]));
triEventRemainder[i] = triStartRemainder - scanlineSlopeRemainder;
__mwi overflow = _mmw_srai_epi32(triEventRemainder[i], 31);
triEventRemainder[i] += overflow & triEdgeY[i];
triEvent[i] = triEventStart + scanlineDelta + (overflow & triSlopeSign[i]);
}
#else
#define LEFT_EDGE_BIAS 0
#define RIGHT_EDGE_BIAS 0
#define UPDATE_TILE_EVENTS_Y(i) triEvent[i] += triSlopeTileDelta[i];
// Get deltas used to increment edge events each time we traverse one scanline of tiles
__mwi triSlopeTileDelta[3];
triSlopeTileDelta[0] = _mmw_set1_epi32(slopeTileDelta[0].mw_i32[triIdx]);
triSlopeTileDelta[1] = _mmw_set1_epi32(slopeTileDelta[1].mw_i32[triIdx]);
triSlopeTileDelta[2] = _mmw_set1_epi32(slopeTileDelta[2].mw_i32[triIdx]);
// Setup edge events for first batch of SIMD_LANES scanlines
__mwi triEvent[3];
triEvent[0] = _mmw_set1_epi32(eventStart[0].mw_i32[triIdx]) + _mmw_mullo_epi32(SIMD_LANE_IDX, _mmw_set1_epi32(slope[0].mw_i32[triIdx]));
triEvent[1] = _mmw_set1_epi32(eventStart[1].mw_i32[triIdx]) + _mmw_mullo_epi32(SIMD_LANE_IDX, _mmw_set1_epi32(slope[1].mw_i32[triIdx]));
triEvent[2] = _mmw_set1_epi32(eventStart[2].mw_i32[triIdx]) + _mmw_mullo_epi32(SIMD_LANE_IDX, _mmw_set1_epi32(slope[2].mw_i32[triIdx]));
#endif
// For big triangles track start & end tile for each scanline and only traverse the valid region
int startDelta, endDelta, topDelta, startEvent, endEvent, topEvent;
if (TIGHT_TRAVERSAL)
{
startDelta = slopeTileDelta[2].mw_i32[triIdx] + LEFT_EDGE_BIAS;
endDelta = slopeTileDelta[0].mw_i32[triIdx] + RIGHT_EDGE_BIAS;
topDelta = slopeTileDelta[1].mw_i32[triIdx] + (MID_VTX_RIGHT ? RIGHT_EDGE_BIAS : LEFT_EDGE_BIAS);
// Compute conservative bounds for the edge events over a 32xN tile
startEvent = eventStart[2].mw_i32[triIdx] + min(0, startDelta);
endEvent = eventStart[0].mw_i32[triIdx] + max(0, endDelta) + (TILE_WIDTH << FP_BITS);
if (MID_VTX_RIGHT)
topEvent = eventStart[1].mw_i32[triIdx] + max(0, topDelta) + (TILE_WIDTH << FP_BITS);
else
topEvent = eventStart[1].mw_i32[triIdx] + min(0, topDelta);
}
if (tileRowIdx <= tileMidRowIdx)
{
int tileStopIdx = min(tileEndRowIdx, tileMidRowIdx);
// Traverse the bottom half of the triangle
while (tileRowIdx < tileStopIdx)
{
int start = 0, end = bbWidth;
if (TIGHT_TRAVERSAL)
{
// Compute tighter start and endpoints to avoid traversing empty space
start = max(0, min(bbWidth - 1, startEvent >> (TILE_WIDTH_SHIFT + FP_BITS)));
end = min(bbWidth, ((int)endEvent >> (TILE_WIDTH_SHIFT + FP_BITS)));
startEvent += startDelta;
endEvent += endDelta;
}
// Traverse the scanline and update the masked hierarchical z buffer
cullResult = TraverseScanline<TEST_Z, 1, 1>(start, end, tileRowIdx, 0, 2, triEvent, zTriMin, zTriMax, z0, zx);
if (TEST_Z && cullResult == CullingResult::VISIBLE) // Early out if performing occlusion query
return CullingResult::VISIBLE;
// move to the next scanline of tiles, update edge events and interpolate z
tileRowIdx += mTilesWidth;
z0 += zy;
UPDATE_TILE_EVENTS_Y(0);
UPDATE_TILE_EVENTS_Y(2);
}
// Traverse the middle scanline of tiles. We must consider all three edges only in this region
if (tileRowIdx < tileEndRowIdx)
{
int start = 0, end = bbWidth;
if (TIGHT_TRAVERSAL)
{
// Compute tighter start and endpoints to avoid traversing lots of empty space
start = max(0, min(bbWidth - 1, startEvent >> (TILE_WIDTH_SHIFT + FP_BITS)));
end = min(bbWidth, ((int)endEvent >> (TILE_WIDTH_SHIFT + FP_BITS)));
// Switch the traversal start / end to account for the upper side edge
endEvent = MID_VTX_RIGHT ? topEvent : endEvent;
endDelta = MID_VTX_RIGHT ? topDelta : endDelta;
startEvent = MID_VTX_RIGHT ? startEvent : topEvent;
startDelta = MID_VTX_RIGHT ? startDelta : topDelta;
startEvent += startDelta;
endEvent += endDelta;
}
// Traverse the scanline and update the masked hierarchical z buffer.
if (MID_VTX_RIGHT)
cullResult = TraverseScanline<TEST_Z, 2, 1>(start, end, tileRowIdx, 0, 2, triEvent, zTriMin, zTriMax, z0, zx);
else
cullResult = TraverseScanline<TEST_Z, 1, 2>(start, end, tileRowIdx, 0, 2, triEvent, zTriMin, zTriMax, z0, zx);
if (TEST_Z && cullResult == CullingResult::VISIBLE) // Early out if performing occlusion query
return CullingResult::VISIBLE;
tileRowIdx += mTilesWidth;
}
// Traverse the top half of the triangle
if (tileRowIdx < tileEndRowIdx)
{
// move to the next scanline of tiles, update edge events and interpolate z
z0 += zy;
int i0 = MID_VTX_RIGHT + 0;
int i1 = MID_VTX_RIGHT + 1;
UPDATE_TILE_EVENTS_Y(i0);
UPDATE_TILE_EVENTS_Y(i1);
for (;;)
{
int start = 0, end = bbWidth;
if (TIGHT_TRAVERSAL)
{
// Compute tighter start and endpoints to avoid traversing lots of empty space
start = max(0, min(bbWidth - 1, startEvent >> (TILE_WIDTH_SHIFT + FP_BITS)));
end = min(bbWidth, ((int)endEvent >> (TILE_WIDTH_SHIFT + FP_BITS)));
startEvent += startDelta;
endEvent += endDelta;
}
// Traverse the scanline and update the masked hierarchical z buffer
cullResult = TraverseScanline<TEST_Z, 1, 1>(start, end, tileRowIdx, MID_VTX_RIGHT + 0, MID_VTX_RIGHT + 1, triEvent, zTriMin, zTriMax, z0, zx);
if (TEST_Z && cullResult == CullingResult::VISIBLE) // Early out if performing occlusion query
return CullingResult::VISIBLE;
// move to the next scanline of tiles, update edge events and interpolate z
tileRowIdx += mTilesWidth;
if (tileRowIdx >= tileEndRowIdx)
break;
z0 += zy;
UPDATE_TILE_EVENTS_Y(i0);
UPDATE_TILE_EVENTS_Y(i1);
}
}
}
else
{
if (TIGHT_TRAVERSAL)
{
// For large triangles, switch the traversal start / end to account for the upper side edge
endEvent = MID_VTX_RIGHT ? topEvent : endEvent;
endDelta = MID_VTX_RIGHT ? topDelta : endDelta;
startEvent = MID_VTX_RIGHT ? startEvent : topEvent;
startDelta = MID_VTX_RIGHT ? startDelta : topDelta;
}
// Traverse the top half of the triangle
if (tileRowIdx < tileEndRowIdx)
{
int i0 = MID_VTX_RIGHT + 0;
int i1 = MID_VTX_RIGHT + 1;
for (;;)
{
int start = 0, end = bbWidth;
if (TIGHT_TRAVERSAL)
{
// Compute tighter start and endpoints to avoid traversing lots of empty space
start = max(0, min(bbWidth - 1, startEvent >> (TILE_WIDTH_SHIFT + FP_BITS)));
end = min(bbWidth, ((int)endEvent >> (TILE_WIDTH_SHIFT + FP_BITS)));
startEvent += startDelta;
endEvent += endDelta;
}
// Traverse the scanline and update the masked hierarchical z buffer
cullResult = TraverseScanline<TEST_Z, 1, 1>(start, end, tileRowIdx, MID_VTX_RIGHT + 0, MID_VTX_RIGHT + 1, triEvent, zTriMin, zTriMax, z0, zx);