Fix Cycles viewport render not updating when tweaking displacement shader.
[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, 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->light_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_mesh = 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