Cycles: ray visibility options now work for lamps and mesh lights, with and without
[blender.git] / intern / cycles / render / light.cpp
1 /*
2  * Copyright 2011, Blender Foundation.
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  */
18
19 #include "device.h"
20 #include "integrator.h"
21 #include "film.h"
22 #include "light.h"
23 #include "mesh.h"
24 #include "object.h"
25 #include "scene.h"
26 #include "shader.h"
27
28 #include "util_foreach.h"
29 #include "util_progress.h"
30
31 CCL_NAMESPACE_BEGIN
32
33 static void dump_background_pixels(Device *device, DeviceScene *dscene, int res, vector<float3>& pixels)
34 {
35         /* create input */
36         int width = res;
37         int height = res;
38
39         device_vector<uint4> d_input;
40         device_vector<float4> d_output;
41
42         uint4 *d_input_data = d_input.resize(width*height);
43
44         for(int y = 0; y < height; y++) {
45                 for(int x = 0; x < width; x++) {
46                         float u = x/(float)width;
47                         float v = y/(float)height;
48
49                         uint4 in = make_uint4(__float_as_int(u), __float_as_int(v), 0, 0);
50                         d_input_data[x + y*width] = in;
51                 }
52         }
53
54         /* compute on device */
55         float4 *d_output_data = d_output.resize(width*height);
56         memset((void*)d_output.data_pointer, 0, d_output.memory_size());
57
58         device->const_copy_to("__data", &dscene->data, sizeof(dscene->data));
59
60         device->mem_alloc(d_input, MEM_READ_ONLY);
61         device->mem_copy_to(d_input);
62         device->mem_alloc(d_output, MEM_WRITE_ONLY);
63
64         DeviceTask main_task(DeviceTask::SHADER);
65         main_task.shader_input = d_input.device_pointer;
66         main_task.shader_output = d_output.device_pointer;
67         main_task.shader_eval_type = SHADER_EVAL_BACKGROUND;
68         main_task.shader_x = 0;
69         main_task.shader_w = width*height;
70
71         /* disabled splitting for now, there's an issue with multi-GPU mem_copy_from */
72         list<DeviceTask> split_tasks;
73         main_task.split_max_size(split_tasks, 128*128); 
74
75         foreach(DeviceTask& task, split_tasks) {
76                 device->task_add(task);
77                 device->task_wait();
78                 device->mem_copy_from(d_output, task.shader_x, 1, task.shader_w, sizeof(float4));
79         }
80
81         device->mem_free(d_input);
82         device->mem_free(d_output);
83
84         d_output_data = reinterpret_cast<float4*>(d_output.data_pointer);
85
86         pixels.resize(width*height);
87
88         for(int y = 0; y < height; y++) {
89                 for(int x = 0; x < width; x++) {
90                         pixels[y*width + x].x = d_output_data[y*width + x].x;
91                         pixels[y*width + x].y = d_output_data[y*width + x].y;
92                         pixels[y*width + x].z = d_output_data[y*width + x].z;
93                 }
94         }
95 }
96
97 /* Light */
98
99 Light::Light()
100 {
101         type = LIGHT_POINT;
102
103         co = make_float3(0.0f, 0.0f, 0.0f);
104
105         dir = make_float3(0.0f, 0.0f, 0.0f);
106         size = 0.0f;
107
108         axisu = make_float3(0.0f, 0.0f, 0.0f);
109         sizeu = 1.0f;
110         axisv = make_float3(0.0f, 0.0f, 0.0f);
111         sizev = 1.0f;
112
113         map_resolution = 512;
114
115         spot_angle = M_PI_4_F;
116         spot_smooth = 0.0f;
117
118         cast_shadow = true;
119         use_mis = false;
120         use_diffuse = true;
121         use_glossy = true;
122         use_transmission = true;
123
124         shader = 0;
125         samples = 1;
126 }
127
128 void Light::tag_update(Scene *scene)
129 {
130         scene->light_manager->need_update = true;
131 }
132
133 /* Light Manager */
134
135 LightManager::LightManager()
136 {
137         need_update = true;
138 }
139
140 LightManager::~LightManager()
141 {
142 }
143
144 void LightManager::device_update_distribution(Device *device, DeviceScene *dscene, Scene *scene, Progress& progress)
145 {
146         progress.set_status("Updating Lights", "Computing distribution");
147
148         /* count */
149         size_t num_lights = scene->lights.size();
150         size_t num_background_lights = 0;
151         size_t num_triangles = 0;
152         size_t num_curve_segments = 0;
153
154         foreach(Object *object, scene->objects) {
155                 Mesh *mesh = object->mesh;
156                 bool have_emission = false;
157
158                 /* skip if we are not visible for BSDFs */
159                 if(!(object->visibility & (PATH_RAY_DIFFUSE|PATH_RAY_GLOSSY|PATH_RAY_TRANSMIT)))
160                         continue;
161
162                 /* skip if we have no emission shaders */
163                 foreach(uint sindex, mesh->used_shaders) {
164                         Shader *shader = scene->shaders[sindex];
165
166                         if(shader->sample_as_light && shader->has_surface_emission) {
167                                 have_emission = true;
168                                 break;
169                         }
170                 }
171
172                 /* count triangles */
173                 if(have_emission) {
174                         for(size_t i = 0; i < mesh->triangles.size(); i++) {
175                                 Shader *shader = scene->shaders[mesh->shader[i]];
176
177                                 if(shader->sample_as_light && shader->has_surface_emission)
178                                         num_triangles++;
179                         }
180
181                         /* disabled for curves */
182 #if 0
183                         foreach(Mesh::Curve& curve, mesh->curves) {
184                                 Shader *shader = scene->shaders[curve.shader];
185
186                                 if(shader->sample_as_light && shader->has_surface_emission)
187                                         num_curve_segments += curve.num_segments();
188 #endif
189                 }
190         }
191
192         size_t num_distribution = num_triangles + num_curve_segments;
193         num_distribution += num_lights;
194
195         /* emission area */
196         float4 *distribution = dscene->light_distribution.resize(num_distribution + 1);
197         float totarea = 0.0f;
198
199         /* triangles */
200         size_t offset = 0;
201         int j = 0;
202
203         foreach(Object *object, scene->objects) {
204                 Mesh *mesh = object->mesh;
205                 bool have_emission = false;
206
207                 /* skip if we are not visible for BSDFs */
208                 if(!(object->visibility & (PATH_RAY_DIFFUSE|PATH_RAY_GLOSSY|PATH_RAY_TRANSMIT))) {
209                         j++;
210                         continue;
211                 }
212
213                 /* skip if we have no emission shaders */
214                 foreach(uint sindex, mesh->used_shaders) {
215                         Shader *shader = scene->shaders[sindex];
216
217                         if(shader->sample_as_light && shader->has_surface_emission) {
218                                 have_emission = true;
219                                 break;
220                         }
221                 }
222
223                 /* sum area */
224                 if(have_emission) {
225                         bool transform_applied = mesh->transform_applied;
226                         Transform tfm = object->tfm;
227                         int object_id = j;
228                         int shader_id = SHADER_MASK;
229
230                         if(transform_applied)
231                                 object_id = ~object_id;
232
233                         if(!(object->visibility & PATH_RAY_DIFFUSE)) {
234                                 shader_id |= SHADER_EXCLUDE_DIFFUSE;
235                                 scene->film->use_light_visibility = true;
236                         }
237                         if(!(object->visibility & PATH_RAY_GLOSSY)) {
238                                 shader_id |= SHADER_EXCLUDE_GLOSSY;
239                                 scene->film->use_light_visibility = true;
240                         }
241                         if(!(object->visibility & PATH_RAY_TRANSMIT)) {
242                                 shader_id |= SHADER_EXCLUDE_TRANSMIT;
243                                 scene->film->use_light_visibility = true;
244                         }
245
246                         for(size_t i = 0; i < mesh->triangles.size(); i++) {
247                                 Shader *shader = scene->shaders[mesh->shader[i]];
248
249                                 if(shader->sample_as_light && shader->has_surface_emission) {
250                                         distribution[offset].x = totarea;
251                                         distribution[offset].y = __int_as_float(i + mesh->tri_offset);
252                                         distribution[offset].z = __int_as_float(shader_id);
253                                         distribution[offset].w = __int_as_float(object_id);
254                                         offset++;
255
256                                         Mesh::Triangle t = mesh->triangles[i];
257                                         float3 p1 = mesh->verts[t.v[0]];
258                                         float3 p2 = mesh->verts[t.v[1]];
259                                         float3 p3 = mesh->verts[t.v[2]];
260
261                                         if(!transform_applied) {
262                                                 p1 = transform_point(&tfm, p1);
263                                                 p2 = transform_point(&tfm, p2);
264                                                 p3 = transform_point(&tfm, p3);
265                                         }
266
267                                         totarea += triangle_area(p1, p2, p3);
268                                 }
269                         }
270
271                         /* sample as light disabled for strands */
272 #if 0
273                         size_t i = 0;
274
275                         foreach(Mesh::Curve& curve, mesh->curves) {
276                                 Shader *shader = scene->shaders[curve.shader];
277                                 int first_key = curve.first_key;
278
279                                 if(shader->sample_as_light && shader->has_surface_emission) {
280                                         for(int j = 0; j < curve.num_segments(); j++) {
281                                                 distribution[offset].x = totarea;
282                                                 distribution[offset].y = __int_as_float(i + mesh->curve_offset); // XXX fix kernel code
283                                                 distribution[offset].z = __int_as_float(j) & SHADER_MASK;
284                                                 distribution[offset].w = __int_as_float(object_id);
285                                                 offset++;
286                                 
287                                                 float3 p1 = mesh->curve_keys[first_key + j].loc;
288                                                 float r1 = mesh->curve_keys[first_key + j].radius;
289                                                 float3 p2 = mesh->curve_keys[first_key + j + 1].loc;
290                                                 float r2 = mesh->curve_keys[first_key + j + 1].radius;
291                                 
292                                                 if(!transform_applied) {
293                                                         p1 = transform_point(&tfm, p1);
294                                                         p2 = transform_point(&tfm, p2);
295                                                 }
296                                 
297                                                 totarea += M_PI_F * (r1 + r2) * len(p1 - p2);
298                                         }
299                                 }
300
301                                 i++;
302                         }
303 #endif
304                 }
305
306                 if(progress.get_cancel()) return;
307
308                 j++;
309         }
310
311         float trianglearea = totarea;
312
313         /* point lights */
314         float lightarea = (totarea > 0.0f)? totarea/scene->lights.size(): 1.0f;
315         bool use_lamp_mis = false;
316
317         for(int i = 0; i < scene->lights.size(); i++, offset++) {
318                 Light *light = scene->lights[i];
319
320                 distribution[offset].x = totarea;
321                 distribution[offset].y = __int_as_float(~(int)i);
322                 distribution[offset].z = 1.0f;
323                 distribution[offset].w = light->size;
324                 totarea += lightarea;
325
326                 if(light->size > 0.0f && light->use_mis)
327                         use_lamp_mis = true;
328                 if(light->type == LIGHT_BACKGROUND)
329                         num_background_lights++;
330         }
331
332         /* normalize cumulative distribution functions */
333         distribution[num_distribution].x = totarea;
334         distribution[num_distribution].y = 0.0f;
335         distribution[num_distribution].z = 0.0f;
336         distribution[num_distribution].w = 0.0f;
337
338         if(totarea > 0.0f) {
339                 for(size_t i = 0; i < num_distribution; i++)
340                         distribution[i].x /= totarea;
341                 distribution[num_distribution].x = 1.0f;
342         }
343
344         if(progress.get_cancel()) return;
345
346         /* update device */
347         KernelIntegrator *kintegrator = &dscene->data.integrator;
348         KernelFilm *kfilm = &dscene->data.film;
349         kintegrator->use_direct_light = (totarea > 0.0f);
350
351         if(kintegrator->use_direct_light) {
352                 /* number of emissives */
353                 kintegrator->num_distribution = num_distribution;
354
355                 /* precompute pdfs */
356                 kintegrator->pdf_triangles = 0.0f;
357                 kintegrator->pdf_lights = 0.0f;
358                 kintegrator->inv_pdf_lights = 0.0f;
359
360                 /* sample one, with 0.5 probability of light or triangle */
361                 kintegrator->num_all_lights = num_lights;
362
363                 if(trianglearea > 0.0f) {
364                         kintegrator->pdf_triangles = 1.0f/trianglearea;
365                         if(num_lights)
366                                 kintegrator->pdf_triangles *= 0.5f;
367                 }
368
369                 if(num_lights) {
370                         kintegrator->pdf_lights = 1.0f/num_lights;
371                         if(trianglearea > 0.0f)
372                                 kintegrator->pdf_lights *= 0.5f;
373
374                         kintegrator->inv_pdf_lights = 1.0f/kintegrator->pdf_lights;
375                 }
376
377                 kintegrator->use_lamp_mis = use_lamp_mis;
378
379                 /* bit of an ugly hack to compensate for emitting triangles influencing
380                  * amount of samples we get for this pass */
381                 kfilm->pass_shadow_scale = 1.0f;
382
383                 if(kintegrator->pdf_triangles != 0.0f)
384                         kfilm->pass_shadow_scale *= 0.5f;
385
386                 if(num_background_lights < num_lights)
387                         kfilm->pass_shadow_scale *= (float)(num_lights - num_background_lights)/(float)num_lights;
388
389                 /* CDF */
390                 device->tex_alloc("__light_distribution", dscene->light_distribution);
391         }
392         else {
393                 dscene->light_distribution.clear();
394
395                 kintegrator->num_distribution = 0;
396                 kintegrator->num_all_lights = 0;
397                 kintegrator->pdf_triangles = 0.0f;
398                 kintegrator->pdf_lights = 0.0f;
399                 kintegrator->inv_pdf_lights = 0.0f;
400                 kintegrator->use_lamp_mis = false;
401                 kfilm->pass_shadow_scale = 1.0f;
402         }
403 }
404
405 void LightManager::device_update_background(Device *device, DeviceScene *dscene, Scene *scene, Progress& progress)
406 {
407         KernelIntegrator *kintegrator = &dscene->data.integrator;
408         Light *background_light = NULL;
409
410         /* find background light */
411         foreach(Light *light, scene->lights) {
412                 if(light->type == LIGHT_BACKGROUND) {
413                         background_light = light;
414                         break;
415                 }
416         }
417
418         /* no background light found, signal renderer to skip sampling */
419         if(!background_light) {
420                 kintegrator->pdf_background_res = 0;
421                 return;
422         }
423
424         progress.set_status("Updating Lights", "Importance map");
425
426         assert(kintegrator->use_direct_light);
427
428         /* get the resolution from the light's size (we stuff it in there) */
429         int res = background_light->map_resolution;
430         kintegrator->pdf_background_res = res;
431
432         assert(res > 0);
433
434         vector<float3> pixels;
435         dump_background_pixels(device, dscene, res, pixels);
436
437         if(progress.get_cancel())
438                 return;
439
440         /* build row distributions and column distribution for the infinite area environment light */
441         int cdf_count = res + 1;
442         float2 *marg_cdf = dscene->light_background_marginal_cdf.resize(cdf_count);
443         float2 *cond_cdf = dscene->light_background_conditional_cdf.resize(cdf_count * cdf_count);
444
445         /* conditional CDFs (rows, U direction) */
446         for(int i = 0; i < res; i++) {
447                 float sin_theta = sinf(M_PI_F * (i + 0.5f) / res);
448                 float3 env_color = pixels[i * res];
449                 float ave_luminamce = average(env_color);
450
451                 cond_cdf[i * cdf_count].x = ave_luminamce * sin_theta;
452                 cond_cdf[i * cdf_count].y = 0.0f;
453
454                 for(int j = 1; j < res; j++) {
455                         env_color = pixels[i * res + j];
456                         ave_luminamce = average(env_color);
457
458                         cond_cdf[i * cdf_count + j].x = ave_luminamce * sin_theta;
459                         cond_cdf[i * cdf_count + j].y = cond_cdf[i * cdf_count + j - 1].y + cond_cdf[i * cdf_count + j - 1].x / res;
460                 }
461
462                 float cdf_total = cond_cdf[i * cdf_count + res - 1].y + cond_cdf[i * cdf_count + res - 1].x / res;
463
464                 /* stuff the total into the brightness value for the last entry, because
465                  * we are going to normalize the CDFs to 0.0 to 1.0 afterwards */
466                 cond_cdf[i * cdf_count + res].x = cdf_total;
467
468                 if(cdf_total > 0.0f)
469                         for(int j = 1; j < res; j++)
470                                 cond_cdf[i * cdf_count + j].y /= cdf_total;
471
472                 cond_cdf[i * cdf_count + res].y = 1.0f;
473         }
474
475         /* marginal CDFs (column, V direction, sum of rows) */
476         marg_cdf[0].x = cond_cdf[res].x;
477         marg_cdf[0].y = 0.0f;
478
479         for(int i = 1; i < res; i++) {
480                 marg_cdf[i].x = cond_cdf[i * cdf_count + res].x;
481                 marg_cdf[i].y = marg_cdf[i - 1].y + marg_cdf[i - 1].x / res;
482         }
483
484         float cdf_total = marg_cdf[res - 1].y + marg_cdf[res - 1].x / res;
485         marg_cdf[res].x = cdf_total;
486
487         if(cdf_total > 0.0f)
488                 for(int i = 1; i < res; i++)
489                         marg_cdf[i].y /= cdf_total;
490
491         marg_cdf[res].y = 1.0f;
492
493         /* update device */
494         device->tex_alloc("__light_background_marginal_cdf", dscene->light_background_marginal_cdf);
495         device->tex_alloc("__light_background_conditional_cdf", dscene->light_background_conditional_cdf);
496 }
497
498 void LightManager::device_update_points(Device *device, DeviceScene *dscene, Scene *scene)
499 {
500         if(scene->lights.size() == 0)
501                 return;
502
503         float4 *light_data = dscene->light_data.resize(scene->lights.size()*LIGHT_SIZE);
504
505         if(!device->info.advanced_shading) {
506                 /* remove unsupported light */
507                 foreach(Light *light, scene->lights) {
508                         if(light->type == LIGHT_BACKGROUND) {
509                                 scene->lights.erase(std::remove(scene->lights.begin(), scene->lights.end(), light), scene->lights.end());
510                                 break;
511                         }
512                 }
513         }
514         scene->film->use_light_visibility = false;
515
516         for(size_t i = 0; i < scene->lights.size(); i++) {
517                 Light *light = scene->lights[i];
518                 float3 co = light->co;
519                 float3 dir = normalize(light->dir);
520                 int shader_id = scene->shader_manager->get_shader_id(scene->lights[i]->shader);
521                 float samples = __int_as_float(light->samples);
522
523                 if(!light->cast_shadow)
524                         shader_id &= ~SHADER_CAST_SHADOW;
525
526                 if(!light->use_diffuse) {
527                         shader_id |= SHADER_EXCLUDE_DIFFUSE;
528                         scene->film->use_light_visibility = true;
529                 }
530                 if(!light->use_glossy) {
531                         shader_id |= SHADER_EXCLUDE_GLOSSY;
532                         scene->film->use_light_visibility = true;
533                 }
534                 if(!light->use_transmission) {
535                         shader_id |= SHADER_EXCLUDE_TRANSMIT;
536                         scene->film->use_light_visibility = true;
537                 }
538
539                 if(light->type == LIGHT_POINT) {
540                         shader_id &= ~SHADER_AREA_LIGHT;
541
542                         float radius = light->size;
543                         float invarea = (radius > 0.0f)? 1.0f/(M_PI_F*radius*radius): 1.0f;
544
545                         if(light->use_mis && radius > 0.0f)
546                                 shader_id |= SHADER_USE_MIS;
547
548                         light_data[i*LIGHT_SIZE + 0] = make_float4(__int_as_float(light->type), co.x, co.y, co.z);
549                         light_data[i*LIGHT_SIZE + 1] = make_float4(__int_as_float(shader_id), radius, invarea, 0.0f);
550                         light_data[i*LIGHT_SIZE + 2] = make_float4(0.0f, 0.0f, 0.0f, 0.0f);
551                         light_data[i*LIGHT_SIZE + 3] = make_float4(samples, 0.0f, 0.0f, 0.0f);
552                 }
553                 else if(light->type == LIGHT_DISTANT) {
554                         shader_id &= ~SHADER_AREA_LIGHT;
555
556                         float radius = light->size;
557                         float angle = atanf(radius);
558                         float cosangle = cosf(angle);
559                         float area = M_PI_F*radius*radius;
560                         float invarea = (area > 0.0f)? 1.0f/area: 1.0f;
561
562                         if(light->use_mis && area > 0.0f)
563                                 shader_id |= SHADER_USE_MIS;
564
565                         light_data[i*LIGHT_SIZE + 0] = make_float4(__int_as_float(light->type), dir.x, dir.y, dir.z);
566                         light_data[i*LIGHT_SIZE + 1] = make_float4(__int_as_float(shader_id), radius, cosangle, invarea);
567                         light_data[i*LIGHT_SIZE + 2] = make_float4(0.0f, 0.0f, 0.0f, 0.0f);
568                         light_data[i*LIGHT_SIZE + 3] = make_float4(samples, 0.0f, 0.0f, 0.0f);
569                 }
570                 else if(light->type == LIGHT_BACKGROUND) {
571                         shader_id &= ~SHADER_AREA_LIGHT;
572                         shader_id |= SHADER_USE_MIS;
573
574                         light_data[i*LIGHT_SIZE + 0] = make_float4(__int_as_float(light->type), 0.0f, 0.0f, 0.0f);
575                         light_data[i*LIGHT_SIZE + 1] = make_float4(__int_as_float(shader_id), 0.0f, 0.0f, 0.0f);
576                         light_data[i*LIGHT_SIZE + 2] = make_float4(0.0f, 0.0f, 0.0f, 0.0f);
577                         light_data[i*LIGHT_SIZE + 3] = make_float4(samples, 0.0f, 0.0f, 0.0f);
578                 }
579                 else if(light->type == LIGHT_AREA) {
580                         float3 axisu = light->axisu*(light->sizeu*light->size);
581                         float3 axisv = light->axisv*(light->sizev*light->size);
582                         float area = len(axisu)*len(axisv);
583                         float invarea = (area > 0.0f)? 1.0f/area: 1.0f;
584
585                         if(light->use_mis && area > 0.0f)
586                                 shader_id |= SHADER_USE_MIS;
587
588                         light_data[i*LIGHT_SIZE + 0] = make_float4(__int_as_float(light->type), co.x, co.y, co.z);
589                         light_data[i*LIGHT_SIZE + 1] = make_float4(__int_as_float(shader_id), axisu.x, axisu.y, axisu.z);
590                         light_data[i*LIGHT_SIZE + 2] = make_float4(invarea, axisv.x, axisv.y, axisv.z);
591                         light_data[i*LIGHT_SIZE + 3] = make_float4(samples, dir.x, dir.y, dir.z);
592                 }
593                 else if(light->type == LIGHT_SPOT) {
594                         shader_id &= ~SHADER_AREA_LIGHT;
595
596                         float radius = light->size;
597                         float invarea = (radius > 0.0f)? 1.0f/(M_PI_F*radius*radius): 1.0f;
598                         float spot_angle = cosf(light->spot_angle*0.5f);
599                         float spot_smooth = (1.0f - spot_angle)*light->spot_smooth;
600
601                         if(light->use_mis && radius > 0.0f)
602                                 shader_id |= SHADER_USE_MIS;
603
604                         light_data[i*LIGHT_SIZE + 0] = make_float4(__int_as_float(light->type), co.x, co.y, co.z);
605                         light_data[i*LIGHT_SIZE + 1] = make_float4(__int_as_float(shader_id), radius, invarea, spot_angle);
606                         light_data[i*LIGHT_SIZE + 2] = make_float4(spot_smooth, dir.x, dir.y, dir.z);
607                         light_data[i*LIGHT_SIZE + 3] = make_float4(samples, 0.0f, 0.0f, 0.0f);
608                 }
609         }
610         
611         device->tex_alloc("__light_data", dscene->light_data);
612 }
613
614 void LightManager::device_update(Device *device, DeviceScene *dscene, Scene *scene, Progress& progress)
615 {
616         if(!need_update)
617                 return;
618
619         device_free(device, dscene);
620
621         device_update_points(device, dscene, scene);
622         if(progress.get_cancel()) return;
623
624         device_update_distribution(device, dscene, scene, progress);
625         if(progress.get_cancel()) return;
626
627         device_update_background(device, dscene, scene, progress);
628         if(progress.get_cancel()) return;
629
630         need_update = false;
631 }
632
633 void LightManager::device_free(Device *device, DeviceScene *dscene)
634 {
635         device->tex_free(dscene->light_distribution);
636         device->tex_free(dscene->light_data);
637         device->tex_free(dscene->light_background_marginal_cdf);
638         device->tex_free(dscene->light_background_conditional_cdf);
639
640         dscene->light_distribution.clear();
641         dscene->light_data.clear();
642         dscene->light_background_marginal_cdf.clear();
643         dscene->light_background_conditional_cdf.clear();
644 }
645
646 void LightManager::tag_update(Scene *scene)
647 {
648         need_update = true;
649 }
650
651 CCL_NAMESPACE_END
652