-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpathtracer.cpp
1739 lines (1619 loc) · 83.3 KB
/
pathtracer.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 "pathtracer.h"
#include "scene.h"
#include "sampler.h"
#include "parallel.h"
#include "scene.h"
#include "buffer.h"
#include "camera.h"
#include "intersection.h"
#include "active_pixels.h"
#include "shape.h"
#include "material.h"
#include "light.h"
#include "test_utils.h"
#include "cuda_utils.h"
#include "thrust_utils.h"
#include <thrust/execution_policy.h>
#include <thrust/fill.h>
#include <thrust/sort.h>
#include <thrust/reduce.h>
#include <thrust/remove.h>
void init_paths(BufferView<Vector3> throughputs,
BufferView<Real> min_roughness,
bool use_gpu) {
DISPATCH(use_gpu, thrust::fill,
throughputs.begin(),
throughputs.end(),
Vector3{1, 1, 1});
DISPATCH(use_gpu, thrust::fill,
min_roughness.begin(),
min_roughness.end(),
Real(0));
}
struct d_primary_intersector {
DEVICE void operator()(int idx) {
// Initialize derivatives
auto d_primary_v = d_vertices + 3 * idx;
d_primary_v[0] = DVertex{};
d_primary_v[1] = DVertex{};
d_primary_v[2] = DVertex{};
auto &d_camera = d_cameras[idx];
d_camera = DCameraInst{};
auto pixel_idx = active_pixels[idx];
const auto &isect = isects[pixel_idx];
auto shape_id = isect.shape_id;
auto tri_id = isect.tri_id;
const auto &shape = shapes[shape_id];
auto ind = get_indices(shape, tri_id);
d_primary_v[0].shape_id = shape_id;
d_primary_v[0].vertex_id = ind[0];
d_primary_v[1].shape_id = shape_id;
d_primary_v[1].vertex_id = ind[1];
d_primary_v[2].shape_id = shape_id;
d_primary_v[2].vertex_id = ind[2];
auto d_ray = DRay{};
d_ray.dir += d_wos[pixel_idx];
d_intersect_shape(shape, tri_id, rays[pixel_idx], d_points[pixel_idx], d_ray, d_primary_v);
auto pixel_x = pixel_idx % camera.width;
auto pixel_y = pixel_idx / camera.width;
auto sample = samples[pixel_idx].xy;
auto screen_pos = Vector2{
(pixel_x + sample[0]) / Real(camera.width),
(pixel_y + sample[1]) / Real(camera.height)
};
d_sample_primary_ray(camera, screen_pos, d_ray, d_camera);
}
const Camera camera;
const Shape *shapes;
const int *active_pixels;
const CameraSample *samples;
const Ray *rays;
const Intersection *isects;
const Vector3 *d_wos;
const SurfacePoint *d_points;
DVertex *d_vertices;
DCameraInst *d_cameras;
};
void d_primary_intersection(const Scene &scene,
const BufferView<int> &active_pixels,
const BufferView<CameraSample> &samples,
const BufferView<Ray> &rays,
const BufferView<Intersection> &intersections,
const BufferView<Vector3> &d_wos,
const BufferView<SurfacePoint> &d_surface_points,
BufferView<DVertex> d_vertices,
BufferView<DCameraInst> d_cameras) {
parallel_for(d_primary_intersector{
scene.camera,
scene.shapes.data,
active_pixels.begin(),
samples.begin(),
rays.begin(),
intersections.begin(),
d_wos.begin(),
d_surface_points.begin(),
d_vertices.begin(),
d_cameras.begin()}, active_pixels.size(), scene.use_gpu);
}
struct direct_visible_lights_accumulator {
DEVICE void operator()(int idx) {
auto pixel_id = active_pixels[idx];
const auto &throughput = throughputs[pixel_id];
const auto &shading_isect = shading_isects[pixel_id];
const auto &shading_point = shading_points[pixel_id];
const auto &shading_shape = scene.shapes[shading_isect.shape_id];
auto wi = -incoming_rays[pixel_id].dir;
Vector3 emission = Vector3{0, 0, 0};
if (shading_shape.light_id >= 0 && dot(wi, shading_point.shading_frame.n) > 0) {
const auto &light = scene.lights[shading_shape.light_id];
emission += light.intensity;
}
auto contrib = weight * throughput * emission;
if (rendered_image != nullptr) {
rendered_image[3 * pixel_id ] += contrib[0];
rendered_image[3 * pixel_id + 1] += contrib[1];
rendered_image[3 * pixel_id + 2] += contrib[2];
}
if (edge_contribs != nullptr) {
edge_contribs[pixel_id] += sum(contrib);
}
}
const FlattenScene scene;
const int *active_pixels;
const Vector3 *throughputs;
const Ray *incoming_rays;
const Intersection *shading_isects;
const SurfacePoint *shading_points;
const Real weight;
float *rendered_image;
Real *edge_contribs;
};
struct d_direct_visible_lights_accumulator {
DEVICE void operator()(int idx) {
auto pixel_id = active_pixels[idx];
const auto &throughput = throughputs[pixel_id];
const auto &shading_isect = shading_isects[pixel_id];
const auto &shading_point = shading_points[pixel_id];
const auto &shading_shape = scene.shapes[shading_isect.shape_id];
auto wi = -incoming_rays[pixel_id].dir;
d_direct_lights[idx] = DLightInst{};
if (shading_shape.light_id >= 0 && dot(wi, shading_point.shading_frame.n) > 0) {
// contrib = weight * throughput * emission
auto d_path_contrib = weight * throughput *
Vector3{d_rendered_image[3 * pixel_id ],
d_rendered_image[3 * pixel_id + 1],
d_rendered_image[3 * pixel_id + 2]};
d_direct_lights[idx].light_id = shading_shape.light_id;
d_direct_lights[idx].intensity = d_path_contrib;
}
}
const FlattenScene scene;
const int *active_pixels;
const Vector3 *throughputs;
const Ray *incoming_rays;
const Intersection *shading_isects;
const SurfacePoint *shading_points;
const Real weight;
const float *d_rendered_image;
DLightInst *d_direct_lights;
};
void accumulate_direct_visible_lights(const Scene &scene,
const BufferView<int> &active_pixels,
const BufferView<Vector3> &throughputs,
const BufferView<Ray> &incoming_rays,
const BufferView<Intersection> &shading_isects,
const BufferView<SurfacePoint> &shading_points,
const Real weight,
float *rendered_image,
BufferView<Real> edge_contribs) {
parallel_for(direct_visible_lights_accumulator{
get_flatten_scene(scene),
active_pixels.begin(),
throughputs.begin(),
incoming_rays.begin(),
shading_isects.begin(),
shading_points.begin(),
weight,
rendered_image,
edge_contribs.begin()
}, active_pixels.size(), scene.use_gpu);
}
void d_accumulate_direct_visible_lights(const Scene &scene,
const BufferView<int> &active_pixels,
const BufferView<Vector3> &throughputs,
const BufferView<Ray> &incoming_rays,
const BufferView<Intersection> &shading_isects,
const BufferView<SurfacePoint> &shading_points,
const Real weight,
const float *d_rendered_image,
BufferView<DLightInst> d_direct_lights) {
parallel_for(d_direct_visible_lights_accumulator{
get_flatten_scene(scene),
active_pixels.begin(),
throughputs.begin(),
incoming_rays.begin(),
shading_isects.begin(),
shading_points.begin(),
weight,
d_rendered_image,
d_direct_lights.begin()
}, active_pixels.size(), scene.use_gpu);
}
struct bsdf_sampler {
DEVICE void operator()(int idx) {
auto pixel_id = active_pixels[idx];
const auto &isect = shading_isects[pixel_id];
const auto &shape = scene.shapes[isect.shape_id];
const auto &material = scene.materials[shape.material_id];
next_rays[pixel_id] = Ray{shading_points[pixel_id].position,
bsdf_sample(
material,
shading_points[pixel_id],
-incoming_rays[pixel_id].dir,
bsdf_samples[pixel_id],
min_roughness[pixel_id],
&next_min_roughness[pixel_id])};
}
const FlattenScene scene;
const int *active_pixels;
const Ray *incoming_rays;
const Intersection *shading_isects;
const SurfacePoint *shading_points;
const BSDFSample *bsdf_samples;
const Real *min_roughness;
Ray *next_rays;
Real *next_min_roughness;
};
void bsdf_sample(const Scene &scene,
const BufferView<int> &active_pixels,
const BufferView<Ray> &incoming_rays,
const BufferView<Intersection> &shading_isects,
const BufferView<SurfacePoint> &shading_points,
const BufferView<BSDFSample> &bsdf_samples,
const BufferView<Real> &min_roughness,
BufferView<Ray> next_rays,
BufferView<Real> next_min_roughness) {
parallel_for(
bsdf_sampler{get_flatten_scene(scene),
active_pixels.begin(),
incoming_rays.begin(),
shading_isects.begin(),
shading_points.begin(),
bsdf_samples.begin(),
min_roughness.begin(),
next_rays.begin(),
next_min_roughness.begin()},
active_pixels.size(), scene.use_gpu);
}
struct path_contribs_accumulator {
DEVICE void operator()(int idx) {
auto pixel_id = active_pixels[idx];
const auto &throughput = throughputs[pixel_id];
const auto &incoming_ray = incoming_rays[pixel_id];
const auto &shading_isect = shading_isects[pixel_id];
const auto &shading_point = shading_points[pixel_id];
const auto &light_isect = light_isects[pixel_id];
const auto &light_point = light_points[pixel_id];
const auto &bsdf_isect = bsdf_isects[pixel_id];
const auto &bsdf_point = bsdf_points[pixel_id];
const auto &min_rough = min_roughness[pixel_id];
auto &next_throughput = next_throughputs[pixel_id];
auto wi = -incoming_ray.dir;
auto p = shading_point.position;
const auto &shading_shape = scene.shapes[shading_isect.shape_id];
const auto &material = scene.materials[shading_shape.material_id];
// Next event estimation
auto nee_contrib = Vector3{0, 0, 0};
if (light_isect.valid()) {
const auto &light_shape = scene.shapes[light_isect.shape_id];
auto dir = light_point.position - p;
auto dist_sq = length_squared(dir);
auto wo = dir / sqrt(dist_sq);
if (light_shape.light_id >= 0 && dot(-wo, light_point.shading_frame.n) > 0) {
auto bsdf_val = bsdf(material, shading_point, wi, wo, min_rough);
auto geometry_term = fabs(dot(wo, light_point.geom_normal)) / dist_sq;
const auto &light = scene.lights[light_shape.light_id];
auto light_contrib = light.intensity;
auto light_pmf = scene.light_pmf[light_shape.light_id];
auto light_area = scene.light_areas[light_shape.light_id];
auto pdf_nee = light_pmf / light_area;
auto pdf_bsdf =
bsdf_pdf(material, shading_point, wi, wo, min_rough) * geometry_term;
auto mis_weight = square(pdf_nee) / (square(pdf_nee) + square(pdf_bsdf));
nee_contrib = (mis_weight * geometry_term / pdf_nee) * bsdf_val * light_contrib;
}
}
// BSDF importance sampling
auto scatter_contrib = Vector3{0, 0, 0};
auto scatter_bsdf = Vector3{0, 0, 0};
if (bsdf_isect.valid()) {
const auto &bsdf_shape = scene.shapes[bsdf_isect.shape_id];
auto dir = bsdf_point.position - p;
auto dist_sq = length_squared(dir);
auto wo = dir / sqrt(dist_sq);
auto pdf_bsdf = bsdf_pdf(material, shading_point, wi, wo, min_rough);
if (pdf_bsdf > 0) {
auto bsdf_val = bsdf(material, shading_point, wi, wo, min_rough);
if (bsdf_shape.light_id >= 0) {
const auto &light = scene.lights[bsdf_shape.light_id];
auto light_contrib = light.intensity;
auto light_pmf = scene.light_pmf[bsdf_shape.light_id];
auto light_area = scene.light_areas[bsdf_shape.light_id];
auto geometry_term = fabs(dot(wo, bsdf_point.geom_normal)) / dist_sq;
auto pdf_nee = (light_pmf / light_area) / geometry_term;
auto mis_weight = square(pdf_bsdf) / (square(pdf_nee) + square(pdf_bsdf));
scatter_contrib = (mis_weight / pdf_bsdf) * bsdf_val * light_contrib;
}
scatter_bsdf = bsdf_val / pdf_bsdf;
next_throughput = throughput * scatter_bsdf;
}
}
auto path_contrib = throughput * (nee_contrib + scatter_contrib);
if (rendered_image != nullptr) {
rendered_image[3 * pixel_id ] += weight * path_contrib[0];
rendered_image[3 * pixel_id + 1] += weight * path_contrib[1];
rendered_image[3 * pixel_id + 2] += weight * path_contrib[2];
}
if (edge_contribs != nullptr) {
edge_contribs[pixel_id] += sum(weight * path_contrib);
}
}
const FlattenScene scene;
const int *active_pixels;
const Vector3 *throughputs;
const Ray *incoming_rays;
const Intersection *shading_isects;
const SurfacePoint *shading_points;
const Intersection *light_isects;
const SurfacePoint *light_points;
const Intersection *bsdf_isects;
const SurfacePoint *bsdf_points;
const Real *min_roughness;
const Real weight;
Vector3 *next_throughputs;
float *rendered_image;
Real *edge_contribs;
};
struct d_path_contribs_accumulator {
DEVICE void operator()(int idx) {
auto pixel_id = active_pixels[idx];
const auto &throughput = throughputs[pixel_id];
const auto &incoming_ray = incoming_rays[pixel_id];
const auto &shading_isect = shading_isects[pixel_id];
const auto &shading_point = shading_points[pixel_id];
const auto &light_isect = light_isects[pixel_id];
const auto &min_rough = min_roughness[pixel_id];
auto d_light_v = d_light_vertices + 3 * idx;
auto d_bsdf_v = d_bsdf_vertices + 3 * idx;
auto &d_diffuse_tex = d_diffuse_texs[idx];
auto &d_specular_tex = d_specular_texs[idx];
auto &d_roughness_tex = d_roughness_texs[idx];
auto &d_nee_light = d_nee_lights[idx];
auto &d_bsdf_light = d_bsdf_lights[idx];
auto &d_throughput = d_throughputs[pixel_id];
auto &d_prev_wo = d_prev_wos[pixel_id];
auto &d_shading_point = d_shading_points[pixel_id];
auto wi = -incoming_ray.dir;
auto p = shading_point.position;
const auto &shading_shape = scene.shapes[shading_isect.shape_id];
const auto &material = scene.materials[shading_shape.material_id];
// rendered_image[3 * pixel_id ] += weight * path_contrib[0]
// rendered_image[3 * pixel_id + 1] += weight * path_contrib[1]
// rendered_image[3 * pixel_id + 2] += weight * path_contrib[2]
auto d_path_contrib = weight *
Vector3{d_rendered_image[3 * pixel_id ],
d_rendered_image[3 * pixel_id + 1],
d_rendered_image[3 * pixel_id + 2]};
// Initialize derivatives
d_light_v[0] = DVertex{};
d_light_v[1] = DVertex{};
d_light_v[2] = DVertex{};
d_bsdf_v[0] = DVertex{};
d_bsdf_v[1] = DVertex{};
d_bsdf_v[2] = DVertex{};
d_diffuse_tex = DTexture3{};
d_specular_tex = DTexture3{};
d_roughness_tex = DTexture1{};
d_nee_light = DLightInst{};
d_bsdf_light = DLightInst{};
d_throughput = Vector3{0, 0, 0};
d_prev_wo = Vector3{0, 0, 0};
d_shading_point = SurfacePoint::zero();
// Next event estimation
if (light_isect.valid()) {
const auto &light_shape = scene.shapes[light_isect.shape_id];
const auto &light_sample = light_samples[pixel_id];
const auto &light_point = light_points[pixel_id];
auto dir = light_point.position - p;
auto dist_sq = length_squared(dir);
auto wo = dir / sqrt(dist_sq);
if (light_shape.light_id >= 0 && dot(-wo, light_point.shading_frame.n) > 0) {
d_diffuse_tex.material_id = shading_shape.material_id;
d_specular_tex.material_id = shading_shape.material_id;
d_roughness_tex.material_id = shading_shape.material_id;
auto light_tri_index = get_indices(light_shape, light_isect.tri_id);
d_light_v[0].shape_id = light_isect.shape_id;
d_light_v[0].vertex_id = light_tri_index[0];
d_light_v[1].shape_id = light_isect.shape_id;
d_light_v[1].vertex_id = light_tri_index[1];
d_light_v[2].shape_id = light_isect.shape_id;
d_light_v[2].vertex_id = light_tri_index[2];
d_nee_light.light_id = light_shape.light_id;
auto bsdf_val = bsdf(material, shading_point, wi, wo, min_rough);
auto cos_light = dot(wo, light_point.geom_normal);
auto geometry_term = fabs(cos_light) / dist_sq;
const auto &light = scene.lights[light_shape.light_id];
auto light_contrib = light.intensity;
auto light_pmf = scene.light_pmf[light_shape.light_id];
auto light_area = scene.light_areas[light_shape.light_id];
auto pdf_nee = light_pmf / light_area;
auto pdf_bsdf =
bsdf_pdf(material, shading_point, wi, wo, min_rough) * geometry_term;
auto mis_weight = square(pdf_nee) / (square(pdf_nee) + square(pdf_bsdf));
auto nee_contrib = (mis_weight * geometry_term / pdf_nee) *
bsdf_val * light_contrib;
// path_contrib = throughput * (nee_contrib + scatter_contrib)
auto d_nee_contrib = d_path_contrib * throughput;
d_throughput += d_path_contrib * nee_contrib;
auto weight = mis_weight / pdf_nee;
// nee_contrib = (weight * geometry_term) *
// bsdf_val * light_contrib
// Ignore derivatives of MIS weight & PMF
auto d_weight = geometry_term *
sum(d_nee_contrib * bsdf_val * light_contrib);
// weight = mis_weight / pdf_nee
auto d_pdf_nee = -d_weight * weight / pdf_nee;
// nee_contrib = (weight * geometry_term) *
// bsdf_val * light_contrib
auto d_geometry_term = weight * sum(d_nee_contrib * bsdf_val * light_contrib);
auto d_bsdf_val = weight * d_nee_contrib * geometry_term * light_contrib;
auto d_light_contrib = weight * d_nee_contrib * geometry_term * bsdf_val;
// pdf_nee = light_pmf / light_area
// = light_pmf * tri_pmf / tri_area
auto d_area = -d_pdf_nee * pdf_nee / get_area(light_shape, light_isect.tri_id);
d_get_area(light_shape, light_isect.tri_id, d_area, d_light_v);
// light_contrib = light.intensity
d_nee_light.intensity += d_light_contrib;
// geometry_term = fabs(cos_light) / dist_sq
auto d_cos_light = cos_light > 0 ?
d_geometry_term / dist_sq : -d_geometry_term / dist_sq;
auto d_dist_sq = -d_geometry_term * geometry_term / dist_sq;
// cos_light = dot(wo, light_point.geom_normal)
auto d_wo = d_cos_light * light_point.geom_normal;
auto d_light_point = SurfacePoint::zero();
d_light_point.geom_normal = d_cos_light * wo;
// bsdf_val = bsdf(material, shading_point, wi, wo)
auto d_wi = Vector3{0, 0, 0};
d_bsdf(material, shading_point, wi, wo, min_rough, d_bsdf_val,
d_diffuse_tex, d_specular_tex, d_roughness_tex,
d_shading_point, d_wi, d_wo);
// wo = dir / sqrt(dist_sq)
auto d_dir = d_wo / sqrt(dist_sq);
// sqrt(dist_sq)
auto d_sqrt_dist_sq = -sum(d_wo * dir) / dist_sq;
d_dist_sq += (0.5f * d_sqrt_dist_sq / sqrt(dist_sq));
// dist_sq = length_squared(dir)
d_dir += d_length_squared(dir, d_dist_sq);
// dir = light_point.position - p
d_light_point.position += d_dir;
d_shading_point.position -= d_dir;
// wi = -incoming_ray.dir (= -prev_wo)
d_prev_wo -= d_wi;
// sample point on light
d_sample_shape(light_shape, light_isect.tri_id,
light_sample.uv, d_light_point, d_light_v);
}
}
// BSDF importance sampling
const auto &bsdf_isect = bsdf_isects[pixel_id];
if (bsdf_isect.valid()) {
const auto &bsdf_shape = scene.shapes[bsdf_isect.shape_id];
d_diffuse_tex.material_id = shading_shape.material_id;
d_specular_tex.material_id = shading_shape.material_id;
d_roughness_tex.material_id = shading_shape.material_id;
const auto &bsdf_sample = bsdf_samples[pixel_id];
const auto &bsdf_point = bsdf_points[pixel_id];
const auto &d_next_wo = d_next_wos[pixel_id];
const auto &d_next_point = d_next_points[pixel_id];
const auto &d_next_throughput = d_next_throughputs[pixel_id];
// Initialize bsdf vertex derivatives
auto bsdf_tri_index = get_indices(bsdf_shape, bsdf_isect.tri_id);
d_bsdf_v[0].shape_id = bsdf_isect.shape_id;
d_bsdf_v[0].vertex_id = bsdf_tri_index[0];
d_bsdf_v[1].shape_id = bsdf_isect.shape_id;
d_bsdf_v[1].vertex_id = bsdf_tri_index[1];
d_bsdf_v[2].shape_id = bsdf_isect.shape_id;
d_bsdf_v[2].vertex_id = bsdf_tri_index[2];
d_bsdf_light.light_id = bsdf_shape.light_id;
auto dir = bsdf_point.position - p;
auto dist_sq = length_squared(dir);
auto wo = dir / sqrt(dist_sq);
auto pdf_bsdf = bsdf_pdf(material, shading_point, wi, wo, min_rough);
if (pdf_bsdf > 0) {
auto bsdf_val = bsdf(material, shading_point, wi, wo, min_rough);
auto scatter_bsdf = bsdf_val / pdf_bsdf;
// next_throughput = throughput * scatter_bsdf
d_throughput += d_next_throughput * scatter_bsdf;
auto d_scatter_bsdf = d_next_throughput * throughput;
// scatter_bsdf = bsdf_val / pdf_bsdf
auto d_bsdf_val = d_scatter_bsdf;
auto d_pdf_bsdf = -sum(d_scatter_bsdf * scatter_bsdf) / pdf_bsdf;
if (bsdf_shape.light_id >= 0 && dot(-wo, bsdf_point.shading_frame.n) > 0) {
auto geometry_term = fabs(dot(wo, bsdf_point.geom_normal)) / dist_sq;
const auto &light = scene.lights[bsdf_shape.light_id];
auto light_contrib = light.intensity;
auto light_pmf = scene.light_pmf[bsdf_shape.light_id];
auto light_area = scene.light_areas[bsdf_shape.light_id];
auto pdf_nee = (light_pmf / light_area) / geometry_term;
auto mis_weight = square(pdf_bsdf) / (square(pdf_nee) + square(pdf_bsdf));
auto scatter_contrib = (mis_weight / pdf_bsdf) * bsdf_val * light_contrib;
// path_contrib = throughput * (nee_contrib + scatter_contrib)
auto d_scatter_contrib = d_path_contrib * throughput;
d_throughput += d_path_contrib * scatter_contrib;
auto weight = mis_weight / pdf_bsdf;
// scatter_contrib = weight * bsdf_val * light_contrib
auto d_weight = sum(d_scatter_contrib * bsdf_val * light_contrib);
// Ignore derivatives of MIS weight
// weight = mis_weight / pdf_bsdf
d_pdf_bsdf += -d_weight * weight / pdf_bsdf;
d_bsdf_val += weight * d_scatter_contrib * light_contrib;
auto d_light_contrib = weight * d_scatter_contrib * bsdf_val;
// light_contrib = light.intensity
d_bsdf_light.intensity += d_light_contrib;
}
auto d_wi = Vector3{0, 0, 0};
auto d_wo = d_next_wo;
d_bsdf_pdf(material, shading_point, wi, wo, min_rough, d_pdf_bsdf,
d_roughness_tex, d_shading_point, d_wi, d_wo);
// bsdf_val = bsdf(material, shading_point, wi, wo)
d_bsdf(material, shading_point, wi, wo, min_rough, d_bsdf_val,
d_diffuse_tex, d_specular_tex, d_roughness_tex,
d_shading_point, d_wi, d_wo);
// wo = dir / sqrt(dist_sq)
auto d_dir = d_wo / sqrt(dist_sq);
auto d_sqrt_dist_sq = -sum(d_wo * dir) / dist_sq;
auto d_dist_sq = 0.5f * d_sqrt_dist_sq / sqrt(dist_sq);
// dist_sq = length_squared(dir)
d_dir += d_length_squared(dir, d_dist_sq);
auto d_bsdf_point = d_next_point;
// dir = bsdf_point.position - p
d_bsdf_point.position += d_dir;
// d_shading_point.position -= d_dir; (see below)
// bsdf intersection
DRay d_ray;
d_intersect_shape(bsdf_shape, bsdf_isect.tri_id,
Ray{shading_point.position, wo}, d_bsdf_point, d_ray, d_bsdf_v);
// XXX HACK: diffuse interreflection causes a lot of noise
// on position derivatives but has small impact on the final derivatives,
// so we ignore them here.
// A properer way is to come up with an importance sampling distribution,
// or use a lot more samples
if (min_rough > 0.01f) {
d_shading_point.position -= d_dir;
d_shading_point.position += d_ray.org;
}
d_wo += d_ray.dir;
// sample bsdf direction
d_bsdf_sample(material,
shading_point,
wi,
bsdf_sample,
min_rough,
d_wo,
d_roughness_tex,
d_shading_point,
d_wi);
// wi = -incoming_ray.dir (= -prev_wo)
d_prev_wo -= d_wi;
}
}
}
const FlattenScene scene;
const int *active_pixels;
const Vector3 *throughputs;
const Ray *incoming_rays;
const LightSample *light_samples;
const BSDFSample *bsdf_samples;
const Intersection *shading_isects;
const SurfacePoint *shading_points;
const Intersection *light_isects;
const SurfacePoint *light_points;
const Intersection *bsdf_isects;
const SurfacePoint *bsdf_points;
const Real *min_roughness;
const Real weight;
const float *d_rendered_image;
const Vector3 *d_next_throughputs;
const Vector3 *d_next_wos;
const SurfacePoint *d_next_points;
DVertex *d_light_vertices;
DVertex *d_bsdf_vertices;
DTexture3 *d_diffuse_texs;
DTexture3 *d_specular_texs;
DTexture1 *d_roughness_texs;
DLightInst *d_nee_lights;
DLightInst *d_bsdf_lights;
Vector3 *d_throughputs;
Vector3 *d_prev_wos;
SurfacePoint *d_shading_points;
};
void accumulate_path_contribs(const Scene &scene,
const BufferView<int> &active_pixels,
const BufferView<Vector3> &throughputs,
const BufferView<Ray> &incoming_rays,
const BufferView<Intersection> &shading_isects,
const BufferView<SurfacePoint> &shading_points,
const BufferView<Intersection> &light_isects,
const BufferView<SurfacePoint> &light_points,
const BufferView<Intersection> &bsdf_isects,
const BufferView<SurfacePoint> &bsdf_points,
const BufferView<Real> &min_roughness,
Real weight,
BufferView<Vector3> next_throughputs,
float *rendered_image,
BufferView<Real> edge_contribs) {
parallel_for(path_contribs_accumulator{
get_flatten_scene(scene),
active_pixels.begin(),
throughputs.begin(),
incoming_rays.begin(),
shading_isects.begin(),
shading_points.begin(),
light_isects.begin(),
light_points.begin(),
bsdf_isects.begin(),
bsdf_points.begin(),
min_roughness.begin(),
weight,
next_throughputs.begin(),
rendered_image,
edge_contribs.begin()}, active_pixels.size(), scene.use_gpu);
}
void d_accumulate_path_contribs(const Scene &scene,
const BufferView<int> &active_pixels,
const BufferView<Vector3> &throughputs,
const BufferView<Ray> &incoming_rays,
const BufferView<LightSample> &light_samples,
const BufferView<BSDFSample> &bsdf_samples,
const BufferView<Intersection> &shading_isects,
const BufferView<SurfacePoint> &shading_points,
const BufferView<Intersection> &light_isects,
const BufferView<SurfacePoint> &light_points,
const BufferView<Intersection> &bsdf_isects,
const BufferView<SurfacePoint> &bsdf_points,
const BufferView<Real> &min_roughness,
const Real weight,
const float *d_rendered_image,
const BufferView<Vector3> &d_next_throughputs,
const BufferView<Vector3> &d_next_wos,
const BufferView<SurfacePoint> &d_next_points,
BufferView<DVertex> d_light_vertices,
BufferView<DVertex> d_bsdf_vertices,
BufferView<DTexture3> d_diffuse_texs,
BufferView<DTexture3> d_specular_texs,
BufferView<DTexture1> d_roughness_texs,
BufferView<DLightInst> d_nee_lights,
BufferView<DLightInst> d_bsdf_lights,
BufferView<Vector3> d_throughputs,
BufferView<Vector3> d_prev_wos,
BufferView<SurfacePoint> d_shading_points) {
parallel_for(d_path_contribs_accumulator{
get_flatten_scene(scene),
active_pixels.begin(),
throughputs.begin(),
incoming_rays.begin(),
light_samples.begin(),
bsdf_samples.begin(),
shading_isects.begin(),
shading_points.begin(),
light_isects.begin(),
light_points.begin(),
bsdf_isects.begin(),
bsdf_points.begin(),
min_roughness.begin(),
weight,
d_rendered_image,
d_next_throughputs.begin(),
d_next_wos.begin(),
d_next_points.begin(),
d_light_vertices.begin(),
d_bsdf_vertices.begin(),
d_diffuse_texs.begin(),
d_specular_texs.begin(),
d_roughness_texs.begin(),
d_nee_lights.begin(),
d_bsdf_lights.begin(),
d_throughputs.begin(),
d_prev_wos.begin(),
d_shading_points.begin()},
active_pixels.size(), scene.use_gpu);
}
struct PathBuffer {
PathBuffer(int max_bounces, int num_pixels, bool use_gpu) :
num_pixels(num_pixels) {
assert(max_bounces >= 1);
// For forward path tracing, we need to allocate memory for
// all bounces
// For edge sampling, we need to allocate memory for
// 2 * num_pixels paths (and 4 * num_pixels for those
// shared between two path vertices).
camera_samples = Buffer<CameraSample>(use_gpu, num_pixels);
light_samples = Buffer<LightSample>(use_gpu, max_bounces * num_pixels);
edge_light_samples = Buffer<LightSample>(use_gpu, 2 * num_pixels);
bsdf_samples = Buffer<BSDFSample>(use_gpu, max_bounces * num_pixels);
edge_bsdf_samples = Buffer<BSDFSample>(use_gpu, 2 * num_pixels);
rays = Buffer<Ray>(use_gpu, (max_bounces + 1) * num_pixels);
edge_rays = Buffer<Ray>(use_gpu, 4 * num_pixels);
active_pixels = Buffer<int>(use_gpu, (max_bounces + 1) * num_pixels);
edge_active_pixels = Buffer<int>(use_gpu, 4 * num_pixels);
shading_isects = Buffer<Intersection>(use_gpu, (max_bounces + 1) * num_pixels);
edge_shading_isects = Buffer<Intersection>(use_gpu, 4 * num_pixels);
shading_points = Buffer<SurfacePoint>(use_gpu, (max_bounces + 1) * num_pixels);
edge_shading_points = Buffer<SurfacePoint>(use_gpu, 4 * num_pixels);
light_isects = Buffer<Intersection>(use_gpu, max_bounces * num_pixels);
edge_light_isects = Buffer<Intersection>(use_gpu, 2 * num_pixels);
light_points = Buffer<SurfacePoint>(use_gpu, max_bounces * num_pixels);
edge_light_points = Buffer<SurfacePoint>(use_gpu, 2 * num_pixels);
throughputs = Buffer<Vector3>(use_gpu, (max_bounces + 1) * num_pixels);
edge_throughputs = Buffer<Vector3>(use_gpu, 4 * num_pixels);
min_roughness = Buffer<Real>(use_gpu, (max_bounces + 1) * num_pixels);
edge_min_roughness = Buffer<Real>(use_gpu, 4 * num_pixels);
// Derivatives buffers
d_next_throughputs = Buffer<Vector3>(use_gpu, num_pixels);
d_next_wos = Buffer<Vector3>(use_gpu, num_pixels);
d_next_points = Buffer<SurfacePoint>(use_gpu, num_pixels);
d_throughputs = Buffer<Vector3>(use_gpu, num_pixels);
d_wos = Buffer<Vector3>(use_gpu, num_pixels);
d_points = Buffer<SurfacePoint>(use_gpu, num_pixels);
d_general_vertices = Buffer<DVertex>(use_gpu, 3 * num_pixels);
d_light_vertices = Buffer<DVertex>(use_gpu, 3 * num_pixels);
d_bsdf_vertices = Buffer<DVertex>(use_gpu, 3 * num_pixels);
d_diffuse_texs = Buffer<DTexture3>(use_gpu, num_pixels);
d_specular_texs = Buffer<DTexture3>(use_gpu, num_pixels);
d_roughness_texs = Buffer<DTexture1>(use_gpu, num_pixels);
d_direct_lights = Buffer<DLightInst>(use_gpu, num_pixels);
d_nee_lights = Buffer<DLightInst>(use_gpu, num_pixels);
d_bsdf_lights = Buffer<DLightInst>(use_gpu, num_pixels);
d_vertex_reduce_buffer = Buffer<DVertex>(use_gpu, 3 * num_pixels);
d_tex3_reduce_buffer = Buffer<DTexture3>(use_gpu, num_pixels);
d_tex1_reduce_buffer = Buffer<DTexture1>(use_gpu, num_pixels);
d_lgt_reduce_buffer = Buffer<DLightInst>(use_gpu, num_pixels);
d_cameras = Buffer<DCameraInst>(use_gpu, num_pixels);
primary_edge_samples = Buffer<PrimaryEdgeSample>(use_gpu, num_pixels);
secondary_edge_samples = Buffer<SecondaryEdgeSample>(use_gpu, num_pixels);
primary_edge_records = Buffer<PrimaryEdgeRecord>(use_gpu, num_pixels);
secondary_edge_records = Buffer<SecondaryEdgeRecord>(use_gpu, num_pixels);
edge_contribs = Buffer<Real>(use_gpu, 2 * num_pixels);
edge_surface_points = Buffer<Vector3>(use_gpu, 2 * num_pixels);
tmp_light_samples = Buffer<LightSample>(use_gpu, num_pixels);
tmp_bsdf_samples = Buffer<BSDFSample>(use_gpu, num_pixels);
}
int num_pixels;
Buffer<CameraSample> camera_samples;
Buffer<LightSample> light_samples, edge_light_samples;
Buffer<BSDFSample> bsdf_samples, edge_bsdf_samples;
Buffer<Ray> rays, edge_rays;
Buffer<int> active_pixels, edge_active_pixels;
Buffer<Intersection> shading_isects, edge_shading_isects;
Buffer<SurfacePoint> shading_points, edge_shading_points;
Buffer<Intersection> light_isects, edge_light_isects;
Buffer<SurfacePoint> light_points, edge_light_points;
Buffer<Vector3> throughputs, edge_throughputs;
Buffer<Real> min_roughness, edge_min_roughness;
// Derivatives related
Buffer<Vector3> d_next_throughputs;
Buffer<Vector3> d_next_wos;
Buffer<SurfacePoint> d_next_points;
Buffer<Vector3> d_throughputs;
Buffer<Vector3> d_wos;
Buffer<SurfacePoint> d_points;
Buffer<DVertex> d_general_vertices;
Buffer<DVertex> d_light_vertices;
Buffer<DVertex> d_bsdf_vertices;
Buffer<DTexture3> d_diffuse_texs;
Buffer<DTexture3> d_specular_texs;
Buffer<DTexture1> d_roughness_texs;
Buffer<DLightInst> d_direct_lights;
Buffer<DLightInst> d_nee_lights;
Buffer<DLightInst> d_bsdf_lights;
Buffer<DVertex> d_vertex_reduce_buffer;
Buffer<DTexture3> d_tex3_reduce_buffer;
Buffer<DTexture1> d_tex1_reduce_buffer;
Buffer<DLightInst> d_lgt_reduce_buffer;
Buffer<DCameraInst> d_cameras;
// Edge sampling related
Buffer<PrimaryEdgeSample> primary_edge_samples;
Buffer<SecondaryEdgeSample> secondary_edge_samples;
Buffer<PrimaryEdgeRecord> primary_edge_records;
Buffer<SecondaryEdgeRecord> secondary_edge_records;
Buffer<Real> edge_contribs;
Buffer<Vector3> edge_surface_points;
// For sharing RNG between pixels
Buffer<LightSample> tmp_light_samples;
Buffer<BSDFSample> tmp_bsdf_samples;
};
void accumulate_vertex(BufferView<DVertex> d_vertices,
BufferView<DVertex> reduce_buffer,
BufferView<DShape> d_shapes,
bool use_gpu) {
if (d_vertices.size() == 0) {
return;
}
// Reduce into unique sequence
auto beg = d_vertices.begin();
auto end = d_vertices.end();
end = DISPATCH(use_gpu, thrust::remove, beg, end, DVertex{-1, -1});
DISPATCH(use_gpu, thrust::sort, beg, end);
auto buffer_beg = reduce_buffer.begin();
auto new_end = DISPATCH(use_gpu, thrust::reduce_by_key,
beg, end, // input keys
beg, // input values
buffer_beg, // output keys
buffer_beg).first; // output values
DISPATCH(use_gpu, thrust::copy, buffer_beg, new_end, beg);
d_vertices.count = new_end - buffer_beg;
// Accumulate to output derivatives
accumulate_vertex(d_vertices, d_shapes, use_gpu);
}
void accumulate_diffuse(const Scene &scene,
BufferView<DTexture3> &d_diffuse,
BufferView<DTexture3> reduce_buffer,
BufferView<DMaterial> d_materials) {
if (d_diffuse.size() == 0) {
return;
}
// Reduce into unique sequence
auto beg = d_diffuse.begin();
auto end = d_diffuse.end();
end = DISPATCH(scene.use_gpu, thrust::remove, beg, end, DTexture3{-1, -1, -1});
DISPATCH(scene.use_gpu, thrust::sort, beg, end);
auto buffer_beg = reduce_buffer.begin();
auto new_end = DISPATCH(scene.use_gpu, thrust::reduce_by_key,
beg, end, // input keys
beg, // input values
buffer_beg, // output keys
buffer_beg).first; // output values
DISPATCH(scene.use_gpu, thrust::copy, buffer_beg, new_end, beg);
d_diffuse.count = new_end - buffer_beg;
// Accumulate to output derivatives
accumulate_diffuse(scene, d_diffuse, d_materials);
}
void accumulate_specular(const Scene &scene,
BufferView<DTexture3> &d_specular,
BufferView<DTexture3> reduce_buffer,
BufferView<DMaterial> d_materials) {
if (d_specular.size() == 0) {
return;
}
// Reduce into unique sequence
auto beg = d_specular.begin();
auto end = d_specular.end();
end = DISPATCH(scene.use_gpu, thrust::remove, beg, end, DTexture3{-1, -1, -1});
DISPATCH(scene.use_gpu, thrust::sort, beg, end);
auto buffer_beg = reduce_buffer.begin();
auto new_end = DISPATCH(scene.use_gpu, thrust::reduce_by_key,
beg, end, // input keys
beg, // input values
buffer_beg, // output keys
buffer_beg).first; // output values
DISPATCH(scene.use_gpu, thrust::copy, buffer_beg, new_end, beg);
d_specular.count = new_end - buffer_beg;
// Accumulate to output derivatives
accumulate_specular(scene, d_specular, d_materials);
}
void accumulate_roughness(const Scene &scene,
BufferView<DTexture1> &d_roughness,
BufferView<DTexture1> reduce_buffer,
BufferView<DMaterial> d_materials) {
if (d_roughness.size() == 0) {
return;
}
// Reduce into unique sequence
auto beg = d_roughness.begin();
auto end = d_roughness.end();
end = DISPATCH(scene.use_gpu, thrust::remove, beg, end, DTexture1{-1, -1, -1});
DISPATCH(scene.use_gpu, thrust::sort, beg, end);
auto buffer_beg = reduce_buffer.begin();
auto new_end = DISPATCH(scene.use_gpu, thrust::reduce_by_key,
beg, end, // input keys
beg, // input values
buffer_beg, // output keys
buffer_beg).first; // output values
DISPATCH(scene.use_gpu, thrust::copy, buffer_beg, new_end, beg);
d_roughness.count = new_end - buffer_beg;
// Accumulate to output derivatives
accumulate_roughness(scene, d_roughness, d_materials);
}
void accumulate_light(BufferView<DLightInst> &d_light_insts,
BufferView<DLightInst> reduce_buffer,
BufferView<DLight> d_lights,
bool use_gpu) {
if (d_light_insts.size() == 0) {
return;
}
// Reduce into unique sequence
auto beg = d_light_insts.begin();
auto end = d_light_insts.end();
end = DISPATCH(use_gpu, thrust::remove, beg, end, DLightInst{-1});
DISPATCH(use_gpu, thrust::sort, beg, end);
auto buffer_beg = reduce_buffer.begin();
auto new_end = DISPATCH(use_gpu, thrust::reduce_by_key,
beg, end, // input keys
beg, // input values
buffer_beg, // output keys
buffer_beg).first; // output values
DISPATCH(use_gpu, thrust::copy, buffer_beg, new_end, beg);
d_light_insts.count = new_end - buffer_beg;
// Accumulate to output derivatives
accumulate_light(d_light_insts, d_lights, use_gpu);
}
// 1 2 3 4 5 -> 1 1 2 2 3 3 4 4 5 5
template <typename T>
struct copy_interleave {
DEVICE void operator()(int idx) {
to[2 * idx + 0] = from[idx];
to[2 * idx + 1] = from[idx];
}
const T *from;
T *to;
};
// Extract the position of a surface point
struct get_position {
DEVICE void operator()(int idx) {
p[active_pixels[idx]] = sp[active_pixels[idx]].position;
}