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