Cycles: Make all #include statements relative to cycles source directory
[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 "render/background.h"
20 #include "render/bake.h"
21 #include "render/camera.h"
22 #include "render/curves.h"
23 #include "device/device.h"
24 #include "render/film.h"
25 #include "render/integrator.h"
26 #include "render/light.h"
27 #include "render/mesh.h"
28 #include "render/object.h"
29 #include "render/osl.h"
30 #include "render/particles.h"
31 #include "render/scene.h"
32 #include "render/shader.h"
33 #include "render/svm.h"
34 #include "render/tables.h"
35
36 #include "util/util_foreach.h"
37 #include "util/util_guarded_allocator.h"
38 #include "util/util_logging.h"
39 #include "util/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, this, 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                 size_t mem_used = util_guarded_get_mem_used();
246                 size_t mem_peak = util_guarded_get_mem_peak();
247
248                 VLOG(1) << "System memory statistics after full device sync:\n"
249                         << "  Usage: " << string_human_readable_number(mem_used)
250                         << " (" << string_human_readable_size(mem_used) << ")\n"
251                         << "  Peak: " << string_human_readable_number(mem_peak)
252                         << " (" << string_human_readable_size(mem_peak) << ")";
253         }
254 }
255
256 Scene::MotionType Scene::need_motion(bool advanced_shading)
257 {
258         if(integrator->motion_blur)
259                 return (advanced_shading)? MOTION_BLUR: MOTION_NONE;
260         else if(Pass::contains(film->passes, PASS_MOTION))
261                 return MOTION_PASS;
262         else
263                 return MOTION_NONE;
264 }
265
266 float Scene::motion_shutter_time()
267 {
268         if(need_motion() == Scene::MOTION_PASS)
269                 return 2.0f;
270         else
271                 return camera->shuttertime;
272 }
273
274 bool Scene::need_global_attribute(AttributeStandard std)
275 {
276         if(std == ATTR_STD_UV)
277                 return Pass::contains(film->passes, PASS_UV);
278         else if(std == ATTR_STD_MOTION_VERTEX_POSITION)
279                 return need_motion() != MOTION_NONE;
280         else if(std == ATTR_STD_MOTION_VERTEX_NORMAL)
281                 return need_motion() == MOTION_BLUR;
282         
283         return false;
284 }
285
286 void Scene::need_global_attributes(AttributeRequestSet& attributes)
287 {
288         for(int std = ATTR_STD_NONE; std < ATTR_STD_NUM; std++)
289                 if(need_global_attribute((AttributeStandard)std))
290                         attributes.add((AttributeStandard)std);
291 }
292
293 bool Scene::need_update()
294 {
295         return (need_reset() || film->need_update);
296 }
297
298 bool Scene::need_data_update()
299 {
300         return (background->need_update
301                 || image_manager->need_update
302                 || object_manager->need_update
303                 || mesh_manager->need_update
304                 || light_manager->need_update
305                 || lookup_tables->need_update
306                 || integrator->need_update
307                 || shader_manager->need_update
308                 || particle_system_manager->need_update
309                 || curve_system_manager->need_update
310                 || bake_manager->need_update
311                 || film->need_update);
312 }
313
314 bool Scene::need_reset()
315 {
316         return need_data_update() || camera->need_update;
317 }
318
319 void Scene::reset()
320 {
321         shader_manager->reset(this);
322         shader_manager->add_default(this);
323
324         /* ensure all objects are updated */
325         camera->tag_update();
326         film->tag_update(this);
327         background->tag_update(this);
328         integrator->tag_update(this);
329         object_manager->tag_update(this);
330         mesh_manager->tag_update(this);
331         light_manager->tag_update(this);
332         particle_system_manager->tag_update(this);
333         curve_system_manager->tag_update(this);
334 }
335
336 void Scene::device_free()
337 {
338         free_memory(false);
339 }
340
341 CCL_NAMESPACE_END
342