Cycles: Cleanup, style
[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         {
578                 b_engine.add_pass("Denoising Normal",          3, "XYZ", b_srlay.name().c_str());
579                 b_engine.add_pass("Denoising Normal Variance", 3, "XYZ", b_srlay.name().c_str());
580                 b_engine.add_pass("Denoising Albedo",          3, "RGB", b_srlay.name().c_str());
581                 b_engine.add_pass("Denoising Albedo Variance", 3, "RGB", b_srlay.name().c_str());
582                 b_engine.add_pass("Denoising Depth",           1, "Z",   b_srlay.name().c_str());
583                 b_engine.add_pass("Denoising Depth Variance",  1, "Z",   b_srlay.name().c_str());
584                 b_engine.add_pass("Denoising Shadow A",        3, "XYV", b_srlay.name().c_str());
585                 b_engine.add_pass("Denoising Shadow B",        3, "XYV", b_srlay.name().c_str());
586                 b_engine.add_pass("Denoising Image",           3, "RGB", b_srlay.name().c_str());
587                 b_engine.add_pass("Denoising Image Variance",  3, "RGB", b_srlay.name().c_str());
588         }
589 #ifdef __KERNEL_DEBUG__
590         if(get_boolean(crp, "pass_debug_bvh_traversed_nodes")) {
591                 b_engine.add_pass("Debug BVH Traversed Nodes", 1, "X", b_srlay.name().c_str());
592                 Pass::add(PASS_BVH_TRAVERSED_NODES, passes);
593         }
594         if(get_boolean(crp, "pass_debug_bvh_traversed_instances")) {
595                 b_engine.add_pass("Debug BVH Traversed Instances", 1, "X", b_srlay.name().c_str());
596                 Pass::add(PASS_BVH_TRAVERSED_INSTANCES, passes);
597         }
598         if(get_boolean(crp, "pass_debug_bvh_intersections")) {
599                 b_engine.add_pass("Debug BVH Intersections", 1, "X", b_srlay.name().c_str());
600                 Pass::add(PASS_BVH_INTERSECTIONS, passes);
601         }
602         if(get_boolean(crp, "pass_debug_ray_bounces")) {
603                 b_engine.add_pass("Debug Ray Bounces", 1, "X", b_srlay.name().c_str());
604                 Pass::add(PASS_RAY_BOUNCES, passes);
605         }
606 #endif
607
608         return passes;
609 }
610
611 /* Scene Parameters */
612
613 SceneParams BlenderSync::get_scene_params(BL::Scene& b_scene,
614                                           bool background)
615 {
616         BL::RenderSettings r = b_scene.render();
617         SceneParams params;
618         PointerRNA cscene = RNA_pointer_get(&b_scene.ptr, "cycles");
619         const bool shadingsystem = RNA_boolean_get(&cscene, "shading_system");
620
621         if(shadingsystem == 0)
622                 params.shadingsystem = SHADINGSYSTEM_SVM;
623         else if(shadingsystem == 1)
624                 params.shadingsystem = SHADINGSYSTEM_OSL;
625         
626         if(background || DebugFlags().viewport_static_bvh)
627                 params.bvh_type = SceneParams::BVH_STATIC;
628         else
629                 params.bvh_type = SceneParams::BVH_DYNAMIC;
630
631         params.use_bvh_spatial_split = RNA_boolean_get(&cscene, "debug_use_spatial_splits");
632         params.use_bvh_unaligned_nodes = RNA_boolean_get(&cscene, "debug_use_hair_bvh");
633         params.num_bvh_time_steps = RNA_int_get(&cscene, "debug_bvh_time_steps");
634
635         if(background && params.shadingsystem != SHADINGSYSTEM_OSL)
636                 params.persistent_data = r.use_persistent_data();
637         else
638                 params.persistent_data = false;
639
640         int texture_limit;
641         if(background) {
642                 texture_limit = RNA_enum_get(&cscene, "texture_limit_render");
643         }
644         else {
645                 texture_limit = RNA_enum_get(&cscene, "texture_limit");
646         }
647         if(texture_limit > 0 && b_scene.render().use_simplify()) {
648                 params.texture_limit = 1 << (texture_limit + 6);
649         }
650         else {
651                 params.texture_limit = 0;
652         }
653
654         params.use_qbvh = DebugFlags().cpu.qbvh;
655
656         return params;
657 }
658
659 /* Session Parameters */
660
661 bool BlenderSync::get_session_pause(BL::Scene& b_scene, bool background)
662 {
663         PointerRNA cscene = RNA_pointer_get(&b_scene.ptr, "cycles");
664         return (background)? false: get_boolean(cscene, "preview_pause");
665 }
666
667 SessionParams BlenderSync::get_session_params(BL::RenderEngine& b_engine,
668                                               BL::UserPreferences& b_userpref,
669                                               BL::Scene& b_scene,
670                                               bool background)
671 {
672         SessionParams params;
673         PointerRNA cscene = RNA_pointer_get(&b_scene.ptr, "cycles");
674
675         /* feature set */
676         params.experimental = (get_enum(cscene, "feature_set") != 0);
677
678         /* threads */
679         BL::RenderSettings b_r = b_scene.render();
680         if(b_r.threads_mode() == BL::RenderSettings::threads_mode_FIXED)
681                 params.threads = b_r.threads();
682         else
683                 params.threads = 0;
684
685         /* Background */
686         params.background = background;
687
688         /* device type */
689         vector<DeviceInfo>& devices = Device::available_devices();
690         
691         /* device default CPU */
692         foreach(DeviceInfo& device, devices) {
693                 if(device.type == DEVICE_CPU) {
694                         params.device = device;
695                         break;
696                 }
697         }
698
699         if(get_enum(cscene, "device") == 2) {
700                 /* find network device */
701                 foreach(DeviceInfo& info, devices)
702                         if(info.type == DEVICE_NETWORK)
703                                 params.device = info;
704         }
705         else if(get_enum(cscene, "device") == 1) {
706                 PointerRNA b_preferences;
707
708                 BL::UserPreferences::addons_iterator b_addon_iter;
709                 for(b_userpref.addons.begin(b_addon_iter); b_addon_iter != b_userpref.addons.end(); ++b_addon_iter) {
710                         if(b_addon_iter->module() == "cycles") {
711                                 b_preferences = b_addon_iter->preferences().ptr;
712                                 break;
713                         }
714                 }
715
716                 enum ComputeDevice {
717                         COMPUTE_DEVICE_CPU = 0,
718                         COMPUTE_DEVICE_CUDA = 1,
719                         COMPUTE_DEVICE_OPENCL = 2,
720                         COMPUTE_DEVICE_NUM = 3,
721                 };
722
723                 ComputeDevice compute_device = (ComputeDevice)get_enum(b_preferences,
724                                                                        "compute_device_type",
725                                                                        COMPUTE_DEVICE_NUM,
726                                                                        COMPUTE_DEVICE_CPU);
727
728                 if(compute_device != COMPUTE_DEVICE_CPU) {
729                         vector<DeviceInfo> used_devices;
730                         RNA_BEGIN(&b_preferences, device, "devices") {
731                                 ComputeDevice device_type = (ComputeDevice)get_enum(device,
732                                                                                     "type",
733                                                                                     COMPUTE_DEVICE_NUM,
734                                                                                     COMPUTE_DEVICE_CPU);
735
736                                 if(get_boolean(device, "use") &&
737                                    (device_type == compute_device || device_type == COMPUTE_DEVICE_CPU)) {
738                                         string id = get_string(device, "id");
739                                         foreach(DeviceInfo& info, devices) {
740                                                 if(info.id == id) {
741                                                         used_devices.push_back(info);
742                                                         break;
743                                                 }
744                                         }
745                                 }
746                         } RNA_END
747
748                         if(used_devices.size() == 1) {
749                                 params.device = used_devices[0];
750                         }
751                         else if(used_devices.size() > 1) {
752                                 params.device = Device::get_multi_device(used_devices,
753                                                                          params.threads,
754                                                                          params.background);
755                         }
756                         /* Else keep using the CPU device that was set before. */
757                 }
758         }
759
760         /* samples */
761         int samples = get_int(cscene, "samples");
762         int aa_samples = get_int(cscene, "aa_samples");
763         int preview_samples = get_int(cscene, "preview_samples");
764         int preview_aa_samples = get_int(cscene, "preview_aa_samples");
765         
766         if(get_boolean(cscene, "use_square_samples")) {
767                 aa_samples = aa_samples * aa_samples;
768                 preview_aa_samples = preview_aa_samples * preview_aa_samples;
769
770                 samples = samples * samples;
771                 preview_samples = preview_samples * preview_samples;
772         }
773
774         if(get_enum(cscene, "progressive") == 0) {
775                 if(background) {
776                         params.samples = aa_samples;
777                 }
778                 else {
779                         params.samples = preview_aa_samples;
780                         if(params.samples == 0)
781                                 params.samples = INT_MAX;
782                 }
783         }
784         else {
785                 if(background) {
786                         params.samples = samples;
787                 }
788                 else {
789                         params.samples = preview_samples;
790                         if(params.samples == 0)
791                                 params.samples = INT_MAX;
792                 }
793         }
794
795         /* tiles */
796         if(params.device.type != DEVICE_CPU && !background) {
797                 /* currently GPU could be much slower than CPU when using tiles,
798                  * still need to be investigated, but meanwhile make it possible
799                  * to work in viewport smoothly
800                  */
801                 int debug_tile_size = get_int(cscene, "debug_tile_size");
802
803                 params.tile_size = make_int2(debug_tile_size, debug_tile_size);
804         }
805         else {
806                 int tile_x = b_engine.tile_x();
807                 int tile_y = b_engine.tile_y();
808
809                 params.tile_size = make_int2(tile_x, tile_y);
810         }
811
812         if((BlenderSession::headless == false) && background) {
813                 params.tile_order = (TileOrder)get_enum(cscene, "tile_order");
814         }
815         else {
816                 params.tile_order = TILE_BOTTOM_TO_TOP;
817         }
818
819         /* other parameters */
820         params.start_resolution = get_int(cscene, "preview_start_resolution");
821         params.pixel_size = b_engine.get_preview_pixel_size(b_scene);
822
823         /* other parameters */
824         params.cancel_timeout = (double)get_float(cscene, "debug_cancel_timeout");
825         params.reset_timeout = (double)get_float(cscene, "debug_reset_timeout");
826         params.text_timeout = (double)get_float(cscene, "debug_text_timeout");
827
828         /* progressive refine */
829         params.progressive_refine = get_boolean(cscene, "use_progressive_refine") &&
830                                     !b_r.use_save_buffers();
831
832         if(params.progressive_refine) {
833                 BL::RenderSettings::layers_iterator b_rlay;
834                 for(b_r.layers.begin(b_rlay); b_rlay != b_r.layers.end(); ++b_rlay) {
835                         PointerRNA crl = RNA_pointer_get(&b_rlay->ptr, "cycles");
836                         if(get_boolean(crl, "use_denoising")) {
837                                 params.progressive_refine = false;
838                         }
839                 }
840         }
841
842         if(background) {
843                 if(params.progressive_refine)
844                         params.progressive = true;
845                 else
846                         params.progressive = false;
847
848                 params.start_resolution = INT_MAX;
849                 params.pixel_size = 1;
850         }
851         else
852                 params.progressive = true;
853
854         /* shading system - scene level needs full refresh */
855         const bool shadingsystem = RNA_boolean_get(&cscene, "shading_system");
856
857         if(shadingsystem == 0)
858                 params.shadingsystem = SHADINGSYSTEM_SVM;
859         else if(shadingsystem == 1)
860                 params.shadingsystem = SHADINGSYSTEM_OSL;
861         
862         /* color managagement */
863 #ifdef GLEW_MX
864         /* When using GLEW MX we need to check whether we've got an OpenGL
865          * context for current window. This is because command line rendering
866          * doesn't have OpenGL context actually.
867          */
868         if(glewGetContext() != NULL)
869 #endif
870         {
871                 params.display_buffer_linear = GLEW_ARB_half_float_pixel &&
872                                                b_engine.support_display_space_shader(b_scene);
873         }
874
875         if(b_engine.is_preview()) {
876                 /* For preview rendering we're using same timeout as
877                  * blender's job update.
878                  */
879                 params.progressive_update_timeout = 0.1;
880         }
881
882         return params;
883 }
884
885 CCL_NAMESPACE_END
886