-
Notifications
You must be signed in to change notification settings - Fork 1
/
SlicerSR.cpp
2190 lines (1844 loc) · 95.2 KB
/
SlicerSR.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
#include "RendererHeader.h"
#define STACK_SIZE 64 // Size of the traversal stack in local memory.
#define M_PI 3.1415926535897932384626422832795028841971f
#define TWO_PI 6.2831853071795864769252867665590057683943f
#define DYNAMIC_FETCH_THRESHOLD 20 // If fewer than this active, fetch new rays
#define samps 1
#define F32_MIN (1.175494351e-38f)
#define F32_MAX (3.402823466e+38f)
#define HDRwidth 3200
#define HDRheight 1600
#define HDR
#define EntrypointSentinel 0x76543210
#define MaxBlockHeight 6
enum Refl_t { DIFF, METAL, SPEC, REFR, COAT }; // material types
typedef vmfloat4 Vec4f;
typedef vmfloat3 Vec3f;
typedef vmfloat4 float4;
typedef vmfloat3 float3;
// CUDA textures containing scene data
Vec4f* bvhNodesTexture;
Vec4f* triWoopTexture;
Vec4f* triNormalsTexture;
int* triIndicesTexture;
inline Vec3f absmax3f(const Vec3f& v1, const Vec3f& v2) {
return Vec3f(v1.x * v1.x > v2.x * v2.x ? v1.x : v2.x, v1.y * v1.y > v2.y * v2.y ? v1.y : v2.y, v1.z * v1.z > v2.z * v2.z ? v1.z : v2.z);
}
struct Ray {
float3 orig; // ray origin
float3 dir; // ray direction
Ray(float3 o_, float3 d_) : orig(o_), dir(d_) {}
};
struct Sphere {
float rad; // radius
float3 pos, emi, col; // position, emission, color
Refl_t refl; // reflection type (DIFFuse, SPECular, REFRactive)
float intersect(const Ray& r) const { // returns distance, 0 if nohit
// ray/sphere intersection
float3 op = pos - r.orig;
float t, epsilon = 0.01f;
float b = dot(op, r.dir);
float disc = b * b - dot(op, op) + rad * rad; // discriminant of quadratic formula
if (disc < 0) return 0; else disc = sqrtf(disc);
return (t = b - disc) > epsilon ? t : ((t = b + disc) > epsilon ? t : 0.0f);
}
};
Sphere spheres[] = {
// sun
//{ 10000, { 50.0f, 40.8f, -1060 }, { 0.3, 0.3, 0.3 }, { 0.175f, 0.175f, 0.25f }, DIFF }, // sky 0.003, 0.003, 0.003
//{ 4.5, { 0.0f, 12.5, 0 }, { 6, 4, 1 }, { .6f, .6f, 0.6f }, DIFF }, /// lightsource
{ 10000.02, { 50.0f, -10001.35, 0 }, { 0.0, 0.0, 0 }, { 0.3f, 0.3f, 0.3f }, DIFF }, // ground 300/-301.0
//{ 10000, { 50.0f, -10000.1, 0 }, { 0, 0, 0 }, { 0.3f, 0.3f, 0.3f }, DIFF }, // double shell to prevent light leaking
//{ 110000, { 50.0f, -110048.5, 0 }, { 3.6, 2.0, 0.2 }, { 0.f, 0.f, 0.f }, DIFF }, // horizon brightener
//{ 0.5, { 30.0f, 180.5, 42 }, { 0, 0, 0 }, { .6f, .6f, 0.6f }, DIFF }, // small sphere 1
//{ 0.8, { 2.0f, 0.f, 0 }, { 0.0, 0.0, 0.0 }, { 0.8f, 0.8f, 0.8f }, SPEC }, // small sphere 2
//{ 0.8, { -3.0f, 0.f, 0 }, { 0.0, 0.0, 0.0 }, { 0.0f, 0.0f, 0.2f }, COAT }, // small sphere 2
{ 2.5, { -6.0f, 0.5f, 0.0f }, { 0.0, 0.0, 0.0 }, { 0.9f, 0.9f, 0.9f }, SPEC }, // small sphere 2
//{ 0.6, { -10.0f, -2.f, 1.0f }, { 0.0, 0.0, 0.0 }, { 0.8f, 0.8f, 0.8f }, DIFF }, // small sphere 2
//{ 0.8, { -1.0f, -0.7f, 4.0f }, { 0.0, 0.0, 0.0 }, { 0.8f, 0.8f, 0.8f }, REFR }, // small sphere 2
//{ 9.4, { 9.0f, 0.f, -9.0f }, { 0.0, 0.0, 0.0 }, { 0.8f, 0.8f, 0.f }, DIFF }, // small sphere 2
//{ 22, { 105.0f, 22, 24 }, { 0, 0, 0 }, { 0.9f, 0.9f, 0.9f }, DIFF }, // small sphere 3
};
// RAY BOX INTERSECTION ROUTINES
// Experimentally determined best mix of float/int/video minmax instructions for Kepler.
// float c0min = spanBeginKepler2(c0lox, c0hix, c0loy, c0hiy, c0loz, c0hiz, tmin); // Tesla does max4(min, min, min, tmin)
// float c0max = spanEndKepler2(c0lox, c0hix, c0loy, c0hiy, c0loz, c0hiz, hitT); // Tesla does min4(max, max, max, tmax)
// Perform min/max operations in hardware
// Using Kepler's video instructions, see http://docs.nvidia.com/cuda/parallel-thread-execution/#axzz3jbhbcTZf // : "=r"(v) overwrites v and puts it in a register
// see https://gcc.gnu.org/onlinedocs/gcc/Extended-Asm.html
#define __int_as_float *(float*)&
#define __float_as_int *(int*)&
int min_min(int a, int b, int c) { return min(min(a, b), c); }
int min_max(int a, int b, int c) { return max(min(a, b), c); }
int max_min(int a, int b, int c) { return min(max(a, b), c); }
int max_max(int a, int b, int c) { return max(max(a, b), c); }
//float fmin_fmin(float a, float b, float c) { int v = min_min(__float_as_int(a), __float_as_int(b), __float_as_int(c)); return __int_as_float(v); }
//float fmin_fmax(float a, float b, float c) { int v = min_max(__float_as_int(a), __float_as_int(b), __float_as_int(c)); return __int_as_float(v); }
//float fmax_fmin(float a, float b, float c) { int v = max_min(__float_as_int(a), __float_as_int(b), __float_as_int(c)); return __int_as_float(v); }
//float fmax_fmax(float a, float b, float c) { int v = max_max(__float_as_int(a), __float_as_int(b), __float_as_int(c)); return __int_as_float(v); }
float fmin_fmin(float a, float b, float c) { return min(min(a, b), c); }
float fmin_fmax(float a, float b, float c) { return max(min(a, b), c); }
float fmax_fmin(float a, float b, float c) { return min(max(a, b), c); }
float fmax_fmax(float a, float b, float c) { return max(max(a, b), c); }
float spanBeginKepler(float a0, float a1, float b0, float b1, float c0, float c1, float d) { return fmax_fmax(fminf(a0, a1), fminf(b0, b1), fmin_fmax(c0, c1, d)); }
float spanEndKepler(float a0, float a1, float b0, float b1, float c0, float c1, float d) { return fmin_fmin(fmaxf(a0, a1), fmaxf(b0, b1), fmax_fmin(c0, c1, d)); }
//float3 min3f(float3 v0, float3 v1) { return float3(min(v0.x, v1.x), min(v0.y, v1.y), min(v0.z, v1.z)); }
//float3 max3f(float3 v0, float3 v1) { return float3(max(v0.x, v1.x), max(v0.y, v1.y), max(v0.z, v1.z)); }
Vec3f min3f(const Vec3f& v1, const Vec3f& v2) { return Vec3f(v1.x < v2.x ? v1.x : v2.x, v1.y < v2.y ? v1.y : v2.y, v1.z < v2.z ? v1.z : v2.z); }
Vec3f max3f(const Vec3f& v1, const Vec3f& v2) { return Vec3f(v1.x > v2.x ? v1.x : v2.x, v1.y > v2.y ? v1.y : v2.y, v1.z > v2.z ? v1.z : v2.z); }
float mmmax(float3 v) { return fmax_fmax(v.x, v.y, v.z); }
float mmmin(float3 v) { return fmin_fmin(v.x, v.y, v.z); }
// standard ray box intersection routines (for debugging purposes only)
// based on Intersect::RayBox() in original Aila/Laine code
float spanBeginKepler2(float lo_x, float hi_x, float lo_y, float hi_y, float lo_z, float hi_z, float d) {
Vec3f t0 = Vec3f(lo_x, lo_y, lo_z);
Vec3f t1 = Vec3f(hi_x, hi_y, hi_z);
Vec3f realmin = min3f(t0, t1);
float raybox_tmin = mmmax(realmin);// .max(); // maxmin
//return Vec2f(tmin, tmax);
return raybox_tmin;
}
float spanEndKepler2(float lo_x, float hi_x, float lo_y, float hi_y, float lo_z, float hi_z, float d) {
Vec3f t0 = Vec3f(lo_x, lo_y, lo_z);
Vec3f t1 = Vec3f(hi_x, hi_y, hi_z);
Vec3f realmax = max3f(t0, t1);
float raybox_tmax = mmmin(realmax);// .min(); /// minmax
//return Vec2f(tmin, tmax);
return raybox_tmax;
}
typedef vmfloat2 float2;
const float __ooeps = exp2f(-80.0f); // Avoid div by zero, returns 1/2^80, an extremely small number
float2 ComputeAaBbHits(const float3& pos_start, const float3& pos_min, const float3& pos_max, const float3& vec_dir)
{
// intersect ray with a box
// http://www.siggraph.org/education/materials/HyperGraph/raytrace/rtinter3.htm
float3 invR;// = float3(1.0f, 1.0f, 1.0f) / vec_dir.x;
invR.x = 1.0f / (fabsf(vec_dir.x) > __ooeps ? vec_dir.x : copysignf(__ooeps, vec_dir.x)); // inverse ray direction
invR.y = 1.0f / (fabsf(vec_dir.y) > __ooeps ? vec_dir.y : copysignf(__ooeps, vec_dir.y)); // inverse ray direction
invR.z = 1.0f / (fabsf(vec_dir.z) > __ooeps ? vec_dir.z : copysignf(__ooeps, vec_dir.z)); // inverse ray direction
float3 tbot = invR * (pos_min - pos_start);
float3 ttop = invR * (pos_max - pos_start);
// re-order intersections to find smallest and largest on each axis
float3 tmin = min3f(ttop, tbot);
float3 tmax = max3f(ttop, tbot);
// find the largest tmin and the smallest tmax
float largest_tmin = max(max(tmin.x, tmin.y), max(tmin.x, tmin.z));
float smallest_tmax = min(min(tmax.x, tmax.y), min(tmax.x, tmax.z));
float tnear = max(largest_tmin, 0.f);
float tfar = smallest_tmax;
return float2(tnear, tfar);
}
void swap2(int& a, int& b) { int temp = a; a = b; b = temp; }
// standard ray triangle intersection routines (for debugging purposes only)
// based on Intersect::RayTriangle() in original Aila/Laine code
Vec3f intersectRayTriangle(const Vec3f& v0, const Vec3f& v1, const Vec3f& v2, const Vec4f& rayorig, const Vec4f& raydir) {
const Vec3f rayorig3f = Vec3f(rayorig.x, rayorig.y, rayorig.z);
const Vec3f raydir3f = Vec3f(raydir.x, raydir.y, raydir.z);
const float EPSILON = 0.00001f; // works better
const Vec3f miss(F32_MAX, F32_MAX, F32_MAX);
float raytmin = rayorig.w;
float raytmax = raydir.w;
Vec3f edge1 = v1 - v0;
Vec3f edge2 = v2 - v0;
Vec3f tvec = rayorig3f - v0;
Vec3f pvec = cross(raydir3f, edge2);
float det = dot(edge1, pvec);
float invdet = 1.0f / det;
float u = dot(tvec, pvec) * invdet;
Vec3f qvec = cross(tvec, edge1);
float v = dot(raydir3f, qvec) * invdet;
if (det > EPSILON)
{
//if (u < 0.0f || u > 1.0f) return miss; // 1.0 want = det * 1/det
//if (v < 0.0f || (u + v) > 1.0f) return miss;
// if u and v are within these bounds, continue and go to float t = dot(...
}
else if (det < -EPSILON)
{
//if (u > 0.0f || u < 1.0f) return miss;
//if (v > 0.0f || (u + v) < 1.0f) return miss;
// else continue
}
else // if det is not larger (more positive) than EPSILON or not smaller (more negative) than -EPSILON, there is a "miss"
return miss;
float t = dot(edge2, qvec) * invdet;
if (t > raytmin && t < raytmax)
return Vec3f(u, v, t);
// otherwise (t < raytmin or t > raytmax) miss
return miss;
}
// modified intersection routine (uses regular instead of woopified triangles) for debugging purposes
void DEBUGintersectBVHandTriangles(const float4 rayorig, const float4 raydir,
const float4* gpuNodes, const float4* gpuTriWoops, const float4* gpuDebugTris, const int* gpuTriIndices,
int& hitTriIdx, float& hitdistance, int& debugbingo, Vec3f& trinormal, bool needClosestHit) { // int leafcount, int tricount,
int traversalStack[STACK_SIZE];
float origx, origy, origz; // Ray origin.
float dirx, diry, dirz; // Ray direction.
float tmin; // t-value from which the ray starts. Usually 0.
float idirx, idiry, idirz; // 1 / dir
float oodx, oody, oodz; // orig / dir
char* stackPtr;
int leafAddr;
int nodeAddr;
int hitIndex;
float hitT;
int threadId1;
//threadId1 = threadIdx.x + blockDim.x * (threadIdx.y + blockDim.y * (blockIdx.x + gridDim.x * blockIdx.y));
origx = rayorig.x;
origy = rayorig.y;
origz = rayorig.z;
dirx = raydir.x;
diry = raydir.y;
dirz = raydir.z;
tmin = rayorig.w;
// ooeps is very small number, used instead of raydir xyz component when that component is near zero
float ooeps = exp2f(-80.0f); // Avoid div by zero, returns 1/2^80, an extremely small number
//float idirx__ = 1.0f / (fabsf(raydir.x) > ooeps ? raydir.x : copysignf(ooeps, raydir.x)); // inverse ray direction
//float idiry__ = 1.0f / (fabsf(raydir.y) > ooeps ? raydir.y : copysignf(ooeps, raydir.y)); // inverse ray direction
//float idirz__ = 1.0f / (fabsf(raydir.z) > ooeps ? raydir.z : copysignf(ooeps, raydir.z)); // inverse ray direction
float ooeps_x = raydir.x >= 0 ? ooeps : -ooeps;
float ooeps_y = raydir.y >= 0 ? ooeps : -ooeps;
float ooeps_z = raydir.z >= 0 ? ooeps : -ooeps;
idirx = 1.0f / (abs(raydir.x) > ooeps ? raydir.x : ooeps_x); // inverse ray direction
idiry = 1.0f / (abs(raydir.y) > ooeps ? raydir.y : ooeps_y); // inverse ray direction
idirz = 1.0f / (abs(raydir.z) > ooeps ? raydir.z : ooeps_z); // inverse ray direction
oodx = origx * idirx; // ray origin / ray direction
oody = origy * idiry; // ray origin / ray direction
oodz = origz * idirz; // ray origin / ray direction
traversalStack[0] = EntrypointSentinel; // Bottom-most entry. 0x76543210 is 1985229328 in decimal
stackPtr = (char*)&traversalStack[0]; // point stackPtr to bottom of traversal stack = EntryPointSentinel
leafAddr = 0; // No postponed leaf.
nodeAddr = 0; // Start from the root.
hitIndex = -1; // No triangle intersected so far.
hitT = raydir.w;
while (nodeAddr != EntrypointSentinel) // EntrypointSentinel = 0x76543210
{
// Traverse internal nodes until all SIMD lanes have found a leaf.
bool searchingLeaf = true; // flag required to increase efficiency of threads in warp
while (nodeAddr >= 0 && nodeAddr != EntrypointSentinel)
{
float4* ptr = (float4*)((char*)gpuNodes + nodeAddr);
float4 __n0xy = ptr[0]; // childnode 0, xy-bounds (c0.lo.x, c0.hi.x, c0.lo.y, c0.hi.y)
float4 __n1xy = ptr[1]; // childnode 1. xy-bounds (c1.lo.x, c1.hi.x, c1.lo.y, c1.hi.y)
float4 __nz = ptr[2]; // childnodes 0 and 1, z-bounds(c0.lo.z, c0.hi.z, c1.lo.z, c1.hi.z)
int nodeIdx = nodeAddr / (int)16;
float4 n0xy = gpuNodes[nodeIdx];// ptr[0]; // childnode 0, xy-bounds (c0.lo.x, c0.hi.x, c0.lo.y, c0.hi.y)
float4 n1xy = gpuNodes[nodeIdx + 1];//ptr[1]; // childnode 1. xy-bounds (c1.lo.x, c1.hi.x, c1.lo.y, c1.hi.y)
float4 nz = gpuNodes[nodeIdx + 2];//ptr[2]; // childnodes 0 and 1, z-bounds(c0.lo.z, c0.hi.z, c1.lo.z, c1.hi.z)
float3 __aabb0_min = float3(n0xy.x, n0xy.z, nz.x);
float3 __aabb0_max = float3(n0xy.y, n0xy.w, nz.y);
float3 __aabb1_min = float3(n1xy.x, n0xy.z, nz.z);
float3 __aabb1_max = float3(n1xy.y, n0xy.w, nz.w);
float3 __pos_org = float3(rayorig.x, rayorig.y, rayorig.z);
float3 __ray_dir = float3(raydir.x, raydir.y, raydir.z);
float2 tt0 = ComputeAaBbHits(__pos_org, __aabb0_min, __aabb0_max, __ray_dir);
float2 tt1 = ComputeAaBbHits(__pos_org, __aabb1_min, __aabb1_max, __ray_dir);
// ptr[3] contains indices to 2 childnodes in case of innernode, see below
// (childindex = size of array during building, see CudaBVH.cpp)
// compute ray intersections with BVH node bounding box
float c0lox = n0xy.x * idirx - oodx; // n0xy.x = c0.lo.x, child 0 minbound x
float c0hix = n0xy.y * idirx - oodx; // n0xy.y = c0.hi.x, child 0 maxbound x
float c0loy = n0xy.z * idiry - oody; // n0xy.z = c0.lo.y, child 0 minbound y
float c0hiy = n0xy.w * idiry - oody; // n0xy.w = c0.hi.y, child 0 maxbound y
float c0loz = nz.x * idirz - oodz; // nz.x = c0.lo.z, child 0 minbound z
float c0hiz = nz.y * idirz - oodz; // nz.y = c0.hi.z, child 0 maxbound z
float c1loz = nz.z * idirz - oodz; // nz.z = c1.lo.z, child 1 minbound z
float c1hiz = nz.w * idirz - oodz; // nz.w = c1.hi.z, child 1 maxbound z
float c0min = spanBeginKepler(c0lox, c0hix, c0loy, c0hiy, c0loz, c0hiz, tmin); // Tesla does max4(min, min, min, tmin)
float c0max = spanEndKepler(c0lox, c0hix, c0loy, c0hiy, c0loz, c0hiz, hitT); // Tesla does min4(max, max, max, tmax)
float c1lox = n1xy.x * idirx - oodx; // n1xy.x = c1.lo.x, child 1 minbound x
float c1hix = n1xy.y * idirx - oodx; // n1xy.y = c1.hi.x, child 1 maxbound x
float c1loy = n1xy.z * idiry - oody; // n1xy.z = c1.lo.y, child 1 minbound y
float c1hiy = n1xy.w * idiry - oody; // n1xy.w = c1.hi.y, child 1 maxbound y
float c1min = spanBeginKepler(c1lox, c1hix, c1loy, c1hiy, c1loz, c1hiz, tmin);
float c1max = spanEndKepler(c1lox, c1hix, c1loy, c1hiy, c1loz, c1hiz, hitT);
float ray_tmax = 1e20;
bool traverseChild0 = (c0min <= c0max) && (c0min >= tmin) && (c0min <= ray_tmax);
bool traverseChild1 = (c1min <= c1max) && (c1min >= tmin) && (c1min <= ray_tmax);
if (!traverseChild0 && !traverseChild1)
{
nodeAddr = *(int*)stackPtr; // fetch next node by popping stack
stackPtr -= 4; // popping decrements stack by 4 bytes (because stackPtr is a pointer to char)
}
// Otherwise => fetch child pointers.
else // one or both children intersected
{
//vmint2 cnodes = *(vmint2*)&ptr[3];
float4 nodef4 = gpuNodes[nodeIdx + 3];
vmint2 cnodes = vmint2(*(int*)&(nodef4.x), *(int*)&(nodef4.y));// = *(int2*) & ptr[3];
// set nodeAddr equal to intersected childnode (first childnode when both children are intersected)
nodeAddr = (traverseChild0) ? cnodes.x : cnodes.y;
// Both children were intersected => push the farther one on the stack.
if (traverseChild0 && traverseChild1) // store closest child in nodeAddr, swap if necessary
{
if (c1min < c0min)
swap2(nodeAddr, cnodes.y);
stackPtr += 4; // pushing increments stack by 4 bytes (stackPtr is a pointer to char)
*(int*)stackPtr = cnodes.y; // push furthest node on the stack
}
}
// First leaf => postpone and continue traversal.
// leafnodes have a negative index to distinguish them from inner nodes
// if nodeAddr less than 0 -> nodeAddr is a leaf
if (nodeAddr < 0 && leafAddr >= 0) // if leafAddr >= 0 -> no leaf found yet (first leaf)
{
searchingLeaf = false; // required for warp efficiency
leafAddr = nodeAddr;
nodeAddr = *(int*)stackPtr; // pops next node from stack
stackPtr -= 4; // decrement by 4 bytes (stackPtr is a pointer to char)
}
// All SIMD lanes have found a leaf => process them.
// NOTE: inline PTX implementation of "if(!__any(leafAddr >= 0)) break;".
// tried everything with CUDA 4.2 but always got several redundant instructions.
// if (!searchingLeaf){ break; }
// if (!__any(searchingLeaf)) break; // "__any" keyword: if none of the threads is searching a leaf, in other words
// if all threads in the warp found a leafnode, then break from while loop and go to triangle intersection
// if(!__any(leafAddr >= 0)) /// als leafAddr in PTX code >= 0, dan is het geen echt leafNode
// break;
unsigned int mask; // mask replaces searchingLeaf in PTX code
bool p = leafAddr >= 0;
if (!p) {
break;
}
//asm("{\n"
// " .reg .pred p; \n"
// "setp.ge.s32 p, %1, 0; \n"
// "vote.ballot.b32 %0,p; \n"
// "}"
// : "=r"(mask)
// : "r"(leafAddr));
//
//if (!mask)
// break;
}
///////////////////////////////////////
/// LEAF NODE / TRIANGLE INTERSECTION
///////////////////////////////////////
while (leafAddr < 0) // if leafAddr is negative, it points to an actual leafnode (when positive or 0 it's an innernode
{
// leafAddr is stored as negative number, see cidx[i] = ~triWoopData.getSize(); in CudaBVH.cpp
for (int triAddr = ~leafAddr;; triAddr += 3)
{ // no defined upper limit for loop, continues until leaf terminator code 0x80000000 is encountered
// Read first 16 bytes of the triangle.
// fetch first triangle vertex
float4 v0f = gpuDebugTris[triAddr + 0];
// End marker 0x80000000 (= negative zero) => all triangles in leaf processed. --> terminate
if (__float_as_int(v0f.x) == 0x80000000) break;
float4 v1f = gpuDebugTris[triAddr + 1];
float4 v2f = gpuDebugTris[triAddr + 2];
const Vec3f v0 = Vec3f(v0f.x, v0f.y, v0f.z);
const Vec3f v1 = Vec3f(v1f.x, v1f.y, v1f.z);
const Vec3f v2 = Vec3f(v2f.x, v2f.y, v2f.z);
// convert float4 to Vec4f
Vec4f rayorigvec4f = Vec4f(rayorig.x, rayorig.y, rayorig.z, rayorig.w);
Vec4f raydirvec4f = Vec4f(raydir.x, raydir.y, raydir.z, raydir.w);
Vec3f bary = intersectRayTriangle(v0, v1, v2, rayorigvec4f, raydirvec4f);
float t = bary.z; // hit distance along ray
if (t > tmin && t < hitT) // if there is a miss, t will be larger than hitT (ray.tmax)
{
hitIndex = triAddr;
hitT = t; /// keeps track of closest hitpoint
trinormal = cross(v0 - v1, v0 - v2);
if (!needClosestHit) { // shadow rays only require "any" hit with scene geometry, not the closest one
nodeAddr = EntrypointSentinel;
break;
}
}
} // triangle
// Another leaf was postponed => process it as well.
leafAddr = nodeAddr;
if (nodeAddr < 0)
{
nodeAddr = *(int*)stackPtr; // pop stack
stackPtr -= 4; // decrement with 4 bytes to get the next int (stackPtr is char*)
}
} // end leaf/triangle intersection loop
} // end of node traversal loop
// Remap intersected triangle index, and store the result.
if (hitIndex != -1) {
// remapping tri indices delayed until this point for performance reasons
// (slow global memory lookup in de gpuTriIndices array) because multiple triangles per node can potentially be hit
hitIndex = gpuTriIndices[hitIndex];
}
hitTriIdx = hitIndex;
hitdistance = hitT;
}
void intersectBVHandTriangles(const float4 rayorig, const float4 raydir,
const float4* gpuNodes, const float4* gpuTriWoops, const float4* gpuDebugTris, const int* gpuTriIndices,
int& hitTriIdx, float& hitdistance, int& debugbingo, Vec3f& trinormal, int leafcount, int tricount, bool anyHit)
{
// assign a CUDA thread to every pixel by using the threadIndex
// global threadId, see richiesams blogspot
//int thread_index = (blockIdx.x + blockIdx.y * gridDim.x) * (blockDim.x * blockDim.y) + (threadIdx.y * blockDim.x) + threadIdx.x;
///////////////////////////////////////////
//// FERMI / KEPLER KERNEL
///////////////////////////////////////////
// BVH layout Compact2 for Kepler, Ccompact for Fermi (nodeOffsetSizeDiv is different)
// void CudaBVH::createCompact(const BVH& bvh, int nodeOffsetSizeDiv)
// createCompact(bvh,16); for Compact2
// createCompact(bvh,1); for Compact
int traversalStack[STACK_SIZE];
// Live state during traversal, stored in registers.
int rayidx; // not used, can be removed
float origx, origy, origz; // Ray origin.
float dirx, diry, dirz; // Ray direction.
float tmin; // t-value from which the ray starts. Usually 0.
float idirx, idiry, idirz; // 1 / ray direction
float oodx, oody, oodz; // ray origin / ray direction
char* stackPtr; // Current position in traversal stack.
int leafAddr; // If negative, then first postponed leaf, non-negative if no leaf (innernode).
int nodeAddr;
int hitIndex; // Triangle index of the closest intersection, -1 if none.
float hitT; // t-value of the closest intersection.
// Kepler kernel only
//int leafAddr2; // Second postponed leaf, non-negative if none.
//int nodeAddr = EntrypointSentinel; // Non-negative: current internal node, negative: second postponed leaf.
int threadId1; // ipv rayidx
// Initialize (stores local variables in registers)
{
// Pick ray index.
//threadId1 = threadIdx.x + blockDim.x * (threadIdx.y + blockDim.y * (blockIdx.x + gridDim.x * blockIdx.y));
// Fetch ray.
// required when tracing ray batches
// float4 o = rays[rayidx * 2 + 0];
// float4 d = rays[rayidx * 2 + 1];
//__shared__ volatile int nextRayArray[MaxBlockHeight]; // Current ray index in global buffer.
origx = rayorig.x;
origy = rayorig.y;
origz = rayorig.z;
dirx = raydir.x;
diry = raydir.y;
dirz = raydir.z;
tmin = rayorig.w;
// ooeps is very small number, used instead of raydir xyz component when that component is near zero
float ooeps = exp2f(-80.0f); // Avoid div by zero, returns 1/2^80, an extremely small number
idirx = 1.0f / (fabsf(raydir.x) > ooeps ? raydir.x : copysignf(ooeps, raydir.x)); // inverse ray direction
idiry = 1.0f / (fabsf(raydir.y) > ooeps ? raydir.y : copysignf(ooeps, raydir.y)); // inverse ray direction
idirz = 1.0f / (fabsf(raydir.z) > ooeps ? raydir.z : copysignf(ooeps, raydir.z)); // inverse ray direction
oodx = origx * idirx; // ray origin / ray direction
oody = origy * idiry; // ray origin / ray direction
oodz = origz * idirz; // ray origin / ray direction
// Setup traversal + initialisation
traversalStack[0] = EntrypointSentinel; // Bottom-most entry. 0x76543210 (1985229328 in decimal)
stackPtr = (char*)&traversalStack[0]; // point stackPtr to bottom of traversal stack = EntryPointSentinel
leafAddr = 0; // No postponed leaf.
nodeAddr = 0; // Start from the root.
hitIndex = -1; // No triangle intersected so far.
hitT = raydir.w; // tmax
}
// Traversal loop.
while (nodeAddr != EntrypointSentinel)
{
// Traverse internal nodes until all SIMD lanes have found a leaf.
bool searchingLeaf = true; // required for warp efficiency
while (nodeAddr >= 0 && nodeAddr != EntrypointSentinel)
{
// Fetch AABBs of the two child nodes.
// nodeAddr is an offset in number of bytes (char) in gpuNodes array
float4* ptr = (float4*)((char*)gpuNodes + nodeAddr);
float4 n0xy = ptr[0]; // childnode 0, xy-bounds (c0.lo.x, c0.hi.x, c0.lo.y, c0.hi.y)
float4 n1xy = ptr[1]; // childnode 1, xy-bounds (c1.lo.x, c1.hi.x, c1.lo.y, c1.hi.y)
float4 nz = ptr[2]; // childnode 0 and 1, z-bounds (c0.lo.z, c0.hi.z, c1.lo.z, c1.hi.z)
// ptr[3] contains indices to 2 childnodes in case of innernode, see below
// (childindex = size of array during building, see CudaBVH.cpp)
// compute ray intersections with BVH node bounding box
/// RAY BOX INTERSECTION
// Intersect the ray against the child nodes.
float c0lox = n0xy.x * idirx - oodx; // n0xy.x = c0.lo.x, child 0 minbound x
float c0hix = n0xy.y * idirx - oodx; // n0xy.y = c0.hi.x, child 0 maxbound x
float c0loy = n0xy.z * idiry - oody; // n0xy.z = c0.lo.y, child 0 minbound y
float c0hiy = n0xy.w * idiry - oody; // n0xy.w = c0.hi.y, child 0 maxbound y
float c0loz = nz.x * idirz - oodz; // nz.x = c0.lo.z, child 0 minbound z
float c0hiz = nz.y * idirz - oodz; // nz.y = c0.hi.z, child 0 maxbound z
float c1loz = nz.z * idirz - oodz; // nz.z = c1.lo.z, child 1 minbound z
float c1hiz = nz.w * idirz - oodz; // nz.w = c1.hi.z, child 1 maxbound z
float c0min = spanBeginKepler(c0lox, c0hix, c0loy, c0hiy, c0loz, c0hiz, tmin); // Tesla does max4(min, min, min, tmin)
float c0max = spanEndKepler(c0lox, c0hix, c0loy, c0hiy, c0loz, c0hiz, hitT); // Tesla does min4(max, max, max, tmax)
float c1lox = n1xy.x * idirx - oodx; // n1xy.x = c1.lo.x, child 1 minbound x
float c1hix = n1xy.y * idirx - oodx; // n1xy.y = c1.hi.x, child 1 maxbound x
float c1loy = n1xy.z * idiry - oody; // n1xy.z = c1.lo.y, child 1 minbound y
float c1hiy = n1xy.w * idiry - oody; // n1xy.w = c1.hi.y, child 1 maxbound y
float c1min = spanBeginKepler(c1lox, c1hix, c1loy, c1hiy, c1loz, c1hiz, tmin);
float c1max = spanEndKepler(c1lox, c1hix, c1loy, c1hiy, c1loz, c1hiz, hitT);
// ray box intersection boundary tests:
float ray_tmax = 1e20;
bool traverseChild0 = (c0min <= c0max); // && (c0min >= tmin) && (c0min <= ray_tmax);
bool traverseChild1 = (c1min <= c1max); // && (c1min >= tmin) && (c1min <= ray_tmax);
// Neither child was intersected => pop stack.
if (!traverseChild0 && !traverseChild1)
{
nodeAddr = *(int*)stackPtr; // fetch next node by popping the stack
stackPtr -= 4; // popping decrements stackPtr by 4 bytes (because stackPtr is a pointer to char)
}
// Otherwise, one or both children intersected => fetch child pointers.
else
{
vmint2 cnodes = *(vmint2*)&ptr[3];
// set nodeAddr equal to intersected childnode index (or first childnode when both children are intersected)
nodeAddr = (traverseChild0) ? cnodes.x : cnodes.y;
// Both children were intersected => push the farther one on the stack.
if (traverseChild0 && traverseChild1) // store closest child in nodeAddr, swap if necessary
{
if (c1min < c0min)
swap2(nodeAddr, cnodes.y);
stackPtr += 4; // pushing increments stack by 4 bytes (stackPtr is a pointer to char)
*(int*)stackPtr = cnodes.y; // push furthest node on the stack
}
}
// First leaf => postpone and continue traversal.
// leafnodes have a negative index to distinguish them from inner nodes
// if nodeAddr less than 0 -> nodeAddr is a leaf
if (nodeAddr < 0 && leafAddr >= 0)
{
searchingLeaf = false; // required for warp efficiency
leafAddr = nodeAddr;
nodeAddr = *(int*)stackPtr; // pops next node from stack
stackPtr -= 4; // decrements stackptr by 4 bytes (because stackPtr is a pointer to char)
}
// All SIMD lanes have found a leaf => process them.
// to increase efficiency, check if all the threads in a warp have found a leaf before proceeding to the
// ray/triangle intersection routine
// this bit of code requires PTX (CUDA assembly) code to work properly
// if (!__any(searchingLeaf)) -> "__any" keyword: if none of the threads is searching a leaf, in other words
// if all threads in the warp found a leafnode, then break from while loop and go to triangle intersection
//if(!__any(leafAddr >= 0))
// break;
// if (!__any(searchingLeaf))
// break; /// break from while loop and go to code below, processing leaf nodes
// NOTE: inline PTX implementation of "if(!__any(leafAddr >= 0)) break;".
// tried everything with CUDA 4.2 but always got several redundant instructions.
unsigned int mask; // replaces searchingLeaf
bool p = leafAddr >= 0;
if (!p) {
break;
}
//asm("{\n"
// " .reg .pred p; \n"
// "setp.ge.s32 p, %1, 0; \n"
// "vote.ballot.b32 %0,p; \n"
// "}"
// : "=r"(mask)
// : "r"(leafAddr));
//
//if (!mask)
// break;
}
///////////////////////////////////////////
/// TRIANGLE INTERSECTION
//////////////////////////////////////
// Process postponed leaf nodes.
while (leafAddr < 0) /// if leafAddr is negative, it points to an actual leafnode (when positive or 0 it's an innernode)
{
// Intersect the ray against each triangle using Sven Woop's algorithm.
// Woop ray triangle intersection: Woop triangles are unit triangles. Each ray
// must be transformed to "unit triangle space", before testing for intersection
for (int triAddr = ~leafAddr;; triAddr += 3) // triAddr is index in triWoop array (and bitwise complement of leafAddr)
{ // no defined upper limit for loop, continues until leaf terminator code 0x80000000 is encountered
// Read first 16 bytes of the triangle.
// fetch first precomputed triangle edge
float4 v00 = triWoopTexture[triAddr];// tex1Dfetch(triWoopTexture, triAddr);
// End marker 0x80000000 (negative zero) => all triangles in leaf processed --> terminate
if (__float_as_int(v00.x) == 0x80000000)
break;
// Compute and check intersection t-value (hit distance along ray).
float Oz = v00.w - origx * v00.x - origy * v00.y - origz * v00.z; // Origin z
float invDz = 1.0f / (dirx * v00.x + diry * v00.y + dirz * v00.z); // inverse Direction z
float t = Oz * invDz;
if (t > tmin && t < hitT)
{
// Compute and check barycentric u.
// fetch second precomputed triangle edge
float4 v11 = triWoopTexture[triAddr + 1];// tex1Dfetch(triWoopTexture, triAddr + 1);
float Ox = v11.w + origx * v11.x + origy * v11.y + origz * v11.z; // Origin.x
float Dx = dirx * v11.x + diry * v11.y + dirz * v11.z; // Direction.x
float u = Ox + t * Dx; /// parametric equation of a ray (intersection point)
if (u >= 0.0f && u <= 1.0f)
{
// Compute and check barycentric v.
// fetch third precomputed triangle edge
float4 v22 = triWoopTexture[triAddr + 2];// tex1Dfetch(triWoopTexture, triAddr + 2);
float Oy = v22.w + origx * v22.x + origy * v22.y + origz * v22.z;
float Dy = dirx * v22.x + diry * v22.y + dirz * v22.z;
float v = Oy + t * Dy;
if (v >= 0.0f && u + v <= 1.0f)
{
// We've got a hit!
// Record intersection.
hitT = t;
hitIndex = triAddr; // store triangle index for shading
// Closest intersection not required => terminate.
if (anyHit) // only true for shadow rays
{
nodeAddr = EntrypointSentinel;
break;
}
// compute normal vector by taking the cross product of two edge vectors
// because of Woop transformation, only one set of vectors works
//trinormal = cross(Vec3f(v22.x, v22.y, v22.z), Vec3f(v11.x, v11.y, v11.z)); // works
trinormal = cross(Vec3f(v11.x, v11.y, v11.z), Vec3f(v22.x, v22.y, v22.z));
}
}
}
} // end triangle intersection
// Another leaf was postponed => process it as well.
leafAddr = nodeAddr;
if (nodeAddr < 0) // nodeAddr is an actual leaf when < 0
{
nodeAddr = *(int*)stackPtr; // pop stack
stackPtr -= 4; // decrement with 4 bytes to get the next int (stackPtr is char*)
}
} // end leaf/triangle intersection loop
} // end traversal loop (AABB and triangle intersection)
// Remap intersected triangle index, and store the result.
if (hitIndex != -1) {
hitIndex = triIndicesTexture[hitIndex];// tex1Dfetch(triIndicesTexture, hitIndex);
// remapping tri indices delayed until this point for performance reasons
// (slow texture memory lookup in de triIndicesTexture) because multiple triangles per node can potentially be hit
}
hitTriIdx = hitIndex;
hitdistance = hitT;
}
bool RenderSrSlicer(VmFnContainer* _fncontainer,
VmGpuManager* gpu_manager,
grd_helper::GpuDX11CommonParameters* dx11CommonParams,
LocalProgress* progress,
double* run_time_ptr)
{
// https://atyuwen.github.io/posts/antialiased-line/
using namespace std::chrono;
//((std::mutex*)HDx11GetMutexGpuCriticalPath())->lock();
#pragma region // Parameter Setting //
VmIObject* iobj = _fncontainer->fnParams.GetParam("_VmIObject*_RenderOut", (VmIObject*)NULL);
int k_value_old = iobj->GetObjParam("_int_NumK", (int)K_NUM_SLICER);
int k_value = _fncontainer->fnParams.GetParam("_int_NumK", k_value_old);
iobj->SetObjParam("_int_NumK", k_value);
int num_moments_old = iobj->GetObjParam("_int_NumQueueLayers", (int)8);
int num_moments = _fncontainer->fnParams.GetParam("_int_NumQueueLayers", num_moments_old);
int num_safe_loopexit = _fncontainer->fnParams.GetParam("_int_SpinLockSafeLoops", (int)10000000);
bool is_final_renderer = _fncontainer->fnParams.GetParam("_bool_IsFinalRenderer", true);
//double v_discont_depth = _fncontainer->fnParams.GetParam("_float_DiscontDepth", -1.0);
float merging_beta = _fncontainer->fnParams.GetParam("_float_MergingBeta", 0.5f);
bool blur_SSAO = _fncontainer->fnParams.GetParam("_bool_BlurSSAO", true);
int camClipMode = _fncontainer->fnParams.GetParam("_int_ClippingMode", (int)0);
vmfloat3 camClipPlanePos = _fncontainer->fnParams.GetParam("_float3_PosClipPlaneWS", vmfloat3(0));
vmfloat3 camClipPlaneDir = _fncontainer->fnParams.GetParam("_float3_VecClipPlaneWS", vmfloat3(0));
vmmat44f camClipMatWS2BS = _fncontainer->fnParams.GetParam("_matrix44f_MatrixClipWS2BS", vmmat44f(1));
std::set<int> camClipperFreeActors = _fncontainer->fnParams.GetParam("_set_int_CamClipperFreeActors", std::set<int>());
bool is_picking_routine = _fncontainer->fnParams.GetParam("_bool_IsPickingRoutine", false);
#ifdef DX10_0
is_picking_routine = false;
#endif
vmint2 picking_pos_ss = _fncontainer->fnParams.GetParam("_int2_PickingPosSS", vmint2(-1, -1));
int buf_ex_scale = _fncontainer->fnParams.GetParam("_int_BufExScale", (int)8); // scaling the capacity of the k-buffer for _bool_PixelTransmittance
bool use_blending_option_MomentOIT = _fncontainer->fnParams.GetParam("_bool_UseBlendingOptionMomentOIT", false);
bool check_pixel_transmittance = _fncontainer->fnParams.GetParam("_bool_PixelTransmittance", false);
//vr_level = 2;
vmfloat4 default_phong_lighting_coeff = vmfloat4(0.2, 1.0, 0.5, 5); // Emission, Diffusion, Specular, Specular Power
float default_point_thickness = _fncontainer->fnParams.GetParam("_float_PointThickness", 3.0f);
float default_surfel_size = _fncontainer->fnParams.GetParam("_float_SurfelSize", 0.0f);
float default_line_thickness = _fncontainer->fnParams.GetParam("_float_LineThickness", 2.0f);
vmfloat3 default_color_cmmobj = _fncontainer->fnParams.GetParam("_float3_CmmGlobalColor", vmfloat3(-1, -1, -1));
bool use_spinlock_pixsynch = _fncontainer->fnParams.GetParam("_bool_UseSpinLock", false);
bool is_ghost_mode = _fncontainer->fnParams.GetParam("_bool_GhostEffect", false);
bool is_rgba = _fncontainer->fnParams.GetParam("_bool_IsRGBA", false); // false means bgra
bool isDrawingOnlyContours = _fncontainer->fnParams.GetParam("_bool_DrawingOnlyContours", false);
// note planeThickness is defined in WS
float planeThickness = _fncontainer->fnParams.GetParam("_float_PlaneThickness", 0.f);
bool is_system_out = false;
// note : planeThickness == 0 calls CPU renderer which uses system-out buffer
if (is_final_renderer || planeThickness <= 0.f) is_system_out = true;
//is_system_out = true;
bool only_surface_test = _fncontainer->fnParams.GetParam("_bool_OnlySurfaceTest", false);
bool test_consoleout = _fncontainer->fnParams.GetParam("_bool_TestConsoleOut", false);
bool test_fps_profiling = _fncontainer->fnParams.GetParam("_bool_TestFpsProfile", false);
auto test_out = [&test_consoleout](const string& _message)
{
if (test_consoleout)
cout << _message << endl;
};
float sample_rate = _fncontainer->fnParams.GetParam("_float_UserSampleRate", 0.0f);
if (sample_rate <= 0) sample_rate = 1.0f;
bool apply_samplerate2gradient = _fncontainer->fnParams.GetParam("_bool_ApplySampleRateToGradient", false);
bool reload_hlsl_objs = _fncontainer->fnParams.GetParam("_bool_ReloadHLSLObjFiles", false);
int __BLOCKSIZE = _fncontainer->fnParams.GetParam("_int_GpuThreadBlockSize", (int)4);
float v_thickness = _fncontainer->fnParams.GetParam("_float_VZThickness", 0.0f);
float gi_v_thickness = _fncontainer->fnParams.GetParam("_float_GIVZThickness", v_thickness);
float scale_z_res = _fncontainer->fnParams.GetParam("_float_zResScale", 1.0f);
int i_test_shader = (int)_fncontainer->fnParams.GetParam("_int_ShaderTest", (int)0);
VmLight* light = _fncontainer->fnParams.GetParamPtr<VmLight>("_VmLight_LightSource");
VmLens* lens = _fncontainer->fnParams.GetParam("_VmLens*_CamLens", (VmLens*)NULL);
LightSource light_src;
GlobalLighting global_lighting;
LensEffect lens_effect;
if (light) {
light_src.is_on_camera = light->is_on_camera;
light_src.is_pointlight = light->is_pointlight;
light_src.light_pos = light->pos;
light_src.light_dir = light->dir;
light_src.light_ambient_color = vmfloat3(1.f);
light_src.light_diffuse_color = vmfloat3(1.f);
light_src.light_specular_color = vmfloat3(1.f);
global_lighting.apply_ssao = light->effect_ssao.is_on_ssao;
global_lighting.ssao_r_kernel = light->effect_ssao.kernel_r;
global_lighting.ssao_num_steps = light->effect_ssao.num_steps;
global_lighting.ssao_num_dirs = light->effect_ssao.num_dirs;
global_lighting.ssao_tangent_bias = light->effect_ssao.tangent_bias;
global_lighting.ssao_blur = light->effect_ssao.smooth_filter;
global_lighting.ssao_intensity = light->effect_ssao.ao_power;
global_lighting.ssao_debug = _fncontainer->fnParams.GetParam("_int_SSAOOutput", (int)0);
}
if (lens) {
lens_effect.apply_ssdof = lens->apply_ssdof;
lens_effect.dof_focus_z = lens->dof_focus_z;
lens_effect.dof_lens_F = lens->dof_lens_F;
lens_effect.dof_lens_r = lens->dof_lens_r;
lens_effect.dof_ray_num_samples = lens->dof_ray_num_samples;
}
#pragma endregion
#pragma region // Shader Setting
// Shader Re-Compile Setting //
if (reload_hlsl_objs)
{
char ownPth[2048];
GetModuleFileNameA(NULL, ownPth, (sizeof(ownPth)));
string exe_path = ownPth;
size_t pos = 0;
std::string token;
string delimiter = "\\";
string hlslobj_path = "";
while ((pos = exe_path.find(delimiter)) != std::string::npos) {
token = exe_path.substr(0, pos);
if (token.find(".exe") != std::string::npos) break;
hlslobj_path += token + "\\";
exe_path.erase(0, pos + delimiter.length());
}
//hlslobj_path += "..\\..\\VmModuleProjects\\hybrid_rendering_engine\\";
hlslobj_path += "..\\..\\VmProjects\\hybrid_rendering_engine\\";
string enginePath;
if (grd_helper::GetEnginePath(enginePath)) {
hlslobj_path = enginePath;
}
string hlslobj_path_4_0 = hlslobj_path + "shader_compiled_objs_4_0\\";
//cout << hlslobj_path << endl;
#ifdef DX10_0
hlslobj_path += "shader_compiled_objs_4_0\\";
#else
hlslobj_path += "shader_compiled_objs\\";
#endif
string prefix_path = hlslobj_path;
vmlog::LogInfo("RELOAD HLSL _ SR slicer renderer");
dx11CommonParams->dx11DeviceImmContext->VSSetShader(NULL, NULL, 0);
dx11CommonParams->dx11DeviceImmContext->GSSetShader(NULL, NULL, 0);
dx11CommonParams->dx11DeviceImmContext->PSSetShader(NULL, NULL, 0);
dx11CommonParams->dx11DeviceImmContext->CSSetShader(NULL, NULL, 0);
#define VS_NUM 5
#define GS_NUM 1
#ifdef DX10_0
#define PS_NUM 3
#define SET_PS(NAME, __S) dx11CommonParams->safe_set_res(grd_helper::COMRES_INDICATOR(GpuhelperResType::PIXEL_SHADER, NAME), __S, true)
#else
#define CS_NUM 6
#define SET_CS(NAME, __S) dx11CommonParams->safe_set_res(grd_helper::COMRES_INDICATOR(GpuhelperResType::COMPUTE_SHADER, NAME), __S, true)
#endif
#define SET_VS(NAME, __S) dx11CommonParams->safe_set_res(grd_helper::COMRES_INDICATOR(GpuhelperResType::VERTEX_SHADER, NAME), __S, true)
#define SET_GS(NAME, __S) dx11CommonParams->safe_set_res(grd_helper::COMRES_INDICATOR(GpuhelperResType::GEOMETRY_SHADER, NAME), __S, true)
#define GETRASTER(NAME) dx11CommonParams->get_rasterizer(#NAME)
#define GETDEPTHSTENTIL(NAME) dx11CommonParams->get_depthstencil(#NAME)
#ifdef DX10_0
string strNames_VS[VS_NUM] = {
"SR_OIT_P_vs_4_0"
,"SR_OIT_PN_vs_4_0"
,"SR_OIT_PT_vs_4_0"
,"SR_OIT_PNT_vs_4_0"
,"SR_OIT_PTTT_vs_4_0"
};
#else
string strNames_VS[VS_NUM] = {
"SR_OIT_P_vs_5_0"
,"SR_OIT_PN_vs_5_0"
,"SR_OIT_PT_vs_5_0"
,"SR_OIT_PNT_vs_5_0"
,"SR_OIT_PTTT_vs_5_0"
};
#endif
for (int i = 0; i < VS_NUM; i++)
{
string strName = strNames_VS[i];
FILE* pFile;
if (fopen_s(&pFile, (prefix_path + strName).c_str(), "rb") == 0)
{
fseek(pFile, 0, SEEK_END);
ullong ullFileSize = ftell(pFile);
fseek(pFile, 0, SEEK_SET);
byte* pyRead = new byte[ullFileSize];
fread(pyRead, sizeof(byte), ullFileSize, pFile);
fclose(pFile);
ID3D11VertexShader* dx11VShader = NULL;
if (dx11CommonParams->dx11Device->CreateVertexShader(pyRead, ullFileSize, NULL, &dx11VShader) != S_OK)
{
VMERRORMESSAGE("SHADER COMPILE FAILURE!");
}
else
{
SET_VS(strName, dx11VShader);
}
VMSAFE_DELETEARRAY(pyRead);
}
}
/**/
string strNames_GS[GS_NUM] = {
"GS_MeshCutLines_gs_4_0"
};
for (int i = 0; i < GS_NUM; i++)
{