style cleanup: block comments
[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 "scene.h"
24
25 #include "util_algorithm.h"
26 #include "util_foreach.h"
27
28 CCL_NAMESPACE_BEGIN
29
30 /* Pass */
31
32 static bool compare_pass_order(const Pass& a, const Pass& b)
33 {
34         if(a.components == b.components)
35                 return (a.type < b.type);
36         return (a.components > b.components);
37 }
38
39 void Pass::add(PassType type, vector<Pass>& passes)
40 {
41         foreach(Pass& existing_pass, passes)
42                 if(existing_pass.type == type)
43                         return;
44
45         Pass pass;
46
47         pass.type = type;
48         pass.filter = true;
49         pass.exposure = false;
50         pass.divide_type = PASS_NONE;
51
52         switch(type) {
53                 case PASS_NONE:
54                         pass.components = 0;
55                         break;
56                 case PASS_COMBINED:
57                         pass.components = 4;
58                         pass.exposure = true;
59                         break;
60                 case PASS_DEPTH:
61                         pass.components = 1;
62                         pass.filter = false;
63                         break;
64                 case PASS_NORMAL:
65                         pass.components = 4;
66                         break;
67                 case PASS_UV:
68                         pass.components = 4;
69                         break;
70                 case PASS_MOTION:
71                         pass.components = 4;
72                         pass.divide_type = PASS_MOTION_WEIGHT;
73                         break;
74                 case PASS_MOTION_WEIGHT:
75                         pass.components = 1;
76                         break;
77                 case PASS_OBJECT_ID:
78                         pass.components = 1;
79                         pass.filter = false;
80                         break;
81                 case PASS_MATERIAL_ID:
82                         pass.components = 1;
83                         pass.filter = false;
84                         break;
85                 case PASS_DIFFUSE_COLOR:
86                         pass.components = 4;
87                         break;
88                 case PASS_GLOSSY_COLOR:
89                         pass.components = 4;
90                         break;
91                 case PASS_TRANSMISSION_COLOR:
92                         pass.components = 4;
93                         break;
94                 case PASS_DIFFUSE_INDIRECT:
95                         pass.components = 4;
96                         pass.exposure = true;
97                         pass.divide_type = PASS_DIFFUSE_COLOR;
98                         break;
99                 case PASS_GLOSSY_INDIRECT:
100                         pass.components = 4;
101                         pass.exposure = true;
102                         pass.divide_type = PASS_GLOSSY_COLOR;
103                         break;
104                 case PASS_TRANSMISSION_INDIRECT:
105                         pass.components = 4;
106                         pass.exposure = true;
107                         pass.divide_type = PASS_TRANSMISSION_COLOR;
108                         break;
109                 case PASS_DIFFUSE_DIRECT:
110                         pass.components = 4;
111                         pass.exposure = true;
112                         pass.divide_type = PASS_DIFFUSE_COLOR;
113                         break;
114                 case PASS_GLOSSY_DIRECT:
115                         pass.components = 4;
116                         pass.exposure = true;
117                         pass.divide_type = PASS_GLOSSY_COLOR;
118                         break;
119                 case PASS_TRANSMISSION_DIRECT:
120                         pass.components = 4;
121                         pass.exposure = true;
122                         pass.divide_type = PASS_TRANSMISSION_COLOR;
123                         break;
124
125                 case PASS_EMISSION:
126                         pass.components = 4;
127                         pass.exposure = true;
128                         break;
129                 case PASS_BACKGROUND:
130                         pass.components = 4;
131                         pass.exposure = true;
132                         break;
133                 case PASS_AO:
134                         pass.components = 4;
135                         break;
136                 case PASS_SHADOW:
137                         pass.components = 4;
138                         pass.exposure = false;
139                         break;
140         }
141
142         passes.push_back(pass);
143
144         /* order from by components, to ensure alignment so passes with size 4
145          * come first and then passes with size 1 */
146         sort(passes.begin(), passes.end(), compare_pass_order);
147
148         if(pass.divide_type != PASS_NONE)
149                 Pass::add(pass.divide_type, passes);
150 }
151
152 bool Pass::equals(const vector<Pass>& A, const vector<Pass>& B)
153 {
154         if(A.size() != B.size())
155                 return false;
156         
157         for(int i = 0; i < A.size(); i++)
158                 if(A[i].type != B[i].type)
159                         return false;
160         
161         return true;
162 }
163
164 bool Pass::contains(const vector<Pass>& passes, PassType type)
165 {
166         foreach(const Pass& pass, passes)
167                 if(pass.type == type)
168                         return true;
169         
170         return false;
171 }
172
173 /* Film */
174
175 Film::Film()
176 {
177         exposure = 0.8f;
178         Pass::add(PASS_COMBINED, passes);
179         need_update = true;
180 }
181
182 Film::~Film()
183 {
184 }
185
186 void Film::device_update(Device *device, DeviceScene *dscene)
187 {
188         if(!need_update)
189                 return;
190
191         KernelFilm *kfilm = &dscene->data.film;
192
193         /* update __data */
194         kfilm->exposure = exposure;
195         kfilm->pass_flag = 0;
196         kfilm->pass_stride = 0;
197         kfilm->use_light_pass = 0;
198
199         foreach(Pass& pass, passes) {
200                 kfilm->pass_flag |= pass.type;
201
202                 switch(pass.type) {
203                         case PASS_COMBINED:
204                                 kfilm->pass_combined = kfilm->pass_stride;
205                                 break;
206                         case PASS_DEPTH:
207                                 kfilm->pass_depth = kfilm->pass_stride;
208                                 break;
209                         case PASS_NORMAL:
210                                 kfilm->pass_normal = kfilm->pass_stride;
211                                 break;
212                         case PASS_UV:
213                                 kfilm->pass_uv = kfilm->pass_stride;
214                                 break;
215                         case PASS_MOTION:
216                                 kfilm->pass_motion = kfilm->pass_stride;
217                                 break;
218                         case PASS_MOTION_WEIGHT:
219                                 kfilm->pass_motion_weight = kfilm->pass_stride;
220                                 break;
221                         case PASS_OBJECT_ID:
222                                 kfilm->pass_object_id = kfilm->pass_stride;
223                                 break;
224                         case PASS_MATERIAL_ID:
225                                 kfilm->pass_material_id = kfilm->pass_stride;
226                                 break;
227                         case PASS_DIFFUSE_COLOR:
228                                 kfilm->pass_diffuse_color = kfilm->pass_stride;
229                                 kfilm->use_light_pass = 1;
230                                 break;
231                         case PASS_GLOSSY_COLOR:
232                                 kfilm->pass_glossy_color = kfilm->pass_stride;
233                                 kfilm->use_light_pass = 1;
234                                 break;
235                         case PASS_TRANSMISSION_COLOR:
236                                 kfilm->pass_transmission_color = kfilm->pass_stride;
237                                 kfilm->use_light_pass = 1;
238                                 break;
239                         case PASS_DIFFUSE_INDIRECT:
240                                 kfilm->pass_diffuse_indirect = kfilm->pass_stride;
241                                 kfilm->use_light_pass = 1;
242                                 break;
243                         case PASS_GLOSSY_INDIRECT:
244                                 kfilm->pass_glossy_indirect = kfilm->pass_stride;
245                                 kfilm->use_light_pass = 1;
246                                 break;
247                         case PASS_TRANSMISSION_INDIRECT:
248                                 kfilm->pass_transmission_indirect = kfilm->pass_stride;
249                                 kfilm->use_light_pass = 1;
250                                 break;
251                         case PASS_DIFFUSE_DIRECT:
252                                 kfilm->pass_diffuse_direct = kfilm->pass_stride;
253                                 kfilm->use_light_pass = 1;
254                                 break;
255                         case PASS_GLOSSY_DIRECT:
256                                 kfilm->pass_glossy_direct = kfilm->pass_stride;
257                                 kfilm->use_light_pass = 1;
258                                 break;
259                         case PASS_TRANSMISSION_DIRECT:
260                                 kfilm->pass_transmission_direct = kfilm->pass_stride;
261                                 kfilm->use_light_pass = 1;
262                                 break;
263
264                         case PASS_EMISSION:
265                                 kfilm->pass_emission = kfilm->pass_stride;
266                                 kfilm->use_light_pass = 1;
267                                 break;
268                         case PASS_BACKGROUND:
269                                 kfilm->pass_background = kfilm->pass_stride;
270                                 kfilm->use_light_pass = 1;
271                         case PASS_AO:
272                                 kfilm->pass_ao = kfilm->pass_stride;
273                                 kfilm->use_light_pass = 1;
274                         case PASS_SHADOW:
275                                 kfilm->pass_shadow = kfilm->pass_stride;
276                                 kfilm->use_light_pass = 1;
277                         case PASS_NONE:
278                                 break;
279                 }
280
281                 kfilm->pass_stride += pass.components;
282         }
283
284         kfilm->pass_stride = align_up(kfilm->pass_stride, 4);
285
286         need_update = false;
287 }
288
289 void Film::device_free(Device *device, DeviceScene *dscene)
290 {
291 }
292
293 bool Film::modified(const Film& film)
294 {
295         return !(exposure == film.exposure
296                 && Pass::equals(passes, film.passes));
297 }
298
299 void Film::tag_update(Scene *scene)
300 {
301         need_update = true;
302 }
303
304 CCL_NAMESPACE_END
305