Welcome to mirror list, hosted at ThFree Co, Russian Federation.

light.cpp « scene « cycles « intern - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 49a8e19c95a25a6d616536f0e558836e51537bd2 (plain)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
/* SPDX-License-Identifier: Apache-2.0
 * Copyright 2011-2022 Blender Foundation */

#include "device/device.h"

#include "scene/background.h"
#include "scene/film.h"
#include "scene/integrator.h"
#include "scene/light.h"
#include "scene/mesh.h"
#include "scene/object.h"
#include "scene/scene.h"
#include "scene/shader.h"
#include "scene/shader_graph.h"
#include "scene/shader_nodes.h"
#include "scene/stats.h"

#include "integrator/shader_eval.h"

#include "util/foreach.h"
#include "util/hash.h"
#include "util/log.h"
#include "util/path.h"
#include "util/progress.h"
#include "util/task.h"

CCL_NAMESPACE_BEGIN

static void shade_background_pixels(Device *device,
                                    DeviceScene *dscene,
                                    int width,
                                    int height,
                                    vector<float3> &pixels,
                                    Progress &progress)
{
  /* Needs to be up to data for attribute access. */
  device->const_copy_to("data", &dscene->data, sizeof(dscene->data));

  const int size = width * height;
  const int num_channels = 3;
  pixels.resize(size);

  /* Evaluate shader on device. */
  ShaderEval shader_eval(device, progress);
  shader_eval.eval(
      SHADER_EVAL_BACKGROUND,
      size,
      num_channels,
      [&](device_vector<KernelShaderEvalInput> &d_input) {
        /* Fill coordinates for shading. */
        KernelShaderEvalInput *d_input_data = d_input.data();

        for (int y = 0; y < height; y++) {
          for (int x = 0; x < width; x++) {
            float u = (x + 0.5f) / width;
            float v = (y + 0.5f) / height;

            KernelShaderEvalInput in;
            in.object = OBJECT_NONE;
            in.prim = PRIM_NONE;
            in.u = u;
            in.v = v;
            d_input_data[x + y * width] = in;
          }
        }

        return size;
      },
      [&](device_vector<float> &d_output) {
        /* Copy output to pixel buffer. */
        float *d_output_data = d_output.data();

        for (int y = 0; y < height; y++) {
          for (int x = 0; x < width; x++) {
            pixels[y * width + x].x = d_output_data[(y * width + x) * num_channels + 0];
            pixels[y * width + x].y = d_output_data[(y * width + x) * num_channels + 1];
            pixels[y * width + x].z = d_output_data[(y * width + x) * num_channels + 2];
          }
        }
      });
}

/* Light */

NODE_DEFINE(Light)
{
  NodeType *type = NodeType::add("light", create);

  static NodeEnum type_enum;
  type_enum.insert("point", LIGHT_POINT);
  type_enum.insert("distant", LIGHT_DISTANT);
  type_enum.insert("background", LIGHT_BACKGROUND);
  type_enum.insert("area", LIGHT_AREA);
  type_enum.insert("spot", LIGHT_SPOT);
  SOCKET_ENUM(light_type, "Type", type_enum, LIGHT_POINT);

  SOCKET_COLOR(strength, "Strength", one_float3());

  SOCKET_POINT(co, "Co", zero_float3());

  SOCKET_VECTOR(dir, "Dir", zero_float3());
  SOCKET_FLOAT(size, "Size", 0.0f);
  SOCKET_FLOAT(angle, "Angle", 0.0f);

  SOCKET_VECTOR(axisu, "Axis U", zero_float3());
  SOCKET_FLOAT(sizeu, "Size U", 1.0f);
  SOCKET_VECTOR(axisv, "Axis V", zero_float3());
  SOCKET_FLOAT(sizev, "Size V", 1.0f);
  SOCKET_BOOLEAN(round, "Round", false);
  SOCKET_FLOAT(spread, "Spread", M_PI_F);

  SOCKET_INT(map_resolution, "Map Resolution", 0);
  SOCKET_FLOAT(average_radiance, "Average Radiance", 0.0f);

  SOCKET_FLOAT(spot_angle, "Spot Angle", M_PI_4_F);
  SOCKET_FLOAT(spot_smooth, "Spot Smooth", 0.0f);

  SOCKET_TRANSFORM(tfm, "Transform", transform_identity());

  SOCKET_BOOLEAN(cast_shadow, "Cast Shadow", true);
  SOCKET_BOOLEAN(use_mis, "Use Mis", false);
  SOCKET_BOOLEAN(use_camera, "Use Camera", true);
  SOCKET_BOOLEAN(use_diffuse, "Use Diffuse", true);
  SOCKET_BOOLEAN(use_glossy, "Use Glossy", true);
  SOCKET_BOOLEAN(use_transmission, "Use Transmission", true);
  SOCKET_BOOLEAN(use_scatter, "Use Scatter", true);
  SOCKET_BOOLEAN(use_caustics, "Shadow Caustics", false);

  SOCKET_INT(max_bounces, "Max Bounces", 1024);
  SOCKET_UINT(random_id, "Random ID", 0);

  SOCKET_BOOLEAN(is_shadow_catcher, "Shadow Catcher", true);
  SOCKET_BOOLEAN(is_portal, "Is Portal", false);
  SOCKET_BOOLEAN(is_enabled, "Is Enabled", true);

  SOCKET_NODE(shader, "Shader", Shader::get_node_type());

  SOCKET_STRING(lightgroup, "Light Group", ustring());

  return type;
}

Light::Light() : Node(get_node_type())
{
  dereference_all_used_nodes();
}

void Light::tag_update(Scene *scene)
{
  if (is_modified()) {
    scene->light_manager->tag_update(scene, LightManager::LIGHT_MODIFIED);
  }
}

bool Light::has_contribution(Scene *scene)
{
  if (strength == zero_float3()) {
    return false;
  }
  if (is_portal) {
    return false;
  }
  if (light_type == LIGHT_BACKGROUND) {
    return true;
  }
  return (shader) ? shader->has_surface_emission : scene->default_light->has_surface_emission;
}

/* Light Manager */

LightManager::LightManager()
{
  update_flags = UPDATE_ALL;
  need_update_background = true;
  last_background_enabled = false;
  last_background_resolution = 0;
}

LightManager::~LightManager()
{
  foreach (IESSlot *slot, ies_slots) {
    delete slot;
  }
}

bool LightManager::has_background_light(Scene *scene)
{
  foreach (Light *light, scene->lights) {
    if (light->light_type == LIGHT_BACKGROUND && light->is_enabled) {
      return true;
    }
  }
  return false;
}

void LightManager::test_enabled_lights(Scene *scene)
{
  /* Make all lights enabled by default, and perform some preliminary checks
   * needed for finer-tuning of settings (for example, check whether we've
   * got portals or not).
   */
  bool has_portal = false, has_background = false;
  foreach (Light *light, scene->lights) {
    light->is_enabled = light->has_contribution(scene);
    has_portal |= light->is_portal;
    has_background |= light->light_type == LIGHT_BACKGROUND;
  }

  bool background_enabled = false;
  int background_resolution = 0;

  if (has_background) {
    /* Ignore background light if:
     * - If unsupported on a device
     * - If we don't need it (no HDRs etc.)
     */
    Shader *shader = scene->background->get_shader(scene);
    const bool disable_mis = !(has_portal || shader->has_surface_spatial_varying);
    if (disable_mis) {
      VLOG_INFO << "Background MIS has been disabled.\n";
    }
    foreach (Light *light, scene->lights) {
      if (light->light_type == LIGHT_BACKGROUND) {
        light->is_enabled = !disable_mis;
        background_enabled = !disable_mis;
        background_resolution = light->map_resolution;
      }
    }
  }

  if (last_background_enabled != background_enabled ||
      last_background_resolution != background_resolution) {
    last_background_enabled = background_enabled;
    last_background_resolution = background_resolution;
    need_update_background = true;
  }
}

bool LightManager::object_usable_as_light(Object *object)
{
  Geometry *geom = object->get_geometry();
  if (geom->geometry_type != Geometry::MESH && geom->geometry_type != Geometry::VOLUME) {
    return false;
  }
  /* Skip objects with NaNs */
  if (!object->bounds.valid()) {
    return false;
  }
  /* Skip if we are not visible for BSDFs. */
  if (!(object->get_visibility() & (PATH_RAY_DIFFUSE | PATH_RAY_GLOSSY | PATH_RAY_TRANSMIT))) {
    return false;
  }
  /* Skip if we have no emission shaders. */
  /* TODO(sergey): Ideally we want to avoid such duplicated loop, since it'll
   * iterate all geometry shaders twice (when counting and when calculating
   * triangle area.
   */
  foreach (Node *node, geom->get_used_shaders()) {
    Shader *shader = static_cast<Shader *>(node);
    if (shader->get_use_mis() && shader->has_surface_emission) {
      return true;
    }
  }
  return false;
}

void LightManager::device_update_distribution(Device *device,
                                              DeviceScene *dscene,
                                              Scene *scene,
                                              Progress &progress)
{
  progress.set_status("Updating Lights", "Computing distribution");

  /* count */
  size_t num_lights = 0;
  size_t num_portals = 0;
  size_t num_background_lights = 0;
  size_t num_distant_lights = 0;
  size_t num_triangles = 0;
  size_t total_triangles = 0;

  /* We want to add both lights and emissive triangles to this vector for light tree construction.
   */
  bool light_tree_enabled = scene->integrator->get_use_light_tree();
  vector<LightTreePrimitive> light_prims;
  vector<LightTreePrimitive> distant_lights;
  vector<uint> object_lookup_offsets(scene->objects.size());

  /* When we keep track of the light index, only contributing lights will be added to the device.
   * Therefore, we want to keep track of the light's index on the device.
   * However, we also need the light's index in the scene when we're constructing the tree. */
  int device_light_index = 0;
  int scene_light_index = 0;
  foreach (Light *light, scene->lights) {
    if (light->is_enabled) {
      if (light_tree_enabled) {
        LightTreePrimitive light_prim;
        light_prim.prim_id = ~device_light_index; /* -prim_id - 1 is a light source index. */
        light_prim.lamp_id = scene_light_index;

        /* Distant lights get added to a separate vector. */
        if (light->light_type == LIGHT_DISTANT || light->light_type == LIGHT_BACKGROUND) {
          distant_lights.push_back(light_prim);
          num_distant_lights++;
        }
        else {
          light_prims.push_back(light_prim);
        }

        device_light_index++;
      }

      num_lights++;
    }
    if (light->is_portal) {
      num_portals++;
    }

    scene_light_index++;
  }

  /* Similarly, we also want to keep track of the index of triangles that are emissive. */
  int object_id = 0;
  foreach (Object *object, scene->objects) {
    if (progress.get_cancel())
      return;

    if (!object_usable_as_light(object)) {
      object_id++;
      continue;
    }

    if (light_tree_enabled) {
      object_lookup_offsets[object_id] = total_triangles;
    }

    /* Count emissive triangles. */
    Mesh *mesh = static_cast<Mesh *>(object->get_geometry());
    size_t mesh_num_triangles = mesh->num_triangles();

    for (size_t i = 0; i < mesh_num_triangles; i++) {
      int shader_index = mesh->get_shader()[i];
      Shader *shader = (shader_index < mesh->get_used_shaders().size()) ?
                           static_cast<Shader *>(mesh->get_used_shaders()[shader_index]) :
                           scene->default_surface;

      if (shader->get_use_mis() && shader->has_surface_emission) {
        /* to-do: for the light tree implementation, we eventually want to include emissive
         * triangles. Right now, point lights are the main concern. */
        if (light_tree_enabled) {
          LightTreePrimitive light_prim;
          light_prim.prim_id = i;
          light_prim.object_id = object_id;
          light_prims.push_back(light_prim);
        }

        num_triangles++;
      }
    }

    total_triangles += mesh_num_triangles;
    object_id++;
  }

  size_t num_distribution = num_triangles + num_lights;
  VLOG_INFO << "Total " << num_distribution << " of light distribution primitives.";

  if (light_tree_enabled && num_distribution > 0) {
    /* For now, we'll start with a smaller number of max lights in a node.
     * More benchmarking is needed to determine what number works best. */
    LightTree light_tree(light_prims, scene, 8);
    light_prims = light_tree.get_prims();

    /* We want to create separate arrays corresponding to triangles and lights,
     * which will be used to index back into the light tree for PDF calculations. */
    uint *light_array = dscene->light_to_tree.alloc(num_lights);
    uint *object_offsets = dscene->object_lookup_offset.alloc(object_lookup_offsets.size());
    uint *triangle_array = dscene->triangle_to_tree.alloc(total_triangles);

    for (int i = 0; i < object_lookup_offsets.size(); i++) {
      object_offsets[i] = object_lookup_offsets[i];
    }

    /* First initialize the light tree's nodes. */
    const vector<PackedLightTreeNode> &linearized_bvh = light_tree.get_nodes();
    KernelLightTreeNode *light_tree_nodes = dscene->light_tree_nodes.alloc(linearized_bvh.size());
    KernelLightTreeEmitter *light_tree_emitters = dscene->light_tree_emitters.alloc(
        light_prims.size());
    for (int index = 0; index < linearized_bvh.size(); index++) {
      const PackedLightTreeNode &node = linearized_bvh[index];

      light_tree_nodes[index].energy = node.energy;

      for (int i = 0; i < 3; i++) {
        light_tree_nodes[index].bounding_box_min[i] = node.bbox.min[i];
        light_tree_nodes[index].bounding_box_max[i] = node.bbox.max[i];
        light_tree_nodes[index].bounding_cone_axis[i] = node.bcone.axis[i];
      }
      light_tree_nodes[index].theta_o = node.bcone.theta_o;
      light_tree_nodes[index].theta_e = node.bcone.theta_e;

      light_tree_nodes[index].bit_trail = node.bit_trail;

      /* Here we need to make a distinction between interior and leaf nodes. */
      if (node.is_leaf_node) {
        light_tree_nodes[index].num_prims = node.num_lights;
        light_tree_nodes[index].child_index = -node.first_prim_index;

        for (int i = 0; i < node.num_lights; i++) {
          int emitter_index = i + node.first_prim_index;
          LightTreePrimitive &prim = light_prims[emitter_index];
          BoundBox bbox = prim.calculate_bbox(scene);
          OrientationBounds bcone = prim.calculate_bcone(scene);
          float energy = prim.calculate_energy(scene);

          light_tree_emitters[emitter_index].energy = energy;

          for (int i = 0; i < 3; i++) {
            light_tree_emitters[emitter_index].bounding_box_min[i] = bbox.min[i];
            light_tree_emitters[emitter_index].bounding_box_max[i] = bbox.max[i];
            light_tree_emitters[emitter_index].bounding_cone_axis[i] = bcone.axis[i];
          }
          light_tree_emitters[emitter_index].theta_o = bcone.theta_o;
          light_tree_emitters[emitter_index].theta_e = bcone.theta_e;

          if (prim.prim_id >= 0) {
            light_tree_emitters[emitter_index].mesh_light.object_id = prim.object_id;

            int shader_flag = 0;
            Object *object = scene->objects[prim.object_id];
            Mesh *mesh = static_cast<Mesh *>(object->get_geometry());
            if (!(object->get_visibility() & PATH_RAY_CAMERA)) {
              shader_flag |= SHADER_EXCLUDE_CAMERA;
            }
            if (!(object->get_visibility() & PATH_RAY_DIFFUSE)) {
              shader_flag |= SHADER_EXCLUDE_DIFFUSE;
            }
            if (!(object->get_visibility() & PATH_RAY_GLOSSY)) {
              shader_flag |= SHADER_EXCLUDE_GLOSSY;
            }
            if (!(object->get_visibility() & PATH_RAY_TRANSMIT)) {
              shader_flag |= SHADER_EXCLUDE_TRANSMIT;
            }
            if (!(object->get_visibility() & PATH_RAY_VOLUME_SCATTER)) {
              shader_flag |= SHADER_EXCLUDE_SCATTER;
            }
            if (!(object->get_is_shadow_catcher())) {
              shader_flag |= SHADER_EXCLUDE_SHADOW_CATCHER;
            }

            light_tree_emitters[emitter_index].prim_id = prim.prim_id + mesh->prim_offset;
            light_tree_emitters[emitter_index].mesh_light.shader_flag = shader_flag;
            triangle_array[prim.prim_id + object_lookup_offsets[prim.object_id]] = emitter_index;
          }
          else {
            Light *lamp = scene->lights[prim.lamp_id];
            light_tree_emitters[emitter_index].prim_id = prim.prim_id;
            light_tree_emitters[emitter_index].lamp.size = lamp->size;
            light_tree_emitters[emitter_index].lamp.pad = 1.0f;
            light_array[~prim.prim_id] = emitter_index;
          }

          light_tree_emitters[emitter_index].parent_index = index;
        }
      }
      else {
        light_tree_nodes[index].energy_variance = node.energy_variance;
        light_tree_nodes[index].child_index = node.second_child_index;
      }
    }

    /* We also add distant lights to a separate group. */
    KernelLightTreeDistantEmitter *light_tree_distant_group =
        dscene->light_tree_distant_group.alloc(num_distant_lights + 1);

    /* We use OrientationBounds here to */
    OrientationBounds distant_light_bounds = OrientationBounds::empty;
    light_tree_distant_group[num_distant_lights].energy = 0.f;
    for (int index = 0; index < num_distant_lights; index++) {
      LightTreePrimitive prim = distant_lights[index];
      Light *light = scene->lights[prim.lamp_id];
      OrientationBounds light_bounds;

      /* Lights in this group are either a background or distant light. */
      light_tree_distant_group[index].prim_id = ~prim.prim_id;

      float energy = 0.0f;
      if (light->light_type == LIGHT_BACKGROUND) {
        /* integrate over cosine-weighted hemisphere */
        energy = light->get_average_radiance() * M_PI_F;

        /* We can set an arbitrary direction for the background light. */
        light_bounds.axis[0] = 0.0f;
        light_bounds.axis[1] = 0.0f;
        light_bounds.axis[2] = 1.0f;

        /* to-do: this may depend on portal lights as well. */
        light_bounds.theta_o = M_PI_F;
      }
      else {
        energy = prim.calculate_energy(scene);

        for (int i = 0; i < 3; i++) {
          light_bounds.axis[i] = -light->dir[i];
        }
        light_bounds.theta_o = tanf(light->angle * 0.5f);
      }

      distant_light_bounds = merge(distant_light_bounds, light_bounds);
      for (int i = 0; i < 3; i++) {
        light_tree_distant_group[index].direction[i] = light_bounds.axis[i];
      }
      light_tree_distant_group[index].bounding_radius = light_bounds.theta_o;
      light_tree_distant_group[index].energy = energy;
      light_array[~prim.prim_id] = index;

      light_tree_distant_group[num_distant_lights].energy += energy;
    }

    /* The net OrientationBounds contain bounding information about all the distant lights. */
    light_tree_distant_group[num_distant_lights].prim_id = -1;
    for (int i = 0; i < 3; i++) {
      light_tree_distant_group[num_distant_lights].direction[i] = distant_light_bounds.axis[i];
    }
    light_tree_distant_group[num_distant_lights].bounding_radius = distant_light_bounds.theta_o;

    dscene->light_tree_nodes.copy_to_device();
    dscene->light_tree_emitters.copy_to_device();
    dscene->light_tree_distant_group.copy_to_device();
    dscene->light_to_tree.copy_to_device();
    dscene->object_lookup_offset.copy_to_device();
    dscene->triangle_to_tree.copy_to_device();
  }

  /* emission area */
  KernelLightDistribution *distribution = dscene->light_distribution.alloc(num_distribution + 1);
  float totarea = 0.0f;

  /* triangles */
  size_t offset = 0;
  int j = 0;

  foreach (Object *object, scene->objects) {
    if (progress.get_cancel())
      return;

    if (!object_usable_as_light(object)) {
      j++;
      continue;
    }
    /* Sum area. */
    Mesh *mesh = static_cast<Mesh *>(object->get_geometry());
    bool transform_applied = mesh->transform_applied;
    Transform tfm = object->get_tfm();
    int object_id = j;
    int shader_flag = 0;

    if (!(object->get_visibility() & PATH_RAY_CAMERA)) {
      shader_flag |= SHADER_EXCLUDE_CAMERA;
    }
    if (!(object->get_visibility() & PATH_RAY_DIFFUSE)) {
      shader_flag |= SHADER_EXCLUDE_DIFFUSE;
    }
    if (!(object->get_visibility() & PATH_RAY_GLOSSY)) {
      shader_flag |= SHADER_EXCLUDE_GLOSSY;
    }
    if (!(object->get_visibility() & PATH_RAY_TRANSMIT)) {
      shader_flag |= SHADER_EXCLUDE_TRANSMIT;
    }
    if (!(object->get_visibility() & PATH_RAY_VOLUME_SCATTER)) {
      shader_flag |= SHADER_EXCLUDE_SCATTER;
    }
    if (!(object->get_is_shadow_catcher())) {
      shader_flag |= SHADER_EXCLUDE_SHADOW_CATCHER;
    }

    size_t mesh_num_triangles = mesh->num_triangles();
    for (size_t i = 0; i < mesh_num_triangles; i++) {
      int shader_index = mesh->get_shader()[i];
      Shader *shader = (shader_index < mesh->get_used_shaders().size()) ?
                           static_cast<Shader *>(mesh->get_used_shaders()[shader_index]) :
                           scene->default_surface;

      if (shader->get_use_mis() && shader->has_surface_emission) {
        distribution[offset].totarea = totarea;
        distribution[offset].prim = i + mesh->prim_offset;
        distribution[offset].mesh_light.shader_flag = shader_flag;
        distribution[offset].mesh_light.object_id = object_id;
        offset++;

        Mesh::Triangle t = mesh->get_triangle(i);
        if (!t.valid(&mesh->get_verts()[0])) {
          continue;
        }
        float3 p1 = mesh->get_verts()[t.v[0]];
        float3 p2 = mesh->get_verts()[t.v[1]];
        float3 p3 = mesh->get_verts()[t.v[2]];

        if (!transform_applied) {
          p1 = transform_point(&tfm, p1);
          p2 = transform_point(&tfm, p2);
          p3 = transform_point(&tfm, p3);
        }

        totarea += triangle_area(p1, p2, p3);
      }
    }

    j++;
  }

  float trianglearea = totarea;
  /* point lights */
  bool use_lamp_mis = false;
  int light_index = 0;

  if (num_lights > 0) {
    float lightarea = (totarea > 0.0f) ? totarea / num_lights : 1.0f;
    foreach (Light *light, scene->lights) {
      if (!light->is_enabled)
        continue;

      distribution[offset].totarea = totarea;
      distribution[offset].prim = ~light_index;
      distribution[offset].lamp.pad = 1.0f;
      distribution[offset].lamp.size = light->size;
      totarea += lightarea;

      if (light->light_type == LIGHT_DISTANT) {
        use_lamp_mis |= (light->angle > 0.0f && light->use_mis);
      }
      else if (light->light_type == LIGHT_POINT || light->light_type == LIGHT_SPOT) {
        use_lamp_mis |= (light->size > 0.0f && light->use_mis);
      }
      else if (light->light_type == LIGHT_AREA) {
        use_lamp_mis |= light->use_mis;
      }
      else if (light->light_type == LIGHT_BACKGROUND) {
        num_background_lights++;
      }

      light_index++;
      offset++;
    }
  }

  /* normalize cumulative distribution functions */
  distribution[num_distribution].totarea = totarea;
  distribution[num_distribution].prim = 0.0f;
  distribution[num_distribution].lamp.pad = 0.0f;
  distribution[num_distribution].lamp.size = 0.0f;

  if (totarea > 0.0f) {
    for (size_t i = 0; i < num_distribution; i++)
      distribution[i].totarea /= totarea;
    distribution[num_distribution].totarea = 1.0f;
  }

  if (progress.get_cancel())
    return;

  /* update device */
  KernelIntegrator *kintegrator = &dscene->data.integrator;
  KernelBackground *kbackground = &dscene->data.background;
  KernelFilm *kfilm = &dscene->data.film;
  kintegrator->use_direct_light = (totarea > 0.0f);

  if (kintegrator->use_direct_light) {
    /* number of emissives */
    kintegrator->num_distribution = num_distribution;

    /* precompute pdfs */
    kintegrator->distribution_pdf_triangles = 0.0f;
    kintegrator->distribution_pdf_lights = 0.0f;

    /* sample one, with 0.5 probability of light or triangle */
    /* to-do: this pdf is probably going to need adjustment if a light tree is used. */
    kintegrator->num_all_lights = num_lights;
    kintegrator->num_distant_lights = num_distant_lights;

    /* distribution_pdf_lights is used when sampling lights from a flat distribution. */
    if (trianglearea > 0.0f) {
      kintegrator->distribution_pdf_triangles = 1.0f / trianglearea;
      if (num_lights)
        kintegrator->distribution_pdf_triangles *= 0.5f;
    }

    if (num_lights) {
      kintegrator->distribution_pdf_lights = 1.0f / num_lights;
      if (trianglearea > 0.0f)
        kintegrator->distribution_pdf_lights *= 0.5f;
    }

    kintegrator->use_lamp_mis = use_lamp_mis;

    /* bit of an ugly hack to compensate for emitting triangles influencing
     * amount of samples we get for this pass */
    kfilm->pass_shadow_scale = 1.0f;

    /* TODO: this won't work for light tree. */
    if (kintegrator->distribution_pdf_triangles != 0.0f)
      kfilm->pass_shadow_scale /= 0.5f;

    if (num_background_lights < num_lights)
      kfilm->pass_shadow_scale /= (float)(num_lights - num_background_lights) / (float)num_lights;

    /* CDF */
    dscene->light_distribution.copy_to_device();

    /* Portals */
    if (num_portals > 0) {
      kbackground->portal_offset = light_index;
    }
  }
  else {
    if (light_tree_enabled) {
      dscene->light_tree_nodes.free();
      dscene->light_tree_emitters.free();
      dscene->light_tree_distant_group.free();
      dscene->light_to_tree.free();
      dscene->object_lookup_offset.free();
      dscene->triangle_to_tree.free();
    }
    dscene->light_distribution.free();

    kintegrator->num_distribution = 0;
    kintegrator->num_all_lights = 0;
    kintegrator->distribution_pdf_triangles = 0.0f;
    kintegrator->distribution_pdf_lights = 0.0f;
    kintegrator->use_lamp_mis = false;

    kfilm->pass_shadow_scale = 1.0f;
  }
}

static void background_cdf(
    int start, int end, int res_x, int res_y, const vector<float3> *pixels, float2 *cond_cdf)
{
  int cdf_width = res_x + 1;
  /* Conditional CDFs (rows, U direction). */
  for (int i = start; i < end; i++) {
    float sin_theta = sinf(M_PI_F * (i + 0.5f) / res_y);
    float3 env_color = (*pixels)[i * res_x];
    float ave_luminance = average(env_color);

    cond_cdf[i * cdf_width].x = ave_luminance * sin_theta;
    cond_cdf[i * cdf_width].y = 0.0f;

    for (int j = 1; j < res_x; j++) {
      env_color = (*pixels)[i * res_x + j];
      ave_luminance = average(env_color);

      cond_cdf[i * cdf_width + j].x = ave_luminance * sin_theta;
      cond_cdf[i * cdf_width + j].y = cond_cdf[i * cdf_width + j - 1].y +
                                      cond_cdf[i * cdf_width + j - 1].x / res_x;
    }

    const float cdf_total = cond_cdf[i * cdf_width + res_x - 1].y +
                            cond_cdf[i * cdf_width + res_x - 1].x / res_x;

    /* stuff the total into the brightness value for the last entry, because
     * we are going to normalize the CDFs to 0.0 to 1.0 afterwards */
    cond_cdf[i * cdf_width + res_x].x = cdf_total;

    if (cdf_total > 0.0f) {
      const float cdf_total_inv = 1.0f / cdf_total;
      for (int j = 1; j < res_x; j++) {
        cond_cdf[i * cdf_width + j].y *= cdf_total_inv;
      }
    }

    cond_cdf[i * cdf_width + res_x].y = 1.0f;
  }
}

void LightManager::device_update_background(Device *device,
                                            DeviceScene *dscene,
                                            Scene *scene,
                                            Progress &progress)
{
  KernelBackground *kbackground = &dscene->data.background;
  Light *background_light = NULL;

  bool background_mis = false;
  size_t num_portals = 0;

  /* find background light */
  foreach (Light *light, scene->lights) {
    if (light->light_type == LIGHT_BACKGROUND && light->is_enabled) {
      background_light = light;
      background_mis |= light->use_mis;
    }
    if (light->is_portal) {
      num_portals++;
    }
  }

  kbackground->num_portals = num_portals;
  kbackground->portal_offset = 0;
  kbackground->portal_weight = num_portals > 0 ? 1.0f : 0.0f;
  kbackground->map_weight = background_mis ? 1.0f : 0.0f;
  kbackground->sun_weight = 0.0f;

  /* no background light found, signal renderer to skip sampling */
  if (!background_light || !background_light->is_enabled) {
    kbackground->map_res_x = 0;
    kbackground->map_res_y = 0;
    kbackground->use_mis = (kbackground->portal_weight > 0.0f);
    return;
  }

  progress.set_status("Updating Lights", "Importance map");

  int2 environment_res = make_int2(0, 0);
  Shader *shader = scene->background->get_shader(scene);
  int num_suns = 0;
  foreach (ShaderNode *node, shader->graph->nodes) {
    if (node->type == EnvironmentTextureNode::get_node_type()) {
      EnvironmentTextureNode *env = (EnvironmentTextureNode *)node;
      ImageMetaData metadata;
      if (!env->handle.empty()) {
        ImageMetaData metadata = env->handle.metadata();
        environment_res.x = max(environment_res.x, (int)metadata.width);
        environment_res.y = max(environment_res.y, (int)metadata.height);
      }
    }
    if (node->type == SkyTextureNode::get_node_type()) {
      SkyTextureNode *sky = (SkyTextureNode *)node;
      if (sky->get_sky_type() == NODE_SKY_NISHITA && sky->get_sun_disc()) {
        /* Ensure that the input coordinates aren't transformed before they reach the node.
         * If that is the case, the logic used for sampling the sun's location does not work
         * and we have to fall back to map-based sampling. */
        const ShaderInput *vec_in = sky->input("Vector");
        if (vec_in && vec_in->link && vec_in->link->parent) {
          ShaderNode *vec_src = vec_in->link->parent;
          if ((vec_src->type != TextureCoordinateNode::get_node_type()) ||
              (vec_in->link != vec_src->output("Generated"))) {
            environment_res.x = max(environment_res.x, 4096);
            environment_res.y = max(environment_res.y, 2048);
            continue;
          }
        }

        /* Determine sun direction from lat/long and texture mapping. */
        float latitude = sky->get_sun_elevation();
        float longitude = M_2PI_F - sky->get_sun_rotation() + M_PI_2_F;
        float3 sun_direction = make_float3(
            cosf(latitude) * cosf(longitude), cosf(latitude) * sinf(longitude), sinf(latitude));
        Transform sky_transform = transform_inverse(sky->tex_mapping.compute_transform());
        sun_direction = transform_direction(&sky_transform, sun_direction);

        /* Pack sun direction and size. */
        float half_angle = sky->get_sun_size() * 0.5f;
        kbackground->sun = make_float4(
            sun_direction.x, sun_direction.y, sun_direction.z, half_angle);

        /* empirical value */
        kbackground->sun_weight = 4.0f;
        environment_res.x = max(environment_res.x, 512);
        environment_res.y = max(environment_res.y, 256);
        num_suns++;
      }
    }
  }

  /* If there's more than one sun, fall back to map sampling instead. */
  if (num_suns != 1) {
    kbackground->sun_weight = 0.0f;
    environment_res.x = max(environment_res.x, 4096);
    environment_res.y = max(environment_res.y, 2048);
  }

  /* Enable MIS for background sampling if any strategy is active. */
  kbackground->use_mis = (kbackground->portal_weight + kbackground->map_weight +
                          kbackground->sun_weight) > 0.0f;

  /* get the resolution from the light's size (we stuff it in there) */
  int2 res = make_int2(background_light->map_resolution, background_light->map_resolution / 2);
  /* If the resolution isn't set manually, try to find an environment texture. */
  if (res.x == 0) {
    res = environment_res;
    if (res.x > 0 && res.y > 0) {
      VLOG_INFO << "Automatically set World MIS resolution to " << res.x << " by " << res.y
                << "\n";
    }
  }
  /* If it's still unknown, just use the default. */
  if (res.x == 0 || res.y == 0) {
    res = make_int2(1024, 512);
    VLOG_INFO << "Setting World MIS resolution to default\n";
  }
  kbackground->map_res_x = res.x;
  kbackground->map_res_y = res.y;

  vector<float3> pixels;
  shade_background_pixels(device, dscene, res.x, res.y, pixels, progress);

  if (progress.get_cancel())
    return;

  /* build row distributions and column distribution for the infinite area environment light */
  int cdf_width = res.x + 1;
  float2 *marg_cdf = dscene->light_background_marginal_cdf.alloc(res.y + 1);
  float2 *cond_cdf = dscene->light_background_conditional_cdf.alloc(cdf_width * res.y);

  double time_start = time_dt();

  /* Create CDF in parallel. */
  const int rows_per_task = divide_up(10240, res.x);
  parallel_for(blocked_range<size_t>(0, res.y, rows_per_task),
               [&](const blocked_range<size_t> &r) {
                 background_cdf(r.begin(), r.end(), res.x, res.y, &pixels, cond_cdf);
               });

  /* marginal CDFs (column, V direction, sum of rows) */
  marg_cdf[0].x = cond_cdf[res.x].x;
  marg_cdf[0].y = 0.0f;

  for (int i = 1; i < res.y; i++) {
    marg_cdf[i].x = cond_cdf[i * cdf_width + res.x].x;
    marg_cdf[i].y = marg_cdf[i - 1].y + marg_cdf[i - 1].x / res.y;
  }

  float cdf_total = marg_cdf[res.y - 1].y + marg_cdf[res.y - 1].x / res.y;
  marg_cdf[res.y].x = cdf_total;

  background_light->set_average_radiance(cdf_total * M_PI_2_F);

  if (cdf_total > 0.0f)
    for (int i = 1; i < res.y; i++)
      marg_cdf[i].y /= cdf_total;

  marg_cdf[res.y].y = 1.0f;

  VLOG_WORK << "Background MIS build time " << time_dt() - time_start << "\n";

  /* update device */
  dscene->light_background_marginal_cdf.copy_to_device();
  dscene->light_background_conditional_cdf.copy_to_device();
}

void LightManager::device_update_points(Device *, DeviceScene *dscene, Scene *scene)
{
  int num_scene_lights = scene->lights.size();

  int num_lights = 0;
  foreach (Light *light, scene->lights) {
    if (light->is_enabled || light->is_portal) {
      num_lights++;
    }
  }

  KernelLight *klights = dscene->lights.alloc(num_lights);

  if (num_lights == 0) {
    VLOG_WORK << "No effective light, ignoring points update.";
    return;
  }

  int light_index = 0;

  foreach (Light *light, scene->lights) {
    if (!light->is_enabled) {
      continue;
    }

    float3 co = light->co;
    Shader *shader = (light->shader) ? light->shader : scene->default_light;
    int shader_id = scene->shader_manager->get_shader_id(shader);
    int max_bounces = light->max_bounces;
    float random = (float)light->random_id * (1.0f / (float)0xFFFFFFFF);

    if (!light->cast_shadow)
      shader_id &= ~SHADER_CAST_SHADOW;

    if (!light->use_camera) {
      shader_id |= SHADER_EXCLUDE_CAMERA;
    }
    if (!light->use_diffuse) {
      shader_id |= SHADER_EXCLUDE_DIFFUSE;
    }
    if (!light->use_glossy) {
      shader_id |= SHADER_EXCLUDE_GLOSSY;
    }
    if (!light->use_transmission) {
      shader_id |= SHADER_EXCLUDE_TRANSMIT;
    }
    if (!light->use_scatter) {
      shader_id |= SHADER_EXCLUDE_SCATTER;
    }
    if (!light->is_shadow_catcher) {
      shader_id |= SHADER_EXCLUDE_SHADOW_CATCHER;
    }

    klights[light_index].type = light->light_type;
    klights[light_index].strength[0] = light->strength.x;
    klights[light_index].strength[1] = light->strength.y;
    klights[light_index].strength[2] = light->strength.z;

    if (light->light_type == LIGHT_POINT) {
      shader_id &= ~SHADER_AREA_LIGHT;

      float radius = light->size;
      float invarea = (radius > 0.0f) ? 1.0f / (M_PI_F * radius * radius) : 1.0f;

      if (light->use_mis && radius > 0.0f)
        shader_id |= SHADER_USE_MIS;

      klights[light_index].co[0] = co.x;
      klights[light_index].co[1] = co.y;
      klights[light_index].co[2] = co.z;

      klights[light_index].spot.radius = radius;
      klights[light_index].spot.invarea = invarea;
    }
    else if (light->light_type == LIGHT_DISTANT) {
      shader_id &= ~SHADER_AREA_LIGHT;

      float angle = light->angle / 2.0f;
      float radius = tanf(angle);
      float cosangle = cosf(angle);
      float area = M_PI_F * radius * radius;
      float invarea = (area > 0.0f) ? 1.0f / area : 1.0f;
      float3 dir = light->dir;

      dir = safe_normalize(dir);

      if (light->use_mis && area > 0.0f)
        shader_id |= SHADER_USE_MIS;

      klights[light_index].co[0] = dir.x;
      klights[light_index].co[1] = dir.y;
      klights[light_index].co[2] = dir.z;

      klights[light_index].distant.invarea = invarea;
      klights[light_index].distant.radius = radius;
      klights[light_index].distant.cosangle = cosangle;
    }
    else if (light->light_type == LIGHT_BACKGROUND) {
      uint visibility = scene->background->get_visibility();

      dscene->data.background.light_index = light_index;

      shader_id &= ~SHADER_AREA_LIGHT;
      shader_id |= SHADER_USE_MIS;

      if (!(visibility & PATH_RAY_DIFFUSE)) {
        shader_id |= SHADER_EXCLUDE_DIFFUSE;
      }
      if (!(visibility & PATH_RAY_GLOSSY)) {
        shader_id |= SHADER_EXCLUDE_GLOSSY;
      }
      if (!(visibility & PATH_RAY_TRANSMIT)) {
        shader_id |= SHADER_EXCLUDE_TRANSMIT;
      }
      if (!(visibility & PATH_RAY_VOLUME_SCATTER)) {
        shader_id |= SHADER_EXCLUDE_SCATTER;
      }
    }
    else if (light->light_type == LIGHT_AREA) {
      float3 axisu = light->axisu * (light->sizeu * light->size);
      float3 axisv = light->axisv * (light->sizev * light->size);
      float area = len(axisu) * len(axisv);
      if (light->round) {
        area *= -M_PI_4_F;
      }
      float invarea = (area != 0.0f) ? 1.0f / area : 1.0f;
      float3 dir = light->dir;

      /* Convert from spread angle 0..180 to 90..0, clamping to a minimum
       * angle to avoid excessive noise. */
      const float min_spread_angle = 1.0f * M_PI_F / 180.0f;
      const float spread_angle = 0.5f * (M_PI_F - max(light->spread, min_spread_angle));
      /* Normalization computed using:
       * integrate cos(x) * (1 - tan(x) * tan(a)) * sin(x) from x = 0 to pi/2 - a. */
      const float tan_spread = tanf(spread_angle);
      const float normalize_spread = 2.0f / (2.0f + (2.0f * spread_angle - M_PI_F) * tan_spread);

      dir = safe_normalize(dir);

      if (light->use_mis && area != 0.0f)
        shader_id |= SHADER_USE_MIS;

      klights[light_index].co[0] = co.x;
      klights[light_index].co[1] = co.y;
      klights[light_index].co[2] = co.z;

      klights[light_index].area.axisu[0] = axisu.x;
      klights[light_index].area.axisu[1] = axisu.y;
      klights[light_index].area.axisu[2] = axisu.z;
      klights[light_index].area.axisv[0] = axisv.x;
      klights[light_index].area.axisv[1] = axisv.y;
      klights[light_index].area.axisv[2] = axisv.z;
      klights[light_index].area.invarea = invarea;
      klights[light_index].area.dir[0] = dir.x;
      klights[light_index].area.dir[1] = dir.y;
      klights[light_index].area.dir[2] = dir.z;
      klights[light_index].area.tan_spread = tan_spread;
      klights[light_index].area.normalize_spread = normalize_spread;
    }
    else if (light->light_type == LIGHT_SPOT) {
      shader_id &= ~SHADER_AREA_LIGHT;

      float radius = light->size;
      float invarea = (radius > 0.0f) ? 1.0f / (M_PI_F * radius * radius) : 1.0f;
      float spot_angle = cosf(light->spot_angle * 0.5f);
      float spot_smooth = (1.0f - spot_angle) * light->spot_smooth;
      float3 dir = light->dir;

      dir = safe_normalize(dir);

      if (light->use_mis && radius > 0.0f)
        shader_id |= SHADER_USE_MIS;

      klights[light_index].co[0] = co.x;
      klights[light_index].co[1] = co.y;
      klights[light_index].co[2] = co.z;

      klights[light_index].spot.radius = radius;
      klights[light_index].spot.invarea = invarea;
      klights[light_index].spot.spot_angle = spot_angle;
      klights[light_index].spot.spot_smooth = spot_smooth;
      klights[light_index].spot.dir[0] = dir.x;
      klights[light_index].spot.dir[1] = dir.y;
      klights[light_index].spot.dir[2] = dir.z;
    }

    klights[light_index].shader_id = shader_id;

    klights[light_index].max_bounces = max_bounces;
    klights[light_index].random = random;
    klights[light_index].use_caustics = light->use_caustics;

    klights[light_index].tfm = light->tfm;
    klights[light_index].itfm = transform_inverse(light->tfm);

    auto it = scene->lightgroups.find(light->lightgroup);
    if (it != scene->lightgroups.end()) {
      klights[light_index].lightgroup = it->second;
    }
    else {
      klights[light_index].lightgroup = LIGHTGROUP_NONE;
    }

    light_index++;
  }

  /* TODO(sergey): Consider moving portals update to their own function
   * keeping this one more manageable.
   */
  foreach (Light *light, scene->lights) {
    if (!light->is_portal)
      continue;
    assert(light->light_type == LIGHT_AREA);

    float3 co = light->co;
    float3 axisu = light->axisu * (light->sizeu * light->size);
    float3 axisv = light->axisv * (light->sizev * light->size);
    float area = len(axisu) * len(axisv);
    if (light->round) {
      area *= -M_PI_4_F;
    }
    float invarea = (area != 0.0f) ? 1.0f / area : 1.0f;
    float3 dir = light->dir;

    dir = safe_normalize(dir);

    klights[light_index].co[0] = co.x;
    klights[light_index].co[1] = co.y;
    klights[light_index].co[2] = co.z;

    klights[light_index].area.axisu[0] = axisu.x;
    klights[light_index].area.axisu[1] = axisu.y;
    klights[light_index].area.axisu[2] = axisu.z;
    klights[light_index].area.axisv[0] = axisv.x;
    klights[light_index].area.axisv[1] = axisv.y;
    klights[light_index].area.axisv[2] = axisv.z;
    klights[light_index].area.invarea = invarea;
    klights[light_index].area.dir[0] = dir.x;
    klights[light_index].area.dir[1] = dir.y;
    klights[light_index].area.dir[2] = dir.z;
    klights[light_index].tfm = light->tfm;
    klights[light_index].itfm = transform_inverse(light->tfm);

    light_index++;
  }

  VLOG_INFO << "Number of lights sent to the device: " << light_index;

  VLOG_INFO << "Number of lights without contribution: " << num_scene_lights - light_index;

  dscene->lights.copy_to_device();
}

void LightManager::device_update(Device *device,
                                 DeviceScene *dscene,
                                 Scene *scene,
                                 Progress &progress)
{
  if (!need_update())
    return;

  scoped_callback_timer timer([scene](double time) {
    if (scene->update_stats) {
      scene->update_stats->light.times.add_entry({"device_update", time});
    }
  });

  VLOG_INFO << "Total " << scene->lights.size() << " lights.";

  /* Detect which lights are enabled, also determines if we need to update the background. */
  test_enabled_lights(scene);

  device_free(device, dscene, need_update_background);

  device_update_points(device, dscene, scene);
  if (progress.get_cancel())
    return;

  if (need_update_background) {
    device_update_background(device, dscene, scene, progress);
    if (progress.get_cancel())
      return;
  }

  device_update_distribution(device, dscene, scene, progress);
  if (progress.get_cancel())
    return;

  device_update_ies(dscene);
  if (progress.get_cancel())
    return;

  update_flags = UPDATE_NONE;
  need_update_background = false;
}

void LightManager::device_free(Device *, DeviceScene *dscene, const bool free_background)
{
  /* to-do: check if the light tree member variables need to be wrapped in a conditional too*/
  dscene->light_tree_nodes.free();
  dscene->light_tree_emitters.free();
  dscene->light_tree_distant_group.free();
  dscene->light_to_tree.free();
  dscene->triangle_to_tree.free();

  dscene->light_distribution.free();
  dscene->lights.free();
  if (free_background) {
    dscene->light_background_marginal_cdf.free();
    dscene->light_background_conditional_cdf.free();
  }
  dscene->ies_lights.free();
}

void LightManager::tag_update(Scene * /*scene*/, uint32_t flag)
{
  update_flags |= flag;
}

bool LightManager::need_update() const
{
  return update_flags != UPDATE_NONE;
}

int LightManager::add_ies_from_file(const string &filename)
{
  string content;

  /* If the file can't be opened, call with an empty line */
  if (filename.empty() || !path_read_text(filename.c_str(), content)) {
    content = "\n";
  }

  return add_ies(content);
}

int LightManager::add_ies(const string &content)
{
  uint hash = hash_string(content.c_str());

  thread_scoped_lock ies_lock(ies_mutex);

  /* Check whether this IES already has a slot. */
  size_t slot;
  for (slot = 0; slot < ies_slots.size(); slot++) {
    if (ies_slots[slot]->hash == hash) {
      ies_slots[slot]->users++;
      return slot;
    }
  }

  /* Try to find an empty slot for the new IES. */
  for (slot = 0; slot < ies_slots.size(); slot++) {
    if (ies_slots[slot]->users == 0 && ies_slots[slot]->hash == 0) {
      break;
    }
  }

  /* If there's no free slot, add one. */
  if (slot == ies_slots.size()) {
    ies_slots.push_back(new IESSlot());
  }

  ies_slots[slot]->ies.load(content);
  ies_slots[slot]->users = 1;
  ies_slots[slot]->hash = hash;

  update_flags = UPDATE_ALL;
  need_update_background = true;

  return slot;
}

void LightManager::remove_ies(int slot)
{
  thread_scoped_lock ies_lock(ies_mutex);

  if (slot < 0 || slot >= ies_slots.size()) {
    assert(false);
    return;
  }

  assert(ies_slots[slot]->users > 0);
  ies_slots[slot]->users--;

  /* If the slot has no more users, update the device to remove it. */
  if (ies_slots[slot]->users == 0) {
    update_flags |= UPDATE_ALL;
    need_update_background = true;
  }
}

void LightManager::device_update_ies(DeviceScene *dscene)
{
  /* Clear empty slots. */
  foreach (IESSlot *slot, ies_slots) {
    if (slot->users == 0) {
      slot->hash = 0;
      slot->ies.clear();
    }
  }

  /* Shrink the slot table by removing empty slots at the end. */
  int slot_end;
  for (slot_end = ies_slots.size(); slot_end; slot_end--) {
    if (ies_slots[slot_end - 1]->users > 0) {
      /* If the preceding slot has users, we found the new end of the table. */
      break;
    }
    else {
      /* The slot will be past the new end of the table, so free it. */
      delete ies_slots[slot_end - 1];
    }
  }
  ies_slots.resize(slot_end);

  if (ies_slots.size() > 0) {
    int packed_size = 0;
    foreach (IESSlot *slot, ies_slots) {
      packed_size += slot->ies.packed_size();
    }

    /* ies_lights starts with an offset table that contains the offset of every slot,
     * or -1 if the slot is invalid.
     * Following that table, the packed valid IES lights are stored. */
    float *data = dscene->ies_lights.alloc(ies_slots.size() + packed_size);

    int offset = ies_slots.size();
    for (int i = 0; i < ies_slots.size(); i++) {
      int size = ies_slots[i]->ies.packed_size();
      if (size > 0) {
        data[i] = __int_as_float(offset);
        ies_slots[i]->ies.pack(data + offset);
        offset += size;
      }
      else {
        data[i] = __int_as_float(-1);
      }
    }

    dscene->ies_lights.copy_to_device();
  }
}

CCL_NAMESPACE_END