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