Cycles: animation denoising support in the kernel.
[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 static const char *cryptomatte_prefix = "Crypto";
44
45 /* Constructor */
46
47 BlenderSync::BlenderSync(BL::RenderEngine& b_engine,
48                          BL::BlendData& b_data,
49                          BL::Scene& b_scene,
50                          Scene *scene,
51                          bool preview,
52                          Progress &progress)
53 : b_engine(b_engine),
54   b_data(b_data),
55   b_scene(b_scene),
56   shader_map(&scene->shaders),
57   object_map(&scene->objects),
58   mesh_map(&scene->meshes),
59   light_map(&scene->lights),
60   particle_system_map(&scene->particle_systems),
61   world_map(NULL),
62   world_recalc(false),
63   scene(scene),
64   preview(preview),
65   experimental(false),
66   dicing_rate(1.0f),
67   max_subdivisions(12),
68   progress(progress)
69 {
70         PointerRNA cscene = RNA_pointer_get(&b_scene.ptr, "cycles");
71         dicing_rate = preview ? RNA_float_get(&cscene, "preview_dicing_rate") : RNA_float_get(&cscene, "dicing_rate");
72         max_subdivisions = RNA_int_get(&cscene, "max_subdivisions");
73 }
74
75 BlenderSync::~BlenderSync()
76 {
77 }
78
79 /* Sync */
80
81 bool BlenderSync::sync_recalc()
82 {
83         /* sync recalc flags from blender to cycles. actual update is done separate,
84          * so we can do it later on if doing it immediate is not suitable */
85
86         BL::BlendData::materials_iterator b_mat;
87         bool has_updated_objects = b_data.objects.is_updated();
88         for(b_data.materials.begin(b_mat); b_mat != b_data.materials.end(); ++b_mat) {
89                 if(b_mat->is_updated() || (b_mat->node_tree() && b_mat->node_tree().is_updated())) {
90                         shader_map.set_recalc(*b_mat);
91                 }
92                 else {
93                         Shader *shader = shader_map.find(*b_mat);
94                         if(has_updated_objects && shader != NULL && shader->has_object_dependency) {
95                                 shader_map.set_recalc(*b_mat);
96                         }
97                 }
98         }
99
100         BL::BlendData::lamps_iterator b_lamp;
101
102         for(b_data.lamps.begin(b_lamp); b_lamp != b_data.lamps.end(); ++b_lamp)
103                 if(b_lamp->is_updated() || (b_lamp->node_tree() && b_lamp->node_tree().is_updated()))
104                         shader_map.set_recalc(*b_lamp);
105
106         bool dicing_prop_changed = false;
107
108         if(experimental) {
109                 PointerRNA cscene = RNA_pointer_get(&b_scene.ptr, "cycles");
110
111                 float updated_dicing_rate = preview ? RNA_float_get(&cscene, "preview_dicing_rate")
112                                                     : RNA_float_get(&cscene, "dicing_rate");
113
114                 if(dicing_rate != updated_dicing_rate) {
115                         dicing_rate = updated_dicing_rate;
116                         dicing_prop_changed = true;
117                 }
118
119                 int updated_max_subdivisions = RNA_int_get(&cscene, "max_subdivisions");
120
121                 if(max_subdivisions != updated_max_subdivisions) {
122                         max_subdivisions = updated_max_subdivisions;
123                         dicing_prop_changed = true;
124                 }
125         }
126
127         BL::BlendData::objects_iterator b_ob;
128
129         for(b_data.objects.begin(b_ob); b_ob != b_data.objects.end(); ++b_ob) {
130                 if(b_ob->is_updated()) {
131                         object_map.set_recalc(*b_ob);
132                         light_map.set_recalc(*b_ob);
133                 }
134
135                 if(object_is_mesh(*b_ob)) {
136                         if(b_ob->is_updated_data() || b_ob->data().is_updated() ||
137                            (dicing_prop_changed && object_subdivision_type(*b_ob, preview, experimental) != Mesh::SUBDIVISION_NONE))
138                         {
139                                 BL::ID key = BKE_object_is_modified(*b_ob)? *b_ob: b_ob->data();
140                                 mesh_map.set_recalc(key);
141                         }
142                 }
143                 else if(object_is_light(*b_ob)) {
144                         if(b_ob->is_updated_data() || b_ob->data().is_updated())
145                                 light_map.set_recalc(*b_ob);
146                 }
147
148                 if(b_ob->is_updated_data()) {
149                         BL::Object::particle_systems_iterator b_psys;
150                         for(b_ob->particle_systems.begin(b_psys); b_psys != b_ob->particle_systems.end(); ++b_psys)
151                                 particle_system_map.set_recalc(*b_ob);
152                 }
153         }
154
155         BL::BlendData::meshes_iterator b_mesh;
156
157         for(b_data.meshes.begin(b_mesh); b_mesh != b_data.meshes.end(); ++b_mesh) {
158                 if(b_mesh->is_updated()) {
159                         mesh_map.set_recalc(*b_mesh);
160                 }
161         }
162
163         BL::BlendData::worlds_iterator b_world;
164
165         for(b_data.worlds.begin(b_world); b_world != b_data.worlds.end(); ++b_world) {
166                 if(world_map == b_world->ptr.data) {
167                         if(b_world->is_updated() ||
168                            (b_world->node_tree() && b_world->node_tree().is_updated()))
169                         {
170                                 world_recalc = true;
171                         }
172                         else if(b_world->node_tree() && b_world->use_nodes()) {
173                                 Shader *shader = scene->default_background;
174                                 if(has_updated_objects && shader->has_object_dependency) {
175                                         world_recalc = true;
176                                 }
177                         }
178                 }
179         }
180
181         bool recalc =
182                 shader_map.has_recalc() ||
183                 object_map.has_recalc() ||
184                 light_map.has_recalc() ||
185                 mesh_map.has_recalc() ||
186                 particle_system_map.has_recalc() ||
187                 BlendDataObjects_is_updated_get(&b_data.ptr) ||
188                 world_recalc;
189
190         return recalc;
191 }
192
193 void BlenderSync::sync_data(BL::RenderSettings& b_render,
194                             BL::SpaceView3D& b_v3d,
195                             BL::Object& b_override,
196                             int width, int height,
197                             void **python_thread_state,
198                             const char *layer)
199 {
200         sync_render_layers(b_v3d, layer);
201         sync_integrator();
202         sync_film();
203         sync_shaders();
204         sync_images();
205         sync_curve_settings();
206
207         mesh_synced.clear(); /* use for objects and motion sync */
208
209         if(scene->need_motion() == Scene::MOTION_PASS ||
210            scene->need_motion() == Scene::MOTION_NONE ||
211            scene->camera->motion_position == Camera::MOTION_POSITION_CENTER)
212         {
213                 sync_objects();
214         }
215         sync_motion(b_render,
216                     b_override,
217                     width, height,
218                     python_thread_state);
219
220         mesh_synced.clear();
221 }
222
223 /* Integrator */
224
225 void BlenderSync::sync_integrator()
226 {
227         BL::RenderSettings r = b_scene.render();
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         if(!preview) {
273                 if(integrator->motion_blur != r.use_motion_blur()) {
274                         scene->object_manager->tag_update(scene);
275                         scene->camera->tag_update();
276                 }
277
278                 integrator->motion_blur = r.use_motion_blur();
279         }
280
281         integrator->method = (Integrator::Method)get_enum(cscene,
282                                                           "progressive",
283                                                           Integrator::NUM_METHODS,
284                                                           Integrator::PATH);
285
286         integrator->sample_all_lights_direct = get_boolean(cscene, "sample_all_lights_direct");
287         integrator->sample_all_lights_indirect = get_boolean(cscene, "sample_all_lights_indirect");
288         integrator->light_sampling_threshold = get_float(cscene, "light_sampling_threshold");
289
290         int diffuse_samples = get_int(cscene, "diffuse_samples");
291         int glossy_samples = get_int(cscene, "glossy_samples");
292         int transmission_samples = get_int(cscene, "transmission_samples");
293         int ao_samples = get_int(cscene, "ao_samples");
294         int mesh_light_samples = get_int(cscene, "mesh_light_samples");
295         int subsurface_samples = get_int(cscene, "subsurface_samples");
296         int volume_samples = get_int(cscene, "volume_samples");
297
298         if(get_boolean(cscene, "use_square_samples")) {
299                 integrator->diffuse_samples = diffuse_samples * diffuse_samples;
300                 integrator->glossy_samples = glossy_samples * glossy_samples;
301                 integrator->transmission_samples = transmission_samples * transmission_samples;
302                 integrator->ao_samples = ao_samples * ao_samples;
303                 integrator->mesh_light_samples = mesh_light_samples * mesh_light_samples;
304                 integrator->subsurface_samples = subsurface_samples * subsurface_samples;
305                 integrator->volume_samples = volume_samples * volume_samples;
306         }
307         else {
308                 integrator->diffuse_samples = diffuse_samples;
309                 integrator->glossy_samples = glossy_samples;
310                 integrator->transmission_samples = transmission_samples;
311                 integrator->ao_samples = ao_samples;
312                 integrator->mesh_light_samples = mesh_light_samples;
313                 integrator->subsurface_samples = subsurface_samples;
314                 integrator->volume_samples = volume_samples;
315         }
316
317         if(b_scene.render().use_simplify()) {
318                 if(preview) {
319                         integrator->ao_bounces = get_int(cscene, "ao_bounces");
320                 }
321                 else {
322                         integrator->ao_bounces = get_int(cscene, "ao_bounces_render");
323                 }
324         }
325         else {
326                 integrator->ao_bounces = 0;
327         }
328
329         if(integrator->modified(previntegrator))
330                 integrator->tag_update(scene);
331 }
332
333 /* Film */
334
335 void BlenderSync::sync_film()
336 {
337         PointerRNA cscene = RNA_pointer_get(&b_scene.ptr, "cycles");
338
339         Film *film = scene->film;
340         Film prevfilm = *film;
341
342         film->exposure = get_float(cscene, "film_exposure");
343         film->filter_type = (FilterType)get_enum(cscene,
344                                                  "pixel_filter_type",
345                                                  FILTER_NUM_TYPES,
346                                                  FILTER_BLACKMAN_HARRIS);
347         film->filter_width = (film->filter_type == FILTER_BOX)? 1.0f: get_float(cscene, "filter_width");
348
349         if(b_scene.world()) {
350                 BL::WorldMistSettings b_mist = b_scene.world().mist_settings();
351
352                 film->mist_start = b_mist.start();
353                 film->mist_depth = b_mist.depth();
354
355                 switch(b_mist.falloff()) {
356                         case BL::WorldMistSettings::falloff_QUADRATIC:
357                                 film->mist_falloff = 2.0f;
358                                 break;
359                         case BL::WorldMistSettings::falloff_LINEAR:
360                                 film->mist_falloff = 1.0f;
361                                 break;
362                         case BL::WorldMistSettings::falloff_INVERSE_QUADRATIC:
363                                 film->mist_falloff = 0.5f;
364                                 break;
365                 }
366         }
367
368         if(film->modified(prevfilm))
369                 film->tag_update(scene);
370 }
371
372 /* Render Layer */
373
374 void BlenderSync::sync_render_layers(BL::SpaceView3D& b_v3d, const char *layer)
375 {
376         PointerRNA cscene = RNA_pointer_get(&b_scene.ptr, "cycles");
377         string layername;
378
379         /* 3d view */
380         if(b_v3d) {
381                 if(RNA_boolean_get(&cscene, "preview_active_layer")) {
382                         BL::RenderLayers layers(b_scene.render().ptr);
383                         layername = layers.active().name();
384                         layer = layername.c_str();
385                 }
386                 else {
387                         render_layer.scene_layer = get_layer(b_v3d.layers(), b_v3d.layers_local_view());
388                         render_layer.layer = render_layer.scene_layer;
389                         render_layer.exclude_layer = 0;
390                         render_layer.holdout_layer = 0;
391                         render_layer.material_override = PointerRNA_NULL;
392                         render_layer.use_background_shader = true;
393                         render_layer.use_background_ao = true;
394                         render_layer.use_hair = true;
395                         render_layer.use_surfaces = true;
396                         render_layer.use_viewport_visibility = true;
397                         render_layer.samples = 0;
398                         render_layer.bound_samples = false;
399                         return;
400                 }
401         }
402
403         /* render layer */
404         BL::RenderSettings r = b_scene.render();
405         BL::RenderSettings::layers_iterator b_rlay;
406         int use_layer_samples = get_enum(cscene, "use_layer_samples");
407         bool first_layer = true;
408         uint layer_override = get_layer(b_engine.layer_override());
409         uint scene_layers = layer_override ? layer_override : get_layer(b_scene.layers());
410
411         for(r.layers.begin(b_rlay); b_rlay != r.layers.end(); ++b_rlay) {
412                 if((!layer && first_layer) || (layer && b_rlay->name() == layer)) {
413                         render_layer.name = b_rlay->name();
414
415                         render_layer.holdout_layer = get_layer(b_rlay->layers_zmask());
416                         render_layer.exclude_layer = get_layer(b_rlay->layers_exclude());
417
418                         render_layer.scene_layer = scene_layers & ~render_layer.exclude_layer;
419                         render_layer.scene_layer |= render_layer.exclude_layer & render_layer.holdout_layer;
420
421                         render_layer.layer = get_layer(b_rlay->layers());
422                         render_layer.layer |= render_layer.holdout_layer;
423
424                         render_layer.material_override = b_rlay->material_override();
425                         render_layer.use_background_shader = b_rlay->use_sky();
426                         render_layer.use_background_ao = b_rlay->use_ao();
427                         render_layer.use_surfaces = b_rlay->use_solid();
428                         render_layer.use_hair = b_rlay->use_strand();
429                         render_layer.use_viewport_visibility = false;
430
431                         render_layer.bound_samples = (use_layer_samples == 1);
432                         if(use_layer_samples != 2) {
433                                 int samples = b_rlay->samples();
434                                 if(get_boolean(cscene, "use_square_samples"))
435                                         render_layer.samples = samples * samples;
436                                 else
437                                         render_layer.samples = samples;
438                         }
439                 }
440
441                 first_layer = false;
442         }
443 }
444
445 /* Images */
446 void BlenderSync::sync_images()
447 {
448         /* Sync is a convention for this API, but currently it frees unused buffers. */
449
450         const bool is_interface_locked = b_engine.render() &&
451                                          b_engine.render().use_lock_interface();
452         if(is_interface_locked == false && BlenderSession::headless == false) {
453                 /* If interface is not locked, it's possible image is needed for
454                  * the display.
455                  */
456                 return;
457         }
458         /* Free buffers used by images which are not needed for render. */
459         BL::BlendData::images_iterator b_image;
460         for(b_data.images.begin(b_image);
461             b_image != b_data.images.end();
462             ++b_image)
463         {
464                 /* TODO(sergey): Consider making it an utility function to check
465                  * whether image is considered builtin.
466                  */
467                 const bool is_builtin = b_image->packed_file() ||
468                                         b_image->source() == BL::Image::source_GENERATED ||
469                                         b_image->source() == BL::Image::source_MOVIE ||
470                                         b_engine.is_preview();
471                 if(is_builtin == false) {
472                         b_image->buffers_free();
473                 }
474                 /* TODO(sergey): Free builtin images not used by any shader. */
475         }
476 }
477
478 /* Passes */
479 PassType BlenderSync::get_pass_type(BL::RenderPass& b_pass)
480 {
481         string name = b_pass.name();
482 #define MAP_PASS(passname, passtype) if(name == passname) return passtype;
483         /* NOTE: Keep in sync with defined names from DNA_scene_types.h */
484         MAP_PASS("Combined", PASS_COMBINED);
485         MAP_PASS("Depth", PASS_DEPTH);
486         MAP_PASS("Mist", PASS_MIST);
487         MAP_PASS("Normal", PASS_NORMAL);
488         MAP_PASS("IndexOB", PASS_OBJECT_ID);
489         MAP_PASS("UV", PASS_UV);
490         MAP_PASS("Vector", PASS_MOTION);
491         MAP_PASS("IndexMA", PASS_MATERIAL_ID);
492
493         MAP_PASS("DiffDir", PASS_DIFFUSE_DIRECT);
494         MAP_PASS("GlossDir", PASS_GLOSSY_DIRECT);
495         MAP_PASS("TransDir", PASS_TRANSMISSION_DIRECT);
496         MAP_PASS("SubsurfaceDir", PASS_SUBSURFACE_DIRECT);
497         MAP_PASS("VolumeDir", PASS_VOLUME_DIRECT);
498
499         MAP_PASS("DiffInd", PASS_DIFFUSE_INDIRECT);
500         MAP_PASS("GlossInd", PASS_GLOSSY_INDIRECT);
501         MAP_PASS("TransInd", PASS_TRANSMISSION_INDIRECT);
502         MAP_PASS("SubsurfaceInd", PASS_SUBSURFACE_INDIRECT);
503         MAP_PASS("VolumeInd", PASS_VOLUME_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         MAP_PASS("Debug Render Time", PASS_RENDER_TIME);
522         if(string_startswith(name, cryptomatte_prefix)) {
523                 return PASS_CRYPTOMATTE;
524         }
525 #undef MAP_PASS
526
527         return PASS_NONE;
528 }
529
530 int BlenderSync::get_denoising_pass(BL::RenderPass& b_pass)
531 {
532         string name = b_pass.name();
533
534         if(name == "Noisy Image") return DENOISING_PASS_PREFILTERED_COLOR;
535
536         if(name.substr(0, 10) != "Denoising ") {
537                 return -1;
538         }
539         name = name.substr(10);
540
541 #define MAP_PASS(passname, offset) if(name == passname) return offset;
542         MAP_PASS("Normal", DENOISING_PASS_PREFILTERED_NORMAL);
543         MAP_PASS("Albedo", DENOISING_PASS_PREFILTERED_ALBEDO);
544         MAP_PASS("Depth", DENOISING_PASS_PREFILTERED_DEPTH);
545         MAP_PASS("Shadowing", DENOISING_PASS_PREFILTERED_SHADOWING);
546         MAP_PASS("Variance", DENOISING_PASS_PREFILTERED_VARIANCE);
547         MAP_PASS("Intensity", DENOISING_PASS_PREFILTERED_INTENSITY);
548         MAP_PASS("Clean", DENOISING_PASS_CLEAN);
549 #undef MAP_PASS
550
551         return -1;
552 }
553
554 vector<Pass> BlenderSync::sync_render_passes(BL::RenderLayer& b_rlay,
555                                              BL::SceneRenderLayer& b_srlay,
556                                              const SessionParams &session_params)
557 {
558         vector<Pass> passes;
559         Pass::add(PASS_COMBINED, passes);
560
561         if(!session_params.device.advanced_shading) {
562                 return passes;
563         }
564
565         /* loop over passes */
566         BL::RenderLayer::passes_iterator b_pass_iter;
567
568         for(b_rlay.passes.begin(b_pass_iter); b_pass_iter != b_rlay.passes.end(); ++b_pass_iter) {
569                 BL::RenderPass b_pass(*b_pass_iter);
570                 PassType pass_type = get_pass_type(b_pass);
571
572                 if(pass_type == PASS_MOTION && scene->integrator->motion_blur)
573                         continue;
574                 if(pass_type != PASS_NONE)
575                         Pass::add(pass_type, passes);
576         }
577
578         PointerRNA crp = RNA_pointer_get(&b_srlay.ptr, "cycles");
579         bool full_denoising = get_boolean(crp, "use_denoising");
580         bool write_denoising_passes = get_boolean(crp, "denoising_store_passes");
581
582         scene->film->denoising_flags = 0;
583         if(full_denoising || write_denoising_passes) {
584 #define MAP_OPTION(name, flag) if(!get_boolean(crp, name)) scene->film->denoising_flags |= flag;
585                 MAP_OPTION("denoising_diffuse_direct",        DENOISING_CLEAN_DIFFUSE_DIR);
586                 MAP_OPTION("denoising_diffuse_indirect",      DENOISING_CLEAN_DIFFUSE_IND);
587                 MAP_OPTION("denoising_glossy_direct",         DENOISING_CLEAN_GLOSSY_DIR);
588                 MAP_OPTION("denoising_glossy_indirect",       DENOISING_CLEAN_GLOSSY_IND);
589                 MAP_OPTION("denoising_transmission_direct",   DENOISING_CLEAN_TRANSMISSION_DIR);
590                 MAP_OPTION("denoising_transmission_indirect", DENOISING_CLEAN_TRANSMISSION_IND);
591                 MAP_OPTION("denoising_subsurface_direct",     DENOISING_CLEAN_SUBSURFACE_DIR);
592                 MAP_OPTION("denoising_subsurface_indirect",   DENOISING_CLEAN_SUBSURFACE_IND);
593 #undef MAP_OPTION
594                 b_engine.add_pass("Noisy Image", 4, "RGBA", b_srlay.name().c_str());
595         }
596
597         if(write_denoising_passes) {
598                 b_engine.add_pass("Denoising Normal",          3, "XYZ", b_srlay.name().c_str());
599                 b_engine.add_pass("Denoising Albedo",          3, "RGB", b_srlay.name().c_str());
600                 b_engine.add_pass("Denoising Depth",           1, "Z",   b_srlay.name().c_str());
601                 b_engine.add_pass("Denoising Shadowing",       1, "X",   b_srlay.name().c_str());
602                 b_engine.add_pass("Denoising Variance",        3, "RGB", b_srlay.name().c_str());
603                 b_engine.add_pass("Denoising Intensity",       1, "X",   b_srlay.name().c_str());
604
605                 if(scene->film->denoising_flags & DENOISING_CLEAN_ALL_PASSES) {
606                         b_engine.add_pass("Denoising Clean",   3, "RGB", b_srlay.name().c_str());
607                 }
608         }
609 #ifdef __KERNEL_DEBUG__
610         if(get_boolean(crp, "pass_debug_bvh_traversed_nodes")) {
611                 b_engine.add_pass("Debug BVH Traversed Nodes", 1, "X", b_srlay.name().c_str());
612                 Pass::add(PASS_BVH_TRAVERSED_NODES, passes);
613         }
614         if(get_boolean(crp, "pass_debug_bvh_traversed_instances")) {
615                 b_engine.add_pass("Debug BVH Traversed Instances", 1, "X", b_srlay.name().c_str());
616                 Pass::add(PASS_BVH_TRAVERSED_INSTANCES, passes);
617         }
618         if(get_boolean(crp, "pass_debug_bvh_intersections")) {
619                 b_engine.add_pass("Debug BVH Intersections", 1, "X", b_srlay.name().c_str());
620                 Pass::add(PASS_BVH_INTERSECTIONS, passes);
621         }
622         if(get_boolean(crp, "pass_debug_ray_bounces")) {
623                 b_engine.add_pass("Debug Ray Bounces", 1, "X", b_srlay.name().c_str());
624                 Pass::add(PASS_RAY_BOUNCES, passes);
625         }
626 #endif
627         if(get_boolean(crp, "pass_debug_render_time")) {
628                 b_engine.add_pass("Debug Render Time", 1, "X", b_srlay.name().c_str());
629                 Pass::add(PASS_RENDER_TIME, passes);
630         }
631         if(get_boolean(crp, "use_pass_volume_direct")) {
632                 b_engine.add_pass("VolumeDir", 3, "RGB", b_srlay.name().c_str());
633                 Pass::add(PASS_VOLUME_DIRECT, passes);
634         }
635         if(get_boolean(crp, "use_pass_volume_indirect")) {
636                 b_engine.add_pass("VolumeInd", 3, "RGB", b_srlay.name().c_str());
637                 Pass::add(PASS_VOLUME_INDIRECT, passes);
638         }
639
640         /* Cryptomatte stores two ID/weight pairs per RGBA layer.
641          * User facing paramter is the number of pairs. */
642         int crypto_depth = min(16, get_int(crp, "pass_crypto_depth")) / 2;
643         scene->film->cryptomatte_depth = crypto_depth;
644         scene->film->cryptomatte_passes = CRYPT_NONE;
645         if(get_boolean(crp, "use_pass_crypto_object")) {
646                 for(int i = 0; i < crypto_depth; ++i) {
647                         string passname = cryptomatte_prefix + string_printf("Object%02d", i);
648                         b_engine.add_pass(passname.c_str(), 4, "RGBA", b_srlay.name().c_str());
649                         Pass::add(PASS_CRYPTOMATTE, passes, passname.c_str());
650                 }
651                 scene->film->cryptomatte_passes = (CryptomatteType)(scene->film->cryptomatte_passes | CRYPT_OBJECT);
652         }
653         if(get_boolean(crp, "use_pass_crypto_material")) {
654                 for(int i = 0; i < crypto_depth; ++i) {
655                         string passname = cryptomatte_prefix + string_printf("Material%02d", i);
656                         b_engine.add_pass(passname.c_str(), 4, "RGBA", b_srlay.name().c_str());
657                         Pass::add(PASS_CRYPTOMATTE, passes, passname.c_str());
658                 }
659                 scene->film->cryptomatte_passes = (CryptomatteType)(scene->film->cryptomatte_passes | CRYPT_MATERIAL);
660         }
661         if(get_boolean(crp, "use_pass_crypto_asset")) {
662                 for(int i = 0; i < crypto_depth; ++i) {
663                         string passname = cryptomatte_prefix + string_printf("Asset%02d", i);
664                         b_engine.add_pass(passname.c_str(), 4, "RGBA", b_srlay.name().c_str());
665                         Pass::add(PASS_CRYPTOMATTE, passes, passname.c_str());
666                 }
667                 scene->film->cryptomatte_passes = (CryptomatteType)(scene->film->cryptomatte_passes | CRYPT_ASSET);
668         }
669         if(get_boolean(crp, "pass_crypto_accurate") && scene->film->cryptomatte_passes != CRYPT_NONE) {
670                 scene->film->cryptomatte_passes = (CryptomatteType)(scene->film->cryptomatte_passes | CRYPT_ACCURATE);
671         }
672
673         return passes;
674 }
675
676 /* Scene Parameters */
677
678 SceneParams BlenderSync::get_scene_params(BL::Scene& b_scene,
679                                           bool background)
680 {
681         BL::RenderSettings r = b_scene.render();
682         SceneParams params;
683         PointerRNA cscene = RNA_pointer_get(&b_scene.ptr, "cycles");
684         const bool shadingsystem = RNA_boolean_get(&cscene, "shading_system");
685
686         if(shadingsystem == 0)
687                 params.shadingsystem = SHADINGSYSTEM_SVM;
688         else if(shadingsystem == 1)
689                 params.shadingsystem = SHADINGSYSTEM_OSL;
690
691         if(background || DebugFlags().viewport_static_bvh)
692                 params.bvh_type = SceneParams::BVH_STATIC;
693         else
694                 params.bvh_type = SceneParams::BVH_DYNAMIC;
695
696         params.use_bvh_spatial_split = RNA_boolean_get(&cscene, "debug_use_spatial_splits");
697         params.use_bvh_unaligned_nodes = RNA_boolean_get(&cscene, "debug_use_hair_bvh");
698         params.num_bvh_time_steps = RNA_int_get(&cscene, "debug_bvh_time_steps");
699
700         if(background && params.shadingsystem != SHADINGSYSTEM_OSL)
701                 params.persistent_data = r.use_persistent_data();
702         else
703                 params.persistent_data = false;
704
705         int texture_limit;
706         if(background) {
707                 texture_limit = RNA_enum_get(&cscene, "texture_limit_render");
708         }
709         else {
710                 texture_limit = RNA_enum_get(&cscene, "texture_limit");
711         }
712         if(texture_limit > 0 && b_scene.render().use_simplify()) {
713                 params.texture_limit = 1 << (texture_limit + 6);
714         }
715         else {
716                 params.texture_limit = 0;
717         }
718
719         /* TODO(sergey): Once OSL supports per-microarchitecture optimization get
720          * rid of this.
721          */
722         if(params.shadingsystem == SHADINGSYSTEM_OSL) {
723                 params.bvh_layout = BVH_LAYOUT_BVH4;
724         }
725         else {
726                 params.bvh_layout = DebugFlags().cpu.bvh_layout;
727         }
728
729 #ifdef WITH_EMBREE
730         params.bvh_layout = RNA_boolean_get(&cscene, "use_bvh_embree") ? BVH_LAYOUT_EMBREE : params.bvh_layout;
731 #endif
732         return params;
733 }
734
735 /* Session Parameters */
736
737 bool BlenderSync::get_session_pause(BL::Scene& b_scene, bool background)
738 {
739         PointerRNA cscene = RNA_pointer_get(&b_scene.ptr, "cycles");
740         return (background)? false: get_boolean(cscene, "preview_pause");
741 }
742
743 SessionParams BlenderSync::get_session_params(BL::RenderEngine& b_engine,
744                                               BL::UserPreferences& b_userpref,
745                                               BL::Scene& b_scene,
746                                               bool background)
747 {
748         SessionParams params;
749         PointerRNA cscene = RNA_pointer_get(&b_scene.ptr, "cycles");
750
751         /* feature set */
752         params.experimental = (get_enum(cscene, "feature_set") != 0);
753
754         /* threads */
755         BL::RenderSettings b_r = b_scene.render();
756         if(b_r.threads_mode() == BL::RenderSettings::threads_mode_FIXED)
757                 params.threads = b_r.threads();
758         else
759                 params.threads = 0;
760
761         /* Background */
762         params.background = background;
763
764         /* Default to CPU device. */
765         params.device = Device::available_devices(DEVICE_MASK_CPU).front();
766
767         if(get_enum(cscene, "device") == 2) {
768                 /* Find network device. */
769                 vector<DeviceInfo> devices = Device::available_devices(DEVICE_MASK_NETWORK);
770                 if(!devices.empty()) {
771                         params.device = devices.front();
772                 }
773         }
774         else if(get_enum(cscene, "device") == 1) {
775                 /* Find cycles preferences. */
776                 PointerRNA b_preferences;
777
778                 BL::UserPreferences::addons_iterator b_addon_iter;
779                 for(b_userpref.addons.begin(b_addon_iter); b_addon_iter != b_userpref.addons.end(); ++b_addon_iter) {
780                         if(b_addon_iter->module() == "cycles") {
781                                 b_preferences = b_addon_iter->preferences().ptr;
782                                 break;
783                         }
784                 }
785
786                 /* Test if we are using GPU devices. */
787                 enum ComputeDevice {
788                         COMPUTE_DEVICE_CPU = 0,
789                         COMPUTE_DEVICE_CUDA = 1,
790                         COMPUTE_DEVICE_OPENCL = 2,
791                         COMPUTE_DEVICE_NUM = 3,
792                 };
793
794                 ComputeDevice compute_device = (ComputeDevice)get_enum(b_preferences,
795                                                                        "compute_device_type",
796                                                                        COMPUTE_DEVICE_NUM,
797                                                                        COMPUTE_DEVICE_CPU);
798
799                 if(compute_device != COMPUTE_DEVICE_CPU) {
800                         /* Query GPU devices with matching types. */
801                         uint mask = DEVICE_MASK_CPU;
802                         if(compute_device == COMPUTE_DEVICE_CUDA) {
803                                 mask |= DEVICE_MASK_CUDA;
804                         }
805                         else if(compute_device == COMPUTE_DEVICE_OPENCL) {
806                                 mask |= DEVICE_MASK_OPENCL;
807                         }
808                         vector<DeviceInfo> devices = Device::available_devices(mask);
809
810                         /* Match device preferences and available devices. */
811                         vector<DeviceInfo> used_devices;
812                         RNA_BEGIN(&b_preferences, device, "devices") {
813                                 if(get_boolean(device, "use")) {
814                                         string id = get_string(device, "id");
815                                         foreach(DeviceInfo& info, devices) {
816                                                 if(info.id == id) {
817                                                         used_devices.push_back(info);
818                                                         break;
819                                                 }
820                                         }
821                                 }
822                         } RNA_END;
823
824                         if(!used_devices.empty()) {
825                                 params.device = Device::get_multi_device(used_devices,
826                                                                          params.threads,
827                                                                          params.background);
828                         }
829                         /* Else keep using the CPU device that was set before. */
830                 }
831         }
832
833         /* samples */
834         int samples = get_int(cscene, "samples");
835         int aa_samples = get_int(cscene, "aa_samples");
836         int preview_samples = get_int(cscene, "preview_samples");
837         int preview_aa_samples = get_int(cscene, "preview_aa_samples");
838
839         if(get_boolean(cscene, "use_square_samples")) {
840                 aa_samples = aa_samples * aa_samples;
841                 preview_aa_samples = preview_aa_samples * preview_aa_samples;
842
843                 samples = samples * samples;
844                 preview_samples = preview_samples * preview_samples;
845         }
846
847         if(get_enum(cscene, "progressive") == 0) {
848                 if(background) {
849                         params.samples = aa_samples;
850                 }
851                 else {
852                         params.samples = preview_aa_samples;
853                         if(params.samples == 0)
854                                 params.samples = INT_MAX;
855                 }
856         }
857         else {
858                 if(background) {
859                         params.samples = samples;
860                 }
861                 else {
862                         params.samples = preview_samples;
863                         if(params.samples == 0)
864                                 params.samples = INT_MAX;
865                 }
866         }
867
868         /* tiles */
869         const bool is_cpu = (params.device.type == DEVICE_CPU);
870         if(!is_cpu && !background) {
871                 /* currently GPU could be much slower than CPU when using tiles,
872                  * still need to be investigated, but meanwhile make it possible
873                  * to work in viewport smoothly
874                  */
875                 int debug_tile_size = get_int(cscene, "debug_tile_size");
876
877                 params.tile_size = make_int2(debug_tile_size, debug_tile_size);
878         }
879         else {
880                 int tile_x = b_engine.tile_x();
881                 int tile_y = b_engine.tile_y();
882
883                 params.tile_size = make_int2(tile_x, tile_y);
884         }
885
886         if((BlenderSession::headless == false) && background) {
887                 params.tile_order = (TileOrder)get_enum(cscene, "tile_order");
888         }
889         else {
890                 params.tile_order = TILE_BOTTOM_TO_TOP;
891         }
892
893         /* other parameters */
894         params.start_resolution = get_int(cscene, "preview_start_resolution");
895         params.pixel_size = b_engine.get_preview_pixel_size(b_scene);
896
897         /* other parameters */
898         params.cancel_timeout = (double)get_float(cscene, "debug_cancel_timeout");
899         params.reset_timeout = (double)get_float(cscene, "debug_reset_timeout");
900         params.text_timeout = (double)get_float(cscene, "debug_text_timeout");
901
902         /* progressive refine */
903         params.progressive_refine = get_boolean(cscene, "use_progressive_refine") &&
904                                     !b_r.use_save_buffers();
905
906         if(params.progressive_refine) {
907                 BL::RenderSettings::layers_iterator b_rlay;
908                 for(b_r.layers.begin(b_rlay); b_rlay != b_r.layers.end(); ++b_rlay) {
909                         PointerRNA crl = RNA_pointer_get(&b_rlay->ptr, "cycles");
910                         if(get_boolean(crl, "use_denoising")) {
911                                 params.progressive_refine = false;
912                         }
913                 }
914         }
915
916         if(background) {
917                 if(params.progressive_refine)
918                         params.progressive = true;
919                 else
920                         params.progressive = false;
921
922                 params.start_resolution = INT_MAX;
923                 params.pixel_size = 1;
924         }
925         else
926                 params.progressive = true;
927
928         /* shading system - scene level needs full refresh */
929         const bool shadingsystem = RNA_boolean_get(&cscene, "shading_system");
930
931         if(shadingsystem == 0)
932                 params.shadingsystem = SHADINGSYSTEM_SVM;
933         else if(shadingsystem == 1)
934                 params.shadingsystem = SHADINGSYSTEM_OSL;
935
936         /* color managagement */
937 #ifdef GLEW_MX
938         /* When using GLEW MX we need to check whether we've got an OpenGL
939          * context for current window. This is because command line rendering
940          * doesn't have OpenGL context actually.
941          */
942         if(glewGetContext() != NULL)
943 #endif
944         {
945                 params.display_buffer_linear = GLEW_ARB_half_float_pixel &&
946                                                b_engine.support_display_space_shader(b_scene);
947         }
948
949         if(b_engine.is_preview()) {
950                 /* For preview rendering we're using same timeout as
951                  * blender's job update.
952                  */
953                 params.progressive_update_timeout = 0.1;
954         }
955
956         params.use_profiling = params.device.has_profiling && !b_engine.is_preview() &&
957                                background && BlenderSession::print_render_stats;
958
959         return params;
960 }
961
962 CCL_NAMESPACE_END