Cleanup: use better names for depsgraph driver relations.
[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_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_NORMAL);
494         MAP_PASS("Normal Variance", DENOISING_PASS_NORMAL_VAR);
495         MAP_PASS("Albedo", DENOISING_PASS_ALBEDO);
496         MAP_PASS("Albedo Variance", DENOISING_PASS_ALBEDO_VAR);
497         MAP_PASS("Depth", DENOISING_PASS_DEPTH);
498         MAP_PASS("Depth Variance", DENOISING_PASS_DEPTH_VAR);
499         MAP_PASS("Shadow A", DENOISING_PASS_SHADOW_A);
500         MAP_PASS("Shadow B", DENOISING_PASS_SHADOW_B);
501         MAP_PASS("Image Variance", DENOISING_PASS_COLOR_VAR);
502         MAP_PASS("Clean", DENOISING_PASS_CLEAN);
503 #undef MAP_PASS
504
505         return -1;
506 }
507
508 vector<Pass> BlenderSync::sync_render_passes(BL::RenderLayer& b_rlay,
509                                              BL::ViewLayer& b_view_layer,
510                                              const SessionParams &session_params)
511 {
512         vector<Pass> passes;
513         Pass::add(PASS_COMBINED, passes);
514
515         if(!session_params.device.advanced_shading) {
516                 return passes;
517         }
518
519         /* loop over passes */
520         BL::RenderLayer::passes_iterator b_pass_iter;
521
522         for(b_rlay.passes.begin(b_pass_iter); b_pass_iter != b_rlay.passes.end(); ++b_pass_iter) {
523                 BL::RenderPass b_pass(*b_pass_iter);
524                 PassType pass_type = get_pass_type(b_pass);
525
526                 if(pass_type == PASS_MOTION && scene->integrator->motion_blur)
527                         continue;
528                 if(pass_type != PASS_NONE)
529                         Pass::add(pass_type, passes);
530         }
531
532         PointerRNA crp = RNA_pointer_get(&b_view_layer.ptr, "cycles");
533         bool use_denoising = get_boolean(crp, "use_denoising");
534         bool store_denoising_passes = get_boolean(crp, "denoising_store_passes");
535         scene->film->denoising_flags = 0;
536         if(use_denoising || store_denoising_passes) {
537 #define MAP_OPTION(name, flag) if(!get_boolean(crp, name)) scene->film->denoising_flags |= flag;
538                 MAP_OPTION("denoising_diffuse_direct",        DENOISING_CLEAN_DIFFUSE_DIR);
539                 MAP_OPTION("denoising_diffuse_indirect",      DENOISING_CLEAN_DIFFUSE_IND);
540                 MAP_OPTION("denoising_glossy_direct",         DENOISING_CLEAN_GLOSSY_DIR);
541                 MAP_OPTION("denoising_glossy_indirect",       DENOISING_CLEAN_GLOSSY_IND);
542                 MAP_OPTION("denoising_transmission_direct",   DENOISING_CLEAN_TRANSMISSION_DIR);
543                 MAP_OPTION("denoising_transmission_indirect", DENOISING_CLEAN_TRANSMISSION_IND);
544                 MAP_OPTION("denoising_subsurface_direct",     DENOISING_CLEAN_SUBSURFACE_DIR);
545                 MAP_OPTION("denoising_subsurface_indirect",   DENOISING_CLEAN_SUBSURFACE_IND);
546 #undef MAP_OPTION
547                 b_engine.add_pass("Noisy Image", 4, "RGBA", b_view_layer.name().c_str());
548         }
549
550         if(store_denoising_passes) {
551                 b_engine.add_pass("Denoising Normal",          3, "XYZ", b_view_layer.name().c_str());
552                 b_engine.add_pass("Denoising Normal Variance", 3, "XYZ", b_view_layer.name().c_str());
553                 b_engine.add_pass("Denoising Albedo",          3, "RGB", b_view_layer.name().c_str());
554                 b_engine.add_pass("Denoising Albedo Variance", 3, "RGB", b_view_layer.name().c_str());
555                 b_engine.add_pass("Denoising Depth",           1, "Z",   b_view_layer.name().c_str());
556                 b_engine.add_pass("Denoising Depth Variance",  1, "Z",   b_view_layer.name().c_str());
557                 b_engine.add_pass("Denoising Shadow A",        3, "XYV", b_view_layer.name().c_str());
558                 b_engine.add_pass("Denoising Shadow B",        3, "XYV", b_view_layer.name().c_str());
559                 b_engine.add_pass("Denoising Image Variance",  3, "RGB", b_view_layer.name().c_str());
560
561                 if(scene->film->denoising_flags & DENOISING_CLEAN_ALL_PASSES) {
562                         b_engine.add_pass("Denoising Clean",   3, "RGB", b_view_layer.name().c_str());
563                 }
564         }
565 #ifdef __KERNEL_DEBUG__
566         if(get_boolean(crp, "pass_debug_bvh_traversed_nodes")) {
567                 b_engine.add_pass("Debug BVH Traversed Nodes", 1, "X", b_view_layer.name().c_str());
568                 Pass::add(PASS_BVH_TRAVERSED_NODES, passes);
569         }
570         if(get_boolean(crp, "pass_debug_bvh_traversed_instances")) {
571                 b_engine.add_pass("Debug BVH Traversed Instances", 1, "X", b_view_layer.name().c_str());
572                 Pass::add(PASS_BVH_TRAVERSED_INSTANCES, passes);
573         }
574         if(get_boolean(crp, "pass_debug_bvh_intersections")) {
575                 b_engine.add_pass("Debug BVH Intersections", 1, "X", b_view_layer.name().c_str());
576                 Pass::add(PASS_BVH_INTERSECTIONS, passes);
577         }
578         if(get_boolean(crp, "pass_debug_ray_bounces")) {
579                 b_engine.add_pass("Debug Ray Bounces", 1, "X", b_view_layer.name().c_str());
580                 Pass::add(PASS_RAY_BOUNCES, passes);
581         }
582 #endif
583         if(get_boolean(crp, "pass_debug_render_time")) {
584                 b_engine.add_pass("Debug Render Time", 1, "X", b_view_layer.name().c_str());
585                 Pass::add(PASS_RENDER_TIME, passes);
586         }
587         if(get_boolean(crp, "use_pass_volume_direct")) {
588                 b_engine.add_pass("VolumeDir", 3, "RGB", b_view_layer.name().c_str());
589                 Pass::add(PASS_VOLUME_DIRECT, passes);
590         }
591         if(get_boolean(crp, "use_pass_volume_indirect")) {
592                 b_engine.add_pass("VolumeInd", 3, "RGB", b_view_layer.name().c_str());
593                 Pass::add(PASS_VOLUME_INDIRECT, passes);
594         }
595
596         /* Cryptomatte stores two ID/weight pairs per RGBA layer.
597          * User facing paramter is the number of pairs. */
598         int crypto_depth = min(16, get_int(crp, "pass_crypto_depth")) / 2;
599         scene->film->cryptomatte_depth = crypto_depth;
600         scene->film->cryptomatte_passes = CRYPT_NONE;
601         if(get_boolean(crp, "use_pass_crypto_object")) {
602                 for(int i = 0; i < crypto_depth; ++i) {
603                         string passname = cryptomatte_prefix + string_printf("Object%02d", i);
604                         b_engine.add_pass(passname.c_str(), 4, "RGBA", b_view_layer.name().c_str());
605                         Pass::add(PASS_CRYPTOMATTE, passes, passname.c_str());
606                 }
607                 scene->film->cryptomatte_passes = (CryptomatteType)(scene->film->cryptomatte_passes | CRYPT_OBJECT);
608         }
609         if(get_boolean(crp, "use_pass_crypto_material")) {
610                 for(int i = 0; i < crypto_depth; ++i) {
611                         string passname = cryptomatte_prefix + string_printf("Material%02d", i);
612                         b_engine.add_pass(passname.c_str(), 4, "RGBA", b_view_layer.name().c_str());
613                         Pass::add(PASS_CRYPTOMATTE, passes, passname.c_str());
614                 }
615                 scene->film->cryptomatte_passes = (CryptomatteType)(scene->film->cryptomatte_passes | CRYPT_MATERIAL);
616         }
617         if(get_boolean(crp, "use_pass_crypto_asset")) {
618                 for(int i = 0; i < crypto_depth; ++i) {
619                         string passname = cryptomatte_prefix + string_printf("Asset%02d", i);
620                         b_engine.add_pass(passname.c_str(), 4, "RGBA", b_view_layer.name().c_str());
621                         Pass::add(PASS_CRYPTOMATTE, passes, passname.c_str());
622                 }
623                 scene->film->cryptomatte_passes = (CryptomatteType)(scene->film->cryptomatte_passes | CRYPT_ASSET);
624         }
625         if(get_boolean(crp, "pass_crypto_accurate") && scene->film->cryptomatte_passes != CRYPT_NONE) {
626                 scene->film->cryptomatte_passes = (CryptomatteType)(scene->film->cryptomatte_passes | CRYPT_ACCURATE);
627         }
628
629         return passes;
630 }
631
632 void BlenderSync::free_data_after_sync(BL::Depsgraph& b_depsgraph)
633 {
634         /* When viewport display is not needed during render we can force some
635          * caches to be releases from blender side in order to reduce peak memory
636          * footprint during synchronization process.
637          */
638         const bool is_interface_locked = b_engine.render() &&
639                                          b_engine.render().use_lock_interface();
640         const bool can_free_caches = BlenderSession::headless || is_interface_locked;
641         if(!can_free_caches) {
642                 return;
643         }
644         /* TODO(sergey): We can actually remove the whole dependency graph,
645          * but that will need some API support first.
646          */
647         BL::Depsgraph::objects_iterator b_ob;
648         for(b_depsgraph.objects.begin(b_ob);
649             b_ob != b_depsgraph.objects.end();
650             ++b_ob)
651         {
652                 b_ob->cache_release();
653         }
654 }
655
656 /* Scene Parameters */
657
658 SceneParams BlenderSync::get_scene_params(BL::Scene& b_scene,
659                                           bool background)
660 {
661         BL::RenderSettings r = b_scene.render();
662         SceneParams params;
663         PointerRNA cscene = RNA_pointer_get(&b_scene.ptr, "cycles");
664         const bool shadingsystem = RNA_boolean_get(&cscene, "shading_system");
665
666         if(shadingsystem == 0)
667                 params.shadingsystem = SHADINGSYSTEM_SVM;
668         else if(shadingsystem == 1)
669                 params.shadingsystem = SHADINGSYSTEM_OSL;
670
671         if(background || DebugFlags().viewport_static_bvh)
672                 params.bvh_type = SceneParams::BVH_STATIC;
673         else
674                 params.bvh_type = SceneParams::BVH_DYNAMIC;
675
676         params.use_bvh_spatial_split = RNA_boolean_get(&cscene, "debug_use_spatial_splits");
677         params.use_bvh_unaligned_nodes = RNA_boolean_get(&cscene, "debug_use_hair_bvh");
678         params.num_bvh_time_steps = RNA_int_get(&cscene, "debug_bvh_time_steps");
679
680         if(background && params.shadingsystem != SHADINGSYSTEM_OSL)
681                 params.persistent_data = r.use_persistent_data();
682         else
683                 params.persistent_data = false;
684
685         int texture_limit;
686         if(background) {
687                 texture_limit = RNA_enum_get(&cscene, "texture_limit_render");
688         }
689         else {
690                 texture_limit = RNA_enum_get(&cscene, "texture_limit");
691         }
692         if(texture_limit > 0 && b_scene.render().use_simplify()) {
693                 params.texture_limit = 1 << (texture_limit + 6);
694         }
695         else {
696                 params.texture_limit = 0;
697         }
698
699         /* TODO(sergey): Once OSL supports per-microarchitecture optimization get
700          * rid of this.
701          */
702         if(params.shadingsystem == SHADINGSYSTEM_OSL) {
703                 params.bvh_layout = BVH_LAYOUT_BVH4;
704         }
705         else {
706                 params.bvh_layout = DebugFlags().cpu.bvh_layout;
707         }
708
709 #ifdef WITH_EMBREE
710         params.bvh_layout = RNA_boolean_get(&cscene, "use_bvh_embree") ? BVH_LAYOUT_EMBREE : params.bvh_layout;
711 #endif
712         return params;
713 }
714
715 /* Session Parameters */
716
717 bool BlenderSync::get_session_pause(BL::Scene& b_scene, bool background)
718 {
719         PointerRNA cscene = RNA_pointer_get(&b_scene.ptr, "cycles");
720         return (background)? false: get_boolean(cscene, "preview_pause");
721 }
722
723 SessionParams BlenderSync::get_session_params(BL::RenderEngine& b_engine,
724                                               BL::Preferences& b_userpref,
725                                               BL::Scene& b_scene,
726                                               bool background)
727 {
728         SessionParams params;
729         PointerRNA cscene = RNA_pointer_get(&b_scene.ptr, "cycles");
730
731         /* feature set */
732         params.experimental = (get_enum(cscene, "feature_set") != 0);
733
734         /* threads */
735         BL::RenderSettings b_r = b_scene.render();
736         if(b_r.threads_mode() == BL::RenderSettings::threads_mode_FIXED)
737                 params.threads = b_r.threads();
738         else
739                 params.threads = 0;
740
741         /* Background */
742         params.background = background;
743
744         /* device type */
745         vector<DeviceInfo>& devices = Device::available_devices();
746
747         /* device default CPU */
748         foreach(DeviceInfo& device, devices) {
749                 if(device.type == DEVICE_CPU) {
750                         params.device = device;
751                         break;
752                 }
753         }
754
755         if(get_enum(cscene, "device") == 2) {
756                 /* find network device */
757                 foreach(DeviceInfo& info, devices)
758                         if(info.type == DEVICE_NETWORK)
759                                 params.device = info;
760         }
761         else if(get_enum(cscene, "device") == 1) {
762                 PointerRNA b_preferences;
763
764                 BL::Preferences::addons_iterator b_addon_iter;
765                 for(b_userpref.addons.begin(b_addon_iter); b_addon_iter != b_userpref.addons.end(); ++b_addon_iter) {
766                         if(b_addon_iter->module() == "cycles") {
767                                 b_preferences = b_addon_iter->preferences().ptr;
768                                 break;
769                         }
770                 }
771
772                 enum ComputeDevice {
773                         COMPUTE_DEVICE_CPU = 0,
774                         COMPUTE_DEVICE_CUDA = 1,
775                         COMPUTE_DEVICE_OPENCL = 2,
776                         COMPUTE_DEVICE_NUM = 3,
777                 };
778
779                 ComputeDevice compute_device = (ComputeDevice)get_enum(b_preferences,
780                                                                        "compute_device_type",
781                                                                        COMPUTE_DEVICE_NUM,
782                                                                        COMPUTE_DEVICE_CPU);
783
784                 if(compute_device != COMPUTE_DEVICE_CPU) {
785                         vector<DeviceInfo> used_devices;
786                         RNA_BEGIN(&b_preferences, device, "devices") {
787                                 ComputeDevice device_type = (ComputeDevice)get_enum(device,
788                                                                                     "type",
789                                                                                     COMPUTE_DEVICE_NUM,
790                                                                                     COMPUTE_DEVICE_CPU);
791
792                                 if(get_boolean(device, "use") &&
793                                    (device_type == compute_device || device_type == COMPUTE_DEVICE_CPU)) {
794                                         string id = get_string(device, "id");
795                                         foreach(DeviceInfo& info, devices) {
796                                                 if(info.id == id) {
797                                                         used_devices.push_back(info);
798                                                         break;
799                                                 }
800                                         }
801                                 }
802                         } RNA_END;
803
804                         if(used_devices.size() == 1) {
805                                 params.device = used_devices[0];
806                         }
807                         else if(used_devices.size() > 1) {
808                                 params.device = Device::get_multi_device(used_devices,
809                                                                          params.threads,
810                                                                          params.background);
811                         }
812                         /* Else keep using the CPU device that was set before. */
813                 }
814         }
815
816         /* samples */
817         int samples = get_int(cscene, "samples");
818         int aa_samples = get_int(cscene, "aa_samples");
819         int preview_samples = get_int(cscene, "preview_samples");
820         int preview_aa_samples = get_int(cscene, "preview_aa_samples");
821
822         if(get_boolean(cscene, "use_square_samples")) {
823                 aa_samples = aa_samples * aa_samples;
824                 preview_aa_samples = preview_aa_samples * preview_aa_samples;
825
826                 samples = samples * samples;
827                 preview_samples = preview_samples * preview_samples;
828         }
829
830         if(get_enum(cscene, "progressive") == 0) {
831                 if(background) {
832                         params.samples = aa_samples;
833                 }
834                 else {
835                         params.samples = preview_aa_samples;
836                         if(params.samples == 0)
837                                 params.samples = INT_MAX;
838                 }
839         }
840         else {
841                 if(background) {
842                         params.samples = samples;
843                 }
844                 else {
845                         params.samples = preview_samples;
846                         if(params.samples == 0)
847                                 params.samples = INT_MAX;
848                 }
849         }
850
851         /* tiles */
852         const bool is_cpu = (params.device.type == DEVICE_CPU);
853         if(!is_cpu && !background) {
854                 /* currently GPU could be much slower than CPU when using tiles,
855                  * still need to be investigated, but meanwhile make it possible
856                  * to work in viewport smoothly
857                  */
858                 int debug_tile_size = get_int(cscene, "debug_tile_size");
859
860                 params.tile_size = make_int2(debug_tile_size, debug_tile_size);
861         }
862         else {
863                 int tile_x = b_engine.tile_x();
864                 int tile_y = b_engine.tile_y();
865
866                 params.tile_size = make_int2(tile_x, tile_y);
867         }
868
869         if((BlenderSession::headless == false) && background) {
870                 params.tile_order = (TileOrder)get_enum(cscene, "tile_order");
871         }
872         else {
873                 params.tile_order = TILE_BOTTOM_TO_TOP;
874         }
875
876         /* other parameters */
877         params.start_resolution = get_int(cscene, "preview_start_resolution");
878         params.pixel_size = b_engine.get_preview_pixel_size(b_scene);
879
880         /* other parameters */
881         params.cancel_timeout = (double)get_float(cscene, "debug_cancel_timeout");
882         params.reset_timeout = (double)get_float(cscene, "debug_reset_timeout");
883         params.text_timeout = (double)get_float(cscene, "debug_text_timeout");
884
885         /* progressive refine */
886         params.progressive_refine = (b_engine.is_preview() ||
887                                      get_boolean(cscene, "use_progressive_refine")) &&
888                                     !b_r.use_save_buffers();
889
890         if(params.progressive_refine) {
891                 BL::Scene::view_layers_iterator b_view_layer;
892                 for(b_scene.view_layers.begin(b_view_layer); b_view_layer != b_scene.view_layers.end(); ++b_view_layer) {
893                         PointerRNA crl = RNA_pointer_get(&b_view_layer->ptr, "cycles");
894                         if(get_boolean(crl, "use_denoising")) {
895                                 params.progressive_refine = false;
896                         }
897                 }
898         }
899
900         if(background) {
901                 if(params.progressive_refine)
902                         params.progressive = true;
903                 else
904                         params.progressive = false;
905
906                 params.start_resolution = INT_MAX;
907                 params.pixel_size = 1;
908         }
909         else
910                 params.progressive = true;
911
912         /* shading system - scene level needs full refresh */
913         const bool shadingsystem = RNA_boolean_get(&cscene, "shading_system");
914
915         if(shadingsystem == 0)
916                 params.shadingsystem = SHADINGSYSTEM_SVM;
917         else if(shadingsystem == 1)
918                 params.shadingsystem = SHADINGSYSTEM_OSL;
919
920         /* color managagement */
921         params.display_buffer_linear = b_engine.support_display_space_shader(b_scene);
922
923         if(b_engine.is_preview()) {
924                 /* For preview rendering we're using same timeout as
925                  * blender's job update.
926                  */
927                 params.progressive_update_timeout = 0.1;
928         }
929
930         params.use_profiling = params.device.has_profiling && !b_engine.is_preview() &&
931                                background && BlenderSession::print_render_stats;
932
933         return params;
934 }
935
936 CCL_NAMESPACE_END