Cycles: merge of changes from tomato branch.
[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         if(device) camera->device_free(device, &dscene);
66         delete camera;
67
68         if(device) filter->device_free(device, &dscene);
69         delete filter;
70
71         if(device) film->device_free(device, &dscene);
72         delete film;
73
74         if(device) background->device_free(device, &dscene);
75         delete background;
76
77         if(device) mesh_manager->device_free(device, &dscene);
78         delete mesh_manager;
79
80         if(device) object_manager->device_free(device, &dscene);
81         delete object_manager;
82
83         if(device) integrator->device_free(device, &dscene);
84         delete integrator;
85
86         if(device) shader_manager->device_free(device, &dscene);
87         delete shader_manager;
88
89         if(device) light_manager->device_free(device, &dscene);
90         delete light_manager;
91
92         foreach(Shader *s, shaders)
93                 delete s;
94         foreach(Mesh *m, meshes)
95                 delete m;
96         foreach(Object *o, objects)
97                 delete o;
98         foreach(Light *l, lights)
99                 delete l;
100         foreach(ParticleSystem *p, particle_systems)
101                 delete p;
102
103         if(device) image_manager->device_free(device, &dscene);
104         delete image_manager;
105
106         if(device) particle_system_manager->device_free(device, &dscene);
107         delete particle_system_manager;
108 }
109
110 void Scene::device_update(Device *device_, Progress& progress)
111 {
112         if(!device)
113                 device = device_;
114         
115         /* The order of updates is important, because there's dependencies between
116          * the different managers, using data computed by previous managers.
117          *
118          * - Background generates shader graph compiled by shader manager.
119          * - Image manager uploads images used by shaders.
120          * - Camera may be used for adapative subdivison.
121          * - Displacement shader must have all shader data available.
122          * - Light manager needs final mesh data to compute emission CDF.
123          */
124         
125         image_manager->set_pack_images(device->info.pack_images);
126
127         progress.set_status("Updating Background");
128         background->device_update(device, &dscene, this);
129
130         if(progress.get_cancel()) return;
131
132         progress.set_status("Updating Shaders");
133         shader_manager->device_update(device, &dscene, this, progress);
134
135         if(progress.get_cancel()) return;
136
137         progress.set_status("Updating Images");
138         image_manager->device_update(device, &dscene, progress);
139
140         if(progress.get_cancel()) return;
141
142         progress.set_status("Updating Camera");
143         camera->device_update(device, &dscene, this);
144
145         if(progress.get_cancel()) return;
146
147         progress.set_status("Updating Objects");
148         object_manager->device_update(device, &dscene, this, progress);
149
150         if(progress.get_cancel()) return;
151
152         progress.set_status("Updating Meshes");
153         mesh_manager->device_update(device, &dscene, this, progress);
154
155         if(progress.get_cancel()) return;
156
157         progress.set_status("Updating Lights");
158         light_manager->device_update(device, &dscene, this, progress);
159
160         if(progress.get_cancel()) return;
161
162         progress.set_status("Updating Particle Systems");
163         particle_system_manager->device_update(device, &dscene, this, progress);
164
165         if(progress.get_cancel()) return;
166
167         progress.set_status("Updating Filter");
168         filter->device_update(device, &dscene);
169
170         if(progress.get_cancel()) return;
171
172         progress.set_status("Updating Film");
173         film->device_update(device, &dscene);
174
175         if(progress.get_cancel()) return;
176
177         progress.set_status("Updating Integrator");
178         integrator->device_update(device, &dscene, this);
179
180         if(progress.get_cancel()) return;
181
182         progress.set_status("Updating Device", "Writing constant memory");
183         device->const_copy_to("__data", &dscene.data, sizeof(dscene.data));
184 }
185
186 Scene::MotionType Scene::need_motion()
187 {
188         if(integrator->motion_blur)
189                 return MOTION_BLUR;
190         else if(Pass::contains(film->passes, PASS_MOTION))
191                 return MOTION_PASS;
192         else
193                 return MOTION_NONE;
194 }
195
196 bool Scene::need_global_attribute(AttributeStandard std)
197 {
198         if(std == ATTR_STD_UV)
199                 return Pass::contains(film->passes, PASS_UV);
200         if(std == ATTR_STD_MOTION_PRE || ATTR_STD_MOTION_POST)
201                 return need_motion() == MOTION_PASS;
202         
203         return false;
204 }
205
206 void Scene::need_global_attributes(AttributeRequestSet& attributes)
207 {
208         for(int std = ATTR_STD_NONE; std < ATTR_STD_NUM; std++)
209                 if(need_global_attribute((AttributeStandard)std))
210                         attributes.add((AttributeStandard)std);
211 }
212
213 bool Scene::need_update()
214 {
215         return (need_reset() || film->need_update);
216 }
217
218 bool Scene::need_reset()
219 {
220         return (background->need_update
221                 || image_manager->need_update
222                 || camera->need_update
223                 || object_manager->need_update
224                 || mesh_manager->need_update
225                 || light_manager->need_update
226                 || filter->need_update
227                 || integrator->need_update
228                 || shader_manager->need_update
229                 || particle_system_manager->need_update);
230 }
231
232 CCL_NAMESPACE_END
233