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