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