Merge branch 'blender2.7'
[blender.git] / intern / cycles / render / film.cpp
1 /*
2  * Copyright 2011-2013 Blender Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include "render/camera.h"
18 #include "device/device.h"
19 #include "render/film.h"
20 #include "render/integrator.h"
21 #include "render/mesh.h"
22 #include "render/scene.h"
23 #include "render/tables.h"
24
25 #include "util/util_algorithm.h"
26 #include "util/util_foreach.h"
27 #include "util/util_math.h"
28 #include "util/util_math_cdf.h"
29
30 CCL_NAMESPACE_BEGIN
31
32 /* Pass */
33
34 static bool compare_pass_order(const Pass& a, const Pass& b)
35 {
36         if(a.components == b.components)
37                 return (a.type < b.type);
38         return (a.components > b.components);
39 }
40
41 void Pass::add(PassType type, vector<Pass>& passes, const char *name)
42 {
43         for(size_t i = 0; i < passes.size(); i++) {
44                 if(passes[i].type == type &&
45                    (name ? (passes[i].name == name) : passes[i].name.empty())) {
46                         return;
47                 }
48         }
49
50         Pass pass;
51
52         pass.type = type;
53         pass.filter = true;
54         pass.exposure = false;
55         pass.divide_type = PASS_NONE;
56         if(name) {
57                 pass.name = name;
58         }
59
60         switch(type) {
61                 case PASS_NONE:
62                         pass.components = 0;
63                         break;
64                 case PASS_COMBINED:
65                         pass.components = 4;
66                         pass.exposure = true;
67                         break;
68                 case PASS_DEPTH:
69                         pass.components = 1;
70                         pass.filter = false;
71                         break;
72                 case PASS_MIST:
73                         pass.components = 1;
74                         break;
75                 case PASS_NORMAL:
76                         pass.components = 4;
77                         break;
78                 case PASS_UV:
79                         pass.components = 4;
80                         break;
81                 case PASS_MOTION:
82                         pass.components = 4;
83                         pass.divide_type = PASS_MOTION_WEIGHT;
84                         break;
85                 case PASS_MOTION_WEIGHT:
86                         pass.components = 1;
87                         break;
88                 case PASS_OBJECT_ID:
89                 case PASS_MATERIAL_ID:
90                         pass.components = 1;
91                         pass.filter = false;
92                         break;
93
94                 case PASS_EMISSION:
95                 case PASS_BACKGROUND:
96                         pass.components = 4;
97                         pass.exposure = true;
98                         break;
99                 case PASS_AO:
100                         pass.components = 4;
101                         break;
102                 case PASS_SHADOW:
103                         pass.components = 4;
104                         pass.exposure = false;
105                         break;
106                 case PASS_LIGHT:
107                         /* This isn't a real pass, used by baking to see whether
108                          * light data is needed or not.
109                          *
110                          * Set components to 0 so pass sort below happens in a
111                          * determined way.
112                          */
113                         pass.components = 0;
114                         break;
115 #ifdef WITH_CYCLES_DEBUG
116                 case PASS_BVH_TRAVERSED_NODES:
117                 case PASS_BVH_TRAVERSED_INSTANCES:
118                 case PASS_BVH_INTERSECTIONS:
119                 case PASS_RAY_BOUNCES:
120                         pass.components = 1;
121                         pass.exposure = false;
122                         break;
123 #endif
124                 case PASS_RENDER_TIME:
125                         /* This pass is handled entirely on the host side. */
126                         pass.components = 0;
127                         break;
128
129                 case PASS_DIFFUSE_COLOR:
130                 case PASS_GLOSSY_COLOR:
131                 case PASS_TRANSMISSION_COLOR:
132                 case PASS_SUBSURFACE_COLOR:
133                         pass.components = 4;
134                         break;
135                 case PASS_DIFFUSE_DIRECT:
136                 case PASS_DIFFUSE_INDIRECT:
137                         pass.components = 4;
138                         pass.exposure = true;
139                         pass.divide_type = PASS_DIFFUSE_COLOR;
140                         break;
141                 case PASS_GLOSSY_DIRECT:
142                 case PASS_GLOSSY_INDIRECT:
143                         pass.components = 4;
144                         pass.exposure = true;
145                         pass.divide_type = PASS_GLOSSY_COLOR;
146                         break;
147                 case PASS_TRANSMISSION_DIRECT:
148                 case PASS_TRANSMISSION_INDIRECT:
149                         pass.components = 4;
150                         pass.exposure = true;
151                         pass.divide_type = PASS_TRANSMISSION_COLOR;
152                         break;
153                 case PASS_SUBSURFACE_DIRECT:
154                 case PASS_SUBSURFACE_INDIRECT:
155                         pass.components = 4;
156                         pass.exposure = true;
157                         pass.divide_type = PASS_SUBSURFACE_COLOR;
158                         break;
159                 case PASS_VOLUME_DIRECT:
160                 case PASS_VOLUME_INDIRECT:
161                         pass.components = 4;
162                         pass.exposure = true;
163                         break;
164                 case PASS_CRYPTOMATTE:
165                         pass.components = 4;
166                         break;
167                 default:
168                         assert(false);
169                         break;
170         }
171
172         passes.push_back(pass);
173
174         /* order from by components, to ensure alignment so passes with size 4
175          * come first and then passes with size 1 */
176         sort(&passes[0], &passes[0] + passes.size(), compare_pass_order);
177
178         if(pass.divide_type != PASS_NONE)
179                 Pass::add(pass.divide_type, passes);
180 }
181
182 bool Pass::equals(const vector<Pass>& A, const vector<Pass>& B)
183 {
184         if(A.size() != B.size())
185                 return false;
186
187         for(int i = 0; i < A.size(); i++)
188                 if(A[i].type != B[i].type || A[i].name != B[i].name)
189                         return false;
190
191         return true;
192 }
193
194 bool Pass::contains(const vector<Pass>& passes, PassType type)
195 {
196         for(size_t i = 0; i < passes.size(); i++)
197                 if(passes[i].type == type)
198                         return true;
199
200         return false;
201 }
202
203 /* Pixel Filter */
204
205 static float filter_func_box(float /*v*/, float /*width*/)
206 {
207         return 1.0f;
208 }
209
210 static float filter_func_gaussian(float v, float width)
211 {
212         v *= 6.0f/width;
213         return expf(-2.0f*v*v);
214 }
215
216 static float filter_func_blackman_harris(float v, float width)
217 {
218         v = M_2PI_F * (v / width + 0.5f);
219         return 0.35875f - 0.48829f*cosf(v) + 0.14128f*cosf(2.0f*v) - 0.01168f*cosf(3.0f*v);
220 }
221
222 static vector<float> filter_table(FilterType type, float width)
223 {
224         vector<float> filter_table(FILTER_TABLE_SIZE);
225         float (*filter_func)(float, float) = NULL;
226
227         switch(type) {
228                 case FILTER_BOX:
229                         filter_func = filter_func_box;
230                         break;
231                 case FILTER_GAUSSIAN:
232                         filter_func = filter_func_gaussian;
233                         width *= 3.0f;
234                         break;
235                 case FILTER_BLACKMAN_HARRIS:
236                         filter_func = filter_func_blackman_harris;
237                         width *= 2.0f;
238                         break;
239                 default:
240                         assert(0);
241         }
242
243         /* Create importance sampling table. */
244
245         /* TODO(sergey): With the even filter table size resolution we can not
246          * really make it nice symmetric importance map without sampling full range
247          * (meaning, we would need to sample full filter range and not use the
248          * make_symmetric argument).
249          *
250          * Current code matches exactly initial filter table code, but we should
251          * consider either making FILTER_TABLE_SIZE odd value or sample full filter.
252          */
253
254         util_cdf_inverted(FILTER_TABLE_SIZE,
255                           0.0f,
256                           width * 0.5f,
257                           function_bind(filter_func, _1, width),
258                           true,
259                           filter_table);
260
261         return filter_table;
262 }
263
264 /* Film */
265
266 NODE_DEFINE(Film)
267 {
268         NodeType* type = NodeType::add("film", create);
269
270         SOCKET_FLOAT(exposure, "Exposure", 0.8f);
271         SOCKET_FLOAT(pass_alpha_threshold, "Pass Alpha Threshold", 0.5f);
272
273         static NodeEnum filter_enum;
274         filter_enum.insert("box", FILTER_BOX);
275         filter_enum.insert("gaussian", FILTER_GAUSSIAN);
276         filter_enum.insert("blackman_harris", FILTER_BLACKMAN_HARRIS);
277
278         SOCKET_ENUM(filter_type, "Filter Type", filter_enum, FILTER_BOX);
279         SOCKET_FLOAT(filter_width, "Filter Width", 1.0f);
280
281         SOCKET_FLOAT(mist_start, "Mist Start", 0.0f);
282         SOCKET_FLOAT(mist_depth, "Mist Depth", 100.0f);
283         SOCKET_FLOAT(mist_falloff, "Mist Falloff", 1.0f);
284
285         SOCKET_BOOLEAN(use_sample_clamp, "Use Sample Clamp", false);
286
287         SOCKET_BOOLEAN(denoising_data_pass,  "Generate Denoising Data Pass",  false);
288         SOCKET_BOOLEAN(denoising_clean_pass, "Generate Denoising Clean Pass", false);
289         SOCKET_BOOLEAN(denoising_prefiltered_pass, "Generate Denoising Prefiltered Pass", false);
290         SOCKET_INT(denoising_flags, "Denoising Flags", 0);
291
292         return type;
293 }
294
295 Film::Film()
296 : Node(node_type)
297 {
298         Pass::add(PASS_COMBINED, passes);
299
300         use_light_visibility = false;
301         filter_table_offset = TABLE_OFFSET_INVALID;
302         cryptomatte_passes = CRYPT_NONE;
303
304         need_update = true;
305 }
306
307 Film::~Film()
308 {
309 }
310
311 void Film::device_update(Device *device, DeviceScene *dscene, Scene *scene)
312 {
313         if(!need_update)
314                 return;
315
316         device_free(device, dscene, scene);
317
318         KernelFilm *kfilm = &dscene->data.film;
319
320         /* update __data */
321         kfilm->exposure = exposure;
322         kfilm->pass_flag = 0;
323         kfilm->light_pass_flag = 0;
324         kfilm->pass_stride = 0;
325         kfilm->use_light_pass = use_light_visibility || use_sample_clamp;
326
327         bool have_cryptomatte = false;
328
329         for(size_t i = 0; i < passes.size(); i++) {
330                 Pass& pass = passes[i];
331
332                 if(pass.type == PASS_NONE) {
333                         continue;
334                 }
335
336                 /* Can't do motion pass if no motion vectors are available. */
337                 if (pass.type == PASS_MOTION || pass.type == PASS_MOTION_WEIGHT) {
338                         if (scene->need_motion() != Scene::MOTION_PASS) {
339                                 kfilm->pass_stride += pass.components;
340                                 continue;
341                         }
342                 }
343
344                 int pass_flag = (1 << (pass.type % 32));
345                 if(pass.type <= PASS_CATEGORY_MAIN_END) {
346                         kfilm->pass_flag |= pass_flag;
347                 }
348                 else {
349                         assert(pass.type <= PASS_CATEGORY_LIGHT_END);
350                         kfilm->use_light_pass = 1;
351                         kfilm->light_pass_flag |= pass_flag;
352                 }
353
354                 switch(pass.type) {
355                         case PASS_COMBINED:
356                                 kfilm->pass_combined = kfilm->pass_stride;
357                                 break;
358                         case PASS_DEPTH:
359                                 kfilm->pass_depth = kfilm->pass_stride;
360                                 break;
361                         case PASS_NORMAL:
362                                 kfilm->pass_normal = kfilm->pass_stride;
363                                 break;
364                         case PASS_UV:
365                                 kfilm->pass_uv = kfilm->pass_stride;
366                                 break;
367                         case PASS_MOTION:
368                                 kfilm->pass_motion = kfilm->pass_stride;
369                                 break;
370                         case PASS_MOTION_WEIGHT:
371                                 kfilm->pass_motion_weight = kfilm->pass_stride;
372                                 break;
373                         case PASS_OBJECT_ID:
374                                 kfilm->pass_object_id = kfilm->pass_stride;
375                                 break;
376                         case PASS_MATERIAL_ID:
377                                 kfilm->pass_material_id = kfilm->pass_stride;
378                                 break;
379
380                         case PASS_MIST:
381                                 kfilm->pass_mist = kfilm->pass_stride;
382                                 break;
383                         case PASS_EMISSION:
384                                 kfilm->pass_emission = kfilm->pass_stride;
385                                 break;
386                         case PASS_BACKGROUND:
387                                 kfilm->pass_background = kfilm->pass_stride;
388                                 break;
389                         case PASS_AO:
390                                 kfilm->pass_ao = kfilm->pass_stride;
391                                 break;
392                         case PASS_SHADOW:
393                                 kfilm->pass_shadow = kfilm->pass_stride;
394                                 break;
395
396                         case PASS_LIGHT:
397                                 break;
398
399                         case PASS_DIFFUSE_COLOR:
400                                 kfilm->pass_diffuse_color = kfilm->pass_stride;
401                                 break;
402                         case PASS_GLOSSY_COLOR:
403                                 kfilm->pass_glossy_color = kfilm->pass_stride;
404                                 break;
405                         case PASS_TRANSMISSION_COLOR:
406                                 kfilm->pass_transmission_color = kfilm->pass_stride;
407                                 break;
408                         case PASS_SUBSURFACE_COLOR:
409                                 kfilm->pass_subsurface_color = kfilm->pass_stride;
410                                 break;
411                         case PASS_DIFFUSE_INDIRECT:
412                                 kfilm->pass_diffuse_indirect = kfilm->pass_stride;
413                                 break;
414                         case PASS_GLOSSY_INDIRECT:
415                                 kfilm->pass_glossy_indirect = kfilm->pass_stride;
416                                 break;
417                         case PASS_TRANSMISSION_INDIRECT:
418                                 kfilm->pass_transmission_indirect = kfilm->pass_stride;
419                                 break;
420                         case PASS_SUBSURFACE_INDIRECT:
421                                 kfilm->pass_subsurface_indirect = kfilm->pass_stride;
422                                 break;
423                         case PASS_VOLUME_INDIRECT:
424                                 kfilm->pass_volume_indirect = kfilm->pass_stride;
425                                 break;
426                         case PASS_DIFFUSE_DIRECT:
427                                 kfilm->pass_diffuse_direct = kfilm->pass_stride;
428                                 break;
429                         case PASS_GLOSSY_DIRECT:
430                                 kfilm->pass_glossy_direct = kfilm->pass_stride;
431                                 break;
432                         case PASS_TRANSMISSION_DIRECT:
433                                 kfilm->pass_transmission_direct = kfilm->pass_stride;
434                                 break;
435                         case PASS_SUBSURFACE_DIRECT:
436                                 kfilm->pass_subsurface_direct = kfilm->pass_stride;
437                                 break;
438                         case PASS_VOLUME_DIRECT:
439                                 kfilm->pass_volume_direct = kfilm->pass_stride;
440                                 break;
441
442 #ifdef WITH_CYCLES_DEBUG
443                         case PASS_BVH_TRAVERSED_NODES:
444                                 kfilm->pass_bvh_traversed_nodes = kfilm->pass_stride;
445                                 break;
446                         case PASS_BVH_TRAVERSED_INSTANCES:
447                                 kfilm->pass_bvh_traversed_instances = kfilm->pass_stride;
448                                 break;
449                         case PASS_BVH_INTERSECTIONS:
450                                 kfilm->pass_bvh_intersections = kfilm->pass_stride;
451                                 break;
452                         case PASS_RAY_BOUNCES:
453                                 kfilm->pass_ray_bounces = kfilm->pass_stride;
454                                 break;
455 #endif
456                         case PASS_RENDER_TIME:
457                                 break;
458                         case PASS_CRYPTOMATTE:
459                                 kfilm->pass_cryptomatte = have_cryptomatte ? min(kfilm->pass_cryptomatte, kfilm->pass_stride) : kfilm->pass_stride;
460                                 have_cryptomatte = true;
461                                 break;
462                         default:
463                                 assert(false);
464                                 break;
465                 }
466
467                 kfilm->pass_stride += pass.components;
468         }
469
470         kfilm->pass_denoising_data = 0;
471         kfilm->pass_denoising_clean = 0;
472         kfilm->denoising_flags = 0;
473         if(denoising_data_pass) {
474                 kfilm->pass_denoising_data = kfilm->pass_stride;
475                 kfilm->pass_stride += DENOISING_PASS_SIZE_BASE;
476                 kfilm->denoising_flags = denoising_flags;
477                 if(denoising_clean_pass) {
478                         kfilm->pass_denoising_clean = kfilm->pass_stride;
479                         kfilm->pass_stride += DENOISING_PASS_SIZE_CLEAN;
480                         kfilm->use_light_pass = 1;
481                 }
482                 if(denoising_prefiltered_pass) {
483                         kfilm->pass_stride += DENOISING_PASS_SIZE_PREFILTERED;
484                 }
485         }
486
487         kfilm->pass_stride = align_up(kfilm->pass_stride, 4);
488         kfilm->pass_alpha_threshold = pass_alpha_threshold;
489
490         /* update filter table */
491         vector<float> table = filter_table(filter_type, filter_width);
492         scene->lookup_tables->remove_table(&filter_table_offset);
493         filter_table_offset = scene->lookup_tables->add_table(dscene, table);
494         kfilm->filter_table_offset = (int)filter_table_offset;
495
496         /* mist pass parameters */
497         kfilm->mist_start = mist_start;
498         kfilm->mist_inv_depth = (mist_depth > 0.0f)? 1.0f/mist_depth: 0.0f;
499         kfilm->mist_falloff = mist_falloff;
500
501         kfilm->cryptomatte_passes = cryptomatte_passes;
502         kfilm->cryptomatte_depth = cryptomatte_depth;
503
504         pass_stride = kfilm->pass_stride;
505         denoising_data_offset = kfilm->pass_denoising_data;
506         denoising_clean_offset = kfilm->pass_denoising_clean;
507
508         need_update = false;
509 }
510
511 void Film::device_free(Device * /*device*/,
512                        DeviceScene * /*dscene*/,
513                        Scene *scene)
514 {
515         scene->lookup_tables->remove_table(&filter_table_offset);
516 }
517
518 bool Film::modified(const Film& film)
519 {
520         return !Node::equals(film) || !Pass::equals(passes, film.passes);
521 }
522
523 void Film::tag_passes_update(Scene *scene, const vector<Pass>& passes_)
524 {
525         if(Pass::contains(passes, PASS_UV) != Pass::contains(passes_, PASS_UV)) {
526                 scene->mesh_manager->tag_update(scene);
527
528                 foreach(Shader *shader, scene->shaders)
529                         shader->need_update_mesh = true;
530         }
531         else if(Pass::contains(passes, PASS_MOTION) != Pass::contains(passes_, PASS_MOTION))
532                 scene->mesh_manager->tag_update(scene);
533
534         passes = passes_;
535 }
536
537 void Film::tag_update(Scene * /*scene*/)
538 {
539         need_update = true;
540 }
541
542 CCL_NAMESPACE_END