Merge branch 'master' into blender2.8
[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         : name("Scene"),
87           device(device),
88           dscene(device),
89           params(params_)
90 {
91         memset((void *)&dscene.data, 0, sizeof(dscene.data));
92
93         camera = new Camera();
94         dicing_camera = new Camera();
95         lookup_tables = new LookupTables();
96         film = new Film();
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();
106
107         /* OSL only works on the CPU */
108         if(device->info.has_osl)
109                 shader_manager = ShaderManager::create(this, params.shadingsystem);
110         else
111                 shader_manager = ShaderManager::create(this, SHADINGSYSTEM_SVM);
112 }
113
114 Scene::~Scene()
115 {
116         free_memory(true);
117 }
118
119 void Scene::free_memory(bool final)
120 {
121         foreach(Shader *s, shaders)
122                 delete s;
123         foreach(Mesh *m, meshes)
124                 delete m;
125         foreach(Object *o, objects)
126                 delete o;
127         foreach(Light *l, lights)
128                 delete l;
129         foreach(ParticleSystem *p, particle_systems)
130                 delete p;
131
132         shaders.clear();
133         meshes.clear();
134         objects.clear();
135         lights.clear();
136         particle_systems.clear();
137
138         if(device) {
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);
143
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);
148
149                 particle_system_manager->device_free(device, &dscene);
150                 curve_system_manager->device_free(device, &dscene);
151
152                 bake_manager->device_free(device, &dscene);
153
154                 if(!params.persistent_data || final)
155                         image_manager->device_free(device);
156                 else
157                         image_manager->device_free_builtin(device);
158
159                 lookup_tables->device_free(device, &dscene);
160         }
161
162         if(final) {
163                 delete lookup_tables;
164                 delete camera;
165                 delete dicing_camera;
166                 delete film;
167                 delete background;
168                 delete integrator;
169                 delete object_manager;
170                 delete mesh_manager;
171                 delete shader_manager;
172                 delete light_manager;
173                 delete particle_system_manager;
174                 delete curve_system_manager;
175                 delete image_manager;
176                 delete bake_manager;
177         }
178 }
179
180 void Scene::device_update(Device *device_, Progress& progress)
181 {
182         if(!device)
183                 device = device_;
184
185         bool print_stats = need_data_update();
186
187         /* The order of updates is important, because there's dependencies between
188          * the different managers, using data computed by previous managers.
189          *
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
196          */
197
198         progress.set_status("Updating Shaders");
199         shader_manager->device_update(device, &dscene, this, progress);
200
201         if(progress.get_cancel() || device->have_error()) return;
202
203         progress.set_status("Updating Background");
204         background->device_update(device, &dscene, this);
205
206         if(progress.get_cancel() || device->have_error()) return;
207
208         progress.set_status("Updating Camera");
209         camera->device_update(device, &dscene, this);
210
211         if(progress.get_cancel() || device->have_error()) return;
212
213         mesh_manager->device_update_preprocess(device, this, progress);
214
215         if(progress.get_cancel() || device->have_error()) return;
216
217         progress.set_status("Updating Objects");
218         object_manager->device_update(device, &dscene, this, progress);
219
220         if(progress.get_cancel() || device->have_error()) return;
221
222         progress.set_status("Updating Hair Systems");
223         curve_system_manager->device_update(device, &dscene, this, progress);
224
225         if(progress.get_cancel() || device->have_error()) return;
226
227         progress.set_status("Updating Particle Systems");
228         particle_system_manager->device_update(device, &dscene, this, progress);
229
230         if(progress.get_cancel() || device->have_error()) return;
231
232         progress.set_status("Updating Meshes");
233         mesh_manager->device_update(device, &dscene, this, progress);
234
235         if(progress.get_cancel() || device->have_error()) return;
236
237         progress.set_status("Updating Objects Flags");
238         object_manager->device_update_flags(device, &dscene, this, progress);
239
240         if(progress.get_cancel() || device->have_error()) return;
241
242         progress.set_status("Updating Images");
243         image_manager->device_update(device, this, progress);
244
245         if(progress.get_cancel() || device->have_error()) return;
246
247         progress.set_status("Updating Camera Volume");
248         camera->device_update_volume(device, &dscene, this);
249
250         if(progress.get_cancel() || device->have_error()) return;
251
252         progress.set_status("Updating Lookup Tables");
253         lookup_tables->device_update(device, &dscene);
254
255         if(progress.get_cancel() || device->have_error()) return;
256
257         progress.set_status("Updating Lights");
258         light_manager->device_update(device, &dscene, this, progress);
259
260         if(progress.get_cancel() || device->have_error()) return;
261
262         progress.set_status("Updating Integrator");
263         integrator->device_update(device, &dscene, this);
264
265         if(progress.get_cancel() || device->have_error()) return;
266
267         progress.set_status("Updating Film");
268         film->device_update(device, &dscene, this);
269
270         if(progress.get_cancel() || device->have_error()) return;
271
272         progress.set_status("Updating Lookup Tables");
273         lookup_tables->device_update(device, &dscene);
274
275         if(progress.get_cancel() || device->have_error()) return;
276
277         progress.set_status("Updating Baking");
278         bake_manager->device_update(device, &dscene, this, progress);
279
280         if(progress.get_cancel() || device->have_error()) return;
281
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));
285         }
286
287         if(print_stats) {
288                 size_t mem_used = util_guarded_get_mem_used();
289                 size_t mem_peak = util_guarded_get_mem_peak();
290
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) << ")";
296         }
297 }
298
299 Scene::MotionType Scene::need_motion()
300 {
301         if(integrator->motion_blur)
302                 return MOTION_BLUR;
303         else if(Pass::contains(film->passes, PASS_MOTION))
304                 return MOTION_PASS;
305         else
306                 return MOTION_NONE;
307 }
308
309 float Scene::motion_shutter_time()
310 {
311         if(need_motion() == Scene::MOTION_PASS)
312                 return 2.0f;
313         else
314                 return camera->shuttertime;
315 }
316
317 bool Scene::need_global_attribute(AttributeStandard std)
318 {
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;
325
326         return false;
327 }
328
329 void Scene::need_global_attributes(AttributeRequestSet& attributes)
330 {
331         for(int std = ATTR_STD_NONE; std < ATTR_STD_NUM; std++)
332                 if(need_global_attribute((AttributeStandard)std))
333                         attributes.add((AttributeStandard)std);
334 }
335
336 bool Scene::need_update()
337 {
338         return (need_reset() || film->need_update);
339 }
340
341 bool Scene::need_data_update()
342 {
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);
355 }
356
357 bool Scene::need_reset()
358 {
359         return need_data_update() || camera->need_update;
360 }
361
362 void Scene::reset()
363 {
364         shader_manager->reset(this);
365         shader_manager->add_default(this);
366
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);
378 }
379
380 void Scene::device_free()
381 {
382         free_memory(false);
383 }
384
385 void Scene::collect_statistics(RenderStats *stats)
386 {
387         mesh_manager->collect_statistics(this, stats);
388         image_manager->collect_statistics(stats);
389 }
390
391 CCL_NAMESPACE_END