Fix Cycles to mostly work with render layer / depsgraph changes.
[blender.git] / intern / cycles / blender / blender_sync.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 "render/background.h"
18 #include "render/camera.h"
19 #include "render/film.h"
20 #include "render/graph.h"
21 #include "render/integrator.h"
22 #include "render/light.h"
23 #include "render/mesh.h"
24 #include "render/nodes.h"
25 #include "render/object.h"
26 #include "render/scene.h"
27 #include "render/shader.h"
28 #include "render/curves.h"
29
30 #include "device/device.h"
31
32 #include "blender/blender_sync.h"
33 #include "blender/blender_session.h"
34 #include "blender/blender_util.h"
35
36 #include "util/util_debug.h"
37 #include "util/util_foreach.h"
38 #include "util/util_opengl.h"
39 #include "util/util_hash.h"
40
41 CCL_NAMESPACE_BEGIN
42
43 /* Constructor */
44
45 BlenderSync::BlenderSync(BL::RenderEngine& b_engine,
46                          BL::BlendData& b_data,
47                          BL::Scene& b_scene,
48                          Scene *scene,
49                          bool preview,
50                          Progress &progress)
51 : b_engine(b_engine),
52   b_data(b_data),
53   b_scene(b_scene),
54   shader_map(&scene->shaders),
55   object_map(&scene->objects),
56   mesh_map(&scene->meshes),
57   light_map(&scene->lights),
58   particle_system_map(&scene->particle_systems),
59   world_map(NULL),
60   world_recalc(false),
61   scene(scene),
62   preview(preview),
63   experimental(false),
64   dicing_rate(1.0f),
65   max_subdivisions(12),
66   progress(progress)
67 {
68         PointerRNA cscene = RNA_pointer_get(&b_scene.ptr, "cycles");
69         dicing_rate = preview ? RNA_float_get(&cscene, "preview_dicing_rate") : RNA_float_get(&cscene, "dicing_rate");
70         max_subdivisions = RNA_int_get(&cscene, "max_subdivisions");
71 }
72
73 BlenderSync::~BlenderSync()
74 {
75 }
76
77 /* Sync */
78
79 bool BlenderSync::sync_recalc()
80 {
81         /* sync recalc flags from blender to cycles. actual update is done separate,
82          * so we can do it later on if doing it immediate is not suitable */
83
84         BL::BlendData::materials_iterator b_mat;
85         bool has_updated_objects = b_data.objects.is_updated();
86         for(b_data.materials.begin(b_mat); b_mat != b_data.materials.end(); ++b_mat) {
87                 if(b_mat->is_updated() || (b_mat->node_tree() && b_mat->node_tree().is_updated())) {
88                         shader_map.set_recalc(*b_mat);
89                 }
90                 else {
91                         Shader *shader = shader_map.find(*b_mat);
92                         if(has_updated_objects && shader != NULL && shader->has_object_dependency) {
93                                 shader_map.set_recalc(*b_mat);
94                         }
95                 }
96         }
97
98         BL::BlendData::lamps_iterator b_lamp;
99
100         for(b_data.lamps.begin(b_lamp); b_lamp != b_data.lamps.end(); ++b_lamp)
101                 if(b_lamp->is_updated() || (b_lamp->node_tree() && b_lamp->node_tree().is_updated()))
102                         shader_map.set_recalc(*b_lamp);
103
104         bool dicing_prop_changed = false;
105
106         if(experimental) {
107                 PointerRNA cscene = RNA_pointer_get(&b_scene.ptr, "cycles");
108
109                 float updated_dicing_rate = preview ? RNA_float_get(&cscene, "preview_dicing_rate")
110                                                     : RNA_float_get(&cscene, "dicing_rate");
111
112                 if(dicing_rate != updated_dicing_rate) {
113                         dicing_rate = updated_dicing_rate;
114                         dicing_prop_changed = true;
115                 }
116
117                 int updated_max_subdivisions = RNA_int_get(&cscene, "max_subdivisions");
118
119                 if(max_subdivisions != updated_max_subdivisions) {
120                         max_subdivisions = updated_max_subdivisions;
121                         dicing_prop_changed = true;
122                 }
123         }
124
125         BL::BlendData::objects_iterator b_ob;
126
127         for(b_data.objects.begin(b_ob); b_ob != b_data.objects.end(); ++b_ob) {
128                 if(b_ob->is_updated()) {
129                         object_map.set_recalc(*b_ob);
130                         light_map.set_recalc(*b_ob);
131                 }
132
133                 if(object_is_mesh(*b_ob)) {
134                         if(b_ob->is_updated_data() || b_ob->data().is_updated() ||
135                            (dicing_prop_changed && object_subdivision_type(*b_ob, preview, experimental) != Mesh::SUBDIVISION_NONE))
136                         {
137                                 BL::ID key = BKE_object_is_modified(*b_ob)? *b_ob: b_ob->data();
138                                 mesh_map.set_recalc(key);
139                         }
140                 }
141                 else if(object_is_light(*b_ob)) {
142                         if(b_ob->is_updated_data() || b_ob->data().is_updated())
143                                 light_map.set_recalc(*b_ob);
144                 }
145                 
146                 if(b_ob->is_updated_data()) {
147                         BL::Object::particle_systems_iterator b_psys;
148                         for(b_ob->particle_systems.begin(b_psys); b_psys != b_ob->particle_systems.end(); ++b_psys)
149                                 particle_system_map.set_recalc(*b_ob);
150                 }
151         }
152
153         BL::BlendData::meshes_iterator b_mesh;
154
155         for(b_data.meshes.begin(b_mesh); b_mesh != b_data.meshes.end(); ++b_mesh) {
156                 if(b_mesh->is_updated()) {
157                         mesh_map.set_recalc(*b_mesh);
158                 }
159         }
160
161         BL::BlendData::worlds_iterator b_world;
162
163         for(b_data.worlds.begin(b_world); b_world != b_data.worlds.end(); ++b_world) {
164                 if(world_map == b_world->ptr.data) {
165                         if(b_world->is_updated() ||
166                            (b_world->node_tree() && b_world->node_tree().is_updated()))
167                         {
168                                 world_recalc = true;
169                         }
170                         else if(b_world->node_tree() && b_world->use_nodes()) {
171                                 Shader *shader = scene->default_background;
172                                 if(has_updated_objects && shader->has_object_dependency) {
173                                         world_recalc = true;
174                                 }
175                         }
176                 }
177         }
178
179         bool recalc =
180                 shader_map.has_recalc() ||
181                 object_map.has_recalc() ||
182                 light_map.has_recalc() ||
183                 mesh_map.has_recalc() ||
184                 particle_system_map.has_recalc() ||
185                 BlendDataObjects_is_updated_get(&b_data.ptr) ||
186                 world_recalc;
187
188         return recalc;
189 }
190
191 void BlenderSync::sync_data(BL::RenderSettings& b_render,
192                                 BL::Depsgraph& b_depsgraph,
193                             BL::SpaceView3D& b_v3d,
194                             BL::Object& b_override,
195                             int width, int height,
196                             void **python_thread_state)
197 {
198         BL::ViewLayer b_view_layer = b_depsgraph.view_layer();
199
200         sync_view_layer(b_v3d, b_view_layer);
201         sync_integrator();
202         sync_film();
203         sync_shaders(b_depsgraph);
204         sync_images();
205         sync_curve_settings();
206
207         mesh_synced.clear(); /* use for objects and motion sync */
208
209         if(scene->need_motion() == Scene::MOTION_PASS ||
210            scene->need_motion() == Scene::MOTION_NONE ||
211            scene->camera->motion_position == Camera::MOTION_POSITION_CENTER)
212         {
213                 sync_objects(b_depsgraph);
214         }
215         sync_motion(b_render,
216                     b_depsgraph,
217                     b_override,
218                     width, height,
219                     python_thread_state);
220
221         mesh_synced.clear();
222 }
223
224 /* Integrator */
225
226 void BlenderSync::sync_integrator()
227 {
228         BL::RenderSettings r = b_scene.render();
229         PointerRNA cscene = RNA_pointer_get(&b_scene.ptr, "cycles");
230
231         experimental = (get_enum(cscene, "feature_set") != 0);
232
233         Integrator *integrator = scene->integrator;
234         Integrator previntegrator = *integrator;
235
236         integrator->max_bounce = get_int(cscene, "max_bounces");
237
238         integrator->max_diffuse_bounce = get_int(cscene, "diffuse_bounces");
239         integrator->max_glossy_bounce = get_int(cscene, "glossy_bounces");
240         integrator->max_transmission_bounce = get_int(cscene, "transmission_bounces");
241         integrator->max_volume_bounce = get_int(cscene, "volume_bounces");
242
243         integrator->transparent_max_bounce = get_int(cscene, "transparent_max_bounces");
244
245         integrator->volume_max_steps = get_int(cscene, "volume_max_steps");
246         integrator->volume_step_size = get_float(cscene, "volume_step_size");
247
248         integrator->caustics_reflective = get_boolean(cscene, "caustics_reflective");
249         integrator->caustics_refractive = get_boolean(cscene, "caustics_refractive");
250         integrator->filter_glossy = get_float(cscene, "blur_glossy");
251
252         integrator->seed = get_int(cscene, "seed");
253         if(get_boolean(cscene, "use_animated_seed")) {
254                 integrator->seed = hash_int_2d(b_scene.frame_current(),
255                                                get_int(cscene, "seed"));
256                 if(b_scene.frame_subframe() != 0.0f) {
257                         /* TODO(sergey): Ideally should be some sort of hash_merge,
258                          * but this is good enough for now.
259                          */
260                         integrator->seed += hash_int_2d((int)(b_scene.frame_subframe() * (float)INT_MAX),
261                                                         get_int(cscene, "seed"));
262                 }
263         }
264
265         integrator->sampling_pattern = (SamplingPattern)get_enum(
266                 cscene,
267                 "sampling_pattern",
268                 SAMPLING_NUM_PATTERNS,
269                 SAMPLING_PATTERN_SOBOL);
270
271         integrator->sample_clamp_direct = get_float(cscene, "sample_clamp_direct");
272         integrator->sample_clamp_indirect = get_float(cscene, "sample_clamp_indirect");
273         if(!preview) {
274                 if(integrator->motion_blur != r.use_motion_blur()) {
275                         scene->object_manager->tag_update(scene);
276                         scene->camera->tag_update();
277                 }
278
279                 integrator->motion_blur = r.use_motion_blur();
280         }
281
282         integrator->method = (Integrator::Method)get_enum(cscene,
283                                                           "progressive",
284                                                           Integrator::NUM_METHODS,
285                                                           Integrator::PATH);
286
287         integrator->sample_all_lights_direct = get_boolean(cscene, "sample_all_lights_direct");
288         integrator->sample_all_lights_indirect = get_boolean(cscene, "sample_all_lights_indirect");
289         integrator->light_sampling_threshold = get_float(cscene, "light_sampling_threshold");
290
291         int diffuse_samples = get_int(cscene, "diffuse_samples");
292         int glossy_samples = get_int(cscene, "glossy_samples");
293         int transmission_samples = get_int(cscene, "transmission_samples");
294         int ao_samples = get_int(cscene, "ao_samples");
295         int mesh_light_samples = get_int(cscene, "mesh_light_samples");
296         int subsurface_samples = get_int(cscene, "subsurface_samples");
297         int volume_samples = get_int(cscene, "volume_samples");
298
299         if(get_boolean(cscene, "use_square_samples")) {
300                 integrator->diffuse_samples = diffuse_samples * diffuse_samples;
301                 integrator->glossy_samples = glossy_samples * glossy_samples;
302                 integrator->transmission_samples = transmission_samples * transmission_samples;
303                 integrator->ao_samples = ao_samples * ao_samples;
304                 integrator->mesh_light_samples = mesh_light_samples * mesh_light_samples;
305                 integrator->subsurface_samples = subsurface_samples * subsurface_samples;
306                 integrator->volume_samples = volume_samples * volume_samples;
307         } 
308         else {
309                 integrator->diffuse_samples = diffuse_samples;
310                 integrator->glossy_samples = glossy_samples;
311                 integrator->transmission_samples = transmission_samples;
312                 integrator->ao_samples = ao_samples;
313                 integrator->mesh_light_samples = mesh_light_samples;
314                 integrator->subsurface_samples = subsurface_samples;
315                 integrator->volume_samples = volume_samples;
316         }
317
318         if(b_scene.render().use_simplify()) {
319                 if(preview) {
320                         integrator->ao_bounces = get_int(cscene, "ao_bounces");
321                 }
322                 else {
323                         integrator->ao_bounces = get_int(cscene, "ao_bounces_render");
324                 }
325         }
326         else {
327                 integrator->ao_bounces = 0;
328         }
329
330         if(integrator->modified(previntegrator))
331                 integrator->tag_update(scene);
332 }
333
334 /* Film */
335
336 void BlenderSync::sync_film()
337 {
338         PointerRNA cscene = RNA_pointer_get(&b_scene.ptr, "cycles");
339
340         Film *film = scene->film;
341         Film prevfilm = *film;
342         
343         film->exposure = get_float(cscene, "film_exposure");
344         film->filter_type = (FilterType)get_enum(cscene,
345                                                  "pixel_filter_type",
346                                                  FILTER_NUM_TYPES,
347                                                  FILTER_BLACKMAN_HARRIS);
348         film->filter_width = (film->filter_type == FILTER_BOX)? 1.0f: get_float(cscene, "filter_width");
349
350         if(b_scene.world()) {
351                 BL::WorldMistSettings b_mist = b_scene.world().mist_settings();
352
353                 film->mist_start = b_mist.start();
354                 film->mist_depth = b_mist.depth();
355
356                 switch(b_mist.falloff()) {
357                         case BL::WorldMistSettings::falloff_QUADRATIC:
358                                 film->mist_falloff = 2.0f;
359                                 break;
360                         case BL::WorldMistSettings::falloff_LINEAR:
361                                 film->mist_falloff = 1.0f;
362                                 break;
363                         case BL::WorldMistSettings::falloff_INVERSE_QUADRATIC:
364                                 film->mist_falloff = 0.5f;
365                                 break;
366                 }
367         }
368
369         if(film->modified(prevfilm))
370                 film->tag_update(scene);
371 }
372
373 /* Render Layer */
374
375 void BlenderSync::sync_view_layer(BL::SpaceView3D& /*b_v3d*/, BL::ViewLayer& b_view_layer)
376 {
377         /* render layer */
378         uint layer_override = get_layer(b_engine.layer_override());
379         uint view_layers = layer_override ? layer_override : get_layer(b_scene.layers());
380
381         view_layer.name = b_view_layer.name();
382
383         view_layer.holdout_layer = 0;
384         view_layer.exclude_layer = 0;
385
386         view_layer.view_layer = view_layers & ~view_layer.exclude_layer;
387         view_layer.view_layer |= view_layer.exclude_layer & view_layer.holdout_layer;
388
389         view_layer.layer = (1 << 20) - 1;
390         view_layer.layer |= view_layer.holdout_layer;
391
392         view_layer.material_override = PointerRNA_NULL;
393         view_layer.use_background_shader = b_view_layer.use_sky();
394         view_layer.use_background_ao = b_view_layer.use_ao();
395         view_layer.use_surfaces = b_view_layer.use_solid();
396         view_layer.use_hair = b_view_layer.use_strand();
397
398         view_layer.bound_samples = false;
399         view_layer.samples = 0;
400 }
401
402 /* Images */
403 void BlenderSync::sync_images()
404 {
405         /* Sync is a convention for this API, but currently it frees unused buffers. */
406
407         const bool is_interface_locked = b_engine.render() &&
408                                          b_engine.render().use_lock_interface();
409         if(is_interface_locked == false && BlenderSession::headless == false) {
410                 /* If interface is not locked, it's possible image is needed for
411                  * the display.
412                  */
413                 return;
414         }
415         /* Free buffers used by images which are not needed for render. */
416         BL::BlendData::images_iterator b_image;
417         for(b_data.images.begin(b_image);
418             b_image != b_data.images.end();
419             ++b_image)
420         {
421                 /* TODO(sergey): Consider making it an utility function to check
422                  * whether image is considered builtin.
423                  */
424                 const bool is_builtin = b_image->packed_file() ||
425                                         b_image->source() == BL::Image::source_GENERATED ||
426                                         b_image->source() == BL::Image::source_MOVIE ||
427                                         b_engine.is_preview();
428                 if(is_builtin == false) {
429                         b_image->buffers_free();
430                 }
431                 /* TODO(sergey): Free builtin images not used by any shader. */
432         }
433 }
434
435 /* Passes */
436 PassType BlenderSync::get_pass_type(BL::RenderPass& b_pass)
437 {
438         string name = b_pass.name();
439 #define MAP_PASS(passname, passtype) if(name == passname) return passtype;
440         /* NOTE: Keep in sync with defined names from DNA_scene_types.h */
441         MAP_PASS("Combined", PASS_COMBINED);
442         MAP_PASS("Depth", PASS_DEPTH);
443         MAP_PASS("Mist", PASS_MIST);
444         MAP_PASS("Normal", PASS_NORMAL);
445         MAP_PASS("IndexOB", PASS_OBJECT_ID);
446         MAP_PASS("UV", PASS_UV);
447         MAP_PASS("Vector", PASS_MOTION);
448         MAP_PASS("IndexMA", PASS_MATERIAL_ID);
449
450         MAP_PASS("DiffDir", PASS_DIFFUSE_DIRECT);
451         MAP_PASS("GlossDir", PASS_GLOSSY_DIRECT);
452         MAP_PASS("TransDir", PASS_TRANSMISSION_DIRECT);
453         MAP_PASS("SubsurfaceDir", PASS_SUBSURFACE_DIRECT);
454         MAP_PASS("VolumeDir", PASS_VOLUME_DIRECT);
455
456         MAP_PASS("DiffInd", PASS_DIFFUSE_INDIRECT);
457         MAP_PASS("GlossInd", PASS_GLOSSY_INDIRECT);
458         MAP_PASS("TransInd", PASS_TRANSMISSION_INDIRECT);
459         MAP_PASS("SubsurfaceInd", PASS_SUBSURFACE_INDIRECT);
460         MAP_PASS("VolumeInd", PASS_VOLUME_INDIRECT);
461
462         MAP_PASS("DiffCol", PASS_DIFFUSE_COLOR);
463         MAP_PASS("GlossCol", PASS_GLOSSY_COLOR);
464         MAP_PASS("TransCol", PASS_TRANSMISSION_COLOR);
465         MAP_PASS("SubsurfaceCol", PASS_SUBSURFACE_COLOR);
466
467         MAP_PASS("Emit", PASS_EMISSION);
468         MAP_PASS("Env", PASS_BACKGROUND);
469         MAP_PASS("AO", PASS_AO);
470         MAP_PASS("Shadow", PASS_SHADOW);
471
472 #ifdef __KERNEL_DEBUG__
473         MAP_PASS("Debug BVH Traversed Nodes", PASS_BVH_TRAVERSED_NODES);
474         MAP_PASS("Debug BVH Traversed Instances", PASS_BVH_TRAVERSED_INSTANCES);
475         MAP_PASS("Debug BVH Intersections", PASS_BVH_INTERSECTIONS);
476         MAP_PASS("Debug Ray Bounces", PASS_RAY_BOUNCES);
477 #endif
478         MAP_PASS("Debug Render Time", PASS_RENDER_TIME);
479 #undef MAP_PASS
480
481         return PASS_NONE;
482 }
483
484 int BlenderSync::get_denoising_pass(BL::RenderPass& b_pass)
485 {
486         string name = b_pass.name();
487         if(name.substr(0, 10) != "Denoising ") {
488                 return -1;
489         }
490         name = name.substr(10);
491
492 #define MAP_PASS(passname, offset) if(name == passname) return offset;
493         MAP_PASS("Normal", DENOISING_PASS_NORMAL);
494         MAP_PASS("Normal Variance", DENOISING_PASS_NORMAL_VAR);
495         MAP_PASS("Albedo", DENOISING_PASS_ALBEDO);
496         MAP_PASS("Albedo Variance", DENOISING_PASS_ALBEDO_VAR);
497         MAP_PASS("Depth", DENOISING_PASS_DEPTH);
498         MAP_PASS("Depth Variance", DENOISING_PASS_DEPTH_VAR);
499         MAP_PASS("Shadow A", DENOISING_PASS_SHADOW_A);
500         MAP_PASS("Shadow B", DENOISING_PASS_SHADOW_B);
501         MAP_PASS("Image", DENOISING_PASS_COLOR);
502         MAP_PASS("Image Variance", DENOISING_PASS_COLOR_VAR);
503 #undef MAP_PASS
504
505         return -1;
506 }
507
508 array<Pass> BlenderSync::sync_render_passes(BL::RenderLayer& b_rlay,
509                                             BL::ViewLayer& b_view_layer,
510                                             const SessionParams &session_params)
511 {
512         array<Pass> passes;
513         Pass::add(PASS_COMBINED, passes);
514
515         if(!session_params.device.advanced_shading) {
516                 return passes;
517         }
518
519         /* loop over passes */
520         BL::RenderLayer::passes_iterator b_pass_iter;
521
522         for(b_rlay.passes.begin(b_pass_iter); b_pass_iter != b_rlay.passes.end(); ++b_pass_iter) {
523                 BL::RenderPass b_pass(*b_pass_iter);
524                 PassType pass_type = get_pass_type(b_pass);
525
526                 if(pass_type == PASS_MOTION && scene->integrator->motion_blur)
527                         continue;
528                 if(pass_type != PASS_NONE)
529                         Pass::add(pass_type, passes);
530         }
531
532         PointerRNA crp = RNA_pointer_get(&b_view_layer.ptr, "cycles");
533         if(get_boolean(crp, "denoising_store_passes") &&
534            get_boolean(crp, "use_denoising"))
535         {
536                 b_engine.add_pass("Denoising Normal",          3, "XYZ", b_view_layer.name().c_str());
537                 b_engine.add_pass("Denoising Normal Variance", 3, "XYZ", b_view_layer.name().c_str());
538                 b_engine.add_pass("Denoising Albedo",          3, "RGB", b_view_layer.name().c_str());
539                 b_engine.add_pass("Denoising Albedo Variance", 3, "RGB", b_view_layer.name().c_str());
540                 b_engine.add_pass("Denoising Depth",           1, "Z",   b_view_layer.name().c_str());
541                 b_engine.add_pass("Denoising Depth Variance",  1, "Z",   b_view_layer.name().c_str());
542                 b_engine.add_pass("Denoising Shadow A",        3, "XYV", b_view_layer.name().c_str());
543                 b_engine.add_pass("Denoising Shadow B",        3, "XYV", b_view_layer.name().c_str());
544                 b_engine.add_pass("Denoising Image",           3, "RGB", b_view_layer.name().c_str());
545                 b_engine.add_pass("Denoising Image Variance",  3, "RGB", b_view_layer.name().c_str());
546         }
547 #ifdef __KERNEL_DEBUG__
548         if(get_boolean(crp, "pass_debug_bvh_traversed_nodes")) {
549                 b_engine.add_pass("Debug BVH Traversed Nodes", 1, "X", b_view_layer.name().c_str());
550                 Pass::add(PASS_BVH_TRAVERSED_NODES, passes);
551         }
552         if(get_boolean(crp, "pass_debug_bvh_traversed_instances")) {
553                 b_engine.add_pass("Debug BVH Traversed Instances", 1, "X", b_view_layer.name().c_str());
554                 Pass::add(PASS_BVH_TRAVERSED_INSTANCES, passes);
555         }
556         if(get_boolean(crp, "pass_debug_bvh_intersections")) {
557                 b_engine.add_pass("Debug BVH Intersections", 1, "X", b_view_layer.name().c_str());
558                 Pass::add(PASS_BVH_INTERSECTIONS, passes);
559         }
560         if(get_boolean(crp, "pass_debug_ray_bounces")) {
561                 b_engine.add_pass("Debug Ray Bounces", 1, "X", b_view_layer.name().c_str());
562                 Pass::add(PASS_RAY_BOUNCES, passes);
563         }
564 #endif
565         if(get_boolean(crp, "pass_debug_render_time")) {
566                 b_engine.add_pass("Debug Render Time", 1, "X", b_view_layer.name().c_str());
567                 Pass::add(PASS_RENDER_TIME, passes);
568         }
569         if(get_boolean(crp, "use_pass_volume_direct")) {
570                 b_engine.add_pass("VolumeDir", 3, "RGB", b_view_layer.name().c_str());
571                 Pass::add(PASS_VOLUME_DIRECT, passes);
572         }
573         if(get_boolean(crp, "use_pass_volume_indirect")) {
574                 b_engine.add_pass("VolumeInd", 3, "RGB", b_view_layer.name().c_str());
575                 Pass::add(PASS_VOLUME_INDIRECT, passes);
576         }
577
578         return passes;
579 }
580
581 /* Scene Parameters */
582
583 SceneParams BlenderSync::get_scene_params(BL::Scene& b_scene,
584                                           bool background)
585 {
586         BL::RenderSettings r = b_scene.render();
587         SceneParams params;
588         PointerRNA cscene = RNA_pointer_get(&b_scene.ptr, "cycles");
589         const bool shadingsystem = RNA_boolean_get(&cscene, "shading_system");
590
591         if(shadingsystem == 0)
592                 params.shadingsystem = SHADINGSYSTEM_SVM;
593         else if(shadingsystem == 1)
594                 params.shadingsystem = SHADINGSYSTEM_OSL;
595         
596         if(background || DebugFlags().viewport_static_bvh)
597                 params.bvh_type = SceneParams::BVH_STATIC;
598         else
599                 params.bvh_type = SceneParams::BVH_DYNAMIC;
600
601         params.use_bvh_spatial_split = RNA_boolean_get(&cscene, "debug_use_spatial_splits");
602         params.use_bvh_unaligned_nodes = RNA_boolean_get(&cscene, "debug_use_hair_bvh");
603         params.num_bvh_time_steps = RNA_int_get(&cscene, "debug_bvh_time_steps");
604
605         if(background && params.shadingsystem != SHADINGSYSTEM_OSL)
606                 params.persistent_data = r.use_persistent_data();
607         else
608                 params.persistent_data = false;
609
610         int texture_limit;
611         if(background) {
612                 texture_limit = RNA_enum_get(&cscene, "texture_limit_render");
613         }
614         else {
615                 texture_limit = RNA_enum_get(&cscene, "texture_limit");
616         }
617         if(texture_limit > 0 && b_scene.render().use_simplify()) {
618                 params.texture_limit = 1 << (texture_limit + 6);
619         }
620         else {
621                 params.texture_limit = 0;
622         }
623
624         params.bvh_layout = DebugFlags().cpu.bvh_layout;
625
626         return params;
627 }
628
629 /* Session Parameters */
630
631 bool BlenderSync::get_session_pause(BL::Scene& b_scene, bool background)
632 {
633         PointerRNA cscene = RNA_pointer_get(&b_scene.ptr, "cycles");
634         return (background)? false: get_boolean(cscene, "preview_pause");
635 }
636
637 SessionParams BlenderSync::get_session_params(BL::RenderEngine& b_engine,
638                                               BL::UserPreferences& b_userpref,
639                                               BL::Scene& b_scene,
640                                               bool background)
641 {
642         SessionParams params;
643         PointerRNA cscene = RNA_pointer_get(&b_scene.ptr, "cycles");
644
645         /* feature set */
646         params.experimental = (get_enum(cscene, "feature_set") != 0);
647
648         /* threads */
649         BL::RenderSettings b_r = b_scene.render();
650         if(b_r.threads_mode() == BL::RenderSettings::threads_mode_FIXED)
651                 params.threads = b_r.threads();
652         else
653                 params.threads = 0;
654
655         /* Background */
656         params.background = background;
657
658         /* device type */
659         vector<DeviceInfo>& devices = Device::available_devices();
660         
661         /* device default CPU */
662         foreach(DeviceInfo& device, devices) {
663                 if(device.type == DEVICE_CPU) {
664                         params.device = device;
665                         break;
666                 }
667         }
668
669         if(get_enum(cscene, "device") == 2) {
670                 /* find network device */
671                 foreach(DeviceInfo& info, devices)
672                         if(info.type == DEVICE_NETWORK)
673                                 params.device = info;
674         }
675         else if(get_enum(cscene, "device") == 1) {
676                 PointerRNA b_preferences;
677
678                 BL::UserPreferences::addons_iterator b_addon_iter;
679                 for(b_userpref.addons.begin(b_addon_iter); b_addon_iter != b_userpref.addons.end(); ++b_addon_iter) {
680                         if(b_addon_iter->module() == "cycles") {
681                                 b_preferences = b_addon_iter->preferences().ptr;
682                                 break;
683                         }
684                 }
685
686                 enum ComputeDevice {
687                         COMPUTE_DEVICE_CPU = 0,
688                         COMPUTE_DEVICE_CUDA = 1,
689                         COMPUTE_DEVICE_OPENCL = 2,
690                         COMPUTE_DEVICE_NUM = 3,
691                 };
692
693                 ComputeDevice compute_device = (ComputeDevice)get_enum(b_preferences,
694                                                                        "compute_device_type",
695                                                                        COMPUTE_DEVICE_NUM,
696                                                                        COMPUTE_DEVICE_CPU);
697
698                 if(compute_device != COMPUTE_DEVICE_CPU) {
699                         vector<DeviceInfo> used_devices;
700                         RNA_BEGIN(&b_preferences, device, "devices") {
701                                 ComputeDevice device_type = (ComputeDevice)get_enum(device,
702                                                                                     "type",
703                                                                                     COMPUTE_DEVICE_NUM,
704                                                                                     COMPUTE_DEVICE_CPU);
705
706                                 if(get_boolean(device, "use") &&
707                                    (device_type == compute_device || device_type == COMPUTE_DEVICE_CPU)) {
708                                         string id = get_string(device, "id");
709                                         foreach(DeviceInfo& info, devices) {
710                                                 if(info.id == id) {
711                                                         used_devices.push_back(info);
712                                                         break;
713                                                 }
714                                         }
715                                 }
716                         } RNA_END
717
718                         if(used_devices.size() == 1) {
719                                 params.device = used_devices[0];
720                         }
721                         else if(used_devices.size() > 1) {
722                                 params.device = Device::get_multi_device(used_devices,
723                                                                          params.threads,
724                                                                          params.background);
725                         }
726                         /* Else keep using the CPU device that was set before. */
727                 }
728         }
729
730         /* samples */
731         int samples = get_int(cscene, "samples");
732         int aa_samples = get_int(cscene, "aa_samples");
733         int preview_samples = get_int(cscene, "preview_samples");
734         int preview_aa_samples = get_int(cscene, "preview_aa_samples");
735         
736         if(get_boolean(cscene, "use_square_samples")) {
737                 aa_samples = aa_samples * aa_samples;
738                 preview_aa_samples = preview_aa_samples * preview_aa_samples;
739
740                 samples = samples * samples;
741                 preview_samples = preview_samples * preview_samples;
742         }
743
744         if(get_enum(cscene, "progressive") == 0) {
745                 if(background) {
746                         params.samples = aa_samples;
747                 }
748                 else {
749                         params.samples = preview_aa_samples;
750                         if(params.samples == 0)
751                                 params.samples = INT_MAX;
752                 }
753         }
754         else {
755                 if(background) {
756                         params.samples = samples;
757                 }
758                 else {
759                         params.samples = preview_samples;
760                         if(params.samples == 0)
761                                 params.samples = INT_MAX;
762                 }
763         }
764
765         /* tiles */
766         if(params.device.type != DEVICE_CPU && !background) {
767                 /* currently GPU could be much slower than CPU when using tiles,
768                  * still need to be investigated, but meanwhile make it possible
769                  * to work in viewport smoothly
770                  */
771                 int debug_tile_size = get_int(cscene, "debug_tile_size");
772
773                 params.tile_size = make_int2(debug_tile_size, debug_tile_size);
774         }
775         else {
776                 int tile_x = b_engine.tile_x();
777                 int tile_y = b_engine.tile_y();
778
779                 params.tile_size = make_int2(tile_x, tile_y);
780         }
781
782         if((BlenderSession::headless == false) && background) {
783                 params.tile_order = (TileOrder)get_enum(cscene, "tile_order");
784         }
785         else {
786                 params.tile_order = TILE_BOTTOM_TO_TOP;
787         }
788
789         /* other parameters */
790         params.start_resolution = get_int(cscene, "preview_start_resolution");
791         params.pixel_size = b_engine.get_preview_pixel_size(b_scene);
792
793         /* other parameters */
794         params.cancel_timeout = (double)get_float(cscene, "debug_cancel_timeout");
795         params.reset_timeout = (double)get_float(cscene, "debug_reset_timeout");
796         params.text_timeout = (double)get_float(cscene, "debug_text_timeout");
797
798         /* progressive refine */
799         params.progressive_refine = get_boolean(cscene, "use_progressive_refine") &&
800                                     !b_r.use_save_buffers();
801
802         if(params.progressive_refine) {
803                 BL::Scene::view_layers_iterator b_view_layer;
804                 for(b_scene.view_layers.begin(b_view_layer); b_view_layer != b_scene.view_layers.end(); ++b_view_layer) {
805                         PointerRNA crl = RNA_pointer_get(&b_view_layer->ptr, "cycles");
806                         if(get_boolean(crl, "use_denoising")) {
807                                 params.progressive_refine = false;
808                         }
809                 }
810         }
811
812         if(background) {
813                 if(params.progressive_refine)
814                         params.progressive = true;
815                 else
816                         params.progressive = false;
817
818                 params.start_resolution = INT_MAX;
819                 params.pixel_size = 1;
820         }
821         else
822                 params.progressive = true;
823
824         /* shading system - scene level needs full refresh */
825         const bool shadingsystem = RNA_boolean_get(&cscene, "shading_system");
826
827         if(shadingsystem == 0)
828                 params.shadingsystem = SHADINGSYSTEM_SVM;
829         else if(shadingsystem == 1)
830                 params.shadingsystem = SHADINGSYSTEM_OSL;
831         
832         /* color managagement */
833         params.display_buffer_linear = GLEW_ARB_half_float_pixel &&
834                                        b_engine.support_display_space_shader(b_scene);
835
836         if(b_engine.is_preview()) {
837                 /* For preview rendering we're using same timeout as
838                  * blender's job update.
839                  */
840                 params.progressive_update_timeout = 0.1;
841         }
842
843         return params;
844 }
845
846 CCL_NAMESPACE_END
847