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