Cycles: Cleanup, silence strict compiler warning
[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_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)
81 {
82         memset((void*)&data, 0, sizeof(data));
83 }
84
85 Scene::Scene(const SceneParams& params_, Device *device)
86 : device(device), dscene(device), params(params_)
87 {
88         memset((void *)&dscene.data, 0, sizeof(dscene.data));
89
90         camera = new Camera();
91         dicing_camera = new Camera();
92         lookup_tables = new LookupTables();
93         film = new Film();
94         background = new Background();
95         light_manager = new LightManager();
96         mesh_manager = new MeshManager();
97         object_manager = new ObjectManager();
98         integrator = new Integrator();
99         image_manager = new ImageManager(device->info);
100         particle_system_manager = new ParticleSystemManager();
101         curve_system_manager = new CurveSystemManager();
102         bake_manager = new BakeManager();
103
104         /* OSL only works on the CPU */
105         if(device->info.has_osl)
106                 shader_manager = ShaderManager::create(this, params.shadingsystem);
107         else
108                 shader_manager = ShaderManager::create(this, SHADINGSYSTEM_SVM);
109 }
110
111 Scene::~Scene()
112 {
113         free_memory(true);
114 }
115
116 void Scene::free_memory(bool final)
117 {
118         foreach(Shader *s, shaders)
119                 delete s;
120         foreach(Mesh *m, meshes)
121                 delete m;
122         foreach(Object *o, objects)
123                 delete o;
124         foreach(Light *l, lights)
125                 delete l;
126         foreach(ParticleSystem *p, particle_systems)
127                 delete p;
128
129         shaders.clear();
130         meshes.clear();
131         objects.clear();
132         lights.clear();
133         particle_systems.clear();
134
135         if(device) {
136                 camera->device_free(device, &dscene, this);
137                 film->device_free(device, &dscene, this);
138                 background->device_free(device, &dscene);
139                 integrator->device_free(device, &dscene);
140
141                 object_manager->device_free(device, &dscene);
142                 mesh_manager->device_free(device, &dscene);
143                 shader_manager->device_free(device, &dscene, this);
144                 light_manager->device_free(device, &dscene);
145
146                 particle_system_manager->device_free(device, &dscene);
147                 curve_system_manager->device_free(device, &dscene);
148
149                 bake_manager->device_free(device, &dscene);
150
151                 if(!params.persistent_data || final)
152                         image_manager->device_free(device);
153                 else
154                         image_manager->device_free_builtin(device);
155
156                 lookup_tables->device_free(device, &dscene);
157         }
158
159         if(final) {
160                 delete lookup_tables;
161                 delete camera;
162                 delete dicing_camera;
163                 delete film;
164                 delete background;
165                 delete integrator;
166                 delete object_manager;
167                 delete mesh_manager;
168                 delete shader_manager;
169                 delete light_manager;
170                 delete particle_system_manager;
171                 delete curve_system_manager;
172                 delete image_manager;
173                 delete bake_manager;
174         }
175 }
176
177 void Scene::device_update(Device *device_, Progress& progress)
178 {
179         if(!device)
180                 device = device_;
181
182         bool print_stats = need_data_update();
183
184         /* The order of updates is important, because there's dependencies between
185          * the different managers, using data computed by previous managers.
186          *
187          * - Image manager uploads images used by shaders.
188          * - Camera may be used for adaptive subdivision.
189          * - Displacement shader must have all shader data available.
190          * - Light manager needs lookup tables and final mesh data to compute emission CDF.
191          * - Film needs light manager to run for use_light_visibility
192          * - Lookup tables are done a second time to handle film tables
193          */
194
195         progress.set_status("Updating Shaders");
196         shader_manager->device_update(device, &dscene, this, progress);
197
198         if(progress.get_cancel() || device->have_error()) return;
199
200         progress.set_status("Updating Background");
201         background->device_update(device, &dscene, this);
202
203         if(progress.get_cancel() || device->have_error()) return;
204
205         progress.set_status("Updating Camera");
206         camera->device_update(device, &dscene, this);
207
208         if(progress.get_cancel() || device->have_error()) return;
209
210         mesh_manager->device_update_preprocess(device, this, progress);
211
212         if(progress.get_cancel() || device->have_error()) return;
213
214         progress.set_status("Updating Objects");
215         object_manager->device_update(device, &dscene, this, progress);
216
217         if(progress.get_cancel() || device->have_error()) return;
218
219         progress.set_status("Updating Particle Systems");
220         particle_system_manager->device_update(device, &dscene, this, progress);
221
222         if(progress.get_cancel() || device->have_error()) return;
223
224         progress.set_status("Updating Meshes");
225         mesh_manager->device_update(device, &dscene, this, progress);
226
227         if(progress.get_cancel() || device->have_error()) return;
228
229         progress.set_status("Updating Objects Flags");
230         object_manager->device_update_flags(device, &dscene, this, progress);
231
232         if(progress.get_cancel() || device->have_error()) return;
233
234         progress.set_status("Updating Images");
235         image_manager->device_update(device, this, progress);
236
237         if(progress.get_cancel() || device->have_error()) return;
238
239         progress.set_status("Updating Camera Volume");
240         camera->device_update_volume(device, &dscene, this);
241
242         if(progress.get_cancel() || device->have_error()) return;
243
244         progress.set_status("Updating Hair Systems");
245         curve_system_manager->device_update(device, &dscene, this, progress);
246
247         if(progress.get_cancel() || device->have_error()) return;
248
249         progress.set_status("Updating Lookup Tables");
250         lookup_tables->device_update(device, &dscene);
251
252         if(progress.get_cancel() || device->have_error()) return;
253
254         progress.set_status("Updating Lights");
255         light_manager->device_update(device, &dscene, this, progress);
256
257         if(progress.get_cancel() || device->have_error()) return;
258
259         progress.set_status("Updating Integrator");
260         integrator->device_update(device, &dscene, this);
261
262         if(progress.get_cancel() || device->have_error()) return;
263
264         progress.set_status("Updating Film");
265         film->device_update(device, &dscene, this);
266
267         if(progress.get_cancel() || device->have_error()) return;
268
269         progress.set_status("Updating Lookup Tables");
270         lookup_tables->device_update(device, &dscene);
271
272         if(progress.get_cancel() || device->have_error()) return;
273
274         progress.set_status("Updating Baking");
275         bake_manager->device_update(device, &dscene, this, progress);
276
277         if(progress.get_cancel() || device->have_error()) return;
278
279         if(device->have_error() == false) {
280                 progress.set_status("Updating Device", "Writing constant memory");
281                 device->const_copy_to("__data", &dscene.data, sizeof(dscene.data));
282         }
283
284         if(print_stats) {
285                 size_t mem_used = util_guarded_get_mem_used();
286                 size_t mem_peak = util_guarded_get_mem_peak();
287
288                 VLOG(1) << "System memory statistics after full device sync:\n"
289                         << "  Usage: " << string_human_readable_number(mem_used)
290                         << " (" << string_human_readable_size(mem_used) << ")\n"
291                         << "  Peak: " << string_human_readable_number(mem_peak)
292                         << " (" << string_human_readable_size(mem_peak) << ")";
293         }
294 }
295
296 Scene::MotionType Scene::need_motion()
297 {
298         if(integrator->motion_blur)
299                 return MOTION_BLUR;
300         else if(Pass::contains(film->passes, PASS_MOTION))
301                 return MOTION_PASS;
302         else
303                 return MOTION_NONE;
304 }
305
306 float Scene::motion_shutter_time()
307 {
308         if(need_motion() == Scene::MOTION_PASS)
309                 return 2.0f;
310         else
311                 return camera->shuttertime;
312 }
313
314 bool Scene::need_global_attribute(AttributeStandard std)
315 {
316         if(std == ATTR_STD_UV)
317                 return Pass::contains(film->passes, PASS_UV);
318         else if(std == ATTR_STD_MOTION_VERTEX_POSITION)
319                 return need_motion() != MOTION_NONE;
320         else if(std == ATTR_STD_MOTION_VERTEX_NORMAL)
321                 return need_motion() == MOTION_BLUR;
322         
323         return false;
324 }
325
326 void Scene::need_global_attributes(AttributeRequestSet& attributes)
327 {
328         for(int std = ATTR_STD_NONE; std < ATTR_STD_NUM; std++)
329                 if(need_global_attribute((AttributeStandard)std))
330                         attributes.add((AttributeStandard)std);
331 }
332
333 bool Scene::need_update()
334 {
335         return (need_reset() || film->need_update);
336 }
337
338 bool Scene::need_data_update()
339 {
340         return (background->need_update
341                 || image_manager->need_update
342                 || object_manager->need_update
343                 || mesh_manager->need_update
344                 || light_manager->need_update
345                 || lookup_tables->need_update
346                 || integrator->need_update
347                 || shader_manager->need_update
348                 || particle_system_manager->need_update
349                 || curve_system_manager->need_update
350                 || bake_manager->need_update
351                 || film->need_update);
352 }
353
354 bool Scene::need_reset()
355 {
356         return need_data_update() || camera->need_update;
357 }
358
359 void Scene::reset()
360 {
361         shader_manager->reset(this);
362         shader_manager->add_default(this);
363
364         /* ensure all objects are updated */
365         camera->tag_update();
366         dicing_camera->tag_update();
367         film->tag_update(this);
368         background->tag_update(this);
369         integrator->tag_update(this);
370         object_manager->tag_update(this);
371         mesh_manager->tag_update(this);
372         light_manager->tag_update(this);
373         particle_system_manager->tag_update(this);
374         curve_system_manager->tag_update(this);
375 }
376
377 void Scene::device_free()
378 {
379         free_memory(false);
380 }
381
382 CCL_NAMESPACE_END
383