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