2 * Copyright 2011-2013 Blender Foundation
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
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"
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"
43 DeviceScene::DeviceScene(Device *device)
44 : bvh_nodes(device, "__bvh_nodes", MEM_TEXTURE),
45 bvh_leaf_nodes(device, "__bvh_leaf_nodes", MEM_TEXTURE),
46 object_node(device, "__object_node", MEM_TEXTURE),
47 prim_tri_index(device, "__prim_tri_index", MEM_TEXTURE),
48 prim_tri_verts(device, "__prim_tri_verts", MEM_TEXTURE),
49 prim_type(device, "__prim_type", MEM_TEXTURE),
50 prim_visibility(device, "__prim_visibility", MEM_TEXTURE),
51 prim_index(device, "__prim_index", MEM_TEXTURE),
52 prim_object(device, "__prim_object", MEM_TEXTURE),
53 prim_time(device, "__prim_time", MEM_TEXTURE),
54 tri_shader(device, "__tri_shader", MEM_TEXTURE),
55 tri_vnormal(device, "__tri_vnormal", MEM_TEXTURE),
56 tri_vindex(device, "__tri_vindex", MEM_TEXTURE),
57 tri_patch(device, "__tri_patch", MEM_TEXTURE),
58 tri_patch_uv(device, "__tri_patch_uv", MEM_TEXTURE),
59 curves(device, "__curves", MEM_TEXTURE),
60 curve_keys(device, "__curve_keys", MEM_TEXTURE),
61 patches(device, "__patches", MEM_TEXTURE),
62 objects(device, "__objects", MEM_TEXTURE),
63 object_motion_pass(device, "__object_motion_pass", MEM_TEXTURE),
64 object_motion(device, "__object_motion", MEM_TEXTURE),
65 object_flag(device, "__object_flag", MEM_TEXTURE),
66 camera_motion(device, "__camera_motion", MEM_TEXTURE),
67 attributes_map(device, "__attributes_map", MEM_TEXTURE),
68 attributes_float(device, "__attributes_float", MEM_TEXTURE),
69 attributes_float3(device, "__attributes_float3", MEM_TEXTURE),
70 attributes_uchar4(device, "__attributes_uchar4", MEM_TEXTURE),
71 light_distribution(device, "__light_distribution", MEM_TEXTURE),
72 lights(device, "__lights", MEM_TEXTURE),
73 light_background_marginal_cdf(device, "__light_background_marginal_cdf", MEM_TEXTURE),
74 light_background_conditional_cdf(device, "__light_background_conditional_cdf", MEM_TEXTURE),
75 particles(device, "__particles", MEM_TEXTURE),
76 svm_nodes(device, "__svm_nodes", MEM_TEXTURE),
77 shaders(device, "__shaders", MEM_TEXTURE),
78 lookup_table(device, "__lookup_table", MEM_TEXTURE),
79 sobol_directions(device, "__sobol_directions", MEM_TEXTURE),
80 ies_lights(device, "__ies", MEM_TEXTURE)
82 memset((void*)&data, 0, sizeof(data));
85 Scene::Scene(const SceneParams& params_, Device *device)
91 memset((void *)&dscene.data, 0, sizeof(dscene.data));
93 camera = new Camera();
94 dicing_camera = new Camera();
95 lookup_tables = new LookupTables();
97 background = new Background();
98 light_manager = new LightManager();
99 mesh_manager = new MeshManager();
100 object_manager = new ObjectManager();
101 integrator = new Integrator();
102 image_manager = new ImageManager(device->info);
103 particle_system_manager = new ParticleSystemManager();
104 curve_system_manager = new CurveSystemManager();
105 bake_manager = new BakeManager();
107 /* OSL only works on the CPU */
108 if(device->info.has_osl)
109 shader_manager = ShaderManager::create(this, params.shadingsystem);
111 shader_manager = ShaderManager::create(this, SHADINGSYSTEM_SVM);
119 void Scene::free_memory(bool final)
121 foreach(Shader *s, shaders)
123 foreach(Mesh *m, meshes)
125 foreach(Object *o, objects)
127 foreach(Light *l, lights)
129 foreach(ParticleSystem *p, particle_systems)
136 particle_systems.clear();
139 camera->device_free(device, &dscene, this);
140 film->device_free(device, &dscene, this);
141 background->device_free(device, &dscene);
142 integrator->device_free(device, &dscene);
144 object_manager->device_free(device, &dscene);
145 mesh_manager->device_free(device, &dscene);
146 shader_manager->device_free(device, &dscene, this);
147 light_manager->device_free(device, &dscene);
149 particle_system_manager->device_free(device, &dscene);
150 curve_system_manager->device_free(device, &dscene);
152 bake_manager->device_free(device, &dscene);
154 if(!params.persistent_data || final)
155 image_manager->device_free(device);
157 image_manager->device_free_builtin(device);
159 lookup_tables->device_free(device, &dscene);
163 delete lookup_tables;
165 delete dicing_camera;
169 delete object_manager;
171 delete shader_manager;
172 delete light_manager;
173 delete particle_system_manager;
174 delete curve_system_manager;
175 delete image_manager;
180 void Scene::device_update(Device *device_, Progress& progress)
185 bool print_stats = need_data_update();
187 /* The order of updates is important, because there's dependencies between
188 * the different managers, using data computed by previous managers.
190 * - Image manager uploads images used by shaders.
191 * - Camera may be used for adaptive subdivision.
192 * - Displacement shader must have all shader data available.
193 * - Light manager needs lookup tables and final mesh data to compute emission CDF.
194 * - Film needs light manager to run for use_light_visibility
195 * - Lookup tables are done a second time to handle film tables
198 progress.set_status("Updating Shaders");
199 shader_manager->device_update(device, &dscene, this, progress);
201 if(progress.get_cancel() || device->have_error()) return;
203 progress.set_status("Updating Background");
204 background->device_update(device, &dscene, this);
206 if(progress.get_cancel() || device->have_error()) return;
208 progress.set_status("Updating Camera");
209 camera->device_update(device, &dscene, this);
211 if(progress.get_cancel() || device->have_error()) return;
213 mesh_manager->device_update_preprocess(device, this, progress);
215 if(progress.get_cancel() || device->have_error()) return;
217 progress.set_status("Updating Objects");
218 object_manager->device_update(device, &dscene, this, progress);
220 if(progress.get_cancel() || device->have_error()) return;
222 progress.set_status("Updating Hair Systems");
223 curve_system_manager->device_update(device, &dscene, this, progress);
225 if(progress.get_cancel() || device->have_error()) return;
227 progress.set_status("Updating Particle Systems");
228 particle_system_manager->device_update(device, &dscene, this, progress);
230 if(progress.get_cancel() || device->have_error()) return;
232 progress.set_status("Updating Meshes");
233 mesh_manager->device_update(device, &dscene, this, progress);
235 if(progress.get_cancel() || device->have_error()) return;
237 progress.set_status("Updating Objects Flags");
238 object_manager->device_update_flags(device, &dscene, this, progress);
240 if(progress.get_cancel() || device->have_error()) return;
242 progress.set_status("Updating Images");
243 image_manager->device_update(device, this, progress);
245 if(progress.get_cancel() || device->have_error()) return;
247 progress.set_status("Updating Camera Volume");
248 camera->device_update_volume(device, &dscene, this);
250 if(progress.get_cancel() || device->have_error()) return;
252 progress.set_status("Updating Lookup Tables");
253 lookup_tables->device_update(device, &dscene);
255 if(progress.get_cancel() || device->have_error()) return;
257 progress.set_status("Updating Lights");
258 light_manager->device_update(device, &dscene, this, progress);
260 if(progress.get_cancel() || device->have_error()) return;
262 progress.set_status("Updating Integrator");
263 integrator->device_update(device, &dscene, this);
265 if(progress.get_cancel() || device->have_error()) return;
267 progress.set_status("Updating Film");
268 film->device_update(device, &dscene, this);
270 if(progress.get_cancel() || device->have_error()) return;
272 progress.set_status("Updating Lookup Tables");
273 lookup_tables->device_update(device, &dscene);
275 if(progress.get_cancel() || device->have_error()) return;
277 progress.set_status("Updating Baking");
278 bake_manager->device_update(device, &dscene, this, progress);
280 if(progress.get_cancel() || device->have_error()) return;
282 if(device->have_error() == false) {
283 progress.set_status("Updating Device", "Writing constant memory");
284 device->const_copy_to("__data", &dscene.data, sizeof(dscene.data));
288 size_t mem_used = util_guarded_get_mem_used();
289 size_t mem_peak = util_guarded_get_mem_peak();
291 VLOG(1) << "System memory statistics after full device sync:\n"
292 << " Usage: " << string_human_readable_number(mem_used)
293 << " (" << string_human_readable_size(mem_used) << ")\n"
294 << " Peak: " << string_human_readable_number(mem_peak)
295 << " (" << string_human_readable_size(mem_peak) << ")";
299 Scene::MotionType Scene::need_motion()
301 if(integrator->motion_blur)
303 else if(Pass::contains(film->passes, PASS_MOTION))
309 float Scene::motion_shutter_time()
311 if(need_motion() == Scene::MOTION_PASS)
314 return camera->shuttertime;
317 bool Scene::need_global_attribute(AttributeStandard std)
319 if(std == ATTR_STD_UV)
320 return Pass::contains(film->passes, PASS_UV);
321 else if(std == ATTR_STD_MOTION_VERTEX_POSITION)
322 return need_motion() != MOTION_NONE;
323 else if(std == ATTR_STD_MOTION_VERTEX_NORMAL)
324 return need_motion() == MOTION_BLUR;
329 void Scene::need_global_attributes(AttributeRequestSet& attributes)
331 for(int std = ATTR_STD_NONE; std < ATTR_STD_NUM; std++)
332 if(need_global_attribute((AttributeStandard)std))
333 attributes.add((AttributeStandard)std);
336 bool Scene::need_update()
338 return (need_reset() || film->need_update);
341 bool Scene::need_data_update()
343 return (background->need_update
344 || image_manager->need_update
345 || object_manager->need_update
346 || mesh_manager->need_update
347 || light_manager->need_update
348 || lookup_tables->need_update
349 || integrator->need_update
350 || shader_manager->need_update
351 || particle_system_manager->need_update
352 || curve_system_manager->need_update
353 || bake_manager->need_update
354 || film->need_update);
357 bool Scene::need_reset()
359 return need_data_update() || camera->need_update;
364 shader_manager->reset(this);
365 shader_manager->add_default(this);
367 /* ensure all objects are updated */
368 camera->tag_update();
369 dicing_camera->tag_update();
370 film->tag_update(this);
371 background->tag_update(this);
372 integrator->tag_update(this);
373 object_manager->tag_update(this);
374 mesh_manager->tag_update(this);
375 light_manager->tag_update(this);
376 particle_system_manager->tag_update(this);
377 curve_system_manager->tag_update(this);
380 void Scene::device_free()
385 void Scene::collect_statistics(RenderStats *stats)
387 mesh_manager->collect_statistics(this, stats);
388 image_manager->collect_statistics(stats);