Cycles: first step for implementation of non-progressive sampler that handles
[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 {
53         scene = scene_;
54         preview = preview_;
55 }
56
57 BlenderSync::~BlenderSync()
58 {
59 }
60
61 /* Sync */
62
63 bool BlenderSync::sync_recalc()
64 {
65         /* sync recalc flags from blender to cycles. actual update is done separate,
66          * so we can do it later on if doing it immediate is not suitable */
67
68         BL::BlendData::materials_iterator b_mat;
69
70         for(b_data.materials.begin(b_mat); b_mat != b_data.materials.end(); ++b_mat)
71                 if(b_mat->is_updated() || (b_mat->node_tree() && b_mat->node_tree().is_updated()))
72                         shader_map.set_recalc(*b_mat);
73
74         BL::BlendData::lamps_iterator b_lamp;
75
76         for(b_data.lamps.begin(b_lamp); b_lamp != b_data.lamps.end(); ++b_lamp)
77                 if(b_lamp->is_updated() || (b_lamp->node_tree() && b_lamp->node_tree().is_updated()))
78                         shader_map.set_recalc(*b_lamp);
79
80         BL::BlendData::objects_iterator b_ob;
81
82         for(b_data.objects.begin(b_ob); b_ob != b_data.objects.end(); ++b_ob) {
83                 if(b_ob->is_updated()) {
84                         object_map.set_recalc(*b_ob);
85                         light_map.set_recalc(*b_ob);
86                 }
87
88                 if(object_is_mesh(*b_ob)) {
89                         if(b_ob->is_updated_data() || b_ob->data().is_updated()) {
90                                 BL::ID key = BKE_object_is_modified(*b_ob)? *b_ob: b_ob->data();
91                                 mesh_map.set_recalc(key);
92                         }
93                 }
94                 else if(object_is_light(*b_ob)) {
95                         if(b_ob->is_updated_data() || b_ob->data().is_updated())
96                                 light_map.set_recalc(*b_ob);
97                 }
98         }
99
100         BL::BlendData::meshes_iterator b_mesh;
101
102         for(b_data.meshes.begin(b_mesh); b_mesh != b_data.meshes.end(); ++b_mesh)
103                 if(b_mesh->is_updated())
104                         mesh_map.set_recalc(*b_mesh);
105
106         BL::BlendData::worlds_iterator b_world;
107
108         for(b_data.worlds.begin(b_world); b_world != b_data.worlds.end(); ++b_world) {
109                 if(world_map == b_world->ptr.data &&
110                    (b_world->is_updated() || (b_world->node_tree() && b_world->node_tree().is_updated())))
111                 {
112                         world_recalc = true;
113                 }
114         }
115
116         bool recalc =
117                 shader_map.has_recalc() ||
118                 object_map.has_recalc() ||
119                 light_map.has_recalc() ||
120                 mesh_map.has_recalc() ||
121                 BlendDataObjects_is_updated_get(&b_data.ptr) ||
122                 world_recalc;
123
124         return recalc;
125 }
126
127 void BlenderSync::sync_data(BL::SpaceView3D b_v3d, BL::Object b_override, const char *layer)
128 {
129         sync_render_layers(b_v3d, layer);
130         sync_integrator();
131         sync_film();
132         sync_shaders();
133         sync_objects(b_v3d);
134         sync_motion(b_v3d, b_override);
135 }
136
137 /* Integrator */
138
139 void BlenderSync::sync_integrator()
140 {
141         PointerRNA cscene = RNA_pointer_get(&b_scene.ptr, "cycles");
142
143         experimental = (RNA_enum_get(&cscene, "feature_set") != 0);
144
145         Integrator *integrator = scene->integrator;
146         Integrator previntegrator = *integrator;
147
148         integrator->min_bounce = get_int(cscene, "min_bounces");
149         integrator->max_bounce = get_int(cscene, "max_bounces");
150
151         integrator->max_diffuse_bounce = get_int(cscene, "diffuse_bounces");
152         integrator->max_glossy_bounce = get_int(cscene, "glossy_bounces");
153         integrator->max_transmission_bounce = get_int(cscene, "transmission_bounces");
154
155         integrator->transparent_max_bounce = get_int(cscene, "transparent_max_bounces");
156         integrator->transparent_min_bounce = get_int(cscene, "transparent_min_bounces");
157         integrator->transparent_shadows = get_boolean(cscene, "use_transparent_shadows");
158
159         integrator->no_caustics = get_boolean(cscene, "no_caustics");
160         integrator->filter_glossy = get_float(cscene, "blur_glossy");
161
162         integrator->seed = get_int(cscene, "seed");
163
164         integrator->layer_flag = render_layer.layer;
165
166         integrator->sample_clamp = get_float(cscene, "sample_clamp");
167 #ifdef __MOTION__
168         integrator->motion_blur = (!preview && r.use_motion_blur());
169 #endif
170
171         integrator->diffuse_samples = get_int(cscene, "diffuse_samples");
172         integrator->glossy_samples = get_int(cscene, "glossy_samples");
173         integrator->transmission_samples = get_int(cscene, "transmission_samples");
174         integrator->ao_samples = get_int(cscene, "ao_samples");
175         integrator->mesh_light_samples = get_int(cscene, "mesh_light_samples");
176         integrator->progressive = get_boolean(cscene, "progressive");
177
178         if(integrator->modified(previntegrator))
179                 integrator->tag_update(scene);
180 }
181
182 /* Film */
183
184 void BlenderSync::sync_film()
185 {
186         PointerRNA cscene = RNA_pointer_get(&b_scene.ptr, "cycles");
187
188         Film *film = scene->film;
189         Film prevfilm = *film;
190
191         film->exposure = get_float(cscene, "film_exposure");
192
193         if(film->modified(prevfilm))
194                 film->tag_update(scene);
195
196         Filter *filter = scene->filter;
197         Filter prevfilter = *filter;
198
199         filter->filter_type = (FilterType)RNA_enum_get(&cscene, "filter_type");
200         filter->filter_width = (filter->filter_type == FILTER_BOX)? 1.0f: get_float(cscene, "filter_width");
201
202         if(filter->modified(prevfilter))
203                 filter->tag_update(scene);
204 }
205
206 /* Render Layer */
207
208 void BlenderSync::sync_render_layers(BL::SpaceView3D b_v3d, const char *layer)
209 {
210         string layername;
211
212         /* 3d view */
213         if(b_v3d) {
214                 PointerRNA cscene = RNA_pointer_get(&b_scene.ptr, "cycles");
215
216                 if(RNA_boolean_get(&cscene, "preview_active_layer")) {
217                         BL::RenderLayers layers(b_scene.render().ptr);
218                         layername = layers.active().name();
219                         layer = layername.c_str();
220                 }
221                 else {
222                         render_layer.scene_layer = get_layer(b_v3d.layers());
223                         render_layer.layer = render_layer.scene_layer;
224                         render_layer.holdout_layer = 0;
225                         render_layer.material_override = PointerRNA_NULL;
226                         render_layer.use_background = true;
227                         render_layer.use_viewport_visibility = true;
228                         render_layer.samples = 0;
229                         return;
230                 }
231         }
232
233         /* render layer */
234         BL::RenderSettings r = b_scene.render();
235         BL::RenderSettings::layers_iterator b_rlay;
236         bool first_layer = true;
237
238         for(r.layers.begin(b_rlay); b_rlay != r.layers.end(); ++b_rlay) {
239                 if((!layer && first_layer) || (layer && b_rlay->name() == layer)) {
240                         render_layer.name = b_rlay->name();
241                         render_layer.scene_layer = get_layer(b_scene.layers()) & ~get_layer(b_rlay->layers_exclude());
242                         render_layer.layer = get_layer(b_rlay->layers());
243                         render_layer.holdout_layer = get_layer(b_rlay->layers_zmask());
244                         render_layer.layer |= render_layer.holdout_layer;
245                         render_layer.material_override = b_rlay->material_override();
246                         render_layer.use_background = b_rlay->use_sky();
247                         render_layer.use_viewport_visibility = false;
248                         render_layer.samples = b_rlay->samples();
249                 }
250
251                 first_layer = false;
252         }
253 }
254
255 /* Scene Parameters */
256
257 SceneParams BlenderSync::get_scene_params(BL::Scene b_scene, bool background)
258 {
259         SceneParams params;
260         PointerRNA cscene = RNA_pointer_get(&b_scene.ptr, "cycles");
261         int shadingsystem = RNA_enum_get(&cscene, "shading_system");
262
263         if(shadingsystem == 0)
264                 params.shadingsystem = SceneParams::SVM;
265         else if(shadingsystem == 1)
266                 params.shadingsystem = SceneParams::OSL;
267         
268         if(background)
269                 params.bvh_type = SceneParams::BVH_STATIC;
270         else
271                 params.bvh_type = (SceneParams::BVHType)RNA_enum_get(&cscene, "debug_bvh_type");
272
273         params.use_bvh_spatial_split = RNA_boolean_get(&cscene, "debug_use_spatial_splits");
274         params.use_bvh_cache = (background)? RNA_boolean_get(&cscene, "use_cache"): false;
275
276         return params;
277 }
278
279 /* Session Parameters */
280
281 bool BlenderSync::get_session_pause(BL::Scene b_scene, bool background)
282 {
283         PointerRNA cscene = RNA_pointer_get(&b_scene.ptr, "cycles");
284         return (background)? false: get_boolean(cscene, "preview_pause");
285 }
286
287 SessionParams BlenderSync::get_session_params(BL::UserPreferences b_userpref, BL::Scene b_scene, bool background)
288 {
289         SessionParams params;
290         PointerRNA cscene = RNA_pointer_get(&b_scene.ptr, "cycles");
291
292         /* feature set */
293         params.experimental = (RNA_enum_get(&cscene, "feature_set") != 0);
294
295         /* device type */
296         vector<DeviceInfo>& devices = Device::available_devices();
297         
298         /* device default CPU */
299         params.device = devices[0];
300
301         if(RNA_enum_get(&cscene, "device") != 0) {
302                 /* find GPU device with given id */
303                 PointerRNA systemptr = b_userpref.system().ptr;
304                 PropertyRNA *deviceprop = RNA_struct_find_property(&systemptr, "compute_device");
305                 int device_id = b_userpref.system().compute_device();
306
307                 const char *id;
308
309                 if(RNA_property_enum_identifier(NULL, &systemptr, deviceprop, device_id, &id)) {
310                         foreach(DeviceInfo& info, devices)
311                                 if(info.id == id)
312                                         params.device = info;
313                 }
314         }
315
316         /* Background */
317         params.background = background;
318
319         /* samples */
320         if(get_boolean(cscene, "progressive")) {
321                 if(background) {
322                         params.samples = get_int(cscene, "samples");
323                 }
324                 else {
325                         params.samples = get_int(cscene, "preview_samples");
326                         if(params.samples == 0)
327                                 params.samples = INT_MAX;
328                 }
329         }
330         else {
331                 if(background) {
332                         params.samples = get_int(cscene, "aa_samples");
333                 }
334                 else {
335                         params.samples = get_int(cscene, "preview_aa_samples");
336                         if(params.samples == 0)
337                                 params.samples = INT_MAX;
338                 }
339         }
340
341         /* other parameters */
342         params.threads = b_scene.render().threads();
343         params.tile_size = get_int(cscene, "debug_tile_size");
344         params.min_size = get_int(cscene, "debug_min_size");
345         params.cancel_timeout = get_float(cscene, "debug_cancel_timeout");
346         params.reset_timeout = get_float(cscene, "debug_reset_timeout");
347         params.text_timeout = get_float(cscene, "debug_text_timeout");
348
349         if(background) {
350                 params.progressive = true;
351                 params.min_size = INT_MAX;
352         }
353         else
354                 params.progressive = true;
355         
356         /* todo: multi device only works with single tiles now */
357         if(params.device.type == DEVICE_MULTI)
358                 params.tile_size = INT_MAX;
359
360         return params;
361 }
362
363 CCL_NAMESPACE_END
364