Render API/Cycles: Identify Render Passes by their name instead of a type flag
[blender-staging.git] / intern / cycles / blender / blender_session.cpp
1 /*
2  * Copyright 2011-2013 Blender Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include <stdlib.h>
18
19 #include "render/background.h"
20 #include "render/buffers.h"
21 #include "render/camera.h"
22 #include "device/device.h"
23 #include "render/integrator.h"
24 #include "render/film.h"
25 #include "render/light.h"
26 #include "render/mesh.h"
27 #include "render/object.h"
28 #include "render/scene.h"
29 #include "render/session.h"
30 #include "render/shader.h"
31
32 #include "util/util_color.h"
33 #include "util/util_foreach.h"
34 #include "util/util_function.h"
35 #include "util/util_hash.h"
36 #include "util/util_logging.h"
37 #include "util/util_progress.h"
38 #include "util/util_time.h"
39
40 #include "blender/blender_sync.h"
41 #include "blender/blender_session.h"
42 #include "blender/blender_util.h"
43
44 CCL_NAMESPACE_BEGIN
45
46 bool BlenderSession::headless = false;
47 int BlenderSession::num_resumable_chunks = 0;
48 int BlenderSession::current_resumable_chunk = 0;
49 int BlenderSession::start_resumable_chunk = 0;
50 int BlenderSession::end_resumable_chunk = 0;
51
52 BlenderSession::BlenderSession(BL::RenderEngine& b_engine,
53                                BL::UserPreferences& b_userpref,
54                                BL::BlendData& b_data,
55                                BL::Scene& b_scene)
56 : b_engine(b_engine),
57   b_userpref(b_userpref),
58   b_data(b_data),
59   b_render(b_engine.render()),
60   b_scene(b_scene),
61   b_v3d(PointerRNA_NULL),
62   b_rv3d(PointerRNA_NULL),
63   python_thread_state(NULL)
64 {
65         /* offline render */
66
67         width = render_resolution_x(b_render);
68         height = render_resolution_y(b_render);
69
70         background = true;
71         last_redraw_time = 0.0;
72         start_resize_time = 0.0;
73         last_status_time = 0.0;
74 }
75
76 BlenderSession::BlenderSession(BL::RenderEngine& b_engine,
77                                BL::UserPreferences& b_userpref,
78                                BL::BlendData& b_data,
79                                BL::Scene& b_scene,
80                                BL::SpaceView3D& b_v3d,
81                                BL::RegionView3D& b_rv3d,
82                                int width, int height)
83 : b_engine(b_engine),
84   b_userpref(b_userpref),
85   b_data(b_data),
86   b_render(b_scene.render()),
87   b_scene(b_scene),
88   b_v3d(b_v3d),
89   b_rv3d(b_rv3d),
90   width(width),
91   height(height),
92   python_thread_state(NULL)
93 {
94         /* 3d view render */
95
96         background = false;
97         last_redraw_time = 0.0;
98         start_resize_time = 0.0;
99         last_status_time = 0.0;
100 }
101
102 BlenderSession::~BlenderSession()
103 {
104         free_session();
105 }
106
107 void BlenderSession::create()
108 {
109         create_session();
110
111         if(b_v3d)
112                 session->start();
113 }
114
115 void BlenderSession::create_session()
116 {
117         SessionParams session_params = BlenderSync::get_session_params(b_engine, b_userpref, b_scene, background);
118         bool is_cpu = session_params.device.type == DEVICE_CPU;
119         SceneParams scene_params = BlenderSync::get_scene_params(b_scene, background, is_cpu);
120         bool session_pause = BlenderSync::get_session_pause(b_scene, background);
121
122         /* reset status/progress */
123         last_status = "";
124         last_error = "";
125         last_progress = -1.0f;
126         start_resize_time = 0.0;
127
128         /* create scene */
129         scene = new Scene(scene_params, session_params.device);
130
131         /* setup callbacks for builtin image support */
132         scene->image_manager->builtin_image_info_cb = function_bind(&BlenderSession::builtin_image_info, this, _1, _2, _3, _4, _5, _6, _7);
133         scene->image_manager->builtin_image_pixels_cb = function_bind(&BlenderSession::builtin_image_pixels, this, _1, _2, _3, _4);
134         scene->image_manager->builtin_image_float_pixels_cb = function_bind(&BlenderSession::builtin_image_float_pixels, this, _1, _2, _3, _4);
135
136         /* create session */
137         session = new Session(session_params);
138         session->scene = scene;
139         session->progress.set_update_callback(function_bind(&BlenderSession::tag_redraw, this));
140         session->progress.set_cancel_callback(function_bind(&BlenderSession::test_cancel, this));
141         session->set_pause(session_pause);
142
143         /* create sync */
144         sync = new BlenderSync(b_engine, b_data, b_scene, scene, !background, session->progress, is_cpu);
145         BL::Object b_camera_override(b_engine.camera_override());
146         if(b_v3d) {
147                 if(session_pause == false) {
148                         /* full data sync */
149                         sync->sync_view(b_v3d, b_rv3d, width, height);
150                         sync->sync_data(b_render,
151                                         b_v3d,
152                                         b_camera_override,
153                                         width, height,
154                                         &python_thread_state,
155                                         b_rlay_name.c_str());
156                 }
157         }
158         else {
159                 /* for final render we will do full data sync per render layer, only
160                  * do some basic syncing here, no objects or materials for speed */
161                 sync->sync_render_layers(b_v3d, NULL);
162                 sync->sync_integrator();
163                 sync->sync_camera(b_render, b_camera_override, width, height, "");
164         }
165
166         /* set buffer parameters */
167         BufferParams buffer_params = BlenderSync::get_buffer_params(b_render, b_v3d, b_rv3d, scene->camera, width, height);
168         session->reset(buffer_params, session_params.samples);
169
170         b_engine.use_highlight_tiles(session_params.progressive_refine == false);
171
172         update_resumable_tile_manager(session_params.samples);
173 }
174
175 void BlenderSession::reset_session(BL::BlendData& b_data_, BL::Scene& b_scene_)
176 {
177         b_data = b_data_;
178         b_render = b_engine.render();
179         b_scene = b_scene_;
180
181         SessionParams session_params = BlenderSync::get_session_params(b_engine, b_userpref, b_scene, background);
182         const bool is_cpu = session_params.device.type == DEVICE_CPU;
183         SceneParams scene_params = BlenderSync::get_scene_params(b_scene, background, is_cpu);
184
185         width = render_resolution_x(b_render);
186         height = render_resolution_y(b_render);
187
188         if(scene->params.modified(scene_params) ||
189            session->params.modified(session_params) ||
190            !scene_params.persistent_data)
191         {
192                 /* if scene or session parameters changed, it's easier to simply re-create
193                  * them rather than trying to distinguish which settings need to be updated
194                  */
195
196                 delete session;
197
198                 create_session();
199
200                 return;
201         }
202
203         session->progress.reset();
204         scene->reset();
205
206         session->tile_manager.set_tile_order(session_params.tile_order);
207
208         /* peak memory usage should show current render peak, not peak for all renders
209          * made by this render session
210          */
211         session->stats.mem_peak = session->stats.mem_used;
212
213         /* sync object should be re-created */
214         sync = new BlenderSync(b_engine, b_data, b_scene, scene, !background, session->progress, is_cpu);
215
216         /* for final render we will do full data sync per render layer, only
217          * do some basic syncing here, no objects or materials for speed */
218         BL::Object b_camera_override(b_engine.camera_override());
219         sync->sync_render_layers(b_v3d, NULL);
220         sync->sync_integrator();
221         sync->sync_camera(b_render, b_camera_override, width, height, "");
222
223         BL::SpaceView3D b_null_space_view3d(PointerRNA_NULL);
224         BL::RegionView3D b_null_region_view3d(PointerRNA_NULL);
225         BufferParams buffer_params = BlenderSync::get_buffer_params(b_render,
226                                                                     b_null_space_view3d,
227                                                                     b_null_region_view3d,
228                                                                     scene->camera,
229                                                                     width, height);
230         session->reset(buffer_params, session_params.samples);
231
232         b_engine.use_highlight_tiles(session_params.progressive_refine == false);
233
234         /* reset time */
235         start_resize_time = 0.0;
236 }
237
238 void BlenderSession::free_session()
239 {
240         if(sync)
241                 delete sync;
242
243         delete session;
244 }
245
246 static ShaderEvalType get_shader_type(const string& pass_type)
247 {
248         const char *shader_type = pass_type.c_str();
249
250         /* data passes */
251         if(strcmp(shader_type, "NORMAL")==0)
252                 return SHADER_EVAL_NORMAL;
253         else if(strcmp(shader_type, "UV")==0)
254                 return SHADER_EVAL_UV;
255         else if(strcmp(shader_type, "DIFFUSE_COLOR")==0)
256                 return SHADER_EVAL_DIFFUSE_COLOR;
257         else if(strcmp(shader_type, "GLOSSY_COLOR")==0)
258                 return SHADER_EVAL_GLOSSY_COLOR;
259         else if(strcmp(shader_type, "TRANSMISSION_COLOR")==0)
260                 return SHADER_EVAL_TRANSMISSION_COLOR;
261         else if(strcmp(shader_type, "SUBSURFACE_COLOR")==0)
262                 return SHADER_EVAL_SUBSURFACE_COLOR;
263         else if(strcmp(shader_type, "EMIT")==0)
264                 return SHADER_EVAL_EMISSION;
265
266         /* light passes */
267         else if(strcmp(shader_type, "AO")==0)
268                 return SHADER_EVAL_AO;
269         else if(strcmp(shader_type, "COMBINED")==0)
270                 return SHADER_EVAL_COMBINED;
271         else if(strcmp(shader_type, "SHADOW")==0)
272                 return SHADER_EVAL_SHADOW;
273         else if(strcmp(shader_type, "DIFFUSE")==0)
274                 return SHADER_EVAL_DIFFUSE;
275         else if(strcmp(shader_type, "GLOSSY")==0)
276                 return SHADER_EVAL_GLOSSY;
277         else if(strcmp(shader_type, "TRANSMISSION")==0)
278                 return SHADER_EVAL_TRANSMISSION;
279         else if(strcmp(shader_type, "SUBSURFACE")==0)
280                 return SHADER_EVAL_SUBSURFACE;
281
282         /* extra */
283         else if(strcmp(shader_type, "ENVIRONMENT")==0)
284                 return SHADER_EVAL_ENVIRONMENT;
285
286         else
287                 return SHADER_EVAL_BAKE;
288 }
289
290 static BL::RenderResult begin_render_result(BL::RenderEngine& b_engine,
291                                             int x, int y,
292                                             int w, int h,
293                                             const char *layername,
294                                             const char *viewname)
295 {
296         return b_engine.begin_result(x, y, w, h, layername, viewname);
297 }
298
299 static void end_render_result(BL::RenderEngine& b_engine,
300                               BL::RenderResult& b_rr,
301                               bool cancel,
302                               bool do_merge_results)
303 {
304         b_engine.end_result(b_rr, (int)cancel, (int)do_merge_results);
305 }
306
307 void BlenderSession::do_write_update_render_tile(RenderTile& rtile, bool do_update_only)
308 {
309         BufferParams& params = rtile.buffers->params;
310         int x = params.full_x - session->tile_manager.params.full_x;
311         int y = params.full_y - session->tile_manager.params.full_y;
312         int w = params.width;
313         int h = params.height;
314
315         /* get render result */
316         BL::RenderResult b_rr = begin_render_result(b_engine, x, y, w, h, b_rlay_name.c_str(), b_rview_name.c_str());
317
318         /* can happen if the intersected rectangle gives 0 width or height */
319         if(b_rr.ptr.data == NULL) {
320                 return;
321         }
322
323         BL::RenderResult::layers_iterator b_single_rlay;
324         b_rr.layers.begin(b_single_rlay);
325
326         /* layer will be missing if it was disabled in the UI */
327         if(b_single_rlay == b_rr.layers.end())
328                 return;
329
330         BL::RenderLayer b_rlay = *b_single_rlay;
331
332         if(do_update_only) {
333                 /* update only needed */
334
335                 if(rtile.sample != 0) {
336                         /* sample would be zero at initial tile update, which is only needed
337                          * to tag tile form blender side as IN PROGRESS for proper highlight
338                          * no buffers should be sent to blender yet
339                          */
340                         update_render_result(b_rr, b_rlay, rtile);
341                 }
342
343                 end_render_result(b_engine, b_rr, true, true);
344         }
345         else {
346                 /* write result */
347                 write_render_result(b_rr, b_rlay, rtile);
348                 end_render_result(b_engine, b_rr, false, true);
349         }
350 }
351
352 void BlenderSession::write_render_tile(RenderTile& rtile)
353 {
354         do_write_update_render_tile(rtile, false);
355 }
356
357 void BlenderSession::update_render_tile(RenderTile& rtile)
358 {
359         /* use final write for preview renders, otherwise render result wouldn't be
360          * be updated in blender side
361          * would need to be investigated a bit further, but for now shall be fine
362          */
363         if(!b_engine.is_preview())
364                 do_write_update_render_tile(rtile, true);
365         else
366                 do_write_update_render_tile(rtile, false);
367 }
368
369 void BlenderSession::render()
370 {
371         /* set callback to write out render results */
372         session->write_render_tile_cb = function_bind(&BlenderSession::write_render_tile, this, _1);
373         session->update_render_tile_cb = function_bind(&BlenderSession::update_render_tile, this, _1);
374
375         /* get buffer parameters */
376         SessionParams session_params = BlenderSync::get_session_params(b_engine, b_userpref, b_scene, background);
377         BufferParams buffer_params = BlenderSync::get_buffer_params(b_render, b_v3d, b_rv3d, scene->camera, width, height);
378
379         /* render each layer */
380         BL::RenderSettings r = b_scene.render();
381         BL::RenderSettings::layers_iterator b_layer_iter;
382         BL::RenderResult::views_iterator b_view_iter;
383         
384         for(r.layers.begin(b_layer_iter); b_layer_iter != r.layers.end(); ++b_layer_iter) {
385                 b_rlay_name = b_layer_iter->name();
386
387                 /* temporary render result to find needed passes and views */
388                 BL::RenderResult b_rr = begin_render_result(b_engine, 0, 0, 1, 1, b_rlay_name.c_str(), NULL);
389                 BL::RenderResult::layers_iterator b_single_rlay;
390                 b_rr.layers.begin(b_single_rlay);
391
392                 /* layer will be missing if it was disabled in the UI */
393                 if(b_single_rlay == b_rr.layers.end()) {
394                         end_render_result(b_engine, b_rr, true, false);
395                         continue;
396                 }
397
398                 BL::RenderLayer b_rlay = *b_single_rlay;
399
400                 /* add passes */
401                 array<Pass> passes;
402                 if(session_params.device.advanced_shading) {
403                         passes = sync->sync_render_passes(b_rlay, *b_layer_iter);
404                 }
405                 else {
406                         Pass::add(PASS_COMBINED, passes);
407                 }
408
409                 buffer_params.passes = passes;
410                 scene->film->pass_alpha_threshold = b_layer_iter->pass_alpha_threshold();
411                 scene->film->tag_passes_update(scene, passes);
412                 scene->film->tag_update(scene);
413                 scene->integrator->tag_update(scene);
414
415                 int view_index = 0;
416                 for(b_rr.views.begin(b_view_iter); b_view_iter != b_rr.views.end(); ++b_view_iter, ++view_index) {
417                         b_rview_name = b_view_iter->name();
418
419                         /* set the current view */
420                         b_engine.active_view_set(b_rview_name.c_str());
421
422                         /* update scene */
423                         BL::Object b_camera_override(b_engine.camera_override());
424                         sync->sync_camera(b_render, b_camera_override, width, height, b_rview_name.c_str());
425                         sync->sync_data(b_render,
426                                         b_v3d,
427                                         b_camera_override,
428                                         width, height,
429                                         &python_thread_state,
430                                         b_rlay_name.c_str());
431
432                         /* Make sure all views have different noise patterns. - hardcoded value just to make it random */
433                         if(view_index != 0) {
434                                 scene->integrator->seed += hash_int_2d(scene->integrator->seed, hash_int(view_index * 0xdeadbeef));
435                                 scene->integrator->tag_update(scene);
436                         }
437
438                         /* Update number of samples per layer. */
439                         int samples = sync->get_layer_samples();
440                         bool bound_samples = sync->get_layer_bound_samples();
441                         int effective_layer_samples;
442
443                         if(samples != 0 && (!bound_samples || (samples < session_params.samples)))
444                                 effective_layer_samples = samples;
445                         else
446                                 effective_layer_samples = session_params.samples;
447
448                         /* Update tile manager if we're doing resumable render. */
449                         update_resumable_tile_manager(effective_layer_samples);
450
451                         /* Update session itself. */
452                         session->reset(buffer_params, effective_layer_samples);
453
454                         /* render */
455                         session->start();
456                         session->wait();
457
458                         if(session->progress.get_cancel())
459                                 break;
460                 }
461
462                 /* free result without merging */
463                 end_render_result(b_engine, b_rr, true, false);
464
465                 if(session->progress.get_cancel())
466                         break;
467         }
468
469         double total_time, render_time;
470         session->progress.get_time(total_time, render_time);
471         VLOG(1) << "Total render time: " << total_time;
472         VLOG(1) << "Render time (without synchronization): " << render_time;
473
474         /* clear callback */
475         session->write_render_tile_cb = function_null;
476         session->update_render_tile_cb = function_null;
477
478         /* free all memory used (host and device), so we wouldn't leave render
479          * engine with extra memory allocated
480          */
481
482         session->device_free();
483
484         delete sync;
485         sync = NULL;
486 }
487
488 static void populate_bake_data(BakeData *data, const
489                                int object_id,
490                                BL::BakePixel& pixel_array,
491                                const int num_pixels)
492 {
493         BL::BakePixel bp = pixel_array;
494
495         int i;
496         for(i = 0; i < num_pixels; i++) {
497                 if(bp.object_id() == object_id) {
498                         data->set(i, bp.primitive_id(), bp.uv(), bp.du_dx(), bp.du_dy(), bp.dv_dx(), bp.dv_dy());
499                 } else {
500                         data->set_null(i);
501                 }
502                 bp = bp.next();
503         }
504 }
505
506 static int bake_pass_filter_get(const int pass_filter)
507 {
508         int flag = BAKE_FILTER_NONE;
509
510         if((pass_filter & BL::BakeSettings::pass_filter_DIRECT) != 0)
511                 flag |= BAKE_FILTER_DIRECT;
512         if((pass_filter & BL::BakeSettings::pass_filter_INDIRECT) != 0)
513                 flag |= BAKE_FILTER_INDIRECT;
514         if((pass_filter & BL::BakeSettings::pass_filter_COLOR) != 0)
515                 flag |= BAKE_FILTER_COLOR;
516
517         if((pass_filter & BL::BakeSettings::pass_filter_DIFFUSE) != 0)
518                 flag |= BAKE_FILTER_DIFFUSE;
519         if((pass_filter & BL::BakeSettings::pass_filter_GLOSSY) != 0)
520                 flag |= BAKE_FILTER_GLOSSY;
521         if((pass_filter & BL::BakeSettings::pass_filter_TRANSMISSION) != 0)
522                 flag |= BAKE_FILTER_TRANSMISSION;
523         if((pass_filter & BL::BakeSettings::pass_filter_SUBSURFACE) != 0)
524                 flag |= BAKE_FILTER_SUBSURFACE;
525
526         if((pass_filter & BL::BakeSettings::pass_filter_EMIT) != 0)
527                 flag |= BAKE_FILTER_EMISSION;
528         if((pass_filter & BL::BakeSettings::pass_filter_AO) != 0)
529                 flag |= BAKE_FILTER_AO;
530
531         return flag;
532 }
533
534 void BlenderSession::bake(BL::Object& b_object,
535                           const string& pass_type,
536                           const int pass_filter,
537                           const int object_id,
538                           BL::BakePixel& pixel_array,
539                           const size_t num_pixels,
540                           const int /*depth*/,
541                           float result[])
542 {
543         ShaderEvalType shader_type = get_shader_type(pass_type);
544         size_t object_index = OBJECT_NONE;
545         int tri_offset = 0;
546
547         /* Set baking flag in advance, so kernel loading can check if we need
548          * any baking capabilities.
549          */
550         scene->bake_manager->set_baking(true);
551
552         /* ensure kernels are loaded before we do any scene updates */
553         session->load_kernels();
554
555         if(session->progress.get_cancel())
556                 return;
557
558         if(shader_type == SHADER_EVAL_UV) {
559                 /* force UV to be available */
560                 Pass::add(PASS_UV, scene->film->passes);
561         }
562
563         int bake_pass_filter = bake_pass_filter_get(pass_filter);
564         bake_pass_filter = BakeManager::shader_type_to_pass_filter(shader_type, bake_pass_filter);
565
566         /* force use_light_pass to be true if we bake more than just colors */
567         if(bake_pass_filter & ~BAKE_FILTER_COLOR) {
568                 Pass::add(PASS_LIGHT, scene->film->passes);
569         }
570
571         /* create device and update scene */
572         scene->film->tag_update(scene);
573         scene->integrator->tag_update(scene);
574
575         /* update scene */
576         BL::Object b_camera_override(b_engine.camera_override());
577         sync->sync_camera(b_render, b_camera_override, width, height, "");
578         sync->sync_data(b_render,
579                         b_v3d,
580                         b_camera_override,
581                         width, height,
582                         &python_thread_state,
583                         b_rlay_name.c_str());
584
585         /* get buffer parameters */
586         SessionParams session_params = BlenderSync::get_session_params(b_engine, b_userpref, b_scene, background);
587         BufferParams buffer_params = BlenderSync::get_buffer_params(b_render, b_v3d, b_rv3d, scene->camera, width, height);
588
589         scene->bake_manager->set_shader_limit((size_t)b_engine.tile_x(), (size_t)b_engine.tile_y());
590
591         /* set number of samples */
592         session->tile_manager.set_samples(session_params.samples);
593         session->reset(buffer_params, session_params.samples);
594         session->update_scene();
595
596         /* find object index. todo: is arbitrary - copied from mesh_displace.cpp */
597         for(size_t i = 0; i < scene->objects.size(); i++) {
598                 if(strcmp(scene->objects[i]->name.c_str(), b_object.name().c_str()) == 0) {
599                         object_index = i;
600                         tri_offset = scene->objects[i]->mesh->tri_offset;
601                         break;
602                 }
603         }
604
605         int object = object_index;
606
607         BakeData *bake_data = scene->bake_manager->init(object, tri_offset, num_pixels);
608
609         populate_bake_data(bake_data, object_id, pixel_array, num_pixels);
610
611         /* set number of samples */
612         session->tile_manager.set_samples(session_params.samples);
613         session->reset(buffer_params, session_params.samples);
614         session->update_scene();
615
616         session->progress.set_update_callback(function_bind(&BlenderSession::update_bake_progress, this));
617
618         scene->bake_manager->bake(scene->device, &scene->dscene, scene, session->progress, shader_type, bake_pass_filter, bake_data, result);
619
620         /* free all memory used (host and device), so we wouldn't leave render
621          * engine with extra memory allocated
622          */
623
624         session->device_free();
625
626         delete sync;
627         sync = NULL;
628 }
629
630 void BlenderSession::do_write_update_render_result(BL::RenderResult& b_rr,
631                                                    BL::RenderLayer& b_rlay,
632                                                    RenderTile& rtile,
633                                                    bool do_update_only)
634 {
635         RenderBuffers *buffers = rtile.buffers;
636
637         /* copy data from device */
638         if(!buffers->copy_from_device())
639                 return;
640
641         BufferParams& params = buffers->params;
642         float exposure = scene->film->exposure;
643
644         vector<float> pixels(params.width*params.height*4);
645
646         /* Adjust absolute sample number to the range. */
647         int sample = rtile.sample;
648         const int range_start_sample = session->tile_manager.range_start_sample;
649         if(range_start_sample != -1) {
650                 sample -= range_start_sample;
651         }
652
653         if(!do_update_only) {
654                 /* copy each pass */
655                 BL::RenderLayer::passes_iterator b_iter;
656
657                 for(b_rlay.passes.begin(b_iter); b_iter != b_rlay.passes.end(); ++b_iter) {
658                         BL::RenderPass b_pass(*b_iter);
659
660                         /* find matching pass type */
661                         PassType pass_type = BlenderSync::get_pass_type(b_pass);
662                         int components = b_pass.channels();
663
664                         bool read = false;
665                         if(pass_type != PASS_NONE) {
666                                 /* copy pixels */
667                                 read = buffers->get_pass_rect(pass_type, exposure, sample, components, &pixels[0]);
668                         }
669
670                         if(!read) {
671                                 memset(&pixels[0], 0, pixels.size()*sizeof(float));
672                         }
673
674                         b_pass.rect(&pixels[0]);
675                 }
676         }
677         else {
678                 /* copy combined pass */
679                 BL::RenderPass b_combined_pass(b_rlay.passes.find_by_name("Combined", b_rview_name.c_str()));
680                 if(buffers->get_pass_rect(PASS_COMBINED, exposure, sample, 4, &pixels[0]))
681                         b_combined_pass.rect(&pixels[0]);
682         }
683
684         /* tag result as updated */
685         b_engine.update_result(b_rr);
686 }
687
688 void BlenderSession::write_render_result(BL::RenderResult& b_rr,
689                                          BL::RenderLayer& b_rlay,
690                                          RenderTile& rtile)
691 {
692         do_write_update_render_result(b_rr, b_rlay, rtile, false);
693 }
694
695 void BlenderSession::update_render_result(BL::RenderResult& b_rr,
696                                           BL::RenderLayer& b_rlay,
697                                           RenderTile& rtile)
698 {
699         do_write_update_render_result(b_rr, b_rlay, rtile, true);
700 }
701
702 void BlenderSession::synchronize()
703 {
704         /* only used for viewport render */
705         if(!b_v3d)
706                 return;
707
708         /* on session/scene parameter changes, we recreate session entirely */
709         SessionParams session_params = BlenderSync::get_session_params(b_engine, b_userpref, b_scene, background);
710         const bool is_cpu = session_params.device.type == DEVICE_CPU;
711         SceneParams scene_params = BlenderSync::get_scene_params(b_scene, background, is_cpu);
712         bool session_pause = BlenderSync::get_session_pause(b_scene, background);
713
714         if(session->params.modified(session_params) ||
715            scene->params.modified(scene_params))
716         {
717                 free_session();
718                 create_session();
719                 session->start();
720                 return;
721         }
722
723         /* increase samples, but never decrease */
724         session->set_samples(session_params.samples);
725         session->set_pause(session_pause);
726
727         /* copy recalc flags, outside of mutex so we can decide to do the real
728          * synchronization at a later time to not block on running updates */
729         sync->sync_recalc();
730
731         /* don't do synchronization if on pause */
732         if(session_pause) {
733                 tag_update();
734                 return;
735         }
736
737         /* try to acquire mutex. if we don't want to or can't, come back later */
738         if(!session->ready_to_reset() || !session->scene->mutex.try_lock()) {
739                 tag_update();
740                 return;
741         }
742
743         /* data and camera synchronize */
744         BL::Object b_camera_override(b_engine.camera_override());
745         sync->sync_data(b_render,
746                         b_v3d,
747                         b_camera_override,
748                         width, height,
749                         &python_thread_state,
750                         b_rlay_name.c_str());
751
752         if(b_rv3d)
753                 sync->sync_view(b_v3d, b_rv3d, width, height);
754         else
755                 sync->sync_camera(b_render, b_camera_override, width, height, "");
756
757         /* unlock */
758         session->scene->mutex.unlock();
759
760         /* reset if needed */
761         if(scene->need_reset()) {
762                 BufferParams buffer_params = BlenderSync::get_buffer_params(b_render, b_v3d, b_rv3d, scene->camera, width, height);
763                 session->reset(buffer_params, session_params.samples);
764
765                 /* reset time */
766                 start_resize_time = 0.0;
767         }
768 }
769
770 bool BlenderSession::draw(int w, int h)
771 {
772         /* pause in redraw in case update is not being called due to final render */
773         session->set_pause(BlenderSync::get_session_pause(b_scene, background));
774
775         /* before drawing, we verify camera and viewport size changes, because
776          * we do not get update callbacks for those, we must detect them here */
777         if(session->ready_to_reset()) {
778                 bool reset = false;
779
780                 /* if dimensions changed, reset */
781                 if(width != w || height != h) {
782                         if(start_resize_time == 0.0) {
783                                 /* don't react immediately to resizes to avoid flickery resizing
784                                  * of the viewport, and some window managers changing the window
785                                  * size temporarily on unminimize */
786                                 start_resize_time = time_dt();
787                                 tag_redraw();
788                         }
789                         else if(time_dt() - start_resize_time < 0.2) {
790                                 tag_redraw();
791                         }
792                         else {
793                                 width = w;
794                                 height = h;
795                                 reset = true;
796                         }
797                 }
798
799                 /* try to acquire mutex. if we can't, come back later */
800                 if(!session->scene->mutex.try_lock()) {
801                         tag_update();
802                 }
803                 else {
804                         /* update camera from 3d view */
805
806                         sync->sync_view(b_v3d, b_rv3d, width, height);
807
808                         if(scene->camera->need_update)
809                                 reset = true;
810
811                         session->scene->mutex.unlock();
812                 }
813
814                 /* reset if requested */
815                 if(reset) {
816                         SessionParams session_params = BlenderSync::get_session_params(b_engine, b_userpref, b_scene, background);
817                         BufferParams buffer_params = BlenderSync::get_buffer_params(b_render, b_v3d, b_rv3d, scene->camera, width, height);
818                         bool session_pause = BlenderSync::get_session_pause(b_scene, background);
819
820                         if(session_pause == false) {
821                                 session->reset(buffer_params, session_params.samples);
822                                 start_resize_time = 0.0;
823                         }
824                 }
825         }
826         else {
827                 tag_update();
828         }
829
830         /* update status and progress for 3d view draw */
831         update_status_progress();
832
833         /* draw */
834         BufferParams buffer_params = BlenderSync::get_buffer_params(b_render, b_v3d, b_rv3d, scene->camera, width, height);
835         DeviceDrawParams draw_params;
836
837         if(session->params.display_buffer_linear) {
838                 draw_params.bind_display_space_shader_cb = function_bind(&BL::RenderEngine::bind_display_space_shader, &b_engine, b_scene);
839                 draw_params.unbind_display_space_shader_cb = function_bind(&BL::RenderEngine::unbind_display_space_shader, &b_engine);
840         }
841
842         return !session->draw(buffer_params, draw_params);
843 }
844
845 void BlenderSession::get_status(string& status, string& substatus)
846 {
847         session->progress.get_status(status, substatus);
848 }
849
850 void BlenderSession::get_progress(float& progress, double& total_time, double& render_time)
851 {
852         session->progress.get_time(total_time, render_time);
853         progress = session->progress.get_progress();
854 }
855
856 void BlenderSession::update_bake_progress()
857 {
858         float progress = session->progress.get_progress();
859
860         if(progress != last_progress) {
861                 b_engine.update_progress(progress);
862                 last_progress = progress;
863         }
864 }
865
866 void BlenderSession::update_status_progress()
867 {
868         string timestatus, status, substatus;
869         string scene = "";
870         float progress;
871         double total_time, remaining_time = 0, render_time;
872         char time_str[128];
873         float mem_used = (float)session->stats.mem_used / 1024.0f / 1024.0f;
874         float mem_peak = (float)session->stats.mem_peak / 1024.0f / 1024.0f;
875
876         get_status(status, substatus);
877         get_progress(progress, total_time, render_time);
878
879         if(progress > 0)
880                 remaining_time = (1.0 - (double)progress) * (render_time / (double)progress);
881
882         if(background) {
883                 scene += " | " + b_scene.name();
884                 if(b_rlay_name != "")
885                         scene += ", "  + b_rlay_name;
886
887                 if(b_rview_name != "")
888                         scene += ", " + b_rview_name;
889         }
890         else {
891                 BLI_timecode_string_from_time_simple(time_str, sizeof(time_str), total_time);
892                 timestatus = "Time:" + string(time_str) + " | ";
893         }
894
895         if(remaining_time > 0) {
896                 BLI_timecode_string_from_time_simple(time_str, sizeof(time_str), remaining_time);
897                 timestatus += "Remaining:" + string(time_str) + " | ";
898         }
899
900         timestatus += string_printf("Mem:%.2fM, Peak:%.2fM", (double)mem_used, (double)mem_peak);
901
902         if(status.size() > 0)
903                 status = " | " + status;
904         if(substatus.size() > 0)
905                 status += " | " + substatus;
906
907         double current_time = time_dt();
908         /* When rendering in a window, redraw the status at least once per second to keep the elapsed and remaining time up-to-date.
909          * For headless rendering, only report when something significant changes to keep the console output readable. */
910         if(status != last_status || (!headless && (current_time - last_status_time) > 1.0)) {
911                 b_engine.update_stats("", (timestatus + scene + status).c_str());
912                 b_engine.update_memory_stats(mem_used, mem_peak);
913                 last_status = status;
914                 last_status_time = current_time;
915         }
916         if(progress != last_progress) {
917                 b_engine.update_progress(progress);
918                 last_progress = progress;
919         }
920
921         if(session->progress.get_error()) {
922                 string error = session->progress.get_error_message();
923                 if(error != last_error) {
924                         /* TODO(sergey): Currently C++ RNA API doesn't let us to
925                          * use mnemonic name for the variable. Would be nice to
926                          * have this figured out.
927                          *
928                          * For until then, 1 << 5 means RPT_ERROR.
929                          */
930                         b_engine.report(1 << 5, error.c_str());
931                         b_engine.error_set(error.c_str());
932                         last_error = error;
933                 }
934         }
935 }
936
937 void BlenderSession::tag_update()
938 {
939         /* tell blender that we want to get another update callback */
940         b_engine.tag_update();
941 }
942
943 void BlenderSession::tag_redraw()
944 {
945         if(background) {
946                 /* update stats and progress, only for background here because
947                  * in 3d view we do it in draw for thread safety reasons */
948                 update_status_progress();
949
950                 /* offline render, redraw if timeout passed */
951                 if(time_dt() - last_redraw_time > 1.0) {
952                         b_engine.tag_redraw();
953                         last_redraw_time = time_dt();
954                 }
955         }
956         else {
957                 /* tell blender that we want to redraw */
958                 b_engine.tag_redraw();
959         }
960 }
961
962 void BlenderSession::test_cancel()
963 {
964         /* test if we need to cancel rendering */
965         if(background)
966                 if(b_engine.test_break())
967                         session->progress.set_cancel("Cancelled");
968 }
969
970 /* builtin image file name is actually an image datablock name with
971  * absolute sequence frame number concatenated via '@' character
972  *
973  * this function splits frame from builtin name
974  */
975 int BlenderSession::builtin_image_frame(const string &builtin_name)
976 {
977         int last = builtin_name.find_last_of('@');
978         return atoi(builtin_name.substr(last + 1, builtin_name.size() - last - 1).c_str());
979 }
980
981 void BlenderSession::builtin_image_info(const string &builtin_name,
982                                         void *builtin_data,
983                                         bool &is_float,
984                                         int &width,
985                                         int &height,
986                                         int &depth,
987                                         int &channels)
988 {
989         /* empty image */
990         is_float = false;
991         width = 1;
992         height = 1;
993         depth = 0;
994         channels = 0;
995
996         if(!builtin_data)
997                 return;
998
999         /* recover ID pointer */
1000         PointerRNA ptr;
1001         RNA_id_pointer_create((ID*)builtin_data, &ptr);
1002         BL::ID b_id(ptr);
1003
1004         if(b_id.is_a(&RNA_Image)) {
1005                 /* image data */
1006                 BL::Image b_image(b_id);
1007
1008                 is_float = b_image.is_float();
1009                 width = b_image.size()[0];
1010                 height = b_image.size()[1];
1011                 depth = 1;
1012                 channels = b_image.channels();
1013         }
1014         else if(b_id.is_a(&RNA_Object)) {
1015                 /* smoke volume data */
1016                 BL::Object b_ob(b_id);
1017                 BL::SmokeDomainSettings b_domain = object_smoke_domain_find(b_ob);
1018
1019                 is_float = true;
1020                 depth = 1;
1021                 channels = 1;
1022
1023                 if(!b_domain)
1024                         return;
1025
1026                 if(builtin_name == Attribute::standard_name(ATTR_STD_VOLUME_DENSITY) ||
1027                    builtin_name == Attribute::standard_name(ATTR_STD_VOLUME_FLAME) ||
1028                    builtin_name == Attribute::standard_name(ATTR_STD_VOLUME_HEAT))
1029                         channels = 1;
1030                 else if(builtin_name == Attribute::standard_name(ATTR_STD_VOLUME_COLOR))
1031                         channels = 4;
1032                 else if(builtin_name == Attribute::standard_name(ATTR_STD_VOLUME_VELOCITY))
1033                         channels = 3;
1034                 else
1035                         return;
1036
1037                 int3 resolution = get_int3(b_domain.domain_resolution());
1038                 int amplify = (b_domain.use_high_resolution())? b_domain.amplify() + 1: 1;
1039
1040                 /* Velocity and heat data is always low-resolution. */
1041                 if(builtin_name == Attribute::standard_name(ATTR_STD_VOLUME_VELOCITY) ||
1042                    builtin_name == Attribute::standard_name(ATTR_STD_VOLUME_HEAT))
1043                 {
1044                         amplify = 1;
1045                 }
1046
1047                 width = resolution.x * amplify;
1048                 height = resolution.y * amplify;
1049                 depth = resolution.z * amplify;
1050         }
1051         else {
1052                 /* TODO(sergey): Check we're indeed in shader node tree. */
1053                 PointerRNA ptr;
1054                 RNA_pointer_create(NULL, &RNA_Node, builtin_data, &ptr);
1055                 BL::Node b_node(ptr);
1056                 if(b_node.is_a(&RNA_ShaderNodeTexPointDensity)) {
1057                         BL::ShaderNodeTexPointDensity b_point_density_node(b_node);
1058                         channels = 4;
1059                         width = height = depth = b_point_density_node.resolution();
1060                         is_float = true;
1061                 }
1062         }
1063 }
1064
1065 bool BlenderSession::builtin_image_pixels(const string &builtin_name,
1066                                           void *builtin_data,
1067                                           unsigned char *pixels,
1068                                           const size_t pixels_size)
1069 {
1070         if(!builtin_data) {
1071                 return false;
1072         }
1073
1074         const int frame = builtin_image_frame(builtin_name);
1075
1076         PointerRNA ptr;
1077         RNA_id_pointer_create((ID*)builtin_data, &ptr);
1078         BL::Image b_image(ptr);
1079
1080         const int width = b_image.size()[0];
1081         const int height = b_image.size()[1];
1082         const int channels = b_image.channels();
1083
1084         unsigned char *image_pixels = image_get_pixels_for_frame(b_image, frame);
1085         const size_t num_pixels = ((size_t)width) * height;
1086
1087         if(image_pixels && num_pixels * channels == pixels_size) {
1088                 memcpy(pixels, image_pixels, pixels_size * sizeof(unsigned char));
1089                 MEM_freeN(image_pixels);
1090         }
1091         else {
1092                 if(channels == 1) {
1093                         memset(pixels, 0, pixels_size * sizeof(unsigned char));
1094                 }
1095                 else {
1096                         const size_t num_pixels_safe = pixels_size / channels;
1097                         unsigned char *cp = pixels;
1098                         for(size_t i = 0; i < num_pixels_safe; i++, cp += channels) {
1099                                 cp[0] = 255;
1100                                 cp[1] = 0;
1101                                 cp[2] = 255;
1102                                 if(channels == 4) {
1103                                         cp[3] = 255;
1104                                 }
1105                         }
1106                 }
1107         }
1108         /* Premultiply, byte images are always straight for Blender. */
1109         unsigned char *cp = pixels;
1110         for(size_t i = 0; i < num_pixels; i++, cp += channels) {
1111                 cp[0] = (cp[0] * cp[3]) >> 8;
1112                 cp[1] = (cp[1] * cp[3]) >> 8;
1113                 cp[2] = (cp[2] * cp[3]) >> 8;
1114         }
1115         return true;
1116 }
1117
1118 bool BlenderSession::builtin_image_float_pixels(const string &builtin_name,
1119                                                 void *builtin_data,
1120                                                 float *pixels,
1121                                                 const size_t pixels_size)
1122 {
1123         if(!builtin_data) {
1124                 return false;
1125         }
1126
1127         PointerRNA ptr;
1128         RNA_id_pointer_create((ID*)builtin_data, &ptr);
1129         BL::ID b_id(ptr);
1130
1131         if(b_id.is_a(&RNA_Image)) {
1132                 /* image data */
1133                 BL::Image b_image(b_id);
1134                 int frame = builtin_image_frame(builtin_name);
1135
1136                 const int width = b_image.size()[0];
1137                 const int height = b_image.size()[1];
1138                 const int channels = b_image.channels();
1139
1140                 float *image_pixels;
1141                 image_pixels = image_get_float_pixels_for_frame(b_image, frame);
1142                 const size_t num_pixels = ((size_t)width) * height;
1143
1144                 if(image_pixels && num_pixels * channels == pixels_size) {
1145                         memcpy(pixels, image_pixels, pixels_size * sizeof(float));
1146                         MEM_freeN(image_pixels);
1147                 }
1148                 else {
1149                         if(channels == 1) {
1150                                 memset(pixels, 0, num_pixels * sizeof(float));
1151                         }
1152                         else {
1153                                 const size_t num_pixels_safe = pixels_size / channels;
1154                                 float *fp = pixels;
1155                                 for(int i = 0; i < num_pixels_safe; i++, fp += channels) {
1156                                         fp[0] = 1.0f;
1157                                         fp[1] = 0.0f;
1158                                         fp[2] = 1.0f;
1159                                         if(channels == 4) {
1160                                                 fp[3] = 1.0f;
1161                                         }
1162                                 }
1163                         }
1164                 }
1165
1166                 return true;
1167         }
1168         else if(b_id.is_a(&RNA_Object)) {
1169                 /* smoke volume data */
1170                 BL::Object b_ob(b_id);
1171                 BL::SmokeDomainSettings b_domain = object_smoke_domain_find(b_ob);
1172
1173                 if(!b_domain) {
1174                         return false;
1175                 }
1176
1177                 int3 resolution = get_int3(b_domain.domain_resolution());
1178                 int length, amplify = (b_domain.use_high_resolution())? b_domain.amplify() + 1: 1;
1179
1180                 /* Velocity and heat data is always low-resolution. */
1181                 if(builtin_name == Attribute::standard_name(ATTR_STD_VOLUME_VELOCITY) ||
1182                    builtin_name == Attribute::standard_name(ATTR_STD_VOLUME_HEAT))
1183                 {
1184                         amplify = 1;
1185                 }
1186
1187                 const int width = resolution.x * amplify;
1188                 const int height = resolution.y * amplify;
1189                 const int depth = resolution.z * amplify;
1190                 const size_t num_pixels = ((size_t)width) * height * depth;
1191
1192                 if(builtin_name == Attribute::standard_name(ATTR_STD_VOLUME_DENSITY)) {
1193                         SmokeDomainSettings_density_grid_get_length(&b_domain.ptr, &length);
1194                         if(length == num_pixels) {
1195                                 SmokeDomainSettings_density_grid_get(&b_domain.ptr, pixels);
1196                                 return true;
1197                         }
1198                 }
1199                 else if(builtin_name == Attribute::standard_name(ATTR_STD_VOLUME_FLAME)) {
1200                         /* this is in range 0..1, and interpreted by the OpenGL smoke viewer
1201                          * as 1500..3000 K with the first part faded to zero density */
1202                         SmokeDomainSettings_flame_grid_get_length(&b_domain.ptr, &length);
1203                         if(length == num_pixels) {
1204                                 SmokeDomainSettings_flame_grid_get(&b_domain.ptr, pixels);
1205                                 return true;
1206                         }
1207                 }
1208                 else if(builtin_name == Attribute::standard_name(ATTR_STD_VOLUME_COLOR)) {
1209                         /* the RGB is "premultiplied" by density for better interpolation results */
1210                         SmokeDomainSettings_color_grid_get_length(&b_domain.ptr, &length);
1211                         if(length == num_pixels*4) {
1212                                 SmokeDomainSettings_color_grid_get(&b_domain.ptr, pixels);
1213                                 return true;
1214                         }
1215                 }
1216                 else if(builtin_name == Attribute::standard_name(ATTR_STD_VOLUME_VELOCITY)) {
1217                         SmokeDomainSettings_velocity_grid_get_length(&b_domain.ptr, &length);
1218                         if(length == num_pixels*3) {
1219                                 SmokeDomainSettings_velocity_grid_get(&b_domain.ptr, pixels);
1220                                 return true;
1221                         }
1222                 }
1223                 else if(builtin_name == Attribute::standard_name(ATTR_STD_VOLUME_HEAT)) {
1224                         SmokeDomainSettings_heat_grid_get_length(&b_domain.ptr, &length);
1225                         if(length == num_pixels) {
1226                                 SmokeDomainSettings_heat_grid_get(&b_domain.ptr, pixels);
1227                                 return true;
1228                         }
1229                 }
1230                 else {
1231                         fprintf(stderr,
1232                                 "Cycles error: unknown volume attribute %s, skipping\n",
1233                                 builtin_name.c_str());
1234                         pixels[0] = 0.0f;
1235                         return false;
1236                 }
1237
1238                 fprintf(stderr, "Cycles error: unexpected smoke volume resolution, skipping\n");
1239         }
1240         else {
1241                 /* TODO(sergey): Check we're indeed in shader node tree. */
1242                 PointerRNA ptr;
1243                 RNA_pointer_create(NULL, &RNA_Node, builtin_data, &ptr);
1244                 BL::Node b_node(ptr);
1245                 if(b_node.is_a(&RNA_ShaderNodeTexPointDensity)) {
1246                         BL::ShaderNodeTexPointDensity b_point_density_node(b_node);
1247                         int length;
1248                         int settings = background ? 1 : 0;  /* 1 - render settings, 0 - vewport settings. */
1249                         b_point_density_node.calc_point_density(b_scene, settings, &length, &pixels);
1250                 }
1251         }
1252
1253         return false;
1254 }
1255
1256 void BlenderSession::update_resumable_tile_manager(int num_samples)
1257 {
1258         const int num_resumable_chunks = BlenderSession::num_resumable_chunks,
1259                   current_resumable_chunk = BlenderSession::current_resumable_chunk;
1260         if(num_resumable_chunks == 0) {
1261                 return;
1262         }
1263
1264         const int num_samples_per_chunk = (int)ceilf((float)num_samples / num_resumable_chunks);
1265
1266         int range_start_sample, range_num_samples;
1267         if(current_resumable_chunk != 0) {
1268                 /* Single chunk rendering. */
1269                 range_start_sample = num_samples_per_chunk * (current_resumable_chunk - 1);
1270                 range_num_samples = num_samples_per_chunk;
1271         }
1272         else {
1273                 /* Ranged-chunks. */
1274                 const int num_chunks = end_resumable_chunk - start_resumable_chunk + 1;
1275                 range_start_sample = num_samples_per_chunk * (start_resumable_chunk - 1);
1276                 range_num_samples = num_chunks * num_samples_per_chunk;
1277         }
1278         /* Make sure we don't overshoot. */
1279         if(range_start_sample + range_num_samples > num_samples) {
1280                 range_num_samples = num_samples - range_num_samples;
1281         }
1282
1283         VLOG(1) << "Samples range start is " << range_start_sample << ", "
1284                 << "number of samples to render is " << range_num_samples;
1285
1286         scene->integrator->start_sample = range_start_sample;
1287         scene->integrator->tag_update(scene);
1288
1289         session->tile_manager.range_start_sample = range_start_sample;
1290         session->tile_manager.range_num_samples = range_num_samples;
1291 }
1292
1293 CCL_NAMESPACE_END