-
Notifications
You must be signed in to change notification settings - Fork 7
/
render.cxx
1444 lines (1265 loc) · 45.2 KB
/
render.cxx
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
//
// FastFlow version by Daniele De Sensi ([email protected])
//
#include "LRT/include/lrt.h"
#include "RTTL/common/RTInclude.hxx"
#include "RTTL/common/RTThread.hxx"
#include "RTTL/common/Timer.hxx"
#include "RTTL/common/RTShader.hxx"
#include "RTTL/BVH/BVH.hxx"
#include "RTTL/Mesh/Mesh.hxx"
#include "RTTL/Triangle/Triangle.hxx"
#include "RTTL/Texture/Texture.hxx"
#include "RTTL/BVH/Builder/OnDemandBuilder.hxx"
#include "LRT/FrameBuffer.hxx"
#if USE_PBOS
#include "LRT/FrameBuffer/PBOFrameBuffer.hxx"
#endif
#ifdef __wald__
// # define USE_GRID
#endif
#ifdef USE_GRID
# include "RTTL/Grid/Grid.hxx"
#endif
#ifdef ENABLE_NORNIR_NATIVE
#undef BLOCKING_MODE
#include <nornir/nornir.hpp>
#endif // ENABLE_NORNIR_NATIVE
#ifdef ENABLE_NORNIR
#include <nornir/instrumenter.hpp>
#include <stdlib.h>
#include <iostream>
#endif
#if defined(ENABLE_NORNIR) || defined(ENABLE_NORNIR_NATIVE)
std::string getParametersPath(){
return std::string(getenv("PARSECDIR")) + std::string("/parameters.xml");
}
#endif
#ifdef FF_VERSION
#undef _INLINE
#define FF_PARFOR_PASSIVE_NOSTEALING
#include <ff/map.hpp>
#include <ff/parallel_for.hpp>
#undef _INLINE
#define _INLINE inline __attribute__((always_inline))
#endif // FF_VERSION
#ifdef ENABLE_CAF
// CAF_V1 | CAF_V2 | CAF_V3 | CAF_V4 | CAF_V4DET
#if !defined(CAF_V1) && !defined(CAF_V2) && !defined(CAF_V3) \
&& !defined(CAF_V4) && !defined(CAF_V4DET)
// #define CAF_V4
# error "DEFINED CAF_Vx"
#endif
#include "caf/all.hpp"
#endif // ENABLE_CAF
#define NORMALIZE_PRIMARY_RAYS
#define RAY_PACKET_LAYOUT_TRIANGLE STORE_NEAR_FAR_DISTANCE | MIN_MAX_RECIPROCAL
#define RAY_PACKET_LAYOUT_SUBDIVISION STORE_NEAR_FAR_DISTANCE | MIN_MAX_RECIPROCAL | STORE_VERTEX_NORMALS
/* -- packet of PACKET_WIDTH * PACKET_WIDTH rays -- */
#define PACKET_WIDTH 8
#define PACKET_WIDTH_SHIFT 3
#define SIMD_WIDTH 4
#define SIMD_VECTORS_PER_PACKET (PACKET_WIDTH*PACKET_WIDTH/SIMD_WIDTH)
#define SIMD_VECTORS_PER_ROW (PACKET_WIDTH/SIMD_WIDTH)
#define RAYS_PER_PACKET (PACKET_WIDTH*PACKET_WIDTH)
#define FOR_ALL_SIMD_VECTORS_IN_PACKET for (unsigned int i=0;i<SIMD_VECTORS_PER_PACKET;i++)
/* -- screen tile used for scheduling work on threads -- */
#define TILE_WIDTH (4*PACKET_WIDTH)
#define TILE_WIDTH_SHIFT 5
#define CAST_FLOAT(s,x) ((float*)&(s))[x]
#define CAST_INT(s,x) ((int*)&(s))[x]
#define CAST_UINT(s,x) ((unsigned int*)&(s))[x]
_ALIGN(DEFAULT_ALIGNMENT) static float coordX[RAYS_PER_PACKET] = {
0,1,2,3,4,5,6,7,
0,1,2,3,4,5,6,7,
0,1,2,3,4,5,6,7,
0,1,2,3,4,5,6,7,
0,1,2,3,4,5,6,7,
0,1,2,3,4,5,6,7,
0,1,2,3,4,5,6,7,
0,1,2,3,4,5,6,7
};
_ALIGN(DEFAULT_ALIGNMENT) static float coordY[RAYS_PER_PACKET] = {
0,0,0,0,0,0,0,0,
1,1,1,1,1,1,1,1,
2,2,2,2,2,2,2,2,
3,3,3,3,3,3,3,3,
4,4,4,4,4,4,4,4,
5,5,5,5,5,5,5,5,
6,6,6,6,6,6,6,6,
7,7,7,7,7,7,7,7
};
static const sse_f factor = _mm_set_ps1(255.0f);
using namespace RTTL;
using namespace std;
#ifdef ENABLE_CAF
using wend = caf::atom_constant<caf::atom("wend")>;
CAF_ALLOW_UNSAFE_MESSAGE_TYPE(std::function<void(size_t)>)
#ifdef CAF_V1
const std::string CAF_V = "CAF_V1";
caf::behavior pfor_worker(caf::event_based_actor *self, uint64_t i, uint64_t nw) {
return {
[=](const size_t& start, const size_t& end, const std::function<void(size_t)>& fun) {
for (size_t i = start; i < end; ++i) {
fun(i);
}
return wend::value;
}
};
}
struct map_state {
std::vector<caf::actor> worker;
};
// caf::behavior pfor_act(caf::stateful_actor<map_state> *self, uint64_t nw) {
// // create workers
// self->state.worker.resize(nw);
// for (uint32_t i = 0; i < nw; i++) {
// caf::actor a = self->spawn<caf::lazy_init>(pfor_worker, i, nw);
// self->state.worker[i] = a;
// }
// return {[=](const size_t& start, const size_t& end,
// const size_t& grain, const std::function<void(size_t)>& fun) {
// size_t nv = end - start;
// size_t chunk = nv / nw;
// size_t plus = nv % nw;
// if (grain > 0 && grain < chunk ) {
// chunk = grain;
// plus = nv % grain;
// }
// auto promis = self->make_response_promise();
// auto n_res = make_shared<size_t>(nw);
// auto update_cb = [=](wend) mutable {
// if (--(*n_res) == 0) {
// promis.deliver(wend::value);
// }
// };
// size_t p_start = start;
// uint32_t iw = 0;
// while(p_start < end) {
// size_t p_end = p_start + chunk;
// if (plus > 0){
// p_end++;
// plus--;
// }
// self->request(self->state.worker[iw], caf::infinite,
// p_start, p_end, fun).then(update_cb);
// p_start = p_end;
// iw = (iw + 1) % nw;
// }
// return promis;
// }};
// }
caf::behavior pfor_act(caf::stateful_actor<map_state> *self, uint64_t nw) {
// create workers
self->state.worker.resize(nw);
for (uint64_t i = 0; i < nw; i++) {
caf::actor a = self->spawn(pfor_worker, i, nw);
self->state.worker[i] = a;
}
return {[=](const size_t& start, const size_t& end, const std::function<void(size_t)>& fun) {
size_t nv = end - start;
size_t chunk = nv / nw;
size_t plus = nv % nw;
auto promis = self->make_response_promise();
auto n_res = make_shared<uint64_t>(nw);
auto update_cb = [=](wend) mutable {
if (--(*n_res) == 0) {
promis.deliver(wend::value);
}
};
size_t p_start = start;
uint32_t iw = 0;
while(p_start < end) {
size_t p_end = p_start + chunk;
if (plus > 0){
p_end++;
plus--;
}
self->request(self->state.worker[iw], caf::infinite,
p_start, p_end, fun).then(update_cb);
p_start = p_end;
iw = (iw + 1) % nw;
}
return promis;
}};
}
#else
#ifdef CAF_V2
const std::string CAF_V = "CAF_V2";
caf::behavior pfor_worker(caf::event_based_actor *self, uint64_t i, uint64_t nw) {
return {
[=](const size_t& start, const size_t& end, const std::function<void(size_t)>& fun) {
for (size_t i = start; i < end; ++i) {
fun(i);
}
return wend::value;
}
};
}
caf::behavior pfor_act(caf::event_based_actor *self) {
return {[=](const size_t& start, const size_t& end,
const size_t& grain, const std::function<void(size_t)>& fun) {
size_t nv = end - start;
size_t nw = nv / grain;
size_t plus = nv % grain;
auto promis = self->make_response_promise();
auto n_res = make_shared<size_t>(nw);
auto update_cb = [=](wend) mutable {
if (--(*n_res) == 0) {
promis.deliver(wend::value);
}
};
size_t p_start = start;
for (auto i=0; i<nw; i++){
size_t p_end = p_start + grain;
if (plus > 0) {
p_end++;
plus--;
}
caf::actor worker = self->spawn(pfor_worker, i, nw);
self->request(worker, caf::infinite, p_start, p_end, fun)
.then(update_cb);
p_start = p_end;
}
return promis;
}};
}
#else
#ifdef CAF_V3
const std::string CAF_V = "CAF_V3";
caf::behavior pfor_worker(caf::event_based_actor *self, uint64_t i) {
return {
[=](const size_t& start, const size_t& end, const std::function<void(size_t)>& fun) {
for (size_t i = start; i < end; ++i) {
fun(i);
}
return wend::value;
}
};
}
struct map_state {
std::vector<caf::actor> worker;
};
caf::behavior pfor_act(caf::stateful_actor<map_state> *self, uint64_t nw) {
// create workers
self->state.worker.resize(nw);
for (uint64_t i = 0; i < nw; i++) {
caf::actor a = self->spawn(pfor_worker, i);
self->state.worker[i] = a;
}
// caf::aout(self) << "DEBUG " << "nw=" << nw << " "
// << "worker.size=" << self->state.worker.size() << std::endl;
return {[=](const size_t& start, const size_t& end,
const size_t& grain, const std::function<void(size_t)>& fun) {
size_t nv = end - start;
size_t w_spawn = nv / grain;
size_t plus = nv % grain;
size_t nw = self->state.worker.size();
// caf::aout(self) << "DEBUG "
// << "nv=" << nv << " "
// << "grain=" << grain << " "
// << "w_spawn=" << w_spawn << " "
// << "nw=" << nw << std::endl;
for (auto i = nw; i < w_spawn; i++) {
caf::actor a = self->spawn(pfor_worker, i);
self->state.worker.push_back(a);
// caf::aout(self) << "+";
}
if (nw < w_spawn) {
caf::aout(self) << std::endl << "spawned " << w_spawn << " worker" << "("
<< "nv=" << nv << ","
<< "grain=" << grain << ")" << std::endl;
}
nw = self->state.worker.size();
// caf::aout(self) << "DEBUG " << "nw=" << nw << std::endl;
auto promis = self->make_response_promise();
auto n_res = make_shared<size_t>(nw);
auto update_cb = [=](wend) mutable {
if (--(*n_res) == 0) {
promis.deliver(wend::value);
}
};
size_t p_start = start;
uint32_t iw = 0;
while(p_start < end) {
size_t p_end = p_start + grain;
if (plus > 0){
p_end++;
plus--;
}
self->request(self->state.worker[iw], caf::infinite,
p_start, p_end, fun).then(update_cb);
// caf::aout(self) << "DEBUG "
// << "worker=" << iw << " "
// << "p_start=" << p_start << " "
// << "p_end=" << p_end << " "
// << "len=" << p_end - p_start << std::endl;
p_start = p_end;
iw = (iw + 1) % nw;
}
return promis;
}};
}
#else
#if defined(CAF_V4) || defined(CAF_V4DET)
#ifdef CAF_V4DET
const std::string CAF_V = "CAF_V4DET";
#else
const std::string CAF_V = "CAF_V4";
#endif
using wget = caf::atom_constant<caf::atom("wget")>;
atomic<size_t> *atomic_i;
caf::behavior pfor_worker(caf::event_based_actor *self, uint64_t iw,
caf::actor emitter) {
return {[=](const size_t &start, const size_t &end,
const std::function<void(size_t)> &fun) {
// caf::aout(self) << "DEBUG "
// << "->worker " << iw << " "
// << "get " << start << " - " << end << std::endl;
self->send(emitter, wget::value);
for (auto i = start; i < end; ++i) {
fun(i);
}
return wend::value;
},
[=](const size_t &size, const std::function<void(size_t)> &fun) {
size_t i;
while ((i = atomic_i->fetch_add(1)) < size) {
// caf::aout(self) << "DEBUG "
// << "->worker " << iw << " "
// << "get " << i << std::endl;
fun(i);
}
return wend::value;
}};
}
void pfor_act(caf::blocking_actor *self, uint64_t nw) {
// create workers
vector<caf::actor> worker(nw);
for (auto i = 0; i < nw; i++) {
#ifdef CAF_V4DET
worker[i] = self->spawn<caf::detached>(pfor_worker, i, caf::actor_cast<caf::actor>(self));
#else
worker[i] = self->spawn(pfor_worker, i, caf::actor_cast<caf::actor>(self));
#endif
}
bool running = true;
self->receive_while(running)(
[=](const size_t &start, const size_t &end, const size_t &grain,
const std::function<void(size_t)> &fun) {
auto sender = caf::actor_cast<caf::actor>(self->current_sender());
size_t nv = end - start;
if (grain == 1) {
// use the atomic version
atomic_i = new atomic<size_t>(0);
for (auto w : worker) {
self->send(w, nv, fun);
}
size_t i{0};
self->receive_for(i, worker.size())([=](wend) {});
free(atomic_i);
} else {
// use the generic with message version
size_t chunk = nv / nw;
size_t plus = nv % nw;
// if grain is specified use it
if (grain > 0 && grain < chunk) {
chunk = grain;
plus = nv % grain;
}
size_t p_start = start;
size_t n_res = 0;
auto send_chunk = [&](const caf::actor& to) {
size_t p_end = p_start + chunk;
if (plus > 0) {
p_end++;
plus--;
}
self->send(to, p_start, p_end, fun);
n_res++;
p_start = p_end;
};
for (auto w : worker) {
if (p_start < end) {
send_chunk(w);
} else {
break;
}
}
bool receive = true;
self->receive_while(receive)(
[&](wget) {
if (p_start < end) {
send_chunk(
caf::actor_cast<caf::actor>(self->current_sender()));
}
},
[&](wend) {
--n_res;
if (p_start >= end && n_res == 0)
receive = false;
});
}
self->response(wend::value);
},
[&](caf::exit_msg &em) {
if (em.reason) {
self->fail_state(std::move(em.reason));
running = false;
}
});
}
#endif // CAF_V4
#endif // CAF_V3
#endif // CAF_V2
#endif // CAF_V1
#endif // ENABLE_CAF
class Camera {
public:
RTVec3f m_cameraOrigin;
RTVec3f m_cameraDirection;
RTVec3f m_cameraUp;
float m_cameraViewAngle;
float m_cameraAspectRatio;
float m_cameraDistance;
inline float getCameraAspect() { return m_cameraAspectRatio; };
inline void setCameraAspect(float aspect) { m_cameraAspectRatio = aspect; };
inline void setCamera(const RTVec3f &origin,
const RTVec3f &direction,
const RTVec3f &up,
const float angle,
const float aspect)
{
m_cameraOrigin = origin;
m_cameraDirection = direction.normalize();
m_cameraUp = up.normalize();
m_cameraViewAngle = angle;
m_cameraAspectRatio = aspect;
m_cameraDistance = 0.5f / tanf(angle * M_PI / 180.0f / 2.0f);
}
};
class Context;
class Context : public MultiThreadedTaskQueue
{
protected:
/* data shared by all threads */
struct SharedThreadData {
/* camera data in SSE friendly layout */
RTVec_t<3,sse_f> origin;
RTVec_t<3,sse_f> up;
RTVec_t<3,sse_f> imagePlaneOrigin;
RTVec_t<3,sse_f> xAxis;
RTVec_t<3,sse_f> yAxis;
RTVec_t<3,sse_f> zAxis;
int resX;
int resY;
int maxTiles;
LRT::FrameBuffer *frameBuffer;
};
/* scene */
int m_geometryMode;
PolygonalBaseMesh *m_mesh;
BVH *m_bvh;
vector< RTMaterial, Align<RTMaterial> > m_material;
vector< RTTextureObject_RGBA_UCHAR*, Align<RTTextureObject_RGBA_UCHAR*> > m_texture;
/* threads */
int m_threads;
bool m_threadsCreated;
// need to be aligned, therefore made static
static SharedThreadData m_threadData;
static AtomicCounter m_tileCounter;
#ifdef FF_VERSION
ff::ParallelFor* pf;
#endif
#ifdef ENABLE_CAF
std::shared_ptr<caf::actor_system> system;
std::shared_ptr<caf::scoped_actor> self;
caf::actor pf;
uint32_t caf_conf_wpt;
uint32_t caf_conf_grain;
uint32_t caf_conf_act;
#endif // ENABLE_CAF
#ifdef ENABLE_NORNIR_NATIVE
nornir::ParallelFor* pf;
#endif
// Nornir: Create instrumenter
#ifdef ENABLE_NORNIR
nornir::Instrumenter* instr;
#endif
/* textures */
_INLINE void initSharedThreadData(Camera *camera,
const int resX,
const int resY,
LRT::FrameBuffer *frameBuffer)
{
const float left = -camera->m_cameraAspectRatio * 0.5f;
const float top = 0.5f;
m_threadData.origin[0] = convert(camera->m_cameraOrigin[0]);
m_threadData.origin[1] = convert(camera->m_cameraOrigin[1]);
m_threadData.origin[2] = convert(camera->m_cameraOrigin[2]);
m_threadData.yAxis[0] = convert(camera->m_cameraDirection[0]);
m_threadData.yAxis[1] = convert(camera->m_cameraDirection[1]);
m_threadData.yAxis[2] = convert(camera->m_cameraDirection[2]);
m_threadData.yAxis.normalize();
m_threadData.up[0] = convert(camera->m_cameraUp[0]);
m_threadData.up[1] = convert(camera->m_cameraUp[1]);
m_threadData.up[2] = convert(camera->m_cameraUp[2]);
m_threadData.xAxis = m_threadData.yAxis^m_threadData.up;
m_threadData.xAxis.normalize();
m_threadData.zAxis = m_threadData.yAxis^m_threadData.xAxis;
m_threadData.zAxis.normalize();
m_threadData.imagePlaneOrigin = m_threadData.yAxis * convert(camera->m_cameraDistance) + convert(left) * m_threadData.xAxis - convert(top) * m_threadData.zAxis;
m_threadData.xAxis = m_threadData.xAxis * camera->m_cameraAspectRatio / resX;
m_threadData.zAxis = m_threadData.zAxis / resY;
m_threadData.resX = resX;
m_threadData.resY = resY;
m_threadData.maxTiles = (resX >> PACKET_WIDTH_SHIFT)*(resY >> PACKET_WIDTH_SHIFT);
m_threadData.frameBuffer = frameBuffer;
}
virtual int task(int jobID, int threadID);
template <class MESH, const int LAYOUT>
_INLINE void renderTile(LRT::FrameBuffer *frameBuffer,
const int startX,const int startY,
const int resX,const int resY);
public:
enum {
MINIRT_POLYGONAL_GEOMETRY,
MINIRT_SUBDIVISION_SURFACE_GEOMETRY
};
Context(){
m_bvh = NULL;
m_mesh = NULL;
m_threads = 1;
m_threadsCreated = false;
m_geometryMode = MINIRT_POLYGONAL_GEOMETRY;
Context::m_tileCounter.reset();
#ifdef FF_VERSION
pf = NULL;
#endif
#ifdef ENABLE_CAF
std::cout << "CAF_VERSION=" << CAF_VERSION << " " << CAF_V << std::endl;
caf_conf_wpt = 1;
if(const char* env_wpt = std::getenv("CAF_CONF_WPT")){
caf_conf_wpt = atoi(env_wpt);
}
caf_conf_act = 0;
if(const char* env_act = std::getenv("CAF_CONF_ACT")){
caf_conf_act = atoi(env_act);
}
#if defined(CAF_V2) || defined(CAF_V3) || defined(CAF_V4) || defined(CAF_V4DET)
caf_conf_grain = 1;
if(const char* env_grain = std::getenv("CAF_CONF_GRAIN")){
caf_conf_grain = atoi(env_grain);
}
#endif // CAF_V2 / CAF_V3
#endif // ENABLE_CAF
#ifdef ENABLE_NORNIR
instr = new nornir::Instrumenter(getParametersPath(), 1, NULL, true);
#endif
}
~Context(){
#ifdef ENABLE_NORNIR
instr->terminate();
std::cout << "riff.time|" << instr->getExecutionTime() << std::endl;
std::cout << "riff.iterations|" << instr->getTotalTasks() << std::endl;
delete instr;
#endif
}
/* ------------------------------------ */
/* -------------- Mini API ------------ */
/* ------------------------------------ */
void init(const int mode = MINIRT_POLYGONAL_GEOMETRY);
void setRenderThreads(const int threads);
void clear();
void addVertices(const RTVec3f *const v,const RTVec2f *const txt,const int vertices);
void addTriangleMesh(const RTVec3i *const t,const int triangles, const int *const shaderID = NULL);
void addQuadMesh(const RTVec4i *const t,const int quads, const int *const shaderID = NULL);
void addMaterials(const RTMaterial *const mat, const int materials);
void addTexture(const int width, const int height, void *data, const int format);
void finalize();
void buildSpatialIndexStructure();
void renderFrame(Camera *camera,
LRT::FrameBuffer *frameBuffer,
const int resX,const int resY);
_INLINE int numPrimitives() const
{
return m_mesh->numPrimitives();
}
_INLINE int numVertices() const
{
return m_mesh->numVertices();
}
_INLINE int numMaterials() const
{
return m_material.size();
}
_INLINE RTBoxSSE getSceneAABB() const
{
return m_mesh->getAABB();
}
};
_ALIGN(DEFAULT_ALIGNMENT) Context::SharedThreadData Context::m_threadData;
_ALIGN(DEFAULT_ALIGNMENT) AtomicCounter Context::m_tileCounter;
/*! get four pixels in sse-float-format, converts those to RGB-uchar */
_INLINE sse_i convert_fourPixels_to_fourRBGAuchars(const sse_f& red,
const sse_f& green,
const sse_f& blue)
{
sse_i r = _mm_cvtps_epi32(red * factor);
sse_i g = _mm_cvtps_epi32(green * factor);
sse_i b = _mm_cvtps_epi32(blue * factor);
sse_i fc = _mm_or_si128(_mm_slli_epi32(r, 16), _mm_or_si128(b, _mm_slli_epi32(g, 8)));
return fc;
}
/* ----------------------------------------------------------------------------------------------------------------- */
/* -- small shaders mostly for debugging and testing, will be removed as soon as the shading compiler is working -- */
/* ----------------------------------------------------------------------------------------------------------------- */
/* moved constants outside the function as otherwise the compiler does not treat them as constants */
static const sse_i moduloX = convert<sse_i>(11);
static const sse_i moduloY = convert<sse_i>(13);
static const sse_i moduloZ = convert<sse_i>(17);
static const sse_f scaleX = convert<sse_f>(1.0f / 11);
static const sse_f scaleY = convert<sse_f>(1.0f / 13);
static const sse_f scaleZ = convert<sse_f>(1.0f / 17);
static const sse_i bias = convert<sse_i>(12);
template <int N, int LAYOUT, int MULTIPLE_ORIGINS, int SHADOW_RAYS, class Mesh>
_INLINE void Shade_RandomID(RayPacket<N, LAYOUT, MULTIPLE_ORIGINS, SHADOW_RAYS> &packet,
const Mesh &mesh,
const RTMaterial *const mat,
RTTextureObject_RGBA_UCHAR **texture,
sse_i *const dest)
{
FOR_ALL_SIMD_VECTORS_IN_PACKET
{
const sse_i t = packet.id(i) + bias;
const sse_f colorX = convert(t & moduloX) * scaleX;
const sse_f colorY = convert(t & moduloY) * scaleY;
const sse_f colorZ = convert(t & moduloZ) * scaleZ;
dest[i] = convert_fourPixels_to_fourRBGAuchars(colorX,colorY,colorZ);
}
}
template <int N, int LAYOUT, int MULTIPLE_ORIGINS, int SHADOW_RAYS, class Mesh>
_INLINE void Shade_PrimitiveID(RayPacket<N, LAYOUT, MULTIPLE_ORIGINS, SHADOW_RAYS> &packet,
const Mesh &mesh,
const RTMaterial *const mat,
RTTextureObject_RGBA_UCHAR **texture,
sse_i *const dest)
{
FOR_ALL_SIMD_VECTORS_IN_PACKET
{
const sse_f t = convert(packet.id(i));
dest[i] = convert_fourPixels_to_fourRBGAuchars(t,t,t);
}
}
template <int N, int LAYOUT, int MULTIPLE_ORIGINS, int SHADOW_RAYS, class Mesh>
_INLINE void Shade_ShaderID(RayPacket<N, LAYOUT, MULTIPLE_ORIGINS, SHADOW_RAYS> &packet,
const Mesh &mesh,
const RTMaterial *const mat,
RTTextureObject_RGBA_UCHAR **texture,
sse_i *const dest)
{
FOR_ALL_SIMD_VECTORS_IN_PACKET
{
const sse_i t = packet.shaderID(i) + bias;
const sse_f colorX = convert(t & moduloX) * scaleX;
const sse_f colorY = convert(t & moduloY) * scaleY;
const sse_f colorZ = convert(t & moduloZ) * scaleZ;
dest[i] = convert_fourPixels_to_fourRBGAuchars(colorX,colorY,colorZ);
}
}
template <int N, int LAYOUT, int MULTIPLE_ORIGINS, int SHADOW_RAYS, class Mesh>
_INLINE void Shade_Diffuse(RayPacket<N, LAYOUT, MULTIPLE_ORIGINS, SHADOW_RAYS> &packet,
const Mesh &mesh,
const RTMaterial *const mat,
RTTextureObject_RGBA_UCHAR **texture,
sse_i *const dest)
{
RTVec_t<3, sse_f> diffuse;
FOR_ALL_SIMD_VECTORS_IN_PACKET
{
RTMaterial::getDiffuse(packet.shaderID(i),mat,diffuse);
//DBG_PRINT(diffuse);
dest[i] = convert_fourPixels_to_fourRBGAuchars(diffuse[0],diffuse[1],diffuse[2]);
}
}
template <int N, int LAYOUT, int MULTIPLE_ORIGINS, int SHADOW_RAYS, class Mesh>
_INLINE void Shade_Normal(RayPacket<N, LAYOUT, MULTIPLE_ORIGINS, SHADOW_RAYS> &packet,
const Mesh &mesh,
const RTMaterial *const mat,
RTTextureObject_RGBA_UCHAR **texture,
sse_i *const dest)
{
RTVec_t<3, sse_f> normal;
FOR_ALL_SIMD_VECTORS_IN_PACKET
{
mesh.getGeometryNormal<N, LAYOUT, MULTIPLE_ORIGINS, SHADOW_RAYS,false>(packet,i,normal);
dest[i] = convert_fourPixels_to_fourRBGAuchars(normal[0],normal[1],normal[2]);
}
}
template <int N, int LAYOUT, int MULTIPLE_ORIGINS, int SHADOW_RAYS, class Mesh>
_INLINE void Shade_EyeLight(RayPacket<N, LAYOUT, MULTIPLE_ORIGINS, SHADOW_RAYS> &packet,
const Mesh &mesh,
const RTMaterial *const mat,
RTTextureObject_RGBA_UCHAR **texture,
sse_i *const dest)
{
RTVec_t<3, sse_f> normal;
const sse_f fixedColor = convert<sse_f>(0.6f);
const sse_f ambient = convert<sse_f>(0.2f);
FOR_ALL_SIMD_VECTORS_IN_PACKET
{
mesh.template getGeometryNormal<N, LAYOUT, MULTIPLE_ORIGINS, SHADOW_RAYS,true>(packet,i,normal);
// needs normalized ray directions
const sse_f dot = abs(normal[0] * packet.directionX(i) + normal[1] * packet.directionY(i) + normal[2] * packet.directionZ(i));
const sse_f color = ambient + fixedColor * dot;
dest[i] = convert_fourPixels_to_fourRBGAuchars(color,color,color);
}
}
template <int N, int LAYOUT, int MULTIPLE_ORIGINS, int SHADOW_RAYS, class Mesh>
_INLINE void Shade_TxtCoord(RayPacket<N, LAYOUT, MULTIPLE_ORIGINS, SHADOW_RAYS> &packet,
const Mesh &mesh,
const RTMaterial *const mat,
RTTextureObject_RGBA_UCHAR **texture,
sse_i *const dest)
{
RTVec_t<2, sse_f> txt;
RTVec_t<4, sse_f> texel;
FOR_ALL_SIMD_VECTORS_IN_PACKET
{
mesh.getTextureCoordinate<N, LAYOUT, MULTIPLE_ORIGINS, SHADOW_RAYS,false>(packet,i,txt);
//DBG_PRINT(mat.m_textureId);
//texture[mat[0].m_textureId]->getTexel(txt[0],txt[1],texel);
dest[i] = convert_fourPixels_to_fourRBGAuchars(txt[0],txt[1],convert<sse_f>(1) - txt[0] - txt[1]);
//dest[i] = convert_fourPixels_to_fourRBGAuchars(texel[0],texel[1],texel[2]);
}
}
template <int N, int LAYOUT, int MULTIPLE_ORIGINS, int SHADOW_RAYS, class Mesh>
_INLINE void Shade_Texture(RayPacket<N, LAYOUT, MULTIPLE_ORIGINS, SHADOW_RAYS> &packet,
const Mesh &mesh,
const RTMaterial *const mat,
RTTextureObject_RGBA_UCHAR **texture,
sse_i *const dest)
{
RTVec_t<2, sse_f> txt;
RTVec_t<4, sse_f> texel;
const sse_i zero = convert<sse_i>(0);
const sse_i noHit = convert<sse_i>(-1);
FOR_ALL_SIMD_VECTORS_IN_PACKET
{
texel[0] = _mm_setzero_ps();
texel[1] = _mm_setzero_ps();
texel[2] = _mm_setzero_ps();
const sse_f noHitMask = _mm_castsi128_ps(_mm_cmpgt_epi32(packet.id(i), noHit));
//if (__builtin_expect(_mm_movemask_ps(noHitMask) == 0x0,0)) continue;
const sse_i shaderID = max(packet.shaderID(i),zero); // -1 not allowed
mesh.getTextureCoordinate<N, LAYOUT, MULTIPLE_ORIGINS, SHADOW_RAYS,false>(packet,i,txt);
const int txtId0 = mat[CAST_INT(shaderID,0)].m_textureId;
const int txtId1 = mat[CAST_INT(shaderID,1)].m_textureId;
const int txtId2 = mat[CAST_INT(shaderID,2)].m_textureId;
const int txtId3 = mat[CAST_INT(shaderID,3)].m_textureId;
if (txtId0 != -1) texture[txtId0]->getTexel<0>(txt[0],txt[1],texel);
if (txtId1 != -1) texture[txtId1]->getTexel<1>(txt[0],txt[1],texel);
if (txtId2 != -1) texture[txtId2]->getTexel<2>(txt[0],txt[1],texel);
if (txtId3 != -1) texture[txtId3]->getTexel<3>(txt[0],txt[1],texel);
texel[0] &= noHitMask;
texel[1] &= noHitMask;
texel[2] &= noHitMask;
#if 0
DBG_PRINT(packet.id(i));
DBG_PRINT(packet.shaderID(i));
DBG_PRINT(txtId0);
DBG_PRINT(txtId1);
DBG_PRINT(txtId2);
DBG_PRINT(txtId3);
DBG_PRINT(txt[0]);
DBG_PRINT(txt[1]);
DBG_PRINT(texel[0]);
DBG_PRINT(texel[1]);
DBG_PRINT(texel[2]);
DBG_PRINT(texel[3]);
exit(0);
#endif
dest[i] = convert_fourPixels_to_fourRBGAuchars(texel[0],texel[1],texel[2]);
}
}
/* --------------------------------------------------------------------------------------------------- */
/* --------------------------------------------------------------------------------------------------- */
/* --------------------------------------------------------------------------------------------------- */
void Context::init(const int mode)
{
#ifndef RT_EMULATE_SSE
const int oldMXCSR = _mm_getcsr();
const int newMXCSR = oldMXCSR | (_MM_FLUSH_ZERO_ON | _MM_MASK_MASK); // | _MM_ROUND_TOWARD_ZERO
_mm_setcsr(newMXCSR);
#endif
m_geometryMode = mode;
assert(m_mesh == NULL);
switch(m_geometryMode)
{
case MINIRT_POLYGONAL_GEOMETRY:
m_mesh = new (aligned_malloc<StandardTriangleMesh>(1)) StandardTriangleMesh; // ugly I know
break;
case MINIRT_SUBDIVISION_SURFACE_GEOMETRY:
m_mesh = new (aligned_malloc<DirectedEdgeMesh>(1)) DirectedEdgeMesh; // ugly I know
break;
default:
FATAL("Context: unkown geometry mode");
}
}
void Context::setRenderThreads(const int nthreads)
{
m_threads = nthreads;
}
void Context::clear()
{
m_mesh->clear();
}
void Context::addVertices(const RTVec3f *const v,const RTVec2f *const txt,const int vertices)
{
m_mesh->addVertices((float*)v,(float*)txt,vertices,RT_VERTEX_3F);
}
void Context::addTriangleMesh(const RTVec3i* const t,const int triangles, const int *const shaderID)
{
m_mesh->addPrimitives((int *)t,triangles,RT_TRIANGLE,shaderID);
}
void Context::addQuadMesh(const RTVec4i* const t,const int quads, const int *const shaderID)
{
m_mesh->addPrimitives((int *)t,quads,RT_QUAD,shaderID);
}
void Context::addMaterials(const RTMaterial *const mat, const int materials)
{
m_material.reserve(materials);
for (int i=0;i<materials;i++)
m_material.push_back(mat[i]);
}
void Context::addTexture(const int width, const int height, void *data, const int format)
{
assert(format == RT_TEXTURE_FORMAT_RGB_UCHAR);
RTTextureObject_RGBA_UCHAR* txt = new RTTextureObject_RGBA_UCHAR(width,height);
/* need here a more sophisticated conversion framework */
#if 1
RTTextureObject_RGBA_UCHAR::Texel *dest = txt->getTexelPtr();
unsigned char *source = (unsigned char*)data;
for (int i=0;i<width*height;i++)
{
dest[i][0] = source[0];
dest[i][1] = source[1];
dest[i][2] = source[2];
dest[i][3] = 0;
source += 3;
}
#endif
m_texture.push_back(txt);
}
void Context::finalize()
{
m_mesh->finalize();
if (m_material.size() == 0)
{
cout << "No materials -> create dummy material" << endl;
RTMaterial mat;
mat.m_diffuse = RTVec3f(0.8f,0.8f,0.8f);
m_material.push_back(mat);
}
}
void Context::buildSpatialIndexStructure()
{
assert(m_mesh->numPrimitives());
assert(m_mesh->numVertices());
assert(m_bvh == NULL);
const int numPrimitives = m_mesh->numPrimitives();
AABB *box = aligned_malloc<AABB>(numPrimitives);
m_mesh->storePrimitiveAABBs(box,numPrimitives);
//for (int i=0;i<mesh->numPrimitives();i++)
// box[i] = static_cast<AABB>(mesh->getAABB(i));
m_bvh = new AABBListBVH(box,numPrimitives);
Timer timer;
timer.start();
m_bvh->build(m_mesh->getAABB(),m_mesh->getCentroidAABB());
const float t = timer.stop();
cout << "build time " << t << endl;
#ifdef USE_GRID
{
Timer timer;
timer.start();