Preferences: correct experimental flag use
[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 "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", 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)
82 {
83   memset((void *)&data, 0, sizeof(data));
84 }
85
86 Scene::Scene(const SceneParams &params_, Device *device)
87     : name("Scene"), device(device), dscene(device), params(params_)
88 {
89   memset((void *)&dscene.data, 0, sizeof(dscene.data));
90
91   camera = new Camera();
92   dicing_camera = new Camera();
93   lookup_tables = new LookupTables();
94   film = new Film();
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();
104
105   /* OSL only works on the CPU */
106   if (device->info.has_osl)
107     shader_manager = ShaderManager::create(this, params.shadingsystem);
108   else
109     shader_manager = ShaderManager::create(this, SHADINGSYSTEM_SVM);
110 }
111
112 Scene::~Scene()
113 {
114   free_memory(true);
115 }
116
117 void Scene::free_memory(bool final)
118 {
119   foreach (Shader *s, shaders)
120     delete s;
121   foreach (Mesh *m, meshes)
122     delete m;
123   foreach (Object *o, objects)
124     delete o;
125   foreach (Light *l, lights)
126     delete l;
127   foreach (ParticleSystem *p, particle_systems)
128     delete p;
129
130   shaders.clear();
131   meshes.clear();
132   objects.clear();
133   lights.clear();
134   particle_systems.clear();
135
136   if (device) {
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);
141
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);
146
147     particle_system_manager->device_free(device, &dscene);
148     curve_system_manager->device_free(device, &dscene);
149
150     bake_manager->device_free(device, &dscene);
151
152     if (!params.persistent_data || final)
153       image_manager->device_free(device);
154     else
155       image_manager->device_free_builtin(device);
156
157     lookup_tables->device_free(device, &dscene);
158   }
159
160   if (final) {
161     delete lookup_tables;
162     delete camera;
163     delete dicing_camera;
164     delete film;
165     delete background;
166     delete integrator;
167     delete object_manager;
168     delete mesh_manager;
169     delete shader_manager;
170     delete light_manager;
171     delete particle_system_manager;
172     delete curve_system_manager;
173     delete image_manager;
174     delete bake_manager;
175   }
176 }
177
178 void Scene::device_update(Device *device_, Progress &progress)
179 {
180   if (!device)
181     device = device_;
182
183   bool print_stats = need_data_update();
184
185   /* The order of updates is important, because there's dependencies between
186    * the different managers, using data computed by previous managers.
187    *
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
194    */
195
196   progress.set_status("Updating Shaders");
197   shader_manager->device_update(device, &dscene, this, progress);
198
199   if (progress.get_cancel() || device->have_error())
200     return;
201
202   progress.set_status("Updating Background");
203   background->device_update(device, &dscene, this);
204
205   if (progress.get_cancel() || device->have_error())
206     return;
207
208   progress.set_status("Updating Camera");
209   camera->device_update(device, &dscene, this);
210
211   if (progress.get_cancel() || device->have_error())
212     return;
213
214   mesh_manager->device_update_preprocess(device, this, progress);
215
216   if (progress.get_cancel() || device->have_error())
217     return;
218
219   progress.set_status("Updating Objects");
220   object_manager->device_update(device, &dscene, this, progress);
221
222   if (progress.get_cancel() || device->have_error())
223     return;
224
225   progress.set_status("Updating Hair Systems");
226   curve_system_manager->device_update(device, &dscene, this, progress);
227
228   if (progress.get_cancel() || device->have_error())
229     return;
230
231   progress.set_status("Updating Particle Systems");
232   particle_system_manager->device_update(device, &dscene, this, progress);
233
234   if (progress.get_cancel() || device->have_error())
235     return;
236
237   progress.set_status("Updating Meshes");
238   mesh_manager->device_update(device, &dscene, this, progress);
239
240   if (progress.get_cancel() || device->have_error())
241     return;
242
243   progress.set_status("Updating Objects Flags");
244   object_manager->device_update_flags(device, &dscene, this, progress);
245
246   if (progress.get_cancel() || device->have_error())
247     return;
248
249   progress.set_status("Updating Images");
250   image_manager->device_update(device, this, progress);
251
252   if (progress.get_cancel() || device->have_error())
253     return;
254
255   progress.set_status("Updating Camera Volume");
256   camera->device_update_volume(device, &dscene, this);
257
258   if (progress.get_cancel() || device->have_error())
259     return;
260
261   progress.set_status("Updating Lookup Tables");
262   lookup_tables->device_update(device, &dscene);
263
264   if (progress.get_cancel() || device->have_error())
265     return;
266
267   progress.set_status("Updating Lights");
268   light_manager->device_update(device, &dscene, this, progress);
269
270   if (progress.get_cancel() || device->have_error())
271     return;
272
273   progress.set_status("Updating Integrator");
274   integrator->device_update(device, &dscene, this);
275
276   if (progress.get_cancel() || device->have_error())
277     return;
278
279   progress.set_status("Updating Film");
280   film->device_update(device, &dscene, this);
281
282   if (progress.get_cancel() || device->have_error())
283     return;
284
285   progress.set_status("Updating Lookup Tables");
286   lookup_tables->device_update(device, &dscene);
287
288   if (progress.get_cancel() || device->have_error())
289     return;
290
291   progress.set_status("Updating Baking");
292   bake_manager->device_update(device, &dscene, this, progress);
293
294   if (progress.get_cancel() || device->have_error())
295     return;
296
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));
300   }
301
302   if (print_stats) {
303     size_t mem_used = util_guarded_get_mem_used();
304     size_t mem_peak = util_guarded_get_mem_peak();
305
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) << ")";
311   }
312 }
313
314 Scene::MotionType Scene::need_motion()
315 {
316   if (integrator->motion_blur)
317     return MOTION_BLUR;
318   else if (Pass::contains(film->passes, PASS_MOTION))
319     return MOTION_PASS;
320   else
321     return MOTION_NONE;
322 }
323
324 float Scene::motion_shutter_time()
325 {
326   if (need_motion() == Scene::MOTION_PASS)
327     return 2.0f;
328   else
329     return camera->shuttertime;
330 }
331
332 bool Scene::need_global_attribute(AttributeStandard std)
333 {
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;
340
341   return false;
342 }
343
344 void Scene::need_global_attributes(AttributeRequestSet &attributes)
345 {
346   for (int std = ATTR_STD_NONE; std < ATTR_STD_NUM; std++)
347     if (need_global_attribute((AttributeStandard)std))
348       attributes.add((AttributeStandard)std);
349 }
350
351 bool Scene::need_update()
352 {
353   return (need_reset() || film->need_update);
354 }
355
356 bool Scene::need_data_update()
357 {
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);
363 }
364
365 bool Scene::need_reset()
366 {
367   return need_data_update() || camera->need_update;
368 }
369
370 void Scene::reset()
371 {
372   shader_manager->reset(this);
373   shader_manager->add_default(this);
374
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);
386 }
387
388 void Scene::device_free()
389 {
390   free_memory(false);
391 }
392
393 void Scene::collect_statistics(RenderStats *stats)
394 {
395   mesh_manager->collect_statistics(this, stats);
396   image_manager->collect_statistics(stats);
397 }
398
399 CCL_NAMESPACE_END