Cycles: persistent images option
[blender.git] / intern / cycles / render / scene.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 <stdlib.h>
20
21 #include "background.h"
22 #include "camera.h"
23 #include "device.h"
24 #include "film.h"
25 #include "filter.h"
26 #include "integrator.h"
27 #include "light.h"
28 #include "shader.h"
29 #include "mesh.h"
30 #include "object.h"
31 #include "particles.h"
32 #include "scene.h"
33 #include "svm.h"
34 #include "osl.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         filter = new Filter();
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         shader_manager = ShaderManager::create(this);
57         particle_system_manager = new ParticleSystemManager();
58
59         if (device_info_.type == DEVICE_CPU)
60                 image_manager->set_extended_image_limits();
61 }
62
63 Scene::~Scene()
64 {
65         free_memory(true);
66 }
67
68 void Scene::free_memory(bool final)
69 {
70         foreach(Shader *s, shaders)
71                 delete s;
72         foreach(Mesh *m, meshes)
73                 delete m;
74         foreach(Object *o, objects)
75                 delete o;
76         foreach(Light *l, lights)
77                 delete l;
78         foreach(ParticleSystem *p, particle_systems)
79                 delete p;
80
81         if(device) {
82                 camera->device_free(device, &dscene);
83                 filter->device_free(device, &dscene);
84                 film->device_free(device, &dscene);
85                 background->device_free(device, &dscene);
86                 integrator->device_free(device, &dscene);
87
88                 object_manager->device_free(device, &dscene);
89                 mesh_manager->device_free(device, &dscene);
90                 shader_manager->device_free(device, &dscene);
91                 light_manager->device_free(device, &dscene);
92
93                 particle_system_manager->device_free(device, &dscene);
94
95                 if(!params.persistent_images || final)
96                         image_manager->device_free(device, &dscene);
97         }
98
99         if(final) {
100                 delete filter;
101                 delete camera;
102                 delete film;
103                 delete background;
104                 delete integrator;
105                 delete object_manager;
106                 delete mesh_manager;
107                 delete shader_manager;
108                 delete light_manager;
109                 delete particle_system_manager;
110                 delete image_manager;
111         }
112         else {
113                 shaders.clear();
114                 meshes.clear();
115                 objects.clear();
116                 lights.clear();
117                 particle_systems.clear();
118         }
119 }
120
121 void Scene::device_update(Device *device_, Progress& progress)
122 {
123         if(!device)
124                 device = device_;
125         
126         /* The order of updates is important, because there's dependencies between
127          * the different managers, using data computed by previous managers.
128          *
129          * - Background generates shader graph compiled by shader manager.
130          * - Image manager uploads images used by shaders.
131          * - Camera may be used for adapative subdivison.
132          * - Displacement shader must have all shader data available.
133          * - Light manager needs final mesh data to compute emission CDF.
134          */
135         
136         image_manager->set_pack_images(device->info.pack_images);
137
138         progress.set_status("Updating Background");
139         background->device_update(device, &dscene, this);
140
141         if(progress.get_cancel()) return;
142
143         progress.set_status("Updating Shaders");
144         shader_manager->device_update(device, &dscene, this, progress);
145
146         if(progress.get_cancel()) return;
147
148         progress.set_status("Updating Images");
149         image_manager->device_update(device, &dscene, progress);
150
151         if(progress.get_cancel()) return;
152
153         progress.set_status("Updating Camera");
154         camera->device_update(device, &dscene, this);
155
156         if(progress.get_cancel()) return;
157
158         progress.set_status("Updating Objects");
159         object_manager->device_update(device, &dscene, this, progress);
160
161         if(progress.get_cancel()) return;
162
163         progress.set_status("Updating Meshes");
164         mesh_manager->device_update(device, &dscene, this, progress);
165
166         if(progress.get_cancel()) return;
167
168         progress.set_status("Updating Lights");
169         light_manager->device_update(device, &dscene, this, progress);
170
171         if(progress.get_cancel()) return;
172
173         progress.set_status("Updating Particle Systems");
174         particle_system_manager->device_update(device, &dscene, this, progress);
175
176         if(progress.get_cancel()) return;
177
178         progress.set_status("Updating Filter");
179         filter->device_update(device, &dscene);
180
181         if(progress.get_cancel()) return;
182
183         progress.set_status("Updating Film");
184         film->device_update(device, &dscene);
185
186         if(progress.get_cancel()) return;
187
188         progress.set_status("Updating Integrator");
189         integrator->device_update(device, &dscene, this);
190
191         if(progress.get_cancel()) return;
192
193         progress.set_status("Updating Device", "Writing constant memory");
194         device->const_copy_to("__data", &dscene.data, sizeof(dscene.data));
195 }
196
197 Scene::MotionType Scene::need_motion(bool advanced_shading)
198 {
199         if(integrator->motion_blur)
200                 return (advanced_shading)? MOTION_BLUR: MOTION_NONE;
201         else if(Pass::contains(film->passes, PASS_MOTION))
202                 return MOTION_PASS;
203         else
204                 return MOTION_NONE;
205 }
206
207 bool Scene::need_global_attribute(AttributeStandard std)
208 {
209         if(std == ATTR_STD_UV)
210                 return Pass::contains(film->passes, PASS_UV);
211         if(std == ATTR_STD_MOTION_PRE || ATTR_STD_MOTION_POST)
212                 return need_motion() == MOTION_PASS;
213         
214         return false;
215 }
216
217 void Scene::need_global_attributes(AttributeRequestSet& attributes)
218 {
219         for(int std = ATTR_STD_NONE; std < ATTR_STD_NUM; std++)
220                 if(need_global_attribute((AttributeStandard)std))
221                         attributes.add((AttributeStandard)std);
222 }
223
224 bool Scene::need_update()
225 {
226         return (need_reset() || film->need_update);
227 }
228
229 bool Scene::need_reset()
230 {
231         return (background->need_update
232                 || image_manager->need_update
233                 || camera->need_update
234                 || object_manager->need_update
235                 || mesh_manager->need_update
236                 || light_manager->need_update
237                 || filter->need_update
238                 || integrator->need_update
239                 || shader_manager->need_update
240                 || particle_system_manager->need_update);
241 }
242
243 void Scene::reset()
244 {
245         shader_manager->add_default(this);
246
247         /* ensure all objects are updated */
248         camera->tag_update();
249         filter->tag_update(this);
250         film->tag_update(this);
251         background->tag_update(this);
252         integrator->tag_update(this);
253 }
254
255 void Scene::device_free()
256 {
257         free_memory(false);
258 }
259
260 CCL_NAMESPACE_END
261