forked from GaijinEntertainment/vecmath
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdag_vecMath_common.h
3138 lines (2750 loc) · 115 KB
/
dag_vecMath_common.h
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
//
// Dagor Engine 6.5
// Copyright (C) 2023 Gaijin Games KFT. All rights reserved
// (for conditions of use see prog/license.txt)
//
#pragma once
#include "dag_vecMath.h"
#ifdef __cplusplus
#include <cmath> //for fabsf, which is used once, and not wise
#else
#include <math.h>
#endif
#ifdef _MSC_VER
#pragma warning(push)
#pragma warning(disable : 4800)
//we use direct conversion to bool from int
#endif
// Fast but still UNSAFE function for float[3] point loading by one 16-bytes read. Only for FALSE data-race fixing!
// It's can be used to suppress false-positive TSAN warnings, for example when multiple threads
// simultaneously read and write to DIFFERENT elements of same float[3] array.
// TSAN catches data race on 4 bytes after loaded float[3], but readed .w component really is not used.
// You can safely use that function for data on stack, but loading of last element of float[3] array
// may be unsafe when it's allocated on heap. Sometimes access to memory after array may cause app crash.
// Don't use that function to fix warnings like "heap use after free".
NO_ASAN_INLINE vec3f v_ldu_p3(const float *m)
{
#if __SANITIZE_THREAD__
return v_ldu_p3_safe(m);
#else
return v_ldu(m);
#endif
}
NO_ASAN_INLINE vec4i v_ldui_p3(const int *m)
{
#if __SANITIZE_THREAD__
return v_ldui_p3_safe(m);
#else
return v_ldui(m);
#endif
}
VECTORCALL VECMATH_FINLINE vec4f v_make_vec4f_mask(uint8_t bitmask)
{
vec4i lookup = v_make_vec4i(1 << 0, 1 << 1, 1 << 2, 1 << 3);
vec4i isolated = v_andi(v_splatsi(bitmask), lookup);
return v_cast_vec4f(v_cmp_eqi(isolated, lookup));
}
VECTORCALL VECMATH_FINLINE vec4f v_insert_x(vec4f a, float x) { return v_perm_ayzw(a, v_splats(x)); }
VECTORCALL VECMATH_FINLINE vec4f v_insert_y(vec4f a, float y) { return v_perm_xbzw(a, v_splats(y)); }
VECTORCALL VECMATH_FINLINE vec4f v_insert_z(vec4f a, float z) { return v_perm_xycw(a, v_splats(z)); }
VECTORCALL VECMATH_FINLINE vec4f v_insert_w(vec4f a, float w) { return v_perm_xyzd(a, v_splats(w)); }
VECTORCALL VECMATH_FINLINE vec4f v_cmp_le(vec4f a, vec4f b) { return v_cmp_ge(b, a); }
VECTORCALL VECMATH_FINLINE vec4f v_cmp_lt(vec4f a, vec4f b) { return v_cmp_gt(b, a); }
VECTORCALL VECMATH_FINLINE vec4f v_clamp(vec4f t, vec4f min_val, vec4f max_val)
{
return v_max(v_min(t, max_val), min_val);
}
VECTORCALL VECMATH_FINLINE vec4i v_clampi(vec4i t, vec4i min_val, vec4i max_val)
{
return v_maxi(v_mini(t, max_val), min_val);
}
VECTORCALL VECMATH_FINLINE vec4f v_cmp_relative_equal(vec4f a, vec4f b, vec4f max_diff, vec4f max_rel_diff)
{
vec4f diff = v_abs(v_sub(a, b));
vec4f m = v_max(v_abs(a), v_abs(b));
return v_cmp_le(diff, v_min(max_diff, v_mul(m, max_rel_diff)));
}
VECTORCALL VECMATH_FINLINE bool v_is_relative_equal_vec3f(vec4f a, vec4f b)
{
return v_check_xyz_all_not_zeroi(v_cmp_relative_equal(a, b));
}
VECTORCALL VECMATH_FINLINE bool v_is_relative_equal_vec4f(vec4f a, vec4f b)
{
return v_check_xyzw_all_not_zeroi(v_cmp_relative_equal(a, b));
}
VECTORCALL VECMATH_FINLINE vec4f v_is_unsafe_divisor(vec4f a)
{
return v_cmp_lt(v_abs(a), V_C_VERY_SMALL_VAL);
}
VECTORCALL VECMATH_FINLINE vec4f v_safediv(vec4f a, vec4f b, vec4f def)
{
vec4f isDiv0 = v_is_unsafe_divisor(b);
return v_sel(v_div(a, b), def, isDiv0);
}
VECTORCALL VECMATH_FINLINE vec4f v_rcp(vec4f a)
{
//! Works only with -fno-reciprocal-math (clang) and -mno-recip (gcc)
return v_div(V_C_ONE, a);
}
VECTORCALL VECMATH_FINLINE vec4f v_rcp_x(vec4f a)
{
return v_div_x(V_C_ONE, a);
}
VECTORCALL VECMATH_FINLINE vec4f v_rcp_safe(vec4f a, vec4f def)
{
vec4f isDiv0 = v_is_unsafe_divisor(a);
return v_sel(v_rcp(a), def, isDiv0);
}
VECTORCALL VECMATH_FINLINE vec4f v_mod(vec4f a, vec4f aDiv)
{
vec4f c = v_div(a, aDiv);
vec4f cTrunc = v_cvt_vec4f(v_cvt_vec4i(c));
vec4f base = v_mul(cTrunc, aDiv);
vec4f r = v_sub(a, base);
return r;
}
VECTORCALL VECMATH_FINLINE vec4f v_lerp_vec4f(vec4f tttt, vec4f a, vec4f b)
{
return v_madd(v_sub(b, a), tttt, a);
}
VECTORCALL VECMATH_FINLINE vec4f v_hand(vec4f a)
{
a = v_and(a, v_rot_1(a));
return v_and(a, v_rot_2(a));
}
VECTORCALL VECMATH_FINLINE vec4f v_hor(vec4f a)
{
a = v_or(a, v_rot_1(a));
return v_or(a, v_rot_2(a));
}
VECTORCALL VECMATH_FINLINE vec4f v_hand3(vec3f a)
{
return v_and(v_splat_x(a), v_and(v_splat_y(a), v_splat_z(a)));
}
VECTORCALL VECMATH_FINLINE vec4f v_hor3(vec3f a)
{
return v_or(v_splat_x(a), v_or(v_splat_y(a), v_splat_z(a)));
}
VECTORCALL VECMATH_FINLINE vec4f v_hmin(vec4f a)
{
a = v_min(a, v_rot_1(a));
return v_min(a, v_rot_2(a));
}
VECTORCALL VECMATH_FINLINE vec4f v_hmax(vec4f a)
{
a = v_max(a, v_rot_1(a));
return v_max(a, v_rot_2(a));
}
VECTORCALL VECMATH_FINLINE vec4f v_hmin3(vec3f a)
{
return v_min(v_splat_x(a), v_min(v_splat_y(a), v_splat_z(a)));
}
VECTORCALL VECMATH_FINLINE vec4f v_hmax3(vec3f a)
{
return v_max(v_splat_x(a), v_max(v_splat_y(a), v_splat_z(a)));
}
VECTORCALL VECMATH_FINLINE vec4f v_hmul(vec4f a)
{
a = v_mul(a, v_rot_1(a));
return v_mul(a, v_rot_2(a));
}
VECTORCALL VECMATH_FINLINE vec4f v_hmul3(vec3f a)
{
return v_mul(v_splat_x(a), v_mul(v_splat_y(a), v_splat_z(a)));
}
VECTORCALL VECMATH_FINLINE vec4f v_sqr(vec4f a)
{
return v_mul(a, a);
}
VECTORCALL VECMATH_FINLINE vec4f v_sqr_x(vec4f a)
{
return v_mul_x(a, a);
}
VECTORCALL VECMATH_FINLINE vec4f v_perm_xxxx(vec4f a) { return v_splat_x(a); }
VECTORCALL VECMATH_FINLINE vec4f v_perm_yyyy(vec4f a) { return v_splat_y(a); }
VECTORCALL VECMATH_FINLINE vec4f v_perm_zzzz(vec4f a) { return v_splat_z(a); }
VECTORCALL VECMATH_FINLINE vec4f v_perm_wwww(vec4f a) { return v_splat_w(a); }
VECTORCALL VECMATH_FINLINE vec4f v_perm_yzwx(vec4f a) { return v_rot_1(a); }
VECTORCALL VECMATH_FINLINE vec4f v_perm_zwxy(vec4f a) { return v_rot_2(a); }
VECTORCALL VECMATH_FINLINE vec4f v_perm_wxyz(vec4f a) { return v_rot_3(a); }
VECTORCALL VECMATH_FINLINE vec4f v_perm_zcwd(vec4f xyzw, vec4f abcd) { return v_merge_lw(xyzw, abcd); }
VECTORCALL VECMATH_FINLINE vec4f v_perm_xayb(vec4f xyzw, vec4f abcd) { return v_merge_hw(xyzw, abcd); }
#if !_TARGET_SIMD_NEON
VECTORCALL VECMATH_FINLINE vec4f v_length4_sq(vec4f a) { return v_dot4(a,a); }
VECTORCALL VECMATH_FINLINE vec3f v_length3_sq(vec3f a) { return v_dot3(a,a); }
VECTORCALL VECMATH_FINLINE vec4f v_length2_sq(vec4f a) { return v_dot2(a,a); }
VECTORCALL VECMATH_FINLINE vec4f v_length4_sq_x(vec4f a) { return v_dot4_x(a,a); }
VECTORCALL VECMATH_FINLINE vec3f v_length3_sq_x(vec3f a) { return v_dot3_x(a,a); }
VECTORCALL VECMATH_FINLINE vec4f v_length2_sq_x(vec4f a) { return v_dot2_x(a,a); }
#endif
VECTORCALL VECMATH_FINLINE vec4f v_length4(vec4f a) { return v_sqrt(v_length4_sq(a)); }
VECTORCALL VECMATH_FINLINE vec3f v_length3(vec3f a) { return v_sqrt(v_length3_sq(a)); }
VECTORCALL VECMATH_FINLINE vec4f v_length2(vec4f a) { return v_sqrt(v_length2_sq(a)); }
VECTORCALL VECMATH_FINLINE vec4f v_length4_est(vec4f a) { return v_sqrt4_fast(v_length4_sq(a)); }
VECTORCALL VECMATH_FINLINE vec3f v_length3_est(vec3f a) { return v_sqrt4_fast(v_length3_sq(a)); }
VECTORCALL VECMATH_FINLINE vec4f v_length2_est(vec4f a) { return v_sqrt4_fast(v_length2_sq(a)); }
VECTORCALL VECMATH_FINLINE vec4f v_length4_x(vec4f a) { return v_sqrt_x(v_length4_sq_x(a)); }
VECTORCALL VECMATH_FINLINE vec3f v_length3_x(vec3f a) { return v_sqrt_x(v_length3_sq_x(a)); }
VECTORCALL VECMATH_FINLINE vec4f v_length2_x(vec4f a) { return v_sqrt_x(v_length2_sq_x(a)); }
VECTORCALL VECMATH_FINLINE vec4f v_length4_est_x(vec4f a) { return v_sqrt_fast_x(v_length4_sq_x(a)); }
VECTORCALL VECMATH_FINLINE vec3f v_length3_est_x(vec3f a) { return v_sqrt_fast_x(v_length3_sq_x(a)); }
VECTORCALL VECMATH_FINLINE vec4f v_length2_est_x(vec4f a) { return v_sqrt_fast_x(v_length2_sq_x(a)); }
VECTORCALL VECMATH_FINLINE vec3f v_striple3(vec3f a, vec3f b, vec3f c) { return v_dot3(v_cross3(a, b), c); }
VECTORCALL VECMATH_FINLINE vec3f v_vtriple3(vec3f a, vec3f b, vec3f c)
{
vec3f ac = v_dot3(a, c);
vec3f ab = v_dot3(a, b);
return v_nmsub(c, ab, v_mul(b, ac));
}
VECTORCALL VECMATH_FINLINE vec3f v_cross3(vec3f a, vec3f b)
{
// a.y * b.z - a.z * b.y
// a.z * b.x - a.x * b.z
// a.x * b.y - a.y * b.x
vec3f tmp0 = v_perm_yzxw(a);
vec3f tmp1 = v_perm_zxyw(b);
vec3f tmp2 = v_mul(tmp0, b);
vec3f tmp3 = v_mul(tmp0, tmp1);
vec3f tmp4 = v_perm_yzxw(tmp2);
return v_sub(tmp3, tmp4);
}
VECTORCALL VECMATH_FINLINE void v_mat44_make_persp_forward(mat44f &dest, float wk, float hk, float zn, float zf)
{
float q = zf/(zf-zn);
dest.col0 = v_make_vec4f(wk, 0, 0, 0);
dest.col1 = v_make_vec4f(0, hk, 0, 0);
dest.col2 = v_make_vec4f(0, 0, q, 1.f);
dest.col3 = v_make_vec4f(0, 0, -q*zn, 0);
}
VECTORCALL VECMATH_FINLINE void v_mat44_make_persp_reverse(mat44f &dest, float wk, float hk, float zn, float zf)
{
dest.col0 = v_make_vec4f(wk, 0, 0, 0);
dest.col1 = v_make_vec4f(0, hk, 0, 0);
dest.col2 = v_make_vec4f(0, 0, zn/(zn-zf), 1.f);
dest.col3 = v_make_vec4f(0, 0, (zn*zf)/(zf-zn), 0);
}
VECTORCALL VECMATH_FINLINE void v_mat44_make_persp(mat44f &dest, float wk, float hk, float zn, float zf)
{
return v_mat44_make_persp_reverse(dest, wk, hk, zn, zf);
}
#if !_TARGET_SIMD_SSE
VECTORCALL VECMATH_FINLINE void v_mat33_transpose(mat33f &dest, vec3f col0, vec3f col1, vec3f col2)
{
vec4f tmp0, tmp1, tmp2, tmp3;
tmp0 = v_merge_hw(col0, col2);
tmp1 = v_merge_hw(col1, v_zero());//may be v_zero can be replaced with col2
tmp2 = v_merge_lw(col0, col2);
tmp3 = v_merge_lw(col1, v_zero());//may be v_zero can be replaced with col2
dest.col0 = v_merge_hw(tmp0, tmp1);
dest.col1 = v_merge_lw(tmp0, tmp1);
dest.col2 = v_merge_hw(tmp2, tmp3);
}
VECTORCALL VECMATH_FINLINE void v_mat44_transpose_to_mat33(mat33f &dest, vec3f col0, vec3f col1, vec3f col2, vec3f col3)
{
vec4f tmp0, tmp1, tmp2, tmp3;
tmp0 = v_merge_hw(col0, col2);
tmp1 = v_merge_hw(col1, col3);
tmp2 = v_merge_lw(col0, col2);
tmp3 = v_merge_lw(col1, col3);
dest.col0 = v_merge_hw(tmp0, tmp1);
dest.col1 = v_merge_lw(tmp0, tmp1);
dest.col2 = v_merge_hw(tmp2, tmp3);
}
#endif
VECTORCALL VECMATH_FINLINE vec4f v_remove_not_finite(vec4f a)
{
static vec4i_const infMask = DECL_VECUINT4( 0x7F800000, 0x7F800000, 0x7F800000, 0x7F800000 );
vec4i ai = v_cast_vec4i(a);
return v_cast_vec4f(v_andnoti(v_cmp_eqi(v_andi(ai, infMask), infMask), ai));
}
#if defined(__FINITE_MATH_ONLY__) && __FINITE_MATH_ONLY__ > 0 // Clang/GCC
VECTORCALL VECMATH_FINLINE vec4f v_remove_nan(vec4f a)
{
static vec4i_const minNan = DECL_VECUINT4( 0x7F800001, 0x7F800001, 0x7F800001, 0x7F800001 );
vec4i am = v_andi(v_cast_vec4i(a), V_CI_INV_SIGN_MASK);
return v_cast_vec4f(v_andi(v_cast_vec4i(a), v_cmp_lti(am, minNan)));
}
#else
VECTORCALL VECMATH_FINLINE vec4f v_remove_nan(vec4f a) {return v_and(a, v_cmp_eq(a,a)); }
#endif
VECTORCALL VECMATH_FINLINE vec4f v_norm4_safe(vec4f a) {return v_remove_not_finite(v_norm4(a));}
VECTORCALL VECMATH_FINLINE vec4f v_norm3_safe(vec4f a) {return v_remove_not_finite(v_norm3(a));}
VECTORCALL VECMATH_FINLINE vec4f v_norm2_safe(vec4f a) {return v_remove_not_finite(v_norm2(a));}
VECTORCALL VECMATH_FINLINE void v_mat33_transpose(mat33f &dest, mat33f_cref src)
{
v_mat33_transpose(dest, src.col0, src.col1, src.col2);
}
VECTORCALL VECMATH_FINLINE void v_mat33_make_from_look(mat33f &dest, vec4f look_dir)
{
vec4f vx = v_cross3(V_C_UNIT_0100, look_dir);
vec4f vxlsq = v_length3_sq(vx);
#if _TARGET_SIMD_SSE
if (v_extract_x(vxlsq) < 1.42e-12F)
vx = v_norm3(v_cross3(v_btsel(V_C_UNIT_0010, look_dir, v_msbit()), look_dir));
else
vx = v_mul(vx, v_rsqrt(vxlsq));
#else
vec4f vx1 = v_norm3(v_cross3(v_btsel(V_C_UNIT_0010, look_dir, v_msbit()), look_dir));
vec4f vx2 = v_mul(vx, v_rsqrt(vxlsq));
vx = v_sel(vx1, vx2, v_cmp_gt(vxlsq, v_zero()));
#endif
dest.col0 = vx;
dest.col1 = v_norm3(v_cross3(look_dir, vx));
dest.col2 = look_dir;
}
VECTORCALL VECMATH_INLINE void v_view_matrix_from_tangentZ( vec3f &left, vec3f &up, vec4f vdir )
{
up = v_sel(V_C_UNIT_0010, V_C_UNIT_1000, v_cmp_gt(v_abs(v_splat_z(vdir)), v_splats(0.999f)));
left = v_norm3(v_cross3(up, vdir));
up = v_cross3(vdir, left);
}
VECTORCALL VECMATH_FINLINE void v_mat44_make33_from_look(mat44f &dest, vec4f look_dir)
{
mat33f m;
v_mat33_make_from_look(m, look_dir);
dest.set33(m);
}
VECTORCALL VECMATH_FINLINE void v_mat44_make_look_at(mat44f &dest, vec4f eye, vec4f at, vec4f up)
{
dest.col2 = v_norm3(v_sub(at, eye));
dest.col0 = v_norm3(v_cross3(up, dest.col2));
dest.col1 = v_cross3(dest.col2, dest.col0);
dest.col3 = eye;
v_mat44_orthonormal_inverse43_to44(dest, dest);
}
VECTORCALL VECMATH_FINLINE void v_mat44_make_ortho(mat44f &dest, float _w, float _h, float _zn, float _zf)
{
v_mat44_ident(dest);
vec4f zn = v_splats(_zn);
vec4f w = v_splats(_w);
vec4f h = v_splats(_h);
vec4f r_zr = v_rcp(v_sub(v_splats(_zf), zn));
dest.col2 = v_mul(dest.col2, r_zr);
dest.col0 = v_mul(v_add(dest.col0, dest.col0), v_rcp(w));
dest.col1 = v_mul(v_add(dest.col1, dest.col1), v_rcp(h));
dest.col3 = v_madd(zn, dest.col2, dest.col3);
}
VECTORCALL VECMATH_FINLINE void v_mat44_add(mat44f &dest, mat44f_cref m1, mat44f_cref m2)
{
dest.col0 = v_add(m1.col0, m2.col0);
dest.col1 = v_add(m1.col1, m2.col1);
dest.col2 = v_add(m1.col2, m2.col2);
dest.col3 = v_add(m1.col3, m2.col3);
}
VECTORCALL VECMATH_FINLINE void v_mat33_add(mat33f &dest, mat33f_cref m1, mat33f_cref m2)
{
dest.col0 = v_add(m1.col0, m2.col0);
dest.col1 = v_add(m1.col1, m2.col1);
dest.col2 = v_add(m1.col2, m2.col2);
}
VECTORCALL VECMATH_FINLINE void v_mat43_sub(mat43f &dest, mat43f_cref m1, mat43f_cref m2)
{
dest.row0 = v_sub(m1.row0, m2.row0);
dest.row1 = v_sub(m1.row1, m2.row1);
dest.row2 = v_sub(m1.row2, m2.row2);
}
VECTORCALL VECMATH_FINLINE void v_mat44_sub(mat44f &dest, mat44f_cref m1, mat44f_cref m2)
{
dest.col0 = v_sub(m1.col0, m2.col0);
dest.col1 = v_sub(m1.col1, m2.col1);
dest.col2 = v_sub(m1.col2, m2.col2);
dest.col3 = v_sub(m1.col3, m2.col3);
}
VECTORCALL VECMATH_FINLINE void v_mat33_sub(mat33f &dest, mat33f_cref m1, mat33f_cref m2)
{
dest.col0 = v_sub(m1.col0, m2.col0);
dest.col1 = v_sub(m1.col1, m2.col1);
dest.col2 = v_sub(m1.col2, m2.col2);
}
VECTORCALL VECMATH_FINLINE void v_mat33_from_mat44(mat33f &dest, mat44f_cref m2)
{
dest.col0 = m2.col0;
dest.col1 = m2.col1;
dest.col2 = m2.col2;
}
VECTORCALL VECMATH_FINLINE void v_mat33_neg(mat44f &dest, mat44f_cref m)
{
vec4f zero = v_zero();
dest.col0 = v_sub(zero, m.col0);
dest.col1 = v_sub(zero, m.col1);
dest.col2 = v_sub(zero, m.col2);
}
VECTORCALL VECMATH_FINLINE void v_mat33_neg(mat33f &dest, mat33f_cref m)
{
vec4f zero = v_zero();
dest.col0 = v_sub(zero, m.col0);
dest.col1 = v_sub(zero, m.col1);
dest.col2 = v_sub(zero, m.col2);
}
VECTORCALL VECMATH_FINLINE void v_mat44_mul_elem(mat44f &dest, mat44f_cref m1, mat44f_cref m2)
{
dest.col0 = v_mul(m1.col0, m2.col0);
dest.col1 = v_mul(m1.col1, m2.col1);
dest.col2 = v_mul(m1.col2, m2.col2);
dest.col3 = v_mul(m1.col3, m2.col3);
}
VECTORCALL VECMATH_FINLINE void v_mat33_mul_elem(mat33f &dest, mat33f_cref m1, mat33f_cref m2)
{
dest.col0 = v_mul(m1.col0, m2.col0);
dest.col1 = v_mul(m1.col1, m2.col1);
dest.col2 = v_mul(m1.col2, m2.col2);
}
VECTORCALL VECMATH_FINLINE vec3f v_mat43_mul_vec3v(mat43f_cref m, vec3f v)
{
mat44f m44;
v_mat43_transpose_to_mat44(m44, m);
return v_mat44_mul_vec3v(m44, v);
}
VECTORCALL VECMATH_FINLINE vec3f v_mat43_mul_vec3p(mat43f_cref m, vec3f v)
{
mat44f m44;
v_mat43_transpose_to_mat44(m44, m);
return v_mat44_mul_vec3p(m44, v);
}
VECTORCALL VECMATH_FINLINE void v_mat44_mul(mat44f &dest, mat44f_cref m1, mat44f_cref m2)
{
vec4f col0 = v_mat44_mul_vec4(m1, m2.col0);
vec4f col1 = v_mat44_mul_vec4(m1, m2.col1);
vec4f col2 = v_mat44_mul_vec4(m1, m2.col2);
vec4f col3 = v_mat44_mul_vec4(m1, m2.col3);
dest.col0 = col0;
dest.col1 = col1;
dest.col2 = col2;
dest.col3 = col3;
}
VECTORCALL VECMATH_FINLINE void v_mat44_mul43(mat44f &dest, mat44f_cref m1, mat44f_cref m2)
{
vec4f col0 = v_mat44_mul_vec3v(m1, m2.col0);
vec4f col1 = v_mat44_mul_vec3v(m1, m2.col1);
vec4f col2 = v_mat44_mul_vec3v(m1, m2.col2);
vec4f col3 = v_mat44_mul_vec3p(m1, m2.col3);
dest.col0 = col0;
dest.col1 = col1;
dest.col2 = col2;
dest.col3 = col3;
}
VECTORCALL VECMATH_FINLINE void v_mat44_mul43(mat44f &dest, mat44f_cref m1, mat43f_cref m2)
{
vec4f xxxx, yyyy, zzzz;
#define SPLAT_MAT_N_COL0(N, COL) \
xxxx = v_splat_##N(m2.row0); yyyy = v_splat_##N(m2.row1); zzzz = v_splat_##N(m2.row2);\
vec4f COL = v_add(v_add(v_mul(xxxx, m1.col0), v_mul(yyyy, m1.col1)), v_mul(zzzz, m1.col2));
#define SPLAT_MAT_N_COL1(N, COL) \
xxxx = v_splat_##N(m2.row0); yyyy = v_splat_##N(m2.row1); zzzz = v_splat_##N(m2.row2);\
vec4f COL = v_add(v_add(v_mul(xxxx, m1.col0), v_mul(yyyy, m1.col1)), v_add(v_mul(zzzz, m1.col2), m1.col3));
SPLAT_MAT_N_COL0(x, col0)
SPLAT_MAT_N_COL0(y, col1)
SPLAT_MAT_N_COL0(z, col2)
SPLAT_MAT_N_COL1(w, col3)
dest.col0 = col0;
dest.col1 = col1;
dest.col2 = col2;
dest.col3 = col3;
#undef SPLAT_MAT_N_COL0
#undef SPLAT_MAT_N_COL1
}
VECTORCALL VECMATH_FINLINE void v_mat44_mul33r(mat44f &dest, mat44f_cref m1, mat44f_cref m2)
{
vec4f col0 = v_mat44_mul_vec3v(m1, m2.col0);
vec4f col1 = v_mat44_mul_vec3v(m1, m2.col1);
vec4f col2 = v_mat44_mul_vec3v(m1, m2.col2);
dest.col0 = col0;
dest.col1 = col1;
dest.col2 = col2;
dest.col3 = m1.col3;
}
VECTORCALL VECMATH_FINLINE void v_mat44_mul33(mat44f &dest, mat44f_cref m1, mat33f_cref m2)
{
vec4f col0 = v_mat44_mul_vec3v(m1, m2.col0);
vec4f col1 = v_mat44_mul_vec3v(m1, m2.col1);
vec4f col2 = v_mat44_mul_vec3v(m1, m2.col2);
dest.col0 = col0;
dest.col1 = col1;
dest.col2 = col2;
dest.col3 = m1.col3;
}
VECTORCALL VECMATH_FINLINE void v_mat33_mul(mat33f &dest, mat33f_cref m1, mat33f_cref m2)
{
vec4f col0 = v_mat33_mul_vec3(m1, m2.col0);
vec4f col1 = v_mat33_mul_vec3(m1, m2.col1);
vec4f col2 = v_mat33_mul_vec3(m1, m2.col2);
dest.col0 = col0;
dest.col1 = col1;
dest.col2 = col2;
}
VECTORCALL VECMATH_FINLINE void v_mat33_mul33r(mat33f &dest, mat33f_cref m1, mat44f_cref m2)
{
vec4f col0 = v_mat33_mul_vec3(m1, m2.col0);
vec4f col1 = v_mat33_mul_vec3(m1, m2.col1);
vec4f col2 = v_mat33_mul_vec3(m1, m2.col2);
dest.col0 = col0;
dest.col1 = col1;
dest.col2 = col2;
}
VECTORCALL VECMATH_FINLINE void v_mat33_orthonormalize(mat33f &dest, mat33f_cref m)
{
vec4f c2 = v_norm3(v_cross3(m.col0, m.col1));
vec4f c1 = v_norm3(v_cross3(c2, m.col0));
vec4f c0 = v_norm3(v_cross3(c1, c2));
dest.col2 = c2;
dest.col1 = c1;
dest.col0 = c0;
}
VECTORCALL VECMATH_FINLINE void v_mat44_orthonormalize33(mat44f &dest, mat44f_cref m)
{
vec4f c2 = v_norm3(v_cross3(m.col0, m.col1));
vec4f c1 = v_norm3(v_cross3(c2, m.col0));
vec4f c0 = v_norm3(v_cross3(c1, c2));
dest.col2 = c2;
dest.col1 = c1;
dest.col0 = c0;
}
VECTORCALL VECMATH_FINLINE void v_mat33_orthonormal_inverse(mat33f &dest, mat33f_cref m)
{
v_mat33_transpose(dest, m);
}
VECTORCALL VECMATH_FINLINE void v_mat44_orthonormal_inverse43(mat44f &dest, mat44f_cref m)
{
mat33f m3;
v_mat44_transpose_to_mat33(m3, m.col0, m.col1, m.col2, v_zero());
dest.set33(m3);
dest.col3 = v_neg(v_mat44_mul_vec3v(dest, m.col3));
}
VECTORCALL VECMATH_FINLINE void v_mat44_orthonormal_inverse43_to44(mat44f &dest, mat44f_cref m)
{
mat33f m3;
v_mat44_transpose_to_mat33(m3, m.col0, m.col1, m.col2, v_zero());
dest.set33(m3);
dest.col3 = v_perm_xyzd(v_neg(v_mat44_mul_vec3v(dest, m.col3)), V_C_UNIT_0001);
}
VECTORCALL VECMATH_FINLINE vec4f v_mat33_det(mat33f_cref m)
{
return v_dot3(m.col2, v_cross3(m.col0, m.col1));
}
VECTORCALL VECMATH_FINLINE void v_mat44_inverse43(mat44f &dest, mat44f_cref m)
{
mat33f m3;
m3.col0 = m.col0;
m3.col1 = m.col1;
m3.col2 = m.col2;
v_mat33_inverse(m3, m3);
dest.set33(m3);
dest.col3 = v_neg(v_mat44_mul_vec3v(dest, m.col3));
}
VECTORCALL VECMATH_FINLINE void v_mat44_inverse43_to44(mat44f &dest, mat44f_cref m)
{
mat33f m3;
m3.col0 = m.col0;
m3.col1 = m.col1;
m3.col2 = m.col2;
v_mat33_inverse(m3, m3);
dest.set33(m3);
vec4f zero = v_zero();
dest.col0 = v_perm_xyzd(dest.col0, zero);
dest.col1 = v_perm_xyzd(dest.col1, zero);
dest.col2 = v_perm_xyzd(dest.col2, zero);
dest.col3 = v_perm_xyzd(v_neg(v_mat44_mul_vec3v(dest, m.col3)), V_C_UNIT_0001);
}
VECTORCALL VECMATH_FINLINE vec4f v_mat44_det43(mat44f_cref m)
{
return v_dot3(m.col2, v_cross3(m.col0, m.col1));
}
VECTORCALL VECMATH_FINLINE vec4f v_mat44_max_scale43_sq(mat44f_cref tm)
{
vec4f xScaleSq = v_length3_sq(tm.col0);
vec4f yScaleSq = v_length3_sq(tm.col1);
vec4f zScaleSq = v_length3_sq(tm.col2);
return v_max(xScaleSq, v_max(yScaleSq, zScaleSq));
}
VECTORCALL VECMATH_FINLINE vec4f v_mat44_max_scale43(mat44f_cref tm)
{
return v_sqrt(v_mat44_max_scale43_sq(tm));
}
VECTORCALL VECMATH_FINLINE vec4f v_mat44_max_scale43_x(mat44f_cref tm)
{
vec4f xScaleSq = v_length3_sq_x(tm.col0);
vec4f yScaleSq = v_length3_sq_x(tm.col1);
vec4f zScaleSq = v_length3_sq_x(tm.col2);
return v_sqrt_x(v_max(xScaleSq, v_max(yScaleSq, zScaleSq)));
}
VECTORCALL VECMATH_FINLINE vec3f v_mat44_scale43_sq(mat44f_cref tm)
{
vec4f xScaleSq = v_length3_sq(tm.col0);
vec4f yScaleSq = v_length3_sq(tm.col1);
vec4f zScaleSq = v_length3_sq(tm.col2);
return v_perm_xzac(v_perm_xycd(xScaleSq, yScaleSq), zScaleSq);
}
VECTORCALL VECMATH_FINLINE vec4f v_mat44_mul_bsph(mat44f_cref m, vec4f bsph)
{
return v_perm_xyzd(v_mat44_mul_vec3p(m, bsph), v_mul(bsph, v_mat44_max_scale43(m)));
}
VECTORCALL VECMATH_FINLINE void v_mat44_mul_bsph(mat44f_cref m, vec4f &bsph_pos, vec4f &bsph_rad_x)
{
bsph_pos = v_mat44_mul_vec3p(m, bsph_pos);
bsph_rad_x = v_mul_x(bsph_rad_x, v_mat44_max_scale43_x(m));
}
VECTORCALL VECMATH_FINLINE mat44f v_mat44_lerp(vec4f tttt, mat44f_cref a, mat44f_cref b)
{
quat4f aq = v_quat_from_mat43(a);
quat4f bq = v_quat_from_mat43(b);
quat4f rq = v_quat_slerp(tttt, aq, bq);
vec3f rpos = v_lerp_vec4f(tttt, a.col3, b.col3);
mat44f tm;
v_mat44_from_quat(tm, rq, rpos);
return tm;
}
VECTORCALL VECMATH_FINLINE void v_bbox3_init_empty(bbox3f &b)
{
b.bmin = V_C_MAX_VAL;
b.bmax = v_sub(v_zero(), b.bmin);
}
VECTORCALL VECMATH_FINLINE void v_bbox3_init_ident(bbox3f &b)
{
b.bmin = v_neg(V_C_HALF);
b.bmax = V_C_HALF;
}
VECTORCALL VECMATH_FINLINE void v_bbox3_init(bbox3f &b, vec3f p) { b.bmin = b.bmax = p; }
VECTORCALL VECMATH_FINLINE void v_bbox3_init(bbox3f &b, mat44f_cref m, bbox3f b2)
{
// What we're doing here is this:
// xxxx*m0 + yyyy*m1 + zzzz*m2 + m3
// xxxx*m0 + yyyy*m1 + ZZZZ*m2 + m3
// xxxx*m0 + YYYY*m1 + zzzz*m2 + m3
// xxxx*m0 + YYYY*m1 + ZZZZ*m2 + m3
// XXXX*m0 + yyyy*m1 + zzzz*m2 + m3
// XXXX*m0 + yyyy*m1 + ZZZZ*m2 + m3
// XXXX*m0 + YYYY*m1 + zzzz*m2 + m3
// XXXX*m0 + YYYY*m1 + ZZZZ*m2 + m3
// Which we don't need to do at all as we just need to calculate 1/4 of this first and then summ up
vec4f boxMulM_0_0 = v_mul(v_splat_x(b2.bmin), m.col0);
vec4f boxMulM_0_1 = v_mul(v_splat_x(b2.bmax), m.col0);
vec4f boxMulM_1_0 = v_mul(v_splat_y(b2.bmin), m.col1);
vec4f boxMulM_1_1 = v_mul(v_splat_y(b2.bmax), m.col1);
vec4f boxMulM_2_0 = v_mul(v_splat_z(b2.bmin), m.col2);
vec4f boxMulM_2_1 = v_mul(v_splat_z(b2.bmax), m.col2);
// Summing y and z
vec4f boxSum_0_0 = v_add(boxMulM_1_0, boxMulM_2_0);
vec4f boxSum_0_1 = v_add(boxMulM_1_0, boxMulM_2_1);
vec4f boxSum_1_0 = v_add(boxMulM_1_1, boxMulM_2_0);
vec4f boxSum_1_1 = v_add(boxMulM_1_1, boxMulM_2_1);
#define COMBINE_BOX(a,b,c) v_add(boxMulM_0_##a, boxSum_##b##_##c)
v_bbox3_init( b, COMBINE_BOX(0, 0, 0));
v_bbox3_add_pt(b, COMBINE_BOX(0, 0, 1));
v_bbox3_add_pt(b, COMBINE_BOX(0, 1, 0));
v_bbox3_add_pt(b, COMBINE_BOX(0, 1, 1));
v_bbox3_add_pt(b, COMBINE_BOX(1, 0, 0));
v_bbox3_add_pt(b, COMBINE_BOX(1, 0, 1));
v_bbox3_add_pt(b, COMBINE_BOX(1, 1, 0));
v_bbox3_add_pt(b, COMBINE_BOX(1, 1, 1));
#undef COMBINE_BOX
b.bmin = v_add(b.bmin, m.col3);
b.bmax = v_add(b.bmax, m.col3);
}
VECTORCALL VECMATH_FINLINE void v_bbox3_init_by_bsph(bbox3f &b, vec3f bsph_center, vec3f bsph_radius)
{
b.bmin = v_sub(bsph_center, bsph_radius);
b.bmax = v_add(bsph_center, bsph_radius);
}
VECTORCALL VECMATH_FINLINE void v_bbox3_init_by_ray(bbox3f &b, vec3f from, vec3f dir, vec4f len)
{
v_bbox3_init_by_segment(b, from, v_madd(dir, len, from));
}
VECTORCALL VECMATH_FINLINE void v_bbox3_init_by_segment(bbox3f &b, vec4f from, vec4f to)
{
b.bmin = v_min(from, to);
b.bmax = v_max(from, to);
}
//return all mask if empty
VECTORCALL VECMATH_FINLINE vec3f v_bbox3_center(bbox3f b)
{
return v_mul(v_add(b.bmin, b.bmax), V_C_HALF);
}
VECTORCALL VECMATH_FINLINE vec4f v_bbox3_outer_rad(bbox3f b)
{
return v_mul_x(V_C_HALF, v_length3_x(v_bbox3_size(b)));
}
VECTORCALL VECMATH_FINLINE vec4f v_bbox3_inner_diameter(bbox3f b)
{
return v_hmin3(v_bbox3_size(b));
}
VECTORCALL VECMATH_FINLINE vec4f v_bbox3_inner_rad(bbox3f b)
{
return v_mul_x(V_C_HALF, v_bbox3_inner_diameter(b));
}
VECTORCALL VECMATH_FINLINE vec4f v_bbox3_outer_rad_from_zero(bbox3f b)
{
return v_length3_x(v_max(v_neg(b.bmin), b.bmax));
}
VECTORCALL VECMATH_FINLINE vec3f v_bbox3_point(bbox3f b, int idx)
{
return v_sel(b.bmin, b.bmax, v_make_vec4f_mask(uint8_t(idx)));
}
VECTORCALL VECMATH_FINLINE void v_bbox3_add_pt(bbox3f &b, vec3f p)
{
b.bmin = v_min(b.bmin, p);
b.bmax = v_max(b.bmax, p);
}
VECTORCALL VECMATH_FINLINE void v_bbox3_add_box(bbox3f &b, bbox3f b2)
{
b.bmin = v_min(b.bmin, b2.bmin);
b.bmax = v_max(b.bmax, b2.bmax);
}
VECTORCALL VECMATH_FINLINE void v_bbox3_add_transformed_box(bbox3f &b, mat44f_cref m, bbox3f b2)
{
bbox3f temp;
v_bbox3_init(temp, m, b2);
v_bbox3_add_box(b, temp);
}
VECTORCALL VECMATH_FINLINE void v_bbox3_add_ray(bbox3f &b, vec3f from, vec3f dir, vec4f len)
{
v_bbox3_add_pt(b, from);
v_bbox3_add_pt(b, v_madd(dir, len, from));
}
VECTORCALL VECMATH_FINLINE bbox3f v_bbox3_sum(bbox3f b1, bbox3f b2)
{
bbox3f b;
b.bmin = v_min(b1.bmin, b2.bmin);
b.bmax = v_max(b1.bmax, b2.bmax);
return b;
}
VECTORCALL VECMATH_FINLINE bbox3f v_bbox3_sel(bbox3f b1, bbox3f b2, vec4f c)
{
bbox3f b;
b.bmin = v_sel(b1.bmin, b2.bmin, c);
b.bmax = v_sel(b1.bmax, b2.bmax, c);
return b;
}
VECTORCALL VECMATH_FINLINE vec3f v_bbox3_size(bbox3f b) { return v_sub(b.bmax, b.bmin); }
VECTORCALL VECMATH_FINLINE vec3f v_bbox3_max_size(bbox3f b)
{
return v_hmax3(v_bbox3_size(b));
}
VECTORCALL VECMATH_FINLINE bbox3f v_bbox3_scale(bbox3f b, vec4f size_factor)
{
vec3f center = v_bbox3_center(b);
return bbox3f
{
v_lerp_vec4f(size_factor, center, b.bmin),
v_lerp_vec4f(size_factor, center, b.bmax)
};
}
VECTORCALL VECMATH_FINLINE bool v_bbox3_is_empty(bbox3f bbox)
{
vec4f invalid = v_cmp_gt(bbox.bmin, bbox.bmax);
return v_check_xyz_any_not_zeroi(invalid);
}
VECTORCALL VECMATH_FINLINE bool v_bbox3_test_pt_inside(bbox3f b, vec3f p)
{
vec3f inside = v_and(v_cmp_ge(p, b.bmin), v_cmp_ge(b.bmax, p));
return v_check_xyz_all_not_zeroi(inside);
}
VECTORCALL VECMATH_FINLINE bool v_bbox3_test_box_inside(bbox3f b1, bbox3f b2)
{
vec3f insideMin = v_and(v_cmp_ge(b2.bmin, b1.bmin), v_cmp_ge(b1.bmax, b2.bmin));
vec3f insideMax = v_and(v_cmp_ge(b2.bmax, b1.bmin), v_cmp_ge(b1.bmax, b2.bmax));
return v_check_xyz_all_not_zeroi(v_and(insideMin, insideMax));
}
VECTORCALL VECMATH_FINLINE bool v_bbox3_test_box_intersect(bbox3f b1, bbox3f b2)
{
vec3f m = v_and(v_cmp_ge(b2.bmax, b1.bmin), v_cmp_ge(b1.bmax, b2.bmin));
return v_check_xyz_all_not_zeroi(m);
}
VECTORCALL VECMATH_FINLINE bool v_bbox3_test_box_intersect_safe(bbox3f b1, bbox3f b2)
{
vec3f m = v_and(v_and(v_cmp_ge(b1.bmax, b1.bmin), v_cmp_ge(b2.bmax, b2.bmin)),
v_and(v_cmp_ge(b2.bmax, b1.bmin), v_cmp_ge(b1.bmax, b2.bmin)));
return v_check_xyz_all_not_zeroi(m);
}
VECTORCALL VECMATH_FINLINE bool v_bbox3_test_pt_inside_xz(bbox3f b, vec3f p)
{
vec3f m = v_and(v_cmp_ge(p, b.bmin), v_cmp_ge(b.bmax, p));
return !v_test_vec_x_eqi_0(v_and(m, v_splat_z(m)));
}
// Checking only planes intersection! You need to test AvsB + BvsA and check inside case for each.
VECTORCALL inline bool v_bbox3_test_trasformed_box_intersect_no_check(bbox3f box0, bbox3f box1, const mat44f& tm1)
{
vec3f msbit = v_msbit();
vec3f elemMask0 = v_cast_vec4f(v_seti_x(-1));
// testing intersection of 12 edges of box1 with 3 orthogonal planes of box0
for (int i = 0; i < 3; i++)
{
vec3f point0 = v_sel(v_zero(), box1.bmin, elemMask0);
vec3f point1 = v_sel(v_zero(), box1.bmax, elemMask0);
vec3f elemMask1 = v_perm_xycd(v_perm_xaxa(v_cmp_eq(elemMask0, v_zero()), v_xor(elemMask0, v_zero())), v_zero()); // take Y on pass 0, and X on others
vec3f elemMask2 = v_cmp_eq(v_or(elemMask0, elemMask1), v_zero());
elemMask0 = v_rot_3(elemMask0);
for (int j = 0; j < 2; j++)
{
vec3f box1LimJ = v_sel(box1.bmax, box1.bmin, v_cast_vec4f(v_splatsi(j - 1)));
point0 = v_sel(point0, box1LimJ, elemMask1);
point1 = v_sel(point1, box1LimJ, elemMask1);
for (int k = 0; k < 2; k++)
{
vec3f box1LimK = v_sel(box1.bmax, box1.bmin, v_cast_vec4f(v_splatsi(k - 1)));
point0 = v_sel(point0, box1LimK, elemMask2);
point1 = v_sel(point1, box1LimK, elemMask2);
// test segment point0-point1 against 3 orthogonal planes of box0
vec3f point0l = v_mat44_mul_vec3p(tm1, point0);
vec3f point1l = v_mat44_mul_vec3p(tm1, point1);
vec3f dir = v_sub(point1l, point0l);
vec3f dirGE0 = v_cmp_ge(dir, v_zero());
vec3f depth = v_sel(v_sub(point0l, box0.bmax),
v_sub(box0.bmin, point0l),
dirGE0);
vec3f length = v_abs(dir);
vec3f width = v_bbox3_size(box0);
vec3f nwidth = v_xor(width, msbit);
vec3f selectPi1 = v_and(v_cmp_gt(depth, v_zero()),
v_cmp_lt(depth, length));
vec3f selectPi2 = v_and(v_cmp_gt(depth, nwidth),
v_cmp_lt(v_sub(depth, length), nwidth));
vec3f validMask = v_or(selectPi1, selectPi2);
if (v_check_xyz_all_zeroi(validMask))
continue;
vec3f selectMask = v_xor(selectPi1, dirGE0);
vec3f lim = v_sel(box0.bmin, box0.bmax, selectMask);
vec3f t = v_div(v_sub(lim, point0l), v_and(dir, validMask)); // gen NaN's for invalid
vec3f px = v_sub(v_lerp_vec4f(v_splat_x(t), point0l, point1l),
v_sel(box0.bmin, box0.bmax, v_splat_x(selectMask)));
vec3f py = v_sub(v_lerp_vec4f(v_splat_y(t), point0l, point1l),
v_sel(box0.bmin, box0.bmax, v_splat_y(selectMask)));
vec3f pz = v_sub(v_lerp_vec4f(v_splat_z(t), point0l, point1l),
v_sel(box0.bmin, box0.bmax, v_splat_z(selectMask)));
vec3f selectMaskSign = v_and(selectMask, msbit);
vec4f pxpy = v_perm_xycd(v_perm_yzxy(px), v_perm_xzxz(py)); // yzac
pxpy = v_xor(pxpy, v_perm_xxyy(selectMaskSign));
pz = v_xor(pz, v_splat_z(selectMaskSign));
vec4f crossXY = v_and(v_cmp_ge(pxpy, v_zero()),
v_cmp_le(pxpy, v_perm_xycd(v_perm_yzxy(width), v_perm_xzxz(width)))); // yzxz
vec4f crossZ = v_and(v_cmp_ge(pz, v_zero()),
v_cmp_le(pz, width));
uint8_t signMask = uint8_t(v_signmask(crossXY) | (v_signmask(crossZ) << 4));
if (signMask & (signMask >> 1) & (1 << 0 | 1 << 2 | 1 << 4))
return true;
}
}
}
return false;
}
VECTORCALL inline bool v_bbox3_test_trasformed_box_intersect(bbox3f box0, bbox3f box1, const mat44f& tm1)
{
// fast check for box1 completely inside box0, or not even roughly intersects
bbox3f box1AABB;
v_bbox3_init(box1AABB, tm1, box1);
if (!v_bbox3_test_box_intersect(box0, box1AABB)) // not even intersecting
return false;
if (v_bbox3_test_box_inside(box0, box1AABB)) // fully inside
return true;
return v_bbox3_test_trasformed_box_intersect_no_check(box0, box1, tm1);
}
VECTORCALL VECMATH_FINLINE bool v_bbox3_test_trasformed_box_intersect(bbox3f box0, const mat44f& tm0, bbox3f box1, const mat44f& tm1,
vec4f size_factor)
{
// validate
vec3f width0 = v_bbox3_size(box0);
vec3f width1 = v_bbox3_size(box1);
if (v_signmask(v_or(width0, width1)) & (1 | 2 | 4)) // any of dimensions is negative
return false;
// check boundings don't intersect
vec4f box0max = v_max(v_abs(box0.bmin), v_abs(box0.bmax));
vec4f box1max = v_max(v_abs(box1.bmin), v_abs(box1.bmax));
vec4f r0 = v_length3_x(v_mat44_mul_vec3p(tm0, box0max));
vec4f r1 = v_length3_x(v_mat44_mul_vec3p(tm1, box1max));
vec4f r = v_mul_x(v_add_x(r0, r1), size_factor);
vec4f distSq = v_length3_sq_x(v_sub(tm1.col3, tm0.col3));
if (v_test_vec_x_gt(distSq, v_mul_x(r, r)))
return false;
// bbox intersection check
mat44f tm;
v_mat44_inverse43(tm, tm0);
v_mat44_mul43(tm, tm, tm1);
if (v_bbox3_test_trasformed_box_intersect(v_bbox3_scale(box0, size_factor), box1, tm))
return true;
v_mat44_inverse43(tm, tm1);
v_mat44_mul43(tm, tm, tm0);
if (v_bbox3_test_trasformed_box_intersect(v_bbox3_scale(box1, size_factor), box0, tm)) //-V764 box1, box order is correct
return true;
return false;
}
VECTORCALL VECMATH_FINLINE bool v_bbox3_test_trasformed_box_intersect(bbox3f box0, const mat44f& tm0, bbox3f box1, const mat44f& tm1)
{
return v_bbox3_test_trasformed_box_intersect(box0, tm0, box1, tm1, V_C_ONE);
}
VECTORCALL VECMATH_FINLINE bool v_bbox3_test_trasformed_box_intersect_rel_tm(bbox3f box0, const mat44f& b0_to_b1,
bbox3f box1, const mat44f& b1_to_b0)
{
bbox3f box1inb0;
v_bbox3_init(box1inb0, b1_to_b0, box1);
if (!v_bbox3_test_box_intersect(box0, box1inb0)) // not even intersecting
return false;
if (v_bbox3_test_box_inside(box0, box1inb0)) // fully inside
return true;
bbox3f box0inb1;
v_bbox3_init(box0inb1, b0_to_b1, box0);
if (!v_bbox3_test_box_intersect(box1, box0inb1)) // not even intersecting
return false;
if (v_bbox3_test_box_inside(box1, box0inb1)) // fully inside
return true;
if (v_bbox3_test_trasformed_box_intersect_no_check(box0, box1, b1_to_b0))
return true;
if (v_bbox3_test_trasformed_box_intersect_no_check(box1, box0, b0_to_b1)) //-V764 box1, box0 order is correct
return true;
return false;
}
VECTORCALL VECMATH_FINLINE bbox3f v_bbox3_get_box_intersection(bbox3f box0, bbox3f box1)
{
return bbox3f { v_max(box0.bmin, box1.bmin), v_min(box0.bmax, box1.bmax) };
}
VECTORCALL VECMATH_FINLINE bool v_bbox3_test_sph_intersect(bbox3f box, vec4f bsph_center, vec4f bsph_r2_x)
{
vec4f distSq = v_length3_sq_x(v_add(v_max(v_sub(box.bmin, bsph_center), v_zero()),