Cycles / SSS:
[blender.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
289         /* layer will be missing if it was disabled in the UI */
290         if(b_single_rlay == b_rr.layers.end())
291                 return;
292
293         BL::RenderLayer b_rlay = *b_single_rlay;
294
295         if (do_update_only) {
296                 /* update only needed */
297
298                 if (rtile.sample != 0) {
299                         /* sample would be zero at initial tile update, which is only needed
300                          * to tag tile form blender side as IN PROGRESS for proper highlight
301                          * no buffers should be sent to blender yet
302                          */
303                         update_render_result(b_rr, b_rlay, rtile);
304                 }
305
306                 end_render_result(b_engine, b_rr, true);
307         }
308         else {
309                 /* write result */
310                 write_render_result(b_rr, b_rlay, rtile);
311                 end_render_result(b_engine, b_rr);
312         }
313 }
314
315 void BlenderSession::write_render_tile(RenderTile& rtile)
316 {
317         do_write_update_render_tile(rtile, false);
318 }
319
320 void BlenderSession::update_render_tile(RenderTile& rtile)
321 {
322         /* use final write for preview renders, otherwise render result wouldn't be
323          * be updated in blender side
324          * would need to be investigated a bit further, but for now shall be fine
325          */
326         if (!b_engine.is_preview())
327                 do_write_update_render_tile(rtile, true);
328         else
329                 do_write_update_render_tile(rtile, false);
330 }
331
332 void BlenderSession::render()
333 {
334         /* set callback to write out render results */
335         session->write_render_tile_cb = function_bind(&BlenderSession::write_render_tile, this, _1);
336         session->update_render_tile_cb = function_bind(&BlenderSession::update_render_tile, this, _1);
337
338         /* get buffer parameters */
339         SessionParams session_params = BlenderSync::get_session_params(b_engine, b_userpref, b_scene, background);
340         BufferParams buffer_params = BlenderSync::get_buffer_params(b_render, b_scene, b_v3d, b_rv3d, scene->camera, width, height);
341
342         /* render each layer */
343         BL::RenderSettings r = b_scene.render();
344         BL::RenderSettings::layers_iterator b_iter;
345         
346         for(r.layers.begin(b_iter); b_iter != r.layers.end(); ++b_iter) {
347                 b_rlay_name = b_iter->name();
348
349                 /* temporary render result to find needed passes */
350                 BL::RenderResult b_rr = begin_render_result(b_engine, 0, 0, 1, 1, b_rlay_name.c_str());
351                 BL::RenderResult::layers_iterator b_single_rlay;
352                 b_rr.layers.begin(b_single_rlay);
353
354                 /* layer will be missing if it was disabled in the UI */
355                 if(b_single_rlay == b_rr.layers.end()) {
356                         end_render_result(b_engine, b_rr, true);
357                         continue;
358                 }
359
360                 BL::RenderLayer b_rlay = *b_single_rlay;
361
362                 /* add passes */
363                 vector<Pass> passes;
364                 Pass::add(PASS_COMBINED, passes);
365
366                 if(session_params.device.advanced_shading) {
367
368                         /* loop over passes */
369                         BL::RenderLayer::passes_iterator b_pass_iter;
370
371                         for(b_rlay.passes.begin(b_pass_iter); b_pass_iter != b_rlay.passes.end(); ++b_pass_iter) {
372                                 BL::RenderPass b_pass(*b_pass_iter);
373                                 PassType pass_type = get_pass_type(b_pass);
374
375                                 if(pass_type == PASS_MOTION && scene->integrator->motion_blur)
376                                         continue;
377                                 if(pass_type != PASS_NONE)
378                                         Pass::add(pass_type, passes);
379                         }
380                 }
381
382                 /* free result without merging */
383                 end_render_result(b_engine, b_rr, true);
384
385                 buffer_params.passes = passes;
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         return !session->draw(buffer_params);
594 }
595
596 void BlenderSession::get_status(string& status, string& substatus)
597 {
598         session->progress.get_status(status, substatus);
599 }
600
601 void BlenderSession::get_progress(float& progress, double& total_time)
602 {
603         double tile_time;
604         int tile, sample, samples_per_tile;
605         int tile_total = session->tile_manager.state.num_tiles;
606
607         session->progress.get_tile(tile, total_time, tile_time);
608
609         sample = session->progress.get_sample();
610         samples_per_tile = session->tile_manager.num_samples;
611
612         if(samples_per_tile && tile_total)
613                 progress = ((float)sample / (float)(tile_total * samples_per_tile));
614         else
615                 progress = 0.0;
616 }
617
618 void BlenderSession::update_status_progress()
619 {
620         string timestatus, status, substatus;
621         float progress;
622         double total_time;
623         char time_str[128];
624         float mem_used = (float)session->stats.mem_used / 1024.0f / 1024.0f;
625         float mem_peak = (float)session->stats.mem_peak / 1024.0f / 1024.0f;
626
627         get_status(status, substatus);
628         get_progress(progress, total_time);
629
630         timestatus = string_printf("Mem:%.2fM, Peak:%.2fM", mem_used, mem_peak);
631
632         if(background) {
633                 timestatus += " | " + b_scene.name();
634                 if(b_rlay_name != "")
635                         timestatus += ", "  + b_rlay_name;
636         }
637         else {
638                 timestatus += " | ";
639
640                 BLI_timestr(total_time, time_str, sizeof(time_str));
641                 timestatus += "Time:" + string(time_str);
642         }
643
644         if(status.size() > 0)
645                 status = " | " + status;
646         if(substatus.size() > 0)
647                 status += " | " + substatus;
648
649         if(status != last_status) {
650                 b_engine.update_stats("", (timestatus + status).c_str());
651                 b_engine.update_memory_stats(mem_used, mem_peak);
652                 last_status = status;
653         }
654         if(progress != last_progress) {
655                 b_engine.update_progress(progress);
656                 last_progress = progress;
657         }
658 }
659
660 void BlenderSession::tag_update()
661 {
662         /* tell blender that we want to get another update callback */
663         b_engine.tag_update();
664 }
665
666 void BlenderSession::tag_redraw()
667 {
668         if(background) {
669                 /* update stats and progress, only for background here because
670                  * in 3d view we do it in draw for thread safety reasons */
671                 update_status_progress();
672
673                 /* offline render, redraw if timeout passed */
674                 if(time_dt() - last_redraw_time > 1.0) {
675                         b_engine.tag_redraw();
676                         last_redraw_time = time_dt();
677                 }
678         }
679         else {
680                 /* tell blender that we want to redraw */
681                 b_engine.tag_redraw();
682         }
683 }
684
685 void BlenderSession::test_cancel()
686 {
687         /* test if we need to cancel rendering */
688         if(background)
689                 if(b_engine.test_break())
690                         session->progress.set_cancel("Cancelled");
691 }
692
693 /* builtin image file name is actually an image datablock name with
694  * absolute sequence frame number concatenated via '@' character
695  *
696  * this function splits frame from builtin name
697  */
698 int BlenderSession::builtin_image_frame(const string &builtin_name)
699 {
700         int last = builtin_name.find_last_of('@');
701         return atoi(builtin_name.substr(last + 1, builtin_name.size() - last - 1).c_str());
702 }
703
704 void BlenderSession::builtin_image_info(const string &builtin_name, void *builtin_data, bool &is_float, int &width, int &height, int &channels)
705 {
706         PointerRNA ptr;
707         RNA_id_pointer_create((ID*)builtin_data, &ptr);
708         BL::Image b_image(ptr);
709
710         if(b_image) {
711                 is_float = b_image.is_float();
712                 width = b_image.size()[0];
713                 height = b_image.size()[1];
714                 channels = b_image.channels();
715         }
716         else {
717                 is_float = false;
718                 width = 0;
719                 height = 0;
720                 channels = 0;
721         }
722 }
723
724 bool BlenderSession::builtin_image_pixels(const string &builtin_name, void *builtin_data, unsigned char *pixels)
725 {
726         int frame = builtin_image_frame(builtin_name);
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                 int width = b_image.size()[0];
734                 int height = b_image.size()[1];
735                 int channels = b_image.channels();
736
737                 unsigned char *image_pixels;
738                 image_pixels = image_get_pixels_for_frame(b_image, frame);
739
740                 if(image_pixels) {
741                         memcpy(pixels, image_pixels, width * height * channels * sizeof(unsigned char));
742                         MEM_freeN(image_pixels);
743                 }
744                 else {
745                         if(channels == 1) {
746                                 memset(pixels, 0, width * height * sizeof(unsigned char));
747                         }
748                         else {
749                                 unsigned char *cp = pixels;
750                                 for(int i = 0; i < width * height; i++, cp += channels) {
751                                         cp[0] = 255;
752                                         cp[1] = 0;
753                                         cp[2] = 255;
754                                         if(channels == 4)
755                                                 cp[3] = 255;
756                                 }
757                         }
758                 }
759
760                 /* premultiply, byte images are always straight for blender */
761                 unsigned char *cp = pixels;
762                 for(int i = 0; i < width * height; i++, cp += channels) {
763                         cp[0] = (cp[0] * cp[3]) >> 8;
764                         cp[1] = (cp[1] * cp[3]) >> 8;
765                         cp[2] = (cp[2] * cp[3]) >> 8;
766                 }
767
768                 return true;
769         }
770
771         return false;
772 }
773
774 bool BlenderSession::builtin_image_float_pixels(const string &builtin_name, void *builtin_data, float *pixels)
775 {
776         int frame = builtin_image_frame(builtin_name);
777
778         PointerRNA ptr;
779         RNA_id_pointer_create((ID*)builtin_data, &ptr);
780         BL::Image b_image(ptr);
781
782         if(b_image) {
783                 int width = b_image.size()[0];
784                 int height = b_image.size()[1];
785                 int channels = b_image.channels();
786
787                 float *image_pixels;
788                 image_pixels = image_get_float_pixels_for_frame(b_image, frame);
789
790                 if(image_pixels) {
791                         memcpy(pixels, image_pixels, width * height * channels * sizeof(float));
792                         MEM_freeN(image_pixels);
793                 }
794                 else {
795                         if(channels == 1) {
796                                 memset(pixels, 0, width * height * sizeof(float));
797                         }
798                         else {
799                                 float *fp = pixels;
800                                 for(int i = 0; i < width * height; i++, fp += channels) {
801                                         fp[0] = 1.0f;
802                                         fp[1] = 0.0f;
803                                         fp[2] = 1.0f;
804                                         if(channels == 4)
805                                                 fp[3] = 1.0f;
806                                 }
807                         }
808                 }
809
810                 return true;
811         }
812
813         return false;
814 }
815
816 CCL_NAMESPACE_END
817