e362a35471dd06100858031b17440ea7e811b7e3
[blender-staging.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 "render/background.h"
20 #include "render/bake.h"
21 #include "render/camera.h"
22 #include "render/curves.h"
23 #include "device/device.h"
24 #include "render/film.h"
25 #include "render/integrator.h"
26 #include "render/light.h"
27 #include "render/mesh.h"
28 #include "render/object.h"
29 #include "render/osl.h"
30 #include "render/particles.h"
31 #include "render/scene.h"
32 #include "render/shader.h"
33 #include "render/svm.h"
34 #include "render/tables.h"
35
36 #include "util/util_foreach.h"
37 #include "util/util_guarded_allocator.h"
38 #include "util/util_logging.h"
39 #include "util/util_progress.h"
40
41 CCL_NAMESPACE_BEGIN
42
43 DeviceScene::DeviceScene(Device *device)
44 : bvh_nodes(device, "__bvh_nodes"),
45   bvh_leaf_nodes(device, "__bvh_leaf_nodes"),
46   object_node(device, "__object_node"),
47   prim_tri_index(device, "__prim_tri_index"),
48   prim_tri_verts(device, "__prim_tri_verts"),
49   prim_type(device, "__prim_type"),
50   prim_visibility(device, "__prim_visibility"),
51   prim_index(device, "__prim_index"),
52   prim_object(device, "__prim_object"),
53   prim_time(device, "__prim_time"),
54   tri_shader(device, "__tri_shader"),
55   tri_vnormal(device, "__tri_vnormal"),
56   tri_vindex(device, "__tri_vindex"),
57   tri_patch(device, "__tri_patch"),
58   tri_patch_uv(device, "__tri_patch_uv"),
59   curves(device, "__curves"),
60   curve_keys(device, "__curve_keys"),
61   patches(device, "__patches"),
62   objects(device, "__objects"),
63   objects_vector(device, "__objects_vector"),
64   attributes_map(device, "__attributes_map"),
65   attributes_float(device, "__attributes_float"),
66   attributes_float3(device, "__attributes_float3"),
67   attributes_uchar4(device, "__attributes_uchar4"),
68   light_distribution(device, "__light_distribution"),
69   light_data(device, "__light_data"),
70   light_background_marginal_cdf(device, "__light_background_marginal_cdf"),
71   light_background_conditional_cdf(device, "__light_background_conditional_cdf"),
72   particles(device, "__particles"),
73   svm_nodes(device, "__svm_nodes"),
74   shader_flag(device, "__shader_flag"),
75   object_flag(device, "__object_flag"),
76   lookup_table(device, "__lookup_table"),
77   sobol_directions(device, "__sobol_directions")
78 {
79         memset(&data, 0, sizeof(data));
80 }
81
82 Scene::Scene(const SceneParams& params_, Device *device)
83 : device(device), dscene(device), params(params_)
84 {
85         memset(&dscene.data, 0, sizeof(dscene.data));
86
87         camera = new Camera();
88         lookup_tables = new LookupTables();
89         film = new Film();
90         background = new Background();
91         light_manager = new LightManager();
92         mesh_manager = new MeshManager();
93         object_manager = new ObjectManager();
94         integrator = new Integrator();
95         image_manager = new ImageManager(device->info);
96         particle_system_manager = new ParticleSystemManager();
97         curve_system_manager = new CurveSystemManager();
98         bake_manager = new BakeManager();
99
100         /* OSL only works on the CPU */
101         if(device->info.has_osl)
102                 shader_manager = ShaderManager::create(this, params.shadingsystem);
103         else
104                 shader_manager = ShaderManager::create(this, SHADINGSYSTEM_SVM);
105 }
106
107 Scene::~Scene()
108 {
109         free_memory(true);
110 }
111
112 void Scene::free_memory(bool final)
113 {
114         foreach(Shader *s, shaders)
115                 delete s;
116         foreach(Mesh *m, meshes)
117                 delete m;
118         foreach(Object *o, objects)
119                 delete o;
120         foreach(Light *l, lights)
121                 delete l;
122         foreach(ParticleSystem *p, particle_systems)
123                 delete p;
124
125         shaders.clear();
126         meshes.clear();
127         objects.clear();
128         lights.clear();
129         particle_systems.clear();
130
131         if(device) {
132                 camera->device_free(device, &dscene, this);
133                 film->device_free(device, &dscene, this);
134                 background->device_free(device, &dscene);
135                 integrator->device_free(device, &dscene);
136
137                 object_manager->device_free(device, &dscene);
138                 mesh_manager->device_free(device, &dscene);
139                 shader_manager->device_free(device, &dscene, this);
140                 light_manager->device_free(device, &dscene);
141
142                 particle_system_manager->device_free(device, &dscene);
143                 curve_system_manager->device_free(device, &dscene);
144
145                 bake_manager->device_free(device, &dscene);
146
147                 if(!params.persistent_data || final)
148                         image_manager->device_free(device);
149                 else
150                         image_manager->device_free_builtin(device);
151
152                 lookup_tables->device_free(device, &dscene);
153         }
154
155         if(final) {
156                 delete lookup_tables;
157                 delete camera;
158                 delete film;
159                 delete background;
160                 delete integrator;
161                 delete object_manager;
162                 delete mesh_manager;
163                 delete shader_manager;
164                 delete light_manager;
165                 delete particle_system_manager;
166                 delete curve_system_manager;
167                 delete image_manager;
168                 delete bake_manager;
169         }
170 }
171
172 void Scene::device_update(Device *device_, Progress& progress)
173 {
174         if(!device)
175                 device = device_;
176
177         bool print_stats = need_data_update();
178
179         /* The order of updates is important, because there's dependencies between
180          * the different managers, using data computed by previous managers.
181          *
182          * - Image manager uploads images used by shaders.
183          * - Camera may be used for adaptive subdivision.
184          * - Displacement shader must have all shader data available.
185          * - Light manager needs lookup tables and final mesh data to compute emission CDF.
186          * - Film needs light manager to run for use_light_visibility
187          * - Lookup tables are done a second time to handle film tables
188          */
189
190         progress.set_status("Updating Shaders");
191         shader_manager->device_update(device, &dscene, this, progress);
192
193         if(progress.get_cancel() || device->have_error()) return;
194
195         progress.set_status("Updating Background");
196         background->device_update(device, &dscene, this);
197
198         if(progress.get_cancel() || device->have_error()) return;
199
200         progress.set_status("Updating Camera");
201         camera->device_update(device, &dscene, this);
202
203         if(progress.get_cancel() || device->have_error()) return;
204
205         progress.set_status("Updating Meshes Flags");
206         mesh_manager->device_update_flags(device, &dscene, this, progress);
207
208         if(progress.get_cancel() || device->have_error()) return;
209
210         progress.set_status("Updating Objects");
211         object_manager->device_update(device, &dscene, this, progress);
212
213         if(progress.get_cancel() || device->have_error()) return;
214
215         progress.set_status("Updating Meshes");
216         mesh_manager->device_update(device, &dscene, this, progress);
217
218         if(progress.get_cancel() || device->have_error()) return;
219
220         progress.set_status("Updating Objects Flags");
221         object_manager->device_update_flags(device, &dscene, this, progress);
222
223         if(progress.get_cancel() || device->have_error()) return;
224
225         progress.set_status("Updating Images");
226         image_manager->device_update(device, this, progress);
227
228         if(progress.get_cancel() || device->have_error()) return;
229
230         progress.set_status("Updating Camera Volume");
231         camera->device_update_volume(device, &dscene, this);
232
233         if(progress.get_cancel() || device->have_error()) return;
234
235         progress.set_status("Updating Hair Systems");
236         curve_system_manager->device_update(device, &dscene, this, progress);
237
238         if(progress.get_cancel() || device->have_error()) return;
239
240         progress.set_status("Updating Lookup Tables");
241         lookup_tables->device_update(device, &dscene);
242
243         if(progress.get_cancel() || device->have_error()) return;
244
245         progress.set_status("Updating Lights");
246         light_manager->device_update(device, &dscene, this, progress);
247
248         if(progress.get_cancel() || device->have_error()) return;
249
250         progress.set_status("Updating Particle Systems");
251         particle_system_manager->device_update(device, &dscene, this, progress);
252
253         if(progress.get_cancel() || device->have_error()) return;
254
255         progress.set_status("Updating Integrator");
256         integrator->device_update(device, &dscene, this);
257
258         if(progress.get_cancel() || device->have_error()) return;
259
260         progress.set_status("Updating Film");
261         film->device_update(device, &dscene, this);
262
263         if(progress.get_cancel() || device->have_error()) return;
264
265         progress.set_status("Updating Lookup Tables");
266         lookup_tables->device_update(device, &dscene);
267
268         if(progress.get_cancel() || device->have_error()) return;
269
270         progress.set_status("Updating Baking");
271         bake_manager->device_update(device, &dscene, this, progress);
272
273         if(progress.get_cancel() || device->have_error()) return;
274
275         if(device->have_error() == false) {
276                 progress.set_status("Updating Device", "Writing constant memory");
277                 device->const_copy_to("__data", &dscene.data, sizeof(dscene.data));
278         }
279
280         if(print_stats) {
281                 size_t mem_used = util_guarded_get_mem_used();
282                 size_t mem_peak = util_guarded_get_mem_peak();
283
284                 VLOG(1) << "System memory statistics after full device sync:\n"
285                         << "  Usage: " << string_human_readable_number(mem_used)
286                         << " (" << string_human_readable_size(mem_used) << ")\n"
287                         << "  Peak: " << string_human_readable_number(mem_peak)
288                         << " (" << string_human_readable_size(mem_peak) << ")";
289         }
290 }
291
292 Scene::MotionType Scene::need_motion(bool advanced_shading)
293 {
294         if(integrator->motion_blur)
295                 return (advanced_shading)? MOTION_BLUR: MOTION_NONE;
296         else if(Pass::contains(film->passes, PASS_MOTION))
297                 return MOTION_PASS;
298         else
299                 return MOTION_NONE;
300 }
301
302 float Scene::motion_shutter_time()
303 {
304         if(need_motion() == Scene::MOTION_PASS)
305                 return 2.0f;
306         else
307                 return camera->shuttertime;
308 }
309
310 bool Scene::need_global_attribute(AttributeStandard std)
311 {
312         if(std == ATTR_STD_UV)
313                 return Pass::contains(film->passes, PASS_UV);
314         else if(std == ATTR_STD_MOTION_VERTEX_POSITION)
315                 return need_motion() != MOTION_NONE;
316         else if(std == ATTR_STD_MOTION_VERTEX_NORMAL)
317                 return need_motion() == MOTION_BLUR;
318         
319         return false;
320 }
321
322 void Scene::need_global_attributes(AttributeRequestSet& attributes)
323 {
324         for(int std = ATTR_STD_NONE; std < ATTR_STD_NUM; std++)
325                 if(need_global_attribute((AttributeStandard)std))
326                         attributes.add((AttributeStandard)std);
327 }
328
329 bool Scene::need_update()
330 {
331         return (need_reset() || film->need_update);
332 }
333
334 bool Scene::need_data_update()
335 {
336         return (background->need_update
337                 || image_manager->need_update
338                 || object_manager->need_update
339                 || mesh_manager->need_update
340                 || light_manager->need_update
341                 || lookup_tables->need_update
342                 || integrator->need_update
343                 || shader_manager->need_update
344                 || particle_system_manager->need_update
345                 || curve_system_manager->need_update
346                 || bake_manager->need_update
347                 || film->need_update);
348 }
349
350 bool Scene::need_reset()
351 {
352         return need_data_update() || camera->need_update;
353 }
354
355 void Scene::reset()
356 {
357         shader_manager->reset(this);
358         shader_manager->add_default(this);
359
360         /* ensure all objects are updated */
361         camera->tag_update();
362         film->tag_update(this);
363         background->tag_update(this);
364         integrator->tag_update(this);
365         object_manager->tag_update(this);
366         mesh_manager->tag_update(this);
367         light_manager->tag_update(this);
368         particle_system_manager->tag_update(this);
369         curve_system_manager->tag_update(this);
370 }
371
372 void Scene::device_free()
373 {
374         free_memory(false);
375 }
376
377 CCL_NAMESPACE_END
378