Merging r48971 through r48980 from trunk into soc-2011-tomato
[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         BL::RenderSettings r = b_scene.render();
49
50         width = (int)(r.resolution_x()*r.resolution_percentage()/100);
51         height = (int)(r.resolution_y()*r.resolution_percentage()/100);
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);
90
91         /* create sync */
92         sync = new BlenderSync(b_engine, b_data, b_scene, scene, !background);
93         sync->sync_data(b_v3d, b_engine.camera_override());
94
95         if(b_rv3d)
96                 sync->sync_view(b_v3d, b_rv3d, width, height);
97         else
98                 sync->sync_camera(b_engine.camera_override(), width, height);
99
100         /* create session */
101         session = new Session(session_params);
102         session->scene = scene;
103         session->progress.set_update_callback(function_bind(&BlenderSession::tag_redraw, this));
104         session->progress.set_cancel_callback(function_bind(&BlenderSession::test_cancel, this));
105         session->set_pause(BlenderSync::get_session_pause(b_scene, background));
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::write_render_buffers(RenderBuffers *buffers)
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         /* write result */
213         write_render_result(b_rr, b_rlay, buffers);
214         end_render_result(b_engine, b_rr);
215 }
216
217 void BlenderSession::render()
218 {
219         /* set callback to write out render results */
220         session->write_render_buffers_cb = function_bind(&BlenderSession::write_render_buffers, this, _1);
221
222         /* get buffer parameters */
223         SessionParams session_params = BlenderSync::get_session_params(b_engine, b_userpref, b_scene, background);
224         BufferParams buffer_params = BlenderSync::get_buffer_params(b_scene, scene->camera, width, height);
225
226         /* render each layer */
227         BL::RenderSettings r = b_scene.render();
228         BL::RenderSettings::layers_iterator b_iter;
229         
230         for(r.layers.begin(b_iter); b_iter != r.layers.end(); ++b_iter) {
231                 b_rlay_name = b_iter->name();
232
233                 /* temporary render result to find needed passes */
234                 BL::RenderResult b_rr = begin_render_result(b_engine, 0, 0, 1, 1, b_rlay_name.c_str());
235                 BL::RenderResult::layers_iterator b_single_rlay;
236                 b_rr.layers.begin(b_single_rlay);
237
238                 /* layer will be missing if it was disabled in the UI */
239                 if(b_single_rlay == b_rr.layers.end()) {
240                         end_render_result(b_engine, b_rr, true);
241                         continue;
242                 }
243
244                 BL::RenderLayer b_rlay = *b_single_rlay;
245
246                 /* add passes */
247                 vector<Pass> passes;
248                 Pass::add(PASS_COMBINED, passes);
249
250                 if(session_params.device.advanced_shading) {
251
252                         /* loop over passes */
253                         BL::RenderLayer::passes_iterator b_pass_iter;
254
255                         for(b_rlay.passes.begin(b_pass_iter); b_pass_iter != b_rlay.passes.end(); ++b_pass_iter) {
256                                 BL::RenderPass b_pass(*b_pass_iter);
257                                 PassType pass_type = get_pass_type(b_pass);
258
259                                 if(pass_type == PASS_MOTION && scene->integrator->motion_blur)
260                                         continue;
261                                 if(pass_type != PASS_NONE)
262                                         Pass::add(pass_type, passes);
263                         }
264                 }
265
266                 /* free result without merging */
267                 end_render_result(b_engine, b_rr, true);
268
269                 buffer_params.passes = passes;
270                 scene->film->tag_passes_update(scene, passes);
271                 scene->film->tag_update(scene);
272                 scene->integrator->tag_update(scene);
273
274                 /* update scene */
275                 sync->sync_data(b_v3d, b_engine.camera_override(), b_rlay_name.c_str());
276
277                 /* update session */
278                 int samples = sync->get_layer_samples();
279                 session->reset(buffer_params, (samples == 0)? session_params.samples: samples);
280
281                 /* render */
282                 session->start();
283                 session->wait();
284
285                 if(session->progress.get_cancel())
286                         break;
287         }
288
289         /* clear callback */
290         session->write_render_buffers_cb = NULL;
291 }
292
293 void BlenderSession::write_render_result(BL::RenderResult b_rr, BL::RenderLayer b_rlay, RenderBuffers *buffers)
294 {
295         /* copy data from device */
296         if(!buffers->copy_from_device())
297                 return;
298
299         BufferParams& params = buffers->params;
300         float exposure = scene->film->exposure;
301         double total_time, sample_time;
302         int sample;
303
304         session->progress.get_sample(sample, total_time, sample_time);
305
306         vector<float> pixels(params.width*params.height*4);
307
308         /* copy each pass */
309         BL::RenderLayer::passes_iterator b_iter;
310         
311         for(b_rlay.passes.begin(b_iter); b_iter != b_rlay.passes.end(); ++b_iter) {
312                 BL::RenderPass b_pass(*b_iter);
313
314                 /* find matching pass type */
315                 PassType pass_type = get_pass_type(b_pass);
316                 int components = b_pass.channels();
317
318                 /* copy pixels */
319                 if(buffers->get_pass(pass_type, exposure, sample, components, &pixels[0]))
320                         rna_RenderPass_rect_set(&b_pass.ptr, &pixels[0]);
321         }
322
323         /* copy combined pass */
324         if(buffers->get_pass(PASS_COMBINED, exposure, sample, 4, &pixels[0]))
325                 rna_RenderLayer_rect_set(&b_rlay.ptr, &pixels[0]);
326
327         /* tag result as updated */
328         RE_engine_update_result((RenderEngine*)b_engine.ptr.data, (RenderResult*)b_rr.ptr.data);
329 }
330
331 void BlenderSession::synchronize()
332 {
333         /* on session/scene parameter changes, we recreate session entirely */
334         SceneParams scene_params = BlenderSync::get_scene_params(b_scene, background);
335         SessionParams session_params = BlenderSync::get_session_params(b_engine, b_userpref, b_scene, background);
336
337         if(session->params.modified(session_params) ||
338            scene->params.modified(scene_params))
339         {
340                 free_session();
341                 create_session();
342                 session->start();
343                 return;
344         }
345
346         /* increase samples, but never decrease */
347         session->set_samples(session_params.samples);
348         session->set_pause(BlenderSync::get_session_pause(b_scene, background));
349
350         /* copy recalc flags, outside of mutex so we can decide to do the real
351          * synchronization at a later time to not block on running updates */
352         sync->sync_recalc();
353
354         /* try to acquire mutex. if we don't want to or can't, come back later */
355         if(!session->ready_to_reset() || !session->scene->mutex.try_lock()) {
356                 tag_update();
357                 return;
358         }
359
360         /* data and camera synchronize */
361         sync->sync_data(b_v3d, b_engine.camera_override());
362
363         if(b_rv3d)
364                 sync->sync_view(b_v3d, b_rv3d, width, height);
365         else
366                 sync->sync_camera(b_engine.camera_override(), width, height);
367
368         /* unlock */
369         session->scene->mutex.unlock();
370
371         /* reset if needed */
372         if(scene->need_reset()) {
373                 BufferParams buffer_params = BlenderSync::get_buffer_params(b_scene, scene->camera, width, height);
374                 session->reset(buffer_params, session_params.samples);
375         }
376 }
377
378 bool BlenderSession::draw(int w, int h)
379 {
380         /* before drawing, we verify camera and viewport size changes, because
381          * we do not get update callbacks for those, we must detect them here */
382         if(session->ready_to_reset()) {
383                 bool reset = false;
384
385                 /* try to acquire mutex. if we can't, come back later */
386                 if(!session->scene->mutex.try_lock()) {
387                         tag_update();
388                 }
389                 else {
390                         /* update camera from 3d view */
391                         bool need_update = scene->camera->need_update;
392
393                         sync->sync_view(b_v3d, b_rv3d, w, h);
394
395                         if(scene->camera->need_update && !need_update)
396                                 reset = true;
397
398                         session->scene->mutex.unlock();
399                 }
400
401                 /* if dimensions changed, reset */
402                 if(width != w || height != h) {
403                         width = w;
404                         height = h;
405                         reset = true;
406                 }
407
408                 /* reset if requested */
409                 if(reset) {
410                         SessionParams session_params = BlenderSync::get_session_params(b_engine, b_userpref, b_scene, background);
411                         BufferParams buffer_params = BlenderSync::get_buffer_params(b_scene, scene->camera, w, h);
412
413                         session->reset(buffer_params, session_params.samples);
414                 }
415         }
416
417         /* update status and progress for 3d view draw */
418         update_status_progress();
419
420         /* draw */
421         BufferParams buffer_params = BlenderSync::get_buffer_params(b_scene, scene->camera, width, height);
422
423         return !session->draw(buffer_params);
424 }
425
426 void BlenderSession::get_status(string& status, string& substatus)
427 {
428         session->progress.get_status(status, substatus);
429 }
430
431 void BlenderSession::get_progress(float& progress, double& total_time)
432 {
433         double sample_time;
434         int sample;
435
436         session->progress.get_sample(sample, total_time, sample_time);
437         progress = ((float)sample/(float)session->params.samples);
438 }
439
440 void BlenderSession::update_status_progress()
441 {
442         string timestatus, status, substatus;
443         float progress;
444         double total_time;
445         char time_str[128];
446
447         get_status(status, substatus);
448         get_progress(progress, total_time);
449
450         BLI_timestr(total_time, time_str);
451         timestatus = "Elapsed: " + string(time_str) + " | ";
452
453         if(substatus.size() > 0)
454                 status += " | " + substatus;
455
456         if(status != last_status) {
457                 RE_engine_update_stats((RenderEngine*)b_engine.ptr.data, "", (timestatus + status).c_str());
458                 last_status = status;
459         }
460         if(progress != last_progress) {
461                 RE_engine_update_progress((RenderEngine*)b_engine.ptr.data, progress);
462                 last_progress = progress;
463         }
464 }
465
466 void BlenderSession::tag_update()
467 {
468         /* tell blender that we want to get another update callback */
469         engine_tag_update((RenderEngine*)b_engine.ptr.data);
470 }
471
472 void BlenderSession::tag_redraw()
473 {
474         if(background) {
475                 /* update stats and progress, only for background here because
476                  * in 3d view we do it in draw for thread safety reasons */
477                 update_status_progress();
478
479                 /* offline render, redraw if timeout passed */
480                 if(time_dt() - last_redraw_time > 1.0) {
481                         engine_tag_redraw((RenderEngine*)b_engine.ptr.data);
482                         last_redraw_time = time_dt();
483                 }
484         }
485         else {
486                 /* tell blender that we want to redraw */
487                 engine_tag_redraw((RenderEngine*)b_engine.ptr.data);
488         }
489 }
490
491 void BlenderSession::test_cancel()
492 {
493         /* test if we need to cancel rendering */
494         if(background)
495                 if(RE_engine_test_break((RenderEngine*)b_engine.ptr.data))
496                         session->progress.set_cancel("Cancelled");
497 }
498
499 CCL_NAMESPACE_END
500