add BLI_strcpy_rlen, replace strcat, which was used in misleading way.
[blender.git] / intern / cycles / blender / blender_sync.cpp
1 /*
2  * Copyright 2011, Blender Foundation.
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  */
18
19 #include "background.h"
20 #include "camera.h"
21 #include "film.h"
22 #include "graph.h"
23 #include "integrator.h"
24 #include "light.h"
25 #include "mesh.h"
26 #include "nodes.h"
27 #include "object.h"
28 #include "scene.h"
29 #include "shader.h"
30 #include "curves.h"
31
32 #include "device.h"
33
34 #include "blender_sync.h"
35 #include "blender_util.h"
36
37 #include "util_debug.h"
38 #include "util_foreach.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, 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         sync_objects(b_v3d);
147         sync_motion(b_v3d, b_override);
148 }
149
150 /* Integrator */
151
152 void BlenderSync::sync_integrator()
153 {
154 #ifdef __CAMERA_MOTION__
155         BL::RenderSettings r = b_scene.render();
156 #endif
157         PointerRNA cscene = RNA_pointer_get(&b_scene.ptr, "cycles");
158
159         experimental = (RNA_enum_get(&cscene, "feature_set") != 0);
160
161         Integrator *integrator = scene->integrator;
162         Integrator previntegrator = *integrator;
163
164         integrator->min_bounce = get_int(cscene, "min_bounces");
165         integrator->max_bounce = get_int(cscene, "max_bounces");
166
167         integrator->max_diffuse_bounce = get_int(cscene, "diffuse_bounces");
168         integrator->max_glossy_bounce = get_int(cscene, "glossy_bounces");
169         integrator->max_transmission_bounce = get_int(cscene, "transmission_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->no_caustics = get_boolean(cscene, "no_caustics");
176         integrator->filter_glossy = get_float(cscene, "blur_glossy");
177
178         integrator->seed = get_int(cscene, "seed");
179
180         integrator->layer_flag = render_layer.layer;
181
182         integrator->sample_clamp = get_float(cscene, "sample_clamp");
183 #ifdef __CAMERA_MOTION__
184         if(!preview) {
185                 if(integrator->motion_blur != r.use_motion_blur()) {
186                         scene->object_manager->tag_update(scene);
187                         scene->camera->tag_update();
188                 }
189
190                 integrator->motion_blur = r.use_motion_blur();
191         }
192 #endif
193
194         integrator->diffuse_samples = get_int(cscene, "diffuse_samples");
195         integrator->glossy_samples = get_int(cscene, "glossy_samples");
196         integrator->transmission_samples = get_int(cscene, "transmission_samples");
197         integrator->ao_samples = get_int(cscene, "ao_samples");
198         integrator->mesh_light_samples = get_int(cscene, "mesh_light_samples");
199         integrator->subsurface_samples = get_int(cscene, "subsurface_samples");
200         integrator->progressive = get_boolean(cscene, "progressive");
201
202         if(experimental)
203                 integrator->sampling_pattern = (SamplingPattern)RNA_enum_get(&cscene, "sampling_pattern");
204
205         if(integrator->modified(previntegrator))
206                 integrator->tag_update(scene);
207 }
208
209 /* Film */
210
211 void BlenderSync::sync_film()
212 {
213         PointerRNA cscene = RNA_pointer_get(&b_scene.ptr, "cycles");
214
215         Film *film = scene->film;
216         Film prevfilm = *film;
217
218         film->exposure = get_float(cscene, "film_exposure");
219         film->filter_type = (FilterType)RNA_enum_get(&cscene, "filter_type");
220         film->filter_width = (film->filter_type == FILTER_BOX)? 1.0f: get_float(cscene, "filter_width");
221
222         if(b_scene.world()) {
223                 BL::WorldMistSettings b_mist = b_scene.world().mist_settings();
224
225                 film->mist_start = b_mist.start();
226                 film->mist_depth = b_mist.depth();
227
228                 switch(b_mist.falloff()) {
229                         case BL::WorldMistSettings::falloff_QUADRATIC:
230                                 film->mist_falloff = 2.0f;
231                                 break;
232                         case BL::WorldMistSettings::falloff_LINEAR:
233                                 film->mist_falloff = 1.0f;
234                                 break;
235                         case BL::WorldMistSettings::falloff_INVERSE_QUADRATIC:
236                                 film->mist_falloff = 0.5f;
237                                 break;
238                 }
239         }
240
241         if(film->modified(prevfilm))
242                 film->tag_update(scene);
243 }
244
245 /* Render Layer */
246
247 void BlenderSync::sync_render_layers(BL::SpaceView3D b_v3d, const char *layer)
248 {
249         PointerRNA cscene = RNA_pointer_get(&b_scene.ptr, "cycles");
250         string layername;
251
252         /* 3d view */
253         if(b_v3d) {
254                 if(RNA_boolean_get(&cscene, "preview_active_layer")) {
255                         BL::RenderLayers layers(b_scene.render().ptr);
256                         layername = layers.active().name();
257                         layer = layername.c_str();
258                 }
259                 else {
260                         render_layer.use_localview = (b_v3d.local_view() ? true : false);
261                         render_layer.scene_layer = get_layer(b_v3d.layers(), b_v3d.layers_local_view(), render_layer.use_localview);
262                         render_layer.layer = render_layer.scene_layer;
263                         render_layer.exclude_layer = 0;
264                         render_layer.holdout_layer = 0;
265                         render_layer.material_override = PointerRNA_NULL;
266                         render_layer.use_background = true;
267                         render_layer.use_hair = true;
268                         render_layer.use_surfaces = true;
269                         render_layer.use_viewport_visibility = true;
270                         render_layer.samples = 0;
271                         render_layer.bound_samples = false;
272                         return;
273                 }
274         }
275
276         /* render layer */
277         BL::RenderSettings r = b_scene.render();
278         BL::RenderSettings::layers_iterator b_rlay;
279         int use_layer_samples = RNA_enum_get(&cscene, "use_layer_samples");
280         bool first_layer = true;
281
282         for(r.layers.begin(b_rlay); b_rlay != r.layers.end(); ++b_rlay) {
283                 if((!layer && first_layer) || (layer && b_rlay->name() == layer)) {
284                         render_layer.name = b_rlay->name();
285
286                         render_layer.holdout_layer = get_layer(b_rlay->layers_zmask());
287                         render_layer.exclude_layer = get_layer(b_rlay->layers_exclude());
288
289                         render_layer.scene_layer = get_layer(b_scene.layers()) & ~render_layer.exclude_layer;
290                         render_layer.scene_layer |= render_layer.exclude_layer & render_layer.holdout_layer;
291
292                         render_layer.layer = get_layer(b_rlay->layers());
293                         render_layer.layer |= render_layer.holdout_layer;
294
295                         render_layer.material_override = b_rlay->material_override();
296                         render_layer.use_background = b_rlay->use_sky();
297                         render_layer.use_surfaces = b_rlay->use_solid();
298                         render_layer.use_hair = b_rlay->use_strand();
299                         render_layer.use_viewport_visibility = false;
300                         render_layer.use_localview = false;
301
302                         render_layer.bound_samples = (use_layer_samples == 1);
303                         if(use_layer_samples != 2)
304                                 render_layer.samples = b_rlay->samples();
305                 }
306
307                 first_layer = false;
308         }
309 }
310
311 /* Scene Parameters */
312
313 SceneParams BlenderSync::get_scene_params(BL::Scene b_scene, bool background)
314 {
315         BL::RenderSettings r = b_scene.render();
316         SceneParams params;
317         PointerRNA cscene = RNA_pointer_get(&b_scene.ptr, "cycles");
318         int shadingsystem = RNA_boolean_get(&cscene, "shading_system");
319
320         if(shadingsystem == 0)
321                 params.shadingsystem = SceneParams::SVM;
322         else if(shadingsystem == 1)
323                 params.shadingsystem = SceneParams::OSL;
324         
325         if(background)
326                 params.bvh_type = SceneParams::BVH_STATIC;
327         else
328                 params.bvh_type = (SceneParams::BVHType)RNA_enum_get(&cscene, "debug_bvh_type");
329
330         params.use_bvh_spatial_split = RNA_boolean_get(&cscene, "debug_use_spatial_splits");
331         params.use_bvh_cache = (background)? RNA_boolean_get(&cscene, "use_cache"): false;
332
333         if(background && params.shadingsystem != SceneParams::OSL)
334                 params.persistent_data = r.use_persistent_data();
335         else
336                 params.persistent_data = false;
337
338         return params;
339 }
340
341 /* Session Parameters */
342
343 bool BlenderSync::get_session_pause(BL::Scene b_scene, bool background)
344 {
345         PointerRNA cscene = RNA_pointer_get(&b_scene.ptr, "cycles");
346         return (background)? false: get_boolean(cscene, "preview_pause");
347 }
348
349 SessionParams BlenderSync::get_session_params(BL::RenderEngine b_engine, BL::UserPreferences b_userpref, BL::Scene b_scene, bool background)
350 {
351         SessionParams params;
352         PointerRNA cscene = RNA_pointer_get(&b_scene.ptr, "cycles");
353
354         /* feature set */
355         params.experimental = (RNA_enum_get(&cscene, "feature_set") != 0);
356
357         /* device type */
358         vector<DeviceInfo>& devices = Device::available_devices();
359         
360         /* device default CPU */
361         params.device = devices[0];
362
363         if(RNA_enum_get(&cscene, "device") == 2) {
364                 /* find network device */
365                 foreach(DeviceInfo& info, devices)
366                         if(info.type == DEVICE_NETWORK)
367                                 params.device = info;
368         }
369         else if(RNA_enum_get(&cscene, "device") == 1) {
370                 /* find GPU device with given id */
371                 PointerRNA systemptr = b_userpref.system().ptr;
372                 PropertyRNA *deviceprop = RNA_struct_find_property(&systemptr, "compute_device");
373                 int device_id = b_userpref.system().compute_device();
374
375                 const char *id;
376
377                 if(RNA_property_enum_identifier(NULL, &systemptr, deviceprop, device_id, &id)) {
378                         foreach(DeviceInfo& info, devices)
379                                 if(info.id == id)
380                                         params.device = info;
381                 }
382         }
383
384         /* Background */
385         params.background = background;
386
387         /* samples */
388         if(get_boolean(cscene, "progressive") == 0 && params.device.type == DEVICE_CPU) {
389                 if(background) {
390                         params.samples = get_int(cscene, "aa_samples");
391                 }
392                 else {
393                         params.samples = get_int(cscene, "preview_aa_samples");
394                         if(params.samples == 0)
395                                 params.samples = INT_MAX;
396                 }
397         }
398         else {
399                 if(background) {
400                         params.samples = get_int(cscene, "samples");
401                 }
402                 else {
403                         params.samples = get_int(cscene, "preview_samples");
404                         if(params.samples == 0)
405                                 params.samples = INT_MAX;
406                 }
407         }
408
409         /* tiles */
410         if(params.device.type != DEVICE_CPU && !background) {
411                 /* currently GPU could be much slower than CPU when using tiles,
412                  * still need to be investigated, but meanwhile make it possible
413                  * to work in viewport smoothly
414                  */
415                 int debug_tile_size = get_int(cscene, "debug_tile_size");
416
417                 params.tile_size = make_int2(debug_tile_size, debug_tile_size);
418         }
419         else {
420                 int tile_x = b_engine.tile_x();
421                 int tile_y = b_engine.tile_y();
422
423                 params.tile_size = make_int2(tile_x, tile_y);
424         }
425         
426         params.tile_order = RNA_enum_get(&cscene, "tile_order");
427
428         params.start_resolution = get_int(cscene, "preview_start_resolution");
429
430         /* other parameters */
431         if(b_scene.render().threads_mode() == BL::RenderSettings::threads_mode_FIXED)
432                 params.threads = b_scene.render().threads();
433         else
434                 params.threads = 0;
435
436         params.cancel_timeout = get_float(cscene, "debug_cancel_timeout");
437         params.reset_timeout = get_float(cscene, "debug_reset_timeout");
438         params.text_timeout = get_float(cscene, "debug_text_timeout");
439
440         params.progressive_refine = get_boolean(cscene, "use_progressive_refine");
441
442         if(background) {
443                 if(params.progressive_refine)
444                         params.progressive = true;
445                 else
446                         params.progressive = false;
447
448                 params.start_resolution = INT_MAX;
449         }
450         else
451                 params.progressive = true;
452
453         /* shading system - scene level needs full refresh */
454         int shadingsystem = RNA_boolean_get(&cscene, "shading_system");
455
456         if(shadingsystem == 0)
457                 params.shadingsystem = SessionParams::SVM;
458         else if(shadingsystem == 1)
459                 params.shadingsystem = SessionParams::OSL;
460
461         return params;
462 }
463
464 CCL_NAMESPACE_END
465