Cycles / SSS:
[blender.git] / intern / cycles / render / film.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 "camera.h"
20 #include "device.h"
21 #include "film.h"
22 #include "integrator.h"
23 #include "mesh.h"
24 #include "scene.h"
25 #include "tables.h"
26
27 #include "util_algorithm.h"
28 #include "util_debug.h"
29 #include "util_foreach.h"
30 #include "util_math.h"
31
32 CCL_NAMESPACE_BEGIN
33
34 /* Pass */
35
36 static bool compare_pass_order(const Pass& a, const Pass& b)
37 {
38         if(a.components == b.components)
39                 return (a.type < b.type);
40         return (a.components > b.components);
41 }
42
43 void Pass::add(PassType type, vector<Pass>& passes)
44 {
45         foreach(Pass& existing_pass, passes)
46                 if(existing_pass.type == type)
47                         return;
48
49         Pass pass;
50
51         pass.type = type;
52         pass.filter = true;
53         pass.exposure = false;
54         pass.divide_type = PASS_NONE;
55
56         switch(type) {
57                 case PASS_NONE:
58                         pass.components = 0;
59                         break;
60                 case PASS_COMBINED:
61                         pass.components = 4;
62                         pass.exposure = true;
63                         break;
64                 case PASS_DEPTH:
65                         pass.components = 1;
66                         pass.filter = false;
67                         break;
68                 case PASS_MIST:
69                         pass.components = 1;
70                         break;
71                 case PASS_NORMAL:
72                         pass.components = 4;
73                         break;
74                 case PASS_UV:
75                         pass.components = 4;
76                         break;
77                 case PASS_MOTION:
78                         pass.components = 4;
79                         pass.divide_type = PASS_MOTION_WEIGHT;
80                         break;
81                 case PASS_MOTION_WEIGHT:
82                         pass.components = 1;
83                         break;
84                 case PASS_OBJECT_ID:
85                         pass.components = 1;
86                         pass.filter = false;
87                         break;
88                 case PASS_MATERIAL_ID:
89                         pass.components = 1;
90                         pass.filter = false;
91                         break;
92                 case PASS_DIFFUSE_COLOR:
93                         pass.components = 4;
94                         break;
95                 case PASS_GLOSSY_COLOR:
96                         pass.components = 4;
97                         break;
98                 case PASS_TRANSMISSION_COLOR:
99                         pass.components = 4;
100                         break;
101                 case PASS_SUBSURFACE_COLOR:
102                         pass.components = 4;
103                         break;
104                 case PASS_DIFFUSE_INDIRECT:
105                         pass.components = 4;
106                         pass.exposure = true;
107                         pass.divide_type = PASS_DIFFUSE_COLOR;
108                         break;
109                 case PASS_GLOSSY_INDIRECT:
110                         pass.components = 4;
111                         pass.exposure = true;
112                         pass.divide_type = PASS_GLOSSY_COLOR;
113                         break;
114                 case PASS_TRANSMISSION_INDIRECT:
115                         pass.components = 4;
116                         pass.exposure = true;
117                         pass.divide_type = PASS_TRANSMISSION_COLOR;
118                         break;
119                 case PASS_SUBSURFACE_INDIRECT:
120                         pass.components = 4;
121                         pass.exposure = true;
122                         pass.divide_type = PASS_SUBSURFACE_COLOR;
123                         break;
124                 case PASS_DIFFUSE_DIRECT:
125                         pass.components = 4;
126                         pass.exposure = true;
127                         pass.divide_type = PASS_DIFFUSE_COLOR;
128                         break;
129                 case PASS_GLOSSY_DIRECT:
130                         pass.components = 4;
131                         pass.exposure = true;
132                         pass.divide_type = PASS_GLOSSY_COLOR;
133                         break;
134                 case PASS_TRANSMISSION_DIRECT:
135                         pass.components = 4;
136                         pass.exposure = true;
137                         pass.divide_type = PASS_TRANSMISSION_COLOR;
138                         break;
139                 case PASS_SUBSURFACE_DIRECT:
140                         pass.components = 4;
141                         pass.exposure = true;
142                         pass.divide_type = PASS_SUBSURFACE_COLOR;
143                         break;
144
145                 case PASS_EMISSION:
146                         pass.components = 4;
147                         pass.exposure = true;
148                         break;
149                 case PASS_BACKGROUND:
150                         pass.components = 4;
151                         pass.exposure = true;
152                         break;
153                 case PASS_AO:
154                         pass.components = 4;
155                         break;
156                 case PASS_SHADOW:
157                         pass.components = 4;
158                         pass.exposure = false;
159                         break;
160         }
161
162         passes.push_back(pass);
163
164         /* order from by components, to ensure alignment so passes with size 4
165          * come first and then passes with size 1 */
166         sort(passes.begin(), passes.end(), compare_pass_order);
167
168         if(pass.divide_type != PASS_NONE)
169                 Pass::add(pass.divide_type, passes);
170 }
171
172 bool Pass::equals(const vector<Pass>& A, const vector<Pass>& B)
173 {
174         if(A.size() != B.size())
175                 return false;
176         
177         for(int i = 0; i < A.size(); i++)
178                 if(A[i].type != B[i].type)
179                         return false;
180         
181         return true;
182 }
183
184 bool Pass::contains(const vector<Pass>& passes, PassType type)
185 {
186         foreach(const Pass& pass, passes)
187                 if(pass.type == type)
188                         return true;
189         
190         return false;
191 }
192
193 /* Pixel Filter */
194
195 static float filter_func_box(float v, float width)
196 {
197         return 1.0f;
198 }
199
200 static float filter_func_gaussian(float v, float width)
201 {
202         v *= 2.0f/width;
203         return expf(-2.0f*v*v);
204 }
205
206 static vector<float> filter_table(FilterType type, float width)
207 {
208         const int filter_table_size = FILTER_TABLE_SIZE-1;
209         vector<float> filter_table_cdf(filter_table_size+1);
210         vector<float> filter_table(filter_table_size+1);
211         float (*filter_func)(float, float) = NULL;
212         int i, half_size = filter_table_size/2;
213
214         switch(type) {
215                 case FILTER_BOX:
216                         filter_func = filter_func_box;
217                         break;
218                 case FILTER_GAUSSIAN:
219                         filter_func = filter_func_gaussian;
220                         break;
221                 default:
222                         assert(0);
223         }
224
225         /* compute cumulative distribution function */
226         filter_table_cdf[0] = 0.0f;
227         
228         for(i = 0; i < filter_table_size; i++) {
229                 float x = i*width*0.5f/(filter_table_size-1);
230                 float y = filter_func(x, width);
231                 filter_table_cdf[i+1] += filter_table_cdf[i] + fabsf(y);
232         }
233
234         for(i = 0; i <= filter_table_size; i++)
235                 filter_table_cdf[i] /= filter_table_cdf[filter_table_size];
236         
237         /* create importance sampling table */
238         for(i = 0; i <= half_size; i++) {
239                 float x = i/(float)half_size;
240                 int index = upper_bound(filter_table_cdf.begin(), filter_table_cdf.end(), x) - filter_table_cdf.begin();
241                 float t;
242
243                 if(index < filter_table_size+1) {
244                         t = (x - filter_table_cdf[index])/(filter_table_cdf[index+1] - filter_table_cdf[index]);
245                 }
246                 else {
247                         t = 0.0f;
248                         index = filter_table_size;
249                 }
250
251                 float y = ((index + t)/(filter_table_size))*width;
252
253                 filter_table[half_size+i] = 0.5f*(1.0f + y);
254                 filter_table[half_size-i] = 0.5f*(1.0f - y);
255         }
256
257         return filter_table;
258 }
259
260 /* Film */
261
262 Film::Film()
263 {
264         exposure = 0.8f;
265         Pass::add(PASS_COMBINED, passes);
266
267         filter_type = FILTER_BOX;
268         filter_width = 1.0f;
269         filter_table_offset = TABLE_OFFSET_INVALID;
270
271         mist_start = 0.0f;
272         mist_depth = 100.0f;
273         mist_falloff = 1.0f;
274
275         use_light_visibility = false;
276
277         need_update = true;
278 }
279
280 Film::~Film()
281 {
282 }
283
284 void Film::device_update(Device *device, DeviceScene *dscene, Scene *scene)
285 {
286         if(!need_update)
287                 return;
288         
289         device_free(device, dscene, scene);
290
291         KernelFilm *kfilm = &dscene->data.film;
292
293         /* update __data */
294         kfilm->exposure = exposure;
295         kfilm->pass_flag = 0;
296         kfilm->pass_stride = 0;
297         kfilm->use_light_pass = use_light_visibility;
298
299         foreach(Pass& pass, passes) {
300                 kfilm->pass_flag |= pass.type;
301
302                 switch(pass.type) {
303                         case PASS_COMBINED:
304                                 kfilm->pass_combined = kfilm->pass_stride;
305                                 break;
306                         case PASS_DEPTH:
307                                 kfilm->pass_depth = kfilm->pass_stride;
308                                 break;
309                         case PASS_MIST:
310                                 kfilm->pass_mist = kfilm->pass_stride;
311                                 kfilm->use_light_pass = 1;
312                                 break;
313                         case PASS_NORMAL:
314                                 kfilm->pass_normal = kfilm->pass_stride;
315                                 break;
316                         case PASS_UV:
317                                 kfilm->pass_uv = kfilm->pass_stride;
318                                 break;
319                         case PASS_MOTION:
320                                 kfilm->pass_motion = kfilm->pass_stride;
321                                 break;
322                         case PASS_MOTION_WEIGHT:
323                                 kfilm->pass_motion_weight = kfilm->pass_stride;
324                                 break;
325                         case PASS_OBJECT_ID:
326                                 kfilm->pass_object_id = kfilm->pass_stride;
327                                 break;
328                         case PASS_MATERIAL_ID:
329                                 kfilm->pass_material_id = kfilm->pass_stride;
330                                 break;
331                         case PASS_DIFFUSE_COLOR:
332                                 kfilm->pass_diffuse_color = kfilm->pass_stride;
333                                 kfilm->use_light_pass = 1;
334                                 break;
335                         case PASS_GLOSSY_COLOR:
336                                 kfilm->pass_glossy_color = kfilm->pass_stride;
337                                 kfilm->use_light_pass = 1;
338                                 break;
339                         case PASS_TRANSMISSION_COLOR:
340                                 kfilm->pass_transmission_color = kfilm->pass_stride;
341                                 kfilm->use_light_pass = 1;
342                                 break;
343                         case PASS_SUBSURFACE_COLOR:
344                                 kfilm->pass_subsurface_color = kfilm->pass_stride;
345                                 kfilm->use_light_pass = 1;
346                                 break;
347                         case PASS_DIFFUSE_INDIRECT:
348                                 kfilm->pass_diffuse_indirect = kfilm->pass_stride;
349                                 kfilm->use_light_pass = 1;
350                                 break;
351                         case PASS_GLOSSY_INDIRECT:
352                                 kfilm->pass_glossy_indirect = kfilm->pass_stride;
353                                 kfilm->use_light_pass = 1;
354                                 break;
355                         case PASS_TRANSMISSION_INDIRECT:
356                                 kfilm->pass_transmission_indirect = kfilm->pass_stride;
357                                 kfilm->use_light_pass = 1;
358                                 break;
359                         case PASS_SUBSURFACE_INDIRECT:
360                                 kfilm->pass_subsurface_indirect = kfilm->pass_stride;
361                                 kfilm->use_light_pass = 1;
362                                 break;
363                         case PASS_DIFFUSE_DIRECT:
364                                 kfilm->pass_diffuse_direct = kfilm->pass_stride;
365                                 kfilm->use_light_pass = 1;
366                                 break;
367                         case PASS_GLOSSY_DIRECT:
368                                 kfilm->pass_glossy_direct = kfilm->pass_stride;
369                                 kfilm->use_light_pass = 1;
370                                 break;
371                         case PASS_TRANSMISSION_DIRECT:
372                                 kfilm->pass_transmission_direct = kfilm->pass_stride;
373                                 kfilm->use_light_pass = 1;
374                                 break;
375                         case PASS_SUBSURFACE_DIRECT:
376                                 kfilm->pass_subsurface_direct = kfilm->pass_stride;
377                                 kfilm->use_light_pass = 1;
378                                 break;
379
380                         case PASS_EMISSION:
381                                 kfilm->pass_emission = kfilm->pass_stride;
382                                 kfilm->use_light_pass = 1;
383                                 break;
384                         case PASS_BACKGROUND:
385                                 kfilm->pass_background = kfilm->pass_stride;
386                                 kfilm->use_light_pass = 1;
387                                 break;
388                         case PASS_AO:
389                                 kfilm->pass_ao = kfilm->pass_stride;
390                                 kfilm->use_light_pass = 1;
391                                 break;
392                         case PASS_SHADOW:
393                                 kfilm->pass_shadow = kfilm->pass_stride;
394                                 kfilm->use_light_pass = 1;
395                                 break;
396                         case PASS_NONE:
397                                 break;
398                 }
399
400                 kfilm->pass_stride += pass.components;
401         }
402
403         kfilm->pass_stride = align_up(kfilm->pass_stride, 4);
404
405         /* update filter table */
406         vector<float> table = filter_table(filter_type, filter_width);
407         filter_table_offset = scene->lookup_tables->add_table(dscene, table);
408         kfilm->filter_table_offset = (int)filter_table_offset;
409
410         /* mist pass parameters */
411         kfilm->mist_start = mist_start;
412         kfilm->mist_inv_depth = (mist_depth > 0.0f)? 1.0f/mist_depth: 0.0f;
413         kfilm->mist_falloff = mist_falloff;
414
415         need_update = false;
416 }
417
418 void Film::device_free(Device *device, DeviceScene *dscene, Scene *scene)
419 {
420         if(filter_table_offset != TABLE_OFFSET_INVALID) {
421                 scene->lookup_tables->remove_table(filter_table_offset);
422                 filter_table_offset = TABLE_OFFSET_INVALID;
423         }
424 }
425
426 bool Film::modified(const Film& film)
427 {
428         return !(exposure == film.exposure
429                 && Pass::equals(passes, film.passes)
430                 && filter_type == film.filter_type
431                 && filter_width == film.filter_width);
432 }
433
434 void Film::tag_passes_update(Scene *scene, const vector<Pass>& passes_)
435 {
436         if(Pass::contains(passes, PASS_UV) != Pass::contains(passes_, PASS_UV))
437                 scene->mesh_manager->tag_update(scene);
438         else if(Pass::contains(passes, PASS_MOTION) != Pass::contains(passes_, PASS_MOTION))
439                 scene->mesh_manager->tag_update(scene);
440
441         passes = passes_;
442 }
443
444 void Film::tag_update(Scene *scene)
445 {
446         need_update = true;
447 }
448
449 CCL_NAMESPACE_END
450