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_float2(device, "__attributes_float2", MEM_TEXTURE),
70 attributes_float3(device, "__attributes_float3", MEM_TEXTURE),
71 attributes_uchar4(device, "__attributes_uchar4", MEM_TEXTURE),
72 light_distribution(device, "__light_distribution", MEM_TEXTURE),
73 lights(device, "__lights", MEM_TEXTURE),
74 light_background_marginal_cdf(device, "__light_background_marginal_cdf", MEM_TEXTURE),
75 light_background_conditional_cdf(device, "__light_background_conditional_cdf", MEM_TEXTURE),
76 particles(device, "__particles", MEM_TEXTURE),
77 svm_nodes(device, "__svm_nodes", MEM_TEXTURE),
78 shaders(device, "__shaders", MEM_TEXTURE),
79 lookup_table(device, "__lookup_table", MEM_TEXTURE),
80 sobol_directions(device, "__sobol_directions", MEM_TEXTURE),
81 ies_lights(device, "__ies", MEM_TEXTURE)
83 memset((void *)&data, 0, sizeof(data));
86 Scene::Scene(const SceneParams ¶ms_, Device *device)
87 : name("Scene"), device(device), dscene(device), params(params_)
89 memset((void *)&dscene.data, 0, sizeof(dscene.data));
91 camera = new Camera();
92 dicing_camera = new Camera();
93 lookup_tables = new LookupTables();
95 background = new Background();
96 light_manager = new LightManager();
97 mesh_manager = new MeshManager();
98 object_manager = new ObjectManager();
99 integrator = new Integrator();
100 image_manager = new ImageManager(device->info);
101 particle_system_manager = new ParticleSystemManager();
102 curve_system_manager = new CurveSystemManager();
103 bake_manager = new BakeManager();
105 /* OSL only works on the CPU */
106 if (device->info.has_osl)
107 shader_manager = ShaderManager::create(this, params.shadingsystem);
109 shader_manager = ShaderManager::create(this, SHADINGSYSTEM_SVM);
117 void Scene::free_memory(bool final)
119 foreach (Shader *s, shaders)
121 foreach (Mesh *m, meshes)
123 foreach (Object *o, objects)
125 foreach (Light *l, lights)
127 foreach (ParticleSystem *p, particle_systems)
134 particle_systems.clear();
137 camera->device_free(device, &dscene, this);
138 film->device_free(device, &dscene, this);
139 background->device_free(device, &dscene);
140 integrator->device_free(device, &dscene);
142 object_manager->device_free(device, &dscene);
143 mesh_manager->device_free(device, &dscene);
144 shader_manager->device_free(device, &dscene, this);
145 light_manager->device_free(device, &dscene);
147 particle_system_manager->device_free(device, &dscene);
148 curve_system_manager->device_free(device, &dscene);
150 bake_manager->device_free(device, &dscene);
152 if (!params.persistent_data || final)
153 image_manager->device_free(device);
155 image_manager->device_free_builtin(device);
157 lookup_tables->device_free(device, &dscene);
161 delete lookup_tables;
163 delete dicing_camera;
167 delete object_manager;
169 delete shader_manager;
170 delete light_manager;
171 delete particle_system_manager;
172 delete curve_system_manager;
173 delete image_manager;
178 void Scene::device_update(Device *device_, Progress &progress)
183 bool print_stats = need_data_update();
185 /* The order of updates is important, because there's dependencies between
186 * the different managers, using data computed by previous managers.
188 * - Image manager uploads images used by shaders.
189 * - Camera may be used for adaptive subdivision.
190 * - Displacement shader must have all shader data available.
191 * - Light manager needs lookup tables and final mesh data to compute emission CDF.
192 * - Film needs light manager to run for use_light_visibility
193 * - Lookup tables are done a second time to handle film tables
196 progress.set_status("Updating Shaders");
197 shader_manager->device_update(device, &dscene, this, progress);
199 if (progress.get_cancel() || device->have_error())
202 progress.set_status("Updating Background");
203 background->device_update(device, &dscene, this);
205 if (progress.get_cancel() || device->have_error())
208 progress.set_status("Updating Camera");
209 camera->device_update(device, &dscene, this);
211 if (progress.get_cancel() || device->have_error())
214 mesh_manager->device_update_preprocess(device, this, progress);
216 if (progress.get_cancel() || device->have_error())
219 progress.set_status("Updating Objects");
220 object_manager->device_update(device, &dscene, this, progress);
222 if (progress.get_cancel() || device->have_error())
225 progress.set_status("Updating Hair Systems");
226 curve_system_manager->device_update(device, &dscene, this, progress);
228 if (progress.get_cancel() || device->have_error())
231 progress.set_status("Updating Particle Systems");
232 particle_system_manager->device_update(device, &dscene, this, progress);
234 if (progress.get_cancel() || device->have_error())
237 progress.set_status("Updating Meshes");
238 mesh_manager->device_update(device, &dscene, this, progress);
240 if (progress.get_cancel() || device->have_error())
243 progress.set_status("Updating Objects Flags");
244 object_manager->device_update_flags(device, &dscene, this, progress);
246 if (progress.get_cancel() || device->have_error())
249 progress.set_status("Updating Images");
250 image_manager->device_update(device, this, progress);
252 if (progress.get_cancel() || device->have_error())
255 progress.set_status("Updating Camera Volume");
256 camera->device_update_volume(device, &dscene, this);
258 if (progress.get_cancel() || device->have_error())
261 progress.set_status("Updating Lookup Tables");
262 lookup_tables->device_update(device, &dscene);
264 if (progress.get_cancel() || device->have_error())
267 progress.set_status("Updating Lights");
268 light_manager->device_update(device, &dscene, this, progress);
270 if (progress.get_cancel() || device->have_error())
273 progress.set_status("Updating Integrator");
274 integrator->device_update(device, &dscene, this);
276 if (progress.get_cancel() || device->have_error())
279 progress.set_status("Updating Film");
280 film->device_update(device, &dscene, this);
282 if (progress.get_cancel() || device->have_error())
285 progress.set_status("Updating Lookup Tables");
286 lookup_tables->device_update(device, &dscene);
288 if (progress.get_cancel() || device->have_error())
291 progress.set_status("Updating Baking");
292 bake_manager->device_update(device, &dscene, this, progress);
294 if (progress.get_cancel() || device->have_error())
297 if (device->have_error() == false) {
298 progress.set_status("Updating Device", "Writing constant memory");
299 device->const_copy_to("__data", &dscene.data, sizeof(dscene.data));
303 size_t mem_used = util_guarded_get_mem_used();
304 size_t mem_peak = util_guarded_get_mem_peak();
306 VLOG(1) << "System memory statistics after full device sync:\n"
307 << " Usage: " << string_human_readable_number(mem_used) << " ("
308 << string_human_readable_size(mem_used) << ")\n"
309 << " Peak: " << string_human_readable_number(mem_peak) << " ("
310 << string_human_readable_size(mem_peak) << ")";
314 Scene::MotionType Scene::need_motion()
316 if (integrator->motion_blur)
318 else if (Pass::contains(film->passes, PASS_MOTION))
324 float Scene::motion_shutter_time()
326 if (need_motion() == Scene::MOTION_PASS)
329 return camera->shuttertime;
332 bool Scene::need_global_attribute(AttributeStandard std)
334 if (std == ATTR_STD_UV)
335 return Pass::contains(film->passes, PASS_UV);
336 else if (std == ATTR_STD_MOTION_VERTEX_POSITION)
337 return need_motion() != MOTION_NONE;
338 else if (std == ATTR_STD_MOTION_VERTEX_NORMAL)
339 return need_motion() == MOTION_BLUR;
344 void Scene::need_global_attributes(AttributeRequestSet &attributes)
346 for (int std = ATTR_STD_NONE; std < ATTR_STD_NUM; std++)
347 if (need_global_attribute((AttributeStandard)std))
348 attributes.add((AttributeStandard)std);
351 bool Scene::need_update()
353 return (need_reset() || film->need_update);
356 bool Scene::need_data_update()
358 return (background->need_update || image_manager->need_update || object_manager->need_update ||
359 mesh_manager->need_update || light_manager->need_update || lookup_tables->need_update ||
360 integrator->need_update || shader_manager->need_update ||
361 particle_system_manager->need_update || curve_system_manager->need_update ||
362 bake_manager->need_update || film->need_update);
365 bool Scene::need_reset()
367 return need_data_update() || camera->need_update;
372 shader_manager->reset(this);
373 shader_manager->add_default(this);
375 /* ensure all objects are updated */
376 camera->tag_update();
377 dicing_camera->tag_update();
378 film->tag_update(this);
379 background->tag_update(this);
380 integrator->tag_update(this);
381 object_manager->tag_update(this);
382 mesh_manager->tag_update(this);
383 light_manager->tag_update(this);
384 particle_system_manager->tag_update(this);
385 curve_system_manager->tag_update(this);
388 void Scene::device_free()
393 void Scene::collect_statistics(RenderStats *stats)
395 mesh_manager->collect_statistics(this, stats);
396 image_manager->collect_statistics(stats);