Code cleanup: spelling fix, Tesselection->tesselation
[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 "film.h"
21 #include "../render/filter.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
31 #include "device.h"
32
33 #include "blender_sync.h"
34 #include "blender_util.h"
35
36 #include "util_debug.h"
37 #include "util_foreach.h"
38
39 CCL_NAMESPACE_BEGIN
40
41 /* Constructor */
42
43 BlenderSync::BlenderSync(BL::BlendData b_data_, BL::Scene b_scene_, Scene *scene_, bool preview_)
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   world_map(NULL),
50   world_recalc(false),
51   experimental(false),
52   active_layer(0)
53 {
54         scene = scene_;
55         preview = preview_;
56 }
57
58 BlenderSync::~BlenderSync()
59 {
60 }
61
62 /* Sync */
63
64 bool BlenderSync::sync_recalc()
65 {
66         /* sync recalc flags from blender to cycles. actual update is done separate,
67            so we can do it later on if doing it immediate is not suitable */
68
69         BL::BlendData::materials_iterator b_mat;
70
71         for(b_data.materials.begin(b_mat); b_mat != b_data.materials.end(); ++b_mat)
72                 if(b_mat->is_updated() || (b_mat->node_tree() && b_mat->node_tree().is_updated()))
73                         shader_map.set_recalc(*b_mat);
74
75         BL::BlendData::lamps_iterator b_lamp;
76
77         for(b_data.lamps.begin(b_lamp); b_lamp != b_data.lamps.end(); ++b_lamp)
78                 if(b_lamp->is_updated() || (b_lamp->node_tree() && b_lamp->node_tree().is_updated()))
79                         shader_map.set_recalc(*b_lamp);
80
81         BL::BlendData::objects_iterator b_ob;
82
83         for(b_data.objects.begin(b_ob); b_ob != b_data.objects.end(); ++b_ob) {
84                 if(b_ob->is_updated()) {
85                         object_map.set_recalc(*b_ob);
86                         light_map.set_recalc(*b_ob);
87                 }
88
89                 if(object_is_mesh(*b_ob)) {
90                         if(b_ob->is_updated_data() || b_ob->data().is_updated()) {
91                                 BL::ID key = object_is_modified(*b_ob)? *b_ob: b_ob->data();
92                                 mesh_map.set_recalc(key);
93                         }
94                 }
95                 else if(object_is_light(*b_ob)) {
96                         if(b_ob->is_updated_data() || b_ob->data().is_updated())
97                                 light_map.set_recalc(*b_ob);
98                 }
99         }
100
101         BL::BlendData::meshes_iterator b_mesh;
102
103         for(b_data.meshes.begin(b_mesh); b_mesh != b_data.meshes.end(); ++b_mesh)
104                 if(b_mesh->is_updated())
105                         mesh_map.set_recalc(*b_mesh);
106
107         BL::BlendData::worlds_iterator b_world;
108
109         for(b_data.worlds.begin(b_world); b_world != b_data.worlds.end(); ++b_world)
110                 if(world_map == b_world->ptr.data &&
111                         (b_world->is_updated() || (b_world->node_tree() && b_world->node_tree().is_updated())))
112                         world_recalc = true;
113
114         bool recalc =
115                 shader_map.has_recalc() ||
116                 object_map.has_recalc() ||
117                 light_map.has_recalc() ||
118                 mesh_map.has_recalc() ||
119                 BlendDataObjects_is_updated_get(&b_data.ptr) ||
120                 world_recalc;
121
122         return recalc;
123 }
124
125 void BlenderSync::sync_data(BL::SpaceView3D b_v3d, int layer)
126 {
127         sync_render_layers(b_v3d);
128         sync_integrator(layer);
129         sync_film();
130         sync_shaders();
131         sync_objects(b_v3d);
132 }
133
134 /* Integrator */
135
136 void BlenderSync::sync_integrator(int layer)
137 {
138         PointerRNA cscene = RNA_pointer_get(&b_scene.ptr, "cycles");
139
140         experimental = (RNA_enum_get(&cscene, "feature_set") != 0);
141
142         Integrator *integrator = scene->integrator;
143         Integrator previntegrator = *integrator;
144
145         integrator->min_bounce = get_int(cscene, "min_bounces");
146         integrator->max_bounce = get_int(cscene, "max_bounces");
147
148         integrator->max_diffuse_bounce = get_int(cscene, "diffuse_bounces");
149         integrator->max_glossy_bounce = get_int(cscene, "glossy_bounces");
150         integrator->max_transmission_bounce = get_int(cscene, "transmission_bounces");
151
152         integrator->transparent_max_bounce = get_int(cscene, "transparent_max_bounces");
153         integrator->transparent_min_bounce = get_int(cscene, "transparent_min_bounces");
154         integrator->transparent_shadows = get_boolean(cscene, "use_transparent_shadows");
155
156         integrator->no_caustics = get_boolean(cscene, "no_caustics");
157         integrator->seed = get_int(cscene, "seed");
158         integrator->layer_flag = render_layers[layer].layer;
159
160         if(integrator->modified(previntegrator))
161                 integrator->tag_update(scene);
162 }
163
164 /* Film */
165
166 void BlenderSync::sync_film()
167 {
168         PointerRNA cscene = RNA_pointer_get(&b_scene.ptr, "cycles");
169
170         Film *film = scene->film;
171         Film prevfilm = *film;
172
173         film->exposure = get_float(cscene, "film_exposure");
174
175         if(film->modified(prevfilm))
176                 film->tag_update(scene);
177
178         Filter *filter = scene->filter;
179         Filter prevfilter = *filter;
180
181         filter->filter_type = (FilterType)RNA_enum_get(&cscene, "filter_type");
182         filter->filter_width = (filter->filter_type == FILTER_BOX)? 1.0f: get_float(cscene, "filter_width");
183
184         if(filter->modified(prevfilter))
185                 filter->tag_update(scene);
186 }
187
188 /* Render Layer */
189
190 void BlenderSync::sync_render_layers(BL::SpaceView3D b_v3d)
191 {
192         render_layers.clear();
193
194         if(b_v3d) {
195                 RenderLayerInfo rlay;
196
197                 rlay.scene_layer = get_layer(b_v3d.layers());
198                 rlay.layer = rlay.scene_layer;
199                 rlay.material_override = PointerRNA_NULL;
200
201                 render_layers.push_back(rlay);
202         }
203         else {
204                 BL::RenderSettings r = b_scene.render();
205                 BL::RenderSettings::layers_iterator b_rlay;
206
207                 for(r.layers.begin(b_rlay); b_rlay != r.layers.end(); ++b_rlay) {
208                         /* single layer for now */
209                         RenderLayerInfo rlay;
210
211                         rlay.scene_layer = get_layer(b_scene.layers());
212                         rlay.layer = get_layer(b_rlay->layers());
213                         rlay.material_override = b_rlay->material_override();
214
215                         render_layers.push_back(rlay);
216                 }
217         }
218 }
219
220 /* Scene Parameters */
221
222 SceneParams BlenderSync::get_scene_params(BL::Scene b_scene, bool background)
223 {
224         SceneParams params;
225         PointerRNA cscene = RNA_pointer_get(&b_scene.ptr, "cycles");
226         int shadingsystem = RNA_enum_get(&cscene, "shading_system");
227
228         if(shadingsystem == 0)
229                 params.shadingsystem = SceneParams::SVM;
230         else if(shadingsystem == 1)
231                 params.shadingsystem = SceneParams::OSL;
232         
233         if(background)
234                 params.bvh_type = SceneParams::BVH_STATIC;
235         else
236                 params.bvh_type = (SceneParams::BVHType)RNA_enum_get(&cscene, "debug_bvh_type");
237
238         params.use_bvh_spatial_split = RNA_boolean_get(&cscene, "debug_use_spatial_splits");
239         params.use_bvh_cache = (background)? RNA_boolean_get(&cscene, "use_cache"): false;
240
241         return params;
242 }
243
244 /* Session Parameters */
245
246 bool BlenderSync::get_session_pause(BL::Scene b_scene, bool background)
247 {
248         PointerRNA cscene = RNA_pointer_get(&b_scene.ptr, "cycles");
249         return (background)? false: get_boolean(cscene, "preview_pause");
250 }
251
252 SessionParams BlenderSync::get_session_params(BL::UserPreferences b_userpref, BL::Scene b_scene, bool background)
253 {
254         SessionParams params;
255         PointerRNA cscene = RNA_pointer_get(&b_scene.ptr, "cycles");
256
257         /* feature set */
258         params.experimental = (RNA_enum_get(&cscene, "feature_set") != 0);
259
260         /* device type */
261         vector<DeviceInfo>& devices = Device::available_devices();
262         
263         /* device default CPU */
264         params.device = devices[0];
265
266         if(RNA_enum_get(&cscene, "device") != 0) {
267                 /* find GPU device with given id */
268                 PointerRNA systemptr = b_userpref.system().ptr;
269                 PropertyRNA *deviceprop = RNA_struct_find_property(&systemptr, "compute_device");
270                 int device_id = b_userpref.system().compute_device();
271
272                 const char *id;
273
274                 if(RNA_property_enum_identifier(NULL, &systemptr, deviceprop, device_id, &id)) {
275                         foreach(DeviceInfo& info, devices)
276                                 if(info.id == id)
277                                         params.device = info;
278                 }
279         }
280
281         /* Background */
282         params.background = background;
283                         
284         /* samples */
285         if(background) {
286                 params.samples = get_int(cscene, "samples");
287         }
288         else {
289                 params.samples = get_int(cscene, "preview_samples");
290                 if(params.samples == 0)
291                         params.samples = INT_MAX;
292         }
293
294         /* other parameters */
295         params.threads = b_scene.render().threads();
296         params.tile_size = get_int(cscene, "debug_tile_size");
297         params.min_size = get_int(cscene, "debug_min_size");
298         params.cancel_timeout = get_float(cscene, "debug_cancel_timeout");
299         params.reset_timeout = get_float(cscene, "debug_reset_timeout");
300         params.text_timeout = get_float(cscene, "debug_text_timeout");
301
302         if(background) {
303                 params.progressive = true;
304                 params.min_size = INT_MAX;
305         }
306         else
307                 params.progressive = true;
308         
309         /* todo: multi device only works with single tiles now */
310         if(params.device.type == DEVICE_MULTI)
311                 params.tile_size = INT_MAX;
312
313         return params;
314 }
315
316 CCL_NAMESPACE_END
317