555692d9e21c30c7321bf97a79d8e239208ee698
[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 "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
146         mesh_synced.clear(); /* use for objects and motion sync */
147
148         sync_objects(b_v3d);
149         sync_motion(b_v3d, b_override, python_thread_state);
150
151         mesh_synced.clear();
152 }
153
154 /* Integrator */
155
156 void BlenderSync::sync_integrator()
157 {
158 #ifdef __CAMERA_MOTION__
159         BL::RenderSettings r = b_scene.render();
160 #endif
161         PointerRNA cscene = RNA_pointer_get(&b_scene.ptr, "cycles");
162
163         experimental = (RNA_enum_get(&cscene, "feature_set") != 0);
164
165         Integrator *integrator = scene->integrator;
166         Integrator previntegrator = *integrator;
167
168         integrator->min_bounce = get_int(cscene, "min_bounces");
169         integrator->max_bounce = get_int(cscene, "max_bounces");
170
171         integrator->max_diffuse_bounce = get_int(cscene, "diffuse_bounces");
172         integrator->max_glossy_bounce = get_int(cscene, "glossy_bounces");
173         integrator->max_transmission_bounce = get_int(cscene, "transmission_bounces");
174         integrator->max_volume_bounce = get_int(cscene, "volume_bounces");
175
176         integrator->transparent_max_bounce = get_int(cscene, "transparent_max_bounces");
177         integrator->transparent_min_bounce = get_int(cscene, "transparent_min_bounces");
178         integrator->transparent_shadows = get_boolean(cscene, "use_transparent_shadows");
179
180         integrator->volume_homogeneous_sampling = RNA_enum_get(&cscene, "volume_homogeneous_sampling");
181         integrator->volume_max_steps = get_int(cscene, "volume_max_steps");
182         integrator->volume_step_size = get_float(cscene, "volume_step_size");
183
184         integrator->no_caustics = get_boolean(cscene, "no_caustics");
185         integrator->filter_glossy = get_float(cscene, "blur_glossy");
186
187         integrator->seed = get_int(cscene, "seed");
188
189         integrator->layer_flag = render_layer.layer;
190
191         integrator->sample_clamp_direct = get_float(cscene, "sample_clamp_direct");
192         integrator->sample_clamp_indirect = get_float(cscene, "sample_clamp_indirect");
193 #ifdef __CAMERA_MOTION__
194         if(!preview) {
195                 if(integrator->motion_blur != r.use_motion_blur()) {
196                         scene->object_manager->tag_update(scene);
197                         scene->camera->tag_update();
198                 }
199
200                 integrator->motion_blur = r.use_motion_blur();
201         }
202 #endif
203
204         integrator->method = (Integrator::Method)get_enum(cscene, "progressive");
205
206         integrator->sample_all_lights_direct = get_boolean(cscene, "sample_all_lights_direct");
207         integrator->sample_all_lights_indirect = get_boolean(cscene, "sample_all_lights_indirect");
208
209         int diffuse_samples = get_int(cscene, "diffuse_samples");
210         int glossy_samples = get_int(cscene, "glossy_samples");
211         int transmission_samples = get_int(cscene, "transmission_samples");
212         int ao_samples = get_int(cscene, "ao_samples");
213         int mesh_light_samples = get_int(cscene, "mesh_light_samples");
214         int subsurface_samples = get_int(cscene, "subsurface_samples");
215         int volume_samples = get_int(cscene, "volume_samples");
216
217         if(get_boolean(cscene, "use_square_samples")) {
218                 integrator->diffuse_samples = diffuse_samples * diffuse_samples;
219                 integrator->glossy_samples = glossy_samples * glossy_samples;
220                 integrator->transmission_samples = transmission_samples * transmission_samples;
221                 integrator->ao_samples = ao_samples * ao_samples;
222                 integrator->mesh_light_samples = mesh_light_samples * mesh_light_samples;
223                 integrator->subsurface_samples = subsurface_samples * subsurface_samples;
224                 integrator->volume_samples = volume_samples * volume_samples;
225         } 
226         else {
227                 integrator->diffuse_samples = diffuse_samples;
228                 integrator->glossy_samples = glossy_samples;
229                 integrator->transmission_samples = transmission_samples;
230                 integrator->ao_samples = ao_samples;
231                 integrator->mesh_light_samples = mesh_light_samples;
232                 integrator->subsurface_samples = subsurface_samples;
233                 integrator->volume_samples = volume_samples;
234         }
235         
236
237         if(experimental)
238                 integrator->sampling_pattern = (SamplingPattern)RNA_enum_get(&cscene, "sampling_pattern");
239
240         if(integrator->modified(previntegrator))
241                 integrator->tag_update(scene);
242 }
243
244 /* Film */
245
246 void BlenderSync::sync_film()
247 {
248         PointerRNA cscene = RNA_pointer_get(&b_scene.ptr, "cycles");
249
250         Film *film = scene->film;
251         Film prevfilm = *film;
252         
253         /* Clamping */
254         Integrator *integrator = scene->integrator;
255         film->use_sample_clamp = (integrator->sample_clamp_direct != 0.0f || integrator->sample_clamp_indirect != 0.0f);
256
257         film->exposure = get_float(cscene, "film_exposure");
258         film->filter_type = (FilterType)RNA_enum_get(&cscene, "filter_type");
259         film->filter_width = (film->filter_type == FILTER_BOX)? 1.0f: get_float(cscene, "filter_width");
260
261         if(b_scene.world()) {
262                 BL::WorldMistSettings b_mist = b_scene.world().mist_settings();
263
264                 film->mist_start = b_mist.start();
265                 film->mist_depth = b_mist.depth();
266
267                 switch(b_mist.falloff()) {
268                         case BL::WorldMistSettings::falloff_QUADRATIC:
269                                 film->mist_falloff = 2.0f;
270                                 break;
271                         case BL::WorldMistSettings::falloff_LINEAR:
272                                 film->mist_falloff = 1.0f;
273                                 break;
274                         case BL::WorldMistSettings::falloff_INVERSE_QUADRATIC:
275                                 film->mist_falloff = 0.5f;
276                                 break;
277                 }
278         }
279
280         if(film->modified(prevfilm))
281                 film->tag_update(scene);
282 }
283
284 /* Render Layer */
285
286 void BlenderSync::sync_render_layers(BL::SpaceView3D b_v3d, const char *layer)
287 {
288         PointerRNA cscene = RNA_pointer_get(&b_scene.ptr, "cycles");
289         string layername;
290
291         /* 3d view */
292         if(b_v3d) {
293                 if(RNA_boolean_get(&cscene, "preview_active_layer")) {
294                         BL::RenderLayers layers(b_scene.render().ptr);
295                         layername = layers.active().name();
296                         layer = layername.c_str();
297                 }
298                 else {
299                         render_layer.use_localview = (b_v3d.local_view() ? true : false);
300                         render_layer.scene_layer = get_layer(b_v3d.layers(), b_v3d.layers_local_view(), render_layer.use_localview);
301                         render_layer.layer = render_layer.scene_layer;
302                         render_layer.exclude_layer = 0;
303                         render_layer.holdout_layer = 0;
304                         render_layer.material_override = PointerRNA_NULL;
305                         render_layer.use_background = true;
306                         render_layer.use_hair = true;
307                         render_layer.use_surfaces = true;
308                         render_layer.use_viewport_visibility = true;
309                         render_layer.samples = 0;
310                         render_layer.bound_samples = false;
311                         return;
312                 }
313         }
314
315         /* render layer */
316         BL::RenderSettings r = b_scene.render();
317         BL::RenderSettings::layers_iterator b_rlay;
318         int use_layer_samples = RNA_enum_get(&cscene, "use_layer_samples");
319         bool first_layer = true;
320
321         for(r.layers.begin(b_rlay); b_rlay != r.layers.end(); ++b_rlay) {
322                 if((!layer && first_layer) || (layer && b_rlay->name() == layer)) {
323                         render_layer.name = b_rlay->name();
324
325                         render_layer.holdout_layer = get_layer(b_rlay->layers_zmask());
326                         render_layer.exclude_layer = get_layer(b_rlay->layers_exclude());
327
328                         render_layer.scene_layer = get_layer(b_scene.layers()) & ~render_layer.exclude_layer;
329                         render_layer.scene_layer |= render_layer.exclude_layer & render_layer.holdout_layer;
330
331                         render_layer.layer = get_layer(b_rlay->layers());
332                         render_layer.layer |= render_layer.holdout_layer;
333
334                         render_layer.material_override = b_rlay->material_override();
335                         render_layer.use_background = b_rlay->use_sky();
336                         render_layer.use_surfaces = b_rlay->use_solid();
337                         render_layer.use_hair = b_rlay->use_strand();
338                         render_layer.use_viewport_visibility = false;
339                         render_layer.use_localview = false;
340
341                         render_layer.bound_samples = (use_layer_samples == 1);
342                         if(use_layer_samples != 2) {
343                                 int samples = b_rlay->samples();
344                                 if(get_boolean(cscene, "use_square_samples"))
345                                         render_layer.samples = samples * samples;
346                                 else
347                                         render_layer.samples = samples;
348                         }
349                 }
350
351                 first_layer = false;
352         }
353 }
354
355 /* Scene Parameters */
356
357 SceneParams BlenderSync::get_scene_params(BL::Scene b_scene, bool background)
358 {
359         BL::RenderSettings r = b_scene.render();
360         SceneParams params;
361         PointerRNA cscene = RNA_pointer_get(&b_scene.ptr, "cycles");
362         const bool shadingsystem = RNA_boolean_get(&cscene, "shading_system");
363
364         if(shadingsystem == 0)
365                 params.shadingsystem = SceneParams::SVM;
366         else if(shadingsystem == 1)
367                 params.shadingsystem = SceneParams::OSL;
368         
369         if(background)
370                 params.bvh_type = SceneParams::BVH_STATIC;
371         else
372                 params.bvh_type = (SceneParams::BVHType)RNA_enum_get(&cscene, "debug_bvh_type");
373
374         params.use_bvh_spatial_split = RNA_boolean_get(&cscene, "debug_use_spatial_splits");
375         params.use_bvh_cache = (background)? RNA_boolean_get(&cscene, "use_cache"): false;
376
377         if(background && params.shadingsystem != SceneParams::OSL)
378                 params.persistent_data = r.use_persistent_data();
379         else
380                 params.persistent_data = false;
381
382         return params;
383 }
384
385 /* Session Parameters */
386
387 bool BlenderSync::get_session_pause(BL::Scene b_scene, bool background)
388 {
389         PointerRNA cscene = RNA_pointer_get(&b_scene.ptr, "cycles");
390         return (background)? false: get_boolean(cscene, "preview_pause");
391 }
392
393 SessionParams BlenderSync::get_session_params(BL::RenderEngine b_engine, BL::UserPreferences b_userpref, BL::Scene b_scene, bool background)
394 {
395         SessionParams params;
396         PointerRNA cscene = RNA_pointer_get(&b_scene.ptr, "cycles");
397
398         /* feature set */
399         params.experimental = (RNA_enum_get(&cscene, "feature_set") != 0);
400
401         /* device type */
402         vector<DeviceInfo>& devices = Device::available_devices();
403         
404         /* device default CPU */
405         params.device = devices[0];
406
407         if(RNA_enum_get(&cscene, "device") == 2) {
408                 /* find network device */
409                 foreach(DeviceInfo& info, devices)
410                         if(info.type == DEVICE_NETWORK)
411                                 params.device = info;
412         }
413         else if(RNA_enum_get(&cscene, "device") == 1) {
414                 /* find GPU device with given id */
415                 PointerRNA systemptr = b_userpref.system().ptr;
416                 PropertyRNA *deviceprop = RNA_struct_find_property(&systemptr, "compute_device");
417                 int device_id = b_userpref.system().compute_device();
418
419                 const char *id;
420
421                 if(RNA_property_enum_identifier(NULL, &systemptr, deviceprop, device_id, &id)) {
422                         foreach(DeviceInfo& info, devices)
423                                 if(info.id == id)
424                                         params.device = info;
425                 }
426         }
427
428         /* Background */
429         params.background = background;
430
431         /* samples */
432         int samples = get_int(cscene, "samples");
433         int aa_samples = get_int(cscene, "aa_samples");
434         int preview_samples = get_int(cscene, "preview_samples");
435         int preview_aa_samples = get_int(cscene, "preview_aa_samples");
436         
437         if(get_boolean(cscene, "use_square_samples")) {
438                 aa_samples = aa_samples * aa_samples;
439                 preview_aa_samples = preview_aa_samples * preview_aa_samples;
440
441                 samples = samples * samples;
442                 preview_samples = preview_samples * preview_samples;
443         }
444
445         if(get_enum(cscene, "progressive") == 0) {
446                 if(background) {
447                         params.samples = aa_samples;
448                 }
449                 else {
450                         params.samples = preview_aa_samples;
451                         if(params.samples == 0)
452                                 params.samples = USHRT_MAX;
453                 }
454         }
455         else {
456                 if(background) {
457                         params.samples = samples;
458                 }
459                 else {
460                         params.samples = preview_samples;
461                         if(params.samples == 0)
462                                 params.samples = USHRT_MAX;
463                 }
464         }
465
466         /* tiles */
467         if(params.device.type != DEVICE_CPU && !background) {
468                 /* currently GPU could be much slower than CPU when using tiles,
469                  * still need to be investigated, but meanwhile make it possible
470                  * to work in viewport smoothly
471                  */
472                 int debug_tile_size = get_int(cscene, "debug_tile_size");
473
474                 params.tile_size = make_int2(debug_tile_size, debug_tile_size);
475         }
476         else {
477                 int tile_x = b_engine.tile_x();
478                 int tile_y = b_engine.tile_y();
479
480                 params.tile_size = make_int2(tile_x, tile_y);
481         }
482         
483         params.tile_order = (TileOrder)RNA_enum_get(&cscene, "tile_order");
484
485         params.start_resolution = get_int(cscene, "preview_start_resolution");
486
487         /* other parameters */
488         if(b_scene.render().threads_mode() == BL::RenderSettings::threads_mode_FIXED)
489                 params.threads = b_scene.render().threads();
490         else
491                 params.threads = 0;
492
493         params.cancel_timeout = get_float(cscene, "debug_cancel_timeout");
494         params.reset_timeout = get_float(cscene, "debug_reset_timeout");
495         params.text_timeout = get_float(cscene, "debug_text_timeout");
496
497         params.progressive_refine = get_boolean(cscene, "use_progressive_refine");
498
499         if(background) {
500                 if(params.progressive_refine)
501                         params.progressive = true;
502                 else
503                         params.progressive = false;
504
505                 params.start_resolution = INT_MAX;
506         }
507         else
508                 params.progressive = true;
509
510         /* shading system - scene level needs full refresh */
511         const bool shadingsystem = RNA_boolean_get(&cscene, "shading_system");
512
513         if(shadingsystem == 0)
514                 params.shadingsystem = SessionParams::SVM;
515         else if(shadingsystem == 1)
516                 params.shadingsystem = SessionParams::OSL;
517         
518         /* color managagement */
519         params.display_buffer_linear = GLEW_ARB_half_float_pixel && b_engine.support_display_space_shader(b_scene);
520
521         return params;
522 }
523
524 CCL_NAMESPACE_END
525