svn merge ^/trunk/blender -r43934:43976
[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   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(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
162                 case BL::RenderPass::type_DIFFUSE:
163                 case BL::RenderPass::type_SHADOW:
164                 case BL::RenderPass::type_AO:
165                 case BL::RenderPass::type_COLOR:
166                 case BL::RenderPass::type_REFRACTION:
167                 case BL::RenderPass::type_SPECULAR:
168                 case BL::RenderPass::type_REFLECTION:
169                 case BL::RenderPass::type_VECTOR:
170                 case BL::RenderPass::type_MIST:
171                         return PASS_NONE;
172         }
173         
174         return PASS_NONE;
175 }
176
177 void BlenderSession::render()
178 {
179         /* get buffer parameters */
180         SessionParams session_params = BlenderSync::get_session_params(b_userpref, b_scene, background);
181         BufferParams buffer_params = BlenderSync::get_buffer_params(b_scene, b_rv3d, width, height);
182         int w = buffer_params.width, h = buffer_params.height;
183
184         /* create render result */
185         RenderResult *rrp = RE_engine_begin_result((RenderEngine*)b_engine.ptr.data, 0, 0, w, h);
186         PointerRNA rrptr;
187         RNA_pointer_create(NULL, &RNA_RenderResult, rrp, &rrptr);
188         b_rr = BL::RenderResult(rrptr);
189
190         BL::RenderSettings r = b_scene.render();
191         BL::RenderResult::layers_iterator b_iter;
192         BL::RenderLayers b_rr_layers(r.ptr);
193         
194         /* render each layer */
195         for(b_rr.layers.begin(b_iter); b_iter != b_rr.layers.end(); ++b_iter) {
196                 /* set layer */
197                 b_rlay = *b_iter;
198
199                 /* add passes */
200                 vector<Pass> passes;
201                 Pass::add(PASS_COMBINED, passes);
202
203                 if(session_params.device.advanced_shading) {
204                         BL::RenderLayer::passes_iterator b_pass_iter;
205                         
206                         for(b_rlay.passes.begin(b_pass_iter); b_pass_iter != b_rlay.passes.end(); ++b_pass_iter) {
207                                 BL::RenderPass b_pass(*b_pass_iter);
208                                 PassType pass_type = get_pass_type(b_pass);
209
210                                 if(pass_type != PASS_NONE)
211                                         Pass::add(pass_type, passes);
212                         }
213                 }
214
215                 buffer_params.passes = passes;
216                 scene->film->passes = passes;
217                 scene->film->tag_update(scene);
218
219                 /* update session */
220                 session->reset(buffer_params, session_params.samples);
221
222                 /* update scene */
223                 sync->sync_data(b_v3d, b_iter->name().c_str());
224
225                 /* render */
226                 session->start();
227                 session->wait();
228
229                 if(session->progress.get_cancel())
230                         break;
231
232                 /* write result */
233                 write_render_result();
234         }
235
236         /* delete render result */
237         RE_engine_end_result((RenderEngine*)b_engine.ptr.data, (RenderResult*)b_rr.ptr.data);
238 }
239
240 void BlenderSession::write_render_result()
241 {
242         /* get state */
243         RenderBuffers *buffers = session->buffers;
244
245         /* copy data from device */
246         if(!buffers->copy_from_device())
247                 return;
248
249         BufferParams& params = buffers->params;
250         float exposure = scene->film->exposure;
251         double total_time, sample_time;
252         int sample;
253
254         session->progress.get_sample(sample, total_time, sample_time);
255
256         vector<float> pixels(params.width*params.height*4);
257
258         /* copy each pass */
259         BL::RenderLayer::passes_iterator b_iter;
260         
261         for(b_rlay.passes.begin(b_iter); b_iter != b_rlay.passes.end(); ++b_iter) {
262                 BL::RenderPass b_pass(*b_iter);
263
264                 /* find matching pass type */
265                 PassType pass_type = get_pass_type(b_pass);
266                 int components = b_pass.channels();
267
268                 /* copy pixels */
269                 if(buffers->get_pass(pass_type, exposure, sample, components, &pixels[0]))
270                         rna_RenderPass_rect_set(&b_pass.ptr, &pixels[0]);
271         }
272
273         /* copy combined pass */
274         if(buffers->get_pass(PASS_COMBINED, exposure, sample, 4, &pixels[0]))
275                 rna_RenderLayer_rect_set(&b_rlay.ptr, &pixels[0]);
276
277         /* tag result as updated */
278         RE_engine_update_result((RenderEngine*)b_engine.ptr.data, (RenderResult*)b_rr.ptr.data);
279 }
280
281 void BlenderSession::synchronize()
282 {
283         /* on session/scene parameter changes, we recreate session entirely */
284         SceneParams scene_params = BlenderSync::get_scene_params(b_scene, background);
285         SessionParams session_params = BlenderSync::get_session_params(b_userpref, b_scene, background);
286
287         if(session->params.modified(session_params) ||
288            scene->params.modified(scene_params)) {
289                 free_session();
290                 create_session();
291                 session->start();
292                 return;
293         }
294
295         /* increase samples, but never decrease */
296         session->set_samples(session_params.samples);
297         session->set_pause(BlenderSync::get_session_pause(b_scene, background));
298
299         /* copy recalc flags, outside of mutex so we can decide to do the real
300            synchronization at a later time to not block on running updates */
301         sync->sync_recalc();
302
303         /* try to acquire mutex. if we don't want to or can't, come back later */
304         if(!session->ready_to_reset() || !session->scene->mutex.try_lock()) {
305                 tag_update();
306                 return;
307         }
308
309         /* data and camera synchronize */
310         sync->sync_data(b_v3d);
311
312         if(b_rv3d)
313                 sync->sync_view(b_v3d, b_rv3d, width, height);
314         else
315                 sync->sync_camera(width, height);
316
317         /* unlock */
318         session->scene->mutex.unlock();
319
320         /* reset if needed */
321         if(scene->need_reset()) {
322                 BufferParams buffer_params = BlenderSync::get_buffer_params(b_scene, b_rv3d, width, height);
323                 session->reset(buffer_params, session_params.samples);
324         }
325 }
326
327 bool BlenderSession::draw(int w, int h)
328 {
329         /* before drawing, we verify camera and viewport size changes, because
330            we do not get update callbacks for those, we must detect them here */
331         if(session->ready_to_reset()) {
332                 bool reset = false;
333
334                 /* try to acquire mutex. if we can't, come back later */
335                 if(!session->scene->mutex.try_lock()) {
336                         tag_update();
337                 }
338                 else {
339                         /* update camera from 3d view */
340                         bool need_update = scene->camera->need_update;
341
342                         sync->sync_view(b_v3d, b_rv3d, w, h);
343
344                         if(scene->camera->need_update && !need_update)
345                                 reset = true;
346
347                         session->scene->mutex.unlock();
348                 }
349
350                 /* if dimensions changed, reset */
351                 if(width != w || height != h) {
352                         width = w;
353                         height = h;
354                         reset = true;
355                 }
356
357                 /* reset if requested */
358                 if(reset) {
359                         SessionParams session_params = BlenderSync::get_session_params(b_userpref, b_scene, background);
360                         BufferParams buffer_params = BlenderSync::get_buffer_params(b_scene, b_rv3d, width, height);
361
362                         session->reset(buffer_params, session_params.samples);
363                 }
364         }
365
366         /* update status and progress for 3d view draw */
367         update_status_progress();
368
369         /* draw */
370         BufferParams buffer_params = BlenderSync::get_buffer_params(b_scene, b_rv3d, width, height);
371
372         return !session->draw(buffer_params);
373 }
374
375 void BlenderSession::get_status(string& status, string& substatus)
376 {
377         session->progress.get_status(status, substatus);
378 }
379
380 void BlenderSession::get_progress(float& progress, double& total_time)
381 {
382         double sample_time;
383         int sample;
384
385         session->progress.get_sample(sample, total_time, sample_time);
386         progress = ((float)sample/(float)session->params.samples);
387 }
388
389 void BlenderSession::update_status_progress()
390 {
391         string timestatus, status, substatus;
392         float progress;
393         double total_time;
394         char time_str[128];
395
396         get_status(status, substatus);
397         get_progress(progress, total_time);
398
399         BLI_timestr(total_time, time_str);
400         timestatus = "Elapsed: " + string(time_str) + " | ";
401
402         if(substatus.size() > 0)
403                 status += " | " + substatus;
404
405         if(status != last_status) {
406                 RE_engine_update_stats((RenderEngine*)b_engine.ptr.data, "", (timestatus + status).c_str());
407                 last_status = status;
408         }
409         if(progress != last_progress) {
410                 RE_engine_update_progress((RenderEngine*)b_engine.ptr.data, progress);
411                 last_progress = progress;
412         }
413 }
414
415 void BlenderSession::tag_update()
416 {
417         /* tell blender that we want to get another update callback */
418         engine_tag_update((RenderEngine*)b_engine.ptr.data);
419 }
420
421 void BlenderSession::tag_redraw()
422 {
423         if(background) {
424                 /* update stats and progress, only for background here because
425                    in 3d view we do it in draw for thread safety reasons */
426                 update_status_progress();
427
428                 /* offline render, redraw if timeout passed */
429                 if(time_dt() - last_redraw_time > 1.0f) {
430                         write_render_result();
431                         engine_tag_redraw((RenderEngine*)b_engine.ptr.data);
432                         last_redraw_time = time_dt();
433                 }
434         }
435         else {
436                 /* tell blender that we want to redraw */
437                 engine_tag_redraw((RenderEngine*)b_engine.ptr.data);
438         }
439 }
440
441 void BlenderSession::test_cancel()
442 {
443         /* test if we need to cancel rendering */
444         if(background)
445                 if(RE_engine_test_break((RenderEngine*)b_engine.ptr.data))
446                         session->progress.set_cancel("Cancelled");
447 }
448
449 CCL_NAMESPACE_END
450