-
Notifications
You must be signed in to change notification settings - Fork 3
/
llighttests_simd.cpp
815 lines (605 loc) · 21.1 KB
/
llighttests_simd.cpp
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
#include "llighttests_simd.h"
namespace LM {
#ifdef LLIGHTMAPPER_USE_SIMD
const __m128 oneM128 = _mm_set1_ps(1.0f);
const __m128 minusOneM128 = _mm_set1_ps(-1.0f);
const __m128 positiveEpsilonM128 = _mm_set1_ps(0.000001f);
const __m128 negativeEpsilonM128 = _mm_set1_ps(-0.000001f);
const __m128 zeroM128 = _mm_set1_ps(0.0f);
//const u_m128 ref_oneM128 = {1.0f, 1.0f, 1.0f, 1.0f};
//const u_m128 ref_minusOneM128 = {-1.0f, -1.0f, -1.0f, -1.0f};
//const u_m128 ref_positiveEpsilonM128 = {0.000001f, 0.000001f, 0.000001f, 0.000001f};
//const u_m128 ref_negativeEpsilonM128 = {-0.000001f, -0.000001f, -0.000001f, -0.000001f};
//const u_m128 ref_zeroM128 = {0.0f, 0.0f, 0.0f, 0.0f};
void multi_cross(__m128 result[3], const __m128 a[3], const __m128 b[3])
{
__m128 tmp;
__m128 tmp2;
tmp = _mm_mul_ps(a[1], b[2]);
tmp2 = _mm_mul_ps(b[1], a[2]);
result[0] = _mm_sub_ps(tmp, tmp2);
tmp = _mm_mul_ps(a[2], b[0]);
tmp2 = _mm_mul_ps(b[2], a[0]);
result[1] = _mm_sub_ps(tmp, tmp2);
tmp = _mm_mul_ps(a[0], b[1]);
tmp2 = _mm_mul_ps(b[0], a[1]);
result[2] = _mm_sub_ps(tmp, tmp2);
}
__m128 multi_dot(const __m128 a[3], const __m128 b[3])
{
__m128 tmp = _mm_mul_ps(a[0], b[0]);
__m128 tmp2 = _mm_mul_ps(a[1], b[1]);
__m128 tmp3 = _mm_mul_ps(a[2], b[2]);
return _mm_add_ps(tmp, _mm_add_ps(tmp2, tmp3));
// return fmadd(a[2], b[2], fmadd(a[1], b[1], mul(a[0], b[0])));
}
void multi_sub(__m128 result[3], const __m128 a[3], const __m128 b[3])
{
result[0] = _mm_sub_ps(a[0], b[0]);
result[1] = _mm_sub_ps(a[1], b[1]);
result[2] = _mm_sub_ps(a[2], b[2]);
}
void PackedRay::Create(const Ray &ray)
{
m_origin[0].mm128 = _mm_set1_ps(ray.o.x);
m_origin[1].mm128 = _mm_set1_ps(ray.o.y);
m_origin[2].mm128 = _mm_set1_ps(ray.o.z);
m_direction[0].mm128 = _mm_set1_ps(ray.d.x);
m_direction[1].mm128 = _mm_set1_ps(ray.d.y);
m_direction[2].mm128 = _mm_set1_ps(ray.d.z);
}
// returns true if a hit
bool PackedRay::IntersectTest_CullBackFaces(const PackedTriangles& packedTris, float max_dist) const
{
//Begin calculating determinant - also used to calculate u parameter
// P
__m128 q[3];
multi_cross(q, &m_direction[0].mm128, &packedTris.e2[0].mm128);
//if determinant is near zero, ray lies in plane of triangle
// det
__m128 a = multi_dot(&packedTris.e1[0].mm128, q);
// reject based on det being close to zero
// NYI
// inv_det
__m128 f = _mm_div_ps(oneM128, a);
// distance from v1 to ray origin
// T
__m128 s[3];
multi_sub(s, &m_origin[0].mm128, &packedTris.v0[0].mm128);
// Calculate u parameter and test bound
__m128 u = _mm_mul_ps(f, multi_dot(s, q));
// the intersection lies outside triangle
// NYI
// Prepare to test v parameter
// Q
__m128 r[3];
multi_cross(r, s, &packedTris.e1[0].mm128);
// calculate V parameter and test bound
// v
__m128 v = _mm_mul_ps(f, multi_dot(&m_direction[0].mm128, r));
// intersection outside of triangles?
// NYI
// t
__m128 t = _mm_mul_ps(f, multi_dot(&packedTris.e2[0].mm128, r));
// if t > epsilon, hit
/////////////////////////////////////////
// back face culling.
// calculate face normal (not normalized)
__m128 face_normals[3];
multi_cross(face_normals, &packedTris.e2[0].mm128, &packedTris.e1[0].mm128);
// dot ray direction
__m128 ray_dot_normal = multi_dot(&m_direction[0].mm128, face_normals);
/////////////////////////////////////////
// Failure conditions
// determinant close to zero?
__m128 nohit = _mm_and_ps(_mm_cmpge_ps(a, negativeEpsilonM128), _mm_cmple_ps(a, positiveEpsilonM128));
nohit = _mm_or_ps(nohit, _mm_cmple_ps(u, zeroM128));
nohit = _mm_or_ps(nohit, _mm_cmple_ps(v, zeroM128));
nohit = _mm_or_ps(nohit, _mm_cmpge_ps(_mm_add_ps(u, v), oneM128));
nohit = _mm_or_ps(nohit, _mm_cmple_ps(t, zeroM128));
// failed = _mm_or_ps(failed, _mm_cmpge_ps(t, m_length));
nohit = _mm_or_ps(nohit, packedTris.inactiveMask.mm128);
// test against minimum t .. not sure if this will speed up or not.
__m128 m_max_dist = _mm_set1_ps(max_dist);
nohit = _mm_or_ps(nohit, _mm_cmpge_ps(t, m_max_dist));
// backface culling - THE BIT OPERATION IS OPPOSITE.
// We want to PREVENT failing if the backface is hit.
__m128 backface_mask = _mm_cmpge_ps(ray_dot_normal, zeroM128);
// always let through the bits set in backface mask
nohit = _mm_or_ps(nohit, backface_mask);
int mask = _mm_movemask_ps(nohit);
if (mask != 15) // first 4 bits set
{
return true;
} // mask
return false;
}
// returns true if a hit
bool PackedRay::IntersectTest(const PackedTriangles& packedTris, float max_dist) const
{
//Begin calculating determinant - also used to calculate u parameter
// P
__m128 q[3];
multi_cross(q, &m_direction[0].mm128, &packedTris.e2[0].mm128);
//if determinant is near zero, ray lies in plane of triangle
// det
__m128 a = multi_dot(&packedTris.e1[0].mm128, q);
// reject based on det being close to zero
// NYI
// inv_det
__m128 f = _mm_div_ps(oneM128, a);
// distance from v1 to ray origin
// T
__m128 s[3];
multi_sub(s, &m_origin[0].mm128, &packedTris.v0[0].mm128);
// Calculate u parameter and test bound
__m128 u = _mm_mul_ps(f, multi_dot(s, q));
// the intersection lies outside triangle
// NYI
// Prepare to test v parameter
// Q
__m128 r[3];
multi_cross(r, s, &packedTris.e1[0].mm128);
// calculate V parameter and test bound
// v
__m128 v = _mm_mul_ps(f, multi_dot(&m_direction[0].mm128, r));
// intersection outside of triangles?
// NYI
// t
__m128 t = _mm_mul_ps(f, multi_dot(&packedTris.e2[0].mm128, r));
// if t > epsilon, hit
// Failure conditions
// determinant close to zero?
__m128 failed = _mm_and_ps(_mm_cmpge_ps(a, negativeEpsilonM128), _mm_cmple_ps(a, positiveEpsilonM128));
failed = _mm_or_ps(failed, _mm_cmple_ps(u, zeroM128));
failed = _mm_or_ps(failed, _mm_cmple_ps(v, zeroM128));
failed = _mm_or_ps(failed, _mm_cmpge_ps(_mm_add_ps(u, v), oneM128));
failed = _mm_or_ps(failed, _mm_cmple_ps(t, zeroM128));
// failed = _mm_or_ps(failed, _mm_cmpge_ps(t, m_length));
failed = _mm_or_ps(failed, packedTris.inactiveMask.mm128);
// test against minimum t .. not sure if this will speed up or not.
__m128 m_max_dist = _mm_set1_ps(max_dist);
failed = _mm_or_ps(failed, _mm_cmpge_ps(t, m_max_dist));
//bool bHit = false;
int mask = _mm_movemask_ps(failed);
if (mask != 15) // first 4 bits set
{
return true;
} // mask
return false;
}
/*
// returns winner index +1, or zero if no hit
int PackedRay::Intersect_TESTREF(const PackedTriangles& packedTris, float &nearest_dist) const
{
//Begin calculating determinant - also used to calculate u parameter
// P
u_m128 q[3];
u_m128::multi_cross(q, &m_direction[0], &packedTris.e2[0]);
__m128 SIMD_q[3];
multi_cross(SIMD_q, &m_direction[0].mm128, &packedTris.e2[0].mm128);
//if determinant is near zero, ray lies in plane of triangle
// det
u_m128 a = u_m128::multi_dot(&packedTris.e1[0], q);
__m128 SIMD_a = multi_dot(&packedTris.e1[0].mm128, SIMD_q);
// reject based on det being close to zero
// NYI
// inv_det
u_m128 f = u_m128::div_ps(ref_oneM128, a);
__m128 SIMD_f = _mm_div_ps(oneM128, SIMD_a);
// distance from v1 to ray origin
// T
u_m128 s[3];
u_m128::multi_sub(s, &m_origin[0], &packedTris.v0[0]);
__m128 SIMD_s[3];
multi_sub(SIMD_s, &m_origin[0].mm128, &packedTris.v0[0].mm128);
// Calculate u parameter and test bound
u_m128 u;
u_m128 dot_sq;
dot_sq = u_m128::multi_dot(s, q);
u = u_m128::mul_ps(f, dot_sq);
__m128 SIMD_dot_sq = multi_dot(SIMD_s, SIMD_q);
__m128 SIMD_u = _mm_mul_ps(SIMD_f, SIMD_dot_sq);
// the intersection lies outside triangle
// NYI
// Prepare to test v parameter
// Q
u_m128 r[3];
u_m128::multi_cross(r, s, &packedTris.e1[0]);
__m128 SIMD_r[3];
multi_cross(SIMD_r, SIMD_s, &packedTris.e1[0].mm128);
// calculate V parameter and test bound
// v
u_m128 dot_dir_r;
u_m128 v;
dot_dir_r = u_m128::multi_dot(&m_direction[0], r);
v = u_m128::mul_ps(f, dot_dir_r);
__m128 SIMD_v = _mm_mul_ps(SIMD_f, multi_dot(&m_direction[0].mm128, SIMD_r));
// intersection outside of triangles?
// NYI
// t
u_m128 t, dot_edge_r;
dot_edge_r = u_m128::multi_dot(&packedTris.e2[0], r);
t = u_m128::mul_ps(f, dot_edge_r);
__m128 SIMD_t = _mm_mul_ps(SIMD_f, multi_dot(&packedTris.e2[0].mm128, SIMD_r));
// if t > epsilon, hit
// Failure conditions
// determinant close to zero?
u_m128 failed = u_m128::mm_and(u_m128::ref_cmpge_ps(a, ref_negativeEpsilonM128), u_m128::ref_cmple_ps(a, ref_positiveEpsilonM128));
__m128 SIMD_failed = _mm_and_ps(_mm_cmpge_ps(SIMD_a, negativeEpsilonM128), _mm_cmple_ps(SIMD_a, positiveEpsilonM128));
failed = u_m128::mm_or(failed, u_m128::ref_cmple_ps(u, ref_zeroM128));
failed = u_m128::mm_or(failed, u_m128::ref_cmple_ps(v, ref_zeroM128));
//failed = u_m128::mm_or(failed, u_m128::ref_cmpge_ps(v, ref_zeroM128));
SIMD_failed = _mm_or_ps(SIMD_failed, _mm_cmple_ps(SIMD_u, zeroM128));
SIMD_failed = _mm_or_ps(SIMD_failed, _mm_cmple_ps(SIMD_v, zeroM128));
failed = u_m128::mm_or(failed, u_m128::ref_cmpge_ps(u_m128::add_ps(u, v), ref_oneM128));
SIMD_failed = _mm_or_ps(SIMD_failed, _mm_cmpge_ps(_mm_add_ps(SIMD_u, SIMD_v), oneM128));
failed = u_m128::mm_or(failed, u_m128::ref_cmple_ps(t, ref_zeroM128));
// failed = _mm_or_ps(failed, _mm_cmpge_ps(t, m_length));
failed = u_m128::mm_or(failed, packedTris.inactiveMask);
// test against minimum t .. not sure if this will speed up or not.
// __m128 m_nearest_dist = _mm_set1_ps(nearest_dist);
// failed = _mm_or_ps(failed, _mm_cmpge_ps(t, m_nearest_dist));
//bool bHit = false;
int mask = failed.movemask_ps();
if (mask != 15) // first 4 bits set
{
// there is at least one winner, so no need to set this
int winner = 0;
float * pT = (float*) &t;
// use simplified way of getting results
uint32_t * pResults = (uint32_t*) &failed;
for (int n=0; n<4; n++)
{
// hit!
// if (!pResults[n] && n<packedTris.num_tris)
if (!pResults[n])
{
if (pT[n] < nearest_dist)
{
nearest_dist = pT[n];
//r_winner = n;
winner = n+1;
//bHit = true;
}
}
} // for n
return winner;
} // mask
return 0;
}
*/
// returns winner index +1, or zero if no hit
int PackedRay::Intersect(const PackedTriangles& packedTris, float &nearest_dist) const
{
//Begin calculating determinant - also used to calculate u parameter
// P
__m128 q[3];
multi_cross(q, &m_direction[0].mm128, &packedTris.e2[0].mm128);
//if determinant is near zero, ray lies in plane of triangle
// det
__m128 a = multi_dot(&packedTris.e1[0].mm128, q);
// reject based on det being close to zero
// NYI
// inv_det
__m128 f = _mm_div_ps(oneM128, a);
// distance from v1 to ray origin
// T
__m128 s[3];
multi_sub(s, &m_origin[0].mm128, &packedTris.v0[0].mm128);
// Calculate u parameter and test bound
__m128 u = _mm_mul_ps(f, multi_dot(s, q));
// the intersection lies outside triangle
// NYI
// Prepare to test v parameter
// Q
__m128 r[3];
multi_cross(r, s, &packedTris.e1[0].mm128);
// calculate V parameter and test bound
// v
__m128 v = _mm_mul_ps(f, multi_dot(&m_direction[0].mm128, r));
// intersection outside of triangles?
// NYI
// t
__m128 t = _mm_mul_ps(f, multi_dot(&packedTris.e2[0].mm128, r));
// if t > epsilon, hit
// Failure conditions
// determinant close to zero?
__m128 failed = _mm_and_ps(_mm_cmpge_ps(a, negativeEpsilonM128), _mm_cmple_ps(a, positiveEpsilonM128));
// __m128 failed = _mm_and_ps(
// _mm_cmp(a, negativeEpsilonM256, _CMP_GT_OQ),
// cmp(a, positiveEpsilonM256, _CMP_LT_OQ)
// );
failed = _mm_or_ps(failed, _mm_cmple_ps(u, zeroM128));
failed = _mm_or_ps(failed, _mm_cmple_ps(v, zeroM128));
failed = _mm_or_ps(failed, _mm_cmpge_ps(_mm_add_ps(u, v), oneM128));
failed = _mm_or_ps(failed, _mm_cmple_ps(t, zeroM128));
// failed = _mm_or_ps(failed, _mm_cmpge_ps(t, m_length));
failed = _mm_or_ps(failed, packedTris.inactiveMask.mm128);
// test against minimum t .. not sure if this will speed up or not.
// __m128 m_nearest_dist = _mm_set1_ps(nearest_dist);
// failed = _mm_or_ps(failed, _mm_cmpge_ps(t, m_nearest_dist));
//bool bHit = false;
int mask = _mm_movemask_ps(failed);
if (mask != 15) // first 4 bits set
{
// there is at least one winner, so no need to set this
int winner = 0;
float * pT = (float*) &t;
// use simplified way of getting results
uint32_t * pResults = (uint32_t*) &failed;
for (int n=0; n<4; n++)
{
// hit!
// if (!pResults[n] && n<packedTris.num_tris)
if (!pResults[n])
{
if (pT[n] < nearest_dist)
{
nearest_dist = pT[n];
//r_winner = n;
winner = n+1;
//bHit = true;
}
}
} // for n
return winner;
} // mask
return 0;
}
bool LightTests_SIMD::TestIntersect4_Packed(const PackedTriangles &ptris, const Ray &ray, float &r_nearest_t, int &r_winner) const
{
// simd ray
PackedRay pray;
pray.m_origin[0].mm128 = _mm_set1_ps(ray.o.x);
pray.m_origin[1].mm128 = _mm_set1_ps(ray.o.y);
pray.m_origin[2].mm128 = _mm_set1_ps(ray.o.z);
pray.m_direction[0].mm128 = _mm_set1_ps(ray.d.x);
pray.m_direction[1].mm128 = _mm_set1_ps(ray.d.y);
pray.m_direction[2].mm128 = _mm_set1_ps(ray.d.z);
r_winner = pray.Intersect(ptris, r_nearest_t);
if (r_winner)
{
r_winner--;
return true;
}
return false;
}
bool LightTests_SIMD::TestIntersect4(const Tri *tris[4], const Ray &ray, float &r_nearest_t, int &r_winner) const
{
return true;
}
#else // not SIMD
// REFERENCE IMPLEMENTATION (for non SSE2 CPUs)
const u_m128 ref_oneM128 = {{1.0f, 1.0f, 1.0f, 1.0f}};
const u_m128 ref_minusOneM128 = {{-1.0f, -1.0f, -1.0f, -1.0f}};
const u_m128 ref_positiveEpsilonM128 = {{0.000001f, 0.000001f, 0.000001f, 0.000001f}};
const u_m128 ref_negativeEpsilonM128 = {{-0.000001f, -0.000001f, -0.000001f, -0.000001f}};
const u_m128 ref_zeroM128 = {{0.0f, 0.0f, 0.0f, 0.0f}};
void PackedRay::Create(const Ray &ray)
{
m_origin[0] = u_m128::set1_ps(ray.o.x);
m_origin[1] = u_m128::set1_ps(ray.o.y);
m_origin[2] = u_m128::set1_ps(ray.o.z);
m_direction[0] = u_m128::set1_ps(ray.d.x);
m_direction[1] = u_m128::set1_ps(ray.d.y);
m_direction[2] = u_m128::set1_ps(ray.d.z);
}
// returns true if a hit
bool PackedRay::IntersectTest_CullBackFaces(const PackedTriangles& packedTris, float max_dist) const
{
//Begin calculating determinant - also used to calculate u parameter
// P
u_m128 q[3];
u_m128::multi_cross(q, &m_direction[0], &packedTris.e2[0]);
//if determinant is near zero, ray lies in plane of triangle
// det
u_m128 a = u_m128::multi_dot(&packedTris.e1[0], q);
// reject based on det being close to zero
// NYI
// inv_det
u_m128 f = u_m128::div_ps(ref_oneM128, a);
// distance from v1 to ray origin
// T
u_m128 s[3];
u_m128::multi_sub(s, &m_origin[0], &packedTris.v0[0]);
// Calculate u parameter and test bound
u_m128 u;
u_m128 dot_sq;
dot_sq = u_m128::multi_dot(s, q);
u = u_m128::mul_ps(f, dot_sq);
// the intersection lies outside triangle
// NYI
// Prepare to test v parameter
// Q
u_m128 r[3];
u_m128::multi_cross(r, s, &packedTris.e1[0]);
// calculate V parameter and test bound
// v
u_m128 dot_dir_r;
u_m128 v;
dot_dir_r = u_m128::multi_dot(&m_direction[0], r);
v = u_m128::mul_ps(f, dot_dir_r);
// intersection outside of triangles?
// NYI
// t
u_m128 t, dot_edge_r;
dot_edge_r = u_m128::multi_dot(&packedTris.e2[0], r);
t = u_m128::mul_ps(f, dot_edge_r);
// if t > epsilon, hit
/////////////////////////////////////////
// back face culling.
// calculate face normal (not normalized)
u_m128 face_normals[3];
u_m128::multi_cross(face_normals, &packedTris.e2[0], &packedTris.e1[0]);
// dot ray direction
u_m128 ray_dot_normal = u_m128::multi_dot(&m_direction[0], face_normals);
/////////////////////////////////////////
// Failure conditions
// determinant close to zero?
u_m128 failed = u_m128::mm_and(u_m128::ref_cmpge_ps(a, ref_negativeEpsilonM128), u_m128::ref_cmple_ps(a, ref_positiveEpsilonM128));
failed = u_m128::mm_or(failed, u_m128::ref_cmple_ps(u, ref_zeroM128));
failed = u_m128::mm_or(failed, u_m128::ref_cmple_ps(v, ref_zeroM128));
failed = u_m128::mm_or(failed, u_m128::ref_cmpge_ps(u_m128::add_ps(u, v), ref_oneM128));
failed = u_m128::mm_or(failed, u_m128::ref_cmple_ps(t, ref_zeroM128));
// failed = _mm_or_ps(failed, _mm_cmpge_ps(t, m_length));
failed = u_m128::mm_or(failed, packedTris.inactiveMask);
// test against max dist.
u_m128 m_max_dist = u_m128::set1_ps(max_dist);
failed = u_m128::mm_or(failed, u_m128::ref_cmpge_ps(t, m_max_dist));
// backface culling - THE BIT OPERATION IS OPPOSITE.
// We want to PREVENT failing if the backface is hit.
u_m128 backface_mask = u_m128::ref_cmpge_ps(ray_dot_normal, ref_zeroM128);
// always let through the bits set in backface mask
failed = u_m128::mm_or(failed, backface_mask);
//bool bHit = false;
int mask = failed.movemask_ps();
if (mask != 15) // first 4 bits set
{
// there is at least one winner
return true;
}
return false;
}
// returns true if a hit
bool PackedRay::IntersectTest(const PackedTriangles& packedTris, float max_dist) const
{
//Begin calculating determinant - also used to calculate u parameter
// P
u_m128 q[3];
u_m128::multi_cross(q, &m_direction[0], &packedTris.e2[0]);
//if determinant is near zero, ray lies in plane of triangle
// det
u_m128 a = u_m128::multi_dot(&packedTris.e1[0], q);
// reject based on det being close to zero
// NYI
// inv_det
u_m128 f = u_m128::div_ps(ref_oneM128, a);
// distance from v1 to ray origin
// T
u_m128 s[3];
u_m128::multi_sub(s, &m_origin[0], &packedTris.v0[0]);
// Calculate u parameter and test bound
u_m128 u;
u_m128 dot_sq;
dot_sq = u_m128::multi_dot(s, q);
u = u_m128::mul_ps(f, dot_sq);
// the intersection lies outside triangle
// NYI
// Prepare to test v parameter
// Q
u_m128 r[3];
u_m128::multi_cross(r, s, &packedTris.e1[0]);
// calculate V parameter and test bound
// v
u_m128 dot_dir_r;
u_m128 v;
dot_dir_r = u_m128::multi_dot(&m_direction[0], r);
v = u_m128::mul_ps(f, dot_dir_r);
// intersection outside of triangles?
// NYI
// t
u_m128 t, dot_edge_r;
dot_edge_r = u_m128::multi_dot(&packedTris.e2[0], r);
t = u_m128::mul_ps(f, dot_edge_r);
// if t > epsilon, hit
// Failure conditions
// determinant close to zero?
u_m128 failed = u_m128::mm_and(u_m128::ref_cmpge_ps(a, ref_negativeEpsilonM128), u_m128::ref_cmple_ps(a, ref_positiveEpsilonM128));
failed = u_m128::mm_or(failed, u_m128::ref_cmple_ps(u, ref_zeroM128));
failed = u_m128::mm_or(failed, u_m128::ref_cmple_ps(v, ref_zeroM128));
failed = u_m128::mm_or(failed, u_m128::ref_cmpge_ps(u_m128::add_ps(u, v), ref_oneM128));
failed = u_m128::mm_or(failed, u_m128::ref_cmple_ps(t, ref_zeroM128));
failed = u_m128::mm_or(failed, packedTris.inactiveMask);
// test against max dist.
u_m128 m_max_dist = u_m128::set1_ps(max_dist);
failed = u_m128::mm_or(failed, u_m128::ref_cmpge_ps(t, m_max_dist));
//bool bHit = false;
int mask = failed.movemask_ps();
if (mask != 15) // first 4 bits set
{
// there is at least one winner
return true;
}
return false;
}
// returns winner index +1, or zero if no hit
int PackedRay::Intersect(const PackedTriangles& packedTris, float &nearest_dist) const
{
//Begin calculating determinant - also used to calculate u parameter
// P
u_m128 q[3];
u_m128::multi_cross(q, &m_direction[0], &packedTris.e2[0]);
//if determinant is near zero, ray lies in plane of triangle
// det
u_m128 a = u_m128::multi_dot(&packedTris.e1[0], q);
// reject based on det being close to zero
// NYI
// inv_det
u_m128 f = u_m128::div_ps(ref_oneM128, a);
// distance from v1 to ray origin
// T
u_m128 s[3];
u_m128::multi_sub(s, &m_origin[0], &packedTris.v0[0]);
// Calculate u parameter and test bound
u_m128 u;
u_m128 dot_sq;
dot_sq = u_m128::multi_dot(s, q);
u = u_m128::mul_ps(f, dot_sq);
// the intersection lies outside triangle
// NYI
// Prepare to test v parameter
// Q
u_m128 r[3];
u_m128::multi_cross(r, s, &packedTris.e1[0]);
// calculate V parameter and test bound
// v
u_m128 dot_dir_r;
u_m128 v;
dot_dir_r = u_m128::multi_dot(&m_direction[0], r);
v = u_m128::mul_ps(f, dot_dir_r);
// intersection outside of triangles?
// NYI
// t
u_m128 t, dot_edge_r;
dot_edge_r = u_m128::multi_dot(&packedTris.e2[0], r);
t = u_m128::mul_ps(f, dot_edge_r);
// if t > epsilon, hit
// Failure conditions
// determinant close to zero?
u_m128 failed = u_m128::mm_and(u_m128::ref_cmpge_ps(a, ref_negativeEpsilonM128), u_m128::ref_cmple_ps(a, ref_positiveEpsilonM128));
failed = u_m128::mm_or(failed, u_m128::ref_cmple_ps(u, ref_zeroM128));
failed = u_m128::mm_or(failed, u_m128::ref_cmple_ps(v, ref_zeroM128));
failed = u_m128::mm_or(failed, u_m128::ref_cmpge_ps(u_m128::add_ps(u, v), ref_oneM128));
failed = u_m128::mm_or(failed, u_m128::ref_cmple_ps(t, ref_zeroM128));
failed = u_m128::mm_or(failed, packedTris.inactiveMask);
//bool bHit = false;
int mask = failed.movemask_ps();
if (mask != 15) // first 4 bits set
{
// there is at least one winner, so no need to set this
int winner = 0;
float * pT = (float*) &t;
// use simplified way of getting results
uint32_t * pResults = (uint32_t*) &failed;
for (int n=0; n<4; n++)
{
// hit!
// if (!pResults[n] && n<packedTris.num_tris)
if (!pResults[n])
{
if (pT[n] < nearest_dist)
{
nearest_dist = pT[n];
//r_winner = n;
winner = n+1;
//bHit = true;
}
}
} // for n
return winner;
} // mask
return 0;
}
#endif // REFERENCE IMPLEMENTATION
} // namespace