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