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