-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathtest.cpp
1463 lines (1226 loc) · 38.7 KB
/
test.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
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
// test.cpp
// Important: This test app requires AVX-512. It does not yet automatically dispatch.
/*
Copyright (c) 2010-2011, Intel Corporation
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
* Neither the name of Intel Corporation nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#define _CRT_SECURE_NO_WARNINGS 1
#include <stdlib.h>
#include <stdio.h>
#include <assert.h>
#include <algorithm>
#ifdef _WIN32
#define WIN32_LEAN_AND_MEAN 1
#define NOMINMAX
#include <windows.h>
#endif
#ifndef CPPSPMD_GLUER
#define CPPSPMD_GLUER(a, b) a##b
#endif
#ifndef CPPSPMD_GLUER2
#define CPPSPMD_GLUER2(a, b) CPPSPMD_GLUER(a, b)
#endif
// float4
#undef CPPSPMD_NAME
#define CPPSPMD_NAME(a) CPPSPMD_GLUER2(a, _float4)
#include "test_kernel_declares.h"
#include "mandelbrot_declares.h"
#include "rt_kernel_declares.h"
#include "simple_declares.h"
#include "volume_kernel_declares.h"
#include "noise_kernel_declares.h"
#include "options_declares.h"
#include "ao_bench_declares.h"
// SSE2
#undef CPPSPMD_NAME
#define CPPSPMD_NAME(a) CPPSPMD_GLUER2(a, _sse2)
#define CPPSPMD_SSE2 1
#include "test_kernel_declares.h"
#include "mandelbrot_declares.h"
#include "rt_kernel_declares.h"
#include "simple_declares.h"
#include "volume_kernel_declares.h"
#include "noise_kernel_declares.h"
#include "options_declares.h"
#include "ao_bench_declares.h"
#undef CPPSPMD_SSE2
// SSE4.1
#undef CPPSPMD_NAME
#define CPPSPMD_NAME(a) CPPSPMD_GLUER2(a, _sse41)
#define CPPSPMD_SSE41 1
#include "test_kernel_declares.h"
#include "mandelbrot_declares.h"
#include "rt_kernel_declares.h"
#include "simple_declares.h"
#include "volume_kernel_declares.h"
#include "noise_kernel_declares.h"
#include "options_declares.h"
#include "ao_bench_declares.h"
#undef CPPSPMD_SSE41
// AVX1
#undef CPPSPMD_NAME
#define CPPSPMD_NAME(a) CPPSPMD_GLUER2(a, _avx1)
#include "test_kernel_declares.h"
#include "mandelbrot_declares.h"
#include "rt_kernel_declares.h"
#include "simple_declares.h"
#include "volume_kernel_declares.h"
#include "noise_kernel_declares.h"
#include "options_declares.h"
#include "options_declares.h"
#include "ao_bench_declares.h"
// AVX1 alt
#undef CPPSPMD_NAME
#define CPPSPMD_NAME(a) CPPSPMD_GLUER2(a, _avx1_alt)
#include "test_kernel_declares.h"
#include "mandelbrot_declares.h"
#include "rt_kernel_declares.h"
#include "simple_declares.h"
#include "volume_kernel_declares.h"
#include "noise_kernel_declares.h"
#include "options_declares.h"
#include "options_declares.h"
#include "ao_bench_declares.h"
// AVX2 FMA
#undef CPPSPMD_NAME
#define CPPSPMD_NAME(a) CPPSPMD_GLUER2(a, _avx2_fma)
#include "test_kernel_declares.h"
#include "mandelbrot_declares.h"
#include "rt_kernel_declares.h"
#include "simple_declares.h"
#include "volume_kernel_declares.h"
#include "noise_kernel_declares.h"
#include "options_declares.h"
#include "ao_bench_declares.h"
// int16 AVX2 FMA
#undef CPPSPMD_NAME
#define CPPSPMD_NAME(a) CPPSPMD_GLUER2(a, _int16_avx2_fma)
#include "test_kernel_declares.h"
#include "mandelbrot_declares.h"
// AVX-512
#undef CPPSPMD_NAME
#define CPPSPMD_NAME(a) CPPSPMD_GLUER2(a, _avx512)
#undef CPPSPMD_AVX512
#define CPPSPMD_AVX512 1
#include "test_kernel_declares.h"
#include "mandelbrot_declares.h"
#include "rt_kernel_declares.h"
#include "simple_declares.h"
#include "volume_kernel_declares.h"
#include "noise_kernel_declares.h"
#include "options_declares.h"
#include "ao_bench_declares.h"
#undef CPPSPMD_AVX512
typedef uint64_t timer_ticks;
class interval_timer
{
public:
interval_timer();
void start();
void stop();
double get_elapsed_secs() const;
inline double get_elapsed_ms() const { return 1000.0f* get_elapsed_secs(); }
static void init();
static inline timer_ticks get_ticks_per_sec() { return g_freq; }
static timer_ticks get_ticks();
static double ticks_to_secs(timer_ticks ticks);
static inline double ticks_to_ms(timer_ticks ticks) { return ticks_to_secs(ticks) * 1000.0f; }
private:
static timer_ticks g_init_ticks, g_freq;
static double g_timer_freq;
timer_ticks m_start_time, m_stop_time;
bool m_started, m_stopped;
};
uint64_t interval_timer::g_init_ticks, interval_timer::g_freq;
double interval_timer::g_timer_freq;
#if defined(_WIN32)
inline void query_counter(timer_ticks* pTicks)
{
QueryPerformanceCounter(reinterpret_cast<LARGE_INTEGER*>(pTicks));
}
inline void query_counter_frequency(timer_ticks* pTicks)
{
QueryPerformanceFrequency(reinterpret_cast<LARGE_INTEGER*>(pTicks));
}
#elif defined(__APPLE__)
#include <sys/time.h>
inline void query_counter(timer_ticks* pTicks)
{
struct timeval cur_time;
gettimeofday(&cur_time, NULL);
*pTicks = static_cast<unsigned long long>(cur_time.tv_sec) * 1000000ULL + static_cast<unsigned long long>(cur_time.tv_usec);
}
inline void query_counter_frequency(timer_ticks* pTicks)
{
*pTicks = 1000000;
}
#elif defined(__GNUC__)
#include <sys/timex.h>
inline void query_counter(timer_ticks* pTicks)
{
struct timeval cur_time;
gettimeofday(&cur_time, NULL);
*pTicks = static_cast<unsigned long long>(cur_time.tv_sec) * 1000000ULL + static_cast<unsigned long long>(cur_time.tv_usec);
}
inline void query_counter_frequency(timer_ticks* pTicks)
{
*pTicks = 1000000;
}
#else
#error TODO
#endif
interval_timer::interval_timer() : m_start_time(0), m_stop_time(0), m_started(false), m_stopped(false)
{
if (!g_timer_freq)
init();
}
void interval_timer::start()
{
query_counter(&m_start_time);
m_started = true;
m_stopped = false;
}
void interval_timer::stop()
{
assert(m_started);
query_counter(&m_stop_time);
m_stopped = true;
}
double interval_timer::get_elapsed_secs() const
{
assert(m_started);
if (!m_started)
return 0;
timer_ticks stop_time = m_stop_time;
if (!m_stopped)
query_counter(&stop_time);
timer_ticks delta = stop_time - m_start_time;
return delta * g_timer_freq;
}
void interval_timer::init()
{
if (!g_timer_freq)
{
query_counter_frequency(&g_freq);
g_timer_freq = 1.0f / g_freq;
query_counter(&g_init_ticks);
}
}
timer_ticks interval_timer::get_ticks()
{
if (!g_timer_freq)
init();
timer_ticks ticks;
query_counter(&ticks);
return ticks - g_init_ticks;
}
double interval_timer::ticks_to_secs(timer_ticks ticks)
{
if (!g_timer_freq)
init();
return ticks * g_timer_freq;
}
//------------------------------------------------------------------------------------------------
// Mandelbrot
/* Write a PPM image file with the image of the Mandelbrot set */
inline void writePPM(int *buf, int width, int height, const char *fn) {
FILE *fp = fopen(fn, "wb");
fprintf(fp, "P6\n");
fprintf(fp, "%d %d\n", width, height);
fprintf(fp, "255\n");
for (int i = 0; i < width*height; ++i) {
// Map the iteration count to colors by just alternating between
// two greys.
char c = (char)((buf[i] & 0x1) ? 240 : 20);
for (int j = 0; j < 3; ++j)
fputc(c, fp);
}
fclose(fp);
printf("Wrote image file %s\n", fn);
}
/* Write a PPM image file with the image */
inline void writePPM(float *buf, int width, int height, const char *fn) {
FILE *fp = fopen(fn, "wb");
fprintf(fp, "P6\n");
fprintf(fp, "%d %d\n", width, height);
fprintf(fp, "255\n");
for (int i = 0; i < width*height; ++i) {
float v = buf[i] * 255.f;
if (v < 0.f) v = 0.f;
else if (v > 255.f) v = 255.f;
unsigned char c = (unsigned char)v;
for (int j = 0; j < 3; ++j)
fputc(c, fp);
}
fclose(fp);
printf("Wrote image file %s\n", fn);
}
static int mandel_c(float c_re, float c_im, int count) {
float z_re = c_re, z_im = c_im;
int i;
for (i = 0; i < count; ++i) {
if (z_re * z_re + z_im * z_im > 4.f)
break;
float new_re = z_re*z_re - z_im*z_im;
float new_im = 2.f * z_re * z_im;
z_re = c_re + new_re;
z_im = c_im + new_im;
}
return i;
}
void mandelbrot_c(float x0, float y0, float x1, float y1,
int width, int height, int maxIterations,
int output[])
{
float dx = (x1 - x0) / width;
float dy = (y1 - y0) / height;
for (int j = 0; j < height; j++) {
for (int i = 0; i < width; ++i) {
float x = x0 + i * dx;
float y = y0 + j * dy;
int index = (j * width + i);
output[index] = mandel_c(x, y, maxIterations);
}
}
}
int test_mandel()
{
unsigned int width = 768;
unsigned int height = 512;
float x0 = -2;
float x1 = 1;
float y0 = -1;
float y1 = 1;
int maxIterations = 256;
int* buf = new int[width * height];
int num_runs = 10;
interval_timer otm;
printf("test_mandel:\n");
double t = 1e+10f;
for (uint32_t i = 0; i < 10; i++)
{
otm.start();
mandelbrot_float4(x0, y0, x1, y1, width, height, maxIterations, buf);
t = std::min<double>(t, otm.get_elapsed_secs());
}
printf("float4 time: %f\n", t);
writePPM(buf, width, height, "mandelbrot_float4.ppm");
t = 1e+10f;
for (uint32_t i = 0; i < num_runs; i++)
{
otm.start();
mandelbrot_avx1(x0, y0, x1, y1, width, height, maxIterations, buf);
t = std::min<double>(t, otm.get_elapsed_secs());
}
printf("AVX1 time: %f\n", t);
writePPM(buf, width, height, "mandelbrot_AVX1.ppm");
t = 1e+10f;
for (uint32_t i = 0; i < num_runs; i++)
{
otm.start();
mandelbrot_avx1_alt(x0, y0, x1, y1, width, height, maxIterations, buf);
t = std::min<double>(t, otm.get_elapsed_secs());
}
printf("AVX1 alt time: %f\n", t);
writePPM(buf, width, height, "mandelbrot_AVX1_alt.ppm");
t = 1e+10f;
for (uint32_t i = 0; i < num_runs; i++)
{
otm.start();
mandelbrot_sse2(x0, y0, x1, y1, width, height, maxIterations, buf);
t = std::min<double>(t, otm.get_elapsed_secs());
}
printf("SSE2 time: %f\n", t);
writePPM(buf, width, height, "mandelbrot_SSE2.ppm");
t = 1e+10f;
for (uint32_t i = 0; i < num_runs; i++)
{
otm.start();
mandelbrot_sse41(x0, y0, x1, y1, width, height, maxIterations, buf);
t = std::min<double>(t, otm.get_elapsed_secs());
}
printf("SSE4.1 time: %f\n", t);
writePPM(buf, width, height, "mandelbrot_SSE41.ppm");
t = 1e+10f;
for (uint32_t i = 0; i < num_runs; i++)
{
otm.start();
mandelbrot_avx2_fma(x0, y0, x1, y1, width, height, maxIterations, buf);
t = std::min<double>(t, otm.get_elapsed_secs());
}
printf("AVX2 time: %f\n", t);
writePPM(buf, width, height, "mandelbrot_AVX2.ppm");
t = 1e+10f;
for (uint32_t i = 0; i < num_runs; i++)
{
otm.start();
mandelbrot_avx512(x0, y0, x1, y1, width, height, maxIterations, buf);
t = std::min<double>(t, otm.get_elapsed_secs());
}
printf("AVX512 time: %f\n", t);
writePPM(buf, width, height, "mandelbrot_AVX512.ppm");
t = 1e+10f;
for (uint32_t i = 0; i < num_runs; i++)
{
otm.start();
mandelbrot_c(x0, y0, x1, y1,
width, height, maxIterations,
buf);
t = std::min<double>(t, otm.get_elapsed_secs());
}
printf("C time: %f\n", t);
writePPM(buf, width, height, "mandelbrot_C.ppm");
return true;
}
//------------------------------------------------------------------------------------------------
// Low-level test
bool test()
{
FILE *pFile = fopen("float4.txt", "w");
if (!pFile) return false;
fprintf(pFile, "float4:\n");
cppspmd_lowlevel_test_float4(pFile);
fclose(pFile);
printf("Wrote file float4.txt\n");
pFile = fopen("sse2.txt", "w");
if (!pFile) return false;
fprintf(pFile, "sse2:\n");
cppspmd_lowlevel_test_sse2(pFile);
fclose(pFile);
printf("Wrote file sse2.txt\n");
pFile = fopen("sse41.txt", "w");
if (!pFile) return false;
fprintf(pFile, "sse41:\n");
cppspmd_lowlevel_test_sse41(pFile);
fclose(pFile);
printf("Wrote file sse41.txt\n");
pFile = fopen("avx1_alt.txt", "w");
if (!pFile) return false;
fprintf(pFile, "avx1_alt:\n");
cppspmd_lowlevel_test_avx1_alt(pFile);
fclose(pFile);
printf("Wrote file avx1_alt.txt\n");
pFile = fopen("avx1.txt", "w");
if (!pFile) return false;
fprintf(pFile, "avx1:\n");
cppspmd_lowlevel_test_avx1(pFile);
fclose(pFile);
printf("Wrote file avx1.txt\n");
pFile = fopen("avx2_fma.txt", "w");
if (!pFile) return false;
fprintf(pFile, "avx2_fma:\n");
cppspmd_lowlevel_test_avx2_fma(pFile);
fclose(pFile);
printf("Wrote file avx2_fma.txt\n");
pFile = fopen("int16_avx2_fma.txt", "w");
if (!pFile) return false;
fprintf(pFile, "int16_avx2_fma:\n");
cppspmd_lowlevel_test_int16_avx2_fma(pFile);
fclose(pFile);
printf("Wrote file int16_avx2_fma.txt\n");
pFile = fopen("avx512.txt", "w");
if (!pFile) return false;
fprintf(pFile, "avx512:\n");
cppspmd_lowlevel_test_avx512(pFile);
fclose(pFile);
printf("Wrote file avx512.txt\n");
return true;
}
//------------------------------------------------------------------------------------------------
// Ray trace test
static void writeImage(int* idImage, float* depthImage, int width, int height, const char* filename) {
FILE* f = fopen(filename, "wb");
if (!f) {
perror(filename);
exit(1);
}
fprintf(f, "P6\n%d %d\n255\n", width, height);
for (int y = 0; y < height; ++y) {
for (int x = 0; x < width; ++x) {
// use the bits from the object id of the hit object to make a
// random color
int id = idImage[y * width + x];
unsigned char r = 0, g = 0, b = 0;
for (int i = 0; i < 8; ++i) {
// extract bit 3*i for red, 3*i+1 for green, 3*i+2 for blue
int rbit = (id & (1 << (3 * i))) >> (3 * i);
int gbit = (id & (1 << (3 * i + 1))) >> (3 * i + 1);
int bbit = (id & (1 << (3 * i + 2))) >> (3 * i + 2);
// and then set the bits of the colors starting from the
// high bits...
r |= rbit << (7 - i);
g |= gbit << (7 - i);
b |= bbit << (7 - i);
}
fputc(r, f);
fputc(g, f);
fputc(b, f);
}
}
fclose(f);
printf("Wrote image file %s\n", filename);
}
///--------------
// Just enough of a float3 class to do what we need in this file.
#ifdef _MSC_VER
__declspec(align(16))
#endif
struct float3 {
float3() {}
float3(float xx, float yy, float zz) {
x = xx;
y = yy;
z = zz;
}
float3 operator*(float f) const { return float3(x * f, y * f, z * f); }
float3 operator-(const float3& f2) const { return float3(x - f2.x, y - f2.y, z - f2.z); }
float3 operator*(const float3& f2) const { return float3(x * f2.x, y * f2.y, z * f2.z); }
float x, y, z;
float pad; // match padding/alignment of ispc version
}
#ifndef _MSC_VER
__attribute__((aligned(16)))
#endif
;
struct Ray {
float3 origin, dir, invDir;
unsigned int dirIsNeg[3];
float mint, maxt;
int hitId;
};
inline float3 Cross(const float3& v1, const float3& v2) {
float v1x = v1.x, v1y = v1.y, v1z = v1.z;
float v2x = v2.x, v2y = v2.y, v2z = v2.z;
float3 ret;
ret.x = (v1y * v2z) - (v1z * v2y);
ret.y = (v1z * v2x) - (v1x * v2z);
ret.z = (v1x * v2y) - (v1y * v2x);
return ret;
}
inline float Dot(const float3& a, const float3& b) { return a.x * b.x + a.y * b.y + a.z * b.z; }
static void generateRay(const float raster2camera[4][4], const float camera2world[4][4], float x, float y, Ray& ray) {
ray.mint = 0.f;
ray.maxt = 1e30f;
ray.hitId = 0;
// transform raster coordinate (x, y, 0) to camera space
float camx = raster2camera[0][0] * x + raster2camera[0][1] * y + raster2camera[0][3];
float camy = raster2camera[1][0] * x + raster2camera[1][1] * y + raster2camera[1][3];
float camz = raster2camera[2][3];
float camw = raster2camera[3][3];
camx /= camw;
camy /= camw;
camz /= camw;
ray.dir.x = camera2world[0][0] * camx + camera2world[0][1] * camy + camera2world[0][2] * camz;
ray.dir.y = camera2world[1][0] * camx + camera2world[1][1] * camy + camera2world[1][2] * camz;
ray.dir.z = camera2world[2][0] * camx + camera2world[2][1] * camy + camera2world[2][2] * camz;
ray.origin.x = camera2world[0][3] / camera2world[3][3];
ray.origin.y = camera2world[1][3] / camera2world[3][3];
ray.origin.z = camera2world[2][3] / camera2world[3][3];
ray.invDir.x = 1.f / ray.dir.x;
ray.invDir.y = 1.f / ray.dir.y;
ray.invDir.z = 1.f / ray.dir.z;
ray.dirIsNeg[0] = (ray.invDir.x < 0) ? 1 : 0;
ray.dirIsNeg[1] = (ray.invDir.y < 0) ? 1 : 0;
ray.dirIsNeg[2] = (ray.invDir.z < 0) ? 1 : 0;
}
static inline bool BBoxIntersect(const float bounds[2][3], const Ray& ray) {
float3 bounds0(bounds[0][0], bounds[0][1], bounds[0][2]);
float3 bounds1(bounds[1][0], bounds[1][1], bounds[1][2]);
float t0 = ray.mint, t1 = ray.maxt;
float3 tNear = (bounds0 - ray.origin) * ray.invDir;
float3 tFar = (bounds1 - ray.origin) * ray.invDir;
if (tNear.x > tFar.x) {
float tmp = tNear.x;
tNear.x = tFar.x;
tFar.x = tmp;
}
t0 = std::max(tNear.x, t0);
t1 = std::min(tFar.x, t1);
if (tNear.y > tFar.y) {
float tmp = tNear.y;
tNear.y = tFar.y;
tFar.y = tmp;
}
t0 = std::max(tNear.y, t0);
t1 = std::min(tFar.y, t1);
if (tNear.z > tFar.z) {
float tmp = tNear.z;
tNear.z = tFar.z;
tFar.z = tmp;
}
t0 = std::max(tNear.z, t0);
t1 = std::min(tFar.z, t1);
return (t0 <= t1);
}
inline bool TriIntersect(const Triangle& tri, Ray& ray) {
float3 p0(tri.p[0][0], tri.p[0][1], tri.p[0][2]);
float3 p1(tri.p[1][0], tri.p[1][1], tri.p[1][2]);
float3 p2(tri.p[2][0], tri.p[2][1], tri.p[2][2]);
float3 e1 = p1 - p0;
float3 e2 = p2 - p0;
float3 s1 = Cross(ray.dir, e2);
float divisor = Dot(s1, e1);
if (divisor == 0.)
return false;
float invDivisor = 1.f / divisor;
// Compute first barycentric coordinate
float3 d = ray.origin - p0;
float b1 = Dot(d, s1) * invDivisor;
if (b1 < 0. || b1 > 1.)
return false;
// Compute second barycentric coordinate
float3 s2 = Cross(d, e1);
float b2 = Dot(ray.dir, s2) * invDivisor;
if (b2 < 0. || b1 + b2 > 1.)
return false;
// Compute _t_ to intersection point
float t = Dot(e2, s2) * invDivisor;
if (t < ray.mint || t > ray.maxt)
return false;
ray.maxt = t;
ray.hitId = tri.id;
return true;
}
bool BVHIntersect(const LinearBVHNode nodes[], const Triangle tris[], Ray& r) {
Ray ray = r;
bool hit = false;
// Follow ray through BVH nodes to find primitive intersections
int todoOffset = 0, nodeNum = 0;
int todo[64];
while (true) {
// Check ray against BVH node
const LinearBVHNode& node = nodes[nodeNum];
if (BBoxIntersect(node.bounds, ray)) {
unsigned int nPrimitives = node.nPrimitives;
if (nPrimitives > 0) {
// Intersect ray with primitives in leaf BVH node
unsigned int primitivesOffset = node.offset;
for (unsigned int i = 0; i < nPrimitives; ++i) {
if (TriIntersect(tris[primitivesOffset + i], ray))
hit = true;
}
if (todoOffset == 0)
break;
nodeNum = todo[--todoOffset];
}
else {
// Put far BVH node on _todo_ stack, advance to near node
if (r.dirIsNeg[node.splitAxis]) {
todo[todoOffset++] = nodeNum + 1;
nodeNum = node.offset;
}
else {
todo[todoOffset++] = node.offset;
nodeNum = nodeNum + 1;
}
}
}
else {
if (todoOffset == 0)
break;
nodeNum = todo[--todoOffset];
}
}
r.maxt = ray.maxt;
r.hitId = ray.hitId;
return hit;
}
void raytrace_serial(int width, int height, int baseWidth, int baseHeight, const float raster2camera[4][4],
const float camera2world[4][4], float image[], int id[], const LinearBVHNode nodes[],
const Triangle triangles[]) {
float widthScale = float(baseWidth) / float(width);
float heightScale = float(baseHeight) / float(height);
for (int y = 0; y < height; ++y) {
for (int x = 0; x < width; ++x) {
Ray ray;
generateRay(raster2camera, camera2world, x * widthScale, y * heightScale, ray);
BVHIntersect(nodes, triangles, ray);
int offset = y * width + x;
image[offset] = ray.maxt;
id[offset] = ray.hitId;
}
}
}
//---------------
int test_rt()
{
float scale = 1.f;
//const char* filename = "teapot";
const char* filename = "sponza";
//const char* filename = "cornell";
#define READ(var, n) \
if (fread(&(var), sizeof(var), n, f) != (unsigned int)n) { \
fprintf(stderr, "Unexpected EOF reading scene file\n"); \
return 1; \
} else /* eat ; */
//
// Read the camera specification information from the camera file
//
char fnbuf[1024];
sprintf(fnbuf, "%s.camera", filename);
FILE* f = fopen(fnbuf, "rb");
if (!f) {
perror(fnbuf);
return 1;
}
//
// Nothing fancy, and trouble if we run on a big-endian system, just
// fread in the bits
//
int baseWidth, baseHeight;
float camera2world[4][4], raster2camera[4][4];
READ(baseWidth, 1);
READ(baseHeight, 1);
READ(camera2world[0][0], 16);
READ(raster2camera[0][0], 16);
//
// Read in the serialized BVH
//
sprintf(fnbuf, "%s.bvh", filename);
f = fopen(fnbuf, "rb");
if (!f) {
perror(fnbuf);
return 1;
}
// The BVH file starts with an int that gives the total number of BVH
// nodes
uint32_t nNodes;
READ(nNodes, 1);
LinearBVHNode* nodes = new LinearBVHNode[nNodes];
for (unsigned int i = 0; i < nNodes; ++i) {
// Each node is 6x floats for a boox, then an integer for an offset
// to the second child node, then an integer that encodes the type
// of node, the total number of int it if a leaf node, etc.
float b[6];
READ(b[0], 6);
nodes[i].bounds[0][0] = b[0];
nodes[i].bounds[0][1] = b[1];
nodes[i].bounds[0][2] = b[2];
nodes[i].bounds[1][0] = b[3];
nodes[i].bounds[1][1] = b[4];
nodes[i].bounds[1][2] = b[5];
READ(nodes[i].offset, 1);
READ(nodes[i].nPrimitives, 1);
READ(nodes[i].splitAxis, 1);
READ(nodes[i].pad, 1);
}
// And then read the triangles
uint32_t nTris;
READ(nTris, 1);
Triangle* triangles = new Triangle[nTris];
for (uint32_t i = 0; i < nTris; ++i) {
// 9x floats for the 3 vertices
float v[9];
READ(v[0], 9);
float* vp = v;
for (int j = 0; j < 3; ++j) {
triangles[i].p[j][0] = *vp++;
triangles[i].p[j][1] = *vp++;
triangles[i].p[j][2] = *vp++;
}
// And create an object id
triangles[i].id = i + 1;
}
fclose(f);
int height = int(baseHeight * scale);
int width = int(baseWidth * scale);
// allocate images; one to hold hit object ids, one to hold depth to
// the first interseciton
int* id = new int[width * height];
float* image = new float[width * height];
interval_timer tm;
tm.start();
#ifdef _DEBUG
const int T = 1;
#else
const int T = 16;
#endif
for (int i = 0; i < T; i++)
{
#if 0
raytrace_serial(width, height, width, height, raster2camera, camera2world, image, id, nodes, triangles);
#else
for (int y = 0; y < height; y += TILE_SIZE)
{
const int tile_height = std::min(TILE_SIZE, height - y);
for (int x = 0; x < width; x += TILE_SIZE)
{
const int tile_width = std::min(TILE_SIZE, width - x);
raytrace_tile_avx512(x, std::min(x + tile_width, width), y, std::min(y + tile_height, height), width, height, raster2camera, camera2world, image, id, nodes, triangles);
//raytrace_tile_avx2_fma(x, std::min(x + tile_width, width), y, std::min(y + tile_height, height), width, height, raster2camera, camera2world, image, id, nodes, triangles);
//raytrace_tile_avx1(x, std::min(x + tile_width, width), y, std::min(y + tile_height, height), width, height, raster2camera, camera2world, image, id, nodes, triangles);
//raytrace_tile_avx1_alt(x, std::min(x + tile_width, width), y, std::min(y + tile_height, height), width, height, raster2camera, camera2world, image, id, nodes, triangles);
//raytrace_tile_sse41(x, std::min(x + tile_width, width), y, std::min(y + tile_height, height), width, height, raster2camera, camera2world, image, id, nodes, triangles);
//raytrace_tile_sse2(x, std::min(x + tile_width, width), y, std::min(y + tile_height, height), width, height, raster2camera, camera2world, image, id, nodes, triangles);
}
}
#endif
}
printf("Elapsed: %f\n", tm.get_elapsed_secs());
writeImage(id, image, width, height, "rt.ppm");
return 0;
}
//------------------------------------------------------------------------------------------------
int test_simple()
{
printf("simple:\n");
float vin[16], vout[16];
// Initialize input buffer
for (int i = 0; i < 16; ++i)
{
vin[i] = (float)i;
}
memset(vout, 0xDE, sizeof(vout));
simple_float4(vin, vout, 16);
// Print results
printf("float4:\n");
for (int i = 0; i < 16; ++i)
{
printf("%d: simple(%f) = %f\n", i, vin[i], vout[i]);
}
memset(vout, 0xDE, sizeof(vout));
simple_sse41(vin, vout, 16);
// Print results
printf("SSE4.1:\n");
for (int i = 0; i < 16; ++i)
{
printf("%d: simple(%f) = %f\n", i, vin[i], vout[i]);
}
memset(vout, 0xDE, sizeof(vout));
simple_sse2(vin, vout, 16);
// Print results
printf("SSE2:\n");
for (int i = 0; i < 16; ++i)
{
printf("%d: simple(%f) = %f\n", i, vin[i], vout[i]);
}
memset(vout, 0xDE, sizeof(vout));
simple_avx2_fma(vin, vout, 16);
// Print results
printf("AVX2:\n");
for (int i = 0; i < 16; ++i)
{
printf("%d: simple(%f) = %f\n", i, vin[i], vout[i]);
}
memset(vout, 0xDE, sizeof(vout));
simple_avx1(vin, vout, 16);
// Print results
printf("AVX1:\n");
for (int i = 0; i < 16; ++i)
{
printf("%d: simple(%f) = %f\n", i, vin[i], vout[i]);
}
memset(vout, 0xDE, sizeof(vout));
simple_avx1_alt(vin, vout, 16);
// Print results
printf("AVX1_alt:\n");
for (int i = 0; i < 16; ++i)
{
printf("%d: simple(%f) = %f\n", i, vin[i], vout[i]);
}
memset(vout, 0xDE, sizeof(vout));
simple_avx512(vin, vout, 16);
// Print results
printf("AVX512:\n");
for (int i = 0; i < 16; ++i)
{
printf("%d: simple(%f) = %f\n", i, vin[i], vout[i]);
}
return 0;
}
//------------------------------------------------------------------------------------------------