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