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