Merge branch 'master' into blender2.8
[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::Depsgraph& b_depsgraph,
48                          BL::Scene& b_scene,
49                          Scene *scene,
50                          bool preview,
51                          Progress &progress)
52 : b_engine(b_engine),
53   b_data(b_data),
54   b_depsgraph(b_depsgraph),
55   b_scene(b_scene),
56   b_view_layer(b_engine.view_layer()),
57   shader_map(&scene->shaders),
58   object_map(&scene->objects),
59   mesh_map(&scene->meshes),
60   light_map(&scene->lights),
61   particle_system_map(&scene->particle_systems),
62   world_map(NULL),
63   world_recalc(false),
64   scene(scene),
65   preview(preview),
66   experimental(false),
67   dicing_rate(1.0f),
68   max_subdivisions(12),
69   progress(progress)
70 {
71         PointerRNA cscene = RNA_pointer_get(&b_scene.ptr, "cycles");
72         dicing_rate = preview ? RNA_float_get(&cscene, "preview_dicing_rate") : RNA_float_get(&cscene, "dicing_rate");
73         max_subdivisions = RNA_int_get(&cscene, "max_subdivisions");
74 }
75
76 BlenderSync::~BlenderSync()
77 {
78 }
79
80 /* Sync */
81
82 bool BlenderSync::sync_recalc()
83 {
84         /* sync recalc flags from blender to cycles. actual update is done separate,
85          * so we can do it later on if doing it immediate is not suitable */
86
87         BL::BlendData::materials_iterator b_mat;
88         bool has_updated_objects = b_data.objects.is_updated();
89         for(b_data.materials.begin(b_mat); b_mat != b_data.materials.end(); ++b_mat) {
90                 if(b_mat->is_updated() || (b_mat->node_tree() && b_mat->node_tree().is_updated())) {
91                         shader_map.set_recalc(*b_mat);
92                 }
93                 else {
94                         Shader *shader = shader_map.find(*b_mat);
95                         if(has_updated_objects && shader != NULL && shader->has_object_dependency) {
96                                 shader_map.set_recalc(*b_mat);
97                         }
98                 }
99         }
100
101         BL::BlendData::lamps_iterator b_lamp;
102
103         for(b_data.lamps.begin(b_lamp); b_lamp != b_data.lamps.end(); ++b_lamp)
104                 if(b_lamp->is_updated() || (b_lamp->node_tree() && b_lamp->node_tree().is_updated()))
105                         shader_map.set_recalc(*b_lamp);
106
107         bool dicing_prop_changed = false;
108
109         if(experimental) {
110                 PointerRNA cscene = RNA_pointer_get(&b_scene.ptr, "cycles");
111
112                 float updated_dicing_rate = preview ? RNA_float_get(&cscene, "preview_dicing_rate")
113                                                     : RNA_float_get(&cscene, "dicing_rate");
114
115                 if(dicing_rate != updated_dicing_rate) {
116                         dicing_rate = updated_dicing_rate;
117                         dicing_prop_changed = true;
118                 }
119
120                 int updated_max_subdivisions = RNA_int_get(&cscene, "max_subdivisions");
121
122                 if(max_subdivisions != updated_max_subdivisions) {
123                         max_subdivisions = updated_max_subdivisions;
124                         dicing_prop_changed = true;
125                 }
126         }
127
128         BL::BlendData::objects_iterator b_ob;
129
130         for(b_data.objects.begin(b_ob); b_ob != b_data.objects.end(); ++b_ob) {
131                 if(b_ob->is_updated()) {
132                         object_map.set_recalc(*b_ob);
133                         light_map.set_recalc(*b_ob);
134                 }
135
136                 if(object_is_mesh(*b_ob)) {
137                         if(b_ob->is_updated_data() || b_ob->data().is_updated() ||
138                            (dicing_prop_changed && object_subdivision_type(*b_ob, preview, experimental) != Mesh::SUBDIVISION_NONE))
139                         {
140                                 BL::ID key = BKE_object_is_modified(*b_ob)? *b_ob: b_ob->data();
141                                 mesh_map.set_recalc(key);
142                         }
143                 }
144                 else if(object_is_light(*b_ob)) {
145                         if(b_ob->is_updated_data() || b_ob->data().is_updated())
146                                 light_map.set_recalc(*b_ob);
147                 }
148                 
149                 if(b_ob->is_updated_data()) {
150                         BL::Object::particle_systems_iterator b_psys;
151                         for(b_ob->particle_systems.begin(b_psys); b_psys != b_ob->particle_systems.end(); ++b_psys)
152                                 particle_system_map.set_recalc(*b_ob);
153                 }
154         }
155
156         BL::BlendData::meshes_iterator b_mesh;
157
158         for(b_data.meshes.begin(b_mesh); b_mesh != b_data.meshes.end(); ++b_mesh) {
159                 if(b_mesh->is_updated()) {
160                         mesh_map.set_recalc(*b_mesh);
161                 }
162         }
163
164         BL::BlendData::worlds_iterator b_world;
165
166         for(b_data.worlds.begin(b_world); b_world != b_data.worlds.end(); ++b_world) {
167                 if(world_map == b_world->ptr.data) {
168                         if(b_world->is_updated() ||
169                            (b_world->node_tree() && b_world->node_tree().is_updated()))
170                         {
171                                 world_recalc = true;
172                         }
173                         else if(b_world->node_tree() && b_world->use_nodes()) {
174                                 Shader *shader = scene->default_background;
175                                 if(has_updated_objects && shader->has_object_dependency) {
176                                         world_recalc = true;
177                                 }
178                         }
179                 }
180         }
181
182         bool recalc =
183                 shader_map.has_recalc() ||
184                 object_map.has_recalc() ||
185                 light_map.has_recalc() ||
186                 mesh_map.has_recalc() ||
187                 particle_system_map.has_recalc() ||
188                 BlendDataObjects_is_updated_get(&b_data.ptr) ||
189                 world_recalc;
190
191         return recalc;
192 }
193
194 void BlenderSync::sync_data(BL::RenderSettings& b_render,
195                             BL::SpaceView3D& b_v3d,
196                             BL::Object& b_override,
197                             int width, int height,
198                             void **python_thread_state,
199                             const char *layer)
200 {
201         sync_view_layers(b_v3d, layer);
202         sync_integrator();
203         sync_film();
204         sync_shaders();
205         sync_images();
206         sync_curve_settings();
207
208         mesh_synced.clear(); /* use for objects and motion sync */
209
210         if(scene->need_motion() == Scene::MOTION_PASS ||
211            scene->need_motion() == Scene::MOTION_NONE ||
212            scene->camera->motion_position == Camera::MOTION_POSITION_CENTER)
213         {
214                 sync_objects();
215         }
216         sync_motion(b_render,
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_layers(BL::SpaceView3D& b_v3d, const char *layer)
376 {
377         string layername;
378
379         /* 3d view */
380         if(b_v3d) {
381                 layername = b_scene.view_layers.active().name();
382                 layer = layername.c_str();
383         }
384
385         /* render layer */
386         BL::Scene::view_layers_iterator b_view_layer;
387         bool first_layer = true;
388         uint layer_override = get_layer(b_engine.layer_override());
389         uint view_layers = layer_override ? layer_override : get_layer(b_scene.layers());
390
391         for(b_scene.view_layers.begin(b_view_layer); b_view_layer != b_scene.view_layers.end(); ++b_view_layer) {
392                 if((!layer && first_layer) || (layer && b_view_layer->name() == layer)) {
393                         view_layer.name = b_view_layer->name();
394
395                         view_layer.holdout_layer = 0;
396                         view_layer.exclude_layer = 0;
397
398                         view_layer.view_layer = view_layers & ~view_layer.exclude_layer;
399                         view_layer.view_layer |= view_layer.exclude_layer & view_layer.holdout_layer;
400
401                         view_layer.layer = (1 << 20) - 1;
402                         view_layer.layer |= view_layer.holdout_layer;
403
404                         view_layer.material_override = PointerRNA_NULL;
405                         view_layer.use_background_shader = b_view_layer->use_sky();
406                         view_layer.use_background_ao = b_view_layer->use_ao();
407                         view_layer.use_surfaces = b_view_layer->use_solid();
408                         view_layer.use_hair = b_view_layer->use_strand();
409
410                         view_layer.bound_samples = false;
411                         view_layer.samples = 0;
412                 }
413
414                 first_layer = false;
415         }
416 }
417
418 /* Images */
419 void BlenderSync::sync_images()
420 {
421         /* Sync is a convention for this API, but currently it frees unused buffers. */
422
423         const bool is_interface_locked = b_engine.render() &&
424                                          b_engine.render().use_lock_interface();
425         if(is_interface_locked == false && BlenderSession::headless == false) {
426                 /* If interface is not locked, it's possible image is needed for
427                  * the display.
428                  */
429                 return;
430         }
431         /* Free buffers used by images which are not needed for render. */
432         BL::BlendData::images_iterator b_image;
433         for(b_data.images.begin(b_image);
434             b_image != b_data.images.end();
435             ++b_image)
436         {
437                 /* TODO(sergey): Consider making it an utility function to check
438                  * whether image is considered builtin.
439                  */
440                 const bool is_builtin = b_image->packed_file() ||
441                                         b_image->source() == BL::Image::source_GENERATED ||
442                                         b_image->source() == BL::Image::source_MOVIE ||
443                                         b_engine.is_preview();
444                 if(is_builtin == false) {
445                         b_image->buffers_free();
446                 }
447                 /* TODO(sergey): Free builtin images not used by any shader. */
448         }
449 }
450
451 /* Passes */
452 PassType BlenderSync::get_pass_type(BL::RenderPass& b_pass)
453 {
454         string name = b_pass.name();
455 #define MAP_PASS(passname, passtype) if(name == passname) return passtype;
456         /* NOTE: Keep in sync with defined names from DNA_scene_types.h */
457         MAP_PASS("Combined", PASS_COMBINED);
458         MAP_PASS("Depth", PASS_DEPTH);
459         MAP_PASS("Mist", PASS_MIST);
460         MAP_PASS("Normal", PASS_NORMAL);
461         MAP_PASS("IndexOB", PASS_OBJECT_ID);
462         MAP_PASS("UV", PASS_UV);
463         MAP_PASS("Vector", PASS_MOTION);
464         MAP_PASS("IndexMA", PASS_MATERIAL_ID);
465
466         MAP_PASS("DiffDir", PASS_DIFFUSE_DIRECT);
467         MAP_PASS("GlossDir", PASS_GLOSSY_DIRECT);
468         MAP_PASS("TransDir", PASS_TRANSMISSION_DIRECT);
469         MAP_PASS("SubsurfaceDir", PASS_SUBSURFACE_DIRECT);
470         MAP_PASS("VolumeDir", PASS_VOLUME_DIRECT);
471
472         MAP_PASS("DiffInd", PASS_DIFFUSE_INDIRECT);
473         MAP_PASS("GlossInd", PASS_GLOSSY_INDIRECT);
474         MAP_PASS("TransInd", PASS_TRANSMISSION_INDIRECT);
475         MAP_PASS("SubsurfaceInd", PASS_SUBSURFACE_INDIRECT);
476         MAP_PASS("VolumeInd", PASS_VOLUME_INDIRECT);
477
478         MAP_PASS("DiffCol", PASS_DIFFUSE_COLOR);
479         MAP_PASS("GlossCol", PASS_GLOSSY_COLOR);
480         MAP_PASS("TransCol", PASS_TRANSMISSION_COLOR);
481         MAP_PASS("SubsurfaceCol", PASS_SUBSURFACE_COLOR);
482
483         MAP_PASS("Emit", PASS_EMISSION);
484         MAP_PASS("Env", PASS_BACKGROUND);
485         MAP_PASS("AO", PASS_AO);
486         MAP_PASS("Shadow", PASS_SHADOW);
487
488 #ifdef __KERNEL_DEBUG__
489         MAP_PASS("Debug BVH Traversed Nodes", PASS_BVH_TRAVERSED_NODES);
490         MAP_PASS("Debug BVH Traversed Instances", PASS_BVH_TRAVERSED_INSTANCES);
491         MAP_PASS("Debug BVH Intersections", PASS_BVH_INTERSECTIONS);
492         MAP_PASS("Debug Ray Bounces", PASS_RAY_BOUNCES);
493 #endif
494         MAP_PASS("Debug Render Time", PASS_RENDER_TIME);
495 #undef MAP_PASS
496
497         return PASS_NONE;
498 }
499
500 int BlenderSync::get_denoising_pass(BL::RenderPass& b_pass)
501 {
502         string name = b_pass.name();
503         if(name.substr(0, 10) != "Denoising ") {
504                 return -1;
505         }
506         name = name.substr(10);
507
508 #define MAP_PASS(passname, offset) if(name == passname) return offset;
509         MAP_PASS("Normal", DENOISING_PASS_NORMAL);
510         MAP_PASS("Normal Variance", DENOISING_PASS_NORMAL_VAR);
511         MAP_PASS("Albedo", DENOISING_PASS_ALBEDO);
512         MAP_PASS("Albedo Variance", DENOISING_PASS_ALBEDO_VAR);
513         MAP_PASS("Depth", DENOISING_PASS_DEPTH);
514         MAP_PASS("Depth Variance", DENOISING_PASS_DEPTH_VAR);
515         MAP_PASS("Shadow A", DENOISING_PASS_SHADOW_A);
516         MAP_PASS("Shadow B", DENOISING_PASS_SHADOW_B);
517         MAP_PASS("Image", DENOISING_PASS_COLOR);
518         MAP_PASS("Image Variance", DENOISING_PASS_COLOR_VAR);
519 #undef MAP_PASS
520
521         return -1;
522 }
523
524 array<Pass> BlenderSync::sync_render_passes(BL::RenderLayer& b_rlay,
525                                             BL::ViewLayer& b_view_layer,
526                                             const SessionParams &session_params)
527 {
528         array<Pass> passes;
529         Pass::add(PASS_COMBINED, passes);
530
531         if(!session_params.device.advanced_shading) {
532                 return passes;
533         }
534
535         /* loop over passes */
536         BL::RenderLayer::passes_iterator b_pass_iter;
537
538         for(b_rlay.passes.begin(b_pass_iter); b_pass_iter != b_rlay.passes.end(); ++b_pass_iter) {
539                 BL::RenderPass b_pass(*b_pass_iter);
540                 PassType pass_type = get_pass_type(b_pass);
541
542                 if(pass_type == PASS_MOTION && scene->integrator->motion_blur)
543                         continue;
544                 if(pass_type != PASS_NONE)
545                         Pass::add(pass_type, passes);
546         }
547
548         PointerRNA crp = RNA_pointer_get(&b_view_layer.ptr, "cycles");
549         if(get_boolean(crp, "denoising_store_passes") &&
550            get_boolean(crp, "use_denoising"))
551         {
552                 b_engine.add_pass("Denoising Normal",          3, "XYZ", b_view_layer.name().c_str());
553                 b_engine.add_pass("Denoising Normal Variance", 3, "XYZ", b_view_layer.name().c_str());
554                 b_engine.add_pass("Denoising Albedo",          3, "RGB", b_view_layer.name().c_str());
555                 b_engine.add_pass("Denoising Albedo Variance", 3, "RGB", b_view_layer.name().c_str());
556                 b_engine.add_pass("Denoising Depth",           1, "Z",   b_view_layer.name().c_str());
557                 b_engine.add_pass("Denoising Depth Variance",  1, "Z",   b_view_layer.name().c_str());
558                 b_engine.add_pass("Denoising Shadow A",        3, "XYV", b_view_layer.name().c_str());
559                 b_engine.add_pass("Denoising Shadow B",        3, "XYV", b_view_layer.name().c_str());
560                 b_engine.add_pass("Denoising Image",           3, "RGB", b_view_layer.name().c_str());
561                 b_engine.add_pass("Denoising Image Variance",  3, "RGB", b_view_layer.name().c_str());
562         }
563 #ifdef __KERNEL_DEBUG__
564         if(get_boolean(crp, "pass_debug_bvh_traversed_nodes")) {
565                 b_engine.add_pass("Debug BVH Traversed Nodes", 1, "X", b_view_layer.name().c_str());
566                 Pass::add(PASS_BVH_TRAVERSED_NODES, passes);
567         }
568         if(get_boolean(crp, "pass_debug_bvh_traversed_instances")) {
569                 b_engine.add_pass("Debug BVH Traversed Instances", 1, "X", b_view_layer.name().c_str());
570                 Pass::add(PASS_BVH_TRAVERSED_INSTANCES, passes);
571         }
572         if(get_boolean(crp, "pass_debug_bvh_intersections")) {
573                 b_engine.add_pass("Debug BVH Intersections", 1, "X", b_view_layer.name().c_str());
574                 Pass::add(PASS_BVH_INTERSECTIONS, passes);
575         }
576         if(get_boolean(crp, "pass_debug_ray_bounces")) {
577                 b_engine.add_pass("Debug Ray Bounces", 1, "X", b_view_layer.name().c_str());
578                 Pass::add(PASS_RAY_BOUNCES, passes);
579         }
580 #endif
581         if(get_boolean(crp, "pass_debug_render_time")) {
582                 b_engine.add_pass("Debug Render Time", 1, "X", b_view_layer.name().c_str());
583                 Pass::add(PASS_RENDER_TIME, passes);
584         }
585         if(get_boolean(crp, "use_pass_volume_direct")) {
586                 b_engine.add_pass("VolumeDir", 3, "RGB", b_view_layer.name().c_str());
587                 Pass::add(PASS_VOLUME_DIRECT, passes);
588         }
589         if(get_boolean(crp, "use_pass_volume_indirect")) {
590                 b_engine.add_pass("VolumeInd", 3, "RGB", b_view_layer.name().c_str());
591                 Pass::add(PASS_VOLUME_INDIRECT, passes);
592         }
593
594         return passes;
595 }
596
597 /* Scene Parameters */
598
599 SceneParams BlenderSync::get_scene_params(BL::Scene& b_scene,
600                                           bool background)
601 {
602         BL::RenderSettings r = b_scene.render();
603         SceneParams params;
604         PointerRNA cscene = RNA_pointer_get(&b_scene.ptr, "cycles");
605         const bool shadingsystem = RNA_boolean_get(&cscene, "shading_system");
606
607         if(shadingsystem == 0)
608                 params.shadingsystem = SHADINGSYSTEM_SVM;
609         else if(shadingsystem == 1)
610                 params.shadingsystem = SHADINGSYSTEM_OSL;
611         
612         if(background || DebugFlags().viewport_static_bvh)
613                 params.bvh_type = SceneParams::BVH_STATIC;
614         else
615                 params.bvh_type = SceneParams::BVH_DYNAMIC;
616
617         params.use_bvh_spatial_split = RNA_boolean_get(&cscene, "debug_use_spatial_splits");
618         params.use_bvh_unaligned_nodes = RNA_boolean_get(&cscene, "debug_use_hair_bvh");
619         params.num_bvh_time_steps = RNA_int_get(&cscene, "debug_bvh_time_steps");
620
621         if(background && params.shadingsystem != SHADINGSYSTEM_OSL)
622                 params.persistent_data = r.use_persistent_data();
623         else
624                 params.persistent_data = false;
625
626         int texture_limit;
627         if(background) {
628                 texture_limit = RNA_enum_get(&cscene, "texture_limit_render");
629         }
630         else {
631                 texture_limit = RNA_enum_get(&cscene, "texture_limit");
632         }
633         if(texture_limit > 0 && b_scene.render().use_simplify()) {
634                 params.texture_limit = 1 << (texture_limit + 6);
635         }
636         else {
637                 params.texture_limit = 0;
638         }
639
640         params.bvh_layout = DebugFlags().cpu.bvh_layout;
641
642         return params;
643 }
644
645 /* Session Parameters */
646
647 bool BlenderSync::get_session_pause(BL::Scene& b_scene, bool background)
648 {
649         PointerRNA cscene = RNA_pointer_get(&b_scene.ptr, "cycles");
650         return (background)? false: get_boolean(cscene, "preview_pause");
651 }
652
653 SessionParams BlenderSync::get_session_params(BL::RenderEngine& b_engine,
654                                               BL::UserPreferences& b_userpref,
655                                               BL::Scene& b_scene,
656                                               bool background)
657 {
658         SessionParams params;
659         PointerRNA cscene = RNA_pointer_get(&b_scene.ptr, "cycles");
660
661         /* feature set */
662         params.experimental = (get_enum(cscene, "feature_set") != 0);
663
664         /* threads */
665         BL::RenderSettings b_r = b_scene.render();
666         if(b_r.threads_mode() == BL::RenderSettings::threads_mode_FIXED)
667                 params.threads = b_r.threads();
668         else
669                 params.threads = 0;
670
671         /* Background */
672         params.background = background;
673
674         /* device type */
675         vector<DeviceInfo>& devices = Device::available_devices();
676         
677         /* device default CPU */
678         foreach(DeviceInfo& device, devices) {
679                 if(device.type == DEVICE_CPU) {
680                         params.device = device;
681                         break;
682                 }
683         }
684
685         if(get_enum(cscene, "device") == 2) {
686                 /* find network device */
687                 foreach(DeviceInfo& info, devices)
688                         if(info.type == DEVICE_NETWORK)
689                                 params.device = info;
690         }
691         else if(get_enum(cscene, "device") == 1) {
692                 PointerRNA b_preferences;
693
694                 BL::UserPreferences::addons_iterator b_addon_iter;
695                 for(b_userpref.addons.begin(b_addon_iter); b_addon_iter != b_userpref.addons.end(); ++b_addon_iter) {
696                         if(b_addon_iter->module() == "cycles") {
697                                 b_preferences = b_addon_iter->preferences().ptr;
698                                 break;
699                         }
700                 }
701
702                 enum ComputeDevice {
703                         COMPUTE_DEVICE_CPU = 0,
704                         COMPUTE_DEVICE_CUDA = 1,
705                         COMPUTE_DEVICE_OPENCL = 2,
706                         COMPUTE_DEVICE_NUM = 3,
707                 };
708
709                 ComputeDevice compute_device = (ComputeDevice)get_enum(b_preferences,
710                                                                        "compute_device_type",
711                                                                        COMPUTE_DEVICE_NUM,
712                                                                        COMPUTE_DEVICE_CPU);
713
714                 if(compute_device != COMPUTE_DEVICE_CPU) {
715                         vector<DeviceInfo> used_devices;
716                         RNA_BEGIN(&b_preferences, device, "devices") {
717                                 ComputeDevice device_type = (ComputeDevice)get_enum(device,
718                                                                                     "type",
719                                                                                     COMPUTE_DEVICE_NUM,
720                                                                                     COMPUTE_DEVICE_CPU);
721
722                                 if(get_boolean(device, "use") &&
723                                    (device_type == compute_device || device_type == COMPUTE_DEVICE_CPU)) {
724                                         string id = get_string(device, "id");
725                                         foreach(DeviceInfo& info, devices) {
726                                                 if(info.id == id) {
727                                                         used_devices.push_back(info);
728                                                         break;
729                                                 }
730                                         }
731                                 }
732                         } RNA_END
733
734                         if(used_devices.size() == 1) {
735                                 params.device = used_devices[0];
736                         }
737                         else if(used_devices.size() > 1) {
738                                 params.device = Device::get_multi_device(used_devices,
739                                                                          params.threads,
740                                                                          params.background);
741                         }
742                         /* Else keep using the CPU device that was set before. */
743                 }
744         }
745
746         /* samples */
747         int samples = get_int(cscene, "samples");
748         int aa_samples = get_int(cscene, "aa_samples");
749         int preview_samples = get_int(cscene, "preview_samples");
750         int preview_aa_samples = get_int(cscene, "preview_aa_samples");
751         
752         if(get_boolean(cscene, "use_square_samples")) {
753                 aa_samples = aa_samples * aa_samples;
754                 preview_aa_samples = preview_aa_samples * preview_aa_samples;
755
756                 samples = samples * samples;
757                 preview_samples = preview_samples * preview_samples;
758         }
759
760         if(get_enum(cscene, "progressive") == 0) {
761                 if(background) {
762                         params.samples = aa_samples;
763                 }
764                 else {
765                         params.samples = preview_aa_samples;
766                         if(params.samples == 0)
767                                 params.samples = INT_MAX;
768                 }
769         }
770         else {
771                 if(background) {
772                         params.samples = samples;
773                 }
774                 else {
775                         params.samples = preview_samples;
776                         if(params.samples == 0)
777                                 params.samples = INT_MAX;
778                 }
779         }
780
781         /* tiles */
782         if(params.device.type != DEVICE_CPU && !background) {
783                 /* currently GPU could be much slower than CPU when using tiles,
784                  * still need to be investigated, but meanwhile make it possible
785                  * to work in viewport smoothly
786                  */
787                 int debug_tile_size = get_int(cscene, "debug_tile_size");
788
789                 params.tile_size = make_int2(debug_tile_size, debug_tile_size);
790         }
791         else {
792                 int tile_x = b_engine.tile_x();
793                 int tile_y = b_engine.tile_y();
794
795                 params.tile_size = make_int2(tile_x, tile_y);
796         }
797
798         if((BlenderSession::headless == false) && background) {
799                 params.tile_order = (TileOrder)get_enum(cscene, "tile_order");
800         }
801         else {
802                 params.tile_order = TILE_BOTTOM_TO_TOP;
803         }
804
805         /* other parameters */
806         params.start_resolution = get_int(cscene, "preview_start_resolution");
807         params.pixel_size = b_engine.get_preview_pixel_size(b_scene);
808
809         /* other parameters */
810         params.cancel_timeout = (double)get_float(cscene, "debug_cancel_timeout");
811         params.reset_timeout = (double)get_float(cscene, "debug_reset_timeout");
812         params.text_timeout = (double)get_float(cscene, "debug_text_timeout");
813
814         /* progressive refine */
815         params.progressive_refine = get_boolean(cscene, "use_progressive_refine") &&
816                                     !b_r.use_save_buffers();
817
818         if(params.progressive_refine) {
819                 BL::Scene::view_layers_iterator b_view_layer;
820                 for(b_scene.view_layers.begin(b_view_layer); b_view_layer != b_scene.view_layers.end(); ++b_view_layer) {
821                         PointerRNA crl = RNA_pointer_get(&b_view_layer->ptr, "cycles");
822                         if(get_boolean(crl, "use_denoising")) {
823                                 params.progressive_refine = false;
824                         }
825                 }
826         }
827
828         if(background) {
829                 if(params.progressive_refine)
830                         params.progressive = true;
831                 else
832                         params.progressive = false;
833
834                 params.start_resolution = INT_MAX;
835                 params.pixel_size = 1;
836         }
837         else
838                 params.progressive = true;
839
840         /* shading system - scene level needs full refresh */
841         const bool shadingsystem = RNA_boolean_get(&cscene, "shading_system");
842
843         if(shadingsystem == 0)
844                 params.shadingsystem = SHADINGSYSTEM_SVM;
845         else if(shadingsystem == 1)
846                 params.shadingsystem = SHADINGSYSTEM_OSL;
847         
848         /* color managagement */
849         params.display_buffer_linear = GLEW_ARB_half_float_pixel &&
850                                        b_engine.support_display_space_shader(b_scene);
851
852         if(b_engine.is_preview()) {
853                 /* For preview rendering we're using same timeout as
854                  * blender's job update.
855                  */
856                 params.progressive_update_timeout = 0.1;
857         }
858
859         return params;
860 }
861
862 CCL_NAMESPACE_END
863