Fix T43311: using displacement shader crashes blender
[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, SHADINGSYSTEM_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                 else
113                         image_manager->device_free_builtin(device, &dscene);
114
115                 lookup_tables->device_free(device, &dscene);
116         }
117
118         if(final) {
119                 delete lookup_tables;
120                 delete camera;
121                 delete film;
122                 delete background;
123                 delete integrator;
124                 delete object_manager;
125                 delete mesh_manager;
126                 delete shader_manager;
127                 delete light_manager;
128                 delete particle_system_manager;
129                 delete curve_system_manager;
130                 delete image_manager;
131                 delete bake_manager;
132         }
133 }
134
135 void Scene::device_update(Device *device_, Progress& progress)
136 {
137         if(!device)
138                 device = device_;
139         
140         /* The order of updates is important, because there's dependencies between
141          * the different managers, using data computed by previous managers.
142          *
143          * - Image manager uploads images used by shaders.
144          * - Camera may be used for adaptive subdivision.
145          * - Displacement shader must have all shader data available.
146          * - Light manager needs lookup tables and final mesh data to compute emission CDF.
147          * - Film needs light manager to run for use_light_visibility
148          * - Lookup tables are done a second time to handle film tables
149          */
150         
151         image_manager->set_pack_images(device->info.pack_images);
152
153         progress.set_status("Updating Shaders");
154         shader_manager->device_update(device, &dscene, this, progress);
155
156         if(progress.get_cancel() || device->have_error()) return;
157
158         progress.set_status("Updating Images");
159         image_manager->device_update(device, &dscene, progress);
160
161         if(progress.get_cancel() || device->have_error()) return;
162
163         progress.set_status("Updating Background");
164         background->device_update(device, &dscene, this);
165
166         if(progress.get_cancel() || device->have_error()) return;
167
168         progress.set_status("Updating Objects");
169         object_manager->device_update(device, &dscene, this, progress);
170
171         if(progress.get_cancel() || device->have_error()) return;
172
173         progress.set_status("Updating Meshes Flags");
174         mesh_manager->device_update_flags(device, &dscene, this, progress);
175
176         if(progress.get_cancel() || device->have_error()) return;
177
178         progress.set_status("Updating Objects Flags");
179         object_manager->device_update_flags(device, &dscene, this, progress);
180
181         if(progress.get_cancel() || device->have_error()) return;
182
183         progress.set_status("Updating Meshes");
184         mesh_manager->device_update(device, &dscene, this, progress);
185
186         if(progress.get_cancel() || device->have_error()) return;
187
188         progress.set_status("Updating Hair Systems");
189         curve_system_manager->device_update(device, &dscene, this, progress);
190
191         if(progress.get_cancel() || device->have_error()) return;
192
193         progress.set_status("Updating Lookup Tables");
194         lookup_tables->device_update(device, &dscene);
195
196         if(progress.get_cancel() || device->have_error()) return;
197
198         /* TODO(sergey): Make sure camera is not needed above. */
199         progress.set_status("Updating Camera");
200         camera->device_update(device, &dscene, this);
201
202         if(progress.get_cancel() || device->have_error()) return;
203
204         progress.set_status("Updating Lights");
205         light_manager->device_update(device, &dscene, this, progress);
206
207         if(progress.get_cancel() || device->have_error()) return;
208
209         progress.set_status("Updating Particle Systems");
210         particle_system_manager->device_update(device, &dscene, this, progress);
211
212         if(progress.get_cancel() || device->have_error()) return;
213
214         progress.set_status("Updating Film");
215         film->device_update(device, &dscene, this);
216
217         if(progress.get_cancel() || device->have_error()) return;
218
219         progress.set_status("Updating Integrator");
220         integrator->device_update(device, &dscene, this);
221
222         if(progress.get_cancel() || device->have_error()) return;
223
224         progress.set_status("Updating Lookup Tables");
225         lookup_tables->device_update(device, &dscene);
226
227         if(progress.get_cancel() || device->have_error()) return;
228
229         progress.set_status("Updating Baking");
230         bake_manager->device_update(device, &dscene, this, progress);
231
232         if(progress.get_cancel() || device->have_error()) return;
233
234         if(device->have_error() == false) {
235                 progress.set_status("Updating Device", "Writing constant memory");
236                 device->const_copy_to("__data", &dscene.data, sizeof(dscene.data));
237         }
238 }
239
240 Scene::MotionType Scene::need_motion(bool advanced_shading)
241 {
242         if(integrator->motion_blur)
243                 return (advanced_shading)? MOTION_BLUR: MOTION_NONE;
244         else if(Pass::contains(film->passes, PASS_MOTION))
245                 return MOTION_PASS;
246         else
247                 return MOTION_NONE;
248 }
249
250 bool Scene::need_global_attribute(AttributeStandard std)
251 {
252         if(std == ATTR_STD_UV)
253                 return Pass::contains(film->passes, PASS_UV);
254         else if(std == ATTR_STD_MOTION_VERTEX_POSITION)
255                 return need_motion() != MOTION_NONE;
256         else if(std == ATTR_STD_MOTION_VERTEX_NORMAL)
257                 return need_motion() == MOTION_BLUR;
258         
259         return false;
260 }
261
262 void Scene::need_global_attributes(AttributeRequestSet& attributes)
263 {
264         for(int std = ATTR_STD_NONE; std < ATTR_STD_NUM; std++)
265                 if(need_global_attribute((AttributeStandard)std))
266                         attributes.add((AttributeStandard)std);
267 }
268
269 bool Scene::need_update()
270 {
271         return (need_reset() || film->need_update);
272 }
273
274 bool Scene::need_reset()
275 {
276         return (background->need_update
277                 || image_manager->need_update
278                 || camera->need_update
279                 || object_manager->need_update
280                 || mesh_manager->need_update
281                 || light_manager->need_update
282                 || lookup_tables->need_update
283                 || integrator->need_update
284                 || shader_manager->need_update
285                 || particle_system_manager->need_update
286                 || curve_system_manager->need_update
287                 || bake_manager->need_update
288                 || film->need_update);
289 }
290
291 void Scene::reset()
292 {
293         shader_manager->reset(this);
294         shader_manager->add_default(this);
295
296         /* ensure all objects are updated */
297         camera->tag_update();
298         film->tag_update(this);
299         background->tag_update(this);
300         integrator->tag_update(this);
301         object_manager->tag_update(this);
302         mesh_manager->tag_update(this);
303         light_manager->tag_update(this);
304         particle_system_manager->tag_update(this);
305         curve_system_manager->tag_update(this);
306 }
307
308 void Scene::device_free()
309 {
310         free_memory(false);
311 }
312
313 CCL_NAMESPACE_END
314