Cycles: Replace object index hack with actual checks for SD_TRANSFORM_APPLIED
[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                 array<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         int object = object_index;
685
686         BakeData *bake_data = scene->bake_manager->init(object, tri_offset, num_pixels);
687
688         populate_bake_data(bake_data, object_id, pixel_array, num_pixels);
689
690         /* set number of samples */
691         session->tile_manager.set_samples(session_params.samples);
692         session->reset(buffer_params, session_params.samples);
693         session->update_scene();
694
695         session->progress.set_update_callback(function_bind(&BlenderSession::update_bake_progress, this));
696
697         scene->bake_manager->bake(scene->device, &scene->dscene, scene, session->progress, shader_type, bake_pass_filter, bake_data, result);
698
699         /* free all memory used (host and device), so we wouldn't leave render
700          * engine with extra memory allocated
701          */
702
703         session->device_free();
704
705         delete sync;
706         sync = NULL;
707 }
708
709 void BlenderSession::do_write_update_render_result(BL::RenderResult& b_rr,
710                                                    BL::RenderLayer& b_rlay,
711                                                    RenderTile& rtile,
712                                                    bool do_update_only)
713 {
714         RenderBuffers *buffers = rtile.buffers;
715
716         /* copy data from device */
717         if(!buffers->copy_from_device())
718                 return;
719
720         BufferParams& params = buffers->params;
721         float exposure = scene->film->exposure;
722
723         vector<float> pixels(params.width*params.height*4);
724
725         /* Adjust absolute sample number to the range. */
726         int sample = rtile.sample;
727         const int range_start_sample = session->tile_manager.range_start_sample;
728         if(range_start_sample != -1) {
729                 sample -= range_start_sample;
730         }
731
732         if(!do_update_only) {
733                 /* copy each pass */
734                 BL::RenderLayer::passes_iterator b_iter;
735
736                 for(b_rlay.passes.begin(b_iter); b_iter != b_rlay.passes.end(); ++b_iter) {
737                         BL::RenderPass b_pass(*b_iter);
738
739                         /* find matching pass type */
740                         PassType pass_type = get_pass_type(b_pass);
741                         int components = b_pass.channels();
742
743                         /* copy pixels */
744                         if(!buffers->get_pass_rect(pass_type, exposure, sample, components, &pixels[0]))
745                                 memset(&pixels[0], 0, pixels.size()*sizeof(float));
746
747                         b_pass.rect(&pixels[0]);
748                 }
749         }
750         else {
751                 /* copy combined pass */
752                 BL::RenderPass b_combined_pass(b_rlay.passes.find_by_type(BL::RenderPass::type_COMBINED, b_rview_name.c_str()));
753                 if(buffers->get_pass_rect(PASS_COMBINED, exposure, sample, 4, &pixels[0]))
754                         b_combined_pass.rect(&pixels[0]);
755         }
756
757         /* tag result as updated */
758         b_engine.update_result(b_rr);
759 }
760
761 void BlenderSession::write_render_result(BL::RenderResult& b_rr,
762                                          BL::RenderLayer& b_rlay,
763                                          RenderTile& rtile)
764 {
765         do_write_update_render_result(b_rr, b_rlay, rtile, false);
766 }
767
768 void BlenderSession::update_render_result(BL::RenderResult& b_rr,
769                                           BL::RenderLayer& b_rlay,
770                                           RenderTile& rtile)
771 {
772         do_write_update_render_result(b_rr, b_rlay, rtile, true);
773 }
774
775 void BlenderSession::synchronize()
776 {
777         /* only used for viewport render */
778         if(!b_v3d)
779                 return;
780
781         /* on session/scene parameter changes, we recreate session entirely */
782         SessionParams session_params = BlenderSync::get_session_params(b_engine, b_userpref, b_scene, background);
783         const bool is_cpu = session_params.device.type == DEVICE_CPU;
784         SceneParams scene_params = BlenderSync::get_scene_params(b_scene, background, is_cpu);
785         bool session_pause = BlenderSync::get_session_pause(b_scene, background);
786
787         if(session->params.modified(session_params) ||
788            scene->params.modified(scene_params))
789         {
790                 free_session();
791                 create_session();
792                 session->start();
793                 return;
794         }
795
796         /* increase samples, but never decrease */
797         session->set_samples(session_params.samples);
798         session->set_pause(session_pause);
799
800         /* copy recalc flags, outside of mutex so we can decide to do the real
801          * synchronization at a later time to not block on running updates */
802         sync->sync_recalc();
803
804         /* don't do synchronization if on pause */
805         if(session_pause) {
806                 tag_update();
807                 return;
808         }
809
810         /* try to acquire mutex. if we don't want to or can't, come back later */
811         if(!session->ready_to_reset() || !session->scene->mutex.try_lock()) {
812                 tag_update();
813                 return;
814         }
815
816         /* data and camera synchronize */
817         BL::Object b_camera_override(b_engine.camera_override());
818         sync->sync_data(b_render,
819                         b_v3d,
820                         b_camera_override,
821                         width, height,
822                         &python_thread_state,
823                         b_rlay_name.c_str());
824
825         if(b_rv3d)
826                 sync->sync_view(b_v3d, b_rv3d, width, height);
827         else
828                 sync->sync_camera(b_render, b_camera_override, width, height, "");
829
830         /* unlock */
831         session->scene->mutex.unlock();
832
833         /* reset if needed */
834         if(scene->need_reset()) {
835                 BufferParams buffer_params = BlenderSync::get_buffer_params(b_render, b_v3d, b_rv3d, scene->camera, width, height);
836                 session->reset(buffer_params, session_params.samples);
837
838                 /* reset time */
839                 start_resize_time = 0.0;
840         }
841 }
842
843 bool BlenderSession::draw(int w, int h)
844 {
845         /* pause in redraw in case update is not being called due to final render */
846         session->set_pause(BlenderSync::get_session_pause(b_scene, background));
847
848         /* before drawing, we verify camera and viewport size changes, because
849          * we do not get update callbacks for those, we must detect them here */
850         if(session->ready_to_reset()) {
851                 bool reset = false;
852
853                 /* if dimensions changed, reset */
854                 if(width != w || height != h) {
855                         if(start_resize_time == 0.0) {
856                                 /* don't react immediately to resizes to avoid flickery resizing
857                                  * of the viewport, and some window managers changing the window
858                                  * size temporarily on unminimize */
859                                 start_resize_time = time_dt();
860                                 tag_redraw();
861                         }
862                         else if(time_dt() - start_resize_time < 0.2) {
863                                 tag_redraw();
864                         }
865                         else {
866                                 width = w;
867                                 height = h;
868                                 reset = true;
869                         }
870                 }
871
872                 /* try to acquire mutex. if we can't, come back later */
873                 if(!session->scene->mutex.try_lock()) {
874                         tag_update();
875                 }
876                 else {
877                         /* update camera from 3d view */
878
879                         sync->sync_view(b_v3d, b_rv3d, width, height);
880
881                         if(scene->camera->need_update)
882                                 reset = true;
883
884                         session->scene->mutex.unlock();
885                 }
886
887                 /* reset if requested */
888                 if(reset) {
889                         SessionParams session_params = BlenderSync::get_session_params(b_engine, b_userpref, b_scene, background);
890                         BufferParams buffer_params = BlenderSync::get_buffer_params(b_render, b_v3d, b_rv3d, scene->camera, width, height);
891                         bool session_pause = BlenderSync::get_session_pause(b_scene, background);
892
893                         if(session_pause == false) {
894                                 session->reset(buffer_params, session_params.samples);
895                                 start_resize_time = 0.0;
896                         }
897                 }
898         }
899         else {
900                 tag_update();
901         }
902
903         /* update status and progress for 3d view draw */
904         update_status_progress();
905
906         /* draw */
907         BufferParams buffer_params = BlenderSync::get_buffer_params(b_render, b_v3d, b_rv3d, scene->camera, width, height);
908         DeviceDrawParams draw_params;
909
910         if(session->params.display_buffer_linear) {
911                 draw_params.bind_display_space_shader_cb = function_bind(&BL::RenderEngine::bind_display_space_shader, &b_engine, b_scene);
912                 draw_params.unbind_display_space_shader_cb = function_bind(&BL::RenderEngine::unbind_display_space_shader, &b_engine);
913         }
914
915         return !session->draw(buffer_params, draw_params);
916 }
917
918 void BlenderSession::get_status(string& status, string& substatus)
919 {
920         session->progress.get_status(status, substatus);
921 }
922
923 void BlenderSession::get_progress(float& progress, double& total_time, double& render_time)
924 {
925         double tile_time;
926         int tile, sample, samples_per_tile;
927         int tile_total = session->tile_manager.state.num_tiles;
928         int samples = session->tile_manager.state.sample + 1;
929         int total_samples = session->tile_manager.get_num_effective_samples();
930
931         session->progress.get_tile(tile, total_time, render_time, tile_time);
932
933         sample = session->progress.get_sample();
934         samples_per_tile = session->tile_manager.get_num_effective_samples();
935
936         if(background && samples_per_tile && tile_total)
937                 progress = ((float)sample / (float)(tile_total * samples_per_tile));
938         else if(!background && samples > 0 && total_samples != INT_MAX)
939                 progress = ((float)samples) / total_samples;
940         else
941                 progress = 0.0;
942 }
943
944 void BlenderSession::update_bake_progress()
945 {
946         float progress;
947         int sample, samples_per_task, parts_total;
948
949         sample = session->progress.get_sample();
950         samples_per_task = scene->bake_manager->num_samples;
951         parts_total = scene->bake_manager->num_parts;
952
953         if(samples_per_task)
954                 progress = ((float)sample / (float)(parts_total * samples_per_task));
955         else
956                 progress = 0.0;
957
958         if(progress != last_progress) {
959                 b_engine.update_progress(progress);
960                 last_progress = progress;
961         }
962 }
963
964 void BlenderSession::update_status_progress()
965 {
966         string timestatus, status, substatus;
967         string scene = "";
968         float progress;
969         double total_time, remaining_time = 0, render_time;
970         char time_str[128];
971         float mem_used = (float)session->stats.mem_used / 1024.0f / 1024.0f;
972         float mem_peak = (float)session->stats.mem_peak / 1024.0f / 1024.0f;
973
974         get_status(status, substatus);
975         get_progress(progress, total_time, render_time);
976
977         if(progress > 0)
978                 remaining_time = (1.0 - (double)progress) * (render_time / (double)progress);
979
980         if(background) {
981                 scene += " | " + b_scene.name();
982                 if(b_rlay_name != "")
983                         scene += ", "  + b_rlay_name;
984
985                 if(b_rview_name != "")
986                         scene += ", " + b_rview_name;
987         }
988         else {
989                 BLI_timecode_string_from_time_simple(time_str, sizeof(time_str), total_time);
990                 timestatus = "Time:" + string(time_str) + " | ";
991         }
992
993         if(remaining_time > 0) {
994                 BLI_timecode_string_from_time_simple(time_str, sizeof(time_str), remaining_time);
995                 timestatus += "Remaining:" + string(time_str) + " | ";
996         }
997
998         timestatus += string_printf("Mem:%.2fM, Peak:%.2fM", (double)mem_used, (double)mem_peak);
999
1000         if(status.size() > 0)
1001                 status = " | " + status;
1002         if(substatus.size() > 0)
1003                 status += " | " + substatus;
1004
1005         if(status != last_status) {
1006                 b_engine.update_stats("", (timestatus + scene + status).c_str());
1007                 b_engine.update_memory_stats(mem_used, mem_peak);
1008                 last_status = status;
1009         }
1010         if(progress != last_progress) {
1011                 b_engine.update_progress(progress);
1012                 last_progress = progress;
1013         }
1014
1015         if(session->progress.get_error()) {
1016                 string error = session->progress.get_error_message();
1017                 if(error != last_error) {
1018                         /* TODO(sergey): Currently C++ RNA API doesn't let us to
1019                          * use mnemonic name for the variable. Would be nice to
1020                          * have this figured out.
1021                          *
1022                          * For until then, 1 << 5 means RPT_ERROR.
1023                          */
1024                         b_engine.report(1 << 5, error.c_str());
1025                         b_engine.error_set(error.c_str());
1026                         last_error = error;
1027                 }
1028         }
1029 }
1030
1031 void BlenderSession::tag_update()
1032 {
1033         /* tell blender that we want to get another update callback */
1034         b_engine.tag_update();
1035 }
1036
1037 void BlenderSession::tag_redraw()
1038 {
1039         if(background) {
1040                 /* update stats and progress, only for background here because
1041                  * in 3d view we do it in draw for thread safety reasons */
1042                 update_status_progress();
1043
1044                 /* offline render, redraw if timeout passed */
1045                 if(time_dt() - last_redraw_time > 1.0) {
1046                         b_engine.tag_redraw();
1047                         last_redraw_time = time_dt();
1048                 }
1049         }
1050         else {
1051                 /* tell blender that we want to redraw */
1052                 b_engine.tag_redraw();
1053         }
1054 }
1055
1056 void BlenderSession::test_cancel()
1057 {
1058         /* test if we need to cancel rendering */
1059         if(background)
1060                 if(b_engine.test_break())
1061                         session->progress.set_cancel("Cancelled");
1062 }
1063
1064 /* builtin image file name is actually an image datablock name with
1065  * absolute sequence frame number concatenated via '@' character
1066  *
1067  * this function splits frame from builtin name
1068  */
1069 int BlenderSession::builtin_image_frame(const string &builtin_name)
1070 {
1071         int last = builtin_name.find_last_of('@');
1072         return atoi(builtin_name.substr(last + 1, builtin_name.size() - last - 1).c_str());
1073 }
1074
1075 void BlenderSession::builtin_image_info(const string &builtin_name, void *builtin_data, bool &is_float, int &width, int &height, int &depth, int &channels)
1076 {
1077         /* empty image */
1078         is_float = false;
1079         width = 1;
1080         height = 1;
1081         depth = 0;
1082         channels = 0;
1083
1084         if(!builtin_data)
1085                 return;
1086
1087         /* recover ID pointer */
1088         PointerRNA ptr;
1089         RNA_id_pointer_create((ID*)builtin_data, &ptr);
1090         BL::ID b_id(ptr);
1091
1092         if(b_id.is_a(&RNA_Image)) {
1093                 /* image data */
1094                 BL::Image b_image(b_id);
1095
1096                 is_float = b_image.is_float();
1097                 width = b_image.size()[0];
1098                 height = b_image.size()[1];
1099                 depth = 1;
1100                 channels = b_image.channels();
1101         }
1102         else if(b_id.is_a(&RNA_Object)) {
1103                 /* smoke volume data */
1104                 BL::Object b_ob(b_id);
1105                 BL::SmokeDomainSettings b_domain = object_smoke_domain_find(b_ob);
1106
1107                 is_float = true;
1108                 depth = 1;
1109                 channels = 1;
1110
1111                 if(!b_domain)
1112                         return;
1113
1114                 if(builtin_name == Attribute::standard_name(ATTR_STD_VOLUME_DENSITY) ||
1115                    builtin_name == Attribute::standard_name(ATTR_STD_VOLUME_FLAME) ||
1116                    builtin_name == Attribute::standard_name(ATTR_STD_VOLUME_HEAT))
1117                         channels = 1;
1118                 else if(builtin_name == Attribute::standard_name(ATTR_STD_VOLUME_COLOR))
1119                         channels = 4;
1120                 else if(builtin_name == Attribute::standard_name(ATTR_STD_VOLUME_VELOCITY))
1121                         channels = 3;
1122                 else
1123                         return;
1124
1125                 int3 resolution = get_int3(b_domain.domain_resolution());
1126                 int amplify = (b_domain.use_high_resolution())? b_domain.amplify() + 1: 1;
1127
1128                 /* Velocity and heat data is always low-resolution. */
1129                 if(builtin_name == Attribute::standard_name(ATTR_STD_VOLUME_VELOCITY) ||
1130                    builtin_name == Attribute::standard_name(ATTR_STD_VOLUME_HEAT))
1131                 {
1132                         amplify = 1;
1133                 }
1134
1135                 width = resolution.x * amplify;
1136                 height = resolution.y * amplify;
1137                 depth = resolution.z * amplify;
1138         }
1139         else {
1140                 /* TODO(sergey): Check we're indeed in shader node tree. */
1141                 PointerRNA ptr;
1142                 RNA_pointer_create(NULL, &RNA_Node, builtin_data, &ptr);
1143                 BL::Node b_node(ptr);
1144                 if(b_node.is_a(&RNA_ShaderNodeTexPointDensity)) {
1145                         BL::ShaderNodeTexPointDensity b_point_density_node(b_node);
1146                         channels = 4;
1147                         width = height = depth = b_point_density_node.resolution();
1148                         is_float = true;
1149                 }
1150         }
1151 }
1152
1153 bool BlenderSession::builtin_image_pixels(const string &builtin_name, void *builtin_data, unsigned char *pixels)
1154 {
1155         if(!builtin_data)
1156                 return false;
1157
1158         int frame = builtin_image_frame(builtin_name);
1159
1160         PointerRNA ptr;
1161         RNA_id_pointer_create((ID*)builtin_data, &ptr);
1162         BL::Image b_image(ptr);
1163
1164         int width = b_image.size()[0];
1165         int height = b_image.size()[1];
1166         int channels = b_image.channels();
1167
1168         unsigned char *image_pixels;
1169         image_pixels = image_get_pixels_for_frame(b_image, frame);
1170         size_t num_pixels = ((size_t)width) * height;
1171
1172         if(image_pixels) {
1173                 memcpy(pixels, image_pixels, num_pixels * channels * sizeof(unsigned char));
1174                 MEM_freeN(image_pixels);
1175         }
1176         else {
1177                 if(channels == 1) {
1178                         memset(pixels, 0, num_pixels * sizeof(unsigned char));
1179                 }
1180                 else {
1181                         unsigned char *cp = pixels;
1182                         for(size_t i = 0; i < num_pixels; i++, cp += channels) {
1183                                 cp[0] = 255;
1184                                 cp[1] = 0;
1185                                 cp[2] = 255;
1186                                 if(channels == 4)
1187                                         cp[3] = 255;
1188                         }
1189                 }
1190         }
1191
1192         /* premultiply, byte images are always straight for blender */
1193         unsigned char *cp = pixels;
1194         for(size_t i = 0; i < num_pixels; i++, cp += channels) {
1195                 cp[0] = (cp[0] * cp[3]) >> 8;
1196                 cp[1] = (cp[1] * cp[3]) >> 8;
1197                 cp[2] = (cp[2] * cp[3]) >> 8;
1198         }
1199
1200         return true;
1201 }
1202
1203 bool BlenderSession::builtin_image_float_pixels(const string &builtin_name, void *builtin_data, float *pixels)
1204 {
1205         if(!builtin_data)
1206                 return false;
1207
1208         PointerRNA ptr;
1209         RNA_id_pointer_create((ID*)builtin_data, &ptr);
1210         BL::ID b_id(ptr);
1211
1212         if(b_id.is_a(&RNA_Image)) {
1213                 /* image data */
1214                 BL::Image b_image(b_id);
1215                 int frame = builtin_image_frame(builtin_name);
1216
1217                 int width = b_image.size()[0];
1218                 int height = b_image.size()[1];
1219                 int channels = b_image.channels();
1220
1221                 float *image_pixels;
1222                 image_pixels = image_get_float_pixels_for_frame(b_image, frame);
1223                 size_t num_pixels = ((size_t)width) * height;
1224
1225                 if(image_pixels) {
1226                         memcpy(pixels, image_pixels, num_pixels * channels * sizeof(float));
1227                         MEM_freeN(image_pixels);
1228                 }
1229                 else {
1230                         if(channels == 1) {
1231                                 memset(pixels, 0, num_pixels * sizeof(float));
1232                         }
1233                         else {
1234                                 float *fp = pixels;
1235                                 for(int i = 0; i < num_pixels; i++, fp += channels) {
1236                                         fp[0] = 1.0f;
1237                                         fp[1] = 0.0f;
1238                                         fp[2] = 1.0f;
1239                                         if(channels == 4)
1240                                                 fp[3] = 1.0f;
1241                                 }
1242                         }
1243                 }
1244
1245                 return true;
1246         }
1247         else if(b_id.is_a(&RNA_Object)) {
1248                 /* smoke volume data */
1249                 BL::Object b_ob(b_id);
1250                 BL::SmokeDomainSettings b_domain = object_smoke_domain_find(b_ob);
1251
1252                 if(!b_domain)
1253                         return false;
1254
1255                 int3 resolution = get_int3(b_domain.domain_resolution());
1256                 int length, amplify = (b_domain.use_high_resolution())? b_domain.amplify() + 1: 1;
1257
1258                 /* Velocity and heat data is always low-resolution. */
1259                 if(builtin_name == Attribute::standard_name(ATTR_STD_VOLUME_VELOCITY) ||
1260                    builtin_name == Attribute::standard_name(ATTR_STD_VOLUME_HEAT))
1261                 {
1262                         amplify = 1;
1263                 }
1264
1265                 int width = resolution.x * amplify;
1266                 int height = resolution.y * amplify;
1267                 int depth = resolution.z * amplify;
1268                 size_t num_pixels = ((size_t)width) * height * depth;
1269
1270                 if(builtin_name == Attribute::standard_name(ATTR_STD_VOLUME_DENSITY)) {
1271                         SmokeDomainSettings_density_grid_get_length(&b_domain.ptr, &length);
1272                         if(length == num_pixels) {
1273                                 SmokeDomainSettings_density_grid_get(&b_domain.ptr, pixels);
1274                                 return true;
1275                         }
1276                 }
1277                 else if(builtin_name == Attribute::standard_name(ATTR_STD_VOLUME_FLAME)) {
1278                         /* this is in range 0..1, and interpreted by the OpenGL smoke viewer
1279                          * as 1500..3000 K with the first part faded to zero density */
1280                         SmokeDomainSettings_flame_grid_get_length(&b_domain.ptr, &length);
1281                         if(length == num_pixels) {
1282                                 SmokeDomainSettings_flame_grid_get(&b_domain.ptr, pixels);
1283                                 return true;
1284                         }
1285                 }
1286                 else if(builtin_name == Attribute::standard_name(ATTR_STD_VOLUME_COLOR)) {
1287                         /* the RGB is "premultiplied" by density for better interpolation results */
1288                         SmokeDomainSettings_color_grid_get_length(&b_domain.ptr, &length);
1289                         if(length == num_pixels*4) {
1290                                 SmokeDomainSettings_color_grid_get(&b_domain.ptr, pixels);
1291                                 return true;
1292                         }
1293                 }
1294                 else if(builtin_name == Attribute::standard_name(ATTR_STD_VOLUME_VELOCITY)) {
1295                         SmokeDomainSettings_velocity_grid_get_length(&b_domain.ptr, &length);
1296                         if(length == num_pixels*3) {
1297                                 SmokeDomainSettings_velocity_grid_get(&b_domain.ptr, pixels);
1298                                 return true;
1299                         }
1300                 }
1301                 else if(builtin_name == Attribute::standard_name(ATTR_STD_VOLUME_HEAT)) {
1302                         SmokeDomainSettings_heat_grid_get_length(&b_domain.ptr, &length);
1303                         if(length == num_pixels) {
1304                                 SmokeDomainSettings_heat_grid_get(&b_domain.ptr, pixels);
1305                                 return true;
1306                         }
1307                 }
1308                 else {
1309                         fprintf(stderr,
1310                                 "Cycles error: unknown volume attribute %s, skipping\n",
1311                                 builtin_name.c_str());
1312                         pixels[0] = 0.0f;
1313                         return false;
1314                 }
1315
1316                 fprintf(stderr, "Cycles error: unexpected smoke volume resolution, skipping\n");
1317         }
1318         else {
1319                 /* TODO(sergey): Check we're indeed in shader node tree. */
1320                 PointerRNA ptr;
1321                 RNA_pointer_create(NULL, &RNA_Node, builtin_data, &ptr);
1322                 BL::Node b_node(ptr);
1323                 if(b_node.is_a(&RNA_ShaderNodeTexPointDensity)) {
1324                         BL::ShaderNodeTexPointDensity b_point_density_node(b_node);
1325                         int length;
1326                         int settings = background ? 1 : 0;  /* 1 - render settings, 0 - vewport settings. */
1327                         b_point_density_node.calc_point_density(b_scene, settings, &length, &pixels);
1328                 }
1329         }
1330
1331         return false;
1332 }
1333
1334 void BlenderSession::update_resumable_tile_manager(int num_samples)
1335 {
1336         const int num_resumable_chunks = BlenderSession::num_resumable_chunks,
1337                   current_resumable_chunk = BlenderSession::current_resumable_chunk;
1338         if(num_resumable_chunks == 0) {
1339                 return;
1340         }
1341
1342         int num_samples_per_chunk = (int)ceilf((float)num_samples / num_resumable_chunks);
1343         int range_start_sample = num_samples_per_chunk * (current_resumable_chunk - 1);
1344         int range_num_samples = num_samples_per_chunk;
1345         if(range_start_sample + range_num_samples > num_samples) {
1346                 range_num_samples = num_samples - range_num_samples;
1347         }
1348
1349         VLOG(1) << "Samples range start is " << range_start_sample << ", "
1350                 << "number of samples to render is " << range_num_samples;
1351
1352         session->tile_manager.range_start_sample = range_start_sample;
1353         session->tile_manager.range_num_samples = range_num_samples;
1354 }
1355
1356 CCL_NAMESPACE_END