OpenGL: manage built-in shaders better
[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(), get_int(cscene, "seed"));
252
253         integrator->sampling_pattern = (SamplingPattern)get_enum(
254                 cscene,
255                 "sampling_pattern",
256                 SAMPLING_NUM_PATTERNS,
257                 SAMPLING_PATTERN_SOBOL);
258
259         integrator->sample_clamp_direct = get_float(cscene, "sample_clamp_direct");
260         integrator->sample_clamp_indirect = get_float(cscene, "sample_clamp_indirect");
261 #ifdef __CAMERA_MOTION__
262         if(!preview) {
263                 if(integrator->motion_blur != r.use_motion_blur()) {
264                         scene->object_manager->tag_update(scene);
265                         scene->camera->tag_update();
266                 }
267
268                 integrator->motion_blur = r.use_motion_blur();
269         }
270 #endif
271
272         integrator->method = (Integrator::Method)get_enum(cscene,
273                                                           "progressive",
274                                                           Integrator::NUM_METHODS,
275                                                           Integrator::PATH);
276
277         integrator->sample_all_lights_direct = get_boolean(cscene, "sample_all_lights_direct");
278         integrator->sample_all_lights_indirect = get_boolean(cscene, "sample_all_lights_indirect");
279         integrator->light_sampling_threshold = get_float(cscene, "light_sampling_threshold");
280
281         int diffuse_samples = get_int(cscene, "diffuse_samples");
282         int glossy_samples = get_int(cscene, "glossy_samples");
283         int transmission_samples = get_int(cscene, "transmission_samples");
284         int ao_samples = get_int(cscene, "ao_samples");
285         int mesh_light_samples = get_int(cscene, "mesh_light_samples");
286         int subsurface_samples = get_int(cscene, "subsurface_samples");
287         int volume_samples = get_int(cscene, "volume_samples");
288
289         if(get_boolean(cscene, "use_square_samples")) {
290                 integrator->diffuse_samples = diffuse_samples * diffuse_samples;
291                 integrator->glossy_samples = glossy_samples * glossy_samples;
292                 integrator->transmission_samples = transmission_samples * transmission_samples;
293                 integrator->ao_samples = ao_samples * ao_samples;
294                 integrator->mesh_light_samples = mesh_light_samples * mesh_light_samples;
295                 integrator->subsurface_samples = subsurface_samples * subsurface_samples;
296                 integrator->volume_samples = volume_samples * volume_samples;
297         } 
298         else {
299                 integrator->diffuse_samples = diffuse_samples;
300                 integrator->glossy_samples = glossy_samples;
301                 integrator->transmission_samples = transmission_samples;
302                 integrator->ao_samples = ao_samples;
303                 integrator->mesh_light_samples = mesh_light_samples;
304                 integrator->subsurface_samples = subsurface_samples;
305                 integrator->volume_samples = volume_samples;
306         }
307
308         if(integrator->modified(previntegrator))
309                 integrator->tag_update(scene);
310 }
311
312 /* Film */
313
314 void BlenderSync::sync_film()
315 {
316         PointerRNA cscene = RNA_pointer_get(&b_scene.ptr, "cycles");
317
318         Film *film = scene->film;
319         Film prevfilm = *film;
320         
321         film->exposure = get_float(cscene, "film_exposure");
322         film->filter_type = (FilterType)get_enum(cscene,
323                                                  "pixel_filter_type",
324                                                  FILTER_NUM_TYPES,
325                                                  FILTER_BLACKMAN_HARRIS);
326         film->filter_width = (film->filter_type == FILTER_BOX)? 1.0f: get_float(cscene, "filter_width");
327
328         if(b_scene.world()) {
329                 BL::WorldMistSettings b_mist = b_scene.world().mist_settings();
330
331                 film->mist_start = b_mist.start();
332                 film->mist_depth = b_mist.depth();
333
334                 switch(b_mist.falloff()) {
335                         case BL::WorldMistSettings::falloff_QUADRATIC:
336                                 film->mist_falloff = 2.0f;
337                                 break;
338                         case BL::WorldMistSettings::falloff_LINEAR:
339                                 film->mist_falloff = 1.0f;
340                                 break;
341                         case BL::WorldMistSettings::falloff_INVERSE_QUADRATIC:
342                                 film->mist_falloff = 0.5f;
343                                 break;
344                 }
345         }
346
347         if(film->modified(prevfilm))
348                 film->tag_update(scene);
349 }
350
351 /* Render Layer */
352
353 void BlenderSync::sync_render_layers(BL::SpaceView3D& b_v3d, const char *layer)
354 {
355         PointerRNA cscene = RNA_pointer_get(&b_scene.ptr, "cycles");
356         string layername;
357
358         /* 3d view */
359         if(b_v3d) {
360                 if(RNA_boolean_get(&cscene, "preview_active_layer")) {
361                         BL::RenderLayers layers(b_scene.render().ptr);
362                         layername = layers.active().name();
363                         layer = layername.c_str();
364                 }
365                 else {
366                         render_layer.scene_layer = get_layer(b_v3d.layers(), b_v3d.layers_local_view());
367                         render_layer.layer = render_layer.scene_layer;
368                         render_layer.exclude_layer = 0;
369                         render_layer.holdout_layer = 0;
370                         render_layer.material_override = PointerRNA_NULL;
371                         render_layer.use_background_shader = true;
372                         render_layer.use_background_ao = true;
373                         render_layer.use_hair = true;
374                         render_layer.use_surfaces = true;
375                         render_layer.use_viewport_visibility = true;
376                         render_layer.samples = 0;
377                         render_layer.bound_samples = false;
378                         return;
379                 }
380         }
381
382         /* render layer */
383         BL::RenderSettings r = b_scene.render();
384         BL::RenderSettings::layers_iterator b_rlay;
385         int use_layer_samples = get_enum(cscene, "use_layer_samples");
386         bool first_layer = true;
387         uint layer_override = get_layer(b_engine.layer_override());
388         uint scene_layers = layer_override ? layer_override : get_layer(b_scene.layers());
389
390         for(r.layers.begin(b_rlay); b_rlay != r.layers.end(); ++b_rlay) {
391                 if((!layer && first_layer) || (layer && b_rlay->name() == layer)) {
392                         render_layer.name = b_rlay->name();
393
394                         render_layer.holdout_layer = get_layer(b_rlay->layers_zmask());
395                         render_layer.exclude_layer = get_layer(b_rlay->layers_exclude());
396
397                         render_layer.scene_layer = scene_layers & ~render_layer.exclude_layer;
398                         render_layer.scene_layer |= render_layer.exclude_layer & render_layer.holdout_layer;
399
400                         render_layer.layer = get_layer(b_rlay->layers());
401                         render_layer.layer |= render_layer.holdout_layer;
402
403                         render_layer.material_override = b_rlay->material_override();
404                         render_layer.use_background_shader = b_rlay->use_sky();
405                         render_layer.use_background_ao = b_rlay->use_ao();
406                         render_layer.use_surfaces = b_rlay->use_solid();
407                         render_layer.use_hair = b_rlay->use_strand();
408                         render_layer.use_viewport_visibility = false;
409
410                         render_layer.bound_samples = (use_layer_samples == 1);
411                         if(use_layer_samples != 2) {
412                                 int samples = b_rlay->samples();
413                                 if(get_boolean(cscene, "use_square_samples"))
414                                         render_layer.samples = samples * samples;
415                                 else
416                                         render_layer.samples = samples;
417                         }
418                 }
419
420                 first_layer = false;
421         }
422 }
423
424 /* Images */
425 void BlenderSync::sync_images()
426 {
427         /* Sync is a convention for this API, but currently it frees unused buffers. */
428
429         const bool is_interface_locked = b_engine.render() &&
430                                          b_engine.render().use_lock_interface();
431         if(is_interface_locked == false && BlenderSession::headless == false) {
432                 /* If interface is not locked, it's possible image is needed for
433                  * the display.
434                  */
435                 return;
436         }
437         /* Free buffers used by images which are not needed for render. */
438         BL::BlendData::images_iterator b_image;
439         for(b_data.images.begin(b_image);
440             b_image != b_data.images.end();
441             ++b_image)
442         {
443                 /* TODO(sergey): Consider making it an utility function to check
444                  * whether image is considered builtin.
445                  */
446                 const bool is_builtin = b_image->packed_file() ||
447                                         b_image->source() == BL::Image::source_GENERATED ||
448                                         b_image->source() == BL::Image::source_MOVIE ||
449                                         b_engine.is_preview();
450                 if(is_builtin == false) {
451                         b_image->buffers_free();
452                 }
453                 /* TODO(sergey): Free builtin images not used by any shader. */
454         }
455 }
456
457 /* Scene Parameters */
458
459 SceneParams BlenderSync::get_scene_params(BL::Scene& b_scene,
460                                           bool background,
461                                           bool is_cpu)
462 {
463         BL::RenderSettings r = b_scene.render();
464         SceneParams params;
465         PointerRNA cscene = RNA_pointer_get(&b_scene.ptr, "cycles");
466         const bool shadingsystem = RNA_boolean_get(&cscene, "shading_system");
467
468         if(shadingsystem == 0)
469                 params.shadingsystem = SHADINGSYSTEM_SVM;
470         else if(shadingsystem == 1)
471                 params.shadingsystem = SHADINGSYSTEM_OSL;
472         
473         if(background)
474                 params.bvh_type = SceneParams::BVH_STATIC;
475         else
476                 params.bvh_type = (SceneParams::BVHType)get_enum(
477                         cscene,
478                         "debug_bvh_type",
479                         SceneParams::BVH_NUM_TYPES,
480                         SceneParams::BVH_STATIC);
481
482         params.use_bvh_spatial_split = RNA_boolean_get(&cscene, "debug_use_spatial_splits");
483         params.use_bvh_unaligned_nodes = RNA_boolean_get(&cscene, "debug_use_hair_bvh");
484
485         if(background && params.shadingsystem != SHADINGSYSTEM_OSL)
486                 params.persistent_data = r.use_persistent_data();
487         else
488                 params.persistent_data = false;
489
490 #if !(defined(__GNUC__) && (defined(i386) || defined(_M_IX86)))
491         if(is_cpu) {
492                 params.use_qbvh = DebugFlags().cpu.qbvh && system_cpu_support_sse2();
493         }
494         else
495 #endif
496         {
497                 params.use_qbvh = false;
498         }
499
500         return params;
501 }
502
503 /* Session Parameters */
504
505 bool BlenderSync::get_session_pause(BL::Scene& b_scene, bool background)
506 {
507         PointerRNA cscene = RNA_pointer_get(&b_scene.ptr, "cycles");
508         return (background)? false: get_boolean(cscene, "preview_pause");
509 }
510
511 SessionParams BlenderSync::get_session_params(BL::RenderEngine& b_engine,
512                                               BL::UserPreferences& b_userpref,
513                                               BL::Scene& b_scene,
514                                               bool background)
515 {
516         SessionParams params;
517         PointerRNA cscene = RNA_pointer_get(&b_scene.ptr, "cycles");
518
519         /* feature set */
520         params.experimental = (get_enum(cscene, "feature_set") != 0);
521
522         /* device type */
523         vector<DeviceInfo>& devices = Device::available_devices();
524         
525         /* device default CPU */
526         foreach(DeviceInfo& device, devices) {
527                 if(device.type == DEVICE_CPU) {
528                         params.device = device;
529                         break;
530                 }
531         }
532
533         if(get_enum(cscene, "device") == 2) {
534                 /* find network device */
535                 foreach(DeviceInfo& info, devices)
536                         if(info.type == DEVICE_NETWORK)
537                                 params.device = info;
538         }
539         else if(get_enum(cscene, "device") == 1) {
540                 PointerRNA b_preferences;
541
542                 BL::UserPreferences::addons_iterator b_addon_iter;
543                 for(b_userpref.addons.begin(b_addon_iter); b_addon_iter != b_userpref.addons.end(); ++b_addon_iter) {
544                         if(b_addon_iter->module() == "cycles") {
545                                 b_preferences = b_addon_iter->preferences().ptr;
546                                 break;
547                         }
548                 }
549
550                 int compute_device = get_enum(b_preferences, "compute_device_type");
551
552                 if(compute_device != 0) {
553                         vector<DeviceInfo> used_devices;
554                         RNA_BEGIN(&b_preferences, device, "devices") {
555                                 if(get_enum(device, "type") == compute_device && get_boolean(device, "use")) {
556                                         string id = get_string(device, "id");
557                                         foreach(DeviceInfo& info, devices) {
558                                                 if(info.id == id) {
559                                                         used_devices.push_back(info);
560                                                         break;
561                                                 }
562                                         }
563                                 }
564                         } RNA_END
565
566                         if(used_devices.size() == 1) {
567                                 params.device = used_devices[0];
568                         }
569                         else if(used_devices.size() > 1) {
570                                 params.device = Device::get_multi_device(used_devices);
571                         }
572                         /* Else keep using the CPU device that was set before. */
573                 }
574         }
575
576         /* Background */
577         params.background = background;
578
579         /* samples */
580         int samples = get_int(cscene, "samples");
581         int aa_samples = get_int(cscene, "aa_samples");
582         int preview_samples = get_int(cscene, "preview_samples");
583         int preview_aa_samples = get_int(cscene, "preview_aa_samples");
584         
585         if(get_boolean(cscene, "use_square_samples")) {
586                 aa_samples = aa_samples * aa_samples;
587                 preview_aa_samples = preview_aa_samples * preview_aa_samples;
588
589                 samples = samples * samples;
590                 preview_samples = preview_samples * preview_samples;
591         }
592
593         if(get_enum(cscene, "progressive") == 0) {
594                 if(background) {
595                         params.samples = aa_samples;
596                 }
597                 else {
598                         params.samples = preview_aa_samples;
599                         if(params.samples == 0)
600                                 params.samples = INT_MAX;
601                 }
602         }
603         else {
604                 if(background) {
605                         params.samples = samples;
606                 }
607                 else {
608                         params.samples = preview_samples;
609                         if(params.samples == 0)
610                                 params.samples = INT_MAX;
611                 }
612         }
613
614         /* tiles */
615         if(params.device.type != DEVICE_CPU && !background) {
616                 /* currently GPU could be much slower than CPU when using tiles,
617                  * still need to be investigated, but meanwhile make it possible
618                  * to work in viewport smoothly
619                  */
620                 int debug_tile_size = get_int(cscene, "debug_tile_size");
621
622                 params.tile_size = make_int2(debug_tile_size, debug_tile_size);
623         }
624         else {
625                 int tile_x = b_engine.tile_x();
626                 int tile_y = b_engine.tile_y();
627
628                 params.tile_size = make_int2(tile_x, tile_y);
629         }
630
631         if((BlenderSession::headless == false) && background) {
632                 params.tile_order = (TileOrder)get_enum(cscene, "tile_order");
633         }
634         else {
635                 params.tile_order = TILE_BOTTOM_TO_TOP;
636         }
637
638         params.start_resolution = get_int(cscene, "preview_start_resolution");
639
640         /* other parameters */
641         if(b_scene.render().threads_mode() == BL::RenderSettings::threads_mode_FIXED)
642                 params.threads = b_scene.render().threads();
643         else
644                 params.threads = 0;
645
646         params.cancel_timeout = (double)get_float(cscene, "debug_cancel_timeout");
647         params.reset_timeout = (double)get_float(cscene, "debug_reset_timeout");
648         params.text_timeout = (double)get_float(cscene, "debug_text_timeout");
649
650         params.progressive_refine = get_boolean(cscene, "use_progressive_refine");
651
652         if(background) {
653                 if(params.progressive_refine)
654                         params.progressive = true;
655                 else
656                         params.progressive = false;
657
658                 params.start_resolution = INT_MAX;
659         }
660         else
661                 params.progressive = true;
662
663         /* shading system - scene level needs full refresh */
664         const bool shadingsystem = RNA_boolean_get(&cscene, "shading_system");
665
666         if(shadingsystem == 0)
667                 params.shadingsystem = SHADINGSYSTEM_SVM;
668         else if(shadingsystem == 1)
669                 params.shadingsystem = SHADINGSYSTEM_OSL;
670         
671         /* color managagement */
672 #ifdef GLEW_MX
673         /* When using GLEW MX we need to check whether we've got an OpenGL
674          * context for current window. This is because command line rendering
675          * doesn't have OpenGL context actually.
676          */
677         if(glewGetContext() != NULL)
678 #endif
679         {
680                 params.display_buffer_linear = GLEW_ARB_half_float_pixel &&
681                                                b_engine.support_display_space_shader(b_scene);
682         }
683
684         if(b_engine.is_preview()) {
685                 /* For preview rendering we're using same timeout as
686                  * blender's job update.
687                  */
688                 params.progressive_update_timeout = 0.1;
689         }
690
691         return params;
692 }
693
694 CCL_NAMESPACE_END
695