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