Merging r46096 through r46110 from trunk into soc-2011-tomato
[blender-staging.git] / intern / cycles / blender / blender_session.cpp
1 /*
2  * Copyright 2011, Blender Foundation.
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  */
18
19 #include "background.h"
20 #include "buffers.h"
21 #include "camera.h"
22 #include "device.h"
23 #include "integrator.h"
24 #include "film.h"
25 #include "light.h"
26 #include "scene.h"
27 #include "session.h"
28 #include "shader.h"
29
30 #include "util_color.h"
31 #include "util_foreach.h"
32 #include "util_function.h"
33 #include "util_progress.h"
34 #include "util_time.h"
35
36 #include "blender_sync.h"
37 #include "blender_session.h"
38 #include "blender_util.h"
39
40 CCL_NAMESPACE_BEGIN
41
42 BlenderSession::BlenderSession(BL::RenderEngine b_engine_, BL::UserPreferences b_userpref_,
43         BL::BlendData b_data_, BL::Scene b_scene_)
44 : b_engine(b_engine_), b_userpref(b_userpref_), b_data(b_data_), b_scene(b_scene_),
45   b_v3d(PointerRNA_NULL), b_rv3d(PointerRNA_NULL),
46   b_rr(PointerRNA_NULL), b_rlay(PointerRNA_NULL)
47 {
48         /* offline render */
49         BL::RenderSettings r = b_scene.render();
50
51         width = (int)(r.resolution_x()*r.resolution_percentage()/100);
52         height = (int)(r.resolution_y()*r.resolution_percentage()/100);
53         background = true;
54         last_redraw_time = 0.0f;
55
56         create_session();
57 }
58
59 BlenderSession::BlenderSession(BL::RenderEngine b_engine_, BL::UserPreferences b_userpref_,
60         BL::BlendData b_data_, BL::Scene b_scene_,
61         BL::SpaceView3D b_v3d_, BL::RegionView3D b_rv3d_, int width_, int height_)
62 : b_engine(b_engine_), b_userpref(b_userpref_), b_data(b_data_), b_scene(b_scene_),
63   b_v3d(b_v3d_), b_rv3d(b_rv3d_), b_rr(PointerRNA_NULL), b_rlay(PointerRNA_NULL)
64 {
65         /* 3d view render */
66         width = width_;
67         height = height_;
68         background = false;
69         last_redraw_time = 0.0f;
70
71         create_session();
72         session->start();
73 }
74
75 BlenderSession::~BlenderSession()
76 {
77         free_session();
78 }
79
80 void BlenderSession::create_session()
81 {
82         SceneParams scene_params = BlenderSync::get_scene_params(b_scene, background);
83         SessionParams session_params = BlenderSync::get_session_params(b_userpref, b_scene, background);
84
85         /* reset status/progress */
86         last_status= "";
87         last_progress= -1.0f;
88
89         /* create scene */
90         scene = new Scene(scene_params);
91
92         /* create sync */
93         sync = new BlenderSync(b_data, b_scene, scene, !background);
94         sync->sync_data(b_v3d);
95
96         if(b_rv3d)
97                 sync->sync_view(b_v3d, b_rv3d, width, height);
98         else
99                 sync->sync_camera(b_engine.camera_override(), width, height);
100
101         /* create session */
102         session = new Session(session_params);
103         session->scene = scene;
104         session->progress.set_update_callback(function_bind(&BlenderSession::tag_redraw, this));
105         session->progress.set_cancel_callback(function_bind(&BlenderSession::test_cancel, this));
106         session->set_pause(BlenderSync::get_session_pause(b_scene, background));
107
108         /* set buffer parameters */
109         BufferParams buffer_params = BlenderSync::get_buffer_params(b_scene, b_rv3d, width, height);
110         session->reset(buffer_params, session_params.samples);
111 }
112
113 void BlenderSession::free_session()
114 {
115         delete sync;
116         delete session;
117 }
118
119 static PassType get_pass_type(BL::RenderPass b_pass)
120 {
121         switch(b_pass.type()) {
122                 case BL::RenderPass::type_COMBINED:
123                         return PASS_COMBINED;
124
125                 case BL::RenderPass::type_Z:
126                         return PASS_DEPTH;
127                 case BL::RenderPass::type_NORMAL:
128                         return PASS_NORMAL;
129                 case BL::RenderPass::type_OBJECT_INDEX:
130                         return PASS_OBJECT_ID;
131                 case BL::RenderPass::type_UV:
132                         return PASS_UV;
133                 case BL::RenderPass::type_MATERIAL_INDEX:
134                         return PASS_MATERIAL_ID;
135
136                 case BL::RenderPass::type_DIFFUSE_DIRECT:
137                         return PASS_DIFFUSE_DIRECT;
138                 case BL::RenderPass::type_GLOSSY_DIRECT:
139                         return PASS_GLOSSY_DIRECT;
140                 case BL::RenderPass::type_TRANSMISSION_DIRECT:
141                         return PASS_TRANSMISSION_DIRECT;
142
143                 case BL::RenderPass::type_DIFFUSE_INDIRECT:
144                         return PASS_DIFFUSE_INDIRECT;
145                 case BL::RenderPass::type_GLOSSY_INDIRECT:
146                         return PASS_GLOSSY_INDIRECT;
147                 case BL::RenderPass::type_TRANSMISSION_INDIRECT:
148                         return PASS_TRANSMISSION_INDIRECT;
149
150                 case BL::RenderPass::type_DIFFUSE_COLOR:
151                         return PASS_DIFFUSE_COLOR;
152                 case BL::RenderPass::type_GLOSSY_COLOR:
153                         return PASS_GLOSSY_COLOR;
154                 case BL::RenderPass::type_TRANSMISSION_COLOR:
155                         return PASS_TRANSMISSION_COLOR;
156
157                 case BL::RenderPass::type_EMIT:
158                         return PASS_EMISSION;
159                 case BL::RenderPass::type_ENVIRONMENT:
160                         return PASS_BACKGROUND;
161                 case BL::RenderPass::type_AO:
162                         return PASS_AO;
163                 case BL::RenderPass::type_SHADOW:
164                         return PASS_SHADOW;
165
166                 case BL::RenderPass::type_DIFFUSE:
167                 case BL::RenderPass::type_COLOR:
168                 case BL::RenderPass::type_REFRACTION:
169                 case BL::RenderPass::type_SPECULAR:
170                 case BL::RenderPass::type_REFLECTION:
171                 case BL::RenderPass::type_VECTOR:
172                 case BL::RenderPass::type_MIST:
173                         return PASS_NONE;
174         }
175         
176         return PASS_NONE;
177 }
178
179 void BlenderSession::render()
180 {
181         /* get buffer parameters */
182         SessionParams session_params = BlenderSync::get_session_params(b_userpref, b_scene, background);
183         BufferParams buffer_params = BlenderSync::get_buffer_params(b_scene, b_rv3d, width, height);
184         int w = buffer_params.width, h = buffer_params.height;
185
186         /* create render result */
187         RenderResult *rrp = RE_engine_begin_result((RenderEngine*)b_engine.ptr.data, 0, 0, w, h);
188         PointerRNA rrptr;
189         RNA_pointer_create(NULL, &RNA_RenderResult, rrp, &rrptr);
190         b_rr = BL::RenderResult(rrptr);
191
192         BL::RenderSettings r = b_scene.render();
193         BL::RenderResult::layers_iterator b_iter;
194         BL::RenderLayers b_rr_layers(r.ptr);
195         
196         /* render each layer */
197         for(b_rr.layers.begin(b_iter); b_iter != b_rr.layers.end(); ++b_iter) {
198                 /* set layer */
199                 b_rlay = *b_iter;
200
201                 /* add passes */
202                 vector<Pass> passes;
203                 Pass::add(PASS_COMBINED, passes);
204
205                 if(session_params.device.advanced_shading) {
206                         BL::RenderLayer::passes_iterator b_pass_iter;
207                         
208                         for(b_rlay.passes.begin(b_pass_iter); b_pass_iter != b_rlay.passes.end(); ++b_pass_iter) {
209                                 BL::RenderPass b_pass(*b_pass_iter);
210                                 PassType pass_type = get_pass_type(b_pass);
211
212                                 if(pass_type != PASS_NONE)
213                                         Pass::add(pass_type, passes);
214                         }
215                 }
216
217                 buffer_params.passes = passes;
218                 scene->film->passes = passes;
219                 scene->film->tag_update(scene);
220
221                 /* update scene */
222                 sync->sync_data(b_v3d, b_iter->name().c_str());
223
224                 /* update session */
225                 int samples = sync->get_layer_samples();
226                 session->reset(buffer_params, (samples == 0)? session_params.samples: samples);
227
228                 /* render */
229                 session->start();
230                 session->wait();
231
232                 if(session->progress.get_cancel())
233                         break;
234
235                 /* write result */
236                 write_render_result();
237         }
238
239         /* delete render result */
240         RE_engine_end_result((RenderEngine*)b_engine.ptr.data, (RenderResult*)b_rr.ptr.data);
241 }
242
243 void BlenderSession::write_render_result()
244 {
245         /* get state */
246         RenderBuffers *buffers = session->buffers;
247
248         /* copy data from device */
249         if(!buffers->copy_from_device())
250                 return;
251
252         BufferParams& params = buffers->params;
253         float exposure = scene->film->exposure;
254         double total_time, sample_time;
255         int sample;
256
257         session->progress.get_sample(sample, total_time, sample_time);
258
259         vector<float> pixels(params.width*params.height*4);
260
261         /* copy each pass */
262         BL::RenderLayer::passes_iterator b_iter;
263         
264         for(b_rlay.passes.begin(b_iter); b_iter != b_rlay.passes.end(); ++b_iter) {
265                 BL::RenderPass b_pass(*b_iter);
266
267                 /* find matching pass type */
268                 PassType pass_type = get_pass_type(b_pass);
269                 int components = b_pass.channels();
270
271                 /* copy pixels */
272                 if(buffers->get_pass(pass_type, exposure, sample, components, &pixels[0]))
273                         rna_RenderPass_rect_set(&b_pass.ptr, &pixels[0]);
274         }
275
276         /* copy combined pass */
277         if(buffers->get_pass(PASS_COMBINED, exposure, sample, 4, &pixels[0]))
278                 rna_RenderLayer_rect_set(&b_rlay.ptr, &pixels[0]);
279
280         /* tag result as updated */
281         RE_engine_update_result((RenderEngine*)b_engine.ptr.data, (RenderResult*)b_rr.ptr.data);
282 }
283
284 void BlenderSession::synchronize()
285 {
286         /* on session/scene parameter changes, we recreate session entirely */
287         SceneParams scene_params = BlenderSync::get_scene_params(b_scene, background);
288         SessionParams session_params = BlenderSync::get_session_params(b_userpref, b_scene, background);
289
290         if(session->params.modified(session_params) ||
291            scene->params.modified(scene_params)) {
292                 free_session();
293                 create_session();
294                 session->start();
295                 return;
296         }
297
298         /* increase samples, but never decrease */
299         session->set_samples(session_params.samples);
300         session->set_pause(BlenderSync::get_session_pause(b_scene, background));
301
302         /* copy recalc flags, outside of mutex so we can decide to do the real
303            synchronization at a later time to not block on running updates */
304         sync->sync_recalc();
305
306         /* try to acquire mutex. if we don't want to or can't, come back later */
307         if(!session->ready_to_reset() || !session->scene->mutex.try_lock()) {
308                 tag_update();
309                 return;
310         }
311
312         /* data and camera synchronize */
313         sync->sync_data(b_v3d);
314
315         if(b_rv3d)
316                 sync->sync_view(b_v3d, b_rv3d, width, height);
317         else
318                 sync->sync_camera(b_engine.camera_override(), width, height);
319
320         /* unlock */
321         session->scene->mutex.unlock();
322
323         /* reset if needed */
324         if(scene->need_reset()) {
325                 BufferParams buffer_params = BlenderSync::get_buffer_params(b_scene, b_rv3d, width, height);
326                 session->reset(buffer_params, session_params.samples);
327         }
328 }
329
330 bool BlenderSession::draw(int w, int h)
331 {
332         /* before drawing, we verify camera and viewport size changes, because
333            we do not get update callbacks for those, we must detect them here */
334         if(session->ready_to_reset()) {
335                 bool reset = false;
336
337                 /* try to acquire mutex. if we can't, come back later */
338                 if(!session->scene->mutex.try_lock()) {
339                         tag_update();
340                 }
341                 else {
342                         /* update camera from 3d view */
343                         bool need_update = scene->camera->need_update;
344
345                         sync->sync_view(b_v3d, b_rv3d, w, h);
346
347                         if(scene->camera->need_update && !need_update)
348                                 reset = true;
349
350                         session->scene->mutex.unlock();
351                 }
352
353                 /* if dimensions changed, reset */
354                 if(width != w || height != h) {
355                         width = w;
356                         height = h;
357                         reset = true;
358                 }
359
360                 /* reset if requested */
361                 if(reset) {
362                         SessionParams session_params = BlenderSync::get_session_params(b_userpref, b_scene, background);
363                         BufferParams buffer_params = BlenderSync::get_buffer_params(b_scene, b_rv3d, width, height);
364
365                         session->reset(buffer_params, session_params.samples);
366                 }
367         }
368
369         /* update status and progress for 3d view draw */
370         update_status_progress();
371
372         /* draw */
373         BufferParams buffer_params = BlenderSync::get_buffer_params(b_scene, b_rv3d, width, height);
374
375         return !session->draw(buffer_params);
376 }
377
378 void BlenderSession::get_status(string& status, string& substatus)
379 {
380         session->progress.get_status(status, substatus);
381 }
382
383 void BlenderSession::get_progress(float& progress, double& total_time)
384 {
385         double sample_time;
386         int sample;
387
388         session->progress.get_sample(sample, total_time, sample_time);
389         progress = ((float)sample/(float)session->params.samples);
390 }
391
392 void BlenderSession::update_status_progress()
393 {
394         string timestatus, status, substatus;
395         float progress;
396         double total_time;
397         char time_str[128];
398
399         get_status(status, substatus);
400         get_progress(progress, total_time);
401
402         BLI_timestr(total_time, time_str);
403         timestatus = "Elapsed: " + string(time_str) + " | ";
404
405         if(substatus.size() > 0)
406                 status += " | " + substatus;
407
408         if(status != last_status) {
409                 RE_engine_update_stats((RenderEngine*)b_engine.ptr.data, "", (timestatus + status).c_str());
410                 last_status = status;
411         }
412         if(progress != last_progress) {
413                 RE_engine_update_progress((RenderEngine*)b_engine.ptr.data, progress);
414                 last_progress = progress;
415         }
416 }
417
418 void BlenderSession::tag_update()
419 {
420         /* tell blender that we want to get another update callback */
421         engine_tag_update((RenderEngine*)b_engine.ptr.data);
422 }
423
424 void BlenderSession::tag_redraw()
425 {
426         if(background) {
427                 /* update stats and progress, only for background here because
428                    in 3d view we do it in draw for thread safety reasons */
429                 update_status_progress();
430
431                 /* offline render, redraw if timeout passed */
432                 if(time_dt() - last_redraw_time > 1.0f) {
433                         write_render_result();
434                         engine_tag_redraw((RenderEngine*)b_engine.ptr.data);
435                         last_redraw_time = time_dt();
436                 }
437         }
438         else {
439                 /* tell blender that we want to redraw */
440                 engine_tag_redraw((RenderEngine*)b_engine.ptr.data);
441         }
442 }
443
444 void BlenderSession::test_cancel()
445 {
446         /* test if we need to cancel rendering */
447         if(background)
448                 if(RE_engine_test_break((RenderEngine*)b_engine.ptr.data))
449                         session->progress.set_cancel("Cancelled");
450 }
451
452 CCL_NAMESPACE_END
453