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