Merge branch 'master' into blender2.8
[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_session.h"
34 #include "blender_util.h"
35
36 #include "util_debug.h"
37 #include "util_foreach.h"
38 #include "util_opengl.h"
39 #include "util_hash.h"
40
41 CCL_NAMESPACE_BEGIN
42
43 /* Constructor */
44
45 BlenderSync::BlenderSync(BL::RenderEngine& b_engine,
46                          BL::BlendData& b_data,
47                          BL::Scene& b_scene,
48                          Scene *scene,
49                          bool preview,
50                          Progress &progress,
51                          bool is_cpu)
52 : b_engine(b_engine),
53   b_data(b_data),
54   b_scene(b_scene),
55   shader_map(&scene->shaders),
56   object_map(&scene->objects),
57   mesh_map(&scene->meshes),
58   light_map(&scene->lights),
59   world_map(NULL),
60   world_recalc(false),
61   scene(scene),
62   preview(preview),
63   experimental(false),
64   is_cpu(is_cpu),
65   dicing_rate(1.0f),
66   max_subdivisions(12),
67   progress(progress)
68 {
69         PointerRNA cscene = RNA_pointer_get(&b_scene.ptr, "cycles");
70         dicing_rate = preview ? RNA_float_get(&cscene, "preview_dicing_rate") : RNA_float_get(&cscene, "dicing_rate");
71         max_subdivisions = RNA_int_get(&cscene, "max_subdivisions");
72 }
73
74 BlenderSync::~BlenderSync()
75 {
76 }
77
78 /* Sync */
79
80 bool BlenderSync::sync_recalc()
81 {
82         /* sync recalc flags from blender to cycles. actual update is done separate,
83          * so we can do it later on if doing it immediate is not suitable */
84
85         BL::BlendData::materials_iterator b_mat;
86         bool has_updated_objects = b_data.objects.is_updated();
87         for(b_data.materials.begin(b_mat); b_mat != b_data.materials.end(); ++b_mat) {
88                 if(b_mat->is_updated() || (b_mat->node_tree() && b_mat->node_tree().is_updated())) {
89                         shader_map.set_recalc(*b_mat);
90                 }
91                 else {
92                         Shader *shader = shader_map.find(*b_mat);
93                         if(has_updated_objects && shader != NULL && shader->has_object_dependency) {
94                                 shader_map.set_recalc(*b_mat);
95                         }
96                 }
97         }
98
99         BL::BlendData::lamps_iterator b_lamp;
100
101         for(b_data.lamps.begin(b_lamp); b_lamp != b_data.lamps.end(); ++b_lamp)
102                 if(b_lamp->is_updated() || (b_lamp->node_tree() && b_lamp->node_tree().is_updated()))
103                         shader_map.set_recalc(*b_lamp);
104
105         bool dicing_prop_changed = false;
106
107         if(experimental) {
108                 PointerRNA cscene = RNA_pointer_get(&b_scene.ptr, "cycles");
109
110                 float updated_dicing_rate = preview ? RNA_float_get(&cscene, "preview_dicing_rate")
111                                                     : RNA_float_get(&cscene, "dicing_rate");
112
113                 if(dicing_rate != updated_dicing_rate) {
114                         dicing_rate = updated_dicing_rate;
115                         dicing_prop_changed = true;
116                 }
117
118                 int updated_max_subdivisions = RNA_int_get(&cscene, "max_subdivisions");
119
120                 if(max_subdivisions != updated_max_subdivisions) {
121                         max_subdivisions = updated_max_subdivisions;
122                         dicing_prop_changed = true;
123                 }
124         }
125
126         BL::BlendData::objects_iterator b_ob;
127
128         for(b_data.objects.begin(b_ob); b_ob != b_data.objects.end(); ++b_ob) {
129                 if(b_ob->is_updated()) {
130                         object_map.set_recalc(*b_ob);
131                         light_map.set_recalc(*b_ob);
132                 }
133
134                 if(object_is_mesh(*b_ob)) {
135                         if(b_ob->is_updated_data() || b_ob->data().is_updated() ||
136                            (dicing_prop_changed && object_subdivision_type(*b_ob, preview, experimental) != Mesh::SUBDIVISION_NONE))
137                         {
138                                 BL::ID key = BKE_object_is_modified(*b_ob)? *b_ob: b_ob->data();
139                                 mesh_map.set_recalc(key);
140                         }
141                 }
142                 else if(object_is_light(*b_ob)) {
143                         if(b_ob->is_updated_data() || b_ob->data().is_updated())
144                                 light_map.set_recalc(*b_ob);
145                 }
146         }
147
148         BL::BlendData::meshes_iterator b_mesh;
149
150         for(b_data.meshes.begin(b_mesh); b_mesh != b_data.meshes.end(); ++b_mesh) {
151                 if(b_mesh->is_updated()) {
152                         mesh_map.set_recalc(*b_mesh);
153                 }
154         }
155
156         BL::BlendData::worlds_iterator b_world;
157
158         for(b_data.worlds.begin(b_world); b_world != b_data.worlds.end(); ++b_world) {
159                 if(world_map == b_world->ptr.data) {
160                         if(b_world->is_updated() ||
161                            (b_world->node_tree() && b_world->node_tree().is_updated()))
162                         {
163                                 world_recalc = true;
164                         }
165                         else if(b_world->node_tree() && b_world->use_nodes()) {
166                                 Shader *shader = scene->default_background;
167                                 if(has_updated_objects && shader->has_object_dependency) {
168                                         world_recalc = true;
169                                 }
170                         }
171                 }
172         }
173
174         bool recalc =
175                 shader_map.has_recalc() ||
176                 object_map.has_recalc() ||
177                 light_map.has_recalc() ||
178                 mesh_map.has_recalc() ||
179                 BlendDataObjects_is_updated_get(&b_data.ptr) ||
180                 world_recalc;
181
182         return recalc;
183 }
184
185 void BlenderSync::sync_data(BL::RenderSettings& b_render,
186                             BL::SpaceView3D& b_v3d,
187                             BL::Object& b_override,
188                             int width, int height,
189                             void **python_thread_state,
190                             const char *layer)
191 {
192         sync_render_layers(b_v3d, layer);
193         sync_integrator();
194         sync_film();
195         sync_shaders();
196         sync_images();
197         sync_curve_settings();
198
199         mesh_synced.clear(); /* use for objects and motion sync */
200
201         if(scene->need_motion() == Scene::MOTION_PASS ||
202            scene->need_motion() == Scene::MOTION_NONE ||
203            scene->camera->motion_position == Camera::MOTION_POSITION_CENTER)
204         {
205                 sync_objects(b_v3d);
206         }
207         sync_motion(b_render,
208                     b_v3d,
209                     b_override,
210                     width, height,
211                     python_thread_state);
212
213         mesh_synced.clear();
214 }
215
216 /* Integrator */
217
218 void BlenderSync::sync_integrator()
219 {
220 #ifdef __CAMERA_MOTION__
221         BL::RenderSettings r = b_scene.render();
222 #endif
223         PointerRNA cscene = RNA_pointer_get(&b_scene.ptr, "cycles");
224
225         experimental = (get_enum(cscene, "feature_set") != 0);
226
227         Integrator *integrator = scene->integrator;
228         Integrator previntegrator = *integrator;
229
230         integrator->min_bounce = get_int(cscene, "min_bounces");
231         integrator->max_bounce = get_int(cscene, "max_bounces");
232
233         integrator->max_diffuse_bounce = get_int(cscene, "diffuse_bounces");
234         integrator->max_glossy_bounce = get_int(cscene, "glossy_bounces");
235         integrator->max_transmission_bounce = get_int(cscene, "transmission_bounces");
236         integrator->max_volume_bounce = get_int(cscene, "volume_bounces");
237
238         integrator->transparent_max_bounce = get_int(cscene, "transparent_max_bounces");
239         integrator->transparent_min_bounce = get_int(cscene, "transparent_min_bounces");
240         integrator->transparent_shadows = get_boolean(cscene, "use_transparent_shadows");
241
242         integrator->volume_max_steps = get_int(cscene, "volume_max_steps");
243         integrator->volume_step_size = get_float(cscene, "volume_step_size");
244
245         integrator->caustics_reflective = get_boolean(cscene, "caustics_reflective");
246         integrator->caustics_refractive = get_boolean(cscene, "caustics_refractive");
247         integrator->filter_glossy = get_float(cscene, "blur_glossy");
248
249         integrator->seed = get_int(cscene, "seed");
250         if(get_boolean(cscene, "use_animated_seed")) {
251                 integrator->seed = hash_int_2d(b_scene.frame_current(),
252                                                get_int(cscene, "seed"));
253                 if(b_scene.frame_subframe() != 0.0f) {
254                         /* TODO(sergey): Ideally should be some sort of hash_merge,
255                          * but this is good enough for now.
256                          */
257                         integrator->seed += hash_int_2d((int)(b_scene.frame_subframe() * (float)INT_MAX),
258                                                         get_int(cscene, "seed"));
259                 }
260         }
261
262         integrator->sampling_pattern = (SamplingPattern)get_enum(
263                 cscene,
264                 "sampling_pattern",
265                 SAMPLING_NUM_PATTERNS,
266                 SAMPLING_PATTERN_SOBOL);
267
268         integrator->sample_clamp_direct = get_float(cscene, "sample_clamp_direct");
269         integrator->sample_clamp_indirect = get_float(cscene, "sample_clamp_indirect");
270 #ifdef __CAMERA_MOTION__
271         if(!preview) {
272                 if(integrator->motion_blur != r.use_motion_blur()) {
273                         scene->object_manager->tag_update(scene);
274                         scene->camera->tag_update();
275                 }
276
277                 integrator->motion_blur = r.use_motion_blur();
278         }
279 #endif
280
281         integrator->method = (Integrator::Method)get_enum(cscene,
282                                                           "progressive",
283                                                           Integrator::NUM_METHODS,
284                                                           Integrator::PATH);
285
286         integrator->sample_all_lights_direct = get_boolean(cscene, "sample_all_lights_direct");
287         integrator->sample_all_lights_indirect = get_boolean(cscene, "sample_all_lights_indirect");
288         integrator->light_sampling_threshold = get_float(cscene, "light_sampling_threshold");
289
290         int diffuse_samples = get_int(cscene, "diffuse_samples");
291         int glossy_samples = get_int(cscene, "glossy_samples");
292         int transmission_samples = get_int(cscene, "transmission_samples");
293         int ao_samples = get_int(cscene, "ao_samples");
294         int mesh_light_samples = get_int(cscene, "mesh_light_samples");
295         int subsurface_samples = get_int(cscene, "subsurface_samples");
296         int volume_samples = get_int(cscene, "volume_samples");
297
298         if(get_boolean(cscene, "use_square_samples")) {
299                 integrator->diffuse_samples = diffuse_samples * diffuse_samples;
300                 integrator->glossy_samples = glossy_samples * glossy_samples;
301                 integrator->transmission_samples = transmission_samples * transmission_samples;
302                 integrator->ao_samples = ao_samples * ao_samples;
303                 integrator->mesh_light_samples = mesh_light_samples * mesh_light_samples;
304                 integrator->subsurface_samples = subsurface_samples * subsurface_samples;
305                 integrator->volume_samples = volume_samples * volume_samples;
306         } 
307         else {
308                 integrator->diffuse_samples = diffuse_samples;
309                 integrator->glossy_samples = glossy_samples;
310                 integrator->transmission_samples = transmission_samples;
311                 integrator->ao_samples = ao_samples;
312                 integrator->mesh_light_samples = mesh_light_samples;
313                 integrator->subsurface_samples = subsurface_samples;
314                 integrator->volume_samples = volume_samples;
315         }
316
317         if(integrator->modified(previntegrator))
318                 integrator->tag_update(scene);
319 }
320
321 /* Film */
322
323 void BlenderSync::sync_film()
324 {
325         PointerRNA cscene = RNA_pointer_get(&b_scene.ptr, "cycles");
326
327         Film *film = scene->film;
328         Film prevfilm = *film;
329         
330         film->exposure = get_float(cscene, "film_exposure");
331         film->filter_type = (FilterType)get_enum(cscene,
332                                                  "pixel_filter_type",
333                                                  FILTER_NUM_TYPES,
334                                                  FILTER_BLACKMAN_HARRIS);
335         film->filter_width = (film->filter_type == FILTER_BOX)? 1.0f: get_float(cscene, "filter_width");
336
337         if(b_scene.world()) {
338                 BL::WorldMistSettings b_mist = b_scene.world().mist_settings();
339
340                 film->mist_start = b_mist.start();
341                 film->mist_depth = b_mist.depth();
342
343                 switch(b_mist.falloff()) {
344                         case BL::WorldMistSettings::falloff_QUADRATIC:
345                                 film->mist_falloff = 2.0f;
346                                 break;
347                         case BL::WorldMistSettings::falloff_LINEAR:
348                                 film->mist_falloff = 1.0f;
349                                 break;
350                         case BL::WorldMistSettings::falloff_INVERSE_QUADRATIC:
351                                 film->mist_falloff = 0.5f;
352                                 break;
353                 }
354         }
355
356         if(film->modified(prevfilm))
357                 film->tag_update(scene);
358 }
359
360 /* Render Layer */
361
362 void BlenderSync::sync_render_layers(BL::SpaceView3D& b_v3d, const char *layer)
363 {
364         PointerRNA cscene = RNA_pointer_get(&b_scene.ptr, "cycles");
365         string layername;
366
367         /* 3d view */
368         if(b_v3d) {
369                 if(RNA_boolean_get(&cscene, "preview_active_layer")) {
370                         BL::RenderLayers layers(b_scene.render().ptr);
371                         layername = layers.active().name();
372                         layer = layername.c_str();
373                 }
374                 else {
375                         render_layer.scene_layer = get_layer(b_v3d.layers(), b_v3d.layers_local_view());
376                         render_layer.layer = render_layer.scene_layer;
377                         render_layer.exclude_layer = 0;
378                         render_layer.holdout_layer = 0;
379                         render_layer.material_override = PointerRNA_NULL;
380                         render_layer.use_background_shader = true;
381                         render_layer.use_background_ao = true;
382                         render_layer.use_hair = true;
383                         render_layer.use_surfaces = true;
384                         render_layer.use_viewport_visibility = true;
385                         render_layer.samples = 0;
386                         render_layer.bound_samples = false;
387                         return;
388                 }
389         }
390
391         /* render layer */
392         BL::RenderSettings r = b_scene.render();
393         BL::RenderSettings::layers_iterator b_rlay;
394         int use_layer_samples = get_enum(cscene, "use_layer_samples");
395         bool first_layer = true;
396         uint layer_override = get_layer(b_engine.layer_override());
397         uint scene_layers = layer_override ? layer_override : get_layer(b_scene.layers());
398
399         for(r.layers.begin(b_rlay); b_rlay != r.layers.end(); ++b_rlay) {
400                 if((!layer && first_layer) || (layer && b_rlay->name() == layer)) {
401                         render_layer.name = b_rlay->name();
402
403                         render_layer.holdout_layer = get_layer(b_rlay->layers_zmask());
404                         render_layer.exclude_layer = get_layer(b_rlay->layers_exclude());
405
406                         render_layer.scene_layer = scene_layers & ~render_layer.exclude_layer;
407                         render_layer.scene_layer |= render_layer.exclude_layer & render_layer.holdout_layer;
408
409                         render_layer.layer = get_layer(b_rlay->layers());
410                         render_layer.layer |= render_layer.holdout_layer;
411
412                         render_layer.material_override = b_rlay->material_override();
413                         render_layer.use_background_shader = b_rlay->use_sky();
414                         render_layer.use_background_ao = b_rlay->use_ao();
415                         render_layer.use_surfaces = b_rlay->use_solid();
416                         render_layer.use_hair = b_rlay->use_strand();
417                         render_layer.use_viewport_visibility = false;
418
419                         render_layer.bound_samples = (use_layer_samples == 1);
420                         if(use_layer_samples != 2) {
421                                 int samples = b_rlay->samples();
422                                 if(get_boolean(cscene, "use_square_samples"))
423                                         render_layer.samples = samples * samples;
424                                 else
425                                         render_layer.samples = samples;
426                         }
427                 }
428
429                 first_layer = false;
430         }
431 }
432
433 /* Images */
434 void BlenderSync::sync_images()
435 {
436         /* Sync is a convention for this API, but currently it frees unused buffers. */
437
438         const bool is_interface_locked = b_engine.render() &&
439                                          b_engine.render().use_lock_interface();
440         if(is_interface_locked == false && BlenderSession::headless == false) {
441                 /* If interface is not locked, it's possible image is needed for
442                  * the display.
443                  */
444                 return;
445         }
446         /* Free buffers used by images which are not needed for render. */
447         BL::BlendData::images_iterator b_image;
448         for(b_data.images.begin(b_image);
449             b_image != b_data.images.end();
450             ++b_image)
451         {
452                 /* TODO(sergey): Consider making it an utility function to check
453                  * whether image is considered builtin.
454                  */
455                 const bool is_builtin = b_image->packed_file() ||
456                                         b_image->source() == BL::Image::source_GENERATED ||
457                                         b_image->source() == BL::Image::source_MOVIE ||
458                                         b_engine.is_preview();
459                 if(is_builtin == false) {
460                         b_image->buffers_free();
461                 }
462                 /* TODO(sergey): Free builtin images not used by any shader. */
463         }
464 }
465
466 /* Scene Parameters */
467
468 SceneParams BlenderSync::get_scene_params(BL::Scene& b_scene,
469                                           bool background,
470                                           bool is_cpu)
471 {
472         BL::RenderSettings r = b_scene.render();
473         SceneParams params;
474         PointerRNA cscene = RNA_pointer_get(&b_scene.ptr, "cycles");
475         const bool shadingsystem = RNA_boolean_get(&cscene, "shading_system");
476
477         if(shadingsystem == 0)
478                 params.shadingsystem = SHADINGSYSTEM_SVM;
479         else if(shadingsystem == 1)
480                 params.shadingsystem = SHADINGSYSTEM_OSL;
481         
482         if(background)
483                 params.bvh_type = SceneParams::BVH_STATIC;
484         else
485                 params.bvh_type = (SceneParams::BVHType)get_enum(
486                         cscene,
487                         "debug_bvh_type",
488                         SceneParams::BVH_NUM_TYPES,
489                         SceneParams::BVH_STATIC);
490
491         params.use_bvh_spatial_split = RNA_boolean_get(&cscene, "debug_use_spatial_splits");
492         params.use_bvh_unaligned_nodes = RNA_boolean_get(&cscene, "debug_use_hair_bvh");
493
494         if(background && params.shadingsystem != SHADINGSYSTEM_OSL)
495                 params.persistent_data = r.use_persistent_data();
496         else
497                 params.persistent_data = false;
498
499         int texture_limit;
500         if(background) {
501                 texture_limit = RNA_enum_get(&cscene, "texture_limit_render");
502         }
503         else {
504                 texture_limit = RNA_enum_get(&cscene, "texture_limit");
505         }
506         if(texture_limit > 0 && b_scene.render().use_simplify()) {
507                 params.texture_limit = 1 << (texture_limit + 6);
508         }
509         else {
510                 params.texture_limit = 0;
511         }
512
513 #if !(defined(__GNUC__) && (defined(i386) || defined(_M_IX86)))
514         if(is_cpu) {
515                 params.use_qbvh = DebugFlags().cpu.qbvh && system_cpu_support_sse2();
516         }
517         else
518 #endif
519         {
520                 params.use_qbvh = false;
521         }
522
523         return params;
524 }
525
526 /* Session Parameters */
527
528 bool BlenderSync::get_session_pause(BL::Scene& b_scene, bool background)
529 {
530         PointerRNA cscene = RNA_pointer_get(&b_scene.ptr, "cycles");
531         return (background)? false: get_boolean(cscene, "preview_pause");
532 }
533
534 SessionParams BlenderSync::get_session_params(BL::RenderEngine& b_engine,
535                                               BL::UserPreferences& b_userpref,
536                                               BL::Scene& b_scene,
537                                               bool background)
538 {
539         SessionParams params;
540         PointerRNA cscene = RNA_pointer_get(&b_scene.ptr, "cycles");
541
542         /* feature set */
543         params.experimental = (get_enum(cscene, "feature_set") != 0);
544
545         /* device type */
546         vector<DeviceInfo>& devices = Device::available_devices();
547         
548         /* device default CPU */
549         foreach(DeviceInfo& device, devices) {
550                 if(device.type == DEVICE_CPU) {
551                         params.device = device;
552                         break;
553                 }
554         }
555
556         if(get_enum(cscene, "device") == 2) {
557                 /* find network device */
558                 foreach(DeviceInfo& info, devices)
559                         if(info.type == DEVICE_NETWORK)
560                                 params.device = info;
561         }
562         else if(get_enum(cscene, "device") == 1) {
563                 PointerRNA b_preferences;
564
565                 BL::UserPreferences::addons_iterator b_addon_iter;
566                 for(b_userpref.addons.begin(b_addon_iter); b_addon_iter != b_userpref.addons.end(); ++b_addon_iter) {
567                         if(b_addon_iter->module() == "cycles") {
568                                 b_preferences = b_addon_iter->preferences().ptr;
569                                 break;
570                         }
571                 }
572
573                 int compute_device = get_enum(b_preferences, "compute_device_type");
574
575                 if(compute_device != 0) {
576                         vector<DeviceInfo> used_devices;
577                         RNA_BEGIN(&b_preferences, device, "devices") {
578                                 if(get_enum(device, "type") == compute_device && get_boolean(device, "use")) {
579                                         string id = get_string(device, "id");
580                                         foreach(DeviceInfo& info, devices) {
581                                                 if(info.id == id) {
582                                                         used_devices.push_back(info);
583                                                         break;
584                                                 }
585                                         }
586                                 }
587                         } RNA_END
588
589                         if(used_devices.size() == 1) {
590                                 params.device = used_devices[0];
591                         }
592                         else if(used_devices.size() > 1) {
593                                 params.device = Device::get_multi_device(used_devices);
594                         }
595                         /* Else keep using the CPU device that was set before. */
596                 }
597         }
598
599         /* Background */
600         params.background = background;
601
602         /* samples */
603         int samples = get_int(cscene, "samples");
604         int aa_samples = get_int(cscene, "aa_samples");
605         int preview_samples = get_int(cscene, "preview_samples");
606         int preview_aa_samples = get_int(cscene, "preview_aa_samples");
607         
608         if(get_boolean(cscene, "use_square_samples")) {
609                 aa_samples = aa_samples * aa_samples;
610                 preview_aa_samples = preview_aa_samples * preview_aa_samples;
611
612                 samples = samples * samples;
613                 preview_samples = preview_samples * preview_samples;
614         }
615
616         if(get_enum(cscene, "progressive") == 0) {
617                 if(background) {
618                         params.samples = aa_samples;
619                 }
620                 else {
621                         params.samples = preview_aa_samples;
622                         if(params.samples == 0)
623                                 params.samples = INT_MAX;
624                 }
625         }
626         else {
627                 if(background) {
628                         params.samples = samples;
629                 }
630                 else {
631                         params.samples = preview_samples;
632                         if(params.samples == 0)
633                                 params.samples = INT_MAX;
634                 }
635         }
636
637         /* tiles */
638         if(params.device.type != DEVICE_CPU && !background) {
639                 /* currently GPU could be much slower than CPU when using tiles,
640                  * still need to be investigated, but meanwhile make it possible
641                  * to work in viewport smoothly
642                  */
643                 int debug_tile_size = get_int(cscene, "debug_tile_size");
644
645                 params.tile_size = make_int2(debug_tile_size, debug_tile_size);
646         }
647         else {
648                 int tile_x = b_engine.tile_x();
649                 int tile_y = b_engine.tile_y();
650
651                 params.tile_size = make_int2(tile_x, tile_y);
652         }
653
654         if((BlenderSession::headless == false) && background) {
655                 params.tile_order = (TileOrder)get_enum(cscene, "tile_order");
656         }
657         else {
658                 params.tile_order = TILE_BOTTOM_TO_TOP;
659         }
660
661         params.start_resolution = get_int(cscene, "preview_start_resolution");
662
663         /* other parameters */
664         if(b_scene.render().threads_mode() == BL::RenderSettings::threads_mode_FIXED)
665                 params.threads = b_scene.render().threads();
666         else
667                 params.threads = 0;
668
669         params.cancel_timeout = (double)get_float(cscene, "debug_cancel_timeout");
670         params.reset_timeout = (double)get_float(cscene, "debug_reset_timeout");
671         params.text_timeout = (double)get_float(cscene, "debug_text_timeout");
672
673         params.progressive_refine = get_boolean(cscene, "use_progressive_refine");
674
675         if(background) {
676                 if(params.progressive_refine)
677                         params.progressive = true;
678                 else
679                         params.progressive = false;
680
681                 params.start_resolution = INT_MAX;
682         }
683         else
684                 params.progressive = true;
685
686         /* shading system - scene level needs full refresh */
687         const bool shadingsystem = RNA_boolean_get(&cscene, "shading_system");
688
689         if(shadingsystem == 0)
690                 params.shadingsystem = SHADINGSYSTEM_SVM;
691         else if(shadingsystem == 1)
692                 params.shadingsystem = SHADINGSYSTEM_OSL;
693         
694         /* color managagement */
695 #ifdef GLEW_MX
696         /* When using GLEW MX we need to check whether we've got an OpenGL
697          * context for current window. This is because command line rendering
698          * doesn't have OpenGL context actually.
699          */
700         if(glewGetContext() != NULL)
701 #endif
702         {
703                 params.display_buffer_linear = GLEW_ARB_half_float_pixel &&
704                                                b_engine.support_display_space_shader(b_scene);
705         }
706
707         if(b_engine.is_preview()) {
708                 /* For preview rendering we're using same timeout as
709                  * blender's job update.
710                  */
711                 params.progressive_update_timeout = 0.1;
712         }
713
714         return params;
715 }
716
717 CCL_NAMESPACE_END
718