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