Fix T38597: cycles status bar missing some updates.
[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 "background.h"
18 #include "buffers.h"
19 #include "camera.h"
20 #include "device.h"
21 #include "integrator.h"
22 #include "film.h"
23 #include "light.h"
24 #include "scene.h"
25 #include "session.h"
26 #include "shader.h"
27
28 #include "util_color.h"
29 #include "util_foreach.h"
30 #include "util_function.h"
31 #include "util_progress.h"
32 #include "util_time.h"
33
34 #include "blender_sync.h"
35 #include "blender_session.h"
36 #include "blender_util.h"
37
38 CCL_NAMESPACE_BEGIN
39
40 BlenderSession::BlenderSession(BL::RenderEngine b_engine_, BL::UserPreferences b_userpref_,
41         BL::BlendData b_data_, BL::Scene b_scene_)
42 : b_engine(b_engine_), b_userpref(b_userpref_), b_data(b_data_), b_render(b_engine_.render()), b_scene(b_scene_),
43   b_v3d(PointerRNA_NULL), b_rv3d(PointerRNA_NULL)
44 {
45         /* offline render */
46
47         width = render_resolution_x(b_render);
48         height = render_resolution_y(b_render);
49
50         background = true;
51         last_redraw_time = 0.0;
52         start_resize_time = 0.0;
53
54         create_session();
55 }
56
57 BlenderSession::BlenderSession(BL::RenderEngine b_engine_, BL::UserPreferences b_userpref_,
58         BL::BlendData b_data_, BL::Scene b_scene_,
59         BL::SpaceView3D b_v3d_, BL::RegionView3D b_rv3d_, int width_, int height_)
60 : b_engine(b_engine_), b_userpref(b_userpref_), b_data(b_data_), b_render(b_scene_.render()), b_scene(b_scene_),
61   b_v3d(b_v3d_), b_rv3d(b_rv3d_)
62 {
63         /* 3d view render */
64
65         width = width_;
66         height = height_;
67         background = false;
68         last_redraw_time = 0.0;
69         start_resize_time = 0.0;
70
71         create_session();
72         session->start();
73 }
74
75 BlenderSession::~BlenderSession()
76 {
77         free_session();
78 }
79
80 void BlenderSession::create_session()
81 {
82         SceneParams scene_params = BlenderSync::get_scene_params(b_scene, background);
83         SessionParams session_params = BlenderSync::get_session_params(b_engine, b_userpref, b_scene, background);
84
85         /* reset status/progress */
86         last_status = "";
87         last_progress = -1.0f;
88         start_resize_time = 0.0;
89
90         /* create scene */
91         scene = new Scene(scene_params, session_params.device);
92
93         /* create session */
94         session = new Session(session_params);
95         session->scene = scene;
96         session->progress.set_update_callback(function_bind(&BlenderSession::tag_redraw, this));
97         session->progress.set_cancel_callback(function_bind(&BlenderSession::test_cancel, this));
98         session->set_pause(BlenderSync::get_session_pause(b_scene, background));
99
100         /* create sync */
101         sync = new BlenderSync(b_engine, b_data, b_scene, scene, !background, session->progress, session_params.device.type == DEVICE_CPU);
102
103         if(b_v3d) {
104                 /* full data sync */
105                 sync->sync_data(b_v3d, b_engine.camera_override());
106                 sync->sync_view(b_v3d, b_rv3d, width, height);
107         }
108         else {
109                 /* for final render we will do full data sync per render layer, only
110                  * do some basic syncing here, no objects or materials for speed */
111                 sync->sync_render_layers(b_v3d, NULL);
112                 sync->sync_integrator();
113                 sync->sync_camera(b_render, b_engine.camera_override(), width, height);
114         }
115
116         /* set buffer parameters */
117         BufferParams buffer_params = BlenderSync::get_buffer_params(b_render, b_scene, b_v3d, b_rv3d, scene->camera, width, height);
118         session->reset(buffer_params, session_params.samples);
119
120         b_engine.use_highlight_tiles(session_params.progressive_refine == false);
121
122         /* setup callbacks for builtin image support */
123         scene->image_manager->builtin_image_info_cb = function_bind(&BlenderSession::builtin_image_info, this, _1, _2, _3, _4, _5, _6);
124         scene->image_manager->builtin_image_pixels_cb = function_bind(&BlenderSession::builtin_image_pixels, this, _1, _2, _3);
125         scene->image_manager->builtin_image_float_pixels_cb = function_bind(&BlenderSession::builtin_image_float_pixels, this, _1, _2, _3);
126 }
127
128 void BlenderSession::reset_session(BL::BlendData b_data_, BL::Scene b_scene_)
129 {
130         b_data = b_data_;
131         b_render = b_engine.render();
132         b_scene = b_scene_;
133
134         SceneParams scene_params = BlenderSync::get_scene_params(b_scene, background);
135         SessionParams session_params = BlenderSync::get_session_params(b_engine, b_userpref, b_scene, background);
136
137         width = render_resolution_x(b_render);
138         height = render_resolution_y(b_render);
139
140         if(scene->params.modified(scene_params) ||
141            session->params.modified(session_params) ||
142            !scene_params.persistent_data)
143         {
144                 /* if scene or session parameters changed, it's easier to simply re-create
145                  * them rather than trying to distinguish which settings need to be updated
146                  */
147
148                 delete session;
149
150                 create_session();
151
152                 return;
153         }
154
155         session->progress.reset();
156         scene->reset();
157
158         session->tile_manager.set_tile_order(session_params.tile_order);
159
160         /* peak memory usage should show current render peak, not peak for all renders
161          * made by this render session
162          */
163         session->stats.mem_peak = session->stats.mem_used;
164
165         /* sync object should be re-created */
166         sync = new BlenderSync(b_engine, b_data, b_scene, scene, !background, session->progress, session_params.device.type == DEVICE_CPU);
167
168         /* for final render we will do full data sync per render layer, only
169          * do some basic syncing here, no objects or materials for speed */
170         sync->sync_render_layers(b_v3d, NULL);
171         sync->sync_integrator();
172         sync->sync_camera(b_render, b_engine.camera_override(), width, height);
173
174         BufferParams buffer_params = BlenderSync::get_buffer_params(b_render, b_scene, PointerRNA_NULL, PointerRNA_NULL, scene->camera, width, height);
175         session->reset(buffer_params, session_params.samples);
176
177         b_engine.use_highlight_tiles(session_params.progressive_refine == false);
178
179         /* reset time */
180         start_resize_time = 0.0;
181 }
182
183 void BlenderSession::free_session()
184 {
185         if(sync)
186                 delete sync;
187
188         delete session;
189 }
190
191 static PassType get_pass_type(BL::RenderPass b_pass)
192 {
193         switch(b_pass.type()) {
194                 case BL::RenderPass::type_COMBINED:
195                         return PASS_COMBINED;
196
197                 case BL::RenderPass::type_Z:
198                         return PASS_DEPTH;
199                 case BL::RenderPass::type_MIST:
200                         return PASS_MIST;
201                 case BL::RenderPass::type_NORMAL:
202                         return PASS_NORMAL;
203                 case BL::RenderPass::type_OBJECT_INDEX:
204                         return PASS_OBJECT_ID;
205                 case BL::RenderPass::type_UV:
206                         return PASS_UV;
207                 case BL::RenderPass::type_VECTOR:
208                         return PASS_MOTION;
209                 case BL::RenderPass::type_MATERIAL_INDEX:
210                         return PASS_MATERIAL_ID;
211
212                 case BL::RenderPass::type_DIFFUSE_DIRECT:
213                         return PASS_DIFFUSE_DIRECT;
214                 case BL::RenderPass::type_GLOSSY_DIRECT:
215                         return PASS_GLOSSY_DIRECT;
216                 case BL::RenderPass::type_TRANSMISSION_DIRECT:
217                         return PASS_TRANSMISSION_DIRECT;
218                 case BL::RenderPass::type_SUBSURFACE_DIRECT:
219                         return PASS_SUBSURFACE_DIRECT;
220
221                 case BL::RenderPass::type_DIFFUSE_INDIRECT:
222                         return PASS_DIFFUSE_INDIRECT;
223                 case BL::RenderPass::type_GLOSSY_INDIRECT:
224                         return PASS_GLOSSY_INDIRECT;
225                 case BL::RenderPass::type_TRANSMISSION_INDIRECT:
226                         return PASS_TRANSMISSION_INDIRECT;
227                 case BL::RenderPass::type_SUBSURFACE_INDIRECT:
228                         return PASS_SUBSURFACE_INDIRECT;
229
230                 case BL::RenderPass::type_DIFFUSE_COLOR:
231                         return PASS_DIFFUSE_COLOR;
232                 case BL::RenderPass::type_GLOSSY_COLOR:
233                         return PASS_GLOSSY_COLOR;
234                 case BL::RenderPass::type_TRANSMISSION_COLOR:
235                         return PASS_TRANSMISSION_COLOR;
236                 case BL::RenderPass::type_SUBSURFACE_COLOR:
237                         return PASS_SUBSURFACE_COLOR;
238
239                 case BL::RenderPass::type_EMIT:
240                         return PASS_EMISSION;
241                 case BL::RenderPass::type_ENVIRONMENT:
242                         return PASS_BACKGROUND;
243                 case BL::RenderPass::type_AO:
244                         return PASS_AO;
245                 case BL::RenderPass::type_SHADOW:
246                         return PASS_SHADOW;
247
248                 case BL::RenderPass::type_DIFFUSE:
249                 case BL::RenderPass::type_COLOR:
250                 case BL::RenderPass::type_REFRACTION:
251                 case BL::RenderPass::type_SPECULAR:
252                 case BL::RenderPass::type_REFLECTION:
253                         return PASS_NONE;
254         }
255         
256         return PASS_NONE;
257 }
258
259 static BL::RenderResult begin_render_result(BL::RenderEngine b_engine, int x, int y, int w, int h, const char *layername)
260 {
261         return b_engine.begin_result(x, y, w, h, layername);
262 }
263
264 static void end_render_result(BL::RenderEngine b_engine, BL::RenderResult b_rr, bool cancel, bool do_merge_results)
265 {
266         b_engine.end_result(b_rr, (int)cancel, (int)do_merge_results);
267 }
268
269 void BlenderSession::do_write_update_render_tile(RenderTile& rtile, bool do_update_only)
270 {
271         BufferParams& params = rtile.buffers->params;
272         int x = params.full_x - session->tile_manager.params.full_x;
273         int y = params.full_y - session->tile_manager.params.full_y;
274         int w = params.width;
275         int h = params.height;
276
277         /* get render result */
278         BL::RenderResult b_rr = begin_render_result(b_engine, x, y, w, h, b_rlay_name.c_str());
279
280         /* can happen if the intersected rectangle gives 0 width or height */
281         if (b_rr.ptr.data == NULL) {
282                 return;
283         }
284
285         BL::RenderResult::layers_iterator b_single_rlay;
286         b_rr.layers.begin(b_single_rlay);
287
288         /* layer will be missing if it was disabled in the UI */
289         if(b_single_rlay == b_rr.layers.end())
290                 return;
291
292         BL::RenderLayer b_rlay = *b_single_rlay;
293
294         if (do_update_only) {
295                 /* update only needed */
296
297                 if (rtile.sample != 0) {
298                         /* sample would be zero at initial tile update, which is only needed
299                          * to tag tile form blender side as IN PROGRESS for proper highlight
300                          * no buffers should be sent to blender yet
301                          */
302                         update_render_result(b_rr, b_rlay, rtile);
303                 }
304
305                 end_render_result(b_engine, b_rr, true, true);
306         }
307         else {
308                 /* write result */
309                 write_render_result(b_rr, b_rlay, rtile);
310                 end_render_result(b_engine, b_rr, false, true);
311         }
312 }
313
314 void BlenderSession::write_render_tile(RenderTile& rtile)
315 {
316         do_write_update_render_tile(rtile, false);
317 }
318
319 void BlenderSession::update_render_tile(RenderTile& rtile)
320 {
321         /* use final write for preview renders, otherwise render result wouldn't be
322          * be updated in blender side
323          * would need to be investigated a bit further, but for now shall be fine
324          */
325         if (!b_engine.is_preview())
326                 do_write_update_render_tile(rtile, true);
327         else
328                 do_write_update_render_tile(rtile, false);
329 }
330
331 void BlenderSession::render()
332 {
333         /* set callback to write out render results */
334         session->write_render_tile_cb = function_bind(&BlenderSession::write_render_tile, this, _1);
335         session->update_render_tile_cb = function_bind(&BlenderSession::update_render_tile, this, _1);
336
337         /* get buffer parameters */
338         SessionParams session_params = BlenderSync::get_session_params(b_engine, b_userpref, b_scene, background);
339         BufferParams buffer_params = BlenderSync::get_buffer_params(b_render, b_scene, b_v3d, b_rv3d, scene->camera, width, height);
340
341         /* render each layer */
342         BL::RenderSettings r = b_scene.render();
343         BL::RenderSettings::layers_iterator b_iter;
344         
345         for(r.layers.begin(b_iter); b_iter != r.layers.end(); ++b_iter) {
346                 b_rlay_name = b_iter->name();
347
348                 /* temporary render result to find needed passes */
349                 BL::RenderResult b_rr = begin_render_result(b_engine, 0, 0, 1, 1, b_rlay_name.c_str());
350                 BL::RenderResult::layers_iterator b_single_rlay;
351                 b_rr.layers.begin(b_single_rlay);
352
353                 /* layer will be missing if it was disabled in the UI */
354                 if(b_single_rlay == b_rr.layers.end()) {
355                         end_render_result(b_engine, b_rr, true, false);
356                         continue;
357                 }
358
359                 BL::RenderLayer b_rlay = *b_single_rlay;
360
361                 /* add passes */
362                 vector<Pass> passes;
363                 Pass::add(PASS_COMBINED, passes);
364
365                 if(session_params.device.advanced_shading) {
366
367                         /* loop over passes */
368                         BL::RenderLayer::passes_iterator b_pass_iter;
369
370                         for(b_rlay.passes.begin(b_pass_iter); b_pass_iter != b_rlay.passes.end(); ++b_pass_iter) {
371                                 BL::RenderPass b_pass(*b_pass_iter);
372                                 PassType pass_type = get_pass_type(b_pass);
373
374                                 if(pass_type == PASS_MOTION && scene->integrator->motion_blur)
375                                         continue;
376                                 if(pass_type != PASS_NONE)
377                                         Pass::add(pass_type, passes);
378                         }
379                 }
380
381                 /* free result without merging */
382                 end_render_result(b_engine, b_rr, true, false);
383
384                 buffer_params.passes = passes;
385                 scene->film->pass_alpha_threshold = b_iter->pass_alpha_threshold();
386                 scene->film->tag_passes_update(scene, passes);
387                 scene->film->tag_update(scene);
388                 scene->integrator->tag_update(scene);
389
390                 /* update scene */
391                 sync->sync_camera(b_render, b_engine.camera_override(), width, height);
392                 sync->sync_data(b_v3d, b_engine.camera_override(), b_rlay_name.c_str());
393
394                 /* update number of samples per layer */
395                 int samples = sync->get_layer_samples();
396                 bool bound_samples = sync->get_layer_bound_samples();
397
398                 if(samples != 0 && (!bound_samples || (samples < session_params.samples)))
399                         session->reset(buffer_params, samples);
400                 else
401                         session->reset(buffer_params, session_params.samples);
402
403                 /* render */
404                 session->start();
405                 session->wait();
406
407                 if(session->progress.get_cancel())
408                         break;
409         }
410
411         /* clear callback */
412         session->write_render_tile_cb = NULL;
413         session->update_render_tile_cb = NULL;
414
415         /* free all memory used (host and device), so we wouldn't leave render
416          * engine with extra memory allocated
417          */
418
419         session->device_free();
420
421         delete sync;
422         sync = NULL;
423 }
424
425 void BlenderSession::do_write_update_render_result(BL::RenderResult b_rr, BL::RenderLayer b_rlay, RenderTile& rtile, bool do_update_only)
426 {
427         RenderBuffers *buffers = rtile.buffers;
428
429         /* copy data from device */
430         if(!buffers->copy_from_device())
431                 return;
432
433         BufferParams& params = buffers->params;
434         float exposure = scene->film->exposure;
435
436         vector<float> pixels(params.width*params.height*4);
437
438         if (!do_update_only) {
439                 /* copy each pass */
440                 BL::RenderLayer::passes_iterator b_iter;
441
442                 for(b_rlay.passes.begin(b_iter); b_iter != b_rlay.passes.end(); ++b_iter) {
443                         BL::RenderPass b_pass(*b_iter);
444
445                         /* find matching pass type */
446                         PassType pass_type = get_pass_type(b_pass);
447                         int components = b_pass.channels();
448
449                         /* copy pixels */
450                         if(!buffers->get_pass_rect(pass_type, exposure, rtile.sample, components, &pixels[0]))
451                                 memset(&pixels[0], 0, pixels.size()*sizeof(float));
452
453                         b_pass.rect(&pixels[0]);
454                 }
455         }
456
457         /* copy combined pass */
458         if(buffers->get_pass_rect(PASS_COMBINED, exposure, rtile.sample, 4, &pixels[0]))
459                 b_rlay.rect(&pixels[0]);
460
461         /* tag result as updated */
462         b_engine.update_result(b_rr);
463 }
464
465 void BlenderSession::write_render_result(BL::RenderResult b_rr, BL::RenderLayer b_rlay, RenderTile& rtile)
466 {
467         do_write_update_render_result(b_rr, b_rlay, rtile, false);
468 }
469
470 void BlenderSession::update_render_result(BL::RenderResult b_rr, BL::RenderLayer b_rlay, RenderTile& rtile)
471 {
472         do_write_update_render_result(b_rr, b_rlay, rtile, true);
473 }
474
475 void BlenderSession::synchronize()
476 {
477         /* only used for viewport render */
478         if(!b_v3d)
479                 return;
480
481         /* on session/scene parameter changes, we recreate session entirely */
482         SceneParams scene_params = BlenderSync::get_scene_params(b_scene, background);
483         SessionParams session_params = BlenderSync::get_session_params(b_engine, b_userpref, b_scene, background);
484
485         if(session->params.modified(session_params) ||
486            scene->params.modified(scene_params))
487         {
488                 free_session();
489                 create_session();
490                 session->start();
491                 return;
492         }
493
494         /* increase samples, but never decrease */
495         session->set_samples(session_params.samples);
496         session->set_pause(BlenderSync::get_session_pause(b_scene, background));
497
498         /* copy recalc flags, outside of mutex so we can decide to do the real
499          * synchronization at a later time to not block on running updates */
500         sync->sync_recalc();
501
502         /* try to acquire mutex. if we don't want to or can't, come back later */
503         if(!session->ready_to_reset() || !session->scene->mutex.try_lock()) {
504                 tag_update();
505                 return;
506         }
507
508         /* data and camera synchronize */
509         sync->sync_data(b_v3d, b_engine.camera_override());
510
511         if(b_rv3d)
512                 sync->sync_view(b_v3d, b_rv3d, width, height);
513         else
514                 sync->sync_camera(b_render, b_engine.camera_override(), width, height);
515
516         /* unlock */
517         session->scene->mutex.unlock();
518
519         /* reset if needed */
520         if(scene->need_reset()) {
521                 BufferParams buffer_params = BlenderSync::get_buffer_params(b_render, b_scene, b_v3d, b_rv3d, scene->camera, width, height);
522                 session->reset(buffer_params, session_params.samples);
523
524                 /* reset time */
525                 start_resize_time = 0.0;
526         }
527 }
528
529 bool BlenderSession::draw(int w, int h)
530 {
531         /* pause in redraw in case update is not being called due to final render */
532         session->set_pause(BlenderSync::get_session_pause(b_scene, background));
533
534         /* before drawing, we verify camera and viewport size changes, because
535          * we do not get update callbacks for those, we must detect them here */
536         if(session->ready_to_reset()) {
537                 bool reset = false;
538
539                 /* if dimensions changed, reset */
540                 if(width != w || height != h) {
541                         if(start_resize_time == 0.0) {
542                                 /* don't react immediately to resizes to avoid flickery resizing
543                                  * of the viewport, and some window managers changing the window
544                                  * size temporarily on unminimize */
545                                 start_resize_time = time_dt();
546                                 tag_redraw();
547                         }
548                         else if(time_dt() - start_resize_time < 0.2) {
549                                 tag_redraw();
550                         }
551                         else {
552                                 width = w;
553                                 height = h;
554                                 reset = true;
555                         }
556                 }
557
558                 /* try to acquire mutex. if we can't, come back later */
559                 if(!session->scene->mutex.try_lock()) {
560                         tag_update();
561                 }
562                 else {
563                         /* update camera from 3d view */
564
565                         sync->sync_view(b_v3d, b_rv3d, width, height);
566
567                         if(scene->camera->need_update)
568                                 reset = true;
569
570                         session->scene->mutex.unlock();
571                 }
572
573                 /* reset if requested */
574                 if(reset) {
575                         SessionParams session_params = BlenderSync::get_session_params(b_engine, b_userpref, b_scene, background);
576                         BufferParams buffer_params = BlenderSync::get_buffer_params(b_render, b_scene, b_v3d, b_rv3d, scene->camera, width, height);
577
578                         session->reset(buffer_params, session_params.samples);
579
580                         start_resize_time = 0.0;
581                 }
582         }
583         else {
584                 tag_update();
585         }
586
587         /* update status and progress for 3d view draw */
588         update_status_progress();
589
590         /* draw */
591         BufferParams buffer_params = BlenderSync::get_buffer_params(b_render, b_scene, b_v3d, b_rv3d, scene->camera, width, height);
592
593         if(session->params.display_buffer_linear)
594                 b_engine.bind_display_space_shader(b_scene);
595
596         bool draw_ok = !session->draw(buffer_params);
597
598         if(session->params.display_buffer_linear)
599                 b_engine.unbind_display_space_shader();
600         
601         return draw_ok;
602 }
603
604 void BlenderSession::get_status(string& status, string& substatus)
605 {
606         session->progress.get_status(status, substatus);
607 }
608
609 void BlenderSession::get_progress(float& progress, double& total_time)
610 {
611         double tile_time;
612         int tile, sample, samples_per_tile;
613         int tile_total = session->tile_manager.state.num_tiles;
614
615         session->progress.get_tile(tile, total_time, tile_time);
616
617         sample = session->progress.get_sample();
618         samples_per_tile = session->tile_manager.num_samples;
619
620         if(samples_per_tile && tile_total)
621                 progress = ((float)sample / (float)(tile_total * samples_per_tile));
622         else
623                 progress = 0.0;
624 }
625
626 void BlenderSession::update_status_progress()
627 {
628         string timestatus, status, substatus;
629         string scene = "";
630         float progress;
631         double total_time, remaining_time = 0;
632         char time_str[128];
633         float mem_used = (float)session->stats.mem_used / 1024.0f / 1024.0f;
634         float mem_peak = (float)session->stats.mem_peak / 1024.0f / 1024.0f;
635         int samples = session->tile_manager.state.sample + 1;
636         int total_samples = session->tile_manager.num_samples;
637
638         get_status(status, substatus);
639         get_progress(progress, total_time);
640
641         
642
643         if(background) {
644                 if(progress>0)
645                         remaining_time = (1-progress) * (total_time / progress);
646
647                 scene += " | " + b_scene.name();
648                 if(b_rlay_name != "")
649                         scene += ", "  + b_rlay_name;
650         }
651         else {
652                 BLI_timestr(total_time, time_str, sizeof(time_str));
653                 timestatus = "Time:" + string(time_str) + " | ";
654
655                 if(samples > 0 && total_samples != USHRT_MAX)
656                         remaining_time = (total_samples - samples) * (total_time / samples);
657         }
658         
659         if(remaining_time>0) {
660                 BLI_timestr(remaining_time, time_str, sizeof(time_str));
661                 timestatus += "Remaining:" + string(time_str) + " | ";
662         }
663         
664         timestatus += string_printf("Mem:%.2fM, Peak:%.2fM", mem_used, mem_peak);
665
666         if(status.size() > 0)
667                 status = " | " + status;
668         if(substatus.size() > 0)
669                 status += " | " + substatus;
670
671         if(status != last_status) {
672                 b_engine.update_stats("", (timestatus + scene + status).c_str());
673                 b_engine.update_memory_stats(mem_used, mem_peak);
674                 last_status = status;
675         }
676         if(progress != last_progress) {
677                 b_engine.update_progress(progress);
678                 last_progress = progress;
679         }
680 }
681
682 void BlenderSession::tag_update()
683 {
684         /* tell blender that we want to get another update callback */
685         b_engine.tag_update();
686 }
687
688 void BlenderSession::tag_redraw()
689 {
690         if(background) {
691                 /* update stats and progress, only for background here because
692                  * in 3d view we do it in draw for thread safety reasons */
693                 update_status_progress();
694
695                 /* offline render, redraw if timeout passed */
696                 if(time_dt() - last_redraw_time > 1.0) {
697                         b_engine.tag_redraw();
698                         last_redraw_time = time_dt();
699                 }
700         }
701         else {
702                 /* tell blender that we want to redraw */
703                 b_engine.tag_redraw();
704         }
705 }
706
707 void BlenderSession::test_cancel()
708 {
709         /* test if we need to cancel rendering */
710         if(background)
711                 if(b_engine.test_break())
712                         session->progress.set_cancel("Cancelled");
713 }
714
715 /* builtin image file name is actually an image datablock name with
716  * absolute sequence frame number concatenated via '@' character
717  *
718  * this function splits frame from builtin name
719  */
720 int BlenderSession::builtin_image_frame(const string &builtin_name)
721 {
722         int last = builtin_name.find_last_of('@');
723         return atoi(builtin_name.substr(last + 1, builtin_name.size() - last - 1).c_str());
724 }
725
726 void BlenderSession::builtin_image_info(const string &builtin_name, void *builtin_data, bool &is_float, int &width, int &height, int &channels)
727 {
728         PointerRNA ptr;
729         RNA_id_pointer_create((ID*)builtin_data, &ptr);
730         BL::Image b_image(ptr);
731
732         if(b_image) {
733                 is_float = b_image.is_float();
734                 width = b_image.size()[0];
735                 height = b_image.size()[1];
736                 channels = b_image.channels();
737         }
738         else {
739                 is_float = false;
740                 width = 0;
741                 height = 0;
742                 channels = 0;
743         }
744 }
745
746 bool BlenderSession::builtin_image_pixels(const string &builtin_name, void *builtin_data, unsigned char *pixels)
747 {
748         int frame = builtin_image_frame(builtin_name);
749
750         PointerRNA ptr;
751         RNA_id_pointer_create((ID*)builtin_data, &ptr);
752         BL::Image b_image(ptr);
753
754         if(b_image) {
755                 int width = b_image.size()[0];
756                 int height = b_image.size()[1];
757                 int channels = b_image.channels();
758
759                 unsigned char *image_pixels;
760                 image_pixels = image_get_pixels_for_frame(b_image, frame);
761
762                 if(image_pixels) {
763                         memcpy(pixels, image_pixels, width * height * channels * sizeof(unsigned char));
764                         MEM_freeN(image_pixels);
765                 }
766                 else {
767                         if(channels == 1) {
768                                 memset(pixels, 0, width * height * sizeof(unsigned char));
769                         }
770                         else {
771                                 unsigned char *cp = pixels;
772                                 for(int i = 0; i < width * height; i++, cp += channels) {
773                                         cp[0] = 255;
774                                         cp[1] = 0;
775                                         cp[2] = 255;
776                                         if(channels == 4)
777                                                 cp[3] = 255;
778                                 }
779                         }
780                 }
781
782                 /* premultiply, byte images are always straight for blender */
783                 unsigned char *cp = pixels;
784                 for(int i = 0; i < width * height; i++, cp += channels) {
785                         cp[0] = (cp[0] * cp[3]) >> 8;
786                         cp[1] = (cp[1] * cp[3]) >> 8;
787                         cp[2] = (cp[2] * cp[3]) >> 8;
788                 }
789
790                 return true;
791         }
792
793         return false;
794 }
795
796 bool BlenderSession::builtin_image_float_pixels(const string &builtin_name, void *builtin_data, float *pixels)
797 {
798         int frame = builtin_image_frame(builtin_name);
799
800         PointerRNA ptr;
801         RNA_id_pointer_create((ID*)builtin_data, &ptr);
802         BL::Image b_image(ptr);
803
804         if(b_image) {
805                 int width = b_image.size()[0];
806                 int height = b_image.size()[1];
807                 int channels = b_image.channels();
808
809                 float *image_pixels;
810                 image_pixels = image_get_float_pixels_for_frame(b_image, frame);
811
812                 if(image_pixels) {
813                         memcpy(pixels, image_pixels, width * height * channels * sizeof(float));
814                         MEM_freeN(image_pixels);
815                 }
816                 else {
817                         if(channels == 1) {
818                                 memset(pixels, 0, width * height * sizeof(float));
819                         }
820                         else {
821                                 float *fp = pixels;
822                                 for(int i = 0; i < width * height; i++, fp += channels) {
823                                         fp[0] = 1.0f;
824                                         fp[1] = 0.0f;
825                                         fp[2] = 1.0f;
826                                         if(channels == 4)
827                                                 fp[3] = 1.0f;
828                                 }
829                         }
830                 }
831
832                 return true;
833         }
834
835         return false;
836 }
837
838 CCL_NAMESPACE_END
839