b070bf89d4460f42dac07401360502a1c1729581
[blender-staging.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::SpaceView3D& b_v3d,
193                             BL::Object& b_override,
194                             int width, int height,
195                             void **python_thread_state,
196                             const char *layer)
197 {
198         sync_render_layers(b_v3d, layer);
199         sync_integrator();
200         sync_film();
201         sync_shaders();
202         sync_images();
203         sync_curve_settings();
204
205         mesh_synced.clear(); /* use for objects and motion sync */
206
207         if(scene->need_motion() == Scene::MOTION_PASS ||
208            scene->need_motion() == Scene::MOTION_NONE ||
209            scene->camera->motion_position == Camera::MOTION_POSITION_CENTER)
210         {
211                 sync_objects();
212         }
213         sync_motion(b_render,
214                     b_override,
215                     width, height,
216                     python_thread_state);
217
218         mesh_synced.clear();
219 }
220
221 /* Integrator */
222
223 void BlenderSync::sync_integrator()
224 {
225 #ifdef __CAMERA_MOTION__
226         BL::RenderSettings r = b_scene.render();
227 #endif
228         PointerRNA cscene = RNA_pointer_get(&b_scene.ptr, "cycles");
229
230         experimental = (get_enum(cscene, "feature_set") != 0);
231
232         Integrator *integrator = scene->integrator;
233         Integrator previntegrator = *integrator;
234
235         integrator->max_bounce = get_int(cscene, "max_bounces");
236
237         integrator->max_diffuse_bounce = get_int(cscene, "diffuse_bounces");
238         integrator->max_glossy_bounce = get_int(cscene, "glossy_bounces");
239         integrator->max_transmission_bounce = get_int(cscene, "transmission_bounces");
240         integrator->max_volume_bounce = get_int(cscene, "volume_bounces");
241
242         integrator->transparent_max_bounce = get_int(cscene, "transparent_max_bounces");
243
244         integrator->volume_max_steps = get_int(cscene, "volume_max_steps");
245         integrator->volume_step_size = get_float(cscene, "volume_step_size");
246
247         integrator->caustics_reflective = get_boolean(cscene, "caustics_reflective");
248         integrator->caustics_refractive = get_boolean(cscene, "caustics_refractive");
249         integrator->filter_glossy = get_float(cscene, "blur_glossy");
250
251         integrator->seed = get_int(cscene, "seed");
252         if(get_boolean(cscene, "use_animated_seed")) {
253                 integrator->seed = hash_int_2d(b_scene.frame_current(),
254                                                get_int(cscene, "seed"));
255                 if(b_scene.frame_subframe() != 0.0f) {
256                         /* TODO(sergey): Ideally should be some sort of hash_merge,
257                          * but this is good enough for now.
258                          */
259                         integrator->seed += hash_int_2d((int)(b_scene.frame_subframe() * (float)INT_MAX),
260                                                         get_int(cscene, "seed"));
261                 }
262         }
263
264         integrator->sampling_pattern = (SamplingPattern)get_enum(
265                 cscene,
266                 "sampling_pattern",
267                 SAMPLING_NUM_PATTERNS,
268                 SAMPLING_PATTERN_SOBOL);
269
270         integrator->sample_clamp_direct = get_float(cscene, "sample_clamp_direct");
271         integrator->sample_clamp_indirect = get_float(cscene, "sample_clamp_indirect");
272 #ifdef __CAMERA_MOTION__
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 #endif
282
283         integrator->method = (Integrator::Method)get_enum(cscene,
284                                                           "progressive",
285                                                           Integrator::NUM_METHODS,
286                                                           Integrator::PATH);
287
288         integrator->sample_all_lights_direct = get_boolean(cscene, "sample_all_lights_direct");
289         integrator->sample_all_lights_indirect = get_boolean(cscene, "sample_all_lights_indirect");
290         integrator->light_sampling_threshold = get_float(cscene, "light_sampling_threshold");
291
292         int diffuse_samples = get_int(cscene, "diffuse_samples");
293         int glossy_samples = get_int(cscene, "glossy_samples");
294         int transmission_samples = get_int(cscene, "transmission_samples");
295         int ao_samples = get_int(cscene, "ao_samples");
296         int mesh_light_samples = get_int(cscene, "mesh_light_samples");
297         int subsurface_samples = get_int(cscene, "subsurface_samples");
298         int volume_samples = get_int(cscene, "volume_samples");
299
300         if(get_boolean(cscene, "use_square_samples")) {
301                 integrator->diffuse_samples = diffuse_samples * diffuse_samples;
302                 integrator->glossy_samples = glossy_samples * glossy_samples;
303                 integrator->transmission_samples = transmission_samples * transmission_samples;
304                 integrator->ao_samples = ao_samples * ao_samples;
305                 integrator->mesh_light_samples = mesh_light_samples * mesh_light_samples;
306                 integrator->subsurface_samples = subsurface_samples * subsurface_samples;
307                 integrator->volume_samples = volume_samples * volume_samples;
308         } 
309         else {
310                 integrator->diffuse_samples = diffuse_samples;
311                 integrator->glossy_samples = glossy_samples;
312                 integrator->transmission_samples = transmission_samples;
313                 integrator->ao_samples = ao_samples;
314                 integrator->mesh_light_samples = mesh_light_samples;
315                 integrator->subsurface_samples = subsurface_samples;
316                 integrator->volume_samples = volume_samples;
317         }
318
319         if(b_scene.render().use_simplify()) {
320                 if(preview) {
321                         integrator->ao_bounces = get_int(cscene, "ao_bounces");
322                 }
323                 else {
324                         integrator->ao_bounces = get_int(cscene, "ao_bounces_render");
325                 }
326         }
327         else {
328                 integrator->ao_bounces = 0;
329         }
330
331         if(integrator->modified(previntegrator))
332                 integrator->tag_update(scene);
333 }
334
335 /* Film */
336
337 void BlenderSync::sync_film()
338 {
339         PointerRNA cscene = RNA_pointer_get(&b_scene.ptr, "cycles");
340
341         Film *film = scene->film;
342         Film prevfilm = *film;
343         
344         film->exposure = get_float(cscene, "film_exposure");
345         film->filter_type = (FilterType)get_enum(cscene,
346                                                  "pixel_filter_type",
347                                                  FILTER_NUM_TYPES,
348                                                  FILTER_BLACKMAN_HARRIS);
349         film->filter_width = (film->filter_type == FILTER_BOX)? 1.0f: get_float(cscene, "filter_width");
350
351         if(b_scene.world()) {
352                 BL::WorldMistSettings b_mist = b_scene.world().mist_settings();
353
354                 film->mist_start = b_mist.start();
355                 film->mist_depth = b_mist.depth();
356
357                 switch(b_mist.falloff()) {
358                         case BL::WorldMistSettings::falloff_QUADRATIC:
359                                 film->mist_falloff = 2.0f;
360                                 break;
361                         case BL::WorldMistSettings::falloff_LINEAR:
362                                 film->mist_falloff = 1.0f;
363                                 break;
364                         case BL::WorldMistSettings::falloff_INVERSE_QUADRATIC:
365                                 film->mist_falloff = 0.5f;
366                                 break;
367                 }
368         }
369
370         if(film->modified(prevfilm))
371                 film->tag_update(scene);
372 }
373
374 /* Render Layer */
375
376 void BlenderSync::sync_render_layers(BL::SpaceView3D& b_v3d, const char *layer)
377 {
378         PointerRNA cscene = RNA_pointer_get(&b_scene.ptr, "cycles");
379         string layername;
380
381         /* 3d view */
382         if(b_v3d) {
383                 if(RNA_boolean_get(&cscene, "preview_active_layer")) {
384                         BL::RenderLayers layers(b_scene.render().ptr);
385                         layername = layers.active().name();
386                         layer = layername.c_str();
387                 }
388                 else {
389                         render_layer.scene_layer = get_layer(b_v3d.layers(), b_v3d.layers_local_view());
390                         render_layer.layer = render_layer.scene_layer;
391                         render_layer.exclude_layer = 0;
392                         render_layer.holdout_layer = 0;
393                         render_layer.material_override = PointerRNA_NULL;
394                         render_layer.use_background_shader = true;
395                         render_layer.use_background_ao = true;
396                         render_layer.use_hair = true;
397                         render_layer.use_surfaces = true;
398                         render_layer.use_viewport_visibility = true;
399                         render_layer.samples = 0;
400                         render_layer.bound_samples = false;
401                         return;
402                 }
403         }
404
405         /* render layer */
406         BL::RenderSettings r = b_scene.render();
407         BL::RenderSettings::layers_iterator b_rlay;
408         int use_layer_samples = get_enum(cscene, "use_layer_samples");
409         bool first_layer = true;
410         uint layer_override = get_layer(b_engine.layer_override());
411         uint scene_layers = layer_override ? layer_override : get_layer(b_scene.layers());
412
413         for(r.layers.begin(b_rlay); b_rlay != r.layers.end(); ++b_rlay) {
414                 if((!layer && first_layer) || (layer && b_rlay->name() == layer)) {
415                         render_layer.name = b_rlay->name();
416
417                         render_layer.holdout_layer = get_layer(b_rlay->layers_zmask());
418                         render_layer.exclude_layer = get_layer(b_rlay->layers_exclude());
419
420                         render_layer.scene_layer = scene_layers & ~render_layer.exclude_layer;
421                         render_layer.scene_layer |= render_layer.exclude_layer & render_layer.holdout_layer;
422
423                         render_layer.layer = get_layer(b_rlay->layers());
424                         render_layer.layer |= render_layer.holdout_layer;
425
426                         render_layer.material_override = b_rlay->material_override();
427                         render_layer.use_background_shader = b_rlay->use_sky();
428                         render_layer.use_background_ao = b_rlay->use_ao();
429                         render_layer.use_surfaces = b_rlay->use_solid();
430                         render_layer.use_hair = b_rlay->use_strand();
431                         render_layer.use_viewport_visibility = false;
432
433                         render_layer.bound_samples = (use_layer_samples == 1);
434                         if(use_layer_samples != 2) {
435                                 int samples = b_rlay->samples();
436                                 if(get_boolean(cscene, "use_square_samples"))
437                                         render_layer.samples = samples * samples;
438                                 else
439                                         render_layer.samples = samples;
440                         }
441                 }
442
443                 first_layer = false;
444         }
445 }
446
447 /* Images */
448 void BlenderSync::sync_images()
449 {
450         /* Sync is a convention for this API, but currently it frees unused buffers. */
451
452         const bool is_interface_locked = b_engine.render() &&
453                                          b_engine.render().use_lock_interface();
454         if(is_interface_locked == false && BlenderSession::headless == false) {
455                 /* If interface is not locked, it's possible image is needed for
456                  * the display.
457                  */
458                 return;
459         }
460         /* Free buffers used by images which are not needed for render. */
461         BL::BlendData::images_iterator b_image;
462         for(b_data.images.begin(b_image);
463             b_image != b_data.images.end();
464             ++b_image)
465         {
466                 /* TODO(sergey): Consider making it an utility function to check
467                  * whether image is considered builtin.
468                  */
469                 const bool is_builtin = b_image->packed_file() ||
470                                         b_image->source() == BL::Image::source_GENERATED ||
471                                         b_image->source() == BL::Image::source_MOVIE ||
472                                         b_engine.is_preview();
473                 if(is_builtin == false) {
474                         b_image->buffers_free();
475                 }
476                 /* TODO(sergey): Free builtin images not used by any shader. */
477         }
478 }
479
480 /* Passes */
481 PassType BlenderSync::get_pass_type(BL::RenderPass& b_pass)
482 {
483         string name = b_pass.name();
484 #define MAP_PASS(passname, passtype) if(name == passname) return passtype;
485         /* NOTE: Keep in sync with defined names from DNA_scene_types.h */
486         MAP_PASS("Combined", PASS_COMBINED);
487         MAP_PASS("Depth", PASS_DEPTH);
488         MAP_PASS("Mist", PASS_MIST);
489         MAP_PASS("Normal", PASS_NORMAL);
490         MAP_PASS("IndexOB", PASS_OBJECT_ID);
491         MAP_PASS("UV", PASS_UV);
492         MAP_PASS("Vector", PASS_MOTION);
493         MAP_PASS("IndexMA", PASS_MATERIAL_ID);
494
495         MAP_PASS("DiffDir", PASS_DIFFUSE_DIRECT);
496         MAP_PASS("GlossDir", PASS_GLOSSY_DIRECT);
497         MAP_PASS("TransDir", PASS_TRANSMISSION_DIRECT);
498         MAP_PASS("SubsurfaceDir", PASS_SUBSURFACE_DIRECT);
499
500         MAP_PASS("DiffInd", PASS_DIFFUSE_INDIRECT);
501         MAP_PASS("GlossInd", PASS_GLOSSY_INDIRECT);
502         MAP_PASS("TransInd", PASS_TRANSMISSION_INDIRECT);
503         MAP_PASS("SubsurfaceInd", PASS_SUBSURFACE_INDIRECT);
504
505         MAP_PASS("DiffCol", PASS_DIFFUSE_COLOR);
506         MAP_PASS("GlossCol", PASS_GLOSSY_COLOR);
507         MAP_PASS("TransCol", PASS_TRANSMISSION_COLOR);
508         MAP_PASS("SubsurfaceCol", PASS_SUBSURFACE_COLOR);
509
510         MAP_PASS("Emit", PASS_EMISSION);
511         MAP_PASS("Env", PASS_BACKGROUND);
512         MAP_PASS("AO", PASS_AO);
513         MAP_PASS("Shadow", PASS_SHADOW);
514
515 #ifdef __KERNEL_DEBUG__
516         MAP_PASS("Debug BVH Traversed Nodes", PASS_BVH_TRAVERSED_NODES);
517         MAP_PASS("Debug BVH Traversed Instances", PASS_BVH_TRAVERSED_INSTANCES);
518         MAP_PASS("Debug BVH Intersections", PASS_BVH_INTERSECTIONS);
519         MAP_PASS("Debug Ray Bounces", PASS_RAY_BOUNCES);
520 #endif
521 #undef MAP_PASS
522
523         return PASS_NONE;
524 }
525
526 int BlenderSync::get_denoising_pass(BL::RenderPass& b_pass)
527 {
528         string name = b_pass.name();
529         if(name.substr(0, 10) != "Denoising ") {
530                 return -1;
531         }
532         name = name.substr(10);
533
534 #define MAP_PASS(passname, offset) if(name == passname) return offset;
535         MAP_PASS("Normal", DENOISING_PASS_NORMAL);
536         MAP_PASS("Normal Variance", DENOISING_PASS_NORMAL_VAR);
537         MAP_PASS("Albedo", DENOISING_PASS_ALBEDO);
538         MAP_PASS("Albedo Variance", DENOISING_PASS_ALBEDO_VAR);
539         MAP_PASS("Depth", DENOISING_PASS_DEPTH);
540         MAP_PASS("Depth Variance", DENOISING_PASS_DEPTH_VAR);
541         MAP_PASS("Shadow A", DENOISING_PASS_SHADOW_A);
542         MAP_PASS("Shadow B", DENOISING_PASS_SHADOW_B);
543         MAP_PASS("Image", DENOISING_PASS_COLOR);
544         MAP_PASS("Image Variance", DENOISING_PASS_COLOR_VAR);
545 #undef MAP_PASS
546
547         return -1;
548 }
549
550 array<Pass> BlenderSync::sync_render_passes(BL::RenderLayer& b_rlay,
551                                             BL::SceneRenderLayer& b_srlay,
552                                             const SessionParams &session_params)
553 {
554         array<Pass> passes;
555         Pass::add(PASS_COMBINED, passes);
556
557         if(!session_params.device.advanced_shading) {
558                 return passes;
559         }
560
561         /* loop over passes */
562         BL::RenderLayer::passes_iterator b_pass_iter;
563
564         for(b_rlay.passes.begin(b_pass_iter); b_pass_iter != b_rlay.passes.end(); ++b_pass_iter) {
565                 BL::RenderPass b_pass(*b_pass_iter);
566                 PassType pass_type = get_pass_type(b_pass);
567
568                 if(pass_type == PASS_MOTION && scene->integrator->motion_blur)
569                         continue;
570                 if(pass_type != PASS_NONE)
571                         Pass::add(pass_type, passes);
572         }
573
574         PointerRNA crp = RNA_pointer_get(&b_srlay.ptr, "cycles");
575         if(get_boolean(crp, "denoising_store_passes") &&
576            get_boolean(crp, "use_denoising")) {
577                 b_engine.add_pass("Denoising Normal",          3, "XYZ", b_srlay.name().c_str());
578                 b_engine.add_pass("Denoising Normal Variance", 3, "XYZ", b_srlay.name().c_str());
579                 b_engine.add_pass("Denoising Albedo",          3, "RGB", b_srlay.name().c_str());
580                 b_engine.add_pass("Denoising Albedo Variance", 3, "RGB", b_srlay.name().c_str());
581                 b_engine.add_pass("Denoising Depth",           1, "Z",   b_srlay.name().c_str());
582                 b_engine.add_pass("Denoising Depth Variance",  1, "Z",   b_srlay.name().c_str());
583                 b_engine.add_pass("Denoising Shadow A",        3, "XYV", b_srlay.name().c_str());
584                 b_engine.add_pass("Denoising Shadow B",        3, "XYV", b_srlay.name().c_str());
585                 b_engine.add_pass("Denoising Image",           3, "RGB", b_srlay.name().c_str());
586                 b_engine.add_pass("Denoising Image Variance",  3, "RGB", b_srlay.name().c_str());
587         }
588 #ifdef __KERNEL_DEBUG__
589         if(get_boolean(crp, "pass_debug_bvh_traversed_nodes")) {
590                 b_engine.add_pass("Debug BVH Traversed Nodes", 1, "X", b_srlay.name().c_str());
591                 Pass::add(PASS_BVH_TRAVERSED_NODES, passes);
592         }
593         if(get_boolean(crp, "pass_debug_bvh_traversed_instances")) {
594                 b_engine.add_pass("Debug BVH Traversed Instances", 1, "X", b_srlay.name().c_str());
595                 Pass::add(PASS_BVH_TRAVERSED_INSTANCES, passes);
596         }
597         if(get_boolean(crp, "pass_debug_bvh_intersections")) {
598                 b_engine.add_pass("Debug BVH Intersections", 1, "X", b_srlay.name().c_str());
599                 Pass::add(PASS_BVH_INTERSECTIONS, passes);
600         }
601         if(get_boolean(crp, "pass_debug_ray_bounces")) {
602                 b_engine.add_pass("Debug Ray Bounces", 1, "X", b_srlay.name().c_str());
603                 Pass::add(PASS_RAY_BOUNCES, passes);
604         }
605 #endif
606
607         return passes;
608 }
609
610 /* Scene Parameters */
611
612 SceneParams BlenderSync::get_scene_params(BL::Scene& b_scene,
613                                           bool background)
614 {
615         BL::RenderSettings r = b_scene.render();
616         SceneParams params;
617         PointerRNA cscene = RNA_pointer_get(&b_scene.ptr, "cycles");
618         const bool shadingsystem = RNA_boolean_get(&cscene, "shading_system");
619
620         if(shadingsystem == 0)
621                 params.shadingsystem = SHADINGSYSTEM_SVM;
622         else if(shadingsystem == 1)
623                 params.shadingsystem = SHADINGSYSTEM_OSL;
624         
625         if(background || DebugFlags().viewport_static_bvh)
626                 params.bvh_type = SceneParams::BVH_STATIC;
627         else
628                 params.bvh_type = SceneParams::BVH_DYNAMIC;
629
630         params.use_bvh_spatial_split = RNA_boolean_get(&cscene, "debug_use_spatial_splits");
631         params.use_bvh_unaligned_nodes = RNA_boolean_get(&cscene, "debug_use_hair_bvh");
632         params.num_bvh_time_steps = RNA_int_get(&cscene, "debug_bvh_time_steps");
633
634         if(background && params.shadingsystem != SHADINGSYSTEM_OSL)
635                 params.persistent_data = r.use_persistent_data();
636         else
637                 params.persistent_data = false;
638
639         int texture_limit;
640         if(background) {
641                 texture_limit = RNA_enum_get(&cscene, "texture_limit_render");
642         }
643         else {
644                 texture_limit = RNA_enum_get(&cscene, "texture_limit");
645         }
646         if(texture_limit > 0 && b_scene.render().use_simplify()) {
647                 params.texture_limit = 1 << (texture_limit + 6);
648         }
649         else {
650                 params.texture_limit = 0;
651         }
652
653         params.use_qbvh = DebugFlags().cpu.qbvh;
654
655         return params;
656 }
657
658 /* Session Parameters */
659
660 bool BlenderSync::get_session_pause(BL::Scene& b_scene, bool background)
661 {
662         PointerRNA cscene = RNA_pointer_get(&b_scene.ptr, "cycles");
663         return (background)? false: get_boolean(cscene, "preview_pause");
664 }
665
666 SessionParams BlenderSync::get_session_params(BL::RenderEngine& b_engine,
667                                               BL::UserPreferences& b_userpref,
668                                               BL::Scene& b_scene,
669                                               bool background)
670 {
671         SessionParams params;
672         PointerRNA cscene = RNA_pointer_get(&b_scene.ptr, "cycles");
673
674         /* feature set */
675         params.experimental = (get_enum(cscene, "feature_set") != 0);
676
677         /* threads */
678         BL::RenderSettings b_r = b_scene.render();
679         if(b_r.threads_mode() == BL::RenderSettings::threads_mode_FIXED)
680                 params.threads = b_r.threads();
681         else
682                 params.threads = 0;
683
684         /* Background */
685         params.background = background;
686
687         /* device type */
688         vector<DeviceInfo>& devices = Device::available_devices();
689         
690         /* device default CPU */
691         foreach(DeviceInfo& device, devices) {
692                 if(device.type == DEVICE_CPU) {
693                         params.device = device;
694                         break;
695                 }
696         }
697
698         if(get_enum(cscene, "device") == 2) {
699                 /* find network device */
700                 foreach(DeviceInfo& info, devices)
701                         if(info.type == DEVICE_NETWORK)
702                                 params.device = info;
703         }
704         else if(get_enum(cscene, "device") == 1) {
705                 PointerRNA b_preferences;
706
707                 BL::UserPreferences::addons_iterator b_addon_iter;
708                 for(b_userpref.addons.begin(b_addon_iter); b_addon_iter != b_userpref.addons.end(); ++b_addon_iter) {
709                         if(b_addon_iter->module() == "cycles") {
710                                 b_preferences = b_addon_iter->preferences().ptr;
711                                 break;
712                         }
713                 }
714
715                 enum ComputeDevice {
716                         COMPUTE_DEVICE_CPU = 0,
717                         COMPUTE_DEVICE_CUDA = 1,
718                         COMPUTE_DEVICE_OPENCL = 2,
719                         COMPUTE_DEVICE_NUM = 3,
720                 };
721
722                 ComputeDevice compute_device = (ComputeDevice)get_enum(b_preferences,
723                                                                        "compute_device_type",
724                                                                        COMPUTE_DEVICE_NUM,
725                                                                        COMPUTE_DEVICE_CPU);
726
727                 if(compute_device != COMPUTE_DEVICE_CPU) {
728                         vector<DeviceInfo> used_devices;
729                         RNA_BEGIN(&b_preferences, device, "devices") {
730                                 ComputeDevice device_type = (ComputeDevice)get_enum(device,
731                                                                                     "type",
732                                                                                     COMPUTE_DEVICE_NUM,
733                                                                                     COMPUTE_DEVICE_CPU);
734
735                                 if(get_boolean(device, "use") &&
736                                    (device_type == compute_device || device_type == COMPUTE_DEVICE_CPU)) {
737                                         string id = get_string(device, "id");
738                                         foreach(DeviceInfo& info, devices) {
739                                                 if(info.id == id) {
740                                                         used_devices.push_back(info);
741                                                         break;
742                                                 }
743                                         }
744                                 }
745                         } RNA_END
746
747                         if(used_devices.size() == 1) {
748                                 params.device = used_devices[0];
749                         }
750                         else if(used_devices.size() > 1) {
751                                 params.device = Device::get_multi_device(used_devices,
752                                                                          params.threads,
753                                                                          params.background);
754                         }
755                         /* Else keep using the CPU device that was set before. */
756                 }
757         }
758
759         /* samples */
760         int samples = get_int(cscene, "samples");
761         int aa_samples = get_int(cscene, "aa_samples");
762         int preview_samples = get_int(cscene, "preview_samples");
763         int preview_aa_samples = get_int(cscene, "preview_aa_samples");
764         
765         if(get_boolean(cscene, "use_square_samples")) {
766                 aa_samples = aa_samples * aa_samples;
767                 preview_aa_samples = preview_aa_samples * preview_aa_samples;
768
769                 samples = samples * samples;
770                 preview_samples = preview_samples * preview_samples;
771         }
772
773         if(get_enum(cscene, "progressive") == 0) {
774                 if(background) {
775                         params.samples = aa_samples;
776                 }
777                 else {
778                         params.samples = preview_aa_samples;
779                         if(params.samples == 0)
780                                 params.samples = INT_MAX;
781                 }
782         }
783         else {
784                 if(background) {
785                         params.samples = samples;
786                 }
787                 else {
788                         params.samples = preview_samples;
789                         if(params.samples == 0)
790                                 params.samples = INT_MAX;
791                 }
792         }
793
794         /* tiles */
795         if(params.device.type != DEVICE_CPU && !background) {
796                 /* currently GPU could be much slower than CPU when using tiles,
797                  * still need to be investigated, but meanwhile make it possible
798                  * to work in viewport smoothly
799                  */
800                 int debug_tile_size = get_int(cscene, "debug_tile_size");
801
802                 params.tile_size = make_int2(debug_tile_size, debug_tile_size);
803         }
804         else {
805                 int tile_x = b_engine.tile_x();
806                 int tile_y = b_engine.tile_y();
807
808                 params.tile_size = make_int2(tile_x, tile_y);
809         }
810
811         if((BlenderSession::headless == false) && background) {
812                 params.tile_order = (TileOrder)get_enum(cscene, "tile_order");
813         }
814         else {
815                 params.tile_order = TILE_BOTTOM_TO_TOP;
816         }
817
818         /* other parameters */
819         params.start_resolution = get_int(cscene, "preview_start_resolution");
820         params.pixel_size = b_engine.get_preview_pixel_size(b_scene);
821
822         /* other parameters */
823         params.cancel_timeout = (double)get_float(cscene, "debug_cancel_timeout");
824         params.reset_timeout = (double)get_float(cscene, "debug_reset_timeout");
825         params.text_timeout = (double)get_float(cscene, "debug_text_timeout");
826
827         /* progressive refine */
828         params.progressive_refine = get_boolean(cscene, "use_progressive_refine") &&
829                                     !b_r.use_save_buffers();
830
831         if(params.progressive_refine) {
832                 BL::RenderSettings::layers_iterator b_rlay;
833                 for(b_r.layers.begin(b_rlay); b_rlay != b_r.layers.end(); ++b_rlay) {
834                         PointerRNA crl = RNA_pointer_get(&b_rlay->ptr, "cycles");
835                         if(get_boolean(crl, "use_denoising")) {
836                                 params.progressive_refine = false;
837                         }
838                 }
839         }
840
841         if(background) {
842                 if(params.progressive_refine)
843                         params.progressive = true;
844                 else
845                         params.progressive = false;
846
847                 params.start_resolution = INT_MAX;
848                 params.pixel_size = 1;
849         }
850         else
851                 params.progressive = true;
852
853         /* shading system - scene level needs full refresh */
854         const bool shadingsystem = RNA_boolean_get(&cscene, "shading_system");
855
856         if(shadingsystem == 0)
857                 params.shadingsystem = SHADINGSYSTEM_SVM;
858         else if(shadingsystem == 1)
859                 params.shadingsystem = SHADINGSYSTEM_OSL;
860         
861         /* color managagement */
862 #ifdef GLEW_MX
863         /* When using GLEW MX we need to check whether we've got an OpenGL
864          * context for current window. This is because command line rendering
865          * doesn't have OpenGL context actually.
866          */
867         if(glewGetContext() != NULL)
868 #endif
869         {
870                 params.display_buffer_linear = GLEW_ARB_half_float_pixel &&
871                                                b_engine.support_display_space_shader(b_scene);
872         }
873
874         if(b_engine.is_preview()) {
875                 /* For preview rendering we're using same timeout as
876                  * blender's job update.
877                  */
878                 params.progressive_update_timeout = 0.1;
879         }
880
881         return params;
882 }
883
884 CCL_NAMESPACE_END
885