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