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