Cycles: Direct multi light sampling in the Branched Path Integrator is optional now.
[blender-staging.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 "background.h"
18 #include "camera.h"
19 #include "film.h"
20 #include "graph.h"
21 #include "integrator.h"
22 #include "light.h"
23 #include "mesh.h"
24 #include "nodes.h"
25 #include "object.h"
26 #include "scene.h"
27 #include "shader.h"
28 #include "curves.h"
29
30 #include "device.h"
31
32 #include "blender_sync.h"
33 #include "blender_util.h"
34
35 #include "util_debug.h"
36 #include "util_foreach.h"
37 #include "util_opengl.h"
38
39 CCL_NAMESPACE_BEGIN
40
41 /* Constructor */
42
43 BlenderSync::BlenderSync(BL::RenderEngine b_engine_, BL::BlendData b_data_, BL::Scene b_scene_, Scene *scene_, bool preview_, Progress &progress_, bool is_cpu_)
44 : b_engine(b_engine_),
45   b_data(b_data_), b_scene(b_scene_),
46   shader_map(&scene_->shaders),
47   object_map(&scene_->objects),
48   mesh_map(&scene_->meshes),
49   light_map(&scene_->lights),
50   particle_system_map(&scene_->particle_systems),
51   world_map(NULL),
52   world_recalc(false),
53   experimental(false),
54   progress(progress_)
55 {
56         scene = scene_;
57         preview = preview_;
58         is_cpu = is_cpu_;
59 }
60
61 BlenderSync::~BlenderSync()
62 {
63 }
64
65 /* Sync */
66
67 bool BlenderSync::sync_recalc()
68 {
69         /* sync recalc flags from blender to cycles. actual update is done separate,
70          * so we can do it later on if doing it immediate is not suitable */
71
72         BL::BlendData::materials_iterator b_mat;
73
74         for(b_data.materials.begin(b_mat); b_mat != b_data.materials.end(); ++b_mat)
75                 if(b_mat->is_updated() || (b_mat->node_tree() && b_mat->node_tree().is_updated()))
76                         shader_map.set_recalc(*b_mat);
77
78         BL::BlendData::lamps_iterator b_lamp;
79
80         for(b_data.lamps.begin(b_lamp); b_lamp != b_data.lamps.end(); ++b_lamp)
81                 if(b_lamp->is_updated() || (b_lamp->node_tree() && b_lamp->node_tree().is_updated()))
82                         shader_map.set_recalc(*b_lamp);
83
84         BL::BlendData::objects_iterator b_ob;
85
86         for(b_data.objects.begin(b_ob); b_ob != b_data.objects.end(); ++b_ob) {
87                 if(b_ob->is_updated()) {
88                         object_map.set_recalc(*b_ob);
89                         light_map.set_recalc(*b_ob);
90                 }
91
92                 if(object_is_mesh(*b_ob)) {
93                         if(b_ob->is_updated_data() || b_ob->data().is_updated()) {
94                                 BL::ID key = BKE_object_is_modified(*b_ob)? *b_ob: b_ob->data();
95                                 mesh_map.set_recalc(key);
96                         }
97                 }
98                 else if(object_is_light(*b_ob)) {
99                         if(b_ob->is_updated_data() || b_ob->data().is_updated())
100                                 light_map.set_recalc(*b_ob);
101                 }
102                 
103                 if(b_ob->is_updated_data()) {
104                         BL::Object::particle_systems_iterator b_psys;
105                         for (b_ob->particle_systems.begin(b_psys); b_psys != b_ob->particle_systems.end(); ++b_psys)
106                                 particle_system_map.set_recalc(*b_ob);
107                 }
108         }
109
110         BL::BlendData::meshes_iterator b_mesh;
111
112         for(b_data.meshes.begin(b_mesh); b_mesh != b_data.meshes.end(); ++b_mesh)
113                 if(b_mesh->is_updated())
114                         mesh_map.set_recalc(*b_mesh);
115
116         BL::BlendData::worlds_iterator b_world;
117
118         for(b_data.worlds.begin(b_world); b_world != b_data.worlds.end(); ++b_world) {
119                 if(world_map == b_world->ptr.data &&
120                    (b_world->is_updated() || (b_world->node_tree() && b_world->node_tree().is_updated())))
121                 {
122                         world_recalc = true;
123                 }
124         }
125
126         bool recalc =
127                 shader_map.has_recalc() ||
128                 object_map.has_recalc() ||
129                 light_map.has_recalc() ||
130                 mesh_map.has_recalc() ||
131                 particle_system_map.has_recalc() ||
132                 BlendDataObjects_is_updated_get(&b_data.ptr) ||
133                 world_recalc;
134
135         return recalc;
136 }
137
138 void BlenderSync::sync_data(BL::SpaceView3D b_v3d, BL::Object b_override, void **python_thread_state, const char *layer)
139 {
140         sync_render_layers(b_v3d, layer);
141         sync_integrator();
142         sync_film();
143         sync_shaders();
144         sync_curve_settings();
145         sync_objects(b_v3d);
146         sync_motion(b_v3d, b_override, python_thread_state);
147 }
148
149 /* Integrator */
150
151 void BlenderSync::sync_integrator()
152 {
153 #ifdef __CAMERA_MOTION__
154         BL::RenderSettings r = b_scene.render();
155 #endif
156         PointerRNA cscene = RNA_pointer_get(&b_scene.ptr, "cycles");
157
158         experimental = (RNA_enum_get(&cscene, "feature_set") != 0);
159
160         Integrator *integrator = scene->integrator;
161         Integrator previntegrator = *integrator;
162
163         integrator->min_bounce = get_int(cscene, "min_bounces");
164         integrator->max_bounce = get_int(cscene, "max_bounces");
165
166         integrator->max_diffuse_bounce = get_int(cscene, "diffuse_bounces");
167         integrator->max_glossy_bounce = get_int(cscene, "glossy_bounces");
168         integrator->max_transmission_bounce = get_int(cscene, "transmission_bounces");
169         integrator->max_volume_bounce = get_int(cscene, "volume_bounces");
170
171         integrator->transparent_max_bounce = get_int(cscene, "transparent_max_bounces");
172         integrator->transparent_min_bounce = get_int(cscene, "transparent_min_bounces");
173         integrator->transparent_shadows = get_boolean(cscene, "use_transparent_shadows");
174
175         integrator->volume_homogeneous_sampling = RNA_enum_get(&cscene, "volume_homogeneous_sampling");
176         integrator->volume_max_steps = get_int(cscene, "volume_max_steps");
177         integrator->volume_step_size = get_float(cscene, "volume_step_size");
178
179         integrator->no_caustics = get_boolean(cscene, "no_caustics");
180         integrator->filter_glossy = get_float(cscene, "blur_glossy");
181
182         integrator->seed = get_int(cscene, "seed");
183
184         integrator->layer_flag = render_layer.layer;
185
186         integrator->sample_clamp_direct = get_float(cscene, "sample_clamp_direct");
187         integrator->sample_clamp_indirect = get_float(cscene, "sample_clamp_indirect");
188 #ifdef __CAMERA_MOTION__
189         if(!preview) {
190                 if(integrator->motion_blur != r.use_motion_blur()) {
191                         scene->object_manager->tag_update(scene);
192                         scene->camera->tag_update();
193                 }
194
195                 integrator->motion_blur = r.use_motion_blur();
196         }
197 #endif
198
199         integrator->method = (Integrator::Method)get_enum(cscene, "progressive");
200
201         integrator->sample_all_lights_direct = get_boolean(cscene, "sample_all_lights_direct");
202         integrator->sample_all_lights_indirect = get_boolean(cscene, "sample_all_lights_indirect");
203
204         int diffuse_samples = get_int(cscene, "diffuse_samples");
205         int glossy_samples = get_int(cscene, "glossy_samples");
206         int transmission_samples = get_int(cscene, "transmission_samples");
207         int ao_samples = get_int(cscene, "ao_samples");
208         int mesh_light_samples = get_int(cscene, "mesh_light_samples");
209         int subsurface_samples = get_int(cscene, "subsurface_samples");
210         int volume_samples = get_int(cscene, "volume_samples");
211
212         if(get_boolean(cscene, "use_square_samples")) {
213                 integrator->diffuse_samples = diffuse_samples * diffuse_samples;
214                 integrator->glossy_samples = glossy_samples * glossy_samples;
215                 integrator->transmission_samples = transmission_samples * transmission_samples;
216                 integrator->ao_samples = ao_samples * ao_samples;
217                 integrator->mesh_light_samples = mesh_light_samples * mesh_light_samples;
218                 integrator->subsurface_samples = subsurface_samples * subsurface_samples;
219                 integrator->volume_samples = volume_samples * volume_samples;
220         } 
221         else {
222                 integrator->diffuse_samples = diffuse_samples;
223                 integrator->glossy_samples = glossy_samples;
224                 integrator->transmission_samples = transmission_samples;
225                 integrator->ao_samples = ao_samples;
226                 integrator->mesh_light_samples = mesh_light_samples;
227                 integrator->subsurface_samples = subsurface_samples;
228                 integrator->volume_samples = volume_samples;
229         }
230         
231
232         if(experimental)
233                 integrator->sampling_pattern = (SamplingPattern)RNA_enum_get(&cscene, "sampling_pattern");
234
235         if(integrator->modified(previntegrator))
236                 integrator->tag_update(scene);
237 }
238
239 /* Film */
240
241 void BlenderSync::sync_film()
242 {
243         PointerRNA cscene = RNA_pointer_get(&b_scene.ptr, "cycles");
244
245         Film *film = scene->film;
246         Film prevfilm = *film;
247         
248         /* Clamping */
249         Integrator *integrator = scene->integrator;
250         film->use_sample_clamp = (integrator->sample_clamp_direct != 0.0f || integrator->sample_clamp_indirect != 0.0f);
251
252         film->exposure = get_float(cscene, "film_exposure");
253         film->filter_type = (FilterType)RNA_enum_get(&cscene, "filter_type");
254         film->filter_width = (film->filter_type == FILTER_BOX)? 1.0f: get_float(cscene, "filter_width");
255
256         if(b_scene.world()) {
257                 BL::WorldMistSettings b_mist = b_scene.world().mist_settings();
258
259                 film->mist_start = b_mist.start();
260                 film->mist_depth = b_mist.depth();
261
262                 switch(b_mist.falloff()) {
263                         case BL::WorldMistSettings::falloff_QUADRATIC:
264                                 film->mist_falloff = 2.0f;
265                                 break;
266                         case BL::WorldMistSettings::falloff_LINEAR:
267                                 film->mist_falloff = 1.0f;
268                                 break;
269                         case BL::WorldMistSettings::falloff_INVERSE_QUADRATIC:
270                                 film->mist_falloff = 0.5f;
271                                 break;
272                 }
273         }
274
275         if(film->modified(prevfilm))
276                 film->tag_update(scene);
277 }
278
279 /* Render Layer */
280
281 void BlenderSync::sync_render_layers(BL::SpaceView3D b_v3d, const char *layer)
282 {
283         PointerRNA cscene = RNA_pointer_get(&b_scene.ptr, "cycles");
284         string layername;
285
286         /* 3d view */
287         if(b_v3d) {
288                 if(RNA_boolean_get(&cscene, "preview_active_layer")) {
289                         BL::RenderLayers layers(b_scene.render().ptr);
290                         layername = layers.active().name();
291                         layer = layername.c_str();
292                 }
293                 else {
294                         render_layer.use_localview = (b_v3d.local_view() ? true : false);
295                         render_layer.scene_layer = get_layer(b_v3d.layers(), b_v3d.layers_local_view(), render_layer.use_localview);
296                         render_layer.layer = render_layer.scene_layer;
297                         render_layer.exclude_layer = 0;
298                         render_layer.holdout_layer = 0;
299                         render_layer.material_override = PointerRNA_NULL;
300                         render_layer.use_background = true;
301                         render_layer.use_hair = true;
302                         render_layer.use_surfaces = true;
303                         render_layer.use_viewport_visibility = true;
304                         render_layer.samples = 0;
305                         render_layer.bound_samples = false;
306                         return;
307                 }
308         }
309
310         /* render layer */
311         BL::RenderSettings r = b_scene.render();
312         BL::RenderSettings::layers_iterator b_rlay;
313         int use_layer_samples = RNA_enum_get(&cscene, "use_layer_samples");
314         bool first_layer = true;
315
316         for(r.layers.begin(b_rlay); b_rlay != r.layers.end(); ++b_rlay) {
317                 if((!layer && first_layer) || (layer && b_rlay->name() == layer)) {
318                         render_layer.name = b_rlay->name();
319
320                         render_layer.holdout_layer = get_layer(b_rlay->layers_zmask());
321                         render_layer.exclude_layer = get_layer(b_rlay->layers_exclude());
322
323                         render_layer.scene_layer = get_layer(b_scene.layers()) & ~render_layer.exclude_layer;
324                         render_layer.scene_layer |= render_layer.exclude_layer & render_layer.holdout_layer;
325
326                         render_layer.layer = get_layer(b_rlay->layers());
327                         render_layer.layer |= render_layer.holdout_layer;
328
329                         render_layer.material_override = b_rlay->material_override();
330                         render_layer.use_background = b_rlay->use_sky();
331                         render_layer.use_surfaces = b_rlay->use_solid();
332                         render_layer.use_hair = b_rlay->use_strand();
333                         render_layer.use_viewport_visibility = false;
334                         render_layer.use_localview = false;
335
336                         render_layer.bound_samples = (use_layer_samples == 1);
337                         if(use_layer_samples != 2) {
338                                 int samples = b_rlay->samples();
339                                 if(get_boolean(cscene, "use_square_samples"))
340                                         render_layer.samples = samples * samples;
341                                 else
342                                         render_layer.samples = samples;
343                         }
344                 }
345
346                 first_layer = false;
347         }
348 }
349
350 /* Scene Parameters */
351
352 SceneParams BlenderSync::get_scene_params(BL::Scene b_scene, bool background)
353 {
354         BL::RenderSettings r = b_scene.render();
355         SceneParams params;
356         PointerRNA cscene = RNA_pointer_get(&b_scene.ptr, "cycles");
357         const bool shadingsystem = RNA_boolean_get(&cscene, "shading_system");
358
359         if(shadingsystem == 0)
360                 params.shadingsystem = SceneParams::SVM;
361         else if(shadingsystem == 1)
362                 params.shadingsystem = SceneParams::OSL;
363         
364         if(background)
365                 params.bvh_type = SceneParams::BVH_STATIC;
366         else
367                 params.bvh_type = (SceneParams::BVHType)RNA_enum_get(&cscene, "debug_bvh_type");
368
369         params.use_bvh_spatial_split = RNA_boolean_get(&cscene, "debug_use_spatial_splits");
370         params.use_bvh_cache = (background)? RNA_boolean_get(&cscene, "use_cache"): false;
371
372         if(background && params.shadingsystem != SceneParams::OSL)
373                 params.persistent_data = r.use_persistent_data();
374         else
375                 params.persistent_data = false;
376
377         return params;
378 }
379
380 /* Session Parameters */
381
382 bool BlenderSync::get_session_pause(BL::Scene b_scene, bool background)
383 {
384         PointerRNA cscene = RNA_pointer_get(&b_scene.ptr, "cycles");
385         return (background)? false: get_boolean(cscene, "preview_pause");
386 }
387
388 SessionParams BlenderSync::get_session_params(BL::RenderEngine b_engine, BL::UserPreferences b_userpref, BL::Scene b_scene, bool background)
389 {
390         SessionParams params;
391         PointerRNA cscene = RNA_pointer_get(&b_scene.ptr, "cycles");
392
393         /* feature set */
394         params.experimental = (RNA_enum_get(&cscene, "feature_set") != 0);
395
396         /* device type */
397         vector<DeviceInfo>& devices = Device::available_devices();
398         
399         /* device default CPU */
400         params.device = devices[0];
401
402         if(RNA_enum_get(&cscene, "device") == 2) {
403                 /* find network device */
404                 foreach(DeviceInfo& info, devices)
405                         if(info.type == DEVICE_NETWORK)
406                                 params.device = info;
407         }
408         else if(RNA_enum_get(&cscene, "device") == 1) {
409                 /* find GPU device with given id */
410                 PointerRNA systemptr = b_userpref.system().ptr;
411                 PropertyRNA *deviceprop = RNA_struct_find_property(&systemptr, "compute_device");
412                 int device_id = b_userpref.system().compute_device();
413
414                 const char *id;
415
416                 if(RNA_property_enum_identifier(NULL, &systemptr, deviceprop, device_id, &id)) {
417                         foreach(DeviceInfo& info, devices)
418                                 if(info.id == id)
419                                         params.device = info;
420                 }
421         }
422
423         /* Background */
424         params.background = background;
425
426         /* samples */
427         int samples = get_int(cscene, "samples");
428         int aa_samples = get_int(cscene, "aa_samples");
429         int preview_samples = get_int(cscene, "preview_samples");
430         int preview_aa_samples = get_int(cscene, "preview_aa_samples");
431         
432         if(get_boolean(cscene, "use_square_samples")) {
433                 aa_samples = aa_samples * aa_samples;
434                 preview_aa_samples = preview_aa_samples * preview_aa_samples;
435
436                 samples = samples * samples;
437                 preview_samples = preview_samples * preview_samples;
438         }
439
440         if(get_enum(cscene, "progressive") == 0) {
441                 if(background) {
442                         params.samples = aa_samples;
443                 }
444                 else {
445                         params.samples = preview_aa_samples;
446                         if(params.samples == 0)
447                                 params.samples = USHRT_MAX;
448                 }
449         }
450         else {
451                 if(background) {
452                         params.samples = samples;
453                 }
454                 else {
455                         params.samples = preview_samples;
456                         if(params.samples == 0)
457                                 params.samples = USHRT_MAX;
458                 }
459         }
460
461         /* tiles */
462         if(params.device.type != DEVICE_CPU && !background) {
463                 /* currently GPU could be much slower than CPU when using tiles,
464                  * still need to be investigated, but meanwhile make it possible
465                  * to work in viewport smoothly
466                  */
467                 int debug_tile_size = get_int(cscene, "debug_tile_size");
468
469                 params.tile_size = make_int2(debug_tile_size, debug_tile_size);
470         }
471         else {
472                 int tile_x = b_engine.tile_x();
473                 int tile_y = b_engine.tile_y();
474
475                 params.tile_size = make_int2(tile_x, tile_y);
476         }
477         
478         params.tile_order = (TileOrder)RNA_enum_get(&cscene, "tile_order");
479
480         params.start_resolution = get_int(cscene, "preview_start_resolution");
481
482         /* other parameters */
483         if(b_scene.render().threads_mode() == BL::RenderSettings::threads_mode_FIXED)
484                 params.threads = b_scene.render().threads();
485         else
486                 params.threads = 0;
487
488         params.cancel_timeout = get_float(cscene, "debug_cancel_timeout");
489         params.reset_timeout = get_float(cscene, "debug_reset_timeout");
490         params.text_timeout = get_float(cscene, "debug_text_timeout");
491
492         params.progressive_refine = get_boolean(cscene, "use_progressive_refine");
493
494         if(background) {
495                 if(params.progressive_refine)
496                         params.progressive = true;
497                 else
498                         params.progressive = false;
499
500                 params.start_resolution = INT_MAX;
501         }
502         else
503                 params.progressive = true;
504
505         /* shading system - scene level needs full refresh */
506         const bool shadingsystem = RNA_boolean_get(&cscene, "shading_system");
507
508         if(shadingsystem == 0)
509                 params.shadingsystem = SessionParams::SVM;
510         else if(shadingsystem == 1)
511                 params.shadingsystem = SessionParams::OSL;
512         
513         /* color managagement */
514         params.display_buffer_linear = GLEW_ARB_half_float_pixel && b_engine.support_display_space_shader(b_scene);
515
516         return params;
517 }
518
519 CCL_NAMESPACE_END
520