Cycles: Avoid crash when accessing non-existing volume attribute
[blender-staging.git] / intern / cycles / blender / blender_session.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 <stdlib.h>
18
19 #include "background.h"
20 #include "buffers.h"
21 #include "camera.h"
22 #include "device.h"
23 #include "integrator.h"
24 #include "film.h"
25 #include "light.h"
26 #include "mesh.h"
27 #include "object.h"
28 #include "scene.h"
29 #include "session.h"
30 #include "shader.h"
31
32 #include "util_color.h"
33 #include "util_foreach.h"
34 #include "util_function.h"
35 #include "util_logging.h"
36 #include "util_progress.h"
37 #include "util_time.h"
38
39 #include "blender_sync.h"
40 #include "blender_session.h"
41 #include "blender_util.h"
42
43 CCL_NAMESPACE_BEGIN
44
45 bool BlenderSession::headless = false;
46 int BlenderSession::num_resumable_chunks = 0;
47 int BlenderSession::current_resumable_chunk = 0;
48
49 BlenderSession::BlenderSession(BL::RenderEngine& b_engine,
50                                BL::UserPreferences& b_userpref,
51                                BL::BlendData& b_data,
52                                BL::Scene& b_scene)
53 : b_engine(b_engine),
54   b_userpref(b_userpref),
55   b_data(b_data),
56   b_render(b_engine.render()),
57   b_scene(b_scene),
58   b_v3d(PointerRNA_NULL),
59   b_rv3d(PointerRNA_NULL),
60   python_thread_state(NULL)
61 {
62         /* offline render */
63
64         width = render_resolution_x(b_render);
65         height = render_resolution_y(b_render);
66
67         background = true;
68         last_redraw_time = 0.0;
69         start_resize_time = 0.0;
70 }
71
72 BlenderSession::BlenderSession(BL::RenderEngine& b_engine,
73                                BL::UserPreferences& b_userpref,
74                                BL::BlendData& b_data,
75                                BL::Scene& b_scene,
76                                BL::SpaceView3D& b_v3d,
77                                BL::RegionView3D& b_rv3d,
78                                int width, int height)
79 : b_engine(b_engine),
80   b_userpref(b_userpref),
81   b_data(b_data),
82   b_render(b_scene.render()),
83   b_scene(b_scene),
84   b_v3d(b_v3d),
85   b_rv3d(b_rv3d),
86   width(width),
87   height(height),
88   python_thread_state(NULL)
89 {
90         /* 3d view render */
91
92         background = false;
93         last_redraw_time = 0.0;
94         start_resize_time = 0.0;
95 }
96
97 BlenderSession::~BlenderSession()
98 {
99         free_session();
100 }
101
102 void BlenderSession::create()
103 {
104         create_session();
105
106         if(b_v3d)
107                 session->start();
108 }
109
110 void BlenderSession::create_session()
111 {
112         SessionParams session_params = BlenderSync::get_session_params(b_engine, b_userpref, b_scene, background);
113         bool is_cpu = session_params.device.type == DEVICE_CPU;
114         SceneParams scene_params = BlenderSync::get_scene_params(b_scene, background, is_cpu);
115         bool session_pause = BlenderSync::get_session_pause(b_scene, background);
116
117         /* reset status/progress */
118         last_status = "";
119         last_error = "";
120         last_progress = -1.0f;
121         start_resize_time = 0.0;
122
123         /* create scene */
124         scene = new Scene(scene_params, session_params.device);
125
126         /* setup callbacks for builtin image support */
127         scene->image_manager->builtin_image_info_cb = function_bind(&BlenderSession::builtin_image_info, this, _1, _2, _3, _4, _5, _6, _7);
128         scene->image_manager->builtin_image_pixels_cb = function_bind(&BlenderSession::builtin_image_pixels, this, _1, _2, _3);
129         scene->image_manager->builtin_image_float_pixels_cb = function_bind(&BlenderSession::builtin_image_float_pixels, this, _1, _2, _3);
130
131         /* create session */
132         session = new Session(session_params);
133         session->scene = scene;
134         session->progress.set_update_callback(function_bind(&BlenderSession::tag_redraw, this));
135         session->progress.set_cancel_callback(function_bind(&BlenderSession::test_cancel, this));
136         session->set_pause(session_pause);
137
138         /* create sync */
139         sync = new BlenderSync(b_engine, b_data, b_scene, scene, !background, session->progress, is_cpu);
140         BL::Object b_camera_override(b_engine.camera_override());
141         if(b_v3d) {
142                 if(session_pause == false) {
143                         /* full data sync */
144                         sync->sync_view(b_v3d, b_rv3d, width, height);
145                         sync->sync_data(b_render,
146                                         b_v3d,
147                                         b_camera_override,
148                                         width, height,
149                                         &python_thread_state,
150                                         b_rlay_name.c_str());
151                 }
152         }
153         else {
154                 /* for final render we will do full data sync per render layer, only
155                  * do some basic syncing here, no objects or materials for speed */
156                 sync->sync_render_layers(b_v3d, NULL);
157                 sync->sync_integrator();
158                 sync->sync_camera(b_render, b_camera_override, width, height, "");
159         }
160
161         /* set buffer parameters */
162         BufferParams buffer_params = BlenderSync::get_buffer_params(b_render, b_v3d, b_rv3d, scene->camera, width, height);
163         session->reset(buffer_params, session_params.samples);
164
165         b_engine.use_highlight_tiles(session_params.progressive_refine == false);
166
167         update_resumable_tile_manager(session_params.samples);
168 }
169
170 void BlenderSession::reset_session(BL::BlendData& b_data_, BL::Scene& b_scene_)
171 {
172         b_data = b_data_;
173         b_render = b_engine.render();
174         b_scene = b_scene_;
175
176         SessionParams session_params = BlenderSync::get_session_params(b_engine, b_userpref, b_scene, background);
177         const bool is_cpu = session_params.device.type == DEVICE_CPU;
178         SceneParams scene_params = BlenderSync::get_scene_params(b_scene, background, is_cpu);
179
180         width = render_resolution_x(b_render);
181         height = render_resolution_y(b_render);
182
183         if(scene->params.modified(scene_params) ||
184            session->params.modified(session_params) ||
185            !scene_params.persistent_data)
186         {
187                 /* if scene or session parameters changed, it's easier to simply re-create
188                  * them rather than trying to distinguish which settings need to be updated
189                  */
190
191                 delete session;
192
193                 create_session();
194
195                 return;
196         }
197
198         session->progress.reset();
199         scene->reset();
200
201         session->tile_manager.set_tile_order(session_params.tile_order);
202
203         /* peak memory usage should show current render peak, not peak for all renders
204          * made by this render session
205          */
206         session->stats.mem_peak = session->stats.mem_used;
207
208         /* sync object should be re-created */
209         sync = new BlenderSync(b_engine, b_data, b_scene, scene, !background, session->progress, is_cpu);
210
211         /* for final render we will do full data sync per render layer, only
212          * do some basic syncing here, no objects or materials for speed */
213         BL::Object b_camera_override(b_engine.camera_override());
214         sync->sync_render_layers(b_v3d, NULL);
215         sync->sync_integrator();
216         sync->sync_camera(b_render, b_camera_override, width, height, "");
217
218         BL::SpaceView3D b_null_space_view3d(PointerRNA_NULL);
219         BL::RegionView3D b_null_region_view3d(PointerRNA_NULL);
220         BufferParams buffer_params = BlenderSync::get_buffer_params(b_render,
221                                                                     b_null_space_view3d,
222                                                                     b_null_region_view3d,
223                                                                     scene->camera,
224                                                                     width, height);
225         session->reset(buffer_params, session_params.samples);
226
227         b_engine.use_highlight_tiles(session_params.progressive_refine == false);
228
229         /* reset time */
230         start_resize_time = 0.0;
231 }
232
233 void BlenderSession::free_session()
234 {
235         if(sync)
236                 delete sync;
237
238         delete session;
239 }
240
241 static PassType get_pass_type(BL::RenderPass& b_pass)
242 {
243         switch(b_pass.type()) {
244                 case BL::RenderPass::type_COMBINED:
245                         return PASS_COMBINED;
246
247                 case BL::RenderPass::type_Z:
248                         return PASS_DEPTH;
249                 case BL::RenderPass::type_MIST:
250                         return PASS_MIST;
251                 case BL::RenderPass::type_NORMAL:
252                         return PASS_NORMAL;
253                 case BL::RenderPass::type_OBJECT_INDEX:
254                         return PASS_OBJECT_ID;
255                 case BL::RenderPass::type_UV:
256                         return PASS_UV;
257                 case BL::RenderPass::type_VECTOR:
258                         return PASS_MOTION;
259                 case BL::RenderPass::type_MATERIAL_INDEX:
260                         return PASS_MATERIAL_ID;
261
262                 case BL::RenderPass::type_DIFFUSE_DIRECT:
263                         return PASS_DIFFUSE_DIRECT;
264                 case BL::RenderPass::type_GLOSSY_DIRECT:
265                         return PASS_GLOSSY_DIRECT;
266                 case BL::RenderPass::type_TRANSMISSION_DIRECT:
267                         return PASS_TRANSMISSION_DIRECT;
268                 case BL::RenderPass::type_SUBSURFACE_DIRECT:
269                         return PASS_SUBSURFACE_DIRECT;
270
271                 case BL::RenderPass::type_DIFFUSE_INDIRECT:
272                         return PASS_DIFFUSE_INDIRECT;
273                 case BL::RenderPass::type_GLOSSY_INDIRECT:
274                         return PASS_GLOSSY_INDIRECT;
275                 case BL::RenderPass::type_TRANSMISSION_INDIRECT:
276                         return PASS_TRANSMISSION_INDIRECT;
277                 case BL::RenderPass::type_SUBSURFACE_INDIRECT:
278                         return PASS_SUBSURFACE_INDIRECT;
279
280                 case BL::RenderPass::type_DIFFUSE_COLOR:
281                         return PASS_DIFFUSE_COLOR;
282                 case BL::RenderPass::type_GLOSSY_COLOR:
283                         return PASS_GLOSSY_COLOR;
284                 case BL::RenderPass::type_TRANSMISSION_COLOR:
285                         return PASS_TRANSMISSION_COLOR;
286                 case BL::RenderPass::type_SUBSURFACE_COLOR:
287                         return PASS_SUBSURFACE_COLOR;
288
289                 case BL::RenderPass::type_EMIT:
290                         return PASS_EMISSION;
291                 case BL::RenderPass::type_ENVIRONMENT:
292                         return PASS_BACKGROUND;
293                 case BL::RenderPass::type_AO:
294                         return PASS_AO;
295                 case BL::RenderPass::type_SHADOW:
296                         return PASS_SHADOW;
297
298                 case BL::RenderPass::type_DIFFUSE:
299                 case BL::RenderPass::type_COLOR:
300                 case BL::RenderPass::type_REFRACTION:
301                 case BL::RenderPass::type_SPECULAR:
302                 case BL::RenderPass::type_REFLECTION:
303                         return PASS_NONE;
304 #ifdef WITH_CYCLES_DEBUG
305                 case BL::RenderPass::type_DEBUG:
306                 {
307                         if(b_pass.debug_type() == BL::RenderPass::debug_type_BVH_TRAVERSAL_STEPS)
308                                 return PASS_BVH_TRAVERSAL_STEPS;
309                         if(b_pass.debug_type() == BL::RenderPass::debug_type_BVH_TRAVERSED_INSTANCES)
310                                 return PASS_BVH_TRAVERSED_INSTANCES;
311                         if(b_pass.debug_type() == BL::RenderPass::debug_type_RAY_BOUNCES)
312                                 return PASS_RAY_BOUNCES;
313                         break;
314                 }
315 #endif
316         }
317         
318         return PASS_NONE;
319 }
320
321 static ShaderEvalType get_shader_type(const string& pass_type)
322 {
323         const char *shader_type = pass_type.c_str();
324
325         /* data passes */
326         if(strcmp(shader_type, "NORMAL")==0)
327                 return SHADER_EVAL_NORMAL;
328         else if(strcmp(shader_type, "UV")==0)
329                 return SHADER_EVAL_UV;
330         else if(strcmp(shader_type, "DIFFUSE_COLOR")==0)
331                 return SHADER_EVAL_DIFFUSE_COLOR;
332         else if(strcmp(shader_type, "GLOSSY_COLOR")==0)
333                 return SHADER_EVAL_GLOSSY_COLOR;
334         else if(strcmp(shader_type, "TRANSMISSION_COLOR")==0)
335                 return SHADER_EVAL_TRANSMISSION_COLOR;
336         else if(strcmp(shader_type, "SUBSURFACE_COLOR")==0)
337                 return SHADER_EVAL_SUBSURFACE_COLOR;
338         else if(strcmp(shader_type, "EMIT")==0)
339                 return SHADER_EVAL_EMISSION;
340
341         /* light passes */
342         else if(strcmp(shader_type, "AO")==0)
343                 return SHADER_EVAL_AO;
344         else if(strcmp(shader_type, "COMBINED")==0)
345                 return SHADER_EVAL_COMBINED;
346         else if(strcmp(shader_type, "SHADOW")==0)
347                 return SHADER_EVAL_SHADOW;
348         else if(strcmp(shader_type, "DIFFUSE")==0)
349                 return SHADER_EVAL_DIFFUSE;
350         else if(strcmp(shader_type, "GLOSSY")==0)
351                 return SHADER_EVAL_GLOSSY;
352         else if(strcmp(shader_type, "TRANSMISSION")==0)
353                 return SHADER_EVAL_TRANSMISSION;
354         else if(strcmp(shader_type, "SUBSURFACE")==0)
355                 return SHADER_EVAL_SUBSURFACE;
356
357         /* extra */
358         else if(strcmp(shader_type, "ENVIRONMENT")==0)
359                 return SHADER_EVAL_ENVIRONMENT;
360
361         else
362                 return SHADER_EVAL_BAKE;
363 }
364
365 static BL::RenderResult begin_render_result(BL::RenderEngine& b_engine,
366                                             int x, int y,
367                                             int w, int h,
368                                             const char *layername,
369                                             const char *viewname)
370 {
371         return b_engine.begin_result(x, y, w, h, layername, viewname);
372 }
373
374 static void end_render_result(BL::RenderEngine& b_engine,
375                               BL::RenderResult& b_rr,
376                               bool cancel,
377                               bool do_merge_results)
378 {
379         b_engine.end_result(b_rr, (int)cancel, (int)do_merge_results);
380 }
381
382 void BlenderSession::do_write_update_render_tile(RenderTile& rtile, bool do_update_only)
383 {
384         BufferParams& params = rtile.buffers->params;
385         int x = params.full_x - session->tile_manager.params.full_x;
386         int y = params.full_y - session->tile_manager.params.full_y;
387         int w = params.width;
388         int h = params.height;
389
390         /* get render result */
391         BL::RenderResult b_rr = begin_render_result(b_engine, x, y, w, h, b_rlay_name.c_str(), b_rview_name.c_str());
392
393         /* can happen if the intersected rectangle gives 0 width or height */
394         if(b_rr.ptr.data == NULL) {
395                 return;
396         }
397
398         BL::RenderResult::layers_iterator b_single_rlay;
399         b_rr.layers.begin(b_single_rlay);
400
401         /* layer will be missing if it was disabled in the UI */
402         if(b_single_rlay == b_rr.layers.end())
403                 return;
404
405         BL::RenderLayer b_rlay = *b_single_rlay;
406
407         if(do_update_only) {
408                 /* update only needed */
409
410                 if(rtile.sample != 0) {
411                         /* sample would be zero at initial tile update, which is only needed
412                          * to tag tile form blender side as IN PROGRESS for proper highlight
413                          * no buffers should be sent to blender yet
414                          */
415                         update_render_result(b_rr, b_rlay, rtile);
416                 }
417
418                 end_render_result(b_engine, b_rr, true, true);
419         }
420         else {
421                 /* write result */
422                 write_render_result(b_rr, b_rlay, rtile);
423                 end_render_result(b_engine, b_rr, false, true);
424         }
425 }
426
427 void BlenderSession::write_render_tile(RenderTile& rtile)
428 {
429         do_write_update_render_tile(rtile, false);
430 }
431
432 void BlenderSession::update_render_tile(RenderTile& rtile)
433 {
434         /* use final write for preview renders, otherwise render result wouldn't be
435          * be updated in blender side
436          * would need to be investigated a bit further, but for now shall be fine
437          */
438         if(!b_engine.is_preview())
439                 do_write_update_render_tile(rtile, true);
440         else
441                 do_write_update_render_tile(rtile, false);
442 }
443
444 void BlenderSession::render()
445 {
446         /* set callback to write out render results */
447         session->write_render_tile_cb = function_bind(&BlenderSession::write_render_tile, this, _1);
448         session->update_render_tile_cb = function_bind(&BlenderSession::update_render_tile, this, _1);
449
450         /* get buffer parameters */
451         SessionParams session_params = BlenderSync::get_session_params(b_engine, b_userpref, b_scene, background);
452         BufferParams buffer_params = BlenderSync::get_buffer_params(b_render, b_v3d, b_rv3d, scene->camera, width, height);
453
454         /* render each layer */
455         BL::RenderSettings r = b_scene.render();
456         BL::RenderSettings::layers_iterator b_layer_iter;
457         BL::RenderResult::views_iterator b_view_iter;
458         
459         for(r.layers.begin(b_layer_iter); b_layer_iter != r.layers.end(); ++b_layer_iter) {
460                 b_rlay_name = b_layer_iter->name();
461
462                 /* temporary render result to find needed passes and views */
463                 BL::RenderResult b_rr = begin_render_result(b_engine, 0, 0, 1, 1, b_rlay_name.c_str(), NULL);
464                 BL::RenderResult::layers_iterator b_single_rlay;
465                 b_rr.layers.begin(b_single_rlay);
466
467                 /* layer will be missing if it was disabled in the UI */
468                 if(b_single_rlay == b_rr.layers.end()) {
469                         end_render_result(b_engine, b_rr, true, false);
470                         continue;
471                 }
472
473                 BL::RenderLayer b_rlay = *b_single_rlay;
474
475                 /* add passes */
476                 vector<Pass> passes;
477                 Pass::add(PASS_COMBINED, passes);
478
479                 if(session_params.device.advanced_shading) {
480
481                         /* loop over passes */
482                         BL::RenderLayer::passes_iterator b_pass_iter;
483
484                         for(b_rlay.passes.begin(b_pass_iter); b_pass_iter != b_rlay.passes.end(); ++b_pass_iter) {
485                                 BL::RenderPass b_pass(*b_pass_iter);
486                                 PassType pass_type = get_pass_type(b_pass);
487
488                                 if(pass_type == PASS_MOTION && scene->integrator->motion_blur)
489                                         continue;
490                                 if(pass_type != PASS_NONE)
491                                         Pass::add(pass_type, passes);
492                         }
493                 }
494
495                 buffer_params.passes = passes;
496                 scene->film->pass_alpha_threshold = b_layer_iter->pass_alpha_threshold();
497                 scene->film->tag_passes_update(scene, passes);
498                 scene->film->tag_update(scene);
499                 scene->integrator->tag_update(scene);
500
501                 for(b_rr.views.begin(b_view_iter); b_view_iter != b_rr.views.end(); ++b_view_iter) {
502                         b_rview_name = b_view_iter->name();
503
504                         /* set the current view */
505                         b_engine.active_view_set(b_rview_name.c_str());
506
507                         /* update scene */
508                         BL::Object b_camera_override(b_engine.camera_override());
509                         sync->sync_camera(b_render, b_camera_override, width, height, b_rview_name.c_str());
510                         sync->sync_data(b_render,
511                                         b_v3d,
512                                         b_camera_override,
513                                         width, height,
514                                         &python_thread_state,
515                                         b_rlay_name.c_str());
516
517                         /* Update number of samples per layer. */
518                         int samples = sync->get_layer_samples();
519                         bool bound_samples = sync->get_layer_bound_samples();
520                         int effective_layer_samples;
521
522                         if(samples != 0 && (!bound_samples || (samples < session_params.samples)))
523                                 effective_layer_samples = samples;
524                         else
525                                 effective_layer_samples = session_params.samples;
526
527                         /* Update tile manager if we're doing resumable render. */
528                         update_resumable_tile_manager(effective_layer_samples);
529
530                         /* Update session itself. */
531                         session->reset(buffer_params, effective_layer_samples);
532
533                         /* render */
534                         session->start();
535                         session->wait();
536
537                         if(session->progress.get_cancel())
538                                 break;
539                 }
540
541                 /* free result without merging */
542                 end_render_result(b_engine, b_rr, true, false);
543
544                 if(session->progress.get_cancel())
545                         break;
546         }
547
548         double total_time, render_time;
549         session->progress.get_time(total_time, render_time);
550         VLOG(1) << "Total render time: " << total_time;
551         VLOG(1) << "Render time (without synchronization): " << render_time;
552
553         /* clear callback */
554         session->write_render_tile_cb = function_null;
555         session->update_render_tile_cb = function_null;
556
557         /* free all memory used (host and device), so we wouldn't leave render
558          * engine with extra memory allocated
559          */
560
561         session->device_free();
562
563         delete sync;
564         sync = NULL;
565 }
566
567 static void populate_bake_data(BakeData *data, const
568                                int object_id,
569                                BL::BakePixel& pixel_array,
570                                const int num_pixels)
571 {
572         BL::BakePixel bp = pixel_array;
573
574         int i;
575         for(i=0; i < num_pixels; i++) {
576                 if(bp.object_id() == object_id) {
577                         data->set(i, bp.primitive_id(), bp.uv(), bp.du_dx(), bp.du_dy(), bp.dv_dx(), bp.dv_dy());
578                 } else {
579                         data->set_null(i);
580                 }
581                 bp = bp.next();
582         }
583 }
584
585 static int bake_pass_filter_get(const int pass_filter)
586 {
587         int flag = BAKE_FILTER_NONE;
588
589         if((pass_filter & BL::BakeSettings::pass_filter_DIRECT) != 0)
590                 flag |= BAKE_FILTER_DIRECT;
591         if((pass_filter & BL::BakeSettings::pass_filter_INDIRECT) != 0)
592                 flag |= BAKE_FILTER_INDIRECT;
593         if((pass_filter & BL::BakeSettings::pass_filter_COLOR) != 0)
594                 flag |= BAKE_FILTER_COLOR;
595
596         if((pass_filter & BL::BakeSettings::pass_filter_DIFFUSE) != 0)
597                 flag |= BAKE_FILTER_DIFFUSE;
598         if((pass_filter & BL::BakeSettings::pass_filter_GLOSSY) != 0)
599                 flag |= BAKE_FILTER_GLOSSY;
600         if((pass_filter & BL::BakeSettings::pass_filter_TRANSMISSION) != 0)
601                 flag |= BAKE_FILTER_TRANSMISSION;
602         if((pass_filter & BL::BakeSettings::pass_filter_SUBSURFACE) != 0)
603                 flag |= BAKE_FILTER_SUBSURFACE;
604
605         if((pass_filter & BL::BakeSettings::pass_filter_EMIT) != 0)
606                 flag |= BAKE_FILTER_EMISSION;
607         if((pass_filter & BL::BakeSettings::pass_filter_AO) != 0)
608                 flag |= BAKE_FILTER_AO;
609
610         return flag;
611 }
612
613 void BlenderSession::bake(BL::Object& b_object,
614                           const string& pass_type,
615                           const int pass_filter,
616                           const int object_id,
617                           BL::BakePixel& pixel_array,
618                           const size_t num_pixels,
619                           const int /*depth*/,
620                           float result[])
621 {
622         ShaderEvalType shader_type = get_shader_type(pass_type);
623         size_t object_index = OBJECT_NONE;
624         int tri_offset = 0;
625
626         /* Set baking flag in advance, so kernel loading can check if we need
627          * any baking capabilities.
628          */
629         scene->bake_manager->set_baking(true);
630
631         /* ensure kernels are loaded before we do any scene updates */
632         session->load_kernels();
633
634         if(session->progress.get_cancel())
635                 return;
636
637         if(shader_type == SHADER_EVAL_UV) {
638                 /* force UV to be available */
639                 Pass::add(PASS_UV, scene->film->passes);
640         }
641
642         int bake_pass_filter = bake_pass_filter_get(pass_filter);
643         bake_pass_filter = BakeManager::shader_type_to_pass_filter(shader_type, bake_pass_filter);
644
645         /* force use_light_pass to be true if we bake more than just colors */
646         if (bake_pass_filter & ~BAKE_FILTER_COLOR) {
647                 Pass::add(PASS_LIGHT, scene->film->passes);
648         }
649
650         /* create device and update scene */
651         scene->film->tag_update(scene);
652         scene->integrator->tag_update(scene);
653
654         /* update scene */
655         BL::Object b_camera_override(b_engine.camera_override());
656         sync->sync_camera(b_render, b_camera_override, width, height, "");
657         sync->sync_data(b_render,
658                         b_v3d,
659                         b_camera_override,
660                         width, height,
661                         &python_thread_state,
662                         b_rlay_name.c_str());
663
664         /* get buffer parameters */
665         SessionParams session_params = BlenderSync::get_session_params(b_engine, b_userpref, b_scene, background);
666         BufferParams buffer_params = BlenderSync::get_buffer_params(b_render, b_v3d, b_rv3d, scene->camera, width, height);
667
668         scene->bake_manager->set_shader_limit((size_t)b_engine.tile_x(), (size_t)b_engine.tile_y());
669
670         /* set number of samples */
671         session->tile_manager.set_samples(session_params.samples);
672         session->reset(buffer_params, session_params.samples);
673         session->update_scene();
674
675         /* find object index. todo: is arbitrary - copied from mesh_displace.cpp */
676         for(size_t i = 0; i < scene->objects.size(); i++) {
677                 if(strcmp(scene->objects[i]->name.c_str(), b_object.name().c_str()) == 0) {
678                         object_index = i;
679                         tri_offset = scene->objects[i]->mesh->tri_offset;
680                         break;
681                 }
682         }
683
684         /* when used, non-instanced convention: object = ~object */
685         int object = ~object_index;
686
687         BakeData *bake_data = scene->bake_manager->init(object, tri_offset, num_pixels);
688
689         populate_bake_data(bake_data, object_id, pixel_array, num_pixels);
690
691         /* set number of samples */
692         session->tile_manager.set_samples(session_params.samples);
693         session->reset(buffer_params, session_params.samples);
694         session->update_scene();
695
696         session->progress.set_update_callback(function_bind(&BlenderSession::update_bake_progress, this));
697
698         scene->bake_manager->bake(scene->device, &scene->dscene, scene, session->progress, shader_type, bake_pass_filter, bake_data, result);
699
700         /* free all memory used (host and device), so we wouldn't leave render
701          * engine with extra memory allocated
702          */
703
704         session->device_free();
705
706         delete sync;
707         sync = NULL;
708 }
709
710 void BlenderSession::do_write_update_render_result(BL::RenderResult& b_rr,
711                                                    BL::RenderLayer& b_rlay,
712                                                    RenderTile& rtile,
713                                                    bool do_update_only)
714 {
715         RenderBuffers *buffers = rtile.buffers;
716
717         /* copy data from device */
718         if(!buffers->copy_from_device())
719                 return;
720
721         BufferParams& params = buffers->params;
722         float exposure = scene->film->exposure;
723
724         vector<float> pixels(params.width*params.height*4);
725
726         /* Adjust absolute sample number to the range. */
727         int sample = rtile.sample;
728         const int range_start_sample = session->tile_manager.range_start_sample;
729         if(range_start_sample != -1) {
730                 sample -= range_start_sample;
731         }
732
733         if(!do_update_only) {
734                 /* copy each pass */
735                 BL::RenderLayer::passes_iterator b_iter;
736
737                 for(b_rlay.passes.begin(b_iter); b_iter != b_rlay.passes.end(); ++b_iter) {
738                         BL::RenderPass b_pass(*b_iter);
739
740                         /* find matching pass type */
741                         PassType pass_type = get_pass_type(b_pass);
742                         int components = b_pass.channels();
743
744                         /* copy pixels */
745                         if(!buffers->get_pass_rect(pass_type, exposure, sample, components, &pixels[0]))
746                                 memset(&pixels[0], 0, pixels.size()*sizeof(float));
747
748                         b_pass.rect(&pixels[0]);
749                 }
750         }
751         else {
752                 /* copy combined pass */
753                 BL::RenderPass b_combined_pass(b_rlay.passes.find_by_type(BL::RenderPass::type_COMBINED, b_rview_name.c_str()));
754                 if(buffers->get_pass_rect(PASS_COMBINED, exposure, sample, 4, &pixels[0]))
755                         b_combined_pass.rect(&pixels[0]);
756         }
757
758         /* tag result as updated */
759         b_engine.update_result(b_rr);
760 }
761
762 void BlenderSession::write_render_result(BL::RenderResult& b_rr,
763                                          BL::RenderLayer& b_rlay,
764                                          RenderTile& rtile)
765 {
766         do_write_update_render_result(b_rr, b_rlay, rtile, false);
767 }
768
769 void BlenderSession::update_render_result(BL::RenderResult& b_rr,
770                                           BL::RenderLayer& b_rlay,
771                                           RenderTile& rtile)
772 {
773         do_write_update_render_result(b_rr, b_rlay, rtile, true);
774 }
775
776 void BlenderSession::synchronize()
777 {
778         /* only used for viewport render */
779         if(!b_v3d)
780                 return;
781
782         /* on session/scene parameter changes, we recreate session entirely */
783         SessionParams session_params = BlenderSync::get_session_params(b_engine, b_userpref, b_scene, background);
784         const bool is_cpu = session_params.device.type == DEVICE_CPU;
785         SceneParams scene_params = BlenderSync::get_scene_params(b_scene, background, is_cpu);
786         bool session_pause = BlenderSync::get_session_pause(b_scene, background);
787
788         if(session->params.modified(session_params) ||
789            scene->params.modified(scene_params))
790         {
791                 free_session();
792                 create_session();
793                 session->start();
794                 return;
795         }
796
797         /* increase samples, but never decrease */
798         session->set_samples(session_params.samples);
799         session->set_pause(session_pause);
800
801         /* copy recalc flags, outside of mutex so we can decide to do the real
802          * synchronization at a later time to not block on running updates */
803         sync->sync_recalc();
804
805         /* don't do synchronization if on pause */
806         if(session_pause) {
807                 tag_update();
808                 return;
809         }
810
811         /* try to acquire mutex. if we don't want to or can't, come back later */
812         if(!session->ready_to_reset() || !session->scene->mutex.try_lock()) {
813                 tag_update();
814                 return;
815         }
816
817         /* data and camera synchronize */
818         BL::Object b_camera_override(b_engine.camera_override());
819         sync->sync_data(b_render,
820                         b_v3d,
821                         b_camera_override,
822                         width, height,
823                         &python_thread_state,
824                         b_rlay_name.c_str());
825
826         if(b_rv3d)
827                 sync->sync_view(b_v3d, b_rv3d, width, height);
828         else
829                 sync->sync_camera(b_render, b_camera_override, width, height, "");
830
831         /* unlock */
832         session->scene->mutex.unlock();
833
834         /* reset if needed */
835         if(scene->need_reset()) {
836                 BufferParams buffer_params = BlenderSync::get_buffer_params(b_render, b_v3d, b_rv3d, scene->camera, width, height);
837                 session->reset(buffer_params, session_params.samples);
838
839                 /* reset time */
840                 start_resize_time = 0.0;
841         }
842 }
843
844 bool BlenderSession::draw(int w, int h)
845 {
846         /* pause in redraw in case update is not being called due to final render */
847         session->set_pause(BlenderSync::get_session_pause(b_scene, background));
848
849         /* before drawing, we verify camera and viewport size changes, because
850          * we do not get update callbacks for those, we must detect them here */
851         if(session->ready_to_reset()) {
852                 bool reset = false;
853
854                 /* if dimensions changed, reset */
855                 if(width != w || height != h) {
856                         if(start_resize_time == 0.0) {
857                                 /* don't react immediately to resizes to avoid flickery resizing
858                                  * of the viewport, and some window managers changing the window
859                                  * size temporarily on unminimize */
860                                 start_resize_time = time_dt();
861                                 tag_redraw();
862                         }
863                         else if(time_dt() - start_resize_time < 0.2) {
864                                 tag_redraw();
865                         }
866                         else {
867                                 width = w;
868                                 height = h;
869                                 reset = true;
870                         }
871                 }
872
873                 /* try to acquire mutex. if we can't, come back later */
874                 if(!session->scene->mutex.try_lock()) {
875                         tag_update();
876                 }
877                 else {
878                         /* update camera from 3d view */
879
880                         sync->sync_view(b_v3d, b_rv3d, width, height);
881
882                         if(scene->camera->need_update)
883                                 reset = true;
884
885                         session->scene->mutex.unlock();
886                 }
887
888                 /* reset if requested */
889                 if(reset) {
890                         SessionParams session_params = BlenderSync::get_session_params(b_engine, b_userpref, b_scene, background);
891                         BufferParams buffer_params = BlenderSync::get_buffer_params(b_render, b_v3d, b_rv3d, scene->camera, width, height);
892                         bool session_pause = BlenderSync::get_session_pause(b_scene, background);
893
894                         if(session_pause == false) {
895                                 session->reset(buffer_params, session_params.samples);
896                                 start_resize_time = 0.0;
897                         }
898                 }
899         }
900         else {
901                 tag_update();
902         }
903
904         /* update status and progress for 3d view draw */
905         update_status_progress();
906
907         /* draw */
908         BufferParams buffer_params = BlenderSync::get_buffer_params(b_render, b_v3d, b_rv3d, scene->camera, width, height);
909         DeviceDrawParams draw_params;
910
911         if(session->params.display_buffer_linear) {
912                 draw_params.bind_display_space_shader_cb = function_bind(&BL::RenderEngine::bind_display_space_shader, &b_engine, b_scene);
913                 draw_params.unbind_display_space_shader_cb = function_bind(&BL::RenderEngine::unbind_display_space_shader, &b_engine);
914         }
915
916         return !session->draw(buffer_params, draw_params);
917 }
918
919 void BlenderSession::get_status(string& status, string& substatus)
920 {
921         session->progress.get_status(status, substatus);
922 }
923
924 void BlenderSession::get_progress(float& progress, double& total_time, double& render_time)
925 {
926         double tile_time;
927         int tile, sample, samples_per_tile;
928         int tile_total = session->tile_manager.state.num_tiles;
929         int samples = session->tile_manager.state.sample + 1;
930         int total_samples = session->tile_manager.get_num_effective_samples();
931
932         session->progress.get_tile(tile, total_time, render_time, tile_time);
933
934         sample = session->progress.get_sample();
935         samples_per_tile = session->tile_manager.get_num_effective_samples();
936
937         if(background && samples_per_tile && tile_total)
938                 progress = ((float)sample / (float)(tile_total * samples_per_tile));
939         else if(!background && samples > 0 && total_samples != INT_MAX)
940                 progress = ((float)samples) / total_samples;
941         else
942                 progress = 0.0;
943 }
944
945 void BlenderSession::update_bake_progress()
946 {
947         float progress;
948         int sample, samples_per_task, parts_total;
949
950         sample = session->progress.get_sample();
951         samples_per_task = scene->bake_manager->num_samples;
952         parts_total = scene->bake_manager->num_parts;
953
954         if(samples_per_task)
955                 progress = ((float)sample / (float)(parts_total * samples_per_task));
956         else
957                 progress = 0.0;
958
959         if(progress != last_progress) {
960                 b_engine.update_progress(progress);
961                 last_progress = progress;
962         }
963 }
964
965 void BlenderSession::update_status_progress()
966 {
967         string timestatus, status, substatus;
968         string scene = "";
969         float progress;
970         double total_time, remaining_time = 0, render_time;
971         char time_str[128];
972         float mem_used = (float)session->stats.mem_used / 1024.0f / 1024.0f;
973         float mem_peak = (float)session->stats.mem_peak / 1024.0f / 1024.0f;
974
975         get_status(status, substatus);
976         get_progress(progress, total_time, render_time);
977
978         if(progress > 0)
979                 remaining_time = (1.0 - (double)progress) * (render_time / (double)progress);
980
981         if(background) {
982                 scene += " | " + b_scene.name();
983                 if(b_rlay_name != "")
984                         scene += ", "  + b_rlay_name;
985
986                 if(b_rview_name != "")
987                         scene += ", " + b_rview_name;
988         }
989         else {
990                 BLI_timecode_string_from_time_simple(time_str, sizeof(time_str), total_time);
991                 timestatus = "Time:" + string(time_str) + " | ";
992         }
993
994         if(remaining_time > 0) {
995                 BLI_timecode_string_from_time_simple(time_str, sizeof(time_str), remaining_time);
996                 timestatus += "Remaining:" + string(time_str) + " | ";
997         }
998
999         timestatus += string_printf("Mem:%.2fM, Peak:%.2fM", (double)mem_used, (double)mem_peak);
1000
1001         if(status.size() > 0)
1002                 status = " | " + status;
1003         if(substatus.size() > 0)
1004                 status += " | " + substatus;
1005
1006         if(status != last_status) {
1007                 b_engine.update_stats("", (timestatus + scene + status).c_str());
1008                 b_engine.update_memory_stats(mem_used, mem_peak);
1009                 last_status = status;
1010         }
1011         if(progress != last_progress) {
1012                 b_engine.update_progress(progress);
1013                 last_progress = progress;
1014         }
1015
1016         if(session->progress.get_error()) {
1017                 string error = session->progress.get_error_message();
1018                 if(error != last_error) {
1019                         /* TODO(sergey): Currently C++ RNA API doesn't let us to
1020                          * use mnemonic name for the variable. Would be nice to
1021                          * have this figured out.
1022                          *
1023                          * For until then, 1 << 5 means RPT_ERROR.
1024                          */
1025                         b_engine.report(1 << 5, error.c_str());
1026                         b_engine.error_set(error.c_str());
1027                         last_error = error;
1028                 }
1029         }
1030 }
1031
1032 void BlenderSession::tag_update()
1033 {
1034         /* tell blender that we want to get another update callback */
1035         b_engine.tag_update();
1036 }
1037
1038 void BlenderSession::tag_redraw()
1039 {
1040         if(background) {
1041                 /* update stats and progress, only for background here because
1042                  * in 3d view we do it in draw for thread safety reasons */
1043                 update_status_progress();
1044
1045                 /* offline render, redraw if timeout passed */
1046                 if(time_dt() - last_redraw_time > 1.0) {
1047                         b_engine.tag_redraw();
1048                         last_redraw_time = time_dt();
1049                 }
1050         }
1051         else {
1052                 /* tell blender that we want to redraw */
1053                 b_engine.tag_redraw();
1054         }
1055 }
1056
1057 void BlenderSession::test_cancel()
1058 {
1059         /* test if we need to cancel rendering */
1060         if(background)
1061                 if(b_engine.test_break())
1062                         session->progress.set_cancel("Cancelled");
1063 }
1064
1065 /* builtin image file name is actually an image datablock name with
1066  * absolute sequence frame number concatenated via '@' character
1067  *
1068  * this function splits frame from builtin name
1069  */
1070 int BlenderSession::builtin_image_frame(const string &builtin_name)
1071 {
1072         int last = builtin_name.find_last_of('@');
1073         return atoi(builtin_name.substr(last + 1, builtin_name.size() - last - 1).c_str());
1074 }
1075
1076 void BlenderSession::builtin_image_info(const string &builtin_name, void *builtin_data, bool &is_float, int &width, int &height, int &depth, int &channels)
1077 {
1078         /* empty image */
1079         is_float = false;
1080         width = 1;
1081         height = 1;
1082         depth = 0;
1083         channels = 0;
1084
1085         if(!builtin_data)
1086                 return;
1087
1088         /* recover ID pointer */
1089         PointerRNA ptr;
1090         RNA_id_pointer_create((ID*)builtin_data, &ptr);
1091         BL::ID b_id(ptr);
1092
1093         if(b_id.is_a(&RNA_Image)) {
1094                 /* image data */
1095                 BL::Image b_image(b_id);
1096
1097                 is_float = b_image.is_float();
1098                 width = b_image.size()[0];
1099                 height = b_image.size()[1];
1100                 depth = 1;
1101                 channels = b_image.channels();
1102         }
1103         else if(b_id.is_a(&RNA_Object)) {
1104                 /* smoke volume data */
1105                 BL::Object b_ob(b_id);
1106                 BL::SmokeDomainSettings b_domain = object_smoke_domain_find(b_ob);
1107
1108                 is_float = true;
1109                 depth = 1;
1110                 channels = 1;
1111
1112                 if(!b_domain)
1113                         return;
1114
1115                 if(builtin_name == Attribute::standard_name(ATTR_STD_VOLUME_DENSITY) ||
1116                    builtin_name == Attribute::standard_name(ATTR_STD_VOLUME_FLAME))
1117                         channels = 1;
1118                 else if(builtin_name == Attribute::standard_name(ATTR_STD_VOLUME_COLOR))
1119                         channels = 4;
1120                 else
1121                         return;
1122
1123                 int3 resolution = get_int3(b_domain.domain_resolution());
1124                 int amplify = (b_domain.use_high_resolution())? b_domain.amplify() + 1: 1;
1125
1126                 width = resolution.x * amplify;
1127                 height = resolution.y * amplify;
1128                 depth = resolution.z * amplify;
1129         }
1130         else {
1131                 /* TODO(sergey): Check we're indeed in shader node tree. */
1132                 PointerRNA ptr;
1133                 RNA_pointer_create(NULL, &RNA_Node, builtin_data, &ptr);
1134                 BL::Node b_node(ptr);
1135                 if(b_node.is_a(&RNA_ShaderNodeTexPointDensity)) {
1136                         BL::ShaderNodeTexPointDensity b_point_density_node(b_node);
1137                         channels = 4;
1138                         width = height = depth = b_point_density_node.resolution();
1139                         is_float = true;
1140                 }
1141         }
1142 }
1143
1144 bool BlenderSession::builtin_image_pixels(const string &builtin_name, void *builtin_data, unsigned char *pixels)
1145 {
1146         if(!builtin_data)
1147                 return false;
1148
1149         int frame = builtin_image_frame(builtin_name);
1150
1151         PointerRNA ptr;
1152         RNA_id_pointer_create((ID*)builtin_data, &ptr);
1153         BL::Image b_image(ptr);
1154
1155         int width = b_image.size()[0];
1156         int height = b_image.size()[1];
1157         int channels = b_image.channels();
1158
1159         unsigned char *image_pixels;
1160         image_pixels = image_get_pixels_for_frame(b_image, frame);
1161         size_t num_pixels = ((size_t)width) * height;
1162
1163         if(image_pixels) {
1164                 memcpy(pixels, image_pixels, num_pixels * channels * sizeof(unsigned char));
1165                 MEM_freeN(image_pixels);
1166         }
1167         else {
1168                 if(channels == 1) {
1169                         memset(pixels, 0, num_pixels * sizeof(unsigned char));
1170                 }
1171                 else {
1172                         unsigned char *cp = pixels;
1173                         for(size_t i = 0; i < num_pixels; i++, cp += channels) {
1174                                 cp[0] = 255;
1175                                 cp[1] = 0;
1176                                 cp[2] = 255;
1177                                 if(channels == 4)
1178                                         cp[3] = 255;
1179                         }
1180                 }
1181         }
1182
1183         /* premultiply, byte images are always straight for blender */
1184         unsigned char *cp = pixels;
1185         for(size_t i = 0; i < num_pixels; i++, cp += channels) {
1186                 cp[0] = (cp[0] * cp[3]) >> 8;
1187                 cp[1] = (cp[1] * cp[3]) >> 8;
1188                 cp[2] = (cp[2] * cp[3]) >> 8;
1189         }
1190
1191         return true;
1192 }
1193
1194 bool BlenderSession::builtin_image_float_pixels(const string &builtin_name, void *builtin_data, float *pixels)
1195 {
1196         if(!builtin_data)
1197                 return false;
1198
1199         PointerRNA ptr;
1200         RNA_id_pointer_create((ID*)builtin_data, &ptr);
1201         BL::ID b_id(ptr);
1202
1203         if(b_id.is_a(&RNA_Image)) {
1204                 /* image data */
1205                 BL::Image b_image(b_id);
1206                 int frame = builtin_image_frame(builtin_name);
1207
1208                 int width = b_image.size()[0];
1209                 int height = b_image.size()[1];
1210                 int channels = b_image.channels();
1211
1212                 float *image_pixels;
1213                 image_pixels = image_get_float_pixels_for_frame(b_image, frame);
1214                 size_t num_pixels = ((size_t)width) * height;
1215
1216                 if(image_pixels) {
1217                         memcpy(pixels, image_pixels, num_pixels * channels * sizeof(float));
1218                         MEM_freeN(image_pixels);
1219                 }
1220                 else {
1221                         if(channels == 1) {
1222                                 memset(pixels, 0, num_pixels * sizeof(float));
1223                         }
1224                         else {
1225                                 float *fp = pixels;
1226                                 for(int i = 0; i < num_pixels; i++, fp += channels) {
1227                                         fp[0] = 1.0f;
1228                                         fp[1] = 0.0f;
1229                                         fp[2] = 1.0f;
1230                                         if(channels == 4)
1231                                                 fp[3] = 1.0f;
1232                                 }
1233                         }
1234                 }
1235
1236                 return true;
1237         }
1238         else if(b_id.is_a(&RNA_Object)) {
1239                 /* smoke volume data */
1240                 BL::Object b_ob(b_id);
1241                 BL::SmokeDomainSettings b_domain = object_smoke_domain_find(b_ob);
1242
1243                 if(!b_domain)
1244                         return false;
1245
1246                 int3 resolution = get_int3(b_domain.domain_resolution());
1247                 int length, amplify = (b_domain.use_high_resolution())? b_domain.amplify() + 1: 1;
1248
1249                 int width = resolution.x * amplify;
1250                 int height = resolution.y * amplify;
1251                 int depth = resolution.z * amplify;
1252                 size_t num_pixels = ((size_t)width) * height * depth;
1253
1254                 if(builtin_name == Attribute::standard_name(ATTR_STD_VOLUME_DENSITY)) {
1255                         SmokeDomainSettings_density_grid_get_length(&b_domain.ptr, &length);
1256
1257                         if(length == num_pixels) {
1258                                 SmokeDomainSettings_density_grid_get(&b_domain.ptr, pixels);
1259                                 return true;
1260                         }
1261                 }
1262                 else if(builtin_name == Attribute::standard_name(ATTR_STD_VOLUME_FLAME)) {
1263                         /* this is in range 0..1, and interpreted by the OpenGL smoke viewer
1264                          * as 1500..3000 K with the first part faded to zero density */
1265                         SmokeDomainSettings_flame_grid_get_length(&b_domain.ptr, &length);
1266
1267                         if(length == num_pixels) {
1268                                 SmokeDomainSettings_flame_grid_get(&b_domain.ptr, pixels);
1269                                 return true;
1270                         }
1271                 }
1272                 else if(builtin_name == Attribute::standard_name(ATTR_STD_VOLUME_COLOR)) {
1273                         /* the RGB is "premultiplied" by density for better interpolation results */
1274                         SmokeDomainSettings_color_grid_get_length(&b_domain.ptr, &length);
1275
1276                         if(length == num_pixels*4) {
1277                                 SmokeDomainSettings_color_grid_get(&b_domain.ptr, pixels);
1278                                 return true;
1279                         }
1280                 }
1281                 else {
1282                         fprintf(stderr, "Cycles error: unknown volume attribute, skipping\n");
1283                         pixels[0] = 0.0f;
1284                         return false;
1285                 }
1286
1287                 fprintf(stderr, "Cycles error: unexpected smoke volume resolution, skipping\n");
1288         }
1289         else {
1290                 /* TODO(sergey): Check we're indeed in shader node tree. */
1291                 PointerRNA ptr;
1292                 RNA_pointer_create(NULL, &RNA_Node, builtin_data, &ptr);
1293                 BL::Node b_node(ptr);
1294                 if(b_node.is_a(&RNA_ShaderNodeTexPointDensity)) {
1295                         BL::ShaderNodeTexPointDensity b_point_density_node(b_node);
1296                         int length;
1297                         int settings = background ? 1 : 0;  /* 1 - render settings, 0 - vewport settings. */
1298                         b_point_density_node.calc_point_density(b_scene, settings, &length, &pixels);
1299                 }
1300         }
1301
1302         return false;
1303 }
1304
1305 void BlenderSession::update_resumable_tile_manager(int num_samples)
1306 {
1307         const int num_resumable_chunks = BlenderSession::num_resumable_chunks,
1308                   current_resumable_chunk = BlenderSession::current_resumable_chunk;
1309         if(num_resumable_chunks == 0) {
1310                 return;
1311         }
1312
1313         int num_samples_per_chunk = (int)ceilf((float)num_samples / num_resumable_chunks);
1314         int range_start_sample = num_samples_per_chunk * (current_resumable_chunk - 1);
1315         int range_num_samples = num_samples_per_chunk;
1316         if(range_start_sample + range_num_samples > num_samples) {
1317                 range_num_samples = num_samples - range_num_samples;
1318         }
1319
1320         VLOG(1) << "Samples range start is " << range_start_sample << ", "
1321                 << "number of samples to render is " << range_num_samples;
1322
1323         session->tile_manager.range_start_sample = range_start_sample;
1324         session->tile_manager.range_num_samples = range_num_samples;
1325 }
1326
1327 CCL_NAMESPACE_END