Cycles: Reduce verbosity of logging
[blender.git] / intern / cycles / render / scene.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 <stdlib.h>
18
19 #include "background.h"
20 #include "bake.h"
21 #include "camera.h"
22 #include "curves.h"
23 #include "device.h"
24 #include "film.h"
25 #include "integrator.h"
26 #include "light.h"
27 #include "mesh.h"
28 #include "object.h"
29 #include "osl.h"
30 #include "particles.h"
31 #include "scene.h"
32 #include "shader.h"
33 #include "svm.h"
34 #include "tables.h"
35
36 #include "util_foreach.h"
37 #include "util_guarded_allocator.h"
38 #include "util_logging.h"
39 #include "util_progress.h"
40
41 CCL_NAMESPACE_BEGIN
42
43 Scene::Scene(const SceneParams& params_, const DeviceInfo& device_info_)
44 : params(params_)
45 {
46         device = NULL;
47         memset(&dscene.data, 0, sizeof(dscene.data));
48
49         camera = new Camera();
50         lookup_tables = new LookupTables();
51         film = new Film();
52         background = new Background();
53         light_manager = new LightManager();
54         mesh_manager = new MeshManager();
55         object_manager = new ObjectManager();
56         integrator = new Integrator();
57         image_manager = new ImageManager(device_info_);
58         particle_system_manager = new ParticleSystemManager();
59         curve_system_manager = new CurveSystemManager();
60         bake_manager = new BakeManager();
61
62         /* OSL only works on the CPU */
63         if(device_info_.type == DEVICE_CPU)
64                 shader_manager = ShaderManager::create(this, params.shadingsystem);
65         else
66                 shader_manager = ShaderManager::create(this, SHADINGSYSTEM_SVM);
67 }
68
69 Scene::~Scene()
70 {
71         free_memory(true);
72 }
73
74 void Scene::free_memory(bool final)
75 {
76         foreach(Shader *s, shaders)
77                 delete s;
78         foreach(Mesh *m, meshes)
79                 delete m;
80         foreach(Object *o, objects)
81                 delete o;
82         foreach(Light *l, lights)
83                 delete l;
84         foreach(ParticleSystem *p, particle_systems)
85                 delete p;
86
87         shaders.clear();
88         meshes.clear();
89         objects.clear();
90         lights.clear();
91         particle_systems.clear();
92
93         if(device) {
94                 camera->device_free(device, &dscene, this);
95                 film->device_free(device, &dscene, this);
96                 background->device_free(device, &dscene);
97                 integrator->device_free(device, &dscene);
98
99                 object_manager->device_free(device, &dscene);
100                 mesh_manager->device_free(device, &dscene);
101                 shader_manager->device_free(device, &dscene, this);
102                 light_manager->device_free(device, &dscene);
103
104                 particle_system_manager->device_free(device, &dscene);
105                 curve_system_manager->device_free(device, &dscene);
106
107                 bake_manager->device_free(device, &dscene);
108
109                 if(!params.persistent_data || final)
110                         image_manager->device_free(device, &dscene);
111                 else
112                         image_manager->device_free_builtin(device, &dscene);
113
114                 lookup_tables->device_free(device, &dscene);
115         }
116
117         if(final) {
118                 delete lookup_tables;
119                 delete camera;
120                 delete film;
121                 delete background;
122                 delete integrator;
123                 delete object_manager;
124                 delete mesh_manager;
125                 delete shader_manager;
126                 delete light_manager;
127                 delete particle_system_manager;
128                 delete curve_system_manager;
129                 delete image_manager;
130                 delete bake_manager;
131         }
132 }
133
134 void Scene::device_update(Device *device_, Progress& progress)
135 {
136         if(!device)
137                 device = device_;
138
139         bool print_stats = need_data_update();
140
141         /* The order of updates is important, because there's dependencies between
142          * the different managers, using data computed by previous managers.
143          *
144          * - Image manager uploads images used by shaders.
145          * - Camera may be used for adaptive subdivision.
146          * - Displacement shader must have all shader data available.
147          * - Light manager needs lookup tables and final mesh data to compute emission CDF.
148          * - Film needs light manager to run for use_light_visibility
149          * - Lookup tables are done a second time to handle film tables
150          */
151         
152         image_manager->set_pack_images(device->info.pack_images);
153
154         progress.set_status("Updating Shaders");
155         shader_manager->device_update(device, &dscene, this, progress);
156
157         if(progress.get_cancel() || device->have_error()) return;
158
159         progress.set_status("Updating Background");
160         background->device_update(device, &dscene, this);
161
162         if(progress.get_cancel() || device->have_error()) return;
163
164         progress.set_status("Updating Camera");
165         camera->device_update(device, &dscene, this);
166
167         if(progress.get_cancel() || device->have_error()) return;
168
169         progress.set_status("Updating Meshes Flags");
170         mesh_manager->device_update_flags(device, &dscene, this, progress);
171
172         if(progress.get_cancel() || device->have_error()) return;
173
174         progress.set_status("Updating Objects");
175         object_manager->device_update(device, &dscene, this, progress);
176
177         if(progress.get_cancel() || device->have_error()) return;
178
179         progress.set_status("Updating Meshes");
180         mesh_manager->device_update(device, &dscene, this, progress);
181
182         if(progress.get_cancel() || device->have_error()) return;
183
184         progress.set_status("Updating Objects Flags");
185         object_manager->device_update_flags(device, &dscene, this, progress);
186
187         if(progress.get_cancel() || device->have_error()) return;
188
189         progress.set_status("Updating Images");
190         image_manager->device_update(device, &dscene, progress);
191
192         if(progress.get_cancel() || device->have_error()) return;
193
194         progress.set_status("Updating Camera Volume");
195         camera->device_update_volume(device, &dscene, this);
196
197         if(progress.get_cancel() || device->have_error()) return;
198
199         progress.set_status("Updating Hair Systems");
200         curve_system_manager->device_update(device, &dscene, this, progress);
201
202         if(progress.get_cancel() || device->have_error()) return;
203
204         progress.set_status("Updating Lookup Tables");
205         lookup_tables->device_update(device, &dscene);
206
207         if(progress.get_cancel() || device->have_error()) return;
208
209         progress.set_status("Updating Lights");
210         light_manager->device_update(device, &dscene, this, progress);
211
212         if(progress.get_cancel() || device->have_error()) return;
213
214         progress.set_status("Updating Particle Systems");
215         particle_system_manager->device_update(device, &dscene, this, progress);
216
217         if(progress.get_cancel() || device->have_error()) return;
218
219         progress.set_status("Updating Integrator");
220         integrator->device_update(device, &dscene, this);
221
222         if(progress.get_cancel() || device->have_error()) return;
223
224         progress.set_status("Updating Film");
225         film->device_update(device, &dscene, this);
226
227         if(progress.get_cancel() || device->have_error()) return;
228
229         progress.set_status("Updating Lookup Tables");
230         lookup_tables->device_update(device, &dscene);
231
232         if(progress.get_cancel() || device->have_error()) return;
233
234         progress.set_status("Updating Baking");
235         bake_manager->device_update(device, &dscene, this, progress);
236
237         if(progress.get_cancel() || device->have_error()) return;
238
239         if(device->have_error() == false) {
240                 progress.set_status("Updating Device", "Writing constant memory");
241                 device->const_copy_to("__data", &dscene.data, sizeof(dscene.data));
242         }
243
244         if(print_stats) {
245                 VLOG(1) << "System memory statistics after full device sync:\n"
246                         << "  Usage: " << util_guarded_get_mem_used() << "\n"
247                         << "  Peak: " << util_guarded_get_mem_peak();
248         }
249 }
250
251 Scene::MotionType Scene::need_motion(bool advanced_shading)
252 {
253         if(integrator->motion_blur)
254                 return (advanced_shading)? MOTION_BLUR: MOTION_NONE;
255         else if(Pass::contains(film->passes, PASS_MOTION))
256                 return MOTION_PASS;
257         else
258                 return MOTION_NONE;
259 }
260
261 bool Scene::need_global_attribute(AttributeStandard std)
262 {
263         if(std == ATTR_STD_UV)
264                 return Pass::contains(film->passes, PASS_UV);
265         else if(std == ATTR_STD_MOTION_VERTEX_POSITION)
266                 return need_motion() != MOTION_NONE;
267         else if(std == ATTR_STD_MOTION_VERTEX_NORMAL)
268                 return need_motion() == MOTION_BLUR;
269         
270         return false;
271 }
272
273 void Scene::need_global_attributes(AttributeRequestSet& attributes)
274 {
275         for(int std = ATTR_STD_NONE; std < ATTR_STD_NUM; std++)
276                 if(need_global_attribute((AttributeStandard)std))
277                         attributes.add((AttributeStandard)std);
278 }
279
280 bool Scene::need_update()
281 {
282         return (need_reset() || film->need_update);
283 }
284
285 bool Scene::need_data_update()
286 {
287         return (background->need_update
288                 || image_manager->need_update
289                 || object_manager->need_update
290                 || mesh_manager->need_update
291                 || light_manager->need_update
292                 || lookup_tables->need_update
293                 || integrator->need_update
294                 || shader_manager->need_update
295                 || particle_system_manager->need_update
296                 || curve_system_manager->need_update
297                 || bake_manager->need_update
298                 || film->need_update);
299 }
300
301 bool Scene::need_reset()
302 {
303         return need_data_update() || camera->need_update;
304 }
305
306 void Scene::reset()
307 {
308         shader_manager->reset(this);
309         shader_manager->add_default(this);
310
311         /* ensure all objects are updated */
312         camera->tag_update();
313         film->tag_update(this);
314         background->tag_update(this);
315         integrator->tag_update(this);
316         object_manager->tag_update(this);
317         mesh_manager->tag_update(this);
318         light_manager->tag_update(this);
319         particle_system_manager->tag_update(this);
320         curve_system_manager->tag_update(this);
321 }
322
323 void Scene::device_free()
324 {
325         free_memory(false);
326 }
327
328 CCL_NAMESPACE_END
329