e7fb9514371a48b3f46d222e79e13bf7e006b530
[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_triangles = 0;
147         size_t num_curve_segments = 0;
148
149         foreach(Object *object, scene->objects) {
150                 Mesh *mesh = object->mesh;
151                 bool have_emission = false;
152
153                 /* skip if we are not visible for BSDFs */
154                 if(!(object->visibility & (PATH_RAY_DIFFUSE|PATH_RAY_GLOSSY|PATH_RAY_TRANSMIT)))
155                         continue;
156
157                 /* skip if we have no emission shaders */
158                 foreach(uint sindex, mesh->used_shaders) {
159                         Shader *shader = scene->shaders[sindex];
160
161                         if(shader->sample_as_light && shader->has_surface_emission) {
162                                 have_emission = true;
163                                 break;
164                         }
165                 }
166
167                 /* count triangles */
168                 if(have_emission) {
169                         for(size_t i = 0; i < mesh->triangles.size(); i++) {
170                                 Shader *shader = scene->shaders[mesh->shader[i]];
171
172                                 if(shader->sample_as_light && shader->has_surface_emission)
173                                         num_triangles++;
174                         }
175
176                         /* disabled for curves */
177 #if 0
178                         foreach(Mesh::Curve& curve, mesh->curves) {
179                                 Shader *shader = scene->shaders[curve.shader];
180
181                                 if(shader->sample_as_light && shader->has_surface_emission)
182                                         num_curve_segments += curve.num_segments();
183 #endif
184                 }
185         }
186
187         size_t num_distribution = num_triangles + num_curve_segments;
188         num_distribution += num_lights;
189
190         /* emission area */
191         float4 *distribution = dscene->light_distribution.resize(num_distribution + 1);
192         float totarea = 0.0f;
193
194         /* triangles */
195         size_t offset = 0;
196         int j = 0;
197
198         foreach(Object *object, scene->objects) {
199                 Mesh *mesh = object->mesh;
200                 bool have_emission = false;
201
202                 /* skip if we are not visible for BSDFs */
203                 if(!(object->visibility & (PATH_RAY_DIFFUSE|PATH_RAY_GLOSSY|PATH_RAY_TRANSMIT))) {
204                         j++;
205                         continue;
206                 }
207
208                 /* skip if we have no emission shaders */
209                 foreach(uint sindex, mesh->used_shaders) {
210                         Shader *shader = scene->shaders[sindex];
211
212                         if(shader->sample_as_light && shader->has_surface_emission) {
213                                 have_emission = true;
214                                 break;
215                         }
216                 }
217
218                 /* sum area */
219                 if(have_emission) {
220                         bool transform_applied = mesh->transform_applied;
221                         Transform tfm = object->tfm;
222                         int object_id = j;
223
224                         if(transform_applied)
225                                 object_id = ~object_id;
226
227                         for(size_t i = 0; i < mesh->triangles.size(); i++) {
228                                 Shader *shader = scene->shaders[mesh->shader[i]];
229
230                                 if(shader->sample_as_light && shader->has_surface_emission) {
231                                         distribution[offset].x = totarea;
232                                         distribution[offset].y = __int_as_float(i + mesh->tri_offset);
233                                         distribution[offset].z = __int_as_float(~0);
234                                         distribution[offset].w = __int_as_float(object_id);
235                                         offset++;
236
237                                         Mesh::Triangle t = mesh->triangles[i];
238                                         float3 p1 = mesh->verts[t.v[0]];
239                                         float3 p2 = mesh->verts[t.v[1]];
240                                         float3 p3 = mesh->verts[t.v[2]];
241
242                                         if(!transform_applied) {
243                                                 p1 = transform_point(&tfm, p1);
244                                                 p2 = transform_point(&tfm, p2);
245                                                 p3 = transform_point(&tfm, p3);
246                                         }
247
248                                         totarea += triangle_area(p1, p2, p3);
249                                 }
250                         }
251
252                         /*sample as light disabled for strands*/
253 #if 0
254                         size_t i = 0;
255
256                         foreach(Mesh::Curve& curve, mesh->curves) {
257                                 Shader *shader = scene->shaders[curve.shader];
258                                 int first_key = curve.first_key;
259
260                                 if(shader->sample_as_light && shader->has_surface_emission) {
261                                         for(int j = 0; j < curve.num_segments(); j++) {
262                                                 distribution[offset].x = totarea;
263                                                 distribution[offset].y = __int_as_float(i + mesh->curve_offset); // XXX fix kernel code
264                                                 distribution[offset].z = __int_as_float(j);
265                                                 distribution[offset].w = __int_as_float(object_id);
266                                                 offset++;
267                                 
268                                                 float3 p1 = mesh->curve_keys[first_key + j].loc;
269                                                 float r1 = mesh->curve_keys[first_key + j].radius;
270                                                 float3 p2 = mesh->curve_keys[first_key + j + 1].loc;
271                                                 float r2 = mesh->curve_keys[first_key + j + 1].radius;
272                                 
273                                                 if(!transform_applied) {
274                                                         p1 = transform_point(&tfm, p1);
275                                                         p2 = transform_point(&tfm, p2);
276                                                 }
277                                 
278                                                 totarea += M_PI_F * (r1 + r2) * len(p1 - p2);
279                                         }
280                                 }
281
282                                 i++;
283                         }
284 #endif
285                 }
286
287                 if(progress.get_cancel()) return;
288
289                 j++;
290         }
291
292         float trianglearea = totarea;
293
294         /* point lights */
295         float lightarea = (totarea > 0.0f)? totarea/scene->lights.size(): 1.0f;
296         bool use_lamp_mis = false;
297
298         for(int i = 0; i < scene->lights.size(); i++, offset++) {
299                 Light *light = scene->lights[i];
300
301                 distribution[offset].x = totarea;
302                 distribution[offset].y = __int_as_float(~(int)i);
303                 distribution[offset].z = 1.0f;
304                 distribution[offset].w = light->size;
305                 totarea += lightarea;
306
307                 if(light->size > 0.0f && light->use_mis)
308                         use_lamp_mis = true;
309         }
310
311         /* normalize cumulative distribution functions */
312         distribution[num_distribution].x = totarea;
313         distribution[num_distribution].y = 0.0f;
314         distribution[num_distribution].z = 0.0f;
315         distribution[num_distribution].w = 0.0f;
316
317         if(totarea > 0.0f) {
318                 for(size_t i = 0; i < num_distribution; i++)
319                         distribution[i].x /= totarea;
320                 distribution[num_distribution].x = 1.0f;
321         }
322
323         if(progress.get_cancel()) return;
324
325         /* update device */
326         KernelIntegrator *kintegrator = &dscene->data.integrator;
327         kintegrator->use_direct_light = (totarea > 0.0f);
328
329         if(kintegrator->use_direct_light) {
330                 /* number of emissives */
331                 kintegrator->num_distribution = (totarea > 0.0f)? num_distribution: 0;
332
333                 /* precompute pdfs */
334                 kintegrator->pdf_triangles = 0.0f;
335                 kintegrator->pdf_lights = 0.0f;
336                 kintegrator->inv_pdf_lights = 0.0f;
337
338                 /* sample one, with 0.5 probability of light or triangle */
339                 kintegrator->num_all_lights = num_lights;
340
341                 if(trianglearea > 0.0f) {
342                         kintegrator->pdf_triangles = 1.0f/trianglearea;
343                         if(num_lights)
344                                 kintegrator->pdf_triangles *= 0.5f;
345                 }
346
347                 if(num_lights) {
348                         kintegrator->pdf_lights = 1.0f/num_lights;
349                         if(trianglearea > 0.0f)
350                                 kintegrator->pdf_lights *= 0.5f;
351
352                         kintegrator->inv_pdf_lights = 1.0f/kintegrator->pdf_lights;
353                 }
354
355                 kintegrator->use_lamp_mis = use_lamp_mis;
356
357                 /* CDF */
358                 device->tex_alloc("__light_distribution", dscene->light_distribution);
359         }
360         else {
361                 dscene->light_distribution.clear();
362
363                 kintegrator->num_distribution = 0;
364                 kintegrator->num_all_lights = 0;
365                 kintegrator->pdf_triangles = 0.0f;
366                 kintegrator->pdf_lights = 0.0f;
367                 kintegrator->inv_pdf_lights = 0.0f;
368                 kintegrator->use_lamp_mis = false;
369         }
370 }
371
372 void LightManager::device_update_background(Device *device, DeviceScene *dscene, Scene *scene, Progress& progress)
373 {
374         KernelIntegrator *kintegrator = &dscene->data.integrator;
375         Light *background_light = NULL;
376
377         /* find background light */
378         foreach(Light *light, scene->lights) {
379                 if(light->type == LIGHT_BACKGROUND) {
380                         background_light = light;
381                         break;
382                 }
383         }
384
385         /* no background light found, signal renderer to skip sampling */
386         if(!background_light) {
387                 kintegrator->pdf_background_res = 0;
388                 return;
389         }
390
391         progress.set_status("Updating Lights", "Importance map");
392
393         assert(kintegrator->use_direct_light);
394
395         /* get the resolution from the light's size (we stuff it in there) */
396         int res = background_light->map_resolution;
397         kintegrator->pdf_background_res = res;
398
399         assert(res > 0);
400
401         vector<float3> pixels;
402         dump_background_pixels(device, dscene, res, pixels);
403
404         if(progress.get_cancel())
405                 return;
406
407         /* build row distributions and column distribution for the infinite area environment light */
408         int cdf_count = res + 1;
409         float2 *marg_cdf = dscene->light_background_marginal_cdf.resize(cdf_count);
410         float2 *cond_cdf = dscene->light_background_conditional_cdf.resize(cdf_count * cdf_count);
411
412         /* conditional CDFs (rows, U direction) */
413         for(int i = 0; i < res; i++) {
414                 float sin_theta = sinf(M_PI_F * (i + 0.5f) / res);
415                 float3 env_color = pixels[i * res];
416                 float ave_luminamce = average(env_color);
417
418                 cond_cdf[i * cdf_count].x = ave_luminamce * sin_theta;
419                 cond_cdf[i * cdf_count].y = 0.0f;
420
421                 for(int j = 1; j < res; j++) {
422                         env_color = pixels[i * res + j];
423                         ave_luminamce = average(env_color);
424
425                         cond_cdf[i * cdf_count + j].x = ave_luminamce * sin_theta;
426                         cond_cdf[i * cdf_count + j].y = cond_cdf[i * cdf_count + j - 1].y + cond_cdf[i * cdf_count + j - 1].x / res;
427                 }
428
429                 float cdf_total = cond_cdf[i * cdf_count + res - 1].y + cond_cdf[i * cdf_count + res - 1].x / res;
430
431                 /* stuff the total into the brightness value for the last entry, because
432                  * we are going to normalize the CDFs to 0.0 to 1.0 afterwards */
433                 cond_cdf[i * cdf_count + res].x = cdf_total;
434
435                 if(cdf_total > 0.0f)
436                         for(int j = 1; j < res; j++)
437                                 cond_cdf[i * cdf_count + j].y /= cdf_total;
438
439                 cond_cdf[i * cdf_count + res].y = 1.0f;
440         }
441
442         /* marginal CDFs (column, V direction, sum of rows) */
443         marg_cdf[0].x = cond_cdf[res].x;
444         marg_cdf[0].y = 0.0f;
445
446         for(int i = 1; i < res; i++) {
447                 marg_cdf[i].x = cond_cdf[i * cdf_count + res].x;
448                 marg_cdf[i].y = marg_cdf[i - 1].y + marg_cdf[i - 1].x / res;
449         }
450
451         float cdf_total = marg_cdf[res - 1].y + marg_cdf[res - 1].x / res;
452         marg_cdf[res].x = cdf_total;
453
454         if(cdf_total > 0.0f)
455                 for(int i = 1; i < res; i++)
456                         marg_cdf[i].y /= cdf_total;
457
458         marg_cdf[res].y = 1.0f;
459
460         /* update device */
461         device->tex_alloc("__light_background_marginal_cdf", dscene->light_background_marginal_cdf);
462         device->tex_alloc("__light_background_conditional_cdf", dscene->light_background_conditional_cdf);
463 }
464
465 void LightManager::device_update_points(Device *device, DeviceScene *dscene, Scene *scene)
466 {
467         if(scene->lights.size() == 0)
468                 return;
469
470         float4 *light_data = dscene->light_data.resize(scene->lights.size()*LIGHT_SIZE);
471
472         if(!device->info.advanced_shading) {
473                 /* remove unsupported light */
474                 foreach(Light *light, scene->lights) {
475                         if(light->type == LIGHT_BACKGROUND) {
476                                 scene->lights.erase(std::remove(scene->lights.begin(), scene->lights.end(), light), scene->lights.end());
477                                 break;
478                         }
479                 }
480         }
481
482         for(size_t i = 0; i < scene->lights.size(); i++) {
483                 Light *light = scene->lights[i];
484                 float3 co = light->co;
485                 float3 dir = normalize(light->dir);
486                 int shader_id = scene->shader_manager->get_shader_id(scene->lights[i]->shader);
487                 float samples = __int_as_float(light->samples);
488
489                 if(!light->cast_shadow)
490                         shader_id &= ~SHADER_CAST_SHADOW;
491
492                 if(light->type == LIGHT_POINT) {
493                         shader_id &= ~SHADER_AREA_LIGHT;
494
495                         float radius = light->size;
496                         float invarea = (radius > 0.0f)? 1.0f/(M_PI_F*radius*radius): 1.0f;
497
498                         if(light->use_mis && radius > 0.0f)
499                                 shader_id |= SHADER_USE_MIS;
500
501                         light_data[i*LIGHT_SIZE + 0] = make_float4(__int_as_float(light->type), co.x, co.y, co.z);
502                         light_data[i*LIGHT_SIZE + 1] = make_float4(__int_as_float(shader_id), radius, invarea, 0.0f);
503                         light_data[i*LIGHT_SIZE + 2] = make_float4(0.0f, 0.0f, 0.0f, 0.0f);
504                         light_data[i*LIGHT_SIZE + 3] = make_float4(samples, 0.0f, 0.0f, 0.0f);
505                 }
506                 else if(light->type == LIGHT_DISTANT) {
507                         shader_id &= ~SHADER_AREA_LIGHT;
508
509                         float radius = light->size;
510                         float angle = atanf(radius);
511                         float cosangle = cosf(angle);
512                         float area = M_PI_F*radius*radius;
513                         float invarea = (area > 0.0f)? 1.0f/area: 1.0f;
514
515                         if(light->use_mis && area > 0.0f)
516                                 shader_id |= SHADER_USE_MIS;
517
518                         light_data[i*LIGHT_SIZE + 0] = make_float4(__int_as_float(light->type), dir.x, dir.y, dir.z);
519                         light_data[i*LIGHT_SIZE + 1] = make_float4(__int_as_float(shader_id), radius, cosangle, invarea);
520                         light_data[i*LIGHT_SIZE + 2] = make_float4(0.0f, 0.0f, 0.0f, 0.0f);
521                         light_data[i*LIGHT_SIZE + 3] = make_float4(samples, 0.0f, 0.0f, 0.0f);
522                 }
523                 else if(light->type == LIGHT_BACKGROUND) {
524                         shader_id &= ~SHADER_AREA_LIGHT;
525                         shader_id |= SHADER_USE_MIS;
526
527                         light_data[i*LIGHT_SIZE + 0] = make_float4(__int_as_float(light->type), 0.0f, 0.0f, 0.0f);
528                         light_data[i*LIGHT_SIZE + 1] = make_float4(__int_as_float(shader_id), 0.0f, 0.0f, 0.0f);
529                         light_data[i*LIGHT_SIZE + 2] = make_float4(0.0f, 0.0f, 0.0f, 0.0f);
530                         light_data[i*LIGHT_SIZE + 3] = make_float4(samples, 0.0f, 0.0f, 0.0f);
531                 }
532                 else if(light->type == LIGHT_AREA) {
533                         float3 axisu = light->axisu*(light->sizeu*light->size);
534                         float3 axisv = light->axisv*(light->sizev*light->size);
535                         float area = len(axisu)*len(axisv);
536                         float invarea = (area > 0.0f)? 1.0f/area: 1.0f;
537
538                         if(light->use_mis && area > 0.0f)
539                                 shader_id |= SHADER_USE_MIS;
540
541                         light_data[i*LIGHT_SIZE + 0] = make_float4(__int_as_float(light->type), co.x, co.y, co.z);
542                         light_data[i*LIGHT_SIZE + 1] = make_float4(__int_as_float(shader_id), axisu.x, axisu.y, axisu.z);
543                         light_data[i*LIGHT_SIZE + 2] = make_float4(invarea, axisv.x, axisv.y, axisv.z);
544                         light_data[i*LIGHT_SIZE + 3] = make_float4(samples, dir.x, dir.y, dir.z);
545                 }
546                 else if(light->type == LIGHT_SPOT) {
547                         shader_id &= ~SHADER_AREA_LIGHT;
548
549                         float radius = light->size;
550                         float invarea = (radius > 0.0f)? 1.0f/(M_PI_F*radius*radius): 1.0f;
551                         float spot_angle = cosf(light->spot_angle*0.5f);
552                         float spot_smooth = (1.0f - spot_angle)*light->spot_smooth;
553
554                         if(light->use_mis && radius > 0.0f)
555                                 shader_id |= SHADER_USE_MIS;
556
557                         light_data[i*LIGHT_SIZE + 0] = make_float4(__int_as_float(light->type), co.x, co.y, co.z);
558                         light_data[i*LIGHT_SIZE + 1] = make_float4(__int_as_float(shader_id), radius, invarea, spot_angle);
559                         light_data[i*LIGHT_SIZE + 2] = make_float4(spot_smooth, dir.x, dir.y, dir.z);
560                         light_data[i*LIGHT_SIZE + 3] = make_float4(samples, 0.0f, 0.0f, 0.0f);
561                 }
562         }
563         
564         device->tex_alloc("__light_data", dscene->light_data);
565 }
566
567 void LightManager::device_update(Device *device, DeviceScene *dscene, Scene *scene, Progress& progress)
568 {
569         if(!need_update)
570                 return;
571
572         device_free(device, dscene);
573
574         device_update_points(device, dscene, scene);
575         if(progress.get_cancel()) return;
576
577         device_update_distribution(device, dscene, scene, progress);
578         if(progress.get_cancel()) return;
579
580         device_update_background(device, dscene, scene, progress);
581         if(progress.get_cancel()) return;
582
583         need_update = false;
584 }
585
586 void LightManager::device_free(Device *device, DeviceScene *dscene)
587 {
588         device->tex_free(dscene->light_distribution);
589         device->tex_free(dscene->light_data);
590         device->tex_free(dscene->light_background_marginal_cdf);
591         device->tex_free(dscene->light_background_conditional_cdf);
592
593         dscene->light_distribution.clear();
594         dscene->light_data.clear();
595         dscene->light_background_marginal_cdf.clear();
596         dscene->light_background_conditional_cdf.clear();
597 }
598
599 void LightManager::tag_update(Scene *scene)
600 {
601         need_update = true;
602 }
603
604 CCL_NAMESPACE_END
605