svn merge ^/trunk/blender -r49520:49531
[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_scene(b_scene_),
45   b_v3d(PointerRNA_NULL), b_rv3d(PointerRNA_NULL)
46 {
47         /* offline render */
48
49         width = b_engine.resolution_x();
50         height = b_engine.resolution_y();
51
52         background = true;
53         last_redraw_time = 0.0f;
54
55         create_session();
56 }
57
58 BlenderSession::BlenderSession(BL::RenderEngine b_engine_, BL::UserPreferences b_userpref_,
59         BL::BlendData b_data_, BL::Scene b_scene_,
60         BL::SpaceView3D b_v3d_, BL::RegionView3D b_rv3d_, int width_, int height_)
61 : b_engine(b_engine_), b_userpref(b_userpref_), b_data(b_data_), b_scene(b_scene_),
62   b_v3d(b_v3d_), b_rv3d(b_rv3d_)
63 {
64         /* 3d view render */
65         width = width_;
66         height = height_;
67         background = false;
68         last_redraw_time = 0.0f;
69
70         create_session();
71         session->start();
72 }
73
74 BlenderSession::~BlenderSession()
75 {
76         free_session();
77 }
78
79 void BlenderSession::create_session()
80 {
81         SceneParams scene_params = BlenderSync::get_scene_params(b_scene, background);
82         SessionParams session_params = BlenderSync::get_session_params(b_engine, b_userpref, b_scene, background);
83
84         /* reset status/progress */
85         last_status = "";
86         last_progress = -1.0f;
87
88         /* create scene */
89         scene = new Scene(scene_params, session_params.device);
90
91         /* create session */
92         session = new Session(session_params);
93         session->scene = scene;
94         session->progress.set_update_callback(function_bind(&BlenderSession::tag_redraw, this));
95         session->progress.set_cancel_callback(function_bind(&BlenderSession::test_cancel, this));
96         session->set_pause(BlenderSync::get_session_pause(b_scene, background));
97
98         /* create sync */
99         sync = new BlenderSync(b_engine, b_data, b_scene, scene, !background, session->progress);
100         sync->sync_data(b_v3d, b_engine.camera_override());
101
102         if(b_rv3d)
103                 sync->sync_view(b_v3d, b_rv3d, width, height);
104         else
105                 sync->sync_camera(b_engine.camera_override(), width, height);
106
107         /* set buffer parameters */
108         BufferParams buffer_params = BlenderSync::get_buffer_params(b_scene, scene->camera, width, height);
109         session->reset(buffer_params, session_params.samples);
110 }
111
112 void BlenderSession::free_session()
113 {
114         delete sync;
115         delete session;
116 }
117
118 static PassType get_pass_type(BL::RenderPass b_pass)
119 {
120         switch(b_pass.type()) {
121                 case BL::RenderPass::type_COMBINED:
122                         return PASS_COMBINED;
123
124                 case BL::RenderPass::type_Z:
125                         return PASS_DEPTH;
126                 case BL::RenderPass::type_NORMAL:
127                         return PASS_NORMAL;
128                 case BL::RenderPass::type_OBJECT_INDEX:
129                         return PASS_OBJECT_ID;
130                 case BL::RenderPass::type_UV:
131                         return PASS_UV;
132                 case BL::RenderPass::type_VECTOR:
133                         return PASS_MOTION;
134                 case BL::RenderPass::type_MATERIAL_INDEX:
135                         return PASS_MATERIAL_ID;
136
137                 case BL::RenderPass::type_DIFFUSE_DIRECT:
138                         return PASS_DIFFUSE_DIRECT;
139                 case BL::RenderPass::type_GLOSSY_DIRECT:
140                         return PASS_GLOSSY_DIRECT;
141                 case BL::RenderPass::type_TRANSMISSION_DIRECT:
142                         return PASS_TRANSMISSION_DIRECT;
143
144                 case BL::RenderPass::type_DIFFUSE_INDIRECT:
145                         return PASS_DIFFUSE_INDIRECT;
146                 case BL::RenderPass::type_GLOSSY_INDIRECT:
147                         return PASS_GLOSSY_INDIRECT;
148                 case BL::RenderPass::type_TRANSMISSION_INDIRECT:
149                         return PASS_TRANSMISSION_INDIRECT;
150
151                 case BL::RenderPass::type_DIFFUSE_COLOR:
152                         return PASS_DIFFUSE_COLOR;
153                 case BL::RenderPass::type_GLOSSY_COLOR:
154                         return PASS_GLOSSY_COLOR;
155                 case BL::RenderPass::type_TRANSMISSION_COLOR:
156                         return PASS_TRANSMISSION_COLOR;
157
158                 case BL::RenderPass::type_EMIT:
159                         return PASS_EMISSION;
160                 case BL::RenderPass::type_ENVIRONMENT:
161                         return PASS_BACKGROUND;
162                 case BL::RenderPass::type_AO:
163                         return PASS_AO;
164                 case BL::RenderPass::type_SHADOW:
165                         return PASS_SHADOW;
166
167                 case BL::RenderPass::type_DIFFUSE:
168                 case BL::RenderPass::type_COLOR:
169                 case BL::RenderPass::type_REFRACTION:
170                 case BL::RenderPass::type_SPECULAR:
171                 case BL::RenderPass::type_REFLECTION:
172                 case BL::RenderPass::type_MIST:
173                         return PASS_NONE;
174         }
175         
176         return PASS_NONE;
177 }
178
179 static BL::RenderResult begin_render_result(BL::RenderEngine b_engine, int x, int y, int w, int h, const char *layername)
180 {
181         RenderResult *rrp = RE_engine_begin_result((RenderEngine*)b_engine.ptr.data, x, y, w, h, layername);
182         PointerRNA rrptr;
183         RNA_pointer_create(NULL, &RNA_RenderResult, rrp, &rrptr);
184         return BL::RenderResult(rrptr);
185 }
186
187 static void end_render_result(BL::RenderEngine b_engine, BL::RenderResult b_rr, bool cancel = false)
188 {
189         RE_engine_end_result((RenderEngine*)b_engine.ptr.data, (RenderResult*)b_rr.ptr.data, (int)cancel);
190 }
191
192 void BlenderSession::do_write_update_render_buffers(RenderBuffers *buffers, bool do_update_only)
193 {
194         BufferParams& params = buffers->params;
195         int x = params.full_x - session->tile_manager.params.full_x;
196         int y = params.full_y - session->tile_manager.params.full_y;
197         int w = params.width;
198         int h = params.height;
199
200         /* get render result */
201         BL::RenderResult b_rr = begin_render_result(b_engine, x, y, w, h, b_rlay_name.c_str());
202
203         /* can happen if the intersected rectangle gives 0 width or height */
204         if (b_rr.ptr.data == NULL) {
205                 return;
206         }
207
208         BL::RenderResult::layers_iterator b_single_rlay;
209         b_rr.layers.begin(b_single_rlay);
210         BL::RenderLayer b_rlay = *b_single_rlay;
211
212         if (do_update_only) {
213                 /* update only needed */
214                 update_render_result(b_rr, b_rlay, buffers);
215                 end_render_result(b_engine, b_rr, true);
216         }
217         else {
218                 /* write result */
219                 write_render_result(b_rr, b_rlay, buffers);
220                 end_render_result(b_engine, b_rr);
221         }
222 }
223
224 void BlenderSession::write_render_buffers(RenderBuffers *buffers)
225 {
226         do_write_update_render_buffers(buffers, false);
227 }
228
229 void BlenderSession::update_render_buffers(RenderBuffers *buffers)
230 {
231         do_write_update_render_buffers(buffers, true);
232 }
233
234 void BlenderSession::render()
235 {
236         /* set callback to write out render results */
237         session->write_render_buffers_cb = function_bind(&BlenderSession::write_render_buffers, this, _1);
238         session->update_render_buffers_cb = function_bind(&BlenderSession::update_render_buffers, this, _1);
239
240         /* get buffer parameters */
241         SessionParams session_params = BlenderSync::get_session_params(b_engine, b_userpref, b_scene, background);
242         BufferParams buffer_params = BlenderSync::get_buffer_params(b_scene, scene->camera, width, height);
243
244         /* render each layer */
245         BL::RenderSettings r = b_scene.render();
246         BL::RenderSettings::layers_iterator b_iter;
247         
248         for(r.layers.begin(b_iter); b_iter != r.layers.end(); ++b_iter) {
249                 b_rlay_name = b_iter->name();
250
251                 /* temporary render result to find needed passes */
252                 BL::RenderResult b_rr = begin_render_result(b_engine, 0, 0, 1, 1, b_rlay_name.c_str());
253                 BL::RenderResult::layers_iterator b_single_rlay;
254                 b_rr.layers.begin(b_single_rlay);
255
256                 /* layer will be missing if it was disabled in the UI */
257                 if(b_single_rlay == b_rr.layers.end()) {
258                         end_render_result(b_engine, b_rr, true);
259                         continue;
260                 }
261
262                 BL::RenderLayer b_rlay = *b_single_rlay;
263
264                 /* add passes */
265                 vector<Pass> passes;
266                 Pass::add(PASS_COMBINED, passes);
267
268                 if(session_params.device.advanced_shading) {
269
270                         /* loop over passes */
271                         BL::RenderLayer::passes_iterator b_pass_iter;
272
273                         for(b_rlay.passes.begin(b_pass_iter); b_pass_iter != b_rlay.passes.end(); ++b_pass_iter) {
274                                 BL::RenderPass b_pass(*b_pass_iter);
275                                 PassType pass_type = get_pass_type(b_pass);
276
277                                 if(pass_type == PASS_MOTION && scene->integrator->motion_blur)
278                                         continue;
279                                 if(pass_type != PASS_NONE)
280                                         Pass::add(pass_type, passes);
281                         }
282                 }
283
284                 /* free result without merging */
285                 end_render_result(b_engine, b_rr, true);
286
287                 buffer_params.passes = passes;
288                 scene->film->tag_passes_update(scene, passes);
289                 scene->film->tag_update(scene);
290                 scene->integrator->tag_update(scene);
291
292                 /* update scene */
293                 sync->sync_data(b_v3d, b_engine.camera_override(), b_rlay_name.c_str());
294
295                 /* update session */
296                 int samples = sync->get_layer_samples();
297                 session->reset(buffer_params, (samples == 0)? session_params.samples: samples);
298
299                 /* render */
300                 session->start();
301                 session->wait();
302
303                 if(session->progress.get_cancel())
304                         break;
305         }
306
307         /* clear callback */
308         session->write_render_buffers_cb = NULL;
309         session->update_render_buffers_cb = NULL;
310 }
311
312 void BlenderSession::do_write_update_render_result(BL::RenderResult b_rr, BL::RenderLayer b_rlay, RenderBuffers *buffers, bool do_update_only)
313 {
314         /* copy data from device */
315         if(!buffers->copy_from_device())
316                 return;
317
318         BufferParams& params = buffers->params;
319         float exposure = scene->film->exposure;
320         double total_time, sample_time;
321         int sample;
322
323         session->progress.get_sample(sample, total_time, sample_time);
324
325         vector<float> pixels(params.width*params.height*4);
326
327         if (!do_update_only) {
328                 /* copy each pass */
329                 BL::RenderLayer::passes_iterator b_iter;
330
331                 for(b_rlay.passes.begin(b_iter); b_iter != b_rlay.passes.end(); ++b_iter) {
332                         BL::RenderPass b_pass(*b_iter);
333
334                         /* find matching pass type */
335                         PassType pass_type = get_pass_type(b_pass);
336                         int components = b_pass.channels();
337
338                         /* copy pixels */
339                         if(buffers->get_pass(pass_type, exposure, sample, components, &pixels[0]))
340                                 rna_RenderPass_rect_set(&b_pass.ptr, &pixels[0]);
341                 }
342         }
343
344         /* copy combined pass */
345         if(buffers->get_pass(PASS_COMBINED, exposure, sample, 4, &pixels[0]))
346                 rna_RenderLayer_rect_set(&b_rlay.ptr, &pixels[0]);
347
348         /* tag result as updated */
349         RE_engine_update_result((RenderEngine*)b_engine.ptr.data, (RenderResult*)b_rr.ptr.data);
350 }
351
352 void BlenderSession::write_render_result(BL::RenderResult b_rr, BL::RenderLayer b_rlay, RenderBuffers *buffers)
353 {
354         do_write_update_render_result(b_rr, b_rlay, buffers, false);
355 }
356
357 void BlenderSession::update_render_result(BL::RenderResult b_rr, BL::RenderLayer b_rlay, RenderBuffers *buffers)
358 {
359         do_write_update_render_result(b_rr, b_rlay, buffers, true);
360 }
361
362 void BlenderSession::synchronize()
363 {
364         /* on session/scene parameter changes, we recreate session entirely */
365         SceneParams scene_params = BlenderSync::get_scene_params(b_scene, background);
366         SessionParams session_params = BlenderSync::get_session_params(b_engine, b_userpref, b_scene, background);
367
368         if(session->params.modified(session_params) ||
369            scene->params.modified(scene_params))
370         {
371                 free_session();
372                 create_session();
373                 session->start();
374                 return;
375         }
376
377         /* increase samples, but never decrease */
378         session->set_samples(session_params.samples);
379         session->set_pause(BlenderSync::get_session_pause(b_scene, background));
380
381         /* copy recalc flags, outside of mutex so we can decide to do the real
382          * synchronization at a later time to not block on running updates */
383         sync->sync_recalc();
384
385         /* try to acquire mutex. if we don't want to or can't, come back later */
386         if(!session->ready_to_reset() || !session->scene->mutex.try_lock()) {
387                 tag_update();
388                 return;
389         }
390
391         /* data and camera synchronize */
392         sync->sync_data(b_v3d, b_engine.camera_override());
393
394         if(b_rv3d)
395                 sync->sync_view(b_v3d, b_rv3d, width, height);
396         else
397                 sync->sync_camera(b_engine.camera_override(), width, height);
398
399         /* unlock */
400         session->scene->mutex.unlock();
401
402         /* reset if needed */
403         if(scene->need_reset()) {
404                 BufferParams buffer_params = BlenderSync::get_buffer_params(b_scene, scene->camera, width, height);
405                 session->reset(buffer_params, session_params.samples);
406         }
407 }
408
409 bool BlenderSession::draw(int w, int h)
410 {
411         /* before drawing, we verify camera and viewport size changes, because
412          * we do not get update callbacks for those, we must detect them here */
413         if(session->ready_to_reset()) {
414                 bool reset = false;
415
416                 /* try to acquire mutex. if we can't, come back later */
417                 if(!session->scene->mutex.try_lock()) {
418                         tag_update();
419                 }
420                 else {
421                         /* update camera from 3d view */
422                         bool need_update = scene->camera->need_update;
423
424                         sync->sync_view(b_v3d, b_rv3d, w, h);
425
426                         if(scene->camera->need_update && !need_update)
427                                 reset = true;
428
429                         session->scene->mutex.unlock();
430                 }
431
432                 /* if dimensions changed, reset */
433                 if(width != w || height != h) {
434                         width = w;
435                         height = h;
436                         reset = true;
437                 }
438
439                 /* reset if requested */
440                 if(reset) {
441                         SessionParams session_params = BlenderSync::get_session_params(b_engine, b_userpref, b_scene, background);
442                         BufferParams buffer_params = BlenderSync::get_buffer_params(b_scene, scene->camera, w, h);
443
444                         session->reset(buffer_params, session_params.samples);
445                 }
446         }
447
448         /* update status and progress for 3d view draw */
449         update_status_progress();
450
451         /* draw */
452         BufferParams buffer_params = BlenderSync::get_buffer_params(b_scene, scene->camera, width, height);
453
454         return !session->draw(buffer_params);
455 }
456
457 void BlenderSession::get_status(string& status, string& substatus)
458 {
459         session->progress.get_status(status, substatus);
460 }
461
462 void BlenderSession::get_progress(float& progress, double& total_time)
463 {
464         double sample_time;
465         int sample;
466
467         session->progress.get_sample(sample, total_time, sample_time);
468         progress = ((float)sample/(float)session->params.samples);
469 }
470
471 void BlenderSession::update_status_progress()
472 {
473         string timestatus, status, substatus;
474         float progress;
475         double total_time;
476         char time_str[128];
477
478         get_status(status, substatus);
479         get_progress(progress, total_time);
480
481         BLI_timestr(total_time, time_str);
482         timestatus = "Elapsed: " + string(time_str) + " | ";
483
484         if(substatus.size() > 0)
485                 status += " | " + substatus;
486
487         if(status != last_status) {
488                 RE_engine_update_stats((RenderEngine*)b_engine.ptr.data, "", (timestatus + status).c_str());
489                 last_status = status;
490         }
491         if(progress != last_progress) {
492                 RE_engine_update_progress((RenderEngine*)b_engine.ptr.data, progress);
493                 last_progress = progress;
494         }
495 }
496
497 void BlenderSession::tag_update()
498 {
499         /* tell blender that we want to get another update callback */
500         engine_tag_update((RenderEngine*)b_engine.ptr.data);
501 }
502
503 void BlenderSession::tag_redraw()
504 {
505         if(background) {
506                 /* update stats and progress, only for background here because
507                  * in 3d view we do it in draw for thread safety reasons */
508                 update_status_progress();
509
510                 /* offline render, redraw if timeout passed */
511                 if(time_dt() - last_redraw_time > 1.0) {
512                         engine_tag_redraw((RenderEngine*)b_engine.ptr.data);
513                         last_redraw_time = time_dt();
514                 }
515         }
516         else {
517                 /* tell blender that we want to redraw */
518                 engine_tag_redraw((RenderEngine*)b_engine.ptr.data);
519         }
520 }
521
522 void BlenderSession::test_cancel()
523 {
524         /* test if we need to cancel rendering */
525         if(background)
526                 if(RE_engine_test_break((RenderEngine*)b_engine.ptr.data))
527                         session->progress.set_cancel("Cancelled");
528 }
529
530 CCL_NAMESPACE_END
531