Merge branch 'blender2.7'
[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_device.h"
33 #include "blender/blender_sync.h"
34 #include "blender/blender_session.h"
35 #include "blender/blender_util.h"
36
37 #include "util/util_debug.h"
38 #include "util/util_foreach.h"
39 #include "util/util_opengl.h"
40 #include "util/util_hash.h"
41
42 CCL_NAMESPACE_BEGIN
43
44 static const char *cryptomatte_prefix = "Crypto";
45
46 /* Constructor */
47
48 BlenderSync::BlenderSync(BL::RenderEngine& b_engine,
49                          BL::BlendData& b_data,
50                          BL::Scene& b_scene,
51                          Scene *scene,
52                          bool preview,
53                          Progress &progress)
54 : b_engine(b_engine),
55   b_data(b_data),
56   b_scene(b_scene),
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 void BlenderSync::sync_recalc(BL::Depsgraph& b_depsgraph)
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         bool has_updated_objects = b_depsgraph.id_type_updated(BL::DriverTarget::id_type_OBJECT);
88         bool dicing_prop_changed = false;
89
90         if(experimental) {
91                 PointerRNA cscene = RNA_pointer_get(&b_scene.ptr, "cycles");
92
93                 float updated_dicing_rate = preview ? RNA_float_get(&cscene, "preview_dicing_rate")
94                                                     : RNA_float_get(&cscene, "dicing_rate");
95
96                 if(dicing_rate != updated_dicing_rate) {
97                         dicing_rate = updated_dicing_rate;
98                         dicing_prop_changed = true;
99                 }
100
101                 int updated_max_subdivisions = RNA_int_get(&cscene, "max_subdivisions");
102
103                 if(max_subdivisions != updated_max_subdivisions) {
104                         max_subdivisions = updated_max_subdivisions;
105                         dicing_prop_changed = true;
106                 }
107         }
108
109         /* Iterate over all IDs in this depsgraph. */
110         BL::Depsgraph::updates_iterator b_update;
111         for(b_depsgraph.updates.begin(b_update); b_update != b_depsgraph.updates.end(); ++b_update) {
112                 BL::ID b_id(b_update->id());
113
114                 /* Material */
115                 if(b_id.is_a(&RNA_Material)) {
116                         BL::Material b_mat(b_id);
117                         shader_map.set_recalc(b_mat);
118                 }
119                 /* Light */
120                 else if(b_id.is_a(&RNA_Light)) {
121                         BL::Light b_light(b_id);
122                         shader_map.set_recalc(b_light);
123                 }
124                 /* Object */
125                 else if(b_id.is_a(&RNA_Object)) {
126                         BL::Object b_ob(b_id);
127                         const bool updated_geometry = b_update->is_updated_geometry();
128
129                         if(b_update->is_updated_transform()) {
130                                 object_map.set_recalc(b_ob);
131                                 light_map.set_recalc(b_ob);
132                         }
133
134                         if(object_is_mesh(b_ob)) {
135                                 if(updated_geometry ||
136                                    (dicing_prop_changed && object_subdivision_type(b_ob, preview, experimental) != Mesh::SUBDIVISION_NONE))
137                                 {
138                                         BL::ID key = BKE_object_is_modified(b_ob)? b_ob: b_ob.data();
139                                         mesh_map.set_recalc(key);
140                                 }
141                         }
142                         else if(object_is_light(b_ob)) {
143                                 if(updated_geometry) {
144                                         light_map.set_recalc(b_ob);
145                                 }
146                         }
147
148                         if(updated_geometry) {
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                 /* Mesh */
155                 else if(b_id.is_a(&RNA_Mesh)) {
156                         BL::Mesh b_mesh(b_id);
157                         mesh_map.set_recalc(b_mesh);
158                 }
159                 /* World */
160                 else if(b_id.is_a(&RNA_World)) {
161                         BL::World b_world(b_id);
162                         if(world_map == b_world.ptr.data) {
163                                 world_recalc = true;
164                         }
165                 }
166         }
167
168         /* Updates shader with object dependency if objects changed. */
169         if(has_updated_objects) {
170                 if(scene->default_background->has_object_dependency) {
171                         world_recalc = true;
172                 }
173
174                 foreach(Shader *shader, scene->shaders) {
175                         if(shader->has_object_dependency) {
176                                 shader->need_sync_object = true;
177                         }
178                 }
179         }
180 }
181
182 void BlenderSync::sync_data(BL::RenderSettings& b_render,
183                             BL::Depsgraph& b_depsgraph,
184                             BL::SpaceView3D& b_v3d,
185                             BL::Object& b_override,
186                             int width, int height,
187                             void **python_thread_state)
188 {
189         BL::ViewLayer b_view_layer = b_depsgraph.view_layer_eval();
190
191         sync_view_layer(b_v3d, b_view_layer);
192         sync_integrator();
193         sync_film();
194         sync_shaders(b_depsgraph);
195         sync_images();
196         sync_curve_settings();
197
198         mesh_synced.clear(); /* use for objects and motion sync */
199
200         if(scene->need_motion() == Scene::MOTION_PASS ||
201            scene->need_motion() == Scene::MOTION_NONE ||
202            scene->camera->motion_position == Camera::MOTION_POSITION_CENTER)
203         {
204                 sync_objects(b_depsgraph);
205         }
206         sync_motion(b_render,
207                     b_depsgraph,
208                     b_override,
209                     width, height,
210                     python_thread_state);
211
212         mesh_synced.clear();
213
214         free_data_after_sync(b_depsgraph);
215 }
216
217 /* Integrator */
218
219 void BlenderSync::sync_integrator()
220 {
221         BL::RenderSettings r = b_scene.render();
222         PointerRNA cscene = RNA_pointer_get(&b_scene.ptr, "cycles");
223
224         experimental = (get_enum(cscene, "feature_set") != 0);
225
226         Integrator *integrator = scene->integrator;
227         Integrator previntegrator = *integrator;
228
229         integrator->max_bounce = get_int(cscene, "max_bounces");
230
231         integrator->max_diffuse_bounce = get_int(cscene, "diffuse_bounces");
232         integrator->max_glossy_bounce = get_int(cscene, "glossy_bounces");
233         integrator->max_transmission_bounce = get_int(cscene, "transmission_bounces");
234         integrator->max_volume_bounce = get_int(cscene, "volume_bounces");
235
236         integrator->transparent_max_bounce = get_int(cscene, "transparent_max_bounces");
237
238         integrator->volume_max_steps = get_int(cscene, "volume_max_steps");
239         integrator->volume_step_size = get_float(cscene, "volume_step_size");
240
241         integrator->caustics_reflective = get_boolean(cscene, "caustics_reflective");
242         integrator->caustics_refractive = get_boolean(cscene, "caustics_refractive");
243         integrator->filter_glossy = get_float(cscene, "blur_glossy");
244
245         integrator->seed = get_int(cscene, "seed");
246         if(get_boolean(cscene, "use_animated_seed")) {
247                 integrator->seed = hash_int_2d(b_scene.frame_current(),
248                                                get_int(cscene, "seed"));
249                 if(b_scene.frame_subframe() != 0.0f) {
250                         /* TODO(sergey): Ideally should be some sort of hash_merge,
251                          * but this is good enough for now.
252                          */
253                         integrator->seed += hash_int_2d((int)(b_scene.frame_subframe() * (float)INT_MAX),
254                                                         get_int(cscene, "seed"));
255                 }
256         }
257
258         integrator->sampling_pattern = (SamplingPattern)get_enum(
259                 cscene,
260                 "sampling_pattern",
261                 SAMPLING_NUM_PATTERNS,
262                 SAMPLING_PATTERN_SOBOL);
263
264         integrator->sample_clamp_direct = get_float(cscene, "sample_clamp_direct");
265         integrator->sample_clamp_indirect = get_float(cscene, "sample_clamp_indirect");
266         if(!preview) {
267                 if(integrator->motion_blur != r.use_motion_blur()) {
268                         scene->object_manager->tag_update(scene);
269                         scene->camera->tag_update();
270                 }
271
272                 integrator->motion_blur = r.use_motion_blur();
273         }
274
275         integrator->method = (Integrator::Method)get_enum(cscene,
276                                                           "progressive",
277                                                           Integrator::NUM_METHODS,
278                                                           Integrator::PATH);
279
280         integrator->sample_all_lights_direct = get_boolean(cscene, "sample_all_lights_direct");
281         integrator->sample_all_lights_indirect = get_boolean(cscene, "sample_all_lights_indirect");
282         integrator->light_sampling_threshold = get_float(cscene, "light_sampling_threshold");
283
284         int diffuse_samples = get_int(cscene, "diffuse_samples");
285         int glossy_samples = get_int(cscene, "glossy_samples");
286         int transmission_samples = get_int(cscene, "transmission_samples");
287         int ao_samples = get_int(cscene, "ao_samples");
288         int mesh_light_samples = get_int(cscene, "mesh_light_samples");
289         int subsurface_samples = get_int(cscene, "subsurface_samples");
290         int volume_samples = get_int(cscene, "volume_samples");
291
292         if(get_boolean(cscene, "use_square_samples")) {
293                 integrator->diffuse_samples = diffuse_samples * diffuse_samples;
294                 integrator->glossy_samples = glossy_samples * glossy_samples;
295                 integrator->transmission_samples = transmission_samples * transmission_samples;
296                 integrator->ao_samples = ao_samples * ao_samples;
297                 integrator->mesh_light_samples = mesh_light_samples * mesh_light_samples;
298                 integrator->subsurface_samples = subsurface_samples * subsurface_samples;
299                 integrator->volume_samples = volume_samples * volume_samples;
300         }
301         else {
302                 integrator->diffuse_samples = diffuse_samples;
303                 integrator->glossy_samples = glossy_samples;
304                 integrator->transmission_samples = transmission_samples;
305                 integrator->ao_samples = ao_samples;
306                 integrator->mesh_light_samples = mesh_light_samples;
307                 integrator->subsurface_samples = subsurface_samples;
308                 integrator->volume_samples = volume_samples;
309         }
310
311         if(b_scene.render().use_simplify()) {
312                 if(preview) {
313                         integrator->ao_bounces = get_int(cscene, "ao_bounces");
314                 }
315                 else {
316                         integrator->ao_bounces = get_int(cscene, "ao_bounces_render");
317                 }
318         }
319         else {
320                 integrator->ao_bounces = 0;
321         }
322
323         if(integrator->modified(previntegrator))
324                 integrator->tag_update(scene);
325 }
326
327 /* Film */
328
329 void BlenderSync::sync_film()
330 {
331         PointerRNA cscene = RNA_pointer_get(&b_scene.ptr, "cycles");
332
333         Film *film = scene->film;
334         Film prevfilm = *film;
335
336         film->exposure = get_float(cscene, "film_exposure");
337         film->filter_type = (FilterType)get_enum(cscene,
338                                                  "pixel_filter_type",
339                                                  FILTER_NUM_TYPES,
340                                                  FILTER_BLACKMAN_HARRIS);
341         film->filter_width = (film->filter_type == FILTER_BOX)? 1.0f: get_float(cscene, "filter_width");
342
343         if(b_scene.world()) {
344                 BL::WorldMistSettings b_mist = b_scene.world().mist_settings();
345
346                 film->mist_start = b_mist.start();
347                 film->mist_depth = b_mist.depth();
348
349                 switch(b_mist.falloff()) {
350                         case BL::WorldMistSettings::falloff_QUADRATIC:
351                                 film->mist_falloff = 2.0f;
352                                 break;
353                         case BL::WorldMistSettings::falloff_LINEAR:
354                                 film->mist_falloff = 1.0f;
355                                 break;
356                         case BL::WorldMistSettings::falloff_INVERSE_QUADRATIC:
357                                 film->mist_falloff = 0.5f;
358                                 break;
359                 }
360         }
361
362         if(film->modified(prevfilm))
363                 film->tag_update(scene);
364 }
365
366 /* Render Layer */
367
368 void BlenderSync::sync_view_layer(BL::SpaceView3D& /*b_v3d*/, BL::ViewLayer& b_view_layer)
369 {
370         /* render layer */
371         view_layer.name = b_view_layer.name();
372         view_layer.use_background_shader = b_view_layer.use_sky();
373         view_layer.use_background_ao = b_view_layer.use_ao();
374         view_layer.use_surfaces = b_view_layer.use_solid();
375         view_layer.use_hair = b_view_layer.use_strand();
376
377         /* Material override. */
378         view_layer.material_override = b_view_layer.material_override();
379
380         /* Sample override. */
381         PointerRNA cscene = RNA_pointer_get(&b_scene.ptr, "cycles");
382         int use_layer_samples = get_enum(cscene, "use_layer_samples");
383
384         view_layer.bound_samples = (use_layer_samples == 1);
385         view_layer.samples = 0;
386
387         if(use_layer_samples != 2) {
388                 int samples = b_view_layer.samples();
389                 if(get_boolean(cscene, "use_square_samples"))
390                         view_layer.samples = samples * samples;
391                 else
392                         view_layer.samples = samples;
393         }
394
395 }
396
397 /* Images */
398 void BlenderSync::sync_images()
399 {
400         /* Sync is a convention for this API, but currently it frees unused buffers. */
401
402         const bool is_interface_locked = b_engine.render() &&
403                                          b_engine.render().use_lock_interface();
404         if(is_interface_locked == false && BlenderSession::headless == false) {
405                 /* If interface is not locked, it's possible image is needed for
406                  * the display.
407                  */
408                 return;
409         }
410         /* Free buffers used by images which are not needed for render. */
411         BL::BlendData::images_iterator b_image;
412         for(b_data.images.begin(b_image);
413             b_image != b_data.images.end();
414             ++b_image)
415         {
416                 /* TODO(sergey): Consider making it an utility function to check
417                  * whether image is considered builtin.
418                  */
419                 const bool is_builtin = b_image->packed_file() ||
420                                         b_image->source() == BL::Image::source_GENERATED ||
421                                         b_image->source() == BL::Image::source_MOVIE ||
422                                         b_engine.is_preview();
423                 if(is_builtin == false) {
424                         b_image->buffers_free();
425                 }
426                 /* TODO(sergey): Free builtin images not used by any shader. */
427         }
428 }
429
430 /* Passes */
431 PassType BlenderSync::get_pass_type(BL::RenderPass& b_pass)
432 {
433         string name = b_pass.name();
434 #define MAP_PASS(passname, passtype) if(name == passname) return passtype;
435         /* NOTE: Keep in sync with defined names from DNA_scene_types.h */
436         MAP_PASS("Combined", PASS_COMBINED);
437         MAP_PASS("Depth", PASS_DEPTH);
438         MAP_PASS("Mist", PASS_MIST);
439         MAP_PASS("Normal", PASS_NORMAL);
440         MAP_PASS("IndexOB", PASS_OBJECT_ID);
441         MAP_PASS("UV", PASS_UV);
442         MAP_PASS("Vector", PASS_MOTION);
443         MAP_PASS("IndexMA", PASS_MATERIAL_ID);
444
445         MAP_PASS("DiffDir", PASS_DIFFUSE_DIRECT);
446         MAP_PASS("GlossDir", PASS_GLOSSY_DIRECT);
447         MAP_PASS("TransDir", PASS_TRANSMISSION_DIRECT);
448         MAP_PASS("SubsurfaceDir", PASS_SUBSURFACE_DIRECT);
449         MAP_PASS("VolumeDir", PASS_VOLUME_DIRECT);
450
451         MAP_PASS("DiffInd", PASS_DIFFUSE_INDIRECT);
452         MAP_PASS("GlossInd", PASS_GLOSSY_INDIRECT);
453         MAP_PASS("TransInd", PASS_TRANSMISSION_INDIRECT);
454         MAP_PASS("SubsurfaceInd", PASS_SUBSURFACE_INDIRECT);
455         MAP_PASS("VolumeInd", PASS_VOLUME_INDIRECT);
456
457         MAP_PASS("DiffCol", PASS_DIFFUSE_COLOR);
458         MAP_PASS("GlossCol", PASS_GLOSSY_COLOR);
459         MAP_PASS("TransCol", PASS_TRANSMISSION_COLOR);
460         MAP_PASS("SubsurfaceCol", PASS_SUBSURFACE_COLOR);
461
462         MAP_PASS("Emit", PASS_EMISSION);
463         MAP_PASS("Env", PASS_BACKGROUND);
464         MAP_PASS("AO", PASS_AO);
465         MAP_PASS("Shadow", PASS_SHADOW);
466
467 #ifdef __KERNEL_DEBUG__
468         MAP_PASS("Debug BVH Traversed Nodes", PASS_BVH_TRAVERSED_NODES);
469         MAP_PASS("Debug BVH Traversed Instances", PASS_BVH_TRAVERSED_INSTANCES);
470         MAP_PASS("Debug BVH Intersections", PASS_BVH_INTERSECTIONS);
471         MAP_PASS("Debug Ray Bounces", PASS_RAY_BOUNCES);
472 #endif
473         MAP_PASS("Debug Render Time", PASS_RENDER_TIME);
474         if(string_startswith(name, cryptomatte_prefix)) {
475                 return PASS_CRYPTOMATTE;
476         }
477 #undef MAP_PASS
478
479         return PASS_NONE;
480 }
481
482 int BlenderSync::get_denoising_pass(BL::RenderPass& b_pass)
483 {
484         string name = b_pass.name();
485
486         if(name == "Noisy Image") return DENOISING_PASS_PREFILTERED_COLOR;
487
488         if(name.substr(0, 10) != "Denoising ") {
489                 return -1;
490         }
491         name = name.substr(10);
492
493 #define MAP_PASS(passname, offset) if(name == passname) return offset;
494         MAP_PASS("Normal", DENOISING_PASS_PREFILTERED_NORMAL);
495         MAP_PASS("Albedo", DENOISING_PASS_PREFILTERED_ALBEDO);
496         MAP_PASS("Depth", DENOISING_PASS_PREFILTERED_DEPTH);
497         MAP_PASS("Shadowing", DENOISING_PASS_PREFILTERED_SHADOWING);
498         MAP_PASS("Variance", DENOISING_PASS_PREFILTERED_VARIANCE);
499         MAP_PASS("Intensity", DENOISING_PASS_PREFILTERED_INTENSITY);
500         MAP_PASS("Clean", DENOISING_PASS_CLEAN);
501 #undef MAP_PASS
502
503         return -1;
504 }
505
506 vector<Pass> BlenderSync::sync_render_passes(BL::RenderLayer& b_rlay,
507                                              BL::ViewLayer& b_view_layer,
508                                              const SessionParams &session_params)
509 {
510         vector<Pass> passes;
511         Pass::add(PASS_COMBINED, passes);
512
513         if(!session_params.device.advanced_shading) {
514                 return passes;
515         }
516
517         /* loop over passes */
518         BL::RenderLayer::passes_iterator b_pass_iter;
519
520         for(b_rlay.passes.begin(b_pass_iter); b_pass_iter != b_rlay.passes.end(); ++b_pass_iter) {
521                 BL::RenderPass b_pass(*b_pass_iter);
522                 PassType pass_type = get_pass_type(b_pass);
523
524                 if(pass_type == PASS_MOTION && scene->integrator->motion_blur)
525                         continue;
526                 if(pass_type != PASS_NONE)
527                         Pass::add(pass_type, passes);
528         }
529
530         PointerRNA crp = RNA_pointer_get(&b_view_layer.ptr, "cycles");
531         bool full_denoising = get_boolean(crp, "use_denoising");
532         bool write_denoising_passes = get_boolean(crp, "denoising_store_passes");
533
534         scene->film->denoising_flags = 0;
535         if(full_denoising || write_denoising_passes) {
536 #define MAP_OPTION(name, flag) if(!get_boolean(crp, name)) scene->film->denoising_flags |= flag;
537                 MAP_OPTION("denoising_diffuse_direct",        DENOISING_CLEAN_DIFFUSE_DIR);
538                 MAP_OPTION("denoising_diffuse_indirect",      DENOISING_CLEAN_DIFFUSE_IND);
539                 MAP_OPTION("denoising_glossy_direct",         DENOISING_CLEAN_GLOSSY_DIR);
540                 MAP_OPTION("denoising_glossy_indirect",       DENOISING_CLEAN_GLOSSY_IND);
541                 MAP_OPTION("denoising_transmission_direct",   DENOISING_CLEAN_TRANSMISSION_DIR);
542                 MAP_OPTION("denoising_transmission_indirect", DENOISING_CLEAN_TRANSMISSION_IND);
543                 MAP_OPTION("denoising_subsurface_direct",     DENOISING_CLEAN_SUBSURFACE_DIR);
544                 MAP_OPTION("denoising_subsurface_indirect",   DENOISING_CLEAN_SUBSURFACE_IND);
545 #undef MAP_OPTION
546                 b_engine.add_pass("Noisy Image", 4, "RGBA", b_view_layer.name().c_str());
547         }
548
549         if(write_denoising_passes) {
550                 b_engine.add_pass("Denoising Normal",          3, "XYZ", b_view_layer.name().c_str());
551                 b_engine.add_pass("Denoising Albedo",          3, "RGB", b_view_layer.name().c_str());
552                 b_engine.add_pass("Denoising Depth",           1, "Z",   b_view_layer.name().c_str());
553                 b_engine.add_pass("Denoising Shadowing",       1, "X",   b_view_layer.name().c_str());
554                 b_engine.add_pass("Denoising Variance",        3, "RGB", b_view_layer.name().c_str());
555                 b_engine.add_pass("Denoising Intensity",       1, "X",   b_view_layer.name().c_str());
556
557                 if(scene->film->denoising_flags & DENOISING_CLEAN_ALL_PASSES) {
558                         b_engine.add_pass("Denoising Clean",   3, "RGB", b_view_layer.name().c_str());
559                 }
560         }
561 #ifdef __KERNEL_DEBUG__
562         if(get_boolean(crp, "pass_debug_bvh_traversed_nodes")) {
563                 b_engine.add_pass("Debug BVH Traversed Nodes", 1, "X", b_view_layer.name().c_str());
564                 Pass::add(PASS_BVH_TRAVERSED_NODES, passes);
565         }
566         if(get_boolean(crp, "pass_debug_bvh_traversed_instances")) {
567                 b_engine.add_pass("Debug BVH Traversed Instances", 1, "X", b_view_layer.name().c_str());
568                 Pass::add(PASS_BVH_TRAVERSED_INSTANCES, passes);
569         }
570         if(get_boolean(crp, "pass_debug_bvh_intersections")) {
571                 b_engine.add_pass("Debug BVH Intersections", 1, "X", b_view_layer.name().c_str());
572                 Pass::add(PASS_BVH_INTERSECTIONS, passes);
573         }
574         if(get_boolean(crp, "pass_debug_ray_bounces")) {
575                 b_engine.add_pass("Debug Ray Bounces", 1, "X", b_view_layer.name().c_str());
576                 Pass::add(PASS_RAY_BOUNCES, passes);
577         }
578 #endif
579         if(get_boolean(crp, "pass_debug_render_time")) {
580                 b_engine.add_pass("Debug Render Time", 1, "X", b_view_layer.name().c_str());
581                 Pass::add(PASS_RENDER_TIME, passes);
582         }
583         if(get_boolean(crp, "use_pass_volume_direct")) {
584                 b_engine.add_pass("VolumeDir", 3, "RGB", b_view_layer.name().c_str());
585                 Pass::add(PASS_VOLUME_DIRECT, passes);
586         }
587         if(get_boolean(crp, "use_pass_volume_indirect")) {
588                 b_engine.add_pass("VolumeInd", 3, "RGB", b_view_layer.name().c_str());
589                 Pass::add(PASS_VOLUME_INDIRECT, passes);
590         }
591
592         /* Cryptomatte stores two ID/weight pairs per RGBA layer.
593          * User facing paramter is the number of pairs. */
594         int crypto_depth = min(16, get_int(crp, "pass_crypto_depth")) / 2;
595         scene->film->cryptomatte_depth = crypto_depth;
596         scene->film->cryptomatte_passes = CRYPT_NONE;
597         if(get_boolean(crp, "use_pass_crypto_object")) {
598                 for(int i = 0; i < crypto_depth; ++i) {
599                         string passname = cryptomatte_prefix + string_printf("Object%02d", i);
600                         b_engine.add_pass(passname.c_str(), 4, "RGBA", b_view_layer.name().c_str());
601                         Pass::add(PASS_CRYPTOMATTE, passes, passname.c_str());
602                 }
603                 scene->film->cryptomatte_passes = (CryptomatteType)(scene->film->cryptomatte_passes | CRYPT_OBJECT);
604         }
605         if(get_boolean(crp, "use_pass_crypto_material")) {
606                 for(int i = 0; i < crypto_depth; ++i) {
607                         string passname = cryptomatte_prefix + string_printf("Material%02d", i);
608                         b_engine.add_pass(passname.c_str(), 4, "RGBA", b_view_layer.name().c_str());
609                         Pass::add(PASS_CRYPTOMATTE, passes, passname.c_str());
610                 }
611                 scene->film->cryptomatte_passes = (CryptomatteType)(scene->film->cryptomatte_passes | CRYPT_MATERIAL);
612         }
613         if(get_boolean(crp, "use_pass_crypto_asset")) {
614                 for(int i = 0; i < crypto_depth; ++i) {
615                         string passname = cryptomatte_prefix + string_printf("Asset%02d", i);
616                         b_engine.add_pass(passname.c_str(), 4, "RGBA", b_view_layer.name().c_str());
617                         Pass::add(PASS_CRYPTOMATTE, passes, passname.c_str());
618                 }
619                 scene->film->cryptomatte_passes = (CryptomatteType)(scene->film->cryptomatte_passes | CRYPT_ASSET);
620         }
621         if(get_boolean(crp, "pass_crypto_accurate") && scene->film->cryptomatte_passes != CRYPT_NONE) {
622                 scene->film->cryptomatte_passes = (CryptomatteType)(scene->film->cryptomatte_passes | CRYPT_ACCURATE);
623         }
624
625         return passes;
626 }
627
628 void BlenderSync::free_data_after_sync(BL::Depsgraph& b_depsgraph)
629 {
630         /* When viewport display is not needed during render we can force some
631          * caches to be releases from blender side in order to reduce peak memory
632          * footprint during synchronization process.
633          */
634         const bool is_interface_locked = b_engine.render() &&
635                                          b_engine.render().use_lock_interface();
636         const bool can_free_caches = BlenderSession::headless || is_interface_locked;
637         if(!can_free_caches) {
638                 return;
639         }
640         /* TODO(sergey): We can actually remove the whole dependency graph,
641          * but that will need some API support first.
642          */
643         BL::Depsgraph::objects_iterator b_ob;
644         for(b_depsgraph.objects.begin(b_ob);
645             b_ob != b_depsgraph.objects.end();
646             ++b_ob)
647         {
648                 b_ob->cache_release();
649         }
650 }
651
652 /* Scene Parameters */
653
654 SceneParams BlenderSync::get_scene_params(BL::Scene& b_scene,
655                                           bool background)
656 {
657         BL::RenderSettings r = b_scene.render();
658         SceneParams params;
659         PointerRNA cscene = RNA_pointer_get(&b_scene.ptr, "cycles");
660         const bool shadingsystem = RNA_boolean_get(&cscene, "shading_system");
661
662         if(shadingsystem == 0)
663                 params.shadingsystem = SHADINGSYSTEM_SVM;
664         else if(shadingsystem == 1)
665                 params.shadingsystem = SHADINGSYSTEM_OSL;
666
667         if(background || DebugFlags().viewport_static_bvh)
668                 params.bvh_type = SceneParams::BVH_STATIC;
669         else
670                 params.bvh_type = SceneParams::BVH_DYNAMIC;
671
672         params.use_bvh_spatial_split = RNA_boolean_get(&cscene, "debug_use_spatial_splits");
673         params.use_bvh_unaligned_nodes = RNA_boolean_get(&cscene, "debug_use_hair_bvh");
674         params.num_bvh_time_steps = RNA_int_get(&cscene, "debug_bvh_time_steps");
675
676         if(background && params.shadingsystem != SHADINGSYSTEM_OSL)
677                 params.persistent_data = r.use_persistent_data();
678         else
679                 params.persistent_data = false;
680
681         int texture_limit;
682         if(background) {
683                 texture_limit = RNA_enum_get(&cscene, "texture_limit_render");
684         }
685         else {
686                 texture_limit = RNA_enum_get(&cscene, "texture_limit");
687         }
688         if(texture_limit > 0 && b_scene.render().use_simplify()) {
689                 params.texture_limit = 1 << (texture_limit + 6);
690         }
691         else {
692                 params.texture_limit = 0;
693         }
694
695         /* TODO(sergey): Once OSL supports per-microarchitecture optimization get
696          * rid of this.
697          */
698         if(params.shadingsystem == SHADINGSYSTEM_OSL) {
699                 params.bvh_layout = BVH_LAYOUT_BVH4;
700         }
701         else {
702                 params.bvh_layout = DebugFlags().cpu.bvh_layout;
703         }
704
705 #ifdef WITH_EMBREE
706         params.bvh_layout = RNA_boolean_get(&cscene, "use_bvh_embree") ? BVH_LAYOUT_EMBREE : params.bvh_layout;
707 #endif
708         return params;
709 }
710
711 /* Session Parameters */
712
713 bool BlenderSync::get_session_pause(BL::Scene& b_scene, bool background)
714 {
715         PointerRNA cscene = RNA_pointer_get(&b_scene.ptr, "cycles");
716         return (background)? false: get_boolean(cscene, "preview_pause");
717 }
718
719 SessionParams BlenderSync::get_session_params(BL::RenderEngine& b_engine,
720                                               BL::Preferences& b_preferences,
721                                               BL::Scene& b_scene,
722                                               bool background)
723 {
724         SessionParams params;
725         PointerRNA cscene = RNA_pointer_get(&b_scene.ptr, "cycles");
726
727         /* feature set */
728         params.experimental = (get_enum(cscene, "feature_set") != 0);
729
730         /* Background */
731         params.background = background;
732
733         /* Device */
734         params.threads = blender_device_threads(b_scene);
735         params.device = blender_device_info(b_preferences, b_scene, params.background);
736
737         /* samples */
738         int samples = get_int(cscene, "samples");
739         int aa_samples = get_int(cscene, "aa_samples");
740         int preview_samples = get_int(cscene, "preview_samples");
741         int preview_aa_samples = get_int(cscene, "preview_aa_samples");
742
743         if(get_boolean(cscene, "use_square_samples")) {
744                 aa_samples = aa_samples * aa_samples;
745                 preview_aa_samples = preview_aa_samples * preview_aa_samples;
746
747                 samples = samples * samples;
748                 preview_samples = preview_samples * preview_samples;
749         }
750
751         if(get_enum(cscene, "progressive") == 0) {
752                 if(background) {
753                         params.samples = aa_samples;
754                 }
755                 else {
756                         params.samples = preview_aa_samples;
757                         if(params.samples == 0)
758                                 params.samples = INT_MAX;
759                 }
760         }
761         else {
762                 if(background) {
763                         params.samples = samples;
764                 }
765                 else {
766                         params.samples = preview_samples;
767                         if(params.samples == 0)
768                                 params.samples = INT_MAX;
769                 }
770         }
771
772         /* tiles */
773         const bool is_cpu = (params.device.type == DEVICE_CPU);
774         if(!is_cpu && !background) {
775                 /* currently GPU could be much slower than CPU when using tiles,
776                  * still need to be investigated, but meanwhile make it possible
777                  * to work in viewport smoothly
778                  */
779                 int debug_tile_size = get_int(cscene, "debug_tile_size");
780
781                 params.tile_size = make_int2(debug_tile_size, debug_tile_size);
782         }
783         else {
784                 int tile_x = b_engine.tile_x();
785                 int tile_y = b_engine.tile_y();
786
787                 params.tile_size = make_int2(tile_x, tile_y);
788         }
789
790         if((BlenderSession::headless == false) && background) {
791                 params.tile_order = (TileOrder)get_enum(cscene, "tile_order");
792         }
793         else {
794                 params.tile_order = TILE_BOTTOM_TO_TOP;
795         }
796
797         /* other parameters */
798         params.start_resolution = get_int(cscene, "preview_start_resolution");
799         params.pixel_size = b_engine.get_preview_pixel_size(b_scene);
800
801         /* other parameters */
802         params.cancel_timeout = (double)get_float(cscene, "debug_cancel_timeout");
803         params.reset_timeout = (double)get_float(cscene, "debug_reset_timeout");
804         params.text_timeout = (double)get_float(cscene, "debug_text_timeout");
805
806         /* progressive refine */
807         BL::RenderSettings b_r = b_scene.render();
808         params.progressive_refine = (b_engine.is_preview() ||
809                                      get_boolean(cscene, "use_progressive_refine")) &&
810                                     !b_r.use_save_buffers();
811
812         if(params.progressive_refine) {
813                 BL::Scene::view_layers_iterator b_view_layer;
814                 for(b_scene.view_layers.begin(b_view_layer); b_view_layer != b_scene.view_layers.end(); ++b_view_layer) {
815                         PointerRNA crl = RNA_pointer_get(&b_view_layer->ptr, "cycles");
816                         if(get_boolean(crl, "use_denoising")) {
817                                 params.progressive_refine = false;
818                         }
819                 }
820         }
821
822         if(background) {
823                 if(params.progressive_refine)
824                         params.progressive = true;
825                 else
826                         params.progressive = false;
827
828                 params.start_resolution = INT_MAX;
829                 params.pixel_size = 1;
830         }
831         else
832                 params.progressive = true;
833
834         /* shading system - scene level needs full refresh */
835         const bool shadingsystem = RNA_boolean_get(&cscene, "shading_system");
836
837         if(shadingsystem == 0)
838                 params.shadingsystem = SHADINGSYSTEM_SVM;
839         else if(shadingsystem == 1)
840                 params.shadingsystem = SHADINGSYSTEM_OSL;
841
842         /* color managagement */
843         params.display_buffer_linear = b_engine.support_display_space_shader(b_scene);
844
845         if(b_engine.is_preview()) {
846                 /* For preview rendering we're using same timeout as
847                  * blender's job update.
848                  */
849                 params.progressive_update_timeout = 0.1;
850         }
851
852         params.use_profiling = params.device.has_profiling && !b_engine.is_preview() &&
853                                background && BlenderSession::print_render_stats;
854
855         return params;
856 }
857
858 CCL_NAMESPACE_END