Sequencer: invalidate current frame cache on sequence transform
[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 "scene.h"
32 #include "svm.h"
33 #include "osl.h"
34
35 #include "util_foreach.h"
36 #include "util_progress.h"
37
38 CCL_NAMESPACE_BEGIN
39
40 Scene::Scene(const SceneParams& params_)
41 : params(params_)
42 {
43         device = NULL;
44         memset(&dscene.data, 0, sizeof(dscene.data));
45
46         camera = new Camera();
47         filter = new Filter();
48         film = new Film();
49         background = new Background();
50         light_manager = new LightManager();
51         mesh_manager = new MeshManager();
52         object_manager = new ObjectManager();
53         integrator = new Integrator();
54         image_manager = new ImageManager();
55         shader_manager = ShaderManager::create(this);
56 }
57
58 Scene::~Scene()
59 {
60         if(device) camera->device_free(device, &dscene);
61         delete camera;
62
63         if(device) filter->device_free(device, &dscene);
64         delete filter;
65
66         if(device) film->device_free(device, &dscene);
67         delete film;
68
69         if(device) background->device_free(device, &dscene);
70         delete background;
71
72         if(device) mesh_manager->device_free(device, &dscene);
73         delete mesh_manager;
74
75         if(device) object_manager->device_free(device, &dscene);
76         delete object_manager;
77
78         if(device) integrator->device_free(device, &dscene);
79         delete integrator;
80
81         if(device) shader_manager->device_free(device, &dscene);
82         delete shader_manager;
83
84         if(device) light_manager->device_free(device, &dscene);
85         delete light_manager;
86
87         foreach(Shader *s, shaders)
88                 delete s;
89         foreach(Mesh *m, meshes)
90                 delete m;
91         foreach(Object *o, objects)
92                 delete o;
93         foreach(Light *l, lights)
94                 delete l;
95
96         if(device) image_manager->device_free(device, &dscene);
97         delete image_manager;
98 }
99
100 void Scene::device_update(Device *device_, Progress& progress)
101 {
102         if(!device)
103                 device = device_;
104         
105         /* The order of updates is important, because there's dependencies between
106          * the different managers, using data computed by previous managers.
107          *
108          * - Background generates shader graph compiled by shader manager.
109          * - Image manager uploads images used by shaders.
110          * - Camera may be used for adapative subdivison.
111          * - Displacement shader must have all shader data available.
112          * - Light manager needs final mesh data to compute emission CDF.
113          */
114         
115         image_manager->set_pack_images(device->info.pack_images);
116
117         progress.set_status("Updating Background");
118         background->device_update(device, &dscene, this);
119
120         if(progress.get_cancel()) return;
121
122         progress.set_status("Updating Shaders");
123         shader_manager->device_update(device, &dscene, this, progress);
124
125         if(progress.get_cancel()) return;
126
127         progress.set_status("Updating Images");
128         image_manager->device_update(device, &dscene, progress);
129
130         if(progress.get_cancel()) return;
131
132         progress.set_status("Updating Camera");
133         camera->device_update(device, &dscene, this);
134
135         if(progress.get_cancel()) return;
136
137         progress.set_status("Updating Objects");
138         object_manager->device_update(device, &dscene, this, progress);
139
140         if(progress.get_cancel()) return;
141
142         progress.set_status("Updating Meshes");
143         mesh_manager->device_update(device, &dscene, this, progress);
144
145         if(progress.get_cancel()) return;
146
147         progress.set_status("Updating Lights");
148         light_manager->device_update(device, &dscene, this, progress);
149
150         if(progress.get_cancel()) return;
151
152         progress.set_status("Updating Filter");
153         filter->device_update(device, &dscene);
154
155         if(progress.get_cancel()) return;
156
157         progress.set_status("Updating Film");
158         film->device_update(device, &dscene);
159
160         if(progress.get_cancel()) return;
161
162         progress.set_status("Updating Integrator");
163         integrator->device_update(device, &dscene, this);
164
165         if(progress.get_cancel()) return;
166
167         progress.set_status("Updating Device", "Writing constant memory");
168         device->const_copy_to("__data", &dscene.data, sizeof(dscene.data));
169 }
170
171 Scene::MotionType Scene::need_motion()
172 {
173         if(integrator->motion_blur)
174                 return MOTION_BLUR;
175         else if(Pass::contains(film->passes, PASS_MOTION))
176                 return MOTION_PASS;
177         else
178                 return MOTION_NONE;
179 }
180
181 bool Scene::need_global_attribute(AttributeStandard std)
182 {
183         if(std == ATTR_STD_UV)
184                 return Pass::contains(film->passes, PASS_UV);
185         if(std == ATTR_STD_MOTION_PRE || ATTR_STD_MOTION_POST)
186                 return need_motion() == MOTION_PASS;
187         
188         return false;
189 }
190
191 void Scene::need_global_attributes(AttributeRequestSet& attributes)
192 {
193         for(int std = ATTR_STD_NONE; std < ATTR_STD_NUM; std++)
194                 if(need_global_attribute((AttributeStandard)std))
195                         attributes.add((AttributeStandard)std);
196 }
197
198 bool Scene::need_update()
199 {
200         return (need_reset() || film->need_update);
201 }
202
203 bool Scene::need_reset()
204 {
205         return (background->need_update
206                 || image_manager->need_update
207                 || camera->need_update
208                 || object_manager->need_update
209                 || mesh_manager->need_update
210                 || light_manager->need_update
211                 || filter->need_update
212                 || integrator->need_update
213                 || shader_manager->need_update);
214 }
215
216 CCL_NAMESPACE_END
217