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